Skip to contentSkip to conversation

perf pmu intel: Adjust cpumaks for sub-NUMA clusters on graniterapids

PATCH Superseded Ian Rogers 3 participants 9 messages May 15, 2025 perf events

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Ian Rogers

author reviewer
#0 lore raw

PATCH 1/1 perf pmu intel: Adjust cpumaks for sub-NUMA clusters on graniterapids

On graniterapids the cache home agent (CHA) and memory controller (IMC) PMUs all have their cpumask set to per-socket information. In order for per NUMA node aggregation to work correctly the PMUs cpumask needs to be set to CPUs for the relevant sub-NUMA grouping.

For example, on a 2 socket graniterapids machine with sub NUMA clustering of 3, for uncore_cha and uncore_imc PMUs the cpumask is "0,120" leading to aggregation only on NUMA nodes 0 and 3:

```
$ perf stat --per-node -e 'UNC_CHA_CLOCKTICKS,UNC_M_CLOCKTICKS' -a sleep 1

 Performance counter stats for 'system wide':

N0        1    277,835,681,344      UNC_CHA_CLOCKTICKS
N0        1     19,242,894,228      UNC_M_CLOCKTICKS
N3        1    277,803,448,124      UNC_CHA_CLOCKTICKS
N3        1     19,240,741,498      UNC_M_CLOCKTICKS

       1.002113847 seconds time elapsed
```

By updating the PMUs cpumasks to "0,120", "40,160" and "80,200" then the correctly 6 NUMA node aggregations are achieved:

```
$ perf stat --per-node -e 'UNC_CHA_CLOCKTICKS,UNC_M_CLOCKTICKS' -a sleep 1

 Performance counter stats for 'system wide':

N0        1     92,748,667,796      UNC_CHA_CLOCKTICKS
N0        0      6,424,021,142      UNC_M_CLOCKTICKS
N1        0     92,753,504,424      UNC_CHA_CLOCKTICKS
N1        1      6,424,308,338      UNC_M_CLOCKTICKS
N2        0     92,751,170,084      UNC_CHA_CLOCKTICKS
N2        0      6,424,227,402      UNC_M_CLOCKTICKS
N3        1     92,745,944,144      UNC_CHA_CLOCKTICKS
N3        0      6,423,752,086      UNC_M_CLOCKTICKS
N4        0     92,725,793,788      UNC_CHA_CLOCKTICKS
N4        1      6,422,393,266      UNC_M_CLOCKTICKS
N5        0     92,717,504,388      UNC_CHA_CLOCKTICKS
N5        0      6,421,842,618      UNC_M_CLOCKTICKS

       1.003406645 seconds time elapsed
```

In general, having the perf tool adjust cpumasks isn't desirable as ideally the PMU driver would be advertising the correct cpumask.

---
 tools/perf/arch/x86/util/pmu.c | 259 ++++++++++++++++++++++++++++++++-
 1 file changed, 254 insertions(+), 5 deletions(-)
tools/perf/arch/x86/util/pmu.c +254 -5
index 8712cbbbc712..38c800c6e9c8 100644
@@ -8,6 +8,8 @@
 #include <linux/perf_event.h>
 #include <linux/zalloc.h>
 #include <api/fs/fs.h>
+#include <api/io_dir.h>
+#include <internal/cpumap.h>
 #include <errno.h>
 
 #include "../../../util/intel-pt.h"
@@ -16,7 +18,247 @@
 #include "../../../util/fncache.h"
 #include "../../../util/pmus.h"
 #include "mem-events.h"
+#include "util/debug.h"
 #include "util/env.h"
+#include "util/header.h"
+
+static bool x86__is_intel_graniterapids(void)
+{
+	static bool checked_if_graniterapids;
+	static bool is_graniterapids;
+
+	if (!checked_if_graniterapids) {
+		const char *graniterapids_cpuid = "GenuineIntel-6-A[DE]";
+		char *cpuid = get_cpuid_str((struct perf_cpu){0});
+
+		is_graniterapids = cpuid && strcmp_cpuid_str(graniterapids_cpuid, cpuid) == 0;
+		free(cpuid);
+		checked_if_graniterapids = true;
+	}
+	return is_graniterapids;
+}
+
+static struct perf_cpu_map *read_sysfs_cpu_map(const char *sysfs_path)
+{
+	struct perf_cpu_map *cpus;
+	char *buf = NULL;
+	size_t buf_len;
+
+	if (sysfs__read_str(sysfs_path, &buf, &buf_len) < 0)
+		return NULL;
+
+	cpus = perf_cpu_map__new(buf);
+	free(buf);
+	return cpus;
+}
+
+static int snc_nodes_per_l3_cache(void)
+{
+	static bool checked_snc;
+	static int snc_nodes;
+
+	if (!checked_snc) {
+		struct perf_cpu_map *node_cpus =
+			read_sysfs_cpu_map("devices/system/node/node0/cpulist");
+		struct perf_cpu_map *cache_cpus =
+			read_sysfs_cpu_map("devices/system/cpu/cpu0/cache/index3/shared_cpu_list");
+
+		snc_nodes = perf_cpu_map__nr(cache_cpus) / perf_cpu_map__nr(node_cpus);
+		perf_cpu_map__put(cache_cpus);
+		perf_cpu_map__put(node_cpus);
+		checked_snc = true;
+	}
+	return snc_nodes;
+}
+
+static bool starts_with(const char *str, const char *prefix)
+{
+	return !strncmp(prefix, str, strlen(prefix));
+}
+
+static int num_chas(void)
+{
+	static bool checked_chas;
+	static int num_chas;
+
+	if (!checked_chas) {
+		int fd = perf_pmu__event_source_devices_fd();
+		struct io_dir dir;
+		struct io_dirent64 *dent;
+
+		if (fd < 0)
+			return -1;
+
+		io_dir__init(&dir, fd);
+
+		while ((dent = io_dir__readdir(&dir)) != NULL) {
+			/* Note, dent->d_type will be DT_LNK and so isn't a useful filter. */
+			if (starts_with(dent->d_name, "uncore_cha_"))
+				num_chas++;
+		}
+		close(fd);
+		checked_chas = true;
+	}
+	return num_chas;
+}
+
+#define MAX_SNCS 6
+
+static int uncore_cha_snc(struct perf_pmu *pmu)
+{
+	// CHA SNC numbers are ordered correspond to the CHAs number.
+	unsigned int cha_num;
+	int num_cha, chas_per_node, cha_snc;
+	int snc_nodes = snc_nodes_per_l3_cache();
+
+	if (snc_nodes <= 1)
+		return 0;
+
+	num_cha = num_chas();
+	if (num_cha <= 0) {
+		pr_warning("Unexpected: no CHAs found\n");
+		return 0;
+	}
+
+	/* Compute SNC for PMU. */
+	if (sscanf(pmu->name, "uncore_cha_%u", &cha_num) != 1) {
+		pr_warning("Unexpected: unable to compute CHA number '%s'\n", pmu->name);
+		return 0;
+	}
+	chas_per_node = num_cha / snc_nodes;
+	cha_snc = cha_num / chas_per_node;
+
+	/* Range check cha_snc. for unexpected out of bounds. */
+	return cha_snc >= MAX_SNCS ? 0 : cha_snc;
+}
+
+static int uncore_imc_snc(struct perf_pmu *pmu)
+{
+	// Compute the IMC SNC using lookup tables.
+	unsigned int imc_num;
+	int snc_nodes = snc_nodes_per_l3_cache();
+	const u8 snc2_map[] = {1, 1, 0, 0, 1, 1, 0, 0};
+	const u8 snc3_map[] = {1, 1, 0, 0, 2, 2, 1, 1, 0, 0, 2, 2};
+	const u8 *snc_map;
+	size_t snc_map_len;
+
+	switch (snc_nodes) {
+	case 2:
+		snc_map = snc2_map;
+		snc_map_len = ARRAY_SIZE(snc2_map);
+		break;
+	case 3:
+		snc_map = snc3_map;
+		snc_map_len = ARRAY_SIZE(snc3_map);
+		break;
+	default:
+		/* Error or no lookup support for SNC with >3 nodes. */
+		return 0;
+	}
+
+	/* Compute SNC for PMU. */
+	if (sscanf(pmu->name, "uncore_imc_%u", &imc_num) != 1) {
+		pr_warning("Unexpected: unable to compute IMC number '%s'\n", pmu->name);
+		return 0;
+	}
+	if (imc_num >= snc_map_len) {
+		pr_warning("Unexpected IMC %d for SNC%d mapping\n", imc_num, snc_nodes);
+		return 0;
+	}
+	return snc_map[imc_num];
+}
+
+static int uncore_cha_imc_compute_cpu_adjust(int pmu_snc)
+{
+	static bool checked_cpu_adjust[MAX_SNCS];
+	static int cpu_adjust[MAX_SNCS];
+	struct perf_cpu_map *node_cpus;
+	char node_path[] = "devices/system/node/node0/cpulist";
+
+	/* Was adjust already computed? */
+	if (checked_cpu_adjust[pmu_snc])
+		return cpu_adjust[pmu_snc];
+
+	/* SNC0 doesn't need an adjust. */
+	if (pmu_snc == 0) {
+		cpu_adjust[0] = 0;
+		checked_cpu_adjust[0] = true;
+		return 0;
+	}
+
+	/*
+	 * Use NUMA topology to compute first CPU of the NUMA node, we want to
+	 * adjust CPU 0 to be this and similarly for other CPUs if there is >1
+	 * socket.
+	 */
+	node_path[24] = pmu_snc; // Shift node0 to be node<pmu_snc>.
+	node_cpus = read_sysfs_cpu_map(node_path);
+	cpu_adjust[pmu_snc] = perf_cpu_map__cpu(node_cpus, 0).cpu;
+	checked_cpu_adjust[pmu_snc] = true;
+	perf_cpu_map__put(node_cpus);
+	return cpu_adjust[pmu_snc];
+}
+
+static void gnr_uncore_cha_imc_adjust_cpumask_for_snc(struct perf_pmu *pmu, bool cha)
+{
+	// With sub-NUMA clustering (SNC) there is a NUMA node per SNC in the
+	// topology. For example, a two socket graniterapids machine may be set
+	// up with 3-way SNC meaning there are 6 NUMA nodes that should be
+	// displayed with --per-node. The cpumask of the CHA and IMC PMUs
+	// reflects per-socket information meaning, for example, uncore_cha_60
+	// on a two socket graniterapids machine with 120 cores per socket will
+	// have a cpumask of "0,120". This cpumask needs adjusting to "40,160"
+	// to reflect that uncore_cha_60 is used for the 2nd SNC of each
+	// socket. Without the adjustment events on uncore_cha_60 will appear in
+	// node 0 and node 3 (in our example 2 socket 3-way set up), but with
+	// the adjustment they will appear in node 1 and node 4. The number of
+	// CHAs is typically larger than the number of cores. The CHA numbers
+	// are assumed to split evenly and inorder wrt core numbers. There are
+	// fewer memory IMC PMUs than cores and mapping is handled using lookup
+	// tables.
+	static struct perf_cpu_map *cha_adjusted[MAX_SNCS];
+	static struct perf_cpu_map *imc_adjusted[MAX_SNCS];
+	struct perf_cpu_map **adjusted = cha ? cha_adjusted : imc_adjusted;
+	int idx, pmu_snc, cpu_adjust;
+	struct perf_cpu cpu;
+	bool alloc;
+
+	// Cpus from the kernel holds first CPU of each socket. e.g. 0,120
+	assert(perf_cpu_map__cpu(pmu->cpus, 0).cpu == 0);
+
+	pmu_snc = cha ? uncore_cha_snc(pmu) : uncore_imc_snc(pmu);
+	if (pmu_snc == 0) {
+		// No adjustment necessary for the first SNC.
+		return;
+	}
+
+	alloc = adjusted[pmu_snc] == NULL;
+	if (alloc) {
+		// Hold onto the perf_cpu_map globally to avoid recomputation.
+		cpu_adjust = uncore_cha_imc_compute_cpu_adjust(pmu_snc);
+		adjusted[pmu_snc] = perf_cpu_map__empty_new(perf_cpu_map__nr(pmu->cpus));
+		if (!adjusted[pmu_snc])
+			return;
+	}
+
+	perf_cpu_map__for_each_cpu(cpu, idx, pmu->cpus) {
+		// Compute the new cpu map values or if not allocating, assert
+		// that they match expectations. asserts will be removed to
+		// avoid overhead in NDEBUG builds.
+		if (alloc) {
+			adjusted[pmu_snc]->map[idx].cpu = cpu.cpu + cpu_adjust;
+		} else if (idx == 0) {
+			cpu_adjust = perf_cpu_map__cpu(adjusted[pmu_snc], idx).cpu - cpu.cpu;
+			assert(uncore_cha_imc_compute_cpu_adjust(pmu_snc) == cpu_adjust);
+		} else {
+			assert(perf_cpu_map__cpu(adjusted[pmu_snc], idx).cpu ==
+			       cpu.cpu + cpu_adjust);
+		}
+	}
+
+	perf_cpu_map__put(pmu->cpus);
+	pmu->cpus = perf_cpu_map__get(adjusted[pmu_snc]);
+}
 
 void perf_pmu__arch_init(struct perf_pmu *pmu)
 {
@@ -49,10 +291,17 @@ void perf_pmu__arch_init(struct perf_pmu *pmu)
 
 		perf_mem_events__loads_ldlat = 0;
 		pmu->mem_events = perf_mem_events_amd_ldlat;
-	} else if (pmu->is_core) {
-		if (perf_pmu__have_event(pmu, "mem-loads-aux"))
-			pmu->mem_events = perf_mem_events_intel_aux;
-		else
-			pmu->mem_events = perf_mem_events_intel;
+	} else {
+		if (pmu->is_core) {
+			if (perf_pmu__have_event(pmu, "mem-loads-aux"))
+				pmu->mem_events = perf_mem_events_intel_aux;
+			else
+				pmu->mem_events = perf_mem_events_intel;
+		} else if (x86__is_intel_graniterapids()) {
+			if (starts_with(pmu->name, "uncore_cha_"))
+				gnr_uncore_cha_imc_adjust_cpumask_for_snc(pmu, /*cha=*/true);
+			else if (starts_with(pmu->name, "uncore_imc_"))
+				gnr_uncore_cha_imc_adjust_cpumask_for_snc(pmu, /*cha=*/false);
+		}
 	}
 }
