Skip to contentSkip to conversation

lscpu: Fix socket information on aarch64 machine

PATCH Superseded Masayoshi Mizuma 2 participants 8 messages Nov 14, 2020 – Nov 20, 2020 Util-Linux package

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

v2 cover lscpu: Fix socket information on aarch64 machine

From: Masayoshi Mizuma <[email protected]>

lscpu may show the wrong number of physical sockets on aarch64 machine as 'Socket(s)'.

That is because lscpu uses a sysfs entry (cpu/cpuX/topology/core_siblings) to get the number of sockets. For aarch64, the sysfs entry is set from MPIDR_EL1 register if the machine doesn't have ACPI PPTT. According to ARM Architecture Reference Manual, the register shows the topology as the affinity, but doesn't show the physical socket information.

There are such aarch64 machines because ARM SBBR v1.0 and v1.1 don't require ACPI PPTT. SBBR v1.2 requires ACPI PPTT.

For the aarch64 machine, probably 'Cluster(s)' is good instead of 'Socket(s)' according to linux/arch/arm64/kernel/topology.c:store_cpu_topology().

To get the number of sockets on the machine, SMBIOS Processor information (Type04) is useful for lscpu because the SMBIOS information is a mandatory feature for the aarch64 machine which is based on ARM SBBR v1.0 and newer.

With these patches, lscpu shows as following on the machine:

For unprivileged user:

    $ lscpu 
    Architecture:            aarch64
    ...
        Socket(s):           -
        Cluster(s):          4

For root:

    # lscpu 
    Architecture:            aarch64
    ...
        Socket(s):           1
        Cluster(s):          4

Changelog:
v2: Rebased to the latest lscpu

Masayoshi Mizuma (5):
lscpu: use cluster on aarch64 machine which doesn't have ACPI PPTT
lscpu-virt: split hypervisor_from_dmi_table()
lscpu-dmi: Move some functions related to DMI to lscpu-dmi
lscpu: add helper to get physical sockets
lscpu: show the number of physical socket on aarch64 machine without
ACPI PPTT

 include/c.h             |   1 +
 sys-utils/Makemodule.am |   1 +
 sys-utils/lscpu-arm.c   |  25 ++++++++++
 sys-utils/lscpu-dmi.c   | 108 ++++++++++++++++++++++++++++++++++++++++
 sys-utils/lscpu-virt.c  |  62 ++++++-----------------
 sys-utils/lscpu.1       |   3 ++
 sys-utils/lscpu.c       |  47 ++++++++++++++---
 sys-utils/lscpu.h       |  39 +++++++--------
 8 files changed, 209 insertions(+), 77 deletions(-)
 create mode 100644 sys-utils/lscpu-dmi.c
Signature · 1 line
2.27.0
#1 lore raw

PATCH v2 1/5 lscpu: use cluster on aarch64 machine which doesn't have ACPI PPTT

From: Masayoshi Mizuma <[email protected]>

lscpu may show the wrong number of sockets if the machine is aarch64 and doesn't have ACPI PPTT.

That's because lscpu shows the number of sockets by using a sysfs entry (cpu/cpuX/topology/core_siblings). The sysfs entry is set by MPIDR_EL1 register if the machine doesn't have ACPI PPTT. MPIDR_EL1 doesn't show the physical socket information directly. It shows the affinity level.

According to linux/arch/arm64/kernel/topology.c:store_cpu_topology(), the top level of affinity is called as 'Cluster'.

Use Cluster instead of Socket on the machine which doesn't have ACPI PPTT.

This patch is useful for aarch64 machine which is based on ARM SBBR v1.0 and v1.1, the specs don't require ACPI PPTT. ARM SBBR v1.2 requires ACPI PPTT.

---
 include/c.h           |  1 +
 sys-utils/lscpu-arm.c | 23 +++++++++++++++++++++++
 sys-utils/lscpu.1     |  3 +++
 sys-utils/lscpu.c     | 42 +++++++++++++++++++++++++++++++++++-------
 sys-utils/lscpu.h     |  3 +++
 5 files changed, 65 insertions(+), 7 deletions(-)
include/c.h +1 -0
index ae0813109..e92636177 100644
@@ -16,6 +16,7 @@
 #include <stdlib.h>
 #include <string.h>
 #include <errno.h>
