Skip to contentSkip to conversation

perf cpumap: Reduce cpu size from int to int16_t

PATCH Superseded Ian Rogers 3 participants 5 messages Dec 7, 2024 – Dec 20, 2024 perf events

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Ian Rogers

author reviewer
#0 lore raw

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

Fewer than 32k CPUs are currently supported by perf. A cpumap stores an int per CPU, so its size is 4 times the number of CPUs in the cpumap. We can reduce the size of the int to an int16_t, saving 2 bytes per CPU in the map.

---
This change is on top of:
https://lore.kernel.org/lkml/[email protected]/

---
 tools/lib/perf/include/perf/cpumap.h |  3 ++-
 tools/perf/util/cpumap.c             | 13 ++++++++-----
 tools/perf/util/env.c                |  2 +-
 3 files changed, 11 insertions(+), 7 deletions(-)
tools/lib/perf/include/perf/cpumap.h +2 -1
index cbb65e55fc67..760a9aae9884 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 +8 -5
index 27094211edd8..85e224d8631b 100644
@@ -427,7 +427,7 @@ static void set_max_cpu_num(void)
 {
 	const char *mnt;
 	char path[PATH_MAX];
-	int ret = -1;
+	int max, ret = -1;
 
 	/* set up default */
 	max_cpu_num.cpu = 4096;
@@ -444,10 +444,12 @@ static void set_max_cpu_num(void)
 		goto out;
 	}
 
-	ret = get_max_num(path, &max_cpu_num.cpu);
+	ret = get_max_num(path, &max);
 	if (ret)
 		goto out;
 
+	max_cpu_num.cpu = max;
+
 	/* get the highest present cpu number for a sparse allocation */
 	ret = snprintf(path, PATH_MAX, "%s/devices/system/cpu/present", mnt);
 	if (ret >= PATH_MAX) {
@@ -455,8 +457,9 @@ static void set_max_cpu_num(void)
 		goto out;
 	}
 
-	ret = get_max_num(path, &max_present_cpu_num.cpu);
-
+	ret = get_max_num(path, &max);
+	if (!ret)
+		max_present_cpu_num.cpu = max;
 out:
 	if (ret)
 		pr_err("Failed to read max cpus, using default of %d\n", max_cpu_num.cpu);