Signature · 1 line
2.49.0.1045.g170613ef41-goog
author reviewer
#1 lore raw
Quoted 234 lines · Ian Rogers

On graniterapids the cache home agent (CHA) and memory controller (IMC) PMUs all have their cpumask set to per-socket information. In order for per NUMA node aggregation to work correctly the PMUs cpumask needs to be set to CPUs for the relevant sub-NUMA grouping.

For example, on a 2 socket graniterapids machine with sub NUMA clustering of 3, for uncore_cha and uncore_imc PMUs the cpumask is "0,120" leading to aggregation only on NUMA nodes 0 and 3:

```
$ perf stat --per-node -e 'UNC_CHA_CLOCKTICKS,UNC_M_CLOCKTICKS' -a sleep 1

 Performance counter stats for 'system wide':

N0        1    277,835,681,344      UNC_CHA_CLOCKTICKS
N0        1     19,242,894,228      UNC_M_CLOCKTICKS
N3        1    277,803,448,124      UNC_CHA_CLOCKTICKS
N3        1     19,240,741,498      UNC_M_CLOCKTICKS

       1.002113847 seconds time elapsed
```

By updating the PMUs cpumasks to "0,120", "40,160" and "80,200" then the correctly 6 NUMA node aggregations are achieved:

```
$ perf stat --per-node -e 'UNC_CHA_CLOCKTICKS,UNC_M_CLOCKTICKS' -a sleep 1

 Performance counter stats for 'system wide':

N0        1     92,748,667,796      UNC_CHA_CLOCKTICKS
N0        0      6,424,021,142      UNC_M_CLOCKTICKS
N1        0     92,753,504,424      UNC_CHA_CLOCKTICKS
N1        1      6,424,308,338      UNC_M_CLOCKTICKS
N2        0     92,751,170,084      UNC_CHA_CLOCKTICKS
N2        0      6,424,227,402      UNC_M_CLOCKTICKS
N3        1     92,745,944,144      UNC_CHA_CLOCKTICKS
N3        0      6,423,752,086      UNC_M_CLOCKTICKS
N4        0     92,725,793,788      UNC_CHA_CLOCKTICKS
N4        1      6,422,393,266      UNC_M_CLOCKTICKS
N5        0     92,717,504,388      UNC_CHA_CLOCKTICKS
N5        0      6,421,842,618      UNC_M_CLOCKTICKS

       1.003406645 seconds time elapsed
```

In general, having the perf tool adjust cpumasks isn't desirable as ideally the PMU driver would be advertising the correct cpumask.

---
 tools/perf/arch/x86/util/pmu.c | 259 ++++++++++++++++++++++++++++++++-
 1 file changed, 254 insertions(+), 5 deletions(-)
tools/perf/arch/x86/util/pmu.c +165 -0
index 8712cbbbc712..38c800c6e9c8 100644
@@ -8,6 +8,8 @@
 #include <linux/perf_event.h>
 #include <linux/zalloc.h>
 #include <api/fs/fs.h>
+#include <api/io_dir.h>
+#include <internal/cpumap.h>
 #include <errno.h>
 
 #include "../../../util/intel-pt.h"
@@ -16,7 +18,247 @@
 #include "../../../util/fncache.h"
 #include "../../../util/pmus.h"
 #include "mem-events.h"
+#include "util/debug.h"
 #include "util/env.h"
