xref: /openbmc/linux/tools/perf/util/evlist.c (revision 9d2dc632)
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"
31b53a0755SJin Yao #include "util/evlist-hybrid.h"
321d3351e6SJin Yao #include "util/pmu.h"
339823147dSArnaldo Carvalho de Melo #include "util/sample.h"
349607ad3aSArnaldo Carvalho de Melo #include <signal.h>
3535b9d88eSArnaldo Carvalho de Melo #include <unistd.h>
36b397f846SArnaldo Carvalho de Melo #include <sched.h>
37f2a39fe8SArnaldo Carvalho de Melo #include <stdlib.h>
38361c99a6SArnaldo Carvalho de Melo 
3950d08e47SArnaldo Carvalho de Melo #include "parse-events.h"
404b6ab94eSJosh Poimboeuf #include <subcmd/parse-options.h>
4150d08e47SArnaldo Carvalho de Melo 
42bafae98eSArnaldo Carvalho de Melo #include <fcntl.h>
4386a5e0c2SArnaldo Carvalho de Melo #include <sys/ioctl.h>
44f8a95309SArnaldo Carvalho de Melo #include <sys/mman.h>
45a7672d1dSArnaldo Carvalho de Melo #include <sys/prctl.h>
466657a099SAdrian Hunter #include <sys/timerfd.h>
47f8a95309SArnaldo Carvalho de Melo 
4870db7533SArnaldo Carvalho de Melo #include <linux/bitops.h>
4970db7533SArnaldo Carvalho de Melo #include <linux/hash.h>
500389cd1fSArnaldo Carvalho de Melo #include <linux/log2.h>
518dd2a131SJiri Olsa #include <linux/err.h>
528520a98dSArnaldo Carvalho de Melo #include <linux/string.h>
536657a099SAdrian Hunter #include <linux/time64.h>
547f7c536fSArnaldo Carvalho de Melo #include <linux/zalloc.h>
554562a739SJiri Olsa #include <perf/evlist.h>
5688761fa1SJiri Olsa #include <perf/evsel.h>
579c3516d1SJiri Olsa #include <perf/cpumap.h>
587728fa0cSJiri Olsa #include <perf/mmap.h>
5970db7533SArnaldo Carvalho de Melo 
60e14e5497SArnaldo Carvalho de Melo #include <internal/xyarray.h>
61e14e5497SArnaldo Carvalho de Melo 
62748fe088SArnaldo Carvalho de Melo #ifdef LACKS_SIGQUEUE_PROTOTYPE
63748fe088SArnaldo Carvalho de Melo int sigqueue(pid_t pid, int sig, const union sigval value);
64748fe088SArnaldo Carvalho de Melo #endif
65748fe088SArnaldo Carvalho de Melo 
669dfcb759SJiri Olsa #define FD(e, x, y) (*(int *)xyarray__entry(e->core.fd, x, y))
678cd36f3eSJiri Olsa #define SID(e, x, y) xyarray__entry(e->core.sample_id, x, y)
68f8a95309SArnaldo Carvalho de Melo 
6952c86bcaSJiri Olsa void evlist__init(struct evlist *evlist, struct perf_cpu_map *cpus,
709749b90eSJiri Olsa 		  struct perf_thread_map *threads)
71361c99a6SArnaldo Carvalho de Melo {
724562a739SJiri Olsa 	perf_evlist__init(&evlist->core);
73453fa030SJiri Olsa 	perf_evlist__set_maps(&evlist->core, cpus, threads);
7435b9d88eSArnaldo Carvalho de Melo 	evlist->workload.pid = -1;
7554cc54deSWang Nan 	evlist->bkw_mmap_state = BKW_MMAP_NOTREADY;
768ab705b5SAlexey Budankov 	evlist->ctl_fd.fd = -1;
778ab705b5SAlexey Budankov 	evlist->ctl_fd.ack = -1;
788ab705b5SAlexey Budankov 	evlist->ctl_fd.pos = -1;
79361c99a6SArnaldo Carvalho de Melo }
80361c99a6SArnaldo Carvalho de Melo 
810f98b11cSJiri Olsa struct evlist *evlist__new(void)
82ef1d1af2SArnaldo Carvalho de Melo {
8363503dbaSJiri Olsa 	struct evlist *evlist = zalloc(sizeof(*evlist));
84ef1d1af2SArnaldo Carvalho de Melo 
85ef1d1af2SArnaldo Carvalho de Melo 	if (evlist != NULL)
8652c86bcaSJiri Olsa 		evlist__init(evlist, NULL, NULL);
87ef1d1af2SArnaldo Carvalho de Melo 
88361c99a6SArnaldo Carvalho de Melo 	return evlist;
89361c99a6SArnaldo Carvalho de Melo }
90361c99a6SArnaldo Carvalho de Melo 
91606e2c29SArnaldo Carvalho de Melo struct evlist *evlist__new_default(void)
92b22d54b0SJiri Olsa {
930f98b11cSJiri Olsa 	struct evlist *evlist = evlist__new();
94b22d54b0SJiri Olsa 
95e251abeeSArnaldo Carvalho de Melo 	if (evlist && evlist__add_default(evlist)) {
96c12995a5SJiri Olsa 		evlist__delete(evlist);
97b22d54b0SJiri Olsa 		evlist = NULL;
98b22d54b0SJiri Olsa 	}
99b22d54b0SJiri Olsa 
100b22d54b0SJiri Olsa 	return evlist;
101b22d54b0SJiri Olsa }
102b22d54b0SJiri Olsa 
103606e2c29SArnaldo Carvalho de Melo struct evlist *evlist__new_dummy(void)
1045bae0250SArnaldo Carvalho de Melo {
1050f98b11cSJiri Olsa 	struct evlist *evlist = evlist__new();
1065bae0250SArnaldo Carvalho de Melo 
107e251abeeSArnaldo Carvalho de Melo 	if (evlist && evlist__add_dummy(evlist)) {
108c12995a5SJiri Olsa 		evlist__delete(evlist);
1095bae0250SArnaldo Carvalho de Melo 		evlist = NULL;
1105bae0250SArnaldo Carvalho de Melo 	}
1115bae0250SArnaldo Carvalho de Melo 
1125bae0250SArnaldo Carvalho de Melo 	return evlist;
1135bae0250SArnaldo Carvalho de Melo }
1145bae0250SArnaldo Carvalho de Melo 
11575562573SAdrian Hunter /**
1160a7e7ec9SArnaldo Carvalho de Melo  * evlist__set_id_pos - set the positions of event ids.
11775562573SAdrian Hunter  * @evlist: selected event list
11875562573SAdrian Hunter  *
11975562573SAdrian Hunter  * Events with compatible sample types all have the same id_pos
12075562573SAdrian Hunter  * and is_pos.  For convenience, put a copy on evlist.
12175562573SAdrian Hunter  */
1220a7e7ec9SArnaldo Carvalho de Melo void evlist__set_id_pos(struct evlist *evlist)
12375562573SAdrian Hunter {
124515dbe48SJiri Olsa 	struct evsel *first = evlist__first(evlist);
12575562573SAdrian Hunter 
12675562573SAdrian Hunter 	evlist->id_pos = first->id_pos;
12775562573SAdrian Hunter 	evlist->is_pos = first->is_pos;
12875562573SAdrian Hunter }
12975562573SAdrian Hunter 
1300a7e7ec9SArnaldo Carvalho de Melo static void evlist__update_id_pos(struct evlist *evlist)
131733cd2feSAdrian Hunter {
13232dcd021SJiri Olsa 	struct evsel *evsel;
133733cd2feSAdrian Hunter 
134e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, evsel)
1354b5e87b7SArnaldo Carvalho de Melo 		evsel__calc_id_pos(evsel);
136733cd2feSAdrian Hunter 
1370a7e7ec9SArnaldo Carvalho de Melo 	evlist__set_id_pos(evlist);
138733cd2feSAdrian Hunter }
139733cd2feSAdrian Hunter 
140e6b1878dSJiri Olsa static void evlist__purge(struct evlist *evlist)
141361c99a6SArnaldo Carvalho de Melo {
14232dcd021SJiri Olsa 	struct evsel *pos, *n;
143361c99a6SArnaldo Carvalho de Melo 
144e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry_safe(evlist, n, pos) {
145b27c4eceSJiri Olsa 		list_del_init(&pos->core.node);
146d49e4695SArnaldo Carvalho de Melo 		pos->evlist = NULL;
1475eb2dd2aSJiri Olsa 		evsel__delete(pos);
148361c99a6SArnaldo Carvalho de Melo 	}
149361c99a6SArnaldo Carvalho de Melo 
1506484d2f9SJiri Olsa 	evlist->core.nr_entries = 0;
151361c99a6SArnaldo Carvalho de Melo }
152361c99a6SArnaldo Carvalho de Melo 
153470579b0SJiri Olsa void evlist__exit(struct evlist *evlist)
154ef1d1af2SArnaldo Carvalho de Melo {
1556657a099SAdrian Hunter 	event_enable_timer__exit(&evlist->eet);
15604662523SArnaldo Carvalho de Melo 	zfree(&evlist->mmap);
1570b72d69aSWang Nan 	zfree(&evlist->overwrite_mmap);
15893dd6e28SJiri Olsa 	perf_evlist__exit(&evlist->core);
159ef1d1af2SArnaldo Carvalho de Melo }
160ef1d1af2SArnaldo Carvalho de Melo 
161c12995a5SJiri Olsa void evlist__delete(struct evlist *evlist)
162361c99a6SArnaldo Carvalho de Melo {
1630b04b3dcSArnaldo Carvalho de Melo 	if (evlist == NULL)
1640b04b3dcSArnaldo Carvalho de Melo 		return;
1650b04b3dcSArnaldo Carvalho de Melo 
166db6b7b13SJiri Olsa 	evlist__munmap(evlist);
167750b4edeSJiri Olsa 	evlist__close(evlist);
168e6b1878dSJiri Olsa 	evlist__purge(evlist);
169470579b0SJiri Olsa 	evlist__exit(evlist);
170361c99a6SArnaldo Carvalho de Melo 	free(evlist);
171361c99a6SArnaldo Carvalho de Melo }
172361c99a6SArnaldo Carvalho de Melo 
173a1cf3a75SJiri Olsa void evlist__add(struct evlist *evlist, struct evsel *entry)
174361c99a6SArnaldo Carvalho de Melo {
1756484d2f9SJiri Olsa 	perf_evlist__add(&evlist->core, &entry->core);
17638fe0e01SJiri Olsa 	entry->evlist = evlist;
17738fe0e01SJiri Olsa 	entry->tracking = !entry->core.idx;
1786484d2f9SJiri Olsa 
1796484d2f9SJiri Olsa 	if (evlist->core.nr_entries == 1)
1800a7e7ec9SArnaldo Carvalho de Melo 		evlist__set_id_pos(evlist);
181361c99a6SArnaldo Carvalho de Melo }
182361c99a6SArnaldo Carvalho de Melo 
18316251027SJiri Olsa void evlist__remove(struct evlist *evlist, struct evsel *evsel)
1844768230aSAdrian Hunter {
1854768230aSAdrian Hunter 	evsel->evlist = NULL;
18652e22fb8SJiri Olsa 	perf_evlist__remove(&evlist->core, &evsel->core);
1874768230aSAdrian Hunter }
1884768230aSAdrian Hunter 
189e414fd1aSArnaldo Carvalho de Melo void evlist__splice_list_tail(struct evlist *evlist, struct list_head *list)
19050d08e47SArnaldo Carvalho de Melo {
1916d2783feSJohn Garry 	while (!list_empty(list)) {
1926d2783feSJohn Garry 		struct evsel *evsel, *temp, *leader = NULL;
19375562573SAdrian Hunter 
194e5cadb93SArnaldo Carvalho de Melo 		__evlist__for_each_entry_safe(list, temp, evsel) {
195b27c4eceSJiri Olsa 			list_del_init(&evsel->core.node);
196a1cf3a75SJiri Olsa 			evlist__add(evlist, evsel);
1976d2783feSJohn Garry 			leader = evsel;
1986d2783feSJohn Garry 			break;
1996d2783feSJohn Garry 		}
2006d2783feSJohn Garry 
2016d2783feSJohn Garry 		__evlist__for_each_entry_safe(list, temp, evsel) {
202fba7c866SJiri Olsa 			if (evsel__has_leader(evsel, leader)) {
2036d2783feSJohn Garry 				list_del_init(&evsel->core.node);
2046d2783feSJohn Garry 				evlist__add(evlist, evsel);
2056d2783feSJohn Garry 			}
2066d2783feSJohn Garry 		}
207f114d6efSAdrian Hunter 	}
20850d08e47SArnaldo Carvalho de Melo }
20950d08e47SArnaldo Carvalho de Melo 
210c0e53476SArnaldo Carvalho de Melo int __evlist__set_tracepoints_handlers(struct evlist *evlist,
211c0e53476SArnaldo Carvalho de Melo 				       const struct evsel_str_handler *assocs, size_t nr_assocs)
212c0e53476SArnaldo Carvalho de Melo {
213c0e53476SArnaldo Carvalho de Melo 	size_t i;
214c0e53476SArnaldo Carvalho de Melo 	int err;
215c0e53476SArnaldo Carvalho de Melo 
216c0e53476SArnaldo Carvalho de Melo 	for (i = 0; i < nr_assocs; i++) {
217c0e53476SArnaldo Carvalho de Melo 		// Adding a handler for an event not in this evlist, just ignore it.
218b02736f7SArnaldo Carvalho de Melo 		struct evsel *evsel = evlist__find_tracepoint_by_name(evlist, assocs[i].name);
219c0e53476SArnaldo Carvalho de Melo 		if (evsel == NULL)
220c0e53476SArnaldo Carvalho de Melo 			continue;
221c0e53476SArnaldo Carvalho de Melo 
222c0e53476SArnaldo Carvalho de Melo 		err = -EEXIST;
223c0e53476SArnaldo Carvalho de Melo 		if (evsel->handler != NULL)
224c0e53476SArnaldo Carvalho de Melo 			goto out;
225c0e53476SArnaldo Carvalho de Melo 		evsel->handler = assocs[i].handler;
226c0e53476SArnaldo Carvalho de Melo 	}
227c0e53476SArnaldo Carvalho de Melo 
228c0e53476SArnaldo Carvalho de Melo 	err = 0;
229c0e53476SArnaldo Carvalho de Melo out:
230c0e53476SArnaldo Carvalho de Melo 	return err;
231c0e53476SArnaldo Carvalho de Melo }
232c0e53476SArnaldo Carvalho de Melo 
2335f8f9567SIan Rogers static void evlist__set_leader(struct evlist *evlist)
2346a4bb04cSJiri Olsa {
2352e6263abSJiri Olsa 	perf_evlist__set_leader(&evlist->core);
23697f63e4aSNamhyung Kim }
2376a4bb04cSJiri Olsa 
238e251abeeSArnaldo Carvalho de Melo int __evlist__add_default(struct evlist *evlist, bool precise)
239361c99a6SArnaldo Carvalho de Melo {
240b53a0755SJin Yao 	struct evsel *evsel;
241361c99a6SArnaldo Carvalho de Melo 
242b53a0755SJin Yao 	evsel = evsel__new_cycles(precise, PERF_TYPE_HARDWARE,
243b53a0755SJin Yao 				  PERF_COUNT_HW_CPU_CYCLES);
244361c99a6SArnaldo Carvalho de Melo 	if (evsel == NULL)
2457c48dcfdSArnaldo Carvalho de Melo 		return -ENOMEM;
246361c99a6SArnaldo Carvalho de Melo 
247a1cf3a75SJiri Olsa 	evlist__add(evlist, evsel);
248361c99a6SArnaldo Carvalho de Melo 	return 0;
249361c99a6SArnaldo Carvalho de Melo }
2505c581041SArnaldo Carvalho de Melo 
25182944899SAdrian Hunter static struct evsel *evlist__dummy_event(struct evlist *evlist)
2525bae0250SArnaldo Carvalho de Melo {
2535bae0250SArnaldo Carvalho de Melo 	struct perf_event_attr attr = {
2545bae0250SArnaldo Carvalho de Melo 		.type	= PERF_TYPE_SOFTWARE,
2555bae0250SArnaldo Carvalho de Melo 		.config = PERF_COUNT_SW_DUMMY,
2565bae0250SArnaldo Carvalho de Melo 		.size	= sizeof(attr), /* to capture ABI version */
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;
283126d68fdSAdrian Hunter 	evsel->core.attr.freq = 0;
284126d68fdSAdrian Hunter 	evsel->core.attr.sample_period = 1;
28560ea006fSNamhyung Kim 	evsel->core.system_wide = system_wide;
286126d68fdSAdrian Hunter 	evsel->no_aux_samples = true;
287126d68fdSAdrian Hunter 	evsel->name = strdup("dummy:u");
288126d68fdSAdrian Hunter 
289126d68fdSAdrian Hunter 	evlist__add(evlist, evsel);
290126d68fdSAdrian Hunter 	return evsel;
291126d68fdSAdrian Hunter }
292126d68fdSAdrian Hunter 
293378ef0f5SIan Rogers #ifdef HAVE_LIBTRACEEVENT
294182bb594SNamhyung Kim struct evsel *evlist__add_sched_switch(struct evlist *evlist, bool system_wide)
295182bb594SNamhyung Kim {
296182bb594SNamhyung Kim 	struct evsel *evsel = evsel__newtp_idx("sched", "sched_switch", 0);
297182bb594SNamhyung Kim 
298182bb594SNamhyung Kim 	if (IS_ERR(evsel))
299182bb594SNamhyung Kim 		return evsel;
300182bb594SNamhyung Kim 
301182bb594SNamhyung Kim 	evsel__set_sample_bit(evsel, CPU);
302182bb594SNamhyung Kim 	evsel__set_sample_bit(evsel, TIME);
303182bb594SNamhyung Kim 
304182bb594SNamhyung Kim 	evsel->core.system_wide = system_wide;
305182bb594SNamhyung Kim 	evsel->no_aux_samples = true;
306182bb594SNamhyung Kim 
307182bb594SNamhyung Kim 	evlist__add(evlist, evsel);
308182bb594SNamhyung Kim 	return evsel;
309378ef0f5SIan Rogers }
310378ef0f5SIan Rogers #endif
311182bb594SNamhyung Kim 
312cdb204adSKan Liang int evlist__add_attrs(struct evlist *evlist, struct perf_event_attr *attrs, size_t nr_attrs)
31350d08e47SArnaldo Carvalho de Melo {
31432dcd021SJiri Olsa 	struct evsel *evsel, *n;
31550d08e47SArnaldo Carvalho de Melo 	LIST_HEAD(head);
31650d08e47SArnaldo Carvalho de Melo 	size_t i;
31750d08e47SArnaldo Carvalho de Melo 
31850d08e47SArnaldo Carvalho de Melo 	for (i = 0; i < nr_attrs; i++) {
3198f6725a2SArnaldo Carvalho de Melo 		evsel = evsel__new_idx(attrs + i, evlist->core.nr_entries + i);
32050d08e47SArnaldo Carvalho de Melo 		if (evsel == NULL)
32150d08e47SArnaldo Carvalho de Melo 			goto out_delete_partial_list;
322b27c4eceSJiri Olsa 		list_add_tail(&evsel->core.node, &head);
32350d08e47SArnaldo Carvalho de Melo 	}
32450d08e47SArnaldo Carvalho de Melo 
325e414fd1aSArnaldo Carvalho de Melo 	evlist__splice_list_tail(evlist, &head);
32650d08e47SArnaldo Carvalho de Melo 
32750d08e47SArnaldo Carvalho de Melo 	return 0;
32850d08e47SArnaldo Carvalho de Melo 
32950d08e47SArnaldo Carvalho de Melo out_delete_partial_list:
330e5cadb93SArnaldo Carvalho de Melo 	__evlist__for_each_entry_safe(&head, n, evsel)
3315eb2dd2aSJiri Olsa 		evsel__delete(evsel);
33250d08e47SArnaldo Carvalho de Melo 	return -1;
33350d08e47SArnaldo Carvalho de Melo }
33450d08e47SArnaldo Carvalho de Melo 
335e251abeeSArnaldo Carvalho de Melo int __evlist__add_default_attrs(struct evlist *evlist, struct perf_event_attr *attrs, size_t nr_attrs)
33679695e1bSArnaldo Carvalho de Melo {
33779695e1bSArnaldo Carvalho de Melo 	size_t i;
33879695e1bSArnaldo Carvalho de Melo 
33979695e1bSArnaldo Carvalho de Melo 	for (i = 0; i < nr_attrs; i++)
34079695e1bSArnaldo Carvalho de Melo 		event_attr_init(attrs + i);
34179695e1bSArnaldo Carvalho de Melo 
342a1cf3a75SJiri Olsa 	return evlist__add_attrs(evlist, attrs, nr_attrs);
34379695e1bSArnaldo Carvalho de Melo }
34479695e1bSArnaldo Carvalho de Melo 
345a9c1ecdaSKan Liang __weak int arch_evlist__add_default_attrs(struct evlist *evlist,
346a9c1ecdaSKan Liang 					  struct perf_event_attr *attrs,
347a9c1ecdaSKan Liang 					  size_t nr_attrs)
34842641d6fSKan Liang {
349a9c1ecdaSKan Liang 	if (!nr_attrs)
35042641d6fSKan Liang 		return 0;
351a9c1ecdaSKan Liang 
352a9c1ecdaSKan Liang 	return __evlist__add_default_attrs(evlist, attrs, nr_attrs);
35342641d6fSKan Liang }
35442641d6fSKan Liang 
355b02736f7SArnaldo Carvalho de Melo struct evsel *evlist__find_tracepoint_by_id(struct evlist *evlist, int id)
356ee29be62SArnaldo Carvalho de Melo {
35732dcd021SJiri Olsa 	struct evsel *evsel;
358ee29be62SArnaldo Carvalho de Melo 
359e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, evsel) {
3601fc632ceSJiri Olsa 		if (evsel->core.attr.type   == PERF_TYPE_TRACEPOINT &&
3611fc632ceSJiri Olsa 		    (int)evsel->core.attr.config == id)
362ee29be62SArnaldo Carvalho de Melo 			return evsel;
363ee29be62SArnaldo Carvalho de Melo 	}
364ee29be62SArnaldo Carvalho de Melo 
365ee29be62SArnaldo Carvalho de Melo 	return NULL;
366ee29be62SArnaldo Carvalho de Melo }
367ee29be62SArnaldo Carvalho de Melo 
368b02736f7SArnaldo Carvalho de Melo struct evsel *evlist__find_tracepoint_by_name(struct evlist *evlist, const char *name)
369a2f2804aSDavid Ahern {
37032dcd021SJiri Olsa 	struct evsel *evsel;
371a2f2804aSDavid Ahern 
372e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, evsel) {
3731fc632ceSJiri Olsa 		if ((evsel->core.attr.type == PERF_TYPE_TRACEPOINT) &&
374a2f2804aSDavid Ahern 		    (strcmp(evsel->name, name) == 0))
375a2f2804aSDavid Ahern 			return evsel;
376a2f2804aSDavid Ahern 	}
377a2f2804aSDavid Ahern 
378a2f2804aSDavid Ahern 	return NULL;
379a2f2804aSDavid Ahern }
380a2f2804aSDavid Ahern 
381378ef0f5SIan Rogers #ifdef HAVE_LIBTRACEEVENT
382e251abeeSArnaldo Carvalho de Melo int evlist__add_newtp(struct evlist *evlist, const char *sys, const char *name, void *handler)
38339876e7dSArnaldo Carvalho de Melo {
3848f6725a2SArnaldo Carvalho de Melo 	struct evsel *evsel = evsel__newtp(sys, name);
38539876e7dSArnaldo Carvalho de Melo 
3868dd2a131SJiri Olsa 	if (IS_ERR(evsel))
38739876e7dSArnaldo Carvalho de Melo 		return -1;
38839876e7dSArnaldo Carvalho de Melo 
389744a9719SArnaldo Carvalho de Melo 	evsel->handler = handler;
390a1cf3a75SJiri Olsa 	evlist__add(evlist, evsel);
39139876e7dSArnaldo Carvalho de Melo 	return 0;
39239876e7dSArnaldo Carvalho de Melo }
393378ef0f5SIan Rogers #endif
39439876e7dSArnaldo Carvalho de Melo 
395472832d2SIan Rogers struct evlist_cpu_iterator evlist__cpu_begin(struct evlist *evlist, struct affinity *affinity)
396a8cbe40fSAndi Kleen {
397472832d2SIan Rogers 	struct evlist_cpu_iterator itr = {
398472832d2SIan Rogers 		.container = evlist,
3998b464eacSIan Rogers 		.evsel = NULL,
400472832d2SIan Rogers 		.cpu_map_idx = 0,
401472832d2SIan Rogers 		.evlist_cpu_map_idx = 0,
402472832d2SIan Rogers 		.evlist_cpu_map_nr = perf_cpu_map__nr(evlist->core.all_cpus),
4036d18804bSIan Rogers 		.cpu = (struct perf_cpu){ .cpu = -1},
404472832d2SIan Rogers 		.affinity = affinity,
405472832d2SIan Rogers 	};
406a8cbe40fSAndi Kleen 
4078b464eacSIan Rogers 	if (evlist__empty(evlist)) {
4088b464eacSIan Rogers 		/* Ensure the empty list doesn't iterate. */
4098b464eacSIan Rogers 		itr.evlist_cpu_map_idx = itr.evlist_cpu_map_nr;
4108b464eacSIan Rogers 	} else {
4118b464eacSIan Rogers 		itr.evsel = evlist__first(evlist);
412472832d2SIan Rogers 		if (itr.affinity) {
413472832d2SIan Rogers 			itr.cpu = perf_cpu_map__cpu(evlist->core.all_cpus, 0);
4146d18804bSIan Rogers 			affinity__set(itr.affinity, itr.cpu.cpu);
415472832d2SIan Rogers 			itr.cpu_map_idx = perf_cpu_map__idx(itr.evsel->core.cpus, itr.cpu);
416a8cbe40fSAndi Kleen 			/*
4178b464eacSIan Rogers 			 * If this CPU isn't in the evsel's cpu map then advance
4188b464eacSIan Rogers 			 * through the list.
419a8cbe40fSAndi Kleen 			 */
420472832d2SIan Rogers 			if (itr.cpu_map_idx == -1)
421472832d2SIan Rogers 				evlist_cpu_iterator__next(&itr);
422472832d2SIan Rogers 		}
4238b464eacSIan Rogers 	}
424472832d2SIan Rogers 	return itr;
425a8cbe40fSAndi Kleen }
426a8cbe40fSAndi Kleen 
427472832d2SIan Rogers void evlist_cpu_iterator__next(struct evlist_cpu_iterator *evlist_cpu_itr)
428a8cbe40fSAndi Kleen {
429472832d2SIan Rogers 	while (evlist_cpu_itr->evsel != evlist__last(evlist_cpu_itr->container)) {
430472832d2SIan Rogers 		evlist_cpu_itr->evsel = evsel__next(evlist_cpu_itr->evsel);
431472832d2SIan Rogers 		evlist_cpu_itr->cpu_map_idx =
432472832d2SIan Rogers 			perf_cpu_map__idx(evlist_cpu_itr->evsel->core.cpus,
433472832d2SIan Rogers 					  evlist_cpu_itr->cpu);
434472832d2SIan Rogers 		if (evlist_cpu_itr->cpu_map_idx != -1)
435472832d2SIan Rogers 			return;
436472832d2SIan Rogers 	}
437472832d2SIan Rogers 	evlist_cpu_itr->evlist_cpu_map_idx++;
438472832d2SIan Rogers 	if (evlist_cpu_itr->evlist_cpu_map_idx < evlist_cpu_itr->evlist_cpu_map_nr) {
439472832d2SIan Rogers 		evlist_cpu_itr->evsel = evlist__first(evlist_cpu_itr->container);
440472832d2SIan Rogers 		evlist_cpu_itr->cpu =
441472832d2SIan Rogers 			perf_cpu_map__cpu(evlist_cpu_itr->container->core.all_cpus,
442472832d2SIan Rogers 					  evlist_cpu_itr->evlist_cpu_map_idx);
443472832d2SIan Rogers 		if (evlist_cpu_itr->affinity)
4446d18804bSIan Rogers 			affinity__set(evlist_cpu_itr->affinity, evlist_cpu_itr->cpu.cpu);
445472832d2SIan Rogers 		evlist_cpu_itr->cpu_map_idx =
446472832d2SIan Rogers 			perf_cpu_map__idx(evlist_cpu_itr->evsel->core.cpus,
447472832d2SIan Rogers 					  evlist_cpu_itr->cpu);
448472832d2SIan Rogers 		/*
449472832d2SIan Rogers 		 * If this CPU isn't in the evsel's cpu map then advance through
450472832d2SIan Rogers 		 * the list.
451472832d2SIan Rogers 		 */
452472832d2SIan Rogers 		if (evlist_cpu_itr->cpu_map_idx == -1)
453472832d2SIan Rogers 			evlist_cpu_iterator__next(evlist_cpu_itr);
454472832d2SIan Rogers 	}
455a8cbe40fSAndi Kleen }
456a8cbe40fSAndi Kleen 
457472832d2SIan Rogers bool evlist_cpu_iterator__end(const struct evlist_cpu_iterator *evlist_cpu_itr)
458a8cbe40fSAndi Kleen {
459472832d2SIan Rogers 	return evlist_cpu_itr->evlist_cpu_map_idx >= evlist_cpu_itr->evlist_cpu_map_nr;
460a8cbe40fSAndi Kleen }
461a8cbe40fSAndi Kleen 
4627cfcd1e0SJiri Olsa static int evsel__strcmp(struct evsel *pos, char *evsel_name)
4637cfcd1e0SJiri Olsa {
4647cfcd1e0SJiri Olsa 	if (!evsel_name)
4657cfcd1e0SJiri Olsa 		return 0;
4667cfcd1e0SJiri Olsa 	if (evsel__is_dummy_event(pos))
4677cfcd1e0SJiri Olsa 		return 1;
4687cfcd1e0SJiri Olsa 	return strcmp(pos->name, evsel_name);
4697cfcd1e0SJiri Olsa }
4707cfcd1e0SJiri Olsa 
4717cfcd1e0SJiri Olsa static int evlist__is_enabled(struct evlist *evlist)
4727cfcd1e0SJiri Olsa {
4737cfcd1e0SJiri Olsa 	struct evsel *pos;
4747cfcd1e0SJiri Olsa 
4757cfcd1e0SJiri Olsa 	evlist__for_each_entry(evlist, pos) {
4767cfcd1e0SJiri Olsa 		if (!evsel__is_group_leader(pos) || !pos->core.fd)
4777cfcd1e0SJiri Olsa 			continue;
4787cfcd1e0SJiri Olsa 		/* If at least one event is enabled, evlist is enabled. */
4797cfcd1e0SJiri Olsa 		if (!pos->disabled)
4807cfcd1e0SJiri Olsa 			return true;
4817cfcd1e0SJiri Olsa 	}
4827cfcd1e0SJiri Olsa 	return false;
4837cfcd1e0SJiri Olsa }
4847cfcd1e0SJiri Olsa 
485329725d5SAdrian Hunter static void __evlist__disable(struct evlist *evlist, char *evsel_name, bool excl_dummy)
4864152ab37SArnaldo Carvalho de Melo {
48732dcd021SJiri Olsa 	struct evsel *pos;
488472832d2SIan Rogers 	struct evlist_cpu_iterator evlist_cpu_itr;
4890d3d2376SArnaldo Carvalho de Melo 	struct affinity saved_affinity, *affinity = NULL;
49087cf8360SAdrian Hunter 	bool has_imm = false;
491704e2f5bSAndi Kleen 
4920d3d2376SArnaldo Carvalho de Melo 	// See explanation in evlist__close()
4930df6ade7SIan Rogers 	if (!cpu_map__is_dummy(evlist->core.user_requested_cpus)) {
4940d3d2376SArnaldo Carvalho de Melo 		if (affinity__setup(&saved_affinity) < 0)
495704e2f5bSAndi Kleen 			return;
4960d3d2376SArnaldo Carvalho de Melo 		affinity = &saved_affinity;
4970d3d2376SArnaldo Carvalho de Melo 	}
498704e2f5bSAndi Kleen 
49987cf8360SAdrian Hunter 	/* Disable 'immediate' events last */
500472832d2SIan Rogers 	for (int imm = 0; imm <= 1; imm++) {
5010d3d2376SArnaldo Carvalho de Melo 		evlist__for_each_cpu(evlist_cpu_itr, evlist, affinity) {
502472832d2SIan Rogers 			pos = evlist_cpu_itr.evsel;
5037cfcd1e0SJiri Olsa 			if (evsel__strcmp(pos, evsel_name))
5047cfcd1e0SJiri Olsa 				continue;
505c754c382SArnaldo Carvalho de Melo 			if (pos->disabled || !evsel__is_group_leader(pos) || !pos->core.fd)
5063fe4430dSJiri Olsa 				continue;
507329725d5SAdrian Hunter 			if (excl_dummy && evsel__is_dummy_event(pos))
508329725d5SAdrian Hunter 				continue;
50987cf8360SAdrian Hunter 			if (pos->immediate)
51087cf8360SAdrian Hunter 				has_imm = true;
51187cf8360SAdrian Hunter 			if (pos->immediate != imm)
51287cf8360SAdrian Hunter 				continue;
513472832d2SIan Rogers 			evsel__disable_cpu(pos, evlist_cpu_itr.cpu_map_idx);
514704e2f5bSAndi Kleen 		}
51587cf8360SAdrian Hunter 		if (!has_imm)
51687cf8360SAdrian Hunter 			break;
51787cf8360SAdrian Hunter 	}
51887cf8360SAdrian Hunter 
5190d3d2376SArnaldo Carvalho de Melo 	affinity__cleanup(affinity);
520704e2f5bSAndi Kleen 	evlist__for_each_entry(evlist, pos) {
5217cfcd1e0SJiri Olsa 		if (evsel__strcmp(pos, evsel_name))
5227cfcd1e0SJiri Olsa 			continue;
523c754c382SArnaldo Carvalho de Melo 		if (!evsel__is_group_leader(pos) || !pos->core.fd)
524704e2f5bSAndi Kleen 			continue;
525329725d5SAdrian Hunter 		if (excl_dummy && evsel__is_dummy_event(pos))
526329725d5SAdrian Hunter 			continue;
527704e2f5bSAndi Kleen 		pos->disabled = true;
5284152ab37SArnaldo Carvalho de Melo 	}
5292b56bcfbSArnaldo Carvalho de Melo 
5307cfcd1e0SJiri Olsa 	/*
5317cfcd1e0SJiri Olsa 	 * If we disabled only single event, we need to check
5327cfcd1e0SJiri Olsa 	 * the enabled state of the evlist manually.
5337cfcd1e0SJiri Olsa 	 */
5347cfcd1e0SJiri Olsa 	if (evsel_name)
5357cfcd1e0SJiri Olsa 		evlist->enabled = evlist__is_enabled(evlist);
5367cfcd1e0SJiri Olsa 	else
5372b56bcfbSArnaldo Carvalho de Melo 		evlist->enabled = false;
5384152ab37SArnaldo Carvalho de Melo }
5394152ab37SArnaldo Carvalho de Melo 
5407cfcd1e0SJiri Olsa void evlist__disable(struct evlist *evlist)
5417cfcd1e0SJiri Olsa {
542329725d5SAdrian Hunter 	__evlist__disable(evlist, NULL, false);
543329725d5SAdrian Hunter }
544329725d5SAdrian Hunter 
545329725d5SAdrian Hunter void evlist__disable_non_dummy(struct evlist *evlist)
546329725d5SAdrian Hunter {
547329725d5SAdrian Hunter 	__evlist__disable(evlist, NULL, true);
5487cfcd1e0SJiri Olsa }
5497cfcd1e0SJiri Olsa 
5507cfcd1e0SJiri Olsa void evlist__disable_evsel(struct evlist *evlist, char *evsel_name)
5517cfcd1e0SJiri Olsa {
552329725d5SAdrian Hunter 	__evlist__disable(evlist, evsel_name, false);
5537cfcd1e0SJiri Olsa }
5547cfcd1e0SJiri Olsa 
555329725d5SAdrian Hunter static void __evlist__enable(struct evlist *evlist, char *evsel_name, bool excl_dummy)
556764e16a3SDavid Ahern {
55732dcd021SJiri Olsa 	struct evsel *pos;
558472832d2SIan Rogers 	struct evlist_cpu_iterator evlist_cpu_itr;
559f350ee95SArnaldo Carvalho de Melo 	struct affinity saved_affinity, *affinity = NULL;
560764e16a3SDavid Ahern 
561f350ee95SArnaldo Carvalho de Melo 	// See explanation in evlist__close()
5620df6ade7SIan Rogers 	if (!cpu_map__is_dummy(evlist->core.user_requested_cpus)) {
563f350ee95SArnaldo Carvalho de Melo 		if (affinity__setup(&saved_affinity) < 0)
564704e2f5bSAndi Kleen 			return;
565f350ee95SArnaldo Carvalho de Melo 		affinity = &saved_affinity;
566f350ee95SArnaldo Carvalho de Melo 	}
567704e2f5bSAndi Kleen 
568f350ee95SArnaldo Carvalho de Melo 	evlist__for_each_cpu(evlist_cpu_itr, evlist, affinity) {
569472832d2SIan Rogers 		pos = evlist_cpu_itr.evsel;
5707cfcd1e0SJiri Olsa 		if (evsel__strcmp(pos, evsel_name))
5717cfcd1e0SJiri Olsa 			continue;
572c754c382SArnaldo Carvalho de Melo 		if (!evsel__is_group_leader(pos) || !pos->core.fd)
573704e2f5bSAndi Kleen 			continue;
574329725d5SAdrian Hunter 		if (excl_dummy && evsel__is_dummy_event(pos))
575329725d5SAdrian Hunter 			continue;
576472832d2SIan Rogers 		evsel__enable_cpu(pos, evlist_cpu_itr.cpu_map_idx);
577704e2f5bSAndi Kleen 	}
578f350ee95SArnaldo Carvalho de Melo 	affinity__cleanup(affinity);
579e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, pos) {
5807cfcd1e0SJiri Olsa 		if (evsel__strcmp(pos, evsel_name))
5817cfcd1e0SJiri Olsa 			continue;
582c754c382SArnaldo Carvalho de Melo 		if (!evsel__is_group_leader(pos) || !pos->core.fd)
5833fe4430dSJiri Olsa 			continue;
584329725d5SAdrian Hunter 		if (excl_dummy && evsel__is_dummy_event(pos))
585329725d5SAdrian Hunter 			continue;
586704e2f5bSAndi Kleen 		pos->disabled = false;
587764e16a3SDavid Ahern 	}
5882b56bcfbSArnaldo Carvalho de Melo 
5897cfcd1e0SJiri Olsa 	/*
5907cfcd1e0SJiri Olsa 	 * Even single event sets the 'enabled' for evlist,
5917cfcd1e0SJiri Olsa 	 * so the toggle can work properly and toggle to
5927cfcd1e0SJiri Olsa 	 * 'disabled' state.
5937cfcd1e0SJiri Olsa 	 */
5942b56bcfbSArnaldo Carvalho de Melo 	evlist->enabled = true;
5952b56bcfbSArnaldo Carvalho de Melo }
5962b56bcfbSArnaldo Carvalho de Melo 
5977cfcd1e0SJiri Olsa void evlist__enable(struct evlist *evlist)
5987cfcd1e0SJiri Olsa {
599329725d5SAdrian Hunter 	__evlist__enable(evlist, NULL, false);
600329725d5SAdrian Hunter }
601329725d5SAdrian Hunter 
602329725d5SAdrian Hunter void evlist__enable_non_dummy(struct evlist *evlist)
603329725d5SAdrian Hunter {
604329725d5SAdrian Hunter 	__evlist__enable(evlist, NULL, true);
6057cfcd1e0SJiri Olsa }
6067cfcd1e0SJiri Olsa 
6077cfcd1e0SJiri Olsa void evlist__enable_evsel(struct evlist *evlist, char *evsel_name)
6087cfcd1e0SJiri Olsa {
609329725d5SAdrian Hunter 	__evlist__enable(evlist, evsel_name, false);
6107cfcd1e0SJiri Olsa }
6117cfcd1e0SJiri Olsa 
612ade9d208SArnaldo Carvalho de Melo void evlist__toggle_enable(struct evlist *evlist)
6132b56bcfbSArnaldo Carvalho de Melo {
614e74676deSJiri Olsa 	(evlist->enabled ? evlist__disable : evlist__enable)(evlist);
615764e16a3SDavid Ahern }
616764e16a3SDavid Ahern 
617f4009e7bSJiri Olsa int evlist__add_pollfd(struct evlist *evlist, int fd)
618e4b356b5SArnaldo Carvalho de Melo {
619ab4c1f9fSAlexey Budankov 	return perf_evlist__add_pollfd(&evlist->core, fd, NULL, POLLIN, fdarray_flag__default);
620e4b356b5SArnaldo Carvalho de Melo }
6211b85337dSArnaldo Carvalho de Melo 
622f4009e7bSJiri Olsa int evlist__filter_pollfd(struct evlist *evlist, short revents_and_mask)
6231ddec7f0SArnaldo Carvalho de Melo {
62484227cb1SJiri Olsa 	return perf_evlist__filter_pollfd(&evlist->core, revents_and_mask);
6251ddec7f0SArnaldo Carvalho de Melo }
6261ddec7f0SArnaldo Carvalho de Melo 
627e16c2ce7SYang Jihong #ifdef HAVE_EVENTFD_SUPPORT
628e16c2ce7SYang Jihong int evlist__add_wakeup_eventfd(struct evlist *evlist, int fd)
629e16c2ce7SYang Jihong {
630e16c2ce7SYang Jihong 	return perf_evlist__add_pollfd(&evlist->core, fd, NULL, POLLIN,
631a032ad87SAdrian Hunter 				       fdarray_flag__nonfilterable |
632a032ad87SAdrian Hunter 				       fdarray_flag__non_perf_event);
633e16c2ce7SYang Jihong }
634e16c2ce7SYang Jihong #endif
635e16c2ce7SYang Jihong 
63680ab2987SJiri Olsa int evlist__poll(struct evlist *evlist, int timeout)
637f66a889dSArnaldo Carvalho de Melo {
63880ab2987SJiri Olsa 	return perf_evlist__poll(&evlist->core, timeout);
639f66a889dSArnaldo Carvalho de Melo }
640f66a889dSArnaldo Carvalho de Melo 
6413ccf8a7bSArnaldo Carvalho de Melo struct perf_sample_id *evlist__id2sid(struct evlist *evlist, u64 id)
64270db7533SArnaldo Carvalho de Melo {
64370db7533SArnaldo Carvalho de Melo 	struct hlist_head *head;
64470db7533SArnaldo Carvalho de Melo 	struct perf_sample_id *sid;
64570db7533SArnaldo Carvalho de Melo 	int hash;
64670db7533SArnaldo Carvalho de Melo 
64770db7533SArnaldo Carvalho de Melo 	hash = hash_64(id, PERF_EVLIST__HLIST_BITS);
6481d5af02dSJiri Olsa 	head = &evlist->core.heads[hash];
64970db7533SArnaldo Carvalho de Melo 
650b67bfe0dSSasha Levin 	hlist_for_each_entry(sid, head, node)
65170db7533SArnaldo Carvalho de Melo 		if (sid->id == id)
652932a3594SJiri Olsa 			return sid;
653932a3594SJiri Olsa 
654932a3594SJiri Olsa 	return NULL;
655932a3594SJiri Olsa }
656932a3594SJiri Olsa 
6573ccf8a7bSArnaldo Carvalho de Melo struct evsel *evlist__id2evsel(struct evlist *evlist, u64 id)
658932a3594SJiri Olsa {
659932a3594SJiri Olsa 	struct perf_sample_id *sid;
660932a3594SJiri Olsa 
6616484d2f9SJiri Olsa 	if (evlist->core.nr_entries == 1 || !id)
662515dbe48SJiri Olsa 		return evlist__first(evlist);
663932a3594SJiri Olsa 
6643ccf8a7bSArnaldo Carvalho de Melo 	sid = evlist__id2sid(evlist, id);
665932a3594SJiri Olsa 	if (sid)
66670c20369SJiri Olsa 		return container_of(sid->evsel, struct evsel, core);
66730e68bccSNamhyung Kim 
6688cedf3a5SArnaldo Carvalho de Melo 	if (!evlist__sample_id_all(evlist))
669515dbe48SJiri Olsa 		return evlist__first(evlist);
67030e68bccSNamhyung Kim 
67170db7533SArnaldo Carvalho de Melo 	return NULL;
67270db7533SArnaldo Carvalho de Melo }
67304391debSArnaldo Carvalho de Melo 
6743ccf8a7bSArnaldo Carvalho de Melo struct evsel *evlist__id2evsel_strict(struct evlist *evlist, u64 id)
675dddcf6abSAdrian Hunter {
676dddcf6abSAdrian Hunter 	struct perf_sample_id *sid;
677dddcf6abSAdrian Hunter 
678dddcf6abSAdrian Hunter 	if (!id)
679dddcf6abSAdrian Hunter 		return NULL;
680dddcf6abSAdrian Hunter 
6813ccf8a7bSArnaldo Carvalho de Melo 	sid = evlist__id2sid(evlist, id);
682dddcf6abSAdrian Hunter 	if (sid)
68370c20369SJiri Olsa 		return container_of(sid->evsel, struct evsel, core);
684dddcf6abSAdrian Hunter 
685dddcf6abSAdrian Hunter 	return NULL;
686dddcf6abSAdrian Hunter }
687dddcf6abSAdrian Hunter 
6883ccf8a7bSArnaldo Carvalho de Melo static int evlist__event2id(struct evlist *evlist, union perf_event *event, u64 *id)
68975562573SAdrian Hunter {
690b1fcd190SJiri Olsa 	const __u64 *array = event->sample.array;
69175562573SAdrian Hunter 	ssize_t n;
69275562573SAdrian Hunter 
69375562573SAdrian Hunter 	n = (event->header.size - sizeof(event->header)) >> 3;
69475562573SAdrian Hunter 
69575562573SAdrian Hunter 	if (event->header.type == PERF_RECORD_SAMPLE) {
69675562573SAdrian Hunter 		if (evlist->id_pos >= n)
69775562573SAdrian Hunter 			return -1;
69875562573SAdrian Hunter 		*id = array[evlist->id_pos];
69975562573SAdrian Hunter 	} else {
70075562573SAdrian Hunter 		if (evlist->is_pos > n)
70175562573SAdrian Hunter 			return -1;
70275562573SAdrian Hunter 		n -= evlist->is_pos;
70375562573SAdrian Hunter 		*id = array[n];
70475562573SAdrian Hunter 	}
70575562573SAdrian Hunter 	return 0;
70675562573SAdrian Hunter }
70775562573SAdrian Hunter 
7083ccf8a7bSArnaldo Carvalho de Melo struct evsel *evlist__event2evsel(struct evlist *evlist, union perf_event *event)
70975562573SAdrian Hunter {
710515dbe48SJiri Olsa 	struct evsel *first = evlist__first(evlist);
71175562573SAdrian Hunter 	struct hlist_head *head;
71275562573SAdrian Hunter 	struct perf_sample_id *sid;
71375562573SAdrian Hunter 	int hash;
71475562573SAdrian Hunter 	u64 id;
71575562573SAdrian Hunter 
7166484d2f9SJiri Olsa 	if (evlist->core.nr_entries == 1)
71798be6966SAdrian Hunter 		return first;
71898be6966SAdrian Hunter 
7191fc632ceSJiri Olsa 	if (!first->core.attr.sample_id_all &&
72098be6966SAdrian Hunter 	    event->header.type != PERF_RECORD_SAMPLE)
72198be6966SAdrian Hunter 		return first;
72275562573SAdrian Hunter 
7233ccf8a7bSArnaldo Carvalho de Melo 	if (evlist__event2id(evlist, event, &id))
72475562573SAdrian Hunter 		return NULL;
72575562573SAdrian Hunter 
72675562573SAdrian Hunter 	/* Synthesized events have an id of zero */
72775562573SAdrian Hunter 	if (!id)
72898be6966SAdrian Hunter 		return first;
72975562573SAdrian Hunter 
73075562573SAdrian Hunter 	hash = hash_64(id, PERF_EVLIST__HLIST_BITS);
7311d5af02dSJiri Olsa 	head = &evlist->core.heads[hash];
73275562573SAdrian Hunter 
73375562573SAdrian Hunter 	hlist_for_each_entry(sid, head, node) {
73475562573SAdrian Hunter 		if (sid->id == id)
73570c20369SJiri Olsa 			return container_of(sid->evsel, struct evsel, core);
73675562573SAdrian Hunter 	}
73775562573SAdrian Hunter 	return NULL;
73875562573SAdrian Hunter }
73975562573SAdrian Hunter 
7400a60b339SArnaldo Carvalho de Melo static int evlist__set_paused(struct evlist *evlist, bool value)
74165aea233SWang Nan {
74265aea233SWang Nan 	int i;
74365aea233SWang Nan 
7440b72d69aSWang Nan 	if (!evlist->overwrite_mmap)
745078c3386SWang Nan 		return 0;
746078c3386SWang Nan 
747c976ee11SJiri Olsa 	for (i = 0; i < evlist->core.nr_mmaps; i++) {
7482cf07b29SJiri Olsa 		int fd = evlist->overwrite_mmap[i].core.fd;
74965aea233SWang Nan 		int err;
75065aea233SWang Nan 
75165aea233SWang Nan 		if (fd < 0)
75265aea233SWang Nan 			continue;
75365aea233SWang Nan 		err = ioctl(fd, PERF_EVENT_IOC_PAUSE_OUTPUT, value ? 1 : 0);
75465aea233SWang Nan 		if (err)
75565aea233SWang Nan 			return err;
75665aea233SWang Nan 	}
75765aea233SWang Nan 	return 0;
75865aea233SWang Nan }
75965aea233SWang Nan 
7600a60b339SArnaldo Carvalho de Melo static int evlist__pause(struct evlist *evlist)
76165aea233SWang Nan {
7620a60b339SArnaldo Carvalho de Melo 	return evlist__set_paused(evlist, true);
76365aea233SWang Nan }
76465aea233SWang Nan 
7650a60b339SArnaldo Carvalho de Melo static int evlist__resume(struct evlist *evlist)
76665aea233SWang Nan {
7670a60b339SArnaldo Carvalho de Melo 	return evlist__set_paused(evlist, false);
76865aea233SWang Nan }
76965aea233SWang Nan 
770db6b7b13SJiri Olsa static void evlist__munmap_nofree(struct evlist *evlist)
771f8a95309SArnaldo Carvalho de Melo {
772aece948fSArnaldo Carvalho de Melo 	int i;
773f8a95309SArnaldo Carvalho de Melo 
774b2cb615dSWang Nan 	if (evlist->mmap)
775c976ee11SJiri Olsa 		for (i = 0; i < evlist->core.nr_mmaps; i++)
77680e53d11SJiri Olsa 			perf_mmap__munmap(&evlist->mmap[i].core);
777b2cb615dSWang Nan 
7780b72d69aSWang Nan 	if (evlist->overwrite_mmap)
779c976ee11SJiri Olsa 		for (i = 0; i < evlist->core.nr_mmaps; i++)
78080e53d11SJiri Olsa 			perf_mmap__munmap(&evlist->overwrite_mmap[i].core);
781a1f72618SWang Nan }
782aece948fSArnaldo Carvalho de Melo 
783db6b7b13SJiri Olsa void evlist__munmap(struct evlist *evlist)
784a1f72618SWang Nan {
785db6b7b13SJiri Olsa 	evlist__munmap_nofree(evlist);
78604662523SArnaldo Carvalho de Melo 	zfree(&evlist->mmap);
7870b72d69aSWang Nan 	zfree(&evlist->overwrite_mmap);
788f8a95309SArnaldo Carvalho de Melo }
789f8a95309SArnaldo Carvalho de Melo 
79080e53d11SJiri Olsa static void perf_mmap__unmap_cb(struct perf_mmap *map)
79180e53d11SJiri Olsa {
79280e53d11SJiri Olsa 	struct mmap *m = container_of(map, struct mmap, core);
79380e53d11SJiri Olsa 
79480e53d11SJiri Olsa 	mmap__munmap(m);
79580e53d11SJiri Olsa }
79680e53d11SJiri Olsa 
797d50cf361SJiri Olsa static struct mmap *evlist__alloc_mmap(struct evlist *evlist,
7982c5f6d87SKan Liang 				       bool overwrite)
799f8a95309SArnaldo Carvalho de Melo {
800d4c6fb36SWang Nan 	int i;
801a5830532SJiri Olsa 	struct mmap *map;
802d4c6fb36SWang Nan 
803c976ee11SJiri Olsa 	map = zalloc(evlist->core.nr_mmaps * sizeof(struct mmap));
8048db6d6b1SWang Nan 	if (!map)
8058db6d6b1SWang Nan 		return NULL;
806946ae1d4SWang Nan 
807c976ee11SJiri Olsa 	for (i = 0; i < evlist->core.nr_mmaps; i++) {
8086eb65f7aSJiri Olsa 		struct perf_mmap *prev = i ? &map[i - 1].core : NULL;
8096eb65f7aSJiri Olsa 
8104738ca30SArnaldo Carvalho de Melo 		/*
8114738ca30SArnaldo Carvalho de Melo 		 * When the perf_mmap() call is made we grab one refcount, plus
8126afad54dSKan Liang 		 * one extra to let perf_mmap__consume() get the last
8134738ca30SArnaldo Carvalho de Melo 		 * events after all real references (perf_mmap__get()) are
8144738ca30SArnaldo Carvalho de Melo 		 * dropped.
8154738ca30SArnaldo Carvalho de Melo 		 *
8164738ca30SArnaldo Carvalho de Melo 		 * Each PERF_EVENT_IOC_SET_OUTPUT points to this mmap and
8174738ca30SArnaldo Carvalho de Melo 		 * thus does perf_mmap__get() on it.
8184738ca30SArnaldo Carvalho de Melo 		 */
8196eb65f7aSJiri Olsa 		perf_mmap__init(&map[i].core, prev, overwrite, perf_mmap__unmap_cb);
8204738ca30SArnaldo Carvalho de Melo 	}
821353120b4SJiri Olsa 
8228db6d6b1SWang Nan 	return map;
823f8a95309SArnaldo Carvalho de Melo }
824f8a95309SArnaldo Carvalho de Melo 
8259abd2ab2SJiri Olsa static void
8269abd2ab2SJiri Olsa perf_evlist__mmap_cb_idx(struct perf_evlist *_evlist,
827d01508f2SAdrian Hunter 			 struct perf_evsel *_evsel,
8289abd2ab2SJiri Olsa 			 struct perf_mmap_param *_mp,
8296a7b8a5aSAdrian Hunter 			 int idx)
8309abd2ab2SJiri Olsa {
8319abd2ab2SJiri Olsa 	struct evlist *evlist = container_of(_evlist, struct evlist, core);
8329abd2ab2SJiri Olsa 	struct mmap_params *mp = container_of(_mp, struct mmap_params, core);
833d01508f2SAdrian Hunter 	struct evsel *evsel = container_of(_evsel, struct evsel, core);
8349abd2ab2SJiri Olsa 
83584bd5abaSAdrian Hunter 	auxtrace_mmap_params__set_idx(&mp->auxtrace_mp, evlist, evsel, idx);
8369abd2ab2SJiri Olsa }
8379abd2ab2SJiri Olsa 
838bb1b1885SJiri Olsa static struct perf_mmap*
839bb1b1885SJiri Olsa perf_evlist__mmap_cb_get(struct perf_evlist *_evlist, bool overwrite, int idx)
840bb1b1885SJiri Olsa {
841bb1b1885SJiri Olsa 	struct evlist *evlist = container_of(_evlist, struct evlist, core);
8423805e4f3SJiri Olsa 	struct mmap *maps;
843bb1b1885SJiri Olsa 
8443805e4f3SJiri Olsa 	maps = overwrite ? evlist->overwrite_mmap : evlist->mmap;
845bb1b1885SJiri Olsa 
846bb1b1885SJiri Olsa 	if (!maps) {
8473805e4f3SJiri Olsa 		maps = evlist__alloc_mmap(evlist, overwrite);
848bb1b1885SJiri Olsa 		if (!maps)
849bb1b1885SJiri Olsa 			return NULL;
850bb1b1885SJiri Olsa 
8513805e4f3SJiri Olsa 		if (overwrite) {
852bb1b1885SJiri Olsa 			evlist->overwrite_mmap = maps;
853bb1b1885SJiri Olsa 			if (evlist->bkw_mmap_state == BKW_MMAP_NOTREADY)
854ade9d208SArnaldo Carvalho de Melo 				evlist__toggle_bkw_mmap(evlist, BKW_MMAP_RUNNING);
8553805e4f3SJiri Olsa 		} else {
8563805e4f3SJiri Olsa 			evlist->mmap = maps;
857bb1b1885SJiri Olsa 		}
858bb1b1885SJiri Olsa 	}
859bb1b1885SJiri Olsa 
860bb1b1885SJiri Olsa 	return &maps[idx].core;
861bb1b1885SJiri Olsa }
862bb1b1885SJiri Olsa 
863b80132b1SJiri Olsa static int
864b80132b1SJiri Olsa perf_evlist__mmap_cb_mmap(struct perf_mmap *_map, struct perf_mmap_param *_mp,
8656d18804bSIan Rogers 			  int output, struct perf_cpu cpu)
866b80132b1SJiri Olsa {
867b80132b1SJiri Olsa 	struct mmap *map = container_of(_map, struct mmap, core);
868b80132b1SJiri Olsa 	struct mmap_params *mp = container_of(_mp, struct mmap_params, core);
869b80132b1SJiri Olsa 
870b80132b1SJiri Olsa 	return mmap__mmap(map, mp, output, cpu);
871b80132b1SJiri Olsa }
872b80132b1SJiri Olsa 
873f5e7150cSArnaldo Carvalho de Melo unsigned long perf_event_mlock_kb_in_pages(void)
874994a1f78SJiri Olsa {
875f5e7150cSArnaldo Carvalho de Melo 	unsigned long pages;
8768185e881SArnaldo Carvalho de Melo 	int max;
8778185e881SArnaldo Carvalho de Melo 
8788185e881SArnaldo Carvalho de Melo 	if (sysctl__read_int("kernel/perf_event_mlock_kb", &max) < 0) {
8798185e881SArnaldo Carvalho de Melo 		/*
8808185e881SArnaldo Carvalho de Melo 		 * Pick a once upon a time good value, i.e. things look
8818185e881SArnaldo Carvalho de Melo 		 * strange since we can't read a sysctl value, but lets not
8828185e881SArnaldo Carvalho de Melo 		 * die yet...
8838185e881SArnaldo Carvalho de Melo 		 */
8848185e881SArnaldo Carvalho de Melo 		max = 512;
8858185e881SArnaldo Carvalho de Melo 	} else {
8868185e881SArnaldo Carvalho de Melo 		max -= (page_size / 1024);
8878185e881SArnaldo Carvalho de Melo 	}
8888185e881SArnaldo Carvalho de Melo 
8898185e881SArnaldo Carvalho de Melo 	pages = (max * 1024) / page_size;
8901be300f4SArnaldo Carvalho de Melo 	if (!is_power_of_2(pages))
8911be300f4SArnaldo Carvalho de Melo 		pages = rounddown_pow_of_two(pages);
892f5e7150cSArnaldo Carvalho de Melo 
893f5e7150cSArnaldo Carvalho de Melo 	return pages;
894f5e7150cSArnaldo Carvalho de Melo }
895f5e7150cSArnaldo Carvalho de Melo 
8969521b5f2SJiri Olsa size_t evlist__mmap_size(unsigned long pages)
897f5e7150cSArnaldo Carvalho de Melo {
898f5e7150cSArnaldo Carvalho de Melo 	if (pages == UINT_MAX)
899f5e7150cSArnaldo Carvalho de Melo 		pages = perf_event_mlock_kb_in_pages();
900f5e7150cSArnaldo Carvalho de Melo 	else if (!is_power_of_2(pages))
901994a1f78SJiri Olsa 		return 0;
902994a1f78SJiri Olsa 
903994a1f78SJiri Olsa 	return (pages + 1) * page_size;
904994a1f78SJiri Olsa }
905994a1f78SJiri Olsa 
90633c2dcfdSDavid Ahern static long parse_pages_arg(const char *str, unsigned long min,
90733c2dcfdSDavid Ahern 			    unsigned long max)
908994a1f78SJiri Olsa {
9092fbe4abeSAdrian Hunter 	unsigned long pages, val;
91027050f53SJiri Olsa 	static struct parse_tag tags[] = {
91127050f53SJiri Olsa 		{ .tag  = 'B', .mult = 1       },
91227050f53SJiri Olsa 		{ .tag  = 'K', .mult = 1 << 10 },
91327050f53SJiri Olsa 		{ .tag  = 'M', .mult = 1 << 20 },
91427050f53SJiri Olsa 		{ .tag  = 'G', .mult = 1 << 30 },
91527050f53SJiri Olsa 		{ .tag  = 0 },
91627050f53SJiri Olsa 	};
917994a1f78SJiri Olsa 
9188973504bSDavid Ahern 	if (str == NULL)
91933c2dcfdSDavid Ahern 		return -EINVAL;
9208973504bSDavid Ahern 
92127050f53SJiri Olsa 	val = parse_tag_value(str, tags);
9222fbe4abeSAdrian Hunter 	if (val != (unsigned long) -1) {
92327050f53SJiri Olsa 		/* we got file size value */
92427050f53SJiri Olsa 		pages = PERF_ALIGN(val, page_size) / page_size;
92527050f53SJiri Olsa 	} else {
92627050f53SJiri Olsa 		/* we got pages count value */
92727050f53SJiri Olsa 		char *eptr;
928994a1f78SJiri Olsa 		pages = strtoul(str, &eptr, 10);
92933c2dcfdSDavid Ahern 		if (*eptr != '\0')
93033c2dcfdSDavid Ahern 			return -EINVAL;
93127050f53SJiri Olsa 	}
932994a1f78SJiri Olsa 
9332bcab6c1SAdrian Hunter 	if (pages == 0 && min == 0) {
93433c2dcfdSDavid Ahern 		/* leave number of pages at 0 */
9351dbfa938SAdrian Hunter 	} else if (!is_power_of_2(pages)) {
9369808143bSJiri Olsa 		char buf[100];
9379808143bSJiri Olsa 
93833c2dcfdSDavid Ahern 		/* round pages up to next power of 2 */
93991529834SArnaldo Carvalho de Melo 		pages = roundup_pow_of_two(pages);
9401dbfa938SAdrian Hunter 		if (!pages)
9411dbfa938SAdrian Hunter 			return -EINVAL;
9429808143bSJiri Olsa 
9439808143bSJiri Olsa 		unit_number__scnprintf(buf, sizeof(buf), pages * page_size);
9449808143bSJiri Olsa 		pr_info("rounding mmap pages size to %s (%lu pages)\n",
9459808143bSJiri Olsa 			buf, pages);
9462fbe4abeSAdrian Hunter 	}
9472fbe4abeSAdrian Hunter 
94833c2dcfdSDavid Ahern 	if (pages > max)
94933c2dcfdSDavid Ahern 		return -EINVAL;
95033c2dcfdSDavid Ahern 
95133c2dcfdSDavid Ahern 	return pages;
95233c2dcfdSDavid Ahern }
95333c2dcfdSDavid Ahern 
95425f84702SArnaldo Carvalho de Melo int __evlist__parse_mmap_pages(unsigned int *mmap_pages, const char *str)
95533c2dcfdSDavid Ahern {
95633c2dcfdSDavid Ahern 	unsigned long max = UINT_MAX;
95733c2dcfdSDavid Ahern 	long pages;
95833c2dcfdSDavid Ahern 
959f5ae9c42SAdrian Hunter 	if (max > SIZE_MAX / page_size)
96033c2dcfdSDavid Ahern 		max = SIZE_MAX / page_size;
96133c2dcfdSDavid Ahern 
96233c2dcfdSDavid Ahern 	pages = parse_pages_arg(str, 1, max);
96333c2dcfdSDavid Ahern 	if (pages < 0) {
96433c2dcfdSDavid Ahern 		pr_err("Invalid argument for --mmap_pages/-m\n");
965994a1f78SJiri Olsa 		return -1;
966994a1f78SJiri Olsa 	}
967994a1f78SJiri Olsa 
968994a1f78SJiri Olsa 	*mmap_pages = pages;
969994a1f78SJiri Olsa 	return 0;
970994a1f78SJiri Olsa }
971994a1f78SJiri Olsa 
97225f84702SArnaldo Carvalho de Melo int evlist__parse_mmap_pages(const struct option *opt, const char *str, int unset __maybe_unused)
973e9db1310SAdrian Hunter {
97425f84702SArnaldo Carvalho de Melo 	return __evlist__parse_mmap_pages(opt->value, str);
975e9db1310SAdrian Hunter }
976e9db1310SAdrian Hunter 
977c83fa7f2SAdrian Hunter /**
9789521b5f2SJiri Olsa  * evlist__mmap_ex - Create mmaps to receive events.
979c83fa7f2SAdrian Hunter  * @evlist: list of events
980c83fa7f2SAdrian Hunter  * @pages: map length in pages
981c83fa7f2SAdrian Hunter  * @overwrite: overwrite older events?
982718c602dSAdrian Hunter  * @auxtrace_pages - auxtrace map length in pages
983718c602dSAdrian Hunter  * @auxtrace_overwrite - overwrite older auxtrace data?
984f8a95309SArnaldo Carvalho de Melo  *
985c83fa7f2SAdrian Hunter  * If @overwrite is %false the user needs to signal event consumption using
9869521b5f2SJiri Olsa  * perf_mmap__write_tail().  Using evlist__mmap_read() does this
987c83fa7f2SAdrian Hunter  * automatically.
988f8a95309SArnaldo Carvalho de Melo  *
989718c602dSAdrian Hunter  * Similarly, if @auxtrace_overwrite is %false the user needs to signal data
990718c602dSAdrian Hunter  * consumption using auxtrace_mmap__write_tail().
991718c602dSAdrian Hunter  *
992c83fa7f2SAdrian Hunter  * Return: %0 on success, negative error code otherwise.
993f8a95309SArnaldo Carvalho de Melo  */
9949521b5f2SJiri Olsa int evlist__mmap_ex(struct evlist *evlist, unsigned int pages,
9957a276ff6SWang Nan 			 unsigned int auxtrace_pages,
99651255a8aSAlexey Budankov 			 bool auxtrace_overwrite, int nr_cblocks, int affinity, int flush,
99751255a8aSAlexey Budankov 			 int comp_level)
998f8a95309SArnaldo Carvalho de Melo {
99971f566a3SWang Nan 	/*
100071f566a3SWang Nan 	 * Delay setting mp.prot: set it before calling perf_mmap__mmap.
100171f566a3SWang Nan 	 * Its value is decided by evsel's write_backward.
100271f566a3SWang Nan 	 * So &mp should not be passed through const pointer.
100371f566a3SWang Nan 	 */
1004e440979fSJiri Olsa 	struct mmap_params mp = {
1005e440979fSJiri Olsa 		.nr_cblocks	= nr_cblocks,
1006e440979fSJiri Olsa 		.affinity	= affinity,
1007e440979fSJiri Olsa 		.flush		= flush,
1008e440979fSJiri Olsa 		.comp_level	= comp_level
1009e440979fSJiri Olsa 	};
1010923d0f18SJiri Olsa 	struct perf_evlist_mmap_ops ops = {
10119abd2ab2SJiri Olsa 		.idx  = perf_evlist__mmap_cb_idx,
1012bb1b1885SJiri Olsa 		.get  = perf_evlist__mmap_cb_get,
1013b80132b1SJiri Olsa 		.mmap = perf_evlist__mmap_cb_mmap,
10149abd2ab2SJiri Olsa 	};
101550a682ceSArnaldo Carvalho de Melo 
1016f6fa4375SJiri Olsa 	evlist->core.mmap_len = evlist__mmap_size(pages);
1017f6fa4375SJiri Olsa 	pr_debug("mmap size %zuB\n", evlist->core.mmap_len);
1018f8a95309SArnaldo Carvalho de Melo 
1019f6fa4375SJiri Olsa 	auxtrace_mmap_params__init(&mp.auxtrace_mp, evlist->core.mmap_len,
1020718c602dSAdrian Hunter 				   auxtrace_pages, auxtrace_overwrite);
1021718c602dSAdrian Hunter 
1022923d0f18SJiri Olsa 	return perf_evlist__mmap_ops(&evlist->core, &ops, &mp.core);
1023f8a95309SArnaldo Carvalho de Melo }
10247e2ed097SArnaldo Carvalho de Melo 
10259521b5f2SJiri Olsa int evlist__mmap(struct evlist *evlist, unsigned int pages)
1026718c602dSAdrian Hunter {
10279521b5f2SJiri Olsa 	return evlist__mmap_ex(evlist, pages, 0, false, 0, PERF_AFFINITY_SYS, 1, 0);
1028718c602dSAdrian Hunter }
1029718c602dSAdrian Hunter 
10307748bb71SArnaldo Carvalho de Melo int evlist__create_maps(struct evlist *evlist, struct target *target)
10317e2ed097SArnaldo Carvalho de Melo {
1032147c508fSJin Yao 	bool all_threads = (target->per_thread && target->system_wide);
1033f854839bSJiri Olsa 	struct perf_cpu_map *cpus;
10349749b90eSJiri Olsa 	struct perf_thread_map *threads;
10357e2ed097SArnaldo Carvalho de Melo 
1036147c508fSJin Yao 	/*
1037147c508fSJin Yao 	 * If specify '-a' and '--per-thread' to perf record, perf record
1038147c508fSJin Yao 	 * will override '--per-thread'. target->per_thread = false and
1039147c508fSJin Yao 	 * target->system_wide = true.
1040147c508fSJin Yao 	 *
1041147c508fSJin Yao 	 * If specify '--per-thread' only to perf record,
1042147c508fSJin Yao 	 * target->per_thread = true and target->system_wide = false.
1043147c508fSJin Yao 	 *
1044147c508fSJin Yao 	 * So target->per_thread && target->system_wide is false.
1045147c508fSJin Yao 	 * For perf record, thread_map__new_str doesn't call
1046147c508fSJin Yao 	 * thread_map__new_all_cpus. That will keep perf record's
1047147c508fSJin Yao 	 * current behavior.
1048147c508fSJin Yao 	 *
1049147c508fSJin Yao 	 * For perf stat, it allows the case that target->per_thread and
1050147c508fSJin Yao 	 * target->system_wide are all true. It means to collect system-wide
1051147c508fSJin Yao 	 * per-thread data. thread_map__new_str will call
1052147c508fSJin Yao 	 * thread_map__new_all_cpus to enumerate all threads.
1053147c508fSJin Yao 	 */
105473c0ca1eSJin Yao 	threads = thread_map__new_str(target->pid, target->tid, target->uid,
1055147c508fSJin Yao 				      all_threads);
105674bfd2b2SAdrian Hunter 
105774bfd2b2SAdrian Hunter 	if (!threads)
10587e2ed097SArnaldo Carvalho de Melo 		return -1;
10597e2ed097SArnaldo Carvalho de Melo 
10609c105fbcSDongsheng Yang 	if (target__uses_dummy_map(target))
1061397721e0SJiri Olsa 		cpus = perf_cpu_map__dummy_new();
1062879d77d0SNamhyung Kim 	else
10639c3516d1SJiri Olsa 		cpus = perf_cpu_map__new(target->cpu_list);
10647e2ed097SArnaldo Carvalho de Melo 
106574bfd2b2SAdrian Hunter 	if (!cpus)
10667e2ed097SArnaldo Carvalho de Melo 		goto out_delete_threads;
10677e2ed097SArnaldo Carvalho de Melo 
1068b726e363SJin Yao 	evlist->core.has_user_cpus = !!target->cpu_list && !target->hybrid;
1069ec9a77a7SAdrian Hunter 
1070453fa030SJiri Olsa 	perf_evlist__set_maps(&evlist->core, cpus, threads);
1071d5bc056eSAdrian Hunter 
1072bfd1b83dSNamhyung Kim 	/* as evlist now has references, put count here */
1073bfd1b83dSNamhyung Kim 	perf_cpu_map__put(cpus);
1074bfd1b83dSNamhyung Kim 	perf_thread_map__put(threads);
1075bfd1b83dSNamhyung Kim 
1076d5bc056eSAdrian Hunter 	return 0;
10777e2ed097SArnaldo Carvalho de Melo 
10787e2ed097SArnaldo Carvalho de Melo out_delete_threads:
10797836e52eSJiri Olsa 	perf_thread_map__put(threads);
10807e2ed097SArnaldo Carvalho de Melo 	return -1;
10817e2ed097SArnaldo Carvalho de Melo }
10827e2ed097SArnaldo Carvalho de Melo 
108324bf91a7SArnaldo Carvalho de Melo int evlist__apply_filters(struct evlist *evlist, struct evsel **err_evsel)
10840a102479SFrederic Weisbecker {
108532dcd021SJiri Olsa 	struct evsel *evsel;
1086745cefc5SArnaldo Carvalho de Melo 	int err = 0;
10870a102479SFrederic Weisbecker 
1088e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, evsel) {
1089745cefc5SArnaldo Carvalho de Melo 		if (evsel->filter == NULL)
10900a102479SFrederic Weisbecker 			continue;
1091745cefc5SArnaldo Carvalho de Melo 
1092d988d5eeSKan Liang 		/*
1093d988d5eeSKan Liang 		 * filters only work for tracepoint event, which doesn't have cpu limit.
1094d988d5eeSKan Liang 		 * So evlist and evsel should always be same.
1095d988d5eeSKan Liang 		 */
1096a00571fdSJiri Olsa 		err = perf_evsel__apply_filter(&evsel->core, evsel->filter);
109723d4aad4SArnaldo Carvalho de Melo 		if (err) {
109823d4aad4SArnaldo Carvalho de Melo 			*err_evsel = evsel;
1099745cefc5SArnaldo Carvalho de Melo 			break;
11000a102479SFrederic Weisbecker 		}
110123d4aad4SArnaldo Carvalho de Melo 	}
11020a102479SFrederic Weisbecker 
1103745cefc5SArnaldo Carvalho de Melo 	return err;
1104745cefc5SArnaldo Carvalho de Melo }
1105745cefc5SArnaldo Carvalho de Melo 
110624bf91a7SArnaldo Carvalho de Melo int evlist__set_tp_filter(struct evlist *evlist, const char *filter)
1107745cefc5SArnaldo Carvalho de Melo {
110832dcd021SJiri Olsa 	struct evsel *evsel;
1109745cefc5SArnaldo Carvalho de Melo 	int err = 0;
1110745cefc5SArnaldo Carvalho de Melo 
111105cea449SArnaldo Carvalho de Melo 	if (filter == NULL)
111205cea449SArnaldo Carvalho de Melo 		return -1;
111305cea449SArnaldo Carvalho de Melo 
1114e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, evsel) {
11151fc632ceSJiri Olsa 		if (evsel->core.attr.type != PERF_TYPE_TRACEPOINT)
1116fdf14720SWang Nan 			continue;
1117fdf14720SWang Nan 
1118ad681adfSArnaldo Carvalho de Melo 		err = evsel__set_filter(evsel, filter);
1119745cefc5SArnaldo Carvalho de Melo 		if (err)
1120745cefc5SArnaldo Carvalho de Melo 			break;
1121745cefc5SArnaldo Carvalho de Melo 	}
1122745cefc5SArnaldo Carvalho de Melo 
1123745cefc5SArnaldo Carvalho de Melo 	return err;
11240a102479SFrederic Weisbecker }
112574429964SFrederic Weisbecker 
112624bf91a7SArnaldo Carvalho de Melo int evlist__append_tp_filter(struct evlist *evlist, const char *filter)
112753c92f73SArnaldo Carvalho de Melo {
112853c92f73SArnaldo Carvalho de Melo 	struct evsel *evsel;
112953c92f73SArnaldo Carvalho de Melo 	int err = 0;
113053c92f73SArnaldo Carvalho de Melo 
113153c92f73SArnaldo Carvalho de Melo 	if (filter == NULL)
113253c92f73SArnaldo Carvalho de Melo 		return -1;
113353c92f73SArnaldo Carvalho de Melo 
113453c92f73SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, evsel) {
113553c92f73SArnaldo Carvalho de Melo 		if (evsel->core.attr.type != PERF_TYPE_TRACEPOINT)
113653c92f73SArnaldo Carvalho de Melo 			continue;
113753c92f73SArnaldo Carvalho de Melo 
1138ad681adfSArnaldo Carvalho de Melo 		err = evsel__append_tp_filter(evsel, filter);
113953c92f73SArnaldo Carvalho de Melo 		if (err)
114053c92f73SArnaldo Carvalho de Melo 			break;
114153c92f73SArnaldo Carvalho de Melo 	}
114253c92f73SArnaldo Carvalho de Melo 
114353c92f73SArnaldo Carvalho de Melo 	return err;
114453c92f73SArnaldo Carvalho de Melo }
114553c92f73SArnaldo Carvalho de Melo 
1146da949f50SArnaldo Carvalho de Melo char *asprintf__tp_filter_pids(size_t npids, pid_t *pids)
1147cfd70a26SArnaldo Carvalho de Melo {
1148cfd70a26SArnaldo Carvalho de Melo 	char *filter;
1149be199adaSArnaldo Carvalho de Melo 	size_t i;
1150cfd70a26SArnaldo Carvalho de Melo 
1151be199adaSArnaldo Carvalho de Melo 	for (i = 0; i < npids; ++i) {
1152be199adaSArnaldo Carvalho de Melo 		if (i == 0) {
1153be199adaSArnaldo Carvalho de Melo 			if (asprintf(&filter, "common_pid != %d", pids[i]) < 0)
115405cea449SArnaldo Carvalho de Melo 				return NULL;
1155be199adaSArnaldo Carvalho de Melo 		} else {
1156be199adaSArnaldo Carvalho de Melo 			char *tmp;
1157be199adaSArnaldo Carvalho de Melo 
1158be199adaSArnaldo Carvalho de Melo 			if (asprintf(&tmp, "%s && common_pid != %d", filter, pids[i]) < 0)
1159be199adaSArnaldo Carvalho de Melo 				goto out_free;
1160be199adaSArnaldo Carvalho de Melo 
1161be199adaSArnaldo Carvalho de Melo 			free(filter);
1162be199adaSArnaldo Carvalho de Melo 			filter = tmp;
1163be199adaSArnaldo Carvalho de Melo 		}
1164be199adaSArnaldo Carvalho de Melo 	}
1165cfd70a26SArnaldo Carvalho de Melo 
116605cea449SArnaldo Carvalho de Melo 	return filter;
1167be199adaSArnaldo Carvalho de Melo out_free:
1168cfd70a26SArnaldo Carvalho de Melo 	free(filter);
116905cea449SArnaldo Carvalho de Melo 	return NULL;
117005cea449SArnaldo Carvalho de Melo }
117105cea449SArnaldo Carvalho de Melo 
117224bf91a7SArnaldo Carvalho de Melo int evlist__set_tp_filter_pids(struct evlist *evlist, size_t npids, pid_t *pids)
117305cea449SArnaldo Carvalho de Melo {
117405cea449SArnaldo Carvalho de Melo 	char *filter = asprintf__tp_filter_pids(npids, pids);
117524bf91a7SArnaldo Carvalho de Melo 	int ret = evlist__set_tp_filter(evlist, filter);
117605cea449SArnaldo Carvalho de Melo 
117705cea449SArnaldo Carvalho de Melo 	free(filter);
1178cfd70a26SArnaldo Carvalho de Melo 	return ret;
1179cfd70a26SArnaldo Carvalho de Melo }
1180cfd70a26SArnaldo Carvalho de Melo 
118124bf91a7SArnaldo Carvalho de Melo int evlist__set_tp_filter_pid(struct evlist *evlist, pid_t pid)
1182be199adaSArnaldo Carvalho de Melo {
118324bf91a7SArnaldo Carvalho de Melo 	return evlist__set_tp_filter_pids(evlist, 1, &pid);
1184be199adaSArnaldo Carvalho de Melo }
1185be199adaSArnaldo Carvalho de Melo 
118624bf91a7SArnaldo Carvalho de Melo int evlist__append_tp_filter_pids(struct evlist *evlist, size_t npids, pid_t *pids)
11871827ab5bSArnaldo Carvalho de Melo {
11881827ab5bSArnaldo Carvalho de Melo 	char *filter = asprintf__tp_filter_pids(npids, pids);
118924bf91a7SArnaldo Carvalho de Melo 	int ret = evlist__append_tp_filter(evlist, filter);
11901827ab5bSArnaldo Carvalho de Melo 
11911827ab5bSArnaldo Carvalho de Melo 	free(filter);
11921827ab5bSArnaldo Carvalho de Melo 	return ret;
11931827ab5bSArnaldo Carvalho de Melo }
11941827ab5bSArnaldo Carvalho de Melo 
119524bf91a7SArnaldo Carvalho de Melo int evlist__append_tp_filter_pid(struct evlist *evlist, pid_t pid)
11961827ab5bSArnaldo Carvalho de Melo {
119724bf91a7SArnaldo Carvalho de Melo 	return evlist__append_tp_filter_pids(evlist, 1, &pid);
11981827ab5bSArnaldo Carvalho de Melo }
11991827ab5bSArnaldo Carvalho de Melo 
1200b3c2cc2bSArnaldo Carvalho de Melo bool evlist__valid_sample_type(struct evlist *evlist)
120174429964SFrederic Weisbecker {
120232dcd021SJiri Olsa 	struct evsel *pos;
1203c2a70653SArnaldo Carvalho de Melo 
12046484d2f9SJiri Olsa 	if (evlist->core.nr_entries == 1)
120575562573SAdrian Hunter 		return true;
120675562573SAdrian Hunter 
120775562573SAdrian Hunter 	if (evlist->id_pos < 0 || evlist->is_pos < 0)
120875562573SAdrian Hunter 		return false;
120975562573SAdrian Hunter 
1210e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, pos) {
121175562573SAdrian Hunter 		if (pos->id_pos != evlist->id_pos ||
121275562573SAdrian Hunter 		    pos->is_pos != evlist->is_pos)
1213c2a70653SArnaldo Carvalho de Melo 			return false;
121474429964SFrederic Weisbecker 	}
121574429964SFrederic Weisbecker 
1216c2a70653SArnaldo Carvalho de Melo 	return true;
1217c2a70653SArnaldo Carvalho de Melo }
1218c2a70653SArnaldo Carvalho de Melo 
1219b3c2cc2bSArnaldo Carvalho de Melo u64 __evlist__combined_sample_type(struct evlist *evlist)
1220c2a70653SArnaldo Carvalho de Melo {
122132dcd021SJiri Olsa 	struct evsel *evsel;
122275562573SAdrian Hunter 
122375562573SAdrian Hunter 	if (evlist->combined_sample_type)
122475562573SAdrian Hunter 		return evlist->combined_sample_type;
122575562573SAdrian Hunter 
1226e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, evsel)
12271fc632ceSJiri Olsa 		evlist->combined_sample_type |= evsel->core.attr.sample_type;
122875562573SAdrian Hunter 
122975562573SAdrian Hunter 	return evlist->combined_sample_type;
123075562573SAdrian Hunter }
123175562573SAdrian Hunter 
1232b3c2cc2bSArnaldo Carvalho de Melo u64 evlist__combined_sample_type(struct evlist *evlist)
123375562573SAdrian Hunter {
123475562573SAdrian Hunter 	evlist->combined_sample_type = 0;
1235b3c2cc2bSArnaldo Carvalho de Melo 	return __evlist__combined_sample_type(evlist);
1236c2a70653SArnaldo Carvalho de Melo }
1237c2a70653SArnaldo Carvalho de Melo 
123892c7d7cdSArnaldo Carvalho de Melo u64 evlist__combined_branch_type(struct evlist *evlist)
123998df858eSAndi Kleen {
124032dcd021SJiri Olsa 	struct evsel *evsel;
124198df858eSAndi Kleen 	u64 branch_type = 0;
124298df858eSAndi Kleen 
1243e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, evsel)
12441fc632ceSJiri Olsa 		branch_type |= evsel->core.attr.branch_sample_type;
124598df858eSAndi Kleen 	return branch_type;
124698df858eSAndi Kleen }
124798df858eSAndi Kleen 
124878e1bc25SArnaldo Carvalho de Melo bool evlist__valid_read_format(struct evlist *evlist)
12499ede473cSJiri Olsa {
1250515dbe48SJiri Olsa 	struct evsel *first = evlist__first(evlist), *pos = first;
12511fc632ceSJiri Olsa 	u64 read_format = first->core.attr.read_format;
12521fc632ceSJiri Olsa 	u64 sample_type = first->core.attr.sample_type;
12539ede473cSJiri Olsa 
1254e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, pos) {
125594d3820fSAdrian Hunter 		if (read_format != pos->core.attr.read_format) {
125694d3820fSAdrian Hunter 			pr_debug("Read format differs %#" PRIx64 " vs %#" PRIx64 "\n",
125794d3820fSAdrian Hunter 				 read_format, (u64)pos->core.attr.read_format);
125894d3820fSAdrian Hunter 		}
12599ede473cSJiri Olsa 	}
12609ede473cSJiri Olsa 
12614d39c89fSIngo Molnar 	/* PERF_SAMPLE_READ implies PERF_FORMAT_ID. */
12629ede473cSJiri Olsa 	if ((sample_type & PERF_SAMPLE_READ) &&
12639ede473cSJiri Olsa 	    !(read_format & PERF_FORMAT_ID)) {
12649ede473cSJiri Olsa 		return false;
12659ede473cSJiri Olsa 	}
12669ede473cSJiri Olsa 
12679ede473cSJiri Olsa 	return true;
12689ede473cSJiri Olsa }
12699ede473cSJiri Olsa 
12701420ba2fSArnaldo Carvalho de Melo u16 evlist__id_hdr_size(struct evlist *evlist)
127181e36bffSArnaldo Carvalho de Melo {
1272515dbe48SJiri Olsa 	struct evsel *first = evlist__first(evlist);
127381e36bffSArnaldo Carvalho de Melo 
12740a64de04SAdrian Hunter 	return first->core.attr.sample_id_all ? evsel__id_hdr_size(first) : 0;
127581e36bffSArnaldo Carvalho de Melo }
127681e36bffSArnaldo Carvalho de Melo 
12778cedf3a5SArnaldo Carvalho de Melo bool evlist__valid_sample_id_all(struct evlist *evlist)
1278c2a70653SArnaldo Carvalho de Melo {
1279515dbe48SJiri Olsa 	struct evsel *first = evlist__first(evlist), *pos = first;
1280c2a70653SArnaldo Carvalho de Melo 
1281e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry_continue(evlist, pos) {
12821fc632ceSJiri Olsa 		if (first->core.attr.sample_id_all != pos->core.attr.sample_id_all)
1283c2a70653SArnaldo Carvalho de Melo 			return false;
1284c2a70653SArnaldo Carvalho de Melo 	}
1285c2a70653SArnaldo Carvalho de Melo 
1286c2a70653SArnaldo Carvalho de Melo 	return true;
128774429964SFrederic Weisbecker }
128874429964SFrederic Weisbecker 
12898cedf3a5SArnaldo Carvalho de Melo bool evlist__sample_id_all(struct evlist *evlist)
129074429964SFrederic Weisbecker {
1291515dbe48SJiri Olsa 	struct evsel *first = evlist__first(evlist);
12921fc632ceSJiri Olsa 	return first->core.attr.sample_id_all;
129374429964SFrederic Weisbecker }
129481cce8deSArnaldo Carvalho de Melo 
1295900c8eadSArnaldo Carvalho de Melo void evlist__set_selected(struct evlist *evlist, struct evsel *evsel)
129681cce8deSArnaldo Carvalho de Melo {
129781cce8deSArnaldo Carvalho de Melo 	evlist->selected = evsel;
129881cce8deSArnaldo Carvalho de Melo }
1299727ab04eSArnaldo Carvalho de Melo 
1300750b4edeSJiri Olsa void evlist__close(struct evlist *evlist)
1301a74b4b66SNamhyung Kim {
130232dcd021SJiri Olsa 	struct evsel *evsel;
1303472832d2SIan Rogers 	struct evlist_cpu_iterator evlist_cpu_itr;
13047736627bSAndi Kleen 	struct affinity affinity;
1305a74b4b66SNamhyung Kim 
13067736627bSAndi Kleen 	/*
13070df6ade7SIan Rogers 	 * With perf record core.user_requested_cpus is usually NULL.
13087736627bSAndi Kleen 	 * Use the old method to handle this for now.
13097736627bSAndi Kleen 	 */
13100df6ade7SIan Rogers 	if (!evlist->core.user_requested_cpus ||
13110df6ade7SIan Rogers 	    cpu_map__is_dummy(evlist->core.user_requested_cpus)) {
1312475fb533SAndi Kleen 		evlist__for_each_entry_reverse(evlist, evsel)
131388761fa1SJiri Olsa 			evsel__close(evsel);
13147736627bSAndi Kleen 		return;
13157736627bSAndi Kleen 	}
13167736627bSAndi Kleen 
13177736627bSAndi Kleen 	if (affinity__setup(&affinity) < 0)
13187736627bSAndi Kleen 		return;
13197736627bSAndi Kleen 
1320472832d2SIan Rogers 	evlist__for_each_cpu(evlist_cpu_itr, evlist, &affinity) {
1321472832d2SIan Rogers 		perf_evsel__close_cpu(&evlist_cpu_itr.evsel->core,
1322472832d2SIan Rogers 				      evlist_cpu_itr.cpu_map_idx);
13237736627bSAndi Kleen 	}
1324472832d2SIan Rogers 
13257736627bSAndi Kleen 	affinity__cleanup(&affinity);
13267736627bSAndi Kleen 	evlist__for_each_entry_reverse(evlist, evsel) {
13277736627bSAndi Kleen 		perf_evsel__free_fd(&evsel->core);
13287736627bSAndi Kleen 		perf_evsel__free_id(&evsel->core);
13297736627bSAndi Kleen 	}
1330513068f2SNamhyung Kim 	perf_evlist__reset_id_hash(&evlist->core);
1331a74b4b66SNamhyung Kim }
1332a74b4b66SNamhyung Kim 
13337748bb71SArnaldo Carvalho de Melo static int evlist__create_syswide_maps(struct evlist *evlist)
13344112eb18SArnaldo Carvalho de Melo {
1335f854839bSJiri Olsa 	struct perf_cpu_map *cpus;
13369749b90eSJiri Olsa 	struct perf_thread_map *threads;
13374112eb18SArnaldo Carvalho de Melo 
13384112eb18SArnaldo Carvalho de Melo 	/*
13394112eb18SArnaldo Carvalho de Melo 	 * Try reading /sys/devices/system/cpu/online to get
13404112eb18SArnaldo Carvalho de Melo 	 * an all cpus map.
13414112eb18SArnaldo Carvalho de Melo 	 *
13424112eb18SArnaldo Carvalho de Melo 	 * FIXME: -ENOMEM is the best we can do here, the cpu_map
13434112eb18SArnaldo Carvalho de Melo 	 * code needs an overhaul to properly forward the
13444112eb18SArnaldo Carvalho de Melo 	 * error, and we may not want to do that fallback to a
13454112eb18SArnaldo Carvalho de Melo 	 * default cpu identity map :-\
13464112eb18SArnaldo Carvalho de Melo 	 */
13479c3516d1SJiri Olsa 	cpus = perf_cpu_map__new(NULL);
13488c0498b6SAdrian Hunter 	if (!cpus)
13494112eb18SArnaldo Carvalho de Melo 		goto out;
13504112eb18SArnaldo Carvalho de Melo 
13514b49cce2SJiri Olsa 	threads = perf_thread_map__new_dummy();
13528c0498b6SAdrian Hunter 	if (!threads)
13538c0498b6SAdrian Hunter 		goto out_put;
13544112eb18SArnaldo Carvalho de Melo 
1355453fa030SJiri Olsa 	perf_evlist__set_maps(&evlist->core, cpus, threads);
1356bfd1b83dSNamhyung Kim 
1357bfd1b83dSNamhyung Kim 	perf_thread_map__put(threads);
13588c0498b6SAdrian Hunter out_put:
135938f01d8dSJiri Olsa 	perf_cpu_map__put(cpus);
1360bfd1b83dSNamhyung Kim out:
1361f717d89aSHaowen Bai 	return -ENOMEM;
13624112eb18SArnaldo Carvalho de Melo }
13634112eb18SArnaldo Carvalho de Melo 
1364474ddc4cSJiri Olsa int evlist__open(struct evlist *evlist)
1365727ab04eSArnaldo Carvalho de Melo {
136632dcd021SJiri Olsa 	struct evsel *evsel;
1367a74b4b66SNamhyung Kim 	int err;
1368727ab04eSArnaldo Carvalho de Melo 
13694112eb18SArnaldo Carvalho de Melo 	/*
13704112eb18SArnaldo Carvalho de Melo 	 * Default: one fd per CPU, all threads, aka systemwide
13714112eb18SArnaldo Carvalho de Melo 	 * as sys_perf_event_open(cpu = -1, thread = -1) is EINVAL
13724112eb18SArnaldo Carvalho de Melo 	 */
13730df6ade7SIan Rogers 	if (evlist->core.threads == NULL && evlist->core.user_requested_cpus == NULL) {
13747748bb71SArnaldo Carvalho de Melo 		err = evlist__create_syswide_maps(evlist);
13754112eb18SArnaldo Carvalho de Melo 		if (err < 0)
13764112eb18SArnaldo Carvalho de Melo 			goto out_err;
13774112eb18SArnaldo Carvalho de Melo 	}
13784112eb18SArnaldo Carvalho de Melo 
13790a7e7ec9SArnaldo Carvalho de Melo 	evlist__update_id_pos(evlist);
1380733cd2feSAdrian Hunter 
1381e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, evsel) {
1382af663bd0SJiri Olsa 		err = evsel__open(evsel, evsel->core.cpus, evsel->core.threads);
1383727ab04eSArnaldo Carvalho de Melo 		if (err < 0)
1384727ab04eSArnaldo Carvalho de Melo 			goto out_err;
1385727ab04eSArnaldo Carvalho de Melo 	}
1386727ab04eSArnaldo Carvalho de Melo 
1387727ab04eSArnaldo Carvalho de Melo 	return 0;
1388727ab04eSArnaldo Carvalho de Melo out_err:
1389750b4edeSJiri Olsa 	evlist__close(evlist);
139041c21a68SNamhyung Kim 	errno = -err;
1391727ab04eSArnaldo Carvalho de Melo 	return err;
1392727ab04eSArnaldo Carvalho de Melo }
139335b9d88eSArnaldo Carvalho de Melo 
13947b392ef0SArnaldo Carvalho de Melo int evlist__prepare_workload(struct evlist *evlist, struct target *target, const char *argv[],
13957b392ef0SArnaldo Carvalho de Melo 			     bool pipe_output, void (*exec_error)(int signo, siginfo_t *info, void *ucontext))
139635b9d88eSArnaldo Carvalho de Melo {
139735b9d88eSArnaldo Carvalho de Melo 	int child_ready_pipe[2], go_pipe[2];
139835b9d88eSArnaldo Carvalho de Melo 	char bf;
139935b9d88eSArnaldo Carvalho de Melo 
140035b9d88eSArnaldo Carvalho de Melo 	if (pipe(child_ready_pipe) < 0) {
140135b9d88eSArnaldo Carvalho de Melo 		perror("failed to create 'ready' pipe");
140235b9d88eSArnaldo Carvalho de Melo 		return -1;
140335b9d88eSArnaldo Carvalho de Melo 	}
140435b9d88eSArnaldo Carvalho de Melo 
140535b9d88eSArnaldo Carvalho de Melo 	if (pipe(go_pipe) < 0) {
140635b9d88eSArnaldo Carvalho de Melo 		perror("failed to create 'go' pipe");
140735b9d88eSArnaldo Carvalho de Melo 		goto out_close_ready_pipe;
140835b9d88eSArnaldo Carvalho de Melo 	}
140935b9d88eSArnaldo Carvalho de Melo 
141035b9d88eSArnaldo Carvalho de Melo 	evlist->workload.pid = fork();
141135b9d88eSArnaldo Carvalho de Melo 	if (evlist->workload.pid < 0) {
141235b9d88eSArnaldo Carvalho de Melo 		perror("failed to fork");
141335b9d88eSArnaldo Carvalho de Melo 		goto out_close_pipes;
141435b9d88eSArnaldo Carvalho de Melo 	}
141535b9d88eSArnaldo Carvalho de Melo 
141635b9d88eSArnaldo Carvalho de Melo 	if (!evlist->workload.pid) {
14175f1c4225SArnaldo Carvalho de Melo 		int ret;
14185f1c4225SArnaldo Carvalho de Melo 
1419119fa3c9SNamhyung Kim 		if (pipe_output)
142035b9d88eSArnaldo Carvalho de Melo 			dup2(2, 1);
142135b9d88eSArnaldo Carvalho de Melo 
14220817df08SDavid Ahern 		signal(SIGTERM, SIG_DFL);
14230817df08SDavid Ahern 
142435b9d88eSArnaldo Carvalho de Melo 		close(child_ready_pipe[0]);
142535b9d88eSArnaldo Carvalho de Melo 		close(go_pipe[1]);
142635b9d88eSArnaldo Carvalho de Melo 		fcntl(go_pipe[0], F_SETFD, FD_CLOEXEC);
142735b9d88eSArnaldo Carvalho de Melo 
142835b9d88eSArnaldo Carvalho de Melo 		/*
1429a7672d1dSArnaldo Carvalho de Melo 		 * Change the name of this process not to confuse --exclude-perf users
1430a7672d1dSArnaldo Carvalho de Melo 		 * that sees 'perf' in the window up to the execvp() and thinks that
1431a7672d1dSArnaldo Carvalho de Melo 		 * perf samples are not being excluded.
1432a7672d1dSArnaldo Carvalho de Melo 		 */
1433a7672d1dSArnaldo Carvalho de Melo 		prctl(PR_SET_NAME, "perf-exec");
1434a7672d1dSArnaldo Carvalho de Melo 
1435a7672d1dSArnaldo Carvalho de Melo 		/*
143635b9d88eSArnaldo Carvalho de Melo 		 * Tell the parent we're ready to go
143735b9d88eSArnaldo Carvalho de Melo 		 */
143835b9d88eSArnaldo Carvalho de Melo 		close(child_ready_pipe[1]);
143935b9d88eSArnaldo Carvalho de Melo 
144035b9d88eSArnaldo Carvalho de Melo 		/*
144135b9d88eSArnaldo Carvalho de Melo 		 * Wait until the parent tells us to go.
144235b9d88eSArnaldo Carvalho de Melo 		 */
14435f1c4225SArnaldo Carvalho de Melo 		ret = read(go_pipe[0], &bf, 1);
14445f1c4225SArnaldo Carvalho de Melo 		/*
14455f1c4225SArnaldo Carvalho de Melo 		 * The parent will ask for the execvp() to be performed by
14465f1c4225SArnaldo Carvalho de Melo 		 * writing exactly one byte, in workload.cork_fd, usually via
14477b392ef0SArnaldo Carvalho de Melo 		 * evlist__start_workload().
14485f1c4225SArnaldo Carvalho de Melo 		 *
144920f86fc1SArnaldo Carvalho de Melo 		 * For cancelling the workload without actually running it,
14505f1c4225SArnaldo Carvalho de Melo 		 * the parent will just close workload.cork_fd, without writing
14515f1c4225SArnaldo Carvalho de Melo 		 * anything, i.e. read will return zero and we just exit()
14525f1c4225SArnaldo Carvalho de Melo 		 * here.
14535f1c4225SArnaldo Carvalho de Melo 		 */
14545f1c4225SArnaldo Carvalho de Melo 		if (ret != 1) {
14555f1c4225SArnaldo Carvalho de Melo 			if (ret == -1)
145635b9d88eSArnaldo Carvalho de Melo 				perror("unable to read pipe");
14575f1c4225SArnaldo Carvalho de Melo 			exit(ret);
14585f1c4225SArnaldo Carvalho de Melo 		}
145935b9d88eSArnaldo Carvalho de Melo 
146035b9d88eSArnaldo Carvalho de Melo 		execvp(argv[0], (char **)argv);
146135b9d88eSArnaldo Carvalho de Melo 
1462735f7e0bSArnaldo Carvalho de Melo 		if (exec_error) {
1463f33cbe72SArnaldo Carvalho de Melo 			union sigval val;
1464f33cbe72SArnaldo Carvalho de Melo 
1465f33cbe72SArnaldo Carvalho de Melo 			val.sival_int = errno;
1466f33cbe72SArnaldo Carvalho de Melo 			if (sigqueue(getppid(), SIGUSR1, val))
146735b9d88eSArnaldo Carvalho de Melo 				perror(argv[0]);
1468f33cbe72SArnaldo Carvalho de Melo 		} else
1469f33cbe72SArnaldo Carvalho de Melo 			perror(argv[0]);
147035b9d88eSArnaldo Carvalho de Melo 		exit(-1);
147135b9d88eSArnaldo Carvalho de Melo 	}
147235b9d88eSArnaldo Carvalho de Melo 
1473735f7e0bSArnaldo Carvalho de Melo 	if (exec_error) {
1474735f7e0bSArnaldo Carvalho de Melo 		struct sigaction act = {
1475735f7e0bSArnaldo Carvalho de Melo 			.sa_flags     = SA_SIGINFO,
1476735f7e0bSArnaldo Carvalho de Melo 			.sa_sigaction = exec_error,
1477735f7e0bSArnaldo Carvalho de Melo 		};
1478735f7e0bSArnaldo Carvalho de Melo 		sigaction(SIGUSR1, &act, NULL);
1479735f7e0bSArnaldo Carvalho de Melo 	}
1480735f7e0bSArnaldo Carvalho de Melo 
14811aaf63b1SArnaldo Carvalho de Melo 	if (target__none(target)) {
148203617c22SJiri Olsa 		if (evlist->core.threads == NULL) {
14831aaf63b1SArnaldo Carvalho de Melo 			fprintf(stderr, "FATAL: evlist->threads need to be set at this point (%s:%d).\n",
14841aaf63b1SArnaldo Carvalho de Melo 				__func__, __LINE__);
14851aaf63b1SArnaldo Carvalho de Melo 			goto out_close_pipes;
14861aaf63b1SArnaldo Carvalho de Melo 		}
148703617c22SJiri Olsa 		perf_thread_map__set_pid(evlist->core.threads, 0, evlist->workload.pid);
14881aaf63b1SArnaldo Carvalho de Melo 	}
148935b9d88eSArnaldo Carvalho de Melo 
149035b9d88eSArnaldo Carvalho de Melo 	close(child_ready_pipe[1]);
149135b9d88eSArnaldo Carvalho de Melo 	close(go_pipe[0]);
149235b9d88eSArnaldo Carvalho de Melo 	/*
149335b9d88eSArnaldo Carvalho de Melo 	 * wait for child to settle
149435b9d88eSArnaldo Carvalho de Melo 	 */
149535b9d88eSArnaldo Carvalho de Melo 	if (read(child_ready_pipe[0], &bf, 1) == -1) {
149635b9d88eSArnaldo Carvalho de Melo 		perror("unable to read pipe");
149735b9d88eSArnaldo Carvalho de Melo 		goto out_close_pipes;
149835b9d88eSArnaldo Carvalho de Melo 	}
149935b9d88eSArnaldo Carvalho de Melo 
1500bcf3145fSNamhyung Kim 	fcntl(go_pipe[1], F_SETFD, FD_CLOEXEC);
150135b9d88eSArnaldo Carvalho de Melo 	evlist->workload.cork_fd = go_pipe[1];
150235b9d88eSArnaldo Carvalho de Melo 	close(child_ready_pipe[0]);
150335b9d88eSArnaldo Carvalho de Melo 	return 0;
150435b9d88eSArnaldo Carvalho de Melo 
150535b9d88eSArnaldo Carvalho de Melo out_close_pipes:
150635b9d88eSArnaldo Carvalho de Melo 	close(go_pipe[0]);
150735b9d88eSArnaldo Carvalho de Melo 	close(go_pipe[1]);
150835b9d88eSArnaldo Carvalho de Melo out_close_ready_pipe:
150935b9d88eSArnaldo Carvalho de Melo 	close(child_ready_pipe[0]);
151035b9d88eSArnaldo Carvalho de Melo 	close(child_ready_pipe[1]);
151135b9d88eSArnaldo Carvalho de Melo 	return -1;
151235b9d88eSArnaldo Carvalho de Melo }
151335b9d88eSArnaldo Carvalho de Melo 
15147b392ef0SArnaldo Carvalho de Melo int evlist__start_workload(struct evlist *evlist)
151535b9d88eSArnaldo Carvalho de Melo {
151635b9d88eSArnaldo Carvalho de Melo 	if (evlist->workload.cork_fd > 0) {
1517b3824404SDavid Ahern 		char bf = 0;
1518bcf3145fSNamhyung Kim 		int ret;
151935b9d88eSArnaldo Carvalho de Melo 		/*
152035b9d88eSArnaldo Carvalho de Melo 		 * Remove the cork, let it rip!
152135b9d88eSArnaldo Carvalho de Melo 		 */
1522bcf3145fSNamhyung Kim 		ret = write(evlist->workload.cork_fd, &bf, 1);
1523bcf3145fSNamhyung Kim 		if (ret < 0)
1524e978be9eSSoramichi Akiyama 			perror("unable to write to pipe");
1525bcf3145fSNamhyung Kim 
1526bcf3145fSNamhyung Kim 		close(evlist->workload.cork_fd);
1527bcf3145fSNamhyung Kim 		return ret;
152835b9d88eSArnaldo Carvalho de Melo 	}
152935b9d88eSArnaldo Carvalho de Melo 
153035b9d88eSArnaldo Carvalho de Melo 	return 0;
153135b9d88eSArnaldo Carvalho de Melo }
1532cb0b29e0SArnaldo Carvalho de Melo 
15332a6599cdSArnaldo Carvalho de Melo int evlist__parse_sample(struct evlist *evlist, union perf_event *event, struct perf_sample *sample)
1534cb0b29e0SArnaldo Carvalho de Melo {
15353ccf8a7bSArnaldo Carvalho de Melo 	struct evsel *evsel = evlist__event2evsel(evlist, event);
15363461b65dSAdrian Hunter 	int ret;
153775562573SAdrian Hunter 
153875562573SAdrian Hunter 	if (!evsel)
153975562573SAdrian Hunter 		return -EFAULT;
15403461b65dSAdrian Hunter 	ret = evsel__parse_sample(evsel, event, sample);
15413461b65dSAdrian Hunter 	if (ret)
15423461b65dSAdrian Hunter 		return ret;
15433461b65dSAdrian Hunter 	if (perf_guest && sample->id) {
15443461b65dSAdrian Hunter 		struct perf_sample_id *sid = evlist__id2sid(evlist, sample->id);
15453461b65dSAdrian Hunter 
15463461b65dSAdrian Hunter 		if (sid) {
15473461b65dSAdrian Hunter 			sample->machine_pid = sid->machine_pid;
15483461b65dSAdrian Hunter 			sample->vcpu = sid->vcpu.cpu;
15493461b65dSAdrian Hunter 		}
15503461b65dSAdrian Hunter 	}
15513461b65dSAdrian Hunter 	return 0;
1552cb0b29e0SArnaldo Carvalho de Melo }
155378f067b3SArnaldo Carvalho de Melo 
15542a6599cdSArnaldo Carvalho de Melo int evlist__parse_sample_timestamp(struct evlist *evlist, union perf_event *event, u64 *timestamp)
155501468120SJiri Olsa {
15563ccf8a7bSArnaldo Carvalho de Melo 	struct evsel *evsel = evlist__event2evsel(evlist, event);
155701468120SJiri Olsa 
155801468120SJiri Olsa 	if (!evsel)
155901468120SJiri Olsa 		return -EFAULT;
15606b6017a2SArnaldo Carvalho de Melo 	return evsel__parse_sample_timestamp(evsel, event, timestamp);
156101468120SJiri Olsa }
156201468120SJiri Olsa 
1563d1f249ecSArnaldo Carvalho de Melo int evlist__strerror_open(struct evlist *evlist, int err, char *buf, size_t size)
1564a8f23d8fSArnaldo Carvalho de Melo {
1565a8f23d8fSArnaldo Carvalho de Melo 	int printed, value;
1566c8b5f2c9SArnaldo Carvalho de Melo 	char sbuf[STRERR_BUFSIZE], *emsg = str_error_r(err, sbuf, sizeof(sbuf));
1567a8f23d8fSArnaldo Carvalho de Melo 
1568a8f23d8fSArnaldo Carvalho de Melo 	switch (err) {
1569a8f23d8fSArnaldo Carvalho de Melo 	case EACCES:
1570a8f23d8fSArnaldo Carvalho de Melo 	case EPERM:
1571a8f23d8fSArnaldo Carvalho de Melo 		printed = scnprintf(buf, size,
1572a8f23d8fSArnaldo Carvalho de Melo 				    "Error:\t%s.\n"
1573a8f23d8fSArnaldo Carvalho de Melo 				    "Hint:\tCheck /proc/sys/kernel/perf_event_paranoid setting.", emsg);
1574a8f23d8fSArnaldo Carvalho de Melo 
15751a47245dSAdrian Hunter 		value = perf_event_paranoid();
1576a8f23d8fSArnaldo Carvalho de Melo 
1577a8f23d8fSArnaldo Carvalho de Melo 		printed += scnprintf(buf + printed, size - printed, "\nHint:\t");
1578a8f23d8fSArnaldo Carvalho de Melo 
1579a8f23d8fSArnaldo Carvalho de Melo 		if (value >= 2) {
1580a8f23d8fSArnaldo Carvalho de Melo 			printed += scnprintf(buf + printed, size - printed,
1581a8f23d8fSArnaldo Carvalho de Melo 					     "For your workloads it needs to be <= 1\nHint:\t");
1582a8f23d8fSArnaldo Carvalho de Melo 		}
1583a8f23d8fSArnaldo Carvalho de Melo 		printed += scnprintf(buf + printed, size - printed,
15845229e366SArnaldo Carvalho de Melo 				     "For system wide tracing it needs to be set to -1.\n");
1585a8f23d8fSArnaldo Carvalho de Melo 
1586a8f23d8fSArnaldo Carvalho de Melo 		printed += scnprintf(buf + printed, size - printed,
15875229e366SArnaldo Carvalho de Melo 				    "Hint:\tTry: 'sudo sh -c \"echo -1 > /proc/sys/kernel/perf_event_paranoid\"'\n"
15885229e366SArnaldo Carvalho de Melo 				    "Hint:\tThe current value is %d.", value);
1589a8f23d8fSArnaldo Carvalho de Melo 		break;
1590d9aade7fSArnaldo Carvalho de Melo 	case EINVAL: {
1591515dbe48SJiri Olsa 		struct evsel *first = evlist__first(evlist);
1592d9aade7fSArnaldo Carvalho de Melo 		int max_freq;
1593d9aade7fSArnaldo Carvalho de Melo 
1594d9aade7fSArnaldo Carvalho de Melo 		if (sysctl__read_int("kernel/perf_event_max_sample_rate", &max_freq) < 0)
1595d9aade7fSArnaldo Carvalho de Melo 			goto out_default;
1596d9aade7fSArnaldo Carvalho de Melo 
15971fc632ceSJiri Olsa 		if (first->core.attr.sample_freq < (u64)max_freq)
1598d9aade7fSArnaldo Carvalho de Melo 			goto out_default;
1599d9aade7fSArnaldo Carvalho de Melo 
1600d9aade7fSArnaldo Carvalho de Melo 		printed = scnprintf(buf, size,
1601d9aade7fSArnaldo Carvalho de Melo 				    "Error:\t%s.\n"
1602d9aade7fSArnaldo Carvalho de Melo 				    "Hint:\tCheck /proc/sys/kernel/perf_event_max_sample_rate.\n"
1603d9aade7fSArnaldo Carvalho de Melo 				    "Hint:\tThe current value is %d and %" PRIu64 " is being requested.",
16041fc632ceSJiri Olsa 				    emsg, max_freq, first->core.attr.sample_freq);
1605d9aade7fSArnaldo Carvalho de Melo 		break;
1606d9aade7fSArnaldo Carvalho de Melo 	}
1607a8f23d8fSArnaldo Carvalho de Melo 	default:
1608d9aade7fSArnaldo Carvalho de Melo out_default:
1609a8f23d8fSArnaldo Carvalho de Melo 		scnprintf(buf, size, "%s", emsg);
1610a8f23d8fSArnaldo Carvalho de Melo 		break;
1611a8f23d8fSArnaldo Carvalho de Melo 	}
1612a8f23d8fSArnaldo Carvalho de Melo 
1613a8f23d8fSArnaldo Carvalho de Melo 	return 0;
1614a8f23d8fSArnaldo Carvalho de Melo }
1615a025e4f0SAdrian Hunter 
1616d1f249ecSArnaldo Carvalho de Melo int evlist__strerror_mmap(struct evlist *evlist, int err, char *buf, size_t size)
1617956fa571SArnaldo Carvalho de Melo {
1618c8b5f2c9SArnaldo Carvalho de Melo 	char sbuf[STRERR_BUFSIZE], *emsg = str_error_r(err, sbuf, sizeof(sbuf));
1619f6fa4375SJiri Olsa 	int pages_attempted = evlist->core.mmap_len / 1024, pages_max_per_user, printed = 0;
1620956fa571SArnaldo Carvalho de Melo 
1621956fa571SArnaldo Carvalho de Melo 	switch (err) {
1622956fa571SArnaldo Carvalho de Melo 	case EPERM:
1623e5d4a290SArnaldo Carvalho de Melo 		sysctl__read_int("kernel/perf_event_mlock_kb", &pages_max_per_user);
1624e965bea1SArnaldo Carvalho de Melo 		printed += scnprintf(buf + printed, size - printed,
1625e965bea1SArnaldo Carvalho de Melo 				     "Error:\t%s.\n"
1626956fa571SArnaldo Carvalho de Melo 				     "Hint:\tCheck /proc/sys/kernel/perf_event_mlock_kb (%d kB) setting.\n"
1627e965bea1SArnaldo Carvalho de Melo 				     "Hint:\tTried using %zd kB.\n",
1628e5d4a290SArnaldo Carvalho de Melo 				     emsg, pages_max_per_user, pages_attempted);
1629e965bea1SArnaldo Carvalho de Melo 
1630e965bea1SArnaldo Carvalho de Melo 		if (pages_attempted >= pages_max_per_user) {
1631e965bea1SArnaldo Carvalho de Melo 			printed += scnprintf(buf + printed, size - printed,
1632e965bea1SArnaldo Carvalho de Melo 					     "Hint:\tTry 'sudo sh -c \"echo %d > /proc/sys/kernel/perf_event_mlock_kb\"', or\n",
1633e965bea1SArnaldo Carvalho de Melo 					     pages_max_per_user + pages_attempted);
1634e965bea1SArnaldo Carvalho de Melo 		}
1635e965bea1SArnaldo Carvalho de Melo 
1636e965bea1SArnaldo Carvalho de Melo 		printed += scnprintf(buf + printed, size - printed,
1637e965bea1SArnaldo Carvalho de Melo 				     "Hint:\tTry using a smaller -m/--mmap-pages value.");
1638956fa571SArnaldo Carvalho de Melo 		break;
1639956fa571SArnaldo Carvalho de Melo 	default:
1640956fa571SArnaldo Carvalho de Melo 		scnprintf(buf, size, "%s", emsg);
1641956fa571SArnaldo Carvalho de Melo 		break;
1642956fa571SArnaldo Carvalho de Melo 	}
1643956fa571SArnaldo Carvalho de Melo 
1644956fa571SArnaldo Carvalho de Melo 	return 0;
1645956fa571SArnaldo Carvalho de Melo }
1646956fa571SArnaldo Carvalho de Melo 
1647e414fd1aSArnaldo Carvalho de Melo void evlist__to_front(struct evlist *evlist, struct evsel *move_evsel)
1648a025e4f0SAdrian Hunter {
164932dcd021SJiri Olsa 	struct evsel *evsel, *n;
1650a025e4f0SAdrian Hunter 	LIST_HEAD(move);
1651a025e4f0SAdrian Hunter 
1652515dbe48SJiri Olsa 	if (move_evsel == evlist__first(evlist))
1653a025e4f0SAdrian Hunter 		return;
1654a025e4f0SAdrian Hunter 
1655e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry_safe(evlist, n, evsel) {
1656fba7c866SJiri Olsa 		if (evsel__leader(evsel) == evsel__leader(move_evsel))
1657b27c4eceSJiri Olsa 			list_move_tail(&evsel->core.node, &move);
1658a025e4f0SAdrian Hunter 	}
1659a025e4f0SAdrian Hunter 
1660ce9036a6SJiri Olsa 	list_splice(&move, &evlist->core.entries);
1661a025e4f0SAdrian Hunter }
166260b0896cSAdrian Hunter 
1663e80db255SArnaldo Carvalho de Melo struct evsel *evlist__get_tracking_event(struct evlist *evlist)
1664442ad225SAdrian Hunter {
1665442ad225SAdrian Hunter 	struct evsel *evsel;
1666442ad225SAdrian Hunter 
1667442ad225SAdrian Hunter 	evlist__for_each_entry(evlist, evsel) {
1668442ad225SAdrian Hunter 		if (evsel->tracking)
1669442ad225SAdrian Hunter 			return evsel;
1670442ad225SAdrian Hunter 	}
1671442ad225SAdrian Hunter 
1672442ad225SAdrian Hunter 	return evlist__first(evlist);
1673442ad225SAdrian Hunter }
1674442ad225SAdrian Hunter 
1675e80db255SArnaldo Carvalho de Melo void evlist__set_tracking_event(struct evlist *evlist, struct evsel *tracking_evsel)
167660b0896cSAdrian Hunter {
167732dcd021SJiri Olsa 	struct evsel *evsel;
167860b0896cSAdrian Hunter 
167960b0896cSAdrian Hunter 	if (tracking_evsel->tracking)
168060b0896cSAdrian Hunter 		return;
168160b0896cSAdrian Hunter 
1682e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, evsel) {
168360b0896cSAdrian Hunter 		if (evsel != tracking_evsel)
168460b0896cSAdrian Hunter 			evsel->tracking = false;
168560b0896cSAdrian Hunter 	}
168660b0896cSAdrian Hunter 
168760b0896cSAdrian Hunter 	tracking_evsel->tracking = true;
168860b0896cSAdrian Hunter }
16897630b3e2SWang Nan 
1690b02736f7SArnaldo Carvalho de Melo struct evsel *evlist__find_evsel_by_str(struct evlist *evlist, const char *str)
16917630b3e2SWang Nan {
169232dcd021SJiri Olsa 	struct evsel *evsel;
16937630b3e2SWang Nan 
1694e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, evsel) {
16957630b3e2SWang Nan 		if (!evsel->name)
16967630b3e2SWang Nan 			continue;
16977630b3e2SWang Nan 		if (strcmp(str, evsel->name) == 0)
16987630b3e2SWang Nan 			return evsel;
16997630b3e2SWang Nan 	}
17007630b3e2SWang Nan 
17017630b3e2SWang Nan 	return NULL;
17027630b3e2SWang Nan }
170354cc54deSWang Nan 
1704ade9d208SArnaldo Carvalho de Melo void evlist__toggle_bkw_mmap(struct evlist *evlist, enum bkw_mmap_state state)
170554cc54deSWang Nan {
170654cc54deSWang Nan 	enum bkw_mmap_state old_state = evlist->bkw_mmap_state;
170754cc54deSWang Nan 	enum action {
170854cc54deSWang Nan 		NONE,
170954cc54deSWang Nan 		PAUSE,
171054cc54deSWang Nan 		RESUME,
171154cc54deSWang Nan 	} action = NONE;
171254cc54deSWang Nan 
17130b72d69aSWang Nan 	if (!evlist->overwrite_mmap)
171454cc54deSWang Nan 		return;
171554cc54deSWang Nan 
171654cc54deSWang Nan 	switch (old_state) {
171754cc54deSWang Nan 	case BKW_MMAP_NOTREADY: {
171854cc54deSWang Nan 		if (state != BKW_MMAP_RUNNING)
1719dd8bd53aSLuis de Bethencourt 			goto state_err;
172054cc54deSWang Nan 		break;
172154cc54deSWang Nan 	}
172254cc54deSWang Nan 	case BKW_MMAP_RUNNING: {
172354cc54deSWang Nan 		if (state != BKW_MMAP_DATA_PENDING)
172454cc54deSWang Nan 			goto state_err;
172554cc54deSWang Nan 		action = PAUSE;
172654cc54deSWang Nan 		break;
172754cc54deSWang Nan 	}
172854cc54deSWang Nan 	case BKW_MMAP_DATA_PENDING: {
172954cc54deSWang Nan 		if (state != BKW_MMAP_EMPTY)
173054cc54deSWang Nan 			goto state_err;
173154cc54deSWang Nan 		break;
173254cc54deSWang Nan 	}
173354cc54deSWang Nan 	case BKW_MMAP_EMPTY: {
173454cc54deSWang Nan 		if (state != BKW_MMAP_RUNNING)
173554cc54deSWang Nan 			goto state_err;
173654cc54deSWang Nan 		action = RESUME;
173754cc54deSWang Nan 		break;
173854cc54deSWang Nan 	}
173954cc54deSWang Nan 	default:
174054cc54deSWang Nan 		WARN_ONCE(1, "Shouldn't get there\n");
174154cc54deSWang Nan 	}
174254cc54deSWang Nan 
174354cc54deSWang Nan 	evlist->bkw_mmap_state = state;
174454cc54deSWang Nan 
174554cc54deSWang Nan 	switch (action) {
174654cc54deSWang Nan 	case PAUSE:
17470a60b339SArnaldo Carvalho de Melo 		evlist__pause(evlist);
174854cc54deSWang Nan 		break;
174954cc54deSWang Nan 	case RESUME:
17500a60b339SArnaldo Carvalho de Melo 		evlist__resume(evlist);
175154cc54deSWang Nan 		break;
175254cc54deSWang Nan 	case NONE:
175354cc54deSWang Nan 	default:
175454cc54deSWang Nan 		break;
175554cc54deSWang Nan 	}
175654cc54deSWang Nan 
175754cc54deSWang Nan state_err:
175854cc54deSWang Nan 	return;
175954cc54deSWang Nan }
176007d6f446SArnaldo Carvalho de Melo 
176178e1bc25SArnaldo Carvalho de Melo bool evlist__exclude_kernel(struct evlist *evlist)
176207d6f446SArnaldo Carvalho de Melo {
176332dcd021SJiri Olsa 	struct evsel *evsel;
176407d6f446SArnaldo Carvalho de Melo 
176507d6f446SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, evsel) {
17661fc632ceSJiri Olsa 		if (!evsel->core.attr.exclude_kernel)
176707d6f446SArnaldo Carvalho de Melo 			return false;
176807d6f446SArnaldo Carvalho de Melo 	}
176907d6f446SArnaldo Carvalho de Melo 
177007d6f446SArnaldo Carvalho de Melo 	return true;
177107d6f446SArnaldo Carvalho de Melo }
1772e2bdbe80SJin Yao 
1773e2bdbe80SJin Yao /*
1774e2bdbe80SJin Yao  * Events in data file are not collect in groups, but we still want
1775e2bdbe80SJin Yao  * the group display. Set the artificial group and set the leader's
1776e2bdbe80SJin Yao  * forced_leader flag to notify the display code.
1777e2bdbe80SJin Yao  */
177864b4778bSArnaldo Carvalho de Melo void evlist__force_leader(struct evlist *evlist)
1779e2bdbe80SJin Yao {
1780*9d2dc632SIan Rogers 	if (evlist__nr_groups(evlist) == 0) {
1781515dbe48SJiri Olsa 		struct evsel *leader = evlist__first(evlist);
1782e2bdbe80SJin Yao 
1783a622eafaSArnaldo Carvalho de Melo 		evlist__set_leader(evlist);
1784e2bdbe80SJin Yao 		leader->forced_leader = true;
1785e2bdbe80SJin Yao 	}
1786e2bdbe80SJin Yao }
1787c3537fc2SAndi Kleen 
178864b4778bSArnaldo Carvalho de Melo struct evsel *evlist__reset_weak_group(struct evlist *evsel_list, struct evsel *evsel, bool close)
1789c3537fc2SAndi Kleen {
179032dcd021SJiri Olsa 	struct evsel *c2, *leader;
1791c3537fc2SAndi Kleen 	bool is_open = true;
1792c3537fc2SAndi Kleen 
1793fba7c866SJiri Olsa 	leader = evsel__leader(evsel);
1794fba7c866SJiri Olsa 
1795c3537fc2SAndi Kleen 	pr_debug("Weak group for %s/%d failed\n",
17965643b1a5SJiri Olsa 			leader->name, leader->core.nr_members);
1797c3537fc2SAndi Kleen 
1798c3537fc2SAndi Kleen 	/*
1799c3537fc2SAndi Kleen 	 * for_each_group_member doesn't work here because it doesn't
1800c3537fc2SAndi Kleen 	 * include the first entry.
1801c3537fc2SAndi Kleen 	 */
1802c3537fc2SAndi Kleen 	evlist__for_each_entry(evsel_list, c2) {
1803c3537fc2SAndi Kleen 		if (c2 == evsel)
1804c3537fc2SAndi Kleen 			is_open = false;
1805fba7c866SJiri Olsa 		if (evsel__has_leader(c2, leader)) {
18064804e011SAndi Kleen 			if (is_open && close)
18075a40e199SAndi Kleen 				perf_evsel__close(&c2->core);
1808d98079c0SIan Rogers 			/*
1809d98079c0SIan Rogers 			 * We want to close all members of the group and reopen
1810d98079c0SIan Rogers 			 * them. Some events, like Intel topdown, require being
1811d98079c0SIan Rogers 			 * in a group and so keep these in the group.
1812d98079c0SIan Rogers 			 */
1813e8f4f794SKan Liang 			evsel__remove_from_group(c2, leader);
1814d98079c0SIan Rogers 
18154804e011SAndi Kleen 			/*
18164804e011SAndi Kleen 			 * Set this for all former members of the group
18174804e011SAndi Kleen 			 * to indicate they get reopened.
18184804e011SAndi Kleen 			 */
18194804e011SAndi Kleen 			c2->reset_group = true;
1820c3537fc2SAndi Kleen 		}
1821c3537fc2SAndi Kleen 	}
1822d98079c0SIan Rogers 	/* Reset the leader count if all entries were removed. */
1823d98079c0SIan Rogers 	if (leader->core.nr_members == 1)
1824d98079c0SIan Rogers 		leader->core.nr_members = 0;
1825c3537fc2SAndi Kleen 	return leader;
1826c3537fc2SAndi Kleen }
1827ec886bf5SAlexey Budankov 
1828a8fcbd26SAdrian Hunter static int evlist__parse_control_fifo(const char *str, int *ctl_fd, int *ctl_fd_ack, bool *ctl_fd_close)
1829a8fcbd26SAdrian Hunter {
1830a8fcbd26SAdrian Hunter 	char *s, *p;
1831a8fcbd26SAdrian Hunter 	int ret = 0, fd;
1832a8fcbd26SAdrian Hunter 
1833a8fcbd26SAdrian Hunter 	if (strncmp(str, "fifo:", 5))
1834a8fcbd26SAdrian Hunter 		return -EINVAL;
1835a8fcbd26SAdrian Hunter 
1836a8fcbd26SAdrian Hunter 	str += 5;
1837a8fcbd26SAdrian Hunter 	if (!*str || *str == ',')
1838a8fcbd26SAdrian Hunter 		return -EINVAL;
1839a8fcbd26SAdrian Hunter 
1840a8fcbd26SAdrian Hunter 	s = strdup(str);
1841a8fcbd26SAdrian Hunter 	if (!s)
1842a8fcbd26SAdrian Hunter 		return -ENOMEM;
1843a8fcbd26SAdrian Hunter 
1844a8fcbd26SAdrian Hunter 	p = strchr(s, ',');
1845a8fcbd26SAdrian Hunter 	if (p)
1846a8fcbd26SAdrian Hunter 		*p = '\0';
1847a8fcbd26SAdrian Hunter 
1848a8fcbd26SAdrian Hunter 	/*
1849a8fcbd26SAdrian Hunter 	 * O_RDWR avoids POLLHUPs which is necessary to allow the other
1850a8fcbd26SAdrian Hunter 	 * end of a FIFO to be repeatedly opened and closed.
1851a8fcbd26SAdrian Hunter 	 */
1852a8fcbd26SAdrian Hunter 	fd = open(s, O_RDWR | O_NONBLOCK | O_CLOEXEC);
1853a8fcbd26SAdrian Hunter 	if (fd < 0) {
1854a8fcbd26SAdrian Hunter 		pr_err("Failed to open '%s'\n", s);
1855a8fcbd26SAdrian Hunter 		ret = -errno;
1856a8fcbd26SAdrian Hunter 		goto out_free;
1857a8fcbd26SAdrian Hunter 	}
1858a8fcbd26SAdrian Hunter 	*ctl_fd = fd;
1859a8fcbd26SAdrian Hunter 	*ctl_fd_close = true;
1860a8fcbd26SAdrian Hunter 
1861a8fcbd26SAdrian Hunter 	if (p && *++p) {
1862a8fcbd26SAdrian Hunter 		/* O_RDWR | O_NONBLOCK means the other end need not be open */
1863a8fcbd26SAdrian Hunter 		fd = open(p, O_RDWR | O_NONBLOCK | O_CLOEXEC);
1864a8fcbd26SAdrian Hunter 		if (fd < 0) {
1865a8fcbd26SAdrian Hunter 			pr_err("Failed to open '%s'\n", p);
1866a8fcbd26SAdrian Hunter 			ret = -errno;
1867a8fcbd26SAdrian Hunter 			goto out_free;
1868a8fcbd26SAdrian Hunter 		}
1869a8fcbd26SAdrian Hunter 		*ctl_fd_ack = fd;
1870a8fcbd26SAdrian Hunter 	}
1871a8fcbd26SAdrian Hunter 
1872a8fcbd26SAdrian Hunter out_free:
1873a8fcbd26SAdrian Hunter 	free(s);
1874a8fcbd26SAdrian Hunter 	return ret;
1875a8fcbd26SAdrian Hunter }
1876a8fcbd26SAdrian Hunter 
1877a8fcbd26SAdrian Hunter int evlist__parse_control(const char *str, int *ctl_fd, int *ctl_fd_ack, bool *ctl_fd_close)
18789864a66dSAdrian Hunter {
18799864a66dSAdrian Hunter 	char *comma = NULL, *endptr = NULL;
18809864a66dSAdrian Hunter 
1881a8fcbd26SAdrian Hunter 	*ctl_fd_close = false;
1882a8fcbd26SAdrian Hunter 
18839864a66dSAdrian Hunter 	if (strncmp(str, "fd:", 3))
1884a8fcbd26SAdrian Hunter 		return evlist__parse_control_fifo(str, ctl_fd, ctl_fd_ack, ctl_fd_close);
18859864a66dSAdrian Hunter 
18869864a66dSAdrian Hunter 	*ctl_fd = strtoul(&str[3], &endptr, 0);
18879864a66dSAdrian Hunter 	if (endptr == &str[3])
18889864a66dSAdrian Hunter 		return -EINVAL;
18899864a66dSAdrian Hunter 
18909864a66dSAdrian Hunter 	comma = strchr(str, ',');
18919864a66dSAdrian Hunter 	if (comma) {
18929864a66dSAdrian Hunter 		if (endptr != comma)
18939864a66dSAdrian Hunter 			return -EINVAL;
18949864a66dSAdrian Hunter 
18959864a66dSAdrian Hunter 		*ctl_fd_ack = strtoul(comma + 1, &endptr, 0);
18969864a66dSAdrian Hunter 		if (endptr == comma + 1 || *endptr != '\0')
18979864a66dSAdrian Hunter 			return -EINVAL;
18989864a66dSAdrian Hunter 	}
18999864a66dSAdrian Hunter 
19009864a66dSAdrian Hunter 	return 0;
19019864a66dSAdrian Hunter }
19029864a66dSAdrian Hunter 
1903ee7fe31eSAdrian Hunter void evlist__close_control(int ctl_fd, int ctl_fd_ack, bool *ctl_fd_close)
1904ee7fe31eSAdrian Hunter {
1905ee7fe31eSAdrian Hunter 	if (*ctl_fd_close) {
1906ee7fe31eSAdrian Hunter 		*ctl_fd_close = false;
1907ee7fe31eSAdrian Hunter 		close(ctl_fd);
1908ee7fe31eSAdrian Hunter 		if (ctl_fd_ack >= 0)
1909ee7fe31eSAdrian Hunter 			close(ctl_fd_ack);
1910ee7fe31eSAdrian Hunter 	}
1911ee7fe31eSAdrian Hunter }
1912ee7fe31eSAdrian Hunter 
1913ec886bf5SAlexey Budankov int evlist__initialize_ctlfd(struct evlist *evlist, int fd, int ack)
1914ec886bf5SAlexey Budankov {
1915ec886bf5SAlexey Budankov 	if (fd == -1) {
1916ec886bf5SAlexey Budankov 		pr_debug("Control descriptor is not initialized\n");
1917ec886bf5SAlexey Budankov 		return 0;
1918ec886bf5SAlexey Budankov 	}
1919ec886bf5SAlexey Budankov 
1920ec886bf5SAlexey Budankov 	evlist->ctl_fd.pos = perf_evlist__add_pollfd(&evlist->core, fd, NULL, POLLIN,
1921feff0b61SAdrian Hunter 						     fdarray_flag__nonfilterable |
1922feff0b61SAdrian Hunter 						     fdarray_flag__non_perf_event);
1923ec886bf5SAlexey Budankov 	if (evlist->ctl_fd.pos < 0) {
1924ec886bf5SAlexey Budankov 		evlist->ctl_fd.pos = -1;
1925ec886bf5SAlexey Budankov 		pr_err("Failed to add ctl fd entry: %m\n");
1926ec886bf5SAlexey Budankov 		return -1;
1927ec886bf5SAlexey Budankov 	}
1928ec886bf5SAlexey Budankov 
1929ec886bf5SAlexey Budankov 	evlist->ctl_fd.fd = fd;
1930ec886bf5SAlexey Budankov 	evlist->ctl_fd.ack = ack;
1931ec886bf5SAlexey Budankov 
1932ec886bf5SAlexey Budankov 	return 0;
1933ec886bf5SAlexey Budankov }
1934ec886bf5SAlexey Budankov 
1935ec886bf5SAlexey Budankov bool evlist__ctlfd_initialized(struct evlist *evlist)
1936ec886bf5SAlexey Budankov {
1937ec886bf5SAlexey Budankov 	return evlist->ctl_fd.pos >= 0;
1938ec886bf5SAlexey Budankov }
1939ec886bf5SAlexey Budankov 
1940ec886bf5SAlexey Budankov int evlist__finalize_ctlfd(struct evlist *evlist)
1941ec886bf5SAlexey Budankov {
1942ec886bf5SAlexey Budankov 	struct pollfd *entries = evlist->core.pollfd.entries;
1943ec886bf5SAlexey Budankov 
1944ec886bf5SAlexey Budankov 	if (!evlist__ctlfd_initialized(evlist))
1945ec886bf5SAlexey Budankov 		return 0;
1946ec886bf5SAlexey Budankov 
1947ec886bf5SAlexey Budankov 	entries[evlist->ctl_fd.pos].fd = -1;
1948ec886bf5SAlexey Budankov 	entries[evlist->ctl_fd.pos].events = 0;
1949ec886bf5SAlexey Budankov 	entries[evlist->ctl_fd.pos].revents = 0;
1950ec886bf5SAlexey Budankov 
1951ec886bf5SAlexey Budankov 	evlist->ctl_fd.pos = -1;
1952ec886bf5SAlexey Budankov 	evlist->ctl_fd.ack = -1;
1953ec886bf5SAlexey Budankov 	evlist->ctl_fd.fd = -1;
1954ec886bf5SAlexey Budankov 
1955ec886bf5SAlexey Budankov 	return 0;
1956ec886bf5SAlexey Budankov }
1957ec886bf5SAlexey Budankov 
1958ec886bf5SAlexey Budankov static int evlist__ctlfd_recv(struct evlist *evlist, enum evlist_ctl_cmd *cmd,
1959ec886bf5SAlexey Budankov 			      char *cmd_data, size_t data_size)
1960ec886bf5SAlexey Budankov {
1961ec886bf5SAlexey Budankov 	int err;
1962ec886bf5SAlexey Budankov 	char c;
1963ec886bf5SAlexey Budankov 	size_t bytes_read = 0;
1964ec886bf5SAlexey Budankov 
196540db8ff5SAdrian Hunter 	*cmd = EVLIST_CTL_CMD_UNSUPPORTED;
1966ec886bf5SAlexey Budankov 	memset(cmd_data, 0, data_size);
1967ec886bf5SAlexey Budankov 	data_size--;
1968ec886bf5SAlexey Budankov 
1969ec886bf5SAlexey Budankov 	do {
1970ec886bf5SAlexey Budankov 		err = read(evlist->ctl_fd.fd, &c, 1);
1971ec886bf5SAlexey Budankov 		if (err > 0) {
1972ec886bf5SAlexey Budankov 			if (c == '\n' || c == '\0')
1973ec886bf5SAlexey Budankov 				break;
1974ec886bf5SAlexey Budankov 			cmd_data[bytes_read++] = c;
1975ec886bf5SAlexey Budankov 			if (bytes_read == data_size)
1976ec886bf5SAlexey Budankov 				break;
197740db8ff5SAdrian Hunter 			continue;
197840db8ff5SAdrian Hunter 		} else if (err == -1) {
197940db8ff5SAdrian Hunter 			if (errno == EINTR)
198040db8ff5SAdrian Hunter 				continue;
198140db8ff5SAdrian Hunter 			if (errno == EAGAIN || errno == EWOULDBLOCK)
198240db8ff5SAdrian Hunter 				err = 0;
198340db8ff5SAdrian Hunter 			else
1984ec886bf5SAlexey Budankov 				pr_err("Failed to read from ctlfd %d: %m\n", evlist->ctl_fd.fd);
1985ec886bf5SAlexey Budankov 		}
198640db8ff5SAdrian Hunter 		break;
1987ec886bf5SAlexey Budankov 	} while (1);
1988ec886bf5SAlexey Budankov 
1989ec886bf5SAlexey Budankov 	pr_debug("Message from ctl_fd: \"%s%s\"\n", cmd_data,
1990ec886bf5SAlexey Budankov 		 bytes_read == data_size ? "" : c == '\n' ? "\\n" : "\\0");
1991ec886bf5SAlexey Budankov 
199240db8ff5SAdrian Hunter 	if (bytes_read > 0) {
1993ec886bf5SAlexey Budankov 		if (!strncmp(cmd_data, EVLIST_CTL_CMD_ENABLE_TAG,
1994ec886bf5SAlexey Budankov 			     (sizeof(EVLIST_CTL_CMD_ENABLE_TAG)-1))) {
1995ec886bf5SAlexey Budankov 			*cmd = EVLIST_CTL_CMD_ENABLE;
1996ec886bf5SAlexey Budankov 		} else if (!strncmp(cmd_data, EVLIST_CTL_CMD_DISABLE_TAG,
1997ec886bf5SAlexey Budankov 				    (sizeof(EVLIST_CTL_CMD_DISABLE_TAG)-1))) {
1998ec886bf5SAlexey Budankov 			*cmd = EVLIST_CTL_CMD_DISABLE;
1999d20aff15SAdrian Hunter 		} else if (!strncmp(cmd_data, EVLIST_CTL_CMD_SNAPSHOT_TAG,
2000d20aff15SAdrian Hunter 				    (sizeof(EVLIST_CTL_CMD_SNAPSHOT_TAG)-1))) {
2001d20aff15SAdrian Hunter 			*cmd = EVLIST_CTL_CMD_SNAPSHOT;
2002d20aff15SAdrian Hunter 			pr_debug("is snapshot\n");
2003142544a9SJiri Olsa 		} else if (!strncmp(cmd_data, EVLIST_CTL_CMD_EVLIST_TAG,
2004142544a9SJiri Olsa 				    (sizeof(EVLIST_CTL_CMD_EVLIST_TAG)-1))) {
2005142544a9SJiri Olsa 			*cmd = EVLIST_CTL_CMD_EVLIST;
2006f186cd61SJiri Olsa 		} else if (!strncmp(cmd_data, EVLIST_CTL_CMD_STOP_TAG,
2007f186cd61SJiri Olsa 				    (sizeof(EVLIST_CTL_CMD_STOP_TAG)-1))) {
2008f186cd61SJiri Olsa 			*cmd = EVLIST_CTL_CMD_STOP;
200947fddcb4SJiri Olsa 		} else if (!strncmp(cmd_data, EVLIST_CTL_CMD_PING_TAG,
201047fddcb4SJiri Olsa 				    (sizeof(EVLIST_CTL_CMD_PING_TAG)-1))) {
201147fddcb4SJiri Olsa 			*cmd = EVLIST_CTL_CMD_PING;
2012ec886bf5SAlexey Budankov 		}
2013ec886bf5SAlexey Budankov 	}
2014ec886bf5SAlexey Budankov 
201540db8ff5SAdrian Hunter 	return bytes_read ? (int)bytes_read : err;
2016ec886bf5SAlexey Budankov }
2017ec886bf5SAlexey Budankov 
2018d20aff15SAdrian Hunter int evlist__ctlfd_ack(struct evlist *evlist)
2019ec886bf5SAlexey Budankov {
2020ec886bf5SAlexey Budankov 	int err;
2021ec886bf5SAlexey Budankov 
2022ec886bf5SAlexey Budankov 	if (evlist->ctl_fd.ack == -1)
2023ec886bf5SAlexey Budankov 		return 0;
2024ec886bf5SAlexey Budankov 
2025ec886bf5SAlexey Budankov 	err = write(evlist->ctl_fd.ack, EVLIST_CTL_CMD_ACK_TAG,
2026ec886bf5SAlexey Budankov 		    sizeof(EVLIST_CTL_CMD_ACK_TAG));
2027ec886bf5SAlexey Budankov 	if (err == -1)
2028ec886bf5SAlexey Budankov 		pr_err("failed to write to ctl_ack_fd %d: %m\n", evlist->ctl_fd.ack);
2029ec886bf5SAlexey Budankov 
2030ec886bf5SAlexey Budankov 	return err;
2031ec886bf5SAlexey Budankov }
2032ec886bf5SAlexey Budankov 
2033991ae4ebSJiri Olsa static int get_cmd_arg(char *cmd_data, size_t cmd_size, char **arg)
2034991ae4ebSJiri Olsa {
2035991ae4ebSJiri Olsa 	char *data = cmd_data + cmd_size;
2036991ae4ebSJiri Olsa 
2037991ae4ebSJiri Olsa 	/* no argument */
2038991ae4ebSJiri Olsa 	if (!*data)
2039991ae4ebSJiri Olsa 		return 0;
2040991ae4ebSJiri Olsa 
2041991ae4ebSJiri Olsa 	/* there's argument */
2042991ae4ebSJiri Olsa 	if (*data == ' ') {
2043991ae4ebSJiri Olsa 		*arg = data + 1;
2044991ae4ebSJiri Olsa 		return 1;
2045991ae4ebSJiri Olsa 	}
2046991ae4ebSJiri Olsa 
2047991ae4ebSJiri Olsa 	/* malformed */
2048991ae4ebSJiri Olsa 	return -1;
2049991ae4ebSJiri Olsa }
2050991ae4ebSJiri Olsa 
2051991ae4ebSJiri Olsa static int evlist__ctlfd_enable(struct evlist *evlist, char *cmd_data, bool enable)
2052991ae4ebSJiri Olsa {
2053991ae4ebSJiri Olsa 	struct evsel *evsel;
2054991ae4ebSJiri Olsa 	char *name;
2055991ae4ebSJiri Olsa 	int err;
2056991ae4ebSJiri Olsa 
2057991ae4ebSJiri Olsa 	err = get_cmd_arg(cmd_data,
2058991ae4ebSJiri Olsa 			  enable ? sizeof(EVLIST_CTL_CMD_ENABLE_TAG) - 1 :
2059991ae4ebSJiri Olsa 				   sizeof(EVLIST_CTL_CMD_DISABLE_TAG) - 1,
2060991ae4ebSJiri Olsa 			  &name);
2061991ae4ebSJiri Olsa 	if (err < 0) {
2062991ae4ebSJiri Olsa 		pr_info("failed: wrong command\n");
2063991ae4ebSJiri Olsa 		return -1;
2064991ae4ebSJiri Olsa 	}
2065991ae4ebSJiri Olsa 
2066991ae4ebSJiri Olsa 	if (err) {
2067991ae4ebSJiri Olsa 		evsel = evlist__find_evsel_by_str(evlist, name);
2068991ae4ebSJiri Olsa 		if (evsel) {
2069991ae4ebSJiri Olsa 			if (enable)
2070991ae4ebSJiri Olsa 				evlist__enable_evsel(evlist, name);
2071991ae4ebSJiri Olsa 			else
2072991ae4ebSJiri Olsa 				evlist__disable_evsel(evlist, name);
2073991ae4ebSJiri Olsa 			pr_info("Event %s %s\n", evsel->name,
2074991ae4ebSJiri Olsa 				enable ? "enabled" : "disabled");
2075991ae4ebSJiri Olsa 		} else {
2076991ae4ebSJiri Olsa 			pr_info("failed: can't find '%s' event\n", name);
2077991ae4ebSJiri Olsa 		}
2078991ae4ebSJiri Olsa 	} else {
2079991ae4ebSJiri Olsa 		if (enable) {
2080991ae4ebSJiri Olsa 			evlist__enable(evlist);
2081991ae4ebSJiri Olsa 			pr_info(EVLIST_ENABLED_MSG);
2082991ae4ebSJiri Olsa 		} else {
2083991ae4ebSJiri Olsa 			evlist__disable(evlist);
2084991ae4ebSJiri Olsa 			pr_info(EVLIST_DISABLED_MSG);
2085991ae4ebSJiri Olsa 		}
2086991ae4ebSJiri Olsa 	}
2087991ae4ebSJiri Olsa 
2088991ae4ebSJiri Olsa 	return 0;
2089991ae4ebSJiri Olsa }
2090991ae4ebSJiri Olsa 
2091142544a9SJiri Olsa static int evlist__ctlfd_list(struct evlist *evlist, char *cmd_data)
2092142544a9SJiri Olsa {
2093142544a9SJiri Olsa 	struct perf_attr_details details = { .verbose = false, };
2094142544a9SJiri Olsa 	struct evsel *evsel;
2095142544a9SJiri Olsa 	char *arg;
2096142544a9SJiri Olsa 	int err;
2097142544a9SJiri Olsa 
2098142544a9SJiri Olsa 	err = get_cmd_arg(cmd_data,
2099142544a9SJiri Olsa 			  sizeof(EVLIST_CTL_CMD_EVLIST_TAG) - 1,
2100142544a9SJiri Olsa 			  &arg);
2101142544a9SJiri Olsa 	if (err < 0) {
2102142544a9SJiri Olsa 		pr_info("failed: wrong command\n");
2103142544a9SJiri Olsa 		return -1;
2104142544a9SJiri Olsa 	}
2105142544a9SJiri Olsa 
2106142544a9SJiri Olsa 	if (err) {
2107142544a9SJiri Olsa 		if (!strcmp(arg, "-v")) {
2108142544a9SJiri Olsa 			details.verbose = true;
2109142544a9SJiri Olsa 		} else if (!strcmp(arg, "-g")) {
2110142544a9SJiri Olsa 			details.event_group = true;
2111142544a9SJiri Olsa 		} else if (!strcmp(arg, "-F")) {
2112142544a9SJiri Olsa 			details.freq = true;
2113142544a9SJiri Olsa 		} else {
2114142544a9SJiri Olsa 			pr_info("failed: wrong command\n");
2115142544a9SJiri Olsa 			return -1;
2116142544a9SJiri Olsa 		}
2117142544a9SJiri Olsa 	}
2118142544a9SJiri Olsa 
2119142544a9SJiri Olsa 	evlist__for_each_entry(evlist, evsel)
2120142544a9SJiri Olsa 		evsel__fprintf(evsel, &details, stderr);
2121142544a9SJiri Olsa 
2122142544a9SJiri Olsa 	return 0;
2123142544a9SJiri Olsa }
2124142544a9SJiri Olsa 
2125ec886bf5SAlexey Budankov int evlist__ctlfd_process(struct evlist *evlist, enum evlist_ctl_cmd *cmd)
2126ec886bf5SAlexey Budankov {
2127ec886bf5SAlexey Budankov 	int err = 0;
2128ec886bf5SAlexey Budankov 	char cmd_data[EVLIST_CTL_CMD_MAX_LEN];
2129ec886bf5SAlexey Budankov 	int ctlfd_pos = evlist->ctl_fd.pos;
2130ec886bf5SAlexey Budankov 	struct pollfd *entries = evlist->core.pollfd.entries;
2131ec886bf5SAlexey Budankov 
2132ec886bf5SAlexey Budankov 	if (!evlist__ctlfd_initialized(evlist) || !entries[ctlfd_pos].revents)
2133ec886bf5SAlexey Budankov 		return 0;
2134ec886bf5SAlexey Budankov 
2135ec886bf5SAlexey Budankov 	if (entries[ctlfd_pos].revents & POLLIN) {
2136ec886bf5SAlexey Budankov 		err = evlist__ctlfd_recv(evlist, cmd, cmd_data,
2137ec886bf5SAlexey Budankov 					 EVLIST_CTL_CMD_MAX_LEN);
2138ec886bf5SAlexey Budankov 		if (err > 0) {
2139ec886bf5SAlexey Budankov 			switch (*cmd) {
2140ec886bf5SAlexey Budankov 			case EVLIST_CTL_CMD_ENABLE:
2141ec886bf5SAlexey Budankov 			case EVLIST_CTL_CMD_DISABLE:
2142991ae4ebSJiri Olsa 				err = evlist__ctlfd_enable(evlist, cmd_data,
2143991ae4ebSJiri Olsa 							   *cmd == EVLIST_CTL_CMD_ENABLE);
2144ec886bf5SAlexey Budankov 				break;
2145142544a9SJiri Olsa 			case EVLIST_CTL_CMD_EVLIST:
2146142544a9SJiri Olsa 				err = evlist__ctlfd_list(evlist, cmd_data);
2147142544a9SJiri Olsa 				break;
2148d20aff15SAdrian Hunter 			case EVLIST_CTL_CMD_SNAPSHOT:
2149f186cd61SJiri Olsa 			case EVLIST_CTL_CMD_STOP:
215047fddcb4SJiri Olsa 			case EVLIST_CTL_CMD_PING:
2151d20aff15SAdrian Hunter 				break;
2152ec886bf5SAlexey Budankov 			case EVLIST_CTL_CMD_ACK:
2153ec886bf5SAlexey Budankov 			case EVLIST_CTL_CMD_UNSUPPORTED:
2154ec886bf5SAlexey Budankov 			default:
2155ec886bf5SAlexey Budankov 				pr_debug("ctlfd: unsupported %d\n", *cmd);
2156ec886bf5SAlexey Budankov 				break;
2157ec886bf5SAlexey Budankov 			}
2158d20aff15SAdrian Hunter 			if (!(*cmd == EVLIST_CTL_CMD_ACK || *cmd == EVLIST_CTL_CMD_UNSUPPORTED ||
2159d20aff15SAdrian Hunter 			      *cmd == EVLIST_CTL_CMD_SNAPSHOT))
2160ec886bf5SAlexey Budankov 				evlist__ctlfd_ack(evlist);
2161ec886bf5SAlexey Budankov 		}
2162ec886bf5SAlexey Budankov 	}
2163ec886bf5SAlexey Budankov 
2164ec886bf5SAlexey Budankov 	if (entries[ctlfd_pos].revents & (POLLHUP | POLLERR))
2165ec886bf5SAlexey Budankov 		evlist__finalize_ctlfd(evlist);
2166ec886bf5SAlexey Budankov 	else
2167ec886bf5SAlexey Budankov 		entries[ctlfd_pos].revents = 0;
2168ec886bf5SAlexey Budankov 
2169ec886bf5SAlexey Budankov 	return err;
2170ec886bf5SAlexey Budankov }
2171b214ba8cSNamhyung Kim 
21726657a099SAdrian Hunter /**
21736657a099SAdrian Hunter  * struct event_enable_time - perf record -D/--delay single time range.
21746657a099SAdrian Hunter  * @start: start of time range to enable events in milliseconds
21756657a099SAdrian Hunter  * @end: end of time range to enable events in milliseconds
21766657a099SAdrian Hunter  *
21776657a099SAdrian Hunter  * N.B. this structure is also accessed as an array of int.
21786657a099SAdrian Hunter  */
21796657a099SAdrian Hunter struct event_enable_time {
21806657a099SAdrian Hunter 	int	start;
21816657a099SAdrian Hunter 	int	end;
21826657a099SAdrian Hunter };
21836657a099SAdrian Hunter 
21846657a099SAdrian Hunter static int parse_event_enable_time(const char *str, struct event_enable_time *range, bool first)
21856657a099SAdrian Hunter {
21866657a099SAdrian Hunter 	const char *fmt = first ? "%u - %u %n" : " , %u - %u %n";
21876657a099SAdrian Hunter 	int ret, start, end, n;
21886657a099SAdrian Hunter 
21896657a099SAdrian Hunter 	ret = sscanf(str, fmt, &start, &end, &n);
21906657a099SAdrian Hunter 	if (ret != 2 || end <= start)
21916657a099SAdrian Hunter 		return -EINVAL;
21926657a099SAdrian Hunter 	if (range) {
21936657a099SAdrian Hunter 		range->start = start;
21946657a099SAdrian Hunter 		range->end = end;
21956657a099SAdrian Hunter 	}
21966657a099SAdrian Hunter 	return n;
21976657a099SAdrian Hunter }
21986657a099SAdrian Hunter 
21996657a099SAdrian Hunter static ssize_t parse_event_enable_times(const char *str, struct event_enable_time *range)
22006657a099SAdrian Hunter {
22016657a099SAdrian Hunter 	int incr = !!range;
22026657a099SAdrian Hunter 	bool first = true;
22036657a099SAdrian Hunter 	ssize_t ret, cnt;
22046657a099SAdrian Hunter 
22056657a099SAdrian Hunter 	for (cnt = 0; *str; cnt++) {
22066657a099SAdrian Hunter 		ret = parse_event_enable_time(str, range, first);
22076657a099SAdrian Hunter 		if (ret < 0)
22086657a099SAdrian Hunter 			return ret;
22096657a099SAdrian Hunter 		/* Check no overlap */
22106657a099SAdrian Hunter 		if (!first && range && range->start <= range[-1].end)
22116657a099SAdrian Hunter 			return -EINVAL;
22126657a099SAdrian Hunter 		str += ret;
22136657a099SAdrian Hunter 		range += incr;
22146657a099SAdrian Hunter 		first = false;
22156657a099SAdrian Hunter 	}
22166657a099SAdrian Hunter 	return cnt;
22176657a099SAdrian Hunter }
22186657a099SAdrian Hunter 
22196657a099SAdrian Hunter /**
22206657a099SAdrian Hunter  * struct event_enable_timer - control structure for perf record -D/--delay.
22216657a099SAdrian Hunter  * @evlist: event list
22226657a099SAdrian Hunter  * @times: time ranges that events are enabled (N.B. this is also accessed as an
22236657a099SAdrian Hunter  *         array of int)
22246657a099SAdrian Hunter  * @times_cnt: number of time ranges
22256657a099SAdrian Hunter  * @timerfd: timer file descriptor
22266657a099SAdrian Hunter  * @pollfd_pos: position in @evlist array of file descriptors to poll (fdarray)
22276657a099SAdrian Hunter  * @times_step: current position in (int *)@times)[],
22286657a099SAdrian Hunter  *              refer event_enable_timer__process()
22296657a099SAdrian Hunter  *
22306657a099SAdrian Hunter  * Note, this structure is only used when there are time ranges, not when there
22316657a099SAdrian Hunter  * is only an initial delay.
22326657a099SAdrian Hunter  */
22336657a099SAdrian Hunter struct event_enable_timer {
22346657a099SAdrian Hunter 	struct evlist *evlist;
22356657a099SAdrian Hunter 	struct event_enable_time *times;
22366657a099SAdrian Hunter 	size_t	times_cnt;
22376657a099SAdrian Hunter 	int	timerfd;
22386657a099SAdrian Hunter 	int	pollfd_pos;
22396657a099SAdrian Hunter 	size_t	times_step;
22406657a099SAdrian Hunter };
22416657a099SAdrian Hunter 
22426657a099SAdrian Hunter static int str_to_delay(const char *str)
22436657a099SAdrian Hunter {
22446657a099SAdrian Hunter 	char *endptr;
22456657a099SAdrian Hunter 	long d;
22466657a099SAdrian Hunter 
22476657a099SAdrian Hunter 	d = strtol(str, &endptr, 10);
22486657a099SAdrian Hunter 	if (*endptr || d > INT_MAX || d < -1)
22496657a099SAdrian Hunter 		return 0;
22506657a099SAdrian Hunter 	return d;
22516657a099SAdrian Hunter }
22526657a099SAdrian Hunter 
22536657a099SAdrian Hunter int evlist__parse_event_enable_time(struct evlist *evlist, struct record_opts *opts,
22546657a099SAdrian Hunter 				    const char *str, int unset)
22556657a099SAdrian Hunter {
22566657a099SAdrian Hunter 	enum fdarray_flags flags = fdarray_flag__nonfilterable | fdarray_flag__non_perf_event;
22576657a099SAdrian Hunter 	struct event_enable_timer *eet;
22586657a099SAdrian Hunter 	ssize_t times_cnt;
22596657a099SAdrian Hunter 	ssize_t ret;
22606657a099SAdrian Hunter 	int err;
22616657a099SAdrian Hunter 
22626657a099SAdrian Hunter 	if (unset)
22636657a099SAdrian Hunter 		return 0;
22646657a099SAdrian Hunter 
2265cb4b9e68SChangbin Du 	opts->target.initial_delay = str_to_delay(str);
2266cb4b9e68SChangbin Du 	if (opts->target.initial_delay)
22676657a099SAdrian Hunter 		return 0;
22686657a099SAdrian Hunter 
22696657a099SAdrian Hunter 	ret = parse_event_enable_times(str, NULL);
22706657a099SAdrian Hunter 	if (ret < 0)
22716657a099SAdrian Hunter 		return ret;
22726657a099SAdrian Hunter 
22736657a099SAdrian Hunter 	times_cnt = ret;
22746657a099SAdrian Hunter 	if (times_cnt == 0)
22756657a099SAdrian Hunter 		return -EINVAL;
22766657a099SAdrian Hunter 
22776657a099SAdrian Hunter 	eet = zalloc(sizeof(*eet));
22786657a099SAdrian Hunter 	if (!eet)
22796657a099SAdrian Hunter 		return -ENOMEM;
22806657a099SAdrian Hunter 
22816657a099SAdrian Hunter 	eet->times = calloc(times_cnt, sizeof(*eet->times));
22826657a099SAdrian Hunter 	if (!eet->times) {
22836657a099SAdrian Hunter 		err = -ENOMEM;
22846657a099SAdrian Hunter 		goto free_eet;
22856657a099SAdrian Hunter 	}
22866657a099SAdrian Hunter 
22876657a099SAdrian Hunter 	if (parse_event_enable_times(str, eet->times) != times_cnt) {
22886657a099SAdrian Hunter 		err = -EINVAL;
22896657a099SAdrian Hunter 		goto free_eet_times;
22906657a099SAdrian Hunter 	}
22916657a099SAdrian Hunter 
22926657a099SAdrian Hunter 	eet->times_cnt = times_cnt;
22936657a099SAdrian Hunter 
22946657a099SAdrian Hunter 	eet->timerfd = timerfd_create(CLOCK_MONOTONIC, TFD_CLOEXEC);
22956657a099SAdrian Hunter 	if (eet->timerfd == -1) {
22966657a099SAdrian Hunter 		err = -errno;
22976657a099SAdrian Hunter 		pr_err("timerfd_create failed: %s\n", strerror(errno));
22986657a099SAdrian Hunter 		goto free_eet_times;
22996657a099SAdrian Hunter 	}
23006657a099SAdrian Hunter 
23016657a099SAdrian Hunter 	eet->pollfd_pos = perf_evlist__add_pollfd(&evlist->core, eet->timerfd, NULL, POLLIN, flags);
23026657a099SAdrian Hunter 	if (eet->pollfd_pos < 0) {
23036657a099SAdrian Hunter 		err = eet->pollfd_pos;
23046657a099SAdrian Hunter 		goto close_timerfd;
23056657a099SAdrian Hunter 	}
23066657a099SAdrian Hunter 
23076657a099SAdrian Hunter 	eet->evlist = evlist;
23086657a099SAdrian Hunter 	evlist->eet = eet;
2309cb4b9e68SChangbin Du 	opts->target.initial_delay = eet->times[0].start;
23106657a099SAdrian Hunter 
23116657a099SAdrian Hunter 	return 0;
23126657a099SAdrian Hunter 
23136657a099SAdrian Hunter close_timerfd:
23146657a099SAdrian Hunter 	close(eet->timerfd);
23156657a099SAdrian Hunter free_eet_times:
23166657a099SAdrian Hunter 	free(eet->times);
23176657a099SAdrian Hunter free_eet:
23186657a099SAdrian Hunter 	free(eet);
23196657a099SAdrian Hunter 	return err;
23206657a099SAdrian Hunter }
23216657a099SAdrian Hunter 
23226657a099SAdrian Hunter static int event_enable_timer__set_timer(struct event_enable_timer *eet, int ms)
23236657a099SAdrian Hunter {
23246657a099SAdrian Hunter 	struct itimerspec its = {
23256657a099SAdrian Hunter 		.it_value.tv_sec = ms / MSEC_PER_SEC,
23266657a099SAdrian Hunter 		.it_value.tv_nsec = (ms % MSEC_PER_SEC) * NSEC_PER_MSEC,
23276657a099SAdrian Hunter 	};
23286657a099SAdrian Hunter 	int err = 0;
23296657a099SAdrian Hunter 
23306657a099SAdrian Hunter 	if (timerfd_settime(eet->timerfd, 0, &its, NULL) < 0) {
23316657a099SAdrian Hunter 		err = -errno;
23326657a099SAdrian Hunter 		pr_err("timerfd_settime failed: %s\n", strerror(errno));
23336657a099SAdrian Hunter 	}
23346657a099SAdrian Hunter 	return err;
23356657a099SAdrian Hunter }
23366657a099SAdrian Hunter 
23376657a099SAdrian Hunter int event_enable_timer__start(struct event_enable_timer *eet)
23386657a099SAdrian Hunter {
23396657a099SAdrian Hunter 	int ms;
23406657a099SAdrian Hunter 
23416657a099SAdrian Hunter 	if (!eet)
23426657a099SAdrian Hunter 		return 0;
23436657a099SAdrian Hunter 
23446657a099SAdrian Hunter 	ms = eet->times[0].end - eet->times[0].start;
23456657a099SAdrian Hunter 	eet->times_step = 1;
23466657a099SAdrian Hunter 
23476657a099SAdrian Hunter 	return event_enable_timer__set_timer(eet, ms);
23486657a099SAdrian Hunter }
23496657a099SAdrian Hunter 
23506657a099SAdrian Hunter int event_enable_timer__process(struct event_enable_timer *eet)
23516657a099SAdrian Hunter {
23526657a099SAdrian Hunter 	struct pollfd *entries;
23536657a099SAdrian Hunter 	short revents;
23546657a099SAdrian Hunter 
23556657a099SAdrian Hunter 	if (!eet)
23566657a099SAdrian Hunter 		return 0;
23576657a099SAdrian Hunter 
23586657a099SAdrian Hunter 	entries = eet->evlist->core.pollfd.entries;
23596657a099SAdrian Hunter 	revents = entries[eet->pollfd_pos].revents;
23606657a099SAdrian Hunter 	entries[eet->pollfd_pos].revents = 0;
23616657a099SAdrian Hunter 
23626657a099SAdrian Hunter 	if (revents & POLLIN) {
23636657a099SAdrian Hunter 		size_t step = eet->times_step;
23646657a099SAdrian Hunter 		size_t pos = step / 2;
23656657a099SAdrian Hunter 
23666657a099SAdrian Hunter 		if (step & 1) {
23676657a099SAdrian Hunter 			evlist__disable_non_dummy(eet->evlist);
23686657a099SAdrian Hunter 			pr_info(EVLIST_DISABLED_MSG);
23696657a099SAdrian Hunter 			if (pos >= eet->times_cnt - 1) {
23706657a099SAdrian Hunter 				/* Disarm timer */
23716657a099SAdrian Hunter 				event_enable_timer__set_timer(eet, 0);
23726657a099SAdrian Hunter 				return 1; /* Stop */
23736657a099SAdrian Hunter 			}
23746657a099SAdrian Hunter 		} else {
23756657a099SAdrian Hunter 			evlist__enable_non_dummy(eet->evlist);
23766657a099SAdrian Hunter 			pr_info(EVLIST_ENABLED_MSG);
23776657a099SAdrian Hunter 		}
23786657a099SAdrian Hunter 
23796657a099SAdrian Hunter 		step += 1;
23806657a099SAdrian Hunter 		pos = step / 2;
23816657a099SAdrian Hunter 
23826657a099SAdrian Hunter 		if (pos < eet->times_cnt) {
23836657a099SAdrian Hunter 			int *times = (int *)eet->times; /* Accessing 'times' as array of int */
23846657a099SAdrian Hunter 			int ms = times[step] - times[step - 1];
23856657a099SAdrian Hunter 
23866657a099SAdrian Hunter 			eet->times_step = step;
23876657a099SAdrian Hunter 			return event_enable_timer__set_timer(eet, ms);
23886657a099SAdrian Hunter 		}
23896657a099SAdrian Hunter 	}
23906657a099SAdrian Hunter 
23916657a099SAdrian Hunter 	return 0;
23926657a099SAdrian Hunter }
23936657a099SAdrian Hunter 
23946657a099SAdrian Hunter void event_enable_timer__exit(struct event_enable_timer **ep)
23956657a099SAdrian Hunter {
23966657a099SAdrian Hunter 	if (!ep || !*ep)
23976657a099SAdrian Hunter 		return;
23986657a099SAdrian Hunter 	free((*ep)->times);
23996657a099SAdrian Hunter 	zfree(ep);
24006657a099SAdrian Hunter }
24016657a099SAdrian Hunter 
2402b214ba8cSNamhyung Kim struct evsel *evlist__find_evsel(struct evlist *evlist, int idx)
2403b214ba8cSNamhyung Kim {
2404b214ba8cSNamhyung Kim 	struct evsel *evsel;
2405b214ba8cSNamhyung Kim 
2406b214ba8cSNamhyung Kim 	evlist__for_each_entry(evlist, evsel) {
240738fe0e01SJiri Olsa 		if (evsel->core.idx == idx)
2408b214ba8cSNamhyung Kim 			return evsel;
2409b214ba8cSNamhyung Kim 	}
2410b214ba8cSNamhyung Kim 	return NULL;
2411b214ba8cSNamhyung Kim }
24129865ea8aSArnaldo Carvalho de Melo 
24139865ea8aSArnaldo Carvalho de Melo int evlist__scnprintf_evsels(struct evlist *evlist, size_t size, char *bf)
24149865ea8aSArnaldo Carvalho de Melo {
24159865ea8aSArnaldo Carvalho de Melo 	struct evsel *evsel;
24169865ea8aSArnaldo Carvalho de Melo 	int printed = 0;
24179865ea8aSArnaldo Carvalho de Melo 
24189865ea8aSArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, evsel) {
24199865ea8aSArnaldo Carvalho de Melo 		if (evsel__is_dummy_event(evsel))
24209865ea8aSArnaldo Carvalho de Melo 			continue;
24219865ea8aSArnaldo Carvalho de Melo 		if (size > (strlen(evsel__name(evsel)) + (printed ? 2 : 1))) {
24229865ea8aSArnaldo Carvalho de Melo 			printed += scnprintf(bf + printed, size - printed, "%s%s", printed ? "," : "", evsel__name(evsel));
24239865ea8aSArnaldo Carvalho de Melo 		} else {
24249865ea8aSArnaldo Carvalho de Melo 			printed += scnprintf(bf + printed, size - printed, "%s...", printed ? "," : "");
24259865ea8aSArnaldo Carvalho de Melo 			break;
24269865ea8aSArnaldo Carvalho de Melo 		}
24279865ea8aSArnaldo Carvalho de Melo 	}
24289865ea8aSArnaldo Carvalho de Melo 
24299865ea8aSArnaldo Carvalho de Melo 	return printed;
24309865ea8aSArnaldo Carvalho de Melo }
2431d5a8bd0fSJin Yao 
2432d5a8bd0fSJin Yao void evlist__check_mem_load_aux(struct evlist *evlist)
2433d5a8bd0fSJin Yao {
2434d5a8bd0fSJin Yao 	struct evsel *leader, *evsel, *pos;
2435d5a8bd0fSJin Yao 
2436d5a8bd0fSJin Yao 	/*
2437d5a8bd0fSJin Yao 	 * For some platforms, the 'mem-loads' event is required to use
2438d5a8bd0fSJin Yao 	 * together with 'mem-loads-aux' within a group and 'mem-loads-aux'
2439d5a8bd0fSJin Yao 	 * must be the group leader. Now we disable this group before reporting
2440d5a8bd0fSJin Yao 	 * because 'mem-loads-aux' is just an auxiliary event. It doesn't carry
2441d5a8bd0fSJin Yao 	 * any valid memory load information.
2442d5a8bd0fSJin Yao 	 */
2443d5a8bd0fSJin Yao 	evlist__for_each_entry(evlist, evsel) {
2444fba7c866SJiri Olsa 		leader = evsel__leader(evsel);
2445d5a8bd0fSJin Yao 		if (leader == evsel)
2446d5a8bd0fSJin Yao 			continue;
2447d5a8bd0fSJin Yao 
2448d5a8bd0fSJin Yao 		if (leader->name && strstr(leader->name, "mem-loads-aux")) {
2449d5a8bd0fSJin Yao 			for_each_group_evsel(pos, leader) {
2450fba7c866SJiri Olsa 				evsel__set_leader(pos, pos);
2451d5a8bd0fSJin Yao 				pos->core.nr_members = 0;
2452d5a8bd0fSJin Yao 			}
2453d5a8bd0fSJin Yao 		}
2454d5a8bd0fSJin Yao 	}
2455d5a8bd0fSJin Yao }
2456