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