1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2011, Red Hat Inc, Arnaldo Carvalho de Melo <acme@redhat.com>
5 * Parts came from builtin-{top,stat,record}.c, see those files for further
13 #include "thread_map.h"
20 #include "bpf-event.h"
24 #include "parse-events.h"
25 #include <subcmd/parse-options.h>
28 #include <sys/ioctl.h>
31 #include <linux/bitops.h>
32 #include <linux/hash.h>
33 #include <linux/log2.h>
34 #include <linux/err.h>
35 #include <linux/zalloc.h>
36 #include <perf/evlist.h>
37 #include <perf/evsel.h>
38 #include <perf/cpumap.h>
40 #ifdef LACKS_SIGQUEUE_PROTOTYPE
41 int sigqueue(pid_t pid, int sig, const union sigval value);
44 #define FD(e, x, y) (*(int *)xyarray__entry(e->core.fd, x, y))
45 #define SID(e, x, y) xyarray__entry(e->sample_id, x, y)
47 void evlist__init(struct evlist *evlist, struct perf_cpu_map *cpus,
48 struct perf_thread_map *threads)
52 for (i = 0; i < PERF_EVLIST__HLIST_SIZE; ++i)
53 INIT_HLIST_HEAD(&evlist->heads[i]);
54 perf_evlist__init(&evlist->core);
55 perf_evlist__set_maps(&evlist->core, cpus, threads);
56 fdarray__init(&evlist->pollfd, 64);
57 evlist->workload.pid = -1;
58 evlist->bkw_mmap_state = BKW_MMAP_NOTREADY;
61 struct evlist *evlist__new(void)
63 struct evlist *evlist = zalloc(sizeof(*evlist));
66 evlist__init(evlist, NULL, NULL);
71 struct evlist *perf_evlist__new_default(void)
73 struct evlist *evlist = evlist__new();
75 if (evlist && perf_evlist__add_default(evlist)) {
76 evlist__delete(evlist);
83 struct evlist *perf_evlist__new_dummy(void)
85 struct evlist *evlist = evlist__new();
87 if (evlist && perf_evlist__add_dummy(evlist)) {
88 evlist__delete(evlist);
96 * perf_evlist__set_id_pos - set the positions of event ids.
97 * @evlist: selected event list
99 * Events with compatible sample types all have the same id_pos
100 * and is_pos. For convenience, put a copy on evlist.
102 void perf_evlist__set_id_pos(struct evlist *evlist)
104 struct evsel *first = perf_evlist__first(evlist);
106 evlist->id_pos = first->id_pos;
107 evlist->is_pos = first->is_pos;
110 static void perf_evlist__update_id_pos(struct evlist *evlist)
114 evlist__for_each_entry(evlist, evsel)
115 perf_evsel__calc_id_pos(evsel);
117 perf_evlist__set_id_pos(evlist);
120 static void perf_evlist__purge(struct evlist *evlist)
122 struct evsel *pos, *n;
124 evlist__for_each_entry_safe(evlist, n, pos) {
125 list_del_init(&pos->core.node);
130 evlist->core.nr_entries = 0;
133 void perf_evlist__exit(struct evlist *evlist)
135 zfree(&evlist->mmap);
136 zfree(&evlist->overwrite_mmap);
137 fdarray__exit(&evlist->pollfd);
140 void evlist__delete(struct evlist *evlist)
145 perf_evlist__munmap(evlist);
146 evlist__close(evlist);
147 perf_cpu_map__put(evlist->core.cpus);
148 perf_thread_map__put(evlist->core.threads);
149 evlist->core.cpus = NULL;
150 evlist->core.threads = NULL;
151 perf_evlist__purge(evlist);
152 perf_evlist__exit(evlist);
156 void evlist__add(struct evlist *evlist, struct evsel *entry)
158 entry->evlist = evlist;
159 entry->idx = evlist->core.nr_entries;
160 entry->tracking = !entry->idx;
162 perf_evlist__add(&evlist->core, &entry->core);
164 if (evlist->core.nr_entries == 1)
165 perf_evlist__set_id_pos(evlist);
168 void evlist__remove(struct evlist *evlist, struct evsel *evsel)
170 evsel->evlist = NULL;
171 perf_evlist__remove(&evlist->core, &evsel->core);
174 void perf_evlist__splice_list_tail(struct evlist *evlist,
175 struct list_head *list)
177 struct evsel *evsel, *temp;
179 __evlist__for_each_entry_safe(list, temp, evsel) {
180 list_del_init(&evsel->core.node);
181 evlist__add(evlist, evsel);
185 void __perf_evlist__set_leader(struct list_head *list)
187 struct evsel *evsel, *leader;
189 leader = list_entry(list->next, struct evsel, core.node);
190 evsel = list_entry(list->prev, struct evsel, core.node);
192 leader->core.nr_members = evsel->idx - leader->idx + 1;
194 __evlist__for_each_entry(list, evsel) {
195 evsel->leader = leader;
199 void perf_evlist__set_leader(struct evlist *evlist)
201 if (evlist->core.nr_entries) {
202 evlist->nr_groups = evlist->core.nr_entries > 1 ? 1 : 0;
203 __perf_evlist__set_leader(&evlist->core.entries);
207 int __perf_evlist__add_default(struct evlist *evlist, bool precise)
209 struct evsel *evsel = perf_evsel__new_cycles(precise);
214 evlist__add(evlist, evsel);
218 int perf_evlist__add_dummy(struct evlist *evlist)
220 struct perf_event_attr attr = {
221 .type = PERF_TYPE_SOFTWARE,
222 .config = PERF_COUNT_SW_DUMMY,
223 .size = sizeof(attr), /* to capture ABI version */
225 struct evsel *evsel = perf_evsel__new_idx(&attr, evlist->core.nr_entries);
230 evlist__add(evlist, evsel);
234 static int evlist__add_attrs(struct evlist *evlist,
235 struct perf_event_attr *attrs, size_t nr_attrs)
237 struct evsel *evsel, *n;
241 for (i = 0; i < nr_attrs; i++) {
242 evsel = perf_evsel__new_idx(attrs + i, evlist->core.nr_entries + i);
244 goto out_delete_partial_list;
245 list_add_tail(&evsel->core.node, &head);
248 perf_evlist__splice_list_tail(evlist, &head);
252 out_delete_partial_list:
253 __evlist__for_each_entry_safe(&head, n, evsel)
254 evsel__delete(evsel);
258 int __perf_evlist__add_default_attrs(struct evlist *evlist,
259 struct perf_event_attr *attrs, size_t nr_attrs)
263 for (i = 0; i < nr_attrs; i++)
264 event_attr_init(attrs + i);
266 return evlist__add_attrs(evlist, attrs, nr_attrs);
270 perf_evlist__find_tracepoint_by_id(struct evlist *evlist, int id)
274 evlist__for_each_entry(evlist, evsel) {
275 if (evsel->core.attr.type == PERF_TYPE_TRACEPOINT &&
276 (int)evsel->core.attr.config == id)
284 perf_evlist__find_tracepoint_by_name(struct evlist *evlist,
289 evlist__for_each_entry(evlist, evsel) {
290 if ((evsel->core.attr.type == PERF_TYPE_TRACEPOINT) &&
291 (strcmp(evsel->name, name) == 0))
298 int perf_evlist__add_newtp(struct evlist *evlist,
299 const char *sys, const char *name, void *handler)
301 struct evsel *evsel = perf_evsel__newtp(sys, name);
306 evsel->handler = handler;
307 evlist__add(evlist, evsel);
311 static int perf_evlist__nr_threads(struct evlist *evlist,
314 if (evsel->system_wide)
317 return thread_map__nr(evlist->core.threads);
320 void evlist__disable(struct evlist *evlist)
324 evlist__for_each_entry(evlist, pos) {
325 if (pos->disabled || !perf_evsel__is_group_leader(pos) || !pos->core.fd)
330 evlist->enabled = false;
333 void evlist__enable(struct evlist *evlist)
337 evlist__for_each_entry(evlist, pos) {
338 if (!perf_evsel__is_group_leader(pos) || !pos->core.fd)
343 evlist->enabled = true;
346 void perf_evlist__toggle_enable(struct evlist *evlist)
348 (evlist->enabled ? evlist__disable : evlist__enable)(evlist);
351 static int perf_evlist__enable_event_cpu(struct evlist *evlist,
352 struct evsel *evsel, int cpu)
355 int nr_threads = perf_evlist__nr_threads(evlist, evsel);
360 for (thread = 0; thread < nr_threads; thread++) {
361 int err = ioctl(FD(evsel, cpu, thread), PERF_EVENT_IOC_ENABLE, 0);
368 static int perf_evlist__enable_event_thread(struct evlist *evlist,
373 int nr_cpus = cpu_map__nr(evlist->core.cpus);
378 for (cpu = 0; cpu < nr_cpus; cpu++) {
379 int err = ioctl(FD(evsel, cpu, thread), PERF_EVENT_IOC_ENABLE, 0);
386 int perf_evlist__enable_event_idx(struct evlist *evlist,
387 struct evsel *evsel, int idx)
389 bool per_cpu_mmaps = !cpu_map__empty(evlist->core.cpus);
392 return perf_evlist__enable_event_cpu(evlist, evsel, idx);
394 return perf_evlist__enable_event_thread(evlist, evsel, idx);
397 int perf_evlist__alloc_pollfd(struct evlist *evlist)
399 int nr_cpus = cpu_map__nr(evlist->core.cpus);
400 int nr_threads = thread_map__nr(evlist->core.threads);
404 evlist__for_each_entry(evlist, evsel) {
405 if (evsel->system_wide)
408 nfds += nr_cpus * nr_threads;
411 if (fdarray__available_entries(&evlist->pollfd) < nfds &&
412 fdarray__grow(&evlist->pollfd, nfds) < 0)
418 static int __perf_evlist__add_pollfd(struct evlist *evlist, int fd,
419 struct perf_mmap *map, short revent)
421 int pos = fdarray__add(&evlist->pollfd, fd, revent | POLLERR | POLLHUP);
423 * Save the idx so that when we filter out fds POLLHUP'ed we can
424 * close the associated evlist->mmap[] entry.
427 evlist->pollfd.priv[pos].ptr = map;
429 fcntl(fd, F_SETFL, O_NONBLOCK);
435 int perf_evlist__add_pollfd(struct evlist *evlist, int fd)
437 return __perf_evlist__add_pollfd(evlist, fd, NULL, POLLIN);
440 static void perf_evlist__munmap_filtered(struct fdarray *fda, int fd,
441 void *arg __maybe_unused)
443 struct perf_mmap *map = fda->priv[fd].ptr;
449 int perf_evlist__filter_pollfd(struct evlist *evlist, short revents_and_mask)
451 return fdarray__filter(&evlist->pollfd, revents_and_mask,
452 perf_evlist__munmap_filtered, NULL);
455 int perf_evlist__poll(struct evlist *evlist, int timeout)
457 return fdarray__poll(&evlist->pollfd, timeout);
460 static void perf_evlist__id_hash(struct evlist *evlist,
462 int cpu, int thread, u64 id)
465 struct perf_sample_id *sid = SID(evsel, cpu, thread);
469 hash = hash_64(sid->id, PERF_EVLIST__HLIST_BITS);
470 hlist_add_head(&sid->node, &evlist->heads[hash]);
473 void perf_evlist__id_add(struct evlist *evlist, struct evsel *evsel,
474 int cpu, int thread, u64 id)
476 perf_evlist__id_hash(evlist, evsel, cpu, thread, id);
477 evsel->id[evsel->ids++] = id;
480 int perf_evlist__id_add_fd(struct evlist *evlist,
482 int cpu, int thread, int fd)
484 u64 read_data[4] = { 0, };
485 int id_idx = 1; /* The first entry is the counter value */
489 ret = ioctl(fd, PERF_EVENT_IOC_ID, &id);
496 /* Legacy way to get event id.. All hail to old kernels! */
499 * This way does not work with group format read, so bail
502 if (perf_evlist__read_format(evlist) & PERF_FORMAT_GROUP)
505 if (!(evsel->core.attr.read_format & PERF_FORMAT_ID) ||
506 read(fd, &read_data, sizeof(read_data)) == -1)
509 if (evsel->core.attr.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
511 if (evsel->core.attr.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
514 id = read_data[id_idx];
517 perf_evlist__id_add(evlist, evsel, cpu, thread, id);
521 static void perf_evlist__set_sid_idx(struct evlist *evlist,
522 struct evsel *evsel, int idx, int cpu,
525 struct perf_sample_id *sid = SID(evsel, cpu, thread);
527 if (evlist->core.cpus && cpu >= 0)
528 sid->cpu = evlist->core.cpus->map[cpu];
531 if (!evsel->system_wide && evlist->core.threads && thread >= 0)
532 sid->tid = thread_map__pid(evlist->core.threads, thread);
537 struct perf_sample_id *perf_evlist__id2sid(struct evlist *evlist, u64 id)
539 struct hlist_head *head;
540 struct perf_sample_id *sid;
543 hash = hash_64(id, PERF_EVLIST__HLIST_BITS);
544 head = &evlist->heads[hash];
546 hlist_for_each_entry(sid, head, node)
553 struct evsel *perf_evlist__id2evsel(struct evlist *evlist, u64 id)
555 struct perf_sample_id *sid;
557 if (evlist->core.nr_entries == 1 || !id)
558 return perf_evlist__first(evlist);
560 sid = perf_evlist__id2sid(evlist, id);
564 if (!perf_evlist__sample_id_all(evlist))
565 return perf_evlist__first(evlist);
570 struct evsel *perf_evlist__id2evsel_strict(struct evlist *evlist,
573 struct perf_sample_id *sid;
578 sid = perf_evlist__id2sid(evlist, id);
585 static int perf_evlist__event2id(struct evlist *evlist,
586 union perf_event *event, u64 *id)
588 const u64 *array = event->sample.array;
591 n = (event->header.size - sizeof(event->header)) >> 3;
593 if (event->header.type == PERF_RECORD_SAMPLE) {
594 if (evlist->id_pos >= n)
596 *id = array[evlist->id_pos];
598 if (evlist->is_pos > n)
606 struct evsel *perf_evlist__event2evsel(struct evlist *evlist,
607 union perf_event *event)
609 struct evsel *first = perf_evlist__first(evlist);
610 struct hlist_head *head;
611 struct perf_sample_id *sid;
615 if (evlist->core.nr_entries == 1)
618 if (!first->core.attr.sample_id_all &&
619 event->header.type != PERF_RECORD_SAMPLE)
622 if (perf_evlist__event2id(evlist, event, &id))
625 /* Synthesized events have an id of zero */
629 hash = hash_64(id, PERF_EVLIST__HLIST_BITS);
630 head = &evlist->heads[hash];
632 hlist_for_each_entry(sid, head, node) {
639 static int perf_evlist__set_paused(struct evlist *evlist, bool value)
643 if (!evlist->overwrite_mmap)
646 for (i = 0; i < evlist->nr_mmaps; i++) {
647 int fd = evlist->overwrite_mmap[i].fd;
652 err = ioctl(fd, PERF_EVENT_IOC_PAUSE_OUTPUT, value ? 1 : 0);
659 static int perf_evlist__pause(struct evlist *evlist)
661 return perf_evlist__set_paused(evlist, true);
664 static int perf_evlist__resume(struct evlist *evlist)
666 return perf_evlist__set_paused(evlist, false);
669 static void perf_evlist__munmap_nofree(struct evlist *evlist)
674 for (i = 0; i < evlist->nr_mmaps; i++)
675 perf_mmap__munmap(&evlist->mmap[i]);
677 if (evlist->overwrite_mmap)
678 for (i = 0; i < evlist->nr_mmaps; i++)
679 perf_mmap__munmap(&evlist->overwrite_mmap[i]);
682 void perf_evlist__munmap(struct evlist *evlist)
684 perf_evlist__munmap_nofree(evlist);
685 zfree(&evlist->mmap);
686 zfree(&evlist->overwrite_mmap);
689 static struct perf_mmap *perf_evlist__alloc_mmap(struct evlist *evlist,
693 struct perf_mmap *map;
695 evlist->nr_mmaps = cpu_map__nr(evlist->core.cpus);
696 if (cpu_map__empty(evlist->core.cpus))
697 evlist->nr_mmaps = thread_map__nr(evlist->core.threads);
698 map = zalloc(evlist->nr_mmaps * sizeof(struct perf_mmap));
702 for (i = 0; i < evlist->nr_mmaps; i++) {
704 map[i].overwrite = overwrite;
706 * When the perf_mmap() call is made we grab one refcount, plus
707 * one extra to let perf_mmap__consume() get the last
708 * events after all real references (perf_mmap__get()) are
711 * Each PERF_EVENT_IOC_SET_OUTPUT points to this mmap and
712 * thus does perf_mmap__get() on it.
714 refcount_set(&map[i].refcnt, 0);
720 perf_evlist__should_poll(struct evlist *evlist __maybe_unused,
723 if (evsel->core.attr.write_backward)
728 static int perf_evlist__mmap_per_evsel(struct evlist *evlist, int idx,
729 struct mmap_params *mp, int cpu_idx,
730 int thread, int *_output, int *_output_overwrite)
734 int evlist_cpu = cpu_map__cpu(evlist->core.cpus, cpu_idx);
736 evlist__for_each_entry(evlist, evsel) {
737 struct perf_mmap *maps = evlist->mmap;
738 int *output = _output;
742 mp->prot = PROT_READ | PROT_WRITE;
743 if (evsel->core.attr.write_backward) {
744 output = _output_overwrite;
745 maps = evlist->overwrite_mmap;
748 maps = perf_evlist__alloc_mmap(evlist, true);
751 evlist->overwrite_mmap = maps;
752 if (evlist->bkw_mmap_state == BKW_MMAP_NOTREADY)
753 perf_evlist__toggle_bkw_mmap(evlist, BKW_MMAP_RUNNING);
755 mp->prot &= ~PROT_WRITE;
758 if (evsel->system_wide && thread)
761 cpu = cpu_map__idx(evsel->core.cpus, evlist_cpu);
765 fd = FD(evsel, cpu, thread);
770 if (perf_mmap__mmap(&maps[idx], mp, *output, evlist_cpu) < 0)
773 if (ioctl(fd, PERF_EVENT_IOC_SET_OUTPUT, *output) != 0)
776 perf_mmap__get(&maps[idx]);
779 revent = perf_evlist__should_poll(evlist, evsel) ? POLLIN : 0;
782 * The system_wide flag causes a selected event to be opened
783 * always without a pid. Consequently it will never get a
784 * POLLHUP, but it is used for tracking in combination with
785 * other events, so it should not need to be polled anyway.
786 * Therefore don't add it for polling.
788 if (!evsel->system_wide &&
789 __perf_evlist__add_pollfd(evlist, fd, &maps[idx], revent) < 0) {
790 perf_mmap__put(&maps[idx]);
794 if (evsel->core.attr.read_format & PERF_FORMAT_ID) {
795 if (perf_evlist__id_add_fd(evlist, evsel, cpu, thread,
798 perf_evlist__set_sid_idx(evlist, evsel, idx, cpu,
806 static int perf_evlist__mmap_per_cpu(struct evlist *evlist,
807 struct mmap_params *mp)
810 int nr_cpus = cpu_map__nr(evlist->core.cpus);
811 int nr_threads = thread_map__nr(evlist->core.threads);
813 pr_debug2("perf event ring buffer mmapped per cpu\n");
814 for (cpu = 0; cpu < nr_cpus; cpu++) {
816 int output_overwrite = -1;
818 auxtrace_mmap_params__set_idx(&mp->auxtrace_mp, evlist, cpu,
821 for (thread = 0; thread < nr_threads; thread++) {
822 if (perf_evlist__mmap_per_evsel(evlist, cpu, mp, cpu,
823 thread, &output, &output_overwrite))
831 perf_evlist__munmap_nofree(evlist);
835 static int perf_evlist__mmap_per_thread(struct evlist *evlist,
836 struct mmap_params *mp)
839 int nr_threads = thread_map__nr(evlist->core.threads);
841 pr_debug2("perf event ring buffer mmapped per thread\n");
842 for (thread = 0; thread < nr_threads; thread++) {
844 int output_overwrite = -1;
846 auxtrace_mmap_params__set_idx(&mp->auxtrace_mp, evlist, thread,
849 if (perf_evlist__mmap_per_evsel(evlist, thread, mp, 0, thread,
850 &output, &output_overwrite))
857 perf_evlist__munmap_nofree(evlist);
861 unsigned long perf_event_mlock_kb_in_pages(void)
866 if (sysctl__read_int("kernel/perf_event_mlock_kb", &max) < 0) {
868 * Pick a once upon a time good value, i.e. things look
869 * strange since we can't read a sysctl value, but lets not
874 max -= (page_size / 1024);
877 pages = (max * 1024) / page_size;
878 if (!is_power_of_2(pages))
879 pages = rounddown_pow_of_two(pages);
884 size_t perf_evlist__mmap_size(unsigned long pages)
886 if (pages == UINT_MAX)
887 pages = perf_event_mlock_kb_in_pages();
888 else if (!is_power_of_2(pages))
891 return (pages + 1) * page_size;
894 static long parse_pages_arg(const char *str, unsigned long min,
897 unsigned long pages, val;
898 static struct parse_tag tags[] = {
899 { .tag = 'B', .mult = 1 },
900 { .tag = 'K', .mult = 1 << 10 },
901 { .tag = 'M', .mult = 1 << 20 },
902 { .tag = 'G', .mult = 1 << 30 },
909 val = parse_tag_value(str, tags);
910 if (val != (unsigned long) -1) {
911 /* we got file size value */
912 pages = PERF_ALIGN(val, page_size) / page_size;
914 /* we got pages count value */
916 pages = strtoul(str, &eptr, 10);
921 if (pages == 0 && min == 0) {
922 /* leave number of pages at 0 */
923 } else if (!is_power_of_2(pages)) {
926 /* round pages up to next power of 2 */
927 pages = roundup_pow_of_two(pages);
931 unit_number__scnprintf(buf, sizeof(buf), pages * page_size);
932 pr_info("rounding mmap pages size to %s (%lu pages)\n",
942 int __perf_evlist__parse_mmap_pages(unsigned int *mmap_pages, const char *str)
944 unsigned long max = UINT_MAX;
947 if (max > SIZE_MAX / page_size)
948 max = SIZE_MAX / page_size;
950 pages = parse_pages_arg(str, 1, max);
952 pr_err("Invalid argument for --mmap_pages/-m\n");
960 int perf_evlist__parse_mmap_pages(const struct option *opt, const char *str,
961 int unset __maybe_unused)
963 return __perf_evlist__parse_mmap_pages(opt->value, str);
967 * perf_evlist__mmap_ex - Create mmaps to receive events.
968 * @evlist: list of events
969 * @pages: map length in pages
970 * @overwrite: overwrite older events?
971 * @auxtrace_pages - auxtrace map length in pages
972 * @auxtrace_overwrite - overwrite older auxtrace data?
974 * If @overwrite is %false the user needs to signal event consumption using
975 * perf_mmap__write_tail(). Using perf_evlist__mmap_read() does this
978 * Similarly, if @auxtrace_overwrite is %false the user needs to signal data
979 * consumption using auxtrace_mmap__write_tail().
981 * Return: %0 on success, negative error code otherwise.
983 int perf_evlist__mmap_ex(struct evlist *evlist, unsigned int pages,
984 unsigned int auxtrace_pages,
985 bool auxtrace_overwrite, int nr_cblocks, int affinity, int flush,
989 const struct perf_cpu_map *cpus = evlist->core.cpus;
990 const struct perf_thread_map *threads = evlist->core.threads;
992 * Delay setting mp.prot: set it before calling perf_mmap__mmap.
993 * Its value is decided by evsel's write_backward.
994 * So &mp should not be passed through const pointer.
996 struct mmap_params mp = { .nr_cblocks = nr_cblocks, .affinity = affinity, .flush = flush,
997 .comp_level = comp_level };
1000 evlist->mmap = perf_evlist__alloc_mmap(evlist, false);
1004 if (evlist->pollfd.entries == NULL && perf_evlist__alloc_pollfd(evlist) < 0)
1007 evlist->mmap_len = perf_evlist__mmap_size(pages);
1008 pr_debug("mmap size %zuB\n", evlist->mmap_len);
1009 mp.mask = evlist->mmap_len - page_size - 1;
1011 auxtrace_mmap_params__init(&mp.auxtrace_mp, evlist->mmap_len,
1012 auxtrace_pages, auxtrace_overwrite);
1014 evlist__for_each_entry(evlist, evsel) {
1015 if ((evsel->core.attr.read_format & PERF_FORMAT_ID) &&
1016 evsel->sample_id == NULL &&
1017 perf_evsel__alloc_id(evsel, cpu_map__nr(cpus), threads->nr) < 0)
1021 if (cpu_map__empty(cpus))
1022 return perf_evlist__mmap_per_thread(evlist, &mp);
1024 return perf_evlist__mmap_per_cpu(evlist, &mp);
1027 int perf_evlist__mmap(struct evlist *evlist, unsigned int pages)
1029 return perf_evlist__mmap_ex(evlist, pages, 0, false, 0, PERF_AFFINITY_SYS, 1, 0);
1032 int perf_evlist__create_maps(struct evlist *evlist, struct target *target)
1034 bool all_threads = (target->per_thread && target->system_wide);
1035 struct perf_cpu_map *cpus;
1036 struct perf_thread_map *threads;
1039 * If specify '-a' and '--per-thread' to perf record, perf record
1040 * will override '--per-thread'. target->per_thread = false and
1041 * target->system_wide = true.
1043 * If specify '--per-thread' only to perf record,
1044 * target->per_thread = true and target->system_wide = false.
1046 * So target->per_thread && target->system_wide is false.
1047 * For perf record, thread_map__new_str doesn't call
1048 * thread_map__new_all_cpus. That will keep perf record's
1051 * For perf stat, it allows the case that target->per_thread and
1052 * target->system_wide are all true. It means to collect system-wide
1053 * per-thread data. thread_map__new_str will call
1054 * thread_map__new_all_cpus to enumerate all threads.
1056 threads = thread_map__new_str(target->pid, target->tid, target->uid,
1062 if (target__uses_dummy_map(target))
1063 cpus = perf_cpu_map__dummy_new();
1065 cpus = perf_cpu_map__new(target->cpu_list);
1068 goto out_delete_threads;
1070 evlist->core.has_user_cpus = !!target->cpu_list;
1072 perf_evlist__set_maps(&evlist->core, cpus, threads);
1077 perf_thread_map__put(threads);
1081 void __perf_evlist__set_sample_bit(struct evlist *evlist,
1082 enum perf_event_sample_format bit)
1084 struct evsel *evsel;
1086 evlist__for_each_entry(evlist, evsel)
1087 __perf_evsel__set_sample_bit(evsel, bit);
1090 void __perf_evlist__reset_sample_bit(struct evlist *evlist,
1091 enum perf_event_sample_format bit)
1093 struct evsel *evsel;
1095 evlist__for_each_entry(evlist, evsel)
1096 __perf_evsel__reset_sample_bit(evsel, bit);
1099 int perf_evlist__apply_filters(struct evlist *evlist, struct evsel **err_evsel)
1101 struct evsel *evsel;
1104 evlist__for_each_entry(evlist, evsel) {
1105 if (evsel->filter == NULL)
1109 * filters only work for tracepoint event, which doesn't have cpu limit.
1110 * So evlist and evsel should always be same.
1112 err = perf_evsel__apply_filter(&evsel->core, evsel->filter);
1122 int perf_evlist__set_tp_filter(struct evlist *evlist, const char *filter)
1124 struct evsel *evsel;
1127 evlist__for_each_entry(evlist, evsel) {
1128 if (evsel->core.attr.type != PERF_TYPE_TRACEPOINT)
1131 err = perf_evsel__set_filter(evsel, filter);
1139 int perf_evlist__set_tp_filter_pids(struct evlist *evlist, size_t npids, pid_t *pids)
1145 for (i = 0; i < npids; ++i) {
1147 if (asprintf(&filter, "common_pid != %d", pids[i]) < 0)
1152 if (asprintf(&tmp, "%s && common_pid != %d", filter, pids[i]) < 0)
1160 ret = perf_evlist__set_tp_filter(evlist, filter);
1166 int perf_evlist__set_tp_filter_pid(struct evlist *evlist, pid_t pid)
1168 return perf_evlist__set_tp_filter_pids(evlist, 1, &pid);
1171 bool perf_evlist__valid_sample_type(struct evlist *evlist)
1175 if (evlist->core.nr_entries == 1)
1178 if (evlist->id_pos < 0 || evlist->is_pos < 0)
1181 evlist__for_each_entry(evlist, pos) {
1182 if (pos->id_pos != evlist->id_pos ||
1183 pos->is_pos != evlist->is_pos)
1190 u64 __perf_evlist__combined_sample_type(struct evlist *evlist)
1192 struct evsel *evsel;
1194 if (evlist->combined_sample_type)
1195 return evlist->combined_sample_type;
1197 evlist__for_each_entry(evlist, evsel)
1198 evlist->combined_sample_type |= evsel->core.attr.sample_type;
1200 return evlist->combined_sample_type;
1203 u64 perf_evlist__combined_sample_type(struct evlist *evlist)
1205 evlist->combined_sample_type = 0;
1206 return __perf_evlist__combined_sample_type(evlist);
1209 u64 perf_evlist__combined_branch_type(struct evlist *evlist)
1211 struct evsel *evsel;
1212 u64 branch_type = 0;
1214 evlist__for_each_entry(evlist, evsel)
1215 branch_type |= evsel->core.attr.branch_sample_type;
1219 bool perf_evlist__valid_read_format(struct evlist *evlist)
1221 struct evsel *first = perf_evlist__first(evlist), *pos = first;
1222 u64 read_format = first->core.attr.read_format;
1223 u64 sample_type = first->core.attr.sample_type;
1225 evlist__for_each_entry(evlist, pos) {
1226 if (read_format != pos->core.attr.read_format)
1230 /* PERF_SAMPLE_READ imples PERF_FORMAT_ID. */
1231 if ((sample_type & PERF_SAMPLE_READ) &&
1232 !(read_format & PERF_FORMAT_ID)) {
1239 u64 perf_evlist__read_format(struct evlist *evlist)
1241 struct evsel *first = perf_evlist__first(evlist);
1242 return first->core.attr.read_format;
1245 u16 perf_evlist__id_hdr_size(struct evlist *evlist)
1247 struct evsel *first = perf_evlist__first(evlist);
1248 struct perf_sample *data;
1252 if (!first->core.attr.sample_id_all)
1255 sample_type = first->core.attr.sample_type;
1257 if (sample_type & PERF_SAMPLE_TID)
1258 size += sizeof(data->tid) * 2;
1260 if (sample_type & PERF_SAMPLE_TIME)
1261 size += sizeof(data->time);
1263 if (sample_type & PERF_SAMPLE_ID)
1264 size += sizeof(data->id);
1266 if (sample_type & PERF_SAMPLE_STREAM_ID)
1267 size += sizeof(data->stream_id);
1269 if (sample_type & PERF_SAMPLE_CPU)
1270 size += sizeof(data->cpu) * 2;
1272 if (sample_type & PERF_SAMPLE_IDENTIFIER)
1273 size += sizeof(data->id);
1278 bool perf_evlist__valid_sample_id_all(struct evlist *evlist)
1280 struct evsel *first = perf_evlist__first(evlist), *pos = first;
1282 evlist__for_each_entry_continue(evlist, pos) {
1283 if (first->core.attr.sample_id_all != pos->core.attr.sample_id_all)
1290 bool perf_evlist__sample_id_all(struct evlist *evlist)
1292 struct evsel *first = perf_evlist__first(evlist);
1293 return first->core.attr.sample_id_all;
1296 void perf_evlist__set_selected(struct evlist *evlist,
1297 struct evsel *evsel)
1299 evlist->selected = evsel;
1302 void evlist__close(struct evlist *evlist)
1304 struct evsel *evsel;
1306 evlist__for_each_entry_reverse(evlist, evsel)
1307 evsel__close(evsel);
1310 static int perf_evlist__create_syswide_maps(struct evlist *evlist)
1312 struct perf_cpu_map *cpus;
1313 struct perf_thread_map *threads;
1317 * Try reading /sys/devices/system/cpu/online to get
1320 * FIXME: -ENOMEM is the best we can do here, the cpu_map
1321 * code needs an overhaul to properly forward the
1322 * error, and we may not want to do that fallback to a
1323 * default cpu identity map :-\
1325 cpus = perf_cpu_map__new(NULL);
1329 threads = perf_thread_map__new_dummy();
1333 perf_evlist__set_maps(&evlist->core, cpus, threads);
1337 perf_cpu_map__put(cpus);
1341 int evlist__open(struct evlist *evlist)
1343 struct evsel *evsel;
1347 * Default: one fd per CPU, all threads, aka systemwide
1348 * as sys_perf_event_open(cpu = -1, thread = -1) is EINVAL
1350 if (evlist->core.threads == NULL && evlist->core.cpus == NULL) {
1351 err = perf_evlist__create_syswide_maps(evlist);
1356 perf_evlist__update_id_pos(evlist);
1358 evlist__for_each_entry(evlist, evsel) {
1359 err = evsel__open(evsel, evsel->core.cpus, evsel->core.threads);
1366 evlist__close(evlist);
1371 int perf_evlist__prepare_workload(struct evlist *evlist, struct target *target,
1372 const char *argv[], bool pipe_output,
1373 void (*exec_error)(int signo, siginfo_t *info, void *ucontext))
1375 int child_ready_pipe[2], go_pipe[2];
1378 if (pipe(child_ready_pipe) < 0) {
1379 perror("failed to create 'ready' pipe");
1383 if (pipe(go_pipe) < 0) {
1384 perror("failed to create 'go' pipe");
1385 goto out_close_ready_pipe;
1388 evlist->workload.pid = fork();
1389 if (evlist->workload.pid < 0) {
1390 perror("failed to fork");
1391 goto out_close_pipes;
1394 if (!evlist->workload.pid) {
1400 signal(SIGTERM, SIG_DFL);
1402 close(child_ready_pipe[0]);
1404 fcntl(go_pipe[0], F_SETFD, FD_CLOEXEC);
1407 * Tell the parent we're ready to go
1409 close(child_ready_pipe[1]);
1412 * Wait until the parent tells us to go.
1414 ret = read(go_pipe[0], &bf, 1);
1416 * The parent will ask for the execvp() to be performed by
1417 * writing exactly one byte, in workload.cork_fd, usually via
1418 * perf_evlist__start_workload().
1420 * For cancelling the workload without actually running it,
1421 * the parent will just close workload.cork_fd, without writing
1422 * anything, i.e. read will return zero and we just exit()
1427 perror("unable to read pipe");
1431 execvp(argv[0], (char **)argv);
1436 val.sival_int = errno;
1437 if (sigqueue(getppid(), SIGUSR1, val))
1445 struct sigaction act = {
1446 .sa_flags = SA_SIGINFO,
1447 .sa_sigaction = exec_error,
1449 sigaction(SIGUSR1, &act, NULL);
1452 if (target__none(target)) {
1453 if (evlist->core.threads == NULL) {
1454 fprintf(stderr, "FATAL: evlist->threads need to be set at this point (%s:%d).\n",
1455 __func__, __LINE__);
1456 goto out_close_pipes;
1458 perf_thread_map__set_pid(evlist->core.threads, 0, evlist->workload.pid);
1461 close(child_ready_pipe[1]);
1464 * wait for child to settle
1466 if (read(child_ready_pipe[0], &bf, 1) == -1) {
1467 perror("unable to read pipe");
1468 goto out_close_pipes;
1471 fcntl(go_pipe[1], F_SETFD, FD_CLOEXEC);
1472 evlist->workload.cork_fd = go_pipe[1];
1473 close(child_ready_pipe[0]);
1479 out_close_ready_pipe:
1480 close(child_ready_pipe[0]);
1481 close(child_ready_pipe[1]);
1485 int perf_evlist__start_workload(struct evlist *evlist)
1487 if (evlist->workload.cork_fd > 0) {
1491 * Remove the cork, let it rip!
1493 ret = write(evlist->workload.cork_fd, &bf, 1);
1495 perror("unable to write to pipe");
1497 close(evlist->workload.cork_fd);
1504 int perf_evlist__parse_sample(struct evlist *evlist, union perf_event *event,
1505 struct perf_sample *sample)
1507 struct evsel *evsel = perf_evlist__event2evsel(evlist, event);
1511 return perf_evsel__parse_sample(evsel, event, sample);
1514 int perf_evlist__parse_sample_timestamp(struct evlist *evlist,
1515 union perf_event *event,
1518 struct evsel *evsel = perf_evlist__event2evsel(evlist, event);
1522 return perf_evsel__parse_sample_timestamp(evsel, event, timestamp);
1525 size_t perf_evlist__fprintf(struct evlist *evlist, FILE *fp)
1527 struct evsel *evsel;
1530 evlist__for_each_entry(evlist, evsel) {
1531 printed += fprintf(fp, "%s%s", evsel->idx ? ", " : "",
1532 perf_evsel__name(evsel));
1535 return printed + fprintf(fp, "\n");
1538 int perf_evlist__strerror_open(struct evlist *evlist,
1539 int err, char *buf, size_t size)
1542 char sbuf[STRERR_BUFSIZE], *emsg = str_error_r(err, sbuf, sizeof(sbuf));
1547 printed = scnprintf(buf, size,
1549 "Hint:\tCheck /proc/sys/kernel/perf_event_paranoid setting.", emsg);
1551 value = perf_event_paranoid();
1553 printed += scnprintf(buf + printed, size - printed, "\nHint:\t");
1556 printed += scnprintf(buf + printed, size - printed,
1557 "For your workloads it needs to be <= 1\nHint:\t");
1559 printed += scnprintf(buf + printed, size - printed,
1560 "For system wide tracing it needs to be set to -1.\n");
1562 printed += scnprintf(buf + printed, size - printed,
1563 "Hint:\tTry: 'sudo sh -c \"echo -1 > /proc/sys/kernel/perf_event_paranoid\"'\n"
1564 "Hint:\tThe current value is %d.", value);
1567 struct evsel *first = perf_evlist__first(evlist);
1570 if (sysctl__read_int("kernel/perf_event_max_sample_rate", &max_freq) < 0)
1573 if (first->core.attr.sample_freq < (u64)max_freq)
1576 printed = scnprintf(buf, size,
1578 "Hint:\tCheck /proc/sys/kernel/perf_event_max_sample_rate.\n"
1579 "Hint:\tThe current value is %d and %" PRIu64 " is being requested.",
1580 emsg, max_freq, first->core.attr.sample_freq);
1585 scnprintf(buf, size, "%s", emsg);
1592 int perf_evlist__strerror_mmap(struct evlist *evlist, int err, char *buf, size_t size)
1594 char sbuf[STRERR_BUFSIZE], *emsg = str_error_r(err, sbuf, sizeof(sbuf));
1595 int pages_attempted = evlist->mmap_len / 1024, pages_max_per_user, printed = 0;
1599 sysctl__read_int("kernel/perf_event_mlock_kb", &pages_max_per_user);
1600 printed += scnprintf(buf + printed, size - printed,
1602 "Hint:\tCheck /proc/sys/kernel/perf_event_mlock_kb (%d kB) setting.\n"
1603 "Hint:\tTried using %zd kB.\n",
1604 emsg, pages_max_per_user, pages_attempted);
1606 if (pages_attempted >= pages_max_per_user) {
1607 printed += scnprintf(buf + printed, size - printed,
1608 "Hint:\tTry 'sudo sh -c \"echo %d > /proc/sys/kernel/perf_event_mlock_kb\"', or\n",
1609 pages_max_per_user + pages_attempted);
1612 printed += scnprintf(buf + printed, size - printed,
1613 "Hint:\tTry using a smaller -m/--mmap-pages value.");
1616 scnprintf(buf, size, "%s", emsg);
1623 void perf_evlist__to_front(struct evlist *evlist,
1624 struct evsel *move_evsel)
1626 struct evsel *evsel, *n;
1629 if (move_evsel == perf_evlist__first(evlist))
1632 evlist__for_each_entry_safe(evlist, n, evsel) {
1633 if (evsel->leader == move_evsel->leader)
1634 list_move_tail(&evsel->core.node, &move);
1637 list_splice(&move, &evlist->core.entries);
1640 void perf_evlist__set_tracking_event(struct evlist *evlist,
1641 struct evsel *tracking_evsel)
1643 struct evsel *evsel;
1645 if (tracking_evsel->tracking)
1648 evlist__for_each_entry(evlist, evsel) {
1649 if (evsel != tracking_evsel)
1650 evsel->tracking = false;
1653 tracking_evsel->tracking = true;
1657 perf_evlist__find_evsel_by_str(struct evlist *evlist,
1660 struct evsel *evsel;
1662 evlist__for_each_entry(evlist, evsel) {
1665 if (strcmp(str, evsel->name) == 0)
1672 void perf_evlist__toggle_bkw_mmap(struct evlist *evlist,
1673 enum bkw_mmap_state state)
1675 enum bkw_mmap_state old_state = evlist->bkw_mmap_state;
1682 if (!evlist->overwrite_mmap)
1685 switch (old_state) {
1686 case BKW_MMAP_NOTREADY: {
1687 if (state != BKW_MMAP_RUNNING)
1691 case BKW_MMAP_RUNNING: {
1692 if (state != BKW_MMAP_DATA_PENDING)
1697 case BKW_MMAP_DATA_PENDING: {
1698 if (state != BKW_MMAP_EMPTY)
1702 case BKW_MMAP_EMPTY: {
1703 if (state != BKW_MMAP_RUNNING)
1709 WARN_ONCE(1, "Shouldn't get there\n");
1712 evlist->bkw_mmap_state = state;
1716 perf_evlist__pause(evlist);
1719 perf_evlist__resume(evlist);
1730 bool perf_evlist__exclude_kernel(struct evlist *evlist)
1732 struct evsel *evsel;
1734 evlist__for_each_entry(evlist, evsel) {
1735 if (!evsel->core.attr.exclude_kernel)
1743 * Events in data file are not collect in groups, but we still want
1744 * the group display. Set the artificial group and set the leader's
1745 * forced_leader flag to notify the display code.
1747 void perf_evlist__force_leader(struct evlist *evlist)
1749 if (!evlist->nr_groups) {
1750 struct evsel *leader = perf_evlist__first(evlist);
1752 perf_evlist__set_leader(evlist);
1753 leader->forced_leader = true;
1757 struct evsel *perf_evlist__reset_weak_group(struct evlist *evsel_list,
1758 struct evsel *evsel)
1760 struct evsel *c2, *leader;
1761 bool is_open = true;
1763 leader = evsel->leader;
1764 pr_debug("Weak group for %s/%d failed\n",
1765 leader->name, leader->core.nr_members);
1768 * for_each_group_member doesn't work here because it doesn't
1769 * include the first entry.
1771 evlist__for_each_entry(evsel_list, c2) {
1774 if (c2->leader == leader) {
1778 c2->core.nr_members = 0;
1784 int perf_evlist__add_sb_event(struct evlist **evlist,
1785 struct perf_event_attr *attr,
1786 perf_evsel__sb_cb_t cb,
1789 struct evsel *evsel;
1790 bool new_evlist = (*evlist) == NULL;
1792 if (*evlist == NULL)
1793 *evlist = evlist__new();
1794 if (*evlist == NULL)
1797 if (!attr->sample_id_all) {
1798 pr_warning("enabling sample_id_all for all side band events\n");
1799 attr->sample_id_all = 1;
1802 evsel = perf_evsel__new_idx(attr, (*evlist)->core.nr_entries);
1806 evsel->side_band.cb = cb;
1807 evsel->side_band.data = data;
1808 evlist__add(*evlist, evsel);
1813 evlist__delete(*evlist);
1819 static void *perf_evlist__poll_thread(void *arg)
1821 struct evlist *evlist = arg;
1822 bool draining = false;
1826 bool got_data = false;
1828 if (evlist->thread.done)
1832 perf_evlist__poll(evlist, 1000);
1834 for (i = 0; i < evlist->nr_mmaps; i++) {
1835 struct perf_mmap *map = &evlist->mmap[i];
1836 union perf_event *event;
1838 if (perf_mmap__read_init(map))
1840 while ((event = perf_mmap__read_event(map)) != NULL) {
1841 struct evsel *evsel = perf_evlist__event2evsel(evlist, event);
1843 if (evsel && evsel->side_band.cb)
1844 evsel->side_band.cb(event, evsel->side_band.data);
1846 pr_warning("cannot locate proper evsel for the side band event\n");
1848 perf_mmap__consume(map);
1851 perf_mmap__read_done(map);
1854 if (draining && !got_data)
1860 int perf_evlist__start_sb_thread(struct evlist *evlist,
1861 struct target *target)
1863 struct evsel *counter;
1868 if (perf_evlist__create_maps(evlist, target))
1869 goto out_delete_evlist;
1871 evlist__for_each_entry(evlist, counter) {
1872 if (evsel__open(counter, evlist->core.cpus,
1873 evlist->core.threads) < 0)
1874 goto out_delete_evlist;
1877 if (perf_evlist__mmap(evlist, UINT_MAX))
1878 goto out_delete_evlist;
1880 evlist__for_each_entry(evlist, counter) {
1881 if (evsel__enable(counter))
1882 goto out_delete_evlist;
1885 evlist->thread.done = 0;
1886 if (pthread_create(&evlist->thread.th, NULL, perf_evlist__poll_thread, evlist))
1887 goto out_delete_evlist;
1892 evlist__delete(evlist);
1897 void perf_evlist__stop_sb_thread(struct evlist *evlist)
1901 evlist->thread.done = 1;
1902 pthread_join(evlist->thread.th, NULL);
1903 evlist__delete(evlist);