+#include "util/header.h"
+
+static bool x86__is_intel_graniterapids(void)
+{
+       static bool checked_if_graniterapids;
+       static bool is_graniterapids;
+
+       if (!checked_if_graniterapids) {
+               const char *graniterapids_cpuid = "GenuineIntel-6-A[DE]";
+               char *cpuid = get_cpuid_str((struct perf_cpu){0});
+
+               is_graniterapids = cpuid && strcmp_cpuid_str(graniterapids_cpuid, cpuid) == 0;
+               free(cpuid);
+               checked_if_graniterapids = true;
+       }
+       return is_graniterapids;
+}
+
+static struct perf_cpu_map *read_sysfs_cpu_map(const char *sysfs_path)
+{
+       struct perf_cpu_map *cpus;
+       char *buf = NULL;
+       size_t buf_len;
+
+       if (sysfs__read_str(sysfs_path, &buf, &buf_len) < 0)
+               return NULL;
+
+       cpus = perf_cpu_map__new(buf);
+       free(buf);
+       return cpus;
+}
+
+static int snc_nodes_per_l3_cache(void)
+{
+       static bool checked_snc;
+       static int snc_nodes;
+
+       if (!checked_snc) {
+               struct perf_cpu_map *node_cpus =
+                       read_sysfs_cpu_map("devices/system/node/node0/cpulist");
+               struct perf_cpu_map *cache_cpus =
+                       read_sysfs_cpu_map("devices/system/cpu/cpu0/cache/index3/shared_cpu_list");
+
+               snc_nodes = perf_cpu_map__nr(cache_cpus) / perf_cpu_map__nr(node_cpus);
+               perf_cpu_map__put(cache_cpus);
+               perf_cpu_map__put(node_cpus);
+               checked_snc = true;
+       }
+       return snc_nodes;
+}
+
+static bool starts_with(const char *str, const char *prefix)
+{
+       return !strncmp(prefix, str, strlen(prefix));
+}
+
+static int num_chas(void)
+{
+       static bool checked_chas;
+       static int num_chas;
+
+       if (!checked_chas) {
+               int fd = perf_pmu__event_source_devices_fd();
+               struct io_dir dir;
+               struct io_dirent64 *dent;
+
+               if (fd < 0)
+                       return -1;
+
+               io_dir__init(&dir, fd);
+
+               while ((dent = io_dir__readdir(&dir)) != NULL) {
+                       /* Note, dent->d_type will be DT_LNK and so isn't a useful filter. */
+                       if (starts_with(dent->d_name, "uncore_cha_"))
+                               num_chas++;
+               }
+               close(fd);
+               checked_chas = true;
+       }
+       return num_chas;
+}
+
+#define MAX_SNCS 6
+
+static int uncore_cha_snc(struct perf_pmu *pmu)
+{
+       // CHA SNC numbers are ordered correspond to the CHAs number.
+       unsigned int cha_num;
+       int num_cha, chas_per_node, cha_snc;
+       int snc_nodes = snc_nodes_per_l3_cache();
+
+       if (snc_nodes <= 1)
+               return 0;
+
+       num_cha = num_chas();
+       if (num_cha <= 0) {
+               pr_warning("Unexpected: no CHAs found\n");
+               return 0;
+       }
+
+       /* Compute SNC for PMU. */
+       if (sscanf(pmu->name, "uncore_cha_%u", &cha_num) != 1) {
+               pr_warning("Unexpected: unable to compute CHA number '%s'\n", pmu->name);
+               return 0;
+       }
+       chas_per_node = num_cha / snc_nodes;
+       cha_snc = cha_num / chas_per_node;
+
+       /* Range check cha_snc. for unexpected out of bounds. */
+       return cha_snc >= MAX_SNCS ? 0 : cha_snc;
+}
+
+static int uncore_imc_snc(struct perf_pmu *pmu)
+{
+       // Compute the IMC SNC using lookup tables.
+       unsigned int imc_num;
+       int snc_nodes = snc_nodes_per_l3_cache();
+       const u8 snc2_map[] = {1, 1, 0, 0, 1, 1, 0, 0};
+       const u8 snc3_map[] = {1, 1, 0, 0, 2, 2, 1, 1, 0, 0, 2, 2};
+       const u8 *snc_map;
+       size_t snc_map_len;
+
+       switch (snc_nodes) {
+       case 2:
+               snc_map = snc2_map;
+               snc_map_len = ARRAY_SIZE(snc2_map);
+               break;
+       case 3:
+               snc_map = snc3_map;
+               snc_map_len = ARRAY_SIZE(snc3_map);
+               break;
+       default:
+               /* Error or no lookup support for SNC with >3 nodes. */
+               return 0;
+       }
+
+       /* Compute SNC for PMU. */
+       if (sscanf(pmu->name, "uncore_imc_%u", &imc_num) != 1) {
+               pr_warning("Unexpected: unable to compute IMC number '%s'\n", pmu->name);
+               return 0;
+       }
+       if (imc_num >= snc_map_len) {
+               pr_warning("Unexpected IMC %d for SNC%d mapping\n", imc_num, snc_nodes);
+               return 0;
+       }
+       return snc_map[imc_num];
+}
+
+static int uncore_cha_imc_compute_cpu_adjust(int pmu_snc)
+{
+       static bool checked_cpu_adjust[MAX_SNCS];
+       static int cpu_adjust[MAX_SNCS];
+       struct perf_cpu_map *node_cpus;
+       char node_path[] = "devices/system/node/node0/cpulist";
+
+       /* Was adjust already computed? */
+       if (checked_cpu_adjust[pmu_snc])
+               return cpu_adjust[pmu_snc];
+
+       /* SNC0 doesn't need an adjust. */
+       if (pmu_snc == 0) {
+               cpu_adjust[0] = 0;

tools/perf/arch/x86/util/pmu.c line 194

Quoted 10 lines
tools/perf/arch/x86/util/pmu.c +10 -0
+               checked_cpu_adjust[0] = true;
+               return 0;
+       }
+
+       /*
+        * Use NUMA topology to compute first CPU of the NUMA node, we want to
+        * adjust CPU 0 to be this and similarly for other CPUs if there is >1
+        * socket.
+        */
+       node_path[24] = pmu_snc; // Shift node0 to be node<pmu_snc>.

Sorry I migrated a patch badly and this should be "+=", will fix in v2 along with the reference count checker.

Thanks,
Ian

Quoted 96 lines
tools/perf/arch/x86/util/pmu.c +79 -6
+       node_cpus = read_sysfs_cpu_map(node_path);
+       cpu_adjust[pmu_snc] = perf_cpu_map__cpu(node_cpus, 0).cpu;
+       checked_cpu_adjust[pmu_snc] = true;
+       perf_cpu_map__put(node_cpus);
+       return cpu_adjust[pmu_snc];
+}
+
+static void gnr_uncore_cha_imc_adjust_cpumask_for_snc(struct perf_pmu *pmu, bool cha)
+{
+       // With sub-NUMA clustering (SNC) there is a NUMA node per SNC in the
+       // topology. For example, a two socket graniterapids machine may be set
+       // up with 3-way SNC meaning there are 6 NUMA nodes that should be
+       // displayed with --per-node. The cpumask of the CHA and IMC PMUs
+       // reflects per-socket information meaning, for example, uncore_cha_60
+       // on a two socket graniterapids machine with 120 cores per socket will
+       // have a cpumask of "0,120". This cpumask needs adjusting to "40,160"
+       // to reflect that uncore_cha_60 is used for the 2nd SNC of each
+       // socket. Without the adjustment events on uncore_cha_60 will appear in
+       // node 0 and node 3 (in our example 2 socket 3-way set up), but with
+       // the adjustment they will appear in node 1 and node 4. The number of
+       // CHAs is typically larger than the number of cores. The CHA numbers
+       // are assumed to split evenly and inorder wrt core numbers. There are
+       // fewer memory IMC PMUs than cores and mapping is handled using lookup
+       // tables.
+       static struct perf_cpu_map *cha_adjusted[MAX_SNCS];
+       static struct perf_cpu_map *imc_adjusted[MAX_SNCS];
+       struct perf_cpu_map **adjusted = cha ? cha_adjusted : imc_adjusted;
+       int idx, pmu_snc, cpu_adjust;
+       struct perf_cpu cpu;
+       bool alloc;
+
+       // Cpus from the kernel holds first CPU of each socket. e.g. 0,120
+       assert(perf_cpu_map__cpu(pmu->cpus, 0).cpu == 0);
+
+       pmu_snc = cha ? uncore_cha_snc(pmu) : uncore_imc_snc(pmu);
+       if (pmu_snc == 0) {
+               // No adjustment necessary for the first SNC.
+               return;
+       }
+
+       alloc = adjusted[pmu_snc] == NULL;
+       if (alloc) {
+               // Hold onto the perf_cpu_map globally to avoid recomputation.
+               cpu_adjust = uncore_cha_imc_compute_cpu_adjust(pmu_snc);
+               adjusted[pmu_snc] = perf_cpu_map__empty_new(perf_cpu_map__nr(pmu->cpus));
+               if (!adjusted[pmu_snc])
+                       return;
+       }
+
+       perf_cpu_map__for_each_cpu(cpu, idx, pmu->cpus) {
+               // Compute the new cpu map values or if not allocating, assert
+               // that they match expectations. asserts will be removed to
+               // avoid overhead in NDEBUG builds.
+               if (alloc) {
+                       adjusted[pmu_snc]->map[idx].cpu = cpu.cpu + cpu_adjust;
+               } else if (idx == 0) {
+                       cpu_adjust = perf_cpu_map__cpu(adjusted[pmu_snc], idx).cpu - cpu.cpu;
+                       assert(uncore_cha_imc_compute_cpu_adjust(pmu_snc) == cpu_adjust);
+               } else {
+                       assert(perf_cpu_map__cpu(adjusted[pmu_snc], idx).cpu ==
+                              cpu.cpu + cpu_adjust);
+               }
+       }
+
+       perf_cpu_map__put(pmu->cpus);
+       pmu->cpus = perf_cpu_map__get(adjusted[pmu_snc]);
+}
 
 void perf_pmu__arch_init(struct perf_pmu *pmu)
 {
@@ -49,10 +291,17 @@ void perf_pmu__arch_init(struct perf_pmu *pmu)
 
                perf_mem_events__loads_ldlat = 0;
                pmu->mem_events = perf_mem_events_amd_ldlat;
-       } else if (pmu->is_core) {
-               if (perf_pmu__have_event(pmu, "mem-loads-aux"))
-                       pmu->mem_events = perf_mem_events_intel_aux;
-               else
-                       pmu->mem_events = perf_mem_events_intel;
+       } else {
+               if (pmu->is_core) {
+                       if (perf_pmu__have_event(pmu, "mem-loads-aux"))
+                               pmu->mem_events = perf_mem_events_intel_aux;
+                       else
+                               pmu->mem_events = perf_mem_events_intel;
+               } else if (x86__is_intel_graniterapids()) {
+                       if (starts_with(pmu->name, "uncore_cha_"))
+                               gnr_uncore_cha_imc_adjust_cpumask_for_snc(pmu, /*cha=*/true);
+                       else if (starts_with(pmu->name, "uncore_imc_"))
+                               gnr_uncore_cha_imc_adjust_cpumask_for_snc(pmu, /*cha=*/false);
+               }
        }
 }
--

2.49.0.1045.g170613ef41-goog

#2 lore raw
In reply to Ian Rogers
Quoted 44 lines · Ian Rogers

On graniterapids the cache home agent (CHA) and memory controller (IMC) PMUs all have their cpumask set to per-socket information. In order for per NUMA node aggregation to work correctly the PMUs cpumask needs to be set to CPUs for the relevant sub-NUMA grouping.

For example, on a 2 socket graniterapids machine with sub NUMA clustering of 3, for uncore_cha and uncore_imc PMUs the cpumask is "0,120" leading to aggregation only on NUMA nodes 0 and 3:

```
$ perf stat --per-node -e 'UNC_CHA_CLOCKTICKS,UNC_M_CLOCKTICKS' -a sleep 1

 Performance counter stats for 'system wide':

N0        1    277,835,681,344      UNC_CHA_CLOCKTICKS
N0        1     19,242,894,228      UNC_M_CLOCKTICKS
N3        1    277,803,448,124      UNC_CHA_CLOCKTICKS
N3        1     19,240,741,498      UNC_M_CLOCKTICKS

       1.002113847 seconds time elapsed
```

By updating the PMUs cpumasks to "0,120", "40,160" and "80,200" then the correctly 6 NUMA node aggregations are achieved:

```
$ perf stat --per-node -e 'UNC_CHA_CLOCKTICKS,UNC_M_CLOCKTICKS' -a sleep 1

 Performance counter stats for 'system wide':

N0        1     92,748,667,796      UNC_CHA_CLOCKTICKS
N0        0      6,424,021,142      UNC_M_CLOCKTICKS
N1        0     92,753,504,424      UNC_CHA_CLOCKTICKS
N1        1      6,424,308,338      UNC_M_CLOCKTICKS
N2        0     92,751,170,084      UNC_CHA_CLOCKTICKS
N2        0      6,424,227,402      UNC_M_CLOCKTICKS
N3        1     92,745,944,144      UNC_CHA_CLOCKTICKS
N3        0      6,423,752,086      UNC_M_CLOCKTICKS
N4        0     92,725,793,788      UNC_CHA_CLOCKTICKS
N4        1      6,422,393,266      UNC_M_CLOCKTICKS
N5        0     92,717,504,388      UNC_CHA_CLOCKTICKS
N5        0      6,421,842,618      UNC_M_CLOCKTICKS

       1.003406645 seconds time elapsed
```
Quoted 3 lines

In general, having the perf tool adjust cpumasks isn't desirable as ideally the PMU driver would be advertising the correct cpumask.

Strictly speaking, the information used here is the CHA and IMC topology information, not a cpumask.

The cpumask in the uncore driver implies the scope of the PMU. Usually, it's the first CPU of each socket/die.

The topology information discloses how the uncore units connect to the HW components, e.g., die, node...
There should be another interface for it. An example is the UPI topology information.
https://lore.kernel.org/lkml/[email protected]/

But I think utilizing the cpumask to imply the topology as this patch does should be a good idea.

But I still incline not to add it to the kernel at least for now. Because,
- The current method is based on some assumptions, e.g., the hardware id and logical id of Notes and CHAs follow the ascending order. It should work for the vast majority of cases. But it's not reliable compared to retrieving from a register.
- Perf tool is more flexible. It can be easily deployed on an old environment without the need to update the kernel.
- The cpumask codes are shared among uncore PMUs in the uncore driver. It requires a big refactor to support per uncore PMU specific cpumask. It may not be worth doing it only for GNR.

Let's support it in the perf tool x86 code for now. We may move it to the uncore driver later once the above items have changed.

Quoted 186 lines
---
 tools/perf/arch/x86/util/pmu.c | 259 ++++++++++++++++++++++++++++++++-
 1 file changed, 254 insertions(+), 5 deletions(-)
tools/perf/arch/x86/util/pmu.c +165 -0
index 8712cbbbc712..38c800c6e9c8 100644
@@ -8,6 +8,8 @@
 #include <linux/perf_event.h>
 #include <linux/zalloc.h>
 #include <api/fs/fs.h>
+#include <api/io_dir.h>
+#include <internal/cpumap.h>
 #include <errno.h>
 
 #include "../../../util/intel-pt.h"
@@ -16,7 +18,247 @@
 #include "../../../util/fncache.h"
 #include "../../../util/pmus.h"
 #include "mem-events.h"
+#include "util/debug.h"
 #include "util/env.h"
