From ad53b4d4c32572625dc35342d8f8b3acb0402320 Mon Sep 17 00:00:00 2001 From: Lukasz Stanislawski Date: Mon, 16 Jul 2018 16:37:43 +0200 Subject: [PATCH] procfs: refactor naming Make procfs functions & structures names map exeacly to filenames from /proc/ Change-Id: Ic3635f4db29be194ebfc97d107fc71cd82d2df1b --- src/process.c | 10 +++++----- src/procfs.c | 25 ++++++++++--------------- src/procfs.h | 45 ++++++++++++++++----------------------------- src/stats.c | 46 +++++++++++++++++++++++----------------------- 4 files changed, 54 insertions(+), 72 deletions(-) diff --git a/src/process.c b/src/process.c index 8f352b7..61f9d1c 100644 --- a/src/process.c +++ b/src/process.c @@ -7,10 +7,10 @@ static int _process_update_cpu_usage(struct process *proc) { - struct procfs_process_cpu_usage_info info; + struct procfs_process_stat info; unsigned long long ticks; - if (procfs_read_process_cpu_usage(proc->pid, &info) != 0) { + if (procfs_read_process_stat(proc->pid, &info) != 0) { return -1; } ticks = info.stime + info.utime; @@ -21,9 +21,9 @@ static int _process_update_cpu_usage(struct process *proc) static int _process_update_memory_usage(struct process *proc) { - struct procfs_process_memory_usage_info info; + struct procfs_process_smaps info; - if (procfs_read_process_memory_usage(proc->pid, &info) != 0) { + if (procfs_read_process_smaps(proc->pid, &info) != 0) { return -1; } proc->memory_used = info.rss; @@ -85,7 +85,7 @@ const char *process_get_exe(struct process *proc) ON_NULL_RETURN_VAL(proc, NULL); if (!proc->exe) { - if (procfs_read_exe(proc->pid, &proc->exe) != 0) + if (procfs_read_process_exe(proc->pid, &proc->exe) != 0) return NULL; } return proc->exe; diff --git a/src/procfs.c b/src/procfs.c index 1296809..54c234a 100644 --- a/src/procfs.c +++ b/src/procfs.c @@ -36,7 +36,7 @@ #define PROC_PID_EXE_PATH "/proc/%d/exe" #define PROC_PID_CMDLINE_PATH "/proc/%d/cmdline" -int procfs_read_system_load_average(struct procfs_load_average_info *info) +int procfs_read_loadavg(struct procfs_loadavg *info) { float a1, a5, a15; @@ -63,12 +63,12 @@ int procfs_read_system_load_average(struct procfs_load_average_info *info) return 0; } -int procfs_read_system_cpu_usage(struct procfs_system_cpu_usage_info *usage) +int procfs_read_stat(struct procfs_stat *usage) { ON_NULL_RETURN_VAL(usage, -1); char line[256]; - struct procfs_system_cpu_usage_info tmp; + struct procfs_stat tmp; const char *prefix = "cpu "; FILE *stat_fp = fopen(STAT_FILEPATH, "r"); @@ -93,12 +93,7 @@ int procfs_read_system_cpu_usage(struct procfs_system_cpu_usage_info *usage) return -1; } -int procfs_read_system_percpu_usage(int max_cpus, struct procfs_system_cpu_usage_info usage[]) -{ - return -1; -} - -int procfs_read_system_memory_usage(struct procfs_system_memory_usage_info *usage) +int procfs_read_meminfo(struct procfs_meminfo *usage) { ON_NULL_RETURN_VAL(usage, -1); @@ -151,7 +146,7 @@ int procfs_read_system_memory_usage(struct procfs_system_memory_usage_info *usag return 0; } -int procfs_read_process_memory_usage(int pid, struct procfs_process_memory_usage_info *usage) +int procfs_read_process_smaps(int pid, struct procfs_process_smaps *usage) { ON_NULL_RETURN_VAL(usage, -1); ON_TRUE_RETURN_VAL(pid <= 0, -1); @@ -167,7 +162,7 @@ int procfs_read_process_memory_usage(int pid, struct procfs_process_memory_usage return -1; } - memset(usage, 0x0, sizeof(struct procfs_process_memory_usage_info)); + memset(usage, 0x0, sizeof(struct procfs_process_smaps)); while (fgets(line, sizeof(line), smaps_fp)) { if (sscanf(line, "Size: %lu", &value) == 1) @@ -191,7 +186,7 @@ int procfs_read_process_memory_usage(int pid, struct procfs_process_memory_usage return 0; } -int procfs_read_process_cpu_usage(int pid, struct procfs_process_cpu_usage_info *usage) +int procfs_read_process_stat(int pid, struct procfs_process_stat *usage) { ON_NULL_RETURN_VAL(usage, -1); ON_TRUE_RETURN_VAL(pid <= 0, -1); @@ -250,7 +245,7 @@ int procfs_read_uptime(unsigned long *uptime) return 0; } -int procfs_read_cpu_count(int *cpu_count) +int procfs_read_cpu_possible(int *cpu_count) { ON_NULL_RETURN_VAL(cpu_count, -1); @@ -319,7 +314,7 @@ int procfs_iterate_pids(procfs_pid_iterator_cb iterator, void *user_data) return 0; } -int procfs_read_exe(int pid, char **exe) +int procfs_read_process_exe(int pid, char **exe) { ON_TRUE_RETURN_VAL(pid < 0, -1); ON_NULL_RETURN_VAL(exe, -1); @@ -352,7 +347,7 @@ int procfs_read_exe(int pid, char **exe) return 0; } -int procfs_read_cmdline(int pid, char **cmdline) +int procfs_read_process_cmdline(int pid, char **cmdline) { ON_TRUE_RETURN_VAL(pid < 0, -1); ON_NULL_RETURN_VAL(cmdline, -1); diff --git a/src/procfs.h b/src/procfs.h index 5de7707..edccf7a 100644 --- a/src/procfs.h +++ b/src/procfs.h @@ -21,9 +21,9 @@ #include /** - * @brief System memory information. + * @brief Meminfo file data. */ -struct procfs_system_memory_usage_info { +struct procfs_meminfo { unsigned long total; /**< Total memory (KiB) */ unsigned long used; /**< Used memory (KiB) */ unsigned long free; /**< Free memory (KiB) */ @@ -34,7 +34,7 @@ struct procfs_system_memory_usage_info { /** * @brief System cpu usage information. */ -struct procfs_system_cpu_usage_info { +struct procfs_stat { unsigned long long user; /** Time running un-niced user processes (clock ticks) */ unsigned long long system; /** Time running kernel process (clock ticks) */ unsigned long long nice; /** Time running niced user processes (clock ticks) */ @@ -50,7 +50,7 @@ struct procfs_system_cpu_usage_info { * http://www.brendangregg.com/blog/2017-08-08/linux-load-averages.html * https://github.com/torvalds/linux/blob/master/kernel/sched/loadavg.c */ -struct procfs_load_average_info { +struct procfs_loadavg { float one_min_avg; /** One minute load average */ float five_min_avg; /** Five minutes load average */ float fifteen_min_avg; /** Fifteen minutes load average */ @@ -59,7 +59,7 @@ struct procfs_load_average_info { /** * @brief Process memory information. */ -struct procfs_process_memory_usage_info { +struct procfs_process_smaps { unsigned long vsz; /** Virtual memory size (KiB) */ unsigned long rss; /** Resident set size (KiB) */ unsigned long pss; /** Proportional set size (KiB) */ @@ -72,7 +72,7 @@ struct procfs_process_memory_usage_info { /** * @brief Process cpu usage information. */ -struct procfs_process_cpu_usage_info { +struct procfs_process_stat { unsigned long long utime; /** Amount of time that this process has been scheduled in user mode (clock ticks) */ unsigned long long stime; /** Amount of time that this process has been scheduled in kernel mode (clock ticks) */ }; @@ -83,7 +83,7 @@ struct procfs_process_cpu_usage_info { * @param[out] avg structure to be filled. * @return: 0 on success, other value on error */ -int procfs_read_system_load_average(struct procfs_load_average_info *avg); +int procfs_read_loadavg(struct procfs_loadavg *avg); /** * @brief Parses information from /proc/stat @@ -91,20 +91,7 @@ int procfs_read_system_load_average(struct procfs_load_average_info *avg); * @param[out] avg structure to be filled. * @return: 0 on success, other value on error */ -int procfs_read_system_cpu_usage(struct procfs_system_cpu_usage_info *usage); - -/** - * @brief Parses information from /proc/stat, returns system cpu usage per - * logical cpu (core). - * - * @param[in] max_cpus maximum number of cpus to read stats from. - * @param[out] usage array to be filled. - * @return: on success - number of entries correctly written to usage array, - * value < 0 if an error occured. - * - * @remark the usage array must be have at least max_cpus lenght. - */ -int procfs_read_system_percpu_usage(int max_cpus, struct procfs_system_cpu_usage_info usage[]); +int procfs_read_stat(struct procfs_stat *stat); /** * @brief Parses information from /proc/meminfo @@ -112,7 +99,7 @@ int procfs_read_system_percpu_usage(int max_cpus, struct procfs_system_cpu_usage * @param[out] avg structure to be filled. * @return: 0 on success, other value on error */ -int procfs_read_system_memory_usage(struct procfs_system_memory_usage_info *usage); +int procfs_read_meminfo(struct procfs_meminfo *meminfo); /** * @brief Parses information from /proc/{pid}/smaps @@ -121,7 +108,7 @@ int procfs_read_system_memory_usage(struct procfs_system_memory_usage_info *usag * @param[out] avg structure to be filled. * @return: 0 on success, other value on error */ -int procfs_read_process_memory_usage(int pid, struct procfs_process_memory_usage_info *usage); +int procfs_read_process_smaps(int pid, struct procfs_process_smaps *smaps); /** * @brief Parses information from /proc/{pid}/stat @@ -130,7 +117,7 @@ int procfs_read_process_memory_usage(int pid, struct procfs_process_memory_usage * @param[out] avg structure to be filled. * @return: 0 on success, other value on error */ -int procfs_read_process_cpu_usage(int pid, struct procfs_process_cpu_usage_info *usage); +int procfs_read_process_stat(int pid, struct procfs_process_stat *stat); /** * @brief Parses information from /proc/uptime @@ -146,7 +133,7 @@ int procfs_read_uptime(unsigned long *uptime); * @param[out] cpu_count Number of logical cpus availabe in the system. * @return: 0 on success, other value on error */ -int procfs_read_cpu_count(int *cpu_count); +int procfs_read_cpu_possible(int *cpu_count); /** * @brief Callback func for @procfs_iterate_pids @@ -166,7 +153,7 @@ typedef bool (*procfs_pid_iterator_cb)(int pid, void *user_data); int procfs_iterate_pids(procfs_pid_iterator_cb iterator, void *user_data); /** - * @brief Reads cmdline file for given pid + * @brief Reads cmdline for /proc//cmdline * * @param[in] pid * @param[out] cmdline pointer to newly allocated null-terminated string buffer or NULL @@ -177,10 +164,10 @@ int procfs_iterate_pids(procfs_pid_iterator_cb iterator, void *user_data); * @note the returned cmdline should be released with @free * @note the cmdline parameters are separated using ' ' sign. */ -int procfs_read_cmdline(int pid, char **cmdline); +int procfs_read_process_cmdline(int pid, char **cmdline); /** - * @brief Reads exe file for given pid + * @brief Reads link form /proc//exe * * @param[in] pid * @param[out] pointer to null-terminated string buffer @@ -189,6 +176,6 @@ int procfs_read_cmdline(int pid, char **cmdline); * * @note the returned buffer should be released with @free */ -int procfs_read_exe(int pid, char **buffer); +int procfs_read_process_exe(int pid, char **buffer); #endif diff --git a/src/stats.c b/src/stats.c index c5b006e..b2eaf4b 100644 --- a/src/stats.c +++ b/src/stats.c @@ -45,15 +45,15 @@ int stats_update_system_stats(struct stats_system *sys) { ON_NULL_RETURN_VAL(sys, -1); - struct procfs_system_cpu_usage_info cpu_info; - struct procfs_system_memory_usage_info mem_info; + struct procfs_stat cpu_info; + struct procfs_meminfo mem_info; - if (procfs_read_system_cpu_usage(&cpu_info) != 0) { + if (procfs_read_stat(&cpu_info) != 0) { return -1; } - if (procfs_read_system_memory_usage(&mem_info) != 0) { - ERR("procfs_read_system_memory_usage failed."); + if (procfs_read_meminfo(&mem_info) != 0) { + ERR("procfs_read_meminfo failed."); return -1; } @@ -83,16 +83,16 @@ 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_cpu_usage_info proc_info; - struct procfs_system_cpu_usage_info sys_info; + struct procfs_process_stat proc_info; + struct procfs_stat sys_info; - if (procfs_read_process_cpu_usage(pid, &proc_info) != 0) { - ERR("procfs_read_process_cpu_usage failed."); + if (procfs_read_process_stat(pid, &proc_info) != 0) { + ERR("procfs_read_process_stat failed."); return -1; } - if (procfs_read_system_cpu_usage(&sys_info) != 0) { - ERR("procfs_read_system_cpu_usage failed."); + if (procfs_read_stat(&sys_info) != 0) { + ERR("procfs_read_stat failed."); return -1; } @@ -122,30 +122,30 @@ 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_memory_usage_info mem_info; - struct procfs_system_memory_usage_info sys_info; + struct procfs_process_smaps mem_info; + struct procfs_meminfo sys_meminfo; - if (procfs_read_process_memory_usage(pid, &mem_info) != 0) { - ERR("procfs_read_process_memory_usage failed."); + if (procfs_read_process_smaps(pid, &mem_info) != 0) { + ERR("procfs_read_process_smaps failed."); return -1; } - if (procfs_read_system_memory_usage(&sys_info) != 0) { - ERR("procfs_read_system_memory_usage failed."); + if (procfs_read_meminfo(&sys_meminfo) != 0) { + ERR("procfs_read_meminfo failed."); return -1; } - *usage = sys_info.total > 0 ? (float)mem_info.rss / sys_info.total : 0; + *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_load_average_info info; + struct procfs_loadavg info; - if (procfs_read_system_load_average(&info) != 0) { - ERR("procfs_read_system_load_average failed."); + if (procfs_read_loadavg(&info) != 0) { + ERR("procfs_read_loadavg failed."); return -1; } @@ -163,8 +163,8 @@ float stats_get_cpu_usage_percentage(unsigned long long ticks_delta, float ticks int stats_init() { - if (procfs_read_cpu_count(&ncpus) != 0) { - ERR("procfs_read_cpu_count failed."); + if (procfs_read_cpu_possible(&ncpus) != 0) { + ERR("procfs_read_cpu_possible failed."); return -1; } timescale = 1.0f / (float)sysconf(_SC_CLK_TCK); -- 2.7.4