]> xenbits.xensource.com Git - libvirt.git/commitdiff
linuxNodeGetCPUStats: Correctly handle cpu prefix
authorBing Bu Cao <mars@linux.vnet.ibm.com>
Thu, 16 Jan 2014 08:18:09 +0000 (16:18 +0800)
committerMichal Privoznik <mprivozn@redhat.com>
Tue, 21 Jan 2014 16:24:03 +0000 (17:24 +0100)
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 <mars@linux.vnet.ibm.com>
src/nodeinfo.c

index 05bc0381346f00e4be1dad433c1d89eec7bfe3e8..cba2fc147ce654e34200bcb3812c346b3a3d18e8 100644 (file)
@@ -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) {