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 goto out;
1366
1367 threads = perf_thread_map__new_dummy();
1368 if (!threads)
1369 goto out_put;
1370
1371 perf_evlist__set_maps(&evlist->core, cpus, threads);
1372
1373 perf_thread_map__put(threads);
1374 out_put:
1375 perf_cpu_map__put(cpus);
1376 out:
1377 return -ENOMEM;
1378 }
1379
evlist__open(struct evlist * evlist)1380 int evlist__open(struct evlist *evlist)
1381 {
1382 struct evsel *evsel;
1383 int err;
1384
1385 /*
1386 * Default: one fd per CPU, all threads, aka systemwide
1387 * as sys_perf_event_open(cpu = -1, thread = -1) is EINVAL
1388 */
1389 if (evlist->core.threads == NULL && evlist->core.user_requested_cpus == NULL) {
1390 err = evlist__create_syswide_maps(evlist);
1391 if (err < 0)
1392 goto out_err;
1393 }
1394
1395 evlist__update_id_pos(evlist);
1396
1397 evlist__for_each_entry(evlist, evsel) {
1398 err = evsel__open(evsel, evsel->core.cpus, evsel->core.threads);
1399 if (err < 0)
1400 goto out_err;
1401 }
1402
1403 return 0;
1404 out_err:
1405 evlist__close(evlist);
1406 errno = -err;
1407 return err;
1408 }
1409
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))1410 int evlist__prepare_workload(struct evlist *evlist, struct target *target, const char *argv[],
1411 bool pipe_output, void (*exec_error)(int signo, siginfo_t *info, void *ucontext))
1412 {
1413 int child_ready_pipe[2], go_pipe[2];
1414 char bf;
1415
1416 evlist->workload.cork_fd = -1;
1417
1418 if (pipe(child_ready_pipe) < 0) {
1419 perror("failed to create 'ready' pipe");
1420 return -1;
1421 }
1422
1423 if (pipe(go_pipe) < 0) {
1424 perror("failed to create 'go' pipe");
1425 goto out_close_ready_pipe;
1426 }
1427
1428 evlist->workload.pid = fork();
1429 if (evlist->workload.pid < 0) {
1430 perror("failed to fork");
1431 goto out_close_pipes;
1432 }
1433
1434 if (!evlist->workload.pid) {
1435 int ret;
1436
1437 if (pipe_output)
1438 dup2(2, 1);
1439
1440 signal(SIGTERM, SIG_DFL);
1441
1442 close(child_ready_pipe[0]);
1443 close(go_pipe[1]);
1444 fcntl(go_pipe[0], F_SETFD, FD_CLOEXEC);
1445
1446 /*
1447 * Change the name of this process not to confuse --exclude-perf users
1448 * that sees 'perf' in the window up to the execvp() and thinks that
1449 * perf samples are not being excluded.
1450 */
1451 prctl(PR_SET_NAME, "perf-exec");
1452
1453 /*
1454 * Tell the parent we're ready to go
1455 */
1456 close(child_ready_pipe[1]);
1457
1458 /*
1459 * Wait until the parent tells us to go.
1460 */
1461 ret = read(go_pipe[0], &bf, 1);
1462 /*
1463 * The parent will ask for the execvp() to be performed by
1464 * writing exactly one byte, in workload.cork_fd, usually via
1465 * evlist__start_workload().
1466 *
1467 * For cancelling the workload without actually running it,
1468 * the parent will just close workload.cork_fd, without writing
1469 * anything, i.e. read will return zero and we just exit()
1470 * here (See evlist__cancel_workload()).
1471 */
1472 if (ret != 1) {
1473 if (ret == -1)
1474 perror("unable to read pipe");
1475 exit(ret);
1476 }
1477
1478 execvp(argv[0], (char **)argv);
1479
1480 if (exec_error) {
1481 union sigval val;
1482
1483 val.sival_int = errno;
1484 if (sigqueue(getppid(), SIGUSR1, val))
1485 perror(argv[0]);
1486 } else
1487 perror(argv[0]);
1488 exit(-1);
1489 }
1490
1491 if (exec_error) {
1492 struct sigaction act = {
1493 .sa_flags = SA_SIGINFO,
1494 .sa_sigaction = exec_error,
1495 };
1496 sigaction(SIGUSR1, &act, NULL);
1497 }
1498
1499 if (target__none(target)) {
1500 if (evlist->core.threads == NULL) {
1501 fprintf(stderr, "FATAL: evlist->threads need to be set at this point (%s:%d).\n",
1502 __func__, __LINE__);
1503 goto out_close_pipes;
1504 }
1505 perf_thread_map__set_pid(evlist->core.threads, 0, evlist->workload.pid);
1506 }
1507
1508 close(child_ready_pipe[1]);
1509 close(go_pipe[0]);
1510 /*
1511 * wait for child to settle
1512 */
1513 if (read(child_ready_pipe[0], &bf, 1) == -1) {
1514 perror("unable to read pipe");
1515 goto out_close_pipes;
1516 }
1517
1518 fcntl(go_pipe[1], F_SETFD, FD_CLOEXEC);
1519 evlist->workload.cork_fd = go_pipe[1];
1520 close(child_ready_pipe[0]);
1521 return 0;
1522
1523 out_close_pipes:
1524 close(go_pipe[0]);
1525 close(go_pipe[1]);
1526 out_close_ready_pipe:
1527 close(child_ready_pipe[0]);
1528 close(child_ready_pipe[1]);
1529 return -1;
1530 }
1531
evlist__start_workload(struct evlist * evlist)1532 int evlist__start_workload(struct evlist *evlist)
1533 {
1534 if (evlist->workload.cork_fd >= 0) {
1535 char bf = 0;
1536 int ret;
1537 /*
1538 * Remove the cork, let it rip!
1539 */
1540 ret = write(evlist->workload.cork_fd, &bf, 1);
1541 if (ret < 0)
1542 perror("unable to write to pipe");
1543
1544 close(evlist->workload.cork_fd);
1545 evlist->workload.cork_fd = -1;
1546 return ret;
1547 }
1548
1549 return 0;
1550 }
1551
evlist__cancel_workload(struct evlist * evlist)1552 void evlist__cancel_workload(struct evlist *evlist)
1553 {
1554 int status;
1555
1556 if (evlist->workload.cork_fd >= 0) {
1557 close(evlist->workload.cork_fd);
1558 evlist->workload.cork_fd = -1;
1559 waitpid(evlist->workload.pid, &status, WNOHANG);
1560 }
1561 }
1562
evlist__parse_sample(struct evlist * evlist,union perf_event * event,struct perf_sample * sample)1563 int evlist__parse_sample(struct evlist *evlist, union perf_event *event, struct perf_sample *sample)
1564 {
1565 struct evsel *evsel = evlist__event2evsel(evlist, event);
1566 int ret;
1567
1568 if (!evsel)
1569 return -EFAULT;
1570 ret = evsel__parse_sample(evsel, event, sample);
1571 if (ret)
1572 return ret;
1573 if (perf_guest && sample->id) {
1574 struct perf_sample_id *sid = evlist__id2sid(evlist, sample->id);
1575
1576 if (sid) {
1577 sample->machine_pid = sid->machine_pid;
1578 sample->vcpu = sid->vcpu.cpu;
1579 }
1580 }
1581 return 0;
1582 }
1583
evlist__parse_sample_timestamp(struct evlist * evlist,union perf_event * event,u64 * timestamp)1584 int evlist__parse_sample_timestamp(struct evlist *evlist, union perf_event *event, u64 *timestamp)
1585 {
1586 struct evsel *evsel = evlist__event2evsel(evlist, event);
1587
1588 if (!evsel)
1589 return -EFAULT;
1590 return evsel__parse_sample_timestamp(evsel, event, timestamp);
1591 }
1592
evlist__strerror_open(struct evlist * evlist,int err,char * buf,size_t size)1593 int evlist__strerror_open(struct evlist *evlist, int err, char *buf, size_t size)
1594 {
1595 int printed, value;
1596 char sbuf[STRERR_BUFSIZE], *emsg = str_error_r(err, sbuf, sizeof(sbuf));
1597
1598 switch (err) {
1599 case EACCES:
1600 case EPERM:
1601 printed = scnprintf(buf, size,
1602 "Error:\t%s.\n"
1603 "Hint:\tCheck /proc/sys/kernel/perf_event_paranoid setting.", emsg);
1604
1605 value = perf_event_paranoid();
1606
1607 printed += scnprintf(buf + printed, size - printed, "\nHint:\t");
1608
1609 if (value >= 2) {
1610 printed += scnprintf(buf + printed, size - printed,
1611 "For your workloads it needs to be <= 1\nHint:\t");
1612 }
1613 printed += scnprintf(buf + printed, size - printed,
1614 "For system wide tracing it needs to be set to -1.\n");
1615
1616 printed += scnprintf(buf + printed, size - printed,
1617 "Hint:\tTry: 'sudo sh -c \"echo -1 > /proc/sys/kernel/perf_event_paranoid\"'\n"
1618 "Hint:\tThe current value is %d.", value);
1619 break;
1620 case EINVAL: {
1621 struct evsel *first = evlist__first(evlist);
1622 int max_freq;
1623
1624 if (sysctl__read_int("kernel/perf_event_max_sample_rate", &max_freq) < 0)
1625 goto out_default;
1626
1627 if (first->core.attr.sample_freq < (u64)max_freq)
1628 goto out_default;
1629
1630 printed = scnprintf(buf, size,
1631 "Error:\t%s.\n"
1632 "Hint:\tCheck /proc/sys/kernel/perf_event_max_sample_rate.\n"
1633 "Hint:\tThe current value is %d and %" PRIu64 " is being requested.",
1634 emsg, max_freq, first->core.attr.sample_freq);
1635 break;
1636 }
1637 default:
1638 out_default:
1639 scnprintf(buf, size, "%s", emsg);
1640 break;
1641 }
1642
1643 return 0;
1644 }
1645
evlist__strerror_mmap(struct evlist * evlist,int err,char * buf,size_t size)1646 int evlist__strerror_mmap(struct evlist *evlist, int err, char *buf, size_t size)
1647 {
1648 char sbuf[STRERR_BUFSIZE], *emsg = str_error_r(err, sbuf, sizeof(sbuf));
1649 int pages_attempted = evlist->core.mmap_len / 1024, pages_max_per_user, printed = 0;
1650
1651 switch (err) {
1652 case EPERM:
1653 sysctl__read_int("kernel/perf_event_mlock_kb", &pages_max_per_user);
1654 printed += scnprintf(buf + printed, size - printed,
1655 "Error:\t%s.\n"
1656 "Hint:\tCheck /proc/sys/kernel/perf_event_mlock_kb (%d kB) setting.\n"
1657 "Hint:\tTried using %zd kB.\n",
1658 emsg, pages_max_per_user, pages_attempted);
1659
1660 if (pages_attempted >= pages_max_per_user) {
1661 printed += scnprintf(buf + printed, size - printed,
1662 "Hint:\tTry 'sudo sh -c \"echo %d > /proc/sys/kernel/perf_event_mlock_kb\"', or\n",
1663 pages_max_per_user + pages_attempted);
1664 }
1665
1666 printed += scnprintf(buf + printed, size - printed,
1667 "Hint:\tTry using a smaller -m/--mmap-pages value.");
1668 break;
1669 default:
1670 scnprintf(buf, size, "%s", emsg);
1671 break;
1672 }
1673
1674 return 0;
1675 }
1676
evlist__to_front(struct evlist * evlist,struct evsel * move_evsel)1677 void evlist__to_front(struct evlist *evlist, struct evsel *move_evsel)
1678 {
1679 struct evsel *evsel, *n;
1680 LIST_HEAD(move);
1681
1682 if (move_evsel == evlist__first(evlist))
1683 return;
1684
1685 evlist__for_each_entry_safe(evlist, n, evsel) {
1686 if (evsel__leader(evsel) == evsel__leader(move_evsel))
1687 list_move_tail(&evsel->core.node, &move);
1688 }
1689
1690 list_splice(&move, &evlist->core.entries);
1691 }
1692
evlist__get_tracking_event(struct evlist * evlist)1693 struct evsel *evlist__get_tracking_event(struct evlist *evlist)
1694 {
1695 struct evsel *evsel;
1696
1697 evlist__for_each_entry(evlist, evsel) {
1698 if (evsel->tracking)
1699 return evsel;
1700 }
1701
1702 return evlist__first(evlist);
1703 }
1704
evlist__set_tracking_event(struct evlist * evlist,struct evsel * tracking_evsel)1705 void evlist__set_tracking_event(struct evlist *evlist, struct evsel *tracking_evsel)
1706 {
1707 struct evsel *evsel;
1708
1709 if (tracking_evsel->tracking)
1710 return;
1711
1712 evlist__for_each_entry(evlist, evsel) {
1713 if (evsel != tracking_evsel)
1714 evsel->tracking = false;
1715 }
1716
1717 tracking_evsel->tracking = true;
1718 }
1719
evlist__findnew_tracking_event(struct evlist * evlist,bool system_wide)1720 struct evsel *evlist__findnew_tracking_event(struct evlist *evlist, bool system_wide)
1721 {
1722 struct evsel *evsel;
1723
1724 evsel = evlist__get_tracking_event(evlist);
1725 if (!evsel__is_dummy_event(evsel)) {
1726 evsel = evlist__add_aux_dummy(evlist, system_wide);
1727 if (!evsel)
1728 return NULL;
1729
1730 evlist__set_tracking_event(evlist, evsel);
1731 } else if (system_wide) {
1732 perf_evlist__go_system_wide(&evlist->core, &evsel->core);
1733 }
1734
1735 return evsel;
1736 }
1737
evlist__find_evsel_by_str(struct evlist * evlist,const char * str)1738 struct evsel *evlist__find_evsel_by_str(struct evlist *evlist, const char *str)
1739 {
1740 struct evsel *evsel;
1741
1742 evlist__for_each_entry(evlist, evsel) {
1743 if (!evsel->name)
1744 continue;
1745 if (evsel__name_is(evsel, str))
1746 return evsel;
1747 }
1748
1749 return NULL;
1750 }
1751
evlist__toggle_bkw_mmap(struct evlist * evlist,enum bkw_mmap_state state)1752 void evlist__toggle_bkw_mmap(struct evlist *evlist, enum bkw_mmap_state state)
1753 {
1754 enum bkw_mmap_state old_state = evlist->bkw_mmap_state;
1755 enum action {
1756 NONE,
1757 PAUSE,
1758 RESUME,
1759 } action = NONE;
1760
1761 if (!evlist->overwrite_mmap)
1762 return;
1763
1764 switch (old_state) {
1765 case BKW_MMAP_NOTREADY: {
1766 if (state != BKW_MMAP_RUNNING)
1767 goto state_err;
1768 break;
1769 }
1770 case BKW_MMAP_RUNNING: {
1771 if (state != BKW_MMAP_DATA_PENDING)
1772 goto state_err;
1773 action = PAUSE;
1774 break;
1775 }
1776 case BKW_MMAP_DATA_PENDING: {
1777 if (state != BKW_MMAP_EMPTY)
1778 goto state_err;
1779 break;
1780 }
1781 case BKW_MMAP_EMPTY: {
1782 if (state != BKW_MMAP_RUNNING)
1783 goto state_err;
1784 action = RESUME;
1785 break;
1786 }
1787 default:
1788 WARN_ONCE(1, "Shouldn't get there\n");
1789 }
1790
1791 evlist->bkw_mmap_state = state;
1792
1793 switch (action) {
1794 case PAUSE:
1795 evlist__pause(evlist);
1796 break;
1797 case RESUME:
1798 evlist__resume(evlist);
1799 break;
1800 case NONE:
1801 default:
1802 break;
1803 }
1804
1805 state_err:
1806 return;
1807 }
1808
evlist__exclude_kernel(struct evlist * evlist)1809 bool evlist__exclude_kernel(struct evlist *evlist)
1810 {
1811 struct evsel *evsel;
1812
1813 evlist__for_each_entry(evlist, evsel) {
1814 if (!evsel->core.attr.exclude_kernel)
1815 return false;
1816 }
1817
1818 return true;
1819 }
1820
1821 /*
1822 * Events in data file are not collect in groups, but we still want
1823 * the group display. Set the artificial group and set the leader's
1824 * forced_leader flag to notify the display code.
1825 */
evlist__force_leader(struct evlist * evlist)1826 void evlist__force_leader(struct evlist *evlist)
1827 {
1828 if (evlist__nr_groups(evlist) == 0) {
1829 struct evsel *leader = evlist__first(evlist);
1830
1831 evlist__set_leader(evlist);
1832 leader->forced_leader = true;
1833 }
1834 }
1835
evlist__reset_weak_group(struct evlist * evsel_list,struct evsel * evsel,bool close)1836 struct evsel *evlist__reset_weak_group(struct evlist *evsel_list, struct evsel *evsel, bool close)
1837 {
1838 struct evsel *c2, *leader;
1839 bool is_open = true;
1840
1841 leader = evsel__leader(evsel);
1842
1843 pr_debug("Weak group for %s/%d failed\n",
1844 leader->name, leader->core.nr_members);
1845
1846 /*
1847 * for_each_group_member doesn't work here because it doesn't
1848 * include the first entry.
1849 */
1850 evlist__for_each_entry(evsel_list, c2) {
1851 if (c2 == evsel)
1852 is_open = false;
1853 if (evsel__has_leader(c2, leader)) {
1854 if (is_open && close)
1855 perf_evsel__close(&c2->core);
1856 /*
1857 * We want to close all members of the group and reopen
1858 * them. Some events, like Intel topdown, require being
1859 * in a group and so keep these in the group.
1860 */
1861 evsel__remove_from_group(c2, leader);
1862
1863 /*
1864 * Set this for all former members of the group
1865 * to indicate they get reopened.
1866 */
1867 c2->reset_group = true;
1868 }
1869 }
1870 /* Reset the leader count if all entries were removed. */
1871 if (leader->core.nr_members == 1)
1872 leader->core.nr_members = 0;
1873 return leader;
1874 }
1875
evlist__parse_control_fifo(const char * str,int * ctl_fd,int * ctl_fd_ack,bool * ctl_fd_close)1876 static int evlist__parse_control_fifo(const char *str, int *ctl_fd, int *ctl_fd_ack, bool *ctl_fd_close)
1877 {
1878 char *s, *p;
1879 int ret = 0, fd;
1880
1881 if (strncmp(str, "fifo:", 5))
1882 return -EINVAL;
1883
1884 str += 5;
1885 if (!*str || *str == ',')
1886 return -EINVAL;
1887
1888 s = strdup(str);
1889 if (!s)
1890 return -ENOMEM;
1891
1892 p = strchr(s, ',');
1893 if (p)
1894 *p = '\0';
1895
1896 /*
1897 * O_RDWR avoids POLLHUPs which is necessary to allow the other
1898 * end of a FIFO to be repeatedly opened and closed.
1899 */
1900 fd = open(s, O_RDWR | O_NONBLOCK | O_CLOEXEC);
1901 if (fd < 0) {
1902 pr_err("Failed to open '%s'\n", s);
1903 ret = -errno;
1904 goto out_free;
1905 }
1906 *ctl_fd = fd;
1907 *ctl_fd_close = true;
1908
1909 if (p && *++p) {
1910 /* O_RDWR | O_NONBLOCK means the other end need not be open */
1911 fd = open(p, O_RDWR | O_NONBLOCK | O_CLOEXEC);
1912 if (fd < 0) {
1913 pr_err("Failed to open '%s'\n", p);
1914 ret = -errno;
1915 goto out_free;
1916 }
1917 *ctl_fd_ack = fd;
1918 }
1919
1920 out_free:
1921 free(s);
1922 return ret;
1923 }
1924
evlist__parse_control(const char * str,int * ctl_fd,int * ctl_fd_ack,bool * ctl_fd_close)1925 int evlist__parse_control(const char *str, int *ctl_fd, int *ctl_fd_ack, bool *ctl_fd_close)
1926 {
1927 char *comma = NULL, *endptr = NULL;
1928
1929 *ctl_fd_close = false;
1930
1931 if (strncmp(str, "fd:", 3))
1932 return evlist__parse_control_fifo(str, ctl_fd, ctl_fd_ack, ctl_fd_close);
1933
1934 *ctl_fd = strtoul(&str[3], &endptr, 0);
1935 if (endptr == &str[3])
1936 return -EINVAL;
1937
1938 comma = strchr(str, ',');
1939 if (comma) {
1940 if (endptr != comma)
1941 return -EINVAL;
1942
1943 *ctl_fd_ack = strtoul(comma + 1, &endptr, 0);
1944 if (endptr == comma + 1 || *endptr != '\0')
1945 return -EINVAL;
1946 }
1947
1948 return 0;
1949 }
1950
evlist__close_control(int ctl_fd,int ctl_fd_ack,bool * ctl_fd_close)1951 void evlist__close_control(int ctl_fd, int ctl_fd_ack, bool *ctl_fd_close)
1952 {
1953 if (*ctl_fd_close) {
1954 *ctl_fd_close = false;
1955 close(ctl_fd);
1956 if (ctl_fd_ack >= 0)
1957 close(ctl_fd_ack);
1958 }
1959 }
1960
evlist__initialize_ctlfd(struct evlist * evlist,int fd,int ack)1961 int evlist__initialize_ctlfd(struct evlist *evlist, int fd, int ack)
1962 {
1963 if (fd == -1) {
1964 pr_debug("Control descriptor is not initialized\n");
1965 return 0;
1966 }
1967
1968 evlist->ctl_fd.pos = perf_evlist__add_pollfd(&evlist->core, fd, NULL, POLLIN,
1969 fdarray_flag__nonfilterable |
1970 fdarray_flag__non_perf_event);
1971 if (evlist->ctl_fd.pos < 0) {
1972 evlist->ctl_fd.pos = -1;
1973 pr_err("Failed to add ctl fd entry: %m\n");
1974 return -1;
1975 }
1976
1977 evlist->ctl_fd.fd = fd;
1978 evlist->ctl_fd.ack = ack;
1979
1980 return 0;
1981 }
1982
evlist__ctlfd_initialized(struct evlist * evlist)1983 bool evlist__ctlfd_initialized(struct evlist *evlist)
1984 {
1985 return evlist->ctl_fd.pos >= 0;
1986 }
1987
evlist__finalize_ctlfd(struct evlist * evlist)1988 int evlist__finalize_ctlfd(struct evlist *evlist)
1989 {
1990 struct pollfd *entries = evlist->core.pollfd.entries;
1991
1992 if (!evlist__ctlfd_initialized(evlist))
1993 return 0;
1994
1995 entries[evlist->ctl_fd.pos].fd = -1;
1996 entries[evlist->ctl_fd.pos].events = 0;
1997 entries[evlist->ctl_fd.pos].revents = 0;
1998
1999 evlist->ctl_fd.pos = -1;
2000 evlist->ctl_fd.ack = -1;
2001 evlist->ctl_fd.fd = -1;
2002
2003 return 0;
2004 }
2005
evlist__ctlfd_recv(struct evlist * evlist,enum evlist_ctl_cmd * cmd,char * cmd_data,size_t data_size)2006 static int evlist__ctlfd_recv(struct evlist *evlist, enum evlist_ctl_cmd *cmd,
2007 char *cmd_data, size_t data_size)
2008 {
2009 int err;
2010 char c;
2011 size_t bytes_read = 0;
2012
2013 *cmd = EVLIST_CTL_CMD_UNSUPPORTED;
2014 memset(cmd_data, 0, data_size);
2015 data_size--;
2016
2017 do {
2018 err = read(evlist->ctl_fd.fd, &c, 1);
2019 if (err > 0) {
2020 if (c == '\n' || c == '\0')
2021 break;
2022 cmd_data[bytes_read++] = c;
2023 if (bytes_read == data_size)
2024 break;
2025 continue;
2026 } else if (err == -1) {
2027 if (errno == EINTR)
2028 continue;
2029 if (errno == EAGAIN || errno == EWOULDBLOCK)
2030 err = 0;
2031 else
2032 pr_err("Failed to read from ctlfd %d: %m\n", evlist->ctl_fd.fd);
2033 }
2034 break;
2035 } while (1);
2036
2037 pr_debug("Message from ctl_fd: \"%s%s\"\n", cmd_data,
2038 bytes_read == data_size ? "" : c == '\n' ? "\\n" : "\\0");
2039
2040 if (bytes_read > 0) {
2041 if (!strncmp(cmd_data, EVLIST_CTL_CMD_ENABLE_TAG,
2042 (sizeof(EVLIST_CTL_CMD_ENABLE_TAG)-1))) {
2043 *cmd = EVLIST_CTL_CMD_ENABLE;
2044 } else if (!strncmp(cmd_data, EVLIST_CTL_CMD_DISABLE_TAG,
2045 (sizeof(EVLIST_CTL_CMD_DISABLE_TAG)-1))) {
2046 *cmd = EVLIST_CTL_CMD_DISABLE;
2047 } else if (!strncmp(cmd_data, EVLIST_CTL_CMD_SNAPSHOT_TAG,
2048 (sizeof(EVLIST_CTL_CMD_SNAPSHOT_TAG)-1))) {
2049 *cmd = EVLIST_CTL_CMD_SNAPSHOT;
2050 pr_debug("is snapshot\n");
2051 } else if (!strncmp(cmd_data, EVLIST_CTL_CMD_EVLIST_TAG,
2052 (sizeof(EVLIST_CTL_CMD_EVLIST_TAG)-1))) {
2053 *cmd = EVLIST_CTL_CMD_EVLIST;
2054 } else if (!strncmp(cmd_data, EVLIST_CTL_CMD_STOP_TAG,
2055 (sizeof(EVLIST_CTL_CMD_STOP_TAG)-1))) {
2056 *cmd = EVLIST_CTL_CMD_STOP;
2057 } else if (!strncmp(cmd_data, EVLIST_CTL_CMD_PING_TAG,
2058 (sizeof(EVLIST_CTL_CMD_PING_TAG)-1))) {
2059 *cmd = EVLIST_CTL_CMD_PING;
2060 }
2061 }
2062
2063 return bytes_read ? (int)bytes_read : err;
2064 }
2065
evlist__ctlfd_ack(struct evlist * evlist)2066 int evlist__ctlfd_ack(struct evlist *evlist)
2067 {
2068 int err;
2069
2070 if (evlist->ctl_fd.ack == -1)
2071 return 0;
2072
2073 err = write(evlist->ctl_fd.ack, EVLIST_CTL_CMD_ACK_TAG,
2074 sizeof(EVLIST_CTL_CMD_ACK_TAG));
2075 if (err == -1)
2076 pr_err("failed to write to ctl_ack_fd %d: %m\n", evlist->ctl_fd.ack);
2077
2078 return err;
2079 }
2080
get_cmd_arg(char * cmd_data,size_t cmd_size,char ** arg)2081 static int get_cmd_arg(char *cmd_data, size_t cmd_size, char **arg)
2082 {
2083 char *data = cmd_data + cmd_size;
2084
2085 /* no argument */
2086 if (!*data)
2087 return 0;
2088
2089 /* there's argument */
2090 if (*data == ' ') {
2091 *arg = data + 1;
2092 return 1;
2093 }
2094
2095 /* malformed */
2096 return -1;
2097 }
2098
evlist__ctlfd_enable(struct evlist * evlist,char * cmd_data,bool enable)2099 static int evlist__ctlfd_enable(struct evlist *evlist, char *cmd_data, bool enable)
2100 {
2101 struct evsel *evsel;
2102 char *name;
2103 int err;
2104
2105 err = get_cmd_arg(cmd_data,
2106 enable ? sizeof(EVLIST_CTL_CMD_ENABLE_TAG) - 1 :
2107 sizeof(EVLIST_CTL_CMD_DISABLE_TAG) - 1,
2108 &name);
2109 if (err < 0) {
2110 pr_info("failed: wrong command\n");
2111 return -1;
2112 }
2113
2114 if (err) {
2115 evsel = evlist__find_evsel_by_str(evlist, name);
2116 if (evsel) {
2117 if (enable)
2118 evlist__enable_evsel(evlist, name);
2119 else
2120 evlist__disable_evsel(evlist, name);
2121 pr_info("Event %s %s\n", evsel->name,
2122 enable ? "enabled" : "disabled");
2123 } else {
2124 pr_info("failed: can't find '%s' event\n", name);
2125 }
2126 } else {
2127 if (enable) {
2128 evlist__enable(evlist);
2129 pr_info(EVLIST_ENABLED_MSG);
2130 } else {
2131 evlist__disable(evlist);
2132 pr_info(EVLIST_DISABLED_MSG);
2133 }
2134 }
2135
2136 return 0;
2137 }
2138
evlist__ctlfd_list(struct evlist * evlist,char * cmd_data)2139 static int evlist__ctlfd_list(struct evlist *evlist, char *cmd_data)
2140 {
2141 struct perf_attr_details details = { .verbose = false, };
2142 struct evsel *evsel;
2143 char *arg;
2144 int err;
2145
2146 err = get_cmd_arg(cmd_data,
2147 sizeof(EVLIST_CTL_CMD_EVLIST_TAG) - 1,
2148 &arg);
2149 if (err < 0) {
2150 pr_info("failed: wrong command\n");
2151 return -1;
2152 }
2153
2154 if (err) {
2155 if (!strcmp(arg, "-v")) {
2156 details.verbose = true;
2157 } else if (!strcmp(arg, "-g")) {
2158 details.event_group = true;
2159 } else if (!strcmp(arg, "-F")) {
2160 details.freq = true;
2161 } else {
2162 pr_info("failed: wrong command\n");
2163 return -1;
2164 }
2165 }
2166
2167 evlist__for_each_entry(evlist, evsel)
2168 evsel__fprintf(evsel, &details, stderr);
2169
2170 return 0;
2171 }
2172
evlist__ctlfd_process(struct evlist * evlist,enum evlist_ctl_cmd * cmd)2173 int evlist__ctlfd_process(struct evlist *evlist, enum evlist_ctl_cmd *cmd)
2174 {
2175 int err = 0;
2176 char cmd_data[EVLIST_CTL_CMD_MAX_LEN];
2177 int ctlfd_pos = evlist->ctl_fd.pos;
2178 struct pollfd *entries = evlist->core.pollfd.entries;
2179
2180 if (!evlist__ctlfd_initialized(evlist) || !entries[ctlfd_pos].revents)
2181 return 0;
2182
2183 if (entries[ctlfd_pos].revents & POLLIN) {
2184 err = evlist__ctlfd_recv(evlist, cmd, cmd_data,
2185 EVLIST_CTL_CMD_MAX_LEN);
2186 if (err > 0) {
2187 switch (*cmd) {
2188 case EVLIST_CTL_CMD_ENABLE:
2189 case EVLIST_CTL_CMD_DISABLE:
2190 err = evlist__ctlfd_enable(evlist, cmd_data,
2191 *cmd == EVLIST_CTL_CMD_ENABLE);
2192 break;
2193 case EVLIST_CTL_CMD_EVLIST:
2194 err = evlist__ctlfd_list(evlist, cmd_data);
2195 break;
2196 case EVLIST_CTL_CMD_SNAPSHOT:
2197 case EVLIST_CTL_CMD_STOP:
2198 case EVLIST_CTL_CMD_PING:
2199 break;
2200 case EVLIST_CTL_CMD_ACK:
2201 case EVLIST_CTL_CMD_UNSUPPORTED:
2202 default:
2203 pr_debug("ctlfd: unsupported %d\n", *cmd);
2204 break;
2205 }
2206 if (!(*cmd == EVLIST_CTL_CMD_ACK || *cmd == EVLIST_CTL_CMD_UNSUPPORTED ||
2207 *cmd == EVLIST_CTL_CMD_SNAPSHOT))
2208 evlist__ctlfd_ack(evlist);
2209 }
2210 }
2211
2212 if (entries[ctlfd_pos].revents & (POLLHUP | POLLERR))
2213 evlist__finalize_ctlfd(evlist);
2214 else
2215 entries[ctlfd_pos].revents = 0;
2216
2217 return err;
2218 }
2219
2220 /**
2221 * struct event_enable_time - perf record -D/--delay single time range.
2222 * @start: start of time range to enable events in milliseconds
2223 * @end: end of time range to enable events in milliseconds
2224 *
2225 * N.B. this structure is also accessed as an array of int.
2226 */
2227 struct event_enable_time {
2228 int start;
2229 int end;
2230 };
2231
parse_event_enable_time(const char * str,struct event_enable_time * range,bool first)2232 static int parse_event_enable_time(const char *str, struct event_enable_time *range, bool first)
2233 {
2234 const char *fmt = first ? "%u - %u %n" : " , %u - %u %n";
2235 int ret, start, end, n;
2236
2237 ret = sscanf(str, fmt, &start, &end, &n);
2238 if (ret != 2 || end <= start)
2239 return -EINVAL;
2240 if (range) {
2241 range->start = start;
2242 range->end = end;
2243 }
2244 return n;
2245 }
2246
parse_event_enable_times(const char * str,struct event_enable_time * range)2247 static ssize_t parse_event_enable_times(const char *str, struct event_enable_time *range)
2248 {
2249 int incr = !!range;
2250 bool first = true;
2251 ssize_t ret, cnt;
2252
2253 for (cnt = 0; *str; cnt++) {
2254 ret = parse_event_enable_time(str, range, first);
2255 if (ret < 0)
2256 return ret;
2257 /* Check no overlap */
2258 if (!first && range && range->start <= range[-1].end)
2259 return -EINVAL;
2260 str += ret;
2261 range += incr;
2262 first = false;
2263 }
2264 return cnt;
2265 }
2266
2267 /**
2268 * struct event_enable_timer - control structure for perf record -D/--delay.
2269 * @evlist: event list
2270 * @times: time ranges that events are enabled (N.B. this is also accessed as an
2271 * array of int)
2272 * @times_cnt: number of time ranges
2273 * @timerfd: timer file descriptor
2274 * @pollfd_pos: position in @evlist array of file descriptors to poll (fdarray)
2275 * @times_step: current position in (int *)@times)[],
2276 * refer event_enable_timer__process()
2277 *
2278 * Note, this structure is only used when there are time ranges, not when there
2279 * is only an initial delay.
2280 */
2281 struct event_enable_timer {
2282 struct evlist *evlist;
2283 struct event_enable_time *times;
2284 size_t times_cnt;
2285 int timerfd;
2286 int pollfd_pos;
2287 size_t times_step;
2288 };
2289
str_to_delay(const char * str)2290 static int str_to_delay(const char *str)
2291 {
2292 char *endptr;
2293 long d;
2294
2295 d = strtol(str, &endptr, 10);
2296 if (*endptr || d > INT_MAX || d < -1)
2297 return 0;
2298 return d;
2299 }
2300
evlist__parse_event_enable_time(struct evlist * evlist,struct record_opts * opts,const char * str,int unset)2301 int evlist__parse_event_enable_time(struct evlist *evlist, struct record_opts *opts,
2302 const char *str, int unset)
2303 {
2304 enum fdarray_flags flags = fdarray_flag__nonfilterable | fdarray_flag__non_perf_event;
2305 struct event_enable_timer *eet;
2306 ssize_t times_cnt;
2307 ssize_t ret;
2308 int err;
2309
2310 if (unset)
2311 return 0;
2312
2313 opts->target.initial_delay = str_to_delay(str);
2314 if (opts->target.initial_delay)
2315 return 0;
2316
2317 ret = parse_event_enable_times(str, NULL);
2318 if (ret < 0)
2319 return ret;
2320
2321 times_cnt = ret;
2322 if (times_cnt == 0)
2323 return -EINVAL;
2324
2325 eet = zalloc(sizeof(*eet));
2326 if (!eet)
2327 return -ENOMEM;
2328
2329 eet->times = calloc(times_cnt, sizeof(*eet->times));
2330 if (!eet->times) {
2331 err = -ENOMEM;
2332 goto free_eet;
2333 }
2334
2335 if (parse_event_enable_times(str, eet->times) != times_cnt) {
2336 err = -EINVAL;
2337 goto free_eet_times;
2338 }
2339
2340 eet->times_cnt = times_cnt;
2341
2342 eet->timerfd = timerfd_create(CLOCK_MONOTONIC, TFD_CLOEXEC);
2343 if (eet->timerfd == -1) {
2344 err = -errno;
2345 pr_err("timerfd_create failed: %s\n", strerror(errno));
2346 goto free_eet_times;
2347 }
2348
2349 eet->pollfd_pos = perf_evlist__add_pollfd(&evlist->core, eet->timerfd, NULL, POLLIN, flags);
2350 if (eet->pollfd_pos < 0) {
2351 err = eet->pollfd_pos;
2352 goto close_timerfd;
2353 }
2354
2355 eet->evlist = evlist;
2356 evlist->eet = eet;
2357 opts->target.initial_delay = eet->times[0].start;
2358
2359 return 0;
2360
2361 close_timerfd:
2362 close(eet->timerfd);
2363 free_eet_times:
2364 zfree(&eet->times);
2365 free_eet:
2366 free(eet);
2367 return err;
2368 }
2369
event_enable_timer__set_timer(struct event_enable_timer * eet,int ms)2370 static int event_enable_timer__set_timer(struct event_enable_timer *eet, int ms)
2371 {
2372 struct itimerspec its = {
2373 .it_value.tv_sec = ms / MSEC_PER_SEC,
2374 .it_value.tv_nsec = (ms % MSEC_PER_SEC) * NSEC_PER_MSEC,
2375 };
2376 int err = 0;
2377
2378 if (timerfd_settime(eet->timerfd, 0, &its, NULL) < 0) {
2379 err = -errno;
2380 pr_err("timerfd_settime failed: %s\n", strerror(errno));
2381 }
2382 return err;
2383 }
2384
event_enable_timer__start(struct event_enable_timer * eet)2385 int event_enable_timer__start(struct event_enable_timer *eet)
2386 {
2387 int ms;
2388
2389 if (!eet)
2390 return 0;
2391
2392 ms = eet->times[0].end - eet->times[0].start;
2393 eet->times_step = 1;
2394
2395 return event_enable_timer__set_timer(eet, ms);
2396 }
2397
event_enable_timer__process(struct event_enable_timer * eet)2398 int event_enable_timer__process(struct event_enable_timer *eet)
2399 {
2400 struct pollfd *entries;
2401 short revents;
2402
2403 if (!eet)
2404 return 0;
2405
2406 entries = eet->evlist->core.pollfd.entries;
2407 revents = entries[eet->pollfd_pos].revents;
2408 entries[eet->pollfd_pos].revents = 0;
2409
2410 if (revents & POLLIN) {
2411 size_t step = eet->times_step;
2412 size_t pos = step / 2;
2413
2414 if (step & 1) {
2415 evlist__disable_non_dummy(eet->evlist);
2416 pr_info(EVLIST_DISABLED_MSG);
2417 if (pos >= eet->times_cnt - 1) {
2418 /* Disarm timer */
2419 event_enable_timer__set_timer(eet, 0);
2420 return 1; /* Stop */
2421 }
2422 } else {
2423 evlist__enable_non_dummy(eet->evlist);
2424 pr_info(EVLIST_ENABLED_MSG);
2425 }
2426
2427 step += 1;
2428 pos = step / 2;
2429
2430 if (pos < eet->times_cnt) {
2431 int *times = (int *)eet->times; /* Accessing 'times' as array of int */
2432 int ms = times[step] - times[step - 1];
2433
2434 eet->times_step = step;
2435 return event_enable_timer__set_timer(eet, ms);
2436 }
2437 }
2438
2439 return 0;
2440 }
2441
event_enable_timer__exit(struct event_enable_timer ** ep)2442 void event_enable_timer__exit(struct event_enable_timer **ep)
2443 {
2444 if (!ep || !*ep)
2445 return;
2446 zfree(&(*ep)->times);
2447 zfree(ep);
2448 }
2449
evlist__find_evsel(struct evlist * evlist,int idx)2450 struct evsel *evlist__find_evsel(struct evlist *evlist, int idx)
2451 {
2452 struct evsel *evsel;
2453
2454 evlist__for_each_entry(evlist, evsel) {
2455 if (evsel->core.idx == idx)
2456 return evsel;
2457 }
2458 return NULL;
2459 }
2460
evlist__scnprintf_evsels(struct evlist * evlist,size_t size,char * bf)2461 int evlist__scnprintf_evsels(struct evlist *evlist, size_t size, char *bf)
2462 {
2463 struct evsel *evsel;
2464 int printed = 0;
2465
2466 evlist__for_each_entry(evlist, evsel) {
2467 if (evsel__is_dummy_event(evsel))
2468 continue;
2469 if (size > (strlen(evsel__name(evsel)) + (printed ? 2 : 1))) {
2470 printed += scnprintf(bf + printed, size - printed, "%s%s", printed ? "," : "", evsel__name(evsel));
2471 } else {
2472 printed += scnprintf(bf + printed, size - printed, "%s...", printed ? "," : "");
2473 break;
2474 }
2475 }
2476
2477 return printed;
2478 }
2479
evlist__check_mem_load_aux(struct evlist * evlist)2480 void evlist__check_mem_load_aux(struct evlist *evlist)
2481 {
2482 struct evsel *leader, *evsel, *pos;
2483
2484 /*
2485 * For some platforms, the 'mem-loads' event is required to use
2486 * together with 'mem-loads-aux' within a group and 'mem-loads-aux'
2487 * must be the group leader. Now we disable this group before reporting
2488 * because 'mem-loads-aux' is just an auxiliary event. It doesn't carry
2489 * any valid memory load information.
2490 */
2491 evlist__for_each_entry(evlist, evsel) {
2492 leader = evsel__leader(evsel);
2493 if (leader == evsel)
2494 continue;
2495
2496 if (leader->name && strstr(leader->name, "mem-loads-aux")) {
2497 for_each_group_evsel(pos, leader) {
2498 evsel__set_leader(pos, pos);
2499 pos->core.nr_members = 0;
2500 }
2501 }
2502 }
2503 }
2504
2505 /**
2506 * evlist__warn_user_requested_cpus() - Check each evsel against requested CPUs
2507 * and warn if the user CPU list is inapplicable for the event's PMU's
2508 * CPUs. Not core PMUs list a CPU in sysfs, but this may be overwritten by a
2509 * user requested CPU and so any online CPU is applicable. Core PMUs handle
2510 * events on the CPUs in their list and otherwise the event isn't supported.
2511 * @evlist: The list of events being checked.
2512 * @cpu_list: The user provided list of CPUs.
2513 */
evlist__warn_user_requested_cpus(struct evlist * evlist,const char * cpu_list)2514 void evlist__warn_user_requested_cpus(struct evlist *evlist, const char *cpu_list)
2515 {
2516 struct perf_cpu_map *user_requested_cpus;
2517 struct evsel *pos;
2518
2519 if (!cpu_list)
2520 return;
2521
2522 user_requested_cpus = perf_cpu_map__new(cpu_list);
2523 if (!user_requested_cpus)
2524 return;
2525
2526 evlist__for_each_entry(evlist, pos) {
2527 struct perf_cpu_map *intersect, *to_test;
2528 const struct perf_pmu *pmu = evsel__find_pmu(pos);
2529
2530 to_test = pmu && pmu->is_core ? pmu->cpus : cpu_map__online();
2531 intersect = perf_cpu_map__intersect(to_test, user_requested_cpus);
2532 if (!perf_cpu_map__equal(intersect, user_requested_cpus)) {
2533 char buf[128];
2534
2535 cpu_map__snprint(to_test, buf, sizeof(buf));
2536 pr_warning("WARNING: A requested CPU in '%s' is not supported by PMU '%s' (CPUs %s) for event '%s'\n",
2537 cpu_list, pmu ? pmu->name : "cpu", buf, evsel__name(pos));
2538 }
2539 perf_cpu_map__put(intersect);
2540 }
2541 perf_cpu_map__put(user_requested_cpus);
2542 }
2543
evlist__uniquify_name(struct evlist * evlist)2544 void evlist__uniquify_name(struct evlist *evlist)
2545 {
2546 struct evsel *pos;
2547 char *new_name;
2548 int ret;
2549
2550 if (perf_pmus__num_core_pmus() == 1)
2551 return;
2552
2553 evlist__for_each_entry(evlist, pos) {
2554 if (!evsel__is_hybrid(pos))
2555 continue;
2556
2557 if (strchr(pos->name, '/'))
2558 continue;
2559
2560 ret = asprintf(&new_name, "%s/%s/",
2561 pos->pmu_name, pos->name);
2562 if (ret) {
2563 free(pos->name);
2564 pos->name = new_name;
2565 }
2566 }
2567 }
2568