1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Copyright (C) 2011, Red Hat Inc, Arnaldo Carvalho de Melo <acme@redhat.com>
4 *
5 * Parts came from builtin-{top,stat,record}.c, see those files for further
6 * copyright notes.
7 */
8 #include <api/fs/fs.h>
9 #include <errno.h>
10 #include <inttypes.h>
11 #include <poll.h>
12 #include "cpumap.h"
13 #include "util/mmap.h"
14 #include "thread_map.h"
15 #include "target.h"
16 #include "evlist.h"
17 #include "evsel.h"
18 #include "record.h"
19 #include "debug.h"
20 #include "units.h"
21 #include "bpf_counter.h"
22 #include <internal/lib.h> // page_size
23 #include "affinity.h"
24 #include "../perf.h"
25 #include "asm/bug.h"
26 #include "bpf-event.h"
27 #include "util/event.h"
28 #include "util/string2.h"
29 #include "util/perf_api_probe.h"
30 #include "util/evsel_fprintf.h"
31 #include "util/pmu.h"
32 #include "util/sample.h"
33 #include "util/bpf-filter.h"
34 #include "util/stat.h"
35 #include "util/util.h"
36 #include "util/env.h"
37 #include "util/intel-tpebs.h"
38 #include "util/metricgroup.h"
39 #include "util/strbuf.h"
40 #include <signal.h>
41 #include <unistd.h>
42 #include <sched.h>
43 #include <stdlib.h>
44
45 #include "parse-events.h"
46 #include <subcmd/parse-options.h>
47
48 #include <fcntl.h>
49 #include <sys/ioctl.h>
50 #include <sys/mman.h>
51 #include <sys/prctl.h>
52 #include <sys/timerfd.h>
53 #include <sys/wait.h>
54
55 #include <linux/bitops.h>
56 #include <linux/hash.h>
57 #include <linux/log2.h>
58 #include <linux/err.h>
59 #include <linux/string.h>
60 #include <linux/time64.h>
61 #include <linux/zalloc.h>
62 #include <perf/evlist.h>
63 #include <perf/evsel.h>
64 #include <perf/cpumap.h>
65 #include <perf/mmap.h>
66
67 #include <internal/xyarray.h>
68
69 #ifdef LACKS_SIGQUEUE_PROTOTYPE
70 int sigqueue(pid_t pid, int sig, const union sigval value);
71 #endif
72
73 #define FD(e, x, y) (*(int *)xyarray__entry(e->core.fd, x, y))
74 #define SID(e, x, y) xyarray__entry(e->core.sample_id, x, y)
75
evlist__init(struct evlist * evlist,struct perf_cpu_map * cpus,struct perf_thread_map * threads)76 void evlist__init(struct evlist *evlist, struct perf_cpu_map *cpus,
77 struct perf_thread_map *threads)
78 {
79 perf_evlist__init(&evlist->core);
80 perf_evlist__set_maps(&evlist->core, cpus, threads);
81 evlist->workload.pid = -1;
82 evlist->bkw_mmap_state = BKW_MMAP_NOTREADY;
83 evlist->ctl_fd.fd = -1;
84 evlist->ctl_fd.ack = -1;
85 evlist->ctl_fd.pos = -1;
86 evlist->nr_br_cntr = -1;
87 metricgroup__rblist_init(&evlist->metric_events);
88 }
89
evlist__new(void)90 struct evlist *evlist__new(void)
91 {
92 struct evlist *evlist = zalloc(sizeof(*evlist));
93
94 if (evlist != NULL)
95 evlist__init(evlist, NULL, NULL);
96
97 return evlist;
98 }
99
evlist__new_default(void)100 struct evlist *evlist__new_default(void)
101 {
102 struct evlist *evlist = evlist__new();
103 bool can_profile_kernel;
104 int err;
105
106 if (!evlist)
107 return NULL;
108
109 can_profile_kernel = perf_event_paranoid_check(1);
110 err = parse_event(evlist, can_profile_kernel ? "cycles:P" : "cycles:Pu");
111 if (err) {
112 evlist__delete(evlist);
113 return NULL;
114 }
115
116 if (evlist->core.nr_entries > 1) {
117 struct evsel *evsel;
118
119 evlist__for_each_entry(evlist, evsel)
120 evsel__set_sample_id(evsel, /*can_sample_identifier=*/false);
121 }
122
123 return evlist;
124 }
125
evlist__new_dummy(void)126 struct evlist *evlist__new_dummy(void)
127 {
128 struct evlist *evlist = evlist__new();
129
130 if (evlist && evlist__add_dummy(evlist)) {
131 evlist__delete(evlist);
132 evlist = NULL;
133 }
134
135 return evlist;
136 }
137
138 /**
139 * evlist__set_id_pos - set the positions of event ids.
140 * @evlist: selected event list
141 *
142 * Events with compatible sample types all have the same id_pos
143 * and is_pos. For convenience, put a copy on evlist.
144 */
evlist__set_id_pos(struct evlist * evlist)145 void evlist__set_id_pos(struct evlist *evlist)
146 {
147 struct evsel *first = evlist__first(evlist);
148
149 evlist->id_pos = first->id_pos;
150 evlist->is_pos = first->is_pos;
151 }
152
evlist__update_id_pos(struct evlist * evlist)153 static void evlist__update_id_pos(struct evlist *evlist)
154 {
155 struct evsel *evsel;
156
157 evlist__for_each_entry(evlist, evsel)
158 evsel__calc_id_pos(evsel);
159
160 evlist__set_id_pos(evlist);
161 }
162
evlist__purge(struct evlist * evlist)163 static void evlist__purge(struct evlist *evlist)
164 {
165 struct evsel *pos, *n;
166
167 evlist__for_each_entry_safe(evlist, n, pos) {
168 list_del_init(&pos->core.node);
169 pos->evlist = NULL;
170 evsel__delete(pos);
171 }
172
173 evlist->core.nr_entries = 0;
174 }
175
evlist__exit(struct evlist * evlist)176 void evlist__exit(struct evlist *evlist)
177 {
178 metricgroup__rblist_exit(&evlist->metric_events);
179 event_enable_timer__exit(&evlist->eet);
180 zfree(&evlist->mmap);
181 zfree(&evlist->overwrite_mmap);
182 perf_evlist__exit(&evlist->core);
183 }
184
evlist__delete(struct evlist * evlist)185 void evlist__delete(struct evlist *evlist)
186 {
187 if (evlist == NULL)
188 return;
189
190 evlist__free_stats(evlist);
191 evlist__munmap(evlist);
192 evlist__close(evlist);
193 evlist__purge(evlist);
194 evlist__exit(evlist);
195 free(evlist);
196 }
197
evlist__add(struct evlist * evlist,struct evsel * entry)198 void evlist__add(struct evlist *evlist, struct evsel *entry)
199 {
200 perf_evlist__add(&evlist->core, &entry->core);
201 entry->evlist = evlist;
202 entry->tracking = !entry->core.idx;
203
204 if (evlist->core.nr_entries == 1)
205 evlist__set_id_pos(evlist);
206 }
207
evlist__remove(struct evlist * evlist,struct evsel * evsel)208 void evlist__remove(struct evlist *evlist, struct evsel *evsel)
209 {
210 evsel->evlist = NULL;
211 perf_evlist__remove(&evlist->core, &evsel->core);
212 }
213
evlist__splice_list_tail(struct evlist * evlist,struct list_head * list)214 void evlist__splice_list_tail(struct evlist *evlist, struct list_head *list)
215 {
216 while (!list_empty(list)) {
217 struct evsel *evsel, *temp, *leader = NULL;
218
219 __evlist__for_each_entry_safe(list, temp, evsel) {
220 list_del_init(&evsel->core.node);
221 evlist__add(evlist, evsel);
222 leader = evsel;
223 break;
224 }
225
226 __evlist__for_each_entry_safe(list, temp, evsel) {
227 if (evsel__has_leader(evsel, leader)) {
228 list_del_init(&evsel->core.node);
229 evlist__add(evlist, evsel);
230 }
231 }
232 }
233 }
234
__evlist__set_tracepoints_handlers(struct evlist * evlist,const struct evsel_str_handler * assocs,size_t nr_assocs)235 int __evlist__set_tracepoints_handlers(struct evlist *evlist,
236 const struct evsel_str_handler *assocs, size_t nr_assocs)
237 {
238 size_t i;
239 int err;
240
241 for (i = 0; i < nr_assocs; i++) {
242 // Adding a handler for an event not in this evlist, just ignore it.
243 struct evsel *evsel = evlist__find_tracepoint_by_name(evlist, assocs[i].name);
244 if (evsel == NULL)
245 continue;
246
247 err = -EEXIST;
248 if (evsel->handler != NULL)
249 goto out;
250 evsel->handler = assocs[i].handler;
251 }
252
253 err = 0;
254 out:
255 return err;
256 }
257
evlist__set_leader(struct evlist * evlist)258 static void evlist__set_leader(struct evlist *evlist)
259 {
260 perf_evlist__set_leader(&evlist->core);
261 }
262
evlist__dummy_event(struct evlist * evlist)263 static struct evsel *evlist__dummy_event(struct evlist *evlist)
264 {
265 struct perf_event_attr attr = {
266 .type = PERF_TYPE_SOFTWARE,
267 .config = PERF_COUNT_SW_DUMMY,
268 .size = sizeof(attr), /* to capture ABI version */
269 /* Avoid frequency mode for dummy events to avoid associated timers. */
270 .freq = 0,
271 .sample_period = 1,
272 };
273
274 return evsel__new_idx(&attr, evlist->core.nr_entries);
275 }
276
evlist__add_dummy(struct evlist * evlist)277 int evlist__add_dummy(struct evlist *evlist)
278 {
279 struct evsel *evsel = evlist__dummy_event(evlist);
280
281 if (evsel == NULL)
282 return -ENOMEM;
283
284 evlist__add(evlist, evsel);
285 return 0;
286 }
287
evlist__add_aux_dummy(struct evlist * evlist,bool system_wide)288 struct evsel *evlist__add_aux_dummy(struct evlist *evlist, bool system_wide)
289 {
290 struct evsel *evsel = evlist__dummy_event(evlist);
291
292 if (!evsel)
293 return NULL;
294
295 evsel->core.attr.exclude_kernel = 1;
296 evsel->core.attr.exclude_guest = 1;
297 evsel->core.attr.exclude_hv = 1;
298 evsel->core.system_wide = system_wide;
299 evsel->no_aux_samples = true;
300 evsel->name = strdup("dummy:u");
301
302 evlist__add(evlist, evsel);
303 return evsel;
304 }
305
306 #ifdef HAVE_LIBTRACEEVENT
evlist__add_sched_switch(struct evlist * evlist,bool system_wide)307 struct evsel *evlist__add_sched_switch(struct evlist *evlist, bool system_wide)
308 {
309 struct evsel *evsel = evsel__newtp_idx("sched", "sched_switch", 0,
310 /*format=*/true);
311
312 if (IS_ERR(evsel))
313 return evsel;
314
315 evsel__set_sample_bit(evsel, CPU);
316 evsel__set_sample_bit(evsel, TIME);
317
318 evsel->core.system_wide = system_wide;
319 evsel->no_aux_samples = true;
320
321 evlist__add(evlist, evsel);
322 return evsel;
323 }
324 #endif
325
evlist__find_tracepoint_by_name(struct evlist * evlist,const char * name)326 struct evsel *evlist__find_tracepoint_by_name(struct evlist *evlist, const char *name)
327 {
328 struct evsel *evsel;
329
330 evlist__for_each_entry(evlist, evsel) {
331 if ((evsel->core.attr.type == PERF_TYPE_TRACEPOINT) &&
332 (strcmp(evsel->name, name) == 0))
333 return evsel;
334 }
335
336 return NULL;
337 }
338
339 #ifdef HAVE_LIBTRACEEVENT
evlist__add_newtp(struct evlist * evlist,const char * sys,const char * name,void * handler)340 int evlist__add_newtp(struct evlist *evlist, const char *sys, const char *name, void *handler)
341 {
342 struct evsel *evsel = evsel__newtp(sys, name);
343
344 if (IS_ERR(evsel))
345 return -1;
346
347 evsel->handler = handler;
348 evlist__add(evlist, evsel);
349 return 0;
350 }
351 #endif
352
evlist__cpu_begin(struct evlist * evlist,struct affinity * affinity)353 struct evlist_cpu_iterator evlist__cpu_begin(struct evlist *evlist, struct affinity *affinity)
354 {
355 struct evlist_cpu_iterator itr = {
356 .container = evlist,
357 .evsel = NULL,
358 .cpu_map_idx = 0,
359 .evlist_cpu_map_idx = 0,
360 .evlist_cpu_map_nr = perf_cpu_map__nr(evlist->core.all_cpus),
361 .cpu = (struct perf_cpu){ .cpu = -1},
362 .affinity = affinity,
363 };
364
365 if (evlist__empty(evlist)) {
366 /* Ensure the empty list doesn't iterate. */
367 itr.evlist_cpu_map_idx = itr.evlist_cpu_map_nr;
368 } else {
369 itr.evsel = evlist__first(evlist);
370 if (itr.affinity) {
371 itr.cpu = perf_cpu_map__cpu(evlist->core.all_cpus, 0);
372 affinity__set(itr.affinity, itr.cpu.cpu);
373 itr.cpu_map_idx = perf_cpu_map__idx(itr.evsel->core.cpus, itr.cpu);
374 /*
375 * If this CPU isn't in the evsel's cpu map then advance
376 * through the list.
377 */
378 if (itr.cpu_map_idx == -1)
379 evlist_cpu_iterator__next(&itr);
380 }
381 }
382 return itr;
383 }
384
evlist_cpu_iterator__next(struct evlist_cpu_iterator * evlist_cpu_itr)385 void evlist_cpu_iterator__next(struct evlist_cpu_iterator *evlist_cpu_itr)
386 {
387 while (evlist_cpu_itr->evsel != evlist__last(evlist_cpu_itr->container)) {
388 evlist_cpu_itr->evsel = evsel__next(evlist_cpu_itr->evsel);
389 evlist_cpu_itr->cpu_map_idx =
390 perf_cpu_map__idx(evlist_cpu_itr->evsel->core.cpus,
391 evlist_cpu_itr->cpu);
392 if (evlist_cpu_itr->cpu_map_idx != -1)
393 return;
394 }
395 evlist_cpu_itr->evlist_cpu_map_idx++;
396 if (evlist_cpu_itr->evlist_cpu_map_idx < evlist_cpu_itr->evlist_cpu_map_nr) {
397 evlist_cpu_itr->evsel = evlist__first(evlist_cpu_itr->container);
398 evlist_cpu_itr->cpu =
399 perf_cpu_map__cpu(evlist_cpu_itr->container->core.all_cpus,
400 evlist_cpu_itr->evlist_cpu_map_idx);
401 if (evlist_cpu_itr->affinity)
402 affinity__set(evlist_cpu_itr->affinity, evlist_cpu_itr->cpu.cpu);
403 evlist_cpu_itr->cpu_map_idx =
404 perf_cpu_map__idx(evlist_cpu_itr->evsel->core.cpus,
405 evlist_cpu_itr->cpu);
406 /*
407 * If this CPU isn't in the evsel's cpu map then advance through
408 * the list.
409 */
410 if (evlist_cpu_itr->cpu_map_idx == -1)
411 evlist_cpu_iterator__next(evlist_cpu_itr);
412 }
413 }
414
evlist_cpu_iterator__end(const struct evlist_cpu_iterator * evlist_cpu_itr)415 bool evlist_cpu_iterator__end(const struct evlist_cpu_iterator *evlist_cpu_itr)
416 {
417 return evlist_cpu_itr->evlist_cpu_map_idx >= evlist_cpu_itr->evlist_cpu_map_nr;
418 }
419
evsel__strcmp(struct evsel * pos,char * evsel_name)420 static int evsel__strcmp(struct evsel *pos, char *evsel_name)
421 {
422 if (!evsel_name)
423 return 0;
424 if (evsel__is_dummy_event(pos))
425 return 1;
426 return !evsel__name_is(pos, evsel_name);
427 }
428
evlist__is_enabled(struct evlist * evlist)429 static int evlist__is_enabled(struct evlist *evlist)
430 {
431 struct evsel *pos;
432
433 evlist__for_each_entry(evlist, pos) {
434 if (!evsel__is_group_leader(pos) || !pos->core.fd)
435 continue;
436 /* If at least one event is enabled, evlist is enabled. */
437 if (!pos->disabled)
438 return true;
439 }
440 return false;
441 }
442
__evlist__disable(struct evlist * evlist,char * evsel_name,bool excl_dummy)443 static void __evlist__disable(struct evlist *evlist, char *evsel_name, bool excl_dummy)
444 {
445 struct evsel *pos;
446 struct evlist_cpu_iterator evlist_cpu_itr;
447 struct affinity saved_affinity, *affinity = NULL;
448 bool has_imm = false;
449
450 // See explanation in evlist__close()
451 if (!cpu_map__is_dummy(evlist->core.user_requested_cpus)) {
452 if (affinity__setup(&saved_affinity) < 0)
453 return;
454 affinity = &saved_affinity;
455 }
456
457 /* Disable 'immediate' events last */
458 for (int imm = 0; imm <= 1; imm++) {
459 evlist__for_each_cpu(evlist_cpu_itr, evlist, affinity) {
460 pos = evlist_cpu_itr.evsel;
461 if (evsel__strcmp(pos, evsel_name))
462 continue;
463 if (pos->disabled || !evsel__is_group_leader(pos) || !pos->core.fd)
464 continue;
465 if (excl_dummy && evsel__is_dummy_event(pos))
466 continue;
467 if (pos->immediate)
468 has_imm = true;
469 if (pos->immediate != imm)
470 continue;
471 evsel__disable_cpu(pos, evlist_cpu_itr.cpu_map_idx);
472 }
473 if (!has_imm)
474 break;
475 }
476
477 affinity__cleanup(affinity);
478 evlist__for_each_entry(evlist, pos) {
479 if (evsel__strcmp(pos, evsel_name))
480 continue;
481 if (!evsel__is_group_leader(pos) || !pos->core.fd)
482 continue;
483 if (excl_dummy && evsel__is_dummy_event(pos))
484 continue;
485 pos->disabled = true;
486 }
487
488 /*
489 * If we disabled only single event, we need to check
490 * the enabled state of the evlist manually.
491 */
492 if (evsel_name)
493 evlist->enabled = evlist__is_enabled(evlist);
494 else
495 evlist->enabled = false;
496 }
497
evlist__disable(struct evlist * evlist)498 void evlist__disable(struct evlist *evlist)
499 {
500 __evlist__disable(evlist, NULL, false);
501 }
502
evlist__disable_non_dummy(struct evlist * evlist)503 void evlist__disable_non_dummy(struct evlist *evlist)
504 {
505 __evlist__disable(evlist, NULL, true);
506 }
507
evlist__disable_evsel(struct evlist * evlist,char * evsel_name)508 void evlist__disable_evsel(struct evlist *evlist, char *evsel_name)
509 {
510 __evlist__disable(evlist, evsel_name, false);
511 }
512
__evlist__enable(struct evlist * evlist,char * evsel_name,bool excl_dummy)513 static void __evlist__enable(struct evlist *evlist, char *evsel_name, bool excl_dummy)
514 {
515 struct evsel *pos;
516 struct evlist_cpu_iterator evlist_cpu_itr;
517 struct affinity saved_affinity, *affinity = NULL;
518
519 // See explanation in evlist__close()
520 if (!cpu_map__is_dummy(evlist->core.user_requested_cpus)) {
521 if (affinity__setup(&saved_affinity) < 0)
522 return;
523 affinity = &saved_affinity;
524 }
525
526 evlist__for_each_cpu(evlist_cpu_itr, evlist, affinity) {
527 pos = evlist_cpu_itr.evsel;
528 if (evsel__strcmp(pos, evsel_name))
529 continue;
530 if (!evsel__is_group_leader(pos) || !pos->core.fd)
531 continue;
532 if (excl_dummy && evsel__is_dummy_event(pos))
533 continue;
534 evsel__enable_cpu(pos, evlist_cpu_itr.cpu_map_idx);
535 }
536 affinity__cleanup(affinity);
537 evlist__for_each_entry(evlist, pos) {
538 if (evsel__strcmp(pos, evsel_name))
539 continue;
540 if (!evsel__is_group_leader(pos) || !pos->core.fd)
541 continue;
542 if (excl_dummy && evsel__is_dummy_event(pos))
543 continue;
544 pos->disabled = false;
545 }
546
547 /*
548 * Even single event sets the 'enabled' for evlist,
549 * so the toggle can work properly and toggle to
550 * 'disabled' state.
551 */
552 evlist->enabled = true;
553 }
554
evlist__enable(struct evlist * evlist)555 void evlist__enable(struct evlist *evlist)
556 {
557 __evlist__enable(evlist, NULL, false);
558 }
559
evlist__enable_non_dummy(struct evlist * evlist)560 void evlist__enable_non_dummy(struct evlist *evlist)
561 {
562 __evlist__enable(evlist, NULL, true);
563 }
564
evlist__enable_evsel(struct evlist * evlist,char * evsel_name)565 void evlist__enable_evsel(struct evlist *evlist, char *evsel_name)
566 {
567 __evlist__enable(evlist, evsel_name, false);
568 }
569
evlist__toggle_enable(struct evlist * evlist)570 void evlist__toggle_enable(struct evlist *evlist)
571 {
572 (evlist->enabled ? evlist__disable : evlist__enable)(evlist);
573 }
574
evlist__add_pollfd(struct evlist * evlist,int fd)575 int evlist__add_pollfd(struct evlist *evlist, int fd)
576 {
577 return perf_evlist__add_pollfd(&evlist->core, fd, NULL, POLLIN, fdarray_flag__default);
578 }
579
evlist__filter_pollfd(struct evlist * evlist,short revents_and_mask)580 int evlist__filter_pollfd(struct evlist *evlist, short revents_and_mask)
581 {
582 return perf_evlist__filter_pollfd(&evlist->core, revents_and_mask);
583 }
584
585 #ifdef HAVE_EVENTFD_SUPPORT
evlist__add_wakeup_eventfd(struct evlist * evlist,int fd)586 int evlist__add_wakeup_eventfd(struct evlist *evlist, int fd)
587 {
588 return perf_evlist__add_pollfd(&evlist->core, fd, NULL, POLLIN,
589 fdarray_flag__nonfilterable |
590 fdarray_flag__non_perf_event);
591 }
592 #endif
593
evlist__poll(struct evlist * evlist,int timeout)594 int evlist__poll(struct evlist *evlist, int timeout)
595 {
596 return perf_evlist__poll(&evlist->core, timeout);
597 }
598
evlist__id2sid(struct evlist * evlist,u64 id)599 struct perf_sample_id *evlist__id2sid(struct evlist *evlist, u64 id)
600 {
601 struct hlist_head *head;
602 struct perf_sample_id *sid;
603 int hash;
604
605 hash = hash_64(id, PERF_EVLIST__HLIST_BITS);
606 head = &evlist->core.heads[hash];
607
608 hlist_for_each_entry(sid, head, node)
609 if (sid->id == id)
610 return sid;
611
612 return NULL;
613 }
614
evlist__id2evsel(struct evlist * evlist,u64 id)615 struct evsel *evlist__id2evsel(struct evlist *evlist, u64 id)
616 {
617 struct perf_sample_id *sid;
618
619 if (evlist->core.nr_entries == 1 || !id)
620 return evlist__first(evlist);
621
622 sid = evlist__id2sid(evlist, id);
623 if (sid)
624 return container_of(sid->evsel, struct evsel, core);
625
626 if (!evlist__sample_id_all(evlist))
627 return evlist__first(evlist);
628
629 return NULL;
630 }
631
evlist__id2evsel_strict(struct evlist * evlist,u64 id)632 struct evsel *evlist__id2evsel_strict(struct evlist *evlist, u64 id)
633 {
634 struct perf_sample_id *sid;
635
636 if (!id)
637 return NULL;
638
639 sid = evlist__id2sid(evlist, id);
640 if (sid)
641 return container_of(sid->evsel, struct evsel, core);
642
643 return NULL;
644 }
645
evlist__event2id(struct evlist * evlist,union perf_event * event,u64 * id)646 static int evlist__event2id(struct evlist *evlist, union perf_event *event, u64 *id)
647 {
648 const __u64 *array = event->sample.array;
649 ssize_t n;
650
651 n = (event->header.size - sizeof(event->header)) >> 3;
652
653 if (event->header.type == PERF_RECORD_SAMPLE) {
654 if (evlist->id_pos >= n)
655 return -1;
656 *id = array[evlist->id_pos];
657 } else {
658 if (evlist->is_pos > n)
659 return -1;
660 n -= evlist->is_pos;
661 *id = array[n];
662 }
663 return 0;
664 }
665
evlist__event2evsel(struct evlist * evlist,union perf_event * event)666 struct evsel *evlist__event2evsel(struct evlist *evlist, union perf_event *event)
667 {
668 struct evsel *first = evlist__first(evlist);
669 struct hlist_head *head;
670 struct perf_sample_id *sid;
671 int hash;
672 u64 id;
673
674 if (evlist->core.nr_entries == 1)
675 return first;
676
677 if (!first->core.attr.sample_id_all &&
678 event->header.type != PERF_RECORD_SAMPLE)
679 return first;
680
681 if (evlist__event2id(evlist, event, &id))
682 return NULL;
683
684 /* Synthesized events have an id of zero */
685 if (!id)
686 return first;
687
688 hash = hash_64(id, PERF_EVLIST__HLIST_BITS);
689 head = &evlist->core.heads[hash];
690
691 hlist_for_each_entry(sid, head, node) {
692 if (sid->id == id)
693 return container_of(sid->evsel, struct evsel, core);
694 }
695 return NULL;
696 }
697
evlist__set_paused(struct evlist * evlist,bool value)698 static int evlist__set_paused(struct evlist *evlist, bool value)
699 {
700 int i;
701
702 if (!evlist->overwrite_mmap)
703 return 0;
704
705 for (i = 0; i < evlist->core.nr_mmaps; i++) {
706 int fd = evlist->overwrite_mmap[i].core.fd;
707 int err;
708
709 if (fd < 0)
710 continue;
711 err = ioctl(fd, PERF_EVENT_IOC_PAUSE_OUTPUT, value ? 1 : 0);
712 if (err)
713 return err;
714 }
715 return 0;
716 }
717
evlist__pause(struct evlist * evlist)718 static int evlist__pause(struct evlist *evlist)
719 {
720 return evlist__set_paused(evlist, true);
721 }
722
evlist__resume(struct evlist * evlist)723 static int evlist__resume(struct evlist *evlist)
724 {
725 return evlist__set_paused(evlist, false);
726 }
727
evlist__munmap_nofree(struct evlist * evlist)728 static void evlist__munmap_nofree(struct evlist *evlist)
729 {
730 int i;
731
732 if (evlist->mmap)
733 for (i = 0; i < evlist->core.nr_mmaps; i++)
734 perf_mmap__munmap(&evlist->mmap[i].core);
735
736 if (evlist->overwrite_mmap)
737 for (i = 0; i < evlist->core.nr_mmaps; i++)
738 perf_mmap__munmap(&evlist->overwrite_mmap[i].core);
739 }
740
evlist__munmap(struct evlist * evlist)741 void evlist__munmap(struct evlist *evlist)
742 {
743 evlist__munmap_nofree(evlist);
744 zfree(&evlist->mmap);
745 zfree(&evlist->overwrite_mmap);
746 }
747
perf_mmap__unmap_cb(struct perf_mmap * map)748 static void perf_mmap__unmap_cb(struct perf_mmap *map)
749 {
750 struct mmap *m = container_of(map, struct mmap, core);
751
752 mmap__munmap(m);
753 }
754
evlist__alloc_mmap(struct evlist * evlist,bool overwrite)755 static struct mmap *evlist__alloc_mmap(struct evlist *evlist,
756 bool overwrite)
757 {
758 int i;
759 struct mmap *map;
760
761 map = zalloc(evlist->core.nr_mmaps * sizeof(struct mmap));
762 if (!map)
763 return NULL;
764
765 for (i = 0; i < evlist->core.nr_mmaps; i++) {
766 struct perf_mmap *prev = i ? &map[i - 1].core : NULL;
767
768 /*
769 * When the perf_mmap() call is made we grab one refcount, plus
770 * one extra to let perf_mmap__consume() get the last
771 * events after all real references (perf_mmap__get()) are
772 * dropped.
773 *
774 * Each PERF_EVENT_IOC_SET_OUTPUT points to this mmap and
775 * thus does perf_mmap__get() on it.
776 */
777 perf_mmap__init(&map[i].core, prev, overwrite, perf_mmap__unmap_cb);
778 }
779
780 return map;
781 }
782
783 static void
perf_evlist__mmap_cb_idx(struct perf_evlist * _evlist,struct perf_evsel * _evsel,struct perf_mmap_param * _mp,int idx)784 perf_evlist__mmap_cb_idx(struct perf_evlist *_evlist,
785 struct perf_evsel *_evsel,
786 struct perf_mmap_param *_mp,
787 int idx)
788 {
789 struct evlist *evlist = container_of(_evlist, struct evlist, core);
790 struct mmap_params *mp = container_of(_mp, struct mmap_params, core);
791 struct evsel *evsel = container_of(_evsel, struct evsel, core);
792
793 auxtrace_mmap_params__set_idx(&mp->auxtrace_mp, evlist, evsel, idx);
794 }
795
796 static struct perf_mmap*
perf_evlist__mmap_cb_get(struct perf_evlist * _evlist,bool overwrite,int idx)797 perf_evlist__mmap_cb_get(struct perf_evlist *_evlist, bool overwrite, int idx)
798 {
799 struct evlist *evlist = container_of(_evlist, struct evlist, core);
800 struct mmap *maps;
801
802 maps = overwrite ? evlist->overwrite_mmap : evlist->mmap;
803
804 if (!maps) {
805 maps = evlist__alloc_mmap(evlist, overwrite);
806 if (!maps)
807 return NULL;
808
809 if (overwrite) {
810 evlist->overwrite_mmap = maps;
811 if (evlist->bkw_mmap_state == BKW_MMAP_NOTREADY)
812 evlist__toggle_bkw_mmap(evlist, BKW_MMAP_RUNNING);
813 } else {
814 evlist->mmap = maps;
815 }
816 }
817
818 return &maps[idx].core;
819 }
820
821 static int
perf_evlist__mmap_cb_mmap(struct perf_mmap * _map,struct perf_mmap_param * _mp,int output,struct perf_cpu cpu)822 perf_evlist__mmap_cb_mmap(struct perf_mmap *_map, struct perf_mmap_param *_mp,
823 int output, struct perf_cpu cpu)
824 {
825 struct mmap *map = container_of(_map, struct mmap, core);
826 struct mmap_params *mp = container_of(_mp, struct mmap_params, core);
827
828 return mmap__mmap(map, mp, output, cpu);
829 }
830
perf_event_mlock_kb_in_pages(void)831 unsigned long perf_event_mlock_kb_in_pages(void)
832 {
833 unsigned long pages;
834 int max;
835
836 if (sysctl__read_int("kernel/perf_event_mlock_kb", &max) < 0) {
837 /*
838 * Pick a once upon a time good value, i.e. things look
839 * strange since we can't read a sysctl value, but lets not
840 * die yet...
841 */
842 max = 512;
843 } else {
844 max -= (page_size / 1024);
845 }
846
847 pages = (max * 1024) / page_size;
848 if (!is_power_of_2(pages))
849 pages = rounddown_pow_of_two(pages);
850
851 return pages;
852 }
853
evlist__mmap_size(unsigned long pages)854 size_t evlist__mmap_size(unsigned long pages)
855 {
856 if (pages == UINT_MAX)
857 pages = perf_event_mlock_kb_in_pages();
858 else if (!is_power_of_2(pages))
859 return 0;
860
861 return (pages + 1) * page_size;
862 }
863
parse_pages_arg(const char * str,unsigned long min,unsigned long max)864 static long parse_pages_arg(const char *str, unsigned long min,
865 unsigned long max)
866 {
867 unsigned long pages, val;
868 static struct parse_tag tags[] = {
869 { .tag = 'B', .mult = 1 },
870 { .tag = 'K', .mult = 1 << 10 },
871 { .tag = 'M', .mult = 1 << 20 },
872 { .tag = 'G', .mult = 1 << 30 },
873 { .tag = 0 },
874 };
875
876 if (str == NULL)
877 return -EINVAL;
878
879 val = parse_tag_value(str, tags);
880 if (val != (unsigned long) -1) {
881 /* we got file size value */
882 pages = PERF_ALIGN(val, page_size) / page_size;
883 } else {
884 /* we got pages count value */
885 char *eptr;
886 pages = strtoul(str, &eptr, 10);
887 if (*eptr != '\0')
888 return -EINVAL;
889 }
890
891 if (pages == 0 && min == 0) {
892 /* leave number of pages at 0 */
893 } else if (!is_power_of_2(pages)) {
894 char buf[100];
895
896 /* round pages up to next power of 2 */
897 pages = roundup_pow_of_two(pages);
898 if (!pages)
899 return -EINVAL;
900
901 unit_number__scnprintf(buf, sizeof(buf), pages * page_size);
902 pr_info("rounding mmap pages size to %s (%lu pages)\n",
903 buf, pages);
904 }
905
906 if (pages > max)
907 return -EINVAL;
908
909 return pages;
910 }
911
__evlist__parse_mmap_pages(unsigned int * mmap_pages,const char * str)912 int __evlist__parse_mmap_pages(unsigned int *mmap_pages, const char *str)
913 {
914 unsigned long max = UINT_MAX;
915 long pages;
916
917 if (max > SIZE_MAX / page_size)
918 max = SIZE_MAX / page_size;
919
920 pages = parse_pages_arg(str, 1, max);
921 if (pages < 0) {
922 pr_err("Invalid argument for --mmap_pages/-m\n");
923 return -1;
924 }
925
926 *mmap_pages = pages;
927 return 0;
928 }
929
evlist__parse_mmap_pages(const struct option * opt,const char * str,int unset __maybe_unused)930 int evlist__parse_mmap_pages(const struct option *opt, const char *str, int unset __maybe_unused)
931 {
932 return __evlist__parse_mmap_pages(opt->value, str);
933 }
934
935 /**
936 * evlist__mmap_ex - Create mmaps to receive events.
937 * @evlist: list of events
938 * @pages: map length in pages
939 * @overwrite: overwrite older events?
940 * @auxtrace_pages - auxtrace map length in pages
941 * @auxtrace_overwrite - overwrite older auxtrace data?
942 *
943 * If @overwrite is %false the user needs to signal event consumption using
944 * perf_mmap__write_tail(). Using evlist__mmap_read() does this
945 * automatically.
946 *
947 * Similarly, if @auxtrace_overwrite is %false the user needs to signal data
948 * consumption using auxtrace_mmap__write_tail().
949 *
950 * Return: %0 on success, negative error code otherwise.
951 */
evlist__mmap_ex(struct evlist * evlist,unsigned int pages,unsigned int auxtrace_pages,bool auxtrace_overwrite,int nr_cblocks,int affinity,int flush,int comp_level)952 int evlist__mmap_ex(struct evlist *evlist, unsigned int pages,
953 unsigned int auxtrace_pages,
954 bool auxtrace_overwrite, int nr_cblocks, int affinity, int flush,
955 int comp_level)
956 {
957 /*
958 * Delay setting mp.prot: set it before calling perf_mmap__mmap.
959 * Its value is decided by evsel's write_backward.
960 * So &mp should not be passed through const pointer.
961 */
962 struct mmap_params mp = {
963 .nr_cblocks = nr_cblocks,
964 .affinity = affinity,
965 .flush = flush,
966 .comp_level = comp_level
967 };
968 struct perf_evlist_mmap_ops ops = {
969 .idx = perf_evlist__mmap_cb_idx,
970 .get = perf_evlist__mmap_cb_get,
971 .mmap = perf_evlist__mmap_cb_mmap,
972 };
973
974 evlist->core.mmap_len = evlist__mmap_size(pages);
975 pr_debug("mmap size %zuB\n", evlist->core.mmap_len);
976
977 auxtrace_mmap_params__init(&mp.auxtrace_mp, evlist->core.mmap_len,
978 auxtrace_pages, auxtrace_overwrite);
979
980 return perf_evlist__mmap_ops(&evlist->core, &ops, &mp.core);
981 }
982
evlist__mmap(struct evlist * evlist,unsigned int pages)983 int evlist__mmap(struct evlist *evlist, unsigned int pages)
984 {
985 return evlist__mmap_ex(evlist, pages, 0, false, 0, PERF_AFFINITY_SYS, 1, 0);
986 }
987
evlist__create_maps(struct evlist * evlist,struct target * target)988 int evlist__create_maps(struct evlist *evlist, struct target *target)
989 {
990 bool all_threads = (target->per_thread && target->system_wide);
991 struct perf_cpu_map *cpus;
992 struct perf_thread_map *threads;
993
994 /*
995 * If specify '-a' and '--per-thread' to perf record, perf record
996 * will override '--per-thread'. target->per_thread = false and
997 * target->system_wide = true.
998 *
999 * If specify '--per-thread' only to perf record,
1000 * target->per_thread = true and target->system_wide = false.
1001 *
1002 * So target->per_thread && target->system_wide is false.
1003 * For perf record, thread_map__new_str doesn't call
1004 * thread_map__new_all_cpus. That will keep perf record's
1005 * current behavior.
1006 *
1007 * For perf stat, it allows the case that target->per_thread and
1008 * target->system_wide are all true. It means to collect system-wide
1009 * per-thread data. thread_map__new_str will call
1010 * thread_map__new_all_cpus to enumerate all threads.
1011 */
1012 threads = thread_map__new_str(target->pid, target->tid, all_threads);
1013
1014 if (!threads)
1015 return -1;
1016
1017 if (target__uses_dummy_map(target) && !evlist__has_bpf_output(evlist))
1018 cpus = perf_cpu_map__new_any_cpu();
1019 else
1020 cpus = perf_cpu_map__new(target->cpu_list);
1021
1022 if (!cpus)
1023 goto out_delete_threads;
1024
1025 evlist->core.has_user_cpus = !!target->cpu_list;
1026
1027 perf_evlist__set_maps(&evlist->core, cpus, threads);
1028
1029 /* as evlist now has references, put count here */
1030 perf_cpu_map__put(cpus);
1031 perf_thread_map__put(threads);
1032
1033 return 0;
1034
1035 out_delete_threads:
1036 perf_thread_map__put(threads);
1037 return -1;
1038 }
1039
evlist__apply_filters(struct evlist * evlist,struct evsel ** err_evsel,struct target * target)1040 int evlist__apply_filters(struct evlist *evlist, struct evsel **err_evsel,
1041 struct target *target)
1042 {
1043 struct evsel *evsel;
1044 int err = 0;
1045
1046 evlist__for_each_entry(evlist, evsel) {
1047 /*
1048 * filters only work for tracepoint event, which doesn't have cpu limit.
1049 * So evlist and evsel should always be same.
1050 */
1051 if (evsel->filter) {
1052 err = perf_evsel__apply_filter(&evsel->core, evsel->filter);
1053 if (err) {
1054 *err_evsel = evsel;
1055 break;
1056 }
1057 }
1058
1059 /*
1060 * non-tracepoint events can have BPF filters.
1061 */
1062 if (!list_empty(&evsel->bpf_filters)) {
1063 err = perf_bpf_filter__prepare(evsel, target);
1064 if (err) {
1065 *err_evsel = evsel;
1066 break;
1067 }
1068 }
1069 }
1070
1071 return err;
1072 }
1073
evlist__set_tp_filter(struct evlist * evlist,const char * filter)1074 int evlist__set_tp_filter(struct evlist *evlist, const char *filter)
1075 {
1076 struct evsel *evsel;
1077 int err = 0;
1078
1079 if (filter == NULL)
1080 return -1;
1081
1082 evlist__for_each_entry(evlist, evsel) {
1083 if (evsel->core.attr.type != PERF_TYPE_TRACEPOINT)
1084 continue;
1085
1086 err = evsel__set_filter(evsel, filter);
1087 if (err)
1088 break;
1089 }
1090
1091 return err;
1092 }
1093
evlist__append_tp_filter(struct evlist * evlist,const char * filter)1094 int evlist__append_tp_filter(struct evlist *evlist, const char *filter)
1095 {
1096 struct evsel *evsel;
1097 int err = 0;
1098
1099 if (filter == NULL)
1100 return -1;
1101
1102 evlist__for_each_entry(evlist, evsel) {
1103 if (evsel->core.attr.type != PERF_TYPE_TRACEPOINT)
1104 continue;
1105
1106 err = evsel__append_tp_filter(evsel, filter);
1107 if (err)
1108 break;
1109 }
1110
1111 return err;
1112 }
1113
asprintf__tp_filter_pids(size_t npids,pid_t * pids)1114 char *asprintf__tp_filter_pids(size_t npids, pid_t *pids)
1115 {
1116 char *filter;
1117 size_t i;
1118
1119 for (i = 0; i < npids; ++i) {
1120 if (i == 0) {
1121 if (asprintf(&filter, "common_pid != %d", pids[i]) < 0)
1122 return NULL;
1123 } else {
1124 char *tmp;
1125
1126 if (asprintf(&tmp, "%s && common_pid != %d", filter, pids[i]) < 0)
1127 goto out_free;
1128
1129 free(filter);
1130 filter = tmp;
1131 }
1132 }
1133
1134 return filter;
1135 out_free:
1136 free(filter);
1137 return NULL;
1138 }
1139
evlist__set_tp_filter_pids(struct evlist * evlist,size_t npids,pid_t * pids)1140 int evlist__set_tp_filter_pids(struct evlist *evlist, size_t npids, pid_t *pids)
1141 {
1142 char *filter = asprintf__tp_filter_pids(npids, pids);
1143 int ret = evlist__set_tp_filter(evlist, filter);
1144
1145 free(filter);
1146 return ret;
1147 }
1148
evlist__append_tp_filter_pids(struct evlist * evlist,size_t npids,pid_t * pids)1149 int evlist__append_tp_filter_pids(struct evlist *evlist, size_t npids, pid_t *pids)
1150 {
1151 char *filter = asprintf__tp_filter_pids(npids, pids);
1152 int ret = evlist__append_tp_filter(evlist, filter);
1153
1154 free(filter);
1155 return ret;
1156 }
1157
evlist__append_tp_filter_pid(struct evlist * evlist,pid_t pid)1158 int evlist__append_tp_filter_pid(struct evlist *evlist, pid_t pid)
1159 {
1160 return evlist__append_tp_filter_pids(evlist, 1, &pid);
1161 }
1162
evlist__valid_sample_type(struct evlist * evlist)1163 bool evlist__valid_sample_type(struct evlist *evlist)
1164 {
1165 struct evsel *pos;
1166
1167 if (evlist->core.nr_entries == 1)
1168 return true;
1169
1170 if (evlist->id_pos < 0 || evlist->is_pos < 0)
1171 return false;
1172
1173 evlist__for_each_entry(evlist, pos) {
1174 if (pos->id_pos != evlist->id_pos ||
1175 pos->is_pos != evlist->is_pos)
1176 return false;
1177 }
1178
1179 return true;
1180 }
1181
__evlist__combined_sample_type(struct evlist * evlist)1182 u64 __evlist__combined_sample_type(struct evlist *evlist)
1183 {
1184 struct evsel *evsel;
1185
1186 if (evlist->combined_sample_type)
1187 return evlist->combined_sample_type;
1188
1189 evlist__for_each_entry(evlist, evsel)
1190 evlist->combined_sample_type |= evsel->core.attr.sample_type;
1191
1192 return evlist->combined_sample_type;
1193 }
1194
evlist__combined_sample_type(struct evlist * evlist)1195 u64 evlist__combined_sample_type(struct evlist *evlist)
1196 {
1197 evlist->combined_sample_type = 0;
1198 return __evlist__combined_sample_type(evlist);
1199 }
1200
evlist__combined_branch_type(struct evlist * evlist)1201 u64 evlist__combined_branch_type(struct evlist *evlist)
1202 {
1203 struct evsel *evsel;
1204 u64 branch_type = 0;
1205
1206 evlist__for_each_entry(evlist, evsel)
1207 branch_type |= evsel->core.attr.branch_sample_type;
1208 return branch_type;
1209 }
1210
1211 static struct evsel *
evlist__find_dup_event_from_prev(struct evlist * evlist,struct evsel * event)1212 evlist__find_dup_event_from_prev(struct evlist *evlist, struct evsel *event)
1213 {
1214 struct evsel *pos;
1215
1216 evlist__for_each_entry(evlist, pos) {
1217 if (event == pos)
1218 break;
1219 if ((pos->core.attr.branch_sample_type & PERF_SAMPLE_BRANCH_COUNTERS) &&
1220 !strcmp(pos->name, event->name))
1221 return pos;
1222 }
1223 return NULL;
1224 }
1225
1226 #define MAX_NR_ABBR_NAME (26 * 11)
1227
1228 /*
1229 * The abbr name is from A to Z9. If the number of event
1230 * which requires the branch counter > MAX_NR_ABBR_NAME,
1231 * return NA.
1232 */
evlist__new_abbr_name(char * name)1233 static void evlist__new_abbr_name(char *name)
1234 {
1235 static int idx;
1236 int i = idx / 26;
1237
1238 if (idx >= MAX_NR_ABBR_NAME) {
1239 name[0] = 'N';
1240 name[1] = 'A';
1241 name[2] = '\0';
1242 return;
1243 }
1244
1245 name[0] = 'A' + (idx % 26);
1246
1247 if (!i)
1248 name[1] = '\0';
1249 else {
1250 name[1] = '0' + i - 1;
1251 name[2] = '\0';
1252 }
1253
1254 idx++;
1255 }
1256
evlist__update_br_cntr(struct evlist * evlist)1257 void evlist__update_br_cntr(struct evlist *evlist)
1258 {
1259 struct evsel *evsel, *dup;
1260 int i = 0;
1261
1262 evlist__for_each_entry(evlist, evsel) {
1263 if (evsel->core.attr.branch_sample_type & PERF_SAMPLE_BRANCH_COUNTERS) {
1264 evsel->br_cntr_idx = i++;
1265 evsel__leader(evsel)->br_cntr_nr++;
1266
1267 dup = evlist__find_dup_event_from_prev(evlist, evsel);
1268 if (dup)
1269 memcpy(evsel->abbr_name, dup->abbr_name, 3 * sizeof(char));
1270 else
1271 evlist__new_abbr_name(evsel->abbr_name);
1272 }
1273 }
1274 evlist->nr_br_cntr = i;
1275 }
1276
evlist__valid_read_format(struct evlist * evlist)1277 bool evlist__valid_read_format(struct evlist *evlist)
1278 {
1279 struct evsel *first = evlist__first(evlist), *pos = first;
1280 u64 read_format = first->core.attr.read_format;
1281 u64 sample_type = first->core.attr.sample_type;
1282
1283 evlist__for_each_entry(evlist, pos) {
1284 if (read_format != pos->core.attr.read_format) {
1285 pr_debug("Read format differs %#" PRIx64 " vs %#" PRIx64 "\n",
1286 read_format, (u64)pos->core.attr.read_format);
1287 }
1288 }
1289
1290 /* PERF_SAMPLE_READ implies PERF_FORMAT_ID. */
1291 if ((sample_type & PERF_SAMPLE_READ) &&
1292 !(read_format & PERF_FORMAT_ID)) {
1293 return false;
1294 }
1295
1296 return true;
1297 }
1298
evlist__id_hdr_size(struct evlist * evlist)1299 u16 evlist__id_hdr_size(struct evlist *evlist)
1300 {
1301 struct evsel *first = evlist__first(evlist);
1302
1303 return first->core.attr.sample_id_all ? evsel__id_hdr_size(first) : 0;
1304 }
1305
evlist__valid_sample_id_all(struct evlist * evlist)1306 bool evlist__valid_sample_id_all(struct evlist *evlist)
1307 {
1308 struct evsel *first = evlist__first(evlist), *pos = first;
1309
1310 evlist__for_each_entry_continue(evlist, pos) {
1311 if (first->core.attr.sample_id_all != pos->core.attr.sample_id_all)
1312 return false;
1313 }
1314
1315 return true;
1316 }
1317
evlist__sample_id_all(struct evlist * evlist)1318 bool evlist__sample_id_all(struct evlist *evlist)
1319 {
1320 struct evsel *first = evlist__first(evlist);
1321 return first->core.attr.sample_id_all;
1322 }
1323
evlist__set_selected(struct evlist * evlist,struct evsel * evsel)1324 void evlist__set_selected(struct evlist *evlist, struct evsel *evsel)
1325 {
1326 evlist->selected = evsel;
1327 }
1328
evlist__close(struct evlist * evlist)1329 void evlist__close(struct evlist *evlist)
1330 {
1331 struct evsel *evsel;
1332 struct evlist_cpu_iterator evlist_cpu_itr;
1333 struct affinity affinity;
1334
1335 /*
1336 * With perf record core.user_requested_cpus is usually NULL.
1337 * Use the old method to handle this for now.
1338 */
1339 if (!evlist->core.user_requested_cpus ||
1340 cpu_map__is_dummy(evlist->core.user_requested_cpus)) {
1341 evlist__for_each_entry_reverse(evlist, evsel)
1342 evsel__close(evsel);
1343 return;
1344 }
1345
1346 if (affinity__setup(&affinity) < 0)
1347 return;
1348
1349 evlist__for_each_cpu(evlist_cpu_itr, evlist, &affinity) {
1350 perf_evsel__close_cpu(&evlist_cpu_itr.evsel->core,
1351 evlist_cpu_itr.cpu_map_idx);
1352 }
1353
1354 affinity__cleanup(&affinity);
1355 evlist__for_each_entry_reverse(evlist, evsel) {
1356 perf_evsel__free_fd(&evsel->core);
1357 perf_evsel__free_id(&evsel->core);
1358 }
1359 perf_evlist__reset_id_hash(&evlist->core);
1360 }
1361
evlist__create_syswide_maps(struct evlist * evlist)1362 static int evlist__create_syswide_maps(struct evlist *evlist)
1363 {
1364 struct perf_cpu_map *cpus;
1365 struct perf_thread_map *threads;
1366
1367 /*
1368 * Try reading /sys/devices/system/cpu/online to get
1369 * an all cpus map.
1370 *
1371 * FIXME: -ENOMEM is the best we can do here, the cpu_map
1372 * code needs an overhaul to properly forward the
1373 * error, and we may not want to do that fallback to a
1374 * default cpu identity map :-\
1375 */
1376 cpus = perf_cpu_map__new_online_cpus();
1377 if (!cpus)
1378 return -ENOMEM;
1379
1380 threads = perf_thread_map__new_dummy();
1381 if (!threads) {
1382 perf_cpu_map__put(cpus);
1383 return -ENOMEM;
1384 }
1385
1386 perf_evlist__set_maps(&evlist->core, cpus, threads);
1387 perf_thread_map__put(threads);
1388 perf_cpu_map__put(cpus);
1389 return 0;
1390 }
1391
evlist__open(struct evlist * evlist)1392 int evlist__open(struct evlist *evlist)
1393 {
1394 struct evsel *evsel;
1395 int err;
1396
1397 /*
1398 * Default: one fd per CPU, all threads, aka systemwide
1399 * as sys_perf_event_open(cpu = -1, thread = -1) is EINVAL
1400 */
1401 if (evlist->core.threads == NULL && evlist->core.user_requested_cpus == NULL) {
1402 err = evlist__create_syswide_maps(evlist);
1403 if (err < 0)
1404 goto out_err;
1405 }
1406
1407 evlist__update_id_pos(evlist);
1408
1409 evlist__for_each_entry(evlist, evsel) {
1410 err = evsel__open(evsel, evsel->core.cpus, evsel->core.threads);
1411 if (err < 0)
1412 goto out_err;
1413 }
1414
1415 return 0;
1416 out_err:
1417 evlist__close(evlist);
1418 errno = -err;
1419 return err;
1420 }
1421
evlist__prepare_workload(struct evlist * evlist,struct target * target,const char * argv[],bool pipe_output,void (* exec_error)(int signo,siginfo_t * info,void * ucontext))1422 int evlist__prepare_workload(struct evlist *evlist, struct target *target, const char *argv[],
1423 bool pipe_output, void (*exec_error)(int signo, siginfo_t *info, void *ucontext))
1424 {
1425 int child_ready_pipe[2], go_pipe[2];
1426 char bf;
1427
1428 evlist->workload.cork_fd = -1;
1429
1430 if (pipe(child_ready_pipe) < 0) {
1431 perror("failed to create 'ready' pipe");
1432 return -1;
1433 }
1434
1435 if (pipe(go_pipe) < 0) {
1436 perror("failed to create 'go' pipe");
1437 goto out_close_ready_pipe;
1438 }
1439
1440 evlist->workload.pid = fork();
1441 if (evlist->workload.pid < 0) {
1442 perror("failed to fork");
1443 goto out_close_pipes;
1444 }
1445
1446 if (!evlist->workload.pid) {
1447 int ret;
1448
1449 if (pipe_output)
1450 dup2(2, 1);
1451
1452 signal(SIGTERM, SIG_DFL);
1453
1454 close(child_ready_pipe[0]);
1455 close(go_pipe[1]);
1456 fcntl(go_pipe[0], F_SETFD, FD_CLOEXEC);
1457
1458 /*
1459 * Change the name of this process not to confuse --exclude-perf users
1460 * that sees 'perf' in the window up to the execvp() and thinks that
1461 * perf samples are not being excluded.
1462 */
1463 prctl(PR_SET_NAME, "perf-exec");
1464
1465 /*
1466 * Tell the parent we're ready to go
1467 */
1468 close(child_ready_pipe[1]);
1469
1470 /*
1471 * Wait until the parent tells us to go.
1472 */
1473 ret = read(go_pipe[0], &bf, 1);
1474 /*
1475 * The parent will ask for the execvp() to be performed by
1476 * writing exactly one byte, in workload.cork_fd, usually via
1477 * evlist__start_workload().
1478 *
1479 * For cancelling the workload without actually running it,
1480 * the parent will just close workload.cork_fd, without writing
1481 * anything, i.e. read will return zero and we just exit()
1482 * here (See evlist__cancel_workload()).
1483 */
1484 if (ret != 1) {
1485 if (ret == -1)
1486 perror("unable to read pipe");
1487 exit(ret);
1488 }
1489
1490 execvp(argv[0], (char **)argv);
1491
1492 if (exec_error) {
1493 union sigval val;
1494
1495 val.sival_int = errno;
1496 if (sigqueue(getppid(), SIGUSR1, val))
1497 perror(argv[0]);
1498 } else
1499 perror(argv[0]);
1500 exit(-1);
1501 }
1502
1503 if (exec_error) {
1504 struct sigaction act = {
1505 .sa_flags = SA_SIGINFO,
1506 .sa_sigaction = exec_error,
1507 };
1508 sigaction(SIGUSR1, &act, NULL);
1509 }
1510
1511 if (target__none(target)) {
1512 if (evlist->core.threads == NULL) {
1513 fprintf(stderr, "FATAL: evlist->threads need to be set at this point (%s:%d).\n",
1514 __func__, __LINE__);
1515 goto out_close_pipes;
1516 }
1517 perf_thread_map__set_pid(evlist->core.threads, 0, evlist->workload.pid);
1518 }
1519
1520 close(child_ready_pipe[1]);
1521 close(go_pipe[0]);
1522 /*
1523 * wait for child to settle
1524 */
1525 if (read(child_ready_pipe[0], &bf, 1) == -1) {
1526 perror("unable to read pipe");
1527 goto out_close_pipes;
1528 }
1529
1530 fcntl(go_pipe[1], F_SETFD, FD_CLOEXEC);
1531 evlist->workload.cork_fd = go_pipe[1];
1532 close(child_ready_pipe[0]);
1533 return 0;
1534
1535 out_close_pipes:
1536 close(go_pipe[0]);
1537 close(go_pipe[1]);
1538 out_close_ready_pipe:
1539 close(child_ready_pipe[0]);
1540 close(child_ready_pipe[1]);
1541 return -1;
1542 }
1543
evlist__start_workload(struct evlist * evlist)1544 int evlist__start_workload(struct evlist *evlist)
1545 {
1546 if (evlist->workload.cork_fd >= 0) {
1547 char bf = 0;
1548 int ret;
1549 /*
1550 * Remove the cork, let it rip!
1551 */
1552 ret = write(evlist->workload.cork_fd, &bf, 1);
1553 if (ret < 0)
1554 perror("unable to write to pipe");
1555
1556 close(evlist->workload.cork_fd);
1557 evlist->workload.cork_fd = -1;
1558 return ret;
1559 }
1560
1561 return 0;
1562 }
1563
evlist__cancel_workload(struct evlist * evlist)1564 void evlist__cancel_workload(struct evlist *evlist)
1565 {
1566 int status;
1567
1568 if (evlist->workload.cork_fd >= 0) {
1569 close(evlist->workload.cork_fd);
1570 evlist->workload.cork_fd = -1;
1571 waitpid(evlist->workload.pid, &status, WNOHANG);
1572 }
1573 }
1574
evlist__parse_sample(struct evlist * evlist,union perf_event * event,struct perf_sample * sample)1575 int evlist__parse_sample(struct evlist *evlist, union perf_event *event, struct perf_sample *sample)
1576 {
1577 struct evsel *evsel = evlist__event2evsel(evlist, event);
1578 int ret;
1579
1580 if (!evsel)
1581 return -EFAULT;
1582 ret = evsel__parse_sample(evsel, event, sample);
1583 if (ret)
1584 return ret;
1585 if (perf_guest && sample->id) {
1586 struct perf_sample_id *sid = evlist__id2sid(evlist, sample->id);
1587
1588 if (sid) {
1589 sample->machine_pid = sid->machine_pid;
1590 sample->vcpu = sid->vcpu.cpu;
1591 }
1592 }
1593 return 0;
1594 }
1595
evlist__parse_sample_timestamp(struct evlist * evlist,union perf_event * event,u64 * timestamp)1596 int evlist__parse_sample_timestamp(struct evlist *evlist, union perf_event *event, u64 *timestamp)
1597 {
1598 struct evsel *evsel = evlist__event2evsel(evlist, event);
1599
1600 if (!evsel)
1601 return -EFAULT;
1602 return evsel__parse_sample_timestamp(evsel, event, timestamp);
1603 }
1604
evlist__strerror_open(struct evlist * evlist,int err,char * buf,size_t size)1605 int evlist__strerror_open(struct evlist *evlist, int err, char *buf, size_t size)
1606 {
1607 int printed, value;
1608 char sbuf[STRERR_BUFSIZE], *emsg = str_error_r(err, sbuf, sizeof(sbuf));
1609
1610 switch (err) {
1611 case EACCES:
1612 case EPERM:
1613 printed = scnprintf(buf, size,
1614 "Error:\t%s.\n"
1615 "Hint:\tCheck /proc/sys/kernel/perf_event_paranoid setting.", emsg);
1616
1617 value = perf_event_paranoid();
1618
1619 printed += scnprintf(buf + printed, size - printed, "\nHint:\t");
1620
1621 if (value >= 2) {
1622 printed += scnprintf(buf + printed, size - printed,
1623 "For your workloads it needs to be <= 1\nHint:\t");
1624 }
1625 printed += scnprintf(buf + printed, size - printed,
1626 "For system wide tracing it needs to be set to -1.\n");
1627
1628 printed += scnprintf(buf + printed, size - printed,
1629 "Hint:\tTry: 'sudo sh -c \"echo -1 > /proc/sys/kernel/perf_event_paranoid\"'\n"
1630 "Hint:\tThe current value is %d.", value);
1631 break;
1632 case EINVAL: {
1633 struct evsel *first = evlist__first(evlist);
1634 int max_freq;
1635
1636 if (sysctl__read_int("kernel/perf_event_max_sample_rate", &max_freq) < 0)
1637 goto out_default;
1638
1639 if (first->core.attr.sample_freq < (u64)max_freq)
1640 goto out_default;
1641
1642 printed = scnprintf(buf, size,
1643 "Error:\t%s.\n"
1644 "Hint:\tCheck /proc/sys/kernel/perf_event_max_sample_rate.\n"
1645 "Hint:\tThe current value is %d and %" PRIu64 " is being requested.",
1646 emsg, max_freq, first->core.attr.sample_freq);
1647 break;
1648 }
1649 default:
1650 out_default:
1651 scnprintf(buf, size, "%s", emsg);
1652 break;
1653 }
1654
1655 return 0;
1656 }
1657
evlist__strerror_mmap(struct evlist * evlist,int err,char * buf,size_t size)1658 int evlist__strerror_mmap(struct evlist *evlist, int err, char *buf, size_t size)
1659 {
1660 char sbuf[STRERR_BUFSIZE], *emsg = str_error_r(err, sbuf, sizeof(sbuf));
1661 int pages_attempted = evlist->core.mmap_len / 1024, pages_max_per_user, printed = 0;
1662
1663 switch (err) {
1664 case EPERM:
1665 sysctl__read_int("kernel/perf_event_mlock_kb", &pages_max_per_user);
1666 printed += scnprintf(buf + printed, size - printed,
1667 "Error:\t%s.\n"
1668 "Hint:\tCheck /proc/sys/kernel/perf_event_mlock_kb (%d kB) setting.\n"
1669 "Hint:\tTried using %zd kB.\n",
1670 emsg, pages_max_per_user, pages_attempted);
1671
1672 if (pages_attempted >= pages_max_per_user) {
1673 printed += scnprintf(buf + printed, size - printed,
1674 "Hint:\tTry 'sudo sh -c \"echo %d > /proc/sys/kernel/perf_event_mlock_kb\"', or\n",
1675 pages_max_per_user + pages_attempted);
1676 }
1677
1678 printed += scnprintf(buf + printed, size - printed,
1679 "Hint:\tTry using a smaller -m/--mmap-pages value.");
1680 break;
1681 default:
1682 scnprintf(buf, size, "%s", emsg);
1683 break;
1684 }
1685
1686 return 0;
1687 }
1688
evlist__to_front(struct evlist * evlist,struct evsel * move_evsel)1689 void evlist__to_front(struct evlist *evlist, struct evsel *move_evsel)
1690 {
1691 struct evsel *evsel, *n;
1692 LIST_HEAD(move);
1693
1694 if (move_evsel == evlist__first(evlist))
1695 return;
1696
1697 evlist__for_each_entry_safe(evlist, n, evsel) {
1698 if (evsel__leader(evsel) == evsel__leader(move_evsel))
1699 list_move_tail(&evsel->core.node, &move);
1700 }
1701
1702 list_splice(&move, &evlist->core.entries);
1703 }
1704
evlist__get_tracking_event(struct evlist * evlist)1705 struct evsel *evlist__get_tracking_event(struct evlist *evlist)
1706 {
1707 struct evsel *evsel;
1708
1709 evlist__for_each_entry(evlist, evsel) {
1710 if (evsel->tracking)
1711 return evsel;
1712 }
1713
1714 return evlist__first(evlist);
1715 }
1716
evlist__set_tracking_event(struct evlist * evlist,struct evsel * tracking_evsel)1717 void evlist__set_tracking_event(struct evlist *evlist, struct evsel *tracking_evsel)
1718 {
1719 struct evsel *evsel;
1720
1721 if (tracking_evsel->tracking)
1722 return;
1723
1724 evlist__for_each_entry(evlist, evsel) {
1725 if (evsel != tracking_evsel)
1726 evsel->tracking = false;
1727 }
1728
1729 tracking_evsel->tracking = true;
1730 }
1731
evlist__findnew_tracking_event(struct evlist * evlist,bool system_wide)1732 struct evsel *evlist__findnew_tracking_event(struct evlist *evlist, bool system_wide)
1733 {
1734 struct evsel *evsel;
1735
1736 evsel = evlist__get_tracking_event(evlist);
1737 if (!evsel__is_dummy_event(evsel)) {
1738 evsel = evlist__add_aux_dummy(evlist, system_wide);
1739 if (!evsel)
1740 return NULL;
1741
1742 evlist__set_tracking_event(evlist, evsel);
1743 } else if (system_wide) {
1744 perf_evlist__go_system_wide(&evlist->core, &evsel->core);
1745 }
1746
1747 return evsel;
1748 }
1749
evlist__find_evsel_by_str(struct evlist * evlist,const char * str)1750 struct evsel *evlist__find_evsel_by_str(struct evlist *evlist, const char *str)
1751 {
1752 struct evsel *evsel;
1753
1754 evlist__for_each_entry(evlist, evsel) {
1755 if (!evsel->name)
1756 continue;
1757 if (evsel__name_is(evsel, str))
1758 return evsel;
1759 }
1760
1761 return NULL;
1762 }
1763
evlist__toggle_bkw_mmap(struct evlist * evlist,enum bkw_mmap_state state)1764 void evlist__toggle_bkw_mmap(struct evlist *evlist, enum bkw_mmap_state state)
1765 {
1766 enum bkw_mmap_state old_state = evlist->bkw_mmap_state;
1767 enum action {
1768 NONE,
1769 PAUSE,
1770 RESUME,
1771 } action = NONE;
1772
1773 if (!evlist->overwrite_mmap)
1774 return;
1775
1776 switch (old_state) {
1777 case BKW_MMAP_NOTREADY: {
1778 if (state != BKW_MMAP_RUNNING)
1779 goto state_err;
1780 break;
1781 }
1782 case BKW_MMAP_RUNNING: {
1783 if (state != BKW_MMAP_DATA_PENDING)
1784 goto state_err;
1785 action = PAUSE;
1786 break;
1787 }
1788 case BKW_MMAP_DATA_PENDING: {
1789 if (state != BKW_MMAP_EMPTY)
1790 goto state_err;
1791 break;
1792 }
1793 case BKW_MMAP_EMPTY: {
1794 if (state != BKW_MMAP_RUNNING)
1795 goto state_err;
1796 action = RESUME;
1797 break;
1798 }
1799 default:
1800 WARN_ONCE(1, "Shouldn't get there\n");
1801 }
1802
1803 evlist->bkw_mmap_state = state;
1804
1805 switch (action) {
1806 case PAUSE:
1807 evlist__pause(evlist);
1808 break;
1809 case RESUME:
1810 evlist__resume(evlist);
1811 break;
1812 case NONE:
1813 default:
1814 break;
1815 }
1816
1817 state_err:
1818 return;
1819 }
1820
evlist__exclude_kernel(struct evlist * evlist)1821 bool evlist__exclude_kernel(struct evlist *evlist)
1822 {
1823 struct evsel *evsel;
1824
1825 evlist__for_each_entry(evlist, evsel) {
1826 if (!evsel->core.attr.exclude_kernel)
1827 return false;
1828 }
1829
1830 return true;
1831 }
1832
1833 /*
1834 * Events in data file are not collect in groups, but we still want
1835 * the group display. Set the artificial group and set the leader's
1836 * forced_leader flag to notify the display code.
1837 */
evlist__force_leader(struct evlist * evlist)1838 void evlist__force_leader(struct evlist *evlist)
1839 {
1840 if (evlist__nr_groups(evlist) == 0) {
1841 struct evsel *leader = evlist__first(evlist);
1842
1843 evlist__set_leader(evlist);
1844 leader->forced_leader = true;
1845 }
1846 }
1847
evlist__reset_weak_group(struct evlist * evsel_list,struct evsel * evsel,bool close)1848 struct evsel *evlist__reset_weak_group(struct evlist *evsel_list, struct evsel *evsel, bool close)
1849 {
1850 struct evsel *c2, *leader;
1851 bool is_open = true;
1852
1853 leader = evsel__leader(evsel);
1854
1855 pr_debug("Weak group for %s/%d failed\n",
1856 leader->name, leader->core.nr_members);
1857
1858 /*
1859 * for_each_group_member doesn't work here because it doesn't
1860 * include the first entry.
1861 */
1862 evlist__for_each_entry(evsel_list, c2) {
1863 if (c2 == evsel)
1864 is_open = false;
1865 if (evsel__has_leader(c2, leader)) {
1866 if (is_open && close)
1867 perf_evsel__close(&c2->core);
1868 /*
1869 * We want to close all members of the group and reopen
1870 * them. Some events, like Intel topdown, require being
1871 * in a group and so keep these in the group.
1872 */
1873 evsel__remove_from_group(c2, leader);
1874
1875 /*
1876 * Set this for all former members of the group
1877 * to indicate they get reopened.
1878 */
1879 c2->reset_group = true;
1880 }
1881 }
1882 /* Reset the leader count if all entries were removed. */
1883 if (leader->core.nr_members == 1)
1884 leader->core.nr_members = 0;
1885 return leader;
1886 }
1887
evlist__parse_control_fifo(const char * str,int * ctl_fd,int * ctl_fd_ack,bool * ctl_fd_close)1888 static int evlist__parse_control_fifo(const char *str, int *ctl_fd, int *ctl_fd_ack, bool *ctl_fd_close)
1889 {
1890 char *s, *p;
1891 int ret = 0, fd;
1892
1893 if (strncmp(str, "fifo:", 5))
1894 return -EINVAL;
1895
1896 str += 5;
1897 if (!*str || *str == ',')
1898 return -EINVAL;
1899
1900 s = strdup(str);
1901 if (!s)
1902 return -ENOMEM;
1903
1904 p = strchr(s, ',');
1905 if (p)
1906 *p = '\0';
1907
1908 /*
1909 * O_RDWR avoids POLLHUPs which is necessary to allow the other
1910 * end of a FIFO to be repeatedly opened and closed.
1911 */
1912 fd = open(s, O_RDWR | O_NONBLOCK | O_CLOEXEC);
1913 if (fd < 0) {
1914 pr_err("Failed to open '%s'\n", s);
1915 ret = -errno;
1916 goto out_free;
1917 }
1918 *ctl_fd = fd;
1919 *ctl_fd_close = true;
1920
1921 if (p && *++p) {
1922 /* O_RDWR | O_NONBLOCK means the other end need not be open */
1923 fd = open(p, O_RDWR | O_NONBLOCK | O_CLOEXEC);
1924 if (fd < 0) {
1925 pr_err("Failed to open '%s'\n", p);
1926 ret = -errno;
1927 goto out_free;
1928 }
1929 *ctl_fd_ack = fd;
1930 }
1931
1932 out_free:
1933 free(s);
1934 return ret;
1935 }
1936
evlist__parse_control(const char * str,int * ctl_fd,int * ctl_fd_ack,bool * ctl_fd_close)1937 int evlist__parse_control(const char *str, int *ctl_fd, int *ctl_fd_ack, bool *ctl_fd_close)
1938 {
1939 char *comma = NULL, *endptr = NULL;
1940
1941 *ctl_fd_close = false;
1942
1943 if (strncmp(str, "fd:", 3))
1944 return evlist__parse_control_fifo(str, ctl_fd, ctl_fd_ack, ctl_fd_close);
1945
1946 *ctl_fd = strtoul(&str[3], &endptr, 0);
1947 if (endptr == &str[3])
1948 return -EINVAL;
1949
1950 comma = strchr(str, ',');
1951 if (comma) {
1952 if (endptr != comma)
1953 return -EINVAL;
1954
1955 *ctl_fd_ack = strtoul(comma + 1, &endptr, 0);
1956 if (endptr == comma + 1 || *endptr != '\0')
1957 return -EINVAL;
1958 }
1959
1960 return 0;
1961 }
1962
evlist__close_control(int ctl_fd,int ctl_fd_ack,bool * ctl_fd_close)1963 void evlist__close_control(int ctl_fd, int ctl_fd_ack, bool *ctl_fd_close)
1964 {
1965 if (*ctl_fd_close) {
1966 *ctl_fd_close = false;
1967 close(ctl_fd);
1968 if (ctl_fd_ack >= 0)
1969 close(ctl_fd_ack);
1970 }
1971 }
1972
evlist__initialize_ctlfd(struct evlist * evlist,int fd,int ack)1973 int evlist__initialize_ctlfd(struct evlist *evlist, int fd, int ack)
1974 {
1975 if (fd == -1) {
1976 pr_debug("Control descriptor is not initialized\n");
1977 return 0;
1978 }
1979
1980 evlist->ctl_fd.pos = perf_evlist__add_pollfd(&evlist->core, fd, NULL, POLLIN,
1981 fdarray_flag__nonfilterable |
1982 fdarray_flag__non_perf_event);
1983 if (evlist->ctl_fd.pos < 0) {
1984 evlist->ctl_fd.pos = -1;
1985 pr_err("Failed to add ctl fd entry: %m\n");
1986 return -1;
1987 }
1988
1989 evlist->ctl_fd.fd = fd;
1990 evlist->ctl_fd.ack = ack;
1991
1992 return 0;
1993 }
1994
evlist__ctlfd_initialized(struct evlist * evlist)1995 bool evlist__ctlfd_initialized(struct evlist *evlist)
1996 {
1997 return evlist->ctl_fd.pos >= 0;
1998 }
1999
evlist__finalize_ctlfd(struct evlist * evlist)2000 int evlist__finalize_ctlfd(struct evlist *evlist)
2001 {
2002 struct pollfd *entries = evlist->core.pollfd.entries;
2003
2004 if (!evlist__ctlfd_initialized(evlist))
2005 return 0;
2006
2007 entries[evlist->ctl_fd.pos].fd = -1;
2008 entries[evlist->ctl_fd.pos].events = 0;
2009 entries[evlist->ctl_fd.pos].revents = 0;
2010
2011 evlist->ctl_fd.pos = -1;
2012 evlist->ctl_fd.ack = -1;
2013 evlist->ctl_fd.fd = -1;
2014
2015 return 0;
2016 }
2017
evlist__ctlfd_recv(struct evlist * evlist,enum evlist_ctl_cmd * cmd,char * cmd_data,size_t data_size)2018 static int evlist__ctlfd_recv(struct evlist *evlist, enum evlist_ctl_cmd *cmd,
2019 char *cmd_data, size_t data_size)
2020 {
2021 int err;
2022 char c;
2023 size_t bytes_read = 0;
2024
2025 *cmd = EVLIST_CTL_CMD_UNSUPPORTED;
2026 memset(cmd_data, 0, data_size);
2027 data_size--;
2028
2029 do {
2030 err = read(evlist->ctl_fd.fd, &c, 1);
2031 if (err > 0) {
2032 if (c == '\n' || c == '\0')
2033 break;
2034 cmd_data[bytes_read++] = c;
2035 if (bytes_read == data_size)
2036 break;
2037 continue;
2038 } else if (err == -1) {
2039 if (errno == EINTR)
2040 continue;
2041 if (errno == EAGAIN || errno == EWOULDBLOCK)
2042 err = 0;
2043 else
2044 pr_err("Failed to read from ctlfd %d: %m\n", evlist->ctl_fd.fd);
2045 }
2046 break;
2047 } while (1);
2048
2049 pr_debug("Message from ctl_fd: \"%s%s\"\n", cmd_data,
2050 bytes_read == data_size ? "" : c == '\n' ? "\\n" : "\\0");
2051
2052 if (bytes_read > 0) {
2053 if (!strncmp(cmd_data, EVLIST_CTL_CMD_ENABLE_TAG,
2054 (sizeof(EVLIST_CTL_CMD_ENABLE_TAG)-1))) {
2055 *cmd = EVLIST_CTL_CMD_ENABLE;
2056 } else if (!strncmp(cmd_data, EVLIST_CTL_CMD_DISABLE_TAG,
2057 (sizeof(EVLIST_CTL_CMD_DISABLE_TAG)-1))) {
2058 *cmd = EVLIST_CTL_CMD_DISABLE;
2059 } else if (!strncmp(cmd_data, EVLIST_CTL_CMD_SNAPSHOT_TAG,
2060 (sizeof(EVLIST_CTL_CMD_SNAPSHOT_TAG)-1))) {
2061 *cmd = EVLIST_CTL_CMD_SNAPSHOT;
2062 pr_debug("is snapshot\n");
2063 } else if (!strncmp(cmd_data, EVLIST_CTL_CMD_EVLIST_TAG,
2064 (sizeof(EVLIST_CTL_CMD_EVLIST_TAG)-1))) {
2065 *cmd = EVLIST_CTL_CMD_EVLIST;
2066 } else if (!strncmp(cmd_data, EVLIST_CTL_CMD_STOP_TAG,
2067 (sizeof(EVLIST_CTL_CMD_STOP_TAG)-1))) {
2068 *cmd = EVLIST_CTL_CMD_STOP;
2069 } else if (!strncmp(cmd_data, EVLIST_CTL_CMD_PING_TAG,
2070 (sizeof(EVLIST_CTL_CMD_PING_TAG)-1))) {
2071 *cmd = EVLIST_CTL_CMD_PING;
2072 }
2073 }
2074
2075 return bytes_read ? (int)bytes_read : err;
2076 }
2077
evlist__ctlfd_ack(struct evlist * evlist)2078 int evlist__ctlfd_ack(struct evlist *evlist)
2079 {
2080 int err;
2081
2082 if (evlist->ctl_fd.ack == -1)
2083 return 0;
2084
2085 err = write(evlist->ctl_fd.ack, EVLIST_CTL_CMD_ACK_TAG,
2086 sizeof(EVLIST_CTL_CMD_ACK_TAG));
2087 if (err == -1)
2088 pr_err("failed to write to ctl_ack_fd %d: %m\n", evlist->ctl_fd.ack);
2089
2090 return err;
2091 }
2092
get_cmd_arg(char * cmd_data,size_t cmd_size,char ** arg)2093 static int get_cmd_arg(char *cmd_data, size_t cmd_size, char **arg)
2094 {
2095 char *data = cmd_data + cmd_size;
2096
2097 /* no argument */
2098 if (!*data)
2099 return 0;
2100
2101 /* there's argument */
2102 if (*data == ' ') {
2103 *arg = data + 1;
2104 return 1;
2105 }
2106
2107 /* malformed */
2108 return -1;
2109 }
2110
evlist__ctlfd_enable(struct evlist * evlist,char * cmd_data,bool enable)2111 static int evlist__ctlfd_enable(struct evlist *evlist, char *cmd_data, bool enable)
2112 {
2113 struct evsel *evsel;
2114 char *name;
2115 int err;
2116
2117 err = get_cmd_arg(cmd_data,
2118 enable ? sizeof(EVLIST_CTL_CMD_ENABLE_TAG) - 1 :
2119 sizeof(EVLIST_CTL_CMD_DISABLE_TAG) - 1,
2120 &name);
2121 if (err < 0) {
2122 pr_info("failed: wrong command\n");
2123 return -1;
2124 }
2125
2126 if (err) {
2127 evsel = evlist__find_evsel_by_str(evlist, name);
2128 if (evsel) {
2129 if (enable)
2130 evlist__enable_evsel(evlist, name);
2131 else
2132 evlist__disable_evsel(evlist, name);
2133 pr_info("Event %s %s\n", evsel->name,
2134 enable ? "enabled" : "disabled");
2135 } else {
2136 pr_info("failed: can't find '%s' event\n", name);
2137 }
2138 } else {
2139 if (enable) {
2140 evlist__enable(evlist);
2141 pr_info(EVLIST_ENABLED_MSG);
2142 } else {
2143 evlist__disable(evlist);
2144 pr_info(EVLIST_DISABLED_MSG);
2145 }
2146 }
2147
2148 return 0;
2149 }
2150
evlist__ctlfd_list(struct evlist * evlist,char * cmd_data)2151 static int evlist__ctlfd_list(struct evlist *evlist, char *cmd_data)
2152 {
2153 struct perf_attr_details details = { .verbose = false, };
2154 struct evsel *evsel;
2155 char *arg;
2156 int err;
2157
2158 err = get_cmd_arg(cmd_data,
2159 sizeof(EVLIST_CTL_CMD_EVLIST_TAG) - 1,
2160 &arg);
2161 if (err < 0) {
2162 pr_info("failed: wrong command\n");
2163 return -1;
2164 }
2165
2166 if (err) {
2167 if (!strcmp(arg, "-v")) {
2168 details.verbose = true;
2169 } else if (!strcmp(arg, "-g")) {
2170 details.event_group = true;
2171 } else if (!strcmp(arg, "-F")) {
2172 details.freq = true;
2173 } else {
2174 pr_info("failed: wrong command\n");
2175 return -1;
2176 }
2177 }
2178
2179 evlist__for_each_entry(evlist, evsel)
2180 evsel__fprintf(evsel, &details, stderr);
2181
2182 return 0;
2183 }
2184
evlist__ctlfd_process(struct evlist * evlist,enum evlist_ctl_cmd * cmd)2185 int evlist__ctlfd_process(struct evlist *evlist, enum evlist_ctl_cmd *cmd)
2186 {
2187 int err = 0;
2188 char cmd_data[EVLIST_CTL_CMD_MAX_LEN];
2189 int ctlfd_pos = evlist->ctl_fd.pos;
2190 struct pollfd *entries = evlist->core.pollfd.entries;
2191
2192 if (!evlist__ctlfd_initialized(evlist) || !entries[ctlfd_pos].revents)
2193 return 0;
2194
2195 if (entries[ctlfd_pos].revents & POLLIN) {
2196 err = evlist__ctlfd_recv(evlist, cmd, cmd_data,
2197 EVLIST_CTL_CMD_MAX_LEN);
2198 if (err > 0) {
2199 switch (*cmd) {
2200 case EVLIST_CTL_CMD_ENABLE:
2201 case EVLIST_CTL_CMD_DISABLE:
2202 err = evlist__ctlfd_enable(evlist, cmd_data,
2203 *cmd == EVLIST_CTL_CMD_ENABLE);
2204 break;
2205 case EVLIST_CTL_CMD_EVLIST:
2206 err = evlist__ctlfd_list(evlist, cmd_data);
2207 break;
2208 case EVLIST_CTL_CMD_SNAPSHOT:
2209 case EVLIST_CTL_CMD_STOP:
2210 case EVLIST_CTL_CMD_PING:
2211 break;
2212 case EVLIST_CTL_CMD_ACK:
2213 case EVLIST_CTL_CMD_UNSUPPORTED:
2214 default:
2215 pr_debug("ctlfd: unsupported %d\n", *cmd);
2216 break;
2217 }
2218 if (!(*cmd == EVLIST_CTL_CMD_ACK || *cmd == EVLIST_CTL_CMD_UNSUPPORTED ||
2219 *cmd == EVLIST_CTL_CMD_SNAPSHOT))
2220 evlist__ctlfd_ack(evlist);
2221 }
2222 }
2223
2224 if (entries[ctlfd_pos].revents & (POLLHUP | POLLERR))
2225 evlist__finalize_ctlfd(evlist);
2226 else
2227 entries[ctlfd_pos].revents = 0;
2228
2229 return err;
2230 }
2231
2232 /**
2233 * struct event_enable_time - perf record -D/--delay single time range.
2234 * @start: start of time range to enable events in milliseconds
2235 * @end: end of time range to enable events in milliseconds
2236 *
2237 * N.B. this structure is also accessed as an array of int.
2238 */
2239 struct event_enable_time {
2240 int start;
2241 int end;
2242 };
2243
parse_event_enable_time(const char * str,struct event_enable_time * range,bool first)2244 static int parse_event_enable_time(const char *str, struct event_enable_time *range, bool first)
2245 {
2246 const char *fmt = first ? "%u - %u %n" : " , %u - %u %n";
2247 int ret, start, end, n;
2248
2249 ret = sscanf(str, fmt, &start, &end, &n);
2250 if (ret != 2 || end <= start)
2251 return -EINVAL;
2252 if (range) {
2253 range->start = start;
2254 range->end = end;
2255 }
2256 return n;
2257 }
2258
parse_event_enable_times(const char * str,struct event_enable_time * range)2259 static ssize_t parse_event_enable_times(const char *str, struct event_enable_time *range)
2260 {
2261 int incr = !!range;
2262 bool first = true;
2263 ssize_t ret, cnt;
2264
2265 for (cnt = 0; *str; cnt++) {
2266 ret = parse_event_enable_time(str, range, first);
2267 if (ret < 0)
2268 return ret;
2269 /* Check no overlap */
2270 if (!first && range && range->start <= range[-1].end)
2271 return -EINVAL;
2272 str += ret;
2273 range += incr;
2274 first = false;
2275 }
2276 return cnt;
2277 }
2278
2279 /**
2280 * struct event_enable_timer - control structure for perf record -D/--delay.
2281 * @evlist: event list
2282 * @times: time ranges that events are enabled (N.B. this is also accessed as an
2283 * array of int)
2284 * @times_cnt: number of time ranges
2285 * @timerfd: timer file descriptor
2286 * @pollfd_pos: position in @evlist array of file descriptors to poll (fdarray)
2287 * @times_step: current position in (int *)@times)[],
2288 * refer event_enable_timer__process()
2289 *
2290 * Note, this structure is only used when there are time ranges, not when there
2291 * is only an initial delay.
2292 */
2293 struct event_enable_timer {
2294 struct evlist *evlist;
2295 struct event_enable_time *times;
2296 size_t times_cnt;
2297 int timerfd;
2298 int pollfd_pos;
2299 size_t times_step;
2300 };
2301
str_to_delay(const char * str)2302 static int str_to_delay(const char *str)
2303 {
2304 char *endptr;
2305 long d;
2306
2307 d = strtol(str, &endptr, 10);
2308 if (*endptr || d > INT_MAX || d < -1)
2309 return 0;
2310 return d;
2311 }
2312
evlist__parse_event_enable_time(struct evlist * evlist,struct record_opts * opts,const char * str,int unset)2313 int evlist__parse_event_enable_time(struct evlist *evlist, struct record_opts *opts,
2314 const char *str, int unset)
2315 {
2316 enum fdarray_flags flags = fdarray_flag__nonfilterable | fdarray_flag__non_perf_event;
2317 struct event_enable_timer *eet;
2318 ssize_t times_cnt;
2319 ssize_t ret;
2320 int err;
2321
2322 if (unset)
2323 return 0;
2324
2325 opts->target.initial_delay = str_to_delay(str);
2326 if (opts->target.initial_delay)
2327 return 0;
2328
2329 ret = parse_event_enable_times(str, NULL);
2330 if (ret < 0)
2331 return ret;
2332
2333 times_cnt = ret;
2334 if (times_cnt == 0)
2335 return -EINVAL;
2336
2337 eet = zalloc(sizeof(*eet));
2338 if (!eet)
2339 return -ENOMEM;
2340
2341 eet->times = calloc(times_cnt, sizeof(*eet->times));
2342 if (!eet->times) {
2343 err = -ENOMEM;
2344 goto free_eet;
2345 }
2346
2347 if (parse_event_enable_times(str, eet->times) != times_cnt) {
2348 err = -EINVAL;
2349 goto free_eet_times;
2350 }
2351
2352 eet->times_cnt = times_cnt;
2353
2354 eet->timerfd = timerfd_create(CLOCK_MONOTONIC, TFD_CLOEXEC);
2355 if (eet->timerfd == -1) {
2356 err = -errno;
2357 pr_err("timerfd_create failed: %s\n", strerror(errno));
2358 goto free_eet_times;
2359 }
2360
2361 eet->pollfd_pos = perf_evlist__add_pollfd(&evlist->core, eet->timerfd, NULL, POLLIN, flags);
2362 if (eet->pollfd_pos < 0) {
2363 err = eet->pollfd_pos;
2364 goto close_timerfd;
2365 }
2366
2367 eet->evlist = evlist;
2368 evlist->eet = eet;
2369 opts->target.initial_delay = eet->times[0].start;
2370
2371 return 0;
2372
2373 close_timerfd:
2374 close(eet->timerfd);
2375 free_eet_times:
2376 zfree(&eet->times);
2377 free_eet:
2378 free(eet);
2379 return err;
2380 }
2381
event_enable_timer__set_timer(struct event_enable_timer * eet,int ms)2382 static int event_enable_timer__set_timer(struct event_enable_timer *eet, int ms)
2383 {
2384 struct itimerspec its = {
2385 .it_value.tv_sec = ms / MSEC_PER_SEC,
2386 .it_value.tv_nsec = (ms % MSEC_PER_SEC) * NSEC_PER_MSEC,
2387 };
2388 int err = 0;
2389
2390 if (timerfd_settime(eet->timerfd, 0, &its, NULL) < 0) {
2391 err = -errno;
2392 pr_err("timerfd_settime failed: %s\n", strerror(errno));
2393 }
2394 return err;
2395 }
2396
event_enable_timer__start(struct event_enable_timer * eet)2397 int event_enable_timer__start(struct event_enable_timer *eet)
2398 {
2399 int ms;
2400
2401 if (!eet)
2402 return 0;
2403
2404 ms = eet->times[0].end - eet->times[0].start;
2405 eet->times_step = 1;
2406
2407 return event_enable_timer__set_timer(eet, ms);
2408 }
2409
event_enable_timer__process(struct event_enable_timer * eet)2410 int event_enable_timer__process(struct event_enable_timer *eet)
2411 {
2412 struct pollfd *entries;
2413 short revents;
2414
2415 if (!eet)
2416 return 0;
2417
2418 entries = eet->evlist->core.pollfd.entries;
2419 revents = entries[eet->pollfd_pos].revents;
2420 entries[eet->pollfd_pos].revents = 0;
2421
2422 if (revents & POLLIN) {
2423 size_t step = eet->times_step;
2424 size_t pos = step / 2;
2425
2426 if (step & 1) {
2427 evlist__disable_non_dummy(eet->evlist);
2428 pr_info(EVLIST_DISABLED_MSG);
2429 if (pos >= eet->times_cnt - 1) {
2430 /* Disarm timer */
2431 event_enable_timer__set_timer(eet, 0);
2432 return 1; /* Stop */
2433 }
2434 } else {
2435 evlist__enable_non_dummy(eet->evlist);
2436 pr_info(EVLIST_ENABLED_MSG);
2437 }
2438
2439 step += 1;
2440 pos = step / 2;
2441
2442 if (pos < eet->times_cnt) {
2443 int *times = (int *)eet->times; /* Accessing 'times' as array of int */
2444 int ms = times[step] - times[step - 1];
2445
2446 eet->times_step = step;
2447 return event_enable_timer__set_timer(eet, ms);
2448 }
2449 }
2450
2451 return 0;
2452 }
2453
event_enable_timer__exit(struct event_enable_timer ** ep)2454 void event_enable_timer__exit(struct event_enable_timer **ep)
2455 {
2456 if (!ep || !*ep)
2457 return;
2458 zfree(&(*ep)->times);
2459 zfree(ep);
2460 }
2461
evlist__find_evsel(struct evlist * evlist,int idx)2462 struct evsel *evlist__find_evsel(struct evlist *evlist, int idx)
2463 {
2464 struct evsel *evsel;
2465
2466 evlist__for_each_entry(evlist, evsel) {
2467 if (evsel->core.idx == idx)
2468 return evsel;
2469 }
2470 return NULL;
2471 }
2472
evlist__format_evsels(struct evlist * evlist,struct strbuf * sb,size_t max_length)2473 void evlist__format_evsels(struct evlist *evlist, struct strbuf *sb, size_t max_length)
2474 {
2475 struct evsel *evsel, *leader = NULL;
2476 bool first = true;
2477
2478 evlist__for_each_entry(evlist, evsel) {
2479 struct evsel *new_leader = evsel__leader(evsel);
2480
2481 if (evsel__is_dummy_event(evsel))
2482 continue;
2483
2484 if (leader != new_leader && leader && leader->core.nr_members > 1)
2485 strbuf_addch(sb, '}');
2486
2487 if (!first)
2488 strbuf_addch(sb, ',');
2489
2490 if (sb->len > max_length) {
2491 strbuf_addstr(sb, "...");
2492 return;
2493 }
2494 if (leader != new_leader && new_leader->core.nr_members > 1)
2495 strbuf_addch(sb, '{');
2496
2497 strbuf_addstr(sb, evsel__name(evsel));
2498 first = false;
2499 leader = new_leader;
2500 }
2501 if (leader && leader->core.nr_members > 1)
2502 strbuf_addch(sb, '}');
2503 }
2504
evlist__check_mem_load_aux(struct evlist * evlist)2505 void evlist__check_mem_load_aux(struct evlist *evlist)
2506 {
2507 struct evsel *leader, *evsel, *pos;
2508
2509 /*
2510 * For some platforms, the 'mem-loads' event is required to use
2511 * together with 'mem-loads-aux' within a group and 'mem-loads-aux'
2512 * must be the group leader. Now we disable this group before reporting
2513 * because 'mem-loads-aux' is just an auxiliary event. It doesn't carry
2514 * any valid memory load information.
2515 */
2516 evlist__for_each_entry(evlist, evsel) {
2517 leader = evsel__leader(evsel);
2518 if (leader == evsel)
2519 continue;
2520
2521 if (leader->name && strstr(leader->name, "mem-loads-aux")) {
2522 for_each_group_evsel(pos, leader) {
2523 evsel__set_leader(pos, pos);
2524 pos->core.nr_members = 0;
2525 }
2526 }
2527 }
2528 }
2529
2530 /**
2531 * evlist__warn_user_requested_cpus() - Check each evsel against requested CPUs
2532 * and warn if the user CPU list is inapplicable for the event's PMU's
2533 * CPUs. Not core PMUs list a CPU in sysfs, but this may be overwritten by a
2534 * user requested CPU and so any online CPU is applicable. Core PMUs handle
2535 * events on the CPUs in their list and otherwise the event isn't supported.
2536 * @evlist: The list of events being checked.
2537 * @cpu_list: The user provided list of CPUs.
2538 */
evlist__warn_user_requested_cpus(struct evlist * evlist,const char * cpu_list)2539 void evlist__warn_user_requested_cpus(struct evlist *evlist, const char *cpu_list)
2540 {
2541 struct perf_cpu_map *user_requested_cpus;
2542 struct evsel *pos;
2543
2544 if (!cpu_list)
2545 return;
2546
2547 user_requested_cpus = perf_cpu_map__new(cpu_list);
2548 if (!user_requested_cpus)
2549 return;
2550
2551 evlist__for_each_entry(evlist, pos) {
2552 evsel__warn_user_requested_cpus(pos, user_requested_cpus);
2553 }
2554 perf_cpu_map__put(user_requested_cpus);
2555 }
2556
2557 /* Should uniquify be disabled for the evlist? */
evlist__disable_uniquify(const struct evlist * evlist)2558 static bool evlist__disable_uniquify(const struct evlist *evlist)
2559 {
2560 struct evsel *counter;
2561 struct perf_pmu *last_pmu = NULL;
2562 bool first = true;
2563
2564 evlist__for_each_entry(evlist, counter) {
2565 /* If PMUs vary then uniquify can be useful. */
2566 if (!first && counter->pmu != last_pmu)
2567 return false;
2568 first = false;
2569 if (counter->pmu) {
2570 /* Allow uniquify for uncore PMUs. */
2571 if (!counter->pmu->is_core)
2572 return false;
2573 /* Keep hybrid event names uniquified for clarity. */
2574 if (perf_pmus__num_core_pmus() > 1)
2575 return false;
2576 }
2577 last_pmu = counter->pmu;
2578 }
2579 return true;
2580 }
2581
evlist__set_needs_uniquify(struct evlist * evlist,const struct perf_stat_config * config)2582 static bool evlist__set_needs_uniquify(struct evlist *evlist, const struct perf_stat_config *config)
2583 {
2584 struct evsel *counter;
2585 bool needs_uniquify = false;
2586
2587 if (evlist__disable_uniquify(evlist)) {
2588 evlist__for_each_entry(evlist, counter)
2589 counter->uniquified_name = true;
2590 return false;
2591 }
2592
2593 evlist__for_each_entry(evlist, counter) {
2594 if (evsel__set_needs_uniquify(counter, config))
2595 needs_uniquify = true;
2596 }
2597 return needs_uniquify;
2598 }
2599
evlist__uniquify_evsel_names(struct evlist * evlist,const struct perf_stat_config * config)2600 void evlist__uniquify_evsel_names(struct evlist *evlist, const struct perf_stat_config *config)
2601 {
2602 if (evlist__set_needs_uniquify(evlist, config)) {
2603 struct evsel *pos;
2604
2605 evlist__for_each_entry(evlist, pos)
2606 evsel__uniquify_counter(pos);
2607 }
2608 }
2609
evlist__has_bpf_output(struct evlist * evlist)2610 bool evlist__has_bpf_output(struct evlist *evlist)
2611 {
2612 struct evsel *evsel;
2613
2614 evlist__for_each_entry(evlist, evsel) {
2615 if (evsel__is_bpf_output(evsel))
2616 return true;
2617 }
2618
2619 return false;
2620 }
2621
evlist__needs_bpf_sb_event(struct evlist * evlist)2622 bool evlist__needs_bpf_sb_event(struct evlist *evlist)
2623 {
2624 struct evsel *evsel;
2625
2626 evlist__for_each_entry(evlist, evsel) {
2627 if (evsel__is_dummy_event(evsel))
2628 continue;
2629 if (!evsel->core.attr.exclude_kernel)
2630 return true;
2631 }
2632
2633 return false;
2634 }
2635