util: kernel: Add cpu utilization helper function 10/271210/9
authorChanwoo Choi <cw00.choi@samsung.com>
Wed, 16 Feb 2022 07:51:54 +0000 (16:51 +0900)
committerChanwoo Choi <cw00.choi@samsung.com>
Fri, 18 Feb 2022 07:51:07 +0000 (16:51 +0900)
Add cpu utilization helper function as following:

[Detailed description of cpu utilization helper function]
int kernel_get_avg_cpu_util(struct cpu_util_data *avg_cpu)
: Store the average cpu utilization of all cpus to 'avg_cpu'

int kernel_get_per_cpu_util(struct cpu_util_data *cpus, int num_possible_cpus,
    int *num_online_cpus);
: Store the per-cpu utilization datas to 'cpus' and store the number of
online to 'num_online_cpus'

Change-Id: I1594dc656759194ffa9468ed6eebbc159a47e5fd
Signed-off-by: Chanwoo Choi <cw00.choi@samsung.com>
include/util/kernel.h
src/util/kernel.c

index 08d776d..ad76c83 100644 (file)
 #include <glib.h>
 #include "common.h"
 
+struct cpu_stat {
+       char name[10];
+       unsigned int cpu;
+       int64_t user;
+       int64_t system;
+       int64_t nice;
+       int64_t idle;
+       int64_t wait;
+       int64_t hard_irq;
+       int64_t soft_irq;
+};
+
 int kernel_get_online_cpu_num(void);
 int kernel_get_possible_cpu_num(void);
 
+int kernel_get_total_cpu_stat(struct cpu_stat *total);
+int kernel_get_per_cpu_stat(struct cpu_stat *cpus, int num_possible_cpus,
+                           int *num_online_cpus);
 #endif
index 37fce1b..490432f 100644 (file)
@@ -54,6 +54,50 @@ static int __get_cpu_num(char *path)
        return ncpu;
 }
 
+static int __get_cpu_stat(FILE *fp, struct cpu_stat *data)
+{
+       int ret;
+
+       if (!fp || !data)
+               return -EINVAL;
+
+       ret = fscanf(fp, "%s %"PRId64" %"PRId64" %"PRId64" %"PRId64" \
+                               %"PRId64" %"PRId64" %"PRId64" %*s %*s %*s",
+               data->name,
+               &data->user,
+               &data->system,
+               &data->nice,
+               &data->idle,
+               &data->wait,
+               &data->hard_irq,
+               &data->soft_irq);
+       if (ret < 8) {
+               ret = -EINVAL;
+               goto err;
+       }
+
+       if (strncmp(data->name, "cpu", 3)) {
+               /* In case of not 'cpu*' string */
+               ret = -EINVAL;
+               goto err;
+       } else if (strlen(data->name) > 3) {
+               /* In case of 'cpu%u' string */
+               ret = sscanf(data->name, "cpu%4u", &data->cpu);
+               if (ret < 1) {
+                       ret = -EINVAL;
+                       goto err;
+               }
+       } else {
+               /* In case of 'cpu' string */
+               data->cpu = -1;
+       }
+
+       return 0;
+err:
+       memset(data, 0, sizeof(struct cpu_stat));
+       return ret;
+}
+
 int kernel_get_online_cpu_num(void)
 {
        return __get_cpu_num("/sys/devices/system/cpu/online");
@@ -63,3 +107,60 @@ int kernel_get_possible_cpu_num(void)
 {
        return __get_cpu_num("/sys/devices/system/cpu/possible");
 }
+
+int kernel_get_total_cpu_stat(struct cpu_stat *total)
+{
+       FILE *fp;
+       int ret;
+
+       if (!total)
+               return -EINVAL;
+
+       fp = fopen("/proc/stat", "r");
+       if (!fp)
+               return -EINVAL;
+
+       ret = __get_cpu_stat(fp, total);
+       if (ret < 0)
+               ret = -EINVAL;
+       fclose(fp);
+
+       return  ret;
+}
+
+int kernel_get_per_cpu_stat(struct cpu_stat *cpus, int num_possible_cpus,
+                           int *num_online_cpus)
+{
+       FILE *fp;
+       char buf[BUFF_MAX];
+       int i, ret = 0, count = 0;
+
+       if (!cpus)
+               return -EINVAL;
+
+       *num_online_cpus = 0;
+
+       fp = fopen("/proc/stat", "r");
+       if (!fp)
+               return -EINVAL;
+
+       /* Skip the first line of total cpu utilizaiton. */
+       if (!fgets(buf, BUFF_MAX, fp)) {
+               ret = -EINVAL;
+               goto err;
+       }
+
+       /* Get per-cpu utilization data */
+       for (i = 0; i < num_possible_cpus; i++) {
+               ret = __get_cpu_stat(fp, &cpus[i]);
+               if (ret < 0)
+                       break;
+               count++;
+       }
+       *num_online_cpus = count;
+
+err:
+       fclose(fp);
+
+       return ret;
+}