xref: /openbmc/linux/tools/perf/util/evlist.c (revision 2e6263ab)
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"
309607ad3aSArnaldo Carvalho de Melo #include <signal.h>
3135b9d88eSArnaldo Carvalho de Melo #include <unistd.h>
32b397f846SArnaldo Carvalho de Melo #include <sched.h>
33f2a39fe8SArnaldo Carvalho de Melo #include <stdlib.h>
34361c99a6SArnaldo Carvalho de Melo 
3550d08e47SArnaldo Carvalho de Melo #include "parse-events.h"
364b6ab94eSJosh Poimboeuf #include <subcmd/parse-options.h>
3750d08e47SArnaldo Carvalho de Melo 
38bafae98eSArnaldo Carvalho de Melo #include <fcntl.h>
3986a5e0c2SArnaldo Carvalho de Melo #include <sys/ioctl.h>
40f8a95309SArnaldo Carvalho de Melo #include <sys/mman.h>
41a7672d1dSArnaldo Carvalho de Melo #include <sys/prctl.h>
42f8a95309SArnaldo Carvalho de Melo 
4370db7533SArnaldo Carvalho de Melo #include <linux/bitops.h>
4470db7533SArnaldo Carvalho de Melo #include <linux/hash.h>
450389cd1fSArnaldo Carvalho de Melo #include <linux/log2.h>
468dd2a131SJiri Olsa #include <linux/err.h>
478520a98dSArnaldo Carvalho de Melo #include <linux/string.h>
487f7c536fSArnaldo Carvalho de Melo #include <linux/zalloc.h>
494562a739SJiri Olsa #include <perf/evlist.h>
5088761fa1SJiri Olsa #include <perf/evsel.h>
519c3516d1SJiri Olsa #include <perf/cpumap.h>
527728fa0cSJiri Olsa #include <perf/mmap.h>
5370db7533SArnaldo Carvalho de Melo 
54e14e5497SArnaldo Carvalho de Melo #include <internal/xyarray.h>
55e14e5497SArnaldo Carvalho de Melo 
56748fe088SArnaldo Carvalho de Melo #ifdef LACKS_SIGQUEUE_PROTOTYPE
57748fe088SArnaldo Carvalho de Melo int sigqueue(pid_t pid, int sig, const union sigval value);
58748fe088SArnaldo Carvalho de Melo #endif
59748fe088SArnaldo Carvalho de Melo 
609dfcb759SJiri Olsa #define FD(e, x, y) (*(int *)xyarray__entry(e->core.fd, x, y))
618cd36f3eSJiri Olsa #define SID(e, x, y) xyarray__entry(e->core.sample_id, x, y)
62f8a95309SArnaldo Carvalho de Melo 
6352c86bcaSJiri Olsa void evlist__init(struct evlist *evlist, struct perf_cpu_map *cpus,
649749b90eSJiri Olsa 		  struct perf_thread_map *threads)
65361c99a6SArnaldo Carvalho de Melo {
664562a739SJiri Olsa 	perf_evlist__init(&evlist->core);
67453fa030SJiri Olsa 	perf_evlist__set_maps(&evlist->core, cpus, threads);
6835b9d88eSArnaldo Carvalho de Melo 	evlist->workload.pid = -1;
6954cc54deSWang Nan 	evlist->bkw_mmap_state = BKW_MMAP_NOTREADY;
708ab705b5SAlexey Budankov 	evlist->ctl_fd.fd = -1;
718ab705b5SAlexey Budankov 	evlist->ctl_fd.ack = -1;
728ab705b5SAlexey Budankov 	evlist->ctl_fd.pos = -1;
73361c99a6SArnaldo Carvalho de Melo }
74361c99a6SArnaldo Carvalho de Melo 
750f98b11cSJiri Olsa struct evlist *evlist__new(void)
76ef1d1af2SArnaldo Carvalho de Melo {
7763503dbaSJiri Olsa 	struct evlist *evlist = zalloc(sizeof(*evlist));
78ef1d1af2SArnaldo Carvalho de Melo 
79ef1d1af2SArnaldo Carvalho de Melo 	if (evlist != NULL)
8052c86bcaSJiri Olsa 		evlist__init(evlist, NULL, NULL);
81ef1d1af2SArnaldo Carvalho de Melo 
82361c99a6SArnaldo Carvalho de Melo 	return evlist;
83361c99a6SArnaldo Carvalho de Melo }
84361c99a6SArnaldo Carvalho de Melo 
85606e2c29SArnaldo Carvalho de Melo struct evlist *evlist__new_default(void)
86b22d54b0SJiri Olsa {
870f98b11cSJiri Olsa 	struct evlist *evlist = evlist__new();
88b22d54b0SJiri Olsa 
89e251abeeSArnaldo Carvalho de Melo 	if (evlist && evlist__add_default(evlist)) {
90c12995a5SJiri Olsa 		evlist__delete(evlist);
91b22d54b0SJiri Olsa 		evlist = NULL;
92b22d54b0SJiri Olsa 	}
93b22d54b0SJiri Olsa 
94b22d54b0SJiri Olsa 	return evlist;
95b22d54b0SJiri Olsa }
96b22d54b0SJiri Olsa 
97606e2c29SArnaldo Carvalho de Melo struct evlist *evlist__new_dummy(void)
985bae0250SArnaldo Carvalho de Melo {
990f98b11cSJiri Olsa 	struct evlist *evlist = evlist__new();
1005bae0250SArnaldo Carvalho de Melo 
101e251abeeSArnaldo Carvalho de Melo 	if (evlist && evlist__add_dummy(evlist)) {
102c12995a5SJiri Olsa 		evlist__delete(evlist);
1035bae0250SArnaldo Carvalho de Melo 		evlist = NULL;
1045bae0250SArnaldo Carvalho de Melo 	}
1055bae0250SArnaldo Carvalho de Melo 
1065bae0250SArnaldo Carvalho de Melo 	return evlist;
1075bae0250SArnaldo Carvalho de Melo }
1085bae0250SArnaldo Carvalho de Melo 
10975562573SAdrian Hunter /**
1100a7e7ec9SArnaldo Carvalho de Melo  * evlist__set_id_pos - set the positions of event ids.
11175562573SAdrian Hunter  * @evlist: selected event list
11275562573SAdrian Hunter  *
11375562573SAdrian Hunter  * Events with compatible sample types all have the same id_pos
11475562573SAdrian Hunter  * and is_pos.  For convenience, put a copy on evlist.
11575562573SAdrian Hunter  */
1160a7e7ec9SArnaldo Carvalho de Melo void evlist__set_id_pos(struct evlist *evlist)
11775562573SAdrian Hunter {
118515dbe48SJiri Olsa 	struct evsel *first = evlist__first(evlist);
11975562573SAdrian Hunter 
12075562573SAdrian Hunter 	evlist->id_pos = first->id_pos;
12175562573SAdrian Hunter 	evlist->is_pos = first->is_pos;
12275562573SAdrian Hunter }
12375562573SAdrian Hunter 
1240a7e7ec9SArnaldo Carvalho de Melo static void evlist__update_id_pos(struct evlist *evlist)
125733cd2feSAdrian Hunter {
12632dcd021SJiri Olsa 	struct evsel *evsel;
127733cd2feSAdrian Hunter 
128e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, evsel)
1294b5e87b7SArnaldo Carvalho de Melo 		evsel__calc_id_pos(evsel);
130733cd2feSAdrian Hunter 
1310a7e7ec9SArnaldo Carvalho de Melo 	evlist__set_id_pos(evlist);
132733cd2feSAdrian Hunter }
133733cd2feSAdrian Hunter 
134e6b1878dSJiri Olsa static void evlist__purge(struct evlist *evlist)
135361c99a6SArnaldo Carvalho de Melo {
13632dcd021SJiri Olsa 	struct evsel *pos, *n;
137361c99a6SArnaldo Carvalho de Melo 
138e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry_safe(evlist, n, pos) {
139b27c4eceSJiri Olsa 		list_del_init(&pos->core.node);
140d49e4695SArnaldo Carvalho de Melo 		pos->evlist = NULL;
1415eb2dd2aSJiri Olsa 		evsel__delete(pos);
142361c99a6SArnaldo Carvalho de Melo 	}
143361c99a6SArnaldo Carvalho de Melo 
1446484d2f9SJiri Olsa 	evlist->core.nr_entries = 0;
145361c99a6SArnaldo Carvalho de Melo }
146361c99a6SArnaldo Carvalho de Melo 
147470579b0SJiri Olsa void evlist__exit(struct evlist *evlist)
148ef1d1af2SArnaldo Carvalho de Melo {
14904662523SArnaldo Carvalho de Melo 	zfree(&evlist->mmap);
1500b72d69aSWang Nan 	zfree(&evlist->overwrite_mmap);
15193dd6e28SJiri Olsa 	perf_evlist__exit(&evlist->core);
152ef1d1af2SArnaldo Carvalho de Melo }
153ef1d1af2SArnaldo Carvalho de Melo 
154c12995a5SJiri Olsa void evlist__delete(struct evlist *evlist)
155361c99a6SArnaldo Carvalho de Melo {
1560b04b3dcSArnaldo Carvalho de Melo 	if (evlist == NULL)
1570b04b3dcSArnaldo Carvalho de Melo 		return;
1580b04b3dcSArnaldo Carvalho de Melo 
159db6b7b13SJiri Olsa 	evlist__munmap(evlist);
160750b4edeSJiri Olsa 	evlist__close(evlist);
161e6b1878dSJiri Olsa 	evlist__purge(evlist);
162470579b0SJiri Olsa 	evlist__exit(evlist);
163361c99a6SArnaldo Carvalho de Melo 	free(evlist);
164361c99a6SArnaldo Carvalho de Melo }
165361c99a6SArnaldo Carvalho de Melo 
166a1cf3a75SJiri Olsa void evlist__add(struct evlist *evlist, struct evsel *entry)
167361c99a6SArnaldo Carvalho de Melo {
1686484d2f9SJiri Olsa 	perf_evlist__add(&evlist->core, &entry->core);
16938fe0e01SJiri Olsa 	entry->evlist = evlist;
17038fe0e01SJiri Olsa 	entry->tracking = !entry->core.idx;
1716484d2f9SJiri Olsa 
1726484d2f9SJiri Olsa 	if (evlist->core.nr_entries == 1)
1730a7e7ec9SArnaldo Carvalho de Melo 		evlist__set_id_pos(evlist);
174361c99a6SArnaldo Carvalho de Melo }
175361c99a6SArnaldo Carvalho de Melo 
17616251027SJiri Olsa void evlist__remove(struct evlist *evlist, struct evsel *evsel)
1774768230aSAdrian Hunter {
1784768230aSAdrian Hunter 	evsel->evlist = NULL;
17952e22fb8SJiri Olsa 	perf_evlist__remove(&evlist->core, &evsel->core);
1804768230aSAdrian Hunter }
1814768230aSAdrian Hunter 
182e414fd1aSArnaldo Carvalho de Melo void evlist__splice_list_tail(struct evlist *evlist, struct list_head *list)
18350d08e47SArnaldo Carvalho de Melo {
1846d2783feSJohn Garry 	while (!list_empty(list)) {
1856d2783feSJohn Garry 		struct evsel *evsel, *temp, *leader = NULL;
18675562573SAdrian Hunter 
187e5cadb93SArnaldo Carvalho de Melo 		__evlist__for_each_entry_safe(list, temp, evsel) {
188b27c4eceSJiri Olsa 			list_del_init(&evsel->core.node);
189a1cf3a75SJiri Olsa 			evlist__add(evlist, evsel);
1906d2783feSJohn Garry 			leader = evsel;
1916d2783feSJohn Garry 			break;
1926d2783feSJohn Garry 		}
1936d2783feSJohn Garry 
1946d2783feSJohn Garry 		__evlist__for_each_entry_safe(list, temp, evsel) {
195fba7c866SJiri Olsa 			if (evsel__has_leader(evsel, leader)) {
1966d2783feSJohn Garry 				list_del_init(&evsel->core.node);
1976d2783feSJohn Garry 				evlist__add(evlist, evsel);
1986d2783feSJohn Garry 			}
1996d2783feSJohn Garry 		}
200f114d6efSAdrian Hunter 	}
20150d08e47SArnaldo Carvalho de Melo }
20250d08e47SArnaldo Carvalho de Melo 
203c0e53476SArnaldo Carvalho de Melo int __evlist__set_tracepoints_handlers(struct evlist *evlist,
204c0e53476SArnaldo Carvalho de Melo 				       const struct evsel_str_handler *assocs, size_t nr_assocs)
205c0e53476SArnaldo Carvalho de Melo {
206c0e53476SArnaldo Carvalho de Melo 	size_t i;
207c0e53476SArnaldo Carvalho de Melo 	int err;
208c0e53476SArnaldo Carvalho de Melo 
209c0e53476SArnaldo Carvalho de Melo 	for (i = 0; i < nr_assocs; i++) {
210c0e53476SArnaldo Carvalho de Melo 		// Adding a handler for an event not in this evlist, just ignore it.
211b02736f7SArnaldo Carvalho de Melo 		struct evsel *evsel = evlist__find_tracepoint_by_name(evlist, assocs[i].name);
212c0e53476SArnaldo Carvalho de Melo 		if (evsel == NULL)
213c0e53476SArnaldo Carvalho de Melo 			continue;
214c0e53476SArnaldo Carvalho de Melo 
215c0e53476SArnaldo Carvalho de Melo 		err = -EEXIST;
216c0e53476SArnaldo Carvalho de Melo 		if (evsel->handler != NULL)
217c0e53476SArnaldo Carvalho de Melo 			goto out;
218c0e53476SArnaldo Carvalho de Melo 		evsel->handler = assocs[i].handler;
219c0e53476SArnaldo Carvalho de Melo 	}
220c0e53476SArnaldo Carvalho de Melo 
221c0e53476SArnaldo Carvalho de Melo 	err = 0;
222c0e53476SArnaldo Carvalho de Melo out:
223c0e53476SArnaldo Carvalho de Melo 	return err;
224c0e53476SArnaldo Carvalho de Melo }
225c0e53476SArnaldo Carvalho de Melo 
226a622eafaSArnaldo Carvalho de Melo void evlist__set_leader(struct evlist *evlist)
2276a4bb04cSJiri Olsa {
228*2e6263abSJiri Olsa 	perf_evlist__set_leader(&evlist->core);
22997f63e4aSNamhyung Kim }
2306a4bb04cSJiri Olsa 
231e251abeeSArnaldo Carvalho de Melo int __evlist__add_default(struct evlist *evlist, bool precise)
232361c99a6SArnaldo Carvalho de Melo {
233b53a0755SJin Yao 	struct evsel *evsel;
234361c99a6SArnaldo Carvalho de Melo 
235b53a0755SJin Yao 	evsel = evsel__new_cycles(precise, PERF_TYPE_HARDWARE,
236b53a0755SJin Yao 				  PERF_COUNT_HW_CPU_CYCLES);
237361c99a6SArnaldo Carvalho de Melo 	if (evsel == NULL)
2387c48dcfdSArnaldo Carvalho de Melo 		return -ENOMEM;
239361c99a6SArnaldo Carvalho de Melo 
240a1cf3a75SJiri Olsa 	evlist__add(evlist, evsel);
241361c99a6SArnaldo Carvalho de Melo 	return 0;
242361c99a6SArnaldo Carvalho de Melo }
2435c581041SArnaldo Carvalho de Melo 
244e251abeeSArnaldo Carvalho de Melo int evlist__add_dummy(struct evlist *evlist)
2455bae0250SArnaldo Carvalho de Melo {
2465bae0250SArnaldo Carvalho de Melo 	struct perf_event_attr attr = {
2475bae0250SArnaldo Carvalho de Melo 		.type	= PERF_TYPE_SOFTWARE,
2485bae0250SArnaldo Carvalho de Melo 		.config = PERF_COUNT_SW_DUMMY,
2495bae0250SArnaldo Carvalho de Melo 		.size	= sizeof(attr), /* to capture ABI version */
2505bae0250SArnaldo Carvalho de Melo 	};
2518f6725a2SArnaldo Carvalho de Melo 	struct evsel *evsel = evsel__new_idx(&attr, evlist->core.nr_entries);
2525bae0250SArnaldo Carvalho de Melo 
2535bae0250SArnaldo Carvalho de Melo 	if (evsel == NULL)
2545bae0250SArnaldo Carvalho de Melo 		return -ENOMEM;
2555bae0250SArnaldo Carvalho de Melo 
256a1cf3a75SJiri Olsa 	evlist__add(evlist, evsel);
2575bae0250SArnaldo Carvalho de Melo 	return 0;
2585bae0250SArnaldo Carvalho de Melo }
2595bae0250SArnaldo Carvalho de Melo 
260e251abeeSArnaldo Carvalho de Melo static int evlist__add_attrs(struct evlist *evlist, struct perf_event_attr *attrs, size_t nr_attrs)
26150d08e47SArnaldo Carvalho de Melo {
26232dcd021SJiri Olsa 	struct evsel *evsel, *n;
26350d08e47SArnaldo Carvalho de Melo 	LIST_HEAD(head);
26450d08e47SArnaldo Carvalho de Melo 	size_t i;
26550d08e47SArnaldo Carvalho de Melo 
26650d08e47SArnaldo Carvalho de Melo 	for (i = 0; i < nr_attrs; i++) {
2678f6725a2SArnaldo Carvalho de Melo 		evsel = evsel__new_idx(attrs + i, evlist->core.nr_entries + i);
26850d08e47SArnaldo Carvalho de Melo 		if (evsel == NULL)
26950d08e47SArnaldo Carvalho de Melo 			goto out_delete_partial_list;
270b27c4eceSJiri Olsa 		list_add_tail(&evsel->core.node, &head);
27150d08e47SArnaldo Carvalho de Melo 	}
27250d08e47SArnaldo Carvalho de Melo 
273e414fd1aSArnaldo Carvalho de Melo 	evlist__splice_list_tail(evlist, &head);
27450d08e47SArnaldo Carvalho de Melo 
27550d08e47SArnaldo Carvalho de Melo 	return 0;
27650d08e47SArnaldo Carvalho de Melo 
27750d08e47SArnaldo Carvalho de Melo out_delete_partial_list:
278e5cadb93SArnaldo Carvalho de Melo 	__evlist__for_each_entry_safe(&head, n, evsel)
2795eb2dd2aSJiri Olsa 		evsel__delete(evsel);
28050d08e47SArnaldo Carvalho de Melo 	return -1;
28150d08e47SArnaldo Carvalho de Melo }
28250d08e47SArnaldo Carvalho de Melo 
283e251abeeSArnaldo Carvalho de Melo int __evlist__add_default_attrs(struct evlist *evlist, struct perf_event_attr *attrs, size_t nr_attrs)
28479695e1bSArnaldo Carvalho de Melo {
28579695e1bSArnaldo Carvalho de Melo 	size_t i;
28679695e1bSArnaldo Carvalho de Melo 
28779695e1bSArnaldo Carvalho de Melo 	for (i = 0; i < nr_attrs; i++)
28879695e1bSArnaldo Carvalho de Melo 		event_attr_init(attrs + i);
28979695e1bSArnaldo Carvalho de Melo 
290a1cf3a75SJiri Olsa 	return evlist__add_attrs(evlist, attrs, nr_attrs);
29179695e1bSArnaldo Carvalho de Melo }
29279695e1bSArnaldo Carvalho de Melo 
29342641d6fSKan Liang __weak int arch_evlist__add_default_attrs(struct evlist *evlist __maybe_unused)
29442641d6fSKan Liang {
29542641d6fSKan Liang 	return 0;
29642641d6fSKan Liang }
29742641d6fSKan Liang 
298b02736f7SArnaldo Carvalho de Melo struct evsel *evlist__find_tracepoint_by_id(struct evlist *evlist, int id)
299ee29be62SArnaldo Carvalho de Melo {
30032dcd021SJiri Olsa 	struct evsel *evsel;
301ee29be62SArnaldo Carvalho de Melo 
302e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, evsel) {
3031fc632ceSJiri Olsa 		if (evsel->core.attr.type   == PERF_TYPE_TRACEPOINT &&
3041fc632ceSJiri Olsa 		    (int)evsel->core.attr.config == id)
305ee29be62SArnaldo Carvalho de Melo 			return evsel;
306ee29be62SArnaldo Carvalho de Melo 	}
307ee29be62SArnaldo Carvalho de Melo 
308ee29be62SArnaldo Carvalho de Melo 	return NULL;
309ee29be62SArnaldo Carvalho de Melo }
310ee29be62SArnaldo Carvalho de Melo 
311b02736f7SArnaldo Carvalho de Melo struct evsel *evlist__find_tracepoint_by_name(struct evlist *evlist, const char *name)
312a2f2804aSDavid Ahern {
31332dcd021SJiri Olsa 	struct evsel *evsel;
314a2f2804aSDavid Ahern 
315e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, evsel) {
3161fc632ceSJiri Olsa 		if ((evsel->core.attr.type == PERF_TYPE_TRACEPOINT) &&
317a2f2804aSDavid Ahern 		    (strcmp(evsel->name, name) == 0))
318a2f2804aSDavid Ahern 			return evsel;
319a2f2804aSDavid Ahern 	}
320a2f2804aSDavid Ahern 
321a2f2804aSDavid Ahern 	return NULL;
322a2f2804aSDavid Ahern }
323a2f2804aSDavid Ahern 
324e251abeeSArnaldo Carvalho de Melo int evlist__add_newtp(struct evlist *evlist, const char *sys, const char *name, void *handler)
32539876e7dSArnaldo Carvalho de Melo {
3268f6725a2SArnaldo Carvalho de Melo 	struct evsel *evsel = evsel__newtp(sys, name);
32739876e7dSArnaldo Carvalho de Melo 
3288dd2a131SJiri Olsa 	if (IS_ERR(evsel))
32939876e7dSArnaldo Carvalho de Melo 		return -1;
33039876e7dSArnaldo Carvalho de Melo 
331744a9719SArnaldo Carvalho de Melo 	evsel->handler = handler;
332a1cf3a75SJiri Olsa 	evlist__add(evlist, evsel);
33339876e7dSArnaldo Carvalho de Melo 	return 0;
33439876e7dSArnaldo Carvalho de Melo }
33539876e7dSArnaldo Carvalho de Melo 
336f63c2f5aSArnaldo Carvalho de Melo static int evlist__nr_threads(struct evlist *evlist, struct evsel *evsel)
337bf8e8f4bSAdrian Hunter {
338648b5af3SJiri Olsa 	if (evsel->core.system_wide)
339bf8e8f4bSAdrian Hunter 		return 1;
340bf8e8f4bSAdrian Hunter 	else
341a2f354e3SJiri Olsa 		return perf_thread_map__nr(evlist->core.threads);
342bf8e8f4bSAdrian Hunter }
343bf8e8f4bSAdrian Hunter 
344a8cbe40fSAndi Kleen void evlist__cpu_iter_start(struct evlist *evlist)
345a8cbe40fSAndi Kleen {
346a8cbe40fSAndi Kleen 	struct evsel *pos;
347a8cbe40fSAndi Kleen 
348a8cbe40fSAndi Kleen 	/*
349a8cbe40fSAndi Kleen 	 * Reset the per evsel cpu_iter. This is needed because
350a8cbe40fSAndi Kleen 	 * each evsel's cpumap may have a different index space,
351a8cbe40fSAndi Kleen 	 * and some operations need the index to modify
352a8cbe40fSAndi Kleen 	 * the FD xyarray (e.g. open, close)
353a8cbe40fSAndi Kleen 	 */
354a8cbe40fSAndi Kleen 	evlist__for_each_entry(evlist, pos)
355a8cbe40fSAndi Kleen 		pos->cpu_iter = 0;
356a8cbe40fSAndi Kleen }
357a8cbe40fSAndi Kleen 
358a8cbe40fSAndi Kleen bool evsel__cpu_iter_skip_no_inc(struct evsel *ev, int cpu)
359a8cbe40fSAndi Kleen {
360a8cbe40fSAndi Kleen 	if (ev->cpu_iter >= ev->core.cpus->nr)
361a8cbe40fSAndi Kleen 		return true;
362a8cbe40fSAndi Kleen 	if (cpu >= 0 && ev->core.cpus->map[ev->cpu_iter] != cpu)
363a8cbe40fSAndi Kleen 		return true;
364a8cbe40fSAndi Kleen 	return false;
365a8cbe40fSAndi Kleen }
366a8cbe40fSAndi Kleen 
367a8cbe40fSAndi Kleen bool evsel__cpu_iter_skip(struct evsel *ev, int cpu)
368a8cbe40fSAndi Kleen {
369a8cbe40fSAndi Kleen 	if (!evsel__cpu_iter_skip_no_inc(ev, cpu)) {
370a8cbe40fSAndi Kleen 		ev->cpu_iter++;
371a8cbe40fSAndi Kleen 		return false;
372a8cbe40fSAndi Kleen 	}
373a8cbe40fSAndi Kleen 	return true;
374a8cbe40fSAndi Kleen }
375a8cbe40fSAndi Kleen 
3767cfcd1e0SJiri Olsa static int evsel__strcmp(struct evsel *pos, char *evsel_name)
3777cfcd1e0SJiri Olsa {
3787cfcd1e0SJiri Olsa 	if (!evsel_name)
3797cfcd1e0SJiri Olsa 		return 0;
3807cfcd1e0SJiri Olsa 	if (evsel__is_dummy_event(pos))
3817cfcd1e0SJiri Olsa 		return 1;
3827cfcd1e0SJiri Olsa 	return strcmp(pos->name, evsel_name);
3837cfcd1e0SJiri Olsa }
3847cfcd1e0SJiri Olsa 
3857cfcd1e0SJiri Olsa static int evlist__is_enabled(struct evlist *evlist)
3867cfcd1e0SJiri Olsa {
3877cfcd1e0SJiri Olsa 	struct evsel *pos;
3887cfcd1e0SJiri Olsa 
3897cfcd1e0SJiri Olsa 	evlist__for_each_entry(evlist, pos) {
3907cfcd1e0SJiri Olsa 		if (!evsel__is_group_leader(pos) || !pos->core.fd)
3917cfcd1e0SJiri Olsa 			continue;
3927cfcd1e0SJiri Olsa 		/* If at least one event is enabled, evlist is enabled. */
3937cfcd1e0SJiri Olsa 		if (!pos->disabled)
3947cfcd1e0SJiri Olsa 			return true;
3957cfcd1e0SJiri Olsa 	}
3967cfcd1e0SJiri Olsa 	return false;
3977cfcd1e0SJiri Olsa }
3987cfcd1e0SJiri Olsa 
3997cfcd1e0SJiri Olsa static void __evlist__disable(struct evlist *evlist, char *evsel_name)
4004152ab37SArnaldo Carvalho de Melo {
40132dcd021SJiri Olsa 	struct evsel *pos;
402704e2f5bSAndi Kleen 	struct affinity affinity;
40387cf8360SAdrian Hunter 	int cpu, i, imm = 0;
40487cf8360SAdrian Hunter 	bool has_imm = false;
405704e2f5bSAndi Kleen 
406704e2f5bSAndi Kleen 	if (affinity__setup(&affinity) < 0)
407704e2f5bSAndi Kleen 		return;
408704e2f5bSAndi Kleen 
40987cf8360SAdrian Hunter 	/* Disable 'immediate' events last */
41087cf8360SAdrian Hunter 	for (imm = 0; imm <= 1; imm++) {
411704e2f5bSAndi Kleen 		evlist__for_each_cpu(evlist, i, cpu) {
412704e2f5bSAndi Kleen 			affinity__set(&affinity, cpu);
4134152ab37SArnaldo Carvalho de Melo 
414e5cadb93SArnaldo Carvalho de Melo 			evlist__for_each_entry(evlist, pos) {
4157cfcd1e0SJiri Olsa 				if (evsel__strcmp(pos, evsel_name))
4167cfcd1e0SJiri Olsa 					continue;
417704e2f5bSAndi Kleen 				if (evsel__cpu_iter_skip(pos, cpu))
418704e2f5bSAndi Kleen 					continue;
419c754c382SArnaldo Carvalho de Melo 				if (pos->disabled || !evsel__is_group_leader(pos) || !pos->core.fd)
4203fe4430dSJiri Olsa 					continue;
42187cf8360SAdrian Hunter 				if (pos->immediate)
42287cf8360SAdrian Hunter 					has_imm = true;
42387cf8360SAdrian Hunter 				if (pos->immediate != imm)
42487cf8360SAdrian Hunter 					continue;
425704e2f5bSAndi Kleen 				evsel__disable_cpu(pos, pos->cpu_iter - 1);
426704e2f5bSAndi Kleen 			}
427704e2f5bSAndi Kleen 		}
42887cf8360SAdrian Hunter 		if (!has_imm)
42987cf8360SAdrian Hunter 			break;
43087cf8360SAdrian Hunter 	}
43187cf8360SAdrian Hunter 
432704e2f5bSAndi Kleen 	affinity__cleanup(&affinity);
433704e2f5bSAndi Kleen 	evlist__for_each_entry(evlist, pos) {
4347cfcd1e0SJiri Olsa 		if (evsel__strcmp(pos, evsel_name))
4357cfcd1e0SJiri Olsa 			continue;
436c754c382SArnaldo Carvalho de Melo 		if (!evsel__is_group_leader(pos) || !pos->core.fd)
437704e2f5bSAndi Kleen 			continue;
438704e2f5bSAndi Kleen 		pos->disabled = true;
4394152ab37SArnaldo Carvalho de Melo 	}
4402b56bcfbSArnaldo Carvalho de Melo 
4417cfcd1e0SJiri Olsa 	/*
4427cfcd1e0SJiri Olsa 	 * If we disabled only single event, we need to check
4437cfcd1e0SJiri Olsa 	 * the enabled state of the evlist manually.
4447cfcd1e0SJiri Olsa 	 */
4457cfcd1e0SJiri Olsa 	if (evsel_name)
4467cfcd1e0SJiri Olsa 		evlist->enabled = evlist__is_enabled(evlist);
4477cfcd1e0SJiri Olsa 	else
4482b56bcfbSArnaldo Carvalho de Melo 		evlist->enabled = false;
4494152ab37SArnaldo Carvalho de Melo }
4504152ab37SArnaldo Carvalho de Melo 
4517cfcd1e0SJiri Olsa void evlist__disable(struct evlist *evlist)
4527cfcd1e0SJiri Olsa {
4537cfcd1e0SJiri Olsa 	__evlist__disable(evlist, NULL);
4547cfcd1e0SJiri Olsa }
4557cfcd1e0SJiri Olsa 
4567cfcd1e0SJiri Olsa void evlist__disable_evsel(struct evlist *evlist, char *evsel_name)
4577cfcd1e0SJiri Olsa {
4587cfcd1e0SJiri Olsa 	__evlist__disable(evlist, evsel_name);
4597cfcd1e0SJiri Olsa }
4607cfcd1e0SJiri Olsa 
4617cfcd1e0SJiri Olsa static void __evlist__enable(struct evlist *evlist, char *evsel_name)
462764e16a3SDavid Ahern {
46332dcd021SJiri Olsa 	struct evsel *pos;
464704e2f5bSAndi Kleen 	struct affinity affinity;
465704e2f5bSAndi Kleen 	int cpu, i;
466764e16a3SDavid Ahern 
467704e2f5bSAndi Kleen 	if (affinity__setup(&affinity) < 0)
468704e2f5bSAndi Kleen 		return;
469704e2f5bSAndi Kleen 
470704e2f5bSAndi Kleen 	evlist__for_each_cpu(evlist, i, cpu) {
471704e2f5bSAndi Kleen 		affinity__set(&affinity, cpu);
472704e2f5bSAndi Kleen 
473704e2f5bSAndi Kleen 		evlist__for_each_entry(evlist, pos) {
4747cfcd1e0SJiri Olsa 			if (evsel__strcmp(pos, evsel_name))
4757cfcd1e0SJiri Olsa 				continue;
476704e2f5bSAndi Kleen 			if (evsel__cpu_iter_skip(pos, cpu))
477704e2f5bSAndi Kleen 				continue;
478c754c382SArnaldo Carvalho de Melo 			if (!evsel__is_group_leader(pos) || !pos->core.fd)
479704e2f5bSAndi Kleen 				continue;
480704e2f5bSAndi Kleen 			evsel__enable_cpu(pos, pos->cpu_iter - 1);
481704e2f5bSAndi Kleen 		}
482704e2f5bSAndi Kleen 	}
483704e2f5bSAndi Kleen 	affinity__cleanup(&affinity);
484e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, pos) {
4857cfcd1e0SJiri Olsa 		if (evsel__strcmp(pos, evsel_name))
4867cfcd1e0SJiri Olsa 			continue;
487c754c382SArnaldo Carvalho de Melo 		if (!evsel__is_group_leader(pos) || !pos->core.fd)
4883fe4430dSJiri Olsa 			continue;
489704e2f5bSAndi Kleen 		pos->disabled = false;
490764e16a3SDavid Ahern 	}
4912b56bcfbSArnaldo Carvalho de Melo 
4927cfcd1e0SJiri Olsa 	/*
4937cfcd1e0SJiri Olsa 	 * Even single event sets the 'enabled' for evlist,
4947cfcd1e0SJiri Olsa 	 * so the toggle can work properly and toggle to
4957cfcd1e0SJiri Olsa 	 * 'disabled' state.
4967cfcd1e0SJiri Olsa 	 */
4972b56bcfbSArnaldo Carvalho de Melo 	evlist->enabled = true;
4982b56bcfbSArnaldo Carvalho de Melo }
4992b56bcfbSArnaldo Carvalho de Melo 
5007cfcd1e0SJiri Olsa void evlist__enable(struct evlist *evlist)
5017cfcd1e0SJiri Olsa {
5027cfcd1e0SJiri Olsa 	__evlist__enable(evlist, NULL);
5037cfcd1e0SJiri Olsa }
5047cfcd1e0SJiri Olsa 
5057cfcd1e0SJiri Olsa void evlist__enable_evsel(struct evlist *evlist, char *evsel_name)
5067cfcd1e0SJiri Olsa {
5077cfcd1e0SJiri Olsa 	__evlist__enable(evlist, evsel_name);
5087cfcd1e0SJiri Olsa }
5097cfcd1e0SJiri Olsa 
510ade9d208SArnaldo Carvalho de Melo void evlist__toggle_enable(struct evlist *evlist)
5112b56bcfbSArnaldo Carvalho de Melo {
512e74676deSJiri Olsa 	(evlist->enabled ? evlist__disable : evlist__enable)(evlist);
513764e16a3SDavid Ahern }
514764e16a3SDavid Ahern 
51537b01abeSArnaldo Carvalho de Melo static int evlist__enable_event_cpu(struct evlist *evlist, struct evsel *evsel, int cpu)
5161c65056cSAdrian Hunter {
51718ef15c6SArnaldo Carvalho de Melo 	int thread;
518f63c2f5aSArnaldo Carvalho de Melo 	int nr_threads = evlist__nr_threads(evlist, evsel);
5191c65056cSAdrian Hunter 
5209dfcb759SJiri Olsa 	if (!evsel->core.fd)
5211c65056cSAdrian Hunter 		return -EINVAL;
5221c65056cSAdrian Hunter 
5231c65056cSAdrian Hunter 	for (thread = 0; thread < nr_threads; thread++) {
52418ef15c6SArnaldo Carvalho de Melo 		int err = ioctl(FD(evsel, cpu, thread), PERF_EVENT_IOC_ENABLE, 0);
5251c65056cSAdrian Hunter 		if (err)
5261c65056cSAdrian Hunter 			return err;
5271c65056cSAdrian Hunter 	}
5281c65056cSAdrian Hunter 	return 0;
5291c65056cSAdrian Hunter }
5301c65056cSAdrian Hunter 
53137b01abeSArnaldo Carvalho de Melo static int evlist__enable_event_thread(struct evlist *evlist, struct evsel *evsel, int thread)
5321c65056cSAdrian Hunter {
53318ef15c6SArnaldo Carvalho de Melo 	int cpu;
5346549cd8fSJiri Olsa 	int nr_cpus = perf_cpu_map__nr(evlist->core.cpus);
5351c65056cSAdrian Hunter 
5369dfcb759SJiri Olsa 	if (!evsel->core.fd)
5371c65056cSAdrian Hunter 		return -EINVAL;
5381c65056cSAdrian Hunter 
5391c65056cSAdrian Hunter 	for (cpu = 0; cpu < nr_cpus; cpu++) {
54018ef15c6SArnaldo Carvalho de Melo 		int err = ioctl(FD(evsel, cpu, thread), PERF_EVENT_IOC_ENABLE, 0);
5411c65056cSAdrian Hunter 		if (err)
5421c65056cSAdrian Hunter 			return err;
5431c65056cSAdrian Hunter 	}
5441c65056cSAdrian Hunter 	return 0;
5451c65056cSAdrian Hunter }
5461c65056cSAdrian Hunter 
54737b01abeSArnaldo Carvalho de Melo int evlist__enable_event_idx(struct evlist *evlist, struct evsel *evsel, int idx)
5481c65056cSAdrian Hunter {
549315c0a1fSJiri Olsa 	bool per_cpu_mmaps = !perf_cpu_map__empty(evlist->core.cpus);
5501c65056cSAdrian Hunter 
5511c65056cSAdrian Hunter 	if (per_cpu_mmaps)
55237b01abeSArnaldo Carvalho de Melo 		return evlist__enable_event_cpu(evlist, evsel, idx);
55337b01abeSArnaldo Carvalho de Melo 
55437b01abeSArnaldo Carvalho de Melo 	return evlist__enable_event_thread(evlist, evsel, idx);
5551c65056cSAdrian Hunter }
5561c65056cSAdrian Hunter 
557f4009e7bSJiri Olsa int evlist__add_pollfd(struct evlist *evlist, int fd)
558e4b356b5SArnaldo Carvalho de Melo {
559ab4c1f9fSAlexey Budankov 	return perf_evlist__add_pollfd(&evlist->core, fd, NULL, POLLIN, fdarray_flag__default);
560e4b356b5SArnaldo Carvalho de Melo }
5611b85337dSArnaldo Carvalho de Melo 
562f4009e7bSJiri Olsa int evlist__filter_pollfd(struct evlist *evlist, short revents_and_mask)
5631ddec7f0SArnaldo Carvalho de Melo {
56484227cb1SJiri Olsa 	return perf_evlist__filter_pollfd(&evlist->core, revents_and_mask);
5651ddec7f0SArnaldo Carvalho de Melo }
5661ddec7f0SArnaldo Carvalho de Melo 
567e16c2ce7SYang Jihong #ifdef HAVE_EVENTFD_SUPPORT
568e16c2ce7SYang Jihong int evlist__add_wakeup_eventfd(struct evlist *evlist, int fd)
569e16c2ce7SYang Jihong {
570e16c2ce7SYang Jihong 	return perf_evlist__add_pollfd(&evlist->core, fd, NULL, POLLIN,
571e16c2ce7SYang Jihong 				       fdarray_flag__nonfilterable);
572e16c2ce7SYang Jihong }
573e16c2ce7SYang Jihong #endif
574e16c2ce7SYang Jihong 
57580ab2987SJiri Olsa int evlist__poll(struct evlist *evlist, int timeout)
576f66a889dSArnaldo Carvalho de Melo {
57780ab2987SJiri Olsa 	return perf_evlist__poll(&evlist->core, timeout);
578f66a889dSArnaldo Carvalho de Melo }
579f66a889dSArnaldo Carvalho de Melo 
5803ccf8a7bSArnaldo Carvalho de Melo struct perf_sample_id *evlist__id2sid(struct evlist *evlist, u64 id)
58170db7533SArnaldo Carvalho de Melo {
58270db7533SArnaldo Carvalho de Melo 	struct hlist_head *head;
58370db7533SArnaldo Carvalho de Melo 	struct perf_sample_id *sid;
58470db7533SArnaldo Carvalho de Melo 	int hash;
58570db7533SArnaldo Carvalho de Melo 
58670db7533SArnaldo Carvalho de Melo 	hash = hash_64(id, PERF_EVLIST__HLIST_BITS);
5871d5af02dSJiri Olsa 	head = &evlist->core.heads[hash];
58870db7533SArnaldo Carvalho de Melo 
589b67bfe0dSSasha Levin 	hlist_for_each_entry(sid, head, node)
59070db7533SArnaldo Carvalho de Melo 		if (sid->id == id)
591932a3594SJiri Olsa 			return sid;
592932a3594SJiri Olsa 
593932a3594SJiri Olsa 	return NULL;
594932a3594SJiri Olsa }
595932a3594SJiri Olsa 
5963ccf8a7bSArnaldo Carvalho de Melo struct evsel *evlist__id2evsel(struct evlist *evlist, u64 id)
597932a3594SJiri Olsa {
598932a3594SJiri Olsa 	struct perf_sample_id *sid;
599932a3594SJiri Olsa 
6006484d2f9SJiri Olsa 	if (evlist->core.nr_entries == 1 || !id)
601515dbe48SJiri Olsa 		return evlist__first(evlist);
602932a3594SJiri Olsa 
6033ccf8a7bSArnaldo Carvalho de Melo 	sid = evlist__id2sid(evlist, id);
604932a3594SJiri Olsa 	if (sid)
60570c20369SJiri Olsa 		return container_of(sid->evsel, struct evsel, core);
60630e68bccSNamhyung Kim 
6078cedf3a5SArnaldo Carvalho de Melo 	if (!evlist__sample_id_all(evlist))
608515dbe48SJiri Olsa 		return evlist__first(evlist);
60930e68bccSNamhyung Kim 
61070db7533SArnaldo Carvalho de Melo 	return NULL;
61170db7533SArnaldo Carvalho de Melo }
61204391debSArnaldo Carvalho de Melo 
6133ccf8a7bSArnaldo Carvalho de Melo struct evsel *evlist__id2evsel_strict(struct evlist *evlist, u64 id)
614dddcf6abSAdrian Hunter {
615dddcf6abSAdrian Hunter 	struct perf_sample_id *sid;
616dddcf6abSAdrian Hunter 
617dddcf6abSAdrian Hunter 	if (!id)
618dddcf6abSAdrian Hunter 		return NULL;
619dddcf6abSAdrian Hunter 
6203ccf8a7bSArnaldo Carvalho de Melo 	sid = evlist__id2sid(evlist, id);
621dddcf6abSAdrian Hunter 	if (sid)
62270c20369SJiri Olsa 		return container_of(sid->evsel, struct evsel, core);
623dddcf6abSAdrian Hunter 
624dddcf6abSAdrian Hunter 	return NULL;
625dddcf6abSAdrian Hunter }
626dddcf6abSAdrian Hunter 
6273ccf8a7bSArnaldo Carvalho de Melo static int evlist__event2id(struct evlist *evlist, union perf_event *event, u64 *id)
62875562573SAdrian Hunter {
629b1fcd190SJiri Olsa 	const __u64 *array = event->sample.array;
63075562573SAdrian Hunter 	ssize_t n;
63175562573SAdrian Hunter 
63275562573SAdrian Hunter 	n = (event->header.size - sizeof(event->header)) >> 3;
63375562573SAdrian Hunter 
63475562573SAdrian Hunter 	if (event->header.type == PERF_RECORD_SAMPLE) {
63575562573SAdrian Hunter 		if (evlist->id_pos >= n)
63675562573SAdrian Hunter 			return -1;
63775562573SAdrian Hunter 		*id = array[evlist->id_pos];
63875562573SAdrian Hunter 	} else {
63975562573SAdrian Hunter 		if (evlist->is_pos > n)
64075562573SAdrian Hunter 			return -1;
64175562573SAdrian Hunter 		n -= evlist->is_pos;
64275562573SAdrian Hunter 		*id = array[n];
64375562573SAdrian Hunter 	}
64475562573SAdrian Hunter 	return 0;
64575562573SAdrian Hunter }
64675562573SAdrian Hunter 
6473ccf8a7bSArnaldo Carvalho de Melo struct evsel *evlist__event2evsel(struct evlist *evlist, union perf_event *event)
64875562573SAdrian Hunter {
649515dbe48SJiri Olsa 	struct evsel *first = evlist__first(evlist);
65075562573SAdrian Hunter 	struct hlist_head *head;
65175562573SAdrian Hunter 	struct perf_sample_id *sid;
65275562573SAdrian Hunter 	int hash;
65375562573SAdrian Hunter 	u64 id;
65475562573SAdrian Hunter 
6556484d2f9SJiri Olsa 	if (evlist->core.nr_entries == 1)
65698be6966SAdrian Hunter 		return first;
65798be6966SAdrian Hunter 
6581fc632ceSJiri Olsa 	if (!first->core.attr.sample_id_all &&
65998be6966SAdrian Hunter 	    event->header.type != PERF_RECORD_SAMPLE)
66098be6966SAdrian Hunter 		return first;
66175562573SAdrian Hunter 
6623ccf8a7bSArnaldo Carvalho de Melo 	if (evlist__event2id(evlist, event, &id))
66375562573SAdrian Hunter 		return NULL;
66475562573SAdrian Hunter 
66575562573SAdrian Hunter 	/* Synthesized events have an id of zero */
66675562573SAdrian Hunter 	if (!id)
66798be6966SAdrian Hunter 		return first;
66875562573SAdrian Hunter 
66975562573SAdrian Hunter 	hash = hash_64(id, PERF_EVLIST__HLIST_BITS);
6701d5af02dSJiri Olsa 	head = &evlist->core.heads[hash];
67175562573SAdrian Hunter 
67275562573SAdrian Hunter 	hlist_for_each_entry(sid, head, node) {
67375562573SAdrian Hunter 		if (sid->id == id)
67470c20369SJiri Olsa 			return container_of(sid->evsel, struct evsel, core);
67575562573SAdrian Hunter 	}
67675562573SAdrian Hunter 	return NULL;
67775562573SAdrian Hunter }
67875562573SAdrian Hunter 
6790a60b339SArnaldo Carvalho de Melo static int evlist__set_paused(struct evlist *evlist, bool value)
68065aea233SWang Nan {
68165aea233SWang Nan 	int i;
68265aea233SWang Nan 
6830b72d69aSWang Nan 	if (!evlist->overwrite_mmap)
684078c3386SWang Nan 		return 0;
685078c3386SWang Nan 
686c976ee11SJiri Olsa 	for (i = 0; i < evlist->core.nr_mmaps; i++) {
6872cf07b29SJiri Olsa 		int fd = evlist->overwrite_mmap[i].core.fd;
68865aea233SWang Nan 		int err;
68965aea233SWang Nan 
69065aea233SWang Nan 		if (fd < 0)
69165aea233SWang Nan 			continue;
69265aea233SWang Nan 		err = ioctl(fd, PERF_EVENT_IOC_PAUSE_OUTPUT, value ? 1 : 0);
69365aea233SWang Nan 		if (err)
69465aea233SWang Nan 			return err;
69565aea233SWang Nan 	}
69665aea233SWang Nan 	return 0;
69765aea233SWang Nan }
69865aea233SWang Nan 
6990a60b339SArnaldo Carvalho de Melo static int evlist__pause(struct evlist *evlist)
70065aea233SWang Nan {
7010a60b339SArnaldo Carvalho de Melo 	return evlist__set_paused(evlist, true);
70265aea233SWang Nan }
70365aea233SWang Nan 
7040a60b339SArnaldo Carvalho de Melo static int evlist__resume(struct evlist *evlist)
70565aea233SWang Nan {
7060a60b339SArnaldo Carvalho de Melo 	return evlist__set_paused(evlist, false);
70765aea233SWang Nan }
70865aea233SWang Nan 
709db6b7b13SJiri Olsa static void evlist__munmap_nofree(struct evlist *evlist)
710f8a95309SArnaldo Carvalho de Melo {
711aece948fSArnaldo Carvalho de Melo 	int i;
712f8a95309SArnaldo Carvalho de Melo 
713b2cb615dSWang Nan 	if (evlist->mmap)
714c976ee11SJiri Olsa 		for (i = 0; i < evlist->core.nr_mmaps; i++)
71580e53d11SJiri Olsa 			perf_mmap__munmap(&evlist->mmap[i].core);
716b2cb615dSWang Nan 
7170b72d69aSWang Nan 	if (evlist->overwrite_mmap)
718c976ee11SJiri Olsa 		for (i = 0; i < evlist->core.nr_mmaps; i++)
71980e53d11SJiri Olsa 			perf_mmap__munmap(&evlist->overwrite_mmap[i].core);
720a1f72618SWang Nan }
721aece948fSArnaldo Carvalho de Melo 
722db6b7b13SJiri Olsa void evlist__munmap(struct evlist *evlist)
723a1f72618SWang Nan {
724db6b7b13SJiri Olsa 	evlist__munmap_nofree(evlist);
72504662523SArnaldo Carvalho de Melo 	zfree(&evlist->mmap);
7260b72d69aSWang Nan 	zfree(&evlist->overwrite_mmap);
727f8a95309SArnaldo Carvalho de Melo }
728f8a95309SArnaldo Carvalho de Melo 
72980e53d11SJiri Olsa static void perf_mmap__unmap_cb(struct perf_mmap *map)
73080e53d11SJiri Olsa {
73180e53d11SJiri Olsa 	struct mmap *m = container_of(map, struct mmap, core);
73280e53d11SJiri Olsa 
73380e53d11SJiri Olsa 	mmap__munmap(m);
73480e53d11SJiri Olsa }
73580e53d11SJiri Olsa 
736d50cf361SJiri Olsa static struct mmap *evlist__alloc_mmap(struct evlist *evlist,
7372c5f6d87SKan Liang 				       bool overwrite)
738f8a95309SArnaldo Carvalho de Melo {
739d4c6fb36SWang Nan 	int i;
740a5830532SJiri Olsa 	struct mmap *map;
741d4c6fb36SWang Nan 
742c976ee11SJiri Olsa 	map = zalloc(evlist->core.nr_mmaps * sizeof(struct mmap));
7438db6d6b1SWang Nan 	if (!map)
7448db6d6b1SWang Nan 		return NULL;
745946ae1d4SWang Nan 
746c976ee11SJiri Olsa 	for (i = 0; i < evlist->core.nr_mmaps; i++) {
7476eb65f7aSJiri Olsa 		struct perf_mmap *prev = i ? &map[i - 1].core : NULL;
7486eb65f7aSJiri Olsa 
7494738ca30SArnaldo Carvalho de Melo 		/*
7504738ca30SArnaldo Carvalho de Melo 		 * When the perf_mmap() call is made we grab one refcount, plus
7516afad54dSKan Liang 		 * one extra to let perf_mmap__consume() get the last
7524738ca30SArnaldo Carvalho de Melo 		 * events after all real references (perf_mmap__get()) are
7534738ca30SArnaldo Carvalho de Melo 		 * dropped.
7544738ca30SArnaldo Carvalho de Melo 		 *
7554738ca30SArnaldo Carvalho de Melo 		 * Each PERF_EVENT_IOC_SET_OUTPUT points to this mmap and
7564738ca30SArnaldo Carvalho de Melo 		 * thus does perf_mmap__get() on it.
7574738ca30SArnaldo Carvalho de Melo 		 */
7586eb65f7aSJiri Olsa 		perf_mmap__init(&map[i].core, prev, overwrite, perf_mmap__unmap_cb);
7594738ca30SArnaldo Carvalho de Melo 	}
760353120b4SJiri Olsa 
7618db6d6b1SWang Nan 	return map;
762f8a95309SArnaldo Carvalho de Melo }
763f8a95309SArnaldo Carvalho de Melo 
7649abd2ab2SJiri Olsa static void
7659abd2ab2SJiri Olsa perf_evlist__mmap_cb_idx(struct perf_evlist *_evlist,
7669abd2ab2SJiri Olsa 			 struct perf_mmap_param *_mp,
7679abd2ab2SJiri Olsa 			 int idx, bool per_cpu)
7689abd2ab2SJiri Olsa {
7699abd2ab2SJiri Olsa 	struct evlist *evlist = container_of(_evlist, struct evlist, core);
7709abd2ab2SJiri Olsa 	struct mmap_params *mp = container_of(_mp, struct mmap_params, core);
7719abd2ab2SJiri Olsa 
7729abd2ab2SJiri Olsa 	auxtrace_mmap_params__set_idx(&mp->auxtrace_mp, evlist, idx, per_cpu);
7739abd2ab2SJiri Olsa }
7749abd2ab2SJiri Olsa 
775bb1b1885SJiri Olsa static struct perf_mmap*
776bb1b1885SJiri Olsa perf_evlist__mmap_cb_get(struct perf_evlist *_evlist, bool overwrite, int idx)
777bb1b1885SJiri Olsa {
778bb1b1885SJiri Olsa 	struct evlist *evlist = container_of(_evlist, struct evlist, core);
7793805e4f3SJiri Olsa 	struct mmap *maps;
780bb1b1885SJiri Olsa 
7813805e4f3SJiri Olsa 	maps = overwrite ? evlist->overwrite_mmap : evlist->mmap;
782bb1b1885SJiri Olsa 
783bb1b1885SJiri Olsa 	if (!maps) {
7843805e4f3SJiri Olsa 		maps = evlist__alloc_mmap(evlist, overwrite);
785bb1b1885SJiri Olsa 		if (!maps)
786bb1b1885SJiri Olsa 			return NULL;
787bb1b1885SJiri Olsa 
7883805e4f3SJiri Olsa 		if (overwrite) {
789bb1b1885SJiri Olsa 			evlist->overwrite_mmap = maps;
790bb1b1885SJiri Olsa 			if (evlist->bkw_mmap_state == BKW_MMAP_NOTREADY)
791ade9d208SArnaldo Carvalho de Melo 				evlist__toggle_bkw_mmap(evlist, BKW_MMAP_RUNNING);
7923805e4f3SJiri Olsa 		} else {
7933805e4f3SJiri Olsa 			evlist->mmap = maps;
794bb1b1885SJiri Olsa 		}
795bb1b1885SJiri Olsa 	}
796bb1b1885SJiri Olsa 
797bb1b1885SJiri Olsa 	return &maps[idx].core;
798bb1b1885SJiri Olsa }
799bb1b1885SJiri Olsa 
800b80132b1SJiri Olsa static int
801b80132b1SJiri Olsa perf_evlist__mmap_cb_mmap(struct perf_mmap *_map, struct perf_mmap_param *_mp,
802b80132b1SJiri Olsa 			  int output, int cpu)
803b80132b1SJiri Olsa {
804b80132b1SJiri Olsa 	struct mmap *map = container_of(_map, struct mmap, core);
805b80132b1SJiri Olsa 	struct mmap_params *mp = container_of(_mp, struct mmap_params, core);
806b80132b1SJiri Olsa 
807b80132b1SJiri Olsa 	return mmap__mmap(map, mp, output, cpu);
808b80132b1SJiri Olsa }
809b80132b1SJiri Olsa 
810f5e7150cSArnaldo Carvalho de Melo unsigned long perf_event_mlock_kb_in_pages(void)
811994a1f78SJiri Olsa {
812f5e7150cSArnaldo Carvalho de Melo 	unsigned long pages;
8138185e881SArnaldo Carvalho de Melo 	int max;
8148185e881SArnaldo Carvalho de Melo 
8158185e881SArnaldo Carvalho de Melo 	if (sysctl__read_int("kernel/perf_event_mlock_kb", &max) < 0) {
8168185e881SArnaldo Carvalho de Melo 		/*
8178185e881SArnaldo Carvalho de Melo 		 * Pick a once upon a time good value, i.e. things look
8188185e881SArnaldo Carvalho de Melo 		 * strange since we can't read a sysctl value, but lets not
8198185e881SArnaldo Carvalho de Melo 		 * die yet...
8208185e881SArnaldo Carvalho de Melo 		 */
8218185e881SArnaldo Carvalho de Melo 		max = 512;
8228185e881SArnaldo Carvalho de Melo 	} else {
8238185e881SArnaldo Carvalho de Melo 		max -= (page_size / 1024);
8248185e881SArnaldo Carvalho de Melo 	}
8258185e881SArnaldo Carvalho de Melo 
8268185e881SArnaldo Carvalho de Melo 	pages = (max * 1024) / page_size;
8271be300f4SArnaldo Carvalho de Melo 	if (!is_power_of_2(pages))
8281be300f4SArnaldo Carvalho de Melo 		pages = rounddown_pow_of_two(pages);
829f5e7150cSArnaldo Carvalho de Melo 
830f5e7150cSArnaldo Carvalho de Melo 	return pages;
831f5e7150cSArnaldo Carvalho de Melo }
832f5e7150cSArnaldo Carvalho de Melo 
8339521b5f2SJiri Olsa size_t evlist__mmap_size(unsigned long pages)
834f5e7150cSArnaldo Carvalho de Melo {
835f5e7150cSArnaldo Carvalho de Melo 	if (pages == UINT_MAX)
836f5e7150cSArnaldo Carvalho de Melo 		pages = perf_event_mlock_kb_in_pages();
837f5e7150cSArnaldo Carvalho de Melo 	else if (!is_power_of_2(pages))
838994a1f78SJiri Olsa 		return 0;
839994a1f78SJiri Olsa 
840994a1f78SJiri Olsa 	return (pages + 1) * page_size;
841994a1f78SJiri Olsa }
842994a1f78SJiri Olsa 
84333c2dcfdSDavid Ahern static long parse_pages_arg(const char *str, unsigned long min,
84433c2dcfdSDavid Ahern 			    unsigned long max)
845994a1f78SJiri Olsa {
8462fbe4abeSAdrian Hunter 	unsigned long pages, val;
84727050f53SJiri Olsa 	static struct parse_tag tags[] = {
84827050f53SJiri Olsa 		{ .tag  = 'B', .mult = 1       },
84927050f53SJiri Olsa 		{ .tag  = 'K', .mult = 1 << 10 },
85027050f53SJiri Olsa 		{ .tag  = 'M', .mult = 1 << 20 },
85127050f53SJiri Olsa 		{ .tag  = 'G', .mult = 1 << 30 },
85227050f53SJiri Olsa 		{ .tag  = 0 },
85327050f53SJiri Olsa 	};
854994a1f78SJiri Olsa 
8558973504bSDavid Ahern 	if (str == NULL)
85633c2dcfdSDavid Ahern 		return -EINVAL;
8578973504bSDavid Ahern 
85827050f53SJiri Olsa 	val = parse_tag_value(str, tags);
8592fbe4abeSAdrian Hunter 	if (val != (unsigned long) -1) {
86027050f53SJiri Olsa 		/* we got file size value */
86127050f53SJiri Olsa 		pages = PERF_ALIGN(val, page_size) / page_size;
86227050f53SJiri Olsa 	} else {
86327050f53SJiri Olsa 		/* we got pages count value */
86427050f53SJiri Olsa 		char *eptr;
865994a1f78SJiri Olsa 		pages = strtoul(str, &eptr, 10);
86633c2dcfdSDavid Ahern 		if (*eptr != '\0')
86733c2dcfdSDavid Ahern 			return -EINVAL;
86827050f53SJiri Olsa 	}
869994a1f78SJiri Olsa 
8702bcab6c1SAdrian Hunter 	if (pages == 0 && min == 0) {
87133c2dcfdSDavid Ahern 		/* leave number of pages at 0 */
8721dbfa938SAdrian Hunter 	} else if (!is_power_of_2(pages)) {
8739808143bSJiri Olsa 		char buf[100];
8749808143bSJiri Olsa 
87533c2dcfdSDavid Ahern 		/* round pages up to next power of 2 */
87691529834SArnaldo Carvalho de Melo 		pages = roundup_pow_of_two(pages);
8771dbfa938SAdrian Hunter 		if (!pages)
8781dbfa938SAdrian Hunter 			return -EINVAL;
8799808143bSJiri Olsa 
8809808143bSJiri Olsa 		unit_number__scnprintf(buf, sizeof(buf), pages * page_size);
8819808143bSJiri Olsa 		pr_info("rounding mmap pages size to %s (%lu pages)\n",
8829808143bSJiri Olsa 			buf, pages);
8832fbe4abeSAdrian Hunter 	}
8842fbe4abeSAdrian Hunter 
88533c2dcfdSDavid Ahern 	if (pages > max)
88633c2dcfdSDavid Ahern 		return -EINVAL;
88733c2dcfdSDavid Ahern 
88833c2dcfdSDavid Ahern 	return pages;
88933c2dcfdSDavid Ahern }
89033c2dcfdSDavid Ahern 
89125f84702SArnaldo Carvalho de Melo int __evlist__parse_mmap_pages(unsigned int *mmap_pages, const char *str)
89233c2dcfdSDavid Ahern {
89333c2dcfdSDavid Ahern 	unsigned long max = UINT_MAX;
89433c2dcfdSDavid Ahern 	long pages;
89533c2dcfdSDavid Ahern 
896f5ae9c42SAdrian Hunter 	if (max > SIZE_MAX / page_size)
89733c2dcfdSDavid Ahern 		max = SIZE_MAX / page_size;
89833c2dcfdSDavid Ahern 
89933c2dcfdSDavid Ahern 	pages = parse_pages_arg(str, 1, max);
90033c2dcfdSDavid Ahern 	if (pages < 0) {
90133c2dcfdSDavid Ahern 		pr_err("Invalid argument for --mmap_pages/-m\n");
902994a1f78SJiri Olsa 		return -1;
903994a1f78SJiri Olsa 	}
904994a1f78SJiri Olsa 
905994a1f78SJiri Olsa 	*mmap_pages = pages;
906994a1f78SJiri Olsa 	return 0;
907994a1f78SJiri Olsa }
908994a1f78SJiri Olsa 
90925f84702SArnaldo Carvalho de Melo int evlist__parse_mmap_pages(const struct option *opt, const char *str, int unset __maybe_unused)
910e9db1310SAdrian Hunter {
91125f84702SArnaldo Carvalho de Melo 	return __evlist__parse_mmap_pages(opt->value, str);
912e9db1310SAdrian Hunter }
913e9db1310SAdrian Hunter 
914c83fa7f2SAdrian Hunter /**
9159521b5f2SJiri Olsa  * evlist__mmap_ex - Create mmaps to receive events.
916c83fa7f2SAdrian Hunter  * @evlist: list of events
917c83fa7f2SAdrian Hunter  * @pages: map length in pages
918c83fa7f2SAdrian Hunter  * @overwrite: overwrite older events?
919718c602dSAdrian Hunter  * @auxtrace_pages - auxtrace map length in pages
920718c602dSAdrian Hunter  * @auxtrace_overwrite - overwrite older auxtrace data?
921f8a95309SArnaldo Carvalho de Melo  *
922c83fa7f2SAdrian Hunter  * If @overwrite is %false the user needs to signal event consumption using
9239521b5f2SJiri Olsa  * perf_mmap__write_tail().  Using evlist__mmap_read() does this
924c83fa7f2SAdrian Hunter  * automatically.
925f8a95309SArnaldo Carvalho de Melo  *
926718c602dSAdrian Hunter  * Similarly, if @auxtrace_overwrite is %false the user needs to signal data
927718c602dSAdrian Hunter  * consumption using auxtrace_mmap__write_tail().
928718c602dSAdrian Hunter  *
929c83fa7f2SAdrian Hunter  * Return: %0 on success, negative error code otherwise.
930f8a95309SArnaldo Carvalho de Melo  */
9319521b5f2SJiri Olsa int evlist__mmap_ex(struct evlist *evlist, unsigned int pages,
9327a276ff6SWang Nan 			 unsigned int auxtrace_pages,
93351255a8aSAlexey Budankov 			 bool auxtrace_overwrite, int nr_cblocks, int affinity, int flush,
93451255a8aSAlexey Budankov 			 int comp_level)
935f8a95309SArnaldo Carvalho de Melo {
93671f566a3SWang Nan 	/*
93771f566a3SWang Nan 	 * Delay setting mp.prot: set it before calling perf_mmap__mmap.
93871f566a3SWang Nan 	 * Its value is decided by evsel's write_backward.
93971f566a3SWang Nan 	 * So &mp should not be passed through const pointer.
94071f566a3SWang Nan 	 */
941e440979fSJiri Olsa 	struct mmap_params mp = {
942e440979fSJiri Olsa 		.nr_cblocks	= nr_cblocks,
943e440979fSJiri Olsa 		.affinity	= affinity,
944e440979fSJiri Olsa 		.flush		= flush,
945e440979fSJiri Olsa 		.comp_level	= comp_level
946e440979fSJiri Olsa 	};
947923d0f18SJiri Olsa 	struct perf_evlist_mmap_ops ops = {
9489abd2ab2SJiri Olsa 		.idx  = perf_evlist__mmap_cb_idx,
949bb1b1885SJiri Olsa 		.get  = perf_evlist__mmap_cb_get,
950b80132b1SJiri Olsa 		.mmap = perf_evlist__mmap_cb_mmap,
9519abd2ab2SJiri Olsa 	};
95250a682ceSArnaldo Carvalho de Melo 
953f6fa4375SJiri Olsa 	evlist->core.mmap_len = evlist__mmap_size(pages);
954f6fa4375SJiri Olsa 	pr_debug("mmap size %zuB\n", evlist->core.mmap_len);
955f8a95309SArnaldo Carvalho de Melo 
956f6fa4375SJiri Olsa 	auxtrace_mmap_params__init(&mp.auxtrace_mp, evlist->core.mmap_len,
957718c602dSAdrian Hunter 				   auxtrace_pages, auxtrace_overwrite);
958718c602dSAdrian Hunter 
959923d0f18SJiri Olsa 	return perf_evlist__mmap_ops(&evlist->core, &ops, &mp.core);
960f8a95309SArnaldo Carvalho de Melo }
9617e2ed097SArnaldo Carvalho de Melo 
9629521b5f2SJiri Olsa int evlist__mmap(struct evlist *evlist, unsigned int pages)
963718c602dSAdrian Hunter {
9649521b5f2SJiri Olsa 	return evlist__mmap_ex(evlist, pages, 0, false, 0, PERF_AFFINITY_SYS, 1, 0);
965718c602dSAdrian Hunter }
966718c602dSAdrian Hunter 
9677748bb71SArnaldo Carvalho de Melo int evlist__create_maps(struct evlist *evlist, struct target *target)
9687e2ed097SArnaldo Carvalho de Melo {
969147c508fSJin Yao 	bool all_threads = (target->per_thread && target->system_wide);
970f854839bSJiri Olsa 	struct perf_cpu_map *cpus;
9719749b90eSJiri Olsa 	struct perf_thread_map *threads;
9727e2ed097SArnaldo Carvalho de Melo 
973147c508fSJin Yao 	/*
974147c508fSJin Yao 	 * If specify '-a' and '--per-thread' to perf record, perf record
975147c508fSJin Yao 	 * will override '--per-thread'. target->per_thread = false and
976147c508fSJin Yao 	 * target->system_wide = true.
977147c508fSJin Yao 	 *
978147c508fSJin Yao 	 * If specify '--per-thread' only to perf record,
979147c508fSJin Yao 	 * target->per_thread = true and target->system_wide = false.
980147c508fSJin Yao 	 *
981147c508fSJin Yao 	 * So target->per_thread && target->system_wide is false.
982147c508fSJin Yao 	 * For perf record, thread_map__new_str doesn't call
983147c508fSJin Yao 	 * thread_map__new_all_cpus. That will keep perf record's
984147c508fSJin Yao 	 * current behavior.
985147c508fSJin Yao 	 *
986147c508fSJin Yao 	 * For perf stat, it allows the case that target->per_thread and
987147c508fSJin Yao 	 * target->system_wide are all true. It means to collect system-wide
988147c508fSJin Yao 	 * per-thread data. thread_map__new_str will call
989147c508fSJin Yao 	 * thread_map__new_all_cpus to enumerate all threads.
990147c508fSJin Yao 	 */
99173c0ca1eSJin Yao 	threads = thread_map__new_str(target->pid, target->tid, target->uid,
992147c508fSJin Yao 				      all_threads);
99374bfd2b2SAdrian Hunter 
99474bfd2b2SAdrian Hunter 	if (!threads)
9957e2ed097SArnaldo Carvalho de Melo 		return -1;
9967e2ed097SArnaldo Carvalho de Melo 
9979c105fbcSDongsheng Yang 	if (target__uses_dummy_map(target))
998397721e0SJiri Olsa 		cpus = perf_cpu_map__dummy_new();
999879d77d0SNamhyung Kim 	else
10009c3516d1SJiri Olsa 		cpus = perf_cpu_map__new(target->cpu_list);
10017e2ed097SArnaldo Carvalho de Melo 
100274bfd2b2SAdrian Hunter 	if (!cpus)
10037e2ed097SArnaldo Carvalho de Melo 		goto out_delete_threads;
10047e2ed097SArnaldo Carvalho de Melo 
1005ec903f26SJiri Olsa 	evlist->core.has_user_cpus = !!target->cpu_list;
1006ec9a77a7SAdrian Hunter 
1007453fa030SJiri Olsa 	perf_evlist__set_maps(&evlist->core, cpus, threads);
1008d5bc056eSAdrian Hunter 
1009bfd1b83dSNamhyung Kim 	/* as evlist now has references, put count here */
1010bfd1b83dSNamhyung Kim 	perf_cpu_map__put(cpus);
1011bfd1b83dSNamhyung Kim 	perf_thread_map__put(threads);
1012bfd1b83dSNamhyung Kim 
1013d5bc056eSAdrian Hunter 	return 0;
10147e2ed097SArnaldo Carvalho de Melo 
10157e2ed097SArnaldo Carvalho de Melo out_delete_threads:
10167836e52eSJiri Olsa 	perf_thread_map__put(threads);
10177e2ed097SArnaldo Carvalho de Melo 	return -1;
10187e2ed097SArnaldo Carvalho de Melo }
10197e2ed097SArnaldo Carvalho de Melo 
102024bf91a7SArnaldo Carvalho de Melo int evlist__apply_filters(struct evlist *evlist, struct evsel **err_evsel)
10210a102479SFrederic Weisbecker {
102232dcd021SJiri Olsa 	struct evsel *evsel;
1023745cefc5SArnaldo Carvalho de Melo 	int err = 0;
10240a102479SFrederic Weisbecker 
1025e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, evsel) {
1026745cefc5SArnaldo Carvalho de Melo 		if (evsel->filter == NULL)
10270a102479SFrederic Weisbecker 			continue;
1028745cefc5SArnaldo Carvalho de Melo 
1029d988d5eeSKan Liang 		/*
1030d988d5eeSKan Liang 		 * filters only work for tracepoint event, which doesn't have cpu limit.
1031d988d5eeSKan Liang 		 * So evlist and evsel should always be same.
1032d988d5eeSKan Liang 		 */
1033a00571fdSJiri Olsa 		err = perf_evsel__apply_filter(&evsel->core, evsel->filter);
103423d4aad4SArnaldo Carvalho de Melo 		if (err) {
103523d4aad4SArnaldo Carvalho de Melo 			*err_evsel = evsel;
1036745cefc5SArnaldo Carvalho de Melo 			break;
10370a102479SFrederic Weisbecker 		}
103823d4aad4SArnaldo Carvalho de Melo 	}
10390a102479SFrederic Weisbecker 
1040745cefc5SArnaldo Carvalho de Melo 	return err;
1041745cefc5SArnaldo Carvalho de Melo }
1042745cefc5SArnaldo Carvalho de Melo 
104324bf91a7SArnaldo Carvalho de Melo int evlist__set_tp_filter(struct evlist *evlist, const char *filter)
1044745cefc5SArnaldo Carvalho de Melo {
104532dcd021SJiri Olsa 	struct evsel *evsel;
1046745cefc5SArnaldo Carvalho de Melo 	int err = 0;
1047745cefc5SArnaldo Carvalho de Melo 
104805cea449SArnaldo Carvalho de Melo 	if (filter == NULL)
104905cea449SArnaldo Carvalho de Melo 		return -1;
105005cea449SArnaldo Carvalho de Melo 
1051e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, evsel) {
10521fc632ceSJiri Olsa 		if (evsel->core.attr.type != PERF_TYPE_TRACEPOINT)
1053fdf14720SWang Nan 			continue;
1054fdf14720SWang Nan 
1055ad681adfSArnaldo Carvalho de Melo 		err = evsel__set_filter(evsel, filter);
1056745cefc5SArnaldo Carvalho de Melo 		if (err)
1057745cefc5SArnaldo Carvalho de Melo 			break;
1058745cefc5SArnaldo Carvalho de Melo 	}
1059745cefc5SArnaldo Carvalho de Melo 
1060745cefc5SArnaldo Carvalho de Melo 	return err;
10610a102479SFrederic Weisbecker }
106274429964SFrederic Weisbecker 
106324bf91a7SArnaldo Carvalho de Melo int evlist__append_tp_filter(struct evlist *evlist, const char *filter)
106453c92f73SArnaldo Carvalho de Melo {
106553c92f73SArnaldo Carvalho de Melo 	struct evsel *evsel;
106653c92f73SArnaldo Carvalho de Melo 	int err = 0;
106753c92f73SArnaldo Carvalho de Melo 
106853c92f73SArnaldo Carvalho de Melo 	if (filter == NULL)
106953c92f73SArnaldo Carvalho de Melo 		return -1;
107053c92f73SArnaldo Carvalho de Melo 
107153c92f73SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, evsel) {
107253c92f73SArnaldo Carvalho de Melo 		if (evsel->core.attr.type != PERF_TYPE_TRACEPOINT)
107353c92f73SArnaldo Carvalho de Melo 			continue;
107453c92f73SArnaldo Carvalho de Melo 
1075ad681adfSArnaldo Carvalho de Melo 		err = evsel__append_tp_filter(evsel, filter);
107653c92f73SArnaldo Carvalho de Melo 		if (err)
107753c92f73SArnaldo Carvalho de Melo 			break;
107853c92f73SArnaldo Carvalho de Melo 	}
107953c92f73SArnaldo Carvalho de Melo 
108053c92f73SArnaldo Carvalho de Melo 	return err;
108153c92f73SArnaldo Carvalho de Melo }
108253c92f73SArnaldo Carvalho de Melo 
1083da949f50SArnaldo Carvalho de Melo char *asprintf__tp_filter_pids(size_t npids, pid_t *pids)
1084cfd70a26SArnaldo Carvalho de Melo {
1085cfd70a26SArnaldo Carvalho de Melo 	char *filter;
1086be199adaSArnaldo Carvalho de Melo 	size_t i;
1087cfd70a26SArnaldo Carvalho de Melo 
1088be199adaSArnaldo Carvalho de Melo 	for (i = 0; i < npids; ++i) {
1089be199adaSArnaldo Carvalho de Melo 		if (i == 0) {
1090be199adaSArnaldo Carvalho de Melo 			if (asprintf(&filter, "common_pid != %d", pids[i]) < 0)
109105cea449SArnaldo Carvalho de Melo 				return NULL;
1092be199adaSArnaldo Carvalho de Melo 		} else {
1093be199adaSArnaldo Carvalho de Melo 			char *tmp;
1094be199adaSArnaldo Carvalho de Melo 
1095be199adaSArnaldo Carvalho de Melo 			if (asprintf(&tmp, "%s && common_pid != %d", filter, pids[i]) < 0)
1096be199adaSArnaldo Carvalho de Melo 				goto out_free;
1097be199adaSArnaldo Carvalho de Melo 
1098be199adaSArnaldo Carvalho de Melo 			free(filter);
1099be199adaSArnaldo Carvalho de Melo 			filter = tmp;
1100be199adaSArnaldo Carvalho de Melo 		}
1101be199adaSArnaldo Carvalho de Melo 	}
1102cfd70a26SArnaldo Carvalho de Melo 
110305cea449SArnaldo Carvalho de Melo 	return filter;
1104be199adaSArnaldo Carvalho de Melo out_free:
1105cfd70a26SArnaldo Carvalho de Melo 	free(filter);
110605cea449SArnaldo Carvalho de Melo 	return NULL;
110705cea449SArnaldo Carvalho de Melo }
110805cea449SArnaldo Carvalho de Melo 
110924bf91a7SArnaldo Carvalho de Melo int evlist__set_tp_filter_pids(struct evlist *evlist, size_t npids, pid_t *pids)
111005cea449SArnaldo Carvalho de Melo {
111105cea449SArnaldo Carvalho de Melo 	char *filter = asprintf__tp_filter_pids(npids, pids);
111224bf91a7SArnaldo Carvalho de Melo 	int ret = evlist__set_tp_filter(evlist, filter);
111305cea449SArnaldo Carvalho de Melo 
111405cea449SArnaldo Carvalho de Melo 	free(filter);
1115cfd70a26SArnaldo Carvalho de Melo 	return ret;
1116cfd70a26SArnaldo Carvalho de Melo }
1117cfd70a26SArnaldo Carvalho de Melo 
111824bf91a7SArnaldo Carvalho de Melo int evlist__set_tp_filter_pid(struct evlist *evlist, pid_t pid)
1119be199adaSArnaldo Carvalho de Melo {
112024bf91a7SArnaldo Carvalho de Melo 	return evlist__set_tp_filter_pids(evlist, 1, &pid);
1121be199adaSArnaldo Carvalho de Melo }
1122be199adaSArnaldo Carvalho de Melo 
112324bf91a7SArnaldo Carvalho de Melo int evlist__append_tp_filter_pids(struct evlist *evlist, size_t npids, pid_t *pids)
11241827ab5bSArnaldo Carvalho de Melo {
11251827ab5bSArnaldo Carvalho de Melo 	char *filter = asprintf__tp_filter_pids(npids, pids);
112624bf91a7SArnaldo Carvalho de Melo 	int ret = evlist__append_tp_filter(evlist, filter);
11271827ab5bSArnaldo Carvalho de Melo 
11281827ab5bSArnaldo Carvalho de Melo 	free(filter);
11291827ab5bSArnaldo Carvalho de Melo 	return ret;
11301827ab5bSArnaldo Carvalho de Melo }
11311827ab5bSArnaldo Carvalho de Melo 
113224bf91a7SArnaldo Carvalho de Melo int evlist__append_tp_filter_pid(struct evlist *evlist, pid_t pid)
11331827ab5bSArnaldo Carvalho de Melo {
113424bf91a7SArnaldo Carvalho de Melo 	return evlist__append_tp_filter_pids(evlist, 1, &pid);
11351827ab5bSArnaldo Carvalho de Melo }
11361827ab5bSArnaldo Carvalho de Melo 
1137b3c2cc2bSArnaldo Carvalho de Melo bool evlist__valid_sample_type(struct evlist *evlist)
113874429964SFrederic Weisbecker {
113932dcd021SJiri Olsa 	struct evsel *pos;
1140c2a70653SArnaldo Carvalho de Melo 
11416484d2f9SJiri Olsa 	if (evlist->core.nr_entries == 1)
114275562573SAdrian Hunter 		return true;
114375562573SAdrian Hunter 
114475562573SAdrian Hunter 	if (evlist->id_pos < 0 || evlist->is_pos < 0)
114575562573SAdrian Hunter 		return false;
114675562573SAdrian Hunter 
1147e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, pos) {
114875562573SAdrian Hunter 		if (pos->id_pos != evlist->id_pos ||
114975562573SAdrian Hunter 		    pos->is_pos != evlist->is_pos)
1150c2a70653SArnaldo Carvalho de Melo 			return false;
115174429964SFrederic Weisbecker 	}
115274429964SFrederic Weisbecker 
1153c2a70653SArnaldo Carvalho de Melo 	return true;
1154c2a70653SArnaldo Carvalho de Melo }
1155c2a70653SArnaldo Carvalho de Melo 
1156b3c2cc2bSArnaldo Carvalho de Melo u64 __evlist__combined_sample_type(struct evlist *evlist)
1157c2a70653SArnaldo Carvalho de Melo {
115832dcd021SJiri Olsa 	struct evsel *evsel;
115975562573SAdrian Hunter 
116075562573SAdrian Hunter 	if (evlist->combined_sample_type)
116175562573SAdrian Hunter 		return evlist->combined_sample_type;
116275562573SAdrian Hunter 
1163e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, evsel)
11641fc632ceSJiri Olsa 		evlist->combined_sample_type |= evsel->core.attr.sample_type;
116575562573SAdrian Hunter 
116675562573SAdrian Hunter 	return evlist->combined_sample_type;
116775562573SAdrian Hunter }
116875562573SAdrian Hunter 
1169b3c2cc2bSArnaldo Carvalho de Melo u64 evlist__combined_sample_type(struct evlist *evlist)
117075562573SAdrian Hunter {
117175562573SAdrian Hunter 	evlist->combined_sample_type = 0;
1172b3c2cc2bSArnaldo Carvalho de Melo 	return __evlist__combined_sample_type(evlist);
1173c2a70653SArnaldo Carvalho de Melo }
1174c2a70653SArnaldo Carvalho de Melo 
117592c7d7cdSArnaldo Carvalho de Melo u64 evlist__combined_branch_type(struct evlist *evlist)
117698df858eSAndi Kleen {
117732dcd021SJiri Olsa 	struct evsel *evsel;
117898df858eSAndi Kleen 	u64 branch_type = 0;
117998df858eSAndi Kleen 
1180e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, evsel)
11811fc632ceSJiri Olsa 		branch_type |= evsel->core.attr.branch_sample_type;
118298df858eSAndi Kleen 	return branch_type;
118398df858eSAndi Kleen }
118498df858eSAndi Kleen 
118578e1bc25SArnaldo Carvalho de Melo bool evlist__valid_read_format(struct evlist *evlist)
11869ede473cSJiri Olsa {
1187515dbe48SJiri Olsa 	struct evsel *first = evlist__first(evlist), *pos = first;
11881fc632ceSJiri Olsa 	u64 read_format = first->core.attr.read_format;
11891fc632ceSJiri Olsa 	u64 sample_type = first->core.attr.sample_type;
11909ede473cSJiri Olsa 
1191e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, pos) {
119294d3820fSAdrian Hunter 		if (read_format != pos->core.attr.read_format) {
119394d3820fSAdrian Hunter 			pr_debug("Read format differs %#" PRIx64 " vs %#" PRIx64 "\n",
119494d3820fSAdrian Hunter 				 read_format, (u64)pos->core.attr.read_format);
119594d3820fSAdrian Hunter 		}
11969ede473cSJiri Olsa 	}
11979ede473cSJiri Olsa 
11984d39c89fSIngo Molnar 	/* PERF_SAMPLE_READ implies PERF_FORMAT_ID. */
11999ede473cSJiri Olsa 	if ((sample_type & PERF_SAMPLE_READ) &&
12009ede473cSJiri Olsa 	    !(read_format & PERF_FORMAT_ID)) {
12019ede473cSJiri Olsa 		return false;
12029ede473cSJiri Olsa 	}
12039ede473cSJiri Olsa 
12049ede473cSJiri Olsa 	return true;
12059ede473cSJiri Olsa }
12069ede473cSJiri Olsa 
12071420ba2fSArnaldo Carvalho de Melo u16 evlist__id_hdr_size(struct evlist *evlist)
120881e36bffSArnaldo Carvalho de Melo {
1209515dbe48SJiri Olsa 	struct evsel *first = evlist__first(evlist);
121081e36bffSArnaldo Carvalho de Melo 	struct perf_sample *data;
121181e36bffSArnaldo Carvalho de Melo 	u64 sample_type;
121281e36bffSArnaldo Carvalho de Melo 	u16 size = 0;
121381e36bffSArnaldo Carvalho de Melo 
12141fc632ceSJiri Olsa 	if (!first->core.attr.sample_id_all)
121581e36bffSArnaldo Carvalho de Melo 		goto out;
121681e36bffSArnaldo Carvalho de Melo 
12171fc632ceSJiri Olsa 	sample_type = first->core.attr.sample_type;
121881e36bffSArnaldo Carvalho de Melo 
121981e36bffSArnaldo Carvalho de Melo 	if (sample_type & PERF_SAMPLE_TID)
122081e36bffSArnaldo Carvalho de Melo 		size += sizeof(data->tid) * 2;
122181e36bffSArnaldo Carvalho de Melo 
122281e36bffSArnaldo Carvalho de Melo        if (sample_type & PERF_SAMPLE_TIME)
122381e36bffSArnaldo Carvalho de Melo 		size += sizeof(data->time);
122481e36bffSArnaldo Carvalho de Melo 
122581e36bffSArnaldo Carvalho de Melo 	if (sample_type & PERF_SAMPLE_ID)
122681e36bffSArnaldo Carvalho de Melo 		size += sizeof(data->id);
122781e36bffSArnaldo Carvalho de Melo 
122881e36bffSArnaldo Carvalho de Melo 	if (sample_type & PERF_SAMPLE_STREAM_ID)
122981e36bffSArnaldo Carvalho de Melo 		size += sizeof(data->stream_id);
123081e36bffSArnaldo Carvalho de Melo 
123181e36bffSArnaldo Carvalho de Melo 	if (sample_type & PERF_SAMPLE_CPU)
123281e36bffSArnaldo Carvalho de Melo 		size += sizeof(data->cpu) * 2;
123375562573SAdrian Hunter 
123475562573SAdrian Hunter 	if (sample_type & PERF_SAMPLE_IDENTIFIER)
123575562573SAdrian Hunter 		size += sizeof(data->id);
123681e36bffSArnaldo Carvalho de Melo out:
123781e36bffSArnaldo Carvalho de Melo 	return size;
123881e36bffSArnaldo Carvalho de Melo }
123981e36bffSArnaldo Carvalho de Melo 
12408cedf3a5SArnaldo Carvalho de Melo bool evlist__valid_sample_id_all(struct evlist *evlist)
1241c2a70653SArnaldo Carvalho de Melo {
1242515dbe48SJiri Olsa 	struct evsel *first = evlist__first(evlist), *pos = first;
1243c2a70653SArnaldo Carvalho de Melo 
1244e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry_continue(evlist, pos) {
12451fc632ceSJiri Olsa 		if (first->core.attr.sample_id_all != pos->core.attr.sample_id_all)
1246c2a70653SArnaldo Carvalho de Melo 			return false;
1247c2a70653SArnaldo Carvalho de Melo 	}
1248c2a70653SArnaldo Carvalho de Melo 
1249c2a70653SArnaldo Carvalho de Melo 	return true;
125074429964SFrederic Weisbecker }
125174429964SFrederic Weisbecker 
12528cedf3a5SArnaldo Carvalho de Melo bool evlist__sample_id_all(struct evlist *evlist)
125374429964SFrederic Weisbecker {
1254515dbe48SJiri Olsa 	struct evsel *first = evlist__first(evlist);
12551fc632ceSJiri Olsa 	return first->core.attr.sample_id_all;
125674429964SFrederic Weisbecker }
125781cce8deSArnaldo Carvalho de Melo 
1258900c8eadSArnaldo Carvalho de Melo void evlist__set_selected(struct evlist *evlist, struct evsel *evsel)
125981cce8deSArnaldo Carvalho de Melo {
126081cce8deSArnaldo Carvalho de Melo 	evlist->selected = evsel;
126181cce8deSArnaldo Carvalho de Melo }
1262727ab04eSArnaldo Carvalho de Melo 
1263750b4edeSJiri Olsa void evlist__close(struct evlist *evlist)
1264a74b4b66SNamhyung Kim {
126532dcd021SJiri Olsa 	struct evsel *evsel;
12667736627bSAndi Kleen 	struct affinity affinity;
12677736627bSAndi Kleen 	int cpu, i;
1268a74b4b66SNamhyung Kim 
12697736627bSAndi Kleen 	/*
12707736627bSAndi Kleen 	 * With perf record core.cpus is usually NULL.
12717736627bSAndi Kleen 	 * Use the old method to handle this for now.
12727736627bSAndi Kleen 	 */
12737736627bSAndi Kleen 	if (!evlist->core.cpus) {
1274475fb533SAndi Kleen 		evlist__for_each_entry_reverse(evlist, evsel)
127588761fa1SJiri Olsa 			evsel__close(evsel);
12767736627bSAndi Kleen 		return;
12777736627bSAndi Kleen 	}
12787736627bSAndi Kleen 
12797736627bSAndi Kleen 	if (affinity__setup(&affinity) < 0)
12807736627bSAndi Kleen 		return;
12817736627bSAndi Kleen 	evlist__for_each_cpu(evlist, i, cpu) {
12827736627bSAndi Kleen 		affinity__set(&affinity, cpu);
12837736627bSAndi Kleen 
12847736627bSAndi Kleen 		evlist__for_each_entry_reverse(evlist, evsel) {
12857736627bSAndi Kleen 			if (evsel__cpu_iter_skip(evsel, cpu))
12867736627bSAndi Kleen 			    continue;
12877736627bSAndi Kleen 			perf_evsel__close_cpu(&evsel->core, evsel->cpu_iter - 1);
12887736627bSAndi Kleen 		}
12897736627bSAndi Kleen 	}
12907736627bSAndi Kleen 	affinity__cleanup(&affinity);
12917736627bSAndi Kleen 	evlist__for_each_entry_reverse(evlist, evsel) {
12927736627bSAndi Kleen 		perf_evsel__free_fd(&evsel->core);
12937736627bSAndi Kleen 		perf_evsel__free_id(&evsel->core);
12947736627bSAndi Kleen 	}
1295513068f2SNamhyung Kim 	perf_evlist__reset_id_hash(&evlist->core);
1296a74b4b66SNamhyung Kim }
1297a74b4b66SNamhyung Kim 
12987748bb71SArnaldo Carvalho de Melo static int evlist__create_syswide_maps(struct evlist *evlist)
12994112eb18SArnaldo Carvalho de Melo {
1300f854839bSJiri Olsa 	struct perf_cpu_map *cpus;
13019749b90eSJiri Olsa 	struct perf_thread_map *threads;
13024112eb18SArnaldo Carvalho de Melo 	int err = -ENOMEM;
13034112eb18SArnaldo Carvalho de Melo 
13044112eb18SArnaldo Carvalho de Melo 	/*
13054112eb18SArnaldo Carvalho de Melo 	 * Try reading /sys/devices/system/cpu/online to get
13064112eb18SArnaldo Carvalho de Melo 	 * an all cpus map.
13074112eb18SArnaldo Carvalho de Melo 	 *
13084112eb18SArnaldo Carvalho de Melo 	 * FIXME: -ENOMEM is the best we can do here, the cpu_map
13094112eb18SArnaldo Carvalho de Melo 	 * code needs an overhaul to properly forward the
13104112eb18SArnaldo Carvalho de Melo 	 * error, and we may not want to do that fallback to a
13114112eb18SArnaldo Carvalho de Melo 	 * default cpu identity map :-\
13124112eb18SArnaldo Carvalho de Melo 	 */
13139c3516d1SJiri Olsa 	cpus = perf_cpu_map__new(NULL);
13148c0498b6SAdrian Hunter 	if (!cpus)
13154112eb18SArnaldo Carvalho de Melo 		goto out;
13164112eb18SArnaldo Carvalho de Melo 
13174b49cce2SJiri Olsa 	threads = perf_thread_map__new_dummy();
13188c0498b6SAdrian Hunter 	if (!threads)
13198c0498b6SAdrian Hunter 		goto out_put;
13204112eb18SArnaldo Carvalho de Melo 
1321453fa030SJiri Olsa 	perf_evlist__set_maps(&evlist->core, cpus, threads);
1322bfd1b83dSNamhyung Kim 
1323bfd1b83dSNamhyung Kim 	perf_thread_map__put(threads);
13248c0498b6SAdrian Hunter out_put:
132538f01d8dSJiri Olsa 	perf_cpu_map__put(cpus);
1326bfd1b83dSNamhyung Kim out:
1327bfd1b83dSNamhyung Kim 	return err;
13284112eb18SArnaldo Carvalho de Melo }
13294112eb18SArnaldo Carvalho de Melo 
1330474ddc4cSJiri Olsa int evlist__open(struct evlist *evlist)
1331727ab04eSArnaldo Carvalho de Melo {
133232dcd021SJiri Olsa 	struct evsel *evsel;
1333a74b4b66SNamhyung Kim 	int err;
1334727ab04eSArnaldo Carvalho de Melo 
13354112eb18SArnaldo Carvalho de Melo 	/*
13364112eb18SArnaldo Carvalho de Melo 	 * Default: one fd per CPU, all threads, aka systemwide
13374112eb18SArnaldo Carvalho de Melo 	 * as sys_perf_event_open(cpu = -1, thread = -1) is EINVAL
13384112eb18SArnaldo Carvalho de Melo 	 */
133903617c22SJiri Olsa 	if (evlist->core.threads == NULL && evlist->core.cpus == NULL) {
13407748bb71SArnaldo Carvalho de Melo 		err = evlist__create_syswide_maps(evlist);
13414112eb18SArnaldo Carvalho de Melo 		if (err < 0)
13424112eb18SArnaldo Carvalho de Melo 			goto out_err;
13434112eb18SArnaldo Carvalho de Melo 	}
13444112eb18SArnaldo Carvalho de Melo 
13450a7e7ec9SArnaldo Carvalho de Melo 	evlist__update_id_pos(evlist);
1346733cd2feSAdrian Hunter 
1347e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, evsel) {
1348af663bd0SJiri Olsa 		err = evsel__open(evsel, evsel->core.cpus, evsel->core.threads);
1349727ab04eSArnaldo Carvalho de Melo 		if (err < 0)
1350727ab04eSArnaldo Carvalho de Melo 			goto out_err;
1351727ab04eSArnaldo Carvalho de Melo 	}
1352727ab04eSArnaldo Carvalho de Melo 
1353727ab04eSArnaldo Carvalho de Melo 	return 0;
1354727ab04eSArnaldo Carvalho de Melo out_err:
1355750b4edeSJiri Olsa 	evlist__close(evlist);
135641c21a68SNamhyung Kim 	errno = -err;
1357727ab04eSArnaldo Carvalho de Melo 	return err;
1358727ab04eSArnaldo Carvalho de Melo }
135935b9d88eSArnaldo Carvalho de Melo 
13607b392ef0SArnaldo Carvalho de Melo int evlist__prepare_workload(struct evlist *evlist, struct target *target, const char *argv[],
13617b392ef0SArnaldo Carvalho de Melo 			     bool pipe_output, void (*exec_error)(int signo, siginfo_t *info, void *ucontext))
136235b9d88eSArnaldo Carvalho de Melo {
136335b9d88eSArnaldo Carvalho de Melo 	int child_ready_pipe[2], go_pipe[2];
136435b9d88eSArnaldo Carvalho de Melo 	char bf;
136535b9d88eSArnaldo Carvalho de Melo 
136635b9d88eSArnaldo Carvalho de Melo 	if (pipe(child_ready_pipe) < 0) {
136735b9d88eSArnaldo Carvalho de Melo 		perror("failed to create 'ready' pipe");
136835b9d88eSArnaldo Carvalho de Melo 		return -1;
136935b9d88eSArnaldo Carvalho de Melo 	}
137035b9d88eSArnaldo Carvalho de Melo 
137135b9d88eSArnaldo Carvalho de Melo 	if (pipe(go_pipe) < 0) {
137235b9d88eSArnaldo Carvalho de Melo 		perror("failed to create 'go' pipe");
137335b9d88eSArnaldo Carvalho de Melo 		goto out_close_ready_pipe;
137435b9d88eSArnaldo Carvalho de Melo 	}
137535b9d88eSArnaldo Carvalho de Melo 
137635b9d88eSArnaldo Carvalho de Melo 	evlist->workload.pid = fork();
137735b9d88eSArnaldo Carvalho de Melo 	if (evlist->workload.pid < 0) {
137835b9d88eSArnaldo Carvalho de Melo 		perror("failed to fork");
137935b9d88eSArnaldo Carvalho de Melo 		goto out_close_pipes;
138035b9d88eSArnaldo Carvalho de Melo 	}
138135b9d88eSArnaldo Carvalho de Melo 
138235b9d88eSArnaldo Carvalho de Melo 	if (!evlist->workload.pid) {
13835f1c4225SArnaldo Carvalho de Melo 		int ret;
13845f1c4225SArnaldo Carvalho de Melo 
1385119fa3c9SNamhyung Kim 		if (pipe_output)
138635b9d88eSArnaldo Carvalho de Melo 			dup2(2, 1);
138735b9d88eSArnaldo Carvalho de Melo 
13880817df08SDavid Ahern 		signal(SIGTERM, SIG_DFL);
13890817df08SDavid Ahern 
139035b9d88eSArnaldo Carvalho de Melo 		close(child_ready_pipe[0]);
139135b9d88eSArnaldo Carvalho de Melo 		close(go_pipe[1]);
139235b9d88eSArnaldo Carvalho de Melo 		fcntl(go_pipe[0], F_SETFD, FD_CLOEXEC);
139335b9d88eSArnaldo Carvalho de Melo 
139435b9d88eSArnaldo Carvalho de Melo 		/*
1395a7672d1dSArnaldo Carvalho de Melo 		 * Change the name of this process not to confuse --exclude-perf users
1396a7672d1dSArnaldo Carvalho de Melo 		 * that sees 'perf' in the window up to the execvp() and thinks that
1397a7672d1dSArnaldo Carvalho de Melo 		 * perf samples are not being excluded.
1398a7672d1dSArnaldo Carvalho de Melo 		 */
1399a7672d1dSArnaldo Carvalho de Melo 		prctl(PR_SET_NAME, "perf-exec");
1400a7672d1dSArnaldo Carvalho de Melo 
1401a7672d1dSArnaldo Carvalho de Melo 		/*
140235b9d88eSArnaldo Carvalho de Melo 		 * Tell the parent we're ready to go
140335b9d88eSArnaldo Carvalho de Melo 		 */
140435b9d88eSArnaldo Carvalho de Melo 		close(child_ready_pipe[1]);
140535b9d88eSArnaldo Carvalho de Melo 
140635b9d88eSArnaldo Carvalho de Melo 		/*
140735b9d88eSArnaldo Carvalho de Melo 		 * Wait until the parent tells us to go.
140835b9d88eSArnaldo Carvalho de Melo 		 */
14095f1c4225SArnaldo Carvalho de Melo 		ret = read(go_pipe[0], &bf, 1);
14105f1c4225SArnaldo Carvalho de Melo 		/*
14115f1c4225SArnaldo Carvalho de Melo 		 * The parent will ask for the execvp() to be performed by
14125f1c4225SArnaldo Carvalho de Melo 		 * writing exactly one byte, in workload.cork_fd, usually via
14137b392ef0SArnaldo Carvalho de Melo 		 * evlist__start_workload().
14145f1c4225SArnaldo Carvalho de Melo 		 *
141520f86fc1SArnaldo Carvalho de Melo 		 * For cancelling the workload without actually running it,
14165f1c4225SArnaldo Carvalho de Melo 		 * the parent will just close workload.cork_fd, without writing
14175f1c4225SArnaldo Carvalho de Melo 		 * anything, i.e. read will return zero and we just exit()
14185f1c4225SArnaldo Carvalho de Melo 		 * here.
14195f1c4225SArnaldo Carvalho de Melo 		 */
14205f1c4225SArnaldo Carvalho de Melo 		if (ret != 1) {
14215f1c4225SArnaldo Carvalho de Melo 			if (ret == -1)
142235b9d88eSArnaldo Carvalho de Melo 				perror("unable to read pipe");
14235f1c4225SArnaldo Carvalho de Melo 			exit(ret);
14245f1c4225SArnaldo Carvalho de Melo 		}
142535b9d88eSArnaldo Carvalho de Melo 
142635b9d88eSArnaldo Carvalho de Melo 		execvp(argv[0], (char **)argv);
142735b9d88eSArnaldo Carvalho de Melo 
1428735f7e0bSArnaldo Carvalho de Melo 		if (exec_error) {
1429f33cbe72SArnaldo Carvalho de Melo 			union sigval val;
1430f33cbe72SArnaldo Carvalho de Melo 
1431f33cbe72SArnaldo Carvalho de Melo 			val.sival_int = errno;
1432f33cbe72SArnaldo Carvalho de Melo 			if (sigqueue(getppid(), SIGUSR1, val))
143335b9d88eSArnaldo Carvalho de Melo 				perror(argv[0]);
1434f33cbe72SArnaldo Carvalho de Melo 		} else
1435f33cbe72SArnaldo Carvalho de Melo 			perror(argv[0]);
143635b9d88eSArnaldo Carvalho de Melo 		exit(-1);
143735b9d88eSArnaldo Carvalho de Melo 	}
143835b9d88eSArnaldo Carvalho de Melo 
1439735f7e0bSArnaldo Carvalho de Melo 	if (exec_error) {
1440735f7e0bSArnaldo Carvalho de Melo 		struct sigaction act = {
1441735f7e0bSArnaldo Carvalho de Melo 			.sa_flags     = SA_SIGINFO,
1442735f7e0bSArnaldo Carvalho de Melo 			.sa_sigaction = exec_error,
1443735f7e0bSArnaldo Carvalho de Melo 		};
1444735f7e0bSArnaldo Carvalho de Melo 		sigaction(SIGUSR1, &act, NULL);
1445735f7e0bSArnaldo Carvalho de Melo 	}
1446735f7e0bSArnaldo Carvalho de Melo 
14471aaf63b1SArnaldo Carvalho de Melo 	if (target__none(target)) {
144803617c22SJiri Olsa 		if (evlist->core.threads == NULL) {
14491aaf63b1SArnaldo Carvalho de Melo 			fprintf(stderr, "FATAL: evlist->threads need to be set at this point (%s:%d).\n",
14501aaf63b1SArnaldo Carvalho de Melo 				__func__, __LINE__);
14511aaf63b1SArnaldo Carvalho de Melo 			goto out_close_pipes;
14521aaf63b1SArnaldo Carvalho de Melo 		}
145303617c22SJiri Olsa 		perf_thread_map__set_pid(evlist->core.threads, 0, evlist->workload.pid);
14541aaf63b1SArnaldo Carvalho de Melo 	}
145535b9d88eSArnaldo Carvalho de Melo 
145635b9d88eSArnaldo Carvalho de Melo 	close(child_ready_pipe[1]);
145735b9d88eSArnaldo Carvalho de Melo 	close(go_pipe[0]);
145835b9d88eSArnaldo Carvalho de Melo 	/*
145935b9d88eSArnaldo Carvalho de Melo 	 * wait for child to settle
146035b9d88eSArnaldo Carvalho de Melo 	 */
146135b9d88eSArnaldo Carvalho de Melo 	if (read(child_ready_pipe[0], &bf, 1) == -1) {
146235b9d88eSArnaldo Carvalho de Melo 		perror("unable to read pipe");
146335b9d88eSArnaldo Carvalho de Melo 		goto out_close_pipes;
146435b9d88eSArnaldo Carvalho de Melo 	}
146535b9d88eSArnaldo Carvalho de Melo 
1466bcf3145fSNamhyung Kim 	fcntl(go_pipe[1], F_SETFD, FD_CLOEXEC);
146735b9d88eSArnaldo Carvalho de Melo 	evlist->workload.cork_fd = go_pipe[1];
146835b9d88eSArnaldo Carvalho de Melo 	close(child_ready_pipe[0]);
146935b9d88eSArnaldo Carvalho de Melo 	return 0;
147035b9d88eSArnaldo Carvalho de Melo 
147135b9d88eSArnaldo Carvalho de Melo out_close_pipes:
147235b9d88eSArnaldo Carvalho de Melo 	close(go_pipe[0]);
147335b9d88eSArnaldo Carvalho de Melo 	close(go_pipe[1]);
147435b9d88eSArnaldo Carvalho de Melo out_close_ready_pipe:
147535b9d88eSArnaldo Carvalho de Melo 	close(child_ready_pipe[0]);
147635b9d88eSArnaldo Carvalho de Melo 	close(child_ready_pipe[1]);
147735b9d88eSArnaldo Carvalho de Melo 	return -1;
147835b9d88eSArnaldo Carvalho de Melo }
147935b9d88eSArnaldo Carvalho de Melo 
14807b392ef0SArnaldo Carvalho de Melo int evlist__start_workload(struct evlist *evlist)
148135b9d88eSArnaldo Carvalho de Melo {
148235b9d88eSArnaldo Carvalho de Melo 	if (evlist->workload.cork_fd > 0) {
1483b3824404SDavid Ahern 		char bf = 0;
1484bcf3145fSNamhyung Kim 		int ret;
148535b9d88eSArnaldo Carvalho de Melo 		/*
148635b9d88eSArnaldo Carvalho de Melo 		 * Remove the cork, let it rip!
148735b9d88eSArnaldo Carvalho de Melo 		 */
1488bcf3145fSNamhyung Kim 		ret = write(evlist->workload.cork_fd, &bf, 1);
1489bcf3145fSNamhyung Kim 		if (ret < 0)
1490e978be9eSSoramichi Akiyama 			perror("unable to write to pipe");
1491bcf3145fSNamhyung Kim 
1492bcf3145fSNamhyung Kim 		close(evlist->workload.cork_fd);
1493bcf3145fSNamhyung Kim 		return ret;
149435b9d88eSArnaldo Carvalho de Melo 	}
149535b9d88eSArnaldo Carvalho de Melo 
149635b9d88eSArnaldo Carvalho de Melo 	return 0;
149735b9d88eSArnaldo Carvalho de Melo }
1498cb0b29e0SArnaldo Carvalho de Melo 
14992a6599cdSArnaldo Carvalho de Melo int evlist__parse_sample(struct evlist *evlist, union perf_event *event, struct perf_sample *sample)
1500cb0b29e0SArnaldo Carvalho de Melo {
15013ccf8a7bSArnaldo Carvalho de Melo 	struct evsel *evsel = evlist__event2evsel(evlist, event);
150275562573SAdrian Hunter 
150375562573SAdrian Hunter 	if (!evsel)
150475562573SAdrian Hunter 		return -EFAULT;
15056b6017a2SArnaldo Carvalho de Melo 	return evsel__parse_sample(evsel, event, sample);
1506cb0b29e0SArnaldo Carvalho de Melo }
150778f067b3SArnaldo Carvalho de Melo 
15082a6599cdSArnaldo Carvalho de Melo int evlist__parse_sample_timestamp(struct evlist *evlist, union perf_event *event, u64 *timestamp)
150901468120SJiri Olsa {
15103ccf8a7bSArnaldo Carvalho de Melo 	struct evsel *evsel = evlist__event2evsel(evlist, event);
151101468120SJiri Olsa 
151201468120SJiri Olsa 	if (!evsel)
151301468120SJiri Olsa 		return -EFAULT;
15146b6017a2SArnaldo Carvalho de Melo 	return evsel__parse_sample_timestamp(evsel, event, timestamp);
151501468120SJiri Olsa }
151601468120SJiri Olsa 
1517d1f249ecSArnaldo Carvalho de Melo int evlist__strerror_open(struct evlist *evlist, int err, char *buf, size_t size)
1518a8f23d8fSArnaldo Carvalho de Melo {
1519a8f23d8fSArnaldo Carvalho de Melo 	int printed, value;
1520c8b5f2c9SArnaldo Carvalho de Melo 	char sbuf[STRERR_BUFSIZE], *emsg = str_error_r(err, sbuf, sizeof(sbuf));
1521a8f23d8fSArnaldo Carvalho de Melo 
1522a8f23d8fSArnaldo Carvalho de Melo 	switch (err) {
1523a8f23d8fSArnaldo Carvalho de Melo 	case EACCES:
1524a8f23d8fSArnaldo Carvalho de Melo 	case EPERM:
1525a8f23d8fSArnaldo Carvalho de Melo 		printed = scnprintf(buf, size,
1526a8f23d8fSArnaldo Carvalho de Melo 				    "Error:\t%s.\n"
1527a8f23d8fSArnaldo Carvalho de Melo 				    "Hint:\tCheck /proc/sys/kernel/perf_event_paranoid setting.", emsg);
1528a8f23d8fSArnaldo Carvalho de Melo 
15291a47245dSAdrian Hunter 		value = perf_event_paranoid();
1530a8f23d8fSArnaldo Carvalho de Melo 
1531a8f23d8fSArnaldo Carvalho de Melo 		printed += scnprintf(buf + printed, size - printed, "\nHint:\t");
1532a8f23d8fSArnaldo Carvalho de Melo 
1533a8f23d8fSArnaldo Carvalho de Melo 		if (value >= 2) {
1534a8f23d8fSArnaldo Carvalho de Melo 			printed += scnprintf(buf + printed, size - printed,
1535a8f23d8fSArnaldo Carvalho de Melo 					     "For your workloads it needs to be <= 1\nHint:\t");
1536a8f23d8fSArnaldo Carvalho de Melo 		}
1537a8f23d8fSArnaldo Carvalho de Melo 		printed += scnprintf(buf + printed, size - printed,
15385229e366SArnaldo Carvalho de Melo 				     "For system wide tracing it needs to be set to -1.\n");
1539a8f23d8fSArnaldo Carvalho de Melo 
1540a8f23d8fSArnaldo Carvalho de Melo 		printed += scnprintf(buf + printed, size - printed,
15415229e366SArnaldo Carvalho de Melo 				    "Hint:\tTry: 'sudo sh -c \"echo -1 > /proc/sys/kernel/perf_event_paranoid\"'\n"
15425229e366SArnaldo Carvalho de Melo 				    "Hint:\tThe current value is %d.", value);
1543a8f23d8fSArnaldo Carvalho de Melo 		break;
1544d9aade7fSArnaldo Carvalho de Melo 	case EINVAL: {
1545515dbe48SJiri Olsa 		struct evsel *first = evlist__first(evlist);
1546d9aade7fSArnaldo Carvalho de Melo 		int max_freq;
1547d9aade7fSArnaldo Carvalho de Melo 
1548d9aade7fSArnaldo Carvalho de Melo 		if (sysctl__read_int("kernel/perf_event_max_sample_rate", &max_freq) < 0)
1549d9aade7fSArnaldo Carvalho de Melo 			goto out_default;
1550d9aade7fSArnaldo Carvalho de Melo 
15511fc632ceSJiri Olsa 		if (first->core.attr.sample_freq < (u64)max_freq)
1552d9aade7fSArnaldo Carvalho de Melo 			goto out_default;
1553d9aade7fSArnaldo Carvalho de Melo 
1554d9aade7fSArnaldo Carvalho de Melo 		printed = scnprintf(buf, size,
1555d9aade7fSArnaldo Carvalho de Melo 				    "Error:\t%s.\n"
1556d9aade7fSArnaldo Carvalho de Melo 				    "Hint:\tCheck /proc/sys/kernel/perf_event_max_sample_rate.\n"
1557d9aade7fSArnaldo Carvalho de Melo 				    "Hint:\tThe current value is %d and %" PRIu64 " is being requested.",
15581fc632ceSJiri Olsa 				    emsg, max_freq, first->core.attr.sample_freq);
1559d9aade7fSArnaldo Carvalho de Melo 		break;
1560d9aade7fSArnaldo Carvalho de Melo 	}
1561a8f23d8fSArnaldo Carvalho de Melo 	default:
1562d9aade7fSArnaldo Carvalho de Melo out_default:
1563a8f23d8fSArnaldo Carvalho de Melo 		scnprintf(buf, size, "%s", emsg);
1564a8f23d8fSArnaldo Carvalho de Melo 		break;
1565a8f23d8fSArnaldo Carvalho de Melo 	}
1566a8f23d8fSArnaldo Carvalho de Melo 
1567a8f23d8fSArnaldo Carvalho de Melo 	return 0;
1568a8f23d8fSArnaldo Carvalho de Melo }
1569a025e4f0SAdrian Hunter 
1570d1f249ecSArnaldo Carvalho de Melo int evlist__strerror_mmap(struct evlist *evlist, int err, char *buf, size_t size)
1571956fa571SArnaldo Carvalho de Melo {
1572c8b5f2c9SArnaldo Carvalho de Melo 	char sbuf[STRERR_BUFSIZE], *emsg = str_error_r(err, sbuf, sizeof(sbuf));
1573f6fa4375SJiri Olsa 	int pages_attempted = evlist->core.mmap_len / 1024, pages_max_per_user, printed = 0;
1574956fa571SArnaldo Carvalho de Melo 
1575956fa571SArnaldo Carvalho de Melo 	switch (err) {
1576956fa571SArnaldo Carvalho de Melo 	case EPERM:
1577e5d4a290SArnaldo Carvalho de Melo 		sysctl__read_int("kernel/perf_event_mlock_kb", &pages_max_per_user);
1578e965bea1SArnaldo Carvalho de Melo 		printed += scnprintf(buf + printed, size - printed,
1579e965bea1SArnaldo Carvalho de Melo 				     "Error:\t%s.\n"
1580956fa571SArnaldo Carvalho de Melo 				     "Hint:\tCheck /proc/sys/kernel/perf_event_mlock_kb (%d kB) setting.\n"
1581e965bea1SArnaldo Carvalho de Melo 				     "Hint:\tTried using %zd kB.\n",
1582e5d4a290SArnaldo Carvalho de Melo 				     emsg, pages_max_per_user, pages_attempted);
1583e965bea1SArnaldo Carvalho de Melo 
1584e965bea1SArnaldo Carvalho de Melo 		if (pages_attempted >= pages_max_per_user) {
1585e965bea1SArnaldo Carvalho de Melo 			printed += scnprintf(buf + printed, size - printed,
1586e965bea1SArnaldo Carvalho de Melo 					     "Hint:\tTry 'sudo sh -c \"echo %d > /proc/sys/kernel/perf_event_mlock_kb\"', or\n",
1587e965bea1SArnaldo Carvalho de Melo 					     pages_max_per_user + pages_attempted);
1588e965bea1SArnaldo Carvalho de Melo 		}
1589e965bea1SArnaldo Carvalho de Melo 
1590e965bea1SArnaldo Carvalho de Melo 		printed += scnprintf(buf + printed, size - printed,
1591e965bea1SArnaldo Carvalho de Melo 				     "Hint:\tTry using a smaller -m/--mmap-pages value.");
1592956fa571SArnaldo Carvalho de Melo 		break;
1593956fa571SArnaldo Carvalho de Melo 	default:
1594956fa571SArnaldo Carvalho de Melo 		scnprintf(buf, size, "%s", emsg);
1595956fa571SArnaldo Carvalho de Melo 		break;
1596956fa571SArnaldo Carvalho de Melo 	}
1597956fa571SArnaldo Carvalho de Melo 
1598956fa571SArnaldo Carvalho de Melo 	return 0;
1599956fa571SArnaldo Carvalho de Melo }
1600956fa571SArnaldo Carvalho de Melo 
1601e414fd1aSArnaldo Carvalho de Melo void evlist__to_front(struct evlist *evlist, struct evsel *move_evsel)
1602a025e4f0SAdrian Hunter {
160332dcd021SJiri Olsa 	struct evsel *evsel, *n;
1604a025e4f0SAdrian Hunter 	LIST_HEAD(move);
1605a025e4f0SAdrian Hunter 
1606515dbe48SJiri Olsa 	if (move_evsel == evlist__first(evlist))
1607a025e4f0SAdrian Hunter 		return;
1608a025e4f0SAdrian Hunter 
1609e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry_safe(evlist, n, evsel) {
1610fba7c866SJiri Olsa 		if (evsel__leader(evsel) == evsel__leader(move_evsel))
1611b27c4eceSJiri Olsa 			list_move_tail(&evsel->core.node, &move);
1612a025e4f0SAdrian Hunter 	}
1613a025e4f0SAdrian Hunter 
1614ce9036a6SJiri Olsa 	list_splice(&move, &evlist->core.entries);
1615a025e4f0SAdrian Hunter }
161660b0896cSAdrian Hunter 
1617e80db255SArnaldo Carvalho de Melo struct evsel *evlist__get_tracking_event(struct evlist *evlist)
1618442ad225SAdrian Hunter {
1619442ad225SAdrian Hunter 	struct evsel *evsel;
1620442ad225SAdrian Hunter 
1621442ad225SAdrian Hunter 	evlist__for_each_entry(evlist, evsel) {
1622442ad225SAdrian Hunter 		if (evsel->tracking)
1623442ad225SAdrian Hunter 			return evsel;
1624442ad225SAdrian Hunter 	}
1625442ad225SAdrian Hunter 
1626442ad225SAdrian Hunter 	return evlist__first(evlist);
1627442ad225SAdrian Hunter }
1628442ad225SAdrian Hunter 
1629e80db255SArnaldo Carvalho de Melo void evlist__set_tracking_event(struct evlist *evlist, struct evsel *tracking_evsel)
163060b0896cSAdrian Hunter {
163132dcd021SJiri Olsa 	struct evsel *evsel;
163260b0896cSAdrian Hunter 
163360b0896cSAdrian Hunter 	if (tracking_evsel->tracking)
163460b0896cSAdrian Hunter 		return;
163560b0896cSAdrian Hunter 
1636e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, evsel) {
163760b0896cSAdrian Hunter 		if (evsel != tracking_evsel)
163860b0896cSAdrian Hunter 			evsel->tracking = false;
163960b0896cSAdrian Hunter 	}
164060b0896cSAdrian Hunter 
164160b0896cSAdrian Hunter 	tracking_evsel->tracking = true;
164260b0896cSAdrian Hunter }
16437630b3e2SWang Nan 
1644b02736f7SArnaldo Carvalho de Melo struct evsel *evlist__find_evsel_by_str(struct evlist *evlist, const char *str)
16457630b3e2SWang Nan {
164632dcd021SJiri Olsa 	struct evsel *evsel;
16477630b3e2SWang Nan 
1648e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, evsel) {
16497630b3e2SWang Nan 		if (!evsel->name)
16507630b3e2SWang Nan 			continue;
16517630b3e2SWang Nan 		if (strcmp(str, evsel->name) == 0)
16527630b3e2SWang Nan 			return evsel;
16537630b3e2SWang Nan 	}
16547630b3e2SWang Nan 
16557630b3e2SWang Nan 	return NULL;
16567630b3e2SWang Nan }
165754cc54deSWang Nan 
1658ade9d208SArnaldo Carvalho de Melo void evlist__toggle_bkw_mmap(struct evlist *evlist, enum bkw_mmap_state state)
165954cc54deSWang Nan {
166054cc54deSWang Nan 	enum bkw_mmap_state old_state = evlist->bkw_mmap_state;
166154cc54deSWang Nan 	enum action {
166254cc54deSWang Nan 		NONE,
166354cc54deSWang Nan 		PAUSE,
166454cc54deSWang Nan 		RESUME,
166554cc54deSWang Nan 	} action = NONE;
166654cc54deSWang Nan 
16670b72d69aSWang Nan 	if (!evlist->overwrite_mmap)
166854cc54deSWang Nan 		return;
166954cc54deSWang Nan 
167054cc54deSWang Nan 	switch (old_state) {
167154cc54deSWang Nan 	case BKW_MMAP_NOTREADY: {
167254cc54deSWang Nan 		if (state != BKW_MMAP_RUNNING)
1673dd8bd53aSLuis de Bethencourt 			goto state_err;
167454cc54deSWang Nan 		break;
167554cc54deSWang Nan 	}
167654cc54deSWang Nan 	case BKW_MMAP_RUNNING: {
167754cc54deSWang Nan 		if (state != BKW_MMAP_DATA_PENDING)
167854cc54deSWang Nan 			goto state_err;
167954cc54deSWang Nan 		action = PAUSE;
168054cc54deSWang Nan 		break;
168154cc54deSWang Nan 	}
168254cc54deSWang Nan 	case BKW_MMAP_DATA_PENDING: {
168354cc54deSWang Nan 		if (state != BKW_MMAP_EMPTY)
168454cc54deSWang Nan 			goto state_err;
168554cc54deSWang Nan 		break;
168654cc54deSWang Nan 	}
168754cc54deSWang Nan 	case BKW_MMAP_EMPTY: {
168854cc54deSWang Nan 		if (state != BKW_MMAP_RUNNING)
168954cc54deSWang Nan 			goto state_err;
169054cc54deSWang Nan 		action = RESUME;
169154cc54deSWang Nan 		break;
169254cc54deSWang Nan 	}
169354cc54deSWang Nan 	default:
169454cc54deSWang Nan 		WARN_ONCE(1, "Shouldn't get there\n");
169554cc54deSWang Nan 	}
169654cc54deSWang Nan 
169754cc54deSWang Nan 	evlist->bkw_mmap_state = state;
169854cc54deSWang Nan 
169954cc54deSWang Nan 	switch (action) {
170054cc54deSWang Nan 	case PAUSE:
17010a60b339SArnaldo Carvalho de Melo 		evlist__pause(evlist);
170254cc54deSWang Nan 		break;
170354cc54deSWang Nan 	case RESUME:
17040a60b339SArnaldo Carvalho de Melo 		evlist__resume(evlist);
170554cc54deSWang Nan 		break;
170654cc54deSWang Nan 	case NONE:
170754cc54deSWang Nan 	default:
170854cc54deSWang Nan 		break;
170954cc54deSWang Nan 	}
171054cc54deSWang Nan 
171154cc54deSWang Nan state_err:
171254cc54deSWang Nan 	return;
171354cc54deSWang Nan }
171407d6f446SArnaldo Carvalho de Melo 
171578e1bc25SArnaldo Carvalho de Melo bool evlist__exclude_kernel(struct evlist *evlist)
171607d6f446SArnaldo Carvalho de Melo {
171732dcd021SJiri Olsa 	struct evsel *evsel;
171807d6f446SArnaldo Carvalho de Melo 
171907d6f446SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, evsel) {
17201fc632ceSJiri Olsa 		if (!evsel->core.attr.exclude_kernel)
172107d6f446SArnaldo Carvalho de Melo 			return false;
172207d6f446SArnaldo Carvalho de Melo 	}
172307d6f446SArnaldo Carvalho de Melo 
172407d6f446SArnaldo Carvalho de Melo 	return true;
172507d6f446SArnaldo Carvalho de Melo }
1726e2bdbe80SJin Yao 
1727e2bdbe80SJin Yao /*
1728e2bdbe80SJin Yao  * Events in data file are not collect in groups, but we still want
1729e2bdbe80SJin Yao  * the group display. Set the artificial group and set the leader's
1730e2bdbe80SJin Yao  * forced_leader flag to notify the display code.
1731e2bdbe80SJin Yao  */
173264b4778bSArnaldo Carvalho de Melo void evlist__force_leader(struct evlist *evlist)
1733e2bdbe80SJin Yao {
17343a683120SJiri Olsa 	if (!evlist->core.nr_groups) {
1735515dbe48SJiri Olsa 		struct evsel *leader = evlist__first(evlist);
1736e2bdbe80SJin Yao 
1737a622eafaSArnaldo Carvalho de Melo 		evlist__set_leader(evlist);
1738e2bdbe80SJin Yao 		leader->forced_leader = true;
1739e2bdbe80SJin Yao 	}
1740e2bdbe80SJin Yao }
1741c3537fc2SAndi Kleen 
174264b4778bSArnaldo Carvalho de Melo struct evsel *evlist__reset_weak_group(struct evlist *evsel_list, struct evsel *evsel, bool close)
1743c3537fc2SAndi Kleen {
174432dcd021SJiri Olsa 	struct evsel *c2, *leader;
1745c3537fc2SAndi Kleen 	bool is_open = true;
1746c3537fc2SAndi Kleen 
1747fba7c866SJiri Olsa 	leader = evsel__leader(evsel);
1748fba7c866SJiri Olsa 
1749c3537fc2SAndi Kleen 	pr_debug("Weak group for %s/%d failed\n",
17505643b1a5SJiri Olsa 			leader->name, leader->core.nr_members);
1751c3537fc2SAndi Kleen 
1752c3537fc2SAndi Kleen 	/*
1753c3537fc2SAndi Kleen 	 * for_each_group_member doesn't work here because it doesn't
1754c3537fc2SAndi Kleen 	 * include the first entry.
1755c3537fc2SAndi Kleen 	 */
1756c3537fc2SAndi Kleen 	evlist__for_each_entry(evsel_list, c2) {
1757c3537fc2SAndi Kleen 		if (c2 == evsel)
1758c3537fc2SAndi Kleen 			is_open = false;
1759fba7c866SJiri Olsa 		if (evsel__has_leader(c2, leader)) {
17604804e011SAndi Kleen 			if (is_open && close)
17615a40e199SAndi Kleen 				perf_evsel__close(&c2->core);
1762fba7c866SJiri Olsa 			evsel__set_leader(c2, c2);
17635643b1a5SJiri Olsa 			c2->core.nr_members = 0;
17644804e011SAndi Kleen 			/*
17654804e011SAndi Kleen 			 * Set this for all former members of the group
17664804e011SAndi Kleen 			 * to indicate they get reopened.
17674804e011SAndi Kleen 			 */
17684804e011SAndi Kleen 			c2->reset_group = true;
1769c3537fc2SAndi Kleen 		}
1770c3537fc2SAndi Kleen 	}
1771c3537fc2SAndi Kleen 	return leader;
1772c3537fc2SAndi Kleen }
1773ec886bf5SAlexey Budankov 
1774a8fcbd26SAdrian Hunter static int evlist__parse_control_fifo(const char *str, int *ctl_fd, int *ctl_fd_ack, bool *ctl_fd_close)
1775a8fcbd26SAdrian Hunter {
1776a8fcbd26SAdrian Hunter 	char *s, *p;
1777a8fcbd26SAdrian Hunter 	int ret = 0, fd;
1778a8fcbd26SAdrian Hunter 
1779a8fcbd26SAdrian Hunter 	if (strncmp(str, "fifo:", 5))
1780a8fcbd26SAdrian Hunter 		return -EINVAL;
1781a8fcbd26SAdrian Hunter 
1782a8fcbd26SAdrian Hunter 	str += 5;
1783a8fcbd26SAdrian Hunter 	if (!*str || *str == ',')
1784a8fcbd26SAdrian Hunter 		return -EINVAL;
1785a8fcbd26SAdrian Hunter 
1786a8fcbd26SAdrian Hunter 	s = strdup(str);
1787a8fcbd26SAdrian Hunter 	if (!s)
1788a8fcbd26SAdrian Hunter 		return -ENOMEM;
1789a8fcbd26SAdrian Hunter 
1790a8fcbd26SAdrian Hunter 	p = strchr(s, ',');
1791a8fcbd26SAdrian Hunter 	if (p)
1792a8fcbd26SAdrian Hunter 		*p = '\0';
1793a8fcbd26SAdrian Hunter 
1794a8fcbd26SAdrian Hunter 	/*
1795a8fcbd26SAdrian Hunter 	 * O_RDWR avoids POLLHUPs which is necessary to allow the other
1796a8fcbd26SAdrian Hunter 	 * end of a FIFO to be repeatedly opened and closed.
1797a8fcbd26SAdrian Hunter 	 */
1798a8fcbd26SAdrian Hunter 	fd = open(s, O_RDWR | O_NONBLOCK | O_CLOEXEC);
1799a8fcbd26SAdrian Hunter 	if (fd < 0) {
1800a8fcbd26SAdrian Hunter 		pr_err("Failed to open '%s'\n", s);
1801a8fcbd26SAdrian Hunter 		ret = -errno;
1802a8fcbd26SAdrian Hunter 		goto out_free;
1803a8fcbd26SAdrian Hunter 	}
1804a8fcbd26SAdrian Hunter 	*ctl_fd = fd;
1805a8fcbd26SAdrian Hunter 	*ctl_fd_close = true;
1806a8fcbd26SAdrian Hunter 
1807a8fcbd26SAdrian Hunter 	if (p && *++p) {
1808a8fcbd26SAdrian Hunter 		/* O_RDWR | O_NONBLOCK means the other end need not be open */
1809a8fcbd26SAdrian Hunter 		fd = open(p, O_RDWR | O_NONBLOCK | O_CLOEXEC);
1810a8fcbd26SAdrian Hunter 		if (fd < 0) {
1811a8fcbd26SAdrian Hunter 			pr_err("Failed to open '%s'\n", p);
1812a8fcbd26SAdrian Hunter 			ret = -errno;
1813a8fcbd26SAdrian Hunter 			goto out_free;
1814a8fcbd26SAdrian Hunter 		}
1815a8fcbd26SAdrian Hunter 		*ctl_fd_ack = fd;
1816a8fcbd26SAdrian Hunter 	}
1817a8fcbd26SAdrian Hunter 
1818a8fcbd26SAdrian Hunter out_free:
1819a8fcbd26SAdrian Hunter 	free(s);
1820a8fcbd26SAdrian Hunter 	return ret;
1821a8fcbd26SAdrian Hunter }
1822a8fcbd26SAdrian Hunter 
1823a8fcbd26SAdrian Hunter int evlist__parse_control(const char *str, int *ctl_fd, int *ctl_fd_ack, bool *ctl_fd_close)
18249864a66dSAdrian Hunter {
18259864a66dSAdrian Hunter 	char *comma = NULL, *endptr = NULL;
18269864a66dSAdrian Hunter 
1827a8fcbd26SAdrian Hunter 	*ctl_fd_close = false;
1828a8fcbd26SAdrian Hunter 
18299864a66dSAdrian Hunter 	if (strncmp(str, "fd:", 3))
1830a8fcbd26SAdrian Hunter 		return evlist__parse_control_fifo(str, ctl_fd, ctl_fd_ack, ctl_fd_close);
18319864a66dSAdrian Hunter 
18329864a66dSAdrian Hunter 	*ctl_fd = strtoul(&str[3], &endptr, 0);
18339864a66dSAdrian Hunter 	if (endptr == &str[3])
18349864a66dSAdrian Hunter 		return -EINVAL;
18359864a66dSAdrian Hunter 
18369864a66dSAdrian Hunter 	comma = strchr(str, ',');
18379864a66dSAdrian Hunter 	if (comma) {
18389864a66dSAdrian Hunter 		if (endptr != comma)
18399864a66dSAdrian Hunter 			return -EINVAL;
18409864a66dSAdrian Hunter 
18419864a66dSAdrian Hunter 		*ctl_fd_ack = strtoul(comma + 1, &endptr, 0);
18429864a66dSAdrian Hunter 		if (endptr == comma + 1 || *endptr != '\0')
18439864a66dSAdrian Hunter 			return -EINVAL;
18449864a66dSAdrian Hunter 	}
18459864a66dSAdrian Hunter 
18469864a66dSAdrian Hunter 	return 0;
18479864a66dSAdrian Hunter }
18489864a66dSAdrian Hunter 
1849ee7fe31eSAdrian Hunter void evlist__close_control(int ctl_fd, int ctl_fd_ack, bool *ctl_fd_close)
1850ee7fe31eSAdrian Hunter {
1851ee7fe31eSAdrian Hunter 	if (*ctl_fd_close) {
1852ee7fe31eSAdrian Hunter 		*ctl_fd_close = false;
1853ee7fe31eSAdrian Hunter 		close(ctl_fd);
1854ee7fe31eSAdrian Hunter 		if (ctl_fd_ack >= 0)
1855ee7fe31eSAdrian Hunter 			close(ctl_fd_ack);
1856ee7fe31eSAdrian Hunter 	}
1857ee7fe31eSAdrian Hunter }
1858ee7fe31eSAdrian Hunter 
1859ec886bf5SAlexey Budankov int evlist__initialize_ctlfd(struct evlist *evlist, int fd, int ack)
1860ec886bf5SAlexey Budankov {
1861ec886bf5SAlexey Budankov 	if (fd == -1) {
1862ec886bf5SAlexey Budankov 		pr_debug("Control descriptor is not initialized\n");
1863ec886bf5SAlexey Budankov 		return 0;
1864ec886bf5SAlexey Budankov 	}
1865ec886bf5SAlexey Budankov 
1866ec886bf5SAlexey Budankov 	evlist->ctl_fd.pos = perf_evlist__add_pollfd(&evlist->core, fd, NULL, POLLIN,
1867ec886bf5SAlexey Budankov 						     fdarray_flag__nonfilterable);
1868ec886bf5SAlexey Budankov 	if (evlist->ctl_fd.pos < 0) {
1869ec886bf5SAlexey Budankov 		evlist->ctl_fd.pos = -1;
1870ec886bf5SAlexey Budankov 		pr_err("Failed to add ctl fd entry: %m\n");
1871ec886bf5SAlexey Budankov 		return -1;
1872ec886bf5SAlexey Budankov 	}
1873ec886bf5SAlexey Budankov 
1874ec886bf5SAlexey Budankov 	evlist->ctl_fd.fd = fd;
1875ec886bf5SAlexey Budankov 	evlist->ctl_fd.ack = ack;
1876ec886bf5SAlexey Budankov 
1877ec886bf5SAlexey Budankov 	return 0;
1878ec886bf5SAlexey Budankov }
1879ec886bf5SAlexey Budankov 
1880ec886bf5SAlexey Budankov bool evlist__ctlfd_initialized(struct evlist *evlist)
1881ec886bf5SAlexey Budankov {
1882ec886bf5SAlexey Budankov 	return evlist->ctl_fd.pos >= 0;
1883ec886bf5SAlexey Budankov }
1884ec886bf5SAlexey Budankov 
1885ec886bf5SAlexey Budankov int evlist__finalize_ctlfd(struct evlist *evlist)
1886ec886bf5SAlexey Budankov {
1887ec886bf5SAlexey Budankov 	struct pollfd *entries = evlist->core.pollfd.entries;
1888ec886bf5SAlexey Budankov 
1889ec886bf5SAlexey Budankov 	if (!evlist__ctlfd_initialized(evlist))
1890ec886bf5SAlexey Budankov 		return 0;
1891ec886bf5SAlexey Budankov 
1892ec886bf5SAlexey Budankov 	entries[evlist->ctl_fd.pos].fd = -1;
1893ec886bf5SAlexey Budankov 	entries[evlist->ctl_fd.pos].events = 0;
1894ec886bf5SAlexey Budankov 	entries[evlist->ctl_fd.pos].revents = 0;
1895ec886bf5SAlexey Budankov 
1896ec886bf5SAlexey Budankov 	evlist->ctl_fd.pos = -1;
1897ec886bf5SAlexey Budankov 	evlist->ctl_fd.ack = -1;
1898ec886bf5SAlexey Budankov 	evlist->ctl_fd.fd = -1;
1899ec886bf5SAlexey Budankov 
1900ec886bf5SAlexey Budankov 	return 0;
1901ec886bf5SAlexey Budankov }
1902ec886bf5SAlexey Budankov 
1903ec886bf5SAlexey Budankov static int evlist__ctlfd_recv(struct evlist *evlist, enum evlist_ctl_cmd *cmd,
1904ec886bf5SAlexey Budankov 			      char *cmd_data, size_t data_size)
1905ec886bf5SAlexey Budankov {
1906ec886bf5SAlexey Budankov 	int err;
1907ec886bf5SAlexey Budankov 	char c;
1908ec886bf5SAlexey Budankov 	size_t bytes_read = 0;
1909ec886bf5SAlexey Budankov 
191040db8ff5SAdrian Hunter 	*cmd = EVLIST_CTL_CMD_UNSUPPORTED;
1911ec886bf5SAlexey Budankov 	memset(cmd_data, 0, data_size);
1912ec886bf5SAlexey Budankov 	data_size--;
1913ec886bf5SAlexey Budankov 
1914ec886bf5SAlexey Budankov 	do {
1915ec886bf5SAlexey Budankov 		err = read(evlist->ctl_fd.fd, &c, 1);
1916ec886bf5SAlexey Budankov 		if (err > 0) {
1917ec886bf5SAlexey Budankov 			if (c == '\n' || c == '\0')
1918ec886bf5SAlexey Budankov 				break;
1919ec886bf5SAlexey Budankov 			cmd_data[bytes_read++] = c;
1920ec886bf5SAlexey Budankov 			if (bytes_read == data_size)
1921ec886bf5SAlexey Budankov 				break;
192240db8ff5SAdrian Hunter 			continue;
192340db8ff5SAdrian Hunter 		} else if (err == -1) {
192440db8ff5SAdrian Hunter 			if (errno == EINTR)
192540db8ff5SAdrian Hunter 				continue;
192640db8ff5SAdrian Hunter 			if (errno == EAGAIN || errno == EWOULDBLOCK)
192740db8ff5SAdrian Hunter 				err = 0;
192840db8ff5SAdrian Hunter 			else
1929ec886bf5SAlexey Budankov 				pr_err("Failed to read from ctlfd %d: %m\n", evlist->ctl_fd.fd);
1930ec886bf5SAlexey Budankov 		}
193140db8ff5SAdrian Hunter 		break;
1932ec886bf5SAlexey Budankov 	} while (1);
1933ec886bf5SAlexey Budankov 
1934ec886bf5SAlexey Budankov 	pr_debug("Message from ctl_fd: \"%s%s\"\n", cmd_data,
1935ec886bf5SAlexey Budankov 		 bytes_read == data_size ? "" : c == '\n' ? "\\n" : "\\0");
1936ec886bf5SAlexey Budankov 
193740db8ff5SAdrian Hunter 	if (bytes_read > 0) {
1938ec886bf5SAlexey Budankov 		if (!strncmp(cmd_data, EVLIST_CTL_CMD_ENABLE_TAG,
1939ec886bf5SAlexey Budankov 			     (sizeof(EVLIST_CTL_CMD_ENABLE_TAG)-1))) {
1940ec886bf5SAlexey Budankov 			*cmd = EVLIST_CTL_CMD_ENABLE;
1941ec886bf5SAlexey Budankov 		} else if (!strncmp(cmd_data, EVLIST_CTL_CMD_DISABLE_TAG,
1942ec886bf5SAlexey Budankov 				    (sizeof(EVLIST_CTL_CMD_DISABLE_TAG)-1))) {
1943ec886bf5SAlexey Budankov 			*cmd = EVLIST_CTL_CMD_DISABLE;
1944d20aff15SAdrian Hunter 		} else if (!strncmp(cmd_data, EVLIST_CTL_CMD_SNAPSHOT_TAG,
1945d20aff15SAdrian Hunter 				    (sizeof(EVLIST_CTL_CMD_SNAPSHOT_TAG)-1))) {
1946d20aff15SAdrian Hunter 			*cmd = EVLIST_CTL_CMD_SNAPSHOT;
1947d20aff15SAdrian Hunter 			pr_debug("is snapshot\n");
1948142544a9SJiri Olsa 		} else if (!strncmp(cmd_data, EVLIST_CTL_CMD_EVLIST_TAG,
1949142544a9SJiri Olsa 				    (sizeof(EVLIST_CTL_CMD_EVLIST_TAG)-1))) {
1950142544a9SJiri Olsa 			*cmd = EVLIST_CTL_CMD_EVLIST;
1951f186cd61SJiri Olsa 		} else if (!strncmp(cmd_data, EVLIST_CTL_CMD_STOP_TAG,
1952f186cd61SJiri Olsa 				    (sizeof(EVLIST_CTL_CMD_STOP_TAG)-1))) {
1953f186cd61SJiri Olsa 			*cmd = EVLIST_CTL_CMD_STOP;
195447fddcb4SJiri Olsa 		} else if (!strncmp(cmd_data, EVLIST_CTL_CMD_PING_TAG,
195547fddcb4SJiri Olsa 				    (sizeof(EVLIST_CTL_CMD_PING_TAG)-1))) {
195647fddcb4SJiri Olsa 			*cmd = EVLIST_CTL_CMD_PING;
1957ec886bf5SAlexey Budankov 		}
1958ec886bf5SAlexey Budankov 	}
1959ec886bf5SAlexey Budankov 
196040db8ff5SAdrian Hunter 	return bytes_read ? (int)bytes_read : err;
1961ec886bf5SAlexey Budankov }
1962ec886bf5SAlexey Budankov 
1963d20aff15SAdrian Hunter int evlist__ctlfd_ack(struct evlist *evlist)
1964ec886bf5SAlexey Budankov {
1965ec886bf5SAlexey Budankov 	int err;
1966ec886bf5SAlexey Budankov 
1967ec886bf5SAlexey Budankov 	if (evlist->ctl_fd.ack == -1)
1968ec886bf5SAlexey Budankov 		return 0;
1969ec886bf5SAlexey Budankov 
1970ec886bf5SAlexey Budankov 	err = write(evlist->ctl_fd.ack, EVLIST_CTL_CMD_ACK_TAG,
1971ec886bf5SAlexey Budankov 		    sizeof(EVLIST_CTL_CMD_ACK_TAG));
1972ec886bf5SAlexey Budankov 	if (err == -1)
1973ec886bf5SAlexey Budankov 		pr_err("failed to write to ctl_ack_fd %d: %m\n", evlist->ctl_fd.ack);
1974ec886bf5SAlexey Budankov 
1975ec886bf5SAlexey Budankov 	return err;
1976ec886bf5SAlexey Budankov }
1977ec886bf5SAlexey Budankov 
1978991ae4ebSJiri Olsa static int get_cmd_arg(char *cmd_data, size_t cmd_size, char **arg)
1979991ae4ebSJiri Olsa {
1980991ae4ebSJiri Olsa 	char *data = cmd_data + cmd_size;
1981991ae4ebSJiri Olsa 
1982991ae4ebSJiri Olsa 	/* no argument */
1983991ae4ebSJiri Olsa 	if (!*data)
1984991ae4ebSJiri Olsa 		return 0;
1985991ae4ebSJiri Olsa 
1986991ae4ebSJiri Olsa 	/* there's argument */
1987991ae4ebSJiri Olsa 	if (*data == ' ') {
1988991ae4ebSJiri Olsa 		*arg = data + 1;
1989991ae4ebSJiri Olsa 		return 1;
1990991ae4ebSJiri Olsa 	}
1991991ae4ebSJiri Olsa 
1992991ae4ebSJiri Olsa 	/* malformed */
1993991ae4ebSJiri Olsa 	return -1;
1994991ae4ebSJiri Olsa }
1995991ae4ebSJiri Olsa 
1996991ae4ebSJiri Olsa static int evlist__ctlfd_enable(struct evlist *evlist, char *cmd_data, bool enable)
1997991ae4ebSJiri Olsa {
1998991ae4ebSJiri Olsa 	struct evsel *evsel;
1999991ae4ebSJiri Olsa 	char *name;
2000991ae4ebSJiri Olsa 	int err;
2001991ae4ebSJiri Olsa 
2002991ae4ebSJiri Olsa 	err = get_cmd_arg(cmd_data,
2003991ae4ebSJiri Olsa 			  enable ? sizeof(EVLIST_CTL_CMD_ENABLE_TAG) - 1 :
2004991ae4ebSJiri Olsa 				   sizeof(EVLIST_CTL_CMD_DISABLE_TAG) - 1,
2005991ae4ebSJiri Olsa 			  &name);
2006991ae4ebSJiri Olsa 	if (err < 0) {
2007991ae4ebSJiri Olsa 		pr_info("failed: wrong command\n");
2008991ae4ebSJiri Olsa 		return -1;
2009991ae4ebSJiri Olsa 	}
2010991ae4ebSJiri Olsa 
2011991ae4ebSJiri Olsa 	if (err) {
2012991ae4ebSJiri Olsa 		evsel = evlist__find_evsel_by_str(evlist, name);
2013991ae4ebSJiri Olsa 		if (evsel) {
2014991ae4ebSJiri Olsa 			if (enable)
2015991ae4ebSJiri Olsa 				evlist__enable_evsel(evlist, name);
2016991ae4ebSJiri Olsa 			else
2017991ae4ebSJiri Olsa 				evlist__disable_evsel(evlist, name);
2018991ae4ebSJiri Olsa 			pr_info("Event %s %s\n", evsel->name,
2019991ae4ebSJiri Olsa 				enable ? "enabled" : "disabled");
2020991ae4ebSJiri Olsa 		} else {
2021991ae4ebSJiri Olsa 			pr_info("failed: can't find '%s' event\n", name);
2022991ae4ebSJiri Olsa 		}
2023991ae4ebSJiri Olsa 	} else {
2024991ae4ebSJiri Olsa 		if (enable) {
2025991ae4ebSJiri Olsa 			evlist__enable(evlist);
2026991ae4ebSJiri Olsa 			pr_info(EVLIST_ENABLED_MSG);
2027991ae4ebSJiri Olsa 		} else {
2028991ae4ebSJiri Olsa 			evlist__disable(evlist);
2029991ae4ebSJiri Olsa 			pr_info(EVLIST_DISABLED_MSG);
2030991ae4ebSJiri Olsa 		}
2031991ae4ebSJiri Olsa 	}
2032991ae4ebSJiri Olsa 
2033991ae4ebSJiri Olsa 	return 0;
2034991ae4ebSJiri Olsa }
2035991ae4ebSJiri Olsa 
2036142544a9SJiri Olsa static int evlist__ctlfd_list(struct evlist *evlist, char *cmd_data)
2037142544a9SJiri Olsa {
2038142544a9SJiri Olsa 	struct perf_attr_details details = { .verbose = false, };
2039142544a9SJiri Olsa 	struct evsel *evsel;
2040142544a9SJiri Olsa 	char *arg;
2041142544a9SJiri Olsa 	int err;
2042142544a9SJiri Olsa 
2043142544a9SJiri Olsa 	err = get_cmd_arg(cmd_data,
2044142544a9SJiri Olsa 			  sizeof(EVLIST_CTL_CMD_EVLIST_TAG) - 1,
2045142544a9SJiri Olsa 			  &arg);
2046142544a9SJiri Olsa 	if (err < 0) {
2047142544a9SJiri Olsa 		pr_info("failed: wrong command\n");
2048142544a9SJiri Olsa 		return -1;
2049142544a9SJiri Olsa 	}
2050142544a9SJiri Olsa 
2051142544a9SJiri Olsa 	if (err) {
2052142544a9SJiri Olsa 		if (!strcmp(arg, "-v")) {
2053142544a9SJiri Olsa 			details.verbose = true;
2054142544a9SJiri Olsa 		} else if (!strcmp(arg, "-g")) {
2055142544a9SJiri Olsa 			details.event_group = true;
2056142544a9SJiri Olsa 		} else if (!strcmp(arg, "-F")) {
2057142544a9SJiri Olsa 			details.freq = true;
2058142544a9SJiri Olsa 		} else {
2059142544a9SJiri Olsa 			pr_info("failed: wrong command\n");
2060142544a9SJiri Olsa 			return -1;
2061142544a9SJiri Olsa 		}
2062142544a9SJiri Olsa 	}
2063142544a9SJiri Olsa 
2064142544a9SJiri Olsa 	evlist__for_each_entry(evlist, evsel)
2065142544a9SJiri Olsa 		evsel__fprintf(evsel, &details, stderr);
2066142544a9SJiri Olsa 
2067142544a9SJiri Olsa 	return 0;
2068142544a9SJiri Olsa }
2069142544a9SJiri Olsa 
2070ec886bf5SAlexey Budankov int evlist__ctlfd_process(struct evlist *evlist, enum evlist_ctl_cmd *cmd)
2071ec886bf5SAlexey Budankov {
2072ec886bf5SAlexey Budankov 	int err = 0;
2073ec886bf5SAlexey Budankov 	char cmd_data[EVLIST_CTL_CMD_MAX_LEN];
2074ec886bf5SAlexey Budankov 	int ctlfd_pos = evlist->ctl_fd.pos;
2075ec886bf5SAlexey Budankov 	struct pollfd *entries = evlist->core.pollfd.entries;
2076ec886bf5SAlexey Budankov 
2077ec886bf5SAlexey Budankov 	if (!evlist__ctlfd_initialized(evlist) || !entries[ctlfd_pos].revents)
2078ec886bf5SAlexey Budankov 		return 0;
2079ec886bf5SAlexey Budankov 
2080ec886bf5SAlexey Budankov 	if (entries[ctlfd_pos].revents & POLLIN) {
2081ec886bf5SAlexey Budankov 		err = evlist__ctlfd_recv(evlist, cmd, cmd_data,
2082ec886bf5SAlexey Budankov 					 EVLIST_CTL_CMD_MAX_LEN);
2083ec886bf5SAlexey Budankov 		if (err > 0) {
2084ec886bf5SAlexey Budankov 			switch (*cmd) {
2085ec886bf5SAlexey Budankov 			case EVLIST_CTL_CMD_ENABLE:
2086ec886bf5SAlexey Budankov 			case EVLIST_CTL_CMD_DISABLE:
2087991ae4ebSJiri Olsa 				err = evlist__ctlfd_enable(evlist, cmd_data,
2088991ae4ebSJiri Olsa 							   *cmd == EVLIST_CTL_CMD_ENABLE);
2089ec886bf5SAlexey Budankov 				break;
2090142544a9SJiri Olsa 			case EVLIST_CTL_CMD_EVLIST:
2091142544a9SJiri Olsa 				err = evlist__ctlfd_list(evlist, cmd_data);
2092142544a9SJiri Olsa 				break;
2093d20aff15SAdrian Hunter 			case EVLIST_CTL_CMD_SNAPSHOT:
2094f186cd61SJiri Olsa 			case EVLIST_CTL_CMD_STOP:
209547fddcb4SJiri Olsa 			case EVLIST_CTL_CMD_PING:
2096d20aff15SAdrian Hunter 				break;
2097ec886bf5SAlexey Budankov 			case EVLIST_CTL_CMD_ACK:
2098ec886bf5SAlexey Budankov 			case EVLIST_CTL_CMD_UNSUPPORTED:
2099ec886bf5SAlexey Budankov 			default:
2100ec886bf5SAlexey Budankov 				pr_debug("ctlfd: unsupported %d\n", *cmd);
2101ec886bf5SAlexey Budankov 				break;
2102ec886bf5SAlexey Budankov 			}
2103d20aff15SAdrian Hunter 			if (!(*cmd == EVLIST_CTL_CMD_ACK || *cmd == EVLIST_CTL_CMD_UNSUPPORTED ||
2104d20aff15SAdrian Hunter 			      *cmd == EVLIST_CTL_CMD_SNAPSHOT))
2105ec886bf5SAlexey Budankov 				evlist__ctlfd_ack(evlist);
2106ec886bf5SAlexey Budankov 		}
2107ec886bf5SAlexey Budankov 	}
2108ec886bf5SAlexey Budankov 
2109ec886bf5SAlexey Budankov 	if (entries[ctlfd_pos].revents & (POLLHUP | POLLERR))
2110ec886bf5SAlexey Budankov 		evlist__finalize_ctlfd(evlist);
2111ec886bf5SAlexey Budankov 	else
2112ec886bf5SAlexey Budankov 		entries[ctlfd_pos].revents = 0;
2113ec886bf5SAlexey Budankov 
2114ec886bf5SAlexey Budankov 	return err;
2115ec886bf5SAlexey Budankov }
2116b214ba8cSNamhyung Kim 
2117b214ba8cSNamhyung Kim struct evsel *evlist__find_evsel(struct evlist *evlist, int idx)
2118b214ba8cSNamhyung Kim {
2119b214ba8cSNamhyung Kim 	struct evsel *evsel;
2120b214ba8cSNamhyung Kim 
2121b214ba8cSNamhyung Kim 	evlist__for_each_entry(evlist, evsel) {
212238fe0e01SJiri Olsa 		if (evsel->core.idx == idx)
2123b214ba8cSNamhyung Kim 			return evsel;
2124b214ba8cSNamhyung Kim 	}
2125b214ba8cSNamhyung Kim 	return NULL;
2126b214ba8cSNamhyung Kim }
21279865ea8aSArnaldo Carvalho de Melo 
21289865ea8aSArnaldo Carvalho de Melo int evlist__scnprintf_evsels(struct evlist *evlist, size_t size, char *bf)
21299865ea8aSArnaldo Carvalho de Melo {
21309865ea8aSArnaldo Carvalho de Melo 	struct evsel *evsel;
21319865ea8aSArnaldo Carvalho de Melo 	int printed = 0;
21329865ea8aSArnaldo Carvalho de Melo 
21339865ea8aSArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, evsel) {
21349865ea8aSArnaldo Carvalho de Melo 		if (evsel__is_dummy_event(evsel))
21359865ea8aSArnaldo Carvalho de Melo 			continue;
21369865ea8aSArnaldo Carvalho de Melo 		if (size > (strlen(evsel__name(evsel)) + (printed ? 2 : 1))) {
21379865ea8aSArnaldo Carvalho de Melo 			printed += scnprintf(bf + printed, size - printed, "%s%s", printed ? "," : "", evsel__name(evsel));
21389865ea8aSArnaldo Carvalho de Melo 		} else {
21399865ea8aSArnaldo Carvalho de Melo 			printed += scnprintf(bf + printed, size - printed, "%s...", printed ? "," : "");
21409865ea8aSArnaldo Carvalho de Melo 			break;
21419865ea8aSArnaldo Carvalho de Melo 		}
21429865ea8aSArnaldo Carvalho de Melo 	}
21439865ea8aSArnaldo Carvalho de Melo 
21449865ea8aSArnaldo Carvalho de Melo 	return printed;
21459865ea8aSArnaldo Carvalho de Melo }
2146d5a8bd0fSJin Yao 
2147d5a8bd0fSJin Yao void evlist__check_mem_load_aux(struct evlist *evlist)
2148d5a8bd0fSJin Yao {
2149d5a8bd0fSJin Yao 	struct evsel *leader, *evsel, *pos;
2150d5a8bd0fSJin Yao 
2151d5a8bd0fSJin Yao 	/*
2152d5a8bd0fSJin Yao 	 * For some platforms, the 'mem-loads' event is required to use
2153d5a8bd0fSJin Yao 	 * together with 'mem-loads-aux' within a group and 'mem-loads-aux'
2154d5a8bd0fSJin Yao 	 * must be the group leader. Now we disable this group before reporting
2155d5a8bd0fSJin Yao 	 * because 'mem-loads-aux' is just an auxiliary event. It doesn't carry
2156d5a8bd0fSJin Yao 	 * any valid memory load information.
2157d5a8bd0fSJin Yao 	 */
2158d5a8bd0fSJin Yao 	evlist__for_each_entry(evlist, evsel) {
2159fba7c866SJiri Olsa 		leader = evsel__leader(evsel);
2160d5a8bd0fSJin Yao 		if (leader == evsel)
2161d5a8bd0fSJin Yao 			continue;
2162d5a8bd0fSJin Yao 
2163d5a8bd0fSJin Yao 		if (leader->name && strstr(leader->name, "mem-loads-aux")) {
2164d5a8bd0fSJin Yao 			for_each_group_evsel(pos, leader) {
2165fba7c866SJiri Olsa 				evsel__set_leader(pos, pos);
2166d5a8bd0fSJin Yao 				pos->core.nr_members = 0;
2167d5a8bd0fSJin Yao 			}
2168d5a8bd0fSJin Yao 		}
2169d5a8bd0fSJin Yao 	}
2170d5a8bd0fSJin Yao }
2171