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 list_for_each_entry(evsel, &evlist->entries, node)
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 list_for_each_entry_safe(pos, n, &evlist->entries, node) {
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 cpu_map__delete(evlist->cpus);
111 thread_map__delete(evlist->threads);
113 evlist->threads = NULL;
114 perf_evlist__purge(evlist);
115 perf_evlist__exit(evlist);
119 void perf_evlist__add(struct perf_evlist *evlist, struct perf_evsel *entry)
121 list_add_tail(&entry->node, &evlist->entries);
122 entry->idx = evlist->nr_entries;
124 if (!evlist->nr_entries++)
125 perf_evlist__set_id_pos(evlist);
128 void perf_evlist__splice_list_tail(struct perf_evlist *evlist,
129 struct list_head *list,
132 bool set_id_pos = !evlist->nr_entries;
134 list_splice_tail(list, &evlist->entries);
135 evlist->nr_entries += nr_entries;
137 perf_evlist__set_id_pos(evlist);
140 void __perf_evlist__set_leader(struct list_head *list)
142 struct perf_evsel *evsel, *leader;
144 leader = list_entry(list->next, struct perf_evsel, node);
145 evsel = list_entry(list->prev, struct perf_evsel, node);
147 leader->nr_members = evsel->idx - leader->idx + 1;
149 list_for_each_entry(evsel, list, node) {
150 evsel->leader = leader;
154 void perf_evlist__set_leader(struct perf_evlist *evlist)
156 if (evlist->nr_entries) {
157 evlist->nr_groups = evlist->nr_entries > 1 ? 1 : 0;
158 __perf_evlist__set_leader(&evlist->entries);
162 int perf_evlist__add_default(struct perf_evlist *evlist)
164 struct perf_event_attr attr = {
165 .type = PERF_TYPE_HARDWARE,
166 .config = PERF_COUNT_HW_CPU_CYCLES,
168 struct perf_evsel *evsel;
170 event_attr_init(&attr);
172 evsel = perf_evsel__new(&attr);
176 /* use strdup() because free(evsel) assumes name is allocated */
177 evsel->name = strdup("cycles");
181 perf_evlist__add(evlist, evsel);
184 perf_evsel__delete(evsel);
189 static int perf_evlist__add_attrs(struct perf_evlist *evlist,
190 struct perf_event_attr *attrs, size_t nr_attrs)
192 struct perf_evsel *evsel, *n;
196 for (i = 0; i < nr_attrs; i++) {
197 evsel = perf_evsel__new_idx(attrs + i, evlist->nr_entries + i);
199 goto out_delete_partial_list;
200 list_add_tail(&evsel->node, &head);
203 perf_evlist__splice_list_tail(evlist, &head, nr_attrs);
207 out_delete_partial_list:
208 list_for_each_entry_safe(evsel, n, &head, node)
209 perf_evsel__delete(evsel);
213 int __perf_evlist__add_default_attrs(struct perf_evlist *evlist,
214 struct perf_event_attr *attrs, size_t nr_attrs)
218 for (i = 0; i < nr_attrs; i++)
219 event_attr_init(attrs + i);
221 return perf_evlist__add_attrs(evlist, attrs, nr_attrs);
225 perf_evlist__find_tracepoint_by_id(struct perf_evlist *evlist, int id)
227 struct perf_evsel *evsel;
229 list_for_each_entry(evsel, &evlist->entries, node) {
230 if (evsel->attr.type == PERF_TYPE_TRACEPOINT &&
231 (int)evsel->attr.config == id)
239 perf_evlist__find_tracepoint_by_name(struct perf_evlist *evlist,
242 struct perf_evsel *evsel;
244 list_for_each_entry(evsel, &evlist->entries, node) {
245 if ((evsel->attr.type == PERF_TYPE_TRACEPOINT) &&
246 (strcmp(evsel->name, name) == 0))
253 int perf_evlist__add_newtp(struct perf_evlist *evlist,
254 const char *sys, const char *name, void *handler)
256 struct perf_evsel *evsel = perf_evsel__newtp(sys, name);
261 evsel->handler = handler;
262 perf_evlist__add(evlist, evsel);
266 void perf_evlist__disable(struct perf_evlist *evlist)
269 struct perf_evsel *pos;
270 int nr_cpus = cpu_map__nr(evlist->cpus);
271 int nr_threads = thread_map__nr(evlist->threads);
273 for (cpu = 0; cpu < nr_cpus; cpu++) {
274 list_for_each_entry(pos, &evlist->entries, node) {
275 if (!perf_evsel__is_group_leader(pos) || !pos->fd)
277 for (thread = 0; thread < nr_threads; thread++)
278 ioctl(FD(pos, cpu, thread),
279 PERF_EVENT_IOC_DISABLE, 0);
284 void perf_evlist__enable(struct perf_evlist *evlist)
287 struct perf_evsel *pos;
288 int nr_cpus = cpu_map__nr(evlist->cpus);
289 int nr_threads = thread_map__nr(evlist->threads);
291 for (cpu = 0; cpu < nr_cpus; cpu++) {
292 list_for_each_entry(pos, &evlist->entries, node) {
293 if (!perf_evsel__is_group_leader(pos) || !pos->fd)
295 for (thread = 0; thread < nr_threads; thread++)
296 ioctl(FD(pos, cpu, thread),
297 PERF_EVENT_IOC_ENABLE, 0);
302 int perf_evlist__disable_event(struct perf_evlist *evlist,
303 struct perf_evsel *evsel)
305 int cpu, thread, err;
310 for (cpu = 0; cpu < evlist->cpus->nr; cpu++) {
311 for (thread = 0; thread < evlist->threads->nr; thread++) {
312 err = ioctl(FD(evsel, cpu, thread),
313 PERF_EVENT_IOC_DISABLE, 0);
321 int perf_evlist__enable_event(struct perf_evlist *evlist,
322 struct perf_evsel *evsel)
324 int cpu, thread, err;
329 for (cpu = 0; cpu < evlist->cpus->nr; cpu++) {
330 for (thread = 0; thread < evlist->threads->nr; thread++) {
331 err = ioctl(FD(evsel, cpu, thread),
332 PERF_EVENT_IOC_ENABLE, 0);
340 static int perf_evlist__alloc_pollfd(struct perf_evlist *evlist)
342 int nr_cpus = cpu_map__nr(evlist->cpus);
343 int nr_threads = thread_map__nr(evlist->threads);
344 int nfds = nr_cpus * nr_threads * evlist->nr_entries;
345 evlist->pollfd = malloc(sizeof(struct pollfd) * nfds);
346 return evlist->pollfd != NULL ? 0 : -ENOMEM;
349 void perf_evlist__add_pollfd(struct perf_evlist *evlist, int fd)
351 fcntl(fd, F_SETFL, O_NONBLOCK);
352 evlist->pollfd[evlist->nr_fds].fd = fd;
353 evlist->pollfd[evlist->nr_fds].events = POLLIN;
357 static void perf_evlist__id_hash(struct perf_evlist *evlist,
358 struct perf_evsel *evsel,
359 int cpu, int thread, u64 id)
362 struct perf_sample_id *sid = SID(evsel, cpu, thread);
366 hash = hash_64(sid->id, PERF_EVLIST__HLIST_BITS);
367 hlist_add_head(&sid->node, &evlist->heads[hash]);
370 void perf_evlist__id_add(struct perf_evlist *evlist, struct perf_evsel *evsel,
371 int cpu, int thread, u64 id)
373 perf_evlist__id_hash(evlist, evsel, cpu, thread, id);
374 evsel->id[evsel->ids++] = id;
377 static int perf_evlist__id_add_fd(struct perf_evlist *evlist,
378 struct perf_evsel *evsel,
379 int cpu, int thread, int fd)
381 u64 read_data[4] = { 0, };
382 int id_idx = 1; /* The first entry is the counter value */
386 ret = ioctl(fd, PERF_EVENT_IOC_ID, &id);
393 /* Legacy way to get event id.. All hail to old kernels! */
396 * This way does not work with group format read, so bail
399 if (perf_evlist__read_format(evlist) & PERF_FORMAT_GROUP)
402 if (!(evsel->attr.read_format & PERF_FORMAT_ID) ||
403 read(fd, &read_data, sizeof(read_data)) == -1)
406 if (evsel->attr.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
408 if (evsel->attr.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
411 id = read_data[id_idx];
414 perf_evlist__id_add(evlist, evsel, cpu, thread, id);
418 struct perf_sample_id *perf_evlist__id2sid(struct perf_evlist *evlist, u64 id)
420 struct hlist_head *head;
421 struct perf_sample_id *sid;
424 hash = hash_64(id, PERF_EVLIST__HLIST_BITS);
425 head = &evlist->heads[hash];
427 hlist_for_each_entry(sid, head, node)
434 struct perf_evsel *perf_evlist__id2evsel(struct perf_evlist *evlist, u64 id)
436 struct perf_sample_id *sid;
438 if (evlist->nr_entries == 1)
439 return perf_evlist__first(evlist);
441 sid = perf_evlist__id2sid(evlist, id);
445 if (!perf_evlist__sample_id_all(evlist))
446 return perf_evlist__first(evlist);
451 static int perf_evlist__event2id(struct perf_evlist *evlist,
452 union perf_event *event, u64 *id)
454 const u64 *array = event->sample.array;
457 n = (event->header.size - sizeof(event->header)) >> 3;
459 if (event->header.type == PERF_RECORD_SAMPLE) {
460 if (evlist->id_pos >= n)
462 *id = array[evlist->id_pos];
464 if (evlist->is_pos > n)
472 static struct perf_evsel *perf_evlist__event2evsel(struct perf_evlist *evlist,
473 union perf_event *event)
475 struct perf_evsel *first = perf_evlist__first(evlist);
476 struct hlist_head *head;
477 struct perf_sample_id *sid;
481 if (evlist->nr_entries == 1)
484 if (!first->attr.sample_id_all &&
485 event->header.type != PERF_RECORD_SAMPLE)
488 if (perf_evlist__event2id(evlist, event, &id))
491 /* Synthesized events have an id of zero */
495 hash = hash_64(id, PERF_EVLIST__HLIST_BITS);
496 head = &evlist->heads[hash];
498 hlist_for_each_entry(sid, head, node) {
505 union perf_event *perf_evlist__mmap_read(struct perf_evlist *evlist, int idx)
507 struct perf_mmap *md = &evlist->mmap[idx];
508 unsigned int head = perf_mmap__read_head(md);
509 unsigned int old = md->prev;
510 unsigned char *data = md->base + page_size;
511 union perf_event *event = NULL;
513 if (evlist->overwrite) {
515 * If we're further behind than half the buffer, there's a chance
516 * the writer will bite our tail and mess up the samples under us.
518 * If we somehow ended up ahead of the head, we got messed up.
520 * In either case, truncate and restart at head.
522 int diff = head - old;
523 if (diff > md->mask / 2 || diff < 0) {
524 fprintf(stderr, "WARNING: failed to keep up with mmap data.\n");
527 * head points to a known good entry, start there.
536 event = (union perf_event *)&data[old & md->mask];
537 size = event->header.size;
540 * Event straddles the mmap boundary -- header should always
541 * be inside due to u64 alignment of output.
543 if ((old & md->mask) + size != ((old + size) & md->mask)) {
544 unsigned int offset = old;
545 unsigned int len = min(sizeof(*event), size), cpy;
546 void *dst = md->event_copy;
549 cpy = min(md->mask + 1 - (offset & md->mask), len);
550 memcpy(dst, &data[offset & md->mask], cpy);
556 event = (union perf_event *) md->event_copy;
567 void perf_evlist__mmap_consume(struct perf_evlist *evlist, int idx)
569 if (!evlist->overwrite) {
570 struct perf_mmap *md = &evlist->mmap[idx];
571 unsigned int old = md->prev;
573 perf_mmap__write_tail(md, old);
577 static void __perf_evlist__munmap(struct perf_evlist *evlist, int idx)
579 if (evlist->mmap[idx].base != NULL) {
580 munmap(evlist->mmap[idx].base, evlist->mmap_len);
581 evlist->mmap[idx].base = NULL;
585 void perf_evlist__munmap(struct perf_evlist *evlist)
589 for (i = 0; i < evlist->nr_mmaps; i++)
590 __perf_evlist__munmap(evlist, i);
592 zfree(&evlist->mmap);
595 static int perf_evlist__alloc_mmap(struct perf_evlist *evlist)
597 evlist->nr_mmaps = cpu_map__nr(evlist->cpus);
598 if (cpu_map__empty(evlist->cpus))
599 evlist->nr_mmaps = thread_map__nr(evlist->threads);
600 evlist->mmap = zalloc(evlist->nr_mmaps * sizeof(struct perf_mmap));
601 return evlist->mmap != NULL ? 0 : -ENOMEM;
604 static int __perf_evlist__mmap(struct perf_evlist *evlist,
605 int idx, int prot, int mask, int fd)
607 evlist->mmap[idx].prev = 0;
608 evlist->mmap[idx].mask = mask;
609 evlist->mmap[idx].base = mmap(NULL, evlist->mmap_len, prot,
611 if (evlist->mmap[idx].base == MAP_FAILED) {
612 pr_debug2("failed to mmap perf event ring buffer, error %d\n",
614 evlist->mmap[idx].base = NULL;
618 perf_evlist__add_pollfd(evlist, fd);
622 static int perf_evlist__mmap_per_evsel(struct perf_evlist *evlist, int idx,
623 int prot, int mask, int cpu, int thread,
626 struct perf_evsel *evsel;
628 list_for_each_entry(evsel, &evlist->entries, node) {
629 int fd = FD(evsel, cpu, thread);
633 if (__perf_evlist__mmap(evlist, idx, prot, mask,
637 if (ioctl(fd, PERF_EVENT_IOC_SET_OUTPUT, *output) != 0)
641 if ((evsel->attr.read_format & PERF_FORMAT_ID) &&
642 perf_evlist__id_add_fd(evlist, evsel, cpu, thread, fd) < 0)
649 static int perf_evlist__mmap_per_cpu(struct perf_evlist *evlist, int prot,
653 int nr_cpus = cpu_map__nr(evlist->cpus);
654 int nr_threads = thread_map__nr(evlist->threads);
656 pr_debug2("perf event ring buffer mmapped per cpu\n");
657 for (cpu = 0; cpu < nr_cpus; cpu++) {
660 for (thread = 0; thread < nr_threads; thread++) {
661 if (perf_evlist__mmap_per_evsel(evlist, cpu, prot, mask,
662 cpu, thread, &output))
670 for (cpu = 0; cpu < nr_cpus; cpu++)
671 __perf_evlist__munmap(evlist, cpu);
675 static int perf_evlist__mmap_per_thread(struct perf_evlist *evlist, int prot,
679 int nr_threads = thread_map__nr(evlist->threads);
681 pr_debug2("perf event ring buffer mmapped per thread\n");
682 for (thread = 0; thread < nr_threads; thread++) {
685 if (perf_evlist__mmap_per_evsel(evlist, thread, prot, mask, 0,
693 for (thread = 0; thread < nr_threads; thread++)
694 __perf_evlist__munmap(evlist, thread);
698 static size_t perf_evlist__mmap_size(unsigned long pages)
700 /* 512 kiB: default amount of unprivileged mlocked memory */
701 if (pages == UINT_MAX)
702 pages = (512 * 1024) / page_size;
703 else if (!is_power_of_2(pages))
706 return (pages + 1) * page_size;
709 static long parse_pages_arg(const char *str, unsigned long min,
712 unsigned long pages, val;
713 static struct parse_tag tags[] = {
714 { .tag = 'B', .mult = 1 },
715 { .tag = 'K', .mult = 1 << 10 },
716 { .tag = 'M', .mult = 1 << 20 },
717 { .tag = 'G', .mult = 1 << 30 },
724 val = parse_tag_value(str, tags);
725 if (val != (unsigned long) -1) {
726 /* we got file size value */
727 pages = PERF_ALIGN(val, page_size) / page_size;
729 /* we got pages count value */
731 pages = strtoul(str, &eptr, 10);
736 if (pages == 0 && min == 0) {
737 /* leave number of pages at 0 */
738 } else if (!is_power_of_2(pages)) {
739 /* round pages up to next power of 2 */
740 pages = next_pow2_l(pages);
743 pr_info("rounding mmap pages size to %lu bytes (%lu pages)\n",
744 pages * page_size, pages);
753 int perf_evlist__parse_mmap_pages(const struct option *opt, const char *str,
754 int unset __maybe_unused)
756 unsigned int *mmap_pages = opt->value;
757 unsigned long max = UINT_MAX;
760 if (max > SIZE_MAX / page_size)
761 max = SIZE_MAX / page_size;
763 pages = parse_pages_arg(str, 1, max);
765 pr_err("Invalid argument for --mmap_pages/-m\n");
774 * perf_evlist__mmap - Create mmaps to receive events.
775 * @evlist: list of events
776 * @pages: map length in pages
777 * @overwrite: overwrite older events?
779 * If @overwrite is %false the user needs to signal event consumption using
780 * perf_mmap__write_tail(). Using perf_evlist__mmap_read() does this
783 * Return: %0 on success, negative error code otherwise.
785 int perf_evlist__mmap(struct perf_evlist *evlist, unsigned int pages,
788 struct perf_evsel *evsel;
789 const struct cpu_map *cpus = evlist->cpus;
790 const struct thread_map *threads = evlist->threads;
791 int prot = PROT_READ | (overwrite ? 0 : PROT_WRITE), mask;
793 if (evlist->mmap == NULL && perf_evlist__alloc_mmap(evlist) < 0)
796 if (evlist->pollfd == NULL && perf_evlist__alloc_pollfd(evlist) < 0)
799 evlist->overwrite = overwrite;
800 evlist->mmap_len = perf_evlist__mmap_size(pages);
801 pr_debug("mmap size %zuB\n", evlist->mmap_len);
802 mask = evlist->mmap_len - page_size - 1;
804 list_for_each_entry(evsel, &evlist->entries, node) {
805 if ((evsel->attr.read_format & PERF_FORMAT_ID) &&
806 evsel->sample_id == NULL &&
807 perf_evsel__alloc_id(evsel, cpu_map__nr(cpus), threads->nr) < 0)
811 if (cpu_map__empty(cpus))
812 return perf_evlist__mmap_per_thread(evlist, prot, mask);
814 return perf_evlist__mmap_per_cpu(evlist, prot, mask);
817 int perf_evlist__create_maps(struct perf_evlist *evlist, struct target *target)
819 evlist->threads = thread_map__new_str(target->pid, target->tid,
822 if (evlist->threads == NULL)
825 if (target__uses_dummy_map(target))
826 evlist->cpus = cpu_map__dummy_new();
828 evlist->cpus = cpu_map__new(target->cpu_list);
830 if (evlist->cpus == NULL)
831 goto out_delete_threads;
836 thread_map__delete(evlist->threads);
840 int perf_evlist__apply_filters(struct perf_evlist *evlist)
842 struct perf_evsel *evsel;
844 const int ncpus = cpu_map__nr(evlist->cpus),
845 nthreads = thread_map__nr(evlist->threads);
847 list_for_each_entry(evsel, &evlist->entries, node) {
848 if (evsel->filter == NULL)
851 err = perf_evsel__set_filter(evsel, ncpus, nthreads, evsel->filter);
859 int perf_evlist__set_filter(struct perf_evlist *evlist, const char *filter)
861 struct perf_evsel *evsel;
863 const int ncpus = cpu_map__nr(evlist->cpus),
864 nthreads = thread_map__nr(evlist->threads);
866 list_for_each_entry(evsel, &evlist->entries, node) {
867 err = perf_evsel__set_filter(evsel, ncpus, nthreads, filter);
875 bool perf_evlist__valid_sample_type(struct perf_evlist *evlist)
877 struct perf_evsel *pos;
879 if (evlist->nr_entries == 1)
882 if (evlist->id_pos < 0 || evlist->is_pos < 0)
885 list_for_each_entry(pos, &evlist->entries, node) {
886 if (pos->id_pos != evlist->id_pos ||
887 pos->is_pos != evlist->is_pos)
894 u64 __perf_evlist__combined_sample_type(struct perf_evlist *evlist)
896 struct perf_evsel *evsel;
898 if (evlist->combined_sample_type)
899 return evlist->combined_sample_type;
901 list_for_each_entry(evsel, &evlist->entries, node)
902 evlist->combined_sample_type |= evsel->attr.sample_type;
904 return evlist->combined_sample_type;
907 u64 perf_evlist__combined_sample_type(struct perf_evlist *evlist)
909 evlist->combined_sample_type = 0;
910 return __perf_evlist__combined_sample_type(evlist);
913 bool perf_evlist__valid_read_format(struct perf_evlist *evlist)
915 struct perf_evsel *first = perf_evlist__first(evlist), *pos = first;
916 u64 read_format = first->attr.read_format;
917 u64 sample_type = first->attr.sample_type;
919 list_for_each_entry_continue(pos, &evlist->entries, node) {
920 if (read_format != pos->attr.read_format)
924 /* PERF_SAMPLE_READ imples PERF_FORMAT_ID. */
925 if ((sample_type & PERF_SAMPLE_READ) &&
926 !(read_format & PERF_FORMAT_ID)) {
933 u64 perf_evlist__read_format(struct perf_evlist *evlist)
935 struct perf_evsel *first = perf_evlist__first(evlist);
936 return first->attr.read_format;
939 u16 perf_evlist__id_hdr_size(struct perf_evlist *evlist)
941 struct perf_evsel *first = perf_evlist__first(evlist);
942 struct perf_sample *data;
946 if (!first->attr.sample_id_all)
949 sample_type = first->attr.sample_type;
951 if (sample_type & PERF_SAMPLE_TID)
952 size += sizeof(data->tid) * 2;
954 if (sample_type & PERF_SAMPLE_TIME)
955 size += sizeof(data->time);
957 if (sample_type & PERF_SAMPLE_ID)
958 size += sizeof(data->id);
960 if (sample_type & PERF_SAMPLE_STREAM_ID)
961 size += sizeof(data->stream_id);
963 if (sample_type & PERF_SAMPLE_CPU)
964 size += sizeof(data->cpu) * 2;
966 if (sample_type & PERF_SAMPLE_IDENTIFIER)
967 size += sizeof(data->id);
972 bool perf_evlist__valid_sample_id_all(struct perf_evlist *evlist)
974 struct perf_evsel *first = perf_evlist__first(evlist), *pos = first;
976 list_for_each_entry_continue(pos, &evlist->entries, node) {
977 if (first->attr.sample_id_all != pos->attr.sample_id_all)
984 bool perf_evlist__sample_id_all(struct perf_evlist *evlist)
986 struct perf_evsel *first = perf_evlist__first(evlist);
987 return first->attr.sample_id_all;
990 void perf_evlist__set_selected(struct perf_evlist *evlist,
991 struct perf_evsel *evsel)
993 evlist->selected = evsel;
996 void perf_evlist__close(struct perf_evlist *evlist)
998 struct perf_evsel *evsel;
999 int ncpus = cpu_map__nr(evlist->cpus);
1000 int nthreads = thread_map__nr(evlist->threads);
1002 list_for_each_entry_reverse(evsel, &evlist->entries, node)
1003 perf_evsel__close(evsel, ncpus, nthreads);
1006 int perf_evlist__open(struct perf_evlist *evlist)
1008 struct perf_evsel *evsel;
1011 perf_evlist__update_id_pos(evlist);
1013 list_for_each_entry(evsel, &evlist->entries, node) {
1014 err = perf_evsel__open(evsel, evlist->cpus, evlist->threads);
1021 perf_evlist__close(evlist);
1026 int perf_evlist__prepare_workload(struct perf_evlist *evlist, struct target *target,
1027 const char *argv[], bool pipe_output,
1028 void (*exec_error)(int signo, siginfo_t *info, void *ucontext))
1030 int child_ready_pipe[2], go_pipe[2];
1033 if (pipe(child_ready_pipe) < 0) {
1034 perror("failed to create 'ready' pipe");
1038 if (pipe(go_pipe) < 0) {
1039 perror("failed to create 'go' pipe");
1040 goto out_close_ready_pipe;
1043 evlist->workload.pid = fork();
1044 if (evlist->workload.pid < 0) {
1045 perror("failed to fork");
1046 goto out_close_pipes;
1049 if (!evlist->workload.pid) {
1053 signal(SIGTERM, SIG_DFL);
1055 close(child_ready_pipe[0]);
1057 fcntl(go_pipe[0], F_SETFD, FD_CLOEXEC);
1060 * Tell the parent we're ready to go
1062 close(child_ready_pipe[1]);
1065 * Wait until the parent tells us to go.
1067 if (read(go_pipe[0], &bf, 1) == -1)
1068 perror("unable to read pipe");
1070 execvp(argv[0], (char **)argv);
1075 val.sival_int = errno;
1076 if (sigqueue(getppid(), SIGUSR1, val))
1084 struct sigaction act = {
1085 .sa_flags = SA_SIGINFO,
1086 .sa_sigaction = exec_error,
1088 sigaction(SIGUSR1, &act, NULL);
1091 if (target__none(target))
1092 evlist->threads->map[0] = evlist->workload.pid;
1094 close(child_ready_pipe[1]);
1097 * wait for child to settle
1099 if (read(child_ready_pipe[0], &bf, 1) == -1) {
1100 perror("unable to read pipe");
1101 goto out_close_pipes;
1104 fcntl(go_pipe[1], F_SETFD, FD_CLOEXEC);
1105 evlist->workload.cork_fd = go_pipe[1];
1106 close(child_ready_pipe[0]);
1112 out_close_ready_pipe:
1113 close(child_ready_pipe[0]);
1114 close(child_ready_pipe[1]);
1118 int perf_evlist__start_workload(struct perf_evlist *evlist)
1120 if (evlist->workload.cork_fd > 0) {
1124 * Remove the cork, let it rip!
1126 ret = write(evlist->workload.cork_fd, &bf, 1);
1128 perror("enable to write to pipe");
1130 close(evlist->workload.cork_fd);
1137 int perf_evlist__parse_sample(struct perf_evlist *evlist, union perf_event *event,
1138 struct perf_sample *sample)
1140 struct perf_evsel *evsel = perf_evlist__event2evsel(evlist, event);
1144 return perf_evsel__parse_sample(evsel, event, sample);
1147 size_t perf_evlist__fprintf(struct perf_evlist *evlist, FILE *fp)
1149 struct perf_evsel *evsel;
1152 list_for_each_entry(evsel, &evlist->entries, node) {
1153 printed += fprintf(fp, "%s%s", evsel->idx ? ", " : "",
1154 perf_evsel__name(evsel));
1157 return printed + fprintf(fp, "\n");
1160 int perf_evlist__strerror_tp(struct perf_evlist *evlist __maybe_unused,
1161 int err, char *buf, size_t size)
1167 scnprintf(buf, size, "%s",
1168 "Error:\tUnable to find debugfs\n"
1169 "Hint:\tWas your kernel was compiled with debugfs support?\n"
1170 "Hint:\tIs the debugfs filesystem mounted?\n"
1171 "Hint:\tTry 'sudo mount -t debugfs nodev /sys/kernel/debug'");
1174 scnprintf(buf, size,
1175 "Error:\tNo permissions to read %s/tracing/events/raw_syscalls\n"
1176 "Hint:\tTry 'sudo mount -o remount,mode=755 %s'\n",
1177 debugfs_mountpoint, debugfs_mountpoint);
1180 scnprintf(buf, size, "%s", strerror_r(err, sbuf, sizeof(sbuf)));
1187 int perf_evlist__strerror_open(struct perf_evlist *evlist __maybe_unused,
1188 int err, char *buf, size_t size)
1191 char sbuf[128], *emsg = strerror_r(err, sbuf, sizeof(sbuf));
1196 printed = scnprintf(buf, size,
1198 "Hint:\tCheck /proc/sys/kernel/perf_event_paranoid setting.", emsg);
1200 value = perf_event_paranoid();
1202 printed += scnprintf(buf + printed, size - printed, "\nHint:\t");
1205 printed += scnprintf(buf + printed, size - printed,
1206 "For your workloads it needs to be <= 1\nHint:\t");
1208 printed += scnprintf(buf + printed, size - printed,
1209 "For system wide tracing it needs to be set to -1");
1211 printed += scnprintf(buf + printed, size - printed,
1212 ".\nHint:\tThe current value is %d.", value);
1215 scnprintf(buf, size, "%s", emsg);
1222 void perf_evlist__to_front(struct perf_evlist *evlist,
1223 struct perf_evsel *move_evsel)
1225 struct perf_evsel *evsel, *n;
1228 if (move_evsel == perf_evlist__first(evlist))
1231 list_for_each_entry_safe(evsel, n, &evlist->entries, node) {
1232 if (evsel->leader == move_evsel->leader)
1233 list_move_tail(&evsel->node, &move);
1236 list_splice(&move, &evlist->entries);