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 perf_evlist__purge(evlist);
111 perf_evlist__exit(evlist);
115 void perf_evlist__add(struct perf_evlist *evlist, struct perf_evsel *entry)
117 list_add_tail(&entry->node, &evlist->entries);
118 entry->idx = evlist->nr_entries;
120 if (!evlist->nr_entries++)
121 perf_evlist__set_id_pos(evlist);
124 void perf_evlist__splice_list_tail(struct perf_evlist *evlist,
125 struct list_head *list,
128 bool set_id_pos = !evlist->nr_entries;
130 list_splice_tail(list, &evlist->entries);
131 evlist->nr_entries += nr_entries;
133 perf_evlist__set_id_pos(evlist);
136 void __perf_evlist__set_leader(struct list_head *list)
138 struct perf_evsel *evsel, *leader;
140 leader = list_entry(list->next, struct perf_evsel, node);
141 evsel = list_entry(list->prev, struct perf_evsel, node);
143 leader->nr_members = evsel->idx - leader->idx + 1;
145 list_for_each_entry(evsel, list, node) {
146 evsel->leader = leader;
150 void perf_evlist__set_leader(struct perf_evlist *evlist)
152 if (evlist->nr_entries) {
153 evlist->nr_groups = evlist->nr_entries > 1 ? 1 : 0;
154 __perf_evlist__set_leader(&evlist->entries);
158 int perf_evlist__add_default(struct perf_evlist *evlist)
160 struct perf_event_attr attr = {
161 .type = PERF_TYPE_HARDWARE,
162 .config = PERF_COUNT_HW_CPU_CYCLES,
164 struct perf_evsel *evsel;
166 event_attr_init(&attr);
168 evsel = perf_evsel__new(&attr);
172 /* use strdup() because free(evsel) assumes name is allocated */
173 evsel->name = strdup("cycles");
177 perf_evlist__add(evlist, evsel);
180 perf_evsel__delete(evsel);
185 static int perf_evlist__add_attrs(struct perf_evlist *evlist,
186 struct perf_event_attr *attrs, size_t nr_attrs)
188 struct perf_evsel *evsel, *n;
192 for (i = 0; i < nr_attrs; i++) {
193 evsel = perf_evsel__new_idx(attrs + i, evlist->nr_entries + i);
195 goto out_delete_partial_list;
196 list_add_tail(&evsel->node, &head);
199 perf_evlist__splice_list_tail(evlist, &head, nr_attrs);
203 out_delete_partial_list:
204 list_for_each_entry_safe(evsel, n, &head, node)
205 perf_evsel__delete(evsel);
209 int __perf_evlist__add_default_attrs(struct perf_evlist *evlist,
210 struct perf_event_attr *attrs, size_t nr_attrs)
214 for (i = 0; i < nr_attrs; i++)
215 event_attr_init(attrs + i);
217 return perf_evlist__add_attrs(evlist, attrs, nr_attrs);
221 perf_evlist__find_tracepoint_by_id(struct perf_evlist *evlist, int id)
223 struct perf_evsel *evsel;
225 list_for_each_entry(evsel, &evlist->entries, node) {
226 if (evsel->attr.type == PERF_TYPE_TRACEPOINT &&
227 (int)evsel->attr.config == id)
235 perf_evlist__find_tracepoint_by_name(struct perf_evlist *evlist,
238 struct perf_evsel *evsel;
240 list_for_each_entry(evsel, &evlist->entries, node) {
241 if ((evsel->attr.type == PERF_TYPE_TRACEPOINT) &&
242 (strcmp(evsel->name, name) == 0))
249 int perf_evlist__add_newtp(struct perf_evlist *evlist,
250 const char *sys, const char *name, void *handler)
252 struct perf_evsel *evsel = perf_evsel__newtp(sys, name);
257 evsel->handler = handler;
258 perf_evlist__add(evlist, evsel);
262 void perf_evlist__disable(struct perf_evlist *evlist)
265 struct perf_evsel *pos;
266 int nr_cpus = cpu_map__nr(evlist->cpus);
267 int nr_threads = thread_map__nr(evlist->threads);
269 for (cpu = 0; cpu < nr_cpus; cpu++) {
270 list_for_each_entry(pos, &evlist->entries, node) {
271 if (!perf_evsel__is_group_leader(pos) || !pos->fd)
273 for (thread = 0; thread < nr_threads; thread++)
274 ioctl(FD(pos, cpu, thread),
275 PERF_EVENT_IOC_DISABLE, 0);
280 void perf_evlist__enable(struct perf_evlist *evlist)
283 struct perf_evsel *pos;
284 int nr_cpus = cpu_map__nr(evlist->cpus);
285 int nr_threads = thread_map__nr(evlist->threads);
287 for (cpu = 0; cpu < nr_cpus; cpu++) {
288 list_for_each_entry(pos, &evlist->entries, node) {
289 if (!perf_evsel__is_group_leader(pos) || !pos->fd)
291 for (thread = 0; thread < nr_threads; thread++)
292 ioctl(FD(pos, cpu, thread),
293 PERF_EVENT_IOC_ENABLE, 0);
298 int perf_evlist__disable_event(struct perf_evlist *evlist,
299 struct perf_evsel *evsel)
301 int cpu, thread, err;
306 for (cpu = 0; cpu < evlist->cpus->nr; cpu++) {
307 for (thread = 0; thread < evlist->threads->nr; thread++) {
308 err = ioctl(FD(evsel, cpu, thread),
309 PERF_EVENT_IOC_DISABLE, 0);
317 int perf_evlist__enable_event(struct perf_evlist *evlist,
318 struct perf_evsel *evsel)
320 int cpu, thread, err;
325 for (cpu = 0; cpu < evlist->cpus->nr; cpu++) {
326 for (thread = 0; thread < evlist->threads->nr; thread++) {
327 err = ioctl(FD(evsel, cpu, thread),
328 PERF_EVENT_IOC_ENABLE, 0);
336 static int perf_evlist__alloc_pollfd(struct perf_evlist *evlist)
338 int nr_cpus = cpu_map__nr(evlist->cpus);
339 int nr_threads = thread_map__nr(evlist->threads);
340 int nfds = nr_cpus * nr_threads * evlist->nr_entries;
341 evlist->pollfd = malloc(sizeof(struct pollfd) * nfds);
342 return evlist->pollfd != NULL ? 0 : -ENOMEM;
345 void perf_evlist__add_pollfd(struct perf_evlist *evlist, int fd)
347 fcntl(fd, F_SETFL, O_NONBLOCK);
348 evlist->pollfd[evlist->nr_fds].fd = fd;
349 evlist->pollfd[evlist->nr_fds].events = POLLIN;
353 static void perf_evlist__id_hash(struct perf_evlist *evlist,
354 struct perf_evsel *evsel,
355 int cpu, int thread, u64 id)
358 struct perf_sample_id *sid = SID(evsel, cpu, thread);
362 hash = hash_64(sid->id, PERF_EVLIST__HLIST_BITS);
363 hlist_add_head(&sid->node, &evlist->heads[hash]);
366 void perf_evlist__id_add(struct perf_evlist *evlist, struct perf_evsel *evsel,
367 int cpu, int thread, u64 id)
369 perf_evlist__id_hash(evlist, evsel, cpu, thread, id);
370 evsel->id[evsel->ids++] = id;
373 static int perf_evlist__id_add_fd(struct perf_evlist *evlist,
374 struct perf_evsel *evsel,
375 int cpu, int thread, int fd)
377 u64 read_data[4] = { 0, };
378 int id_idx = 1; /* The first entry is the counter value */
382 ret = ioctl(fd, PERF_EVENT_IOC_ID, &id);
389 /* Legacy way to get event id.. All hail to old kernels! */
392 * This way does not work with group format read, so bail
395 if (perf_evlist__read_format(evlist) & PERF_FORMAT_GROUP)
398 if (!(evsel->attr.read_format & PERF_FORMAT_ID) ||
399 read(fd, &read_data, sizeof(read_data)) == -1)
402 if (evsel->attr.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
404 if (evsel->attr.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
407 id = read_data[id_idx];
410 perf_evlist__id_add(evlist, evsel, cpu, thread, id);
414 struct perf_sample_id *perf_evlist__id2sid(struct perf_evlist *evlist, u64 id)
416 struct hlist_head *head;
417 struct perf_sample_id *sid;
420 hash = hash_64(id, PERF_EVLIST__HLIST_BITS);
421 head = &evlist->heads[hash];
423 hlist_for_each_entry(sid, head, node)
430 struct perf_evsel *perf_evlist__id2evsel(struct perf_evlist *evlist, u64 id)
432 struct perf_sample_id *sid;
434 if (evlist->nr_entries == 1)
435 return perf_evlist__first(evlist);
437 sid = perf_evlist__id2sid(evlist, id);
441 if (!perf_evlist__sample_id_all(evlist))
442 return perf_evlist__first(evlist);
447 static int perf_evlist__event2id(struct perf_evlist *evlist,
448 union perf_event *event, u64 *id)
450 const u64 *array = event->sample.array;
453 n = (event->header.size - sizeof(event->header)) >> 3;
455 if (event->header.type == PERF_RECORD_SAMPLE) {
456 if (evlist->id_pos >= n)
458 *id = array[evlist->id_pos];
460 if (evlist->is_pos > n)
468 static struct perf_evsel *perf_evlist__event2evsel(struct perf_evlist *evlist,
469 union perf_event *event)
471 struct perf_evsel *first = perf_evlist__first(evlist);
472 struct hlist_head *head;
473 struct perf_sample_id *sid;
477 if (evlist->nr_entries == 1)
480 if (!first->attr.sample_id_all &&
481 event->header.type != PERF_RECORD_SAMPLE)
484 if (perf_evlist__event2id(evlist, event, &id))
487 /* Synthesized events have an id of zero */
491 hash = hash_64(id, PERF_EVLIST__HLIST_BITS);
492 head = &evlist->heads[hash];
494 hlist_for_each_entry(sid, head, node) {
501 union perf_event *perf_evlist__mmap_read(struct perf_evlist *evlist, int idx)
503 struct perf_mmap *md = &evlist->mmap[idx];
504 unsigned int head = perf_mmap__read_head(md);
505 unsigned int old = md->prev;
506 unsigned char *data = md->base + page_size;
507 union perf_event *event = NULL;
509 if (evlist->overwrite) {
511 * If we're further behind than half the buffer, there's a chance
512 * the writer will bite our tail and mess up the samples under us.
514 * If we somehow ended up ahead of the head, we got messed up.
516 * In either case, truncate and restart at head.
518 int diff = head - old;
519 if (diff > md->mask / 2 || diff < 0) {
520 fprintf(stderr, "WARNING: failed to keep up with mmap data.\n");
523 * head points to a known good entry, start there.
532 event = (union perf_event *)&data[old & md->mask];
533 size = event->header.size;
536 * Event straddles the mmap boundary -- header should always
537 * be inside due to u64 alignment of output.
539 if ((old & md->mask) + size != ((old + size) & md->mask)) {
540 unsigned int offset = old;
541 unsigned int len = min(sizeof(*event), size), cpy;
542 void *dst = md->event_copy;
545 cpy = min(md->mask + 1 - (offset & md->mask), len);
546 memcpy(dst, &data[offset & md->mask], cpy);
552 event = (union perf_event *) md->event_copy;
563 void perf_evlist__mmap_consume(struct perf_evlist *evlist, int idx)
565 if (!evlist->overwrite) {
566 struct perf_mmap *md = &evlist->mmap[idx];
567 unsigned int old = md->prev;
569 perf_mmap__write_tail(md, old);
573 static void __perf_evlist__munmap(struct perf_evlist *evlist, int idx)
575 if (evlist->mmap[idx].base != NULL) {
576 munmap(evlist->mmap[idx].base, evlist->mmap_len);
577 evlist->mmap[idx].base = NULL;
581 void perf_evlist__munmap(struct perf_evlist *evlist)
585 for (i = 0; i < evlist->nr_mmaps; i++)
586 __perf_evlist__munmap(evlist, i);
588 zfree(&evlist->mmap);
591 static int perf_evlist__alloc_mmap(struct perf_evlist *evlist)
593 evlist->nr_mmaps = cpu_map__nr(evlist->cpus);
594 if (cpu_map__empty(evlist->cpus))
595 evlist->nr_mmaps = thread_map__nr(evlist->threads);
596 evlist->mmap = zalloc(evlist->nr_mmaps * sizeof(struct perf_mmap));
597 return evlist->mmap != NULL ? 0 : -ENOMEM;
600 static int __perf_evlist__mmap(struct perf_evlist *evlist,
601 int idx, int prot, int mask, int fd)
603 evlist->mmap[idx].prev = 0;
604 evlist->mmap[idx].mask = mask;
605 evlist->mmap[idx].base = mmap(NULL, evlist->mmap_len, prot,
607 if (evlist->mmap[idx].base == MAP_FAILED) {
608 pr_debug2("failed to mmap perf event ring buffer, error %d\n",
610 evlist->mmap[idx].base = NULL;
614 perf_evlist__add_pollfd(evlist, fd);
618 static int perf_evlist__mmap_per_evsel(struct perf_evlist *evlist, int idx,
619 int prot, int mask, int cpu, int thread,
622 struct perf_evsel *evsel;
624 list_for_each_entry(evsel, &evlist->entries, node) {
625 int fd = FD(evsel, cpu, thread);
629 if (__perf_evlist__mmap(evlist, idx, prot, mask,
633 if (ioctl(fd, PERF_EVENT_IOC_SET_OUTPUT, *output) != 0)
637 if ((evsel->attr.read_format & PERF_FORMAT_ID) &&
638 perf_evlist__id_add_fd(evlist, evsel, cpu, thread, fd) < 0)
645 static int perf_evlist__mmap_per_cpu(struct perf_evlist *evlist, int prot,
649 int nr_cpus = cpu_map__nr(evlist->cpus);
650 int nr_threads = thread_map__nr(evlist->threads);
652 pr_debug2("perf event ring buffer mmapped per cpu\n");
653 for (cpu = 0; cpu < nr_cpus; cpu++) {
656 for (thread = 0; thread < nr_threads; thread++) {
657 if (perf_evlist__mmap_per_evsel(evlist, cpu, prot, mask,
658 cpu, thread, &output))
666 for (cpu = 0; cpu < nr_cpus; cpu++)
667 __perf_evlist__munmap(evlist, cpu);
671 static int perf_evlist__mmap_per_thread(struct perf_evlist *evlist, int prot,
675 int nr_threads = thread_map__nr(evlist->threads);
677 pr_debug2("perf event ring buffer mmapped per thread\n");
678 for (thread = 0; thread < nr_threads; thread++) {
681 if (perf_evlist__mmap_per_evsel(evlist, thread, prot, mask, 0,
689 for (thread = 0; thread < nr_threads; thread++)
690 __perf_evlist__munmap(evlist, thread);
694 static size_t perf_evlist__mmap_size(unsigned long pages)
696 /* 512 kiB: default amount of unprivileged mlocked memory */
697 if (pages == UINT_MAX)
698 pages = (512 * 1024) / page_size;
699 else if (!is_power_of_2(pages))
702 return (pages + 1) * page_size;
705 static long parse_pages_arg(const char *str, unsigned long min,
708 unsigned long pages, val;
709 static struct parse_tag tags[] = {
710 { .tag = 'B', .mult = 1 },
711 { .tag = 'K', .mult = 1 << 10 },
712 { .tag = 'M', .mult = 1 << 20 },
713 { .tag = 'G', .mult = 1 << 30 },
720 val = parse_tag_value(str, tags);
721 if (val != (unsigned long) -1) {
722 /* we got file size value */
723 pages = PERF_ALIGN(val, page_size) / page_size;
725 /* we got pages count value */
727 pages = strtoul(str, &eptr, 10);
732 if (pages == 0 && min == 0) {
733 /* leave number of pages at 0 */
734 } else if (!is_power_of_2(pages)) {
735 /* round pages up to next power of 2 */
736 pages = next_pow2_l(pages);
739 pr_info("rounding mmap pages size to %lu bytes (%lu pages)\n",
740 pages * page_size, pages);
749 int perf_evlist__parse_mmap_pages(const struct option *opt, const char *str,
750 int unset __maybe_unused)
752 unsigned int *mmap_pages = opt->value;
753 unsigned long max = UINT_MAX;
756 if (max > SIZE_MAX / page_size)
757 max = SIZE_MAX / page_size;
759 pages = parse_pages_arg(str, 1, max);
761 pr_err("Invalid argument for --mmap_pages/-m\n");
770 * perf_evlist__mmap - Create mmaps to receive events.
771 * @evlist: list of events
772 * @pages: map length in pages
773 * @overwrite: overwrite older events?
775 * If @overwrite is %false the user needs to signal event consumption using
776 * perf_mmap__write_tail(). Using perf_evlist__mmap_read() does this
779 * Return: %0 on success, negative error code otherwise.
781 int perf_evlist__mmap(struct perf_evlist *evlist, unsigned int pages,
784 struct perf_evsel *evsel;
785 const struct cpu_map *cpus = evlist->cpus;
786 const struct thread_map *threads = evlist->threads;
787 int prot = PROT_READ | (overwrite ? 0 : PROT_WRITE), mask;
789 if (evlist->mmap == NULL && perf_evlist__alloc_mmap(evlist) < 0)
792 if (evlist->pollfd == NULL && perf_evlist__alloc_pollfd(evlist) < 0)
795 evlist->overwrite = overwrite;
796 evlist->mmap_len = perf_evlist__mmap_size(pages);
797 pr_debug("mmap size %zuB\n", evlist->mmap_len);
798 mask = evlist->mmap_len - page_size - 1;
800 list_for_each_entry(evsel, &evlist->entries, node) {
801 if ((evsel->attr.read_format & PERF_FORMAT_ID) &&
802 evsel->sample_id == NULL &&
803 perf_evsel__alloc_id(evsel, cpu_map__nr(cpus), threads->nr) < 0)
807 if (cpu_map__empty(cpus))
808 return perf_evlist__mmap_per_thread(evlist, prot, mask);
810 return perf_evlist__mmap_per_cpu(evlist, prot, mask);
813 int perf_evlist__create_maps(struct perf_evlist *evlist, struct target *target)
815 evlist->threads = thread_map__new_str(target->pid, target->tid,
818 if (evlist->threads == NULL)
821 if (target__uses_dummy_map(target))
822 evlist->cpus = cpu_map__dummy_new();
824 evlist->cpus = cpu_map__new(target->cpu_list);
826 if (evlist->cpus == NULL)
827 goto out_delete_threads;
832 thread_map__delete(evlist->threads);
836 void perf_evlist__delete_maps(struct perf_evlist *evlist)
838 cpu_map__delete(evlist->cpus);
839 thread_map__delete(evlist->threads);
841 evlist->threads = NULL;
844 int perf_evlist__apply_filters(struct perf_evlist *evlist)
846 struct perf_evsel *evsel;
848 const int ncpus = cpu_map__nr(evlist->cpus),
849 nthreads = thread_map__nr(evlist->threads);
851 list_for_each_entry(evsel, &evlist->entries, node) {
852 if (evsel->filter == NULL)
855 err = perf_evsel__set_filter(evsel, ncpus, nthreads, evsel->filter);
863 int perf_evlist__set_filter(struct perf_evlist *evlist, const char *filter)
865 struct perf_evsel *evsel;
867 const int ncpus = cpu_map__nr(evlist->cpus),
868 nthreads = thread_map__nr(evlist->threads);
870 list_for_each_entry(evsel, &evlist->entries, node) {
871 err = perf_evsel__set_filter(evsel, ncpus, nthreads, filter);
879 bool perf_evlist__valid_sample_type(struct perf_evlist *evlist)
881 struct perf_evsel *pos;
883 if (evlist->nr_entries == 1)
886 if (evlist->id_pos < 0 || evlist->is_pos < 0)
889 list_for_each_entry(pos, &evlist->entries, node) {
890 if (pos->id_pos != evlist->id_pos ||
891 pos->is_pos != evlist->is_pos)
898 u64 __perf_evlist__combined_sample_type(struct perf_evlist *evlist)
900 struct perf_evsel *evsel;
902 if (evlist->combined_sample_type)
903 return evlist->combined_sample_type;
905 list_for_each_entry(evsel, &evlist->entries, node)
906 evlist->combined_sample_type |= evsel->attr.sample_type;
908 return evlist->combined_sample_type;
911 u64 perf_evlist__combined_sample_type(struct perf_evlist *evlist)
913 evlist->combined_sample_type = 0;
914 return __perf_evlist__combined_sample_type(evlist);
917 bool perf_evlist__valid_read_format(struct perf_evlist *evlist)
919 struct perf_evsel *first = perf_evlist__first(evlist), *pos = first;
920 u64 read_format = first->attr.read_format;
921 u64 sample_type = first->attr.sample_type;
923 list_for_each_entry_continue(pos, &evlist->entries, node) {
924 if (read_format != pos->attr.read_format)
928 /* PERF_SAMPLE_READ imples PERF_FORMAT_ID. */
929 if ((sample_type & PERF_SAMPLE_READ) &&
930 !(read_format & PERF_FORMAT_ID)) {
937 u64 perf_evlist__read_format(struct perf_evlist *evlist)
939 struct perf_evsel *first = perf_evlist__first(evlist);
940 return first->attr.read_format;
943 u16 perf_evlist__id_hdr_size(struct perf_evlist *evlist)
945 struct perf_evsel *first = perf_evlist__first(evlist);
946 struct perf_sample *data;
950 if (!first->attr.sample_id_all)
953 sample_type = first->attr.sample_type;
955 if (sample_type & PERF_SAMPLE_TID)
956 size += sizeof(data->tid) * 2;
958 if (sample_type & PERF_SAMPLE_TIME)
959 size += sizeof(data->time);
961 if (sample_type & PERF_SAMPLE_ID)
962 size += sizeof(data->id);
964 if (sample_type & PERF_SAMPLE_STREAM_ID)
965 size += sizeof(data->stream_id);
967 if (sample_type & PERF_SAMPLE_CPU)
968 size += sizeof(data->cpu) * 2;
970 if (sample_type & PERF_SAMPLE_IDENTIFIER)
971 size += sizeof(data->id);
976 bool perf_evlist__valid_sample_id_all(struct perf_evlist *evlist)
978 struct perf_evsel *first = perf_evlist__first(evlist), *pos = first;
980 list_for_each_entry_continue(pos, &evlist->entries, node) {
981 if (first->attr.sample_id_all != pos->attr.sample_id_all)
988 bool perf_evlist__sample_id_all(struct perf_evlist *evlist)
990 struct perf_evsel *first = perf_evlist__first(evlist);
991 return first->attr.sample_id_all;
994 void perf_evlist__set_selected(struct perf_evlist *evlist,
995 struct perf_evsel *evsel)
997 evlist->selected = evsel;
1000 void perf_evlist__close(struct perf_evlist *evlist)
1002 struct perf_evsel *evsel;
1003 int ncpus = cpu_map__nr(evlist->cpus);
1004 int nthreads = thread_map__nr(evlist->threads);
1006 list_for_each_entry_reverse(evsel, &evlist->entries, node)
1007 perf_evsel__close(evsel, ncpus, nthreads);
1010 int perf_evlist__open(struct perf_evlist *evlist)
1012 struct perf_evsel *evsel;
1015 perf_evlist__update_id_pos(evlist);
1017 list_for_each_entry(evsel, &evlist->entries, node) {
1018 err = perf_evsel__open(evsel, evlist->cpus, evlist->threads);
1025 perf_evlist__close(evlist);
1030 int perf_evlist__prepare_workload(struct perf_evlist *evlist, struct target *target,
1031 const char *argv[], bool pipe_output,
1032 void (*exec_error)(int signo, siginfo_t *info, void *ucontext))
1034 int child_ready_pipe[2], go_pipe[2];
1037 if (pipe(child_ready_pipe) < 0) {
1038 perror("failed to create 'ready' pipe");
1042 if (pipe(go_pipe) < 0) {
1043 perror("failed to create 'go' pipe");
1044 goto out_close_ready_pipe;
1047 evlist->workload.pid = fork();
1048 if (evlist->workload.pid < 0) {
1049 perror("failed to fork");
1050 goto out_close_pipes;
1053 if (!evlist->workload.pid) {
1057 signal(SIGTERM, SIG_DFL);
1059 close(child_ready_pipe[0]);
1061 fcntl(go_pipe[0], F_SETFD, FD_CLOEXEC);
1064 * Tell the parent we're ready to go
1066 close(child_ready_pipe[1]);
1069 * Wait until the parent tells us to go.
1071 if (read(go_pipe[0], &bf, 1) == -1)
1072 perror("unable to read pipe");
1074 execvp(argv[0], (char **)argv);
1079 val.sival_int = errno;
1080 if (sigqueue(getppid(), SIGUSR1, val))
1088 struct sigaction act = {
1089 .sa_flags = SA_SIGINFO,
1090 .sa_sigaction = exec_error,
1092 sigaction(SIGUSR1, &act, NULL);
1095 if (target__none(target))
1096 evlist->threads->map[0] = evlist->workload.pid;
1098 close(child_ready_pipe[1]);
1101 * wait for child to settle
1103 if (read(child_ready_pipe[0], &bf, 1) == -1) {
1104 perror("unable to read pipe");
1105 goto out_close_pipes;
1108 fcntl(go_pipe[1], F_SETFD, FD_CLOEXEC);
1109 evlist->workload.cork_fd = go_pipe[1];
1110 close(child_ready_pipe[0]);
1116 out_close_ready_pipe:
1117 close(child_ready_pipe[0]);
1118 close(child_ready_pipe[1]);
1122 int perf_evlist__start_workload(struct perf_evlist *evlist)
1124 if (evlist->workload.cork_fd > 0) {
1128 * Remove the cork, let it rip!
1130 ret = write(evlist->workload.cork_fd, &bf, 1);
1132 perror("enable to write to pipe");
1134 close(evlist->workload.cork_fd);
1141 int perf_evlist__parse_sample(struct perf_evlist *evlist, union perf_event *event,
1142 struct perf_sample *sample)
1144 struct perf_evsel *evsel = perf_evlist__event2evsel(evlist, event);
1148 return perf_evsel__parse_sample(evsel, event, sample);
1151 size_t perf_evlist__fprintf(struct perf_evlist *evlist, FILE *fp)
1153 struct perf_evsel *evsel;
1156 list_for_each_entry(evsel, &evlist->entries, node) {
1157 printed += fprintf(fp, "%s%s", evsel->idx ? ", " : "",
1158 perf_evsel__name(evsel));
1161 return printed + fprintf(fp, "\n");
1164 int perf_evlist__strerror_tp(struct perf_evlist *evlist __maybe_unused,
1165 int err, char *buf, size_t size)
1171 scnprintf(buf, size, "%s",
1172 "Error:\tUnable to find debugfs\n"
1173 "Hint:\tWas your kernel was compiled with debugfs support?\n"
1174 "Hint:\tIs the debugfs filesystem mounted?\n"
1175 "Hint:\tTry 'sudo mount -t debugfs nodev /sys/kernel/debug'");
1178 scnprintf(buf, size,
1179 "Error:\tNo permissions to read %s/tracing/events/raw_syscalls\n"
1180 "Hint:\tTry 'sudo mount -o remount,mode=755 %s'\n",
1181 debugfs_mountpoint, debugfs_mountpoint);
1184 scnprintf(buf, size, "%s", strerror_r(err, sbuf, sizeof(sbuf)));
1191 int perf_evlist__strerror_open(struct perf_evlist *evlist __maybe_unused,
1192 int err, char *buf, size_t size)
1195 char sbuf[128], *emsg = strerror_r(err, sbuf, sizeof(sbuf));
1200 printed = scnprintf(buf, size,
1202 "Hint:\tCheck /proc/sys/kernel/perf_event_paranoid setting.", emsg);
1204 value = perf_event_paranoid();
1206 printed += scnprintf(buf + printed, size - printed, "\nHint:\t");
1209 printed += scnprintf(buf + printed, size - printed,
1210 "For your workloads it needs to be <= 1\nHint:\t");
1212 printed += scnprintf(buf + printed, size - printed,
1213 "For system wide tracing it needs to be set to -1");
1215 printed += scnprintf(buf + printed, size - printed,
1216 ".\nHint:\tThe current value is %d.", value);
1219 scnprintf(buf, size, "%s", emsg);
1226 void perf_evlist__to_front(struct perf_evlist *evlist,
1227 struct perf_evsel *move_evsel)
1229 struct perf_evsel *evsel, *n;
1232 if (move_evsel == perf_evlist__first(evlist))
1235 list_for_each_entry_safe(evsel, n, &evlist->entries, node) {
1236 if (evsel->leader == move_evsel->leader)
1237 list_move_tail(&evsel->node, &move);
1240 list_splice(&move, &evlist->entries);