1 // SPDX-License-Identifier: GPL-2.0
4 /* For the CPU_* macros */
10 #include <api/fs/fs.h>
11 #include <linux/err.h>
12 #include <api/fs/tracing_path.h>
15 #include "thread_map.h"
20 int test__openat_syscall_event_on_all_cpus(struct test *test __maybe_unused, int subtest __maybe_unused)
22 int err = -1, fd, cpu;
24 struct perf_evsel *evsel;
25 unsigned int nr_openat_calls = 111, i;
27 struct thread_map *threads = thread_map__new(-1, getpid(), UINT_MAX);
28 char sbuf[STRERR_BUFSIZE];
31 if (threads == NULL) {
32 pr_debug("thread_map__new\n");
36 cpus = cpu_map__new(NULL);
38 pr_debug("cpu_map__new\n");
39 goto out_thread_map_delete;
44 evsel = perf_evsel__newtp("syscalls", "sys_enter_openat");
46 tracing_path__strerror_open_tp(errno, errbuf, sizeof(errbuf), "syscalls", "sys_enter_openat");
47 pr_debug("%s\n", errbuf);
48 goto out_cpu_map_delete;
51 if (perf_evsel__open(evsel, cpus, threads) < 0) {
52 pr_debug("failed to open counter: %s, "
53 "tweak /proc/sys/kernel/perf_event_paranoid?\n",
54 str_error_r(errno, sbuf, sizeof(sbuf)));
55 goto out_evsel_delete;
58 for (cpu = 0; cpu < cpus->nr; ++cpu) {
59 unsigned int ncalls = nr_openat_calls + cpu;
61 * XXX eventually lift this restriction in a way that
62 * keeps perf building on older glibc installations
63 * without CPU_ALLOC. 1024 cpus in 2010 still seems
64 * a reasonable upper limit tho :-)
66 if (cpus->map[cpu] >= CPU_SETSIZE) {
67 pr_debug("Ignoring CPU %d\n", cpus->map[cpu]);
71 CPU_SET(cpus->map[cpu], &cpu_set);
72 if (sched_setaffinity(0, sizeof(cpu_set), &cpu_set) < 0) {
73 pr_debug("sched_setaffinity() failed on CPU %d: %s ",
75 str_error_r(errno, sbuf, sizeof(sbuf)));
78 for (i = 0; i < ncalls; ++i) {
79 fd = openat(0, "/etc/passwd", O_RDONLY);
82 CPU_CLR(cpus->map[cpu], &cpu_set);
86 * Here we need to explicitly preallocate the counts, as if
87 * we use the auto allocation it will allocate just for 1 cpu,
88 * as we start by cpu 0.
90 if (perf_evsel__alloc_counts(evsel, cpus->nr, 1) < 0) {
91 pr_debug("perf_evsel__alloc_counts(ncpus=%d)\n", cpus->nr);
97 for (cpu = 0; cpu < cpus->nr; ++cpu) {
98 unsigned int expected;
100 if (cpus->map[cpu] >= CPU_SETSIZE)
103 if (perf_evsel__read_on_cpu(evsel, cpu, 0) < 0) {
104 pr_debug("perf_evsel__read_on_cpu\n");
109 expected = nr_openat_calls + cpu;
110 if (perf_counts(evsel->counts, cpu, 0)->val != expected) {
111 pr_debug("perf_evsel__read_on_cpu: expected to intercept %d calls on cpu %d, got %" PRIu64 "\n",
112 expected, cpus->map[cpu], perf_counts(evsel->counts, cpu, 0)->val);
117 perf_evsel__free_counts(evsel);
119 perf_evsel__close_fd(evsel);
121 perf_evsel__delete(evsel);
124 out_thread_map_delete:
125 thread_map__put(threads);