+#include "util/header.h"
+
+static bool x86__is_intel_graniterapids(void)
+{
+	static bool checked_if_graniterapids;
+	static bool is_graniterapids;
+
+	if (!checked_if_graniterapids) {
+		const char *graniterapids_cpuid = "GenuineIntel-6-A[DE]";
+		char *cpuid = get_cpuid_str((struct perf_cpu){0});
+
+		is_graniterapids = cpuid && strcmp_cpuid_str(graniterapids_cpuid, cpuid) == 0;
+		free(cpuid);
+		checked_if_graniterapids = true;
+	}
+	return is_graniterapids;
+}
+
+static struct perf_cpu_map *read_sysfs_cpu_map(const char *sysfs_path)
+{
+	struct perf_cpu_map *cpus;
+	char *buf = NULL;
+	size_t buf_len;
+
+	if (sysfs__read_str(sysfs_path, &buf, &buf_len) < 0)
+		return NULL;
+
+	cpus = perf_cpu_map__new(buf);
+	free(buf);
+	return cpus;
+}
+
+static int snc_nodes_per_l3_cache(void)
+{
+	static bool checked_snc;
+	static int snc_nodes;
+
+	if (!checked_snc) {
+		struct perf_cpu_map *node_cpus =
+			read_sysfs_cpu_map("devices/system/node/node0/cpulist");
+		struct perf_cpu_map *cache_cpus =
+			read_sysfs_cpu_map("devices/system/cpu/cpu0/cache/index3/shared_cpu_list");
+
+		snc_nodes = perf_cpu_map__nr(cache_cpus) / perf_cpu_map__nr(node_cpus);
+		perf_cpu_map__put(cache_cpus);
+		perf_cpu_map__put(node_cpus);
+		checked_snc = true;
+	}
+	return snc_nodes;
+}
+
+static bool starts_with(const char *str, const char *prefix)
+{
+	return !strncmp(prefix, str, strlen(prefix));
+}
+
+static int num_chas(void)
+{
+	static bool checked_chas;
+	static int num_chas;
+
+	if (!checked_chas) {
+		int fd = perf_pmu__event_source_devices_fd();
+		struct io_dir dir;
+		struct io_dirent64 *dent;
+
+		if (fd < 0)
+			return -1;
+
+		io_dir__init(&dir, fd);
+
+		while ((dent = io_dir__readdir(&dir)) != NULL) {
+			/* Note, dent->d_type will be DT_LNK and so isn't a useful filter. */
+			if (starts_with(dent->d_name, "uncore_cha_"))
+				num_chas++;
+		}
+		close(fd);
+		checked_chas = true;
+	}
+	return num_chas;
+}
+
+#define MAX_SNCS 6
+
+static int uncore_cha_snc(struct perf_pmu *pmu)
+{
+	// CHA SNC numbers are ordered correspond to the CHAs number.
+	unsigned int cha_num;
+	int num_cha, chas_per_node, cha_snc;
+	int snc_nodes = snc_nodes_per_l3_cache();
+
+	if (snc_nodes <= 1)
+		return 0;
+
+	num_cha = num_chas();
+	if (num_cha <= 0) {
+		pr_warning("Unexpected: no CHAs found\n");
+		return 0;
+	}
+
+	/* Compute SNC for PMU. */
+	if (sscanf(pmu->name, "uncore_cha_%u", &cha_num) != 1) {
+		pr_warning("Unexpected: unable to compute CHA number '%s'\n", pmu->name);
+		return 0;
+	}
+	chas_per_node = num_cha / snc_nodes;
+	cha_snc = cha_num / chas_per_node;
+
+	/* Range check cha_snc. for unexpected out of bounds. */
+	return cha_snc >= MAX_SNCS ? 0 : cha_snc;
+}
+
+static int uncore_imc_snc(struct perf_pmu *pmu)
+{
+	// Compute the IMC SNC using lookup tables.
+	unsigned int imc_num;
+	int snc_nodes = snc_nodes_per_l3_cache();
+	const u8 snc2_map[] = {1, 1, 0, 0, 1, 1, 0, 0};
+	const u8 snc3_map[] = {1, 1, 0, 0, 2, 2, 1, 1, 0, 0, 2, 2};
+	const u8 *snc_map;
+	size_t snc_map_len;
+
+	switch (snc_nodes) {
+	case 2:
+		snc_map = snc2_map;
+		snc_map_len = ARRAY_SIZE(snc2_map);
+		break;
+	case 3:
+		snc_map = snc3_map;
+		snc_map_len = ARRAY_SIZE(snc3_map);
+		break;
+	default:
+		/* Error or no lookup support for SNC with >3 nodes. */
+		return 0;
+	}
+
+	/* Compute SNC for PMU. */
+	if (sscanf(pmu->name, "uncore_imc_%u", &imc_num) != 1) {
+		pr_warning("Unexpected: unable to compute IMC number '%s'\n", pmu->name);
+		return 0;
+	}
+	if (imc_num >= snc_map_len) {
+		pr_warning("Unexpected IMC %d for SNC%d mapping\n", imc_num, snc_nodes);
+		return 0;
+	}
+	return snc_map[imc_num];
+}
+
+static int uncore_cha_imc_compute_cpu_adjust(int pmu_snc)
+{
+	static bool checked_cpu_adjust[MAX_SNCS];
+	static int cpu_adjust[MAX_SNCS];
+	struct perf_cpu_map *node_cpus;
+	char node_path[] = "devices/system/node/node0/cpulist";
+
+	/* Was adjust already computed? */
+	if (checked_cpu_adjust[pmu_snc])
+		return cpu_adjust[pmu_snc];
+
+	/* SNC0 doesn't need an adjust. */
+	if (pmu_snc == 0) {
+		cpu_adjust[0] = 0;

tools/perf/arch/x86/util/pmu.c line 194

Quoted 10 lines
tools/perf/arch/x86/util/pmu.c +10 -0
+		checked_cpu_adjust[0] = true;
+		return 0;
+	}
+
+	/*
+	 * Use NUMA topology to compute first CPU of the NUMA node, we want to
+	 * adjust CPU 0 to be this and similarly for other CPUs if there is >1
+	 * socket.
+	 */
+	node_path[24] = pmu_snc; // Shift node0 to be node<pmu_snc>.

I think scnprintf should be a prefer way to update the idx. E.g.,

#define NODE_PATH "devices/system/node/node%d/cpulist"

scnprintf(node_path, MAX_PATH, NODE_PATH, pmu_snc);

Quoted 23 lines
tools/perf/arch/x86/util/pmu.c +23 -0
+	node_cpus = read_sysfs_cpu_map(node_path);
+	cpu_adjust[pmu_snc] = perf_cpu_map__cpu(node_cpus, 0).cpu;
+	checked_cpu_adjust[pmu_snc] = true;
+	perf_cpu_map__put(node_cpus);
+	return cpu_adjust[pmu_snc];
+}
+
+static void gnr_uncore_cha_imc_adjust_cpumask_for_snc(struct perf_pmu *pmu, bool cha)
+{
+	// With sub-NUMA clustering (SNC) there is a NUMA node per SNC in the
+	// topology. For example, a two socket graniterapids machine may be set
+	// up with 3-way SNC meaning there are 6 NUMA nodes that should be
+	// displayed with --per-node. The cpumask of the CHA and IMC PMUs
+	// reflects per-socket information meaning, for example, uncore_cha_60
+	// on a two socket graniterapids machine with 120 cores per socket will
+	// have a cpumask of "0,120". This cpumask needs adjusting to "40,160"
+	// to reflect that uncore_cha_60 is used for the 2nd SNC of each
+	// socket. Without the adjustment events on uncore_cha_60 will appear in
+	// node 0 and node 3 (in our example 2 socket 3-way set up), but with
+	// the adjustment they will appear in node 1 and node 4. The number of
+	// CHAs is typically larger than the number of cores. The CHA numbers
+	// are assumed to split evenly and inorder wrt core numbers. There are
+	// fewer memory IMC PMUs than cores and mapping is handled using lookup

tools/perf/arch/x86/util/pmu.c line 227

Quoted 10 lines
tools/perf/arch/x86/util/pmu.c +10 -0
+	// tables.
+	static struct perf_cpu_map *cha_adjusted[MAX_SNCS];
+	static struct perf_cpu_map *imc_adjusted[MAX_SNCS];
+	struct perf_cpu_map **adjusted = cha ? cha_adjusted : imc_adjusted;
+	int idx, pmu_snc, cpu_adjust;
+	struct perf_cpu cpu;
+	bool alloc;
+
+	// Cpus from the kernel holds first CPU of each socket. e.g. 0,120
+	assert(perf_cpu_map__cpu(pmu->cpus, 0).cpu == 0);

Can it just simply return if a non-0 is detected?
In case, the uncore driver is updated later. This can be treated as a fallback solution.

Thanks,
Kan> +

Quoted 59 lines
tools/perf/arch/x86/util/pmu.c +45 -5
+	pmu_snc = cha ? uncore_cha_snc(pmu) : uncore_imc_snc(pmu);
+	if (pmu_snc == 0) {
+		// No adjustment necessary for the first SNC.
+		return;
+	}
+
+	alloc = adjusted[pmu_snc] == NULL;
+	if (alloc) {
+		// Hold onto the perf_cpu_map globally to avoid recomputation.
+		cpu_adjust = uncore_cha_imc_compute_cpu_adjust(pmu_snc);
+		adjusted[pmu_snc] = perf_cpu_map__empty_new(perf_cpu_map__nr(pmu->cpus));
+		if (!adjusted[pmu_snc])
+			return;
+	}
+
+	perf_cpu_map__for_each_cpu(cpu, idx, pmu->cpus) {
+		// Compute the new cpu map values or if not allocating, assert
+		// that they match expectations. asserts will be removed to
+		// avoid overhead in NDEBUG builds.
+		if (alloc) {
+			adjusted[pmu_snc]->map[idx].cpu = cpu.cpu + cpu_adjust;
+		} else if (idx == 0) {
+			cpu_adjust = perf_cpu_map__cpu(adjusted[pmu_snc], idx).cpu - cpu.cpu;
+			assert(uncore_cha_imc_compute_cpu_adjust(pmu_snc) == cpu_adjust);
+		} else {
+			assert(perf_cpu_map__cpu(adjusted[pmu_snc], idx).cpu ==
+			       cpu.cpu + cpu_adjust);
+		}
+	}
+
+	perf_cpu_map__put(pmu->cpus);
+	pmu->cpus = perf_cpu_map__get(adjusted[pmu_snc]);
+}
 
 void perf_pmu__arch_init(struct perf_pmu *pmu)
 {
@@ -49,10 +291,17 @@ void perf_pmu__arch_init(struct perf_pmu *pmu)
 
 		perf_mem_events__loads_ldlat = 0;
 		pmu->mem_events = perf_mem_events_amd_ldlat;
-	} else if (pmu->is_core) {
-		if (perf_pmu__have_event(pmu, "mem-loads-aux"))
-			pmu->mem_events = perf_mem_events_intel_aux;
-		else
-			pmu->mem_events = perf_mem_events_intel;
+	} else {
+		if (pmu->is_core) {
+			if (perf_pmu__have_event(pmu, "mem-loads-aux"))
+				pmu->mem_events = perf_mem_events_intel_aux;
+			else
+				pmu->mem_events = perf_mem_events_intel;
+		} else if (x86__is_intel_graniterapids()) {
+			if (starts_with(pmu->name, "uncore_cha_"))
+				gnr_uncore_cha_imc_adjust_cpumask_for_snc(pmu, /*cha=*/true);
+			else if (starts_with(pmu->name, "uncore_imc_"))
+				gnr_uncore_cha_imc_adjust_cpumask_for_snc(pmu, /*cha=*/false);
+		}
 	}
 }
author reviewer
#3 lore raw
Quoted 52 lines · Kan
Quoted 44 lines · Ian Rogers

On graniterapids the cache home agent (CHA) and memory controller (IMC) PMUs all have their cpumask set to per-socket information. In order for per NUMA node aggregation to work correctly the PMUs cpumask needs to be set to CPUs for the relevant sub-NUMA grouping.

