*/
#include <stdio.h>
+#include <string.h>
#include "procfs.h"
#include "log.h"
#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)
{
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;
}