1 // SPDX-License-Identifier: GPL-2.0
8 #include <linux/zalloc.h>
9 #include <perf/cpumap.h>
10 #include <perf/evlist.h>
11 #include <perf/mmap.h>
14 #include "parse-events.h"
17 #include "thread_map.h"
20 #include "util/mmap.h"
22 static int spin_sleep(void)
24 struct timeval start
, now
, diff
, maxtime
;
29 maxtime
.tv_usec
= 50000;
31 err
= gettimeofday(&start
, NULL
);
37 for (i
= 0; i
< 1000; i
++)
40 err
= gettimeofday(&now
, NULL
);
44 timersub(&now
, &start
, &diff
);
45 if (timercmp(&diff
, &maxtime
, > /* For checkpatch */))
49 ts
.tv_nsec
= 50 * 1000 * 1000;
53 err
= nanosleep(&ts
, NULL
);
60 struct switch_tracking
{
61 struct evsel
*switch_evsel
;
62 struct evsel
*cycles_evsel
;
66 int cycles_before_comm_1
;
67 int cycles_between_comm_2_and_comm_3
;
68 int cycles_after_comm_4
;
71 static int check_comm(struct switch_tracking
*switch_tracking
,
72 union perf_event
*event
, const char *comm
, int nr
)
74 if (event
->header
.type
== PERF_RECORD_COMM
&&
75 (pid_t
)event
->comm
.pid
== getpid() &&
76 (pid_t
)event
->comm
.tid
== getpid() &&
77 strcmp(event
->comm
.comm
, comm
) == 0) {
78 if (switch_tracking
->comm_seen
[nr
]) {
79 pr_debug("Duplicate comm event\n");
82 switch_tracking
->comm_seen
[nr
] = 1;
83 pr_debug3("comm event: %s nr: %d\n", event
->comm
.comm
, nr
);
89 static int check_cpu(struct switch_tracking
*switch_tracking
, int cpu
)
96 if (!switch_tracking
->tids
) {
97 switch_tracking
->tids
= calloc(nr
, sizeof(pid_t
));
98 if (!switch_tracking
->tids
)
100 for (i
= 0; i
< nr
; i
++)
101 switch_tracking
->tids
[i
] = -1;
102 switch_tracking
->nr_tids
= nr
;
106 if (cpu
>= switch_tracking
->nr_tids
) {
109 addr
= realloc(switch_tracking
->tids
, nr
* sizeof(pid_t
));
112 switch_tracking
->tids
= addr
;
113 for (i
= switch_tracking
->nr_tids
; i
< nr
; i
++)
114 switch_tracking
->tids
[i
] = -1;
115 switch_tracking
->nr_tids
= nr
;
122 static int process_sample_event(struct evlist
*evlist
,
123 union perf_event
*event
,
124 struct switch_tracking
*switch_tracking
)
126 struct perf_sample sample
;
128 pid_t next_tid
, prev_tid
;
131 if (evlist__parse_sample(evlist
, event
, &sample
)) {
132 pr_debug("evlist__parse_sample failed\n");
136 evsel
= evlist__id2evsel(evlist
, sample
.id
);
137 if (evsel
== switch_tracking
->switch_evsel
) {
138 next_tid
= evsel__intval(evsel
, &sample
, "next_pid");
139 prev_tid
= evsel__intval(evsel
, &sample
, "prev_pid");
141 pr_debug3("sched_switch: cpu: %d prev_tid %d next_tid %d\n",
142 cpu
, prev_tid
, next_tid
);
143 err
= check_cpu(switch_tracking
, cpu
);
147 * Check for no missing sched_switch events i.e. that the
148 * evsel->core.system_wide flag has worked.
150 if (switch_tracking
->tids
[cpu
] != -1 &&
151 switch_tracking
->tids
[cpu
] != prev_tid
) {
152 pr_debug("Missing sched_switch events\n");
155 switch_tracking
->tids
[cpu
] = next_tid
;
158 if (evsel
== switch_tracking
->cycles_evsel
) {
159 pr_debug3("cycles event\n");
160 if (!switch_tracking
->comm_seen
[0])
161 switch_tracking
->cycles_before_comm_1
= 1;
162 if (switch_tracking
->comm_seen
[1] &&
163 !switch_tracking
->comm_seen
[2])
164 switch_tracking
->cycles_between_comm_2_and_comm_3
= 1;
165 if (switch_tracking
->comm_seen
[3])
166 switch_tracking
->cycles_after_comm_4
= 1;
172 static int process_event(struct evlist
*evlist
, union perf_event
*event
,
173 struct switch_tracking
*switch_tracking
)
175 if (event
->header
.type
== PERF_RECORD_SAMPLE
)
176 return process_sample_event(evlist
, event
, switch_tracking
);
178 if (event
->header
.type
== PERF_RECORD_COMM
) {
181 err
= check_comm(switch_tracking
, event
, "Test COMM 1", 0);
185 err
= check_comm(switch_tracking
, event
, "Test COMM 2", 1);
189 err
= check_comm(switch_tracking
, event
, "Test COMM 3", 2);
193 err
= check_comm(switch_tracking
, event
, "Test COMM 4", 3);
198 pr_debug("Unexpected comm event\n");
207 struct list_head list
;
208 union perf_event
*event
;
212 static int add_event(struct evlist
*evlist
, struct list_head
*events
,
213 union perf_event
*event
)
215 struct perf_sample sample
;
216 struct event_node
*node
;
218 node
= malloc(sizeof(struct event_node
));
220 pr_debug("malloc failed\n");
224 list_add(&node
->list
, events
);
226 if (evlist__parse_sample(evlist
, event
, &sample
)) {
227 pr_debug("evlist__parse_sample failed\n");
232 pr_debug("event with no time\n");
236 node
->event_time
= sample
.time
;
241 static void free_event_nodes(struct list_head
*events
)
243 struct event_node
*node
;
245 while (!list_empty(events
)) {
246 node
= list_entry(events
->next
, struct event_node
, list
);
247 list_del_init(&node
->list
);
252 static int compar(const void *a
, const void *b
)
254 const struct event_node
*nodea
= a
;
255 const struct event_node
*nodeb
= b
;
256 s64 cmp
= nodea
->event_time
- nodeb
->event_time
;
261 static int process_events(struct evlist
*evlist
,
262 struct switch_tracking
*switch_tracking
)
264 union perf_event
*event
;
265 unsigned pos
, cnt
= 0;
267 struct event_node
*events_array
, *node
;
271 for (i
= 0; i
< evlist
->core
.nr_mmaps
; i
++) {
272 md
= &evlist
->mmap
[i
];
273 if (perf_mmap__read_init(&md
->core
) < 0)
276 while ((event
= perf_mmap__read_event(&md
->core
)) != NULL
) {
278 ret
= add_event(evlist
, &events
, event
);
279 perf_mmap__consume(&md
->core
);
283 perf_mmap__read_done(&md
->core
);
286 events_array
= calloc(cnt
, sizeof(struct event_node
));
288 pr_debug("calloc failed\n");
294 list_for_each_entry(node
, &events
, list
)
295 events_array
[pos
++] = *node
;
297 qsort(events_array
, cnt
, sizeof(struct event_node
), compar
);
299 for (pos
= 0; pos
< cnt
; pos
++) {
300 ret
= process_event(evlist
, events_array
[pos
].event
,
308 pr_debug("%u events recorded\n", cnt
);
311 free_event_nodes(&events
);
316 * test__switch_tracking - test using sched_switch and tracking events.
318 * This function implements a test that checks that sched_switch events and
319 * tracking events can be recorded for a workload (current process) using the
320 * evsel->core.system_wide and evsel->tracking flags (respectively) with other events
321 * sometimes enabled or disabled.
323 int test__switch_tracking(struct test
*test __maybe_unused
, int subtest __maybe_unused
)
325 const char *sched_switch
= "sched:sched_switch";
326 struct switch_tracking switch_tracking
= { .tids
= NULL
, };
327 struct record_opts opts
= {
328 .mmap_pages
= UINT_MAX
,
329 .user_freq
= UINT_MAX
,
330 .user_interval
= ULLONG_MAX
,
336 struct perf_thread_map
*threads
= NULL
;
337 struct perf_cpu_map
*cpus
= NULL
;
338 struct evlist
*evlist
= NULL
;
339 struct evsel
*evsel
, *cpu_clocks_evsel
, *cycles_evsel
;
340 struct evsel
*switch_evsel
, *tracking_evsel
;
344 threads
= thread_map__new(-1, getpid(), UINT_MAX
);
346 pr_debug("thread_map__new failed!\n");
350 cpus
= perf_cpu_map__new(NULL
);
352 pr_debug("perf_cpu_map__new failed!\n");
356 evlist
= evlist__new();
358 pr_debug("evlist__new failed!\n");
362 perf_evlist__set_maps(&evlist
->core
, cpus
, threads
);
365 err
= parse_events(evlist
, "cpu-clock:u", NULL
);
367 pr_debug("Failed to parse event dummy:u\n");
371 cpu_clocks_evsel
= evlist__last(evlist
);
374 err
= parse_events(evlist
, "cycles:u", NULL
);
376 pr_debug("Failed to parse event cycles:u\n");
380 cycles_evsel
= evlist__last(evlist
);
383 if (!evlist__can_select_event(evlist
, sched_switch
)) {
384 pr_debug("No sched_switch\n");
389 err
= parse_events(evlist
, sched_switch
, NULL
);
391 pr_debug("Failed to parse event %s\n", sched_switch
);
395 switch_evsel
= evlist__last(evlist
);
397 evsel__set_sample_bit(switch_evsel
, CPU
);
398 evsel__set_sample_bit(switch_evsel
, TIME
);
400 switch_evsel
->core
.system_wide
= true;
401 switch_evsel
->no_aux_samples
= true;
402 switch_evsel
->immediate
= true;
404 /* Test moving an event to the front */
405 if (cycles_evsel
== evlist__first(evlist
)) {
406 pr_debug("cycles event already at front");
409 evlist__to_front(evlist
, cycles_evsel
);
410 if (cycles_evsel
!= evlist__first(evlist
)) {
411 pr_debug("Failed to move cycles event to front");
415 evsel__set_sample_bit(cycles_evsel
, CPU
);
416 evsel__set_sample_bit(cycles_evsel
, TIME
);
419 err
= parse_events(evlist
, "dummy:u", NULL
);
421 pr_debug("Failed to parse event dummy:u\n");
425 tracking_evsel
= evlist__last(evlist
);
427 evlist__set_tracking_event(evlist
, tracking_evsel
);
429 tracking_evsel
->core
.attr
.freq
= 0;
430 tracking_evsel
->core
.attr
.sample_period
= 1;
432 evsel__set_sample_bit(tracking_evsel
, TIME
);
435 evlist__config(evlist
, &opts
, NULL
);
437 /* Check moved event is still at the front */
438 if (cycles_evsel
!= evlist__first(evlist
)) {
439 pr_debug("Front event no longer at front");
443 /* Check tracking event is tracking */
444 if (!tracking_evsel
->core
.attr
.mmap
|| !tracking_evsel
->core
.attr
.comm
) {
445 pr_debug("Tracking event not tracking\n");
449 /* Check non-tracking events are not tracking */
450 evlist__for_each_entry(evlist
, evsel
) {
451 if (evsel
!= tracking_evsel
) {
452 if (evsel
->core
.attr
.mmap
|| evsel
->core
.attr
.comm
) {
453 pr_debug("Non-tracking event is tracking\n");
459 if (evlist__open(evlist
) < 0) {
460 pr_debug("Not supported\n");
465 err
= evlist__mmap(evlist
, UINT_MAX
);
467 pr_debug("evlist__mmap failed!\n");
471 evlist__enable(evlist
);
473 err
= evsel__disable(cpu_clocks_evsel
);
475 pr_debug("perf_evlist__disable_event failed!\n");
481 pr_debug("spin_sleep failed!\n");
485 comm
= "Test COMM 1";
486 err
= prctl(PR_SET_NAME
, (unsigned long)comm
, 0, 0, 0);
488 pr_debug("PR_SET_NAME failed!\n");
492 err
= evsel__disable(cycles_evsel
);
494 pr_debug("perf_evlist__disable_event failed!\n");
498 comm
= "Test COMM 2";
499 err
= prctl(PR_SET_NAME
, (unsigned long)comm
, 0, 0, 0);
501 pr_debug("PR_SET_NAME failed!\n");
507 pr_debug("spin_sleep failed!\n");
511 comm
= "Test COMM 3";
512 err
= prctl(PR_SET_NAME
, (unsigned long)comm
, 0, 0, 0);
514 pr_debug("PR_SET_NAME failed!\n");
518 err
= evsel__enable(cycles_evsel
);
520 pr_debug("perf_evlist__disable_event failed!\n");
524 comm
= "Test COMM 4";
525 err
= prctl(PR_SET_NAME
, (unsigned long)comm
, 0, 0, 0);
527 pr_debug("PR_SET_NAME failed!\n");
533 pr_debug("spin_sleep failed!\n");
537 evlist__disable(evlist
);
539 switch_tracking
.switch_evsel
= switch_evsel
;
540 switch_tracking
.cycles_evsel
= cycles_evsel
;
542 err
= process_events(evlist
, &switch_tracking
);
544 zfree(&switch_tracking
.tids
);
549 /* Check all 4 comm events were seen i.e. that evsel->tracking works */
550 if (!switch_tracking
.comm_seen
[0] || !switch_tracking
.comm_seen
[1] ||
551 !switch_tracking
.comm_seen
[2] || !switch_tracking
.comm_seen
[3]) {
552 pr_debug("Missing comm events\n");
556 /* Check cycles event got enabled */
557 if (!switch_tracking
.cycles_before_comm_1
) {
558 pr_debug("Missing cycles events\n");
562 /* Check cycles event got disabled */
563 if (switch_tracking
.cycles_between_comm_2_and_comm_3
) {
564 pr_debug("cycles events even though event was disabled\n");
568 /* Check cycles event got enabled again */
569 if (!switch_tracking
.cycles_after_comm_4
) {
570 pr_debug("Missing cycles events\n");
575 evlist__disable(evlist
);
576 evlist__delete(evlist
);
578 perf_cpu_map__put(cpus
);
579 perf_thread_map__put(threads
);