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 evlist__nr_threads(struct evlist *evlist, struct evsel *evsel)
335 if (evsel->core.system_wide)
338 return perf_thread_map__nr(evlist->core.threads);
341 void evlist__cpu_iter_start(struct evlist *evlist)
346 * Reset the per evsel cpu_iter. This is needed because
347 * each evsel's cpumap may have a different index space,
348 * and some operations need the index to modify
349 * the FD xyarray (e.g. open, close)
351 evlist__for_each_entry(evlist, pos)
355 bool evsel__cpu_iter_skip_no_inc(struct evsel *ev, int cpu)
357 if (ev->cpu_iter >= ev->core.cpus->nr)
359 if (cpu >= 0 && ev->core.cpus->map[ev->cpu_iter] != cpu)
364 bool evsel__cpu_iter_skip(struct evsel *ev, int cpu)
366 if (!evsel__cpu_iter_skip_no_inc(ev, cpu)) {
373 void evlist__disable(struct evlist *evlist)
376 struct affinity affinity;
378 bool has_imm = false;
380 if (affinity__setup(&affinity) < 0)
383 /* Disable 'immediate' events last */
384 for (imm = 0; imm <= 1; imm++) {
385 evlist__for_each_cpu(evlist, i, cpu) {
386 affinity__set(&affinity, cpu);
388 evlist__for_each_entry(evlist, pos) {
389 if (evsel__cpu_iter_skip(pos, cpu))
391 if (pos->disabled || !evsel__is_group_leader(pos) || !pos->core.fd)
395 if (pos->immediate != imm)
397 evsel__disable_cpu(pos, pos->cpu_iter - 1);
404 affinity__cleanup(&affinity);
405 evlist__for_each_entry(evlist, pos) {
406 if (!evsel__is_group_leader(pos) || !pos->core.fd)
408 pos->disabled = true;
411 evlist->enabled = false;
414 void evlist__enable(struct evlist *evlist)
417 struct affinity affinity;
420 if (affinity__setup(&affinity) < 0)
423 evlist__for_each_cpu(evlist, i, cpu) {
424 affinity__set(&affinity, cpu);
426 evlist__for_each_entry(evlist, pos) {
427 if (evsel__cpu_iter_skip(pos, cpu))
429 if (!evsel__is_group_leader(pos) || !pos->core.fd)
431 evsel__enable_cpu(pos, pos->cpu_iter - 1);
434 affinity__cleanup(&affinity);
435 evlist__for_each_entry(evlist, pos) {
436 if (!evsel__is_group_leader(pos) || !pos->core.fd)
438 pos->disabled = false;
441 evlist->enabled = true;
444 void evlist__toggle_enable(struct evlist *evlist)
446 (evlist->enabled ? evlist__disable : evlist__enable)(evlist);
449 static int evlist__enable_event_cpu(struct evlist *evlist, struct evsel *evsel, int cpu)
452 int nr_threads = evlist__nr_threads(evlist, evsel);
457 for (thread = 0; thread < nr_threads; thread++) {
458 int err = ioctl(FD(evsel, cpu, thread), PERF_EVENT_IOC_ENABLE, 0);
465 static int evlist__enable_event_thread(struct evlist *evlist, struct evsel *evsel, int thread)
468 int nr_cpus = perf_cpu_map__nr(evlist->core.cpus);
473 for (cpu = 0; cpu < nr_cpus; cpu++) {
474 int err = ioctl(FD(evsel, cpu, thread), PERF_EVENT_IOC_ENABLE, 0);
481 int evlist__enable_event_idx(struct evlist *evlist, struct evsel *evsel, int idx)
483 bool per_cpu_mmaps = !perf_cpu_map__empty(evlist->core.cpus);
486 return evlist__enable_event_cpu(evlist, evsel, idx);
488 return evlist__enable_event_thread(evlist, evsel, idx);
491 int evlist__add_pollfd(struct evlist *evlist, int fd)
493 return perf_evlist__add_pollfd(&evlist->core, fd, NULL, POLLIN, fdarray_flag__default);
496 int evlist__filter_pollfd(struct evlist *evlist, short revents_and_mask)
498 return perf_evlist__filter_pollfd(&evlist->core, revents_and_mask);
501 int evlist__poll(struct evlist *evlist, int timeout)
503 return perf_evlist__poll(&evlist->core, timeout);
506 struct perf_sample_id *evlist__id2sid(struct evlist *evlist, u64 id)
508 struct hlist_head *head;
509 struct perf_sample_id *sid;
512 hash = hash_64(id, PERF_EVLIST__HLIST_BITS);
513 head = &evlist->core.heads[hash];
515 hlist_for_each_entry(sid, head, node)
522 struct evsel *evlist__id2evsel(struct evlist *evlist, u64 id)
524 struct perf_sample_id *sid;
526 if (evlist->core.nr_entries == 1 || !id)
527 return evlist__first(evlist);
529 sid = evlist__id2sid(evlist, id);
531 return container_of(sid->evsel, struct evsel, core);
533 if (!evlist__sample_id_all(evlist))
534 return evlist__first(evlist);
539 struct evsel *evlist__id2evsel_strict(struct evlist *evlist, u64 id)
541 struct perf_sample_id *sid;
546 sid = evlist__id2sid(evlist, id);
548 return container_of(sid->evsel, struct evsel, core);
553 static int evlist__event2id(struct evlist *evlist, union perf_event *event, u64 *id)
555 const __u64 *array = event->sample.array;
558 n = (event->header.size - sizeof(event->header)) >> 3;
560 if (event->header.type == PERF_RECORD_SAMPLE) {
561 if (evlist->id_pos >= n)
563 *id = array[evlist->id_pos];
565 if (evlist->is_pos > n)
573 struct evsel *evlist__event2evsel(struct evlist *evlist, union perf_event *event)
575 struct evsel *first = evlist__first(evlist);
576 struct hlist_head *head;
577 struct perf_sample_id *sid;
581 if (evlist->core.nr_entries == 1)
584 if (!first->core.attr.sample_id_all &&
585 event->header.type != PERF_RECORD_SAMPLE)
588 if (evlist__event2id(evlist, event, &id))
591 /* Synthesized events have an id of zero */
595 hash = hash_64(id, PERF_EVLIST__HLIST_BITS);
596 head = &evlist->core.heads[hash];
598 hlist_for_each_entry(sid, head, node) {
600 return container_of(sid->evsel, struct evsel, core);
605 static int evlist__set_paused(struct evlist *evlist, bool value)
609 if (!evlist->overwrite_mmap)
612 for (i = 0; i < evlist->core.nr_mmaps; i++) {
613 int fd = evlist->overwrite_mmap[i].core.fd;
618 err = ioctl(fd, PERF_EVENT_IOC_PAUSE_OUTPUT, value ? 1 : 0);
625 static int evlist__pause(struct evlist *evlist)
627 return evlist__set_paused(evlist, true);
630 static int evlist__resume(struct evlist *evlist)
632 return evlist__set_paused(evlist, false);
635 static void evlist__munmap_nofree(struct evlist *evlist)
640 for (i = 0; i < evlist->core.nr_mmaps; i++)
641 perf_mmap__munmap(&evlist->mmap[i].core);
643 if (evlist->overwrite_mmap)
644 for (i = 0; i < evlist->core.nr_mmaps; i++)
645 perf_mmap__munmap(&evlist->overwrite_mmap[i].core);
648 void evlist__munmap(struct evlist *evlist)
650 evlist__munmap_nofree(evlist);
651 zfree(&evlist->mmap);
652 zfree(&evlist->overwrite_mmap);
655 static void perf_mmap__unmap_cb(struct perf_mmap *map)
657 struct mmap *m = container_of(map, struct mmap, core);
662 static struct mmap *evlist__alloc_mmap(struct evlist *evlist,
668 map = zalloc(evlist->core.nr_mmaps * sizeof(struct mmap));
672 for (i = 0; i < evlist->core.nr_mmaps; i++) {
673 struct perf_mmap *prev = i ? &map[i - 1].core : NULL;
676 * When the perf_mmap() call is made we grab one refcount, plus
677 * one extra to let perf_mmap__consume() get the last
678 * events after all real references (perf_mmap__get()) are
681 * Each PERF_EVENT_IOC_SET_OUTPUT points to this mmap and
682 * thus does perf_mmap__get() on it.
684 perf_mmap__init(&map[i].core, prev, overwrite, perf_mmap__unmap_cb);
691 perf_evlist__mmap_cb_idx(struct perf_evlist *_evlist,
692 struct perf_mmap_param *_mp,
693 int idx, bool per_cpu)
695 struct evlist *evlist = container_of(_evlist, struct evlist, core);
696 struct mmap_params *mp = container_of(_mp, struct mmap_params, core);
698 auxtrace_mmap_params__set_idx(&mp->auxtrace_mp, evlist, idx, per_cpu);
701 static struct perf_mmap*
702 perf_evlist__mmap_cb_get(struct perf_evlist *_evlist, bool overwrite, int idx)
704 struct evlist *evlist = container_of(_evlist, struct evlist, core);
707 maps = overwrite ? evlist->overwrite_mmap : evlist->mmap;
710 maps = evlist__alloc_mmap(evlist, overwrite);
715 evlist->overwrite_mmap = maps;
716 if (evlist->bkw_mmap_state == BKW_MMAP_NOTREADY)
717 evlist__toggle_bkw_mmap(evlist, BKW_MMAP_RUNNING);
723 return &maps[idx].core;
727 perf_evlist__mmap_cb_mmap(struct perf_mmap *_map, struct perf_mmap_param *_mp,
730 struct mmap *map = container_of(_map, struct mmap, core);
731 struct mmap_params *mp = container_of(_mp, struct mmap_params, core);
733 return mmap__mmap(map, mp, output, cpu);
736 unsigned long perf_event_mlock_kb_in_pages(void)
741 if (sysctl__read_int("kernel/perf_event_mlock_kb", &max) < 0) {
743 * Pick a once upon a time good value, i.e. things look
744 * strange since we can't read a sysctl value, but lets not
749 max -= (page_size / 1024);
752 pages = (max * 1024) / page_size;
753 if (!is_power_of_2(pages))
754 pages = rounddown_pow_of_two(pages);
759 size_t evlist__mmap_size(unsigned long pages)
761 if (pages == UINT_MAX)
762 pages = perf_event_mlock_kb_in_pages();
763 else if (!is_power_of_2(pages))
766 return (pages + 1) * page_size;
769 static long parse_pages_arg(const char *str, unsigned long min,
772 unsigned long pages, val;
773 static struct parse_tag tags[] = {
774 { .tag = 'B', .mult = 1 },
775 { .tag = 'K', .mult = 1 << 10 },
776 { .tag = 'M', .mult = 1 << 20 },
777 { .tag = 'G', .mult = 1 << 30 },
784 val = parse_tag_value(str, tags);
785 if (val != (unsigned long) -1) {
786 /* we got file size value */
787 pages = PERF_ALIGN(val, page_size) / page_size;
789 /* we got pages count value */
791 pages = strtoul(str, &eptr, 10);
796 if (pages == 0 && min == 0) {
797 /* leave number of pages at 0 */
798 } else if (!is_power_of_2(pages)) {
801 /* round pages up to next power of 2 */
802 pages = roundup_pow_of_two(pages);
806 unit_number__scnprintf(buf, sizeof(buf), pages * page_size);
807 pr_info("rounding mmap pages size to %s (%lu pages)\n",
817 int __evlist__parse_mmap_pages(unsigned int *mmap_pages, const char *str)
819 unsigned long max = UINT_MAX;
822 if (max > SIZE_MAX / page_size)
823 max = SIZE_MAX / page_size;
825 pages = parse_pages_arg(str, 1, max);
827 pr_err("Invalid argument for --mmap_pages/-m\n");
835 int evlist__parse_mmap_pages(const struct option *opt, const char *str, int unset __maybe_unused)
837 return __evlist__parse_mmap_pages(opt->value, str);
841 * evlist__mmap_ex - Create mmaps to receive events.
842 * @evlist: list of events
843 * @pages: map length in pages
844 * @overwrite: overwrite older events?
845 * @auxtrace_pages - auxtrace map length in pages
846 * @auxtrace_overwrite - overwrite older auxtrace data?
848 * If @overwrite is %false the user needs to signal event consumption using
849 * perf_mmap__write_tail(). Using evlist__mmap_read() does this
852 * Similarly, if @auxtrace_overwrite is %false the user needs to signal data
853 * consumption using auxtrace_mmap__write_tail().
855 * Return: %0 on success, negative error code otherwise.
857 int evlist__mmap_ex(struct evlist *evlist, unsigned int pages,
858 unsigned int auxtrace_pages,
859 bool auxtrace_overwrite, int nr_cblocks, int affinity, int flush,
863 * Delay setting mp.prot: set it before calling perf_mmap__mmap.
864 * Its value is decided by evsel's write_backward.
865 * So &mp should not be passed through const pointer.
867 struct mmap_params mp = {
868 .nr_cblocks = nr_cblocks,
869 .affinity = affinity,
871 .comp_level = comp_level
873 struct perf_evlist_mmap_ops ops = {
874 .idx = perf_evlist__mmap_cb_idx,
875 .get = perf_evlist__mmap_cb_get,
876 .mmap = perf_evlist__mmap_cb_mmap,
879 evlist->core.mmap_len = evlist__mmap_size(pages);
880 pr_debug("mmap size %zuB\n", evlist->core.mmap_len);
882 auxtrace_mmap_params__init(&mp.auxtrace_mp, evlist->core.mmap_len,
883 auxtrace_pages, auxtrace_overwrite);
885 return perf_evlist__mmap_ops(&evlist->core, &ops, &mp.core);
888 int evlist__mmap(struct evlist *evlist, unsigned int pages)
890 return evlist__mmap_ex(evlist, pages, 0, false, 0, PERF_AFFINITY_SYS, 1, 0);
893 int evlist__create_maps(struct evlist *evlist, struct target *target)
895 bool all_threads = (target->per_thread && target->system_wide);
896 struct perf_cpu_map *cpus;
897 struct perf_thread_map *threads;
900 * If specify '-a' and '--per-thread' to perf record, perf record
901 * will override '--per-thread'. target->per_thread = false and
902 * target->system_wide = true.
904 * If specify '--per-thread' only to perf record,
905 * target->per_thread = true and target->system_wide = false.
907 * So target->per_thread && target->system_wide is false.
908 * For perf record, thread_map__new_str doesn't call
909 * thread_map__new_all_cpus. That will keep perf record's
912 * For perf stat, it allows the case that target->per_thread and
913 * target->system_wide are all true. It means to collect system-wide
914 * per-thread data. thread_map__new_str will call
915 * thread_map__new_all_cpus to enumerate all threads.
917 threads = thread_map__new_str(target->pid, target->tid, target->uid,
923 if (target__uses_dummy_map(target))
924 cpus = perf_cpu_map__dummy_new();
926 cpus = perf_cpu_map__new(target->cpu_list);
929 goto out_delete_threads;
931 evlist->core.has_user_cpus = !!target->cpu_list;
933 perf_evlist__set_maps(&evlist->core, cpus, threads);
935 /* as evlist now has references, put count here */
936 perf_cpu_map__put(cpus);
937 perf_thread_map__put(threads);
942 perf_thread_map__put(threads);
946 int evlist__apply_filters(struct evlist *evlist, struct evsel **err_evsel)
951 evlist__for_each_entry(evlist, evsel) {
952 if (evsel->filter == NULL)
956 * filters only work for tracepoint event, which doesn't have cpu limit.
957 * So evlist and evsel should always be same.
959 err = perf_evsel__apply_filter(&evsel->core, evsel->filter);
969 int evlist__set_tp_filter(struct evlist *evlist, const char *filter)
977 evlist__for_each_entry(evlist, evsel) {
978 if (evsel->core.attr.type != PERF_TYPE_TRACEPOINT)
981 err = evsel__set_filter(evsel, filter);
989 int evlist__append_tp_filter(struct evlist *evlist, const char *filter)
997 evlist__for_each_entry(evlist, evsel) {
998 if (evsel->core.attr.type != PERF_TYPE_TRACEPOINT)
1001 err = evsel__append_tp_filter(evsel, filter);
1009 char *asprintf__tp_filter_pids(size_t npids, pid_t *pids)
1014 for (i = 0; i < npids; ++i) {
1016 if (asprintf(&filter, "common_pid != %d", pids[i]) < 0)
1021 if (asprintf(&tmp, "%s && common_pid != %d", filter, pids[i]) < 0)
1035 int evlist__set_tp_filter_pids(struct evlist *evlist, size_t npids, pid_t *pids)
1037 char *filter = asprintf__tp_filter_pids(npids, pids);
1038 int ret = evlist__set_tp_filter(evlist, filter);
1044 int evlist__set_tp_filter_pid(struct evlist *evlist, pid_t pid)
1046 return evlist__set_tp_filter_pids(evlist, 1, &pid);
1049 int evlist__append_tp_filter_pids(struct evlist *evlist, size_t npids, pid_t *pids)
1051 char *filter = asprintf__tp_filter_pids(npids, pids);
1052 int ret = evlist__append_tp_filter(evlist, filter);
1058 int evlist__append_tp_filter_pid(struct evlist *evlist, pid_t pid)
1060 return evlist__append_tp_filter_pids(evlist, 1, &pid);
1063 bool evlist__valid_sample_type(struct evlist *evlist)
1067 if (evlist->core.nr_entries == 1)
1070 if (evlist->id_pos < 0 || evlist->is_pos < 0)
1073 evlist__for_each_entry(evlist, pos) {
1074 if (pos->id_pos != evlist->id_pos ||
1075 pos->is_pos != evlist->is_pos)
1082 u64 __evlist__combined_sample_type(struct evlist *evlist)
1084 struct evsel *evsel;
1086 if (evlist->combined_sample_type)
1087 return evlist->combined_sample_type;
1089 evlist__for_each_entry(evlist, evsel)
1090 evlist->combined_sample_type |= evsel->core.attr.sample_type;
1092 return evlist->combined_sample_type;
1095 u64 evlist__combined_sample_type(struct evlist *evlist)
1097 evlist->combined_sample_type = 0;
1098 return __evlist__combined_sample_type(evlist);
1101 u64 evlist__combined_branch_type(struct evlist *evlist)
1103 struct evsel *evsel;
1104 u64 branch_type = 0;
1106 evlist__for_each_entry(evlist, evsel)
1107 branch_type |= evsel->core.attr.branch_sample_type;
1111 bool evlist__valid_read_format(struct evlist *evlist)
1113 struct evsel *first = evlist__first(evlist), *pos = first;
1114 u64 read_format = first->core.attr.read_format;
1115 u64 sample_type = first->core.attr.sample_type;
1117 evlist__for_each_entry(evlist, pos) {
1118 if (read_format != pos->core.attr.read_format) {
1119 pr_debug("Read format differs %#" PRIx64 " vs %#" PRIx64 "\n",
1120 read_format, (u64)pos->core.attr.read_format);
1124 /* PERF_SAMPLE_READ imples PERF_FORMAT_ID. */
1125 if ((sample_type & PERF_SAMPLE_READ) &&
1126 !(read_format & PERF_FORMAT_ID)) {
1133 u16 evlist__id_hdr_size(struct evlist *evlist)
1135 struct evsel *first = evlist__first(evlist);
1136 struct perf_sample *data;
1140 if (!first->core.attr.sample_id_all)
1143 sample_type = first->core.attr.sample_type;
1145 if (sample_type & PERF_SAMPLE_TID)
1146 size += sizeof(data->tid) * 2;
1148 if (sample_type & PERF_SAMPLE_TIME)
1149 size += sizeof(data->time);
1151 if (sample_type & PERF_SAMPLE_ID)
1152 size += sizeof(data->id);
1154 if (sample_type & PERF_SAMPLE_STREAM_ID)
1155 size += sizeof(data->stream_id);
1157 if (sample_type & PERF_SAMPLE_CPU)
1158 size += sizeof(data->cpu) * 2;
1160 if (sample_type & PERF_SAMPLE_IDENTIFIER)
1161 size += sizeof(data->id);
1166 bool evlist__valid_sample_id_all(struct evlist *evlist)
1168 struct evsel *first = evlist__first(evlist), *pos = first;
1170 evlist__for_each_entry_continue(evlist, pos) {
1171 if (first->core.attr.sample_id_all != pos->core.attr.sample_id_all)
1178 bool evlist__sample_id_all(struct evlist *evlist)
1180 struct evsel *first = evlist__first(evlist);
1181 return first->core.attr.sample_id_all;
1184 void evlist__set_selected(struct evlist *evlist, struct evsel *evsel)
1186 evlist->selected = evsel;
1189 void evlist__close(struct evlist *evlist)
1191 struct evsel *evsel;
1192 struct affinity affinity;
1196 * With perf record core.cpus is usually NULL.
1197 * Use the old method to handle this for now.
1199 if (!evlist->core.cpus) {
1200 evlist__for_each_entry_reverse(evlist, evsel)
1201 evsel__close(evsel);
1205 if (affinity__setup(&affinity) < 0)
1207 evlist__for_each_cpu(evlist, i, cpu) {
1208 affinity__set(&affinity, cpu);
1210 evlist__for_each_entry_reverse(evlist, evsel) {
1211 if (evsel__cpu_iter_skip(evsel, cpu))
1213 perf_evsel__close_cpu(&evsel->core, evsel->cpu_iter - 1);
1216 affinity__cleanup(&affinity);
1217 evlist__for_each_entry_reverse(evlist, evsel) {
1218 perf_evsel__free_fd(&evsel->core);
1219 perf_evsel__free_id(&evsel->core);
1223 static int evlist__create_syswide_maps(struct evlist *evlist)
1225 struct perf_cpu_map *cpus;
1226 struct perf_thread_map *threads;
1230 * Try reading /sys/devices/system/cpu/online to get
1233 * FIXME: -ENOMEM is the best we can do here, the cpu_map
1234 * code needs an overhaul to properly forward the
1235 * error, and we may not want to do that fallback to a
1236 * default cpu identity map :-\
1238 cpus = perf_cpu_map__new(NULL);
1242 threads = perf_thread_map__new_dummy();
1246 perf_evlist__set_maps(&evlist->core, cpus, threads);
1248 perf_thread_map__put(threads);
1250 perf_cpu_map__put(cpus);
1255 int evlist__open(struct evlist *evlist)
1257 struct evsel *evsel;
1261 * Default: one fd per CPU, all threads, aka systemwide
1262 * as sys_perf_event_open(cpu = -1, thread = -1) is EINVAL
1264 if (evlist->core.threads == NULL && evlist->core.cpus == NULL) {
1265 err = evlist__create_syswide_maps(evlist);
1270 evlist__update_id_pos(evlist);
1272 evlist__for_each_entry(evlist, evsel) {
1273 err = evsel__open(evsel, evsel->core.cpus, evsel->core.threads);
1280 evlist__close(evlist);
1285 int evlist__prepare_workload(struct evlist *evlist, struct target *target, const char *argv[],
1286 bool pipe_output, void (*exec_error)(int signo, siginfo_t *info, void *ucontext))
1288 int child_ready_pipe[2], go_pipe[2];
1291 if (pipe(child_ready_pipe) < 0) {
1292 perror("failed to create 'ready' pipe");
1296 if (pipe(go_pipe) < 0) {
1297 perror("failed to create 'go' pipe");
1298 goto out_close_ready_pipe;
1301 evlist->workload.pid = fork();
1302 if (evlist->workload.pid < 0) {
1303 perror("failed to fork");
1304 goto out_close_pipes;
1307 if (!evlist->workload.pid) {
1313 signal(SIGTERM, SIG_DFL);
1315 close(child_ready_pipe[0]);
1317 fcntl(go_pipe[0], F_SETFD, FD_CLOEXEC);
1320 * Tell the parent we're ready to go
1322 close(child_ready_pipe[1]);
1325 * Wait until the parent tells us to go.
1327 ret = read(go_pipe[0], &bf, 1);
1329 * The parent will ask for the execvp() to be performed by
1330 * writing exactly one byte, in workload.cork_fd, usually via
1331 * evlist__start_workload().
1333 * For cancelling the workload without actually running it,
1334 * the parent will just close workload.cork_fd, without writing
1335 * anything, i.e. read will return zero and we just exit()
1340 perror("unable to read pipe");
1344 execvp(argv[0], (char **)argv);
1349 val.sival_int = errno;
1350 if (sigqueue(getppid(), SIGUSR1, val))
1358 struct sigaction act = {
1359 .sa_flags = SA_SIGINFO,
1360 .sa_sigaction = exec_error,
1362 sigaction(SIGUSR1, &act, NULL);
1365 if (target__none(target)) {
1366 if (evlist->core.threads == NULL) {
1367 fprintf(stderr, "FATAL: evlist->threads need to be set at this point (%s:%d).\n",
1368 __func__, __LINE__);
1369 goto out_close_pipes;
1371 perf_thread_map__set_pid(evlist->core.threads, 0, evlist->workload.pid);
1374 close(child_ready_pipe[1]);
1377 * wait for child to settle
1379 if (read(child_ready_pipe[0], &bf, 1) == -1) {
1380 perror("unable to read pipe");
1381 goto out_close_pipes;
1384 fcntl(go_pipe[1], F_SETFD, FD_CLOEXEC);
1385 evlist->workload.cork_fd = go_pipe[1];
1386 close(child_ready_pipe[0]);
1392 out_close_ready_pipe:
1393 close(child_ready_pipe[0]);
1394 close(child_ready_pipe[1]);
1398 int evlist__start_workload(struct evlist *evlist)
1400 if (evlist->workload.cork_fd > 0) {
1404 * Remove the cork, let it rip!
1406 ret = write(evlist->workload.cork_fd, &bf, 1);
1408 perror("unable to write to pipe");
1410 close(evlist->workload.cork_fd);
1417 int evlist__parse_sample(struct evlist *evlist, union perf_event *event, struct perf_sample *sample)
1419 struct evsel *evsel = evlist__event2evsel(evlist, event);
1423 return evsel__parse_sample(evsel, event, sample);
1426 int evlist__parse_sample_timestamp(struct evlist *evlist, union perf_event *event, u64 *timestamp)
1428 struct evsel *evsel = evlist__event2evsel(evlist, event);
1432 return evsel__parse_sample_timestamp(evsel, event, timestamp);
1435 int evlist__strerror_open(struct evlist *evlist, int err, char *buf, size_t size)
1438 char sbuf[STRERR_BUFSIZE], *emsg = str_error_r(err, sbuf, sizeof(sbuf));
1443 printed = scnprintf(buf, size,
1445 "Hint:\tCheck /proc/sys/kernel/perf_event_paranoid setting.", emsg);
1447 value = perf_event_paranoid();
1449 printed += scnprintf(buf + printed, size - printed, "\nHint:\t");
1452 printed += scnprintf(buf + printed, size - printed,
1453 "For your workloads it needs to be <= 1\nHint:\t");
1455 printed += scnprintf(buf + printed, size - printed,
1456 "For system wide tracing it needs to be set to -1.\n");
1458 printed += scnprintf(buf + printed, size - printed,
1459 "Hint:\tTry: 'sudo sh -c \"echo -1 > /proc/sys/kernel/perf_event_paranoid\"'\n"
1460 "Hint:\tThe current value is %d.", value);
1463 struct evsel *first = evlist__first(evlist);
1466 if (sysctl__read_int("kernel/perf_event_max_sample_rate", &max_freq) < 0)
1469 if (first->core.attr.sample_freq < (u64)max_freq)
1472 printed = scnprintf(buf, size,
1474 "Hint:\tCheck /proc/sys/kernel/perf_event_max_sample_rate.\n"
1475 "Hint:\tThe current value is %d and %" PRIu64 " is being requested.",
1476 emsg, max_freq, first->core.attr.sample_freq);
1481 scnprintf(buf, size, "%s", emsg);
1488 int evlist__strerror_mmap(struct evlist *evlist, int err, char *buf, size_t size)
1490 char sbuf[STRERR_BUFSIZE], *emsg = str_error_r(err, sbuf, sizeof(sbuf));
1491 int pages_attempted = evlist->core.mmap_len / 1024, pages_max_per_user, printed = 0;
1495 sysctl__read_int("kernel/perf_event_mlock_kb", &pages_max_per_user);
1496 printed += scnprintf(buf + printed, size - printed,
1498 "Hint:\tCheck /proc/sys/kernel/perf_event_mlock_kb (%d kB) setting.\n"
1499 "Hint:\tTried using %zd kB.\n",
1500 emsg, pages_max_per_user, pages_attempted);
1502 if (pages_attempted >= pages_max_per_user) {
1503 printed += scnprintf(buf + printed, size - printed,
1504 "Hint:\tTry 'sudo sh -c \"echo %d > /proc/sys/kernel/perf_event_mlock_kb\"', or\n",
1505 pages_max_per_user + pages_attempted);
1508 printed += scnprintf(buf + printed, size - printed,
1509 "Hint:\tTry using a smaller -m/--mmap-pages value.");
1512 scnprintf(buf, size, "%s", emsg);
1519 void evlist__to_front(struct evlist *evlist, struct evsel *move_evsel)
1521 struct evsel *evsel, *n;
1524 if (move_evsel == evlist__first(evlist))
1527 evlist__for_each_entry_safe(evlist, n, evsel) {
1528 if (evsel->leader == move_evsel->leader)
1529 list_move_tail(&evsel->core.node, &move);
1532 list_splice(&move, &evlist->core.entries);
1535 struct evsel *evlist__get_tracking_event(struct evlist *evlist)
1537 struct evsel *evsel;
1539 evlist__for_each_entry(evlist, evsel) {
1540 if (evsel->tracking)
1544 return evlist__first(evlist);
1547 void evlist__set_tracking_event(struct evlist *evlist, struct evsel *tracking_evsel)
1549 struct evsel *evsel;
1551 if (tracking_evsel->tracking)
1554 evlist__for_each_entry(evlist, evsel) {
1555 if (evsel != tracking_evsel)
1556 evsel->tracking = false;
1559 tracking_evsel->tracking = true;
1562 struct evsel *evlist__find_evsel_by_str(struct evlist *evlist, const char *str)
1564 struct evsel *evsel;
1566 evlist__for_each_entry(evlist, evsel) {
1569 if (strcmp(str, evsel->name) == 0)
1576 void evlist__toggle_bkw_mmap(struct evlist *evlist, enum bkw_mmap_state state)
1578 enum bkw_mmap_state old_state = evlist->bkw_mmap_state;
1585 if (!evlist->overwrite_mmap)
1588 switch (old_state) {
1589 case BKW_MMAP_NOTREADY: {
1590 if (state != BKW_MMAP_RUNNING)
1594 case BKW_MMAP_RUNNING: {
1595 if (state != BKW_MMAP_DATA_PENDING)
1600 case BKW_MMAP_DATA_PENDING: {
1601 if (state != BKW_MMAP_EMPTY)
1605 case BKW_MMAP_EMPTY: {
1606 if (state != BKW_MMAP_RUNNING)
1612 WARN_ONCE(1, "Shouldn't get there\n");
1615 evlist->bkw_mmap_state = state;
1619 evlist__pause(evlist);
1622 evlist__resume(evlist);
1633 bool evlist__exclude_kernel(struct evlist *evlist)
1635 struct evsel *evsel;
1637 evlist__for_each_entry(evlist, evsel) {
1638 if (!evsel->core.attr.exclude_kernel)
1646 * Events in data file are not collect in groups, but we still want
1647 * the group display. Set the artificial group and set the leader's
1648 * forced_leader flag to notify the display code.
1650 void evlist__force_leader(struct evlist *evlist)
1652 if (!evlist->nr_groups) {
1653 struct evsel *leader = evlist__first(evlist);
1655 evlist__set_leader(evlist);
1656 leader->forced_leader = true;
1660 struct evsel *evlist__reset_weak_group(struct evlist *evsel_list, struct evsel *evsel, bool close)
1662 struct evsel *c2, *leader;
1663 bool is_open = true;
1665 leader = evsel->leader;
1666 pr_debug("Weak group for %s/%d failed\n",
1667 leader->name, leader->core.nr_members);
1670 * for_each_group_member doesn't work here because it doesn't
1671 * include the first entry.
1673 evlist__for_each_entry(evsel_list, c2) {
1676 if (c2->leader == leader) {
1677 if (is_open && close)
1678 perf_evsel__close(&c2->core);
1680 c2->core.nr_members = 0;
1682 * Set this for all former members of the group
1683 * to indicate they get reopened.
1685 c2->reset_group = true;
1691 static int evlist__parse_control_fifo(const char *str, int *ctl_fd, int *ctl_fd_ack, bool *ctl_fd_close)
1696 if (strncmp(str, "fifo:", 5))
1700 if (!*str || *str == ',')
1712 * O_RDWR avoids POLLHUPs which is necessary to allow the other
1713 * end of a FIFO to be repeatedly opened and closed.
1715 fd = open(s, O_RDWR | O_NONBLOCK | O_CLOEXEC);
1717 pr_err("Failed to open '%s'\n", s);
1722 *ctl_fd_close = true;
1725 /* O_RDWR | O_NONBLOCK means the other end need not be open */
1726 fd = open(p, O_RDWR | O_NONBLOCK | O_CLOEXEC);
1728 pr_err("Failed to open '%s'\n", p);
1740 int evlist__parse_control(const char *str, int *ctl_fd, int *ctl_fd_ack, bool *ctl_fd_close)
1742 char *comma = NULL, *endptr = NULL;
1744 *ctl_fd_close = false;
1746 if (strncmp(str, "fd:", 3))
1747 return evlist__parse_control_fifo(str, ctl_fd, ctl_fd_ack, ctl_fd_close);
1749 *ctl_fd = strtoul(&str[3], &endptr, 0);
1750 if (endptr == &str[3])
1753 comma = strchr(str, ',');
1755 if (endptr != comma)
1758 *ctl_fd_ack = strtoul(comma + 1, &endptr, 0);
1759 if (endptr == comma + 1 || *endptr != '\0')
1766 void evlist__close_control(int ctl_fd, int ctl_fd_ack, bool *ctl_fd_close)
1768 if (*ctl_fd_close) {
1769 *ctl_fd_close = false;
1771 if (ctl_fd_ack >= 0)
1776 int evlist__initialize_ctlfd(struct evlist *evlist, int fd, int ack)
1779 pr_debug("Control descriptor is not initialized\n");
1783 evlist->ctl_fd.pos = perf_evlist__add_pollfd(&evlist->core, fd, NULL, POLLIN,
1784 fdarray_flag__nonfilterable);
1785 if (evlist->ctl_fd.pos < 0) {
1786 evlist->ctl_fd.pos = -1;
1787 pr_err("Failed to add ctl fd entry: %m\n");
1791 evlist->ctl_fd.fd = fd;
1792 evlist->ctl_fd.ack = ack;
1797 bool evlist__ctlfd_initialized(struct evlist *evlist)
1799 return evlist->ctl_fd.pos >= 0;
1802 int evlist__finalize_ctlfd(struct evlist *evlist)
1804 struct pollfd *entries = evlist->core.pollfd.entries;
1806 if (!evlist__ctlfd_initialized(evlist))
1809 entries[evlist->ctl_fd.pos].fd = -1;
1810 entries[evlist->ctl_fd.pos].events = 0;
1811 entries[evlist->ctl_fd.pos].revents = 0;
1813 evlist->ctl_fd.pos = -1;
1814 evlist->ctl_fd.ack = -1;
1815 evlist->ctl_fd.fd = -1;
1820 static int evlist__ctlfd_recv(struct evlist *evlist, enum evlist_ctl_cmd *cmd,
1821 char *cmd_data, size_t data_size)
1825 size_t bytes_read = 0;
1827 *cmd = EVLIST_CTL_CMD_UNSUPPORTED;
1828 memset(cmd_data, 0, data_size);
1832 err = read(evlist->ctl_fd.fd, &c, 1);
1834 if (c == '\n' || c == '\0')
1836 cmd_data[bytes_read++] = c;
1837 if (bytes_read == data_size)
1840 } else if (err == -1) {
1843 if (errno == EAGAIN || errno == EWOULDBLOCK)
1846 pr_err("Failed to read from ctlfd %d: %m\n", evlist->ctl_fd.fd);
1851 pr_debug("Message from ctl_fd: \"%s%s\"\n", cmd_data,
1852 bytes_read == data_size ? "" : c == '\n' ? "\\n" : "\\0");
1854 if (bytes_read > 0) {
1855 if (!strncmp(cmd_data, EVLIST_CTL_CMD_ENABLE_TAG,
1856 (sizeof(EVLIST_CTL_CMD_ENABLE_TAG)-1))) {
1857 *cmd = EVLIST_CTL_CMD_ENABLE;
1858 } else if (!strncmp(cmd_data, EVLIST_CTL_CMD_DISABLE_TAG,
1859 (sizeof(EVLIST_CTL_CMD_DISABLE_TAG)-1))) {
1860 *cmd = EVLIST_CTL_CMD_DISABLE;
1861 } else if (!strncmp(cmd_data, EVLIST_CTL_CMD_SNAPSHOT_TAG,
1862 (sizeof(EVLIST_CTL_CMD_SNAPSHOT_TAG)-1))) {
1863 *cmd = EVLIST_CTL_CMD_SNAPSHOT;
1864 pr_debug("is snapshot\n");
1868 return bytes_read ? (int)bytes_read : err;
1871 int evlist__ctlfd_ack(struct evlist *evlist)
1875 if (evlist->ctl_fd.ack == -1)
1878 err = write(evlist->ctl_fd.ack, EVLIST_CTL_CMD_ACK_TAG,
1879 sizeof(EVLIST_CTL_CMD_ACK_TAG));
1881 pr_err("failed to write to ctl_ack_fd %d: %m\n", evlist->ctl_fd.ack);
1886 int evlist__ctlfd_process(struct evlist *evlist, enum evlist_ctl_cmd *cmd)
1889 char cmd_data[EVLIST_CTL_CMD_MAX_LEN];
1890 int ctlfd_pos = evlist->ctl_fd.pos;
1891 struct pollfd *entries = evlist->core.pollfd.entries;
1893 if (!evlist__ctlfd_initialized(evlist) || !entries[ctlfd_pos].revents)
1896 if (entries[ctlfd_pos].revents & POLLIN) {
1897 err = evlist__ctlfd_recv(evlist, cmd, cmd_data,
1898 EVLIST_CTL_CMD_MAX_LEN);
1901 case EVLIST_CTL_CMD_ENABLE:
1902 evlist__enable(evlist);
1904 case EVLIST_CTL_CMD_DISABLE:
1905 evlist__disable(evlist);
1907 case EVLIST_CTL_CMD_SNAPSHOT:
1909 case EVLIST_CTL_CMD_ACK:
1910 case EVLIST_CTL_CMD_UNSUPPORTED:
1912 pr_debug("ctlfd: unsupported %d\n", *cmd);
1915 if (!(*cmd == EVLIST_CTL_CMD_ACK || *cmd == EVLIST_CTL_CMD_UNSUPPORTED ||
1916 *cmd == EVLIST_CTL_CMD_SNAPSHOT))
1917 evlist__ctlfd_ack(evlist);
1921 if (entries[ctlfd_pos].revents & (POLLHUP | POLLERR))
1922 evlist__finalize_ctlfd(evlist);
1924 entries[ctlfd_pos].revents = 0;
1929 struct evsel *evlist__find_evsel(struct evlist *evlist, int idx)
1931 struct evsel *evsel;
1933 evlist__for_each_entry(evlist, evsel) {
1934 if (evsel->idx == idx)