1 #include <sched.h>
2 #include "evlist.h"
3 #include "evsel.h"
4 #include "perf.h"
5 #include "debug.h"
6 #include "tests.h"
7 
sched__get_first_possible_cpu(pid_t pid,cpu_set_t * maskp)8 static int sched__get_first_possible_cpu(pid_t pid, cpu_set_t *maskp)
9 {
10 	int i, cpu = -1, nrcpus = 1024;
11 realloc:
12 	CPU_ZERO(maskp);
13 
14 	if (sched_getaffinity(pid, sizeof(*maskp), maskp) == -1) {
15 		if (errno == EINVAL && nrcpus < (1024 << 8)) {
16 			nrcpus = nrcpus << 2;
17 			goto realloc;
18 		}
19 		perror("sched_getaffinity");
20 			return -1;
21 	}
22 
23 	for (i = 0; i < nrcpus; i++) {
24 		if (CPU_ISSET(i, maskp)) {
25 			if (cpu == -1)
26 				cpu = i;
27 			else
28 				CPU_CLR(i, maskp);
29 		}
30 	}
31 
32 	return cpu;
33 }
34 
test__PERF_RECORD(void)35 int test__PERF_RECORD(void)
36 {
37 	struct perf_record_opts opts = {
38 		.target = {
39 			.uid = UINT_MAX,
40 			.uses_mmap = true,
41 		},
42 		.no_delay   = true,
43 		.freq	    = 10,
44 		.mmap_pages = 256,
45 	};
46 	cpu_set_t cpu_mask;
47 	size_t cpu_mask_size = sizeof(cpu_mask);
48 	struct perf_evlist *evlist = perf_evlist__new();
49 	struct perf_evsel *evsel;
50 	struct perf_sample sample;
51 	const char *cmd = "sleep";
52 	const char *argv[] = { cmd, "1", NULL, };
53 	char *bname, *mmap_filename;
54 	u64 prev_time = 0;
55 	bool found_cmd_mmap = false,
56 	     found_libc_mmap = false,
57 	     found_vdso_mmap = false,
58 	     found_ld_mmap = false;
59 	int err = -1, errs = 0, i, wakeups = 0;
60 	u32 cpu;
61 	int total_events = 0, nr_events[PERF_RECORD_MAX] = { 0, };
62 
63 	if (evlist == NULL || argv == NULL) {
64 		pr_debug("Not enough memory to create evlist\n");
65 		goto out;
66 	}
67 
68 	/*
69 	 * We need at least one evsel in the evlist, use the default
70 	 * one: "cycles".
71 	 */
72 	err = perf_evlist__add_default(evlist);
73 	if (err < 0) {
74 		pr_debug("Not enough memory to create evsel\n");
75 		goto out_delete_evlist;
76 	}
77 
78 	/*
79 	 * Create maps of threads and cpus to monitor. In this case
80 	 * we start with all threads and cpus (-1, -1) but then in
81 	 * perf_evlist__prepare_workload we'll fill in the only thread
82 	 * we're monitoring, the one forked there.
83 	 */
84 	err = perf_evlist__create_maps(evlist, &opts.target);
85 	if (err < 0) {
86 		pr_debug("Not enough memory to create thread/cpu maps\n");
87 		goto out_delete_evlist;
88 	}
89 
90 	/*
91 	 * Prepare the workload in argv[] to run, it'll fork it, and then wait
92 	 * for perf_evlist__start_workload() to exec it. This is done this way
93 	 * so that we have time to open the evlist (calling sys_perf_event_open
94 	 * on all the fds) and then mmap them.
95 	 */
96 	err = perf_evlist__prepare_workload(evlist, &opts.target, argv,
97 					    false, false);
98 	if (err < 0) {
99 		pr_debug("Couldn't run the workload!\n");
100 		goto out_delete_maps;
101 	}
102 
103 	/*
104 	 * Config the evsels, setting attr->comm on the first one, etc.
105 	 */
106 	evsel = perf_evlist__first(evlist);
107 	perf_evsel__set_sample_bit(evsel, CPU);
108 	perf_evsel__set_sample_bit(evsel, TID);
109 	perf_evsel__set_sample_bit(evsel, TIME);
110 	perf_evlist__config(evlist, &opts);
111 
112 	err = sched__get_first_possible_cpu(evlist->workload.pid, &cpu_mask);
113 	if (err < 0) {
114 		pr_debug("sched__get_first_possible_cpu: %s\n", strerror(errno));
115 		goto out_delete_maps;
116 	}
117 
118 	cpu = err;
119 
120 	/*
121 	 * So that we can check perf_sample.cpu on all the samples.
122 	 */
123 	if (sched_setaffinity(evlist->workload.pid, cpu_mask_size, &cpu_mask) < 0) {
124 		pr_debug("sched_setaffinity: %s\n", strerror(errno));
125 		goto out_delete_maps;
126 	}
127 
128 	/*
129 	 * Call sys_perf_event_open on all the fds on all the evsels,
130 	 * grouping them if asked to.
131 	 */
132 	err = perf_evlist__open(evlist);
133 	if (err < 0) {
134 		pr_debug("perf_evlist__open: %s\n", strerror(errno));
135 		goto out_delete_maps;
136 	}
137 
138 	/*
139 	 * mmap the first fd on a given CPU and ask for events for the other
140 	 * fds in the same CPU to be injected in the same mmap ring buffer
141 	 * (using ioctl(PERF_EVENT_IOC_SET_OUTPUT)).
142 	 */
143 	err = perf_evlist__mmap(evlist, opts.mmap_pages, false);
144 	if (err < 0) {
145 		pr_debug("perf_evlist__mmap: %s\n", strerror(errno));
146 		goto out_close_evlist;
147 	}
148 
149 	/*
150 	 * Now that all is properly set up, enable the events, they will
151 	 * count just on workload.pid, which will start...
152 	 */
153 	perf_evlist__enable(evlist);
154 
155 	/*
156 	 * Now!
157 	 */
158 	perf_evlist__start_workload(evlist);
159 
160 	while (1) {
161 		int before = total_events;
162 
163 		for (i = 0; i < evlist->nr_mmaps; i++) {
164 			union perf_event *event;
165 
166 			while ((event = perf_evlist__mmap_read(evlist, i)) != NULL) {
167 				const u32 type = event->header.type;
168 				const char *name = perf_event__name(type);
169 
170 				++total_events;
171 				if (type < PERF_RECORD_MAX)
172 					nr_events[type]++;
173 
174 				err = perf_evlist__parse_sample(evlist, event, &sample);
175 				if (err < 0) {
176 					if (verbose)
177 						perf_event__fprintf(event, stderr);
178 					pr_debug("Couldn't parse sample\n");
179 					goto out_err;
180 				}
181 
182 				if (verbose) {
183 					pr_info("%" PRIu64" %d ", sample.time, sample.cpu);
184 					perf_event__fprintf(event, stderr);
185 				}
186 
187 				if (prev_time > sample.time) {
188 					pr_debug("%s going backwards in time, prev=%" PRIu64 ", curr=%" PRIu64 "\n",
189 						 name, prev_time, sample.time);
190 					++errs;
191 				}
192 
193 				prev_time = sample.time;
194 
195 				if (sample.cpu != cpu) {
196 					pr_debug("%s with unexpected cpu, expected %d, got %d\n",
197 						 name, cpu, sample.cpu);
198 					++errs;
199 				}
200 
201 				if ((pid_t)sample.pid != evlist->workload.pid) {
202 					pr_debug("%s with unexpected pid, expected %d, got %d\n",
203 						 name, evlist->workload.pid, sample.pid);
204 					++errs;
205 				}
206 
207 				if ((pid_t)sample.tid != evlist->workload.pid) {
208 					pr_debug("%s with unexpected tid, expected %d, got %d\n",
209 						 name, evlist->workload.pid, sample.tid);
210 					++errs;
211 				}
212 
213 				if ((type == PERF_RECORD_COMM ||
214 				     type == PERF_RECORD_MMAP ||
215 				     type == PERF_RECORD_MMAP2 ||
216 				     type == PERF_RECORD_FORK ||
217 				     type == PERF_RECORD_EXIT) &&
218 				     (pid_t)event->comm.pid != evlist->workload.pid) {
219 					pr_debug("%s with unexpected pid/tid\n", name);
220 					++errs;
221 				}
222 
223 				if ((type == PERF_RECORD_COMM ||
224 				     type == PERF_RECORD_MMAP ||
225 				     type == PERF_RECORD_MMAP2) &&
226 				     event->comm.pid != event->comm.tid) {
227 					pr_debug("%s with different pid/tid!\n", name);
228 					++errs;
229 				}
230 
231 				switch (type) {
232 				case PERF_RECORD_COMM:
233 					if (strcmp(event->comm.comm, cmd)) {
234 						pr_debug("%s with unexpected comm!\n", name);
235 						++errs;
236 					}
237 					break;
238 				case PERF_RECORD_EXIT:
239 					goto found_exit;
240 				case PERF_RECORD_MMAP:
241 					mmap_filename = event->mmap.filename;
242 					goto check_bname;
243 				case PERF_RECORD_MMAP2:
244 					mmap_filename = event->mmap2.filename;
245 				check_bname:
246 					bname = strrchr(mmap_filename, '/');
247 					if (bname != NULL) {
248 						if (!found_cmd_mmap)
249 							found_cmd_mmap = !strcmp(bname + 1, cmd);
250 						if (!found_libc_mmap)
251 							found_libc_mmap = !strncmp(bname + 1, "libc", 4);
252 						if (!found_ld_mmap)
253 							found_ld_mmap = !strncmp(bname + 1, "ld", 2);
254 					} else if (!found_vdso_mmap)
255 						found_vdso_mmap = !strcmp(mmap_filename, "[vdso]");
256 					break;
257 
258 				case PERF_RECORD_SAMPLE:
259 					/* Just ignore samples for now */
260 					break;
261 				default:
262 					pr_debug("Unexpected perf_event->header.type %d!\n",
263 						 type);
264 					++errs;
265 				}
266 
267 				perf_evlist__mmap_consume(evlist, i);
268 			}
269 		}
270 
271 		/*
272 		 * We don't use poll here because at least at 3.1 times the
273 		 * PERF_RECORD_{!SAMPLE} events don't honour
274 		 * perf_event_attr.wakeup_events, just PERF_EVENT_SAMPLE does.
275 		 */
276 		if (total_events == before && false)
277 			poll(evlist->pollfd, evlist->nr_fds, -1);
278 
279 		sleep(1);
280 		if (++wakeups > 5) {
281 			pr_debug("No PERF_RECORD_EXIT event!\n");
282 			break;
283 		}
284 	}
285 
286 found_exit:
287 	if (nr_events[PERF_RECORD_COMM] > 1) {
288 		pr_debug("Excessive number of PERF_RECORD_COMM events!\n");
289 		++errs;
290 	}
291 
292 	if (nr_events[PERF_RECORD_COMM] == 0) {
293 		pr_debug("Missing PERF_RECORD_COMM for %s!\n", cmd);
294 		++errs;
295 	}
296 
297 	if (!found_cmd_mmap) {
298 		pr_debug("PERF_RECORD_MMAP for %s missing!\n", cmd);
299 		++errs;
300 	}
301 
302 	if (!found_libc_mmap) {
303 		pr_debug("PERF_RECORD_MMAP for %s missing!\n", "libc");
304 		++errs;
305 	}
306 
307 	if (!found_ld_mmap) {
308 		pr_debug("PERF_RECORD_MMAP for %s missing!\n", "ld");
309 		++errs;
310 	}
311 
312 	if (!found_vdso_mmap) {
313 		pr_debug("PERF_RECORD_MMAP for %s missing!\n", "[vdso]");
314 		++errs;
315 	}
316 out_err:
317 	perf_evlist__munmap(evlist);
318 out_close_evlist:
319 	perf_evlist__close(evlist);
320 out_delete_maps:
321 	perf_evlist__delete_maps(evlist);
322 out_delete_evlist:
323 	perf_evlist__delete(evlist);
324 out:
325 	return (err < 0 || errs > 0) ? -1 : 0;
326 }
327