For example, on a 2 socket graniterapids machine with sub NUMA clustering of 3, for uncore_cha and uncore_imc PMUs the cpumask is "0,120" leading to aggregation only on NUMA nodes 0 and 3:

```
$ perf stat --per-node -e 'UNC_CHA_CLOCKTICKS,UNC_M_CLOCKTICKS' -a sleep 1

 Performance counter stats for 'system wide':

N0        1    277,835,681,344      UNC_CHA_CLOCKTICKS
N0        1     19,242,894,228      UNC_M_CLOCKTICKS
N3        1    277,803,448,124      UNC_CHA_CLOCKTICKS
N3        1     19,240,741,498      UNC_M_CLOCKTICKS

       1.002113847 seconds time elapsed
```

By updating the PMUs cpumasks to "0,120", "40,160" and "80,200" then the correctly 6 NUMA node aggregations are achieved:

```
$ perf stat --per-node -e 'UNC_CHA_CLOCKTICKS,UNC_M_CLOCKTICKS' -a sleep 1

 Performance counter stats for 'system wide':

N0        1     92,748,667,796      UNC_CHA_CLOCKTICKS
N0        0      6,424,021,142      UNC_M_CLOCKTICKS
N1        0     92,753,504,424      UNC_CHA_CLOCKTICKS
N1        1      6,424,308,338      UNC_M_CLOCKTICKS
N2        0     92,751,170,084      UNC_CHA_CLOCKTICKS
N2        0      6,424,227,402      UNC_M_CLOCKTICKS
N3        1     92,745,944,144      UNC_CHA_CLOCKTICKS
N3        0      6,423,752,086      UNC_M_CLOCKTICKS
N4        0     92,725,793,788      UNC_CHA_CLOCKTICKS
N4        1      6,422,393,266      UNC_M_CLOCKTICKS
N5        0     92,717,504,388      UNC_CHA_CLOCKTICKS
N5        0      6,421,842,618      UNC_M_CLOCKTICKS

       1.003406645 seconds time elapsed
```
Quoted 3 lines

In general, having the perf tool adjust cpumasks isn't desirable as ideally the PMU driver would be advertising the correct cpumask.

Quoted 5 lines

Strictly speaking, the information used here is the CHA and IMC topology information, not a cpumask.

The cpumask in the uncore driver implies the scope of the PMU. Usually, it's the first CPU of each socket/die.

I think it is understandable that things have diverged as previously socket and NUMA topologies were equivalent. Should cpumasks be documented like we have for the PMU names:
https://web.git.kernel.org/pub/scm/linux/kernel/git/perf/perf-tools-next.git/tree/Documentation/ABI/testing/sysfs-bus-event_source-devices?h=perf-tools-next
I think without documentation the behavior of the perf tool is the expected use of the file. If the perf tool isn't behaving correctly then it is a bug in the driver. I see a lot of such bugs, mainly not on Intel platforms, where the cpumask is missing and then taken by the perf tool to mean the cpumask is all online CPUs; which is very bad for uncore devices leading to counts multiplied by the number of CPUs and multiplexing. I think there is similar confusion over the meaning of the CPUs in the cpumask. They are the CPUs perf should open an event on, for uncore you can specify other CPUs and this may be a way to spread kernel overhead, for core the meaning is different. In the perf tool implementation we use cpumasks to guide setting the thread affinity to try to lower the system call overheads. I worry different people assume different things and then these things start fighting against each other - such as setting a CPU to try to lower overhead but instead incurring IPI overheads.

Quoted 8 lines

The topology information discloses how the uncore units connect to the HW components, e.g., die, node...
There should be another interface for it. An example is the UPI topology information.
https://lore.kernel.org/lkml/[email protected]/

But I think utilizing the cpumask to imply the topology as this patch does should be a good idea.

I'm not sure how I'd hook up the topology information. On skylakex for a UPI device I see:

```
$ ls /sys/devices/uncore_upi_0/
cpumask  die0  die1  format  perf_event_mux_interval_ms  power
subsystem  type  uevent
$ cat /sys/devices/uncore_upi_0/cpumask
0,18
$ cat /sys/devices/uncore_upi_0/die0
upi_1,die_1
$ cat /sys/devices/uncore_upi_0/die1
upi_1,die_0
```

There's work to connect this up with NUMA.

Quoted 11 lines

But I still incline not to add it to the kernel at least for now. Because,
- The current method is based on some assumptions, e.g., the hardware id and logical id of Notes and CHAs follow the ascending order. It should work for the vast majority of cases. But it's not reliable compared to retrieving from a register.
- Perf tool is more flexible. It can be easily deployed on an old environment without the need to update the kernel.
- The cpumask codes are shared among uncore PMUs in the uncore driver. It requires a big refactor to support per uncore PMU specific cpumask. It may not be worth doing it only for GNR.

Quoted 2 lines

Let's support it in the perf tool x86 code for now. We may move it to the uncore driver later once the above items have changed.

Agreed. At least this patch is a proof of concept that doing this fixes --per-node while keeping --per-socket also working.

Quoted 199 lines
Quoted 186 lines
---
 tools/perf/arch/x86/util/pmu.c | 259 ++++++++++++++++++++++++++++++++-
 1 file changed, 254 insertions(+), 5 deletions(-)
tools/perf/arch/x86/util/pmu.c +165 -0
index 8712cbbbc712..38c800c6e9c8 100644
@@ -8,6 +8,8 @@
 #include <linux/perf_event.h>
 #include <linux/zalloc.h>
 #include <api/fs/fs.h>
+#include <api/io_dir.h>
+#include <internal/cpumap.h>
 #include <errno.h>
 
 #include "../../../util/intel-pt.h"
@@ -16,7 +18,247 @@
 #include "../../../util/fncache.h"
 #include "../../../util/pmus.h"
 #include "mem-events.h"
+#include "util/debug.h"
 #include "util/env.h"
