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