1 // SPDX-License-Identifier: GPL-2.0
7 #include <linux/zalloc.h>
9 #include "parse-events.h"
12 #include "thread_map.h"
16 static int spin_sleep(void)
18 struct timeval start
, now
, diff
, maxtime
;
23 maxtime
.tv_usec
= 50000;
25 err
= gettimeofday(&start
, NULL
);
31 for (i
= 0; i
< 1000; i
++)
34 err
= gettimeofday(&now
, NULL
);
38 timersub(&now
, &start
, &diff
);
39 if (timercmp(&diff
, &maxtime
, > /* For checkpatch */))
43 ts
.tv_nsec
= 50 * 1000 * 1000;
47 err
= nanosleep(&ts
, NULL
);
54 struct switch_tracking
{
55 struct perf_evsel
*switch_evsel
;
56 struct perf_evsel
*cycles_evsel
;
60 int cycles_before_comm_1
;
61 int cycles_between_comm_2_and_comm_3
;
62 int cycles_after_comm_4
;
65 static int check_comm(struct switch_tracking
*switch_tracking
,
66 union perf_event
*event
, const char *comm
, int nr
)
68 if (event
->header
.type
== PERF_RECORD_COMM
&&
69 (pid_t
)event
->comm
.pid
== getpid() &&
70 (pid_t
)event
->comm
.tid
== getpid() &&
71 strcmp(event
->comm
.comm
, comm
) == 0) {
72 if (switch_tracking
->comm_seen
[nr
]) {
73 pr_debug("Duplicate comm event\n");
76 switch_tracking
->comm_seen
[nr
] = 1;
77 pr_debug3("comm event: %s nr: %d\n", event
->comm
.comm
, nr
);
83 static int check_cpu(struct switch_tracking
*switch_tracking
, int cpu
)
90 if (!switch_tracking
->tids
) {
91 switch_tracking
->tids
= calloc(nr
, sizeof(pid_t
));
92 if (!switch_tracking
->tids
)
94 for (i
= 0; i
< nr
; i
++)
95 switch_tracking
->tids
[i
] = -1;
96 switch_tracking
->nr_tids
= nr
;
100 if (cpu
>= switch_tracking
->nr_tids
) {
103 addr
= realloc(switch_tracking
->tids
, nr
* sizeof(pid_t
));
106 switch_tracking
->tids
= addr
;
107 for (i
= switch_tracking
->nr_tids
; i
< nr
; i
++)
108 switch_tracking
->tids
[i
] = -1;
109 switch_tracking
->nr_tids
= nr
;
116 static int process_sample_event(struct perf_evlist
*evlist
,
117 union perf_event
*event
,
118 struct switch_tracking
*switch_tracking
)
120 struct perf_sample sample
;
121 struct perf_evsel
*evsel
;
122 pid_t next_tid
, prev_tid
;
125 if (perf_evlist__parse_sample(evlist
, event
, &sample
)) {
126 pr_debug("perf_evlist__parse_sample failed\n");
130 evsel
= perf_evlist__id2evsel(evlist
, sample
.id
);
131 if (evsel
== switch_tracking
->switch_evsel
) {
132 next_tid
= perf_evsel__intval(evsel
, &sample
, "next_pid");
133 prev_tid
= perf_evsel__intval(evsel
, &sample
, "prev_pid");
135 pr_debug3("sched_switch: cpu: %d prev_tid %d next_tid %d\n",
136 cpu
, prev_tid
, next_tid
);
137 err
= check_cpu(switch_tracking
, cpu
);
141 * Check for no missing sched_switch events i.e. that the
142 * evsel->system_wide flag has worked.
144 if (switch_tracking
->tids
[cpu
] != -1 &&
145 switch_tracking
->tids
[cpu
] != prev_tid
) {
146 pr_debug("Missing sched_switch events\n");
149 switch_tracking
->tids
[cpu
] = next_tid
;
152 if (evsel
== switch_tracking
->cycles_evsel
) {
153 pr_debug3("cycles event\n");
154 if (!switch_tracking
->comm_seen
[0])
155 switch_tracking
->cycles_before_comm_1
= 1;
156 if (switch_tracking
->comm_seen
[1] &&
157 !switch_tracking
->comm_seen
[2])
158 switch_tracking
->cycles_between_comm_2_and_comm_3
= 1;
159 if (switch_tracking
->comm_seen
[3])
160 switch_tracking
->cycles_after_comm_4
= 1;
166 static int process_event(struct perf_evlist
*evlist
, union perf_event
*event
,
167 struct switch_tracking
*switch_tracking
)
169 if (event
->header
.type
== PERF_RECORD_SAMPLE
)
170 return process_sample_event(evlist
, event
, switch_tracking
);
172 if (event
->header
.type
== PERF_RECORD_COMM
) {
175 err
= check_comm(switch_tracking
, event
, "Test COMM 1", 0);
179 err
= check_comm(switch_tracking
, event
, "Test COMM 2", 1);
183 err
= check_comm(switch_tracking
, event
, "Test COMM 3", 2);
187 err
= check_comm(switch_tracking
, event
, "Test COMM 4", 3);
192 pr_debug("Unexpected comm event\n");
201 struct list_head list
;
202 union perf_event
*event
;
206 static int add_event(struct perf_evlist
*evlist
, struct list_head
*events
,
207 union perf_event
*event
)
209 struct perf_sample sample
;
210 struct event_node
*node
;
212 node
= malloc(sizeof(struct event_node
));
214 pr_debug("malloc failed\n");
218 list_add(&node
->list
, events
);
220 if (perf_evlist__parse_sample(evlist
, event
, &sample
)) {
221 pr_debug("perf_evlist__parse_sample failed\n");
226 pr_debug("event with no time\n");
230 node
->event_time
= sample
.time
;
235 static void free_event_nodes(struct list_head
*events
)
237 struct event_node
*node
;
239 while (!list_empty(events
)) {
240 node
= list_entry(events
->next
, struct event_node
, list
);
241 list_del_init(&node
->list
);
246 static int compar(const void *a
, const void *b
)
248 const struct event_node
*nodea
= a
;
249 const struct event_node
*nodeb
= b
;
250 s64 cmp
= nodea
->event_time
- nodeb
->event_time
;
255 static int process_events(struct perf_evlist
*evlist
,
256 struct switch_tracking
*switch_tracking
)
258 union perf_event
*event
;
259 unsigned pos
, cnt
= 0;
261 struct event_node
*events_array
, *node
;
262 struct perf_mmap
*md
;
265 for (i
= 0; i
< evlist
->nr_mmaps
; i
++) {
266 md
= &evlist
->mmap
[i
];
267 if (perf_mmap__read_init(md
) < 0)
270 while ((event
= perf_mmap__read_event(md
)) != NULL
) {
272 ret
= add_event(evlist
, &events
, event
);
273 perf_mmap__consume(md
);
277 perf_mmap__read_done(md
);
280 events_array
= calloc(cnt
, sizeof(struct event_node
));
282 pr_debug("calloc failed\n");
288 list_for_each_entry(node
, &events
, list
)
289 events_array
[pos
++] = *node
;
291 qsort(events_array
, cnt
, sizeof(struct event_node
), compar
);
293 for (pos
= 0; pos
< cnt
; pos
++) {
294 ret
= process_event(evlist
, events_array
[pos
].event
,
302 pr_debug("%u events recorded\n", cnt
);
305 free_event_nodes(&events
);
310 * test__switch_tracking - test using sched_switch and tracking events.
312 * This function implements a test that checks that sched_switch events and
313 * tracking events can be recorded for a workload (current process) using the
314 * evsel->system_wide and evsel->tracking flags (respectively) with other events
315 * sometimes enabled or disabled.
317 int test__switch_tracking(struct test
*test __maybe_unused
, int subtest __maybe_unused
)
319 const char *sched_switch
= "sched:sched_switch";
320 struct switch_tracking switch_tracking
= { .tids
= NULL
, };
321 struct record_opts opts
= {
322 .mmap_pages
= UINT_MAX
,
323 .user_freq
= UINT_MAX
,
324 .user_interval
= ULLONG_MAX
,
330 struct thread_map
*threads
= NULL
;
331 struct cpu_map
*cpus
= NULL
;
332 struct perf_evlist
*evlist
= NULL
;
333 struct perf_evsel
*evsel
, *cpu_clocks_evsel
, *cycles_evsel
;
334 struct perf_evsel
*switch_evsel
, *tracking_evsel
;
338 threads
= thread_map__new(-1, getpid(), UINT_MAX
);
340 pr_debug("thread_map__new failed!\n");
344 cpus
= cpu_map__new(NULL
);
346 pr_debug("cpu_map__new failed!\n");
350 evlist
= perf_evlist__new();
352 pr_debug("perf_evlist__new failed!\n");
356 perf_evlist__set_maps(evlist
, cpus
, threads
);
359 err
= parse_events(evlist
, "cpu-clock:u", NULL
);
361 pr_debug("Failed to parse event dummy:u\n");
365 cpu_clocks_evsel
= perf_evlist__last(evlist
);
368 err
= parse_events(evlist
, "cycles:u", NULL
);
370 pr_debug("Failed to parse event cycles:u\n");
374 cycles_evsel
= perf_evlist__last(evlist
);
377 if (!perf_evlist__can_select_event(evlist
, sched_switch
)) {
378 pr_debug("No sched_switch\n");
383 err
= parse_events(evlist
, sched_switch
, NULL
);
385 pr_debug("Failed to parse event %s\n", sched_switch
);
389 switch_evsel
= perf_evlist__last(evlist
);
391 perf_evsel__set_sample_bit(switch_evsel
, CPU
);
392 perf_evsel__set_sample_bit(switch_evsel
, TIME
);
394 switch_evsel
->system_wide
= true;
395 switch_evsel
->no_aux_samples
= true;
396 switch_evsel
->immediate
= true;
398 /* Test moving an event to the front */
399 if (cycles_evsel
== perf_evlist__first(evlist
)) {
400 pr_debug("cycles event already at front");
403 perf_evlist__to_front(evlist
, cycles_evsel
);
404 if (cycles_evsel
!= perf_evlist__first(evlist
)) {
405 pr_debug("Failed to move cycles event to front");
409 perf_evsel__set_sample_bit(cycles_evsel
, CPU
);
410 perf_evsel__set_sample_bit(cycles_evsel
, TIME
);
413 err
= parse_events(evlist
, "dummy:u", NULL
);
415 pr_debug("Failed to parse event dummy:u\n");
419 tracking_evsel
= perf_evlist__last(evlist
);
421 perf_evlist__set_tracking_event(evlist
, tracking_evsel
);
423 tracking_evsel
->attr
.freq
= 0;
424 tracking_evsel
->attr
.sample_period
= 1;
426 perf_evsel__set_sample_bit(tracking_evsel
, TIME
);
429 perf_evlist__config(evlist
, &opts
, NULL
);
431 /* Check moved event is still at the front */
432 if (cycles_evsel
!= perf_evlist__first(evlist
)) {
433 pr_debug("Front event no longer at front");
437 /* Check tracking event is tracking */
438 if (!tracking_evsel
->attr
.mmap
|| !tracking_evsel
->attr
.comm
) {
439 pr_debug("Tracking event not tracking\n");
443 /* Check non-tracking events are not tracking */
444 evlist__for_each_entry(evlist
, evsel
) {
445 if (evsel
!= tracking_evsel
) {
446 if (evsel
->attr
.mmap
|| evsel
->attr
.comm
) {
447 pr_debug("Non-tracking event is tracking\n");
453 if (perf_evlist__open(evlist
) < 0) {
454 pr_debug("Not supported\n");
459 err
= perf_evlist__mmap(evlist
, UINT_MAX
);
461 pr_debug("perf_evlist__mmap failed!\n");
465 perf_evlist__enable(evlist
);
467 err
= perf_evsel__disable(cpu_clocks_evsel
);
469 pr_debug("perf_evlist__disable_event failed!\n");
475 pr_debug("spin_sleep failed!\n");
479 comm
= "Test COMM 1";
480 err
= prctl(PR_SET_NAME
, (unsigned long)comm
, 0, 0, 0);
482 pr_debug("PR_SET_NAME failed!\n");
486 err
= perf_evsel__disable(cycles_evsel
);
488 pr_debug("perf_evlist__disable_event failed!\n");
492 comm
= "Test COMM 2";
493 err
= prctl(PR_SET_NAME
, (unsigned long)comm
, 0, 0, 0);
495 pr_debug("PR_SET_NAME failed!\n");
501 pr_debug("spin_sleep failed!\n");
505 comm
= "Test COMM 3";
506 err
= prctl(PR_SET_NAME
, (unsigned long)comm
, 0, 0, 0);
508 pr_debug("PR_SET_NAME failed!\n");
512 err
= perf_evsel__enable(cycles_evsel
);
514 pr_debug("perf_evlist__disable_event failed!\n");
518 comm
= "Test COMM 4";
519 err
= prctl(PR_SET_NAME
, (unsigned long)comm
, 0, 0, 0);
521 pr_debug("PR_SET_NAME failed!\n");
527 pr_debug("spin_sleep failed!\n");
531 perf_evlist__disable(evlist
);
533 switch_tracking
.switch_evsel
= switch_evsel
;
534 switch_tracking
.cycles_evsel
= cycles_evsel
;
536 err
= process_events(evlist
, &switch_tracking
);
538 zfree(&switch_tracking
.tids
);
543 /* Check all 4 comm events were seen i.e. that evsel->tracking works */
544 if (!switch_tracking
.comm_seen
[0] || !switch_tracking
.comm_seen
[1] ||
545 !switch_tracking
.comm_seen
[2] || !switch_tracking
.comm_seen
[3]) {
546 pr_debug("Missing comm events\n");
550 /* Check cycles event got enabled */
551 if (!switch_tracking
.cycles_before_comm_1
) {
552 pr_debug("Missing cycles events\n");
556 /* Check cycles event got disabled */
557 if (switch_tracking
.cycles_between_comm_2_and_comm_3
) {
558 pr_debug("cycles events even though event was disabled\n");
562 /* Check cycles event got enabled again */
563 if (!switch_tracking
.cycles_after_comm_4
) {
564 pr_debug("Missing cycles events\n");
569 perf_evlist__disable(evlist
);
570 perf_evlist__delete(evlist
);
573 thread_map__put(threads
);