util: kernel: Fix to return valid result on getting cpu stat 83/279983/3
authorDongwoo Lee <dwoo08.lee@samsung.com>
Fri, 19 Aug 2022 10:03:59 +0000 (19:03 +0900)
committerDongwoo Lee <dwlee08@gmail.com>
Tue, 23 Aug 2022 00:04:34 +0000 (17:04 -0700)
In the case of the target which supports cpu hotplug, the number of
possible online cpus can be changed and thus __get_cpu_stat can be
failed for offline cpus. At worst case if the last __get_cpu_stat is
resulted in negative, kernel_get_per_cpu_stat can return error even
though it is not actual failure. To prevent this situation, this
ignores the case that __get_cpu_stat returns failure.

Change-Id: I5d33e740b2451e7c644b407c1f92785a1e570585
Signed-off-by: Dongwoo Lee <dwoo08.lee@samsung.com>
src/util/kernel.c

index 40fd869..8226b7f 100644 (file)
@@ -146,6 +146,7 @@ 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)
 {
+       struct cpu_stat cpu;
        FILE *fp;
        char buf[BUFF_MAX];
        int i, ret = 0, count = 0;
@@ -167,9 +168,16 @@ int kernel_get_per_cpu_stat(struct cpu_stat *cpus, int num_possible_cpus,
 
        /* Get per-cpu utilization data */
        for (i = 0; i < num_possible_cpus; i++) {
-               ret = __get_cpu_stat(fp, &cpus[i]);
-               if (ret < 0)
-                       break;
+               if (__get_cpu_stat(fp, &cpu) < 0)
+                       continue;
+
+               if (cpu.cpu < 0) {
+                       ret = -EINVAL;
+                       goto err;
+               }
+
+               memcpy(&cpus[cpu.cpu], &cpu, sizeof(struct cpu_stat));
+
                count++;
        }
        *num_online_cpus = count;