1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2011, Red Hat Inc, Arnaldo Carvalho de Melo <acme@redhat.com>
5 * Parts came from builtin-{top,stat,record}.c, see those files for further
13 #include "util/mmap.h"
14 #include "thread_map.h"
20 #include <internal/lib.h> // page_size
24 #include "bpf-event.h"
25 #include "util/string2.h"
26 #include "util/perf_api_probe.h"
32 #include "parse-events.h"
33 #include <subcmd/parse-options.h>
36 #include <sys/ioctl.h>
39 #include <linux/bitops.h>
40 #include <linux/hash.h>
41 #include <linux/log2.h>
42 #include <linux/err.h>
43 #include <linux/string.h>
44 #include <linux/zalloc.h>
45 #include <perf/evlist.h>
46 #include <perf/evsel.h>
47 #include <perf/cpumap.h>
48 #include <perf/mmap.h>
50 #include <internal/xyarray.h>
52 #ifdef LACKS_SIGQUEUE_PROTOTYPE
53 int sigqueue(pid_t pid, int sig, const union sigval value);
56 #define FD(e, x, y) (*(int *)xyarray__entry(e->core.fd, x, y))
57 #define SID(e, x, y) xyarray__entry(e->core.sample_id, x, y)
59 void evlist__init(struct evlist *evlist, struct perf_cpu_map *cpus,
60 struct perf_thread_map *threads)
62 perf_evlist__init(&evlist->core);
63 perf_evlist__set_maps(&evlist->core, cpus, threads);
64 evlist->workload.pid = -1;
65 evlist->bkw_mmap_state = BKW_MMAP_NOTREADY;
66 evlist->ctl_fd.fd = -1;
67 evlist->ctl_fd.ack = -1;
68 evlist->ctl_fd.pos = -1;
71 struct evlist *evlist__new(void)
73 struct evlist *evlist = zalloc(sizeof(*evlist));
76 evlist__init(evlist, NULL, NULL);
81 struct evlist *evlist__new_default(void)
83 struct evlist *evlist = evlist__new();
85 if (evlist && evlist__add_default(evlist)) {
86 evlist__delete(evlist);
93 struct evlist *evlist__new_dummy(void)
95 struct evlist *evlist = evlist__new();
97 if (evlist && evlist__add_dummy(evlist)) {
98 evlist__delete(evlist);
106 * evlist__set_id_pos - set the positions of event ids.
107 * @evlist: selected event list
109 * Events with compatible sample types all have the same id_pos
110 * and is_pos. For convenience, put a copy on evlist.
112 void evlist__set_id_pos(struct evlist *evlist)
114 struct evsel *first = evlist__first(evlist);
116 evlist->id_pos = first->id_pos;
117 evlist->is_pos = first->is_pos;
120 static void evlist__update_id_pos(struct evlist *evlist)
124 evlist__for_each_entry(evlist, evsel)
125 evsel__calc_id_pos(evsel);
127 evlist__set_id_pos(evlist);
130 static void evlist__purge(struct evlist *evlist)
132 struct evsel *pos, *n;
134 evlist__for_each_entry_safe(evlist, n, pos) {
135 list_del_init(&pos->core.node);
140 evlist->core.nr_entries = 0;
143 void evlist__exit(struct evlist *evlist)
145 zfree(&evlist->mmap);
146 zfree(&evlist->overwrite_mmap);
147 perf_evlist__exit(&evlist->core);
150 void evlist__delete(struct evlist *evlist)
155 evlist__munmap(evlist);
156 evlist__close(evlist);
157 evlist__purge(evlist);
158 evlist__exit(evlist);
162 void evlist__add(struct evlist *evlist, struct evsel *entry)
164 entry->evlist = evlist;
165 entry->idx = evlist->core.nr_entries;
166 entry->tracking = !entry->idx;
168 perf_evlist__add(&evlist->core, &entry->core);
170 if (evlist->core.nr_entries == 1)
171 evlist__set_id_pos(evlist);
174 void evlist__remove(struct evlist *evlist, struct evsel *evsel)
176 evsel->evlist = NULL;
177 perf_evlist__remove(&evlist->core, &evsel->core);
180 void evlist__splice_list_tail(struct evlist *evlist, struct list_head *list)
182 struct evsel *evsel, *temp;
184 __evlist__for_each_entry_safe(list, temp, evsel) {
185 list_del_init(&evsel->core.node);
186 evlist__add(evlist, evsel);
190 int __evlist__set_tracepoints_handlers(struct evlist *evlist,
191 const struct evsel_str_handler *assocs, size_t nr_assocs)
196 for (i = 0; i < nr_assocs; i++) {
197 // Adding a handler for an event not in this evlist, just ignore it.
198 struct evsel *evsel = evlist__find_tracepoint_by_name(evlist, assocs[i].name);
203 if (evsel->handler != NULL)
205 evsel->handler = assocs[i].handler;
213 void __evlist__set_leader(struct list_head *list)
215 struct evsel *evsel, *leader;
217 leader = list_entry(list->next, struct evsel, core.node);
218 evsel = list_entry(list->prev, struct evsel, core.node);
220 leader->core.nr_members = evsel->idx - leader->idx + 1;
222 __evlist__for_each_entry(list, evsel) {
223 evsel->leader = leader;
227 void evlist__set_leader(struct evlist *evlist)
229 if (evlist->core.nr_entries) {
230 evlist->nr_groups = evlist->core.nr_entries > 1 ? 1 : 0;
231 __evlist__set_leader(&evlist->core.entries);
235 int __evlist__add_default(struct evlist *evlist, bool precise)
237 struct evsel *evsel = evsel__new_cycles(precise);
242 evlist__add(evlist, evsel);
246 int evlist__add_dummy(struct evlist *evlist)
248 struct perf_event_attr attr = {
249 .type = PERF_TYPE_SOFTWARE,
250 .config = PERF_COUNT_SW_DUMMY,
251 .size = sizeof(attr), /* to capture ABI version */
253 struct evsel *evsel = evsel__new_idx(&attr, evlist->core.nr_entries);
258 evlist__add(evlist, evsel);
262 static int evlist__add_attrs(struct evlist *evlist, struct perf_event_attr *attrs, size_t nr_attrs)
264 struct evsel *evsel, *n;
268 for (i = 0; i < nr_attrs; i++) {
269 evsel = evsel__new_idx(attrs + i, evlist->core.nr_entries + i);
271 goto out_delete_partial_list;
272 list_add_tail(&evsel->core.node, &head);
275 evlist__splice_list_tail(evlist, &head);
279 out_delete_partial_list:
280 __evlist__for_each_entry_safe(&head, n, evsel)
281 evsel__delete(evsel);
285 int __evlist__add_default_attrs(struct evlist *evlist, struct perf_event_attr *attrs, size_t nr_attrs)
289 for (i = 0; i < nr_attrs; i++)
290 event_attr_init(attrs + i);
292 return evlist__add_attrs(evlist, attrs, nr_attrs);
295 struct evsel *evlist__find_tracepoint_by_id(struct evlist *evlist, int id)
299 evlist__for_each_entry(evlist, evsel) {
300 if (evsel->core.attr.type == PERF_TYPE_TRACEPOINT &&
301 (int)evsel->core.attr.config == id)
308 struct evsel *evlist__find_tracepoint_by_name(struct evlist *evlist, const char *name)
312 evlist__for_each_entry(evlist, evsel) {
313 if ((evsel->core.attr.type == PERF_TYPE_TRACEPOINT) &&
314 (strcmp(evsel->name, name) == 0))
321 int evlist__add_newtp(struct evlist *evlist, const char *sys, const char *name, void *handler)
323 struct evsel *evsel = evsel__newtp(sys, name);
328 evsel->handler = handler;
329 evlist__add(evlist, evsel);
333 static int perf_evlist__nr_threads(struct evlist *evlist,
336 if (evsel->core.system_wide)
339 return perf_thread_map__nr(evlist->core.threads);
342 void evlist__cpu_iter_start(struct evlist *evlist)
347 * Reset the per evsel cpu_iter. This is needed because
348 * each evsel's cpumap may have a different index space,
349 * and some operations need the index to modify
350 * the FD xyarray (e.g. open, close)
352 evlist__for_each_entry(evlist, pos)
356 bool evsel__cpu_iter_skip_no_inc(struct evsel *ev, int cpu)
358 if (ev->cpu_iter >= ev->core.cpus->nr)
360 if (cpu >= 0 && ev->core.cpus->map[ev->cpu_iter] != cpu)
365 bool evsel__cpu_iter_skip(struct evsel *ev, int cpu)
367 if (!evsel__cpu_iter_skip_no_inc(ev, cpu)) {
374 void evlist__disable(struct evlist *evlist)
377 struct affinity affinity;
379 bool has_imm = false;
381 if (affinity__setup(&affinity) < 0)
384 /* Disable 'immediate' events last */
385 for (imm = 0; imm <= 1; imm++) {
386 evlist__for_each_cpu(evlist, i, cpu) {
387 affinity__set(&affinity, cpu);
389 evlist__for_each_entry(evlist, pos) {
390 if (evsel__cpu_iter_skip(pos, cpu))
392 if (pos->disabled || !evsel__is_group_leader(pos) || !pos->core.fd)
396 if (pos->immediate != imm)
398 evsel__disable_cpu(pos, pos->cpu_iter - 1);
405 affinity__cleanup(&affinity);
406 evlist__for_each_entry(evlist, pos) {
407 if (!evsel__is_group_leader(pos) || !pos->core.fd)
409 pos->disabled = true;
412 evlist->enabled = false;
415 void evlist__enable(struct evlist *evlist)
418 struct affinity affinity;
421 if (affinity__setup(&affinity) < 0)
424 evlist__for_each_cpu(evlist, i, cpu) {
425 affinity__set(&affinity, cpu);
427 evlist__for_each_entry(evlist, pos) {
428 if (evsel__cpu_iter_skip(pos, cpu))
430 if (!evsel__is_group_leader(pos) || !pos->core.fd)
432 evsel__enable_cpu(pos, pos->cpu_iter - 1);
435 affinity__cleanup(&affinity);
436 evlist__for_each_entry(evlist, pos) {
437 if (!evsel__is_group_leader(pos) || !pos->core.fd)
439 pos->disabled = false;
442 evlist->enabled = true;
445 void evlist__toggle_enable(struct evlist *evlist)
447 (evlist->enabled ? evlist__disable : evlist__enable)(evlist);
450 static int evlist__enable_event_cpu(struct evlist *evlist, struct evsel *evsel, int cpu)
453 int nr_threads = perf_evlist__nr_threads(evlist, evsel);
458 for (thread = 0; thread < nr_threads; thread++) {
459 int err = ioctl(FD(evsel, cpu, thread), PERF_EVENT_IOC_ENABLE, 0);
466 static int evlist__enable_event_thread(struct evlist *evlist, struct evsel *evsel, int thread)
469 int nr_cpus = perf_cpu_map__nr(evlist->core.cpus);
474 for (cpu = 0; cpu < nr_cpus; cpu++) {
475 int err = ioctl(FD(evsel, cpu, thread), PERF_EVENT_IOC_ENABLE, 0);
482 int evlist__enable_event_idx(struct evlist *evlist, struct evsel *evsel, int idx)
484 bool per_cpu_mmaps = !perf_cpu_map__empty(evlist->core.cpus);
487 return evlist__enable_event_cpu(evlist, evsel, idx);
489 return evlist__enable_event_thread(evlist, evsel, idx);
492 int evlist__add_pollfd(struct evlist *evlist, int fd)
494 return perf_evlist__add_pollfd(&evlist->core, fd, NULL, POLLIN, fdarray_flag__default);
497 int evlist__filter_pollfd(struct evlist *evlist, short revents_and_mask)
499 return perf_evlist__filter_pollfd(&evlist->core, revents_and_mask);
502 int evlist__poll(struct evlist *evlist, int timeout)
504 return perf_evlist__poll(&evlist->core, timeout);
507 struct perf_sample_id *evlist__id2sid(struct evlist *evlist, u64 id)
509 struct hlist_head *head;
510 struct perf_sample_id *sid;
513 hash = hash_64(id, PERF_EVLIST__HLIST_BITS);
514 head = &evlist->core.heads[hash];
516 hlist_for_each_entry(sid, head, node)
523 struct evsel *evlist__id2evsel(struct evlist *evlist, u64 id)
525 struct perf_sample_id *sid;
527 if (evlist->core.nr_entries == 1 || !id)
528 return evlist__first(evlist);
530 sid = evlist__id2sid(evlist, id);
532 return container_of(sid->evsel, struct evsel, core);
534 if (!evlist__sample_id_all(evlist))
535 return evlist__first(evlist);
540 struct evsel *evlist__id2evsel_strict(struct evlist *evlist, u64 id)
542 struct perf_sample_id *sid;
547 sid = evlist__id2sid(evlist, id);
549 return container_of(sid->evsel, struct evsel, core);
554 static int evlist__event2id(struct evlist *evlist, union perf_event *event, u64 *id)
556 const __u64 *array = event->sample.array;
559 n = (event->header.size - sizeof(event->header)) >> 3;
561 if (event->header.type == PERF_RECORD_SAMPLE) {
562 if (evlist->id_pos >= n)
564 *id = array[evlist->id_pos];
566 if (evlist->is_pos > n)
574 struct evsel *evlist__event2evsel(struct evlist *evlist, union perf_event *event)
576 struct evsel *first = evlist__first(evlist);
577 struct hlist_head *head;
578 struct perf_sample_id *sid;
582 if (evlist->core.nr_entries == 1)
585 if (!first->core.attr.sample_id_all &&
586 event->header.type != PERF_RECORD_SAMPLE)
589 if (evlist__event2id(evlist, event, &id))
592 /* Synthesized events have an id of zero */
596 hash = hash_64(id, PERF_EVLIST__HLIST_BITS);
597 head = &evlist->core.heads[hash];
599 hlist_for_each_entry(sid, head, node) {
601 return container_of(sid->evsel, struct evsel, core);
606 static int evlist__set_paused(struct evlist *evlist, bool value)
610 if (!evlist->overwrite_mmap)
613 for (i = 0; i < evlist->core.nr_mmaps; i++) {
614 int fd = evlist->overwrite_mmap[i].core.fd;
619 err = ioctl(fd, PERF_EVENT_IOC_PAUSE_OUTPUT, value ? 1 : 0);
626 static int evlist__pause(struct evlist *evlist)
628 return evlist__set_paused(evlist, true);
631 static int evlist__resume(struct evlist *evlist)
633 return evlist__set_paused(evlist, false);
636 static void evlist__munmap_nofree(struct evlist *evlist)
641 for (i = 0; i < evlist->core.nr_mmaps; i++)
642 perf_mmap__munmap(&evlist->mmap[i].core);
644 if (evlist->overwrite_mmap)
645 for (i = 0; i < evlist->core.nr_mmaps; i++)
646 perf_mmap__munmap(&evlist->overwrite_mmap[i].core);
649 void evlist__munmap(struct evlist *evlist)
651 evlist__munmap_nofree(evlist);
652 zfree(&evlist->mmap);
653 zfree(&evlist->overwrite_mmap);
656 static void perf_mmap__unmap_cb(struct perf_mmap *map)
658 struct mmap *m = container_of(map, struct mmap, core);
663 static struct mmap *evlist__alloc_mmap(struct evlist *evlist,
669 map = zalloc(evlist->core.nr_mmaps * sizeof(struct mmap));
673 for (i = 0; i < evlist->core.nr_mmaps; i++) {
674 struct perf_mmap *prev = i ? &map[i - 1].core : NULL;
677 * When the perf_mmap() call is made we grab one refcount, plus
678 * one extra to let perf_mmap__consume() get the last
679 * events after all real references (perf_mmap__get()) are
682 * Each PERF_EVENT_IOC_SET_OUTPUT points to this mmap and
683 * thus does perf_mmap__get() on it.
685 perf_mmap__init(&map[i].core, prev, overwrite, perf_mmap__unmap_cb);
692 perf_evlist__mmap_cb_idx(struct perf_evlist *_evlist,
693 struct perf_mmap_param *_mp,
694 int idx, bool per_cpu)
696 struct evlist *evlist = container_of(_evlist, struct evlist, core);
697 struct mmap_params *mp = container_of(_mp, struct mmap_params, core);
699 auxtrace_mmap_params__set_idx(&mp->auxtrace_mp, evlist, idx, per_cpu);
702 static struct perf_mmap*
703 perf_evlist__mmap_cb_get(struct perf_evlist *_evlist, bool overwrite, int idx)
705 struct evlist *evlist = container_of(_evlist, struct evlist, core);
708 maps = overwrite ? evlist->overwrite_mmap : evlist->mmap;
711 maps = evlist__alloc_mmap(evlist, overwrite);
716 evlist->overwrite_mmap = maps;
717 if (evlist->bkw_mmap_state == BKW_MMAP_NOTREADY)
718 evlist__toggle_bkw_mmap(evlist, BKW_MMAP_RUNNING);
724 return &maps[idx].core;
728 perf_evlist__mmap_cb_mmap(struct perf_mmap *_map, struct perf_mmap_param *_mp,
731 struct mmap *map = container_of(_map, struct mmap, core);
732 struct mmap_params *mp = container_of(_mp, struct mmap_params, core);
734 return mmap__mmap(map, mp, output, cpu);
737 unsigned long perf_event_mlock_kb_in_pages(void)
742 if (sysctl__read_int("kernel/perf_event_mlock_kb", &max) < 0) {
744 * Pick a once upon a time good value, i.e. things look
745 * strange since we can't read a sysctl value, but lets not
750 max -= (page_size / 1024);
753 pages = (max * 1024) / page_size;
754 if (!is_power_of_2(pages))
755 pages = rounddown_pow_of_two(pages);
760 size_t evlist__mmap_size(unsigned long pages)
762 if (pages == UINT_MAX)
763 pages = perf_event_mlock_kb_in_pages();
764 else if (!is_power_of_2(pages))
767 return (pages + 1) * page_size;
770 static long parse_pages_arg(const char *str, unsigned long min,
773 unsigned long pages, val;
774 static struct parse_tag tags[] = {
775 { .tag = 'B', .mult = 1 },
776 { .tag = 'K', .mult = 1 << 10 },
777 { .tag = 'M', .mult = 1 << 20 },
778 { .tag = 'G', .mult = 1 << 30 },
785 val = parse_tag_value(str, tags);
786 if (val != (unsigned long) -1) {
787 /* we got file size value */
788 pages = PERF_ALIGN(val, page_size) / page_size;
790 /* we got pages count value */
792 pages = strtoul(str, &eptr, 10);
797 if (pages == 0 && min == 0) {
798 /* leave number of pages at 0 */
799 } else if (!is_power_of_2(pages)) {
802 /* round pages up to next power of 2 */
803 pages = roundup_pow_of_two(pages);
807 unit_number__scnprintf(buf, sizeof(buf), pages * page_size);
808 pr_info("rounding mmap pages size to %s (%lu pages)\n",
818 int __evlist__parse_mmap_pages(unsigned int *mmap_pages, const char *str)
820 unsigned long max = UINT_MAX;
823 if (max > SIZE_MAX / page_size)
824 max = SIZE_MAX / page_size;
826 pages = parse_pages_arg(str, 1, max);
828 pr_err("Invalid argument for --mmap_pages/-m\n");
836 int evlist__parse_mmap_pages(const struct option *opt, const char *str, int unset __maybe_unused)
838 return __evlist__parse_mmap_pages(opt->value, str);
842 * evlist__mmap_ex - Create mmaps to receive events.
843 * @evlist: list of events
844 * @pages: map length in pages
845 * @overwrite: overwrite older events?
846 * @auxtrace_pages - auxtrace map length in pages
847 * @auxtrace_overwrite - overwrite older auxtrace data?
849 * If @overwrite is %false the user needs to signal event consumption using
850 * perf_mmap__write_tail(). Using evlist__mmap_read() does this
853 * Similarly, if @auxtrace_overwrite is %false the user needs to signal data
854 * consumption using auxtrace_mmap__write_tail().
856 * Return: %0 on success, negative error code otherwise.
858 int evlist__mmap_ex(struct evlist *evlist, unsigned int pages,
859 unsigned int auxtrace_pages,
860 bool auxtrace_overwrite, int nr_cblocks, int affinity, int flush,
864 * Delay setting mp.prot: set it before calling perf_mmap__mmap.
865 * Its value is decided by evsel's write_backward.
866 * So &mp should not be passed through const pointer.
868 struct mmap_params mp = {
869 .nr_cblocks = nr_cblocks,
870 .affinity = affinity,
872 .comp_level = comp_level
874 struct perf_evlist_mmap_ops ops = {
875 .idx = perf_evlist__mmap_cb_idx,
876 .get = perf_evlist__mmap_cb_get,
877 .mmap = perf_evlist__mmap_cb_mmap,
880 evlist->core.mmap_len = evlist__mmap_size(pages);
881 pr_debug("mmap size %zuB\n", evlist->core.mmap_len);
883 auxtrace_mmap_params__init(&mp.auxtrace_mp, evlist->core.mmap_len,
884 auxtrace_pages, auxtrace_overwrite);
886 return perf_evlist__mmap_ops(&evlist->core, &ops, &mp.core);
889 int evlist__mmap(struct evlist *evlist, unsigned int pages)
891 return evlist__mmap_ex(evlist, pages, 0, false, 0, PERF_AFFINITY_SYS, 1, 0);
894 int evlist__create_maps(struct evlist *evlist, struct target *target)
896 bool all_threads = (target->per_thread && target->system_wide);
897 struct perf_cpu_map *cpus;
898 struct perf_thread_map *threads;
901 * If specify '-a' and '--per-thread' to perf record, perf record
902 * will override '--per-thread'. target->per_thread = false and
903 * target->system_wide = true.
905 * If specify '--per-thread' only to perf record,
906 * target->per_thread = true and target->system_wide = false.
908 * So target->per_thread && target->system_wide is false.
909 * For perf record, thread_map__new_str doesn't call
910 * thread_map__new_all_cpus. That will keep perf record's
913 * For perf stat, it allows the case that target->per_thread and
914 * target->system_wide are all true. It means to collect system-wide
915 * per-thread data. thread_map__new_str will call
916 * thread_map__new_all_cpus to enumerate all threads.
918 threads = thread_map__new_str(target->pid, target->tid, target->uid,
924 if (target__uses_dummy_map(target))
925 cpus = perf_cpu_map__dummy_new();
927 cpus = perf_cpu_map__new(target->cpu_list);
930 goto out_delete_threads;
932 evlist->core.has_user_cpus = !!target->cpu_list;
934 perf_evlist__set_maps(&evlist->core, cpus, threads);
936 /* as evlist now has references, put count here */
937 perf_cpu_map__put(cpus);
938 perf_thread_map__put(threads);
943 perf_thread_map__put(threads);
947 int evlist__apply_filters(struct evlist *evlist, struct evsel **err_evsel)
952 evlist__for_each_entry(evlist, evsel) {
953 if (evsel->filter == NULL)
957 * filters only work for tracepoint event, which doesn't have cpu limit.
958 * So evlist and evsel should always be same.
960 err = perf_evsel__apply_filter(&evsel->core, evsel->filter);
970 int evlist__set_tp_filter(struct evlist *evlist, const char *filter)
978 evlist__for_each_entry(evlist, evsel) {
979 if (evsel->core.attr.type != PERF_TYPE_TRACEPOINT)
982 err = evsel__set_filter(evsel, filter);
990 int evlist__append_tp_filter(struct evlist *evlist, const char *filter)
998 evlist__for_each_entry(evlist, evsel) {
999 if (evsel->core.attr.type != PERF_TYPE_TRACEPOINT)
1002 err = evsel__append_tp_filter(evsel, filter);
1010 char *asprintf__tp_filter_pids(size_t npids, pid_t *pids)
1015 for (i = 0; i < npids; ++i) {
1017 if (asprintf(&filter, "common_pid != %d", pids[i]) < 0)
1022 if (asprintf(&tmp, "%s && common_pid != %d", filter, pids[i]) < 0)
1036 int evlist__set_tp_filter_pids(struct evlist *evlist, size_t npids, pid_t *pids)
1038 char *filter = asprintf__tp_filter_pids(npids, pids);
1039 int ret = evlist__set_tp_filter(evlist, filter);
1045 int evlist__set_tp_filter_pid(struct evlist *evlist, pid_t pid)
1047 return evlist__set_tp_filter_pids(evlist, 1, &pid);
1050 int evlist__append_tp_filter_pids(struct evlist *evlist, size_t npids, pid_t *pids)
1052 char *filter = asprintf__tp_filter_pids(npids, pids);
1053 int ret = evlist__append_tp_filter(evlist, filter);
1059 int evlist__append_tp_filter_pid(struct evlist *evlist, pid_t pid)
1061 return evlist__append_tp_filter_pids(evlist, 1, &pid);
1064 bool evlist__valid_sample_type(struct evlist *evlist)
1068 if (evlist->core.nr_entries == 1)
1071 if (evlist->id_pos < 0 || evlist->is_pos < 0)
1074 evlist__for_each_entry(evlist, pos) {
1075 if (pos->id_pos != evlist->id_pos ||
1076 pos->is_pos != evlist->is_pos)
1083 u64 __evlist__combined_sample_type(struct evlist *evlist)
1085 struct evsel *evsel;
1087 if (evlist->combined_sample_type)
1088 return evlist->combined_sample_type;
1090 evlist__for_each_entry(evlist, evsel)
1091 evlist->combined_sample_type |= evsel->core.attr.sample_type;
1093 return evlist->combined_sample_type;
1096 u64 evlist__combined_sample_type(struct evlist *evlist)
1098 evlist->combined_sample_type = 0;
1099 return __evlist__combined_sample_type(evlist);
1102 u64 evlist__combined_branch_type(struct evlist *evlist)
1104 struct evsel *evsel;
1105 u64 branch_type = 0;
1107 evlist__for_each_entry(evlist, evsel)
1108 branch_type |= evsel->core.attr.branch_sample_type;
1112 bool evlist__valid_read_format(struct evlist *evlist)
1114 struct evsel *first = evlist__first(evlist), *pos = first;
1115 u64 read_format = first->core.attr.read_format;
1116 u64 sample_type = first->core.attr.sample_type;
1118 evlist__for_each_entry(evlist, pos) {
1119 if (read_format != pos->core.attr.read_format) {
1120 pr_debug("Read format differs %#" PRIx64 " vs %#" PRIx64 "\n",
1121 read_format, (u64)pos->core.attr.read_format);
1125 /* PERF_SAMPLE_READ imples PERF_FORMAT_ID. */
1126 if ((sample_type & PERF_SAMPLE_READ) &&
1127 !(read_format & PERF_FORMAT_ID)) {
1134 u16 evlist__id_hdr_size(struct evlist *evlist)
1136 struct evsel *first = evlist__first(evlist);
1137 struct perf_sample *data;
1141 if (!first->core.attr.sample_id_all)
1144 sample_type = first->core.attr.sample_type;
1146 if (sample_type & PERF_SAMPLE_TID)
1147 size += sizeof(data->tid) * 2;
1149 if (sample_type & PERF_SAMPLE_TIME)
1150 size += sizeof(data->time);
1152 if (sample_type & PERF_SAMPLE_ID)
1153 size += sizeof(data->id);
1155 if (sample_type & PERF_SAMPLE_STREAM_ID)
1156 size += sizeof(data->stream_id);
1158 if (sample_type & PERF_SAMPLE_CPU)
1159 size += sizeof(data->cpu) * 2;
1161 if (sample_type & PERF_SAMPLE_IDENTIFIER)
1162 size += sizeof(data->id);
1167 bool evlist__valid_sample_id_all(struct evlist *evlist)
1169 struct evsel *first = evlist__first(evlist), *pos = first;
1171 evlist__for_each_entry_continue(evlist, pos) {
1172 if (first->core.attr.sample_id_all != pos->core.attr.sample_id_all)
1179 bool evlist__sample_id_all(struct evlist *evlist)
1181 struct evsel *first = evlist__first(evlist);
1182 return first->core.attr.sample_id_all;
1185 void evlist__set_selected(struct evlist *evlist, struct evsel *evsel)
1187 evlist->selected = evsel;
1190 void evlist__close(struct evlist *evlist)
1192 struct evsel *evsel;
1193 struct affinity affinity;
1197 * With perf record core.cpus is usually NULL.
1198 * Use the old method to handle this for now.
1200 if (!evlist->core.cpus) {
1201 evlist__for_each_entry_reverse(evlist, evsel)
1202 evsel__close(evsel);
1206 if (affinity__setup(&affinity) < 0)
1208 evlist__for_each_cpu(evlist, i, cpu) {
1209 affinity__set(&affinity, cpu);
1211 evlist__for_each_entry_reverse(evlist, evsel) {
1212 if (evsel__cpu_iter_skip(evsel, cpu))
1214 perf_evsel__close_cpu(&evsel->core, evsel->cpu_iter - 1);
1217 affinity__cleanup(&affinity);
1218 evlist__for_each_entry_reverse(evlist, evsel) {
1219 perf_evsel__free_fd(&evsel->core);
1220 perf_evsel__free_id(&evsel->core);
1224 static int evlist__create_syswide_maps(struct evlist *evlist)
1226 struct perf_cpu_map *cpus;
1227 struct perf_thread_map *threads;
1231 * Try reading /sys/devices/system/cpu/online to get
1234 * FIXME: -ENOMEM is the best we can do here, the cpu_map
1235 * code needs an overhaul to properly forward the
1236 * error, and we may not want to do that fallback to a
1237 * default cpu identity map :-\
1239 cpus = perf_cpu_map__new(NULL);
1243 threads = perf_thread_map__new_dummy();
1247 perf_evlist__set_maps(&evlist->core, cpus, threads);
1249 perf_thread_map__put(threads);
1251 perf_cpu_map__put(cpus);
1256 int evlist__open(struct evlist *evlist)
1258 struct evsel *evsel;
1262 * Default: one fd per CPU, all threads, aka systemwide
1263 * as sys_perf_event_open(cpu = -1, thread = -1) is EINVAL
1265 if (evlist->core.threads == NULL && evlist->core.cpus == NULL) {
1266 err = evlist__create_syswide_maps(evlist);
1271 evlist__update_id_pos(evlist);
1273 evlist__for_each_entry(evlist, evsel) {
1274 err = evsel__open(evsel, evsel->core.cpus, evsel->core.threads);
1281 evlist__close(evlist);
1286 int evlist__prepare_workload(struct evlist *evlist, struct target *target, const char *argv[],
1287 bool pipe_output, void (*exec_error)(int signo, siginfo_t *info, void *ucontext))
1289 int child_ready_pipe[2], go_pipe[2];
1292 if (pipe(child_ready_pipe) < 0) {
1293 perror("failed to create 'ready' pipe");
1297 if (pipe(go_pipe) < 0) {
1298 perror("failed to create 'go' pipe");
1299 goto out_close_ready_pipe;
1302 evlist->workload.pid = fork();
1303 if (evlist->workload.pid < 0) {
1304 perror("failed to fork");
1305 goto out_close_pipes;
1308 if (!evlist->workload.pid) {
1314 signal(SIGTERM, SIG_DFL);
1316 close(child_ready_pipe[0]);
1318 fcntl(go_pipe[0], F_SETFD, FD_CLOEXEC);
1321 * Tell the parent we're ready to go
1323 close(child_ready_pipe[1]);
1326 * Wait until the parent tells us to go.
1328 ret = read(go_pipe[0], &bf, 1);
1330 * The parent will ask for the execvp() to be performed by
1331 * writing exactly one byte, in workload.cork_fd, usually via
1332 * evlist__start_workload().
1334 * For cancelling the workload without actually running it,
1335 * the parent will just close workload.cork_fd, without writing
1336 * anything, i.e. read will return zero and we just exit()
1341 perror("unable to read pipe");
1345 execvp(argv[0], (char **)argv);
1350 val.sival_int = errno;
1351 if (sigqueue(getppid(), SIGUSR1, val))
1359 struct sigaction act = {
1360 .sa_flags = SA_SIGINFO,
1361 .sa_sigaction = exec_error,
1363 sigaction(SIGUSR1, &act, NULL);
1366 if (target__none(target)) {
1367 if (evlist->core.threads == NULL) {
1368 fprintf(stderr, "FATAL: evlist->threads need to be set at this point (%s:%d).\n",
1369 __func__, __LINE__);
1370 goto out_close_pipes;
1372 perf_thread_map__set_pid(evlist->core.threads, 0, evlist->workload.pid);
1375 close(child_ready_pipe[1]);
1378 * wait for child to settle
1380 if (read(child_ready_pipe[0], &bf, 1) == -1) {
1381 perror("unable to read pipe");
1382 goto out_close_pipes;
1385 fcntl(go_pipe[1], F_SETFD, FD_CLOEXEC);
1386 evlist->workload.cork_fd = go_pipe[1];
1387 close(child_ready_pipe[0]);
1393 out_close_ready_pipe:
1394 close(child_ready_pipe[0]);
1395 close(child_ready_pipe[1]);
1399 int evlist__start_workload(struct evlist *evlist)
1401 if (evlist->workload.cork_fd > 0) {
1405 * Remove the cork, let it rip!
1407 ret = write(evlist->workload.cork_fd, &bf, 1);
1409 perror("unable to write to pipe");
1411 close(evlist->workload.cork_fd);
1418 int evlist__parse_sample(struct evlist *evlist, union perf_event *event, struct perf_sample *sample)
1420 struct evsel *evsel = evlist__event2evsel(evlist, event);
1424 return evsel__parse_sample(evsel, event, sample);
1427 int evlist__parse_sample_timestamp(struct evlist *evlist, union perf_event *event, u64 *timestamp)
1429 struct evsel *evsel = evlist__event2evsel(evlist, event);
1433 return evsel__parse_sample_timestamp(evsel, event, timestamp);
1436 int evlist__strerror_open(struct evlist *evlist, int err, char *buf, size_t size)
1439 char sbuf[STRERR_BUFSIZE], *emsg = str_error_r(err, sbuf, sizeof(sbuf));
1444 printed = scnprintf(buf, size,
1446 "Hint:\tCheck /proc/sys/kernel/perf_event_paranoid setting.", emsg);
1448 value = perf_event_paranoid();
1450 printed += scnprintf(buf + printed, size - printed, "\nHint:\t");
1453 printed += scnprintf(buf + printed, size - printed,
1454 "For your workloads it needs to be <= 1\nHint:\t");
1456 printed += scnprintf(buf + printed, size - printed,
1457 "For system wide tracing it needs to be set to -1.\n");
1459 printed += scnprintf(buf + printed, size - printed,
1460 "Hint:\tTry: 'sudo sh -c \"echo -1 > /proc/sys/kernel/perf_event_paranoid\"'\n"
1461 "Hint:\tThe current value is %d.", value);
1464 struct evsel *first = evlist__first(evlist);
1467 if (sysctl__read_int("kernel/perf_event_max_sample_rate", &max_freq) < 0)
1470 if (first->core.attr.sample_freq < (u64)max_freq)
1473 printed = scnprintf(buf, size,
1475 "Hint:\tCheck /proc/sys/kernel/perf_event_max_sample_rate.\n"
1476 "Hint:\tThe current value is %d and %" PRIu64 " is being requested.",
1477 emsg, max_freq, first->core.attr.sample_freq);
1482 scnprintf(buf, size, "%s", emsg);
1489 int evlist__strerror_mmap(struct evlist *evlist, int err, char *buf, size_t size)
1491 char sbuf[STRERR_BUFSIZE], *emsg = str_error_r(err, sbuf, sizeof(sbuf));
1492 int pages_attempted = evlist->core.mmap_len / 1024, pages_max_per_user, printed = 0;
1496 sysctl__read_int("kernel/perf_event_mlock_kb", &pages_max_per_user);
1497 printed += scnprintf(buf + printed, size - printed,
1499 "Hint:\tCheck /proc/sys/kernel/perf_event_mlock_kb (%d kB) setting.\n"
1500 "Hint:\tTried using %zd kB.\n",
1501 emsg, pages_max_per_user, pages_attempted);
1503 if (pages_attempted >= pages_max_per_user) {
1504 printed += scnprintf(buf + printed, size - printed,
1505 "Hint:\tTry 'sudo sh -c \"echo %d > /proc/sys/kernel/perf_event_mlock_kb\"', or\n",
1506 pages_max_per_user + pages_attempted);
1509 printed += scnprintf(buf + printed, size - printed,
1510 "Hint:\tTry using a smaller -m/--mmap-pages value.");
1513 scnprintf(buf, size, "%s", emsg);
1520 void evlist__to_front(struct evlist *evlist, struct evsel *move_evsel)
1522 struct evsel *evsel, *n;
1525 if (move_evsel == evlist__first(evlist))
1528 evlist__for_each_entry_safe(evlist, n, evsel) {
1529 if (evsel->leader == move_evsel->leader)
1530 list_move_tail(&evsel->core.node, &move);
1533 list_splice(&move, &evlist->core.entries);
1536 struct evsel *evlist__get_tracking_event(struct evlist *evlist)
1538 struct evsel *evsel;
1540 evlist__for_each_entry(evlist, evsel) {
1541 if (evsel->tracking)
1545 return evlist__first(evlist);
1548 void evlist__set_tracking_event(struct evlist *evlist, struct evsel *tracking_evsel)
1550 struct evsel *evsel;
1552 if (tracking_evsel->tracking)
1555 evlist__for_each_entry(evlist, evsel) {
1556 if (evsel != tracking_evsel)
1557 evsel->tracking = false;
1560 tracking_evsel->tracking = true;
1563 struct evsel *evlist__find_evsel_by_str(struct evlist *evlist, const char *str)
1565 struct evsel *evsel;
1567 evlist__for_each_entry(evlist, evsel) {
1570 if (strcmp(str, evsel->name) == 0)
1577 void evlist__toggle_bkw_mmap(struct evlist *evlist, enum bkw_mmap_state state)
1579 enum bkw_mmap_state old_state = evlist->bkw_mmap_state;
1586 if (!evlist->overwrite_mmap)
1589 switch (old_state) {
1590 case BKW_MMAP_NOTREADY: {
1591 if (state != BKW_MMAP_RUNNING)
1595 case BKW_MMAP_RUNNING: {
1596 if (state != BKW_MMAP_DATA_PENDING)
1601 case BKW_MMAP_DATA_PENDING: {
1602 if (state != BKW_MMAP_EMPTY)
1606 case BKW_MMAP_EMPTY: {
1607 if (state != BKW_MMAP_RUNNING)
1613 WARN_ONCE(1, "Shouldn't get there\n");
1616 evlist->bkw_mmap_state = state;
1620 evlist__pause(evlist);
1623 evlist__resume(evlist);
1634 bool evlist__exclude_kernel(struct evlist *evlist)
1636 struct evsel *evsel;
1638 evlist__for_each_entry(evlist, evsel) {
1639 if (!evsel->core.attr.exclude_kernel)
1647 * Events in data file are not collect in groups, but we still want
1648 * the group display. Set the artificial group and set the leader's
1649 * forced_leader flag to notify the display code.
1651 void evlist__force_leader(struct evlist *evlist)
1653 if (!evlist->nr_groups) {
1654 struct evsel *leader = evlist__first(evlist);
1656 evlist__set_leader(evlist);
1657 leader->forced_leader = true;
1661 struct evsel *evlist__reset_weak_group(struct evlist *evsel_list, struct evsel *evsel, bool close)
1663 struct evsel *c2, *leader;
1664 bool is_open = true;
1666 leader = evsel->leader;
1667 pr_debug("Weak group for %s/%d failed\n",
1668 leader->name, leader->core.nr_members);
1671 * for_each_group_member doesn't work here because it doesn't
1672 * include the first entry.
1674 evlist__for_each_entry(evsel_list, c2) {
1677 if (c2->leader == leader) {
1678 if (is_open && close)
1679 perf_evsel__close(&c2->core);
1681 c2->core.nr_members = 0;
1683 * Set this for all former members of the group
1684 * to indicate they get reopened.
1686 c2->reset_group = true;
1692 static int evlist__parse_control_fifo(const char *str, int *ctl_fd, int *ctl_fd_ack, bool *ctl_fd_close)
1697 if (strncmp(str, "fifo:", 5))
1701 if (!*str || *str == ',')
1713 * O_RDWR avoids POLLHUPs which is necessary to allow the other
1714 * end of a FIFO to be repeatedly opened and closed.
1716 fd = open(s, O_RDWR | O_NONBLOCK | O_CLOEXEC);
1718 pr_err("Failed to open '%s'\n", s);
1723 *ctl_fd_close = true;
1726 /* O_RDWR | O_NONBLOCK means the other end need not be open */
1727 fd = open(p, O_RDWR | O_NONBLOCK | O_CLOEXEC);
1729 pr_err("Failed to open '%s'\n", p);
1741 int evlist__parse_control(const char *str, int *ctl_fd, int *ctl_fd_ack, bool *ctl_fd_close)
1743 char *comma = NULL, *endptr = NULL;
1745 *ctl_fd_close = false;
1747 if (strncmp(str, "fd:", 3))
1748 return evlist__parse_control_fifo(str, ctl_fd, ctl_fd_ack, ctl_fd_close);
1750 *ctl_fd = strtoul(&str[3], &endptr, 0);
1751 if (endptr == &str[3])
1754 comma = strchr(str, ',');
1756 if (endptr != comma)
1759 *ctl_fd_ack = strtoul(comma + 1, &endptr, 0);
1760 if (endptr == comma + 1 || *endptr != '\0')
1767 void evlist__close_control(int ctl_fd, int ctl_fd_ack, bool *ctl_fd_close)
1769 if (*ctl_fd_close) {
1770 *ctl_fd_close = false;
1772 if (ctl_fd_ack >= 0)
1777 int evlist__initialize_ctlfd(struct evlist *evlist, int fd, int ack)
1780 pr_debug("Control descriptor is not initialized\n");
1784 evlist->ctl_fd.pos = perf_evlist__add_pollfd(&evlist->core, fd, NULL, POLLIN,
1785 fdarray_flag__nonfilterable);
1786 if (evlist->ctl_fd.pos < 0) {
1787 evlist->ctl_fd.pos = -1;
1788 pr_err("Failed to add ctl fd entry: %m\n");
1792 evlist->ctl_fd.fd = fd;
1793 evlist->ctl_fd.ack = ack;
1798 bool evlist__ctlfd_initialized(struct evlist *evlist)
1800 return evlist->ctl_fd.pos >= 0;
1803 int evlist__finalize_ctlfd(struct evlist *evlist)
1805 struct pollfd *entries = evlist->core.pollfd.entries;
1807 if (!evlist__ctlfd_initialized(evlist))
1810 entries[evlist->ctl_fd.pos].fd = -1;
1811 entries[evlist->ctl_fd.pos].events = 0;
1812 entries[evlist->ctl_fd.pos].revents = 0;
1814 evlist->ctl_fd.pos = -1;
1815 evlist->ctl_fd.ack = -1;
1816 evlist->ctl_fd.fd = -1;
1821 static int evlist__ctlfd_recv(struct evlist *evlist, enum evlist_ctl_cmd *cmd,
1822 char *cmd_data, size_t data_size)
1826 size_t bytes_read = 0;
1828 *cmd = EVLIST_CTL_CMD_UNSUPPORTED;
1829 memset(cmd_data, 0, data_size);
1833 err = read(evlist->ctl_fd.fd, &c, 1);
1835 if (c == '\n' || c == '\0')
1837 cmd_data[bytes_read++] = c;
1838 if (bytes_read == data_size)
1841 } else if (err == -1) {
1844 if (errno == EAGAIN || errno == EWOULDBLOCK)
1847 pr_err("Failed to read from ctlfd %d: %m\n", evlist->ctl_fd.fd);
1852 pr_debug("Message from ctl_fd: \"%s%s\"\n", cmd_data,
1853 bytes_read == data_size ? "" : c == '\n' ? "\\n" : "\\0");
1855 if (bytes_read > 0) {
1856 if (!strncmp(cmd_data, EVLIST_CTL_CMD_ENABLE_TAG,
1857 (sizeof(EVLIST_CTL_CMD_ENABLE_TAG)-1))) {
1858 *cmd = EVLIST_CTL_CMD_ENABLE;
1859 } else if (!strncmp(cmd_data, EVLIST_CTL_CMD_DISABLE_TAG,
1860 (sizeof(EVLIST_CTL_CMD_DISABLE_TAG)-1))) {
1861 *cmd = EVLIST_CTL_CMD_DISABLE;
1862 } else if (!strncmp(cmd_data, EVLIST_CTL_CMD_SNAPSHOT_TAG,
1863 (sizeof(EVLIST_CTL_CMD_SNAPSHOT_TAG)-1))) {
1864 *cmd = EVLIST_CTL_CMD_SNAPSHOT;
1865 pr_debug("is snapshot\n");
1869 return bytes_read ? (int)bytes_read : err;
1872 int evlist__ctlfd_ack(struct evlist *evlist)
1876 if (evlist->ctl_fd.ack == -1)
1879 err = write(evlist->ctl_fd.ack, EVLIST_CTL_CMD_ACK_TAG,
1880 sizeof(EVLIST_CTL_CMD_ACK_TAG));
1882 pr_err("failed to write to ctl_ack_fd %d: %m\n", evlist->ctl_fd.ack);
1887 int evlist__ctlfd_process(struct evlist *evlist, enum evlist_ctl_cmd *cmd)
1890 char cmd_data[EVLIST_CTL_CMD_MAX_LEN];
1891 int ctlfd_pos = evlist->ctl_fd.pos;
1892 struct pollfd *entries = evlist->core.pollfd.entries;
1894 if (!evlist__ctlfd_initialized(evlist) || !entries[ctlfd_pos].revents)
1897 if (entries[ctlfd_pos].revents & POLLIN) {
1898 err = evlist__ctlfd_recv(evlist, cmd, cmd_data,
1899 EVLIST_CTL_CMD_MAX_LEN);
1902 case EVLIST_CTL_CMD_ENABLE:
1903 evlist__enable(evlist);
1905 case EVLIST_CTL_CMD_DISABLE:
1906 evlist__disable(evlist);
1908 case EVLIST_CTL_CMD_SNAPSHOT:
1910 case EVLIST_CTL_CMD_ACK:
1911 case EVLIST_CTL_CMD_UNSUPPORTED:
1913 pr_debug("ctlfd: unsupported %d\n", *cmd);
1916 if (!(*cmd == EVLIST_CTL_CMD_ACK || *cmd == EVLIST_CTL_CMD_UNSUPPORTED ||
1917 *cmd == EVLIST_CTL_CMD_SNAPSHOT))
1918 evlist__ctlfd_ack(evlist);
1922 if (entries[ctlfd_pos].revents & (POLLHUP | POLLERR))
1923 evlist__finalize_ctlfd(evlist);
1925 entries[ctlfd_pos].revents = 0;
1930 struct evsel *evlist__find_evsel(struct evlist *evlist, int idx)
1932 struct evsel *evsel;
1934 evlist__for_each_entry(evlist, evsel) {
1935 if (evsel->idx == idx)