mirror of
https://github.com/libvirt/libvirt.git
synced 2025-02-25 18:55:26 -06:00
Simplify linuxNodeGetCPUStats
Split out the repetitive code.
This commit is contained in:
parent
c022fbc9bb
commit
61cf8d8461
@ -674,6 +674,20 @@ cleanup:
|
|||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static int
|
||||||
|
virNodeCPUStatsAssign(virNodeCPUStatsPtr param,
|
||||||
|
const char *name,
|
||||||
|
unsigned long long value)
|
||||||
|
{
|
||||||
|
if (virStrcpyStatic(param->field, name) == NULL) {
|
||||||
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
||||||
|
"%s", _("Field kernel cpu time too long for destination"));
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
param->value = value;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
# define TICK_TO_NSEC (1000ull * 1000ull * 1000ull / sysconf(_SC_CLK_TCK))
|
# define TICK_TO_NSEC (1000ull * 1000ull * 1000ull / sysconf(_SC_CLK_TCK))
|
||||||
|
|
||||||
int
|
int
|
||||||
@ -712,8 +726,6 @@ linuxNodeGetCPUStats(FILE *procstat,
|
|||||||
char *buf = line;
|
char *buf = line;
|
||||||
|
|
||||||
if (STRPREFIX(buf, cpu_header)) { /* aka logical CPU time */
|
if (STRPREFIX(buf, cpu_header)) { /* aka logical CPU time */
|
||||||
size_t i;
|
|
||||||
|
|
||||||
if (sscanf(buf,
|
if (sscanf(buf,
|
||||||
"%*s %llu %llu %llu %llu %llu" // user ~ iowait
|
"%*s %llu %llu %llu %llu %llu" // user ~ iowait
|
||||||
"%llu %llu %llu %llu %llu", // irq ~ guest_nice
|
"%llu %llu %llu %llu %llu", // irq ~ guest_nice
|
||||||
@ -722,51 +734,22 @@ linuxNodeGetCPUStats(FILE *procstat,
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
for (i = 0; i < *nparams; i++) {
|
if (virNodeCPUStatsAssign(¶ms[0], VIR_NODE_CPU_STATS_KERNEL,
|
||||||
virNodeCPUStatsPtr param = ¶ms[i];
|
(sys + irq + softirq) * TICK_TO_NSEC) < 0)
|
||||||
|
goto cleanup;
|
||||||
|
|
||||||
switch (i) {
|
if (virNodeCPUStatsAssign(¶ms[1], VIR_NODE_CPU_STATS_USER,
|
||||||
case 0: /* fill kernel cpu time here */
|
(usr + ni) * TICK_TO_NSEC) < 0)
|
||||||
if (virStrcpyStatic(param->field, VIR_NODE_CPU_STATS_KERNEL) == NULL) {
|
goto cleanup;
|
||||||
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
||||||
"%s", _("Field kernel cpu time too long for destination"));
|
|
||||||
goto cleanup;
|
|
||||||
}
|
|
||||||
param->value = (sys + irq + softirq) * TICK_TO_NSEC;
|
|
||||||
break;
|
|
||||||
|
|
||||||
case 1: /* fill user cpu time here */
|
if (virNodeCPUStatsAssign(¶ms[2], VIR_NODE_CPU_STATS_IDLE,
|
||||||
if (virStrcpyStatic(param->field, VIR_NODE_CPU_STATS_USER) == NULL) {
|
idle * TICK_TO_NSEC) < 0)
|
||||||
virReportError(VIR_ERR_INTERNAL_ERROR,
|
goto cleanup;
|
||||||
"%s", _("Field kernel cpu time too long for destination"));
|
|
||||||
goto cleanup;
|
|
||||||
}
|
|
||||||
param->value = (usr + ni) * TICK_TO_NSEC;
|
|
||||||
break;
|
|
||||||
|
|
||||||
case 2: /* fill idle cpu time here */
|
if (virNodeCPUStatsAssign(¶ms[3], VIR_NODE_CPU_STATS_IOWAIT,
|
||||||
if (virStrcpyStatic(param->field, VIR_NODE_CPU_STATS_IDLE) == NULL) {
|
iowait * TICK_TO_NSEC) < 0)
|
||||||
virReportError(VIR_ERR_INTERNAL_ERROR,
|
goto cleanup;
|
||||||
"%s", _("Field kernel cpu time too long for destination"));
|
|
||||||
goto cleanup;
|
|
||||||
}
|
|
||||||
param->value = idle * TICK_TO_NSEC;
|
|
||||||
break;
|
|
||||||
|
|
||||||
case 3: /* fill iowait cpu time here */
|
|
||||||
if (virStrcpyStatic(param->field, VIR_NODE_CPU_STATS_IOWAIT) == NULL) {
|
|
||||||
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
||||||
"%s", _("Field kernel cpu time too long for destination"));
|
|
||||||
goto cleanup;
|
|
||||||
}
|
|
||||||
param->value = iowait * TICK_TO_NSEC;
|
|
||||||
break;
|
|
||||||
|
|
||||||
default:
|
|
||||||
break;
|
|
||||||
/* should not hit here */
|
|
||||||
}
|
|
||||||
}
|
|
||||||
ret = 0;
|
ret = 0;
|
||||||
goto cleanup;
|
goto cleanup;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user