Skip to content

Commit c0e885e

Browse files
Athira Rajeevacmel
authored andcommitted
perf tests record: Update 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. Reported-by: Tejas Manhas <[email protected]> Signed-off-by: Athira Rajeev <[email protected]> Tested-by: Ian Rogers <[email protected]> Tested-by: Tejas Manhas <[email protected]> Tested-by: Venkat Rao Bagalkote <[email protected]> Acked-by: Namhyung Kim <[email protected]> Cc: Aditya Bodkhe <[email protected]> Cc: Adrian Hunter <[email protected]> Cc: Hari Bathini <[email protected]> Cc: Jiri Olsa <[email protected]> Cc: Kajol Jain <[email protected]> Cc: Madhavan Srinivasan <[email protected]> Signed-off-by: Arnaldo Carvalho de Melo <[email protected]>
1 parent e7ace97 commit c0e885e

File tree

1 file changed

+26
-10
lines changed

1 file changed

+26
-10
lines changed

tools/perf/tests/perf-record.c

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,19 @@
1313
#include "tests.h"
1414
#include "util/mmap.h"
1515
#include "util/sample.h"
16+
#include "util/cpumap.h"
1617

1718
static int sched__get_first_possible_cpu(pid_t pid, cpu_set_t *maskp)
1819
{
19-
int i, cpu = -1, nrcpus = 1024;
20+
int i, cpu = -1;
21+
int nrcpus = cpu__max_cpu().cpu;
22+
size_t size = CPU_ALLOC_SIZE(nrcpus);
23+
2024
realloc:
21-
CPU_ZERO(maskp);
25+
CPU_ZERO_S(size, maskp);
2226

23-
if (sched_getaffinity(pid, sizeof(*maskp), maskp) == -1) {
24-
if (errno == EINVAL && nrcpus < (1024 << 8)) {
27+
if (sched_getaffinity(pid, size, maskp) == -1) {
28+
if (errno == EINVAL && nrcpus < (cpu__max_cpu().cpu << 8)) {
2529
nrcpus = nrcpus << 2;
2630
goto realloc;
2731
}
@@ -30,11 +34,11 @@ static int sched__get_first_possible_cpu(pid_t pid, cpu_set_t *maskp)
3034
}
3135

3236
for (i = 0; i < nrcpus; i++) {
33-
if (CPU_ISSET(i, maskp)) {
37+
if (CPU_ISSET_S(i, size, maskp)) {
3438
if (cpu == -1)
3539
cpu = i;
3640
else
37-
CPU_CLR(i, maskp);
41+
CPU_CLR_S(i, size, maskp);
3842
}
3943
}
4044

@@ -50,8 +54,9 @@ static int test__PERF_RECORD(struct test_suite *test __maybe_unused, int subtest
5054
.no_buffering = true,
5155
.mmap_pages = 256,
5256
};
53-
cpu_set_t cpu_mask;
54-
size_t cpu_mask_size = sizeof(cpu_mask);
57+
int nrcpus = cpu__max_cpu().cpu;
58+
cpu_set_t *cpu_mask;
59+
size_t cpu_mask_size;
5560
struct evlist *evlist = evlist__new_dummy();
5661
struct evsel *evsel;
5762
struct perf_sample sample;
@@ -69,12 +74,22 @@ static int test__PERF_RECORD(struct test_suite *test __maybe_unused, int subtest
6974
int total_events = 0, nr_events[PERF_RECORD_MAX] = { 0, };
7075
char sbuf[STRERR_BUFSIZE];
7176

77+
cpu_mask = CPU_ALLOC(nrcpus);
78+
if (!cpu_mask) {
79+
pr_debug("failed to create cpumask\n");
80+
goto out;
81+
}
82+
83+
cpu_mask_size = CPU_ALLOC_SIZE(nrcpus);
84+
CPU_ZERO_S(cpu_mask_size, cpu_mask);
85+
7286
perf_sample__init(&sample, /*all=*/false);
7387
if (evlist == NULL) /* Fallback for kernels lacking PERF_COUNT_SW_DUMMY */
7488
evlist = evlist__new_default();
7589

7690
if (evlist == NULL) {
7791
pr_debug("Not enough memory to create evlist\n");
92+
CPU_FREE(cpu_mask);
7893
goto out;
7994
}
8095

@@ -111,7 +126,7 @@ static int test__PERF_RECORD(struct test_suite *test __maybe_unused, int subtest
111126
evsel__set_sample_bit(evsel, TIME);
112127
evlist__config(evlist, &opts, NULL);
113128

114-
err = sched__get_first_possible_cpu(evlist->workload.pid, &cpu_mask);
129+
err = sched__get_first_possible_cpu(evlist->workload.pid, cpu_mask);
115130
if (err < 0) {
116131
pr_debug("sched__get_first_possible_cpu: %s\n",
117132
str_error_r(errno, sbuf, sizeof(sbuf)));
@@ -123,7 +138,7 @@ static int test__PERF_RECORD(struct test_suite *test __maybe_unused, int subtest
123138
/*
124139
* So that we can check perf_sample.cpu on all the samples.
125140
*/
126-
if (sched_setaffinity(evlist->workload.pid, cpu_mask_size, &cpu_mask) < 0) {
141+
if (sched_setaffinity(evlist->workload.pid, cpu_mask_size, cpu_mask) < 0) {
127142
pr_debug("sched_setaffinity: %s\n",
128143
str_error_r(errno, sbuf, sizeof(sbuf)));
129144
goto out_delete_evlist;
@@ -328,6 +343,7 @@ static int test__PERF_RECORD(struct test_suite *test __maybe_unused, int subtest
328343
++errs;
329344
}
330345
out_delete_evlist:
346+
CPU_FREE(cpu_mask);
331347
evlist__delete(evlist);
332348
out:
333349
perf_sample__exit(&sample);

0 commit comments

Comments
 (0)