+#include <sys/utsname.h>
 
 #include <assert.h>
 
sys-utils/lscpu-arm.c +23 -0
index 1cbc9775f..7ef05e8e7 100644
@@ -362,10 +362,33 @@ static void arm_decode(struct lscpu_cxt *cxt, struct lscpu_cputype *ct)
 	arm_rXpY_decode(ct);
 }
 
+int lscpu_is_cluster_arm(struct lscpu_cxt *cxt)
+{
+	char *arch;
+	struct stat st;
+	struct utsname utsbuf;
+
+	if (cxt)
+		arch = cxt->arch->name;
+	else {
+		if (uname(&utsbuf) == -1)
+			err(EXIT_FAILURE, _("error: uname failed"));
+		arch = utsbuf.machine;
+	}
+
+	if (!(strcmp(arch, "aarch64")) && (stat(_PATH_ACPI_PPTT, &st) < 0)
+			&& (cxt->ncputypes == 1))
+		return 1;
+	else
+		return 0;
+}
+
 void lscpu_decode_arm(struct lscpu_cxt *cxt)
 {
 	size_t i;
 
+	cxt->is_cluster = lscpu_is_cluster_arm(cxt);
+
 	for (i = 0; i < cxt->ncputypes; i++)
 		arm_decode(cxt, cxt->cputypes[i]);
 }
sys-utils/lscpu.1 +3 -0
index ed14dc663..a41125395 100644
@@ -55,6 +55,9 @@ The logical core number.  A core can contain several CPUs.
 .B SOCKET
 The logical socket number.  A socket can contain several cores.
 .TP
+.B CLUSTER
+The logical cluster number.  A cluster can contain several cores.
+.TP
 .B BOOK
 The logical book number.  A book can contain several sockets.
 .TP
sys-utils/lscpu.c +35 -7

95 lines not on this page · raw message

sys-utils/lscpu.h +3 -0
index 465bd3fe2..227e7f391 100644
@@ -33,6 +33,7 @@ UL_DEBUG_DECLARE_MASK(lscpu);
 #define _PATH_SYS_NODE		_PATH_SYS_SYSTEM "/node"
 #define _PATH_SYS_DMI		"/sys/firmware/dmi/tables/DMI"
 #define _PATH_SYS_DMI_TYPE4	"/sys/firmware/dmi/entries/4-0/raw"
