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>
39 #include <sys/prctl.h>
41 #include <linux/bitops.h>
42 #include <linux/hash.h>
43 #include <linux/log2.h>
44 #include <linux/err.h>
45 #include <linux/string.h>
46 #include <linux/zalloc.h>
47 #include <perf/evlist.h>
48 #include <perf/evsel.h>
49 #include <perf/cpumap.h>
50 #include <perf/mmap.h>
52 #include <internal/xyarray.h>
54 #ifdef LACKS_SIGQUEUE_PROTOTYPE
55 int sigqueue(pid_t pid, int sig, const union sigval value);
58 #define FD(e, x, y) (*(int *)xyarray__entry(e->core.fd, x, y))
59 #define SID(e, x, y) xyarray__entry(e->core.sample_id, x, y)
61 void evlist__init(struct evlist *evlist, struct perf_cpu_map *cpus,
62 struct perf_thread_map *threads)
64 perf_evlist__init(&evlist->core);
65 perf_evlist__set_maps(&evlist->core, cpus, threads);
66 evlist->workload.pid = -1;
67 evlist->bkw_mmap_state = BKW_MMAP_NOTREADY;
68 evlist->ctl_fd.fd = -1;
69 evlist->ctl_fd.ack = -1;
70 evlist->ctl_fd.pos = -1;
73 struct evlist *evlist__new(void)
75 struct evlist *evlist = zalloc(sizeof(*evlist));
78 evlist__init(evlist, NULL, NULL);
83 struct evlist *evlist__new_default(void)
85 struct evlist *evlist = evlist__new();
87 if (evlist && evlist__add_default(evlist)) {
88 evlist__delete(evlist);
95 struct evlist *evlist__new_dummy(void)
97 struct evlist *evlist = evlist__new();
99 if (evlist && evlist__add_dummy(evlist)) {
100 evlist__delete(evlist);
108 * evlist__set_id_pos - set the positions of event ids.
109 * @evlist: selected event list
111 * Events with compatible sample types all have the same id_pos
112 * and is_pos. For convenience, put a copy on evlist.
114 void evlist__set_id_pos(struct evlist *evlist)
116 struct evsel *first = evlist__first(evlist);
118 evlist->id_pos = first->id_pos;
119 evlist->is_pos = first->is_pos;
122 static void evlist__update_id_pos(struct evlist *evlist)
126 evlist__for_each_entry(evlist, evsel)
127 evsel__calc_id_pos(evsel);
129 evlist__set_id_pos(evlist);
132 static void evlist__purge(struct evlist *evlist)
134 struct evsel *pos, *n;
136 evlist__for_each_entry_safe(evlist, n, pos) {
137 list_del_init(&pos->core.node);
142 evlist->core.nr_entries = 0;
145 void evlist__exit(struct evlist *evlist)
147 zfree(&evlist->mmap);
148 zfree(&evlist->overwrite_mmap);
149 perf_evlist__exit(&evlist->core);
152 void evlist__delete(struct evlist *evlist)
157 evlist__munmap(evlist);
158 evlist__close(evlist);
159 evlist__purge(evlist);
160 evlist__exit(evlist);
164 void evlist__add(struct evlist *evlist, struct evsel *entry)
166 entry->evlist = evlist;
167 entry->idx = evlist->core.nr_entries;
168 entry->tracking = !entry->idx;
170 perf_evlist__add(&evlist->core, &entry->core);
172 if (evlist->core.nr_entries == 1)
173 evlist__set_id_pos(evlist);
176 void evlist__remove(struct evlist *evlist, struct evsel *evsel)
178 evsel->evlist = NULL;
179 perf_evlist__remove(&evlist->core, &evsel->core);
182 void evlist__splice_list_tail(struct evlist *evlist, struct list_head *list)
184 while (!list_empty(list)) {
185 struct evsel *evsel, *temp, *leader = NULL;
187 __evlist__for_each_entry_safe(list, temp, evsel) {
188 list_del_init(&evsel->core.node);
189 evlist__add(evlist, evsel);
194 __evlist__for_each_entry_safe(list, temp, evsel) {
195 if (evsel->leader == leader) {
196 list_del_init(&evsel->core.node);
197 evlist__add(evlist, evsel);
203 int __evlist__set_tracepoints_handlers(struct evlist *evlist,
204 const struct evsel_str_handler *assocs, size_t nr_assocs)
209 for (i = 0; i < nr_assocs; i++) {
210 // Adding a handler for an event not in this evlist, just ignore it.
211 struct evsel *evsel = evlist__find_tracepoint_by_name(evlist, assocs[i].name);
216 if (evsel->handler != NULL)
218 evsel->handler = assocs[i].handler;
226 void __evlist__set_leader(struct list_head *list)
228 struct evsel *evsel, *leader;
230 leader = list_entry(list->next, struct evsel, core.node);
231 evsel = list_entry(list->prev, struct evsel, core.node);
233 leader->core.nr_members = evsel->idx - leader->idx + 1;
235 __evlist__for_each_entry(list, evsel) {
236 evsel->leader = leader;
240 void evlist__set_leader(struct evlist *evlist)
242 if (evlist->core.nr_entries) {
243 evlist->nr_groups = evlist->core.nr_entries > 1 ? 1 : 0;
244 __evlist__set_leader(&evlist->core.entries);
248 int __evlist__add_default(struct evlist *evlist, bool precise)
250 struct evsel *evsel = evsel__new_cycles(precise);
255 evlist__add(evlist, evsel);
259 int evlist__add_dummy(struct evlist *evlist)
261 struct perf_event_attr attr = {
262 .type = PERF_TYPE_SOFTWARE,
263 .config = PERF_COUNT_SW_DUMMY,
264 .size = sizeof(attr), /* to capture ABI version */
266 struct evsel *evsel = evsel__new_idx(&attr, evlist->core.nr_entries);
271 evlist__add(evlist, evsel);
275 static int evlist__add_attrs(struct evlist *evlist, struct perf_event_attr *attrs, size_t nr_attrs)
277 struct evsel *evsel, *n;
281 for (i = 0; i < nr_attrs; i++) {
282 evsel = evsel__new_idx(attrs + i, evlist->core.nr_entries + i);
284 goto out_delete_partial_list;
285 list_add_tail(&evsel->core.node, &head);
288 evlist__splice_list_tail(evlist, &head);
292 out_delete_partial_list:
293 __evlist__for_each_entry_safe(&head, n, evsel)
294 evsel__delete(evsel);
298 int __evlist__add_default_attrs(struct evlist *evlist, struct perf_event_attr *attrs, size_t nr_attrs)
302 for (i = 0; i < nr_attrs; i++)
303 event_attr_init(attrs + i);
305 return evlist__add_attrs(evlist, attrs, nr_attrs);
308 __weak int arch_evlist__add_default_attrs(struct evlist *evlist __maybe_unused)
313 struct evsel *evlist__find_tracepoint_by_id(struct evlist *evlist, int id)
317 evlist__for_each_entry(evlist, evsel) {
318 if (evsel->core.attr.type == PERF_TYPE_TRACEPOINT &&
319 (int)evsel->core.attr.config == id)
326 struct evsel *evlist__find_tracepoint_by_name(struct evlist *evlist, const char *name)
330 evlist__for_each_entry(evlist, evsel) {
331 if ((evsel->core.attr.type == PERF_TYPE_TRACEPOINT) &&
332 (strcmp(evsel->name, name) == 0))
339 int evlist__add_newtp(struct evlist *evlist, const char *sys, const char *name, void *handler)
341 struct evsel *evsel = evsel__newtp(sys, name);
346 evsel->handler = handler;
347 evlist__add(evlist, evsel);
351 static int evlist__nr_threads(struct evlist *evlist, struct evsel *evsel)
353 if (evsel->core.system_wide)
356 return perf_thread_map__nr(evlist->core.threads);
359 void evlist__cpu_iter_start(struct evlist *evlist)
364 * Reset the per evsel cpu_iter. This is needed because
365 * each evsel's cpumap may have a different index space,
366 * and some operations need the index to modify
367 * the FD xyarray (e.g. open, close)
369 evlist__for_each_entry(evlist, pos)
373 bool evsel__cpu_iter_skip_no_inc(struct evsel *ev, int cpu)
375 if (ev->cpu_iter >= ev->core.cpus->nr)
377 if (cpu >= 0 && ev->core.cpus->map[ev->cpu_iter] != cpu)
382 bool evsel__cpu_iter_skip(struct evsel *ev, int cpu)
384 if (!evsel__cpu_iter_skip_no_inc(ev, cpu)) {
391 static int evsel__strcmp(struct evsel *pos, char *evsel_name)
395 if (evsel__is_dummy_event(pos))
397 return strcmp(pos->name, evsel_name);
400 static int evlist__is_enabled(struct evlist *evlist)
404 evlist__for_each_entry(evlist, pos) {
405 if (!evsel__is_group_leader(pos) || !pos->core.fd)
407 /* If at least one event is enabled, evlist is enabled. */
414 static void __evlist__disable(struct evlist *evlist, char *evsel_name)
417 struct affinity affinity;
419 bool has_imm = false;
421 if (affinity__setup(&affinity) < 0)
424 /* Disable 'immediate' events last */
425 for (imm = 0; imm <= 1; imm++) {
426 evlist__for_each_cpu(evlist, i, cpu) {
427 affinity__set(&affinity, cpu);
429 evlist__for_each_entry(evlist, pos) {
430 if (evsel__strcmp(pos, evsel_name))
432 if (evsel__cpu_iter_skip(pos, cpu))
434 if (pos->disabled || !evsel__is_group_leader(pos) || !pos->core.fd)
438 if (pos->immediate != imm)
440 evsel__disable_cpu(pos, pos->cpu_iter - 1);
447 affinity__cleanup(&affinity);
448 evlist__for_each_entry(evlist, pos) {
449 if (evsel__strcmp(pos, evsel_name))
451 if (!evsel__is_group_leader(pos) || !pos->core.fd)
453 pos->disabled = true;
457 * If we disabled only single event, we need to check
458 * the enabled state of the evlist manually.
461 evlist->enabled = evlist__is_enabled(evlist);
463 evlist->enabled = false;
466 void evlist__disable(struct evlist *evlist)
468 __evlist__disable(evlist, NULL);
471 void evlist__disable_evsel(struct evlist *evlist, char *evsel_name)
473 __evlist__disable(evlist, evsel_name);
476 static void __evlist__enable(struct evlist *evlist, char *evsel_name)
479 struct affinity affinity;
482 if (affinity__setup(&affinity) < 0)
485 evlist__for_each_cpu(evlist, i, cpu) {
486 affinity__set(&affinity, cpu);
488 evlist__for_each_entry(evlist, pos) {
489 if (evsel__strcmp(pos, evsel_name))
491 if (evsel__cpu_iter_skip(pos, cpu))
493 if (!evsel__is_group_leader(pos) || !pos->core.fd)
495 evsel__enable_cpu(pos, pos->cpu_iter - 1);
498 affinity__cleanup(&affinity);
499 evlist__for_each_entry(evlist, pos) {
500 if (evsel__strcmp(pos, evsel_name))
502 if (!evsel__is_group_leader(pos) || !pos->core.fd)
504 pos->disabled = false;
508 * Even single event sets the 'enabled' for evlist,
509 * so the toggle can work properly and toggle to
512 evlist->enabled = true;
515 void evlist__enable(struct evlist *evlist)
517 __evlist__enable(evlist, NULL);
520 void evlist__enable_evsel(struct evlist *evlist, char *evsel_name)
522 __evlist__enable(evlist, evsel_name);
525 void evlist__toggle_enable(struct evlist *evlist)
527 (evlist->enabled ? evlist__disable : evlist__enable)(evlist);
530 static int evlist__enable_event_cpu(struct evlist *evlist, struct evsel *evsel, int cpu)
533 int nr_threads = evlist__nr_threads(evlist, evsel);
538 for (thread = 0; thread < nr_threads; thread++) {
539 int err = ioctl(FD(evsel, cpu, thread), PERF_EVENT_IOC_ENABLE, 0);
546 static int evlist__enable_event_thread(struct evlist *evlist, struct evsel *evsel, int thread)
549 int nr_cpus = perf_cpu_map__nr(evlist->core.cpus);
554 for (cpu = 0; cpu < nr_cpus; cpu++) {
555 int err = ioctl(FD(evsel, cpu, thread), PERF_EVENT_IOC_ENABLE, 0);
562 int evlist__enable_event_idx(struct evlist *evlist, struct evsel *evsel, int idx)
564 bool per_cpu_mmaps = !perf_cpu_map__empty(evlist->core.cpus);
567 return evlist__enable_event_cpu(evlist, evsel, idx);
569 return evlist__enable_event_thread(evlist, evsel, idx);
572 int evlist__add_pollfd(struct evlist *evlist, int fd)
574 return perf_evlist__add_pollfd(&evlist->core, fd, NULL, POLLIN, fdarray_flag__default);
577 int evlist__filter_pollfd(struct evlist *evlist, short revents_and_mask)
579 return perf_evlist__filter_pollfd(&evlist->core, revents_and_mask);
582 #ifdef HAVE_EVENTFD_SUPPORT
583 int evlist__add_wakeup_eventfd(struct evlist *evlist, int fd)
585 return perf_evlist__add_pollfd(&evlist->core, fd, NULL, POLLIN,
586 fdarray_flag__nonfilterable);
590 int evlist__poll(struct evlist *evlist, int timeout)
592 return perf_evlist__poll(&evlist->core, timeout);
595 struct perf_sample_id *evlist__id2sid(struct evlist *evlist, u64 id)
597 struct hlist_head *head;
598 struct perf_sample_id *sid;
601 hash = hash_64(id, PERF_EVLIST__HLIST_BITS);
602 head = &evlist->core.heads[hash];
604 hlist_for_each_entry(sid, head, node)
611 struct evsel *evlist__id2evsel(struct evlist *evlist, u64 id)
613 struct perf_sample_id *sid;
615 if (evlist->core.nr_entries == 1 || !id)
616 return evlist__first(evlist);
618 sid = evlist__id2sid(evlist, id);
620 return container_of(sid->evsel, struct evsel, core);
622 if (!evlist__sample_id_all(evlist))
623 return evlist__first(evlist);
628 struct evsel *evlist__id2evsel_strict(struct evlist *evlist, u64 id)
630 struct perf_sample_id *sid;
635 sid = evlist__id2sid(evlist, id);
637 return container_of(sid->evsel, struct evsel, core);
642 static int evlist__event2id(struct evlist *evlist, union perf_event *event, u64 *id)
644 const __u64 *array = event->sample.array;
647 n = (event->header.size - sizeof(event->header)) >> 3;
649 if (event->header.type == PERF_RECORD_SAMPLE) {
650 if (evlist->id_pos >= n)
652 *id = array[evlist->id_pos];
654 if (evlist->is_pos > n)
662 struct evsel *evlist__event2evsel(struct evlist *evlist, union perf_event *event)
664 struct evsel *first = evlist__first(evlist);
665 struct hlist_head *head;
666 struct perf_sample_id *sid;
670 if (evlist->core.nr_entries == 1)
673 if (!first->core.attr.sample_id_all &&
674 event->header.type != PERF_RECORD_SAMPLE)
677 if (evlist__event2id(evlist, event, &id))
680 /* Synthesized events have an id of zero */
684 hash = hash_64(id, PERF_EVLIST__HLIST_BITS);
685 head = &evlist->core.heads[hash];
687 hlist_for_each_entry(sid, head, node) {
689 return container_of(sid->evsel, struct evsel, core);
694 static int evlist__set_paused(struct evlist *evlist, bool value)
698 if (!evlist->overwrite_mmap)
701 for (i = 0; i < evlist->core.nr_mmaps; i++) {
702 int fd = evlist->overwrite_mmap[i].core.fd;
707 err = ioctl(fd, PERF_EVENT_IOC_PAUSE_OUTPUT, value ? 1 : 0);
714 static int evlist__pause(struct evlist *evlist)
716 return evlist__set_paused(evlist, true);
719 static int evlist__resume(struct evlist *evlist)
721 return evlist__set_paused(evlist, false);
724 static void evlist__munmap_nofree(struct evlist *evlist)
729 for (i = 0; i < evlist->core.nr_mmaps; i++)
730 perf_mmap__munmap(&evlist->mmap[i].core);
732 if (evlist->overwrite_mmap)
733 for (i = 0; i < evlist->core.nr_mmaps; i++)
734 perf_mmap__munmap(&evlist->overwrite_mmap[i].core);
737 void evlist__munmap(struct evlist *evlist)
739 evlist__munmap_nofree(evlist);
740 zfree(&evlist->mmap);
741 zfree(&evlist->overwrite_mmap);
744 static void perf_mmap__unmap_cb(struct perf_mmap *map)
746 struct mmap *m = container_of(map, struct mmap, core);
751 static struct mmap *evlist__alloc_mmap(struct evlist *evlist,
757 map = zalloc(evlist->core.nr_mmaps * sizeof(struct mmap));
761 for (i = 0; i < evlist->core.nr_mmaps; i++) {
762 struct perf_mmap *prev = i ? &map[i - 1].core : NULL;
765 * When the perf_mmap() call is made we grab one refcount, plus
766 * one extra to let perf_mmap__consume() get the last
767 * events after all real references (perf_mmap__get()) are
770 * Each PERF_EVENT_IOC_SET_OUTPUT points to this mmap and
771 * thus does perf_mmap__get() on it.
773 perf_mmap__init(&map[i].core, prev, overwrite, perf_mmap__unmap_cb);
780 perf_evlist__mmap_cb_idx(struct perf_evlist *_evlist,
781 struct perf_mmap_param *_mp,
782 int idx, bool per_cpu)
784 struct evlist *evlist = container_of(_evlist, struct evlist, core);
785 struct mmap_params *mp = container_of(_mp, struct mmap_params, core);
787 auxtrace_mmap_params__set_idx(&mp->auxtrace_mp, evlist, idx, per_cpu);
790 static struct perf_mmap*
791 perf_evlist__mmap_cb_get(struct perf_evlist *_evlist, bool overwrite, int idx)
793 struct evlist *evlist = container_of(_evlist, struct evlist, core);
796 maps = overwrite ? evlist->overwrite_mmap : evlist->mmap;
799 maps = evlist__alloc_mmap(evlist, overwrite);
804 evlist->overwrite_mmap = maps;
805 if (evlist->bkw_mmap_state == BKW_MMAP_NOTREADY)
806 evlist__toggle_bkw_mmap(evlist, BKW_MMAP_RUNNING);
812 return &maps[idx].core;
816 perf_evlist__mmap_cb_mmap(struct perf_mmap *_map, struct perf_mmap_param *_mp,
819 struct mmap *map = container_of(_map, struct mmap, core);
820 struct mmap_params *mp = container_of(_mp, struct mmap_params, core);
822 return mmap__mmap(map, mp, output, cpu);
825 unsigned long perf_event_mlock_kb_in_pages(void)
830 if (sysctl__read_int("kernel/perf_event_mlock_kb", &max) < 0) {
832 * Pick a once upon a time good value, i.e. things look
833 * strange since we can't read a sysctl value, but lets not
838 max -= (page_size / 1024);
841 pages = (max * 1024) / page_size;
842 if (!is_power_of_2(pages))
843 pages = rounddown_pow_of_two(pages);
848 size_t evlist__mmap_size(unsigned long pages)
850 if (pages == UINT_MAX)
851 pages = perf_event_mlock_kb_in_pages();
852 else if (!is_power_of_2(pages))
855 return (pages + 1) * page_size;
858 static long parse_pages_arg(const char *str, unsigned long min,
861 unsigned long pages, val;
862 static struct parse_tag tags[] = {
863 { .tag = 'B', .mult = 1 },
864 { .tag = 'K', .mult = 1 << 10 },
865 { .tag = 'M', .mult = 1 << 20 },
866 { .tag = 'G', .mult = 1 << 30 },
873 val = parse_tag_value(str, tags);
874 if (val != (unsigned long) -1) {
875 /* we got file size value */
876 pages = PERF_ALIGN(val, page_size) / page_size;
878 /* we got pages count value */
880 pages = strtoul(str, &eptr, 10);
885 if (pages == 0 && min == 0) {
886 /* leave number of pages at 0 */
887 } else if (!is_power_of_2(pages)) {
890 /* round pages up to next power of 2 */
891 pages = roundup_pow_of_two(pages);
895 unit_number__scnprintf(buf, sizeof(buf), pages * page_size);
896 pr_info("rounding mmap pages size to %s (%lu pages)\n",
906 int __evlist__parse_mmap_pages(unsigned int *mmap_pages, const char *str)
908 unsigned long max = UINT_MAX;
911 if (max > SIZE_MAX / page_size)
912 max = SIZE_MAX / page_size;
914 pages = parse_pages_arg(str, 1, max);
916 pr_err("Invalid argument for --mmap_pages/-m\n");
924 int evlist__parse_mmap_pages(const struct option *opt, const char *str, int unset __maybe_unused)
926 return __evlist__parse_mmap_pages(opt->value, str);
930 * evlist__mmap_ex - Create mmaps to receive events.
931 * @evlist: list of events
932 * @pages: map length in pages
933 * @overwrite: overwrite older events?
934 * @auxtrace_pages - auxtrace map length in pages
935 * @auxtrace_overwrite - overwrite older auxtrace data?
937 * If @overwrite is %false the user needs to signal event consumption using
938 * perf_mmap__write_tail(). Using evlist__mmap_read() does this
941 * Similarly, if @auxtrace_overwrite is %false the user needs to signal data
942 * consumption using auxtrace_mmap__write_tail().
944 * Return: %0 on success, negative error code otherwise.
946 int evlist__mmap_ex(struct evlist *evlist, unsigned int pages,
947 unsigned int auxtrace_pages,
948 bool auxtrace_overwrite, int nr_cblocks, int affinity, int flush,
952 * Delay setting mp.prot: set it before calling perf_mmap__mmap.
953 * Its value is decided by evsel's write_backward.
954 * So &mp should not be passed through const pointer.
956 struct mmap_params mp = {
957 .nr_cblocks = nr_cblocks,
958 .affinity = affinity,
960 .comp_level = comp_level
962 struct perf_evlist_mmap_ops ops = {
963 .idx = perf_evlist__mmap_cb_idx,
964 .get = perf_evlist__mmap_cb_get,
965 .mmap = perf_evlist__mmap_cb_mmap,
968 evlist->core.mmap_len = evlist__mmap_size(pages);
969 pr_debug("mmap size %zuB\n", evlist->core.mmap_len);
971 auxtrace_mmap_params__init(&mp.auxtrace_mp, evlist->core.mmap_len,
972 auxtrace_pages, auxtrace_overwrite);
974 return perf_evlist__mmap_ops(&evlist->core, &ops, &mp.core);
977 int evlist__mmap(struct evlist *evlist, unsigned int pages)
979 return evlist__mmap_ex(evlist, pages, 0, false, 0, PERF_AFFINITY_SYS, 1, 0);
982 int evlist__create_maps(struct evlist *evlist, struct target *target)
984 bool all_threads = (target->per_thread && target->system_wide);
985 struct perf_cpu_map *cpus;
986 struct perf_thread_map *threads;
989 * If specify '-a' and '--per-thread' to perf record, perf record
990 * will override '--per-thread'. target->per_thread = false and
991 * target->system_wide = true.
993 * If specify '--per-thread' only to perf record,
994 * target->per_thread = true and target->system_wide = false.
996 * So target->per_thread && target->system_wide is false.
997 * For perf record, thread_map__new_str doesn't call
998 * thread_map__new_all_cpus. That will keep perf record's
1001 * For perf stat, it allows the case that target->per_thread and
1002 * target->system_wide are all true. It means to collect system-wide
1003 * per-thread data. thread_map__new_str will call
1004 * thread_map__new_all_cpus to enumerate all threads.
1006 threads = thread_map__new_str(target->pid, target->tid, target->uid,
1012 if (target__uses_dummy_map(target))
1013 cpus = perf_cpu_map__dummy_new();
1015 cpus = perf_cpu_map__new(target->cpu_list);
1018 goto out_delete_threads;
1020 evlist->core.has_user_cpus = !!target->cpu_list;
1022 perf_evlist__set_maps(&evlist->core, cpus, threads);
1024 /* as evlist now has references, put count here */
1025 perf_cpu_map__put(cpus);
1026 perf_thread_map__put(threads);
1031 perf_thread_map__put(threads);
1035 int evlist__apply_filters(struct evlist *evlist, struct evsel **err_evsel)
1037 struct evsel *evsel;
1040 evlist__for_each_entry(evlist, evsel) {
1041 if (evsel->filter == NULL)
1045 * filters only work for tracepoint event, which doesn't have cpu limit.
1046 * So evlist and evsel should always be same.
1048 err = perf_evsel__apply_filter(&evsel->core, evsel->filter);
1058 int evlist__set_tp_filter(struct evlist *evlist, const char *filter)
1060 struct evsel *evsel;
1066 evlist__for_each_entry(evlist, evsel) {
1067 if (evsel->core.attr.type != PERF_TYPE_TRACEPOINT)
1070 err = evsel__set_filter(evsel, filter);
1078 int evlist__append_tp_filter(struct evlist *evlist, const char *filter)
1080 struct evsel *evsel;
1086 evlist__for_each_entry(evlist, evsel) {
1087 if (evsel->core.attr.type != PERF_TYPE_TRACEPOINT)
1090 err = evsel__append_tp_filter(evsel, filter);
1098 char *asprintf__tp_filter_pids(size_t npids, pid_t *pids)
1103 for (i = 0; i < npids; ++i) {
1105 if (asprintf(&filter, "common_pid != %d", pids[i]) < 0)
1110 if (asprintf(&tmp, "%s && common_pid != %d", filter, pids[i]) < 0)
1124 int evlist__set_tp_filter_pids(struct evlist *evlist, size_t npids, pid_t *pids)
1126 char *filter = asprintf__tp_filter_pids(npids, pids);
1127 int ret = evlist__set_tp_filter(evlist, filter);
1133 int evlist__set_tp_filter_pid(struct evlist *evlist, pid_t pid)
1135 return evlist__set_tp_filter_pids(evlist, 1, &pid);
1138 int evlist__append_tp_filter_pids(struct evlist *evlist, size_t npids, pid_t *pids)
1140 char *filter = asprintf__tp_filter_pids(npids, pids);
1141 int ret = evlist__append_tp_filter(evlist, filter);
1147 int evlist__append_tp_filter_pid(struct evlist *evlist, pid_t pid)
1149 return evlist__append_tp_filter_pids(evlist, 1, &pid);
1152 bool evlist__valid_sample_type(struct evlist *evlist)
1156 if (evlist->core.nr_entries == 1)
1159 if (evlist->id_pos < 0 || evlist->is_pos < 0)
1162 evlist__for_each_entry(evlist, pos) {
1163 if (pos->id_pos != evlist->id_pos ||
1164 pos->is_pos != evlist->is_pos)
1171 u64 __evlist__combined_sample_type(struct evlist *evlist)
1173 struct evsel *evsel;
1175 if (evlist->combined_sample_type)
1176 return evlist->combined_sample_type;
1178 evlist__for_each_entry(evlist, evsel)
1179 evlist->combined_sample_type |= evsel->core.attr.sample_type;
1181 return evlist->combined_sample_type;
1184 u64 evlist__combined_sample_type(struct evlist *evlist)
1186 evlist->combined_sample_type = 0;
1187 return __evlist__combined_sample_type(evlist);
1190 u64 evlist__combined_branch_type(struct evlist *evlist)
1192 struct evsel *evsel;
1193 u64 branch_type = 0;
1195 evlist__for_each_entry(evlist, evsel)
1196 branch_type |= evsel->core.attr.branch_sample_type;
1200 bool evlist__valid_read_format(struct evlist *evlist)
1202 struct evsel *first = evlist__first(evlist), *pos = first;
1203 u64 read_format = first->core.attr.read_format;
1204 u64 sample_type = first->core.attr.sample_type;
1206 evlist__for_each_entry(evlist, pos) {
1207 if (read_format != pos->core.attr.read_format) {
1208 pr_debug("Read format differs %#" PRIx64 " vs %#" PRIx64 "\n",
1209 read_format, (u64)pos->core.attr.read_format);
1213 /* PERF_SAMPLE_READ imples PERF_FORMAT_ID. */
1214 if ((sample_type & PERF_SAMPLE_READ) &&
1215 !(read_format & PERF_FORMAT_ID)) {
1222 u16 evlist__id_hdr_size(struct evlist *evlist)
1224 struct evsel *first = evlist__first(evlist);
1225 struct perf_sample *data;
1229 if (!first->core.attr.sample_id_all)
1232 sample_type = first->core.attr.sample_type;
1234 if (sample_type & PERF_SAMPLE_TID)
1235 size += sizeof(data->tid) * 2;
1237 if (sample_type & PERF_SAMPLE_TIME)
1238 size += sizeof(data->time);
1240 if (sample_type & PERF_SAMPLE_ID)
1241 size += sizeof(data->id);
1243 if (sample_type & PERF_SAMPLE_STREAM_ID)
1244 size += sizeof(data->stream_id);
1246 if (sample_type & PERF_SAMPLE_CPU)
1247 size += sizeof(data->cpu) * 2;
1249 if (sample_type & PERF_SAMPLE_IDENTIFIER)
1250 size += sizeof(data->id);
1255 bool evlist__valid_sample_id_all(struct evlist *evlist)
1257 struct evsel *first = evlist__first(evlist), *pos = first;
1259 evlist__for_each_entry_continue(evlist, pos) {
1260 if (first->core.attr.sample_id_all != pos->core.attr.sample_id_all)
1267 bool evlist__sample_id_all(struct evlist *evlist)
1269 struct evsel *first = evlist__first(evlist);
1270 return first->core.attr.sample_id_all;
1273 void evlist__set_selected(struct evlist *evlist, struct evsel *evsel)
1275 evlist->selected = evsel;
1278 void evlist__close(struct evlist *evlist)
1280 struct evsel *evsel;
1281 struct affinity affinity;
1285 * With perf record core.cpus is usually NULL.
1286 * Use the old method to handle this for now.
1288 if (!evlist->core.cpus) {
1289 evlist__for_each_entry_reverse(evlist, evsel)
1290 evsel__close(evsel);
1294 if (affinity__setup(&affinity) < 0)
1296 evlist__for_each_cpu(evlist, i, cpu) {
1297 affinity__set(&affinity, cpu);
1299 evlist__for_each_entry_reverse(evlist, evsel) {
1300 if (evsel__cpu_iter_skip(evsel, cpu))
1302 perf_evsel__close_cpu(&evsel->core, evsel->cpu_iter - 1);
1305 affinity__cleanup(&affinity);
1306 evlist__for_each_entry_reverse(evlist, evsel) {
1307 perf_evsel__free_fd(&evsel->core);
1308 perf_evsel__free_id(&evsel->core);
1310 perf_evlist__reset_id_hash(&evlist->core);
1313 static int evlist__create_syswide_maps(struct evlist *evlist)
1315 struct perf_cpu_map *cpus;
1316 struct perf_thread_map *threads;
1320 * Try reading /sys/devices/system/cpu/online to get
1323 * FIXME: -ENOMEM is the best we can do here, the cpu_map
1324 * code needs an overhaul to properly forward the
1325 * error, and we may not want to do that fallback to a
1326 * default cpu identity map :-\
1328 cpus = perf_cpu_map__new(NULL);
1332 threads = perf_thread_map__new_dummy();
1336 perf_evlist__set_maps(&evlist->core, cpus, threads);
1338 perf_thread_map__put(threads);
1340 perf_cpu_map__put(cpus);
1345 int evlist__open(struct evlist *evlist)
1347 struct evsel *evsel;
1351 * Default: one fd per CPU, all threads, aka systemwide
1352 * as sys_perf_event_open(cpu = -1, thread = -1) is EINVAL
1354 if (evlist->core.threads == NULL && evlist->core.cpus == NULL) {
1355 err = evlist__create_syswide_maps(evlist);
1360 evlist__update_id_pos(evlist);
1362 evlist__for_each_entry(evlist, evsel) {
1363 err = evsel__open(evsel, evsel->core.cpus, evsel->core.threads);
1370 evlist__close(evlist);
1375 int evlist__prepare_workload(struct evlist *evlist, struct target *target, const char *argv[],
1376 bool pipe_output, void (*exec_error)(int signo, siginfo_t *info, void *ucontext))
1378 int child_ready_pipe[2], go_pipe[2];
1381 if (pipe(child_ready_pipe) < 0) {
1382 perror("failed to create 'ready' pipe");
1386 if (pipe(go_pipe) < 0) {
1387 perror("failed to create 'go' pipe");
1388 goto out_close_ready_pipe;
1391 evlist->workload.pid = fork();
1392 if (evlist->workload.pid < 0) {
1393 perror("failed to fork");
1394 goto out_close_pipes;
1397 if (!evlist->workload.pid) {
1403 signal(SIGTERM, SIG_DFL);
1405 close(child_ready_pipe[0]);
1407 fcntl(go_pipe[0], F_SETFD, FD_CLOEXEC);
1410 * Change the name of this process not to confuse --exclude-perf users
1411 * that sees 'perf' in the window up to the execvp() and thinks that
1412 * perf samples are not being excluded.
1414 prctl(PR_SET_NAME, "perf-exec");
1417 * Tell the parent we're ready to go
1419 close(child_ready_pipe[1]);
1422 * Wait until the parent tells us to go.
1424 ret = read(go_pipe[0], &bf, 1);
1426 * The parent will ask for the execvp() to be performed by
1427 * writing exactly one byte, in workload.cork_fd, usually via
1428 * evlist__start_workload().
1430 * For cancelling the workload without actually running it,
1431 * the parent will just close workload.cork_fd, without writing
1432 * anything, i.e. read will return zero and we just exit()
1437 perror("unable to read pipe");
1441 execvp(argv[0], (char **)argv);
1446 val.sival_int = errno;
1447 if (sigqueue(getppid(), SIGUSR1, val))
1455 struct sigaction act = {
1456 .sa_flags = SA_SIGINFO,
1457 .sa_sigaction = exec_error,
1459 sigaction(SIGUSR1, &act, NULL);
1462 if (target__none(target)) {
1463 if (evlist->core.threads == NULL) {
1464 fprintf(stderr, "FATAL: evlist->threads need to be set at this point (%s:%d).\n",
1465 __func__, __LINE__);
1466 goto out_close_pipes;
1468 perf_thread_map__set_pid(evlist->core.threads, 0, evlist->workload.pid);
1471 close(child_ready_pipe[1]);
1474 * wait for child to settle
1476 if (read(child_ready_pipe[0], &bf, 1) == -1) {
1477 perror("unable to read pipe");
1478 goto out_close_pipes;
1481 fcntl(go_pipe[1], F_SETFD, FD_CLOEXEC);
1482 evlist->workload.cork_fd = go_pipe[1];
1483 close(child_ready_pipe[0]);
1489 out_close_ready_pipe:
1490 close(child_ready_pipe[0]);
1491 close(child_ready_pipe[1]);
1495 int evlist__start_workload(struct evlist *evlist)
1497 if (evlist->workload.cork_fd > 0) {
1501 * Remove the cork, let it rip!
1503 ret = write(evlist->workload.cork_fd, &bf, 1);
1505 perror("unable to write to pipe");
1507 close(evlist->workload.cork_fd);
1514 int evlist__parse_sample(struct evlist *evlist, union perf_event *event, struct perf_sample *sample)
1516 struct evsel *evsel = evlist__event2evsel(evlist, event);
1520 return evsel__parse_sample(evsel, event, sample);
1523 int evlist__parse_sample_timestamp(struct evlist *evlist, union perf_event *event, u64 *timestamp)
1525 struct evsel *evsel = evlist__event2evsel(evlist, event);
1529 return evsel__parse_sample_timestamp(evsel, event, timestamp);
1532 int evlist__strerror_open(struct evlist *evlist, int err, char *buf, size_t size)
1535 char sbuf[STRERR_BUFSIZE], *emsg = str_error_r(err, sbuf, sizeof(sbuf));
1540 printed = scnprintf(buf, size,
1542 "Hint:\tCheck /proc/sys/kernel/perf_event_paranoid setting.", emsg);
1544 value = perf_event_paranoid();
1546 printed += scnprintf(buf + printed, size - printed, "\nHint:\t");
1549 printed += scnprintf(buf + printed, size - printed,
1550 "For your workloads it needs to be <= 1\nHint:\t");
1552 printed += scnprintf(buf + printed, size - printed,
1553 "For system wide tracing it needs to be set to -1.\n");
1555 printed += scnprintf(buf + printed, size - printed,
1556 "Hint:\tTry: 'sudo sh -c \"echo -1 > /proc/sys/kernel/perf_event_paranoid\"'\n"
1557 "Hint:\tThe current value is %d.", value);
1560 struct evsel *first = evlist__first(evlist);
1563 if (sysctl__read_int("kernel/perf_event_max_sample_rate", &max_freq) < 0)
1566 if (first->core.attr.sample_freq < (u64)max_freq)
1569 printed = scnprintf(buf, size,
1571 "Hint:\tCheck /proc/sys/kernel/perf_event_max_sample_rate.\n"
1572 "Hint:\tThe current value is %d and %" PRIu64 " is being requested.",
1573 emsg, max_freq, first->core.attr.sample_freq);
1578 scnprintf(buf, size, "%s", emsg);
1585 int evlist__strerror_mmap(struct evlist *evlist, int err, char *buf, size_t size)
1587 char sbuf[STRERR_BUFSIZE], *emsg = str_error_r(err, sbuf, sizeof(sbuf));
1588 int pages_attempted = evlist->core.mmap_len / 1024, pages_max_per_user, printed = 0;
1592 sysctl__read_int("kernel/perf_event_mlock_kb", &pages_max_per_user);
1593 printed += scnprintf(buf + printed, size - printed,
1595 "Hint:\tCheck /proc/sys/kernel/perf_event_mlock_kb (%d kB) setting.\n"
1596 "Hint:\tTried using %zd kB.\n",
1597 emsg, pages_max_per_user, pages_attempted);
1599 if (pages_attempted >= pages_max_per_user) {
1600 printed += scnprintf(buf + printed, size - printed,
1601 "Hint:\tTry 'sudo sh -c \"echo %d > /proc/sys/kernel/perf_event_mlock_kb\"', or\n",
1602 pages_max_per_user + pages_attempted);
1605 printed += scnprintf(buf + printed, size - printed,
1606 "Hint:\tTry using a smaller -m/--mmap-pages value.");
1609 scnprintf(buf, size, "%s", emsg);
1616 void evlist__to_front(struct evlist *evlist, struct evsel *move_evsel)
1618 struct evsel *evsel, *n;
1621 if (move_evsel == evlist__first(evlist))
1624 evlist__for_each_entry_safe(evlist, n, evsel) {
1625 if (evsel->leader == move_evsel->leader)
1626 list_move_tail(&evsel->core.node, &move);
1629 list_splice(&move, &evlist->core.entries);
1632 struct evsel *evlist__get_tracking_event(struct evlist *evlist)
1634 struct evsel *evsel;
1636 evlist__for_each_entry(evlist, evsel) {
1637 if (evsel->tracking)
1641 return evlist__first(evlist);
1644 void evlist__set_tracking_event(struct evlist *evlist, struct evsel *tracking_evsel)
1646 struct evsel *evsel;
1648 if (tracking_evsel->tracking)
1651 evlist__for_each_entry(evlist, evsel) {
1652 if (evsel != tracking_evsel)
1653 evsel->tracking = false;
1656 tracking_evsel->tracking = true;
1659 struct evsel *evlist__find_evsel_by_str(struct evlist *evlist, const char *str)
1661 struct evsel *evsel;
1663 evlist__for_each_entry(evlist, evsel) {
1666 if (strcmp(str, evsel->name) == 0)
1673 void evlist__toggle_bkw_mmap(struct evlist *evlist, enum bkw_mmap_state state)
1675 enum bkw_mmap_state old_state = evlist->bkw_mmap_state;
1682 if (!evlist->overwrite_mmap)
1685 switch (old_state) {
1686 case BKW_MMAP_NOTREADY: {
1687 if (state != BKW_MMAP_RUNNING)
1691 case BKW_MMAP_RUNNING: {
1692 if (state != BKW_MMAP_DATA_PENDING)
1697 case BKW_MMAP_DATA_PENDING: {
1698 if (state != BKW_MMAP_EMPTY)
1702 case BKW_MMAP_EMPTY: {
1703 if (state != BKW_MMAP_RUNNING)
1709 WARN_ONCE(1, "Shouldn't get there\n");
1712 evlist->bkw_mmap_state = state;
1716 evlist__pause(evlist);
1719 evlist__resume(evlist);
1730 bool evlist__exclude_kernel(struct evlist *evlist)
1732 struct evsel *evsel;
1734 evlist__for_each_entry(evlist, evsel) {
1735 if (!evsel->core.attr.exclude_kernel)
1743 * Events in data file are not collect in groups, but we still want
1744 * the group display. Set the artificial group and set the leader's
1745 * forced_leader flag to notify the display code.
1747 void evlist__force_leader(struct evlist *evlist)
1749 if (!evlist->nr_groups) {
1750 struct evsel *leader = evlist__first(evlist);
1752 evlist__set_leader(evlist);
1753 leader->forced_leader = true;
1757 struct evsel *evlist__reset_weak_group(struct evlist *evsel_list, struct evsel *evsel, bool close)
1759 struct evsel *c2, *leader;
1760 bool is_open = true;
1762 leader = evsel->leader;
1763 pr_debug("Weak group for %s/%d failed\n",
1764 leader->name, leader->core.nr_members);
1767 * for_each_group_member doesn't work here because it doesn't
1768 * include the first entry.
1770 evlist__for_each_entry(evsel_list, c2) {
1773 if (c2->leader == leader) {
1774 if (is_open && close)
1775 perf_evsel__close(&c2->core);
1777 c2->core.nr_members = 0;
1779 * Set this for all former members of the group
1780 * to indicate they get reopened.
1782 c2->reset_group = true;
1788 static int evlist__parse_control_fifo(const char *str, int *ctl_fd, int *ctl_fd_ack, bool *ctl_fd_close)
1793 if (strncmp(str, "fifo:", 5))
1797 if (!*str || *str == ',')
1809 * O_RDWR avoids POLLHUPs which is necessary to allow the other
1810 * end of a FIFO to be repeatedly opened and closed.
1812 fd = open(s, O_RDWR | O_NONBLOCK | O_CLOEXEC);
1814 pr_err("Failed to open '%s'\n", s);
1819 *ctl_fd_close = true;
1822 /* O_RDWR | O_NONBLOCK means the other end need not be open */
1823 fd = open(p, O_RDWR | O_NONBLOCK | O_CLOEXEC);
1825 pr_err("Failed to open '%s'\n", p);
1837 int evlist__parse_control(const char *str, int *ctl_fd, int *ctl_fd_ack, bool *ctl_fd_close)
1839 char *comma = NULL, *endptr = NULL;
1841 *ctl_fd_close = false;
1843 if (strncmp(str, "fd:", 3))
1844 return evlist__parse_control_fifo(str, ctl_fd, ctl_fd_ack, ctl_fd_close);
1846 *ctl_fd = strtoul(&str[3], &endptr, 0);
1847 if (endptr == &str[3])
1850 comma = strchr(str, ',');
1852 if (endptr != comma)
1855 *ctl_fd_ack = strtoul(comma + 1, &endptr, 0);
1856 if (endptr == comma + 1 || *endptr != '\0')
1863 void evlist__close_control(int ctl_fd, int ctl_fd_ack, bool *ctl_fd_close)
1865 if (*ctl_fd_close) {
1866 *ctl_fd_close = false;
1868 if (ctl_fd_ack >= 0)
1873 int evlist__initialize_ctlfd(struct evlist *evlist, int fd, int ack)
1876 pr_debug("Control descriptor is not initialized\n");
1880 evlist->ctl_fd.pos = perf_evlist__add_pollfd(&evlist->core, fd, NULL, POLLIN,
1881 fdarray_flag__nonfilterable);
1882 if (evlist->ctl_fd.pos < 0) {
1883 evlist->ctl_fd.pos = -1;
1884 pr_err("Failed to add ctl fd entry: %m\n");
1888 evlist->ctl_fd.fd = fd;
1889 evlist->ctl_fd.ack = ack;
1894 bool evlist__ctlfd_initialized(struct evlist *evlist)
1896 return evlist->ctl_fd.pos >= 0;
1899 int evlist__finalize_ctlfd(struct evlist *evlist)
1901 struct pollfd *entries = evlist->core.pollfd.entries;
1903 if (!evlist__ctlfd_initialized(evlist))
1906 entries[evlist->ctl_fd.pos].fd = -1;
1907 entries[evlist->ctl_fd.pos].events = 0;
1908 entries[evlist->ctl_fd.pos].revents = 0;
1910 evlist->ctl_fd.pos = -1;
1911 evlist->ctl_fd.ack = -1;
1912 evlist->ctl_fd.fd = -1;
1917 static int evlist__ctlfd_recv(struct evlist *evlist, enum evlist_ctl_cmd *cmd,
1918 char *cmd_data, size_t data_size)
1922 size_t bytes_read = 0;
1924 *cmd = EVLIST_CTL_CMD_UNSUPPORTED;
1925 memset(cmd_data, 0, data_size);
1929 err = read(evlist->ctl_fd.fd, &c, 1);
1931 if (c == '\n' || c == '\0')
1933 cmd_data[bytes_read++] = c;
1934 if (bytes_read == data_size)
1937 } else if (err == -1) {
1940 if (errno == EAGAIN || errno == EWOULDBLOCK)
1943 pr_err("Failed to read from ctlfd %d: %m\n", evlist->ctl_fd.fd);
1948 pr_debug("Message from ctl_fd: \"%s%s\"\n", cmd_data,
1949 bytes_read == data_size ? "" : c == '\n' ? "\\n" : "\\0");
1951 if (bytes_read > 0) {
1952 if (!strncmp(cmd_data, EVLIST_CTL_CMD_ENABLE_TAG,
1953 (sizeof(EVLIST_CTL_CMD_ENABLE_TAG)-1))) {
1954 *cmd = EVLIST_CTL_CMD_ENABLE;
1955 } else if (!strncmp(cmd_data, EVLIST_CTL_CMD_DISABLE_TAG,
1956 (sizeof(EVLIST_CTL_CMD_DISABLE_TAG)-1))) {
1957 *cmd = EVLIST_CTL_CMD_DISABLE;
1958 } else if (!strncmp(cmd_data, EVLIST_CTL_CMD_SNAPSHOT_TAG,
1959 (sizeof(EVLIST_CTL_CMD_SNAPSHOT_TAG)-1))) {
1960 *cmd = EVLIST_CTL_CMD_SNAPSHOT;
1961 pr_debug("is snapshot\n");
1962 } else if (!strncmp(cmd_data, EVLIST_CTL_CMD_EVLIST_TAG,
1963 (sizeof(EVLIST_CTL_CMD_EVLIST_TAG)-1))) {
1964 *cmd = EVLIST_CTL_CMD_EVLIST;
1965 } else if (!strncmp(cmd_data, EVLIST_CTL_CMD_STOP_TAG,
1966 (sizeof(EVLIST_CTL_CMD_STOP_TAG)-1))) {
1967 *cmd = EVLIST_CTL_CMD_STOP;
1968 } else if (!strncmp(cmd_data, EVLIST_CTL_CMD_PING_TAG,
1969 (sizeof(EVLIST_CTL_CMD_PING_TAG)-1))) {
1970 *cmd = EVLIST_CTL_CMD_PING;
1974 return bytes_read ? (int)bytes_read : err;
1977 int evlist__ctlfd_ack(struct evlist *evlist)
1981 if (evlist->ctl_fd.ack == -1)
1984 err = write(evlist->ctl_fd.ack, EVLIST_CTL_CMD_ACK_TAG,
1985 sizeof(EVLIST_CTL_CMD_ACK_TAG));
1987 pr_err("failed to write to ctl_ack_fd %d: %m\n", evlist->ctl_fd.ack);
1992 static int get_cmd_arg(char *cmd_data, size_t cmd_size, char **arg)
1994 char *data = cmd_data + cmd_size;
2000 /* there's argument */
2010 static int evlist__ctlfd_enable(struct evlist *evlist, char *cmd_data, bool enable)
2012 struct evsel *evsel;
2016 err = get_cmd_arg(cmd_data,
2017 enable ? sizeof(EVLIST_CTL_CMD_ENABLE_TAG) - 1 :
2018 sizeof(EVLIST_CTL_CMD_DISABLE_TAG) - 1,
2021 pr_info("failed: wrong command\n");
2026 evsel = evlist__find_evsel_by_str(evlist, name);
2029 evlist__enable_evsel(evlist, name);
2031 evlist__disable_evsel(evlist, name);
2032 pr_info("Event %s %s\n", evsel->name,
2033 enable ? "enabled" : "disabled");
2035 pr_info("failed: can't find '%s' event\n", name);
2039 evlist__enable(evlist);
2040 pr_info(EVLIST_ENABLED_MSG);
2042 evlist__disable(evlist);
2043 pr_info(EVLIST_DISABLED_MSG);
2050 static int evlist__ctlfd_list(struct evlist *evlist, char *cmd_data)
2052 struct perf_attr_details details = { .verbose = false, };
2053 struct evsel *evsel;
2057 err = get_cmd_arg(cmd_data,
2058 sizeof(EVLIST_CTL_CMD_EVLIST_TAG) - 1,
2061 pr_info("failed: wrong command\n");
2066 if (!strcmp(arg, "-v")) {
2067 details.verbose = true;
2068 } else if (!strcmp(arg, "-g")) {
2069 details.event_group = true;
2070 } else if (!strcmp(arg, "-F")) {
2071 details.freq = true;
2073 pr_info("failed: wrong command\n");
2078 evlist__for_each_entry(evlist, evsel)
2079 evsel__fprintf(evsel, &details, stderr);
2084 int evlist__ctlfd_process(struct evlist *evlist, enum evlist_ctl_cmd *cmd)
2087 char cmd_data[EVLIST_CTL_CMD_MAX_LEN];
2088 int ctlfd_pos = evlist->ctl_fd.pos;
2089 struct pollfd *entries = evlist->core.pollfd.entries;
2091 if (!evlist__ctlfd_initialized(evlist) || !entries[ctlfd_pos].revents)
2094 if (entries[ctlfd_pos].revents & POLLIN) {
2095 err = evlist__ctlfd_recv(evlist, cmd, cmd_data,
2096 EVLIST_CTL_CMD_MAX_LEN);
2099 case EVLIST_CTL_CMD_ENABLE:
2100 case EVLIST_CTL_CMD_DISABLE:
2101 err = evlist__ctlfd_enable(evlist, cmd_data,
2102 *cmd == EVLIST_CTL_CMD_ENABLE);
2104 case EVLIST_CTL_CMD_EVLIST:
2105 err = evlist__ctlfd_list(evlist, cmd_data);
2107 case EVLIST_CTL_CMD_SNAPSHOT:
2108 case EVLIST_CTL_CMD_STOP:
2109 case EVLIST_CTL_CMD_PING:
2111 case EVLIST_CTL_CMD_ACK:
2112 case EVLIST_CTL_CMD_UNSUPPORTED:
2114 pr_debug("ctlfd: unsupported %d\n", *cmd);
2117 if (!(*cmd == EVLIST_CTL_CMD_ACK || *cmd == EVLIST_CTL_CMD_UNSUPPORTED ||
2118 *cmd == EVLIST_CTL_CMD_SNAPSHOT))
2119 evlist__ctlfd_ack(evlist);
2123 if (entries[ctlfd_pos].revents & (POLLHUP | POLLERR))
2124 evlist__finalize_ctlfd(evlist);
2126 entries[ctlfd_pos].revents = 0;
2131 struct evsel *evlist__find_evsel(struct evlist *evlist, int idx)
2133 struct evsel *evsel;
2135 evlist__for_each_entry(evlist, evsel) {
2136 if (evsel->idx == idx)