+#include "util/header.h"
+
+static bool x86__is_intel_graniterapids(void)
+{
+     static bool checked_if_graniterapids;
+     static bool is_graniterapids;
+
+     if (!checked_if_graniterapids) {
+             const char *graniterapids_cpuid = "GenuineIntel-6-A[DE]";
+             char *cpuid = get_cpuid_str((struct perf_cpu){0});
+
+             is_graniterapids = cpuid && strcmp_cpuid_str(graniterapids_cpuid, cpuid) == 0;
+             free(cpuid);
+             checked_if_graniterapids = true;
+     }
+     return is_graniterapids;
+}
+
+static struct perf_cpu_map *read_sysfs_cpu_map(const char *sysfs_path)
+{
+     struct perf_cpu_map *cpus;
+     char *buf = NULL;
+     size_t buf_len;
+
+     if (sysfs__read_str(sysfs_path, &buf, &buf_len) < 0)
+             return NULL;
+
+     cpus = perf_cpu_map__new(buf);
+     free(buf);
+     return cpus;
+}
+
+static int snc_nodes_per_l3_cache(void)
+{
+     static bool checked_snc;
+     static int snc_nodes;
+
+     if (!checked_snc) {
+             struct perf_cpu_map *node_cpus =
+                     read_sysfs_cpu_map("devices/system/node/node0/cpulist");
+             struct perf_cpu_map *cache_cpus =
+                     read_sysfs_cpu_map("devices/system/cpu/cpu0/cache/index3/shared_cpu_list");
+
+             snc_nodes = perf_cpu_map__nr(cache_cpus) / perf_cpu_map__nr(node_cpus);
+             perf_cpu_map__put(cache_cpus);
+             perf_cpu_map__put(node_cpus);
+             checked_snc = true;
+     }
+     return snc_nodes;
+}
+
+static bool starts_with(const char *str, const char *prefix)
+{
+     return !strncmp(prefix, str, strlen(prefix));
+}
+
+static int num_chas(void)
+{
+     static bool checked_chas;
+     static int num_chas;
+
+     if (!checked_chas) {
+             int fd = perf_pmu__event_source_devices_fd();
+             struct io_dir dir;
+             struct io_dirent64 *dent;
+
+             if (fd < 0)
+                     return -1;
+
+             io_dir__init(&dir, fd);
+
+             while ((dent = io_dir__readdir(&dir)) != NULL) {
+                     /* Note, dent->d_type will be DT_LNK and so isn't a useful filter. */
+                     if (starts_with(dent->d_name, "uncore_cha_"))
+                             num_chas++;
+             }
+             close(fd);
+             checked_chas = true;
+     }
+     return num_chas;
+}
+
+#define MAX_SNCS 6
+
+static int uncore_cha_snc(struct perf_pmu *pmu)
+{
+     // CHA SNC numbers are ordered correspond to the CHAs number.
+     unsigned int cha_num;
+     int num_cha, chas_per_node, cha_snc;
+     int snc_nodes = snc_nodes_per_l3_cache();
+
+     if (snc_nodes <= 1)
+             return 0;
+
+     num_cha = num_chas();
+     if (num_cha <= 0) {
+             pr_warning("Unexpected: no CHAs found\n");
+             return 0;
+     }
+
+     /* Compute SNC for PMU. */
+     if (sscanf(pmu->name, "uncore_cha_%u", &cha_num) != 1) {
+             pr_warning("Unexpected: unable to compute CHA number '%s'\n", pmu->name);
+             return 0;
+     }
+     chas_per_node = num_cha / snc_nodes;
+     cha_snc = cha_num / chas_per_node;
+
+     /* Range check cha_snc. for unexpected out of bounds. */
+     return cha_snc >= MAX_SNCS ? 0 : cha_snc;
+}
+
+static int uncore_imc_snc(struct perf_pmu *pmu)
+{
+     // Compute the IMC SNC using lookup tables.
+     unsigned int imc_num;
+     int snc_nodes = snc_nodes_per_l3_cache();
+     const u8 snc2_map[] = {1, 1, 0, 0, 1, 1, 0, 0};
+     const u8 snc3_map[] = {1, 1, 0, 0, 2, 2, 1, 1, 0, 0, 2, 2};
+     const u8 *snc_map;
+     size_t snc_map_len;
+
+     switch (snc_nodes) {
+     case 2:
+             snc_map = snc2_map;
+             snc_map_len = ARRAY_SIZE(snc2_map);
+             break;
+     case 3:
+             snc_map = snc3_map;
+             snc_map_len = ARRAY_SIZE(snc3_map);
+             break;
+     default:
+             /* Error or no lookup support for SNC with >3 nodes. */
+             return 0;
+     }
+
+     /* Compute SNC for PMU. */
+     if (sscanf(pmu->name, "uncore_imc_%u", &imc_num) != 1) {
+             pr_warning("Unexpected: unable to compute IMC number '%s'\n", pmu->name);
+             return 0;
+     }
+     if (imc_num >= snc_map_len) {
+             pr_warning("Unexpected IMC %d for SNC%d mapping\n", imc_num, snc_nodes);
+             return 0;
+     }
+     return snc_map[imc_num];
+}
+
+static int uncore_cha_imc_compute_cpu_adjust(int pmu_snc)
+{
+     static bool checked_cpu_adjust[MAX_SNCS];
+     static int cpu_adjust[MAX_SNCS];
+     struct perf_cpu_map *node_cpus;
+     char node_path[] = "devices/system/node/node0/cpulist";
+
+     /* Was adjust already computed? */
+     if (checked_cpu_adjust[pmu_snc])
+             return cpu_adjust[pmu_snc];
+
+     /* SNC0 doesn't need an adjust. */
+     if (pmu_snc == 0) {
+             cpu_adjust[0] = 0;

tools/perf/arch/x86/util/pmu.c line 194

Quoted 10 lines
tools/perf/arch/x86/util/pmu.c +10 -0
+             checked_cpu_adjust[0] = true;
+             return 0;
+     }
+
+     /*
+      * Use NUMA topology to compute first CPU of the NUMA node, we want to
+      * adjust CPU 0 to be this and similarly for other CPUs if there is >1
+      * socket.
+      */
+     node_path[24] = pmu_snc; // Shift node0 to be node<pmu_snc>.

I think scnprintf should be a prefer way to update the idx. E.g.,

Quoted 3 lines

#define NODE_PATH "devices/system/node/node%d/cpulist"

scnprintf(node_path, MAX_PATH, NODE_PATH, pmu_snc);

I'm concerned throughout the code about making things slow when --per-node isn't a particular common option. Perhaps it will be for graniterapids. The values of pmu_snc are 0 to 6 so this is sound but I can cover this with an assert.

Quoted 30 lines
Quoted 30 lines
tools/perf/arch/x86/util/pmu.c +29 -0
+     node_cpus = read_sysfs_cpu_map(node_path);
+     cpu_adjust[pmu_snc] = perf_cpu_map__cpu(node_cpus, 0).cpu;
+     checked_cpu_adjust[pmu_snc] = true;
+     perf_cpu_map__put(node_cpus);
+     return cpu_adjust[pmu_snc];
+}
+
+static void gnr_uncore_cha_imc_adjust_cpumask_for_snc(struct perf_pmu *pmu, bool cha)
+{
+     // With sub-NUMA clustering (SNC) there is a NUMA node per SNC in the
+     // topology. For example, a two socket graniterapids machine may be set
+     // up with 3-way SNC meaning there are 6 NUMA nodes that should be
+     // displayed with --per-node. The cpumask of the CHA and IMC PMUs
+     // reflects per-socket information meaning, for example, uncore_cha_60
+     // on a two socket graniterapids machine with 120 cores per socket will
+     // have a cpumask of "0,120". This cpumask needs adjusting to "40,160"
+     // to reflect that uncore_cha_60 is used for the 2nd SNC of each
+     // socket. Without the adjustment events on uncore_cha_60 will appear in
+     // node 0 and node 3 (in our example 2 socket 3-way set up), but with
+     // the adjustment they will appear in node 1 and node 4. The number of
+     // CHAs is typically larger than the number of cores. The CHA numbers
+     // are assumed to split evenly and inorder wrt core numbers. There are
+     // fewer memory IMC PMUs than cores and mapping is handled using lookup
+     // tables.
+     static struct perf_cpu_map *cha_adjusted[MAX_SNCS];
+     static struct perf_cpu_map *imc_adjusted[MAX_SNCS];
+     struct perf_cpu_map **adjusted = cha ? cha_adjusted : imc_adjusted;
+     int idx, pmu_snc, cpu_adjust;
+     struct perf_cpu cpu;
Quoted 8 lines

tools/perf/arch/x86/util/pmu.c line 227

Quoted 4 lines
tools/perf/arch/x86/util/pmu.c +4 -0
+     bool alloc;
+
+     // Cpus from the kernel holds first CPU of each socket. e.g. 0,120
+     assert(perf_cpu_map__cpu(pmu->cpus, 0).cpu == 0);

Can it just simply return if a non-0 is detected?
In case, the uncore driver is updated later. This can be treated as a fallback solution.

Sure. The assert should fire in non-debug builds and was supposed to catch if the kernel driver had changed wrt expectations in the code here. Doing the return seems good as it should mean some level of compatibility between the perf tool and different kernel driver versions were the cpumask to change. I'll add it in v3.

Thanks,
Ian

Quoted 62 lines

Thanks,
Kan> +

Quoted 59 lines
tools/perf/arch/x86/util/pmu.c +45 -5
+     pmu_snc = cha ? uncore_cha_snc(pmu) : uncore_imc_snc(pmu);
+     if (pmu_snc == 0) {
+             // No adjustment necessary for the first SNC.
+             return;
+     }
+
+     alloc = adjusted[pmu_snc] == NULL;
+     if (alloc) {
+             // Hold onto the perf_cpu_map globally to avoid recomputation.
+             cpu_adjust = uncore_cha_imc_compute_cpu_adjust(pmu_snc);
+             adjusted[pmu_snc] = perf_cpu_map__empty_new(perf_cpu_map__nr(pmu->cpus));
+             if (!adjusted[pmu_snc])
+                     return;
+     }
+
+     perf_cpu_map__for_each_cpu(cpu, idx, pmu->cpus) {
+             // Compute the new cpu map values or if not allocating, assert
+             // that they match expectations. asserts will be removed to
+             // avoid overhead in NDEBUG builds.
+             if (alloc) {
+                     adjusted[pmu_snc]->map[idx].cpu = cpu.cpu + cpu_adjust;
+             } else if (idx == 0) {
+                     cpu_adjust = perf_cpu_map__cpu(adjusted[pmu_snc], idx).cpu - cpu.cpu;
+                     assert(uncore_cha_imc_compute_cpu_adjust(pmu_snc) == cpu_adjust);
+             } else {
+                     assert(perf_cpu_map__cpu(adjusted[pmu_snc], idx).cpu ==
+                            cpu.cpu + cpu_adjust);
+             }
+     }
+
+     perf_cpu_map__put(pmu->cpus);
+     pmu->cpus = perf_cpu_map__get(adjusted[pmu_snc]);
+}
 
 void perf_pmu__arch_init(struct perf_pmu *pmu)
 {
@@ -49,10 +291,17 @@ void perf_pmu__arch_init(struct perf_pmu *pmu)
 
              perf_mem_events__loads_ldlat = 0;
              pmu->mem_events = perf_mem_events_amd_ldlat;
-     } else if (pmu->is_core) {
-             if (perf_pmu__have_event(pmu, "mem-loads-aux"))
-                     pmu->mem_events = perf_mem_events_intel_aux;
-             else
-                     pmu->mem_events = perf_mem_events_intel;
+     } else {
+             if (pmu->is_core) {
+                     if (perf_pmu__have_event(pmu, "mem-loads-aux"))
+                             pmu->mem_events = perf_mem_events_intel_aux;
+                     else
+                             pmu->mem_events = perf_mem_events_intel;
+             } else if (x86__is_intel_graniterapids()) {
+                     if (starts_with(pmu->name, "uncore_cha_"))
+                             gnr_uncore_cha_imc_adjust_cpumask_for_snc(pmu, /*cha=*/true);
+                     else if (starts_with(pmu->name, "uncore_imc_"))
+                             gnr_uncore_cha_imc_adjust_cpumask_for_snc(pmu, /*cha=*/false);
+             }
      }
 }
#4 lore raw
In reply to Ian Rogers
Quoted 4 lines · Ian Rogers

On graniterapids the cache home agent (CHA) and memory controller (IMC) PMUs all have their cpumask set to per-socket information. In order for per NUMA node aggregation to work correctly the PMUs cpumask needs to be set to CPUs for the relevant sub-NUMA grouping.

I'm blindly applying it as I can't test these changes, and I think this is bad.

Hopefully we should try to agree on having, say, two Tested-by from people having access to this.

In the past processing patches for hardware that wasn't available for testing lead to tons of trouble, but perhaps c'est la vie :-\

Hopefully this doesn't become just a silly rant.

- Arnaldo

Quoted 91 lines

For example, on a 2 socket graniterapids machine with sub NUMA clustering of 3, for uncore_cha and uncore_imc PMUs the cpumask is "0,120" leading to aggregation only on NUMA nodes 0 and 3:

```
$ perf stat --per-node -e 'UNC_CHA_CLOCKTICKS,UNC_M_CLOCKTICKS' -a sleep 1

 Performance counter stats for 'system wide':

N0        1    277,835,681,344      UNC_CHA_CLOCKTICKS
N0        1     19,242,894,228      UNC_M_CLOCKTICKS
N3        1    277,803,448,124      UNC_CHA_CLOCKTICKS
N3        1     19,240,741,498      UNC_M_CLOCKTICKS

       1.002113847 seconds time elapsed
```

By updating the PMUs cpumasks to "0,120", "40,160" and "80,200" then the correctly 6 NUMA node aggregations are achieved:

```
$ perf stat --per-node -e 'UNC_CHA_CLOCKTICKS,UNC_M_CLOCKTICKS' -a sleep 1

 Performance counter stats for 'system wide':

N0        1     92,748,667,796      UNC_CHA_CLOCKTICKS
N0        0      6,424,021,142      UNC_M_CLOCKTICKS
N1        0     92,753,504,424      UNC_CHA_CLOCKTICKS
N1        1      6,424,308,338      UNC_M_CLOCKTICKS
N2        0     92,751,170,084      UNC_CHA_CLOCKTICKS
N2        0      6,424,227,402      UNC_M_CLOCKTICKS
N3        1     92,745,944,144      UNC_CHA_CLOCKTICKS
N3        0      6,423,752,086      UNC_M_CLOCKTICKS
N4        0     92,725,793,788      UNC_CHA_CLOCKTICKS
N4        1      6,422,393,266      UNC_M_CLOCKTICKS
N5        0     92,717,504,388      UNC_CHA_CLOCKTICKS
N5        0      6,421,842,618      UNC_M_CLOCKTICKS

       1.003406645 seconds time elapsed
```

In general, having the perf tool adjust cpumasks isn't desirable as ideally the PMU driver would be advertising the correct cpumask.

---
 tools/perf/arch/x86/util/pmu.c | 259 ++++++++++++++++++++++++++++++++-
 1 file changed, 254 insertions(+), 5 deletions(-)
tools/perf/arch/x86/util/pmu.c +28 -0
index 8712cbbbc712..38c800c6e9c8 100644
@@ -8,6 +8,8 @@
 #include <linux/perf_event.h>
 #include <linux/zalloc.h>
 #include <api/fs/fs.h>
+#include <api/io_dir.h>
+#include <internal/cpumap.h>
 #include <errno.h>
 
 #include "../../../util/intel-pt.h"
@@ -16,7 +18,247 @@
 #include "../../../util/fncache.h"
 #include "../../../util/pmus.h"
 #include "mem-events.h"
+#include "util/debug.h"
 #include "util/env.h"
