[PATCH 3 of 5] Fix numa_num_configured_nodes() to match man page description

"Tim Pepper" <[email protected]>
Newsgroups org.kernel.vger.linux-numa
Message-ID <[email protected]>
Author: Tim Pepper <[email protected]>
Date:   Tue Sep 21 13:39:54 2010 -0700

    Fix numa_num_configured_nodes() to match man page description
    
    numa_num_configured_nodes() is currently returning simply the highest
    node number plus one.  The man page says it should be the number of nodes
    with memory.  It is for use in conjunction with the numa_all_nodes_ptr
    nodemask which tracks nodes with memory (from which the calling process
    is allowed to allocate).
    
    Signed-off-by: Tim Pepper <[email protected]>
---
 libnuma.c |   29 ++++++++++++++++++++++++++---
 1 files changed, 26 insertions(+), 3 deletions(-)

diff --git a/libnuma.c b/libnuma.c
index 39a1bc0..641ad26 100644
--- a/libnuma.c
+++ b/libnuma.c
@@ -51,6 +51,7 @@ struct bitmask *numa_all_cpus_ptr = NULL;
    of numa_no_nodes and numa_all_nodes, but the loader does not correctly
    handle versioning of BSS versus small data items */
 
+static struct bitmask *numa_memnode_ptr = NULL;
 static unsigned long *node_cpu_mask_v1[NUMA_NUM_NODES];
 struct bitmask **node_cpu_mask_v2;
 
@@ -102,6 +103,8 @@ numa_fini(void)
 		numa_bitmask_free(numa_all_nodes_ptr);
 	if (numa_no_nodes_ptr)
 		numa_bitmask_free(numa_no_nodes_ptr);
+	if (numa_memnode_ptr)
+		numa_bitmask_free(numa_memnode_ptr);
 }
 
 /*
@@ -289,13 +292,17 @@ int numa_pagesize(void)
 make_internal_alias(numa_pagesize);
 
 /*
- * Find the highest numbered existing memory node: maxconfigurednode.
+ * Find nodes with memory (numa_memnode_ptr) and the highest numbered
+ * existing node (maxconfigurednode).
  */
 static void
 set_configured_nodes(void)
 {
 	DIR *d;
 	struct dirent *de;
+	long long freep;
+
+	numa_memnode_ptr = numa_allocate_nodemask();
 
 	d = opendir("/sys/devices/system/node");
 	if (!d) {
@@ -306,6 +313,8 @@ set_configured_nodes(void)
 			if (strncmp(de->d_name, "node", 4))
 				continue;
 			nd = strtoul(de->d_name+4, NULL, 0);
+			if (numa_node_size64(nd, &freep) > 0)
+				numa_bitmask_setbit(numa_memnode_ptr, nd);
 			if (maxconfigurednode < nd)
 				maxconfigurednode = nd;
 		}
@@ -569,8 +578,8 @@ static void
 set_sizes(void)
 {
 	sizes_set++;
-	set_configured_nodes();	/* configured nodes listed in /sys */
 	set_nodemask_size();	/* size of kernel nodemask_t */
+	set_configured_nodes();	/* configured nodes listed in /sys */
 	set_numa_max_cpu();	/* size of kernel cpumask_t */
 	set_configured_cpus();	/* cpus listed in /sys/devices/system/cpu */
 	set_task_constraints(); /* cpus and nodes for current task */
@@ -579,7 +588,21 @@ set_sizes(void)
 int
 numa_num_configured_nodes(void)
 {
-	return maxconfigurednode+1;
+	/*
+	* NOTE: this function's behavior matches the documentation (ie: it
+	* returns a count of nodes with memory) despite the poor function
+	* naming.  We also cannot use the similarly poorly named
+	* numa_all_nodes_ptr as it only tracks nodes with memory from which
+	* the calling process can allocate.  Think sparse nodes, memory-less
+	* nodes, cpusets...
+	*/
+	int memnodecount=0, i;
+
+	for (i=0; i <= maxconfigurednode; i++) {
+		if (numa_bitmask_isbitset(numa_memnode_ptr, i))
+			memnodecount++;
+	}
+	return memnodecount;
 }
 
 int
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.