Skip to contentSkip to conversation

tools/perf/tests: Update perf record testcase to fix usage of affinity for machines with #CPUs > 1K

PATCH Applied Athira Rajeev 6 participants 10 messages Aug 14, 2025 – Sep 15, 2025 perf events

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 tools/perf/tests: Update perf record testcase to fix usage of affinity for machines with #CPUs > 1K

The perf record testcase fails on systems with more than 1K CPUs.

Testcase: perf test -vv "PERF_RECORD_* events & perf_sample fields"

PERF_RECORD_* events & perf_sample fields :
--- start ---
test child forked, pid 272482
sched_getaffinity: Invalid argument sched__get_first_possible_cpu: Invalid argument test child finished with -1
---- end ----
PERF_RECORD_* events & perf_sample fields: FAILED!

sched__get_first_possible_cpu uses "sched_getaffinity" to get the cpumask and this call is returning EINVAL (Invalid argument). This happens because the default mask size in glibc is 1024. To overcome this 1024 CPUs mask size limitation of cpu_set_t, change the mask size using the CPU_*_S macros ie, use CPU_ALLOC to allocate cpumask, CPU_ALLOC_SIZE for size. Same fix needed for mask which is used to setaffinity so that mask size is large enough to represent number of possible CPU's in the system.

---
 tools/perf/tests/perf-record.c | 36 ++++++++++++++++++++++++----------
 1 file changed, 26 insertions(+), 10 deletions(-)
tools/perf/tests/perf-record.c +26 -10
index 0b3c37e66871..d895df037707 100644
@@ -13,15 +13,19 @@
 #include "tests.h"
 #include "util/mmap.h"
 #include "util/sample.h"
