xref: /linux/tools/perf/tests/mmap-basic.c (revision 9a379e77033f02c4a071891afdf0f0a01eff8ccb)
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 
7 #include "evlist.h"
8 #include "evsel.h"
9 #include "thread_map.h"
10 #include "cpumap.h"
11 #include "tests.h"
12 #include <linux/err.h>
13 #include <linux/kernel.h>
14 
15 /*
16  * This test will generate random numbers of calls to some getpid syscalls,
17  * then establish an mmap for a group of events that are created to monitor
18  * the syscalls.
19  *
20  * It will receive the events, using mmap, use its PERF_SAMPLE_ID generated
21  * sample.id field to map back to its respective perf_evsel instance.
22  *
23  * Then it checks if the number of syscalls reported as perf events by
24  * the kernel corresponds to the number of syscalls made.
25  */
26 int test__basic_mmap(struct test *test __maybe_unused, int subtest __maybe_unused)
27 {
28 	int err = -1;
29 	union perf_event *event;
30 	struct thread_map *threads;
31 	struct cpu_map *cpus;
32 	struct perf_evlist *evlist;
33 	cpu_set_t cpu_set;
34 	const char *syscall_names[] = { "getsid", "getppid", "getpgid", };
35 	pid_t (*syscalls[])(void) = { (void *)getsid, getppid, (void*)getpgid };
36 #define nsyscalls ARRAY_SIZE(syscall_names)
37 	unsigned int nr_events[nsyscalls],
38 		     expected_nr_events[nsyscalls], i, j;
39 	struct perf_evsel *evsels[nsyscalls], *evsel;
40 	char sbuf[STRERR_BUFSIZE];
41 
42 	threads = thread_map__new(-1, getpid(), UINT_MAX);
43 	if (threads == NULL) {
44 		pr_debug("thread_map__new\n");
45 		return -1;
46 	}
47 
48 	cpus = cpu_map__new(NULL);
49 	if (cpus == NULL) {
50 		pr_debug("cpu_map__new\n");
51 		goto out_free_threads;
52 	}
53 
54 	CPU_ZERO(&cpu_set);
55 	CPU_SET(cpus->map[0], &cpu_set);
56 	sched_setaffinity(0, sizeof(cpu_set), &cpu_set);
57 	if (sched_setaffinity(0, sizeof(cpu_set), &cpu_set) < 0) {
58 		pr_debug("sched_setaffinity() failed on CPU %d: %s ",
59 			 cpus->map[0], str_error_r(errno, sbuf, sizeof(sbuf)));
60 		goto out_free_cpus;
61 	}
62 
63 	evlist = perf_evlist__new();
64 	if (evlist == NULL) {
65 		pr_debug("perf_evlist__new\n");
66 		goto out_free_cpus;
67 	}
68 
69 	perf_evlist__set_maps(evlist, cpus, threads);
70 
71 	for (i = 0; i < nsyscalls; ++i) {
72 		char name[64];
73 
74 		snprintf(name, sizeof(name), "sys_enter_%s", syscall_names[i]);
75 		evsels[i] = perf_evsel__newtp("syscalls", name);
76 		if (IS_ERR(evsels[i])) {
77 			pr_debug("perf_evsel__new\n");
78 			goto out_delete_evlist;
79 		}
80 
81 		evsels[i]->attr.wakeup_events = 1;
82 		perf_evsel__set_sample_id(evsels[i], false);
83 
84 		perf_evlist__add(evlist, evsels[i]);
85 
86 		if (perf_evsel__open(evsels[i], cpus, threads) < 0) {
87 			pr_debug("failed to open counter: %s, "
88 				 "tweak /proc/sys/kernel/perf_event_paranoid?\n",
89 				 str_error_r(errno, sbuf, sizeof(sbuf)));
90 			goto out_delete_evlist;
91 		}
92 
93 		nr_events[i] = 0;
94 		expected_nr_events[i] = 1 + rand() % 127;
95 	}
96 
97 	if (perf_evlist__mmap(evlist, 128) < 0) {
98 		pr_debug("failed to mmap events: %d (%s)\n", errno,
99 			 str_error_r(errno, sbuf, sizeof(sbuf)));
100 		goto out_delete_evlist;
101 	}
102 
103 	for (i = 0; i < nsyscalls; ++i)
104 		for (j = 0; j < expected_nr_events[i]; ++j) {
105 			int foo = syscalls[i]();
106 			++foo;
107 		}
108 
109 	while ((event = perf_evlist__mmap_read(evlist, 0)) != NULL) {
110 		struct perf_sample sample;
111 
112 		if (event->header.type != PERF_RECORD_SAMPLE) {
113 			pr_debug("unexpected %s event\n",
114 				 perf_event__name(event->header.type));
115 			goto out_delete_evlist;
116 		}
117 
118 		err = perf_evlist__parse_sample(evlist, event, &sample);
119 		if (err) {
120 			pr_err("Can't parse sample, err = %d\n", err);
121 			goto out_delete_evlist;
122 		}
123 
124 		err = -1;
125 		evsel = perf_evlist__id2evsel(evlist, sample.id);
126 		if (evsel == NULL) {
127 			pr_debug("event with id %" PRIu64
128 				 " doesn't map to an evsel\n", sample.id);
129 			goto out_delete_evlist;
130 		}
131 		nr_events[evsel->idx]++;
132 		perf_evlist__mmap_consume(evlist, 0);
133 	}
134 
135 	err = 0;
136 	evlist__for_each_entry(evlist, evsel) {
137 		if (nr_events[evsel->idx] != expected_nr_events[evsel->idx]) {
138 			pr_debug("expected %d %s events, got %d\n",
139 				 expected_nr_events[evsel->idx],
140 				 perf_evsel__name(evsel), nr_events[evsel->idx]);
141 			err = -1;
142 			goto out_delete_evlist;
143 		}
144 	}
145 
146 out_delete_evlist:
147 	perf_evlist__delete(evlist);
148 	cpus	= NULL;
149 	threads = NULL;
150 out_free_cpus:
151 	cpu_map__put(cpus);
152 out_free_threads:
153 	thread_map__put(threads);
154 	return err;
155 }
156