1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2011, Red Hat Inc, Arnaldo Carvalho de Melo <acme@redhat.com>
5 * Parts came from builtin-{top,stat,record}.c, see those files for further
13 #include "util/mmap.h"
14 #include "thread_map.h"
20 #include "bpf_counter.h"
21 #include <internal/lib.h> // page_size
25 #include "bpf-event.h"
26 #include "util/string2.h"
27 #include "util/perf_api_probe.h"
28 #include "util/evsel_fprintf.h"
34 #include "parse-events.h"
35 #include <subcmd/parse-options.h>
38 #include <sys/ioctl.h>
40 #include <sys/prctl.h>
42 #include <linux/bitops.h>
43 #include <linux/hash.h>
44 #include <linux/log2.h>
45 #include <linux/err.h>
46 #include <linux/string.h>
47 #include <linux/zalloc.h>
48 #include <perf/evlist.h>
49 #include <perf/evsel.h>
50 #include <perf/cpumap.h>
51 #include <perf/mmap.h>
53 #include <internal/xyarray.h>
55 #ifdef LACKS_SIGQUEUE_PROTOTYPE
56 int sigqueue(pid_t pid, int sig, const union sigval value);
59 #define FD(e, x, y) (*(int *)xyarray__entry(e->core.fd, x, y))
60 #define SID(e, x, y) xyarray__entry(e->core.sample_id, x, y)
62 void evlist__init(struct evlist *evlist, struct perf_cpu_map *cpus,
63 struct perf_thread_map *threads)
65 perf_evlist__init(&evlist->core);
66 perf_evlist__set_maps(&evlist->core, cpus, threads);
67 evlist->workload.pid = -1;
68 evlist->bkw_mmap_state = BKW_MMAP_NOTREADY;
69 evlist->ctl_fd.fd = -1;
70 evlist->ctl_fd.ack = -1;
71 evlist->ctl_fd.pos = -1;
74 struct evlist *evlist__new(void)
76 struct evlist *evlist = zalloc(sizeof(*evlist));
79 evlist__init(evlist, NULL, NULL);
84 struct evlist *evlist__new_default(void)
86 struct evlist *evlist = evlist__new();
88 if (evlist && evlist__add_default(evlist)) {
89 evlist__delete(evlist);
96 struct evlist *evlist__new_dummy(void)
98 struct evlist *evlist = evlist__new();
100 if (evlist && evlist__add_dummy(evlist)) {
101 evlist__delete(evlist);
109 * evlist__set_id_pos - set the positions of event ids.
110 * @evlist: selected event list
112 * Events with compatible sample types all have the same id_pos
113 * and is_pos. For convenience, put a copy on evlist.
115 void evlist__set_id_pos(struct evlist *evlist)
117 struct evsel *first = evlist__first(evlist);
119 evlist->id_pos = first->id_pos;
120 evlist->is_pos = first->is_pos;
123 static void evlist__update_id_pos(struct evlist *evlist)
127 evlist__for_each_entry(evlist, evsel)
128 evsel__calc_id_pos(evsel);
130 evlist__set_id_pos(evlist);
133 static void evlist__purge(struct evlist *evlist)
135 struct evsel *pos, *n;
137 evlist__for_each_entry_safe(evlist, n, pos) {
138 list_del_init(&pos->core.node);
143 evlist->core.nr_entries = 0;
146 void evlist__exit(struct evlist *evlist)
148 zfree(&evlist->mmap);
149 zfree(&evlist->overwrite_mmap);
150 perf_evlist__exit(&evlist->core);
153 void evlist__delete(struct evlist *evlist)
158 evlist__munmap(evlist);
159 evlist__close(evlist);
160 evlist__purge(evlist);
161 evlist__exit(evlist);
165 void evlist__add(struct evlist *evlist, struct evsel *entry)
167 entry->evlist = evlist;
168 entry->idx = evlist->core.nr_entries;
169 entry->tracking = !entry->idx;
171 perf_evlist__add(&evlist->core, &entry->core);
173 if (evlist->core.nr_entries == 1)
174 evlist__set_id_pos(evlist);
177 void evlist__remove(struct evlist *evlist, struct evsel *evsel)
179 evsel->evlist = NULL;
180 perf_evlist__remove(&evlist->core, &evsel->core);
183 void evlist__splice_list_tail(struct evlist *evlist, struct list_head *list)
185 while (!list_empty(list)) {
186 struct evsel *evsel, *temp, *leader = NULL;
188 __evlist__for_each_entry_safe(list, temp, evsel) {
189 list_del_init(&evsel->core.node);
190 evlist__add(evlist, evsel);
195 __evlist__for_each_entry_safe(list, temp, evsel) {
196 if (evsel->leader == leader) {
197 list_del_init(&evsel->core.node);
198 evlist__add(evlist, evsel);
204 int __evlist__set_tracepoints_handlers(struct evlist *evlist,
205 const struct evsel_str_handler *assocs, size_t nr_assocs)
210 for (i = 0; i < nr_assocs; i++) {
211 // Adding a handler for an event not in this evlist, just ignore it.
212 struct evsel *evsel = evlist__find_tracepoint_by_name(evlist, assocs[i].name);
217 if (evsel->handler != NULL)
219 evsel->handler = assocs[i].handler;
227 void __evlist__set_leader(struct list_head *list)
229 struct evsel *evsel, *leader;
231 leader = list_entry(list->next, struct evsel, core.node);
232 evsel = list_entry(list->prev, struct evsel, core.node);
234 leader->core.nr_members = evsel->idx - leader->idx + 1;
236 __evlist__for_each_entry(list, evsel) {
237 evsel->leader = leader;
241 void evlist__set_leader(struct evlist *evlist)
243 if (evlist->core.nr_entries) {
244 evlist->nr_groups = evlist->core.nr_entries > 1 ? 1 : 0;
245 __evlist__set_leader(&evlist->core.entries);
249 int __evlist__add_default(struct evlist *evlist, bool precise)
251 struct evsel *evsel = evsel__new_cycles(precise);
256 evlist__add(evlist, evsel);
260 int evlist__add_dummy(struct evlist *evlist)
262 struct perf_event_attr attr = {
263 .type = PERF_TYPE_SOFTWARE,
264 .config = PERF_COUNT_SW_DUMMY,
265 .size = sizeof(attr), /* to capture ABI version */
267 struct evsel *evsel = evsel__new_idx(&attr, evlist->core.nr_entries);
272 evlist__add(evlist, evsel);
276 static int evlist__add_attrs(struct evlist *evlist, struct perf_event_attr *attrs, size_t nr_attrs)
278 struct evsel *evsel, *n;
282 for (i = 0; i < nr_attrs; i++) {
283 evsel = evsel__new_idx(attrs + i, evlist->core.nr_entries + i);
285 goto out_delete_partial_list;
286 list_add_tail(&evsel->core.node, &head);
289 evlist__splice_list_tail(evlist, &head);
293 out_delete_partial_list:
294 __evlist__for_each_entry_safe(&head, n, evsel)
295 evsel__delete(evsel);
299 int __evlist__add_default_attrs(struct evlist *evlist, struct perf_event_attr *attrs, size_t nr_attrs)
303 for (i = 0; i < nr_attrs; i++)
304 event_attr_init(attrs + i);
306 return evlist__add_attrs(evlist, attrs, nr_attrs);
309 __weak int arch_evlist__add_default_attrs(struct evlist *evlist __maybe_unused)
314 struct evsel *evlist__find_tracepoint_by_id(struct evlist *evlist, int id)
318 evlist__for_each_entry(evlist, evsel) {
319 if (evsel->core.attr.type == PERF_TYPE_TRACEPOINT &&
320 (int)evsel->core.attr.config == id)
327 struct evsel *evlist__find_tracepoint_by_name(struct evlist *evlist, const char *name)
331 evlist__for_each_entry(evlist, evsel) {
332 if ((evsel->core.attr.type == PERF_TYPE_TRACEPOINT) &&
333 (strcmp(evsel->name, name) == 0))
340 int evlist__add_newtp(struct evlist *evlist, const char *sys, const char *name, void *handler)
342 struct evsel *evsel = evsel__newtp(sys, name);
347 evsel->handler = handler;
348 evlist__add(evlist, evsel);
352 static int evlist__nr_threads(struct evlist *evlist, struct evsel *evsel)
354 if (evsel->core.system_wide)
357 return perf_thread_map__nr(evlist->core.threads);
360 void evlist__cpu_iter_start(struct evlist *evlist)
365 * Reset the per evsel cpu_iter. This is needed because
366 * each evsel's cpumap may have a different index space,
367 * and some operations need the index to modify
368 * the FD xyarray (e.g. open, close)
370 evlist__for_each_entry(evlist, pos)
374 bool evsel__cpu_iter_skip_no_inc(struct evsel *ev, int cpu)
376 if (ev->cpu_iter >= ev->core.cpus->nr)
378 if (cpu >= 0 && ev->core.cpus->map[ev->cpu_iter] != cpu)
383 bool evsel__cpu_iter_skip(struct evsel *ev, int cpu)
385 if (!evsel__cpu_iter_skip_no_inc(ev, cpu)) {
392 static int evsel__strcmp(struct evsel *pos, char *evsel_name)
396 if (evsel__is_dummy_event(pos))
398 return strcmp(pos->name, evsel_name);
401 static int evlist__is_enabled(struct evlist *evlist)
405 evlist__for_each_entry(evlist, pos) {
406 if (!evsel__is_group_leader(pos) || !pos->core.fd)
408 /* If at least one event is enabled, evlist is enabled. */
415 static void __evlist__disable(struct evlist *evlist, char *evsel_name)
418 struct affinity affinity;
420 bool has_imm = false;
422 if (affinity__setup(&affinity) < 0)
425 evlist__for_each_entry(evlist, pos)
426 bpf_counter__disable(pos);
428 /* Disable 'immediate' events last */
429 for (imm = 0; imm <= 1; imm++) {
430 evlist__for_each_cpu(evlist, i, cpu) {
431 affinity__set(&affinity, cpu);
433 evlist__for_each_entry(evlist, pos) {
434 if (evsel__strcmp(pos, evsel_name))
436 if (evsel__cpu_iter_skip(pos, cpu))
438 if (pos->disabled || !evsel__is_group_leader(pos) || !pos->core.fd)
442 if (pos->immediate != imm)
444 evsel__disable_cpu(pos, pos->cpu_iter - 1);
451 affinity__cleanup(&affinity);
452 evlist__for_each_entry(evlist, pos) {
453 if (evsel__strcmp(pos, evsel_name))
455 if (!evsel__is_group_leader(pos) || !pos->core.fd)
457 pos->disabled = true;
461 * If we disabled only single event, we need to check
462 * the enabled state of the evlist manually.
465 evlist->enabled = evlist__is_enabled(evlist);
467 evlist->enabled = false;
470 void evlist__disable(struct evlist *evlist)
472 __evlist__disable(evlist, NULL);
475 void evlist__disable_evsel(struct evlist *evlist, char *evsel_name)
477 __evlist__disable(evlist, evsel_name);
480 static void __evlist__enable(struct evlist *evlist, char *evsel_name)
483 struct affinity affinity;
486 if (affinity__setup(&affinity) < 0)
489 evlist__for_each_cpu(evlist, i, cpu) {
490 affinity__set(&affinity, cpu);
492 evlist__for_each_entry(evlist, pos) {
493 if (evsel__strcmp(pos, evsel_name))
495 if (evsel__cpu_iter_skip(pos, cpu))
497 if (!evsel__is_group_leader(pos) || !pos->core.fd)
499 evsel__enable_cpu(pos, pos->cpu_iter - 1);
502 affinity__cleanup(&affinity);
503 evlist__for_each_entry(evlist, pos) {
504 if (evsel__strcmp(pos, evsel_name))
506 if (!evsel__is_group_leader(pos) || !pos->core.fd)
508 pos->disabled = false;
512 * Even single event sets the 'enabled' for evlist,
513 * so the toggle can work properly and toggle to
516 evlist->enabled = true;
519 void evlist__enable(struct evlist *evlist)
521 __evlist__enable(evlist, NULL);
524 void evlist__enable_evsel(struct evlist *evlist, char *evsel_name)
526 __evlist__enable(evlist, evsel_name);
529 void evlist__toggle_enable(struct evlist *evlist)
531 (evlist->enabled ? evlist__disable : evlist__enable)(evlist);
534 static int evlist__enable_event_cpu(struct evlist *evlist, struct evsel *evsel, int cpu)
537 int nr_threads = evlist__nr_threads(evlist, evsel);
542 for (thread = 0; thread < nr_threads; thread++) {
543 int err = ioctl(FD(evsel, cpu, thread), PERF_EVENT_IOC_ENABLE, 0);
550 static int evlist__enable_event_thread(struct evlist *evlist, struct evsel *evsel, int thread)
553 int nr_cpus = perf_cpu_map__nr(evlist->core.cpus);
558 for (cpu = 0; cpu < nr_cpus; cpu++) {
559 int err = ioctl(FD(evsel, cpu, thread), PERF_EVENT_IOC_ENABLE, 0);
566 int evlist__enable_event_idx(struct evlist *evlist, struct evsel *evsel, int idx)
568 bool per_cpu_mmaps = !perf_cpu_map__empty(evlist->core.cpus);
571 return evlist__enable_event_cpu(evlist, evsel, idx);
573 return evlist__enable_event_thread(evlist, evsel, idx);
576 int evlist__add_pollfd(struct evlist *evlist, int fd)
578 return perf_evlist__add_pollfd(&evlist->core, fd, NULL, POLLIN, fdarray_flag__default);
581 int evlist__filter_pollfd(struct evlist *evlist, short revents_and_mask)
583 return perf_evlist__filter_pollfd(&evlist->core, revents_and_mask);
586 #ifdef HAVE_EVENTFD_SUPPORT
587 int evlist__add_wakeup_eventfd(struct evlist *evlist, int fd)
589 return perf_evlist__add_pollfd(&evlist->core, fd, NULL, POLLIN,
590 fdarray_flag__nonfilterable);
594 int evlist__poll(struct evlist *evlist, int timeout)
596 return perf_evlist__poll(&evlist->core, timeout);
599 struct perf_sample_id *evlist__id2sid(struct evlist *evlist, u64 id)
601 struct hlist_head *head;
602 struct perf_sample_id *sid;
605 hash = hash_64(id, PERF_EVLIST__HLIST_BITS);
606 head = &evlist->core.heads[hash];
608 hlist_for_each_entry(sid, head, node)
615 struct evsel *evlist__id2evsel(struct evlist *evlist, u64 id)
617 struct perf_sample_id *sid;
619 if (evlist->core.nr_entries == 1 || !id)
620 return evlist__first(evlist);
622 sid = evlist__id2sid(evlist, id);
624 return container_of(sid->evsel, struct evsel, core);
626 if (!evlist__sample_id_all(evlist))
627 return evlist__first(evlist);
632 struct evsel *evlist__id2evsel_strict(struct evlist *evlist, u64 id)
634 struct perf_sample_id *sid;
639 sid = evlist__id2sid(evlist, id);
641 return container_of(sid->evsel, struct evsel, core);
646 static int evlist__event2id(struct evlist *evlist, union perf_event *event, u64 *id)
648 const __u64 *array = event->sample.array;
651 n = (event->header.size - sizeof(event->header)) >> 3;
653 if (event->header.type == PERF_RECORD_SAMPLE) {
654 if (evlist->id_pos >= n)
656 *id = array[evlist->id_pos];
658 if (evlist->is_pos > n)
666 struct evsel *evlist__event2evsel(struct evlist *evlist, union perf_event *event)
668 struct evsel *first = evlist__first(evlist);
669 struct hlist_head *head;
670 struct perf_sample_id *sid;
674 if (evlist->core.nr_entries == 1)
677 if (!first->core.attr.sample_id_all &&
678 event->header.type != PERF_RECORD_SAMPLE)
681 if (evlist__event2id(evlist, event, &id))
684 /* Synthesized events have an id of zero */
688 hash = hash_64(id, PERF_EVLIST__HLIST_BITS);
689 head = &evlist->core.heads[hash];
691 hlist_for_each_entry(sid, head, node) {
693 return container_of(sid->evsel, struct evsel, core);
698 static int evlist__set_paused(struct evlist *evlist, bool value)
702 if (!evlist->overwrite_mmap)
705 for (i = 0; i < evlist->core.nr_mmaps; i++) {
706 int fd = evlist->overwrite_mmap[i].core.fd;
711 err = ioctl(fd, PERF_EVENT_IOC_PAUSE_OUTPUT, value ? 1 : 0);
718 static int evlist__pause(struct evlist *evlist)
720 return evlist__set_paused(evlist, true);
723 static int evlist__resume(struct evlist *evlist)
725 return evlist__set_paused(evlist, false);
728 static void evlist__munmap_nofree(struct evlist *evlist)
733 for (i = 0; i < evlist->core.nr_mmaps; i++)
734 perf_mmap__munmap(&evlist->mmap[i].core);
736 if (evlist->overwrite_mmap)
737 for (i = 0; i < evlist->core.nr_mmaps; i++)
738 perf_mmap__munmap(&evlist->overwrite_mmap[i].core);
741 void evlist__munmap(struct evlist *evlist)
743 evlist__munmap_nofree(evlist);
744 zfree(&evlist->mmap);
745 zfree(&evlist->overwrite_mmap);
748 static void perf_mmap__unmap_cb(struct perf_mmap *map)
750 struct mmap *m = container_of(map, struct mmap, core);
755 static struct mmap *evlist__alloc_mmap(struct evlist *evlist,
761 map = zalloc(evlist->core.nr_mmaps * sizeof(struct mmap));
765 for (i = 0; i < evlist->core.nr_mmaps; i++) {
766 struct perf_mmap *prev = i ? &map[i - 1].core : NULL;
769 * When the perf_mmap() call is made we grab one refcount, plus
770 * one extra to let perf_mmap__consume() get the last
771 * events after all real references (perf_mmap__get()) are
774 * Each PERF_EVENT_IOC_SET_OUTPUT points to this mmap and
775 * thus does perf_mmap__get() on it.
777 perf_mmap__init(&map[i].core, prev, overwrite, perf_mmap__unmap_cb);
784 perf_evlist__mmap_cb_idx(struct perf_evlist *_evlist,
785 struct perf_mmap_param *_mp,
786 int idx, bool per_cpu)
788 struct evlist *evlist = container_of(_evlist, struct evlist, core);
789 struct mmap_params *mp = container_of(_mp, struct mmap_params, core);
791 auxtrace_mmap_params__set_idx(&mp->auxtrace_mp, evlist, idx, per_cpu);
794 static struct perf_mmap*
795 perf_evlist__mmap_cb_get(struct perf_evlist *_evlist, bool overwrite, int idx)
797 struct evlist *evlist = container_of(_evlist, struct evlist, core);
800 maps = overwrite ? evlist->overwrite_mmap : evlist->mmap;
803 maps = evlist__alloc_mmap(evlist, overwrite);
808 evlist->overwrite_mmap = maps;
809 if (evlist->bkw_mmap_state == BKW_MMAP_NOTREADY)
810 evlist__toggle_bkw_mmap(evlist, BKW_MMAP_RUNNING);
816 return &maps[idx].core;
820 perf_evlist__mmap_cb_mmap(struct perf_mmap *_map, struct perf_mmap_param *_mp,
823 struct mmap *map = container_of(_map, struct mmap, core);
824 struct mmap_params *mp = container_of(_mp, struct mmap_params, core);
826 return mmap__mmap(map, mp, output, cpu);
829 unsigned long perf_event_mlock_kb_in_pages(void)
834 if (sysctl__read_int("kernel/perf_event_mlock_kb", &max) < 0) {
836 * Pick a once upon a time good value, i.e. things look
837 * strange since we can't read a sysctl value, but lets not
842 max -= (page_size / 1024);
845 pages = (max * 1024) / page_size;
846 if (!is_power_of_2(pages))
847 pages = rounddown_pow_of_two(pages);
852 size_t evlist__mmap_size(unsigned long pages)
854 if (pages == UINT_MAX)
855 pages = perf_event_mlock_kb_in_pages();
856 else if (!is_power_of_2(pages))
859 return (pages + 1) * page_size;
862 static long parse_pages_arg(const char *str, unsigned long min,
865 unsigned long pages, val;
866 static struct parse_tag tags[] = {
867 { .tag = 'B', .mult = 1 },
868 { .tag = 'K', .mult = 1 << 10 },
869 { .tag = 'M', .mult = 1 << 20 },
870 { .tag = 'G', .mult = 1 << 30 },
877 val = parse_tag_value(str, tags);
878 if (val != (unsigned long) -1) {
879 /* we got file size value */
880 pages = PERF_ALIGN(val, page_size) / page_size;
882 /* we got pages count value */
884 pages = strtoul(str, &eptr, 10);
889 if (pages == 0 && min == 0) {
890 /* leave number of pages at 0 */
891 } else if (!is_power_of_2(pages)) {
894 /* round pages up to next power of 2 */
895 pages = roundup_pow_of_two(pages);
899 unit_number__scnprintf(buf, sizeof(buf), pages * page_size);
900 pr_info("rounding mmap pages size to %s (%lu pages)\n",
910 int __evlist__parse_mmap_pages(unsigned int *mmap_pages, const char *str)
912 unsigned long max = UINT_MAX;
915 if (max > SIZE_MAX / page_size)
916 max = SIZE_MAX / page_size;
918 pages = parse_pages_arg(str, 1, max);
920 pr_err("Invalid argument for --mmap_pages/-m\n");
928 int evlist__parse_mmap_pages(const struct option *opt, const char *str, int unset __maybe_unused)
930 return __evlist__parse_mmap_pages(opt->value, str);
934 * evlist__mmap_ex - Create mmaps to receive events.
935 * @evlist: list of events
936 * @pages: map length in pages
937 * @overwrite: overwrite older events?
938 * @auxtrace_pages - auxtrace map length in pages
939 * @auxtrace_overwrite - overwrite older auxtrace data?
941 * If @overwrite is %false the user needs to signal event consumption using
942 * perf_mmap__write_tail(). Using evlist__mmap_read() does this
945 * Similarly, if @auxtrace_overwrite is %false the user needs to signal data
946 * consumption using auxtrace_mmap__write_tail().
948 * Return: %0 on success, negative error code otherwise.
950 int evlist__mmap_ex(struct evlist *evlist, unsigned int pages,
951 unsigned int auxtrace_pages,
952 bool auxtrace_overwrite, int nr_cblocks, int affinity, int flush,
956 * Delay setting mp.prot: set it before calling perf_mmap__mmap.
957 * Its value is decided by evsel's write_backward.
958 * So &mp should not be passed through const pointer.
960 struct mmap_params mp = {
961 .nr_cblocks = nr_cblocks,
962 .affinity = affinity,
964 .comp_level = comp_level
966 struct perf_evlist_mmap_ops ops = {
967 .idx = perf_evlist__mmap_cb_idx,
968 .get = perf_evlist__mmap_cb_get,
969 .mmap = perf_evlist__mmap_cb_mmap,
972 evlist->core.mmap_len = evlist__mmap_size(pages);
973 pr_debug("mmap size %zuB\n", evlist->core.mmap_len);
975 auxtrace_mmap_params__init(&mp.auxtrace_mp, evlist->core.mmap_len,
976 auxtrace_pages, auxtrace_overwrite);
978 return perf_evlist__mmap_ops(&evlist->core, &ops, &mp.core);
981 int evlist__mmap(struct evlist *evlist, unsigned int pages)
983 return evlist__mmap_ex(evlist, pages, 0, false, 0, PERF_AFFINITY_SYS, 1, 0);
986 int evlist__create_maps(struct evlist *evlist, struct target *target)
988 bool all_threads = (target->per_thread && target->system_wide);
989 struct perf_cpu_map *cpus;
990 struct perf_thread_map *threads;
993 * If specify '-a' and '--per-thread' to perf record, perf record
994 * will override '--per-thread'. target->per_thread = false and
995 * target->system_wide = true.
997 * If specify '--per-thread' only to perf record,
998 * target->per_thread = true and target->system_wide = false.
1000 * So target->per_thread && target->system_wide is false.
1001 * For perf record, thread_map__new_str doesn't call
1002 * thread_map__new_all_cpus. That will keep perf record's
1005 * For perf stat, it allows the case that target->per_thread and
1006 * target->system_wide are all true. It means to collect system-wide
1007 * per-thread data. thread_map__new_str will call
1008 * thread_map__new_all_cpus to enumerate all threads.
1010 threads = thread_map__new_str(target->pid, target->tid, target->uid,
1016 if (target__uses_dummy_map(target))
1017 cpus = perf_cpu_map__dummy_new();
1019 cpus = perf_cpu_map__new(target->cpu_list);
1022 goto out_delete_threads;
1024 evlist->core.has_user_cpus = !!target->cpu_list;
1026 perf_evlist__set_maps(&evlist->core, cpus, threads);
1028 /* as evlist now has references, put count here */
1029 perf_cpu_map__put(cpus);
1030 perf_thread_map__put(threads);
1035 perf_thread_map__put(threads);
1039 int evlist__apply_filters(struct evlist *evlist, struct evsel **err_evsel)
1041 struct evsel *evsel;
1044 evlist__for_each_entry(evlist, evsel) {
1045 if (evsel->filter == NULL)
1049 * filters only work for tracepoint event, which doesn't have cpu limit.
1050 * So evlist and evsel should always be same.
1052 err = perf_evsel__apply_filter(&evsel->core, evsel->filter);
1062 int evlist__set_tp_filter(struct evlist *evlist, const char *filter)
1064 struct evsel *evsel;
1070 evlist__for_each_entry(evlist, evsel) {
1071 if (evsel->core.attr.type != PERF_TYPE_TRACEPOINT)
1074 err = evsel__set_filter(evsel, filter);
1082 int evlist__append_tp_filter(struct evlist *evlist, const char *filter)
1084 struct evsel *evsel;
1090 evlist__for_each_entry(evlist, evsel) {
1091 if (evsel->core.attr.type != PERF_TYPE_TRACEPOINT)
1094 err = evsel__append_tp_filter(evsel, filter);
1102 char *asprintf__tp_filter_pids(size_t npids, pid_t *pids)
1107 for (i = 0; i < npids; ++i) {
1109 if (asprintf(&filter, "common_pid != %d", pids[i]) < 0)
1114 if (asprintf(&tmp, "%s && common_pid != %d", filter, pids[i]) < 0)
1128 int evlist__set_tp_filter_pids(struct evlist *evlist, size_t npids, pid_t *pids)
1130 char *filter = asprintf__tp_filter_pids(npids, pids);
1131 int ret = evlist__set_tp_filter(evlist, filter);
1137 int evlist__set_tp_filter_pid(struct evlist *evlist, pid_t pid)
1139 return evlist__set_tp_filter_pids(evlist, 1, &pid);
1142 int evlist__append_tp_filter_pids(struct evlist *evlist, size_t npids, pid_t *pids)
1144 char *filter = asprintf__tp_filter_pids(npids, pids);
1145 int ret = evlist__append_tp_filter(evlist, filter);
1151 int evlist__append_tp_filter_pid(struct evlist *evlist, pid_t pid)
1153 return evlist__append_tp_filter_pids(evlist, 1, &pid);
1156 bool evlist__valid_sample_type(struct evlist *evlist)
1160 if (evlist->core.nr_entries == 1)
1163 if (evlist->id_pos < 0 || evlist->is_pos < 0)
1166 evlist__for_each_entry(evlist, pos) {
1167 if (pos->id_pos != evlist->id_pos ||
1168 pos->is_pos != evlist->is_pos)
1175 u64 __evlist__combined_sample_type(struct evlist *evlist)
1177 struct evsel *evsel;
1179 if (evlist->combined_sample_type)
1180 return evlist->combined_sample_type;
1182 evlist__for_each_entry(evlist, evsel)
1183 evlist->combined_sample_type |= evsel->core.attr.sample_type;
1185 return evlist->combined_sample_type;
1188 u64 evlist__combined_sample_type(struct evlist *evlist)
1190 evlist->combined_sample_type = 0;
1191 return __evlist__combined_sample_type(evlist);
1194 u64 evlist__combined_branch_type(struct evlist *evlist)
1196 struct evsel *evsel;
1197 u64 branch_type = 0;
1199 evlist__for_each_entry(evlist, evsel)
1200 branch_type |= evsel->core.attr.branch_sample_type;
1204 bool evlist__valid_read_format(struct evlist *evlist)
1206 struct evsel *first = evlist__first(evlist), *pos = first;
1207 u64 read_format = first->core.attr.read_format;
1208 u64 sample_type = first->core.attr.sample_type;
1210 evlist__for_each_entry(evlist, pos) {
1211 if (read_format != pos->core.attr.read_format) {
1212 pr_debug("Read format differs %#" PRIx64 " vs %#" PRIx64 "\n",
1213 read_format, (u64)pos->core.attr.read_format);
1217 /* PERF_SAMPLE_READ implies PERF_FORMAT_ID. */
1218 if ((sample_type & PERF_SAMPLE_READ) &&
1219 !(read_format & PERF_FORMAT_ID)) {
1226 u16 evlist__id_hdr_size(struct evlist *evlist)
1228 struct evsel *first = evlist__first(evlist);
1229 struct perf_sample *data;
1233 if (!first->core.attr.sample_id_all)
1236 sample_type = first->core.attr.sample_type;
1238 if (sample_type & PERF_SAMPLE_TID)
1239 size += sizeof(data->tid) * 2;
1241 if (sample_type & PERF_SAMPLE_TIME)
1242 size += sizeof(data->time);
1244 if (sample_type & PERF_SAMPLE_ID)
1245 size += sizeof(data->id);
1247 if (sample_type & PERF_SAMPLE_STREAM_ID)
1248 size += sizeof(data->stream_id);
1250 if (sample_type & PERF_SAMPLE_CPU)
1251 size += sizeof(data->cpu) * 2;
1253 if (sample_type & PERF_SAMPLE_IDENTIFIER)
1254 size += sizeof(data->id);
1259 bool evlist__valid_sample_id_all(struct evlist *evlist)
1261 struct evsel *first = evlist__first(evlist), *pos = first;
1263 evlist__for_each_entry_continue(evlist, pos) {
1264 if (first->core.attr.sample_id_all != pos->core.attr.sample_id_all)
1271 bool evlist__sample_id_all(struct evlist *evlist)
1273 struct evsel *first = evlist__first(evlist);
1274 return first->core.attr.sample_id_all;
1277 void evlist__set_selected(struct evlist *evlist, struct evsel *evsel)
1279 evlist->selected = evsel;
1282 void evlist__close(struct evlist *evlist)
1284 struct evsel *evsel;
1285 struct affinity affinity;
1289 * With perf record core.cpus is usually NULL.
1290 * Use the old method to handle this for now.
1292 if (!evlist->core.cpus) {
1293 evlist__for_each_entry_reverse(evlist, evsel)
1294 evsel__close(evsel);
1298 if (affinity__setup(&affinity) < 0)
1300 evlist__for_each_cpu(evlist, i, cpu) {
1301 affinity__set(&affinity, cpu);
1303 evlist__for_each_entry_reverse(evlist, evsel) {
1304 if (evsel__cpu_iter_skip(evsel, cpu))
1306 perf_evsel__close_cpu(&evsel->core, evsel->cpu_iter - 1);
1309 affinity__cleanup(&affinity);
1310 evlist__for_each_entry_reverse(evlist, evsel) {
1311 perf_evsel__free_fd(&evsel->core);
1312 perf_evsel__free_id(&evsel->core);
1314 perf_evlist__reset_id_hash(&evlist->core);
1317 static int evlist__create_syswide_maps(struct evlist *evlist)
1319 struct perf_cpu_map *cpus;
1320 struct perf_thread_map *threads;
1324 * Try reading /sys/devices/system/cpu/online to get
1327 * FIXME: -ENOMEM is the best we can do here, the cpu_map
1328 * code needs an overhaul to properly forward the
1329 * error, and we may not want to do that fallback to a
1330 * default cpu identity map :-\
1332 cpus = perf_cpu_map__new(NULL);
1336 threads = perf_thread_map__new_dummy();
1340 perf_evlist__set_maps(&evlist->core, cpus, threads);
1342 perf_thread_map__put(threads);
1344 perf_cpu_map__put(cpus);
1349 int evlist__open(struct evlist *evlist)
1351 struct evsel *evsel;
1355 * Default: one fd per CPU, all threads, aka systemwide
1356 * as sys_perf_event_open(cpu = -1, thread = -1) is EINVAL
1358 if (evlist->core.threads == NULL && evlist->core.cpus == NULL) {
1359 err = evlist__create_syswide_maps(evlist);
1364 evlist__update_id_pos(evlist);
1366 evlist__for_each_entry(evlist, evsel) {
1367 err = evsel__open(evsel, evsel->core.cpus, evsel->core.threads);
1374 evlist__close(evlist);
1379 int evlist__prepare_workload(struct evlist *evlist, struct target *target, const char *argv[],
1380 bool pipe_output, void (*exec_error)(int signo, siginfo_t *info, void *ucontext))
1382 int child_ready_pipe[2], go_pipe[2];
1385 if (pipe(child_ready_pipe) < 0) {
1386 perror("failed to create 'ready' pipe");
1390 if (pipe(go_pipe) < 0) {
1391 perror("failed to create 'go' pipe");
1392 goto out_close_ready_pipe;
1395 evlist->workload.pid = fork();
1396 if (evlist->workload.pid < 0) {
1397 perror("failed to fork");
1398 goto out_close_pipes;
1401 if (!evlist->workload.pid) {
1407 signal(SIGTERM, SIG_DFL);
1409 close(child_ready_pipe[0]);
1411 fcntl(go_pipe[0], F_SETFD, FD_CLOEXEC);
1414 * Change the name of this process not to confuse --exclude-perf users
1415 * that sees 'perf' in the window up to the execvp() and thinks that
1416 * perf samples are not being excluded.
1418 prctl(PR_SET_NAME, "perf-exec");
1421 * Tell the parent we're ready to go
1423 close(child_ready_pipe[1]);
1426 * Wait until the parent tells us to go.
1428 ret = read(go_pipe[0], &bf, 1);
1430 * The parent will ask for the execvp() to be performed by
1431 * writing exactly one byte, in workload.cork_fd, usually via
1432 * evlist__start_workload().
1434 * For cancelling the workload without actually running it,
1435 * the parent will just close workload.cork_fd, without writing
1436 * anything, i.e. read will return zero and we just exit()
1441 perror("unable to read pipe");
1445 execvp(argv[0], (char **)argv);
1450 val.sival_int = errno;
1451 if (sigqueue(getppid(), SIGUSR1, val))
1459 struct sigaction act = {
1460 .sa_flags = SA_SIGINFO,
1461 .sa_sigaction = exec_error,
1463 sigaction(SIGUSR1, &act, NULL);
1466 if (target__none(target)) {
1467 if (evlist->core.threads == NULL) {
1468 fprintf(stderr, "FATAL: evlist->threads need to be set at this point (%s:%d).\n",
1469 __func__, __LINE__);
1470 goto out_close_pipes;
1472 perf_thread_map__set_pid(evlist->core.threads, 0, evlist->workload.pid);
1475 close(child_ready_pipe[1]);
1478 * wait for child to settle
1480 if (read(child_ready_pipe[0], &bf, 1) == -1) {
1481 perror("unable to read pipe");
1482 goto out_close_pipes;
1485 fcntl(go_pipe[1], F_SETFD, FD_CLOEXEC);
1486 evlist->workload.cork_fd = go_pipe[1];
1487 close(child_ready_pipe[0]);
1493 out_close_ready_pipe:
1494 close(child_ready_pipe[0]);
1495 close(child_ready_pipe[1]);
1499 int evlist__start_workload(struct evlist *evlist)
1501 if (evlist->workload.cork_fd > 0) {
1505 * Remove the cork, let it rip!
1507 ret = write(evlist->workload.cork_fd, &bf, 1);
1509 perror("unable to write to pipe");
1511 close(evlist->workload.cork_fd);
1518 int evlist__parse_sample(struct evlist *evlist, union perf_event *event, struct perf_sample *sample)
1520 struct evsel *evsel = evlist__event2evsel(evlist, event);
1524 return evsel__parse_sample(evsel, event, sample);
1527 int evlist__parse_sample_timestamp(struct evlist *evlist, union perf_event *event, u64 *timestamp)
1529 struct evsel *evsel = evlist__event2evsel(evlist, event);
1533 return evsel__parse_sample_timestamp(evsel, event, timestamp);
1536 int evlist__strerror_open(struct evlist *evlist, int err, char *buf, size_t size)
1539 char sbuf[STRERR_BUFSIZE], *emsg = str_error_r(err, sbuf, sizeof(sbuf));
1544 printed = scnprintf(buf, size,
1546 "Hint:\tCheck /proc/sys/kernel/perf_event_paranoid setting.", emsg);
1548 value = perf_event_paranoid();
1550 printed += scnprintf(buf + printed, size - printed, "\nHint:\t");
1553 printed += scnprintf(buf + printed, size - printed,
1554 "For your workloads it needs to be <= 1\nHint:\t");
1556 printed += scnprintf(buf + printed, size - printed,
1557 "For system wide tracing it needs to be set to -1.\n");
1559 printed += scnprintf(buf + printed, size - printed,
1560 "Hint:\tTry: 'sudo sh -c \"echo -1 > /proc/sys/kernel/perf_event_paranoid\"'\n"
1561 "Hint:\tThe current value is %d.", value);
1564 struct evsel *first = evlist__first(evlist);
1567 if (sysctl__read_int("kernel/perf_event_max_sample_rate", &max_freq) < 0)
1570 if (first->core.attr.sample_freq < (u64)max_freq)
1573 printed = scnprintf(buf, size,
1575 "Hint:\tCheck /proc/sys/kernel/perf_event_max_sample_rate.\n"
1576 "Hint:\tThe current value is %d and %" PRIu64 " is being requested.",
1577 emsg, max_freq, first->core.attr.sample_freq);
1582 scnprintf(buf, size, "%s", emsg);
1589 int evlist__strerror_mmap(struct evlist *evlist, int err, char *buf, size_t size)
1591 char sbuf[STRERR_BUFSIZE], *emsg = str_error_r(err, sbuf, sizeof(sbuf));
1592 int pages_attempted = evlist->core.mmap_len / 1024, pages_max_per_user, printed = 0;
1596 sysctl__read_int("kernel/perf_event_mlock_kb", &pages_max_per_user);
1597 printed += scnprintf(buf + printed, size - printed,
1599 "Hint:\tCheck /proc/sys/kernel/perf_event_mlock_kb (%d kB) setting.\n"
1600 "Hint:\tTried using %zd kB.\n",
1601 emsg, pages_max_per_user, pages_attempted);
1603 if (pages_attempted >= pages_max_per_user) {
1604 printed += scnprintf(buf + printed, size - printed,
1605 "Hint:\tTry 'sudo sh -c \"echo %d > /proc/sys/kernel/perf_event_mlock_kb\"', or\n",
1606 pages_max_per_user + pages_attempted);
1609 printed += scnprintf(buf + printed, size - printed,
1610 "Hint:\tTry using a smaller -m/--mmap-pages value.");
1613 scnprintf(buf, size, "%s", emsg);
1620 void evlist__to_front(struct evlist *evlist, struct evsel *move_evsel)
1622 struct evsel *evsel, *n;
1625 if (move_evsel == evlist__first(evlist))
1628 evlist__for_each_entry_safe(evlist, n, evsel) {
1629 if (evsel->leader == move_evsel->leader)
1630 list_move_tail(&evsel->core.node, &move);
1633 list_splice(&move, &evlist->core.entries);
1636 struct evsel *evlist__get_tracking_event(struct evlist *evlist)
1638 struct evsel *evsel;
1640 evlist__for_each_entry(evlist, evsel) {
1641 if (evsel->tracking)
1645 return evlist__first(evlist);
1648 void evlist__set_tracking_event(struct evlist *evlist, struct evsel *tracking_evsel)
1650 struct evsel *evsel;
1652 if (tracking_evsel->tracking)
1655 evlist__for_each_entry(evlist, evsel) {
1656 if (evsel != tracking_evsel)
1657 evsel->tracking = false;
1660 tracking_evsel->tracking = true;
1663 struct evsel *evlist__find_evsel_by_str(struct evlist *evlist, const char *str)
1665 struct evsel *evsel;
1667 evlist__for_each_entry(evlist, evsel) {
1670 if (strcmp(str, evsel->name) == 0)
1677 void evlist__toggle_bkw_mmap(struct evlist *evlist, enum bkw_mmap_state state)
1679 enum bkw_mmap_state old_state = evlist->bkw_mmap_state;
1686 if (!evlist->overwrite_mmap)
1689 switch (old_state) {
1690 case BKW_MMAP_NOTREADY: {
1691 if (state != BKW_MMAP_RUNNING)
1695 case BKW_MMAP_RUNNING: {
1696 if (state != BKW_MMAP_DATA_PENDING)
1701 case BKW_MMAP_DATA_PENDING: {
1702 if (state != BKW_MMAP_EMPTY)
1706 case BKW_MMAP_EMPTY: {
1707 if (state != BKW_MMAP_RUNNING)
1713 WARN_ONCE(1, "Shouldn't get there\n");
1716 evlist->bkw_mmap_state = state;
1720 evlist__pause(evlist);
1723 evlist__resume(evlist);
1734 bool evlist__exclude_kernel(struct evlist *evlist)
1736 struct evsel *evsel;
1738 evlist__for_each_entry(evlist, evsel) {
1739 if (!evsel->core.attr.exclude_kernel)
1747 * Events in data file are not collect in groups, but we still want
1748 * the group display. Set the artificial group and set the leader's
1749 * forced_leader flag to notify the display code.
1751 void evlist__force_leader(struct evlist *evlist)
1753 if (!evlist->nr_groups) {
1754 struct evsel *leader = evlist__first(evlist);
1756 evlist__set_leader(evlist);
1757 leader->forced_leader = true;
1761 struct evsel *evlist__reset_weak_group(struct evlist *evsel_list, struct evsel *evsel, bool close)
1763 struct evsel *c2, *leader;
1764 bool is_open = true;
1766 leader = evsel->leader;
1767 pr_debug("Weak group for %s/%d failed\n",
1768 leader->name, leader->core.nr_members);
1771 * for_each_group_member doesn't work here because it doesn't
1772 * include the first entry.
1774 evlist__for_each_entry(evsel_list, c2) {
1777 if (c2->leader == leader) {
1778 if (is_open && close)
1779 perf_evsel__close(&c2->core);
1781 c2->core.nr_members = 0;
1783 * Set this for all former members of the group
1784 * to indicate they get reopened.
1786 c2->reset_group = true;
1792 static int evlist__parse_control_fifo(const char *str, int *ctl_fd, int *ctl_fd_ack, bool *ctl_fd_close)
1797 if (strncmp(str, "fifo:", 5))
1801 if (!*str || *str == ',')
1813 * O_RDWR avoids POLLHUPs which is necessary to allow the other
1814 * end of a FIFO to be repeatedly opened and closed.
1816 fd = open(s, O_RDWR | O_NONBLOCK | O_CLOEXEC);
1818 pr_err("Failed to open '%s'\n", s);
1823 *ctl_fd_close = true;
1826 /* O_RDWR | O_NONBLOCK means the other end need not be open */
1827 fd = open(p, O_RDWR | O_NONBLOCK | O_CLOEXEC);
1829 pr_err("Failed to open '%s'\n", p);
1841 int evlist__parse_control(const char *str, int *ctl_fd, int *ctl_fd_ack, bool *ctl_fd_close)
1843 char *comma = NULL, *endptr = NULL;
1845 *ctl_fd_close = false;
1847 if (strncmp(str, "fd:", 3))
1848 return evlist__parse_control_fifo(str, ctl_fd, ctl_fd_ack, ctl_fd_close);
1850 *ctl_fd = strtoul(&str[3], &endptr, 0);
1851 if (endptr == &str[3])
1854 comma = strchr(str, ',');
1856 if (endptr != comma)
1859 *ctl_fd_ack = strtoul(comma + 1, &endptr, 0);
1860 if (endptr == comma + 1 || *endptr != '\0')
1867 void evlist__close_control(int ctl_fd, int ctl_fd_ack, bool *ctl_fd_close)
1869 if (*ctl_fd_close) {
1870 *ctl_fd_close = false;
1872 if (ctl_fd_ack >= 0)
1877 int evlist__initialize_ctlfd(struct evlist *evlist, int fd, int ack)
1880 pr_debug("Control descriptor is not initialized\n");
1884 evlist->ctl_fd.pos = perf_evlist__add_pollfd(&evlist->core, fd, NULL, POLLIN,
1885 fdarray_flag__nonfilterable);
1886 if (evlist->ctl_fd.pos < 0) {
1887 evlist->ctl_fd.pos = -1;
1888 pr_err("Failed to add ctl fd entry: %m\n");
1892 evlist->ctl_fd.fd = fd;
1893 evlist->ctl_fd.ack = ack;
1898 bool evlist__ctlfd_initialized(struct evlist *evlist)
1900 return evlist->ctl_fd.pos >= 0;
1903 int evlist__finalize_ctlfd(struct evlist *evlist)
1905 struct pollfd *entries = evlist->core.pollfd.entries;
1907 if (!evlist__ctlfd_initialized(evlist))
1910 entries[evlist->ctl_fd.pos].fd = -1;
1911 entries[evlist->ctl_fd.pos].events = 0;
1912 entries[evlist->ctl_fd.pos].revents = 0;
1914 evlist->ctl_fd.pos = -1;
1915 evlist->ctl_fd.ack = -1;
1916 evlist->ctl_fd.fd = -1;
1921 static int evlist__ctlfd_recv(struct evlist *evlist, enum evlist_ctl_cmd *cmd,
1922 char *cmd_data, size_t data_size)
1926 size_t bytes_read = 0;
1928 *cmd = EVLIST_CTL_CMD_UNSUPPORTED;
1929 memset(cmd_data, 0, data_size);
1933 err = read(evlist->ctl_fd.fd, &c, 1);
1935 if (c == '\n' || c == '\0')
1937 cmd_data[bytes_read++] = c;
1938 if (bytes_read == data_size)
1941 } else if (err == -1) {
1944 if (errno == EAGAIN || errno == EWOULDBLOCK)
1947 pr_err("Failed to read from ctlfd %d: %m\n", evlist->ctl_fd.fd);
1952 pr_debug("Message from ctl_fd: \"%s%s\"\n", cmd_data,
1953 bytes_read == data_size ? "" : c == '\n' ? "\\n" : "\\0");
1955 if (bytes_read > 0) {
1956 if (!strncmp(cmd_data, EVLIST_CTL_CMD_ENABLE_TAG,
1957 (sizeof(EVLIST_CTL_CMD_ENABLE_TAG)-1))) {
1958 *cmd = EVLIST_CTL_CMD_ENABLE;
1959 } else if (!strncmp(cmd_data, EVLIST_CTL_CMD_DISABLE_TAG,
1960 (sizeof(EVLIST_CTL_CMD_DISABLE_TAG)-1))) {
1961 *cmd = EVLIST_CTL_CMD_DISABLE;
1962 } else if (!strncmp(cmd_data, EVLIST_CTL_CMD_SNAPSHOT_TAG,
1963 (sizeof(EVLIST_CTL_CMD_SNAPSHOT_TAG)-1))) {
1964 *cmd = EVLIST_CTL_CMD_SNAPSHOT;
1965 pr_debug("is snapshot\n");
1966 } else if (!strncmp(cmd_data, EVLIST_CTL_CMD_EVLIST_TAG,
1967 (sizeof(EVLIST_CTL_CMD_EVLIST_TAG)-1))) {
1968 *cmd = EVLIST_CTL_CMD_EVLIST;
1969 } else if (!strncmp(cmd_data, EVLIST_CTL_CMD_STOP_TAG,
1970 (sizeof(EVLIST_CTL_CMD_STOP_TAG)-1))) {
1971 *cmd = EVLIST_CTL_CMD_STOP;
1972 } else if (!strncmp(cmd_data, EVLIST_CTL_CMD_PING_TAG,
1973 (sizeof(EVLIST_CTL_CMD_PING_TAG)-1))) {
1974 *cmd = EVLIST_CTL_CMD_PING;
1978 return bytes_read ? (int)bytes_read : err;
1981 int evlist__ctlfd_ack(struct evlist *evlist)
1985 if (evlist->ctl_fd.ack == -1)
1988 err = write(evlist->ctl_fd.ack, EVLIST_CTL_CMD_ACK_TAG,
1989 sizeof(EVLIST_CTL_CMD_ACK_TAG));
1991 pr_err("failed to write to ctl_ack_fd %d: %m\n", evlist->ctl_fd.ack);
1996 static int get_cmd_arg(char *cmd_data, size_t cmd_size, char **arg)
1998 char *data = cmd_data + cmd_size;
2004 /* there's argument */
2014 static int evlist__ctlfd_enable(struct evlist *evlist, char *cmd_data, bool enable)
2016 struct evsel *evsel;
2020 err = get_cmd_arg(cmd_data,
2021 enable ? sizeof(EVLIST_CTL_CMD_ENABLE_TAG) - 1 :
2022 sizeof(EVLIST_CTL_CMD_DISABLE_TAG) - 1,
2025 pr_info("failed: wrong command\n");
2030 evsel = evlist__find_evsel_by_str(evlist, name);
2033 evlist__enable_evsel(evlist, name);
2035 evlist__disable_evsel(evlist, name);
2036 pr_info("Event %s %s\n", evsel->name,
2037 enable ? "enabled" : "disabled");
2039 pr_info("failed: can't find '%s' event\n", name);
2043 evlist__enable(evlist);
2044 pr_info(EVLIST_ENABLED_MSG);
2046 evlist__disable(evlist);
2047 pr_info(EVLIST_DISABLED_MSG);
2054 static int evlist__ctlfd_list(struct evlist *evlist, char *cmd_data)
2056 struct perf_attr_details details = { .verbose = false, };
2057 struct evsel *evsel;
2061 err = get_cmd_arg(cmd_data,
2062 sizeof(EVLIST_CTL_CMD_EVLIST_TAG) - 1,
2065 pr_info("failed: wrong command\n");
2070 if (!strcmp(arg, "-v")) {
2071 details.verbose = true;
2072 } else if (!strcmp(arg, "-g")) {
2073 details.event_group = true;
2074 } else if (!strcmp(arg, "-F")) {
2075 details.freq = true;
2077 pr_info("failed: wrong command\n");
2082 evlist__for_each_entry(evlist, evsel)
2083 evsel__fprintf(evsel, &details, stderr);
2088 int evlist__ctlfd_process(struct evlist *evlist, enum evlist_ctl_cmd *cmd)
2091 char cmd_data[EVLIST_CTL_CMD_MAX_LEN];
2092 int ctlfd_pos = evlist->ctl_fd.pos;
2093 struct pollfd *entries = evlist->core.pollfd.entries;
2095 if (!evlist__ctlfd_initialized(evlist) || !entries[ctlfd_pos].revents)
2098 if (entries[ctlfd_pos].revents & POLLIN) {
2099 err = evlist__ctlfd_recv(evlist, cmd, cmd_data,
2100 EVLIST_CTL_CMD_MAX_LEN);
2103 case EVLIST_CTL_CMD_ENABLE:
2104 case EVLIST_CTL_CMD_DISABLE:
2105 err = evlist__ctlfd_enable(evlist, cmd_data,
2106 *cmd == EVLIST_CTL_CMD_ENABLE);
2108 case EVLIST_CTL_CMD_EVLIST:
2109 err = evlist__ctlfd_list(evlist, cmd_data);
2111 case EVLIST_CTL_CMD_SNAPSHOT:
2112 case EVLIST_CTL_CMD_STOP:
2113 case EVLIST_CTL_CMD_PING:
2115 case EVLIST_CTL_CMD_ACK:
2116 case EVLIST_CTL_CMD_UNSUPPORTED:
2118 pr_debug("ctlfd: unsupported %d\n", *cmd);
2121 if (!(*cmd == EVLIST_CTL_CMD_ACK || *cmd == EVLIST_CTL_CMD_UNSUPPORTED ||
2122 *cmd == EVLIST_CTL_CMD_SNAPSHOT))
2123 evlist__ctlfd_ack(evlist);
2127 if (entries[ctlfd_pos].revents & (POLLHUP | POLLERR))
2128 evlist__finalize_ctlfd(evlist);
2130 entries[ctlfd_pos].revents = 0;
2135 struct evsel *evlist__find_evsel(struct evlist *evlist, int idx)
2137 struct evsel *evsel;
2139 evlist__for_each_entry(evlist, evsel) {
2140 if (evsel->idx == idx)
2146 int evlist__scnprintf_evsels(struct evlist *evlist, size_t size, char *bf)
2148 struct evsel *evsel;
2151 evlist__for_each_entry(evlist, evsel) {
2152 if (evsel__is_dummy_event(evsel))
2154 if (size > (strlen(evsel__name(evsel)) + (printed ? 2 : 1))) {
2155 printed += scnprintf(bf + printed, size - printed, "%s%s", printed ? "," : "", evsel__name(evsel));
2157 printed += scnprintf(bf + printed, size - printed, "%s...", printed ? "," : "");