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);
1311 static int evlist__create_syswide_maps(struct evlist *evlist)
1313 struct perf_cpu_map *cpus;
1314 struct perf_thread_map *threads;
1318 * Try reading /sys/devices/system/cpu/online to get
1321 * FIXME: -ENOMEM is the best we can do here, the cpu_map
1322 * code needs an overhaul to properly forward the
1323 * error, and we may not want to do that fallback to a
1324 * default cpu identity map :-\
1326 cpus = perf_cpu_map__new(NULL);
1330 threads = perf_thread_map__new_dummy();
1334 perf_evlist__set_maps(&evlist->core, cpus, threads);
1336 perf_thread_map__put(threads);
1338 perf_cpu_map__put(cpus);
1343 int evlist__open(struct evlist *evlist)
1345 struct evsel *evsel;
1349 * Default: one fd per CPU, all threads, aka systemwide
1350 * as sys_perf_event_open(cpu = -1, thread = -1) is EINVAL
1352 if (evlist->core.threads == NULL && evlist->core.cpus == NULL) {
1353 err = evlist__create_syswide_maps(evlist);
1358 evlist__update_id_pos(evlist);
1360 evlist__for_each_entry(evlist, evsel) {
1361 err = evsel__open(evsel, evsel->core.cpus, evsel->core.threads);
1368 evlist__close(evlist);
1373 int evlist__prepare_workload(struct evlist *evlist, struct target *target, const char *argv[],
1374 bool pipe_output, void (*exec_error)(int signo, siginfo_t *info, void *ucontext))
1376 int child_ready_pipe[2], go_pipe[2];
1379 if (pipe(child_ready_pipe) < 0) {
1380 perror("failed to create 'ready' pipe");
1384 if (pipe(go_pipe) < 0) {
1385 perror("failed to create 'go' pipe");
1386 goto out_close_ready_pipe;
1389 evlist->workload.pid = fork();
1390 if (evlist->workload.pid < 0) {
1391 perror("failed to fork");
1392 goto out_close_pipes;
1395 if (!evlist->workload.pid) {
1401 signal(SIGTERM, SIG_DFL);
1403 close(child_ready_pipe[0]);
1405 fcntl(go_pipe[0], F_SETFD, FD_CLOEXEC);
1408 * Tell the parent we're ready to go
1410 close(child_ready_pipe[1]);
1413 * Wait until the parent tells us to go.
1415 ret = read(go_pipe[0], &bf, 1);
1417 * The parent will ask for the execvp() to be performed by
1418 * writing exactly one byte, in workload.cork_fd, usually via
1419 * evlist__start_workload().
1421 * For cancelling the workload without actually running it,
1422 * the parent will just close workload.cork_fd, without writing
1423 * anything, i.e. read will return zero and we just exit()
1428 perror("unable to read pipe");
1432 execvp(argv[0], (char **)argv);
1437 val.sival_int = errno;
1438 if (sigqueue(getppid(), SIGUSR1, val))
1446 struct sigaction act = {
1447 .sa_flags = SA_SIGINFO,
1448 .sa_sigaction = exec_error,
1450 sigaction(SIGUSR1, &act, NULL);
1453 if (target__none(target)) {
1454 if (evlist->core.threads == NULL) {
1455 fprintf(stderr, "FATAL: evlist->threads need to be set at this point (%s:%d).\n",
1456 __func__, __LINE__);
1457 goto out_close_pipes;
1459 perf_thread_map__set_pid(evlist->core.threads, 0, evlist->workload.pid);
1462 close(child_ready_pipe[1]);
1465 * wait for child to settle
1467 if (read(child_ready_pipe[0], &bf, 1) == -1) {
1468 perror("unable to read pipe");
1469 goto out_close_pipes;
1472 fcntl(go_pipe[1], F_SETFD, FD_CLOEXEC);
1473 evlist->workload.cork_fd = go_pipe[1];
1474 close(child_ready_pipe[0]);
1480 out_close_ready_pipe:
1481 close(child_ready_pipe[0]);
1482 close(child_ready_pipe[1]);
1486 int evlist__start_workload(struct evlist *evlist)
1488 if (evlist->workload.cork_fd > 0) {
1492 * Remove the cork, let it rip!
1494 ret = write(evlist->workload.cork_fd, &bf, 1);
1496 perror("unable to write to pipe");
1498 close(evlist->workload.cork_fd);
1505 int evlist__parse_sample(struct evlist *evlist, union perf_event *event, struct perf_sample *sample)
1507 struct evsel *evsel = evlist__event2evsel(evlist, event);
1511 return evsel__parse_sample(evsel, event, sample);
1514 int evlist__parse_sample_timestamp(struct evlist *evlist, union perf_event *event, u64 *timestamp)
1516 struct evsel *evsel = evlist__event2evsel(evlist, event);
1520 return evsel__parse_sample_timestamp(evsel, event, timestamp);
1523 int evlist__strerror_open(struct evlist *evlist, int err, char *buf, size_t size)
1526 char sbuf[STRERR_BUFSIZE], *emsg = str_error_r(err, sbuf, sizeof(sbuf));
1531 printed = scnprintf(buf, size,
1533 "Hint:\tCheck /proc/sys/kernel/perf_event_paranoid setting.", emsg);
1535 value = perf_event_paranoid();
1537 printed += scnprintf(buf + printed, size - printed, "\nHint:\t");
1540 printed += scnprintf(buf + printed, size - printed,
1541 "For your workloads it needs to be <= 1\nHint:\t");
1543 printed += scnprintf(buf + printed, size - printed,
1544 "For system wide tracing it needs to be set to -1.\n");
1546 printed += scnprintf(buf + printed, size - printed,
1547 "Hint:\tTry: 'sudo sh -c \"echo -1 > /proc/sys/kernel/perf_event_paranoid\"'\n"
1548 "Hint:\tThe current value is %d.", value);
1551 struct evsel *first = evlist__first(evlist);
1554 if (sysctl__read_int("kernel/perf_event_max_sample_rate", &max_freq) < 0)
1557 if (first->core.attr.sample_freq < (u64)max_freq)
1560 printed = scnprintf(buf, size,
1562 "Hint:\tCheck /proc/sys/kernel/perf_event_max_sample_rate.\n"
1563 "Hint:\tThe current value is %d and %" PRIu64 " is being requested.",
1564 emsg, max_freq, first->core.attr.sample_freq);
1569 scnprintf(buf, size, "%s", emsg);
1576 int evlist__strerror_mmap(struct evlist *evlist, int err, char *buf, size_t size)
1578 char sbuf[STRERR_BUFSIZE], *emsg = str_error_r(err, sbuf, sizeof(sbuf));
1579 int pages_attempted = evlist->core.mmap_len / 1024, pages_max_per_user, printed = 0;
1583 sysctl__read_int("kernel/perf_event_mlock_kb", &pages_max_per_user);
1584 printed += scnprintf(buf + printed, size - printed,
1586 "Hint:\tCheck /proc/sys/kernel/perf_event_mlock_kb (%d kB) setting.\n"
1587 "Hint:\tTried using %zd kB.\n",
1588 emsg, pages_max_per_user, pages_attempted);
1590 if (pages_attempted >= pages_max_per_user) {
1591 printed += scnprintf(buf + printed, size - printed,
1592 "Hint:\tTry 'sudo sh -c \"echo %d > /proc/sys/kernel/perf_event_mlock_kb\"', or\n",
1593 pages_max_per_user + pages_attempted);
1596 printed += scnprintf(buf + printed, size - printed,
1597 "Hint:\tTry using a smaller -m/--mmap-pages value.");
1600 scnprintf(buf, size, "%s", emsg);
1607 void evlist__to_front(struct evlist *evlist, struct evsel *move_evsel)
1609 struct evsel *evsel, *n;
1612 if (move_evsel == evlist__first(evlist))
1615 evlist__for_each_entry_safe(evlist, n, evsel) {
1616 if (evsel->leader == move_evsel->leader)
1617 list_move_tail(&evsel->core.node, &move);
1620 list_splice(&move, &evlist->core.entries);
1623 struct evsel *evlist__get_tracking_event(struct evlist *evlist)
1625 struct evsel *evsel;
1627 evlist__for_each_entry(evlist, evsel) {
1628 if (evsel->tracking)
1632 return evlist__first(evlist);
1635 void evlist__set_tracking_event(struct evlist *evlist, struct evsel *tracking_evsel)
1637 struct evsel *evsel;
1639 if (tracking_evsel->tracking)
1642 evlist__for_each_entry(evlist, evsel) {
1643 if (evsel != tracking_evsel)
1644 evsel->tracking = false;
1647 tracking_evsel->tracking = true;
1650 struct evsel *evlist__find_evsel_by_str(struct evlist *evlist, const char *str)
1652 struct evsel *evsel;
1654 evlist__for_each_entry(evlist, evsel) {
1657 if (strcmp(str, evsel->name) == 0)
1664 void evlist__toggle_bkw_mmap(struct evlist *evlist, enum bkw_mmap_state state)
1666 enum bkw_mmap_state old_state = evlist->bkw_mmap_state;
1673 if (!evlist->overwrite_mmap)
1676 switch (old_state) {
1677 case BKW_MMAP_NOTREADY: {
1678 if (state != BKW_MMAP_RUNNING)
1682 case BKW_MMAP_RUNNING: {
1683 if (state != BKW_MMAP_DATA_PENDING)
1688 case BKW_MMAP_DATA_PENDING: {
1689 if (state != BKW_MMAP_EMPTY)
1693 case BKW_MMAP_EMPTY: {
1694 if (state != BKW_MMAP_RUNNING)
1700 WARN_ONCE(1, "Shouldn't get there\n");
1703 evlist->bkw_mmap_state = state;
1707 evlist__pause(evlist);
1710 evlist__resume(evlist);
1721 bool evlist__exclude_kernel(struct evlist *evlist)
1723 struct evsel *evsel;
1725 evlist__for_each_entry(evlist, evsel) {
1726 if (!evsel->core.attr.exclude_kernel)
1734 * Events in data file are not collect in groups, but we still want
1735 * the group display. Set the artificial group and set the leader's
1736 * forced_leader flag to notify the display code.
1738 void evlist__force_leader(struct evlist *evlist)
1740 if (!evlist->nr_groups) {
1741 struct evsel *leader = evlist__first(evlist);
1743 evlist__set_leader(evlist);
1744 leader->forced_leader = true;
1748 struct evsel *evlist__reset_weak_group(struct evlist *evsel_list, struct evsel *evsel, bool close)
1750 struct evsel *c2, *leader;
1751 bool is_open = true;
1753 leader = evsel->leader;
1754 pr_debug("Weak group for %s/%d failed\n",
1755 leader->name, leader->core.nr_members);
1758 * for_each_group_member doesn't work here because it doesn't
1759 * include the first entry.
1761 evlist__for_each_entry(evsel_list, c2) {
1764 if (c2->leader == leader) {
1765 if (is_open && close)
1766 perf_evsel__close(&c2->core);
1768 c2->core.nr_members = 0;
1770 * Set this for all former members of the group
1771 * to indicate they get reopened.
1773 c2->reset_group = true;
1779 static int evlist__parse_control_fifo(const char *str, int *ctl_fd, int *ctl_fd_ack, bool *ctl_fd_close)
1784 if (strncmp(str, "fifo:", 5))
1788 if (!*str || *str == ',')
1800 * O_RDWR avoids POLLHUPs which is necessary to allow the other
1801 * end of a FIFO to be repeatedly opened and closed.
1803 fd = open(s, O_RDWR | O_NONBLOCK | O_CLOEXEC);
1805 pr_err("Failed to open '%s'\n", s);
1810 *ctl_fd_close = true;
1813 /* O_RDWR | O_NONBLOCK means the other end need not be open */
1814 fd = open(p, O_RDWR | O_NONBLOCK | O_CLOEXEC);
1816 pr_err("Failed to open '%s'\n", p);
1828 int evlist__parse_control(const char *str, int *ctl_fd, int *ctl_fd_ack, bool *ctl_fd_close)
1830 char *comma = NULL, *endptr = NULL;
1832 *ctl_fd_close = false;
1834 if (strncmp(str, "fd:", 3))
1835 return evlist__parse_control_fifo(str, ctl_fd, ctl_fd_ack, ctl_fd_close);
1837 *ctl_fd = strtoul(&str[3], &endptr, 0);
1838 if (endptr == &str[3])
1841 comma = strchr(str, ',');
1843 if (endptr != comma)
1846 *ctl_fd_ack = strtoul(comma + 1, &endptr, 0);
1847 if (endptr == comma + 1 || *endptr != '\0')
1854 void evlist__close_control(int ctl_fd, int ctl_fd_ack, bool *ctl_fd_close)
1856 if (*ctl_fd_close) {
1857 *ctl_fd_close = false;
1859 if (ctl_fd_ack >= 0)
1864 int evlist__initialize_ctlfd(struct evlist *evlist, int fd, int ack)
1867 pr_debug("Control descriptor is not initialized\n");
1871 evlist->ctl_fd.pos = perf_evlist__add_pollfd(&evlist->core, fd, NULL, POLLIN,
1872 fdarray_flag__nonfilterable);
1873 if (evlist->ctl_fd.pos < 0) {
1874 evlist->ctl_fd.pos = -1;
1875 pr_err("Failed to add ctl fd entry: %m\n");
1879 evlist->ctl_fd.fd = fd;
1880 evlist->ctl_fd.ack = ack;
1885 bool evlist__ctlfd_initialized(struct evlist *evlist)
1887 return evlist->ctl_fd.pos >= 0;
1890 int evlist__finalize_ctlfd(struct evlist *evlist)
1892 struct pollfd *entries = evlist->core.pollfd.entries;
1894 if (!evlist__ctlfd_initialized(evlist))
1897 entries[evlist->ctl_fd.pos].fd = -1;
1898 entries[evlist->ctl_fd.pos].events = 0;
1899 entries[evlist->ctl_fd.pos].revents = 0;
1901 evlist->ctl_fd.pos = -1;
1902 evlist->ctl_fd.ack = -1;
1903 evlist->ctl_fd.fd = -1;
1908 static int evlist__ctlfd_recv(struct evlist *evlist, enum evlist_ctl_cmd *cmd,
1909 char *cmd_data, size_t data_size)
1913 size_t bytes_read = 0;
1915 *cmd = EVLIST_CTL_CMD_UNSUPPORTED;
1916 memset(cmd_data, 0, data_size);
1920 err = read(evlist->ctl_fd.fd, &c, 1);
1922 if (c == '\n' || c == '\0')
1924 cmd_data[bytes_read++] = c;
1925 if (bytes_read == data_size)
1928 } else if (err == -1) {
1931 if (errno == EAGAIN || errno == EWOULDBLOCK)
1934 pr_err("Failed to read from ctlfd %d: %m\n", evlist->ctl_fd.fd);
1939 pr_debug("Message from ctl_fd: \"%s%s\"\n", cmd_data,
1940 bytes_read == data_size ? "" : c == '\n' ? "\\n" : "\\0");
1942 if (bytes_read > 0) {
1943 if (!strncmp(cmd_data, EVLIST_CTL_CMD_ENABLE_TAG,
1944 (sizeof(EVLIST_CTL_CMD_ENABLE_TAG)-1))) {
1945 *cmd = EVLIST_CTL_CMD_ENABLE;
1946 } else if (!strncmp(cmd_data, EVLIST_CTL_CMD_DISABLE_TAG,
1947 (sizeof(EVLIST_CTL_CMD_DISABLE_TAG)-1))) {
1948 *cmd = EVLIST_CTL_CMD_DISABLE;
1949 } else if (!strncmp(cmd_data, EVLIST_CTL_CMD_SNAPSHOT_TAG,
1950 (sizeof(EVLIST_CTL_CMD_SNAPSHOT_TAG)-1))) {
1951 *cmd = EVLIST_CTL_CMD_SNAPSHOT;
1952 pr_debug("is snapshot\n");
1953 } else if (!strncmp(cmd_data, EVLIST_CTL_CMD_EVLIST_TAG,
1954 (sizeof(EVLIST_CTL_CMD_EVLIST_TAG)-1))) {
1955 *cmd = EVLIST_CTL_CMD_EVLIST;
1956 } else if (!strncmp(cmd_data, EVLIST_CTL_CMD_STOP_TAG,
1957 (sizeof(EVLIST_CTL_CMD_STOP_TAG)-1))) {
1958 *cmd = EVLIST_CTL_CMD_STOP;
1959 } else if (!strncmp(cmd_data, EVLIST_CTL_CMD_PING_TAG,
1960 (sizeof(EVLIST_CTL_CMD_PING_TAG)-1))) {
1961 *cmd = EVLIST_CTL_CMD_PING;
1965 return bytes_read ? (int)bytes_read : err;
1968 int evlist__ctlfd_ack(struct evlist *evlist)
1972 if (evlist->ctl_fd.ack == -1)
1975 err = write(evlist->ctl_fd.ack, EVLIST_CTL_CMD_ACK_TAG,
1976 sizeof(EVLIST_CTL_CMD_ACK_TAG));
1978 pr_err("failed to write to ctl_ack_fd %d: %m\n", evlist->ctl_fd.ack);
1983 static int get_cmd_arg(char *cmd_data, size_t cmd_size, char **arg)
1985 char *data = cmd_data + cmd_size;
1991 /* there's argument */
2001 static int evlist__ctlfd_enable(struct evlist *evlist, char *cmd_data, bool enable)
2003 struct evsel *evsel;
2007 err = get_cmd_arg(cmd_data,
2008 enable ? sizeof(EVLIST_CTL_CMD_ENABLE_TAG) - 1 :
2009 sizeof(EVLIST_CTL_CMD_DISABLE_TAG) - 1,
2012 pr_info("failed: wrong command\n");
2017 evsel = evlist__find_evsel_by_str(evlist, name);
2020 evlist__enable_evsel(evlist, name);
2022 evlist__disable_evsel(evlist, name);
2023 pr_info("Event %s %s\n", evsel->name,
2024 enable ? "enabled" : "disabled");
2026 pr_info("failed: can't find '%s' event\n", name);
2030 evlist__enable(evlist);
2031 pr_info(EVLIST_ENABLED_MSG);
2033 evlist__disable(evlist);
2034 pr_info(EVLIST_DISABLED_MSG);
2041 static int evlist__ctlfd_list(struct evlist *evlist, char *cmd_data)
2043 struct perf_attr_details details = { .verbose = false, };
2044 struct evsel *evsel;
2048 err = get_cmd_arg(cmd_data,
2049 sizeof(EVLIST_CTL_CMD_EVLIST_TAG) - 1,
2052 pr_info("failed: wrong command\n");
2057 if (!strcmp(arg, "-v")) {
2058 details.verbose = true;
2059 } else if (!strcmp(arg, "-g")) {
2060 details.event_group = true;
2061 } else if (!strcmp(arg, "-F")) {
2062 details.freq = true;
2064 pr_info("failed: wrong command\n");
2069 evlist__for_each_entry(evlist, evsel)
2070 evsel__fprintf(evsel, &details, stderr);
2075 int evlist__ctlfd_process(struct evlist *evlist, enum evlist_ctl_cmd *cmd)
2078 char cmd_data[EVLIST_CTL_CMD_MAX_LEN];
2079 int ctlfd_pos = evlist->ctl_fd.pos;
2080 struct pollfd *entries = evlist->core.pollfd.entries;
2082 if (!evlist__ctlfd_initialized(evlist) || !entries[ctlfd_pos].revents)
2085 if (entries[ctlfd_pos].revents & POLLIN) {
2086 err = evlist__ctlfd_recv(evlist, cmd, cmd_data,
2087 EVLIST_CTL_CMD_MAX_LEN);
2090 case EVLIST_CTL_CMD_ENABLE:
2091 case EVLIST_CTL_CMD_DISABLE:
2092 err = evlist__ctlfd_enable(evlist, cmd_data,
2093 *cmd == EVLIST_CTL_CMD_ENABLE);
2095 case EVLIST_CTL_CMD_EVLIST:
2096 err = evlist__ctlfd_list(evlist, cmd_data);
2098 case EVLIST_CTL_CMD_SNAPSHOT:
2099 case EVLIST_CTL_CMD_STOP:
2100 case EVLIST_CTL_CMD_PING:
2102 case EVLIST_CTL_CMD_ACK:
2103 case EVLIST_CTL_CMD_UNSUPPORTED:
2105 pr_debug("ctlfd: unsupported %d\n", *cmd);
2108 if (!(*cmd == EVLIST_CTL_CMD_ACK || *cmd == EVLIST_CTL_CMD_UNSUPPORTED ||
2109 *cmd == EVLIST_CTL_CMD_SNAPSHOT))
2110 evlist__ctlfd_ack(evlist);
2114 if (entries[ctlfd_pos].revents & (POLLHUP | POLLERR))
2115 evlist__finalize_ctlfd(evlist);
2117 entries[ctlfd_pos].revents = 0;
2122 struct evsel *evlist__find_evsel(struct evlist *evlist, int idx)
2124 struct evsel *evsel;
2126 evlist__for_each_entry(evlist, evsel) {
2127 if (evsel->idx == idx)