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/debugfs.h>
11 #include <api/fs/fs.h>
14 #include "thread_map.h"
21 #include "parse-events.h"
22 #include "parse-options.h"
26 #include <linux/bitops.h>
27 #include <linux/hash.h>
28 #include <linux/log2.h>
30 static void perf_evlist__mmap_put(struct perf_evlist *evlist, int idx);
31 static void __perf_evlist__munmap(struct perf_evlist *evlist, int idx);
33 #define FD(e, x, y) (*(int *)xyarray__entry(e->fd, x, y))
34 #define SID(e, x, y) xyarray__entry(e->sample_id, x, y)
36 void perf_evlist__init(struct perf_evlist *evlist, struct cpu_map *cpus,
37 struct thread_map *threads)
41 for (i = 0; i < PERF_EVLIST__HLIST_SIZE; ++i)
42 INIT_HLIST_HEAD(&evlist->heads[i]);
43 INIT_LIST_HEAD(&evlist->entries);
44 perf_evlist__set_maps(evlist, cpus, threads);
45 fdarray__init(&evlist->pollfd, 64);
46 evlist->workload.pid = -1;
49 struct perf_evlist *perf_evlist__new(void)
51 struct perf_evlist *evlist = zalloc(sizeof(*evlist));
54 perf_evlist__init(evlist, NULL, NULL);
59 struct perf_evlist *perf_evlist__new_default(void)
61 struct perf_evlist *evlist = perf_evlist__new();
63 if (evlist && perf_evlist__add_default(evlist)) {
64 perf_evlist__delete(evlist);
72 * perf_evlist__set_id_pos - set the positions of event ids.
73 * @evlist: selected event list
75 * Events with compatible sample types all have the same id_pos
76 * and is_pos. For convenience, put a copy on evlist.
78 void perf_evlist__set_id_pos(struct perf_evlist *evlist)
80 struct perf_evsel *first = perf_evlist__first(evlist);
82 evlist->id_pos = first->id_pos;
83 evlist->is_pos = first->is_pos;
86 static void perf_evlist__update_id_pos(struct perf_evlist *evlist)
88 struct perf_evsel *evsel;
90 evlist__for_each(evlist, evsel)
91 perf_evsel__calc_id_pos(evsel);
93 perf_evlist__set_id_pos(evlist);
96 static void perf_evlist__purge(struct perf_evlist *evlist)
98 struct perf_evsel *pos, *n;
100 evlist__for_each_safe(evlist, n, pos) {
101 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__delete(evlist->cpus);
119 thread_map__delete(evlist->threads);
121 evlist->threads = NULL;
122 perf_evlist__purge(evlist);
123 perf_evlist__exit(evlist);
127 void perf_evlist__add(struct perf_evlist *evlist, struct perf_evsel *entry)
129 list_add_tail(&entry->node, &evlist->entries);
130 entry->idx = evlist->nr_entries;
131 entry->tracking = !entry->idx;
133 if (!evlist->nr_entries++)
134 perf_evlist__set_id_pos(evlist);
137 void perf_evlist__splice_list_tail(struct perf_evlist *evlist,
138 struct list_head *list,
141 bool set_id_pos = !evlist->nr_entries;
143 list_splice_tail(list, &evlist->entries);
144 evlist->nr_entries += nr_entries;
146 perf_evlist__set_id_pos(evlist);
149 void __perf_evlist__set_leader(struct list_head *list)
151 struct perf_evsel *evsel, *leader;
153 leader = list_entry(list->next, struct perf_evsel, node);
154 evsel = list_entry(list->prev, struct perf_evsel, node);
156 leader->nr_members = evsel->idx - leader->idx + 1;
158 __evlist__for_each(list, evsel) {
159 evsel->leader = leader;
163 void perf_evlist__set_leader(struct perf_evlist *evlist)
165 if (evlist->nr_entries) {
166 evlist->nr_groups = evlist->nr_entries > 1 ? 1 : 0;
167 __perf_evlist__set_leader(&evlist->entries);
171 int perf_evlist__add_default(struct perf_evlist *evlist)
173 struct perf_event_attr attr = {
174 .type = PERF_TYPE_HARDWARE,
175 .config = PERF_COUNT_HW_CPU_CYCLES,
177 struct perf_evsel *evsel;
179 event_attr_init(&attr);
181 evsel = perf_evsel__new(&attr);
185 /* use strdup() because free(evsel) assumes name is allocated */
186 evsel->name = strdup("cycles");
190 perf_evlist__add(evlist, evsel);
193 perf_evsel__delete(evsel);
198 static int perf_evlist__add_attrs(struct perf_evlist *evlist,
199 struct perf_event_attr *attrs, size_t nr_attrs)
201 struct perf_evsel *evsel, *n;
205 for (i = 0; i < nr_attrs; i++) {
206 evsel = perf_evsel__new_idx(attrs + i, evlist->nr_entries + i);
208 goto out_delete_partial_list;
209 list_add_tail(&evsel->node, &head);
212 perf_evlist__splice_list_tail(evlist, &head, nr_attrs);
216 out_delete_partial_list:
217 __evlist__for_each_safe(&head, n, evsel)
218 perf_evsel__delete(evsel);
222 int __perf_evlist__add_default_attrs(struct perf_evlist *evlist,
223 struct perf_event_attr *attrs, size_t nr_attrs)
227 for (i = 0; i < nr_attrs; i++)
228 event_attr_init(attrs + i);
230 return perf_evlist__add_attrs(evlist, attrs, nr_attrs);
234 perf_evlist__find_tracepoint_by_id(struct perf_evlist *evlist, int id)
236 struct perf_evsel *evsel;
238 evlist__for_each(evlist, evsel) {
239 if (evsel->attr.type == PERF_TYPE_TRACEPOINT &&
240 (int)evsel->attr.config == id)
248 perf_evlist__find_tracepoint_by_name(struct perf_evlist *evlist,
251 struct perf_evsel *evsel;
253 evlist__for_each(evlist, evsel) {
254 if ((evsel->attr.type == PERF_TYPE_TRACEPOINT) &&
255 (strcmp(evsel->name, name) == 0))
262 int perf_evlist__add_newtp(struct perf_evlist *evlist,
263 const char *sys, const char *name, void *handler)
265 struct perf_evsel *evsel = perf_evsel__newtp(sys, name);
270 evsel->handler = handler;
271 perf_evlist__add(evlist, evsel);
275 static int perf_evlist__nr_threads(struct perf_evlist *evlist,
276 struct perf_evsel *evsel)
278 if (evsel->system_wide)
281 return thread_map__nr(evlist->threads);
284 void perf_evlist__disable(struct perf_evlist *evlist)
287 struct perf_evsel *pos;
288 int nr_cpus = cpu_map__nr(evlist->cpus);
291 for (cpu = 0; cpu < nr_cpus; cpu++) {
292 evlist__for_each(evlist, pos) {
293 if (!perf_evsel__is_group_leader(pos) || !pos->fd)
295 nr_threads = perf_evlist__nr_threads(evlist, pos);
296 for (thread = 0; thread < nr_threads; thread++)
297 ioctl(FD(pos, cpu, thread),
298 PERF_EVENT_IOC_DISABLE, 0);
303 void perf_evlist__enable(struct perf_evlist *evlist)
306 struct perf_evsel *pos;
307 int nr_cpus = cpu_map__nr(evlist->cpus);
310 for (cpu = 0; cpu < nr_cpus; cpu++) {
311 evlist__for_each(evlist, pos) {
312 if (!perf_evsel__is_group_leader(pos) || !pos->fd)
314 nr_threads = perf_evlist__nr_threads(evlist, pos);
315 for (thread = 0; thread < nr_threads; thread++)
316 ioctl(FD(pos, cpu, thread),
317 PERF_EVENT_IOC_ENABLE, 0);
322 int perf_evlist__disable_event(struct perf_evlist *evlist,
323 struct perf_evsel *evsel)
325 int cpu, thread, err;
326 int nr_cpus = cpu_map__nr(evlist->cpus);
327 int nr_threads = perf_evlist__nr_threads(evlist, evsel);
332 for (cpu = 0; cpu < nr_cpus; cpu++) {
333 for (thread = 0; thread < nr_threads; thread++) {
334 err = ioctl(FD(evsel, cpu, thread),
335 PERF_EVENT_IOC_DISABLE, 0);
343 int perf_evlist__enable_event(struct perf_evlist *evlist,
344 struct perf_evsel *evsel)
346 int cpu, thread, err;
347 int nr_cpus = cpu_map__nr(evlist->cpus);
348 int nr_threads = perf_evlist__nr_threads(evlist, evsel);
353 for (cpu = 0; cpu < nr_cpus; cpu++) {
354 for (thread = 0; thread < nr_threads; thread++) {
355 err = ioctl(FD(evsel, cpu, thread),
356 PERF_EVENT_IOC_ENABLE, 0);
364 static int perf_evlist__enable_event_cpu(struct perf_evlist *evlist,
365 struct perf_evsel *evsel, int cpu)
368 int nr_threads = perf_evlist__nr_threads(evlist, evsel);
373 for (thread = 0; thread < nr_threads; thread++) {
374 err = ioctl(FD(evsel, cpu, thread),
375 PERF_EVENT_IOC_ENABLE, 0);
382 static int perf_evlist__enable_event_thread(struct perf_evlist *evlist,
383 struct perf_evsel *evsel,
387 int nr_cpus = cpu_map__nr(evlist->cpus);
392 for (cpu = 0; cpu < nr_cpus; cpu++) {
393 err = ioctl(FD(evsel, cpu, thread), PERF_EVENT_IOC_ENABLE, 0);
400 int perf_evlist__enable_event_idx(struct perf_evlist *evlist,
401 struct perf_evsel *evsel, int idx)
403 bool per_cpu_mmaps = !cpu_map__empty(evlist->cpus);
406 return perf_evlist__enable_event_cpu(evlist, evsel, idx);
408 return perf_evlist__enable_event_thread(evlist, evsel, idx);
411 int perf_evlist__alloc_pollfd(struct perf_evlist *evlist)
413 int nr_cpus = cpu_map__nr(evlist->cpus);
414 int nr_threads = thread_map__nr(evlist->threads);
416 struct perf_evsel *evsel;
418 evlist__for_each(evlist, evsel) {
419 if (evsel->system_wide)
422 nfds += nr_cpus * nr_threads;
425 if (fdarray__available_entries(&evlist->pollfd) < nfds &&
426 fdarray__grow(&evlist->pollfd, nfds) < 0)
432 static int __perf_evlist__add_pollfd(struct perf_evlist *evlist, int fd, int idx)
434 int pos = fdarray__add(&evlist->pollfd, fd, POLLIN | POLLERR | POLLHUP);
436 * Save the idx so that when we filter out fds POLLHUP'ed we can
437 * close the associated evlist->mmap[] entry.
440 evlist->pollfd.priv[pos].idx = idx;
442 fcntl(fd, F_SETFL, O_NONBLOCK);
448 int perf_evlist__add_pollfd(struct perf_evlist *evlist, int fd)
450 return __perf_evlist__add_pollfd(evlist, fd, -1);
453 static void perf_evlist__munmap_filtered(struct fdarray *fda, int fd)
455 struct perf_evlist *evlist = container_of(fda, struct perf_evlist, pollfd);
457 perf_evlist__mmap_put(evlist, fda->priv[fd].idx);
460 int perf_evlist__filter_pollfd(struct perf_evlist *evlist, short revents_and_mask)
462 return fdarray__filter(&evlist->pollfd, revents_and_mask,
463 perf_evlist__munmap_filtered);
466 int perf_evlist__poll(struct perf_evlist *evlist, int timeout)
468 return fdarray__poll(&evlist->pollfd, timeout);
471 static void perf_evlist__id_hash(struct perf_evlist *evlist,
472 struct perf_evsel *evsel,
473 int cpu, int thread, u64 id)
476 struct perf_sample_id *sid = SID(evsel, cpu, thread);
480 hash = hash_64(sid->id, PERF_EVLIST__HLIST_BITS);
481 hlist_add_head(&sid->node, &evlist->heads[hash]);
484 void perf_evlist__id_add(struct perf_evlist *evlist, struct perf_evsel *evsel,
485 int cpu, int thread, u64 id)
487 perf_evlist__id_hash(evlist, evsel, cpu, thread, id);
488 evsel->id[evsel->ids++] = id;
491 static int perf_evlist__id_add_fd(struct perf_evlist *evlist,
492 struct perf_evsel *evsel,
493 int cpu, int thread, int fd)
495 u64 read_data[4] = { 0, };
496 int id_idx = 1; /* The first entry is the counter value */
500 ret = ioctl(fd, PERF_EVENT_IOC_ID, &id);
507 /* Legacy way to get event id.. All hail to old kernels! */
510 * This way does not work with group format read, so bail
513 if (perf_evlist__read_format(evlist) & PERF_FORMAT_GROUP)
516 if (!(evsel->attr.read_format & PERF_FORMAT_ID) ||
517 read(fd, &read_data, sizeof(read_data)) == -1)
520 if (evsel->attr.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
522 if (evsel->attr.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
525 id = read_data[id_idx];
528 perf_evlist__id_add(evlist, evsel, cpu, thread, id);
532 static void perf_evlist__set_sid_idx(struct perf_evlist *evlist,
533 struct perf_evsel *evsel, int idx, int cpu,
536 struct perf_sample_id *sid = SID(evsel, cpu, thread);
538 if (evlist->cpus && cpu >= 0)
539 sid->cpu = evlist->cpus->map[cpu];
542 if (!evsel->system_wide && evlist->threads && thread >= 0)
543 sid->tid = evlist->threads->map[thread];
548 struct perf_sample_id *perf_evlist__id2sid(struct perf_evlist *evlist, u64 id)
550 struct hlist_head *head;
551 struct perf_sample_id *sid;
554 hash = hash_64(id, PERF_EVLIST__HLIST_BITS);
555 head = &evlist->heads[hash];
557 hlist_for_each_entry(sid, head, node)
564 struct perf_evsel *perf_evlist__id2evsel(struct perf_evlist *evlist, u64 id)
566 struct perf_sample_id *sid;
568 if (evlist->nr_entries == 1)
569 return perf_evlist__first(evlist);
571 sid = perf_evlist__id2sid(evlist, id);
575 if (!perf_evlist__sample_id_all(evlist))
576 return perf_evlist__first(evlist);
581 static int perf_evlist__event2id(struct perf_evlist *evlist,
582 union perf_event *event, u64 *id)
584 const u64 *array = event->sample.array;
587 n = (event->header.size - sizeof(event->header)) >> 3;
589 if (event->header.type == PERF_RECORD_SAMPLE) {
590 if (evlist->id_pos >= n)
592 *id = array[evlist->id_pos];
594 if (evlist->is_pos > n)
602 static struct perf_evsel *perf_evlist__event2evsel(struct perf_evlist *evlist,
603 union perf_event *event)
605 struct perf_evsel *first = perf_evlist__first(evlist);
606 struct hlist_head *head;
607 struct perf_sample_id *sid;
611 if (evlist->nr_entries == 1)
614 if (!first->attr.sample_id_all &&
615 event->header.type != PERF_RECORD_SAMPLE)
618 if (perf_evlist__event2id(evlist, event, &id))
621 /* Synthesized events have an id of zero */
625 hash = hash_64(id, PERF_EVLIST__HLIST_BITS);
626 head = &evlist->heads[hash];
628 hlist_for_each_entry(sid, head, node) {
635 union perf_event *perf_evlist__mmap_read(struct perf_evlist *evlist, int idx)
637 struct perf_mmap *md = &evlist->mmap[idx];
638 unsigned int head = perf_mmap__read_head(md);
639 unsigned int old = md->prev;
640 unsigned char *data = md->base + page_size;
641 union perf_event *event = NULL;
643 if (evlist->overwrite) {
645 * If we're further behind than half the buffer, there's a chance
646 * the writer will bite our tail and mess up the samples under us.
648 * If we somehow ended up ahead of the head, we got messed up.
650 * In either case, truncate and restart at head.
652 int diff = head - old;
653 if (diff > md->mask / 2 || diff < 0) {
654 fprintf(stderr, "WARNING: failed to keep up with mmap data.\n");
657 * head points to a known good entry, start there.
666 event = (union perf_event *)&data[old & md->mask];
667 size = event->header.size;
670 * Event straddles the mmap boundary -- header should always
671 * be inside due to u64 alignment of output.
673 if ((old & md->mask) + size != ((old + size) & md->mask)) {
674 unsigned int offset = old;
675 unsigned int len = min(sizeof(*event), size), cpy;
676 void *dst = md->event_copy;
679 cpy = min(md->mask + 1 - (offset & md->mask), len);
680 memcpy(dst, &data[offset & md->mask], cpy);
686 event = (union perf_event *) md->event_copy;
697 static bool perf_mmap__empty(struct perf_mmap *md)
699 return perf_mmap__read_head(md) != md->prev;
702 static void perf_evlist__mmap_get(struct perf_evlist *evlist, int idx)
704 ++evlist->mmap[idx].refcnt;
707 static void perf_evlist__mmap_put(struct perf_evlist *evlist, int idx)
709 BUG_ON(evlist->mmap[idx].refcnt == 0);
711 if (--evlist->mmap[idx].refcnt == 0)
712 __perf_evlist__munmap(evlist, idx);
715 void perf_evlist__mmap_consume(struct perf_evlist *evlist, int idx)
717 struct perf_mmap *md = &evlist->mmap[idx];
719 if (!evlist->overwrite) {
720 unsigned int old = md->prev;
722 perf_mmap__write_tail(md, old);
725 if (md->refcnt == 1 && perf_mmap__empty(md))
726 perf_evlist__mmap_put(evlist, idx);
729 static void __perf_evlist__munmap(struct perf_evlist *evlist, int idx)
731 if (evlist->mmap[idx].base != NULL) {
732 munmap(evlist->mmap[idx].base, evlist->mmap_len);
733 evlist->mmap[idx].base = NULL;
734 evlist->mmap[idx].refcnt = 0;
738 void perf_evlist__munmap(struct perf_evlist *evlist)
742 if (evlist->mmap == NULL)
745 for (i = 0; i < evlist->nr_mmaps; i++)
746 __perf_evlist__munmap(evlist, i);
748 zfree(&evlist->mmap);
751 static int perf_evlist__alloc_mmap(struct perf_evlist *evlist)
753 evlist->nr_mmaps = cpu_map__nr(evlist->cpus);
754 if (cpu_map__empty(evlist->cpus))
755 evlist->nr_mmaps = thread_map__nr(evlist->threads);
756 evlist->mmap = zalloc(evlist->nr_mmaps * sizeof(struct perf_mmap));
757 return evlist->mmap != NULL ? 0 : -ENOMEM;
765 static int __perf_evlist__mmap(struct perf_evlist *evlist, int idx,
766 struct mmap_params *mp, int fd)
769 * The last one will be done at perf_evlist__mmap_consume(), so that we
770 * make sure we don't prevent tools from consuming every last event in
773 * I.e. we can get the POLLHUP meaning that the fd doesn't exist
774 * anymore, but the last events for it are still in the ring buffer,
775 * waiting to be consumed.
777 * Tools can chose to ignore this at their own discretion, but the
778 * evlist layer can't just drop it when filtering events in
779 * perf_evlist__filter_pollfd().
781 evlist->mmap[idx].refcnt = 2;
782 evlist->mmap[idx].prev = 0;
783 evlist->mmap[idx].mask = mp->mask;
784 evlist->mmap[idx].base = mmap(NULL, evlist->mmap_len, mp->prot,
786 if (evlist->mmap[idx].base == MAP_FAILED) {
787 pr_debug2("failed to mmap perf event ring buffer, error %d\n",
789 evlist->mmap[idx].base = NULL;
796 static int perf_evlist__mmap_per_evsel(struct perf_evlist *evlist, int idx,
797 struct mmap_params *mp, int cpu,
798 int thread, int *output)
800 struct perf_evsel *evsel;
802 evlist__for_each(evlist, evsel) {
805 if (evsel->system_wide && thread)
808 fd = FD(evsel, cpu, thread);
812 if (__perf_evlist__mmap(evlist, idx, mp, *output) < 0)
815 if (ioctl(fd, PERF_EVENT_IOC_SET_OUTPUT, *output) != 0)
818 perf_evlist__mmap_get(evlist, idx);
822 * The system_wide flag causes a selected event to be opened
823 * always without a pid. Consequently it will never get a
824 * POLLHUP, but it is used for tracking in combination with
825 * other events, so it should not need to be polled anyway.
826 * Therefore don't add it for polling.
828 if (!evsel->system_wide &&
829 __perf_evlist__add_pollfd(evlist, fd, idx) < 0) {
830 perf_evlist__mmap_put(evlist, idx);
834 if (evsel->attr.read_format & PERF_FORMAT_ID) {
835 if (perf_evlist__id_add_fd(evlist, evsel, cpu, thread,
838 perf_evlist__set_sid_idx(evlist, evsel, idx, cpu,
846 static int perf_evlist__mmap_per_cpu(struct perf_evlist *evlist,
847 struct mmap_params *mp)
850 int nr_cpus = cpu_map__nr(evlist->cpus);
851 int nr_threads = thread_map__nr(evlist->threads);
853 pr_debug2("perf event ring buffer mmapped per cpu\n");
854 for (cpu = 0; cpu < nr_cpus; cpu++) {
857 for (thread = 0; thread < nr_threads; thread++) {
858 if (perf_evlist__mmap_per_evsel(evlist, cpu, mp, cpu,
867 for (cpu = 0; cpu < nr_cpus; cpu++)
868 __perf_evlist__munmap(evlist, cpu);
872 static int perf_evlist__mmap_per_thread(struct perf_evlist *evlist,
873 struct mmap_params *mp)
876 int nr_threads = thread_map__nr(evlist->threads);
878 pr_debug2("perf event ring buffer mmapped per thread\n");
879 for (thread = 0; thread < nr_threads; thread++) {
882 if (perf_evlist__mmap_per_evsel(evlist, thread, mp, 0, thread,
890 for (thread = 0; thread < nr_threads; thread++)
891 __perf_evlist__munmap(evlist, thread);
895 static size_t perf_evlist__mmap_size(unsigned long pages)
897 if (pages == UINT_MAX) {
900 if (sysctl__read_int("kernel/perf_event_mlock_kb", &max) < 0) {
902 * Pick a once upon a time good value, i.e. things look
903 * strange since we can't read a sysctl value, but lets not
908 max -= (page_size / 1024);
911 pages = (max * 1024) / page_size;
912 if (!is_power_of_2(pages))
913 pages = rounddown_pow_of_two(pages);
914 } else if (!is_power_of_2(pages))
917 return (pages + 1) * page_size;
920 static long parse_pages_arg(const char *str, unsigned long min,
923 unsigned long pages, val;
924 static struct parse_tag tags[] = {
925 { .tag = 'B', .mult = 1 },
926 { .tag = 'K', .mult = 1 << 10 },
927 { .tag = 'M', .mult = 1 << 20 },
928 { .tag = 'G', .mult = 1 << 30 },
935 val = parse_tag_value(str, tags);
936 if (val != (unsigned long) -1) {
937 /* we got file size value */
938 pages = PERF_ALIGN(val, page_size) / page_size;
940 /* we got pages count value */
942 pages = strtoul(str, &eptr, 10);
947 if (pages == 0 && min == 0) {
948 /* leave number of pages at 0 */
949 } else if (!is_power_of_2(pages)) {
950 /* round pages up to next power of 2 */
951 pages = roundup_pow_of_two(pages);
954 pr_info("rounding mmap pages size to %lu bytes (%lu pages)\n",
955 pages * page_size, pages);
964 int perf_evlist__parse_mmap_pages(const struct option *opt, const char *str,
965 int unset __maybe_unused)
967 unsigned int *mmap_pages = opt->value;
968 unsigned long max = UINT_MAX;
971 if (max > SIZE_MAX / page_size)
972 max = SIZE_MAX / page_size;
974 pages = parse_pages_arg(str, 1, max);
976 pr_err("Invalid argument for --mmap_pages/-m\n");
985 * perf_evlist__mmap - Create mmaps to receive events.
986 * @evlist: list of events
987 * @pages: map length in pages
988 * @overwrite: overwrite older events?
990 * If @overwrite is %false the user needs to signal event consumption using
991 * perf_mmap__write_tail(). Using perf_evlist__mmap_read() does this
994 * Return: %0 on success, negative error code otherwise.
996 int perf_evlist__mmap(struct perf_evlist *evlist, unsigned int pages,
999 struct perf_evsel *evsel;
1000 const struct cpu_map *cpus = evlist->cpus;
1001 const struct thread_map *threads = evlist->threads;
1002 struct mmap_params mp = {
1003 .prot = PROT_READ | (overwrite ? 0 : PROT_WRITE),
1006 if (evlist->mmap == NULL && perf_evlist__alloc_mmap(evlist) < 0)
1009 if (evlist->pollfd.entries == NULL && perf_evlist__alloc_pollfd(evlist) < 0)
1012 evlist->overwrite = overwrite;
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 evlist__for_each(evlist, evsel) {
1018 if ((evsel->attr.read_format & PERF_FORMAT_ID) &&
1019 evsel->sample_id == NULL &&
1020 perf_evsel__alloc_id(evsel, cpu_map__nr(cpus), threads->nr) < 0)
1024 if (cpu_map__empty(cpus))
1025 return perf_evlist__mmap_per_thread(evlist, &mp);
1027 return perf_evlist__mmap_per_cpu(evlist, &mp);
1030 int perf_evlist__create_maps(struct perf_evlist *evlist, struct target *target)
1032 evlist->threads = thread_map__new_str(target->pid, target->tid,
1035 if (evlist->threads == NULL)
1038 if (target__uses_dummy_map(target))
1039 evlist->cpus = cpu_map__dummy_new();
1041 evlist->cpus = cpu_map__new(target->cpu_list);
1043 if (evlist->cpus == NULL)
1044 goto out_delete_threads;
1049 thread_map__delete(evlist->threads);
1050 evlist->threads = NULL;
1054 int perf_evlist__apply_filters(struct perf_evlist *evlist)
1056 struct perf_evsel *evsel;
1058 const int ncpus = cpu_map__nr(evlist->cpus),
1059 nthreads = thread_map__nr(evlist->threads);
1061 evlist__for_each(evlist, evsel) {
1062 if (evsel->filter == NULL)
1065 err = perf_evsel__set_filter(evsel, ncpus, nthreads, evsel->filter);
1073 int perf_evlist__set_filter(struct perf_evlist *evlist, const char *filter)
1075 struct perf_evsel *evsel;
1077 const int ncpus = cpu_map__nr(evlist->cpus),
1078 nthreads = thread_map__nr(evlist->threads);
1080 evlist__for_each(evlist, evsel) {
1081 err = perf_evsel__set_filter(evsel, ncpus, nthreads, filter);
1089 bool perf_evlist__valid_sample_type(struct perf_evlist *evlist)
1091 struct perf_evsel *pos;
1093 if (evlist->nr_entries == 1)
1096 if (evlist->id_pos < 0 || evlist->is_pos < 0)
1099 evlist__for_each(evlist, pos) {
1100 if (pos->id_pos != evlist->id_pos ||
1101 pos->is_pos != evlist->is_pos)
1108 u64 __perf_evlist__combined_sample_type(struct perf_evlist *evlist)
1110 struct perf_evsel *evsel;
1112 if (evlist->combined_sample_type)
1113 return evlist->combined_sample_type;
1115 evlist__for_each(evlist, evsel)
1116 evlist->combined_sample_type |= evsel->attr.sample_type;
1118 return evlist->combined_sample_type;
1121 u64 perf_evlist__combined_sample_type(struct perf_evlist *evlist)
1123 evlist->combined_sample_type = 0;
1124 return __perf_evlist__combined_sample_type(evlist);
1127 bool perf_evlist__valid_read_format(struct perf_evlist *evlist)
1129 struct perf_evsel *first = perf_evlist__first(evlist), *pos = first;
1130 u64 read_format = first->attr.read_format;
1131 u64 sample_type = first->attr.sample_type;
1133 evlist__for_each(evlist, pos) {
1134 if (read_format != pos->attr.read_format)
1138 /* PERF_SAMPLE_READ imples PERF_FORMAT_ID. */
1139 if ((sample_type & PERF_SAMPLE_READ) &&
1140 !(read_format & PERF_FORMAT_ID)) {
1147 u64 perf_evlist__read_format(struct perf_evlist *evlist)
1149 struct perf_evsel *first = perf_evlist__first(evlist);
1150 return first->attr.read_format;
1153 u16 perf_evlist__id_hdr_size(struct perf_evlist *evlist)
1155 struct perf_evsel *first = perf_evlist__first(evlist);
1156 struct perf_sample *data;
1160 if (!first->attr.sample_id_all)
1163 sample_type = first->attr.sample_type;
1165 if (sample_type & PERF_SAMPLE_TID)
1166 size += sizeof(data->tid) * 2;
1168 if (sample_type & PERF_SAMPLE_TIME)
1169 size += sizeof(data->time);
1171 if (sample_type & PERF_SAMPLE_ID)
1172 size += sizeof(data->id);
1174 if (sample_type & PERF_SAMPLE_STREAM_ID)
1175 size += sizeof(data->stream_id);
1177 if (sample_type & PERF_SAMPLE_CPU)
1178 size += sizeof(data->cpu) * 2;
1180 if (sample_type & PERF_SAMPLE_IDENTIFIER)
1181 size += sizeof(data->id);
1186 bool perf_evlist__valid_sample_id_all(struct perf_evlist *evlist)
1188 struct perf_evsel *first = perf_evlist__first(evlist), *pos = first;
1190 evlist__for_each_continue(evlist, pos) {
1191 if (first->attr.sample_id_all != pos->attr.sample_id_all)
1198 bool perf_evlist__sample_id_all(struct perf_evlist *evlist)
1200 struct perf_evsel *first = perf_evlist__first(evlist);
1201 return first->attr.sample_id_all;
1204 void perf_evlist__set_selected(struct perf_evlist *evlist,
1205 struct perf_evsel *evsel)
1207 evlist->selected = evsel;
1210 void perf_evlist__close(struct perf_evlist *evlist)
1212 struct perf_evsel *evsel;
1213 int ncpus = cpu_map__nr(evlist->cpus);
1214 int nthreads = thread_map__nr(evlist->threads);
1217 evlist__for_each_reverse(evlist, evsel) {
1218 n = evsel->cpus ? evsel->cpus->nr : ncpus;
1219 perf_evsel__close(evsel, n, nthreads);
1223 static int perf_evlist__create_syswide_maps(struct perf_evlist *evlist)
1228 * Try reading /sys/devices/system/cpu/online to get
1231 * FIXME: -ENOMEM is the best we can do here, the cpu_map
1232 * code needs an overhaul to properly forward the
1233 * error, and we may not want to do that fallback to a
1234 * default cpu identity map :-\
1236 evlist->cpus = cpu_map__new(NULL);
1237 if (evlist->cpus == NULL)
1240 evlist->threads = thread_map__new_dummy();
1241 if (evlist->threads == NULL)
1248 cpu_map__delete(evlist->cpus);
1249 evlist->cpus = NULL;
1253 int perf_evlist__open(struct perf_evlist *evlist)
1255 struct perf_evsel *evsel;
1259 * Default: one fd per CPU, all threads, aka systemwide
1260 * as sys_perf_event_open(cpu = -1, thread = -1) is EINVAL
1262 if (evlist->threads == NULL && evlist->cpus == NULL) {
1263 err = perf_evlist__create_syswide_maps(evlist);
1268 perf_evlist__update_id_pos(evlist);
1270 evlist__for_each(evlist, evsel) {
1271 err = perf_evsel__open(evsel, evlist->cpus, evlist->threads);
1278 perf_evlist__close(evlist);
1283 int perf_evlist__prepare_workload(struct perf_evlist *evlist, struct target *target,
1284 const char *argv[], bool pipe_output,
1285 void (*exec_error)(int signo, siginfo_t *info, void *ucontext))
1287 int child_ready_pipe[2], go_pipe[2];
1290 if (pipe(child_ready_pipe) < 0) {
1291 perror("failed to create 'ready' pipe");
1295 if (pipe(go_pipe) < 0) {
1296 perror("failed to create 'go' pipe");
1297 goto out_close_ready_pipe;
1300 evlist->workload.pid = fork();
1301 if (evlist->workload.pid < 0) {
1302 perror("failed to fork");
1303 goto out_close_pipes;
1306 if (!evlist->workload.pid) {
1312 signal(SIGTERM, SIG_DFL);
1314 close(child_ready_pipe[0]);
1316 fcntl(go_pipe[0], F_SETFD, FD_CLOEXEC);
1319 * Tell the parent we're ready to go
1321 close(child_ready_pipe[1]);
1324 * Wait until the parent tells us to go.
1326 ret = read(go_pipe[0], &bf, 1);
1328 * The parent will ask for the execvp() to be performed by
1329 * writing exactly one byte, in workload.cork_fd, usually via
1330 * perf_evlist__start_workload().
1332 * For cancelling the workload without actuallin running it,
1333 * the parent will just close workload.cork_fd, without writing
1334 * anything, i.e. read will return zero and we just exit()
1339 perror("unable to read pipe");
1343 execvp(argv[0], (char **)argv);
1348 val.sival_int = errno;
1349 if (sigqueue(getppid(), SIGUSR1, val))
1357 struct sigaction act = {
1358 .sa_flags = SA_SIGINFO,
1359 .sa_sigaction = exec_error,
1361 sigaction(SIGUSR1, &act, NULL);
1364 if (target__none(target)) {
1365 if (evlist->threads == NULL) {
1366 fprintf(stderr, "FATAL: evlist->threads need to be set at this point (%s:%d).\n",
1367 __func__, __LINE__);
1368 goto out_close_pipes;
1370 evlist->threads->map[0] = evlist->workload.pid;
1373 close(child_ready_pipe[1]);
1376 * wait for child to settle
1378 if (read(child_ready_pipe[0], &bf, 1) == -1) {
1379 perror("unable to read pipe");
1380 goto out_close_pipes;
1383 fcntl(go_pipe[1], F_SETFD, FD_CLOEXEC);
1384 evlist->workload.cork_fd = go_pipe[1];
1385 close(child_ready_pipe[0]);
1391 out_close_ready_pipe:
1392 close(child_ready_pipe[0]);
1393 close(child_ready_pipe[1]);
1397 int perf_evlist__start_workload(struct perf_evlist *evlist)
1399 if (evlist->workload.cork_fd > 0) {
1403 * Remove the cork, let it rip!
1405 ret = write(evlist->workload.cork_fd, &bf, 1);
1407 perror("enable to write to pipe");
1409 close(evlist->workload.cork_fd);
1416 int perf_evlist__parse_sample(struct perf_evlist *evlist, union perf_event *event,
1417 struct perf_sample *sample)
1419 struct perf_evsel *evsel = perf_evlist__event2evsel(evlist, event);
1423 return perf_evsel__parse_sample(evsel, event, sample);
1426 size_t perf_evlist__fprintf(struct perf_evlist *evlist, FILE *fp)
1428 struct perf_evsel *evsel;
1431 evlist__for_each(evlist, evsel) {
1432 printed += fprintf(fp, "%s%s", evsel->idx ? ", " : "",
1433 perf_evsel__name(evsel));
1436 return printed + fprintf(fp, "\n");
1439 int perf_evlist__strerror_tp(struct perf_evlist *evlist __maybe_unused,
1440 int err, char *buf, size_t size)
1446 scnprintf(buf, size, "%s",
1447 "Error:\tUnable to find debugfs\n"
1448 "Hint:\tWas your kernel was compiled with debugfs support?\n"
1449 "Hint:\tIs the debugfs filesystem mounted?\n"
1450 "Hint:\tTry 'sudo mount -t debugfs nodev /sys/kernel/debug'");
1453 scnprintf(buf, size,
1454 "Error:\tNo permissions to read %s/tracing/events/raw_syscalls\n"
1455 "Hint:\tTry 'sudo mount -o remount,mode=755 %s'\n",
1456 debugfs_mountpoint, debugfs_mountpoint);
1459 scnprintf(buf, size, "%s", strerror_r(err, sbuf, sizeof(sbuf)));
1466 int perf_evlist__strerror_open(struct perf_evlist *evlist __maybe_unused,
1467 int err, char *buf, size_t size)
1470 char sbuf[STRERR_BUFSIZE], *emsg = strerror_r(err, sbuf, sizeof(sbuf));
1475 printed = scnprintf(buf, size,
1477 "Hint:\tCheck /proc/sys/kernel/perf_event_paranoid setting.", emsg);
1479 value = perf_event_paranoid();
1481 printed += scnprintf(buf + printed, size - printed, "\nHint:\t");
1484 printed += scnprintf(buf + printed, size - printed,
1485 "For your workloads it needs to be <= 1\nHint:\t");
1487 printed += scnprintf(buf + printed, size - printed,
1488 "For system wide tracing it needs to be set to -1.\n");
1490 printed += scnprintf(buf + printed, size - printed,
1491 "Hint:\tTry: 'sudo sh -c \"echo -1 > /proc/sys/kernel/perf_event_paranoid\"'\n"
1492 "Hint:\tThe current value is %d.", value);
1495 scnprintf(buf, size, "%s", emsg);
1502 int perf_evlist__strerror_mmap(struct perf_evlist *evlist, int err, char *buf, size_t size)
1504 char sbuf[STRERR_BUFSIZE], *emsg = strerror_r(err, sbuf, sizeof(sbuf));
1505 int pages_attempted = evlist->mmap_len / 1024, pages_max_per_user, printed = 0;
1509 sysctl__read_int("kernel/perf_event_mlock_kb", &pages_max_per_user);
1510 printed += scnprintf(buf + printed, size - printed,
1512 "Hint:\tCheck /proc/sys/kernel/perf_event_mlock_kb (%d kB) setting.\n"
1513 "Hint:\tTried using %zd kB.\n",
1514 emsg, pages_max_per_user, pages_attempted);
1516 if (pages_attempted >= pages_max_per_user) {
1517 printed += scnprintf(buf + printed, size - printed,
1518 "Hint:\tTry 'sudo sh -c \"echo %d > /proc/sys/kernel/perf_event_mlock_kb\"', or\n",
1519 pages_max_per_user + pages_attempted);
1522 printed += scnprintf(buf + printed, size - printed,
1523 "Hint:\tTry using a smaller -m/--mmap-pages value.");
1526 scnprintf(buf, size, "%s", emsg);
1533 void perf_evlist__to_front(struct perf_evlist *evlist,
1534 struct perf_evsel *move_evsel)
1536 struct perf_evsel *evsel, *n;
1539 if (move_evsel == perf_evlist__first(evlist))
1542 evlist__for_each_safe(evlist, n, evsel) {
1543 if (evsel->leader == move_evsel->leader)
1544 list_move_tail(&evsel->node, &move);
1547 list_splice(&move, &evlist->entries);
1550 void perf_evlist__set_tracking_event(struct perf_evlist *evlist,
1551 struct perf_evsel *tracking_evsel)
1553 struct perf_evsel *evsel;
1555 if (tracking_evsel->tracking)
1558 evlist__for_each(evlist, evsel) {
1559 if (evsel != tracking_evsel)
1560 evsel->tracking = false;
1563 tracking_evsel->tracking = true;