1 // SPDX-License-Identifier: GPL-2.0
8 #include <linux/zalloc.h>
10 #include <perf/cpumap.h>
11 #include <perf/evlist.h>
12 #include <perf/mmap.h>
15 #include "parse-events.h"
18 #include "thread_map.h"
21 #include "util/mmap.h"
22 #include "util/sample.h"
25 static int spin_sleep(void)
27 struct timeval start
, now
, diff
, maxtime
;
32 maxtime
.tv_usec
= 50000;
34 err
= gettimeofday(&start
, NULL
);
40 for (i
= 0; i
< 1000; i
++)
43 err
= gettimeofday(&now
, NULL
);
47 timersub(&now
, &start
, &diff
);
48 if (timercmp(&diff
, &maxtime
, > /* For checkpatch */))
52 ts
.tv_nsec
= 50 * 1000 * 1000;
56 err
= nanosleep(&ts
, NULL
);
63 struct switch_tracking
{
64 struct evsel
*switch_evsel
;
65 struct evsel
*cycles_evsel
;
69 int cycles_before_comm_1
;
70 int cycles_between_comm_2_and_comm_3
;
71 int cycles_after_comm_4
;
74 static int check_comm(struct switch_tracking
*switch_tracking
,
75 union perf_event
*event
, const char *comm
, int nr
)
77 if (event
->header
.type
== PERF_RECORD_COMM
&&
78 (pid_t
)event
->comm
.pid
== getpid() &&
79 (pid_t
)event
->comm
.tid
== getpid() &&
80 strcmp(event
->comm
.comm
, comm
) == 0) {
81 if (switch_tracking
->comm_seen
[nr
]) {
82 pr_debug("Duplicate comm event\n");
85 switch_tracking
->comm_seen
[nr
] = 1;
86 pr_debug3("comm event: %s nr: %d\n", event
->comm
.comm
, nr
);
92 static int check_cpu(struct switch_tracking
*switch_tracking
, int cpu
)
99 if (!switch_tracking
->tids
) {
100 switch_tracking
->tids
= calloc(nr
, sizeof(pid_t
));
101 if (!switch_tracking
->tids
)
103 for (i
= 0; i
< nr
; i
++)
104 switch_tracking
->tids
[i
] = -1;
105 switch_tracking
->nr_tids
= nr
;
109 if (cpu
>= switch_tracking
->nr_tids
) {
112 addr
= realloc(switch_tracking
->tids
, nr
* sizeof(pid_t
));
115 switch_tracking
->tids
= addr
;
116 for (i
= switch_tracking
->nr_tids
; i
< nr
; i
++)
117 switch_tracking
->tids
[i
] = -1;
118 switch_tracking
->nr_tids
= nr
;
125 static int process_sample_event(struct evlist
*evlist
,
126 union perf_event
*event
,
127 struct switch_tracking
*switch_tracking
)
129 struct perf_sample sample
;
131 pid_t next_tid
, prev_tid
;
134 if (evlist__parse_sample(evlist
, event
, &sample
)) {
135 pr_debug("evlist__parse_sample failed\n");
139 evsel
= evlist__id2evsel(evlist
, sample
.id
);
140 if (evsel
== switch_tracking
->switch_evsel
) {
141 next_tid
= evsel__intval(evsel
, &sample
, "next_pid");
142 prev_tid
= evsel__intval(evsel
, &sample
, "prev_pid");
144 pr_debug3("sched_switch: cpu: %d prev_tid %d next_tid %d\n",
145 cpu
, prev_tid
, next_tid
);
146 err
= check_cpu(switch_tracking
, cpu
);
150 * Check for no missing sched_switch events i.e. that the
151 * evsel->core.system_wide flag has worked.
153 if (switch_tracking
->tids
[cpu
] != -1 &&
154 switch_tracking
->tids
[cpu
] != prev_tid
) {
155 pr_debug("Missing sched_switch events\n");
158 switch_tracking
->tids
[cpu
] = next_tid
;
161 if (evsel
== switch_tracking
->cycles_evsel
) {
162 pr_debug3("cycles event\n");
163 if (!switch_tracking
->comm_seen
[0])
164 switch_tracking
->cycles_before_comm_1
= 1;
165 if (switch_tracking
->comm_seen
[1] &&
166 !switch_tracking
->comm_seen
[2])
167 switch_tracking
->cycles_between_comm_2_and_comm_3
= 1;
168 if (switch_tracking
->comm_seen
[3])
169 switch_tracking
->cycles_after_comm_4
= 1;
175 static int process_event(struct evlist
*evlist
, union perf_event
*event
,
176 struct switch_tracking
*switch_tracking
)
178 if (event
->header
.type
== PERF_RECORD_SAMPLE
)
179 return process_sample_event(evlist
, event
, switch_tracking
);
181 if (event
->header
.type
== PERF_RECORD_COMM
) {
184 err
= check_comm(switch_tracking
, event
, "Test COMM 1", 0);
188 err
= check_comm(switch_tracking
, event
, "Test COMM 2", 1);
192 err
= check_comm(switch_tracking
, event
, "Test COMM 3", 2);
196 err
= check_comm(switch_tracking
, event
, "Test COMM 4", 3);
201 pr_debug("Unexpected comm event\n");
210 struct list_head list
;
211 union perf_event
*event
;
215 static int add_event(struct evlist
*evlist
, struct list_head
*events
,
216 union perf_event
*event
)
218 struct perf_sample sample
;
219 struct event_node
*node
;
221 node
= malloc(sizeof(struct event_node
));
223 pr_debug("malloc failed\n");
227 list_add(&node
->list
, events
);
229 if (evlist__parse_sample(evlist
, event
, &sample
)) {
230 pr_debug("evlist__parse_sample failed\n");
235 pr_debug("event with no time\n");
239 node
->event_time
= sample
.time
;
244 static void free_event_nodes(struct list_head
*events
)
246 struct event_node
*node
;
248 while (!list_empty(events
)) {
249 node
= list_entry(events
->next
, struct event_node
, list
);
250 list_del_init(&node
->list
);
255 static int compar(const void *a
, const void *b
)
257 const struct event_node
*nodea
= a
;
258 const struct event_node
*nodeb
= b
;
259 s64 cmp
= nodea
->event_time
- nodeb
->event_time
;
264 static int process_events(struct evlist
*evlist
,
265 struct switch_tracking
*switch_tracking
)
267 union perf_event
*event
;
268 unsigned pos
, cnt
= 0;
270 struct event_node
*events_array
, *node
;
274 for (i
= 0; i
< evlist
->core
.nr_mmaps
; i
++) {
275 md
= &evlist
->mmap
[i
];
276 if (perf_mmap__read_init(&md
->core
) < 0)
279 while ((event
= perf_mmap__read_event(&md
->core
)) != NULL
) {
281 ret
= add_event(evlist
, &events
, event
);
282 perf_mmap__consume(&md
->core
);
286 perf_mmap__read_done(&md
->core
);
289 events_array
= calloc(cnt
, sizeof(struct event_node
));
291 pr_debug("calloc failed\n");
297 list_for_each_entry(node
, &events
, list
)
298 events_array
[pos
++] = *node
;
300 qsort(events_array
, cnt
, sizeof(struct event_node
), compar
);
302 for (pos
= 0; pos
< cnt
; pos
++) {
303 ret
= process_event(evlist
, events_array
[pos
].event
,
311 pr_debug("%u events recorded\n", cnt
);
314 free_event_nodes(&events
);
319 * test__switch_tracking - test using sched_switch and tracking events.
321 * This function implements a test that checks that sched_switch events and
322 * tracking events can be recorded for a workload (current process) using the
323 * evsel->core.system_wide and evsel->tracking flags (respectively) with other events
324 * sometimes enabled or disabled.
326 static int test__switch_tracking(struct test_suite
*test __maybe_unused
, int subtest __maybe_unused
)
328 const char *sched_switch
= "sched:sched_switch";
329 const char *cycles
= "cycles:u";
330 struct switch_tracking switch_tracking
= { .tids
= NULL
, };
331 struct record_opts opts
= {
332 .mmap_pages
= UINT_MAX
,
333 .user_freq
= UINT_MAX
,
334 .user_interval
= ULLONG_MAX
,
340 struct perf_thread_map
*threads
= NULL
;
341 struct perf_cpu_map
*cpus
= NULL
;
342 struct evlist
*evlist
= NULL
;
343 struct evsel
*evsel
, *cpu_clocks_evsel
, *cycles_evsel
;
344 struct evsel
*switch_evsel
, *tracking_evsel
;
348 threads
= thread_map__new(-1, getpid(), UINT_MAX
);
350 pr_debug("thread_map__new failed!\n");
354 cpus
= perf_cpu_map__new_online_cpus();
356 pr_debug("perf_cpu_map__new failed!\n");
360 evlist
= evlist__new();
362 pr_debug("evlist__new failed!\n");
366 perf_evlist__set_maps(&evlist
->core
, cpus
, threads
);
369 err
= parse_event(evlist
, "cpu-clock:u");
371 pr_debug("Failed to parse event dummy:u\n");
375 cpu_clocks_evsel
= evlist__last(evlist
);
378 err
= parse_event(evlist
, cycles
);
380 pr_debug("Failed to parse event %s\n", cycles
);
384 cycles_evsel
= evlist__last(evlist
);
387 if (!evlist__can_select_event(evlist
, sched_switch
)) {
388 pr_debug("No sched_switch\n");
393 switch_evsel
= evlist__add_sched_switch(evlist
, true);
394 if (IS_ERR(switch_evsel
)) {
395 err
= PTR_ERR(switch_evsel
);
396 pr_debug("Failed to create event %s\n", sched_switch
);
400 switch_evsel
->immediate
= true;
402 /* Test moving an event to the front */
403 if (cycles_evsel
== evlist__first(evlist
)) {
404 pr_debug("cycles event already at front");
407 evlist__to_front(evlist
, cycles_evsel
);
408 if (cycles_evsel
!= evlist__first(evlist
)) {
409 pr_debug("Failed to move cycles event to front");
413 evsel__set_sample_bit(cycles_evsel
, CPU
);
414 evsel__set_sample_bit(cycles_evsel
, TIME
);
417 err
= parse_event(evlist
, "dummy:u");
419 pr_debug("Failed to parse event dummy:u\n");
423 tracking_evsel
= evlist__last(evlist
);
425 evlist__set_tracking_event(evlist
, tracking_evsel
);
427 tracking_evsel
->core
.attr
.freq
= 0;
428 tracking_evsel
->core
.attr
.sample_period
= 1;
430 evsel__set_sample_bit(tracking_evsel
, TIME
);
433 evlist__config(evlist
, &opts
, NULL
);
435 /* Check moved event is still at the front */
436 if (cycles_evsel
!= evlist__first(evlist
)) {
437 pr_debug("Front event no longer at front");
441 /* Check tracking event is tracking */
442 if (!tracking_evsel
->core
.attr
.mmap
|| !tracking_evsel
->core
.attr
.comm
) {
443 pr_debug("Tracking event not tracking\n");
447 /* Check non-tracking events are not tracking */
448 evlist__for_each_entry(evlist
, evsel
) {
449 if (evsel
!= tracking_evsel
) {
450 if (evsel
->core
.attr
.mmap
|| evsel
->core
.attr
.comm
) {
451 pr_debug("Non-tracking event is tracking\n");
457 if (evlist__open(evlist
) < 0) {
458 pr_debug("Not supported\n");
463 err
= evlist__mmap(evlist
, UINT_MAX
);
465 pr_debug("evlist__mmap failed!\n");
469 evlist__enable(evlist
);
471 err
= evsel__disable(cpu_clocks_evsel
);
473 pr_debug("perf_evlist__disable_event failed!\n");
479 pr_debug("spin_sleep failed!\n");
483 comm
= "Test COMM 1";
484 err
= prctl(PR_SET_NAME
, (unsigned long)comm
, 0, 0, 0);
486 pr_debug("PR_SET_NAME failed!\n");
490 err
= evsel__disable(cycles_evsel
);
492 pr_debug("perf_evlist__disable_event failed!\n");
496 comm
= "Test COMM 2";
497 err
= prctl(PR_SET_NAME
, (unsigned long)comm
, 0, 0, 0);
499 pr_debug("PR_SET_NAME failed!\n");
505 pr_debug("spin_sleep failed!\n");
509 comm
= "Test COMM 3";
510 err
= prctl(PR_SET_NAME
, (unsigned long)comm
, 0, 0, 0);
512 pr_debug("PR_SET_NAME failed!\n");
516 err
= evsel__enable(cycles_evsel
);
518 pr_debug("perf_evlist__disable_event failed!\n");
522 comm
= "Test COMM 4";
523 err
= prctl(PR_SET_NAME
, (unsigned long)comm
, 0, 0, 0);
525 pr_debug("PR_SET_NAME failed!\n");
531 pr_debug("spin_sleep failed!\n");
535 evlist__disable(evlist
);
537 switch_tracking
.switch_evsel
= switch_evsel
;
538 switch_tracking
.cycles_evsel
= cycles_evsel
;
540 err
= process_events(evlist
, &switch_tracking
);
542 zfree(&switch_tracking
.tids
);
547 /* Check all 4 comm events were seen i.e. that evsel->tracking works */
548 if (!switch_tracking
.comm_seen
[0] || !switch_tracking
.comm_seen
[1] ||
549 !switch_tracking
.comm_seen
[2] || !switch_tracking
.comm_seen
[3]) {
550 pr_debug("Missing comm events\n");
554 /* Check cycles event got enabled */
555 if (!switch_tracking
.cycles_before_comm_1
) {
556 pr_debug("Missing cycles events\n");
560 /* Check cycles event got disabled */
561 if (switch_tracking
.cycles_between_comm_2_and_comm_3
) {
562 pr_debug("cycles events even though event was disabled\n");
566 /* Check cycles event got enabled again */
567 if (!switch_tracking
.cycles_after_comm_4
) {
568 pr_debug("Missing cycles events\n");
573 evlist__disable(evlist
);
574 evlist__delete(evlist
);
576 perf_cpu_map__put(cpus
);
577 perf_thread_map__put(threads
);
586 DEFINE_SUITE("Track with sched_switch", switch_tracking
);