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