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