From e6ed9be1287d9ae56e42ee7a8390d79a2dc93d61 Mon Sep 17 00:00:00 2001 From: Lukasz Stanislawski Date: Wed, 18 Jul 2018 14:24:32 +0200 Subject: [PATCH] stats: rename to sys-stats + refactor Make stats module handle only system-wide statistics. Change-Id: If9b391e7a635ec3185a3459a25ed49c758721f98 --- src/CMakeLists.txt | 2 +- src/proc-scanner.c | 2 +- src/process.c | 43 +++++++++++- src/process.h | 33 +++++++-- src/report-generator.c | 63 +++++++++--------- src/stats.c | 177 ------------------------------------------------- src/stats.h | 151 ----------------------------------------- src/sys-stats.c | 95 ++++++++++++++++++++++++++ src/sys-stats.h | 72 ++++++++++++++++++++ src/task-worker.c | 8 +-- 10 files changed, 272 insertions(+), 374 deletions(-) delete mode 100644 src/stats.c delete mode 100644 src/stats.h create mode 100644 src/sys-stats.c create mode 100644 src/sys-stats.h diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 422cfed..dfbd9e3 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -26,7 +26,7 @@ SET(SRCS config-deserializer.c appinfo-provider.c report-json-serializer.c - stats.c + sys-stats.c clock.c ipc.c process.c diff --git a/src/proc-scanner.c b/src/proc-scanner.c index b2fdf9e..336a23f 100644 --- a/src/proc-scanner.c +++ b/src/proc-scanner.c @@ -140,7 +140,7 @@ static bool _proc_scanner_read_pid(int pid, void *user_data) struct process *proc = _proc_scanner_find_process_in_history(scanner, pid); if (!proc) { - process_init(pid, &proc_new); + process_init_process(pid, &proc_new); proc = &proc_new; } diff --git a/src/process.c b/src/process.c index 70f4bb0..17159f7 100644 --- a/src/process.c +++ b/src/process.c @@ -1,11 +1,18 @@ #include #include +#include #include "process.h" +#include "clock.h" +#include "sys-stats.h" #include "err-check.h" #include "procfs.h" #include "appinfo-provider.h" +static struct sys_stats sys; +static int pagesize; +static float timescale; + int process_get_memory_usage(struct process *proc, int *usage) { ON_NULL_RETURN_VAL(proc, -1); @@ -35,12 +42,15 @@ int process_update(struct process *proc) ticks = info.stime + info.utime; proc->frame_ticks_used = ticks - proc->total_ticks_used; proc->total_ticks_used = ticks; - proc->memory_used = info.rss * stats_get_page_size() / 1024; + proc->memory_used = info.rss * pagesize / 1024; + float now = clock_monotonic_get(); + proc->frame_time_inverted = 1.0f / (now - proc->update_time); + proc->update_time = now; return 0; } -void process_init(int pid, struct process *proc) +void process_init_process(int pid, struct process *proc) { memset(proc, 0x0, sizeof(struct process)); proc->pid = pid; @@ -87,3 +97,32 @@ void process_move(struct process *dst, struct process *src) src->appid = NULL; src->exe = NULL; } + +int process_get_cpu_usage_percentage(struct process *proc, float *usage) +{ + ON_NULL_RETURN_VAL(proc, -1); + ON_NULL_RETURN_VAL(usage, -1); + + *usage = (float)proc->frame_ticks_used * proc->frame_time_inverted * timescale; + return 0; +} + +int process_get_memory_usage_percentage(struct process *proc, float *usage) +{ + ON_NULL_RETURN_VAL(proc, -1); + ON_NULL_RETURN_VAL(usage, -1); + + *usage = (float)proc->memory_used / sys.total_memory; + return 0; +} + +int process_init() +{ + if (sys_stats_update(&sys) != 0) { + ERR("stats_update_system_stats failed."); + return -1; + } + timescale = 1.0f / (float)sysconf(_SC_CLK_TCK); + pagesize = getpagesize(); + return 0; +} diff --git a/src/process.h b/src/process.h index 7d13938..12344ee 100644 --- a/src/process.h +++ b/src/process.h @@ -17,10 +17,6 @@ #ifndef __PROCESS_H #define __PROCESS_H -#include - -#include "stats.h" - /** * @brief The process structure. * @@ -35,9 +31,16 @@ struct process unsigned long long total_ticks_used; unsigned long long frame_ticks_used; int memory_used; + float update_time; + float frame_time_inverted; }; /** + * @brief Initialize process module. + */ +int process_init(); + +/** * @brief Gets last read process memory usage * * @param[in]: proc process @@ -74,7 +77,7 @@ int process_update(struct process *proc); * * @return 0 on success, other value on error. */ -void process_init(int pid, struct process *proc); +void process_init_process(int pid, struct process *proc); /** * @brief Shutdown process structure @@ -122,4 +125,24 @@ const char *process_get_exe(struct process *proc); */ void process_move(struct process *dst, struct process *src); +/** + * @brief Gets average CPU usage percentage since last update. + * + * @param[in]: proc process + * @param[out]: usage the cpu usage percentage + * + * @return 0 on success, other value on error. + */ +int process_get_cpu_usage_percentage(struct process *proc, float *usage); + +/** + * @brief Gets average memory usage percentage on last update. + * + * @param[in]: proc process + * @param[out]: usage the memory usage percentage + * + * @return 0 on success, other value on error. + */ +int process_get_memory_usage_percentage(struct process *dst, float *usage); + #endif diff --git a/src/report-generator.c b/src/report-generator.c index 3901899..c6e761f 100644 --- a/src/report-generator.c +++ b/src/report-generator.c @@ -24,21 +24,24 @@ #include "log.h" #include "err-check.h" #include "appinfo-provider.h" -#include "stats.h" +#include "sys-stats.h" #include "clock.h" #include "proc-scanner.h" +#include "process.h" struct report_generator_system { /** system cpu usage statistics */ - struct stats_system previous; + struct sys_stats stats; }; struct report_generator_process { /** process pid */ int pid; - /** process cpu usage statistics */ - struct stats_process previous; + /** process statistics */ + struct process proc; + /** update time */ + float proc_update_time; }; struct report_generator_app @@ -49,14 +52,14 @@ struct report_generator_app struct report_generator_top { - struct stats_system sys_stats; + struct sys_stats sys_stats; proc_scanner_t *scanner; report_generator_top_type_e type; int limit; }; struct report_generator_top_closure { - struct stats_system sys_stats; + struct sys_stats sys_stats; int current_index; int max_index; struct process_usage_report *usage_report; @@ -71,7 +74,7 @@ report_generator_system_t *report_generator_new_system_report_generator() if (!ret) return NULL; - if (stats_update_system_stats(&ret->previous) != 0) { + if (sys_stats_update(&ret->stats) != 0) { ERR("stats_update_system_stats failed"); free(ret); return NULL; @@ -96,12 +99,15 @@ report_generator_process_t *report_generator_new_process_report_generator(int pi if (!ret) return NULL; - if (stats_update_process_stats(pid, &ret->previous) != 0) { - ERR("stats_update_process_stats failed."); + process_init(pid, &ret->proc); + + if (process_update(&ret->proc) != 0) { + ERR("process_update failed."); free(ret); return NULL; }; + ret->proc_update_time = clock_monotonic_get(); ret->pid = pid; return ret; @@ -146,14 +152,13 @@ int report_generator_generate_system_cpu_usage_report( ON_NULL_RETURN_VAL(report, -1); float usage; - struct stats_system current; - if (stats_update_system_stats(¤t) != 0) { + if (sys_stats_update(&generator->stats) != 0) { ERR("stats_update_system_stats failed."); return -1; } - if (stats_get_system_cpu_usage_average(&generator->previous, ¤t, &usage)) + if (sys_stats_get_cpu_usage_percentage(&generator->stats, &usage)) { ERR("stats_get_system_cpu_usage_average failed"); return -1; @@ -162,8 +167,6 @@ int report_generator_generate_system_cpu_usage_report( report->usage = usage; report->time = clock_realtime_get(); - generator->previous = current; - return 0; } @@ -175,12 +178,12 @@ int report_generator_generate_system_memory_usage_report( float usage; - if (stats_update_system_stats(&generator->previous) != 0) { + if (sys_stats_update(&generator->stats) != 0) { ERR("stats_update_system_stats failed."); return -1; } - if (stats_get_system_memory_usage(&generator->previous, &usage) != 0) { + if (sys_stats_get_memory_usage_percentage(&generator->stats, &usage) != 0) { ERR("stats_get_system_memory_usage failed."); return -1; } @@ -198,16 +201,15 @@ int report_generator_generate_process_cpu_usage_report( ON_NULL_RETURN_VAL(generator, -1); ON_NULL_RETURN_VAL(report, -1); - struct stats_process current = {0,}; float usage; - if (stats_update_process_stats(generator->pid, ¤t) != 0) { - ERR("stats_update_process_stats failed."); + if (process_update(&generator->proc) != 0) { + ERR("process_update failed."); return -1; } - if (stats_get_process_cpu_usage_average(&generator->previous, ¤t, &usage) ) { - ERR("stats_update_process_stats failed."); + if (process_get_cpu_usage_percentage(&generator->proc, &usage) != 0) { + ERR("process_get_cpu_usage_percentage failed."); return -1; } @@ -215,8 +217,6 @@ int report_generator_generate_process_cpu_usage_report( report->pid = generator->pid; report->usage = usage; - generator->previous = current; - return 0; } @@ -229,8 +229,8 @@ int report_generator_generate_process_memory_usage_report( float usage; - if (stats_get_process_memory_usage(generator->pid, &usage) != 0) { - ERR("stats_get_process_memory_usage failed."); + if (process_get_memory_usage_percentage(&generator->proc, &usage) != 0) { + ERR("process_get_memory_usage_percentage failed."); return -1; } @@ -311,7 +311,7 @@ int report_generator_generate_load_average_report(struct system_load_average_rep float a1, a5, a15; - if (stats_get_load_averages(&a1, &a5, &a15) != 0) { + if (sys_stats_get_load_averages(&a1, &a5, &a15) != 0) { ERR("stats_get_load_averages failed."); return -1; } @@ -344,7 +344,7 @@ report_generator_top_t *report_generator_new_top_report_generator(report_generat report_generator_free_top_generator(gen); return NULL; } - if (stats_update_system_stats(&gen->sys_stats) != 0) { + if (sys_stats_update(&gen->sys_stats) != 0) { report_generator_free_top_generator(gen); return NULL; } @@ -389,15 +389,12 @@ static bool _append_to_cpu_report(struct process *proc, void *data) struct report_generator_top_closure *closure = data; struct process_usage_report report = {0,}; const char *appid; - unsigned long long ticks = 0; if (closure->current_index >= closure->max_index) return false; - if (process_get_cpu_usage(proc, &ticks) != 0) { + if (process_get_cpu_usage_percentage(proc, &report.usage) != 0) { report.usage = NAN; - } else { - report.usage = stats_get_cpu_usage_percentage(ticks, closure->sys_stats.frame_time_inverted); } appid = process_get_appid(proc); @@ -498,7 +495,7 @@ int report_generator_generate_top_cpu_report( struct report_generator_top_closure closure = {0,}; - if (stats_update_system_stats(&generator->sys_stats) != 0) { + if (sys_stats_update(&generator->sys_stats) != 0) { return -1; } @@ -539,7 +536,7 @@ int report_generator_generate_top_memory_report( struct report_generator_top_closure closure = {0,}; - if (stats_update_system_stats(&generator->sys_stats) != 0) { + if (sys_stats_update(&generator->sys_stats) != 0) { return -1; } diff --git a/src/stats.c b/src/stats.c deleted file mode 100644 index ef22b1b..0000000 --- a/src/stats.c +++ /dev/null @@ -1,177 +0,0 @@ -/* - * Copyright (c) 2018 Samsung Electronics Co., Ltd. - * - * Licensed under the Flora License, Version 1.1 (the License); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://floralicense.org/license/ - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an AS IS BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include - -#include "stats.h" -#include "procfs.h" -#include "err-check.h" -#include "clock.h" - -static int ncpus; -static float timescale; - -int stats_get_system_cpu_usage_average(struct stats_system *previous, struct stats_system *current, float *usage) -{ - struct stats_system diff; - - diff.busy_ticks = current->busy_ticks > previous->busy_ticks ? current->busy_ticks - previous->busy_ticks : 0; - - diff.total_ticks = current->total_ticks > previous->total_ticks ? current->total_ticks - previous->total_ticks : 0; - - if (diff.total_ticks == 0) { - *usage = 0; - } else { - *usage = (float)diff.busy_ticks / diff.total_ticks; - } - - return 0; -} - -int stats_update_system_stats(struct stats_system *sys) -{ - ON_NULL_RETURN_VAL(sys, -1); - - struct procfs_stat cpu_info; - struct procfs_meminfo mem_info; - - if (procfs_read_stat(&cpu_info) != 0) { - return -1; - } - - if (procfs_read_meminfo(&mem_info) != 0) { - ERR("procfs_read_meminfo failed."); - return -1; - } - - sys->busy_ticks = cpu_info.user + cpu_info.system + cpu_info.nice + cpu_info.irq + cpu_info.softirq; - sys->total_ticks = cpu_info.user + cpu_info.system + cpu_info.nice + cpu_info.irq + cpu_info.softirq + cpu_info.idle + cpu_info.iowait; - sys->memory_used = mem_info.used; - sys->total_memory = mem_info.total; - - float now = clock_monotonic_get(); - sys->frame_time_inverted = 1.0f / (now - sys->update_time); - sys->update_time = now; - - return 0; -} - -int stats_get_system_memory_usage(struct stats_system *sys, float *usage) -{ - ON_NULL_RETURN_VAL(usage, -1); - - *usage = (float)sys->memory_used / sys->total_memory; - - return 0; -} - -int stats_update_process_stats(int pid, struct stats_process *stats) -{ - ON_TRUE_RETURN_VAL(pid < 0, -1); - ON_NULL_RETURN_VAL(stats, -1); - - struct procfs_process_stat proc_info; - struct procfs_stat sys_info; - - if (procfs_read_process_stat(pid, &proc_info) != 0) { - ERR("procfs_read_process_stat failed."); - return -1; - } - - if (procfs_read_stat(&sys_info) != 0) { - ERR("procfs_read_stat failed."); - return -1; - } - - stats->process_ticks = proc_info.stime + proc_info.utime; - stats->system_ticks = sys_info.user + sys_info.system + sys_info.nice + sys_info.idle; - - return 0; -} - -int stats_get_process_cpu_usage_average(struct stats_process *previous, struct stats_process *current, float *usage) -{ - struct stats_process diff; - - diff.process_ticks = current->process_ticks > previous->process_ticks ? current->process_ticks - previous->process_ticks: 0; - diff.system_ticks = current->system_ticks > previous->system_ticks ? current->system_ticks - previous->system_ticks : 0; - - if (diff.system_ticks == 0) - *usage = 0; - else - *usage = (float)diff.process_ticks / diff.system_ticks * ncpus; - - return 0; -} - -int stats_get_process_memory_usage(int pid, float *usage) -{ - ON_TRUE_RETURN_VAL(pid < 0, -1); - ON_NULL_RETURN_VAL(usage, -1); - - struct procfs_process_smaps mem_info; - struct procfs_meminfo sys_meminfo; - - if (procfs_read_process_smaps(pid, &mem_info) != 0) { - ERR("procfs_read_process_smaps failed."); - return -1; - } - - if (procfs_read_meminfo(&sys_meminfo) != 0) { - ERR("procfs_read_meminfo failed."); - return -1; - } - - *usage = sys_meminfo.total > 0 ? (float)mem_info.rss / sys_meminfo.total : 0; - - return 0; -} - -int stats_get_load_averages(float *a1, float *a5, float *a15) -{ - struct procfs_loadavg info; - - if (procfs_read_loadavg(&info) != 0) { - ERR("procfs_read_loadavg failed."); - return -1; - } - - if (a1) *a1 = info.one_min_avg; - if (a5) *a5 = info.five_min_avg; - if (a15) *a15 = info.fifteen_min_avg; - - return 0; -} - -float stats_get_cpu_usage_percentage(unsigned long long ticks_delta, float ticks_time_inverted) -{ - return (float)ticks_delta * ticks_time_inverted * timescale; -} - -int stats_init() -{ - if (procfs_read_cpu_possible(&ncpus) != 0) { - ERR("procfs_read_cpu_possible failed."); - return -1; - } - timescale = 1.0f / (float)sysconf(_SC_CLK_TCK); - return 0; -} - -int stats_get_page_size() -{ - return getpagesize(); -} diff --git a/src/stats.h b/src/stats.h deleted file mode 100644 index 5ee9956..0000000 --- a/src/stats.h +++ /dev/null @@ -1,151 +0,0 @@ -/* - * Copyright (c) 2018 Samsung Electronics Co., Ltd. - * - * Licensed under the Flora License, Version 1.1 (the License); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://floralicense.org/license/ - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an AS IS BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef _STATS_H_ -#define _STATS_H_ - -#include - -/** - * @brief System's statistics snapshot - */ -struct stats_system -{ - unsigned long long busy_ticks; - unsigned long long total_ticks; - unsigned long long memory_used; - unsigned long long total_memory; - float update_time; - float frame_time_inverted; -}; - -/** - * @brief Process's statistics snapshot - */ -struct stats_process -{ - unsigned long long system_ticks; - unsigned long long process_ticks; -}; - -/** - * @brief Initializes stats module. - * - * @return: 0 on success, other value on error. - * - * @note function should be called before any other function call from this - * module. - */ -int stats_init(); - -/** - * @brief Calculates average cpu usage between two stats snapshots. - * - * @param[in] previous the stats snapshots taken before current - * @param[in] current the stats snapshots taken after previous - * @param[out] usage the cpu usage as percent. - * - * @return: 0 on success, other value on error - */ -int stats_get_system_cpu_usage_average(struct stats_system *previous, struct stats_system *current, float *usage); - -/** - * @brief Takes system statistics snapshot. - * - * @param[out] stats System's statistics snapshot. - * - * @return: 0 on success, other value on error - */ -int stats_update_system_stats(struct stats_system *stats); - -/** - * @brief Calculates system memory usage. - * - * @param[out] usage the memory usage as percent. - * - * @return: 0 on success, other value on error - */ -int stats_get_system_memory_usage(struct stats_system *stats, float *usage); - -/** - * @brief Takes process statistics snapshot. - * - * @param[in] pid the process id. - * @param[out] stats process statistcs. - * - * @return: 0 on success, other value on error - */ -int stats_update_process_stats(int pid, struct stats_process *stats); - -/** - * @brief Calculates average process cpu usage between two stats snapshots. - * - * @param[in] previous the stats snapshots taken before current - * @param[in] current the stats snapshots taken after previous - * @param[out] usage the cpu usage as percent. It may be greater then 100% in - * case of multithreaded applications. - * - * @note in case when process has 2 threads which runs tight loop, the function - * will report 200% usage. - * - * @return: 0 on success, other value on error - */ -int stats_get_process_cpu_usage_average(struct stats_process *previous, struct stats_process *current, float *usage); - -/** - * @brief Calculates process memory usage. - * - * @param[in] pid the process id. - * @param[out] usage process memory usage. - * - * @return: 0 on success, other value on error - */ -int stats_get_process_memory_usage(int pid, float *usage); - -/** - * @brief Gets system load averages stats. - * - * @param[out] a1 one minute average. - * @param[out] a5 five minute average. - * @param[out] a15 fifteen minute average. - * - * @return: 0 on success, other value on error - */ -int stats_get_load_averages(float *a1, float *a5, float *a15); - -/** - * @brief Gets CPU usage percentage - * - * @param[in] ticks_delta the amount of clock ticks - * @param[in] ticks_time_inverted the time of ticks_delete inverted ^-1 - * - * @return: CPU usage perecentage - * - * @note in case when process has 2 threads which runs tight loop, the function - * will report 200% usage (2.0f). - */ -float stats_get_cpu_usage_percentage(unsigned long long ticks_delta, float ticks_time_inverted); - -/** - * @brief Get system's page size in bytes. - * - * @return page size - */ -int stats_get_page_size(); - -#endif - - diff --git a/src/sys-stats.c b/src/sys-stats.c new file mode 100644 index 0000000..bcd3812 --- /dev/null +++ b/src/sys-stats.c @@ -0,0 +1,95 @@ +/* + * Copyright (c) 2018 Samsung Electronics Co., Ltd. + * + * Licensed under the Flora License, Version 1.1 (the License); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://floralicense.org/license/ + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an AS IS BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include + +#include "sys-stats.h" +#include "procfs.h" +#include "err-check.h" +#include "clock.h" + +int sys_stats_get_cpu_usage_percentage(struct sys_stats *stats, float *usage) +{ + ON_NULL_RETURN_VAL(stats, -1); + ON_NULL_RETURN_VAL(usage, -1); + + if (stats->total_ticks_delta == 0) { + *usage = 0; + } else { + *usage = (float)stats->busy_ticks_delta / stats->total_ticks_delta; + } + + return 0; +} + +int sys_stats_update(struct sys_stats *sys) +{ + ON_NULL_RETURN_VAL(sys, -1); + + struct procfs_stat cpu_info; + struct procfs_meminfo mem_info; + unsigned long long ticks; + + if (procfs_read_stat(&cpu_info) != 0) { + ERR("procfs_read_stat failed."); + return -1; + } + + if (procfs_read_meminfo(&mem_info) != 0) { + ERR("procfs_read_meminfo failed."); + return -1; + } + + ticks = cpu_info.user + cpu_info.system + cpu_info.nice + cpu_info.irq + cpu_info.softirq; + sys->busy_ticks_delta = ticks > sys->busy_ticks ? ticks - sys->busy_ticks : 0; + sys->busy_ticks = ticks; + + ticks = cpu_info.user + cpu_info.system + cpu_info.nice + cpu_info.irq + + cpu_info.softirq + cpu_info.idle + cpu_info.iowait; + sys->total_ticks_delta = ticks > sys->total_ticks ? ticks - sys->total_ticks : 0; + sys->total_ticks = ticks; + + sys->memory_used = mem_info.used; + sys->total_memory = mem_info.total; + + return 0; +} + +int sys_stats_get_memory_usage_percentage(struct sys_stats *sys, float *usage) +{ + ON_NULL_RETURN_VAL(sys, -1); + ON_NULL_RETURN_VAL(usage, -1); + + *usage = (float)sys->memory_used / sys->total_memory; + + return 0; +} + +int sys_stats_get_load_averages(float *a1, float *a5, float *a15) +{ + struct procfs_loadavg info; + + if (procfs_read_loadavg(&info) != 0) { + ERR("procfs_read_loadavg failed."); + return -1; + } + + if (a1) *a1 = info.one_min_avg; + if (a5) *a5 = info.five_min_avg; + if (a15) *a15 = info.fifteen_min_avg; + + return 0; +} diff --git a/src/sys-stats.h b/src/sys-stats.h new file mode 100644 index 0000000..8c2d784 --- /dev/null +++ b/src/sys-stats.h @@ -0,0 +1,72 @@ +/* + * Copyright (c) 2018 Samsung Electronics Co., Ltd. + * + * Licensed under the Flora License, Version 1.1 (the License); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://floralicense.org/license/ + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an AS IS BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef _SYS_STATS_H_ +#define _SYS_STATS_H_ + +/** + * @brief System's statistics snapshot + */ +struct sys_stats +{ + unsigned long long busy_ticks; + unsigned long long busy_ticks_delta; + unsigned long long total_ticks; + unsigned long long total_ticks_delta; + unsigned long long memory_used; + unsigned long long total_memory; +}; + +/** + * @brief Calculates average cpu usage between two stats snapshots. + * + * @param[in] stats the stats snapshots + * @param[out] usage the cpu usage as percent. + * + * @return: 0 on success, other value on error + */ +int sys_stats_get_cpu_usage_percentage(struct sys_stats *stats, float *usage); + +/** + * @brief Takes system statistics snapshot. + * + * @param[out] stats System's statistics snapshot. + * + * @return: 0 on success, other value on error + */ +int sys_stats_update(struct sys_stats *stats); + +/** + * @brief Calculates system memory usage. + * + * @param[out] usage the memory usage as percent. + * + * @return: 0 on success, other value on error + */ +int sys_stats_get_memory_usage_percentage(struct sys_stats *stats, float *usage); + +/** + * @brief Gets system load averages stats. + * + * @param[out] a1 one minute average. + * @param[out] a5 five minute average. + * @param[out] a15 fifteen minute average. + * + * @return: 0 on success, other value on error + */ +int sys_stats_get_load_averages(float *a1, float *a5, float *a15); + +#endif diff --git a/src/task-worker.c b/src/task-worker.c index 37caf1e..6f6a9d3 100644 --- a/src/task-worker.c +++ b/src/task-worker.c @@ -24,7 +24,7 @@ #include "log.h" #include "scheduler.h" #include "task-worker.h" -#include "stats.h" +#include "process.h" #include "ipc.h" static gboolean sigint_handler(gpointer user_data); @@ -59,9 +59,9 @@ int main(int argc, char *argv[]) app_provider_init(); ipc_init(argv[1], task_counter); - if (stats_init() != 0) + if (process_init() != 0) { - ERR("Stats module initialization failed"); + ERR("Process module initialization failed"); g_free(data.current_config); ipc_shutdown(); app_provider_shutdown(); @@ -95,4 +95,4 @@ static gboolean sigint_handler(gpointer user_data) g_main_loop_quit(data.main_loop); return TRUE; -} \ No newline at end of file +} -- 2.7.4