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
;
263 for (i
= 0; i
< evlist
->nr_mmaps
; i
++) {
264 while ((event
= perf_evlist__mmap_read(evlist
, i
)) != NULL
) {
266 ret
= add_event(evlist
, &events
, event
);
267 perf_evlist__mmap_consume(evlist
, i
);
273 events_array
= calloc(cnt
, sizeof(struct event_node
));
275 pr_debug("calloc failed\n");
281 list_for_each_entry(node
, &events
, list
)
282 events_array
[pos
++] = *node
;
284 qsort(events_array
, cnt
, sizeof(struct event_node
), compar
);
286 for (pos
= 0; pos
< cnt
; pos
++) {
287 ret
= process_event(evlist
, events_array
[pos
].event
,
295 pr_debug("%u events recorded\n", cnt
);
298 free_event_nodes(&events
);
303 * test__switch_tracking - test using sched_switch and tracking events.
305 * This function implements a test that checks that sched_switch events and
306 * tracking events can be recorded for a workload (current process) using the
307 * evsel->system_wide and evsel->tracking flags (respectively) with other events
308 * sometimes enabled or disabled.
310 int test__switch_tracking(struct test
*test __maybe_unused
, int subtest __maybe_unused
)
312 const char *sched_switch
= "sched:sched_switch";
313 struct switch_tracking switch_tracking
= { .tids
= NULL
, };
314 struct record_opts opts
= {
315 .mmap_pages
= UINT_MAX
,
316 .user_freq
= UINT_MAX
,
317 .user_interval
= ULLONG_MAX
,
323 struct thread_map
*threads
= NULL
;
324 struct cpu_map
*cpus
= NULL
;
325 struct perf_evlist
*evlist
= NULL
;
326 struct perf_evsel
*evsel
, *cpu_clocks_evsel
, *cycles_evsel
;
327 struct perf_evsel
*switch_evsel
, *tracking_evsel
;
331 threads
= thread_map__new(-1, getpid(), UINT_MAX
);
333 pr_debug("thread_map__new failed!\n");
337 cpus
= cpu_map__new(NULL
);
339 pr_debug("cpu_map__new failed!\n");
343 evlist
= perf_evlist__new();
345 pr_debug("perf_evlist__new failed!\n");
349 perf_evlist__set_maps(evlist
, cpus
, threads
);
352 err
= parse_events(evlist
, "cpu-clock:u", NULL
);
354 pr_debug("Failed to parse event dummy:u\n");
358 cpu_clocks_evsel
= perf_evlist__last(evlist
);
361 err
= parse_events(evlist
, "cycles:u", NULL
);
363 pr_debug("Failed to parse event cycles:u\n");
367 cycles_evsel
= perf_evlist__last(evlist
);
370 if (!perf_evlist__can_select_event(evlist
, sched_switch
)) {
371 pr_debug("No sched_switch\n");
376 err
= parse_events(evlist
, sched_switch
, NULL
);
378 pr_debug("Failed to parse event %s\n", sched_switch
);
382 switch_evsel
= perf_evlist__last(evlist
);
384 perf_evsel__set_sample_bit(switch_evsel
, CPU
);
385 perf_evsel__set_sample_bit(switch_evsel
, TIME
);
387 switch_evsel
->system_wide
= true;
388 switch_evsel
->no_aux_samples
= true;
389 switch_evsel
->immediate
= true;
391 /* Test moving an event to the front */
392 if (cycles_evsel
== perf_evlist__first(evlist
)) {
393 pr_debug("cycles event already at front");
396 perf_evlist__to_front(evlist
, cycles_evsel
);
397 if (cycles_evsel
!= perf_evlist__first(evlist
)) {
398 pr_debug("Failed to move cycles event to front");
402 perf_evsel__set_sample_bit(cycles_evsel
, CPU
);
403 perf_evsel__set_sample_bit(cycles_evsel
, TIME
);
406 err
= parse_events(evlist
, "dummy:u", NULL
);
408 pr_debug("Failed to parse event dummy:u\n");
412 tracking_evsel
= perf_evlist__last(evlist
);
414 perf_evlist__set_tracking_event(evlist
, tracking_evsel
);
416 tracking_evsel
->attr
.freq
= 0;
417 tracking_evsel
->attr
.sample_period
= 1;
419 perf_evsel__set_sample_bit(tracking_evsel
, TIME
);
422 perf_evlist__config(evlist
, &opts
, NULL
);
424 /* Check moved event is still at the front */
425 if (cycles_evsel
!= perf_evlist__first(evlist
)) {
426 pr_debug("Front event no longer at front");
430 /* Check tracking event is tracking */
431 if (!tracking_evsel
->attr
.mmap
|| !tracking_evsel
->attr
.comm
) {
432 pr_debug("Tracking event not tracking\n");
436 /* Check non-tracking events are not tracking */
437 evlist__for_each_entry(evlist
, evsel
) {
438 if (evsel
!= tracking_evsel
) {
439 if (evsel
->attr
.mmap
|| evsel
->attr
.comm
) {
440 pr_debug("Non-tracking event is tracking\n");
446 if (perf_evlist__open(evlist
) < 0) {
447 pr_debug("Not supported\n");
452 err
= perf_evlist__mmap(evlist
, UINT_MAX
);
454 pr_debug("perf_evlist__mmap failed!\n");
458 perf_evlist__enable(evlist
);
460 err
= perf_evsel__disable(cpu_clocks_evsel
);
462 pr_debug("perf_evlist__disable_event failed!\n");
468 pr_debug("spin_sleep failed!\n");
472 comm
= "Test COMM 1";
473 err
= prctl(PR_SET_NAME
, (unsigned long)comm
, 0, 0, 0);
475 pr_debug("PR_SET_NAME failed!\n");
479 err
= perf_evsel__disable(cycles_evsel
);
481 pr_debug("perf_evlist__disable_event failed!\n");
485 comm
= "Test COMM 2";
486 err
= prctl(PR_SET_NAME
, (unsigned long)comm
, 0, 0, 0);
488 pr_debug("PR_SET_NAME failed!\n");
494 pr_debug("spin_sleep failed!\n");
498 comm
= "Test COMM 3";
499 err
= prctl(PR_SET_NAME
, (unsigned long)comm
, 0, 0, 0);
501 pr_debug("PR_SET_NAME failed!\n");
505 err
= perf_evsel__enable(cycles_evsel
);
507 pr_debug("perf_evlist__disable_event failed!\n");
511 comm
= "Test COMM 4";
512 err
= prctl(PR_SET_NAME
, (unsigned long)comm
, 0, 0, 0);
514 pr_debug("PR_SET_NAME failed!\n");
520 pr_debug("spin_sleep failed!\n");
524 perf_evlist__disable(evlist
);
526 switch_tracking
.switch_evsel
= switch_evsel
;
527 switch_tracking
.cycles_evsel
= cycles_evsel
;
529 err
= process_events(evlist
, &switch_tracking
);
531 zfree(&switch_tracking
.tids
);
536 /* Check all 4 comm events were seen i.e. that evsel->tracking works */
537 if (!switch_tracking
.comm_seen
[0] || !switch_tracking
.comm_seen
[1] ||
538 !switch_tracking
.comm_seen
[2] || !switch_tracking
.comm_seen
[3]) {
539 pr_debug("Missing comm events\n");
543 /* Check cycles event got enabled */
544 if (!switch_tracking
.cycles_before_comm_1
) {
545 pr_debug("Missing cycles events\n");
549 /* Check cycles event got disabled */
550 if (switch_tracking
.cycles_between_comm_2_and_comm_3
) {
551 pr_debug("cycles events even though event was disabled\n");
555 /* Check cycles event got enabled again */
556 if (!switch_tracking
.cycles_after_comm_4
) {
557 pr_debug("Missing cycles events\n");
562 perf_evlist__disable(evlist
);
563 perf_evlist__delete(evlist
);
566 thread_map__put(threads
);