xref: /openbmc/linux/tools/perf/util/evlist.c (revision 472832d2)
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 
245e251abeeSArnaldo Carvalho de Melo int evlist__add_dummy(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 	};
2528f6725a2SArnaldo Carvalho de Melo 	struct evsel *evsel = evsel__new_idx(&attr, evlist->core.nr_entries);
2535bae0250SArnaldo Carvalho de Melo 
2545bae0250SArnaldo Carvalho de Melo 	if (evsel == NULL)
2555bae0250SArnaldo Carvalho de Melo 		return -ENOMEM;
2565bae0250SArnaldo Carvalho de Melo 
257a1cf3a75SJiri Olsa 	evlist__add(evlist, evsel);
2585bae0250SArnaldo Carvalho de Melo 	return 0;
2595bae0250SArnaldo Carvalho de Melo }
2605bae0250SArnaldo Carvalho de Melo 
261e251abeeSArnaldo Carvalho de Melo static int evlist__add_attrs(struct evlist *evlist, struct perf_event_attr *attrs, size_t nr_attrs)
26250d08e47SArnaldo Carvalho de Melo {
26332dcd021SJiri Olsa 	struct evsel *evsel, *n;
26450d08e47SArnaldo Carvalho de Melo 	LIST_HEAD(head);
26550d08e47SArnaldo Carvalho de Melo 	size_t i;
26650d08e47SArnaldo Carvalho de Melo 
26750d08e47SArnaldo Carvalho de Melo 	for (i = 0; i < nr_attrs; i++) {
2688f6725a2SArnaldo Carvalho de Melo 		evsel = evsel__new_idx(attrs + i, evlist->core.nr_entries + i);
26950d08e47SArnaldo Carvalho de Melo 		if (evsel == NULL)
27050d08e47SArnaldo Carvalho de Melo 			goto out_delete_partial_list;
271b27c4eceSJiri Olsa 		list_add_tail(&evsel->core.node, &head);
27250d08e47SArnaldo Carvalho de Melo 	}
27350d08e47SArnaldo Carvalho de Melo 
274e414fd1aSArnaldo Carvalho de Melo 	evlist__splice_list_tail(evlist, &head);
27550d08e47SArnaldo Carvalho de Melo 
27650d08e47SArnaldo Carvalho de Melo 	return 0;
27750d08e47SArnaldo Carvalho de Melo 
27850d08e47SArnaldo Carvalho de Melo out_delete_partial_list:
279e5cadb93SArnaldo Carvalho de Melo 	__evlist__for_each_entry_safe(&head, n, evsel)
2805eb2dd2aSJiri Olsa 		evsel__delete(evsel);
28150d08e47SArnaldo Carvalho de Melo 	return -1;
28250d08e47SArnaldo Carvalho de Melo }
28350d08e47SArnaldo Carvalho de Melo 
284e251abeeSArnaldo Carvalho de Melo int __evlist__add_default_attrs(struct evlist *evlist, struct perf_event_attr *attrs, size_t nr_attrs)
28579695e1bSArnaldo Carvalho de Melo {
28679695e1bSArnaldo Carvalho de Melo 	size_t i;
28779695e1bSArnaldo Carvalho de Melo 
28879695e1bSArnaldo Carvalho de Melo 	for (i = 0; i < nr_attrs; i++)
28979695e1bSArnaldo Carvalho de Melo 		event_attr_init(attrs + i);
29079695e1bSArnaldo Carvalho de Melo 
291a1cf3a75SJiri Olsa 	return evlist__add_attrs(evlist, attrs, nr_attrs);
29279695e1bSArnaldo Carvalho de Melo }
29379695e1bSArnaldo Carvalho de Melo 
29442641d6fSKan Liang __weak int arch_evlist__add_default_attrs(struct evlist *evlist __maybe_unused)
29542641d6fSKan Liang {
29642641d6fSKan Liang 	return 0;
29742641d6fSKan Liang }
29842641d6fSKan Liang 
299b02736f7SArnaldo Carvalho de Melo struct evsel *evlist__find_tracepoint_by_id(struct evlist *evlist, int id)
300ee29be62SArnaldo Carvalho de Melo {
30132dcd021SJiri Olsa 	struct evsel *evsel;
302ee29be62SArnaldo Carvalho de Melo 
303e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, evsel) {
3041fc632ceSJiri Olsa 		if (evsel->core.attr.type   == PERF_TYPE_TRACEPOINT &&
3051fc632ceSJiri Olsa 		    (int)evsel->core.attr.config == id)
306ee29be62SArnaldo Carvalho de Melo 			return evsel;
307ee29be62SArnaldo Carvalho de Melo 	}
308ee29be62SArnaldo Carvalho de Melo 
309ee29be62SArnaldo Carvalho de Melo 	return NULL;
310ee29be62SArnaldo Carvalho de Melo }
311ee29be62SArnaldo Carvalho de Melo 
312b02736f7SArnaldo Carvalho de Melo struct evsel *evlist__find_tracepoint_by_name(struct evlist *evlist, const char *name)
313a2f2804aSDavid Ahern {
31432dcd021SJiri Olsa 	struct evsel *evsel;
315a2f2804aSDavid Ahern 
316e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, evsel) {
3171fc632ceSJiri Olsa 		if ((evsel->core.attr.type == PERF_TYPE_TRACEPOINT) &&
318a2f2804aSDavid Ahern 		    (strcmp(evsel->name, name) == 0))
319a2f2804aSDavid Ahern 			return evsel;
320a2f2804aSDavid Ahern 	}
321a2f2804aSDavid Ahern 
322a2f2804aSDavid Ahern 	return NULL;
323a2f2804aSDavid Ahern }
324a2f2804aSDavid Ahern 
325e251abeeSArnaldo Carvalho de Melo int evlist__add_newtp(struct evlist *evlist, const char *sys, const char *name, void *handler)
32639876e7dSArnaldo Carvalho de Melo {
3278f6725a2SArnaldo Carvalho de Melo 	struct evsel *evsel = evsel__newtp(sys, name);
32839876e7dSArnaldo Carvalho de Melo 
3298dd2a131SJiri Olsa 	if (IS_ERR(evsel))
33039876e7dSArnaldo Carvalho de Melo 		return -1;
33139876e7dSArnaldo Carvalho de Melo 
332744a9719SArnaldo Carvalho de Melo 	evsel->handler = handler;
333a1cf3a75SJiri Olsa 	evlist__add(evlist, evsel);
33439876e7dSArnaldo Carvalho de Melo 	return 0;
33539876e7dSArnaldo Carvalho de Melo }
33639876e7dSArnaldo Carvalho de Melo 
337f63c2f5aSArnaldo Carvalho de Melo static int evlist__nr_threads(struct evlist *evlist, struct evsel *evsel)
338bf8e8f4bSAdrian Hunter {
339648b5af3SJiri Olsa 	if (evsel->core.system_wide)
340bf8e8f4bSAdrian Hunter 		return 1;
341bf8e8f4bSAdrian Hunter 	else
342a2f354e3SJiri Olsa 		return perf_thread_map__nr(evlist->core.threads);
343bf8e8f4bSAdrian Hunter }
344bf8e8f4bSAdrian Hunter 
345*472832d2SIan Rogers struct evlist_cpu_iterator evlist__cpu_begin(struct evlist *evlist, struct affinity *affinity)
346a8cbe40fSAndi Kleen {
347*472832d2SIan Rogers 	struct evlist_cpu_iterator itr = {
348*472832d2SIan Rogers 		.container = evlist,
349*472832d2SIan Rogers 		.evsel = evlist__first(evlist),
350*472832d2SIan Rogers 		.cpu_map_idx = 0,
351*472832d2SIan Rogers 		.evlist_cpu_map_idx = 0,
352*472832d2SIan Rogers 		.evlist_cpu_map_nr = perf_cpu_map__nr(evlist->core.all_cpus),
353*472832d2SIan Rogers 		.cpu = -1,
354*472832d2SIan Rogers 		.affinity = affinity,
355*472832d2SIan Rogers 	};
356a8cbe40fSAndi Kleen 
357*472832d2SIan Rogers 	if (itr.affinity) {
358*472832d2SIan Rogers 		itr.cpu = perf_cpu_map__cpu(evlist->core.all_cpus, 0);
359*472832d2SIan Rogers 		affinity__set(itr.affinity, itr.cpu);
360*472832d2SIan Rogers 		itr.cpu_map_idx = perf_cpu_map__idx(itr.evsel->core.cpus, itr.cpu);
361a8cbe40fSAndi Kleen 		/*
362*472832d2SIan Rogers 		 * If this CPU isn't in the evsel's cpu map then advance through
363*472832d2SIan Rogers 		 * the list.
364a8cbe40fSAndi Kleen 		 */
365*472832d2SIan Rogers 		if (itr.cpu_map_idx == -1)
366*472832d2SIan Rogers 			evlist_cpu_iterator__next(&itr);
367*472832d2SIan Rogers 	}
368*472832d2SIan Rogers 	return itr;
369a8cbe40fSAndi Kleen }
370a8cbe40fSAndi Kleen 
371*472832d2SIan Rogers void evlist_cpu_iterator__next(struct evlist_cpu_iterator *evlist_cpu_itr)
372a8cbe40fSAndi Kleen {
373*472832d2SIan Rogers 	while (evlist_cpu_itr->evsel != evlist__last(evlist_cpu_itr->container)) {
374*472832d2SIan Rogers 		evlist_cpu_itr->evsel = evsel__next(evlist_cpu_itr->evsel);
375*472832d2SIan Rogers 		evlist_cpu_itr->cpu_map_idx =
376*472832d2SIan Rogers 			perf_cpu_map__idx(evlist_cpu_itr->evsel->core.cpus,
377*472832d2SIan Rogers 					  evlist_cpu_itr->cpu);
378*472832d2SIan Rogers 		if (evlist_cpu_itr->cpu_map_idx != -1)
379*472832d2SIan Rogers 			return;
380*472832d2SIan Rogers 	}
381*472832d2SIan Rogers 	evlist_cpu_itr->evlist_cpu_map_idx++;
382*472832d2SIan Rogers 	if (evlist_cpu_itr->evlist_cpu_map_idx < evlist_cpu_itr->evlist_cpu_map_nr) {
383*472832d2SIan Rogers 		evlist_cpu_itr->evsel = evlist__first(evlist_cpu_itr->container);
384*472832d2SIan Rogers 		evlist_cpu_itr->cpu =
385*472832d2SIan Rogers 			perf_cpu_map__cpu(evlist_cpu_itr->container->core.all_cpus,
386*472832d2SIan Rogers 					  evlist_cpu_itr->evlist_cpu_map_idx);
387*472832d2SIan Rogers 		if (evlist_cpu_itr->affinity)
388*472832d2SIan Rogers 			affinity__set(evlist_cpu_itr->affinity, evlist_cpu_itr->cpu);
389*472832d2SIan Rogers 		evlist_cpu_itr->cpu_map_idx =
390*472832d2SIan Rogers 			perf_cpu_map__idx(evlist_cpu_itr->evsel->core.cpus,
391*472832d2SIan Rogers 					  evlist_cpu_itr->cpu);
392*472832d2SIan Rogers 		/*
393*472832d2SIan Rogers 		 * If this CPU isn't in the evsel's cpu map then advance through
394*472832d2SIan Rogers 		 * the list.
395*472832d2SIan Rogers 		 */
396*472832d2SIan Rogers 		if (evlist_cpu_itr->cpu_map_idx == -1)
397*472832d2SIan Rogers 			evlist_cpu_iterator__next(evlist_cpu_itr);
398*472832d2SIan Rogers 	}
399a8cbe40fSAndi Kleen }
400a8cbe40fSAndi Kleen 
401*472832d2SIan Rogers bool evlist_cpu_iterator__end(const struct evlist_cpu_iterator *evlist_cpu_itr)
402a8cbe40fSAndi Kleen {
403*472832d2SIan Rogers 	return evlist_cpu_itr->evlist_cpu_map_idx >= evlist_cpu_itr->evlist_cpu_map_nr;
404a8cbe40fSAndi Kleen }
405a8cbe40fSAndi Kleen 
4067cfcd1e0SJiri Olsa static int evsel__strcmp(struct evsel *pos, char *evsel_name)
4077cfcd1e0SJiri Olsa {
4087cfcd1e0SJiri Olsa 	if (!evsel_name)
4097cfcd1e0SJiri Olsa 		return 0;
4107cfcd1e0SJiri Olsa 	if (evsel__is_dummy_event(pos))
4117cfcd1e0SJiri Olsa 		return 1;
4127cfcd1e0SJiri Olsa 	return strcmp(pos->name, evsel_name);
4137cfcd1e0SJiri Olsa }
4147cfcd1e0SJiri Olsa 
4157cfcd1e0SJiri Olsa static int evlist__is_enabled(struct evlist *evlist)
4167cfcd1e0SJiri Olsa {
4177cfcd1e0SJiri Olsa 	struct evsel *pos;
4187cfcd1e0SJiri Olsa 
4197cfcd1e0SJiri Olsa 	evlist__for_each_entry(evlist, pos) {
4207cfcd1e0SJiri Olsa 		if (!evsel__is_group_leader(pos) || !pos->core.fd)
4217cfcd1e0SJiri Olsa 			continue;
4227cfcd1e0SJiri Olsa 		/* If at least one event is enabled, evlist is enabled. */
4237cfcd1e0SJiri Olsa 		if (!pos->disabled)
4247cfcd1e0SJiri Olsa 			return true;
4257cfcd1e0SJiri Olsa 	}
4267cfcd1e0SJiri Olsa 	return false;
4277cfcd1e0SJiri Olsa }
4287cfcd1e0SJiri Olsa 
4297cfcd1e0SJiri Olsa static void __evlist__disable(struct evlist *evlist, char *evsel_name)
4304152ab37SArnaldo Carvalho de Melo {
43132dcd021SJiri Olsa 	struct evsel *pos;
432*472832d2SIan Rogers 	struct evlist_cpu_iterator evlist_cpu_itr;
433704e2f5bSAndi Kleen 	struct affinity affinity;
43487cf8360SAdrian Hunter 	bool has_imm = false;
435704e2f5bSAndi Kleen 
436704e2f5bSAndi Kleen 	if (affinity__setup(&affinity) < 0)
437704e2f5bSAndi Kleen 		return;
438704e2f5bSAndi Kleen 
43987cf8360SAdrian Hunter 	/* Disable 'immediate' events last */
440*472832d2SIan Rogers 	for (int imm = 0; imm <= 1; imm++) {
441*472832d2SIan Rogers 		evlist__for_each_cpu(evlist_cpu_itr, evlist, &affinity) {
442*472832d2SIan Rogers 			pos = evlist_cpu_itr.evsel;
4437cfcd1e0SJiri Olsa 			if (evsel__strcmp(pos, evsel_name))
4447cfcd1e0SJiri Olsa 				continue;
445c754c382SArnaldo Carvalho de Melo 			if (pos->disabled || !evsel__is_group_leader(pos) || !pos->core.fd)
4463fe4430dSJiri Olsa 				continue;
44787cf8360SAdrian Hunter 			if (pos->immediate)
44887cf8360SAdrian Hunter 				has_imm = true;
44987cf8360SAdrian Hunter 			if (pos->immediate != imm)
45087cf8360SAdrian Hunter 				continue;
451*472832d2SIan Rogers 			evsel__disable_cpu(pos, evlist_cpu_itr.cpu_map_idx);
452704e2f5bSAndi Kleen 		}
45387cf8360SAdrian Hunter 		if (!has_imm)
45487cf8360SAdrian Hunter 			break;
45587cf8360SAdrian Hunter 	}
45687cf8360SAdrian Hunter 
457704e2f5bSAndi Kleen 	affinity__cleanup(&affinity);
458704e2f5bSAndi Kleen 	evlist__for_each_entry(evlist, pos) {
4597cfcd1e0SJiri Olsa 		if (evsel__strcmp(pos, evsel_name))
4607cfcd1e0SJiri Olsa 			continue;
461c754c382SArnaldo Carvalho de Melo 		if (!evsel__is_group_leader(pos) || !pos->core.fd)
462704e2f5bSAndi Kleen 			continue;
463704e2f5bSAndi Kleen 		pos->disabled = true;
4644152ab37SArnaldo Carvalho de Melo 	}
4652b56bcfbSArnaldo Carvalho de Melo 
4667cfcd1e0SJiri Olsa 	/*
4677cfcd1e0SJiri Olsa 	 * If we disabled only single event, we need to check
4687cfcd1e0SJiri Olsa 	 * the enabled state of the evlist manually.
4697cfcd1e0SJiri Olsa 	 */
4707cfcd1e0SJiri Olsa 	if (evsel_name)
4717cfcd1e0SJiri Olsa 		evlist->enabled = evlist__is_enabled(evlist);
4727cfcd1e0SJiri Olsa 	else
4732b56bcfbSArnaldo Carvalho de Melo 		evlist->enabled = false;
4744152ab37SArnaldo Carvalho de Melo }
4754152ab37SArnaldo Carvalho de Melo 
4767cfcd1e0SJiri Olsa void evlist__disable(struct evlist *evlist)
4777cfcd1e0SJiri Olsa {
4787cfcd1e0SJiri Olsa 	__evlist__disable(evlist, NULL);
4797cfcd1e0SJiri Olsa }
4807cfcd1e0SJiri Olsa 
4817cfcd1e0SJiri Olsa void evlist__disable_evsel(struct evlist *evlist, char *evsel_name)
4827cfcd1e0SJiri Olsa {
4837cfcd1e0SJiri Olsa 	__evlist__disable(evlist, evsel_name);
4847cfcd1e0SJiri Olsa }
4857cfcd1e0SJiri Olsa 
4867cfcd1e0SJiri Olsa static void __evlist__enable(struct evlist *evlist, char *evsel_name)
487764e16a3SDavid Ahern {
48832dcd021SJiri Olsa 	struct evsel *pos;
489*472832d2SIan Rogers 	struct evlist_cpu_iterator evlist_cpu_itr;
490704e2f5bSAndi Kleen 	struct affinity affinity;
491764e16a3SDavid Ahern 
492704e2f5bSAndi Kleen 	if (affinity__setup(&affinity) < 0)
493704e2f5bSAndi Kleen 		return;
494704e2f5bSAndi Kleen 
495*472832d2SIan Rogers 	evlist__for_each_cpu(evlist_cpu_itr, evlist, &affinity) {
496*472832d2SIan Rogers 		pos = evlist_cpu_itr.evsel;
4977cfcd1e0SJiri Olsa 		if (evsel__strcmp(pos, evsel_name))
4987cfcd1e0SJiri Olsa 			continue;
499c754c382SArnaldo Carvalho de Melo 		if (!evsel__is_group_leader(pos) || !pos->core.fd)
500704e2f5bSAndi Kleen 			continue;
501*472832d2SIan Rogers 		evsel__enable_cpu(pos, evlist_cpu_itr.cpu_map_idx);
502704e2f5bSAndi Kleen 	}
503704e2f5bSAndi Kleen 	affinity__cleanup(&affinity);
504e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, pos) {
5057cfcd1e0SJiri Olsa 		if (evsel__strcmp(pos, evsel_name))
5067cfcd1e0SJiri Olsa 			continue;
507c754c382SArnaldo Carvalho de Melo 		if (!evsel__is_group_leader(pos) || !pos->core.fd)
5083fe4430dSJiri Olsa 			continue;
509704e2f5bSAndi Kleen 		pos->disabled = false;
510764e16a3SDavid Ahern 	}
5112b56bcfbSArnaldo Carvalho de Melo 
5127cfcd1e0SJiri Olsa 	/*
5137cfcd1e0SJiri Olsa 	 * Even single event sets the 'enabled' for evlist,
5147cfcd1e0SJiri Olsa 	 * so the toggle can work properly and toggle to
5157cfcd1e0SJiri Olsa 	 * 'disabled' state.
5167cfcd1e0SJiri Olsa 	 */
5172b56bcfbSArnaldo Carvalho de Melo 	evlist->enabled = true;
5182b56bcfbSArnaldo Carvalho de Melo }
5192b56bcfbSArnaldo Carvalho de Melo 
5207cfcd1e0SJiri Olsa void evlist__enable(struct evlist *evlist)
5217cfcd1e0SJiri Olsa {
5227cfcd1e0SJiri Olsa 	__evlist__enable(evlist, NULL);
5237cfcd1e0SJiri Olsa }
5247cfcd1e0SJiri Olsa 
5257cfcd1e0SJiri Olsa void evlist__enable_evsel(struct evlist *evlist, char *evsel_name)
5267cfcd1e0SJiri Olsa {
5277cfcd1e0SJiri Olsa 	__evlist__enable(evlist, evsel_name);
5287cfcd1e0SJiri Olsa }
5297cfcd1e0SJiri Olsa 
530ade9d208SArnaldo Carvalho de Melo void evlist__toggle_enable(struct evlist *evlist)
5312b56bcfbSArnaldo Carvalho de Melo {
532e74676deSJiri Olsa 	(evlist->enabled ? evlist__disable : evlist__enable)(evlist);
533764e16a3SDavid Ahern }
534764e16a3SDavid Ahern 
53537b01abeSArnaldo Carvalho de Melo static int evlist__enable_event_cpu(struct evlist *evlist, struct evsel *evsel, int cpu)
5361c65056cSAdrian Hunter {
53718ef15c6SArnaldo Carvalho de Melo 	int thread;
538f63c2f5aSArnaldo Carvalho de Melo 	int nr_threads = evlist__nr_threads(evlist, evsel);
5391c65056cSAdrian Hunter 
5409dfcb759SJiri Olsa 	if (!evsel->core.fd)
5411c65056cSAdrian Hunter 		return -EINVAL;
5421c65056cSAdrian Hunter 
5431c65056cSAdrian Hunter 	for (thread = 0; thread < nr_threads; thread++) {
54418ef15c6SArnaldo Carvalho de Melo 		int err = ioctl(FD(evsel, cpu, thread), PERF_EVENT_IOC_ENABLE, 0);
5451c65056cSAdrian Hunter 		if (err)
5461c65056cSAdrian Hunter 			return err;
5471c65056cSAdrian Hunter 	}
5481c65056cSAdrian Hunter 	return 0;
5491c65056cSAdrian Hunter }
5501c65056cSAdrian Hunter 
55137b01abeSArnaldo Carvalho de Melo static int evlist__enable_event_thread(struct evlist *evlist, struct evsel *evsel, int thread)
5521c65056cSAdrian Hunter {
55318ef15c6SArnaldo Carvalho de Melo 	int cpu;
5546549cd8fSJiri Olsa 	int nr_cpus = perf_cpu_map__nr(evlist->core.cpus);
5551c65056cSAdrian Hunter 
5569dfcb759SJiri Olsa 	if (!evsel->core.fd)
5571c65056cSAdrian Hunter 		return -EINVAL;
5581c65056cSAdrian Hunter 
5591c65056cSAdrian Hunter 	for (cpu = 0; cpu < nr_cpus; cpu++) {
56018ef15c6SArnaldo Carvalho de Melo 		int err = ioctl(FD(evsel, cpu, thread), PERF_EVENT_IOC_ENABLE, 0);
5611c65056cSAdrian Hunter 		if (err)
5621c65056cSAdrian Hunter 			return err;
5631c65056cSAdrian Hunter 	}
5641c65056cSAdrian Hunter 	return 0;
5651c65056cSAdrian Hunter }
5661c65056cSAdrian Hunter 
56737b01abeSArnaldo Carvalho de Melo int evlist__enable_event_idx(struct evlist *evlist, struct evsel *evsel, int idx)
5681c65056cSAdrian Hunter {
569315c0a1fSJiri Olsa 	bool per_cpu_mmaps = !perf_cpu_map__empty(evlist->core.cpus);
5701c65056cSAdrian Hunter 
5711c65056cSAdrian Hunter 	if (per_cpu_mmaps)
57237b01abeSArnaldo Carvalho de Melo 		return evlist__enable_event_cpu(evlist, evsel, idx);
57337b01abeSArnaldo Carvalho de Melo 
57437b01abeSArnaldo Carvalho de Melo 	return evlist__enable_event_thread(evlist, evsel, idx);
5751c65056cSAdrian Hunter }
5761c65056cSAdrian Hunter 
577f4009e7bSJiri Olsa int evlist__add_pollfd(struct evlist *evlist, int fd)
578e4b356b5SArnaldo Carvalho de Melo {
579ab4c1f9fSAlexey Budankov 	return perf_evlist__add_pollfd(&evlist->core, fd, NULL, POLLIN, fdarray_flag__default);
580e4b356b5SArnaldo Carvalho de Melo }
5811b85337dSArnaldo Carvalho de Melo 
582f4009e7bSJiri Olsa int evlist__filter_pollfd(struct evlist *evlist, short revents_and_mask)
5831ddec7f0SArnaldo Carvalho de Melo {
58484227cb1SJiri Olsa 	return perf_evlist__filter_pollfd(&evlist->core, revents_and_mask);
5851ddec7f0SArnaldo Carvalho de Melo }
5861ddec7f0SArnaldo Carvalho de Melo 
587e16c2ce7SYang Jihong #ifdef HAVE_EVENTFD_SUPPORT
588e16c2ce7SYang Jihong int evlist__add_wakeup_eventfd(struct evlist *evlist, int fd)
589e16c2ce7SYang Jihong {
590e16c2ce7SYang Jihong 	return perf_evlist__add_pollfd(&evlist->core, fd, NULL, POLLIN,
591e16c2ce7SYang Jihong 				       fdarray_flag__nonfilterable);
592e16c2ce7SYang Jihong }
593e16c2ce7SYang Jihong #endif
594e16c2ce7SYang Jihong 
59580ab2987SJiri Olsa int evlist__poll(struct evlist *evlist, int timeout)
596f66a889dSArnaldo Carvalho de Melo {
59780ab2987SJiri Olsa 	return perf_evlist__poll(&evlist->core, timeout);
598f66a889dSArnaldo Carvalho de Melo }
599f66a889dSArnaldo Carvalho de Melo 
6003ccf8a7bSArnaldo Carvalho de Melo struct perf_sample_id *evlist__id2sid(struct evlist *evlist, u64 id)
60170db7533SArnaldo Carvalho de Melo {
60270db7533SArnaldo Carvalho de Melo 	struct hlist_head *head;
60370db7533SArnaldo Carvalho de Melo 	struct perf_sample_id *sid;
60470db7533SArnaldo Carvalho de Melo 	int hash;
60570db7533SArnaldo Carvalho de Melo 
60670db7533SArnaldo Carvalho de Melo 	hash = hash_64(id, PERF_EVLIST__HLIST_BITS);
6071d5af02dSJiri Olsa 	head = &evlist->core.heads[hash];
60870db7533SArnaldo Carvalho de Melo 
609b67bfe0dSSasha Levin 	hlist_for_each_entry(sid, head, node)
61070db7533SArnaldo Carvalho de Melo 		if (sid->id == id)
611932a3594SJiri Olsa 			return sid;
612932a3594SJiri Olsa 
613932a3594SJiri Olsa 	return NULL;
614932a3594SJiri Olsa }
615932a3594SJiri Olsa 
6163ccf8a7bSArnaldo Carvalho de Melo struct evsel *evlist__id2evsel(struct evlist *evlist, u64 id)
617932a3594SJiri Olsa {
618932a3594SJiri Olsa 	struct perf_sample_id *sid;
619932a3594SJiri Olsa 
6206484d2f9SJiri Olsa 	if (evlist->core.nr_entries == 1 || !id)
621515dbe48SJiri Olsa 		return evlist__first(evlist);
622932a3594SJiri Olsa 
6233ccf8a7bSArnaldo Carvalho de Melo 	sid = evlist__id2sid(evlist, id);
624932a3594SJiri Olsa 	if (sid)
62570c20369SJiri Olsa 		return container_of(sid->evsel, struct evsel, core);
62630e68bccSNamhyung Kim 
6278cedf3a5SArnaldo Carvalho de Melo 	if (!evlist__sample_id_all(evlist))
628515dbe48SJiri Olsa 		return evlist__first(evlist);
62930e68bccSNamhyung Kim 
63070db7533SArnaldo Carvalho de Melo 	return NULL;
63170db7533SArnaldo Carvalho de Melo }
63204391debSArnaldo Carvalho de Melo 
6333ccf8a7bSArnaldo Carvalho de Melo struct evsel *evlist__id2evsel_strict(struct evlist *evlist, u64 id)
634dddcf6abSAdrian Hunter {
635dddcf6abSAdrian Hunter 	struct perf_sample_id *sid;
636dddcf6abSAdrian Hunter 
637dddcf6abSAdrian Hunter 	if (!id)
638dddcf6abSAdrian Hunter 		return NULL;
639dddcf6abSAdrian Hunter 
6403ccf8a7bSArnaldo Carvalho de Melo 	sid = evlist__id2sid(evlist, id);
641dddcf6abSAdrian Hunter 	if (sid)
64270c20369SJiri Olsa 		return container_of(sid->evsel, struct evsel, core);
643dddcf6abSAdrian Hunter 
644dddcf6abSAdrian Hunter 	return NULL;
645dddcf6abSAdrian Hunter }
646dddcf6abSAdrian Hunter 
6473ccf8a7bSArnaldo Carvalho de Melo static int evlist__event2id(struct evlist *evlist, union perf_event *event, u64 *id)
64875562573SAdrian Hunter {
649b1fcd190SJiri Olsa 	const __u64 *array = event->sample.array;
65075562573SAdrian Hunter 	ssize_t n;
65175562573SAdrian Hunter 
65275562573SAdrian Hunter 	n = (event->header.size - sizeof(event->header)) >> 3;
65375562573SAdrian Hunter 
65475562573SAdrian Hunter 	if (event->header.type == PERF_RECORD_SAMPLE) {
65575562573SAdrian Hunter 		if (evlist->id_pos >= n)
65675562573SAdrian Hunter 			return -1;
65775562573SAdrian Hunter 		*id = array[evlist->id_pos];
65875562573SAdrian Hunter 	} else {
65975562573SAdrian Hunter 		if (evlist->is_pos > n)
66075562573SAdrian Hunter 			return -1;
66175562573SAdrian Hunter 		n -= evlist->is_pos;
66275562573SAdrian Hunter 		*id = array[n];
66375562573SAdrian Hunter 	}
66475562573SAdrian Hunter 	return 0;
66575562573SAdrian Hunter }
66675562573SAdrian Hunter 
6673ccf8a7bSArnaldo Carvalho de Melo struct evsel *evlist__event2evsel(struct evlist *evlist, union perf_event *event)
66875562573SAdrian Hunter {
669515dbe48SJiri Olsa 	struct evsel *first = evlist__first(evlist);
67075562573SAdrian Hunter 	struct hlist_head *head;
67175562573SAdrian Hunter 	struct perf_sample_id *sid;
67275562573SAdrian Hunter 	int hash;
67375562573SAdrian Hunter 	u64 id;
67475562573SAdrian Hunter 
6756484d2f9SJiri Olsa 	if (evlist->core.nr_entries == 1)
67698be6966SAdrian Hunter 		return first;
67798be6966SAdrian Hunter 
6781fc632ceSJiri Olsa 	if (!first->core.attr.sample_id_all &&
67998be6966SAdrian Hunter 	    event->header.type != PERF_RECORD_SAMPLE)
68098be6966SAdrian Hunter 		return first;
68175562573SAdrian Hunter 
6823ccf8a7bSArnaldo Carvalho de Melo 	if (evlist__event2id(evlist, event, &id))
68375562573SAdrian Hunter 		return NULL;
68475562573SAdrian Hunter 
68575562573SAdrian Hunter 	/* Synthesized events have an id of zero */
68675562573SAdrian Hunter 	if (!id)
68798be6966SAdrian Hunter 		return first;
68875562573SAdrian Hunter 
68975562573SAdrian Hunter 	hash = hash_64(id, PERF_EVLIST__HLIST_BITS);
6901d5af02dSJiri Olsa 	head = &evlist->core.heads[hash];
69175562573SAdrian Hunter 
69275562573SAdrian Hunter 	hlist_for_each_entry(sid, head, node) {
69375562573SAdrian Hunter 		if (sid->id == id)
69470c20369SJiri Olsa 			return container_of(sid->evsel, struct evsel, core);
69575562573SAdrian Hunter 	}
69675562573SAdrian Hunter 	return NULL;
69775562573SAdrian Hunter }
69875562573SAdrian Hunter 
6990a60b339SArnaldo Carvalho de Melo static int evlist__set_paused(struct evlist *evlist, bool value)
70065aea233SWang Nan {
70165aea233SWang Nan 	int i;
70265aea233SWang Nan 
7030b72d69aSWang Nan 	if (!evlist->overwrite_mmap)
704078c3386SWang Nan 		return 0;
705078c3386SWang Nan 
706c976ee11SJiri Olsa 	for (i = 0; i < evlist->core.nr_mmaps; i++) {
7072cf07b29SJiri Olsa 		int fd = evlist->overwrite_mmap[i].core.fd;
70865aea233SWang Nan 		int err;
70965aea233SWang Nan 
71065aea233SWang Nan 		if (fd < 0)
71165aea233SWang Nan 			continue;
71265aea233SWang Nan 		err = ioctl(fd, PERF_EVENT_IOC_PAUSE_OUTPUT, value ? 1 : 0);
71365aea233SWang Nan 		if (err)
71465aea233SWang Nan 			return err;
71565aea233SWang Nan 	}
71665aea233SWang Nan 	return 0;
71765aea233SWang Nan }
71865aea233SWang Nan 
7190a60b339SArnaldo Carvalho de Melo static int evlist__pause(struct evlist *evlist)
72065aea233SWang Nan {
7210a60b339SArnaldo Carvalho de Melo 	return evlist__set_paused(evlist, true);
72265aea233SWang Nan }
72365aea233SWang Nan 
7240a60b339SArnaldo Carvalho de Melo static int evlist__resume(struct evlist *evlist)
72565aea233SWang Nan {
7260a60b339SArnaldo Carvalho de Melo 	return evlist__set_paused(evlist, false);
72765aea233SWang Nan }
72865aea233SWang Nan 
729db6b7b13SJiri Olsa static void evlist__munmap_nofree(struct evlist *evlist)
730f8a95309SArnaldo Carvalho de Melo {
731aece948fSArnaldo Carvalho de Melo 	int i;
732f8a95309SArnaldo Carvalho de Melo 
733b2cb615dSWang Nan 	if (evlist->mmap)
734c976ee11SJiri Olsa 		for (i = 0; i < evlist->core.nr_mmaps; i++)
73580e53d11SJiri Olsa 			perf_mmap__munmap(&evlist->mmap[i].core);
736b2cb615dSWang Nan 
7370b72d69aSWang Nan 	if (evlist->overwrite_mmap)
738c976ee11SJiri Olsa 		for (i = 0; i < evlist->core.nr_mmaps; i++)
73980e53d11SJiri Olsa 			perf_mmap__munmap(&evlist->overwrite_mmap[i].core);
740a1f72618SWang Nan }
741aece948fSArnaldo Carvalho de Melo 
742db6b7b13SJiri Olsa void evlist__munmap(struct evlist *evlist)
743a1f72618SWang Nan {
744db6b7b13SJiri Olsa 	evlist__munmap_nofree(evlist);
74504662523SArnaldo Carvalho de Melo 	zfree(&evlist->mmap);
7460b72d69aSWang Nan 	zfree(&evlist->overwrite_mmap);
747f8a95309SArnaldo Carvalho de Melo }
748f8a95309SArnaldo Carvalho de Melo 
74980e53d11SJiri Olsa static void perf_mmap__unmap_cb(struct perf_mmap *map)
75080e53d11SJiri Olsa {
75180e53d11SJiri Olsa 	struct mmap *m = container_of(map, struct mmap, core);
75280e53d11SJiri Olsa 
75380e53d11SJiri Olsa 	mmap__munmap(m);
75480e53d11SJiri Olsa }
75580e53d11SJiri Olsa 
756d50cf361SJiri Olsa static struct mmap *evlist__alloc_mmap(struct evlist *evlist,
7572c5f6d87SKan Liang 				       bool overwrite)
758f8a95309SArnaldo Carvalho de Melo {
759d4c6fb36SWang Nan 	int i;
760a5830532SJiri Olsa 	struct mmap *map;
761d4c6fb36SWang Nan 
762c976ee11SJiri Olsa 	map = zalloc(evlist->core.nr_mmaps * sizeof(struct mmap));
7638db6d6b1SWang Nan 	if (!map)
7648db6d6b1SWang Nan 		return NULL;
765946ae1d4SWang Nan 
766c976ee11SJiri Olsa 	for (i = 0; i < evlist->core.nr_mmaps; i++) {
7676eb65f7aSJiri Olsa 		struct perf_mmap *prev = i ? &map[i - 1].core : NULL;
7686eb65f7aSJiri Olsa 
7694738ca30SArnaldo Carvalho de Melo 		/*
7704738ca30SArnaldo Carvalho de Melo 		 * When the perf_mmap() call is made we grab one refcount, plus
7716afad54dSKan Liang 		 * one extra to let perf_mmap__consume() get the last
7724738ca30SArnaldo Carvalho de Melo 		 * events after all real references (perf_mmap__get()) are
7734738ca30SArnaldo Carvalho de Melo 		 * dropped.
7744738ca30SArnaldo Carvalho de Melo 		 *
7754738ca30SArnaldo Carvalho de Melo 		 * Each PERF_EVENT_IOC_SET_OUTPUT points to this mmap and
7764738ca30SArnaldo Carvalho de Melo 		 * thus does perf_mmap__get() on it.
7774738ca30SArnaldo Carvalho de Melo 		 */
7786eb65f7aSJiri Olsa 		perf_mmap__init(&map[i].core, prev, overwrite, perf_mmap__unmap_cb);
7794738ca30SArnaldo Carvalho de Melo 	}
780353120b4SJiri Olsa 
7818db6d6b1SWang Nan 	return map;
782f8a95309SArnaldo Carvalho de Melo }
783f8a95309SArnaldo Carvalho de Melo 
7849abd2ab2SJiri Olsa static void
7859abd2ab2SJiri Olsa perf_evlist__mmap_cb_idx(struct perf_evlist *_evlist,
7869abd2ab2SJiri Olsa 			 struct perf_mmap_param *_mp,
7879abd2ab2SJiri Olsa 			 int idx, bool per_cpu)
7889abd2ab2SJiri Olsa {
7899abd2ab2SJiri Olsa 	struct evlist *evlist = container_of(_evlist, struct evlist, core);
7909abd2ab2SJiri Olsa 	struct mmap_params *mp = container_of(_mp, struct mmap_params, core);
7919abd2ab2SJiri Olsa 
7929abd2ab2SJiri Olsa 	auxtrace_mmap_params__set_idx(&mp->auxtrace_mp, evlist, idx, per_cpu);
7939abd2ab2SJiri Olsa }
7949abd2ab2SJiri Olsa 
795bb1b1885SJiri Olsa static struct perf_mmap*
796bb1b1885SJiri Olsa perf_evlist__mmap_cb_get(struct perf_evlist *_evlist, bool overwrite, int idx)
797bb1b1885SJiri Olsa {
798bb1b1885SJiri Olsa 	struct evlist *evlist = container_of(_evlist, struct evlist, core);
7993805e4f3SJiri Olsa 	struct mmap *maps;
800bb1b1885SJiri Olsa 
8013805e4f3SJiri Olsa 	maps = overwrite ? evlist->overwrite_mmap : evlist->mmap;
802bb1b1885SJiri Olsa 
803bb1b1885SJiri Olsa 	if (!maps) {
8043805e4f3SJiri Olsa 		maps = evlist__alloc_mmap(evlist, overwrite);
805bb1b1885SJiri Olsa 		if (!maps)
806bb1b1885SJiri Olsa 			return NULL;
807bb1b1885SJiri Olsa 
8083805e4f3SJiri Olsa 		if (overwrite) {
809bb1b1885SJiri Olsa 			evlist->overwrite_mmap = maps;
810bb1b1885SJiri Olsa 			if (evlist->bkw_mmap_state == BKW_MMAP_NOTREADY)
811ade9d208SArnaldo Carvalho de Melo 				evlist__toggle_bkw_mmap(evlist, BKW_MMAP_RUNNING);
8123805e4f3SJiri Olsa 		} else {
8133805e4f3SJiri Olsa 			evlist->mmap = maps;
814bb1b1885SJiri Olsa 		}
815bb1b1885SJiri Olsa 	}
816bb1b1885SJiri Olsa 
817bb1b1885SJiri Olsa 	return &maps[idx].core;
818bb1b1885SJiri Olsa }
819bb1b1885SJiri Olsa 
820b80132b1SJiri Olsa static int
821b80132b1SJiri Olsa perf_evlist__mmap_cb_mmap(struct perf_mmap *_map, struct perf_mmap_param *_mp,
822b80132b1SJiri Olsa 			  int output, int cpu)
823b80132b1SJiri Olsa {
824b80132b1SJiri Olsa 	struct mmap *map = container_of(_map, struct mmap, core);
825b80132b1SJiri Olsa 	struct mmap_params *mp = container_of(_mp, struct mmap_params, core);
826b80132b1SJiri Olsa 
827b80132b1SJiri Olsa 	return mmap__mmap(map, mp, output, cpu);
828b80132b1SJiri Olsa }
829b80132b1SJiri Olsa 
830f5e7150cSArnaldo Carvalho de Melo unsigned long perf_event_mlock_kb_in_pages(void)
831994a1f78SJiri Olsa {
832f5e7150cSArnaldo Carvalho de Melo 	unsigned long pages;
8338185e881SArnaldo Carvalho de Melo 	int max;
8348185e881SArnaldo Carvalho de Melo 
8358185e881SArnaldo Carvalho de Melo 	if (sysctl__read_int("kernel/perf_event_mlock_kb", &max) < 0) {
8368185e881SArnaldo Carvalho de Melo 		/*
8378185e881SArnaldo Carvalho de Melo 		 * Pick a once upon a time good value, i.e. things look
8388185e881SArnaldo Carvalho de Melo 		 * strange since we can't read a sysctl value, but lets not
8398185e881SArnaldo Carvalho de Melo 		 * die yet...
8408185e881SArnaldo Carvalho de Melo 		 */
8418185e881SArnaldo Carvalho de Melo 		max = 512;
8428185e881SArnaldo Carvalho de Melo 	} else {
8438185e881SArnaldo Carvalho de Melo 		max -= (page_size / 1024);
8448185e881SArnaldo Carvalho de Melo 	}
8458185e881SArnaldo Carvalho de Melo 
8468185e881SArnaldo Carvalho de Melo 	pages = (max * 1024) / page_size;
8471be300f4SArnaldo Carvalho de Melo 	if (!is_power_of_2(pages))
8481be300f4SArnaldo Carvalho de Melo 		pages = rounddown_pow_of_two(pages);
849f5e7150cSArnaldo Carvalho de Melo 
850f5e7150cSArnaldo Carvalho de Melo 	return pages;
851f5e7150cSArnaldo Carvalho de Melo }
852f5e7150cSArnaldo Carvalho de Melo 
8539521b5f2SJiri Olsa size_t evlist__mmap_size(unsigned long pages)
854f5e7150cSArnaldo Carvalho de Melo {
855f5e7150cSArnaldo Carvalho de Melo 	if (pages == UINT_MAX)
856f5e7150cSArnaldo Carvalho de Melo 		pages = perf_event_mlock_kb_in_pages();
857f5e7150cSArnaldo Carvalho de Melo 	else if (!is_power_of_2(pages))
858994a1f78SJiri Olsa 		return 0;
859994a1f78SJiri Olsa 
860994a1f78SJiri Olsa 	return (pages + 1) * page_size;
861994a1f78SJiri Olsa }
862994a1f78SJiri Olsa 
86333c2dcfdSDavid Ahern static long parse_pages_arg(const char *str, unsigned long min,
86433c2dcfdSDavid Ahern 			    unsigned long max)
865994a1f78SJiri Olsa {
8662fbe4abeSAdrian Hunter 	unsigned long pages, val;
86727050f53SJiri Olsa 	static struct parse_tag tags[] = {
86827050f53SJiri Olsa 		{ .tag  = 'B', .mult = 1       },
86927050f53SJiri Olsa 		{ .tag  = 'K', .mult = 1 << 10 },
87027050f53SJiri Olsa 		{ .tag  = 'M', .mult = 1 << 20 },
87127050f53SJiri Olsa 		{ .tag  = 'G', .mult = 1 << 30 },
87227050f53SJiri Olsa 		{ .tag  = 0 },
87327050f53SJiri Olsa 	};
874994a1f78SJiri Olsa 
8758973504bSDavid Ahern 	if (str == NULL)
87633c2dcfdSDavid Ahern 		return -EINVAL;
8778973504bSDavid Ahern 
87827050f53SJiri Olsa 	val = parse_tag_value(str, tags);
8792fbe4abeSAdrian Hunter 	if (val != (unsigned long) -1) {
88027050f53SJiri Olsa 		/* we got file size value */
88127050f53SJiri Olsa 		pages = PERF_ALIGN(val, page_size) / page_size;
88227050f53SJiri Olsa 	} else {
88327050f53SJiri Olsa 		/* we got pages count value */
88427050f53SJiri Olsa 		char *eptr;
885994a1f78SJiri Olsa 		pages = strtoul(str, &eptr, 10);
88633c2dcfdSDavid Ahern 		if (*eptr != '\0')
88733c2dcfdSDavid Ahern 			return -EINVAL;
88827050f53SJiri Olsa 	}
889994a1f78SJiri Olsa 
8902bcab6c1SAdrian Hunter 	if (pages == 0 && min == 0) {
89133c2dcfdSDavid Ahern 		/* leave number of pages at 0 */
8921dbfa938SAdrian Hunter 	} else if (!is_power_of_2(pages)) {
8939808143bSJiri Olsa 		char buf[100];
8949808143bSJiri Olsa 
89533c2dcfdSDavid Ahern 		/* round pages up to next power of 2 */
89691529834SArnaldo Carvalho de Melo 		pages = roundup_pow_of_two(pages);
8971dbfa938SAdrian Hunter 		if (!pages)
8981dbfa938SAdrian Hunter 			return -EINVAL;
8999808143bSJiri Olsa 
9009808143bSJiri Olsa 		unit_number__scnprintf(buf, sizeof(buf), pages * page_size);
9019808143bSJiri Olsa 		pr_info("rounding mmap pages size to %s (%lu pages)\n",
9029808143bSJiri Olsa 			buf, pages);
9032fbe4abeSAdrian Hunter 	}
9042fbe4abeSAdrian Hunter 
90533c2dcfdSDavid Ahern 	if (pages > max)
90633c2dcfdSDavid Ahern 		return -EINVAL;
90733c2dcfdSDavid Ahern 
90833c2dcfdSDavid Ahern 	return pages;
90933c2dcfdSDavid Ahern }
91033c2dcfdSDavid Ahern 
91125f84702SArnaldo Carvalho de Melo int __evlist__parse_mmap_pages(unsigned int *mmap_pages, const char *str)
91233c2dcfdSDavid Ahern {
91333c2dcfdSDavid Ahern 	unsigned long max = UINT_MAX;
91433c2dcfdSDavid Ahern 	long pages;
91533c2dcfdSDavid Ahern 
916f5ae9c42SAdrian Hunter 	if (max > SIZE_MAX / page_size)
91733c2dcfdSDavid Ahern 		max = SIZE_MAX / page_size;
91833c2dcfdSDavid Ahern 
91933c2dcfdSDavid Ahern 	pages = parse_pages_arg(str, 1, max);
92033c2dcfdSDavid Ahern 	if (pages < 0) {
92133c2dcfdSDavid Ahern 		pr_err("Invalid argument for --mmap_pages/-m\n");
922994a1f78SJiri Olsa 		return -1;
923994a1f78SJiri Olsa 	}
924994a1f78SJiri Olsa 
925994a1f78SJiri Olsa 	*mmap_pages = pages;
926994a1f78SJiri Olsa 	return 0;
927994a1f78SJiri Olsa }
928994a1f78SJiri Olsa 
92925f84702SArnaldo Carvalho de Melo int evlist__parse_mmap_pages(const struct option *opt, const char *str, int unset __maybe_unused)
930e9db1310SAdrian Hunter {
93125f84702SArnaldo Carvalho de Melo 	return __evlist__parse_mmap_pages(opt->value, str);
932e9db1310SAdrian Hunter }
933e9db1310SAdrian Hunter 
934c83fa7f2SAdrian Hunter /**
9359521b5f2SJiri Olsa  * evlist__mmap_ex - Create mmaps to receive events.
936c83fa7f2SAdrian Hunter  * @evlist: list of events
937c83fa7f2SAdrian Hunter  * @pages: map length in pages
938c83fa7f2SAdrian Hunter  * @overwrite: overwrite older events?
939718c602dSAdrian Hunter  * @auxtrace_pages - auxtrace map length in pages
940718c602dSAdrian Hunter  * @auxtrace_overwrite - overwrite older auxtrace data?
941f8a95309SArnaldo Carvalho de Melo  *
942c83fa7f2SAdrian Hunter  * If @overwrite is %false the user needs to signal event consumption using
9439521b5f2SJiri Olsa  * perf_mmap__write_tail().  Using evlist__mmap_read() does this
944c83fa7f2SAdrian Hunter  * automatically.
945f8a95309SArnaldo Carvalho de Melo  *
946718c602dSAdrian Hunter  * Similarly, if @auxtrace_overwrite is %false the user needs to signal data
947718c602dSAdrian Hunter  * consumption using auxtrace_mmap__write_tail().
948718c602dSAdrian Hunter  *
949c83fa7f2SAdrian Hunter  * Return: %0 on success, negative error code otherwise.
950f8a95309SArnaldo Carvalho de Melo  */
9519521b5f2SJiri Olsa int evlist__mmap_ex(struct evlist *evlist, unsigned int pages,
9527a276ff6SWang Nan 			 unsigned int auxtrace_pages,
95351255a8aSAlexey Budankov 			 bool auxtrace_overwrite, int nr_cblocks, int affinity, int flush,
95451255a8aSAlexey Budankov 			 int comp_level)
955f8a95309SArnaldo Carvalho de Melo {
95671f566a3SWang Nan 	/*
95771f566a3SWang Nan 	 * Delay setting mp.prot: set it before calling perf_mmap__mmap.
95871f566a3SWang Nan 	 * Its value is decided by evsel's write_backward.
95971f566a3SWang Nan 	 * So &mp should not be passed through const pointer.
96071f566a3SWang Nan 	 */
961e440979fSJiri Olsa 	struct mmap_params mp = {
962e440979fSJiri Olsa 		.nr_cblocks	= nr_cblocks,
963e440979fSJiri Olsa 		.affinity	= affinity,
964e440979fSJiri Olsa 		.flush		= flush,
965e440979fSJiri Olsa 		.comp_level	= comp_level
966e440979fSJiri Olsa 	};
967923d0f18SJiri Olsa 	struct perf_evlist_mmap_ops ops = {
9689abd2ab2SJiri Olsa 		.idx  = perf_evlist__mmap_cb_idx,
969bb1b1885SJiri Olsa 		.get  = perf_evlist__mmap_cb_get,
970b80132b1SJiri Olsa 		.mmap = perf_evlist__mmap_cb_mmap,
9719abd2ab2SJiri Olsa 	};
97250a682ceSArnaldo Carvalho de Melo 
973f6fa4375SJiri Olsa 	evlist->core.mmap_len = evlist__mmap_size(pages);
974f6fa4375SJiri Olsa 	pr_debug("mmap size %zuB\n", evlist->core.mmap_len);
975f8a95309SArnaldo Carvalho de Melo 
976f6fa4375SJiri Olsa 	auxtrace_mmap_params__init(&mp.auxtrace_mp, evlist->core.mmap_len,
977718c602dSAdrian Hunter 				   auxtrace_pages, auxtrace_overwrite);
978718c602dSAdrian Hunter 
979923d0f18SJiri Olsa 	return perf_evlist__mmap_ops(&evlist->core, &ops, &mp.core);
980f8a95309SArnaldo Carvalho de Melo }
9817e2ed097SArnaldo Carvalho de Melo 
9829521b5f2SJiri Olsa int evlist__mmap(struct evlist *evlist, unsigned int pages)
983718c602dSAdrian Hunter {
9849521b5f2SJiri Olsa 	return evlist__mmap_ex(evlist, pages, 0, false, 0, PERF_AFFINITY_SYS, 1, 0);
985718c602dSAdrian Hunter }
986718c602dSAdrian Hunter 
9877748bb71SArnaldo Carvalho de Melo int evlist__create_maps(struct evlist *evlist, struct target *target)
9887e2ed097SArnaldo Carvalho de Melo {
989147c508fSJin Yao 	bool all_threads = (target->per_thread && target->system_wide);
990f854839bSJiri Olsa 	struct perf_cpu_map *cpus;
9919749b90eSJiri Olsa 	struct perf_thread_map *threads;
9927e2ed097SArnaldo Carvalho de Melo 
993147c508fSJin Yao 	/*
994147c508fSJin Yao 	 * If specify '-a' and '--per-thread' to perf record, perf record
995147c508fSJin Yao 	 * will override '--per-thread'. target->per_thread = false and
996147c508fSJin Yao 	 * target->system_wide = true.
997147c508fSJin Yao 	 *
998147c508fSJin Yao 	 * If specify '--per-thread' only to perf record,
999147c508fSJin Yao 	 * target->per_thread = true and target->system_wide = false.
1000147c508fSJin Yao 	 *
1001147c508fSJin Yao 	 * So target->per_thread && target->system_wide is false.
1002147c508fSJin Yao 	 * For perf record, thread_map__new_str doesn't call
1003147c508fSJin Yao 	 * thread_map__new_all_cpus. That will keep perf record's
1004147c508fSJin Yao 	 * current behavior.
1005147c508fSJin Yao 	 *
1006147c508fSJin Yao 	 * For perf stat, it allows the case that target->per_thread and
1007147c508fSJin Yao 	 * target->system_wide are all true. It means to collect system-wide
1008147c508fSJin Yao 	 * per-thread data. thread_map__new_str will call
1009147c508fSJin Yao 	 * thread_map__new_all_cpus to enumerate all threads.
1010147c508fSJin Yao 	 */
101173c0ca1eSJin Yao 	threads = thread_map__new_str(target->pid, target->tid, target->uid,
1012147c508fSJin Yao 				      all_threads);
101374bfd2b2SAdrian Hunter 
101474bfd2b2SAdrian Hunter 	if (!threads)
10157e2ed097SArnaldo Carvalho de Melo 		return -1;
10167e2ed097SArnaldo Carvalho de Melo 
10179c105fbcSDongsheng Yang 	if (target__uses_dummy_map(target))
1018397721e0SJiri Olsa 		cpus = perf_cpu_map__dummy_new();
1019879d77d0SNamhyung Kim 	else
10209c3516d1SJiri Olsa 		cpus = perf_cpu_map__new(target->cpu_list);
10217e2ed097SArnaldo Carvalho de Melo 
102274bfd2b2SAdrian Hunter 	if (!cpus)
10237e2ed097SArnaldo Carvalho de Melo 		goto out_delete_threads;
10247e2ed097SArnaldo Carvalho de Melo 
1025b726e363SJin Yao 	evlist->core.has_user_cpus = !!target->cpu_list && !target->hybrid;
1026ec9a77a7SAdrian Hunter 
1027453fa030SJiri Olsa 	perf_evlist__set_maps(&evlist->core, cpus, threads);
1028d5bc056eSAdrian Hunter 
1029bfd1b83dSNamhyung Kim 	/* as evlist now has references, put count here */
1030bfd1b83dSNamhyung Kim 	perf_cpu_map__put(cpus);
1031bfd1b83dSNamhyung Kim 	perf_thread_map__put(threads);
1032bfd1b83dSNamhyung Kim 
1033d5bc056eSAdrian Hunter 	return 0;
10347e2ed097SArnaldo Carvalho de Melo 
10357e2ed097SArnaldo Carvalho de Melo out_delete_threads:
10367836e52eSJiri Olsa 	perf_thread_map__put(threads);
10377e2ed097SArnaldo Carvalho de Melo 	return -1;
10387e2ed097SArnaldo Carvalho de Melo }
10397e2ed097SArnaldo Carvalho de Melo 
104024bf91a7SArnaldo Carvalho de Melo int evlist__apply_filters(struct evlist *evlist, struct evsel **err_evsel)
10410a102479SFrederic Weisbecker {
104232dcd021SJiri Olsa 	struct evsel *evsel;
1043745cefc5SArnaldo Carvalho de Melo 	int err = 0;
10440a102479SFrederic Weisbecker 
1045e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, evsel) {
1046745cefc5SArnaldo Carvalho de Melo 		if (evsel->filter == NULL)
10470a102479SFrederic Weisbecker 			continue;
1048745cefc5SArnaldo Carvalho de Melo 
1049d988d5eeSKan Liang 		/*
1050d988d5eeSKan Liang 		 * filters only work for tracepoint event, which doesn't have cpu limit.
1051d988d5eeSKan Liang 		 * So evlist and evsel should always be same.
1052d988d5eeSKan Liang 		 */
1053a00571fdSJiri Olsa 		err = perf_evsel__apply_filter(&evsel->core, evsel->filter);
105423d4aad4SArnaldo Carvalho de Melo 		if (err) {
105523d4aad4SArnaldo Carvalho de Melo 			*err_evsel = evsel;
1056745cefc5SArnaldo Carvalho de Melo 			break;
10570a102479SFrederic Weisbecker 		}
105823d4aad4SArnaldo Carvalho de Melo 	}
10590a102479SFrederic Weisbecker 
1060745cefc5SArnaldo Carvalho de Melo 	return err;
1061745cefc5SArnaldo Carvalho de Melo }
1062745cefc5SArnaldo Carvalho de Melo 
106324bf91a7SArnaldo Carvalho de Melo int evlist__set_tp_filter(struct evlist *evlist, const char *filter)
1064745cefc5SArnaldo Carvalho de Melo {
106532dcd021SJiri Olsa 	struct evsel *evsel;
1066745cefc5SArnaldo Carvalho de Melo 	int err = 0;
1067745cefc5SArnaldo Carvalho de Melo 
106805cea449SArnaldo Carvalho de Melo 	if (filter == NULL)
106905cea449SArnaldo Carvalho de Melo 		return -1;
107005cea449SArnaldo Carvalho de Melo 
1071e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, evsel) {
10721fc632ceSJiri Olsa 		if (evsel->core.attr.type != PERF_TYPE_TRACEPOINT)
1073fdf14720SWang Nan 			continue;
1074fdf14720SWang Nan 
1075ad681adfSArnaldo Carvalho de Melo 		err = evsel__set_filter(evsel, filter);
1076745cefc5SArnaldo Carvalho de Melo 		if (err)
1077745cefc5SArnaldo Carvalho de Melo 			break;
1078745cefc5SArnaldo Carvalho de Melo 	}
1079745cefc5SArnaldo Carvalho de Melo 
1080745cefc5SArnaldo Carvalho de Melo 	return err;
10810a102479SFrederic Weisbecker }
108274429964SFrederic Weisbecker 
108324bf91a7SArnaldo Carvalho de Melo int evlist__append_tp_filter(struct evlist *evlist, const char *filter)
108453c92f73SArnaldo Carvalho de Melo {
108553c92f73SArnaldo Carvalho de Melo 	struct evsel *evsel;
108653c92f73SArnaldo Carvalho de Melo 	int err = 0;
108753c92f73SArnaldo Carvalho de Melo 
108853c92f73SArnaldo Carvalho de Melo 	if (filter == NULL)
108953c92f73SArnaldo Carvalho de Melo 		return -1;
109053c92f73SArnaldo Carvalho de Melo 
109153c92f73SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, evsel) {
109253c92f73SArnaldo Carvalho de Melo 		if (evsel->core.attr.type != PERF_TYPE_TRACEPOINT)
109353c92f73SArnaldo Carvalho de Melo 			continue;
109453c92f73SArnaldo Carvalho de Melo 
1095ad681adfSArnaldo Carvalho de Melo 		err = evsel__append_tp_filter(evsel, filter);
109653c92f73SArnaldo Carvalho de Melo 		if (err)
109753c92f73SArnaldo Carvalho de Melo 			break;
109853c92f73SArnaldo Carvalho de Melo 	}
109953c92f73SArnaldo Carvalho de Melo 
110053c92f73SArnaldo Carvalho de Melo 	return err;
110153c92f73SArnaldo Carvalho de Melo }
110253c92f73SArnaldo Carvalho de Melo 
1103da949f50SArnaldo Carvalho de Melo char *asprintf__tp_filter_pids(size_t npids, pid_t *pids)
1104cfd70a26SArnaldo Carvalho de Melo {
1105cfd70a26SArnaldo Carvalho de Melo 	char *filter;
1106be199adaSArnaldo Carvalho de Melo 	size_t i;
1107cfd70a26SArnaldo Carvalho de Melo 
1108be199adaSArnaldo Carvalho de Melo 	for (i = 0; i < npids; ++i) {
1109be199adaSArnaldo Carvalho de Melo 		if (i == 0) {
1110be199adaSArnaldo Carvalho de Melo 			if (asprintf(&filter, "common_pid != %d", pids[i]) < 0)
111105cea449SArnaldo Carvalho de Melo 				return NULL;
1112be199adaSArnaldo Carvalho de Melo 		} else {
1113be199adaSArnaldo Carvalho de Melo 			char *tmp;
1114be199adaSArnaldo Carvalho de Melo 
1115be199adaSArnaldo Carvalho de Melo 			if (asprintf(&tmp, "%s && common_pid != %d", filter, pids[i]) < 0)
1116be199adaSArnaldo Carvalho de Melo 				goto out_free;
1117be199adaSArnaldo Carvalho de Melo 
1118be199adaSArnaldo Carvalho de Melo 			free(filter);
1119be199adaSArnaldo Carvalho de Melo 			filter = tmp;
1120be199adaSArnaldo Carvalho de Melo 		}
1121be199adaSArnaldo Carvalho de Melo 	}
1122cfd70a26SArnaldo Carvalho de Melo 
112305cea449SArnaldo Carvalho de Melo 	return filter;
1124be199adaSArnaldo Carvalho de Melo out_free:
1125cfd70a26SArnaldo Carvalho de Melo 	free(filter);
112605cea449SArnaldo Carvalho de Melo 	return NULL;
112705cea449SArnaldo Carvalho de Melo }
112805cea449SArnaldo Carvalho de Melo 
112924bf91a7SArnaldo Carvalho de Melo int evlist__set_tp_filter_pids(struct evlist *evlist, size_t npids, pid_t *pids)
113005cea449SArnaldo Carvalho de Melo {
113105cea449SArnaldo Carvalho de Melo 	char *filter = asprintf__tp_filter_pids(npids, pids);
113224bf91a7SArnaldo Carvalho de Melo 	int ret = evlist__set_tp_filter(evlist, filter);
113305cea449SArnaldo Carvalho de Melo 
113405cea449SArnaldo Carvalho de Melo 	free(filter);
1135cfd70a26SArnaldo Carvalho de Melo 	return ret;
1136cfd70a26SArnaldo Carvalho de Melo }
1137cfd70a26SArnaldo Carvalho de Melo 
113824bf91a7SArnaldo Carvalho de Melo int evlist__set_tp_filter_pid(struct evlist *evlist, pid_t pid)
1139be199adaSArnaldo Carvalho de Melo {
114024bf91a7SArnaldo Carvalho de Melo 	return evlist__set_tp_filter_pids(evlist, 1, &pid);
1141be199adaSArnaldo Carvalho de Melo }
1142be199adaSArnaldo Carvalho de Melo 
114324bf91a7SArnaldo Carvalho de Melo int evlist__append_tp_filter_pids(struct evlist *evlist, size_t npids, pid_t *pids)
11441827ab5bSArnaldo Carvalho de Melo {
11451827ab5bSArnaldo Carvalho de Melo 	char *filter = asprintf__tp_filter_pids(npids, pids);
114624bf91a7SArnaldo Carvalho de Melo 	int ret = evlist__append_tp_filter(evlist, filter);
11471827ab5bSArnaldo Carvalho de Melo 
11481827ab5bSArnaldo Carvalho de Melo 	free(filter);
11491827ab5bSArnaldo Carvalho de Melo 	return ret;
11501827ab5bSArnaldo Carvalho de Melo }
11511827ab5bSArnaldo Carvalho de Melo 
115224bf91a7SArnaldo Carvalho de Melo int evlist__append_tp_filter_pid(struct evlist *evlist, pid_t pid)
11531827ab5bSArnaldo Carvalho de Melo {
115424bf91a7SArnaldo Carvalho de Melo 	return evlist__append_tp_filter_pids(evlist, 1, &pid);
11551827ab5bSArnaldo Carvalho de Melo }
11561827ab5bSArnaldo Carvalho de Melo 
1157b3c2cc2bSArnaldo Carvalho de Melo bool evlist__valid_sample_type(struct evlist *evlist)
115874429964SFrederic Weisbecker {
115932dcd021SJiri Olsa 	struct evsel *pos;
1160c2a70653SArnaldo Carvalho de Melo 
11616484d2f9SJiri Olsa 	if (evlist->core.nr_entries == 1)
116275562573SAdrian Hunter 		return true;
116375562573SAdrian Hunter 
116475562573SAdrian Hunter 	if (evlist->id_pos < 0 || evlist->is_pos < 0)
116575562573SAdrian Hunter 		return false;
116675562573SAdrian Hunter 
1167e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, pos) {
116875562573SAdrian Hunter 		if (pos->id_pos != evlist->id_pos ||
116975562573SAdrian Hunter 		    pos->is_pos != evlist->is_pos)
1170c2a70653SArnaldo Carvalho de Melo 			return false;
117174429964SFrederic Weisbecker 	}
117274429964SFrederic Weisbecker 
1173c2a70653SArnaldo Carvalho de Melo 	return true;
1174c2a70653SArnaldo Carvalho de Melo }
1175c2a70653SArnaldo Carvalho de Melo 
1176b3c2cc2bSArnaldo Carvalho de Melo u64 __evlist__combined_sample_type(struct evlist *evlist)
1177c2a70653SArnaldo Carvalho de Melo {
117832dcd021SJiri Olsa 	struct evsel *evsel;
117975562573SAdrian Hunter 
118075562573SAdrian Hunter 	if (evlist->combined_sample_type)
118175562573SAdrian Hunter 		return evlist->combined_sample_type;
118275562573SAdrian Hunter 
1183e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, evsel)
11841fc632ceSJiri Olsa 		evlist->combined_sample_type |= evsel->core.attr.sample_type;
118575562573SAdrian Hunter 
118675562573SAdrian Hunter 	return evlist->combined_sample_type;
118775562573SAdrian Hunter }
118875562573SAdrian Hunter 
1189b3c2cc2bSArnaldo Carvalho de Melo u64 evlist__combined_sample_type(struct evlist *evlist)
119075562573SAdrian Hunter {
119175562573SAdrian Hunter 	evlist->combined_sample_type = 0;
1192b3c2cc2bSArnaldo Carvalho de Melo 	return __evlist__combined_sample_type(evlist);
1193c2a70653SArnaldo Carvalho de Melo }
1194c2a70653SArnaldo Carvalho de Melo 
119592c7d7cdSArnaldo Carvalho de Melo u64 evlist__combined_branch_type(struct evlist *evlist)
119698df858eSAndi Kleen {
119732dcd021SJiri Olsa 	struct evsel *evsel;
119898df858eSAndi Kleen 	u64 branch_type = 0;
119998df858eSAndi Kleen 
1200e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, evsel)
12011fc632ceSJiri Olsa 		branch_type |= evsel->core.attr.branch_sample_type;
120298df858eSAndi Kleen 	return branch_type;
120398df858eSAndi Kleen }
120498df858eSAndi Kleen 
120578e1bc25SArnaldo Carvalho de Melo bool evlist__valid_read_format(struct evlist *evlist)
12069ede473cSJiri Olsa {
1207515dbe48SJiri Olsa 	struct evsel *first = evlist__first(evlist), *pos = first;
12081fc632ceSJiri Olsa 	u64 read_format = first->core.attr.read_format;
12091fc632ceSJiri Olsa 	u64 sample_type = first->core.attr.sample_type;
12109ede473cSJiri Olsa 
1211e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, pos) {
121294d3820fSAdrian Hunter 		if (read_format != pos->core.attr.read_format) {
121394d3820fSAdrian Hunter 			pr_debug("Read format differs %#" PRIx64 " vs %#" PRIx64 "\n",
121494d3820fSAdrian Hunter 				 read_format, (u64)pos->core.attr.read_format);
121594d3820fSAdrian Hunter 		}
12169ede473cSJiri Olsa 	}
12179ede473cSJiri Olsa 
12184d39c89fSIngo Molnar 	/* PERF_SAMPLE_READ implies PERF_FORMAT_ID. */
12199ede473cSJiri Olsa 	if ((sample_type & PERF_SAMPLE_READ) &&
12209ede473cSJiri Olsa 	    !(read_format & PERF_FORMAT_ID)) {
12219ede473cSJiri Olsa 		return false;
12229ede473cSJiri Olsa 	}
12239ede473cSJiri Olsa 
12249ede473cSJiri Olsa 	return true;
12259ede473cSJiri Olsa }
12269ede473cSJiri Olsa 
12271420ba2fSArnaldo Carvalho de Melo u16 evlist__id_hdr_size(struct evlist *evlist)
122881e36bffSArnaldo Carvalho de Melo {
1229515dbe48SJiri Olsa 	struct evsel *first = evlist__first(evlist);
123081e36bffSArnaldo Carvalho de Melo 	struct perf_sample *data;
123181e36bffSArnaldo Carvalho de Melo 	u64 sample_type;
123281e36bffSArnaldo Carvalho de Melo 	u16 size = 0;
123381e36bffSArnaldo Carvalho de Melo 
12341fc632ceSJiri Olsa 	if (!first->core.attr.sample_id_all)
123581e36bffSArnaldo Carvalho de Melo 		goto out;
123681e36bffSArnaldo Carvalho de Melo 
12371fc632ceSJiri Olsa 	sample_type = first->core.attr.sample_type;
123881e36bffSArnaldo Carvalho de Melo 
123981e36bffSArnaldo Carvalho de Melo 	if (sample_type & PERF_SAMPLE_TID)
124081e36bffSArnaldo Carvalho de Melo 		size += sizeof(data->tid) * 2;
124181e36bffSArnaldo Carvalho de Melo 
124281e36bffSArnaldo Carvalho de Melo        if (sample_type & PERF_SAMPLE_TIME)
124381e36bffSArnaldo Carvalho de Melo 		size += sizeof(data->time);
124481e36bffSArnaldo Carvalho de Melo 
124581e36bffSArnaldo Carvalho de Melo 	if (sample_type & PERF_SAMPLE_ID)
124681e36bffSArnaldo Carvalho de Melo 		size += sizeof(data->id);
124781e36bffSArnaldo Carvalho de Melo 
124881e36bffSArnaldo Carvalho de Melo 	if (sample_type & PERF_SAMPLE_STREAM_ID)
124981e36bffSArnaldo Carvalho de Melo 		size += sizeof(data->stream_id);
125081e36bffSArnaldo Carvalho de Melo 
125181e36bffSArnaldo Carvalho de Melo 	if (sample_type & PERF_SAMPLE_CPU)
125281e36bffSArnaldo Carvalho de Melo 		size += sizeof(data->cpu) * 2;
125375562573SAdrian Hunter 
125475562573SAdrian Hunter 	if (sample_type & PERF_SAMPLE_IDENTIFIER)
125575562573SAdrian Hunter 		size += sizeof(data->id);
125681e36bffSArnaldo Carvalho de Melo out:
125781e36bffSArnaldo Carvalho de Melo 	return size;
125881e36bffSArnaldo Carvalho de Melo }
125981e36bffSArnaldo Carvalho de Melo 
12608cedf3a5SArnaldo Carvalho de Melo bool evlist__valid_sample_id_all(struct evlist *evlist)
1261c2a70653SArnaldo Carvalho de Melo {
1262515dbe48SJiri Olsa 	struct evsel *first = evlist__first(evlist), *pos = first;
1263c2a70653SArnaldo Carvalho de Melo 
1264e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry_continue(evlist, pos) {
12651fc632ceSJiri Olsa 		if (first->core.attr.sample_id_all != pos->core.attr.sample_id_all)
1266c2a70653SArnaldo Carvalho de Melo 			return false;
1267c2a70653SArnaldo Carvalho de Melo 	}
1268c2a70653SArnaldo Carvalho de Melo 
1269c2a70653SArnaldo Carvalho de Melo 	return true;
127074429964SFrederic Weisbecker }
127174429964SFrederic Weisbecker 
12728cedf3a5SArnaldo Carvalho de Melo bool evlist__sample_id_all(struct evlist *evlist)
127374429964SFrederic Weisbecker {
1274515dbe48SJiri Olsa 	struct evsel *first = evlist__first(evlist);
12751fc632ceSJiri Olsa 	return first->core.attr.sample_id_all;
127674429964SFrederic Weisbecker }
127781cce8deSArnaldo Carvalho de Melo 
1278900c8eadSArnaldo Carvalho de Melo void evlist__set_selected(struct evlist *evlist, struct evsel *evsel)
127981cce8deSArnaldo Carvalho de Melo {
128081cce8deSArnaldo Carvalho de Melo 	evlist->selected = evsel;
128181cce8deSArnaldo Carvalho de Melo }
1282727ab04eSArnaldo Carvalho de Melo 
1283750b4edeSJiri Olsa void evlist__close(struct evlist *evlist)
1284a74b4b66SNamhyung Kim {
128532dcd021SJiri Olsa 	struct evsel *evsel;
1286*472832d2SIan Rogers 	struct evlist_cpu_iterator evlist_cpu_itr;
12877736627bSAndi Kleen 	struct affinity affinity;
1288a74b4b66SNamhyung Kim 
12897736627bSAndi Kleen 	/*
12907736627bSAndi Kleen 	 * With perf record core.cpus is usually NULL.
12917736627bSAndi Kleen 	 * Use the old method to handle this for now.
12927736627bSAndi Kleen 	 */
12937736627bSAndi Kleen 	if (!evlist->core.cpus) {
1294475fb533SAndi Kleen 		evlist__for_each_entry_reverse(evlist, evsel)
129588761fa1SJiri Olsa 			evsel__close(evsel);
12967736627bSAndi Kleen 		return;
12977736627bSAndi Kleen 	}
12987736627bSAndi Kleen 
12997736627bSAndi Kleen 	if (affinity__setup(&affinity) < 0)
13007736627bSAndi Kleen 		return;
13017736627bSAndi Kleen 
1302*472832d2SIan Rogers 	evlist__for_each_cpu(evlist_cpu_itr, evlist, &affinity) {
1303*472832d2SIan Rogers 		perf_evsel__close_cpu(&evlist_cpu_itr.evsel->core,
1304*472832d2SIan Rogers 				      evlist_cpu_itr.cpu_map_idx);
13057736627bSAndi Kleen 	}
1306*472832d2SIan Rogers 
13077736627bSAndi Kleen 	affinity__cleanup(&affinity);
13087736627bSAndi Kleen 	evlist__for_each_entry_reverse(evlist, evsel) {
13097736627bSAndi Kleen 		perf_evsel__free_fd(&evsel->core);
13107736627bSAndi Kleen 		perf_evsel__free_id(&evsel->core);
13117736627bSAndi Kleen 	}
1312513068f2SNamhyung Kim 	perf_evlist__reset_id_hash(&evlist->core);
1313a74b4b66SNamhyung Kim }
1314a74b4b66SNamhyung Kim 
13157748bb71SArnaldo Carvalho de Melo static int evlist__create_syswide_maps(struct evlist *evlist)
13164112eb18SArnaldo Carvalho de Melo {
1317f854839bSJiri Olsa 	struct perf_cpu_map *cpus;
13189749b90eSJiri Olsa 	struct perf_thread_map *threads;
13194112eb18SArnaldo Carvalho de Melo 	int err = -ENOMEM;
13204112eb18SArnaldo Carvalho de Melo 
13214112eb18SArnaldo Carvalho de Melo 	/*
13224112eb18SArnaldo Carvalho de Melo 	 * Try reading /sys/devices/system/cpu/online to get
13234112eb18SArnaldo Carvalho de Melo 	 * an all cpus map.
13244112eb18SArnaldo Carvalho de Melo 	 *
13254112eb18SArnaldo Carvalho de Melo 	 * FIXME: -ENOMEM is the best we can do here, the cpu_map
13264112eb18SArnaldo Carvalho de Melo 	 * code needs an overhaul to properly forward the
13274112eb18SArnaldo Carvalho de Melo 	 * error, and we may not want to do that fallback to a
13284112eb18SArnaldo Carvalho de Melo 	 * default cpu identity map :-\
13294112eb18SArnaldo Carvalho de Melo 	 */
13309c3516d1SJiri Olsa 	cpus = perf_cpu_map__new(NULL);
13318c0498b6SAdrian Hunter 	if (!cpus)
13324112eb18SArnaldo Carvalho de Melo 		goto out;
13334112eb18SArnaldo Carvalho de Melo 
13344b49cce2SJiri Olsa 	threads = perf_thread_map__new_dummy();
13358c0498b6SAdrian Hunter 	if (!threads)
13368c0498b6SAdrian Hunter 		goto out_put;
13374112eb18SArnaldo Carvalho de Melo 
1338453fa030SJiri Olsa 	perf_evlist__set_maps(&evlist->core, cpus, threads);
1339bfd1b83dSNamhyung Kim 
1340bfd1b83dSNamhyung Kim 	perf_thread_map__put(threads);
13418c0498b6SAdrian Hunter out_put:
134238f01d8dSJiri Olsa 	perf_cpu_map__put(cpus);
1343bfd1b83dSNamhyung Kim out:
1344bfd1b83dSNamhyung Kim 	return err;
13454112eb18SArnaldo Carvalho de Melo }
13464112eb18SArnaldo Carvalho de Melo 
1347474ddc4cSJiri Olsa int evlist__open(struct evlist *evlist)
1348727ab04eSArnaldo Carvalho de Melo {
134932dcd021SJiri Olsa 	struct evsel *evsel;
1350a74b4b66SNamhyung Kim 	int err;
1351727ab04eSArnaldo Carvalho de Melo 
13524112eb18SArnaldo Carvalho de Melo 	/*
13534112eb18SArnaldo Carvalho de Melo 	 * Default: one fd per CPU, all threads, aka systemwide
13544112eb18SArnaldo Carvalho de Melo 	 * as sys_perf_event_open(cpu = -1, thread = -1) is EINVAL
13554112eb18SArnaldo Carvalho de Melo 	 */
135603617c22SJiri Olsa 	if (evlist->core.threads == NULL && evlist->core.cpus == NULL) {
13577748bb71SArnaldo Carvalho de Melo 		err = evlist__create_syswide_maps(evlist);
13584112eb18SArnaldo Carvalho de Melo 		if (err < 0)
13594112eb18SArnaldo Carvalho de Melo 			goto out_err;
13604112eb18SArnaldo Carvalho de Melo 	}
13614112eb18SArnaldo Carvalho de Melo 
13620a7e7ec9SArnaldo Carvalho de Melo 	evlist__update_id_pos(evlist);
1363733cd2feSAdrian Hunter 
1364e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, evsel) {
1365af663bd0SJiri Olsa 		err = evsel__open(evsel, evsel->core.cpus, evsel->core.threads);
1366727ab04eSArnaldo Carvalho de Melo 		if (err < 0)
1367727ab04eSArnaldo Carvalho de Melo 			goto out_err;
1368727ab04eSArnaldo Carvalho de Melo 	}
1369727ab04eSArnaldo Carvalho de Melo 
1370727ab04eSArnaldo Carvalho de Melo 	return 0;
1371727ab04eSArnaldo Carvalho de Melo out_err:
1372750b4edeSJiri Olsa 	evlist__close(evlist);
137341c21a68SNamhyung Kim 	errno = -err;
1374727ab04eSArnaldo Carvalho de Melo 	return err;
1375727ab04eSArnaldo Carvalho de Melo }
137635b9d88eSArnaldo Carvalho de Melo 
13777b392ef0SArnaldo Carvalho de Melo int evlist__prepare_workload(struct evlist *evlist, struct target *target, const char *argv[],
13787b392ef0SArnaldo Carvalho de Melo 			     bool pipe_output, void (*exec_error)(int signo, siginfo_t *info, void *ucontext))
137935b9d88eSArnaldo Carvalho de Melo {
138035b9d88eSArnaldo Carvalho de Melo 	int child_ready_pipe[2], go_pipe[2];
138135b9d88eSArnaldo Carvalho de Melo 	char bf;
138235b9d88eSArnaldo Carvalho de Melo 
138335b9d88eSArnaldo Carvalho de Melo 	if (pipe(child_ready_pipe) < 0) {
138435b9d88eSArnaldo Carvalho de Melo 		perror("failed to create 'ready' pipe");
138535b9d88eSArnaldo Carvalho de Melo 		return -1;
138635b9d88eSArnaldo Carvalho de Melo 	}
138735b9d88eSArnaldo Carvalho de Melo 
138835b9d88eSArnaldo Carvalho de Melo 	if (pipe(go_pipe) < 0) {
138935b9d88eSArnaldo Carvalho de Melo 		perror("failed to create 'go' pipe");
139035b9d88eSArnaldo Carvalho de Melo 		goto out_close_ready_pipe;
139135b9d88eSArnaldo Carvalho de Melo 	}
139235b9d88eSArnaldo Carvalho de Melo 
139335b9d88eSArnaldo Carvalho de Melo 	evlist->workload.pid = fork();
139435b9d88eSArnaldo Carvalho de Melo 	if (evlist->workload.pid < 0) {
139535b9d88eSArnaldo Carvalho de Melo 		perror("failed to fork");
139635b9d88eSArnaldo Carvalho de Melo 		goto out_close_pipes;
139735b9d88eSArnaldo Carvalho de Melo 	}
139835b9d88eSArnaldo Carvalho de Melo 
139935b9d88eSArnaldo Carvalho de Melo 	if (!evlist->workload.pid) {
14005f1c4225SArnaldo Carvalho de Melo 		int ret;
14015f1c4225SArnaldo Carvalho de Melo 
1402119fa3c9SNamhyung Kim 		if (pipe_output)
140335b9d88eSArnaldo Carvalho de Melo 			dup2(2, 1);
140435b9d88eSArnaldo Carvalho de Melo 
14050817df08SDavid Ahern 		signal(SIGTERM, SIG_DFL);
14060817df08SDavid Ahern 
140735b9d88eSArnaldo Carvalho de Melo 		close(child_ready_pipe[0]);
140835b9d88eSArnaldo Carvalho de Melo 		close(go_pipe[1]);
140935b9d88eSArnaldo Carvalho de Melo 		fcntl(go_pipe[0], F_SETFD, FD_CLOEXEC);
141035b9d88eSArnaldo Carvalho de Melo 
141135b9d88eSArnaldo Carvalho de Melo 		/*
1412a7672d1dSArnaldo Carvalho de Melo 		 * Change the name of this process not to confuse --exclude-perf users
1413a7672d1dSArnaldo Carvalho de Melo 		 * that sees 'perf' in the window up to the execvp() and thinks that
1414a7672d1dSArnaldo Carvalho de Melo 		 * perf samples are not being excluded.
1415a7672d1dSArnaldo Carvalho de Melo 		 */
1416a7672d1dSArnaldo Carvalho de Melo 		prctl(PR_SET_NAME, "perf-exec");
1417a7672d1dSArnaldo Carvalho de Melo 
1418a7672d1dSArnaldo Carvalho de Melo 		/*
141935b9d88eSArnaldo Carvalho de Melo 		 * Tell the parent we're ready to go
142035b9d88eSArnaldo Carvalho de Melo 		 */
142135b9d88eSArnaldo Carvalho de Melo 		close(child_ready_pipe[1]);
142235b9d88eSArnaldo Carvalho de Melo 
142335b9d88eSArnaldo Carvalho de Melo 		/*
142435b9d88eSArnaldo Carvalho de Melo 		 * Wait until the parent tells us to go.
142535b9d88eSArnaldo Carvalho de Melo 		 */
14265f1c4225SArnaldo Carvalho de Melo 		ret = read(go_pipe[0], &bf, 1);
14275f1c4225SArnaldo Carvalho de Melo 		/*
14285f1c4225SArnaldo Carvalho de Melo 		 * The parent will ask for the execvp() to be performed by
14295f1c4225SArnaldo Carvalho de Melo 		 * writing exactly one byte, in workload.cork_fd, usually via
14307b392ef0SArnaldo Carvalho de Melo 		 * evlist__start_workload().
14315f1c4225SArnaldo Carvalho de Melo 		 *
143220f86fc1SArnaldo Carvalho de Melo 		 * For cancelling the workload without actually running it,
14335f1c4225SArnaldo Carvalho de Melo 		 * the parent will just close workload.cork_fd, without writing
14345f1c4225SArnaldo Carvalho de Melo 		 * anything, i.e. read will return zero and we just exit()
14355f1c4225SArnaldo Carvalho de Melo 		 * here.
14365f1c4225SArnaldo Carvalho de Melo 		 */
14375f1c4225SArnaldo Carvalho de Melo 		if (ret != 1) {
14385f1c4225SArnaldo Carvalho de Melo 			if (ret == -1)
143935b9d88eSArnaldo Carvalho de Melo 				perror("unable to read pipe");
14405f1c4225SArnaldo Carvalho de Melo 			exit(ret);
14415f1c4225SArnaldo Carvalho de Melo 		}
144235b9d88eSArnaldo Carvalho de Melo 
144335b9d88eSArnaldo Carvalho de Melo 		execvp(argv[0], (char **)argv);
144435b9d88eSArnaldo Carvalho de Melo 
1445735f7e0bSArnaldo Carvalho de Melo 		if (exec_error) {
1446f33cbe72SArnaldo Carvalho de Melo 			union sigval val;
1447f33cbe72SArnaldo Carvalho de Melo 
1448f33cbe72SArnaldo Carvalho de Melo 			val.sival_int = errno;
1449f33cbe72SArnaldo Carvalho de Melo 			if (sigqueue(getppid(), SIGUSR1, val))
145035b9d88eSArnaldo Carvalho de Melo 				perror(argv[0]);
1451f33cbe72SArnaldo Carvalho de Melo 		} else
1452f33cbe72SArnaldo Carvalho de Melo 			perror(argv[0]);
145335b9d88eSArnaldo Carvalho de Melo 		exit(-1);
145435b9d88eSArnaldo Carvalho de Melo 	}
145535b9d88eSArnaldo Carvalho de Melo 
1456735f7e0bSArnaldo Carvalho de Melo 	if (exec_error) {
1457735f7e0bSArnaldo Carvalho de Melo 		struct sigaction act = {
1458735f7e0bSArnaldo Carvalho de Melo 			.sa_flags     = SA_SIGINFO,
1459735f7e0bSArnaldo Carvalho de Melo 			.sa_sigaction = exec_error,
1460735f7e0bSArnaldo Carvalho de Melo 		};
1461735f7e0bSArnaldo Carvalho de Melo 		sigaction(SIGUSR1, &act, NULL);
1462735f7e0bSArnaldo Carvalho de Melo 	}
1463735f7e0bSArnaldo Carvalho de Melo 
14641aaf63b1SArnaldo Carvalho de Melo 	if (target__none(target)) {
146503617c22SJiri Olsa 		if (evlist->core.threads == NULL) {
14661aaf63b1SArnaldo Carvalho de Melo 			fprintf(stderr, "FATAL: evlist->threads need to be set at this point (%s:%d).\n",
14671aaf63b1SArnaldo Carvalho de Melo 				__func__, __LINE__);
14681aaf63b1SArnaldo Carvalho de Melo 			goto out_close_pipes;
14691aaf63b1SArnaldo Carvalho de Melo 		}
147003617c22SJiri Olsa 		perf_thread_map__set_pid(evlist->core.threads, 0, evlist->workload.pid);
14711aaf63b1SArnaldo Carvalho de Melo 	}
147235b9d88eSArnaldo Carvalho de Melo 
147335b9d88eSArnaldo Carvalho de Melo 	close(child_ready_pipe[1]);
147435b9d88eSArnaldo Carvalho de Melo 	close(go_pipe[0]);
147535b9d88eSArnaldo Carvalho de Melo 	/*
147635b9d88eSArnaldo Carvalho de Melo 	 * wait for child to settle
147735b9d88eSArnaldo Carvalho de Melo 	 */
147835b9d88eSArnaldo Carvalho de Melo 	if (read(child_ready_pipe[0], &bf, 1) == -1) {
147935b9d88eSArnaldo Carvalho de Melo 		perror("unable to read pipe");
148035b9d88eSArnaldo Carvalho de Melo 		goto out_close_pipes;
148135b9d88eSArnaldo Carvalho de Melo 	}
148235b9d88eSArnaldo Carvalho de Melo 
1483bcf3145fSNamhyung Kim 	fcntl(go_pipe[1], F_SETFD, FD_CLOEXEC);
148435b9d88eSArnaldo Carvalho de Melo 	evlist->workload.cork_fd = go_pipe[1];
148535b9d88eSArnaldo Carvalho de Melo 	close(child_ready_pipe[0]);
148635b9d88eSArnaldo Carvalho de Melo 	return 0;
148735b9d88eSArnaldo Carvalho de Melo 
148835b9d88eSArnaldo Carvalho de Melo out_close_pipes:
148935b9d88eSArnaldo Carvalho de Melo 	close(go_pipe[0]);
149035b9d88eSArnaldo Carvalho de Melo 	close(go_pipe[1]);
149135b9d88eSArnaldo Carvalho de Melo out_close_ready_pipe:
149235b9d88eSArnaldo Carvalho de Melo 	close(child_ready_pipe[0]);
149335b9d88eSArnaldo Carvalho de Melo 	close(child_ready_pipe[1]);
149435b9d88eSArnaldo Carvalho de Melo 	return -1;
149535b9d88eSArnaldo Carvalho de Melo }
149635b9d88eSArnaldo Carvalho de Melo 
14977b392ef0SArnaldo Carvalho de Melo int evlist__start_workload(struct evlist *evlist)
149835b9d88eSArnaldo Carvalho de Melo {
149935b9d88eSArnaldo Carvalho de Melo 	if (evlist->workload.cork_fd > 0) {
1500b3824404SDavid Ahern 		char bf = 0;
1501bcf3145fSNamhyung Kim 		int ret;
150235b9d88eSArnaldo Carvalho de Melo 		/*
150335b9d88eSArnaldo Carvalho de Melo 		 * Remove the cork, let it rip!
150435b9d88eSArnaldo Carvalho de Melo 		 */
1505bcf3145fSNamhyung Kim 		ret = write(evlist->workload.cork_fd, &bf, 1);
1506bcf3145fSNamhyung Kim 		if (ret < 0)
1507e978be9eSSoramichi Akiyama 			perror("unable to write to pipe");
1508bcf3145fSNamhyung Kim 
1509bcf3145fSNamhyung Kim 		close(evlist->workload.cork_fd);
1510bcf3145fSNamhyung Kim 		return ret;
151135b9d88eSArnaldo Carvalho de Melo 	}
151235b9d88eSArnaldo Carvalho de Melo 
151335b9d88eSArnaldo Carvalho de Melo 	return 0;
151435b9d88eSArnaldo Carvalho de Melo }
1515cb0b29e0SArnaldo Carvalho de Melo 
15162a6599cdSArnaldo Carvalho de Melo int evlist__parse_sample(struct evlist *evlist, union perf_event *event, struct perf_sample *sample)
1517cb0b29e0SArnaldo Carvalho de Melo {
15183ccf8a7bSArnaldo Carvalho de Melo 	struct evsel *evsel = evlist__event2evsel(evlist, event);
151975562573SAdrian Hunter 
152075562573SAdrian Hunter 	if (!evsel)
152175562573SAdrian Hunter 		return -EFAULT;
15226b6017a2SArnaldo Carvalho de Melo 	return evsel__parse_sample(evsel, event, sample);
1523cb0b29e0SArnaldo Carvalho de Melo }
152478f067b3SArnaldo Carvalho de Melo 
15252a6599cdSArnaldo Carvalho de Melo int evlist__parse_sample_timestamp(struct evlist *evlist, union perf_event *event, u64 *timestamp)
152601468120SJiri Olsa {
15273ccf8a7bSArnaldo Carvalho de Melo 	struct evsel *evsel = evlist__event2evsel(evlist, event);
152801468120SJiri Olsa 
152901468120SJiri Olsa 	if (!evsel)
153001468120SJiri Olsa 		return -EFAULT;
15316b6017a2SArnaldo Carvalho de Melo 	return evsel__parse_sample_timestamp(evsel, event, timestamp);
153201468120SJiri Olsa }
153301468120SJiri Olsa 
1534d1f249ecSArnaldo Carvalho de Melo int evlist__strerror_open(struct evlist *evlist, int err, char *buf, size_t size)
1535a8f23d8fSArnaldo Carvalho de Melo {
1536a8f23d8fSArnaldo Carvalho de Melo 	int printed, value;
1537c8b5f2c9SArnaldo Carvalho de Melo 	char sbuf[STRERR_BUFSIZE], *emsg = str_error_r(err, sbuf, sizeof(sbuf));
1538a8f23d8fSArnaldo Carvalho de Melo 
1539a8f23d8fSArnaldo Carvalho de Melo 	switch (err) {
1540a8f23d8fSArnaldo Carvalho de Melo 	case EACCES:
1541a8f23d8fSArnaldo Carvalho de Melo 	case EPERM:
1542a8f23d8fSArnaldo Carvalho de Melo 		printed = scnprintf(buf, size,
1543a8f23d8fSArnaldo Carvalho de Melo 				    "Error:\t%s.\n"
1544a8f23d8fSArnaldo Carvalho de Melo 				    "Hint:\tCheck /proc/sys/kernel/perf_event_paranoid setting.", emsg);
1545a8f23d8fSArnaldo Carvalho de Melo 
15461a47245dSAdrian Hunter 		value = perf_event_paranoid();
1547a8f23d8fSArnaldo Carvalho de Melo 
1548a8f23d8fSArnaldo Carvalho de Melo 		printed += scnprintf(buf + printed, size - printed, "\nHint:\t");
1549a8f23d8fSArnaldo Carvalho de Melo 
1550a8f23d8fSArnaldo Carvalho de Melo 		if (value >= 2) {
1551a8f23d8fSArnaldo Carvalho de Melo 			printed += scnprintf(buf + printed, size - printed,
1552a8f23d8fSArnaldo Carvalho de Melo 					     "For your workloads it needs to be <= 1\nHint:\t");
1553a8f23d8fSArnaldo Carvalho de Melo 		}
1554a8f23d8fSArnaldo Carvalho de Melo 		printed += scnprintf(buf + printed, size - printed,
15555229e366SArnaldo Carvalho de Melo 				     "For system wide tracing it needs to be set to -1.\n");
1556a8f23d8fSArnaldo Carvalho de Melo 
1557a8f23d8fSArnaldo Carvalho de Melo 		printed += scnprintf(buf + printed, size - printed,
15585229e366SArnaldo Carvalho de Melo 				    "Hint:\tTry: 'sudo sh -c \"echo -1 > /proc/sys/kernel/perf_event_paranoid\"'\n"
15595229e366SArnaldo Carvalho de Melo 				    "Hint:\tThe current value is %d.", value);
1560a8f23d8fSArnaldo Carvalho de Melo 		break;
1561d9aade7fSArnaldo Carvalho de Melo 	case EINVAL: {
1562515dbe48SJiri Olsa 		struct evsel *first = evlist__first(evlist);
1563d9aade7fSArnaldo Carvalho de Melo 		int max_freq;
1564d9aade7fSArnaldo Carvalho de Melo 
1565d9aade7fSArnaldo Carvalho de Melo 		if (sysctl__read_int("kernel/perf_event_max_sample_rate", &max_freq) < 0)
1566d9aade7fSArnaldo Carvalho de Melo 			goto out_default;
1567d9aade7fSArnaldo Carvalho de Melo 
15681fc632ceSJiri Olsa 		if (first->core.attr.sample_freq < (u64)max_freq)
1569d9aade7fSArnaldo Carvalho de Melo 			goto out_default;
1570d9aade7fSArnaldo Carvalho de Melo 
1571d9aade7fSArnaldo Carvalho de Melo 		printed = scnprintf(buf, size,
1572d9aade7fSArnaldo Carvalho de Melo 				    "Error:\t%s.\n"
1573d9aade7fSArnaldo Carvalho de Melo 				    "Hint:\tCheck /proc/sys/kernel/perf_event_max_sample_rate.\n"
1574d9aade7fSArnaldo Carvalho de Melo 				    "Hint:\tThe current value is %d and %" PRIu64 " is being requested.",
15751fc632ceSJiri Olsa 				    emsg, max_freq, first->core.attr.sample_freq);
1576d9aade7fSArnaldo Carvalho de Melo 		break;
1577d9aade7fSArnaldo Carvalho de Melo 	}
1578a8f23d8fSArnaldo Carvalho de Melo 	default:
1579d9aade7fSArnaldo Carvalho de Melo out_default:
1580a8f23d8fSArnaldo Carvalho de Melo 		scnprintf(buf, size, "%s", emsg);
1581a8f23d8fSArnaldo Carvalho de Melo 		break;
1582a8f23d8fSArnaldo Carvalho de Melo 	}
1583a8f23d8fSArnaldo Carvalho de Melo 
1584a8f23d8fSArnaldo Carvalho de Melo 	return 0;
1585a8f23d8fSArnaldo Carvalho de Melo }
1586a025e4f0SAdrian Hunter 
1587d1f249ecSArnaldo Carvalho de Melo int evlist__strerror_mmap(struct evlist *evlist, int err, char *buf, size_t size)
1588956fa571SArnaldo Carvalho de Melo {
1589c8b5f2c9SArnaldo Carvalho de Melo 	char sbuf[STRERR_BUFSIZE], *emsg = str_error_r(err, sbuf, sizeof(sbuf));
1590f6fa4375SJiri Olsa 	int pages_attempted = evlist->core.mmap_len / 1024, pages_max_per_user, printed = 0;
1591956fa571SArnaldo Carvalho de Melo 
1592956fa571SArnaldo Carvalho de Melo 	switch (err) {
1593956fa571SArnaldo Carvalho de Melo 	case EPERM:
1594e5d4a290SArnaldo Carvalho de Melo 		sysctl__read_int("kernel/perf_event_mlock_kb", &pages_max_per_user);
1595e965bea1SArnaldo Carvalho de Melo 		printed += scnprintf(buf + printed, size - printed,
1596e965bea1SArnaldo Carvalho de Melo 				     "Error:\t%s.\n"
1597956fa571SArnaldo Carvalho de Melo 				     "Hint:\tCheck /proc/sys/kernel/perf_event_mlock_kb (%d kB) setting.\n"
1598e965bea1SArnaldo Carvalho de Melo 				     "Hint:\tTried using %zd kB.\n",
1599e5d4a290SArnaldo Carvalho de Melo 				     emsg, pages_max_per_user, pages_attempted);
1600e965bea1SArnaldo Carvalho de Melo 
1601e965bea1SArnaldo Carvalho de Melo 		if (pages_attempted >= pages_max_per_user) {
1602e965bea1SArnaldo Carvalho de Melo 			printed += scnprintf(buf + printed, size - printed,
1603e965bea1SArnaldo Carvalho de Melo 					     "Hint:\tTry 'sudo sh -c \"echo %d > /proc/sys/kernel/perf_event_mlock_kb\"', or\n",
1604e965bea1SArnaldo Carvalho de Melo 					     pages_max_per_user + pages_attempted);
1605e965bea1SArnaldo Carvalho de Melo 		}
1606e965bea1SArnaldo Carvalho de Melo 
1607e965bea1SArnaldo Carvalho de Melo 		printed += scnprintf(buf + printed, size - printed,
1608e965bea1SArnaldo Carvalho de Melo 				     "Hint:\tTry using a smaller -m/--mmap-pages value.");
1609956fa571SArnaldo Carvalho de Melo 		break;
1610956fa571SArnaldo Carvalho de Melo 	default:
1611956fa571SArnaldo Carvalho de Melo 		scnprintf(buf, size, "%s", emsg);
1612956fa571SArnaldo Carvalho de Melo 		break;
1613956fa571SArnaldo Carvalho de Melo 	}
1614956fa571SArnaldo Carvalho de Melo 
1615956fa571SArnaldo Carvalho de Melo 	return 0;
1616956fa571SArnaldo Carvalho de Melo }
1617956fa571SArnaldo Carvalho de Melo 
1618e414fd1aSArnaldo Carvalho de Melo void evlist__to_front(struct evlist *evlist, struct evsel *move_evsel)
1619a025e4f0SAdrian Hunter {
162032dcd021SJiri Olsa 	struct evsel *evsel, *n;
1621a025e4f0SAdrian Hunter 	LIST_HEAD(move);
1622a025e4f0SAdrian Hunter 
1623515dbe48SJiri Olsa 	if (move_evsel == evlist__first(evlist))
1624a025e4f0SAdrian Hunter 		return;
1625a025e4f0SAdrian Hunter 
1626e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry_safe(evlist, n, evsel) {
1627fba7c866SJiri Olsa 		if (evsel__leader(evsel) == evsel__leader(move_evsel))
1628b27c4eceSJiri Olsa 			list_move_tail(&evsel->core.node, &move);
1629a025e4f0SAdrian Hunter 	}
1630a025e4f0SAdrian Hunter 
1631ce9036a6SJiri Olsa 	list_splice(&move, &evlist->core.entries);
1632a025e4f0SAdrian Hunter }
163360b0896cSAdrian Hunter 
1634e80db255SArnaldo Carvalho de Melo struct evsel *evlist__get_tracking_event(struct evlist *evlist)
1635442ad225SAdrian Hunter {
1636442ad225SAdrian Hunter 	struct evsel *evsel;
1637442ad225SAdrian Hunter 
1638442ad225SAdrian Hunter 	evlist__for_each_entry(evlist, evsel) {
1639442ad225SAdrian Hunter 		if (evsel->tracking)
1640442ad225SAdrian Hunter 			return evsel;
1641442ad225SAdrian Hunter 	}
1642442ad225SAdrian Hunter 
1643442ad225SAdrian Hunter 	return evlist__first(evlist);
1644442ad225SAdrian Hunter }
1645442ad225SAdrian Hunter 
1646e80db255SArnaldo Carvalho de Melo void evlist__set_tracking_event(struct evlist *evlist, struct evsel *tracking_evsel)
164760b0896cSAdrian Hunter {
164832dcd021SJiri Olsa 	struct evsel *evsel;
164960b0896cSAdrian Hunter 
165060b0896cSAdrian Hunter 	if (tracking_evsel->tracking)
165160b0896cSAdrian Hunter 		return;
165260b0896cSAdrian Hunter 
1653e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, evsel) {
165460b0896cSAdrian Hunter 		if (evsel != tracking_evsel)
165560b0896cSAdrian Hunter 			evsel->tracking = false;
165660b0896cSAdrian Hunter 	}
165760b0896cSAdrian Hunter 
165860b0896cSAdrian Hunter 	tracking_evsel->tracking = true;
165960b0896cSAdrian Hunter }
16607630b3e2SWang Nan 
1661b02736f7SArnaldo Carvalho de Melo struct evsel *evlist__find_evsel_by_str(struct evlist *evlist, const char *str)
16627630b3e2SWang Nan {
166332dcd021SJiri Olsa 	struct evsel *evsel;
16647630b3e2SWang Nan 
1665e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, evsel) {
16667630b3e2SWang Nan 		if (!evsel->name)
16677630b3e2SWang Nan 			continue;
16687630b3e2SWang Nan 		if (strcmp(str, evsel->name) == 0)
16697630b3e2SWang Nan 			return evsel;
16707630b3e2SWang Nan 	}
16717630b3e2SWang Nan 
16727630b3e2SWang Nan 	return NULL;
16737630b3e2SWang Nan }
167454cc54deSWang Nan 
1675ade9d208SArnaldo Carvalho de Melo void evlist__toggle_bkw_mmap(struct evlist *evlist, enum bkw_mmap_state state)
167654cc54deSWang Nan {
167754cc54deSWang Nan 	enum bkw_mmap_state old_state = evlist->bkw_mmap_state;
167854cc54deSWang Nan 	enum action {
167954cc54deSWang Nan 		NONE,
168054cc54deSWang Nan 		PAUSE,
168154cc54deSWang Nan 		RESUME,
168254cc54deSWang Nan 	} action = NONE;
168354cc54deSWang Nan 
16840b72d69aSWang Nan 	if (!evlist->overwrite_mmap)
168554cc54deSWang Nan 		return;
168654cc54deSWang Nan 
168754cc54deSWang Nan 	switch (old_state) {
168854cc54deSWang Nan 	case BKW_MMAP_NOTREADY: {
168954cc54deSWang Nan 		if (state != BKW_MMAP_RUNNING)
1690dd8bd53aSLuis de Bethencourt 			goto state_err;
169154cc54deSWang Nan 		break;
169254cc54deSWang Nan 	}
169354cc54deSWang Nan 	case BKW_MMAP_RUNNING: {
169454cc54deSWang Nan 		if (state != BKW_MMAP_DATA_PENDING)
169554cc54deSWang Nan 			goto state_err;
169654cc54deSWang Nan 		action = PAUSE;
169754cc54deSWang Nan 		break;
169854cc54deSWang Nan 	}
169954cc54deSWang Nan 	case BKW_MMAP_DATA_PENDING: {
170054cc54deSWang Nan 		if (state != BKW_MMAP_EMPTY)
170154cc54deSWang Nan 			goto state_err;
170254cc54deSWang Nan 		break;
170354cc54deSWang Nan 	}
170454cc54deSWang Nan 	case BKW_MMAP_EMPTY: {
170554cc54deSWang Nan 		if (state != BKW_MMAP_RUNNING)
170654cc54deSWang Nan 			goto state_err;
170754cc54deSWang Nan 		action = RESUME;
170854cc54deSWang Nan 		break;
170954cc54deSWang Nan 	}
171054cc54deSWang Nan 	default:
171154cc54deSWang Nan 		WARN_ONCE(1, "Shouldn't get there\n");
171254cc54deSWang Nan 	}
171354cc54deSWang Nan 
171454cc54deSWang Nan 	evlist->bkw_mmap_state = state;
171554cc54deSWang Nan 
171654cc54deSWang Nan 	switch (action) {
171754cc54deSWang Nan 	case PAUSE:
17180a60b339SArnaldo Carvalho de Melo 		evlist__pause(evlist);
171954cc54deSWang Nan 		break;
172054cc54deSWang Nan 	case RESUME:
17210a60b339SArnaldo Carvalho de Melo 		evlist__resume(evlist);
172254cc54deSWang Nan 		break;
172354cc54deSWang Nan 	case NONE:
172454cc54deSWang Nan 	default:
172554cc54deSWang Nan 		break;
172654cc54deSWang Nan 	}
172754cc54deSWang Nan 
172854cc54deSWang Nan state_err:
172954cc54deSWang Nan 	return;
173054cc54deSWang Nan }
173107d6f446SArnaldo Carvalho de Melo 
173278e1bc25SArnaldo Carvalho de Melo bool evlist__exclude_kernel(struct evlist *evlist)
173307d6f446SArnaldo Carvalho de Melo {
173432dcd021SJiri Olsa 	struct evsel *evsel;
173507d6f446SArnaldo Carvalho de Melo 
173607d6f446SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, evsel) {
17371fc632ceSJiri Olsa 		if (!evsel->core.attr.exclude_kernel)
173807d6f446SArnaldo Carvalho de Melo 			return false;
173907d6f446SArnaldo Carvalho de Melo 	}
174007d6f446SArnaldo Carvalho de Melo 
174107d6f446SArnaldo Carvalho de Melo 	return true;
174207d6f446SArnaldo Carvalho de Melo }
1743e2bdbe80SJin Yao 
1744e2bdbe80SJin Yao /*
1745e2bdbe80SJin Yao  * Events in data file are not collect in groups, but we still want
1746e2bdbe80SJin Yao  * the group display. Set the artificial group and set the leader's
1747e2bdbe80SJin Yao  * forced_leader flag to notify the display code.
1748e2bdbe80SJin Yao  */
174964b4778bSArnaldo Carvalho de Melo void evlist__force_leader(struct evlist *evlist)
1750e2bdbe80SJin Yao {
17513a683120SJiri Olsa 	if (!evlist->core.nr_groups) {
1752515dbe48SJiri Olsa 		struct evsel *leader = evlist__first(evlist);
1753e2bdbe80SJin Yao 
1754a622eafaSArnaldo Carvalho de Melo 		evlist__set_leader(evlist);
1755e2bdbe80SJin Yao 		leader->forced_leader = true;
1756e2bdbe80SJin Yao 	}
1757e2bdbe80SJin Yao }
1758c3537fc2SAndi Kleen 
175964b4778bSArnaldo Carvalho de Melo struct evsel *evlist__reset_weak_group(struct evlist *evsel_list, struct evsel *evsel, bool close)
1760c3537fc2SAndi Kleen {
176132dcd021SJiri Olsa 	struct evsel *c2, *leader;
1762c3537fc2SAndi Kleen 	bool is_open = true;
1763c3537fc2SAndi Kleen 
1764fba7c866SJiri Olsa 	leader = evsel__leader(evsel);
1765fba7c866SJiri Olsa 
1766c3537fc2SAndi Kleen 	pr_debug("Weak group for %s/%d failed\n",
17675643b1a5SJiri Olsa 			leader->name, leader->core.nr_members);
1768c3537fc2SAndi Kleen 
1769c3537fc2SAndi Kleen 	/*
1770c3537fc2SAndi Kleen 	 * for_each_group_member doesn't work here because it doesn't
1771c3537fc2SAndi Kleen 	 * include the first entry.
1772c3537fc2SAndi Kleen 	 */
1773c3537fc2SAndi Kleen 	evlist__for_each_entry(evsel_list, c2) {
1774c3537fc2SAndi Kleen 		if (c2 == evsel)
1775c3537fc2SAndi Kleen 			is_open = false;
1776fba7c866SJiri Olsa 		if (evsel__has_leader(c2, leader)) {
17774804e011SAndi Kleen 			if (is_open && close)
17785a40e199SAndi Kleen 				perf_evsel__close(&c2->core);
1779fba7c866SJiri Olsa 			evsel__set_leader(c2, c2);
17805643b1a5SJiri Olsa 			c2->core.nr_members = 0;
17814804e011SAndi Kleen 			/*
17824804e011SAndi Kleen 			 * Set this for all former members of the group
17834804e011SAndi Kleen 			 * to indicate they get reopened.
17844804e011SAndi Kleen 			 */
17854804e011SAndi Kleen 			c2->reset_group = true;
1786c3537fc2SAndi Kleen 		}
1787c3537fc2SAndi Kleen 	}
1788c3537fc2SAndi Kleen 	return leader;
1789c3537fc2SAndi Kleen }
1790ec886bf5SAlexey Budankov 
1791a8fcbd26SAdrian Hunter static int evlist__parse_control_fifo(const char *str, int *ctl_fd, int *ctl_fd_ack, bool *ctl_fd_close)
1792a8fcbd26SAdrian Hunter {
1793a8fcbd26SAdrian Hunter 	char *s, *p;
1794a8fcbd26SAdrian Hunter 	int ret = 0, fd;
1795a8fcbd26SAdrian Hunter 
1796a8fcbd26SAdrian Hunter 	if (strncmp(str, "fifo:", 5))
1797a8fcbd26SAdrian Hunter 		return -EINVAL;
1798a8fcbd26SAdrian Hunter 
1799a8fcbd26SAdrian Hunter 	str += 5;
1800a8fcbd26SAdrian Hunter 	if (!*str || *str == ',')
1801a8fcbd26SAdrian Hunter 		return -EINVAL;
1802a8fcbd26SAdrian Hunter 
1803a8fcbd26SAdrian Hunter 	s = strdup(str);
1804a8fcbd26SAdrian Hunter 	if (!s)
1805a8fcbd26SAdrian Hunter 		return -ENOMEM;
1806a8fcbd26SAdrian Hunter 
1807a8fcbd26SAdrian Hunter 	p = strchr(s, ',');
1808a8fcbd26SAdrian Hunter 	if (p)
1809a8fcbd26SAdrian Hunter 		*p = '\0';
1810a8fcbd26SAdrian Hunter 
1811a8fcbd26SAdrian Hunter 	/*
1812a8fcbd26SAdrian Hunter 	 * O_RDWR avoids POLLHUPs which is necessary to allow the other
1813a8fcbd26SAdrian Hunter 	 * end of a FIFO to be repeatedly opened and closed.
1814a8fcbd26SAdrian Hunter 	 */
1815a8fcbd26SAdrian Hunter 	fd = open(s, O_RDWR | O_NONBLOCK | O_CLOEXEC);
1816a8fcbd26SAdrian Hunter 	if (fd < 0) {
1817a8fcbd26SAdrian Hunter 		pr_err("Failed to open '%s'\n", s);
1818a8fcbd26SAdrian Hunter 		ret = -errno;
1819a8fcbd26SAdrian Hunter 		goto out_free;
1820a8fcbd26SAdrian Hunter 	}
1821a8fcbd26SAdrian Hunter 	*ctl_fd = fd;
1822a8fcbd26SAdrian Hunter 	*ctl_fd_close = true;
1823a8fcbd26SAdrian Hunter 
1824a8fcbd26SAdrian Hunter 	if (p && *++p) {
1825a8fcbd26SAdrian Hunter 		/* O_RDWR | O_NONBLOCK means the other end need not be open */
1826a8fcbd26SAdrian Hunter 		fd = open(p, O_RDWR | O_NONBLOCK | O_CLOEXEC);
1827a8fcbd26SAdrian Hunter 		if (fd < 0) {
1828a8fcbd26SAdrian Hunter 			pr_err("Failed to open '%s'\n", p);
1829a8fcbd26SAdrian Hunter 			ret = -errno;
1830a8fcbd26SAdrian Hunter 			goto out_free;
1831a8fcbd26SAdrian Hunter 		}
1832a8fcbd26SAdrian Hunter 		*ctl_fd_ack = fd;
1833a8fcbd26SAdrian Hunter 	}
1834a8fcbd26SAdrian Hunter 
1835a8fcbd26SAdrian Hunter out_free:
1836a8fcbd26SAdrian Hunter 	free(s);
1837a8fcbd26SAdrian Hunter 	return ret;
1838a8fcbd26SAdrian Hunter }
1839a8fcbd26SAdrian Hunter 
1840a8fcbd26SAdrian Hunter int evlist__parse_control(const char *str, int *ctl_fd, int *ctl_fd_ack, bool *ctl_fd_close)
18419864a66dSAdrian Hunter {
18429864a66dSAdrian Hunter 	char *comma = NULL, *endptr = NULL;
18439864a66dSAdrian Hunter 
1844a8fcbd26SAdrian Hunter 	*ctl_fd_close = false;
1845a8fcbd26SAdrian Hunter 
18469864a66dSAdrian Hunter 	if (strncmp(str, "fd:", 3))
1847a8fcbd26SAdrian Hunter 		return evlist__parse_control_fifo(str, ctl_fd, ctl_fd_ack, ctl_fd_close);
18489864a66dSAdrian Hunter 
18499864a66dSAdrian Hunter 	*ctl_fd = strtoul(&str[3], &endptr, 0);
18509864a66dSAdrian Hunter 	if (endptr == &str[3])
18519864a66dSAdrian Hunter 		return -EINVAL;
18529864a66dSAdrian Hunter 
18539864a66dSAdrian Hunter 	comma = strchr(str, ',');
18549864a66dSAdrian Hunter 	if (comma) {
18559864a66dSAdrian Hunter 		if (endptr != comma)
18569864a66dSAdrian Hunter 			return -EINVAL;
18579864a66dSAdrian Hunter 
18589864a66dSAdrian Hunter 		*ctl_fd_ack = strtoul(comma + 1, &endptr, 0);
18599864a66dSAdrian Hunter 		if (endptr == comma + 1 || *endptr != '\0')
18609864a66dSAdrian Hunter 			return -EINVAL;
18619864a66dSAdrian Hunter 	}
18629864a66dSAdrian Hunter 
18639864a66dSAdrian Hunter 	return 0;
18649864a66dSAdrian Hunter }
18659864a66dSAdrian Hunter 
1866ee7fe31eSAdrian Hunter void evlist__close_control(int ctl_fd, int ctl_fd_ack, bool *ctl_fd_close)
1867ee7fe31eSAdrian Hunter {
1868ee7fe31eSAdrian Hunter 	if (*ctl_fd_close) {
1869ee7fe31eSAdrian Hunter 		*ctl_fd_close = false;
1870ee7fe31eSAdrian Hunter 		close(ctl_fd);
1871ee7fe31eSAdrian Hunter 		if (ctl_fd_ack >= 0)
1872ee7fe31eSAdrian Hunter 			close(ctl_fd_ack);
1873ee7fe31eSAdrian Hunter 	}
1874ee7fe31eSAdrian Hunter }
1875ee7fe31eSAdrian Hunter 
1876ec886bf5SAlexey Budankov int evlist__initialize_ctlfd(struct evlist *evlist, int fd, int ack)
1877ec886bf5SAlexey Budankov {
1878ec886bf5SAlexey Budankov 	if (fd == -1) {
1879ec886bf5SAlexey Budankov 		pr_debug("Control descriptor is not initialized\n");
1880ec886bf5SAlexey Budankov 		return 0;
1881ec886bf5SAlexey Budankov 	}
1882ec886bf5SAlexey Budankov 
1883ec886bf5SAlexey Budankov 	evlist->ctl_fd.pos = perf_evlist__add_pollfd(&evlist->core, fd, NULL, POLLIN,
1884ec886bf5SAlexey Budankov 						     fdarray_flag__nonfilterable);
1885ec886bf5SAlexey Budankov 	if (evlist->ctl_fd.pos < 0) {
1886ec886bf5SAlexey Budankov 		evlist->ctl_fd.pos = -1;
1887ec886bf5SAlexey Budankov 		pr_err("Failed to add ctl fd entry: %m\n");
1888ec886bf5SAlexey Budankov 		return -1;
1889ec886bf5SAlexey Budankov 	}
1890ec886bf5SAlexey Budankov 
1891ec886bf5SAlexey Budankov 	evlist->ctl_fd.fd = fd;
1892ec886bf5SAlexey Budankov 	evlist->ctl_fd.ack = ack;
1893ec886bf5SAlexey Budankov 
1894ec886bf5SAlexey Budankov 	return 0;
1895ec886bf5SAlexey Budankov }
1896ec886bf5SAlexey Budankov 
1897ec886bf5SAlexey Budankov bool evlist__ctlfd_initialized(struct evlist *evlist)
1898ec886bf5SAlexey Budankov {
1899ec886bf5SAlexey Budankov 	return evlist->ctl_fd.pos >= 0;
1900ec886bf5SAlexey Budankov }
1901ec886bf5SAlexey Budankov 
1902ec886bf5SAlexey Budankov int evlist__finalize_ctlfd(struct evlist *evlist)
1903ec886bf5SAlexey Budankov {
1904ec886bf5SAlexey Budankov 	struct pollfd *entries = evlist->core.pollfd.entries;
1905ec886bf5SAlexey Budankov 
1906ec886bf5SAlexey Budankov 	if (!evlist__ctlfd_initialized(evlist))
1907ec886bf5SAlexey Budankov 		return 0;
1908ec886bf5SAlexey Budankov 
1909ec886bf5SAlexey Budankov 	entries[evlist->ctl_fd.pos].fd = -1;
1910ec886bf5SAlexey Budankov 	entries[evlist->ctl_fd.pos].events = 0;
1911ec886bf5SAlexey Budankov 	entries[evlist->ctl_fd.pos].revents = 0;
1912ec886bf5SAlexey Budankov 
1913ec886bf5SAlexey Budankov 	evlist->ctl_fd.pos = -1;
1914ec886bf5SAlexey Budankov 	evlist->ctl_fd.ack = -1;
1915ec886bf5SAlexey Budankov 	evlist->ctl_fd.fd = -1;
1916ec886bf5SAlexey Budankov 
1917ec886bf5SAlexey Budankov 	return 0;
1918ec886bf5SAlexey Budankov }
1919ec886bf5SAlexey Budankov 
1920ec886bf5SAlexey Budankov static int evlist__ctlfd_recv(struct evlist *evlist, enum evlist_ctl_cmd *cmd,
1921ec886bf5SAlexey Budankov 			      char *cmd_data, size_t data_size)
1922ec886bf5SAlexey Budankov {
1923ec886bf5SAlexey Budankov 	int err;
1924ec886bf5SAlexey Budankov 	char c;
1925ec886bf5SAlexey Budankov 	size_t bytes_read = 0;
1926ec886bf5SAlexey Budankov 
192740db8ff5SAdrian Hunter 	*cmd = EVLIST_CTL_CMD_UNSUPPORTED;
1928ec886bf5SAlexey Budankov 	memset(cmd_data, 0, data_size);
1929ec886bf5SAlexey Budankov 	data_size--;
1930ec886bf5SAlexey Budankov 
1931ec886bf5SAlexey Budankov 	do {
1932ec886bf5SAlexey Budankov 		err = read(evlist->ctl_fd.fd, &c, 1);
1933ec886bf5SAlexey Budankov 		if (err > 0) {
1934ec886bf5SAlexey Budankov 			if (c == '\n' || c == '\0')
1935ec886bf5SAlexey Budankov 				break;
1936ec886bf5SAlexey Budankov 			cmd_data[bytes_read++] = c;
1937ec886bf5SAlexey Budankov 			if (bytes_read == data_size)
1938ec886bf5SAlexey Budankov 				break;
193940db8ff5SAdrian Hunter 			continue;
194040db8ff5SAdrian Hunter 		} else if (err == -1) {
194140db8ff5SAdrian Hunter 			if (errno == EINTR)
194240db8ff5SAdrian Hunter 				continue;
194340db8ff5SAdrian Hunter 			if (errno == EAGAIN || errno == EWOULDBLOCK)
194440db8ff5SAdrian Hunter 				err = 0;
194540db8ff5SAdrian Hunter 			else
1946ec886bf5SAlexey Budankov 				pr_err("Failed to read from ctlfd %d: %m\n", evlist->ctl_fd.fd);
1947ec886bf5SAlexey Budankov 		}
194840db8ff5SAdrian Hunter 		break;
1949ec886bf5SAlexey Budankov 	} while (1);
1950ec886bf5SAlexey Budankov 
1951ec886bf5SAlexey Budankov 	pr_debug("Message from ctl_fd: \"%s%s\"\n", cmd_data,
1952ec886bf5SAlexey Budankov 		 bytes_read == data_size ? "" : c == '\n' ? "\\n" : "\\0");
1953ec886bf5SAlexey Budankov 
195440db8ff5SAdrian Hunter 	if (bytes_read > 0) {
1955ec886bf5SAlexey Budankov 		if (!strncmp(cmd_data, EVLIST_CTL_CMD_ENABLE_TAG,
1956ec886bf5SAlexey Budankov 			     (sizeof(EVLIST_CTL_CMD_ENABLE_TAG)-1))) {
1957ec886bf5SAlexey Budankov 			*cmd = EVLIST_CTL_CMD_ENABLE;
1958ec886bf5SAlexey Budankov 		} else if (!strncmp(cmd_data, EVLIST_CTL_CMD_DISABLE_TAG,
1959ec886bf5SAlexey Budankov 				    (sizeof(EVLIST_CTL_CMD_DISABLE_TAG)-1))) {
1960ec886bf5SAlexey Budankov 			*cmd = EVLIST_CTL_CMD_DISABLE;
1961d20aff15SAdrian Hunter 		} else if (!strncmp(cmd_data, EVLIST_CTL_CMD_SNAPSHOT_TAG,
1962d20aff15SAdrian Hunter 				    (sizeof(EVLIST_CTL_CMD_SNAPSHOT_TAG)-1))) {
1963d20aff15SAdrian Hunter 			*cmd = EVLIST_CTL_CMD_SNAPSHOT;
1964d20aff15SAdrian Hunter 			pr_debug("is snapshot\n");
1965142544a9SJiri Olsa 		} else if (!strncmp(cmd_data, EVLIST_CTL_CMD_EVLIST_TAG,
1966142544a9SJiri Olsa 				    (sizeof(EVLIST_CTL_CMD_EVLIST_TAG)-1))) {
1967142544a9SJiri Olsa 			*cmd = EVLIST_CTL_CMD_EVLIST;
1968f186cd61SJiri Olsa 		} else if (!strncmp(cmd_data, EVLIST_CTL_CMD_STOP_TAG,
1969f186cd61SJiri Olsa 				    (sizeof(EVLIST_CTL_CMD_STOP_TAG)-1))) {
1970f186cd61SJiri Olsa 			*cmd = EVLIST_CTL_CMD_STOP;
197147fddcb4SJiri Olsa 		} else if (!strncmp(cmd_data, EVLIST_CTL_CMD_PING_TAG,
197247fddcb4SJiri Olsa 				    (sizeof(EVLIST_CTL_CMD_PING_TAG)-1))) {
197347fddcb4SJiri Olsa 			*cmd = EVLIST_CTL_CMD_PING;
1974ec886bf5SAlexey Budankov 		}
1975ec886bf5SAlexey Budankov 	}
1976ec886bf5SAlexey Budankov 
197740db8ff5SAdrian Hunter 	return bytes_read ? (int)bytes_read : err;
1978ec886bf5SAlexey Budankov }
1979ec886bf5SAlexey Budankov 
1980d20aff15SAdrian Hunter int evlist__ctlfd_ack(struct evlist *evlist)
1981ec886bf5SAlexey Budankov {
1982ec886bf5SAlexey Budankov 	int err;
1983ec886bf5SAlexey Budankov 
1984ec886bf5SAlexey Budankov 	if (evlist->ctl_fd.ack == -1)
1985ec886bf5SAlexey Budankov 		return 0;
1986ec886bf5SAlexey Budankov 
1987ec886bf5SAlexey Budankov 	err = write(evlist->ctl_fd.ack, EVLIST_CTL_CMD_ACK_TAG,
1988ec886bf5SAlexey Budankov 		    sizeof(EVLIST_CTL_CMD_ACK_TAG));
1989ec886bf5SAlexey Budankov 	if (err == -1)
1990ec886bf5SAlexey Budankov 		pr_err("failed to write to ctl_ack_fd %d: %m\n", evlist->ctl_fd.ack);
1991ec886bf5SAlexey Budankov 
1992ec886bf5SAlexey Budankov 	return err;
1993ec886bf5SAlexey Budankov }
1994ec886bf5SAlexey Budankov 
1995991ae4ebSJiri Olsa static int get_cmd_arg(char *cmd_data, size_t cmd_size, char **arg)
1996991ae4ebSJiri Olsa {
1997991ae4ebSJiri Olsa 	char *data = cmd_data + cmd_size;
1998991ae4ebSJiri Olsa 
1999991ae4ebSJiri Olsa 	/* no argument */
2000991ae4ebSJiri Olsa 	if (!*data)
2001991ae4ebSJiri Olsa 		return 0;
2002991ae4ebSJiri Olsa 
2003991ae4ebSJiri Olsa 	/* there's argument */
2004991ae4ebSJiri Olsa 	if (*data == ' ') {
2005991ae4ebSJiri Olsa 		*arg = data + 1;
2006991ae4ebSJiri Olsa 		return 1;
2007991ae4ebSJiri Olsa 	}
2008991ae4ebSJiri Olsa 
2009991ae4ebSJiri Olsa 	/* malformed */
2010991ae4ebSJiri Olsa 	return -1;
2011991ae4ebSJiri Olsa }
2012991ae4ebSJiri Olsa 
2013991ae4ebSJiri Olsa static int evlist__ctlfd_enable(struct evlist *evlist, char *cmd_data, bool enable)
2014991ae4ebSJiri Olsa {
2015991ae4ebSJiri Olsa 	struct evsel *evsel;
2016991ae4ebSJiri Olsa 	char *name;
2017991ae4ebSJiri Olsa 	int err;
2018991ae4ebSJiri Olsa 
2019991ae4ebSJiri Olsa 	err = get_cmd_arg(cmd_data,
2020991ae4ebSJiri Olsa 			  enable ? sizeof(EVLIST_CTL_CMD_ENABLE_TAG) - 1 :
2021991ae4ebSJiri Olsa 				   sizeof(EVLIST_CTL_CMD_DISABLE_TAG) - 1,
2022991ae4ebSJiri Olsa 			  &name);
2023991ae4ebSJiri Olsa 	if (err < 0) {
2024991ae4ebSJiri Olsa 		pr_info("failed: wrong command\n");
2025991ae4ebSJiri Olsa 		return -1;
2026991ae4ebSJiri Olsa 	}
2027991ae4ebSJiri Olsa 
2028991ae4ebSJiri Olsa 	if (err) {
2029991ae4ebSJiri Olsa 		evsel = evlist__find_evsel_by_str(evlist, name);
2030991ae4ebSJiri Olsa 		if (evsel) {
2031991ae4ebSJiri Olsa 			if (enable)
2032991ae4ebSJiri Olsa 				evlist__enable_evsel(evlist, name);
2033991ae4ebSJiri Olsa 			else
2034991ae4ebSJiri Olsa 				evlist__disable_evsel(evlist, name);
2035991ae4ebSJiri Olsa 			pr_info("Event %s %s\n", evsel->name,
2036991ae4ebSJiri Olsa 				enable ? "enabled" : "disabled");
2037991ae4ebSJiri Olsa 		} else {
2038991ae4ebSJiri Olsa 			pr_info("failed: can't find '%s' event\n", name);
2039991ae4ebSJiri Olsa 		}
2040991ae4ebSJiri Olsa 	} else {
2041991ae4ebSJiri Olsa 		if (enable) {
2042991ae4ebSJiri Olsa 			evlist__enable(evlist);
2043991ae4ebSJiri Olsa 			pr_info(EVLIST_ENABLED_MSG);
2044991ae4ebSJiri Olsa 		} else {
2045991ae4ebSJiri Olsa 			evlist__disable(evlist);
2046991ae4ebSJiri Olsa 			pr_info(EVLIST_DISABLED_MSG);
2047991ae4ebSJiri Olsa 		}
2048991ae4ebSJiri Olsa 	}
2049991ae4ebSJiri Olsa 
2050991ae4ebSJiri Olsa 	return 0;
2051991ae4ebSJiri Olsa }
2052991ae4ebSJiri Olsa 
2053142544a9SJiri Olsa static int evlist__ctlfd_list(struct evlist *evlist, char *cmd_data)
2054142544a9SJiri Olsa {
2055142544a9SJiri Olsa 	struct perf_attr_details details = { .verbose = false, };
2056142544a9SJiri Olsa 	struct evsel *evsel;
2057142544a9SJiri Olsa 	char *arg;
2058142544a9SJiri Olsa 	int err;
2059142544a9SJiri Olsa 
2060142544a9SJiri Olsa 	err = get_cmd_arg(cmd_data,
2061142544a9SJiri Olsa 			  sizeof(EVLIST_CTL_CMD_EVLIST_TAG) - 1,
2062142544a9SJiri Olsa 			  &arg);
2063142544a9SJiri Olsa 	if (err < 0) {
2064142544a9SJiri Olsa 		pr_info("failed: wrong command\n");
2065142544a9SJiri Olsa 		return -1;
2066142544a9SJiri Olsa 	}
2067142544a9SJiri Olsa 
2068142544a9SJiri Olsa 	if (err) {
2069142544a9SJiri Olsa 		if (!strcmp(arg, "-v")) {
2070142544a9SJiri Olsa 			details.verbose = true;
2071142544a9SJiri Olsa 		} else if (!strcmp(arg, "-g")) {
2072142544a9SJiri Olsa 			details.event_group = true;
2073142544a9SJiri Olsa 		} else if (!strcmp(arg, "-F")) {
2074142544a9SJiri Olsa 			details.freq = true;
2075142544a9SJiri Olsa 		} else {
2076142544a9SJiri Olsa 			pr_info("failed: wrong command\n");
2077142544a9SJiri Olsa 			return -1;
2078142544a9SJiri Olsa 		}
2079142544a9SJiri Olsa 	}
2080142544a9SJiri Olsa 
2081142544a9SJiri Olsa 	evlist__for_each_entry(evlist, evsel)
2082142544a9SJiri Olsa 		evsel__fprintf(evsel, &details, stderr);
2083142544a9SJiri Olsa 
2084142544a9SJiri Olsa 	return 0;
2085142544a9SJiri Olsa }
2086142544a9SJiri Olsa 
2087ec886bf5SAlexey Budankov int evlist__ctlfd_process(struct evlist *evlist, enum evlist_ctl_cmd *cmd)
2088ec886bf5SAlexey Budankov {
2089ec886bf5SAlexey Budankov 	int err = 0;
2090ec886bf5SAlexey Budankov 	char cmd_data[EVLIST_CTL_CMD_MAX_LEN];
2091ec886bf5SAlexey Budankov 	int ctlfd_pos = evlist->ctl_fd.pos;
2092ec886bf5SAlexey Budankov 	struct pollfd *entries = evlist->core.pollfd.entries;
2093ec886bf5SAlexey Budankov 
2094ec886bf5SAlexey Budankov 	if (!evlist__ctlfd_initialized(evlist) || !entries[ctlfd_pos].revents)
2095ec886bf5SAlexey Budankov 		return 0;
2096ec886bf5SAlexey Budankov 
2097ec886bf5SAlexey Budankov 	if (entries[ctlfd_pos].revents & POLLIN) {
2098ec886bf5SAlexey Budankov 		err = evlist__ctlfd_recv(evlist, cmd, cmd_data,
2099ec886bf5SAlexey Budankov 					 EVLIST_CTL_CMD_MAX_LEN);
2100ec886bf5SAlexey Budankov 		if (err > 0) {
2101ec886bf5SAlexey Budankov 			switch (*cmd) {
2102ec886bf5SAlexey Budankov 			case EVLIST_CTL_CMD_ENABLE:
2103ec886bf5SAlexey Budankov 			case EVLIST_CTL_CMD_DISABLE:
2104991ae4ebSJiri Olsa 				err = evlist__ctlfd_enable(evlist, cmd_data,
2105991ae4ebSJiri Olsa 							   *cmd == EVLIST_CTL_CMD_ENABLE);
2106ec886bf5SAlexey Budankov 				break;
2107142544a9SJiri Olsa 			case EVLIST_CTL_CMD_EVLIST:
2108142544a9SJiri Olsa 				err = evlist__ctlfd_list(evlist, cmd_data);
2109142544a9SJiri Olsa 				break;
2110d20aff15SAdrian Hunter 			case EVLIST_CTL_CMD_SNAPSHOT:
2111f186cd61SJiri Olsa 			case EVLIST_CTL_CMD_STOP:
211247fddcb4SJiri Olsa 			case EVLIST_CTL_CMD_PING:
2113d20aff15SAdrian Hunter 				break;
2114ec886bf5SAlexey Budankov 			case EVLIST_CTL_CMD_ACK:
2115ec886bf5SAlexey Budankov 			case EVLIST_CTL_CMD_UNSUPPORTED:
2116ec886bf5SAlexey Budankov 			default:
2117ec886bf5SAlexey Budankov 				pr_debug("ctlfd: unsupported %d\n", *cmd);
2118ec886bf5SAlexey Budankov 				break;
2119ec886bf5SAlexey Budankov 			}
2120d20aff15SAdrian Hunter 			if (!(*cmd == EVLIST_CTL_CMD_ACK || *cmd == EVLIST_CTL_CMD_UNSUPPORTED ||
2121d20aff15SAdrian Hunter 			      *cmd == EVLIST_CTL_CMD_SNAPSHOT))
2122ec886bf5SAlexey Budankov 				evlist__ctlfd_ack(evlist);
2123ec886bf5SAlexey Budankov 		}
2124ec886bf5SAlexey Budankov 	}
2125ec886bf5SAlexey Budankov 
2126ec886bf5SAlexey Budankov 	if (entries[ctlfd_pos].revents & (POLLHUP | POLLERR))
2127ec886bf5SAlexey Budankov 		evlist__finalize_ctlfd(evlist);
2128ec886bf5SAlexey Budankov 	else
2129ec886bf5SAlexey Budankov 		entries[ctlfd_pos].revents = 0;
2130ec886bf5SAlexey Budankov 
2131ec886bf5SAlexey Budankov 	return err;
2132ec886bf5SAlexey Budankov }
2133b214ba8cSNamhyung Kim 
2134b214ba8cSNamhyung Kim struct evsel *evlist__find_evsel(struct evlist *evlist, int idx)
2135b214ba8cSNamhyung Kim {
2136b214ba8cSNamhyung Kim 	struct evsel *evsel;
2137b214ba8cSNamhyung Kim 
2138b214ba8cSNamhyung Kim 	evlist__for_each_entry(evlist, evsel) {
213938fe0e01SJiri Olsa 		if (evsel->core.idx == idx)
2140b214ba8cSNamhyung Kim 			return evsel;
2141b214ba8cSNamhyung Kim 	}
2142b214ba8cSNamhyung Kim 	return NULL;
2143b214ba8cSNamhyung Kim }
21449865ea8aSArnaldo Carvalho de Melo 
21459865ea8aSArnaldo Carvalho de Melo int evlist__scnprintf_evsels(struct evlist *evlist, size_t size, char *bf)
21469865ea8aSArnaldo Carvalho de Melo {
21479865ea8aSArnaldo Carvalho de Melo 	struct evsel *evsel;
21489865ea8aSArnaldo Carvalho de Melo 	int printed = 0;
21499865ea8aSArnaldo Carvalho de Melo 
21509865ea8aSArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, evsel) {
21519865ea8aSArnaldo Carvalho de Melo 		if (evsel__is_dummy_event(evsel))
21529865ea8aSArnaldo Carvalho de Melo 			continue;
21539865ea8aSArnaldo Carvalho de Melo 		if (size > (strlen(evsel__name(evsel)) + (printed ? 2 : 1))) {
21549865ea8aSArnaldo Carvalho de Melo 			printed += scnprintf(bf + printed, size - printed, "%s%s", printed ? "," : "", evsel__name(evsel));
21559865ea8aSArnaldo Carvalho de Melo 		} else {
21569865ea8aSArnaldo Carvalho de Melo 			printed += scnprintf(bf + printed, size - printed, "%s...", printed ? "," : "");
21579865ea8aSArnaldo Carvalho de Melo 			break;
21589865ea8aSArnaldo Carvalho de Melo 		}
21599865ea8aSArnaldo Carvalho de Melo 	}
21609865ea8aSArnaldo Carvalho de Melo 
21619865ea8aSArnaldo Carvalho de Melo 	return printed;
21629865ea8aSArnaldo Carvalho de Melo }
2163d5a8bd0fSJin Yao 
2164d5a8bd0fSJin Yao void evlist__check_mem_load_aux(struct evlist *evlist)
2165d5a8bd0fSJin Yao {
2166d5a8bd0fSJin Yao 	struct evsel *leader, *evsel, *pos;
2167d5a8bd0fSJin Yao 
2168d5a8bd0fSJin Yao 	/*
2169d5a8bd0fSJin Yao 	 * For some platforms, the 'mem-loads' event is required to use
2170d5a8bd0fSJin Yao 	 * together with 'mem-loads-aux' within a group and 'mem-loads-aux'
2171d5a8bd0fSJin Yao 	 * must be the group leader. Now we disable this group before reporting
2172d5a8bd0fSJin Yao 	 * because 'mem-loads-aux' is just an auxiliary event. It doesn't carry
2173d5a8bd0fSJin Yao 	 * any valid memory load information.
2174d5a8bd0fSJin Yao 	 */
2175d5a8bd0fSJin Yao 	evlist__for_each_entry(evlist, evsel) {
2176fba7c866SJiri Olsa 		leader = evsel__leader(evsel);
2177d5a8bd0fSJin Yao 		if (leader == evsel)
2178d5a8bd0fSJin Yao 			continue;
2179d5a8bd0fSJin Yao 
2180d5a8bd0fSJin Yao 		if (leader->name && strstr(leader->name, "mem-loads-aux")) {
2181d5a8bd0fSJin Yao 			for_each_group_evsel(pos, leader) {
2182fba7c866SJiri Olsa 				evsel__set_leader(pos, pos);
2183d5a8bd0fSJin Yao 				pos->core.nr_members = 0;
2184d5a8bd0fSJin Yao 			}
2185d5a8bd0fSJin Yao 		}
2186d5a8bd0fSJin Yao 	}
2187d5a8bd0fSJin Yao }
2188