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 "thread_map.h"
22 #include "bpf-event.h"
27 #include "parse-events.h"
28 #include <subcmd/parse-options.h>
31 #include <sys/ioctl.h>
34 #include <linux/bitops.h>
35 #include <linux/hash.h>
36 #include <linux/log2.h>
37 #include <linux/err.h>
38 #include <linux/string.h>
39 #include <linux/zalloc.h>
40 #include <perf/evlist.h>
41 #include <perf/evsel.h>
42 #include <perf/cpumap.h>
44 #include <internal/xyarray.h>
46 #ifdef LACKS_SIGQUEUE_PROTOTYPE
47 int sigqueue(pid_t pid, int sig, const union sigval value);
50 #define FD(e, x, y) (*(int *)xyarray__entry(e->core.fd, x, y))
51 #define SID(e, x, y) xyarray__entry(e->sample_id, x, y)
53 void evlist__init(struct evlist *evlist, struct perf_cpu_map *cpus,
54 struct perf_thread_map *threads)
58 for (i = 0; i < PERF_EVLIST__HLIST_SIZE; ++i)
59 INIT_HLIST_HEAD(&evlist->heads[i]);
60 perf_evlist__init(&evlist->core);
61 perf_evlist__set_maps(&evlist->core, cpus, threads);
62 fdarray__init(&evlist->pollfd, 64);
63 evlist->workload.pid = -1;
64 evlist->bkw_mmap_state = BKW_MMAP_NOTREADY;
67 struct evlist *evlist__new(void)
69 struct evlist *evlist = zalloc(sizeof(*evlist));
72 evlist__init(evlist, NULL, NULL);
77 struct evlist *perf_evlist__new_default(void)
79 struct evlist *evlist = evlist__new();
81 if (evlist && perf_evlist__add_default(evlist)) {
82 evlist__delete(evlist);
89 struct evlist *perf_evlist__new_dummy(void)
91 struct evlist *evlist = evlist__new();
93 if (evlist && perf_evlist__add_dummy(evlist)) {
94 evlist__delete(evlist);
102 * perf_evlist__set_id_pos - set the positions of event ids.
103 * @evlist: selected event list
105 * Events with compatible sample types all have the same id_pos
106 * and is_pos. For convenience, put a copy on evlist.
108 void perf_evlist__set_id_pos(struct evlist *evlist)
110 struct evsel *first = perf_evlist__first(evlist);
112 evlist->id_pos = first->id_pos;
113 evlist->is_pos = first->is_pos;
116 static void perf_evlist__update_id_pos(struct evlist *evlist)
120 evlist__for_each_entry(evlist, evsel)
121 perf_evsel__calc_id_pos(evsel);
123 perf_evlist__set_id_pos(evlist);
126 static void perf_evlist__purge(struct evlist *evlist)
128 struct evsel *pos, *n;
130 evlist__for_each_entry_safe(evlist, n, pos) {
131 list_del_init(&pos->core.node);
136 evlist->core.nr_entries = 0;
139 void perf_evlist__exit(struct evlist *evlist)
141 zfree(&evlist->mmap);
142 zfree(&evlist->overwrite_mmap);
143 fdarray__exit(&evlist->pollfd);
146 void evlist__delete(struct evlist *evlist)
151 perf_evlist__munmap(evlist);
152 evlist__close(evlist);
153 perf_cpu_map__put(evlist->core.cpus);
154 perf_thread_map__put(evlist->core.threads);
155 evlist->core.cpus = NULL;
156 evlist->core.threads = NULL;
157 perf_evlist__purge(evlist);
158 perf_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 perf_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 perf_evlist__splice_list_tail(struct evlist *evlist,
181 struct list_head *list)
183 struct evsel *evsel, *temp;
185 __evlist__for_each_entry_safe(list, temp, evsel) {
186 list_del_init(&evsel->core.node);
187 evlist__add(evlist, evsel);
191 void __perf_evlist__set_leader(struct list_head *list)
193 struct evsel *evsel, *leader;
195 leader = list_entry(list->next, struct evsel, core.node);
196 evsel = list_entry(list->prev, struct evsel, core.node);
198 leader->core.nr_members = evsel->idx - leader->idx + 1;
200 __evlist__for_each_entry(list, evsel) {
201 evsel->leader = leader;
205 void perf_evlist__set_leader(struct evlist *evlist)
207 if (evlist->core.nr_entries) {
208 evlist->nr_groups = evlist->core.nr_entries > 1 ? 1 : 0;
209 __perf_evlist__set_leader(&evlist->core.entries);
213 int __perf_evlist__add_default(struct evlist *evlist, bool precise)
215 struct evsel *evsel = perf_evsel__new_cycles(precise);
220 evlist__add(evlist, evsel);
224 int perf_evlist__add_dummy(struct evlist *evlist)
226 struct perf_event_attr attr = {
227 .type = PERF_TYPE_SOFTWARE,
228 .config = PERF_COUNT_SW_DUMMY,
229 .size = sizeof(attr), /* to capture ABI version */
231 struct evsel *evsel = perf_evsel__new_idx(&attr, evlist->core.nr_entries);
236 evlist__add(evlist, evsel);
240 static int evlist__add_attrs(struct evlist *evlist,
241 struct perf_event_attr *attrs, size_t nr_attrs)
243 struct evsel *evsel, *n;
247 for (i = 0; i < nr_attrs; i++) {
248 evsel = perf_evsel__new_idx(attrs + i, evlist->core.nr_entries + i);
250 goto out_delete_partial_list;
251 list_add_tail(&evsel->core.node, &head);
254 perf_evlist__splice_list_tail(evlist, &head);
258 out_delete_partial_list:
259 __evlist__for_each_entry_safe(&head, n, evsel)
260 evsel__delete(evsel);
264 int __perf_evlist__add_default_attrs(struct evlist *evlist,
265 struct perf_event_attr *attrs, size_t nr_attrs)
269 for (i = 0; i < nr_attrs; i++)
270 event_attr_init(attrs + i);
272 return evlist__add_attrs(evlist, attrs, nr_attrs);
276 perf_evlist__find_tracepoint_by_id(struct evlist *evlist, int id)
280 evlist__for_each_entry(evlist, evsel) {
281 if (evsel->core.attr.type == PERF_TYPE_TRACEPOINT &&
282 (int)evsel->core.attr.config == id)
290 perf_evlist__find_tracepoint_by_name(struct evlist *evlist,
295 evlist__for_each_entry(evlist, evsel) {
296 if ((evsel->core.attr.type == PERF_TYPE_TRACEPOINT) &&
297 (strcmp(evsel->name, name) == 0))
304 int perf_evlist__add_newtp(struct evlist *evlist,
305 const char *sys, const char *name, void *handler)
307 struct evsel *evsel = perf_evsel__newtp(sys, name);
312 evsel->handler = handler;
313 evlist__add(evlist, evsel);
317 static int perf_evlist__nr_threads(struct evlist *evlist,
320 if (evsel->system_wide)
323 return perf_thread_map__nr(evlist->core.threads);
326 void evlist__disable(struct evlist *evlist)
330 evlist__for_each_entry(evlist, pos) {
331 if (pos->disabled || !perf_evsel__is_group_leader(pos) || !pos->core.fd)
336 evlist->enabled = false;
339 void evlist__enable(struct evlist *evlist)
343 evlist__for_each_entry(evlist, pos) {
344 if (!perf_evsel__is_group_leader(pos) || !pos->core.fd)
349 evlist->enabled = true;
352 void perf_evlist__toggle_enable(struct evlist *evlist)
354 (evlist->enabled ? evlist__disable : evlist__enable)(evlist);
357 static int perf_evlist__enable_event_cpu(struct evlist *evlist,
358 struct evsel *evsel, int cpu)
361 int nr_threads = perf_evlist__nr_threads(evlist, evsel);
366 for (thread = 0; thread < nr_threads; thread++) {
367 int err = ioctl(FD(evsel, cpu, thread), PERF_EVENT_IOC_ENABLE, 0);
374 static int perf_evlist__enable_event_thread(struct evlist *evlist,
379 int nr_cpus = perf_cpu_map__nr(evlist->core.cpus);
384 for (cpu = 0; cpu < nr_cpus; cpu++) {
385 int err = ioctl(FD(evsel, cpu, thread), PERF_EVENT_IOC_ENABLE, 0);
392 int perf_evlist__enable_event_idx(struct evlist *evlist,
393 struct evsel *evsel, int idx)
395 bool per_cpu_mmaps = !perf_cpu_map__empty(evlist->core.cpus);
398 return perf_evlist__enable_event_cpu(evlist, evsel, idx);
400 return perf_evlist__enable_event_thread(evlist, evsel, idx);
403 int perf_evlist__alloc_pollfd(struct evlist *evlist)
405 int nr_cpus = perf_cpu_map__nr(evlist->core.cpus);
406 int nr_threads = perf_thread_map__nr(evlist->core.threads);
410 evlist__for_each_entry(evlist, evsel) {
411 if (evsel->system_wide)
414 nfds += nr_cpus * nr_threads;
417 if (fdarray__available_entries(&evlist->pollfd) < nfds &&
418 fdarray__grow(&evlist->pollfd, nfds) < 0)
424 static int __perf_evlist__add_pollfd(struct evlist *evlist, int fd,
425 struct perf_mmap *map, short revent)
427 int pos = fdarray__add(&evlist->pollfd, fd, revent | POLLERR | POLLHUP);
429 * Save the idx so that when we filter out fds POLLHUP'ed we can
430 * close the associated evlist->mmap[] entry.
433 evlist->pollfd.priv[pos].ptr = map;
435 fcntl(fd, F_SETFL, O_NONBLOCK);
441 int perf_evlist__add_pollfd(struct evlist *evlist, int fd)
443 return __perf_evlist__add_pollfd(evlist, fd, NULL, POLLIN);
446 static void perf_evlist__munmap_filtered(struct fdarray *fda, int fd,
447 void *arg __maybe_unused)
449 struct perf_mmap *map = fda->priv[fd].ptr;
455 int perf_evlist__filter_pollfd(struct evlist *evlist, short revents_and_mask)
457 return fdarray__filter(&evlist->pollfd, revents_and_mask,
458 perf_evlist__munmap_filtered, NULL);
461 int perf_evlist__poll(struct evlist *evlist, int timeout)
463 return fdarray__poll(&evlist->pollfd, timeout);
466 static void perf_evlist__id_hash(struct evlist *evlist,
468 int cpu, int thread, u64 id)
471 struct perf_sample_id *sid = SID(evsel, cpu, thread);
475 hash = hash_64(sid->id, PERF_EVLIST__HLIST_BITS);
476 hlist_add_head(&sid->node, &evlist->heads[hash]);
479 void perf_evlist__id_add(struct evlist *evlist, struct evsel *evsel,
480 int cpu, int thread, u64 id)
482 perf_evlist__id_hash(evlist, evsel, cpu, thread, id);
483 evsel->id[evsel->ids++] = id;
486 int perf_evlist__id_add_fd(struct evlist *evlist,
488 int cpu, int thread, int fd)
490 u64 read_data[4] = { 0, };
491 int id_idx = 1; /* The first entry is the counter value */
495 ret = ioctl(fd, PERF_EVENT_IOC_ID, &id);
502 /* Legacy way to get event id.. All hail to old kernels! */
505 * This way does not work with group format read, so bail
508 if (perf_evlist__read_format(evlist) & PERF_FORMAT_GROUP)
511 if (!(evsel->core.attr.read_format & PERF_FORMAT_ID) ||
512 read(fd, &read_data, sizeof(read_data)) == -1)
515 if (evsel->core.attr.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
517 if (evsel->core.attr.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
520 id = read_data[id_idx];
523 perf_evlist__id_add(evlist, evsel, cpu, thread, id);
527 static void perf_evlist__set_sid_idx(struct evlist *evlist,
528 struct evsel *evsel, int idx, int cpu,
531 struct perf_sample_id *sid = SID(evsel, cpu, thread);
533 if (evlist->core.cpus && cpu >= 0)
534 sid->cpu = evlist->core.cpus->map[cpu];
537 if (!evsel->system_wide && evlist->core.threads && thread >= 0)
538 sid->tid = perf_thread_map__pid(evlist->core.threads, thread);
543 struct perf_sample_id *perf_evlist__id2sid(struct evlist *evlist, u64 id)
545 struct hlist_head *head;
546 struct perf_sample_id *sid;
549 hash = hash_64(id, PERF_EVLIST__HLIST_BITS);
550 head = &evlist->heads[hash];
552 hlist_for_each_entry(sid, head, node)
559 struct evsel *perf_evlist__id2evsel(struct evlist *evlist, u64 id)
561 struct perf_sample_id *sid;
563 if (evlist->core.nr_entries == 1 || !id)
564 return perf_evlist__first(evlist);
566 sid = perf_evlist__id2sid(evlist, id);
570 if (!perf_evlist__sample_id_all(evlist))
571 return perf_evlist__first(evlist);
576 struct evsel *perf_evlist__id2evsel_strict(struct evlist *evlist,
579 struct perf_sample_id *sid;
584 sid = perf_evlist__id2sid(evlist, id);
591 static int perf_evlist__event2id(struct evlist *evlist,
592 union perf_event *event, u64 *id)
594 const __u64 *array = event->sample.array;
597 n = (event->header.size - sizeof(event->header)) >> 3;
599 if (event->header.type == PERF_RECORD_SAMPLE) {
600 if (evlist->id_pos >= n)
602 *id = array[evlist->id_pos];
604 if (evlist->is_pos > n)
612 struct evsel *perf_evlist__event2evsel(struct evlist *evlist,
613 union perf_event *event)
615 struct evsel *first = perf_evlist__first(evlist);
616 struct hlist_head *head;
617 struct perf_sample_id *sid;
621 if (evlist->core.nr_entries == 1)
624 if (!first->core.attr.sample_id_all &&
625 event->header.type != PERF_RECORD_SAMPLE)
628 if (perf_evlist__event2id(evlist, event, &id))
631 /* Synthesized events have an id of zero */
635 hash = hash_64(id, PERF_EVLIST__HLIST_BITS);
636 head = &evlist->heads[hash];
638 hlist_for_each_entry(sid, head, node) {
645 static int perf_evlist__set_paused(struct evlist *evlist, bool value)
649 if (!evlist->overwrite_mmap)
652 for (i = 0; i < evlist->nr_mmaps; i++) {
653 int fd = evlist->overwrite_mmap[i].fd;
658 err = ioctl(fd, PERF_EVENT_IOC_PAUSE_OUTPUT, value ? 1 : 0);
665 static int perf_evlist__pause(struct evlist *evlist)
667 return perf_evlist__set_paused(evlist, true);
670 static int perf_evlist__resume(struct evlist *evlist)
672 return perf_evlist__set_paused(evlist, false);
675 static void perf_evlist__munmap_nofree(struct evlist *evlist)
680 for (i = 0; i < evlist->nr_mmaps; i++)
681 perf_mmap__munmap(&evlist->mmap[i]);
683 if (evlist->overwrite_mmap)
684 for (i = 0; i < evlist->nr_mmaps; i++)
685 perf_mmap__munmap(&evlist->overwrite_mmap[i]);
688 void perf_evlist__munmap(struct evlist *evlist)
690 perf_evlist__munmap_nofree(evlist);
691 zfree(&evlist->mmap);
692 zfree(&evlist->overwrite_mmap);
695 static struct perf_mmap *perf_evlist__alloc_mmap(struct evlist *evlist,
699 struct perf_mmap *map;
701 evlist->nr_mmaps = perf_cpu_map__nr(evlist->core.cpus);
702 if (perf_cpu_map__empty(evlist->core.cpus))
703 evlist->nr_mmaps = perf_thread_map__nr(evlist->core.threads);
704 map = zalloc(evlist->nr_mmaps * sizeof(struct perf_mmap));
708 for (i = 0; i < evlist->nr_mmaps; i++) {
710 map[i].overwrite = overwrite;
712 * When the perf_mmap() call is made we grab one refcount, plus
713 * one extra to let perf_mmap__consume() get the last
714 * events after all real references (perf_mmap__get()) are
717 * Each PERF_EVENT_IOC_SET_OUTPUT points to this mmap and
718 * thus does perf_mmap__get() on it.
720 refcount_set(&map[i].refcnt, 0);
726 perf_evlist__should_poll(struct evlist *evlist __maybe_unused,
729 if (evsel->core.attr.write_backward)
734 static int perf_evlist__mmap_per_evsel(struct evlist *evlist, int idx,
735 struct mmap_params *mp, int cpu_idx,
736 int thread, int *_output, int *_output_overwrite)
740 int evlist_cpu = cpu_map__cpu(evlist->core.cpus, cpu_idx);
742 evlist__for_each_entry(evlist, evsel) {
743 struct perf_mmap *maps = evlist->mmap;
744 int *output = _output;
748 mp->prot = PROT_READ | PROT_WRITE;
749 if (evsel->core.attr.write_backward) {
750 output = _output_overwrite;
751 maps = evlist->overwrite_mmap;
754 maps = perf_evlist__alloc_mmap(evlist, true);
757 evlist->overwrite_mmap = maps;
758 if (evlist->bkw_mmap_state == BKW_MMAP_NOTREADY)
759 perf_evlist__toggle_bkw_mmap(evlist, BKW_MMAP_RUNNING);
761 mp->prot &= ~PROT_WRITE;
764 if (evsel->system_wide && thread)
767 cpu = perf_cpu_map__idx(evsel->core.cpus, evlist_cpu);
771 fd = FD(evsel, cpu, thread);
776 if (perf_mmap__mmap(&maps[idx], mp, *output, evlist_cpu) < 0)
779 if (ioctl(fd, PERF_EVENT_IOC_SET_OUTPUT, *output) != 0)
782 perf_mmap__get(&maps[idx]);
785 revent = perf_evlist__should_poll(evlist, evsel) ? POLLIN : 0;
788 * The system_wide flag causes a selected event to be opened
789 * always without a pid. Consequently it will never get a
790 * POLLHUP, but it is used for tracking in combination with
791 * other events, so it should not need to be polled anyway.
792 * Therefore don't add it for polling.
794 if (!evsel->system_wide &&
795 __perf_evlist__add_pollfd(evlist, fd, &maps[idx], revent) < 0) {
796 perf_mmap__put(&maps[idx]);
800 if (evsel->core.attr.read_format & PERF_FORMAT_ID) {
801 if (perf_evlist__id_add_fd(evlist, evsel, cpu, thread,
804 perf_evlist__set_sid_idx(evlist, evsel, idx, cpu,
812 static int perf_evlist__mmap_per_cpu(struct evlist *evlist,
813 struct mmap_params *mp)
816 int nr_cpus = perf_cpu_map__nr(evlist->core.cpus);
817 int nr_threads = perf_thread_map__nr(evlist->core.threads);
819 pr_debug2("perf event ring buffer mmapped per cpu\n");
820 for (cpu = 0; cpu < nr_cpus; cpu++) {
822 int output_overwrite = -1;
824 auxtrace_mmap_params__set_idx(&mp->auxtrace_mp, evlist, cpu,
827 for (thread = 0; thread < nr_threads; thread++) {
828 if (perf_evlist__mmap_per_evsel(evlist, cpu, mp, cpu,
829 thread, &output, &output_overwrite))
837 perf_evlist__munmap_nofree(evlist);
841 static int perf_evlist__mmap_per_thread(struct evlist *evlist,
842 struct mmap_params *mp)
845 int nr_threads = perf_thread_map__nr(evlist->core.threads);
847 pr_debug2("perf event ring buffer mmapped per thread\n");
848 for (thread = 0; thread < nr_threads; thread++) {
850 int output_overwrite = -1;
852 auxtrace_mmap_params__set_idx(&mp->auxtrace_mp, evlist, thread,
855 if (perf_evlist__mmap_per_evsel(evlist, thread, mp, 0, thread,
856 &output, &output_overwrite))
863 perf_evlist__munmap_nofree(evlist);
867 unsigned long perf_event_mlock_kb_in_pages(void)
872 if (sysctl__read_int("kernel/perf_event_mlock_kb", &max) < 0) {
874 * Pick a once upon a time good value, i.e. things look
875 * strange since we can't read a sysctl value, but lets not
880 max -= (page_size / 1024);
883 pages = (max * 1024) / page_size;
884 if (!is_power_of_2(pages))
885 pages = rounddown_pow_of_two(pages);
890 size_t perf_evlist__mmap_size(unsigned long pages)
892 if (pages == UINT_MAX)
893 pages = perf_event_mlock_kb_in_pages();
894 else if (!is_power_of_2(pages))
897 return (pages + 1) * page_size;
900 static long parse_pages_arg(const char *str, unsigned long min,
903 unsigned long pages, val;
904 static struct parse_tag tags[] = {
905 { .tag = 'B', .mult = 1 },
906 { .tag = 'K', .mult = 1 << 10 },
907 { .tag = 'M', .mult = 1 << 20 },
908 { .tag = 'G', .mult = 1 << 30 },
915 val = parse_tag_value(str, tags);
916 if (val != (unsigned long) -1) {
917 /* we got file size value */
918 pages = PERF_ALIGN(val, page_size) / page_size;
920 /* we got pages count value */
922 pages = strtoul(str, &eptr, 10);
927 if (pages == 0 && min == 0) {
928 /* leave number of pages at 0 */
929 } else if (!is_power_of_2(pages)) {
932 /* round pages up to next power of 2 */
933 pages = roundup_pow_of_two(pages);
937 unit_number__scnprintf(buf, sizeof(buf), pages * page_size);
938 pr_info("rounding mmap pages size to %s (%lu pages)\n",
948 int __perf_evlist__parse_mmap_pages(unsigned int *mmap_pages, const char *str)
950 unsigned long max = UINT_MAX;
953 if (max > SIZE_MAX / page_size)
954 max = SIZE_MAX / page_size;
956 pages = parse_pages_arg(str, 1, max);
958 pr_err("Invalid argument for --mmap_pages/-m\n");
966 int perf_evlist__parse_mmap_pages(const struct option *opt, const char *str,
967 int unset __maybe_unused)
969 return __perf_evlist__parse_mmap_pages(opt->value, str);
973 * perf_evlist__mmap_ex - Create mmaps to receive events.
974 * @evlist: list of events
975 * @pages: map length in pages
976 * @overwrite: overwrite older events?
977 * @auxtrace_pages - auxtrace map length in pages
978 * @auxtrace_overwrite - overwrite older auxtrace data?
980 * If @overwrite is %false the user needs to signal event consumption using
981 * perf_mmap__write_tail(). Using perf_evlist__mmap_read() does this
984 * Similarly, if @auxtrace_overwrite is %false the user needs to signal data
985 * consumption using auxtrace_mmap__write_tail().
987 * Return: %0 on success, negative error code otherwise.
989 int perf_evlist__mmap_ex(struct evlist *evlist, unsigned int pages,
990 unsigned int auxtrace_pages,
991 bool auxtrace_overwrite, int nr_cblocks, int affinity, int flush,
995 const struct perf_cpu_map *cpus = evlist->core.cpus;
996 const struct perf_thread_map *threads = evlist->core.threads;
998 * Delay setting mp.prot: set it before calling perf_mmap__mmap.
999 * Its value is decided by evsel's write_backward.
1000 * So &mp should not be passed through const pointer.
1002 struct mmap_params mp = { .nr_cblocks = nr_cblocks, .affinity = affinity, .flush = flush,
1003 .comp_level = comp_level };
1006 evlist->mmap = perf_evlist__alloc_mmap(evlist, false);
1010 if (evlist->pollfd.entries == NULL && perf_evlist__alloc_pollfd(evlist) < 0)
1013 evlist->mmap_len = perf_evlist__mmap_size(pages);
1014 pr_debug("mmap size %zuB\n", evlist->mmap_len);
1015 mp.mask = evlist->mmap_len - page_size - 1;
1017 auxtrace_mmap_params__init(&mp.auxtrace_mp, evlist->mmap_len,
1018 auxtrace_pages, auxtrace_overwrite);
1020 evlist__for_each_entry(evlist, evsel) {
1021 if ((evsel->core.attr.read_format & PERF_FORMAT_ID) &&
1022 evsel->sample_id == NULL &&
1023 perf_evsel__alloc_id(evsel, perf_cpu_map__nr(cpus), threads->nr) < 0)
1027 if (perf_cpu_map__empty(cpus))
1028 return perf_evlist__mmap_per_thread(evlist, &mp);
1030 return perf_evlist__mmap_per_cpu(evlist, &mp);
1033 int perf_evlist__mmap(struct evlist *evlist, unsigned int pages)
1035 return perf_evlist__mmap_ex(evlist, pages, 0, false, 0, PERF_AFFINITY_SYS, 1, 0);
1038 int perf_evlist__create_maps(struct evlist *evlist, struct target *target)
1040 bool all_threads = (target->per_thread && target->system_wide);
1041 struct perf_cpu_map *cpus;
1042 struct perf_thread_map *threads;
1045 * If specify '-a' and '--per-thread' to perf record, perf record
1046 * will override '--per-thread'. target->per_thread = false and
1047 * target->system_wide = true.
1049 * If specify '--per-thread' only to perf record,
1050 * target->per_thread = true and target->system_wide = false.
1052 * So target->per_thread && target->system_wide is false.
1053 * For perf record, thread_map__new_str doesn't call
1054 * thread_map__new_all_cpus. That will keep perf record's
1057 * For perf stat, it allows the case that target->per_thread and
1058 * target->system_wide are all true. It means to collect system-wide
1059 * per-thread data. thread_map__new_str will call
1060 * thread_map__new_all_cpus to enumerate all threads.
1062 threads = thread_map__new_str(target->pid, target->tid, target->uid,
1068 if (target__uses_dummy_map(target))
1069 cpus = perf_cpu_map__dummy_new();
1071 cpus = perf_cpu_map__new(target->cpu_list);
1074 goto out_delete_threads;
1076 evlist->core.has_user_cpus = !!target->cpu_list;
1078 perf_evlist__set_maps(&evlist->core, cpus, threads);
1083 perf_thread_map__put(threads);
1087 void __perf_evlist__set_sample_bit(struct evlist *evlist,
1088 enum perf_event_sample_format bit)
1090 struct evsel *evsel;
1092 evlist__for_each_entry(evlist, evsel)
1093 __perf_evsel__set_sample_bit(evsel, bit);
1096 void __perf_evlist__reset_sample_bit(struct evlist *evlist,
1097 enum perf_event_sample_format bit)
1099 struct evsel *evsel;
1101 evlist__for_each_entry(evlist, evsel)
1102 __perf_evsel__reset_sample_bit(evsel, bit);
1105 int perf_evlist__apply_filters(struct evlist *evlist, struct evsel **err_evsel)
1107 struct evsel *evsel;
1110 evlist__for_each_entry(evlist, evsel) {
1111 if (evsel->filter == NULL)
1115 * filters only work for tracepoint event, which doesn't have cpu limit.
1116 * So evlist and evsel should always be same.
1118 err = perf_evsel__apply_filter(&evsel->core, evsel->filter);
1128 int perf_evlist__set_tp_filter(struct evlist *evlist, const char *filter)
1130 struct evsel *evsel;
1133 evlist__for_each_entry(evlist, evsel) {
1134 if (evsel->core.attr.type != PERF_TYPE_TRACEPOINT)
1137 err = perf_evsel__set_filter(evsel, filter);
1145 int perf_evlist__set_tp_filter_pids(struct evlist *evlist, size_t npids, pid_t *pids)
1151 for (i = 0; i < npids; ++i) {
1153 if (asprintf(&filter, "common_pid != %d", pids[i]) < 0)
1158 if (asprintf(&tmp, "%s && common_pid != %d", filter, pids[i]) < 0)
1166 ret = perf_evlist__set_tp_filter(evlist, filter);
1172 int perf_evlist__set_tp_filter_pid(struct evlist *evlist, pid_t pid)
1174 return perf_evlist__set_tp_filter_pids(evlist, 1, &pid);
1177 bool perf_evlist__valid_sample_type(struct evlist *evlist)
1181 if (evlist->core.nr_entries == 1)
1184 if (evlist->id_pos < 0 || evlist->is_pos < 0)
1187 evlist__for_each_entry(evlist, pos) {
1188 if (pos->id_pos != evlist->id_pos ||
1189 pos->is_pos != evlist->is_pos)
1196 u64 __perf_evlist__combined_sample_type(struct evlist *evlist)
1198 struct evsel *evsel;
1200 if (evlist->combined_sample_type)
1201 return evlist->combined_sample_type;
1203 evlist__for_each_entry(evlist, evsel)
1204 evlist->combined_sample_type |= evsel->core.attr.sample_type;
1206 return evlist->combined_sample_type;
1209 u64 perf_evlist__combined_sample_type(struct evlist *evlist)
1211 evlist->combined_sample_type = 0;
1212 return __perf_evlist__combined_sample_type(evlist);
1215 u64 perf_evlist__combined_branch_type(struct evlist *evlist)
1217 struct evsel *evsel;
1218 u64 branch_type = 0;
1220 evlist__for_each_entry(evlist, evsel)
1221 branch_type |= evsel->core.attr.branch_sample_type;
1225 bool perf_evlist__valid_read_format(struct evlist *evlist)
1227 struct evsel *first = perf_evlist__first(evlist), *pos = first;
1228 u64 read_format = first->core.attr.read_format;
1229 u64 sample_type = first->core.attr.sample_type;
1231 evlist__for_each_entry(evlist, pos) {
1232 if (read_format != pos->core.attr.read_format)
1236 /* PERF_SAMPLE_READ imples PERF_FORMAT_ID. */
1237 if ((sample_type & PERF_SAMPLE_READ) &&
1238 !(read_format & PERF_FORMAT_ID)) {
1245 u64 perf_evlist__read_format(struct evlist *evlist)
1247 struct evsel *first = perf_evlist__first(evlist);
1248 return first->core.attr.read_format;
1251 u16 perf_evlist__id_hdr_size(struct evlist *evlist)
1253 struct evsel *first = perf_evlist__first(evlist);
1254 struct perf_sample *data;
1258 if (!first->core.attr.sample_id_all)
1261 sample_type = first->core.attr.sample_type;
1263 if (sample_type & PERF_SAMPLE_TID)
1264 size += sizeof(data->tid) * 2;
1266 if (sample_type & PERF_SAMPLE_TIME)
1267 size += sizeof(data->time);
1269 if (sample_type & PERF_SAMPLE_ID)
1270 size += sizeof(data->id);
1272 if (sample_type & PERF_SAMPLE_STREAM_ID)
1273 size += sizeof(data->stream_id);
1275 if (sample_type & PERF_SAMPLE_CPU)
1276 size += sizeof(data->cpu) * 2;
1278 if (sample_type & PERF_SAMPLE_IDENTIFIER)
1279 size += sizeof(data->id);
1284 bool perf_evlist__valid_sample_id_all(struct evlist *evlist)
1286 struct evsel *first = perf_evlist__first(evlist), *pos = first;
1288 evlist__for_each_entry_continue(evlist, pos) {
1289 if (first->core.attr.sample_id_all != pos->core.attr.sample_id_all)
1296 bool perf_evlist__sample_id_all(struct evlist *evlist)
1298 struct evsel *first = perf_evlist__first(evlist);
1299 return first->core.attr.sample_id_all;
1302 void perf_evlist__set_selected(struct evlist *evlist,
1303 struct evsel *evsel)
1305 evlist->selected = evsel;
1308 void evlist__close(struct evlist *evlist)
1310 struct evsel *evsel;
1312 evlist__for_each_entry_reverse(evlist, evsel)
1313 evsel__close(evsel);
1316 static int perf_evlist__create_syswide_maps(struct evlist *evlist)
1318 struct perf_cpu_map *cpus;
1319 struct perf_thread_map *threads;
1323 * Try reading /sys/devices/system/cpu/online to get
1326 * FIXME: -ENOMEM is the best we can do here, the cpu_map
1327 * code needs an overhaul to properly forward the
1328 * error, and we may not want to do that fallback to a
1329 * default cpu identity map :-\
1331 cpus = perf_cpu_map__new(NULL);
1335 threads = perf_thread_map__new_dummy();
1339 perf_evlist__set_maps(&evlist->core, cpus, threads);
1343 perf_cpu_map__put(cpus);
1347 int evlist__open(struct evlist *evlist)
1349 struct evsel *evsel;
1353 * Default: one fd per CPU, all threads, aka systemwide
1354 * as sys_perf_event_open(cpu = -1, thread = -1) is EINVAL
1356 if (evlist->core.threads == NULL && evlist->core.cpus == NULL) {
1357 err = perf_evlist__create_syswide_maps(evlist);
1362 perf_evlist__update_id_pos(evlist);
1364 evlist__for_each_entry(evlist, evsel) {
1365 err = evsel__open(evsel, evsel->core.cpus, evsel->core.threads);
1372 evlist__close(evlist);
1377 int perf_evlist__prepare_workload(struct evlist *evlist, struct target *target,
1378 const char *argv[], bool pipe_output,
1379 void (*exec_error)(int signo, siginfo_t *info, void *ucontext))
1381 int child_ready_pipe[2], go_pipe[2];
1384 if (pipe(child_ready_pipe) < 0) {
1385 perror("failed to create 'ready' pipe");
1389 if (pipe(go_pipe) < 0) {
1390 perror("failed to create 'go' pipe");
1391 goto out_close_ready_pipe;
1394 evlist->workload.pid = fork();
1395 if (evlist->workload.pid < 0) {
1396 perror("failed to fork");
1397 goto out_close_pipes;
1400 if (!evlist->workload.pid) {
1406 signal(SIGTERM, SIG_DFL);
1408 close(child_ready_pipe[0]);
1410 fcntl(go_pipe[0], F_SETFD, FD_CLOEXEC);
1413 * Tell the parent we're ready to go
1415 close(child_ready_pipe[1]);
1418 * Wait until the parent tells us to go.
1420 ret = read(go_pipe[0], &bf, 1);
1422 * The parent will ask for the execvp() to be performed by
1423 * writing exactly one byte, in workload.cork_fd, usually via
1424 * perf_evlist__start_workload().
1426 * For cancelling the workload without actually running it,
1427 * the parent will just close workload.cork_fd, without writing
1428 * anything, i.e. read will return zero and we just exit()
1433 perror("unable to read pipe");
1437 execvp(argv[0], (char **)argv);
1442 val.sival_int = errno;
1443 if (sigqueue(getppid(), SIGUSR1, val))
1451 struct sigaction act = {
1452 .sa_flags = SA_SIGINFO,
1453 .sa_sigaction = exec_error,
1455 sigaction(SIGUSR1, &act, NULL);
1458 if (target__none(target)) {
1459 if (evlist->core.threads == NULL) {
1460 fprintf(stderr, "FATAL: evlist->threads need to be set at this point (%s:%d).\n",
1461 __func__, __LINE__);
1462 goto out_close_pipes;
1464 perf_thread_map__set_pid(evlist->core.threads, 0, evlist->workload.pid);
1467 close(child_ready_pipe[1]);
1470 * wait for child to settle
1472 if (read(child_ready_pipe[0], &bf, 1) == -1) {
1473 perror("unable to read pipe");
1474 goto out_close_pipes;
1477 fcntl(go_pipe[1], F_SETFD, FD_CLOEXEC);
1478 evlist->workload.cork_fd = go_pipe[1];
1479 close(child_ready_pipe[0]);
1485 out_close_ready_pipe:
1486 close(child_ready_pipe[0]);
1487 close(child_ready_pipe[1]);
1491 int perf_evlist__start_workload(struct evlist *evlist)
1493 if (evlist->workload.cork_fd > 0) {
1497 * Remove the cork, let it rip!
1499 ret = write(evlist->workload.cork_fd, &bf, 1);
1501 perror("unable to write to pipe");
1503 close(evlist->workload.cork_fd);
1510 int perf_evlist__parse_sample(struct evlist *evlist, union perf_event *event,
1511 struct perf_sample *sample)
1513 struct evsel *evsel = perf_evlist__event2evsel(evlist, event);
1517 return perf_evsel__parse_sample(evsel, event, sample);
1520 int perf_evlist__parse_sample_timestamp(struct evlist *evlist,
1521 union perf_event *event,
1524 struct evsel *evsel = perf_evlist__event2evsel(evlist, event);
1528 return perf_evsel__parse_sample_timestamp(evsel, event, timestamp);
1531 size_t perf_evlist__fprintf(struct evlist *evlist, FILE *fp)
1533 struct evsel *evsel;
1536 evlist__for_each_entry(evlist, evsel) {
1537 printed += fprintf(fp, "%s%s", evsel->idx ? ", " : "",
1538 perf_evsel__name(evsel));
1541 return printed + fprintf(fp, "\n");
1544 int perf_evlist__strerror_open(struct evlist *evlist,
1545 int err, char *buf, size_t size)
1548 char sbuf[STRERR_BUFSIZE], *emsg = str_error_r(err, sbuf, sizeof(sbuf));
1553 printed = scnprintf(buf, size,
1555 "Hint:\tCheck /proc/sys/kernel/perf_event_paranoid setting.", emsg);
1557 value = perf_event_paranoid();
1559 printed += scnprintf(buf + printed, size - printed, "\nHint:\t");
1562 printed += scnprintf(buf + printed, size - printed,
1563 "For your workloads it needs to be <= 1\nHint:\t");
1565 printed += scnprintf(buf + printed, size - printed,
1566 "For system wide tracing it needs to be set to -1.\n");
1568 printed += scnprintf(buf + printed, size - printed,
1569 "Hint:\tTry: 'sudo sh -c \"echo -1 > /proc/sys/kernel/perf_event_paranoid\"'\n"
1570 "Hint:\tThe current value is %d.", value);
1573 struct evsel *first = perf_evlist__first(evlist);
1576 if (sysctl__read_int("kernel/perf_event_max_sample_rate", &max_freq) < 0)
1579 if (first->core.attr.sample_freq < (u64)max_freq)
1582 printed = scnprintf(buf, size,
1584 "Hint:\tCheck /proc/sys/kernel/perf_event_max_sample_rate.\n"
1585 "Hint:\tThe current value is %d and %" PRIu64 " is being requested.",
1586 emsg, max_freq, first->core.attr.sample_freq);
1591 scnprintf(buf, size, "%s", emsg);
1598 int perf_evlist__strerror_mmap(struct evlist *evlist, int err, char *buf, size_t size)
1600 char sbuf[STRERR_BUFSIZE], *emsg = str_error_r(err, sbuf, sizeof(sbuf));
1601 int pages_attempted = evlist->mmap_len / 1024, pages_max_per_user, printed = 0;
1605 sysctl__read_int("kernel/perf_event_mlock_kb", &pages_max_per_user);
1606 printed += scnprintf(buf + printed, size - printed,
1608 "Hint:\tCheck /proc/sys/kernel/perf_event_mlock_kb (%d kB) setting.\n"
1609 "Hint:\tTried using %zd kB.\n",
1610 emsg, pages_max_per_user, pages_attempted);
1612 if (pages_attempted >= pages_max_per_user) {
1613 printed += scnprintf(buf + printed, size - printed,
1614 "Hint:\tTry 'sudo sh -c \"echo %d > /proc/sys/kernel/perf_event_mlock_kb\"', or\n",
1615 pages_max_per_user + pages_attempted);
1618 printed += scnprintf(buf + printed, size - printed,
1619 "Hint:\tTry using a smaller -m/--mmap-pages value.");
1622 scnprintf(buf, size, "%s", emsg);
1629 void perf_evlist__to_front(struct evlist *evlist,
1630 struct evsel *move_evsel)
1632 struct evsel *evsel, *n;
1635 if (move_evsel == perf_evlist__first(evlist))
1638 evlist__for_each_entry_safe(evlist, n, evsel) {
1639 if (evsel->leader == move_evsel->leader)
1640 list_move_tail(&evsel->core.node, &move);
1643 list_splice(&move, &evlist->core.entries);
1646 void perf_evlist__set_tracking_event(struct evlist *evlist,
1647 struct evsel *tracking_evsel)
1649 struct evsel *evsel;
1651 if (tracking_evsel->tracking)
1654 evlist__for_each_entry(evlist, evsel) {
1655 if (evsel != tracking_evsel)
1656 evsel->tracking = false;
1659 tracking_evsel->tracking = true;
1663 perf_evlist__find_evsel_by_str(struct evlist *evlist,
1666 struct evsel *evsel;
1668 evlist__for_each_entry(evlist, evsel) {
1671 if (strcmp(str, evsel->name) == 0)
1678 void perf_evlist__toggle_bkw_mmap(struct evlist *evlist,
1679 enum bkw_mmap_state state)
1681 enum bkw_mmap_state old_state = evlist->bkw_mmap_state;
1688 if (!evlist->overwrite_mmap)
1691 switch (old_state) {
1692 case BKW_MMAP_NOTREADY: {
1693 if (state != BKW_MMAP_RUNNING)
1697 case BKW_MMAP_RUNNING: {
1698 if (state != BKW_MMAP_DATA_PENDING)
1703 case BKW_MMAP_DATA_PENDING: {
1704 if (state != BKW_MMAP_EMPTY)
1708 case BKW_MMAP_EMPTY: {
1709 if (state != BKW_MMAP_RUNNING)
1715 WARN_ONCE(1, "Shouldn't get there\n");
1718 evlist->bkw_mmap_state = state;
1722 perf_evlist__pause(evlist);
1725 perf_evlist__resume(evlist);
1736 bool perf_evlist__exclude_kernel(struct evlist *evlist)
1738 struct evsel *evsel;
1740 evlist__for_each_entry(evlist, evsel) {
1741 if (!evsel->core.attr.exclude_kernel)
1749 * Events in data file are not collect in groups, but we still want
1750 * the group display. Set the artificial group and set the leader's
1751 * forced_leader flag to notify the display code.
1753 void perf_evlist__force_leader(struct evlist *evlist)
1755 if (!evlist->nr_groups) {
1756 struct evsel *leader = perf_evlist__first(evlist);
1758 perf_evlist__set_leader(evlist);
1759 leader->forced_leader = true;
1763 struct evsel *perf_evlist__reset_weak_group(struct evlist *evsel_list,
1764 struct evsel *evsel)
1766 struct evsel *c2, *leader;
1767 bool is_open = true;
1769 leader = evsel->leader;
1770 pr_debug("Weak group for %s/%d failed\n",
1771 leader->name, leader->core.nr_members);
1774 * for_each_group_member doesn't work here because it doesn't
1775 * include the first entry.
1777 evlist__for_each_entry(evsel_list, c2) {
1780 if (c2->leader == leader) {
1784 c2->core.nr_members = 0;
1790 int perf_evlist__add_sb_event(struct evlist **evlist,
1791 struct perf_event_attr *attr,
1792 perf_evsel__sb_cb_t cb,
1795 struct evsel *evsel;
1796 bool new_evlist = (*evlist) == NULL;
1798 if (*evlist == NULL)
1799 *evlist = evlist__new();
1800 if (*evlist == NULL)
1803 if (!attr->sample_id_all) {
1804 pr_warning("enabling sample_id_all for all side band events\n");
1805 attr->sample_id_all = 1;
1808 evsel = perf_evsel__new_idx(attr, (*evlist)->core.nr_entries);
1812 evsel->side_band.cb = cb;
1813 evsel->side_band.data = data;
1814 evlist__add(*evlist, evsel);
1819 evlist__delete(*evlist);
1825 static void *perf_evlist__poll_thread(void *arg)
1827 struct evlist *evlist = arg;
1828 bool draining = false;
1831 * In order to read symbols from other namespaces perf to needs to call
1832 * setns(2). This isn't permitted if the struct_fs has multiple users.
1833 * unshare(2) the fs so that we may continue to setns into namespaces
1834 * that we're observing when, for instance, reading the build-ids at
1835 * the end of a 'perf record' session.
1840 bool got_data = false;
1842 if (evlist->thread.done)
1846 perf_evlist__poll(evlist, 1000);
1848 for (i = 0; i < evlist->nr_mmaps; i++) {
1849 struct perf_mmap *map = &evlist->mmap[i];
1850 union perf_event *event;
1852 if (perf_mmap__read_init(map))
1854 while ((event = perf_mmap__read_event(map)) != NULL) {
1855 struct evsel *evsel = perf_evlist__event2evsel(evlist, event);
1857 if (evsel && evsel->side_band.cb)
1858 evsel->side_band.cb(event, evsel->side_band.data);
1860 pr_warning("cannot locate proper evsel for the side band event\n");
1862 perf_mmap__consume(map);
1865 perf_mmap__read_done(map);
1868 if (draining && !got_data)
1874 int perf_evlist__start_sb_thread(struct evlist *evlist,
1875 struct target *target)
1877 struct evsel *counter;
1882 if (perf_evlist__create_maps(evlist, target))
1883 goto out_delete_evlist;
1885 evlist__for_each_entry(evlist, counter) {
1886 if (evsel__open(counter, evlist->core.cpus,
1887 evlist->core.threads) < 0)
1888 goto out_delete_evlist;
1891 if (perf_evlist__mmap(evlist, UINT_MAX))
1892 goto out_delete_evlist;
1894 evlist__for_each_entry(evlist, counter) {
1895 if (evsel__enable(counter))
1896 goto out_delete_evlist;
1899 evlist->thread.done = 0;
1900 if (pthread_create(&evlist->thread.th, NULL, perf_evlist__poll_thread, evlist))
1901 goto out_delete_evlist;
1906 evlist__delete(evlist);
1911 void perf_evlist__stop_sb_thread(struct evlist *evlist)
1915 evlist->thread.done = 1;
1916 pthread_join(evlist->thread.th, NULL);
1917 evlist__delete(evlist);