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"
20 #include "bpf_counter.h"
21 #include <internal/lib.h> // page_size
25 #include "bpf-event.h"
26 #include "util/string2.h"
27 #include "util/perf_api_probe.h"
28 #include "util/evsel_fprintf.h"
29 #include "util/evlist-hybrid.h"
35 #include "parse-events.h"
36 #include <subcmd/parse-options.h>
39 #include <sys/ioctl.h>
41 #include <sys/prctl.h>
43 #include <linux/bitops.h>
44 #include <linux/hash.h>
45 #include <linux/log2.h>
46 #include <linux/err.h>
47 #include <linux/string.h>
48 #include <linux/zalloc.h>
49 #include <perf/evlist.h>
50 #include <perf/evsel.h>
51 #include <perf/cpumap.h>
52 #include <perf/mmap.h>
54 #include <internal/xyarray.h>
56 #ifdef LACKS_SIGQUEUE_PROTOTYPE
57 int sigqueue(pid_t pid, int sig, const union sigval value);
60 #define FD(e, x, y) (*(int *)xyarray__entry(e->core.fd, x, y))
61 #define SID(e, x, y) xyarray__entry(e->core.sample_id, x, y)
63 void evlist__init(struct evlist *evlist, struct perf_cpu_map *cpus,
64 struct perf_thread_map *threads)
66 perf_evlist__init(&evlist->core);
67 perf_evlist__set_maps(&evlist->core, cpus, threads);
68 evlist->workload.pid = -1;
69 evlist->bkw_mmap_state = BKW_MMAP_NOTREADY;
70 evlist->ctl_fd.fd = -1;
71 evlist->ctl_fd.ack = -1;
72 evlist->ctl_fd.pos = -1;
75 struct evlist *evlist__new(void)
77 struct evlist *evlist = zalloc(sizeof(*evlist));
80 evlist__init(evlist, NULL, NULL);
85 struct evlist *evlist__new_default(void)
87 struct evlist *evlist = evlist__new();
89 if (evlist && evlist__add_default(evlist)) {
90 evlist__delete(evlist);
97 struct evlist *evlist__new_dummy(void)
99 struct evlist *evlist = evlist__new();
101 if (evlist && evlist__add_dummy(evlist)) {
102 evlist__delete(evlist);
110 * evlist__set_id_pos - set the positions of event ids.
111 * @evlist: selected event list
113 * Events with compatible sample types all have the same id_pos
114 * and is_pos. For convenience, put a copy on evlist.
116 void evlist__set_id_pos(struct evlist *evlist)
118 struct evsel *first = evlist__first(evlist);
120 evlist->id_pos = first->id_pos;
121 evlist->is_pos = first->is_pos;
124 static void evlist__update_id_pos(struct evlist *evlist)
128 evlist__for_each_entry(evlist, evsel)
129 evsel__calc_id_pos(evsel);
131 evlist__set_id_pos(evlist);
134 static void evlist__purge(struct evlist *evlist)
136 struct evsel *pos, *n;
138 evlist__for_each_entry_safe(evlist, n, pos) {
139 list_del_init(&pos->core.node);
144 evlist->core.nr_entries = 0;
147 void evlist__exit(struct evlist *evlist)
149 zfree(&evlist->mmap);
150 zfree(&evlist->overwrite_mmap);
151 perf_evlist__exit(&evlist->core);
154 void evlist__delete(struct evlist *evlist)
159 evlist__munmap(evlist);
160 evlist__close(evlist);
161 evlist__purge(evlist);
162 evlist__exit(evlist);
166 void evlist__add(struct evlist *evlist, struct evsel *entry)
168 entry->evlist = evlist;
169 entry->idx = evlist->core.nr_entries;
170 entry->tracking = !entry->idx;
172 perf_evlist__add(&evlist->core, &entry->core);
174 if (evlist->core.nr_entries == 1)
175 evlist__set_id_pos(evlist);
178 void evlist__remove(struct evlist *evlist, struct evsel *evsel)
180 evsel->evlist = NULL;
181 perf_evlist__remove(&evlist->core, &evsel->core);
184 void evlist__splice_list_tail(struct evlist *evlist, struct list_head *list)
186 while (!list_empty(list)) {
187 struct evsel *evsel, *temp, *leader = NULL;
189 __evlist__for_each_entry_safe(list, temp, evsel) {
190 list_del_init(&evsel->core.node);
191 evlist__add(evlist, evsel);
196 __evlist__for_each_entry_safe(list, temp, evsel) {
197 if (evsel->leader == leader) {
198 list_del_init(&evsel->core.node);
199 evlist__add(evlist, evsel);
205 int __evlist__set_tracepoints_handlers(struct evlist *evlist,
206 const struct evsel_str_handler *assocs, size_t nr_assocs)
211 for (i = 0; i < nr_assocs; i++) {
212 // Adding a handler for an event not in this evlist, just ignore it.
213 struct evsel *evsel = evlist__find_tracepoint_by_name(evlist, assocs[i].name);
218 if (evsel->handler != NULL)
220 evsel->handler = assocs[i].handler;
228 void __evlist__set_leader(struct list_head *list)
230 struct evsel *evsel, *leader;
232 leader = list_entry(list->next, struct evsel, core.node);
233 evsel = list_entry(list->prev, struct evsel, core.node);
235 leader->core.nr_members = evsel->idx - leader->idx + 1;
237 __evlist__for_each_entry(list, evsel) {
238 evsel->leader = leader;
242 void evlist__set_leader(struct evlist *evlist)
244 if (evlist->core.nr_entries) {
245 evlist->nr_groups = evlist->core.nr_entries > 1 ? 1 : 0;
246 __evlist__set_leader(&evlist->core.entries);
250 int __evlist__add_default(struct evlist *evlist, bool precise)
254 evsel = evsel__new_cycles(precise, PERF_TYPE_HARDWARE,
255 PERF_COUNT_HW_CPU_CYCLES);
259 evlist__add(evlist, evsel);
263 int evlist__add_dummy(struct evlist *evlist)
265 struct perf_event_attr attr = {
266 .type = PERF_TYPE_SOFTWARE,
267 .config = PERF_COUNT_SW_DUMMY,
268 .size = sizeof(attr), /* to capture ABI version */
270 struct evsel *evsel = evsel__new_idx(&attr, evlist->core.nr_entries);
275 evlist__add(evlist, evsel);
279 static int evlist__add_attrs(struct evlist *evlist, struct perf_event_attr *attrs, size_t nr_attrs)
281 struct evsel *evsel, *n;
285 for (i = 0; i < nr_attrs; i++) {
286 evsel = evsel__new_idx(attrs + i, evlist->core.nr_entries + i);
288 goto out_delete_partial_list;
289 list_add_tail(&evsel->core.node, &head);
292 evlist__splice_list_tail(evlist, &head);
296 out_delete_partial_list:
297 __evlist__for_each_entry_safe(&head, n, evsel)
298 evsel__delete(evsel);
302 int __evlist__add_default_attrs(struct evlist *evlist, struct perf_event_attr *attrs, size_t nr_attrs)
306 for (i = 0; i < nr_attrs; i++)
307 event_attr_init(attrs + i);
309 return evlist__add_attrs(evlist, attrs, nr_attrs);
312 __weak int arch_evlist__add_default_attrs(struct evlist *evlist __maybe_unused)
317 struct evsel *evlist__find_tracepoint_by_id(struct evlist *evlist, int id)
321 evlist__for_each_entry(evlist, evsel) {
322 if (evsel->core.attr.type == PERF_TYPE_TRACEPOINT &&
323 (int)evsel->core.attr.config == id)
330 struct evsel *evlist__find_tracepoint_by_name(struct evlist *evlist, const char *name)
334 evlist__for_each_entry(evlist, evsel) {
335 if ((evsel->core.attr.type == PERF_TYPE_TRACEPOINT) &&
336 (strcmp(evsel->name, name) == 0))
343 int evlist__add_newtp(struct evlist *evlist, const char *sys, const char *name, void *handler)
345 struct evsel *evsel = evsel__newtp(sys, name);
350 evsel->handler = handler;
351 evlist__add(evlist, evsel);
355 static int evlist__nr_threads(struct evlist *evlist, struct evsel *evsel)
357 if (evsel->core.system_wide)
360 return perf_thread_map__nr(evlist->core.threads);
363 void evlist__cpu_iter_start(struct evlist *evlist)
368 * Reset the per evsel cpu_iter. This is needed because
369 * each evsel's cpumap may have a different index space,
370 * and some operations need the index to modify
371 * the FD xyarray (e.g. open, close)
373 evlist__for_each_entry(evlist, pos)
377 bool evsel__cpu_iter_skip_no_inc(struct evsel *ev, int cpu)
379 if (ev->cpu_iter >= ev->core.cpus->nr)
381 if (cpu >= 0 && ev->core.cpus->map[ev->cpu_iter] != cpu)
386 bool evsel__cpu_iter_skip(struct evsel *ev, int cpu)
388 if (!evsel__cpu_iter_skip_no_inc(ev, cpu)) {
395 static int evsel__strcmp(struct evsel *pos, char *evsel_name)
399 if (evsel__is_dummy_event(pos))
401 return strcmp(pos->name, evsel_name);
404 static int evlist__is_enabled(struct evlist *evlist)
408 evlist__for_each_entry(evlist, pos) {
409 if (!evsel__is_group_leader(pos) || !pos->core.fd)
411 /* If at least one event is enabled, evlist is enabled. */
418 static void __evlist__disable(struct evlist *evlist, char *evsel_name)
421 struct affinity affinity;
423 bool has_imm = false;
425 if (affinity__setup(&affinity) < 0)
428 evlist__for_each_entry(evlist, pos)
429 bpf_counter__disable(pos);
431 /* Disable 'immediate' events last */
432 for (imm = 0; imm <= 1; imm++) {
433 evlist__for_each_cpu(evlist, i, cpu) {
434 affinity__set(&affinity, cpu);
436 evlist__for_each_entry(evlist, pos) {
437 if (evsel__strcmp(pos, evsel_name))
439 if (evsel__cpu_iter_skip(pos, cpu))
441 if (pos->disabled || !evsel__is_group_leader(pos) || !pos->core.fd)
445 if (pos->immediate != imm)
447 evsel__disable_cpu(pos, pos->cpu_iter - 1);
454 affinity__cleanup(&affinity);
455 evlist__for_each_entry(evlist, pos) {
456 if (evsel__strcmp(pos, evsel_name))
458 if (!evsel__is_group_leader(pos) || !pos->core.fd)
460 pos->disabled = true;
464 * If we disabled only single event, we need to check
465 * the enabled state of the evlist manually.
468 evlist->enabled = evlist__is_enabled(evlist);
470 evlist->enabled = false;
473 void evlist__disable(struct evlist *evlist)
475 __evlist__disable(evlist, NULL);
478 void evlist__disable_evsel(struct evlist *evlist, char *evsel_name)
480 __evlist__disable(evlist, evsel_name);
483 static void __evlist__enable(struct evlist *evlist, char *evsel_name)
486 struct affinity affinity;
489 if (affinity__setup(&affinity) < 0)
492 evlist__for_each_cpu(evlist, i, cpu) {
493 affinity__set(&affinity, cpu);
495 evlist__for_each_entry(evlist, pos) {
496 if (evsel__strcmp(pos, evsel_name))
498 if (evsel__cpu_iter_skip(pos, cpu))
500 if (!evsel__is_group_leader(pos) || !pos->core.fd)
502 evsel__enable_cpu(pos, pos->cpu_iter - 1);
505 affinity__cleanup(&affinity);
506 evlist__for_each_entry(evlist, pos) {
507 if (evsel__strcmp(pos, evsel_name))
509 if (!evsel__is_group_leader(pos) || !pos->core.fd)
511 pos->disabled = false;
515 * Even single event sets the 'enabled' for evlist,
516 * so the toggle can work properly and toggle to
519 evlist->enabled = true;
522 void evlist__enable(struct evlist *evlist)
524 __evlist__enable(evlist, NULL);
527 void evlist__enable_evsel(struct evlist *evlist, char *evsel_name)
529 __evlist__enable(evlist, evsel_name);
532 void evlist__toggle_enable(struct evlist *evlist)
534 (evlist->enabled ? evlist__disable : evlist__enable)(evlist);
537 static int evlist__enable_event_cpu(struct evlist *evlist, struct evsel *evsel, int cpu)
540 int nr_threads = evlist__nr_threads(evlist, evsel);
545 for (thread = 0; thread < nr_threads; thread++) {
546 int err = ioctl(FD(evsel, cpu, thread), PERF_EVENT_IOC_ENABLE, 0);
553 static int evlist__enable_event_thread(struct evlist *evlist, struct evsel *evsel, int thread)
556 int nr_cpus = perf_cpu_map__nr(evlist->core.cpus);
561 for (cpu = 0; cpu < nr_cpus; cpu++) {
562 int err = ioctl(FD(evsel, cpu, thread), PERF_EVENT_IOC_ENABLE, 0);
569 int evlist__enable_event_idx(struct evlist *evlist, struct evsel *evsel, int idx)
571 bool per_cpu_mmaps = !perf_cpu_map__empty(evlist->core.cpus);
574 return evlist__enable_event_cpu(evlist, evsel, idx);
576 return evlist__enable_event_thread(evlist, evsel, idx);
579 int evlist__add_pollfd(struct evlist *evlist, int fd)
581 return perf_evlist__add_pollfd(&evlist->core, fd, NULL, POLLIN, fdarray_flag__default);
584 int evlist__filter_pollfd(struct evlist *evlist, short revents_and_mask)
586 return perf_evlist__filter_pollfd(&evlist->core, revents_and_mask);
589 #ifdef HAVE_EVENTFD_SUPPORT
590 int evlist__add_wakeup_eventfd(struct evlist *evlist, int fd)
592 return perf_evlist__add_pollfd(&evlist->core, fd, NULL, POLLIN,
593 fdarray_flag__nonfilterable);
597 int evlist__poll(struct evlist *evlist, int timeout)
599 return perf_evlist__poll(&evlist->core, timeout);
602 struct perf_sample_id *evlist__id2sid(struct evlist *evlist, u64 id)
604 struct hlist_head *head;
605 struct perf_sample_id *sid;
608 hash = hash_64(id, PERF_EVLIST__HLIST_BITS);
609 head = &evlist->core.heads[hash];
611 hlist_for_each_entry(sid, head, node)
618 struct evsel *evlist__id2evsel(struct evlist *evlist, u64 id)
620 struct perf_sample_id *sid;
622 if (evlist->core.nr_entries == 1 || !id)
623 return evlist__first(evlist);
625 sid = evlist__id2sid(evlist, id);
627 return container_of(sid->evsel, struct evsel, core);
629 if (!evlist__sample_id_all(evlist))
630 return evlist__first(evlist);
635 struct evsel *evlist__id2evsel_strict(struct evlist *evlist, u64 id)
637 struct perf_sample_id *sid;
642 sid = evlist__id2sid(evlist, id);
644 return container_of(sid->evsel, struct evsel, core);
649 static int evlist__event2id(struct evlist *evlist, union perf_event *event, u64 *id)
651 const __u64 *array = event->sample.array;
654 n = (event->header.size - sizeof(event->header)) >> 3;
656 if (event->header.type == PERF_RECORD_SAMPLE) {
657 if (evlist->id_pos >= n)
659 *id = array[evlist->id_pos];
661 if (evlist->is_pos > n)
669 struct evsel *evlist__event2evsel(struct evlist *evlist, union perf_event *event)
671 struct evsel *first = evlist__first(evlist);
672 struct hlist_head *head;
673 struct perf_sample_id *sid;
677 if (evlist->core.nr_entries == 1)
680 if (!first->core.attr.sample_id_all &&
681 event->header.type != PERF_RECORD_SAMPLE)
684 if (evlist__event2id(evlist, event, &id))
687 /* Synthesized events have an id of zero */
691 hash = hash_64(id, PERF_EVLIST__HLIST_BITS);
692 head = &evlist->core.heads[hash];
694 hlist_for_each_entry(sid, head, node) {
696 return container_of(sid->evsel, struct evsel, core);
701 static int evlist__set_paused(struct evlist *evlist, bool value)
705 if (!evlist->overwrite_mmap)
708 for (i = 0; i < evlist->core.nr_mmaps; i++) {
709 int fd = evlist->overwrite_mmap[i].core.fd;
714 err = ioctl(fd, PERF_EVENT_IOC_PAUSE_OUTPUT, value ? 1 : 0);
721 static int evlist__pause(struct evlist *evlist)
723 return evlist__set_paused(evlist, true);
726 static int evlist__resume(struct evlist *evlist)
728 return evlist__set_paused(evlist, false);
731 static void evlist__munmap_nofree(struct evlist *evlist)
736 for (i = 0; i < evlist->core.nr_mmaps; i++)
737 perf_mmap__munmap(&evlist->mmap[i].core);
739 if (evlist->overwrite_mmap)
740 for (i = 0; i < evlist->core.nr_mmaps; i++)
741 perf_mmap__munmap(&evlist->overwrite_mmap[i].core);
744 void evlist__munmap(struct evlist *evlist)
746 evlist__munmap_nofree(evlist);
747 zfree(&evlist->mmap);
748 zfree(&evlist->overwrite_mmap);
751 static void perf_mmap__unmap_cb(struct perf_mmap *map)
753 struct mmap *m = container_of(map, struct mmap, core);
758 static struct mmap *evlist__alloc_mmap(struct evlist *evlist,
764 map = zalloc(evlist->core.nr_mmaps * sizeof(struct mmap));
768 for (i = 0; i < evlist->core.nr_mmaps; i++) {
769 struct perf_mmap *prev = i ? &map[i - 1].core : NULL;
772 * When the perf_mmap() call is made we grab one refcount, plus
773 * one extra to let perf_mmap__consume() get the last
774 * events after all real references (perf_mmap__get()) are
777 * Each PERF_EVENT_IOC_SET_OUTPUT points to this mmap and
778 * thus does perf_mmap__get() on it.
780 perf_mmap__init(&map[i].core, prev, overwrite, perf_mmap__unmap_cb);
787 perf_evlist__mmap_cb_idx(struct perf_evlist *_evlist,
788 struct perf_mmap_param *_mp,
789 int idx, bool per_cpu)
791 struct evlist *evlist = container_of(_evlist, struct evlist, core);
792 struct mmap_params *mp = container_of(_mp, struct mmap_params, core);
794 auxtrace_mmap_params__set_idx(&mp->auxtrace_mp, evlist, idx, per_cpu);
797 static struct perf_mmap*
798 perf_evlist__mmap_cb_get(struct perf_evlist *_evlist, bool overwrite, int idx)
800 struct evlist *evlist = container_of(_evlist, struct evlist, core);
803 maps = overwrite ? evlist->overwrite_mmap : evlist->mmap;
806 maps = evlist__alloc_mmap(evlist, overwrite);
811 evlist->overwrite_mmap = maps;
812 if (evlist->bkw_mmap_state == BKW_MMAP_NOTREADY)
813 evlist__toggle_bkw_mmap(evlist, BKW_MMAP_RUNNING);
819 return &maps[idx].core;
823 perf_evlist__mmap_cb_mmap(struct perf_mmap *_map, struct perf_mmap_param *_mp,
826 struct mmap *map = container_of(_map, struct mmap, core);
827 struct mmap_params *mp = container_of(_mp, struct mmap_params, core);
829 return mmap__mmap(map, mp, output, cpu);
832 unsigned long perf_event_mlock_kb_in_pages(void)
837 if (sysctl__read_int("kernel/perf_event_mlock_kb", &max) < 0) {
839 * Pick a once upon a time good value, i.e. things look
840 * strange since we can't read a sysctl value, but lets not
845 max -= (page_size / 1024);
848 pages = (max * 1024) / page_size;
849 if (!is_power_of_2(pages))
850 pages = rounddown_pow_of_two(pages);
855 size_t evlist__mmap_size(unsigned long pages)
857 if (pages == UINT_MAX)
858 pages = perf_event_mlock_kb_in_pages();
859 else if (!is_power_of_2(pages))
862 return (pages + 1) * page_size;
865 static long parse_pages_arg(const char *str, unsigned long min,
868 unsigned long pages, val;
869 static struct parse_tag tags[] = {
870 { .tag = 'B', .mult = 1 },
871 { .tag = 'K', .mult = 1 << 10 },
872 { .tag = 'M', .mult = 1 << 20 },
873 { .tag = 'G', .mult = 1 << 30 },
880 val = parse_tag_value(str, tags);
881 if (val != (unsigned long) -1) {
882 /* we got file size value */
883 pages = PERF_ALIGN(val, page_size) / page_size;
885 /* we got pages count value */
887 pages = strtoul(str, &eptr, 10);
892 if (pages == 0 && min == 0) {
893 /* leave number of pages at 0 */
894 } else if (!is_power_of_2(pages)) {
897 /* round pages up to next power of 2 */
898 pages = roundup_pow_of_two(pages);
902 unit_number__scnprintf(buf, sizeof(buf), pages * page_size);
903 pr_info("rounding mmap pages size to %s (%lu pages)\n",
913 int __evlist__parse_mmap_pages(unsigned int *mmap_pages, const char *str)
915 unsigned long max = UINT_MAX;
918 if (max > SIZE_MAX / page_size)
919 max = SIZE_MAX / page_size;
921 pages = parse_pages_arg(str, 1, max);
923 pr_err("Invalid argument for --mmap_pages/-m\n");
931 int evlist__parse_mmap_pages(const struct option *opt, const char *str, int unset __maybe_unused)
933 return __evlist__parse_mmap_pages(opt->value, str);
937 * evlist__mmap_ex - Create mmaps to receive events.
938 * @evlist: list of events
939 * @pages: map length in pages
940 * @overwrite: overwrite older events?
941 * @auxtrace_pages - auxtrace map length in pages
942 * @auxtrace_overwrite - overwrite older auxtrace data?
944 * If @overwrite is %false the user needs to signal event consumption using
945 * perf_mmap__write_tail(). Using evlist__mmap_read() does this
948 * Similarly, if @auxtrace_overwrite is %false the user needs to signal data
949 * consumption using auxtrace_mmap__write_tail().
951 * Return: %0 on success, negative error code otherwise.
953 int evlist__mmap_ex(struct evlist *evlist, unsigned int pages,
954 unsigned int auxtrace_pages,
955 bool auxtrace_overwrite, int nr_cblocks, int affinity, int flush,
959 * Delay setting mp.prot: set it before calling perf_mmap__mmap.
960 * Its value is decided by evsel's write_backward.
961 * So &mp should not be passed through const pointer.
963 struct mmap_params mp = {
964 .nr_cblocks = nr_cblocks,
965 .affinity = affinity,
967 .comp_level = comp_level
969 struct perf_evlist_mmap_ops ops = {
970 .idx = perf_evlist__mmap_cb_idx,
971 .get = perf_evlist__mmap_cb_get,
972 .mmap = perf_evlist__mmap_cb_mmap,
975 evlist->core.mmap_len = evlist__mmap_size(pages);
976 pr_debug("mmap size %zuB\n", evlist->core.mmap_len);
978 auxtrace_mmap_params__init(&mp.auxtrace_mp, evlist->core.mmap_len,
979 auxtrace_pages, auxtrace_overwrite);
981 return perf_evlist__mmap_ops(&evlist->core, &ops, &mp.core);
984 int evlist__mmap(struct evlist *evlist, unsigned int pages)
986 return evlist__mmap_ex(evlist, pages, 0, false, 0, PERF_AFFINITY_SYS, 1, 0);
989 int evlist__create_maps(struct evlist *evlist, struct target *target)
991 bool all_threads = (target->per_thread && target->system_wide);
992 struct perf_cpu_map *cpus;
993 struct perf_thread_map *threads;
996 * If specify '-a' and '--per-thread' to perf record, perf record
997 * will override '--per-thread'. target->per_thread = false and
998 * target->system_wide = true.
1000 * If specify '--per-thread' only to perf record,
1001 * target->per_thread = true and target->system_wide = false.
1003 * So target->per_thread && target->system_wide is false.
1004 * For perf record, thread_map__new_str doesn't call
1005 * thread_map__new_all_cpus. That will keep perf record's
1008 * For perf stat, it allows the case that target->per_thread and
1009 * target->system_wide are all true. It means to collect system-wide
1010 * per-thread data. thread_map__new_str will call
1011 * thread_map__new_all_cpus to enumerate all threads.
1013 threads = thread_map__new_str(target->pid, target->tid, target->uid,
1019 if (target__uses_dummy_map(target))
1020 cpus = perf_cpu_map__dummy_new();
1022 cpus = perf_cpu_map__new(target->cpu_list);
1025 goto out_delete_threads;
1027 evlist->core.has_user_cpus = !!target->cpu_list;
1029 perf_evlist__set_maps(&evlist->core, cpus, threads);
1031 /* as evlist now has references, put count here */
1032 perf_cpu_map__put(cpus);
1033 perf_thread_map__put(threads);
1038 perf_thread_map__put(threads);
1042 int evlist__apply_filters(struct evlist *evlist, struct evsel **err_evsel)
1044 struct evsel *evsel;
1047 evlist__for_each_entry(evlist, evsel) {
1048 if (evsel->filter == NULL)
1052 * filters only work for tracepoint event, which doesn't have cpu limit.
1053 * So evlist and evsel should always be same.
1055 err = perf_evsel__apply_filter(&evsel->core, evsel->filter);
1065 int evlist__set_tp_filter(struct evlist *evlist, const char *filter)
1067 struct evsel *evsel;
1073 evlist__for_each_entry(evlist, evsel) {
1074 if (evsel->core.attr.type != PERF_TYPE_TRACEPOINT)
1077 err = evsel__set_filter(evsel, filter);
1085 int evlist__append_tp_filter(struct evlist *evlist, const char *filter)
1087 struct evsel *evsel;
1093 evlist__for_each_entry(evlist, evsel) {
1094 if (evsel->core.attr.type != PERF_TYPE_TRACEPOINT)
1097 err = evsel__append_tp_filter(evsel, filter);
1105 char *asprintf__tp_filter_pids(size_t npids, pid_t *pids)
1110 for (i = 0; i < npids; ++i) {
1112 if (asprintf(&filter, "common_pid != %d", pids[i]) < 0)
1117 if (asprintf(&tmp, "%s && common_pid != %d", filter, pids[i]) < 0)
1131 int evlist__set_tp_filter_pids(struct evlist *evlist, size_t npids, pid_t *pids)
1133 char *filter = asprintf__tp_filter_pids(npids, pids);
1134 int ret = evlist__set_tp_filter(evlist, filter);
1140 int evlist__set_tp_filter_pid(struct evlist *evlist, pid_t pid)
1142 return evlist__set_tp_filter_pids(evlist, 1, &pid);
1145 int evlist__append_tp_filter_pids(struct evlist *evlist, size_t npids, pid_t *pids)
1147 char *filter = asprintf__tp_filter_pids(npids, pids);
1148 int ret = evlist__append_tp_filter(evlist, filter);
1154 int evlist__append_tp_filter_pid(struct evlist *evlist, pid_t pid)
1156 return evlist__append_tp_filter_pids(evlist, 1, &pid);
1159 bool evlist__valid_sample_type(struct evlist *evlist)
1163 if (evlist->core.nr_entries == 1)
1166 if (evlist->id_pos < 0 || evlist->is_pos < 0)
1169 evlist__for_each_entry(evlist, pos) {
1170 if (pos->id_pos != evlist->id_pos ||
1171 pos->is_pos != evlist->is_pos)
1178 u64 __evlist__combined_sample_type(struct evlist *evlist)
1180 struct evsel *evsel;
1182 if (evlist->combined_sample_type)
1183 return evlist->combined_sample_type;
1185 evlist__for_each_entry(evlist, evsel)
1186 evlist->combined_sample_type |= evsel->core.attr.sample_type;
1188 return evlist->combined_sample_type;
1191 u64 evlist__combined_sample_type(struct evlist *evlist)
1193 evlist->combined_sample_type = 0;
1194 return __evlist__combined_sample_type(evlist);
1197 u64 evlist__combined_branch_type(struct evlist *evlist)
1199 struct evsel *evsel;
1200 u64 branch_type = 0;
1202 evlist__for_each_entry(evlist, evsel)
1203 branch_type |= evsel->core.attr.branch_sample_type;
1207 bool evlist__valid_read_format(struct evlist *evlist)
1209 struct evsel *first = evlist__first(evlist), *pos = first;
1210 u64 read_format = first->core.attr.read_format;
1211 u64 sample_type = first->core.attr.sample_type;
1213 evlist__for_each_entry(evlist, pos) {
1214 if (read_format != pos->core.attr.read_format) {
1215 pr_debug("Read format differs %#" PRIx64 " vs %#" PRIx64 "\n",
1216 read_format, (u64)pos->core.attr.read_format);
1220 /* PERF_SAMPLE_READ implies PERF_FORMAT_ID. */
1221 if ((sample_type & PERF_SAMPLE_READ) &&
1222 !(read_format & PERF_FORMAT_ID)) {
1229 u16 evlist__id_hdr_size(struct evlist *evlist)
1231 struct evsel *first = evlist__first(evlist);
1232 struct perf_sample *data;
1236 if (!first->core.attr.sample_id_all)
1239 sample_type = first->core.attr.sample_type;
1241 if (sample_type & PERF_SAMPLE_TID)
1242 size += sizeof(data->tid) * 2;
1244 if (sample_type & PERF_SAMPLE_TIME)
1245 size += sizeof(data->time);
1247 if (sample_type & PERF_SAMPLE_ID)
1248 size += sizeof(data->id);
1250 if (sample_type & PERF_SAMPLE_STREAM_ID)
1251 size += sizeof(data->stream_id);
1253 if (sample_type & PERF_SAMPLE_CPU)
1254 size += sizeof(data->cpu) * 2;
1256 if (sample_type & PERF_SAMPLE_IDENTIFIER)
1257 size += sizeof(data->id);
1262 bool evlist__valid_sample_id_all(struct evlist *evlist)
1264 struct evsel *first = evlist__first(evlist), *pos = first;
1266 evlist__for_each_entry_continue(evlist, pos) {
1267 if (first->core.attr.sample_id_all != pos->core.attr.sample_id_all)
1274 bool evlist__sample_id_all(struct evlist *evlist)
1276 struct evsel *first = evlist__first(evlist);
1277 return first->core.attr.sample_id_all;
1280 void evlist__set_selected(struct evlist *evlist, struct evsel *evsel)
1282 evlist->selected = evsel;
1285 void evlist__close(struct evlist *evlist)
1287 struct evsel *evsel;
1288 struct affinity affinity;
1292 * With perf record core.cpus is usually NULL.
1293 * Use the old method to handle this for now.
1295 if (!evlist->core.cpus) {
1296 evlist__for_each_entry_reverse(evlist, evsel)
1297 evsel__close(evsel);
1301 if (affinity__setup(&affinity) < 0)
1303 evlist__for_each_cpu(evlist, i, cpu) {
1304 affinity__set(&affinity, cpu);
1306 evlist__for_each_entry_reverse(evlist, evsel) {
1307 if (evsel__cpu_iter_skip(evsel, cpu))
1309 perf_evsel__close_cpu(&evsel->core, evsel->cpu_iter - 1);
1312 affinity__cleanup(&affinity);
1313 evlist__for_each_entry_reverse(evlist, evsel) {
1314 perf_evsel__free_fd(&evsel->core);
1315 perf_evsel__free_id(&evsel->core);
1317 perf_evlist__reset_id_hash(&evlist->core);
1320 static int evlist__create_syswide_maps(struct evlist *evlist)
1322 struct perf_cpu_map *cpus;
1323 struct perf_thread_map *threads;
1327 * Try reading /sys/devices/system/cpu/online to get
1330 * FIXME: -ENOMEM is the best we can do here, the cpu_map
1331 * code needs an overhaul to properly forward the
1332 * error, and we may not want to do that fallback to a
1333 * default cpu identity map :-\
1335 cpus = perf_cpu_map__new(NULL);
1339 threads = perf_thread_map__new_dummy();
1343 perf_evlist__set_maps(&evlist->core, cpus, threads);
1345 perf_thread_map__put(threads);
1347 perf_cpu_map__put(cpus);
1352 int evlist__open(struct evlist *evlist)
1354 struct evsel *evsel;
1358 * Default: one fd per CPU, all threads, aka systemwide
1359 * as sys_perf_event_open(cpu = -1, thread = -1) is EINVAL
1361 if (evlist->core.threads == NULL && evlist->core.cpus == NULL) {
1362 err = evlist__create_syswide_maps(evlist);
1367 evlist__update_id_pos(evlist);
1369 evlist__for_each_entry(evlist, evsel) {
1370 err = evsel__open(evsel, evsel->core.cpus, evsel->core.threads);
1377 evlist__close(evlist);
1382 int evlist__prepare_workload(struct evlist *evlist, struct target *target, const char *argv[],
1383 bool pipe_output, void (*exec_error)(int signo, siginfo_t *info, void *ucontext))
1385 int child_ready_pipe[2], go_pipe[2];
1388 if (pipe(child_ready_pipe) < 0) {
1389 perror("failed to create 'ready' pipe");
1393 if (pipe(go_pipe) < 0) {
1394 perror("failed to create 'go' pipe");
1395 goto out_close_ready_pipe;
1398 evlist->workload.pid = fork();
1399 if (evlist->workload.pid < 0) {
1400 perror("failed to fork");
1401 goto out_close_pipes;
1404 if (!evlist->workload.pid) {
1410 signal(SIGTERM, SIG_DFL);
1412 close(child_ready_pipe[0]);
1414 fcntl(go_pipe[0], F_SETFD, FD_CLOEXEC);
1417 * Change the name of this process not to confuse --exclude-perf users
1418 * that sees 'perf' in the window up to the execvp() and thinks that
1419 * perf samples are not being excluded.
1421 prctl(PR_SET_NAME, "perf-exec");
1424 * Tell the parent we're ready to go
1426 close(child_ready_pipe[1]);
1429 * Wait until the parent tells us to go.
1431 ret = read(go_pipe[0], &bf, 1);
1433 * The parent will ask for the execvp() to be performed by
1434 * writing exactly one byte, in workload.cork_fd, usually via
1435 * evlist__start_workload().
1437 * For cancelling the workload without actually running it,
1438 * the parent will just close workload.cork_fd, without writing
1439 * anything, i.e. read will return zero and we just exit()
1444 perror("unable to read pipe");
1448 execvp(argv[0], (char **)argv);
1453 val.sival_int = errno;
1454 if (sigqueue(getppid(), SIGUSR1, val))
1462 struct sigaction act = {
1463 .sa_flags = SA_SIGINFO,
1464 .sa_sigaction = exec_error,
1466 sigaction(SIGUSR1, &act, NULL);
1469 if (target__none(target)) {
1470 if (evlist->core.threads == NULL) {
1471 fprintf(stderr, "FATAL: evlist->threads need to be set at this point (%s:%d).\n",
1472 __func__, __LINE__);
1473 goto out_close_pipes;
1475 perf_thread_map__set_pid(evlist->core.threads, 0, evlist->workload.pid);
1478 close(child_ready_pipe[1]);
1481 * wait for child to settle
1483 if (read(child_ready_pipe[0], &bf, 1) == -1) {
1484 perror("unable to read pipe");
1485 goto out_close_pipes;
1488 fcntl(go_pipe[1], F_SETFD, FD_CLOEXEC);
1489 evlist->workload.cork_fd = go_pipe[1];
1490 close(child_ready_pipe[0]);
1496 out_close_ready_pipe:
1497 close(child_ready_pipe[0]);
1498 close(child_ready_pipe[1]);
1502 int evlist__start_workload(struct evlist *evlist)
1504 if (evlist->workload.cork_fd > 0) {
1508 * Remove the cork, let it rip!
1510 ret = write(evlist->workload.cork_fd, &bf, 1);
1512 perror("unable to write to pipe");
1514 close(evlist->workload.cork_fd);
1521 int evlist__parse_sample(struct evlist *evlist, union perf_event *event, struct perf_sample *sample)
1523 struct evsel *evsel = evlist__event2evsel(evlist, event);
1527 return evsel__parse_sample(evsel, event, sample);
1530 int evlist__parse_sample_timestamp(struct evlist *evlist, union perf_event *event, u64 *timestamp)
1532 struct evsel *evsel = evlist__event2evsel(evlist, event);
1536 return evsel__parse_sample_timestamp(evsel, event, timestamp);
1539 int evlist__strerror_open(struct evlist *evlist, int err, char *buf, size_t size)
1542 char sbuf[STRERR_BUFSIZE], *emsg = str_error_r(err, sbuf, sizeof(sbuf));
1547 printed = scnprintf(buf, size,
1549 "Hint:\tCheck /proc/sys/kernel/perf_event_paranoid setting.", emsg);
1551 value = perf_event_paranoid();
1553 printed += scnprintf(buf + printed, size - printed, "\nHint:\t");
1556 printed += scnprintf(buf + printed, size - printed,
1557 "For your workloads it needs to be <= 1\nHint:\t");
1559 printed += scnprintf(buf + printed, size - printed,
1560 "For system wide tracing it needs to be set to -1.\n");
1562 printed += scnprintf(buf + printed, size - printed,
1563 "Hint:\tTry: 'sudo sh -c \"echo -1 > /proc/sys/kernel/perf_event_paranoid\"'\n"
1564 "Hint:\tThe current value is %d.", value);
1567 struct evsel *first = evlist__first(evlist);
1570 if (sysctl__read_int("kernel/perf_event_max_sample_rate", &max_freq) < 0)
1573 if (first->core.attr.sample_freq < (u64)max_freq)
1576 printed = scnprintf(buf, size,
1578 "Hint:\tCheck /proc/sys/kernel/perf_event_max_sample_rate.\n"
1579 "Hint:\tThe current value is %d and %" PRIu64 " is being requested.",
1580 emsg, max_freq, first->core.attr.sample_freq);
1585 scnprintf(buf, size, "%s", emsg);
1592 int evlist__strerror_mmap(struct evlist *evlist, int err, char *buf, size_t size)
1594 char sbuf[STRERR_BUFSIZE], *emsg = str_error_r(err, sbuf, sizeof(sbuf));
1595 int pages_attempted = evlist->core.mmap_len / 1024, pages_max_per_user, printed = 0;
1599 sysctl__read_int("kernel/perf_event_mlock_kb", &pages_max_per_user);
1600 printed += scnprintf(buf + printed, size - printed,
1602 "Hint:\tCheck /proc/sys/kernel/perf_event_mlock_kb (%d kB) setting.\n"
1603 "Hint:\tTried using %zd kB.\n",
1604 emsg, pages_max_per_user, pages_attempted);
1606 if (pages_attempted >= pages_max_per_user) {
1607 printed += scnprintf(buf + printed, size - printed,
1608 "Hint:\tTry 'sudo sh -c \"echo %d > /proc/sys/kernel/perf_event_mlock_kb\"', or\n",
1609 pages_max_per_user + pages_attempted);
1612 printed += scnprintf(buf + printed, size - printed,
1613 "Hint:\tTry using a smaller -m/--mmap-pages value.");
1616 scnprintf(buf, size, "%s", emsg);
1623 void evlist__to_front(struct evlist *evlist, struct evsel *move_evsel)
1625 struct evsel *evsel, *n;
1628 if (move_evsel == evlist__first(evlist))
1631 evlist__for_each_entry_safe(evlist, n, evsel) {
1632 if (evsel->leader == move_evsel->leader)
1633 list_move_tail(&evsel->core.node, &move);
1636 list_splice(&move, &evlist->core.entries);
1639 struct evsel *evlist__get_tracking_event(struct evlist *evlist)
1641 struct evsel *evsel;
1643 evlist__for_each_entry(evlist, evsel) {
1644 if (evsel->tracking)
1648 return evlist__first(evlist);
1651 void evlist__set_tracking_event(struct evlist *evlist, struct evsel *tracking_evsel)
1653 struct evsel *evsel;
1655 if (tracking_evsel->tracking)
1658 evlist__for_each_entry(evlist, evsel) {
1659 if (evsel != tracking_evsel)
1660 evsel->tracking = false;
1663 tracking_evsel->tracking = true;
1666 struct evsel *evlist__find_evsel_by_str(struct evlist *evlist, const char *str)
1668 struct evsel *evsel;
1670 evlist__for_each_entry(evlist, evsel) {
1673 if (strcmp(str, evsel->name) == 0)
1680 void evlist__toggle_bkw_mmap(struct evlist *evlist, enum bkw_mmap_state state)
1682 enum bkw_mmap_state old_state = evlist->bkw_mmap_state;
1689 if (!evlist->overwrite_mmap)
1692 switch (old_state) {
1693 case BKW_MMAP_NOTREADY: {
1694 if (state != BKW_MMAP_RUNNING)
1698 case BKW_MMAP_RUNNING: {
1699 if (state != BKW_MMAP_DATA_PENDING)
1704 case BKW_MMAP_DATA_PENDING: {
1705 if (state != BKW_MMAP_EMPTY)
1709 case BKW_MMAP_EMPTY: {
1710 if (state != BKW_MMAP_RUNNING)
1716 WARN_ONCE(1, "Shouldn't get there\n");
1719 evlist->bkw_mmap_state = state;
1723 evlist__pause(evlist);
1726 evlist__resume(evlist);
1737 bool evlist__exclude_kernel(struct evlist *evlist)
1739 struct evsel *evsel;
1741 evlist__for_each_entry(evlist, evsel) {
1742 if (!evsel->core.attr.exclude_kernel)
1750 * Events in data file are not collect in groups, but we still want
1751 * the group display. Set the artificial group and set the leader's
1752 * forced_leader flag to notify the display code.
1754 void evlist__force_leader(struct evlist *evlist)
1756 if (!evlist->nr_groups) {
1757 struct evsel *leader = evlist__first(evlist);
1759 evlist__set_leader(evlist);
1760 leader->forced_leader = true;
1764 struct evsel *evlist__reset_weak_group(struct evlist *evsel_list, struct evsel *evsel, bool close)
1766 struct evsel *c2, *leader;
1767 bool is_open = true;
1769 leader = evsel->leader;
1770 pr_debug("Weak group for %s/%d failed\n",
1771 leader->name, leader->core.nr_members);
1774 * for_each_group_member doesn't work here because it doesn't
1775 * include the first entry.
1777 evlist__for_each_entry(evsel_list, c2) {
1780 if (c2->leader == leader) {
1781 if (is_open && close)
1782 perf_evsel__close(&c2->core);
1784 c2->core.nr_members = 0;
1786 * Set this for all former members of the group
1787 * to indicate they get reopened.
1789 c2->reset_group = true;
1795 static int evlist__parse_control_fifo(const char *str, int *ctl_fd, int *ctl_fd_ack, bool *ctl_fd_close)
1800 if (strncmp(str, "fifo:", 5))
1804 if (!*str || *str == ',')
1816 * O_RDWR avoids POLLHUPs which is necessary to allow the other
1817 * end of a FIFO to be repeatedly opened and closed.
1819 fd = open(s, O_RDWR | O_NONBLOCK | O_CLOEXEC);
1821 pr_err("Failed to open '%s'\n", s);
1826 *ctl_fd_close = true;
1829 /* O_RDWR | O_NONBLOCK means the other end need not be open */
1830 fd = open(p, O_RDWR | O_NONBLOCK | O_CLOEXEC);
1832 pr_err("Failed to open '%s'\n", p);
1844 int evlist__parse_control(const char *str, int *ctl_fd, int *ctl_fd_ack, bool *ctl_fd_close)
1846 char *comma = NULL, *endptr = NULL;
1848 *ctl_fd_close = false;
1850 if (strncmp(str, "fd:", 3))
1851 return evlist__parse_control_fifo(str, ctl_fd, ctl_fd_ack, ctl_fd_close);
1853 *ctl_fd = strtoul(&str[3], &endptr, 0);
1854 if (endptr == &str[3])
1857 comma = strchr(str, ',');
1859 if (endptr != comma)
1862 *ctl_fd_ack = strtoul(comma + 1, &endptr, 0);
1863 if (endptr == comma + 1 || *endptr != '\0')
1870 void evlist__close_control(int ctl_fd, int ctl_fd_ack, bool *ctl_fd_close)
1872 if (*ctl_fd_close) {
1873 *ctl_fd_close = false;
1875 if (ctl_fd_ack >= 0)
1880 int evlist__initialize_ctlfd(struct evlist *evlist, int fd, int ack)
1883 pr_debug("Control descriptor is not initialized\n");
1887 evlist->ctl_fd.pos = perf_evlist__add_pollfd(&evlist->core, fd, NULL, POLLIN,
1888 fdarray_flag__nonfilterable);
1889 if (evlist->ctl_fd.pos < 0) {
1890 evlist->ctl_fd.pos = -1;
1891 pr_err("Failed to add ctl fd entry: %m\n");
1895 evlist->ctl_fd.fd = fd;
1896 evlist->ctl_fd.ack = ack;
1901 bool evlist__ctlfd_initialized(struct evlist *evlist)
1903 return evlist->ctl_fd.pos >= 0;
1906 int evlist__finalize_ctlfd(struct evlist *evlist)
1908 struct pollfd *entries = evlist->core.pollfd.entries;
1910 if (!evlist__ctlfd_initialized(evlist))
1913 entries[evlist->ctl_fd.pos].fd = -1;
1914 entries[evlist->ctl_fd.pos].events = 0;
1915 entries[evlist->ctl_fd.pos].revents = 0;
1917 evlist->ctl_fd.pos = -1;
1918 evlist->ctl_fd.ack = -1;
1919 evlist->ctl_fd.fd = -1;
1924 static int evlist__ctlfd_recv(struct evlist *evlist, enum evlist_ctl_cmd *cmd,
1925 char *cmd_data, size_t data_size)
1929 size_t bytes_read = 0;
1931 *cmd = EVLIST_CTL_CMD_UNSUPPORTED;
1932 memset(cmd_data, 0, data_size);
1936 err = read(evlist->ctl_fd.fd, &c, 1);
1938 if (c == '\n' || c == '\0')
1940 cmd_data[bytes_read++] = c;
1941 if (bytes_read == data_size)
1944 } else if (err == -1) {
1947 if (errno == EAGAIN || errno == EWOULDBLOCK)
1950 pr_err("Failed to read from ctlfd %d: %m\n", evlist->ctl_fd.fd);
1955 pr_debug("Message from ctl_fd: \"%s%s\"\n", cmd_data,
1956 bytes_read == data_size ? "" : c == '\n' ? "\\n" : "\\0");
1958 if (bytes_read > 0) {
1959 if (!strncmp(cmd_data, EVLIST_CTL_CMD_ENABLE_TAG,
1960 (sizeof(EVLIST_CTL_CMD_ENABLE_TAG)-1))) {
1961 *cmd = EVLIST_CTL_CMD_ENABLE;
1962 } else if (!strncmp(cmd_data, EVLIST_CTL_CMD_DISABLE_TAG,
1963 (sizeof(EVLIST_CTL_CMD_DISABLE_TAG)-1))) {
1964 *cmd = EVLIST_CTL_CMD_DISABLE;
1965 } else if (!strncmp(cmd_data, EVLIST_CTL_CMD_SNAPSHOT_TAG,
1966 (sizeof(EVLIST_CTL_CMD_SNAPSHOT_TAG)-1))) {
1967 *cmd = EVLIST_CTL_CMD_SNAPSHOT;
1968 pr_debug("is snapshot\n");
1969 } else if (!strncmp(cmd_data, EVLIST_CTL_CMD_EVLIST_TAG,
1970 (sizeof(EVLIST_CTL_CMD_EVLIST_TAG)-1))) {
1971 *cmd = EVLIST_CTL_CMD_EVLIST;
1972 } else if (!strncmp(cmd_data, EVLIST_CTL_CMD_STOP_TAG,
1973 (sizeof(EVLIST_CTL_CMD_STOP_TAG)-1))) {
1974 *cmd = EVLIST_CTL_CMD_STOP;
1975 } else if (!strncmp(cmd_data, EVLIST_CTL_CMD_PING_TAG,
1976 (sizeof(EVLIST_CTL_CMD_PING_TAG)-1))) {
1977 *cmd = EVLIST_CTL_CMD_PING;
1981 return bytes_read ? (int)bytes_read : err;
1984 int evlist__ctlfd_ack(struct evlist *evlist)
1988 if (evlist->ctl_fd.ack == -1)
1991 err = write(evlist->ctl_fd.ack, EVLIST_CTL_CMD_ACK_TAG,
1992 sizeof(EVLIST_CTL_CMD_ACK_TAG));
1994 pr_err("failed to write to ctl_ack_fd %d: %m\n", evlist->ctl_fd.ack);
1999 static int get_cmd_arg(char *cmd_data, size_t cmd_size, char **arg)
2001 char *data = cmd_data + cmd_size;
2007 /* there's argument */
2017 static int evlist__ctlfd_enable(struct evlist *evlist, char *cmd_data, bool enable)
2019 struct evsel *evsel;
2023 err = get_cmd_arg(cmd_data,
2024 enable ? sizeof(EVLIST_CTL_CMD_ENABLE_TAG) - 1 :
2025 sizeof(EVLIST_CTL_CMD_DISABLE_TAG) - 1,
2028 pr_info("failed: wrong command\n");
2033 evsel = evlist__find_evsel_by_str(evlist, name);
2036 evlist__enable_evsel(evlist, name);
2038 evlist__disable_evsel(evlist, name);
2039 pr_info("Event %s %s\n", evsel->name,
2040 enable ? "enabled" : "disabled");
2042 pr_info("failed: can't find '%s' event\n", name);
2046 evlist__enable(evlist);
2047 pr_info(EVLIST_ENABLED_MSG);
2049 evlist__disable(evlist);
2050 pr_info(EVLIST_DISABLED_MSG);
2057 static int evlist__ctlfd_list(struct evlist *evlist, char *cmd_data)
2059 struct perf_attr_details details = { .verbose = false, };
2060 struct evsel *evsel;
2064 err = get_cmd_arg(cmd_data,
2065 sizeof(EVLIST_CTL_CMD_EVLIST_TAG) - 1,
2068 pr_info("failed: wrong command\n");
2073 if (!strcmp(arg, "-v")) {
2074 details.verbose = true;
2075 } else if (!strcmp(arg, "-g")) {
2076 details.event_group = true;
2077 } else if (!strcmp(arg, "-F")) {
2078 details.freq = true;
2080 pr_info("failed: wrong command\n");
2085 evlist__for_each_entry(evlist, evsel)
2086 evsel__fprintf(evsel, &details, stderr);
2091 int evlist__ctlfd_process(struct evlist *evlist, enum evlist_ctl_cmd *cmd)
2094 char cmd_data[EVLIST_CTL_CMD_MAX_LEN];
2095 int ctlfd_pos = evlist->ctl_fd.pos;
2096 struct pollfd *entries = evlist->core.pollfd.entries;
2098 if (!evlist__ctlfd_initialized(evlist) || !entries[ctlfd_pos].revents)
2101 if (entries[ctlfd_pos].revents & POLLIN) {
2102 err = evlist__ctlfd_recv(evlist, cmd, cmd_data,
2103 EVLIST_CTL_CMD_MAX_LEN);
2106 case EVLIST_CTL_CMD_ENABLE:
2107 case EVLIST_CTL_CMD_DISABLE:
2108 err = evlist__ctlfd_enable(evlist, cmd_data,
2109 *cmd == EVLIST_CTL_CMD_ENABLE);
2111 case EVLIST_CTL_CMD_EVLIST:
2112 err = evlist__ctlfd_list(evlist, cmd_data);
2114 case EVLIST_CTL_CMD_SNAPSHOT:
2115 case EVLIST_CTL_CMD_STOP:
2116 case EVLIST_CTL_CMD_PING:
2118 case EVLIST_CTL_CMD_ACK:
2119 case EVLIST_CTL_CMD_UNSUPPORTED:
2121 pr_debug("ctlfd: unsupported %d\n", *cmd);
2124 if (!(*cmd == EVLIST_CTL_CMD_ACK || *cmd == EVLIST_CTL_CMD_UNSUPPORTED ||
2125 *cmd == EVLIST_CTL_CMD_SNAPSHOT))
2126 evlist__ctlfd_ack(evlist);
2130 if (entries[ctlfd_pos].revents & (POLLHUP | POLLERR))
2131 evlist__finalize_ctlfd(evlist);
2133 entries[ctlfd_pos].revents = 0;
2138 struct evsel *evlist__find_evsel(struct evlist *evlist, int idx)
2140 struct evsel *evsel;
2142 evlist__for_each_entry(evlist, evsel) {
2143 if (evsel->idx == idx)
2149 int evlist__scnprintf_evsels(struct evlist *evlist, size_t size, char *bf)
2151 struct evsel *evsel;
2154 evlist__for_each_entry(evlist, evsel) {
2155 if (evsel__is_dummy_event(evsel))
2157 if (size > (strlen(evsel__name(evsel)) + (printed ? 2 : 1))) {
2158 printed += scnprintf(bf + printed, size - printed, "%s%s", printed ? "," : "", evsel__name(evsel));
2160 printed += scnprintf(bf + printed, size - printed, "%s...", printed ? "," : "");