fix a kmap leak in virtio_console
[linux/fpc-iii.git] / tools / perf / util / evlist.c
blob59ef2802fcf631fa0e7d6f2f97e59e6881f675d4
1 /*
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
5 * copyright notes.
7 * Released under the GPL v2. (and only v2, not any later version)
8 */
9 #include "util.h"
10 #include <api/fs/debugfs.h>
11 #include <poll.h>
12 #include "cpumap.h"
13 #include "thread_map.h"
14 #include "target.h"
15 #include "evlist.h"
16 #include "evsel.h"
17 #include "debug.h"
18 #include <unistd.h>
20 #include "parse-events.h"
21 #include "parse-options.h"
23 #include <sys/mman.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)
34 int i;
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));
47 if (evlist != NULL)
48 perf_evlist__init(evlist, NULL, NULL);
50 return evlist;
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);
59 evlist = NULL;
62 return evlist;
65 /**
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);
114 evlist->cpus = NULL;
115 evlist->threads = NULL;
116 perf_evlist__purge(evlist);
117 perf_evlist__exit(evlist);
118 free(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,
132 int nr_entries)
134 bool set_id_pos = !evlist->nr_entries;
136 list_splice_tail(list, &evlist->entries);
137 evlist->nr_entries += nr_entries;
138 if (set_id_pos)
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);
175 if (evsel == NULL)
176 goto error;
178 /* use strdup() because free(evsel) assumes name is allocated */
179 evsel->name = strdup("cycles");
180 if (!evsel->name)
181 goto error_free;
183 perf_evlist__add(evlist, evsel);
184 return 0;
185 error_free:
186 perf_evsel__delete(evsel);
187 error:
188 return -ENOMEM;
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;
195 LIST_HEAD(head);
196 size_t i;
198 for (i = 0; i < nr_attrs; i++) {
199 evsel = perf_evsel__new_idx(attrs + i, evlist->nr_entries + i);
200 if (evsel == NULL)
201 goto out_delete_partial_list;
202 list_add_tail(&evsel->node, &head);
205 perf_evlist__splice_list_tail(evlist, &head, nr_attrs);
207 return 0;
209 out_delete_partial_list:
210 __evlist__for_each_safe(&head, n, evsel)
211 perf_evsel__delete(evsel);
212 return -1;
215 int __perf_evlist__add_default_attrs(struct perf_evlist *evlist,
216 struct perf_event_attr *attrs, size_t nr_attrs)
218 size_t i;
220 for (i = 0; i < nr_attrs; i++)
221 event_attr_init(attrs + i);
223 return perf_evlist__add_attrs(evlist, attrs, nr_attrs);
226 struct perf_evsel *
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)
234 return evsel;
237 return NULL;
240 struct perf_evsel *
241 perf_evlist__find_tracepoint_by_name(struct perf_evlist *evlist,
242 const char *name)
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))
249 return evsel;
252 return NULL;
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);
260 if (evsel == NULL)
261 return -1;
263 evsel->handler = handler;
264 perf_evlist__add(evlist, evsel);
265 return 0;
268 void perf_evlist__disable(struct perf_evlist *evlist)
270 int cpu, thread;
271 struct perf_evsel *pos;
272 int nr_cpus = cpu_map__nr(evlist->cpus);
273 int nr_threads = thread_map__nr(evlist->threads);
275 for (cpu = 0; cpu < nr_cpus; cpu++) {
276 evlist__for_each(evlist, pos) {
277 if (!perf_evsel__is_group_leader(pos) || !pos->fd)
278 continue;
279 for (thread = 0; thread < nr_threads; thread++)
280 ioctl(FD(pos, cpu, thread),
281 PERF_EVENT_IOC_DISABLE, 0);
286 void perf_evlist__enable(struct perf_evlist *evlist)
288 int cpu, thread;
289 struct perf_evsel *pos;
290 int nr_cpus = cpu_map__nr(evlist->cpus);
291 int nr_threads = thread_map__nr(evlist->threads);
293 for (cpu = 0; cpu < nr_cpus; cpu++) {
294 evlist__for_each(evlist, pos) {
295 if (!perf_evsel__is_group_leader(pos) || !pos->fd)
296 continue;
297 for (thread = 0; thread < nr_threads; thread++)
298 ioctl(FD(pos, cpu, thread),
299 PERF_EVENT_IOC_ENABLE, 0);
304 int perf_evlist__disable_event(struct perf_evlist *evlist,
305 struct perf_evsel *evsel)
307 int cpu, thread, err;
309 if (!evsel->fd)
310 return 0;
312 for (cpu = 0; cpu < evlist->cpus->nr; cpu++) {
313 for (thread = 0; thread < evlist->threads->nr; thread++) {
314 err = ioctl(FD(evsel, cpu, thread),
315 PERF_EVENT_IOC_DISABLE, 0);
316 if (err)
317 return err;
320 return 0;
323 int perf_evlist__enable_event(struct perf_evlist *evlist,
324 struct perf_evsel *evsel)
326 int cpu, thread, err;
328 if (!evsel->fd)
329 return -EINVAL;
331 for (cpu = 0; cpu < evlist->cpus->nr; cpu++) {
332 for (thread = 0; thread < evlist->threads->nr; thread++) {
333 err = ioctl(FD(evsel, cpu, thread),
334 PERF_EVENT_IOC_ENABLE, 0);
335 if (err)
336 return err;
339 return 0;
342 static int perf_evlist__alloc_pollfd(struct perf_evlist *evlist)
344 int nr_cpus = cpu_map__nr(evlist->cpus);
345 int nr_threads = thread_map__nr(evlist->threads);
346 int nfds = nr_cpus * nr_threads * evlist->nr_entries;
347 evlist->pollfd = malloc(sizeof(struct pollfd) * nfds);
348 return evlist->pollfd != NULL ? 0 : -ENOMEM;
351 void perf_evlist__add_pollfd(struct perf_evlist *evlist, int fd)
353 fcntl(fd, F_SETFL, O_NONBLOCK);
354 evlist->pollfd[evlist->nr_fds].fd = fd;
355 evlist->pollfd[evlist->nr_fds].events = POLLIN;
356 evlist->nr_fds++;
359 static void perf_evlist__id_hash(struct perf_evlist *evlist,
360 struct perf_evsel *evsel,
361 int cpu, int thread, u64 id)
363 int hash;
364 struct perf_sample_id *sid = SID(evsel, cpu, thread);
366 sid->id = id;
367 sid->evsel = evsel;
368 hash = hash_64(sid->id, PERF_EVLIST__HLIST_BITS);
369 hlist_add_head(&sid->node, &evlist->heads[hash]);
372 void perf_evlist__id_add(struct perf_evlist *evlist, struct perf_evsel *evsel,
373 int cpu, int thread, u64 id)
375 perf_evlist__id_hash(evlist, evsel, cpu, thread, id);
376 evsel->id[evsel->ids++] = id;
379 static int perf_evlist__id_add_fd(struct perf_evlist *evlist,
380 struct perf_evsel *evsel,
381 int cpu, int thread, int fd)
383 u64 read_data[4] = { 0, };
384 int id_idx = 1; /* The first entry is the counter value */
385 u64 id;
386 int ret;
388 ret = ioctl(fd, PERF_EVENT_IOC_ID, &id);
389 if (!ret)
390 goto add;
392 if (errno != ENOTTY)
393 return -1;
395 /* Legacy way to get event id.. All hail to old kernels! */
398 * This way does not work with group format read, so bail
399 * out in that case.
401 if (perf_evlist__read_format(evlist) & PERF_FORMAT_GROUP)
402 return -1;
404 if (!(evsel->attr.read_format & PERF_FORMAT_ID) ||
405 read(fd, &read_data, sizeof(read_data)) == -1)
406 return -1;
408 if (evsel->attr.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
409 ++id_idx;
410 if (evsel->attr.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
411 ++id_idx;
413 id = read_data[id_idx];
415 add:
416 perf_evlist__id_add(evlist, evsel, cpu, thread, id);
417 return 0;
420 struct perf_sample_id *perf_evlist__id2sid(struct perf_evlist *evlist, u64 id)
422 struct hlist_head *head;
423 struct perf_sample_id *sid;
424 int hash;
426 hash = hash_64(id, PERF_EVLIST__HLIST_BITS);
427 head = &evlist->heads[hash];
429 hlist_for_each_entry(sid, head, node)
430 if (sid->id == id)
431 return sid;
433 return NULL;
436 struct perf_evsel *perf_evlist__id2evsel(struct perf_evlist *evlist, u64 id)
438 struct perf_sample_id *sid;
440 if (evlist->nr_entries == 1)
441 return perf_evlist__first(evlist);
443 sid = perf_evlist__id2sid(evlist, id);
444 if (sid)
445 return sid->evsel;
447 if (!perf_evlist__sample_id_all(evlist))
448 return perf_evlist__first(evlist);
450 return NULL;
453 static int perf_evlist__event2id(struct perf_evlist *evlist,
454 union perf_event *event, u64 *id)
456 const u64 *array = event->sample.array;
457 ssize_t n;
459 n = (event->header.size - sizeof(event->header)) >> 3;
461 if (event->header.type == PERF_RECORD_SAMPLE) {
462 if (evlist->id_pos >= n)
463 return -1;
464 *id = array[evlist->id_pos];
465 } else {
466 if (evlist->is_pos > n)
467 return -1;
468 n -= evlist->is_pos;
469 *id = array[n];
471 return 0;
474 static struct perf_evsel *perf_evlist__event2evsel(struct perf_evlist *evlist,
475 union perf_event *event)
477 struct perf_evsel *first = perf_evlist__first(evlist);
478 struct hlist_head *head;
479 struct perf_sample_id *sid;
480 int hash;
481 u64 id;
483 if (evlist->nr_entries == 1)
484 return first;
486 if (!first->attr.sample_id_all &&
487 event->header.type != PERF_RECORD_SAMPLE)
488 return first;
490 if (perf_evlist__event2id(evlist, event, &id))
491 return NULL;
493 /* Synthesized events have an id of zero */
494 if (!id)
495 return first;
497 hash = hash_64(id, PERF_EVLIST__HLIST_BITS);
498 head = &evlist->heads[hash];
500 hlist_for_each_entry(sid, head, node) {
501 if (sid->id == id)
502 return sid->evsel;
504 return NULL;
507 union perf_event *perf_evlist__mmap_read(struct perf_evlist *evlist, int idx)
509 struct perf_mmap *md = &evlist->mmap[idx];
510 unsigned int head = perf_mmap__read_head(md);
511 unsigned int old = md->prev;
512 unsigned char *data = md->base + page_size;
513 union perf_event *event = NULL;
515 if (evlist->overwrite) {
517 * If we're further behind than half the buffer, there's a chance
518 * the writer will bite our tail and mess up the samples under us.
520 * If we somehow ended up ahead of the head, we got messed up.
522 * In either case, truncate and restart at head.
524 int diff = head - old;
525 if (diff > md->mask / 2 || diff < 0) {
526 fprintf(stderr, "WARNING: failed to keep up with mmap data.\n");
529 * head points to a known good entry, start there.
531 old = head;
535 if (old != head) {
536 size_t size;
538 event = (union perf_event *)&data[old & md->mask];
539 size = event->header.size;
542 * Event straddles the mmap boundary -- header should always
543 * be inside due to u64 alignment of output.
545 if ((old & md->mask) + size != ((old + size) & md->mask)) {
546 unsigned int offset = old;
547 unsigned int len = min(sizeof(*event), size), cpy;
548 void *dst = md->event_copy;
550 do {
551 cpy = min(md->mask + 1 - (offset & md->mask), len);
552 memcpy(dst, &data[offset & md->mask], cpy);
553 offset += cpy;
554 dst += cpy;
555 len -= cpy;
556 } while (len);
558 event = (union perf_event *) md->event_copy;
561 old += size;
564 md->prev = old;
566 return event;
569 void perf_evlist__mmap_consume(struct perf_evlist *evlist, int idx)
571 if (!evlist->overwrite) {
572 struct perf_mmap *md = &evlist->mmap[idx];
573 unsigned int old = md->prev;
575 perf_mmap__write_tail(md, old);
579 static void __perf_evlist__munmap(struct perf_evlist *evlist, int idx)
581 if (evlist->mmap[idx].base != NULL) {
582 munmap(evlist->mmap[idx].base, evlist->mmap_len);
583 evlist->mmap[idx].base = NULL;
587 void perf_evlist__munmap(struct perf_evlist *evlist)
589 int i;
591 if (evlist->mmap == NULL)
592 return;
594 for (i = 0; i < evlist->nr_mmaps; i++)
595 __perf_evlist__munmap(evlist, i);
597 zfree(&evlist->mmap);
600 static int perf_evlist__alloc_mmap(struct perf_evlist *evlist)
602 evlist->nr_mmaps = cpu_map__nr(evlist->cpus);
603 if (cpu_map__empty(evlist->cpus))
604 evlist->nr_mmaps = thread_map__nr(evlist->threads);
605 evlist->mmap = zalloc(evlist->nr_mmaps * sizeof(struct perf_mmap));
606 return evlist->mmap != NULL ? 0 : -ENOMEM;
609 static int __perf_evlist__mmap(struct perf_evlist *evlist,
610 int idx, int prot, int mask, int fd)
612 evlist->mmap[idx].prev = 0;
613 evlist->mmap[idx].mask = mask;
614 evlist->mmap[idx].base = mmap(NULL, evlist->mmap_len, prot,
615 MAP_SHARED, fd, 0);
616 if (evlist->mmap[idx].base == MAP_FAILED) {
617 pr_debug2("failed to mmap perf event ring buffer, error %d\n",
618 errno);
619 evlist->mmap[idx].base = NULL;
620 return -1;
623 perf_evlist__add_pollfd(evlist, fd);
624 return 0;
627 static int perf_evlist__mmap_per_evsel(struct perf_evlist *evlist, int idx,
628 int prot, int mask, int cpu, int thread,
629 int *output)
631 struct perf_evsel *evsel;
633 evlist__for_each(evlist, evsel) {
634 int fd = FD(evsel, cpu, thread);
636 if (*output == -1) {
637 *output = fd;
638 if (__perf_evlist__mmap(evlist, idx, prot, mask,
639 *output) < 0)
640 return -1;
641 } else {
642 if (ioctl(fd, PERF_EVENT_IOC_SET_OUTPUT, *output) != 0)
643 return -1;
646 if ((evsel->attr.read_format & PERF_FORMAT_ID) &&
647 perf_evlist__id_add_fd(evlist, evsel, cpu, thread, fd) < 0)
648 return -1;
651 return 0;
654 static int perf_evlist__mmap_per_cpu(struct perf_evlist *evlist, int prot,
655 int mask)
657 int cpu, thread;
658 int nr_cpus = cpu_map__nr(evlist->cpus);
659 int nr_threads = thread_map__nr(evlist->threads);
661 pr_debug2("perf event ring buffer mmapped per cpu\n");
662 for (cpu = 0; cpu < nr_cpus; cpu++) {
663 int output = -1;
665 for (thread = 0; thread < nr_threads; thread++) {
666 if (perf_evlist__mmap_per_evsel(evlist, cpu, prot, mask,
667 cpu, thread, &output))
668 goto out_unmap;
672 return 0;
674 out_unmap:
675 for (cpu = 0; cpu < nr_cpus; cpu++)
676 __perf_evlist__munmap(evlist, cpu);
677 return -1;
680 static int perf_evlist__mmap_per_thread(struct perf_evlist *evlist, int prot,
681 int mask)
683 int thread;
684 int nr_threads = thread_map__nr(evlist->threads);
686 pr_debug2("perf event ring buffer mmapped per thread\n");
687 for (thread = 0; thread < nr_threads; thread++) {
688 int output = -1;
690 if (perf_evlist__mmap_per_evsel(evlist, thread, prot, mask, 0,
691 thread, &output))
692 goto out_unmap;
695 return 0;
697 out_unmap:
698 for (thread = 0; thread < nr_threads; thread++)
699 __perf_evlist__munmap(evlist, thread);
700 return -1;
703 static size_t perf_evlist__mmap_size(unsigned long pages)
705 /* 512 kiB: default amount of unprivileged mlocked memory */
706 if (pages == UINT_MAX)
707 pages = (512 * 1024) / page_size;
708 else if (!is_power_of_2(pages))
709 return 0;
711 return (pages + 1) * page_size;
714 static long parse_pages_arg(const char *str, unsigned long min,
715 unsigned long max)
717 unsigned long pages, val;
718 static struct parse_tag tags[] = {
719 { .tag = 'B', .mult = 1 },
720 { .tag = 'K', .mult = 1 << 10 },
721 { .tag = 'M', .mult = 1 << 20 },
722 { .tag = 'G', .mult = 1 << 30 },
723 { .tag = 0 },
726 if (str == NULL)
727 return -EINVAL;
729 val = parse_tag_value(str, tags);
730 if (val != (unsigned long) -1) {
731 /* we got file size value */
732 pages = PERF_ALIGN(val, page_size) / page_size;
733 } else {
734 /* we got pages count value */
735 char *eptr;
736 pages = strtoul(str, &eptr, 10);
737 if (*eptr != '\0')
738 return -EINVAL;
741 if (pages == 0 && min == 0) {
742 /* leave number of pages at 0 */
743 } else if (!is_power_of_2(pages)) {
744 /* round pages up to next power of 2 */
745 pages = next_pow2_l(pages);
746 if (!pages)
747 return -EINVAL;
748 pr_info("rounding mmap pages size to %lu bytes (%lu pages)\n",
749 pages * page_size, pages);
752 if (pages > max)
753 return -EINVAL;
755 return pages;
758 int perf_evlist__parse_mmap_pages(const struct option *opt, const char *str,
759 int unset __maybe_unused)
761 unsigned int *mmap_pages = opt->value;
762 unsigned long max = UINT_MAX;
763 long pages;
765 if (max > SIZE_MAX / page_size)
766 max = SIZE_MAX / page_size;
768 pages = parse_pages_arg(str, 1, max);
769 if (pages < 0) {
770 pr_err("Invalid argument for --mmap_pages/-m\n");
771 return -1;
774 *mmap_pages = pages;
775 return 0;
779 * perf_evlist__mmap - Create mmaps to receive events.
780 * @evlist: list of events
781 * @pages: map length in pages
782 * @overwrite: overwrite older events?
784 * If @overwrite is %false the user needs to signal event consumption using
785 * perf_mmap__write_tail(). Using perf_evlist__mmap_read() does this
786 * automatically.
788 * Return: %0 on success, negative error code otherwise.
790 int perf_evlist__mmap(struct perf_evlist *evlist, unsigned int pages,
791 bool overwrite)
793 struct perf_evsel *evsel;
794 const struct cpu_map *cpus = evlist->cpus;
795 const struct thread_map *threads = evlist->threads;
796 int prot = PROT_READ | (overwrite ? 0 : PROT_WRITE), mask;
798 if (evlist->mmap == NULL && perf_evlist__alloc_mmap(evlist) < 0)
799 return -ENOMEM;
801 if (evlist->pollfd == NULL && perf_evlist__alloc_pollfd(evlist) < 0)
802 return -ENOMEM;
804 evlist->overwrite = overwrite;
805 evlist->mmap_len = perf_evlist__mmap_size(pages);
806 pr_debug("mmap size %zuB\n", evlist->mmap_len);
807 mask = evlist->mmap_len - page_size - 1;
809 evlist__for_each(evlist, evsel) {
810 if ((evsel->attr.read_format & PERF_FORMAT_ID) &&
811 evsel->sample_id == NULL &&
812 perf_evsel__alloc_id(evsel, cpu_map__nr(cpus), threads->nr) < 0)
813 return -ENOMEM;
816 if (cpu_map__empty(cpus))
817 return perf_evlist__mmap_per_thread(evlist, prot, mask);
819 return perf_evlist__mmap_per_cpu(evlist, prot, mask);
822 int perf_evlist__create_maps(struct perf_evlist *evlist, struct target *target)
824 evlist->threads = thread_map__new_str(target->pid, target->tid,
825 target->uid);
827 if (evlist->threads == NULL)
828 return -1;
830 if (target__uses_dummy_map(target))
831 evlist->cpus = cpu_map__dummy_new();
832 else
833 evlist->cpus = cpu_map__new(target->cpu_list);
835 if (evlist->cpus == NULL)
836 goto out_delete_threads;
838 return 0;
840 out_delete_threads:
841 thread_map__delete(evlist->threads);
842 return -1;
845 int perf_evlist__apply_filters(struct perf_evlist *evlist)
847 struct perf_evsel *evsel;
848 int err = 0;
849 const int ncpus = cpu_map__nr(evlist->cpus),
850 nthreads = thread_map__nr(evlist->threads);
852 evlist__for_each(evlist, evsel) {
853 if (evsel->filter == NULL)
854 continue;
856 err = perf_evsel__set_filter(evsel, ncpus, nthreads, evsel->filter);
857 if (err)
858 break;
861 return err;
864 int perf_evlist__set_filter(struct perf_evlist *evlist, const char *filter)
866 struct perf_evsel *evsel;
867 int err = 0;
868 const int ncpus = cpu_map__nr(evlist->cpus),
869 nthreads = thread_map__nr(evlist->threads);
871 evlist__for_each(evlist, evsel) {
872 err = perf_evsel__set_filter(evsel, ncpus, nthreads, filter);
873 if (err)
874 break;
877 return err;
880 bool perf_evlist__valid_sample_type(struct perf_evlist *evlist)
882 struct perf_evsel *pos;
884 if (evlist->nr_entries == 1)
885 return true;
887 if (evlist->id_pos < 0 || evlist->is_pos < 0)
888 return false;
890 evlist__for_each(evlist, pos) {
891 if (pos->id_pos != evlist->id_pos ||
892 pos->is_pos != evlist->is_pos)
893 return false;
896 return true;
899 u64 __perf_evlist__combined_sample_type(struct perf_evlist *evlist)
901 struct perf_evsel *evsel;
903 if (evlist->combined_sample_type)
904 return evlist->combined_sample_type;
906 evlist__for_each(evlist, evsel)
907 evlist->combined_sample_type |= evsel->attr.sample_type;
909 return evlist->combined_sample_type;
912 u64 perf_evlist__combined_sample_type(struct perf_evlist *evlist)
914 evlist->combined_sample_type = 0;
915 return __perf_evlist__combined_sample_type(evlist);
918 bool perf_evlist__valid_read_format(struct perf_evlist *evlist)
920 struct perf_evsel *first = perf_evlist__first(evlist), *pos = first;
921 u64 read_format = first->attr.read_format;
922 u64 sample_type = first->attr.sample_type;
924 evlist__for_each(evlist, pos) {
925 if (read_format != pos->attr.read_format)
926 return false;
929 /* PERF_SAMPLE_READ imples PERF_FORMAT_ID. */
930 if ((sample_type & PERF_SAMPLE_READ) &&
931 !(read_format & PERF_FORMAT_ID)) {
932 return false;
935 return true;
938 u64 perf_evlist__read_format(struct perf_evlist *evlist)
940 struct perf_evsel *first = perf_evlist__first(evlist);
941 return first->attr.read_format;
944 u16 perf_evlist__id_hdr_size(struct perf_evlist *evlist)
946 struct perf_evsel *first = perf_evlist__first(evlist);
947 struct perf_sample *data;
948 u64 sample_type;
949 u16 size = 0;
951 if (!first->attr.sample_id_all)
952 goto out;
954 sample_type = first->attr.sample_type;
956 if (sample_type & PERF_SAMPLE_TID)
957 size += sizeof(data->tid) * 2;
959 if (sample_type & PERF_SAMPLE_TIME)
960 size += sizeof(data->time);
962 if (sample_type & PERF_SAMPLE_ID)
963 size += sizeof(data->id);
965 if (sample_type & PERF_SAMPLE_STREAM_ID)
966 size += sizeof(data->stream_id);
968 if (sample_type & PERF_SAMPLE_CPU)
969 size += sizeof(data->cpu) * 2;
971 if (sample_type & PERF_SAMPLE_IDENTIFIER)
972 size += sizeof(data->id);
973 out:
974 return size;
977 bool perf_evlist__valid_sample_id_all(struct perf_evlist *evlist)
979 struct perf_evsel *first = perf_evlist__first(evlist), *pos = first;
981 evlist__for_each_continue(evlist, pos) {
982 if (first->attr.sample_id_all != pos->attr.sample_id_all)
983 return false;
986 return true;
989 bool perf_evlist__sample_id_all(struct perf_evlist *evlist)
991 struct perf_evsel *first = perf_evlist__first(evlist);
992 return first->attr.sample_id_all;
995 void perf_evlist__set_selected(struct perf_evlist *evlist,
996 struct perf_evsel *evsel)
998 evlist->selected = evsel;
1001 void perf_evlist__close(struct perf_evlist *evlist)
1003 struct perf_evsel *evsel;
1004 int ncpus = cpu_map__nr(evlist->cpus);
1005 int nthreads = thread_map__nr(evlist->threads);
1006 int n;
1008 evlist__for_each_reverse(evlist, evsel) {
1009 n = evsel->cpus ? evsel->cpus->nr : ncpus;
1010 perf_evsel__close(evsel, n, nthreads);
1014 int perf_evlist__open(struct perf_evlist *evlist)
1016 struct perf_evsel *evsel;
1017 int err;
1019 perf_evlist__update_id_pos(evlist);
1021 evlist__for_each(evlist, evsel) {
1022 err = perf_evsel__open(evsel, evlist->cpus, evlist->threads);
1023 if (err < 0)
1024 goto out_err;
1027 return 0;
1028 out_err:
1029 perf_evlist__close(evlist);
1030 errno = -err;
1031 return err;
1034 int perf_evlist__prepare_workload(struct perf_evlist *evlist, struct target *target,
1035 const char *argv[], bool pipe_output,
1036 void (*exec_error)(int signo, siginfo_t *info, void *ucontext))
1038 int child_ready_pipe[2], go_pipe[2];
1039 char bf;
1041 if (pipe(child_ready_pipe) < 0) {
1042 perror("failed to create 'ready' pipe");
1043 return -1;
1046 if (pipe(go_pipe) < 0) {
1047 perror("failed to create 'go' pipe");
1048 goto out_close_ready_pipe;
1051 evlist->workload.pid = fork();
1052 if (evlist->workload.pid < 0) {
1053 perror("failed to fork");
1054 goto out_close_pipes;
1057 if (!evlist->workload.pid) {
1058 if (pipe_output)
1059 dup2(2, 1);
1061 signal(SIGTERM, SIG_DFL);
1063 close(child_ready_pipe[0]);
1064 close(go_pipe[1]);
1065 fcntl(go_pipe[0], F_SETFD, FD_CLOEXEC);
1068 * Tell the parent we're ready to go
1070 close(child_ready_pipe[1]);
1073 * Wait until the parent tells us to go.
1075 if (read(go_pipe[0], &bf, 1) == -1)
1076 perror("unable to read pipe");
1078 execvp(argv[0], (char **)argv);
1080 if (exec_error) {
1081 union sigval val;
1083 val.sival_int = errno;
1084 if (sigqueue(getppid(), SIGUSR1, val))
1085 perror(argv[0]);
1086 } else
1087 perror(argv[0]);
1088 exit(-1);
1091 if (exec_error) {
1092 struct sigaction act = {
1093 .sa_flags = SA_SIGINFO,
1094 .sa_sigaction = exec_error,
1096 sigaction(SIGUSR1, &act, NULL);
1099 if (target__none(target))
1100 evlist->threads->map[0] = evlist->workload.pid;
1102 close(child_ready_pipe[1]);
1103 close(go_pipe[0]);
1105 * wait for child to settle
1107 if (read(child_ready_pipe[0], &bf, 1) == -1) {
1108 perror("unable to read pipe");
1109 goto out_close_pipes;
1112 fcntl(go_pipe[1], F_SETFD, FD_CLOEXEC);
1113 evlist->workload.cork_fd = go_pipe[1];
1114 close(child_ready_pipe[0]);
1115 return 0;
1117 out_close_pipes:
1118 close(go_pipe[0]);
1119 close(go_pipe[1]);
1120 out_close_ready_pipe:
1121 close(child_ready_pipe[0]);
1122 close(child_ready_pipe[1]);
1123 return -1;
1126 int perf_evlist__start_workload(struct perf_evlist *evlist)
1128 if (evlist->workload.cork_fd > 0) {
1129 char bf = 0;
1130 int ret;
1132 * Remove the cork, let it rip!
1134 ret = write(evlist->workload.cork_fd, &bf, 1);
1135 if (ret < 0)
1136 perror("enable to write to pipe");
1138 close(evlist->workload.cork_fd);
1139 return ret;
1142 return 0;
1145 int perf_evlist__parse_sample(struct perf_evlist *evlist, union perf_event *event,
1146 struct perf_sample *sample)
1148 struct perf_evsel *evsel = perf_evlist__event2evsel(evlist, event);
1150 if (!evsel)
1151 return -EFAULT;
1152 return perf_evsel__parse_sample(evsel, event, sample);
1155 size_t perf_evlist__fprintf(struct perf_evlist *evlist, FILE *fp)
1157 struct perf_evsel *evsel;
1158 size_t printed = 0;
1160 evlist__for_each(evlist, evsel) {
1161 printed += fprintf(fp, "%s%s", evsel->idx ? ", " : "",
1162 perf_evsel__name(evsel));
1165 return printed + fprintf(fp, "\n");
1168 int perf_evlist__strerror_tp(struct perf_evlist *evlist __maybe_unused,
1169 int err, char *buf, size_t size)
1171 char sbuf[128];
1173 switch (err) {
1174 case ENOENT:
1175 scnprintf(buf, size, "%s",
1176 "Error:\tUnable to find debugfs\n"
1177 "Hint:\tWas your kernel was compiled with debugfs support?\n"
1178 "Hint:\tIs the debugfs filesystem mounted?\n"
1179 "Hint:\tTry 'sudo mount -t debugfs nodev /sys/kernel/debug'");
1180 break;
1181 case EACCES:
1182 scnprintf(buf, size,
1183 "Error:\tNo permissions to read %s/tracing/events/raw_syscalls\n"
1184 "Hint:\tTry 'sudo mount -o remount,mode=755 %s'\n",
1185 debugfs_mountpoint, debugfs_mountpoint);
1186 break;
1187 default:
1188 scnprintf(buf, size, "%s", strerror_r(err, sbuf, sizeof(sbuf)));
1189 break;
1192 return 0;
1195 int perf_evlist__strerror_open(struct perf_evlist *evlist __maybe_unused,
1196 int err, char *buf, size_t size)
1198 int printed, value;
1199 char sbuf[128], *emsg = strerror_r(err, sbuf, sizeof(sbuf));
1201 switch (err) {
1202 case EACCES:
1203 case EPERM:
1204 printed = scnprintf(buf, size,
1205 "Error:\t%s.\n"
1206 "Hint:\tCheck /proc/sys/kernel/perf_event_paranoid setting.", emsg);
1208 value = perf_event_paranoid();
1210 printed += scnprintf(buf + printed, size - printed, "\nHint:\t");
1212 if (value >= 2) {
1213 printed += scnprintf(buf + printed, size - printed,
1214 "For your workloads it needs to be <= 1\nHint:\t");
1216 printed += scnprintf(buf + printed, size - printed,
1217 "For system wide tracing it needs to be set to -1");
1219 printed += scnprintf(buf + printed, size - printed,
1220 ".\nHint:\tThe current value is %d.", value);
1221 break;
1222 default:
1223 scnprintf(buf, size, "%s", emsg);
1224 break;
1227 return 0;
1230 void perf_evlist__to_front(struct perf_evlist *evlist,
1231 struct perf_evsel *move_evsel)
1233 struct perf_evsel *evsel, *n;
1234 LIST_HEAD(move);
1236 if (move_evsel == perf_evlist__first(evlist))
1237 return;
1239 evlist__for_each_safe(evlist, n, evsel) {
1240 if (evsel->leader == move_evsel->leader)
1241 list_move_tail(&evsel->node, &move);
1244 list_splice(&move, &evlist->entries);