xref: /openbmc/linux/tools/perf/tests/mmap-basic.c (revision 94d964e5)
1 // SPDX-License-Identifier: GPL-2.0
2 #include <errno.h>
3 #include <inttypes.h>
4 /* For the CLR_() macros */
5 #include <pthread.h>
6 #include <stdlib.h>
7 #include <perf/cpumap.h>
8 
9 #include "debug.h"
10 #include "evlist.h"
11 #include "evsel.h"
12 #include "thread_map.h"
13 #include "tests.h"
14 #include "util/mmap.h"
15 #include <linux/err.h>
16 #include <linux/kernel.h>
17 #include <linux/string.h>
18 #include <perf/evlist.h>
19 #include <perf/mmap.h>
20 
21 /*
22  * This test will generate random numbers of calls to some getpid syscalls,
23  * then establish an mmap for a group of events that are created to monitor
24  * the syscalls.
25  *
26  * It will receive the events, using mmap, use its PERF_SAMPLE_ID generated
27  * sample.id field to map back to its respective perf_evsel instance.
28  *
29  * Then it checks if the number of syscalls reported as perf events by
30  * the kernel corresponds to the number of syscalls made.
31  */
32 static int test__basic_mmap(struct test_suite *test __maybe_unused, int subtest __maybe_unused)
33 {
34 	int err = -1;
35 	union perf_event *event;
36 	struct perf_thread_map *threads;
37 	struct perf_cpu_map *cpus;
38 	struct evlist *evlist;
39 	cpu_set_t cpu_set;
40 	const char *syscall_names[] = { "getsid", "getppid", "getpgid", };
41 	pid_t (*syscalls[])(void) = { (void *)getsid, getppid, (void*)getpgid };
42 #define nsyscalls ARRAY_SIZE(syscall_names)
43 	unsigned int nr_events[nsyscalls],
44 		     expected_nr_events[nsyscalls], i, j;
45 	struct evsel *evsels[nsyscalls], *evsel;
46 	char sbuf[STRERR_BUFSIZE];
47 	struct mmap *md;
48 
49 	threads = thread_map__new(-1, getpid(), UINT_MAX);
50 	if (threads == NULL) {
51 		pr_debug("thread_map__new\n");
52 		return -1;
53 	}
54 
55 	cpus = perf_cpu_map__new(NULL);
56 	if (cpus == NULL) {
57 		pr_debug("perf_cpu_map__new\n");
58 		goto out_free_threads;
59 	}
60 
61 	CPU_ZERO(&cpu_set);
62 	CPU_SET(perf_cpu_map__cpu(cpus, 0).cpu, &cpu_set);
63 	sched_setaffinity(0, sizeof(cpu_set), &cpu_set);
64 	if (sched_setaffinity(0, sizeof(cpu_set), &cpu_set) < 0) {
65 		pr_debug("sched_setaffinity() failed on CPU %d: %s ",
66 			 perf_cpu_map__cpu(cpus, 0).cpu,
67 			 str_error_r(errno, sbuf, sizeof(sbuf)));
68 		goto out_free_cpus;
69 	}
70 
71 	evlist = evlist__new();
72 	if (evlist == NULL) {
73 		pr_debug("evlist__new\n");
74 		goto out_free_cpus;
75 	}
76 
77 	perf_evlist__set_maps(&evlist->core, cpus, threads);
78 
79 	for (i = 0; i < nsyscalls; ++i) {
80 		char name[64];
81 
82 		snprintf(name, sizeof(name), "sys_enter_%s", syscall_names[i]);
83 		evsels[i] = evsel__newtp("syscalls", name);
84 		if (IS_ERR(evsels[i])) {
85 			pr_debug("evsel__new(%s)\n", name);
86 			goto out_delete_evlist;
87 		}
88 
89 		evsels[i]->core.attr.wakeup_events = 1;
90 		evsel__set_sample_id(evsels[i], false);
91 
92 		evlist__add(evlist, evsels[i]);
93 
94 		if (evsel__open(evsels[i], cpus, threads) < 0) {
95 			pr_debug("failed to open counter: %s, "
96 				 "tweak /proc/sys/kernel/perf_event_paranoid?\n",
97 				 str_error_r(errno, sbuf, sizeof(sbuf)));
98 			goto out_delete_evlist;
99 		}
100 
101 		nr_events[i] = 0;
102 		expected_nr_events[i] = 1 + rand() % 127;
103 	}
104 
105 	if (evlist__mmap(evlist, 128) < 0) {
106 		pr_debug("failed to mmap events: %d (%s)\n", errno,
107 			 str_error_r(errno, sbuf, sizeof(sbuf)));
108 		goto out_delete_evlist;
109 	}
110 
111 	for (i = 0; i < nsyscalls; ++i)
112 		for (j = 0; j < expected_nr_events[i]; ++j) {
113 			int foo = syscalls[i]();
114 			++foo;
115 		}
116 
117 	md = &evlist->mmap[0];
118 	if (perf_mmap__read_init(&md->core) < 0)
119 		goto out_init;
120 
121 	while ((event = perf_mmap__read_event(&md->core)) != NULL) {
122 		struct perf_sample sample;
123 
124 		if (event->header.type != PERF_RECORD_SAMPLE) {
125 			pr_debug("unexpected %s event\n",
126 				 perf_event__name(event->header.type));
127 			goto out_delete_evlist;
128 		}
129 
130 		err = evlist__parse_sample(evlist, event, &sample);
131 		if (err) {
132 			pr_err("Can't parse sample, err = %d\n", err);
133 			goto out_delete_evlist;
134 		}
135 
136 		err = -1;
137 		evsel = evlist__id2evsel(evlist, sample.id);
138 		if (evsel == NULL) {
139 			pr_debug("event with id %" PRIu64
140 				 " doesn't map to an evsel\n", sample.id);
141 			goto out_delete_evlist;
142 		}
143 		nr_events[evsel->core.idx]++;
144 		perf_mmap__consume(&md->core);
145 	}
146 	perf_mmap__read_done(&md->core);
147 
148 out_init:
149 	err = 0;
150 	evlist__for_each_entry(evlist, evsel) {
151 		if (nr_events[evsel->core.idx] != expected_nr_events[evsel->core.idx]) {
152 			pr_debug("expected %d %s events, got %d\n",
153 				 expected_nr_events[evsel->core.idx],
154 				 evsel__name(evsel), nr_events[evsel->core.idx]);
155 			err = -1;
156 			goto out_delete_evlist;
157 		}
158 	}
159 
160 out_delete_evlist:
161 	evlist__delete(evlist);
162 out_free_cpus:
163 	perf_cpu_map__put(cpus);
164 out_free_threads:
165 	perf_thread_map__put(threads);
166 	return err;
167 }
168 
169 DEFINE_SUITE("Read samples using the mmap interface", basic_mmap);
170