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 <internal/lib.h> // page_size
24 #include "bpf-event.h"
25 #include "util/string2.h"
26 #include "util/perf_api_probe.h"
27 #include "util/evsel_fprintf.h"
33 #include "parse-events.h"
34 #include <subcmd/parse-options.h>
37 #include <sys/ioctl.h>
40 #include <linux/bitops.h>
41 #include <linux/hash.h>
42 #include <linux/log2.h>
43 #include <linux/err.h>
44 #include <linux/string.h>
45 #include <linux/zalloc.h>
46 #include <perf/evlist.h>
47 #include <perf/evsel.h>
48 #include <perf/cpumap.h>
49 #include <perf/mmap.h>
51 #include <internal/xyarray.h>
53 #ifdef LACKS_SIGQUEUE_PROTOTYPE
54 int sigqueue(pid_t pid, int sig, const union sigval value);
57 #define FD(e, x, y) (*(int *)xyarray__entry(e->core.fd, x, y))
58 #define SID(e, x, y) xyarray__entry(e->core.sample_id, x, y)
60 void evlist__init(struct evlist *evlist, struct perf_cpu_map *cpus,
61 struct perf_thread_map *threads)
63 perf_evlist__init(&evlist->core);
64 perf_evlist__set_maps(&evlist->core, cpus, threads);
65 evlist->workload.pid = -1;
66 evlist->bkw_mmap_state = BKW_MMAP_NOTREADY;
67 evlist->ctl_fd.fd = -1;
68 evlist->ctl_fd.ack = -1;
69 evlist->ctl_fd.pos = -1;
72 struct evlist *evlist__new(void)
74 struct evlist *evlist = zalloc(sizeof(*evlist));
77 evlist__init(evlist, NULL, NULL);
82 struct evlist *evlist__new_default(void)
84 struct evlist *evlist = evlist__new();
86 if (evlist && evlist__add_default(evlist)) {
87 evlist__delete(evlist);
94 struct evlist *evlist__new_dummy(void)
96 struct evlist *evlist = evlist__new();
98 if (evlist && evlist__add_dummy(evlist)) {
99 evlist__delete(evlist);
107 * evlist__set_id_pos - set the positions of event ids.
108 * @evlist: selected event list
110 * Events with compatible sample types all have the same id_pos
111 * and is_pos. For convenience, put a copy on evlist.
113 void evlist__set_id_pos(struct evlist *evlist)
115 struct evsel *first = evlist__first(evlist);
117 evlist->id_pos = first->id_pos;
118 evlist->is_pos = first->is_pos;
121 static void evlist__update_id_pos(struct evlist *evlist)
125 evlist__for_each_entry(evlist, evsel)
126 evsel__calc_id_pos(evsel);
128 evlist__set_id_pos(evlist);
131 static void evlist__purge(struct evlist *evlist)
133 struct evsel *pos, *n;
135 evlist__for_each_entry_safe(evlist, n, pos) {
136 list_del_init(&pos->core.node);
141 evlist->core.nr_entries = 0;
144 void evlist__exit(struct evlist *evlist)
146 zfree(&evlist->mmap);
147 zfree(&evlist->overwrite_mmap);
148 perf_evlist__exit(&evlist->core);
151 void evlist__delete(struct evlist *evlist)
156 evlist__munmap(evlist);
157 evlist__close(evlist);
158 evlist__purge(evlist);
159 evlist__exit(evlist);
163 void evlist__add(struct evlist *evlist, struct evsel *entry)
165 entry->evlist = evlist;
166 entry->idx = evlist->core.nr_entries;
167 entry->tracking = !entry->idx;
169 perf_evlist__add(&evlist->core, &entry->core);
171 if (evlist->core.nr_entries == 1)
172 evlist__set_id_pos(evlist);
175 void evlist__remove(struct evlist *evlist, struct evsel *evsel)
177 evsel->evlist = NULL;
178 perf_evlist__remove(&evlist->core, &evsel->core);
181 void evlist__splice_list_tail(struct evlist *evlist, struct list_head *list)
183 while (!list_empty(list)) {
184 struct evsel *evsel, *temp, *leader = NULL;
186 __evlist__for_each_entry_safe(list, temp, evsel) {
187 list_del_init(&evsel->core.node);
188 evlist__add(evlist, evsel);
193 __evlist__for_each_entry_safe(list, temp, evsel) {
194 if (evsel->leader == leader) {
195 list_del_init(&evsel->core.node);
196 evlist__add(evlist, evsel);
202 int __evlist__set_tracepoints_handlers(struct evlist *evlist,
203 const struct evsel_str_handler *assocs, size_t nr_assocs)
208 for (i = 0; i < nr_assocs; i++) {
209 // Adding a handler for an event not in this evlist, just ignore it.
210 struct evsel *evsel = evlist__find_tracepoint_by_name(evlist, assocs[i].name);
215 if (evsel->handler != NULL)
217 evsel->handler = assocs[i].handler;
225 void __evlist__set_leader(struct list_head *list)
227 struct evsel *evsel, *leader;
229 leader = list_entry(list->next, struct evsel, core.node);
230 evsel = list_entry(list->prev, struct evsel, core.node);
232 leader->core.nr_members = evsel->idx - leader->idx + 1;
234 __evlist__for_each_entry(list, evsel) {
235 evsel->leader = leader;
239 void evlist__set_leader(struct evlist *evlist)
241 if (evlist->core.nr_entries) {
242 evlist->nr_groups = evlist->core.nr_entries > 1 ? 1 : 0;
243 __evlist__set_leader(&evlist->core.entries);
247 int __evlist__add_default(struct evlist *evlist, bool precise)
249 struct evsel *evsel = evsel__new_cycles(precise);
254 evlist__add(evlist, evsel);
258 int evlist__add_dummy(struct evlist *evlist)
260 struct perf_event_attr attr = {
261 .type = PERF_TYPE_SOFTWARE,
262 .config = PERF_COUNT_SW_DUMMY,
263 .size = sizeof(attr), /* to capture ABI version */
265 struct evsel *evsel = evsel__new_idx(&attr, evlist->core.nr_entries);
270 evlist__add(evlist, evsel);
274 static int evlist__add_attrs(struct evlist *evlist, struct perf_event_attr *attrs, size_t nr_attrs)
276 struct evsel *evsel, *n;
280 for (i = 0; i < nr_attrs; i++) {
281 evsel = evsel__new_idx(attrs + i, evlist->core.nr_entries + i);
283 goto out_delete_partial_list;
284 list_add_tail(&evsel->core.node, &head);
287 evlist__splice_list_tail(evlist, &head);
291 out_delete_partial_list:
292 __evlist__for_each_entry_safe(&head, n, evsel)
293 evsel__delete(evsel);
297 int __evlist__add_default_attrs(struct evlist *evlist, struct perf_event_attr *attrs, size_t nr_attrs)
301 for (i = 0; i < nr_attrs; i++)
302 event_attr_init(attrs + i);
304 return evlist__add_attrs(evlist, attrs, nr_attrs);
307 __weak int arch_evlist__add_default_attrs(struct evlist *evlist __maybe_unused)
312 struct evsel *evlist__find_tracepoint_by_id(struct evlist *evlist, int id)
316 evlist__for_each_entry(evlist, evsel) {
317 if (evsel->core.attr.type == PERF_TYPE_TRACEPOINT &&
318 (int)evsel->core.attr.config == id)
325 struct evsel *evlist__find_tracepoint_by_name(struct evlist *evlist, const char *name)
329 evlist__for_each_entry(evlist, evsel) {
330 if ((evsel->core.attr.type == PERF_TYPE_TRACEPOINT) &&
331 (strcmp(evsel->name, name) == 0))
338 int evlist__add_newtp(struct evlist *evlist, const char *sys, const char *name, void *handler)
340 struct evsel *evsel = evsel__newtp(sys, name);
345 evsel->handler = handler;
346 evlist__add(evlist, evsel);
350 static int evlist__nr_threads(struct evlist *evlist, struct evsel *evsel)
352 if (evsel->core.system_wide)
355 return perf_thread_map__nr(evlist->core.threads);
358 void evlist__cpu_iter_start(struct evlist *evlist)
363 * Reset the per evsel cpu_iter. This is needed because
364 * each evsel's cpumap may have a different index space,
365 * and some operations need the index to modify
366 * the FD xyarray (e.g. open, close)
368 evlist__for_each_entry(evlist, pos)
372 bool evsel__cpu_iter_skip_no_inc(struct evsel *ev, int cpu)
374 if (ev->cpu_iter >= ev->core.cpus->nr)
376 if (cpu >= 0 && ev->core.cpus->map[ev->cpu_iter] != cpu)
381 bool evsel__cpu_iter_skip(struct evsel *ev, int cpu)
383 if (!evsel__cpu_iter_skip_no_inc(ev, cpu)) {
390 static int evsel__strcmp(struct evsel *pos, char *evsel_name)
394 if (evsel__is_dummy_event(pos))
396 return strcmp(pos->name, evsel_name);
399 static int evlist__is_enabled(struct evlist *evlist)
403 evlist__for_each_entry(evlist, pos) {
404 if (!evsel__is_group_leader(pos) || !pos->core.fd)
406 /* If at least one event is enabled, evlist is enabled. */
413 static void __evlist__disable(struct evlist *evlist, char *evsel_name)
416 struct affinity affinity;
418 bool has_imm = false;
420 if (affinity__setup(&affinity) < 0)
423 /* Disable 'immediate' events last */
424 for (imm = 0; imm <= 1; imm++) {
425 evlist__for_each_cpu(evlist, i, cpu) {
426 affinity__set(&affinity, cpu);
428 evlist__for_each_entry(evlist, pos) {
429 if (evsel__strcmp(pos, evsel_name))
431 if (evsel__cpu_iter_skip(pos, cpu))
433 if (pos->disabled || !evsel__is_group_leader(pos) || !pos->core.fd)
437 if (pos->immediate != imm)
439 evsel__disable_cpu(pos, pos->cpu_iter - 1);
446 affinity__cleanup(&affinity);
447 evlist__for_each_entry(evlist, pos) {
448 if (evsel__strcmp(pos, evsel_name))
450 if (!evsel__is_group_leader(pos) || !pos->core.fd)
452 pos->disabled = true;
456 * If we disabled only single event, we need to check
457 * the enabled state of the evlist manually.
460 evlist->enabled = evlist__is_enabled(evlist);
462 evlist->enabled = false;
465 void evlist__disable(struct evlist *evlist)
467 __evlist__disable(evlist, NULL);
470 void evlist__disable_evsel(struct evlist *evlist, char *evsel_name)
472 __evlist__disable(evlist, evsel_name);
475 static void __evlist__enable(struct evlist *evlist, char *evsel_name)
478 struct affinity affinity;
481 if (affinity__setup(&affinity) < 0)
484 evlist__for_each_cpu(evlist, i, cpu) {
485 affinity__set(&affinity, cpu);
487 evlist__for_each_entry(evlist, pos) {
488 if (evsel__strcmp(pos, evsel_name))
490 if (evsel__cpu_iter_skip(pos, cpu))
492 if (!evsel__is_group_leader(pos) || !pos->core.fd)
494 evsel__enable_cpu(pos, pos->cpu_iter - 1);
497 affinity__cleanup(&affinity);
498 evlist__for_each_entry(evlist, pos) {
499 if (evsel__strcmp(pos, evsel_name))
501 if (!evsel__is_group_leader(pos) || !pos->core.fd)
503 pos->disabled = false;
507 * Even single event sets the 'enabled' for evlist,
508 * so the toggle can work properly and toggle to
511 evlist->enabled = true;
514 void evlist__enable(struct evlist *evlist)
516 __evlist__enable(evlist, NULL);
519 void evlist__enable_evsel(struct evlist *evlist, char *evsel_name)
521 __evlist__enable(evlist, evsel_name);
524 void evlist__toggle_enable(struct evlist *evlist)
526 (evlist->enabled ? evlist__disable : evlist__enable)(evlist);
529 static int evlist__enable_event_cpu(struct evlist *evlist, struct evsel *evsel, int cpu)
532 int nr_threads = evlist__nr_threads(evlist, evsel);
537 for (thread = 0; thread < nr_threads; thread++) {
538 int err = ioctl(FD(evsel, cpu, thread), PERF_EVENT_IOC_ENABLE, 0);
545 static int evlist__enable_event_thread(struct evlist *evlist, struct evsel *evsel, int thread)
548 int nr_cpus = perf_cpu_map__nr(evlist->core.cpus);
553 for (cpu = 0; cpu < nr_cpus; cpu++) {
554 int err = ioctl(FD(evsel, cpu, thread), PERF_EVENT_IOC_ENABLE, 0);
561 int evlist__enable_event_idx(struct evlist *evlist, struct evsel *evsel, int idx)
563 bool per_cpu_mmaps = !perf_cpu_map__empty(evlist->core.cpus);
566 return evlist__enable_event_cpu(evlist, evsel, idx);
568 return evlist__enable_event_thread(evlist, evsel, idx);
571 int evlist__add_pollfd(struct evlist *evlist, int fd)
573 return perf_evlist__add_pollfd(&evlist->core, fd, NULL, POLLIN, fdarray_flag__default);
576 int evlist__filter_pollfd(struct evlist *evlist, short revents_and_mask)
578 return perf_evlist__filter_pollfd(&evlist->core, revents_and_mask);
581 #ifdef HAVE_EVENTFD_SUPPORT
582 int evlist__add_wakeup_eventfd(struct evlist *evlist, int fd)
584 return perf_evlist__add_pollfd(&evlist->core, fd, NULL, POLLIN,
585 fdarray_flag__nonfilterable);
589 int evlist__poll(struct evlist *evlist, int timeout)
591 return perf_evlist__poll(&evlist->core, timeout);
594 struct perf_sample_id *evlist__id2sid(struct evlist *evlist, u64 id)
596 struct hlist_head *head;
597 struct perf_sample_id *sid;
600 hash = hash_64(id, PERF_EVLIST__HLIST_BITS);
601 head = &evlist->core.heads[hash];
603 hlist_for_each_entry(sid, head, node)
610 struct evsel *evlist__id2evsel(struct evlist *evlist, u64 id)
612 struct perf_sample_id *sid;
614 if (evlist->core.nr_entries == 1 || !id)
615 return evlist__first(evlist);
617 sid = evlist__id2sid(evlist, id);
619 return container_of(sid->evsel, struct evsel, core);
621 if (!evlist__sample_id_all(evlist))
622 return evlist__first(evlist);
627 struct evsel *evlist__id2evsel_strict(struct evlist *evlist, u64 id)
629 struct perf_sample_id *sid;
634 sid = evlist__id2sid(evlist, id);
636 return container_of(sid->evsel, struct evsel, core);
641 static int evlist__event2id(struct evlist *evlist, union perf_event *event, u64 *id)
643 const __u64 *array = event->sample.array;
646 n = (event->header.size - sizeof(event->header)) >> 3;
648 if (event->header.type == PERF_RECORD_SAMPLE) {
649 if (evlist->id_pos >= n)
651 *id = array[evlist->id_pos];
653 if (evlist->is_pos > n)
661 struct evsel *evlist__event2evsel(struct evlist *evlist, union perf_event *event)
663 struct evsel *first = evlist__first(evlist);
664 struct hlist_head *head;
665 struct perf_sample_id *sid;
669 if (evlist->core.nr_entries == 1)
672 if (!first->core.attr.sample_id_all &&
673 event->header.type != PERF_RECORD_SAMPLE)
676 if (evlist__event2id(evlist, event, &id))
679 /* Synthesized events have an id of zero */
683 hash = hash_64(id, PERF_EVLIST__HLIST_BITS);
684 head = &evlist->core.heads[hash];
686 hlist_for_each_entry(sid, head, node) {
688 return container_of(sid->evsel, struct evsel, core);
693 static int evlist__set_paused(struct evlist *evlist, bool value)
697 if (!evlist->overwrite_mmap)
700 for (i = 0; i < evlist->core.nr_mmaps; i++) {
701 int fd = evlist->overwrite_mmap[i].core.fd;
706 err = ioctl(fd, PERF_EVENT_IOC_PAUSE_OUTPUT, value ? 1 : 0);
713 static int evlist__pause(struct evlist *evlist)
715 return evlist__set_paused(evlist, true);
718 static int evlist__resume(struct evlist *evlist)
720 return evlist__set_paused(evlist, false);
723 static void evlist__munmap_nofree(struct evlist *evlist)
728 for (i = 0; i < evlist->core.nr_mmaps; i++)
729 perf_mmap__munmap(&evlist->mmap[i].core);
731 if (evlist->overwrite_mmap)
732 for (i = 0; i < evlist->core.nr_mmaps; i++)
733 perf_mmap__munmap(&evlist->overwrite_mmap[i].core);
736 void evlist__munmap(struct evlist *evlist)
738 evlist__munmap_nofree(evlist);
739 zfree(&evlist->mmap);
740 zfree(&evlist->overwrite_mmap);
743 static void perf_mmap__unmap_cb(struct perf_mmap *map)
745 struct mmap *m = container_of(map, struct mmap, core);
750 static struct mmap *evlist__alloc_mmap(struct evlist *evlist,
756 map = zalloc(evlist->core.nr_mmaps * sizeof(struct mmap));
760 for (i = 0; i < evlist->core.nr_mmaps; i++) {
761 struct perf_mmap *prev = i ? &map[i - 1].core : NULL;
764 * When the perf_mmap() call is made we grab one refcount, plus
765 * one extra to let perf_mmap__consume() get the last
766 * events after all real references (perf_mmap__get()) are
769 * Each PERF_EVENT_IOC_SET_OUTPUT points to this mmap and
770 * thus does perf_mmap__get() on it.
772 perf_mmap__init(&map[i].core, prev, overwrite, perf_mmap__unmap_cb);
779 perf_evlist__mmap_cb_idx(struct perf_evlist *_evlist,
780 struct perf_mmap_param *_mp,
781 int idx, bool per_cpu)
783 struct evlist *evlist = container_of(_evlist, struct evlist, core);
784 struct mmap_params *mp = container_of(_mp, struct mmap_params, core);
786 auxtrace_mmap_params__set_idx(&mp->auxtrace_mp, evlist, idx, per_cpu);
789 static struct perf_mmap*
790 perf_evlist__mmap_cb_get(struct perf_evlist *_evlist, bool overwrite, int idx)
792 struct evlist *evlist = container_of(_evlist, struct evlist, core);
795 maps = overwrite ? evlist->overwrite_mmap : evlist->mmap;
798 maps = evlist__alloc_mmap(evlist, overwrite);
803 evlist->overwrite_mmap = maps;
804 if (evlist->bkw_mmap_state == BKW_MMAP_NOTREADY)
805 evlist__toggle_bkw_mmap(evlist, BKW_MMAP_RUNNING);
811 return &maps[idx].core;
815 perf_evlist__mmap_cb_mmap(struct perf_mmap *_map, struct perf_mmap_param *_mp,
818 struct mmap *map = container_of(_map, struct mmap, core);
819 struct mmap_params *mp = container_of(_mp, struct mmap_params, core);
821 return mmap__mmap(map, mp, output, cpu);
824 unsigned long perf_event_mlock_kb_in_pages(void)
829 if (sysctl__read_int("kernel/perf_event_mlock_kb", &max) < 0) {
831 * Pick a once upon a time good value, i.e. things look
832 * strange since we can't read a sysctl value, but lets not
837 max -= (page_size / 1024);
840 pages = (max * 1024) / page_size;
841 if (!is_power_of_2(pages))
842 pages = rounddown_pow_of_two(pages);
847 size_t evlist__mmap_size(unsigned long pages)
849 if (pages == UINT_MAX)
850 pages = perf_event_mlock_kb_in_pages();
851 else if (!is_power_of_2(pages))
854 return (pages + 1) * page_size;
857 static long parse_pages_arg(const char *str, unsigned long min,
860 unsigned long pages, val;
861 static struct parse_tag tags[] = {
862 { .tag = 'B', .mult = 1 },
863 { .tag = 'K', .mult = 1 << 10 },
864 { .tag = 'M', .mult = 1 << 20 },
865 { .tag = 'G', .mult = 1 << 30 },
872 val = parse_tag_value(str, tags);
873 if (val != (unsigned long) -1) {
874 /* we got file size value */
875 pages = PERF_ALIGN(val, page_size) / page_size;
877 /* we got pages count value */
879 pages = strtoul(str, &eptr, 10);
884 if (pages == 0 && min == 0) {
885 /* leave number of pages at 0 */
886 } else if (!is_power_of_2(pages)) {
889 /* round pages up to next power of 2 */
890 pages = roundup_pow_of_two(pages);
894 unit_number__scnprintf(buf, sizeof(buf), pages * page_size);
895 pr_info("rounding mmap pages size to %s (%lu pages)\n",
905 int __evlist__parse_mmap_pages(unsigned int *mmap_pages, const char *str)
907 unsigned long max = UINT_MAX;
910 if (max > SIZE_MAX / page_size)
911 max = SIZE_MAX / page_size;
913 pages = parse_pages_arg(str, 1, max);
915 pr_err("Invalid argument for --mmap_pages/-m\n");
923 int evlist__parse_mmap_pages(const struct option *opt, const char *str, int unset __maybe_unused)
925 return __evlist__parse_mmap_pages(opt->value, str);
929 * evlist__mmap_ex - Create mmaps to receive events.
930 * @evlist: list of events
931 * @pages: map length in pages
932 * @overwrite: overwrite older events?
933 * @auxtrace_pages - auxtrace map length in pages
934 * @auxtrace_overwrite - overwrite older auxtrace data?
936 * If @overwrite is %false the user needs to signal event consumption using
937 * perf_mmap__write_tail(). Using evlist__mmap_read() does this
940 * Similarly, if @auxtrace_overwrite is %false the user needs to signal data
941 * consumption using auxtrace_mmap__write_tail().
943 * Return: %0 on success, negative error code otherwise.
945 int evlist__mmap_ex(struct evlist *evlist, unsigned int pages,
946 unsigned int auxtrace_pages,
947 bool auxtrace_overwrite, int nr_cblocks, int affinity, int flush,
951 * Delay setting mp.prot: set it before calling perf_mmap__mmap.
952 * Its value is decided by evsel's write_backward.
953 * So &mp should not be passed through const pointer.
955 struct mmap_params mp = {
956 .nr_cblocks = nr_cblocks,
957 .affinity = affinity,
959 .comp_level = comp_level
961 struct perf_evlist_mmap_ops ops = {
962 .idx = perf_evlist__mmap_cb_idx,
963 .get = perf_evlist__mmap_cb_get,
964 .mmap = perf_evlist__mmap_cb_mmap,
967 evlist->core.mmap_len = evlist__mmap_size(pages);
968 pr_debug("mmap size %zuB\n", evlist->core.mmap_len);
970 auxtrace_mmap_params__init(&mp.auxtrace_mp, evlist->core.mmap_len,
971 auxtrace_pages, auxtrace_overwrite);
973 return perf_evlist__mmap_ops(&evlist->core, &ops, &mp.core);
976 int evlist__mmap(struct evlist *evlist, unsigned int pages)
978 return evlist__mmap_ex(evlist, pages, 0, false, 0, PERF_AFFINITY_SYS, 1, 0);
981 int evlist__create_maps(struct evlist *evlist, struct target *target)
983 bool all_threads = (target->per_thread && target->system_wide);
984 struct perf_cpu_map *cpus;
985 struct perf_thread_map *threads;
988 * If specify '-a' and '--per-thread' to perf record, perf record
989 * will override '--per-thread'. target->per_thread = false and
990 * target->system_wide = true.
992 * If specify '--per-thread' only to perf record,
993 * target->per_thread = true and target->system_wide = false.
995 * So target->per_thread && target->system_wide is false.
996 * For perf record, thread_map__new_str doesn't call
997 * thread_map__new_all_cpus. That will keep perf record's
1000 * For perf stat, it allows the case that target->per_thread and
1001 * target->system_wide are all true. It means to collect system-wide
1002 * per-thread data. thread_map__new_str will call
1003 * thread_map__new_all_cpus to enumerate all threads.
1005 threads = thread_map__new_str(target->pid, target->tid, target->uid,
1011 if (target__uses_dummy_map(target))
1012 cpus = perf_cpu_map__dummy_new();
1014 cpus = perf_cpu_map__new(target->cpu_list);
1017 goto out_delete_threads;
1019 evlist->core.has_user_cpus = !!target->cpu_list;
1021 perf_evlist__set_maps(&evlist->core, cpus, threads);
1023 /* as evlist now has references, put count here */
1024 perf_cpu_map__put(cpus);
1025 perf_thread_map__put(threads);
1030 perf_thread_map__put(threads);
1034 int evlist__apply_filters(struct evlist *evlist, struct evsel **err_evsel)
1036 struct evsel *evsel;
1039 evlist__for_each_entry(evlist, evsel) {
1040 if (evsel->filter == NULL)
1044 * filters only work for tracepoint event, which doesn't have cpu limit.
1045 * So evlist and evsel should always be same.
1047 err = perf_evsel__apply_filter(&evsel->core, evsel->filter);
1057 int evlist__set_tp_filter(struct evlist *evlist, const char *filter)
1059 struct evsel *evsel;
1065 evlist__for_each_entry(evlist, evsel) {
1066 if (evsel->core.attr.type != PERF_TYPE_TRACEPOINT)
1069 err = evsel__set_filter(evsel, filter);
1077 int evlist__append_tp_filter(struct evlist *evlist, const char *filter)
1079 struct evsel *evsel;
1085 evlist__for_each_entry(evlist, evsel) {
1086 if (evsel->core.attr.type != PERF_TYPE_TRACEPOINT)
1089 err = evsel__append_tp_filter(evsel, filter);
1097 char *asprintf__tp_filter_pids(size_t npids, pid_t *pids)
1102 for (i = 0; i < npids; ++i) {
1104 if (asprintf(&filter, "common_pid != %d", pids[i]) < 0)
1109 if (asprintf(&tmp, "%s && common_pid != %d", filter, pids[i]) < 0)
1123 int evlist__set_tp_filter_pids(struct evlist *evlist, size_t npids, pid_t *pids)
1125 char *filter = asprintf__tp_filter_pids(npids, pids);
1126 int ret = evlist__set_tp_filter(evlist, filter);
1132 int evlist__set_tp_filter_pid(struct evlist *evlist, pid_t pid)
1134 return evlist__set_tp_filter_pids(evlist, 1, &pid);
1137 int evlist__append_tp_filter_pids(struct evlist *evlist, size_t npids, pid_t *pids)
1139 char *filter = asprintf__tp_filter_pids(npids, pids);
1140 int ret = evlist__append_tp_filter(evlist, filter);
1146 int evlist__append_tp_filter_pid(struct evlist *evlist, pid_t pid)
1148 return evlist__append_tp_filter_pids(evlist, 1, &pid);
1151 bool evlist__valid_sample_type(struct evlist *evlist)
1155 if (evlist->core.nr_entries == 1)
1158 if (evlist->id_pos < 0 || evlist->is_pos < 0)
1161 evlist__for_each_entry(evlist, pos) {
1162 if (pos->id_pos != evlist->id_pos ||
1163 pos->is_pos != evlist->is_pos)
1170 u64 __evlist__combined_sample_type(struct evlist *evlist)
1172 struct evsel *evsel;
1174 if (evlist->combined_sample_type)
1175 return evlist->combined_sample_type;
1177 evlist__for_each_entry(evlist, evsel)
1178 evlist->combined_sample_type |= evsel->core.attr.sample_type;
1180 return evlist->combined_sample_type;
1183 u64 evlist__combined_sample_type(struct evlist *evlist)
1185 evlist->combined_sample_type = 0;
1186 return __evlist__combined_sample_type(evlist);
1189 u64 evlist__combined_branch_type(struct evlist *evlist)
1191 struct evsel *evsel;
1192 u64 branch_type = 0;
1194 evlist__for_each_entry(evlist, evsel)
1195 branch_type |= evsel->core.attr.branch_sample_type;
1199 bool evlist__valid_read_format(struct evlist *evlist)
1201 struct evsel *first = evlist__first(evlist), *pos = first;
1202 u64 read_format = first->core.attr.read_format;
1203 u64 sample_type = first->core.attr.sample_type;
1205 evlist__for_each_entry(evlist, pos) {
1206 if (read_format != pos->core.attr.read_format) {
1207 pr_debug("Read format differs %#" PRIx64 " vs %#" PRIx64 "\n",
1208 read_format, (u64)pos->core.attr.read_format);
1212 /* PERF_SAMPLE_READ imples PERF_FORMAT_ID. */
1213 if ((sample_type & PERF_SAMPLE_READ) &&
1214 !(read_format & PERF_FORMAT_ID)) {
1221 u16 evlist__id_hdr_size(struct evlist *evlist)
1223 struct evsel *first = evlist__first(evlist);
1224 struct perf_sample *data;
1228 if (!first->core.attr.sample_id_all)
1231 sample_type = first->core.attr.sample_type;
1233 if (sample_type & PERF_SAMPLE_TID)
1234 size += sizeof(data->tid) * 2;
1236 if (sample_type & PERF_SAMPLE_TIME)
1237 size += sizeof(data->time);
1239 if (sample_type & PERF_SAMPLE_ID)
1240 size += sizeof(data->id);
1242 if (sample_type & PERF_SAMPLE_STREAM_ID)
1243 size += sizeof(data->stream_id);
1245 if (sample_type & PERF_SAMPLE_CPU)
1246 size += sizeof(data->cpu) * 2;
1248 if (sample_type & PERF_SAMPLE_IDENTIFIER)
1249 size += sizeof(data->id);
1254 bool evlist__valid_sample_id_all(struct evlist *evlist)
1256 struct evsel *first = evlist__first(evlist), *pos = first;
1258 evlist__for_each_entry_continue(evlist, pos) {
1259 if (first->core.attr.sample_id_all != pos->core.attr.sample_id_all)
1266 bool evlist__sample_id_all(struct evlist *evlist)
1268 struct evsel *first = evlist__first(evlist);
1269 return first->core.attr.sample_id_all;
1272 void evlist__set_selected(struct evlist *evlist, struct evsel *evsel)
1274 evlist->selected = evsel;
1277 void evlist__close(struct evlist *evlist)
1279 struct evsel *evsel;
1280 struct affinity affinity;
1284 * With perf record core.cpus is usually NULL.
1285 * Use the old method to handle this for now.
1287 if (!evlist->core.cpus) {
1288 evlist__for_each_entry_reverse(evlist, evsel)
1289 evsel__close(evsel);
1293 if (affinity__setup(&affinity) < 0)
1295 evlist__for_each_cpu(evlist, i, cpu) {
1296 affinity__set(&affinity, cpu);
1298 evlist__for_each_entry_reverse(evlist, evsel) {
1299 if (evsel__cpu_iter_skip(evsel, cpu))
1301 perf_evsel__close_cpu(&evsel->core, evsel->cpu_iter - 1);
1304 affinity__cleanup(&affinity);
1305 evlist__for_each_entry_reverse(evlist, evsel) {
1306 perf_evsel__free_fd(&evsel->core);
1307 perf_evsel__free_id(&evsel->core);
1309 perf_evlist__reset_id_hash(&evlist->core);
1312 static int evlist__create_syswide_maps(struct evlist *evlist)
1314 struct perf_cpu_map *cpus;
1315 struct perf_thread_map *threads;
1319 * Try reading /sys/devices/system/cpu/online to get
1322 * FIXME: -ENOMEM is the best we can do here, the cpu_map
1323 * code needs an overhaul to properly forward the
1324 * error, and we may not want to do that fallback to a
1325 * default cpu identity map :-\
1327 cpus = perf_cpu_map__new(NULL);
1331 threads = perf_thread_map__new_dummy();
1335 perf_evlist__set_maps(&evlist->core, cpus, threads);
1337 perf_thread_map__put(threads);
1339 perf_cpu_map__put(cpus);
1344 int evlist__open(struct evlist *evlist)
1346 struct evsel *evsel;
1350 * Default: one fd per CPU, all threads, aka systemwide
1351 * as sys_perf_event_open(cpu = -1, thread = -1) is EINVAL
1353 if (evlist->core.threads == NULL && evlist->core.cpus == NULL) {
1354 err = evlist__create_syswide_maps(evlist);
1359 evlist__update_id_pos(evlist);
1361 evlist__for_each_entry(evlist, evsel) {
1362 err = evsel__open(evsel, evsel->core.cpus, evsel->core.threads);
1369 evlist__close(evlist);
1374 int evlist__prepare_workload(struct evlist *evlist, struct target *target, const char *argv[],
1375 bool pipe_output, void (*exec_error)(int signo, siginfo_t *info, void *ucontext))
1377 int child_ready_pipe[2], go_pipe[2];
1380 if (pipe(child_ready_pipe) < 0) {
1381 perror("failed to create 'ready' pipe");
1385 if (pipe(go_pipe) < 0) {
1386 perror("failed to create 'go' pipe");
1387 goto out_close_ready_pipe;
1390 evlist->workload.pid = fork();
1391 if (evlist->workload.pid < 0) {
1392 perror("failed to fork");
1393 goto out_close_pipes;
1396 if (!evlist->workload.pid) {
1402 signal(SIGTERM, SIG_DFL);
1404 close(child_ready_pipe[0]);
1406 fcntl(go_pipe[0], F_SETFD, FD_CLOEXEC);
1409 * Tell the parent we're ready to go
1411 close(child_ready_pipe[1]);
1414 * Wait until the parent tells us to go.
1416 ret = read(go_pipe[0], &bf, 1);
1418 * The parent will ask for the execvp() to be performed by
1419 * writing exactly one byte, in workload.cork_fd, usually via
1420 * evlist__start_workload().
1422 * For cancelling the workload without actually running it,
1423 * the parent will just close workload.cork_fd, without writing
1424 * anything, i.e. read will return zero and we just exit()
1429 perror("unable to read pipe");
1433 execvp(argv[0], (char **)argv);
1438 val.sival_int = errno;
1439 if (sigqueue(getppid(), SIGUSR1, val))
1447 struct sigaction act = {
1448 .sa_flags = SA_SIGINFO,
1449 .sa_sigaction = exec_error,
1451 sigaction(SIGUSR1, &act, NULL);
1454 if (target__none(target)) {
1455 if (evlist->core.threads == NULL) {
1456 fprintf(stderr, "FATAL: evlist->threads need to be set at this point (%s:%d).\n",
1457 __func__, __LINE__);
1458 goto out_close_pipes;
1460 perf_thread_map__set_pid(evlist->core.threads, 0, evlist->workload.pid);
1463 close(child_ready_pipe[1]);
1466 * wait for child to settle
1468 if (read(child_ready_pipe[0], &bf, 1) == -1) {
1469 perror("unable to read pipe");
1470 goto out_close_pipes;
1473 fcntl(go_pipe[1], F_SETFD, FD_CLOEXEC);
1474 evlist->workload.cork_fd = go_pipe[1];
1475 close(child_ready_pipe[0]);
1481 out_close_ready_pipe:
1482 close(child_ready_pipe[0]);
1483 close(child_ready_pipe[1]);
1487 int evlist__start_workload(struct evlist *evlist)
1489 if (evlist->workload.cork_fd > 0) {
1493 * Remove the cork, let it rip!
1495 ret = write(evlist->workload.cork_fd, &bf, 1);
1497 perror("unable to write to pipe");
1499 close(evlist->workload.cork_fd);
1506 int evlist__parse_sample(struct evlist *evlist, union perf_event *event, struct perf_sample *sample)
1508 struct evsel *evsel = evlist__event2evsel(evlist, event);
1512 return evsel__parse_sample(evsel, event, sample);
1515 int evlist__parse_sample_timestamp(struct evlist *evlist, union perf_event *event, u64 *timestamp)
1517 struct evsel *evsel = evlist__event2evsel(evlist, event);
1521 return evsel__parse_sample_timestamp(evsel, event, timestamp);
1524 int evlist__strerror_open(struct evlist *evlist, int err, char *buf, size_t size)
1527 char sbuf[STRERR_BUFSIZE], *emsg = str_error_r(err, sbuf, sizeof(sbuf));
1532 printed = scnprintf(buf, size,
1534 "Hint:\tCheck /proc/sys/kernel/perf_event_paranoid setting.", emsg);
1536 value = perf_event_paranoid();
1538 printed += scnprintf(buf + printed, size - printed, "\nHint:\t");
1541 printed += scnprintf(buf + printed, size - printed,
1542 "For your workloads it needs to be <= 1\nHint:\t");
1544 printed += scnprintf(buf + printed, size - printed,
1545 "For system wide tracing it needs to be set to -1.\n");
1547 printed += scnprintf(buf + printed, size - printed,
1548 "Hint:\tTry: 'sudo sh -c \"echo -1 > /proc/sys/kernel/perf_event_paranoid\"'\n"
1549 "Hint:\tThe current value is %d.", value);
1552 struct evsel *first = evlist__first(evlist);
1555 if (sysctl__read_int("kernel/perf_event_max_sample_rate", &max_freq) < 0)
1558 if (first->core.attr.sample_freq < (u64)max_freq)
1561 printed = scnprintf(buf, size,
1563 "Hint:\tCheck /proc/sys/kernel/perf_event_max_sample_rate.\n"
1564 "Hint:\tThe current value is %d and %" PRIu64 " is being requested.",
1565 emsg, max_freq, first->core.attr.sample_freq);
1570 scnprintf(buf, size, "%s", emsg);
1577 int evlist__strerror_mmap(struct evlist *evlist, int err, char *buf, size_t size)
1579 char sbuf[STRERR_BUFSIZE], *emsg = str_error_r(err, sbuf, sizeof(sbuf));
1580 int pages_attempted = evlist->core.mmap_len / 1024, pages_max_per_user, printed = 0;
1584 sysctl__read_int("kernel/perf_event_mlock_kb", &pages_max_per_user);
1585 printed += scnprintf(buf + printed, size - printed,
1587 "Hint:\tCheck /proc/sys/kernel/perf_event_mlock_kb (%d kB) setting.\n"
1588 "Hint:\tTried using %zd kB.\n",
1589 emsg, pages_max_per_user, pages_attempted);
1591 if (pages_attempted >= pages_max_per_user) {
1592 printed += scnprintf(buf + printed, size - printed,
1593 "Hint:\tTry 'sudo sh -c \"echo %d > /proc/sys/kernel/perf_event_mlock_kb\"', or\n",
1594 pages_max_per_user + pages_attempted);
1597 printed += scnprintf(buf + printed, size - printed,
1598 "Hint:\tTry using a smaller -m/--mmap-pages value.");
1601 scnprintf(buf, size, "%s", emsg);
1608 void evlist__to_front(struct evlist *evlist, struct evsel *move_evsel)
1610 struct evsel *evsel, *n;
1613 if (move_evsel == evlist__first(evlist))
1616 evlist__for_each_entry_safe(evlist, n, evsel) {
1617 if (evsel->leader == move_evsel->leader)
1618 list_move_tail(&evsel->core.node, &move);
1621 list_splice(&move, &evlist->core.entries);
1624 struct evsel *evlist__get_tracking_event(struct evlist *evlist)
1626 struct evsel *evsel;
1628 evlist__for_each_entry(evlist, evsel) {
1629 if (evsel->tracking)
1633 return evlist__first(evlist);
1636 void evlist__set_tracking_event(struct evlist *evlist, struct evsel *tracking_evsel)
1638 struct evsel *evsel;
1640 if (tracking_evsel->tracking)
1643 evlist__for_each_entry(evlist, evsel) {
1644 if (evsel != tracking_evsel)
1645 evsel->tracking = false;
1648 tracking_evsel->tracking = true;
1651 struct evsel *evlist__find_evsel_by_str(struct evlist *evlist, const char *str)
1653 struct evsel *evsel;
1655 evlist__for_each_entry(evlist, evsel) {
1658 if (strcmp(str, evsel->name) == 0)
1665 void evlist__toggle_bkw_mmap(struct evlist *evlist, enum bkw_mmap_state state)
1667 enum bkw_mmap_state old_state = evlist->bkw_mmap_state;
1674 if (!evlist->overwrite_mmap)
1677 switch (old_state) {
1678 case BKW_MMAP_NOTREADY: {
1679 if (state != BKW_MMAP_RUNNING)
1683 case BKW_MMAP_RUNNING: {
1684 if (state != BKW_MMAP_DATA_PENDING)
1689 case BKW_MMAP_DATA_PENDING: {
1690 if (state != BKW_MMAP_EMPTY)
1694 case BKW_MMAP_EMPTY: {
1695 if (state != BKW_MMAP_RUNNING)
1701 WARN_ONCE(1, "Shouldn't get there\n");
1704 evlist->bkw_mmap_state = state;
1708 evlist__pause(evlist);
1711 evlist__resume(evlist);
1722 bool evlist__exclude_kernel(struct evlist *evlist)
1724 struct evsel *evsel;
1726 evlist__for_each_entry(evlist, evsel) {
1727 if (!evsel->core.attr.exclude_kernel)
1735 * Events in data file are not collect in groups, but we still want
1736 * the group display. Set the artificial group and set the leader's
1737 * forced_leader flag to notify the display code.
1739 void evlist__force_leader(struct evlist *evlist)
1741 if (!evlist->nr_groups) {
1742 struct evsel *leader = evlist__first(evlist);
1744 evlist__set_leader(evlist);
1745 leader->forced_leader = true;
1749 struct evsel *evlist__reset_weak_group(struct evlist *evsel_list, struct evsel *evsel, bool close)
1751 struct evsel *c2, *leader;
1752 bool is_open = true;
1754 leader = evsel->leader;
1755 pr_debug("Weak group for %s/%d failed\n",
1756 leader->name, leader->core.nr_members);
1759 * for_each_group_member doesn't work here because it doesn't
1760 * include the first entry.
1762 evlist__for_each_entry(evsel_list, c2) {
1765 if (c2->leader == leader) {
1766 if (is_open && close)
1767 perf_evsel__close(&c2->core);
1769 c2->core.nr_members = 0;
1771 * Set this for all former members of the group
1772 * to indicate they get reopened.
1774 c2->reset_group = true;
1780 static int evlist__parse_control_fifo(const char *str, int *ctl_fd, int *ctl_fd_ack, bool *ctl_fd_close)
1785 if (strncmp(str, "fifo:", 5))
1789 if (!*str || *str == ',')
1801 * O_RDWR avoids POLLHUPs which is necessary to allow the other
1802 * end of a FIFO to be repeatedly opened and closed.
1804 fd = open(s, O_RDWR | O_NONBLOCK | O_CLOEXEC);
1806 pr_err("Failed to open '%s'\n", s);
1811 *ctl_fd_close = true;
1814 /* O_RDWR | O_NONBLOCK means the other end need not be open */
1815 fd = open(p, O_RDWR | O_NONBLOCK | O_CLOEXEC);
1817 pr_err("Failed to open '%s'\n", p);
1829 int evlist__parse_control(const char *str, int *ctl_fd, int *ctl_fd_ack, bool *ctl_fd_close)
1831 char *comma = NULL, *endptr = NULL;
1833 *ctl_fd_close = false;
1835 if (strncmp(str, "fd:", 3))
1836 return evlist__parse_control_fifo(str, ctl_fd, ctl_fd_ack, ctl_fd_close);
1838 *ctl_fd = strtoul(&str[3], &endptr, 0);
1839 if (endptr == &str[3])
1842 comma = strchr(str, ',');
1844 if (endptr != comma)
1847 *ctl_fd_ack = strtoul(comma + 1, &endptr, 0);
1848 if (endptr == comma + 1 || *endptr != '\0')
1855 void evlist__close_control(int ctl_fd, int ctl_fd_ack, bool *ctl_fd_close)
1857 if (*ctl_fd_close) {
1858 *ctl_fd_close = false;
1860 if (ctl_fd_ack >= 0)
1865 int evlist__initialize_ctlfd(struct evlist *evlist, int fd, int ack)
1868 pr_debug("Control descriptor is not initialized\n");
1872 evlist->ctl_fd.pos = perf_evlist__add_pollfd(&evlist->core, fd, NULL, POLLIN,
1873 fdarray_flag__nonfilterable);
1874 if (evlist->ctl_fd.pos < 0) {
1875 evlist->ctl_fd.pos = -1;
1876 pr_err("Failed to add ctl fd entry: %m\n");
1880 evlist->ctl_fd.fd = fd;
1881 evlist->ctl_fd.ack = ack;
1886 bool evlist__ctlfd_initialized(struct evlist *evlist)
1888 return evlist->ctl_fd.pos >= 0;
1891 int evlist__finalize_ctlfd(struct evlist *evlist)
1893 struct pollfd *entries = evlist->core.pollfd.entries;
1895 if (!evlist__ctlfd_initialized(evlist))
1898 entries[evlist->ctl_fd.pos].fd = -1;
1899 entries[evlist->ctl_fd.pos].events = 0;
1900 entries[evlist->ctl_fd.pos].revents = 0;
1902 evlist->ctl_fd.pos = -1;
1903 evlist->ctl_fd.ack = -1;
1904 evlist->ctl_fd.fd = -1;
1909 static int evlist__ctlfd_recv(struct evlist *evlist, enum evlist_ctl_cmd *cmd,
1910 char *cmd_data, size_t data_size)
1914 size_t bytes_read = 0;
1916 *cmd = EVLIST_CTL_CMD_UNSUPPORTED;
1917 memset(cmd_data, 0, data_size);
1921 err = read(evlist->ctl_fd.fd, &c, 1);
1923 if (c == '\n' || c == '\0')
1925 cmd_data[bytes_read++] = c;
1926 if (bytes_read == data_size)
1929 } else if (err == -1) {
1932 if (errno == EAGAIN || errno == EWOULDBLOCK)
1935 pr_err("Failed to read from ctlfd %d: %m\n", evlist->ctl_fd.fd);
1940 pr_debug("Message from ctl_fd: \"%s%s\"\n", cmd_data,
1941 bytes_read == data_size ? "" : c == '\n' ? "\\n" : "\\0");
1943 if (bytes_read > 0) {
1944 if (!strncmp(cmd_data, EVLIST_CTL_CMD_ENABLE_TAG,
1945 (sizeof(EVLIST_CTL_CMD_ENABLE_TAG)-1))) {
1946 *cmd = EVLIST_CTL_CMD_ENABLE;
1947 } else if (!strncmp(cmd_data, EVLIST_CTL_CMD_DISABLE_TAG,
1948 (sizeof(EVLIST_CTL_CMD_DISABLE_TAG)-1))) {
1949 *cmd = EVLIST_CTL_CMD_DISABLE;
1950 } else if (!strncmp(cmd_data, EVLIST_CTL_CMD_SNAPSHOT_TAG,
1951 (sizeof(EVLIST_CTL_CMD_SNAPSHOT_TAG)-1))) {
1952 *cmd = EVLIST_CTL_CMD_SNAPSHOT;
1953 pr_debug("is snapshot\n");
1954 } else if (!strncmp(cmd_data, EVLIST_CTL_CMD_EVLIST_TAG,
1955 (sizeof(EVLIST_CTL_CMD_EVLIST_TAG)-1))) {
1956 *cmd = EVLIST_CTL_CMD_EVLIST;
1957 } else if (!strncmp(cmd_data, EVLIST_CTL_CMD_STOP_TAG,
1958 (sizeof(EVLIST_CTL_CMD_STOP_TAG)-1))) {
1959 *cmd = EVLIST_CTL_CMD_STOP;
1960 } else if (!strncmp(cmd_data, EVLIST_CTL_CMD_PING_TAG,
1961 (sizeof(EVLIST_CTL_CMD_PING_TAG)-1))) {
1962 *cmd = EVLIST_CTL_CMD_PING;
1966 return bytes_read ? (int)bytes_read : err;
1969 int evlist__ctlfd_ack(struct evlist *evlist)
1973 if (evlist->ctl_fd.ack == -1)
1976 err = write(evlist->ctl_fd.ack, EVLIST_CTL_CMD_ACK_TAG,
1977 sizeof(EVLIST_CTL_CMD_ACK_TAG));
1979 pr_err("failed to write to ctl_ack_fd %d: %m\n", evlist->ctl_fd.ack);
1984 static int get_cmd_arg(char *cmd_data, size_t cmd_size, char **arg)
1986 char *data = cmd_data + cmd_size;
1992 /* there's argument */
2002 static int evlist__ctlfd_enable(struct evlist *evlist, char *cmd_data, bool enable)
2004 struct evsel *evsel;
2008 err = get_cmd_arg(cmd_data,
2009 enable ? sizeof(EVLIST_CTL_CMD_ENABLE_TAG) - 1 :
2010 sizeof(EVLIST_CTL_CMD_DISABLE_TAG) - 1,
2013 pr_info("failed: wrong command\n");
2018 evsel = evlist__find_evsel_by_str(evlist, name);
2021 evlist__enable_evsel(evlist, name);
2023 evlist__disable_evsel(evlist, name);
2024 pr_info("Event %s %s\n", evsel->name,
2025 enable ? "enabled" : "disabled");
2027 pr_info("failed: can't find '%s' event\n", name);
2031 evlist__enable(evlist);
2032 pr_info(EVLIST_ENABLED_MSG);
2034 evlist__disable(evlist);
2035 pr_info(EVLIST_DISABLED_MSG);
2042 static int evlist__ctlfd_list(struct evlist *evlist, char *cmd_data)
2044 struct perf_attr_details details = { .verbose = false, };
2045 struct evsel *evsel;
2049 err = get_cmd_arg(cmd_data,
2050 sizeof(EVLIST_CTL_CMD_EVLIST_TAG) - 1,
2053 pr_info("failed: wrong command\n");
2058 if (!strcmp(arg, "-v")) {
2059 details.verbose = true;
2060 } else if (!strcmp(arg, "-g")) {
2061 details.event_group = true;
2062 } else if (!strcmp(arg, "-F")) {
2063 details.freq = true;
2065 pr_info("failed: wrong command\n");
2070 evlist__for_each_entry(evlist, evsel)
2071 evsel__fprintf(evsel, &details, stderr);
2076 int evlist__ctlfd_process(struct evlist *evlist, enum evlist_ctl_cmd *cmd)
2079 char cmd_data[EVLIST_CTL_CMD_MAX_LEN];
2080 int ctlfd_pos = evlist->ctl_fd.pos;
2081 struct pollfd *entries = evlist->core.pollfd.entries;
2083 if (!evlist__ctlfd_initialized(evlist) || !entries[ctlfd_pos].revents)
2086 if (entries[ctlfd_pos].revents & POLLIN) {
2087 err = evlist__ctlfd_recv(evlist, cmd, cmd_data,
2088 EVLIST_CTL_CMD_MAX_LEN);
2091 case EVLIST_CTL_CMD_ENABLE:
2092 case EVLIST_CTL_CMD_DISABLE:
2093 err = evlist__ctlfd_enable(evlist, cmd_data,
2094 *cmd == EVLIST_CTL_CMD_ENABLE);
2096 case EVLIST_CTL_CMD_EVLIST:
2097 err = evlist__ctlfd_list(evlist, cmd_data);
2099 case EVLIST_CTL_CMD_SNAPSHOT:
2100 case EVLIST_CTL_CMD_STOP:
2101 case EVLIST_CTL_CMD_PING:
2103 case EVLIST_CTL_CMD_ACK:
2104 case EVLIST_CTL_CMD_UNSUPPORTED:
2106 pr_debug("ctlfd: unsupported %d\n", *cmd);
2109 if (!(*cmd == EVLIST_CTL_CMD_ACK || *cmd == EVLIST_CTL_CMD_UNSUPPORTED ||
2110 *cmd == EVLIST_CTL_CMD_SNAPSHOT))
2111 evlist__ctlfd_ack(evlist);
2115 if (entries[ctlfd_pos].revents & (POLLHUP | POLLERR))
2116 evlist__finalize_ctlfd(evlist);
2118 entries[ctlfd_pos].revents = 0;
2123 struct evsel *evlist__find_evsel(struct evlist *evlist, int idx)
2125 struct evsel *evsel;
2127 evlist__for_each_entry(evlist, evsel) {
2128 if (evsel->idx == idx)