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 void perf_evlist__add(struct perf_evlist *evlist, struct perf_evsel *entry)
129 entry->evlist = evlist;
130 list_add_tail(&entry->node, &evlist->entries);
131 entry->idx = evlist->nr_entries;
132 entry->tracking = !entry->idx;
134 if (!evlist->nr_entries++)
135 perf_evlist__set_id_pos(evlist);
138 void perf_evlist__splice_list_tail(struct perf_evlist *evlist,
139 struct list_head *list)
141 struct perf_evsel *evsel, *temp;
143 __evlist__for_each_safe(list, temp, evsel) {
144 list_del_init(&evsel->node);
145 perf_evlist__add(evlist, evsel);
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);
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);
302 evlist->enabled = false;
305 void perf_evlist__enable(struct perf_evlist *evlist)
308 struct perf_evsel *pos;
309 int nr_cpus = cpu_map__nr(evlist->cpus);
312 for (cpu = 0; cpu < nr_cpus; cpu++) {
313 evlist__for_each(evlist, pos) {
314 if (!perf_evsel__is_group_leader(pos) || !pos->fd)
316 nr_threads = perf_evlist__nr_threads(evlist, pos);
317 for (thread = 0; thread < nr_threads; thread++)
318 ioctl(FD(pos, cpu, thread),
319 PERF_EVENT_IOC_ENABLE, 0);
323 evlist->enabled = true;
326 void perf_evlist__toggle_enable(struct perf_evlist *evlist)
328 (evlist->enabled ? perf_evlist__disable : perf_evlist__enable)(evlist);
331 int perf_evlist__disable_event(struct perf_evlist *evlist,
332 struct perf_evsel *evsel)
334 int cpu, thread, err;
335 int nr_cpus = cpu_map__nr(evlist->cpus);
336 int nr_threads = perf_evlist__nr_threads(evlist, evsel);
341 for (cpu = 0; cpu < nr_cpus; cpu++) {
342 for (thread = 0; thread < nr_threads; thread++) {
343 err = ioctl(FD(evsel, cpu, thread),
344 PERF_EVENT_IOC_DISABLE, 0);
352 int perf_evlist__enable_event(struct perf_evlist *evlist,
353 struct perf_evsel *evsel)
355 int cpu, thread, err;
356 int nr_cpus = cpu_map__nr(evlist->cpus);
357 int nr_threads = perf_evlist__nr_threads(evlist, evsel);
362 for (cpu = 0; cpu < nr_cpus; cpu++) {
363 for (thread = 0; thread < nr_threads; thread++) {
364 err = ioctl(FD(evsel, cpu, thread),
365 PERF_EVENT_IOC_ENABLE, 0);
373 static int perf_evlist__enable_event_cpu(struct perf_evlist *evlist,
374 struct perf_evsel *evsel, int cpu)
377 int nr_threads = perf_evlist__nr_threads(evlist, evsel);
382 for (thread = 0; thread < nr_threads; thread++) {
383 err = ioctl(FD(evsel, cpu, thread),
384 PERF_EVENT_IOC_ENABLE, 0);
391 static int perf_evlist__enable_event_thread(struct perf_evlist *evlist,
392 struct perf_evsel *evsel,
396 int nr_cpus = cpu_map__nr(evlist->cpus);
401 for (cpu = 0; cpu < nr_cpus; cpu++) {
402 err = ioctl(FD(evsel, cpu, thread), PERF_EVENT_IOC_ENABLE, 0);
409 int perf_evlist__enable_event_idx(struct perf_evlist *evlist,
410 struct perf_evsel *evsel, int idx)
412 bool per_cpu_mmaps = !cpu_map__empty(evlist->cpus);
415 return perf_evlist__enable_event_cpu(evlist, evsel, idx);
417 return perf_evlist__enable_event_thread(evlist, evsel, idx);
420 int perf_evlist__alloc_pollfd(struct perf_evlist *evlist)
422 int nr_cpus = cpu_map__nr(evlist->cpus);
423 int nr_threads = thread_map__nr(evlist->threads);
425 struct perf_evsel *evsel;
427 evlist__for_each(evlist, evsel) {
428 if (evsel->system_wide)
431 nfds += nr_cpus * nr_threads;
434 if (fdarray__available_entries(&evlist->pollfd) < nfds &&
435 fdarray__grow(&evlist->pollfd, nfds) < 0)
441 static int __perf_evlist__add_pollfd(struct perf_evlist *evlist, int fd, int idx)
443 int pos = fdarray__add(&evlist->pollfd, fd, POLLIN | POLLERR | POLLHUP);
445 * Save the idx so that when we filter out fds POLLHUP'ed we can
446 * close the associated evlist->mmap[] entry.
449 evlist->pollfd.priv[pos].idx = idx;
451 fcntl(fd, F_SETFL, O_NONBLOCK);
457 int perf_evlist__add_pollfd(struct perf_evlist *evlist, int fd)
459 return __perf_evlist__add_pollfd(evlist, fd, -1);
462 static void perf_evlist__munmap_filtered(struct fdarray *fda, int fd)
464 struct perf_evlist *evlist = container_of(fda, struct perf_evlist, pollfd);
466 perf_evlist__mmap_put(evlist, fda->priv[fd].idx);
469 int perf_evlist__filter_pollfd(struct perf_evlist *evlist, short revents_and_mask)
471 return fdarray__filter(&evlist->pollfd, revents_and_mask,
472 perf_evlist__munmap_filtered);
475 int perf_evlist__poll(struct perf_evlist *evlist, int timeout)
477 return fdarray__poll(&evlist->pollfd, timeout);
480 static void perf_evlist__id_hash(struct perf_evlist *evlist,
481 struct perf_evsel *evsel,
482 int cpu, int thread, u64 id)
485 struct perf_sample_id *sid = SID(evsel, cpu, thread);
489 hash = hash_64(sid->id, PERF_EVLIST__HLIST_BITS);
490 hlist_add_head(&sid->node, &evlist->heads[hash]);
493 void perf_evlist__id_add(struct perf_evlist *evlist, struct perf_evsel *evsel,
494 int cpu, int thread, u64 id)
496 perf_evlist__id_hash(evlist, evsel, cpu, thread, id);
497 evsel->id[evsel->ids++] = id;
500 static int perf_evlist__id_add_fd(struct perf_evlist *evlist,
501 struct perf_evsel *evsel,
502 int cpu, int thread, int fd)
504 u64 read_data[4] = { 0, };
505 int id_idx = 1; /* The first entry is the counter value */
509 ret = ioctl(fd, PERF_EVENT_IOC_ID, &id);
516 /* Legacy way to get event id.. All hail to old kernels! */
519 * This way does not work with group format read, so bail
522 if (perf_evlist__read_format(evlist) & PERF_FORMAT_GROUP)
525 if (!(evsel->attr.read_format & PERF_FORMAT_ID) ||
526 read(fd, &read_data, sizeof(read_data)) == -1)
529 if (evsel->attr.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
531 if (evsel->attr.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
534 id = read_data[id_idx];
537 perf_evlist__id_add(evlist, evsel, cpu, thread, id);
541 static void perf_evlist__set_sid_idx(struct perf_evlist *evlist,
542 struct perf_evsel *evsel, int idx, int cpu,
545 struct perf_sample_id *sid = SID(evsel, cpu, thread);
547 if (evlist->cpus && cpu >= 0)
548 sid->cpu = evlist->cpus->map[cpu];
551 if (!evsel->system_wide && evlist->threads && thread >= 0)
552 sid->tid = thread_map__pid(evlist->threads, thread);
557 struct perf_sample_id *perf_evlist__id2sid(struct perf_evlist *evlist, u64 id)
559 struct hlist_head *head;
560 struct perf_sample_id *sid;
563 hash = hash_64(id, PERF_EVLIST__HLIST_BITS);
564 head = &evlist->heads[hash];
566 hlist_for_each_entry(sid, head, node)
573 struct perf_evsel *perf_evlist__id2evsel(struct perf_evlist *evlist, u64 id)
575 struct perf_sample_id *sid;
577 if (evlist->nr_entries == 1 || !id)
578 return perf_evlist__first(evlist);
580 sid = perf_evlist__id2sid(evlist, id);
584 if (!perf_evlist__sample_id_all(evlist))
585 return perf_evlist__first(evlist);
590 static int perf_evlist__event2id(struct perf_evlist *evlist,
591 union perf_event *event, u64 *id)
593 const u64 *array = event->sample.array;
596 n = (event->header.size - sizeof(event->header)) >> 3;
598 if (event->header.type == PERF_RECORD_SAMPLE) {
599 if (evlist->id_pos >= n)
601 *id = array[evlist->id_pos];
603 if (evlist->is_pos > n)
611 static struct perf_evsel *perf_evlist__event2evsel(struct perf_evlist *evlist,
612 union perf_event *event)
614 struct perf_evsel *first = perf_evlist__first(evlist);
615 struct hlist_head *head;
616 struct perf_sample_id *sid;
620 if (evlist->nr_entries == 1)
623 if (!first->attr.sample_id_all &&
624 event->header.type != PERF_RECORD_SAMPLE)
627 if (perf_evlist__event2id(evlist, event, &id))
630 /* Synthesized events have an id of zero */
634 hash = hash_64(id, PERF_EVLIST__HLIST_BITS);
635 head = &evlist->heads[hash];
637 hlist_for_each_entry(sid, head, node) {
644 union perf_event *perf_evlist__mmap_read(struct perf_evlist *evlist, int idx)
646 struct perf_mmap *md = &evlist->mmap[idx];
649 unsigned char *data = md->base + page_size;
650 union perf_event *event = NULL;
653 * Check if event was unmapped due to a POLLHUP/POLLERR.
655 if (!atomic_read(&md->refcnt))
658 head = perf_mmap__read_head(md);
659 if (evlist->overwrite) {
661 * If we're further behind than half the buffer, there's a chance
662 * the writer will bite our tail and mess up the samples under us.
664 * If we somehow ended up ahead of the head, we got messed up.
666 * In either case, truncate and restart at head.
668 int diff = head - old;
669 if (diff > md->mask / 2 || diff < 0) {
670 fprintf(stderr, "WARNING: failed to keep up with mmap data.\n");
673 * head points to a known good entry, start there.
682 event = (union perf_event *)&data[old & md->mask];
683 size = event->header.size;
686 * Event straddles the mmap boundary -- header should always
687 * be inside due to u64 alignment of output.
689 if ((old & md->mask) + size != ((old + size) & md->mask)) {
690 unsigned int offset = old;
691 unsigned int len = min(sizeof(*event), size), cpy;
692 void *dst = md->event_copy;
695 cpy = min(md->mask + 1 - (offset & md->mask), len);
696 memcpy(dst, &data[offset & md->mask], cpy);
702 event = (union perf_event *) md->event_copy;
713 static bool perf_mmap__empty(struct perf_mmap *md)
715 return perf_mmap__read_head(md) == md->prev && !md->auxtrace_mmap.base;
718 static void perf_evlist__mmap_get(struct perf_evlist *evlist, int idx)
720 atomic_inc(&evlist->mmap[idx].refcnt);
723 static void perf_evlist__mmap_put(struct perf_evlist *evlist, int idx)
725 BUG_ON(atomic_read(&evlist->mmap[idx].refcnt) == 0);
727 if (atomic_dec_and_test(&evlist->mmap[idx].refcnt))
728 __perf_evlist__munmap(evlist, idx);
731 void perf_evlist__mmap_consume(struct perf_evlist *evlist, int idx)
733 struct perf_mmap *md = &evlist->mmap[idx];
735 if (!evlist->overwrite) {
738 perf_mmap__write_tail(md, old);
741 if (atomic_read(&md->refcnt) == 1 && perf_mmap__empty(md))
742 perf_evlist__mmap_put(evlist, idx);
745 int __weak auxtrace_mmap__mmap(struct auxtrace_mmap *mm __maybe_unused,
746 struct auxtrace_mmap_params *mp __maybe_unused,
747 void *userpg __maybe_unused,
748 int fd __maybe_unused)
753 void __weak auxtrace_mmap__munmap(struct auxtrace_mmap *mm __maybe_unused)
757 void __weak auxtrace_mmap_params__init(
758 struct auxtrace_mmap_params *mp __maybe_unused,
759 off_t auxtrace_offset __maybe_unused,
760 unsigned int auxtrace_pages __maybe_unused,
761 bool auxtrace_overwrite __maybe_unused)
765 void __weak auxtrace_mmap_params__set_idx(
766 struct auxtrace_mmap_params *mp __maybe_unused,
767 struct perf_evlist *evlist __maybe_unused,
768 int idx __maybe_unused,
769 bool per_cpu __maybe_unused)
773 static void __perf_evlist__munmap(struct perf_evlist *evlist, int idx)
775 if (evlist->mmap[idx].base != NULL) {
776 munmap(evlist->mmap[idx].base, evlist->mmap_len);
777 evlist->mmap[idx].base = NULL;
778 atomic_set(&evlist->mmap[idx].refcnt, 0);
780 auxtrace_mmap__munmap(&evlist->mmap[idx].auxtrace_mmap);
783 void perf_evlist__munmap(struct perf_evlist *evlist)
787 if (evlist->mmap == NULL)
790 for (i = 0; i < evlist->nr_mmaps; i++)
791 __perf_evlist__munmap(evlist, i);
793 zfree(&evlist->mmap);
796 static int perf_evlist__alloc_mmap(struct perf_evlist *evlist)
798 evlist->nr_mmaps = cpu_map__nr(evlist->cpus);
799 if (cpu_map__empty(evlist->cpus))
800 evlist->nr_mmaps = thread_map__nr(evlist->threads);
801 evlist->mmap = zalloc(evlist->nr_mmaps * sizeof(struct perf_mmap));
802 return evlist->mmap != NULL ? 0 : -ENOMEM;
808 struct auxtrace_mmap_params auxtrace_mp;
811 static int __perf_evlist__mmap(struct perf_evlist *evlist, int idx,
812 struct mmap_params *mp, int fd)
815 * The last one will be done at perf_evlist__mmap_consume(), so that we
816 * make sure we don't prevent tools from consuming every last event in
819 * I.e. we can get the POLLHUP meaning that the fd doesn't exist
820 * anymore, but the last events for it are still in the ring buffer,
821 * waiting to be consumed.
823 * Tools can chose to ignore this at their own discretion, but the
824 * evlist layer can't just drop it when filtering events in
825 * perf_evlist__filter_pollfd().
827 atomic_set(&evlist->mmap[idx].refcnt, 2);
828 evlist->mmap[idx].prev = 0;
829 evlist->mmap[idx].mask = mp->mask;
830 evlist->mmap[idx].base = mmap(NULL, evlist->mmap_len, mp->prot,
832 if (evlist->mmap[idx].base == MAP_FAILED) {
833 pr_debug2("failed to mmap perf event ring buffer, error %d\n",
835 evlist->mmap[idx].base = NULL;
839 if (auxtrace_mmap__mmap(&evlist->mmap[idx].auxtrace_mmap,
840 &mp->auxtrace_mp, evlist->mmap[idx].base, fd))
846 static int perf_evlist__mmap_per_evsel(struct perf_evlist *evlist, int idx,
847 struct mmap_params *mp, int cpu,
848 int thread, int *output)
850 struct perf_evsel *evsel;
852 evlist__for_each(evlist, evsel) {
855 if (evsel->system_wide && thread)
858 fd = FD(evsel, cpu, thread);
862 if (__perf_evlist__mmap(evlist, idx, mp, *output) < 0)
865 if (ioctl(fd, PERF_EVENT_IOC_SET_OUTPUT, *output) != 0)
868 perf_evlist__mmap_get(evlist, idx);
872 * The system_wide flag causes a selected event to be opened
873 * always without a pid. Consequently it will never get a
874 * POLLHUP, but it is used for tracking in combination with
875 * other events, so it should not need to be polled anyway.
876 * Therefore don't add it for polling.
878 if (!evsel->system_wide &&
879 __perf_evlist__add_pollfd(evlist, fd, idx) < 0) {
880 perf_evlist__mmap_put(evlist, idx);
884 if (evsel->attr.read_format & PERF_FORMAT_ID) {
885 if (perf_evlist__id_add_fd(evlist, evsel, cpu, thread,
888 perf_evlist__set_sid_idx(evlist, evsel, idx, cpu,
896 static int perf_evlist__mmap_per_cpu(struct perf_evlist *evlist,
897 struct mmap_params *mp)
900 int nr_cpus = cpu_map__nr(evlist->cpus);
901 int nr_threads = thread_map__nr(evlist->threads);
903 pr_debug2("perf event ring buffer mmapped per cpu\n");
904 for (cpu = 0; cpu < nr_cpus; cpu++) {
907 auxtrace_mmap_params__set_idx(&mp->auxtrace_mp, evlist, cpu,
910 for (thread = 0; thread < nr_threads; thread++) {
911 if (perf_evlist__mmap_per_evsel(evlist, cpu, mp, cpu,
920 for (cpu = 0; cpu < nr_cpus; cpu++)
921 __perf_evlist__munmap(evlist, cpu);
925 static int perf_evlist__mmap_per_thread(struct perf_evlist *evlist,
926 struct mmap_params *mp)
929 int nr_threads = thread_map__nr(evlist->threads);
931 pr_debug2("perf event ring buffer mmapped per thread\n");
932 for (thread = 0; thread < nr_threads; thread++) {
935 auxtrace_mmap_params__set_idx(&mp->auxtrace_mp, evlist, thread,
938 if (perf_evlist__mmap_per_evsel(evlist, thread, mp, 0, thread,
946 for (thread = 0; thread < nr_threads; thread++)
947 __perf_evlist__munmap(evlist, thread);
951 static size_t perf_evlist__mmap_size(unsigned long pages)
953 if (pages == UINT_MAX) {
956 if (sysctl__read_int("kernel/perf_event_mlock_kb", &max) < 0) {
958 * Pick a once upon a time good value, i.e. things look
959 * strange since we can't read a sysctl value, but lets not
964 max -= (page_size / 1024);
967 pages = (max * 1024) / page_size;
968 if (!is_power_of_2(pages))
969 pages = rounddown_pow_of_two(pages);
970 } else if (!is_power_of_2(pages))
973 return (pages + 1) * page_size;
976 static long parse_pages_arg(const char *str, unsigned long min,
979 unsigned long pages, val;
980 static struct parse_tag tags[] = {
981 { .tag = 'B', .mult = 1 },
982 { .tag = 'K', .mult = 1 << 10 },
983 { .tag = 'M', .mult = 1 << 20 },
984 { .tag = 'G', .mult = 1 << 30 },
991 val = parse_tag_value(str, tags);
992 if (val != (unsigned long) -1) {
993 /* we got file size value */
994 pages = PERF_ALIGN(val, page_size) / page_size;
996 /* we got pages count value */
998 pages = strtoul(str, &eptr, 10);
1003 if (pages == 0 && min == 0) {
1004 /* leave number of pages at 0 */
1005 } else if (!is_power_of_2(pages)) {
1006 /* round pages up to next power of 2 */
1007 pages = roundup_pow_of_two(pages);
1010 pr_info("rounding mmap pages size to %lu bytes (%lu pages)\n",
1011 pages * page_size, pages);
1020 int __perf_evlist__parse_mmap_pages(unsigned int *mmap_pages, const char *str)
1022 unsigned long max = UINT_MAX;
1025 if (max > SIZE_MAX / page_size)
1026 max = SIZE_MAX / page_size;
1028 pages = parse_pages_arg(str, 1, max);
1030 pr_err("Invalid argument for --mmap_pages/-m\n");
1034 *mmap_pages = pages;
1038 int perf_evlist__parse_mmap_pages(const struct option *opt, const char *str,
1039 int unset __maybe_unused)
1041 return __perf_evlist__parse_mmap_pages(opt->value, str);
1045 * perf_evlist__mmap_ex - Create mmaps to receive events.
1046 * @evlist: list of events
1047 * @pages: map length in pages
1048 * @overwrite: overwrite older events?
1049 * @auxtrace_pages - auxtrace map length in pages
1050 * @auxtrace_overwrite - overwrite older auxtrace data?
1052 * If @overwrite is %false the user needs to signal event consumption using
1053 * perf_mmap__write_tail(). Using perf_evlist__mmap_read() does this
1056 * Similarly, if @auxtrace_overwrite is %false the user needs to signal data
1057 * consumption using auxtrace_mmap__write_tail().
1059 * Return: %0 on success, negative error code otherwise.
1061 int perf_evlist__mmap_ex(struct perf_evlist *evlist, unsigned int pages,
1062 bool overwrite, unsigned int auxtrace_pages,
1063 bool auxtrace_overwrite)
1065 struct perf_evsel *evsel;
1066 const struct cpu_map *cpus = evlist->cpus;
1067 const struct thread_map *threads = evlist->threads;
1068 struct mmap_params mp = {
1069 .prot = PROT_READ | (overwrite ? 0 : PROT_WRITE),
1072 if (evlist->mmap == NULL && perf_evlist__alloc_mmap(evlist) < 0)
1075 if (evlist->pollfd.entries == NULL && perf_evlist__alloc_pollfd(evlist) < 0)
1078 evlist->overwrite = overwrite;
1079 evlist->mmap_len = perf_evlist__mmap_size(pages);
1080 pr_debug("mmap size %zuB\n", evlist->mmap_len);
1081 mp.mask = evlist->mmap_len - page_size - 1;
1083 auxtrace_mmap_params__init(&mp.auxtrace_mp, evlist->mmap_len,
1084 auxtrace_pages, auxtrace_overwrite);
1086 evlist__for_each(evlist, evsel) {
1087 if ((evsel->attr.read_format & PERF_FORMAT_ID) &&
1088 evsel->sample_id == NULL &&
1089 perf_evsel__alloc_id(evsel, cpu_map__nr(cpus), threads->nr) < 0)
1093 if (cpu_map__empty(cpus))
1094 return perf_evlist__mmap_per_thread(evlist, &mp);
1096 return perf_evlist__mmap_per_cpu(evlist, &mp);
1099 int perf_evlist__mmap(struct perf_evlist *evlist, unsigned int pages,
1102 return perf_evlist__mmap_ex(evlist, pages, overwrite, 0, false);
1105 static void 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->own_cpus || evlist->has_user_cpus) {
1115 cpu_map__put(evsel->cpus);
1116 evsel->cpus = cpu_map__get(evlist->cpus);
1117 } else if (evsel->cpus != evsel->own_cpus) {
1118 cpu_map__put(evsel->cpus);
1119 evsel->cpus = cpu_map__get(evsel->own_cpus);
1122 thread_map__put(evsel->threads);
1123 evsel->threads = thread_map__get(evlist->threads);
1127 int perf_evlist__create_maps(struct perf_evlist *evlist, struct target *target)
1129 struct cpu_map *cpus;
1130 struct thread_map *threads;
1132 threads = thread_map__new_str(target->pid, target->tid, target->uid);
1137 if (target__uses_dummy_map(target))
1138 cpus = cpu_map__dummy_new();
1140 cpus = cpu_map__new(target->cpu_list);
1143 goto out_delete_threads;
1145 evlist->has_user_cpus = !!target->cpu_list;
1147 perf_evlist__set_maps(evlist, cpus, threads);
1152 thread_map__put(threads);
1156 void perf_evlist__set_maps(struct perf_evlist *evlist, struct cpu_map *cpus,
1157 struct thread_map *threads)
1160 * Allow for the possibility that one or another of the maps isn't being
1161 * changed i.e. don't put it. Note we are assuming the maps that are
1162 * being applied are brand new and evlist is taking ownership of the
1163 * original reference count of 1. If that is not the case it is up to
1164 * the caller to increase the reference count.
1166 if (cpus != evlist->cpus) {
1167 cpu_map__put(evlist->cpus);
1168 evlist->cpus = cpus;
1171 if (threads != evlist->threads) {
1172 thread_map__put(evlist->threads);
1173 evlist->threads = threads;
1176 perf_evlist__propagate_maps(evlist);
1179 int perf_evlist__apply_filters(struct perf_evlist *evlist, struct perf_evsel **err_evsel)
1181 struct perf_evsel *evsel;
1183 const int ncpus = cpu_map__nr(evlist->cpus),
1184 nthreads = thread_map__nr(evlist->threads);
1186 evlist__for_each(evlist, evsel) {
1187 if (evsel->filter == NULL)
1191 * filters only work for tracepoint event, which doesn't have cpu limit.
1192 * So evlist and evsel should always be same.
1194 err = perf_evsel__apply_filter(evsel, ncpus, nthreads, evsel->filter);
1204 int perf_evlist__set_filter(struct perf_evlist *evlist, const char *filter)
1206 struct perf_evsel *evsel;
1209 evlist__for_each(evlist, evsel) {
1210 err = perf_evsel__set_filter(evsel, filter);
1218 int perf_evlist__set_filter_pids(struct perf_evlist *evlist, size_t npids, pid_t *pids)
1224 for (i = 0; i < npids; ++i) {
1226 if (asprintf(&filter, "common_pid != %d", pids[i]) < 0)
1231 if (asprintf(&tmp, "%s && common_pid != %d", filter, pids[i]) < 0)
1239 ret = perf_evlist__set_filter(evlist, filter);
1245 int perf_evlist__set_filter_pid(struct perf_evlist *evlist, pid_t pid)
1247 return perf_evlist__set_filter_pids(evlist, 1, &pid);
1250 bool perf_evlist__valid_sample_type(struct perf_evlist *evlist)
1252 struct perf_evsel *pos;
1254 if (evlist->nr_entries == 1)
1257 if (evlist->id_pos < 0 || evlist->is_pos < 0)
1260 evlist__for_each(evlist, pos) {
1261 if (pos->id_pos != evlist->id_pos ||
1262 pos->is_pos != evlist->is_pos)
1269 u64 __perf_evlist__combined_sample_type(struct perf_evlist *evlist)
1271 struct perf_evsel *evsel;
1273 if (evlist->combined_sample_type)
1274 return evlist->combined_sample_type;
1276 evlist__for_each(evlist, evsel)
1277 evlist->combined_sample_type |= evsel->attr.sample_type;
1279 return evlist->combined_sample_type;
1282 u64 perf_evlist__combined_sample_type(struct perf_evlist *evlist)
1284 evlist->combined_sample_type = 0;
1285 return __perf_evlist__combined_sample_type(evlist);
1288 u64 perf_evlist__combined_branch_type(struct perf_evlist *evlist)
1290 struct perf_evsel *evsel;
1291 u64 branch_type = 0;
1293 evlist__for_each(evlist, evsel)
1294 branch_type |= evsel->attr.branch_sample_type;
1298 bool perf_evlist__valid_read_format(struct perf_evlist *evlist)
1300 struct perf_evsel *first = perf_evlist__first(evlist), *pos = first;
1301 u64 read_format = first->attr.read_format;
1302 u64 sample_type = first->attr.sample_type;
1304 evlist__for_each(evlist, pos) {
1305 if (read_format != pos->attr.read_format)
1309 /* PERF_SAMPLE_READ imples PERF_FORMAT_ID. */
1310 if ((sample_type & PERF_SAMPLE_READ) &&
1311 !(read_format & PERF_FORMAT_ID)) {
1318 u64 perf_evlist__read_format(struct perf_evlist *evlist)
1320 struct perf_evsel *first = perf_evlist__first(evlist);
1321 return first->attr.read_format;
1324 u16 perf_evlist__id_hdr_size(struct perf_evlist *evlist)
1326 struct perf_evsel *first = perf_evlist__first(evlist);
1327 struct perf_sample *data;
1331 if (!first->attr.sample_id_all)
1334 sample_type = first->attr.sample_type;
1336 if (sample_type & PERF_SAMPLE_TID)
1337 size += sizeof(data->tid) * 2;
1339 if (sample_type & PERF_SAMPLE_TIME)
1340 size += sizeof(data->time);
1342 if (sample_type & PERF_SAMPLE_ID)
1343 size += sizeof(data->id);
1345 if (sample_type & PERF_SAMPLE_STREAM_ID)
1346 size += sizeof(data->stream_id);
1348 if (sample_type & PERF_SAMPLE_CPU)
1349 size += sizeof(data->cpu) * 2;
1351 if (sample_type & PERF_SAMPLE_IDENTIFIER)
1352 size += sizeof(data->id);
1357 bool perf_evlist__valid_sample_id_all(struct perf_evlist *evlist)
1359 struct perf_evsel *first = perf_evlist__first(evlist), *pos = first;
1361 evlist__for_each_continue(evlist, pos) {
1362 if (first->attr.sample_id_all != pos->attr.sample_id_all)
1369 bool perf_evlist__sample_id_all(struct perf_evlist *evlist)
1371 struct perf_evsel *first = perf_evlist__first(evlist);
1372 return first->attr.sample_id_all;
1375 void perf_evlist__set_selected(struct perf_evlist *evlist,
1376 struct perf_evsel *evsel)
1378 evlist->selected = evsel;
1381 void perf_evlist__close(struct perf_evlist *evlist)
1383 struct perf_evsel *evsel;
1384 int ncpus = cpu_map__nr(evlist->cpus);
1385 int nthreads = thread_map__nr(evlist->threads);
1388 evlist__for_each_reverse(evlist, evsel) {
1389 n = evsel->cpus ? evsel->cpus->nr : ncpus;
1390 perf_evsel__close(evsel, n, nthreads);
1394 static int perf_evlist__create_syswide_maps(struct perf_evlist *evlist)
1399 * Try reading /sys/devices/system/cpu/online to get
1402 * FIXME: -ENOMEM is the best we can do here, the cpu_map
1403 * code needs an overhaul to properly forward the
1404 * error, and we may not want to do that fallback to a
1405 * default cpu identity map :-\
1407 evlist->cpus = cpu_map__new(NULL);
1408 if (evlist->cpus == NULL)
1411 evlist->threads = thread_map__new_dummy();
1412 if (evlist->threads == NULL)
1419 cpu_map__put(evlist->cpus);
1420 evlist->cpus = NULL;
1424 int perf_evlist__open(struct perf_evlist *evlist)
1426 struct perf_evsel *evsel;
1430 * Default: one fd per CPU, all threads, aka systemwide
1431 * as sys_perf_event_open(cpu = -1, thread = -1) is EINVAL
1433 if (evlist->threads == NULL && evlist->cpus == NULL) {
1434 err = perf_evlist__create_syswide_maps(evlist);
1439 perf_evlist__update_id_pos(evlist);
1441 evlist__for_each(evlist, evsel) {
1442 err = perf_evsel__open(evsel, evlist->cpus, evlist->threads);
1449 perf_evlist__close(evlist);
1454 int perf_evlist__prepare_workload(struct perf_evlist *evlist, struct target *target,
1455 const char *argv[], bool pipe_output,
1456 void (*exec_error)(int signo, siginfo_t *info, void *ucontext))
1458 int child_ready_pipe[2], go_pipe[2];
1461 if (pipe(child_ready_pipe) < 0) {
1462 perror("failed to create 'ready' pipe");
1466 if (pipe(go_pipe) < 0) {
1467 perror("failed to create 'go' pipe");
1468 goto out_close_ready_pipe;
1471 evlist->workload.pid = fork();
1472 if (evlist->workload.pid < 0) {
1473 perror("failed to fork");
1474 goto out_close_pipes;
1477 if (!evlist->workload.pid) {
1483 signal(SIGTERM, SIG_DFL);
1485 close(child_ready_pipe[0]);
1487 fcntl(go_pipe[0], F_SETFD, FD_CLOEXEC);
1490 * Tell the parent we're ready to go
1492 close(child_ready_pipe[1]);
1495 * Wait until the parent tells us to go.
1497 ret = read(go_pipe[0], &bf, 1);
1499 * The parent will ask for the execvp() to be performed by
1500 * writing exactly one byte, in workload.cork_fd, usually via
1501 * perf_evlist__start_workload().
1503 * For cancelling the workload without actually running it,
1504 * the parent will just close workload.cork_fd, without writing
1505 * anything, i.e. read will return zero and we just exit()
1510 perror("unable to read pipe");
1514 execvp(argv[0], (char **)argv);
1519 val.sival_int = errno;
1520 if (sigqueue(getppid(), SIGUSR1, val))
1528 struct sigaction act = {
1529 .sa_flags = SA_SIGINFO,
1530 .sa_sigaction = exec_error,
1532 sigaction(SIGUSR1, &act, NULL);
1535 if (target__none(target)) {
1536 if (evlist->threads == NULL) {
1537 fprintf(stderr, "FATAL: evlist->threads need to be set at this point (%s:%d).\n",
1538 __func__, __LINE__);
1539 goto out_close_pipes;
1541 thread_map__set_pid(evlist->threads, 0, evlist->workload.pid);
1544 close(child_ready_pipe[1]);
1547 * wait for child to settle
1549 if (read(child_ready_pipe[0], &bf, 1) == -1) {
1550 perror("unable to read pipe");
1551 goto out_close_pipes;
1554 fcntl(go_pipe[1], F_SETFD, FD_CLOEXEC);
1555 evlist->workload.cork_fd = go_pipe[1];
1556 close(child_ready_pipe[0]);
1562 out_close_ready_pipe:
1563 close(child_ready_pipe[0]);
1564 close(child_ready_pipe[1]);
1568 int perf_evlist__start_workload(struct perf_evlist *evlist)
1570 if (evlist->workload.cork_fd > 0) {
1574 * Remove the cork, let it rip!
1576 ret = write(evlist->workload.cork_fd, &bf, 1);
1578 perror("enable to write to pipe");
1580 close(evlist->workload.cork_fd);
1587 int perf_evlist__parse_sample(struct perf_evlist *evlist, union perf_event *event,
1588 struct perf_sample *sample)
1590 struct perf_evsel *evsel = perf_evlist__event2evsel(evlist, event);
1594 return perf_evsel__parse_sample(evsel, event, sample);
1597 size_t perf_evlist__fprintf(struct perf_evlist *evlist, FILE *fp)
1599 struct perf_evsel *evsel;
1602 evlist__for_each(evlist, evsel) {
1603 printed += fprintf(fp, "%s%s", evsel->idx ? ", " : "",
1604 perf_evsel__name(evsel));
1607 return printed + fprintf(fp, "\n");
1610 int perf_evlist__strerror_open(struct perf_evlist *evlist __maybe_unused,
1611 int err, char *buf, size_t size)
1614 char sbuf[STRERR_BUFSIZE], *emsg = strerror_r(err, sbuf, sizeof(sbuf));
1619 printed = scnprintf(buf, size,
1621 "Hint:\tCheck /proc/sys/kernel/perf_event_paranoid setting.", emsg);
1623 value = perf_event_paranoid();
1625 printed += scnprintf(buf + printed, size - printed, "\nHint:\t");
1628 printed += scnprintf(buf + printed, size - printed,
1629 "For your workloads it needs to be <= 1\nHint:\t");
1631 printed += scnprintf(buf + printed, size - printed,
1632 "For system wide tracing it needs to be set to -1.\n");
1634 printed += scnprintf(buf + printed, size - printed,
1635 "Hint:\tTry: 'sudo sh -c \"echo -1 > /proc/sys/kernel/perf_event_paranoid\"'\n"
1636 "Hint:\tThe current value is %d.", value);
1639 scnprintf(buf, size, "%s", emsg);
1646 int perf_evlist__strerror_mmap(struct perf_evlist *evlist, int err, char *buf, size_t size)
1648 char sbuf[STRERR_BUFSIZE], *emsg = strerror_r(err, sbuf, sizeof(sbuf));
1649 int pages_attempted = evlist->mmap_len / 1024, pages_max_per_user, printed = 0;
1653 sysctl__read_int("kernel/perf_event_mlock_kb", &pages_max_per_user);
1654 printed += scnprintf(buf + printed, size - printed,
1656 "Hint:\tCheck /proc/sys/kernel/perf_event_mlock_kb (%d kB) setting.\n"
1657 "Hint:\tTried using %zd kB.\n",
1658 emsg, pages_max_per_user, pages_attempted);
1660 if (pages_attempted >= pages_max_per_user) {
1661 printed += scnprintf(buf + printed, size - printed,
1662 "Hint:\tTry 'sudo sh -c \"echo %d > /proc/sys/kernel/perf_event_mlock_kb\"', or\n",
1663 pages_max_per_user + pages_attempted);
1666 printed += scnprintf(buf + printed, size - printed,
1667 "Hint:\tTry using a smaller -m/--mmap-pages value.");
1670 scnprintf(buf, size, "%s", emsg);
1677 void perf_evlist__to_front(struct perf_evlist *evlist,
1678 struct perf_evsel *move_evsel)
1680 struct perf_evsel *evsel, *n;
1683 if (move_evsel == perf_evlist__first(evlist))
1686 evlist__for_each_safe(evlist, n, evsel) {
1687 if (evsel->leader == move_evsel->leader)
1688 list_move_tail(&evsel->node, &move);
1691 list_splice(&move, &evlist->entries);
1694 void perf_evlist__set_tracking_event(struct perf_evlist *evlist,
1695 struct perf_evsel *tracking_evsel)
1697 struct perf_evsel *evsel;
1699 if (tracking_evsel->tracking)
1702 evlist__for_each(evlist, evsel) {
1703 if (evsel != tracking_evsel)
1704 evsel->tracking = false;
1707 tracking_evsel->tracking = true;