From 94f82053590795a67bd5fb38026c86fb5329e7b7 Mon Sep 17 00:00:00 2001 From: Bing Bu Cao Date: Thu, 16 Jan 2014 16:18:09 +0800 Subject: [PATCH] linuxNodeGetCPUStats: Correctly handle cpu prefix To retrieve node cpu statistics on Linux system, the linuxNodeGetCPUstats function simply uses STRPREFIX() to match the cpuid with the one read from /proc/stat. However, as the file is read line by line it may happen, that some CPUs share the same prefix. So if user requested stats for the first CPU, which is offline, then there's no cpu1 in the stats file so the one that we match is cpu10. Which is obviously wrong. Fortunately, the IDs are terminated by a space, so we can utilize that. Signed-off-by: Bing Bu Cao --- src/nodeinfo.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/nodeinfo.c b/src/nodeinfo.c index 05bc038134..cba2fc147c 100644 --- a/src/nodeinfo.c +++ b/src/nodeinfo.c @@ -708,9 +708,9 @@ linuxNodeGetCPUStats(FILE *procstat, } if (cpuNum == VIR_NODE_CPU_STATS_ALL_CPUS) { - strcpy(cpu_header, "cpu"); + strcpy(cpu_header, "cpu "); } else { - snprintf(cpu_header, sizeof(cpu_header), "cpu%d", cpuNum); + snprintf(cpu_header, sizeof(cpu_header), "cpu%d ", cpuNum); } while (fgets(line, sizeof(line), procstat) != NULL) { -- GitLab