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 "util/mmap.h"
14 #include "thread_map.h"
21 #include "bpf_counter.h"
22 #include <internal/lib.h> // page_size
26 #include "bpf-event.h"
27 #include "util/event.h"
28 #include "util/string2.h"
29 #include "util/perf_api_probe.h"
30 #include "util/evsel_fprintf.h"
31 #include "util/evlist-hybrid.h"
33 #include "util/sample.h"
39 #include "parse-events.h"
40 #include <subcmd/parse-options.h>
43 #include <sys/ioctl.h>
45 #include <sys/prctl.h>
46 #include <sys/timerfd.h>
48 #include <linux/bitops.h>
49 #include <linux/hash.h>
50 #include <linux/log2.h>
51 #include <linux/err.h>
52 #include <linux/string.h>
53 #include <linux/time64.h>
54 #include <linux/zalloc.h>
55 #include <perf/evlist.h>
56 #include <perf/evsel.h>
57 #include <perf/cpumap.h>
58 #include <perf/mmap.h>
60 #include <internal/xyarray.h>
62 #ifdef LACKS_SIGQUEUE_PROTOTYPE
63 int sigqueue(pid_t pid, int sig, const union sigval value);
66 #define FD(e, x, y) (*(int *)xyarray__entry(e->core.fd, x, y))
67 #define SID(e, x, y) xyarray__entry(e->core.sample_id, x, y)
69 void evlist__init(struct evlist *evlist, struct perf_cpu_map *cpus,
70 struct perf_thread_map *threads)
72 perf_evlist__init(&evlist->core);
73 perf_evlist__set_maps(&evlist->core, cpus, threads);
74 evlist->workload.pid = -1;
75 evlist->bkw_mmap_state = BKW_MMAP_NOTREADY;
76 evlist->ctl_fd.fd = -1;
77 evlist->ctl_fd.ack = -1;
78 evlist->ctl_fd.pos = -1;
81 struct evlist *evlist__new(void)
83 struct evlist *evlist = zalloc(sizeof(*evlist));
86 evlist__init(evlist, NULL, NULL);
91 struct evlist *evlist__new_default(void)
93 struct evlist *evlist = evlist__new();
95 if (evlist && evlist__add_default(evlist)) {
96 evlist__delete(evlist);
103 struct evlist *evlist__new_dummy(void)
105 struct evlist *evlist = evlist__new();
107 if (evlist && evlist__add_dummy(evlist)) {
108 evlist__delete(evlist);
116 * evlist__set_id_pos - set the positions of event ids.
117 * @evlist: selected event list
119 * Events with compatible sample types all have the same id_pos
120 * and is_pos. For convenience, put a copy on evlist.
122 void evlist__set_id_pos(struct evlist *evlist)
124 struct evsel *first = evlist__first(evlist);
126 evlist->id_pos = first->id_pos;
127 evlist->is_pos = first->is_pos;
130 static void evlist__update_id_pos(struct evlist *evlist)
134 evlist__for_each_entry(evlist, evsel)
135 evsel__calc_id_pos(evsel);
137 evlist__set_id_pos(evlist);
140 static void evlist__purge(struct evlist *evlist)
142 struct evsel *pos, *n;
144 evlist__for_each_entry_safe(evlist, n, pos) {
145 list_del_init(&pos->core.node);
150 evlist->core.nr_entries = 0;
153 void evlist__exit(struct evlist *evlist)
155 event_enable_timer__exit(&evlist->eet);
156 zfree(&evlist->mmap);
157 zfree(&evlist->overwrite_mmap);
158 perf_evlist__exit(&evlist->core);
161 void evlist__delete(struct evlist *evlist)
166 evlist__munmap(evlist);
167 evlist__close(evlist);
168 evlist__purge(evlist);
169 evlist__exit(evlist);
173 void evlist__add(struct evlist *evlist, struct evsel *entry)
175 perf_evlist__add(&evlist->core, &entry->core);
176 entry->evlist = evlist;
177 entry->tracking = !entry->core.idx;
179 if (evlist->core.nr_entries == 1)
180 evlist__set_id_pos(evlist);
183 void evlist__remove(struct evlist *evlist, struct evsel *evsel)
185 evsel->evlist = NULL;
186 perf_evlist__remove(&evlist->core, &evsel->core);
189 void evlist__splice_list_tail(struct evlist *evlist, struct list_head *list)
191 while (!list_empty(list)) {
192 struct evsel *evsel, *temp, *leader = NULL;
194 __evlist__for_each_entry_safe(list, temp, evsel) {
195 list_del_init(&evsel->core.node);
196 evlist__add(evlist, evsel);
201 __evlist__for_each_entry_safe(list, temp, evsel) {
202 if (evsel__has_leader(evsel, leader)) {
203 list_del_init(&evsel->core.node);
204 evlist__add(evlist, evsel);
210 int __evlist__set_tracepoints_handlers(struct evlist *evlist,
211 const struct evsel_str_handler *assocs, size_t nr_assocs)
216 for (i = 0; i < nr_assocs; i++) {
217 // Adding a handler for an event not in this evlist, just ignore it.
218 struct evsel *evsel = evlist__find_tracepoint_by_name(evlist, assocs[i].name);
223 if (evsel->handler != NULL)
225 evsel->handler = assocs[i].handler;
233 static void evlist__set_leader(struct evlist *evlist)
235 perf_evlist__set_leader(&evlist->core);
238 int __evlist__add_default(struct evlist *evlist, bool precise)
242 evsel = evsel__new_cycles(precise, PERF_TYPE_HARDWARE,
243 PERF_COUNT_HW_CPU_CYCLES);
247 evlist__add(evlist, evsel);
251 static struct evsel *evlist__dummy_event(struct evlist *evlist)
253 struct perf_event_attr attr = {
254 .type = PERF_TYPE_SOFTWARE,
255 .config = PERF_COUNT_SW_DUMMY,
256 .size = sizeof(attr), /* to capture ABI version */
259 return evsel__new_idx(&attr, evlist->core.nr_entries);
262 int evlist__add_dummy(struct evlist *evlist)
264 struct evsel *evsel = evlist__dummy_event(evlist);
269 evlist__add(evlist, evsel);
273 struct evsel *evlist__add_aux_dummy(struct evlist *evlist, bool system_wide)
275 struct evsel *evsel = evlist__dummy_event(evlist);
280 evsel->core.attr.exclude_kernel = 1;
281 evsel->core.attr.exclude_guest = 1;
282 evsel->core.attr.exclude_hv = 1;
283 evsel->core.attr.freq = 0;
284 evsel->core.attr.sample_period = 1;
285 evsel->core.system_wide = system_wide;
286 evsel->no_aux_samples = true;
287 evsel->name = strdup("dummy:u");
289 evlist__add(evlist, evsel);
293 #ifdef HAVE_LIBTRACEEVENT
294 struct evsel *evlist__add_sched_switch(struct evlist *evlist, bool system_wide)
296 struct evsel *evsel = evsel__newtp_idx("sched", "sched_switch", 0);
301 evsel__set_sample_bit(evsel, CPU);
302 evsel__set_sample_bit(evsel, TIME);
304 evsel->core.system_wide = system_wide;
305 evsel->no_aux_samples = true;
307 evlist__add(evlist, evsel);
312 int evlist__add_attrs(struct evlist *evlist, struct perf_event_attr *attrs, size_t nr_attrs)
314 struct evsel *evsel, *n;
318 for (i = 0; i < nr_attrs; i++) {
319 evsel = evsel__new_idx(attrs + i, evlist->core.nr_entries + i);
321 goto out_delete_partial_list;
322 list_add_tail(&evsel->core.node, &head);
325 evlist__splice_list_tail(evlist, &head);
329 out_delete_partial_list:
330 __evlist__for_each_entry_safe(&head, n, evsel)
331 evsel__delete(evsel);
335 int __evlist__add_default_attrs(struct evlist *evlist, struct perf_event_attr *attrs, size_t nr_attrs)
339 for (i = 0; i < nr_attrs; i++)
340 event_attr_init(attrs + i);
342 return evlist__add_attrs(evlist, attrs, nr_attrs);
345 __weak int arch_evlist__add_default_attrs(struct evlist *evlist,
346 struct perf_event_attr *attrs,
352 return __evlist__add_default_attrs(evlist, attrs, nr_attrs);
355 struct evsel *evlist__find_tracepoint_by_id(struct evlist *evlist, int id)
359 evlist__for_each_entry(evlist, evsel) {
360 if (evsel->core.attr.type == PERF_TYPE_TRACEPOINT &&
361 (int)evsel->core.attr.config == id)
368 struct evsel *evlist__find_tracepoint_by_name(struct evlist *evlist, const char *name)
372 evlist__for_each_entry(evlist, evsel) {
373 if ((evsel->core.attr.type == PERF_TYPE_TRACEPOINT) &&
374 (strcmp(evsel->name, name) == 0))
381 #ifdef HAVE_LIBTRACEEVENT
382 int evlist__add_newtp(struct evlist *evlist, const char *sys, const char *name, void *handler)
384 struct evsel *evsel = evsel__newtp(sys, name);
389 evsel->handler = handler;
390 evlist__add(evlist, evsel);
395 struct evlist_cpu_iterator evlist__cpu_begin(struct evlist *evlist, struct affinity *affinity)
397 struct evlist_cpu_iterator itr = {
401 .evlist_cpu_map_idx = 0,
402 .evlist_cpu_map_nr = perf_cpu_map__nr(evlist->core.all_cpus),
403 .cpu = (struct perf_cpu){ .cpu = -1},
404 .affinity = affinity,
407 if (evlist__empty(evlist)) {
408 /* Ensure the empty list doesn't iterate. */
409 itr.evlist_cpu_map_idx = itr.evlist_cpu_map_nr;
411 itr.evsel = evlist__first(evlist);
413 itr.cpu = perf_cpu_map__cpu(evlist->core.all_cpus, 0);
414 affinity__set(itr.affinity, itr.cpu.cpu);
415 itr.cpu_map_idx = perf_cpu_map__idx(itr.evsel->core.cpus, itr.cpu);
417 * If this CPU isn't in the evsel's cpu map then advance
420 if (itr.cpu_map_idx == -1)
421 evlist_cpu_iterator__next(&itr);
427 void evlist_cpu_iterator__next(struct evlist_cpu_iterator *evlist_cpu_itr)
429 while (evlist_cpu_itr->evsel != evlist__last(evlist_cpu_itr->container)) {
430 evlist_cpu_itr->evsel = evsel__next(evlist_cpu_itr->evsel);
431 evlist_cpu_itr->cpu_map_idx =
432 perf_cpu_map__idx(evlist_cpu_itr->evsel->core.cpus,
433 evlist_cpu_itr->cpu);
434 if (evlist_cpu_itr->cpu_map_idx != -1)
437 evlist_cpu_itr->evlist_cpu_map_idx++;
438 if (evlist_cpu_itr->evlist_cpu_map_idx < evlist_cpu_itr->evlist_cpu_map_nr) {
439 evlist_cpu_itr->evsel = evlist__first(evlist_cpu_itr->container);
440 evlist_cpu_itr->cpu =
441 perf_cpu_map__cpu(evlist_cpu_itr->container->core.all_cpus,
442 evlist_cpu_itr->evlist_cpu_map_idx);
443 if (evlist_cpu_itr->affinity)
444 affinity__set(evlist_cpu_itr->affinity, evlist_cpu_itr->cpu.cpu);
445 evlist_cpu_itr->cpu_map_idx =
446 perf_cpu_map__idx(evlist_cpu_itr->evsel->core.cpus,
447 evlist_cpu_itr->cpu);
449 * If this CPU isn't in the evsel's cpu map then advance through
452 if (evlist_cpu_itr->cpu_map_idx == -1)
453 evlist_cpu_iterator__next(evlist_cpu_itr);
457 bool evlist_cpu_iterator__end(const struct evlist_cpu_iterator *evlist_cpu_itr)
459 return evlist_cpu_itr->evlist_cpu_map_idx >= evlist_cpu_itr->evlist_cpu_map_nr;
462 static int evsel__strcmp(struct evsel *pos, char *evsel_name)
466 if (evsel__is_dummy_event(pos))
468 return strcmp(pos->name, evsel_name);
471 static int evlist__is_enabled(struct evlist *evlist)
475 evlist__for_each_entry(evlist, pos) {
476 if (!evsel__is_group_leader(pos) || !pos->core.fd)
478 /* If at least one event is enabled, evlist is enabled. */
485 static void __evlist__disable(struct evlist *evlist, char *evsel_name, bool excl_dummy)
488 struct evlist_cpu_iterator evlist_cpu_itr;
489 struct affinity saved_affinity, *affinity = NULL;
490 bool has_imm = false;
492 // See explanation in evlist__close()
493 if (!cpu_map__is_dummy(evlist->core.user_requested_cpus)) {
494 if (affinity__setup(&saved_affinity) < 0)
496 affinity = &saved_affinity;
499 /* Disable 'immediate' events last */
500 for (int imm = 0; imm <= 1; imm++) {
501 evlist__for_each_cpu(evlist_cpu_itr, evlist, affinity) {
502 pos = evlist_cpu_itr.evsel;
503 if (evsel__strcmp(pos, evsel_name))
505 if (pos->disabled || !evsel__is_group_leader(pos) || !pos->core.fd)
507 if (excl_dummy && evsel__is_dummy_event(pos))
511 if (pos->immediate != imm)
513 evsel__disable_cpu(pos, evlist_cpu_itr.cpu_map_idx);
519 affinity__cleanup(affinity);
520 evlist__for_each_entry(evlist, pos) {
521 if (evsel__strcmp(pos, evsel_name))
523 if (!evsel__is_group_leader(pos) || !pos->core.fd)
525 if (excl_dummy && evsel__is_dummy_event(pos))
527 pos->disabled = true;
531 * If we disabled only single event, we need to check
532 * the enabled state of the evlist manually.
535 evlist->enabled = evlist__is_enabled(evlist);
537 evlist->enabled = false;
540 void evlist__disable(struct evlist *evlist)
542 __evlist__disable(evlist, NULL, false);
545 void evlist__disable_non_dummy(struct evlist *evlist)
547 __evlist__disable(evlist, NULL, true);
550 void evlist__disable_evsel(struct evlist *evlist, char *evsel_name)
552 __evlist__disable(evlist, evsel_name, false);
555 static void __evlist__enable(struct evlist *evlist, char *evsel_name, bool excl_dummy)
558 struct evlist_cpu_iterator evlist_cpu_itr;
559 struct affinity saved_affinity, *affinity = NULL;
561 // See explanation in evlist__close()
562 if (!cpu_map__is_dummy(evlist->core.user_requested_cpus)) {
563 if (affinity__setup(&saved_affinity) < 0)
565 affinity = &saved_affinity;
568 evlist__for_each_cpu(evlist_cpu_itr, evlist, affinity) {
569 pos = evlist_cpu_itr.evsel;
570 if (evsel__strcmp(pos, evsel_name))
572 if (!evsel__is_group_leader(pos) || !pos->core.fd)
574 if (excl_dummy && evsel__is_dummy_event(pos))
576 evsel__enable_cpu(pos, evlist_cpu_itr.cpu_map_idx);
578 affinity__cleanup(affinity);
579 evlist__for_each_entry(evlist, pos) {
580 if (evsel__strcmp(pos, evsel_name))
582 if (!evsel__is_group_leader(pos) || !pos->core.fd)
584 if (excl_dummy && evsel__is_dummy_event(pos))
586 pos->disabled = false;
590 * Even single event sets the 'enabled' for evlist,
591 * so the toggle can work properly and toggle to
594 evlist->enabled = true;
597 void evlist__enable(struct evlist *evlist)
599 __evlist__enable(evlist, NULL, false);
602 void evlist__enable_non_dummy(struct evlist *evlist)
604 __evlist__enable(evlist, NULL, true);
607 void evlist__enable_evsel(struct evlist *evlist, char *evsel_name)
609 __evlist__enable(evlist, evsel_name, false);
612 void evlist__toggle_enable(struct evlist *evlist)
614 (evlist->enabled ? evlist__disable : evlist__enable)(evlist);
617 int evlist__add_pollfd(struct evlist *evlist, int fd)
619 return perf_evlist__add_pollfd(&evlist->core, fd, NULL, POLLIN, fdarray_flag__default);
622 int evlist__filter_pollfd(struct evlist *evlist, short revents_and_mask)
624 return perf_evlist__filter_pollfd(&evlist->core, revents_and_mask);
627 #ifdef HAVE_EVENTFD_SUPPORT
628 int evlist__add_wakeup_eventfd(struct evlist *evlist, int fd)
630 return perf_evlist__add_pollfd(&evlist->core, fd, NULL, POLLIN,
631 fdarray_flag__nonfilterable |
632 fdarray_flag__non_perf_event);
636 int evlist__poll(struct evlist *evlist, int timeout)
638 return perf_evlist__poll(&evlist->core, timeout);
641 struct perf_sample_id *evlist__id2sid(struct evlist *evlist, u64 id)
643 struct hlist_head *head;
644 struct perf_sample_id *sid;
647 hash = hash_64(id, PERF_EVLIST__HLIST_BITS);
648 head = &evlist->core.heads[hash];
650 hlist_for_each_entry(sid, head, node)
657 struct evsel *evlist__id2evsel(struct evlist *evlist, u64 id)
659 struct perf_sample_id *sid;
661 if (evlist->core.nr_entries == 1 || !id)
662 return evlist__first(evlist);
664 sid = evlist__id2sid(evlist, id);
666 return container_of(sid->evsel, struct evsel, core);
668 if (!evlist__sample_id_all(evlist))
669 return evlist__first(evlist);
674 struct evsel *evlist__id2evsel_strict(struct evlist *evlist, u64 id)
676 struct perf_sample_id *sid;
681 sid = evlist__id2sid(evlist, id);
683 return container_of(sid->evsel, struct evsel, core);
688 static int evlist__event2id(struct evlist *evlist, union perf_event *event, u64 *id)
690 const __u64 *array = event->sample.array;
693 n = (event->header.size - sizeof(event->header)) >> 3;
695 if (event->header.type == PERF_RECORD_SAMPLE) {
696 if (evlist->id_pos >= n)
698 *id = array[evlist->id_pos];
700 if (evlist->is_pos > n)
708 struct evsel *evlist__event2evsel(struct evlist *evlist, union perf_event *event)
710 struct evsel *first = evlist__first(evlist);
711 struct hlist_head *head;
712 struct perf_sample_id *sid;
716 if (evlist->core.nr_entries == 1)
719 if (!first->core.attr.sample_id_all &&
720 event->header.type != PERF_RECORD_SAMPLE)
723 if (evlist__event2id(evlist, event, &id))
726 /* Synthesized events have an id of zero */
730 hash = hash_64(id, PERF_EVLIST__HLIST_BITS);
731 head = &evlist->core.heads[hash];
733 hlist_for_each_entry(sid, head, node) {
735 return container_of(sid->evsel, struct evsel, core);
740 static int evlist__set_paused(struct evlist *evlist, bool value)
744 if (!evlist->overwrite_mmap)
747 for (i = 0; i < evlist->core.nr_mmaps; i++) {
748 int fd = evlist->overwrite_mmap[i].core.fd;
753 err = ioctl(fd, PERF_EVENT_IOC_PAUSE_OUTPUT, value ? 1 : 0);
760 static int evlist__pause(struct evlist *evlist)
762 return evlist__set_paused(evlist, true);
765 static int evlist__resume(struct evlist *evlist)
767 return evlist__set_paused(evlist, false);
770 static void evlist__munmap_nofree(struct evlist *evlist)
775 for (i = 0; i < evlist->core.nr_mmaps; i++)
776 perf_mmap__munmap(&evlist->mmap[i].core);
778 if (evlist->overwrite_mmap)
779 for (i = 0; i < evlist->core.nr_mmaps; i++)
780 perf_mmap__munmap(&evlist->overwrite_mmap[i].core);
783 void evlist__munmap(struct evlist *evlist)
785 evlist__munmap_nofree(evlist);
786 zfree(&evlist->mmap);
787 zfree(&evlist->overwrite_mmap);
790 static void perf_mmap__unmap_cb(struct perf_mmap *map)
792 struct mmap *m = container_of(map, struct mmap, core);
797 static struct mmap *evlist__alloc_mmap(struct evlist *evlist,
803 map = zalloc(evlist->core.nr_mmaps * sizeof(struct mmap));
807 for (i = 0; i < evlist->core.nr_mmaps; i++) {
808 struct perf_mmap *prev = i ? &map[i - 1].core : NULL;
811 * When the perf_mmap() call is made we grab one refcount, plus
812 * one extra to let perf_mmap__consume() get the last
813 * events after all real references (perf_mmap__get()) are
816 * Each PERF_EVENT_IOC_SET_OUTPUT points to this mmap and
817 * thus does perf_mmap__get() on it.
819 perf_mmap__init(&map[i].core, prev, overwrite, perf_mmap__unmap_cb);
826 perf_evlist__mmap_cb_idx(struct perf_evlist *_evlist,
827 struct perf_evsel *_evsel,
828 struct perf_mmap_param *_mp,
831 struct evlist *evlist = container_of(_evlist, struct evlist, core);
832 struct mmap_params *mp = container_of(_mp, struct mmap_params, core);
833 struct evsel *evsel = container_of(_evsel, struct evsel, core);
835 auxtrace_mmap_params__set_idx(&mp->auxtrace_mp, evlist, evsel, idx);
838 static struct perf_mmap*
839 perf_evlist__mmap_cb_get(struct perf_evlist *_evlist, bool overwrite, int idx)
841 struct evlist *evlist = container_of(_evlist, struct evlist, core);
844 maps = overwrite ? evlist->overwrite_mmap : evlist->mmap;
847 maps = evlist__alloc_mmap(evlist, overwrite);
852 evlist->overwrite_mmap = maps;
853 if (evlist->bkw_mmap_state == BKW_MMAP_NOTREADY)
854 evlist__toggle_bkw_mmap(evlist, BKW_MMAP_RUNNING);
860 return &maps[idx].core;
864 perf_evlist__mmap_cb_mmap(struct perf_mmap *_map, struct perf_mmap_param *_mp,
865 int output, struct perf_cpu cpu)
867 struct mmap *map = container_of(_map, struct mmap, core);
868 struct mmap_params *mp = container_of(_mp, struct mmap_params, core);
870 return mmap__mmap(map, mp, output, cpu);
873 unsigned long perf_event_mlock_kb_in_pages(void)
878 if (sysctl__read_int("kernel/perf_event_mlock_kb", &max) < 0) {
880 * Pick a once upon a time good value, i.e. things look
881 * strange since we can't read a sysctl value, but lets not
886 max -= (page_size / 1024);
889 pages = (max * 1024) / page_size;
890 if (!is_power_of_2(pages))
891 pages = rounddown_pow_of_two(pages);
896 size_t evlist__mmap_size(unsigned long pages)
898 if (pages == UINT_MAX)
899 pages = perf_event_mlock_kb_in_pages();
900 else if (!is_power_of_2(pages))
903 return (pages + 1) * page_size;
906 static long parse_pages_arg(const char *str, unsigned long min,
909 unsigned long pages, val;
910 static struct parse_tag tags[] = {
911 { .tag = 'B', .mult = 1 },
912 { .tag = 'K', .mult = 1 << 10 },
913 { .tag = 'M', .mult = 1 << 20 },
914 { .tag = 'G', .mult = 1 << 30 },
921 val = parse_tag_value(str, tags);
922 if (val != (unsigned long) -1) {
923 /* we got file size value */
924 pages = PERF_ALIGN(val, page_size) / page_size;
926 /* we got pages count value */
928 pages = strtoul(str, &eptr, 10);
933 if (pages == 0 && min == 0) {
934 /* leave number of pages at 0 */
935 } else if (!is_power_of_2(pages)) {
938 /* round pages up to next power of 2 */
939 pages = roundup_pow_of_two(pages);
943 unit_number__scnprintf(buf, sizeof(buf), pages * page_size);
944 pr_info("rounding mmap pages size to %s (%lu pages)\n",
954 int __evlist__parse_mmap_pages(unsigned int *mmap_pages, const char *str)
956 unsigned long max = UINT_MAX;
959 if (max > SIZE_MAX / page_size)
960 max = SIZE_MAX / page_size;
962 pages = parse_pages_arg(str, 1, max);
964 pr_err("Invalid argument for --mmap_pages/-m\n");
972 int evlist__parse_mmap_pages(const struct option *opt, const char *str, int unset __maybe_unused)
974 return __evlist__parse_mmap_pages(opt->value, str);
978 * evlist__mmap_ex - Create mmaps to receive events.
979 * @evlist: list of events
980 * @pages: map length in pages
981 * @overwrite: overwrite older events?
982 * @auxtrace_pages - auxtrace map length in pages
983 * @auxtrace_overwrite - overwrite older auxtrace data?
985 * If @overwrite is %false the user needs to signal event consumption using
986 * perf_mmap__write_tail(). Using evlist__mmap_read() does this
989 * Similarly, if @auxtrace_overwrite is %false the user needs to signal data
990 * consumption using auxtrace_mmap__write_tail().
992 * Return: %0 on success, negative error code otherwise.
994 int evlist__mmap_ex(struct evlist *evlist, unsigned int pages,
995 unsigned int auxtrace_pages,
996 bool auxtrace_overwrite, int nr_cblocks, int affinity, int flush,
1000 * Delay setting mp.prot: set it before calling perf_mmap__mmap.
1001 * Its value is decided by evsel's write_backward.
1002 * So &mp should not be passed through const pointer.
1004 struct mmap_params mp = {
1005 .nr_cblocks = nr_cblocks,
1006 .affinity = affinity,
1008 .comp_level = comp_level
1010 struct perf_evlist_mmap_ops ops = {
1011 .idx = perf_evlist__mmap_cb_idx,
1012 .get = perf_evlist__mmap_cb_get,
1013 .mmap = perf_evlist__mmap_cb_mmap,
1016 evlist->core.mmap_len = evlist__mmap_size(pages);
1017 pr_debug("mmap size %zuB\n", evlist->core.mmap_len);
1019 auxtrace_mmap_params__init(&mp.auxtrace_mp, evlist->core.mmap_len,
1020 auxtrace_pages, auxtrace_overwrite);
1022 return perf_evlist__mmap_ops(&evlist->core, &ops, &mp.core);
1025 int evlist__mmap(struct evlist *evlist, unsigned int pages)
1027 return evlist__mmap_ex(evlist, pages, 0, false, 0, PERF_AFFINITY_SYS, 1, 0);
1030 int evlist__create_maps(struct evlist *evlist, struct target *target)
1032 bool all_threads = (target->per_thread && target->system_wide);
1033 struct perf_cpu_map *cpus;
1034 struct perf_thread_map *threads;
1037 * If specify '-a' and '--per-thread' to perf record, perf record
1038 * will override '--per-thread'. target->per_thread = false and
1039 * target->system_wide = true.
1041 * If specify '--per-thread' only to perf record,
1042 * target->per_thread = true and target->system_wide = false.
1044 * So target->per_thread && target->system_wide is false.
1045 * For perf record, thread_map__new_str doesn't call
1046 * thread_map__new_all_cpus. That will keep perf record's
1049 * For perf stat, it allows the case that target->per_thread and
1050 * target->system_wide are all true. It means to collect system-wide
1051 * per-thread data. thread_map__new_str will call
1052 * thread_map__new_all_cpus to enumerate all threads.
1054 threads = thread_map__new_str(target->pid, target->tid, target->uid,
1060 if (target__uses_dummy_map(target))
1061 cpus = perf_cpu_map__dummy_new();
1063 cpus = perf_cpu_map__new(target->cpu_list);
1066 goto out_delete_threads;
1068 evlist->core.has_user_cpus = !!target->cpu_list && !target->hybrid;
1070 perf_evlist__set_maps(&evlist->core, cpus, threads);
1072 /* as evlist now has references, put count here */
1073 perf_cpu_map__put(cpus);
1074 perf_thread_map__put(threads);
1079 perf_thread_map__put(threads);
1083 int evlist__apply_filters(struct evlist *evlist, struct evsel **err_evsel)
1085 struct evsel *evsel;
1088 evlist__for_each_entry(evlist, evsel) {
1089 if (evsel->filter == NULL)
1093 * filters only work for tracepoint event, which doesn't have cpu limit.
1094 * So evlist and evsel should always be same.
1096 err = perf_evsel__apply_filter(&evsel->core, evsel->filter);
1106 int evlist__set_tp_filter(struct evlist *evlist, const char *filter)
1108 struct evsel *evsel;
1114 evlist__for_each_entry(evlist, evsel) {
1115 if (evsel->core.attr.type != PERF_TYPE_TRACEPOINT)
1118 err = evsel__set_filter(evsel, filter);
1126 int evlist__append_tp_filter(struct evlist *evlist, const char *filter)
1128 struct evsel *evsel;
1134 evlist__for_each_entry(evlist, evsel) {
1135 if (evsel->core.attr.type != PERF_TYPE_TRACEPOINT)
1138 err = evsel__append_tp_filter(evsel, filter);
1146 char *asprintf__tp_filter_pids(size_t npids, pid_t *pids)
1151 for (i = 0; i < npids; ++i) {
1153 if (asprintf(&filter, "common_pid != %d", pids[i]) < 0)
1158 if (asprintf(&tmp, "%s && common_pid != %d", filter, pids[i]) < 0)
1172 int evlist__set_tp_filter_pids(struct evlist *evlist, size_t npids, pid_t *pids)
1174 char *filter = asprintf__tp_filter_pids(npids, pids);
1175 int ret = evlist__set_tp_filter(evlist, filter);
1181 int evlist__set_tp_filter_pid(struct evlist *evlist, pid_t pid)
1183 return evlist__set_tp_filter_pids(evlist, 1, &pid);
1186 int evlist__append_tp_filter_pids(struct evlist *evlist, size_t npids, pid_t *pids)
1188 char *filter = asprintf__tp_filter_pids(npids, pids);
1189 int ret = evlist__append_tp_filter(evlist, filter);
1195 int evlist__append_tp_filter_pid(struct evlist *evlist, pid_t pid)
1197 return evlist__append_tp_filter_pids(evlist, 1, &pid);
1200 bool evlist__valid_sample_type(struct evlist *evlist)
1204 if (evlist->core.nr_entries == 1)
1207 if (evlist->id_pos < 0 || evlist->is_pos < 0)
1210 evlist__for_each_entry(evlist, pos) {
1211 if (pos->id_pos != evlist->id_pos ||
1212 pos->is_pos != evlist->is_pos)
1219 u64 __evlist__combined_sample_type(struct evlist *evlist)
1221 struct evsel *evsel;
1223 if (evlist->combined_sample_type)
1224 return evlist->combined_sample_type;
1226 evlist__for_each_entry(evlist, evsel)
1227 evlist->combined_sample_type |= evsel->core.attr.sample_type;
1229 return evlist->combined_sample_type;
1232 u64 evlist__combined_sample_type(struct evlist *evlist)
1234 evlist->combined_sample_type = 0;
1235 return __evlist__combined_sample_type(evlist);
1238 u64 evlist__combined_branch_type(struct evlist *evlist)
1240 struct evsel *evsel;
1241 u64 branch_type = 0;
1243 evlist__for_each_entry(evlist, evsel)
1244 branch_type |= evsel->core.attr.branch_sample_type;
1248 bool evlist__valid_read_format(struct evlist *evlist)
1250 struct evsel *first = evlist__first(evlist), *pos = first;
1251 u64 read_format = first->core.attr.read_format;
1252 u64 sample_type = first->core.attr.sample_type;
1254 evlist__for_each_entry(evlist, pos) {
1255 if (read_format != pos->core.attr.read_format) {
1256 pr_debug("Read format differs %#" PRIx64 " vs %#" PRIx64 "\n",
1257 read_format, (u64)pos->core.attr.read_format);
1261 /* PERF_SAMPLE_READ implies PERF_FORMAT_ID. */
1262 if ((sample_type & PERF_SAMPLE_READ) &&
1263 !(read_format & PERF_FORMAT_ID)) {
1270 u16 evlist__id_hdr_size(struct evlist *evlist)
1272 struct evsel *first = evlist__first(evlist);
1274 return first->core.attr.sample_id_all ? evsel__id_hdr_size(first) : 0;
1277 bool evlist__valid_sample_id_all(struct evlist *evlist)
1279 struct evsel *first = evlist__first(evlist), *pos = first;
1281 evlist__for_each_entry_continue(evlist, pos) {
1282 if (first->core.attr.sample_id_all != pos->core.attr.sample_id_all)
1289 bool evlist__sample_id_all(struct evlist *evlist)
1291 struct evsel *first = evlist__first(evlist);
1292 return first->core.attr.sample_id_all;
1295 void evlist__set_selected(struct evlist *evlist, struct evsel *evsel)
1297 evlist->selected = evsel;
1300 void evlist__close(struct evlist *evlist)
1302 struct evsel *evsel;
1303 struct evlist_cpu_iterator evlist_cpu_itr;
1304 struct affinity affinity;
1307 * With perf record core.user_requested_cpus is usually NULL.
1308 * Use the old method to handle this for now.
1310 if (!evlist->core.user_requested_cpus ||
1311 cpu_map__is_dummy(evlist->core.user_requested_cpus)) {
1312 evlist__for_each_entry_reverse(evlist, evsel)
1313 evsel__close(evsel);
1317 if (affinity__setup(&affinity) < 0)
1320 evlist__for_each_cpu(evlist_cpu_itr, evlist, &affinity) {
1321 perf_evsel__close_cpu(&evlist_cpu_itr.evsel->core,
1322 evlist_cpu_itr.cpu_map_idx);
1325 affinity__cleanup(&affinity);
1326 evlist__for_each_entry_reverse(evlist, evsel) {
1327 perf_evsel__free_fd(&evsel->core);
1328 perf_evsel__free_id(&evsel->core);
1330 perf_evlist__reset_id_hash(&evlist->core);
1333 static int evlist__create_syswide_maps(struct evlist *evlist)
1335 struct perf_cpu_map *cpus;
1336 struct perf_thread_map *threads;
1339 * Try reading /sys/devices/system/cpu/online to get
1342 * FIXME: -ENOMEM is the best we can do here, the cpu_map
1343 * code needs an overhaul to properly forward the
1344 * error, and we may not want to do that fallback to a
1345 * default cpu identity map :-\
1347 cpus = perf_cpu_map__new(NULL);
1351 threads = perf_thread_map__new_dummy();
1355 perf_evlist__set_maps(&evlist->core, cpus, threads);
1357 perf_thread_map__put(threads);
1359 perf_cpu_map__put(cpus);
1364 int evlist__open(struct evlist *evlist)
1366 struct evsel *evsel;
1370 * Default: one fd per CPU, all threads, aka systemwide
1371 * as sys_perf_event_open(cpu = -1, thread = -1) is EINVAL
1373 if (evlist->core.threads == NULL && evlist->core.user_requested_cpus == NULL) {
1374 err = evlist__create_syswide_maps(evlist);
1379 evlist__update_id_pos(evlist);
1381 evlist__for_each_entry(evlist, evsel) {
1382 err = evsel__open(evsel, evsel->core.cpus, evsel->core.threads);
1389 evlist__close(evlist);
1394 int evlist__prepare_workload(struct evlist *evlist, struct target *target, const char *argv[],
1395 bool pipe_output, void (*exec_error)(int signo, siginfo_t *info, void *ucontext))
1397 int child_ready_pipe[2], go_pipe[2];
1400 if (pipe(child_ready_pipe) < 0) {
1401 perror("failed to create 'ready' pipe");
1405 if (pipe(go_pipe) < 0) {
1406 perror("failed to create 'go' pipe");
1407 goto out_close_ready_pipe;
1410 evlist->workload.pid = fork();
1411 if (evlist->workload.pid < 0) {
1412 perror("failed to fork");
1413 goto out_close_pipes;
1416 if (!evlist->workload.pid) {
1422 signal(SIGTERM, SIG_DFL);
1424 close(child_ready_pipe[0]);
1426 fcntl(go_pipe[0], F_SETFD, FD_CLOEXEC);
1429 * Change the name of this process not to confuse --exclude-perf users
1430 * that sees 'perf' in the window up to the execvp() and thinks that
1431 * perf samples are not being excluded.
1433 prctl(PR_SET_NAME, "perf-exec");
1436 * Tell the parent we're ready to go
1438 close(child_ready_pipe[1]);
1441 * Wait until the parent tells us to go.
1443 ret = read(go_pipe[0], &bf, 1);
1445 * The parent will ask for the execvp() to be performed by
1446 * writing exactly one byte, in workload.cork_fd, usually via
1447 * evlist__start_workload().
1449 * For cancelling the workload without actually running it,
1450 * the parent will just close workload.cork_fd, without writing
1451 * anything, i.e. read will return zero and we just exit()
1456 perror("unable to read pipe");
1460 execvp(argv[0], (char **)argv);
1465 val.sival_int = errno;
1466 if (sigqueue(getppid(), SIGUSR1, val))
1474 struct sigaction act = {
1475 .sa_flags = SA_SIGINFO,
1476 .sa_sigaction = exec_error,
1478 sigaction(SIGUSR1, &act, NULL);
1481 if (target__none(target)) {
1482 if (evlist->core.threads == NULL) {
1483 fprintf(stderr, "FATAL: evlist->threads need to be set at this point (%s:%d).\n",
1484 __func__, __LINE__);
1485 goto out_close_pipes;
1487 perf_thread_map__set_pid(evlist->core.threads, 0, evlist->workload.pid);
1490 close(child_ready_pipe[1]);
1493 * wait for child to settle
1495 if (read(child_ready_pipe[0], &bf, 1) == -1) {
1496 perror("unable to read pipe");
1497 goto out_close_pipes;
1500 fcntl(go_pipe[1], F_SETFD, FD_CLOEXEC);
1501 evlist->workload.cork_fd = go_pipe[1];
1502 close(child_ready_pipe[0]);
1508 out_close_ready_pipe:
1509 close(child_ready_pipe[0]);
1510 close(child_ready_pipe[1]);
1514 int evlist__start_workload(struct evlist *evlist)
1516 if (evlist->workload.cork_fd > 0) {
1520 * Remove the cork, let it rip!
1522 ret = write(evlist->workload.cork_fd, &bf, 1);
1524 perror("unable to write to pipe");
1526 close(evlist->workload.cork_fd);
1533 int evlist__parse_sample(struct evlist *evlist, union perf_event *event, struct perf_sample *sample)
1535 struct evsel *evsel = evlist__event2evsel(evlist, event);
1540 ret = evsel__parse_sample(evsel, event, sample);
1543 if (perf_guest && sample->id) {
1544 struct perf_sample_id *sid = evlist__id2sid(evlist, sample->id);
1547 sample->machine_pid = sid->machine_pid;
1548 sample->vcpu = sid->vcpu.cpu;
1554 int evlist__parse_sample_timestamp(struct evlist *evlist, union perf_event *event, u64 *timestamp)
1556 struct evsel *evsel = evlist__event2evsel(evlist, event);
1560 return evsel__parse_sample_timestamp(evsel, event, timestamp);
1563 int evlist__strerror_open(struct evlist *evlist, int err, char *buf, size_t size)
1566 char sbuf[STRERR_BUFSIZE], *emsg = str_error_r(err, sbuf, sizeof(sbuf));
1571 printed = scnprintf(buf, size,
1573 "Hint:\tCheck /proc/sys/kernel/perf_event_paranoid setting.", emsg);
1575 value = perf_event_paranoid();
1577 printed += scnprintf(buf + printed, size - printed, "\nHint:\t");
1580 printed += scnprintf(buf + printed, size - printed,
1581 "For your workloads it needs to be <= 1\nHint:\t");
1583 printed += scnprintf(buf + printed, size - printed,
1584 "For system wide tracing it needs to be set to -1.\n");
1586 printed += scnprintf(buf + printed, size - printed,
1587 "Hint:\tTry: 'sudo sh -c \"echo -1 > /proc/sys/kernel/perf_event_paranoid\"'\n"
1588 "Hint:\tThe current value is %d.", value);
1591 struct evsel *first = evlist__first(evlist);
1594 if (sysctl__read_int("kernel/perf_event_max_sample_rate", &max_freq) < 0)
1597 if (first->core.attr.sample_freq < (u64)max_freq)
1600 printed = scnprintf(buf, size,
1602 "Hint:\tCheck /proc/sys/kernel/perf_event_max_sample_rate.\n"
1603 "Hint:\tThe current value is %d and %" PRIu64 " is being requested.",
1604 emsg, max_freq, first->core.attr.sample_freq);
1609 scnprintf(buf, size, "%s", emsg);
1616 int evlist__strerror_mmap(struct evlist *evlist, int err, char *buf, size_t size)
1618 char sbuf[STRERR_BUFSIZE], *emsg = str_error_r(err, sbuf, sizeof(sbuf));
1619 int pages_attempted = evlist->core.mmap_len / 1024, pages_max_per_user, printed = 0;
1623 sysctl__read_int("kernel/perf_event_mlock_kb", &pages_max_per_user);
1624 printed += scnprintf(buf + printed, size - printed,
1626 "Hint:\tCheck /proc/sys/kernel/perf_event_mlock_kb (%d kB) setting.\n"
1627 "Hint:\tTried using %zd kB.\n",
1628 emsg, pages_max_per_user, pages_attempted);
1630 if (pages_attempted >= pages_max_per_user) {
1631 printed += scnprintf(buf + printed, size - printed,
1632 "Hint:\tTry 'sudo sh -c \"echo %d > /proc/sys/kernel/perf_event_mlock_kb\"', or\n",
1633 pages_max_per_user + pages_attempted);
1636 printed += scnprintf(buf + printed, size - printed,
1637 "Hint:\tTry using a smaller -m/--mmap-pages value.");
1640 scnprintf(buf, size, "%s", emsg);
1647 void evlist__to_front(struct evlist *evlist, struct evsel *move_evsel)
1649 struct evsel *evsel, *n;
1652 if (move_evsel == evlist__first(evlist))
1655 evlist__for_each_entry_safe(evlist, n, evsel) {
1656 if (evsel__leader(evsel) == evsel__leader(move_evsel))
1657 list_move_tail(&evsel->core.node, &move);
1660 list_splice(&move, &evlist->core.entries);
1663 struct evsel *evlist__get_tracking_event(struct evlist *evlist)
1665 struct evsel *evsel;
1667 evlist__for_each_entry(evlist, evsel) {
1668 if (evsel->tracking)
1672 return evlist__first(evlist);
1675 void evlist__set_tracking_event(struct evlist *evlist, struct evsel *tracking_evsel)
1677 struct evsel *evsel;
1679 if (tracking_evsel->tracking)
1682 evlist__for_each_entry(evlist, evsel) {
1683 if (evsel != tracking_evsel)
1684 evsel->tracking = false;
1687 tracking_evsel->tracking = true;
1690 struct evsel *evlist__find_evsel_by_str(struct evlist *evlist, const char *str)
1692 struct evsel *evsel;
1694 evlist__for_each_entry(evlist, evsel) {
1697 if (strcmp(str, evsel->name) == 0)
1704 void evlist__toggle_bkw_mmap(struct evlist *evlist, enum bkw_mmap_state state)
1706 enum bkw_mmap_state old_state = evlist->bkw_mmap_state;
1713 if (!evlist->overwrite_mmap)
1716 switch (old_state) {
1717 case BKW_MMAP_NOTREADY: {
1718 if (state != BKW_MMAP_RUNNING)
1722 case BKW_MMAP_RUNNING: {
1723 if (state != BKW_MMAP_DATA_PENDING)
1728 case BKW_MMAP_DATA_PENDING: {
1729 if (state != BKW_MMAP_EMPTY)
1733 case BKW_MMAP_EMPTY: {
1734 if (state != BKW_MMAP_RUNNING)
1740 WARN_ONCE(1, "Shouldn't get there\n");
1743 evlist->bkw_mmap_state = state;
1747 evlist__pause(evlist);
1750 evlist__resume(evlist);
1761 bool evlist__exclude_kernel(struct evlist *evlist)
1763 struct evsel *evsel;
1765 evlist__for_each_entry(evlist, evsel) {
1766 if (!evsel->core.attr.exclude_kernel)
1774 * Events in data file are not collect in groups, but we still want
1775 * the group display. Set the artificial group and set the leader's
1776 * forced_leader flag to notify the display code.
1778 void evlist__force_leader(struct evlist *evlist)
1780 if (!evlist->core.nr_groups) {
1781 struct evsel *leader = evlist__first(evlist);
1783 evlist__set_leader(evlist);
1784 leader->forced_leader = true;
1788 struct evsel *evlist__reset_weak_group(struct evlist *evsel_list, struct evsel *evsel, bool close)
1790 struct evsel *c2, *leader;
1791 bool is_open = true;
1793 leader = evsel__leader(evsel);
1795 pr_debug("Weak group for %s/%d failed\n",
1796 leader->name, leader->core.nr_members);
1799 * for_each_group_member doesn't work here because it doesn't
1800 * include the first entry.
1802 evlist__for_each_entry(evsel_list, c2) {
1805 if (evsel__has_leader(c2, leader)) {
1806 if (is_open && close)
1807 perf_evsel__close(&c2->core);
1809 * We want to close all members of the group and reopen
1810 * them. Some events, like Intel topdown, require being
1811 * in a group and so keep these in the group.
1813 evsel__remove_from_group(c2, leader);
1816 * Set this for all former members of the group
1817 * to indicate they get reopened.
1819 c2->reset_group = true;
1822 /* Reset the leader count if all entries were removed. */
1823 if (leader->core.nr_members == 1)
1824 leader->core.nr_members = 0;
1828 static int evlist__parse_control_fifo(const char *str, int *ctl_fd, int *ctl_fd_ack, bool *ctl_fd_close)
1833 if (strncmp(str, "fifo:", 5))
1837 if (!*str || *str == ',')
1849 * O_RDWR avoids POLLHUPs which is necessary to allow the other
1850 * end of a FIFO to be repeatedly opened and closed.
1852 fd = open(s, O_RDWR | O_NONBLOCK | O_CLOEXEC);
1854 pr_err("Failed to open '%s'\n", s);
1859 *ctl_fd_close = true;
1862 /* O_RDWR | O_NONBLOCK means the other end need not be open */
1863 fd = open(p, O_RDWR | O_NONBLOCK | O_CLOEXEC);
1865 pr_err("Failed to open '%s'\n", p);
1877 int evlist__parse_control(const char *str, int *ctl_fd, int *ctl_fd_ack, bool *ctl_fd_close)
1879 char *comma = NULL, *endptr = NULL;
1881 *ctl_fd_close = false;
1883 if (strncmp(str, "fd:", 3))
1884 return evlist__parse_control_fifo(str, ctl_fd, ctl_fd_ack, ctl_fd_close);
1886 *ctl_fd = strtoul(&str[3], &endptr, 0);
1887 if (endptr == &str[3])
1890 comma = strchr(str, ',');
1892 if (endptr != comma)
1895 *ctl_fd_ack = strtoul(comma + 1, &endptr, 0);
1896 if (endptr == comma + 1 || *endptr != '\0')
1903 void evlist__close_control(int ctl_fd, int ctl_fd_ack, bool *ctl_fd_close)
1905 if (*ctl_fd_close) {
1906 *ctl_fd_close = false;
1908 if (ctl_fd_ack >= 0)
1913 int evlist__initialize_ctlfd(struct evlist *evlist, int fd, int ack)
1916 pr_debug("Control descriptor is not initialized\n");
1920 evlist->ctl_fd.pos = perf_evlist__add_pollfd(&evlist->core, fd, NULL, POLLIN,
1921 fdarray_flag__nonfilterable |
1922 fdarray_flag__non_perf_event);
1923 if (evlist->ctl_fd.pos < 0) {
1924 evlist->ctl_fd.pos = -1;
1925 pr_err("Failed to add ctl fd entry: %m\n");
1929 evlist->ctl_fd.fd = fd;
1930 evlist->ctl_fd.ack = ack;
1935 bool evlist__ctlfd_initialized(struct evlist *evlist)
1937 return evlist->ctl_fd.pos >= 0;
1940 int evlist__finalize_ctlfd(struct evlist *evlist)
1942 struct pollfd *entries = evlist->core.pollfd.entries;
1944 if (!evlist__ctlfd_initialized(evlist))
1947 entries[evlist->ctl_fd.pos].fd = -1;
1948 entries[evlist->ctl_fd.pos].events = 0;
1949 entries[evlist->ctl_fd.pos].revents = 0;
1951 evlist->ctl_fd.pos = -1;
1952 evlist->ctl_fd.ack = -1;
1953 evlist->ctl_fd.fd = -1;
1958 static int evlist__ctlfd_recv(struct evlist *evlist, enum evlist_ctl_cmd *cmd,
1959 char *cmd_data, size_t data_size)
1963 size_t bytes_read = 0;
1965 *cmd = EVLIST_CTL_CMD_UNSUPPORTED;
1966 memset(cmd_data, 0, data_size);
1970 err = read(evlist->ctl_fd.fd, &c, 1);
1972 if (c == '\n' || c == '\0')
1974 cmd_data[bytes_read++] = c;
1975 if (bytes_read == data_size)
1978 } else if (err == -1) {
1981 if (errno == EAGAIN || errno == EWOULDBLOCK)
1984 pr_err("Failed to read from ctlfd %d: %m\n", evlist->ctl_fd.fd);
1989 pr_debug("Message from ctl_fd: \"%s%s\"\n", cmd_data,
1990 bytes_read == data_size ? "" : c == '\n' ? "\\n" : "\\0");
1992 if (bytes_read > 0) {
1993 if (!strncmp(cmd_data, EVLIST_CTL_CMD_ENABLE_TAG,
1994 (sizeof(EVLIST_CTL_CMD_ENABLE_TAG)-1))) {
1995 *cmd = EVLIST_CTL_CMD_ENABLE;
1996 } else if (!strncmp(cmd_data, EVLIST_CTL_CMD_DISABLE_TAG,
1997 (sizeof(EVLIST_CTL_CMD_DISABLE_TAG)-1))) {
1998 *cmd = EVLIST_CTL_CMD_DISABLE;
1999 } else if (!strncmp(cmd_data, EVLIST_CTL_CMD_SNAPSHOT_TAG,
2000 (sizeof(EVLIST_CTL_CMD_SNAPSHOT_TAG)-1))) {
2001 *cmd = EVLIST_CTL_CMD_SNAPSHOT;
2002 pr_debug("is snapshot\n");
2003 } else if (!strncmp(cmd_data, EVLIST_CTL_CMD_EVLIST_TAG,
2004 (sizeof(EVLIST_CTL_CMD_EVLIST_TAG)-1))) {
2005 *cmd = EVLIST_CTL_CMD_EVLIST;
2006 } else if (!strncmp(cmd_data, EVLIST_CTL_CMD_STOP_TAG,
2007 (sizeof(EVLIST_CTL_CMD_STOP_TAG)-1))) {
2008 *cmd = EVLIST_CTL_CMD_STOP;
2009 } else if (!strncmp(cmd_data, EVLIST_CTL_CMD_PING_TAG,
2010 (sizeof(EVLIST_CTL_CMD_PING_TAG)-1))) {
2011 *cmd = EVLIST_CTL_CMD_PING;
2015 return bytes_read ? (int)bytes_read : err;
2018 int evlist__ctlfd_ack(struct evlist *evlist)
2022 if (evlist->ctl_fd.ack == -1)
2025 err = write(evlist->ctl_fd.ack, EVLIST_CTL_CMD_ACK_TAG,
2026 sizeof(EVLIST_CTL_CMD_ACK_TAG));
2028 pr_err("failed to write to ctl_ack_fd %d: %m\n", evlist->ctl_fd.ack);
2033 static int get_cmd_arg(char *cmd_data, size_t cmd_size, char **arg)
2035 char *data = cmd_data + cmd_size;
2041 /* there's argument */
2051 static int evlist__ctlfd_enable(struct evlist *evlist, char *cmd_data, bool enable)
2053 struct evsel *evsel;
2057 err = get_cmd_arg(cmd_data,
2058 enable ? sizeof(EVLIST_CTL_CMD_ENABLE_TAG) - 1 :
2059 sizeof(EVLIST_CTL_CMD_DISABLE_TAG) - 1,
2062 pr_info("failed: wrong command\n");
2067 evsel = evlist__find_evsel_by_str(evlist, name);
2070 evlist__enable_evsel(evlist, name);
2072 evlist__disable_evsel(evlist, name);
2073 pr_info("Event %s %s\n", evsel->name,
2074 enable ? "enabled" : "disabled");
2076 pr_info("failed: can't find '%s' event\n", name);
2080 evlist__enable(evlist);
2081 pr_info(EVLIST_ENABLED_MSG);
2083 evlist__disable(evlist);
2084 pr_info(EVLIST_DISABLED_MSG);
2091 static int evlist__ctlfd_list(struct evlist *evlist, char *cmd_data)
2093 struct perf_attr_details details = { .verbose = false, };
2094 struct evsel *evsel;
2098 err = get_cmd_arg(cmd_data,
2099 sizeof(EVLIST_CTL_CMD_EVLIST_TAG) - 1,
2102 pr_info("failed: wrong command\n");
2107 if (!strcmp(arg, "-v")) {
2108 details.verbose = true;
2109 } else if (!strcmp(arg, "-g")) {
2110 details.event_group = true;
2111 } else if (!strcmp(arg, "-F")) {
2112 details.freq = true;
2114 pr_info("failed: wrong command\n");
2119 evlist__for_each_entry(evlist, evsel)
2120 evsel__fprintf(evsel, &details, stderr);
2125 int evlist__ctlfd_process(struct evlist *evlist, enum evlist_ctl_cmd *cmd)
2128 char cmd_data[EVLIST_CTL_CMD_MAX_LEN];
2129 int ctlfd_pos = evlist->ctl_fd.pos;
2130 struct pollfd *entries = evlist->core.pollfd.entries;
2132 if (!evlist__ctlfd_initialized(evlist) || !entries[ctlfd_pos].revents)
2135 if (entries[ctlfd_pos].revents & POLLIN) {
2136 err = evlist__ctlfd_recv(evlist, cmd, cmd_data,
2137 EVLIST_CTL_CMD_MAX_LEN);
2140 case EVLIST_CTL_CMD_ENABLE:
2141 case EVLIST_CTL_CMD_DISABLE:
2142 err = evlist__ctlfd_enable(evlist, cmd_data,
2143 *cmd == EVLIST_CTL_CMD_ENABLE);
2145 case EVLIST_CTL_CMD_EVLIST:
2146 err = evlist__ctlfd_list(evlist, cmd_data);
2148 case EVLIST_CTL_CMD_SNAPSHOT:
2149 case EVLIST_CTL_CMD_STOP:
2150 case EVLIST_CTL_CMD_PING:
2152 case EVLIST_CTL_CMD_ACK:
2153 case EVLIST_CTL_CMD_UNSUPPORTED:
2155 pr_debug("ctlfd: unsupported %d\n", *cmd);
2158 if (!(*cmd == EVLIST_CTL_CMD_ACK || *cmd == EVLIST_CTL_CMD_UNSUPPORTED ||
2159 *cmd == EVLIST_CTL_CMD_SNAPSHOT))
2160 evlist__ctlfd_ack(evlist);
2164 if (entries[ctlfd_pos].revents & (POLLHUP | POLLERR))
2165 evlist__finalize_ctlfd(evlist);
2167 entries[ctlfd_pos].revents = 0;
2173 * struct event_enable_time - perf record -D/--delay single time range.
2174 * @start: start of time range to enable events in milliseconds
2175 * @end: end of time range to enable events in milliseconds
2177 * N.B. this structure is also accessed as an array of int.
2179 struct event_enable_time {
2184 static int parse_event_enable_time(const char *str, struct event_enable_time *range, bool first)
2186 const char *fmt = first ? "%u - %u %n" : " , %u - %u %n";
2187 int ret, start, end, n;
2189 ret = sscanf(str, fmt, &start, &end, &n);
2190 if (ret != 2 || end <= start)
2193 range->start = start;
2199 static ssize_t parse_event_enable_times(const char *str, struct event_enable_time *range)
2205 for (cnt = 0; *str; cnt++) {
2206 ret = parse_event_enable_time(str, range, first);
2209 /* Check no overlap */
2210 if (!first && range && range->start <= range[-1].end)
2220 * struct event_enable_timer - control structure for perf record -D/--delay.
2221 * @evlist: event list
2222 * @times: time ranges that events are enabled (N.B. this is also accessed as an
2224 * @times_cnt: number of time ranges
2225 * @timerfd: timer file descriptor
2226 * @pollfd_pos: position in @evlist array of file descriptors to poll (fdarray)
2227 * @times_step: current position in (int *)@times)[],
2228 * refer event_enable_timer__process()
2230 * Note, this structure is only used when there are time ranges, not when there
2231 * is only an initial delay.
2233 struct event_enable_timer {
2234 struct evlist *evlist;
2235 struct event_enable_time *times;
2242 static int str_to_delay(const char *str)
2247 d = strtol(str, &endptr, 10);
2248 if (*endptr || d > INT_MAX || d < -1)
2253 int evlist__parse_event_enable_time(struct evlist *evlist, struct record_opts *opts,
2254 const char *str, int unset)
2256 enum fdarray_flags flags = fdarray_flag__nonfilterable | fdarray_flag__non_perf_event;
2257 struct event_enable_timer *eet;
2265 opts->initial_delay = str_to_delay(str);
2266 if (opts->initial_delay)
2269 ret = parse_event_enable_times(str, NULL);
2277 eet = zalloc(sizeof(*eet));
2281 eet->times = calloc(times_cnt, sizeof(*eet->times));
2287 if (parse_event_enable_times(str, eet->times) != times_cnt) {
2289 goto free_eet_times;
2292 eet->times_cnt = times_cnt;
2294 eet->timerfd = timerfd_create(CLOCK_MONOTONIC, TFD_CLOEXEC);
2295 if (eet->timerfd == -1) {
2297 pr_err("timerfd_create failed: %s\n", strerror(errno));
2298 goto free_eet_times;
2301 eet->pollfd_pos = perf_evlist__add_pollfd(&evlist->core, eet->timerfd, NULL, POLLIN, flags);
2302 if (eet->pollfd_pos < 0) {
2303 err = eet->pollfd_pos;
2307 eet->evlist = evlist;
2309 opts->initial_delay = eet->times[0].start;
2314 close(eet->timerfd);
2322 static int event_enable_timer__set_timer(struct event_enable_timer *eet, int ms)
2324 struct itimerspec its = {
2325 .it_value.tv_sec = ms / MSEC_PER_SEC,
2326 .it_value.tv_nsec = (ms % MSEC_PER_SEC) * NSEC_PER_MSEC,
2330 if (timerfd_settime(eet->timerfd, 0, &its, NULL) < 0) {
2332 pr_err("timerfd_settime failed: %s\n", strerror(errno));
2337 int event_enable_timer__start(struct event_enable_timer *eet)
2344 ms = eet->times[0].end - eet->times[0].start;
2345 eet->times_step = 1;
2347 return event_enable_timer__set_timer(eet, ms);
2350 int event_enable_timer__process(struct event_enable_timer *eet)
2352 struct pollfd *entries;
2358 entries = eet->evlist->core.pollfd.entries;
2359 revents = entries[eet->pollfd_pos].revents;
2360 entries[eet->pollfd_pos].revents = 0;
2362 if (revents & POLLIN) {
2363 size_t step = eet->times_step;
2364 size_t pos = step / 2;
2367 evlist__disable_non_dummy(eet->evlist);
2368 pr_info(EVLIST_DISABLED_MSG);
2369 if (pos >= eet->times_cnt - 1) {
2371 event_enable_timer__set_timer(eet, 0);
2372 return 1; /* Stop */
2375 evlist__enable_non_dummy(eet->evlist);
2376 pr_info(EVLIST_ENABLED_MSG);
2382 if (pos < eet->times_cnt) {
2383 int *times = (int *)eet->times; /* Accessing 'times' as array of int */
2384 int ms = times[step] - times[step - 1];
2386 eet->times_step = step;
2387 return event_enable_timer__set_timer(eet, ms);
2394 void event_enable_timer__exit(struct event_enable_timer **ep)
2402 struct evsel *evlist__find_evsel(struct evlist *evlist, int idx)
2404 struct evsel *evsel;
2406 evlist__for_each_entry(evlist, evsel) {
2407 if (evsel->core.idx == idx)
2413 int evlist__scnprintf_evsels(struct evlist *evlist, size_t size, char *bf)
2415 struct evsel *evsel;
2418 evlist__for_each_entry(evlist, evsel) {
2419 if (evsel__is_dummy_event(evsel))
2421 if (size > (strlen(evsel__name(evsel)) + (printed ? 2 : 1))) {
2422 printed += scnprintf(bf + printed, size - printed, "%s%s", printed ? "," : "", evsel__name(evsel));
2424 printed += scnprintf(bf + printed, size - printed, "%s...", printed ? "," : "");
2432 void evlist__check_mem_load_aux(struct evlist *evlist)
2434 struct evsel *leader, *evsel, *pos;
2437 * For some platforms, the 'mem-loads' event is required to use
2438 * together with 'mem-loads-aux' within a group and 'mem-loads-aux'
2439 * must be the group leader. Now we disable this group before reporting
2440 * because 'mem-loads-aux' is just an auxiliary event. It doesn't carry
2441 * any valid memory load information.
2443 evlist__for_each_entry(evlist, evsel) {
2444 leader = evsel__leader(evsel);
2445 if (leader == evsel)
2448 if (leader->name && strstr(leader->name, "mem-loads-aux")) {
2449 for_each_group_evsel(pos, leader) {
2450 evsel__set_leader(pos, pos);
2451 pos->core.nr_members = 0;