@@ -606,7 +609,7 @@ size_t cpu_map__snprint(struct perf_cpu_map *map, char *buf, size_t size)
 #define COMMA first ? "" : ","
 
 	for (i = 0; i < perf_cpu_map__nr(map) + 1; i++) {
-		struct perf_cpu cpu = { .cpu = INT_MAX };
+		struct perf_cpu cpu = { .cpu = INT16_MAX };
 		bool last = i == perf_cpu_map__nr(map);
 
 		if (!last)
tools/perf/util/env.c +1 -1
index e2843ca2edd9..f1d7d22e7e98 100644
@@ -531,7 +531,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.47.0.338.g60cca15819-goog
#1 lore raw

Hi Ian,

Quoted 4 lines · Ian Rogers

Fewer than 32k CPUs are currently supported by perf. A cpumap stores an int per CPU, so its size is 4 times the number of CPUs in the cpumap.

Maybe I have a stupid question. An int value has 4 bytes, on the other hand, we needs 2 bytes to store a 32k value (even 4096 needs 2 bytes for storing the value).

How can conclude "its size is 4 times the number of CPUs"?

Quoted 55 lines

We can reduce the size of the int to an int16_t, saving 2 bytes per CPU in the map.

---
 tools/lib/perf/include/perf/cpumap.h |  3 ++-
 tools/perf/util/cpumap.c             | 13 ++++++++-----
 tools/perf/util/env.c                |  2 +-
 3 files changed, 11 insertions(+), 7 deletions(-)
tools/lib/perf/include/perf/cpumap.h +2 -1
index cbb65e55fc67..760a9aae9884 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 +4 -2
index 27094211edd8..85e224d8631b 100644
@@ -427,7 +427,7 @@ static void set_max_cpu_num(void)
 {
        const char *mnt;
        char path[PATH_MAX];
-       int ret = -1;
+       int max, ret = -1;
 
        /* set up default */
        max_cpu_num.cpu = 4096;
@@ -444,10 +444,12 @@ static void set_max_cpu_num(void)
                goto out;
        }
 
-       ret = get_max_num(path, &max_cpu_num.cpu);
+       ret = get_max_num(path, &max);
        if (ret)
                goto out;
 
+       max_cpu_num.cpu = max;
+
        /* get the highest present cpu number for a sparse allocation */
        ret = snprintf(path, PATH_MAX, "%s/devices/system/cpu/present", mnt);
        if (ret >= PATH_MAX) {

tools/perf/util/cpumap.c static void set_max_cpu_num(void) · line 462

Quoted 8 lines
tools/perf/util/cpumap.c +3 -2
@@ -455,8 +457,9 @@ static void set_max_cpu_num(void)
                goto out;
        }
 
-       ret = get_max_num(path, &max_present_cpu_num.cpu);
-
+       ret = get_max_num(path, &max);
+       if (!ret)
+               max_present_cpu_num.cpu = max;

This is an improvement for max CPU number, but it is irrevelant to changing the CPU type to int16_t. It is better to split it into a new patch.

If get an error for max present CPU number, should we rollback to 4096 for both max_cpu_num and max_present_cpu_num?

Thanks,
Leo

Quoted 28 lines
tools/perf/util/cpumap.c +1 -1
 out:
        if (ret)
                pr_err("Failed to read max cpus, using default of %d\n", max_cpu_num.cpu);
@@ -606,7 +609,7 @@ size_t cpu_map__snprint(struct perf_cpu_map *map, char *buf, size_t size)
 #define COMMA first ? "" : ","
 
        for (i = 0; i < perf_cpu_map__nr(map) + 1; i++) {
-               struct perf_cpu cpu = { .cpu = INT_MAX };
+               struct perf_cpu cpu = { .cpu = INT16_MAX };
                bool last = i == perf_cpu_map__nr(map);
 
                if (!last)
tools/perf/util/env.c +1 -2
index e2843ca2edd9..f1d7d22e7e98 100644
@@ -531,7 +531,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++;
--

2.47.0.338.g60cca15819-goog

author reviewer
#2 lore raw
Quoted 9 lines · Leo Yan

Hi Ian,

Quoted 4 lines · Ian Rogers

Fewer than 32k CPUs are currently supported by perf. A cpumap stores an int per CPU, so its size is 4 times the number of CPUs in the cpumap.

Quoted 5 lines

Maybe I have a stupid question. An int value has 4 bytes, on the other hand, we needs 2 bytes to store a 32k value (even 4096 needs 2 bytes for storing the value).

How can conclude "its size is 4 times the number of CPUs"?

I'm just trying to say "sizeof(int) == 4" and we have as many of them as we have logical CPUs.

Quoted 58 lines
Quoted 58 lines

We can reduce the size of the int to an int16_t, saving 2 bytes per CPU in the map.

---
 tools/lib/perf/include/perf/cpumap.h |  3 ++-
 tools/perf/util/cpumap.c             | 13 ++++++++-----
 tools/perf/util/env.c                |  2 +-
 3 files changed, 11 insertions(+), 7 deletions(-)
tools/lib/perf/include/perf/cpumap.h +2 -1
index cbb65e55fc67..760a9aae9884 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 +4 -2
index 27094211edd8..85e224d8631b 100644
@@ -427,7 +427,7 @@ static void set_max_cpu_num(void)
 {
        const char *mnt;
        char path[PATH_MAX];
-       int ret = -1;
+       int max, ret = -1;
 
        /* set up default */
        max_cpu_num.cpu = 4096;
@@ -444,10 +444,12 @@ static void set_max_cpu_num(void)
                goto out;
        }
 
-       ret = get_max_num(path, &max_cpu_num.cpu);
+       ret = get_max_num(path, &max);
        if (ret)
                goto out;
 
+       max_cpu_num.cpu = max;
+
        /* get the highest present cpu number for a sparse allocation */
        ret = snprintf(path, PATH_MAX, "%s/devices/system/cpu/present", mnt);
        if (ret >= PATH_MAX) {
@@ -455,8 +457,9 @@ static void set_max_cpu_num(void)
                goto out;
        }
 
Quoted 9 lines

tools/perf/util/cpumap.c static void set_max_cpu_num(void) · line 462

Quoted 5 lines
tools/perf/util/cpumap.c +3 -2
-       ret = get_max_num(path, &max_present_cpu_num.cpu);
-
+       ret = get_max_num(path, &max);
+       if (!ret)
+               max_present_cpu_num.cpu = max;

This is an improvement for max CPU number, but it is irrevelant to changing the CPU type to int16_t. It is better to split it into a new patch.

The change is done this way as passing an int16_t* to a function expecting an int* causes compiler warnings.

Quoted 2 lines

If get an error for max present CPU number, should we rollback to 4096 for both max_cpu_num and max_present_cpu_num?

We could. It is probably safest to keep the existing behavior.

Thanks,
Ian

Quoted 31 lines

Thanks,
Leo

Quoted 28 lines
tools/perf/util/cpumap.c +1 -1
 out:
        if (ret)
                pr_err("Failed to read max cpus, using default of %d\n", max_cpu_num.cpu);
@@ -606,7 +609,7 @@ size_t cpu_map__snprint(struct perf_cpu_map *map, char *buf, size_t size)
 #define COMMA first ? "" : ","
 
        for (i = 0; i < perf_cpu_map__nr(map) + 1; i++) {
-               struct perf_cpu cpu = { .cpu = INT_MAX };
+               struct perf_cpu cpu = { .cpu = INT16_MAX };
                bool last = i == perf_cpu_map__nr(map);
 
                if (!last)
tools/perf/util/env.c +1 -2
index e2843ca2edd9..f1d7d22e7e98 100644
@@ -531,7 +531,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++;
--

2.47.0.338.g60cca15819-goog

Reviewed-by on v1 1/1
#3 lore raw
In reply to Ian Rogers
Quoted 2 lines · Ian Rogers

Fewer than 32k CPUs are currently supported by perf.

Minor suggestions for the commit log. Perhaps it is clearer to say

Perf does not support more than 32k CPUs.

Quoted 4 lines

A cpumap stores
an int per CPU, so its size is 4 times the number of CPUs in the cpumap.

Perf cpumap currently uses a 4 byte int to index CPU, where a 2 byte int is sufficient to cover the CPU range needed.

Otherwise

Tim

Quoted 91 lines

We can reduce the size of the int to an int16_t, saving 2 bytes per CPU in the map.

---
This change is on top of:
https://lore.kernel.org/lkml/[email protected]/

---
 tools/lib/perf/include/perf/cpumap.h |  3 ++-
 tools/perf/util/cpumap.c             | 13 ++++++++-----
 tools/perf/util/env.c                |  2 +-
 3 files changed, 11 insertions(+), 7 deletions(-)
tools/lib/perf/include/perf/cpumap.h +2 -1
index cbb65e55fc67..760a9aae9884 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 +8 -5
index 27094211edd8..85e224d8631b 100644
@@ -427,7 +427,7 @@ static void set_max_cpu_num(void)
 {
 	const char *mnt;
 	char path[PATH_MAX];
-	int ret = -1;
+	int max, ret = -1;
 
 	/* set up default */
 	max_cpu_num.cpu = 4096;
@@ -444,10 +444,12 @@ static void set_max_cpu_num(void)
 		goto out;
 	}
 
-	ret = get_max_num(path, &max_cpu_num.cpu);
+	ret = get_max_num(path, &max);
 	if (ret)
 		goto out;
 
+	max_cpu_num.cpu = max;
+
 	/* get the highest present cpu number for a sparse allocation */
 	ret = snprintf(path, PATH_MAX, "%s/devices/system/cpu/present", mnt);
 	if (ret >= PATH_MAX) {
@@ -455,8 +457,9 @@ static void set_max_cpu_num(void)
 		goto out;
 	}
 
-	ret = get_max_num(path, &max_present_cpu_num.cpu);
-
+	ret = get_max_num(path, &max);
+	if (!ret)
+		max_present_cpu_num.cpu = max;
 out:
 	if (ret)
 		pr_err("Failed to read max cpus, using default of %d\n", max_cpu_num.cpu);
@@ -606,7 +609,7 @@ size_t cpu_map__snprint(struct perf_cpu_map *map, char *buf, size_t size)
 #define COMMA first ? "" : ","
 
 	for (i = 0; i < perf_cpu_map__nr(map) + 1; i++) {
-		struct perf_cpu cpu = { .cpu = INT_MAX };
+		struct perf_cpu cpu = { .cpu = INT16_MAX };
 		bool last = i == perf_cpu_map__nr(map);
 
 		if (!last)
tools/perf/util/env.c +1 -1
index e2843ca2edd9..f1d7d22e7e98 100644
@@ -531,7 +531,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++;
author reviewer
#4 lore raw
Quoted 12 lines · Tim Chen
Quoted 2 lines · Ian Rogers

Fewer than 32k CPUs are currently supported by perf.

Minor suggestions for the commit log. Perhaps it is clearer to say

Perf does not support more than 32k CPUs.

Quoted 4 lines

A cpumap stores
an int per CPU, so its size is 4 times the number of CPUs in the cpumap.

Quoted 6 lines

Perf cpumap currently uses a 4 byte int to index CPU, where a 2 byte int is sufficient to cover the CPU range needed.

Otherwise

Thanks for the review. The commit message tweak you provide is confusing. A cpumap doesn't use an int, a cpumap has an array of perf_cpus that are ints, these are the things being sized reduced here. The index into a cpumap is regularly used as a way of densely encoding accesses into things like counters into uncore PMUs, so an index is a distinct thing from a CPU and a cpumap. I'll see what I can do to the commit message given both your and Leo's feedback but what was originally written was the most technically accurate so it'll end up likely rhyming with it.

Thanks,
Ian

Quoted 93 lines

Tim

Quoted 91 lines

We can reduce the size of the int to an int16_t, saving 2 bytes per CPU in the map.

---
This change is on top of:
https://lore.kernel.org/lkml/[email protected]/

---
 tools/lib/perf/include/perf/cpumap.h |  3 ++-
 tools/perf/util/cpumap.c             | 13 ++++++++-----
 tools/perf/util/env.c                |  2 +-
 3 files changed, 11 insertions(+), 7 deletions(-)
tools/lib/perf/include/perf/cpumap.h +2 -1
index cbb65e55fc67..760a9aae9884 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 +8 -5
index 27094211edd8..85e224d8631b 100644
@@ -427,7 +427,7 @@ static void set_max_cpu_num(void)
 {
      const char *mnt;
      char path[PATH_MAX];
-     int ret = -1;
+     int max, ret = -1;
 
      /* set up default */
      max_cpu_num.cpu = 4096;
@@ -444,10 +444,12 @@ static void set_max_cpu_num(void)
              goto out;
      }
 
-     ret = get_max_num(path, &max_cpu_num.cpu);
+     ret = get_max_num(path, &max);
      if (ret)
              goto out;
 
+     max_cpu_num.cpu = max;
+
      /* get the highest present cpu number for a sparse allocation */
      ret = snprintf(path, PATH_MAX, "%s/devices/system/cpu/present", mnt);
      if (ret >= PATH_MAX) {
@@ -455,8 +457,9 @@ static void set_max_cpu_num(void)
              goto out;
      }
 
-     ret = get_max_num(path, &max_present_cpu_num.cpu);
-
+     ret = get_max_num(path, &max);
+     if (!ret)
+             max_present_cpu_num.cpu = max;
 out:
      if (ret)
              pr_err("Failed to read max cpus, using default of %d\n", max_cpu_num.cpu);
@@ -606,7 +609,7 @@ size_t cpu_map__snprint(struct perf_cpu_map *map, char *buf, size_t size)
 #define COMMA first ? "" : ","
 
      for (i = 0; i < perf_cpu_map__nr(map) + 1; i++) {
-             struct perf_cpu cpu = { .cpu = INT_MAX };
+             struct perf_cpu cpu = { .cpu = INT16_MAX };
              bool last = i == perf_cpu_map__nr(map);
 
              if (!last)
tools/perf/util/env.c +1 -1
index e2843ca2edd9..f1d7d22e7e98 100644
@@ -531,7 +531,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++;