2 * Copyright (C) 2011, Red Hat Inc, Arnaldo Carvalho de Melo <acme@redhat.com>
4 * Parts came from builtin-{top,stat,record}.c, see those files for further
7 * Released under the GPL v2. (and only v2, not any later version)
10 #include <api/fs/fs.h>
13 #include "thread_map.h"
20 #include "parse-events.h"
21 #include "parse-options.h"
25 #include <linux/bitops.h>
26 #include <linux/hash.h>
27 #include <linux/log2.h>
29 static void perf_evlist__mmap_put(struct perf_evlist *evlist, int idx);
30 static void __perf_evlist__munmap(struct perf_evlist *evlist, int idx);
32 #define FD(e, x, y) (*(int *)xyarray__entry(e->fd, x, y))
33 #define SID(e, x, y) xyarray__entry(e->sample_id, x, y)
35 void perf_evlist__init(struct perf_evlist *evlist, struct cpu_map *cpus,
36 struct thread_map *threads)
40 for (i = 0; i < PERF_EVLIST__HLIST_SIZE; ++i)
41 INIT_HLIST_HEAD(&evlist->heads[i]);
42 INIT_LIST_HEAD(&evlist->entries);
43 perf_evlist__set_maps(evlist, cpus, threads);
44 fdarray__init(&evlist->pollfd, 64);
45 evlist->workload.pid = -1;
48 struct perf_evlist *perf_evlist__new(void)
50 struct perf_evlist *evlist = zalloc(sizeof(*evlist));
53 perf_evlist__init(evlist, NULL, NULL);
58 struct perf_evlist *perf_evlist__new_default(void)
60 struct perf_evlist *evlist = perf_evlist__new();
62 if (evlist && perf_evlist__add_default(evlist)) {
63 perf_evlist__delete(evlist);
71 * perf_evlist__set_id_pos - set the positions of event ids.
72 * @evlist: selected event list
74 * Events with compatible sample types all have the same id_pos
75 * and is_pos. For convenience, put a copy on evlist.
77 void perf_evlist__set_id_pos(struct perf_evlist *evlist)
79 struct perf_evsel *first = perf_evlist__first(evlist);
81 evlist->id_pos = first->id_pos;
82 evlist->is_pos = first->is_pos;
85 static void perf_evlist__update_id_pos(struct perf_evlist *evlist)
87 struct perf_evsel *evsel;
89 evlist__for_each(evlist, evsel)
90 perf_evsel__calc_id_pos(evsel);
92 perf_evlist__set_id_pos(evlist);
95 static void perf_evlist__purge(struct perf_evlist *evlist)
97 struct perf_evsel *pos, *n;
99 evlist__for_each_safe(evlist, n, pos) {
100 list_del_init(&pos->node);
101 perf_evsel__delete(pos);
104 evlist->nr_entries = 0;
107 void perf_evlist__exit(struct perf_evlist *evlist)
109 zfree(&evlist->mmap);
110 fdarray__exit(&evlist->pollfd);
113 void perf_evlist__delete(struct perf_evlist *evlist)
115 perf_evlist__munmap(evlist);
116 perf_evlist__close(evlist);
117 cpu_map__put(evlist->cpus);
118 thread_map__put(evlist->threads);
120 evlist->threads = NULL;
121 perf_evlist__purge(evlist);
122 perf_evlist__exit(evlist);
126 void perf_evlist__add(struct perf_evlist *evlist, struct perf_evsel *entry)
128 list_add_tail(&entry->node, &evlist->entries);
129 entry->idx = evlist->nr_entries;
130 entry->tracking = !entry->idx;
132 if (!evlist->nr_entries++)
133 perf_evlist__set_id_pos(evlist);
136 void perf_evlist__splice_list_tail(struct perf_evlist *evlist,
137 struct list_head *list,
140 bool set_id_pos = !evlist->nr_entries;
142 list_splice_tail(list, &evlist->entries);
143 evlist->nr_entries += nr_entries;
145 perf_evlist__set_id_pos(evlist);
148 void __perf_evlist__set_leader(struct list_head *list)
150 struct perf_evsel *evsel, *leader;
152 leader = list_entry(list->next, struct perf_evsel, node);
153 evsel = list_entry(list->prev, struct perf_evsel, node);
155 leader->nr_members = evsel->idx - leader->idx + 1;
157 __evlist__for_each(list, evsel) {
158 evsel->leader = leader;
162 void perf_evlist__set_leader(struct perf_evlist *evlist)
164 if (evlist->nr_entries) {
165 evlist->nr_groups = evlist->nr_entries > 1 ? 1 : 0;
166 __perf_evlist__set_leader(&evlist->entries);
170 int perf_evlist__add_default(struct perf_evlist *evlist)
172 struct perf_event_attr attr = {
173 .type = PERF_TYPE_HARDWARE,
174 .config = PERF_COUNT_HW_CPU_CYCLES,
176 struct perf_evsel *evsel;
178 event_attr_init(&attr);
180 evsel = perf_evsel__new(&attr);
184 /* use strdup() because free(evsel) assumes name is allocated */
185 evsel->name = strdup("cycles");
189 perf_evlist__add(evlist, evsel);
192 perf_evsel__delete(evsel);
197 static int perf_evlist__add_attrs(struct perf_evlist *evlist,
198 struct perf_event_attr *attrs, size_t nr_attrs)
200 struct perf_evsel *evsel, *n;
204 for (i = 0; i < nr_attrs; i++) {
205 evsel = perf_evsel__new_idx(attrs + i, evlist->nr_entries + i);
207 goto out_delete_partial_list;
208 list_add_tail(&evsel->node, &head);
211 perf_evlist__splice_list_tail(evlist, &head, nr_attrs);
215 out_delete_partial_list:
216 __evlist__for_each_safe(&head, n, evsel)
217 perf_evsel__delete(evsel);
221 int __perf_evlist__add_default_attrs(struct perf_evlist *evlist,
222 struct perf_event_attr *attrs, size_t nr_attrs)
226 for (i = 0; i < nr_attrs; i++)
227 event_attr_init(attrs + i);
229 return perf_evlist__add_attrs(evlist, attrs, nr_attrs);
233 perf_evlist__find_tracepoint_by_id(struct perf_evlist *evlist, int id)
235 struct perf_evsel *evsel;
237 evlist__for_each(evlist, evsel) {
238 if (evsel->attr.type == PERF_TYPE_TRACEPOINT &&
239 (int)evsel->attr.config == id)
247 perf_evlist__find_tracepoint_by_name(struct perf_evlist *evlist,
250 struct perf_evsel *evsel;
252 evlist__for_each(evlist, evsel) {
253 if ((evsel->attr.type == PERF_TYPE_TRACEPOINT) &&
254 (strcmp(evsel->name, name) == 0))
261 int perf_evlist__add_newtp(struct perf_evlist *evlist,
262 const char *sys, const char *name, void *handler)
264 struct perf_evsel *evsel = perf_evsel__newtp(sys, name);
269 evsel->handler = handler;
270 perf_evlist__add(evlist, evsel);
274 static int perf_evlist__nr_threads(struct perf_evlist *evlist,
275 struct perf_evsel *evsel)
277 if (evsel->system_wide)
280 return thread_map__nr(evlist->threads);
283 void perf_evlist__disable(struct perf_evlist *evlist)
286 struct perf_evsel *pos;
287 int nr_cpus = cpu_map__nr(evlist->cpus);
290 for (cpu = 0; cpu < nr_cpus; cpu++) {
291 evlist__for_each(evlist, pos) {
292 if (!perf_evsel__is_group_leader(pos) || !pos->fd)
294 nr_threads = perf_evlist__nr_threads(evlist, pos);
295 for (thread = 0; thread < nr_threads; thread++)
296 ioctl(FD(pos, cpu, thread),
297 PERF_EVENT_IOC_DISABLE, 0);
301 evlist->enabled = false;
304 void perf_evlist__enable(struct perf_evlist *evlist)
307 struct perf_evsel *pos;
308 int nr_cpus = cpu_map__nr(evlist->cpus);
311 for (cpu = 0; cpu < nr_cpus; cpu++) {
312 evlist__for_each(evlist, pos) {
313 if (!perf_evsel__is_group_leader(pos) || !pos->fd)
315 nr_threads = perf_evlist__nr_threads(evlist, pos);
316 for (thread = 0; thread < nr_threads; thread++)
317 ioctl(FD(pos, cpu, thread),
318 PERF_EVENT_IOC_ENABLE, 0);
322 evlist->enabled = true;
325 void perf_evlist__toggle_enable(struct perf_evlist *evlist)
327 (evlist->enabled ? perf_evlist__disable : perf_evlist__enable)(evlist);
330 int perf_evlist__disable_event(struct perf_evlist *evlist,
331 struct perf_evsel *evsel)
333 int cpu, thread, err;
334 int nr_cpus = cpu_map__nr(evlist->cpus);
335 int nr_threads = perf_evlist__nr_threads(evlist, evsel);
340 for (cpu = 0; cpu < nr_cpus; cpu++) {
341 for (thread = 0; thread < nr_threads; thread++) {
342 err = ioctl(FD(evsel, cpu, thread),
343 PERF_EVENT_IOC_DISABLE, 0);
351 int perf_evlist__enable_event(struct perf_evlist *evlist,
352 struct perf_evsel *evsel)
354 int cpu, thread, err;
355 int nr_cpus = cpu_map__nr(evlist->cpus);
356 int nr_threads = perf_evlist__nr_threads(evlist, evsel);
361 for (cpu = 0; cpu < nr_cpus; cpu++) {
362 for (thread = 0; thread < nr_threads; thread++) {
363 err = ioctl(FD(evsel, cpu, thread),
364 PERF_EVENT_IOC_ENABLE, 0);
372 static int perf_evlist__enable_event_cpu(struct perf_evlist *evlist,
373 struct perf_evsel *evsel, int cpu)
376 int nr_threads = perf_evlist__nr_threads(evlist, evsel);
381 for (thread = 0; thread < nr_threads; thread++) {
382 err = ioctl(FD(evsel, cpu, thread),
383 PERF_EVENT_IOC_ENABLE, 0);
390 static int perf_evlist__enable_event_thread(struct perf_evlist *evlist,
391 struct perf_evsel *evsel,
395 int nr_cpus = cpu_map__nr(evlist->cpus);
400 for (cpu = 0; cpu < nr_cpus; cpu++) {
401 err = ioctl(FD(evsel, cpu, thread), PERF_EVENT_IOC_ENABLE, 0);
408 int perf_evlist__enable_event_idx(struct perf_evlist *evlist,
409 struct perf_evsel *evsel, int idx)
411 bool per_cpu_mmaps = !cpu_map__empty(evlist->cpus);
414 return perf_evlist__enable_event_cpu(evlist, evsel, idx);
416 return perf_evlist__enable_event_thread(evlist, evsel, idx);
419 int perf_evlist__alloc_pollfd(struct perf_evlist *evlist)
421 int nr_cpus = cpu_map__nr(evlist->cpus);
422 int nr_threads = thread_map__nr(evlist->threads);
424 struct perf_evsel *evsel;
426 evlist__for_each(evlist, evsel) {
427 if (evsel->system_wide)
430 nfds += nr_cpus * nr_threads;
433 if (fdarray__available_entries(&evlist->pollfd) < nfds &&
434 fdarray__grow(&evlist->pollfd, nfds) < 0)
440 static int __perf_evlist__add_pollfd(struct perf_evlist *evlist, int fd, int idx)
442 int pos = fdarray__add(&evlist->pollfd, fd, POLLIN | POLLERR | POLLHUP);
444 * Save the idx so that when we filter out fds POLLHUP'ed we can
445 * close the associated evlist->mmap[] entry.
448 evlist->pollfd.priv[pos].idx = idx;
450 fcntl(fd, F_SETFL, O_NONBLOCK);
456 int perf_evlist__add_pollfd(struct perf_evlist *evlist, int fd)
458 return __perf_evlist__add_pollfd(evlist, fd, -1);
461 static void perf_evlist__munmap_filtered(struct fdarray *fda, int fd)
463 struct perf_evlist *evlist = container_of(fda, struct perf_evlist, pollfd);
465 perf_evlist__mmap_put(evlist, fda->priv[fd].idx);
468 int perf_evlist__filter_pollfd(struct perf_evlist *evlist, short revents_and_mask)
470 return fdarray__filter(&evlist->pollfd, revents_and_mask,
471 perf_evlist__munmap_filtered);
474 int perf_evlist__poll(struct perf_evlist *evlist, int timeout)
476 return fdarray__poll(&evlist->pollfd, timeout);
479 static void perf_evlist__id_hash(struct perf_evlist *evlist,
480 struct perf_evsel *evsel,
481 int cpu, int thread, u64 id)
484 struct perf_sample_id *sid = SID(evsel, cpu, thread);
488 hash = hash_64(sid->id, PERF_EVLIST__HLIST_BITS);
489 hlist_add_head(&sid->node, &evlist->heads[hash]);
492 void perf_evlist__id_add(struct perf_evlist *evlist, struct perf_evsel *evsel,
493 int cpu, int thread, u64 id)
495 perf_evlist__id_hash(evlist, evsel, cpu, thread, id);
496 evsel->id[evsel->ids++] = id;
499 static int perf_evlist__id_add_fd(struct perf_evlist *evlist,
500 struct perf_evsel *evsel,
501 int cpu, int thread, int fd)
503 u64 read_data[4] = { 0, };
504 int id_idx = 1; /* The first entry is the counter value */
508 ret = ioctl(fd, PERF_EVENT_IOC_ID, &id);
515 /* Legacy way to get event id.. All hail to old kernels! */
518 * This way does not work with group format read, so bail
521 if (perf_evlist__read_format(evlist) & PERF_FORMAT_GROUP)
524 if (!(evsel->attr.read_format & PERF_FORMAT_ID) ||
525 read(fd, &read_data, sizeof(read_data)) == -1)
528 if (evsel->attr.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
530 if (evsel->attr.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
533 id = read_data[id_idx];
536 perf_evlist__id_add(evlist, evsel, cpu, thread, id);
540 static void perf_evlist__set_sid_idx(struct perf_evlist *evlist,
541 struct perf_evsel *evsel, int idx, int cpu,
544 struct perf_sample_id *sid = SID(evsel, cpu, thread);
546 if (evlist->cpus && cpu >= 0)
547 sid->cpu = evlist->cpus->map[cpu];
550 if (!evsel->system_wide && evlist->threads && thread >= 0)
551 sid->tid = thread_map__pid(evlist->threads, thread);
556 struct perf_sample_id *perf_evlist__id2sid(struct perf_evlist *evlist, u64 id)
558 struct hlist_head *head;
559 struct perf_sample_id *sid;
562 hash = hash_64(id, PERF_EVLIST__HLIST_BITS);
563 head = &evlist->heads[hash];
565 hlist_for_each_entry(sid, head, node)
572 struct perf_evsel *perf_evlist__id2evsel(struct perf_evlist *evlist, u64 id)
574 struct perf_sample_id *sid;
576 if (evlist->nr_entries == 1)
577 return perf_evlist__first(evlist);
579 sid = perf_evlist__id2sid(evlist, id);
583 if (!perf_evlist__sample_id_all(evlist))
584 return perf_evlist__first(evlist);
589 static int perf_evlist__event2id(struct perf_evlist *evlist,
590 union perf_event *event, u64 *id)
592 const u64 *array = event->sample.array;
595 n = (event->header.size - sizeof(event->header)) >> 3;
597 if (event->header.type == PERF_RECORD_SAMPLE) {
598 if (evlist->id_pos >= n)
600 *id = array[evlist->id_pos];
602 if (evlist->is_pos > n)
610 static struct perf_evsel *perf_evlist__event2evsel(struct perf_evlist *evlist,
611 union perf_event *event)
613 struct perf_evsel *first = perf_evlist__first(evlist);
614 struct hlist_head *head;
615 struct perf_sample_id *sid;
619 if (evlist->nr_entries == 1)
622 if (!first->attr.sample_id_all &&
623 event->header.type != PERF_RECORD_SAMPLE)
626 if (perf_evlist__event2id(evlist, event, &id))
629 /* Synthesized events have an id of zero */
633 hash = hash_64(id, PERF_EVLIST__HLIST_BITS);
634 head = &evlist->heads[hash];
636 hlist_for_each_entry(sid, head, node) {
643 union perf_event *perf_evlist__mmap_read(struct perf_evlist *evlist, int idx)
645 struct perf_mmap *md = &evlist->mmap[idx];
648 unsigned char *data = md->base + page_size;
649 union perf_event *event = NULL;
652 * Check if event was unmapped due to a POLLHUP/POLLERR.
654 if (!atomic_read(&md->refcnt))
657 head = perf_mmap__read_head(md);
658 if (evlist->overwrite) {
660 * If we're further behind than half the buffer, there's a chance
661 * the writer will bite our tail and mess up the samples under us.
663 * If we somehow ended up ahead of the head, we got messed up.
665 * In either case, truncate and restart at head.
667 int diff = head - old;
668 if (diff > md->mask / 2 || diff < 0) {
669 fprintf(stderr, "WARNING: failed to keep up with mmap data.\n");
672 * head points to a known good entry, start there.
681 event = (union perf_event *)&data[old & md->mask];
682 size = event->header.size;
685 * Event straddles the mmap boundary -- header should always
686 * be inside due to u64 alignment of output.
688 if ((old & md->mask) + size != ((old + size) & md->mask)) {
689 unsigned int offset = old;
690 unsigned int len = min(sizeof(*event), size), cpy;
691 void *dst = md->event_copy;
694 cpy = min(md->mask + 1 - (offset & md->mask), len);
695 memcpy(dst, &data[offset & md->mask], cpy);
701 event = (union perf_event *) md->event_copy;
712 static bool perf_mmap__empty(struct perf_mmap *md)
714 return perf_mmap__read_head(md) == md->prev && !md->auxtrace_mmap.base;
717 static void perf_evlist__mmap_get(struct perf_evlist *evlist, int idx)
719 atomic_inc(&evlist->mmap[idx].refcnt);
722 static void perf_evlist__mmap_put(struct perf_evlist *evlist, int idx)
724 BUG_ON(atomic_read(&evlist->mmap[idx].refcnt) == 0);
726 if (atomic_dec_and_test(&evlist->mmap[idx].refcnt))
727 __perf_evlist__munmap(evlist, idx);
730 void perf_evlist__mmap_consume(struct perf_evlist *evlist, int idx)
732 struct perf_mmap *md = &evlist->mmap[idx];
734 if (!evlist->overwrite) {
737 perf_mmap__write_tail(md, old);
740 if (atomic_read(&md->refcnt) == 1 && perf_mmap__empty(md))
741 perf_evlist__mmap_put(evlist, idx);
744 int __weak auxtrace_mmap__mmap(struct auxtrace_mmap *mm __maybe_unused,
745 struct auxtrace_mmap_params *mp __maybe_unused,
746 void *userpg __maybe_unused,
747 int fd __maybe_unused)
752 void __weak auxtrace_mmap__munmap(struct auxtrace_mmap *mm __maybe_unused)
756 void __weak auxtrace_mmap_params__init(
757 struct auxtrace_mmap_params *mp __maybe_unused,
758 off_t auxtrace_offset __maybe_unused,
759 unsigned int auxtrace_pages __maybe_unused,
760 bool auxtrace_overwrite __maybe_unused)
764 void __weak auxtrace_mmap_params__set_idx(
765 struct auxtrace_mmap_params *mp __maybe_unused,
766 struct perf_evlist *evlist __maybe_unused,
767 int idx __maybe_unused,
768 bool per_cpu __maybe_unused)
772 static void __perf_evlist__munmap(struct perf_evlist *evlist, int idx)
774 if (evlist->mmap[idx].base != NULL) {
775 munmap(evlist->mmap[idx].base, evlist->mmap_len);
776 evlist->mmap[idx].base = NULL;
777 atomic_set(&evlist->mmap[idx].refcnt, 0);
779 auxtrace_mmap__munmap(&evlist->mmap[idx].auxtrace_mmap);
782 void perf_evlist__munmap(struct perf_evlist *evlist)
786 if (evlist->mmap == NULL)
789 for (i = 0; i < evlist->nr_mmaps; i++)
790 __perf_evlist__munmap(evlist, i);
792 zfree(&evlist->mmap);
795 static int perf_evlist__alloc_mmap(struct perf_evlist *evlist)
797 evlist->nr_mmaps = cpu_map__nr(evlist->cpus);
798 if (cpu_map__empty(evlist->cpus))
799 evlist->nr_mmaps = thread_map__nr(evlist->threads);
800 evlist->mmap = zalloc(evlist->nr_mmaps * sizeof(struct perf_mmap));
801 return evlist->mmap != NULL ? 0 : -ENOMEM;
807 struct auxtrace_mmap_params auxtrace_mp;
810 static int __perf_evlist__mmap(struct perf_evlist *evlist, int idx,
811 struct mmap_params *mp, int fd)
814 * The last one will be done at perf_evlist__mmap_consume(), so that we
815 * make sure we don't prevent tools from consuming every last event in
818 * I.e. we can get the POLLHUP meaning that the fd doesn't exist
819 * anymore, but the last events for it are still in the ring buffer,
820 * waiting to be consumed.
822 * Tools can chose to ignore this at their own discretion, but the
823 * evlist layer can't just drop it when filtering events in
824 * perf_evlist__filter_pollfd().
826 atomic_set(&evlist->mmap[idx].refcnt, 2);
827 evlist->mmap[idx].prev = 0;
828 evlist->mmap[idx].mask = mp->mask;
829 evlist->mmap[idx].base = mmap(NULL, evlist->mmap_len, mp->prot,
831 if (evlist->mmap[idx].base == MAP_FAILED) {
832 pr_debug2("failed to mmap perf event ring buffer, error %d\n",
834 evlist->mmap[idx].base = NULL;
838 if (auxtrace_mmap__mmap(&evlist->mmap[idx].auxtrace_mmap,
839 &mp->auxtrace_mp, evlist->mmap[idx].base, fd))
845 static int perf_evlist__mmap_per_evsel(struct perf_evlist *evlist, int idx,
846 struct mmap_params *mp, int cpu,
847 int thread, int *output)
849 struct perf_evsel *evsel;
851 evlist__for_each(evlist, evsel) {
854 if (evsel->system_wide && thread)
857 fd = FD(evsel, cpu, thread);
861 if (__perf_evlist__mmap(evlist, idx, mp, *output) < 0)
864 if (ioctl(fd, PERF_EVENT_IOC_SET_OUTPUT, *output) != 0)
867 perf_evlist__mmap_get(evlist, idx);
871 * The system_wide flag causes a selected event to be opened
872 * always without a pid. Consequently it will never get a
873 * POLLHUP, but it is used for tracking in combination with
874 * other events, so it should not need to be polled anyway.
875 * Therefore don't add it for polling.
877 if (!evsel->system_wide &&
878 __perf_evlist__add_pollfd(evlist, fd, idx) < 0) {
879 perf_evlist__mmap_put(evlist, idx);
883 if (evsel->attr.read_format & PERF_FORMAT_ID) {
884 if (perf_evlist__id_add_fd(evlist, evsel, cpu, thread,
887 perf_evlist__set_sid_idx(evlist, evsel, idx, cpu,
895 static int perf_evlist__mmap_per_cpu(struct perf_evlist *evlist,
896 struct mmap_params *mp)
899 int nr_cpus = cpu_map__nr(evlist->cpus);
900 int nr_threads = thread_map__nr(evlist->threads);
902 pr_debug2("perf event ring buffer mmapped per cpu\n");
903 for (cpu = 0; cpu < nr_cpus; cpu++) {
906 auxtrace_mmap_params__set_idx(&mp->auxtrace_mp, evlist, cpu,
909 for (thread = 0; thread < nr_threads; thread++) {
910 if (perf_evlist__mmap_per_evsel(evlist, cpu, mp, cpu,
919 for (cpu = 0; cpu < nr_cpus; cpu++)
920 __perf_evlist__munmap(evlist, cpu);
924 static int perf_evlist__mmap_per_thread(struct perf_evlist *evlist,
925 struct mmap_params *mp)
928 int nr_threads = thread_map__nr(evlist->threads);
930 pr_debug2("perf event ring buffer mmapped per thread\n");
931 for (thread = 0; thread < nr_threads; thread++) {
934 auxtrace_mmap_params__set_idx(&mp->auxtrace_mp, evlist, thread,
937 if (perf_evlist__mmap_per_evsel(evlist, thread, mp, 0, thread,
945 for (thread = 0; thread < nr_threads; thread++)
946 __perf_evlist__munmap(evlist, thread);
950 static size_t perf_evlist__mmap_size(unsigned long pages)
952 if (pages == UINT_MAX) {
955 if (sysctl__read_int("kernel/perf_event_mlock_kb", &max) < 0) {
957 * Pick a once upon a time good value, i.e. things look
958 * strange since we can't read a sysctl value, but lets not
963 max -= (page_size / 1024);
966 pages = (max * 1024) / page_size;
967 if (!is_power_of_2(pages))
968 pages = rounddown_pow_of_two(pages);
969 } else if (!is_power_of_2(pages))
972 return (pages + 1) * page_size;
975 static long parse_pages_arg(const char *str, unsigned long min,
978 unsigned long pages, val;
979 static struct parse_tag tags[] = {
980 { .tag = 'B', .mult = 1 },
981 { .tag = 'K', .mult = 1 << 10 },
982 { .tag = 'M', .mult = 1 << 20 },
983 { .tag = 'G', .mult = 1 << 30 },
990 val = parse_tag_value(str, tags);
991 if (val != (unsigned long) -1) {
992 /* we got file size value */
993 pages = PERF_ALIGN(val, page_size) / page_size;
995 /* we got pages count value */
997 pages = strtoul(str, &eptr, 10);
1002 if (pages == 0 && min == 0) {
1003 /* leave number of pages at 0 */
1004 } else if (!is_power_of_2(pages)) {
1005 /* round pages up to next power of 2 */
1006 pages = roundup_pow_of_two(pages);
1009 pr_info("rounding mmap pages size to %lu bytes (%lu pages)\n",
1010 pages * page_size, pages);
1019 int __perf_evlist__parse_mmap_pages(unsigned int *mmap_pages, const char *str)
1021 unsigned long max = UINT_MAX;
1024 if (max > SIZE_MAX / page_size)
1025 max = SIZE_MAX / page_size;
1027 pages = parse_pages_arg(str, 1, max);
1029 pr_err("Invalid argument for --mmap_pages/-m\n");
1033 *mmap_pages = pages;
1037 int perf_evlist__parse_mmap_pages(const struct option *opt, const char *str,
1038 int unset __maybe_unused)
1040 return __perf_evlist__parse_mmap_pages(opt->value, str);
1044 * perf_evlist__mmap_ex - Create mmaps to receive events.
1045 * @evlist: list of events
1046 * @pages: map length in pages
1047 * @overwrite: overwrite older events?
1048 * @auxtrace_pages - auxtrace map length in pages
1049 * @auxtrace_overwrite - overwrite older auxtrace data?
1051 * If @overwrite is %false the user needs to signal event consumption using
1052 * perf_mmap__write_tail(). Using perf_evlist__mmap_read() does this
1055 * Similarly, if @auxtrace_overwrite is %false the user needs to signal data
1056 * consumption using auxtrace_mmap__write_tail().
1058 * Return: %0 on success, negative error code otherwise.
1060 int perf_evlist__mmap_ex(struct perf_evlist *evlist, unsigned int pages,
1061 bool overwrite, unsigned int auxtrace_pages,
1062 bool auxtrace_overwrite)
1064 struct perf_evsel *evsel;
1065 const struct cpu_map *cpus = evlist->cpus;
1066 const struct thread_map *threads = evlist->threads;
1067 struct mmap_params mp = {
1068 .prot = PROT_READ | (overwrite ? 0 : PROT_WRITE),
1071 if (evlist->mmap == NULL && perf_evlist__alloc_mmap(evlist) < 0)
1074 if (evlist->pollfd.entries == NULL && perf_evlist__alloc_pollfd(evlist) < 0)
1077 evlist->overwrite = overwrite;
1078 evlist->mmap_len = perf_evlist__mmap_size(pages);
1079 pr_debug("mmap size %zuB\n", evlist->mmap_len);
1080 mp.mask = evlist->mmap_len - page_size - 1;
1082 auxtrace_mmap_params__init(&mp.auxtrace_mp, evlist->mmap_len,
1083 auxtrace_pages, auxtrace_overwrite);
1085 evlist__for_each(evlist, evsel) {
1086 if ((evsel->attr.read_format & PERF_FORMAT_ID) &&
1087 evsel->sample_id == NULL &&
1088 perf_evsel__alloc_id(evsel, cpu_map__nr(cpus), threads->nr) < 0)
1092 if (cpu_map__empty(cpus))
1093 return perf_evlist__mmap_per_thread(evlist, &mp);
1095 return perf_evlist__mmap_per_cpu(evlist, &mp);
1098 int perf_evlist__mmap(struct perf_evlist *evlist, unsigned int pages,
1101 return perf_evlist__mmap_ex(evlist, pages, overwrite, 0, false);
1104 static int perf_evlist__propagate_maps(struct perf_evlist *evlist,
1107 struct perf_evsel *evsel;
1109 evlist__for_each(evlist, evsel) {
1111 * We already have cpus for evsel (via PMU sysfs) so
1112 * keep it, if there's no target cpu list defined.
1114 if (evsel->cpus && has_user_cpus)
1115 cpu_map__put(evsel->cpus);
1117 if (!evsel->cpus || has_user_cpus)
1118 evsel->cpus = cpu_map__get(evlist->cpus);
1120 evsel->threads = thread_map__get(evlist->threads);
1122 if ((evlist->cpus && !evsel->cpus) ||
1123 (evlist->threads && !evsel->threads))
1130 int perf_evlist__create_maps(struct perf_evlist *evlist, struct target *target)
1132 evlist->threads = thread_map__new_str(target->pid, target->tid,
1135 if (evlist->threads == NULL)
1138 if (target__uses_dummy_map(target))
1139 evlist->cpus = cpu_map__dummy_new();
1141 evlist->cpus = cpu_map__new(target->cpu_list);
1143 if (evlist->cpus == NULL)
1144 goto out_delete_threads;
1146 return perf_evlist__propagate_maps(evlist, !!target->cpu_list);
1149 thread_map__put(evlist->threads);
1150 evlist->threads = NULL;
1154 int perf_evlist__set_maps(struct perf_evlist *evlist,
1155 struct cpu_map *cpus,
1156 struct thread_map *threads)
1159 cpu_map__put(evlist->cpus);
1161 evlist->cpus = cpus;
1163 if (evlist->threads)
1164 thread_map__put(evlist->threads);
1166 evlist->threads = threads;
1168 return perf_evlist__propagate_maps(evlist, false);
1171 int perf_evlist__apply_filters(struct perf_evlist *evlist, struct perf_evsel **err_evsel)
1173 struct perf_evsel *evsel;
1175 const int ncpus = cpu_map__nr(evlist->cpus),
1176 nthreads = thread_map__nr(evlist->threads);
1178 evlist__for_each(evlist, evsel) {
1179 if (evsel->filter == NULL)
1182 err = perf_evsel__apply_filter(evsel, ncpus, nthreads, evsel->filter);
1192 int perf_evlist__set_filter(struct perf_evlist *evlist, const char *filter)
1194 struct perf_evsel *evsel;
1197 evlist__for_each(evlist, evsel) {
1198 err = perf_evsel__set_filter(evsel, filter);
1206 int perf_evlist__set_filter_pids(struct perf_evlist *evlist, size_t npids, pid_t *pids)
1212 for (i = 0; i < npids; ++i) {
1214 if (asprintf(&filter, "common_pid != %d", pids[i]) < 0)
1219 if (asprintf(&tmp, "%s && common_pid != %d", filter, pids[i]) < 0)
1227 ret = perf_evlist__set_filter(evlist, filter);
1233 int perf_evlist__set_filter_pid(struct perf_evlist *evlist, pid_t pid)
1235 return perf_evlist__set_filter_pids(evlist, 1, &pid);
1238 bool perf_evlist__valid_sample_type(struct perf_evlist *evlist)
1240 struct perf_evsel *pos;
1242 if (evlist->nr_entries == 1)
1245 if (evlist->id_pos < 0 || evlist->is_pos < 0)
1248 evlist__for_each(evlist, pos) {
1249 if (pos->id_pos != evlist->id_pos ||
1250 pos->is_pos != evlist->is_pos)
1257 u64 __perf_evlist__combined_sample_type(struct perf_evlist *evlist)
1259 struct perf_evsel *evsel;
1261 if (evlist->combined_sample_type)
1262 return evlist->combined_sample_type;
1264 evlist__for_each(evlist, evsel)
1265 evlist->combined_sample_type |= evsel->attr.sample_type;
1267 return evlist->combined_sample_type;
1270 u64 perf_evlist__combined_sample_type(struct perf_evlist *evlist)
1272 evlist->combined_sample_type = 0;
1273 return __perf_evlist__combined_sample_type(evlist);
1276 u64 perf_evlist__combined_branch_type(struct perf_evlist *evlist)
1278 struct perf_evsel *evsel;
1279 u64 branch_type = 0;
1281 evlist__for_each(evlist, evsel)
1282 branch_type |= evsel->attr.branch_sample_type;
1286 bool perf_evlist__valid_read_format(struct perf_evlist *evlist)
1288 struct perf_evsel *first = perf_evlist__first(evlist), *pos = first;
1289 u64 read_format = first->attr.read_format;
1290 u64 sample_type = first->attr.sample_type;
1292 evlist__for_each(evlist, pos) {
1293 if (read_format != pos->attr.read_format)
1297 /* PERF_SAMPLE_READ imples PERF_FORMAT_ID. */
1298 if ((sample_type & PERF_SAMPLE_READ) &&
1299 !(read_format & PERF_FORMAT_ID)) {
1306 u64 perf_evlist__read_format(struct perf_evlist *evlist)
1308 struct perf_evsel *first = perf_evlist__first(evlist);
1309 return first->attr.read_format;
1312 u16 perf_evlist__id_hdr_size(struct perf_evlist *evlist)
1314 struct perf_evsel *first = perf_evlist__first(evlist);
1315 struct perf_sample *data;
1319 if (!first->attr.sample_id_all)
1322 sample_type = first->attr.sample_type;
1324 if (sample_type & PERF_SAMPLE_TID)
1325 size += sizeof(data->tid) * 2;
1327 if (sample_type & PERF_SAMPLE_TIME)
1328 size += sizeof(data->time);
1330 if (sample_type & PERF_SAMPLE_ID)
1331 size += sizeof(data->id);
1333 if (sample_type & PERF_SAMPLE_STREAM_ID)
1334 size += sizeof(data->stream_id);
1336 if (sample_type & PERF_SAMPLE_CPU)
1337 size += sizeof(data->cpu) * 2;
1339 if (sample_type & PERF_SAMPLE_IDENTIFIER)
1340 size += sizeof(data->id);
1345 bool perf_evlist__valid_sample_id_all(struct perf_evlist *evlist)
1347 struct perf_evsel *first = perf_evlist__first(evlist), *pos = first;
1349 evlist__for_each_continue(evlist, pos) {
1350 if (first->attr.sample_id_all != pos->attr.sample_id_all)
1357 bool perf_evlist__sample_id_all(struct perf_evlist *evlist)
1359 struct perf_evsel *first = perf_evlist__first(evlist);
1360 return first->attr.sample_id_all;
1363 void perf_evlist__set_selected(struct perf_evlist *evlist,
1364 struct perf_evsel *evsel)
1366 evlist->selected = evsel;
1369 void perf_evlist__close(struct perf_evlist *evlist)
1371 struct perf_evsel *evsel;
1372 int ncpus = cpu_map__nr(evlist->cpus);
1373 int nthreads = thread_map__nr(evlist->threads);
1376 evlist__for_each_reverse(evlist, evsel) {
1377 n = evsel->cpus ? evsel->cpus->nr : ncpus;
1378 perf_evsel__close(evsel, n, nthreads);
1382 static int perf_evlist__create_syswide_maps(struct perf_evlist *evlist)
1387 * Try reading /sys/devices/system/cpu/online to get
1390 * FIXME: -ENOMEM is the best we can do here, the cpu_map
1391 * code needs an overhaul to properly forward the
1392 * error, and we may not want to do that fallback to a
1393 * default cpu identity map :-\
1395 evlist->cpus = cpu_map__new(NULL);
1396 if (evlist->cpus == NULL)
1399 evlist->threads = thread_map__new_dummy();
1400 if (evlist->threads == NULL)
1407 cpu_map__put(evlist->cpus);
1408 evlist->cpus = NULL;
1412 int perf_evlist__open(struct perf_evlist *evlist)
1414 struct perf_evsel *evsel;
1418 * Default: one fd per CPU, all threads, aka systemwide
1419 * as sys_perf_event_open(cpu = -1, thread = -1) is EINVAL
1421 if (evlist->threads == NULL && evlist->cpus == NULL) {
1422 err = perf_evlist__create_syswide_maps(evlist);
1427 perf_evlist__update_id_pos(evlist);
1429 evlist__for_each(evlist, evsel) {
1430 err = perf_evsel__open(evsel, evlist->cpus, evlist->threads);
1437 perf_evlist__close(evlist);
1442 int perf_evlist__prepare_workload(struct perf_evlist *evlist, struct target *target,
1443 const char *argv[], bool pipe_output,
1444 void (*exec_error)(int signo, siginfo_t *info, void *ucontext))
1446 int child_ready_pipe[2], go_pipe[2];
1449 if (pipe(child_ready_pipe) < 0) {
1450 perror("failed to create 'ready' pipe");
1454 if (pipe(go_pipe) < 0) {
1455 perror("failed to create 'go' pipe");
1456 goto out_close_ready_pipe;
1459 evlist->workload.pid = fork();
1460 if (evlist->workload.pid < 0) {
1461 perror("failed to fork");
1462 goto out_close_pipes;
1465 if (!evlist->workload.pid) {
1471 signal(SIGTERM, SIG_DFL);
1473 close(child_ready_pipe[0]);
1475 fcntl(go_pipe[0], F_SETFD, FD_CLOEXEC);
1478 * Tell the parent we're ready to go
1480 close(child_ready_pipe[1]);
1483 * Wait until the parent tells us to go.
1485 ret = read(go_pipe[0], &bf, 1);
1487 * The parent will ask for the execvp() to be performed by
1488 * writing exactly one byte, in workload.cork_fd, usually via
1489 * perf_evlist__start_workload().
1491 * For cancelling the workload without actually running it,
1492 * the parent will just close workload.cork_fd, without writing
1493 * anything, i.e. read will return zero and we just exit()
1498 perror("unable to read pipe");
1502 execvp(argv[0], (char **)argv);
1507 val.sival_int = errno;
1508 if (sigqueue(getppid(), SIGUSR1, val))
1516 struct sigaction act = {
1517 .sa_flags = SA_SIGINFO,
1518 .sa_sigaction = exec_error,
1520 sigaction(SIGUSR1, &act, NULL);
1523 if (target__none(target)) {
1524 if (evlist->threads == NULL) {
1525 fprintf(stderr, "FATAL: evlist->threads need to be set at this point (%s:%d).\n",
1526 __func__, __LINE__);
1527 goto out_close_pipes;
1529 thread_map__set_pid(evlist->threads, 0, evlist->workload.pid);
1532 close(child_ready_pipe[1]);
1535 * wait for child to settle
1537 if (read(child_ready_pipe[0], &bf, 1) == -1) {
1538 perror("unable to read pipe");
1539 goto out_close_pipes;
1542 fcntl(go_pipe[1], F_SETFD, FD_CLOEXEC);
1543 evlist->workload.cork_fd = go_pipe[1];
1544 close(child_ready_pipe[0]);
1550 out_close_ready_pipe:
1551 close(child_ready_pipe[0]);
1552 close(child_ready_pipe[1]);
1556 int perf_evlist__start_workload(struct perf_evlist *evlist)
1558 if (evlist->workload.cork_fd > 0) {
1562 * Remove the cork, let it rip!
1564 ret = write(evlist->workload.cork_fd, &bf, 1);
1566 perror("enable to write to pipe");
1568 close(evlist->workload.cork_fd);
1575 int perf_evlist__parse_sample(struct perf_evlist *evlist, union perf_event *event,
1576 struct perf_sample *sample)
1578 struct perf_evsel *evsel = perf_evlist__event2evsel(evlist, event);
1582 return perf_evsel__parse_sample(evsel, event, sample);
1585 size_t perf_evlist__fprintf(struct perf_evlist *evlist, FILE *fp)
1587 struct perf_evsel *evsel;
1590 evlist__for_each(evlist, evsel) {
1591 printed += fprintf(fp, "%s%s", evsel->idx ? ", " : "",
1592 perf_evsel__name(evsel));
1595 return printed + fprintf(fp, "\n");
1598 int perf_evlist__strerror_open(struct perf_evlist *evlist __maybe_unused,
1599 int err, char *buf, size_t size)
1602 char sbuf[STRERR_BUFSIZE], *emsg = strerror_r(err, sbuf, sizeof(sbuf));
1607 printed = scnprintf(buf, size,
1609 "Hint:\tCheck /proc/sys/kernel/perf_event_paranoid setting.", emsg);
1611 value = perf_event_paranoid();
1613 printed += scnprintf(buf + printed, size - printed, "\nHint:\t");
1616 printed += scnprintf(buf + printed, size - printed,
1617 "For your workloads it needs to be <= 1\nHint:\t");
1619 printed += scnprintf(buf + printed, size - printed,
1620 "For system wide tracing it needs to be set to -1.\n");
1622 printed += scnprintf(buf + printed, size - printed,
1623 "Hint:\tTry: 'sudo sh -c \"echo -1 > /proc/sys/kernel/perf_event_paranoid\"'\n"
1624 "Hint:\tThe current value is %d.", value);
1627 scnprintf(buf, size, "%s", emsg);
1634 int perf_evlist__strerror_mmap(struct perf_evlist *evlist, int err, char *buf, size_t size)
1636 char sbuf[STRERR_BUFSIZE], *emsg = strerror_r(err, sbuf, sizeof(sbuf));
1637 int pages_attempted = evlist->mmap_len / 1024, pages_max_per_user, printed = 0;
1641 sysctl__read_int("kernel/perf_event_mlock_kb", &pages_max_per_user);
1642 printed += scnprintf(buf + printed, size - printed,
1644 "Hint:\tCheck /proc/sys/kernel/perf_event_mlock_kb (%d kB) setting.\n"
1645 "Hint:\tTried using %zd kB.\n",
1646 emsg, pages_max_per_user, pages_attempted);
1648 if (pages_attempted >= pages_max_per_user) {
1649 printed += scnprintf(buf + printed, size - printed,
1650 "Hint:\tTry 'sudo sh -c \"echo %d > /proc/sys/kernel/perf_event_mlock_kb\"', or\n",
1651 pages_max_per_user + pages_attempted);
1654 printed += scnprintf(buf + printed, size - printed,
1655 "Hint:\tTry using a smaller -m/--mmap-pages value.");
1658 scnprintf(buf, size, "%s", emsg);
1665 void perf_evlist__to_front(struct perf_evlist *evlist,
1666 struct perf_evsel *move_evsel)
1668 struct perf_evsel *evsel, *n;
1671 if (move_evsel == perf_evlist__first(evlist))
1674 evlist__for_each_safe(evlist, n, evsel) {
1675 if (evsel->leader == move_evsel->leader)
1676 list_move_tail(&evsel->node, &move);
1679 list_splice(&move, &evlist->entries);
1682 void perf_evlist__set_tracking_event(struct perf_evlist *evlist,
1683 struct perf_evsel *tracking_evsel)
1685 struct perf_evsel *evsel;
1687 if (tracking_evsel->tracking)
1690 evlist__for_each(evlist, evsel) {
1691 if (evsel != tracking_evsel)
1692 evsel->tracking = false;
1695 tracking_evsel->tracking = true;