+#define _PATH_ACPI_PPTT		"/sys/firmware/acpi/tables/PPTT"
 
 struct lscpu_cache {
 	int		id;		/* unique identifier */
@@ -242,6 +243,7 @@ struct lscpu_cxt {
 		     json : 1,
 		     bytes : 1;
 
+	int is_cluster; /* For aarch64 if the machine doesn't have ACPI PPTT */
 };
 
 #define is_cpu_online(_cxt, _cpu) \
@@ -291,6 +293,7 @@ int lscpu_cpu_set_type(struct lscpu_cpu *cpu, struct lscpu_cputype *type);
 int lscpu_create_cpus(struct lscpu_cxt *cxt, cpu_set_t *cpuset, size_t setsize);
 struct lscpu_cpu *lscpu_cpus_loopup_by_type(struct lscpu_cxt *cxt, struct lscpu_cputype *ct);
 
+int lscpu_is_cluster_arm(struct lscpu_cxt *cxt);
 void lscpu_decode_arm(struct lscpu_cxt *cxt);
 
 int lookup(char *line, char *pattern, char **value);
Signature · 1 line
2.27.0
maintainer
#2 lore raw

Re: [PATCH v2 1/5] lscpu: use cluster on aarch64 machine which doesn't have ACPI PPTT

Quoted 7 lines · Masayoshi Mizuma

cpu_column_name_to_id(const char *name, size_t namesz) {

sys-utils/lscpu.c +1 -0
 	size_t i;
+	int is_cluster = lscpu_is_cluster_arm(NULL);
 
 	for (i = 0; i < ARRAY_SIZE(coldescs_cpu); i++) {
 		const char *cn = coldescs_cpu[i].name;

sys-utils/lscpu.c static int · line 198

Quoted 10 lines
sys-utils/lscpu.c +8 -1
 
-		if (!strncasecmp(name, cn, namesz) && !*(cn + namesz))
+		if (!strncasecmp(name, cn, namesz) && !*(cn + namesz)) {
+			if ((!strncasecmp(cn, "cluster", namesz)) && (!is_cluster)) {
+				warnx(_("%s doesn't work on this machine. Use socket."), name);
+				return -1;
+			} else if ((!strncasecmp(cn, "socket", namesz)) && (is_cluster)) {
+				warnx(_("%s doesn't work on this machine. Use cluster."), name);
+				return -1;
+			}

This is very unusual for your utils and it makes scripts with lscpu non-portable. It would be better to remove this change.

We usually follow the columns as specified by user and if we can't fill any data then we return nothing (or "-"). For example you can use "-o DRAWER" on system where this stuff is not supported.

sys-utils/lscpu.c static char *get_cell_data( · line 353

Quoted 12 lines
sys-utils/lscpu.c +2 -0
 			return i;
+		}
 	}
 	warnx(_("unknown column: %s"), name);
 	return -1;
@@ -337,6 +348,7 @@ static char *get_cell_data(
 		fill_id(cxt, cpu, core, buf, bufsz);
 		break;
 	case COL_CPU_SOCKET:
+	case COL_CPU_CLUSTER:
 		fill_id(cxt, cpu, socket, buf, bufsz);
 		break;

What about:

  case COL_CPU_SOCKET:
    fill_id(cxt, cpu, socket, buf, bufsz); 
    break;
  case COL_CPU_CLUSTER:
    if (cxt->is_cluster)
        fill_id(cxt, cpu, socket, buf, bufsz);
    break;

It means "SOCKET" works everywhere, "CLUSTER" returns data only on ARMs with cluster(s).

Karel

Signature · 2 lines
 Karel Zak  <[email protected]>
 http://karelzak.blogspot.com
#3 lore raw

Re: [PATCH v2 1/5] lscpu: use cluster on aarch64 machine which doesn't have ACPI PPTT

Quoted 22 lines · Karel Zak
Quoted 7 lines · Masayoshi Mizuma

cpu_column_name_to_id(const char *name, size_t namesz) {

sys-utils/lscpu.c +1 -0
 	size_t i;
+	int is_cluster = lscpu_is_cluster_arm(NULL);
 
 	for (i = 0; i < ARRAY_SIZE(coldescs_cpu); i++) {
 		const char *cn = coldescs_cpu[i].name;

sys-utils/lscpu.c static int · line 198

Quoted 10 lines
sys-utils/lscpu.c +8 -1
 
-		if (!strncasecmp(name, cn, namesz) && !*(cn + namesz))
+		if (!strncasecmp(name, cn, namesz) && !*(cn + namesz)) {
+			if ((!strncasecmp(cn, "cluster", namesz)) && (!is_cluster)) {
+				warnx(_("%s doesn't work on this machine. Use socket."), name);
+				return -1;
+			} else if ((!strncasecmp(cn, "socket", namesz)) && (is_cluster)) {
+				warnx(_("%s doesn't work on this machine. Use cluster."), name);
+				return -1;
+			}

This is very unusual for your utils and it makes scripts with lscpu non-portable. It would be better to remove this change.

Quoted 3 lines

We usually follow the columns as specified by user and if we can't fill any data then we return nothing (or "-"). For example you can use "-o DRAWER" on system where this stuff is not supported.

Thanks, got it. I'll remove this change.

Quoted 24 lines

sys-utils/lscpu.c static char *get_cell_data( · line 353

Quoted 13 lines
sys-utils/lscpu.c +2 -0
 			return i;
+		}
 	}
 	warnx(_("unknown column: %s"), name);
 	return -1;
@@ -337,6 +348,7 @@ static char *get_cell_data(
 		fill_id(cxt, cpu, core, buf, bufsz);
 		break;
 	case COL_CPU_SOCKET:
+	case COL_CPU_CLUSTER:
 		fill_id(cxt, cpu, socket, buf, bufsz);
 		break;

What about:

  case COL_CPU_SOCKET:
    fill_id(cxt, cpu, socket, buf, bufsz); 
    break;
  case COL_CPU_CLUSTER:
    if (cxt->is_cluster)
        fill_id(cxt, cpu, socket, buf, bufsz);
    break;
Quoted 2 lines

It means "SOCKET" works everywhere, "CLUSTER" returns data only on ARMs with cluster(s).

Great idea! I'll fix it as that.

Thanks!
Masa

#4 lore raw

PATCH v2 2/5 lscpu-virt: split hypervisor_from_dmi_table()

In reply to Masayoshi Mizuma

From: Masayoshi Mizuma <[email protected]>

Split hypervisor_from_dmi_table() to parsing dmi table and checking the hypervisor vendor.

---
 sys-utils/lscpu-virt.c | 54 ++++++++++++++++++++++++++----------------
 sys-utils/lscpu.h      |  6 +++++
 2 files changed, 40 insertions(+), 20 deletions(-)
sys-utils/lscpu-virt.c +34 -20
index 527f7caa4..32558fd9f 100644
@@ -76,20 +76,13 @@ nothing:
 	return NULL;
 }
 
-static int hypervisor_from_dmi_table(uint32_t base, uint16_t len,
-				uint16_t num, const char *devmem)
+static int parse_dmi_table(uint16_t len, uint16_t num,
+				uint8_t *data,
+				struct dmi_info *di)
 {
-	uint8_t *buf;
-	uint8_t *data;
+	uint8_t *buf = data;
+	int rc = -1;
 	int i = 0;
-	char *vendor = NULL;
-	char *product = NULL;
-	char *manufacturer = NULL;
-	int rc = VIRT_VENDOR_NONE;
-
-	data = buf = get_mem_chunk(base, len, devmem);
-	if (!buf)
-		goto done;
 
 	 /* 4 is the length of an SMBIOS structure header */
 	while (i < num && data + 4 <= buf + len) {
@@ -113,11 +106,11 @@ static int hypervisor_from_dmi_table(uint32_t base, uint16_t len,
 		next += 2;
 		switch (h.type) {
 			case 0:
-				vendor = dmi_string(&h, data[0x04]);
+				di->vendor = dmi_string(&h, data[0x04]);
 				break;
 			case 1:
-				manufacturer = dmi_string(&h, data[0x04]);
-				product = dmi_string(&h, data[0x05]);
+				di->manufacturer = dmi_string(&h, data[0x04]);
+				di->product = dmi_string(&h, data[0x05]);
 				break;
 			default:
 				break;
@@ -126,15 +119,36 @@ static int hypervisor_from_dmi_table(uint32_t base, uint16_t len,
 		data = next;
 		i++;
 	}
-	if (manufacturer && !strcmp(manufacturer, "innotek GmbH"))
+	rc = 0;
+done:
+	return rc;
+}
+
+static int hypervisor_from_dmi_table(uint32_t base, uint16_t len,
+				uint16_t num, const char *devmem)
+{
+	uint8_t *data;
+	int rc = VIRT_VENDOR_NONE;
+	struct dmi_info di;
+
+	data = get_mem_chunk(base, len, devmem);
+	if (!data)
+		return rc;
+
+	memset(&di, 0, sizeof(struct dmi_info));
+	rc = parse_dmi_table(len, num, data, &di);
+	if (rc < 0)
+		goto done;
+
+	if (di.manufacturer && !strcmp(di.manufacturer, "innotek GmbH"))
 		rc = VIRT_VENDOR_INNOTEK;
-	else if (manufacturer && strstr(manufacturer, "HITACHI") &&
-					product && strstr(product, "LPAR"))
+	else if (di.manufacturer && strstr(di.manufacturer, "HITACHI") &&
+					di.product && strstr(di.product, "LPAR"))
 		rc = VIRT_VENDOR_HITACHI;
-	else if (vendor && !strcmp(vendor, "Parallels"))
+	else if (di.vendor && !strcmp(di.vendor, "Parallels"))
 		rc = VIRT_VENDOR_PARALLELS;
 done:
-	free(buf);
+	free(data);
 	return rc;
 }
 
sys-utils/lscpu.h +6 -0
index 227e7f391..b199de3dd 100644
@@ -306,6 +306,12 @@ struct lscpu_dmi_header
 	uint8_t *data;
 };
 
+struct dmi_info {
+	char *vendor;
+	char *product;
+	char *manufacturer;
+};
+
 static inline void to_dmi_header(struct lscpu_dmi_header *h, uint8_t *data)
 {
 	h->type = data[0];
Signature · 1 line
2.27.0
#5 lore raw

PATCH v2 3/5 lscpu-dmi: Move some functions related to DMI to lscpu-dmi

In reply to Masayoshi Mizuma

From: Masayoshi Mizuma <[email protected]>

Move some functions related to DMI to lscpu-dmi.

---
 sys-utils/Makemodule.am |  1 +
 sys-utils/lscpu-dmi.c   | 79 +++++++++++++++++++++++++++++++++++++++++
 sys-utils/lscpu-virt.c  | 50 +-------------------------
 sys-utils/lscpu.h       | 28 +++------------
 4 files changed, 86 insertions(+), 72 deletions(-)
 create mode 100644 sys-utils/lscpu-dmi.c
sys-utils/Makemodule.am +1 -0
index 5459a3a2e..df8fd403a 100644
@@ -396,6 +396,7 @@ lscpu_SOURCES = sys-utils/lscpu.c \
 		sys-utils/lscpu-topology.c \
 		sys-utils/lscpu-virt.c \
 		sys-utils/lscpu-arm.c \
+		sys-utils/lscpu-dmi.c \
 		sys-utils/lscpu.h
 lscpu_LDADD = $(LDADD) libcommon.la libsmartcols.la $(RTAS_LIBS)
 lscpu_CFLAGS = $(AM_CFLAGS) -I$(ul_libsmartcols_incdir)
sys-utils/lscpu-dmi.c new file +79 -0

79 lines not on this page · raw message

sys-utils/lscpu-virt.c +1 -49

62 lines not on this page · raw message

sys-utils/lscpu.h +5 -23
index b199de3dd..565f6aee5 100644
@@ -298,6 +298,8 @@ void lscpu_decode_arm(struct lscpu_cxt *cxt);
 
 int lookup(char *line, char *pattern, char **value);
 
+void *get_mem_chunk(size_t base, size_t len, const char *devmem);
+
 struct lscpu_dmi_header
 {
 	uint8_t type;
@@ -312,28 +314,8 @@ struct dmi_info {
 	char *manufacturer;
 };
 
-static inline void to_dmi_header(struct lscpu_dmi_header *h, uint8_t *data)
-{
-	h->type = data[0];
-	h->length = data[1];
-	memcpy(&h->handle, data + 2, sizeof(h->handle));
-	h->data = data;
-}
-
-static inline char *dmi_string(const struct lscpu_dmi_header *dm, uint8_t s)
-{
-	char *bp = (char *)dm->data;
-
-	if (!s || !bp)
-		return NULL;
-
-	bp += dm->length;
-	while (s > 1 && *bp) {
-		bp += strlen(bp);
-		bp++;
-		s--;
-	}
 
-	return !*bp ? NULL : bp;
-}
+void to_dmi_header(struct lscpu_dmi_header *h, uint8_t *data);
+char *dmi_string(const struct lscpu_dmi_header *dm, uint8_t s);
+int parse_dmi_table(uint16_t len, uint16_t num, uint8_t *data, struct dmi_info *di);
 #endif /* LSCPU_H */
Signature · 1 line
2.27.0
#6 lore raw

PATCH v2 4/5 lscpu: add helper to get physical sockets

In reply to Masayoshi Mizuma

From: Masayoshi Mizuma <[email protected]>

Add a helper function, get_number_of_physical_sockets_from_dmi(), to get physical sockets from DMI table in case of the sysfs for cpu topology doesn't have the physical socket information.

get_number_of_physical_sockets_from_dmi() parse the DMI table and counts the number of SMBIOS Processor Information (Type04) structure.

---
 sys-utils/lscpu-dmi.c | 29 +++++++++++++++++++++++++++++
 sys-utils/lscpu.h     |  2 ++
 2 files changed, 31 insertions(+)
sys-utils/lscpu-dmi.c +29 -0
index 00d4439f8..e7ffa88d3 100644
@@ -66,6 +66,9 @@ int parse_dmi_table(uint16_t len, uint16_t num,
 			di->manufacturer = dmi_string(&h, data[0x04]);
 			di->product = dmi_string(&h, data[0x05]);
 			break;
+		case 4:
+			di->sockets++;
+			break;
 		default:
 			break;
 		}
@@ -77,3 +80,29 @@ int parse_dmi_table(uint16_t len, uint16_t num,
 done:
 	return rc;
 }
+
+size_t get_number_of_physical_sockets_from_dmi(void)
+{
+	static char const sys_fw_dmi_tables[] = _PATH_SYS_DMI;
+	struct dmi_info di;
+	struct stat st;
+	uint8_t *data;
+	int rc = 0;
+
+	if (stat(sys_fw_dmi_tables, &st))
+		return rc;
+
+	data = get_mem_chunk(0, st.st_size, sys_fw_dmi_tables);
+	if (!data)
+		return rc;
+
+	memset(&di, 0, sizeof(struct dmi_info));
+	rc = parse_dmi_table(st.st_size, st.st_size/4, data, &di);
+
+	free(data);
+
+	if ((rc < 0) || !di.sockets)
+		return 0;
+	else
+		return di.sockets;
+}
sys-utils/lscpu.h +2 -0
index 565f6aee5..d137b94b1 100644
@@ -312,10 +312,12 @@ struct dmi_info {
 	char *vendor;
 	char *product;
 	char *manufacturer;
+	int sockets;
 };
 
 
 void to_dmi_header(struct lscpu_dmi_header *h, uint8_t *data);
 char *dmi_string(const struct lscpu_dmi_header *dm, uint8_t s);
 int parse_dmi_table(uint16_t len, uint16_t num, uint8_t *data, struct dmi_info *di);
+size_t get_number_of_physical_sockets_from_dmi(void);
 #endif /* LSCPU_H */
Signature · 1 line
2.27.0
#7 lore raw

PATCH v2 5/5 lscpu: show the number of physical socket on aarch64 machine without ACPI PPTT

In reply to Masayoshi Mizuma

From: Masayoshi Mizuma <[email protected]>

Show the number of the number of physical socket even if the sysfs doesn't have the physical socket information.

Note, lscpu shows the number of physical socket as 'Socket(s):' only if root user runs it because accessing the DMI table requires root privilege.

---
 sys-utils/lscpu-arm.c | 2 ++
 sys-utils/lscpu.c     | 9 +++++++--
 sys-utils/lscpu.h     | 2 ++
 3 files changed, 11 insertions(+), 2 deletions(-)
sys-utils/lscpu-arm.c +2 -0
index 7ef05e8e7..72c2abf65 100644
@@ -360,6 +360,8 @@ static void arm_decode(struct lscpu_cxt *cxt, struct lscpu_cputype *ct)
 
 	arm_ids_decode(ct);
 	arm_rXpY_decode(ct);
+	if (cxt->is_cluster)
+		ct->nr_socket_on_cluster = get_number_of_physical_sockets_from_dmi();
 }
 
 int lscpu_is_cluster_arm(struct lscpu_cxt *cxt)
sys-utils/lscpu.c +7 -2
index c5a4cc984..0c7d86a00 100644
@@ -868,10 +868,15 @@ print_summary_cputype(struct lscpu_cxt *cxt,
 		} else
 			add_summary_n(tb, sec, _("Book(s):"), ct->nbooks_per_drawer ?: ct->nbooks);
 	} else {
-		if (cxt->is_cluster)
+		if (cxt->is_cluster) {
+			if (ct->nr_socket_on_cluster > 0)
+				add_summary_n(tb, sec, _("Socket(s):"), ct->nr_socket_on_cluster);
+			else
+				add_summary_s(tb, sec, _("Socket(s):"), "-");
+
 			add_summary_n(tb, sec, _("Cluster(s):"),
 					ct->nsockets_per_book ?: ct->nsockets);
-		else
+		} else
 			add_summary_n(tb, sec, _("Socket(s):"),
 					ct->nsockets_per_book ?: ct->nsockets);
 	}
sys-utils/lscpu.h +2 -0
index d137b94b1..4511c317c 100644
@@ -101,6 +101,8 @@ struct lscpu_cputype {
 			has_configured : 1,
 			has_polarization : 1,
 			has_addresses : 1;
+
+	size_t nr_socket_on_cluster; /* the number of sockets if the is_cluster is 1 */
 };
 
 /* dispatching modes */
Signature · 1 line
2.27.0