Skip to contentSkip to conversation

perf cpumap: Reduce cpu size from int to int16_t

PATCH Applied Ian Rogers 3 participants 3 messages Feb 10, 2025 – Feb 27, 2025 perf events

lore
Share & tools
lore

Ian Rogers

author reviewer
#0 lore raw

PATCH v3 1/1 perf cpumap: Reduce cpu size from int to int16_t

Fewer than 32k logical CPUs are currently supported by perf. A cpumap is indexed by an integer (see perf_cpu_map__cpu) yielding a perf_cpu that wraps a 4-byte int for the logical CPU - the wrapping is done deliberately to avoid confusing a logical CPU with an index into a cpumap. Using a 4-byte int within the perf_cpu is larger than required so this patch reduces it to the 2-byte int16_t. For a cpumap containing 16 entries this will reduce the array size from 64 to 32 bytes. For very large servers with lots of logical CPUs the size savings will be greater.

---
v3. Add bounds checks as suggested by Leo Yan <[email protected]>. v2. Rebase and tweak commit message add Reviewed-by: Tim Chen

    <[email protected]>.
---
 tools/lib/perf/cpumap.c              |  8 ++--
 tools/lib/perf/include/perf/cpumap.h |  3 +-
 tools/perf/util/cpumap.c             | 68 +++++++++++++++++++---------
 tools/perf/util/env.c                |  2 +-
 4 files changed, 54 insertions(+), 27 deletions(-)
tools/lib/perf/cpumap.c +4 -4
index fcc47214062a..4454a5987570 100644
@@ -185,7 +185,7 @@ struct perf_cpu_map *perf_cpu_map__new(const char *cpu_list)
 	while (isdigit(*cpu_list)) {
 		p = NULL;
 		start_cpu = strtoul(cpu_list, &p, 0);
-		if (start_cpu >= INT_MAX
+		if (start_cpu >= INT16_MAX
 		    || (*p != '\0' && *p != ',' && *p != '-' && *p != '\n'))
 			goto invalid;
 
@@ -194,7 +194,7 @@ struct perf_cpu_map *perf_cpu_map__new(const char *cpu_list)
 			p = NULL;
 			end_cpu = strtoul(cpu_list, &p, 0);
 
-			if (end_cpu >= INT_MAX || (*p != '\0' && *p != ',' && *p != '\n'))
+			if (end_cpu >= INT16_MAX || (*p != '\0' && *p != ',' && *p != '\n'))
 				goto invalid;
 
 			if (end_cpu < start_cpu)
@@ -209,7 +209,7 @@ struct perf_cpu_map *perf_cpu_map__new(const char *cpu_list)
 		for (; start_cpu <= end_cpu; start_cpu++) {
 			/* check for duplicates */
 			for (i = 0; i < nr_cpus; i++)
-				if (tmp_cpus[i].cpu == (int)start_cpu)
+				if (tmp_cpus[i].cpu == (int16_t)start_cpu)
 					goto invalid;
 
 			if (nr_cpus == max_entries) {
@@ -219,7 +219,7 @@ struct perf_cpu_map *perf_cpu_map__new(const char *cpu_list)
 					goto invalid;
 				tmp_cpus = tmp;
 			}
-			tmp_cpus[nr_cpus++].cpu = (int)start_cpu;
+			tmp_cpus[nr_cpus++].cpu = (int16_t)start_cpu;
 		}
 		if (*p)
 			++p;
tools/lib/perf/include/perf/cpumap.h +2 -1
index 188a667babc6..8c1ab0f9194e 100644
@@ -4,10 +4,11 @@
 
 #include <perf/core.h>
 #include <stdbool.h>
+#include <stdint.h>
 
 /** A wrapper around a CPU to avoid confusion with the perf_cpu_map's map's indices. */
 struct perf_cpu {
-	int cpu;
+	int16_t cpu;
 };
 
 struct perf_cache {
tools/perf/util/cpumap.c +47 -21

114 lines not on this page · raw message

tools/perf/util/env.c +1 -1
index cae4f6d63318..36411749e007 100644
@@ -543,7 +543,7 @@ int perf_env__numa_node(struct perf_env *env, struct perf_cpu cpu)
 
 		for (i = 0; i < env->nr_numa_nodes; i++) {
 			nn = &env->numa_nodes[i];
-			nr = max(nr, perf_cpu_map__max(nn->map).cpu);
+			nr = max(nr, (int)perf_cpu_map__max(nn->map).cpu);
 		}
 
 		nr++;
Signature · 1 line
2.48.1.502.g6dc24dfdaf-goog
reviewer Reviewed-by on v3 1/1
#1 lore raw
Quoted 14 lines · Ian Rogers

Fewer than 32k logical CPUs are currently supported by perf. A cpumap is indexed by an integer (see perf_cpu_map__cpu) yielding a perf_cpu that wraps a 4-byte int for the logical CPU - the wrapping is done deliberately to avoid confusing a logical CPU with an index into a cpumap. Using a 4-byte int within the perf_cpu is larger than required so this patch reduces it to the 2-byte int16_t. For a cpumap containing 16 entries this will reduce the array size from 64 to 32 bytes. For very large servers with lots of logical CPUs the size savings will be greater.

---
v3. Add bounds checks as suggested by Leo Yan <[email protected]>. v2. Rebase and tweak commit message add Reviewed-by: Tim Chen

Quoted 8 lines
     <[email protected]>.
---
  tools/lib/perf/cpumap.c              |  8 ++--
  tools/lib/perf/include/perf/cpumap.h |  3 +-
  tools/perf/util/cpumap.c             | 68 +++++++++++++++++++---------
  tools/perf/util/env.c                |  2 +-
  4 files changed, 54 insertions(+), 27 deletions(-)
maintainer Applied
#2 lore raw
In reply to Ian Rogers
Quoted 11 lines · Ian Rogers

Fewer than 32k logical CPUs are currently supported by perf. A cpumap is indexed by an integer (see perf_cpu_map__cpu) yielding a perf_cpu that wraps a 4-byte int for the logical CPU - the wrapping is done deliberately to avoid confusing a logical CPU with an index into a cpumap. Using a 4-byte int within the perf_cpu is larger than required so this patch reduces it to the 2-byte int16_t. For a cpumap containing 16 entries this will reduce the array size from 64 to 32 bytes. For very large servers with lots of logical CPUs the size savings will be greater.

[...]

Applied to perf-tools-next, thanks!

Best regards,
Namhyung