diff options
author | Lukasz Stanislawski <lukasz.stanislawski@gmail.com> | 2018-06-10 23:27:43 +0200 |
---|---|---|
committer | Lukasz Stanislawski <l.stanislaws@samsung.com> | 2018-06-12 10:09:17 +0000 |
commit | 349da2b29fc57b5f29b6f2d4a04488b25fbf8ee7 (patch) | |
tree | 3395e566159a596c209636cf292fe1fbb963c9d5 | |
parent | 7c33a51cecba249b6801c646e15f0a57abc85f95 (diff) | |
download | ttsd-worker-task-349da2b29fc57b5f29b6f2d4a04488b25fbf8ee7.tar.gz ttsd-worker-task-349da2b29fc57b5f29b6f2d4a04488b25fbf8ee7.tar.bz2 ttsd-worker-task-349da2b29fc57b5f29b6f2d4a04488b25fbf8ee7.zip |
procfs: implement cpu usage
Change-Id: Ic740b533722f6b3646528295d76bb43a0c01ce37
-rw-r--r-- | src/procfs.c | 29 |
1 files changed, 28 insertions, 1 deletions
diff --git a/src/procfs.c b/src/procfs.c index 90bf5e6..a7a461c 100644 --- a/src/procfs.c +++ b/src/procfs.c @@ -15,6 +15,7 @@ */ #include <stdio.h> +#include <string.h> #include "procfs.h" #include "log.h" @@ -24,6 +25,7 @@ #define UPTIME_FILEPATH "/proc/uptime" #define POSSIBLE_CPUS_FILEPATH "/sys/devices/system/cpu/possible" #define MEMINFO_FILEPATH "/proc/meminfo" +#define STAT_FILEPATH "/proc/stat" int procfs_read_system_load_average(struct procfs_load_average_info *info) { @@ -52,8 +54,33 @@ int procfs_read_system_load_average(struct procfs_load_average_info *info) return 0; } -int profcs_read_system_cpu_usage(struct procfs_system_cpu_usage_info *usage) +int procfs_read_system_cpu_usage(struct procfs_system_cpu_usage_info *usage) { + ON_NULL_RETURN_VAL(usage, -1); + + char line[256]; + struct procfs_system_cpu_usage_info tmp; + const char *prefix = "cpu "; + + FILE *stat_fp = fopen(STAT_FILEPATH, "r"); + if (!stat_fp) { + ERR("failed to open " STAT_FILEPATH); + return -1; + } + + while (fgets(line, sizeof(line), stat_fp)) { + if (!strncmp(line, prefix, strlen(prefix)) && + sscanf(line + 4, "%llu %llu %llu %llu %llu %llu %llu", + &tmp.user, &tmp.nice, &tmp.system, &tmp.idle, + &tmp.iowait, &tmp.irq, &tmp.softirq) == 7) { + *usage = tmp; + fclose(stat_fp); + return 0; + } + } + + fclose(stat_fp); + return -1; } |