1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2011, Red Hat Inc, Arnaldo Carvalho de Melo <acme@redhat.com>
5 * Parts came from builtin-{top,stat,record}.c, see those files for further
13 #include "util/mmap.h"
14 #include "thread_map.h"
20 #include "bpf_counter.h"
21 #include <internal/lib.h> // page_size
25 #include "bpf-event.h"
26 #include "util/string2.h"
27 #include "util/perf_api_probe.h"
28 #include "util/evsel_fprintf.h"
29 #include "util/evlist-hybrid.h"
36 #include "parse-events.h"
37 #include <subcmd/parse-options.h>
40 #include <sys/ioctl.h>
42 #include <sys/prctl.h>
44 #include <linux/bitops.h>
45 #include <linux/hash.h>
46 #include <linux/log2.h>
47 #include <linux/err.h>
48 #include <linux/string.h>
49 #include <linux/zalloc.h>
50 #include <perf/evlist.h>
51 #include <perf/evsel.h>
52 #include <perf/cpumap.h>
53 #include <perf/mmap.h>
55 #include <internal/xyarray.h>
57 #ifdef LACKS_SIGQUEUE_PROTOTYPE
58 int sigqueue(pid_t pid, int sig, const union sigval value);
61 #define FD(e, x, y) (*(int *)xyarray__entry(e->core.fd, x, y))
62 #define SID(e, x, y) xyarray__entry(e->core.sample_id, x, y)
64 void evlist__init(struct evlist *evlist, struct perf_cpu_map *cpus,
65 struct perf_thread_map *threads)
67 perf_evlist__init(&evlist->core);
68 perf_evlist__set_maps(&evlist->core, cpus, threads);
69 evlist->workload.pid = -1;
70 evlist->bkw_mmap_state = BKW_MMAP_NOTREADY;
71 evlist->ctl_fd.fd = -1;
72 evlist->ctl_fd.ack = -1;
73 evlist->ctl_fd.pos = -1;
76 struct evlist *evlist__new(void)
78 struct evlist *evlist = zalloc(sizeof(*evlist));
81 evlist__init(evlist, NULL, NULL);
86 struct evlist *evlist__new_default(void)
88 struct evlist *evlist = evlist__new();
90 if (evlist && evlist__add_default(evlist)) {
91 evlist__delete(evlist);
98 struct evlist *evlist__new_dummy(void)
100 struct evlist *evlist = evlist__new();
102 if (evlist && evlist__add_dummy(evlist)) {
103 evlist__delete(evlist);
111 * evlist__set_id_pos - set the positions of event ids.
112 * @evlist: selected event list
114 * Events with compatible sample types all have the same id_pos
115 * and is_pos. For convenience, put a copy on evlist.
117 void evlist__set_id_pos(struct evlist *evlist)
119 struct evsel *first = evlist__first(evlist);
121 evlist->id_pos = first->id_pos;
122 evlist->is_pos = first->is_pos;
125 static void evlist__update_id_pos(struct evlist *evlist)
129 evlist__for_each_entry(evlist, evsel)
130 evsel__calc_id_pos(evsel);
132 evlist__set_id_pos(evlist);
135 static void evlist__purge(struct evlist *evlist)
137 struct evsel *pos, *n;
139 evlist__for_each_entry_safe(evlist, n, pos) {
140 list_del_init(&pos->core.node);
145 evlist->core.nr_entries = 0;
148 void evlist__exit(struct evlist *evlist)
150 zfree(&evlist->mmap);
151 zfree(&evlist->overwrite_mmap);
152 perf_evlist__exit(&evlist->core);
155 void evlist__delete(struct evlist *evlist)
160 evlist__munmap(evlist);
161 evlist__close(evlist);
162 evlist__purge(evlist);
163 evlist__exit(evlist);
167 void evlist__add(struct evlist *evlist, struct evsel *entry)
169 perf_evlist__add(&evlist->core, &entry->core);
170 entry->evlist = evlist;
171 entry->tracking = !entry->core.idx;
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__has_leader(evsel, 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 evlist *evlist)
229 perf_evlist__set_leader(&evlist->core);
232 int __evlist__add_default(struct evlist *evlist, bool precise)
236 evsel = evsel__new_cycles(precise, PERF_TYPE_HARDWARE,
237 PERF_COUNT_HW_CPU_CYCLES);
241 evlist__add(evlist, evsel);
245 int evlist__add_dummy(struct evlist *evlist)
247 struct perf_event_attr attr = {
248 .type = PERF_TYPE_SOFTWARE,
249 .config = PERF_COUNT_SW_DUMMY,
250 .size = sizeof(attr), /* to capture ABI version */
252 struct evsel *evsel = evsel__new_idx(&attr, evlist->core.nr_entries);
257 evlist__add(evlist, evsel);
261 static int evlist__add_attrs(struct evlist *evlist, struct perf_event_attr *attrs, size_t nr_attrs)
263 struct evsel *evsel, *n;
267 for (i = 0; i < nr_attrs; i++) {
268 evsel = evsel__new_idx(attrs + i, evlist->core.nr_entries + i);
270 goto out_delete_partial_list;
271 list_add_tail(&evsel->core.node, &head);
274 evlist__splice_list_tail(evlist, &head);
278 out_delete_partial_list:
279 __evlist__for_each_entry_safe(&head, n, evsel)
280 evsel__delete(evsel);
284 int __evlist__add_default_attrs(struct evlist *evlist, struct perf_event_attr *attrs, size_t nr_attrs)
288 for (i = 0; i < nr_attrs; i++)
289 event_attr_init(attrs + i);
291 return evlist__add_attrs(evlist, attrs, nr_attrs);
294 __weak int arch_evlist__add_default_attrs(struct evlist *evlist __maybe_unused)
299 struct evsel *evlist__find_tracepoint_by_id(struct evlist *evlist, int id)
303 evlist__for_each_entry(evlist, evsel) {
304 if (evsel->core.attr.type == PERF_TYPE_TRACEPOINT &&
305 (int)evsel->core.attr.config == id)
312 struct evsel *evlist__find_tracepoint_by_name(struct evlist *evlist, const char *name)
316 evlist__for_each_entry(evlist, evsel) {
317 if ((evsel->core.attr.type == PERF_TYPE_TRACEPOINT) &&
318 (strcmp(evsel->name, name) == 0))
325 int evlist__add_newtp(struct evlist *evlist, const char *sys, const char *name, void *handler)
327 struct evsel *evsel = evsel__newtp(sys, name);
332 evsel->handler = handler;
333 evlist__add(evlist, evsel);
337 static int evlist__nr_threads(struct evlist *evlist, struct evsel *evsel)
339 if (evsel->core.system_wide)
342 return perf_thread_map__nr(evlist->core.threads);
345 struct evlist_cpu_iterator evlist__cpu_begin(struct evlist *evlist, struct affinity *affinity)
347 struct evlist_cpu_iterator itr = {
349 .evsel = evlist__first(evlist),
351 .evlist_cpu_map_idx = 0,
352 .evlist_cpu_map_nr = perf_cpu_map__nr(evlist->core.all_cpus),
353 .cpu = (struct perf_cpu){ .cpu = -1},
354 .affinity = affinity,
358 itr.cpu = perf_cpu_map__cpu(evlist->core.all_cpus, 0);
359 affinity__set(itr.affinity, itr.cpu.cpu);
360 itr.cpu_map_idx = perf_cpu_map__idx(itr.evsel->core.cpus, itr.cpu);
362 * If this CPU isn't in the evsel's cpu map then advance through
365 if (itr.cpu_map_idx == -1)
366 evlist_cpu_iterator__next(&itr);
371 void evlist_cpu_iterator__next(struct evlist_cpu_iterator *evlist_cpu_itr)
373 while (evlist_cpu_itr->evsel != evlist__last(evlist_cpu_itr->container)) {
374 evlist_cpu_itr->evsel = evsel__next(evlist_cpu_itr->evsel);
375 evlist_cpu_itr->cpu_map_idx =
376 perf_cpu_map__idx(evlist_cpu_itr->evsel->core.cpus,
377 evlist_cpu_itr->cpu);
378 if (evlist_cpu_itr->cpu_map_idx != -1)
381 evlist_cpu_itr->evlist_cpu_map_idx++;
382 if (evlist_cpu_itr->evlist_cpu_map_idx < evlist_cpu_itr->evlist_cpu_map_nr) {
383 evlist_cpu_itr->evsel = evlist__first(evlist_cpu_itr->container);
384 evlist_cpu_itr->cpu =
385 perf_cpu_map__cpu(evlist_cpu_itr->container->core.all_cpus,
386 evlist_cpu_itr->evlist_cpu_map_idx);
387 if (evlist_cpu_itr->affinity)
388 affinity__set(evlist_cpu_itr->affinity, evlist_cpu_itr->cpu.cpu);
389 evlist_cpu_itr->cpu_map_idx =
390 perf_cpu_map__idx(evlist_cpu_itr->evsel->core.cpus,
391 evlist_cpu_itr->cpu);
393 * If this CPU isn't in the evsel's cpu map then advance through
396 if (evlist_cpu_itr->cpu_map_idx == -1)
397 evlist_cpu_iterator__next(evlist_cpu_itr);
401 bool evlist_cpu_iterator__end(const struct evlist_cpu_iterator *evlist_cpu_itr)
403 return evlist_cpu_itr->evlist_cpu_map_idx >= evlist_cpu_itr->evlist_cpu_map_nr;
406 static int evsel__strcmp(struct evsel *pos, char *evsel_name)
410 if (evsel__is_dummy_event(pos))
412 return strcmp(pos->name, evsel_name);
415 static int evlist__is_enabled(struct evlist *evlist)
419 evlist__for_each_entry(evlist, pos) {
420 if (!evsel__is_group_leader(pos) || !pos->core.fd)
422 /* If at least one event is enabled, evlist is enabled. */
429 static void __evlist__disable(struct evlist *evlist, char *evsel_name)
432 struct evlist_cpu_iterator evlist_cpu_itr;
433 struct affinity saved_affinity, *affinity = NULL;
434 bool has_imm = false;
436 // See explanation in evlist__close()
437 if (!cpu_map__is_dummy(evlist->core.cpus)) {
438 if (affinity__setup(&saved_affinity) < 0)
440 affinity = &saved_affinity;
443 /* Disable 'immediate' events last */
444 for (int imm = 0; imm <= 1; imm++) {
445 evlist__for_each_cpu(evlist_cpu_itr, evlist, affinity) {
446 pos = evlist_cpu_itr.evsel;
447 if (evsel__strcmp(pos, evsel_name))
449 if (pos->disabled || !evsel__is_group_leader(pos) || !pos->core.fd)
453 if (pos->immediate != imm)
455 evsel__disable_cpu(pos, evlist_cpu_itr.cpu_map_idx);
461 affinity__cleanup(affinity);
462 evlist__for_each_entry(evlist, pos) {
463 if (evsel__strcmp(pos, evsel_name))
465 if (!evsel__is_group_leader(pos) || !pos->core.fd)
467 pos->disabled = true;
471 * If we disabled only single event, we need to check
472 * the enabled state of the evlist manually.
475 evlist->enabled = evlist__is_enabled(evlist);
477 evlist->enabled = false;
480 void evlist__disable(struct evlist *evlist)
482 __evlist__disable(evlist, NULL);
485 void evlist__disable_evsel(struct evlist *evlist, char *evsel_name)
487 __evlist__disable(evlist, evsel_name);
490 static void __evlist__enable(struct evlist *evlist, char *evsel_name)
493 struct evlist_cpu_iterator evlist_cpu_itr;
494 struct affinity saved_affinity, *affinity = NULL;
496 // See explanation in evlist__close()
497 if (!cpu_map__is_dummy(evlist->core.cpus)) {
498 if (affinity__setup(&saved_affinity) < 0)
500 affinity = &saved_affinity;
503 evlist__for_each_cpu(evlist_cpu_itr, evlist, affinity) {
504 pos = evlist_cpu_itr.evsel;
505 if (evsel__strcmp(pos, evsel_name))
507 if (!evsel__is_group_leader(pos) || !pos->core.fd)
509 evsel__enable_cpu(pos, evlist_cpu_itr.cpu_map_idx);
511 affinity__cleanup(affinity);
512 evlist__for_each_entry(evlist, pos) {
513 if (evsel__strcmp(pos, evsel_name))
515 if (!evsel__is_group_leader(pos) || !pos->core.fd)
517 pos->disabled = false;
521 * Even single event sets the 'enabled' for evlist,
522 * so the toggle can work properly and toggle to
525 evlist->enabled = true;
528 void evlist__enable(struct evlist *evlist)
530 __evlist__enable(evlist, NULL);
533 void evlist__enable_evsel(struct evlist *evlist, char *evsel_name)
535 __evlist__enable(evlist, evsel_name);
538 void evlist__toggle_enable(struct evlist *evlist)
540 (evlist->enabled ? evlist__disable : evlist__enable)(evlist);
543 static int evlist__enable_event_cpu(struct evlist *evlist, struct evsel *evsel, int cpu)
546 int nr_threads = evlist__nr_threads(evlist, evsel);
551 for (thread = 0; thread < nr_threads; thread++) {
552 int err = ioctl(FD(evsel, cpu, thread), PERF_EVENT_IOC_ENABLE, 0);
559 static int evlist__enable_event_thread(struct evlist *evlist, struct evsel *evsel, int thread)
562 int nr_cpus = perf_cpu_map__nr(evlist->core.cpus);
567 for (cpu = 0; cpu < nr_cpus; cpu++) {
568 int err = ioctl(FD(evsel, cpu, thread), PERF_EVENT_IOC_ENABLE, 0);
575 int evlist__enable_event_idx(struct evlist *evlist, struct evsel *evsel, int idx)
577 bool per_cpu_mmaps = !perf_cpu_map__empty(evlist->core.cpus);
580 return evlist__enable_event_cpu(evlist, evsel, idx);
582 return evlist__enable_event_thread(evlist, evsel, idx);
585 int evlist__add_pollfd(struct evlist *evlist, int fd)
587 return perf_evlist__add_pollfd(&evlist->core, fd, NULL, POLLIN, fdarray_flag__default);
590 int evlist__filter_pollfd(struct evlist *evlist, short revents_and_mask)
592 return perf_evlist__filter_pollfd(&evlist->core, revents_and_mask);
595 #ifdef HAVE_EVENTFD_SUPPORT
596 int evlist__add_wakeup_eventfd(struct evlist *evlist, int fd)
598 return perf_evlist__add_pollfd(&evlist->core, fd, NULL, POLLIN,
599 fdarray_flag__nonfilterable);
603 int evlist__poll(struct evlist *evlist, int timeout)
605 return perf_evlist__poll(&evlist->core, timeout);
608 struct perf_sample_id *evlist__id2sid(struct evlist *evlist, u64 id)
610 struct hlist_head *head;
611 struct perf_sample_id *sid;
614 hash = hash_64(id, PERF_EVLIST__HLIST_BITS);
615 head = &evlist->core.heads[hash];
617 hlist_for_each_entry(sid, head, node)
624 struct evsel *evlist__id2evsel(struct evlist *evlist, u64 id)
626 struct perf_sample_id *sid;
628 if (evlist->core.nr_entries == 1 || !id)
629 return evlist__first(evlist);
631 sid = evlist__id2sid(evlist, id);
633 return container_of(sid->evsel, struct evsel, core);
635 if (!evlist__sample_id_all(evlist))
636 return evlist__first(evlist);
641 struct evsel *evlist__id2evsel_strict(struct evlist *evlist, u64 id)
643 struct perf_sample_id *sid;
648 sid = evlist__id2sid(evlist, id);
650 return container_of(sid->evsel, struct evsel, core);
655 static int evlist__event2id(struct evlist *evlist, union perf_event *event, u64 *id)
657 const __u64 *array = event->sample.array;
660 n = (event->header.size - sizeof(event->header)) >> 3;
662 if (event->header.type == PERF_RECORD_SAMPLE) {
663 if (evlist->id_pos >= n)
665 *id = array[evlist->id_pos];
667 if (evlist->is_pos > n)
675 struct evsel *evlist__event2evsel(struct evlist *evlist, union perf_event *event)
677 struct evsel *first = evlist__first(evlist);
678 struct hlist_head *head;
679 struct perf_sample_id *sid;
683 if (evlist->core.nr_entries == 1)
686 if (!first->core.attr.sample_id_all &&
687 event->header.type != PERF_RECORD_SAMPLE)
690 if (evlist__event2id(evlist, event, &id))
693 /* Synthesized events have an id of zero */
697 hash = hash_64(id, PERF_EVLIST__HLIST_BITS);
698 head = &evlist->core.heads[hash];
700 hlist_for_each_entry(sid, head, node) {
702 return container_of(sid->evsel, struct evsel, core);
707 static int evlist__set_paused(struct evlist *evlist, bool value)
711 if (!evlist->overwrite_mmap)
714 for (i = 0; i < evlist->core.nr_mmaps; i++) {
715 int fd = evlist->overwrite_mmap[i].core.fd;
720 err = ioctl(fd, PERF_EVENT_IOC_PAUSE_OUTPUT, value ? 1 : 0);
727 static int evlist__pause(struct evlist *evlist)
729 return evlist__set_paused(evlist, true);
732 static int evlist__resume(struct evlist *evlist)
734 return evlist__set_paused(evlist, false);
737 static void evlist__munmap_nofree(struct evlist *evlist)
742 for (i = 0; i < evlist->core.nr_mmaps; i++)
743 perf_mmap__munmap(&evlist->mmap[i].core);
745 if (evlist->overwrite_mmap)
746 for (i = 0; i < evlist->core.nr_mmaps; i++)
747 perf_mmap__munmap(&evlist->overwrite_mmap[i].core);
750 void evlist__munmap(struct evlist *evlist)
752 evlist__munmap_nofree(evlist);
753 zfree(&evlist->mmap);
754 zfree(&evlist->overwrite_mmap);
757 static void perf_mmap__unmap_cb(struct perf_mmap *map)
759 struct mmap *m = container_of(map, struct mmap, core);
764 static struct mmap *evlist__alloc_mmap(struct evlist *evlist,
770 map = zalloc(evlist->core.nr_mmaps * sizeof(struct mmap));
774 for (i = 0; i < evlist->core.nr_mmaps; i++) {
775 struct perf_mmap *prev = i ? &map[i - 1].core : NULL;
778 * When the perf_mmap() call is made we grab one refcount, plus
779 * one extra to let perf_mmap__consume() get the last
780 * events after all real references (perf_mmap__get()) are
783 * Each PERF_EVENT_IOC_SET_OUTPUT points to this mmap and
784 * thus does perf_mmap__get() on it.
786 perf_mmap__init(&map[i].core, prev, overwrite, perf_mmap__unmap_cb);
793 perf_evlist__mmap_cb_idx(struct perf_evlist *_evlist,
794 struct perf_mmap_param *_mp,
795 int idx, bool per_cpu)
797 struct evlist *evlist = container_of(_evlist, struct evlist, core);
798 struct mmap_params *mp = container_of(_mp, struct mmap_params, core);
800 auxtrace_mmap_params__set_idx(&mp->auxtrace_mp, evlist, idx, per_cpu);
803 static struct perf_mmap*
804 perf_evlist__mmap_cb_get(struct perf_evlist *_evlist, bool overwrite, int idx)
806 struct evlist *evlist = container_of(_evlist, struct evlist, core);
809 maps = overwrite ? evlist->overwrite_mmap : evlist->mmap;
812 maps = evlist__alloc_mmap(evlist, overwrite);
817 evlist->overwrite_mmap = maps;
818 if (evlist->bkw_mmap_state == BKW_MMAP_NOTREADY)
819 evlist__toggle_bkw_mmap(evlist, BKW_MMAP_RUNNING);
825 return &maps[idx].core;
829 perf_evlist__mmap_cb_mmap(struct perf_mmap *_map, struct perf_mmap_param *_mp,
830 int output, struct perf_cpu cpu)
832 struct mmap *map = container_of(_map, struct mmap, core);
833 struct mmap_params *mp = container_of(_mp, struct mmap_params, core);
835 return mmap__mmap(map, mp, output, cpu);
838 unsigned long perf_event_mlock_kb_in_pages(void)
843 if (sysctl__read_int("kernel/perf_event_mlock_kb", &max) < 0) {
845 * Pick a once upon a time good value, i.e. things look
846 * strange since we can't read a sysctl value, but lets not
851 max -= (page_size / 1024);
854 pages = (max * 1024) / page_size;
855 if (!is_power_of_2(pages))
856 pages = rounddown_pow_of_two(pages);
861 size_t evlist__mmap_size(unsigned long pages)
863 if (pages == UINT_MAX)
864 pages = perf_event_mlock_kb_in_pages();
865 else if (!is_power_of_2(pages))
868 return (pages + 1) * page_size;
871 static long parse_pages_arg(const char *str, unsigned long min,
874 unsigned long pages, val;
875 static struct parse_tag tags[] = {
876 { .tag = 'B', .mult = 1 },
877 { .tag = 'K', .mult = 1 << 10 },
878 { .tag = 'M', .mult = 1 << 20 },
879 { .tag = 'G', .mult = 1 << 30 },
886 val = parse_tag_value(str, tags);
887 if (val != (unsigned long) -1) {
888 /* we got file size value */
889 pages = PERF_ALIGN(val, page_size) / page_size;
891 /* we got pages count value */
893 pages = strtoul(str, &eptr, 10);
898 if (pages == 0 && min == 0) {
899 /* leave number of pages at 0 */
900 } else if (!is_power_of_2(pages)) {
903 /* round pages up to next power of 2 */
904 pages = roundup_pow_of_two(pages);
908 unit_number__scnprintf(buf, sizeof(buf), pages * page_size);
909 pr_info("rounding mmap pages size to %s (%lu pages)\n",
919 int __evlist__parse_mmap_pages(unsigned int *mmap_pages, const char *str)
921 unsigned long max = UINT_MAX;
924 if (max > SIZE_MAX / page_size)
925 max = SIZE_MAX / page_size;
927 pages = parse_pages_arg(str, 1, max);
929 pr_err("Invalid argument for --mmap_pages/-m\n");
937 int evlist__parse_mmap_pages(const struct option *opt, const char *str, int unset __maybe_unused)
939 return __evlist__parse_mmap_pages(opt->value, str);
943 * evlist__mmap_ex - Create mmaps to receive events.
944 * @evlist: list of events
945 * @pages: map length in pages
946 * @overwrite: overwrite older events?
947 * @auxtrace_pages - auxtrace map length in pages
948 * @auxtrace_overwrite - overwrite older auxtrace data?
950 * If @overwrite is %false the user needs to signal event consumption using
951 * perf_mmap__write_tail(). Using evlist__mmap_read() does this
954 * Similarly, if @auxtrace_overwrite is %false the user needs to signal data
955 * consumption using auxtrace_mmap__write_tail().
957 * Return: %0 on success, negative error code otherwise.
959 int evlist__mmap_ex(struct evlist *evlist, unsigned int pages,
960 unsigned int auxtrace_pages,
961 bool auxtrace_overwrite, int nr_cblocks, int affinity, int flush,
965 * Delay setting mp.prot: set it before calling perf_mmap__mmap.
966 * Its value is decided by evsel's write_backward.
967 * So &mp should not be passed through const pointer.
969 struct mmap_params mp = {
970 .nr_cblocks = nr_cblocks,
971 .affinity = affinity,
973 .comp_level = comp_level
975 struct perf_evlist_mmap_ops ops = {
976 .idx = perf_evlist__mmap_cb_idx,
977 .get = perf_evlist__mmap_cb_get,
978 .mmap = perf_evlist__mmap_cb_mmap,
981 evlist->core.mmap_len = evlist__mmap_size(pages);
982 pr_debug("mmap size %zuB\n", evlist->core.mmap_len);
984 auxtrace_mmap_params__init(&mp.auxtrace_mp, evlist->core.mmap_len,
985 auxtrace_pages, auxtrace_overwrite);
987 return perf_evlist__mmap_ops(&evlist->core, &ops, &mp.core);
990 int evlist__mmap(struct evlist *evlist, unsigned int pages)
992 return evlist__mmap_ex(evlist, pages, 0, false, 0, PERF_AFFINITY_SYS, 1, 0);
995 int evlist__create_maps(struct evlist *evlist, struct target *target)
997 bool all_threads = (target->per_thread && target->system_wide);
998 struct perf_cpu_map *cpus;
999 struct perf_thread_map *threads;
1002 * If specify '-a' and '--per-thread' to perf record, perf record
1003 * will override '--per-thread'. target->per_thread = false and
1004 * target->system_wide = true.
1006 * If specify '--per-thread' only to perf record,
1007 * target->per_thread = true and target->system_wide = false.
1009 * So target->per_thread && target->system_wide is false.
1010 * For perf record, thread_map__new_str doesn't call
1011 * thread_map__new_all_cpus. That will keep perf record's
1014 * For perf stat, it allows the case that target->per_thread and
1015 * target->system_wide are all true. It means to collect system-wide
1016 * per-thread data. thread_map__new_str will call
1017 * thread_map__new_all_cpus to enumerate all threads.
1019 threads = thread_map__new_str(target->pid, target->tid, target->uid,
1025 if (target__uses_dummy_map(target))
1026 cpus = perf_cpu_map__dummy_new();
1028 cpus = perf_cpu_map__new(target->cpu_list);
1031 goto out_delete_threads;
1033 evlist->core.has_user_cpus = !!target->cpu_list && !target->hybrid;
1035 perf_evlist__set_maps(&evlist->core, cpus, threads);
1037 /* as evlist now has references, put count here */
1038 perf_cpu_map__put(cpus);
1039 perf_thread_map__put(threads);
1044 perf_thread_map__put(threads);
1048 int evlist__apply_filters(struct evlist *evlist, struct evsel **err_evsel)
1050 struct evsel *evsel;
1053 evlist__for_each_entry(evlist, evsel) {
1054 if (evsel->filter == NULL)
1058 * filters only work for tracepoint event, which doesn't have cpu limit.
1059 * So evlist and evsel should always be same.
1061 err = perf_evsel__apply_filter(&evsel->core, evsel->filter);
1071 int evlist__set_tp_filter(struct evlist *evlist, const char *filter)
1073 struct evsel *evsel;
1079 evlist__for_each_entry(evlist, evsel) {
1080 if (evsel->core.attr.type != PERF_TYPE_TRACEPOINT)
1083 err = evsel__set_filter(evsel, filter);
1091 int evlist__append_tp_filter(struct evlist *evlist, const char *filter)
1093 struct evsel *evsel;
1099 evlist__for_each_entry(evlist, evsel) {
1100 if (evsel->core.attr.type != PERF_TYPE_TRACEPOINT)
1103 err = evsel__append_tp_filter(evsel, filter);
1111 char *asprintf__tp_filter_pids(size_t npids, pid_t *pids)
1116 for (i = 0; i < npids; ++i) {
1118 if (asprintf(&filter, "common_pid != %d", pids[i]) < 0)
1123 if (asprintf(&tmp, "%s && common_pid != %d", filter, pids[i]) < 0)
1137 int evlist__set_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__set_tp_filter(evlist, filter);
1146 int evlist__set_tp_filter_pid(struct evlist *evlist, pid_t pid)
1148 return evlist__set_tp_filter_pids(evlist, 1, &pid);
1151 int evlist__append_tp_filter_pids(struct evlist *evlist, size_t npids, pid_t *pids)
1153 char *filter = asprintf__tp_filter_pids(npids, pids);
1154 int ret = evlist__append_tp_filter(evlist, filter);
1160 int evlist__append_tp_filter_pid(struct evlist *evlist, pid_t pid)
1162 return evlist__append_tp_filter_pids(evlist, 1, &pid);
1165 bool evlist__valid_sample_type(struct evlist *evlist)
1169 if (evlist->core.nr_entries == 1)
1172 if (evlist->id_pos < 0 || evlist->is_pos < 0)
1175 evlist__for_each_entry(evlist, pos) {
1176 if (pos->id_pos != evlist->id_pos ||
1177 pos->is_pos != evlist->is_pos)
1184 u64 __evlist__combined_sample_type(struct evlist *evlist)
1186 struct evsel *evsel;
1188 if (evlist->combined_sample_type)
1189 return evlist->combined_sample_type;
1191 evlist__for_each_entry(evlist, evsel)
1192 evlist->combined_sample_type |= evsel->core.attr.sample_type;
1194 return evlist->combined_sample_type;
1197 u64 evlist__combined_sample_type(struct evlist *evlist)
1199 evlist->combined_sample_type = 0;
1200 return __evlist__combined_sample_type(evlist);
1203 u64 evlist__combined_branch_type(struct evlist *evlist)
1205 struct evsel *evsel;
1206 u64 branch_type = 0;
1208 evlist__for_each_entry(evlist, evsel)
1209 branch_type |= evsel->core.attr.branch_sample_type;
1213 bool evlist__valid_read_format(struct evlist *evlist)
1215 struct evsel *first = evlist__first(evlist), *pos = first;
1216 u64 read_format = first->core.attr.read_format;
1217 u64 sample_type = first->core.attr.sample_type;
1219 evlist__for_each_entry(evlist, pos) {
1220 if (read_format != pos->core.attr.read_format) {
1221 pr_debug("Read format differs %#" PRIx64 " vs %#" PRIx64 "\n",
1222 read_format, (u64)pos->core.attr.read_format);
1226 /* PERF_SAMPLE_READ implies PERF_FORMAT_ID. */
1227 if ((sample_type & PERF_SAMPLE_READ) &&
1228 !(read_format & PERF_FORMAT_ID)) {
1235 u16 evlist__id_hdr_size(struct evlist *evlist)
1237 struct evsel *first = evlist__first(evlist);
1238 struct perf_sample *data;
1242 if (!first->core.attr.sample_id_all)
1245 sample_type = first->core.attr.sample_type;
1247 if (sample_type & PERF_SAMPLE_TID)
1248 size += sizeof(data->tid) * 2;
1250 if (sample_type & PERF_SAMPLE_TIME)
1251 size += sizeof(data->time);
1253 if (sample_type & PERF_SAMPLE_ID)
1254 size += sizeof(data->id);
1256 if (sample_type & PERF_SAMPLE_STREAM_ID)
1257 size += sizeof(data->stream_id);
1259 if (sample_type & PERF_SAMPLE_CPU)
1260 size += sizeof(data->cpu) * 2;
1262 if (sample_type & PERF_SAMPLE_IDENTIFIER)
1263 size += sizeof(data->id);
1268 bool evlist__valid_sample_id_all(struct evlist *evlist)
1270 struct evsel *first = evlist__first(evlist), *pos = first;
1272 evlist__for_each_entry_continue(evlist, pos) {
1273 if (first->core.attr.sample_id_all != pos->core.attr.sample_id_all)
1280 bool evlist__sample_id_all(struct evlist *evlist)
1282 struct evsel *first = evlist__first(evlist);
1283 return first->core.attr.sample_id_all;
1286 void evlist__set_selected(struct evlist *evlist, struct evsel *evsel)
1288 evlist->selected = evsel;
1291 void evlist__close(struct evlist *evlist)
1293 struct evsel *evsel;
1294 struct evlist_cpu_iterator evlist_cpu_itr;
1295 struct affinity affinity;
1298 * With perf record core.cpus is usually NULL.
1299 * Use the old method to handle this for now.
1301 if (!evlist->core.cpus || cpu_map__is_dummy(evlist->core.cpus)) {
1302 evlist__for_each_entry_reverse(evlist, evsel)
1303 evsel__close(evsel);
1307 if (affinity__setup(&affinity) < 0)
1310 evlist__for_each_cpu(evlist_cpu_itr, evlist, &affinity) {
1311 perf_evsel__close_cpu(&evlist_cpu_itr.evsel->core,
1312 evlist_cpu_itr.cpu_map_idx);
1315 affinity__cleanup(&affinity);
1316 evlist__for_each_entry_reverse(evlist, evsel) {
1317 perf_evsel__free_fd(&evsel->core);
1318 perf_evsel__free_id(&evsel->core);
1320 perf_evlist__reset_id_hash(&evlist->core);
1323 static int evlist__create_syswide_maps(struct evlist *evlist)
1325 struct perf_cpu_map *cpus;
1326 struct perf_thread_map *threads;
1330 * Try reading /sys/devices/system/cpu/online to get
1333 * FIXME: -ENOMEM is the best we can do here, the cpu_map
1334 * code needs an overhaul to properly forward the
1335 * error, and we may not want to do that fallback to a
1336 * default cpu identity map :-\
1338 cpus = perf_cpu_map__new(NULL);
1342 threads = perf_thread_map__new_dummy();
1346 perf_evlist__set_maps(&evlist->core, cpus, threads);
1348 perf_thread_map__put(threads);
1350 perf_cpu_map__put(cpus);
1355 int evlist__open(struct evlist *evlist)
1357 struct evsel *evsel;
1361 * Default: one fd per CPU, all threads, aka systemwide
1362 * as sys_perf_event_open(cpu = -1, thread = -1) is EINVAL
1364 if (evlist->core.threads == NULL && evlist->core.cpus == NULL) {
1365 err = evlist__create_syswide_maps(evlist);
1370 evlist__update_id_pos(evlist);
1372 evlist__for_each_entry(evlist, evsel) {
1373 err = evsel__open(evsel, evsel->core.cpus, evsel->core.threads);
1380 evlist__close(evlist);
1385 int evlist__prepare_workload(struct evlist *evlist, struct target *target, const char *argv[],
1386 bool pipe_output, void (*exec_error)(int signo, siginfo_t *info, void *ucontext))
1388 int child_ready_pipe[2], go_pipe[2];
1391 if (pipe(child_ready_pipe) < 0) {
1392 perror("failed to create 'ready' pipe");
1396 if (pipe(go_pipe) < 0) {
1397 perror("failed to create 'go' pipe");
1398 goto out_close_ready_pipe;
1401 evlist->workload.pid = fork();
1402 if (evlist->workload.pid < 0) {
1403 perror("failed to fork");
1404 goto out_close_pipes;
1407 if (!evlist->workload.pid) {
1413 signal(SIGTERM, SIG_DFL);
1415 close(child_ready_pipe[0]);
1417 fcntl(go_pipe[0], F_SETFD, FD_CLOEXEC);
1420 * Change the name of this process not to confuse --exclude-perf users
1421 * that sees 'perf' in the window up to the execvp() and thinks that
1422 * perf samples are not being excluded.
1424 prctl(PR_SET_NAME, "perf-exec");
1427 * Tell the parent we're ready to go
1429 close(child_ready_pipe[1]);
1432 * Wait until the parent tells us to go.
1434 ret = read(go_pipe[0], &bf, 1);
1436 * The parent will ask for the execvp() to be performed by
1437 * writing exactly one byte, in workload.cork_fd, usually via
1438 * evlist__start_workload().
1440 * For cancelling the workload without actually running it,
1441 * the parent will just close workload.cork_fd, without writing
1442 * anything, i.e. read will return zero and we just exit()
1447 perror("unable to read pipe");
1451 execvp(argv[0], (char **)argv);
1456 val.sival_int = errno;
1457 if (sigqueue(getppid(), SIGUSR1, val))
1465 struct sigaction act = {
1466 .sa_flags = SA_SIGINFO,
1467 .sa_sigaction = exec_error,
1469 sigaction(SIGUSR1, &act, NULL);
1472 if (target__none(target)) {
1473 if (evlist->core.threads == NULL) {
1474 fprintf(stderr, "FATAL: evlist->threads need to be set at this point (%s:%d).\n",
1475 __func__, __LINE__);
1476 goto out_close_pipes;
1478 perf_thread_map__set_pid(evlist->core.threads, 0, evlist->workload.pid);
1481 close(child_ready_pipe[1]);
1484 * wait for child to settle
1486 if (read(child_ready_pipe[0], &bf, 1) == -1) {
1487 perror("unable to read pipe");
1488 goto out_close_pipes;
1491 fcntl(go_pipe[1], F_SETFD, FD_CLOEXEC);
1492 evlist->workload.cork_fd = go_pipe[1];
1493 close(child_ready_pipe[0]);
1499 out_close_ready_pipe:
1500 close(child_ready_pipe[0]);
1501 close(child_ready_pipe[1]);
1505 int evlist__start_workload(struct evlist *evlist)
1507 if (evlist->workload.cork_fd > 0) {
1511 * Remove the cork, let it rip!
1513 ret = write(evlist->workload.cork_fd, &bf, 1);
1515 perror("unable to write to pipe");
1517 close(evlist->workload.cork_fd);
1524 int evlist__parse_sample(struct evlist *evlist, union perf_event *event, struct perf_sample *sample)
1526 struct evsel *evsel = evlist__event2evsel(evlist, event);
1530 return evsel__parse_sample(evsel, event, sample);
1533 int evlist__parse_sample_timestamp(struct evlist *evlist, union perf_event *event, u64 *timestamp)
1535 struct evsel *evsel = evlist__event2evsel(evlist, event);
1539 return evsel__parse_sample_timestamp(evsel, event, timestamp);
1542 int evlist__strerror_open(struct evlist *evlist, int err, char *buf, size_t size)
1545 char sbuf[STRERR_BUFSIZE], *emsg = str_error_r(err, sbuf, sizeof(sbuf));
1550 printed = scnprintf(buf, size,
1552 "Hint:\tCheck /proc/sys/kernel/perf_event_paranoid setting.", emsg);
1554 value = perf_event_paranoid();
1556 printed += scnprintf(buf + printed, size - printed, "\nHint:\t");
1559 printed += scnprintf(buf + printed, size - printed,
1560 "For your workloads it needs to be <= 1\nHint:\t");
1562 printed += scnprintf(buf + printed, size - printed,
1563 "For system wide tracing it needs to be set to -1.\n");
1565 printed += scnprintf(buf + printed, size - printed,
1566 "Hint:\tTry: 'sudo sh -c \"echo -1 > /proc/sys/kernel/perf_event_paranoid\"'\n"
1567 "Hint:\tThe current value is %d.", value);
1570 struct evsel *first = evlist__first(evlist);
1573 if (sysctl__read_int("kernel/perf_event_max_sample_rate", &max_freq) < 0)
1576 if (first->core.attr.sample_freq < (u64)max_freq)
1579 printed = scnprintf(buf, size,
1581 "Hint:\tCheck /proc/sys/kernel/perf_event_max_sample_rate.\n"
1582 "Hint:\tThe current value is %d and %" PRIu64 " is being requested.",
1583 emsg, max_freq, first->core.attr.sample_freq);
1588 scnprintf(buf, size, "%s", emsg);
1595 int evlist__strerror_mmap(struct evlist *evlist, int err, char *buf, size_t size)
1597 char sbuf[STRERR_BUFSIZE], *emsg = str_error_r(err, sbuf, sizeof(sbuf));
1598 int pages_attempted = evlist->core.mmap_len / 1024, pages_max_per_user, printed = 0;
1602 sysctl__read_int("kernel/perf_event_mlock_kb", &pages_max_per_user);
1603 printed += scnprintf(buf + printed, size - printed,
1605 "Hint:\tCheck /proc/sys/kernel/perf_event_mlock_kb (%d kB) setting.\n"
1606 "Hint:\tTried using %zd kB.\n",
1607 emsg, pages_max_per_user, pages_attempted);
1609 if (pages_attempted >= pages_max_per_user) {
1610 printed += scnprintf(buf + printed, size - printed,
1611 "Hint:\tTry 'sudo sh -c \"echo %d > /proc/sys/kernel/perf_event_mlock_kb\"', or\n",
1612 pages_max_per_user + pages_attempted);
1615 printed += scnprintf(buf + printed, size - printed,
1616 "Hint:\tTry using a smaller -m/--mmap-pages value.");
1619 scnprintf(buf, size, "%s", emsg);
1626 void evlist__to_front(struct evlist *evlist, struct evsel *move_evsel)
1628 struct evsel *evsel, *n;
1631 if (move_evsel == evlist__first(evlist))
1634 evlist__for_each_entry_safe(evlist, n, evsel) {
1635 if (evsel__leader(evsel) == evsel__leader(move_evsel))
1636 list_move_tail(&evsel->core.node, &move);
1639 list_splice(&move, &evlist->core.entries);
1642 struct evsel *evlist__get_tracking_event(struct evlist *evlist)
1644 struct evsel *evsel;
1646 evlist__for_each_entry(evlist, evsel) {
1647 if (evsel->tracking)
1651 return evlist__first(evlist);
1654 void evlist__set_tracking_event(struct evlist *evlist, struct evsel *tracking_evsel)
1656 struct evsel *evsel;
1658 if (tracking_evsel->tracking)
1661 evlist__for_each_entry(evlist, evsel) {
1662 if (evsel != tracking_evsel)
1663 evsel->tracking = false;
1666 tracking_evsel->tracking = true;
1669 struct evsel *evlist__find_evsel_by_str(struct evlist *evlist, const char *str)
1671 struct evsel *evsel;
1673 evlist__for_each_entry(evlist, evsel) {
1676 if (strcmp(str, evsel->name) == 0)
1683 void evlist__toggle_bkw_mmap(struct evlist *evlist, enum bkw_mmap_state state)
1685 enum bkw_mmap_state old_state = evlist->bkw_mmap_state;
1692 if (!evlist->overwrite_mmap)
1695 switch (old_state) {
1696 case BKW_MMAP_NOTREADY: {
1697 if (state != BKW_MMAP_RUNNING)
1701 case BKW_MMAP_RUNNING: {
1702 if (state != BKW_MMAP_DATA_PENDING)
1707 case BKW_MMAP_DATA_PENDING: {
1708 if (state != BKW_MMAP_EMPTY)
1712 case BKW_MMAP_EMPTY: {
1713 if (state != BKW_MMAP_RUNNING)
1719 WARN_ONCE(1, "Shouldn't get there\n");
1722 evlist->bkw_mmap_state = state;
1726 evlist__pause(evlist);
1729 evlist__resume(evlist);
1740 bool evlist__exclude_kernel(struct evlist *evlist)
1742 struct evsel *evsel;
1744 evlist__for_each_entry(evlist, evsel) {
1745 if (!evsel->core.attr.exclude_kernel)
1753 * Events in data file are not collect in groups, but we still want
1754 * the group display. Set the artificial group and set the leader's
1755 * forced_leader flag to notify the display code.
1757 void evlist__force_leader(struct evlist *evlist)
1759 if (!evlist->core.nr_groups) {
1760 struct evsel *leader = evlist__first(evlist);
1762 evlist__set_leader(evlist);
1763 leader->forced_leader = true;
1767 struct evsel *evlist__reset_weak_group(struct evlist *evsel_list, struct evsel *evsel, bool close)
1769 struct evsel *c2, *leader;
1770 bool is_open = true;
1772 leader = evsel__leader(evsel);
1774 pr_debug("Weak group for %s/%d failed\n",
1775 leader->name, leader->core.nr_members);
1778 * for_each_group_member doesn't work here because it doesn't
1779 * include the first entry.
1781 evlist__for_each_entry(evsel_list, c2) {
1784 if (evsel__has_leader(c2, leader)) {
1785 if (is_open && close)
1786 perf_evsel__close(&c2->core);
1787 evsel__set_leader(c2, c2);
1788 c2->core.nr_members = 0;
1790 * Set this for all former members of the group
1791 * to indicate they get reopened.
1793 c2->reset_group = true;
1799 static int evlist__parse_control_fifo(const char *str, int *ctl_fd, int *ctl_fd_ack, bool *ctl_fd_close)
1804 if (strncmp(str, "fifo:", 5))
1808 if (!*str || *str == ',')
1820 * O_RDWR avoids POLLHUPs which is necessary to allow the other
1821 * end of a FIFO to be repeatedly opened and closed.
1823 fd = open(s, O_RDWR | O_NONBLOCK | O_CLOEXEC);
1825 pr_err("Failed to open '%s'\n", s);
1830 *ctl_fd_close = true;
1833 /* O_RDWR | O_NONBLOCK means the other end need not be open */
1834 fd = open(p, O_RDWR | O_NONBLOCK | O_CLOEXEC);
1836 pr_err("Failed to open '%s'\n", p);
1848 int evlist__parse_control(const char *str, int *ctl_fd, int *ctl_fd_ack, bool *ctl_fd_close)
1850 char *comma = NULL, *endptr = NULL;
1852 *ctl_fd_close = false;
1854 if (strncmp(str, "fd:", 3))
1855 return evlist__parse_control_fifo(str, ctl_fd, ctl_fd_ack, ctl_fd_close);
1857 *ctl_fd = strtoul(&str[3], &endptr, 0);
1858 if (endptr == &str[3])
1861 comma = strchr(str, ',');
1863 if (endptr != comma)
1866 *ctl_fd_ack = strtoul(comma + 1, &endptr, 0);
1867 if (endptr == comma + 1 || *endptr != '\0')
1874 void evlist__close_control(int ctl_fd, int ctl_fd_ack, bool *ctl_fd_close)
1876 if (*ctl_fd_close) {
1877 *ctl_fd_close = false;
1879 if (ctl_fd_ack >= 0)
1884 int evlist__initialize_ctlfd(struct evlist *evlist, int fd, int ack)
1887 pr_debug("Control descriptor is not initialized\n");
1891 evlist->ctl_fd.pos = perf_evlist__add_pollfd(&evlist->core, fd, NULL, POLLIN,
1892 fdarray_flag__nonfilterable);
1893 if (evlist->ctl_fd.pos < 0) {
1894 evlist->ctl_fd.pos = -1;
1895 pr_err("Failed to add ctl fd entry: %m\n");
1899 evlist->ctl_fd.fd = fd;
1900 evlist->ctl_fd.ack = ack;
1905 bool evlist__ctlfd_initialized(struct evlist *evlist)
1907 return evlist->ctl_fd.pos >= 0;
1910 int evlist__finalize_ctlfd(struct evlist *evlist)
1912 struct pollfd *entries = evlist->core.pollfd.entries;
1914 if (!evlist__ctlfd_initialized(evlist))
1917 entries[evlist->ctl_fd.pos].fd = -1;
1918 entries[evlist->ctl_fd.pos].events = 0;
1919 entries[evlist->ctl_fd.pos].revents = 0;
1921 evlist->ctl_fd.pos = -1;
1922 evlist->ctl_fd.ack = -1;
1923 evlist->ctl_fd.fd = -1;
1928 static int evlist__ctlfd_recv(struct evlist *evlist, enum evlist_ctl_cmd *cmd,
1929 char *cmd_data, size_t data_size)
1933 size_t bytes_read = 0;
1935 *cmd = EVLIST_CTL_CMD_UNSUPPORTED;
1936 memset(cmd_data, 0, data_size);
1940 err = read(evlist->ctl_fd.fd, &c, 1);
1942 if (c == '\n' || c == '\0')
1944 cmd_data[bytes_read++] = c;
1945 if (bytes_read == data_size)
1948 } else if (err == -1) {
1951 if (errno == EAGAIN || errno == EWOULDBLOCK)
1954 pr_err("Failed to read from ctlfd %d: %m\n", evlist->ctl_fd.fd);
1959 pr_debug("Message from ctl_fd: \"%s%s\"\n", cmd_data,
1960 bytes_read == data_size ? "" : c == '\n' ? "\\n" : "\\0");
1962 if (bytes_read > 0) {
1963 if (!strncmp(cmd_data, EVLIST_CTL_CMD_ENABLE_TAG,
1964 (sizeof(EVLIST_CTL_CMD_ENABLE_TAG)-1))) {
1965 *cmd = EVLIST_CTL_CMD_ENABLE;
1966 } else if (!strncmp(cmd_data, EVLIST_CTL_CMD_DISABLE_TAG,
1967 (sizeof(EVLIST_CTL_CMD_DISABLE_TAG)-1))) {
1968 *cmd = EVLIST_CTL_CMD_DISABLE;
1969 } else if (!strncmp(cmd_data, EVLIST_CTL_CMD_SNAPSHOT_TAG,
1970 (sizeof(EVLIST_CTL_CMD_SNAPSHOT_TAG)-1))) {
1971 *cmd = EVLIST_CTL_CMD_SNAPSHOT;
1972 pr_debug("is snapshot\n");
1973 } else if (!strncmp(cmd_data, EVLIST_CTL_CMD_EVLIST_TAG,
1974 (sizeof(EVLIST_CTL_CMD_EVLIST_TAG)-1))) {
1975 *cmd = EVLIST_CTL_CMD_EVLIST;
1976 } else if (!strncmp(cmd_data, EVLIST_CTL_CMD_STOP_TAG,
1977 (sizeof(EVLIST_CTL_CMD_STOP_TAG)-1))) {
1978 *cmd = EVLIST_CTL_CMD_STOP;
1979 } else if (!strncmp(cmd_data, EVLIST_CTL_CMD_PING_TAG,
1980 (sizeof(EVLIST_CTL_CMD_PING_TAG)-1))) {
1981 *cmd = EVLIST_CTL_CMD_PING;
1985 return bytes_read ? (int)bytes_read : err;
1988 int evlist__ctlfd_ack(struct evlist *evlist)
1992 if (evlist->ctl_fd.ack == -1)
1995 err = write(evlist->ctl_fd.ack, EVLIST_CTL_CMD_ACK_TAG,
1996 sizeof(EVLIST_CTL_CMD_ACK_TAG));
1998 pr_err("failed to write to ctl_ack_fd %d: %m\n", evlist->ctl_fd.ack);
2003 static int get_cmd_arg(char *cmd_data, size_t cmd_size, char **arg)
2005 char *data = cmd_data + cmd_size;
2011 /* there's argument */
2021 static int evlist__ctlfd_enable(struct evlist *evlist, char *cmd_data, bool enable)
2023 struct evsel *evsel;
2027 err = get_cmd_arg(cmd_data,
2028 enable ? sizeof(EVLIST_CTL_CMD_ENABLE_TAG) - 1 :
2029 sizeof(EVLIST_CTL_CMD_DISABLE_TAG) - 1,
2032 pr_info("failed: wrong command\n");
2037 evsel = evlist__find_evsel_by_str(evlist, name);
2040 evlist__enable_evsel(evlist, name);
2042 evlist__disable_evsel(evlist, name);
2043 pr_info("Event %s %s\n", evsel->name,
2044 enable ? "enabled" : "disabled");
2046 pr_info("failed: can't find '%s' event\n", name);
2050 evlist__enable(evlist);
2051 pr_info(EVLIST_ENABLED_MSG);
2053 evlist__disable(evlist);
2054 pr_info(EVLIST_DISABLED_MSG);
2061 static int evlist__ctlfd_list(struct evlist *evlist, char *cmd_data)
2063 struct perf_attr_details details = { .verbose = false, };
2064 struct evsel *evsel;
2068 err = get_cmd_arg(cmd_data,
2069 sizeof(EVLIST_CTL_CMD_EVLIST_TAG) - 1,
2072 pr_info("failed: wrong command\n");
2077 if (!strcmp(arg, "-v")) {
2078 details.verbose = true;
2079 } else if (!strcmp(arg, "-g")) {
2080 details.event_group = true;
2081 } else if (!strcmp(arg, "-F")) {
2082 details.freq = true;
2084 pr_info("failed: wrong command\n");
2089 evlist__for_each_entry(evlist, evsel)
2090 evsel__fprintf(evsel, &details, stderr);
2095 int evlist__ctlfd_process(struct evlist *evlist, enum evlist_ctl_cmd *cmd)
2098 char cmd_data[EVLIST_CTL_CMD_MAX_LEN];
2099 int ctlfd_pos = evlist->ctl_fd.pos;
2100 struct pollfd *entries = evlist->core.pollfd.entries;
2102 if (!evlist__ctlfd_initialized(evlist) || !entries[ctlfd_pos].revents)
2105 if (entries[ctlfd_pos].revents & POLLIN) {
2106 err = evlist__ctlfd_recv(evlist, cmd, cmd_data,
2107 EVLIST_CTL_CMD_MAX_LEN);
2110 case EVLIST_CTL_CMD_ENABLE:
2111 case EVLIST_CTL_CMD_DISABLE:
2112 err = evlist__ctlfd_enable(evlist, cmd_data,
2113 *cmd == EVLIST_CTL_CMD_ENABLE);
2115 case EVLIST_CTL_CMD_EVLIST:
2116 err = evlist__ctlfd_list(evlist, cmd_data);
2118 case EVLIST_CTL_CMD_SNAPSHOT:
2119 case EVLIST_CTL_CMD_STOP:
2120 case EVLIST_CTL_CMD_PING:
2122 case EVLIST_CTL_CMD_ACK:
2123 case EVLIST_CTL_CMD_UNSUPPORTED:
2125 pr_debug("ctlfd: unsupported %d\n", *cmd);
2128 if (!(*cmd == EVLIST_CTL_CMD_ACK || *cmd == EVLIST_CTL_CMD_UNSUPPORTED ||
2129 *cmd == EVLIST_CTL_CMD_SNAPSHOT))
2130 evlist__ctlfd_ack(evlist);
2134 if (entries[ctlfd_pos].revents & (POLLHUP | POLLERR))
2135 evlist__finalize_ctlfd(evlist);
2137 entries[ctlfd_pos].revents = 0;
2142 struct evsel *evlist__find_evsel(struct evlist *evlist, int idx)
2144 struct evsel *evsel;
2146 evlist__for_each_entry(evlist, evsel) {
2147 if (evsel->core.idx == idx)
2153 int evlist__scnprintf_evsels(struct evlist *evlist, size_t size, char *bf)
2155 struct evsel *evsel;
2158 evlist__for_each_entry(evlist, evsel) {
2159 if (evsel__is_dummy_event(evsel))
2161 if (size > (strlen(evsel__name(evsel)) + (printed ? 2 : 1))) {
2162 printed += scnprintf(bf + printed, size - printed, "%s%s", printed ? "," : "", evsel__name(evsel));
2164 printed += scnprintf(bf + printed, size - printed, "%s...", printed ? "," : "");
2172 void evlist__check_mem_load_aux(struct evlist *evlist)
2174 struct evsel *leader, *evsel, *pos;
2177 * For some platforms, the 'mem-loads' event is required to use
2178 * together with 'mem-loads-aux' within a group and 'mem-loads-aux'
2179 * must be the group leader. Now we disable this group before reporting
2180 * because 'mem-loads-aux' is just an auxiliary event. It doesn't carry
2181 * any valid memory load information.
2183 evlist__for_each_entry(evlist, evsel) {
2184 leader = evsel__leader(evsel);
2185 if (leader == evsel)
2188 if (leader->name && strstr(leader->name, "mem-loads-aux")) {
2189 for_each_group_evsel(pos, leader) {
2190 evsel__set_leader(pos, pos);
2191 pos->core.nr_members = 0;