7a3408
From e1215ada2684480adcac35d58728cfac4fffd537 Mon Sep 17 00:00:00 2001
7a3408
Message-Id: <e1215ada2684480adcac35d58728cfac4fffd537@dist-git>
7a3408
From: John Ferlan <jferlan@redhat.com>
7a3408
Date: Wed, 5 Aug 2015 18:18:07 +0200
7a3408
Subject: [PATCH] nodeinfo: Introduce local linuxGetCPUPresentPath
7a3408
7a3408
The API will print the path to the /cpu/present file using the sysfs_prefix.
7a3408
7a3408
NB: This is setup for future patches which will allow local/test sysfs paths.
7a3408
7a3408
(cherry picked from commit 3119e05e267d6d5e49b54c8353d9e49da4889d1d)
7a3408
7a3408
Bug: https://bugzilla.redhat.com/show_bug.cgi?id=1213713
7a3408
7a3408
Signed-off-by: Andrea Bolognani <abologna@redhat.com>
7a3408
Signed-off-by: Jiri Denemark <jdenemar@redhat.com>
7a3408
---
7a3408
 src/nodeinfo.c | 43 ++++++++++++++++++++++++++++++++++---------
7a3408
 1 file changed, 34 insertions(+), 9 deletions(-)
7a3408
7a3408
diff --git a/src/nodeinfo.c b/src/nodeinfo.c
7a3408
index 2fafe2d..f3e3108 100644
7a3408
--- a/src/nodeinfo.c
7a3408
+++ b/src/nodeinfo.c
7a3408
@@ -1,7 +1,7 @@
7a3408
 /*
7a3408
  * nodeinfo.c: Helper routines for OS specific node information
7a3408
  *
7a3408
- * Copyright (C) 2006-2008, 2010-2014 Red Hat, Inc.
7a3408
+ * Copyright (C) 2006-2008, 2010-2015 Red Hat, Inc.
7a3408
  * Copyright (C) 2006 Daniel P. Berrange
7a3408
  *
7a3408
  * This library is free software; you can redistribute it and/or
7a3408
@@ -944,6 +944,16 @@ linuxNodeGetMemoryStats(FILE *meminfo,
7a3408
     return ret;
7a3408
 }
7a3408
 
7a3408
+static char *
7a3408
+linuxGetCPUPresentPath(const char *sysfs_prefix)
7a3408
+{
7a3408
+    const char *prefix = sysfs_prefix ? sysfs_prefix : SYSFS_SYSTEM_PATH;
7a3408
+    char *path = NULL;
7a3408
+
7a3408
+    if (virAsprintf(&path, "%s/cpu/present", prefix) < 0)
7a3408
+        return NULL;
7a3408
+    return path;
7a3408
+}
7a3408
 
7a3408
 /* Determine the maximum cpu id from a Linux sysfs cpu/present file. */
7a3408
 static int
7a3408
@@ -1193,27 +1203,34 @@ nodeGetCPUCount(void)
7a3408
      * that such kernels also lack hotplug, and therefore cpu/cpuNN
7a3408
      * will be consecutive.
7a3408
      */
7a3408
+    char *present_path = NULL;
7a3408
     char *cpupath = NULL;
7a3408
-    int ncpu;
7a3408
+    int ncpu = -1;
7a3408
 
7a3408
-    if (virFileExists(SYSFS_SYSTEM_PATH "/cpu/present")) {
7a3408
-        ncpu = linuxParseCPUmax(SYSFS_SYSTEM_PATH "/cpu/present");
7a3408
+    if (!(present_path = linuxGetCPUPresentPath(NULL)))
7a3408
+        return -1;
7a3408
+
7a3408
+    if (virFileExists(present_path)) {
7a3408
+        ncpu = linuxParseCPUmax(present_path);
7a3408
     } else if (virFileExists(SYSFS_SYSTEM_PATH "/cpu/cpu0")) {
7a3408
         ncpu = 0;
7a3408
         do {
7a3408
             ncpu++;
7a3408
             VIR_FREE(cpupath);
7a3408
             if (virAsprintf(&cpupath, "%s/cpu/cpu%d",
7a3408
-                            SYSFS_SYSTEM_PATH, ncpu) < 0)
7a3408
-                return -1;
7a3408
+                            SYSFS_SYSTEM_PATH, ncpu) < 0) {
7a3408
+                ncpu = -1;
7a3408
+                goto cleanup;
7a3408
+            }
7a3408
         } while (virFileExists(cpupath));
7a3408
     } else {
7a3408
         /* no cpu/cpu0: we give up */
7a3408
         virReportError(VIR_ERR_NO_SUPPORT, "%s",
7a3408
                        _("host cpu counting not supported on this node"));
7a3408
-        return -1;
7a3408
     }
7a3408
 
7a3408
+ cleanup:
7a3408
+    VIR_FREE(present_path);
7a3408
     VIR_FREE(cpupath);
7a3408
     return ncpu;
7a3408
 #elif defined(__FreeBSD__) || defined(__APPLE__)
7a3408
@@ -1229,13 +1246,21 @@ virBitmapPtr
7a3408
 nodeGetPresentCPUBitmap(void)
7a3408
 {
7a3408
     int max_present;
7a3408
+#ifdef __linux__
7a3408
+    char *present_path = NULL;
7a3408
+    virBitmapPtr bitmap = NULL;
7a3408
+#endif
7a3408
 
7a3408
     if ((max_present = nodeGetCPUCount()) < 0)
7a3408
         return NULL;
7a3408
 
7a3408
 #ifdef __linux__
7a3408
-    if (virFileExists(SYSFS_SYSTEM_PATH "/cpu/present"))
7a3408
-        return linuxParseCPUmap(max_present, SYSFS_SYSTEM_PATH "/cpu/present");
7a3408
+    if (!(present_path = linuxGetCPUPresentPath(NULL)))
7a3408
+        return NULL;
7a3408
+    if (virFileExists(present_path))
7a3408
+        bitmap = linuxParseCPUmap(max_present, present_path);
7a3408
+    VIR_FREE(present_path);
7a3408
+    return bitmap;
7a3408
 #endif
7a3408
     virReportError(VIR_ERR_NO_SUPPORT, "%s",
7a3408
                    _("non-continuous host cpu numbers not implemented on this platform"));
7a3408
-- 
7a3408
2.5.0
7a3408