The iterator is checked for being less than or equal to need_cpus.
The 'n' variable is incremented need_cpus + 1 times.

Simplify the computation of need_cpus and make its value one larger,
to let it be used instead of 'n' and compared without the equal sign
in loop conditions.

Just index the sum_cpu_time array instead of using a helper variable.

Start the loop at start_cpu instead of continuing for all lower values.
---
 src/util/vircgroup.c | 32 +++++++++-----------------------
 1 file changed, 9 insertions(+), 23 deletions(-)

diff --git a/src/util/vircgroup.c b/src/util/vircgroup.c
index 2272bc6..74e0907 100644
--- a/src/util/vircgroup.c
+++ b/src/util/vircgroup.c
@@ -2904,8 +2904,6 @@ virCgroupGetPercpuStats(virCgroupPtr group,
     char *pos;
     char *buf = NULL;
     unsigned long long *sum_cpu_time = NULL;
-    unsigned long long *sum_cpu_pos;
-    unsigned int n = 0;
     virTypedParameterPtr ent;
     int param_idx;
     unsigned long long cpu_time;
@@ -2919,14 +2917,11 @@ virCgroupGetPercpuStats(virCgroupPtr group,
     }
 
     /* To parse account file, we need to know how many cpus are present.  */
-    total_cpus = nodeGetCPUCount();
-    if (total_cpus < 0)
+    if ((total_cpus = nodeGetCPUCount()) < 0)
         return rv;
 
-    if (ncpus == 0) {
-        rv = total_cpus;
-        goto cleanup;
-    }
+    if (ncpus == 0)
+        return total_cpus;
 
     if (start_cpu >= total_cpus) {
         virReportError(VIR_ERR_INVALID_ARG,
@@ -2944,18 +2939,13 @@ virCgroupGetPercpuStats(virCgroupPtr group,
     param_idx = 0;
 
     /* number of cpus to compute */
-    if (start_cpu >= total_cpus - ncpus)
-        need_cpus = total_cpus - 1;
-    else
-        need_cpus = start_cpu + ncpus - 1;
+    need_cpus = MIN(total_cpus, start_cpu + ncpus);
 
-    for (i = 0; i <= need_cpus; i++) {
+    for (i = 0; i < need_cpus; i++) {
         if (virStrToLong_ull(pos, &pos, 10, &cpu_time) < 0) {
             virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                            _("cpuacct parse error"));
             goto cleanup;
-        } else {
-            n++;
         }
         if (i < start_cpu)
             continue;
@@ -2970,21 +2960,17 @@ virCgroupGetPercpuStats(virCgroupPtr group,
     /* return percpu vcputime in index 1 */
     param_idx++;
 
-    if (VIR_ALLOC_N(sum_cpu_time, n) < 0)
+    if (VIR_ALLOC_N(sum_cpu_time, need_cpus) < 0)
         goto cleanup;
-    if (virCgroupGetPercpuVcpuSum(group, nvcpupids, sum_cpu_time, n) < 0)
+    if (virCgroupGetPercpuVcpuSum(group, nvcpupids, sum_cpu_time, need_cpus) < 
0)
         goto cleanup;
 
-    sum_cpu_pos = sum_cpu_time;
-    for (i = 0; i <= need_cpus; i++) {
-        cpu_time = *(sum_cpu_pos++);
-        if (i < start_cpu)
-            continue;
+    for (i = start_cpu; i < need_cpus; i++) {
         if (virTypedParameterAssign(&params[(i - start_cpu) * nparams +
                                             param_idx],
                                     VIR_DOMAIN_CPU_STATS_VCPUTIME,
                                     VIR_TYPED_PARAM_ULLONG,
-                                    cpu_time) < 0)
+                                    sum_cpu_time[i]) < 0)
             goto cleanup;
     }
 
-- 
1.8.3.2

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list

Reply via email to