+#include "util/header.h"
+
+static bool x86__is_intel_graniterapids(void)
+{
+	static bool checked_if_graniterapids;
+	static bool is_graniterapids;
+
+	if (!checked_if_graniterapids) {
+		const char *graniterapids_cpuid = "GenuineIntel-6-A[DE]";
+		char *cpuid = get_cpuid_str((struct perf_cpu){0});
+
+		is_graniterapids = cpuid && strcmp_cpuid_str(graniterapids_cpuid, cpuid) == 0;
+		free(cpuid);
+		checked_if_graniterapids = true;
+	}
+	return is_graniterapids;
+}
+
+static struct perf_cpu_map *read_sysfs_cpu_map(const char *sysfs_path)
+{
+	struct perf_cpu_map *cpus;
+	char *buf = NULL;
+	size_t buf_len;
+
+	if (sysfs__read_str(sysfs_path, &buf, &buf_len) < 0)

tools/perf/arch/x86/util/pmu.c line 57

Quoted 10 lines
tools/perf/arch/x86/util/pmu.c +10 -0
+		return NULL;
+
+	cpus = perf_cpu_map__new(buf);
+	free(buf);
+	return cpus;
+}
+
+static int snc_nodes_per_l3_cache(void)
+{
+	static bool checked_snc;

+ static int snc_nodes;

Quoted 232 lines
tools/perf/arch/x86/util/pmu.c +215 -5
+
+	if (!checked_snc) {
+		struct perf_cpu_map *node_cpus =
+			read_sysfs_cpu_map("devices/system/node/node0/cpulist");
+		struct perf_cpu_map *cache_cpus =
+			read_sysfs_cpu_map("devices/system/cpu/cpu0/cache/index3/shared_cpu_list");
+
+		snc_nodes = perf_cpu_map__nr(cache_cpus) / perf_cpu_map__nr(node_cpus);
+		perf_cpu_map__put(cache_cpus);
+		perf_cpu_map__put(node_cpus);
+		checked_snc = true;
+	}
+	return snc_nodes;
+}
+
+static bool starts_with(const char *str, const char *prefix)
+{
+	return !strncmp(prefix, str, strlen(prefix));
+}
+
+static int num_chas(void)
+{
+	static bool checked_chas;
+	static int num_chas;
+
+	if (!checked_chas) {
+		int fd = perf_pmu__event_source_devices_fd();
+		struct io_dir dir;
+		struct io_dirent64 *dent;
+
+		if (fd < 0)
+			return -1;
+
+		io_dir__init(&dir, fd);
+
+		while ((dent = io_dir__readdir(&dir)) != NULL) {
+			/* Note, dent->d_type will be DT_LNK and so isn't a useful filter. */
+			if (starts_with(dent->d_name, "uncore_cha_"))
+				num_chas++;
+		}
+		close(fd);
+		checked_chas = true;
+	}
+	return num_chas;
+}
+
+#define MAX_SNCS 6
+
+static int uncore_cha_snc(struct perf_pmu *pmu)
+{
+	// CHA SNC numbers are ordered correspond to the CHAs number.
+	unsigned int cha_num;
+	int num_cha, chas_per_node, cha_snc;
+	int snc_nodes = snc_nodes_per_l3_cache();
+
+	if (snc_nodes <= 1)
+		return 0;
+
+	num_cha = num_chas();
+	if (num_cha <= 0) {
+		pr_warning("Unexpected: no CHAs found\n");
+		return 0;
+	}
+
+	/* Compute SNC for PMU. */
+	if (sscanf(pmu->name, "uncore_cha_%u", &cha_num) != 1) {
+		pr_warning("Unexpected: unable to compute CHA number '%s'\n", pmu->name);
+		return 0;
+	}
+	chas_per_node = num_cha / snc_nodes;
+	cha_snc = cha_num / chas_per_node;
+
+	/* Range check cha_snc. for unexpected out of bounds. */
+	return cha_snc >= MAX_SNCS ? 0 : cha_snc;
+}
+
+static int uncore_imc_snc(struct perf_pmu *pmu)
+{
+	// Compute the IMC SNC using lookup tables.
+	unsigned int imc_num;
+	int snc_nodes = snc_nodes_per_l3_cache();
+	const u8 snc2_map[] = {1, 1, 0, 0, 1, 1, 0, 0};
+	const u8 snc3_map[] = {1, 1, 0, 0, 2, 2, 1, 1, 0, 0, 2, 2};
+	const u8 *snc_map;
+	size_t snc_map_len;
+
+	switch (snc_nodes) {
+	case 2:
+		snc_map = snc2_map;
+		snc_map_len = ARRAY_SIZE(snc2_map);
+		break;
+	case 3:
+		snc_map = snc3_map;
+		snc_map_len = ARRAY_SIZE(snc3_map);
+		break;
+	default:
+		/* Error or no lookup support for SNC with >3 nodes. */
+		return 0;
+	}
+
+	/* Compute SNC for PMU. */
+	if (sscanf(pmu->name, "uncore_imc_%u", &imc_num) != 1) {
+		pr_warning("Unexpected: unable to compute IMC number '%s'\n", pmu->name);
+		return 0;
+	}
+	if (imc_num >= snc_map_len) {
+		pr_warning("Unexpected IMC %d for SNC%d mapping\n", imc_num, snc_nodes);
+		return 0;
+	}
+	return snc_map[imc_num];
+}
+
+static int uncore_cha_imc_compute_cpu_adjust(int pmu_snc)
+{
+	static bool checked_cpu_adjust[MAX_SNCS];
+	static int cpu_adjust[MAX_SNCS];
+	struct perf_cpu_map *node_cpus;
+	char node_path[] = "devices/system/node/node0/cpulist";
+
+	/* Was adjust already computed? */
+	if (checked_cpu_adjust[pmu_snc])
+		return cpu_adjust[pmu_snc];
+
+	/* SNC0 doesn't need an adjust. */
+	if (pmu_snc == 0) {
+		cpu_adjust[0] = 0;
+		checked_cpu_adjust[0] = true;
+		return 0;
+	}
+
+	/*
+	 * Use NUMA topology to compute first CPU of the NUMA node, we want to
+	 * adjust CPU 0 to be this and similarly for other CPUs if there is >1
+	 * socket.
+	 */
+	node_path[24] = pmu_snc; // Shift node0 to be node<pmu_snc>.
+	node_cpus = read_sysfs_cpu_map(node_path);
+	cpu_adjust[pmu_snc] = perf_cpu_map__cpu(node_cpus, 0).cpu;
+	checked_cpu_adjust[pmu_snc] = true;
+	perf_cpu_map__put(node_cpus);
+	return cpu_adjust[pmu_snc];
+}
+
+static void gnr_uncore_cha_imc_adjust_cpumask_for_snc(struct perf_pmu *pmu, bool cha)
+{
+	// With sub-NUMA clustering (SNC) there is a NUMA node per SNC in the
+	// topology. For example, a two socket graniterapids machine may be set
+	// up with 3-way SNC meaning there are 6 NUMA nodes that should be
+	// displayed with --per-node. The cpumask of the CHA and IMC PMUs
+	// reflects per-socket information meaning, for example, uncore_cha_60
+	// on a two socket graniterapids machine with 120 cores per socket will
+	// have a cpumask of "0,120". This cpumask needs adjusting to "40,160"
+	// to reflect that uncore_cha_60 is used for the 2nd SNC of each
+	// socket. Without the adjustment events on uncore_cha_60 will appear in
+	// node 0 and node 3 (in our example 2 socket 3-way set up), but with
+	// the adjustment they will appear in node 1 and node 4. The number of
+	// CHAs is typically larger than the number of cores. The CHA numbers
+	// are assumed to split evenly and inorder wrt core numbers. There are
+	// fewer memory IMC PMUs than cores and mapping is handled using lookup
+	// tables.
+	static struct perf_cpu_map *cha_adjusted[MAX_SNCS];
+	static struct perf_cpu_map *imc_adjusted[MAX_SNCS];
+	struct perf_cpu_map **adjusted = cha ? cha_adjusted : imc_adjusted;
+	int idx, pmu_snc, cpu_adjust;
+	struct perf_cpu cpu;
+	bool alloc;
+
+	// Cpus from the kernel holds first CPU of each socket. e.g. 0,120
+	assert(perf_cpu_map__cpu(pmu->cpus, 0).cpu == 0);
+
+	pmu_snc = cha ? uncore_cha_snc(pmu) : uncore_imc_snc(pmu);
+	if (pmu_snc == 0) {
+		// No adjustment necessary for the first SNC.
+		return;
+	}
+
+	alloc = adjusted[pmu_snc] == NULL;
+	if (alloc) {
+		// Hold onto the perf_cpu_map globally to avoid recomputation.
+		cpu_adjust = uncore_cha_imc_compute_cpu_adjust(pmu_snc);
+		adjusted[pmu_snc] = perf_cpu_map__empty_new(perf_cpu_map__nr(pmu->cpus));
+		if (!adjusted[pmu_snc])
+			return;
+	}
+
+	perf_cpu_map__for_each_cpu(cpu, idx, pmu->cpus) {
+		// Compute the new cpu map values or if not allocating, assert
+		// that they match expectations. asserts will be removed to
+		// avoid overhead in NDEBUG builds.
+		if (alloc) {
+			adjusted[pmu_snc]->map[idx].cpu = cpu.cpu + cpu_adjust;
+		} else if (idx == 0) {
+			cpu_adjust = perf_cpu_map__cpu(adjusted[pmu_snc], idx).cpu - cpu.cpu;
+			assert(uncore_cha_imc_compute_cpu_adjust(pmu_snc) == cpu_adjust);
+		} else {
+			assert(perf_cpu_map__cpu(adjusted[pmu_snc], idx).cpu ==
+			       cpu.cpu + cpu_adjust);
+		}
+	}
+
+	perf_cpu_map__put(pmu->cpus);
+	pmu->cpus = perf_cpu_map__get(adjusted[pmu_snc]);
+}
 
 void perf_pmu__arch_init(struct perf_pmu *pmu)
 {
@@ -49,10 +291,17 @@ void perf_pmu__arch_init(struct perf_pmu *pmu)
 
 		perf_mem_events__loads_ldlat = 0;
 		pmu->mem_events = perf_mem_events_amd_ldlat;
-	} else if (pmu->is_core) {
-		if (perf_pmu__have_event(pmu, "mem-loads-aux"))
-			pmu->mem_events = perf_mem_events_intel_aux;
-		else
-			pmu->mem_events = perf_mem_events_intel;
+	} else {
+		if (pmu->is_core) {
+			if (perf_pmu__have_event(pmu, "mem-loads-aux"))
+				pmu->mem_events = perf_mem_events_intel_aux;
+			else
+				pmu->mem_events = perf_mem_events_intel;
+		} else if (x86__is_intel_graniterapids()) {
+			if (starts_with(pmu->name, "uncore_cha_"))
+				gnr_uncore_cha_imc_adjust_cpumask_for_snc(pmu, /*cha=*/true);
+			else if (starts_with(pmu->name, "uncore_imc_"))
+				gnr_uncore_cha_imc_adjust_cpumask_for_snc(pmu, /*cha=*/false);
+		}
 	}
 }
Signature · 1 line
2.49.0.1045.g170613ef41-goog
#5 lore raw
Quoted 8 lines · Arnaldo Carvalho de Melo
Quoted 4 lines · Ian Rogers

On graniterapids the cache home agent (CHA) and memory controller (IMC) PMUs all have their cpumask set to per-socket information. In order for per NUMA node aggregation to work correctly the PMUs cpumask needs to be set to CPUs for the relevant sub-NUMA grouping.

I'm blindly applying it as I can't test these changes, and I think this is bad.

In the end the only review/action I could do was to turn:

Subject: Re: [PATCH v1] perf pmu intel: Adjust cpumaks for sub-NUMA clusters

Into:

Subject: Re: [PATCH v1] perf pmu intel: Adjust cpumasks for sub-NUMA clusters

