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;
126 if (!evlist->nr_entries++)
127 perf_evlist__set_id_pos(evlist);
130 void perf_evlist__splice_list_tail(struct perf_evlist *evlist,
131 struct list_head *list,
134 bool set_id_pos = !evlist->nr_entries;
136 list_splice_tail(list, &evlist->entries);
137 evlist->nr_entries += nr_entries;
139 perf_evlist__set_id_pos(evlist);
142 void __perf_evlist__set_leader(struct list_head *list)
144 struct perf_evsel *evsel, *leader;
146 leader = list_entry(list->next, struct perf_evsel, node);
147 evsel = list_entry(list->prev, struct perf_evsel, node);
149 leader->nr_members = evsel->idx - leader->idx + 1;
151 __evlist__for_each(list, evsel) {
152 evsel->leader = leader;
156 void perf_evlist__set_leader(struct perf_evlist *evlist)
158 if (evlist->nr_entries) {
159 evlist->nr_groups = evlist->nr_entries > 1 ? 1 : 0;
160 __perf_evlist__set_leader(&evlist->entries);
164 int perf_evlist__add_default(struct perf_evlist *evlist)
166 struct perf_event_attr attr = {
167 .type = PERF_TYPE_HARDWARE,
168 .config = PERF_COUNT_HW_CPU_CYCLES,
170 struct perf_evsel *evsel;
172 event_attr_init(&attr);
174 evsel = perf_evsel__new(&attr);
178 /* use strdup() because free(evsel) assumes name is allocated */
179 evsel->name = strdup("cycles");
183 perf_evlist__add(evlist, evsel);
186 perf_evsel__delete(evsel);
191 static int perf_evlist__add_attrs(struct perf_evlist *evlist,
192 struct perf_event_attr *attrs, size_t nr_attrs)
194 struct perf_evsel *evsel, *n;
198 for (i = 0; i < nr_attrs; i++) {
199 evsel = perf_evsel__new_idx(attrs + i, evlist->nr_entries + i);
201 goto out_delete_partial_list;
202 list_add_tail(&evsel->node, &head);
205 perf_evlist__splice_list_tail(evlist, &head, nr_attrs);
209 out_delete_partial_list:
210 __evlist__for_each_safe(&head, n, evsel)
211 perf_evsel__delete(evsel);
215 int __perf_evlist__add_default_attrs(struct perf_evlist *evlist,
216 struct perf_event_attr *attrs, size_t nr_attrs)
220 for (i = 0; i < nr_attrs; i++)
221 event_attr_init(attrs + i);
223 return perf_evlist__add_attrs(evlist, attrs, nr_attrs);
227 perf_evlist__find_tracepoint_by_id(struct perf_evlist *evlist, int id)
229 struct perf_evsel *evsel;
231 evlist__for_each(evlist, evsel) {
232 if (evsel->attr.type == PERF_TYPE_TRACEPOINT &&
233 (int)evsel->attr.config == id)
241 perf_evlist__find_tracepoint_by_name(struct perf_evlist *evlist,
244 struct perf_evsel *evsel;
246 evlist__for_each(evlist, evsel) {
247 if ((evsel->attr.type == PERF_TYPE_TRACEPOINT) &&
248 (strcmp(evsel->name, name) == 0))
255 int perf_evlist__add_newtp(struct perf_evlist *evlist,
256 const char *sys, const char *name, void *handler)
258 struct perf_evsel *evsel = perf_evsel__newtp(sys, name);
263 evsel->handler = handler;
264 perf_evlist__add(evlist, evsel);
268 static int perf_evlist__nr_threads(struct perf_evlist *evlist,
269 struct perf_evsel *evsel)
271 if (evsel->system_wide)
274 return thread_map__nr(evlist->threads);
277 void perf_evlist__disable(struct perf_evlist *evlist)
280 struct perf_evsel *pos;
281 int nr_cpus = cpu_map__nr(evlist->cpus);
284 for (cpu = 0; cpu < nr_cpus; cpu++) {
285 evlist__for_each(evlist, pos) {
286 if (!perf_evsel__is_group_leader(pos) || !pos->fd)
288 nr_threads = perf_evlist__nr_threads(evlist, pos);
289 for (thread = 0; thread < nr_threads; thread++)
290 ioctl(FD(pos, cpu, thread),
291 PERF_EVENT_IOC_DISABLE, 0);
296 void perf_evlist__enable(struct perf_evlist *evlist)
299 struct perf_evsel *pos;
300 int nr_cpus = cpu_map__nr(evlist->cpus);
303 for (cpu = 0; cpu < nr_cpus; cpu++) {
304 evlist__for_each(evlist, pos) {
305 if (!perf_evsel__is_group_leader(pos) || !pos->fd)
307 nr_threads = perf_evlist__nr_threads(evlist, pos);
308 for (thread = 0; thread < nr_threads; thread++)
309 ioctl(FD(pos, cpu, thread),
310 PERF_EVENT_IOC_ENABLE, 0);
315 int perf_evlist__disable_event(struct perf_evlist *evlist,
316 struct perf_evsel *evsel)
318 int cpu, thread, err;
319 int nr_cpus = cpu_map__nr(evlist->cpus);
320 int nr_threads = perf_evlist__nr_threads(evlist, evsel);
325 for (cpu = 0; cpu < nr_cpus; cpu++) {
326 for (thread = 0; thread < nr_threads; thread++) {
327 err = ioctl(FD(evsel, cpu, thread),
328 PERF_EVENT_IOC_DISABLE, 0);
336 int perf_evlist__enable_event(struct perf_evlist *evlist,
337 struct perf_evsel *evsel)
339 int cpu, thread, err;
340 int nr_cpus = cpu_map__nr(evlist->cpus);
341 int nr_threads = perf_evlist__nr_threads(evlist, evsel);
346 for (cpu = 0; cpu < nr_cpus; cpu++) {
347 for (thread = 0; thread < nr_threads; thread++) {
348 err = ioctl(FD(evsel, cpu, thread),
349 PERF_EVENT_IOC_ENABLE, 0);
357 static int perf_evlist__alloc_pollfd(struct perf_evlist *evlist)
359 int nr_cpus = cpu_map__nr(evlist->cpus);
360 int nr_threads = thread_map__nr(evlist->threads);
362 struct perf_evsel *evsel;
364 list_for_each_entry(evsel, &evlist->entries, node) {
365 if (evsel->system_wide)
368 nfds += nr_cpus * nr_threads;
371 evlist->pollfd = malloc(sizeof(struct pollfd) * nfds);
372 return evlist->pollfd != NULL ? 0 : -ENOMEM;
375 void perf_evlist__add_pollfd(struct perf_evlist *evlist, int fd)
377 fcntl(fd, F_SETFL, O_NONBLOCK);
378 evlist->pollfd[evlist->nr_fds].fd = fd;
379 evlist->pollfd[evlist->nr_fds].events = POLLIN;
383 static void perf_evlist__id_hash(struct perf_evlist *evlist,
384 struct perf_evsel *evsel,
385 int cpu, int thread, u64 id)
388 struct perf_sample_id *sid = SID(evsel, cpu, thread);
392 hash = hash_64(sid->id, PERF_EVLIST__HLIST_BITS);
393 hlist_add_head(&sid->node, &evlist->heads[hash]);
396 void perf_evlist__id_add(struct perf_evlist *evlist, struct perf_evsel *evsel,
397 int cpu, int thread, u64 id)
399 perf_evlist__id_hash(evlist, evsel, cpu, thread, id);
400 evsel->id[evsel->ids++] = id;
403 static int perf_evlist__id_add_fd(struct perf_evlist *evlist,
404 struct perf_evsel *evsel,
405 int cpu, int thread, int fd)
407 u64 read_data[4] = { 0, };
408 int id_idx = 1; /* The first entry is the counter value */
412 ret = ioctl(fd, PERF_EVENT_IOC_ID, &id);
419 /* Legacy way to get event id.. All hail to old kernels! */
422 * This way does not work with group format read, so bail
425 if (perf_evlist__read_format(evlist) & PERF_FORMAT_GROUP)
428 if (!(evsel->attr.read_format & PERF_FORMAT_ID) ||
429 read(fd, &read_data, sizeof(read_data)) == -1)
432 if (evsel->attr.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
434 if (evsel->attr.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
437 id = read_data[id_idx];
440 perf_evlist__id_add(evlist, evsel, cpu, thread, id);
444 struct perf_sample_id *perf_evlist__id2sid(struct perf_evlist *evlist, u64 id)
446 struct hlist_head *head;
447 struct perf_sample_id *sid;
450 hash = hash_64(id, PERF_EVLIST__HLIST_BITS);
451 head = &evlist->heads[hash];
453 hlist_for_each_entry(sid, head, node)
460 struct perf_evsel *perf_evlist__id2evsel(struct perf_evlist *evlist, u64 id)
462 struct perf_sample_id *sid;
464 if (evlist->nr_entries == 1)
465 return perf_evlist__first(evlist);
467 sid = perf_evlist__id2sid(evlist, id);
471 if (!perf_evlist__sample_id_all(evlist))
472 return perf_evlist__first(evlist);
477 static int perf_evlist__event2id(struct perf_evlist *evlist,
478 union perf_event *event, u64 *id)
480 const u64 *array = event->sample.array;
483 n = (event->header.size - sizeof(event->header)) >> 3;
485 if (event->header.type == PERF_RECORD_SAMPLE) {
486 if (evlist->id_pos >= n)
488 *id = array[evlist->id_pos];
490 if (evlist->is_pos > n)
498 static struct perf_evsel *perf_evlist__event2evsel(struct perf_evlist *evlist,
499 union perf_event *event)
501 struct perf_evsel *first = perf_evlist__first(evlist);
502 struct hlist_head *head;
503 struct perf_sample_id *sid;
507 if (evlist->nr_entries == 1)
510 if (!first->attr.sample_id_all &&
511 event->header.type != PERF_RECORD_SAMPLE)
514 if (perf_evlist__event2id(evlist, event, &id))
517 /* Synthesized events have an id of zero */
521 hash = hash_64(id, PERF_EVLIST__HLIST_BITS);
522 head = &evlist->heads[hash];
524 hlist_for_each_entry(sid, head, node) {
531 union perf_event *perf_evlist__mmap_read(struct perf_evlist *evlist, int idx)
533 struct perf_mmap *md = &evlist->mmap[idx];
534 unsigned int head = perf_mmap__read_head(md);
535 unsigned int old = md->prev;
536 unsigned char *data = md->base + page_size;
537 union perf_event *event = NULL;
539 if (evlist->overwrite) {
541 * If we're further behind than half the buffer, there's a chance
542 * the writer will bite our tail and mess up the samples under us.
544 * If we somehow ended up ahead of the head, we got messed up.
546 * In either case, truncate and restart at head.
548 int diff = head - old;
549 if (diff > md->mask / 2 || diff < 0) {
550 fprintf(stderr, "WARNING: failed to keep up with mmap data.\n");
553 * head points to a known good entry, start there.
562 event = (union perf_event *)&data[old & md->mask];
563 size = event->header.size;
566 * Event straddles the mmap boundary -- header should always
567 * be inside due to u64 alignment of output.
569 if ((old & md->mask) + size != ((old + size) & md->mask)) {
570 unsigned int offset = old;
571 unsigned int len = min(sizeof(*event), size), cpy;
572 void *dst = md->event_copy;
575 cpy = min(md->mask + 1 - (offset & md->mask), len);
576 memcpy(dst, &data[offset & md->mask], cpy);
582 event = (union perf_event *) md->event_copy;
593 void perf_evlist__mmap_consume(struct perf_evlist *evlist, int idx)
595 if (!evlist->overwrite) {
596 struct perf_mmap *md = &evlist->mmap[idx];
597 unsigned int old = md->prev;
599 perf_mmap__write_tail(md, old);
603 static void __perf_evlist__munmap(struct perf_evlist *evlist, int idx)
605 if (evlist->mmap[idx].base != NULL) {
606 munmap(evlist->mmap[idx].base, evlist->mmap_len);
607 evlist->mmap[idx].base = NULL;
611 void perf_evlist__munmap(struct perf_evlist *evlist)
615 if (evlist->mmap == NULL)
618 for (i = 0; i < evlist->nr_mmaps; i++)
619 __perf_evlist__munmap(evlist, i);
621 zfree(&evlist->mmap);
624 static int perf_evlist__alloc_mmap(struct perf_evlist *evlist)
626 evlist->nr_mmaps = cpu_map__nr(evlist->cpus);
627 if (cpu_map__empty(evlist->cpus))
628 evlist->nr_mmaps = thread_map__nr(evlist->threads);
629 evlist->mmap = zalloc(evlist->nr_mmaps * sizeof(struct perf_mmap));
630 return evlist->mmap != NULL ? 0 : -ENOMEM;
638 static int __perf_evlist__mmap(struct perf_evlist *evlist, int idx,
639 struct mmap_params *mp, int fd)
641 evlist->mmap[idx].prev = 0;
642 evlist->mmap[idx].mask = mp->mask;
643 evlist->mmap[idx].base = mmap(NULL, evlist->mmap_len, mp->prot,
645 if (evlist->mmap[idx].base == MAP_FAILED) {
646 pr_debug2("failed to mmap perf event ring buffer, error %d\n",
648 evlist->mmap[idx].base = NULL;
652 perf_evlist__add_pollfd(evlist, fd);
656 static int perf_evlist__mmap_per_evsel(struct perf_evlist *evlist, int idx,
657 struct mmap_params *mp, int cpu,
658 int thread, int *output)
660 struct perf_evsel *evsel;
662 evlist__for_each(evlist, evsel) {
665 if (evsel->system_wide && thread)
668 fd = FD(evsel, cpu, thread);
672 if (__perf_evlist__mmap(evlist, idx, mp, *output) < 0)
675 if (ioctl(fd, PERF_EVENT_IOC_SET_OUTPUT, *output) != 0)
679 if ((evsel->attr.read_format & PERF_FORMAT_ID) &&
680 perf_evlist__id_add_fd(evlist, evsel, cpu, thread, fd) < 0)
687 static int perf_evlist__mmap_per_cpu(struct perf_evlist *evlist,
688 struct mmap_params *mp)
691 int nr_cpus = cpu_map__nr(evlist->cpus);
692 int nr_threads = thread_map__nr(evlist->threads);
694 pr_debug2("perf event ring buffer mmapped per cpu\n");
695 for (cpu = 0; cpu < nr_cpus; cpu++) {
698 for (thread = 0; thread < nr_threads; thread++) {
699 if (perf_evlist__mmap_per_evsel(evlist, cpu, mp, cpu,
708 for (cpu = 0; cpu < nr_cpus; cpu++)
709 __perf_evlist__munmap(evlist, cpu);
713 static int perf_evlist__mmap_per_thread(struct perf_evlist *evlist,
714 struct mmap_params *mp)
717 int nr_threads = thread_map__nr(evlist->threads);
719 pr_debug2("perf event ring buffer mmapped per thread\n");
720 for (thread = 0; thread < nr_threads; thread++) {
723 if (perf_evlist__mmap_per_evsel(evlist, thread, mp, 0, thread,
731 for (thread = 0; thread < nr_threads; thread++)
732 __perf_evlist__munmap(evlist, thread);
736 static size_t perf_evlist__mmap_size(unsigned long pages)
738 /* 512 kiB: default amount of unprivileged mlocked memory */
739 if (pages == UINT_MAX)
740 pages = (512 * 1024) / page_size;
741 else if (!is_power_of_2(pages))
744 return (pages + 1) * page_size;
747 static long parse_pages_arg(const char *str, unsigned long min,
750 unsigned long pages, val;
751 static struct parse_tag tags[] = {
752 { .tag = 'B', .mult = 1 },
753 { .tag = 'K', .mult = 1 << 10 },
754 { .tag = 'M', .mult = 1 << 20 },
755 { .tag = 'G', .mult = 1 << 30 },
762 val = parse_tag_value(str, tags);
763 if (val != (unsigned long) -1) {
764 /* we got file size value */
765 pages = PERF_ALIGN(val, page_size) / page_size;
767 /* we got pages count value */
769 pages = strtoul(str, &eptr, 10);
774 if (pages == 0 && min == 0) {
775 /* leave number of pages at 0 */
776 } else if (!is_power_of_2(pages)) {
777 /* round pages up to next power of 2 */
778 pages = next_pow2_l(pages);
781 pr_info("rounding mmap pages size to %lu bytes (%lu pages)\n",
782 pages * page_size, pages);
791 int perf_evlist__parse_mmap_pages(const struct option *opt, const char *str,
792 int unset __maybe_unused)
794 unsigned int *mmap_pages = opt->value;
795 unsigned long max = UINT_MAX;
798 if (max > SIZE_MAX / page_size)
799 max = SIZE_MAX / page_size;
801 pages = parse_pages_arg(str, 1, max);
803 pr_err("Invalid argument for --mmap_pages/-m\n");
812 * perf_evlist__mmap - Create mmaps to receive events.
813 * @evlist: list of events
814 * @pages: map length in pages
815 * @overwrite: overwrite older events?
817 * If @overwrite is %false the user needs to signal event consumption using
818 * perf_mmap__write_tail(). Using perf_evlist__mmap_read() does this
821 * Return: %0 on success, negative error code otherwise.
823 int perf_evlist__mmap(struct perf_evlist *evlist, unsigned int pages,
826 struct perf_evsel *evsel;
827 const struct cpu_map *cpus = evlist->cpus;
828 const struct thread_map *threads = evlist->threads;
829 struct mmap_params mp = {
830 .prot = PROT_READ | (overwrite ? 0 : PROT_WRITE),
833 if (evlist->mmap == NULL && perf_evlist__alloc_mmap(evlist) < 0)
836 if (evlist->pollfd == NULL && perf_evlist__alloc_pollfd(evlist) < 0)
839 evlist->overwrite = overwrite;
840 evlist->mmap_len = perf_evlist__mmap_size(pages);
841 pr_debug("mmap size %zuB\n", evlist->mmap_len);
842 mp.mask = evlist->mmap_len - page_size - 1;
844 evlist__for_each(evlist, evsel) {
845 if ((evsel->attr.read_format & PERF_FORMAT_ID) &&
846 evsel->sample_id == NULL &&
847 perf_evsel__alloc_id(evsel, cpu_map__nr(cpus), threads->nr) < 0)
851 if (cpu_map__empty(cpus))
852 return perf_evlist__mmap_per_thread(evlist, &mp);
854 return perf_evlist__mmap_per_cpu(evlist, &mp);
857 int perf_evlist__create_maps(struct perf_evlist *evlist, struct target *target)
859 evlist->threads = thread_map__new_str(target->pid, target->tid,
862 if (evlist->threads == NULL)
865 if (target__uses_dummy_map(target))
866 evlist->cpus = cpu_map__dummy_new();
868 evlist->cpus = cpu_map__new(target->cpu_list);
870 if (evlist->cpus == NULL)
871 goto out_delete_threads;
876 thread_map__delete(evlist->threads);
880 int perf_evlist__apply_filters(struct perf_evlist *evlist)
882 struct perf_evsel *evsel;
884 const int ncpus = cpu_map__nr(evlist->cpus),
885 nthreads = thread_map__nr(evlist->threads);
887 evlist__for_each(evlist, evsel) {
888 if (evsel->filter == NULL)
891 err = perf_evsel__set_filter(evsel, ncpus, nthreads, evsel->filter);
899 int perf_evlist__set_filter(struct perf_evlist *evlist, const char *filter)
901 struct perf_evsel *evsel;
903 const int ncpus = cpu_map__nr(evlist->cpus),
904 nthreads = thread_map__nr(evlist->threads);
906 evlist__for_each(evlist, evsel) {
907 err = perf_evsel__set_filter(evsel, ncpus, nthreads, filter);
915 bool perf_evlist__valid_sample_type(struct perf_evlist *evlist)
917 struct perf_evsel *pos;
919 if (evlist->nr_entries == 1)
922 if (evlist->id_pos < 0 || evlist->is_pos < 0)
925 evlist__for_each(evlist, pos) {
926 if (pos->id_pos != evlist->id_pos ||
927 pos->is_pos != evlist->is_pos)
934 u64 __perf_evlist__combined_sample_type(struct perf_evlist *evlist)
936 struct perf_evsel *evsel;
938 if (evlist->combined_sample_type)
939 return evlist->combined_sample_type;
941 evlist__for_each(evlist, evsel)
942 evlist->combined_sample_type |= evsel->attr.sample_type;
944 return evlist->combined_sample_type;
947 u64 perf_evlist__combined_sample_type(struct perf_evlist *evlist)
949 evlist->combined_sample_type = 0;
950 return __perf_evlist__combined_sample_type(evlist);
953 bool perf_evlist__valid_read_format(struct perf_evlist *evlist)
955 struct perf_evsel *first = perf_evlist__first(evlist), *pos = first;
956 u64 read_format = first->attr.read_format;
957 u64 sample_type = first->attr.sample_type;
959 evlist__for_each(evlist, pos) {
960 if (read_format != pos->attr.read_format)
964 /* PERF_SAMPLE_READ imples PERF_FORMAT_ID. */
965 if ((sample_type & PERF_SAMPLE_READ) &&
966 !(read_format & PERF_FORMAT_ID)) {
973 u64 perf_evlist__read_format(struct perf_evlist *evlist)
975 struct perf_evsel *first = perf_evlist__first(evlist);
976 return first->attr.read_format;
979 u16 perf_evlist__id_hdr_size(struct perf_evlist *evlist)
981 struct perf_evsel *first = perf_evlist__first(evlist);
982 struct perf_sample *data;
986 if (!first->attr.sample_id_all)
989 sample_type = first->attr.sample_type;
991 if (sample_type & PERF_SAMPLE_TID)
992 size += sizeof(data->tid) * 2;
994 if (sample_type & PERF_SAMPLE_TIME)
995 size += sizeof(data->time);
997 if (sample_type & PERF_SAMPLE_ID)
998 size += sizeof(data->id);
1000 if (sample_type & PERF_SAMPLE_STREAM_ID)
1001 size += sizeof(data->stream_id);
1003 if (sample_type & PERF_SAMPLE_CPU)
1004 size += sizeof(data->cpu) * 2;
1006 if (sample_type & PERF_SAMPLE_IDENTIFIER)
1007 size += sizeof(data->id);
1012 bool perf_evlist__valid_sample_id_all(struct perf_evlist *evlist)
1014 struct perf_evsel *first = perf_evlist__first(evlist), *pos = first;
1016 evlist__for_each_continue(evlist, pos) {
1017 if (first->attr.sample_id_all != pos->attr.sample_id_all)
1024 bool perf_evlist__sample_id_all(struct perf_evlist *evlist)
1026 struct perf_evsel *first = perf_evlist__first(evlist);
1027 return first->attr.sample_id_all;
1030 void perf_evlist__set_selected(struct perf_evlist *evlist,
1031 struct perf_evsel *evsel)
1033 evlist->selected = evsel;
1036 void perf_evlist__close(struct perf_evlist *evlist)
1038 struct perf_evsel *evsel;
1039 int ncpus = cpu_map__nr(evlist->cpus);
1040 int nthreads = thread_map__nr(evlist->threads);
1043 evlist__for_each_reverse(evlist, evsel) {
1044 n = evsel->cpus ? evsel->cpus->nr : ncpus;
1045 perf_evsel__close(evsel, n, nthreads);
1049 int perf_evlist__open(struct perf_evlist *evlist)
1051 struct perf_evsel *evsel;
1054 perf_evlist__update_id_pos(evlist);
1056 evlist__for_each(evlist, evsel) {
1057 err = perf_evsel__open(evsel, evlist->cpus, evlist->threads);
1064 perf_evlist__close(evlist);
1069 int perf_evlist__prepare_workload(struct perf_evlist *evlist, struct target *target,
1070 const char *argv[], bool pipe_output,
1071 void (*exec_error)(int signo, siginfo_t *info, void *ucontext))
1073 int child_ready_pipe[2], go_pipe[2];
1076 if (pipe(child_ready_pipe) < 0) {
1077 perror("failed to create 'ready' pipe");
1081 if (pipe(go_pipe) < 0) {
1082 perror("failed to create 'go' pipe");
1083 goto out_close_ready_pipe;
1086 evlist->workload.pid = fork();
1087 if (evlist->workload.pid < 0) {
1088 perror("failed to fork");
1089 goto out_close_pipes;
1092 if (!evlist->workload.pid) {
1098 signal(SIGTERM, SIG_DFL);
1100 close(child_ready_pipe[0]);
1102 fcntl(go_pipe[0], F_SETFD, FD_CLOEXEC);
1105 * Tell the parent we're ready to go
1107 close(child_ready_pipe[1]);
1110 * Wait until the parent tells us to go.
1112 ret = read(go_pipe[0], &bf, 1);
1114 * The parent will ask for the execvp() to be performed by
1115 * writing exactly one byte, in workload.cork_fd, usually via
1116 * perf_evlist__start_workload().
1118 * For cancelling the workload without actuallin running it,
1119 * the parent will just close workload.cork_fd, without writing
1120 * anything, i.e. read will return zero and we just exit()
1125 perror("unable to read pipe");
1129 execvp(argv[0], (char **)argv);
1134 val.sival_int = errno;
1135 if (sigqueue(getppid(), SIGUSR1, val))
1143 struct sigaction act = {
1144 .sa_flags = SA_SIGINFO,
1145 .sa_sigaction = exec_error,
1147 sigaction(SIGUSR1, &act, NULL);
1150 if (target__none(target))
1151 evlist->threads->map[0] = evlist->workload.pid;
1153 close(child_ready_pipe[1]);
1156 * wait for child to settle
1158 if (read(child_ready_pipe[0], &bf, 1) == -1) {
1159 perror("unable to read pipe");
1160 goto out_close_pipes;
1163 fcntl(go_pipe[1], F_SETFD, FD_CLOEXEC);
1164 evlist->workload.cork_fd = go_pipe[1];
1165 close(child_ready_pipe[0]);
1171 out_close_ready_pipe:
1172 close(child_ready_pipe[0]);
1173 close(child_ready_pipe[1]);
1177 int perf_evlist__start_workload(struct perf_evlist *evlist)
1179 if (evlist->workload.cork_fd > 0) {
1183 * Remove the cork, let it rip!
1185 ret = write(evlist->workload.cork_fd, &bf, 1);
1187 perror("enable to write to pipe");
1189 close(evlist->workload.cork_fd);
1196 int perf_evlist__parse_sample(struct perf_evlist *evlist, union perf_event *event,
1197 struct perf_sample *sample)
1199 struct perf_evsel *evsel = perf_evlist__event2evsel(evlist, event);
1203 return perf_evsel__parse_sample(evsel, event, sample);
1206 size_t perf_evlist__fprintf(struct perf_evlist *evlist, FILE *fp)
1208 struct perf_evsel *evsel;
1211 evlist__for_each(evlist, evsel) {
1212 printed += fprintf(fp, "%s%s", evsel->idx ? ", " : "",
1213 perf_evsel__name(evsel));
1216 return printed + fprintf(fp, "\n");
1219 int perf_evlist__strerror_tp(struct perf_evlist *evlist __maybe_unused,
1220 int err, char *buf, size_t size)
1226 scnprintf(buf, size, "%s",
1227 "Error:\tUnable to find debugfs\n"
1228 "Hint:\tWas your kernel was compiled with debugfs support?\n"
1229 "Hint:\tIs the debugfs filesystem mounted?\n"
1230 "Hint:\tTry 'sudo mount -t debugfs nodev /sys/kernel/debug'");
1233 scnprintf(buf, size,
1234 "Error:\tNo permissions to read %s/tracing/events/raw_syscalls\n"
1235 "Hint:\tTry 'sudo mount -o remount,mode=755 %s'\n",
1236 debugfs_mountpoint, debugfs_mountpoint);
1239 scnprintf(buf, size, "%s", strerror_r(err, sbuf, sizeof(sbuf)));
1246 int perf_evlist__strerror_open(struct perf_evlist *evlist __maybe_unused,
1247 int err, char *buf, size_t size)
1250 char sbuf[128], *emsg = strerror_r(err, sbuf, sizeof(sbuf));
1255 printed = scnprintf(buf, size,
1257 "Hint:\tCheck /proc/sys/kernel/perf_event_paranoid setting.", emsg);
1259 value = perf_event_paranoid();
1261 printed += scnprintf(buf + printed, size - printed, "\nHint:\t");
1264 printed += scnprintf(buf + printed, size - printed,
1265 "For your workloads it needs to be <= 1\nHint:\t");
1267 printed += scnprintf(buf + printed, size - printed,
1268 "For system wide tracing it needs to be set to -1.\n");
1270 printed += scnprintf(buf + printed, size - printed,
1271 "Hint:\tTry: 'sudo sh -c \"echo -1 > /proc/sys/kernel/perf_event_paranoid\"'\n"
1272 "Hint:\tThe current value is %d.", value);
1275 scnprintf(buf, size, "%s", emsg);
1282 void perf_evlist__to_front(struct perf_evlist *evlist,
1283 struct perf_evsel *move_evsel)
1285 struct perf_evsel *evsel, *n;
1288 if (move_evsel == perf_evlist__first(evlist))
1291 evlist__for_each_safe(evlist, n, evsel) {
1292 if (evsel->leader == move_evsel->leader)
1293 list_move_tail(&evsel->node, &move);
1296 list_splice(&move, &evlist->entries);