]> xenbits.xensource.com Git - people/dariof/libvirt.git/commitdiff
nodeinfo: enable nodeGetCPUCount for older kernels
authorViktor Mihajlovski <mihajlov@linux.vnet.ibm.com>
Tue, 13 Nov 2012 12:54:36 +0000 (13:54 +0100)
committerEric Blake <eblake@redhat.com>
Thu, 15 Nov 2012 03:43:54 +0000 (20:43 -0700)
Since /sys/devices/system/cpu/present is not available on
older kernels like on RHEL 5.x nodeGetCPUCount will
fail there. The fallback implemented is to scan for
/sys/devices/system/cpu/cpuNN entries.

Signed-off-by: Viktor Mihajlovski <mihajlov@linux.vnet.ibm.com>
src/nodeinfo.c

index e3d7cfeae41f3dee824b902dfaa1dad28be59359..4589b77d582d06492996d98b7319f4f6c611f8f8 100644 (file)
@@ -978,11 +978,35 @@ int
 nodeGetCPUCount(void)
 {
 #ifdef __linux__
-    /* XXX should we also work on older kernels, like RHEL5, that lack
-     * cpu/present and cpu/online files?  Those kernels also lack cpu
-     * hotplugging, so it would be a matter of finding the largest
-     * cpu/cpuNN directory, and returning NN + 1 */
-    return linuxParseCPUmax(SYSFS_SYSTEM_PATH "/cpu/present");
+    /* To support older kernels that lack cpu/present, such as 2.6.18
+     * in RHEL5, we fall back to count cpu/cpuNN entries; this assumes
+     * that such kernels also lack hotplug, and therefore cpu/cpuNN
+     * will be consecutive.
+     */
+    char *cpupath = NULL;
+    int i = 0;
+
+    if (virFileExists(SYSFS_SYSTEM_PATH "/cpu/present")) {
+        i = linuxParseCPUmax(SYSFS_SYSTEM_PATH "/cpu/present");
+    } else if (virFileExists(SYSFS_SYSTEM_PATH "/cpu/cpu0")) {
+        do {
+            i++;
+            VIR_FREE(cpupath);
+            if (virAsprintf(&cpupath, "%s/cpu/cpu%d",
+                            SYSFS_SYSTEM_PATH, i) < 0) {
+                virReportOOMError();
+                return -1;
+            }
+        } while (virFileExists(cpupath));
+    } else {
+        /* no cpu/cpu0: we give up */
+        virReportError(VIR_ERR_NO_SUPPORT, "%s",
+                       _("host cpu counting not supported on this node"));
+        return -1;
+    }
+
+    VIR_FREE(cpupath);
+    return i;
 #else
     virReportError(VIR_ERR_NO_SUPPORT, "%s",
                    _("host cpu counting not implemented on this platform"));