:-(

Besides the build tests, etc.

- Arnaldo

author reviewer
#6 lore raw
Quoted 15 lines · Arnaldo Carvalho de Melo
Quoted 8 lines · Arnaldo Carvalho de Melo
Quoted 4 lines · Ian Rogers

On graniterapids the cache home agent (CHA) and memory controller (IMC) PMUs all have their cpumask set to per-socket information. In order for per NUMA node aggregation to work correctly the PMUs cpumask needs to be set to CPUs for the relevant sub-NUMA grouping.

I'm blindly applying it as I can't test these changes, and I think this is bad.

In the end the only review/action I could do was to turn:

Subject: Re: [PATCH v1] perf pmu intel: Adjust cpumaks for sub-NUMA clusters

Quoted 7 lines

Into:

Subject: Re: [PATCH v1] perf pmu intel: Adjust cpumasks for sub-NUMA clusters

:-(

Besides the build tests, etc.

It isn't the easiest to test. Let me add Weilin Wang on v3 as I think she has a graniterapids and could hopefully provide a tested-by tag :-)

Thanks,
Ian

Quoted 1 line

- Arnaldo

#7 lore raw
Quoted 21 lines · Ian Rogers
Quoted 19 lines · Arnaldo Carvalho de Melo
Quoted 8 lines · Arnaldo Carvalho de Melo
Quoted 4 lines · Ian Rogers

On graniterapids the cache home agent (CHA) and memory controller (IMC) PMUs all have their cpumask set to per-socket information. In order for per NUMA node aggregation to work correctly the PMUs cpumask needs to be set to CPUs for the relevant sub-NUMA grouping.

I'm blindly applying it as I can't test these changes, and I think this is bad.

In the end the only review/action I could do was to turn:

Subject: Re: [PATCH v1] perf pmu intel: Adjust cpumaks for sub-NUMA clusters

Into:

Subject: Re: [PATCH v1] perf pmu intel: Adjust cpumasks for sub-NUMA clusters

Quoted 7 lines
Quoted 3 lines

:-(

Besides the build tests, etc.

It isn't the easiest to test. Let me add Weilin Wang on v3 as I think she has a graniterapids and could hopefully provide a tested-by tag :-)

But, one more review action, will wait for v2:

         make_refcnt_check_O: cd . && make EXTRA_CFLAGS=-DREFCNT_CHECKING=1 FEATURES_DUMP=/home/acme/git/perf-tools-next/tools/perf/BUILD_TEST_FEATURE_DUMP -j32 O=/tmp/tmp.HAAu6nXJ16 DESTDIR=/tmp/tmp.NpycD5uTsi

cd . && make EXTRA_CFLAGS=-DREFCNT_CHECKING=1 FEATURES_DUMP=/home/acme/git/perf-tools-next/tools/perf/BUILD_TEST_FEATURE_DUMP -j32 O=/tmp/tmp.HAAu6nXJ16 DESTDIR=/tmp/tmp.NpycD5uTsi
BUILD: Doing 'make -j32' parallel build
Warning: Kernel ABI header differences:
diff -u tools/include/uapi/linux/bits.h include/uapi/linux/bits.h
diff -u tools/include/linux/bits.h include/linux/bits.h
diff -u tools/include/vdso/unaligned.h include/vdso/unaligned.h
diff -u tools/arch/arm64/include/asm/cputype.h arch/arm64/include/asm/cputype.h
diff -u tools/perf/trace/beauty/include/uapi/linux/vhost.h include/uapi/linux/vhost.h
Makefile.config:968: No libllvm 13+ found, slower source file resolution, please install llvm-devel/llvm-dev
Makefile.config:1147: No openjdk development package found, please install JDK package, e.g. openjdk-8-jdk, java-1.8.0-openjdk-devel

GEN /tmp/tmp.HAAu6nXJ16/common-cmds.h
<SNIP>

  CC      /tmp/tmp.HAAu6nXJ16/arch/x86/util/pmu.o
  TEST    /tmp/tmp.HAAu6nXJ16/pmu-events/empty-pmu-events.log
  GEN     /tmp/tmp.HAAu6nXJ16/pmu-events/pmu-events.c
  CC      /tmp/tmp.HAAu6nXJ16/arch/x86/tests/hybrid.o
  CC      /tmp/tmp.HAAu6nXJ16/arch/x86/tests/intel-pt-test.o
  CC      /tmp/tmp.HAAu6nXJ16/util/block-info.o
  CC      /tmp/tmp.HAAu6nXJ16/util/block-range.o
  CC      /tmp/tmp.HAAu6nXJ16/util/build-id.o
  CC      /tmp/tmp.HAAu6nXJ16/tests/pmu.o
  MKDIR   /tmp/tmp.HAAu6nXJ16/ui/browsers/
  CC      /tmp/tmp.HAAu6nXJ16/ui/browsers/annotate-data.o
  CC      /tmp/tmp.HAAu6nXJ16/bench/futex-wake.o
  CC      /tmp/tmp.HAAu6nXJ16/arch/x86/util/kvm-stat.o
  CC      /tmp/tmp.HAAu6nXJ16/builtin-help.o
  MKDIR   /tmp/tmp.HAAu6nXJ16/ui/tui/
  MKDIR   /tmp/tmp.HAAu6nXJ16/ui/tui/
  CC      /tmp/tmp.HAAu6nXJ16/ui/tui/setup.o
  CC      /tmp/tmp.HAAu6nXJ16/ui/browsers/hists.o
  CC      /tmp/tmp.HAAu6nXJ16/ui/tui/util.o

arch/x86/util/pmu.c: In function ‘gnr_uncore_cha_imc_adjust_cpumask_for_snc’:
arch/x86/util/pmu.c:249:42: error: ‘struct perf_cpu_map’ has no member named ‘map’
249 | adjusted[pmu_snc]->map[idx].cpu = cpu.cpu + cpu_adjust;
| ^~
CC /tmp/tmp.HAAu6nXJ16/builtin-buildid-list.o
make[8]: *** [/home/acme/git/perf-tools-next/tools/build/Makefile.build:86: /tmp/tmp.HAAu6nXJ16/arch/x86/util/pmu.o] Error 1 make[8]: *** Waiting for unfinished jobs....
CC /tmp/tmp.HAAu6nXJ16/arch/x86/tests/bp-modify.o
LD /tmp/tmp.HAAu6nXJ16/scripts/python/Perf-Trace-Util/perf-util-in.o

author reviewer
#8 lore raw
Quoted 66 lines · Arnaldo Carvalho de Melo
Quoted 21 lines · Ian Rogers
Quoted 19 lines · Arnaldo Carvalho de Melo
Quoted 8 lines · Arnaldo Carvalho de Melo
Quoted 4 lines · Ian Rogers

On graniterapids the cache home agent (CHA) and memory controller (IMC) PMUs all have their cpumask set to per-socket information. In order for per NUMA node aggregation to work correctly the PMUs cpumask needs to be set to CPUs for the relevant sub-NUMA grouping.

I'm blindly applying it as I can't test these changes, and I think this is bad.

In the end the only review/action I could do was to turn:

Subject: Re: [PATCH v1] perf pmu intel: Adjust cpumaks for sub-NUMA clusters

Into:

Subject: Re: [PATCH v1] perf pmu intel: Adjust cpumasks for sub-NUMA clusters

Quoted 7 lines
Quoted 3 lines

:-(

Besides the build tests, etc.

It isn't the easiest to test. Let me add Weilin Wang on v3 as I think she has a graniterapids and could hopefully provide a tested-by tag :-)

But, one more review action, will wait for v2:

         make_refcnt_check_O: cd . && make EXTRA_CFLAGS=-DREFCNT_CHECKING=1 FEATURES_DUMP=/home/acme/git/perf-tools-next/tools/perf/BUILD_TEST_FEATURE_DUMP -j32 O=/tmp/tmp.HAAu6nXJ16 DESTDIR=/tmp/tmp.NpycD5uTsi

cd . && make EXTRA_CFLAGS=-DREFCNT_CHECKING=1 FEATURES_DUMP=/home/acme/git/perf-tools-next/tools/perf/BUILD_TEST_FEATURE_DUMP -j32 O=/tmp/tmp.HAAu6nXJ16 DESTDIR=/tmp/tmp.NpycD5uTsi
BUILD: Doing 'make -j32' parallel build
Warning: Kernel ABI header differences:
diff -u tools/include/uapi/linux/bits.h include/uapi/linux/bits.h
diff -u tools/include/linux/bits.h include/linux/bits.h
diff -u tools/include/vdso/unaligned.h include/vdso/unaligned.h
diff -u tools/arch/arm64/include/asm/cputype.h arch/arm64/include/asm/cputype.h
diff -u tools/perf/trace/beauty/include/uapi/linux/vhost.h include/uapi/linux/vhost.h
Makefile.config:968: No libllvm 13+ found, slower source file resolution, please install llvm-devel/llvm-dev
Makefile.config:1147: No openjdk development package found, please install JDK package, e.g. openjdk-8-jdk, java-1.8.0-openjdk-devel

GEN /tmp/tmp.HAAu6nXJ16/common-cmds.h
<SNIP>

  CC      /tmp/tmp.HAAu6nXJ16/arch/x86/util/pmu.o
  TEST    /tmp/tmp.HAAu6nXJ16/pmu-events/empty-pmu-events.log
  GEN     /tmp/tmp.HAAu6nXJ16/pmu-events/pmu-events.c
  CC      /tmp/tmp.HAAu6nXJ16/arch/x86/tests/hybrid.o
  CC      /tmp/tmp.HAAu6nXJ16/arch/x86/tests/intel-pt-test.o
  CC      /tmp/tmp.HAAu6nXJ16/util/block-info.o
  CC      /tmp/tmp.HAAu6nXJ16/util/block-range.o
  CC      /tmp/tmp.HAAu6nXJ16/util/build-id.o
  CC      /tmp/tmp.HAAu6nXJ16/tests/pmu.o
  MKDIR   /tmp/tmp.HAAu6nXJ16/ui/browsers/
  CC      /tmp/tmp.HAAu6nXJ16/ui/browsers/annotate-data.o
  CC      /tmp/tmp.HAAu6nXJ16/bench/futex-wake.o
  CC      /tmp/tmp.HAAu6nXJ16/arch/x86/util/kvm-stat.o
  CC      /tmp/tmp.HAAu6nXJ16/builtin-help.o
  MKDIR   /tmp/tmp.HAAu6nXJ16/ui/tui/
  MKDIR   /tmp/tmp.HAAu6nXJ16/ui/tui/
  CC      /tmp/tmp.HAAu6nXJ16/ui/tui/setup.o
  CC      /tmp/tmp.HAAu6nXJ16/ui/browsers/hists.o
  CC      /tmp/tmp.HAAu6nXJ16/ui/tui/util.o
Quoted 10 lines

arch/x86/util/pmu.c: In function ‘gnr_uncore_cha_imc_adjust_cpumask_for_snc’:
arch/x86/util/pmu.c:249:42: error: ‘struct perf_cpu_map’ has no member named ‘map’
249 | adjusted[pmu_snc]->map[idx].cpu = cpu.cpu + cpu_adjust;
| ^~
CC /tmp/tmp.HAAu6nXJ16/builtin-buildid-list.o
make[8]: *** [/home/acme/git/perf-tools-next/tools/build/Makefile.build:86: /tmp/tmp.HAAu6nXJ16/arch/x86/util/pmu.o] Error 1 make[8]: *** Waiting for unfinished jobs....
CC /tmp/tmp.HAAu6nXJ16/arch/x86/tests/bp-modify.o
LD /tmp/tmp.HAAu6nXJ16/scripts/python/Perf-Trace-Util/perf-util-in.o

Yep asan/refcnt checker was fixed in v2. I just sent v3 to hopefully address Kan's feedback:
https://lore.kernel.org/lkml/[email protected]/

Thanks,
Ian