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