xref: /openbmc/linux/tools/perf/util/evlist.c (revision 5ac72634)
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"
34d180aa56SNamhyung Kim #include "util/bpf-filter.h"
35ea0c5239SIan Rogers #include "util/util.h"
369607ad3aSArnaldo Carvalho de Melo #include <signal.h>
3735b9d88eSArnaldo Carvalho de Melo #include <unistd.h>
38b397f846SArnaldo Carvalho de Melo #include <sched.h>
39f2a39fe8SArnaldo Carvalho de Melo #include <stdlib.h>
40361c99a6SArnaldo Carvalho de Melo 
4150d08e47SArnaldo Carvalho de Melo #include "parse-events.h"
424b6ab94eSJosh Poimboeuf #include <subcmd/parse-options.h>
4350d08e47SArnaldo Carvalho de Melo 
44bafae98eSArnaldo Carvalho de Melo #include <fcntl.h>
4586a5e0c2SArnaldo Carvalho de Melo #include <sys/ioctl.h>
46f8a95309SArnaldo Carvalho de Melo #include <sys/mman.h>
47a7672d1dSArnaldo Carvalho de Melo #include <sys/prctl.h>
486657a099SAdrian Hunter #include <sys/timerfd.h>
49f8a95309SArnaldo Carvalho de Melo 
5070db7533SArnaldo Carvalho de Melo #include <linux/bitops.h>
5170db7533SArnaldo Carvalho de Melo #include <linux/hash.h>
520389cd1fSArnaldo Carvalho de Melo #include <linux/log2.h>
538dd2a131SJiri Olsa #include <linux/err.h>
548520a98dSArnaldo Carvalho de Melo #include <linux/string.h>
556657a099SAdrian Hunter #include <linux/time64.h>
567f7c536fSArnaldo Carvalho de Melo #include <linux/zalloc.h>
574562a739SJiri Olsa #include <perf/evlist.h>
5888761fa1SJiri Olsa #include <perf/evsel.h>
599c3516d1SJiri Olsa #include <perf/cpumap.h>
607728fa0cSJiri Olsa #include <perf/mmap.h>
6170db7533SArnaldo Carvalho de Melo 
62e14e5497SArnaldo Carvalho de Melo #include <internal/xyarray.h>
63e14e5497SArnaldo Carvalho de Melo 
64748fe088SArnaldo Carvalho de Melo #ifdef LACKS_SIGQUEUE_PROTOTYPE
65748fe088SArnaldo Carvalho de Melo int sigqueue(pid_t pid, int sig, const union sigval value);
66748fe088SArnaldo Carvalho de Melo #endif
67748fe088SArnaldo Carvalho de Melo 
689dfcb759SJiri Olsa #define FD(e, x, y) (*(int *)xyarray__entry(e->core.fd, x, y))
698cd36f3eSJiri Olsa #define SID(e, x, y) xyarray__entry(e->core.sample_id, x, y)
70f8a95309SArnaldo Carvalho de Melo 
7152c86bcaSJiri Olsa void evlist__init(struct evlist *evlist, struct perf_cpu_map *cpus,
729749b90eSJiri Olsa 		  struct perf_thread_map *threads)
73361c99a6SArnaldo Carvalho de Melo {
744562a739SJiri Olsa 	perf_evlist__init(&evlist->core);
75453fa030SJiri Olsa 	perf_evlist__set_maps(&evlist->core, cpus, threads);
7635b9d88eSArnaldo Carvalho de Melo 	evlist->workload.pid = -1;
7754cc54deSWang Nan 	evlist->bkw_mmap_state = BKW_MMAP_NOTREADY;
788ab705b5SAlexey Budankov 	evlist->ctl_fd.fd = -1;
798ab705b5SAlexey Budankov 	evlist->ctl_fd.ack = -1;
808ab705b5SAlexey Budankov 	evlist->ctl_fd.pos = -1;
81361c99a6SArnaldo Carvalho de Melo }
82361c99a6SArnaldo Carvalho de Melo 
830f98b11cSJiri Olsa struct evlist *evlist__new(void)
84ef1d1af2SArnaldo Carvalho de Melo {
8563503dbaSJiri Olsa 	struct evlist *evlist = zalloc(sizeof(*evlist));
86ef1d1af2SArnaldo Carvalho de Melo 
87ef1d1af2SArnaldo Carvalho de Melo 	if (evlist != NULL)
8852c86bcaSJiri Olsa 		evlist__init(evlist, NULL, NULL);
89ef1d1af2SArnaldo Carvalho de Melo 
90361c99a6SArnaldo Carvalho de Melo 	return evlist;
91361c99a6SArnaldo Carvalho de Melo }
92361c99a6SArnaldo Carvalho de Melo 
93606e2c29SArnaldo Carvalho de Melo struct evlist *evlist__new_default(void)
94b22d54b0SJiri Olsa {
950f98b11cSJiri Olsa 	struct evlist *evlist = evlist__new();
96b22d54b0SJiri Olsa 
97e251abeeSArnaldo Carvalho de Melo 	if (evlist && evlist__add_default(evlist)) {
98c12995a5SJiri Olsa 		evlist__delete(evlist);
99b22d54b0SJiri Olsa 		evlist = NULL;
100b22d54b0SJiri Olsa 	}
101b22d54b0SJiri Olsa 
102b22d54b0SJiri Olsa 	return evlist;
103b22d54b0SJiri Olsa }
104b22d54b0SJiri Olsa 
105606e2c29SArnaldo Carvalho de Melo struct evlist *evlist__new_dummy(void)
1065bae0250SArnaldo Carvalho de Melo {
1070f98b11cSJiri Olsa 	struct evlist *evlist = evlist__new();
1085bae0250SArnaldo Carvalho de Melo 
109e251abeeSArnaldo Carvalho de Melo 	if (evlist && evlist__add_dummy(evlist)) {
110c12995a5SJiri Olsa 		evlist__delete(evlist);
1115bae0250SArnaldo Carvalho de Melo 		evlist = NULL;
1125bae0250SArnaldo Carvalho de Melo 	}
1135bae0250SArnaldo Carvalho de Melo 
1145bae0250SArnaldo Carvalho de Melo 	return evlist;
1155bae0250SArnaldo Carvalho de Melo }
1165bae0250SArnaldo Carvalho de Melo 
11775562573SAdrian Hunter /**
1180a7e7ec9SArnaldo Carvalho de Melo  * evlist__set_id_pos - set the positions of event ids.
11975562573SAdrian Hunter  * @evlist: selected event list
12075562573SAdrian Hunter  *
12175562573SAdrian Hunter  * Events with compatible sample types all have the same id_pos
12275562573SAdrian Hunter  * and is_pos.  For convenience, put a copy on evlist.
12375562573SAdrian Hunter  */
1240a7e7ec9SArnaldo Carvalho de Melo void evlist__set_id_pos(struct evlist *evlist)
12575562573SAdrian Hunter {
126515dbe48SJiri Olsa 	struct evsel *first = evlist__first(evlist);
12775562573SAdrian Hunter 
12875562573SAdrian Hunter 	evlist->id_pos = first->id_pos;
12975562573SAdrian Hunter 	evlist->is_pos = first->is_pos;
13075562573SAdrian Hunter }
13175562573SAdrian Hunter 
1320a7e7ec9SArnaldo Carvalho de Melo static void evlist__update_id_pos(struct evlist *evlist)
133733cd2feSAdrian Hunter {
13432dcd021SJiri Olsa 	struct evsel *evsel;
135733cd2feSAdrian Hunter 
136e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, evsel)
1374b5e87b7SArnaldo Carvalho de Melo 		evsel__calc_id_pos(evsel);
138733cd2feSAdrian Hunter 
1390a7e7ec9SArnaldo Carvalho de Melo 	evlist__set_id_pos(evlist);
140733cd2feSAdrian Hunter }
141733cd2feSAdrian Hunter 
142e6b1878dSJiri Olsa static void evlist__purge(struct evlist *evlist)
143361c99a6SArnaldo Carvalho de Melo {
14432dcd021SJiri Olsa 	struct evsel *pos, *n;
145361c99a6SArnaldo Carvalho de Melo 
146e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry_safe(evlist, n, pos) {
147b27c4eceSJiri Olsa 		list_del_init(&pos->core.node);
148d49e4695SArnaldo Carvalho de Melo 		pos->evlist = NULL;
1495eb2dd2aSJiri Olsa 		evsel__delete(pos);
150361c99a6SArnaldo Carvalho de Melo 	}
151361c99a6SArnaldo Carvalho de Melo 
1526484d2f9SJiri Olsa 	evlist->core.nr_entries = 0;
153361c99a6SArnaldo Carvalho de Melo }
154361c99a6SArnaldo Carvalho de Melo 
155470579b0SJiri Olsa void evlist__exit(struct evlist *evlist)
156ef1d1af2SArnaldo Carvalho de Melo {
1576657a099SAdrian Hunter 	event_enable_timer__exit(&evlist->eet);
15804662523SArnaldo Carvalho de Melo 	zfree(&evlist->mmap);
1590b72d69aSWang Nan 	zfree(&evlist->overwrite_mmap);
16093dd6e28SJiri Olsa 	perf_evlist__exit(&evlist->core);
161ef1d1af2SArnaldo Carvalho de Melo }
162ef1d1af2SArnaldo Carvalho de Melo 
163c12995a5SJiri Olsa void evlist__delete(struct evlist *evlist)
164361c99a6SArnaldo Carvalho de Melo {
1650b04b3dcSArnaldo Carvalho de Melo 	if (evlist == NULL)
1660b04b3dcSArnaldo Carvalho de Melo 		return;
1670b04b3dcSArnaldo Carvalho de Melo 
168db6b7b13SJiri Olsa 	evlist__munmap(evlist);
169750b4edeSJiri Olsa 	evlist__close(evlist);
170e6b1878dSJiri Olsa 	evlist__purge(evlist);
171470579b0SJiri Olsa 	evlist__exit(evlist);
172361c99a6SArnaldo Carvalho de Melo 	free(evlist);
173361c99a6SArnaldo Carvalho de Melo }
174361c99a6SArnaldo Carvalho de Melo 
175a1cf3a75SJiri Olsa void evlist__add(struct evlist *evlist, struct evsel *entry)
176361c99a6SArnaldo Carvalho de Melo {
1776484d2f9SJiri Olsa 	perf_evlist__add(&evlist->core, &entry->core);
17838fe0e01SJiri Olsa 	entry->evlist = evlist;
17938fe0e01SJiri Olsa 	entry->tracking = !entry->core.idx;
1806484d2f9SJiri Olsa 
1816484d2f9SJiri Olsa 	if (evlist->core.nr_entries == 1)
1820a7e7ec9SArnaldo Carvalho de Melo 		evlist__set_id_pos(evlist);
183361c99a6SArnaldo Carvalho de Melo }
184361c99a6SArnaldo Carvalho de Melo 
18516251027SJiri Olsa void evlist__remove(struct evlist *evlist, struct evsel *evsel)
1864768230aSAdrian Hunter {
1874768230aSAdrian Hunter 	evsel->evlist = NULL;
18852e22fb8SJiri Olsa 	perf_evlist__remove(&evlist->core, &evsel->core);
1894768230aSAdrian Hunter }
1904768230aSAdrian Hunter 
191e414fd1aSArnaldo Carvalho de Melo void evlist__splice_list_tail(struct evlist *evlist, struct list_head *list)
19250d08e47SArnaldo Carvalho de Melo {
1936d2783feSJohn Garry 	while (!list_empty(list)) {
1946d2783feSJohn Garry 		struct evsel *evsel, *temp, *leader = NULL;
19575562573SAdrian Hunter 
196e5cadb93SArnaldo Carvalho de Melo 		__evlist__for_each_entry_safe(list, temp, evsel) {
197b27c4eceSJiri Olsa 			list_del_init(&evsel->core.node);
198a1cf3a75SJiri Olsa 			evlist__add(evlist, evsel);
1996d2783feSJohn Garry 			leader = evsel;
2006d2783feSJohn Garry 			break;
2016d2783feSJohn Garry 		}
2026d2783feSJohn Garry 
2036d2783feSJohn Garry 		__evlist__for_each_entry_safe(list, temp, evsel) {
204fba7c866SJiri Olsa 			if (evsel__has_leader(evsel, leader)) {
2056d2783feSJohn Garry 				list_del_init(&evsel->core.node);
2066d2783feSJohn Garry 				evlist__add(evlist, evsel);
2076d2783feSJohn Garry 			}
2086d2783feSJohn Garry 		}
209f114d6efSAdrian Hunter 	}
21050d08e47SArnaldo Carvalho de Melo }
21150d08e47SArnaldo Carvalho de Melo 
212c0e53476SArnaldo Carvalho de Melo int __evlist__set_tracepoints_handlers(struct evlist *evlist,
213c0e53476SArnaldo Carvalho de Melo 				       const struct evsel_str_handler *assocs, size_t nr_assocs)
214c0e53476SArnaldo Carvalho de Melo {
215c0e53476SArnaldo Carvalho de Melo 	size_t i;
216c0e53476SArnaldo Carvalho de Melo 	int err;
217c0e53476SArnaldo Carvalho de Melo 
218c0e53476SArnaldo Carvalho de Melo 	for (i = 0; i < nr_assocs; i++) {
219c0e53476SArnaldo Carvalho de Melo 		// Adding a handler for an event not in this evlist, just ignore it.
220b02736f7SArnaldo Carvalho de Melo 		struct evsel *evsel = evlist__find_tracepoint_by_name(evlist, assocs[i].name);
221c0e53476SArnaldo Carvalho de Melo 		if (evsel == NULL)
222c0e53476SArnaldo Carvalho de Melo 			continue;
223c0e53476SArnaldo Carvalho de Melo 
224c0e53476SArnaldo Carvalho de Melo 		err = -EEXIST;
225c0e53476SArnaldo Carvalho de Melo 		if (evsel->handler != NULL)
226c0e53476SArnaldo Carvalho de Melo 			goto out;
227c0e53476SArnaldo Carvalho de Melo 		evsel->handler = assocs[i].handler;
228c0e53476SArnaldo Carvalho de Melo 	}
229c0e53476SArnaldo Carvalho de Melo 
230c0e53476SArnaldo Carvalho de Melo 	err = 0;
231c0e53476SArnaldo Carvalho de Melo out:
232c0e53476SArnaldo Carvalho de Melo 	return err;
233c0e53476SArnaldo Carvalho de Melo }
234c0e53476SArnaldo Carvalho de Melo 
2355f8f9567SIan Rogers static void evlist__set_leader(struct evlist *evlist)
2366a4bb04cSJiri Olsa {
2372e6263abSJiri Olsa 	perf_evlist__set_leader(&evlist->core);
23897f63e4aSNamhyung Kim }
2396a4bb04cSJiri Olsa 
240e251abeeSArnaldo Carvalho de Melo int __evlist__add_default(struct evlist *evlist, bool precise)
241361c99a6SArnaldo Carvalho de Melo {
242b53a0755SJin Yao 	struct evsel *evsel;
243361c99a6SArnaldo Carvalho de Melo 
244b53a0755SJin Yao 	evsel = evsel__new_cycles(precise, PERF_TYPE_HARDWARE,
245b53a0755SJin Yao 				  PERF_COUNT_HW_CPU_CYCLES);
246361c99a6SArnaldo Carvalho de Melo 	if (evsel == NULL)
2477c48dcfdSArnaldo Carvalho de Melo 		return -ENOMEM;
248361c99a6SArnaldo Carvalho de Melo 
249a1cf3a75SJiri Olsa 	evlist__add(evlist, evsel);
250361c99a6SArnaldo Carvalho de Melo 	return 0;
251361c99a6SArnaldo Carvalho de Melo }
2525c581041SArnaldo Carvalho de Melo 
25382944899SAdrian Hunter static struct evsel *evlist__dummy_event(struct evlist *evlist)
2545bae0250SArnaldo Carvalho de Melo {
2555bae0250SArnaldo Carvalho de Melo 	struct perf_event_attr attr = {
2565bae0250SArnaldo Carvalho de Melo 		.type	= PERF_TYPE_SOFTWARE,
2575bae0250SArnaldo Carvalho de Melo 		.config = PERF_COUNT_SW_DUMMY,
2585bae0250SArnaldo Carvalho de Melo 		.size	= sizeof(attr), /* to capture ABI version */
2595bae0250SArnaldo Carvalho de Melo 	};
26082944899SAdrian Hunter 
26182944899SAdrian Hunter 	return evsel__new_idx(&attr, evlist->core.nr_entries);
26282944899SAdrian Hunter }
26382944899SAdrian Hunter 
26482944899SAdrian Hunter int evlist__add_dummy(struct evlist *evlist)
26582944899SAdrian Hunter {
26682944899SAdrian Hunter 	struct evsel *evsel = evlist__dummy_event(evlist);
2675bae0250SArnaldo Carvalho de Melo 
2685bae0250SArnaldo Carvalho de Melo 	if (evsel == NULL)
2695bae0250SArnaldo Carvalho de Melo 		return -ENOMEM;
2705bae0250SArnaldo Carvalho de Melo 
271a1cf3a75SJiri Olsa 	evlist__add(evlist, evsel);
2725bae0250SArnaldo Carvalho de Melo 	return 0;
2735bae0250SArnaldo Carvalho de Melo }
2745bae0250SArnaldo Carvalho de Melo 
275126d68fdSAdrian Hunter struct evsel *evlist__add_aux_dummy(struct evlist *evlist, bool system_wide)
276126d68fdSAdrian Hunter {
277126d68fdSAdrian Hunter 	struct evsel *evsel = evlist__dummy_event(evlist);
278126d68fdSAdrian Hunter 
279126d68fdSAdrian Hunter 	if (!evsel)
280126d68fdSAdrian Hunter 		return NULL;
281126d68fdSAdrian Hunter 
282126d68fdSAdrian Hunter 	evsel->core.attr.exclude_kernel = 1;
283126d68fdSAdrian Hunter 	evsel->core.attr.exclude_guest = 1;
284126d68fdSAdrian Hunter 	evsel->core.attr.exclude_hv = 1;
285126d68fdSAdrian Hunter 	evsel->core.attr.freq = 0;
286126d68fdSAdrian Hunter 	evsel->core.attr.sample_period = 1;
28760ea006fSNamhyung Kim 	evsel->core.system_wide = system_wide;
288126d68fdSAdrian Hunter 	evsel->no_aux_samples = true;
289126d68fdSAdrian Hunter 	evsel->name = strdup("dummy:u");
290126d68fdSAdrian Hunter 
291126d68fdSAdrian Hunter 	evlist__add(evlist, evsel);
292126d68fdSAdrian Hunter 	return evsel;
293126d68fdSAdrian Hunter }
294126d68fdSAdrian Hunter 
295378ef0f5SIan Rogers #ifdef HAVE_LIBTRACEEVENT
296182bb594SNamhyung Kim struct evsel *evlist__add_sched_switch(struct evlist *evlist, bool system_wide)
297182bb594SNamhyung Kim {
298182bb594SNamhyung Kim 	struct evsel *evsel = evsel__newtp_idx("sched", "sched_switch", 0);
299182bb594SNamhyung Kim 
300182bb594SNamhyung Kim 	if (IS_ERR(evsel))
301182bb594SNamhyung Kim 		return evsel;
302182bb594SNamhyung Kim 
303182bb594SNamhyung Kim 	evsel__set_sample_bit(evsel, CPU);
304182bb594SNamhyung Kim 	evsel__set_sample_bit(evsel, TIME);
305182bb594SNamhyung Kim 
306182bb594SNamhyung Kim 	evsel->core.system_wide = system_wide;
307182bb594SNamhyung Kim 	evsel->no_aux_samples = true;
308182bb594SNamhyung Kim 
309182bb594SNamhyung Kim 	evlist__add(evlist, evsel);
310182bb594SNamhyung Kim 	return evsel;
311378ef0f5SIan Rogers }
312378ef0f5SIan Rogers #endif
313182bb594SNamhyung Kim 
314cdb204adSKan Liang int evlist__add_attrs(struct evlist *evlist, struct perf_event_attr *attrs, size_t nr_attrs)
31550d08e47SArnaldo Carvalho de Melo {
31632dcd021SJiri Olsa 	struct evsel *evsel, *n;
31750d08e47SArnaldo Carvalho de Melo 	LIST_HEAD(head);
31850d08e47SArnaldo Carvalho de Melo 	size_t i;
31950d08e47SArnaldo Carvalho de Melo 
32050d08e47SArnaldo Carvalho de Melo 	for (i = 0; i < nr_attrs; i++) {
3218f6725a2SArnaldo Carvalho de Melo 		evsel = evsel__new_idx(attrs + i, evlist->core.nr_entries + i);
32250d08e47SArnaldo Carvalho de Melo 		if (evsel == NULL)
32350d08e47SArnaldo Carvalho de Melo 			goto out_delete_partial_list;
324b27c4eceSJiri Olsa 		list_add_tail(&evsel->core.node, &head);
32550d08e47SArnaldo Carvalho de Melo 	}
32650d08e47SArnaldo Carvalho de Melo 
327e414fd1aSArnaldo Carvalho de Melo 	evlist__splice_list_tail(evlist, &head);
32850d08e47SArnaldo Carvalho de Melo 
32950d08e47SArnaldo Carvalho de Melo 	return 0;
33050d08e47SArnaldo Carvalho de Melo 
33150d08e47SArnaldo Carvalho de Melo out_delete_partial_list:
332e5cadb93SArnaldo Carvalho de Melo 	__evlist__for_each_entry_safe(&head, n, evsel)
3335eb2dd2aSJiri Olsa 		evsel__delete(evsel);
33450d08e47SArnaldo Carvalho de Melo 	return -1;
33550d08e47SArnaldo Carvalho de Melo }
33650d08e47SArnaldo Carvalho de Melo 
337e251abeeSArnaldo Carvalho de Melo int __evlist__add_default_attrs(struct evlist *evlist, struct perf_event_attr *attrs, size_t nr_attrs)
33879695e1bSArnaldo Carvalho de Melo {
33979695e1bSArnaldo Carvalho de Melo 	size_t i;
34079695e1bSArnaldo Carvalho de Melo 
34179695e1bSArnaldo Carvalho de Melo 	for (i = 0; i < nr_attrs; i++)
34279695e1bSArnaldo Carvalho de Melo 		event_attr_init(attrs + i);
34379695e1bSArnaldo Carvalho de Melo 
344a1cf3a75SJiri Olsa 	return evlist__add_attrs(evlist, attrs, nr_attrs);
34579695e1bSArnaldo Carvalho de Melo }
34679695e1bSArnaldo Carvalho de Melo 
347a9c1ecdaSKan Liang __weak int arch_evlist__add_default_attrs(struct evlist *evlist,
348a9c1ecdaSKan Liang 					  struct perf_event_attr *attrs,
349a9c1ecdaSKan Liang 					  size_t nr_attrs)
35042641d6fSKan Liang {
351a9c1ecdaSKan Liang 	if (!nr_attrs)
35242641d6fSKan Liang 		return 0;
353a9c1ecdaSKan Liang 
354a9c1ecdaSKan Liang 	return __evlist__add_default_attrs(evlist, attrs, nr_attrs);
35542641d6fSKan Liang }
35642641d6fSKan Liang 
357b02736f7SArnaldo Carvalho de Melo struct evsel *evlist__find_tracepoint_by_id(struct evlist *evlist, int id)
358ee29be62SArnaldo Carvalho de Melo {
35932dcd021SJiri Olsa 	struct evsel *evsel;
360ee29be62SArnaldo Carvalho de Melo 
361e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, evsel) {
3621fc632ceSJiri Olsa 		if (evsel->core.attr.type   == PERF_TYPE_TRACEPOINT &&
3631fc632ceSJiri Olsa 		    (int)evsel->core.attr.config == id)
364ee29be62SArnaldo Carvalho de Melo 			return evsel;
365ee29be62SArnaldo Carvalho de Melo 	}
366ee29be62SArnaldo Carvalho de Melo 
367ee29be62SArnaldo Carvalho de Melo 	return NULL;
368ee29be62SArnaldo Carvalho de Melo }
369ee29be62SArnaldo Carvalho de Melo 
370b02736f7SArnaldo Carvalho de Melo struct evsel *evlist__find_tracepoint_by_name(struct evlist *evlist, const char *name)
371a2f2804aSDavid Ahern {
37232dcd021SJiri Olsa 	struct evsel *evsel;
373a2f2804aSDavid Ahern 
374e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, evsel) {
3751fc632ceSJiri Olsa 		if ((evsel->core.attr.type == PERF_TYPE_TRACEPOINT) &&
376a2f2804aSDavid Ahern 		    (strcmp(evsel->name, name) == 0))
377a2f2804aSDavid Ahern 			return evsel;
378a2f2804aSDavid Ahern 	}
379a2f2804aSDavid Ahern 
380a2f2804aSDavid Ahern 	return NULL;
381a2f2804aSDavid Ahern }
382a2f2804aSDavid Ahern 
383378ef0f5SIan Rogers #ifdef HAVE_LIBTRACEEVENT
384e251abeeSArnaldo Carvalho de Melo int evlist__add_newtp(struct evlist *evlist, const char *sys, const char *name, void *handler)
38539876e7dSArnaldo Carvalho de Melo {
3868f6725a2SArnaldo Carvalho de Melo 	struct evsel *evsel = evsel__newtp(sys, name);
38739876e7dSArnaldo Carvalho de Melo 
3888dd2a131SJiri Olsa 	if (IS_ERR(evsel))
38939876e7dSArnaldo Carvalho de Melo 		return -1;
39039876e7dSArnaldo Carvalho de Melo 
391744a9719SArnaldo Carvalho de Melo 	evsel->handler = handler;
392a1cf3a75SJiri Olsa 	evlist__add(evlist, evsel);
39339876e7dSArnaldo Carvalho de Melo 	return 0;
39439876e7dSArnaldo Carvalho de Melo }
395378ef0f5SIan Rogers #endif
39639876e7dSArnaldo Carvalho de Melo 
397472832d2SIan Rogers struct evlist_cpu_iterator evlist__cpu_begin(struct evlist *evlist, struct affinity *affinity)
398a8cbe40fSAndi Kleen {
399472832d2SIan Rogers 	struct evlist_cpu_iterator itr = {
400472832d2SIan Rogers 		.container = evlist,
4018b464eacSIan Rogers 		.evsel = NULL,
402472832d2SIan Rogers 		.cpu_map_idx = 0,
403472832d2SIan Rogers 		.evlist_cpu_map_idx = 0,
404472832d2SIan Rogers 		.evlist_cpu_map_nr = perf_cpu_map__nr(evlist->core.all_cpus),
4056d18804bSIan Rogers 		.cpu = (struct perf_cpu){ .cpu = -1},
406472832d2SIan Rogers 		.affinity = affinity,
407472832d2SIan Rogers 	};
408a8cbe40fSAndi Kleen 
4098b464eacSIan Rogers 	if (evlist__empty(evlist)) {
4108b464eacSIan Rogers 		/* Ensure the empty list doesn't iterate. */
4118b464eacSIan Rogers 		itr.evlist_cpu_map_idx = itr.evlist_cpu_map_nr;
4128b464eacSIan Rogers 	} else {
4138b464eacSIan Rogers 		itr.evsel = evlist__first(evlist);
414472832d2SIan Rogers 		if (itr.affinity) {
415472832d2SIan Rogers 			itr.cpu = perf_cpu_map__cpu(evlist->core.all_cpus, 0);
4166d18804bSIan Rogers 			affinity__set(itr.affinity, itr.cpu.cpu);
417472832d2SIan Rogers 			itr.cpu_map_idx = perf_cpu_map__idx(itr.evsel->core.cpus, itr.cpu);
418a8cbe40fSAndi Kleen 			/*
4198b464eacSIan Rogers 			 * If this CPU isn't in the evsel's cpu map then advance
4208b464eacSIan Rogers 			 * through the list.
421a8cbe40fSAndi Kleen 			 */
422472832d2SIan Rogers 			if (itr.cpu_map_idx == -1)
423472832d2SIan Rogers 				evlist_cpu_iterator__next(&itr);
424472832d2SIan Rogers 		}
4258b464eacSIan Rogers 	}
426472832d2SIan Rogers 	return itr;
427a8cbe40fSAndi Kleen }
428a8cbe40fSAndi Kleen 
429472832d2SIan Rogers void evlist_cpu_iterator__next(struct evlist_cpu_iterator *evlist_cpu_itr)
430a8cbe40fSAndi Kleen {
431472832d2SIan Rogers 	while (evlist_cpu_itr->evsel != evlist__last(evlist_cpu_itr->container)) {
432472832d2SIan Rogers 		evlist_cpu_itr->evsel = evsel__next(evlist_cpu_itr->evsel);
433472832d2SIan Rogers 		evlist_cpu_itr->cpu_map_idx =
434472832d2SIan Rogers 			perf_cpu_map__idx(evlist_cpu_itr->evsel->core.cpus,
435472832d2SIan Rogers 					  evlist_cpu_itr->cpu);
436472832d2SIan Rogers 		if (evlist_cpu_itr->cpu_map_idx != -1)
437472832d2SIan Rogers 			return;
438472832d2SIan Rogers 	}
439472832d2SIan Rogers 	evlist_cpu_itr->evlist_cpu_map_idx++;
440472832d2SIan Rogers 	if (evlist_cpu_itr->evlist_cpu_map_idx < evlist_cpu_itr->evlist_cpu_map_nr) {
441472832d2SIan Rogers 		evlist_cpu_itr->evsel = evlist__first(evlist_cpu_itr->container);
442472832d2SIan Rogers 		evlist_cpu_itr->cpu =
443472832d2SIan Rogers 			perf_cpu_map__cpu(evlist_cpu_itr->container->core.all_cpus,
444472832d2SIan Rogers 					  evlist_cpu_itr->evlist_cpu_map_idx);
445472832d2SIan Rogers 		if (evlist_cpu_itr->affinity)
4466d18804bSIan Rogers 			affinity__set(evlist_cpu_itr->affinity, evlist_cpu_itr->cpu.cpu);
447472832d2SIan Rogers 		evlist_cpu_itr->cpu_map_idx =
448472832d2SIan Rogers 			perf_cpu_map__idx(evlist_cpu_itr->evsel->core.cpus,
449472832d2SIan Rogers 					  evlist_cpu_itr->cpu);
450472832d2SIan Rogers 		/*
451472832d2SIan Rogers 		 * If this CPU isn't in the evsel's cpu map then advance through
452472832d2SIan Rogers 		 * the list.
453472832d2SIan Rogers 		 */
454472832d2SIan Rogers 		if (evlist_cpu_itr->cpu_map_idx == -1)
455472832d2SIan Rogers 			evlist_cpu_iterator__next(evlist_cpu_itr);
456472832d2SIan Rogers 	}
457a8cbe40fSAndi Kleen }
458a8cbe40fSAndi Kleen 
459472832d2SIan Rogers bool evlist_cpu_iterator__end(const struct evlist_cpu_iterator *evlist_cpu_itr)
460a8cbe40fSAndi Kleen {
461472832d2SIan Rogers 	return evlist_cpu_itr->evlist_cpu_map_idx >= evlist_cpu_itr->evlist_cpu_map_nr;
462a8cbe40fSAndi Kleen }
463a8cbe40fSAndi Kleen 
4647cfcd1e0SJiri Olsa static int evsel__strcmp(struct evsel *pos, char *evsel_name)
4657cfcd1e0SJiri Olsa {
4667cfcd1e0SJiri Olsa 	if (!evsel_name)
4677cfcd1e0SJiri Olsa 		return 0;
4687cfcd1e0SJiri Olsa 	if (evsel__is_dummy_event(pos))
4697cfcd1e0SJiri Olsa 		return 1;
470ce1d3bc2SArnaldo Carvalho de Melo 	return !evsel__name_is(pos, evsel_name);
4717cfcd1e0SJiri Olsa }
4727cfcd1e0SJiri Olsa 
4737cfcd1e0SJiri Olsa static int evlist__is_enabled(struct evlist *evlist)
4747cfcd1e0SJiri Olsa {
4757cfcd1e0SJiri Olsa 	struct evsel *pos;
4767cfcd1e0SJiri Olsa 
4777cfcd1e0SJiri Olsa 	evlist__for_each_entry(evlist, pos) {
4787cfcd1e0SJiri Olsa 		if (!evsel__is_group_leader(pos) || !pos->core.fd)
4797cfcd1e0SJiri Olsa 			continue;
4807cfcd1e0SJiri Olsa 		/* If at least one event is enabled, evlist is enabled. */
4817cfcd1e0SJiri Olsa 		if (!pos->disabled)
4827cfcd1e0SJiri Olsa 			return true;
4837cfcd1e0SJiri Olsa 	}
4847cfcd1e0SJiri Olsa 	return false;
4857cfcd1e0SJiri Olsa }
4867cfcd1e0SJiri Olsa 
487329725d5SAdrian Hunter static void __evlist__disable(struct evlist *evlist, char *evsel_name, bool excl_dummy)
4884152ab37SArnaldo Carvalho de Melo {
48932dcd021SJiri Olsa 	struct evsel *pos;
490472832d2SIan Rogers 	struct evlist_cpu_iterator evlist_cpu_itr;
4910d3d2376SArnaldo Carvalho de Melo 	struct affinity saved_affinity, *affinity = NULL;
49287cf8360SAdrian Hunter 	bool has_imm = false;
493704e2f5bSAndi Kleen 
4940d3d2376SArnaldo Carvalho de Melo 	// See explanation in evlist__close()
4950df6ade7SIan Rogers 	if (!cpu_map__is_dummy(evlist->core.user_requested_cpus)) {
4960d3d2376SArnaldo Carvalho de Melo 		if (affinity__setup(&saved_affinity) < 0)
497704e2f5bSAndi Kleen 			return;
4980d3d2376SArnaldo Carvalho de Melo 		affinity = &saved_affinity;
4990d3d2376SArnaldo Carvalho de Melo 	}
500704e2f5bSAndi Kleen 
50187cf8360SAdrian Hunter 	/* Disable 'immediate' events last */
502472832d2SIan Rogers 	for (int imm = 0; imm <= 1; imm++) {
5030d3d2376SArnaldo Carvalho de Melo 		evlist__for_each_cpu(evlist_cpu_itr, evlist, affinity) {
504472832d2SIan Rogers 			pos = evlist_cpu_itr.evsel;
5057cfcd1e0SJiri Olsa 			if (evsel__strcmp(pos, evsel_name))
5067cfcd1e0SJiri Olsa 				continue;
507c754c382SArnaldo Carvalho de Melo 			if (pos->disabled || !evsel__is_group_leader(pos) || !pos->core.fd)
5083fe4430dSJiri Olsa 				continue;
509329725d5SAdrian Hunter 			if (excl_dummy && evsel__is_dummy_event(pos))
510329725d5SAdrian Hunter 				continue;
51187cf8360SAdrian Hunter 			if (pos->immediate)
51287cf8360SAdrian Hunter 				has_imm = true;
51387cf8360SAdrian Hunter 			if (pos->immediate != imm)
51487cf8360SAdrian Hunter 				continue;
515472832d2SIan Rogers 			evsel__disable_cpu(pos, evlist_cpu_itr.cpu_map_idx);
516704e2f5bSAndi Kleen 		}
51787cf8360SAdrian Hunter 		if (!has_imm)
51887cf8360SAdrian Hunter 			break;
51987cf8360SAdrian Hunter 	}
52087cf8360SAdrian Hunter 
5210d3d2376SArnaldo Carvalho de Melo 	affinity__cleanup(affinity);
522704e2f5bSAndi Kleen 	evlist__for_each_entry(evlist, pos) {
5237cfcd1e0SJiri Olsa 		if (evsel__strcmp(pos, evsel_name))
5247cfcd1e0SJiri Olsa 			continue;
525c754c382SArnaldo Carvalho de Melo 		if (!evsel__is_group_leader(pos) || !pos->core.fd)
526704e2f5bSAndi Kleen 			continue;
527329725d5SAdrian Hunter 		if (excl_dummy && evsel__is_dummy_event(pos))
528329725d5SAdrian Hunter 			continue;
529704e2f5bSAndi Kleen 		pos->disabled = true;
5304152ab37SArnaldo Carvalho de Melo 	}
5312b56bcfbSArnaldo Carvalho de Melo 
5327cfcd1e0SJiri Olsa 	/*
5337cfcd1e0SJiri Olsa 	 * If we disabled only single event, we need to check
5347cfcd1e0SJiri Olsa 	 * the enabled state of the evlist manually.
5357cfcd1e0SJiri Olsa 	 */
5367cfcd1e0SJiri Olsa 	if (evsel_name)
5377cfcd1e0SJiri Olsa 		evlist->enabled = evlist__is_enabled(evlist);
5387cfcd1e0SJiri Olsa 	else
5392b56bcfbSArnaldo Carvalho de Melo 		evlist->enabled = false;
5404152ab37SArnaldo Carvalho de Melo }
5414152ab37SArnaldo Carvalho de Melo 
5427cfcd1e0SJiri Olsa void evlist__disable(struct evlist *evlist)
5437cfcd1e0SJiri Olsa {
544329725d5SAdrian Hunter 	__evlist__disable(evlist, NULL, false);
545329725d5SAdrian Hunter }
546329725d5SAdrian Hunter 
547329725d5SAdrian Hunter void evlist__disable_non_dummy(struct evlist *evlist)
548329725d5SAdrian Hunter {
549329725d5SAdrian Hunter 	__evlist__disable(evlist, NULL, true);
5507cfcd1e0SJiri Olsa }
5517cfcd1e0SJiri Olsa 
5527cfcd1e0SJiri Olsa void evlist__disable_evsel(struct evlist *evlist, char *evsel_name)
5537cfcd1e0SJiri Olsa {
554329725d5SAdrian Hunter 	__evlist__disable(evlist, evsel_name, false);
5557cfcd1e0SJiri Olsa }
5567cfcd1e0SJiri Olsa 
557329725d5SAdrian Hunter static void __evlist__enable(struct evlist *evlist, char *evsel_name, bool excl_dummy)
558764e16a3SDavid Ahern {
55932dcd021SJiri Olsa 	struct evsel *pos;
560472832d2SIan Rogers 	struct evlist_cpu_iterator evlist_cpu_itr;
561f350ee95SArnaldo Carvalho de Melo 	struct affinity saved_affinity, *affinity = NULL;
562764e16a3SDavid Ahern 
563f350ee95SArnaldo Carvalho de Melo 	// See explanation in evlist__close()
5640df6ade7SIan Rogers 	if (!cpu_map__is_dummy(evlist->core.user_requested_cpus)) {
565f350ee95SArnaldo Carvalho de Melo 		if (affinity__setup(&saved_affinity) < 0)
566704e2f5bSAndi Kleen 			return;
567f350ee95SArnaldo Carvalho de Melo 		affinity = &saved_affinity;
568f350ee95SArnaldo Carvalho de Melo 	}
569704e2f5bSAndi Kleen 
570f350ee95SArnaldo Carvalho de Melo 	evlist__for_each_cpu(evlist_cpu_itr, evlist, affinity) {
571472832d2SIan Rogers 		pos = evlist_cpu_itr.evsel;
5727cfcd1e0SJiri Olsa 		if (evsel__strcmp(pos, evsel_name))
5737cfcd1e0SJiri Olsa 			continue;
574c754c382SArnaldo Carvalho de Melo 		if (!evsel__is_group_leader(pos) || !pos->core.fd)
575704e2f5bSAndi Kleen 			continue;
576329725d5SAdrian Hunter 		if (excl_dummy && evsel__is_dummy_event(pos))
577329725d5SAdrian Hunter 			continue;
578472832d2SIan Rogers 		evsel__enable_cpu(pos, evlist_cpu_itr.cpu_map_idx);
579704e2f5bSAndi Kleen 	}
580f350ee95SArnaldo Carvalho de Melo 	affinity__cleanup(affinity);
581e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, pos) {
5827cfcd1e0SJiri Olsa 		if (evsel__strcmp(pos, evsel_name))
5837cfcd1e0SJiri Olsa 			continue;
584c754c382SArnaldo Carvalho de Melo 		if (!evsel__is_group_leader(pos) || !pos->core.fd)
5853fe4430dSJiri Olsa 			continue;
586329725d5SAdrian Hunter 		if (excl_dummy && evsel__is_dummy_event(pos))
587329725d5SAdrian Hunter 			continue;
588704e2f5bSAndi Kleen 		pos->disabled = false;
589764e16a3SDavid Ahern 	}
5902b56bcfbSArnaldo Carvalho de Melo 
5917cfcd1e0SJiri Olsa 	/*
5927cfcd1e0SJiri Olsa 	 * Even single event sets the 'enabled' for evlist,
5937cfcd1e0SJiri Olsa 	 * so the toggle can work properly and toggle to
5947cfcd1e0SJiri Olsa 	 * 'disabled' state.
5957cfcd1e0SJiri Olsa 	 */
5962b56bcfbSArnaldo Carvalho de Melo 	evlist->enabled = true;
5972b56bcfbSArnaldo Carvalho de Melo }
5982b56bcfbSArnaldo Carvalho de Melo 
5997cfcd1e0SJiri Olsa void evlist__enable(struct evlist *evlist)
6007cfcd1e0SJiri Olsa {
601329725d5SAdrian Hunter 	__evlist__enable(evlist, NULL, false);
602329725d5SAdrian Hunter }
603329725d5SAdrian Hunter 
604329725d5SAdrian Hunter void evlist__enable_non_dummy(struct evlist *evlist)
605329725d5SAdrian Hunter {
606329725d5SAdrian Hunter 	__evlist__enable(evlist, NULL, true);
6077cfcd1e0SJiri Olsa }
6087cfcd1e0SJiri Olsa 
6097cfcd1e0SJiri Olsa void evlist__enable_evsel(struct evlist *evlist, char *evsel_name)
6107cfcd1e0SJiri Olsa {
611329725d5SAdrian Hunter 	__evlist__enable(evlist, evsel_name, false);
6127cfcd1e0SJiri Olsa }
6137cfcd1e0SJiri Olsa 
614ade9d208SArnaldo Carvalho de Melo void evlist__toggle_enable(struct evlist *evlist)
6152b56bcfbSArnaldo Carvalho de Melo {
616e74676deSJiri Olsa 	(evlist->enabled ? evlist__disable : evlist__enable)(evlist);
617764e16a3SDavid Ahern }
618764e16a3SDavid Ahern 
619f4009e7bSJiri Olsa int evlist__add_pollfd(struct evlist *evlist, int fd)
620e4b356b5SArnaldo Carvalho de Melo {
621ab4c1f9fSAlexey Budankov 	return perf_evlist__add_pollfd(&evlist->core, fd, NULL, POLLIN, fdarray_flag__default);
622e4b356b5SArnaldo Carvalho de Melo }
6231b85337dSArnaldo Carvalho de Melo 
624f4009e7bSJiri Olsa int evlist__filter_pollfd(struct evlist *evlist, short revents_and_mask)
6251ddec7f0SArnaldo Carvalho de Melo {
62684227cb1SJiri Olsa 	return perf_evlist__filter_pollfd(&evlist->core, revents_and_mask);
6271ddec7f0SArnaldo Carvalho de Melo }
6281ddec7f0SArnaldo Carvalho de Melo 
629e16c2ce7SYang Jihong #ifdef HAVE_EVENTFD_SUPPORT
630e16c2ce7SYang Jihong int evlist__add_wakeup_eventfd(struct evlist *evlist, int fd)
631e16c2ce7SYang Jihong {
632e16c2ce7SYang Jihong 	return perf_evlist__add_pollfd(&evlist->core, fd, NULL, POLLIN,
633a032ad87SAdrian Hunter 				       fdarray_flag__nonfilterable |
634a032ad87SAdrian Hunter 				       fdarray_flag__non_perf_event);
635e16c2ce7SYang Jihong }
636e16c2ce7SYang Jihong #endif
637e16c2ce7SYang Jihong 
63880ab2987SJiri Olsa int evlist__poll(struct evlist *evlist, int timeout)
639f66a889dSArnaldo Carvalho de Melo {
64080ab2987SJiri Olsa 	return perf_evlist__poll(&evlist->core, timeout);
641f66a889dSArnaldo Carvalho de Melo }
642f66a889dSArnaldo Carvalho de Melo 
6433ccf8a7bSArnaldo Carvalho de Melo struct perf_sample_id *evlist__id2sid(struct evlist *evlist, u64 id)
64470db7533SArnaldo Carvalho de Melo {
64570db7533SArnaldo Carvalho de Melo 	struct hlist_head *head;
64670db7533SArnaldo Carvalho de Melo 	struct perf_sample_id *sid;
64770db7533SArnaldo Carvalho de Melo 	int hash;
64870db7533SArnaldo Carvalho de Melo 
64970db7533SArnaldo Carvalho de Melo 	hash = hash_64(id, PERF_EVLIST__HLIST_BITS);
6501d5af02dSJiri Olsa 	head = &evlist->core.heads[hash];
65170db7533SArnaldo Carvalho de Melo 
652b67bfe0dSSasha Levin 	hlist_for_each_entry(sid, head, node)
65370db7533SArnaldo Carvalho de Melo 		if (sid->id == id)
654932a3594SJiri Olsa 			return sid;
655932a3594SJiri Olsa 
656932a3594SJiri Olsa 	return NULL;
657932a3594SJiri Olsa }
658932a3594SJiri Olsa 
6593ccf8a7bSArnaldo Carvalho de Melo struct evsel *evlist__id2evsel(struct evlist *evlist, u64 id)
660932a3594SJiri Olsa {
661932a3594SJiri Olsa 	struct perf_sample_id *sid;
662932a3594SJiri Olsa 
6636484d2f9SJiri Olsa 	if (evlist->core.nr_entries == 1 || !id)
664515dbe48SJiri Olsa 		return evlist__first(evlist);
665932a3594SJiri Olsa 
6663ccf8a7bSArnaldo Carvalho de Melo 	sid = evlist__id2sid(evlist, id);
667932a3594SJiri Olsa 	if (sid)
66870c20369SJiri Olsa 		return container_of(sid->evsel, struct evsel, core);
66930e68bccSNamhyung Kim 
6708cedf3a5SArnaldo Carvalho de Melo 	if (!evlist__sample_id_all(evlist))
671515dbe48SJiri Olsa 		return evlist__first(evlist);
67230e68bccSNamhyung Kim 
67370db7533SArnaldo Carvalho de Melo 	return NULL;
67470db7533SArnaldo Carvalho de Melo }
67504391debSArnaldo Carvalho de Melo 
6763ccf8a7bSArnaldo Carvalho de Melo struct evsel *evlist__id2evsel_strict(struct evlist *evlist, u64 id)
677dddcf6abSAdrian Hunter {
678dddcf6abSAdrian Hunter 	struct perf_sample_id *sid;
679dddcf6abSAdrian Hunter 
680dddcf6abSAdrian Hunter 	if (!id)
681dddcf6abSAdrian Hunter 		return NULL;
682dddcf6abSAdrian Hunter 
6833ccf8a7bSArnaldo Carvalho de Melo 	sid = evlist__id2sid(evlist, id);
684dddcf6abSAdrian Hunter 	if (sid)
68570c20369SJiri Olsa 		return container_of(sid->evsel, struct evsel, core);
686dddcf6abSAdrian Hunter 
687dddcf6abSAdrian Hunter 	return NULL;
688dddcf6abSAdrian Hunter }
689dddcf6abSAdrian Hunter 
6903ccf8a7bSArnaldo Carvalho de Melo static int evlist__event2id(struct evlist *evlist, union perf_event *event, u64 *id)
69175562573SAdrian Hunter {
692b1fcd190SJiri Olsa 	const __u64 *array = event->sample.array;
69375562573SAdrian Hunter 	ssize_t n;
69475562573SAdrian Hunter 
69575562573SAdrian Hunter 	n = (event->header.size - sizeof(event->header)) >> 3;
69675562573SAdrian Hunter 
69775562573SAdrian Hunter 	if (event->header.type == PERF_RECORD_SAMPLE) {
69875562573SAdrian Hunter 		if (evlist->id_pos >= n)
69975562573SAdrian Hunter 			return -1;
70075562573SAdrian Hunter 		*id = array[evlist->id_pos];
70175562573SAdrian Hunter 	} else {
70275562573SAdrian Hunter 		if (evlist->is_pos > n)
70375562573SAdrian Hunter 			return -1;
70475562573SAdrian Hunter 		n -= evlist->is_pos;
70575562573SAdrian Hunter 		*id = array[n];
70675562573SAdrian Hunter 	}
70775562573SAdrian Hunter 	return 0;
70875562573SAdrian Hunter }
70975562573SAdrian Hunter 
7103ccf8a7bSArnaldo Carvalho de Melo struct evsel *evlist__event2evsel(struct evlist *evlist, union perf_event *event)
71175562573SAdrian Hunter {
712515dbe48SJiri Olsa 	struct evsel *first = evlist__first(evlist);
71375562573SAdrian Hunter 	struct hlist_head *head;
71475562573SAdrian Hunter 	struct perf_sample_id *sid;
71575562573SAdrian Hunter 	int hash;
71675562573SAdrian Hunter 	u64 id;
71775562573SAdrian Hunter 
7186484d2f9SJiri Olsa 	if (evlist->core.nr_entries == 1)
71998be6966SAdrian Hunter 		return first;
72098be6966SAdrian Hunter 
7211fc632ceSJiri Olsa 	if (!first->core.attr.sample_id_all &&
72298be6966SAdrian Hunter 	    event->header.type != PERF_RECORD_SAMPLE)
72398be6966SAdrian Hunter 		return first;
72475562573SAdrian Hunter 
7253ccf8a7bSArnaldo Carvalho de Melo 	if (evlist__event2id(evlist, event, &id))
72675562573SAdrian Hunter 		return NULL;
72775562573SAdrian Hunter 
72875562573SAdrian Hunter 	/* Synthesized events have an id of zero */
72975562573SAdrian Hunter 	if (!id)
73098be6966SAdrian Hunter 		return first;
73175562573SAdrian Hunter 
73275562573SAdrian Hunter 	hash = hash_64(id, PERF_EVLIST__HLIST_BITS);
7331d5af02dSJiri Olsa 	head = &evlist->core.heads[hash];
73475562573SAdrian Hunter 
73575562573SAdrian Hunter 	hlist_for_each_entry(sid, head, node) {
73675562573SAdrian Hunter 		if (sid->id == id)
73770c20369SJiri Olsa 			return container_of(sid->evsel, struct evsel, core);
73875562573SAdrian Hunter 	}
73975562573SAdrian Hunter 	return NULL;
74075562573SAdrian Hunter }
74175562573SAdrian Hunter 
7420a60b339SArnaldo Carvalho de Melo static int evlist__set_paused(struct evlist *evlist, bool value)
74365aea233SWang Nan {
74465aea233SWang Nan 	int i;
74565aea233SWang Nan 
7460b72d69aSWang Nan 	if (!evlist->overwrite_mmap)
747078c3386SWang Nan 		return 0;
748078c3386SWang Nan 
749c976ee11SJiri Olsa 	for (i = 0; i < evlist->core.nr_mmaps; i++) {
7502cf07b29SJiri Olsa 		int fd = evlist->overwrite_mmap[i].core.fd;
75165aea233SWang Nan 		int err;
75265aea233SWang Nan 
75365aea233SWang Nan 		if (fd < 0)
75465aea233SWang Nan 			continue;
75565aea233SWang Nan 		err = ioctl(fd, PERF_EVENT_IOC_PAUSE_OUTPUT, value ? 1 : 0);
75665aea233SWang Nan 		if (err)
75765aea233SWang Nan 			return err;
75865aea233SWang Nan 	}
75965aea233SWang Nan 	return 0;
76065aea233SWang Nan }
76165aea233SWang Nan 
7620a60b339SArnaldo Carvalho de Melo static int evlist__pause(struct evlist *evlist)
76365aea233SWang Nan {
7640a60b339SArnaldo Carvalho de Melo 	return evlist__set_paused(evlist, true);
76565aea233SWang Nan }
76665aea233SWang Nan 
7670a60b339SArnaldo Carvalho de Melo static int evlist__resume(struct evlist *evlist)
76865aea233SWang Nan {
7690a60b339SArnaldo Carvalho de Melo 	return evlist__set_paused(evlist, false);
77065aea233SWang Nan }
77165aea233SWang Nan 
772db6b7b13SJiri Olsa static void evlist__munmap_nofree(struct evlist *evlist)
773f8a95309SArnaldo Carvalho de Melo {
774aece948fSArnaldo Carvalho de Melo 	int i;
775f8a95309SArnaldo Carvalho de Melo 
776b2cb615dSWang Nan 	if (evlist->mmap)
777c976ee11SJiri Olsa 		for (i = 0; i < evlist->core.nr_mmaps; i++)
77880e53d11SJiri Olsa 			perf_mmap__munmap(&evlist->mmap[i].core);
779b2cb615dSWang Nan 
7800b72d69aSWang Nan 	if (evlist->overwrite_mmap)
781c976ee11SJiri Olsa 		for (i = 0; i < evlist->core.nr_mmaps; i++)
78280e53d11SJiri Olsa 			perf_mmap__munmap(&evlist->overwrite_mmap[i].core);
783a1f72618SWang Nan }
784aece948fSArnaldo Carvalho de Melo 
785db6b7b13SJiri Olsa void evlist__munmap(struct evlist *evlist)
786a1f72618SWang Nan {
787db6b7b13SJiri Olsa 	evlist__munmap_nofree(evlist);
78804662523SArnaldo Carvalho de Melo 	zfree(&evlist->mmap);
7890b72d69aSWang Nan 	zfree(&evlist->overwrite_mmap);
790f8a95309SArnaldo Carvalho de Melo }
791f8a95309SArnaldo Carvalho de Melo 
79280e53d11SJiri Olsa static void perf_mmap__unmap_cb(struct perf_mmap *map)
79380e53d11SJiri Olsa {
79480e53d11SJiri Olsa 	struct mmap *m = container_of(map, struct mmap, core);
79580e53d11SJiri Olsa 
79680e53d11SJiri Olsa 	mmap__munmap(m);
79780e53d11SJiri Olsa }
79880e53d11SJiri Olsa 
799d50cf361SJiri Olsa static struct mmap *evlist__alloc_mmap(struct evlist *evlist,
8002c5f6d87SKan Liang 				       bool overwrite)
801f8a95309SArnaldo Carvalho de Melo {
802d4c6fb36SWang Nan 	int i;
803a5830532SJiri Olsa 	struct mmap *map;
804d4c6fb36SWang Nan 
805c976ee11SJiri Olsa 	map = zalloc(evlist->core.nr_mmaps * sizeof(struct mmap));
8068db6d6b1SWang Nan 	if (!map)
8078db6d6b1SWang Nan 		return NULL;
808946ae1d4SWang Nan 
809c976ee11SJiri Olsa 	for (i = 0; i < evlist->core.nr_mmaps; i++) {
8106eb65f7aSJiri Olsa 		struct perf_mmap *prev = i ? &map[i - 1].core : NULL;
8116eb65f7aSJiri Olsa 
8124738ca30SArnaldo Carvalho de Melo 		/*
8134738ca30SArnaldo Carvalho de Melo 		 * When the perf_mmap() call is made we grab one refcount, plus
8146afad54dSKan Liang 		 * one extra to let perf_mmap__consume() get the last
8154738ca30SArnaldo Carvalho de Melo 		 * events after all real references (perf_mmap__get()) are
8164738ca30SArnaldo Carvalho de Melo 		 * dropped.
8174738ca30SArnaldo Carvalho de Melo 		 *
8184738ca30SArnaldo Carvalho de Melo 		 * Each PERF_EVENT_IOC_SET_OUTPUT points to this mmap and
8194738ca30SArnaldo Carvalho de Melo 		 * thus does perf_mmap__get() on it.
8204738ca30SArnaldo Carvalho de Melo 		 */
8216eb65f7aSJiri Olsa 		perf_mmap__init(&map[i].core, prev, overwrite, perf_mmap__unmap_cb);
8224738ca30SArnaldo Carvalho de Melo 	}
823353120b4SJiri Olsa 
8248db6d6b1SWang Nan 	return map;
825f8a95309SArnaldo Carvalho de Melo }
826f8a95309SArnaldo Carvalho de Melo 
8279abd2ab2SJiri Olsa static void
8289abd2ab2SJiri Olsa perf_evlist__mmap_cb_idx(struct perf_evlist *_evlist,
829d01508f2SAdrian Hunter 			 struct perf_evsel *_evsel,
8309abd2ab2SJiri Olsa 			 struct perf_mmap_param *_mp,
8316a7b8a5aSAdrian Hunter 			 int idx)
8329abd2ab2SJiri Olsa {
8339abd2ab2SJiri Olsa 	struct evlist *evlist = container_of(_evlist, struct evlist, core);
8349abd2ab2SJiri Olsa 	struct mmap_params *mp = container_of(_mp, struct mmap_params, core);
835d01508f2SAdrian Hunter 	struct evsel *evsel = container_of(_evsel, struct evsel, core);
8369abd2ab2SJiri Olsa 
83784bd5abaSAdrian Hunter 	auxtrace_mmap_params__set_idx(&mp->auxtrace_mp, evlist, evsel, idx);
8389abd2ab2SJiri Olsa }
8399abd2ab2SJiri Olsa 
840bb1b1885SJiri Olsa static struct perf_mmap*
841bb1b1885SJiri Olsa perf_evlist__mmap_cb_get(struct perf_evlist *_evlist, bool overwrite, int idx)
842bb1b1885SJiri Olsa {
843bb1b1885SJiri Olsa 	struct evlist *evlist = container_of(_evlist, struct evlist, core);
8443805e4f3SJiri Olsa 	struct mmap *maps;
845bb1b1885SJiri Olsa 
8463805e4f3SJiri Olsa 	maps = overwrite ? evlist->overwrite_mmap : evlist->mmap;
847bb1b1885SJiri Olsa 
848bb1b1885SJiri Olsa 	if (!maps) {
8493805e4f3SJiri Olsa 		maps = evlist__alloc_mmap(evlist, overwrite);
850bb1b1885SJiri Olsa 		if (!maps)
851bb1b1885SJiri Olsa 			return NULL;
852bb1b1885SJiri Olsa 
8533805e4f3SJiri Olsa 		if (overwrite) {
854bb1b1885SJiri Olsa 			evlist->overwrite_mmap = maps;
855bb1b1885SJiri Olsa 			if (evlist->bkw_mmap_state == BKW_MMAP_NOTREADY)
856ade9d208SArnaldo Carvalho de Melo 				evlist__toggle_bkw_mmap(evlist, BKW_MMAP_RUNNING);
8573805e4f3SJiri Olsa 		} else {
8583805e4f3SJiri Olsa 			evlist->mmap = maps;
859bb1b1885SJiri Olsa 		}
860bb1b1885SJiri Olsa 	}
861bb1b1885SJiri Olsa 
862bb1b1885SJiri Olsa 	return &maps[idx].core;
863bb1b1885SJiri Olsa }
864bb1b1885SJiri Olsa 
865b80132b1SJiri Olsa static int
866b80132b1SJiri Olsa perf_evlist__mmap_cb_mmap(struct perf_mmap *_map, struct perf_mmap_param *_mp,
8676d18804bSIan Rogers 			  int output, struct perf_cpu cpu)
868b80132b1SJiri Olsa {
869b80132b1SJiri Olsa 	struct mmap *map = container_of(_map, struct mmap, core);
870b80132b1SJiri Olsa 	struct mmap_params *mp = container_of(_mp, struct mmap_params, core);
871b80132b1SJiri Olsa 
872b80132b1SJiri Olsa 	return mmap__mmap(map, mp, output, cpu);
873b80132b1SJiri Olsa }
874b80132b1SJiri Olsa 
875f5e7150cSArnaldo Carvalho de Melo unsigned long perf_event_mlock_kb_in_pages(void)
876994a1f78SJiri Olsa {
877f5e7150cSArnaldo Carvalho de Melo 	unsigned long pages;
8788185e881SArnaldo Carvalho de Melo 	int max;
8798185e881SArnaldo Carvalho de Melo 
8808185e881SArnaldo Carvalho de Melo 	if (sysctl__read_int("kernel/perf_event_mlock_kb", &max) < 0) {
8818185e881SArnaldo Carvalho de Melo 		/*
8828185e881SArnaldo Carvalho de Melo 		 * Pick a once upon a time good value, i.e. things look
8838185e881SArnaldo Carvalho de Melo 		 * strange since we can't read a sysctl value, but lets not
8848185e881SArnaldo Carvalho de Melo 		 * die yet...
8858185e881SArnaldo Carvalho de Melo 		 */
8868185e881SArnaldo Carvalho de Melo 		max = 512;
8878185e881SArnaldo Carvalho de Melo 	} else {
8888185e881SArnaldo Carvalho de Melo 		max -= (page_size / 1024);
8898185e881SArnaldo Carvalho de Melo 	}
8908185e881SArnaldo Carvalho de Melo 
8918185e881SArnaldo Carvalho de Melo 	pages = (max * 1024) / page_size;
8921be300f4SArnaldo Carvalho de Melo 	if (!is_power_of_2(pages))
8931be300f4SArnaldo Carvalho de Melo 		pages = rounddown_pow_of_two(pages);
894f5e7150cSArnaldo Carvalho de Melo 
895f5e7150cSArnaldo Carvalho de Melo 	return pages;
896f5e7150cSArnaldo Carvalho de Melo }
897f5e7150cSArnaldo Carvalho de Melo 
8989521b5f2SJiri Olsa size_t evlist__mmap_size(unsigned long pages)
899f5e7150cSArnaldo Carvalho de Melo {
900f5e7150cSArnaldo Carvalho de Melo 	if (pages == UINT_MAX)
901f5e7150cSArnaldo Carvalho de Melo 		pages = perf_event_mlock_kb_in_pages();
902f5e7150cSArnaldo Carvalho de Melo 	else if (!is_power_of_2(pages))
903994a1f78SJiri Olsa 		return 0;
904994a1f78SJiri Olsa 
905994a1f78SJiri Olsa 	return (pages + 1) * page_size;
906994a1f78SJiri Olsa }
907994a1f78SJiri Olsa 
90833c2dcfdSDavid Ahern static long parse_pages_arg(const char *str, unsigned long min,
90933c2dcfdSDavid Ahern 			    unsigned long max)
910994a1f78SJiri Olsa {
9112fbe4abeSAdrian Hunter 	unsigned long pages, val;
91227050f53SJiri Olsa 	static struct parse_tag tags[] = {
91327050f53SJiri Olsa 		{ .tag  = 'B', .mult = 1       },
91427050f53SJiri Olsa 		{ .tag  = 'K', .mult = 1 << 10 },
91527050f53SJiri Olsa 		{ .tag  = 'M', .mult = 1 << 20 },
91627050f53SJiri Olsa 		{ .tag  = 'G', .mult = 1 << 30 },
91727050f53SJiri Olsa 		{ .tag  = 0 },
91827050f53SJiri Olsa 	};
919994a1f78SJiri Olsa 
9208973504bSDavid Ahern 	if (str == NULL)
92133c2dcfdSDavid Ahern 		return -EINVAL;
9228973504bSDavid Ahern 
92327050f53SJiri Olsa 	val = parse_tag_value(str, tags);
9242fbe4abeSAdrian Hunter 	if (val != (unsigned long) -1) {
92527050f53SJiri Olsa 		/* we got file size value */
92627050f53SJiri Olsa 		pages = PERF_ALIGN(val, page_size) / page_size;
92727050f53SJiri Olsa 	} else {
92827050f53SJiri Olsa 		/* we got pages count value */
92927050f53SJiri Olsa 		char *eptr;
930994a1f78SJiri Olsa 		pages = strtoul(str, &eptr, 10);
93133c2dcfdSDavid Ahern 		if (*eptr != '\0')
93233c2dcfdSDavid Ahern 			return -EINVAL;
93327050f53SJiri Olsa 	}
934994a1f78SJiri Olsa 
9352bcab6c1SAdrian Hunter 	if (pages == 0 && min == 0) {
93633c2dcfdSDavid Ahern 		/* leave number of pages at 0 */
9371dbfa938SAdrian Hunter 	} else if (!is_power_of_2(pages)) {
9389808143bSJiri Olsa 		char buf[100];
9399808143bSJiri Olsa 
94033c2dcfdSDavid Ahern 		/* round pages up to next power of 2 */
94191529834SArnaldo Carvalho de Melo 		pages = roundup_pow_of_two(pages);
9421dbfa938SAdrian Hunter 		if (!pages)
9431dbfa938SAdrian Hunter 			return -EINVAL;
9449808143bSJiri Olsa 
9459808143bSJiri Olsa 		unit_number__scnprintf(buf, sizeof(buf), pages * page_size);
9469808143bSJiri Olsa 		pr_info("rounding mmap pages size to %s (%lu pages)\n",
9479808143bSJiri Olsa 			buf, pages);
9482fbe4abeSAdrian Hunter 	}
9492fbe4abeSAdrian Hunter 
95033c2dcfdSDavid Ahern 	if (pages > max)
95133c2dcfdSDavid Ahern 		return -EINVAL;
95233c2dcfdSDavid Ahern 
95333c2dcfdSDavid Ahern 	return pages;
95433c2dcfdSDavid Ahern }
95533c2dcfdSDavid Ahern 
95625f84702SArnaldo Carvalho de Melo int __evlist__parse_mmap_pages(unsigned int *mmap_pages, const char *str)
95733c2dcfdSDavid Ahern {
95833c2dcfdSDavid Ahern 	unsigned long max = UINT_MAX;
95933c2dcfdSDavid Ahern 	long pages;
96033c2dcfdSDavid Ahern 
961f5ae9c42SAdrian Hunter 	if (max > SIZE_MAX / page_size)
96233c2dcfdSDavid Ahern 		max = SIZE_MAX / page_size;
96333c2dcfdSDavid Ahern 
96433c2dcfdSDavid Ahern 	pages = parse_pages_arg(str, 1, max);
96533c2dcfdSDavid Ahern 	if (pages < 0) {
96633c2dcfdSDavid Ahern 		pr_err("Invalid argument for --mmap_pages/-m\n");
967994a1f78SJiri Olsa 		return -1;
968994a1f78SJiri Olsa 	}
969994a1f78SJiri Olsa 
970994a1f78SJiri Olsa 	*mmap_pages = pages;
971994a1f78SJiri Olsa 	return 0;
972994a1f78SJiri Olsa }
973994a1f78SJiri Olsa 
97425f84702SArnaldo Carvalho de Melo int evlist__parse_mmap_pages(const struct option *opt, const char *str, int unset __maybe_unused)
975e9db1310SAdrian Hunter {
97625f84702SArnaldo Carvalho de Melo 	return __evlist__parse_mmap_pages(opt->value, str);
977e9db1310SAdrian Hunter }
978e9db1310SAdrian Hunter 
979c83fa7f2SAdrian Hunter /**
9809521b5f2SJiri Olsa  * evlist__mmap_ex - Create mmaps to receive events.
981c83fa7f2SAdrian Hunter  * @evlist: list of events
982c83fa7f2SAdrian Hunter  * @pages: map length in pages
983c83fa7f2SAdrian Hunter  * @overwrite: overwrite older events?
984718c602dSAdrian Hunter  * @auxtrace_pages - auxtrace map length in pages
985718c602dSAdrian Hunter  * @auxtrace_overwrite - overwrite older auxtrace data?
986f8a95309SArnaldo Carvalho de Melo  *
987c83fa7f2SAdrian Hunter  * If @overwrite is %false the user needs to signal event consumption using
9889521b5f2SJiri Olsa  * perf_mmap__write_tail().  Using evlist__mmap_read() does this
989c83fa7f2SAdrian Hunter  * automatically.
990f8a95309SArnaldo Carvalho de Melo  *
991718c602dSAdrian Hunter  * Similarly, if @auxtrace_overwrite is %false the user needs to signal data
992718c602dSAdrian Hunter  * consumption using auxtrace_mmap__write_tail().
993718c602dSAdrian Hunter  *
994c83fa7f2SAdrian Hunter  * Return: %0 on success, negative error code otherwise.
995f8a95309SArnaldo Carvalho de Melo  */
9969521b5f2SJiri Olsa int evlist__mmap_ex(struct evlist *evlist, unsigned int pages,
9977a276ff6SWang Nan 			 unsigned int auxtrace_pages,
99851255a8aSAlexey Budankov 			 bool auxtrace_overwrite, int nr_cblocks, int affinity, int flush,
99951255a8aSAlexey Budankov 			 int comp_level)
1000f8a95309SArnaldo Carvalho de Melo {
100171f566a3SWang Nan 	/*
100271f566a3SWang Nan 	 * Delay setting mp.prot: set it before calling perf_mmap__mmap.
100371f566a3SWang Nan 	 * Its value is decided by evsel's write_backward.
100471f566a3SWang Nan 	 * So &mp should not be passed through const pointer.
100571f566a3SWang Nan 	 */
1006e440979fSJiri Olsa 	struct mmap_params mp = {
1007e440979fSJiri Olsa 		.nr_cblocks	= nr_cblocks,
1008e440979fSJiri Olsa 		.affinity	= affinity,
1009e440979fSJiri Olsa 		.flush		= flush,
1010e440979fSJiri Olsa 		.comp_level	= comp_level
1011e440979fSJiri Olsa 	};
1012923d0f18SJiri Olsa 	struct perf_evlist_mmap_ops ops = {
10139abd2ab2SJiri Olsa 		.idx  = perf_evlist__mmap_cb_idx,
1014bb1b1885SJiri Olsa 		.get  = perf_evlist__mmap_cb_get,
1015b80132b1SJiri Olsa 		.mmap = perf_evlist__mmap_cb_mmap,
10169abd2ab2SJiri Olsa 	};
101750a682ceSArnaldo Carvalho de Melo 
1018f6fa4375SJiri Olsa 	evlist->core.mmap_len = evlist__mmap_size(pages);
1019f6fa4375SJiri Olsa 	pr_debug("mmap size %zuB\n", evlist->core.mmap_len);
1020f8a95309SArnaldo Carvalho de Melo 
1021f6fa4375SJiri Olsa 	auxtrace_mmap_params__init(&mp.auxtrace_mp, evlist->core.mmap_len,
1022718c602dSAdrian Hunter 				   auxtrace_pages, auxtrace_overwrite);
1023718c602dSAdrian Hunter 
1024923d0f18SJiri Olsa 	return perf_evlist__mmap_ops(&evlist->core, &ops, &mp.core);
1025f8a95309SArnaldo Carvalho de Melo }
10267e2ed097SArnaldo Carvalho de Melo 
10279521b5f2SJiri Olsa int evlist__mmap(struct evlist *evlist, unsigned int pages)
1028718c602dSAdrian Hunter {
10299521b5f2SJiri Olsa 	return evlist__mmap_ex(evlist, pages, 0, false, 0, PERF_AFFINITY_SYS, 1, 0);
1030718c602dSAdrian Hunter }
1031718c602dSAdrian Hunter 
10327748bb71SArnaldo Carvalho de Melo int evlist__create_maps(struct evlist *evlist, struct target *target)
10337e2ed097SArnaldo Carvalho de Melo {
1034147c508fSJin Yao 	bool all_threads = (target->per_thread && target->system_wide);
1035f854839bSJiri Olsa 	struct perf_cpu_map *cpus;
10369749b90eSJiri Olsa 	struct perf_thread_map *threads;
10377e2ed097SArnaldo Carvalho de Melo 
1038147c508fSJin Yao 	/*
1039147c508fSJin Yao 	 * If specify '-a' and '--per-thread' to perf record, perf record
1040147c508fSJin Yao 	 * will override '--per-thread'. target->per_thread = false and
1041147c508fSJin Yao 	 * target->system_wide = true.
1042147c508fSJin Yao 	 *
1043147c508fSJin Yao 	 * If specify '--per-thread' only to perf record,
1044147c508fSJin Yao 	 * target->per_thread = true and target->system_wide = false.
1045147c508fSJin Yao 	 *
1046147c508fSJin Yao 	 * So target->per_thread && target->system_wide is false.
1047147c508fSJin Yao 	 * For perf record, thread_map__new_str doesn't call
1048147c508fSJin Yao 	 * thread_map__new_all_cpus. That will keep perf record's
1049147c508fSJin Yao 	 * current behavior.
1050147c508fSJin Yao 	 *
1051147c508fSJin Yao 	 * For perf stat, it allows the case that target->per_thread and
1052147c508fSJin Yao 	 * target->system_wide are all true. It means to collect system-wide
1053147c508fSJin Yao 	 * per-thread data. thread_map__new_str will call
1054147c508fSJin Yao 	 * thread_map__new_all_cpus to enumerate all threads.
1055147c508fSJin Yao 	 */
105673c0ca1eSJin Yao 	threads = thread_map__new_str(target->pid, target->tid, target->uid,
1057147c508fSJin Yao 				      all_threads);
105874bfd2b2SAdrian Hunter 
105974bfd2b2SAdrian Hunter 	if (!threads)
10607e2ed097SArnaldo Carvalho de Melo 		return -1;
10617e2ed097SArnaldo Carvalho de Melo 
10629c105fbcSDongsheng Yang 	if (target__uses_dummy_map(target))
1063397721e0SJiri Olsa 		cpus = perf_cpu_map__dummy_new();
1064879d77d0SNamhyung Kim 	else
10659c3516d1SJiri Olsa 		cpus = perf_cpu_map__new(target->cpu_list);
10667e2ed097SArnaldo Carvalho de Melo 
106774bfd2b2SAdrian Hunter 	if (!cpus)
10687e2ed097SArnaldo Carvalho de Melo 		goto out_delete_threads;
10697e2ed097SArnaldo Carvalho de Melo 
107042249160SIan Rogers 	evlist->core.has_user_cpus = !!target->cpu_list;
1071ec9a77a7SAdrian Hunter 
1072453fa030SJiri Olsa 	perf_evlist__set_maps(&evlist->core, cpus, threads);
1073d5bc056eSAdrian Hunter 
1074bfd1b83dSNamhyung Kim 	/* as evlist now has references, put count here */
1075bfd1b83dSNamhyung Kim 	perf_cpu_map__put(cpus);
1076bfd1b83dSNamhyung Kim 	perf_thread_map__put(threads);
1077bfd1b83dSNamhyung Kim 
1078d5bc056eSAdrian Hunter 	return 0;
10797e2ed097SArnaldo Carvalho de Melo 
10807e2ed097SArnaldo Carvalho de Melo out_delete_threads:
10817836e52eSJiri Olsa 	perf_thread_map__put(threads);
10827e2ed097SArnaldo Carvalho de Melo 	return -1;
10837e2ed097SArnaldo Carvalho de Melo }
10847e2ed097SArnaldo Carvalho de Melo 
108524bf91a7SArnaldo Carvalho de Melo int evlist__apply_filters(struct evlist *evlist, struct evsel **err_evsel)
10860a102479SFrederic Weisbecker {
108732dcd021SJiri Olsa 	struct evsel *evsel;
1088745cefc5SArnaldo Carvalho de Melo 	int err = 0;
10890a102479SFrederic Weisbecker 
1090e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, evsel) {
1091d988d5eeSKan Liang 		/*
1092d988d5eeSKan Liang 		 * filters only work for tracepoint event, which doesn't have cpu limit.
1093d988d5eeSKan Liang 		 * So evlist and evsel should always be same.
1094d988d5eeSKan Liang 		 */
1095d180aa56SNamhyung Kim 		if (evsel->filter) {
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 
1103d180aa56SNamhyung Kim 		/*
1104d180aa56SNamhyung Kim 		 * non-tracepoint events can have BPF filters.
1105d180aa56SNamhyung Kim 		 */
1106d180aa56SNamhyung Kim 		if (!list_empty(&evsel->bpf_filters)) {
1107d180aa56SNamhyung Kim 			err = perf_bpf_filter__prepare(evsel);
1108d180aa56SNamhyung Kim 			if (err) {
1109d180aa56SNamhyung Kim 				*err_evsel = evsel;
1110d180aa56SNamhyung Kim 				break;
1111d180aa56SNamhyung Kim 			}
1112d180aa56SNamhyung Kim 		}
1113d180aa56SNamhyung Kim 	}
1114d180aa56SNamhyung Kim 
1115745cefc5SArnaldo Carvalho de Melo 	return err;
1116745cefc5SArnaldo Carvalho de Melo }
1117745cefc5SArnaldo Carvalho de Melo 
111824bf91a7SArnaldo Carvalho de Melo int evlist__set_tp_filter(struct evlist *evlist, const char *filter)
1119745cefc5SArnaldo Carvalho de Melo {
112032dcd021SJiri Olsa 	struct evsel *evsel;
1121745cefc5SArnaldo Carvalho de Melo 	int err = 0;
1122745cefc5SArnaldo Carvalho de Melo 
112305cea449SArnaldo Carvalho de Melo 	if (filter == NULL)
112405cea449SArnaldo Carvalho de Melo 		return -1;
112505cea449SArnaldo Carvalho de Melo 
1126e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, evsel) {
11271fc632ceSJiri Olsa 		if (evsel->core.attr.type != PERF_TYPE_TRACEPOINT)
1128fdf14720SWang Nan 			continue;
1129fdf14720SWang Nan 
1130ad681adfSArnaldo Carvalho de Melo 		err = evsel__set_filter(evsel, filter);
1131745cefc5SArnaldo Carvalho de Melo 		if (err)
1132745cefc5SArnaldo Carvalho de Melo 			break;
1133745cefc5SArnaldo Carvalho de Melo 	}
1134745cefc5SArnaldo Carvalho de Melo 
1135745cefc5SArnaldo Carvalho de Melo 	return err;
11360a102479SFrederic Weisbecker }
113774429964SFrederic Weisbecker 
113824bf91a7SArnaldo Carvalho de Melo int evlist__append_tp_filter(struct evlist *evlist, const char *filter)
113953c92f73SArnaldo Carvalho de Melo {
114053c92f73SArnaldo Carvalho de Melo 	struct evsel *evsel;
114153c92f73SArnaldo Carvalho de Melo 	int err = 0;
114253c92f73SArnaldo Carvalho de Melo 
114353c92f73SArnaldo Carvalho de Melo 	if (filter == NULL)
114453c92f73SArnaldo Carvalho de Melo 		return -1;
114553c92f73SArnaldo Carvalho de Melo 
114653c92f73SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, evsel) {
114753c92f73SArnaldo Carvalho de Melo 		if (evsel->core.attr.type != PERF_TYPE_TRACEPOINT)
114853c92f73SArnaldo Carvalho de Melo 			continue;
114953c92f73SArnaldo Carvalho de Melo 
1150ad681adfSArnaldo Carvalho de Melo 		err = evsel__append_tp_filter(evsel, filter);
115153c92f73SArnaldo Carvalho de Melo 		if (err)
115253c92f73SArnaldo Carvalho de Melo 			break;
115353c92f73SArnaldo Carvalho de Melo 	}
115453c92f73SArnaldo Carvalho de Melo 
115553c92f73SArnaldo Carvalho de Melo 	return err;
115653c92f73SArnaldo Carvalho de Melo }
115753c92f73SArnaldo Carvalho de Melo 
1158da949f50SArnaldo Carvalho de Melo char *asprintf__tp_filter_pids(size_t npids, pid_t *pids)
1159cfd70a26SArnaldo Carvalho de Melo {
1160cfd70a26SArnaldo Carvalho de Melo 	char *filter;
1161be199adaSArnaldo Carvalho de Melo 	size_t i;
1162cfd70a26SArnaldo Carvalho de Melo 
1163be199adaSArnaldo Carvalho de Melo 	for (i = 0; i < npids; ++i) {
1164be199adaSArnaldo Carvalho de Melo 		if (i == 0) {
1165be199adaSArnaldo Carvalho de Melo 			if (asprintf(&filter, "common_pid != %d", pids[i]) < 0)
116605cea449SArnaldo Carvalho de Melo 				return NULL;
1167be199adaSArnaldo Carvalho de Melo 		} else {
1168be199adaSArnaldo Carvalho de Melo 			char *tmp;
1169be199adaSArnaldo Carvalho de Melo 
1170be199adaSArnaldo Carvalho de Melo 			if (asprintf(&tmp, "%s && common_pid != %d", filter, pids[i]) < 0)
1171be199adaSArnaldo Carvalho de Melo 				goto out_free;
1172be199adaSArnaldo Carvalho de Melo 
1173be199adaSArnaldo Carvalho de Melo 			free(filter);
1174be199adaSArnaldo Carvalho de Melo 			filter = tmp;
1175be199adaSArnaldo Carvalho de Melo 		}
1176be199adaSArnaldo Carvalho de Melo 	}
1177cfd70a26SArnaldo Carvalho de Melo 
117805cea449SArnaldo Carvalho de Melo 	return filter;
1179be199adaSArnaldo Carvalho de Melo out_free:
1180cfd70a26SArnaldo Carvalho de Melo 	free(filter);
118105cea449SArnaldo Carvalho de Melo 	return NULL;
118205cea449SArnaldo Carvalho de Melo }
118305cea449SArnaldo Carvalho de Melo 
118424bf91a7SArnaldo Carvalho de Melo int evlist__set_tp_filter_pids(struct evlist *evlist, size_t npids, pid_t *pids)
118505cea449SArnaldo Carvalho de Melo {
118605cea449SArnaldo Carvalho de Melo 	char *filter = asprintf__tp_filter_pids(npids, pids);
118724bf91a7SArnaldo Carvalho de Melo 	int ret = evlist__set_tp_filter(evlist, filter);
118805cea449SArnaldo Carvalho de Melo 
118905cea449SArnaldo Carvalho de Melo 	free(filter);
1190cfd70a26SArnaldo Carvalho de Melo 	return ret;
1191cfd70a26SArnaldo Carvalho de Melo }
1192cfd70a26SArnaldo Carvalho de Melo 
119324bf91a7SArnaldo Carvalho de Melo int evlist__set_tp_filter_pid(struct evlist *evlist, pid_t pid)
1194be199adaSArnaldo Carvalho de Melo {
119524bf91a7SArnaldo Carvalho de Melo 	return evlist__set_tp_filter_pids(evlist, 1, &pid);
1196be199adaSArnaldo Carvalho de Melo }
1197be199adaSArnaldo Carvalho de Melo 
119824bf91a7SArnaldo Carvalho de Melo int evlist__append_tp_filter_pids(struct evlist *evlist, size_t npids, pid_t *pids)
11991827ab5bSArnaldo Carvalho de Melo {
12001827ab5bSArnaldo Carvalho de Melo 	char *filter = asprintf__tp_filter_pids(npids, pids);
120124bf91a7SArnaldo Carvalho de Melo 	int ret = evlist__append_tp_filter(evlist, filter);
12021827ab5bSArnaldo Carvalho de Melo 
12031827ab5bSArnaldo Carvalho de Melo 	free(filter);
12041827ab5bSArnaldo Carvalho de Melo 	return ret;
12051827ab5bSArnaldo Carvalho de Melo }
12061827ab5bSArnaldo Carvalho de Melo 
120724bf91a7SArnaldo Carvalho de Melo int evlist__append_tp_filter_pid(struct evlist *evlist, pid_t pid)
12081827ab5bSArnaldo Carvalho de Melo {
120924bf91a7SArnaldo Carvalho de Melo 	return evlist__append_tp_filter_pids(evlist, 1, &pid);
12101827ab5bSArnaldo Carvalho de Melo }
12111827ab5bSArnaldo Carvalho de Melo 
1212b3c2cc2bSArnaldo Carvalho de Melo bool evlist__valid_sample_type(struct evlist *evlist)
121374429964SFrederic Weisbecker {
121432dcd021SJiri Olsa 	struct evsel *pos;
1215c2a70653SArnaldo Carvalho de Melo 
12166484d2f9SJiri Olsa 	if (evlist->core.nr_entries == 1)
121775562573SAdrian Hunter 		return true;
121875562573SAdrian Hunter 
121975562573SAdrian Hunter 	if (evlist->id_pos < 0 || evlist->is_pos < 0)
122075562573SAdrian Hunter 		return false;
122175562573SAdrian Hunter 
1222e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, pos) {
122375562573SAdrian Hunter 		if (pos->id_pos != evlist->id_pos ||
122475562573SAdrian Hunter 		    pos->is_pos != evlist->is_pos)
1225c2a70653SArnaldo Carvalho de Melo 			return false;
122674429964SFrederic Weisbecker 	}
122774429964SFrederic Weisbecker 
1228c2a70653SArnaldo Carvalho de Melo 	return true;
1229c2a70653SArnaldo Carvalho de Melo }
1230c2a70653SArnaldo Carvalho de Melo 
1231b3c2cc2bSArnaldo Carvalho de Melo u64 __evlist__combined_sample_type(struct evlist *evlist)
1232c2a70653SArnaldo Carvalho de Melo {
123332dcd021SJiri Olsa 	struct evsel *evsel;
123475562573SAdrian Hunter 
123575562573SAdrian Hunter 	if (evlist->combined_sample_type)
123675562573SAdrian Hunter 		return evlist->combined_sample_type;
123775562573SAdrian Hunter 
1238e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, evsel)
12391fc632ceSJiri Olsa 		evlist->combined_sample_type |= evsel->core.attr.sample_type;
124075562573SAdrian Hunter 
124175562573SAdrian Hunter 	return evlist->combined_sample_type;
124275562573SAdrian Hunter }
124375562573SAdrian Hunter 
1244b3c2cc2bSArnaldo Carvalho de Melo u64 evlist__combined_sample_type(struct evlist *evlist)
124575562573SAdrian Hunter {
124675562573SAdrian Hunter 	evlist->combined_sample_type = 0;
1247b3c2cc2bSArnaldo Carvalho de Melo 	return __evlist__combined_sample_type(evlist);
1248c2a70653SArnaldo Carvalho de Melo }
1249c2a70653SArnaldo Carvalho de Melo 
125092c7d7cdSArnaldo Carvalho de Melo u64 evlist__combined_branch_type(struct evlist *evlist)
125198df858eSAndi Kleen {
125232dcd021SJiri Olsa 	struct evsel *evsel;
125398df858eSAndi Kleen 	u64 branch_type = 0;
125498df858eSAndi Kleen 
1255e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, evsel)
12561fc632ceSJiri Olsa 		branch_type |= evsel->core.attr.branch_sample_type;
125798df858eSAndi Kleen 	return branch_type;
125898df858eSAndi Kleen }
125998df858eSAndi Kleen 
126078e1bc25SArnaldo Carvalho de Melo bool evlist__valid_read_format(struct evlist *evlist)
12619ede473cSJiri Olsa {
1262515dbe48SJiri Olsa 	struct evsel *first = evlist__first(evlist), *pos = first;
12631fc632ceSJiri Olsa 	u64 read_format = first->core.attr.read_format;
12641fc632ceSJiri Olsa 	u64 sample_type = first->core.attr.sample_type;
12659ede473cSJiri Olsa 
1266e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, pos) {
126794d3820fSAdrian Hunter 		if (read_format != pos->core.attr.read_format) {
126894d3820fSAdrian Hunter 			pr_debug("Read format differs %#" PRIx64 " vs %#" PRIx64 "\n",
126994d3820fSAdrian Hunter 				 read_format, (u64)pos->core.attr.read_format);
127094d3820fSAdrian Hunter 		}
12719ede473cSJiri Olsa 	}
12729ede473cSJiri Olsa 
12734d39c89fSIngo Molnar 	/* PERF_SAMPLE_READ implies PERF_FORMAT_ID. */
12749ede473cSJiri Olsa 	if ((sample_type & PERF_SAMPLE_READ) &&
12759ede473cSJiri Olsa 	    !(read_format & PERF_FORMAT_ID)) {
12769ede473cSJiri Olsa 		return false;
12779ede473cSJiri Olsa 	}
12789ede473cSJiri Olsa 
12799ede473cSJiri Olsa 	return true;
12809ede473cSJiri Olsa }
12819ede473cSJiri Olsa 
12821420ba2fSArnaldo Carvalho de Melo u16 evlist__id_hdr_size(struct evlist *evlist)
128381e36bffSArnaldo Carvalho de Melo {
1284515dbe48SJiri Olsa 	struct evsel *first = evlist__first(evlist);
128581e36bffSArnaldo Carvalho de Melo 
12860a64de04SAdrian Hunter 	return first->core.attr.sample_id_all ? evsel__id_hdr_size(first) : 0;
128781e36bffSArnaldo Carvalho de Melo }
128881e36bffSArnaldo Carvalho de Melo 
12898cedf3a5SArnaldo Carvalho de Melo bool evlist__valid_sample_id_all(struct evlist *evlist)
1290c2a70653SArnaldo Carvalho de Melo {
1291515dbe48SJiri Olsa 	struct evsel *first = evlist__first(evlist), *pos = first;
1292c2a70653SArnaldo Carvalho de Melo 
1293e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry_continue(evlist, pos) {
12941fc632ceSJiri Olsa 		if (first->core.attr.sample_id_all != pos->core.attr.sample_id_all)
1295c2a70653SArnaldo Carvalho de Melo 			return false;
1296c2a70653SArnaldo Carvalho de Melo 	}
1297c2a70653SArnaldo Carvalho de Melo 
1298c2a70653SArnaldo Carvalho de Melo 	return true;
129974429964SFrederic Weisbecker }
130074429964SFrederic Weisbecker 
13018cedf3a5SArnaldo Carvalho de Melo bool evlist__sample_id_all(struct evlist *evlist)
130274429964SFrederic Weisbecker {
1303515dbe48SJiri Olsa 	struct evsel *first = evlist__first(evlist);
13041fc632ceSJiri Olsa 	return first->core.attr.sample_id_all;
130574429964SFrederic Weisbecker }
130681cce8deSArnaldo Carvalho de Melo 
1307900c8eadSArnaldo Carvalho de Melo void evlist__set_selected(struct evlist *evlist, struct evsel *evsel)
130881cce8deSArnaldo Carvalho de Melo {
130981cce8deSArnaldo Carvalho de Melo 	evlist->selected = evsel;
131081cce8deSArnaldo Carvalho de Melo }
1311727ab04eSArnaldo Carvalho de Melo 
1312750b4edeSJiri Olsa void evlist__close(struct evlist *evlist)
1313a74b4b66SNamhyung Kim {
131432dcd021SJiri Olsa 	struct evsel *evsel;
1315472832d2SIan Rogers 	struct evlist_cpu_iterator evlist_cpu_itr;
13167736627bSAndi Kleen 	struct affinity affinity;
1317a74b4b66SNamhyung Kim 
13187736627bSAndi Kleen 	/*
13190df6ade7SIan Rogers 	 * With perf record core.user_requested_cpus is usually NULL.
13207736627bSAndi Kleen 	 * Use the old method to handle this for now.
13217736627bSAndi Kleen 	 */
13220df6ade7SIan Rogers 	if (!evlist->core.user_requested_cpus ||
13230df6ade7SIan Rogers 	    cpu_map__is_dummy(evlist->core.user_requested_cpus)) {
1324475fb533SAndi Kleen 		evlist__for_each_entry_reverse(evlist, evsel)
132588761fa1SJiri Olsa 			evsel__close(evsel);
13267736627bSAndi Kleen 		return;
13277736627bSAndi Kleen 	}
13287736627bSAndi Kleen 
13297736627bSAndi Kleen 	if (affinity__setup(&affinity) < 0)
13307736627bSAndi Kleen 		return;
13317736627bSAndi Kleen 
1332472832d2SIan Rogers 	evlist__for_each_cpu(evlist_cpu_itr, evlist, &affinity) {
1333472832d2SIan Rogers 		perf_evsel__close_cpu(&evlist_cpu_itr.evsel->core,
1334472832d2SIan Rogers 				      evlist_cpu_itr.cpu_map_idx);
13357736627bSAndi Kleen 	}
1336472832d2SIan Rogers 
13377736627bSAndi Kleen 	affinity__cleanup(&affinity);
13387736627bSAndi Kleen 	evlist__for_each_entry_reverse(evlist, evsel) {
13397736627bSAndi Kleen 		perf_evsel__free_fd(&evsel->core);
13407736627bSAndi Kleen 		perf_evsel__free_id(&evsel->core);
13417736627bSAndi Kleen 	}
1342513068f2SNamhyung Kim 	perf_evlist__reset_id_hash(&evlist->core);
1343a74b4b66SNamhyung Kim }
1344a74b4b66SNamhyung Kim 
13457748bb71SArnaldo Carvalho de Melo static int evlist__create_syswide_maps(struct evlist *evlist)
13464112eb18SArnaldo Carvalho de Melo {
1347f854839bSJiri Olsa 	struct perf_cpu_map *cpus;
13489749b90eSJiri Olsa 	struct perf_thread_map *threads;
13494112eb18SArnaldo Carvalho de Melo 
13504112eb18SArnaldo Carvalho de Melo 	/*
13514112eb18SArnaldo Carvalho de Melo 	 * Try reading /sys/devices/system/cpu/online to get
13524112eb18SArnaldo Carvalho de Melo 	 * an all cpus map.
13534112eb18SArnaldo Carvalho de Melo 	 *
13544112eb18SArnaldo Carvalho de Melo 	 * FIXME: -ENOMEM is the best we can do here, the cpu_map
13554112eb18SArnaldo Carvalho de Melo 	 * code needs an overhaul to properly forward the
13564112eb18SArnaldo Carvalho de Melo 	 * error, and we may not want to do that fallback to a
13574112eb18SArnaldo Carvalho de Melo 	 * default cpu identity map :-\
13584112eb18SArnaldo Carvalho de Melo 	 */
13599c3516d1SJiri Olsa 	cpus = perf_cpu_map__new(NULL);
13608c0498b6SAdrian Hunter 	if (!cpus)
13614112eb18SArnaldo Carvalho de Melo 		goto out;
13624112eb18SArnaldo Carvalho de Melo 
13634b49cce2SJiri Olsa 	threads = perf_thread_map__new_dummy();
13648c0498b6SAdrian Hunter 	if (!threads)
13658c0498b6SAdrian Hunter 		goto out_put;
13664112eb18SArnaldo Carvalho de Melo 
1367453fa030SJiri Olsa 	perf_evlist__set_maps(&evlist->core, cpus, threads);
1368bfd1b83dSNamhyung Kim 
1369bfd1b83dSNamhyung Kim 	perf_thread_map__put(threads);
13708c0498b6SAdrian Hunter out_put:
137138f01d8dSJiri Olsa 	perf_cpu_map__put(cpus);
1372bfd1b83dSNamhyung Kim out:
1373f717d89aSHaowen Bai 	return -ENOMEM;
13744112eb18SArnaldo Carvalho de Melo }
13754112eb18SArnaldo Carvalho de Melo 
1376474ddc4cSJiri Olsa int evlist__open(struct evlist *evlist)
1377727ab04eSArnaldo Carvalho de Melo {
137832dcd021SJiri Olsa 	struct evsel *evsel;
1379a74b4b66SNamhyung Kim 	int err;
1380727ab04eSArnaldo Carvalho de Melo 
13814112eb18SArnaldo Carvalho de Melo 	/*
13824112eb18SArnaldo Carvalho de Melo 	 * Default: one fd per CPU, all threads, aka systemwide
13834112eb18SArnaldo Carvalho de Melo 	 * as sys_perf_event_open(cpu = -1, thread = -1) is EINVAL
13844112eb18SArnaldo Carvalho de Melo 	 */
13850df6ade7SIan Rogers 	if (evlist->core.threads == NULL && evlist->core.user_requested_cpus == NULL) {
13867748bb71SArnaldo Carvalho de Melo 		err = evlist__create_syswide_maps(evlist);
13874112eb18SArnaldo Carvalho de Melo 		if (err < 0)
13884112eb18SArnaldo Carvalho de Melo 			goto out_err;
13894112eb18SArnaldo Carvalho de Melo 	}
13904112eb18SArnaldo Carvalho de Melo 
13910a7e7ec9SArnaldo Carvalho de Melo 	evlist__update_id_pos(evlist);
1392733cd2feSAdrian Hunter 
1393e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, evsel) {
1394af663bd0SJiri Olsa 		err = evsel__open(evsel, evsel->core.cpus, evsel->core.threads);
1395727ab04eSArnaldo Carvalho de Melo 		if (err < 0)
1396727ab04eSArnaldo Carvalho de Melo 			goto out_err;
1397727ab04eSArnaldo Carvalho de Melo 	}
1398727ab04eSArnaldo Carvalho de Melo 
1399727ab04eSArnaldo Carvalho de Melo 	return 0;
1400727ab04eSArnaldo Carvalho de Melo out_err:
1401750b4edeSJiri Olsa 	evlist__close(evlist);
140241c21a68SNamhyung Kim 	errno = -err;
1403727ab04eSArnaldo Carvalho de Melo 	return err;
1404727ab04eSArnaldo Carvalho de Melo }
140535b9d88eSArnaldo Carvalho de Melo 
14067b392ef0SArnaldo Carvalho de Melo int evlist__prepare_workload(struct evlist *evlist, struct target *target, const char *argv[],
14077b392ef0SArnaldo Carvalho de Melo 			     bool pipe_output, void (*exec_error)(int signo, siginfo_t *info, void *ucontext))
140835b9d88eSArnaldo Carvalho de Melo {
140935b9d88eSArnaldo Carvalho de Melo 	int child_ready_pipe[2], go_pipe[2];
141035b9d88eSArnaldo Carvalho de Melo 	char bf;
141135b9d88eSArnaldo Carvalho de Melo 
141235b9d88eSArnaldo Carvalho de Melo 	if (pipe(child_ready_pipe) < 0) {
141335b9d88eSArnaldo Carvalho de Melo 		perror("failed to create 'ready' pipe");
141435b9d88eSArnaldo Carvalho de Melo 		return -1;
141535b9d88eSArnaldo Carvalho de Melo 	}
141635b9d88eSArnaldo Carvalho de Melo 
141735b9d88eSArnaldo Carvalho de Melo 	if (pipe(go_pipe) < 0) {
141835b9d88eSArnaldo Carvalho de Melo 		perror("failed to create 'go' pipe");
141935b9d88eSArnaldo Carvalho de Melo 		goto out_close_ready_pipe;
142035b9d88eSArnaldo Carvalho de Melo 	}
142135b9d88eSArnaldo Carvalho de Melo 
142235b9d88eSArnaldo Carvalho de Melo 	evlist->workload.pid = fork();
142335b9d88eSArnaldo Carvalho de Melo 	if (evlist->workload.pid < 0) {
142435b9d88eSArnaldo Carvalho de Melo 		perror("failed to fork");
142535b9d88eSArnaldo Carvalho de Melo 		goto out_close_pipes;
142635b9d88eSArnaldo Carvalho de Melo 	}
142735b9d88eSArnaldo Carvalho de Melo 
142835b9d88eSArnaldo Carvalho de Melo 	if (!evlist->workload.pid) {
14295f1c4225SArnaldo Carvalho de Melo 		int ret;
14305f1c4225SArnaldo Carvalho de Melo 
1431119fa3c9SNamhyung Kim 		if (pipe_output)
143235b9d88eSArnaldo Carvalho de Melo 			dup2(2, 1);
143335b9d88eSArnaldo Carvalho de Melo 
14340817df08SDavid Ahern 		signal(SIGTERM, SIG_DFL);
14350817df08SDavid Ahern 
143635b9d88eSArnaldo Carvalho de Melo 		close(child_ready_pipe[0]);
143735b9d88eSArnaldo Carvalho de Melo 		close(go_pipe[1]);
143835b9d88eSArnaldo Carvalho de Melo 		fcntl(go_pipe[0], F_SETFD, FD_CLOEXEC);
143935b9d88eSArnaldo Carvalho de Melo 
144035b9d88eSArnaldo Carvalho de Melo 		/*
1441a7672d1dSArnaldo Carvalho de Melo 		 * Change the name of this process not to confuse --exclude-perf users
1442a7672d1dSArnaldo Carvalho de Melo 		 * that sees 'perf' in the window up to the execvp() and thinks that
1443a7672d1dSArnaldo Carvalho de Melo 		 * perf samples are not being excluded.
1444a7672d1dSArnaldo Carvalho de Melo 		 */
1445a7672d1dSArnaldo Carvalho de Melo 		prctl(PR_SET_NAME, "perf-exec");
1446a7672d1dSArnaldo Carvalho de Melo 
1447a7672d1dSArnaldo Carvalho de Melo 		/*
144835b9d88eSArnaldo Carvalho de Melo 		 * Tell the parent we're ready to go
144935b9d88eSArnaldo Carvalho de Melo 		 */
145035b9d88eSArnaldo Carvalho de Melo 		close(child_ready_pipe[1]);
145135b9d88eSArnaldo Carvalho de Melo 
145235b9d88eSArnaldo Carvalho de Melo 		/*
145335b9d88eSArnaldo Carvalho de Melo 		 * Wait until the parent tells us to go.
145435b9d88eSArnaldo Carvalho de Melo 		 */
14555f1c4225SArnaldo Carvalho de Melo 		ret = read(go_pipe[0], &bf, 1);
14565f1c4225SArnaldo Carvalho de Melo 		/*
14575f1c4225SArnaldo Carvalho de Melo 		 * The parent will ask for the execvp() to be performed by
14585f1c4225SArnaldo Carvalho de Melo 		 * writing exactly one byte, in workload.cork_fd, usually via
14597b392ef0SArnaldo Carvalho de Melo 		 * evlist__start_workload().
14605f1c4225SArnaldo Carvalho de Melo 		 *
146120f86fc1SArnaldo Carvalho de Melo 		 * For cancelling the workload without actually running it,
14625f1c4225SArnaldo Carvalho de Melo 		 * the parent will just close workload.cork_fd, without writing
14635f1c4225SArnaldo Carvalho de Melo 		 * anything, i.e. read will return zero and we just exit()
14645f1c4225SArnaldo Carvalho de Melo 		 * here.
14655f1c4225SArnaldo Carvalho de Melo 		 */
14665f1c4225SArnaldo Carvalho de Melo 		if (ret != 1) {
14675f1c4225SArnaldo Carvalho de Melo 			if (ret == -1)
146835b9d88eSArnaldo Carvalho de Melo 				perror("unable to read pipe");
14695f1c4225SArnaldo Carvalho de Melo 			exit(ret);
14705f1c4225SArnaldo Carvalho de Melo 		}
147135b9d88eSArnaldo Carvalho de Melo 
147235b9d88eSArnaldo Carvalho de Melo 		execvp(argv[0], (char **)argv);
147335b9d88eSArnaldo Carvalho de Melo 
1474735f7e0bSArnaldo Carvalho de Melo 		if (exec_error) {
1475f33cbe72SArnaldo Carvalho de Melo 			union sigval val;
1476f33cbe72SArnaldo Carvalho de Melo 
1477f33cbe72SArnaldo Carvalho de Melo 			val.sival_int = errno;
1478f33cbe72SArnaldo Carvalho de Melo 			if (sigqueue(getppid(), SIGUSR1, val))
147935b9d88eSArnaldo Carvalho de Melo 				perror(argv[0]);
1480f33cbe72SArnaldo Carvalho de Melo 		} else
1481f33cbe72SArnaldo Carvalho de Melo 			perror(argv[0]);
148235b9d88eSArnaldo Carvalho de Melo 		exit(-1);
148335b9d88eSArnaldo Carvalho de Melo 	}
148435b9d88eSArnaldo Carvalho de Melo 
1485735f7e0bSArnaldo Carvalho de Melo 	if (exec_error) {
1486735f7e0bSArnaldo Carvalho de Melo 		struct sigaction act = {
1487735f7e0bSArnaldo Carvalho de Melo 			.sa_flags     = SA_SIGINFO,
1488735f7e0bSArnaldo Carvalho de Melo 			.sa_sigaction = exec_error,
1489735f7e0bSArnaldo Carvalho de Melo 		};
1490735f7e0bSArnaldo Carvalho de Melo 		sigaction(SIGUSR1, &act, NULL);
1491735f7e0bSArnaldo Carvalho de Melo 	}
1492735f7e0bSArnaldo Carvalho de Melo 
14931aaf63b1SArnaldo Carvalho de Melo 	if (target__none(target)) {
149403617c22SJiri Olsa 		if (evlist->core.threads == NULL) {
14951aaf63b1SArnaldo Carvalho de Melo 			fprintf(stderr, "FATAL: evlist->threads need to be set at this point (%s:%d).\n",
14961aaf63b1SArnaldo Carvalho de Melo 				__func__, __LINE__);
14971aaf63b1SArnaldo Carvalho de Melo 			goto out_close_pipes;
14981aaf63b1SArnaldo Carvalho de Melo 		}
149903617c22SJiri Olsa 		perf_thread_map__set_pid(evlist->core.threads, 0, evlist->workload.pid);
15001aaf63b1SArnaldo Carvalho de Melo 	}
150135b9d88eSArnaldo Carvalho de Melo 
150235b9d88eSArnaldo Carvalho de Melo 	close(child_ready_pipe[1]);
150335b9d88eSArnaldo Carvalho de Melo 	close(go_pipe[0]);
150435b9d88eSArnaldo Carvalho de Melo 	/*
150535b9d88eSArnaldo Carvalho de Melo 	 * wait for child to settle
150635b9d88eSArnaldo Carvalho de Melo 	 */
150735b9d88eSArnaldo Carvalho de Melo 	if (read(child_ready_pipe[0], &bf, 1) == -1) {
150835b9d88eSArnaldo Carvalho de Melo 		perror("unable to read pipe");
150935b9d88eSArnaldo Carvalho de Melo 		goto out_close_pipes;
151035b9d88eSArnaldo Carvalho de Melo 	}
151135b9d88eSArnaldo Carvalho de Melo 
1512bcf3145fSNamhyung Kim 	fcntl(go_pipe[1], F_SETFD, FD_CLOEXEC);
151335b9d88eSArnaldo Carvalho de Melo 	evlist->workload.cork_fd = go_pipe[1];
151435b9d88eSArnaldo Carvalho de Melo 	close(child_ready_pipe[0]);
151535b9d88eSArnaldo Carvalho de Melo 	return 0;
151635b9d88eSArnaldo Carvalho de Melo 
151735b9d88eSArnaldo Carvalho de Melo out_close_pipes:
151835b9d88eSArnaldo Carvalho de Melo 	close(go_pipe[0]);
151935b9d88eSArnaldo Carvalho de Melo 	close(go_pipe[1]);
152035b9d88eSArnaldo Carvalho de Melo out_close_ready_pipe:
152135b9d88eSArnaldo Carvalho de Melo 	close(child_ready_pipe[0]);
152235b9d88eSArnaldo Carvalho de Melo 	close(child_ready_pipe[1]);
152335b9d88eSArnaldo Carvalho de Melo 	return -1;
152435b9d88eSArnaldo Carvalho de Melo }
152535b9d88eSArnaldo Carvalho de Melo 
15267b392ef0SArnaldo Carvalho de Melo int evlist__start_workload(struct evlist *evlist)
152735b9d88eSArnaldo Carvalho de Melo {
152835b9d88eSArnaldo Carvalho de Melo 	if (evlist->workload.cork_fd > 0) {
1529b3824404SDavid Ahern 		char bf = 0;
1530bcf3145fSNamhyung Kim 		int ret;
153135b9d88eSArnaldo Carvalho de Melo 		/*
153235b9d88eSArnaldo Carvalho de Melo 		 * Remove the cork, let it rip!
153335b9d88eSArnaldo Carvalho de Melo 		 */
1534bcf3145fSNamhyung Kim 		ret = write(evlist->workload.cork_fd, &bf, 1);
1535bcf3145fSNamhyung Kim 		if (ret < 0)
1536e978be9eSSoramichi Akiyama 			perror("unable to write to pipe");
1537bcf3145fSNamhyung Kim 
1538bcf3145fSNamhyung Kim 		close(evlist->workload.cork_fd);
1539bcf3145fSNamhyung Kim 		return ret;
154035b9d88eSArnaldo Carvalho de Melo 	}
154135b9d88eSArnaldo Carvalho de Melo 
154235b9d88eSArnaldo Carvalho de Melo 	return 0;
154335b9d88eSArnaldo Carvalho de Melo }
1544cb0b29e0SArnaldo Carvalho de Melo 
15452a6599cdSArnaldo Carvalho de Melo int evlist__parse_sample(struct evlist *evlist, union perf_event *event, struct perf_sample *sample)
1546cb0b29e0SArnaldo Carvalho de Melo {
15473ccf8a7bSArnaldo Carvalho de Melo 	struct evsel *evsel = evlist__event2evsel(evlist, event);
15483461b65dSAdrian Hunter 	int ret;
154975562573SAdrian Hunter 
155075562573SAdrian Hunter 	if (!evsel)
155175562573SAdrian Hunter 		return -EFAULT;
15523461b65dSAdrian Hunter 	ret = evsel__parse_sample(evsel, event, sample);
15533461b65dSAdrian Hunter 	if (ret)
15543461b65dSAdrian Hunter 		return ret;
15553461b65dSAdrian Hunter 	if (perf_guest && sample->id) {
15563461b65dSAdrian Hunter 		struct perf_sample_id *sid = evlist__id2sid(evlist, sample->id);
15573461b65dSAdrian Hunter 
15583461b65dSAdrian Hunter 		if (sid) {
15593461b65dSAdrian Hunter 			sample->machine_pid = sid->machine_pid;
15603461b65dSAdrian Hunter 			sample->vcpu = sid->vcpu.cpu;
15613461b65dSAdrian Hunter 		}
15623461b65dSAdrian Hunter 	}
15633461b65dSAdrian Hunter 	return 0;
1564cb0b29e0SArnaldo Carvalho de Melo }
156578f067b3SArnaldo Carvalho de Melo 
15662a6599cdSArnaldo Carvalho de Melo int evlist__parse_sample_timestamp(struct evlist *evlist, union perf_event *event, u64 *timestamp)
156701468120SJiri Olsa {
15683ccf8a7bSArnaldo Carvalho de Melo 	struct evsel *evsel = evlist__event2evsel(evlist, event);
156901468120SJiri Olsa 
157001468120SJiri Olsa 	if (!evsel)
157101468120SJiri Olsa 		return -EFAULT;
15726b6017a2SArnaldo Carvalho de Melo 	return evsel__parse_sample_timestamp(evsel, event, timestamp);
157301468120SJiri Olsa }
157401468120SJiri Olsa 
1575d1f249ecSArnaldo Carvalho de Melo int evlist__strerror_open(struct evlist *evlist, int err, char *buf, size_t size)
1576a8f23d8fSArnaldo Carvalho de Melo {
1577a8f23d8fSArnaldo Carvalho de Melo 	int printed, value;
1578c8b5f2c9SArnaldo Carvalho de Melo 	char sbuf[STRERR_BUFSIZE], *emsg = str_error_r(err, sbuf, sizeof(sbuf));
1579a8f23d8fSArnaldo Carvalho de Melo 
1580a8f23d8fSArnaldo Carvalho de Melo 	switch (err) {
1581a8f23d8fSArnaldo Carvalho de Melo 	case EACCES:
1582a8f23d8fSArnaldo Carvalho de Melo 	case EPERM:
1583a8f23d8fSArnaldo Carvalho de Melo 		printed = scnprintf(buf, size,
1584a8f23d8fSArnaldo Carvalho de Melo 				    "Error:\t%s.\n"
1585a8f23d8fSArnaldo Carvalho de Melo 				    "Hint:\tCheck /proc/sys/kernel/perf_event_paranoid setting.", emsg);
1586a8f23d8fSArnaldo Carvalho de Melo 
15871a47245dSAdrian Hunter 		value = perf_event_paranoid();
1588a8f23d8fSArnaldo Carvalho de Melo 
1589a8f23d8fSArnaldo Carvalho de Melo 		printed += scnprintf(buf + printed, size - printed, "\nHint:\t");
1590a8f23d8fSArnaldo Carvalho de Melo 
1591a8f23d8fSArnaldo Carvalho de Melo 		if (value >= 2) {
1592a8f23d8fSArnaldo Carvalho de Melo 			printed += scnprintf(buf + printed, size - printed,
1593a8f23d8fSArnaldo Carvalho de Melo 					     "For your workloads it needs to be <= 1\nHint:\t");
1594a8f23d8fSArnaldo Carvalho de Melo 		}
1595a8f23d8fSArnaldo Carvalho de Melo 		printed += scnprintf(buf + printed, size - printed,
15965229e366SArnaldo Carvalho de Melo 				     "For system wide tracing it needs to be set to -1.\n");
1597a8f23d8fSArnaldo Carvalho de Melo 
1598a8f23d8fSArnaldo Carvalho de Melo 		printed += scnprintf(buf + printed, size - printed,
15995229e366SArnaldo Carvalho de Melo 				    "Hint:\tTry: 'sudo sh -c \"echo -1 > /proc/sys/kernel/perf_event_paranoid\"'\n"
16005229e366SArnaldo Carvalho de Melo 				    "Hint:\tThe current value is %d.", value);
1601a8f23d8fSArnaldo Carvalho de Melo 		break;
1602d9aade7fSArnaldo Carvalho de Melo 	case EINVAL: {
1603515dbe48SJiri Olsa 		struct evsel *first = evlist__first(evlist);
1604d9aade7fSArnaldo Carvalho de Melo 		int max_freq;
1605d9aade7fSArnaldo Carvalho de Melo 
1606d9aade7fSArnaldo Carvalho de Melo 		if (sysctl__read_int("kernel/perf_event_max_sample_rate", &max_freq) < 0)
1607d9aade7fSArnaldo Carvalho de Melo 			goto out_default;
1608d9aade7fSArnaldo Carvalho de Melo 
16091fc632ceSJiri Olsa 		if (first->core.attr.sample_freq < (u64)max_freq)
1610d9aade7fSArnaldo Carvalho de Melo 			goto out_default;
1611d9aade7fSArnaldo Carvalho de Melo 
1612d9aade7fSArnaldo Carvalho de Melo 		printed = scnprintf(buf, size,
1613d9aade7fSArnaldo Carvalho de Melo 				    "Error:\t%s.\n"
1614d9aade7fSArnaldo Carvalho de Melo 				    "Hint:\tCheck /proc/sys/kernel/perf_event_max_sample_rate.\n"
1615d9aade7fSArnaldo Carvalho de Melo 				    "Hint:\tThe current value is %d and %" PRIu64 " is being requested.",
16161fc632ceSJiri Olsa 				    emsg, max_freq, first->core.attr.sample_freq);
1617d9aade7fSArnaldo Carvalho de Melo 		break;
1618d9aade7fSArnaldo Carvalho de Melo 	}
1619a8f23d8fSArnaldo Carvalho de Melo 	default:
1620d9aade7fSArnaldo Carvalho de Melo out_default:
1621a8f23d8fSArnaldo Carvalho de Melo 		scnprintf(buf, size, "%s", emsg);
1622a8f23d8fSArnaldo Carvalho de Melo 		break;
1623a8f23d8fSArnaldo Carvalho de Melo 	}
1624a8f23d8fSArnaldo Carvalho de Melo 
1625a8f23d8fSArnaldo Carvalho de Melo 	return 0;
1626a8f23d8fSArnaldo Carvalho de Melo }
1627a025e4f0SAdrian Hunter 
1628d1f249ecSArnaldo Carvalho de Melo int evlist__strerror_mmap(struct evlist *evlist, int err, char *buf, size_t size)
1629956fa571SArnaldo Carvalho de Melo {
1630c8b5f2c9SArnaldo Carvalho de Melo 	char sbuf[STRERR_BUFSIZE], *emsg = str_error_r(err, sbuf, sizeof(sbuf));
1631f6fa4375SJiri Olsa 	int pages_attempted = evlist->core.mmap_len / 1024, pages_max_per_user, printed = 0;
1632956fa571SArnaldo Carvalho de Melo 
1633956fa571SArnaldo Carvalho de Melo 	switch (err) {
1634956fa571SArnaldo Carvalho de Melo 	case EPERM:
1635e5d4a290SArnaldo Carvalho de Melo 		sysctl__read_int("kernel/perf_event_mlock_kb", &pages_max_per_user);
1636e965bea1SArnaldo Carvalho de Melo 		printed += scnprintf(buf + printed, size - printed,
1637e965bea1SArnaldo Carvalho de Melo 				     "Error:\t%s.\n"
1638956fa571SArnaldo Carvalho de Melo 				     "Hint:\tCheck /proc/sys/kernel/perf_event_mlock_kb (%d kB) setting.\n"
1639e965bea1SArnaldo Carvalho de Melo 				     "Hint:\tTried using %zd kB.\n",
1640e5d4a290SArnaldo Carvalho de Melo 				     emsg, pages_max_per_user, pages_attempted);
1641e965bea1SArnaldo Carvalho de Melo 
1642e965bea1SArnaldo Carvalho de Melo 		if (pages_attempted >= pages_max_per_user) {
1643e965bea1SArnaldo Carvalho de Melo 			printed += scnprintf(buf + printed, size - printed,
1644e965bea1SArnaldo Carvalho de Melo 					     "Hint:\tTry 'sudo sh -c \"echo %d > /proc/sys/kernel/perf_event_mlock_kb\"', or\n",
1645e965bea1SArnaldo Carvalho de Melo 					     pages_max_per_user + pages_attempted);
1646e965bea1SArnaldo Carvalho de Melo 		}
1647e965bea1SArnaldo Carvalho de Melo 
1648e965bea1SArnaldo Carvalho de Melo 		printed += scnprintf(buf + printed, size - printed,
1649e965bea1SArnaldo Carvalho de Melo 				     "Hint:\tTry using a smaller -m/--mmap-pages value.");
1650956fa571SArnaldo Carvalho de Melo 		break;
1651956fa571SArnaldo Carvalho de Melo 	default:
1652956fa571SArnaldo Carvalho de Melo 		scnprintf(buf, size, "%s", emsg);
1653956fa571SArnaldo Carvalho de Melo 		break;
1654956fa571SArnaldo Carvalho de Melo 	}
1655956fa571SArnaldo Carvalho de Melo 
1656956fa571SArnaldo Carvalho de Melo 	return 0;
1657956fa571SArnaldo Carvalho de Melo }
1658956fa571SArnaldo Carvalho de Melo 
1659e414fd1aSArnaldo Carvalho de Melo void evlist__to_front(struct evlist *evlist, struct evsel *move_evsel)
1660a025e4f0SAdrian Hunter {
166132dcd021SJiri Olsa 	struct evsel *evsel, *n;
1662a025e4f0SAdrian Hunter 	LIST_HEAD(move);
1663a025e4f0SAdrian Hunter 
1664515dbe48SJiri Olsa 	if (move_evsel == evlist__first(evlist))
1665a025e4f0SAdrian Hunter 		return;
1666a025e4f0SAdrian Hunter 
1667e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry_safe(evlist, n, evsel) {
1668fba7c866SJiri Olsa 		if (evsel__leader(evsel) == evsel__leader(move_evsel))
1669b27c4eceSJiri Olsa 			list_move_tail(&evsel->core.node, &move);
1670a025e4f0SAdrian Hunter 	}
1671a025e4f0SAdrian Hunter 
1672ce9036a6SJiri Olsa 	list_splice(&move, &evlist->core.entries);
1673a025e4f0SAdrian Hunter }
167460b0896cSAdrian Hunter 
1675e80db255SArnaldo Carvalho de Melo struct evsel *evlist__get_tracking_event(struct evlist *evlist)
1676442ad225SAdrian Hunter {
1677442ad225SAdrian Hunter 	struct evsel *evsel;
1678442ad225SAdrian Hunter 
1679442ad225SAdrian Hunter 	evlist__for_each_entry(evlist, evsel) {
1680442ad225SAdrian Hunter 		if (evsel->tracking)
1681442ad225SAdrian Hunter 			return evsel;
1682442ad225SAdrian Hunter 	}
1683442ad225SAdrian Hunter 
1684442ad225SAdrian Hunter 	return evlist__first(evlist);
1685442ad225SAdrian Hunter }
1686442ad225SAdrian Hunter 
1687e80db255SArnaldo Carvalho de Melo void evlist__set_tracking_event(struct evlist *evlist, struct evsel *tracking_evsel)
168860b0896cSAdrian Hunter {
168932dcd021SJiri Olsa 	struct evsel *evsel;
169060b0896cSAdrian Hunter 
169160b0896cSAdrian Hunter 	if (tracking_evsel->tracking)
169260b0896cSAdrian Hunter 		return;
169360b0896cSAdrian Hunter 
1694e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, evsel) {
169560b0896cSAdrian Hunter 		if (evsel != tracking_evsel)
169660b0896cSAdrian Hunter 			evsel->tracking = false;
169760b0896cSAdrian Hunter 	}
169860b0896cSAdrian Hunter 
169960b0896cSAdrian Hunter 	tracking_evsel->tracking = true;
170060b0896cSAdrian Hunter }
17017630b3e2SWang Nan 
1702b02736f7SArnaldo Carvalho de Melo struct evsel *evlist__find_evsel_by_str(struct evlist *evlist, const char *str)
17037630b3e2SWang Nan {
170432dcd021SJiri Olsa 	struct evsel *evsel;
17057630b3e2SWang Nan 
1706e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, evsel) {
17077630b3e2SWang Nan 		if (!evsel->name)
17087630b3e2SWang Nan 			continue;
1709ce1d3bc2SArnaldo Carvalho de Melo 		if (evsel__name_is(evsel, str))
17107630b3e2SWang Nan 			return evsel;
17117630b3e2SWang Nan 	}
17127630b3e2SWang Nan 
17137630b3e2SWang Nan 	return NULL;
17147630b3e2SWang Nan }
171554cc54deSWang Nan 
1716ade9d208SArnaldo Carvalho de Melo void evlist__toggle_bkw_mmap(struct evlist *evlist, enum bkw_mmap_state state)
171754cc54deSWang Nan {
171854cc54deSWang Nan 	enum bkw_mmap_state old_state = evlist->bkw_mmap_state;
171954cc54deSWang Nan 	enum action {
172054cc54deSWang Nan 		NONE,
172154cc54deSWang Nan 		PAUSE,
172254cc54deSWang Nan 		RESUME,
172354cc54deSWang Nan 	} action = NONE;
172454cc54deSWang Nan 
17250b72d69aSWang Nan 	if (!evlist->overwrite_mmap)
172654cc54deSWang Nan 		return;
172754cc54deSWang Nan 
172854cc54deSWang Nan 	switch (old_state) {
172954cc54deSWang Nan 	case BKW_MMAP_NOTREADY: {
173054cc54deSWang Nan 		if (state != BKW_MMAP_RUNNING)
1731dd8bd53aSLuis de Bethencourt 			goto state_err;
173254cc54deSWang Nan 		break;
173354cc54deSWang Nan 	}
173454cc54deSWang Nan 	case BKW_MMAP_RUNNING: {
173554cc54deSWang Nan 		if (state != BKW_MMAP_DATA_PENDING)
173654cc54deSWang Nan 			goto state_err;
173754cc54deSWang Nan 		action = PAUSE;
173854cc54deSWang Nan 		break;
173954cc54deSWang Nan 	}
174054cc54deSWang Nan 	case BKW_MMAP_DATA_PENDING: {
174154cc54deSWang Nan 		if (state != BKW_MMAP_EMPTY)
174254cc54deSWang Nan 			goto state_err;
174354cc54deSWang Nan 		break;
174454cc54deSWang Nan 	}
174554cc54deSWang Nan 	case BKW_MMAP_EMPTY: {
174654cc54deSWang Nan 		if (state != BKW_MMAP_RUNNING)
174754cc54deSWang Nan 			goto state_err;
174854cc54deSWang Nan 		action = RESUME;
174954cc54deSWang Nan 		break;
175054cc54deSWang Nan 	}
175154cc54deSWang Nan 	default:
175254cc54deSWang Nan 		WARN_ONCE(1, "Shouldn't get there\n");
175354cc54deSWang Nan 	}
175454cc54deSWang Nan 
175554cc54deSWang Nan 	evlist->bkw_mmap_state = state;
175654cc54deSWang Nan 
175754cc54deSWang Nan 	switch (action) {
175854cc54deSWang Nan 	case PAUSE:
17590a60b339SArnaldo Carvalho de Melo 		evlist__pause(evlist);
176054cc54deSWang Nan 		break;
176154cc54deSWang Nan 	case RESUME:
17620a60b339SArnaldo Carvalho de Melo 		evlist__resume(evlist);
176354cc54deSWang Nan 		break;
176454cc54deSWang Nan 	case NONE:
176554cc54deSWang Nan 	default:
176654cc54deSWang Nan 		break;
176754cc54deSWang Nan 	}
176854cc54deSWang Nan 
176954cc54deSWang Nan state_err:
177054cc54deSWang Nan 	return;
177154cc54deSWang Nan }
177207d6f446SArnaldo Carvalho de Melo 
177378e1bc25SArnaldo Carvalho de Melo bool evlist__exclude_kernel(struct evlist *evlist)
177407d6f446SArnaldo Carvalho de Melo {
177532dcd021SJiri Olsa 	struct evsel *evsel;
177607d6f446SArnaldo Carvalho de Melo 
177707d6f446SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, evsel) {
17781fc632ceSJiri Olsa 		if (!evsel->core.attr.exclude_kernel)
177907d6f446SArnaldo Carvalho de Melo 			return false;
178007d6f446SArnaldo Carvalho de Melo 	}
178107d6f446SArnaldo Carvalho de Melo 
178207d6f446SArnaldo Carvalho de Melo 	return true;
178307d6f446SArnaldo Carvalho de Melo }
1784e2bdbe80SJin Yao 
1785e2bdbe80SJin Yao /*
1786e2bdbe80SJin Yao  * Events in data file are not collect in groups, but we still want
1787e2bdbe80SJin Yao  * the group display. Set the artificial group and set the leader's
1788e2bdbe80SJin Yao  * forced_leader flag to notify the display code.
1789e2bdbe80SJin Yao  */
179064b4778bSArnaldo Carvalho de Melo void evlist__force_leader(struct evlist *evlist)
1791e2bdbe80SJin Yao {
17929d2dc632SIan Rogers 	if (evlist__nr_groups(evlist) == 0) {
1793515dbe48SJiri Olsa 		struct evsel *leader = evlist__first(evlist);
1794e2bdbe80SJin Yao 
1795a622eafaSArnaldo Carvalho de Melo 		evlist__set_leader(evlist);
1796e2bdbe80SJin Yao 		leader->forced_leader = true;
1797e2bdbe80SJin Yao 	}
1798e2bdbe80SJin Yao }
1799c3537fc2SAndi Kleen 
180064b4778bSArnaldo Carvalho de Melo struct evsel *evlist__reset_weak_group(struct evlist *evsel_list, struct evsel *evsel, bool close)
1801c3537fc2SAndi Kleen {
180232dcd021SJiri Olsa 	struct evsel *c2, *leader;
1803c3537fc2SAndi Kleen 	bool is_open = true;
1804c3537fc2SAndi Kleen 
1805fba7c866SJiri Olsa 	leader = evsel__leader(evsel);
1806fba7c866SJiri Olsa 
1807c3537fc2SAndi Kleen 	pr_debug("Weak group for %s/%d failed\n",
18085643b1a5SJiri Olsa 			leader->name, leader->core.nr_members);
1809c3537fc2SAndi Kleen 
1810c3537fc2SAndi Kleen 	/*
1811c3537fc2SAndi Kleen 	 * for_each_group_member doesn't work here because it doesn't
1812c3537fc2SAndi Kleen 	 * include the first entry.
1813c3537fc2SAndi Kleen 	 */
1814c3537fc2SAndi Kleen 	evlist__for_each_entry(evsel_list, c2) {
1815c3537fc2SAndi Kleen 		if (c2 == evsel)
1816c3537fc2SAndi Kleen 			is_open = false;
1817fba7c866SJiri Olsa 		if (evsel__has_leader(c2, leader)) {
18184804e011SAndi Kleen 			if (is_open && close)
18195a40e199SAndi Kleen 				perf_evsel__close(&c2->core);
1820d98079c0SIan Rogers 			/*
1821d98079c0SIan Rogers 			 * We want to close all members of the group and reopen
1822d98079c0SIan Rogers 			 * them. Some events, like Intel topdown, require being
1823d98079c0SIan Rogers 			 * in a group and so keep these in the group.
1824d98079c0SIan Rogers 			 */
1825e8f4f794SKan Liang 			evsel__remove_from_group(c2, leader);
1826d98079c0SIan Rogers 
18274804e011SAndi Kleen 			/*
18284804e011SAndi Kleen 			 * Set this for all former members of the group
18294804e011SAndi Kleen 			 * to indicate they get reopened.
18304804e011SAndi Kleen 			 */
18314804e011SAndi Kleen 			c2->reset_group = true;
1832c3537fc2SAndi Kleen 		}
1833c3537fc2SAndi Kleen 	}
1834d98079c0SIan Rogers 	/* Reset the leader count if all entries were removed. */
1835d98079c0SIan Rogers 	if (leader->core.nr_members == 1)
1836d98079c0SIan Rogers 		leader->core.nr_members = 0;
1837c3537fc2SAndi Kleen 	return leader;
1838c3537fc2SAndi Kleen }
1839ec886bf5SAlexey Budankov 
1840a8fcbd26SAdrian Hunter static int evlist__parse_control_fifo(const char *str, int *ctl_fd, int *ctl_fd_ack, bool *ctl_fd_close)
1841a8fcbd26SAdrian Hunter {
1842a8fcbd26SAdrian Hunter 	char *s, *p;
1843a8fcbd26SAdrian Hunter 	int ret = 0, fd;
1844a8fcbd26SAdrian Hunter 
1845a8fcbd26SAdrian Hunter 	if (strncmp(str, "fifo:", 5))
1846a8fcbd26SAdrian Hunter 		return -EINVAL;
1847a8fcbd26SAdrian Hunter 
1848a8fcbd26SAdrian Hunter 	str += 5;
1849a8fcbd26SAdrian Hunter 	if (!*str || *str == ',')
1850a8fcbd26SAdrian Hunter 		return -EINVAL;
1851a8fcbd26SAdrian Hunter 
1852a8fcbd26SAdrian Hunter 	s = strdup(str);
1853a8fcbd26SAdrian Hunter 	if (!s)
1854a8fcbd26SAdrian Hunter 		return -ENOMEM;
1855a8fcbd26SAdrian Hunter 
1856a8fcbd26SAdrian Hunter 	p = strchr(s, ',');
1857a8fcbd26SAdrian Hunter 	if (p)
1858a8fcbd26SAdrian Hunter 		*p = '\0';
1859a8fcbd26SAdrian Hunter 
1860a8fcbd26SAdrian Hunter 	/*
1861a8fcbd26SAdrian Hunter 	 * O_RDWR avoids POLLHUPs which is necessary to allow the other
1862a8fcbd26SAdrian Hunter 	 * end of a FIFO to be repeatedly opened and closed.
1863a8fcbd26SAdrian Hunter 	 */
1864a8fcbd26SAdrian Hunter 	fd = open(s, O_RDWR | O_NONBLOCK | O_CLOEXEC);
1865a8fcbd26SAdrian Hunter 	if (fd < 0) {
1866a8fcbd26SAdrian Hunter 		pr_err("Failed to open '%s'\n", s);
1867a8fcbd26SAdrian Hunter 		ret = -errno;
1868a8fcbd26SAdrian Hunter 		goto out_free;
1869a8fcbd26SAdrian Hunter 	}
1870a8fcbd26SAdrian Hunter 	*ctl_fd = fd;
1871a8fcbd26SAdrian Hunter 	*ctl_fd_close = true;
1872a8fcbd26SAdrian Hunter 
1873a8fcbd26SAdrian Hunter 	if (p && *++p) {
1874a8fcbd26SAdrian Hunter 		/* O_RDWR | O_NONBLOCK means the other end need not be open */
1875a8fcbd26SAdrian Hunter 		fd = open(p, O_RDWR | O_NONBLOCK | O_CLOEXEC);
1876a8fcbd26SAdrian Hunter 		if (fd < 0) {
1877a8fcbd26SAdrian Hunter 			pr_err("Failed to open '%s'\n", p);
1878a8fcbd26SAdrian Hunter 			ret = -errno;
1879a8fcbd26SAdrian Hunter 			goto out_free;
1880a8fcbd26SAdrian Hunter 		}
1881a8fcbd26SAdrian Hunter 		*ctl_fd_ack = fd;
1882a8fcbd26SAdrian Hunter 	}
1883a8fcbd26SAdrian Hunter 
1884a8fcbd26SAdrian Hunter out_free:
1885a8fcbd26SAdrian Hunter 	free(s);
1886a8fcbd26SAdrian Hunter 	return ret;
1887a8fcbd26SAdrian Hunter }
1888a8fcbd26SAdrian Hunter 
1889a8fcbd26SAdrian Hunter int evlist__parse_control(const char *str, int *ctl_fd, int *ctl_fd_ack, bool *ctl_fd_close)
18909864a66dSAdrian Hunter {
18919864a66dSAdrian Hunter 	char *comma = NULL, *endptr = NULL;
18929864a66dSAdrian Hunter 
1893a8fcbd26SAdrian Hunter 	*ctl_fd_close = false;
1894a8fcbd26SAdrian Hunter 
18959864a66dSAdrian Hunter 	if (strncmp(str, "fd:", 3))
1896a8fcbd26SAdrian Hunter 		return evlist__parse_control_fifo(str, ctl_fd, ctl_fd_ack, ctl_fd_close);
18979864a66dSAdrian Hunter 
18989864a66dSAdrian Hunter 	*ctl_fd = strtoul(&str[3], &endptr, 0);
18999864a66dSAdrian Hunter 	if (endptr == &str[3])
19009864a66dSAdrian Hunter 		return -EINVAL;
19019864a66dSAdrian Hunter 
19029864a66dSAdrian Hunter 	comma = strchr(str, ',');
19039864a66dSAdrian Hunter 	if (comma) {
19049864a66dSAdrian Hunter 		if (endptr != comma)
19059864a66dSAdrian Hunter 			return -EINVAL;
19069864a66dSAdrian Hunter 
19079864a66dSAdrian Hunter 		*ctl_fd_ack = strtoul(comma + 1, &endptr, 0);
19089864a66dSAdrian Hunter 		if (endptr == comma + 1 || *endptr != '\0')
19099864a66dSAdrian Hunter 			return -EINVAL;
19109864a66dSAdrian Hunter 	}
19119864a66dSAdrian Hunter 
19129864a66dSAdrian Hunter 	return 0;
19139864a66dSAdrian Hunter }
19149864a66dSAdrian Hunter 
1915ee7fe31eSAdrian Hunter void evlist__close_control(int ctl_fd, int ctl_fd_ack, bool *ctl_fd_close)
1916ee7fe31eSAdrian Hunter {
1917ee7fe31eSAdrian Hunter 	if (*ctl_fd_close) {
1918ee7fe31eSAdrian Hunter 		*ctl_fd_close = false;
1919ee7fe31eSAdrian Hunter 		close(ctl_fd);
1920ee7fe31eSAdrian Hunter 		if (ctl_fd_ack >= 0)
1921ee7fe31eSAdrian Hunter 			close(ctl_fd_ack);
1922ee7fe31eSAdrian Hunter 	}
1923ee7fe31eSAdrian Hunter }
1924ee7fe31eSAdrian Hunter 
1925ec886bf5SAlexey Budankov int evlist__initialize_ctlfd(struct evlist *evlist, int fd, int ack)
1926ec886bf5SAlexey Budankov {
1927ec886bf5SAlexey Budankov 	if (fd == -1) {
1928ec886bf5SAlexey Budankov 		pr_debug("Control descriptor is not initialized\n");
1929ec886bf5SAlexey Budankov 		return 0;
1930ec886bf5SAlexey Budankov 	}
1931ec886bf5SAlexey Budankov 
1932ec886bf5SAlexey Budankov 	evlist->ctl_fd.pos = perf_evlist__add_pollfd(&evlist->core, fd, NULL, POLLIN,
1933feff0b61SAdrian Hunter 						     fdarray_flag__nonfilterable |
1934feff0b61SAdrian Hunter 						     fdarray_flag__non_perf_event);
1935ec886bf5SAlexey Budankov 	if (evlist->ctl_fd.pos < 0) {
1936ec886bf5SAlexey Budankov 		evlist->ctl_fd.pos = -1;
1937ec886bf5SAlexey Budankov 		pr_err("Failed to add ctl fd entry: %m\n");
1938ec886bf5SAlexey Budankov 		return -1;
1939ec886bf5SAlexey Budankov 	}
1940ec886bf5SAlexey Budankov 
1941ec886bf5SAlexey Budankov 	evlist->ctl_fd.fd = fd;
1942ec886bf5SAlexey Budankov 	evlist->ctl_fd.ack = ack;
1943ec886bf5SAlexey Budankov 
1944ec886bf5SAlexey Budankov 	return 0;
1945ec886bf5SAlexey Budankov }
1946ec886bf5SAlexey Budankov 
1947ec886bf5SAlexey Budankov bool evlist__ctlfd_initialized(struct evlist *evlist)
1948ec886bf5SAlexey Budankov {
1949ec886bf5SAlexey Budankov 	return evlist->ctl_fd.pos >= 0;
1950ec886bf5SAlexey Budankov }
1951ec886bf5SAlexey Budankov 
1952ec886bf5SAlexey Budankov int evlist__finalize_ctlfd(struct evlist *evlist)
1953ec886bf5SAlexey Budankov {
1954ec886bf5SAlexey Budankov 	struct pollfd *entries = evlist->core.pollfd.entries;
1955ec886bf5SAlexey Budankov 
1956ec886bf5SAlexey Budankov 	if (!evlist__ctlfd_initialized(evlist))
1957ec886bf5SAlexey Budankov 		return 0;
1958ec886bf5SAlexey Budankov 
1959ec886bf5SAlexey Budankov 	entries[evlist->ctl_fd.pos].fd = -1;
1960ec886bf5SAlexey Budankov 	entries[evlist->ctl_fd.pos].events = 0;
1961ec886bf5SAlexey Budankov 	entries[evlist->ctl_fd.pos].revents = 0;
1962ec886bf5SAlexey Budankov 
1963ec886bf5SAlexey Budankov 	evlist->ctl_fd.pos = -1;
1964ec886bf5SAlexey Budankov 	evlist->ctl_fd.ack = -1;
1965ec886bf5SAlexey Budankov 	evlist->ctl_fd.fd = -1;
1966ec886bf5SAlexey Budankov 
1967ec886bf5SAlexey Budankov 	return 0;
1968ec886bf5SAlexey Budankov }
1969ec886bf5SAlexey Budankov 
1970ec886bf5SAlexey Budankov static int evlist__ctlfd_recv(struct evlist *evlist, enum evlist_ctl_cmd *cmd,
1971ec886bf5SAlexey Budankov 			      char *cmd_data, size_t data_size)
1972ec886bf5SAlexey Budankov {
1973ec886bf5SAlexey Budankov 	int err;
1974ec886bf5SAlexey Budankov 	char c;
1975ec886bf5SAlexey Budankov 	size_t bytes_read = 0;
1976ec886bf5SAlexey Budankov 
197740db8ff5SAdrian Hunter 	*cmd = EVLIST_CTL_CMD_UNSUPPORTED;
1978ec886bf5SAlexey Budankov 	memset(cmd_data, 0, data_size);
1979ec886bf5SAlexey Budankov 	data_size--;
1980ec886bf5SAlexey Budankov 
1981ec886bf5SAlexey Budankov 	do {
1982ec886bf5SAlexey Budankov 		err = read(evlist->ctl_fd.fd, &c, 1);
1983ec886bf5SAlexey Budankov 		if (err > 0) {
1984ec886bf5SAlexey Budankov 			if (c == '\n' || c == '\0')
1985ec886bf5SAlexey Budankov 				break;
1986ec886bf5SAlexey Budankov 			cmd_data[bytes_read++] = c;
1987ec886bf5SAlexey Budankov 			if (bytes_read == data_size)
1988ec886bf5SAlexey Budankov 				break;
198940db8ff5SAdrian Hunter 			continue;
199040db8ff5SAdrian Hunter 		} else if (err == -1) {
199140db8ff5SAdrian Hunter 			if (errno == EINTR)
199240db8ff5SAdrian Hunter 				continue;
199340db8ff5SAdrian Hunter 			if (errno == EAGAIN || errno == EWOULDBLOCK)
199440db8ff5SAdrian Hunter 				err = 0;
199540db8ff5SAdrian Hunter 			else
1996ec886bf5SAlexey Budankov 				pr_err("Failed to read from ctlfd %d: %m\n", evlist->ctl_fd.fd);
1997ec886bf5SAlexey Budankov 		}
199840db8ff5SAdrian Hunter 		break;
1999ec886bf5SAlexey Budankov 	} while (1);
2000ec886bf5SAlexey Budankov 
2001ec886bf5SAlexey Budankov 	pr_debug("Message from ctl_fd: \"%s%s\"\n", cmd_data,
2002ec886bf5SAlexey Budankov 		 bytes_read == data_size ? "" : c == '\n' ? "\\n" : "\\0");
2003ec886bf5SAlexey Budankov 
200440db8ff5SAdrian Hunter 	if (bytes_read > 0) {
2005ec886bf5SAlexey Budankov 		if (!strncmp(cmd_data, EVLIST_CTL_CMD_ENABLE_TAG,
2006ec886bf5SAlexey Budankov 			     (sizeof(EVLIST_CTL_CMD_ENABLE_TAG)-1))) {
2007ec886bf5SAlexey Budankov 			*cmd = EVLIST_CTL_CMD_ENABLE;
2008ec886bf5SAlexey Budankov 		} else if (!strncmp(cmd_data, EVLIST_CTL_CMD_DISABLE_TAG,
2009ec886bf5SAlexey Budankov 				    (sizeof(EVLIST_CTL_CMD_DISABLE_TAG)-1))) {
2010ec886bf5SAlexey Budankov 			*cmd = EVLIST_CTL_CMD_DISABLE;
2011d20aff15SAdrian Hunter 		} else if (!strncmp(cmd_data, EVLIST_CTL_CMD_SNAPSHOT_TAG,
2012d20aff15SAdrian Hunter 				    (sizeof(EVLIST_CTL_CMD_SNAPSHOT_TAG)-1))) {
2013d20aff15SAdrian Hunter 			*cmd = EVLIST_CTL_CMD_SNAPSHOT;
2014d20aff15SAdrian Hunter 			pr_debug("is snapshot\n");
2015142544a9SJiri Olsa 		} else if (!strncmp(cmd_data, EVLIST_CTL_CMD_EVLIST_TAG,
2016142544a9SJiri Olsa 				    (sizeof(EVLIST_CTL_CMD_EVLIST_TAG)-1))) {
2017142544a9SJiri Olsa 			*cmd = EVLIST_CTL_CMD_EVLIST;
2018f186cd61SJiri Olsa 		} else if (!strncmp(cmd_data, EVLIST_CTL_CMD_STOP_TAG,
2019f186cd61SJiri Olsa 				    (sizeof(EVLIST_CTL_CMD_STOP_TAG)-1))) {
2020f186cd61SJiri Olsa 			*cmd = EVLIST_CTL_CMD_STOP;
202147fddcb4SJiri Olsa 		} else if (!strncmp(cmd_data, EVLIST_CTL_CMD_PING_TAG,
202247fddcb4SJiri Olsa 				    (sizeof(EVLIST_CTL_CMD_PING_TAG)-1))) {
202347fddcb4SJiri Olsa 			*cmd = EVLIST_CTL_CMD_PING;
2024ec886bf5SAlexey Budankov 		}
2025ec886bf5SAlexey Budankov 	}
2026ec886bf5SAlexey Budankov 
202740db8ff5SAdrian Hunter 	return bytes_read ? (int)bytes_read : err;
2028ec886bf5SAlexey Budankov }
2029ec886bf5SAlexey Budankov 
2030d20aff15SAdrian Hunter int evlist__ctlfd_ack(struct evlist *evlist)
2031ec886bf5SAlexey Budankov {
2032ec886bf5SAlexey Budankov 	int err;
2033ec886bf5SAlexey Budankov 
2034ec886bf5SAlexey Budankov 	if (evlist->ctl_fd.ack == -1)
2035ec886bf5SAlexey Budankov 		return 0;
2036ec886bf5SAlexey Budankov 
2037ec886bf5SAlexey Budankov 	err = write(evlist->ctl_fd.ack, EVLIST_CTL_CMD_ACK_TAG,
2038ec886bf5SAlexey Budankov 		    sizeof(EVLIST_CTL_CMD_ACK_TAG));
2039ec886bf5SAlexey Budankov 	if (err == -1)
2040ec886bf5SAlexey Budankov 		pr_err("failed to write to ctl_ack_fd %d: %m\n", evlist->ctl_fd.ack);
2041ec886bf5SAlexey Budankov 
2042ec886bf5SAlexey Budankov 	return err;
2043ec886bf5SAlexey Budankov }
2044ec886bf5SAlexey Budankov 
2045991ae4ebSJiri Olsa static int get_cmd_arg(char *cmd_data, size_t cmd_size, char **arg)
2046991ae4ebSJiri Olsa {
2047991ae4ebSJiri Olsa 	char *data = cmd_data + cmd_size;
2048991ae4ebSJiri Olsa 
2049991ae4ebSJiri Olsa 	/* no argument */
2050991ae4ebSJiri Olsa 	if (!*data)
2051991ae4ebSJiri Olsa 		return 0;
2052991ae4ebSJiri Olsa 
2053991ae4ebSJiri Olsa 	/* there's argument */
2054991ae4ebSJiri Olsa 	if (*data == ' ') {
2055991ae4ebSJiri Olsa 		*arg = data + 1;
2056991ae4ebSJiri Olsa 		return 1;
2057991ae4ebSJiri Olsa 	}
2058991ae4ebSJiri Olsa 
2059991ae4ebSJiri Olsa 	/* malformed */
2060991ae4ebSJiri Olsa 	return -1;
2061991ae4ebSJiri Olsa }
2062991ae4ebSJiri Olsa 
2063991ae4ebSJiri Olsa static int evlist__ctlfd_enable(struct evlist *evlist, char *cmd_data, bool enable)
2064991ae4ebSJiri Olsa {
2065991ae4ebSJiri Olsa 	struct evsel *evsel;
2066991ae4ebSJiri Olsa 	char *name;
2067991ae4ebSJiri Olsa 	int err;
2068991ae4ebSJiri Olsa 
2069991ae4ebSJiri Olsa 	err = get_cmd_arg(cmd_data,
2070991ae4ebSJiri Olsa 			  enable ? sizeof(EVLIST_CTL_CMD_ENABLE_TAG) - 1 :
2071991ae4ebSJiri Olsa 				   sizeof(EVLIST_CTL_CMD_DISABLE_TAG) - 1,
2072991ae4ebSJiri Olsa 			  &name);
2073991ae4ebSJiri Olsa 	if (err < 0) {
2074991ae4ebSJiri Olsa 		pr_info("failed: wrong command\n");
2075991ae4ebSJiri Olsa 		return -1;
2076991ae4ebSJiri Olsa 	}
2077991ae4ebSJiri Olsa 
2078991ae4ebSJiri Olsa 	if (err) {
2079991ae4ebSJiri Olsa 		evsel = evlist__find_evsel_by_str(evlist, name);
2080991ae4ebSJiri Olsa 		if (evsel) {
2081991ae4ebSJiri Olsa 			if (enable)
2082991ae4ebSJiri Olsa 				evlist__enable_evsel(evlist, name);
2083991ae4ebSJiri Olsa 			else
2084991ae4ebSJiri Olsa 				evlist__disable_evsel(evlist, name);
2085991ae4ebSJiri Olsa 			pr_info("Event %s %s\n", evsel->name,
2086991ae4ebSJiri Olsa 				enable ? "enabled" : "disabled");
2087991ae4ebSJiri Olsa 		} else {
2088991ae4ebSJiri Olsa 			pr_info("failed: can't find '%s' event\n", name);
2089991ae4ebSJiri Olsa 		}
2090991ae4ebSJiri Olsa 	} else {
2091991ae4ebSJiri Olsa 		if (enable) {
2092991ae4ebSJiri Olsa 			evlist__enable(evlist);
2093991ae4ebSJiri Olsa 			pr_info(EVLIST_ENABLED_MSG);
2094991ae4ebSJiri Olsa 		} else {
2095991ae4ebSJiri Olsa 			evlist__disable(evlist);
2096991ae4ebSJiri Olsa 			pr_info(EVLIST_DISABLED_MSG);
2097991ae4ebSJiri Olsa 		}
2098991ae4ebSJiri Olsa 	}
2099991ae4ebSJiri Olsa 
2100991ae4ebSJiri Olsa 	return 0;
2101991ae4ebSJiri Olsa }
2102991ae4ebSJiri Olsa 
2103142544a9SJiri Olsa static int evlist__ctlfd_list(struct evlist *evlist, char *cmd_data)
2104142544a9SJiri Olsa {
2105142544a9SJiri Olsa 	struct perf_attr_details details = { .verbose = false, };
2106142544a9SJiri Olsa 	struct evsel *evsel;
2107142544a9SJiri Olsa 	char *arg;
2108142544a9SJiri Olsa 	int err;
2109142544a9SJiri Olsa 
2110142544a9SJiri Olsa 	err = get_cmd_arg(cmd_data,
2111142544a9SJiri Olsa 			  sizeof(EVLIST_CTL_CMD_EVLIST_TAG) - 1,
2112142544a9SJiri Olsa 			  &arg);
2113142544a9SJiri Olsa 	if (err < 0) {
2114142544a9SJiri Olsa 		pr_info("failed: wrong command\n");
2115142544a9SJiri Olsa 		return -1;
2116142544a9SJiri Olsa 	}
2117142544a9SJiri Olsa 
2118142544a9SJiri Olsa 	if (err) {
2119142544a9SJiri Olsa 		if (!strcmp(arg, "-v")) {
2120142544a9SJiri Olsa 			details.verbose = true;
2121142544a9SJiri Olsa 		} else if (!strcmp(arg, "-g")) {
2122142544a9SJiri Olsa 			details.event_group = true;
2123142544a9SJiri Olsa 		} else if (!strcmp(arg, "-F")) {
2124142544a9SJiri Olsa 			details.freq = true;
2125142544a9SJiri Olsa 		} else {
2126142544a9SJiri Olsa 			pr_info("failed: wrong command\n");
2127142544a9SJiri Olsa 			return -1;
2128142544a9SJiri Olsa 		}
2129142544a9SJiri Olsa 	}
2130142544a9SJiri Olsa 
2131142544a9SJiri Olsa 	evlist__for_each_entry(evlist, evsel)
2132142544a9SJiri Olsa 		evsel__fprintf(evsel, &details, stderr);
2133142544a9SJiri Olsa 
2134142544a9SJiri Olsa 	return 0;
2135142544a9SJiri Olsa }
2136142544a9SJiri Olsa 
2137ec886bf5SAlexey Budankov int evlist__ctlfd_process(struct evlist *evlist, enum evlist_ctl_cmd *cmd)
2138ec886bf5SAlexey Budankov {
2139ec886bf5SAlexey Budankov 	int err = 0;
2140ec886bf5SAlexey Budankov 	char cmd_data[EVLIST_CTL_CMD_MAX_LEN];
2141ec886bf5SAlexey Budankov 	int ctlfd_pos = evlist->ctl_fd.pos;
2142ec886bf5SAlexey Budankov 	struct pollfd *entries = evlist->core.pollfd.entries;
2143ec886bf5SAlexey Budankov 
2144ec886bf5SAlexey Budankov 	if (!evlist__ctlfd_initialized(evlist) || !entries[ctlfd_pos].revents)
2145ec886bf5SAlexey Budankov 		return 0;
2146ec886bf5SAlexey Budankov 
2147ec886bf5SAlexey Budankov 	if (entries[ctlfd_pos].revents & POLLIN) {
2148ec886bf5SAlexey Budankov 		err = evlist__ctlfd_recv(evlist, cmd, cmd_data,
2149ec886bf5SAlexey Budankov 					 EVLIST_CTL_CMD_MAX_LEN);
2150ec886bf5SAlexey Budankov 		if (err > 0) {
2151ec886bf5SAlexey Budankov 			switch (*cmd) {
2152ec886bf5SAlexey Budankov 			case EVLIST_CTL_CMD_ENABLE:
2153ec886bf5SAlexey Budankov 			case EVLIST_CTL_CMD_DISABLE:
2154991ae4ebSJiri Olsa 				err = evlist__ctlfd_enable(evlist, cmd_data,
2155991ae4ebSJiri Olsa 							   *cmd == EVLIST_CTL_CMD_ENABLE);
2156ec886bf5SAlexey Budankov 				break;
2157142544a9SJiri Olsa 			case EVLIST_CTL_CMD_EVLIST:
2158142544a9SJiri Olsa 				err = evlist__ctlfd_list(evlist, cmd_data);
2159142544a9SJiri Olsa 				break;
2160d20aff15SAdrian Hunter 			case EVLIST_CTL_CMD_SNAPSHOT:
2161f186cd61SJiri Olsa 			case EVLIST_CTL_CMD_STOP:
216247fddcb4SJiri Olsa 			case EVLIST_CTL_CMD_PING:
2163d20aff15SAdrian Hunter 				break;
2164ec886bf5SAlexey Budankov 			case EVLIST_CTL_CMD_ACK:
2165ec886bf5SAlexey Budankov 			case EVLIST_CTL_CMD_UNSUPPORTED:
2166ec886bf5SAlexey Budankov 			default:
2167ec886bf5SAlexey Budankov 				pr_debug("ctlfd: unsupported %d\n", *cmd);
2168ec886bf5SAlexey Budankov 				break;
2169ec886bf5SAlexey Budankov 			}
2170d20aff15SAdrian Hunter 			if (!(*cmd == EVLIST_CTL_CMD_ACK || *cmd == EVLIST_CTL_CMD_UNSUPPORTED ||
2171d20aff15SAdrian Hunter 			      *cmd == EVLIST_CTL_CMD_SNAPSHOT))
2172ec886bf5SAlexey Budankov 				evlist__ctlfd_ack(evlist);
2173ec886bf5SAlexey Budankov 		}
2174ec886bf5SAlexey Budankov 	}
2175ec886bf5SAlexey Budankov 
2176ec886bf5SAlexey Budankov 	if (entries[ctlfd_pos].revents & (POLLHUP | POLLERR))
2177ec886bf5SAlexey Budankov 		evlist__finalize_ctlfd(evlist);
2178ec886bf5SAlexey Budankov 	else
2179ec886bf5SAlexey Budankov 		entries[ctlfd_pos].revents = 0;
2180ec886bf5SAlexey Budankov 
2181ec886bf5SAlexey Budankov 	return err;
2182ec886bf5SAlexey Budankov }
2183b214ba8cSNamhyung Kim 
21846657a099SAdrian Hunter /**
21856657a099SAdrian Hunter  * struct event_enable_time - perf record -D/--delay single time range.
21866657a099SAdrian Hunter  * @start: start of time range to enable events in milliseconds
21876657a099SAdrian Hunter  * @end: end of time range to enable events in milliseconds
21886657a099SAdrian Hunter  *
21896657a099SAdrian Hunter  * N.B. this structure is also accessed as an array of int.
21906657a099SAdrian Hunter  */
21916657a099SAdrian Hunter struct event_enable_time {
21926657a099SAdrian Hunter 	int	start;
21936657a099SAdrian Hunter 	int	end;
21946657a099SAdrian Hunter };
21956657a099SAdrian Hunter 
21966657a099SAdrian Hunter static int parse_event_enable_time(const char *str, struct event_enable_time *range, bool first)
21976657a099SAdrian Hunter {
21986657a099SAdrian Hunter 	const char *fmt = first ? "%u - %u %n" : " , %u - %u %n";
21996657a099SAdrian Hunter 	int ret, start, end, n;
22006657a099SAdrian Hunter 
22016657a099SAdrian Hunter 	ret = sscanf(str, fmt, &start, &end, &n);
22026657a099SAdrian Hunter 	if (ret != 2 || end <= start)
22036657a099SAdrian Hunter 		return -EINVAL;
22046657a099SAdrian Hunter 	if (range) {
22056657a099SAdrian Hunter 		range->start = start;
22066657a099SAdrian Hunter 		range->end = end;
22076657a099SAdrian Hunter 	}
22086657a099SAdrian Hunter 	return n;
22096657a099SAdrian Hunter }
22106657a099SAdrian Hunter 
22116657a099SAdrian Hunter static ssize_t parse_event_enable_times(const char *str, struct event_enable_time *range)
22126657a099SAdrian Hunter {
22136657a099SAdrian Hunter 	int incr = !!range;
22146657a099SAdrian Hunter 	bool first = true;
22156657a099SAdrian Hunter 	ssize_t ret, cnt;
22166657a099SAdrian Hunter 
22176657a099SAdrian Hunter 	for (cnt = 0; *str; cnt++) {
22186657a099SAdrian Hunter 		ret = parse_event_enable_time(str, range, first);
22196657a099SAdrian Hunter 		if (ret < 0)
22206657a099SAdrian Hunter 			return ret;
22216657a099SAdrian Hunter 		/* Check no overlap */
22226657a099SAdrian Hunter 		if (!first && range && range->start <= range[-1].end)
22236657a099SAdrian Hunter 			return -EINVAL;
22246657a099SAdrian Hunter 		str += ret;
22256657a099SAdrian Hunter 		range += incr;
22266657a099SAdrian Hunter 		first = false;
22276657a099SAdrian Hunter 	}
22286657a099SAdrian Hunter 	return cnt;
22296657a099SAdrian Hunter }
22306657a099SAdrian Hunter 
22316657a099SAdrian Hunter /**
22326657a099SAdrian Hunter  * struct event_enable_timer - control structure for perf record -D/--delay.
22336657a099SAdrian Hunter  * @evlist: event list
22346657a099SAdrian Hunter  * @times: time ranges that events are enabled (N.B. this is also accessed as an
22356657a099SAdrian Hunter  *         array of int)
22366657a099SAdrian Hunter  * @times_cnt: number of time ranges
22376657a099SAdrian Hunter  * @timerfd: timer file descriptor
22386657a099SAdrian Hunter  * @pollfd_pos: position in @evlist array of file descriptors to poll (fdarray)
22396657a099SAdrian Hunter  * @times_step: current position in (int *)@times)[],
22406657a099SAdrian Hunter  *              refer event_enable_timer__process()
22416657a099SAdrian Hunter  *
22426657a099SAdrian Hunter  * Note, this structure is only used when there are time ranges, not when there
22436657a099SAdrian Hunter  * is only an initial delay.
22446657a099SAdrian Hunter  */
22456657a099SAdrian Hunter struct event_enable_timer {
22466657a099SAdrian Hunter 	struct evlist *evlist;
22476657a099SAdrian Hunter 	struct event_enable_time *times;
22486657a099SAdrian Hunter 	size_t	times_cnt;
22496657a099SAdrian Hunter 	int	timerfd;
22506657a099SAdrian Hunter 	int	pollfd_pos;
22516657a099SAdrian Hunter 	size_t	times_step;
22526657a099SAdrian Hunter };
22536657a099SAdrian Hunter 
22546657a099SAdrian Hunter static int str_to_delay(const char *str)
22556657a099SAdrian Hunter {
22566657a099SAdrian Hunter 	char *endptr;
22576657a099SAdrian Hunter 	long d;
22586657a099SAdrian Hunter 
22596657a099SAdrian Hunter 	d = strtol(str, &endptr, 10);
22606657a099SAdrian Hunter 	if (*endptr || d > INT_MAX || d < -1)
22616657a099SAdrian Hunter 		return 0;
22626657a099SAdrian Hunter 	return d;
22636657a099SAdrian Hunter }
22646657a099SAdrian Hunter 
22656657a099SAdrian Hunter int evlist__parse_event_enable_time(struct evlist *evlist, struct record_opts *opts,
22666657a099SAdrian Hunter 				    const char *str, int unset)
22676657a099SAdrian Hunter {
22686657a099SAdrian Hunter 	enum fdarray_flags flags = fdarray_flag__nonfilterable | fdarray_flag__non_perf_event;
22696657a099SAdrian Hunter 	struct event_enable_timer *eet;
22706657a099SAdrian Hunter 	ssize_t times_cnt;
22716657a099SAdrian Hunter 	ssize_t ret;
22726657a099SAdrian Hunter 	int err;
22736657a099SAdrian Hunter 
22746657a099SAdrian Hunter 	if (unset)
22756657a099SAdrian Hunter 		return 0;
22766657a099SAdrian Hunter 
2277cb4b9e68SChangbin Du 	opts->target.initial_delay = str_to_delay(str);
2278cb4b9e68SChangbin Du 	if (opts->target.initial_delay)
22796657a099SAdrian Hunter 		return 0;
22806657a099SAdrian Hunter 
22816657a099SAdrian Hunter 	ret = parse_event_enable_times(str, NULL);
22826657a099SAdrian Hunter 	if (ret < 0)
22836657a099SAdrian Hunter 		return ret;
22846657a099SAdrian Hunter 
22856657a099SAdrian Hunter 	times_cnt = ret;
22866657a099SAdrian Hunter 	if (times_cnt == 0)
22876657a099SAdrian Hunter 		return -EINVAL;
22886657a099SAdrian Hunter 
22896657a099SAdrian Hunter 	eet = zalloc(sizeof(*eet));
22906657a099SAdrian Hunter 	if (!eet)
22916657a099SAdrian Hunter 		return -ENOMEM;
22926657a099SAdrian Hunter 
22936657a099SAdrian Hunter 	eet->times = calloc(times_cnt, sizeof(*eet->times));
22946657a099SAdrian Hunter 	if (!eet->times) {
22956657a099SAdrian Hunter 		err = -ENOMEM;
22966657a099SAdrian Hunter 		goto free_eet;
22976657a099SAdrian Hunter 	}
22986657a099SAdrian Hunter 
22996657a099SAdrian Hunter 	if (parse_event_enable_times(str, eet->times) != times_cnt) {
23006657a099SAdrian Hunter 		err = -EINVAL;
23016657a099SAdrian Hunter 		goto free_eet_times;
23026657a099SAdrian Hunter 	}
23036657a099SAdrian Hunter 
23046657a099SAdrian Hunter 	eet->times_cnt = times_cnt;
23056657a099SAdrian Hunter 
23066657a099SAdrian Hunter 	eet->timerfd = timerfd_create(CLOCK_MONOTONIC, TFD_CLOEXEC);
23076657a099SAdrian Hunter 	if (eet->timerfd == -1) {
23086657a099SAdrian Hunter 		err = -errno;
23096657a099SAdrian Hunter 		pr_err("timerfd_create failed: %s\n", strerror(errno));
23106657a099SAdrian Hunter 		goto free_eet_times;
23116657a099SAdrian Hunter 	}
23126657a099SAdrian Hunter 
23136657a099SAdrian Hunter 	eet->pollfd_pos = perf_evlist__add_pollfd(&evlist->core, eet->timerfd, NULL, POLLIN, flags);
23146657a099SAdrian Hunter 	if (eet->pollfd_pos < 0) {
23156657a099SAdrian Hunter 		err = eet->pollfd_pos;
23166657a099SAdrian Hunter 		goto close_timerfd;
23176657a099SAdrian Hunter 	}
23186657a099SAdrian Hunter 
23196657a099SAdrian Hunter 	eet->evlist = evlist;
23206657a099SAdrian Hunter 	evlist->eet = eet;
2321cb4b9e68SChangbin Du 	opts->target.initial_delay = eet->times[0].start;
23226657a099SAdrian Hunter 
23236657a099SAdrian Hunter 	return 0;
23246657a099SAdrian Hunter 
23256657a099SAdrian Hunter close_timerfd:
23266657a099SAdrian Hunter 	close(eet->timerfd);
23276657a099SAdrian Hunter free_eet_times:
23282bfc8134SArnaldo Carvalho de Melo 	zfree(&eet->times);
23296657a099SAdrian Hunter free_eet:
23306657a099SAdrian Hunter 	free(eet);
23316657a099SAdrian Hunter 	return err;
23326657a099SAdrian Hunter }
23336657a099SAdrian Hunter 
23346657a099SAdrian Hunter static int event_enable_timer__set_timer(struct event_enable_timer *eet, int ms)
23356657a099SAdrian Hunter {
23366657a099SAdrian Hunter 	struct itimerspec its = {
23376657a099SAdrian Hunter 		.it_value.tv_sec = ms / MSEC_PER_SEC,
23386657a099SAdrian Hunter 		.it_value.tv_nsec = (ms % MSEC_PER_SEC) * NSEC_PER_MSEC,
23396657a099SAdrian Hunter 	};
23406657a099SAdrian Hunter 	int err = 0;
23416657a099SAdrian Hunter 
23426657a099SAdrian Hunter 	if (timerfd_settime(eet->timerfd, 0, &its, NULL) < 0) {
23436657a099SAdrian Hunter 		err = -errno;
23446657a099SAdrian Hunter 		pr_err("timerfd_settime failed: %s\n", strerror(errno));
23456657a099SAdrian Hunter 	}
23466657a099SAdrian Hunter 	return err;
23476657a099SAdrian Hunter }
23486657a099SAdrian Hunter 
23496657a099SAdrian Hunter int event_enable_timer__start(struct event_enable_timer *eet)
23506657a099SAdrian Hunter {
23516657a099SAdrian Hunter 	int ms;
23526657a099SAdrian Hunter 
23536657a099SAdrian Hunter 	if (!eet)
23546657a099SAdrian Hunter 		return 0;
23556657a099SAdrian Hunter 
23566657a099SAdrian Hunter 	ms = eet->times[0].end - eet->times[0].start;
23576657a099SAdrian Hunter 	eet->times_step = 1;
23586657a099SAdrian Hunter 
23596657a099SAdrian Hunter 	return event_enable_timer__set_timer(eet, ms);
23606657a099SAdrian Hunter }
23616657a099SAdrian Hunter 
23626657a099SAdrian Hunter int event_enable_timer__process(struct event_enable_timer *eet)
23636657a099SAdrian Hunter {
23646657a099SAdrian Hunter 	struct pollfd *entries;
23656657a099SAdrian Hunter 	short revents;
23666657a099SAdrian Hunter 
23676657a099SAdrian Hunter 	if (!eet)
23686657a099SAdrian Hunter 		return 0;
23696657a099SAdrian Hunter 
23706657a099SAdrian Hunter 	entries = eet->evlist->core.pollfd.entries;
23716657a099SAdrian Hunter 	revents = entries[eet->pollfd_pos].revents;
23726657a099SAdrian Hunter 	entries[eet->pollfd_pos].revents = 0;
23736657a099SAdrian Hunter 
23746657a099SAdrian Hunter 	if (revents & POLLIN) {
23756657a099SAdrian Hunter 		size_t step = eet->times_step;
23766657a099SAdrian Hunter 		size_t pos = step / 2;
23776657a099SAdrian Hunter 
23786657a099SAdrian Hunter 		if (step & 1) {
23796657a099SAdrian Hunter 			evlist__disable_non_dummy(eet->evlist);
23806657a099SAdrian Hunter 			pr_info(EVLIST_DISABLED_MSG);
23816657a099SAdrian Hunter 			if (pos >= eet->times_cnt - 1) {
23826657a099SAdrian Hunter 				/* Disarm timer */
23836657a099SAdrian Hunter 				event_enable_timer__set_timer(eet, 0);
23846657a099SAdrian Hunter 				return 1; /* Stop */
23856657a099SAdrian Hunter 			}
23866657a099SAdrian Hunter 		} else {
23876657a099SAdrian Hunter 			evlist__enable_non_dummy(eet->evlist);
23886657a099SAdrian Hunter 			pr_info(EVLIST_ENABLED_MSG);
23896657a099SAdrian Hunter 		}
23906657a099SAdrian Hunter 
23916657a099SAdrian Hunter 		step += 1;
23926657a099SAdrian Hunter 		pos = step / 2;
23936657a099SAdrian Hunter 
23946657a099SAdrian Hunter 		if (pos < eet->times_cnt) {
23956657a099SAdrian Hunter 			int *times = (int *)eet->times; /* Accessing 'times' as array of int */
23966657a099SAdrian Hunter 			int ms = times[step] - times[step - 1];
23976657a099SAdrian Hunter 
23986657a099SAdrian Hunter 			eet->times_step = step;
23996657a099SAdrian Hunter 			return event_enable_timer__set_timer(eet, ms);
24006657a099SAdrian Hunter 		}
24016657a099SAdrian Hunter 	}
24026657a099SAdrian Hunter 
24036657a099SAdrian Hunter 	return 0;
24046657a099SAdrian Hunter }
24056657a099SAdrian Hunter 
24066657a099SAdrian Hunter void event_enable_timer__exit(struct event_enable_timer **ep)
24076657a099SAdrian Hunter {
24086657a099SAdrian Hunter 	if (!ep || !*ep)
24096657a099SAdrian Hunter 		return;
24102bfc8134SArnaldo Carvalho de Melo 	zfree(&(*ep)->times);
24116657a099SAdrian Hunter 	zfree(ep);
24126657a099SAdrian Hunter }
24136657a099SAdrian Hunter 
2414b214ba8cSNamhyung Kim struct evsel *evlist__find_evsel(struct evlist *evlist, int idx)
2415b214ba8cSNamhyung Kim {
2416b214ba8cSNamhyung Kim 	struct evsel *evsel;
2417b214ba8cSNamhyung Kim 
2418b214ba8cSNamhyung Kim 	evlist__for_each_entry(evlist, evsel) {
241938fe0e01SJiri Olsa 		if (evsel->core.idx == idx)
2420b214ba8cSNamhyung Kim 			return evsel;
2421b214ba8cSNamhyung Kim 	}
2422b214ba8cSNamhyung Kim 	return NULL;
2423b214ba8cSNamhyung Kim }
24249865ea8aSArnaldo Carvalho de Melo 
24259865ea8aSArnaldo Carvalho de Melo int evlist__scnprintf_evsels(struct evlist *evlist, size_t size, char *bf)
24269865ea8aSArnaldo Carvalho de Melo {
24279865ea8aSArnaldo Carvalho de Melo 	struct evsel *evsel;
24289865ea8aSArnaldo Carvalho de Melo 	int printed = 0;
24299865ea8aSArnaldo Carvalho de Melo 
24309865ea8aSArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, evsel) {
24319865ea8aSArnaldo Carvalho de Melo 		if (evsel__is_dummy_event(evsel))
24329865ea8aSArnaldo Carvalho de Melo 			continue;
24339865ea8aSArnaldo Carvalho de Melo 		if (size > (strlen(evsel__name(evsel)) + (printed ? 2 : 1))) {
24349865ea8aSArnaldo Carvalho de Melo 			printed += scnprintf(bf + printed, size - printed, "%s%s", printed ? "," : "", evsel__name(evsel));
24359865ea8aSArnaldo Carvalho de Melo 		} else {
24369865ea8aSArnaldo Carvalho de Melo 			printed += scnprintf(bf + printed, size - printed, "%s...", printed ? "," : "");
24379865ea8aSArnaldo Carvalho de Melo 			break;
24389865ea8aSArnaldo Carvalho de Melo 		}
24399865ea8aSArnaldo Carvalho de Melo 	}
24409865ea8aSArnaldo Carvalho de Melo 
24419865ea8aSArnaldo Carvalho de Melo 	return printed;
24429865ea8aSArnaldo Carvalho de Melo }
2443d5a8bd0fSJin Yao 
2444d5a8bd0fSJin Yao void evlist__check_mem_load_aux(struct evlist *evlist)
2445d5a8bd0fSJin Yao {
2446d5a8bd0fSJin Yao 	struct evsel *leader, *evsel, *pos;
2447d5a8bd0fSJin Yao 
2448d5a8bd0fSJin Yao 	/*
2449d5a8bd0fSJin Yao 	 * For some platforms, the 'mem-loads' event is required to use
2450d5a8bd0fSJin Yao 	 * together with 'mem-loads-aux' within a group and 'mem-loads-aux'
2451d5a8bd0fSJin Yao 	 * must be the group leader. Now we disable this group before reporting
2452d5a8bd0fSJin Yao 	 * because 'mem-loads-aux' is just an auxiliary event. It doesn't carry
2453d5a8bd0fSJin Yao 	 * any valid memory load information.
2454d5a8bd0fSJin Yao 	 */
2455d5a8bd0fSJin Yao 	evlist__for_each_entry(evlist, evsel) {
2456fba7c866SJiri Olsa 		leader = evsel__leader(evsel);
2457d5a8bd0fSJin Yao 		if (leader == evsel)
2458d5a8bd0fSJin Yao 			continue;
2459d5a8bd0fSJin Yao 
2460d5a8bd0fSJin Yao 		if (leader->name && strstr(leader->name, "mem-loads-aux")) {
2461d5a8bd0fSJin Yao 			for_each_group_evsel(pos, leader) {
2462fba7c866SJiri Olsa 				evsel__set_leader(pos, pos);
2463d5a8bd0fSJin Yao 				pos->core.nr_members = 0;
2464d5a8bd0fSJin Yao 			}
2465d5a8bd0fSJin Yao 		}
2466d5a8bd0fSJin Yao 	}
2467d5a8bd0fSJin Yao }
2468*5ac72634SIan Rogers 
2469*5ac72634SIan Rogers /**
2470*5ac72634SIan Rogers  * evlist__warn_user_requested_cpus() - Check each evsel against requested CPUs
2471*5ac72634SIan Rogers  *     and warn if the user CPU list is inapplicable for the event's PMU's
2472*5ac72634SIan Rogers  *     CPUs. Not core PMUs list a CPU in sysfs, but this may be overwritten by a
2473*5ac72634SIan Rogers  *     user requested CPU and so any online CPU is applicable. Core PMUs handle
2474*5ac72634SIan Rogers  *     events on the CPUs in their list and otherwise the event isn't supported.
2475*5ac72634SIan Rogers  * @evlist: The list of events being checked.
2476*5ac72634SIan Rogers  * @cpu_list: The user provided list of CPUs.
2477*5ac72634SIan Rogers  */
2478*5ac72634SIan Rogers void evlist__warn_user_requested_cpus(struct evlist *evlist, const char *cpu_list)
2479*5ac72634SIan Rogers {
2480*5ac72634SIan Rogers 	struct perf_cpu_map *user_requested_cpus;
2481*5ac72634SIan Rogers 	struct evsel *pos;
2482*5ac72634SIan Rogers 
2483*5ac72634SIan Rogers 	if (!cpu_list)
2484*5ac72634SIan Rogers 		return;
2485*5ac72634SIan Rogers 
2486*5ac72634SIan Rogers 	user_requested_cpus = perf_cpu_map__new(cpu_list);
2487*5ac72634SIan Rogers 	if (!user_requested_cpus)
2488*5ac72634SIan Rogers 		return;
2489*5ac72634SIan Rogers 
2490*5ac72634SIan Rogers 	evlist__for_each_entry(evlist, pos) {
2491*5ac72634SIan Rogers 		struct perf_cpu_map *intersect, *to_test;
2492*5ac72634SIan Rogers 		const struct perf_pmu *pmu = evsel__find_pmu(pos);
2493*5ac72634SIan Rogers 
2494*5ac72634SIan Rogers 		to_test = pmu && pmu->is_core ? pmu->cpus : cpu_map__online();
2495*5ac72634SIan Rogers 		intersect = perf_cpu_map__intersect(to_test, user_requested_cpus);
2496*5ac72634SIan Rogers 		if (!perf_cpu_map__equal(intersect, user_requested_cpus)) {
2497*5ac72634SIan Rogers 			char buf[128];
2498*5ac72634SIan Rogers 
2499*5ac72634SIan Rogers 			cpu_map__snprint(to_test, buf, sizeof(buf));
2500*5ac72634SIan Rogers 			pr_warning("WARNING: A requested CPU in '%s' is not supported by PMU '%s' (CPUs %s) for event '%s'\n",
2501*5ac72634SIan Rogers 				cpu_list, pmu ? pmu->name : "cpu", buf, evsel__name(pos));
2502*5ac72634SIan Rogers 		}
2503*5ac72634SIan Rogers 		perf_cpu_map__put(intersect);
2504*5ac72634SIan Rogers 	}
2505*5ac72634SIan Rogers 	perf_cpu_map__put(user_requested_cpus);
2506*5ac72634SIan Rogers }
2507