1 // SPDX-License-Identifier: GPL-2.0
2 #include <sys/time.h>
3 #include <sys/prctl.h>
4 #include <errno.h>
5 #include <limits.h>
6 #include <time.h>
7 #include <stdlib.h>
8 #include <linux/zalloc.h>
9 #include <perf/cpumap.h>
10 #include <perf/evlist.h>
11 #include <perf/mmap.h>
12 
13 #include "debug.h"
14 #include "parse-events.h"
15 #include "evlist.h"
16 #include "evsel.h"
17 #include "thread_map.h"
18 #include "record.h"
19 #include "tests.h"
20 #include "util/mmap.h"
21 #include "pmu.h"
22 
23 static int spin_sleep(void)
24 {
25 	struct timeval start, now, diff, maxtime;
26 	struct timespec ts;
27 	int err, i;
28 
29 	maxtime.tv_sec = 0;
30 	maxtime.tv_usec = 50000;
31 
32 	err = gettimeofday(&start, NULL);
33 	if (err)
34 		return err;
35 
36 	/* Spin for 50ms */
37 	while (1) {
38 		for (i = 0; i < 1000; i++)
39 			barrier();
40 
41 		err = gettimeofday(&now, NULL);
42 		if (err)
43 			return err;
44 
45 		timersub(&now, &start, &diff);
46 		if (timercmp(&diff, &maxtime, > /* For checkpatch */))
47 			break;
48 	}
49 
50 	ts.tv_nsec = 50 * 1000 * 1000;
51 	ts.tv_sec = 0;
52 
53 	/* Sleep for 50ms */
54 	err = nanosleep(&ts, NULL);
55 	if (err == EINTR)
56 		err = 0;
57 
58 	return err;
59 }
60 
61 struct switch_tracking {
62 	struct evsel *switch_evsel;
63 	struct evsel *cycles_evsel;
64 	pid_t *tids;
65 	int nr_tids;
66 	int comm_seen[4];
67 	int cycles_before_comm_1;
68 	int cycles_between_comm_2_and_comm_3;
69 	int cycles_after_comm_4;
70 };
71 
72 static int check_comm(struct switch_tracking *switch_tracking,
73 		      union perf_event *event, const char *comm, int nr)
74 {
75 	if (event->header.type == PERF_RECORD_COMM &&
76 	    (pid_t)event->comm.pid == getpid() &&
77 	    (pid_t)event->comm.tid == getpid() &&
78 	    strcmp(event->comm.comm, comm) == 0) {
79 		if (switch_tracking->comm_seen[nr]) {
80 			pr_debug("Duplicate comm event\n");
81 			return -1;
82 		}
83 		switch_tracking->comm_seen[nr] = 1;
84 		pr_debug3("comm event: %s nr: %d\n", event->comm.comm, nr);
85 		return 1;
86 	}
87 	return 0;
88 }
89 
90 static int check_cpu(struct switch_tracking *switch_tracking, int cpu)
91 {
92 	int i, nr = cpu + 1;
93 
94 	if (cpu < 0)
95 		return -1;
96 
97 	if (!switch_tracking->tids) {
98 		switch_tracking->tids = calloc(nr, sizeof(pid_t));
99 		if (!switch_tracking->tids)
100 			return -1;
101 		for (i = 0; i < nr; i++)
102 			switch_tracking->tids[i] = -1;
103 		switch_tracking->nr_tids = nr;
104 		return 0;
105 	}
106 
107 	if (cpu >= switch_tracking->nr_tids) {
108 		void *addr;
109 
110 		addr = realloc(switch_tracking->tids, nr * sizeof(pid_t));
111 		if (!addr)
112 			return -1;
113 		switch_tracking->tids = addr;
114 		for (i = switch_tracking->nr_tids; i < nr; i++)
115 			switch_tracking->tids[i] = -1;
116 		switch_tracking->nr_tids = nr;
117 		return 0;
118 	}
119 
120 	return 0;
121 }
122 
123 static int process_sample_event(struct evlist *evlist,
124 				union perf_event *event,
125 				struct switch_tracking *switch_tracking)
126 {
127 	struct perf_sample sample;
128 	struct evsel *evsel;
129 	pid_t next_tid, prev_tid;
130 	int cpu, err;
131 
132 	if (evlist__parse_sample(evlist, event, &sample)) {
133 		pr_debug("evlist__parse_sample failed\n");
134 		return -1;
135 	}
136 
137 	evsel = evlist__id2evsel(evlist, sample.id);
138 	if (evsel == switch_tracking->switch_evsel) {
139 		next_tid = evsel__intval(evsel, &sample, "next_pid");
140 		prev_tid = evsel__intval(evsel, &sample, "prev_pid");
141 		cpu = sample.cpu;
142 		pr_debug3("sched_switch: cpu: %d prev_tid %d next_tid %d\n",
143 			  cpu, prev_tid, next_tid);
144 		err = check_cpu(switch_tracking, cpu);
145 		if (err)
146 			return err;
147 		/*
148 		 * Check for no missing sched_switch events i.e. that the
149 		 * evsel->core.system_wide flag has worked.
150 		 */
151 		if (switch_tracking->tids[cpu] != -1 &&
152 		    switch_tracking->tids[cpu] != prev_tid) {
153 			pr_debug("Missing sched_switch events\n");
154 			return -1;
155 		}
156 		switch_tracking->tids[cpu] = next_tid;
157 	}
158 
159 	if (evsel == switch_tracking->cycles_evsel) {
160 		pr_debug3("cycles event\n");
161 		if (!switch_tracking->comm_seen[0])
162 			switch_tracking->cycles_before_comm_1 = 1;
163 		if (switch_tracking->comm_seen[1] &&
164 		    !switch_tracking->comm_seen[2])
165 			switch_tracking->cycles_between_comm_2_and_comm_3 = 1;
166 		if (switch_tracking->comm_seen[3])
167 			switch_tracking->cycles_after_comm_4 = 1;
168 	}
169 
170 	return 0;
171 }
172 
173 static int process_event(struct evlist *evlist, union perf_event *event,
174 			 struct switch_tracking *switch_tracking)
175 {
176 	if (event->header.type == PERF_RECORD_SAMPLE)
177 		return process_sample_event(evlist, event, switch_tracking);
178 
179 	if (event->header.type == PERF_RECORD_COMM) {
180 		int err, done = 0;
181 
182 		err = check_comm(switch_tracking, event, "Test COMM 1", 0);
183 		if (err < 0)
184 			return -1;
185 		done += err;
186 		err = check_comm(switch_tracking, event, "Test COMM 2", 1);
187 		if (err < 0)
188 			return -1;
189 		done += err;
190 		err = check_comm(switch_tracking, event, "Test COMM 3", 2);
191 		if (err < 0)
192 			return -1;
193 		done += err;
194 		err = check_comm(switch_tracking, event, "Test COMM 4", 3);
195 		if (err < 0)
196 			return -1;
197 		done += err;
198 		if (done != 1) {
199 			pr_debug("Unexpected comm event\n");
200 			return -1;
201 		}
202 	}
203 
204 	return 0;
205 }
206 
207 struct event_node {
208 	struct list_head list;
209 	union perf_event *event;
210 	u64 event_time;
211 };
212 
213 static int add_event(struct evlist *evlist, struct list_head *events,
214 		     union perf_event *event)
215 {
216 	struct perf_sample sample;
217 	struct event_node *node;
218 
219 	node = malloc(sizeof(struct event_node));
220 	if (!node) {
221 		pr_debug("malloc failed\n");
222 		return -1;
223 	}
224 	node->event = event;
225 	list_add(&node->list, events);
226 
227 	if (evlist__parse_sample(evlist, event, &sample)) {
228 		pr_debug("evlist__parse_sample failed\n");
229 		return -1;
230 	}
231 
232 	if (!sample.time) {
233 		pr_debug("event with no time\n");
234 		return -1;
235 	}
236 
237 	node->event_time = sample.time;
238 
239 	return 0;
240 }
241 
242 static void free_event_nodes(struct list_head *events)
243 {
244 	struct event_node *node;
245 
246 	while (!list_empty(events)) {
247 		node = list_entry(events->next, struct event_node, list);
248 		list_del_init(&node->list);
249 		free(node);
250 	}
251 }
252 
253 static int compar(const void *a, const void *b)
254 {
255 	const struct event_node *nodea = a;
256 	const struct event_node *nodeb = b;
257 	s64 cmp = nodea->event_time - nodeb->event_time;
258 
259 	return cmp;
260 }
261 
262 static int process_events(struct evlist *evlist,
263 			  struct switch_tracking *switch_tracking)
264 {
265 	union perf_event *event;
266 	unsigned pos, cnt = 0;
267 	LIST_HEAD(events);
268 	struct event_node *events_array, *node;
269 	struct mmap *md;
270 	int i, ret;
271 
272 	for (i = 0; i < evlist->core.nr_mmaps; i++) {
273 		md = &evlist->mmap[i];
274 		if (perf_mmap__read_init(&md->core) < 0)
275 			continue;
276 
277 		while ((event = perf_mmap__read_event(&md->core)) != NULL) {
278 			cnt += 1;
279 			ret = add_event(evlist, &events, event);
280 			 perf_mmap__consume(&md->core);
281 			if (ret < 0)
282 				goto out_free_nodes;
283 		}
284 		perf_mmap__read_done(&md->core);
285 	}
286 
287 	events_array = calloc(cnt, sizeof(struct event_node));
288 	if (!events_array) {
289 		pr_debug("calloc failed\n");
290 		ret = -1;
291 		goto out_free_nodes;
292 	}
293 
294 	pos = 0;
295 	list_for_each_entry(node, &events, list)
296 		events_array[pos++] = *node;
297 
298 	qsort(events_array, cnt, sizeof(struct event_node), compar);
299 
300 	for (pos = 0; pos < cnt; pos++) {
301 		ret = process_event(evlist, events_array[pos].event,
302 				    switch_tracking);
303 		if (ret < 0)
304 			goto out_free;
305 	}
306 
307 	ret = 0;
308 out_free:
309 	pr_debug("%u events recorded\n", cnt);
310 	free(events_array);
311 out_free_nodes:
312 	free_event_nodes(&events);
313 	return ret;
314 }
315 
316 /**
317  * test__switch_tracking - test using sched_switch and tracking events.
318  *
319  * This function implements a test that checks that sched_switch events and
320  * tracking events can be recorded for a workload (current process) using the
321  * evsel->core.system_wide and evsel->tracking flags (respectively) with other events
322  * sometimes enabled or disabled.
323  */
324 static int test__switch_tracking(struct test_suite *test __maybe_unused, int subtest __maybe_unused)
325 {
326 	const char *sched_switch = "sched:sched_switch";
327 	struct switch_tracking switch_tracking = { .tids = NULL, };
328 	struct record_opts opts = {
329 		.mmap_pages	     = UINT_MAX,
330 		.user_freq	     = UINT_MAX,
331 		.user_interval	     = ULLONG_MAX,
332 		.freq		     = 4000,
333 		.target		     = {
334 			.uses_mmap   = true,
335 		},
336 	};
337 	struct perf_thread_map *threads = NULL;
338 	struct perf_cpu_map *cpus = NULL;
339 	struct evlist *evlist = NULL;
340 	struct evsel *evsel, *cpu_clocks_evsel, *cycles_evsel;
341 	struct evsel *switch_evsel, *tracking_evsel;
342 	const char *comm;
343 	int err = -1;
344 
345 	threads = thread_map__new(-1, getpid(), UINT_MAX);
346 	if (!threads) {
347 		pr_debug("thread_map__new failed!\n");
348 		goto out_err;
349 	}
350 
351 	cpus = perf_cpu_map__new(NULL);
352 	if (!cpus) {
353 		pr_debug("perf_cpu_map__new failed!\n");
354 		goto out_err;
355 	}
356 
357 	evlist = evlist__new();
358 	if (!evlist) {
359 		pr_debug("evlist__new failed!\n");
360 		goto out_err;
361 	}
362 
363 	perf_evlist__set_maps(&evlist->core, cpus, threads);
364 
365 	/* First event */
366 	err = parse_events(evlist, "cpu-clock:u", NULL);
367 	if (err) {
368 		pr_debug("Failed to parse event dummy:u\n");
369 		goto out_err;
370 	}
371 
372 	cpu_clocks_evsel = evlist__last(evlist);
373 
374 	/* Second event */
375 	if (perf_pmu__has_hybrid())
376 		err = parse_events(evlist, "cpu_core/cycles/u", NULL);
377 	else
378 		err = parse_events(evlist, "cycles:u", NULL);
379 	if (err) {
380 		pr_debug("Failed to parse event cycles:u\n");
381 		goto out_err;
382 	}
383 
384 	cycles_evsel = evlist__last(evlist);
385 
386 	/* Third event */
387 	if (!evlist__can_select_event(evlist, sched_switch)) {
388 		pr_debug("No sched_switch\n");
389 		err = 0;
390 		goto out;
391 	}
392 
393 	err = parse_events(evlist, sched_switch, NULL);
394 	if (err) {
395 		pr_debug("Failed to parse event %s\n", sched_switch);
396 		goto out_err;
397 	}
398 
399 	switch_evsel = evlist__last(evlist);
400 
401 	evsel__set_sample_bit(switch_evsel, CPU);
402 	evsel__set_sample_bit(switch_evsel, TIME);
403 
404 	switch_evsel->core.system_wide = true;
405 	switch_evsel->no_aux_samples = true;
406 	switch_evsel->immediate = true;
407 
408 	/* Test moving an event to the front */
409 	if (cycles_evsel == evlist__first(evlist)) {
410 		pr_debug("cycles event already at front");
411 		goto out_err;
412 	}
413 	evlist__to_front(evlist, cycles_evsel);
414 	if (cycles_evsel != evlist__first(evlist)) {
415 		pr_debug("Failed to move cycles event to front");
416 		goto out_err;
417 	}
418 
419 	evsel__set_sample_bit(cycles_evsel, CPU);
420 	evsel__set_sample_bit(cycles_evsel, TIME);
421 
422 	/* Fourth event */
423 	err = parse_events(evlist, "dummy:u", NULL);
424 	if (err) {
425 		pr_debug("Failed to parse event dummy:u\n");
426 		goto out_err;
427 	}
428 
429 	tracking_evsel = evlist__last(evlist);
430 
431 	evlist__set_tracking_event(evlist, tracking_evsel);
432 
433 	tracking_evsel->core.attr.freq = 0;
434 	tracking_evsel->core.attr.sample_period = 1;
435 
436 	evsel__set_sample_bit(tracking_evsel, TIME);
437 
438 	/* Config events */
439 	evlist__config(evlist, &opts, NULL);
440 
441 	/* Check moved event is still at the front */
442 	if (cycles_evsel != evlist__first(evlist)) {
443 		pr_debug("Front event no longer at front");
444 		goto out_err;
445 	}
446 
447 	/* Check tracking event is tracking */
448 	if (!tracking_evsel->core.attr.mmap || !tracking_evsel->core.attr.comm) {
449 		pr_debug("Tracking event not tracking\n");
450 		goto out_err;
451 	}
452 
453 	/* Check non-tracking events are not tracking */
454 	evlist__for_each_entry(evlist, evsel) {
455 		if (evsel != tracking_evsel) {
456 			if (evsel->core.attr.mmap || evsel->core.attr.comm) {
457 				pr_debug("Non-tracking event is tracking\n");
458 				goto out_err;
459 			}
460 		}
461 	}
462 
463 	if (evlist__open(evlist) < 0) {
464 		pr_debug("Not supported\n");
465 		err = 0;
466 		goto out;
467 	}
468 
469 	err = evlist__mmap(evlist, UINT_MAX);
470 	if (err) {
471 		pr_debug("evlist__mmap failed!\n");
472 		goto out_err;
473 	}
474 
475 	evlist__enable(evlist);
476 
477 	err = evsel__disable(cpu_clocks_evsel);
478 	if (err) {
479 		pr_debug("perf_evlist__disable_event failed!\n");
480 		goto out_err;
481 	}
482 
483 	err = spin_sleep();
484 	if (err) {
485 		pr_debug("spin_sleep failed!\n");
486 		goto out_err;
487 	}
488 
489 	comm = "Test COMM 1";
490 	err = prctl(PR_SET_NAME, (unsigned long)comm, 0, 0, 0);
491 	if (err) {
492 		pr_debug("PR_SET_NAME failed!\n");
493 		goto out_err;
494 	}
495 
496 	err = evsel__disable(cycles_evsel);
497 	if (err) {
498 		pr_debug("perf_evlist__disable_event failed!\n");
499 		goto out_err;
500 	}
501 
502 	comm = "Test COMM 2";
503 	err = prctl(PR_SET_NAME, (unsigned long)comm, 0, 0, 0);
504 	if (err) {
505 		pr_debug("PR_SET_NAME failed!\n");
506 		goto out_err;
507 	}
508 
509 	err = spin_sleep();
510 	if (err) {
511 		pr_debug("spin_sleep failed!\n");
512 		goto out_err;
513 	}
514 
515 	comm = "Test COMM 3";
516 	err = prctl(PR_SET_NAME, (unsigned long)comm, 0, 0, 0);
517 	if (err) {
518 		pr_debug("PR_SET_NAME failed!\n");
519 		goto out_err;
520 	}
521 
522 	err = evsel__enable(cycles_evsel);
523 	if (err) {
524 		pr_debug("perf_evlist__disable_event failed!\n");
525 		goto out_err;
526 	}
527 
528 	comm = "Test COMM 4";
529 	err = prctl(PR_SET_NAME, (unsigned long)comm, 0, 0, 0);
530 	if (err) {
531 		pr_debug("PR_SET_NAME failed!\n");
532 		goto out_err;
533 	}
534 
535 	err = spin_sleep();
536 	if (err) {
537 		pr_debug("spin_sleep failed!\n");
538 		goto out_err;
539 	}
540 
541 	evlist__disable(evlist);
542 
543 	switch_tracking.switch_evsel = switch_evsel;
544 	switch_tracking.cycles_evsel = cycles_evsel;
545 
546 	err = process_events(evlist, &switch_tracking);
547 
548 	zfree(&switch_tracking.tids);
549 
550 	if (err)
551 		goto out_err;
552 
553 	/* Check all 4 comm events were seen i.e. that evsel->tracking works */
554 	if (!switch_tracking.comm_seen[0] || !switch_tracking.comm_seen[1] ||
555 	    !switch_tracking.comm_seen[2] || !switch_tracking.comm_seen[3]) {
556 		pr_debug("Missing comm events\n");
557 		goto out_err;
558 	}
559 
560 	/* Check cycles event got enabled */
561 	if (!switch_tracking.cycles_before_comm_1) {
562 		pr_debug("Missing cycles events\n");
563 		goto out_err;
564 	}
565 
566 	/* Check cycles event got disabled */
567 	if (switch_tracking.cycles_between_comm_2_and_comm_3) {
568 		pr_debug("cycles events even though event was disabled\n");
569 		goto out_err;
570 	}
571 
572 	/* Check cycles event got enabled again */
573 	if (!switch_tracking.cycles_after_comm_4) {
574 		pr_debug("Missing cycles events\n");
575 		goto out_err;
576 	}
577 out:
578 	if (evlist) {
579 		evlist__disable(evlist);
580 		evlist__delete(evlist);
581 	}
582 	perf_cpu_map__put(cpus);
583 	perf_thread_map__put(threads);
584 
585 	return err;
586 
587 out_err:
588 	err = -1;
589 	goto out;
590 }
591 
592 DEFINE_SUITE("Track with sched_switch", switch_tracking);
593