6 #include "parse-events.h"
9 #include "thread_map.h"
13 static int spin_sleep(void)
15 struct timeval start
, now
, diff
, maxtime
;
20 maxtime
.tv_usec
= 50000;
22 err
= gettimeofday(&start
, NULL
);
28 for (i
= 0; i
< 1000; i
++)
31 err
= gettimeofday(&now
, NULL
);
35 timersub(&now
, &start
, &diff
);
36 if (timercmp(&diff
, &maxtime
, > /* For checkpatch */))
40 ts
.tv_nsec
= 50 * 1000 * 1000;
44 err
= nanosleep(&ts
, NULL
);
51 struct switch_tracking
{
52 struct perf_evsel
*switch_evsel
;
53 struct perf_evsel
*cycles_evsel
;
57 int cycles_before_comm_1
;
58 int cycles_between_comm_2_and_comm_3
;
59 int cycles_after_comm_4
;
62 static int check_comm(struct switch_tracking
*switch_tracking
,
63 union perf_event
*event
, const char *comm
, int nr
)
65 if (event
->header
.type
== PERF_RECORD_COMM
&&
66 (pid_t
)event
->comm
.pid
== getpid() &&
67 (pid_t
)event
->comm
.tid
== getpid() &&
68 strcmp(event
->comm
.comm
, comm
) == 0) {
69 if (switch_tracking
->comm_seen
[nr
]) {
70 pr_debug("Duplicate comm event\n");
73 switch_tracking
->comm_seen
[nr
] = 1;
74 pr_debug3("comm event: %s nr: %d\n", event
->comm
.comm
, nr
);
80 static int check_cpu(struct switch_tracking
*switch_tracking
, int cpu
)
87 if (!switch_tracking
->tids
) {
88 switch_tracking
->tids
= calloc(nr
, sizeof(pid_t
));
89 if (!switch_tracking
->tids
)
91 for (i
= 0; i
< nr
; i
++)
92 switch_tracking
->tids
[i
] = -1;
93 switch_tracking
->nr_tids
= nr
;
97 if (cpu
>= switch_tracking
->nr_tids
) {
100 addr
= realloc(switch_tracking
->tids
, nr
* sizeof(pid_t
));
103 switch_tracking
->tids
= addr
;
104 for (i
= switch_tracking
->nr_tids
; i
< nr
; i
++)
105 switch_tracking
->tids
[i
] = -1;
106 switch_tracking
->nr_tids
= nr
;
113 static int process_sample_event(struct perf_evlist
*evlist
,
114 union perf_event
*event
,
115 struct switch_tracking
*switch_tracking
)
117 struct perf_sample sample
;
118 struct perf_evsel
*evsel
;
119 pid_t next_tid
, prev_tid
;
122 if (perf_evlist__parse_sample(evlist
, event
, &sample
)) {
123 pr_debug("perf_evlist__parse_sample failed\n");
127 evsel
= perf_evlist__id2evsel(evlist
, sample
.id
);
128 if (evsel
== switch_tracking
->switch_evsel
) {
129 next_tid
= perf_evsel__intval(evsel
, &sample
, "next_pid");
130 prev_tid
= perf_evsel__intval(evsel
, &sample
, "prev_pid");
132 pr_debug3("sched_switch: cpu: %d prev_tid %d next_tid %d\n",
133 cpu
, prev_tid
, next_tid
);
134 err
= check_cpu(switch_tracking
, cpu
);
138 * Check for no missing sched_switch events i.e. that the
139 * evsel->system_wide flag has worked.
141 if (switch_tracking
->tids
[cpu
] != -1 &&
142 switch_tracking
->tids
[cpu
] != prev_tid
) {
143 pr_debug("Missing sched_switch events\n");
146 switch_tracking
->tids
[cpu
] = next_tid
;
149 if (evsel
== switch_tracking
->cycles_evsel
) {
150 pr_debug3("cycles event\n");
151 if (!switch_tracking
->comm_seen
[0])
152 switch_tracking
->cycles_before_comm_1
= 1;
153 if (switch_tracking
->comm_seen
[1] &&
154 !switch_tracking
->comm_seen
[2])
155 switch_tracking
->cycles_between_comm_2_and_comm_3
= 1;
156 if (switch_tracking
->comm_seen
[3])
157 switch_tracking
->cycles_after_comm_4
= 1;
163 static int process_event(struct perf_evlist
*evlist
, union perf_event
*event
,
164 struct switch_tracking
*switch_tracking
)
166 if (event
->header
.type
== PERF_RECORD_SAMPLE
)
167 return process_sample_event(evlist
, event
, switch_tracking
);
169 if (event
->header
.type
== PERF_RECORD_COMM
) {
172 err
= check_comm(switch_tracking
, event
, "Test COMM 1", 0);
176 err
= check_comm(switch_tracking
, event
, "Test COMM 2", 1);
180 err
= check_comm(switch_tracking
, event
, "Test COMM 3", 2);
184 err
= check_comm(switch_tracking
, event
, "Test COMM 4", 3);
189 pr_debug("Unexpected comm event\n");
198 struct list_head list
;
199 union perf_event
*event
;
203 static int add_event(struct perf_evlist
*evlist
, struct list_head
*events
,
204 union perf_event
*event
)
206 struct perf_sample sample
;
207 struct event_node
*node
;
209 node
= malloc(sizeof(struct event_node
));
211 pr_debug("malloc failed\n");
215 list_add(&node
->list
, events
);
217 if (perf_evlist__parse_sample(evlist
, event
, &sample
)) {
218 pr_debug("perf_evlist__parse_sample failed\n");
223 pr_debug("event with no time\n");
227 node
->event_time
= sample
.time
;
232 static void free_event_nodes(struct list_head
*events
)
234 struct event_node
*node
;
236 while (!list_empty(events
)) {
237 node
= list_entry(events
->next
, struct event_node
, list
);
238 list_del(&node
->list
);
243 static int compar(const void *a
, const void *b
)
245 const struct event_node
*nodea
= a
;
246 const struct event_node
*nodeb
= b
;
247 s64 cmp
= nodea
->event_time
- nodeb
->event_time
;
252 static int process_events(struct perf_evlist
*evlist
,
253 struct switch_tracking
*switch_tracking
)
255 union perf_event
*event
;
256 unsigned pos
, cnt
= 0;
258 struct event_node
*events_array
, *node
;
261 for (i
= 0; i
< evlist
->nr_mmaps
; i
++) {
262 while ((event
= perf_evlist__mmap_read(evlist
, i
)) != NULL
) {
264 ret
= add_event(evlist
, &events
, event
);
265 perf_evlist__mmap_consume(evlist
, i
);
271 events_array
= calloc(cnt
, sizeof(struct event_node
));
273 pr_debug("calloc failed\n");
279 list_for_each_entry(node
, &events
, list
)
280 events_array
[pos
++] = *node
;
282 qsort(events_array
, cnt
, sizeof(struct event_node
), compar
);
284 for (pos
= 0; pos
< cnt
; pos
++) {
285 ret
= process_event(evlist
, events_array
[pos
].event
,
293 pr_debug("%u events recorded\n", cnt
);
296 free_event_nodes(&events
);
301 * test__switch_tracking - test using sched_switch and tracking events.
303 * This function implements a test that checks that sched_switch events and
304 * tracking events can be recorded for a workload (current process) using the
305 * evsel->system_wide and evsel->tracking flags (respectively) with other events
306 * sometimes enabled or disabled.
308 int test__switch_tracking(void)
310 const char *sched_switch
= "sched:sched_switch";
311 struct switch_tracking switch_tracking
= { .tids
= NULL
, };
312 struct record_opts opts
= {
313 .mmap_pages
= UINT_MAX
,
314 .user_freq
= UINT_MAX
,
315 .user_interval
= ULLONG_MAX
,
321 struct thread_map
*threads
= NULL
;
322 struct cpu_map
*cpus
= NULL
;
323 struct perf_evlist
*evlist
= NULL
;
324 struct perf_evsel
*evsel
, *cpu_clocks_evsel
, *cycles_evsel
;
325 struct perf_evsel
*switch_evsel
, *tracking_evsel
;
329 threads
= thread_map__new(-1, getpid(), UINT_MAX
);
331 pr_debug("thread_map__new failed!\n");
335 cpus
= cpu_map__new(NULL
);
337 pr_debug("cpu_map__new failed!\n");
341 evlist
= perf_evlist__new();
343 pr_debug("perf_evlist__new failed!\n");
347 perf_evlist__set_maps(evlist
, cpus
, threads
);
350 err
= parse_events(evlist
, "cpu-clock:u");
352 pr_debug("Failed to parse event dummy:u\n");
356 cpu_clocks_evsel
= perf_evlist__last(evlist
);
359 err
= parse_events(evlist
, "cycles:u");
361 pr_debug("Failed to parse event cycles:u\n");
365 cycles_evsel
= perf_evlist__last(evlist
);
368 if (!perf_evlist__can_select_event(evlist
, sched_switch
)) {
369 fprintf(stderr
, " (no sched_switch)");
374 err
= parse_events(evlist
, sched_switch
);
376 pr_debug("Failed to parse event %s\n", sched_switch
);
380 switch_evsel
= perf_evlist__last(evlist
);
382 perf_evsel__set_sample_bit(switch_evsel
, CPU
);
383 perf_evsel__set_sample_bit(switch_evsel
, TIME
);
385 switch_evsel
->system_wide
= true;
386 switch_evsel
->no_aux_samples
= true;
387 switch_evsel
->immediate
= true;
389 /* Test moving an event to the front */
390 if (cycles_evsel
== perf_evlist__first(evlist
)) {
391 pr_debug("cycles event already at front");
394 perf_evlist__to_front(evlist
, cycles_evsel
);
395 if (cycles_evsel
!= perf_evlist__first(evlist
)) {
396 pr_debug("Failed to move cycles event to front");
400 perf_evsel__set_sample_bit(cycles_evsel
, CPU
);
401 perf_evsel__set_sample_bit(cycles_evsel
, TIME
);
404 err
= parse_events(evlist
, "dummy:u");
406 pr_debug("Failed to parse event dummy:u\n");
410 tracking_evsel
= perf_evlist__last(evlist
);
412 perf_evlist__set_tracking_event(evlist
, tracking_evsel
);
414 tracking_evsel
->attr
.freq
= 0;
415 tracking_evsel
->attr
.sample_period
= 1;
417 perf_evsel__set_sample_bit(tracking_evsel
, TIME
);
420 perf_evlist__config(evlist
, &opts
);
422 /* Check moved event is still at the front */
423 if (cycles_evsel
!= perf_evlist__first(evlist
)) {
424 pr_debug("Front event no longer at front");
428 /* Check tracking event is tracking */
429 if (!tracking_evsel
->attr
.mmap
|| !tracking_evsel
->attr
.comm
) {
430 pr_debug("Tracking event not tracking\n");
434 /* Check non-tracking events are not tracking */
435 evlist__for_each(evlist
, evsel
) {
436 if (evsel
!= tracking_evsel
) {
437 if (evsel
->attr
.mmap
|| evsel
->attr
.comm
) {
438 pr_debug("Non-tracking event is tracking\n");
444 if (perf_evlist__open(evlist
) < 0) {
445 fprintf(stderr
, " (not supported)");
450 err
= perf_evlist__mmap(evlist
, UINT_MAX
, false);
452 pr_debug("perf_evlist__mmap failed!\n");
456 perf_evlist__enable(evlist
);
458 err
= perf_evlist__disable_event(evlist
, cpu_clocks_evsel
);
460 pr_debug("perf_evlist__disable_event failed!\n");
466 pr_debug("spin_sleep failed!\n");
470 comm
= "Test COMM 1";
471 err
= prctl(PR_SET_NAME
, (unsigned long)comm
, 0, 0, 0);
473 pr_debug("PR_SET_NAME failed!\n");
477 err
= perf_evlist__disable_event(evlist
, cycles_evsel
);
479 pr_debug("perf_evlist__disable_event failed!\n");
483 comm
= "Test COMM 2";
484 err
= prctl(PR_SET_NAME
, (unsigned long)comm
, 0, 0, 0);
486 pr_debug("PR_SET_NAME failed!\n");
492 pr_debug("spin_sleep failed!\n");
496 comm
= "Test COMM 3";
497 err
= prctl(PR_SET_NAME
, (unsigned long)comm
, 0, 0, 0);
499 pr_debug("PR_SET_NAME failed!\n");
503 err
= perf_evlist__enable_event(evlist
, cycles_evsel
);
505 pr_debug("perf_evlist__disable_event failed!\n");
509 comm
= "Test COMM 4";
510 err
= prctl(PR_SET_NAME
, (unsigned long)comm
, 0, 0, 0);
512 pr_debug("PR_SET_NAME failed!\n");
518 pr_debug("spin_sleep failed!\n");
522 perf_evlist__disable(evlist
);
524 switch_tracking
.switch_evsel
= switch_evsel
;
525 switch_tracking
.cycles_evsel
= cycles_evsel
;
527 err
= process_events(evlist
, &switch_tracking
);
529 zfree(&switch_tracking
.tids
);
534 /* Check all 4 comm events were seen i.e. that evsel->tracking works */
535 if (!switch_tracking
.comm_seen
[0] || !switch_tracking
.comm_seen
[1] ||
536 !switch_tracking
.comm_seen
[2] || !switch_tracking
.comm_seen
[3]) {
537 pr_debug("Missing comm events\n");
541 /* Check cycles event got enabled */
542 if (!switch_tracking
.cycles_before_comm_1
) {
543 pr_debug("Missing cycles events\n");
547 /* Check cycles event got disabled */
548 if (switch_tracking
.cycles_between_comm_2_and_comm_3
) {
549 pr_debug("cycles events even though event was disabled\n");
553 /* Check cycles event got enabled again */
554 if (!switch_tracking
.cycles_after_comm_4
) {
555 pr_debug("Missing cycles events\n");
560 perf_evlist__disable(evlist
);
561 perf_evlist__delete(evlist
);
563 cpu_map__delete(cpus
);
564 thread_map__delete(threads
);