xref: /openbmc/linux/tools/perf/tests/mmap-basic.c (revision 519b58bb)
1 // SPDX-License-Identifier: GPL-2.0
2 #include <errno.h>
3 #include <inttypes.h>
4 #include <stdlib.h>
5 #include <perf/cpumap.h>
6 
7 #include "debug.h"
8 #include "evlist.h"
9 #include "evsel.h"
10 #include "thread_map.h"
11 #include "tests.h"
12 #include "util/mmap.h"
13 #include <linux/err.h>
14 #include <linux/kernel.h>
15 #include <linux/string.h>
16 #include <perf/evlist.h>
17 #include <perf/mmap.h>
18 
19 /*
20  * This test will generate random numbers of calls to some getpid syscalls,
21  * then establish an mmap for a group of events that are created to monitor
22  * the syscalls.
23  *
24  * It will receive the events, using mmap, use its PERF_SAMPLE_ID generated
25  * sample.id field to map back to its respective perf_evsel instance.
26  *
27  * Then it checks if the number of syscalls reported as perf events by
28  * the kernel corresponds to the number of syscalls made.
29  */
30 static int test__basic_mmap(struct test_suite *test __maybe_unused, int subtest __maybe_unused)
31 {
32 	int err = TEST_FAIL;
33 	union perf_event *event;
34 	struct perf_thread_map *threads;
35 	struct perf_cpu_map *cpus;
36 	struct evlist *evlist;
37 	cpu_set_t cpu_set;
38 	const char *syscall_names[] = { "getsid", "getppid", "getpgid", };
39 	pid_t (*syscalls[])(void) = { (void *)getsid, getppid, (void*)getpgid };
40 #define nsyscalls ARRAY_SIZE(syscall_names)
41 	unsigned int nr_events[nsyscalls],
42 		     expected_nr_events[nsyscalls], i, j;
43 	struct evsel *evsels[nsyscalls], *evsel;
44 	char sbuf[STRERR_BUFSIZE];
45 	struct mmap *md;
46 
47 	threads = thread_map__new(-1, getpid(), UINT_MAX);
48 	if (threads == NULL) {
49 		pr_debug("thread_map__new\n");
50 		return -1;
51 	}
52 
53 	cpus = perf_cpu_map__new(NULL);
54 	if (cpus == NULL) {
55 		pr_debug("perf_cpu_map__new\n");
56 		goto out_free_threads;
57 	}
58 
59 	CPU_ZERO(&cpu_set);
60 	CPU_SET(perf_cpu_map__cpu(cpus, 0).cpu, &cpu_set);
61 	sched_setaffinity(0, sizeof(cpu_set), &cpu_set);
62 	if (sched_setaffinity(0, sizeof(cpu_set), &cpu_set) < 0) {
63 		pr_debug("sched_setaffinity() failed on CPU %d: %s ",
64 			 perf_cpu_map__cpu(cpus, 0).cpu,
65 			 str_error_r(errno, sbuf, sizeof(sbuf)));
66 		goto out_free_cpus;
67 	}
68 
69 	evlist = evlist__new();
70 	if (evlist == NULL) {
71 		pr_debug("evlist__new\n");
72 		goto out_free_cpus;
73 	}
74 
75 	perf_evlist__set_maps(&evlist->core, cpus, threads);
76 
77 	for (i = 0; i < nsyscalls; ++i) {
78 		char name[64];
79 
80 		snprintf(name, sizeof(name), "sys_enter_%s", syscall_names[i]);
81 		evsels[i] = evsel__newtp("syscalls", name);
82 		if (IS_ERR(evsels[i])) {
83 			pr_debug("evsel__new(%s)\n", name);
84 			if (PTR_ERR(evsels[i]) == -EACCES) {
85 				/* Permissions failure, flag the failure as a skip. */
86 				err = TEST_SKIP;
87 			}
88 			goto out_delete_evlist;
89 		}
90 
91 		evsels[i]->core.attr.wakeup_events = 1;
92 		evsel__set_sample_id(evsels[i], false);
93 
94 		evlist__add(evlist, evsels[i]);
95 
96 		if (evsel__open(evsels[i], cpus, threads) < 0) {
97 			pr_debug("failed to open counter: %s, "
98 				 "tweak /proc/sys/kernel/perf_event_paranoid?\n",
99 				 str_error_r(errno, sbuf, sizeof(sbuf)));
100 			goto out_delete_evlist;
101 		}
102 
103 		nr_events[i] = 0;
104 		expected_nr_events[i] = 1 + rand() % 127;
105 	}
106 
107 	if (evlist__mmap(evlist, 128) < 0) {
108 		pr_debug("failed to mmap events: %d (%s)\n", errno,
109 			 str_error_r(errno, sbuf, sizeof(sbuf)));
110 		goto out_delete_evlist;
111 	}
112 
113 	for (i = 0; i < nsyscalls; ++i)
114 		for (j = 0; j < expected_nr_events[i]; ++j) {
115 			syscalls[i]();
116 		}
117 
118 	md = &evlist->mmap[0];
119 	if (perf_mmap__read_init(&md->core) < 0)
120 		goto out_init;
121 
122 	while ((event = perf_mmap__read_event(&md->core)) != NULL) {
123 		struct perf_sample sample;
124 
125 		if (event->header.type != PERF_RECORD_SAMPLE) {
126 			pr_debug("unexpected %s event\n",
127 				 perf_event__name(event->header.type));
128 			goto out_delete_evlist;
129 		}
130 
131 		err = evlist__parse_sample(evlist, event, &sample);
132 		if (err) {
133 			pr_err("Can't parse sample, err = %d\n", err);
134 			goto out_delete_evlist;
135 		}
136 
137 		err = -1;
138 		evsel = evlist__id2evsel(evlist, sample.id);
139 		if (evsel == NULL) {
140 			pr_debug("event with id %" PRIu64
141 				 " doesn't map to an evsel\n", sample.id);
142 			goto out_delete_evlist;
143 		}
144 		nr_events[evsel->core.idx]++;
145 		perf_mmap__consume(&md->core);
146 	}
147 	perf_mmap__read_done(&md->core);
148 
149 out_init:
150 	err = 0;
151 	evlist__for_each_entry(evlist, evsel) {
152 		if (nr_events[evsel->core.idx] != expected_nr_events[evsel->core.idx]) {
153 			pr_debug("expected %d %s events, got %d\n",
154 				 expected_nr_events[evsel->core.idx],
155 				 evsel__name(evsel), nr_events[evsel->core.idx]);
156 			err = -1;
157 			goto out_delete_evlist;
158 		}
159 	}
160 
161 out_delete_evlist:
162 	evlist__delete(evlist);
163 out_free_cpus:
164 	perf_cpu_map__put(cpus);
165 out_free_threads:
166 	perf_thread_map__put(threads);
167 	return err;
168 }
169 
170 static int test_stat_user_read(int event)
171 {
172 	struct perf_counts_values counts = { .val = 0 };
173 	struct perf_thread_map *threads;
174 	struct perf_evsel *evsel;
175 	struct perf_event_mmap_page *pc;
176 	struct perf_event_attr attr = {
177 		.type	= PERF_TYPE_HARDWARE,
178 		.config	= event,
179 #ifdef __aarch64__
180 		.config1 = 0x2,		/* Request user access */
181 #endif
182 	};
183 	int err, i, ret = TEST_FAIL;
184 	bool opened = false, mapped = false;
185 
186 	threads = perf_thread_map__new_dummy();
187 	TEST_ASSERT_VAL("failed to create threads", threads);
188 
189 	perf_thread_map__set_pid(threads, 0, 0);
190 
191 	evsel = perf_evsel__new(&attr);
192 	TEST_ASSERT_VAL("failed to create evsel", evsel);
193 
194 	err = perf_evsel__open(evsel, NULL, threads);
195 	if (err) {
196 		pr_err("failed to open evsel: %s\n", strerror(-err));
197 		ret = TEST_SKIP;
198 		goto out;
199 	}
200 	opened = true;
201 
202 	err = perf_evsel__mmap(evsel, 0);
203 	if (err) {
204 		pr_err("failed to mmap evsel: %s\n", strerror(-err));
205 		goto out;
206 	}
207 	mapped = true;
208 
209 	pc = perf_evsel__mmap_base(evsel, 0, 0);
210 	if (!pc) {
211 		pr_err("failed to get mmapped address\n");
212 		goto out;
213 	}
214 
215 	if (!pc->cap_user_rdpmc || !pc->index) {
216 		pr_err("userspace counter access not %s\n",
217 			!pc->cap_user_rdpmc ? "supported" : "enabled");
218 		ret = TEST_SKIP;
219 		goto out;
220 	}
221 	if (pc->pmc_width < 32) {
222 		pr_err("userspace counter width not set (%d)\n", pc->pmc_width);
223 		goto out;
224 	}
225 
226 	perf_evsel__read(evsel, 0, 0, &counts);
227 	if (counts.val == 0) {
228 		pr_err("failed to read value for evsel\n");
229 		goto out;
230 	}
231 
232 	for (i = 0; i < 5; i++) {
233 		volatile int count = 0x10000 << i;
234 		__u64 start, end, last = 0;
235 
236 		pr_debug("\tloop = %u, ", count);
237 
238 		perf_evsel__read(evsel, 0, 0, &counts);
239 		start = counts.val;
240 
241 		while (count--) ;
242 
243 		perf_evsel__read(evsel, 0, 0, &counts);
244 		end = counts.val;
245 
246 		if ((end - start) < last) {
247 			pr_err("invalid counter data: end=%llu start=%llu last= %llu\n",
248 				end, start, last);
249 			goto out;
250 		}
251 		last = end - start;
252 		pr_debug("count = %llu\n", end - start);
253 	}
254 	ret = TEST_OK;
255 
256 out:
257 	if (mapped)
258 		perf_evsel__munmap(evsel);
259 	if (opened)
260 		perf_evsel__close(evsel);
261 	perf_evsel__delete(evsel);
262 
263 	perf_thread_map__put(threads);
264 	return ret;
265 }
266 
267 static int test__mmap_user_read_instr(struct test_suite *test __maybe_unused,
268 				      int subtest __maybe_unused)
269 {
270 	return test_stat_user_read(PERF_COUNT_HW_INSTRUCTIONS);
271 }
272 
273 static int test__mmap_user_read_cycles(struct test_suite *test __maybe_unused,
274 				       int subtest __maybe_unused)
275 {
276 	return test_stat_user_read(PERF_COUNT_HW_CPU_CYCLES);
277 }
278 
279 static struct test_case tests__basic_mmap[] = {
280 	TEST_CASE_REASON("Read samples using the mmap interface",
281 			 basic_mmap,
282 			 "permissions"),
283 	TEST_CASE_REASON("User space counter reading of instructions",
284 			 mmap_user_read_instr,
285 #if defined(__i386__) || defined(__x86_64__) || defined(__aarch64__)
286 			 "permissions"
287 #else
288 			 "unsupported"
289 #endif
290 		),
291 	TEST_CASE_REASON("User space counter reading of cycles",
292 			 mmap_user_read_cycles,
293 #if defined(__i386__) || defined(__x86_64__) || defined(__aarch64__)
294 			 "permissions"
295 #else
296 			 "unsupported"
297 #endif
298 		),
299 	{	.name = NULL, }
300 };
301 
302 struct test_suite suite__basic_mmap = {
303 	.desc = "mmap interface tests",
304 	.test_cases = tests__basic_mmap,
305 };
306