提交 2adf59eb 编写于 作者: J Ján Tomko

Clean up virCgroupGetPercpuStats

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.
上级 9fe5267a
...@@ -2904,8 +2904,6 @@ virCgroupGetPercpuStats(virCgroupPtr group, ...@@ -2904,8 +2904,6 @@ virCgroupGetPercpuStats(virCgroupPtr group,
char *pos; char *pos;
char *buf = NULL; char *buf = NULL;
unsigned long long *sum_cpu_time = NULL; unsigned long long *sum_cpu_time = NULL;
unsigned long long *sum_cpu_pos;
unsigned int n = 0;
virTypedParameterPtr ent; virTypedParameterPtr ent;
int param_idx; int param_idx;
unsigned long long cpu_time; unsigned long long cpu_time;
...@@ -2919,14 +2917,11 @@ virCgroupGetPercpuStats(virCgroupPtr group, ...@@ -2919,14 +2917,11 @@ virCgroupGetPercpuStats(virCgroupPtr group,
} }
/* To parse account file, we need to know how many cpus are present. */ /* To parse account file, we need to know how many cpus are present. */
total_cpus = nodeGetCPUCount(); if ((total_cpus = nodeGetCPUCount()) < 0)
if (total_cpus < 0)
return rv; return rv;
if (ncpus == 0) { if (ncpus == 0)
rv = total_cpus; return total_cpus;
goto cleanup;
}
if (start_cpu >= total_cpus) { if (start_cpu >= total_cpus) {
virReportError(VIR_ERR_INVALID_ARG, virReportError(VIR_ERR_INVALID_ARG,
...@@ -2944,18 +2939,13 @@ virCgroupGetPercpuStats(virCgroupPtr group, ...@@ -2944,18 +2939,13 @@ virCgroupGetPercpuStats(virCgroupPtr group,
param_idx = 0; param_idx = 0;
/* number of cpus to compute */ /* number of cpus to compute */
if (start_cpu >= total_cpus - ncpus) need_cpus = MIN(total_cpus, start_cpu + ncpus);
need_cpus = total_cpus - 1;
else
need_cpus = start_cpu + ncpus - 1;
for (i = 0; i <= need_cpus; i++) { for (i = 0; i < need_cpus; i++) {
if (virStrToLong_ull(pos, &pos, 10, &cpu_time) < 0) { if (virStrToLong_ull(pos, &pos, 10, &cpu_time) < 0) {
virReportError(VIR_ERR_INTERNAL_ERROR, "%s", virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
_("cpuacct parse error")); _("cpuacct parse error"));
goto cleanup; goto cleanup;
} else {
n++;
} }
if (i < start_cpu) if (i < start_cpu)
continue; continue;
...@@ -2970,21 +2960,17 @@ virCgroupGetPercpuStats(virCgroupPtr group, ...@@ -2970,21 +2960,17 @@ virCgroupGetPercpuStats(virCgroupPtr group,
/* return percpu vcputime in index 1 */ /* return percpu vcputime in index 1 */
param_idx++; param_idx++;
if (VIR_ALLOC_N(sum_cpu_time, n) < 0) if (VIR_ALLOC_N(sum_cpu_time, need_cpus) < 0)
goto cleanup; goto cleanup;
if (virCgroupGetPercpuVcpuSum(group, nvcpupids, sum_cpu_time, n) < 0) if (virCgroupGetPercpuVcpuSum(group, nvcpupids, sum_cpu_time, need_cpus) < 0)
goto cleanup; goto cleanup;
sum_cpu_pos = sum_cpu_time; for (i = start_cpu; i < need_cpus; i++) {
for (i = 0; i <= need_cpus; i++) {
cpu_time = *(sum_cpu_pos++);
if (i < start_cpu)
continue;
if (virTypedParameterAssign(&params[(i - start_cpu) * nparams + if (virTypedParameterAssign(&params[(i - start_cpu) * nparams +
param_idx], param_idx],
VIR_DOMAIN_CPU_STATS_VCPUTIME, VIR_DOMAIN_CPU_STATS_VCPUTIME,
VIR_TYPED_PARAM_ULLONG, VIR_TYPED_PARAM_ULLONG,
cpu_time) < 0) sum_cpu_time[i]) < 0)
goto cleanup; goto cleanup;
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册