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>
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>
28 #define FD(e, x, y) (*(int *)xyarray__entry(e->fd, x, y))
29 #define SID(e, x, y) xyarray__entry(e->sample_id, x, y)
31 void perf_evlist__init(struct perf_evlist *evlist, struct cpu_map *cpus,
32 struct thread_map *threads)
36 for (i = 0; i < PERF_EVLIST__HLIST_SIZE; ++i)
37 INIT_HLIST_HEAD(&evlist->heads[i]);
38 INIT_LIST_HEAD(&evlist->entries);
39 perf_evlist__set_maps(evlist, cpus, threads);
40 evlist->workload.pid = -1;
43 struct perf_evlist *perf_evlist__new(void)
45 struct perf_evlist *evlist = zalloc(sizeof(*evlist));
48 perf_evlist__init(evlist, NULL, NULL);
53 struct perf_evlist *perf_evlist__new_default(void)
55 struct perf_evlist *evlist = perf_evlist__new();
57 if (evlist && perf_evlist__add_default(evlist)) {
58 perf_evlist__delete(evlist);
66 * perf_evlist__set_id_pos - set the positions of event ids.
67 * @evlist: selected event list
69 * Events with compatible sample types all have the same id_pos
70 * and is_pos. For convenience, put a copy on evlist.
72 void perf_evlist__set_id_pos(struct perf_evlist *evlist)
74 struct perf_evsel *first = perf_evlist__first(evlist);
76 evlist->id_pos = first->id_pos;
77 evlist->is_pos = first->is_pos;
80 static void perf_evlist__update_id_pos(struct perf_evlist *evlist)
82 struct perf_evsel *evsel;
84 evlist__for_each(evlist, evsel)
85 perf_evsel__calc_id_pos(evsel);
87 perf_evlist__set_id_pos(evlist);
90 static void perf_evlist__purge(struct perf_evlist *evlist)
92 struct perf_evsel *pos, *n;
94 evlist__for_each_safe(evlist, n, pos) {
95 list_del_init(&pos->node);
96 perf_evsel__delete(pos);
99 evlist->nr_entries = 0;
102 void perf_evlist__exit(struct perf_evlist *evlist)
104 zfree(&evlist->mmap);
105 zfree(&evlist->pollfd);
108 void perf_evlist__delete(struct perf_evlist *evlist)
110 perf_evlist__munmap(evlist);
111 perf_evlist__close(evlist);
112 cpu_map__delete(evlist->cpus);
113 thread_map__delete(evlist->threads);
115 evlist->threads = NULL;
116 perf_evlist__purge(evlist);
117 perf_evlist__exit(evlist);
121 void perf_evlist__add(struct perf_evlist *evlist, struct perf_evsel *entry)
123 list_add_tail(&entry->node, &evlist->entries);
124 entry->idx = evlist->nr_entries;
125 entry->tracking = !entry->idx;
127 if (!evlist->nr_entries++)
128 perf_evlist__set_id_pos(evlist);
131 void perf_evlist__splice_list_tail(struct perf_evlist *evlist,
132 struct list_head *list,
135 bool set_id_pos = !evlist->nr_entries;
137 list_splice_tail(list, &evlist->entries);
138 evlist->nr_entries += nr_entries;
140 perf_evlist__set_id_pos(evlist);
143 void __perf_evlist__set_leader(struct list_head *list)
145 struct perf_evsel *evsel, *leader;
147 leader = list_entry(list->next, struct perf_evsel, node);
148 evsel = list_entry(list->prev, struct perf_evsel, node);
150 leader->nr_members = evsel->idx - leader->idx + 1;
152 __evlist__for_each(list, evsel) {
153 evsel->leader = leader;
157 void perf_evlist__set_leader(struct perf_evlist *evlist)
159 if (evlist->nr_entries) {
160 evlist->nr_groups = evlist->nr_entries > 1 ? 1 : 0;
161 __perf_evlist__set_leader(&evlist->entries);
165 int perf_evlist__add_default(struct perf_evlist *evlist)
167 struct perf_event_attr attr = {
168 .type = PERF_TYPE_HARDWARE,
169 .config = PERF_COUNT_HW_CPU_CYCLES,
171 struct perf_evsel *evsel;
173 event_attr_init(&attr);
175 evsel = perf_evsel__new(&attr);
179 /* use strdup() because free(evsel) assumes name is allocated */
180 evsel->name = strdup("cycles");
184 perf_evlist__add(evlist, evsel);
187 perf_evsel__delete(evsel);
192 static int perf_evlist__add_attrs(struct perf_evlist *evlist,
193 struct perf_event_attr *attrs, size_t nr_attrs)
195 struct perf_evsel *evsel, *n;
199 for (i = 0; i < nr_attrs; i++) {
200 evsel = perf_evsel__new_idx(attrs + i, evlist->nr_entries + i);
202 goto out_delete_partial_list;
203 list_add_tail(&evsel->node, &head);
206 perf_evlist__splice_list_tail(evlist, &head, nr_attrs);
210 out_delete_partial_list:
211 __evlist__for_each_safe(&head, n, evsel)
212 perf_evsel__delete(evsel);
216 int __perf_evlist__add_default_attrs(struct perf_evlist *evlist,
217 struct perf_event_attr *attrs, size_t nr_attrs)
221 for (i = 0; i < nr_attrs; i++)
222 event_attr_init(attrs + i);
224 return perf_evlist__add_attrs(evlist, attrs, nr_attrs);
228 perf_evlist__find_tracepoint_by_id(struct perf_evlist *evlist, int id)
230 struct perf_evsel *evsel;
232 evlist__for_each(evlist, evsel) {
233 if (evsel->attr.type == PERF_TYPE_TRACEPOINT &&
234 (int)evsel->attr.config == id)
242 perf_evlist__find_tracepoint_by_name(struct perf_evlist *evlist,
245 struct perf_evsel *evsel;
247 evlist__for_each(evlist, evsel) {
248 if ((evsel->attr.type == PERF_TYPE_TRACEPOINT) &&
249 (strcmp(evsel->name, name) == 0))
256 int perf_evlist__add_newtp(struct perf_evlist *evlist,
257 const char *sys, const char *name, void *handler)
259 struct perf_evsel *evsel = perf_evsel__newtp(sys, name);
264 evsel->handler = handler;
265 perf_evlist__add(evlist, evsel);
269 static int perf_evlist__nr_threads(struct perf_evlist *evlist,
270 struct perf_evsel *evsel)
272 if (evsel->system_wide)
275 return thread_map__nr(evlist->threads);
278 void perf_evlist__disable(struct perf_evlist *evlist)
281 struct perf_evsel *pos;
282 int nr_cpus = cpu_map__nr(evlist->cpus);
285 for (cpu = 0; cpu < nr_cpus; cpu++) {
286 evlist__for_each(evlist, pos) {
287 if (!perf_evsel__is_group_leader(pos) || !pos->fd)
289 nr_threads = perf_evlist__nr_threads(evlist, pos);
290 for (thread = 0; thread < nr_threads; thread++)
291 ioctl(FD(pos, cpu, thread),
292 PERF_EVENT_IOC_DISABLE, 0);
297 void perf_evlist__enable(struct perf_evlist *evlist)
300 struct perf_evsel *pos;
301 int nr_cpus = cpu_map__nr(evlist->cpus);
304 for (cpu = 0; cpu < nr_cpus; cpu++) {
305 evlist__for_each(evlist, pos) {
306 if (!perf_evsel__is_group_leader(pos) || !pos->fd)
308 nr_threads = perf_evlist__nr_threads(evlist, pos);
309 for (thread = 0; thread < nr_threads; thread++)
310 ioctl(FD(pos, cpu, thread),
311 PERF_EVENT_IOC_ENABLE, 0);
316 int perf_evlist__disable_event(struct perf_evlist *evlist,
317 struct perf_evsel *evsel)
319 int cpu, thread, err;
320 int nr_cpus = cpu_map__nr(evlist->cpus);
321 int nr_threads = perf_evlist__nr_threads(evlist, evsel);
326 for (cpu = 0; cpu < nr_cpus; cpu++) {
327 for (thread = 0; thread < nr_threads; thread++) {
328 err = ioctl(FD(evsel, cpu, thread),
329 PERF_EVENT_IOC_DISABLE, 0);
337 int perf_evlist__enable_event(struct perf_evlist *evlist,
338 struct perf_evsel *evsel)
340 int cpu, thread, err;
341 int nr_cpus = cpu_map__nr(evlist->cpus);
342 int nr_threads = perf_evlist__nr_threads(evlist, evsel);
347 for (cpu = 0; cpu < nr_cpus; cpu++) {
348 for (thread = 0; thread < nr_threads; thread++) {
349 err = ioctl(FD(evsel, cpu, thread),
350 PERF_EVENT_IOC_ENABLE, 0);
358 static int perf_evlist__enable_event_cpu(struct perf_evlist *evlist,
359 struct perf_evsel *evsel, int cpu)
362 int nr_threads = perf_evlist__nr_threads(evlist, evsel);
367 for (thread = 0; thread < nr_threads; thread++) {
368 err = ioctl(FD(evsel, cpu, thread),
369 PERF_EVENT_IOC_ENABLE, 0);
376 static int perf_evlist__enable_event_thread(struct perf_evlist *evlist,
377 struct perf_evsel *evsel,
381 int nr_cpus = cpu_map__nr(evlist->cpus);
386 for (cpu = 0; cpu < nr_cpus; cpu++) {
387 err = ioctl(FD(evsel, cpu, thread), PERF_EVENT_IOC_ENABLE, 0);
394 int perf_evlist__enable_event_idx(struct perf_evlist *evlist,
395 struct perf_evsel *evsel, int idx)
397 bool per_cpu_mmaps = !cpu_map__empty(evlist->cpus);
400 return perf_evlist__enable_event_cpu(evlist, evsel, idx);
402 return perf_evlist__enable_event_thread(evlist, evsel, idx);
405 static int perf_evlist__alloc_pollfd(struct perf_evlist *evlist)
407 int nr_cpus = cpu_map__nr(evlist->cpus);
408 int nr_threads = thread_map__nr(evlist->threads);
410 struct perf_evsel *evsel;
412 list_for_each_entry(evsel, &evlist->entries, node) {
413 if (evsel->system_wide)
416 nfds += nr_cpus * nr_threads;
419 evlist->pollfd = malloc(sizeof(struct pollfd) * nfds);
420 return evlist->pollfd != NULL ? 0 : -ENOMEM;
423 void perf_evlist__add_pollfd(struct perf_evlist *evlist, int fd)
425 fcntl(fd, F_SETFL, O_NONBLOCK);
426 evlist->pollfd[evlist->nr_fds].fd = fd;
427 evlist->pollfd[evlist->nr_fds].events = POLLIN | POLLERR | POLLHUP;
431 int perf_evlist__filter_pollfd(struct perf_evlist *evlist, short revents_and_mask)
435 if (evlist->nr_fds == 0)
438 for (fd = 0; fd < evlist->nr_fds; ++fd) {
439 if (evlist->pollfd[fd].revents & revents_and_mask)
443 evlist->pollfd[nr_fds] = evlist->pollfd[fd];
448 evlist->nr_fds = nr_fds;
452 static void perf_evlist__id_hash(struct perf_evlist *evlist,
453 struct perf_evsel *evsel,
454 int cpu, int thread, u64 id)
457 struct perf_sample_id *sid = SID(evsel, cpu, thread);
461 hash = hash_64(sid->id, PERF_EVLIST__HLIST_BITS);
462 hlist_add_head(&sid->node, &evlist->heads[hash]);
465 void perf_evlist__id_add(struct perf_evlist *evlist, struct perf_evsel *evsel,
466 int cpu, int thread, u64 id)
468 perf_evlist__id_hash(evlist, evsel, cpu, thread, id);
469 evsel->id[evsel->ids++] = id;
472 static int perf_evlist__id_add_fd(struct perf_evlist *evlist,
473 struct perf_evsel *evsel,
474 int cpu, int thread, int fd)
476 u64 read_data[4] = { 0, };
477 int id_idx = 1; /* The first entry is the counter value */
481 ret = ioctl(fd, PERF_EVENT_IOC_ID, &id);
488 /* Legacy way to get event id.. All hail to old kernels! */
491 * This way does not work with group format read, so bail
494 if (perf_evlist__read_format(evlist) & PERF_FORMAT_GROUP)
497 if (!(evsel->attr.read_format & PERF_FORMAT_ID) ||
498 read(fd, &read_data, sizeof(read_data)) == -1)
501 if (evsel->attr.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
503 if (evsel->attr.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
506 id = read_data[id_idx];
509 perf_evlist__id_add(evlist, evsel, cpu, thread, id);
513 struct perf_sample_id *perf_evlist__id2sid(struct perf_evlist *evlist, u64 id)
515 struct hlist_head *head;
516 struct perf_sample_id *sid;
519 hash = hash_64(id, PERF_EVLIST__HLIST_BITS);
520 head = &evlist->heads[hash];
522 hlist_for_each_entry(sid, head, node)
529 struct perf_evsel *perf_evlist__id2evsel(struct perf_evlist *evlist, u64 id)
531 struct perf_sample_id *sid;
533 if (evlist->nr_entries == 1)
534 return perf_evlist__first(evlist);
536 sid = perf_evlist__id2sid(evlist, id);
540 if (!perf_evlist__sample_id_all(evlist))
541 return perf_evlist__first(evlist);
546 static int perf_evlist__event2id(struct perf_evlist *evlist,
547 union perf_event *event, u64 *id)
549 const u64 *array = event->sample.array;
552 n = (event->header.size - sizeof(event->header)) >> 3;
554 if (event->header.type == PERF_RECORD_SAMPLE) {
555 if (evlist->id_pos >= n)
557 *id = array[evlist->id_pos];
559 if (evlist->is_pos > n)
567 static struct perf_evsel *perf_evlist__event2evsel(struct perf_evlist *evlist,
568 union perf_event *event)
570 struct perf_evsel *first = perf_evlist__first(evlist);
571 struct hlist_head *head;
572 struct perf_sample_id *sid;
576 if (evlist->nr_entries == 1)
579 if (!first->attr.sample_id_all &&
580 event->header.type != PERF_RECORD_SAMPLE)
583 if (perf_evlist__event2id(evlist, event, &id))
586 /* Synthesized events have an id of zero */
590 hash = hash_64(id, PERF_EVLIST__HLIST_BITS);
591 head = &evlist->heads[hash];
593 hlist_for_each_entry(sid, head, node) {
600 union perf_event *perf_evlist__mmap_read(struct perf_evlist *evlist, int idx)
602 struct perf_mmap *md = &evlist->mmap[idx];
603 unsigned int head = perf_mmap__read_head(md);
604 unsigned int old = md->prev;
605 unsigned char *data = md->base + page_size;
606 union perf_event *event = NULL;
608 if (evlist->overwrite) {
610 * If we're further behind than half the buffer, there's a chance
611 * the writer will bite our tail and mess up the samples under us.
613 * If we somehow ended up ahead of the head, we got messed up.
615 * In either case, truncate and restart at head.
617 int diff = head - old;
618 if (diff > md->mask / 2 || diff < 0) {
619 fprintf(stderr, "WARNING: failed to keep up with mmap data.\n");
622 * head points to a known good entry, start there.
631 event = (union perf_event *)&data[old & md->mask];
632 size = event->header.size;
635 * Event straddles the mmap boundary -- header should always
636 * be inside due to u64 alignment of output.
638 if ((old & md->mask) + size != ((old + size) & md->mask)) {
639 unsigned int offset = old;
640 unsigned int len = min(sizeof(*event), size), cpy;
641 void *dst = md->event_copy;
644 cpy = min(md->mask + 1 - (offset & md->mask), len);
645 memcpy(dst, &data[offset & md->mask], cpy);
651 event = (union perf_event *) md->event_copy;
662 void perf_evlist__mmap_consume(struct perf_evlist *evlist, int idx)
664 if (!evlist->overwrite) {
665 struct perf_mmap *md = &evlist->mmap[idx];
666 unsigned int old = md->prev;
668 perf_mmap__write_tail(md, old);
672 static void __perf_evlist__munmap(struct perf_evlist *evlist, int idx)
674 if (evlist->mmap[idx].base != NULL) {
675 munmap(evlist->mmap[idx].base, evlist->mmap_len);
676 evlist->mmap[idx].base = NULL;
680 void perf_evlist__munmap(struct perf_evlist *evlist)
684 if (evlist->mmap == NULL)
687 for (i = 0; i < evlist->nr_mmaps; i++)
688 __perf_evlist__munmap(evlist, i);
690 zfree(&evlist->mmap);
693 static int perf_evlist__alloc_mmap(struct perf_evlist *evlist)
695 evlist->nr_mmaps = cpu_map__nr(evlist->cpus);
696 if (cpu_map__empty(evlist->cpus))
697 evlist->nr_mmaps = thread_map__nr(evlist->threads);
698 evlist->mmap = zalloc(evlist->nr_mmaps * sizeof(struct perf_mmap));
699 return evlist->mmap != NULL ? 0 : -ENOMEM;
707 static int __perf_evlist__mmap(struct perf_evlist *evlist, int idx,
708 struct mmap_params *mp, int fd)
710 evlist->mmap[idx].prev = 0;
711 evlist->mmap[idx].mask = mp->mask;
712 evlist->mmap[idx].base = mmap(NULL, evlist->mmap_len, mp->prot,
714 if (evlist->mmap[idx].base == MAP_FAILED) {
715 pr_debug2("failed to mmap perf event ring buffer, error %d\n",
717 evlist->mmap[idx].base = NULL;
721 perf_evlist__add_pollfd(evlist, fd);
725 static int perf_evlist__mmap_per_evsel(struct perf_evlist *evlist, int idx,
726 struct mmap_params *mp, int cpu,
727 int thread, int *output)
729 struct perf_evsel *evsel;
731 evlist__for_each(evlist, evsel) {
734 if (evsel->system_wide && thread)
737 fd = FD(evsel, cpu, thread);
741 if (__perf_evlist__mmap(evlist, idx, mp, *output) < 0)
744 if (ioctl(fd, PERF_EVENT_IOC_SET_OUTPUT, *output) != 0)
748 if ((evsel->attr.read_format & PERF_FORMAT_ID) &&
749 perf_evlist__id_add_fd(evlist, evsel, cpu, thread, fd) < 0)
756 static int perf_evlist__mmap_per_cpu(struct perf_evlist *evlist,
757 struct mmap_params *mp)
760 int nr_cpus = cpu_map__nr(evlist->cpus);
761 int nr_threads = thread_map__nr(evlist->threads);
763 pr_debug2("perf event ring buffer mmapped per cpu\n");
764 for (cpu = 0; cpu < nr_cpus; cpu++) {
767 for (thread = 0; thread < nr_threads; thread++) {
768 if (perf_evlist__mmap_per_evsel(evlist, cpu, mp, cpu,
777 for (cpu = 0; cpu < nr_cpus; cpu++)
778 __perf_evlist__munmap(evlist, cpu);
782 static int perf_evlist__mmap_per_thread(struct perf_evlist *evlist,
783 struct mmap_params *mp)
786 int nr_threads = thread_map__nr(evlist->threads);
788 pr_debug2("perf event ring buffer mmapped per thread\n");
789 for (thread = 0; thread < nr_threads; thread++) {
792 if (perf_evlist__mmap_per_evsel(evlist, thread, mp, 0, thread,
800 for (thread = 0; thread < nr_threads; thread++)
801 __perf_evlist__munmap(evlist, thread);
805 static size_t perf_evlist__mmap_size(unsigned long pages)
807 /* 512 kiB: default amount of unprivileged mlocked memory */
808 if (pages == UINT_MAX)
809 pages = (512 * 1024) / page_size;
810 else if (!is_power_of_2(pages))
813 return (pages + 1) * page_size;
816 static long parse_pages_arg(const char *str, unsigned long min,
819 unsigned long pages, val;
820 static struct parse_tag tags[] = {
821 { .tag = 'B', .mult = 1 },
822 { .tag = 'K', .mult = 1 << 10 },
823 { .tag = 'M', .mult = 1 << 20 },
824 { .tag = 'G', .mult = 1 << 30 },
831 val = parse_tag_value(str, tags);
832 if (val != (unsigned long) -1) {
833 /* we got file size value */
834 pages = PERF_ALIGN(val, page_size) / page_size;
836 /* we got pages count value */
838 pages = strtoul(str, &eptr, 10);
843 if (pages == 0 && min == 0) {
844 /* leave number of pages at 0 */
845 } else if (!is_power_of_2(pages)) {
846 /* round pages up to next power of 2 */
847 pages = next_pow2_l(pages);
850 pr_info("rounding mmap pages size to %lu bytes (%lu pages)\n",
851 pages * page_size, pages);
860 int perf_evlist__parse_mmap_pages(const struct option *opt, const char *str,
861 int unset __maybe_unused)
863 unsigned int *mmap_pages = opt->value;
864 unsigned long max = UINT_MAX;
867 if (max > SIZE_MAX / page_size)
868 max = SIZE_MAX / page_size;
870 pages = parse_pages_arg(str, 1, max);
872 pr_err("Invalid argument for --mmap_pages/-m\n");
881 * perf_evlist__mmap - Create mmaps to receive events.
882 * @evlist: list of events
883 * @pages: map length in pages
884 * @overwrite: overwrite older events?
886 * If @overwrite is %false the user needs to signal event consumption using
887 * perf_mmap__write_tail(). Using perf_evlist__mmap_read() does this
890 * Return: %0 on success, negative error code otherwise.
892 int perf_evlist__mmap(struct perf_evlist *evlist, unsigned int pages,
895 struct perf_evsel *evsel;
896 const struct cpu_map *cpus = evlist->cpus;
897 const struct thread_map *threads = evlist->threads;
898 struct mmap_params mp = {
899 .prot = PROT_READ | (overwrite ? 0 : PROT_WRITE),
902 if (evlist->mmap == NULL && perf_evlist__alloc_mmap(evlist) < 0)
905 if (evlist->pollfd == NULL && perf_evlist__alloc_pollfd(evlist) < 0)
908 evlist->overwrite = overwrite;
909 evlist->mmap_len = perf_evlist__mmap_size(pages);
910 pr_debug("mmap size %zuB\n", evlist->mmap_len);
911 mp.mask = evlist->mmap_len - page_size - 1;
913 evlist__for_each(evlist, evsel) {
914 if ((evsel->attr.read_format & PERF_FORMAT_ID) &&
915 evsel->sample_id == NULL &&
916 perf_evsel__alloc_id(evsel, cpu_map__nr(cpus), threads->nr) < 0)
920 if (cpu_map__empty(cpus))
921 return perf_evlist__mmap_per_thread(evlist, &mp);
923 return perf_evlist__mmap_per_cpu(evlist, &mp);
926 int perf_evlist__create_maps(struct perf_evlist *evlist, struct target *target)
928 evlist->threads = thread_map__new_str(target->pid, target->tid,
931 if (evlist->threads == NULL)
934 if (target__uses_dummy_map(target))
935 evlist->cpus = cpu_map__dummy_new();
937 evlist->cpus = cpu_map__new(target->cpu_list);
939 if (evlist->cpus == NULL)
940 goto out_delete_threads;
945 thread_map__delete(evlist->threads);
949 int perf_evlist__apply_filters(struct perf_evlist *evlist)
951 struct perf_evsel *evsel;
953 const int ncpus = cpu_map__nr(evlist->cpus),
954 nthreads = thread_map__nr(evlist->threads);
956 evlist__for_each(evlist, evsel) {
957 if (evsel->filter == NULL)
960 err = perf_evsel__set_filter(evsel, ncpus, nthreads, evsel->filter);
968 int perf_evlist__set_filter(struct perf_evlist *evlist, const char *filter)
970 struct perf_evsel *evsel;
972 const int ncpus = cpu_map__nr(evlist->cpus),
973 nthreads = thread_map__nr(evlist->threads);
975 evlist__for_each(evlist, evsel) {
976 err = perf_evsel__set_filter(evsel, ncpus, nthreads, filter);
984 bool perf_evlist__valid_sample_type(struct perf_evlist *evlist)
986 struct perf_evsel *pos;
988 if (evlist->nr_entries == 1)
991 if (evlist->id_pos < 0 || evlist->is_pos < 0)
994 evlist__for_each(evlist, pos) {
995 if (pos->id_pos != evlist->id_pos ||
996 pos->is_pos != evlist->is_pos)
1003 u64 __perf_evlist__combined_sample_type(struct perf_evlist *evlist)
1005 struct perf_evsel *evsel;
1007 if (evlist->combined_sample_type)
1008 return evlist->combined_sample_type;
1010 evlist__for_each(evlist, evsel)
1011 evlist->combined_sample_type |= evsel->attr.sample_type;
1013 return evlist->combined_sample_type;
1016 u64 perf_evlist__combined_sample_type(struct perf_evlist *evlist)
1018 evlist->combined_sample_type = 0;
1019 return __perf_evlist__combined_sample_type(evlist);
1022 bool perf_evlist__valid_read_format(struct perf_evlist *evlist)
1024 struct perf_evsel *first = perf_evlist__first(evlist), *pos = first;
1025 u64 read_format = first->attr.read_format;
1026 u64 sample_type = first->attr.sample_type;
1028 evlist__for_each(evlist, pos) {
1029 if (read_format != pos->attr.read_format)
1033 /* PERF_SAMPLE_READ imples PERF_FORMAT_ID. */
1034 if ((sample_type & PERF_SAMPLE_READ) &&
1035 !(read_format & PERF_FORMAT_ID)) {
1042 u64 perf_evlist__read_format(struct perf_evlist *evlist)
1044 struct perf_evsel *first = perf_evlist__first(evlist);
1045 return first->attr.read_format;
1048 u16 perf_evlist__id_hdr_size(struct perf_evlist *evlist)
1050 struct perf_evsel *first = perf_evlist__first(evlist);
1051 struct perf_sample *data;
1055 if (!first->attr.sample_id_all)
1058 sample_type = first->attr.sample_type;
1060 if (sample_type & PERF_SAMPLE_TID)
1061 size += sizeof(data->tid) * 2;
1063 if (sample_type & PERF_SAMPLE_TIME)
1064 size += sizeof(data->time);
1066 if (sample_type & PERF_SAMPLE_ID)
1067 size += sizeof(data->id);
1069 if (sample_type & PERF_SAMPLE_STREAM_ID)
1070 size += sizeof(data->stream_id);
1072 if (sample_type & PERF_SAMPLE_CPU)
1073 size += sizeof(data->cpu) * 2;
1075 if (sample_type & PERF_SAMPLE_IDENTIFIER)
1076 size += sizeof(data->id);
1081 bool perf_evlist__valid_sample_id_all(struct perf_evlist *evlist)
1083 struct perf_evsel *first = perf_evlist__first(evlist), *pos = first;
1085 evlist__for_each_continue(evlist, pos) {
1086 if (first->attr.sample_id_all != pos->attr.sample_id_all)
1093 bool perf_evlist__sample_id_all(struct perf_evlist *evlist)
1095 struct perf_evsel *first = perf_evlist__first(evlist);
1096 return first->attr.sample_id_all;
1099 void perf_evlist__set_selected(struct perf_evlist *evlist,
1100 struct perf_evsel *evsel)
1102 evlist->selected = evsel;
1105 void perf_evlist__close(struct perf_evlist *evlist)
1107 struct perf_evsel *evsel;
1108 int ncpus = cpu_map__nr(evlist->cpus);
1109 int nthreads = thread_map__nr(evlist->threads);
1112 evlist__for_each_reverse(evlist, evsel) {
1113 n = evsel->cpus ? evsel->cpus->nr : ncpus;
1114 perf_evsel__close(evsel, n, nthreads);
1118 int perf_evlist__open(struct perf_evlist *evlist)
1120 struct perf_evsel *evsel;
1123 perf_evlist__update_id_pos(evlist);
1125 evlist__for_each(evlist, evsel) {
1126 err = perf_evsel__open(evsel, evlist->cpus, evlist->threads);
1133 perf_evlist__close(evlist);
1138 int perf_evlist__prepare_workload(struct perf_evlist *evlist, struct target *target,
1139 const char *argv[], bool pipe_output,
1140 void (*exec_error)(int signo, siginfo_t *info, void *ucontext))
1142 int child_ready_pipe[2], go_pipe[2];
1145 if (pipe(child_ready_pipe) < 0) {
1146 perror("failed to create 'ready' pipe");
1150 if (pipe(go_pipe) < 0) {
1151 perror("failed to create 'go' pipe");
1152 goto out_close_ready_pipe;
1155 evlist->workload.pid = fork();
1156 if (evlist->workload.pid < 0) {
1157 perror("failed to fork");
1158 goto out_close_pipes;
1161 if (!evlist->workload.pid) {
1167 signal(SIGTERM, SIG_DFL);
1169 close(child_ready_pipe[0]);
1171 fcntl(go_pipe[0], F_SETFD, FD_CLOEXEC);
1174 * Tell the parent we're ready to go
1176 close(child_ready_pipe[1]);
1179 * Wait until the parent tells us to go.
1181 ret = read(go_pipe[0], &bf, 1);
1183 * The parent will ask for the execvp() to be performed by
1184 * writing exactly one byte, in workload.cork_fd, usually via
1185 * perf_evlist__start_workload().
1187 * For cancelling the workload without actuallin running it,
1188 * the parent will just close workload.cork_fd, without writing
1189 * anything, i.e. read will return zero and we just exit()
1194 perror("unable to read pipe");
1198 execvp(argv[0], (char **)argv);
1203 val.sival_int = errno;
1204 if (sigqueue(getppid(), SIGUSR1, val))
1212 struct sigaction act = {
1213 .sa_flags = SA_SIGINFO,
1214 .sa_sigaction = exec_error,
1216 sigaction(SIGUSR1, &act, NULL);
1219 if (target__none(target))
1220 evlist->threads->map[0] = evlist->workload.pid;
1222 close(child_ready_pipe[1]);
1225 * wait for child to settle
1227 if (read(child_ready_pipe[0], &bf, 1) == -1) {
1228 perror("unable to read pipe");
1229 goto out_close_pipes;
1232 fcntl(go_pipe[1], F_SETFD, FD_CLOEXEC);
1233 evlist->workload.cork_fd = go_pipe[1];
1234 close(child_ready_pipe[0]);
1240 out_close_ready_pipe:
1241 close(child_ready_pipe[0]);
1242 close(child_ready_pipe[1]);
1246 int perf_evlist__start_workload(struct perf_evlist *evlist)
1248 if (evlist->workload.cork_fd > 0) {
1252 * Remove the cork, let it rip!
1254 ret = write(evlist->workload.cork_fd, &bf, 1);
1256 perror("enable to write to pipe");
1258 close(evlist->workload.cork_fd);
1265 int perf_evlist__parse_sample(struct perf_evlist *evlist, union perf_event *event,
1266 struct perf_sample *sample)
1268 struct perf_evsel *evsel = perf_evlist__event2evsel(evlist, event);
1272 return perf_evsel__parse_sample(evsel, event, sample);
1275 size_t perf_evlist__fprintf(struct perf_evlist *evlist, FILE *fp)
1277 struct perf_evsel *evsel;
1280 evlist__for_each(evlist, evsel) {
1281 printed += fprintf(fp, "%s%s", evsel->idx ? ", " : "",
1282 perf_evsel__name(evsel));
1285 return printed + fprintf(fp, "\n");
1288 int perf_evlist__strerror_tp(struct perf_evlist *evlist __maybe_unused,
1289 int err, char *buf, size_t size)
1295 scnprintf(buf, size, "%s",
1296 "Error:\tUnable to find debugfs\n"
1297 "Hint:\tWas your kernel was compiled with debugfs support?\n"
1298 "Hint:\tIs the debugfs filesystem mounted?\n"
1299 "Hint:\tTry 'sudo mount -t debugfs nodev /sys/kernel/debug'");
1302 scnprintf(buf, size,
1303 "Error:\tNo permissions to read %s/tracing/events/raw_syscalls\n"
1304 "Hint:\tTry 'sudo mount -o remount,mode=755 %s'\n",
1305 debugfs_mountpoint, debugfs_mountpoint);
1308 scnprintf(buf, size, "%s", strerror_r(err, sbuf, sizeof(sbuf)));
1315 int perf_evlist__strerror_open(struct perf_evlist *evlist __maybe_unused,
1316 int err, char *buf, size_t size)
1319 char sbuf[STRERR_BUFSIZE], *emsg = strerror_r(err, sbuf, sizeof(sbuf));
1324 printed = scnprintf(buf, size,
1326 "Hint:\tCheck /proc/sys/kernel/perf_event_paranoid setting.", emsg);
1328 value = perf_event_paranoid();
1330 printed += scnprintf(buf + printed, size - printed, "\nHint:\t");
1333 printed += scnprintf(buf + printed, size - printed,
1334 "For your workloads it needs to be <= 1\nHint:\t");
1336 printed += scnprintf(buf + printed, size - printed,
1337 "For system wide tracing it needs to be set to -1.\n");
1339 printed += scnprintf(buf + printed, size - printed,
1340 "Hint:\tTry: 'sudo sh -c \"echo -1 > /proc/sys/kernel/perf_event_paranoid\"'\n"
1341 "Hint:\tThe current value is %d.", value);
1344 scnprintf(buf, size, "%s", emsg);
1351 void perf_evlist__to_front(struct perf_evlist *evlist,
1352 struct perf_evsel *move_evsel)
1354 struct perf_evsel *evsel, *n;
1357 if (move_evsel == perf_evlist__first(evlist))
1360 evlist__for_each_safe(evlist, n, evsel) {
1361 if (evsel->leader == move_evsel->leader)
1362 list_move_tail(&evsel->node, &move);
1365 list_splice(&move, &evlist->entries);
1368 void perf_evlist__set_tracking_event(struct perf_evlist *evlist,
1369 struct perf_evsel *tracking_evsel)
1371 struct perf_evsel *evsel;
1373 if (tracking_evsel->tracking)
1376 evlist__for_each(evlist, evsel) {
1377 if (evsel != tracking_evsel)
1378 evsel->tracking = false;
1381 tracking_evsel->tracking = true;