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);
102 perf_evsel__delete(pos);
105 evlist->nr_entries = 0;
108 void perf_evlist__exit(struct perf_evlist *evlist)
110 zfree(&evlist->mmap);
111 fdarray__exit(&evlist->pollfd);
114 void perf_evlist__delete(struct perf_evlist *evlist)
116 perf_evlist__munmap(evlist);
117 perf_evlist__close(evlist);
118 cpu_map__put(evlist->cpus);
119 thread_map__put(evlist->threads);
121 evlist->threads = NULL;
122 perf_evlist__purge(evlist);
123 perf_evlist__exit(evlist);
127 static void __perf_evlist__propagate_maps(struct perf_evlist *evlist,
128 struct perf_evsel *evsel)
131 * We already have cpus for evsel (via PMU sysfs) so
132 * keep it, if there's no target cpu list defined.
134 if (!evsel->own_cpus || evlist->has_user_cpus) {
135 cpu_map__put(evsel->cpus);
136 evsel->cpus = cpu_map__get(evlist->cpus);
137 } else if (evsel->cpus != evsel->own_cpus) {
138 cpu_map__put(evsel->cpus);
139 evsel->cpus = cpu_map__get(evsel->own_cpus);
142 thread_map__put(evsel->threads);
143 evsel->threads = thread_map__get(evlist->threads);
146 static void perf_evlist__propagate_maps(struct perf_evlist *evlist)
148 struct perf_evsel *evsel;
150 evlist__for_each(evlist, evsel)
151 __perf_evlist__propagate_maps(evlist, evsel);
154 void perf_evlist__add(struct perf_evlist *evlist, struct perf_evsel *entry)
156 entry->evlist = evlist;
157 list_add_tail(&entry->node, &evlist->entries);
158 entry->idx = evlist->nr_entries;
159 entry->tracking = !entry->idx;
161 if (!evlist->nr_entries++)
162 perf_evlist__set_id_pos(evlist);
164 __perf_evlist__propagate_maps(evlist, entry);
167 void perf_evlist__splice_list_tail(struct perf_evlist *evlist,
168 struct list_head *list)
170 struct perf_evsel *evsel, *temp;
172 __evlist__for_each_safe(list, temp, evsel) {
173 list_del_init(&evsel->node);
174 perf_evlist__add(evlist, evsel);
178 void __perf_evlist__set_leader(struct list_head *list)
180 struct perf_evsel *evsel, *leader;
182 leader = list_entry(list->next, struct perf_evsel, node);
183 evsel = list_entry(list->prev, struct perf_evsel, node);
185 leader->nr_members = evsel->idx - leader->idx + 1;
187 __evlist__for_each(list, evsel) {
188 evsel->leader = leader;
192 void perf_evlist__set_leader(struct perf_evlist *evlist)
194 if (evlist->nr_entries) {
195 evlist->nr_groups = evlist->nr_entries > 1 ? 1 : 0;
196 __perf_evlist__set_leader(&evlist->entries);
200 int perf_evlist__add_default(struct perf_evlist *evlist)
202 struct perf_event_attr attr = {
203 .type = PERF_TYPE_HARDWARE,
204 .config = PERF_COUNT_HW_CPU_CYCLES,
206 struct perf_evsel *evsel;
208 event_attr_init(&attr);
210 evsel = perf_evsel__new(&attr);
214 /* use strdup() because free(evsel) assumes name is allocated */
215 evsel->name = strdup("cycles");
219 perf_evlist__add(evlist, evsel);
222 perf_evsel__delete(evsel);
227 static int perf_evlist__add_attrs(struct perf_evlist *evlist,
228 struct perf_event_attr *attrs, size_t nr_attrs)
230 struct perf_evsel *evsel, *n;
234 for (i = 0; i < nr_attrs; i++) {
235 evsel = perf_evsel__new_idx(attrs + i, evlist->nr_entries + i);
237 goto out_delete_partial_list;
238 list_add_tail(&evsel->node, &head);
241 perf_evlist__splice_list_tail(evlist, &head);
245 out_delete_partial_list:
246 __evlist__for_each_safe(&head, n, evsel)
247 perf_evsel__delete(evsel);
251 int __perf_evlist__add_default_attrs(struct perf_evlist *evlist,
252 struct perf_event_attr *attrs, size_t nr_attrs)
256 for (i = 0; i < nr_attrs; i++)
257 event_attr_init(attrs + i);
259 return perf_evlist__add_attrs(evlist, attrs, nr_attrs);
263 perf_evlist__find_tracepoint_by_id(struct perf_evlist *evlist, int id)
265 struct perf_evsel *evsel;
267 evlist__for_each(evlist, evsel) {
268 if (evsel->attr.type == PERF_TYPE_TRACEPOINT &&
269 (int)evsel->attr.config == id)
277 perf_evlist__find_tracepoint_by_name(struct perf_evlist *evlist,
280 struct perf_evsel *evsel;
282 evlist__for_each(evlist, evsel) {
283 if ((evsel->attr.type == PERF_TYPE_TRACEPOINT) &&
284 (strcmp(evsel->name, name) == 0))
291 int perf_evlist__add_newtp(struct perf_evlist *evlist,
292 const char *sys, const char *name, void *handler)
294 struct perf_evsel *evsel = perf_evsel__newtp(sys, name);
299 evsel->handler = handler;
300 perf_evlist__add(evlist, evsel);
304 static int perf_evlist__nr_threads(struct perf_evlist *evlist,
305 struct perf_evsel *evsel)
307 if (evsel->system_wide)
310 return thread_map__nr(evlist->threads);
313 void perf_evlist__disable(struct perf_evlist *evlist)
316 struct perf_evsel *pos;
317 int nr_cpus = cpu_map__nr(evlist->cpus);
320 for (cpu = 0; cpu < nr_cpus; cpu++) {
321 evlist__for_each(evlist, pos) {
322 if (!perf_evsel__is_group_leader(pos) || !pos->fd)
324 nr_threads = perf_evlist__nr_threads(evlist, pos);
325 for (thread = 0; thread < nr_threads; thread++)
326 ioctl(FD(pos, cpu, thread),
327 PERF_EVENT_IOC_DISABLE, 0);
331 evlist->enabled = false;
334 void perf_evlist__enable(struct perf_evlist *evlist)
337 struct perf_evsel *pos;
338 int nr_cpus = cpu_map__nr(evlist->cpus);
341 for (cpu = 0; cpu < nr_cpus; cpu++) {
342 evlist__for_each(evlist, pos) {
343 if (!perf_evsel__is_group_leader(pos) || !pos->fd)
345 nr_threads = perf_evlist__nr_threads(evlist, pos);
346 for (thread = 0; thread < nr_threads; thread++)
347 ioctl(FD(pos, cpu, thread),
348 PERF_EVENT_IOC_ENABLE, 0);
352 evlist->enabled = true;
355 void perf_evlist__toggle_enable(struct perf_evlist *evlist)
357 (evlist->enabled ? perf_evlist__disable : perf_evlist__enable)(evlist);
360 int perf_evlist__disable_event(struct perf_evlist *evlist,
361 struct perf_evsel *evsel)
363 int cpu, thread, err;
364 int nr_cpus = cpu_map__nr(evlist->cpus);
365 int nr_threads = perf_evlist__nr_threads(evlist, evsel);
370 for (cpu = 0; cpu < nr_cpus; cpu++) {
371 for (thread = 0; thread < nr_threads; thread++) {
372 err = ioctl(FD(evsel, cpu, thread),
373 PERF_EVENT_IOC_DISABLE, 0);
381 int perf_evlist__enable_event(struct perf_evlist *evlist,
382 struct perf_evsel *evsel)
384 int cpu, thread, err;
385 int nr_cpus = cpu_map__nr(evlist->cpus);
386 int nr_threads = perf_evlist__nr_threads(evlist, evsel);
391 for (cpu = 0; cpu < nr_cpus; cpu++) {
392 for (thread = 0; thread < nr_threads; thread++) {
393 err = ioctl(FD(evsel, cpu, thread),
394 PERF_EVENT_IOC_ENABLE, 0);
402 static int perf_evlist__enable_event_cpu(struct perf_evlist *evlist,
403 struct perf_evsel *evsel, int cpu)
406 int nr_threads = perf_evlist__nr_threads(evlist, evsel);
411 for (thread = 0; thread < nr_threads; thread++) {
412 err = ioctl(FD(evsel, cpu, thread),
413 PERF_EVENT_IOC_ENABLE, 0);
420 static int perf_evlist__enable_event_thread(struct perf_evlist *evlist,
421 struct perf_evsel *evsel,
425 int nr_cpus = cpu_map__nr(evlist->cpus);
430 for (cpu = 0; cpu < nr_cpus; cpu++) {
431 err = ioctl(FD(evsel, cpu, thread), PERF_EVENT_IOC_ENABLE, 0);
438 int perf_evlist__enable_event_idx(struct perf_evlist *evlist,
439 struct perf_evsel *evsel, int idx)
441 bool per_cpu_mmaps = !cpu_map__empty(evlist->cpus);
444 return perf_evlist__enable_event_cpu(evlist, evsel, idx);
446 return perf_evlist__enable_event_thread(evlist, evsel, idx);
449 int perf_evlist__alloc_pollfd(struct perf_evlist *evlist)
451 int nr_cpus = cpu_map__nr(evlist->cpus);
452 int nr_threads = thread_map__nr(evlist->threads);
454 struct perf_evsel *evsel;
456 evlist__for_each(evlist, evsel) {
457 if (evsel->system_wide)
460 nfds += nr_cpus * nr_threads;
463 if (fdarray__available_entries(&evlist->pollfd) < nfds &&
464 fdarray__grow(&evlist->pollfd, nfds) < 0)
470 static int __perf_evlist__add_pollfd(struct perf_evlist *evlist, int fd, int idx)
472 int pos = fdarray__add(&evlist->pollfd, fd, POLLIN | POLLERR | POLLHUP);
474 * Save the idx so that when we filter out fds POLLHUP'ed we can
475 * close the associated evlist->mmap[] entry.
478 evlist->pollfd.priv[pos].idx = idx;
480 fcntl(fd, F_SETFL, O_NONBLOCK);
486 int perf_evlist__add_pollfd(struct perf_evlist *evlist, int fd)
488 return __perf_evlist__add_pollfd(evlist, fd, -1);
491 static void perf_evlist__munmap_filtered(struct fdarray *fda, int fd)
493 struct perf_evlist *evlist = container_of(fda, struct perf_evlist, pollfd);
495 perf_evlist__mmap_put(evlist, fda->priv[fd].idx);
498 int perf_evlist__filter_pollfd(struct perf_evlist *evlist, short revents_and_mask)
500 return fdarray__filter(&evlist->pollfd, revents_and_mask,
501 perf_evlist__munmap_filtered);
504 int perf_evlist__poll(struct perf_evlist *evlist, int timeout)
506 return fdarray__poll(&evlist->pollfd, timeout);
509 static void perf_evlist__id_hash(struct perf_evlist *evlist,
510 struct perf_evsel *evsel,
511 int cpu, int thread, u64 id)
514 struct perf_sample_id *sid = SID(evsel, cpu, thread);
518 hash = hash_64(sid->id, PERF_EVLIST__HLIST_BITS);
519 hlist_add_head(&sid->node, &evlist->heads[hash]);
522 void perf_evlist__id_add(struct perf_evlist *evlist, struct perf_evsel *evsel,
523 int cpu, int thread, u64 id)
525 perf_evlist__id_hash(evlist, evsel, cpu, thread, id);
526 evsel->id[evsel->ids++] = id;
529 static int perf_evlist__id_add_fd(struct perf_evlist *evlist,
530 struct perf_evsel *evsel,
531 int cpu, int thread, int fd)
533 u64 read_data[4] = { 0, };
534 int id_idx = 1; /* The first entry is the counter value */
538 ret = ioctl(fd, PERF_EVENT_IOC_ID, &id);
545 /* Legacy way to get event id.. All hail to old kernels! */
548 * This way does not work with group format read, so bail
551 if (perf_evlist__read_format(evlist) & PERF_FORMAT_GROUP)
554 if (!(evsel->attr.read_format & PERF_FORMAT_ID) ||
555 read(fd, &read_data, sizeof(read_data)) == -1)
558 if (evsel->attr.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
560 if (evsel->attr.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
563 id = read_data[id_idx];
566 perf_evlist__id_add(evlist, evsel, cpu, thread, id);
570 static void perf_evlist__set_sid_idx(struct perf_evlist *evlist,
571 struct perf_evsel *evsel, int idx, int cpu,
574 struct perf_sample_id *sid = SID(evsel, cpu, thread);
576 if (evlist->cpus && cpu >= 0)
577 sid->cpu = evlist->cpus->map[cpu];
580 if (!evsel->system_wide && evlist->threads && thread >= 0)
581 sid->tid = thread_map__pid(evlist->threads, thread);
586 struct perf_sample_id *perf_evlist__id2sid(struct perf_evlist *evlist, u64 id)
588 struct hlist_head *head;
589 struct perf_sample_id *sid;
592 hash = hash_64(id, PERF_EVLIST__HLIST_BITS);
593 head = &evlist->heads[hash];
595 hlist_for_each_entry(sid, head, node)
602 struct perf_evsel *perf_evlist__id2evsel(struct perf_evlist *evlist, u64 id)
604 struct perf_sample_id *sid;
606 if (evlist->nr_entries == 1 || !id)
607 return perf_evlist__first(evlist);
609 sid = perf_evlist__id2sid(evlist, id);
613 if (!perf_evlist__sample_id_all(evlist))
614 return perf_evlist__first(evlist);
619 static int perf_evlist__event2id(struct perf_evlist *evlist,
620 union perf_event *event, u64 *id)
622 const u64 *array = event->sample.array;
625 n = (event->header.size - sizeof(event->header)) >> 3;
627 if (event->header.type == PERF_RECORD_SAMPLE) {
628 if (evlist->id_pos >= n)
630 *id = array[evlist->id_pos];
632 if (evlist->is_pos > n)
640 static struct perf_evsel *perf_evlist__event2evsel(struct perf_evlist *evlist,
641 union perf_event *event)
643 struct perf_evsel *first = perf_evlist__first(evlist);
644 struct hlist_head *head;
645 struct perf_sample_id *sid;
649 if (evlist->nr_entries == 1)
652 if (!first->attr.sample_id_all &&
653 event->header.type != PERF_RECORD_SAMPLE)
656 if (perf_evlist__event2id(evlist, event, &id))
659 /* Synthesized events have an id of zero */
663 hash = hash_64(id, PERF_EVLIST__HLIST_BITS);
664 head = &evlist->heads[hash];
666 hlist_for_each_entry(sid, head, node) {
673 union perf_event *perf_evlist__mmap_read(struct perf_evlist *evlist, int idx)
675 struct perf_mmap *md = &evlist->mmap[idx];
678 unsigned char *data = md->base + page_size;
679 union perf_event *event = NULL;
682 * Check if event was unmapped due to a POLLHUP/POLLERR.
684 if (!atomic_read(&md->refcnt))
687 head = perf_mmap__read_head(md);
688 if (evlist->overwrite) {
690 * If we're further behind than half the buffer, there's a chance
691 * the writer will bite our tail and mess up the samples under us.
693 * If we somehow ended up ahead of the head, we got messed up.
695 * In either case, truncate and restart at head.
697 int diff = head - old;
698 if (diff > md->mask / 2 || diff < 0) {
699 fprintf(stderr, "WARNING: failed to keep up with mmap data.\n");
702 * head points to a known good entry, start there.
711 event = (union perf_event *)&data[old & md->mask];
712 size = event->header.size;
715 * Event straddles the mmap boundary -- header should always
716 * be inside due to u64 alignment of output.
718 if ((old & md->mask) + size != ((old + size) & md->mask)) {
719 unsigned int offset = old;
720 unsigned int len = min(sizeof(*event), size), cpy;
721 void *dst = md->event_copy;
724 cpy = min(md->mask + 1 - (offset & md->mask), len);
725 memcpy(dst, &data[offset & md->mask], cpy);
731 event = (union perf_event *) md->event_copy;
742 static bool perf_mmap__empty(struct perf_mmap *md)
744 return perf_mmap__read_head(md) == md->prev && !md->auxtrace_mmap.base;
747 static void perf_evlist__mmap_get(struct perf_evlist *evlist, int idx)
749 atomic_inc(&evlist->mmap[idx].refcnt);
752 static void perf_evlist__mmap_put(struct perf_evlist *evlist, int idx)
754 BUG_ON(atomic_read(&evlist->mmap[idx].refcnt) == 0);
756 if (atomic_dec_and_test(&evlist->mmap[idx].refcnt))
757 __perf_evlist__munmap(evlist, idx);
760 void perf_evlist__mmap_consume(struct perf_evlist *evlist, int idx)
762 struct perf_mmap *md = &evlist->mmap[idx];
764 if (!evlist->overwrite) {
767 perf_mmap__write_tail(md, old);
770 if (atomic_read(&md->refcnt) == 1 && perf_mmap__empty(md))
771 perf_evlist__mmap_put(evlist, idx);
774 int __weak auxtrace_mmap__mmap(struct auxtrace_mmap *mm __maybe_unused,
775 struct auxtrace_mmap_params *mp __maybe_unused,
776 void *userpg __maybe_unused,
777 int fd __maybe_unused)
782 void __weak auxtrace_mmap__munmap(struct auxtrace_mmap *mm __maybe_unused)
786 void __weak auxtrace_mmap_params__init(
787 struct auxtrace_mmap_params *mp __maybe_unused,
788 off_t auxtrace_offset __maybe_unused,
789 unsigned int auxtrace_pages __maybe_unused,
790 bool auxtrace_overwrite __maybe_unused)
794 void __weak auxtrace_mmap_params__set_idx(
795 struct auxtrace_mmap_params *mp __maybe_unused,
796 struct perf_evlist *evlist __maybe_unused,
797 int idx __maybe_unused,
798 bool per_cpu __maybe_unused)
802 static void __perf_evlist__munmap(struct perf_evlist *evlist, int idx)
804 if (evlist->mmap[idx].base != NULL) {
805 munmap(evlist->mmap[idx].base, evlist->mmap_len);
806 evlist->mmap[idx].base = NULL;
807 atomic_set(&evlist->mmap[idx].refcnt, 0);
809 auxtrace_mmap__munmap(&evlist->mmap[idx].auxtrace_mmap);
812 void perf_evlist__munmap(struct perf_evlist *evlist)
816 if (evlist->mmap == NULL)
819 for (i = 0; i < evlist->nr_mmaps; i++)
820 __perf_evlist__munmap(evlist, i);
822 zfree(&evlist->mmap);
825 static int perf_evlist__alloc_mmap(struct perf_evlist *evlist)
827 evlist->nr_mmaps = cpu_map__nr(evlist->cpus);
828 if (cpu_map__empty(evlist->cpus))
829 evlist->nr_mmaps = thread_map__nr(evlist->threads);
830 evlist->mmap = zalloc(evlist->nr_mmaps * sizeof(struct perf_mmap));
831 return evlist->mmap != NULL ? 0 : -ENOMEM;
837 struct auxtrace_mmap_params auxtrace_mp;
840 static int __perf_evlist__mmap(struct perf_evlist *evlist, int idx,
841 struct mmap_params *mp, int fd)
844 * The last one will be done at perf_evlist__mmap_consume(), so that we
845 * make sure we don't prevent tools from consuming every last event in
848 * I.e. we can get the POLLHUP meaning that the fd doesn't exist
849 * anymore, but the last events for it are still in the ring buffer,
850 * waiting to be consumed.
852 * Tools can chose to ignore this at their own discretion, but the
853 * evlist layer can't just drop it when filtering events in
854 * perf_evlist__filter_pollfd().
856 atomic_set(&evlist->mmap[idx].refcnt, 2);
857 evlist->mmap[idx].prev = 0;
858 evlist->mmap[idx].mask = mp->mask;
859 evlist->mmap[idx].base = mmap(NULL, evlist->mmap_len, mp->prot,
861 if (evlist->mmap[idx].base == MAP_FAILED) {
862 pr_debug2("failed to mmap perf event ring buffer, error %d\n",
864 evlist->mmap[idx].base = NULL;
868 if (auxtrace_mmap__mmap(&evlist->mmap[idx].auxtrace_mmap,
869 &mp->auxtrace_mp, evlist->mmap[idx].base, fd))
875 static int perf_evlist__mmap_per_evsel(struct perf_evlist *evlist, int idx,
876 struct mmap_params *mp, int cpu,
877 int thread, int *output)
879 struct perf_evsel *evsel;
881 evlist__for_each(evlist, evsel) {
884 if (evsel->system_wide && thread)
887 fd = FD(evsel, cpu, thread);
891 if (__perf_evlist__mmap(evlist, idx, mp, *output) < 0)
894 if (ioctl(fd, PERF_EVENT_IOC_SET_OUTPUT, *output) != 0)
897 perf_evlist__mmap_get(evlist, idx);
901 * The system_wide flag causes a selected event to be opened
902 * always without a pid. Consequently it will never get a
903 * POLLHUP, but it is used for tracking in combination with
904 * other events, so it should not need to be polled anyway.
905 * Therefore don't add it for polling.
907 if (!evsel->system_wide &&
908 __perf_evlist__add_pollfd(evlist, fd, idx) < 0) {
909 perf_evlist__mmap_put(evlist, idx);
913 if (evsel->attr.read_format & PERF_FORMAT_ID) {
914 if (perf_evlist__id_add_fd(evlist, evsel, cpu, thread,
917 perf_evlist__set_sid_idx(evlist, evsel, idx, cpu,
925 static int perf_evlist__mmap_per_cpu(struct perf_evlist *evlist,
926 struct mmap_params *mp)
929 int nr_cpus = cpu_map__nr(evlist->cpus);
930 int nr_threads = thread_map__nr(evlist->threads);
932 pr_debug2("perf event ring buffer mmapped per cpu\n");
933 for (cpu = 0; cpu < nr_cpus; cpu++) {
936 auxtrace_mmap_params__set_idx(&mp->auxtrace_mp, evlist, cpu,
939 for (thread = 0; thread < nr_threads; thread++) {
940 if (perf_evlist__mmap_per_evsel(evlist, cpu, mp, cpu,
949 for (cpu = 0; cpu < nr_cpus; cpu++)
950 __perf_evlist__munmap(evlist, cpu);
954 static int perf_evlist__mmap_per_thread(struct perf_evlist *evlist,
955 struct mmap_params *mp)
958 int nr_threads = thread_map__nr(evlist->threads);
960 pr_debug2("perf event ring buffer mmapped per thread\n");
961 for (thread = 0; thread < nr_threads; thread++) {
964 auxtrace_mmap_params__set_idx(&mp->auxtrace_mp, evlist, thread,
967 if (perf_evlist__mmap_per_evsel(evlist, thread, mp, 0, thread,
975 for (thread = 0; thread < nr_threads; thread++)
976 __perf_evlist__munmap(evlist, thread);
980 static size_t perf_evlist__mmap_size(unsigned long pages)
982 if (pages == UINT_MAX) {
985 if (sysctl__read_int("kernel/perf_event_mlock_kb", &max) < 0) {
987 * Pick a once upon a time good value, i.e. things look
988 * strange since we can't read a sysctl value, but lets not
993 max -= (page_size / 1024);
996 pages = (max * 1024) / page_size;
997 if (!is_power_of_2(pages))
998 pages = rounddown_pow_of_two(pages);
999 } else if (!is_power_of_2(pages))
1002 return (pages + 1) * page_size;
1005 static long parse_pages_arg(const char *str, unsigned long min,
1008 unsigned long pages, val;
1009 static struct parse_tag tags[] = {
1010 { .tag = 'B', .mult = 1 },
1011 { .tag = 'K', .mult = 1 << 10 },
1012 { .tag = 'M', .mult = 1 << 20 },
1013 { .tag = 'G', .mult = 1 << 30 },
1020 val = parse_tag_value(str, tags);
1021 if (val != (unsigned long) -1) {
1022 /* we got file size value */
1023 pages = PERF_ALIGN(val, page_size) / page_size;
1025 /* we got pages count value */
1027 pages = strtoul(str, &eptr, 10);
1032 if (pages == 0 && min == 0) {
1033 /* leave number of pages at 0 */
1034 } else if (!is_power_of_2(pages)) {
1035 /* round pages up to next power of 2 */
1036 pages = roundup_pow_of_two(pages);
1039 pr_info("rounding mmap pages size to %lu bytes (%lu pages)\n",
1040 pages * page_size, pages);
1049 int __perf_evlist__parse_mmap_pages(unsigned int *mmap_pages, const char *str)
1051 unsigned long max = UINT_MAX;
1054 if (max > SIZE_MAX / page_size)
1055 max = SIZE_MAX / page_size;
1057 pages = parse_pages_arg(str, 1, max);
1059 pr_err("Invalid argument for --mmap_pages/-m\n");
1063 *mmap_pages = pages;
1067 int perf_evlist__parse_mmap_pages(const struct option *opt, const char *str,
1068 int unset __maybe_unused)
1070 return __perf_evlist__parse_mmap_pages(opt->value, str);
1074 * perf_evlist__mmap_ex - Create mmaps to receive events.
1075 * @evlist: list of events
1076 * @pages: map length in pages
1077 * @overwrite: overwrite older events?
1078 * @auxtrace_pages - auxtrace map length in pages
1079 * @auxtrace_overwrite - overwrite older auxtrace data?
1081 * If @overwrite is %false the user needs to signal event consumption using
1082 * perf_mmap__write_tail(). Using perf_evlist__mmap_read() does this
1085 * Similarly, if @auxtrace_overwrite is %false the user needs to signal data
1086 * consumption using auxtrace_mmap__write_tail().
1088 * Return: %0 on success, negative error code otherwise.
1090 int perf_evlist__mmap_ex(struct perf_evlist *evlist, unsigned int pages,
1091 bool overwrite, unsigned int auxtrace_pages,
1092 bool auxtrace_overwrite)
1094 struct perf_evsel *evsel;
1095 const struct cpu_map *cpus = evlist->cpus;
1096 const struct thread_map *threads = evlist->threads;
1097 struct mmap_params mp = {
1098 .prot = PROT_READ | (overwrite ? 0 : PROT_WRITE),
1101 if (evlist->mmap == NULL && perf_evlist__alloc_mmap(evlist) < 0)
1104 if (evlist->pollfd.entries == NULL && perf_evlist__alloc_pollfd(evlist) < 0)
1107 evlist->overwrite = overwrite;
1108 evlist->mmap_len = perf_evlist__mmap_size(pages);
1109 pr_debug("mmap size %zuB\n", evlist->mmap_len);
1110 mp.mask = evlist->mmap_len - page_size - 1;
1112 auxtrace_mmap_params__init(&mp.auxtrace_mp, evlist->mmap_len,
1113 auxtrace_pages, auxtrace_overwrite);
1115 evlist__for_each(evlist, evsel) {
1116 if ((evsel->attr.read_format & PERF_FORMAT_ID) &&
1117 evsel->sample_id == NULL &&
1118 perf_evsel__alloc_id(evsel, cpu_map__nr(cpus), threads->nr) < 0)
1122 if (cpu_map__empty(cpus))
1123 return perf_evlist__mmap_per_thread(evlist, &mp);
1125 return perf_evlist__mmap_per_cpu(evlist, &mp);
1128 int perf_evlist__mmap(struct perf_evlist *evlist, unsigned int pages,
1131 return perf_evlist__mmap_ex(evlist, pages, overwrite, 0, false);
1134 int perf_evlist__create_maps(struct perf_evlist *evlist, struct target *target)
1136 struct cpu_map *cpus;
1137 struct thread_map *threads;
1139 threads = thread_map__new_str(target->pid, target->tid, target->uid);
1144 if (target__uses_dummy_map(target))
1145 cpus = cpu_map__dummy_new();
1147 cpus = cpu_map__new(target->cpu_list);
1150 goto out_delete_threads;
1152 evlist->has_user_cpus = !!target->cpu_list;
1154 perf_evlist__set_maps(evlist, cpus, threads);
1159 thread_map__put(threads);
1163 void perf_evlist__set_maps(struct perf_evlist *evlist, struct cpu_map *cpus,
1164 struct thread_map *threads)
1167 * Allow for the possibility that one or another of the maps isn't being
1168 * changed i.e. don't put it. Note we are assuming the maps that are
1169 * being applied are brand new and evlist is taking ownership of the
1170 * original reference count of 1. If that is not the case it is up to
1171 * the caller to increase the reference count.
1173 if (cpus != evlist->cpus) {
1174 cpu_map__put(evlist->cpus);
1175 evlist->cpus = cpus;
1178 if (threads != evlist->threads) {
1179 thread_map__put(evlist->threads);
1180 evlist->threads = threads;
1183 perf_evlist__propagate_maps(evlist);
1186 int perf_evlist__apply_filters(struct perf_evlist *evlist, struct perf_evsel **err_evsel)
1188 struct perf_evsel *evsel;
1190 const int ncpus = cpu_map__nr(evlist->cpus),
1191 nthreads = thread_map__nr(evlist->threads);
1193 evlist__for_each(evlist, evsel) {
1194 if (evsel->filter == NULL)
1198 * filters only work for tracepoint event, which doesn't have cpu limit.
1199 * So evlist and evsel should always be same.
1201 err = perf_evsel__apply_filter(evsel, ncpus, nthreads, evsel->filter);
1211 int perf_evlist__set_filter(struct perf_evlist *evlist, const char *filter)
1213 struct perf_evsel *evsel;
1216 evlist__for_each(evlist, evsel) {
1217 err = perf_evsel__set_filter(evsel, filter);
1225 int perf_evlist__set_filter_pids(struct perf_evlist *evlist, size_t npids, pid_t *pids)
1231 for (i = 0; i < npids; ++i) {
1233 if (asprintf(&filter, "common_pid != %d", pids[i]) < 0)
1238 if (asprintf(&tmp, "%s && common_pid != %d", filter, pids[i]) < 0)
1246 ret = perf_evlist__set_filter(evlist, filter);
1252 int perf_evlist__set_filter_pid(struct perf_evlist *evlist, pid_t pid)
1254 return perf_evlist__set_filter_pids(evlist, 1, &pid);
1257 bool perf_evlist__valid_sample_type(struct perf_evlist *evlist)
1259 struct perf_evsel *pos;
1261 if (evlist->nr_entries == 1)
1264 if (evlist->id_pos < 0 || evlist->is_pos < 0)
1267 evlist__for_each(evlist, pos) {
1268 if (pos->id_pos != evlist->id_pos ||
1269 pos->is_pos != evlist->is_pos)
1276 u64 __perf_evlist__combined_sample_type(struct perf_evlist *evlist)
1278 struct perf_evsel *evsel;
1280 if (evlist->combined_sample_type)
1281 return evlist->combined_sample_type;
1283 evlist__for_each(evlist, evsel)
1284 evlist->combined_sample_type |= evsel->attr.sample_type;
1286 return evlist->combined_sample_type;
1289 u64 perf_evlist__combined_sample_type(struct perf_evlist *evlist)
1291 evlist->combined_sample_type = 0;
1292 return __perf_evlist__combined_sample_type(evlist);
1295 u64 perf_evlist__combined_branch_type(struct perf_evlist *evlist)
1297 struct perf_evsel *evsel;
1298 u64 branch_type = 0;
1300 evlist__for_each(evlist, evsel)
1301 branch_type |= evsel->attr.branch_sample_type;
1305 bool perf_evlist__valid_read_format(struct perf_evlist *evlist)
1307 struct perf_evsel *first = perf_evlist__first(evlist), *pos = first;
1308 u64 read_format = first->attr.read_format;
1309 u64 sample_type = first->attr.sample_type;
1311 evlist__for_each(evlist, pos) {
1312 if (read_format != pos->attr.read_format)
1316 /* PERF_SAMPLE_READ imples PERF_FORMAT_ID. */
1317 if ((sample_type & PERF_SAMPLE_READ) &&
1318 !(read_format & PERF_FORMAT_ID)) {
1325 u64 perf_evlist__read_format(struct perf_evlist *evlist)
1327 struct perf_evsel *first = perf_evlist__first(evlist);
1328 return first->attr.read_format;
1331 u16 perf_evlist__id_hdr_size(struct perf_evlist *evlist)
1333 struct perf_evsel *first = perf_evlist__first(evlist);
1334 struct perf_sample *data;
1338 if (!first->attr.sample_id_all)
1341 sample_type = first->attr.sample_type;
1343 if (sample_type & PERF_SAMPLE_TID)
1344 size += sizeof(data->tid) * 2;
1346 if (sample_type & PERF_SAMPLE_TIME)
1347 size += sizeof(data->time);
1349 if (sample_type & PERF_SAMPLE_ID)
1350 size += sizeof(data->id);
1352 if (sample_type & PERF_SAMPLE_STREAM_ID)
1353 size += sizeof(data->stream_id);
1355 if (sample_type & PERF_SAMPLE_CPU)
1356 size += sizeof(data->cpu) * 2;
1358 if (sample_type & PERF_SAMPLE_IDENTIFIER)
1359 size += sizeof(data->id);
1364 bool perf_evlist__valid_sample_id_all(struct perf_evlist *evlist)
1366 struct perf_evsel *first = perf_evlist__first(evlist), *pos = first;
1368 evlist__for_each_continue(evlist, pos) {
1369 if (first->attr.sample_id_all != pos->attr.sample_id_all)
1376 bool perf_evlist__sample_id_all(struct perf_evlist *evlist)
1378 struct perf_evsel *first = perf_evlist__first(evlist);
1379 return first->attr.sample_id_all;
1382 void perf_evlist__set_selected(struct perf_evlist *evlist,
1383 struct perf_evsel *evsel)
1385 evlist->selected = evsel;
1388 void perf_evlist__close(struct perf_evlist *evlist)
1390 struct perf_evsel *evsel;
1391 int ncpus = cpu_map__nr(evlist->cpus);
1392 int nthreads = thread_map__nr(evlist->threads);
1395 evlist__for_each_reverse(evlist, evsel) {
1396 n = evsel->cpus ? evsel->cpus->nr : ncpus;
1397 perf_evsel__close(evsel, n, nthreads);
1401 static int perf_evlist__create_syswide_maps(struct perf_evlist *evlist)
1403 struct cpu_map *cpus;
1404 struct thread_map *threads;
1408 * Try reading /sys/devices/system/cpu/online to get
1411 * FIXME: -ENOMEM is the best we can do here, the cpu_map
1412 * code needs an overhaul to properly forward the
1413 * error, and we may not want to do that fallback to a
1414 * default cpu identity map :-\
1416 cpus = cpu_map__new(NULL);
1420 threads = thread_map__new_dummy();
1424 perf_evlist__set_maps(evlist, cpus, threads);
1432 int perf_evlist__open(struct perf_evlist *evlist)
1434 struct perf_evsel *evsel;
1438 * Default: one fd per CPU, all threads, aka systemwide
1439 * as sys_perf_event_open(cpu = -1, thread = -1) is EINVAL
1441 if (evlist->threads == NULL && evlist->cpus == NULL) {
1442 err = perf_evlist__create_syswide_maps(evlist);
1447 perf_evlist__update_id_pos(evlist);
1449 evlist__for_each(evlist, evsel) {
1450 err = perf_evsel__open(evsel, evlist->cpus, evlist->threads);
1457 perf_evlist__close(evlist);
1462 int perf_evlist__prepare_workload(struct perf_evlist *evlist, struct target *target,
1463 const char *argv[], bool pipe_output,
1464 void (*exec_error)(int signo, siginfo_t *info, void *ucontext))
1466 int child_ready_pipe[2], go_pipe[2];
1469 if (pipe(child_ready_pipe) < 0) {
1470 perror("failed to create 'ready' pipe");
1474 if (pipe(go_pipe) < 0) {
1475 perror("failed to create 'go' pipe");
1476 goto out_close_ready_pipe;
1479 evlist->workload.pid = fork();
1480 if (evlist->workload.pid < 0) {
1481 perror("failed to fork");
1482 goto out_close_pipes;
1485 if (!evlist->workload.pid) {
1491 signal(SIGTERM, SIG_DFL);
1493 close(child_ready_pipe[0]);
1495 fcntl(go_pipe[0], F_SETFD, FD_CLOEXEC);
1498 * Tell the parent we're ready to go
1500 close(child_ready_pipe[1]);
1503 * Wait until the parent tells us to go.
1505 ret = read(go_pipe[0], &bf, 1);
1507 * The parent will ask for the execvp() to be performed by
1508 * writing exactly one byte, in workload.cork_fd, usually via
1509 * perf_evlist__start_workload().
1511 * For cancelling the workload without actually running it,
1512 * the parent will just close workload.cork_fd, without writing
1513 * anything, i.e. read will return zero and we just exit()
1518 perror("unable to read pipe");
1522 execvp(argv[0], (char **)argv);
1527 val.sival_int = errno;
1528 if (sigqueue(getppid(), SIGUSR1, val))
1536 struct sigaction act = {
1537 .sa_flags = SA_SIGINFO,
1538 .sa_sigaction = exec_error,
1540 sigaction(SIGUSR1, &act, NULL);
1543 if (target__none(target)) {
1544 if (evlist->threads == NULL) {
1545 fprintf(stderr, "FATAL: evlist->threads need to be set at this point (%s:%d).\n",
1546 __func__, __LINE__);
1547 goto out_close_pipes;
1549 thread_map__set_pid(evlist->threads, 0, evlist->workload.pid);
1552 close(child_ready_pipe[1]);
1555 * wait for child to settle
1557 if (read(child_ready_pipe[0], &bf, 1) == -1) {
1558 perror("unable to read pipe");
1559 goto out_close_pipes;
1562 fcntl(go_pipe[1], F_SETFD, FD_CLOEXEC);
1563 evlist->workload.cork_fd = go_pipe[1];
1564 close(child_ready_pipe[0]);
1570 out_close_ready_pipe:
1571 close(child_ready_pipe[0]);
1572 close(child_ready_pipe[1]);
1576 int perf_evlist__start_workload(struct perf_evlist *evlist)
1578 if (evlist->workload.cork_fd > 0) {
1582 * Remove the cork, let it rip!
1584 ret = write(evlist->workload.cork_fd, &bf, 1);
1586 perror("enable to write to pipe");
1588 close(evlist->workload.cork_fd);
1595 int perf_evlist__parse_sample(struct perf_evlist *evlist, union perf_event *event,
1596 struct perf_sample *sample)
1598 struct perf_evsel *evsel = perf_evlist__event2evsel(evlist, event);
1602 return perf_evsel__parse_sample(evsel, event, sample);
1605 size_t perf_evlist__fprintf(struct perf_evlist *evlist, FILE *fp)
1607 struct perf_evsel *evsel;
1610 evlist__for_each(evlist, evsel) {
1611 printed += fprintf(fp, "%s%s", evsel->idx ? ", " : "",
1612 perf_evsel__name(evsel));
1615 return printed + fprintf(fp, "\n");
1618 int perf_evlist__strerror_open(struct perf_evlist *evlist __maybe_unused,
1619 int err, char *buf, size_t size)
1622 char sbuf[STRERR_BUFSIZE], *emsg = strerror_r(err, sbuf, sizeof(sbuf));
1627 printed = scnprintf(buf, size,
1629 "Hint:\tCheck /proc/sys/kernel/perf_event_paranoid setting.", emsg);
1631 value = perf_event_paranoid();
1633 printed += scnprintf(buf + printed, size - printed, "\nHint:\t");
1636 printed += scnprintf(buf + printed, size - printed,
1637 "For your workloads it needs to be <= 1\nHint:\t");
1639 printed += scnprintf(buf + printed, size - printed,
1640 "For system wide tracing it needs to be set to -1.\n");
1642 printed += scnprintf(buf + printed, size - printed,
1643 "Hint:\tTry: 'sudo sh -c \"echo -1 > /proc/sys/kernel/perf_event_paranoid\"'\n"
1644 "Hint:\tThe current value is %d.", value);
1647 scnprintf(buf, size, "%s", emsg);
1654 int perf_evlist__strerror_mmap(struct perf_evlist *evlist, int err, char *buf, size_t size)
1656 char sbuf[STRERR_BUFSIZE], *emsg = strerror_r(err, sbuf, sizeof(sbuf));
1657 int pages_attempted = evlist->mmap_len / 1024, pages_max_per_user, printed = 0;
1661 sysctl__read_int("kernel/perf_event_mlock_kb", &pages_max_per_user);
1662 printed += scnprintf(buf + printed, size - printed,
1664 "Hint:\tCheck /proc/sys/kernel/perf_event_mlock_kb (%d kB) setting.\n"
1665 "Hint:\tTried using %zd kB.\n",
1666 emsg, pages_max_per_user, pages_attempted);
1668 if (pages_attempted >= pages_max_per_user) {
1669 printed += scnprintf(buf + printed, size - printed,
1670 "Hint:\tTry 'sudo sh -c \"echo %d > /proc/sys/kernel/perf_event_mlock_kb\"', or\n",
1671 pages_max_per_user + pages_attempted);
1674 printed += scnprintf(buf + printed, size - printed,
1675 "Hint:\tTry using a smaller -m/--mmap-pages value.");
1678 scnprintf(buf, size, "%s", emsg);
1685 void perf_evlist__to_front(struct perf_evlist *evlist,
1686 struct perf_evsel *move_evsel)
1688 struct perf_evsel *evsel, *n;
1691 if (move_evsel == perf_evlist__first(evlist))
1694 evlist__for_each_safe(evlist, n, evsel) {
1695 if (evsel->leader == move_evsel->leader)
1696 list_move_tail(&evsel->node, &move);
1699 list_splice(&move, &evlist->entries);
1702 void perf_evlist__set_tracking_event(struct perf_evlist *evlist,
1703 struct perf_evsel *tracking_evsel)
1705 struct perf_evsel *evsel;
1707 if (tracking_evsel->tracking)
1710 evlist__for_each(evlist, evsel) {
1711 if (evsel != tracking_evsel)
1712 evsel->tracking = false;
1715 tracking_evsel->tracking = true;