cdown / rpms / util-linux

Forked from rpms/util-linux 2 years ago
Clone
6717ab
diff -up util-linux-2.23.2/sys-utils/lscpu.c.kzak util-linux-2.23.2/sys-utils/lscpu.c
6717ab
--- util-linux-2.23.2/sys-utils/lscpu.c.kzak	2013-07-30 10:39:26.342739583 +0200
6717ab
+++ util-linux-2.23.2/sys-utils/lscpu.c	2014-01-14 11:21:51.837599200 +0100
6717ab
@@ -49,6 +49,7 @@
6717ab
 /* /sys paths */
6717ab
 #define _PATH_SYS_SYSTEM	"/sys/devices/system"
6717ab
 #define _PATH_SYS_CPU		_PATH_SYS_SYSTEM "/cpu"
6717ab
+#define _PATH_SYS_NODE		_PATH_SYS_SYSTEM "/node"
6717ab
 #define _PATH_PROC_XEN		"/proc/xen"
6717ab
 #define _PATH_PROC_XENCAP	_PATH_PROC_XEN "/capabilities"
6717ab
 #define _PATH_PROC_CPUINFO	"/proc/cpuinfo"
6717ab
@@ -157,6 +158,7 @@ struct lscpu_desc {
6717ab
 	cpu_set_t	*online;	/* mask with online CPUs */
6717ab
 
6717ab
 	int		nnodes;		/* number of NUMA modes */
6717ab
+	int		*idx2nodenum;	/* Support for discontinuous nodes */
6717ab
 	cpu_set_t	**nodemaps;	/* array with NUMA nodes */
6717ab
 
6717ab
 	/* books -- based on book_siblings (internal kernel map of cpuX's
6717ab
@@ -802,25 +804,59 @@ read_cache(struct lscpu_desc *desc, int
6717ab
 	}
6717ab
 }
6717ab
 
6717ab
+static inline int is_node_dirent(struct dirent *d)
6717ab
+{
6717ab
+	return
6717ab
+		d &&
6717ab
+#ifdef _DIRENT_HAVE_D_TYPE
6717ab
+		d->d_type == DT_DIR &&
6717ab
+#endif
6717ab
+		strncmp(d->d_name, "node", 4) == 0 &&
6717ab
+		isdigit_string(d->d_name + 4);
6717ab
+}
6717ab
+
6717ab
 static void
6717ab
 read_nodes(struct lscpu_desc *desc)
6717ab
 {
6717ab
-	int i;
6717ab
+	int i = 0;
6717ab
+	DIR *dir;
6717ab
+	struct dirent *d;
6717ab
+	char *path;
6717ab
 
6717ab
 	/* number of NUMA node */
6717ab
-	while (path_exist(_PATH_SYS_SYSTEM "/node/node%d", desc->nnodes))
6717ab
-		desc->nnodes++;
6717ab
+	path = path_strdup(_PATH_SYS_NODE);
6717ab
+	dir = opendir(path);
6717ab
+	free(path);
6717ab
+
6717ab
+	while (dir && (d = readdir(dir))) {
6717ab
+		if (is_node_dirent(d))
6717ab
+			desc->nnodes++;
6717ab
+	}
6717ab
 
6717ab
-	if (!desc->nnodes)
6717ab
+	if (!desc->nnodes) {
6717ab
+		if (dir)
6717ab
+			closedir(dir);
6717ab
 		return;
6717ab
+	}
6717ab
 
6717ab
 	desc->nodemaps = xcalloc(desc->nnodes, sizeof(cpu_set_t *));
6717ab
+	desc->idx2nodenum = xmalloc(desc->nnodes * sizeof(int));
6717ab
+
6717ab
+	if (dir) {
6717ab
+		rewinddir(dir);
6717ab
+		while ((d = readdir(dir)) && i < desc->nnodes) {
6717ab
+			if (is_node_dirent(d))
6717ab
+				desc->idx2nodenum[i++] = strtol_or_err(((d->d_name) + 4),
6717ab
+							_("Failed to extract the node number"));
6717ab
+		}
6717ab
+		closedir(dir);
6717ab
+	}
6717ab
 
6717ab
 	/* information about how nodes share different CPUs */
6717ab
 	for (i = 0; i < desc->nnodes; i++)
6717ab
 		desc->nodemaps[i] = path_read_cpuset(maxcpus,
6717ab
 					_PATH_SYS_SYSTEM "/node/node%d/cpumap",
6717ab
-					i);
6717ab
+					desc->idx2nodenum[i]);
6717ab
 }
6717ab
 
6717ab
 static char *
6717ab
@@ -850,7 +886,7 @@ get_cell_data(struct lscpu_desc *desc, i
6717ab
 	case COL_NODE:
6717ab
 		if (cpuset_ary_isset(cpu, desc->nodemaps,
6717ab
 				     desc->nnodes, setsize, &idx) == 0)
6717ab
-			snprintf(buf, bufsz, "%zd", idx);
6717ab
+			snprintf(buf, bufsz, "%d", desc->idx2nodenum[idx]);
6717ab
 		break;
6717ab
 	case COL_BOOK:
6717ab
 		if (cpuset_ary_isset(cpu, desc->bookmaps,
6717ab
@@ -1250,7 +1286,7 @@ print_summary(struct lscpu_desc *desc, s
6717ab
 	}
6717ab
 
6717ab
 	for (i = 0; i < desc->nnodes; i++) {
6717ab
-		snprintf(buf, sizeof(buf), _("NUMA node%d CPU(s):"), i);
6717ab
+		snprintf(buf, sizeof(buf), _("NUMA node%d CPU(s):"), desc->idx2nodenum[i]);
6717ab
 		print_cpuset(buf, desc->nodemaps[i], mod->hex);
6717ab
 	}
6717ab
 }