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