1 // SPDX-License-Identifier: GPL-2.0
2 #include <errno.h>
3 #include <inttypes.h>
4 /* For the CPU_* macros */
5 #include <pthread.h>
6 
7 #include <sys/types.h>
8 #include <sys/stat.h>
9 #include <fcntl.h>
10 #include <api/fs/fs.h>
11 #include <linux/err.h>
12 #include <linux/string.h>
13 #include <api/fs/tracing_path.h>
14 #include "evsel.h"
15 #include "tests.h"
16 #include "thread_map.h"
17 #include <perf/cpumap.h>
18 #include <internal/cpumap.h>
19 #include "debug.h"
20 #include "stat.h"
21 #include "util/counts.h"
22 
test__openat_syscall_event_on_all_cpus(struct test * test __maybe_unused,int subtest __maybe_unused)23 int test__openat_syscall_event_on_all_cpus(struct test *test __maybe_unused, int subtest __maybe_unused)
24 {
25 	int err = -1, fd, cpu;
26 	struct perf_cpu_map *cpus;
27 	struct evsel *evsel;
28 	unsigned int nr_openat_calls = 111, i;
29 	cpu_set_t cpu_set;
30 	struct perf_thread_map *threads = thread_map__new(-1, getpid(), UINT_MAX);
31 	char sbuf[STRERR_BUFSIZE];
32 	char errbuf[BUFSIZ];
33 
34 	if (threads == NULL) {
35 		pr_debug("thread_map__new\n");
36 		return -1;
37 	}
38 
39 	cpus = perf_cpu_map__new(NULL);
40 	if (cpus == NULL) {
41 		pr_debug("perf_cpu_map__new\n");
42 		goto out_thread_map_delete;
43 	}
44 
45 	CPU_ZERO(&cpu_set);
46 
47 	evsel = perf_evsel__newtp("syscalls", "sys_enter_openat");
48 	if (IS_ERR(evsel)) {
49 		tracing_path__strerror_open_tp(errno, errbuf, sizeof(errbuf), "syscalls", "sys_enter_openat");
50 		pr_debug("%s\n", errbuf);
51 		goto out_cpu_map_delete;
52 	}
53 
54 	if (evsel__open(evsel, cpus, threads) < 0) {
55 		pr_debug("failed to open counter: %s, "
56 			 "tweak /proc/sys/kernel/perf_event_paranoid?\n",
57 			 str_error_r(errno, sbuf, sizeof(sbuf)));
58 		goto out_evsel_delete;
59 	}
60 
61 	for (cpu = 0; cpu < cpus->nr; ++cpu) {
62 		unsigned int ncalls = nr_openat_calls + cpu;
63 		/*
64 		 * XXX eventually lift this restriction in a way that
65 		 * keeps perf building on older glibc installations
66 		 * without CPU_ALLOC. 1024 cpus in 2010 still seems
67 		 * a reasonable upper limit tho :-)
68 		 */
69 		if (cpus->map[cpu] >= CPU_SETSIZE) {
70 			pr_debug("Ignoring CPU %d\n", cpus->map[cpu]);
71 			continue;
72 		}
73 
74 		CPU_SET(cpus->map[cpu], &cpu_set);
75 		if (sched_setaffinity(0, sizeof(cpu_set), &cpu_set) < 0) {
76 			pr_debug("sched_setaffinity() failed on CPU %d: %s ",
77 				 cpus->map[cpu],
78 				 str_error_r(errno, sbuf, sizeof(sbuf)));
79 			goto out_close_fd;
80 		}
81 		for (i = 0; i < ncalls; ++i) {
82 			fd = openat(0, "/etc/passwd", O_RDONLY);
83 			close(fd);
84 		}
85 		CPU_CLR(cpus->map[cpu], &cpu_set);
86 	}
87 
88 	/*
89 	 * Here we need to explicitly preallocate the counts, as if
90 	 * we use the auto allocation it will allocate just for 1 cpu,
91 	 * as we start by cpu 0.
92 	 */
93 	if (perf_evsel__alloc_counts(evsel, cpus->nr, 1) < 0) {
94 		pr_debug("perf_evsel__alloc_counts(ncpus=%d)\n", cpus->nr);
95 		goto out_close_fd;
96 	}
97 
98 	err = 0;
99 
100 	for (cpu = 0; cpu < cpus->nr; ++cpu) {
101 		unsigned int expected;
102 
103 		if (cpus->map[cpu] >= CPU_SETSIZE)
104 			continue;
105 
106 		if (perf_evsel__read_on_cpu(evsel, cpu, 0) < 0) {
107 			pr_debug("perf_evsel__read_on_cpu\n");
108 			err = -1;
109 			break;
110 		}
111 
112 		expected = nr_openat_calls + cpu;
113 		if (perf_counts(evsel->counts, cpu, 0)->val != expected) {
114 			pr_debug("perf_evsel__read_on_cpu: expected to intercept %d calls on cpu %d, got %" PRIu64 "\n",
115 				 expected, cpus->map[cpu], perf_counts(evsel->counts, cpu, 0)->val);
116 			err = -1;
117 		}
118 	}
119 
120 	perf_evsel__free_counts(evsel);
121 out_close_fd:
122 	perf_evsel__close_fd(&evsel->core);
123 out_evsel_delete:
124 	evsel__delete(evsel);
125 out_cpu_map_delete:
126 	perf_cpu_map__put(cpus);
127 out_thread_map_delete:
128 	perf_thread_map__put(threads);
129 	return err;
130 }
131