1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2011, Red Hat Inc, Arnaldo Carvalho de Melo <acme@redhat.com>
5 * Parts came from builtin-{top,stat,record}.c, see those files for further
13 #include "util/mmap.h"
14 #include "thread_map.h"
21 #include "bpf_counter.h"
22 #include <internal/lib.h> // page_size
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"
32 #include "util/sample.h"
33 #include "util/bpf-filter.h"
34 #include "util/stat.h"
35 #include "util/util.h"
41 #include "parse-events.h"
42 #include <subcmd/parse-options.h>
45 #include <sys/ioctl.h>
47 #include <sys/prctl.h>
48 #include <sys/timerfd.h>
50 #include <linux/bitops.h>
51 #include <linux/hash.h>
52 #include <linux/log2.h>
53 #include <linux/err.h>
54 #include <linux/string.h>
55 #include <linux/time64.h>
56 #include <linux/zalloc.h>
57 #include <perf/evlist.h>
58 #include <perf/evsel.h>
59 #include <perf/cpumap.h>
60 #include <perf/mmap.h>
62 #include <internal/xyarray.h>
64 #ifdef LACKS_SIGQUEUE_PROTOTYPE
65 int sigqueue(pid_t pid, int sig, const union sigval value);
68 #define FD(e, x, y) (*(int *)xyarray__entry(e->core.fd, x, y))
69 #define SID(e, x, y) xyarray__entry(e->core.sample_id, x, y)
71 void evlist__init(struct evlist *evlist, struct perf_cpu_map *cpus,
72 struct perf_thread_map *threads)
74 perf_evlist__init(&evlist->core);
75 perf_evlist__set_maps(&evlist->core, cpus, threads);
76 evlist->workload.pid = -1;
77 evlist->bkw_mmap_state = BKW_MMAP_NOTREADY;
78 evlist->ctl_fd.fd = -1;
79 evlist->ctl_fd.ack = -1;
80 evlist->ctl_fd.pos = -1;
83 struct evlist *evlist__new(void)
85 struct evlist *evlist = zalloc(sizeof(*evlist));
88 evlist__init(evlist, NULL, NULL);
93 struct evlist *evlist__new_default(void)
95 struct evlist *evlist = evlist__new();
96 bool can_profile_kernel;
102 can_profile_kernel = perf_event_paranoid_check(1);
103 err = parse_event(evlist, can_profile_kernel ? "cycles:P" : "cycles:Pu");
105 evlist__delete(evlist);
109 if (evlist->core.nr_entries > 1) {
112 evlist__for_each_entry(evlist, evsel)
113 evsel__set_sample_id(evsel, /*can_sample_identifier=*/false);
119 struct evlist *evlist__new_dummy(void)
121 struct evlist *evlist = evlist__new();
123 if (evlist && evlist__add_dummy(evlist)) {
124 evlist__delete(evlist);
132 * evlist__set_id_pos - set the positions of event ids.
133 * @evlist: selected event list
135 * Events with compatible sample types all have the same id_pos
136 * and is_pos. For convenience, put a copy on evlist.
138 void evlist__set_id_pos(struct evlist *evlist)
140 struct evsel *first = evlist__first(evlist);
142 evlist->id_pos = first->id_pos;
143 evlist->is_pos = first->is_pos;
146 static void evlist__update_id_pos(struct evlist *evlist)
150 evlist__for_each_entry(evlist, evsel)
151 evsel__calc_id_pos(evsel);
153 evlist__set_id_pos(evlist);
156 static void evlist__purge(struct evlist *evlist)
158 struct evsel *pos, *n;
160 evlist__for_each_entry_safe(evlist, n, pos) {
161 list_del_init(&pos->core.node);
166 evlist->core.nr_entries = 0;
169 void evlist__exit(struct evlist *evlist)
171 event_enable_timer__exit(&evlist->eet);
172 zfree(&evlist->mmap);
173 zfree(&evlist->overwrite_mmap);
174 perf_evlist__exit(&evlist->core);
177 void evlist__delete(struct evlist *evlist)
182 evlist__free_stats(evlist);
183 evlist__munmap(evlist);
184 evlist__close(evlist);
185 evlist__purge(evlist);
186 evlist__exit(evlist);
190 void evlist__add(struct evlist *evlist, struct evsel *entry)
192 perf_evlist__add(&evlist->core, &entry->core);
193 entry->evlist = evlist;
194 entry->tracking = !entry->core.idx;
196 if (evlist->core.nr_entries == 1)
197 evlist__set_id_pos(evlist);
200 void evlist__remove(struct evlist *evlist, struct evsel *evsel)
202 evsel->evlist = NULL;
203 perf_evlist__remove(&evlist->core, &evsel->core);
206 void evlist__splice_list_tail(struct evlist *evlist, struct list_head *list)
208 while (!list_empty(list)) {
209 struct evsel *evsel, *temp, *leader = NULL;
211 __evlist__for_each_entry_safe(list, temp, evsel) {
212 list_del_init(&evsel->core.node);
213 evlist__add(evlist, evsel);
218 __evlist__for_each_entry_safe(list, temp, evsel) {
219 if (evsel__has_leader(evsel, leader)) {
220 list_del_init(&evsel->core.node);
221 evlist__add(evlist, evsel);
227 int __evlist__set_tracepoints_handlers(struct evlist *evlist,
228 const struct evsel_str_handler *assocs, size_t nr_assocs)
233 for (i = 0; i < nr_assocs; i++) {
234 // Adding a handler for an event not in this evlist, just ignore it.
235 struct evsel *evsel = evlist__find_tracepoint_by_name(evlist, assocs[i].name);
240 if (evsel->handler != NULL)
242 evsel->handler = assocs[i].handler;
250 static void evlist__set_leader(struct evlist *evlist)
252 perf_evlist__set_leader(&evlist->core);
255 static struct evsel *evlist__dummy_event(struct evlist *evlist)
257 struct perf_event_attr attr = {
258 .type = PERF_TYPE_SOFTWARE,
259 .config = PERF_COUNT_SW_DUMMY,
260 .size = sizeof(attr), /* to capture ABI version */
261 /* Avoid frequency mode for dummy events to avoid associated timers. */
266 return evsel__new_idx(&attr, evlist->core.nr_entries);
269 int evlist__add_dummy(struct evlist *evlist)
271 struct evsel *evsel = evlist__dummy_event(evlist);
276 evlist__add(evlist, evsel);
280 struct evsel *evlist__add_aux_dummy(struct evlist *evlist, bool system_wide)
282 struct evsel *evsel = evlist__dummy_event(evlist);
287 evsel->core.attr.exclude_kernel = 1;
288 evsel->core.attr.exclude_guest = 1;
289 evsel->core.attr.exclude_hv = 1;
290 evsel->core.system_wide = system_wide;
291 evsel->no_aux_samples = true;
292 evsel->name = strdup("dummy:u");
294 evlist__add(evlist, evsel);
298 #ifdef HAVE_LIBTRACEEVENT
299 struct evsel *evlist__add_sched_switch(struct evlist *evlist, bool system_wide)
301 struct evsel *evsel = evsel__newtp_idx("sched", "sched_switch", 0);
306 evsel__set_sample_bit(evsel, CPU);
307 evsel__set_sample_bit(evsel, TIME);
309 evsel->core.system_wide = system_wide;
310 evsel->no_aux_samples = true;
312 evlist__add(evlist, evsel);
317 int evlist__add_attrs(struct evlist *evlist, struct perf_event_attr *attrs, size_t nr_attrs)
319 struct evsel *evsel, *n;
323 for (i = 0; i < nr_attrs; i++) {
324 evsel = evsel__new_idx(attrs + i, evlist->core.nr_entries + i);
326 goto out_delete_partial_list;
327 list_add_tail(&evsel->core.node, &head);
330 evlist__splice_list_tail(evlist, &head);
334 out_delete_partial_list:
335 __evlist__for_each_entry_safe(&head, n, evsel)
336 evsel__delete(evsel);
340 int __evlist__add_default_attrs(struct evlist *evlist, struct perf_event_attr *attrs, size_t nr_attrs)
344 for (i = 0; i < nr_attrs; i++)
345 event_attr_init(attrs + i);
347 return evlist__add_attrs(evlist, attrs, nr_attrs);
350 __weak int arch_evlist__add_default_attrs(struct evlist *evlist,
351 struct perf_event_attr *attrs,
357 return __evlist__add_default_attrs(evlist, attrs, nr_attrs);
360 struct evsel *evlist__find_tracepoint_by_id(struct evlist *evlist, int id)
364 evlist__for_each_entry(evlist, evsel) {
365 if (evsel->core.attr.type == PERF_TYPE_TRACEPOINT &&
366 (int)evsel->core.attr.config == id)
373 struct evsel *evlist__find_tracepoint_by_name(struct evlist *evlist, const char *name)
377 evlist__for_each_entry(evlist, evsel) {
378 if ((evsel->core.attr.type == PERF_TYPE_TRACEPOINT) &&
379 (strcmp(evsel->name, name) == 0))
386 #ifdef HAVE_LIBTRACEEVENT
387 int evlist__add_newtp(struct evlist *evlist, const char *sys, const char *name, void *handler)
389 struct evsel *evsel = evsel__newtp(sys, name);
394 evsel->handler = handler;
395 evlist__add(evlist, evsel);
400 struct evlist_cpu_iterator evlist__cpu_begin(struct evlist *evlist, struct affinity *affinity)
402 struct evlist_cpu_iterator itr = {
406 .evlist_cpu_map_idx = 0,
407 .evlist_cpu_map_nr = perf_cpu_map__nr(evlist->core.all_cpus),
408 .cpu = (struct perf_cpu){ .cpu = -1},
409 .affinity = affinity,
412 if (evlist__empty(evlist)) {
413 /* Ensure the empty list doesn't iterate. */
414 itr.evlist_cpu_map_idx = itr.evlist_cpu_map_nr;
416 itr.evsel = evlist__first(evlist);
418 itr.cpu = perf_cpu_map__cpu(evlist->core.all_cpus, 0);
419 affinity__set(itr.affinity, itr.cpu.cpu);
420 itr.cpu_map_idx = perf_cpu_map__idx(itr.evsel->core.cpus, itr.cpu);
422 * If this CPU isn't in the evsel's cpu map then advance
425 if (itr.cpu_map_idx == -1)
426 evlist_cpu_iterator__next(&itr);
432 void evlist_cpu_iterator__next(struct evlist_cpu_iterator *evlist_cpu_itr)
434 while (evlist_cpu_itr->evsel != evlist__last(evlist_cpu_itr->container)) {
435 evlist_cpu_itr->evsel = evsel__next(evlist_cpu_itr->evsel);
436 evlist_cpu_itr->cpu_map_idx =
437 perf_cpu_map__idx(evlist_cpu_itr->evsel->core.cpus,
438 evlist_cpu_itr->cpu);
439 if (evlist_cpu_itr->cpu_map_idx != -1)
442 evlist_cpu_itr->evlist_cpu_map_idx++;
443 if (evlist_cpu_itr->evlist_cpu_map_idx < evlist_cpu_itr->evlist_cpu_map_nr) {
444 evlist_cpu_itr->evsel = evlist__first(evlist_cpu_itr->container);
445 evlist_cpu_itr->cpu =
446 perf_cpu_map__cpu(evlist_cpu_itr->container->core.all_cpus,
447 evlist_cpu_itr->evlist_cpu_map_idx);
448 if (evlist_cpu_itr->affinity)
449 affinity__set(evlist_cpu_itr->affinity, evlist_cpu_itr->cpu.cpu);
450 evlist_cpu_itr->cpu_map_idx =
451 perf_cpu_map__idx(evlist_cpu_itr->evsel->core.cpus,
452 evlist_cpu_itr->cpu);
454 * If this CPU isn't in the evsel's cpu map then advance through
457 if (evlist_cpu_itr->cpu_map_idx == -1)
458 evlist_cpu_iterator__next(evlist_cpu_itr);
462 bool evlist_cpu_iterator__end(const struct evlist_cpu_iterator *evlist_cpu_itr)
464 return evlist_cpu_itr->evlist_cpu_map_idx >= evlist_cpu_itr->evlist_cpu_map_nr;
467 static int evsel__strcmp(struct evsel *pos, char *evsel_name)
471 if (evsel__is_dummy_event(pos))
473 return !evsel__name_is(pos, evsel_name);
476 static int evlist__is_enabled(struct evlist *evlist)
480 evlist__for_each_entry(evlist, pos) {
481 if (!evsel__is_group_leader(pos) || !pos->core.fd)
483 /* If at least one event is enabled, evlist is enabled. */
490 static void __evlist__disable(struct evlist *evlist, char *evsel_name, bool excl_dummy)
493 struct evlist_cpu_iterator evlist_cpu_itr;
494 struct affinity saved_affinity, *affinity = NULL;
495 bool has_imm = false;
497 // See explanation in evlist__close()
498 if (!cpu_map__is_dummy(evlist->core.user_requested_cpus)) {
499 if (affinity__setup(&saved_affinity) < 0)
501 affinity = &saved_affinity;
504 /* Disable 'immediate' events last */
505 for (int imm = 0; imm <= 1; imm++) {
506 evlist__for_each_cpu(evlist_cpu_itr, evlist, affinity) {
507 pos = evlist_cpu_itr.evsel;
508 if (evsel__strcmp(pos, evsel_name))
510 if (pos->disabled || !evsel__is_group_leader(pos) || !pos->core.fd)
512 if (excl_dummy && evsel__is_dummy_event(pos))
516 if (pos->immediate != imm)
518 evsel__disable_cpu(pos, evlist_cpu_itr.cpu_map_idx);
524 affinity__cleanup(affinity);
525 evlist__for_each_entry(evlist, pos) {
526 if (evsel__strcmp(pos, evsel_name))
528 if (!evsel__is_group_leader(pos) || !pos->core.fd)
530 if (excl_dummy && evsel__is_dummy_event(pos))
532 pos->disabled = true;
536 * If we disabled only single event, we need to check
537 * the enabled state of the evlist manually.
540 evlist->enabled = evlist__is_enabled(evlist);
542 evlist->enabled = false;
545 void evlist__disable(struct evlist *evlist)
547 __evlist__disable(evlist, NULL, false);
550 void evlist__disable_non_dummy(struct evlist *evlist)
552 __evlist__disable(evlist, NULL, true);
555 void evlist__disable_evsel(struct evlist *evlist, char *evsel_name)
557 __evlist__disable(evlist, evsel_name, false);
560 static void __evlist__enable(struct evlist *evlist, char *evsel_name, bool excl_dummy)
563 struct evlist_cpu_iterator evlist_cpu_itr;
564 struct affinity saved_affinity, *affinity = NULL;
566 // See explanation in evlist__close()
567 if (!cpu_map__is_dummy(evlist->core.user_requested_cpus)) {
568 if (affinity__setup(&saved_affinity) < 0)
570 affinity = &saved_affinity;
573 evlist__for_each_cpu(evlist_cpu_itr, evlist, affinity) {
574 pos = evlist_cpu_itr.evsel;
575 if (evsel__strcmp(pos, evsel_name))
577 if (!evsel__is_group_leader(pos) || !pos->core.fd)
579 if (excl_dummy && evsel__is_dummy_event(pos))
581 evsel__enable_cpu(pos, evlist_cpu_itr.cpu_map_idx);
583 affinity__cleanup(affinity);
584 evlist__for_each_entry(evlist, pos) {
585 if (evsel__strcmp(pos, evsel_name))
587 if (!evsel__is_group_leader(pos) || !pos->core.fd)
589 if (excl_dummy && evsel__is_dummy_event(pos))
591 pos->disabled = false;
595 * Even single event sets the 'enabled' for evlist,
596 * so the toggle can work properly and toggle to
599 evlist->enabled = true;
602 void evlist__enable(struct evlist *evlist)
604 __evlist__enable(evlist, NULL, false);
607 void evlist__enable_non_dummy(struct evlist *evlist)
609 __evlist__enable(evlist, NULL, true);
612 void evlist__enable_evsel(struct evlist *evlist, char *evsel_name)
614 __evlist__enable(evlist, evsel_name, false);
617 void evlist__toggle_enable(struct evlist *evlist)
619 (evlist->enabled ? evlist__disable : evlist__enable)(evlist);
622 int evlist__add_pollfd(struct evlist *evlist, int fd)
624 return perf_evlist__add_pollfd(&evlist->core, fd, NULL, POLLIN, fdarray_flag__default);
627 int evlist__filter_pollfd(struct evlist *evlist, short revents_and_mask)
629 return perf_evlist__filter_pollfd(&evlist->core, revents_and_mask);
632 #ifdef HAVE_EVENTFD_SUPPORT
633 int evlist__add_wakeup_eventfd(struct evlist *evlist, int fd)
635 return perf_evlist__add_pollfd(&evlist->core, fd, NULL, POLLIN,
636 fdarray_flag__nonfilterable |
637 fdarray_flag__non_perf_event);
641 int evlist__poll(struct evlist *evlist, int timeout)
643 return perf_evlist__poll(&evlist->core, timeout);
646 struct perf_sample_id *evlist__id2sid(struct evlist *evlist, u64 id)
648 struct hlist_head *head;
649 struct perf_sample_id *sid;
652 hash = hash_64(id, PERF_EVLIST__HLIST_BITS);
653 head = &evlist->core.heads[hash];
655 hlist_for_each_entry(sid, head, node)
662 struct evsel *evlist__id2evsel(struct evlist *evlist, u64 id)
664 struct perf_sample_id *sid;
666 if (evlist->core.nr_entries == 1 || !id)
667 return evlist__first(evlist);
669 sid = evlist__id2sid(evlist, id);
671 return container_of(sid->evsel, struct evsel, core);
673 if (!evlist__sample_id_all(evlist))
674 return evlist__first(evlist);
679 struct evsel *evlist__id2evsel_strict(struct evlist *evlist, u64 id)
681 struct perf_sample_id *sid;
686 sid = evlist__id2sid(evlist, id);
688 return container_of(sid->evsel, struct evsel, core);
693 static int evlist__event2id(struct evlist *evlist, union perf_event *event, u64 *id)
695 const __u64 *array = event->sample.array;
698 n = (event->header.size - sizeof(event->header)) >> 3;
700 if (event->header.type == PERF_RECORD_SAMPLE) {
701 if (evlist->id_pos >= n)
703 *id = array[evlist->id_pos];
705 if (evlist->is_pos > n)
713 struct evsel *evlist__event2evsel(struct evlist *evlist, union perf_event *event)
715 struct evsel *first = evlist__first(evlist);
716 struct hlist_head *head;
717 struct perf_sample_id *sid;
721 if (evlist->core.nr_entries == 1)
724 if (!first->core.attr.sample_id_all &&
725 event->header.type != PERF_RECORD_SAMPLE)
728 if (evlist__event2id(evlist, event, &id))
731 /* Synthesized events have an id of zero */
735 hash = hash_64(id, PERF_EVLIST__HLIST_BITS);
736 head = &evlist->core.heads[hash];
738 hlist_for_each_entry(sid, head, node) {
740 return container_of(sid->evsel, struct evsel, core);
745 static int evlist__set_paused(struct evlist *evlist, bool value)
749 if (!evlist->overwrite_mmap)
752 for (i = 0; i < evlist->core.nr_mmaps; i++) {
753 int fd = evlist->overwrite_mmap[i].core.fd;
758 err = ioctl(fd, PERF_EVENT_IOC_PAUSE_OUTPUT, value ? 1 : 0);
765 static int evlist__pause(struct evlist *evlist)
767 return evlist__set_paused(evlist, true);
770 static int evlist__resume(struct evlist *evlist)
772 return evlist__set_paused(evlist, false);
775 static void evlist__munmap_nofree(struct evlist *evlist)
780 for (i = 0; i < evlist->core.nr_mmaps; i++)
781 perf_mmap__munmap(&evlist->mmap[i].core);
783 if (evlist->overwrite_mmap)
784 for (i = 0; i < evlist->core.nr_mmaps; i++)
785 perf_mmap__munmap(&evlist->overwrite_mmap[i].core);
788 void evlist__munmap(struct evlist *evlist)
790 evlist__munmap_nofree(evlist);
791 zfree(&evlist->mmap);
792 zfree(&evlist->overwrite_mmap);
795 static void perf_mmap__unmap_cb(struct perf_mmap *map)
797 struct mmap *m = container_of(map, struct mmap, core);
802 static struct mmap *evlist__alloc_mmap(struct evlist *evlist,
808 map = zalloc(evlist->core.nr_mmaps * sizeof(struct mmap));
812 for (i = 0; i < evlist->core.nr_mmaps; i++) {
813 struct perf_mmap *prev = i ? &map[i - 1].core : NULL;
816 * When the perf_mmap() call is made we grab one refcount, plus
817 * one extra to let perf_mmap__consume() get the last
818 * events after all real references (perf_mmap__get()) are
821 * Each PERF_EVENT_IOC_SET_OUTPUT points to this mmap and
822 * thus does perf_mmap__get() on it.
824 perf_mmap__init(&map[i].core, prev, overwrite, perf_mmap__unmap_cb);
831 perf_evlist__mmap_cb_idx(struct perf_evlist *_evlist,
832 struct perf_evsel *_evsel,
833 struct perf_mmap_param *_mp,
836 struct evlist *evlist = container_of(_evlist, struct evlist, core);
837 struct mmap_params *mp = container_of(_mp, struct mmap_params, core);
838 struct evsel *evsel = container_of(_evsel, struct evsel, core);
840 auxtrace_mmap_params__set_idx(&mp->auxtrace_mp, evlist, evsel, idx);
843 static struct perf_mmap*
844 perf_evlist__mmap_cb_get(struct perf_evlist *_evlist, bool overwrite, int idx)
846 struct evlist *evlist = container_of(_evlist, struct evlist, core);
849 maps = overwrite ? evlist->overwrite_mmap : evlist->mmap;
852 maps = evlist__alloc_mmap(evlist, overwrite);
857 evlist->overwrite_mmap = maps;
858 if (evlist->bkw_mmap_state == BKW_MMAP_NOTREADY)
859 evlist__toggle_bkw_mmap(evlist, BKW_MMAP_RUNNING);
865 return &maps[idx].core;
869 perf_evlist__mmap_cb_mmap(struct perf_mmap *_map, struct perf_mmap_param *_mp,
870 int output, struct perf_cpu cpu)
872 struct mmap *map = container_of(_map, struct mmap, core);
873 struct mmap_params *mp = container_of(_mp, struct mmap_params, core);
875 return mmap__mmap(map, mp, output, cpu);
878 unsigned long perf_event_mlock_kb_in_pages(void)
883 if (sysctl__read_int("kernel/perf_event_mlock_kb", &max) < 0) {
885 * Pick a once upon a time good value, i.e. things look
886 * strange since we can't read a sysctl value, but lets not
891 max -= (page_size / 1024);
894 pages = (max * 1024) / page_size;
895 if (!is_power_of_2(pages))
896 pages = rounddown_pow_of_two(pages);
901 size_t evlist__mmap_size(unsigned long pages)
903 if (pages == UINT_MAX)
904 pages = perf_event_mlock_kb_in_pages();
905 else if (!is_power_of_2(pages))
908 return (pages + 1) * page_size;
911 static long parse_pages_arg(const char *str, unsigned long min,
914 unsigned long pages, val;
915 static struct parse_tag tags[] = {
916 { .tag = 'B', .mult = 1 },
917 { .tag = 'K', .mult = 1 << 10 },
918 { .tag = 'M', .mult = 1 << 20 },
919 { .tag = 'G', .mult = 1 << 30 },
926 val = parse_tag_value(str, tags);
927 if (val != (unsigned long) -1) {
928 /* we got file size value */
929 pages = PERF_ALIGN(val, page_size) / page_size;
931 /* we got pages count value */
933 pages = strtoul(str, &eptr, 10);
938 if (pages == 0 && min == 0) {
939 /* leave number of pages at 0 */
940 } else if (!is_power_of_2(pages)) {
943 /* round pages up to next power of 2 */
944 pages = roundup_pow_of_two(pages);
948 unit_number__scnprintf(buf, sizeof(buf), pages * page_size);
949 pr_info("rounding mmap pages size to %s (%lu pages)\n",
959 int __evlist__parse_mmap_pages(unsigned int *mmap_pages, const char *str)
961 unsigned long max = UINT_MAX;
964 if (max > SIZE_MAX / page_size)
965 max = SIZE_MAX / page_size;
967 pages = parse_pages_arg(str, 1, max);
969 pr_err("Invalid argument for --mmap_pages/-m\n");
977 int evlist__parse_mmap_pages(const struct option *opt, const char *str, int unset __maybe_unused)
979 return __evlist__parse_mmap_pages(opt->value, str);
983 * evlist__mmap_ex - Create mmaps to receive events.
984 * @evlist: list of events
985 * @pages: map length in pages
986 * @overwrite: overwrite older events?
987 * @auxtrace_pages - auxtrace map length in pages
988 * @auxtrace_overwrite - overwrite older auxtrace data?
990 * If @overwrite is %false the user needs to signal event consumption using
991 * perf_mmap__write_tail(). Using evlist__mmap_read() does this
994 * Similarly, if @auxtrace_overwrite is %false the user needs to signal data
995 * consumption using auxtrace_mmap__write_tail().
997 * Return: %0 on success, negative error code otherwise.
999 int evlist__mmap_ex(struct evlist *evlist, unsigned int pages,
1000 unsigned int auxtrace_pages,
1001 bool auxtrace_overwrite, int nr_cblocks, int affinity, int flush,
1005 * Delay setting mp.prot: set it before calling perf_mmap__mmap.
1006 * Its value is decided by evsel's write_backward.
1007 * So &mp should not be passed through const pointer.
1009 struct mmap_params mp = {
1010 .nr_cblocks = nr_cblocks,
1011 .affinity = affinity,
1013 .comp_level = comp_level
1015 struct perf_evlist_mmap_ops ops = {
1016 .idx = perf_evlist__mmap_cb_idx,
1017 .get = perf_evlist__mmap_cb_get,
1018 .mmap = perf_evlist__mmap_cb_mmap,
1021 evlist->core.mmap_len = evlist__mmap_size(pages);
1022 pr_debug("mmap size %zuB\n", evlist->core.mmap_len);
1024 auxtrace_mmap_params__init(&mp.auxtrace_mp, evlist->core.mmap_len,
1025 auxtrace_pages, auxtrace_overwrite);
1027 return perf_evlist__mmap_ops(&evlist->core, &ops, &mp.core);
1030 int evlist__mmap(struct evlist *evlist, unsigned int pages)
1032 return evlist__mmap_ex(evlist, pages, 0, false, 0, PERF_AFFINITY_SYS, 1, 0);
1035 int evlist__create_maps(struct evlist *evlist, struct target *target)
1037 bool all_threads = (target->per_thread && target->system_wide);
1038 struct perf_cpu_map *cpus;
1039 struct perf_thread_map *threads;
1042 * If specify '-a' and '--per-thread' to perf record, perf record
1043 * will override '--per-thread'. target->per_thread = false and
1044 * target->system_wide = true.
1046 * If specify '--per-thread' only to perf record,
1047 * target->per_thread = true and target->system_wide = false.
1049 * So target->per_thread && target->system_wide is false.
1050 * For perf record, thread_map__new_str doesn't call
1051 * thread_map__new_all_cpus. That will keep perf record's
1054 * For perf stat, it allows the case that target->per_thread and
1055 * target->system_wide are all true. It means to collect system-wide
1056 * per-thread data. thread_map__new_str will call
1057 * thread_map__new_all_cpus to enumerate all threads.
1059 threads = thread_map__new_str(target->pid, target->tid, target->uid,
1065 if (target__uses_dummy_map(target))
1066 cpus = perf_cpu_map__new_any_cpu();
1068 cpus = perf_cpu_map__new(target->cpu_list);
1071 goto out_delete_threads;
1073 evlist->core.has_user_cpus = !!target->cpu_list;
1075 perf_evlist__set_maps(&evlist->core, cpus, threads);
1077 /* as evlist now has references, put count here */
1078 perf_cpu_map__put(cpus);
1079 perf_thread_map__put(threads);
1084 perf_thread_map__put(threads);
1088 int evlist__apply_filters(struct evlist *evlist, struct evsel **err_evsel)
1090 struct evsel *evsel;
1093 evlist__for_each_entry(evlist, evsel) {
1095 * filters only work for tracepoint event, which doesn't have cpu limit.
1096 * So evlist and evsel should always be same.
1098 if (evsel->filter) {
1099 err = perf_evsel__apply_filter(&evsel->core, evsel->filter);
1107 * non-tracepoint events can have BPF filters.
1109 if (!list_empty(&evsel->bpf_filters)) {
1110 err = perf_bpf_filter__prepare(evsel);
1121 int evlist__set_tp_filter(struct evlist *evlist, const char *filter)
1123 struct evsel *evsel;
1129 evlist__for_each_entry(evlist, evsel) {
1130 if (evsel->core.attr.type != PERF_TYPE_TRACEPOINT)
1133 err = evsel__set_filter(evsel, filter);
1141 int evlist__append_tp_filter(struct evlist *evlist, const char *filter)
1143 struct evsel *evsel;
1149 evlist__for_each_entry(evlist, evsel) {
1150 if (evsel->core.attr.type != PERF_TYPE_TRACEPOINT)
1153 err = evsel__append_tp_filter(evsel, filter);
1161 char *asprintf__tp_filter_pids(size_t npids, pid_t *pids)
1166 for (i = 0; i < npids; ++i) {
1168 if (asprintf(&filter, "common_pid != %d", pids[i]) < 0)
1173 if (asprintf(&tmp, "%s && common_pid != %d", filter, pids[i]) < 0)
1187 int evlist__set_tp_filter_pids(struct evlist *evlist, size_t npids, pid_t *pids)
1189 char *filter = asprintf__tp_filter_pids(npids, pids);
1190 int ret = evlist__set_tp_filter(evlist, filter);
1196 int evlist__set_tp_filter_pid(struct evlist *evlist, pid_t pid)
1198 return evlist__set_tp_filter_pids(evlist, 1, &pid);
1201 int evlist__append_tp_filter_pids(struct evlist *evlist, size_t npids, pid_t *pids)
1203 char *filter = asprintf__tp_filter_pids(npids, pids);
1204 int ret = evlist__append_tp_filter(evlist, filter);
1210 int evlist__append_tp_filter_pid(struct evlist *evlist, pid_t pid)
1212 return evlist__append_tp_filter_pids(evlist, 1, &pid);
1215 bool evlist__valid_sample_type(struct evlist *evlist)
1219 if (evlist->core.nr_entries == 1)
1222 if (evlist->id_pos < 0 || evlist->is_pos < 0)
1225 evlist__for_each_entry(evlist, pos) {
1226 if (pos->id_pos != evlist->id_pos ||
1227 pos->is_pos != evlist->is_pos)
1234 u64 __evlist__combined_sample_type(struct evlist *evlist)
1236 struct evsel *evsel;
1238 if (evlist->combined_sample_type)
1239 return evlist->combined_sample_type;
1241 evlist__for_each_entry(evlist, evsel)
1242 evlist->combined_sample_type |= evsel->core.attr.sample_type;
1244 return evlist->combined_sample_type;
1247 u64 evlist__combined_sample_type(struct evlist *evlist)
1249 evlist->combined_sample_type = 0;
1250 return __evlist__combined_sample_type(evlist);
1253 u64 evlist__combined_branch_type(struct evlist *evlist)
1255 struct evsel *evsel;
1256 u64 branch_type = 0;
1258 evlist__for_each_entry(evlist, evsel)
1259 branch_type |= evsel->core.attr.branch_sample_type;
1263 bool evlist__valid_read_format(struct evlist *evlist)
1265 struct evsel *first = evlist__first(evlist), *pos = first;
1266 u64 read_format = first->core.attr.read_format;
1267 u64 sample_type = first->core.attr.sample_type;
1269 evlist__for_each_entry(evlist, pos) {
1270 if (read_format != pos->core.attr.read_format) {
1271 pr_debug("Read format differs %#" PRIx64 " vs %#" PRIx64 "\n",
1272 read_format, (u64)pos->core.attr.read_format);
1276 /* PERF_SAMPLE_READ implies PERF_FORMAT_ID. */
1277 if ((sample_type & PERF_SAMPLE_READ) &&
1278 !(read_format & PERF_FORMAT_ID)) {
1285 u16 evlist__id_hdr_size(struct evlist *evlist)
1287 struct evsel *first = evlist__first(evlist);
1289 return first->core.attr.sample_id_all ? evsel__id_hdr_size(first) : 0;
1292 bool evlist__valid_sample_id_all(struct evlist *evlist)
1294 struct evsel *first = evlist__first(evlist), *pos = first;
1296 evlist__for_each_entry_continue(evlist, pos) {
1297 if (first->core.attr.sample_id_all != pos->core.attr.sample_id_all)
1304 bool evlist__sample_id_all(struct evlist *evlist)
1306 struct evsel *first = evlist__first(evlist);
1307 return first->core.attr.sample_id_all;
1310 void evlist__set_selected(struct evlist *evlist, struct evsel *evsel)
1312 evlist->selected = evsel;
1315 void evlist__close(struct evlist *evlist)
1317 struct evsel *evsel;
1318 struct evlist_cpu_iterator evlist_cpu_itr;
1319 struct affinity affinity;
1322 * With perf record core.user_requested_cpus is usually NULL.
1323 * Use the old method to handle this for now.
1325 if (!evlist->core.user_requested_cpus ||
1326 cpu_map__is_dummy(evlist->core.user_requested_cpus)) {
1327 evlist__for_each_entry_reverse(evlist, evsel)
1328 evsel__close(evsel);
1332 if (affinity__setup(&affinity) < 0)
1335 evlist__for_each_cpu(evlist_cpu_itr, evlist, &affinity) {
1336 perf_evsel__close_cpu(&evlist_cpu_itr.evsel->core,
1337 evlist_cpu_itr.cpu_map_idx);
1340 affinity__cleanup(&affinity);
1341 evlist__for_each_entry_reverse(evlist, evsel) {
1342 perf_evsel__free_fd(&evsel->core);
1343 perf_evsel__free_id(&evsel->core);
1345 perf_evlist__reset_id_hash(&evlist->core);
1348 static int evlist__create_syswide_maps(struct evlist *evlist)
1350 struct perf_cpu_map *cpus;
1351 struct perf_thread_map *threads;
1354 * Try reading /sys/devices/system/cpu/online to get
1357 * FIXME: -ENOMEM is the best we can do here, the cpu_map
1358 * code needs an overhaul to properly forward the
1359 * error, and we may not want to do that fallback to a
1360 * default cpu identity map :-\
1362 cpus = perf_cpu_map__new_online_cpus();
1366 threads = perf_thread_map__new_dummy();
1370 perf_evlist__set_maps(&evlist->core, cpus, threads);
1372 perf_thread_map__put(threads);
1374 perf_cpu_map__put(cpus);
1379 int evlist__open(struct evlist *evlist)
1381 struct evsel *evsel;
1385 * Default: one fd per CPU, all threads, aka systemwide
1386 * as sys_perf_event_open(cpu = -1, thread = -1) is EINVAL
1388 if (evlist->core.threads == NULL && evlist->core.user_requested_cpus == NULL) {
1389 err = evlist__create_syswide_maps(evlist);
1394 evlist__update_id_pos(evlist);
1396 evlist__for_each_entry(evlist, evsel) {
1397 err = evsel__open(evsel, evsel->core.cpus, evsel->core.threads);
1404 evlist__close(evlist);
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))
1412 int child_ready_pipe[2], go_pipe[2];
1415 if (pipe(child_ready_pipe) < 0) {
1416 perror("failed to create 'ready' pipe");
1420 if (pipe(go_pipe) < 0) {
1421 perror("failed to create 'go' pipe");
1422 goto out_close_ready_pipe;
1425 evlist->workload.pid = fork();
1426 if (evlist->workload.pid < 0) {
1427 perror("failed to fork");
1428 goto out_close_pipes;
1431 if (!evlist->workload.pid) {
1437 signal(SIGTERM, SIG_DFL);
1439 close(child_ready_pipe[0]);
1441 fcntl(go_pipe[0], F_SETFD, FD_CLOEXEC);
1444 * Change the name of this process not to confuse --exclude-perf users
1445 * that sees 'perf' in the window up to the execvp() and thinks that
1446 * perf samples are not being excluded.
1448 prctl(PR_SET_NAME, "perf-exec");
1451 * Tell the parent we're ready to go
1453 close(child_ready_pipe[1]);
1456 * Wait until the parent tells us to go.
1458 ret = read(go_pipe[0], &bf, 1);
1460 * The parent will ask for the execvp() to be performed by
1461 * writing exactly one byte, in workload.cork_fd, usually via
1462 * evlist__start_workload().
1464 * For cancelling the workload without actually running it,
1465 * the parent will just close workload.cork_fd, without writing
1466 * anything, i.e. read will return zero and we just exit()
1471 perror("unable to read pipe");
1475 execvp(argv[0], (char **)argv);
1480 val.sival_int = errno;
1481 if (sigqueue(getppid(), SIGUSR1, val))
1489 struct sigaction act = {
1490 .sa_flags = SA_SIGINFO,
1491 .sa_sigaction = exec_error,
1493 sigaction(SIGUSR1, &act, NULL);
1496 if (target__none(target)) {
1497 if (evlist->core.threads == NULL) {
1498 fprintf(stderr, "FATAL: evlist->threads need to be set at this point (%s:%d).\n",
1499 __func__, __LINE__);
1500 goto out_close_pipes;
1502 perf_thread_map__set_pid(evlist->core.threads, 0, evlist->workload.pid);
1505 close(child_ready_pipe[1]);
1508 * wait for child to settle
1510 if (read(child_ready_pipe[0], &bf, 1) == -1) {
1511 perror("unable to read pipe");
1512 goto out_close_pipes;
1515 fcntl(go_pipe[1], F_SETFD, FD_CLOEXEC);
1516 evlist->workload.cork_fd = go_pipe[1];
1517 close(child_ready_pipe[0]);
1523 out_close_ready_pipe:
1524 close(child_ready_pipe[0]);
1525 close(child_ready_pipe[1]);
1529 int evlist__start_workload(struct evlist *evlist)
1531 if (evlist->workload.cork_fd > 0) {
1535 * Remove the cork, let it rip!
1537 ret = write(evlist->workload.cork_fd, &bf, 1);
1539 perror("unable to write to pipe");
1541 close(evlist->workload.cork_fd);
1548 int evlist__parse_sample(struct evlist *evlist, union perf_event *event, struct perf_sample *sample)
1550 struct evsel *evsel = evlist__event2evsel(evlist, event);
1555 ret = evsel__parse_sample(evsel, event, sample);
1558 if (perf_guest && sample->id) {
1559 struct perf_sample_id *sid = evlist__id2sid(evlist, sample->id);
1562 sample->machine_pid = sid->machine_pid;
1563 sample->vcpu = sid->vcpu.cpu;
1569 int evlist__parse_sample_timestamp(struct evlist *evlist, union perf_event *event, u64 *timestamp)
1571 struct evsel *evsel = evlist__event2evsel(evlist, event);
1575 return evsel__parse_sample_timestamp(evsel, event, timestamp);
1578 int evlist__strerror_open(struct evlist *evlist, int err, char *buf, size_t size)
1581 char sbuf[STRERR_BUFSIZE], *emsg = str_error_r(err, sbuf, sizeof(sbuf));
1586 printed = scnprintf(buf, size,
1588 "Hint:\tCheck /proc/sys/kernel/perf_event_paranoid setting.", emsg);
1590 value = perf_event_paranoid();
1592 printed += scnprintf(buf + printed, size - printed, "\nHint:\t");
1595 printed += scnprintf(buf + printed, size - printed,
1596 "For your workloads it needs to be <= 1\nHint:\t");
1598 printed += scnprintf(buf + printed, size - printed,
1599 "For system wide tracing it needs to be set to -1.\n");
1601 printed += scnprintf(buf + printed, size - printed,
1602 "Hint:\tTry: 'sudo sh -c \"echo -1 > /proc/sys/kernel/perf_event_paranoid\"'\n"
1603 "Hint:\tThe current value is %d.", value);
1606 struct evsel *first = evlist__first(evlist);
1609 if (sysctl__read_int("kernel/perf_event_max_sample_rate", &max_freq) < 0)
1612 if (first->core.attr.sample_freq < (u64)max_freq)
1615 printed = scnprintf(buf, size,
1617 "Hint:\tCheck /proc/sys/kernel/perf_event_max_sample_rate.\n"
1618 "Hint:\tThe current value is %d and %" PRIu64 " is being requested.",
1619 emsg, max_freq, first->core.attr.sample_freq);
1624 scnprintf(buf, size, "%s", emsg);
1631 int evlist__strerror_mmap(struct evlist *evlist, int err, char *buf, size_t size)
1633 char sbuf[STRERR_BUFSIZE], *emsg = str_error_r(err, sbuf, sizeof(sbuf));
1634 int pages_attempted = evlist->core.mmap_len / 1024, pages_max_per_user, printed = 0;
1638 sysctl__read_int("kernel/perf_event_mlock_kb", &pages_max_per_user);
1639 printed += scnprintf(buf + printed, size - printed,
1641 "Hint:\tCheck /proc/sys/kernel/perf_event_mlock_kb (%d kB) setting.\n"
1642 "Hint:\tTried using %zd kB.\n",
1643 emsg, pages_max_per_user, pages_attempted);
1645 if (pages_attempted >= pages_max_per_user) {
1646 printed += scnprintf(buf + printed, size - printed,
1647 "Hint:\tTry 'sudo sh -c \"echo %d > /proc/sys/kernel/perf_event_mlock_kb\"', or\n",
1648 pages_max_per_user + pages_attempted);
1651 printed += scnprintf(buf + printed, size - printed,
1652 "Hint:\tTry using a smaller -m/--mmap-pages value.");
1655 scnprintf(buf, size, "%s", emsg);
1662 void evlist__to_front(struct evlist *evlist, struct evsel *move_evsel)
1664 struct evsel *evsel, *n;
1667 if (move_evsel == evlist__first(evlist))
1670 evlist__for_each_entry_safe(evlist, n, evsel) {
1671 if (evsel__leader(evsel) == evsel__leader(move_evsel))
1672 list_move_tail(&evsel->core.node, &move);
1675 list_splice(&move, &evlist->core.entries);
1678 struct evsel *evlist__get_tracking_event(struct evlist *evlist)
1680 struct evsel *evsel;
1682 evlist__for_each_entry(evlist, evsel) {
1683 if (evsel->tracking)
1687 return evlist__first(evlist);
1690 void evlist__set_tracking_event(struct evlist *evlist, struct evsel *tracking_evsel)
1692 struct evsel *evsel;
1694 if (tracking_evsel->tracking)
1697 evlist__for_each_entry(evlist, evsel) {
1698 if (evsel != tracking_evsel)
1699 evsel->tracking = false;
1702 tracking_evsel->tracking = true;
1705 struct evsel *evlist__findnew_tracking_event(struct evlist *evlist, bool system_wide)
1707 struct evsel *evsel;
1709 evsel = evlist__get_tracking_event(evlist);
1710 if (!evsel__is_dummy_event(evsel)) {
1711 evsel = evlist__add_aux_dummy(evlist, system_wide);
1715 evlist__set_tracking_event(evlist, evsel);
1716 } else if (system_wide) {
1717 perf_evlist__go_system_wide(&evlist->core, &evsel->core);
1723 struct evsel *evlist__find_evsel_by_str(struct evlist *evlist, const char *str)
1725 struct evsel *evsel;
1727 evlist__for_each_entry(evlist, evsel) {
1730 if (evsel__name_is(evsel, str))
1737 void evlist__toggle_bkw_mmap(struct evlist *evlist, enum bkw_mmap_state state)
1739 enum bkw_mmap_state old_state = evlist->bkw_mmap_state;
1746 if (!evlist->overwrite_mmap)
1749 switch (old_state) {
1750 case BKW_MMAP_NOTREADY: {
1751 if (state != BKW_MMAP_RUNNING)
1755 case BKW_MMAP_RUNNING: {
1756 if (state != BKW_MMAP_DATA_PENDING)
1761 case BKW_MMAP_DATA_PENDING: {
1762 if (state != BKW_MMAP_EMPTY)
1766 case BKW_MMAP_EMPTY: {
1767 if (state != BKW_MMAP_RUNNING)
1773 WARN_ONCE(1, "Shouldn't get there\n");
1776 evlist->bkw_mmap_state = state;
1780 evlist__pause(evlist);
1783 evlist__resume(evlist);
1794 bool evlist__exclude_kernel(struct evlist *evlist)
1796 struct evsel *evsel;
1798 evlist__for_each_entry(evlist, evsel) {
1799 if (!evsel->core.attr.exclude_kernel)
1807 * Events in data file are not collect in groups, but we still want
1808 * the group display. Set the artificial group and set the leader's
1809 * forced_leader flag to notify the display code.
1811 void evlist__force_leader(struct evlist *evlist)
1813 if (evlist__nr_groups(evlist) == 0) {
1814 struct evsel *leader = evlist__first(evlist);
1816 evlist__set_leader(evlist);
1817 leader->forced_leader = true;
1821 struct evsel *evlist__reset_weak_group(struct evlist *evsel_list, struct evsel *evsel, bool close)
1823 struct evsel *c2, *leader;
1824 bool is_open = true;
1826 leader = evsel__leader(evsel);
1828 pr_debug("Weak group for %s/%d failed\n",
1829 leader->name, leader->core.nr_members);
1832 * for_each_group_member doesn't work here because it doesn't
1833 * include the first entry.
1835 evlist__for_each_entry(evsel_list, c2) {
1838 if (evsel__has_leader(c2, leader)) {
1839 if (is_open && close)
1840 perf_evsel__close(&c2->core);
1842 * We want to close all members of the group and reopen
1843 * them. Some events, like Intel topdown, require being
1844 * in a group and so keep these in the group.
1846 evsel__remove_from_group(c2, leader);
1849 * Set this for all former members of the group
1850 * to indicate they get reopened.
1852 c2->reset_group = true;
1855 /* Reset the leader count if all entries were removed. */
1856 if (leader->core.nr_members == 1)
1857 leader->core.nr_members = 0;
1861 static int evlist__parse_control_fifo(const char *str, int *ctl_fd, int *ctl_fd_ack, bool *ctl_fd_close)
1866 if (strncmp(str, "fifo:", 5))
1870 if (!*str || *str == ',')
1882 * O_RDWR avoids POLLHUPs which is necessary to allow the other
1883 * end of a FIFO to be repeatedly opened and closed.
1885 fd = open(s, O_RDWR | O_NONBLOCK | O_CLOEXEC);
1887 pr_err("Failed to open '%s'\n", s);
1892 *ctl_fd_close = true;
1895 /* O_RDWR | O_NONBLOCK means the other end need not be open */
1896 fd = open(p, O_RDWR | O_NONBLOCK | O_CLOEXEC);
1898 pr_err("Failed to open '%s'\n", p);
1910 int evlist__parse_control(const char *str, int *ctl_fd, int *ctl_fd_ack, bool *ctl_fd_close)
1912 char *comma = NULL, *endptr = NULL;
1914 *ctl_fd_close = false;
1916 if (strncmp(str, "fd:", 3))
1917 return evlist__parse_control_fifo(str, ctl_fd, ctl_fd_ack, ctl_fd_close);
1919 *ctl_fd = strtoul(&str[3], &endptr, 0);
1920 if (endptr == &str[3])
1923 comma = strchr(str, ',');
1925 if (endptr != comma)
1928 *ctl_fd_ack = strtoul(comma + 1, &endptr, 0);
1929 if (endptr == comma + 1 || *endptr != '\0')
1936 void evlist__close_control(int ctl_fd, int ctl_fd_ack, bool *ctl_fd_close)
1938 if (*ctl_fd_close) {
1939 *ctl_fd_close = false;
1941 if (ctl_fd_ack >= 0)
1946 int evlist__initialize_ctlfd(struct evlist *evlist, int fd, int ack)
1949 pr_debug("Control descriptor is not initialized\n");
1953 evlist->ctl_fd.pos = perf_evlist__add_pollfd(&evlist->core, fd, NULL, POLLIN,
1954 fdarray_flag__nonfilterable |
1955 fdarray_flag__non_perf_event);
1956 if (evlist->ctl_fd.pos < 0) {
1957 evlist->ctl_fd.pos = -1;
1958 pr_err("Failed to add ctl fd entry: %m\n");
1962 evlist->ctl_fd.fd = fd;
1963 evlist->ctl_fd.ack = ack;
1968 bool evlist__ctlfd_initialized(struct evlist *evlist)
1970 return evlist->ctl_fd.pos >= 0;
1973 int evlist__finalize_ctlfd(struct evlist *evlist)
1975 struct pollfd *entries = evlist->core.pollfd.entries;
1977 if (!evlist__ctlfd_initialized(evlist))
1980 entries[evlist->ctl_fd.pos].fd = -1;
1981 entries[evlist->ctl_fd.pos].events = 0;
1982 entries[evlist->ctl_fd.pos].revents = 0;
1984 evlist->ctl_fd.pos = -1;
1985 evlist->ctl_fd.ack = -1;
1986 evlist->ctl_fd.fd = -1;
1991 static int evlist__ctlfd_recv(struct evlist *evlist, enum evlist_ctl_cmd *cmd,
1992 char *cmd_data, size_t data_size)
1996 size_t bytes_read = 0;
1998 *cmd = EVLIST_CTL_CMD_UNSUPPORTED;
1999 memset(cmd_data, 0, data_size);
2003 err = read(evlist->ctl_fd.fd, &c, 1);
2005 if (c == '\n' || c == '\0')
2007 cmd_data[bytes_read++] = c;
2008 if (bytes_read == data_size)
2011 } else if (err == -1) {
2014 if (errno == EAGAIN || errno == EWOULDBLOCK)
2017 pr_err("Failed to read from ctlfd %d: %m\n", evlist->ctl_fd.fd);
2022 pr_debug("Message from ctl_fd: \"%s%s\"\n", cmd_data,
2023 bytes_read == data_size ? "" : c == '\n' ? "\\n" : "\\0");
2025 if (bytes_read > 0) {
2026 if (!strncmp(cmd_data, EVLIST_CTL_CMD_ENABLE_TAG,
2027 (sizeof(EVLIST_CTL_CMD_ENABLE_TAG)-1))) {
2028 *cmd = EVLIST_CTL_CMD_ENABLE;
2029 } else if (!strncmp(cmd_data, EVLIST_CTL_CMD_DISABLE_TAG,
2030 (sizeof(EVLIST_CTL_CMD_DISABLE_TAG)-1))) {
2031 *cmd = EVLIST_CTL_CMD_DISABLE;
2032 } else if (!strncmp(cmd_data, EVLIST_CTL_CMD_SNAPSHOT_TAG,
2033 (sizeof(EVLIST_CTL_CMD_SNAPSHOT_TAG)-1))) {
2034 *cmd = EVLIST_CTL_CMD_SNAPSHOT;
2035 pr_debug("is snapshot\n");
2036 } else if (!strncmp(cmd_data, EVLIST_CTL_CMD_EVLIST_TAG,
2037 (sizeof(EVLIST_CTL_CMD_EVLIST_TAG)-1))) {
2038 *cmd = EVLIST_CTL_CMD_EVLIST;
2039 } else if (!strncmp(cmd_data, EVLIST_CTL_CMD_STOP_TAG,
2040 (sizeof(EVLIST_CTL_CMD_STOP_TAG)-1))) {
2041 *cmd = EVLIST_CTL_CMD_STOP;
2042 } else if (!strncmp(cmd_data, EVLIST_CTL_CMD_PING_TAG,
2043 (sizeof(EVLIST_CTL_CMD_PING_TAG)-1))) {
2044 *cmd = EVLIST_CTL_CMD_PING;
2048 return bytes_read ? (int)bytes_read : err;
2051 int evlist__ctlfd_ack(struct evlist *evlist)
2055 if (evlist->ctl_fd.ack == -1)
2058 err = write(evlist->ctl_fd.ack, EVLIST_CTL_CMD_ACK_TAG,
2059 sizeof(EVLIST_CTL_CMD_ACK_TAG));
2061 pr_err("failed to write to ctl_ack_fd %d: %m\n", evlist->ctl_fd.ack);
2066 static int get_cmd_arg(char *cmd_data, size_t cmd_size, char **arg)
2068 char *data = cmd_data + cmd_size;
2074 /* there's argument */
2084 static int evlist__ctlfd_enable(struct evlist *evlist, char *cmd_data, bool enable)
2086 struct evsel *evsel;
2090 err = get_cmd_arg(cmd_data,
2091 enable ? sizeof(EVLIST_CTL_CMD_ENABLE_TAG) - 1 :
2092 sizeof(EVLIST_CTL_CMD_DISABLE_TAG) - 1,
2095 pr_info("failed: wrong command\n");
2100 evsel = evlist__find_evsel_by_str(evlist, name);
2103 evlist__enable_evsel(evlist, name);
2105 evlist__disable_evsel(evlist, name);
2106 pr_info("Event %s %s\n", evsel->name,
2107 enable ? "enabled" : "disabled");
2109 pr_info("failed: can't find '%s' event\n", name);
2113 evlist__enable(evlist);
2114 pr_info(EVLIST_ENABLED_MSG);
2116 evlist__disable(evlist);
2117 pr_info(EVLIST_DISABLED_MSG);
2124 static int evlist__ctlfd_list(struct evlist *evlist, char *cmd_data)
2126 struct perf_attr_details details = { .verbose = false, };
2127 struct evsel *evsel;
2131 err = get_cmd_arg(cmd_data,
2132 sizeof(EVLIST_CTL_CMD_EVLIST_TAG) - 1,
2135 pr_info("failed: wrong command\n");
2140 if (!strcmp(arg, "-v")) {
2141 details.verbose = true;
2142 } else if (!strcmp(arg, "-g")) {
2143 details.event_group = true;
2144 } else if (!strcmp(arg, "-F")) {
2145 details.freq = true;
2147 pr_info("failed: wrong command\n");
2152 evlist__for_each_entry(evlist, evsel)
2153 evsel__fprintf(evsel, &details, stderr);
2158 int evlist__ctlfd_process(struct evlist *evlist, enum evlist_ctl_cmd *cmd)
2161 char cmd_data[EVLIST_CTL_CMD_MAX_LEN];
2162 int ctlfd_pos = evlist->ctl_fd.pos;
2163 struct pollfd *entries = evlist->core.pollfd.entries;
2165 if (!evlist__ctlfd_initialized(evlist) || !entries[ctlfd_pos].revents)
2168 if (entries[ctlfd_pos].revents & POLLIN) {
2169 err = evlist__ctlfd_recv(evlist, cmd, cmd_data,
2170 EVLIST_CTL_CMD_MAX_LEN);
2173 case EVLIST_CTL_CMD_ENABLE:
2174 case EVLIST_CTL_CMD_DISABLE:
2175 err = evlist__ctlfd_enable(evlist, cmd_data,
2176 *cmd == EVLIST_CTL_CMD_ENABLE);
2178 case EVLIST_CTL_CMD_EVLIST:
2179 err = evlist__ctlfd_list(evlist, cmd_data);
2181 case EVLIST_CTL_CMD_SNAPSHOT:
2182 case EVLIST_CTL_CMD_STOP:
2183 case EVLIST_CTL_CMD_PING:
2185 case EVLIST_CTL_CMD_ACK:
2186 case EVLIST_CTL_CMD_UNSUPPORTED:
2188 pr_debug("ctlfd: unsupported %d\n", *cmd);
2191 if (!(*cmd == EVLIST_CTL_CMD_ACK || *cmd == EVLIST_CTL_CMD_UNSUPPORTED ||
2192 *cmd == EVLIST_CTL_CMD_SNAPSHOT))
2193 evlist__ctlfd_ack(evlist);
2197 if (entries[ctlfd_pos].revents & (POLLHUP | POLLERR))
2198 evlist__finalize_ctlfd(evlist);
2200 entries[ctlfd_pos].revents = 0;
2206 * struct event_enable_time - perf record -D/--delay single time range.
2207 * @start: start of time range to enable events in milliseconds
2208 * @end: end of time range to enable events in milliseconds
2210 * N.B. this structure is also accessed as an array of int.
2212 struct event_enable_time {
2217 static int parse_event_enable_time(const char *str, struct event_enable_time *range, bool first)
2219 const char *fmt = first ? "%u - %u %n" : " , %u - %u %n";
2220 int ret, start, end, n;
2222 ret = sscanf(str, fmt, &start, &end, &n);
2223 if (ret != 2 || end <= start)
2226 range->start = start;
2232 static ssize_t parse_event_enable_times(const char *str, struct event_enable_time *range)
2238 for (cnt = 0; *str; cnt++) {
2239 ret = parse_event_enable_time(str, range, first);
2242 /* Check no overlap */
2243 if (!first && range && range->start <= range[-1].end)
2253 * struct event_enable_timer - control structure for perf record -D/--delay.
2254 * @evlist: event list
2255 * @times: time ranges that events are enabled (N.B. this is also accessed as an
2257 * @times_cnt: number of time ranges
2258 * @timerfd: timer file descriptor
2259 * @pollfd_pos: position in @evlist array of file descriptors to poll (fdarray)
2260 * @times_step: current position in (int *)@times)[],
2261 * refer event_enable_timer__process()
2263 * Note, this structure is only used when there are time ranges, not when there
2264 * is only an initial delay.
2266 struct event_enable_timer {
2267 struct evlist *evlist;
2268 struct event_enable_time *times;
2275 static int str_to_delay(const char *str)
2280 d = strtol(str, &endptr, 10);
2281 if (*endptr || d > INT_MAX || d < -1)
2286 int evlist__parse_event_enable_time(struct evlist *evlist, struct record_opts *opts,
2287 const char *str, int unset)
2289 enum fdarray_flags flags = fdarray_flag__nonfilterable | fdarray_flag__non_perf_event;
2290 struct event_enable_timer *eet;
2298 opts->target.initial_delay = str_to_delay(str);
2299 if (opts->target.initial_delay)
2302 ret = parse_event_enable_times(str, NULL);
2310 eet = zalloc(sizeof(*eet));
2314 eet->times = calloc(times_cnt, sizeof(*eet->times));
2320 if (parse_event_enable_times(str, eet->times) != times_cnt) {
2322 goto free_eet_times;
2325 eet->times_cnt = times_cnt;
2327 eet->timerfd = timerfd_create(CLOCK_MONOTONIC, TFD_CLOEXEC);
2328 if (eet->timerfd == -1) {
2330 pr_err("timerfd_create failed: %s\n", strerror(errno));
2331 goto free_eet_times;
2334 eet->pollfd_pos = perf_evlist__add_pollfd(&evlist->core, eet->timerfd, NULL, POLLIN, flags);
2335 if (eet->pollfd_pos < 0) {
2336 err = eet->pollfd_pos;
2340 eet->evlist = evlist;
2342 opts->target.initial_delay = eet->times[0].start;
2347 close(eet->timerfd);
2355 static int event_enable_timer__set_timer(struct event_enable_timer *eet, int ms)
2357 struct itimerspec its = {
2358 .it_value.tv_sec = ms / MSEC_PER_SEC,
2359 .it_value.tv_nsec = (ms % MSEC_PER_SEC) * NSEC_PER_MSEC,
2363 if (timerfd_settime(eet->timerfd, 0, &its, NULL) < 0) {
2365 pr_err("timerfd_settime failed: %s\n", strerror(errno));
2370 int event_enable_timer__start(struct event_enable_timer *eet)
2377 ms = eet->times[0].end - eet->times[0].start;
2378 eet->times_step = 1;
2380 return event_enable_timer__set_timer(eet, ms);
2383 int event_enable_timer__process(struct event_enable_timer *eet)
2385 struct pollfd *entries;
2391 entries = eet->evlist->core.pollfd.entries;
2392 revents = entries[eet->pollfd_pos].revents;
2393 entries[eet->pollfd_pos].revents = 0;
2395 if (revents & POLLIN) {
2396 size_t step = eet->times_step;
2397 size_t pos = step / 2;
2400 evlist__disable_non_dummy(eet->evlist);
2401 pr_info(EVLIST_DISABLED_MSG);
2402 if (pos >= eet->times_cnt - 1) {
2404 event_enable_timer__set_timer(eet, 0);
2405 return 1; /* Stop */
2408 evlist__enable_non_dummy(eet->evlist);
2409 pr_info(EVLIST_ENABLED_MSG);
2415 if (pos < eet->times_cnt) {
2416 int *times = (int *)eet->times; /* Accessing 'times' as array of int */
2417 int ms = times[step] - times[step - 1];
2419 eet->times_step = step;
2420 return event_enable_timer__set_timer(eet, ms);
2427 void event_enable_timer__exit(struct event_enable_timer **ep)
2431 zfree(&(*ep)->times);
2435 struct evsel *evlist__find_evsel(struct evlist *evlist, int idx)
2437 struct evsel *evsel;
2439 evlist__for_each_entry(evlist, evsel) {
2440 if (evsel->core.idx == idx)
2446 int evlist__scnprintf_evsels(struct evlist *evlist, size_t size, char *bf)
2448 struct evsel *evsel;
2451 evlist__for_each_entry(evlist, evsel) {
2452 if (evsel__is_dummy_event(evsel))
2454 if (size > (strlen(evsel__name(evsel)) + (printed ? 2 : 1))) {
2455 printed += scnprintf(bf + printed, size - printed, "%s%s", printed ? "," : "", evsel__name(evsel));
2457 printed += scnprintf(bf + printed, size - printed, "%s...", printed ? "," : "");
2465 void evlist__check_mem_load_aux(struct evlist *evlist)
2467 struct evsel *leader, *evsel, *pos;
2470 * For some platforms, the 'mem-loads' event is required to use
2471 * together with 'mem-loads-aux' within a group and 'mem-loads-aux'
2472 * must be the group leader. Now we disable this group before reporting
2473 * because 'mem-loads-aux' is just an auxiliary event. It doesn't carry
2474 * any valid memory load information.
2476 evlist__for_each_entry(evlist, evsel) {
2477 leader = evsel__leader(evsel);
2478 if (leader == evsel)
2481 if (leader->name && strstr(leader->name, "mem-loads-aux")) {
2482 for_each_group_evsel(pos, leader) {
2483 evsel__set_leader(pos, pos);
2484 pos->core.nr_members = 0;
2491 * evlist__warn_user_requested_cpus() - Check each evsel against requested CPUs
2492 * and warn if the user CPU list is inapplicable for the event's PMU's
2493 * CPUs. Not core PMUs list a CPU in sysfs, but this may be overwritten by a
2494 * user requested CPU and so any online CPU is applicable. Core PMUs handle
2495 * events on the CPUs in their list and otherwise the event isn't supported.
2496 * @evlist: The list of events being checked.
2497 * @cpu_list: The user provided list of CPUs.
2499 void evlist__warn_user_requested_cpus(struct evlist *evlist, const char *cpu_list)
2501 struct perf_cpu_map *user_requested_cpus;
2507 user_requested_cpus = perf_cpu_map__new(cpu_list);
2508 if (!user_requested_cpus)
2511 evlist__for_each_entry(evlist, pos) {
2512 struct perf_cpu_map *intersect, *to_test;
2513 const struct perf_pmu *pmu = evsel__find_pmu(pos);
2515 to_test = pmu && pmu->is_core ? pmu->cpus : cpu_map__online();
2516 intersect = perf_cpu_map__intersect(to_test, user_requested_cpus);
2517 if (!perf_cpu_map__equal(intersect, user_requested_cpus)) {
2520 cpu_map__snprint(to_test, buf, sizeof(buf));
2521 pr_warning("WARNING: A requested CPU in '%s' is not supported by PMU '%s' (CPUs %s) for event '%s'\n",
2522 cpu_list, pmu ? pmu->name : "cpu", buf, evsel__name(pos));
2524 perf_cpu_map__put(intersect);
2526 perf_cpu_map__put(user_requested_cpus);
2529 void evlist__uniquify_name(struct evlist *evlist)
2531 char *new_name, empty_attributes[2] = ":", *attributes;
2534 if (perf_pmus__num_core_pmus() == 1)
2537 evlist__for_each_entry(evlist, pos) {
2538 if (!evsel__is_hybrid(pos))
2541 if (strchr(pos->name, '/'))
2544 attributes = strchr(pos->name, ':');
2548 attributes = empty_attributes;
2550 if (asprintf(&new_name, "%s/%s/%s", pos->pmu_name, pos->name, attributes + 1)) {
2552 pos->name = new_name;