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 	const char *cycles = "cycles:u";
328 	struct switch_tracking switch_tracking = { .tids = NULL, };
329 	struct record_opts opts = {
330 		.mmap_pages	     = UINT_MAX,
331 		.user_freq	     = UINT_MAX,
332 		.user_interval	     = ULLONG_MAX,
333 		.freq		     = 4000,
334 		.target		     = {
335 			.uses_mmap   = true,
336 		},
337 	};
338 	struct perf_thread_map *threads = NULL;
339 	struct perf_cpu_map *cpus = NULL;
340 	struct evlist *evlist = NULL;
341 	struct evsel *evsel, *cpu_clocks_evsel, *cycles_evsel;
342 	struct evsel *switch_evsel, *tracking_evsel;
343 	const char *comm;
344 	int err = -1;
345 
346 	threads = thread_map__new(-1, getpid(), UINT_MAX);
347 	if (!threads) {
348 		pr_debug("thread_map__new failed!\n");
349 		goto out_err;
350 	}
351 
352 	cpus = perf_cpu_map__new(NULL);
353 	if (!cpus) {
354 		pr_debug("perf_cpu_map__new failed!\n");
355 		goto out_err;
356 	}
357 
358 	evlist = evlist__new();
359 	if (!evlist) {
360 		pr_debug("evlist__new failed!\n");
361 		goto out_err;
362 	}
363 
364 	perf_evlist__set_maps(&evlist->core, cpus, threads);
365 
366 	/* First event */
367 	err = parse_event(evlist, "cpu-clock:u");
368 	if (err) {
369 		pr_debug("Failed to parse event dummy:u\n");
370 		goto out_err;
371 	}
372 
373 	cpu_clocks_evsel = evlist__last(evlist);
374 
375 	/* Second event */
376 	if (perf_pmu__has_hybrid()) {
377 		cycles = "cpu_core/cycles/u";
378 		err = parse_event(evlist, cycles);
379 		if (err) {
380 			cycles = "cpu_atom/cycles/u";
381 			pr_debug("Trying %s\n", cycles);
382 			err = parse_event(evlist, cycles);
383 		}
384 	} else {
385 		err = parse_event(evlist, cycles);
386 	}
387 	if (err) {
388 		pr_debug("Failed to parse event %s\n", cycles);
389 		goto out_err;
390 	}
391 
392 	cycles_evsel = evlist__last(evlist);
393 
394 	/* Third event */
395 	if (!evlist__can_select_event(evlist, sched_switch)) {
396 		pr_debug("No sched_switch\n");
397 		err = 0;
398 		goto out;
399 	}
400 
401 	err = parse_event(evlist, sched_switch);
402 	if (err) {
403 		pr_debug("Failed to parse event %s\n", sched_switch);
404 		goto out_err;
405 	}
406 
407 	switch_evsel = evlist__last(evlist);
408 
409 	evsel__set_sample_bit(switch_evsel, CPU);
410 	evsel__set_sample_bit(switch_evsel, TIME);
411 
412 	switch_evsel->core.system_wide = true;
413 	switch_evsel->no_aux_samples = true;
414 	switch_evsel->immediate = true;
415 
416 	/* Test moving an event to the front */
417 	if (cycles_evsel == evlist__first(evlist)) {
418 		pr_debug("cycles event already at front");
419 		goto out_err;
420 	}
421 	evlist__to_front(evlist, cycles_evsel);
422 	if (cycles_evsel != evlist__first(evlist)) {
423 		pr_debug("Failed to move cycles event to front");
424 		goto out_err;
425 	}
426 
427 	evsel__set_sample_bit(cycles_evsel, CPU);
428 	evsel__set_sample_bit(cycles_evsel, TIME);
429 
430 	/* Fourth event */
431 	err = parse_event(evlist, "dummy:u");
432 	if (err) {
433 		pr_debug("Failed to parse event dummy:u\n");
434 		goto out_err;
435 	}
436 
437 	tracking_evsel = evlist__last(evlist);
438 
439 	evlist__set_tracking_event(evlist, tracking_evsel);
440 
441 	tracking_evsel->core.attr.freq = 0;
442 	tracking_evsel->core.attr.sample_period = 1;
443 
444 	evsel__set_sample_bit(tracking_evsel, TIME);
445 
446 	/* Config events */
447 	evlist__config(evlist, &opts, NULL);
448 
449 	/* Check moved event is still at the front */
450 	if (cycles_evsel != evlist__first(evlist)) {
451 		pr_debug("Front event no longer at front");
452 		goto out_err;
453 	}
454 
455 	/* Check tracking event is tracking */
456 	if (!tracking_evsel->core.attr.mmap || !tracking_evsel->core.attr.comm) {
457 		pr_debug("Tracking event not tracking\n");
458 		goto out_err;
459 	}
460 
461 	/* Check non-tracking events are not tracking */
462 	evlist__for_each_entry(evlist, evsel) {
463 		if (evsel != tracking_evsel) {
464 			if (evsel->core.attr.mmap || evsel->core.attr.comm) {
465 				pr_debug("Non-tracking event is tracking\n");
466 				goto out_err;
467 			}
468 		}
469 	}
470 
471 	if (evlist__open(evlist) < 0) {
472 		pr_debug("Not supported\n");
473 		err = 0;
474 		goto out;
475 	}
476 
477 	err = evlist__mmap(evlist, UINT_MAX);
478 	if (err) {
479 		pr_debug("evlist__mmap failed!\n");
480 		goto out_err;
481 	}
482 
483 	evlist__enable(evlist);
484 
485 	err = evsel__disable(cpu_clocks_evsel);
486 	if (err) {
487 		pr_debug("perf_evlist__disable_event failed!\n");
488 		goto out_err;
489 	}
490 
491 	err = spin_sleep();
492 	if (err) {
493 		pr_debug("spin_sleep failed!\n");
494 		goto out_err;
495 	}
496 
497 	comm = "Test COMM 1";
498 	err = prctl(PR_SET_NAME, (unsigned long)comm, 0, 0, 0);
499 	if (err) {
500 		pr_debug("PR_SET_NAME failed!\n");
501 		goto out_err;
502 	}
503 
504 	err = evsel__disable(cycles_evsel);
505 	if (err) {
506 		pr_debug("perf_evlist__disable_event failed!\n");
507 		goto out_err;
508 	}
509 
510 	comm = "Test COMM 2";
511 	err = prctl(PR_SET_NAME, (unsigned long)comm, 0, 0, 0);
512 	if (err) {
513 		pr_debug("PR_SET_NAME failed!\n");
514 		goto out_err;
515 	}
516 
517 	err = spin_sleep();
518 	if (err) {
519 		pr_debug("spin_sleep failed!\n");
520 		goto out_err;
521 	}
522 
523 	comm = "Test COMM 3";
524 	err = prctl(PR_SET_NAME, (unsigned long)comm, 0, 0, 0);
525 	if (err) {
526 		pr_debug("PR_SET_NAME failed!\n");
527 		goto out_err;
528 	}
529 
530 	err = evsel__enable(cycles_evsel);
531 	if (err) {
532 		pr_debug("perf_evlist__disable_event failed!\n");
533 		goto out_err;
534 	}
535 
536 	comm = "Test COMM 4";
537 	err = prctl(PR_SET_NAME, (unsigned long)comm, 0, 0, 0);
538 	if (err) {
539 		pr_debug("PR_SET_NAME failed!\n");
540 		goto out_err;
541 	}
542 
543 	err = spin_sleep();
544 	if (err) {
545 		pr_debug("spin_sleep failed!\n");
546 		goto out_err;
547 	}
548 
549 	evlist__disable(evlist);
550 
551 	switch_tracking.switch_evsel = switch_evsel;
552 	switch_tracking.cycles_evsel = cycles_evsel;
553 
554 	err = process_events(evlist, &switch_tracking);
555 
556 	zfree(&switch_tracking.tids);
557 
558 	if (err)
559 		goto out_err;
560 
561 	/* Check all 4 comm events were seen i.e. that evsel->tracking works */
562 	if (!switch_tracking.comm_seen[0] || !switch_tracking.comm_seen[1] ||
563 	    !switch_tracking.comm_seen[2] || !switch_tracking.comm_seen[3]) {
564 		pr_debug("Missing comm events\n");
565 		goto out_err;
566 	}
567 
568 	/* Check cycles event got enabled */
569 	if (!switch_tracking.cycles_before_comm_1) {
570 		pr_debug("Missing cycles events\n");
571 		goto out_err;
572 	}
573 
574 	/* Check cycles event got disabled */
575 	if (switch_tracking.cycles_between_comm_2_and_comm_3) {
576 		pr_debug("cycles events even though event was disabled\n");
577 		goto out_err;
578 	}
579 
580 	/* Check cycles event got enabled again */
581 	if (!switch_tracking.cycles_after_comm_4) {
582 		pr_debug("Missing cycles events\n");
583 		goto out_err;
584 	}
585 out:
586 	if (evlist) {
587 		evlist__disable(evlist);
588 		evlist__delete(evlist);
589 	}
590 	perf_cpu_map__put(cpus);
591 	perf_thread_map__put(threads);
592 
593 	return err;
594 
595 out_err:
596 	err = -1;
597 	goto out;
598 }
599 
600 DEFINE_SUITE("Track with sched_switch", switch_tracking);
601