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 static struct evsel *evlist__dummy_event(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 */
253 return evsel__new_idx(&attr, evlist->core.nr_entries);
256 int evlist__add_dummy(struct evlist *evlist)
258 struct evsel *evsel = evlist__dummy_event(evlist);
263 evlist__add(evlist, evsel);
267 static void evlist__add_on_all_cpus(struct evlist *evlist, struct evsel *evsel)
269 evsel->core.system_wide = true;
274 * Note perf_event_open() does not accept CPUs that are not online, so
275 * in fact this CPU list will include only all online CPUs.
277 perf_cpu_map__put(evsel->core.own_cpus);
278 evsel->core.own_cpus = perf_cpu_map__new(NULL);
279 perf_cpu_map__put(evsel->core.cpus);
280 evsel->core.cpus = perf_cpu_map__get(evsel->core.own_cpus);
283 perf_thread_map__put(evsel->core.threads);
284 evsel->core.threads = perf_thread_map__new_dummy();
286 evlist__add(evlist, evsel);
289 struct evsel *evlist__add_aux_dummy(struct evlist *evlist, bool system_wide)
291 struct evsel *evsel = evlist__dummy_event(evlist);
296 evsel->core.attr.exclude_kernel = 1;
297 evsel->core.attr.exclude_guest = 1;
298 evsel->core.attr.exclude_hv = 1;
299 evsel->core.attr.freq = 0;
300 evsel->core.attr.sample_period = 1;
301 evsel->no_aux_samples = true;
302 evsel->name = strdup("dummy:u");
305 evlist__add_on_all_cpus(evlist, evsel);
307 evlist__add(evlist, evsel);
312 static int evlist__add_attrs(struct evlist *evlist, struct perf_event_attr *attrs, size_t nr_attrs)
314 struct evsel *evsel, *n;
318 for (i = 0; i < nr_attrs; i++) {
319 evsel = evsel__new_idx(attrs + i, evlist->core.nr_entries + i);
321 goto out_delete_partial_list;
322 list_add_tail(&evsel->core.node, &head);
325 evlist__splice_list_tail(evlist, &head);
329 out_delete_partial_list:
330 __evlist__for_each_entry_safe(&head, n, evsel)
331 evsel__delete(evsel);
335 int __evlist__add_default_attrs(struct evlist *evlist, struct perf_event_attr *attrs, size_t nr_attrs)
339 for (i = 0; i < nr_attrs; i++)
340 event_attr_init(attrs + i);
342 return evlist__add_attrs(evlist, attrs, nr_attrs);
345 __weak int arch_evlist__add_default_attrs(struct evlist *evlist __maybe_unused)
350 struct evsel *evlist__find_tracepoint_by_id(struct evlist *evlist, int id)
354 evlist__for_each_entry(evlist, evsel) {
355 if (evsel->core.attr.type == PERF_TYPE_TRACEPOINT &&
356 (int)evsel->core.attr.config == id)
363 struct evsel *evlist__find_tracepoint_by_name(struct evlist *evlist, const char *name)
367 evlist__for_each_entry(evlist, evsel) {
368 if ((evsel->core.attr.type == PERF_TYPE_TRACEPOINT) &&
369 (strcmp(evsel->name, name) == 0))
376 int evlist__add_newtp(struct evlist *evlist, const char *sys, const char *name, void *handler)
378 struct evsel *evsel = evsel__newtp(sys, name);
383 evsel->handler = handler;
384 evlist__add(evlist, evsel);
388 struct evlist_cpu_iterator evlist__cpu_begin(struct evlist *evlist, struct affinity *affinity)
390 struct evlist_cpu_iterator itr = {
394 .evlist_cpu_map_idx = 0,
395 .evlist_cpu_map_nr = perf_cpu_map__nr(evlist->core.all_cpus),
396 .cpu = (struct perf_cpu){ .cpu = -1},
397 .affinity = affinity,
400 if (evlist__empty(evlist)) {
401 /* Ensure the empty list doesn't iterate. */
402 itr.evlist_cpu_map_idx = itr.evlist_cpu_map_nr;
404 itr.evsel = evlist__first(evlist);
406 itr.cpu = perf_cpu_map__cpu(evlist->core.all_cpus, 0);
407 affinity__set(itr.affinity, itr.cpu.cpu);
408 itr.cpu_map_idx = perf_cpu_map__idx(itr.evsel->core.cpus, itr.cpu);
410 * If this CPU isn't in the evsel's cpu map then advance
413 if (itr.cpu_map_idx == -1)
414 evlist_cpu_iterator__next(&itr);
420 void evlist_cpu_iterator__next(struct evlist_cpu_iterator *evlist_cpu_itr)
422 while (evlist_cpu_itr->evsel != evlist__last(evlist_cpu_itr->container)) {
423 evlist_cpu_itr->evsel = evsel__next(evlist_cpu_itr->evsel);
424 evlist_cpu_itr->cpu_map_idx =
425 perf_cpu_map__idx(evlist_cpu_itr->evsel->core.cpus,
426 evlist_cpu_itr->cpu);
427 if (evlist_cpu_itr->cpu_map_idx != -1)
430 evlist_cpu_itr->evlist_cpu_map_idx++;
431 if (evlist_cpu_itr->evlist_cpu_map_idx < evlist_cpu_itr->evlist_cpu_map_nr) {
432 evlist_cpu_itr->evsel = evlist__first(evlist_cpu_itr->container);
433 evlist_cpu_itr->cpu =
434 perf_cpu_map__cpu(evlist_cpu_itr->container->core.all_cpus,
435 evlist_cpu_itr->evlist_cpu_map_idx);
436 if (evlist_cpu_itr->affinity)
437 affinity__set(evlist_cpu_itr->affinity, evlist_cpu_itr->cpu.cpu);
438 evlist_cpu_itr->cpu_map_idx =
439 perf_cpu_map__idx(evlist_cpu_itr->evsel->core.cpus,
440 evlist_cpu_itr->cpu);
442 * If this CPU isn't in the evsel's cpu map then advance through
445 if (evlist_cpu_itr->cpu_map_idx == -1)
446 evlist_cpu_iterator__next(evlist_cpu_itr);
450 bool evlist_cpu_iterator__end(const struct evlist_cpu_iterator *evlist_cpu_itr)
452 return evlist_cpu_itr->evlist_cpu_map_idx >= evlist_cpu_itr->evlist_cpu_map_nr;
455 static int evsel__strcmp(struct evsel *pos, char *evsel_name)
459 if (evsel__is_dummy_event(pos))
461 return strcmp(pos->name, evsel_name);
464 static int evlist__is_enabled(struct evlist *evlist)
468 evlist__for_each_entry(evlist, pos) {
469 if (!evsel__is_group_leader(pos) || !pos->core.fd)
471 /* If at least one event is enabled, evlist is enabled. */
478 static void __evlist__disable(struct evlist *evlist, char *evsel_name)
481 struct evlist_cpu_iterator evlist_cpu_itr;
482 struct affinity saved_affinity, *affinity = NULL;
483 bool has_imm = false;
485 // See explanation in evlist__close()
486 if (!cpu_map__is_dummy(evlist->core.user_requested_cpus)) {
487 if (affinity__setup(&saved_affinity) < 0)
489 affinity = &saved_affinity;
492 /* Disable 'immediate' events last */
493 for (int imm = 0; imm <= 1; imm++) {
494 evlist__for_each_cpu(evlist_cpu_itr, evlist, affinity) {
495 pos = evlist_cpu_itr.evsel;
496 if (evsel__strcmp(pos, evsel_name))
498 if (pos->disabled || !evsel__is_group_leader(pos) || !pos->core.fd)
502 if (pos->immediate != imm)
504 evsel__disable_cpu(pos, evlist_cpu_itr.cpu_map_idx);
510 affinity__cleanup(affinity);
511 evlist__for_each_entry(evlist, pos) {
512 if (evsel__strcmp(pos, evsel_name))
514 if (!evsel__is_group_leader(pos) || !pos->core.fd)
516 pos->disabled = true;
520 * If we disabled only single event, we need to check
521 * the enabled state of the evlist manually.
524 evlist->enabled = evlist__is_enabled(evlist);
526 evlist->enabled = false;
529 void evlist__disable(struct evlist *evlist)
531 __evlist__disable(evlist, NULL);
534 void evlist__disable_evsel(struct evlist *evlist, char *evsel_name)
536 __evlist__disable(evlist, evsel_name);
539 static void __evlist__enable(struct evlist *evlist, char *evsel_name)
542 struct evlist_cpu_iterator evlist_cpu_itr;
543 struct affinity saved_affinity, *affinity = NULL;
545 // See explanation in evlist__close()
546 if (!cpu_map__is_dummy(evlist->core.user_requested_cpus)) {
547 if (affinity__setup(&saved_affinity) < 0)
549 affinity = &saved_affinity;
552 evlist__for_each_cpu(evlist_cpu_itr, evlist, affinity) {
553 pos = evlist_cpu_itr.evsel;
554 if (evsel__strcmp(pos, evsel_name))
556 if (!evsel__is_group_leader(pos) || !pos->core.fd)
558 evsel__enable_cpu(pos, evlist_cpu_itr.cpu_map_idx);
560 affinity__cleanup(affinity);
561 evlist__for_each_entry(evlist, pos) {
562 if (evsel__strcmp(pos, evsel_name))
564 if (!evsel__is_group_leader(pos) || !pos->core.fd)
566 pos->disabled = false;
570 * Even single event sets the 'enabled' for evlist,
571 * so the toggle can work properly and toggle to
574 evlist->enabled = true;
577 void evlist__enable(struct evlist *evlist)
579 __evlist__enable(evlist, NULL);
582 void evlist__enable_evsel(struct evlist *evlist, char *evsel_name)
584 __evlist__enable(evlist, evsel_name);
587 void evlist__toggle_enable(struct evlist *evlist)
589 (evlist->enabled ? evlist__disable : evlist__enable)(evlist);
592 int evlist__add_pollfd(struct evlist *evlist, int fd)
594 return perf_evlist__add_pollfd(&evlist->core, fd, NULL, POLLIN, fdarray_flag__default);
597 int evlist__filter_pollfd(struct evlist *evlist, short revents_and_mask)
599 return perf_evlist__filter_pollfd(&evlist->core, revents_and_mask);
602 #ifdef HAVE_EVENTFD_SUPPORT
603 int evlist__add_wakeup_eventfd(struct evlist *evlist, int fd)
605 return perf_evlist__add_pollfd(&evlist->core, fd, NULL, POLLIN,
606 fdarray_flag__nonfilterable);
610 int evlist__poll(struct evlist *evlist, int timeout)
612 return perf_evlist__poll(&evlist->core, timeout);
615 struct perf_sample_id *evlist__id2sid(struct evlist *evlist, u64 id)
617 struct hlist_head *head;
618 struct perf_sample_id *sid;
621 hash = hash_64(id, PERF_EVLIST__HLIST_BITS);
622 head = &evlist->core.heads[hash];
624 hlist_for_each_entry(sid, head, node)
631 struct evsel *evlist__id2evsel(struct evlist *evlist, u64 id)
633 struct perf_sample_id *sid;
635 if (evlist->core.nr_entries == 1 || !id)
636 return evlist__first(evlist);
638 sid = evlist__id2sid(evlist, id);
640 return container_of(sid->evsel, struct evsel, core);
642 if (!evlist__sample_id_all(evlist))
643 return evlist__first(evlist);
648 struct evsel *evlist__id2evsel_strict(struct evlist *evlist, u64 id)
650 struct perf_sample_id *sid;
655 sid = evlist__id2sid(evlist, id);
657 return container_of(sid->evsel, struct evsel, core);
662 static int evlist__event2id(struct evlist *evlist, union perf_event *event, u64 *id)
664 const __u64 *array = event->sample.array;
667 n = (event->header.size - sizeof(event->header)) >> 3;
669 if (event->header.type == PERF_RECORD_SAMPLE) {
670 if (evlist->id_pos >= n)
672 *id = array[evlist->id_pos];
674 if (evlist->is_pos > n)
682 struct evsel *evlist__event2evsel(struct evlist *evlist, union perf_event *event)
684 struct evsel *first = evlist__first(evlist);
685 struct hlist_head *head;
686 struct perf_sample_id *sid;
690 if (evlist->core.nr_entries == 1)
693 if (!first->core.attr.sample_id_all &&
694 event->header.type != PERF_RECORD_SAMPLE)
697 if (evlist__event2id(evlist, event, &id))
700 /* Synthesized events have an id of zero */
704 hash = hash_64(id, PERF_EVLIST__HLIST_BITS);
705 head = &evlist->core.heads[hash];
707 hlist_for_each_entry(sid, head, node) {
709 return container_of(sid->evsel, struct evsel, core);
714 static int evlist__set_paused(struct evlist *evlist, bool value)
718 if (!evlist->overwrite_mmap)
721 for (i = 0; i < evlist->core.nr_mmaps; i++) {
722 int fd = evlist->overwrite_mmap[i].core.fd;
727 err = ioctl(fd, PERF_EVENT_IOC_PAUSE_OUTPUT, value ? 1 : 0);
734 static int evlist__pause(struct evlist *evlist)
736 return evlist__set_paused(evlist, true);
739 static int evlist__resume(struct evlist *evlist)
741 return evlist__set_paused(evlist, false);
744 static void evlist__munmap_nofree(struct evlist *evlist)
749 for (i = 0; i < evlist->core.nr_mmaps; i++)
750 perf_mmap__munmap(&evlist->mmap[i].core);
752 if (evlist->overwrite_mmap)
753 for (i = 0; i < evlist->core.nr_mmaps; i++)
754 perf_mmap__munmap(&evlist->overwrite_mmap[i].core);
757 void evlist__munmap(struct evlist *evlist)
759 evlist__munmap_nofree(evlist);
760 zfree(&evlist->mmap);
761 zfree(&evlist->overwrite_mmap);
764 static void perf_mmap__unmap_cb(struct perf_mmap *map)
766 struct mmap *m = container_of(map, struct mmap, core);
771 static struct mmap *evlist__alloc_mmap(struct evlist *evlist,
777 map = zalloc(evlist->core.nr_mmaps * sizeof(struct mmap));
781 for (i = 0; i < evlist->core.nr_mmaps; i++) {
782 struct perf_mmap *prev = i ? &map[i - 1].core : NULL;
785 * When the perf_mmap() call is made we grab one refcount, plus
786 * one extra to let perf_mmap__consume() get the last
787 * events after all real references (perf_mmap__get()) are
790 * Each PERF_EVENT_IOC_SET_OUTPUT points to this mmap and
791 * thus does perf_mmap__get() on it.
793 perf_mmap__init(&map[i].core, prev, overwrite, perf_mmap__unmap_cb);
800 perf_evlist__mmap_cb_idx(struct perf_evlist *_evlist,
801 struct perf_evsel *_evsel,
802 struct perf_mmap_param *_mp,
805 struct evlist *evlist = container_of(_evlist, struct evlist, core);
806 struct mmap_params *mp = container_of(_mp, struct mmap_params, core);
807 struct evsel *evsel = container_of(_evsel, struct evsel, core);
809 auxtrace_mmap_params__set_idx(&mp->auxtrace_mp, evlist, evsel, idx);
812 static struct perf_mmap*
813 perf_evlist__mmap_cb_get(struct perf_evlist *_evlist, bool overwrite, int idx)
815 struct evlist *evlist = container_of(_evlist, struct evlist, core);
818 maps = overwrite ? evlist->overwrite_mmap : evlist->mmap;
821 maps = evlist__alloc_mmap(evlist, overwrite);
826 evlist->overwrite_mmap = maps;
827 if (evlist->bkw_mmap_state == BKW_MMAP_NOTREADY)
828 evlist__toggle_bkw_mmap(evlist, BKW_MMAP_RUNNING);
834 return &maps[idx].core;
838 perf_evlist__mmap_cb_mmap(struct perf_mmap *_map, struct perf_mmap_param *_mp,
839 int output, struct perf_cpu cpu)
841 struct mmap *map = container_of(_map, struct mmap, core);
842 struct mmap_params *mp = container_of(_mp, struct mmap_params, core);
844 return mmap__mmap(map, mp, output, cpu);
847 unsigned long perf_event_mlock_kb_in_pages(void)
852 if (sysctl__read_int("kernel/perf_event_mlock_kb", &max) < 0) {
854 * Pick a once upon a time good value, i.e. things look
855 * strange since we can't read a sysctl value, but lets not
860 max -= (page_size / 1024);
863 pages = (max * 1024) / page_size;
864 if (!is_power_of_2(pages))
865 pages = rounddown_pow_of_two(pages);
870 size_t evlist__mmap_size(unsigned long pages)
872 if (pages == UINT_MAX)
873 pages = perf_event_mlock_kb_in_pages();
874 else if (!is_power_of_2(pages))
877 return (pages + 1) * page_size;
880 static long parse_pages_arg(const char *str, unsigned long min,
883 unsigned long pages, val;
884 static struct parse_tag tags[] = {
885 { .tag = 'B', .mult = 1 },
886 { .tag = 'K', .mult = 1 << 10 },
887 { .tag = 'M', .mult = 1 << 20 },
888 { .tag = 'G', .mult = 1 << 30 },
895 val = parse_tag_value(str, tags);
896 if (val != (unsigned long) -1) {
897 /* we got file size value */
898 pages = PERF_ALIGN(val, page_size) / page_size;
900 /* we got pages count value */
902 pages = strtoul(str, &eptr, 10);
907 if (pages == 0 && min == 0) {
908 /* leave number of pages at 0 */
909 } else if (!is_power_of_2(pages)) {
912 /* round pages up to next power of 2 */
913 pages = roundup_pow_of_two(pages);
917 unit_number__scnprintf(buf, sizeof(buf), pages * page_size);
918 pr_info("rounding mmap pages size to %s (%lu pages)\n",
928 int __evlist__parse_mmap_pages(unsigned int *mmap_pages, const char *str)
930 unsigned long max = UINT_MAX;
933 if (max > SIZE_MAX / page_size)
934 max = SIZE_MAX / page_size;
936 pages = parse_pages_arg(str, 1, max);
938 pr_err("Invalid argument for --mmap_pages/-m\n");
946 int evlist__parse_mmap_pages(const struct option *opt, const char *str, int unset __maybe_unused)
948 return __evlist__parse_mmap_pages(opt->value, str);
952 * evlist__mmap_ex - Create mmaps to receive events.
953 * @evlist: list of events
954 * @pages: map length in pages
955 * @overwrite: overwrite older events?
956 * @auxtrace_pages - auxtrace map length in pages
957 * @auxtrace_overwrite - overwrite older auxtrace data?
959 * If @overwrite is %false the user needs to signal event consumption using
960 * perf_mmap__write_tail(). Using evlist__mmap_read() does this
963 * Similarly, if @auxtrace_overwrite is %false the user needs to signal data
964 * consumption using auxtrace_mmap__write_tail().
966 * Return: %0 on success, negative error code otherwise.
968 int evlist__mmap_ex(struct evlist *evlist, unsigned int pages,
969 unsigned int auxtrace_pages,
970 bool auxtrace_overwrite, int nr_cblocks, int affinity, int flush,
974 * Delay setting mp.prot: set it before calling perf_mmap__mmap.
975 * Its value is decided by evsel's write_backward.
976 * So &mp should not be passed through const pointer.
978 struct mmap_params mp = {
979 .nr_cblocks = nr_cblocks,
980 .affinity = affinity,
982 .comp_level = comp_level
984 struct perf_evlist_mmap_ops ops = {
985 .idx = perf_evlist__mmap_cb_idx,
986 .get = perf_evlist__mmap_cb_get,
987 .mmap = perf_evlist__mmap_cb_mmap,
990 evlist->core.mmap_len = evlist__mmap_size(pages);
991 pr_debug("mmap size %zuB\n", evlist->core.mmap_len);
993 auxtrace_mmap_params__init(&mp.auxtrace_mp, evlist->core.mmap_len,
994 auxtrace_pages, auxtrace_overwrite);
996 return perf_evlist__mmap_ops(&evlist->core, &ops, &mp.core);
999 int evlist__mmap(struct evlist *evlist, unsigned int pages)
1001 return evlist__mmap_ex(evlist, pages, 0, false, 0, PERF_AFFINITY_SYS, 1, 0);
1004 int evlist__create_maps(struct evlist *evlist, struct target *target)
1006 bool all_threads = (target->per_thread && target->system_wide);
1007 struct perf_cpu_map *cpus;
1008 struct perf_thread_map *threads;
1011 * If specify '-a' and '--per-thread' to perf record, perf record
1012 * will override '--per-thread'. target->per_thread = false and
1013 * target->system_wide = true.
1015 * If specify '--per-thread' only to perf record,
1016 * target->per_thread = true and target->system_wide = false.
1018 * So target->per_thread && target->system_wide is false.
1019 * For perf record, thread_map__new_str doesn't call
1020 * thread_map__new_all_cpus. That will keep perf record's
1023 * For perf stat, it allows the case that target->per_thread and
1024 * target->system_wide are all true. It means to collect system-wide
1025 * per-thread data. thread_map__new_str will call
1026 * thread_map__new_all_cpus to enumerate all threads.
1028 threads = thread_map__new_str(target->pid, target->tid, target->uid,
1034 if (target__uses_dummy_map(target))
1035 cpus = perf_cpu_map__dummy_new();
1037 cpus = perf_cpu_map__new(target->cpu_list);
1040 goto out_delete_threads;
1042 evlist->core.has_user_cpus = !!target->cpu_list && !target->hybrid;
1044 perf_evlist__set_maps(&evlist->core, cpus, threads);
1046 /* as evlist now has references, put count here */
1047 perf_cpu_map__put(cpus);
1048 perf_thread_map__put(threads);
1053 perf_thread_map__put(threads);
1057 int evlist__apply_filters(struct evlist *evlist, struct evsel **err_evsel)
1059 struct evsel *evsel;
1062 evlist__for_each_entry(evlist, evsel) {
1063 if (evsel->filter == NULL)
1067 * filters only work for tracepoint event, which doesn't have cpu limit.
1068 * So evlist and evsel should always be same.
1070 err = perf_evsel__apply_filter(&evsel->core, evsel->filter);
1080 int evlist__set_tp_filter(struct evlist *evlist, const char *filter)
1082 struct evsel *evsel;
1088 evlist__for_each_entry(evlist, evsel) {
1089 if (evsel->core.attr.type != PERF_TYPE_TRACEPOINT)
1092 err = evsel__set_filter(evsel, filter);
1100 int evlist__append_tp_filter(struct evlist *evlist, const char *filter)
1102 struct evsel *evsel;
1108 evlist__for_each_entry(evlist, evsel) {
1109 if (evsel->core.attr.type != PERF_TYPE_TRACEPOINT)
1112 err = evsel__append_tp_filter(evsel, filter);
1120 char *asprintf__tp_filter_pids(size_t npids, pid_t *pids)
1125 for (i = 0; i < npids; ++i) {
1127 if (asprintf(&filter, "common_pid != %d", pids[i]) < 0)
1132 if (asprintf(&tmp, "%s && common_pid != %d", filter, pids[i]) < 0)
1146 int evlist__set_tp_filter_pids(struct evlist *evlist, size_t npids, pid_t *pids)
1148 char *filter = asprintf__tp_filter_pids(npids, pids);
1149 int ret = evlist__set_tp_filter(evlist, filter);
1155 int evlist__set_tp_filter_pid(struct evlist *evlist, pid_t pid)
1157 return evlist__set_tp_filter_pids(evlist, 1, &pid);
1160 int evlist__append_tp_filter_pids(struct evlist *evlist, size_t npids, pid_t *pids)
1162 char *filter = asprintf__tp_filter_pids(npids, pids);
1163 int ret = evlist__append_tp_filter(evlist, filter);
1169 int evlist__append_tp_filter_pid(struct evlist *evlist, pid_t pid)
1171 return evlist__append_tp_filter_pids(evlist, 1, &pid);
1174 bool evlist__valid_sample_type(struct evlist *evlist)
1178 if (evlist->core.nr_entries == 1)
1181 if (evlist->id_pos < 0 || evlist->is_pos < 0)
1184 evlist__for_each_entry(evlist, pos) {
1185 if (pos->id_pos != evlist->id_pos ||
1186 pos->is_pos != evlist->is_pos)
1193 u64 __evlist__combined_sample_type(struct evlist *evlist)
1195 struct evsel *evsel;
1197 if (evlist->combined_sample_type)
1198 return evlist->combined_sample_type;
1200 evlist__for_each_entry(evlist, evsel)
1201 evlist->combined_sample_type |= evsel->core.attr.sample_type;
1203 return evlist->combined_sample_type;
1206 u64 evlist__combined_sample_type(struct evlist *evlist)
1208 evlist->combined_sample_type = 0;
1209 return __evlist__combined_sample_type(evlist);
1212 u64 evlist__combined_branch_type(struct evlist *evlist)
1214 struct evsel *evsel;
1215 u64 branch_type = 0;
1217 evlist__for_each_entry(evlist, evsel)
1218 branch_type |= evsel->core.attr.branch_sample_type;
1222 bool evlist__valid_read_format(struct evlist *evlist)
1224 struct evsel *first = evlist__first(evlist), *pos = first;
1225 u64 read_format = first->core.attr.read_format;
1226 u64 sample_type = first->core.attr.sample_type;
1228 evlist__for_each_entry(evlist, pos) {
1229 if (read_format != pos->core.attr.read_format) {
1230 pr_debug("Read format differs %#" PRIx64 " vs %#" PRIx64 "\n",
1231 read_format, (u64)pos->core.attr.read_format);
1235 /* PERF_SAMPLE_READ implies PERF_FORMAT_ID. */
1236 if ((sample_type & PERF_SAMPLE_READ) &&
1237 !(read_format & PERF_FORMAT_ID)) {
1244 u16 evlist__id_hdr_size(struct evlist *evlist)
1246 struct evsel *first = evlist__first(evlist);
1247 struct perf_sample *data;
1251 if (!first->core.attr.sample_id_all)
1254 sample_type = first->core.attr.sample_type;
1256 if (sample_type & PERF_SAMPLE_TID)
1257 size += sizeof(data->tid) * 2;
1259 if (sample_type & PERF_SAMPLE_TIME)
1260 size += sizeof(data->time);
1262 if (sample_type & PERF_SAMPLE_ID)
1263 size += sizeof(data->id);
1265 if (sample_type & PERF_SAMPLE_STREAM_ID)
1266 size += sizeof(data->stream_id);
1268 if (sample_type & PERF_SAMPLE_CPU)
1269 size += sizeof(data->cpu) * 2;
1271 if (sample_type & PERF_SAMPLE_IDENTIFIER)
1272 size += sizeof(data->id);
1277 bool evlist__valid_sample_id_all(struct evlist *evlist)
1279 struct evsel *first = evlist__first(evlist), *pos = first;
1281 evlist__for_each_entry_continue(evlist, pos) {
1282 if (first->core.attr.sample_id_all != pos->core.attr.sample_id_all)
1289 bool evlist__sample_id_all(struct evlist *evlist)
1291 struct evsel *first = evlist__first(evlist);
1292 return first->core.attr.sample_id_all;
1295 void evlist__set_selected(struct evlist *evlist, struct evsel *evsel)
1297 evlist->selected = evsel;
1300 void evlist__close(struct evlist *evlist)
1302 struct evsel *evsel;
1303 struct evlist_cpu_iterator evlist_cpu_itr;
1304 struct affinity affinity;
1307 * With perf record core.user_requested_cpus is usually NULL.
1308 * Use the old method to handle this for now.
1310 if (!evlist->core.user_requested_cpus ||
1311 cpu_map__is_dummy(evlist->core.user_requested_cpus)) {
1312 evlist__for_each_entry_reverse(evlist, evsel)
1313 evsel__close(evsel);
1317 if (affinity__setup(&affinity) < 0)
1320 evlist__for_each_cpu(evlist_cpu_itr, evlist, &affinity) {
1321 perf_evsel__close_cpu(&evlist_cpu_itr.evsel->core,
1322 evlist_cpu_itr.cpu_map_idx);
1325 affinity__cleanup(&affinity);
1326 evlist__for_each_entry_reverse(evlist, evsel) {
1327 perf_evsel__free_fd(&evsel->core);
1328 perf_evsel__free_id(&evsel->core);
1330 perf_evlist__reset_id_hash(&evlist->core);
1333 static int evlist__create_syswide_maps(struct evlist *evlist)
1335 struct perf_cpu_map *cpus;
1336 struct perf_thread_map *threads;
1339 * Try reading /sys/devices/system/cpu/online to get
1342 * FIXME: -ENOMEM is the best we can do here, the cpu_map
1343 * code needs an overhaul to properly forward the
1344 * error, and we may not want to do that fallback to a
1345 * default cpu identity map :-\
1347 cpus = perf_cpu_map__new(NULL);
1351 threads = perf_thread_map__new_dummy();
1355 perf_evlist__set_maps(&evlist->core, cpus, threads);
1357 perf_thread_map__put(threads);
1359 perf_cpu_map__put(cpus);
1364 int evlist__open(struct evlist *evlist)
1366 struct evsel *evsel;
1370 * Default: one fd per CPU, all threads, aka systemwide
1371 * as sys_perf_event_open(cpu = -1, thread = -1) is EINVAL
1373 if (evlist->core.threads == NULL && evlist->core.user_requested_cpus == NULL) {
1374 err = evlist__create_syswide_maps(evlist);
1379 evlist__update_id_pos(evlist);
1381 evlist__for_each_entry(evlist, evsel) {
1382 err = evsel__open(evsel, evsel->core.cpus, evsel->core.threads);
1389 evlist__close(evlist);
1394 int evlist__prepare_workload(struct evlist *evlist, struct target *target, const char *argv[],
1395 bool pipe_output, void (*exec_error)(int signo, siginfo_t *info, void *ucontext))
1397 int child_ready_pipe[2], go_pipe[2];
1400 if (pipe(child_ready_pipe) < 0) {
1401 perror("failed to create 'ready' pipe");
1405 if (pipe(go_pipe) < 0) {
1406 perror("failed to create 'go' pipe");
1407 goto out_close_ready_pipe;
1410 evlist->workload.pid = fork();
1411 if (evlist->workload.pid < 0) {
1412 perror("failed to fork");
1413 goto out_close_pipes;
1416 if (!evlist->workload.pid) {
1422 signal(SIGTERM, SIG_DFL);
1424 close(child_ready_pipe[0]);
1426 fcntl(go_pipe[0], F_SETFD, FD_CLOEXEC);
1429 * Change the name of this process not to confuse --exclude-perf users
1430 * that sees 'perf' in the window up to the execvp() and thinks that
1431 * perf samples are not being excluded.
1433 prctl(PR_SET_NAME, "perf-exec");
1436 * Tell the parent we're ready to go
1438 close(child_ready_pipe[1]);
1441 * Wait until the parent tells us to go.
1443 ret = read(go_pipe[0], &bf, 1);
1445 * The parent will ask for the execvp() to be performed by
1446 * writing exactly one byte, in workload.cork_fd, usually via
1447 * evlist__start_workload().
1449 * For cancelling the workload without actually running it,
1450 * the parent will just close workload.cork_fd, without writing
1451 * anything, i.e. read will return zero and we just exit()
1456 perror("unable to read pipe");
1460 execvp(argv[0], (char **)argv);
1465 val.sival_int = errno;
1466 if (sigqueue(getppid(), SIGUSR1, val))
1474 struct sigaction act = {
1475 .sa_flags = SA_SIGINFO,
1476 .sa_sigaction = exec_error,
1478 sigaction(SIGUSR1, &act, NULL);
1481 if (target__none(target)) {
1482 if (evlist->core.threads == NULL) {
1483 fprintf(stderr, "FATAL: evlist->threads need to be set at this point (%s:%d).\n",
1484 __func__, __LINE__);
1485 goto out_close_pipes;
1487 perf_thread_map__set_pid(evlist->core.threads, 0, evlist->workload.pid);
1490 close(child_ready_pipe[1]);
1493 * wait for child to settle
1495 if (read(child_ready_pipe[0], &bf, 1) == -1) {
1496 perror("unable to read pipe");
1497 goto out_close_pipes;
1500 fcntl(go_pipe[1], F_SETFD, FD_CLOEXEC);
1501 evlist->workload.cork_fd = go_pipe[1];
1502 close(child_ready_pipe[0]);
1508 out_close_ready_pipe:
1509 close(child_ready_pipe[0]);
1510 close(child_ready_pipe[1]);
1514 int evlist__start_workload(struct evlist *evlist)
1516 if (evlist->workload.cork_fd > 0) {
1520 * Remove the cork, let it rip!
1522 ret = write(evlist->workload.cork_fd, &bf, 1);
1524 perror("unable to write to pipe");
1526 close(evlist->workload.cork_fd);
1533 int evlist__parse_sample(struct evlist *evlist, union perf_event *event, struct perf_sample *sample)
1535 struct evsel *evsel = evlist__event2evsel(evlist, event);
1539 return evsel__parse_sample(evsel, event, sample);
1542 int evlist__parse_sample_timestamp(struct evlist *evlist, union perf_event *event, u64 *timestamp)
1544 struct evsel *evsel = evlist__event2evsel(evlist, event);
1548 return evsel__parse_sample_timestamp(evsel, event, timestamp);
1551 int evlist__strerror_open(struct evlist *evlist, int err, char *buf, size_t size)
1554 char sbuf[STRERR_BUFSIZE], *emsg = str_error_r(err, sbuf, sizeof(sbuf));
1559 printed = scnprintf(buf, size,
1561 "Hint:\tCheck /proc/sys/kernel/perf_event_paranoid setting.", emsg);
1563 value = perf_event_paranoid();
1565 printed += scnprintf(buf + printed, size - printed, "\nHint:\t");
1568 printed += scnprintf(buf + printed, size - printed,
1569 "For your workloads it needs to be <= 1\nHint:\t");
1571 printed += scnprintf(buf + printed, size - printed,
1572 "For system wide tracing it needs to be set to -1.\n");
1574 printed += scnprintf(buf + printed, size - printed,
1575 "Hint:\tTry: 'sudo sh -c \"echo -1 > /proc/sys/kernel/perf_event_paranoid\"'\n"
1576 "Hint:\tThe current value is %d.", value);
1579 struct evsel *first = evlist__first(evlist);
1582 if (sysctl__read_int("kernel/perf_event_max_sample_rate", &max_freq) < 0)
1585 if (first->core.attr.sample_freq < (u64)max_freq)
1588 printed = scnprintf(buf, size,
1590 "Hint:\tCheck /proc/sys/kernel/perf_event_max_sample_rate.\n"
1591 "Hint:\tThe current value is %d and %" PRIu64 " is being requested.",
1592 emsg, max_freq, first->core.attr.sample_freq);
1597 scnprintf(buf, size, "%s", emsg);
1604 int evlist__strerror_mmap(struct evlist *evlist, int err, char *buf, size_t size)
1606 char sbuf[STRERR_BUFSIZE], *emsg = str_error_r(err, sbuf, sizeof(sbuf));
1607 int pages_attempted = evlist->core.mmap_len / 1024, pages_max_per_user, printed = 0;
1611 sysctl__read_int("kernel/perf_event_mlock_kb", &pages_max_per_user);
1612 printed += scnprintf(buf + printed, size - printed,
1614 "Hint:\tCheck /proc/sys/kernel/perf_event_mlock_kb (%d kB) setting.\n"
1615 "Hint:\tTried using %zd kB.\n",
1616 emsg, pages_max_per_user, pages_attempted);
1618 if (pages_attempted >= pages_max_per_user) {
1619 printed += scnprintf(buf + printed, size - printed,
1620 "Hint:\tTry 'sudo sh -c \"echo %d > /proc/sys/kernel/perf_event_mlock_kb\"', or\n",
1621 pages_max_per_user + pages_attempted);
1624 printed += scnprintf(buf + printed, size - printed,
1625 "Hint:\tTry using a smaller -m/--mmap-pages value.");
1628 scnprintf(buf, size, "%s", emsg);
1635 void evlist__to_front(struct evlist *evlist, struct evsel *move_evsel)
1637 struct evsel *evsel, *n;
1640 if (move_evsel == evlist__first(evlist))
1643 evlist__for_each_entry_safe(evlist, n, evsel) {
1644 if (evsel__leader(evsel) == evsel__leader(move_evsel))
1645 list_move_tail(&evsel->core.node, &move);
1648 list_splice(&move, &evlist->core.entries);
1651 struct evsel *evlist__get_tracking_event(struct evlist *evlist)
1653 struct evsel *evsel;
1655 evlist__for_each_entry(evlist, evsel) {
1656 if (evsel->tracking)
1660 return evlist__first(evlist);
1663 void evlist__set_tracking_event(struct evlist *evlist, struct evsel *tracking_evsel)
1665 struct evsel *evsel;
1667 if (tracking_evsel->tracking)
1670 evlist__for_each_entry(evlist, evsel) {
1671 if (evsel != tracking_evsel)
1672 evsel->tracking = false;
1675 tracking_evsel->tracking = true;
1678 struct evsel *evlist__find_evsel_by_str(struct evlist *evlist, const char *str)
1680 struct evsel *evsel;
1682 evlist__for_each_entry(evlist, evsel) {
1685 if (strcmp(str, evsel->name) == 0)
1692 void evlist__toggle_bkw_mmap(struct evlist *evlist, enum bkw_mmap_state state)
1694 enum bkw_mmap_state old_state = evlist->bkw_mmap_state;
1701 if (!evlist->overwrite_mmap)
1704 switch (old_state) {
1705 case BKW_MMAP_NOTREADY: {
1706 if (state != BKW_MMAP_RUNNING)
1710 case BKW_MMAP_RUNNING: {
1711 if (state != BKW_MMAP_DATA_PENDING)
1716 case BKW_MMAP_DATA_PENDING: {
1717 if (state != BKW_MMAP_EMPTY)
1721 case BKW_MMAP_EMPTY: {
1722 if (state != BKW_MMAP_RUNNING)
1728 WARN_ONCE(1, "Shouldn't get there\n");
1731 evlist->bkw_mmap_state = state;
1735 evlist__pause(evlist);
1738 evlist__resume(evlist);
1749 bool evlist__exclude_kernel(struct evlist *evlist)
1751 struct evsel *evsel;
1753 evlist__for_each_entry(evlist, evsel) {
1754 if (!evsel->core.attr.exclude_kernel)
1762 * Events in data file are not collect in groups, but we still want
1763 * the group display. Set the artificial group and set the leader's
1764 * forced_leader flag to notify the display code.
1766 void evlist__force_leader(struct evlist *evlist)
1768 if (!evlist->core.nr_groups) {
1769 struct evsel *leader = evlist__first(evlist);
1771 evlist__set_leader(evlist);
1772 leader->forced_leader = true;
1776 struct evsel *evlist__reset_weak_group(struct evlist *evsel_list, struct evsel *evsel, bool close)
1778 struct evsel *c2, *leader;
1779 bool is_open = true;
1781 leader = evsel__leader(evsel);
1783 pr_debug("Weak group for %s/%d failed\n",
1784 leader->name, leader->core.nr_members);
1787 * for_each_group_member doesn't work here because it doesn't
1788 * include the first entry.
1790 evlist__for_each_entry(evsel_list, c2) {
1793 if (evsel__has_leader(c2, leader)) {
1794 if (is_open && close)
1795 perf_evsel__close(&c2->core);
1797 * We want to close all members of the group and reopen
1798 * them. Some events, like Intel topdown, require being
1799 * in a group and so keep these in the group.
1801 evsel__remove_from_group(c2, leader);
1804 * Set this for all former members of the group
1805 * to indicate they get reopened.
1807 c2->reset_group = true;
1810 /* Reset the leader count if all entries were removed. */
1811 if (leader->core.nr_members == 1)
1812 leader->core.nr_members = 0;
1816 static int evlist__parse_control_fifo(const char *str, int *ctl_fd, int *ctl_fd_ack, bool *ctl_fd_close)
1821 if (strncmp(str, "fifo:", 5))
1825 if (!*str || *str == ',')
1837 * O_RDWR avoids POLLHUPs which is necessary to allow the other
1838 * end of a FIFO to be repeatedly opened and closed.
1840 fd = open(s, O_RDWR | O_NONBLOCK | O_CLOEXEC);
1842 pr_err("Failed to open '%s'\n", s);
1847 *ctl_fd_close = true;
1850 /* O_RDWR | O_NONBLOCK means the other end need not be open */
1851 fd = open(p, O_RDWR | O_NONBLOCK | O_CLOEXEC);
1853 pr_err("Failed to open '%s'\n", p);
1865 int evlist__parse_control(const char *str, int *ctl_fd, int *ctl_fd_ack, bool *ctl_fd_close)
1867 char *comma = NULL, *endptr = NULL;
1869 *ctl_fd_close = false;
1871 if (strncmp(str, "fd:", 3))
1872 return evlist__parse_control_fifo(str, ctl_fd, ctl_fd_ack, ctl_fd_close);
1874 *ctl_fd = strtoul(&str[3], &endptr, 0);
1875 if (endptr == &str[3])
1878 comma = strchr(str, ',');
1880 if (endptr != comma)
1883 *ctl_fd_ack = strtoul(comma + 1, &endptr, 0);
1884 if (endptr == comma + 1 || *endptr != '\0')
1891 void evlist__close_control(int ctl_fd, int ctl_fd_ack, bool *ctl_fd_close)
1893 if (*ctl_fd_close) {
1894 *ctl_fd_close = false;
1896 if (ctl_fd_ack >= 0)
1901 int evlist__initialize_ctlfd(struct evlist *evlist, int fd, int ack)
1904 pr_debug("Control descriptor is not initialized\n");
1908 evlist->ctl_fd.pos = perf_evlist__add_pollfd(&evlist->core, fd, NULL, POLLIN,
1909 fdarray_flag__nonfilterable);
1910 if (evlist->ctl_fd.pos < 0) {
1911 evlist->ctl_fd.pos = -1;
1912 pr_err("Failed to add ctl fd entry: %m\n");
1916 evlist->ctl_fd.fd = fd;
1917 evlist->ctl_fd.ack = ack;
1922 bool evlist__ctlfd_initialized(struct evlist *evlist)
1924 return evlist->ctl_fd.pos >= 0;
1927 int evlist__finalize_ctlfd(struct evlist *evlist)
1929 struct pollfd *entries = evlist->core.pollfd.entries;
1931 if (!evlist__ctlfd_initialized(evlist))
1934 entries[evlist->ctl_fd.pos].fd = -1;
1935 entries[evlist->ctl_fd.pos].events = 0;
1936 entries[evlist->ctl_fd.pos].revents = 0;
1938 evlist->ctl_fd.pos = -1;
1939 evlist->ctl_fd.ack = -1;
1940 evlist->ctl_fd.fd = -1;
1945 static int evlist__ctlfd_recv(struct evlist *evlist, enum evlist_ctl_cmd *cmd,
1946 char *cmd_data, size_t data_size)
1950 size_t bytes_read = 0;
1952 *cmd = EVLIST_CTL_CMD_UNSUPPORTED;
1953 memset(cmd_data, 0, data_size);
1957 err = read(evlist->ctl_fd.fd, &c, 1);
1959 if (c == '\n' || c == '\0')
1961 cmd_data[bytes_read++] = c;
1962 if (bytes_read == data_size)
1965 } else if (err == -1) {
1968 if (errno == EAGAIN || errno == EWOULDBLOCK)
1971 pr_err("Failed to read from ctlfd %d: %m\n", evlist->ctl_fd.fd);
1976 pr_debug("Message from ctl_fd: \"%s%s\"\n", cmd_data,
1977 bytes_read == data_size ? "" : c == '\n' ? "\\n" : "\\0");
1979 if (bytes_read > 0) {
1980 if (!strncmp(cmd_data, EVLIST_CTL_CMD_ENABLE_TAG,
1981 (sizeof(EVLIST_CTL_CMD_ENABLE_TAG)-1))) {
1982 *cmd = EVLIST_CTL_CMD_ENABLE;
1983 } else if (!strncmp(cmd_data, EVLIST_CTL_CMD_DISABLE_TAG,
1984 (sizeof(EVLIST_CTL_CMD_DISABLE_TAG)-1))) {
1985 *cmd = EVLIST_CTL_CMD_DISABLE;
1986 } else if (!strncmp(cmd_data, EVLIST_CTL_CMD_SNAPSHOT_TAG,
1987 (sizeof(EVLIST_CTL_CMD_SNAPSHOT_TAG)-1))) {
1988 *cmd = EVLIST_CTL_CMD_SNAPSHOT;
1989 pr_debug("is snapshot\n");
1990 } else if (!strncmp(cmd_data, EVLIST_CTL_CMD_EVLIST_TAG,
1991 (sizeof(EVLIST_CTL_CMD_EVLIST_TAG)-1))) {
1992 *cmd = EVLIST_CTL_CMD_EVLIST;
1993 } else if (!strncmp(cmd_data, EVLIST_CTL_CMD_STOP_TAG,
1994 (sizeof(EVLIST_CTL_CMD_STOP_TAG)-1))) {
1995 *cmd = EVLIST_CTL_CMD_STOP;
1996 } else if (!strncmp(cmd_data, EVLIST_CTL_CMD_PING_TAG,
1997 (sizeof(EVLIST_CTL_CMD_PING_TAG)-1))) {
1998 *cmd = EVLIST_CTL_CMD_PING;
2002 return bytes_read ? (int)bytes_read : err;
2005 int evlist__ctlfd_ack(struct evlist *evlist)
2009 if (evlist->ctl_fd.ack == -1)
2012 err = write(evlist->ctl_fd.ack, EVLIST_CTL_CMD_ACK_TAG,
2013 sizeof(EVLIST_CTL_CMD_ACK_TAG));
2015 pr_err("failed to write to ctl_ack_fd %d: %m\n", evlist->ctl_fd.ack);
2020 static int get_cmd_arg(char *cmd_data, size_t cmd_size, char **arg)
2022 char *data = cmd_data + cmd_size;
2028 /* there's argument */
2038 static int evlist__ctlfd_enable(struct evlist *evlist, char *cmd_data, bool enable)
2040 struct evsel *evsel;
2044 err = get_cmd_arg(cmd_data,
2045 enable ? sizeof(EVLIST_CTL_CMD_ENABLE_TAG) - 1 :
2046 sizeof(EVLIST_CTL_CMD_DISABLE_TAG) - 1,
2049 pr_info("failed: wrong command\n");
2054 evsel = evlist__find_evsel_by_str(evlist, name);
2057 evlist__enable_evsel(evlist, name);
2059 evlist__disable_evsel(evlist, name);
2060 pr_info("Event %s %s\n", evsel->name,
2061 enable ? "enabled" : "disabled");
2063 pr_info("failed: can't find '%s' event\n", name);
2067 evlist__enable(evlist);
2068 pr_info(EVLIST_ENABLED_MSG);
2070 evlist__disable(evlist);
2071 pr_info(EVLIST_DISABLED_MSG);
2078 static int evlist__ctlfd_list(struct evlist *evlist, char *cmd_data)
2080 struct perf_attr_details details = { .verbose = false, };
2081 struct evsel *evsel;
2085 err = get_cmd_arg(cmd_data,
2086 sizeof(EVLIST_CTL_CMD_EVLIST_TAG) - 1,
2089 pr_info("failed: wrong command\n");
2094 if (!strcmp(arg, "-v")) {
2095 details.verbose = true;
2096 } else if (!strcmp(arg, "-g")) {
2097 details.event_group = true;
2098 } else if (!strcmp(arg, "-F")) {
2099 details.freq = true;
2101 pr_info("failed: wrong command\n");
2106 evlist__for_each_entry(evlist, evsel)
2107 evsel__fprintf(evsel, &details, stderr);
2112 int evlist__ctlfd_process(struct evlist *evlist, enum evlist_ctl_cmd *cmd)
2115 char cmd_data[EVLIST_CTL_CMD_MAX_LEN];
2116 int ctlfd_pos = evlist->ctl_fd.pos;
2117 struct pollfd *entries = evlist->core.pollfd.entries;
2119 if (!evlist__ctlfd_initialized(evlist) || !entries[ctlfd_pos].revents)
2122 if (entries[ctlfd_pos].revents & POLLIN) {
2123 err = evlist__ctlfd_recv(evlist, cmd, cmd_data,
2124 EVLIST_CTL_CMD_MAX_LEN);
2127 case EVLIST_CTL_CMD_ENABLE:
2128 case EVLIST_CTL_CMD_DISABLE:
2129 err = evlist__ctlfd_enable(evlist, cmd_data,
2130 *cmd == EVLIST_CTL_CMD_ENABLE);
2132 case EVLIST_CTL_CMD_EVLIST:
2133 err = evlist__ctlfd_list(evlist, cmd_data);
2135 case EVLIST_CTL_CMD_SNAPSHOT:
2136 case EVLIST_CTL_CMD_STOP:
2137 case EVLIST_CTL_CMD_PING:
2139 case EVLIST_CTL_CMD_ACK:
2140 case EVLIST_CTL_CMD_UNSUPPORTED:
2142 pr_debug("ctlfd: unsupported %d\n", *cmd);
2145 if (!(*cmd == EVLIST_CTL_CMD_ACK || *cmd == EVLIST_CTL_CMD_UNSUPPORTED ||
2146 *cmd == EVLIST_CTL_CMD_SNAPSHOT))
2147 evlist__ctlfd_ack(evlist);
2151 if (entries[ctlfd_pos].revents & (POLLHUP | POLLERR))
2152 evlist__finalize_ctlfd(evlist);
2154 entries[ctlfd_pos].revents = 0;
2159 int evlist__ctlfd_update(struct evlist *evlist, struct pollfd *update)
2161 int ctlfd_pos = evlist->ctl_fd.pos;
2162 struct pollfd *entries = evlist->core.pollfd.entries;
2164 if (!evlist__ctlfd_initialized(evlist))
2167 if (entries[ctlfd_pos].fd != update->fd ||
2168 entries[ctlfd_pos].events != update->events)
2171 entries[ctlfd_pos].revents = update->revents;
2175 struct evsel *evlist__find_evsel(struct evlist *evlist, int idx)
2177 struct evsel *evsel;
2179 evlist__for_each_entry(evlist, evsel) {
2180 if (evsel->core.idx == idx)
2186 int evlist__scnprintf_evsels(struct evlist *evlist, size_t size, char *bf)
2188 struct evsel *evsel;
2191 evlist__for_each_entry(evlist, evsel) {
2192 if (evsel__is_dummy_event(evsel))
2194 if (size > (strlen(evsel__name(evsel)) + (printed ? 2 : 1))) {
2195 printed += scnprintf(bf + printed, size - printed, "%s%s", printed ? "," : "", evsel__name(evsel));
2197 printed += scnprintf(bf + printed, size - printed, "%s...", printed ? "," : "");
2205 void evlist__check_mem_load_aux(struct evlist *evlist)
2207 struct evsel *leader, *evsel, *pos;
2210 * For some platforms, the 'mem-loads' event is required to use
2211 * together with 'mem-loads-aux' within a group and 'mem-loads-aux'
2212 * must be the group leader. Now we disable this group before reporting
2213 * because 'mem-loads-aux' is just an auxiliary event. It doesn't carry
2214 * any valid memory load information.
2216 evlist__for_each_entry(evlist, evsel) {
2217 leader = evsel__leader(evsel);
2218 if (leader == evsel)
2221 if (leader->name && strstr(leader->name, "mem-loads-aux")) {
2222 for_each_group_evsel(pos, leader) {
2223 evsel__set_leader(pos, pos);
2224 pos->core.nr_members = 0;