7a3408
From 40939c6b2ef877a46fa0b3ec177673e6b0831414 Mon Sep 17 00:00:00 2001
7a3408
Message-Id: <40939c6b2ef877a46fa0b3ec177673e6b0831414@dist-git>
7a3408
From: Andrea Bolognani <abologna@redhat.com>
7a3408
Date: Wed, 5 Aug 2015 18:18:30 +0200
7a3408
Subject: [PATCH] nodeinfo: Use a bitmap to keep track of node CPUs
7a3408
7a3408
Keep track of what CPUs belong to the current node while walking
7a3408
through the sysfs node entry, so we don't need to do it a second
7a3408
time immediately afterwards.
7a3408
7a3408
This also allows us to loop through all CPUs that are part of a
7a3408
node in guaranteed ascending order, which is something that is
7a3408
required for some upcoming changes.
7a3408
7a3408
(cherry picked from commit 05be6062822f3f694b9e2ba1982d60cad5c09f33)
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 | 26 +++++++++++++++++---------
7a3408
 1 file changed, 17 insertions(+), 9 deletions(-)
7a3408
7a3408
diff --git a/src/nodeinfo.c b/src/nodeinfo.c
7a3408
index 2d910c9..2328a86 100644
7a3408
--- a/src/nodeinfo.c
7a3408
+++ b/src/nodeinfo.c
7a3408
@@ -410,8 +410,10 @@ virNodeParseNode(const char *sysfs_prefix,
7a3408
     struct dirent *cpudirent = NULL;
7a3408
     virBitmapPtr present_cpumap = NULL;
7a3408
     virBitmapPtr online_cpus_map = NULL;
7a3408
+    virBitmapPtr node_cpus_map = NULL;
7a3408
     virBitmapPtr sockets_map = NULL;
7a3408
     virBitmapPtr *cores_maps = NULL;
7a3408
+    int npresent_cpus;
7a3408
     int sock_max = 0;
7a3408
     int sock;
7a3408
     int core;
7a3408
@@ -436,6 +438,12 @@ virNodeParseNode(const char *sysfs_prefix,
7a3408
     if (!online_cpus_map)
7a3408
         goto cleanup;
7a3408
 
7a3408
+    npresent_cpus = virBitmapSize(present_cpumap);
7a3408
+
7a3408
+    /* Keep track of the CPUs that belong to the current node */
7a3408
+    if (!(node_cpus_map = virBitmapNew(npresent_cpus)))
7a3408
+        goto cleanup;
7a3408
+
7a3408
     /* enumerate sockets in the node */
7a3408
     if (!(sockets_map = virBitmapNew(ID_MAX + 1)))
7a3408
         goto cleanup;
7a3408
@@ -447,6 +455,10 @@ virNodeParseNode(const char *sysfs_prefix,
7a3408
         if (!virBitmapIsBitSet(present_cpumap, cpu))
7a3408
             continue;
7a3408
 
7a3408
+        /* Mark this CPU as part of the current node */
7a3408
+        if (virBitmapSetBit(node_cpus_map, cpu) < 0)
7a3408
+            goto cleanup;
7a3408
+
7a3408
         if (!virBitmapIsBitSet(online_cpus_map, cpu))
7a3408
             continue;
7a3408
 
7a3408
@@ -480,13 +492,11 @@ virNodeParseNode(const char *sysfs_prefix,
7a3408
         if (!(cores_maps[i] = virBitmapNew(ID_MAX + 1)))
7a3408
             goto cleanup;
7a3408
 
7a3408
-    /* iterate over all CPU's in the node */
7a3408
-    rewinddir(cpudir);
7a3408
-    while ((direrr = virDirRead(cpudir, &cpudirent, node)) > 0) {
7a3408
-        if (sscanf(cpudirent->d_name, "cpu%u", &cpu) != 1)
7a3408
-            continue;
7a3408
+    /* Iterate over all CPUs in the node, in ascending order */
7a3408
+    for (cpu = 0; cpu < npresent_cpus; cpu++) {
7a3408
 
7a3408
-        if (!virBitmapIsBitSet(present_cpumap, cpu))
7a3408
+        /* Skip CPUs that are not part of the current node */
7a3408
+        if (!virBitmapIsBitSet(node_cpus_map, cpu))
7a3408
             continue;
7a3408
 
7a3408
         if (!virBitmapIsBitSet(online_cpus_map, cpu)) {
7a3408
@@ -529,9 +539,6 @@ virNodeParseNode(const char *sysfs_prefix,
7a3408
             *threads = siblings;
7a3408
     }
7a3408
 
7a3408
-    if (direrr < 0)
7a3408
-        goto cleanup;
7a3408
-
7a3408
     /* finalize the returned data */
7a3408
     *sockets = virBitmapCountBits(sockets_map);
7a3408
 
7a3408
@@ -557,6 +564,7 @@ virNodeParseNode(const char *sysfs_prefix,
7a3408
             virBitmapFree(cores_maps[i]);
7a3408
     VIR_FREE(cores_maps);
7a3408
     virBitmapFree(sockets_map);
7a3408
+    virBitmapFree(node_cpus_map);
7a3408
     virBitmapFree(online_cpus_map);
7a3408
     virBitmapFree(present_cpumap);
7a3408
 
7a3408
-- 
7a3408
2.5.0
7a3408