xref: /openbmc/linux/tools/perf/util/evlist.c (revision 329725d5)
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"
18e3e1a54fSAdrian Hunter #include "debug.h"
1958db1d6eSArnaldo Carvalho de Melo #include "units.h"
205508c9daSSong Liu #include "bpf_counter.h"
2120f2be1dSJiri Olsa #include <internal/lib.h> // page_size
227736627bSAndi Kleen #include "affinity.h"
23c1a604dfSArnaldo Carvalho de Melo #include "../perf.h"
2454cc54deSWang Nan #include "asm/bug.h"
25657ee553SSong Liu #include "bpf-event.h"
26da949f50SArnaldo Carvalho de Melo #include "util/string2.h"
2740c7d246SArnaldo Carvalho de Melo #include "util/perf_api_probe.h"
28142544a9SJiri Olsa #include "util/evsel_fprintf.h"
29b53a0755SJin Yao #include "util/evlist-hybrid.h"
301d3351e6SJin Yao #include "util/pmu.h"
319607ad3aSArnaldo Carvalho de Melo #include <signal.h>
3235b9d88eSArnaldo Carvalho de Melo #include <unistd.h>
33b397f846SArnaldo Carvalho de Melo #include <sched.h>
34f2a39fe8SArnaldo Carvalho de Melo #include <stdlib.h>
35361c99a6SArnaldo Carvalho de Melo 
3650d08e47SArnaldo Carvalho de Melo #include "parse-events.h"
374b6ab94eSJosh Poimboeuf #include <subcmd/parse-options.h>
3850d08e47SArnaldo Carvalho de Melo 
39bafae98eSArnaldo Carvalho de Melo #include <fcntl.h>
4086a5e0c2SArnaldo Carvalho de Melo #include <sys/ioctl.h>
41f8a95309SArnaldo Carvalho de Melo #include <sys/mman.h>
42a7672d1dSArnaldo Carvalho de Melo #include <sys/prctl.h>
43f8a95309SArnaldo Carvalho de Melo 
4470db7533SArnaldo Carvalho de Melo #include <linux/bitops.h>
4570db7533SArnaldo Carvalho de Melo #include <linux/hash.h>
460389cd1fSArnaldo Carvalho de Melo #include <linux/log2.h>
478dd2a131SJiri Olsa #include <linux/err.h>
488520a98dSArnaldo Carvalho de Melo #include <linux/string.h>
497f7c536fSArnaldo Carvalho de Melo #include <linux/zalloc.h>
504562a739SJiri Olsa #include <perf/evlist.h>
5188761fa1SJiri Olsa #include <perf/evsel.h>
529c3516d1SJiri Olsa #include <perf/cpumap.h>
537728fa0cSJiri Olsa #include <perf/mmap.h>
5470db7533SArnaldo Carvalho de Melo 
55e14e5497SArnaldo Carvalho de Melo #include <internal/xyarray.h>
56e14e5497SArnaldo Carvalho de Melo 
57748fe088SArnaldo Carvalho de Melo #ifdef LACKS_SIGQUEUE_PROTOTYPE
58748fe088SArnaldo Carvalho de Melo int sigqueue(pid_t pid, int sig, const union sigval value);
59748fe088SArnaldo Carvalho de Melo #endif
60748fe088SArnaldo Carvalho de Melo 
619dfcb759SJiri Olsa #define FD(e, x, y) (*(int *)xyarray__entry(e->core.fd, x, y))
628cd36f3eSJiri Olsa #define SID(e, x, y) xyarray__entry(e->core.sample_id, x, y)
63f8a95309SArnaldo Carvalho de Melo 
6452c86bcaSJiri Olsa void evlist__init(struct evlist *evlist, struct perf_cpu_map *cpus,
659749b90eSJiri Olsa 		  struct perf_thread_map *threads)
66361c99a6SArnaldo Carvalho de Melo {
674562a739SJiri Olsa 	perf_evlist__init(&evlist->core);
68453fa030SJiri Olsa 	perf_evlist__set_maps(&evlist->core, cpus, threads);
6935b9d88eSArnaldo Carvalho de Melo 	evlist->workload.pid = -1;
7054cc54deSWang Nan 	evlist->bkw_mmap_state = BKW_MMAP_NOTREADY;
718ab705b5SAlexey Budankov 	evlist->ctl_fd.fd = -1;
728ab705b5SAlexey Budankov 	evlist->ctl_fd.ack = -1;
738ab705b5SAlexey Budankov 	evlist->ctl_fd.pos = -1;
74361c99a6SArnaldo Carvalho de Melo }
75361c99a6SArnaldo Carvalho de Melo 
760f98b11cSJiri Olsa struct evlist *evlist__new(void)
77ef1d1af2SArnaldo Carvalho de Melo {
7863503dbaSJiri Olsa 	struct evlist *evlist = zalloc(sizeof(*evlist));
79ef1d1af2SArnaldo Carvalho de Melo 
80ef1d1af2SArnaldo Carvalho de Melo 	if (evlist != NULL)
8152c86bcaSJiri Olsa 		evlist__init(evlist, NULL, NULL);
82ef1d1af2SArnaldo Carvalho de Melo 
83361c99a6SArnaldo Carvalho de Melo 	return evlist;
84361c99a6SArnaldo Carvalho de Melo }
85361c99a6SArnaldo Carvalho de Melo 
86606e2c29SArnaldo Carvalho de Melo struct evlist *evlist__new_default(void)
87b22d54b0SJiri Olsa {
880f98b11cSJiri Olsa 	struct evlist *evlist = evlist__new();
89b22d54b0SJiri Olsa 
90e251abeeSArnaldo Carvalho de Melo 	if (evlist && evlist__add_default(evlist)) {
91c12995a5SJiri Olsa 		evlist__delete(evlist);
92b22d54b0SJiri Olsa 		evlist = NULL;
93b22d54b0SJiri Olsa 	}
94b22d54b0SJiri Olsa 
95b22d54b0SJiri Olsa 	return evlist;
96b22d54b0SJiri Olsa }
97b22d54b0SJiri Olsa 
98606e2c29SArnaldo Carvalho de Melo struct evlist *evlist__new_dummy(void)
995bae0250SArnaldo Carvalho de Melo {
1000f98b11cSJiri Olsa 	struct evlist *evlist = evlist__new();
1015bae0250SArnaldo Carvalho de Melo 
102e251abeeSArnaldo Carvalho de Melo 	if (evlist && evlist__add_dummy(evlist)) {
103c12995a5SJiri Olsa 		evlist__delete(evlist);
1045bae0250SArnaldo Carvalho de Melo 		evlist = NULL;
1055bae0250SArnaldo Carvalho de Melo 	}
1065bae0250SArnaldo Carvalho de Melo 
1075bae0250SArnaldo Carvalho de Melo 	return evlist;
1085bae0250SArnaldo Carvalho de Melo }
1095bae0250SArnaldo Carvalho de Melo 
11075562573SAdrian Hunter /**
1110a7e7ec9SArnaldo Carvalho de Melo  * evlist__set_id_pos - set the positions of event ids.
11275562573SAdrian Hunter  * @evlist: selected event list
11375562573SAdrian Hunter  *
11475562573SAdrian Hunter  * Events with compatible sample types all have the same id_pos
11575562573SAdrian Hunter  * and is_pos.  For convenience, put a copy on evlist.
11675562573SAdrian Hunter  */
1170a7e7ec9SArnaldo Carvalho de Melo void evlist__set_id_pos(struct evlist *evlist)
11875562573SAdrian Hunter {
119515dbe48SJiri Olsa 	struct evsel *first = evlist__first(evlist);
12075562573SAdrian Hunter 
12175562573SAdrian Hunter 	evlist->id_pos = first->id_pos;
12275562573SAdrian Hunter 	evlist->is_pos = first->is_pos;
12375562573SAdrian Hunter }
12475562573SAdrian Hunter 
1250a7e7ec9SArnaldo Carvalho de Melo static void evlist__update_id_pos(struct evlist *evlist)
126733cd2feSAdrian Hunter {
12732dcd021SJiri Olsa 	struct evsel *evsel;
128733cd2feSAdrian Hunter 
129e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, evsel)
1304b5e87b7SArnaldo Carvalho de Melo 		evsel__calc_id_pos(evsel);
131733cd2feSAdrian Hunter 
1320a7e7ec9SArnaldo Carvalho de Melo 	evlist__set_id_pos(evlist);
133733cd2feSAdrian Hunter }
134733cd2feSAdrian Hunter 
135e6b1878dSJiri Olsa static void evlist__purge(struct evlist *evlist)
136361c99a6SArnaldo Carvalho de Melo {
13732dcd021SJiri Olsa 	struct evsel *pos, *n;
138361c99a6SArnaldo Carvalho de Melo 
139e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry_safe(evlist, n, pos) {
140b27c4eceSJiri Olsa 		list_del_init(&pos->core.node);
141d49e4695SArnaldo Carvalho de Melo 		pos->evlist = NULL;
1425eb2dd2aSJiri Olsa 		evsel__delete(pos);
143361c99a6SArnaldo Carvalho de Melo 	}
144361c99a6SArnaldo Carvalho de Melo 
1456484d2f9SJiri Olsa 	evlist->core.nr_entries = 0;
146361c99a6SArnaldo Carvalho de Melo }
147361c99a6SArnaldo Carvalho de Melo 
148470579b0SJiri Olsa void evlist__exit(struct evlist *evlist)
149ef1d1af2SArnaldo Carvalho de Melo {
15004662523SArnaldo Carvalho de Melo 	zfree(&evlist->mmap);
1510b72d69aSWang Nan 	zfree(&evlist->overwrite_mmap);
15293dd6e28SJiri Olsa 	perf_evlist__exit(&evlist->core);
153ef1d1af2SArnaldo Carvalho de Melo }
154ef1d1af2SArnaldo Carvalho de Melo 
155c12995a5SJiri Olsa void evlist__delete(struct evlist *evlist)
156361c99a6SArnaldo Carvalho de Melo {
1570b04b3dcSArnaldo Carvalho de Melo 	if (evlist == NULL)
1580b04b3dcSArnaldo Carvalho de Melo 		return;
1590b04b3dcSArnaldo Carvalho de Melo 
160db6b7b13SJiri Olsa 	evlist__munmap(evlist);
161750b4edeSJiri Olsa 	evlist__close(evlist);
162e6b1878dSJiri Olsa 	evlist__purge(evlist);
163470579b0SJiri Olsa 	evlist__exit(evlist);
164361c99a6SArnaldo Carvalho de Melo 	free(evlist);
165361c99a6SArnaldo Carvalho de Melo }
166361c99a6SArnaldo Carvalho de Melo 
167a1cf3a75SJiri Olsa void evlist__add(struct evlist *evlist, struct evsel *entry)
168361c99a6SArnaldo Carvalho de Melo {
1696484d2f9SJiri Olsa 	perf_evlist__add(&evlist->core, &entry->core);
17038fe0e01SJiri Olsa 	entry->evlist = evlist;
17138fe0e01SJiri Olsa 	entry->tracking = !entry->core.idx;
1726484d2f9SJiri Olsa 
1736484d2f9SJiri Olsa 	if (evlist->core.nr_entries == 1)
1740a7e7ec9SArnaldo Carvalho de Melo 		evlist__set_id_pos(evlist);
175361c99a6SArnaldo Carvalho de Melo }
176361c99a6SArnaldo Carvalho de Melo 
17716251027SJiri Olsa void evlist__remove(struct evlist *evlist, struct evsel *evsel)
1784768230aSAdrian Hunter {
1794768230aSAdrian Hunter 	evsel->evlist = NULL;
18052e22fb8SJiri Olsa 	perf_evlist__remove(&evlist->core, &evsel->core);
1814768230aSAdrian Hunter }
1824768230aSAdrian Hunter 
183e414fd1aSArnaldo Carvalho de Melo void evlist__splice_list_tail(struct evlist *evlist, struct list_head *list)
18450d08e47SArnaldo Carvalho de Melo {
1856d2783feSJohn Garry 	while (!list_empty(list)) {
1866d2783feSJohn Garry 		struct evsel *evsel, *temp, *leader = NULL;
18775562573SAdrian Hunter 
188e5cadb93SArnaldo Carvalho de Melo 		__evlist__for_each_entry_safe(list, temp, evsel) {
189b27c4eceSJiri Olsa 			list_del_init(&evsel->core.node);
190a1cf3a75SJiri Olsa 			evlist__add(evlist, evsel);
1916d2783feSJohn Garry 			leader = evsel;
1926d2783feSJohn Garry 			break;
1936d2783feSJohn Garry 		}
1946d2783feSJohn Garry 
1956d2783feSJohn Garry 		__evlist__for_each_entry_safe(list, temp, evsel) {
196fba7c866SJiri Olsa 			if (evsel__has_leader(evsel, leader)) {
1976d2783feSJohn Garry 				list_del_init(&evsel->core.node);
1986d2783feSJohn Garry 				evlist__add(evlist, evsel);
1996d2783feSJohn Garry 			}
2006d2783feSJohn Garry 		}
201f114d6efSAdrian Hunter 	}
20250d08e47SArnaldo Carvalho de Melo }
20350d08e47SArnaldo Carvalho de Melo 
204c0e53476SArnaldo Carvalho de Melo int __evlist__set_tracepoints_handlers(struct evlist *evlist,
205c0e53476SArnaldo Carvalho de Melo 				       const struct evsel_str_handler *assocs, size_t nr_assocs)
206c0e53476SArnaldo Carvalho de Melo {
207c0e53476SArnaldo Carvalho de Melo 	size_t i;
208c0e53476SArnaldo Carvalho de Melo 	int err;
209c0e53476SArnaldo Carvalho de Melo 
210c0e53476SArnaldo Carvalho de Melo 	for (i = 0; i < nr_assocs; i++) {
211c0e53476SArnaldo Carvalho de Melo 		// Adding a handler for an event not in this evlist, just ignore it.
212b02736f7SArnaldo Carvalho de Melo 		struct evsel *evsel = evlist__find_tracepoint_by_name(evlist, assocs[i].name);
213c0e53476SArnaldo Carvalho de Melo 		if (evsel == NULL)
214c0e53476SArnaldo Carvalho de Melo 			continue;
215c0e53476SArnaldo Carvalho de Melo 
216c0e53476SArnaldo Carvalho de Melo 		err = -EEXIST;
217c0e53476SArnaldo Carvalho de Melo 		if (evsel->handler != NULL)
218c0e53476SArnaldo Carvalho de Melo 			goto out;
219c0e53476SArnaldo Carvalho de Melo 		evsel->handler = assocs[i].handler;
220c0e53476SArnaldo Carvalho de Melo 	}
221c0e53476SArnaldo Carvalho de Melo 
222c0e53476SArnaldo Carvalho de Melo 	err = 0;
223c0e53476SArnaldo Carvalho de Melo out:
224c0e53476SArnaldo Carvalho de Melo 	return err;
225c0e53476SArnaldo Carvalho de Melo }
226c0e53476SArnaldo Carvalho de Melo 
227a622eafaSArnaldo Carvalho de Melo void evlist__set_leader(struct evlist *evlist)
2286a4bb04cSJiri Olsa {
2292e6263abSJiri Olsa 	perf_evlist__set_leader(&evlist->core);
23097f63e4aSNamhyung Kim }
2316a4bb04cSJiri Olsa 
232e251abeeSArnaldo Carvalho de Melo int __evlist__add_default(struct evlist *evlist, bool precise)
233361c99a6SArnaldo Carvalho de Melo {
234b53a0755SJin Yao 	struct evsel *evsel;
235361c99a6SArnaldo Carvalho de Melo 
236b53a0755SJin Yao 	evsel = evsel__new_cycles(precise, PERF_TYPE_HARDWARE,
237b53a0755SJin Yao 				  PERF_COUNT_HW_CPU_CYCLES);
238361c99a6SArnaldo Carvalho de Melo 	if (evsel == NULL)
2397c48dcfdSArnaldo Carvalho de Melo 		return -ENOMEM;
240361c99a6SArnaldo Carvalho de Melo 
241a1cf3a75SJiri Olsa 	evlist__add(evlist, evsel);
242361c99a6SArnaldo Carvalho de Melo 	return 0;
243361c99a6SArnaldo Carvalho de Melo }
2445c581041SArnaldo Carvalho de Melo 
24582944899SAdrian Hunter static struct evsel *evlist__dummy_event(struct evlist *evlist)
2465bae0250SArnaldo Carvalho de Melo {
2475bae0250SArnaldo Carvalho de Melo 	struct perf_event_attr attr = {
2485bae0250SArnaldo Carvalho de Melo 		.type	= PERF_TYPE_SOFTWARE,
2495bae0250SArnaldo Carvalho de Melo 		.config = PERF_COUNT_SW_DUMMY,
2505bae0250SArnaldo Carvalho de Melo 		.size	= sizeof(attr), /* to capture ABI version */
2515bae0250SArnaldo Carvalho de Melo 	};
25282944899SAdrian Hunter 
25382944899SAdrian Hunter 	return evsel__new_idx(&attr, evlist->core.nr_entries);
25482944899SAdrian Hunter }
25582944899SAdrian Hunter 
25682944899SAdrian Hunter int evlist__add_dummy(struct evlist *evlist)
25782944899SAdrian Hunter {
25882944899SAdrian Hunter 	struct evsel *evsel = evlist__dummy_event(evlist);
2595bae0250SArnaldo Carvalho de Melo 
2605bae0250SArnaldo Carvalho de Melo 	if (evsel == NULL)
2615bae0250SArnaldo Carvalho de Melo 		return -ENOMEM;
2625bae0250SArnaldo Carvalho de Melo 
263a1cf3a75SJiri Olsa 	evlist__add(evlist, evsel);
2645bae0250SArnaldo Carvalho de Melo 	return 0;
2655bae0250SArnaldo Carvalho de Melo }
2665bae0250SArnaldo Carvalho de Melo 
267126d68fdSAdrian Hunter static void evlist__add_on_all_cpus(struct evlist *evlist, struct evsel *evsel)
268126d68fdSAdrian Hunter {
269126d68fdSAdrian Hunter 	evsel->core.system_wide = true;
270126d68fdSAdrian Hunter 
271126d68fdSAdrian Hunter 	/*
272126d68fdSAdrian Hunter 	 * All CPUs.
273126d68fdSAdrian Hunter 	 *
274126d68fdSAdrian Hunter 	 * Note perf_event_open() does not accept CPUs that are not online, so
275126d68fdSAdrian Hunter 	 * in fact this CPU list will include only all online CPUs.
276126d68fdSAdrian Hunter 	 */
277126d68fdSAdrian Hunter 	perf_cpu_map__put(evsel->core.own_cpus);
278126d68fdSAdrian Hunter 	evsel->core.own_cpus = perf_cpu_map__new(NULL);
279126d68fdSAdrian Hunter 	perf_cpu_map__put(evsel->core.cpus);
280126d68fdSAdrian Hunter 	evsel->core.cpus = perf_cpu_map__get(evsel->core.own_cpus);
281126d68fdSAdrian Hunter 
282126d68fdSAdrian Hunter 	/* No threads */
283126d68fdSAdrian Hunter 	perf_thread_map__put(evsel->core.threads);
284126d68fdSAdrian Hunter 	evsel->core.threads = perf_thread_map__new_dummy();
285126d68fdSAdrian Hunter 
286126d68fdSAdrian Hunter 	evlist__add(evlist, evsel);
287126d68fdSAdrian Hunter }
288126d68fdSAdrian Hunter 
289126d68fdSAdrian Hunter struct evsel *evlist__add_aux_dummy(struct evlist *evlist, bool system_wide)
290126d68fdSAdrian Hunter {
291126d68fdSAdrian Hunter 	struct evsel *evsel = evlist__dummy_event(evlist);
292126d68fdSAdrian Hunter 
293126d68fdSAdrian Hunter 	if (!evsel)
294126d68fdSAdrian Hunter 		return NULL;
295126d68fdSAdrian Hunter 
296126d68fdSAdrian Hunter 	evsel->core.attr.exclude_kernel = 1;
297126d68fdSAdrian Hunter 	evsel->core.attr.exclude_guest = 1;
298126d68fdSAdrian Hunter 	evsel->core.attr.exclude_hv = 1;
299126d68fdSAdrian Hunter 	evsel->core.attr.freq = 0;
300126d68fdSAdrian Hunter 	evsel->core.attr.sample_period = 1;
301126d68fdSAdrian Hunter 	evsel->no_aux_samples = true;
302126d68fdSAdrian Hunter 	evsel->name = strdup("dummy:u");
303126d68fdSAdrian Hunter 
304126d68fdSAdrian Hunter 	if (system_wide)
305126d68fdSAdrian Hunter 		evlist__add_on_all_cpus(evlist, evsel);
306126d68fdSAdrian Hunter 	else
307126d68fdSAdrian Hunter 		evlist__add(evlist, evsel);
308126d68fdSAdrian Hunter 
309126d68fdSAdrian Hunter 	return evsel;
310126d68fdSAdrian Hunter }
311126d68fdSAdrian Hunter 
312cdb204adSKan Liang int evlist__add_attrs(struct evlist *evlist, struct perf_event_attr *attrs, size_t nr_attrs)
31350d08e47SArnaldo Carvalho de Melo {
31432dcd021SJiri Olsa 	struct evsel *evsel, *n;
31550d08e47SArnaldo Carvalho de Melo 	LIST_HEAD(head);
31650d08e47SArnaldo Carvalho de Melo 	size_t i;
31750d08e47SArnaldo Carvalho de Melo 
31850d08e47SArnaldo Carvalho de Melo 	for (i = 0; i < nr_attrs; i++) {
3198f6725a2SArnaldo Carvalho de Melo 		evsel = evsel__new_idx(attrs + i, evlist->core.nr_entries + i);
32050d08e47SArnaldo Carvalho de Melo 		if (evsel == NULL)
32150d08e47SArnaldo Carvalho de Melo 			goto out_delete_partial_list;
322b27c4eceSJiri Olsa 		list_add_tail(&evsel->core.node, &head);
32350d08e47SArnaldo Carvalho de Melo 	}
32450d08e47SArnaldo Carvalho de Melo 
325e414fd1aSArnaldo Carvalho de Melo 	evlist__splice_list_tail(evlist, &head);
32650d08e47SArnaldo Carvalho de Melo 
32750d08e47SArnaldo Carvalho de Melo 	return 0;
32850d08e47SArnaldo Carvalho de Melo 
32950d08e47SArnaldo Carvalho de Melo out_delete_partial_list:
330e5cadb93SArnaldo Carvalho de Melo 	__evlist__for_each_entry_safe(&head, n, evsel)
3315eb2dd2aSJiri Olsa 		evsel__delete(evsel);
33250d08e47SArnaldo Carvalho de Melo 	return -1;
33350d08e47SArnaldo Carvalho de Melo }
33450d08e47SArnaldo Carvalho de Melo 
335e251abeeSArnaldo Carvalho de Melo int __evlist__add_default_attrs(struct evlist *evlist, struct perf_event_attr *attrs, size_t nr_attrs)
33679695e1bSArnaldo Carvalho de Melo {
33779695e1bSArnaldo Carvalho de Melo 	size_t i;
33879695e1bSArnaldo Carvalho de Melo 
33979695e1bSArnaldo Carvalho de Melo 	for (i = 0; i < nr_attrs; i++)
34079695e1bSArnaldo Carvalho de Melo 		event_attr_init(attrs + i);
34179695e1bSArnaldo Carvalho de Melo 
342a1cf3a75SJiri Olsa 	return evlist__add_attrs(evlist, attrs, nr_attrs);
34379695e1bSArnaldo Carvalho de Melo }
34479695e1bSArnaldo Carvalho de Melo 
345a9c1ecdaSKan Liang __weak int arch_evlist__add_default_attrs(struct evlist *evlist,
346a9c1ecdaSKan Liang 					  struct perf_event_attr *attrs,
347a9c1ecdaSKan Liang 					  size_t nr_attrs)
34842641d6fSKan Liang {
349a9c1ecdaSKan Liang 	if (!nr_attrs)
35042641d6fSKan Liang 		return 0;
351a9c1ecdaSKan Liang 
352a9c1ecdaSKan Liang 	return __evlist__add_default_attrs(evlist, attrs, nr_attrs);
35342641d6fSKan Liang }
35442641d6fSKan Liang 
355b02736f7SArnaldo Carvalho de Melo struct evsel *evlist__find_tracepoint_by_id(struct evlist *evlist, int id)
356ee29be62SArnaldo Carvalho de Melo {
35732dcd021SJiri Olsa 	struct evsel *evsel;
358ee29be62SArnaldo Carvalho de Melo 
359e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, evsel) {
3601fc632ceSJiri Olsa 		if (evsel->core.attr.type   == PERF_TYPE_TRACEPOINT &&
3611fc632ceSJiri Olsa 		    (int)evsel->core.attr.config == id)
362ee29be62SArnaldo Carvalho de Melo 			return evsel;
363ee29be62SArnaldo Carvalho de Melo 	}
364ee29be62SArnaldo Carvalho de Melo 
365ee29be62SArnaldo Carvalho de Melo 	return NULL;
366ee29be62SArnaldo Carvalho de Melo }
367ee29be62SArnaldo Carvalho de Melo 
368b02736f7SArnaldo Carvalho de Melo struct evsel *evlist__find_tracepoint_by_name(struct evlist *evlist, const char *name)
369a2f2804aSDavid Ahern {
37032dcd021SJiri Olsa 	struct evsel *evsel;
371a2f2804aSDavid Ahern 
372e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, evsel) {
3731fc632ceSJiri Olsa 		if ((evsel->core.attr.type == PERF_TYPE_TRACEPOINT) &&
374a2f2804aSDavid Ahern 		    (strcmp(evsel->name, name) == 0))
375a2f2804aSDavid Ahern 			return evsel;
376a2f2804aSDavid Ahern 	}
377a2f2804aSDavid Ahern 
378a2f2804aSDavid Ahern 	return NULL;
379a2f2804aSDavid Ahern }
380a2f2804aSDavid Ahern 
381e251abeeSArnaldo Carvalho de Melo int evlist__add_newtp(struct evlist *evlist, const char *sys, const char *name, void *handler)
38239876e7dSArnaldo Carvalho de Melo {
3838f6725a2SArnaldo Carvalho de Melo 	struct evsel *evsel = evsel__newtp(sys, name);
38439876e7dSArnaldo Carvalho de Melo 
3858dd2a131SJiri Olsa 	if (IS_ERR(evsel))
38639876e7dSArnaldo Carvalho de Melo 		return -1;
38739876e7dSArnaldo Carvalho de Melo 
388744a9719SArnaldo Carvalho de Melo 	evsel->handler = handler;
389a1cf3a75SJiri Olsa 	evlist__add(evlist, evsel);
39039876e7dSArnaldo Carvalho de Melo 	return 0;
39139876e7dSArnaldo Carvalho de Melo }
39239876e7dSArnaldo Carvalho de Melo 
393472832d2SIan Rogers struct evlist_cpu_iterator evlist__cpu_begin(struct evlist *evlist, struct affinity *affinity)
394a8cbe40fSAndi Kleen {
395472832d2SIan Rogers 	struct evlist_cpu_iterator itr = {
396472832d2SIan Rogers 		.container = evlist,
3978b464eacSIan Rogers 		.evsel = NULL,
398472832d2SIan Rogers 		.cpu_map_idx = 0,
399472832d2SIan Rogers 		.evlist_cpu_map_idx = 0,
400472832d2SIan Rogers 		.evlist_cpu_map_nr = perf_cpu_map__nr(evlist->core.all_cpus),
4016d18804bSIan Rogers 		.cpu = (struct perf_cpu){ .cpu = -1},
402472832d2SIan Rogers 		.affinity = affinity,
403472832d2SIan Rogers 	};
404a8cbe40fSAndi Kleen 
4058b464eacSIan Rogers 	if (evlist__empty(evlist)) {
4068b464eacSIan Rogers 		/* Ensure the empty list doesn't iterate. */
4078b464eacSIan Rogers 		itr.evlist_cpu_map_idx = itr.evlist_cpu_map_nr;
4088b464eacSIan Rogers 	} else {
4098b464eacSIan Rogers 		itr.evsel = evlist__first(evlist);
410472832d2SIan Rogers 		if (itr.affinity) {
411472832d2SIan Rogers 			itr.cpu = perf_cpu_map__cpu(evlist->core.all_cpus, 0);
4126d18804bSIan Rogers 			affinity__set(itr.affinity, itr.cpu.cpu);
413472832d2SIan Rogers 			itr.cpu_map_idx = perf_cpu_map__idx(itr.evsel->core.cpus, itr.cpu);
414a8cbe40fSAndi Kleen 			/*
4158b464eacSIan Rogers 			 * If this CPU isn't in the evsel's cpu map then advance
4168b464eacSIan Rogers 			 * through the list.
417a8cbe40fSAndi Kleen 			 */
418472832d2SIan Rogers 			if (itr.cpu_map_idx == -1)
419472832d2SIan Rogers 				evlist_cpu_iterator__next(&itr);
420472832d2SIan Rogers 		}
4218b464eacSIan Rogers 	}
422472832d2SIan Rogers 	return itr;
423a8cbe40fSAndi Kleen }
424a8cbe40fSAndi Kleen 
425472832d2SIan Rogers void evlist_cpu_iterator__next(struct evlist_cpu_iterator *evlist_cpu_itr)
426a8cbe40fSAndi Kleen {
427472832d2SIan Rogers 	while (evlist_cpu_itr->evsel != evlist__last(evlist_cpu_itr->container)) {
428472832d2SIan Rogers 		evlist_cpu_itr->evsel = evsel__next(evlist_cpu_itr->evsel);
429472832d2SIan Rogers 		evlist_cpu_itr->cpu_map_idx =
430472832d2SIan Rogers 			perf_cpu_map__idx(evlist_cpu_itr->evsel->core.cpus,
431472832d2SIan Rogers 					  evlist_cpu_itr->cpu);
432472832d2SIan Rogers 		if (evlist_cpu_itr->cpu_map_idx != -1)
433472832d2SIan Rogers 			return;
434472832d2SIan Rogers 	}
435472832d2SIan Rogers 	evlist_cpu_itr->evlist_cpu_map_idx++;
436472832d2SIan Rogers 	if (evlist_cpu_itr->evlist_cpu_map_idx < evlist_cpu_itr->evlist_cpu_map_nr) {
437472832d2SIan Rogers 		evlist_cpu_itr->evsel = evlist__first(evlist_cpu_itr->container);
438472832d2SIan Rogers 		evlist_cpu_itr->cpu =
439472832d2SIan Rogers 			perf_cpu_map__cpu(evlist_cpu_itr->container->core.all_cpus,
440472832d2SIan Rogers 					  evlist_cpu_itr->evlist_cpu_map_idx);
441472832d2SIan Rogers 		if (evlist_cpu_itr->affinity)
4426d18804bSIan Rogers 			affinity__set(evlist_cpu_itr->affinity, evlist_cpu_itr->cpu.cpu);
443472832d2SIan Rogers 		evlist_cpu_itr->cpu_map_idx =
444472832d2SIan Rogers 			perf_cpu_map__idx(evlist_cpu_itr->evsel->core.cpus,
445472832d2SIan Rogers 					  evlist_cpu_itr->cpu);
446472832d2SIan Rogers 		/*
447472832d2SIan Rogers 		 * If this CPU isn't in the evsel's cpu map then advance through
448472832d2SIan Rogers 		 * the list.
449472832d2SIan Rogers 		 */
450472832d2SIan Rogers 		if (evlist_cpu_itr->cpu_map_idx == -1)
451472832d2SIan Rogers 			evlist_cpu_iterator__next(evlist_cpu_itr);
452472832d2SIan Rogers 	}
453a8cbe40fSAndi Kleen }
454a8cbe40fSAndi Kleen 
455472832d2SIan Rogers bool evlist_cpu_iterator__end(const struct evlist_cpu_iterator *evlist_cpu_itr)
456a8cbe40fSAndi Kleen {
457472832d2SIan Rogers 	return evlist_cpu_itr->evlist_cpu_map_idx >= evlist_cpu_itr->evlist_cpu_map_nr;
458a8cbe40fSAndi Kleen }
459a8cbe40fSAndi Kleen 
4607cfcd1e0SJiri Olsa static int evsel__strcmp(struct evsel *pos, char *evsel_name)
4617cfcd1e0SJiri Olsa {
4627cfcd1e0SJiri Olsa 	if (!evsel_name)
4637cfcd1e0SJiri Olsa 		return 0;
4647cfcd1e0SJiri Olsa 	if (evsel__is_dummy_event(pos))
4657cfcd1e0SJiri Olsa 		return 1;
4667cfcd1e0SJiri Olsa 	return strcmp(pos->name, evsel_name);
4677cfcd1e0SJiri Olsa }
4687cfcd1e0SJiri Olsa 
4697cfcd1e0SJiri Olsa static int evlist__is_enabled(struct evlist *evlist)
4707cfcd1e0SJiri Olsa {
4717cfcd1e0SJiri Olsa 	struct evsel *pos;
4727cfcd1e0SJiri Olsa 
4737cfcd1e0SJiri Olsa 	evlist__for_each_entry(evlist, pos) {
4747cfcd1e0SJiri Olsa 		if (!evsel__is_group_leader(pos) || !pos->core.fd)
4757cfcd1e0SJiri Olsa 			continue;
4767cfcd1e0SJiri Olsa 		/* If at least one event is enabled, evlist is enabled. */
4777cfcd1e0SJiri Olsa 		if (!pos->disabled)
4787cfcd1e0SJiri Olsa 			return true;
4797cfcd1e0SJiri Olsa 	}
4807cfcd1e0SJiri Olsa 	return false;
4817cfcd1e0SJiri Olsa }
4827cfcd1e0SJiri Olsa 
483*329725d5SAdrian Hunter static void __evlist__disable(struct evlist *evlist, char *evsel_name, bool excl_dummy)
4844152ab37SArnaldo Carvalho de Melo {
48532dcd021SJiri Olsa 	struct evsel *pos;
486472832d2SIan Rogers 	struct evlist_cpu_iterator evlist_cpu_itr;
4870d3d2376SArnaldo Carvalho de Melo 	struct affinity saved_affinity, *affinity = NULL;
48887cf8360SAdrian Hunter 	bool has_imm = false;
489704e2f5bSAndi Kleen 
4900d3d2376SArnaldo Carvalho de Melo 	// See explanation in evlist__close()
4910df6ade7SIan Rogers 	if (!cpu_map__is_dummy(evlist->core.user_requested_cpus)) {
4920d3d2376SArnaldo Carvalho de Melo 		if (affinity__setup(&saved_affinity) < 0)
493704e2f5bSAndi Kleen 			return;
4940d3d2376SArnaldo Carvalho de Melo 		affinity = &saved_affinity;
4950d3d2376SArnaldo Carvalho de Melo 	}
496704e2f5bSAndi Kleen 
49787cf8360SAdrian Hunter 	/* Disable 'immediate' events last */
498472832d2SIan Rogers 	for (int imm = 0; imm <= 1; imm++) {
4990d3d2376SArnaldo Carvalho de Melo 		evlist__for_each_cpu(evlist_cpu_itr, evlist, affinity) {
500472832d2SIan Rogers 			pos = evlist_cpu_itr.evsel;
5017cfcd1e0SJiri Olsa 			if (evsel__strcmp(pos, evsel_name))
5027cfcd1e0SJiri Olsa 				continue;
503c754c382SArnaldo Carvalho de Melo 			if (pos->disabled || !evsel__is_group_leader(pos) || !pos->core.fd)
5043fe4430dSJiri Olsa 				continue;
505*329725d5SAdrian Hunter 			if (excl_dummy && evsel__is_dummy_event(pos))
506*329725d5SAdrian Hunter 				continue;
50787cf8360SAdrian Hunter 			if (pos->immediate)
50887cf8360SAdrian Hunter 				has_imm = true;
50987cf8360SAdrian Hunter 			if (pos->immediate != imm)
51087cf8360SAdrian Hunter 				continue;
511472832d2SIan Rogers 			evsel__disable_cpu(pos, evlist_cpu_itr.cpu_map_idx);
512704e2f5bSAndi Kleen 		}
51387cf8360SAdrian Hunter 		if (!has_imm)
51487cf8360SAdrian Hunter 			break;
51587cf8360SAdrian Hunter 	}
51687cf8360SAdrian Hunter 
5170d3d2376SArnaldo Carvalho de Melo 	affinity__cleanup(affinity);
518704e2f5bSAndi Kleen 	evlist__for_each_entry(evlist, pos) {
5197cfcd1e0SJiri Olsa 		if (evsel__strcmp(pos, evsel_name))
5207cfcd1e0SJiri Olsa 			continue;
521c754c382SArnaldo Carvalho de Melo 		if (!evsel__is_group_leader(pos) || !pos->core.fd)
522704e2f5bSAndi Kleen 			continue;
523*329725d5SAdrian Hunter 		if (excl_dummy && evsel__is_dummy_event(pos))
524*329725d5SAdrian Hunter 			continue;
525704e2f5bSAndi Kleen 		pos->disabled = true;
5264152ab37SArnaldo Carvalho de Melo 	}
5272b56bcfbSArnaldo Carvalho de Melo 
5287cfcd1e0SJiri Olsa 	/*
5297cfcd1e0SJiri Olsa 	 * If we disabled only single event, we need to check
5307cfcd1e0SJiri Olsa 	 * the enabled state of the evlist manually.
5317cfcd1e0SJiri Olsa 	 */
5327cfcd1e0SJiri Olsa 	if (evsel_name)
5337cfcd1e0SJiri Olsa 		evlist->enabled = evlist__is_enabled(evlist);
5347cfcd1e0SJiri Olsa 	else
5352b56bcfbSArnaldo Carvalho de Melo 		evlist->enabled = false;
5364152ab37SArnaldo Carvalho de Melo }
5374152ab37SArnaldo Carvalho de Melo 
5387cfcd1e0SJiri Olsa void evlist__disable(struct evlist *evlist)
5397cfcd1e0SJiri Olsa {
540*329725d5SAdrian Hunter 	__evlist__disable(evlist, NULL, false);
541*329725d5SAdrian Hunter }
542*329725d5SAdrian Hunter 
543*329725d5SAdrian Hunter void evlist__disable_non_dummy(struct evlist *evlist)
544*329725d5SAdrian Hunter {
545*329725d5SAdrian Hunter 	__evlist__disable(evlist, NULL, true);
5467cfcd1e0SJiri Olsa }
5477cfcd1e0SJiri Olsa 
5487cfcd1e0SJiri Olsa void evlist__disable_evsel(struct evlist *evlist, char *evsel_name)
5497cfcd1e0SJiri Olsa {
550*329725d5SAdrian Hunter 	__evlist__disable(evlist, evsel_name, false);
5517cfcd1e0SJiri Olsa }
5527cfcd1e0SJiri Olsa 
553*329725d5SAdrian Hunter static void __evlist__enable(struct evlist *evlist, char *evsel_name, bool excl_dummy)
554764e16a3SDavid Ahern {
55532dcd021SJiri Olsa 	struct evsel *pos;
556472832d2SIan Rogers 	struct evlist_cpu_iterator evlist_cpu_itr;
557f350ee95SArnaldo Carvalho de Melo 	struct affinity saved_affinity, *affinity = NULL;
558764e16a3SDavid Ahern 
559f350ee95SArnaldo Carvalho de Melo 	// See explanation in evlist__close()
5600df6ade7SIan Rogers 	if (!cpu_map__is_dummy(evlist->core.user_requested_cpus)) {
561f350ee95SArnaldo Carvalho de Melo 		if (affinity__setup(&saved_affinity) < 0)
562704e2f5bSAndi Kleen 			return;
563f350ee95SArnaldo Carvalho de Melo 		affinity = &saved_affinity;
564f350ee95SArnaldo Carvalho de Melo 	}
565704e2f5bSAndi Kleen 
566f350ee95SArnaldo Carvalho de Melo 	evlist__for_each_cpu(evlist_cpu_itr, evlist, affinity) {
567472832d2SIan Rogers 		pos = evlist_cpu_itr.evsel;
5687cfcd1e0SJiri Olsa 		if (evsel__strcmp(pos, evsel_name))
5697cfcd1e0SJiri Olsa 			continue;
570c754c382SArnaldo Carvalho de Melo 		if (!evsel__is_group_leader(pos) || !pos->core.fd)
571704e2f5bSAndi Kleen 			continue;
572*329725d5SAdrian Hunter 		if (excl_dummy && evsel__is_dummy_event(pos))
573*329725d5SAdrian Hunter 			continue;
574472832d2SIan Rogers 		evsel__enable_cpu(pos, evlist_cpu_itr.cpu_map_idx);
575704e2f5bSAndi Kleen 	}
576f350ee95SArnaldo Carvalho de Melo 	affinity__cleanup(affinity);
577e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, pos) {
5787cfcd1e0SJiri Olsa 		if (evsel__strcmp(pos, evsel_name))
5797cfcd1e0SJiri Olsa 			continue;
580c754c382SArnaldo Carvalho de Melo 		if (!evsel__is_group_leader(pos) || !pos->core.fd)
5813fe4430dSJiri Olsa 			continue;
582*329725d5SAdrian Hunter 		if (excl_dummy && evsel__is_dummy_event(pos))
583*329725d5SAdrian Hunter 			continue;
584704e2f5bSAndi Kleen 		pos->disabled = false;
585764e16a3SDavid Ahern 	}
5862b56bcfbSArnaldo Carvalho de Melo 
5877cfcd1e0SJiri Olsa 	/*
5887cfcd1e0SJiri Olsa 	 * Even single event sets the 'enabled' for evlist,
5897cfcd1e0SJiri Olsa 	 * so the toggle can work properly and toggle to
5907cfcd1e0SJiri Olsa 	 * 'disabled' state.
5917cfcd1e0SJiri Olsa 	 */
5922b56bcfbSArnaldo Carvalho de Melo 	evlist->enabled = true;
5932b56bcfbSArnaldo Carvalho de Melo }
5942b56bcfbSArnaldo Carvalho de Melo 
5957cfcd1e0SJiri Olsa void evlist__enable(struct evlist *evlist)
5967cfcd1e0SJiri Olsa {
597*329725d5SAdrian Hunter 	__evlist__enable(evlist, NULL, false);
598*329725d5SAdrian Hunter }
599*329725d5SAdrian Hunter 
600*329725d5SAdrian Hunter void evlist__enable_non_dummy(struct evlist *evlist)
601*329725d5SAdrian Hunter {
602*329725d5SAdrian Hunter 	__evlist__enable(evlist, NULL, true);
6037cfcd1e0SJiri Olsa }
6047cfcd1e0SJiri Olsa 
6057cfcd1e0SJiri Olsa void evlist__enable_evsel(struct evlist *evlist, char *evsel_name)
6067cfcd1e0SJiri Olsa {
607*329725d5SAdrian Hunter 	__evlist__enable(evlist, evsel_name, false);
6087cfcd1e0SJiri Olsa }
6097cfcd1e0SJiri Olsa 
610ade9d208SArnaldo Carvalho de Melo void evlist__toggle_enable(struct evlist *evlist)
6112b56bcfbSArnaldo Carvalho de Melo {
612e74676deSJiri Olsa 	(evlist->enabled ? evlist__disable : evlist__enable)(evlist);
613764e16a3SDavid Ahern }
614764e16a3SDavid Ahern 
615f4009e7bSJiri Olsa int evlist__add_pollfd(struct evlist *evlist, int fd)
616e4b356b5SArnaldo Carvalho de Melo {
617ab4c1f9fSAlexey Budankov 	return perf_evlist__add_pollfd(&evlist->core, fd, NULL, POLLIN, fdarray_flag__default);
618e4b356b5SArnaldo Carvalho de Melo }
6191b85337dSArnaldo Carvalho de Melo 
620f4009e7bSJiri Olsa int evlist__filter_pollfd(struct evlist *evlist, short revents_and_mask)
6211ddec7f0SArnaldo Carvalho de Melo {
62284227cb1SJiri Olsa 	return perf_evlist__filter_pollfd(&evlist->core, revents_and_mask);
6231ddec7f0SArnaldo Carvalho de Melo }
6241ddec7f0SArnaldo Carvalho de Melo 
625e16c2ce7SYang Jihong #ifdef HAVE_EVENTFD_SUPPORT
626e16c2ce7SYang Jihong int evlist__add_wakeup_eventfd(struct evlist *evlist, int fd)
627e16c2ce7SYang Jihong {
628e16c2ce7SYang Jihong 	return perf_evlist__add_pollfd(&evlist->core, fd, NULL, POLLIN,
629a032ad87SAdrian Hunter 				       fdarray_flag__nonfilterable |
630a032ad87SAdrian Hunter 				       fdarray_flag__non_perf_event);
631e16c2ce7SYang Jihong }
632e16c2ce7SYang Jihong #endif
633e16c2ce7SYang Jihong 
63480ab2987SJiri Olsa int evlist__poll(struct evlist *evlist, int timeout)
635f66a889dSArnaldo Carvalho de Melo {
63680ab2987SJiri Olsa 	return perf_evlist__poll(&evlist->core, timeout);
637f66a889dSArnaldo Carvalho de Melo }
638f66a889dSArnaldo Carvalho de Melo 
6393ccf8a7bSArnaldo Carvalho de Melo struct perf_sample_id *evlist__id2sid(struct evlist *evlist, u64 id)
64070db7533SArnaldo Carvalho de Melo {
64170db7533SArnaldo Carvalho de Melo 	struct hlist_head *head;
64270db7533SArnaldo Carvalho de Melo 	struct perf_sample_id *sid;
64370db7533SArnaldo Carvalho de Melo 	int hash;
64470db7533SArnaldo Carvalho de Melo 
64570db7533SArnaldo Carvalho de Melo 	hash = hash_64(id, PERF_EVLIST__HLIST_BITS);
6461d5af02dSJiri Olsa 	head = &evlist->core.heads[hash];
64770db7533SArnaldo Carvalho de Melo 
648b67bfe0dSSasha Levin 	hlist_for_each_entry(sid, head, node)
64970db7533SArnaldo Carvalho de Melo 		if (sid->id == id)
650932a3594SJiri Olsa 			return sid;
651932a3594SJiri Olsa 
652932a3594SJiri Olsa 	return NULL;
653932a3594SJiri Olsa }
654932a3594SJiri Olsa 
6553ccf8a7bSArnaldo Carvalho de Melo struct evsel *evlist__id2evsel(struct evlist *evlist, u64 id)
656932a3594SJiri Olsa {
657932a3594SJiri Olsa 	struct perf_sample_id *sid;
658932a3594SJiri Olsa 
6596484d2f9SJiri Olsa 	if (evlist->core.nr_entries == 1 || !id)
660515dbe48SJiri Olsa 		return evlist__first(evlist);
661932a3594SJiri Olsa 
6623ccf8a7bSArnaldo Carvalho de Melo 	sid = evlist__id2sid(evlist, id);
663932a3594SJiri Olsa 	if (sid)
66470c20369SJiri Olsa 		return container_of(sid->evsel, struct evsel, core);
66530e68bccSNamhyung Kim 
6668cedf3a5SArnaldo Carvalho de Melo 	if (!evlist__sample_id_all(evlist))
667515dbe48SJiri Olsa 		return evlist__first(evlist);
66830e68bccSNamhyung Kim 
66970db7533SArnaldo Carvalho de Melo 	return NULL;
67070db7533SArnaldo Carvalho de Melo }
67104391debSArnaldo Carvalho de Melo 
6723ccf8a7bSArnaldo Carvalho de Melo struct evsel *evlist__id2evsel_strict(struct evlist *evlist, u64 id)
673dddcf6abSAdrian Hunter {
674dddcf6abSAdrian Hunter 	struct perf_sample_id *sid;
675dddcf6abSAdrian Hunter 
676dddcf6abSAdrian Hunter 	if (!id)
677dddcf6abSAdrian Hunter 		return NULL;
678dddcf6abSAdrian Hunter 
6793ccf8a7bSArnaldo Carvalho de Melo 	sid = evlist__id2sid(evlist, id);
680dddcf6abSAdrian Hunter 	if (sid)
68170c20369SJiri Olsa 		return container_of(sid->evsel, struct evsel, core);
682dddcf6abSAdrian Hunter 
683dddcf6abSAdrian Hunter 	return NULL;
684dddcf6abSAdrian Hunter }
685dddcf6abSAdrian Hunter 
6863ccf8a7bSArnaldo Carvalho de Melo static int evlist__event2id(struct evlist *evlist, union perf_event *event, u64 *id)
68775562573SAdrian Hunter {
688b1fcd190SJiri Olsa 	const __u64 *array = event->sample.array;
68975562573SAdrian Hunter 	ssize_t n;
69075562573SAdrian Hunter 
69175562573SAdrian Hunter 	n = (event->header.size - sizeof(event->header)) >> 3;
69275562573SAdrian Hunter 
69375562573SAdrian Hunter 	if (event->header.type == PERF_RECORD_SAMPLE) {
69475562573SAdrian Hunter 		if (evlist->id_pos >= n)
69575562573SAdrian Hunter 			return -1;
69675562573SAdrian Hunter 		*id = array[evlist->id_pos];
69775562573SAdrian Hunter 	} else {
69875562573SAdrian Hunter 		if (evlist->is_pos > n)
69975562573SAdrian Hunter 			return -1;
70075562573SAdrian Hunter 		n -= evlist->is_pos;
70175562573SAdrian Hunter 		*id = array[n];
70275562573SAdrian Hunter 	}
70375562573SAdrian Hunter 	return 0;
70475562573SAdrian Hunter }
70575562573SAdrian Hunter 
7063ccf8a7bSArnaldo Carvalho de Melo struct evsel *evlist__event2evsel(struct evlist *evlist, union perf_event *event)
70775562573SAdrian Hunter {
708515dbe48SJiri Olsa 	struct evsel *first = evlist__first(evlist);
70975562573SAdrian Hunter 	struct hlist_head *head;
71075562573SAdrian Hunter 	struct perf_sample_id *sid;
71175562573SAdrian Hunter 	int hash;
71275562573SAdrian Hunter 	u64 id;
71375562573SAdrian Hunter 
7146484d2f9SJiri Olsa 	if (evlist->core.nr_entries == 1)
71598be6966SAdrian Hunter 		return first;
71698be6966SAdrian Hunter 
7171fc632ceSJiri Olsa 	if (!first->core.attr.sample_id_all &&
71898be6966SAdrian Hunter 	    event->header.type != PERF_RECORD_SAMPLE)
71998be6966SAdrian Hunter 		return first;
72075562573SAdrian Hunter 
7213ccf8a7bSArnaldo Carvalho de Melo 	if (evlist__event2id(evlist, event, &id))
72275562573SAdrian Hunter 		return NULL;
72375562573SAdrian Hunter 
72475562573SAdrian Hunter 	/* Synthesized events have an id of zero */
72575562573SAdrian Hunter 	if (!id)
72698be6966SAdrian Hunter 		return first;
72775562573SAdrian Hunter 
72875562573SAdrian Hunter 	hash = hash_64(id, PERF_EVLIST__HLIST_BITS);
7291d5af02dSJiri Olsa 	head = &evlist->core.heads[hash];
73075562573SAdrian Hunter 
73175562573SAdrian Hunter 	hlist_for_each_entry(sid, head, node) {
73275562573SAdrian Hunter 		if (sid->id == id)
73370c20369SJiri Olsa 			return container_of(sid->evsel, struct evsel, core);
73475562573SAdrian Hunter 	}
73575562573SAdrian Hunter 	return NULL;
73675562573SAdrian Hunter }
73775562573SAdrian Hunter 
7380a60b339SArnaldo Carvalho de Melo static int evlist__set_paused(struct evlist *evlist, bool value)
73965aea233SWang Nan {
74065aea233SWang Nan 	int i;
74165aea233SWang Nan 
7420b72d69aSWang Nan 	if (!evlist->overwrite_mmap)
743078c3386SWang Nan 		return 0;
744078c3386SWang Nan 
745c976ee11SJiri Olsa 	for (i = 0; i < evlist->core.nr_mmaps; i++) {
7462cf07b29SJiri Olsa 		int fd = evlist->overwrite_mmap[i].core.fd;
74765aea233SWang Nan 		int err;
74865aea233SWang Nan 
74965aea233SWang Nan 		if (fd < 0)
75065aea233SWang Nan 			continue;
75165aea233SWang Nan 		err = ioctl(fd, PERF_EVENT_IOC_PAUSE_OUTPUT, value ? 1 : 0);
75265aea233SWang Nan 		if (err)
75365aea233SWang Nan 			return err;
75465aea233SWang Nan 	}
75565aea233SWang Nan 	return 0;
75665aea233SWang Nan }
75765aea233SWang Nan 
7580a60b339SArnaldo Carvalho de Melo static int evlist__pause(struct evlist *evlist)
75965aea233SWang Nan {
7600a60b339SArnaldo Carvalho de Melo 	return evlist__set_paused(evlist, true);
76165aea233SWang Nan }
76265aea233SWang Nan 
7630a60b339SArnaldo Carvalho de Melo static int evlist__resume(struct evlist *evlist)
76465aea233SWang Nan {
7650a60b339SArnaldo Carvalho de Melo 	return evlist__set_paused(evlist, false);
76665aea233SWang Nan }
76765aea233SWang Nan 
768db6b7b13SJiri Olsa static void evlist__munmap_nofree(struct evlist *evlist)
769f8a95309SArnaldo Carvalho de Melo {
770aece948fSArnaldo Carvalho de Melo 	int i;
771f8a95309SArnaldo Carvalho de Melo 
772b2cb615dSWang Nan 	if (evlist->mmap)
773c976ee11SJiri Olsa 		for (i = 0; i < evlist->core.nr_mmaps; i++)
77480e53d11SJiri Olsa 			perf_mmap__munmap(&evlist->mmap[i].core);
775b2cb615dSWang Nan 
7760b72d69aSWang Nan 	if (evlist->overwrite_mmap)
777c976ee11SJiri Olsa 		for (i = 0; i < evlist->core.nr_mmaps; i++)
77880e53d11SJiri Olsa 			perf_mmap__munmap(&evlist->overwrite_mmap[i].core);
779a1f72618SWang Nan }
780aece948fSArnaldo Carvalho de Melo 
781db6b7b13SJiri Olsa void evlist__munmap(struct evlist *evlist)
782a1f72618SWang Nan {
783db6b7b13SJiri Olsa 	evlist__munmap_nofree(evlist);
78404662523SArnaldo Carvalho de Melo 	zfree(&evlist->mmap);
7850b72d69aSWang Nan 	zfree(&evlist->overwrite_mmap);
786f8a95309SArnaldo Carvalho de Melo }
787f8a95309SArnaldo Carvalho de Melo 
78880e53d11SJiri Olsa static void perf_mmap__unmap_cb(struct perf_mmap *map)
78980e53d11SJiri Olsa {
79080e53d11SJiri Olsa 	struct mmap *m = container_of(map, struct mmap, core);
79180e53d11SJiri Olsa 
79280e53d11SJiri Olsa 	mmap__munmap(m);
79380e53d11SJiri Olsa }
79480e53d11SJiri Olsa 
795d50cf361SJiri Olsa static struct mmap *evlist__alloc_mmap(struct evlist *evlist,
7962c5f6d87SKan Liang 				       bool overwrite)
797f8a95309SArnaldo Carvalho de Melo {
798d4c6fb36SWang Nan 	int i;
799a5830532SJiri Olsa 	struct mmap *map;
800d4c6fb36SWang Nan 
801c976ee11SJiri Olsa 	map = zalloc(evlist->core.nr_mmaps * sizeof(struct mmap));
8028db6d6b1SWang Nan 	if (!map)
8038db6d6b1SWang Nan 		return NULL;
804946ae1d4SWang Nan 
805c976ee11SJiri Olsa 	for (i = 0; i < evlist->core.nr_mmaps; i++) {
8066eb65f7aSJiri Olsa 		struct perf_mmap *prev = i ? &map[i - 1].core : NULL;
8076eb65f7aSJiri Olsa 
8084738ca30SArnaldo Carvalho de Melo 		/*
8094738ca30SArnaldo Carvalho de Melo 		 * When the perf_mmap() call is made we grab one refcount, plus
8106afad54dSKan Liang 		 * one extra to let perf_mmap__consume() get the last
8114738ca30SArnaldo Carvalho de Melo 		 * events after all real references (perf_mmap__get()) are
8124738ca30SArnaldo Carvalho de Melo 		 * dropped.
8134738ca30SArnaldo Carvalho de Melo 		 *
8144738ca30SArnaldo Carvalho de Melo 		 * Each PERF_EVENT_IOC_SET_OUTPUT points to this mmap and
8154738ca30SArnaldo Carvalho de Melo 		 * thus does perf_mmap__get() on it.
8164738ca30SArnaldo Carvalho de Melo 		 */
8176eb65f7aSJiri Olsa 		perf_mmap__init(&map[i].core, prev, overwrite, perf_mmap__unmap_cb);
8184738ca30SArnaldo Carvalho de Melo 	}
819353120b4SJiri Olsa 
8208db6d6b1SWang Nan 	return map;
821f8a95309SArnaldo Carvalho de Melo }
822f8a95309SArnaldo Carvalho de Melo 
8239abd2ab2SJiri Olsa static void
8249abd2ab2SJiri Olsa perf_evlist__mmap_cb_idx(struct perf_evlist *_evlist,
825d01508f2SAdrian Hunter 			 struct perf_evsel *_evsel,
8269abd2ab2SJiri Olsa 			 struct perf_mmap_param *_mp,
8276a7b8a5aSAdrian Hunter 			 int idx)
8289abd2ab2SJiri Olsa {
8299abd2ab2SJiri Olsa 	struct evlist *evlist = container_of(_evlist, struct evlist, core);
8309abd2ab2SJiri Olsa 	struct mmap_params *mp = container_of(_mp, struct mmap_params, core);
831d01508f2SAdrian Hunter 	struct evsel *evsel = container_of(_evsel, struct evsel, core);
8329abd2ab2SJiri Olsa 
83384bd5abaSAdrian Hunter 	auxtrace_mmap_params__set_idx(&mp->auxtrace_mp, evlist, evsel, idx);
8349abd2ab2SJiri Olsa }
8359abd2ab2SJiri Olsa 
836bb1b1885SJiri Olsa static struct perf_mmap*
837bb1b1885SJiri Olsa perf_evlist__mmap_cb_get(struct perf_evlist *_evlist, bool overwrite, int idx)
838bb1b1885SJiri Olsa {
839bb1b1885SJiri Olsa 	struct evlist *evlist = container_of(_evlist, struct evlist, core);
8403805e4f3SJiri Olsa 	struct mmap *maps;
841bb1b1885SJiri Olsa 
8423805e4f3SJiri Olsa 	maps = overwrite ? evlist->overwrite_mmap : evlist->mmap;
843bb1b1885SJiri Olsa 
844bb1b1885SJiri Olsa 	if (!maps) {
8453805e4f3SJiri Olsa 		maps = evlist__alloc_mmap(evlist, overwrite);
846bb1b1885SJiri Olsa 		if (!maps)
847bb1b1885SJiri Olsa 			return NULL;
848bb1b1885SJiri Olsa 
8493805e4f3SJiri Olsa 		if (overwrite) {
850bb1b1885SJiri Olsa 			evlist->overwrite_mmap = maps;
851bb1b1885SJiri Olsa 			if (evlist->bkw_mmap_state == BKW_MMAP_NOTREADY)
852ade9d208SArnaldo Carvalho de Melo 				evlist__toggle_bkw_mmap(evlist, BKW_MMAP_RUNNING);
8533805e4f3SJiri Olsa 		} else {
8543805e4f3SJiri Olsa 			evlist->mmap = maps;
855bb1b1885SJiri Olsa 		}
856bb1b1885SJiri Olsa 	}
857bb1b1885SJiri Olsa 
858bb1b1885SJiri Olsa 	return &maps[idx].core;
859bb1b1885SJiri Olsa }
860bb1b1885SJiri Olsa 
861b80132b1SJiri Olsa static int
862b80132b1SJiri Olsa perf_evlist__mmap_cb_mmap(struct perf_mmap *_map, struct perf_mmap_param *_mp,
8636d18804bSIan Rogers 			  int output, struct perf_cpu cpu)
864b80132b1SJiri Olsa {
865b80132b1SJiri Olsa 	struct mmap *map = container_of(_map, struct mmap, core);
866b80132b1SJiri Olsa 	struct mmap_params *mp = container_of(_mp, struct mmap_params, core);
867b80132b1SJiri Olsa 
868b80132b1SJiri Olsa 	return mmap__mmap(map, mp, output, cpu);
869b80132b1SJiri Olsa }
870b80132b1SJiri Olsa 
871f5e7150cSArnaldo Carvalho de Melo unsigned long perf_event_mlock_kb_in_pages(void)
872994a1f78SJiri Olsa {
873f5e7150cSArnaldo Carvalho de Melo 	unsigned long pages;
8748185e881SArnaldo Carvalho de Melo 	int max;
8758185e881SArnaldo Carvalho de Melo 
8768185e881SArnaldo Carvalho de Melo 	if (sysctl__read_int("kernel/perf_event_mlock_kb", &max) < 0) {
8778185e881SArnaldo Carvalho de Melo 		/*
8788185e881SArnaldo Carvalho de Melo 		 * Pick a once upon a time good value, i.e. things look
8798185e881SArnaldo Carvalho de Melo 		 * strange since we can't read a sysctl value, but lets not
8808185e881SArnaldo Carvalho de Melo 		 * die yet...
8818185e881SArnaldo Carvalho de Melo 		 */
8828185e881SArnaldo Carvalho de Melo 		max = 512;
8838185e881SArnaldo Carvalho de Melo 	} else {
8848185e881SArnaldo Carvalho de Melo 		max -= (page_size / 1024);
8858185e881SArnaldo Carvalho de Melo 	}
8868185e881SArnaldo Carvalho de Melo 
8878185e881SArnaldo Carvalho de Melo 	pages = (max * 1024) / page_size;
8881be300f4SArnaldo Carvalho de Melo 	if (!is_power_of_2(pages))
8891be300f4SArnaldo Carvalho de Melo 		pages = rounddown_pow_of_two(pages);
890f5e7150cSArnaldo Carvalho de Melo 
891f5e7150cSArnaldo Carvalho de Melo 	return pages;
892f5e7150cSArnaldo Carvalho de Melo }
893f5e7150cSArnaldo Carvalho de Melo 
8949521b5f2SJiri Olsa size_t evlist__mmap_size(unsigned long pages)
895f5e7150cSArnaldo Carvalho de Melo {
896f5e7150cSArnaldo Carvalho de Melo 	if (pages == UINT_MAX)
897f5e7150cSArnaldo Carvalho de Melo 		pages = perf_event_mlock_kb_in_pages();
898f5e7150cSArnaldo Carvalho de Melo 	else if (!is_power_of_2(pages))
899994a1f78SJiri Olsa 		return 0;
900994a1f78SJiri Olsa 
901994a1f78SJiri Olsa 	return (pages + 1) * page_size;
902994a1f78SJiri Olsa }
903994a1f78SJiri Olsa 
90433c2dcfdSDavid Ahern static long parse_pages_arg(const char *str, unsigned long min,
90533c2dcfdSDavid Ahern 			    unsigned long max)
906994a1f78SJiri Olsa {
9072fbe4abeSAdrian Hunter 	unsigned long pages, val;
90827050f53SJiri Olsa 	static struct parse_tag tags[] = {
90927050f53SJiri Olsa 		{ .tag  = 'B', .mult = 1       },
91027050f53SJiri Olsa 		{ .tag  = 'K', .mult = 1 << 10 },
91127050f53SJiri Olsa 		{ .tag  = 'M', .mult = 1 << 20 },
91227050f53SJiri Olsa 		{ .tag  = 'G', .mult = 1 << 30 },
91327050f53SJiri Olsa 		{ .tag  = 0 },
91427050f53SJiri Olsa 	};
915994a1f78SJiri Olsa 
9168973504bSDavid Ahern 	if (str == NULL)
91733c2dcfdSDavid Ahern 		return -EINVAL;
9188973504bSDavid Ahern 
91927050f53SJiri Olsa 	val = parse_tag_value(str, tags);
9202fbe4abeSAdrian Hunter 	if (val != (unsigned long) -1) {
92127050f53SJiri Olsa 		/* we got file size value */
92227050f53SJiri Olsa 		pages = PERF_ALIGN(val, page_size) / page_size;
92327050f53SJiri Olsa 	} else {
92427050f53SJiri Olsa 		/* we got pages count value */
92527050f53SJiri Olsa 		char *eptr;
926994a1f78SJiri Olsa 		pages = strtoul(str, &eptr, 10);
92733c2dcfdSDavid Ahern 		if (*eptr != '\0')
92833c2dcfdSDavid Ahern 			return -EINVAL;
92927050f53SJiri Olsa 	}
930994a1f78SJiri Olsa 
9312bcab6c1SAdrian Hunter 	if (pages == 0 && min == 0) {
93233c2dcfdSDavid Ahern 		/* leave number of pages at 0 */
9331dbfa938SAdrian Hunter 	} else if (!is_power_of_2(pages)) {
9349808143bSJiri Olsa 		char buf[100];
9359808143bSJiri Olsa 
93633c2dcfdSDavid Ahern 		/* round pages up to next power of 2 */
93791529834SArnaldo Carvalho de Melo 		pages = roundup_pow_of_two(pages);
9381dbfa938SAdrian Hunter 		if (!pages)
9391dbfa938SAdrian Hunter 			return -EINVAL;
9409808143bSJiri Olsa 
9419808143bSJiri Olsa 		unit_number__scnprintf(buf, sizeof(buf), pages * page_size);
9429808143bSJiri Olsa 		pr_info("rounding mmap pages size to %s (%lu pages)\n",
9439808143bSJiri Olsa 			buf, pages);
9442fbe4abeSAdrian Hunter 	}
9452fbe4abeSAdrian Hunter 
94633c2dcfdSDavid Ahern 	if (pages > max)
94733c2dcfdSDavid Ahern 		return -EINVAL;
94833c2dcfdSDavid Ahern 
94933c2dcfdSDavid Ahern 	return pages;
95033c2dcfdSDavid Ahern }
95133c2dcfdSDavid Ahern 
95225f84702SArnaldo Carvalho de Melo int __evlist__parse_mmap_pages(unsigned int *mmap_pages, const char *str)
95333c2dcfdSDavid Ahern {
95433c2dcfdSDavid Ahern 	unsigned long max = UINT_MAX;
95533c2dcfdSDavid Ahern 	long pages;
95633c2dcfdSDavid Ahern 
957f5ae9c42SAdrian Hunter 	if (max > SIZE_MAX / page_size)
95833c2dcfdSDavid Ahern 		max = SIZE_MAX / page_size;
95933c2dcfdSDavid Ahern 
96033c2dcfdSDavid Ahern 	pages = parse_pages_arg(str, 1, max);
96133c2dcfdSDavid Ahern 	if (pages < 0) {
96233c2dcfdSDavid Ahern 		pr_err("Invalid argument for --mmap_pages/-m\n");
963994a1f78SJiri Olsa 		return -1;
964994a1f78SJiri Olsa 	}
965994a1f78SJiri Olsa 
966994a1f78SJiri Olsa 	*mmap_pages = pages;
967994a1f78SJiri Olsa 	return 0;
968994a1f78SJiri Olsa }
969994a1f78SJiri Olsa 
97025f84702SArnaldo Carvalho de Melo int evlist__parse_mmap_pages(const struct option *opt, const char *str, int unset __maybe_unused)
971e9db1310SAdrian Hunter {
97225f84702SArnaldo Carvalho de Melo 	return __evlist__parse_mmap_pages(opt->value, str);
973e9db1310SAdrian Hunter }
974e9db1310SAdrian Hunter 
975c83fa7f2SAdrian Hunter /**
9769521b5f2SJiri Olsa  * evlist__mmap_ex - Create mmaps to receive events.
977c83fa7f2SAdrian Hunter  * @evlist: list of events
978c83fa7f2SAdrian Hunter  * @pages: map length in pages
979c83fa7f2SAdrian Hunter  * @overwrite: overwrite older events?
980718c602dSAdrian Hunter  * @auxtrace_pages - auxtrace map length in pages
981718c602dSAdrian Hunter  * @auxtrace_overwrite - overwrite older auxtrace data?
982f8a95309SArnaldo Carvalho de Melo  *
983c83fa7f2SAdrian Hunter  * If @overwrite is %false the user needs to signal event consumption using
9849521b5f2SJiri Olsa  * perf_mmap__write_tail().  Using evlist__mmap_read() does this
985c83fa7f2SAdrian Hunter  * automatically.
986f8a95309SArnaldo Carvalho de Melo  *
987718c602dSAdrian Hunter  * Similarly, if @auxtrace_overwrite is %false the user needs to signal data
988718c602dSAdrian Hunter  * consumption using auxtrace_mmap__write_tail().
989718c602dSAdrian Hunter  *
990c83fa7f2SAdrian Hunter  * Return: %0 on success, negative error code otherwise.
991f8a95309SArnaldo Carvalho de Melo  */
9929521b5f2SJiri Olsa int evlist__mmap_ex(struct evlist *evlist, unsigned int pages,
9937a276ff6SWang Nan 			 unsigned int auxtrace_pages,
99451255a8aSAlexey Budankov 			 bool auxtrace_overwrite, int nr_cblocks, int affinity, int flush,
99551255a8aSAlexey Budankov 			 int comp_level)
996f8a95309SArnaldo Carvalho de Melo {
99771f566a3SWang Nan 	/*
99871f566a3SWang Nan 	 * Delay setting mp.prot: set it before calling perf_mmap__mmap.
99971f566a3SWang Nan 	 * Its value is decided by evsel's write_backward.
100071f566a3SWang Nan 	 * So &mp should not be passed through const pointer.
100171f566a3SWang Nan 	 */
1002e440979fSJiri Olsa 	struct mmap_params mp = {
1003e440979fSJiri Olsa 		.nr_cblocks	= nr_cblocks,
1004e440979fSJiri Olsa 		.affinity	= affinity,
1005e440979fSJiri Olsa 		.flush		= flush,
1006e440979fSJiri Olsa 		.comp_level	= comp_level
1007e440979fSJiri Olsa 	};
1008923d0f18SJiri Olsa 	struct perf_evlist_mmap_ops ops = {
10099abd2ab2SJiri Olsa 		.idx  = perf_evlist__mmap_cb_idx,
1010bb1b1885SJiri Olsa 		.get  = perf_evlist__mmap_cb_get,
1011b80132b1SJiri Olsa 		.mmap = perf_evlist__mmap_cb_mmap,
10129abd2ab2SJiri Olsa 	};
101350a682ceSArnaldo Carvalho de Melo 
1014f6fa4375SJiri Olsa 	evlist->core.mmap_len = evlist__mmap_size(pages);
1015f6fa4375SJiri Olsa 	pr_debug("mmap size %zuB\n", evlist->core.mmap_len);
1016f8a95309SArnaldo Carvalho de Melo 
1017f6fa4375SJiri Olsa 	auxtrace_mmap_params__init(&mp.auxtrace_mp, evlist->core.mmap_len,
1018718c602dSAdrian Hunter 				   auxtrace_pages, auxtrace_overwrite);
1019718c602dSAdrian Hunter 
1020923d0f18SJiri Olsa 	return perf_evlist__mmap_ops(&evlist->core, &ops, &mp.core);
1021f8a95309SArnaldo Carvalho de Melo }
10227e2ed097SArnaldo Carvalho de Melo 
10239521b5f2SJiri Olsa int evlist__mmap(struct evlist *evlist, unsigned int pages)
1024718c602dSAdrian Hunter {
10259521b5f2SJiri Olsa 	return evlist__mmap_ex(evlist, pages, 0, false, 0, PERF_AFFINITY_SYS, 1, 0);
1026718c602dSAdrian Hunter }
1027718c602dSAdrian Hunter 
10287748bb71SArnaldo Carvalho de Melo int evlist__create_maps(struct evlist *evlist, struct target *target)
10297e2ed097SArnaldo Carvalho de Melo {
1030147c508fSJin Yao 	bool all_threads = (target->per_thread && target->system_wide);
1031f854839bSJiri Olsa 	struct perf_cpu_map *cpus;
10329749b90eSJiri Olsa 	struct perf_thread_map *threads;
10337e2ed097SArnaldo Carvalho de Melo 
1034147c508fSJin Yao 	/*
1035147c508fSJin Yao 	 * If specify '-a' and '--per-thread' to perf record, perf record
1036147c508fSJin Yao 	 * will override '--per-thread'. target->per_thread = false and
1037147c508fSJin Yao 	 * target->system_wide = true.
1038147c508fSJin Yao 	 *
1039147c508fSJin Yao 	 * If specify '--per-thread' only to perf record,
1040147c508fSJin Yao 	 * target->per_thread = true and target->system_wide = false.
1041147c508fSJin Yao 	 *
1042147c508fSJin Yao 	 * So target->per_thread && target->system_wide is false.
1043147c508fSJin Yao 	 * For perf record, thread_map__new_str doesn't call
1044147c508fSJin Yao 	 * thread_map__new_all_cpus. That will keep perf record's
1045147c508fSJin Yao 	 * current behavior.
1046147c508fSJin Yao 	 *
1047147c508fSJin Yao 	 * For perf stat, it allows the case that target->per_thread and
1048147c508fSJin Yao 	 * target->system_wide are all true. It means to collect system-wide
1049147c508fSJin Yao 	 * per-thread data. thread_map__new_str will call
1050147c508fSJin Yao 	 * thread_map__new_all_cpus to enumerate all threads.
1051147c508fSJin Yao 	 */
105273c0ca1eSJin Yao 	threads = thread_map__new_str(target->pid, target->tid, target->uid,
1053147c508fSJin Yao 				      all_threads);
105474bfd2b2SAdrian Hunter 
105574bfd2b2SAdrian Hunter 	if (!threads)
10567e2ed097SArnaldo Carvalho de Melo 		return -1;
10577e2ed097SArnaldo Carvalho de Melo 
10589c105fbcSDongsheng Yang 	if (target__uses_dummy_map(target))
1059397721e0SJiri Olsa 		cpus = perf_cpu_map__dummy_new();
1060879d77d0SNamhyung Kim 	else
10619c3516d1SJiri Olsa 		cpus = perf_cpu_map__new(target->cpu_list);
10627e2ed097SArnaldo Carvalho de Melo 
106374bfd2b2SAdrian Hunter 	if (!cpus)
10647e2ed097SArnaldo Carvalho de Melo 		goto out_delete_threads;
10657e2ed097SArnaldo Carvalho de Melo 
1066b726e363SJin Yao 	evlist->core.has_user_cpus = !!target->cpu_list && !target->hybrid;
1067ec9a77a7SAdrian Hunter 
1068453fa030SJiri Olsa 	perf_evlist__set_maps(&evlist->core, cpus, threads);
1069d5bc056eSAdrian Hunter 
1070bfd1b83dSNamhyung Kim 	/* as evlist now has references, put count here */
1071bfd1b83dSNamhyung Kim 	perf_cpu_map__put(cpus);
1072bfd1b83dSNamhyung Kim 	perf_thread_map__put(threads);
1073bfd1b83dSNamhyung Kim 
1074d5bc056eSAdrian Hunter 	return 0;
10757e2ed097SArnaldo Carvalho de Melo 
10767e2ed097SArnaldo Carvalho de Melo out_delete_threads:
10777836e52eSJiri Olsa 	perf_thread_map__put(threads);
10787e2ed097SArnaldo Carvalho de Melo 	return -1;
10797e2ed097SArnaldo Carvalho de Melo }
10807e2ed097SArnaldo Carvalho de Melo 
108124bf91a7SArnaldo Carvalho de Melo int evlist__apply_filters(struct evlist *evlist, struct evsel **err_evsel)
10820a102479SFrederic Weisbecker {
108332dcd021SJiri Olsa 	struct evsel *evsel;
1084745cefc5SArnaldo Carvalho de Melo 	int err = 0;
10850a102479SFrederic Weisbecker 
1086e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, evsel) {
1087745cefc5SArnaldo Carvalho de Melo 		if (evsel->filter == NULL)
10880a102479SFrederic Weisbecker 			continue;
1089745cefc5SArnaldo Carvalho de Melo 
1090d988d5eeSKan Liang 		/*
1091d988d5eeSKan Liang 		 * filters only work for tracepoint event, which doesn't have cpu limit.
1092d988d5eeSKan Liang 		 * So evlist and evsel should always be same.
1093d988d5eeSKan Liang 		 */
1094a00571fdSJiri Olsa 		err = perf_evsel__apply_filter(&evsel->core, evsel->filter);
109523d4aad4SArnaldo Carvalho de Melo 		if (err) {
109623d4aad4SArnaldo Carvalho de Melo 			*err_evsel = evsel;
1097745cefc5SArnaldo Carvalho de Melo 			break;
10980a102479SFrederic Weisbecker 		}
109923d4aad4SArnaldo Carvalho de Melo 	}
11000a102479SFrederic Weisbecker 
1101745cefc5SArnaldo Carvalho de Melo 	return err;
1102745cefc5SArnaldo Carvalho de Melo }
1103745cefc5SArnaldo Carvalho de Melo 
110424bf91a7SArnaldo Carvalho de Melo int evlist__set_tp_filter(struct evlist *evlist, const char *filter)
1105745cefc5SArnaldo Carvalho de Melo {
110632dcd021SJiri Olsa 	struct evsel *evsel;
1107745cefc5SArnaldo Carvalho de Melo 	int err = 0;
1108745cefc5SArnaldo Carvalho de Melo 
110905cea449SArnaldo Carvalho de Melo 	if (filter == NULL)
111005cea449SArnaldo Carvalho de Melo 		return -1;
111105cea449SArnaldo Carvalho de Melo 
1112e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, evsel) {
11131fc632ceSJiri Olsa 		if (evsel->core.attr.type != PERF_TYPE_TRACEPOINT)
1114fdf14720SWang Nan 			continue;
1115fdf14720SWang Nan 
1116ad681adfSArnaldo Carvalho de Melo 		err = evsel__set_filter(evsel, filter);
1117745cefc5SArnaldo Carvalho de Melo 		if (err)
1118745cefc5SArnaldo Carvalho de Melo 			break;
1119745cefc5SArnaldo Carvalho de Melo 	}
1120745cefc5SArnaldo Carvalho de Melo 
1121745cefc5SArnaldo Carvalho de Melo 	return err;
11220a102479SFrederic Weisbecker }
112374429964SFrederic Weisbecker 
112424bf91a7SArnaldo Carvalho de Melo int evlist__append_tp_filter(struct evlist *evlist, const char *filter)
112553c92f73SArnaldo Carvalho de Melo {
112653c92f73SArnaldo Carvalho de Melo 	struct evsel *evsel;
112753c92f73SArnaldo Carvalho de Melo 	int err = 0;
112853c92f73SArnaldo Carvalho de Melo 
112953c92f73SArnaldo Carvalho de Melo 	if (filter == NULL)
113053c92f73SArnaldo Carvalho de Melo 		return -1;
113153c92f73SArnaldo Carvalho de Melo 
113253c92f73SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, evsel) {
113353c92f73SArnaldo Carvalho de Melo 		if (evsel->core.attr.type != PERF_TYPE_TRACEPOINT)
113453c92f73SArnaldo Carvalho de Melo 			continue;
113553c92f73SArnaldo Carvalho de Melo 
1136ad681adfSArnaldo Carvalho de Melo 		err = evsel__append_tp_filter(evsel, filter);
113753c92f73SArnaldo Carvalho de Melo 		if (err)
113853c92f73SArnaldo Carvalho de Melo 			break;
113953c92f73SArnaldo Carvalho de Melo 	}
114053c92f73SArnaldo Carvalho de Melo 
114153c92f73SArnaldo Carvalho de Melo 	return err;
114253c92f73SArnaldo Carvalho de Melo }
114353c92f73SArnaldo Carvalho de Melo 
1144da949f50SArnaldo Carvalho de Melo char *asprintf__tp_filter_pids(size_t npids, pid_t *pids)
1145cfd70a26SArnaldo Carvalho de Melo {
1146cfd70a26SArnaldo Carvalho de Melo 	char *filter;
1147be199adaSArnaldo Carvalho de Melo 	size_t i;
1148cfd70a26SArnaldo Carvalho de Melo 
1149be199adaSArnaldo Carvalho de Melo 	for (i = 0; i < npids; ++i) {
1150be199adaSArnaldo Carvalho de Melo 		if (i == 0) {
1151be199adaSArnaldo Carvalho de Melo 			if (asprintf(&filter, "common_pid != %d", pids[i]) < 0)
115205cea449SArnaldo Carvalho de Melo 				return NULL;
1153be199adaSArnaldo Carvalho de Melo 		} else {
1154be199adaSArnaldo Carvalho de Melo 			char *tmp;
1155be199adaSArnaldo Carvalho de Melo 
1156be199adaSArnaldo Carvalho de Melo 			if (asprintf(&tmp, "%s && common_pid != %d", filter, pids[i]) < 0)
1157be199adaSArnaldo Carvalho de Melo 				goto out_free;
1158be199adaSArnaldo Carvalho de Melo 
1159be199adaSArnaldo Carvalho de Melo 			free(filter);
1160be199adaSArnaldo Carvalho de Melo 			filter = tmp;
1161be199adaSArnaldo Carvalho de Melo 		}
1162be199adaSArnaldo Carvalho de Melo 	}
1163cfd70a26SArnaldo Carvalho de Melo 
116405cea449SArnaldo Carvalho de Melo 	return filter;
1165be199adaSArnaldo Carvalho de Melo out_free:
1166cfd70a26SArnaldo Carvalho de Melo 	free(filter);
116705cea449SArnaldo Carvalho de Melo 	return NULL;
116805cea449SArnaldo Carvalho de Melo }
116905cea449SArnaldo Carvalho de Melo 
117024bf91a7SArnaldo Carvalho de Melo int evlist__set_tp_filter_pids(struct evlist *evlist, size_t npids, pid_t *pids)
117105cea449SArnaldo Carvalho de Melo {
117205cea449SArnaldo Carvalho de Melo 	char *filter = asprintf__tp_filter_pids(npids, pids);
117324bf91a7SArnaldo Carvalho de Melo 	int ret = evlist__set_tp_filter(evlist, filter);
117405cea449SArnaldo Carvalho de Melo 
117505cea449SArnaldo Carvalho de Melo 	free(filter);
1176cfd70a26SArnaldo Carvalho de Melo 	return ret;
1177cfd70a26SArnaldo Carvalho de Melo }
1178cfd70a26SArnaldo Carvalho de Melo 
117924bf91a7SArnaldo Carvalho de Melo int evlist__set_tp_filter_pid(struct evlist *evlist, pid_t pid)
1180be199adaSArnaldo Carvalho de Melo {
118124bf91a7SArnaldo Carvalho de Melo 	return evlist__set_tp_filter_pids(evlist, 1, &pid);
1182be199adaSArnaldo Carvalho de Melo }
1183be199adaSArnaldo Carvalho de Melo 
118424bf91a7SArnaldo Carvalho de Melo int evlist__append_tp_filter_pids(struct evlist *evlist, size_t npids, pid_t *pids)
11851827ab5bSArnaldo Carvalho de Melo {
11861827ab5bSArnaldo Carvalho de Melo 	char *filter = asprintf__tp_filter_pids(npids, pids);
118724bf91a7SArnaldo Carvalho de Melo 	int ret = evlist__append_tp_filter(evlist, filter);
11881827ab5bSArnaldo Carvalho de Melo 
11891827ab5bSArnaldo Carvalho de Melo 	free(filter);
11901827ab5bSArnaldo Carvalho de Melo 	return ret;
11911827ab5bSArnaldo Carvalho de Melo }
11921827ab5bSArnaldo Carvalho de Melo 
119324bf91a7SArnaldo Carvalho de Melo int evlist__append_tp_filter_pid(struct evlist *evlist, pid_t pid)
11941827ab5bSArnaldo Carvalho de Melo {
119524bf91a7SArnaldo Carvalho de Melo 	return evlist__append_tp_filter_pids(evlist, 1, &pid);
11961827ab5bSArnaldo Carvalho de Melo }
11971827ab5bSArnaldo Carvalho de Melo 
1198b3c2cc2bSArnaldo Carvalho de Melo bool evlist__valid_sample_type(struct evlist *evlist)
119974429964SFrederic Weisbecker {
120032dcd021SJiri Olsa 	struct evsel *pos;
1201c2a70653SArnaldo Carvalho de Melo 
12026484d2f9SJiri Olsa 	if (evlist->core.nr_entries == 1)
120375562573SAdrian Hunter 		return true;
120475562573SAdrian Hunter 
120575562573SAdrian Hunter 	if (evlist->id_pos < 0 || evlist->is_pos < 0)
120675562573SAdrian Hunter 		return false;
120775562573SAdrian Hunter 
1208e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, pos) {
120975562573SAdrian Hunter 		if (pos->id_pos != evlist->id_pos ||
121075562573SAdrian Hunter 		    pos->is_pos != evlist->is_pos)
1211c2a70653SArnaldo Carvalho de Melo 			return false;
121274429964SFrederic Weisbecker 	}
121374429964SFrederic Weisbecker 
1214c2a70653SArnaldo Carvalho de Melo 	return true;
1215c2a70653SArnaldo Carvalho de Melo }
1216c2a70653SArnaldo Carvalho de Melo 
1217b3c2cc2bSArnaldo Carvalho de Melo u64 __evlist__combined_sample_type(struct evlist *evlist)
1218c2a70653SArnaldo Carvalho de Melo {
121932dcd021SJiri Olsa 	struct evsel *evsel;
122075562573SAdrian Hunter 
122175562573SAdrian Hunter 	if (evlist->combined_sample_type)
122275562573SAdrian Hunter 		return evlist->combined_sample_type;
122375562573SAdrian Hunter 
1224e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, evsel)
12251fc632ceSJiri Olsa 		evlist->combined_sample_type |= evsel->core.attr.sample_type;
122675562573SAdrian Hunter 
122775562573SAdrian Hunter 	return evlist->combined_sample_type;
122875562573SAdrian Hunter }
122975562573SAdrian Hunter 
1230b3c2cc2bSArnaldo Carvalho de Melo u64 evlist__combined_sample_type(struct evlist *evlist)
123175562573SAdrian Hunter {
123275562573SAdrian Hunter 	evlist->combined_sample_type = 0;
1233b3c2cc2bSArnaldo Carvalho de Melo 	return __evlist__combined_sample_type(evlist);
1234c2a70653SArnaldo Carvalho de Melo }
1235c2a70653SArnaldo Carvalho de Melo 
123692c7d7cdSArnaldo Carvalho de Melo u64 evlist__combined_branch_type(struct evlist *evlist)
123798df858eSAndi Kleen {
123832dcd021SJiri Olsa 	struct evsel *evsel;
123998df858eSAndi Kleen 	u64 branch_type = 0;
124098df858eSAndi Kleen 
1241e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, evsel)
12421fc632ceSJiri Olsa 		branch_type |= evsel->core.attr.branch_sample_type;
124398df858eSAndi Kleen 	return branch_type;
124498df858eSAndi Kleen }
124598df858eSAndi Kleen 
124678e1bc25SArnaldo Carvalho de Melo bool evlist__valid_read_format(struct evlist *evlist)
12479ede473cSJiri Olsa {
1248515dbe48SJiri Olsa 	struct evsel *first = evlist__first(evlist), *pos = first;
12491fc632ceSJiri Olsa 	u64 read_format = first->core.attr.read_format;
12501fc632ceSJiri Olsa 	u64 sample_type = first->core.attr.sample_type;
12519ede473cSJiri Olsa 
1252e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, pos) {
125394d3820fSAdrian Hunter 		if (read_format != pos->core.attr.read_format) {
125494d3820fSAdrian Hunter 			pr_debug("Read format differs %#" PRIx64 " vs %#" PRIx64 "\n",
125594d3820fSAdrian Hunter 				 read_format, (u64)pos->core.attr.read_format);
125694d3820fSAdrian Hunter 		}
12579ede473cSJiri Olsa 	}
12589ede473cSJiri Olsa 
12594d39c89fSIngo Molnar 	/* PERF_SAMPLE_READ implies PERF_FORMAT_ID. */
12609ede473cSJiri Olsa 	if ((sample_type & PERF_SAMPLE_READ) &&
12619ede473cSJiri Olsa 	    !(read_format & PERF_FORMAT_ID)) {
12629ede473cSJiri Olsa 		return false;
12639ede473cSJiri Olsa 	}
12649ede473cSJiri Olsa 
12659ede473cSJiri Olsa 	return true;
12669ede473cSJiri Olsa }
12679ede473cSJiri Olsa 
12681420ba2fSArnaldo Carvalho de Melo u16 evlist__id_hdr_size(struct evlist *evlist)
126981e36bffSArnaldo Carvalho de Melo {
1270515dbe48SJiri Olsa 	struct evsel *first = evlist__first(evlist);
127181e36bffSArnaldo Carvalho de Melo 
12720a64de04SAdrian Hunter 	return first->core.attr.sample_id_all ? evsel__id_hdr_size(first) : 0;
127381e36bffSArnaldo Carvalho de Melo }
127481e36bffSArnaldo Carvalho de Melo 
12758cedf3a5SArnaldo Carvalho de Melo bool evlist__valid_sample_id_all(struct evlist *evlist)
1276c2a70653SArnaldo Carvalho de Melo {
1277515dbe48SJiri Olsa 	struct evsel *first = evlist__first(evlist), *pos = first;
1278c2a70653SArnaldo Carvalho de Melo 
1279e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry_continue(evlist, pos) {
12801fc632ceSJiri Olsa 		if (first->core.attr.sample_id_all != pos->core.attr.sample_id_all)
1281c2a70653SArnaldo Carvalho de Melo 			return false;
1282c2a70653SArnaldo Carvalho de Melo 	}
1283c2a70653SArnaldo Carvalho de Melo 
1284c2a70653SArnaldo Carvalho de Melo 	return true;
128574429964SFrederic Weisbecker }
128674429964SFrederic Weisbecker 
12878cedf3a5SArnaldo Carvalho de Melo bool evlist__sample_id_all(struct evlist *evlist)
128874429964SFrederic Weisbecker {
1289515dbe48SJiri Olsa 	struct evsel *first = evlist__first(evlist);
12901fc632ceSJiri Olsa 	return first->core.attr.sample_id_all;
129174429964SFrederic Weisbecker }
129281cce8deSArnaldo Carvalho de Melo 
1293900c8eadSArnaldo Carvalho de Melo void evlist__set_selected(struct evlist *evlist, struct evsel *evsel)
129481cce8deSArnaldo Carvalho de Melo {
129581cce8deSArnaldo Carvalho de Melo 	evlist->selected = evsel;
129681cce8deSArnaldo Carvalho de Melo }
1297727ab04eSArnaldo Carvalho de Melo 
1298750b4edeSJiri Olsa void evlist__close(struct evlist *evlist)
1299a74b4b66SNamhyung Kim {
130032dcd021SJiri Olsa 	struct evsel *evsel;
1301472832d2SIan Rogers 	struct evlist_cpu_iterator evlist_cpu_itr;
13027736627bSAndi Kleen 	struct affinity affinity;
1303a74b4b66SNamhyung Kim 
13047736627bSAndi Kleen 	/*
13050df6ade7SIan Rogers 	 * With perf record core.user_requested_cpus is usually NULL.
13067736627bSAndi Kleen 	 * Use the old method to handle this for now.
13077736627bSAndi Kleen 	 */
13080df6ade7SIan Rogers 	if (!evlist->core.user_requested_cpus ||
13090df6ade7SIan Rogers 	    cpu_map__is_dummy(evlist->core.user_requested_cpus)) {
1310475fb533SAndi Kleen 		evlist__for_each_entry_reverse(evlist, evsel)
131188761fa1SJiri Olsa 			evsel__close(evsel);
13127736627bSAndi Kleen 		return;
13137736627bSAndi Kleen 	}
13147736627bSAndi Kleen 
13157736627bSAndi Kleen 	if (affinity__setup(&affinity) < 0)
13167736627bSAndi Kleen 		return;
13177736627bSAndi Kleen 
1318472832d2SIan Rogers 	evlist__for_each_cpu(evlist_cpu_itr, evlist, &affinity) {
1319472832d2SIan Rogers 		perf_evsel__close_cpu(&evlist_cpu_itr.evsel->core,
1320472832d2SIan Rogers 				      evlist_cpu_itr.cpu_map_idx);
13217736627bSAndi Kleen 	}
1322472832d2SIan Rogers 
13237736627bSAndi Kleen 	affinity__cleanup(&affinity);
13247736627bSAndi Kleen 	evlist__for_each_entry_reverse(evlist, evsel) {
13257736627bSAndi Kleen 		perf_evsel__free_fd(&evsel->core);
13267736627bSAndi Kleen 		perf_evsel__free_id(&evsel->core);
13277736627bSAndi Kleen 	}
1328513068f2SNamhyung Kim 	perf_evlist__reset_id_hash(&evlist->core);
1329a74b4b66SNamhyung Kim }
1330a74b4b66SNamhyung Kim 
13317748bb71SArnaldo Carvalho de Melo static int evlist__create_syswide_maps(struct evlist *evlist)
13324112eb18SArnaldo Carvalho de Melo {
1333f854839bSJiri Olsa 	struct perf_cpu_map *cpus;
13349749b90eSJiri Olsa 	struct perf_thread_map *threads;
13354112eb18SArnaldo Carvalho de Melo 
13364112eb18SArnaldo Carvalho de Melo 	/*
13374112eb18SArnaldo Carvalho de Melo 	 * Try reading /sys/devices/system/cpu/online to get
13384112eb18SArnaldo Carvalho de Melo 	 * an all cpus map.
13394112eb18SArnaldo Carvalho de Melo 	 *
13404112eb18SArnaldo Carvalho de Melo 	 * FIXME: -ENOMEM is the best we can do here, the cpu_map
13414112eb18SArnaldo Carvalho de Melo 	 * code needs an overhaul to properly forward the
13424112eb18SArnaldo Carvalho de Melo 	 * error, and we may not want to do that fallback to a
13434112eb18SArnaldo Carvalho de Melo 	 * default cpu identity map :-\
13444112eb18SArnaldo Carvalho de Melo 	 */
13459c3516d1SJiri Olsa 	cpus = perf_cpu_map__new(NULL);
13468c0498b6SAdrian Hunter 	if (!cpus)
13474112eb18SArnaldo Carvalho de Melo 		goto out;
13484112eb18SArnaldo Carvalho de Melo 
13494b49cce2SJiri Olsa 	threads = perf_thread_map__new_dummy();
13508c0498b6SAdrian Hunter 	if (!threads)
13518c0498b6SAdrian Hunter 		goto out_put;
13524112eb18SArnaldo Carvalho de Melo 
1353453fa030SJiri Olsa 	perf_evlist__set_maps(&evlist->core, cpus, threads);
1354bfd1b83dSNamhyung Kim 
1355bfd1b83dSNamhyung Kim 	perf_thread_map__put(threads);
13568c0498b6SAdrian Hunter out_put:
135738f01d8dSJiri Olsa 	perf_cpu_map__put(cpus);
1358bfd1b83dSNamhyung Kim out:
1359f717d89aSHaowen Bai 	return -ENOMEM;
13604112eb18SArnaldo Carvalho de Melo }
13614112eb18SArnaldo Carvalho de Melo 
1362474ddc4cSJiri Olsa int evlist__open(struct evlist *evlist)
1363727ab04eSArnaldo Carvalho de Melo {
136432dcd021SJiri Olsa 	struct evsel *evsel;
1365a74b4b66SNamhyung Kim 	int err;
1366727ab04eSArnaldo Carvalho de Melo 
13674112eb18SArnaldo Carvalho de Melo 	/*
13684112eb18SArnaldo Carvalho de Melo 	 * Default: one fd per CPU, all threads, aka systemwide
13694112eb18SArnaldo Carvalho de Melo 	 * as sys_perf_event_open(cpu = -1, thread = -1) is EINVAL
13704112eb18SArnaldo Carvalho de Melo 	 */
13710df6ade7SIan Rogers 	if (evlist->core.threads == NULL && evlist->core.user_requested_cpus == NULL) {
13727748bb71SArnaldo Carvalho de Melo 		err = evlist__create_syswide_maps(evlist);
13734112eb18SArnaldo Carvalho de Melo 		if (err < 0)
13744112eb18SArnaldo Carvalho de Melo 			goto out_err;
13754112eb18SArnaldo Carvalho de Melo 	}
13764112eb18SArnaldo Carvalho de Melo 
13770a7e7ec9SArnaldo Carvalho de Melo 	evlist__update_id_pos(evlist);
1378733cd2feSAdrian Hunter 
1379e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, evsel) {
1380af663bd0SJiri Olsa 		err = evsel__open(evsel, evsel->core.cpus, evsel->core.threads);
1381727ab04eSArnaldo Carvalho de Melo 		if (err < 0)
1382727ab04eSArnaldo Carvalho de Melo 			goto out_err;
1383727ab04eSArnaldo Carvalho de Melo 	}
1384727ab04eSArnaldo Carvalho de Melo 
1385727ab04eSArnaldo Carvalho de Melo 	return 0;
1386727ab04eSArnaldo Carvalho de Melo out_err:
1387750b4edeSJiri Olsa 	evlist__close(evlist);
138841c21a68SNamhyung Kim 	errno = -err;
1389727ab04eSArnaldo Carvalho de Melo 	return err;
1390727ab04eSArnaldo Carvalho de Melo }
139135b9d88eSArnaldo Carvalho de Melo 
13927b392ef0SArnaldo Carvalho de Melo int evlist__prepare_workload(struct evlist *evlist, struct target *target, const char *argv[],
13937b392ef0SArnaldo Carvalho de Melo 			     bool pipe_output, void (*exec_error)(int signo, siginfo_t *info, void *ucontext))
139435b9d88eSArnaldo Carvalho de Melo {
139535b9d88eSArnaldo Carvalho de Melo 	int child_ready_pipe[2], go_pipe[2];
139635b9d88eSArnaldo Carvalho de Melo 	char bf;
139735b9d88eSArnaldo Carvalho de Melo 
139835b9d88eSArnaldo Carvalho de Melo 	if (pipe(child_ready_pipe) < 0) {
139935b9d88eSArnaldo Carvalho de Melo 		perror("failed to create 'ready' pipe");
140035b9d88eSArnaldo Carvalho de Melo 		return -1;
140135b9d88eSArnaldo Carvalho de Melo 	}
140235b9d88eSArnaldo Carvalho de Melo 
140335b9d88eSArnaldo Carvalho de Melo 	if (pipe(go_pipe) < 0) {
140435b9d88eSArnaldo Carvalho de Melo 		perror("failed to create 'go' pipe");
140535b9d88eSArnaldo Carvalho de Melo 		goto out_close_ready_pipe;
140635b9d88eSArnaldo Carvalho de Melo 	}
140735b9d88eSArnaldo Carvalho de Melo 
140835b9d88eSArnaldo Carvalho de Melo 	evlist->workload.pid = fork();
140935b9d88eSArnaldo Carvalho de Melo 	if (evlist->workload.pid < 0) {
141035b9d88eSArnaldo Carvalho de Melo 		perror("failed to fork");
141135b9d88eSArnaldo Carvalho de Melo 		goto out_close_pipes;
141235b9d88eSArnaldo Carvalho de Melo 	}
141335b9d88eSArnaldo Carvalho de Melo 
141435b9d88eSArnaldo Carvalho de Melo 	if (!evlist->workload.pid) {
14155f1c4225SArnaldo Carvalho de Melo 		int ret;
14165f1c4225SArnaldo Carvalho de Melo 
1417119fa3c9SNamhyung Kim 		if (pipe_output)
141835b9d88eSArnaldo Carvalho de Melo 			dup2(2, 1);
141935b9d88eSArnaldo Carvalho de Melo 
14200817df08SDavid Ahern 		signal(SIGTERM, SIG_DFL);
14210817df08SDavid Ahern 
142235b9d88eSArnaldo Carvalho de Melo 		close(child_ready_pipe[0]);
142335b9d88eSArnaldo Carvalho de Melo 		close(go_pipe[1]);
142435b9d88eSArnaldo Carvalho de Melo 		fcntl(go_pipe[0], F_SETFD, FD_CLOEXEC);
142535b9d88eSArnaldo Carvalho de Melo 
142635b9d88eSArnaldo Carvalho de Melo 		/*
1427a7672d1dSArnaldo Carvalho de Melo 		 * Change the name of this process not to confuse --exclude-perf users
1428a7672d1dSArnaldo Carvalho de Melo 		 * that sees 'perf' in the window up to the execvp() and thinks that
1429a7672d1dSArnaldo Carvalho de Melo 		 * perf samples are not being excluded.
1430a7672d1dSArnaldo Carvalho de Melo 		 */
1431a7672d1dSArnaldo Carvalho de Melo 		prctl(PR_SET_NAME, "perf-exec");
1432a7672d1dSArnaldo Carvalho de Melo 
1433a7672d1dSArnaldo Carvalho de Melo 		/*
143435b9d88eSArnaldo Carvalho de Melo 		 * Tell the parent we're ready to go
143535b9d88eSArnaldo Carvalho de Melo 		 */
143635b9d88eSArnaldo Carvalho de Melo 		close(child_ready_pipe[1]);
143735b9d88eSArnaldo Carvalho de Melo 
143835b9d88eSArnaldo Carvalho de Melo 		/*
143935b9d88eSArnaldo Carvalho de Melo 		 * Wait until the parent tells us to go.
144035b9d88eSArnaldo Carvalho de Melo 		 */
14415f1c4225SArnaldo Carvalho de Melo 		ret = read(go_pipe[0], &bf, 1);
14425f1c4225SArnaldo Carvalho de Melo 		/*
14435f1c4225SArnaldo Carvalho de Melo 		 * The parent will ask for the execvp() to be performed by
14445f1c4225SArnaldo Carvalho de Melo 		 * writing exactly one byte, in workload.cork_fd, usually via
14457b392ef0SArnaldo Carvalho de Melo 		 * evlist__start_workload().
14465f1c4225SArnaldo Carvalho de Melo 		 *
144720f86fc1SArnaldo Carvalho de Melo 		 * For cancelling the workload without actually running it,
14485f1c4225SArnaldo Carvalho de Melo 		 * the parent will just close workload.cork_fd, without writing
14495f1c4225SArnaldo Carvalho de Melo 		 * anything, i.e. read will return zero and we just exit()
14505f1c4225SArnaldo Carvalho de Melo 		 * here.
14515f1c4225SArnaldo Carvalho de Melo 		 */
14525f1c4225SArnaldo Carvalho de Melo 		if (ret != 1) {
14535f1c4225SArnaldo Carvalho de Melo 			if (ret == -1)
145435b9d88eSArnaldo Carvalho de Melo 				perror("unable to read pipe");
14555f1c4225SArnaldo Carvalho de Melo 			exit(ret);
14565f1c4225SArnaldo Carvalho de Melo 		}
145735b9d88eSArnaldo Carvalho de Melo 
145835b9d88eSArnaldo Carvalho de Melo 		execvp(argv[0], (char **)argv);
145935b9d88eSArnaldo Carvalho de Melo 
1460735f7e0bSArnaldo Carvalho de Melo 		if (exec_error) {
1461f33cbe72SArnaldo Carvalho de Melo 			union sigval val;
1462f33cbe72SArnaldo Carvalho de Melo 
1463f33cbe72SArnaldo Carvalho de Melo 			val.sival_int = errno;
1464f33cbe72SArnaldo Carvalho de Melo 			if (sigqueue(getppid(), SIGUSR1, val))
146535b9d88eSArnaldo Carvalho de Melo 				perror(argv[0]);
1466f33cbe72SArnaldo Carvalho de Melo 		} else
1467f33cbe72SArnaldo Carvalho de Melo 			perror(argv[0]);
146835b9d88eSArnaldo Carvalho de Melo 		exit(-1);
146935b9d88eSArnaldo Carvalho de Melo 	}
147035b9d88eSArnaldo Carvalho de Melo 
1471735f7e0bSArnaldo Carvalho de Melo 	if (exec_error) {
1472735f7e0bSArnaldo Carvalho de Melo 		struct sigaction act = {
1473735f7e0bSArnaldo Carvalho de Melo 			.sa_flags     = SA_SIGINFO,
1474735f7e0bSArnaldo Carvalho de Melo 			.sa_sigaction = exec_error,
1475735f7e0bSArnaldo Carvalho de Melo 		};
1476735f7e0bSArnaldo Carvalho de Melo 		sigaction(SIGUSR1, &act, NULL);
1477735f7e0bSArnaldo Carvalho de Melo 	}
1478735f7e0bSArnaldo Carvalho de Melo 
14791aaf63b1SArnaldo Carvalho de Melo 	if (target__none(target)) {
148003617c22SJiri Olsa 		if (evlist->core.threads == NULL) {
14811aaf63b1SArnaldo Carvalho de Melo 			fprintf(stderr, "FATAL: evlist->threads need to be set at this point (%s:%d).\n",
14821aaf63b1SArnaldo Carvalho de Melo 				__func__, __LINE__);
14831aaf63b1SArnaldo Carvalho de Melo 			goto out_close_pipes;
14841aaf63b1SArnaldo Carvalho de Melo 		}
148503617c22SJiri Olsa 		perf_thread_map__set_pid(evlist->core.threads, 0, evlist->workload.pid);
14861aaf63b1SArnaldo Carvalho de Melo 	}
148735b9d88eSArnaldo Carvalho de Melo 
148835b9d88eSArnaldo Carvalho de Melo 	close(child_ready_pipe[1]);
148935b9d88eSArnaldo Carvalho de Melo 	close(go_pipe[0]);
149035b9d88eSArnaldo Carvalho de Melo 	/*
149135b9d88eSArnaldo Carvalho de Melo 	 * wait for child to settle
149235b9d88eSArnaldo Carvalho de Melo 	 */
149335b9d88eSArnaldo Carvalho de Melo 	if (read(child_ready_pipe[0], &bf, 1) == -1) {
149435b9d88eSArnaldo Carvalho de Melo 		perror("unable to read pipe");
149535b9d88eSArnaldo Carvalho de Melo 		goto out_close_pipes;
149635b9d88eSArnaldo Carvalho de Melo 	}
149735b9d88eSArnaldo Carvalho de Melo 
1498bcf3145fSNamhyung Kim 	fcntl(go_pipe[1], F_SETFD, FD_CLOEXEC);
149935b9d88eSArnaldo Carvalho de Melo 	evlist->workload.cork_fd = go_pipe[1];
150035b9d88eSArnaldo Carvalho de Melo 	close(child_ready_pipe[0]);
150135b9d88eSArnaldo Carvalho de Melo 	return 0;
150235b9d88eSArnaldo Carvalho de Melo 
150335b9d88eSArnaldo Carvalho de Melo out_close_pipes:
150435b9d88eSArnaldo Carvalho de Melo 	close(go_pipe[0]);
150535b9d88eSArnaldo Carvalho de Melo 	close(go_pipe[1]);
150635b9d88eSArnaldo Carvalho de Melo out_close_ready_pipe:
150735b9d88eSArnaldo Carvalho de Melo 	close(child_ready_pipe[0]);
150835b9d88eSArnaldo Carvalho de Melo 	close(child_ready_pipe[1]);
150935b9d88eSArnaldo Carvalho de Melo 	return -1;
151035b9d88eSArnaldo Carvalho de Melo }
151135b9d88eSArnaldo Carvalho de Melo 
15127b392ef0SArnaldo Carvalho de Melo int evlist__start_workload(struct evlist *evlist)
151335b9d88eSArnaldo Carvalho de Melo {
151435b9d88eSArnaldo Carvalho de Melo 	if (evlist->workload.cork_fd > 0) {
1515b3824404SDavid Ahern 		char bf = 0;
1516bcf3145fSNamhyung Kim 		int ret;
151735b9d88eSArnaldo Carvalho de Melo 		/*
151835b9d88eSArnaldo Carvalho de Melo 		 * Remove the cork, let it rip!
151935b9d88eSArnaldo Carvalho de Melo 		 */
1520bcf3145fSNamhyung Kim 		ret = write(evlist->workload.cork_fd, &bf, 1);
1521bcf3145fSNamhyung Kim 		if (ret < 0)
1522e978be9eSSoramichi Akiyama 			perror("unable to write to pipe");
1523bcf3145fSNamhyung Kim 
1524bcf3145fSNamhyung Kim 		close(evlist->workload.cork_fd);
1525bcf3145fSNamhyung Kim 		return ret;
152635b9d88eSArnaldo Carvalho de Melo 	}
152735b9d88eSArnaldo Carvalho de Melo 
152835b9d88eSArnaldo Carvalho de Melo 	return 0;
152935b9d88eSArnaldo Carvalho de Melo }
1530cb0b29e0SArnaldo Carvalho de Melo 
15312a6599cdSArnaldo Carvalho de Melo int evlist__parse_sample(struct evlist *evlist, union perf_event *event, struct perf_sample *sample)
1532cb0b29e0SArnaldo Carvalho de Melo {
15333ccf8a7bSArnaldo Carvalho de Melo 	struct evsel *evsel = evlist__event2evsel(evlist, event);
15343461b65dSAdrian Hunter 	int ret;
153575562573SAdrian Hunter 
153675562573SAdrian Hunter 	if (!evsel)
153775562573SAdrian Hunter 		return -EFAULT;
15383461b65dSAdrian Hunter 	ret = evsel__parse_sample(evsel, event, sample);
15393461b65dSAdrian Hunter 	if (ret)
15403461b65dSAdrian Hunter 		return ret;
15413461b65dSAdrian Hunter 	if (perf_guest && sample->id) {
15423461b65dSAdrian Hunter 		struct perf_sample_id *sid = evlist__id2sid(evlist, sample->id);
15433461b65dSAdrian Hunter 
15443461b65dSAdrian Hunter 		if (sid) {
15453461b65dSAdrian Hunter 			sample->machine_pid = sid->machine_pid;
15463461b65dSAdrian Hunter 			sample->vcpu = sid->vcpu.cpu;
15473461b65dSAdrian Hunter 		}
15483461b65dSAdrian Hunter 	}
15493461b65dSAdrian Hunter 	return 0;
1550cb0b29e0SArnaldo Carvalho de Melo }
155178f067b3SArnaldo Carvalho de Melo 
15522a6599cdSArnaldo Carvalho de Melo int evlist__parse_sample_timestamp(struct evlist *evlist, union perf_event *event, u64 *timestamp)
155301468120SJiri Olsa {
15543ccf8a7bSArnaldo Carvalho de Melo 	struct evsel *evsel = evlist__event2evsel(evlist, event);
155501468120SJiri Olsa 
155601468120SJiri Olsa 	if (!evsel)
155701468120SJiri Olsa 		return -EFAULT;
15586b6017a2SArnaldo Carvalho de Melo 	return evsel__parse_sample_timestamp(evsel, event, timestamp);
155901468120SJiri Olsa }
156001468120SJiri Olsa 
1561d1f249ecSArnaldo Carvalho de Melo int evlist__strerror_open(struct evlist *evlist, int err, char *buf, size_t size)
1562a8f23d8fSArnaldo Carvalho de Melo {
1563a8f23d8fSArnaldo Carvalho de Melo 	int printed, value;
1564c8b5f2c9SArnaldo Carvalho de Melo 	char sbuf[STRERR_BUFSIZE], *emsg = str_error_r(err, sbuf, sizeof(sbuf));
1565a8f23d8fSArnaldo Carvalho de Melo 
1566a8f23d8fSArnaldo Carvalho de Melo 	switch (err) {
1567a8f23d8fSArnaldo Carvalho de Melo 	case EACCES:
1568a8f23d8fSArnaldo Carvalho de Melo 	case EPERM:
1569a8f23d8fSArnaldo Carvalho de Melo 		printed = scnprintf(buf, size,
1570a8f23d8fSArnaldo Carvalho de Melo 				    "Error:\t%s.\n"
1571a8f23d8fSArnaldo Carvalho de Melo 				    "Hint:\tCheck /proc/sys/kernel/perf_event_paranoid setting.", emsg);
1572a8f23d8fSArnaldo Carvalho de Melo 
15731a47245dSAdrian Hunter 		value = perf_event_paranoid();
1574a8f23d8fSArnaldo Carvalho de Melo 
1575a8f23d8fSArnaldo Carvalho de Melo 		printed += scnprintf(buf + printed, size - printed, "\nHint:\t");
1576a8f23d8fSArnaldo Carvalho de Melo 
1577a8f23d8fSArnaldo Carvalho de Melo 		if (value >= 2) {
1578a8f23d8fSArnaldo Carvalho de Melo 			printed += scnprintf(buf + printed, size - printed,
1579a8f23d8fSArnaldo Carvalho de Melo 					     "For your workloads it needs to be <= 1\nHint:\t");
1580a8f23d8fSArnaldo Carvalho de Melo 		}
1581a8f23d8fSArnaldo Carvalho de Melo 		printed += scnprintf(buf + printed, size - printed,
15825229e366SArnaldo Carvalho de Melo 				     "For system wide tracing it needs to be set to -1.\n");
1583a8f23d8fSArnaldo Carvalho de Melo 
1584a8f23d8fSArnaldo Carvalho de Melo 		printed += scnprintf(buf + printed, size - printed,
15855229e366SArnaldo Carvalho de Melo 				    "Hint:\tTry: 'sudo sh -c \"echo -1 > /proc/sys/kernel/perf_event_paranoid\"'\n"
15865229e366SArnaldo Carvalho de Melo 				    "Hint:\tThe current value is %d.", value);
1587a8f23d8fSArnaldo Carvalho de Melo 		break;
1588d9aade7fSArnaldo Carvalho de Melo 	case EINVAL: {
1589515dbe48SJiri Olsa 		struct evsel *first = evlist__first(evlist);
1590d9aade7fSArnaldo Carvalho de Melo 		int max_freq;
1591d9aade7fSArnaldo Carvalho de Melo 
1592d9aade7fSArnaldo Carvalho de Melo 		if (sysctl__read_int("kernel/perf_event_max_sample_rate", &max_freq) < 0)
1593d9aade7fSArnaldo Carvalho de Melo 			goto out_default;
1594d9aade7fSArnaldo Carvalho de Melo 
15951fc632ceSJiri Olsa 		if (first->core.attr.sample_freq < (u64)max_freq)
1596d9aade7fSArnaldo Carvalho de Melo 			goto out_default;
1597d9aade7fSArnaldo Carvalho de Melo 
1598d9aade7fSArnaldo Carvalho de Melo 		printed = scnprintf(buf, size,
1599d9aade7fSArnaldo Carvalho de Melo 				    "Error:\t%s.\n"
1600d9aade7fSArnaldo Carvalho de Melo 				    "Hint:\tCheck /proc/sys/kernel/perf_event_max_sample_rate.\n"
1601d9aade7fSArnaldo Carvalho de Melo 				    "Hint:\tThe current value is %d and %" PRIu64 " is being requested.",
16021fc632ceSJiri Olsa 				    emsg, max_freq, first->core.attr.sample_freq);
1603d9aade7fSArnaldo Carvalho de Melo 		break;
1604d9aade7fSArnaldo Carvalho de Melo 	}
1605a8f23d8fSArnaldo Carvalho de Melo 	default:
1606d9aade7fSArnaldo Carvalho de Melo out_default:
1607a8f23d8fSArnaldo Carvalho de Melo 		scnprintf(buf, size, "%s", emsg);
1608a8f23d8fSArnaldo Carvalho de Melo 		break;
1609a8f23d8fSArnaldo Carvalho de Melo 	}
1610a8f23d8fSArnaldo Carvalho de Melo 
1611a8f23d8fSArnaldo Carvalho de Melo 	return 0;
1612a8f23d8fSArnaldo Carvalho de Melo }
1613a025e4f0SAdrian Hunter 
1614d1f249ecSArnaldo Carvalho de Melo int evlist__strerror_mmap(struct evlist *evlist, int err, char *buf, size_t size)
1615956fa571SArnaldo Carvalho de Melo {
1616c8b5f2c9SArnaldo Carvalho de Melo 	char sbuf[STRERR_BUFSIZE], *emsg = str_error_r(err, sbuf, sizeof(sbuf));
1617f6fa4375SJiri Olsa 	int pages_attempted = evlist->core.mmap_len / 1024, pages_max_per_user, printed = 0;
1618956fa571SArnaldo Carvalho de Melo 
1619956fa571SArnaldo Carvalho de Melo 	switch (err) {
1620956fa571SArnaldo Carvalho de Melo 	case EPERM:
1621e5d4a290SArnaldo Carvalho de Melo 		sysctl__read_int("kernel/perf_event_mlock_kb", &pages_max_per_user);
1622e965bea1SArnaldo Carvalho de Melo 		printed += scnprintf(buf + printed, size - printed,
1623e965bea1SArnaldo Carvalho de Melo 				     "Error:\t%s.\n"
1624956fa571SArnaldo Carvalho de Melo 				     "Hint:\tCheck /proc/sys/kernel/perf_event_mlock_kb (%d kB) setting.\n"
1625e965bea1SArnaldo Carvalho de Melo 				     "Hint:\tTried using %zd kB.\n",
1626e5d4a290SArnaldo Carvalho de Melo 				     emsg, pages_max_per_user, pages_attempted);
1627e965bea1SArnaldo Carvalho de Melo 
1628e965bea1SArnaldo Carvalho de Melo 		if (pages_attempted >= pages_max_per_user) {
1629e965bea1SArnaldo Carvalho de Melo 			printed += scnprintf(buf + printed, size - printed,
1630e965bea1SArnaldo Carvalho de Melo 					     "Hint:\tTry 'sudo sh -c \"echo %d > /proc/sys/kernel/perf_event_mlock_kb\"', or\n",
1631e965bea1SArnaldo Carvalho de Melo 					     pages_max_per_user + pages_attempted);
1632e965bea1SArnaldo Carvalho de Melo 		}
1633e965bea1SArnaldo Carvalho de Melo 
1634e965bea1SArnaldo Carvalho de Melo 		printed += scnprintf(buf + printed, size - printed,
1635e965bea1SArnaldo Carvalho de Melo 				     "Hint:\tTry using a smaller -m/--mmap-pages value.");
1636956fa571SArnaldo Carvalho de Melo 		break;
1637956fa571SArnaldo Carvalho de Melo 	default:
1638956fa571SArnaldo Carvalho de Melo 		scnprintf(buf, size, "%s", emsg);
1639956fa571SArnaldo Carvalho de Melo 		break;
1640956fa571SArnaldo Carvalho de Melo 	}
1641956fa571SArnaldo Carvalho de Melo 
1642956fa571SArnaldo Carvalho de Melo 	return 0;
1643956fa571SArnaldo Carvalho de Melo }
1644956fa571SArnaldo Carvalho de Melo 
1645e414fd1aSArnaldo Carvalho de Melo void evlist__to_front(struct evlist *evlist, struct evsel *move_evsel)
1646a025e4f0SAdrian Hunter {
164732dcd021SJiri Olsa 	struct evsel *evsel, *n;
1648a025e4f0SAdrian Hunter 	LIST_HEAD(move);
1649a025e4f0SAdrian Hunter 
1650515dbe48SJiri Olsa 	if (move_evsel == evlist__first(evlist))
1651a025e4f0SAdrian Hunter 		return;
1652a025e4f0SAdrian Hunter 
1653e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry_safe(evlist, n, evsel) {
1654fba7c866SJiri Olsa 		if (evsel__leader(evsel) == evsel__leader(move_evsel))
1655b27c4eceSJiri Olsa 			list_move_tail(&evsel->core.node, &move);
1656a025e4f0SAdrian Hunter 	}
1657a025e4f0SAdrian Hunter 
1658ce9036a6SJiri Olsa 	list_splice(&move, &evlist->core.entries);
1659a025e4f0SAdrian Hunter }
166060b0896cSAdrian Hunter 
1661e80db255SArnaldo Carvalho de Melo struct evsel *evlist__get_tracking_event(struct evlist *evlist)
1662442ad225SAdrian Hunter {
1663442ad225SAdrian Hunter 	struct evsel *evsel;
1664442ad225SAdrian Hunter 
1665442ad225SAdrian Hunter 	evlist__for_each_entry(evlist, evsel) {
1666442ad225SAdrian Hunter 		if (evsel->tracking)
1667442ad225SAdrian Hunter 			return evsel;
1668442ad225SAdrian Hunter 	}
1669442ad225SAdrian Hunter 
1670442ad225SAdrian Hunter 	return evlist__first(evlist);
1671442ad225SAdrian Hunter }
1672442ad225SAdrian Hunter 
1673e80db255SArnaldo Carvalho de Melo void evlist__set_tracking_event(struct evlist *evlist, struct evsel *tracking_evsel)
167460b0896cSAdrian Hunter {
167532dcd021SJiri Olsa 	struct evsel *evsel;
167660b0896cSAdrian Hunter 
167760b0896cSAdrian Hunter 	if (tracking_evsel->tracking)
167860b0896cSAdrian Hunter 		return;
167960b0896cSAdrian Hunter 
1680e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, evsel) {
168160b0896cSAdrian Hunter 		if (evsel != tracking_evsel)
168260b0896cSAdrian Hunter 			evsel->tracking = false;
168360b0896cSAdrian Hunter 	}
168460b0896cSAdrian Hunter 
168560b0896cSAdrian Hunter 	tracking_evsel->tracking = true;
168660b0896cSAdrian Hunter }
16877630b3e2SWang Nan 
1688b02736f7SArnaldo Carvalho de Melo struct evsel *evlist__find_evsel_by_str(struct evlist *evlist, const char *str)
16897630b3e2SWang Nan {
169032dcd021SJiri Olsa 	struct evsel *evsel;
16917630b3e2SWang Nan 
1692e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, evsel) {
16937630b3e2SWang Nan 		if (!evsel->name)
16947630b3e2SWang Nan 			continue;
16957630b3e2SWang Nan 		if (strcmp(str, evsel->name) == 0)
16967630b3e2SWang Nan 			return evsel;
16977630b3e2SWang Nan 	}
16987630b3e2SWang Nan 
16997630b3e2SWang Nan 	return NULL;
17007630b3e2SWang Nan }
170154cc54deSWang Nan 
1702ade9d208SArnaldo Carvalho de Melo void evlist__toggle_bkw_mmap(struct evlist *evlist, enum bkw_mmap_state state)
170354cc54deSWang Nan {
170454cc54deSWang Nan 	enum bkw_mmap_state old_state = evlist->bkw_mmap_state;
170554cc54deSWang Nan 	enum action {
170654cc54deSWang Nan 		NONE,
170754cc54deSWang Nan 		PAUSE,
170854cc54deSWang Nan 		RESUME,
170954cc54deSWang Nan 	} action = NONE;
171054cc54deSWang Nan 
17110b72d69aSWang Nan 	if (!evlist->overwrite_mmap)
171254cc54deSWang Nan 		return;
171354cc54deSWang Nan 
171454cc54deSWang Nan 	switch (old_state) {
171554cc54deSWang Nan 	case BKW_MMAP_NOTREADY: {
171654cc54deSWang Nan 		if (state != BKW_MMAP_RUNNING)
1717dd8bd53aSLuis de Bethencourt 			goto state_err;
171854cc54deSWang Nan 		break;
171954cc54deSWang Nan 	}
172054cc54deSWang Nan 	case BKW_MMAP_RUNNING: {
172154cc54deSWang Nan 		if (state != BKW_MMAP_DATA_PENDING)
172254cc54deSWang Nan 			goto state_err;
172354cc54deSWang Nan 		action = PAUSE;
172454cc54deSWang Nan 		break;
172554cc54deSWang Nan 	}
172654cc54deSWang Nan 	case BKW_MMAP_DATA_PENDING: {
172754cc54deSWang Nan 		if (state != BKW_MMAP_EMPTY)
172854cc54deSWang Nan 			goto state_err;
172954cc54deSWang Nan 		break;
173054cc54deSWang Nan 	}
173154cc54deSWang Nan 	case BKW_MMAP_EMPTY: {
173254cc54deSWang Nan 		if (state != BKW_MMAP_RUNNING)
173354cc54deSWang Nan 			goto state_err;
173454cc54deSWang Nan 		action = RESUME;
173554cc54deSWang Nan 		break;
173654cc54deSWang Nan 	}
173754cc54deSWang Nan 	default:
173854cc54deSWang Nan 		WARN_ONCE(1, "Shouldn't get there\n");
173954cc54deSWang Nan 	}
174054cc54deSWang Nan 
174154cc54deSWang Nan 	evlist->bkw_mmap_state = state;
174254cc54deSWang Nan 
174354cc54deSWang Nan 	switch (action) {
174454cc54deSWang Nan 	case PAUSE:
17450a60b339SArnaldo Carvalho de Melo 		evlist__pause(evlist);
174654cc54deSWang Nan 		break;
174754cc54deSWang Nan 	case RESUME:
17480a60b339SArnaldo Carvalho de Melo 		evlist__resume(evlist);
174954cc54deSWang Nan 		break;
175054cc54deSWang Nan 	case NONE:
175154cc54deSWang Nan 	default:
175254cc54deSWang Nan 		break;
175354cc54deSWang Nan 	}
175454cc54deSWang Nan 
175554cc54deSWang Nan state_err:
175654cc54deSWang Nan 	return;
175754cc54deSWang Nan }
175807d6f446SArnaldo Carvalho de Melo 
175978e1bc25SArnaldo Carvalho de Melo bool evlist__exclude_kernel(struct evlist *evlist)
176007d6f446SArnaldo Carvalho de Melo {
176132dcd021SJiri Olsa 	struct evsel *evsel;
176207d6f446SArnaldo Carvalho de Melo 
176307d6f446SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, evsel) {
17641fc632ceSJiri Olsa 		if (!evsel->core.attr.exclude_kernel)
176507d6f446SArnaldo Carvalho de Melo 			return false;
176607d6f446SArnaldo Carvalho de Melo 	}
176707d6f446SArnaldo Carvalho de Melo 
176807d6f446SArnaldo Carvalho de Melo 	return true;
176907d6f446SArnaldo Carvalho de Melo }
1770e2bdbe80SJin Yao 
1771e2bdbe80SJin Yao /*
1772e2bdbe80SJin Yao  * Events in data file are not collect in groups, but we still want
1773e2bdbe80SJin Yao  * the group display. Set the artificial group and set the leader's
1774e2bdbe80SJin Yao  * forced_leader flag to notify the display code.
1775e2bdbe80SJin Yao  */
177664b4778bSArnaldo Carvalho de Melo void evlist__force_leader(struct evlist *evlist)
1777e2bdbe80SJin Yao {
17783a683120SJiri Olsa 	if (!evlist->core.nr_groups) {
1779515dbe48SJiri Olsa 		struct evsel *leader = evlist__first(evlist);
1780e2bdbe80SJin Yao 
1781a622eafaSArnaldo Carvalho de Melo 		evlist__set_leader(evlist);
1782e2bdbe80SJin Yao 		leader->forced_leader = true;
1783e2bdbe80SJin Yao 	}
1784e2bdbe80SJin Yao }
1785c3537fc2SAndi Kleen 
178664b4778bSArnaldo Carvalho de Melo struct evsel *evlist__reset_weak_group(struct evlist *evsel_list, struct evsel *evsel, bool close)
1787c3537fc2SAndi Kleen {
178832dcd021SJiri Olsa 	struct evsel *c2, *leader;
1789c3537fc2SAndi Kleen 	bool is_open = true;
1790c3537fc2SAndi Kleen 
1791fba7c866SJiri Olsa 	leader = evsel__leader(evsel);
1792fba7c866SJiri Olsa 
1793c3537fc2SAndi Kleen 	pr_debug("Weak group for %s/%d failed\n",
17945643b1a5SJiri Olsa 			leader->name, leader->core.nr_members);
1795c3537fc2SAndi Kleen 
1796c3537fc2SAndi Kleen 	/*
1797c3537fc2SAndi Kleen 	 * for_each_group_member doesn't work here because it doesn't
1798c3537fc2SAndi Kleen 	 * include the first entry.
1799c3537fc2SAndi Kleen 	 */
1800c3537fc2SAndi Kleen 	evlist__for_each_entry(evsel_list, c2) {
1801c3537fc2SAndi Kleen 		if (c2 == evsel)
1802c3537fc2SAndi Kleen 			is_open = false;
1803fba7c866SJiri Olsa 		if (evsel__has_leader(c2, leader)) {
18044804e011SAndi Kleen 			if (is_open && close)
18055a40e199SAndi Kleen 				perf_evsel__close(&c2->core);
1806d98079c0SIan Rogers 			/*
1807d98079c0SIan Rogers 			 * We want to close all members of the group and reopen
1808d98079c0SIan Rogers 			 * them. Some events, like Intel topdown, require being
1809d98079c0SIan Rogers 			 * in a group and so keep these in the group.
1810d98079c0SIan Rogers 			 */
1811e8f4f794SKan Liang 			evsel__remove_from_group(c2, leader);
1812d98079c0SIan Rogers 
18134804e011SAndi Kleen 			/*
18144804e011SAndi Kleen 			 * Set this for all former members of the group
18154804e011SAndi Kleen 			 * to indicate they get reopened.
18164804e011SAndi Kleen 			 */
18174804e011SAndi Kleen 			c2->reset_group = true;
1818c3537fc2SAndi Kleen 		}
1819c3537fc2SAndi Kleen 	}
1820d98079c0SIan Rogers 	/* Reset the leader count if all entries were removed. */
1821d98079c0SIan Rogers 	if (leader->core.nr_members == 1)
1822d98079c0SIan Rogers 		leader->core.nr_members = 0;
1823c3537fc2SAndi Kleen 	return leader;
1824c3537fc2SAndi Kleen }
1825ec886bf5SAlexey Budankov 
1826a8fcbd26SAdrian Hunter static int evlist__parse_control_fifo(const char *str, int *ctl_fd, int *ctl_fd_ack, bool *ctl_fd_close)
1827a8fcbd26SAdrian Hunter {
1828a8fcbd26SAdrian Hunter 	char *s, *p;
1829a8fcbd26SAdrian Hunter 	int ret = 0, fd;
1830a8fcbd26SAdrian Hunter 
1831a8fcbd26SAdrian Hunter 	if (strncmp(str, "fifo:", 5))
1832a8fcbd26SAdrian Hunter 		return -EINVAL;
1833a8fcbd26SAdrian Hunter 
1834a8fcbd26SAdrian Hunter 	str += 5;
1835a8fcbd26SAdrian Hunter 	if (!*str || *str == ',')
1836a8fcbd26SAdrian Hunter 		return -EINVAL;
1837a8fcbd26SAdrian Hunter 
1838a8fcbd26SAdrian Hunter 	s = strdup(str);
1839a8fcbd26SAdrian Hunter 	if (!s)
1840a8fcbd26SAdrian Hunter 		return -ENOMEM;
1841a8fcbd26SAdrian Hunter 
1842a8fcbd26SAdrian Hunter 	p = strchr(s, ',');
1843a8fcbd26SAdrian Hunter 	if (p)
1844a8fcbd26SAdrian Hunter 		*p = '\0';
1845a8fcbd26SAdrian Hunter 
1846a8fcbd26SAdrian Hunter 	/*
1847a8fcbd26SAdrian Hunter 	 * O_RDWR avoids POLLHUPs which is necessary to allow the other
1848a8fcbd26SAdrian Hunter 	 * end of a FIFO to be repeatedly opened and closed.
1849a8fcbd26SAdrian Hunter 	 */
1850a8fcbd26SAdrian Hunter 	fd = open(s, O_RDWR | O_NONBLOCK | O_CLOEXEC);
1851a8fcbd26SAdrian Hunter 	if (fd < 0) {
1852a8fcbd26SAdrian Hunter 		pr_err("Failed to open '%s'\n", s);
1853a8fcbd26SAdrian Hunter 		ret = -errno;
1854a8fcbd26SAdrian Hunter 		goto out_free;
1855a8fcbd26SAdrian Hunter 	}
1856a8fcbd26SAdrian Hunter 	*ctl_fd = fd;
1857a8fcbd26SAdrian Hunter 	*ctl_fd_close = true;
1858a8fcbd26SAdrian Hunter 
1859a8fcbd26SAdrian Hunter 	if (p && *++p) {
1860a8fcbd26SAdrian Hunter 		/* O_RDWR | O_NONBLOCK means the other end need not be open */
1861a8fcbd26SAdrian Hunter 		fd = open(p, O_RDWR | O_NONBLOCK | O_CLOEXEC);
1862a8fcbd26SAdrian Hunter 		if (fd < 0) {
1863a8fcbd26SAdrian Hunter 			pr_err("Failed to open '%s'\n", p);
1864a8fcbd26SAdrian Hunter 			ret = -errno;
1865a8fcbd26SAdrian Hunter 			goto out_free;
1866a8fcbd26SAdrian Hunter 		}
1867a8fcbd26SAdrian Hunter 		*ctl_fd_ack = fd;
1868a8fcbd26SAdrian Hunter 	}
1869a8fcbd26SAdrian Hunter 
1870a8fcbd26SAdrian Hunter out_free:
1871a8fcbd26SAdrian Hunter 	free(s);
1872a8fcbd26SAdrian Hunter 	return ret;
1873a8fcbd26SAdrian Hunter }
1874a8fcbd26SAdrian Hunter 
1875a8fcbd26SAdrian Hunter int evlist__parse_control(const char *str, int *ctl_fd, int *ctl_fd_ack, bool *ctl_fd_close)
18769864a66dSAdrian Hunter {
18779864a66dSAdrian Hunter 	char *comma = NULL, *endptr = NULL;
18789864a66dSAdrian Hunter 
1879a8fcbd26SAdrian Hunter 	*ctl_fd_close = false;
1880a8fcbd26SAdrian Hunter 
18819864a66dSAdrian Hunter 	if (strncmp(str, "fd:", 3))
1882a8fcbd26SAdrian Hunter 		return evlist__parse_control_fifo(str, ctl_fd, ctl_fd_ack, ctl_fd_close);
18839864a66dSAdrian Hunter 
18849864a66dSAdrian Hunter 	*ctl_fd = strtoul(&str[3], &endptr, 0);
18859864a66dSAdrian Hunter 	if (endptr == &str[3])
18869864a66dSAdrian Hunter 		return -EINVAL;
18879864a66dSAdrian Hunter 
18889864a66dSAdrian Hunter 	comma = strchr(str, ',');
18899864a66dSAdrian Hunter 	if (comma) {
18909864a66dSAdrian Hunter 		if (endptr != comma)
18919864a66dSAdrian Hunter 			return -EINVAL;
18929864a66dSAdrian Hunter 
18939864a66dSAdrian Hunter 		*ctl_fd_ack = strtoul(comma + 1, &endptr, 0);
18949864a66dSAdrian Hunter 		if (endptr == comma + 1 || *endptr != '\0')
18959864a66dSAdrian Hunter 			return -EINVAL;
18969864a66dSAdrian Hunter 	}
18979864a66dSAdrian Hunter 
18989864a66dSAdrian Hunter 	return 0;
18999864a66dSAdrian Hunter }
19009864a66dSAdrian Hunter 
1901ee7fe31eSAdrian Hunter void evlist__close_control(int ctl_fd, int ctl_fd_ack, bool *ctl_fd_close)
1902ee7fe31eSAdrian Hunter {
1903ee7fe31eSAdrian Hunter 	if (*ctl_fd_close) {
1904ee7fe31eSAdrian Hunter 		*ctl_fd_close = false;
1905ee7fe31eSAdrian Hunter 		close(ctl_fd);
1906ee7fe31eSAdrian Hunter 		if (ctl_fd_ack >= 0)
1907ee7fe31eSAdrian Hunter 			close(ctl_fd_ack);
1908ee7fe31eSAdrian Hunter 	}
1909ee7fe31eSAdrian Hunter }
1910ee7fe31eSAdrian Hunter 
1911ec886bf5SAlexey Budankov int evlist__initialize_ctlfd(struct evlist *evlist, int fd, int ack)
1912ec886bf5SAlexey Budankov {
1913ec886bf5SAlexey Budankov 	if (fd == -1) {
1914ec886bf5SAlexey Budankov 		pr_debug("Control descriptor is not initialized\n");
1915ec886bf5SAlexey Budankov 		return 0;
1916ec886bf5SAlexey Budankov 	}
1917ec886bf5SAlexey Budankov 
1918ec886bf5SAlexey Budankov 	evlist->ctl_fd.pos = perf_evlist__add_pollfd(&evlist->core, fd, NULL, POLLIN,
1919feff0b61SAdrian Hunter 						     fdarray_flag__nonfilterable |
1920feff0b61SAdrian Hunter 						     fdarray_flag__non_perf_event);
1921ec886bf5SAlexey Budankov 	if (evlist->ctl_fd.pos < 0) {
1922ec886bf5SAlexey Budankov 		evlist->ctl_fd.pos = -1;
1923ec886bf5SAlexey Budankov 		pr_err("Failed to add ctl fd entry: %m\n");
1924ec886bf5SAlexey Budankov 		return -1;
1925ec886bf5SAlexey Budankov 	}
1926ec886bf5SAlexey Budankov 
1927ec886bf5SAlexey Budankov 	evlist->ctl_fd.fd = fd;
1928ec886bf5SAlexey Budankov 	evlist->ctl_fd.ack = ack;
1929ec886bf5SAlexey Budankov 
1930ec886bf5SAlexey Budankov 	return 0;
1931ec886bf5SAlexey Budankov }
1932ec886bf5SAlexey Budankov 
1933ec886bf5SAlexey Budankov bool evlist__ctlfd_initialized(struct evlist *evlist)
1934ec886bf5SAlexey Budankov {
1935ec886bf5SAlexey Budankov 	return evlist->ctl_fd.pos >= 0;
1936ec886bf5SAlexey Budankov }
1937ec886bf5SAlexey Budankov 
1938ec886bf5SAlexey Budankov int evlist__finalize_ctlfd(struct evlist *evlist)
1939ec886bf5SAlexey Budankov {
1940ec886bf5SAlexey Budankov 	struct pollfd *entries = evlist->core.pollfd.entries;
1941ec886bf5SAlexey Budankov 
1942ec886bf5SAlexey Budankov 	if (!evlist__ctlfd_initialized(evlist))
1943ec886bf5SAlexey Budankov 		return 0;
1944ec886bf5SAlexey Budankov 
1945ec886bf5SAlexey Budankov 	entries[evlist->ctl_fd.pos].fd = -1;
1946ec886bf5SAlexey Budankov 	entries[evlist->ctl_fd.pos].events = 0;
1947ec886bf5SAlexey Budankov 	entries[evlist->ctl_fd.pos].revents = 0;
1948ec886bf5SAlexey Budankov 
1949ec886bf5SAlexey Budankov 	evlist->ctl_fd.pos = -1;
1950ec886bf5SAlexey Budankov 	evlist->ctl_fd.ack = -1;
1951ec886bf5SAlexey Budankov 	evlist->ctl_fd.fd = -1;
1952ec886bf5SAlexey Budankov 
1953ec886bf5SAlexey Budankov 	return 0;
1954ec886bf5SAlexey Budankov }
1955ec886bf5SAlexey Budankov 
1956ec886bf5SAlexey Budankov static int evlist__ctlfd_recv(struct evlist *evlist, enum evlist_ctl_cmd *cmd,
1957ec886bf5SAlexey Budankov 			      char *cmd_data, size_t data_size)
1958ec886bf5SAlexey Budankov {
1959ec886bf5SAlexey Budankov 	int err;
1960ec886bf5SAlexey Budankov 	char c;
1961ec886bf5SAlexey Budankov 	size_t bytes_read = 0;
1962ec886bf5SAlexey Budankov 
196340db8ff5SAdrian Hunter 	*cmd = EVLIST_CTL_CMD_UNSUPPORTED;
1964ec886bf5SAlexey Budankov 	memset(cmd_data, 0, data_size);
1965ec886bf5SAlexey Budankov 	data_size--;
1966ec886bf5SAlexey Budankov 
1967ec886bf5SAlexey Budankov 	do {
1968ec886bf5SAlexey Budankov 		err = read(evlist->ctl_fd.fd, &c, 1);
1969ec886bf5SAlexey Budankov 		if (err > 0) {
1970ec886bf5SAlexey Budankov 			if (c == '\n' || c == '\0')
1971ec886bf5SAlexey Budankov 				break;
1972ec886bf5SAlexey Budankov 			cmd_data[bytes_read++] = c;
1973ec886bf5SAlexey Budankov 			if (bytes_read == data_size)
1974ec886bf5SAlexey Budankov 				break;
197540db8ff5SAdrian Hunter 			continue;
197640db8ff5SAdrian Hunter 		} else if (err == -1) {
197740db8ff5SAdrian Hunter 			if (errno == EINTR)
197840db8ff5SAdrian Hunter 				continue;
197940db8ff5SAdrian Hunter 			if (errno == EAGAIN || errno == EWOULDBLOCK)
198040db8ff5SAdrian Hunter 				err = 0;
198140db8ff5SAdrian Hunter 			else
1982ec886bf5SAlexey Budankov 				pr_err("Failed to read from ctlfd %d: %m\n", evlist->ctl_fd.fd);
1983ec886bf5SAlexey Budankov 		}
198440db8ff5SAdrian Hunter 		break;
1985ec886bf5SAlexey Budankov 	} while (1);
1986ec886bf5SAlexey Budankov 
1987ec886bf5SAlexey Budankov 	pr_debug("Message from ctl_fd: \"%s%s\"\n", cmd_data,
1988ec886bf5SAlexey Budankov 		 bytes_read == data_size ? "" : c == '\n' ? "\\n" : "\\0");
1989ec886bf5SAlexey Budankov 
199040db8ff5SAdrian Hunter 	if (bytes_read > 0) {
1991ec886bf5SAlexey Budankov 		if (!strncmp(cmd_data, EVLIST_CTL_CMD_ENABLE_TAG,
1992ec886bf5SAlexey Budankov 			     (sizeof(EVLIST_CTL_CMD_ENABLE_TAG)-1))) {
1993ec886bf5SAlexey Budankov 			*cmd = EVLIST_CTL_CMD_ENABLE;
1994ec886bf5SAlexey Budankov 		} else if (!strncmp(cmd_data, EVLIST_CTL_CMD_DISABLE_TAG,
1995ec886bf5SAlexey Budankov 				    (sizeof(EVLIST_CTL_CMD_DISABLE_TAG)-1))) {
1996ec886bf5SAlexey Budankov 			*cmd = EVLIST_CTL_CMD_DISABLE;
1997d20aff15SAdrian Hunter 		} else if (!strncmp(cmd_data, EVLIST_CTL_CMD_SNAPSHOT_TAG,
1998d20aff15SAdrian Hunter 				    (sizeof(EVLIST_CTL_CMD_SNAPSHOT_TAG)-1))) {
1999d20aff15SAdrian Hunter 			*cmd = EVLIST_CTL_CMD_SNAPSHOT;
2000d20aff15SAdrian Hunter 			pr_debug("is snapshot\n");
2001142544a9SJiri Olsa 		} else if (!strncmp(cmd_data, EVLIST_CTL_CMD_EVLIST_TAG,
2002142544a9SJiri Olsa 				    (sizeof(EVLIST_CTL_CMD_EVLIST_TAG)-1))) {
2003142544a9SJiri Olsa 			*cmd = EVLIST_CTL_CMD_EVLIST;
2004f186cd61SJiri Olsa 		} else if (!strncmp(cmd_data, EVLIST_CTL_CMD_STOP_TAG,
2005f186cd61SJiri Olsa 				    (sizeof(EVLIST_CTL_CMD_STOP_TAG)-1))) {
2006f186cd61SJiri Olsa 			*cmd = EVLIST_CTL_CMD_STOP;
200747fddcb4SJiri Olsa 		} else if (!strncmp(cmd_data, EVLIST_CTL_CMD_PING_TAG,
200847fddcb4SJiri Olsa 				    (sizeof(EVLIST_CTL_CMD_PING_TAG)-1))) {
200947fddcb4SJiri Olsa 			*cmd = EVLIST_CTL_CMD_PING;
2010ec886bf5SAlexey Budankov 		}
2011ec886bf5SAlexey Budankov 	}
2012ec886bf5SAlexey Budankov 
201340db8ff5SAdrian Hunter 	return bytes_read ? (int)bytes_read : err;
2014ec886bf5SAlexey Budankov }
2015ec886bf5SAlexey Budankov 
2016d20aff15SAdrian Hunter int evlist__ctlfd_ack(struct evlist *evlist)
2017ec886bf5SAlexey Budankov {
2018ec886bf5SAlexey Budankov 	int err;
2019ec886bf5SAlexey Budankov 
2020ec886bf5SAlexey Budankov 	if (evlist->ctl_fd.ack == -1)
2021ec886bf5SAlexey Budankov 		return 0;
2022ec886bf5SAlexey Budankov 
2023ec886bf5SAlexey Budankov 	err = write(evlist->ctl_fd.ack, EVLIST_CTL_CMD_ACK_TAG,
2024ec886bf5SAlexey Budankov 		    sizeof(EVLIST_CTL_CMD_ACK_TAG));
2025ec886bf5SAlexey Budankov 	if (err == -1)
2026ec886bf5SAlexey Budankov 		pr_err("failed to write to ctl_ack_fd %d: %m\n", evlist->ctl_fd.ack);
2027ec886bf5SAlexey Budankov 
2028ec886bf5SAlexey Budankov 	return err;
2029ec886bf5SAlexey Budankov }
2030ec886bf5SAlexey Budankov 
2031991ae4ebSJiri Olsa static int get_cmd_arg(char *cmd_data, size_t cmd_size, char **arg)
2032991ae4ebSJiri Olsa {
2033991ae4ebSJiri Olsa 	char *data = cmd_data + cmd_size;
2034991ae4ebSJiri Olsa 
2035991ae4ebSJiri Olsa 	/* no argument */
2036991ae4ebSJiri Olsa 	if (!*data)
2037991ae4ebSJiri Olsa 		return 0;
2038991ae4ebSJiri Olsa 
2039991ae4ebSJiri Olsa 	/* there's argument */
2040991ae4ebSJiri Olsa 	if (*data == ' ') {
2041991ae4ebSJiri Olsa 		*arg = data + 1;
2042991ae4ebSJiri Olsa 		return 1;
2043991ae4ebSJiri Olsa 	}
2044991ae4ebSJiri Olsa 
2045991ae4ebSJiri Olsa 	/* malformed */
2046991ae4ebSJiri Olsa 	return -1;
2047991ae4ebSJiri Olsa }
2048991ae4ebSJiri Olsa 
2049991ae4ebSJiri Olsa static int evlist__ctlfd_enable(struct evlist *evlist, char *cmd_data, bool enable)
2050991ae4ebSJiri Olsa {
2051991ae4ebSJiri Olsa 	struct evsel *evsel;
2052991ae4ebSJiri Olsa 	char *name;
2053991ae4ebSJiri Olsa 	int err;
2054991ae4ebSJiri Olsa 
2055991ae4ebSJiri Olsa 	err = get_cmd_arg(cmd_data,
2056991ae4ebSJiri Olsa 			  enable ? sizeof(EVLIST_CTL_CMD_ENABLE_TAG) - 1 :
2057991ae4ebSJiri Olsa 				   sizeof(EVLIST_CTL_CMD_DISABLE_TAG) - 1,
2058991ae4ebSJiri Olsa 			  &name);
2059991ae4ebSJiri Olsa 	if (err < 0) {
2060991ae4ebSJiri Olsa 		pr_info("failed: wrong command\n");
2061991ae4ebSJiri Olsa 		return -1;
2062991ae4ebSJiri Olsa 	}
2063991ae4ebSJiri Olsa 
2064991ae4ebSJiri Olsa 	if (err) {
2065991ae4ebSJiri Olsa 		evsel = evlist__find_evsel_by_str(evlist, name);
2066991ae4ebSJiri Olsa 		if (evsel) {
2067991ae4ebSJiri Olsa 			if (enable)
2068991ae4ebSJiri Olsa 				evlist__enable_evsel(evlist, name);
2069991ae4ebSJiri Olsa 			else
2070991ae4ebSJiri Olsa 				evlist__disable_evsel(evlist, name);
2071991ae4ebSJiri Olsa 			pr_info("Event %s %s\n", evsel->name,
2072991ae4ebSJiri Olsa 				enable ? "enabled" : "disabled");
2073991ae4ebSJiri Olsa 		} else {
2074991ae4ebSJiri Olsa 			pr_info("failed: can't find '%s' event\n", name);
2075991ae4ebSJiri Olsa 		}
2076991ae4ebSJiri Olsa 	} else {
2077991ae4ebSJiri Olsa 		if (enable) {
2078991ae4ebSJiri Olsa 			evlist__enable(evlist);
2079991ae4ebSJiri Olsa 			pr_info(EVLIST_ENABLED_MSG);
2080991ae4ebSJiri Olsa 		} else {
2081991ae4ebSJiri Olsa 			evlist__disable(evlist);
2082991ae4ebSJiri Olsa 			pr_info(EVLIST_DISABLED_MSG);
2083991ae4ebSJiri Olsa 		}
2084991ae4ebSJiri Olsa 	}
2085991ae4ebSJiri Olsa 
2086991ae4ebSJiri Olsa 	return 0;
2087991ae4ebSJiri Olsa }
2088991ae4ebSJiri Olsa 
2089142544a9SJiri Olsa static int evlist__ctlfd_list(struct evlist *evlist, char *cmd_data)
2090142544a9SJiri Olsa {
2091142544a9SJiri Olsa 	struct perf_attr_details details = { .verbose = false, };
2092142544a9SJiri Olsa 	struct evsel *evsel;
2093142544a9SJiri Olsa 	char *arg;
2094142544a9SJiri Olsa 	int err;
2095142544a9SJiri Olsa 
2096142544a9SJiri Olsa 	err = get_cmd_arg(cmd_data,
2097142544a9SJiri Olsa 			  sizeof(EVLIST_CTL_CMD_EVLIST_TAG) - 1,
2098142544a9SJiri Olsa 			  &arg);
2099142544a9SJiri Olsa 	if (err < 0) {
2100142544a9SJiri Olsa 		pr_info("failed: wrong command\n");
2101142544a9SJiri Olsa 		return -1;
2102142544a9SJiri Olsa 	}
2103142544a9SJiri Olsa 
2104142544a9SJiri Olsa 	if (err) {
2105142544a9SJiri Olsa 		if (!strcmp(arg, "-v")) {
2106142544a9SJiri Olsa 			details.verbose = true;
2107142544a9SJiri Olsa 		} else if (!strcmp(arg, "-g")) {
2108142544a9SJiri Olsa 			details.event_group = true;
2109142544a9SJiri Olsa 		} else if (!strcmp(arg, "-F")) {
2110142544a9SJiri Olsa 			details.freq = true;
2111142544a9SJiri Olsa 		} else {
2112142544a9SJiri Olsa 			pr_info("failed: wrong command\n");
2113142544a9SJiri Olsa 			return -1;
2114142544a9SJiri Olsa 		}
2115142544a9SJiri Olsa 	}
2116142544a9SJiri Olsa 
2117142544a9SJiri Olsa 	evlist__for_each_entry(evlist, evsel)
2118142544a9SJiri Olsa 		evsel__fprintf(evsel, &details, stderr);
2119142544a9SJiri Olsa 
2120142544a9SJiri Olsa 	return 0;
2121142544a9SJiri Olsa }
2122142544a9SJiri Olsa 
2123ec886bf5SAlexey Budankov int evlist__ctlfd_process(struct evlist *evlist, enum evlist_ctl_cmd *cmd)
2124ec886bf5SAlexey Budankov {
2125ec886bf5SAlexey Budankov 	int err = 0;
2126ec886bf5SAlexey Budankov 	char cmd_data[EVLIST_CTL_CMD_MAX_LEN];
2127ec886bf5SAlexey Budankov 	int ctlfd_pos = evlist->ctl_fd.pos;
2128ec886bf5SAlexey Budankov 	struct pollfd *entries = evlist->core.pollfd.entries;
2129ec886bf5SAlexey Budankov 
2130ec886bf5SAlexey Budankov 	if (!evlist__ctlfd_initialized(evlist) || !entries[ctlfd_pos].revents)
2131ec886bf5SAlexey Budankov 		return 0;
2132ec886bf5SAlexey Budankov 
2133ec886bf5SAlexey Budankov 	if (entries[ctlfd_pos].revents & POLLIN) {
2134ec886bf5SAlexey Budankov 		err = evlist__ctlfd_recv(evlist, cmd, cmd_data,
2135ec886bf5SAlexey Budankov 					 EVLIST_CTL_CMD_MAX_LEN);
2136ec886bf5SAlexey Budankov 		if (err > 0) {
2137ec886bf5SAlexey Budankov 			switch (*cmd) {
2138ec886bf5SAlexey Budankov 			case EVLIST_CTL_CMD_ENABLE:
2139ec886bf5SAlexey Budankov 			case EVLIST_CTL_CMD_DISABLE:
2140991ae4ebSJiri Olsa 				err = evlist__ctlfd_enable(evlist, cmd_data,
2141991ae4ebSJiri Olsa 							   *cmd == EVLIST_CTL_CMD_ENABLE);
2142ec886bf5SAlexey Budankov 				break;
2143142544a9SJiri Olsa 			case EVLIST_CTL_CMD_EVLIST:
2144142544a9SJiri Olsa 				err = evlist__ctlfd_list(evlist, cmd_data);
2145142544a9SJiri Olsa 				break;
2146d20aff15SAdrian Hunter 			case EVLIST_CTL_CMD_SNAPSHOT:
2147f186cd61SJiri Olsa 			case EVLIST_CTL_CMD_STOP:
214847fddcb4SJiri Olsa 			case EVLIST_CTL_CMD_PING:
2149d20aff15SAdrian Hunter 				break;
2150ec886bf5SAlexey Budankov 			case EVLIST_CTL_CMD_ACK:
2151ec886bf5SAlexey Budankov 			case EVLIST_CTL_CMD_UNSUPPORTED:
2152ec886bf5SAlexey Budankov 			default:
2153ec886bf5SAlexey Budankov 				pr_debug("ctlfd: unsupported %d\n", *cmd);
2154ec886bf5SAlexey Budankov 				break;
2155ec886bf5SAlexey Budankov 			}
2156d20aff15SAdrian Hunter 			if (!(*cmd == EVLIST_CTL_CMD_ACK || *cmd == EVLIST_CTL_CMD_UNSUPPORTED ||
2157d20aff15SAdrian Hunter 			      *cmd == EVLIST_CTL_CMD_SNAPSHOT))
2158ec886bf5SAlexey Budankov 				evlist__ctlfd_ack(evlist);
2159ec886bf5SAlexey Budankov 		}
2160ec886bf5SAlexey Budankov 	}
2161ec886bf5SAlexey Budankov 
2162ec886bf5SAlexey Budankov 	if (entries[ctlfd_pos].revents & (POLLHUP | POLLERR))
2163ec886bf5SAlexey Budankov 		evlist__finalize_ctlfd(evlist);
2164ec886bf5SAlexey Budankov 	else
2165ec886bf5SAlexey Budankov 		entries[ctlfd_pos].revents = 0;
2166ec886bf5SAlexey Budankov 
2167ec886bf5SAlexey Budankov 	return err;
2168ec886bf5SAlexey Budankov }
2169b214ba8cSNamhyung Kim 
2170b214ba8cSNamhyung Kim struct evsel *evlist__find_evsel(struct evlist *evlist, int idx)
2171b214ba8cSNamhyung Kim {
2172b214ba8cSNamhyung Kim 	struct evsel *evsel;
2173b214ba8cSNamhyung Kim 
2174b214ba8cSNamhyung Kim 	evlist__for_each_entry(evlist, evsel) {
217538fe0e01SJiri Olsa 		if (evsel->core.idx == idx)
2176b214ba8cSNamhyung Kim 			return evsel;
2177b214ba8cSNamhyung Kim 	}
2178b214ba8cSNamhyung Kim 	return NULL;
2179b214ba8cSNamhyung Kim }
21809865ea8aSArnaldo Carvalho de Melo 
21819865ea8aSArnaldo Carvalho de Melo int evlist__scnprintf_evsels(struct evlist *evlist, size_t size, char *bf)
21829865ea8aSArnaldo Carvalho de Melo {
21839865ea8aSArnaldo Carvalho de Melo 	struct evsel *evsel;
21849865ea8aSArnaldo Carvalho de Melo 	int printed = 0;
21859865ea8aSArnaldo Carvalho de Melo 
21869865ea8aSArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, evsel) {
21879865ea8aSArnaldo Carvalho de Melo 		if (evsel__is_dummy_event(evsel))
21889865ea8aSArnaldo Carvalho de Melo 			continue;
21899865ea8aSArnaldo Carvalho de Melo 		if (size > (strlen(evsel__name(evsel)) + (printed ? 2 : 1))) {
21909865ea8aSArnaldo Carvalho de Melo 			printed += scnprintf(bf + printed, size - printed, "%s%s", printed ? "," : "", evsel__name(evsel));
21919865ea8aSArnaldo Carvalho de Melo 		} else {
21929865ea8aSArnaldo Carvalho de Melo 			printed += scnprintf(bf + printed, size - printed, "%s...", printed ? "," : "");
21939865ea8aSArnaldo Carvalho de Melo 			break;
21949865ea8aSArnaldo Carvalho de Melo 		}
21959865ea8aSArnaldo Carvalho de Melo 	}
21969865ea8aSArnaldo Carvalho de Melo 
21979865ea8aSArnaldo Carvalho de Melo 	return printed;
21989865ea8aSArnaldo Carvalho de Melo }
2199d5a8bd0fSJin Yao 
2200d5a8bd0fSJin Yao void evlist__check_mem_load_aux(struct evlist *evlist)
2201d5a8bd0fSJin Yao {
2202d5a8bd0fSJin Yao 	struct evsel *leader, *evsel, *pos;
2203d5a8bd0fSJin Yao 
2204d5a8bd0fSJin Yao 	/*
2205d5a8bd0fSJin Yao 	 * For some platforms, the 'mem-loads' event is required to use
2206d5a8bd0fSJin Yao 	 * together with 'mem-loads-aux' within a group and 'mem-loads-aux'
2207d5a8bd0fSJin Yao 	 * must be the group leader. Now we disable this group before reporting
2208d5a8bd0fSJin Yao 	 * because 'mem-loads-aux' is just an auxiliary event. It doesn't carry
2209d5a8bd0fSJin Yao 	 * any valid memory load information.
2210d5a8bd0fSJin Yao 	 */
2211d5a8bd0fSJin Yao 	evlist__for_each_entry(evlist, evsel) {
2212fba7c866SJiri Olsa 		leader = evsel__leader(evsel);
2213d5a8bd0fSJin Yao 		if (leader == evsel)
2214d5a8bd0fSJin Yao 			continue;
2215d5a8bd0fSJin Yao 
2216d5a8bd0fSJin Yao 		if (leader->name && strstr(leader->name, "mem-loads-aux")) {
2217d5a8bd0fSJin Yao 			for_each_group_evsel(pos, leader) {
2218fba7c866SJiri Olsa 				evsel__set_leader(pos, pos);
2219d5a8bd0fSJin Yao 				pos->core.nr_members = 0;
2220d5a8bd0fSJin Yao 			}
2221d5a8bd0fSJin Yao 		}
2222d5a8bd0fSJin Yao 	}
2223d5a8bd0fSJin Yao }
2224