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