+#include "util/cpumap.h"
 
 static int sched__get_first_possible_cpu(pid_t pid, cpu_set_t *maskp)
 {
-	int i, cpu = -1, nrcpus = 1024;
+	int i, cpu = -1;
+	int nrcpus = cpu__max_cpu().cpu;
+	size_t size = CPU_ALLOC_SIZE(nrcpus);
+
 realloc:
-	CPU_ZERO(maskp);
+	CPU_ZERO_S(size, maskp);
 
-	if (sched_getaffinity(pid, sizeof(*maskp), maskp) == -1) {
-		if (errno == EINVAL && nrcpus < (1024 << 8)) {
+	if (sched_getaffinity(pid, size, maskp) == -1) {
+		if (errno == EINVAL && nrcpus < (cpu__max_cpu().cpu << 8)) {
 			nrcpus = nrcpus << 2;
 			goto realloc;
 		}
@@ -30,11 +34,11 @@ static int sched__get_first_possible_cpu(pid_t pid, cpu_set_t *maskp)
 	}
 
 	for (i = 0; i < nrcpus; i++) {
-		if (CPU_ISSET(i, maskp)) {
+		if (CPU_ISSET_S(i, size, maskp)) {
 			if (cpu == -1)
 				cpu = i;
 			else
-				CPU_CLR(i, maskp);
+				CPU_CLR_S(i, size, maskp);
 		}
 	}
 
@@ -50,8 +54,9 @@ static int test__PERF_RECORD(struct test_suite *test __maybe_unused, int subtest
 		.no_buffering = true,
 		.mmap_pages   = 256,
 	};
-	cpu_set_t cpu_mask;
-	size_t cpu_mask_size = sizeof(cpu_mask);
+	int nrcpus = cpu__max_cpu().cpu;
+	cpu_set_t *cpu_mask;
+	size_t cpu_mask_size;
 	struct evlist *evlist = evlist__new_dummy();
 	struct evsel *evsel;
 	struct perf_sample sample;
@@ -69,12 +74,22 @@ static int test__PERF_RECORD(struct test_suite *test __maybe_unused, int subtest
 	int total_events = 0, nr_events[PERF_RECORD_MAX] = { 0, };
 	char sbuf[STRERR_BUFSIZE];
 
+	cpu_mask = CPU_ALLOC(nrcpus);
+	if (!cpu_mask) {
+		pr_debug("failed to create cpumask\n");
+		goto out;
+	}
+
+	cpu_mask_size = CPU_ALLOC_SIZE(nrcpus);
+	CPU_ZERO_S(cpu_mask_size, cpu_mask);
+
 	perf_sample__init(&sample, /*all=*/false);
 	if (evlist == NULL) /* Fallback for kernels lacking PERF_COUNT_SW_DUMMY */
 		evlist = evlist__new_default();
 
 	if (evlist == NULL) {
 		pr_debug("Not enough memory to create evlist\n");
+		CPU_FREE(cpu_mask);
 		goto out;
 	}
 
@@ -111,7 +126,7 @@ static int test__PERF_RECORD(struct test_suite *test __maybe_unused, int subtest
 	evsel__set_sample_bit(evsel, TIME);
 	evlist__config(evlist, &opts, NULL);
 
-	err = sched__get_first_possible_cpu(evlist->workload.pid, &cpu_mask);
+	err = sched__get_first_possible_cpu(evlist->workload.pid, cpu_mask);
 	if (err < 0) {
 		pr_debug("sched__get_first_possible_cpu: %s\n",
 			 str_error_r(errno, sbuf, sizeof(sbuf)));
@@ -123,7 +138,7 @@ static int test__PERF_RECORD(struct test_suite *test __maybe_unused, int subtest
 	/*
 	 * So that we can check perf_sample.cpu on all the samples.
 	 */
-	if (sched_setaffinity(evlist->workload.pid, cpu_mask_size, &cpu_mask) < 0) {
+	if (sched_setaffinity(evlist->workload.pid, cpu_mask_size, cpu_mask) < 0) {
 		pr_debug("sched_setaffinity: %s\n",
 			 str_error_r(errno, sbuf, sizeof(sbuf)));
 		goto out_delete_evlist;
@@ -328,6 +343,7 @@ static int test__PERF_RECORD(struct test_suite *test __maybe_unused, int subtest
 		++errs;
 	}
 out_delete_evlist:
+	CPU_FREE(cpu_mask);
 	evlist__delete(evlist);
 out:
 	perf_sample__exit(&sample);
Signature · 1 line
2.43.7
Tested-by on v1 1/1
#1 lore raw
Quoted 26 lines · Athira Rajeev

The perf record testcase fails on systems with more than 1K CPUs.

Testcase: perf test -vv "PERF_RECORD_* events & perf_sample fields"

PERF_RECORD_* events & perf_sample fields :
--- start ---
test child forked, pid 272482
sched_getaffinity: Invalid argument sched__get_first_possible_cpu: Invalid argument test child finished with -1
---- end ----
PERF_RECORD_* events & perf_sample fields: FAILED!

sched__get_first_possible_cpu uses "sched_getaffinity" to get the cpumask and this call is returning EINVAL (Invalid argument). This happens because the default mask size in glibc is 1024. To overcome this 1024 CPUs mask size limitation of cpu_set_t, change the mask size using the CPU_*_S macros ie, use CPU_ALLOC to allocate cpumask, CPU_ALLOC_SIZE for size. Same fix needed for mask which is used to setaffinity so that mask size is large enough to represent number of possible CPU's in the system.

Quoted 5 lines

---
tools/perf/tests/perf-record.c | 36 ++++++++++++++++++++++++----------

1 file changed, 26 insertions(+), 10 deletions(-)

Tested this patch by applying on top of mainline kernel and it fixes the reported issue. Hence,

Before this patch:

7: PERF_RECORD_* events & perf_sample fields:
--- start ---
test child forked, pid 78426
Using CPUID 0x00820200
sched_getaffinity: Invalid argument sched__get_first_possible_cpu: Invalid argument ---- end(-1) ----
Leak of file descriptor 6 that opened: 'pipe:[476656]'

---- unexpected signal (6) ----
Failed to read build ID for //anon
Failed to read build ID for //anon
Failed to read build ID for //anon
Failed to read build ID for //anon
Failed to read build ID for //anon

    #0 0x101871fc in child_test_sig_handler builtin-test.c:0
Failed to open [vdso], continuing without symbols
    #1 0x7fffae7f04d8 [vdso][4d8]
    #2 0x7fffade396f8 in raise libc-2.28.so[496f8]
    #3 0x7fffade13ff4 in abort libc-2.28.so[23ff4]
    #4 0x101879c4 in check_leaks builtin-test.c:0
    #5 0x10187c20 in run_test_child builtin-test.c:0
    #6 0x100fe7dc in start_command run-command.c:128
    #7 0x10188574 in __cmd_test builtin-test.c:0
    #8 0x10188c4c in cmd_test ??:0
    #9 0x100ea3ac in run_builtin perf.c:0
    #10 0x100eaa34 in handle_internal_command perf.c:0
    #11 0x10036aa8 in main ??:0
    #12 0x7fffade19f5c in generic_start_main.isra.0 libc-2.28.so[29f5c]
    #13 0x7fffade1a0f4 in __libc_start_main libc-2.28.so[2a0f4]

7: PERF_RECORD_* events & perf_sample fields : FAILED!

After this patch:

0x7fffad7bf168: mmap mask[0]: 0x7fffad7bf258: mmap mask[0]: 0x7fffad7bf348: mmap mask[0]: 0x7fffad7bf438: mmap mask[0]: 0x7fffad7bf528: mmap mask[0]: 0x7fffad7bf618: mmap mask[0]: 0x7fffad7bf708: mmap mask[0]: 0x7fffad7bf7f8: mmap mask[0]: 0x7fffad7bf8e8: mmap mask[0]: 0x7fffad7bf9d8: mmap mask[0]: 151627927388184 0 PERF_RECORD_COMM exec: sleep:130633/130633 151627927461320 0 PERF_RECORD_MMAP2 130633/130633: [0x124a50000(0x30000) @ 0 fd:00 251660138 919860811]: r-xp /usr/bin/sleep
151627927510848 0 PERF_RECORD_MMAP2 130633/130633: [0x7fff9c430000(0x60000) @ 0 fd:00 134289888 563052989]: r-xp /usr/lib64/ld-2.28.so
151627927559440 0 PERF_RECORD_MMAP2 130633/130633: [0x7fff9c410000(0x20000) @ 0 00:00 0 0]: r-xp [vdso]
151627927751224 0 PERF_RECORD_MMAP2 130633/130633: [0x7fff9c1f0000(0x210000) @ 0 fd:00 100663849 3568210255]: r-xp /usr/lib64/glibc-hwcaps/power9/libc-2.28.so
151628928449218 0 PERF_RECORD_EXIT(130633:130633):(130632:130632)
---- end(0) ----
7: PERF_RECORD_* events & perf_sample fields : Ok

Regards,
Venkat.

Quoted 108 lines
tools/perf/tests/perf-record.c +5 -1
index 0b3c37e66871..d895df037707 100644
@@ -13,15 +13,19 @@
 #include "tests.h"
 #include "util/mmap.h"
 #include "util/sample.h"
+#include "util/cpumap.h"
 
 static int sched__get_first_possible_cpu(pid_t pid, cpu_set_t *maskp)
 {
- int i, cpu = -1, nrcpus = 1024;
+ int i, cpu = -1;
+ int nrcpus = cpu__max_cpu().cpu;
+ size_t size = CPU_ALLOC_SIZE(nrcpus);
+

realloc:
- CPU_ZERO(maskp);
+ CPU_ZERO_S(size, maskp);

- if (sched_getaffinity(pid, sizeof(*maskp), maskp) == -1) {
- if (errno == EINVAL && nrcpus < (1024 << 8)) {
+ if (sched_getaffinity(pid, size, maskp) == -1) {
+ if (errno == EINVAL && nrcpus < (cpu__max_cpu().cpu << 8)) { nrcpus = nrcpus << 2;
goto realloc;
}

tools/perf/tests/perf-record.c +1 -1
@@ -30,11 +34,11 @@ static int sched__get_first_possible_cpu(pid_t pid, cpu_set_t *maskp)
 }
 
 for (i = 0; i < nrcpus; i++) {
- if (CPU_ISSET(i, maskp)) {
+ if (CPU_ISSET_S(i, size, maskp)) {
 if (cpu == -1)
 cpu = i;

else
- CPU_CLR(i, maskp);
+ CPU_CLR_S(i, size, maskp);
}
}

tools/perf/tests/perf-record.c +12 -2
@@ -50,8 +54,9 @@ static int test__PERF_RECORD(struct test_suite *test __maybe_unused, int subtest
 .no_buffering = true,
 .mmap_pages   = 256,
 };
- cpu_set_t cpu_mask;
- size_t cpu_mask_size = sizeof(cpu_mask);
+ int nrcpus = cpu__max_cpu().cpu;
+ cpu_set_t *cpu_mask;
+ size_t cpu_mask_size;
 struct evlist *evlist = evlist__new_dummy();
 struct evsel *evsel;
 struct perf_sample sample;
@@ -69,12 +74,22 @@ static int test__PERF_RECORD(struct test_suite *test __maybe_unused, int subtest
 int total_events = 0, nr_events[PERF_RECORD_MAX] = { 0, };
 char sbuf[STRERR_BUFSIZE];
 
+ cpu_mask = CPU_ALLOC(nrcpus);
+ if (!cpu_mask) {
+ pr_debug("failed to create cpumask\n");
+ goto out;
+ }
+
+ cpu_mask_size = CPU_ALLOC_SIZE(nrcpus);
+ CPU_ZERO_S(cpu_mask_size, cpu_mask);
+
 perf_sample__init(&sample, /*all=*/false);

if (evlist == NULL) /* Fallback for kernels lacking PERF_COUNT_SW_DUMMY */ evlist = evlist__new_default();

if (evlist == NULL) {
pr_debug("Not enough memory to create evlist\n");
+ CPU_FREE(cpu_mask);
goto out;
}

tools/perf/tests/perf-record.c +3 -2
@@ -111,7 +126,7 @@ static int test__PERF_RECORD(struct test_suite *test __maybe_unused, int subtest
 evsel__set_sample_bit(evsel, TIME);
 evlist__config(evlist, &opts, NULL);
 
- err = sched__get_first_possible_cpu(evlist->workload.pid, &cpu_mask);
+ err = sched__get_first_possible_cpu(evlist->workload.pid, cpu_mask);
 if (err < 0) {
 pr_debug("sched__get_first_possible_cpu: %s\n",
 str_error_r(errno, sbuf, sizeof(sbuf)));
@@ -123,7 +138,7 @@ static int test__PERF_RECORD(struct test_suite *test __maybe_unused, int subtest
 /*
 * So that we can check perf_sample.cpu on all the samples.
 */
- if (sched_setaffinity(evlist->workload.pid, cpu_mask_size, &cpu_mask) < 0) {
+ if (sched_setaffinity(evlist->workload.pid, cpu_mask_size, cpu_mask) < 0) {
 pr_debug("sched_setaffinity: %s\n",
 str_error_r(errno, sbuf, sizeof(sbuf)));
 goto out_delete_evlist;
@@ -328,6 +343,7 @@ static int test__PERF_RECORD(struct test_suite *test __maybe_unused, int subtest
++errs;
 }

out_delete_evlist:
+ CPU_FREE(cpu_mask);
evlist__delete(evlist);
out:
perf_sample__exit(&sample);

Signature · 1 line
2.43.7
maintainer Acked-by on v1 1/1
#2 lore raw
In reply to Athira Rajeev

Hello,

Quoted 22 lines · Athira Rajeev

The perf record testcase fails on systems with more than 1K CPUs.

Testcase: perf test -vv "PERF_RECORD_* events & perf_sample fields"

PERF_RECORD_* events & perf_sample fields :
--- start ---
test child forked, pid 272482
sched_getaffinity: Invalid argument sched__get_first_possible_cpu: Invalid argument test child finished with -1
---- end ----
PERF_RECORD_* events & perf_sample fields: FAILED!

sched__get_first_possible_cpu uses "sched_getaffinity" to get the cpumask and this call is returning EINVAL (Invalid argument). This happens because the default mask size in glibc is 1024. To overcome this 1024 CPUs mask size limitation of cpu_set_t, change the mask size using the CPU_*_S macros ie, use CPU_ALLOC to allocate cpumask, CPU_ALLOC_SIZE for size. Same fix needed for mask which is used to setaffinity so that mask size is large enough to represent number of possible CPU's in the system.

Quoted 2 lines

Thanks,
Namhyung

Quoted 110 lines
---
 tools/perf/tests/perf-record.c | 36 ++++++++++++++++++++++++----------
 1 file changed, 26 insertions(+), 10 deletions(-)
tools/perf/tests/perf-record.c +26 -10
index 0b3c37e66871..d895df037707 100644
@@ -13,15 +13,19 @@
 #include "tests.h"
 #include "util/mmap.h"
 #include "util/sample.h"
+#include "util/cpumap.h"
 
 static int sched__get_first_possible_cpu(pid_t pid, cpu_set_t *maskp)
 {
-	int i, cpu = -1, nrcpus = 1024;
+	int i, cpu = -1;
+	int nrcpus = cpu__max_cpu().cpu;
+	size_t size = CPU_ALLOC_SIZE(nrcpus);
+
 realloc:
-	CPU_ZERO(maskp);
+	CPU_ZERO_S(size, maskp);
 
-	if (sched_getaffinity(pid, sizeof(*maskp), maskp) == -1) {
-		if (errno == EINVAL && nrcpus < (1024 << 8)) {
+	if (sched_getaffinity(pid, size, maskp) == -1) {
+		if (errno == EINVAL && nrcpus < (cpu__max_cpu().cpu << 8)) {
 			nrcpus = nrcpus << 2;
 			goto realloc;
 		}
@@ -30,11 +34,11 @@ static int sched__get_first_possible_cpu(pid_t pid, cpu_set_t *maskp)
 	}
 
 	for (i = 0; i < nrcpus; i++) {
-		if (CPU_ISSET(i, maskp)) {
+		if (CPU_ISSET_S(i, size, maskp)) {
 			if (cpu == -1)
 				cpu = i;
 			else
-				CPU_CLR(i, maskp);
+				CPU_CLR_S(i, size, maskp);
 		}
 	}
 
@@ -50,8 +54,9 @@ static int test__PERF_RECORD(struct test_suite *test __maybe_unused, int subtest
 		.no_buffering = true,
 		.mmap_pages   = 256,
 	};
-	cpu_set_t cpu_mask;
-	size_t cpu_mask_size = sizeof(cpu_mask);
+	int nrcpus = cpu__max_cpu().cpu;
+	cpu_set_t *cpu_mask;
+	size_t cpu_mask_size;
 	struct evlist *evlist = evlist__new_dummy();
 	struct evsel *evsel;
 	struct perf_sample sample;
@@ -69,12 +74,22 @@ static int test__PERF_RECORD(struct test_suite *test __maybe_unused, int subtest
 	int total_events = 0, nr_events[PERF_RECORD_MAX] = { 0, };
 	char sbuf[STRERR_BUFSIZE];
 
+	cpu_mask = CPU_ALLOC(nrcpus);
+	if (!cpu_mask) {
+		pr_debug("failed to create cpumask\n");
+		goto out;
+	}
+
+	cpu_mask_size = CPU_ALLOC_SIZE(nrcpus);
+	CPU_ZERO_S(cpu_mask_size, cpu_mask);
+
 	perf_sample__init(&sample, /*all=*/false);
 	if (evlist == NULL) /* Fallback for kernels lacking PERF_COUNT_SW_DUMMY */
 		evlist = evlist__new_default();
 
 	if (evlist == NULL) {
 		pr_debug("Not enough memory to create evlist\n");
+		CPU_FREE(cpu_mask);
 		goto out;
 	}
 
@@ -111,7 +126,7 @@ static int test__PERF_RECORD(struct test_suite *test __maybe_unused, int subtest
 	evsel__set_sample_bit(evsel, TIME);
 	evlist__config(evlist, &opts, NULL);
 
-	err = sched__get_first_possible_cpu(evlist->workload.pid, &cpu_mask);
+	err = sched__get_first_possible_cpu(evlist->workload.pid, cpu_mask);
 	if (err < 0) {
 		pr_debug("sched__get_first_possible_cpu: %s\n",
 			 str_error_r(errno, sbuf, sizeof(sbuf)));
@@ -123,7 +138,7 @@ static int test__PERF_RECORD(struct test_suite *test __maybe_unused, int subtest
 	/*
 	 * So that we can check perf_sample.cpu on all the samples.
 	 */
-	if (sched_setaffinity(evlist->workload.pid, cpu_mask_size, &cpu_mask) < 0) {
+	if (sched_setaffinity(evlist->workload.pid, cpu_mask_size, cpu_mask) < 0) {
 		pr_debug("sched_setaffinity: %s\n",
 			 str_error_r(errno, sbuf, sizeof(sbuf)));
 		goto out_delete_evlist;
@@ -328,6 +343,7 @@ static int test__PERF_RECORD(struct test_suite *test __maybe_unused, int subtest
 		++errs;
 	}
 out_delete_evlist:
+	CPU_FREE(cpu_mask);
 	evlist__delete(evlist);
 out:
 	perf_sample__exit(&sample);
Signature · 1 line
2.43.7
#3 lore raw
Quoted 29 lines · Namhyung Kim

Hello,

Quoted 22 lines · Athira Rajeev

The perf record testcase fails on systems with more than 1K CPUs.

Testcase: perf test -vv "PERF_RECORD_* events & perf_sample fields"

PERF_RECORD_* events & perf_sample fields :
--- start ---
test child forked, pid 272482
sched_getaffinity: Invalid argument sched__get_first_possible_cpu: Invalid argument test child finished with -1
---- end ----
PERF_RECORD_* events & perf_sample fields: FAILED!

sched__get_first_possible_cpu uses "sched_getaffinity" to get the cpumask and this call is returning EINVAL (Invalid argument). This happens because the default mask size in glibc is 1024. To overcome this 1024 CPUs mask size limitation of cpu_set_t, change the mask size using the CPU_*_S macros ie, use CPU_ALLOC to allocate cpumask, CPU_ALLOC_SIZE for size. Same fix needed for mask which is used to setaffinity so that mask size is large enough to represent number of possible CPU's in the system.

Quoted 7 lines
Quoted 2 lines

Thanks,
Namhyung

Thanks Namhyung for the Ack

Athira.

Quoted 110 lines
Quoted 110 lines

---
tools/perf/tests/perf-record.c | 36 ++++++++++++++++++++++++----------

1 file changed, 26 insertions(+), 10 deletions(-)
tools/perf/tests/perf-record.c +5 -1
index 0b3c37e66871..d895df037707 100644
@@ -13,15 +13,19 @@
 #include "tests.h"
 #include "util/mmap.h"
 #include "util/sample.h"
+#include "util/cpumap.h"
 
 static int sched__get_first_possible_cpu(pid_t pid, cpu_set_t *maskp)
 {
- int i, cpu = -1, nrcpus = 1024;
+ int i, cpu = -1;
+ int nrcpus = cpu__max_cpu().cpu;
+ size_t size = CPU_ALLOC_SIZE(nrcpus);
+

realloc:
- CPU_ZERO(maskp);
+ CPU_ZERO_S(size, maskp);

- if (sched_getaffinity(pid, sizeof(*maskp), maskp) == -1) {
- if (errno == EINVAL && nrcpus < (1024 << 8)) {
+ if (sched_getaffinity(pid, size, maskp) == -1) {
+ if (errno == EINVAL && nrcpus < (cpu__max_cpu().cpu << 8)) { nrcpus = nrcpus << 2;
goto realloc;
}

tools/perf/tests/perf-record.c +1 -1
@@ -30,11 +34,11 @@ static int sched__get_first_possible_cpu(pid_t pid, cpu_set_t *maskp)
 }
 
 for (i = 0; i < nrcpus; i++) {
- if (CPU_ISSET(i, maskp)) {
+ if (CPU_ISSET_S(i, size, maskp)) {
 if (cpu == -1)
 cpu = i;

else
- CPU_CLR(i, maskp);
+ CPU_CLR_S(i, size, maskp);
}
}

tools/perf/tests/perf-record.c +12 -2
@@ -50,8 +54,9 @@ static int test__PERF_RECORD(struct test_suite *test __maybe_unused, int subtest
 .no_buffering = true,
 .mmap_pages   = 256,
 };
- cpu_set_t cpu_mask;
- size_t cpu_mask_size = sizeof(cpu_mask);
+ int nrcpus = cpu__max_cpu().cpu;
+ cpu_set_t *cpu_mask;
+ size_t cpu_mask_size;
 struct evlist *evlist = evlist__new_dummy();
 struct evsel *evsel;
 struct perf_sample sample;
@@ -69,12 +74,22 @@ static int test__PERF_RECORD(struct test_suite *test __maybe_unused, int subtest
 int total_events = 0, nr_events[PERF_RECORD_MAX] = { 0, };
 char sbuf[STRERR_BUFSIZE];
 
+ cpu_mask = CPU_ALLOC(nrcpus);
+ if (!cpu_mask) {
+ pr_debug("failed to create cpumask\n");
+ goto out;
+ }
+
+ cpu_mask_size = CPU_ALLOC_SIZE(nrcpus);
+ CPU_ZERO_S(cpu_mask_size, cpu_mask);
+
 perf_sample__init(&sample, /*all=*/false);

if (evlist == NULL) /* Fallback for kernels lacking PERF_COUNT_SW_DUMMY */ evlist = evlist__new_default();

if (evlist == NULL) {
pr_debug("Not enough memory to create evlist\n");
+ CPU_FREE(cpu_mask);
goto out;
}

tools/perf/tests/perf-record.c +3 -2
@@ -111,7 +126,7 @@ static int test__PERF_RECORD(struct test_suite *test __maybe_unused, int subtest
 evsel__set_sample_bit(evsel, TIME);
 evlist__config(evlist, &opts, NULL);
 
- err = sched__get_first_possible_cpu(evlist->workload.pid, &cpu_mask);
+ err = sched__get_first_possible_cpu(evlist->workload.pid, cpu_mask);
 if (err < 0) {
 pr_debug("sched__get_first_possible_cpu: %s\n",
 str_error_r(errno, sbuf, sizeof(sbuf)));
@@ -123,7 +138,7 @@ static int test__PERF_RECORD(struct test_suite *test __maybe_unused, int subtest
 /*
 * So that we can check perf_sample.cpu on all the samples.
 */
- if (sched_setaffinity(evlist->workload.pid, cpu_mask_size, &cpu_mask) < 0) {
+ if (sched_setaffinity(evlist->workload.pid, cpu_mask_size, cpu_mask) < 0) {
 pr_debug("sched_setaffinity: %s\n",
 str_error_r(errno, sbuf, sizeof(sbuf)));
 goto out_delete_evlist;
@@ -328,6 +343,7 @@ static int test__PERF_RECORD(struct test_suite *test __maybe_unused, int subtest
++errs;
 }

out_delete_evlist:
+ CPU_FREE(cpu_mask);
evlist__delete(evlist);
out:
perf_sample__exit(&sample);

Signature · 1 line
2.43.7
#4 lore raw
Quoted 36 lines · Athira Rajeev
Quoted 33 lines · Namhyung Kim

Hello,

Quoted 24 lines · Athira Rajeev

The perf record testcase fails on systems with more than 1K CPUs.

Testcase: perf test -vv "PERF_RECORD_* events & perf_sample fields"

PERF_RECORD_* events & perf_sample fields :
--- start ---
test child forked, pid 272482
sched_getaffinity: Invalid argument sched__get_first_possible_cpu: Invalid argument test child finished with -1
---- end ----
PERF_RECORD_* events & perf_sample fields: FAILED!

sched__get_first_possible_cpu uses "sched_getaffinity" to get the cpumask and this call is returning EINVAL (Invalid argument). This happens because the default mask size in glibc is 1024. To overcome this 1024 CPUs mask size limitation of cpu_set_t, change the mask size using the CPU_*_S macros ie, use CPU_ALLOC to allocate cpumask, CPU_ALLOC_SIZE for size. Same fix needed for mask which is used to setaffinity so that mask size is large enough to represent number of possible CPU's in the system.

Quoted 8 lines
Quoted 4 lines

Thanks,
Namhyung

Thanks Namhyung for the Ack

Athira.

Hi,

Can we please pull this patch in if this looks good ?

Thanks
Athira

Quoted 110 lines
Quoted 110 lines
Quoted 110 lines

---
tools/perf/tests/perf-record.c | 36 ++++++++++++++++++++++++----------

1 file changed, 26 insertions(+), 10 deletions(-)
tools/perf/tests/perf-record.c +5 -1
index 0b3c37e66871..d895df037707 100644
@@ -13,15 +13,19 @@
 #include "tests.h"
 #include "util/mmap.h"
 #include "util/sample.h"
+#include "util/cpumap.h"
 
 static int sched__get_first_possible_cpu(pid_t pid, cpu_set_t *maskp)
 {
- int i, cpu = -1, nrcpus = 1024;
+ int i, cpu = -1;
+ int nrcpus = cpu__max_cpu().cpu;
+ size_t size = CPU_ALLOC_SIZE(nrcpus);
+

realloc:
- CPU_ZERO(maskp);
+ CPU_ZERO_S(size, maskp);

- if (sched_getaffinity(pid, sizeof(*maskp), maskp) == -1) {
- if (errno == EINVAL && nrcpus < (1024 << 8)) {
+ if (sched_getaffinity(pid, size, maskp) == -1) {
+ if (errno == EINVAL && nrcpus < (cpu__max_cpu().cpu << 8)) { nrcpus = nrcpus << 2;
goto realloc;
}

tools/perf/tests/perf-record.c +1 -1
@@ -30,11 +34,11 @@ static int sched__get_first_possible_cpu(pid_t pid, cpu_set_t *maskp)
 }
 
 for (i = 0; i < nrcpus; i++) {
- if (CPU_ISSET(i, maskp)) {
+ if (CPU_ISSET_S(i, size, maskp)) {
 if (cpu == -1)
 cpu = i;

else
- CPU_CLR(i, maskp);
+ CPU_CLR_S(i, size, maskp);
}
}

tools/perf/tests/perf-record.c +12 -2
@@ -50,8 +54,9 @@ static int test__PERF_RECORD(struct test_suite *test __maybe_unused, int subtest
 .no_buffering = true,
 .mmap_pages   = 256,
 };
- cpu_set_t cpu_mask;
- size_t cpu_mask_size = sizeof(cpu_mask);
+ int nrcpus = cpu__max_cpu().cpu;
+ cpu_set_t *cpu_mask;
+ size_t cpu_mask_size;
 struct evlist *evlist = evlist__new_dummy();
 struct evsel *evsel;
 struct perf_sample sample;
@@ -69,12 +74,22 @@ static int test__PERF_RECORD(struct test_suite *test __maybe_unused, int subtest
 int total_events = 0, nr_events[PERF_RECORD_MAX] = { 0, };
 char sbuf[STRERR_BUFSIZE];
 
+ cpu_mask = CPU_ALLOC(nrcpus);
+ if (!cpu_mask) {
+ pr_debug("failed to create cpumask\n");
+ goto out;
+ }
+
+ cpu_mask_size = CPU_ALLOC_SIZE(nrcpus);
+ CPU_ZERO_S(cpu_mask_size, cpu_mask);
+
 perf_sample__init(&sample, /*all=*/false);

if (evlist == NULL) /* Fallback for kernels lacking PERF_COUNT_SW_DUMMY */ evlist = evlist__new_default();

if (evlist == NULL) {
pr_debug("Not enough memory to create evlist\n");
+ CPU_FREE(cpu_mask);
goto out;
}

tools/perf/tests/perf-record.c +3 -2
@@ -111,7 +126,7 @@ static int test__PERF_RECORD(struct test_suite *test __maybe_unused, int subtest
 evsel__set_sample_bit(evsel, TIME);
 evlist__config(evlist, &opts, NULL);
 
- err = sched__get_first_possible_cpu(evlist->workload.pid, &cpu_mask);
+ err = sched__get_first_possible_cpu(evlist->workload.pid, cpu_mask);
 if (err < 0) {
 pr_debug("sched__get_first_possible_cpu: %s\n",
 str_error_r(errno, sbuf, sizeof(sbuf)));
@@ -123,7 +138,7 @@ static int test__PERF_RECORD(struct test_suite *test __maybe_unused, int subtest
 /*
 * So that we can check perf_sample.cpu on all the samples.
 */
- if (sched_setaffinity(evlist->workload.pid, cpu_mask_size, &cpu_mask) < 0) {
+ if (sched_setaffinity(evlist->workload.pid, cpu_mask_size, cpu_mask) < 0) {
 pr_debug("sched_setaffinity: %s\n",
 str_error_r(errno, sbuf, sizeof(sbuf)));
 goto out_delete_evlist;
@@ -328,6 +343,7 @@ static int test__PERF_RECORD(struct test_suite *test __maybe_unused, int subtest
++errs;
 }

out_delete_evlist:
+ CPU_FREE(cpu_mask);
evlist__delete(evlist);
out:
perf_sample__exit(&sample);

Signature · 1 line
2.43.7
reviewer Tested-by
#5 lore raw
Quoted 6 lines · Athira Rajeev
Quoted 6 lines · Athira Rajeev
Quoted 5 lines · Namhyung Kim

Hello,

On Thu, Aug 14, 2025 at 05:19:08PM +0530, Athira Rajeev wrote:

Quoted 8 lines
Quoted 5 lines
Quoted 5 lines
Quoted 2 lines

Hi,

Can we please pull this patch in if this looks good ?

Thanks,
Ian

maintainer Applied
#6 lore raw
Quoted 10 lines · Ian Rogers
Quoted 9 lines · Athira Rajeev
Quoted 9 lines · Athira Rajeev
Quoted 8 lines · Namhyung Kim

Hello,

Quoted 2 lines · Athira Rajeev
Quoted 8 lines
Quoted 5 lines
Quoted 2 lines
Quoted 2 lines

Hi,

Can we please pull this patch in if this looks good ?

Thanks, applied to perf-tools-next,

- Arnaldo

#7 lore raw
Quoted 19 lines · Arnaldo Carvalho de Melo
Quoted 14 lines · Ian Rogers
Quoted 13 lines · Athira Rajeev
Quoted 11 lines · Athira Rajeev
Quoted 10 lines · Namhyung Kim

Hello,

Quoted 2 lines · Athira Rajeev

Hi,

Quoted 8 lines
Quoted 4 lines
Quoted 1 line

Can we please pull this patch in if this looks good ?

Thanks, applied to perf-tools-next,

- Arnaldo

Thanks Arnaldo for picking the patch

Thanks
Athira

Tested-by on v1 1/1
#8 lore raw
In reply to Athira Rajeev
Quoted 124 lines · Athira Rajeev

The perf record testcase fails on systems with more than 1K CPUs.

Testcase: perf test -vv "PERF_RECORD_* events & perf_sample fields"

PERF_RECORD_* events & perf_sample fields :
--- start ---
test child forked, pid 272482
sched_getaffinity: Invalid argument sched__get_first_possible_cpu: Invalid argument test child finished with -1
---- end ----
PERF_RECORD_* events & perf_sample fields: FAILED!

sched__get_first_possible_cpu uses "sched_getaffinity" to get the cpumask and this call is returning EINVAL (Invalid argument). This happens because the default mask size in glibc is 1024. To overcome this 1024 CPUs mask size limitation of cpu_set_t, change the mask size using the CPU_*_S macros ie, use CPU_ALLOC to allocate cpumask, CPU_ALLOC_SIZE for size. Same fix needed for mask which is used to setaffinity so that mask size is large enough to represent number of possible CPU's in the system.

---
  tools/perf/tests/perf-record.c | 36 ++++++++++++++++++++++++----------
  1 file changed, 26 insertions(+), 10 deletions(-)
tools/perf/tests/perf-record.c +25 -10
index 0b3c37e66871..d895df037707 100644
@@ -13,15 +13,19 @@
  #include "tests.h"
  #include "util/mmap.h"
  #include "util/sample.h"
+#include "util/cpumap.h"
 
  static int sched__get_first_possible_cpu(pid_t pid, cpu_set_t *maskp)
  {
-	int i, cpu = -1, nrcpus = 1024;
+	int i, cpu = -1;
+	int nrcpus = cpu__max_cpu().cpu;
+	size_t size = CPU_ALLOC_SIZE(nrcpus);
+
  realloc:
-	CPU_ZERO(maskp);
+	CPU_ZERO_S(size, maskp);
 
-	if (sched_getaffinity(pid, sizeof(*maskp), maskp) == -1) {
-		if (errno == EINVAL && nrcpus < (1024 << 8)) {
+	if (sched_getaffinity(pid, size, maskp) == -1) {
+		if (errno == EINVAL && nrcpus < (cpu__max_cpu().cpu << 8)) {
  			nrcpus = nrcpus << 2;
  			goto realloc;
  		}
@@ -30,11 +34,11 @@ static int sched__get_first_possible_cpu(pid_t pid, cpu_set_t *maskp)
  	}
 
  	for (i = 0; i < nrcpus; i++) {
-		if (CPU_ISSET(i, maskp)) {
+		if (CPU_ISSET_S(i, size, maskp)) {
  			if (cpu == -1)
  				cpu = i;
  			else
-				CPU_CLR(i, maskp);
+				CPU_CLR_S(i, size, maskp);
  		}
  	}
 
@@ -50,8 +54,9 @@ static int test__PERF_RECORD(struct test_suite *test __maybe_unused, int subtest
  		.no_buffering = true,
  		.mmap_pages   = 256,
  	};
-	cpu_set_t cpu_mask;
-	size_t cpu_mask_size = sizeof(cpu_mask);
+	int nrcpus = cpu__max_cpu().cpu;
+	cpu_set_t *cpu_mask;
+	size_t cpu_mask_size;
  	struct evlist *evlist = evlist__new_dummy();
  	struct evsel *evsel;
  	struct perf_sample sample;
@@ -69,12 +74,22 @@ static int test__PERF_RECORD(struct test_suite *test __maybe_unused, int subtest
  	int total_events = 0, nr_events[PERF_RECORD_MAX] = { 0, };
  	char sbuf[STRERR_BUFSIZE];
 
+	cpu_mask = CPU_ALLOC(nrcpus);
+	if (!cpu_mask) {
+		pr_debug("failed to create cpumask\n");
+		goto out;
+	}
+
+	cpu_mask_size = CPU_ALLOC_SIZE(nrcpus);
+	CPU_ZERO_S(cpu_mask_size, cpu_mask);
+
  	perf_sample__init(&sample, /*all=*/false);
  	if (evlist == NULL) /* Fallback for kernels lacking PERF_COUNT_SW_DUMMY */
  		evlist = evlist__new_default();
 
  	if (evlist == NULL) {
  		pr_debug("Not enough memory to create evlist\n");
+		CPU_FREE(cpu_mask);
  		goto out;
  	}
 
@@ -111,7 +126,7 @@ static int test__PERF_RECORD(struct test_suite *test __maybe_unused, int subtest
  	evsel__set_sample_bit(evsel, TIME);
  	evlist__config(evlist, &opts, NULL);
 
-	err = sched__get_first_possible_cpu(evlist->workload.pid, &cpu_mask);
+	err = sched__get_first_possible_cpu(evlist->workload.pid, cpu_mask);
  	if (err < 0) {
  		pr_debug("sched__get_first_possible_cpu: %s\n",
  			 str_error_r(errno, sbuf, sizeof(sbuf)));
@@ -123,7 +138,7 @@ static int test__PERF_RECORD(struct test_suite *test __maybe_unused, int subtest
  	/*
  	 * So that we can check perf_sample.cpu on all the samples.
  	 */
-	if (sched_setaffinity(evlist->workload.pid, cpu_mask_size, &cpu_mask) < 0) {
+	if (sched_setaffinity(evlist->workload.pid, cpu_mask_size, cpu_mask) < 0) {
  		pr_debug("sched_setaffinity: %s\n",
  			 str_error_r(errno, sbuf, sizeof(sbuf)));
  		goto out_delete_evlist;

tools/perf/tests/perf-record.c static int test__PERF_RECORD(struct test_suite *test __maybe_unused, int subtest · line 349

Quoted 7 lines
tools/perf/tests/perf-record.c +1 -0
@@ -328,6 +343,7 @@ static int test__PERF_RECORD(struct test_suite *test __maybe_unused, int subtest
  		++errs;
  	}
  out_delete_evlist:
+	CPU_FREE(cpu_mask);
  	evlist__delete(evlist);
  out:
  	perf_sample__exit(&sample);

Hi,

I have tested the patch on the setup where the issue was seen, the patch fixes the issue. Please add the tested-by tag as below.

Thanks & Regards
Tejas

#9 lore raw
Quoted 133 lines · On 19 Aug 2025, at 5:26 PM, tejas05 <[email protected]> wrote:
Quoted 116 lines · Athira Rajeev

The perf record testcase fails on systems with more than 1K CPUs.

Testcase: perf test -vv "PERF_RECORD_* events & perf_sample fields"

PERF_RECORD_* events & perf_sample fields :
--- start ---
test child forked, pid 272482
sched_getaffinity: Invalid argument sched__get_first_possible_cpu: Invalid argument test child finished with -1
---- end ----
PERF_RECORD_* events & perf_sample fields: FAILED!

sched__get_first_possible_cpu uses "sched_getaffinity" to get the cpumask and this call is returning EINVAL (Invalid argument). This happens because the default mask size in glibc is 1024. To overcome this 1024 CPUs mask size limitation of cpu_set_t, change the mask size using the CPU_*_S macros ie, use CPU_ALLOC to allocate cpumask, CPU_ALLOC_SIZE for size. Same fix needed for mask which is used to setaffinity so that mask size is large enough to represent number of possible CPU's in the system.

---
 tools/perf/tests/perf-record.c | 36 ++++++++++++++++++++++++----------
 1 file changed, 26 insertions(+), 10 deletions(-)
tools/perf/tests/perf-record.c +10 -5
index 0b3c37e66871..d895df037707 100644
@@ -13,15 +13,19 @@
 #include "tests.h"
 #include "util/mmap.h"
 #include "util/sample.h"
+#include "util/cpumap.h"
   static int sched__get_first_possible_cpu(pid_t pid, cpu_set_t *maskp)
 {
- int i, cpu = -1, nrcpus = 1024;
+ int i, cpu = -1;
+ int nrcpus = cpu__max_cpu().cpu;
+ size_t size = CPU_ALLOC_SIZE(nrcpus);
+
 realloc:
- CPU_ZERO(maskp);
+ CPU_ZERO_S(size, maskp);
 - if (sched_getaffinity(pid, sizeof(*maskp), maskp) == -1) {
- if (errno == EINVAL && nrcpus < (1024 << 8)) {
+ if (sched_getaffinity(pid, size, maskp) == -1) {
+ if (errno == EINVAL && nrcpus < (cpu__max_cpu().cpu << 8)) {
  nrcpus = nrcpus << 2;
  goto realloc;
  }
@@ -30,11 +34,11 @@ static int sched__get_first_possible_cpu(pid_t pid, cpu_set_t *maskp)
  }
    for (i = 0; i < nrcpus; i++) {
- if (CPU_ISSET(i, maskp)) {
+ if (CPU_ISSET_S(i, size, maskp)) {
  if (cpu == -1)
  cpu = i;
  else
- CPU_CLR(i, maskp);
+ CPU_CLR_S(i, size, maskp);
  }
  }
 @@ -50,8 +54,9 @@ static int test__PERF_RECORD(struct test_suite *test __maybe_unused, int subtest
  .no_buffering = true,

.mmap_pages = 256,
};
- cpu_set_t cpu_mask;
- size_t cpu_mask_size = sizeof(cpu_mask);
+ int nrcpus = cpu__max_cpu().cpu;
+ cpu_set_t *cpu_mask;
+ size_t cpu_mask_size;
struct evlist *evlist = evlist__new_dummy();
struct evsel *evsel;
struct perf_sample sample;

tools/perf/tests/perf-record.c +11 -1
@@ -69,12 +74,22 @@ static int test__PERF_RECORD(struct test_suite *test __maybe_unused, int subtest
  int total_events = 0, nr_events[PERF_RECORD_MAX] = { 0, };
  char sbuf[STRERR_BUFSIZE];
 + cpu_mask = CPU_ALLOC(nrcpus);
+ if (!cpu_mask) {
+ pr_debug("failed to create cpumask\n");
+ goto out;
+ }
+
+ cpu_mask_size = CPU_ALLOC_SIZE(nrcpus);
+ CPU_ZERO_S(cpu_mask_size, cpu_mask);
+
  perf_sample__init(&sample, /*all=*/false);
  if (evlist == NULL) /* Fallback for kernels lacking PERF_COUNT_SW_DUMMY */
  evlist = evlist__new_default();
    if (evlist == NULL) {
  pr_debug("Not enough memory to create evlist\n");
+ CPU_FREE(cpu_mask);
  goto out;
  }
 @@ -111,7 +126,7 @@ static int test__PERF_RECORD(struct test_suite *test __maybe_unused, int subtest
  evsel__set_sample_bit(evsel, TIME);
  evlist__config(evlist, &opts, NULL);
 - err = sched__get_first_possible_cpu(evlist->workload.pid, &cpu_mask);
+ err = sched__get_first_possible_cpu(evlist->workload.pid, cpu_mask);
  if (err < 0) {
  pr_debug("sched__get_first_possible_cpu: %s\n",
   str_error_r(errno, sbuf, sizeof(sbuf)));
@@ -123,7 +138,7 @@ static int test__PERF_RECORD(struct test_suite *test __maybe_unused, int subtest
  /*
   * So that we can check perf_sample.cpu on all the samples.
   */
- if (sched_setaffinity(evlist->workload.pid, cpu_mask_size, &cpu_mask) < 0) {
+ if (sched_setaffinity(evlist->workload.pid, cpu_mask_size, cpu_mask) < 0) {
  pr_debug("sched_setaffinity: %s\n",
   str_error_r(errno, sbuf, sizeof(sbuf)));
  goto out_delete_evlist;

tools/perf/tests/perf-record.c static int test__PERF_RECORD(struct test_suite *test __maybe_unused, int subtest · line 349

Quoted 7 lines
tools/perf/tests/perf-record.c +1 -0
@@ -328,6 +343,7 @@ static int test__PERF_RECORD(struct test_suite *test __maybe_unused, int subtest
  ++errs;
  }
 out_delete_evlist:
+ CPU_FREE(cpu_mask);
  evlist__delete(evlist);
 out:
  perf_sample__exit(&sample);

Hi,

Quoted 5 lines

I have tested the patch on the setup where the issue was seen, the patch fixes the issue. Please add the tested-by tag as below.

Thanks & Regards
Tejas

Hi Tejas, Venkat

Thanks for testing the patch

Athira.