1 // SPDX-License-Identifier: GPL-2.0
8 #include "parse-events.h"
11 #include "thread_map.h"
15 static int spin_sleep(void)
17 struct timeval start, now, diff, maxtime;
22 maxtime.tv_usec = 50000;
24 err = gettimeofday(&start, NULL);
30 for (i = 0; i < 1000; i++)
33 err = gettimeofday(&now, NULL);
37 timersub(&now, &start, &diff);
38 if (timercmp(&diff, &maxtime, > /* For checkpatch */))
42 ts.tv_nsec = 50 * 1000 * 1000;
46 err = nanosleep(&ts, NULL);
53 struct switch_tracking {
54 struct perf_evsel *switch_evsel;
55 struct perf_evsel *cycles_evsel;
59 int cycles_before_comm_1;
60 int cycles_between_comm_2_and_comm_3;
61 int cycles_after_comm_4;
64 static int check_comm(struct switch_tracking *switch_tracking,
65 union perf_event *event, const char *comm, int nr)
67 if (event->header.type == PERF_RECORD_COMM &&
68 (pid_t)event->comm.pid == getpid() &&
69 (pid_t)event->comm.tid == getpid() &&
70 strcmp(event->comm.comm, comm) == 0) {
71 if (switch_tracking->comm_seen[nr]) {
72 pr_debug("Duplicate comm event\n");
75 switch_tracking->comm_seen[nr] = 1;
76 pr_debug3("comm event: %s nr: %d\n", event->comm.comm, nr);
82 static int check_cpu(struct switch_tracking *switch_tracking, int cpu)
89 if (!switch_tracking->tids) {
90 switch_tracking->tids = calloc(nr, sizeof(pid_t));
91 if (!switch_tracking->tids)
93 for (i = 0; i < nr; i++)
94 switch_tracking->tids[i] = -1;
95 switch_tracking->nr_tids = nr;
99 if (cpu >= switch_tracking->nr_tids) {
102 addr = realloc(switch_tracking->tids, nr * sizeof(pid_t));
105 switch_tracking->tids = addr;
106 for (i = switch_tracking->nr_tids; i < nr; i++)
107 switch_tracking->tids[i] = -1;
108 switch_tracking->nr_tids = nr;
115 static int process_sample_event(struct perf_evlist *evlist,
116 union perf_event *event,
117 struct switch_tracking *switch_tracking)
119 struct perf_sample sample;
120 struct perf_evsel *evsel;
121 pid_t next_tid, prev_tid;
124 if (perf_evlist__parse_sample(evlist, event, &sample)) {
125 pr_debug("perf_evlist__parse_sample failed\n");
129 evsel = perf_evlist__id2evsel(evlist, sample.id);
130 if (evsel == switch_tracking->switch_evsel) {
131 next_tid = perf_evsel__intval(evsel, &sample, "next_pid");
132 prev_tid = perf_evsel__intval(evsel, &sample, "prev_pid");
134 pr_debug3("sched_switch: cpu: %d prev_tid %d next_tid %d\n",
135 cpu, prev_tid, next_tid);
136 err = check_cpu(switch_tracking, cpu);
140 * Check for no missing sched_switch events i.e. that the
141 * evsel->system_wide flag has worked.
143 if (switch_tracking->tids[cpu] != -1 &&
144 switch_tracking->tids[cpu] != prev_tid) {
145 pr_debug("Missing sched_switch events\n");
148 switch_tracking->tids[cpu] = next_tid;
151 if (evsel == switch_tracking->cycles_evsel) {
152 pr_debug3("cycles event\n");
153 if (!switch_tracking->comm_seen[0])
154 switch_tracking->cycles_before_comm_1 = 1;
155 if (switch_tracking->comm_seen[1] &&
156 !switch_tracking->comm_seen[2])
157 switch_tracking->cycles_between_comm_2_and_comm_3 = 1;
158 if (switch_tracking->comm_seen[3])
159 switch_tracking->cycles_after_comm_4 = 1;
165 static int process_event(struct perf_evlist *evlist, union perf_event *event,
166 struct switch_tracking *switch_tracking)
168 if (event->header.type == PERF_RECORD_SAMPLE)
169 return process_sample_event(evlist, event, switch_tracking);
171 if (event->header.type == PERF_RECORD_COMM) {
174 err = check_comm(switch_tracking, event, "Test COMM 1", 0);
178 err = check_comm(switch_tracking, event, "Test COMM 2", 1);
182 err = check_comm(switch_tracking, event, "Test COMM 3", 2);
186 err = check_comm(switch_tracking, event, "Test COMM 4", 3);
191 pr_debug("Unexpected comm event\n");
200 struct list_head list;
201 union perf_event *event;
205 static int add_event(struct perf_evlist *evlist, struct list_head *events,
206 union perf_event *event)
208 struct perf_sample sample;
209 struct event_node *node;
211 node = malloc(sizeof(struct event_node));
213 pr_debug("malloc failed\n");
217 list_add(&node->list, events);
219 if (perf_evlist__parse_sample(evlist, event, &sample)) {
220 pr_debug("perf_evlist__parse_sample failed\n");
225 pr_debug("event with no time\n");
229 node->event_time = sample.time;
234 static void free_event_nodes(struct list_head *events)
236 struct event_node *node;
238 while (!list_empty(events)) {
239 node = list_entry(events->next, struct event_node, list);
240 list_del(&node->list);
245 static int compar(const void *a, const void *b)
247 const struct event_node *nodea = a;
248 const struct event_node *nodeb = b;
249 s64 cmp = nodea->event_time - nodeb->event_time;
254 static int process_events(struct perf_evlist *evlist,
255 struct switch_tracking *switch_tracking)
257 union perf_event *event;
258 unsigned pos, cnt = 0;
260 struct event_node *events_array, *node;
261 struct perf_mmap *md;
264 for (i = 0; i < evlist->nr_mmaps; i++) {
265 md = &evlist->mmap[i];
266 if (perf_mmap__read_init(md) < 0)
269 while ((event = perf_mmap__read_event(md)) != NULL) {
271 ret = add_event(evlist, &events, event);
272 perf_mmap__consume(md);
276 perf_mmap__read_done(md);
279 events_array = calloc(cnt, sizeof(struct event_node));
281 pr_debug("calloc failed\n");
287 list_for_each_entry(node, &events, list)
288 events_array[pos++] = *node;
290 qsort(events_array, cnt, sizeof(struct event_node), compar);
292 for (pos = 0; pos < cnt; pos++) {
293 ret = process_event(evlist, events_array[pos].event,
301 pr_debug("%u events recorded\n", cnt);
304 free_event_nodes(&events);
309 * test__switch_tracking - test using sched_switch and tracking events.
311 * This function implements a test that checks that sched_switch events and
312 * tracking events can be recorded for a workload (current process) using the
313 * evsel->system_wide and evsel->tracking flags (respectively) with other events
314 * sometimes enabled or disabled.
316 int test__switch_tracking(struct test *test __maybe_unused, int subtest __maybe_unused)
318 const char *sched_switch = "sched:sched_switch";
319 struct switch_tracking switch_tracking = { .tids = NULL, };
320 struct record_opts opts = {
321 .mmap_pages = UINT_MAX,
322 .user_freq = UINT_MAX,
323 .user_interval = ULLONG_MAX,
329 struct thread_map *threads = NULL;
330 struct cpu_map *cpus = NULL;
331 struct perf_evlist *evlist = NULL;
332 struct perf_evsel *evsel, *cpu_clocks_evsel, *cycles_evsel;
333 struct perf_evsel *switch_evsel, *tracking_evsel;
337 threads = thread_map__new(-1, getpid(), UINT_MAX);
339 pr_debug("thread_map__new failed!\n");
343 cpus = cpu_map__new(NULL);
345 pr_debug("cpu_map__new failed!\n");
349 evlist = perf_evlist__new();
351 pr_debug("perf_evlist__new failed!\n");
355 perf_evlist__set_maps(evlist, cpus, threads);
358 err = parse_events(evlist, "cpu-clock:u", NULL);
360 pr_debug("Failed to parse event dummy:u\n");
364 cpu_clocks_evsel = perf_evlist__last(evlist);
367 err = parse_events(evlist, "cycles:u", NULL);
369 pr_debug("Failed to parse event cycles:u\n");
373 cycles_evsel = perf_evlist__last(evlist);
376 if (!perf_evlist__can_select_event(evlist, sched_switch)) {
377 pr_debug("No sched_switch\n");
382 err = parse_events(evlist, sched_switch, NULL);
384 pr_debug("Failed to parse event %s\n", sched_switch);
388 switch_evsel = perf_evlist__last(evlist);
390 perf_evsel__set_sample_bit(switch_evsel, CPU);
391 perf_evsel__set_sample_bit(switch_evsel, TIME);
393 switch_evsel->system_wide = true;
394 switch_evsel->no_aux_samples = true;
395 switch_evsel->immediate = true;
397 /* Test moving an event to the front */
398 if (cycles_evsel == perf_evlist__first(evlist)) {
399 pr_debug("cycles event already at front");
402 perf_evlist__to_front(evlist, cycles_evsel);
403 if (cycles_evsel != perf_evlist__first(evlist)) {
404 pr_debug("Failed to move cycles event to front");
408 perf_evsel__set_sample_bit(cycles_evsel, CPU);
409 perf_evsel__set_sample_bit(cycles_evsel, TIME);
412 err = parse_events(evlist, "dummy:u", NULL);
414 pr_debug("Failed to parse event dummy:u\n");
418 tracking_evsel = perf_evlist__last(evlist);
420 perf_evlist__set_tracking_event(evlist, tracking_evsel);
422 tracking_evsel->attr.freq = 0;
423 tracking_evsel->attr.sample_period = 1;
425 perf_evsel__set_sample_bit(tracking_evsel, TIME);
428 perf_evlist__config(evlist, &opts, NULL);
430 /* Check moved event is still at the front */
431 if (cycles_evsel != perf_evlist__first(evlist)) {
432 pr_debug("Front event no longer at front");
436 /* Check tracking event is tracking */
437 if (!tracking_evsel->attr.mmap || !tracking_evsel->attr.comm) {
438 pr_debug("Tracking event not tracking\n");
442 /* Check non-tracking events are not tracking */
443 evlist__for_each_entry(evlist, evsel) {
444 if (evsel != tracking_evsel) {
445 if (evsel->attr.mmap || evsel->attr.comm) {
446 pr_debug("Non-tracking event is tracking\n");
452 if (perf_evlist__open(evlist) < 0) {
453 pr_debug("Not supported\n");
458 err = perf_evlist__mmap(evlist, UINT_MAX);
460 pr_debug("perf_evlist__mmap failed!\n");
464 perf_evlist__enable(evlist);
466 err = perf_evsel__disable(cpu_clocks_evsel);
468 pr_debug("perf_evlist__disable_event failed!\n");
474 pr_debug("spin_sleep failed!\n");
478 comm = "Test COMM 1";
479 err = prctl(PR_SET_NAME, (unsigned long)comm, 0, 0, 0);
481 pr_debug("PR_SET_NAME failed!\n");
485 err = perf_evsel__disable(cycles_evsel);
487 pr_debug("perf_evlist__disable_event failed!\n");
491 comm = "Test COMM 2";
492 err = prctl(PR_SET_NAME, (unsigned long)comm, 0, 0, 0);
494 pr_debug("PR_SET_NAME failed!\n");
500 pr_debug("spin_sleep failed!\n");
504 comm = "Test COMM 3";
505 err = prctl(PR_SET_NAME, (unsigned long)comm, 0, 0, 0);
507 pr_debug("PR_SET_NAME failed!\n");
511 err = perf_evsel__enable(cycles_evsel);
513 pr_debug("perf_evlist__disable_event failed!\n");
517 comm = "Test COMM 4";
518 err = prctl(PR_SET_NAME, (unsigned long)comm, 0, 0, 0);
520 pr_debug("PR_SET_NAME failed!\n");
526 pr_debug("spin_sleep failed!\n");
530 perf_evlist__disable(evlist);
532 switch_tracking.switch_evsel = switch_evsel;
533 switch_tracking.cycles_evsel = cycles_evsel;
535 err = process_events(evlist, &switch_tracking);
537 zfree(&switch_tracking.tids);
542 /* Check all 4 comm events were seen i.e. that evsel->tracking works */
543 if (!switch_tracking.comm_seen[0] || !switch_tracking.comm_seen[1] ||
544 !switch_tracking.comm_seen[2] || !switch_tracking.comm_seen[3]) {
545 pr_debug("Missing comm events\n");
549 /* Check cycles event got enabled */
550 if (!switch_tracking.cycles_before_comm_1) {
551 pr_debug("Missing cycles events\n");
555 /* Check cycles event got disabled */
556 if (switch_tracking.cycles_between_comm_2_and_comm_3) {
557 pr_debug("cycles events even though event was disabled\n");
561 /* Check cycles event got enabled again */
562 if (!switch_tracking.cycles_after_comm_4) {
563 pr_debug("Missing cycles events\n");
568 perf_evlist__disable(evlist);
569 perf_evlist__delete(evlist);
572 thread_map__put(threads);