xref: /openbmc/linux/tools/perf/util/evlist.c (revision 67a21d9b)
191007045SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2f8a95309SArnaldo Carvalho de Melo /*
3f8a95309SArnaldo Carvalho de Melo  * Copyright (C) 2011, Red Hat Inc, Arnaldo Carvalho de Melo <acme@redhat.com>
4f8a95309SArnaldo Carvalho de Melo  *
5f8a95309SArnaldo Carvalho de Melo  * Parts came from builtin-{top,stat,record}.c, see those files for further
6f8a95309SArnaldo Carvalho de Melo  * copyright notes.
7f8a95309SArnaldo Carvalho de Melo  */
8956fa571SArnaldo Carvalho de Melo #include <api/fs/fs.h>
9a43783aeSArnaldo Carvalho de Melo #include <errno.h>
10fd20e811SArnaldo Carvalho de Melo #include <inttypes.h>
115c581041SArnaldo Carvalho de Melo #include <poll.h>
12f8a95309SArnaldo Carvalho de Melo #include "cpumap.h"
13e0fcfb08SArnaldo Carvalho de Melo #include "util/mmap.h"
14f8a95309SArnaldo Carvalho de Melo #include "thread_map.h"
1512864b31SNamhyung Kim #include "target.h"
16361c99a6SArnaldo Carvalho de Melo #include "evlist.h"
17361c99a6SArnaldo Carvalho de Melo #include "evsel.h"
186657a099SAdrian Hunter #include "record.h"
19e3e1a54fSAdrian Hunter #include "debug.h"
2058db1d6eSArnaldo Carvalho de Melo #include "units.h"
215508c9daSSong Liu #include "bpf_counter.h"
2220f2be1dSJiri Olsa #include <internal/lib.h> // page_size
237736627bSAndi Kleen #include "affinity.h"
24c1a604dfSArnaldo Carvalho de Melo #include "../perf.h"
2554cc54deSWang Nan #include "asm/bug.h"
26657ee553SSong Liu #include "bpf-event.h"
272e5a738aSArnaldo Carvalho de Melo #include "util/event.h"
28da949f50SArnaldo Carvalho de Melo #include "util/string2.h"
2940c7d246SArnaldo Carvalho de Melo #include "util/perf_api_probe.h"
30142544a9SJiri Olsa #include "util/evsel_fprintf.h"
311d3351e6SJin Yao #include "util/pmu.h"
329823147dSArnaldo Carvalho de Melo #include "util/sample.h"
33d180aa56SNamhyung Kim #include "util/bpf-filter.h"
34ac873ac3SIan Rogers #include "util/stat.h"
35ea0c5239SIan Rogers #include "util/util.h"
369607ad3aSArnaldo Carvalho de Melo #include <signal.h>
3735b9d88eSArnaldo Carvalho de Melo #include <unistd.h>
38b397f846SArnaldo Carvalho de Melo #include <sched.h>
39f2a39fe8SArnaldo Carvalho de Melo #include <stdlib.h>
40361c99a6SArnaldo Carvalho de Melo 
4150d08e47SArnaldo Carvalho de Melo #include "parse-events.h"
424b6ab94eSJosh Poimboeuf #include <subcmd/parse-options.h>
4350d08e47SArnaldo Carvalho de Melo 
44bafae98eSArnaldo Carvalho de Melo #include <fcntl.h>
4586a5e0c2SArnaldo Carvalho de Melo #include <sys/ioctl.h>
46f8a95309SArnaldo Carvalho de Melo #include <sys/mman.h>
47a7672d1dSArnaldo Carvalho de Melo #include <sys/prctl.h>
486657a099SAdrian Hunter #include <sys/timerfd.h>
49f8a95309SArnaldo Carvalho de Melo 
5070db7533SArnaldo Carvalho de Melo #include <linux/bitops.h>
5170db7533SArnaldo Carvalho de Melo #include <linux/hash.h>
520389cd1fSArnaldo Carvalho de Melo #include <linux/log2.h>
538dd2a131SJiri Olsa #include <linux/err.h>
548520a98dSArnaldo Carvalho de Melo #include <linux/string.h>
556657a099SAdrian Hunter #include <linux/time64.h>
567f7c536fSArnaldo Carvalho de Melo #include <linux/zalloc.h>
574562a739SJiri Olsa #include <perf/evlist.h>
5888761fa1SJiri Olsa #include <perf/evsel.h>
599c3516d1SJiri Olsa #include <perf/cpumap.h>
607728fa0cSJiri Olsa #include <perf/mmap.h>
6170db7533SArnaldo Carvalho de Melo 
62e14e5497SArnaldo Carvalho de Melo #include <internal/xyarray.h>
63e14e5497SArnaldo Carvalho de Melo 
64748fe088SArnaldo Carvalho de Melo #ifdef LACKS_SIGQUEUE_PROTOTYPE
65748fe088SArnaldo Carvalho de Melo int sigqueue(pid_t pid, int sig, const union sigval value);
66748fe088SArnaldo Carvalho de Melo #endif
67748fe088SArnaldo Carvalho de Melo 
689dfcb759SJiri Olsa #define FD(e, x, y) (*(int *)xyarray__entry(e->core.fd, x, y))
698cd36f3eSJiri Olsa #define SID(e, x, y) xyarray__entry(e->core.sample_id, x, y)
70f8a95309SArnaldo Carvalho de Melo 
7152c86bcaSJiri Olsa void evlist__init(struct evlist *evlist, struct perf_cpu_map *cpus,
729749b90eSJiri Olsa 		  struct perf_thread_map *threads)
73361c99a6SArnaldo Carvalho de Melo {
744562a739SJiri Olsa 	perf_evlist__init(&evlist->core);
75453fa030SJiri Olsa 	perf_evlist__set_maps(&evlist->core, cpus, threads);
7635b9d88eSArnaldo Carvalho de Melo 	evlist->workload.pid = -1;
7754cc54deSWang Nan 	evlist->bkw_mmap_state = BKW_MMAP_NOTREADY;
788ab705b5SAlexey Budankov 	evlist->ctl_fd.fd = -1;
798ab705b5SAlexey Budankov 	evlist->ctl_fd.ack = -1;
808ab705b5SAlexey Budankov 	evlist->ctl_fd.pos = -1;
81361c99a6SArnaldo Carvalho de Melo }
82361c99a6SArnaldo Carvalho de Melo 
830f98b11cSJiri Olsa struct evlist *evlist__new(void)
84ef1d1af2SArnaldo Carvalho de Melo {
8563503dbaSJiri Olsa 	struct evlist *evlist = zalloc(sizeof(*evlist));
86ef1d1af2SArnaldo Carvalho de Melo 
87ef1d1af2SArnaldo Carvalho de Melo 	if (evlist != NULL)
8852c86bcaSJiri Olsa 		evlist__init(evlist, NULL, NULL);
89ef1d1af2SArnaldo Carvalho de Melo 
90361c99a6SArnaldo Carvalho de Melo 	return evlist;
91361c99a6SArnaldo Carvalho de Melo }
92361c99a6SArnaldo Carvalho de Melo 
93606e2c29SArnaldo Carvalho de Melo struct evlist *evlist__new_default(void)
94b22d54b0SJiri Olsa {
950f98b11cSJiri Olsa 	struct evlist *evlist = evlist__new();
967b100989SIan Rogers 	bool can_profile_kernel;
977b100989SIan Rogers 	int err;
98b22d54b0SJiri Olsa 
997b100989SIan Rogers 	if (!evlist)
1007b100989SIan Rogers 		return NULL;
1017b100989SIan Rogers 
1027b100989SIan Rogers 	can_profile_kernel = perf_event_paranoid_check(1);
1037b100989SIan Rogers 	err = parse_event(evlist, can_profile_kernel ? "cycles:P" : "cycles:Pu");
1047b100989SIan Rogers 	if (err) {
105c12995a5SJiri Olsa 		evlist__delete(evlist);
106b22d54b0SJiri Olsa 		evlist = NULL;
107b22d54b0SJiri Olsa 	}
108b22d54b0SJiri Olsa 
109b22d54b0SJiri Olsa 	return evlist;
110b22d54b0SJiri Olsa }
111b22d54b0SJiri Olsa 
112606e2c29SArnaldo Carvalho de Melo struct evlist *evlist__new_dummy(void)
1135bae0250SArnaldo Carvalho de Melo {
1140f98b11cSJiri Olsa 	struct evlist *evlist = evlist__new();
1155bae0250SArnaldo Carvalho de Melo 
116e251abeeSArnaldo Carvalho de Melo 	if (evlist && evlist__add_dummy(evlist)) {
117c12995a5SJiri Olsa 		evlist__delete(evlist);
1185bae0250SArnaldo Carvalho de Melo 		evlist = NULL;
1195bae0250SArnaldo Carvalho de Melo 	}
1205bae0250SArnaldo Carvalho de Melo 
1215bae0250SArnaldo Carvalho de Melo 	return evlist;
1225bae0250SArnaldo Carvalho de Melo }
1235bae0250SArnaldo Carvalho de Melo 
12475562573SAdrian Hunter /**
1250a7e7ec9SArnaldo Carvalho de Melo  * evlist__set_id_pos - set the positions of event ids.
12675562573SAdrian Hunter  * @evlist: selected event list
12775562573SAdrian Hunter  *
12875562573SAdrian Hunter  * Events with compatible sample types all have the same id_pos
12975562573SAdrian Hunter  * and is_pos.  For convenience, put a copy on evlist.
13075562573SAdrian Hunter  */
1310a7e7ec9SArnaldo Carvalho de Melo void evlist__set_id_pos(struct evlist *evlist)
13275562573SAdrian Hunter {
133515dbe48SJiri Olsa 	struct evsel *first = evlist__first(evlist);
13475562573SAdrian Hunter 
13575562573SAdrian Hunter 	evlist->id_pos = first->id_pos;
13675562573SAdrian Hunter 	evlist->is_pos = first->is_pos;
13775562573SAdrian Hunter }
13875562573SAdrian Hunter 
1390a7e7ec9SArnaldo Carvalho de Melo static void evlist__update_id_pos(struct evlist *evlist)
140733cd2feSAdrian Hunter {
14132dcd021SJiri Olsa 	struct evsel *evsel;
142733cd2feSAdrian Hunter 
143e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, evsel)
1444b5e87b7SArnaldo Carvalho de Melo 		evsel__calc_id_pos(evsel);
145733cd2feSAdrian Hunter 
1460a7e7ec9SArnaldo Carvalho de Melo 	evlist__set_id_pos(evlist);
147733cd2feSAdrian Hunter }
148733cd2feSAdrian Hunter 
149e6b1878dSJiri Olsa static void evlist__purge(struct evlist *evlist)
150361c99a6SArnaldo Carvalho de Melo {
15132dcd021SJiri Olsa 	struct evsel *pos, *n;
152361c99a6SArnaldo Carvalho de Melo 
153e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry_safe(evlist, n, pos) {
154b27c4eceSJiri Olsa 		list_del_init(&pos->core.node);
155d49e4695SArnaldo Carvalho de Melo 		pos->evlist = NULL;
1565eb2dd2aSJiri Olsa 		evsel__delete(pos);
157361c99a6SArnaldo Carvalho de Melo 	}
158361c99a6SArnaldo Carvalho de Melo 
1596484d2f9SJiri Olsa 	evlist->core.nr_entries = 0;
160361c99a6SArnaldo Carvalho de Melo }
161361c99a6SArnaldo Carvalho de Melo 
162470579b0SJiri Olsa void evlist__exit(struct evlist *evlist)
163ef1d1af2SArnaldo Carvalho de Melo {
1646657a099SAdrian Hunter 	event_enable_timer__exit(&evlist->eet);
16504662523SArnaldo Carvalho de Melo 	zfree(&evlist->mmap);
1660b72d69aSWang Nan 	zfree(&evlist->overwrite_mmap);
16793dd6e28SJiri Olsa 	perf_evlist__exit(&evlist->core);
168ef1d1af2SArnaldo Carvalho de Melo }
169ef1d1af2SArnaldo Carvalho de Melo 
170c12995a5SJiri Olsa void evlist__delete(struct evlist *evlist)
171361c99a6SArnaldo Carvalho de Melo {
1720b04b3dcSArnaldo Carvalho de Melo 	if (evlist == NULL)
1730b04b3dcSArnaldo Carvalho de Melo 		return;
1740b04b3dcSArnaldo Carvalho de Melo 
175ac873ac3SIan Rogers 	evlist__free_stats(evlist);
176db6b7b13SJiri Olsa 	evlist__munmap(evlist);
177750b4edeSJiri Olsa 	evlist__close(evlist);
178e6b1878dSJiri Olsa 	evlist__purge(evlist);
179470579b0SJiri Olsa 	evlist__exit(evlist);
180361c99a6SArnaldo Carvalho de Melo 	free(evlist);
181361c99a6SArnaldo Carvalho de Melo }
182361c99a6SArnaldo Carvalho de Melo 
183a1cf3a75SJiri Olsa void evlist__add(struct evlist *evlist, struct evsel *entry)
184361c99a6SArnaldo Carvalho de Melo {
1856484d2f9SJiri Olsa 	perf_evlist__add(&evlist->core, &entry->core);
18638fe0e01SJiri Olsa 	entry->evlist = evlist;
18738fe0e01SJiri Olsa 	entry->tracking = !entry->core.idx;
1886484d2f9SJiri Olsa 
1896484d2f9SJiri Olsa 	if (evlist->core.nr_entries == 1)
1900a7e7ec9SArnaldo Carvalho de Melo 		evlist__set_id_pos(evlist);
191361c99a6SArnaldo Carvalho de Melo }
192361c99a6SArnaldo Carvalho de Melo 
19316251027SJiri Olsa void evlist__remove(struct evlist *evlist, struct evsel *evsel)
1944768230aSAdrian Hunter {
1954768230aSAdrian Hunter 	evsel->evlist = NULL;
19652e22fb8SJiri Olsa 	perf_evlist__remove(&evlist->core, &evsel->core);
1974768230aSAdrian Hunter }
1984768230aSAdrian Hunter 
199e414fd1aSArnaldo Carvalho de Melo void evlist__splice_list_tail(struct evlist *evlist, struct list_head *list)
20050d08e47SArnaldo Carvalho de Melo {
2016d2783feSJohn Garry 	while (!list_empty(list)) {
2026d2783feSJohn Garry 		struct evsel *evsel, *temp, *leader = NULL;
20375562573SAdrian Hunter 
204e5cadb93SArnaldo Carvalho de Melo 		__evlist__for_each_entry_safe(list, temp, evsel) {
205b27c4eceSJiri Olsa 			list_del_init(&evsel->core.node);
206a1cf3a75SJiri Olsa 			evlist__add(evlist, evsel);
2076d2783feSJohn Garry 			leader = evsel;
2086d2783feSJohn Garry 			break;
2096d2783feSJohn Garry 		}
2106d2783feSJohn Garry 
2116d2783feSJohn Garry 		__evlist__for_each_entry_safe(list, temp, evsel) {
212fba7c866SJiri Olsa 			if (evsel__has_leader(evsel, leader)) {
2136d2783feSJohn Garry 				list_del_init(&evsel->core.node);
2146d2783feSJohn Garry 				evlist__add(evlist, evsel);
2156d2783feSJohn Garry 			}
2166d2783feSJohn Garry 		}
217f114d6efSAdrian Hunter 	}
21850d08e47SArnaldo Carvalho de Melo }
21950d08e47SArnaldo Carvalho de Melo 
220c0e53476SArnaldo Carvalho de Melo int __evlist__set_tracepoints_handlers(struct evlist *evlist,
221c0e53476SArnaldo Carvalho de Melo 				       const struct evsel_str_handler *assocs, size_t nr_assocs)
222c0e53476SArnaldo Carvalho de Melo {
223c0e53476SArnaldo Carvalho de Melo 	size_t i;
224c0e53476SArnaldo Carvalho de Melo 	int err;
225c0e53476SArnaldo Carvalho de Melo 
226c0e53476SArnaldo Carvalho de Melo 	for (i = 0; i < nr_assocs; i++) {
227c0e53476SArnaldo Carvalho de Melo 		// Adding a handler for an event not in this evlist, just ignore it.
228b02736f7SArnaldo Carvalho de Melo 		struct evsel *evsel = evlist__find_tracepoint_by_name(evlist, assocs[i].name);
229c0e53476SArnaldo Carvalho de Melo 		if (evsel == NULL)
230c0e53476SArnaldo Carvalho de Melo 			continue;
231c0e53476SArnaldo Carvalho de Melo 
232c0e53476SArnaldo Carvalho de Melo 		err = -EEXIST;
233c0e53476SArnaldo Carvalho de Melo 		if (evsel->handler != NULL)
234c0e53476SArnaldo Carvalho de Melo 			goto out;
235c0e53476SArnaldo Carvalho de Melo 		evsel->handler = assocs[i].handler;
236c0e53476SArnaldo Carvalho de Melo 	}
237c0e53476SArnaldo Carvalho de Melo 
238c0e53476SArnaldo Carvalho de Melo 	err = 0;
239c0e53476SArnaldo Carvalho de Melo out:
240c0e53476SArnaldo Carvalho de Melo 	return err;
241c0e53476SArnaldo Carvalho de Melo }
242c0e53476SArnaldo Carvalho de Melo 
2435f8f9567SIan Rogers static void evlist__set_leader(struct evlist *evlist)
2446a4bb04cSJiri Olsa {
2452e6263abSJiri Olsa 	perf_evlist__set_leader(&evlist->core);
24697f63e4aSNamhyung Kim }
2476a4bb04cSJiri Olsa 
24882944899SAdrian Hunter static struct evsel *evlist__dummy_event(struct evlist *evlist)
2495bae0250SArnaldo Carvalho de Melo {
2505bae0250SArnaldo Carvalho de Melo 	struct perf_event_attr attr = {
2515bae0250SArnaldo Carvalho de Melo 		.type	= PERF_TYPE_SOFTWARE,
2525bae0250SArnaldo Carvalho de Melo 		.config = PERF_COUNT_SW_DUMMY,
2535bae0250SArnaldo Carvalho de Melo 		.size	= sizeof(attr), /* to capture ABI version */
254*67a21d9bSIan Rogers 		/* Avoid frequency mode for dummy events to avoid associated timers. */
255*67a21d9bSIan Rogers 		.freq = 0,
256*67a21d9bSIan Rogers 		.sample_period = 1,
2575bae0250SArnaldo Carvalho de Melo 	};
25882944899SAdrian Hunter 
25982944899SAdrian Hunter 	return evsel__new_idx(&attr, evlist->core.nr_entries);
26082944899SAdrian Hunter }
26182944899SAdrian Hunter 
26282944899SAdrian Hunter int evlist__add_dummy(struct evlist *evlist)
26382944899SAdrian Hunter {
26482944899SAdrian Hunter 	struct evsel *evsel = evlist__dummy_event(evlist);
2655bae0250SArnaldo Carvalho de Melo 
2665bae0250SArnaldo Carvalho de Melo 	if (evsel == NULL)
2675bae0250SArnaldo Carvalho de Melo 		return -ENOMEM;
2685bae0250SArnaldo Carvalho de Melo 
269a1cf3a75SJiri Olsa 	evlist__add(evlist, evsel);
2705bae0250SArnaldo Carvalho de Melo 	return 0;
2715bae0250SArnaldo Carvalho de Melo }
2725bae0250SArnaldo Carvalho de Melo 
273126d68fdSAdrian Hunter struct evsel *evlist__add_aux_dummy(struct evlist *evlist, bool system_wide)
274126d68fdSAdrian Hunter {
275126d68fdSAdrian Hunter 	struct evsel *evsel = evlist__dummy_event(evlist);
276126d68fdSAdrian Hunter 
277126d68fdSAdrian Hunter 	if (!evsel)
278126d68fdSAdrian Hunter 		return NULL;
279126d68fdSAdrian Hunter 
280126d68fdSAdrian Hunter 	evsel->core.attr.exclude_kernel = 1;
281126d68fdSAdrian Hunter 	evsel->core.attr.exclude_guest = 1;
282126d68fdSAdrian Hunter 	evsel->core.attr.exclude_hv = 1;
28360ea006fSNamhyung Kim 	evsel->core.system_wide = system_wide;
284126d68fdSAdrian Hunter 	evsel->no_aux_samples = true;
285126d68fdSAdrian Hunter 	evsel->name = strdup("dummy:u");
286126d68fdSAdrian Hunter 
287126d68fdSAdrian Hunter 	evlist__add(evlist, evsel);
288126d68fdSAdrian Hunter 	return evsel;
289126d68fdSAdrian Hunter }
290126d68fdSAdrian Hunter 
291378ef0f5SIan Rogers #ifdef HAVE_LIBTRACEEVENT
292182bb594SNamhyung Kim struct evsel *evlist__add_sched_switch(struct evlist *evlist, bool system_wide)
293182bb594SNamhyung Kim {
294182bb594SNamhyung Kim 	struct evsel *evsel = evsel__newtp_idx("sched", "sched_switch", 0);
295182bb594SNamhyung Kim 
296182bb594SNamhyung Kim 	if (IS_ERR(evsel))
297182bb594SNamhyung Kim 		return evsel;
298182bb594SNamhyung Kim 
299182bb594SNamhyung Kim 	evsel__set_sample_bit(evsel, CPU);
300182bb594SNamhyung Kim 	evsel__set_sample_bit(evsel, TIME);
301182bb594SNamhyung Kim 
302182bb594SNamhyung Kim 	evsel->core.system_wide = system_wide;
303182bb594SNamhyung Kim 	evsel->no_aux_samples = true;
304182bb594SNamhyung Kim 
305182bb594SNamhyung Kim 	evlist__add(evlist, evsel);
306182bb594SNamhyung Kim 	return evsel;
307378ef0f5SIan Rogers }
308378ef0f5SIan Rogers #endif
309182bb594SNamhyung Kim 
310cdb204adSKan Liang int evlist__add_attrs(struct evlist *evlist, struct perf_event_attr *attrs, size_t nr_attrs)
31150d08e47SArnaldo Carvalho de Melo {
31232dcd021SJiri Olsa 	struct evsel *evsel, *n;
31350d08e47SArnaldo Carvalho de Melo 	LIST_HEAD(head);
31450d08e47SArnaldo Carvalho de Melo 	size_t i;
31550d08e47SArnaldo Carvalho de Melo 
31650d08e47SArnaldo Carvalho de Melo 	for (i = 0; i < nr_attrs; i++) {
3178f6725a2SArnaldo Carvalho de Melo 		evsel = evsel__new_idx(attrs + i, evlist->core.nr_entries + i);
31850d08e47SArnaldo Carvalho de Melo 		if (evsel == NULL)
31950d08e47SArnaldo Carvalho de Melo 			goto out_delete_partial_list;
320b27c4eceSJiri Olsa 		list_add_tail(&evsel->core.node, &head);
32150d08e47SArnaldo Carvalho de Melo 	}
32250d08e47SArnaldo Carvalho de Melo 
323e414fd1aSArnaldo Carvalho de Melo 	evlist__splice_list_tail(evlist, &head);
32450d08e47SArnaldo Carvalho de Melo 
32550d08e47SArnaldo Carvalho de Melo 	return 0;
32650d08e47SArnaldo Carvalho de Melo 
32750d08e47SArnaldo Carvalho de Melo out_delete_partial_list:
328e5cadb93SArnaldo Carvalho de Melo 	__evlist__for_each_entry_safe(&head, n, evsel)
3295eb2dd2aSJiri Olsa 		evsel__delete(evsel);
33050d08e47SArnaldo Carvalho de Melo 	return -1;
33150d08e47SArnaldo Carvalho de Melo }
33250d08e47SArnaldo Carvalho de Melo 
333e251abeeSArnaldo Carvalho de Melo int __evlist__add_default_attrs(struct evlist *evlist, struct perf_event_attr *attrs, size_t nr_attrs)
33479695e1bSArnaldo Carvalho de Melo {
33579695e1bSArnaldo Carvalho de Melo 	size_t i;
33679695e1bSArnaldo Carvalho de Melo 
33779695e1bSArnaldo Carvalho de Melo 	for (i = 0; i < nr_attrs; i++)
33879695e1bSArnaldo Carvalho de Melo 		event_attr_init(attrs + i);
33979695e1bSArnaldo Carvalho de Melo 
340a1cf3a75SJiri Olsa 	return evlist__add_attrs(evlist, attrs, nr_attrs);
34179695e1bSArnaldo Carvalho de Melo }
34279695e1bSArnaldo Carvalho de Melo 
343a9c1ecdaSKan Liang __weak int arch_evlist__add_default_attrs(struct evlist *evlist,
344a9c1ecdaSKan Liang 					  struct perf_event_attr *attrs,
345a9c1ecdaSKan Liang 					  size_t nr_attrs)
34642641d6fSKan Liang {
347a9c1ecdaSKan Liang 	if (!nr_attrs)
34842641d6fSKan Liang 		return 0;
349a9c1ecdaSKan Liang 
350a9c1ecdaSKan Liang 	return __evlist__add_default_attrs(evlist, attrs, nr_attrs);
35142641d6fSKan Liang }
35242641d6fSKan Liang 
353b02736f7SArnaldo Carvalho de Melo struct evsel *evlist__find_tracepoint_by_id(struct evlist *evlist, int id)
354ee29be62SArnaldo Carvalho de Melo {
35532dcd021SJiri Olsa 	struct evsel *evsel;
356ee29be62SArnaldo Carvalho de Melo 
357e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, evsel) {
3581fc632ceSJiri Olsa 		if (evsel->core.attr.type   == PERF_TYPE_TRACEPOINT &&
3591fc632ceSJiri Olsa 		    (int)evsel->core.attr.config == id)
360ee29be62SArnaldo Carvalho de Melo 			return evsel;
361ee29be62SArnaldo Carvalho de Melo 	}
362ee29be62SArnaldo Carvalho de Melo 
363ee29be62SArnaldo Carvalho de Melo 	return NULL;
364ee29be62SArnaldo Carvalho de Melo }
365ee29be62SArnaldo Carvalho de Melo 
366b02736f7SArnaldo Carvalho de Melo struct evsel *evlist__find_tracepoint_by_name(struct evlist *evlist, const char *name)
367a2f2804aSDavid Ahern {
36832dcd021SJiri Olsa 	struct evsel *evsel;
369a2f2804aSDavid Ahern 
370e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, evsel) {
3711fc632ceSJiri Olsa 		if ((evsel->core.attr.type == PERF_TYPE_TRACEPOINT) &&
372a2f2804aSDavid Ahern 		    (strcmp(evsel->name, name) == 0))
373a2f2804aSDavid Ahern 			return evsel;
374a2f2804aSDavid Ahern 	}
375a2f2804aSDavid Ahern 
376a2f2804aSDavid Ahern 	return NULL;
377a2f2804aSDavid Ahern }
378a2f2804aSDavid Ahern 
379378ef0f5SIan Rogers #ifdef HAVE_LIBTRACEEVENT
380e251abeeSArnaldo Carvalho de Melo int evlist__add_newtp(struct evlist *evlist, const char *sys, const char *name, void *handler)
38139876e7dSArnaldo Carvalho de Melo {
3828f6725a2SArnaldo Carvalho de Melo 	struct evsel *evsel = evsel__newtp(sys, name);
38339876e7dSArnaldo Carvalho de Melo 
3848dd2a131SJiri Olsa 	if (IS_ERR(evsel))
38539876e7dSArnaldo Carvalho de Melo 		return -1;
38639876e7dSArnaldo Carvalho de Melo 
387744a9719SArnaldo Carvalho de Melo 	evsel->handler = handler;
388a1cf3a75SJiri Olsa 	evlist__add(evlist, evsel);
38939876e7dSArnaldo Carvalho de Melo 	return 0;
39039876e7dSArnaldo Carvalho de Melo }
391378ef0f5SIan Rogers #endif
39239876e7dSArnaldo Carvalho de Melo 
393472832d2SIan Rogers struct evlist_cpu_iterator evlist__cpu_begin(struct evlist *evlist, struct affinity *affinity)
394a8cbe40fSAndi Kleen {
395472832d2SIan Rogers 	struct evlist_cpu_iterator itr = {
396472832d2SIan Rogers 		.container = evlist,
3978b464eacSIan Rogers 		.evsel = NULL,
398472832d2SIan Rogers 		.cpu_map_idx = 0,
399472832d2SIan Rogers 		.evlist_cpu_map_idx = 0,
400472832d2SIan Rogers 		.evlist_cpu_map_nr = perf_cpu_map__nr(evlist->core.all_cpus),
4016d18804bSIan Rogers 		.cpu = (struct perf_cpu){ .cpu = -1},
402472832d2SIan Rogers 		.affinity = affinity,
403472832d2SIan Rogers 	};
404a8cbe40fSAndi Kleen 
4058b464eacSIan Rogers 	if (evlist__empty(evlist)) {
4068b464eacSIan Rogers 		/* Ensure the empty list doesn't iterate. */
4078b464eacSIan Rogers 		itr.evlist_cpu_map_idx = itr.evlist_cpu_map_nr;
4088b464eacSIan Rogers 	} else {
4098b464eacSIan Rogers 		itr.evsel = evlist__first(evlist);
410472832d2SIan Rogers 		if (itr.affinity) {
411472832d2SIan Rogers 			itr.cpu = perf_cpu_map__cpu(evlist->core.all_cpus, 0);
4126d18804bSIan Rogers 			affinity__set(itr.affinity, itr.cpu.cpu);
413472832d2SIan Rogers 			itr.cpu_map_idx = perf_cpu_map__idx(itr.evsel->core.cpus, itr.cpu);
414a8cbe40fSAndi Kleen 			/*
4158b464eacSIan Rogers 			 * If this CPU isn't in the evsel's cpu map then advance
4168b464eacSIan Rogers 			 * through the list.
417a8cbe40fSAndi Kleen 			 */
418472832d2SIan Rogers 			if (itr.cpu_map_idx == -1)
419472832d2SIan Rogers 				evlist_cpu_iterator__next(&itr);
420472832d2SIan Rogers 		}
4218b464eacSIan Rogers 	}
422472832d2SIan Rogers 	return itr;
423a8cbe40fSAndi Kleen }
424a8cbe40fSAndi Kleen 
425472832d2SIan Rogers void evlist_cpu_iterator__next(struct evlist_cpu_iterator *evlist_cpu_itr)
426a8cbe40fSAndi Kleen {
427472832d2SIan Rogers 	while (evlist_cpu_itr->evsel != evlist__last(evlist_cpu_itr->container)) {
428472832d2SIan Rogers 		evlist_cpu_itr->evsel = evsel__next(evlist_cpu_itr->evsel);
429472832d2SIan Rogers 		evlist_cpu_itr->cpu_map_idx =
430472832d2SIan Rogers 			perf_cpu_map__idx(evlist_cpu_itr->evsel->core.cpus,
431472832d2SIan Rogers 					  evlist_cpu_itr->cpu);
432472832d2SIan Rogers 		if (evlist_cpu_itr->cpu_map_idx != -1)
433472832d2SIan Rogers 			return;
434472832d2SIan Rogers 	}
435472832d2SIan Rogers 	evlist_cpu_itr->evlist_cpu_map_idx++;
436472832d2SIan Rogers 	if (evlist_cpu_itr->evlist_cpu_map_idx < evlist_cpu_itr->evlist_cpu_map_nr) {
437472832d2SIan Rogers 		evlist_cpu_itr->evsel = evlist__first(evlist_cpu_itr->container);
438472832d2SIan Rogers 		evlist_cpu_itr->cpu =
439472832d2SIan Rogers 			perf_cpu_map__cpu(evlist_cpu_itr->container->core.all_cpus,
440472832d2SIan Rogers 					  evlist_cpu_itr->evlist_cpu_map_idx);
441472832d2SIan Rogers 		if (evlist_cpu_itr->affinity)
4426d18804bSIan Rogers 			affinity__set(evlist_cpu_itr->affinity, evlist_cpu_itr->cpu.cpu);
443472832d2SIan Rogers 		evlist_cpu_itr->cpu_map_idx =
444472832d2SIan Rogers 			perf_cpu_map__idx(evlist_cpu_itr->evsel->core.cpus,
445472832d2SIan Rogers 					  evlist_cpu_itr->cpu);
446472832d2SIan Rogers 		/*
447472832d2SIan Rogers 		 * If this CPU isn't in the evsel's cpu map then advance through
448472832d2SIan Rogers 		 * the list.
449472832d2SIan Rogers 		 */
450472832d2SIan Rogers 		if (evlist_cpu_itr->cpu_map_idx == -1)
451472832d2SIan Rogers 			evlist_cpu_iterator__next(evlist_cpu_itr);
452472832d2SIan Rogers 	}
453a8cbe40fSAndi Kleen }
454a8cbe40fSAndi Kleen 
455472832d2SIan Rogers bool evlist_cpu_iterator__end(const struct evlist_cpu_iterator *evlist_cpu_itr)
456a8cbe40fSAndi Kleen {
457472832d2SIan Rogers 	return evlist_cpu_itr->evlist_cpu_map_idx >= evlist_cpu_itr->evlist_cpu_map_nr;
458a8cbe40fSAndi Kleen }
459a8cbe40fSAndi Kleen 
4607cfcd1e0SJiri Olsa static int evsel__strcmp(struct evsel *pos, char *evsel_name)
4617cfcd1e0SJiri Olsa {
4627cfcd1e0SJiri Olsa 	if (!evsel_name)
4637cfcd1e0SJiri Olsa 		return 0;
4647cfcd1e0SJiri Olsa 	if (evsel__is_dummy_event(pos))
4657cfcd1e0SJiri Olsa 		return 1;
466ce1d3bc2SArnaldo Carvalho de Melo 	return !evsel__name_is(pos, evsel_name);
4677cfcd1e0SJiri Olsa }
4687cfcd1e0SJiri Olsa 
4697cfcd1e0SJiri Olsa static int evlist__is_enabled(struct evlist *evlist)
4707cfcd1e0SJiri Olsa {
4717cfcd1e0SJiri Olsa 	struct evsel *pos;
4727cfcd1e0SJiri Olsa 
4737cfcd1e0SJiri Olsa 	evlist__for_each_entry(evlist, pos) {
4747cfcd1e0SJiri Olsa 		if (!evsel__is_group_leader(pos) || !pos->core.fd)
4757cfcd1e0SJiri Olsa 			continue;
4767cfcd1e0SJiri Olsa 		/* If at least one event is enabled, evlist is enabled. */
4777cfcd1e0SJiri Olsa 		if (!pos->disabled)
4787cfcd1e0SJiri Olsa 			return true;
4797cfcd1e0SJiri Olsa 	}
4807cfcd1e0SJiri Olsa 	return false;
4817cfcd1e0SJiri Olsa }
4827cfcd1e0SJiri Olsa 
483329725d5SAdrian Hunter static void __evlist__disable(struct evlist *evlist, char *evsel_name, bool excl_dummy)
4844152ab37SArnaldo Carvalho de Melo {
48532dcd021SJiri Olsa 	struct evsel *pos;
486472832d2SIan Rogers 	struct evlist_cpu_iterator evlist_cpu_itr;
4870d3d2376SArnaldo Carvalho de Melo 	struct affinity saved_affinity, *affinity = NULL;
48887cf8360SAdrian Hunter 	bool has_imm = false;
489704e2f5bSAndi Kleen 
4900d3d2376SArnaldo Carvalho de Melo 	// See explanation in evlist__close()
4910df6ade7SIan Rogers 	if (!cpu_map__is_dummy(evlist->core.user_requested_cpus)) {
4920d3d2376SArnaldo Carvalho de Melo 		if (affinity__setup(&saved_affinity) < 0)
493704e2f5bSAndi Kleen 			return;
4940d3d2376SArnaldo Carvalho de Melo 		affinity = &saved_affinity;
4950d3d2376SArnaldo Carvalho de Melo 	}
496704e2f5bSAndi Kleen 
49787cf8360SAdrian Hunter 	/* Disable 'immediate' events last */
498472832d2SIan Rogers 	for (int imm = 0; imm <= 1; imm++) {
4990d3d2376SArnaldo Carvalho de Melo 		evlist__for_each_cpu(evlist_cpu_itr, evlist, affinity) {
500472832d2SIan Rogers 			pos = evlist_cpu_itr.evsel;
5017cfcd1e0SJiri Olsa 			if (evsel__strcmp(pos, evsel_name))
5027cfcd1e0SJiri Olsa 				continue;
503c754c382SArnaldo Carvalho de Melo 			if (pos->disabled || !evsel__is_group_leader(pos) || !pos->core.fd)
5043fe4430dSJiri Olsa 				continue;
505329725d5SAdrian Hunter 			if (excl_dummy && evsel__is_dummy_event(pos))
506329725d5SAdrian Hunter 				continue;
50787cf8360SAdrian Hunter 			if (pos->immediate)
50887cf8360SAdrian Hunter 				has_imm = true;
50987cf8360SAdrian Hunter 			if (pos->immediate != imm)
51087cf8360SAdrian Hunter 				continue;
511472832d2SIan Rogers 			evsel__disable_cpu(pos, evlist_cpu_itr.cpu_map_idx);
512704e2f5bSAndi Kleen 		}
51387cf8360SAdrian Hunter 		if (!has_imm)
51487cf8360SAdrian Hunter 			break;
51587cf8360SAdrian Hunter 	}
51687cf8360SAdrian Hunter 
5170d3d2376SArnaldo Carvalho de Melo 	affinity__cleanup(affinity);
518704e2f5bSAndi Kleen 	evlist__for_each_entry(evlist, pos) {
5197cfcd1e0SJiri Olsa 		if (evsel__strcmp(pos, evsel_name))
5207cfcd1e0SJiri Olsa 			continue;
521c754c382SArnaldo Carvalho de Melo 		if (!evsel__is_group_leader(pos) || !pos->core.fd)
522704e2f5bSAndi Kleen 			continue;
523329725d5SAdrian Hunter 		if (excl_dummy && evsel__is_dummy_event(pos))
524329725d5SAdrian Hunter 			continue;
525704e2f5bSAndi Kleen 		pos->disabled = true;
5264152ab37SArnaldo Carvalho de Melo 	}
5272b56bcfbSArnaldo Carvalho de Melo 
5287cfcd1e0SJiri Olsa 	/*
5297cfcd1e0SJiri Olsa 	 * If we disabled only single event, we need to check
5307cfcd1e0SJiri Olsa 	 * the enabled state of the evlist manually.
5317cfcd1e0SJiri Olsa 	 */
5327cfcd1e0SJiri Olsa 	if (evsel_name)
5337cfcd1e0SJiri Olsa 		evlist->enabled = evlist__is_enabled(evlist);
5347cfcd1e0SJiri Olsa 	else
5352b56bcfbSArnaldo Carvalho de Melo 		evlist->enabled = false;
5364152ab37SArnaldo Carvalho de Melo }
5374152ab37SArnaldo Carvalho de Melo 
5387cfcd1e0SJiri Olsa void evlist__disable(struct evlist *evlist)
5397cfcd1e0SJiri Olsa {
540329725d5SAdrian Hunter 	__evlist__disable(evlist, NULL, false);
541329725d5SAdrian Hunter }
542329725d5SAdrian Hunter 
543329725d5SAdrian Hunter void evlist__disable_non_dummy(struct evlist *evlist)
544329725d5SAdrian Hunter {
545329725d5SAdrian Hunter 	__evlist__disable(evlist, NULL, true);
5467cfcd1e0SJiri Olsa }
5477cfcd1e0SJiri Olsa 
5487cfcd1e0SJiri Olsa void evlist__disable_evsel(struct evlist *evlist, char *evsel_name)
5497cfcd1e0SJiri Olsa {
550329725d5SAdrian Hunter 	__evlist__disable(evlist, evsel_name, false);
5517cfcd1e0SJiri Olsa }
5527cfcd1e0SJiri Olsa 
553329725d5SAdrian Hunter static void __evlist__enable(struct evlist *evlist, char *evsel_name, bool excl_dummy)
554764e16a3SDavid Ahern {
55532dcd021SJiri Olsa 	struct evsel *pos;
556472832d2SIan Rogers 	struct evlist_cpu_iterator evlist_cpu_itr;
557f350ee95SArnaldo Carvalho de Melo 	struct affinity saved_affinity, *affinity = NULL;
558764e16a3SDavid Ahern 
559f350ee95SArnaldo Carvalho de Melo 	// See explanation in evlist__close()
5600df6ade7SIan Rogers 	if (!cpu_map__is_dummy(evlist->core.user_requested_cpus)) {
561f350ee95SArnaldo Carvalho de Melo 		if (affinity__setup(&saved_affinity) < 0)
562704e2f5bSAndi Kleen 			return;
563f350ee95SArnaldo Carvalho de Melo 		affinity = &saved_affinity;
564f350ee95SArnaldo Carvalho de Melo 	}
565704e2f5bSAndi Kleen 
566f350ee95SArnaldo Carvalho de Melo 	evlist__for_each_cpu(evlist_cpu_itr, evlist, affinity) {
567472832d2SIan Rogers 		pos = evlist_cpu_itr.evsel;
5687cfcd1e0SJiri Olsa 		if (evsel__strcmp(pos, evsel_name))
5697cfcd1e0SJiri Olsa 			continue;
570c754c382SArnaldo Carvalho de Melo 		if (!evsel__is_group_leader(pos) || !pos->core.fd)
571704e2f5bSAndi Kleen 			continue;
572329725d5SAdrian Hunter 		if (excl_dummy && evsel__is_dummy_event(pos))
573329725d5SAdrian Hunter 			continue;
574472832d2SIan Rogers 		evsel__enable_cpu(pos, evlist_cpu_itr.cpu_map_idx);
575704e2f5bSAndi Kleen 	}
576f350ee95SArnaldo Carvalho de Melo 	affinity__cleanup(affinity);
577e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, pos) {
5787cfcd1e0SJiri Olsa 		if (evsel__strcmp(pos, evsel_name))
5797cfcd1e0SJiri Olsa 			continue;
580c754c382SArnaldo Carvalho de Melo 		if (!evsel__is_group_leader(pos) || !pos->core.fd)
5813fe4430dSJiri Olsa 			continue;
582329725d5SAdrian Hunter 		if (excl_dummy && evsel__is_dummy_event(pos))
583329725d5SAdrian Hunter 			continue;
584704e2f5bSAndi Kleen 		pos->disabled = false;
585764e16a3SDavid Ahern 	}
5862b56bcfbSArnaldo Carvalho de Melo 
5877cfcd1e0SJiri Olsa 	/*
5887cfcd1e0SJiri Olsa 	 * Even single event sets the 'enabled' for evlist,
5897cfcd1e0SJiri Olsa 	 * so the toggle can work properly and toggle to
5907cfcd1e0SJiri Olsa 	 * 'disabled' state.
5917cfcd1e0SJiri Olsa 	 */
5922b56bcfbSArnaldo Carvalho de Melo 	evlist->enabled = true;
5932b56bcfbSArnaldo Carvalho de Melo }
5942b56bcfbSArnaldo Carvalho de Melo 
5957cfcd1e0SJiri Olsa void evlist__enable(struct evlist *evlist)
5967cfcd1e0SJiri Olsa {
597329725d5SAdrian Hunter 	__evlist__enable(evlist, NULL, false);
598329725d5SAdrian Hunter }
599329725d5SAdrian Hunter 
600329725d5SAdrian Hunter void evlist__enable_non_dummy(struct evlist *evlist)
601329725d5SAdrian Hunter {
602329725d5SAdrian Hunter 	__evlist__enable(evlist, NULL, true);
6037cfcd1e0SJiri Olsa }
6047cfcd1e0SJiri Olsa 
6057cfcd1e0SJiri Olsa void evlist__enable_evsel(struct evlist *evlist, char *evsel_name)
6067cfcd1e0SJiri Olsa {
607329725d5SAdrian Hunter 	__evlist__enable(evlist, evsel_name, false);
6087cfcd1e0SJiri Olsa }
6097cfcd1e0SJiri Olsa 
610ade9d208SArnaldo Carvalho de Melo void evlist__toggle_enable(struct evlist *evlist)
6112b56bcfbSArnaldo Carvalho de Melo {
612e74676deSJiri Olsa 	(evlist->enabled ? evlist__disable : evlist__enable)(evlist);
613764e16a3SDavid Ahern }
614764e16a3SDavid Ahern 
615f4009e7bSJiri Olsa int evlist__add_pollfd(struct evlist *evlist, int fd)
616e4b356b5SArnaldo Carvalho de Melo {
617ab4c1f9fSAlexey Budankov 	return perf_evlist__add_pollfd(&evlist->core, fd, NULL, POLLIN, fdarray_flag__default);
618e4b356b5SArnaldo Carvalho de Melo }
6191b85337dSArnaldo Carvalho de Melo 
620f4009e7bSJiri Olsa int evlist__filter_pollfd(struct evlist *evlist, short revents_and_mask)
6211ddec7f0SArnaldo Carvalho de Melo {
62284227cb1SJiri Olsa 	return perf_evlist__filter_pollfd(&evlist->core, revents_and_mask);
6231ddec7f0SArnaldo Carvalho de Melo }
6241ddec7f0SArnaldo Carvalho de Melo 
625e16c2ce7SYang Jihong #ifdef HAVE_EVENTFD_SUPPORT
626e16c2ce7SYang Jihong int evlist__add_wakeup_eventfd(struct evlist *evlist, int fd)
627e16c2ce7SYang Jihong {
628e16c2ce7SYang Jihong 	return perf_evlist__add_pollfd(&evlist->core, fd, NULL, POLLIN,
629a032ad87SAdrian Hunter 				       fdarray_flag__nonfilterable |
630a032ad87SAdrian Hunter 				       fdarray_flag__non_perf_event);
631e16c2ce7SYang Jihong }
632e16c2ce7SYang Jihong #endif
633e16c2ce7SYang Jihong 
63480ab2987SJiri Olsa int evlist__poll(struct evlist *evlist, int timeout)
635f66a889dSArnaldo Carvalho de Melo {
63680ab2987SJiri Olsa 	return perf_evlist__poll(&evlist->core, timeout);
637f66a889dSArnaldo Carvalho de Melo }
638f66a889dSArnaldo Carvalho de Melo 
6393ccf8a7bSArnaldo Carvalho de Melo struct perf_sample_id *evlist__id2sid(struct evlist *evlist, u64 id)
64070db7533SArnaldo Carvalho de Melo {
64170db7533SArnaldo Carvalho de Melo 	struct hlist_head *head;
64270db7533SArnaldo Carvalho de Melo 	struct perf_sample_id *sid;
64370db7533SArnaldo Carvalho de Melo 	int hash;
64470db7533SArnaldo Carvalho de Melo 
64570db7533SArnaldo Carvalho de Melo 	hash = hash_64(id, PERF_EVLIST__HLIST_BITS);
6461d5af02dSJiri Olsa 	head = &evlist->core.heads[hash];
64770db7533SArnaldo Carvalho de Melo 
648b67bfe0dSSasha Levin 	hlist_for_each_entry(sid, head, node)
64970db7533SArnaldo Carvalho de Melo 		if (sid->id == id)
650932a3594SJiri Olsa 			return sid;
651932a3594SJiri Olsa 
652932a3594SJiri Olsa 	return NULL;
653932a3594SJiri Olsa }
654932a3594SJiri Olsa 
6553ccf8a7bSArnaldo Carvalho de Melo struct evsel *evlist__id2evsel(struct evlist *evlist, u64 id)
656932a3594SJiri Olsa {
657932a3594SJiri Olsa 	struct perf_sample_id *sid;
658932a3594SJiri Olsa 
6596484d2f9SJiri Olsa 	if (evlist->core.nr_entries == 1 || !id)
660515dbe48SJiri Olsa 		return evlist__first(evlist);
661932a3594SJiri Olsa 
6623ccf8a7bSArnaldo Carvalho de Melo 	sid = evlist__id2sid(evlist, id);
663932a3594SJiri Olsa 	if (sid)
66470c20369SJiri Olsa 		return container_of(sid->evsel, struct evsel, core);
66530e68bccSNamhyung Kim 
6668cedf3a5SArnaldo Carvalho de Melo 	if (!evlist__sample_id_all(evlist))
667515dbe48SJiri Olsa 		return evlist__first(evlist);
66830e68bccSNamhyung Kim 
66970db7533SArnaldo Carvalho de Melo 	return NULL;
67070db7533SArnaldo Carvalho de Melo }
67104391debSArnaldo Carvalho de Melo 
6723ccf8a7bSArnaldo Carvalho de Melo struct evsel *evlist__id2evsel_strict(struct evlist *evlist, u64 id)
673dddcf6abSAdrian Hunter {
674dddcf6abSAdrian Hunter 	struct perf_sample_id *sid;
675dddcf6abSAdrian Hunter 
676dddcf6abSAdrian Hunter 	if (!id)
677dddcf6abSAdrian Hunter 		return NULL;
678dddcf6abSAdrian Hunter 
6793ccf8a7bSArnaldo Carvalho de Melo 	sid = evlist__id2sid(evlist, id);
680dddcf6abSAdrian Hunter 	if (sid)
68170c20369SJiri Olsa 		return container_of(sid->evsel, struct evsel, core);
682dddcf6abSAdrian Hunter 
683dddcf6abSAdrian Hunter 	return NULL;
684dddcf6abSAdrian Hunter }
685dddcf6abSAdrian Hunter 
6863ccf8a7bSArnaldo Carvalho de Melo static int evlist__event2id(struct evlist *evlist, union perf_event *event, u64 *id)
68775562573SAdrian Hunter {
688b1fcd190SJiri Olsa 	const __u64 *array = event->sample.array;
68975562573SAdrian Hunter 	ssize_t n;
69075562573SAdrian Hunter 
69175562573SAdrian Hunter 	n = (event->header.size - sizeof(event->header)) >> 3;
69275562573SAdrian Hunter 
69375562573SAdrian Hunter 	if (event->header.type == PERF_RECORD_SAMPLE) {
69475562573SAdrian Hunter 		if (evlist->id_pos >= n)
69575562573SAdrian Hunter 			return -1;
69675562573SAdrian Hunter 		*id = array[evlist->id_pos];
69775562573SAdrian Hunter 	} else {
69875562573SAdrian Hunter 		if (evlist->is_pos > n)
69975562573SAdrian Hunter 			return -1;
70075562573SAdrian Hunter 		n -= evlist->is_pos;
70175562573SAdrian Hunter 		*id = array[n];
70275562573SAdrian Hunter 	}
70375562573SAdrian Hunter 	return 0;
70475562573SAdrian Hunter }
70575562573SAdrian Hunter 
7063ccf8a7bSArnaldo Carvalho de Melo struct evsel *evlist__event2evsel(struct evlist *evlist, union perf_event *event)
70775562573SAdrian Hunter {
708515dbe48SJiri Olsa 	struct evsel *first = evlist__first(evlist);
70975562573SAdrian Hunter 	struct hlist_head *head;
71075562573SAdrian Hunter 	struct perf_sample_id *sid;
71175562573SAdrian Hunter 	int hash;
71275562573SAdrian Hunter 	u64 id;
71375562573SAdrian Hunter 
7146484d2f9SJiri Olsa 	if (evlist->core.nr_entries == 1)
71598be6966SAdrian Hunter 		return first;
71698be6966SAdrian Hunter 
7171fc632ceSJiri Olsa 	if (!first->core.attr.sample_id_all &&
71898be6966SAdrian Hunter 	    event->header.type != PERF_RECORD_SAMPLE)
71998be6966SAdrian Hunter 		return first;
72075562573SAdrian Hunter 
7213ccf8a7bSArnaldo Carvalho de Melo 	if (evlist__event2id(evlist, event, &id))
72275562573SAdrian Hunter 		return NULL;
72375562573SAdrian Hunter 
72475562573SAdrian Hunter 	/* Synthesized events have an id of zero */
72575562573SAdrian Hunter 	if (!id)
72698be6966SAdrian Hunter 		return first;
72775562573SAdrian Hunter 
72875562573SAdrian Hunter 	hash = hash_64(id, PERF_EVLIST__HLIST_BITS);
7291d5af02dSJiri Olsa 	head = &evlist->core.heads[hash];
73075562573SAdrian Hunter 
73175562573SAdrian Hunter 	hlist_for_each_entry(sid, head, node) {
73275562573SAdrian Hunter 		if (sid->id == id)
73370c20369SJiri Olsa 			return container_of(sid->evsel, struct evsel, core);
73475562573SAdrian Hunter 	}
73575562573SAdrian Hunter 	return NULL;
73675562573SAdrian Hunter }
73775562573SAdrian Hunter 
7380a60b339SArnaldo Carvalho de Melo static int evlist__set_paused(struct evlist *evlist, bool value)
73965aea233SWang Nan {
74065aea233SWang Nan 	int i;
74165aea233SWang Nan 
7420b72d69aSWang Nan 	if (!evlist->overwrite_mmap)
743078c3386SWang Nan 		return 0;
744078c3386SWang Nan 
745c976ee11SJiri Olsa 	for (i = 0; i < evlist->core.nr_mmaps; i++) {
7462cf07b29SJiri Olsa 		int fd = evlist->overwrite_mmap[i].core.fd;
74765aea233SWang Nan 		int err;
74865aea233SWang Nan 
74965aea233SWang Nan 		if (fd < 0)
75065aea233SWang Nan 			continue;
75165aea233SWang Nan 		err = ioctl(fd, PERF_EVENT_IOC_PAUSE_OUTPUT, value ? 1 : 0);
75265aea233SWang Nan 		if (err)
75365aea233SWang Nan 			return err;
75465aea233SWang Nan 	}
75565aea233SWang Nan 	return 0;
75665aea233SWang Nan }
75765aea233SWang Nan 
7580a60b339SArnaldo Carvalho de Melo static int evlist__pause(struct evlist *evlist)
75965aea233SWang Nan {
7600a60b339SArnaldo Carvalho de Melo 	return evlist__set_paused(evlist, true);
76165aea233SWang Nan }
76265aea233SWang Nan 
7630a60b339SArnaldo Carvalho de Melo static int evlist__resume(struct evlist *evlist)
76465aea233SWang Nan {
7650a60b339SArnaldo Carvalho de Melo 	return evlist__set_paused(evlist, false);
76665aea233SWang Nan }
76765aea233SWang Nan 
768db6b7b13SJiri Olsa static void evlist__munmap_nofree(struct evlist *evlist)
769f8a95309SArnaldo Carvalho de Melo {
770aece948fSArnaldo Carvalho de Melo 	int i;
771f8a95309SArnaldo Carvalho de Melo 
772b2cb615dSWang Nan 	if (evlist->mmap)
773c976ee11SJiri Olsa 		for (i = 0; i < evlist->core.nr_mmaps; i++)
77480e53d11SJiri Olsa 			perf_mmap__munmap(&evlist->mmap[i].core);
775b2cb615dSWang Nan 
7760b72d69aSWang Nan 	if (evlist->overwrite_mmap)
777c976ee11SJiri Olsa 		for (i = 0; i < evlist->core.nr_mmaps; i++)
77880e53d11SJiri Olsa 			perf_mmap__munmap(&evlist->overwrite_mmap[i].core);
779a1f72618SWang Nan }
780aece948fSArnaldo Carvalho de Melo 
781db6b7b13SJiri Olsa void evlist__munmap(struct evlist *evlist)
782a1f72618SWang Nan {
783db6b7b13SJiri Olsa 	evlist__munmap_nofree(evlist);
78404662523SArnaldo Carvalho de Melo 	zfree(&evlist->mmap);
7850b72d69aSWang Nan 	zfree(&evlist->overwrite_mmap);
786f8a95309SArnaldo Carvalho de Melo }
787f8a95309SArnaldo Carvalho de Melo 
78880e53d11SJiri Olsa static void perf_mmap__unmap_cb(struct perf_mmap *map)
78980e53d11SJiri Olsa {
79080e53d11SJiri Olsa 	struct mmap *m = container_of(map, struct mmap, core);
79180e53d11SJiri Olsa 
79280e53d11SJiri Olsa 	mmap__munmap(m);
79380e53d11SJiri Olsa }
79480e53d11SJiri Olsa 
795d50cf361SJiri Olsa static struct mmap *evlist__alloc_mmap(struct evlist *evlist,
7962c5f6d87SKan Liang 				       bool overwrite)
797f8a95309SArnaldo Carvalho de Melo {
798d4c6fb36SWang Nan 	int i;
799a5830532SJiri Olsa 	struct mmap *map;
800d4c6fb36SWang Nan 
801c976ee11SJiri Olsa 	map = zalloc(evlist->core.nr_mmaps * sizeof(struct mmap));
8028db6d6b1SWang Nan 	if (!map)
8038db6d6b1SWang Nan 		return NULL;
804946ae1d4SWang Nan 
805c976ee11SJiri Olsa 	for (i = 0; i < evlist->core.nr_mmaps; i++) {
8066eb65f7aSJiri Olsa 		struct perf_mmap *prev = i ? &map[i - 1].core : NULL;
8076eb65f7aSJiri Olsa 
8084738ca30SArnaldo Carvalho de Melo 		/*
8094738ca30SArnaldo Carvalho de Melo 		 * When the perf_mmap() call is made we grab one refcount, plus
8106afad54dSKan Liang 		 * one extra to let perf_mmap__consume() get the last
8114738ca30SArnaldo Carvalho de Melo 		 * events after all real references (perf_mmap__get()) are
8124738ca30SArnaldo Carvalho de Melo 		 * dropped.
8134738ca30SArnaldo Carvalho de Melo 		 *
8144738ca30SArnaldo Carvalho de Melo 		 * Each PERF_EVENT_IOC_SET_OUTPUT points to this mmap and
8154738ca30SArnaldo Carvalho de Melo 		 * thus does perf_mmap__get() on it.
8164738ca30SArnaldo Carvalho de Melo 		 */
8176eb65f7aSJiri Olsa 		perf_mmap__init(&map[i].core, prev, overwrite, perf_mmap__unmap_cb);
8184738ca30SArnaldo Carvalho de Melo 	}
819353120b4SJiri Olsa 
8208db6d6b1SWang Nan 	return map;
821f8a95309SArnaldo Carvalho de Melo }
822f8a95309SArnaldo Carvalho de Melo 
8239abd2ab2SJiri Olsa static void
8249abd2ab2SJiri Olsa perf_evlist__mmap_cb_idx(struct perf_evlist *_evlist,
825d01508f2SAdrian Hunter 			 struct perf_evsel *_evsel,
8269abd2ab2SJiri Olsa 			 struct perf_mmap_param *_mp,
8276a7b8a5aSAdrian Hunter 			 int idx)
8289abd2ab2SJiri Olsa {
8299abd2ab2SJiri Olsa 	struct evlist *evlist = container_of(_evlist, struct evlist, core);
8309abd2ab2SJiri Olsa 	struct mmap_params *mp = container_of(_mp, struct mmap_params, core);
831d01508f2SAdrian Hunter 	struct evsel *evsel = container_of(_evsel, struct evsel, core);
8329abd2ab2SJiri Olsa 
83384bd5abaSAdrian Hunter 	auxtrace_mmap_params__set_idx(&mp->auxtrace_mp, evlist, evsel, idx);
8349abd2ab2SJiri Olsa }
8359abd2ab2SJiri Olsa 
836bb1b1885SJiri Olsa static struct perf_mmap*
837bb1b1885SJiri Olsa perf_evlist__mmap_cb_get(struct perf_evlist *_evlist, bool overwrite, int idx)
838bb1b1885SJiri Olsa {
839bb1b1885SJiri Olsa 	struct evlist *evlist = container_of(_evlist, struct evlist, core);
8403805e4f3SJiri Olsa 	struct mmap *maps;
841bb1b1885SJiri Olsa 
8423805e4f3SJiri Olsa 	maps = overwrite ? evlist->overwrite_mmap : evlist->mmap;
843bb1b1885SJiri Olsa 
844bb1b1885SJiri Olsa 	if (!maps) {
8453805e4f3SJiri Olsa 		maps = evlist__alloc_mmap(evlist, overwrite);
846bb1b1885SJiri Olsa 		if (!maps)
847bb1b1885SJiri Olsa 			return NULL;
848bb1b1885SJiri Olsa 
8493805e4f3SJiri Olsa 		if (overwrite) {
850bb1b1885SJiri Olsa 			evlist->overwrite_mmap = maps;
851bb1b1885SJiri Olsa 			if (evlist->bkw_mmap_state == BKW_MMAP_NOTREADY)
852ade9d208SArnaldo Carvalho de Melo 				evlist__toggle_bkw_mmap(evlist, BKW_MMAP_RUNNING);
8533805e4f3SJiri Olsa 		} else {
8543805e4f3SJiri Olsa 			evlist->mmap = maps;
855bb1b1885SJiri Olsa 		}
856bb1b1885SJiri Olsa 	}
857bb1b1885SJiri Olsa 
858bb1b1885SJiri Olsa 	return &maps[idx].core;
859bb1b1885SJiri Olsa }
860bb1b1885SJiri Olsa 
861b80132b1SJiri Olsa static int
862b80132b1SJiri Olsa perf_evlist__mmap_cb_mmap(struct perf_mmap *_map, struct perf_mmap_param *_mp,
8636d18804bSIan Rogers 			  int output, struct perf_cpu cpu)
864b80132b1SJiri Olsa {
865b80132b1SJiri Olsa 	struct mmap *map = container_of(_map, struct mmap, core);
866b80132b1SJiri Olsa 	struct mmap_params *mp = container_of(_mp, struct mmap_params, core);
867b80132b1SJiri Olsa 
868b80132b1SJiri Olsa 	return mmap__mmap(map, mp, output, cpu);
869b80132b1SJiri Olsa }
870b80132b1SJiri Olsa 
871f5e7150cSArnaldo Carvalho de Melo unsigned long perf_event_mlock_kb_in_pages(void)
872994a1f78SJiri Olsa {
873f5e7150cSArnaldo Carvalho de Melo 	unsigned long pages;
8748185e881SArnaldo Carvalho de Melo 	int max;
8758185e881SArnaldo Carvalho de Melo 
8768185e881SArnaldo Carvalho de Melo 	if (sysctl__read_int("kernel/perf_event_mlock_kb", &max) < 0) {
8778185e881SArnaldo Carvalho de Melo 		/*
8788185e881SArnaldo Carvalho de Melo 		 * Pick a once upon a time good value, i.e. things look
8798185e881SArnaldo Carvalho de Melo 		 * strange since we can't read a sysctl value, but lets not
8808185e881SArnaldo Carvalho de Melo 		 * die yet...
8818185e881SArnaldo Carvalho de Melo 		 */
8828185e881SArnaldo Carvalho de Melo 		max = 512;
8838185e881SArnaldo Carvalho de Melo 	} else {
8848185e881SArnaldo Carvalho de Melo 		max -= (page_size / 1024);
8858185e881SArnaldo Carvalho de Melo 	}
8868185e881SArnaldo Carvalho de Melo 
8878185e881SArnaldo Carvalho de Melo 	pages = (max * 1024) / page_size;
8881be300f4SArnaldo Carvalho de Melo 	if (!is_power_of_2(pages))
8891be300f4SArnaldo Carvalho de Melo 		pages = rounddown_pow_of_two(pages);
890f5e7150cSArnaldo Carvalho de Melo 
891f5e7150cSArnaldo Carvalho de Melo 	return pages;
892f5e7150cSArnaldo Carvalho de Melo }
893f5e7150cSArnaldo Carvalho de Melo 
8949521b5f2SJiri Olsa size_t evlist__mmap_size(unsigned long pages)
895f5e7150cSArnaldo Carvalho de Melo {
896f5e7150cSArnaldo Carvalho de Melo 	if (pages == UINT_MAX)
897f5e7150cSArnaldo Carvalho de Melo 		pages = perf_event_mlock_kb_in_pages();
898f5e7150cSArnaldo Carvalho de Melo 	else if (!is_power_of_2(pages))
899994a1f78SJiri Olsa 		return 0;
900994a1f78SJiri Olsa 
901994a1f78SJiri Olsa 	return (pages + 1) * page_size;
902994a1f78SJiri Olsa }
903994a1f78SJiri Olsa 
90433c2dcfdSDavid Ahern static long parse_pages_arg(const char *str, unsigned long min,
90533c2dcfdSDavid Ahern 			    unsigned long max)
906994a1f78SJiri Olsa {
9072fbe4abeSAdrian Hunter 	unsigned long pages, val;
90827050f53SJiri Olsa 	static struct parse_tag tags[] = {
90927050f53SJiri Olsa 		{ .tag  = 'B', .mult = 1       },
91027050f53SJiri Olsa 		{ .tag  = 'K', .mult = 1 << 10 },
91127050f53SJiri Olsa 		{ .tag  = 'M', .mult = 1 << 20 },
91227050f53SJiri Olsa 		{ .tag  = 'G', .mult = 1 << 30 },
91327050f53SJiri Olsa 		{ .tag  = 0 },
91427050f53SJiri Olsa 	};
915994a1f78SJiri Olsa 
9168973504bSDavid Ahern 	if (str == NULL)
91733c2dcfdSDavid Ahern 		return -EINVAL;
9188973504bSDavid Ahern 
91927050f53SJiri Olsa 	val = parse_tag_value(str, tags);
9202fbe4abeSAdrian Hunter 	if (val != (unsigned long) -1) {
92127050f53SJiri Olsa 		/* we got file size value */
92227050f53SJiri Olsa 		pages = PERF_ALIGN(val, page_size) / page_size;
92327050f53SJiri Olsa 	} else {
92427050f53SJiri Olsa 		/* we got pages count value */
92527050f53SJiri Olsa 		char *eptr;
926994a1f78SJiri Olsa 		pages = strtoul(str, &eptr, 10);
92733c2dcfdSDavid Ahern 		if (*eptr != '\0')
92833c2dcfdSDavid Ahern 			return -EINVAL;
92927050f53SJiri Olsa 	}
930994a1f78SJiri Olsa 
9312bcab6c1SAdrian Hunter 	if (pages == 0 && min == 0) {
93233c2dcfdSDavid Ahern 		/* leave number of pages at 0 */
9331dbfa938SAdrian Hunter 	} else if (!is_power_of_2(pages)) {
9349808143bSJiri Olsa 		char buf[100];
9359808143bSJiri Olsa 
93633c2dcfdSDavid Ahern 		/* round pages up to next power of 2 */
93791529834SArnaldo Carvalho de Melo 		pages = roundup_pow_of_two(pages);
9381dbfa938SAdrian Hunter 		if (!pages)
9391dbfa938SAdrian Hunter 			return -EINVAL;
9409808143bSJiri Olsa 
9419808143bSJiri Olsa 		unit_number__scnprintf(buf, sizeof(buf), pages * page_size);
9429808143bSJiri Olsa 		pr_info("rounding mmap pages size to %s (%lu pages)\n",
9439808143bSJiri Olsa 			buf, pages);
9442fbe4abeSAdrian Hunter 	}
9452fbe4abeSAdrian Hunter 
94633c2dcfdSDavid Ahern 	if (pages > max)
94733c2dcfdSDavid Ahern 		return -EINVAL;
94833c2dcfdSDavid Ahern 
94933c2dcfdSDavid Ahern 	return pages;
95033c2dcfdSDavid Ahern }
95133c2dcfdSDavid Ahern 
95225f84702SArnaldo Carvalho de Melo int __evlist__parse_mmap_pages(unsigned int *mmap_pages, const char *str)
95333c2dcfdSDavid Ahern {
95433c2dcfdSDavid Ahern 	unsigned long max = UINT_MAX;
95533c2dcfdSDavid Ahern 	long pages;
95633c2dcfdSDavid Ahern 
957f5ae9c42SAdrian Hunter 	if (max > SIZE_MAX / page_size)
95833c2dcfdSDavid Ahern 		max = SIZE_MAX / page_size;
95933c2dcfdSDavid Ahern 
96033c2dcfdSDavid Ahern 	pages = parse_pages_arg(str, 1, max);
96133c2dcfdSDavid Ahern 	if (pages < 0) {
96233c2dcfdSDavid Ahern 		pr_err("Invalid argument for --mmap_pages/-m\n");
963994a1f78SJiri Olsa 		return -1;
964994a1f78SJiri Olsa 	}
965994a1f78SJiri Olsa 
966994a1f78SJiri Olsa 	*mmap_pages = pages;
967994a1f78SJiri Olsa 	return 0;
968994a1f78SJiri Olsa }
969994a1f78SJiri Olsa 
97025f84702SArnaldo Carvalho de Melo int evlist__parse_mmap_pages(const struct option *opt, const char *str, int unset __maybe_unused)
971e9db1310SAdrian Hunter {
97225f84702SArnaldo Carvalho de Melo 	return __evlist__parse_mmap_pages(opt->value, str);
973e9db1310SAdrian Hunter }
974e9db1310SAdrian Hunter 
975c83fa7f2SAdrian Hunter /**
9769521b5f2SJiri Olsa  * evlist__mmap_ex - Create mmaps to receive events.
977c83fa7f2SAdrian Hunter  * @evlist: list of events
978c83fa7f2SAdrian Hunter  * @pages: map length in pages
979c83fa7f2SAdrian Hunter  * @overwrite: overwrite older events?
980718c602dSAdrian Hunter  * @auxtrace_pages - auxtrace map length in pages
981718c602dSAdrian Hunter  * @auxtrace_overwrite - overwrite older auxtrace data?
982f8a95309SArnaldo Carvalho de Melo  *
983c83fa7f2SAdrian Hunter  * If @overwrite is %false the user needs to signal event consumption using
9849521b5f2SJiri Olsa  * perf_mmap__write_tail().  Using evlist__mmap_read() does this
985c83fa7f2SAdrian Hunter  * automatically.
986f8a95309SArnaldo Carvalho de Melo  *
987718c602dSAdrian Hunter  * Similarly, if @auxtrace_overwrite is %false the user needs to signal data
988718c602dSAdrian Hunter  * consumption using auxtrace_mmap__write_tail().
989718c602dSAdrian Hunter  *
990c83fa7f2SAdrian Hunter  * Return: %0 on success, negative error code otherwise.
991f8a95309SArnaldo Carvalho de Melo  */
9929521b5f2SJiri Olsa int evlist__mmap_ex(struct evlist *evlist, unsigned int pages,
9937a276ff6SWang Nan 			 unsigned int auxtrace_pages,
99451255a8aSAlexey Budankov 			 bool auxtrace_overwrite, int nr_cblocks, int affinity, int flush,
99551255a8aSAlexey Budankov 			 int comp_level)
996f8a95309SArnaldo Carvalho de Melo {
99771f566a3SWang Nan 	/*
99871f566a3SWang Nan 	 * Delay setting mp.prot: set it before calling perf_mmap__mmap.
99971f566a3SWang Nan 	 * Its value is decided by evsel's write_backward.
100071f566a3SWang Nan 	 * So &mp should not be passed through const pointer.
100171f566a3SWang Nan 	 */
1002e440979fSJiri Olsa 	struct mmap_params mp = {
1003e440979fSJiri Olsa 		.nr_cblocks	= nr_cblocks,
1004e440979fSJiri Olsa 		.affinity	= affinity,
1005e440979fSJiri Olsa 		.flush		= flush,
1006e440979fSJiri Olsa 		.comp_level	= comp_level
1007e440979fSJiri Olsa 	};
1008923d0f18SJiri Olsa 	struct perf_evlist_mmap_ops ops = {
10099abd2ab2SJiri Olsa 		.idx  = perf_evlist__mmap_cb_idx,
1010bb1b1885SJiri Olsa 		.get  = perf_evlist__mmap_cb_get,
1011b80132b1SJiri Olsa 		.mmap = perf_evlist__mmap_cb_mmap,
10129abd2ab2SJiri Olsa 	};
101350a682ceSArnaldo Carvalho de Melo 
1014f6fa4375SJiri Olsa 	evlist->core.mmap_len = evlist__mmap_size(pages);
1015f6fa4375SJiri Olsa 	pr_debug("mmap size %zuB\n", evlist->core.mmap_len);
1016f8a95309SArnaldo Carvalho de Melo 
1017f6fa4375SJiri Olsa 	auxtrace_mmap_params__init(&mp.auxtrace_mp, evlist->core.mmap_len,
1018718c602dSAdrian Hunter 				   auxtrace_pages, auxtrace_overwrite);
1019718c602dSAdrian Hunter 
1020923d0f18SJiri Olsa 	return perf_evlist__mmap_ops(&evlist->core, &ops, &mp.core);
1021f8a95309SArnaldo Carvalho de Melo }
10227e2ed097SArnaldo Carvalho de Melo 
10239521b5f2SJiri Olsa int evlist__mmap(struct evlist *evlist, unsigned int pages)
1024718c602dSAdrian Hunter {
10259521b5f2SJiri Olsa 	return evlist__mmap_ex(evlist, pages, 0, false, 0, PERF_AFFINITY_SYS, 1, 0);
1026718c602dSAdrian Hunter }
1027718c602dSAdrian Hunter 
10287748bb71SArnaldo Carvalho de Melo int evlist__create_maps(struct evlist *evlist, struct target *target)
10297e2ed097SArnaldo Carvalho de Melo {
1030147c508fSJin Yao 	bool all_threads = (target->per_thread && target->system_wide);
1031f854839bSJiri Olsa 	struct perf_cpu_map *cpus;
10329749b90eSJiri Olsa 	struct perf_thread_map *threads;
10337e2ed097SArnaldo Carvalho de Melo 
1034147c508fSJin Yao 	/*
1035147c508fSJin Yao 	 * If specify '-a' and '--per-thread' to perf record, perf record
1036147c508fSJin Yao 	 * will override '--per-thread'. target->per_thread = false and
1037147c508fSJin Yao 	 * target->system_wide = true.
1038147c508fSJin Yao 	 *
1039147c508fSJin Yao 	 * If specify '--per-thread' only to perf record,
1040147c508fSJin Yao 	 * target->per_thread = true and target->system_wide = false.
1041147c508fSJin Yao 	 *
1042147c508fSJin Yao 	 * So target->per_thread && target->system_wide is false.
1043147c508fSJin Yao 	 * For perf record, thread_map__new_str doesn't call
1044147c508fSJin Yao 	 * thread_map__new_all_cpus. That will keep perf record's
1045147c508fSJin Yao 	 * current behavior.
1046147c508fSJin Yao 	 *
1047147c508fSJin Yao 	 * For perf stat, it allows the case that target->per_thread and
1048147c508fSJin Yao 	 * target->system_wide are all true. It means to collect system-wide
1049147c508fSJin Yao 	 * per-thread data. thread_map__new_str will call
1050147c508fSJin Yao 	 * thread_map__new_all_cpus to enumerate all threads.
1051147c508fSJin Yao 	 */
105273c0ca1eSJin Yao 	threads = thread_map__new_str(target->pid, target->tid, target->uid,
1053147c508fSJin Yao 				      all_threads);
105474bfd2b2SAdrian Hunter 
105574bfd2b2SAdrian Hunter 	if (!threads)
10567e2ed097SArnaldo Carvalho de Melo 		return -1;
10577e2ed097SArnaldo Carvalho de Melo 
10589c105fbcSDongsheng Yang 	if (target__uses_dummy_map(target))
1059397721e0SJiri Olsa 		cpus = perf_cpu_map__dummy_new();
1060879d77d0SNamhyung Kim 	else
10619c3516d1SJiri Olsa 		cpus = perf_cpu_map__new(target->cpu_list);
10627e2ed097SArnaldo Carvalho de Melo 
106374bfd2b2SAdrian Hunter 	if (!cpus)
10647e2ed097SArnaldo Carvalho de Melo 		goto out_delete_threads;
10657e2ed097SArnaldo Carvalho de Melo 
106642249160SIan Rogers 	evlist->core.has_user_cpus = !!target->cpu_list;
1067ec9a77a7SAdrian Hunter 
1068453fa030SJiri Olsa 	perf_evlist__set_maps(&evlist->core, cpus, threads);
1069d5bc056eSAdrian Hunter 
1070bfd1b83dSNamhyung Kim 	/* as evlist now has references, put count here */
1071bfd1b83dSNamhyung Kim 	perf_cpu_map__put(cpus);
1072bfd1b83dSNamhyung Kim 	perf_thread_map__put(threads);
1073bfd1b83dSNamhyung Kim 
1074d5bc056eSAdrian Hunter 	return 0;
10757e2ed097SArnaldo Carvalho de Melo 
10767e2ed097SArnaldo Carvalho de Melo out_delete_threads:
10777836e52eSJiri Olsa 	perf_thread_map__put(threads);
10787e2ed097SArnaldo Carvalho de Melo 	return -1;
10797e2ed097SArnaldo Carvalho de Melo }
10807e2ed097SArnaldo Carvalho de Melo 
108124bf91a7SArnaldo Carvalho de Melo int evlist__apply_filters(struct evlist *evlist, struct evsel **err_evsel)
10820a102479SFrederic Weisbecker {
108332dcd021SJiri Olsa 	struct evsel *evsel;
1084745cefc5SArnaldo Carvalho de Melo 	int err = 0;
10850a102479SFrederic Weisbecker 
1086e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, evsel) {
1087d988d5eeSKan Liang 		/*
1088d988d5eeSKan Liang 		 * filters only work for tracepoint event, which doesn't have cpu limit.
1089d988d5eeSKan Liang 		 * So evlist and evsel should always be same.
1090d988d5eeSKan Liang 		 */
1091d180aa56SNamhyung Kim 		if (evsel->filter) {
1092a00571fdSJiri Olsa 			err = perf_evsel__apply_filter(&evsel->core, evsel->filter);
109323d4aad4SArnaldo Carvalho de Melo 			if (err) {
109423d4aad4SArnaldo Carvalho de Melo 				*err_evsel = evsel;
1095745cefc5SArnaldo Carvalho de Melo 				break;
10960a102479SFrederic Weisbecker 			}
109723d4aad4SArnaldo Carvalho de Melo 		}
10980a102479SFrederic Weisbecker 
1099d180aa56SNamhyung Kim 		/*
1100d180aa56SNamhyung Kim 		 * non-tracepoint events can have BPF filters.
1101d180aa56SNamhyung Kim 		 */
1102d180aa56SNamhyung Kim 		if (!list_empty(&evsel->bpf_filters)) {
1103d180aa56SNamhyung Kim 			err = perf_bpf_filter__prepare(evsel);
1104d180aa56SNamhyung Kim 			if (err) {
1105d180aa56SNamhyung Kim 				*err_evsel = evsel;
1106d180aa56SNamhyung Kim 				break;
1107d180aa56SNamhyung Kim 			}
1108d180aa56SNamhyung Kim 		}
1109d180aa56SNamhyung Kim 	}
1110d180aa56SNamhyung Kim 
1111745cefc5SArnaldo Carvalho de Melo 	return err;
1112745cefc5SArnaldo Carvalho de Melo }
1113745cefc5SArnaldo Carvalho de Melo 
111424bf91a7SArnaldo Carvalho de Melo int evlist__set_tp_filter(struct evlist *evlist, const char *filter)
1115745cefc5SArnaldo Carvalho de Melo {
111632dcd021SJiri Olsa 	struct evsel *evsel;
1117745cefc5SArnaldo Carvalho de Melo 	int err = 0;
1118745cefc5SArnaldo Carvalho de Melo 
111905cea449SArnaldo Carvalho de Melo 	if (filter == NULL)
112005cea449SArnaldo Carvalho de Melo 		return -1;
112105cea449SArnaldo Carvalho de Melo 
1122e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, evsel) {
11231fc632ceSJiri Olsa 		if (evsel->core.attr.type != PERF_TYPE_TRACEPOINT)
1124fdf14720SWang Nan 			continue;
1125fdf14720SWang Nan 
1126ad681adfSArnaldo Carvalho de Melo 		err = evsel__set_filter(evsel, filter);
1127745cefc5SArnaldo Carvalho de Melo 		if (err)
1128745cefc5SArnaldo Carvalho de Melo 			break;
1129745cefc5SArnaldo Carvalho de Melo 	}
1130745cefc5SArnaldo Carvalho de Melo 
1131745cefc5SArnaldo Carvalho de Melo 	return err;
11320a102479SFrederic Weisbecker }
113374429964SFrederic Weisbecker 
113424bf91a7SArnaldo Carvalho de Melo int evlist__append_tp_filter(struct evlist *evlist, const char *filter)
113553c92f73SArnaldo Carvalho de Melo {
113653c92f73SArnaldo Carvalho de Melo 	struct evsel *evsel;
113753c92f73SArnaldo Carvalho de Melo 	int err = 0;
113853c92f73SArnaldo Carvalho de Melo 
113953c92f73SArnaldo Carvalho de Melo 	if (filter == NULL)
114053c92f73SArnaldo Carvalho de Melo 		return -1;
114153c92f73SArnaldo Carvalho de Melo 
114253c92f73SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, evsel) {
114353c92f73SArnaldo Carvalho de Melo 		if (evsel->core.attr.type != PERF_TYPE_TRACEPOINT)
114453c92f73SArnaldo Carvalho de Melo 			continue;
114553c92f73SArnaldo Carvalho de Melo 
1146ad681adfSArnaldo Carvalho de Melo 		err = evsel__append_tp_filter(evsel, filter);
114753c92f73SArnaldo Carvalho de Melo 		if (err)
114853c92f73SArnaldo Carvalho de Melo 			break;
114953c92f73SArnaldo Carvalho de Melo 	}
115053c92f73SArnaldo Carvalho de Melo 
115153c92f73SArnaldo Carvalho de Melo 	return err;
115253c92f73SArnaldo Carvalho de Melo }
115353c92f73SArnaldo Carvalho de Melo 
1154da949f50SArnaldo Carvalho de Melo char *asprintf__tp_filter_pids(size_t npids, pid_t *pids)
1155cfd70a26SArnaldo Carvalho de Melo {
1156cfd70a26SArnaldo Carvalho de Melo 	char *filter;
1157be199adaSArnaldo Carvalho de Melo 	size_t i;
1158cfd70a26SArnaldo Carvalho de Melo 
1159be199adaSArnaldo Carvalho de Melo 	for (i = 0; i < npids; ++i) {
1160be199adaSArnaldo Carvalho de Melo 		if (i == 0) {
1161be199adaSArnaldo Carvalho de Melo 			if (asprintf(&filter, "common_pid != %d", pids[i]) < 0)
116205cea449SArnaldo Carvalho de Melo 				return NULL;
1163be199adaSArnaldo Carvalho de Melo 		} else {
1164be199adaSArnaldo Carvalho de Melo 			char *tmp;
1165be199adaSArnaldo Carvalho de Melo 
1166be199adaSArnaldo Carvalho de Melo 			if (asprintf(&tmp, "%s && common_pid != %d", filter, pids[i]) < 0)
1167be199adaSArnaldo Carvalho de Melo 				goto out_free;
1168be199adaSArnaldo Carvalho de Melo 
1169be199adaSArnaldo Carvalho de Melo 			free(filter);
1170be199adaSArnaldo Carvalho de Melo 			filter = tmp;
1171be199adaSArnaldo Carvalho de Melo 		}
1172be199adaSArnaldo Carvalho de Melo 	}
1173cfd70a26SArnaldo Carvalho de Melo 
117405cea449SArnaldo Carvalho de Melo 	return filter;
1175be199adaSArnaldo Carvalho de Melo out_free:
1176cfd70a26SArnaldo Carvalho de Melo 	free(filter);
117705cea449SArnaldo Carvalho de Melo 	return NULL;
117805cea449SArnaldo Carvalho de Melo }
117905cea449SArnaldo Carvalho de Melo 
118024bf91a7SArnaldo Carvalho de Melo int evlist__set_tp_filter_pids(struct evlist *evlist, size_t npids, pid_t *pids)
118105cea449SArnaldo Carvalho de Melo {
118205cea449SArnaldo Carvalho de Melo 	char *filter = asprintf__tp_filter_pids(npids, pids);
118324bf91a7SArnaldo Carvalho de Melo 	int ret = evlist__set_tp_filter(evlist, filter);
118405cea449SArnaldo Carvalho de Melo 
118505cea449SArnaldo Carvalho de Melo 	free(filter);
1186cfd70a26SArnaldo Carvalho de Melo 	return ret;
1187cfd70a26SArnaldo Carvalho de Melo }
1188cfd70a26SArnaldo Carvalho de Melo 
118924bf91a7SArnaldo Carvalho de Melo int evlist__set_tp_filter_pid(struct evlist *evlist, pid_t pid)
1190be199adaSArnaldo Carvalho de Melo {
119124bf91a7SArnaldo Carvalho de Melo 	return evlist__set_tp_filter_pids(evlist, 1, &pid);
1192be199adaSArnaldo Carvalho de Melo }
1193be199adaSArnaldo Carvalho de Melo 
119424bf91a7SArnaldo Carvalho de Melo int evlist__append_tp_filter_pids(struct evlist *evlist, size_t npids, pid_t *pids)
11951827ab5bSArnaldo Carvalho de Melo {
11961827ab5bSArnaldo Carvalho de Melo 	char *filter = asprintf__tp_filter_pids(npids, pids);
119724bf91a7SArnaldo Carvalho de Melo 	int ret = evlist__append_tp_filter(evlist, filter);
11981827ab5bSArnaldo Carvalho de Melo 
11991827ab5bSArnaldo Carvalho de Melo 	free(filter);
12001827ab5bSArnaldo Carvalho de Melo 	return ret;
12011827ab5bSArnaldo Carvalho de Melo }
12021827ab5bSArnaldo Carvalho de Melo 
120324bf91a7SArnaldo Carvalho de Melo int evlist__append_tp_filter_pid(struct evlist *evlist, pid_t pid)
12041827ab5bSArnaldo Carvalho de Melo {
120524bf91a7SArnaldo Carvalho de Melo 	return evlist__append_tp_filter_pids(evlist, 1, &pid);
12061827ab5bSArnaldo Carvalho de Melo }
12071827ab5bSArnaldo Carvalho de Melo 
1208b3c2cc2bSArnaldo Carvalho de Melo bool evlist__valid_sample_type(struct evlist *evlist)
120974429964SFrederic Weisbecker {
121032dcd021SJiri Olsa 	struct evsel *pos;
1211c2a70653SArnaldo Carvalho de Melo 
12126484d2f9SJiri Olsa 	if (evlist->core.nr_entries == 1)
121375562573SAdrian Hunter 		return true;
121475562573SAdrian Hunter 
121575562573SAdrian Hunter 	if (evlist->id_pos < 0 || evlist->is_pos < 0)
121675562573SAdrian Hunter 		return false;
121775562573SAdrian Hunter 
1218e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, pos) {
121975562573SAdrian Hunter 		if (pos->id_pos != evlist->id_pos ||
122075562573SAdrian Hunter 		    pos->is_pos != evlist->is_pos)
1221c2a70653SArnaldo Carvalho de Melo 			return false;
122274429964SFrederic Weisbecker 	}
122374429964SFrederic Weisbecker 
1224c2a70653SArnaldo Carvalho de Melo 	return true;
1225c2a70653SArnaldo Carvalho de Melo }
1226c2a70653SArnaldo Carvalho de Melo 
1227b3c2cc2bSArnaldo Carvalho de Melo u64 __evlist__combined_sample_type(struct evlist *evlist)
1228c2a70653SArnaldo Carvalho de Melo {
122932dcd021SJiri Olsa 	struct evsel *evsel;
123075562573SAdrian Hunter 
123175562573SAdrian Hunter 	if (evlist->combined_sample_type)
123275562573SAdrian Hunter 		return evlist->combined_sample_type;
123375562573SAdrian Hunter 
1234e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, evsel)
12351fc632ceSJiri Olsa 		evlist->combined_sample_type |= evsel->core.attr.sample_type;
123675562573SAdrian Hunter 
123775562573SAdrian Hunter 	return evlist->combined_sample_type;
123875562573SAdrian Hunter }
123975562573SAdrian Hunter 
1240b3c2cc2bSArnaldo Carvalho de Melo u64 evlist__combined_sample_type(struct evlist *evlist)
124175562573SAdrian Hunter {
124275562573SAdrian Hunter 	evlist->combined_sample_type = 0;
1243b3c2cc2bSArnaldo Carvalho de Melo 	return __evlist__combined_sample_type(evlist);
1244c2a70653SArnaldo Carvalho de Melo }
1245c2a70653SArnaldo Carvalho de Melo 
124692c7d7cdSArnaldo Carvalho de Melo u64 evlist__combined_branch_type(struct evlist *evlist)
124798df858eSAndi Kleen {
124832dcd021SJiri Olsa 	struct evsel *evsel;
124998df858eSAndi Kleen 	u64 branch_type = 0;
125098df858eSAndi Kleen 
1251e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, evsel)
12521fc632ceSJiri Olsa 		branch_type |= evsel->core.attr.branch_sample_type;
125398df858eSAndi Kleen 	return branch_type;
125498df858eSAndi Kleen }
125598df858eSAndi Kleen 
125678e1bc25SArnaldo Carvalho de Melo bool evlist__valid_read_format(struct evlist *evlist)
12579ede473cSJiri Olsa {
1258515dbe48SJiri Olsa 	struct evsel *first = evlist__first(evlist), *pos = first;
12591fc632ceSJiri Olsa 	u64 read_format = first->core.attr.read_format;
12601fc632ceSJiri Olsa 	u64 sample_type = first->core.attr.sample_type;
12619ede473cSJiri Olsa 
1262e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, pos) {
126394d3820fSAdrian Hunter 		if (read_format != pos->core.attr.read_format) {
126494d3820fSAdrian Hunter 			pr_debug("Read format differs %#" PRIx64 " vs %#" PRIx64 "\n",
126594d3820fSAdrian Hunter 				 read_format, (u64)pos->core.attr.read_format);
126694d3820fSAdrian Hunter 		}
12679ede473cSJiri Olsa 	}
12689ede473cSJiri Olsa 
12694d39c89fSIngo Molnar 	/* PERF_SAMPLE_READ implies PERF_FORMAT_ID. */
12709ede473cSJiri Olsa 	if ((sample_type & PERF_SAMPLE_READ) &&
12719ede473cSJiri Olsa 	    !(read_format & PERF_FORMAT_ID)) {
12729ede473cSJiri Olsa 		return false;
12739ede473cSJiri Olsa 	}
12749ede473cSJiri Olsa 
12759ede473cSJiri Olsa 	return true;
12769ede473cSJiri Olsa }
12779ede473cSJiri Olsa 
12781420ba2fSArnaldo Carvalho de Melo u16 evlist__id_hdr_size(struct evlist *evlist)
127981e36bffSArnaldo Carvalho de Melo {
1280515dbe48SJiri Olsa 	struct evsel *first = evlist__first(evlist);
128181e36bffSArnaldo Carvalho de Melo 
12820a64de04SAdrian Hunter 	return first->core.attr.sample_id_all ? evsel__id_hdr_size(first) : 0;
128381e36bffSArnaldo Carvalho de Melo }
128481e36bffSArnaldo Carvalho de Melo 
12858cedf3a5SArnaldo Carvalho de Melo bool evlist__valid_sample_id_all(struct evlist *evlist)
1286c2a70653SArnaldo Carvalho de Melo {
1287515dbe48SJiri Olsa 	struct evsel *first = evlist__first(evlist), *pos = first;
1288c2a70653SArnaldo Carvalho de Melo 
1289e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry_continue(evlist, pos) {
12901fc632ceSJiri Olsa 		if (first->core.attr.sample_id_all != pos->core.attr.sample_id_all)
1291c2a70653SArnaldo Carvalho de Melo 			return false;
1292c2a70653SArnaldo Carvalho de Melo 	}
1293c2a70653SArnaldo Carvalho de Melo 
1294c2a70653SArnaldo Carvalho de Melo 	return true;
129574429964SFrederic Weisbecker }
129674429964SFrederic Weisbecker 
12978cedf3a5SArnaldo Carvalho de Melo bool evlist__sample_id_all(struct evlist *evlist)
129874429964SFrederic Weisbecker {
1299515dbe48SJiri Olsa 	struct evsel *first = evlist__first(evlist);
13001fc632ceSJiri Olsa 	return first->core.attr.sample_id_all;
130174429964SFrederic Weisbecker }
130281cce8deSArnaldo Carvalho de Melo 
1303900c8eadSArnaldo Carvalho de Melo void evlist__set_selected(struct evlist *evlist, struct evsel *evsel)
130481cce8deSArnaldo Carvalho de Melo {
130581cce8deSArnaldo Carvalho de Melo 	evlist->selected = evsel;
130681cce8deSArnaldo Carvalho de Melo }
1307727ab04eSArnaldo Carvalho de Melo 
1308750b4edeSJiri Olsa void evlist__close(struct evlist *evlist)
1309a74b4b66SNamhyung Kim {
131032dcd021SJiri Olsa 	struct evsel *evsel;
1311472832d2SIan Rogers 	struct evlist_cpu_iterator evlist_cpu_itr;
13127736627bSAndi Kleen 	struct affinity affinity;
1313a74b4b66SNamhyung Kim 
13147736627bSAndi Kleen 	/*
13150df6ade7SIan Rogers 	 * With perf record core.user_requested_cpus is usually NULL.
13167736627bSAndi Kleen 	 * Use the old method to handle this for now.
13177736627bSAndi Kleen 	 */
13180df6ade7SIan Rogers 	if (!evlist->core.user_requested_cpus ||
13190df6ade7SIan Rogers 	    cpu_map__is_dummy(evlist->core.user_requested_cpus)) {
1320475fb533SAndi Kleen 		evlist__for_each_entry_reverse(evlist, evsel)
132188761fa1SJiri Olsa 			evsel__close(evsel);
13227736627bSAndi Kleen 		return;
13237736627bSAndi Kleen 	}
13247736627bSAndi Kleen 
13257736627bSAndi Kleen 	if (affinity__setup(&affinity) < 0)
13267736627bSAndi Kleen 		return;
13277736627bSAndi Kleen 
1328472832d2SIan Rogers 	evlist__for_each_cpu(evlist_cpu_itr, evlist, &affinity) {
1329472832d2SIan Rogers 		perf_evsel__close_cpu(&evlist_cpu_itr.evsel->core,
1330472832d2SIan Rogers 				      evlist_cpu_itr.cpu_map_idx);
13317736627bSAndi Kleen 	}
1332472832d2SIan Rogers 
13337736627bSAndi Kleen 	affinity__cleanup(&affinity);
13347736627bSAndi Kleen 	evlist__for_each_entry_reverse(evlist, evsel) {
13357736627bSAndi Kleen 		perf_evsel__free_fd(&evsel->core);
13367736627bSAndi Kleen 		perf_evsel__free_id(&evsel->core);
13377736627bSAndi Kleen 	}
1338513068f2SNamhyung Kim 	perf_evlist__reset_id_hash(&evlist->core);
1339a74b4b66SNamhyung Kim }
1340a74b4b66SNamhyung Kim 
13417748bb71SArnaldo Carvalho de Melo static int evlist__create_syswide_maps(struct evlist *evlist)
13424112eb18SArnaldo Carvalho de Melo {
1343f854839bSJiri Olsa 	struct perf_cpu_map *cpus;
13449749b90eSJiri Olsa 	struct perf_thread_map *threads;
13454112eb18SArnaldo Carvalho de Melo 
13464112eb18SArnaldo Carvalho de Melo 	/*
13474112eb18SArnaldo Carvalho de Melo 	 * Try reading /sys/devices/system/cpu/online to get
13484112eb18SArnaldo Carvalho de Melo 	 * an all cpus map.
13494112eb18SArnaldo Carvalho de Melo 	 *
13504112eb18SArnaldo Carvalho de Melo 	 * FIXME: -ENOMEM is the best we can do here, the cpu_map
13514112eb18SArnaldo Carvalho de Melo 	 * code needs an overhaul to properly forward the
13524112eb18SArnaldo Carvalho de Melo 	 * error, and we may not want to do that fallback to a
13534112eb18SArnaldo Carvalho de Melo 	 * default cpu identity map :-\
13544112eb18SArnaldo Carvalho de Melo 	 */
13559c3516d1SJiri Olsa 	cpus = perf_cpu_map__new(NULL);
13568c0498b6SAdrian Hunter 	if (!cpus)
13574112eb18SArnaldo Carvalho de Melo 		goto out;
13584112eb18SArnaldo Carvalho de Melo 
13594b49cce2SJiri Olsa 	threads = perf_thread_map__new_dummy();
13608c0498b6SAdrian Hunter 	if (!threads)
13618c0498b6SAdrian Hunter 		goto out_put;
13624112eb18SArnaldo Carvalho de Melo 
1363453fa030SJiri Olsa 	perf_evlist__set_maps(&evlist->core, cpus, threads);
1364bfd1b83dSNamhyung Kim 
1365bfd1b83dSNamhyung Kim 	perf_thread_map__put(threads);
13668c0498b6SAdrian Hunter out_put:
136738f01d8dSJiri Olsa 	perf_cpu_map__put(cpus);
1368bfd1b83dSNamhyung Kim out:
1369f717d89aSHaowen Bai 	return -ENOMEM;
13704112eb18SArnaldo Carvalho de Melo }
13714112eb18SArnaldo Carvalho de Melo 
1372474ddc4cSJiri Olsa int evlist__open(struct evlist *evlist)
1373727ab04eSArnaldo Carvalho de Melo {
137432dcd021SJiri Olsa 	struct evsel *evsel;
1375a74b4b66SNamhyung Kim 	int err;
1376727ab04eSArnaldo Carvalho de Melo 
13774112eb18SArnaldo Carvalho de Melo 	/*
13784112eb18SArnaldo Carvalho de Melo 	 * Default: one fd per CPU, all threads, aka systemwide
13794112eb18SArnaldo Carvalho de Melo 	 * as sys_perf_event_open(cpu = -1, thread = -1) is EINVAL
13804112eb18SArnaldo Carvalho de Melo 	 */
13810df6ade7SIan Rogers 	if (evlist->core.threads == NULL && evlist->core.user_requested_cpus == NULL) {
13827748bb71SArnaldo Carvalho de Melo 		err = evlist__create_syswide_maps(evlist);
13834112eb18SArnaldo Carvalho de Melo 		if (err < 0)
13844112eb18SArnaldo Carvalho de Melo 			goto out_err;
13854112eb18SArnaldo Carvalho de Melo 	}
13864112eb18SArnaldo Carvalho de Melo 
13870a7e7ec9SArnaldo Carvalho de Melo 	evlist__update_id_pos(evlist);
1388733cd2feSAdrian Hunter 
1389e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, evsel) {
1390af663bd0SJiri Olsa 		err = evsel__open(evsel, evsel->core.cpus, evsel->core.threads);
1391727ab04eSArnaldo Carvalho de Melo 		if (err < 0)
1392727ab04eSArnaldo Carvalho de Melo 			goto out_err;
1393727ab04eSArnaldo Carvalho de Melo 	}
1394727ab04eSArnaldo Carvalho de Melo 
1395727ab04eSArnaldo Carvalho de Melo 	return 0;
1396727ab04eSArnaldo Carvalho de Melo out_err:
1397750b4edeSJiri Olsa 	evlist__close(evlist);
139841c21a68SNamhyung Kim 	errno = -err;
1399727ab04eSArnaldo Carvalho de Melo 	return err;
1400727ab04eSArnaldo Carvalho de Melo }
140135b9d88eSArnaldo Carvalho de Melo 
14027b392ef0SArnaldo Carvalho de Melo int evlist__prepare_workload(struct evlist *evlist, struct target *target, const char *argv[],
14037b392ef0SArnaldo Carvalho de Melo 			     bool pipe_output, void (*exec_error)(int signo, siginfo_t *info, void *ucontext))
140435b9d88eSArnaldo Carvalho de Melo {
140535b9d88eSArnaldo Carvalho de Melo 	int child_ready_pipe[2], go_pipe[2];
140635b9d88eSArnaldo Carvalho de Melo 	char bf;
140735b9d88eSArnaldo Carvalho de Melo 
140835b9d88eSArnaldo Carvalho de Melo 	if (pipe(child_ready_pipe) < 0) {
140935b9d88eSArnaldo Carvalho de Melo 		perror("failed to create 'ready' pipe");
141035b9d88eSArnaldo Carvalho de Melo 		return -1;
141135b9d88eSArnaldo Carvalho de Melo 	}
141235b9d88eSArnaldo Carvalho de Melo 
141335b9d88eSArnaldo Carvalho de Melo 	if (pipe(go_pipe) < 0) {
141435b9d88eSArnaldo Carvalho de Melo 		perror("failed to create 'go' pipe");
141535b9d88eSArnaldo Carvalho de Melo 		goto out_close_ready_pipe;
141635b9d88eSArnaldo Carvalho de Melo 	}
141735b9d88eSArnaldo Carvalho de Melo 
141835b9d88eSArnaldo Carvalho de Melo 	evlist->workload.pid = fork();
141935b9d88eSArnaldo Carvalho de Melo 	if (evlist->workload.pid < 0) {
142035b9d88eSArnaldo Carvalho de Melo 		perror("failed to fork");
142135b9d88eSArnaldo Carvalho de Melo 		goto out_close_pipes;
142235b9d88eSArnaldo Carvalho de Melo 	}
142335b9d88eSArnaldo Carvalho de Melo 
142435b9d88eSArnaldo Carvalho de Melo 	if (!evlist->workload.pid) {
14255f1c4225SArnaldo Carvalho de Melo 		int ret;
14265f1c4225SArnaldo Carvalho de Melo 
1427119fa3c9SNamhyung Kim 		if (pipe_output)
142835b9d88eSArnaldo Carvalho de Melo 			dup2(2, 1);
142935b9d88eSArnaldo Carvalho de Melo 
14300817df08SDavid Ahern 		signal(SIGTERM, SIG_DFL);
14310817df08SDavid Ahern 
143235b9d88eSArnaldo Carvalho de Melo 		close(child_ready_pipe[0]);
143335b9d88eSArnaldo Carvalho de Melo 		close(go_pipe[1]);
143435b9d88eSArnaldo Carvalho de Melo 		fcntl(go_pipe[0], F_SETFD, FD_CLOEXEC);
143535b9d88eSArnaldo Carvalho de Melo 
143635b9d88eSArnaldo Carvalho de Melo 		/*
1437a7672d1dSArnaldo Carvalho de Melo 		 * Change the name of this process not to confuse --exclude-perf users
1438a7672d1dSArnaldo Carvalho de Melo 		 * that sees 'perf' in the window up to the execvp() and thinks that
1439a7672d1dSArnaldo Carvalho de Melo 		 * perf samples are not being excluded.
1440a7672d1dSArnaldo Carvalho de Melo 		 */
1441a7672d1dSArnaldo Carvalho de Melo 		prctl(PR_SET_NAME, "perf-exec");
1442a7672d1dSArnaldo Carvalho de Melo 
1443a7672d1dSArnaldo Carvalho de Melo 		/*
144435b9d88eSArnaldo Carvalho de Melo 		 * Tell the parent we're ready to go
144535b9d88eSArnaldo Carvalho de Melo 		 */
144635b9d88eSArnaldo Carvalho de Melo 		close(child_ready_pipe[1]);
144735b9d88eSArnaldo Carvalho de Melo 
144835b9d88eSArnaldo Carvalho de Melo 		/*
144935b9d88eSArnaldo Carvalho de Melo 		 * Wait until the parent tells us to go.
145035b9d88eSArnaldo Carvalho de Melo 		 */
14515f1c4225SArnaldo Carvalho de Melo 		ret = read(go_pipe[0], &bf, 1);
14525f1c4225SArnaldo Carvalho de Melo 		/*
14535f1c4225SArnaldo Carvalho de Melo 		 * The parent will ask for the execvp() to be performed by
14545f1c4225SArnaldo Carvalho de Melo 		 * writing exactly one byte, in workload.cork_fd, usually via
14557b392ef0SArnaldo Carvalho de Melo 		 * evlist__start_workload().
14565f1c4225SArnaldo Carvalho de Melo 		 *
145720f86fc1SArnaldo Carvalho de Melo 		 * For cancelling the workload without actually running it,
14585f1c4225SArnaldo Carvalho de Melo 		 * the parent will just close workload.cork_fd, without writing
14595f1c4225SArnaldo Carvalho de Melo 		 * anything, i.e. read will return zero and we just exit()
14605f1c4225SArnaldo Carvalho de Melo 		 * here.
14615f1c4225SArnaldo Carvalho de Melo 		 */
14625f1c4225SArnaldo Carvalho de Melo 		if (ret != 1) {
14635f1c4225SArnaldo Carvalho de Melo 			if (ret == -1)
146435b9d88eSArnaldo Carvalho de Melo 				perror("unable to read pipe");
14655f1c4225SArnaldo Carvalho de Melo 			exit(ret);
14665f1c4225SArnaldo Carvalho de Melo 		}
146735b9d88eSArnaldo Carvalho de Melo 
146835b9d88eSArnaldo Carvalho de Melo 		execvp(argv[0], (char **)argv);
146935b9d88eSArnaldo Carvalho de Melo 
1470735f7e0bSArnaldo Carvalho de Melo 		if (exec_error) {
1471f33cbe72SArnaldo Carvalho de Melo 			union sigval val;
1472f33cbe72SArnaldo Carvalho de Melo 
1473f33cbe72SArnaldo Carvalho de Melo 			val.sival_int = errno;
1474f33cbe72SArnaldo Carvalho de Melo 			if (sigqueue(getppid(), SIGUSR1, val))
147535b9d88eSArnaldo Carvalho de Melo 				perror(argv[0]);
1476f33cbe72SArnaldo Carvalho de Melo 		} else
1477f33cbe72SArnaldo Carvalho de Melo 			perror(argv[0]);
147835b9d88eSArnaldo Carvalho de Melo 		exit(-1);
147935b9d88eSArnaldo Carvalho de Melo 	}
148035b9d88eSArnaldo Carvalho de Melo 
1481735f7e0bSArnaldo Carvalho de Melo 	if (exec_error) {
1482735f7e0bSArnaldo Carvalho de Melo 		struct sigaction act = {
1483735f7e0bSArnaldo Carvalho de Melo 			.sa_flags     = SA_SIGINFO,
1484735f7e0bSArnaldo Carvalho de Melo 			.sa_sigaction = exec_error,
1485735f7e0bSArnaldo Carvalho de Melo 		};
1486735f7e0bSArnaldo Carvalho de Melo 		sigaction(SIGUSR1, &act, NULL);
1487735f7e0bSArnaldo Carvalho de Melo 	}
1488735f7e0bSArnaldo Carvalho de Melo 
14891aaf63b1SArnaldo Carvalho de Melo 	if (target__none(target)) {
149003617c22SJiri Olsa 		if (evlist->core.threads == NULL) {
14911aaf63b1SArnaldo Carvalho de Melo 			fprintf(stderr, "FATAL: evlist->threads need to be set at this point (%s:%d).\n",
14921aaf63b1SArnaldo Carvalho de Melo 				__func__, __LINE__);
14931aaf63b1SArnaldo Carvalho de Melo 			goto out_close_pipes;
14941aaf63b1SArnaldo Carvalho de Melo 		}
149503617c22SJiri Olsa 		perf_thread_map__set_pid(evlist->core.threads, 0, evlist->workload.pid);
14961aaf63b1SArnaldo Carvalho de Melo 	}
149735b9d88eSArnaldo Carvalho de Melo 
149835b9d88eSArnaldo Carvalho de Melo 	close(child_ready_pipe[1]);
149935b9d88eSArnaldo Carvalho de Melo 	close(go_pipe[0]);
150035b9d88eSArnaldo Carvalho de Melo 	/*
150135b9d88eSArnaldo Carvalho de Melo 	 * wait for child to settle
150235b9d88eSArnaldo Carvalho de Melo 	 */
150335b9d88eSArnaldo Carvalho de Melo 	if (read(child_ready_pipe[0], &bf, 1) == -1) {
150435b9d88eSArnaldo Carvalho de Melo 		perror("unable to read pipe");
150535b9d88eSArnaldo Carvalho de Melo 		goto out_close_pipes;
150635b9d88eSArnaldo Carvalho de Melo 	}
150735b9d88eSArnaldo Carvalho de Melo 
1508bcf3145fSNamhyung Kim 	fcntl(go_pipe[1], F_SETFD, FD_CLOEXEC);
150935b9d88eSArnaldo Carvalho de Melo 	evlist->workload.cork_fd = go_pipe[1];
151035b9d88eSArnaldo Carvalho de Melo 	close(child_ready_pipe[0]);
151135b9d88eSArnaldo Carvalho de Melo 	return 0;
151235b9d88eSArnaldo Carvalho de Melo 
151335b9d88eSArnaldo Carvalho de Melo out_close_pipes:
151435b9d88eSArnaldo Carvalho de Melo 	close(go_pipe[0]);
151535b9d88eSArnaldo Carvalho de Melo 	close(go_pipe[1]);
151635b9d88eSArnaldo Carvalho de Melo out_close_ready_pipe:
151735b9d88eSArnaldo Carvalho de Melo 	close(child_ready_pipe[0]);
151835b9d88eSArnaldo Carvalho de Melo 	close(child_ready_pipe[1]);
151935b9d88eSArnaldo Carvalho de Melo 	return -1;
152035b9d88eSArnaldo Carvalho de Melo }
152135b9d88eSArnaldo Carvalho de Melo 
15227b392ef0SArnaldo Carvalho de Melo int evlist__start_workload(struct evlist *evlist)
152335b9d88eSArnaldo Carvalho de Melo {
152435b9d88eSArnaldo Carvalho de Melo 	if (evlist->workload.cork_fd > 0) {
1525b3824404SDavid Ahern 		char bf = 0;
1526bcf3145fSNamhyung Kim 		int ret;
152735b9d88eSArnaldo Carvalho de Melo 		/*
152835b9d88eSArnaldo Carvalho de Melo 		 * Remove the cork, let it rip!
152935b9d88eSArnaldo Carvalho de Melo 		 */
1530bcf3145fSNamhyung Kim 		ret = write(evlist->workload.cork_fd, &bf, 1);
1531bcf3145fSNamhyung Kim 		if (ret < 0)
1532e978be9eSSoramichi Akiyama 			perror("unable to write to pipe");
1533bcf3145fSNamhyung Kim 
1534bcf3145fSNamhyung Kim 		close(evlist->workload.cork_fd);
1535bcf3145fSNamhyung Kim 		return ret;
153635b9d88eSArnaldo Carvalho de Melo 	}
153735b9d88eSArnaldo Carvalho de Melo 
153835b9d88eSArnaldo Carvalho de Melo 	return 0;
153935b9d88eSArnaldo Carvalho de Melo }
1540cb0b29e0SArnaldo Carvalho de Melo 
15412a6599cdSArnaldo Carvalho de Melo int evlist__parse_sample(struct evlist *evlist, union perf_event *event, struct perf_sample *sample)
1542cb0b29e0SArnaldo Carvalho de Melo {
15433ccf8a7bSArnaldo Carvalho de Melo 	struct evsel *evsel = evlist__event2evsel(evlist, event);
15443461b65dSAdrian Hunter 	int ret;
154575562573SAdrian Hunter 
154675562573SAdrian Hunter 	if (!evsel)
154775562573SAdrian Hunter 		return -EFAULT;
15483461b65dSAdrian Hunter 	ret = evsel__parse_sample(evsel, event, sample);
15493461b65dSAdrian Hunter 	if (ret)
15503461b65dSAdrian Hunter 		return ret;
15513461b65dSAdrian Hunter 	if (perf_guest && sample->id) {
15523461b65dSAdrian Hunter 		struct perf_sample_id *sid = evlist__id2sid(evlist, sample->id);
15533461b65dSAdrian Hunter 
15543461b65dSAdrian Hunter 		if (sid) {
15553461b65dSAdrian Hunter 			sample->machine_pid = sid->machine_pid;
15563461b65dSAdrian Hunter 			sample->vcpu = sid->vcpu.cpu;
15573461b65dSAdrian Hunter 		}
15583461b65dSAdrian Hunter 	}
15593461b65dSAdrian Hunter 	return 0;
1560cb0b29e0SArnaldo Carvalho de Melo }
156178f067b3SArnaldo Carvalho de Melo 
15622a6599cdSArnaldo Carvalho de Melo int evlist__parse_sample_timestamp(struct evlist *evlist, union perf_event *event, u64 *timestamp)
156301468120SJiri Olsa {
15643ccf8a7bSArnaldo Carvalho de Melo 	struct evsel *evsel = evlist__event2evsel(evlist, event);
156501468120SJiri Olsa 
156601468120SJiri Olsa 	if (!evsel)
156701468120SJiri Olsa 		return -EFAULT;
15686b6017a2SArnaldo Carvalho de Melo 	return evsel__parse_sample_timestamp(evsel, event, timestamp);
156901468120SJiri Olsa }
157001468120SJiri Olsa 
1571d1f249ecSArnaldo Carvalho de Melo int evlist__strerror_open(struct evlist *evlist, int err, char *buf, size_t size)
1572a8f23d8fSArnaldo Carvalho de Melo {
1573a8f23d8fSArnaldo Carvalho de Melo 	int printed, value;
1574c8b5f2c9SArnaldo Carvalho de Melo 	char sbuf[STRERR_BUFSIZE], *emsg = str_error_r(err, sbuf, sizeof(sbuf));
1575a8f23d8fSArnaldo Carvalho de Melo 
1576a8f23d8fSArnaldo Carvalho de Melo 	switch (err) {
1577a8f23d8fSArnaldo Carvalho de Melo 	case EACCES:
1578a8f23d8fSArnaldo Carvalho de Melo 	case EPERM:
1579a8f23d8fSArnaldo Carvalho de Melo 		printed = scnprintf(buf, size,
1580a8f23d8fSArnaldo Carvalho de Melo 				    "Error:\t%s.\n"
1581a8f23d8fSArnaldo Carvalho de Melo 				    "Hint:\tCheck /proc/sys/kernel/perf_event_paranoid setting.", emsg);
1582a8f23d8fSArnaldo Carvalho de Melo 
15831a47245dSAdrian Hunter 		value = perf_event_paranoid();
1584a8f23d8fSArnaldo Carvalho de Melo 
1585a8f23d8fSArnaldo Carvalho de Melo 		printed += scnprintf(buf + printed, size - printed, "\nHint:\t");
1586a8f23d8fSArnaldo Carvalho de Melo 
1587a8f23d8fSArnaldo Carvalho de Melo 		if (value >= 2) {
1588a8f23d8fSArnaldo Carvalho de Melo 			printed += scnprintf(buf + printed, size - printed,
1589a8f23d8fSArnaldo Carvalho de Melo 					     "For your workloads it needs to be <= 1\nHint:\t");
1590a8f23d8fSArnaldo Carvalho de Melo 		}
1591a8f23d8fSArnaldo Carvalho de Melo 		printed += scnprintf(buf + printed, size - printed,
15925229e366SArnaldo Carvalho de Melo 				     "For system wide tracing it needs to be set to -1.\n");
1593a8f23d8fSArnaldo Carvalho de Melo 
1594a8f23d8fSArnaldo Carvalho de Melo 		printed += scnprintf(buf + printed, size - printed,
15955229e366SArnaldo Carvalho de Melo 				    "Hint:\tTry: 'sudo sh -c \"echo -1 > /proc/sys/kernel/perf_event_paranoid\"'\n"
15965229e366SArnaldo Carvalho de Melo 				    "Hint:\tThe current value is %d.", value);
1597a8f23d8fSArnaldo Carvalho de Melo 		break;
1598d9aade7fSArnaldo Carvalho de Melo 	case EINVAL: {
1599515dbe48SJiri Olsa 		struct evsel *first = evlist__first(evlist);
1600d9aade7fSArnaldo Carvalho de Melo 		int max_freq;
1601d9aade7fSArnaldo Carvalho de Melo 
1602d9aade7fSArnaldo Carvalho de Melo 		if (sysctl__read_int("kernel/perf_event_max_sample_rate", &max_freq) < 0)
1603d9aade7fSArnaldo Carvalho de Melo 			goto out_default;
1604d9aade7fSArnaldo Carvalho de Melo 
16051fc632ceSJiri Olsa 		if (first->core.attr.sample_freq < (u64)max_freq)
1606d9aade7fSArnaldo Carvalho de Melo 			goto out_default;
1607d9aade7fSArnaldo Carvalho de Melo 
1608d9aade7fSArnaldo Carvalho de Melo 		printed = scnprintf(buf, size,
1609d9aade7fSArnaldo Carvalho de Melo 				    "Error:\t%s.\n"
1610d9aade7fSArnaldo Carvalho de Melo 				    "Hint:\tCheck /proc/sys/kernel/perf_event_max_sample_rate.\n"
1611d9aade7fSArnaldo Carvalho de Melo 				    "Hint:\tThe current value is %d and %" PRIu64 " is being requested.",
16121fc632ceSJiri Olsa 				    emsg, max_freq, first->core.attr.sample_freq);
1613d9aade7fSArnaldo Carvalho de Melo 		break;
1614d9aade7fSArnaldo Carvalho de Melo 	}
1615a8f23d8fSArnaldo Carvalho de Melo 	default:
1616d9aade7fSArnaldo Carvalho de Melo out_default:
1617a8f23d8fSArnaldo Carvalho de Melo 		scnprintf(buf, size, "%s", emsg);
1618a8f23d8fSArnaldo Carvalho de Melo 		break;
1619a8f23d8fSArnaldo Carvalho de Melo 	}
1620a8f23d8fSArnaldo Carvalho de Melo 
1621a8f23d8fSArnaldo Carvalho de Melo 	return 0;
1622a8f23d8fSArnaldo Carvalho de Melo }
1623a025e4f0SAdrian Hunter 
1624d1f249ecSArnaldo Carvalho de Melo int evlist__strerror_mmap(struct evlist *evlist, int err, char *buf, size_t size)
1625956fa571SArnaldo Carvalho de Melo {
1626c8b5f2c9SArnaldo Carvalho de Melo 	char sbuf[STRERR_BUFSIZE], *emsg = str_error_r(err, sbuf, sizeof(sbuf));
1627f6fa4375SJiri Olsa 	int pages_attempted = evlist->core.mmap_len / 1024, pages_max_per_user, printed = 0;
1628956fa571SArnaldo Carvalho de Melo 
1629956fa571SArnaldo Carvalho de Melo 	switch (err) {
1630956fa571SArnaldo Carvalho de Melo 	case EPERM:
1631e5d4a290SArnaldo Carvalho de Melo 		sysctl__read_int("kernel/perf_event_mlock_kb", &pages_max_per_user);
1632e965bea1SArnaldo Carvalho de Melo 		printed += scnprintf(buf + printed, size - printed,
1633e965bea1SArnaldo Carvalho de Melo 				     "Error:\t%s.\n"
1634956fa571SArnaldo Carvalho de Melo 				     "Hint:\tCheck /proc/sys/kernel/perf_event_mlock_kb (%d kB) setting.\n"
1635e965bea1SArnaldo Carvalho de Melo 				     "Hint:\tTried using %zd kB.\n",
1636e5d4a290SArnaldo Carvalho de Melo 				     emsg, pages_max_per_user, pages_attempted);
1637e965bea1SArnaldo Carvalho de Melo 
1638e965bea1SArnaldo Carvalho de Melo 		if (pages_attempted >= pages_max_per_user) {
1639e965bea1SArnaldo Carvalho de Melo 			printed += scnprintf(buf + printed, size - printed,
1640e965bea1SArnaldo Carvalho de Melo 					     "Hint:\tTry 'sudo sh -c \"echo %d > /proc/sys/kernel/perf_event_mlock_kb\"', or\n",
1641e965bea1SArnaldo Carvalho de Melo 					     pages_max_per_user + pages_attempted);
1642e965bea1SArnaldo Carvalho de Melo 		}
1643e965bea1SArnaldo Carvalho de Melo 
1644e965bea1SArnaldo Carvalho de Melo 		printed += scnprintf(buf + printed, size - printed,
1645e965bea1SArnaldo Carvalho de Melo 				     "Hint:\tTry using a smaller -m/--mmap-pages value.");
1646956fa571SArnaldo Carvalho de Melo 		break;
1647956fa571SArnaldo Carvalho de Melo 	default:
1648956fa571SArnaldo Carvalho de Melo 		scnprintf(buf, size, "%s", emsg);
1649956fa571SArnaldo Carvalho de Melo 		break;
1650956fa571SArnaldo Carvalho de Melo 	}
1651956fa571SArnaldo Carvalho de Melo 
1652956fa571SArnaldo Carvalho de Melo 	return 0;
1653956fa571SArnaldo Carvalho de Melo }
1654956fa571SArnaldo Carvalho de Melo 
1655e414fd1aSArnaldo Carvalho de Melo void evlist__to_front(struct evlist *evlist, struct evsel *move_evsel)
1656a025e4f0SAdrian Hunter {
165732dcd021SJiri Olsa 	struct evsel *evsel, *n;
1658a025e4f0SAdrian Hunter 	LIST_HEAD(move);
1659a025e4f0SAdrian Hunter 
1660515dbe48SJiri Olsa 	if (move_evsel == evlist__first(evlist))
1661a025e4f0SAdrian Hunter 		return;
1662a025e4f0SAdrian Hunter 
1663e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry_safe(evlist, n, evsel) {
1664fba7c866SJiri Olsa 		if (evsel__leader(evsel) == evsel__leader(move_evsel))
1665b27c4eceSJiri Olsa 			list_move_tail(&evsel->core.node, &move);
1666a025e4f0SAdrian Hunter 	}
1667a025e4f0SAdrian Hunter 
1668ce9036a6SJiri Olsa 	list_splice(&move, &evlist->core.entries);
1669a025e4f0SAdrian Hunter }
167060b0896cSAdrian Hunter 
1671e80db255SArnaldo Carvalho de Melo struct evsel *evlist__get_tracking_event(struct evlist *evlist)
1672442ad225SAdrian Hunter {
1673442ad225SAdrian Hunter 	struct evsel *evsel;
1674442ad225SAdrian Hunter 
1675442ad225SAdrian Hunter 	evlist__for_each_entry(evlist, evsel) {
1676442ad225SAdrian Hunter 		if (evsel->tracking)
1677442ad225SAdrian Hunter 			return evsel;
1678442ad225SAdrian Hunter 	}
1679442ad225SAdrian Hunter 
1680442ad225SAdrian Hunter 	return evlist__first(evlist);
1681442ad225SAdrian Hunter }
1682442ad225SAdrian Hunter 
1683e80db255SArnaldo Carvalho de Melo void evlist__set_tracking_event(struct evlist *evlist, struct evsel *tracking_evsel)
168460b0896cSAdrian Hunter {
168532dcd021SJiri Olsa 	struct evsel *evsel;
168660b0896cSAdrian Hunter 
168760b0896cSAdrian Hunter 	if (tracking_evsel->tracking)
168860b0896cSAdrian Hunter 		return;
168960b0896cSAdrian Hunter 
1690e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, evsel) {
169160b0896cSAdrian Hunter 		if (evsel != tracking_evsel)
169260b0896cSAdrian Hunter 			evsel->tracking = false;
169360b0896cSAdrian Hunter 	}
169460b0896cSAdrian Hunter 
169560b0896cSAdrian Hunter 	tracking_evsel->tracking = true;
169660b0896cSAdrian Hunter }
16977630b3e2SWang Nan 
1698b02736f7SArnaldo Carvalho de Melo struct evsel *evlist__find_evsel_by_str(struct evlist *evlist, const char *str)
16997630b3e2SWang Nan {
170032dcd021SJiri Olsa 	struct evsel *evsel;
17017630b3e2SWang Nan 
1702e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, evsel) {
17037630b3e2SWang Nan 		if (!evsel->name)
17047630b3e2SWang Nan 			continue;
1705ce1d3bc2SArnaldo Carvalho de Melo 		if (evsel__name_is(evsel, str))
17067630b3e2SWang Nan 			return evsel;
17077630b3e2SWang Nan 	}
17087630b3e2SWang Nan 
17097630b3e2SWang Nan 	return NULL;
17107630b3e2SWang Nan }
171154cc54deSWang Nan 
1712ade9d208SArnaldo Carvalho de Melo void evlist__toggle_bkw_mmap(struct evlist *evlist, enum bkw_mmap_state state)
171354cc54deSWang Nan {
171454cc54deSWang Nan 	enum bkw_mmap_state old_state = evlist->bkw_mmap_state;
171554cc54deSWang Nan 	enum action {
171654cc54deSWang Nan 		NONE,
171754cc54deSWang Nan 		PAUSE,
171854cc54deSWang Nan 		RESUME,
171954cc54deSWang Nan 	} action = NONE;
172054cc54deSWang Nan 
17210b72d69aSWang Nan 	if (!evlist->overwrite_mmap)
172254cc54deSWang Nan 		return;
172354cc54deSWang Nan 
172454cc54deSWang Nan 	switch (old_state) {
172554cc54deSWang Nan 	case BKW_MMAP_NOTREADY: {
172654cc54deSWang Nan 		if (state != BKW_MMAP_RUNNING)
1727dd8bd53aSLuis de Bethencourt 			goto state_err;
172854cc54deSWang Nan 		break;
172954cc54deSWang Nan 	}
173054cc54deSWang Nan 	case BKW_MMAP_RUNNING: {
173154cc54deSWang Nan 		if (state != BKW_MMAP_DATA_PENDING)
173254cc54deSWang Nan 			goto state_err;
173354cc54deSWang Nan 		action = PAUSE;
173454cc54deSWang Nan 		break;
173554cc54deSWang Nan 	}
173654cc54deSWang Nan 	case BKW_MMAP_DATA_PENDING: {
173754cc54deSWang Nan 		if (state != BKW_MMAP_EMPTY)
173854cc54deSWang Nan 			goto state_err;
173954cc54deSWang Nan 		break;
174054cc54deSWang Nan 	}
174154cc54deSWang Nan 	case BKW_MMAP_EMPTY: {
174254cc54deSWang Nan 		if (state != BKW_MMAP_RUNNING)
174354cc54deSWang Nan 			goto state_err;
174454cc54deSWang Nan 		action = RESUME;
174554cc54deSWang Nan 		break;
174654cc54deSWang Nan 	}
174754cc54deSWang Nan 	default:
174854cc54deSWang Nan 		WARN_ONCE(1, "Shouldn't get there\n");
174954cc54deSWang Nan 	}
175054cc54deSWang Nan 
175154cc54deSWang Nan 	evlist->bkw_mmap_state = state;
175254cc54deSWang Nan 
175354cc54deSWang Nan 	switch (action) {
175454cc54deSWang Nan 	case PAUSE:
17550a60b339SArnaldo Carvalho de Melo 		evlist__pause(evlist);
175654cc54deSWang Nan 		break;
175754cc54deSWang Nan 	case RESUME:
17580a60b339SArnaldo Carvalho de Melo 		evlist__resume(evlist);
175954cc54deSWang Nan 		break;
176054cc54deSWang Nan 	case NONE:
176154cc54deSWang Nan 	default:
176254cc54deSWang Nan 		break;
176354cc54deSWang Nan 	}
176454cc54deSWang Nan 
176554cc54deSWang Nan state_err:
176654cc54deSWang Nan 	return;
176754cc54deSWang Nan }
176807d6f446SArnaldo Carvalho de Melo 
176978e1bc25SArnaldo Carvalho de Melo bool evlist__exclude_kernel(struct evlist *evlist)
177007d6f446SArnaldo Carvalho de Melo {
177132dcd021SJiri Olsa 	struct evsel *evsel;
177207d6f446SArnaldo Carvalho de Melo 
177307d6f446SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, evsel) {
17741fc632ceSJiri Olsa 		if (!evsel->core.attr.exclude_kernel)
177507d6f446SArnaldo Carvalho de Melo 			return false;
177607d6f446SArnaldo Carvalho de Melo 	}
177707d6f446SArnaldo Carvalho de Melo 
177807d6f446SArnaldo Carvalho de Melo 	return true;
177907d6f446SArnaldo Carvalho de Melo }
1780e2bdbe80SJin Yao 
1781e2bdbe80SJin Yao /*
1782e2bdbe80SJin Yao  * Events in data file are not collect in groups, but we still want
1783e2bdbe80SJin Yao  * the group display. Set the artificial group and set the leader's
1784e2bdbe80SJin Yao  * forced_leader flag to notify the display code.
1785e2bdbe80SJin Yao  */
178664b4778bSArnaldo Carvalho de Melo void evlist__force_leader(struct evlist *evlist)
1787e2bdbe80SJin Yao {
17889d2dc632SIan Rogers 	if (evlist__nr_groups(evlist) == 0) {
1789515dbe48SJiri Olsa 		struct evsel *leader = evlist__first(evlist);
1790e2bdbe80SJin Yao 
1791a622eafaSArnaldo Carvalho de Melo 		evlist__set_leader(evlist);
1792e2bdbe80SJin Yao 		leader->forced_leader = true;
1793e2bdbe80SJin Yao 	}
1794e2bdbe80SJin Yao }
1795c3537fc2SAndi Kleen 
179664b4778bSArnaldo Carvalho de Melo struct evsel *evlist__reset_weak_group(struct evlist *evsel_list, struct evsel *evsel, bool close)
1797c3537fc2SAndi Kleen {
179832dcd021SJiri Olsa 	struct evsel *c2, *leader;
1799c3537fc2SAndi Kleen 	bool is_open = true;
1800c3537fc2SAndi Kleen 
1801fba7c866SJiri Olsa 	leader = evsel__leader(evsel);
1802fba7c866SJiri Olsa 
1803c3537fc2SAndi Kleen 	pr_debug("Weak group for %s/%d failed\n",
18045643b1a5SJiri Olsa 			leader->name, leader->core.nr_members);
1805c3537fc2SAndi Kleen 
1806c3537fc2SAndi Kleen 	/*
1807c3537fc2SAndi Kleen 	 * for_each_group_member doesn't work here because it doesn't
1808c3537fc2SAndi Kleen 	 * include the first entry.
1809c3537fc2SAndi Kleen 	 */
1810c3537fc2SAndi Kleen 	evlist__for_each_entry(evsel_list, c2) {
1811c3537fc2SAndi Kleen 		if (c2 == evsel)
1812c3537fc2SAndi Kleen 			is_open = false;
1813fba7c866SJiri Olsa 		if (evsel__has_leader(c2, leader)) {
18144804e011SAndi Kleen 			if (is_open && close)
18155a40e199SAndi Kleen 				perf_evsel__close(&c2->core);
1816d98079c0SIan Rogers 			/*
1817d98079c0SIan Rogers 			 * We want to close all members of the group and reopen
1818d98079c0SIan Rogers 			 * them. Some events, like Intel topdown, require being
1819d98079c0SIan Rogers 			 * in a group and so keep these in the group.
1820d98079c0SIan Rogers 			 */
1821e8f4f794SKan Liang 			evsel__remove_from_group(c2, leader);
1822d98079c0SIan Rogers 
18234804e011SAndi Kleen 			/*
18244804e011SAndi Kleen 			 * Set this for all former members of the group
18254804e011SAndi Kleen 			 * to indicate they get reopened.
18264804e011SAndi Kleen 			 */
18274804e011SAndi Kleen 			c2->reset_group = true;
1828c3537fc2SAndi Kleen 		}
1829c3537fc2SAndi Kleen 	}
1830d98079c0SIan Rogers 	/* Reset the leader count if all entries were removed. */
1831d98079c0SIan Rogers 	if (leader->core.nr_members == 1)
1832d98079c0SIan Rogers 		leader->core.nr_members = 0;
1833c3537fc2SAndi Kleen 	return leader;
1834c3537fc2SAndi Kleen }
1835ec886bf5SAlexey Budankov 
1836a8fcbd26SAdrian Hunter static int evlist__parse_control_fifo(const char *str, int *ctl_fd, int *ctl_fd_ack, bool *ctl_fd_close)
1837a8fcbd26SAdrian Hunter {
1838a8fcbd26SAdrian Hunter 	char *s, *p;
1839a8fcbd26SAdrian Hunter 	int ret = 0, fd;
1840a8fcbd26SAdrian Hunter 
1841a8fcbd26SAdrian Hunter 	if (strncmp(str, "fifo:", 5))
1842a8fcbd26SAdrian Hunter 		return -EINVAL;
1843a8fcbd26SAdrian Hunter 
1844a8fcbd26SAdrian Hunter 	str += 5;
1845a8fcbd26SAdrian Hunter 	if (!*str || *str == ',')
1846a8fcbd26SAdrian Hunter 		return -EINVAL;
1847a8fcbd26SAdrian Hunter 
1848a8fcbd26SAdrian Hunter 	s = strdup(str);
1849a8fcbd26SAdrian Hunter 	if (!s)
1850a8fcbd26SAdrian Hunter 		return -ENOMEM;
1851a8fcbd26SAdrian Hunter 
1852a8fcbd26SAdrian Hunter 	p = strchr(s, ',');
1853a8fcbd26SAdrian Hunter 	if (p)
1854a8fcbd26SAdrian Hunter 		*p = '\0';
1855a8fcbd26SAdrian Hunter 
1856a8fcbd26SAdrian Hunter 	/*
1857a8fcbd26SAdrian Hunter 	 * O_RDWR avoids POLLHUPs which is necessary to allow the other
1858a8fcbd26SAdrian Hunter 	 * end of a FIFO to be repeatedly opened and closed.
1859a8fcbd26SAdrian Hunter 	 */
1860a8fcbd26SAdrian Hunter 	fd = open(s, O_RDWR | O_NONBLOCK | O_CLOEXEC);
1861a8fcbd26SAdrian Hunter 	if (fd < 0) {
1862a8fcbd26SAdrian Hunter 		pr_err("Failed to open '%s'\n", s);
1863a8fcbd26SAdrian Hunter 		ret = -errno;
1864a8fcbd26SAdrian Hunter 		goto out_free;
1865a8fcbd26SAdrian Hunter 	}
1866a8fcbd26SAdrian Hunter 	*ctl_fd = fd;
1867a8fcbd26SAdrian Hunter 	*ctl_fd_close = true;
1868a8fcbd26SAdrian Hunter 
1869a8fcbd26SAdrian Hunter 	if (p && *++p) {
1870a8fcbd26SAdrian Hunter 		/* O_RDWR | O_NONBLOCK means the other end need not be open */
1871a8fcbd26SAdrian Hunter 		fd = open(p, O_RDWR | O_NONBLOCK | O_CLOEXEC);
1872a8fcbd26SAdrian Hunter 		if (fd < 0) {
1873a8fcbd26SAdrian Hunter 			pr_err("Failed to open '%s'\n", p);
1874a8fcbd26SAdrian Hunter 			ret = -errno;
1875a8fcbd26SAdrian Hunter 			goto out_free;
1876a8fcbd26SAdrian Hunter 		}
1877a8fcbd26SAdrian Hunter 		*ctl_fd_ack = fd;
1878a8fcbd26SAdrian Hunter 	}
1879a8fcbd26SAdrian Hunter 
1880a8fcbd26SAdrian Hunter out_free:
1881a8fcbd26SAdrian Hunter 	free(s);
1882a8fcbd26SAdrian Hunter 	return ret;
1883a8fcbd26SAdrian Hunter }
1884a8fcbd26SAdrian Hunter 
1885a8fcbd26SAdrian Hunter int evlist__parse_control(const char *str, int *ctl_fd, int *ctl_fd_ack, bool *ctl_fd_close)
18869864a66dSAdrian Hunter {
18879864a66dSAdrian Hunter 	char *comma = NULL, *endptr = NULL;
18889864a66dSAdrian Hunter 
1889a8fcbd26SAdrian Hunter 	*ctl_fd_close = false;
1890a8fcbd26SAdrian Hunter 
18919864a66dSAdrian Hunter 	if (strncmp(str, "fd:", 3))
1892a8fcbd26SAdrian Hunter 		return evlist__parse_control_fifo(str, ctl_fd, ctl_fd_ack, ctl_fd_close);
18939864a66dSAdrian Hunter 
18949864a66dSAdrian Hunter 	*ctl_fd = strtoul(&str[3], &endptr, 0);
18959864a66dSAdrian Hunter 	if (endptr == &str[3])
18969864a66dSAdrian Hunter 		return -EINVAL;
18979864a66dSAdrian Hunter 
18989864a66dSAdrian Hunter 	comma = strchr(str, ',');
18999864a66dSAdrian Hunter 	if (comma) {
19009864a66dSAdrian Hunter 		if (endptr != comma)
19019864a66dSAdrian Hunter 			return -EINVAL;
19029864a66dSAdrian Hunter 
19039864a66dSAdrian Hunter 		*ctl_fd_ack = strtoul(comma + 1, &endptr, 0);
19049864a66dSAdrian Hunter 		if (endptr == comma + 1 || *endptr != '\0')
19059864a66dSAdrian Hunter 			return -EINVAL;
19069864a66dSAdrian Hunter 	}
19079864a66dSAdrian Hunter 
19089864a66dSAdrian Hunter 	return 0;
19099864a66dSAdrian Hunter }
19109864a66dSAdrian Hunter 
1911ee7fe31eSAdrian Hunter void evlist__close_control(int ctl_fd, int ctl_fd_ack, bool *ctl_fd_close)
1912ee7fe31eSAdrian Hunter {
1913ee7fe31eSAdrian Hunter 	if (*ctl_fd_close) {
1914ee7fe31eSAdrian Hunter 		*ctl_fd_close = false;
1915ee7fe31eSAdrian Hunter 		close(ctl_fd);
1916ee7fe31eSAdrian Hunter 		if (ctl_fd_ack >= 0)
1917ee7fe31eSAdrian Hunter 			close(ctl_fd_ack);
1918ee7fe31eSAdrian Hunter 	}
1919ee7fe31eSAdrian Hunter }
1920ee7fe31eSAdrian Hunter 
1921ec886bf5SAlexey Budankov int evlist__initialize_ctlfd(struct evlist *evlist, int fd, int ack)
1922ec886bf5SAlexey Budankov {
1923ec886bf5SAlexey Budankov 	if (fd == -1) {
1924ec886bf5SAlexey Budankov 		pr_debug("Control descriptor is not initialized\n");
1925ec886bf5SAlexey Budankov 		return 0;
1926ec886bf5SAlexey Budankov 	}
1927ec886bf5SAlexey Budankov 
1928ec886bf5SAlexey Budankov 	evlist->ctl_fd.pos = perf_evlist__add_pollfd(&evlist->core, fd, NULL, POLLIN,
1929feff0b61SAdrian Hunter 						     fdarray_flag__nonfilterable |
1930feff0b61SAdrian Hunter 						     fdarray_flag__non_perf_event);
1931ec886bf5SAlexey Budankov 	if (evlist->ctl_fd.pos < 0) {
1932ec886bf5SAlexey Budankov 		evlist->ctl_fd.pos = -1;
1933ec886bf5SAlexey Budankov 		pr_err("Failed to add ctl fd entry: %m\n");
1934ec886bf5SAlexey Budankov 		return -1;
1935ec886bf5SAlexey Budankov 	}
1936ec886bf5SAlexey Budankov 
1937ec886bf5SAlexey Budankov 	evlist->ctl_fd.fd = fd;
1938ec886bf5SAlexey Budankov 	evlist->ctl_fd.ack = ack;
1939ec886bf5SAlexey Budankov 
1940ec886bf5SAlexey Budankov 	return 0;
1941ec886bf5SAlexey Budankov }
1942ec886bf5SAlexey Budankov 
1943ec886bf5SAlexey Budankov bool evlist__ctlfd_initialized(struct evlist *evlist)
1944ec886bf5SAlexey Budankov {
1945ec886bf5SAlexey Budankov 	return evlist->ctl_fd.pos >= 0;
1946ec886bf5SAlexey Budankov }
1947ec886bf5SAlexey Budankov 
1948ec886bf5SAlexey Budankov int evlist__finalize_ctlfd(struct evlist *evlist)
1949ec886bf5SAlexey Budankov {
1950ec886bf5SAlexey Budankov 	struct pollfd *entries = evlist->core.pollfd.entries;
1951ec886bf5SAlexey Budankov 
1952ec886bf5SAlexey Budankov 	if (!evlist__ctlfd_initialized(evlist))
1953ec886bf5SAlexey Budankov 		return 0;
1954ec886bf5SAlexey Budankov 
1955ec886bf5SAlexey Budankov 	entries[evlist->ctl_fd.pos].fd = -1;
1956ec886bf5SAlexey Budankov 	entries[evlist->ctl_fd.pos].events = 0;
1957ec886bf5SAlexey Budankov 	entries[evlist->ctl_fd.pos].revents = 0;
1958ec886bf5SAlexey Budankov 
1959ec886bf5SAlexey Budankov 	evlist->ctl_fd.pos = -1;
1960ec886bf5SAlexey Budankov 	evlist->ctl_fd.ack = -1;
1961ec886bf5SAlexey Budankov 	evlist->ctl_fd.fd = -1;
1962ec886bf5SAlexey Budankov 
1963ec886bf5SAlexey Budankov 	return 0;
1964ec886bf5SAlexey Budankov }
1965ec886bf5SAlexey Budankov 
1966ec886bf5SAlexey Budankov static int evlist__ctlfd_recv(struct evlist *evlist, enum evlist_ctl_cmd *cmd,
1967ec886bf5SAlexey Budankov 			      char *cmd_data, size_t data_size)
1968ec886bf5SAlexey Budankov {
1969ec886bf5SAlexey Budankov 	int err;
1970ec886bf5SAlexey Budankov 	char c;
1971ec886bf5SAlexey Budankov 	size_t bytes_read = 0;
1972ec886bf5SAlexey Budankov 
197340db8ff5SAdrian Hunter 	*cmd = EVLIST_CTL_CMD_UNSUPPORTED;
1974ec886bf5SAlexey Budankov 	memset(cmd_data, 0, data_size);
1975ec886bf5SAlexey Budankov 	data_size--;
1976ec886bf5SAlexey Budankov 
1977ec886bf5SAlexey Budankov 	do {
1978ec886bf5SAlexey Budankov 		err = read(evlist->ctl_fd.fd, &c, 1);
1979ec886bf5SAlexey Budankov 		if (err > 0) {
1980ec886bf5SAlexey Budankov 			if (c == '\n' || c == '\0')
1981ec886bf5SAlexey Budankov 				break;
1982ec886bf5SAlexey Budankov 			cmd_data[bytes_read++] = c;
1983ec886bf5SAlexey Budankov 			if (bytes_read == data_size)
1984ec886bf5SAlexey Budankov 				break;
198540db8ff5SAdrian Hunter 			continue;
198640db8ff5SAdrian Hunter 		} else if (err == -1) {
198740db8ff5SAdrian Hunter 			if (errno == EINTR)
198840db8ff5SAdrian Hunter 				continue;
198940db8ff5SAdrian Hunter 			if (errno == EAGAIN || errno == EWOULDBLOCK)
199040db8ff5SAdrian Hunter 				err = 0;
199140db8ff5SAdrian Hunter 			else
1992ec886bf5SAlexey Budankov 				pr_err("Failed to read from ctlfd %d: %m\n", evlist->ctl_fd.fd);
1993ec886bf5SAlexey Budankov 		}
199440db8ff5SAdrian Hunter 		break;
1995ec886bf5SAlexey Budankov 	} while (1);
1996ec886bf5SAlexey Budankov 
1997ec886bf5SAlexey Budankov 	pr_debug("Message from ctl_fd: \"%s%s\"\n", cmd_data,
1998ec886bf5SAlexey Budankov 		 bytes_read == data_size ? "" : c == '\n' ? "\\n" : "\\0");
1999ec886bf5SAlexey Budankov 
200040db8ff5SAdrian Hunter 	if (bytes_read > 0) {
2001ec886bf5SAlexey Budankov 		if (!strncmp(cmd_data, EVLIST_CTL_CMD_ENABLE_TAG,
2002ec886bf5SAlexey Budankov 			     (sizeof(EVLIST_CTL_CMD_ENABLE_TAG)-1))) {
2003ec886bf5SAlexey Budankov 			*cmd = EVLIST_CTL_CMD_ENABLE;
2004ec886bf5SAlexey Budankov 		} else if (!strncmp(cmd_data, EVLIST_CTL_CMD_DISABLE_TAG,
2005ec886bf5SAlexey Budankov 				    (sizeof(EVLIST_CTL_CMD_DISABLE_TAG)-1))) {
2006ec886bf5SAlexey Budankov 			*cmd = EVLIST_CTL_CMD_DISABLE;
2007d20aff15SAdrian Hunter 		} else if (!strncmp(cmd_data, EVLIST_CTL_CMD_SNAPSHOT_TAG,
2008d20aff15SAdrian Hunter 				    (sizeof(EVLIST_CTL_CMD_SNAPSHOT_TAG)-1))) {
2009d20aff15SAdrian Hunter 			*cmd = EVLIST_CTL_CMD_SNAPSHOT;
2010d20aff15SAdrian Hunter 			pr_debug("is snapshot\n");
2011142544a9SJiri Olsa 		} else if (!strncmp(cmd_data, EVLIST_CTL_CMD_EVLIST_TAG,
2012142544a9SJiri Olsa 				    (sizeof(EVLIST_CTL_CMD_EVLIST_TAG)-1))) {
2013142544a9SJiri Olsa 			*cmd = EVLIST_CTL_CMD_EVLIST;
2014f186cd61SJiri Olsa 		} else if (!strncmp(cmd_data, EVLIST_CTL_CMD_STOP_TAG,
2015f186cd61SJiri Olsa 				    (sizeof(EVLIST_CTL_CMD_STOP_TAG)-1))) {
2016f186cd61SJiri Olsa 			*cmd = EVLIST_CTL_CMD_STOP;
201747fddcb4SJiri Olsa 		} else if (!strncmp(cmd_data, EVLIST_CTL_CMD_PING_TAG,
201847fddcb4SJiri Olsa 				    (sizeof(EVLIST_CTL_CMD_PING_TAG)-1))) {
201947fddcb4SJiri Olsa 			*cmd = EVLIST_CTL_CMD_PING;
2020ec886bf5SAlexey Budankov 		}
2021ec886bf5SAlexey Budankov 	}
2022ec886bf5SAlexey Budankov 
202340db8ff5SAdrian Hunter 	return bytes_read ? (int)bytes_read : err;
2024ec886bf5SAlexey Budankov }
2025ec886bf5SAlexey Budankov 
2026d20aff15SAdrian Hunter int evlist__ctlfd_ack(struct evlist *evlist)
2027ec886bf5SAlexey Budankov {
2028ec886bf5SAlexey Budankov 	int err;
2029ec886bf5SAlexey Budankov 
2030ec886bf5SAlexey Budankov 	if (evlist->ctl_fd.ack == -1)
2031ec886bf5SAlexey Budankov 		return 0;
2032ec886bf5SAlexey Budankov 
2033ec886bf5SAlexey Budankov 	err = write(evlist->ctl_fd.ack, EVLIST_CTL_CMD_ACK_TAG,
2034ec886bf5SAlexey Budankov 		    sizeof(EVLIST_CTL_CMD_ACK_TAG));
2035ec886bf5SAlexey Budankov 	if (err == -1)
2036ec886bf5SAlexey Budankov 		pr_err("failed to write to ctl_ack_fd %d: %m\n", evlist->ctl_fd.ack);
2037ec886bf5SAlexey Budankov 
2038ec886bf5SAlexey Budankov 	return err;
2039ec886bf5SAlexey Budankov }
2040ec886bf5SAlexey Budankov 
2041991ae4ebSJiri Olsa static int get_cmd_arg(char *cmd_data, size_t cmd_size, char **arg)
2042991ae4ebSJiri Olsa {
2043991ae4ebSJiri Olsa 	char *data = cmd_data + cmd_size;
2044991ae4ebSJiri Olsa 
2045991ae4ebSJiri Olsa 	/* no argument */
2046991ae4ebSJiri Olsa 	if (!*data)
2047991ae4ebSJiri Olsa 		return 0;
2048991ae4ebSJiri Olsa 
2049991ae4ebSJiri Olsa 	/* there's argument */
2050991ae4ebSJiri Olsa 	if (*data == ' ') {
2051991ae4ebSJiri Olsa 		*arg = data + 1;
2052991ae4ebSJiri Olsa 		return 1;
2053991ae4ebSJiri Olsa 	}
2054991ae4ebSJiri Olsa 
2055991ae4ebSJiri Olsa 	/* malformed */
2056991ae4ebSJiri Olsa 	return -1;
2057991ae4ebSJiri Olsa }
2058991ae4ebSJiri Olsa 
2059991ae4ebSJiri Olsa static int evlist__ctlfd_enable(struct evlist *evlist, char *cmd_data, bool enable)
2060991ae4ebSJiri Olsa {
2061991ae4ebSJiri Olsa 	struct evsel *evsel;
2062991ae4ebSJiri Olsa 	char *name;
2063991ae4ebSJiri Olsa 	int err;
2064991ae4ebSJiri Olsa 
2065991ae4ebSJiri Olsa 	err = get_cmd_arg(cmd_data,
2066991ae4ebSJiri Olsa 			  enable ? sizeof(EVLIST_CTL_CMD_ENABLE_TAG) - 1 :
2067991ae4ebSJiri Olsa 				   sizeof(EVLIST_CTL_CMD_DISABLE_TAG) - 1,
2068991ae4ebSJiri Olsa 			  &name);
2069991ae4ebSJiri Olsa 	if (err < 0) {
2070991ae4ebSJiri Olsa 		pr_info("failed: wrong command\n");
2071991ae4ebSJiri Olsa 		return -1;
2072991ae4ebSJiri Olsa 	}
2073991ae4ebSJiri Olsa 
2074991ae4ebSJiri Olsa 	if (err) {
2075991ae4ebSJiri Olsa 		evsel = evlist__find_evsel_by_str(evlist, name);
2076991ae4ebSJiri Olsa 		if (evsel) {
2077991ae4ebSJiri Olsa 			if (enable)
2078991ae4ebSJiri Olsa 				evlist__enable_evsel(evlist, name);
2079991ae4ebSJiri Olsa 			else
2080991ae4ebSJiri Olsa 				evlist__disable_evsel(evlist, name);
2081991ae4ebSJiri Olsa 			pr_info("Event %s %s\n", evsel->name,
2082991ae4ebSJiri Olsa 				enable ? "enabled" : "disabled");
2083991ae4ebSJiri Olsa 		} else {
2084991ae4ebSJiri Olsa 			pr_info("failed: can't find '%s' event\n", name);
2085991ae4ebSJiri Olsa 		}
2086991ae4ebSJiri Olsa 	} else {
2087991ae4ebSJiri Olsa 		if (enable) {
2088991ae4ebSJiri Olsa 			evlist__enable(evlist);
2089991ae4ebSJiri Olsa 			pr_info(EVLIST_ENABLED_MSG);
2090991ae4ebSJiri Olsa 		} else {
2091991ae4ebSJiri Olsa 			evlist__disable(evlist);
2092991ae4ebSJiri Olsa 			pr_info(EVLIST_DISABLED_MSG);
2093991ae4ebSJiri Olsa 		}
2094991ae4ebSJiri Olsa 	}
2095991ae4ebSJiri Olsa 
2096991ae4ebSJiri Olsa 	return 0;
2097991ae4ebSJiri Olsa }
2098991ae4ebSJiri Olsa 
2099142544a9SJiri Olsa static int evlist__ctlfd_list(struct evlist *evlist, char *cmd_data)
2100142544a9SJiri Olsa {
2101142544a9SJiri Olsa 	struct perf_attr_details details = { .verbose = false, };
2102142544a9SJiri Olsa 	struct evsel *evsel;
2103142544a9SJiri Olsa 	char *arg;
2104142544a9SJiri Olsa 	int err;
2105142544a9SJiri Olsa 
2106142544a9SJiri Olsa 	err = get_cmd_arg(cmd_data,
2107142544a9SJiri Olsa 			  sizeof(EVLIST_CTL_CMD_EVLIST_TAG) - 1,
2108142544a9SJiri Olsa 			  &arg);
2109142544a9SJiri Olsa 	if (err < 0) {
2110142544a9SJiri Olsa 		pr_info("failed: wrong command\n");
2111142544a9SJiri Olsa 		return -1;
2112142544a9SJiri Olsa 	}
2113142544a9SJiri Olsa 
2114142544a9SJiri Olsa 	if (err) {
2115142544a9SJiri Olsa 		if (!strcmp(arg, "-v")) {
2116142544a9SJiri Olsa 			details.verbose = true;
2117142544a9SJiri Olsa 		} else if (!strcmp(arg, "-g")) {
2118142544a9SJiri Olsa 			details.event_group = true;
2119142544a9SJiri Olsa 		} else if (!strcmp(arg, "-F")) {
2120142544a9SJiri Olsa 			details.freq = true;
2121142544a9SJiri Olsa 		} else {
2122142544a9SJiri Olsa 			pr_info("failed: wrong command\n");
2123142544a9SJiri Olsa 			return -1;
2124142544a9SJiri Olsa 		}
2125142544a9SJiri Olsa 	}
2126142544a9SJiri Olsa 
2127142544a9SJiri Olsa 	evlist__for_each_entry(evlist, evsel)
2128142544a9SJiri Olsa 		evsel__fprintf(evsel, &details, stderr);
2129142544a9SJiri Olsa 
2130142544a9SJiri Olsa 	return 0;
2131142544a9SJiri Olsa }
2132142544a9SJiri Olsa 
2133ec886bf5SAlexey Budankov int evlist__ctlfd_process(struct evlist *evlist, enum evlist_ctl_cmd *cmd)
2134ec886bf5SAlexey Budankov {
2135ec886bf5SAlexey Budankov 	int err = 0;
2136ec886bf5SAlexey Budankov 	char cmd_data[EVLIST_CTL_CMD_MAX_LEN];
2137ec886bf5SAlexey Budankov 	int ctlfd_pos = evlist->ctl_fd.pos;
2138ec886bf5SAlexey Budankov 	struct pollfd *entries = evlist->core.pollfd.entries;
2139ec886bf5SAlexey Budankov 
2140ec886bf5SAlexey Budankov 	if (!evlist__ctlfd_initialized(evlist) || !entries[ctlfd_pos].revents)
2141ec886bf5SAlexey Budankov 		return 0;
2142ec886bf5SAlexey Budankov 
2143ec886bf5SAlexey Budankov 	if (entries[ctlfd_pos].revents & POLLIN) {
2144ec886bf5SAlexey Budankov 		err = evlist__ctlfd_recv(evlist, cmd, cmd_data,
2145ec886bf5SAlexey Budankov 					 EVLIST_CTL_CMD_MAX_LEN);
2146ec886bf5SAlexey Budankov 		if (err > 0) {
2147ec886bf5SAlexey Budankov 			switch (*cmd) {
2148ec886bf5SAlexey Budankov 			case EVLIST_CTL_CMD_ENABLE:
2149ec886bf5SAlexey Budankov 			case EVLIST_CTL_CMD_DISABLE:
2150991ae4ebSJiri Olsa 				err = evlist__ctlfd_enable(evlist, cmd_data,
2151991ae4ebSJiri Olsa 							   *cmd == EVLIST_CTL_CMD_ENABLE);
2152ec886bf5SAlexey Budankov 				break;
2153142544a9SJiri Olsa 			case EVLIST_CTL_CMD_EVLIST:
2154142544a9SJiri Olsa 				err = evlist__ctlfd_list(evlist, cmd_data);
2155142544a9SJiri Olsa 				break;
2156d20aff15SAdrian Hunter 			case EVLIST_CTL_CMD_SNAPSHOT:
2157f186cd61SJiri Olsa 			case EVLIST_CTL_CMD_STOP:
215847fddcb4SJiri Olsa 			case EVLIST_CTL_CMD_PING:
2159d20aff15SAdrian Hunter 				break;
2160ec886bf5SAlexey Budankov 			case EVLIST_CTL_CMD_ACK:
2161ec886bf5SAlexey Budankov 			case EVLIST_CTL_CMD_UNSUPPORTED:
2162ec886bf5SAlexey Budankov 			default:
2163ec886bf5SAlexey Budankov 				pr_debug("ctlfd: unsupported %d\n", *cmd);
2164ec886bf5SAlexey Budankov 				break;
2165ec886bf5SAlexey Budankov 			}
2166d20aff15SAdrian Hunter 			if (!(*cmd == EVLIST_CTL_CMD_ACK || *cmd == EVLIST_CTL_CMD_UNSUPPORTED ||
2167d20aff15SAdrian Hunter 			      *cmd == EVLIST_CTL_CMD_SNAPSHOT))
2168ec886bf5SAlexey Budankov 				evlist__ctlfd_ack(evlist);
2169ec886bf5SAlexey Budankov 		}
2170ec886bf5SAlexey Budankov 	}
2171ec886bf5SAlexey Budankov 
2172ec886bf5SAlexey Budankov 	if (entries[ctlfd_pos].revents & (POLLHUP | POLLERR))
2173ec886bf5SAlexey Budankov 		evlist__finalize_ctlfd(evlist);
2174ec886bf5SAlexey Budankov 	else
2175ec886bf5SAlexey Budankov 		entries[ctlfd_pos].revents = 0;
2176ec886bf5SAlexey Budankov 
2177ec886bf5SAlexey Budankov 	return err;
2178ec886bf5SAlexey Budankov }
2179b214ba8cSNamhyung Kim 
21806657a099SAdrian Hunter /**
21816657a099SAdrian Hunter  * struct event_enable_time - perf record -D/--delay single time range.
21826657a099SAdrian Hunter  * @start: start of time range to enable events in milliseconds
21836657a099SAdrian Hunter  * @end: end of time range to enable events in milliseconds
21846657a099SAdrian Hunter  *
21856657a099SAdrian Hunter  * N.B. this structure is also accessed as an array of int.
21866657a099SAdrian Hunter  */
21876657a099SAdrian Hunter struct event_enable_time {
21886657a099SAdrian Hunter 	int	start;
21896657a099SAdrian Hunter 	int	end;
21906657a099SAdrian Hunter };
21916657a099SAdrian Hunter 
21926657a099SAdrian Hunter static int parse_event_enable_time(const char *str, struct event_enable_time *range, bool first)
21936657a099SAdrian Hunter {
21946657a099SAdrian Hunter 	const char *fmt = first ? "%u - %u %n" : " , %u - %u %n";
21956657a099SAdrian Hunter 	int ret, start, end, n;
21966657a099SAdrian Hunter 
21976657a099SAdrian Hunter 	ret = sscanf(str, fmt, &start, &end, &n);
21986657a099SAdrian Hunter 	if (ret != 2 || end <= start)
21996657a099SAdrian Hunter 		return -EINVAL;
22006657a099SAdrian Hunter 	if (range) {
22016657a099SAdrian Hunter 		range->start = start;
22026657a099SAdrian Hunter 		range->end = end;
22036657a099SAdrian Hunter 	}
22046657a099SAdrian Hunter 	return n;
22056657a099SAdrian Hunter }
22066657a099SAdrian Hunter 
22076657a099SAdrian Hunter static ssize_t parse_event_enable_times(const char *str, struct event_enable_time *range)
22086657a099SAdrian Hunter {
22096657a099SAdrian Hunter 	int incr = !!range;
22106657a099SAdrian Hunter 	bool first = true;
22116657a099SAdrian Hunter 	ssize_t ret, cnt;
22126657a099SAdrian Hunter 
22136657a099SAdrian Hunter 	for (cnt = 0; *str; cnt++) {
22146657a099SAdrian Hunter 		ret = parse_event_enable_time(str, range, first);
22156657a099SAdrian Hunter 		if (ret < 0)
22166657a099SAdrian Hunter 			return ret;
22176657a099SAdrian Hunter 		/* Check no overlap */
22186657a099SAdrian Hunter 		if (!first && range && range->start <= range[-1].end)
22196657a099SAdrian Hunter 			return -EINVAL;
22206657a099SAdrian Hunter 		str += ret;
22216657a099SAdrian Hunter 		range += incr;
22226657a099SAdrian Hunter 		first = false;
22236657a099SAdrian Hunter 	}
22246657a099SAdrian Hunter 	return cnt;
22256657a099SAdrian Hunter }
22266657a099SAdrian Hunter 
22276657a099SAdrian Hunter /**
22286657a099SAdrian Hunter  * struct event_enable_timer - control structure for perf record -D/--delay.
22296657a099SAdrian Hunter  * @evlist: event list
22306657a099SAdrian Hunter  * @times: time ranges that events are enabled (N.B. this is also accessed as an
22316657a099SAdrian Hunter  *         array of int)
22326657a099SAdrian Hunter  * @times_cnt: number of time ranges
22336657a099SAdrian Hunter  * @timerfd: timer file descriptor
22346657a099SAdrian Hunter  * @pollfd_pos: position in @evlist array of file descriptors to poll (fdarray)
22356657a099SAdrian Hunter  * @times_step: current position in (int *)@times)[],
22366657a099SAdrian Hunter  *              refer event_enable_timer__process()
22376657a099SAdrian Hunter  *
22386657a099SAdrian Hunter  * Note, this structure is only used when there are time ranges, not when there
22396657a099SAdrian Hunter  * is only an initial delay.
22406657a099SAdrian Hunter  */
22416657a099SAdrian Hunter struct event_enable_timer {
22426657a099SAdrian Hunter 	struct evlist *evlist;
22436657a099SAdrian Hunter 	struct event_enable_time *times;
22446657a099SAdrian Hunter 	size_t	times_cnt;
22456657a099SAdrian Hunter 	int	timerfd;
22466657a099SAdrian Hunter 	int	pollfd_pos;
22476657a099SAdrian Hunter 	size_t	times_step;
22486657a099SAdrian Hunter };
22496657a099SAdrian Hunter 
22506657a099SAdrian Hunter static int str_to_delay(const char *str)
22516657a099SAdrian Hunter {
22526657a099SAdrian Hunter 	char *endptr;
22536657a099SAdrian Hunter 	long d;
22546657a099SAdrian Hunter 
22556657a099SAdrian Hunter 	d = strtol(str, &endptr, 10);
22566657a099SAdrian Hunter 	if (*endptr || d > INT_MAX || d < -1)
22576657a099SAdrian Hunter 		return 0;
22586657a099SAdrian Hunter 	return d;
22596657a099SAdrian Hunter }
22606657a099SAdrian Hunter 
22616657a099SAdrian Hunter int evlist__parse_event_enable_time(struct evlist *evlist, struct record_opts *opts,
22626657a099SAdrian Hunter 				    const char *str, int unset)
22636657a099SAdrian Hunter {
22646657a099SAdrian Hunter 	enum fdarray_flags flags = fdarray_flag__nonfilterable | fdarray_flag__non_perf_event;
22656657a099SAdrian Hunter 	struct event_enable_timer *eet;
22666657a099SAdrian Hunter 	ssize_t times_cnt;
22676657a099SAdrian Hunter 	ssize_t ret;
22686657a099SAdrian Hunter 	int err;
22696657a099SAdrian Hunter 
22706657a099SAdrian Hunter 	if (unset)
22716657a099SAdrian Hunter 		return 0;
22726657a099SAdrian Hunter 
2273cb4b9e68SChangbin Du 	opts->target.initial_delay = str_to_delay(str);
2274cb4b9e68SChangbin Du 	if (opts->target.initial_delay)
22756657a099SAdrian Hunter 		return 0;
22766657a099SAdrian Hunter 
22776657a099SAdrian Hunter 	ret = parse_event_enable_times(str, NULL);
22786657a099SAdrian Hunter 	if (ret < 0)
22796657a099SAdrian Hunter 		return ret;
22806657a099SAdrian Hunter 
22816657a099SAdrian Hunter 	times_cnt = ret;
22826657a099SAdrian Hunter 	if (times_cnt == 0)
22836657a099SAdrian Hunter 		return -EINVAL;
22846657a099SAdrian Hunter 
22856657a099SAdrian Hunter 	eet = zalloc(sizeof(*eet));
22866657a099SAdrian Hunter 	if (!eet)
22876657a099SAdrian Hunter 		return -ENOMEM;
22886657a099SAdrian Hunter 
22896657a099SAdrian Hunter 	eet->times = calloc(times_cnt, sizeof(*eet->times));
22906657a099SAdrian Hunter 	if (!eet->times) {
22916657a099SAdrian Hunter 		err = -ENOMEM;
22926657a099SAdrian Hunter 		goto free_eet;
22936657a099SAdrian Hunter 	}
22946657a099SAdrian Hunter 
22956657a099SAdrian Hunter 	if (parse_event_enable_times(str, eet->times) != times_cnt) {
22966657a099SAdrian Hunter 		err = -EINVAL;
22976657a099SAdrian Hunter 		goto free_eet_times;
22986657a099SAdrian Hunter 	}
22996657a099SAdrian Hunter 
23006657a099SAdrian Hunter 	eet->times_cnt = times_cnt;
23016657a099SAdrian Hunter 
23026657a099SAdrian Hunter 	eet->timerfd = timerfd_create(CLOCK_MONOTONIC, TFD_CLOEXEC);
23036657a099SAdrian Hunter 	if (eet->timerfd == -1) {
23046657a099SAdrian Hunter 		err = -errno;
23056657a099SAdrian Hunter 		pr_err("timerfd_create failed: %s\n", strerror(errno));
23066657a099SAdrian Hunter 		goto free_eet_times;
23076657a099SAdrian Hunter 	}
23086657a099SAdrian Hunter 
23096657a099SAdrian Hunter 	eet->pollfd_pos = perf_evlist__add_pollfd(&evlist->core, eet->timerfd, NULL, POLLIN, flags);
23106657a099SAdrian Hunter 	if (eet->pollfd_pos < 0) {
23116657a099SAdrian Hunter 		err = eet->pollfd_pos;
23126657a099SAdrian Hunter 		goto close_timerfd;
23136657a099SAdrian Hunter 	}
23146657a099SAdrian Hunter 
23156657a099SAdrian Hunter 	eet->evlist = evlist;
23166657a099SAdrian Hunter 	evlist->eet = eet;
2317cb4b9e68SChangbin Du 	opts->target.initial_delay = eet->times[0].start;
23186657a099SAdrian Hunter 
23196657a099SAdrian Hunter 	return 0;
23206657a099SAdrian Hunter 
23216657a099SAdrian Hunter close_timerfd:
23226657a099SAdrian Hunter 	close(eet->timerfd);
23236657a099SAdrian Hunter free_eet_times:
23242bfc8134SArnaldo Carvalho de Melo 	zfree(&eet->times);
23256657a099SAdrian Hunter free_eet:
23266657a099SAdrian Hunter 	free(eet);
23276657a099SAdrian Hunter 	return err;
23286657a099SAdrian Hunter }
23296657a099SAdrian Hunter 
23306657a099SAdrian Hunter static int event_enable_timer__set_timer(struct event_enable_timer *eet, int ms)
23316657a099SAdrian Hunter {
23326657a099SAdrian Hunter 	struct itimerspec its = {
23336657a099SAdrian Hunter 		.it_value.tv_sec = ms / MSEC_PER_SEC,
23346657a099SAdrian Hunter 		.it_value.tv_nsec = (ms % MSEC_PER_SEC) * NSEC_PER_MSEC,
23356657a099SAdrian Hunter 	};
23366657a099SAdrian Hunter 	int err = 0;
23376657a099SAdrian Hunter 
23386657a099SAdrian Hunter 	if (timerfd_settime(eet->timerfd, 0, &its, NULL) < 0) {
23396657a099SAdrian Hunter 		err = -errno;
23406657a099SAdrian Hunter 		pr_err("timerfd_settime failed: %s\n", strerror(errno));
23416657a099SAdrian Hunter 	}
23426657a099SAdrian Hunter 	return err;
23436657a099SAdrian Hunter }
23446657a099SAdrian Hunter 
23456657a099SAdrian Hunter int event_enable_timer__start(struct event_enable_timer *eet)
23466657a099SAdrian Hunter {
23476657a099SAdrian Hunter 	int ms;
23486657a099SAdrian Hunter 
23496657a099SAdrian Hunter 	if (!eet)
23506657a099SAdrian Hunter 		return 0;
23516657a099SAdrian Hunter 
23526657a099SAdrian Hunter 	ms = eet->times[0].end - eet->times[0].start;
23536657a099SAdrian Hunter 	eet->times_step = 1;
23546657a099SAdrian Hunter 
23556657a099SAdrian Hunter 	return event_enable_timer__set_timer(eet, ms);
23566657a099SAdrian Hunter }
23576657a099SAdrian Hunter 
23586657a099SAdrian Hunter int event_enable_timer__process(struct event_enable_timer *eet)
23596657a099SAdrian Hunter {
23606657a099SAdrian Hunter 	struct pollfd *entries;
23616657a099SAdrian Hunter 	short revents;
23626657a099SAdrian Hunter 
23636657a099SAdrian Hunter 	if (!eet)
23646657a099SAdrian Hunter 		return 0;
23656657a099SAdrian Hunter 
23666657a099SAdrian Hunter 	entries = eet->evlist->core.pollfd.entries;
23676657a099SAdrian Hunter 	revents = entries[eet->pollfd_pos].revents;
23686657a099SAdrian Hunter 	entries[eet->pollfd_pos].revents = 0;
23696657a099SAdrian Hunter 
23706657a099SAdrian Hunter 	if (revents & POLLIN) {
23716657a099SAdrian Hunter 		size_t step = eet->times_step;
23726657a099SAdrian Hunter 		size_t pos = step / 2;
23736657a099SAdrian Hunter 
23746657a099SAdrian Hunter 		if (step & 1) {
23756657a099SAdrian Hunter 			evlist__disable_non_dummy(eet->evlist);
23766657a099SAdrian Hunter 			pr_info(EVLIST_DISABLED_MSG);
23776657a099SAdrian Hunter 			if (pos >= eet->times_cnt - 1) {
23786657a099SAdrian Hunter 				/* Disarm timer */
23796657a099SAdrian Hunter 				event_enable_timer__set_timer(eet, 0);
23806657a099SAdrian Hunter 				return 1; /* Stop */
23816657a099SAdrian Hunter 			}
23826657a099SAdrian Hunter 		} else {
23836657a099SAdrian Hunter 			evlist__enable_non_dummy(eet->evlist);
23846657a099SAdrian Hunter 			pr_info(EVLIST_ENABLED_MSG);
23856657a099SAdrian Hunter 		}
23866657a099SAdrian Hunter 
23876657a099SAdrian Hunter 		step += 1;
23886657a099SAdrian Hunter 		pos = step / 2;
23896657a099SAdrian Hunter 
23906657a099SAdrian Hunter 		if (pos < eet->times_cnt) {
23916657a099SAdrian Hunter 			int *times = (int *)eet->times; /* Accessing 'times' as array of int */
23926657a099SAdrian Hunter 			int ms = times[step] - times[step - 1];
23936657a099SAdrian Hunter 
23946657a099SAdrian Hunter 			eet->times_step = step;
23956657a099SAdrian Hunter 			return event_enable_timer__set_timer(eet, ms);
23966657a099SAdrian Hunter 		}
23976657a099SAdrian Hunter 	}
23986657a099SAdrian Hunter 
23996657a099SAdrian Hunter 	return 0;
24006657a099SAdrian Hunter }
24016657a099SAdrian Hunter 
24026657a099SAdrian Hunter void event_enable_timer__exit(struct event_enable_timer **ep)
24036657a099SAdrian Hunter {
24046657a099SAdrian Hunter 	if (!ep || !*ep)
24056657a099SAdrian Hunter 		return;
24062bfc8134SArnaldo Carvalho de Melo 	zfree(&(*ep)->times);
24076657a099SAdrian Hunter 	zfree(ep);
24086657a099SAdrian Hunter }
24096657a099SAdrian Hunter 
2410b214ba8cSNamhyung Kim struct evsel *evlist__find_evsel(struct evlist *evlist, int idx)
2411b214ba8cSNamhyung Kim {
2412b214ba8cSNamhyung Kim 	struct evsel *evsel;
2413b214ba8cSNamhyung Kim 
2414b214ba8cSNamhyung Kim 	evlist__for_each_entry(evlist, evsel) {
241538fe0e01SJiri Olsa 		if (evsel->core.idx == idx)
2416b214ba8cSNamhyung Kim 			return evsel;
2417b214ba8cSNamhyung Kim 	}
2418b214ba8cSNamhyung Kim 	return NULL;
2419b214ba8cSNamhyung Kim }
24209865ea8aSArnaldo Carvalho de Melo 
24219865ea8aSArnaldo Carvalho de Melo int evlist__scnprintf_evsels(struct evlist *evlist, size_t size, char *bf)
24229865ea8aSArnaldo Carvalho de Melo {
24239865ea8aSArnaldo Carvalho de Melo 	struct evsel *evsel;
24249865ea8aSArnaldo Carvalho de Melo 	int printed = 0;
24259865ea8aSArnaldo Carvalho de Melo 
24269865ea8aSArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, evsel) {
24279865ea8aSArnaldo Carvalho de Melo 		if (evsel__is_dummy_event(evsel))
24289865ea8aSArnaldo Carvalho de Melo 			continue;
24299865ea8aSArnaldo Carvalho de Melo 		if (size > (strlen(evsel__name(evsel)) + (printed ? 2 : 1))) {
24309865ea8aSArnaldo Carvalho de Melo 			printed += scnprintf(bf + printed, size - printed, "%s%s", printed ? "," : "", evsel__name(evsel));
24319865ea8aSArnaldo Carvalho de Melo 		} else {
24329865ea8aSArnaldo Carvalho de Melo 			printed += scnprintf(bf + printed, size - printed, "%s...", printed ? "," : "");
24339865ea8aSArnaldo Carvalho de Melo 			break;
24349865ea8aSArnaldo Carvalho de Melo 		}
24359865ea8aSArnaldo Carvalho de Melo 	}
24369865ea8aSArnaldo Carvalho de Melo 
24379865ea8aSArnaldo Carvalho de Melo 	return printed;
24389865ea8aSArnaldo Carvalho de Melo }
2439d5a8bd0fSJin Yao 
2440d5a8bd0fSJin Yao void evlist__check_mem_load_aux(struct evlist *evlist)
2441d5a8bd0fSJin Yao {
2442d5a8bd0fSJin Yao 	struct evsel *leader, *evsel, *pos;
2443d5a8bd0fSJin Yao 
2444d5a8bd0fSJin Yao 	/*
2445d5a8bd0fSJin Yao 	 * For some platforms, the 'mem-loads' event is required to use
2446d5a8bd0fSJin Yao 	 * together with 'mem-loads-aux' within a group and 'mem-loads-aux'
2447d5a8bd0fSJin Yao 	 * must be the group leader. Now we disable this group before reporting
2448d5a8bd0fSJin Yao 	 * because 'mem-loads-aux' is just an auxiliary event. It doesn't carry
2449d5a8bd0fSJin Yao 	 * any valid memory load information.
2450d5a8bd0fSJin Yao 	 */
2451d5a8bd0fSJin Yao 	evlist__for_each_entry(evlist, evsel) {
2452fba7c866SJiri Olsa 		leader = evsel__leader(evsel);
2453d5a8bd0fSJin Yao 		if (leader == evsel)
2454d5a8bd0fSJin Yao 			continue;
2455d5a8bd0fSJin Yao 
2456d5a8bd0fSJin Yao 		if (leader->name && strstr(leader->name, "mem-loads-aux")) {
2457d5a8bd0fSJin Yao 			for_each_group_evsel(pos, leader) {
2458fba7c866SJiri Olsa 				evsel__set_leader(pos, pos);
2459d5a8bd0fSJin Yao 				pos->core.nr_members = 0;
2460d5a8bd0fSJin Yao 			}
2461d5a8bd0fSJin Yao 		}
2462d5a8bd0fSJin Yao 	}
2463d5a8bd0fSJin Yao }
24645ac72634SIan Rogers 
24655ac72634SIan Rogers /**
24665ac72634SIan Rogers  * evlist__warn_user_requested_cpus() - Check each evsel against requested CPUs
24675ac72634SIan Rogers  *     and warn if the user CPU list is inapplicable for the event's PMU's
24685ac72634SIan Rogers  *     CPUs. Not core PMUs list a CPU in sysfs, but this may be overwritten by a
24695ac72634SIan Rogers  *     user requested CPU and so any online CPU is applicable. Core PMUs handle
24705ac72634SIan Rogers  *     events on the CPUs in their list and otherwise the event isn't supported.
24715ac72634SIan Rogers  * @evlist: The list of events being checked.
24725ac72634SIan Rogers  * @cpu_list: The user provided list of CPUs.
24735ac72634SIan Rogers  */
24745ac72634SIan Rogers void evlist__warn_user_requested_cpus(struct evlist *evlist, const char *cpu_list)
24755ac72634SIan Rogers {
24765ac72634SIan Rogers 	struct perf_cpu_map *user_requested_cpus;
24775ac72634SIan Rogers 	struct evsel *pos;
24785ac72634SIan Rogers 
24795ac72634SIan Rogers 	if (!cpu_list)
24805ac72634SIan Rogers 		return;
24815ac72634SIan Rogers 
24825ac72634SIan Rogers 	user_requested_cpus = perf_cpu_map__new(cpu_list);
24835ac72634SIan Rogers 	if (!user_requested_cpus)
24845ac72634SIan Rogers 		return;
24855ac72634SIan Rogers 
24865ac72634SIan Rogers 	evlist__for_each_entry(evlist, pos) {
24875ac72634SIan Rogers 		struct perf_cpu_map *intersect, *to_test;
24885ac72634SIan Rogers 		const struct perf_pmu *pmu = evsel__find_pmu(pos);
24895ac72634SIan Rogers 
24905ac72634SIan Rogers 		to_test = pmu && pmu->is_core ? pmu->cpus : cpu_map__online();
24915ac72634SIan Rogers 		intersect = perf_cpu_map__intersect(to_test, user_requested_cpus);
24925ac72634SIan Rogers 		if (!perf_cpu_map__equal(intersect, user_requested_cpus)) {
24935ac72634SIan Rogers 			char buf[128];
24945ac72634SIan Rogers 
24955ac72634SIan Rogers 			cpu_map__snprint(to_test, buf, sizeof(buf));
24965ac72634SIan Rogers 			pr_warning("WARNING: A requested CPU in '%s' is not supported by PMU '%s' (CPUs %s) for event '%s'\n",
24975ac72634SIan Rogers 				cpu_list, pmu ? pmu->name : "cpu", buf, evsel__name(pos));
24985ac72634SIan Rogers 		}
24995ac72634SIan Rogers 		perf_cpu_map__put(intersect);
25005ac72634SIan Rogers 	}
25015ac72634SIan Rogers 	perf_cpu_map__put(user_requested_cpus);
25025ac72634SIan Rogers }
2503