cdown / rpms / util-linux

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