xref: /openbmc/linux/tools/perf/util/evlist.c (revision d98079c0)
191007045SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2f8a95309SArnaldo Carvalho de Melo /*
3f8a95309SArnaldo Carvalho de Melo  * Copyright (C) 2011, Red Hat Inc, Arnaldo Carvalho de Melo <acme@redhat.com>
4f8a95309SArnaldo Carvalho de Melo  *
5f8a95309SArnaldo Carvalho de Melo  * Parts came from builtin-{top,stat,record}.c, see those files for further
6f8a95309SArnaldo Carvalho de Melo  * copyright notes.
7f8a95309SArnaldo Carvalho de Melo  */
8956fa571SArnaldo Carvalho de Melo #include <api/fs/fs.h>
9a43783aeSArnaldo Carvalho de Melo #include <errno.h>
10fd20e811SArnaldo Carvalho de Melo #include <inttypes.h>
115c581041SArnaldo Carvalho de Melo #include <poll.h>
12f8a95309SArnaldo Carvalho de Melo #include "cpumap.h"
13e0fcfb08SArnaldo Carvalho de Melo #include "util/mmap.h"
14f8a95309SArnaldo Carvalho de Melo #include "thread_map.h"
1512864b31SNamhyung Kim #include "target.h"
16361c99a6SArnaldo Carvalho de Melo #include "evlist.h"
17361c99a6SArnaldo Carvalho de Melo #include "evsel.h"
18e3e1a54fSAdrian Hunter #include "debug.h"
1958db1d6eSArnaldo Carvalho de Melo #include "units.h"
205508c9daSSong Liu #include "bpf_counter.h"
2120f2be1dSJiri Olsa #include <internal/lib.h> // page_size
227736627bSAndi Kleen #include "affinity.h"
23c1a604dfSArnaldo Carvalho de Melo #include "../perf.h"
2454cc54deSWang Nan #include "asm/bug.h"
25657ee553SSong Liu #include "bpf-event.h"
26da949f50SArnaldo Carvalho de Melo #include "util/string2.h"
2740c7d246SArnaldo Carvalho de Melo #include "util/perf_api_probe.h"
28142544a9SJiri Olsa #include "util/evsel_fprintf.h"
29b53a0755SJin Yao #include "util/evlist-hybrid.h"
301d3351e6SJin Yao #include "util/pmu.h"
319607ad3aSArnaldo Carvalho de Melo #include <signal.h>
3235b9d88eSArnaldo Carvalho de Melo #include <unistd.h>
33b397f846SArnaldo Carvalho de Melo #include <sched.h>
34f2a39fe8SArnaldo Carvalho de Melo #include <stdlib.h>
35361c99a6SArnaldo Carvalho de Melo 
3650d08e47SArnaldo Carvalho de Melo #include "parse-events.h"
374b6ab94eSJosh Poimboeuf #include <subcmd/parse-options.h>
3850d08e47SArnaldo Carvalho de Melo 
39bafae98eSArnaldo Carvalho de Melo #include <fcntl.h>
4086a5e0c2SArnaldo Carvalho de Melo #include <sys/ioctl.h>
41f8a95309SArnaldo Carvalho de Melo #include <sys/mman.h>
42a7672d1dSArnaldo Carvalho de Melo #include <sys/prctl.h>
43f8a95309SArnaldo Carvalho de Melo 
4470db7533SArnaldo Carvalho de Melo #include <linux/bitops.h>
4570db7533SArnaldo Carvalho de Melo #include <linux/hash.h>
460389cd1fSArnaldo Carvalho de Melo #include <linux/log2.h>
478dd2a131SJiri Olsa #include <linux/err.h>
488520a98dSArnaldo Carvalho de Melo #include <linux/string.h>
497f7c536fSArnaldo Carvalho de Melo #include <linux/zalloc.h>
504562a739SJiri Olsa #include <perf/evlist.h>
5188761fa1SJiri Olsa #include <perf/evsel.h>
529c3516d1SJiri Olsa #include <perf/cpumap.h>
537728fa0cSJiri Olsa #include <perf/mmap.h>
5470db7533SArnaldo Carvalho de Melo 
55e14e5497SArnaldo Carvalho de Melo #include <internal/xyarray.h>
56e14e5497SArnaldo Carvalho de Melo 
57748fe088SArnaldo Carvalho de Melo #ifdef LACKS_SIGQUEUE_PROTOTYPE
58748fe088SArnaldo Carvalho de Melo int sigqueue(pid_t pid, int sig, const union sigval value);
59748fe088SArnaldo Carvalho de Melo #endif
60748fe088SArnaldo Carvalho de Melo 
619dfcb759SJiri Olsa #define FD(e, x, y) (*(int *)xyarray__entry(e->core.fd, x, y))
628cd36f3eSJiri Olsa #define SID(e, x, y) xyarray__entry(e->core.sample_id, x, y)
63f8a95309SArnaldo Carvalho de Melo 
6452c86bcaSJiri Olsa void evlist__init(struct evlist *evlist, struct perf_cpu_map *cpus,
659749b90eSJiri Olsa 		  struct perf_thread_map *threads)
66361c99a6SArnaldo Carvalho de Melo {
674562a739SJiri Olsa 	perf_evlist__init(&evlist->core);
68453fa030SJiri Olsa 	perf_evlist__set_maps(&evlist->core, cpus, threads);
6935b9d88eSArnaldo Carvalho de Melo 	evlist->workload.pid = -1;
7054cc54deSWang Nan 	evlist->bkw_mmap_state = BKW_MMAP_NOTREADY;
718ab705b5SAlexey Budankov 	evlist->ctl_fd.fd = -1;
728ab705b5SAlexey Budankov 	evlist->ctl_fd.ack = -1;
738ab705b5SAlexey Budankov 	evlist->ctl_fd.pos = -1;
74361c99a6SArnaldo Carvalho de Melo }
75361c99a6SArnaldo Carvalho de Melo 
760f98b11cSJiri Olsa struct evlist *evlist__new(void)
77ef1d1af2SArnaldo Carvalho de Melo {
7863503dbaSJiri Olsa 	struct evlist *evlist = zalloc(sizeof(*evlist));
79ef1d1af2SArnaldo Carvalho de Melo 
80ef1d1af2SArnaldo Carvalho de Melo 	if (evlist != NULL)
8152c86bcaSJiri Olsa 		evlist__init(evlist, NULL, NULL);
82ef1d1af2SArnaldo Carvalho de Melo 
83361c99a6SArnaldo Carvalho de Melo 	return evlist;
84361c99a6SArnaldo Carvalho de Melo }
85361c99a6SArnaldo Carvalho de Melo 
86606e2c29SArnaldo Carvalho de Melo struct evlist *evlist__new_default(void)
87b22d54b0SJiri Olsa {
880f98b11cSJiri Olsa 	struct evlist *evlist = evlist__new();
89b22d54b0SJiri Olsa 
90e251abeeSArnaldo Carvalho de Melo 	if (evlist && evlist__add_default(evlist)) {
91c12995a5SJiri Olsa 		evlist__delete(evlist);
92b22d54b0SJiri Olsa 		evlist = NULL;
93b22d54b0SJiri Olsa 	}
94b22d54b0SJiri Olsa 
95b22d54b0SJiri Olsa 	return evlist;
96b22d54b0SJiri Olsa }
97b22d54b0SJiri Olsa 
98606e2c29SArnaldo Carvalho de Melo struct evlist *evlist__new_dummy(void)
995bae0250SArnaldo Carvalho de Melo {
1000f98b11cSJiri Olsa 	struct evlist *evlist = evlist__new();
1015bae0250SArnaldo Carvalho de Melo 
102e251abeeSArnaldo Carvalho de Melo 	if (evlist && evlist__add_dummy(evlist)) {
103c12995a5SJiri Olsa 		evlist__delete(evlist);
1045bae0250SArnaldo Carvalho de Melo 		evlist = NULL;
1055bae0250SArnaldo Carvalho de Melo 	}
1065bae0250SArnaldo Carvalho de Melo 
1075bae0250SArnaldo Carvalho de Melo 	return evlist;
1085bae0250SArnaldo Carvalho de Melo }
1095bae0250SArnaldo Carvalho de Melo 
11075562573SAdrian Hunter /**
1110a7e7ec9SArnaldo Carvalho de Melo  * evlist__set_id_pos - set the positions of event ids.
11275562573SAdrian Hunter  * @evlist: selected event list
11375562573SAdrian Hunter  *
11475562573SAdrian Hunter  * Events with compatible sample types all have the same id_pos
11575562573SAdrian Hunter  * and is_pos.  For convenience, put a copy on evlist.
11675562573SAdrian Hunter  */
1170a7e7ec9SArnaldo Carvalho de Melo void evlist__set_id_pos(struct evlist *evlist)
11875562573SAdrian Hunter {
119515dbe48SJiri Olsa 	struct evsel *first = evlist__first(evlist);
12075562573SAdrian Hunter 
12175562573SAdrian Hunter 	evlist->id_pos = first->id_pos;
12275562573SAdrian Hunter 	evlist->is_pos = first->is_pos;
12375562573SAdrian Hunter }
12475562573SAdrian Hunter 
1250a7e7ec9SArnaldo Carvalho de Melo static void evlist__update_id_pos(struct evlist *evlist)
126733cd2feSAdrian Hunter {
12732dcd021SJiri Olsa 	struct evsel *evsel;
128733cd2feSAdrian Hunter 
129e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, evsel)
1304b5e87b7SArnaldo Carvalho de Melo 		evsel__calc_id_pos(evsel);
131733cd2feSAdrian Hunter 
1320a7e7ec9SArnaldo Carvalho de Melo 	evlist__set_id_pos(evlist);
133733cd2feSAdrian Hunter }
134733cd2feSAdrian Hunter 
135e6b1878dSJiri Olsa static void evlist__purge(struct evlist *evlist)
136361c99a6SArnaldo Carvalho de Melo {
13732dcd021SJiri Olsa 	struct evsel *pos, *n;
138361c99a6SArnaldo Carvalho de Melo 
139e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry_safe(evlist, n, pos) {
140b27c4eceSJiri Olsa 		list_del_init(&pos->core.node);
141d49e4695SArnaldo Carvalho de Melo 		pos->evlist = NULL;
1425eb2dd2aSJiri Olsa 		evsel__delete(pos);
143361c99a6SArnaldo Carvalho de Melo 	}
144361c99a6SArnaldo Carvalho de Melo 
1456484d2f9SJiri Olsa 	evlist->core.nr_entries = 0;
146361c99a6SArnaldo Carvalho de Melo }
147361c99a6SArnaldo Carvalho de Melo 
148470579b0SJiri Olsa void evlist__exit(struct evlist *evlist)
149ef1d1af2SArnaldo Carvalho de Melo {
15004662523SArnaldo Carvalho de Melo 	zfree(&evlist->mmap);
1510b72d69aSWang Nan 	zfree(&evlist->overwrite_mmap);
15293dd6e28SJiri Olsa 	perf_evlist__exit(&evlist->core);
153ef1d1af2SArnaldo Carvalho de Melo }
154ef1d1af2SArnaldo Carvalho de Melo 
155c12995a5SJiri Olsa void evlist__delete(struct evlist *evlist)
156361c99a6SArnaldo Carvalho de Melo {
1570b04b3dcSArnaldo Carvalho de Melo 	if (evlist == NULL)
1580b04b3dcSArnaldo Carvalho de Melo 		return;
1590b04b3dcSArnaldo Carvalho de Melo 
160db6b7b13SJiri Olsa 	evlist__munmap(evlist);
161750b4edeSJiri Olsa 	evlist__close(evlist);
162e6b1878dSJiri Olsa 	evlist__purge(evlist);
163470579b0SJiri Olsa 	evlist__exit(evlist);
164361c99a6SArnaldo Carvalho de Melo 	free(evlist);
165361c99a6SArnaldo Carvalho de Melo }
166361c99a6SArnaldo Carvalho de Melo 
167a1cf3a75SJiri Olsa void evlist__add(struct evlist *evlist, struct evsel *entry)
168361c99a6SArnaldo Carvalho de Melo {
1696484d2f9SJiri Olsa 	perf_evlist__add(&evlist->core, &entry->core);
17038fe0e01SJiri Olsa 	entry->evlist = evlist;
17138fe0e01SJiri Olsa 	entry->tracking = !entry->core.idx;
1726484d2f9SJiri Olsa 
1736484d2f9SJiri Olsa 	if (evlist->core.nr_entries == 1)
1740a7e7ec9SArnaldo Carvalho de Melo 		evlist__set_id_pos(evlist);
175361c99a6SArnaldo Carvalho de Melo }
176361c99a6SArnaldo Carvalho de Melo 
17716251027SJiri Olsa void evlist__remove(struct evlist *evlist, struct evsel *evsel)
1784768230aSAdrian Hunter {
1794768230aSAdrian Hunter 	evsel->evlist = NULL;
18052e22fb8SJiri Olsa 	perf_evlist__remove(&evlist->core, &evsel->core);
1814768230aSAdrian Hunter }
1824768230aSAdrian Hunter 
183e414fd1aSArnaldo Carvalho de Melo void evlist__splice_list_tail(struct evlist *evlist, struct list_head *list)
18450d08e47SArnaldo Carvalho de Melo {
1856d2783feSJohn Garry 	while (!list_empty(list)) {
1866d2783feSJohn Garry 		struct evsel *evsel, *temp, *leader = NULL;
18775562573SAdrian Hunter 
188e5cadb93SArnaldo Carvalho de Melo 		__evlist__for_each_entry_safe(list, temp, evsel) {
189b27c4eceSJiri Olsa 			list_del_init(&evsel->core.node);
190a1cf3a75SJiri Olsa 			evlist__add(evlist, evsel);
1916d2783feSJohn Garry 			leader = evsel;
1926d2783feSJohn Garry 			break;
1936d2783feSJohn Garry 		}
1946d2783feSJohn Garry 
1956d2783feSJohn Garry 		__evlist__for_each_entry_safe(list, temp, evsel) {
196fba7c866SJiri Olsa 			if (evsel__has_leader(evsel, leader)) {
1976d2783feSJohn Garry 				list_del_init(&evsel->core.node);
1986d2783feSJohn Garry 				evlist__add(evlist, evsel);
1996d2783feSJohn Garry 			}
2006d2783feSJohn Garry 		}
201f114d6efSAdrian Hunter 	}
20250d08e47SArnaldo Carvalho de Melo }
20350d08e47SArnaldo Carvalho de Melo 
204c0e53476SArnaldo Carvalho de Melo int __evlist__set_tracepoints_handlers(struct evlist *evlist,
205c0e53476SArnaldo Carvalho de Melo 				       const struct evsel_str_handler *assocs, size_t nr_assocs)
206c0e53476SArnaldo Carvalho de Melo {
207c0e53476SArnaldo Carvalho de Melo 	size_t i;
208c0e53476SArnaldo Carvalho de Melo 	int err;
209c0e53476SArnaldo Carvalho de Melo 
210c0e53476SArnaldo Carvalho de Melo 	for (i = 0; i < nr_assocs; i++) {
211c0e53476SArnaldo Carvalho de Melo 		// Adding a handler for an event not in this evlist, just ignore it.
212b02736f7SArnaldo Carvalho de Melo 		struct evsel *evsel = evlist__find_tracepoint_by_name(evlist, assocs[i].name);
213c0e53476SArnaldo Carvalho de Melo 		if (evsel == NULL)
214c0e53476SArnaldo Carvalho de Melo 			continue;
215c0e53476SArnaldo Carvalho de Melo 
216c0e53476SArnaldo Carvalho de Melo 		err = -EEXIST;
217c0e53476SArnaldo Carvalho de Melo 		if (evsel->handler != NULL)
218c0e53476SArnaldo Carvalho de Melo 			goto out;
219c0e53476SArnaldo Carvalho de Melo 		evsel->handler = assocs[i].handler;
220c0e53476SArnaldo Carvalho de Melo 	}
221c0e53476SArnaldo Carvalho de Melo 
222c0e53476SArnaldo Carvalho de Melo 	err = 0;
223c0e53476SArnaldo Carvalho de Melo out:
224c0e53476SArnaldo Carvalho de Melo 	return err;
225c0e53476SArnaldo Carvalho de Melo }
226c0e53476SArnaldo Carvalho de Melo 
227a622eafaSArnaldo Carvalho de Melo void evlist__set_leader(struct evlist *evlist)
2286a4bb04cSJiri Olsa {
2292e6263abSJiri Olsa 	perf_evlist__set_leader(&evlist->core);
23097f63e4aSNamhyung Kim }
2316a4bb04cSJiri Olsa 
232e251abeeSArnaldo Carvalho de Melo int __evlist__add_default(struct evlist *evlist, bool precise)
233361c99a6SArnaldo Carvalho de Melo {
234b53a0755SJin Yao 	struct evsel *evsel;
235361c99a6SArnaldo Carvalho de Melo 
236b53a0755SJin Yao 	evsel = evsel__new_cycles(precise, PERF_TYPE_HARDWARE,
237b53a0755SJin Yao 				  PERF_COUNT_HW_CPU_CYCLES);
238361c99a6SArnaldo Carvalho de Melo 	if (evsel == NULL)
2397c48dcfdSArnaldo Carvalho de Melo 		return -ENOMEM;
240361c99a6SArnaldo Carvalho de Melo 
241a1cf3a75SJiri Olsa 	evlist__add(evlist, evsel);
242361c99a6SArnaldo Carvalho de Melo 	return 0;
243361c99a6SArnaldo Carvalho de Melo }
2445c581041SArnaldo Carvalho de Melo 
245e251abeeSArnaldo Carvalho de Melo int evlist__add_dummy(struct evlist *evlist)
2465bae0250SArnaldo Carvalho de Melo {
2475bae0250SArnaldo Carvalho de Melo 	struct perf_event_attr attr = {
2485bae0250SArnaldo Carvalho de Melo 		.type	= PERF_TYPE_SOFTWARE,
2495bae0250SArnaldo Carvalho de Melo 		.config = PERF_COUNT_SW_DUMMY,
2505bae0250SArnaldo Carvalho de Melo 		.size	= sizeof(attr), /* to capture ABI version */
2515bae0250SArnaldo Carvalho de Melo 	};
2528f6725a2SArnaldo Carvalho de Melo 	struct evsel *evsel = evsel__new_idx(&attr, evlist->core.nr_entries);
2535bae0250SArnaldo Carvalho de Melo 
2545bae0250SArnaldo Carvalho de Melo 	if (evsel == NULL)
2555bae0250SArnaldo Carvalho de Melo 		return -ENOMEM;
2565bae0250SArnaldo Carvalho de Melo 
257a1cf3a75SJiri Olsa 	evlist__add(evlist, evsel);
2585bae0250SArnaldo Carvalho de Melo 	return 0;
2595bae0250SArnaldo Carvalho de Melo }
2605bae0250SArnaldo Carvalho de Melo 
261e251abeeSArnaldo Carvalho de Melo static int evlist__add_attrs(struct evlist *evlist, struct perf_event_attr *attrs, size_t nr_attrs)
26250d08e47SArnaldo Carvalho de Melo {
26332dcd021SJiri Olsa 	struct evsel *evsel, *n;
26450d08e47SArnaldo Carvalho de Melo 	LIST_HEAD(head);
26550d08e47SArnaldo Carvalho de Melo 	size_t i;
26650d08e47SArnaldo Carvalho de Melo 
26750d08e47SArnaldo Carvalho de Melo 	for (i = 0; i < nr_attrs; i++) {
2688f6725a2SArnaldo Carvalho de Melo 		evsel = evsel__new_idx(attrs + i, evlist->core.nr_entries + i);
26950d08e47SArnaldo Carvalho de Melo 		if (evsel == NULL)
27050d08e47SArnaldo Carvalho de Melo 			goto out_delete_partial_list;
271b27c4eceSJiri Olsa 		list_add_tail(&evsel->core.node, &head);
27250d08e47SArnaldo Carvalho de Melo 	}
27350d08e47SArnaldo Carvalho de Melo 
274e414fd1aSArnaldo Carvalho de Melo 	evlist__splice_list_tail(evlist, &head);
27550d08e47SArnaldo Carvalho de Melo 
27650d08e47SArnaldo Carvalho de Melo 	return 0;
27750d08e47SArnaldo Carvalho de Melo 
27850d08e47SArnaldo Carvalho de Melo out_delete_partial_list:
279e5cadb93SArnaldo Carvalho de Melo 	__evlist__for_each_entry_safe(&head, n, evsel)
2805eb2dd2aSJiri Olsa 		evsel__delete(evsel);
28150d08e47SArnaldo Carvalho de Melo 	return -1;
28250d08e47SArnaldo Carvalho de Melo }
28350d08e47SArnaldo Carvalho de Melo 
284e251abeeSArnaldo Carvalho de Melo int __evlist__add_default_attrs(struct evlist *evlist, struct perf_event_attr *attrs, size_t nr_attrs)
28579695e1bSArnaldo Carvalho de Melo {
28679695e1bSArnaldo Carvalho de Melo 	size_t i;
28779695e1bSArnaldo Carvalho de Melo 
28879695e1bSArnaldo Carvalho de Melo 	for (i = 0; i < nr_attrs; i++)
28979695e1bSArnaldo Carvalho de Melo 		event_attr_init(attrs + i);
29079695e1bSArnaldo Carvalho de Melo 
291a1cf3a75SJiri Olsa 	return evlist__add_attrs(evlist, attrs, nr_attrs);
29279695e1bSArnaldo Carvalho de Melo }
29379695e1bSArnaldo Carvalho de Melo 
29442641d6fSKan Liang __weak int arch_evlist__add_default_attrs(struct evlist *evlist __maybe_unused)
29542641d6fSKan Liang {
29642641d6fSKan Liang 	return 0;
29742641d6fSKan Liang }
29842641d6fSKan Liang 
299b02736f7SArnaldo Carvalho de Melo struct evsel *evlist__find_tracepoint_by_id(struct evlist *evlist, int id)
300ee29be62SArnaldo Carvalho de Melo {
30132dcd021SJiri Olsa 	struct evsel *evsel;
302ee29be62SArnaldo Carvalho de Melo 
303e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, evsel) {
3041fc632ceSJiri Olsa 		if (evsel->core.attr.type   == PERF_TYPE_TRACEPOINT &&
3051fc632ceSJiri Olsa 		    (int)evsel->core.attr.config == id)
306ee29be62SArnaldo Carvalho de Melo 			return evsel;
307ee29be62SArnaldo Carvalho de Melo 	}
308ee29be62SArnaldo Carvalho de Melo 
309ee29be62SArnaldo Carvalho de Melo 	return NULL;
310ee29be62SArnaldo Carvalho de Melo }
311ee29be62SArnaldo Carvalho de Melo 
312b02736f7SArnaldo Carvalho de Melo struct evsel *evlist__find_tracepoint_by_name(struct evlist *evlist, const char *name)
313a2f2804aSDavid Ahern {
31432dcd021SJiri Olsa 	struct evsel *evsel;
315a2f2804aSDavid Ahern 
316e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, evsel) {
3171fc632ceSJiri Olsa 		if ((evsel->core.attr.type == PERF_TYPE_TRACEPOINT) &&
318a2f2804aSDavid Ahern 		    (strcmp(evsel->name, name) == 0))
319a2f2804aSDavid Ahern 			return evsel;
320a2f2804aSDavid Ahern 	}
321a2f2804aSDavid Ahern 
322a2f2804aSDavid Ahern 	return NULL;
323a2f2804aSDavid Ahern }
324a2f2804aSDavid Ahern 
325e251abeeSArnaldo Carvalho de Melo int evlist__add_newtp(struct evlist *evlist, const char *sys, const char *name, void *handler)
32639876e7dSArnaldo Carvalho de Melo {
3278f6725a2SArnaldo Carvalho de Melo 	struct evsel *evsel = evsel__newtp(sys, name);
32839876e7dSArnaldo Carvalho de Melo 
3298dd2a131SJiri Olsa 	if (IS_ERR(evsel))
33039876e7dSArnaldo Carvalho de Melo 		return -1;
33139876e7dSArnaldo Carvalho de Melo 
332744a9719SArnaldo Carvalho de Melo 	evsel->handler = handler;
333a1cf3a75SJiri Olsa 	evlist__add(evlist, evsel);
33439876e7dSArnaldo Carvalho de Melo 	return 0;
33539876e7dSArnaldo Carvalho de Melo }
33639876e7dSArnaldo Carvalho de Melo 
337472832d2SIan Rogers struct evlist_cpu_iterator evlist__cpu_begin(struct evlist *evlist, struct affinity *affinity)
338a8cbe40fSAndi Kleen {
339472832d2SIan Rogers 	struct evlist_cpu_iterator itr = {
340472832d2SIan Rogers 		.container = evlist,
3418b464eacSIan Rogers 		.evsel = NULL,
342472832d2SIan Rogers 		.cpu_map_idx = 0,
343472832d2SIan Rogers 		.evlist_cpu_map_idx = 0,
344472832d2SIan Rogers 		.evlist_cpu_map_nr = perf_cpu_map__nr(evlist->core.all_cpus),
3456d18804bSIan Rogers 		.cpu = (struct perf_cpu){ .cpu = -1},
346472832d2SIan Rogers 		.affinity = affinity,
347472832d2SIan Rogers 	};
348a8cbe40fSAndi Kleen 
3498b464eacSIan Rogers 	if (evlist__empty(evlist)) {
3508b464eacSIan Rogers 		/* Ensure the empty list doesn't iterate. */
3518b464eacSIan Rogers 		itr.evlist_cpu_map_idx = itr.evlist_cpu_map_nr;
3528b464eacSIan Rogers 	} else {
3538b464eacSIan Rogers 		itr.evsel = evlist__first(evlist);
354472832d2SIan Rogers 		if (itr.affinity) {
355472832d2SIan Rogers 			itr.cpu = perf_cpu_map__cpu(evlist->core.all_cpus, 0);
3566d18804bSIan Rogers 			affinity__set(itr.affinity, itr.cpu.cpu);
357472832d2SIan Rogers 			itr.cpu_map_idx = perf_cpu_map__idx(itr.evsel->core.cpus, itr.cpu);
358a8cbe40fSAndi Kleen 			/*
3598b464eacSIan Rogers 			 * If this CPU isn't in the evsel's cpu map then advance
3608b464eacSIan Rogers 			 * through the list.
361a8cbe40fSAndi Kleen 			 */
362472832d2SIan Rogers 			if (itr.cpu_map_idx == -1)
363472832d2SIan Rogers 				evlist_cpu_iterator__next(&itr);
364472832d2SIan Rogers 		}
3658b464eacSIan Rogers 	}
366472832d2SIan Rogers 	return itr;
367a8cbe40fSAndi Kleen }
368a8cbe40fSAndi Kleen 
369472832d2SIan Rogers void evlist_cpu_iterator__next(struct evlist_cpu_iterator *evlist_cpu_itr)
370a8cbe40fSAndi Kleen {
371472832d2SIan Rogers 	while (evlist_cpu_itr->evsel != evlist__last(evlist_cpu_itr->container)) {
372472832d2SIan Rogers 		evlist_cpu_itr->evsel = evsel__next(evlist_cpu_itr->evsel);
373472832d2SIan Rogers 		evlist_cpu_itr->cpu_map_idx =
374472832d2SIan Rogers 			perf_cpu_map__idx(evlist_cpu_itr->evsel->core.cpus,
375472832d2SIan Rogers 					  evlist_cpu_itr->cpu);
376472832d2SIan Rogers 		if (evlist_cpu_itr->cpu_map_idx != -1)
377472832d2SIan Rogers 			return;
378472832d2SIan Rogers 	}
379472832d2SIan Rogers 	evlist_cpu_itr->evlist_cpu_map_idx++;
380472832d2SIan Rogers 	if (evlist_cpu_itr->evlist_cpu_map_idx < evlist_cpu_itr->evlist_cpu_map_nr) {
381472832d2SIan Rogers 		evlist_cpu_itr->evsel = evlist__first(evlist_cpu_itr->container);
382472832d2SIan Rogers 		evlist_cpu_itr->cpu =
383472832d2SIan Rogers 			perf_cpu_map__cpu(evlist_cpu_itr->container->core.all_cpus,
384472832d2SIan Rogers 					  evlist_cpu_itr->evlist_cpu_map_idx);
385472832d2SIan Rogers 		if (evlist_cpu_itr->affinity)
3866d18804bSIan Rogers 			affinity__set(evlist_cpu_itr->affinity, evlist_cpu_itr->cpu.cpu);
387472832d2SIan Rogers 		evlist_cpu_itr->cpu_map_idx =
388472832d2SIan Rogers 			perf_cpu_map__idx(evlist_cpu_itr->evsel->core.cpus,
389472832d2SIan Rogers 					  evlist_cpu_itr->cpu);
390472832d2SIan Rogers 		/*
391472832d2SIan Rogers 		 * If this CPU isn't in the evsel's cpu map then advance through
392472832d2SIan Rogers 		 * the list.
393472832d2SIan Rogers 		 */
394472832d2SIan Rogers 		if (evlist_cpu_itr->cpu_map_idx == -1)
395472832d2SIan Rogers 			evlist_cpu_iterator__next(evlist_cpu_itr);
396472832d2SIan Rogers 	}
397a8cbe40fSAndi Kleen }
398a8cbe40fSAndi Kleen 
399472832d2SIan Rogers bool evlist_cpu_iterator__end(const struct evlist_cpu_iterator *evlist_cpu_itr)
400a8cbe40fSAndi Kleen {
401472832d2SIan Rogers 	return evlist_cpu_itr->evlist_cpu_map_idx >= evlist_cpu_itr->evlist_cpu_map_nr;
402a8cbe40fSAndi Kleen }
403a8cbe40fSAndi Kleen 
4047cfcd1e0SJiri Olsa static int evsel__strcmp(struct evsel *pos, char *evsel_name)
4057cfcd1e0SJiri Olsa {
4067cfcd1e0SJiri Olsa 	if (!evsel_name)
4077cfcd1e0SJiri Olsa 		return 0;
4087cfcd1e0SJiri Olsa 	if (evsel__is_dummy_event(pos))
4097cfcd1e0SJiri Olsa 		return 1;
4107cfcd1e0SJiri Olsa 	return strcmp(pos->name, evsel_name);
4117cfcd1e0SJiri Olsa }
4127cfcd1e0SJiri Olsa 
4137cfcd1e0SJiri Olsa static int evlist__is_enabled(struct evlist *evlist)
4147cfcd1e0SJiri Olsa {
4157cfcd1e0SJiri Olsa 	struct evsel *pos;
4167cfcd1e0SJiri Olsa 
4177cfcd1e0SJiri Olsa 	evlist__for_each_entry(evlist, pos) {
4187cfcd1e0SJiri Olsa 		if (!evsel__is_group_leader(pos) || !pos->core.fd)
4197cfcd1e0SJiri Olsa 			continue;
4207cfcd1e0SJiri Olsa 		/* If at least one event is enabled, evlist is enabled. */
4217cfcd1e0SJiri Olsa 		if (!pos->disabled)
4227cfcd1e0SJiri Olsa 			return true;
4237cfcd1e0SJiri Olsa 	}
4247cfcd1e0SJiri Olsa 	return false;
4257cfcd1e0SJiri Olsa }
4267cfcd1e0SJiri Olsa 
4277cfcd1e0SJiri Olsa static void __evlist__disable(struct evlist *evlist, char *evsel_name)
4284152ab37SArnaldo Carvalho de Melo {
42932dcd021SJiri Olsa 	struct evsel *pos;
430472832d2SIan Rogers 	struct evlist_cpu_iterator evlist_cpu_itr;
4310d3d2376SArnaldo Carvalho de Melo 	struct affinity saved_affinity, *affinity = NULL;
43287cf8360SAdrian Hunter 	bool has_imm = false;
433704e2f5bSAndi Kleen 
4340d3d2376SArnaldo Carvalho de Melo 	// See explanation in evlist__close()
4350df6ade7SIan Rogers 	if (!cpu_map__is_dummy(evlist->core.user_requested_cpus)) {
4360d3d2376SArnaldo Carvalho de Melo 		if (affinity__setup(&saved_affinity) < 0)
437704e2f5bSAndi Kleen 			return;
4380d3d2376SArnaldo Carvalho de Melo 		affinity = &saved_affinity;
4390d3d2376SArnaldo Carvalho de Melo 	}
440704e2f5bSAndi Kleen 
44187cf8360SAdrian Hunter 	/* Disable 'immediate' events last */
442472832d2SIan Rogers 	for (int imm = 0; imm <= 1; imm++) {
4430d3d2376SArnaldo Carvalho de Melo 		evlist__for_each_cpu(evlist_cpu_itr, evlist, affinity) {
444472832d2SIan Rogers 			pos = evlist_cpu_itr.evsel;
4457cfcd1e0SJiri Olsa 			if (evsel__strcmp(pos, evsel_name))
4467cfcd1e0SJiri Olsa 				continue;
447c754c382SArnaldo Carvalho de Melo 			if (pos->disabled || !evsel__is_group_leader(pos) || !pos->core.fd)
4483fe4430dSJiri Olsa 				continue;
44987cf8360SAdrian Hunter 			if (pos->immediate)
45087cf8360SAdrian Hunter 				has_imm = true;
45187cf8360SAdrian Hunter 			if (pos->immediate != imm)
45287cf8360SAdrian Hunter 				continue;
453472832d2SIan Rogers 			evsel__disable_cpu(pos, evlist_cpu_itr.cpu_map_idx);
454704e2f5bSAndi Kleen 		}
45587cf8360SAdrian Hunter 		if (!has_imm)
45687cf8360SAdrian Hunter 			break;
45787cf8360SAdrian Hunter 	}
45887cf8360SAdrian Hunter 
4590d3d2376SArnaldo Carvalho de Melo 	affinity__cleanup(affinity);
460704e2f5bSAndi Kleen 	evlist__for_each_entry(evlist, pos) {
4617cfcd1e0SJiri Olsa 		if (evsel__strcmp(pos, evsel_name))
4627cfcd1e0SJiri Olsa 			continue;
463c754c382SArnaldo Carvalho de Melo 		if (!evsel__is_group_leader(pos) || !pos->core.fd)
464704e2f5bSAndi Kleen 			continue;
465704e2f5bSAndi Kleen 		pos->disabled = true;
4664152ab37SArnaldo Carvalho de Melo 	}
4672b56bcfbSArnaldo Carvalho de Melo 
4687cfcd1e0SJiri Olsa 	/*
4697cfcd1e0SJiri Olsa 	 * If we disabled only single event, we need to check
4707cfcd1e0SJiri Olsa 	 * the enabled state of the evlist manually.
4717cfcd1e0SJiri Olsa 	 */
4727cfcd1e0SJiri Olsa 	if (evsel_name)
4737cfcd1e0SJiri Olsa 		evlist->enabled = evlist__is_enabled(evlist);
4747cfcd1e0SJiri Olsa 	else
4752b56bcfbSArnaldo Carvalho de Melo 		evlist->enabled = false;
4764152ab37SArnaldo Carvalho de Melo }
4774152ab37SArnaldo Carvalho de Melo 
4787cfcd1e0SJiri Olsa void evlist__disable(struct evlist *evlist)
4797cfcd1e0SJiri Olsa {
4807cfcd1e0SJiri Olsa 	__evlist__disable(evlist, NULL);
4817cfcd1e0SJiri Olsa }
4827cfcd1e0SJiri Olsa 
4837cfcd1e0SJiri Olsa void evlist__disable_evsel(struct evlist *evlist, char *evsel_name)
4847cfcd1e0SJiri Olsa {
4857cfcd1e0SJiri Olsa 	__evlist__disable(evlist, evsel_name);
4867cfcd1e0SJiri Olsa }
4877cfcd1e0SJiri Olsa 
4887cfcd1e0SJiri Olsa static void __evlist__enable(struct evlist *evlist, char *evsel_name)
489764e16a3SDavid Ahern {
49032dcd021SJiri Olsa 	struct evsel *pos;
491472832d2SIan Rogers 	struct evlist_cpu_iterator evlist_cpu_itr;
492f350ee95SArnaldo Carvalho de Melo 	struct affinity saved_affinity, *affinity = NULL;
493764e16a3SDavid Ahern 
494f350ee95SArnaldo Carvalho de Melo 	// See explanation in evlist__close()
4950df6ade7SIan Rogers 	if (!cpu_map__is_dummy(evlist->core.user_requested_cpus)) {
496f350ee95SArnaldo Carvalho de Melo 		if (affinity__setup(&saved_affinity) < 0)
497704e2f5bSAndi Kleen 			return;
498f350ee95SArnaldo Carvalho de Melo 		affinity = &saved_affinity;
499f350ee95SArnaldo Carvalho de Melo 	}
500704e2f5bSAndi Kleen 
501f350ee95SArnaldo Carvalho de Melo 	evlist__for_each_cpu(evlist_cpu_itr, evlist, affinity) {
502472832d2SIan Rogers 		pos = evlist_cpu_itr.evsel;
5037cfcd1e0SJiri Olsa 		if (evsel__strcmp(pos, evsel_name))
5047cfcd1e0SJiri Olsa 			continue;
505c754c382SArnaldo Carvalho de Melo 		if (!evsel__is_group_leader(pos) || !pos->core.fd)
506704e2f5bSAndi Kleen 			continue;
507472832d2SIan Rogers 		evsel__enable_cpu(pos, evlist_cpu_itr.cpu_map_idx);
508704e2f5bSAndi Kleen 	}
509f350ee95SArnaldo Carvalho de Melo 	affinity__cleanup(affinity);
510e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, pos) {
5117cfcd1e0SJiri Olsa 		if (evsel__strcmp(pos, evsel_name))
5127cfcd1e0SJiri Olsa 			continue;
513c754c382SArnaldo Carvalho de Melo 		if (!evsel__is_group_leader(pos) || !pos->core.fd)
5143fe4430dSJiri Olsa 			continue;
515704e2f5bSAndi Kleen 		pos->disabled = false;
516764e16a3SDavid Ahern 	}
5172b56bcfbSArnaldo Carvalho de Melo 
5187cfcd1e0SJiri Olsa 	/*
5197cfcd1e0SJiri Olsa 	 * Even single event sets the 'enabled' for evlist,
5207cfcd1e0SJiri Olsa 	 * so the toggle can work properly and toggle to
5217cfcd1e0SJiri Olsa 	 * 'disabled' state.
5227cfcd1e0SJiri Olsa 	 */
5232b56bcfbSArnaldo Carvalho de Melo 	evlist->enabled = true;
5242b56bcfbSArnaldo Carvalho de Melo }
5252b56bcfbSArnaldo Carvalho de Melo 
5267cfcd1e0SJiri Olsa void evlist__enable(struct evlist *evlist)
5277cfcd1e0SJiri Olsa {
5287cfcd1e0SJiri Olsa 	__evlist__enable(evlist, NULL);
5297cfcd1e0SJiri Olsa }
5307cfcd1e0SJiri Olsa 
5317cfcd1e0SJiri Olsa void evlist__enable_evsel(struct evlist *evlist, char *evsel_name)
5327cfcd1e0SJiri Olsa {
5337cfcd1e0SJiri Olsa 	__evlist__enable(evlist, evsel_name);
5347cfcd1e0SJiri Olsa }
5357cfcd1e0SJiri Olsa 
536ade9d208SArnaldo Carvalho de Melo void evlist__toggle_enable(struct evlist *evlist)
5372b56bcfbSArnaldo Carvalho de Melo {
538e74676deSJiri Olsa 	(evlist->enabled ? evlist__disable : evlist__enable)(evlist);
539764e16a3SDavid Ahern }
540764e16a3SDavid Ahern 
541f4009e7bSJiri Olsa int evlist__add_pollfd(struct evlist *evlist, int fd)
542e4b356b5SArnaldo Carvalho de Melo {
543ab4c1f9fSAlexey Budankov 	return perf_evlist__add_pollfd(&evlist->core, fd, NULL, POLLIN, fdarray_flag__default);
544e4b356b5SArnaldo Carvalho de Melo }
5451b85337dSArnaldo Carvalho de Melo 
546f4009e7bSJiri Olsa int evlist__filter_pollfd(struct evlist *evlist, short revents_and_mask)
5471ddec7f0SArnaldo Carvalho de Melo {
54884227cb1SJiri Olsa 	return perf_evlist__filter_pollfd(&evlist->core, revents_and_mask);
5491ddec7f0SArnaldo Carvalho de Melo }
5501ddec7f0SArnaldo Carvalho de Melo 
551e16c2ce7SYang Jihong #ifdef HAVE_EVENTFD_SUPPORT
552e16c2ce7SYang Jihong int evlist__add_wakeup_eventfd(struct evlist *evlist, int fd)
553e16c2ce7SYang Jihong {
554e16c2ce7SYang Jihong 	return perf_evlist__add_pollfd(&evlist->core, fd, NULL, POLLIN,
555e16c2ce7SYang Jihong 				       fdarray_flag__nonfilterable);
556e16c2ce7SYang Jihong }
557e16c2ce7SYang Jihong #endif
558e16c2ce7SYang Jihong 
55980ab2987SJiri Olsa int evlist__poll(struct evlist *evlist, int timeout)
560f66a889dSArnaldo Carvalho de Melo {
56180ab2987SJiri Olsa 	return perf_evlist__poll(&evlist->core, timeout);
562f66a889dSArnaldo Carvalho de Melo }
563f66a889dSArnaldo Carvalho de Melo 
5643ccf8a7bSArnaldo Carvalho de Melo struct perf_sample_id *evlist__id2sid(struct evlist *evlist, u64 id)
56570db7533SArnaldo Carvalho de Melo {
56670db7533SArnaldo Carvalho de Melo 	struct hlist_head *head;
56770db7533SArnaldo Carvalho de Melo 	struct perf_sample_id *sid;
56870db7533SArnaldo Carvalho de Melo 	int hash;
56970db7533SArnaldo Carvalho de Melo 
57070db7533SArnaldo Carvalho de Melo 	hash = hash_64(id, PERF_EVLIST__HLIST_BITS);
5711d5af02dSJiri Olsa 	head = &evlist->core.heads[hash];
57270db7533SArnaldo Carvalho de Melo 
573b67bfe0dSSasha Levin 	hlist_for_each_entry(sid, head, node)
57470db7533SArnaldo Carvalho de Melo 		if (sid->id == id)
575932a3594SJiri Olsa 			return sid;
576932a3594SJiri Olsa 
577932a3594SJiri Olsa 	return NULL;
578932a3594SJiri Olsa }
579932a3594SJiri Olsa 
5803ccf8a7bSArnaldo Carvalho de Melo struct evsel *evlist__id2evsel(struct evlist *evlist, u64 id)
581932a3594SJiri Olsa {
582932a3594SJiri Olsa 	struct perf_sample_id *sid;
583932a3594SJiri Olsa 
5846484d2f9SJiri Olsa 	if (evlist->core.nr_entries == 1 || !id)
585515dbe48SJiri Olsa 		return evlist__first(evlist);
586932a3594SJiri Olsa 
5873ccf8a7bSArnaldo Carvalho de Melo 	sid = evlist__id2sid(evlist, id);
588932a3594SJiri Olsa 	if (sid)
58970c20369SJiri Olsa 		return container_of(sid->evsel, struct evsel, core);
59030e68bccSNamhyung Kim 
5918cedf3a5SArnaldo Carvalho de Melo 	if (!evlist__sample_id_all(evlist))
592515dbe48SJiri Olsa 		return evlist__first(evlist);
59330e68bccSNamhyung Kim 
59470db7533SArnaldo Carvalho de Melo 	return NULL;
59570db7533SArnaldo Carvalho de Melo }
59604391debSArnaldo Carvalho de Melo 
5973ccf8a7bSArnaldo Carvalho de Melo struct evsel *evlist__id2evsel_strict(struct evlist *evlist, u64 id)
598dddcf6abSAdrian Hunter {
599dddcf6abSAdrian Hunter 	struct perf_sample_id *sid;
600dddcf6abSAdrian Hunter 
601dddcf6abSAdrian Hunter 	if (!id)
602dddcf6abSAdrian Hunter 		return NULL;
603dddcf6abSAdrian Hunter 
6043ccf8a7bSArnaldo Carvalho de Melo 	sid = evlist__id2sid(evlist, id);
605dddcf6abSAdrian Hunter 	if (sid)
60670c20369SJiri Olsa 		return container_of(sid->evsel, struct evsel, core);
607dddcf6abSAdrian Hunter 
608dddcf6abSAdrian Hunter 	return NULL;
609dddcf6abSAdrian Hunter }
610dddcf6abSAdrian Hunter 
6113ccf8a7bSArnaldo Carvalho de Melo static int evlist__event2id(struct evlist *evlist, union perf_event *event, u64 *id)
61275562573SAdrian Hunter {
613b1fcd190SJiri Olsa 	const __u64 *array = event->sample.array;
61475562573SAdrian Hunter 	ssize_t n;
61575562573SAdrian Hunter 
61675562573SAdrian Hunter 	n = (event->header.size - sizeof(event->header)) >> 3;
61775562573SAdrian Hunter 
61875562573SAdrian Hunter 	if (event->header.type == PERF_RECORD_SAMPLE) {
61975562573SAdrian Hunter 		if (evlist->id_pos >= n)
62075562573SAdrian Hunter 			return -1;
62175562573SAdrian Hunter 		*id = array[evlist->id_pos];
62275562573SAdrian Hunter 	} else {
62375562573SAdrian Hunter 		if (evlist->is_pos > n)
62475562573SAdrian Hunter 			return -1;
62575562573SAdrian Hunter 		n -= evlist->is_pos;
62675562573SAdrian Hunter 		*id = array[n];
62775562573SAdrian Hunter 	}
62875562573SAdrian Hunter 	return 0;
62975562573SAdrian Hunter }
63075562573SAdrian Hunter 
6313ccf8a7bSArnaldo Carvalho de Melo struct evsel *evlist__event2evsel(struct evlist *evlist, union perf_event *event)
63275562573SAdrian Hunter {
633515dbe48SJiri Olsa 	struct evsel *first = evlist__first(evlist);
63475562573SAdrian Hunter 	struct hlist_head *head;
63575562573SAdrian Hunter 	struct perf_sample_id *sid;
63675562573SAdrian Hunter 	int hash;
63775562573SAdrian Hunter 	u64 id;
63875562573SAdrian Hunter 
6396484d2f9SJiri Olsa 	if (evlist->core.nr_entries == 1)
64098be6966SAdrian Hunter 		return first;
64198be6966SAdrian Hunter 
6421fc632ceSJiri Olsa 	if (!first->core.attr.sample_id_all &&
64398be6966SAdrian Hunter 	    event->header.type != PERF_RECORD_SAMPLE)
64498be6966SAdrian Hunter 		return first;
64575562573SAdrian Hunter 
6463ccf8a7bSArnaldo Carvalho de Melo 	if (evlist__event2id(evlist, event, &id))
64775562573SAdrian Hunter 		return NULL;
64875562573SAdrian Hunter 
64975562573SAdrian Hunter 	/* Synthesized events have an id of zero */
65075562573SAdrian Hunter 	if (!id)
65198be6966SAdrian Hunter 		return first;
65275562573SAdrian Hunter 
65375562573SAdrian Hunter 	hash = hash_64(id, PERF_EVLIST__HLIST_BITS);
6541d5af02dSJiri Olsa 	head = &evlist->core.heads[hash];
65575562573SAdrian Hunter 
65675562573SAdrian Hunter 	hlist_for_each_entry(sid, head, node) {
65775562573SAdrian Hunter 		if (sid->id == id)
65870c20369SJiri Olsa 			return container_of(sid->evsel, struct evsel, core);
65975562573SAdrian Hunter 	}
66075562573SAdrian Hunter 	return NULL;
66175562573SAdrian Hunter }
66275562573SAdrian Hunter 
6630a60b339SArnaldo Carvalho de Melo static int evlist__set_paused(struct evlist *evlist, bool value)
66465aea233SWang Nan {
66565aea233SWang Nan 	int i;
66665aea233SWang Nan 
6670b72d69aSWang Nan 	if (!evlist->overwrite_mmap)
668078c3386SWang Nan 		return 0;
669078c3386SWang Nan 
670c976ee11SJiri Olsa 	for (i = 0; i < evlist->core.nr_mmaps; i++) {
6712cf07b29SJiri Olsa 		int fd = evlist->overwrite_mmap[i].core.fd;
67265aea233SWang Nan 		int err;
67365aea233SWang Nan 
67465aea233SWang Nan 		if (fd < 0)
67565aea233SWang Nan 			continue;
67665aea233SWang Nan 		err = ioctl(fd, PERF_EVENT_IOC_PAUSE_OUTPUT, value ? 1 : 0);
67765aea233SWang Nan 		if (err)
67865aea233SWang Nan 			return err;
67965aea233SWang Nan 	}
68065aea233SWang Nan 	return 0;
68165aea233SWang Nan }
68265aea233SWang Nan 
6830a60b339SArnaldo Carvalho de Melo static int evlist__pause(struct evlist *evlist)
68465aea233SWang Nan {
6850a60b339SArnaldo Carvalho de Melo 	return evlist__set_paused(evlist, true);
68665aea233SWang Nan }
68765aea233SWang Nan 
6880a60b339SArnaldo Carvalho de Melo static int evlist__resume(struct evlist *evlist)
68965aea233SWang Nan {
6900a60b339SArnaldo Carvalho de Melo 	return evlist__set_paused(evlist, false);
69165aea233SWang Nan }
69265aea233SWang Nan 
693db6b7b13SJiri Olsa static void evlist__munmap_nofree(struct evlist *evlist)
694f8a95309SArnaldo Carvalho de Melo {
695aece948fSArnaldo Carvalho de Melo 	int i;
696f8a95309SArnaldo Carvalho de Melo 
697b2cb615dSWang Nan 	if (evlist->mmap)
698c976ee11SJiri Olsa 		for (i = 0; i < evlist->core.nr_mmaps; i++)
69980e53d11SJiri Olsa 			perf_mmap__munmap(&evlist->mmap[i].core);
700b2cb615dSWang Nan 
7010b72d69aSWang Nan 	if (evlist->overwrite_mmap)
702c976ee11SJiri Olsa 		for (i = 0; i < evlist->core.nr_mmaps; i++)
70380e53d11SJiri Olsa 			perf_mmap__munmap(&evlist->overwrite_mmap[i].core);
704a1f72618SWang Nan }
705aece948fSArnaldo Carvalho de Melo 
706db6b7b13SJiri Olsa void evlist__munmap(struct evlist *evlist)
707a1f72618SWang Nan {
708db6b7b13SJiri Olsa 	evlist__munmap_nofree(evlist);
70904662523SArnaldo Carvalho de Melo 	zfree(&evlist->mmap);
7100b72d69aSWang Nan 	zfree(&evlist->overwrite_mmap);
711f8a95309SArnaldo Carvalho de Melo }
712f8a95309SArnaldo Carvalho de Melo 
71380e53d11SJiri Olsa static void perf_mmap__unmap_cb(struct perf_mmap *map)
71480e53d11SJiri Olsa {
71580e53d11SJiri Olsa 	struct mmap *m = container_of(map, struct mmap, core);
71680e53d11SJiri Olsa 
71780e53d11SJiri Olsa 	mmap__munmap(m);
71880e53d11SJiri Olsa }
71980e53d11SJiri Olsa 
720d50cf361SJiri Olsa static struct mmap *evlist__alloc_mmap(struct evlist *evlist,
7212c5f6d87SKan Liang 				       bool overwrite)
722f8a95309SArnaldo Carvalho de Melo {
723d4c6fb36SWang Nan 	int i;
724a5830532SJiri Olsa 	struct mmap *map;
725d4c6fb36SWang Nan 
726c976ee11SJiri Olsa 	map = zalloc(evlist->core.nr_mmaps * sizeof(struct mmap));
7278db6d6b1SWang Nan 	if (!map)
7288db6d6b1SWang Nan 		return NULL;
729946ae1d4SWang Nan 
730c976ee11SJiri Olsa 	for (i = 0; i < evlist->core.nr_mmaps; i++) {
7316eb65f7aSJiri Olsa 		struct perf_mmap *prev = i ? &map[i - 1].core : NULL;
7326eb65f7aSJiri Olsa 
7334738ca30SArnaldo Carvalho de Melo 		/*
7344738ca30SArnaldo Carvalho de Melo 		 * When the perf_mmap() call is made we grab one refcount, plus
7356afad54dSKan Liang 		 * one extra to let perf_mmap__consume() get the last
7364738ca30SArnaldo Carvalho de Melo 		 * events after all real references (perf_mmap__get()) are
7374738ca30SArnaldo Carvalho de Melo 		 * dropped.
7384738ca30SArnaldo Carvalho de Melo 		 *
7394738ca30SArnaldo Carvalho de Melo 		 * Each PERF_EVENT_IOC_SET_OUTPUT points to this mmap and
7404738ca30SArnaldo Carvalho de Melo 		 * thus does perf_mmap__get() on it.
7414738ca30SArnaldo Carvalho de Melo 		 */
7426eb65f7aSJiri Olsa 		perf_mmap__init(&map[i].core, prev, overwrite, perf_mmap__unmap_cb);
7434738ca30SArnaldo Carvalho de Melo 	}
744353120b4SJiri Olsa 
7458db6d6b1SWang Nan 	return map;
746f8a95309SArnaldo Carvalho de Melo }
747f8a95309SArnaldo Carvalho de Melo 
7489abd2ab2SJiri Olsa static void
7499abd2ab2SJiri Olsa perf_evlist__mmap_cb_idx(struct perf_evlist *_evlist,
7508f111be6SAdrian Hunter 			 struct perf_evsel *_evsel __maybe_unused,
7519abd2ab2SJiri Olsa 			 struct perf_mmap_param *_mp,
7526a7b8a5aSAdrian Hunter 			 int idx)
7539abd2ab2SJiri Olsa {
7549abd2ab2SJiri Olsa 	struct evlist *evlist = container_of(_evlist, struct evlist, core);
7559abd2ab2SJiri Olsa 	struct mmap_params *mp = container_of(_mp, struct mmap_params, core);
7566a7b8a5aSAdrian Hunter 	bool per_cpu = !perf_cpu_map__empty(_evlist->user_requested_cpus);
7579abd2ab2SJiri Olsa 
7589abd2ab2SJiri Olsa 	auxtrace_mmap_params__set_idx(&mp->auxtrace_mp, evlist, idx, per_cpu);
7599abd2ab2SJiri Olsa }
7609abd2ab2SJiri Olsa 
761bb1b1885SJiri Olsa static struct perf_mmap*
762bb1b1885SJiri Olsa perf_evlist__mmap_cb_get(struct perf_evlist *_evlist, bool overwrite, int idx)
763bb1b1885SJiri Olsa {
764bb1b1885SJiri Olsa 	struct evlist *evlist = container_of(_evlist, struct evlist, core);
7653805e4f3SJiri Olsa 	struct mmap *maps;
766bb1b1885SJiri Olsa 
7673805e4f3SJiri Olsa 	maps = overwrite ? evlist->overwrite_mmap : evlist->mmap;
768bb1b1885SJiri Olsa 
769bb1b1885SJiri Olsa 	if (!maps) {
7703805e4f3SJiri Olsa 		maps = evlist__alloc_mmap(evlist, overwrite);
771bb1b1885SJiri Olsa 		if (!maps)
772bb1b1885SJiri Olsa 			return NULL;
773bb1b1885SJiri Olsa 
7743805e4f3SJiri Olsa 		if (overwrite) {
775bb1b1885SJiri Olsa 			evlist->overwrite_mmap = maps;
776bb1b1885SJiri Olsa 			if (evlist->bkw_mmap_state == BKW_MMAP_NOTREADY)
777ade9d208SArnaldo Carvalho de Melo 				evlist__toggle_bkw_mmap(evlist, BKW_MMAP_RUNNING);
7783805e4f3SJiri Olsa 		} else {
7793805e4f3SJiri Olsa 			evlist->mmap = maps;
780bb1b1885SJiri Olsa 		}
781bb1b1885SJiri Olsa 	}
782bb1b1885SJiri Olsa 
783bb1b1885SJiri Olsa 	return &maps[idx].core;
784bb1b1885SJiri Olsa }
785bb1b1885SJiri Olsa 
786b80132b1SJiri Olsa static int
787b80132b1SJiri Olsa perf_evlist__mmap_cb_mmap(struct perf_mmap *_map, struct perf_mmap_param *_mp,
7886d18804bSIan Rogers 			  int output, struct perf_cpu cpu)
789b80132b1SJiri Olsa {
790b80132b1SJiri Olsa 	struct mmap *map = container_of(_map, struct mmap, core);
791b80132b1SJiri Olsa 	struct mmap_params *mp = container_of(_mp, struct mmap_params, core);
792b80132b1SJiri Olsa 
793b80132b1SJiri Olsa 	return mmap__mmap(map, mp, output, cpu);
794b80132b1SJiri Olsa }
795b80132b1SJiri Olsa 
796f5e7150cSArnaldo Carvalho de Melo unsigned long perf_event_mlock_kb_in_pages(void)
797994a1f78SJiri Olsa {
798f5e7150cSArnaldo Carvalho de Melo 	unsigned long pages;
7998185e881SArnaldo Carvalho de Melo 	int max;
8008185e881SArnaldo Carvalho de Melo 
8018185e881SArnaldo Carvalho de Melo 	if (sysctl__read_int("kernel/perf_event_mlock_kb", &max) < 0) {
8028185e881SArnaldo Carvalho de Melo 		/*
8038185e881SArnaldo Carvalho de Melo 		 * Pick a once upon a time good value, i.e. things look
8048185e881SArnaldo Carvalho de Melo 		 * strange since we can't read a sysctl value, but lets not
8058185e881SArnaldo Carvalho de Melo 		 * die yet...
8068185e881SArnaldo Carvalho de Melo 		 */
8078185e881SArnaldo Carvalho de Melo 		max = 512;
8088185e881SArnaldo Carvalho de Melo 	} else {
8098185e881SArnaldo Carvalho de Melo 		max -= (page_size / 1024);
8108185e881SArnaldo Carvalho de Melo 	}
8118185e881SArnaldo Carvalho de Melo 
8128185e881SArnaldo Carvalho de Melo 	pages = (max * 1024) / page_size;
8131be300f4SArnaldo Carvalho de Melo 	if (!is_power_of_2(pages))
8141be300f4SArnaldo Carvalho de Melo 		pages = rounddown_pow_of_two(pages);
815f5e7150cSArnaldo Carvalho de Melo 
816f5e7150cSArnaldo Carvalho de Melo 	return pages;
817f5e7150cSArnaldo Carvalho de Melo }
818f5e7150cSArnaldo Carvalho de Melo 
8199521b5f2SJiri Olsa size_t evlist__mmap_size(unsigned long pages)
820f5e7150cSArnaldo Carvalho de Melo {
821f5e7150cSArnaldo Carvalho de Melo 	if (pages == UINT_MAX)
822f5e7150cSArnaldo Carvalho de Melo 		pages = perf_event_mlock_kb_in_pages();
823f5e7150cSArnaldo Carvalho de Melo 	else if (!is_power_of_2(pages))
824994a1f78SJiri Olsa 		return 0;
825994a1f78SJiri Olsa 
826994a1f78SJiri Olsa 	return (pages + 1) * page_size;
827994a1f78SJiri Olsa }
828994a1f78SJiri Olsa 
82933c2dcfdSDavid Ahern static long parse_pages_arg(const char *str, unsigned long min,
83033c2dcfdSDavid Ahern 			    unsigned long max)
831994a1f78SJiri Olsa {
8322fbe4abeSAdrian Hunter 	unsigned long pages, val;
83327050f53SJiri Olsa 	static struct parse_tag tags[] = {
83427050f53SJiri Olsa 		{ .tag  = 'B', .mult = 1       },
83527050f53SJiri Olsa 		{ .tag  = 'K', .mult = 1 << 10 },
83627050f53SJiri Olsa 		{ .tag  = 'M', .mult = 1 << 20 },
83727050f53SJiri Olsa 		{ .tag  = 'G', .mult = 1 << 30 },
83827050f53SJiri Olsa 		{ .tag  = 0 },
83927050f53SJiri Olsa 	};
840994a1f78SJiri Olsa 
8418973504bSDavid Ahern 	if (str == NULL)
84233c2dcfdSDavid Ahern 		return -EINVAL;
8438973504bSDavid Ahern 
84427050f53SJiri Olsa 	val = parse_tag_value(str, tags);
8452fbe4abeSAdrian Hunter 	if (val != (unsigned long) -1) {
84627050f53SJiri Olsa 		/* we got file size value */
84727050f53SJiri Olsa 		pages = PERF_ALIGN(val, page_size) / page_size;
84827050f53SJiri Olsa 	} else {
84927050f53SJiri Olsa 		/* we got pages count value */
85027050f53SJiri Olsa 		char *eptr;
851994a1f78SJiri Olsa 		pages = strtoul(str, &eptr, 10);
85233c2dcfdSDavid Ahern 		if (*eptr != '\0')
85333c2dcfdSDavid Ahern 			return -EINVAL;
85427050f53SJiri Olsa 	}
855994a1f78SJiri Olsa 
8562bcab6c1SAdrian Hunter 	if (pages == 0 && min == 0) {
85733c2dcfdSDavid Ahern 		/* leave number of pages at 0 */
8581dbfa938SAdrian Hunter 	} else if (!is_power_of_2(pages)) {
8599808143bSJiri Olsa 		char buf[100];
8609808143bSJiri Olsa 
86133c2dcfdSDavid Ahern 		/* round pages up to next power of 2 */
86291529834SArnaldo Carvalho de Melo 		pages = roundup_pow_of_two(pages);
8631dbfa938SAdrian Hunter 		if (!pages)
8641dbfa938SAdrian Hunter 			return -EINVAL;
8659808143bSJiri Olsa 
8669808143bSJiri Olsa 		unit_number__scnprintf(buf, sizeof(buf), pages * page_size);
8679808143bSJiri Olsa 		pr_info("rounding mmap pages size to %s (%lu pages)\n",
8689808143bSJiri Olsa 			buf, pages);
8692fbe4abeSAdrian Hunter 	}
8702fbe4abeSAdrian Hunter 
87133c2dcfdSDavid Ahern 	if (pages > max)
87233c2dcfdSDavid Ahern 		return -EINVAL;
87333c2dcfdSDavid Ahern 
87433c2dcfdSDavid Ahern 	return pages;
87533c2dcfdSDavid Ahern }
87633c2dcfdSDavid Ahern 
87725f84702SArnaldo Carvalho de Melo int __evlist__parse_mmap_pages(unsigned int *mmap_pages, const char *str)
87833c2dcfdSDavid Ahern {
87933c2dcfdSDavid Ahern 	unsigned long max = UINT_MAX;
88033c2dcfdSDavid Ahern 	long pages;
88133c2dcfdSDavid Ahern 
882f5ae9c42SAdrian Hunter 	if (max > SIZE_MAX / page_size)
88333c2dcfdSDavid Ahern 		max = SIZE_MAX / page_size;
88433c2dcfdSDavid Ahern 
88533c2dcfdSDavid Ahern 	pages = parse_pages_arg(str, 1, max);
88633c2dcfdSDavid Ahern 	if (pages < 0) {
88733c2dcfdSDavid Ahern 		pr_err("Invalid argument for --mmap_pages/-m\n");
888994a1f78SJiri Olsa 		return -1;
889994a1f78SJiri Olsa 	}
890994a1f78SJiri Olsa 
891994a1f78SJiri Olsa 	*mmap_pages = pages;
892994a1f78SJiri Olsa 	return 0;
893994a1f78SJiri Olsa }
894994a1f78SJiri Olsa 
89525f84702SArnaldo Carvalho de Melo int evlist__parse_mmap_pages(const struct option *opt, const char *str, int unset __maybe_unused)
896e9db1310SAdrian Hunter {
89725f84702SArnaldo Carvalho de Melo 	return __evlist__parse_mmap_pages(opt->value, str);
898e9db1310SAdrian Hunter }
899e9db1310SAdrian Hunter 
900c83fa7f2SAdrian Hunter /**
9019521b5f2SJiri Olsa  * evlist__mmap_ex - Create mmaps to receive events.
902c83fa7f2SAdrian Hunter  * @evlist: list of events
903c83fa7f2SAdrian Hunter  * @pages: map length in pages
904c83fa7f2SAdrian Hunter  * @overwrite: overwrite older events?
905718c602dSAdrian Hunter  * @auxtrace_pages - auxtrace map length in pages
906718c602dSAdrian Hunter  * @auxtrace_overwrite - overwrite older auxtrace data?
907f8a95309SArnaldo Carvalho de Melo  *
908c83fa7f2SAdrian Hunter  * If @overwrite is %false the user needs to signal event consumption using
9099521b5f2SJiri Olsa  * perf_mmap__write_tail().  Using evlist__mmap_read() does this
910c83fa7f2SAdrian Hunter  * automatically.
911f8a95309SArnaldo Carvalho de Melo  *
912718c602dSAdrian Hunter  * Similarly, if @auxtrace_overwrite is %false the user needs to signal data
913718c602dSAdrian Hunter  * consumption using auxtrace_mmap__write_tail().
914718c602dSAdrian Hunter  *
915c83fa7f2SAdrian Hunter  * Return: %0 on success, negative error code otherwise.
916f8a95309SArnaldo Carvalho de Melo  */
9179521b5f2SJiri Olsa int evlist__mmap_ex(struct evlist *evlist, unsigned int pages,
9187a276ff6SWang Nan 			 unsigned int auxtrace_pages,
91951255a8aSAlexey Budankov 			 bool auxtrace_overwrite, int nr_cblocks, int affinity, int flush,
92051255a8aSAlexey Budankov 			 int comp_level)
921f8a95309SArnaldo Carvalho de Melo {
92271f566a3SWang Nan 	/*
92371f566a3SWang Nan 	 * Delay setting mp.prot: set it before calling perf_mmap__mmap.
92471f566a3SWang Nan 	 * Its value is decided by evsel's write_backward.
92571f566a3SWang Nan 	 * So &mp should not be passed through const pointer.
92671f566a3SWang Nan 	 */
927e440979fSJiri Olsa 	struct mmap_params mp = {
928e440979fSJiri Olsa 		.nr_cblocks	= nr_cblocks,
929e440979fSJiri Olsa 		.affinity	= affinity,
930e440979fSJiri Olsa 		.flush		= flush,
931e440979fSJiri Olsa 		.comp_level	= comp_level
932e440979fSJiri Olsa 	};
933923d0f18SJiri Olsa 	struct perf_evlist_mmap_ops ops = {
9349abd2ab2SJiri Olsa 		.idx  = perf_evlist__mmap_cb_idx,
935bb1b1885SJiri Olsa 		.get  = perf_evlist__mmap_cb_get,
936b80132b1SJiri Olsa 		.mmap = perf_evlist__mmap_cb_mmap,
9379abd2ab2SJiri Olsa 	};
93850a682ceSArnaldo Carvalho de Melo 
939f6fa4375SJiri Olsa 	evlist->core.mmap_len = evlist__mmap_size(pages);
940f6fa4375SJiri Olsa 	pr_debug("mmap size %zuB\n", evlist->core.mmap_len);
941f8a95309SArnaldo Carvalho de Melo 
942f6fa4375SJiri Olsa 	auxtrace_mmap_params__init(&mp.auxtrace_mp, evlist->core.mmap_len,
943718c602dSAdrian Hunter 				   auxtrace_pages, auxtrace_overwrite);
944718c602dSAdrian Hunter 
945923d0f18SJiri Olsa 	return perf_evlist__mmap_ops(&evlist->core, &ops, &mp.core);
946f8a95309SArnaldo Carvalho de Melo }
9477e2ed097SArnaldo Carvalho de Melo 
9489521b5f2SJiri Olsa int evlist__mmap(struct evlist *evlist, unsigned int pages)
949718c602dSAdrian Hunter {
9509521b5f2SJiri Olsa 	return evlist__mmap_ex(evlist, pages, 0, false, 0, PERF_AFFINITY_SYS, 1, 0);
951718c602dSAdrian Hunter }
952718c602dSAdrian Hunter 
9537748bb71SArnaldo Carvalho de Melo int evlist__create_maps(struct evlist *evlist, struct target *target)
9547e2ed097SArnaldo Carvalho de Melo {
955147c508fSJin Yao 	bool all_threads = (target->per_thread && target->system_wide);
956f854839bSJiri Olsa 	struct perf_cpu_map *cpus;
9579749b90eSJiri Olsa 	struct perf_thread_map *threads;
9587e2ed097SArnaldo Carvalho de Melo 
959147c508fSJin Yao 	/*
960147c508fSJin Yao 	 * If specify '-a' and '--per-thread' to perf record, perf record
961147c508fSJin Yao 	 * will override '--per-thread'. target->per_thread = false and
962147c508fSJin Yao 	 * target->system_wide = true.
963147c508fSJin Yao 	 *
964147c508fSJin Yao 	 * If specify '--per-thread' only to perf record,
965147c508fSJin Yao 	 * target->per_thread = true and target->system_wide = false.
966147c508fSJin Yao 	 *
967147c508fSJin Yao 	 * So target->per_thread && target->system_wide is false.
968147c508fSJin Yao 	 * For perf record, thread_map__new_str doesn't call
969147c508fSJin Yao 	 * thread_map__new_all_cpus. That will keep perf record's
970147c508fSJin Yao 	 * current behavior.
971147c508fSJin Yao 	 *
972147c508fSJin Yao 	 * For perf stat, it allows the case that target->per_thread and
973147c508fSJin Yao 	 * target->system_wide are all true. It means to collect system-wide
974147c508fSJin Yao 	 * per-thread data. thread_map__new_str will call
975147c508fSJin Yao 	 * thread_map__new_all_cpus to enumerate all threads.
976147c508fSJin Yao 	 */
97773c0ca1eSJin Yao 	threads = thread_map__new_str(target->pid, target->tid, target->uid,
978147c508fSJin Yao 				      all_threads);
97974bfd2b2SAdrian Hunter 
98074bfd2b2SAdrian Hunter 	if (!threads)
9817e2ed097SArnaldo Carvalho de Melo 		return -1;
9827e2ed097SArnaldo Carvalho de Melo 
9839c105fbcSDongsheng Yang 	if (target__uses_dummy_map(target))
984397721e0SJiri Olsa 		cpus = perf_cpu_map__dummy_new();
985879d77d0SNamhyung Kim 	else
9869c3516d1SJiri Olsa 		cpus = perf_cpu_map__new(target->cpu_list);
9877e2ed097SArnaldo Carvalho de Melo 
98874bfd2b2SAdrian Hunter 	if (!cpus)
9897e2ed097SArnaldo Carvalho de Melo 		goto out_delete_threads;
9907e2ed097SArnaldo Carvalho de Melo 
991b726e363SJin Yao 	evlist->core.has_user_cpus = !!target->cpu_list && !target->hybrid;
992ec9a77a7SAdrian Hunter 
993453fa030SJiri Olsa 	perf_evlist__set_maps(&evlist->core, cpus, threads);
994d5bc056eSAdrian Hunter 
995bfd1b83dSNamhyung Kim 	/* as evlist now has references, put count here */
996bfd1b83dSNamhyung Kim 	perf_cpu_map__put(cpus);
997bfd1b83dSNamhyung Kim 	perf_thread_map__put(threads);
998bfd1b83dSNamhyung Kim 
999d5bc056eSAdrian Hunter 	return 0;
10007e2ed097SArnaldo Carvalho de Melo 
10017e2ed097SArnaldo Carvalho de Melo out_delete_threads:
10027836e52eSJiri Olsa 	perf_thread_map__put(threads);
10037e2ed097SArnaldo Carvalho de Melo 	return -1;
10047e2ed097SArnaldo Carvalho de Melo }
10057e2ed097SArnaldo Carvalho de Melo 
100624bf91a7SArnaldo Carvalho de Melo int evlist__apply_filters(struct evlist *evlist, struct evsel **err_evsel)
10070a102479SFrederic Weisbecker {
100832dcd021SJiri Olsa 	struct evsel *evsel;
1009745cefc5SArnaldo Carvalho de Melo 	int err = 0;
10100a102479SFrederic Weisbecker 
1011e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, evsel) {
1012745cefc5SArnaldo Carvalho de Melo 		if (evsel->filter == NULL)
10130a102479SFrederic Weisbecker 			continue;
1014745cefc5SArnaldo Carvalho de Melo 
1015d988d5eeSKan Liang 		/*
1016d988d5eeSKan Liang 		 * filters only work for tracepoint event, which doesn't have cpu limit.
1017d988d5eeSKan Liang 		 * So evlist and evsel should always be same.
1018d988d5eeSKan Liang 		 */
1019a00571fdSJiri Olsa 		err = perf_evsel__apply_filter(&evsel->core, evsel->filter);
102023d4aad4SArnaldo Carvalho de Melo 		if (err) {
102123d4aad4SArnaldo Carvalho de Melo 			*err_evsel = evsel;
1022745cefc5SArnaldo Carvalho de Melo 			break;
10230a102479SFrederic Weisbecker 		}
102423d4aad4SArnaldo Carvalho de Melo 	}
10250a102479SFrederic Weisbecker 
1026745cefc5SArnaldo Carvalho de Melo 	return err;
1027745cefc5SArnaldo Carvalho de Melo }
1028745cefc5SArnaldo Carvalho de Melo 
102924bf91a7SArnaldo Carvalho de Melo int evlist__set_tp_filter(struct evlist *evlist, const char *filter)
1030745cefc5SArnaldo Carvalho de Melo {
103132dcd021SJiri Olsa 	struct evsel *evsel;
1032745cefc5SArnaldo Carvalho de Melo 	int err = 0;
1033745cefc5SArnaldo Carvalho de Melo 
103405cea449SArnaldo Carvalho de Melo 	if (filter == NULL)
103505cea449SArnaldo Carvalho de Melo 		return -1;
103605cea449SArnaldo Carvalho de Melo 
1037e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, evsel) {
10381fc632ceSJiri Olsa 		if (evsel->core.attr.type != PERF_TYPE_TRACEPOINT)
1039fdf14720SWang Nan 			continue;
1040fdf14720SWang Nan 
1041ad681adfSArnaldo Carvalho de Melo 		err = evsel__set_filter(evsel, filter);
1042745cefc5SArnaldo Carvalho de Melo 		if (err)
1043745cefc5SArnaldo Carvalho de Melo 			break;
1044745cefc5SArnaldo Carvalho de Melo 	}
1045745cefc5SArnaldo Carvalho de Melo 
1046745cefc5SArnaldo Carvalho de Melo 	return err;
10470a102479SFrederic Weisbecker }
104874429964SFrederic Weisbecker 
104924bf91a7SArnaldo Carvalho de Melo int evlist__append_tp_filter(struct evlist *evlist, const char *filter)
105053c92f73SArnaldo Carvalho de Melo {
105153c92f73SArnaldo Carvalho de Melo 	struct evsel *evsel;
105253c92f73SArnaldo Carvalho de Melo 	int err = 0;
105353c92f73SArnaldo Carvalho de Melo 
105453c92f73SArnaldo Carvalho de Melo 	if (filter == NULL)
105553c92f73SArnaldo Carvalho de Melo 		return -1;
105653c92f73SArnaldo Carvalho de Melo 
105753c92f73SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, evsel) {
105853c92f73SArnaldo Carvalho de Melo 		if (evsel->core.attr.type != PERF_TYPE_TRACEPOINT)
105953c92f73SArnaldo Carvalho de Melo 			continue;
106053c92f73SArnaldo Carvalho de Melo 
1061ad681adfSArnaldo Carvalho de Melo 		err = evsel__append_tp_filter(evsel, filter);
106253c92f73SArnaldo Carvalho de Melo 		if (err)
106353c92f73SArnaldo Carvalho de Melo 			break;
106453c92f73SArnaldo Carvalho de Melo 	}
106553c92f73SArnaldo Carvalho de Melo 
106653c92f73SArnaldo Carvalho de Melo 	return err;
106753c92f73SArnaldo Carvalho de Melo }
106853c92f73SArnaldo Carvalho de Melo 
1069da949f50SArnaldo Carvalho de Melo char *asprintf__tp_filter_pids(size_t npids, pid_t *pids)
1070cfd70a26SArnaldo Carvalho de Melo {
1071cfd70a26SArnaldo Carvalho de Melo 	char *filter;
1072be199adaSArnaldo Carvalho de Melo 	size_t i;
1073cfd70a26SArnaldo Carvalho de Melo 
1074be199adaSArnaldo Carvalho de Melo 	for (i = 0; i < npids; ++i) {
1075be199adaSArnaldo Carvalho de Melo 		if (i == 0) {
1076be199adaSArnaldo Carvalho de Melo 			if (asprintf(&filter, "common_pid != %d", pids[i]) < 0)
107705cea449SArnaldo Carvalho de Melo 				return NULL;
1078be199adaSArnaldo Carvalho de Melo 		} else {
1079be199adaSArnaldo Carvalho de Melo 			char *tmp;
1080be199adaSArnaldo Carvalho de Melo 
1081be199adaSArnaldo Carvalho de Melo 			if (asprintf(&tmp, "%s && common_pid != %d", filter, pids[i]) < 0)
1082be199adaSArnaldo Carvalho de Melo 				goto out_free;
1083be199adaSArnaldo Carvalho de Melo 
1084be199adaSArnaldo Carvalho de Melo 			free(filter);
1085be199adaSArnaldo Carvalho de Melo 			filter = tmp;
1086be199adaSArnaldo Carvalho de Melo 		}
1087be199adaSArnaldo Carvalho de Melo 	}
1088cfd70a26SArnaldo Carvalho de Melo 
108905cea449SArnaldo Carvalho de Melo 	return filter;
1090be199adaSArnaldo Carvalho de Melo out_free:
1091cfd70a26SArnaldo Carvalho de Melo 	free(filter);
109205cea449SArnaldo Carvalho de Melo 	return NULL;
109305cea449SArnaldo Carvalho de Melo }
109405cea449SArnaldo Carvalho de Melo 
109524bf91a7SArnaldo Carvalho de Melo int evlist__set_tp_filter_pids(struct evlist *evlist, size_t npids, pid_t *pids)
109605cea449SArnaldo Carvalho de Melo {
109705cea449SArnaldo Carvalho de Melo 	char *filter = asprintf__tp_filter_pids(npids, pids);
109824bf91a7SArnaldo Carvalho de Melo 	int ret = evlist__set_tp_filter(evlist, filter);
109905cea449SArnaldo Carvalho de Melo 
110005cea449SArnaldo Carvalho de Melo 	free(filter);
1101cfd70a26SArnaldo Carvalho de Melo 	return ret;
1102cfd70a26SArnaldo Carvalho de Melo }
1103cfd70a26SArnaldo Carvalho de Melo 
110424bf91a7SArnaldo Carvalho de Melo int evlist__set_tp_filter_pid(struct evlist *evlist, pid_t pid)
1105be199adaSArnaldo Carvalho de Melo {
110624bf91a7SArnaldo Carvalho de Melo 	return evlist__set_tp_filter_pids(evlist, 1, &pid);
1107be199adaSArnaldo Carvalho de Melo }
1108be199adaSArnaldo Carvalho de Melo 
110924bf91a7SArnaldo Carvalho de Melo int evlist__append_tp_filter_pids(struct evlist *evlist, size_t npids, pid_t *pids)
11101827ab5bSArnaldo Carvalho de Melo {
11111827ab5bSArnaldo Carvalho de Melo 	char *filter = asprintf__tp_filter_pids(npids, pids);
111224bf91a7SArnaldo Carvalho de Melo 	int ret = evlist__append_tp_filter(evlist, filter);
11131827ab5bSArnaldo Carvalho de Melo 
11141827ab5bSArnaldo Carvalho de Melo 	free(filter);
11151827ab5bSArnaldo Carvalho de Melo 	return ret;
11161827ab5bSArnaldo Carvalho de Melo }
11171827ab5bSArnaldo Carvalho de Melo 
111824bf91a7SArnaldo Carvalho de Melo int evlist__append_tp_filter_pid(struct evlist *evlist, pid_t pid)
11191827ab5bSArnaldo Carvalho de Melo {
112024bf91a7SArnaldo Carvalho de Melo 	return evlist__append_tp_filter_pids(evlist, 1, &pid);
11211827ab5bSArnaldo Carvalho de Melo }
11221827ab5bSArnaldo Carvalho de Melo 
1123b3c2cc2bSArnaldo Carvalho de Melo bool evlist__valid_sample_type(struct evlist *evlist)
112474429964SFrederic Weisbecker {
112532dcd021SJiri Olsa 	struct evsel *pos;
1126c2a70653SArnaldo Carvalho de Melo 
11276484d2f9SJiri Olsa 	if (evlist->core.nr_entries == 1)
112875562573SAdrian Hunter 		return true;
112975562573SAdrian Hunter 
113075562573SAdrian Hunter 	if (evlist->id_pos < 0 || evlist->is_pos < 0)
113175562573SAdrian Hunter 		return false;
113275562573SAdrian Hunter 
1133e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, pos) {
113475562573SAdrian Hunter 		if (pos->id_pos != evlist->id_pos ||
113575562573SAdrian Hunter 		    pos->is_pos != evlist->is_pos)
1136c2a70653SArnaldo Carvalho de Melo 			return false;
113774429964SFrederic Weisbecker 	}
113874429964SFrederic Weisbecker 
1139c2a70653SArnaldo Carvalho de Melo 	return true;
1140c2a70653SArnaldo Carvalho de Melo }
1141c2a70653SArnaldo Carvalho de Melo 
1142b3c2cc2bSArnaldo Carvalho de Melo u64 __evlist__combined_sample_type(struct evlist *evlist)
1143c2a70653SArnaldo Carvalho de Melo {
114432dcd021SJiri Olsa 	struct evsel *evsel;
114575562573SAdrian Hunter 
114675562573SAdrian Hunter 	if (evlist->combined_sample_type)
114775562573SAdrian Hunter 		return evlist->combined_sample_type;
114875562573SAdrian Hunter 
1149e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, evsel)
11501fc632ceSJiri Olsa 		evlist->combined_sample_type |= evsel->core.attr.sample_type;
115175562573SAdrian Hunter 
115275562573SAdrian Hunter 	return evlist->combined_sample_type;
115375562573SAdrian Hunter }
115475562573SAdrian Hunter 
1155b3c2cc2bSArnaldo Carvalho de Melo u64 evlist__combined_sample_type(struct evlist *evlist)
115675562573SAdrian Hunter {
115775562573SAdrian Hunter 	evlist->combined_sample_type = 0;
1158b3c2cc2bSArnaldo Carvalho de Melo 	return __evlist__combined_sample_type(evlist);
1159c2a70653SArnaldo Carvalho de Melo }
1160c2a70653SArnaldo Carvalho de Melo 
116192c7d7cdSArnaldo Carvalho de Melo u64 evlist__combined_branch_type(struct evlist *evlist)
116298df858eSAndi Kleen {
116332dcd021SJiri Olsa 	struct evsel *evsel;
116498df858eSAndi Kleen 	u64 branch_type = 0;
116598df858eSAndi Kleen 
1166e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, evsel)
11671fc632ceSJiri Olsa 		branch_type |= evsel->core.attr.branch_sample_type;
116898df858eSAndi Kleen 	return branch_type;
116998df858eSAndi Kleen }
117098df858eSAndi Kleen 
117178e1bc25SArnaldo Carvalho de Melo bool evlist__valid_read_format(struct evlist *evlist)
11729ede473cSJiri Olsa {
1173515dbe48SJiri Olsa 	struct evsel *first = evlist__first(evlist), *pos = first;
11741fc632ceSJiri Olsa 	u64 read_format = first->core.attr.read_format;
11751fc632ceSJiri Olsa 	u64 sample_type = first->core.attr.sample_type;
11769ede473cSJiri Olsa 
1177e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, pos) {
117894d3820fSAdrian Hunter 		if (read_format != pos->core.attr.read_format) {
117994d3820fSAdrian Hunter 			pr_debug("Read format differs %#" PRIx64 " vs %#" PRIx64 "\n",
118094d3820fSAdrian Hunter 				 read_format, (u64)pos->core.attr.read_format);
118194d3820fSAdrian Hunter 		}
11829ede473cSJiri Olsa 	}
11839ede473cSJiri Olsa 
11844d39c89fSIngo Molnar 	/* PERF_SAMPLE_READ implies PERF_FORMAT_ID. */
11859ede473cSJiri Olsa 	if ((sample_type & PERF_SAMPLE_READ) &&
11869ede473cSJiri Olsa 	    !(read_format & PERF_FORMAT_ID)) {
11879ede473cSJiri Olsa 		return false;
11889ede473cSJiri Olsa 	}
11899ede473cSJiri Olsa 
11909ede473cSJiri Olsa 	return true;
11919ede473cSJiri Olsa }
11929ede473cSJiri Olsa 
11931420ba2fSArnaldo Carvalho de Melo u16 evlist__id_hdr_size(struct evlist *evlist)
119481e36bffSArnaldo Carvalho de Melo {
1195515dbe48SJiri Olsa 	struct evsel *first = evlist__first(evlist);
119681e36bffSArnaldo Carvalho de Melo 	struct perf_sample *data;
119781e36bffSArnaldo Carvalho de Melo 	u64 sample_type;
119881e36bffSArnaldo Carvalho de Melo 	u16 size = 0;
119981e36bffSArnaldo Carvalho de Melo 
12001fc632ceSJiri Olsa 	if (!first->core.attr.sample_id_all)
120181e36bffSArnaldo Carvalho de Melo 		goto out;
120281e36bffSArnaldo Carvalho de Melo 
12031fc632ceSJiri Olsa 	sample_type = first->core.attr.sample_type;
120481e36bffSArnaldo Carvalho de Melo 
120581e36bffSArnaldo Carvalho de Melo 	if (sample_type & PERF_SAMPLE_TID)
120681e36bffSArnaldo Carvalho de Melo 		size += sizeof(data->tid) * 2;
120781e36bffSArnaldo Carvalho de Melo 
120881e36bffSArnaldo Carvalho de Melo        if (sample_type & PERF_SAMPLE_TIME)
120981e36bffSArnaldo Carvalho de Melo 		size += sizeof(data->time);
121081e36bffSArnaldo Carvalho de Melo 
121181e36bffSArnaldo Carvalho de Melo 	if (sample_type & PERF_SAMPLE_ID)
121281e36bffSArnaldo Carvalho de Melo 		size += sizeof(data->id);
121381e36bffSArnaldo Carvalho de Melo 
121481e36bffSArnaldo Carvalho de Melo 	if (sample_type & PERF_SAMPLE_STREAM_ID)
121581e36bffSArnaldo Carvalho de Melo 		size += sizeof(data->stream_id);
121681e36bffSArnaldo Carvalho de Melo 
121781e36bffSArnaldo Carvalho de Melo 	if (sample_type & PERF_SAMPLE_CPU)
121881e36bffSArnaldo Carvalho de Melo 		size += sizeof(data->cpu) * 2;
121975562573SAdrian Hunter 
122075562573SAdrian Hunter 	if (sample_type & PERF_SAMPLE_IDENTIFIER)
122175562573SAdrian Hunter 		size += sizeof(data->id);
122281e36bffSArnaldo Carvalho de Melo out:
122381e36bffSArnaldo Carvalho de Melo 	return size;
122481e36bffSArnaldo Carvalho de Melo }
122581e36bffSArnaldo Carvalho de Melo 
12268cedf3a5SArnaldo Carvalho de Melo bool evlist__valid_sample_id_all(struct evlist *evlist)
1227c2a70653SArnaldo Carvalho de Melo {
1228515dbe48SJiri Olsa 	struct evsel *first = evlist__first(evlist), *pos = first;
1229c2a70653SArnaldo Carvalho de Melo 
1230e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry_continue(evlist, pos) {
12311fc632ceSJiri Olsa 		if (first->core.attr.sample_id_all != pos->core.attr.sample_id_all)
1232c2a70653SArnaldo Carvalho de Melo 			return false;
1233c2a70653SArnaldo Carvalho de Melo 	}
1234c2a70653SArnaldo Carvalho de Melo 
1235c2a70653SArnaldo Carvalho de Melo 	return true;
123674429964SFrederic Weisbecker }
123774429964SFrederic Weisbecker 
12388cedf3a5SArnaldo Carvalho de Melo bool evlist__sample_id_all(struct evlist *evlist)
123974429964SFrederic Weisbecker {
1240515dbe48SJiri Olsa 	struct evsel *first = evlist__first(evlist);
12411fc632ceSJiri Olsa 	return first->core.attr.sample_id_all;
124274429964SFrederic Weisbecker }
124381cce8deSArnaldo Carvalho de Melo 
1244900c8eadSArnaldo Carvalho de Melo void evlist__set_selected(struct evlist *evlist, struct evsel *evsel)
124581cce8deSArnaldo Carvalho de Melo {
124681cce8deSArnaldo Carvalho de Melo 	evlist->selected = evsel;
124781cce8deSArnaldo Carvalho de Melo }
1248727ab04eSArnaldo Carvalho de Melo 
1249750b4edeSJiri Olsa void evlist__close(struct evlist *evlist)
1250a74b4b66SNamhyung Kim {
125132dcd021SJiri Olsa 	struct evsel *evsel;
1252472832d2SIan Rogers 	struct evlist_cpu_iterator evlist_cpu_itr;
12537736627bSAndi Kleen 	struct affinity affinity;
1254a74b4b66SNamhyung Kim 
12557736627bSAndi Kleen 	/*
12560df6ade7SIan Rogers 	 * With perf record core.user_requested_cpus is usually NULL.
12577736627bSAndi Kleen 	 * Use the old method to handle this for now.
12587736627bSAndi Kleen 	 */
12590df6ade7SIan Rogers 	if (!evlist->core.user_requested_cpus ||
12600df6ade7SIan Rogers 	    cpu_map__is_dummy(evlist->core.user_requested_cpus)) {
1261475fb533SAndi Kleen 		evlist__for_each_entry_reverse(evlist, evsel)
126288761fa1SJiri Olsa 			evsel__close(evsel);
12637736627bSAndi Kleen 		return;
12647736627bSAndi Kleen 	}
12657736627bSAndi Kleen 
12667736627bSAndi Kleen 	if (affinity__setup(&affinity) < 0)
12677736627bSAndi Kleen 		return;
12687736627bSAndi Kleen 
1269472832d2SIan Rogers 	evlist__for_each_cpu(evlist_cpu_itr, evlist, &affinity) {
1270472832d2SIan Rogers 		perf_evsel__close_cpu(&evlist_cpu_itr.evsel->core,
1271472832d2SIan Rogers 				      evlist_cpu_itr.cpu_map_idx);
12727736627bSAndi Kleen 	}
1273472832d2SIan Rogers 
12747736627bSAndi Kleen 	affinity__cleanup(&affinity);
12757736627bSAndi Kleen 	evlist__for_each_entry_reverse(evlist, evsel) {
12767736627bSAndi Kleen 		perf_evsel__free_fd(&evsel->core);
12777736627bSAndi Kleen 		perf_evsel__free_id(&evsel->core);
12787736627bSAndi Kleen 	}
1279513068f2SNamhyung Kim 	perf_evlist__reset_id_hash(&evlist->core);
1280a74b4b66SNamhyung Kim }
1281a74b4b66SNamhyung Kim 
12827748bb71SArnaldo Carvalho de Melo static int evlist__create_syswide_maps(struct evlist *evlist)
12834112eb18SArnaldo Carvalho de Melo {
1284f854839bSJiri Olsa 	struct perf_cpu_map *cpus;
12859749b90eSJiri Olsa 	struct perf_thread_map *threads;
12864112eb18SArnaldo Carvalho de Melo 
12874112eb18SArnaldo Carvalho de Melo 	/*
12884112eb18SArnaldo Carvalho de Melo 	 * Try reading /sys/devices/system/cpu/online to get
12894112eb18SArnaldo Carvalho de Melo 	 * an all cpus map.
12904112eb18SArnaldo Carvalho de Melo 	 *
12914112eb18SArnaldo Carvalho de Melo 	 * FIXME: -ENOMEM is the best we can do here, the cpu_map
12924112eb18SArnaldo Carvalho de Melo 	 * code needs an overhaul to properly forward the
12934112eb18SArnaldo Carvalho de Melo 	 * error, and we may not want to do that fallback to a
12944112eb18SArnaldo Carvalho de Melo 	 * default cpu identity map :-\
12954112eb18SArnaldo Carvalho de Melo 	 */
12969c3516d1SJiri Olsa 	cpus = perf_cpu_map__new(NULL);
12978c0498b6SAdrian Hunter 	if (!cpus)
12984112eb18SArnaldo Carvalho de Melo 		goto out;
12994112eb18SArnaldo Carvalho de Melo 
13004b49cce2SJiri Olsa 	threads = perf_thread_map__new_dummy();
13018c0498b6SAdrian Hunter 	if (!threads)
13028c0498b6SAdrian Hunter 		goto out_put;
13034112eb18SArnaldo Carvalho de Melo 
1304453fa030SJiri Olsa 	perf_evlist__set_maps(&evlist->core, cpus, threads);
1305bfd1b83dSNamhyung Kim 
1306bfd1b83dSNamhyung Kim 	perf_thread_map__put(threads);
13078c0498b6SAdrian Hunter out_put:
130838f01d8dSJiri Olsa 	perf_cpu_map__put(cpus);
1309bfd1b83dSNamhyung Kim out:
1310f717d89aSHaowen Bai 	return -ENOMEM;
13114112eb18SArnaldo Carvalho de Melo }
13124112eb18SArnaldo Carvalho de Melo 
1313474ddc4cSJiri Olsa int evlist__open(struct evlist *evlist)
1314727ab04eSArnaldo Carvalho de Melo {
131532dcd021SJiri Olsa 	struct evsel *evsel;
1316a74b4b66SNamhyung Kim 	int err;
1317727ab04eSArnaldo Carvalho de Melo 
13184112eb18SArnaldo Carvalho de Melo 	/*
13194112eb18SArnaldo Carvalho de Melo 	 * Default: one fd per CPU, all threads, aka systemwide
13204112eb18SArnaldo Carvalho de Melo 	 * as sys_perf_event_open(cpu = -1, thread = -1) is EINVAL
13214112eb18SArnaldo Carvalho de Melo 	 */
13220df6ade7SIan Rogers 	if (evlist->core.threads == NULL && evlist->core.user_requested_cpus == NULL) {
13237748bb71SArnaldo Carvalho de Melo 		err = evlist__create_syswide_maps(evlist);
13244112eb18SArnaldo Carvalho de Melo 		if (err < 0)
13254112eb18SArnaldo Carvalho de Melo 			goto out_err;
13264112eb18SArnaldo Carvalho de Melo 	}
13274112eb18SArnaldo Carvalho de Melo 
13280a7e7ec9SArnaldo Carvalho de Melo 	evlist__update_id_pos(evlist);
1329733cd2feSAdrian Hunter 
1330e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, evsel) {
1331af663bd0SJiri Olsa 		err = evsel__open(evsel, evsel->core.cpus, evsel->core.threads);
1332727ab04eSArnaldo Carvalho de Melo 		if (err < 0)
1333727ab04eSArnaldo Carvalho de Melo 			goto out_err;
1334727ab04eSArnaldo Carvalho de Melo 	}
1335727ab04eSArnaldo Carvalho de Melo 
1336727ab04eSArnaldo Carvalho de Melo 	return 0;
1337727ab04eSArnaldo Carvalho de Melo out_err:
1338750b4edeSJiri Olsa 	evlist__close(evlist);
133941c21a68SNamhyung Kim 	errno = -err;
1340727ab04eSArnaldo Carvalho de Melo 	return err;
1341727ab04eSArnaldo Carvalho de Melo }
134235b9d88eSArnaldo Carvalho de Melo 
13437b392ef0SArnaldo Carvalho de Melo int evlist__prepare_workload(struct evlist *evlist, struct target *target, const char *argv[],
13447b392ef0SArnaldo Carvalho de Melo 			     bool pipe_output, void (*exec_error)(int signo, siginfo_t *info, void *ucontext))
134535b9d88eSArnaldo Carvalho de Melo {
134635b9d88eSArnaldo Carvalho de Melo 	int child_ready_pipe[2], go_pipe[2];
134735b9d88eSArnaldo Carvalho de Melo 	char bf;
134835b9d88eSArnaldo Carvalho de Melo 
134935b9d88eSArnaldo Carvalho de Melo 	if (pipe(child_ready_pipe) < 0) {
135035b9d88eSArnaldo Carvalho de Melo 		perror("failed to create 'ready' pipe");
135135b9d88eSArnaldo Carvalho de Melo 		return -1;
135235b9d88eSArnaldo Carvalho de Melo 	}
135335b9d88eSArnaldo Carvalho de Melo 
135435b9d88eSArnaldo Carvalho de Melo 	if (pipe(go_pipe) < 0) {
135535b9d88eSArnaldo Carvalho de Melo 		perror("failed to create 'go' pipe");
135635b9d88eSArnaldo Carvalho de Melo 		goto out_close_ready_pipe;
135735b9d88eSArnaldo Carvalho de Melo 	}
135835b9d88eSArnaldo Carvalho de Melo 
135935b9d88eSArnaldo Carvalho de Melo 	evlist->workload.pid = fork();
136035b9d88eSArnaldo Carvalho de Melo 	if (evlist->workload.pid < 0) {
136135b9d88eSArnaldo Carvalho de Melo 		perror("failed to fork");
136235b9d88eSArnaldo Carvalho de Melo 		goto out_close_pipes;
136335b9d88eSArnaldo Carvalho de Melo 	}
136435b9d88eSArnaldo Carvalho de Melo 
136535b9d88eSArnaldo Carvalho de Melo 	if (!evlist->workload.pid) {
13665f1c4225SArnaldo Carvalho de Melo 		int ret;
13675f1c4225SArnaldo Carvalho de Melo 
1368119fa3c9SNamhyung Kim 		if (pipe_output)
136935b9d88eSArnaldo Carvalho de Melo 			dup2(2, 1);
137035b9d88eSArnaldo Carvalho de Melo 
13710817df08SDavid Ahern 		signal(SIGTERM, SIG_DFL);
13720817df08SDavid Ahern 
137335b9d88eSArnaldo Carvalho de Melo 		close(child_ready_pipe[0]);
137435b9d88eSArnaldo Carvalho de Melo 		close(go_pipe[1]);
137535b9d88eSArnaldo Carvalho de Melo 		fcntl(go_pipe[0], F_SETFD, FD_CLOEXEC);
137635b9d88eSArnaldo Carvalho de Melo 
137735b9d88eSArnaldo Carvalho de Melo 		/*
1378a7672d1dSArnaldo Carvalho de Melo 		 * Change the name of this process not to confuse --exclude-perf users
1379a7672d1dSArnaldo Carvalho de Melo 		 * that sees 'perf' in the window up to the execvp() and thinks that
1380a7672d1dSArnaldo Carvalho de Melo 		 * perf samples are not being excluded.
1381a7672d1dSArnaldo Carvalho de Melo 		 */
1382a7672d1dSArnaldo Carvalho de Melo 		prctl(PR_SET_NAME, "perf-exec");
1383a7672d1dSArnaldo Carvalho de Melo 
1384a7672d1dSArnaldo Carvalho de Melo 		/*
138535b9d88eSArnaldo Carvalho de Melo 		 * Tell the parent we're ready to go
138635b9d88eSArnaldo Carvalho de Melo 		 */
138735b9d88eSArnaldo Carvalho de Melo 		close(child_ready_pipe[1]);
138835b9d88eSArnaldo Carvalho de Melo 
138935b9d88eSArnaldo Carvalho de Melo 		/*
139035b9d88eSArnaldo Carvalho de Melo 		 * Wait until the parent tells us to go.
139135b9d88eSArnaldo Carvalho de Melo 		 */
13925f1c4225SArnaldo Carvalho de Melo 		ret = read(go_pipe[0], &bf, 1);
13935f1c4225SArnaldo Carvalho de Melo 		/*
13945f1c4225SArnaldo Carvalho de Melo 		 * The parent will ask for the execvp() to be performed by
13955f1c4225SArnaldo Carvalho de Melo 		 * writing exactly one byte, in workload.cork_fd, usually via
13967b392ef0SArnaldo Carvalho de Melo 		 * evlist__start_workload().
13975f1c4225SArnaldo Carvalho de Melo 		 *
139820f86fc1SArnaldo Carvalho de Melo 		 * For cancelling the workload without actually running it,
13995f1c4225SArnaldo Carvalho de Melo 		 * the parent will just close workload.cork_fd, without writing
14005f1c4225SArnaldo Carvalho de Melo 		 * anything, i.e. read will return zero and we just exit()
14015f1c4225SArnaldo Carvalho de Melo 		 * here.
14025f1c4225SArnaldo Carvalho de Melo 		 */
14035f1c4225SArnaldo Carvalho de Melo 		if (ret != 1) {
14045f1c4225SArnaldo Carvalho de Melo 			if (ret == -1)
140535b9d88eSArnaldo Carvalho de Melo 				perror("unable to read pipe");
14065f1c4225SArnaldo Carvalho de Melo 			exit(ret);
14075f1c4225SArnaldo Carvalho de Melo 		}
140835b9d88eSArnaldo Carvalho de Melo 
140935b9d88eSArnaldo Carvalho de Melo 		execvp(argv[0], (char **)argv);
141035b9d88eSArnaldo Carvalho de Melo 
1411735f7e0bSArnaldo Carvalho de Melo 		if (exec_error) {
1412f33cbe72SArnaldo Carvalho de Melo 			union sigval val;
1413f33cbe72SArnaldo Carvalho de Melo 
1414f33cbe72SArnaldo Carvalho de Melo 			val.sival_int = errno;
1415f33cbe72SArnaldo Carvalho de Melo 			if (sigqueue(getppid(), SIGUSR1, val))
141635b9d88eSArnaldo Carvalho de Melo 				perror(argv[0]);
1417f33cbe72SArnaldo Carvalho de Melo 		} else
1418f33cbe72SArnaldo Carvalho de Melo 			perror(argv[0]);
141935b9d88eSArnaldo Carvalho de Melo 		exit(-1);
142035b9d88eSArnaldo Carvalho de Melo 	}
142135b9d88eSArnaldo Carvalho de Melo 
1422735f7e0bSArnaldo Carvalho de Melo 	if (exec_error) {
1423735f7e0bSArnaldo Carvalho de Melo 		struct sigaction act = {
1424735f7e0bSArnaldo Carvalho de Melo 			.sa_flags     = SA_SIGINFO,
1425735f7e0bSArnaldo Carvalho de Melo 			.sa_sigaction = exec_error,
1426735f7e0bSArnaldo Carvalho de Melo 		};
1427735f7e0bSArnaldo Carvalho de Melo 		sigaction(SIGUSR1, &act, NULL);
1428735f7e0bSArnaldo Carvalho de Melo 	}
1429735f7e0bSArnaldo Carvalho de Melo 
14301aaf63b1SArnaldo Carvalho de Melo 	if (target__none(target)) {
143103617c22SJiri Olsa 		if (evlist->core.threads == NULL) {
14321aaf63b1SArnaldo Carvalho de Melo 			fprintf(stderr, "FATAL: evlist->threads need to be set at this point (%s:%d).\n",
14331aaf63b1SArnaldo Carvalho de Melo 				__func__, __LINE__);
14341aaf63b1SArnaldo Carvalho de Melo 			goto out_close_pipes;
14351aaf63b1SArnaldo Carvalho de Melo 		}
143603617c22SJiri Olsa 		perf_thread_map__set_pid(evlist->core.threads, 0, evlist->workload.pid);
14371aaf63b1SArnaldo Carvalho de Melo 	}
143835b9d88eSArnaldo Carvalho de Melo 
143935b9d88eSArnaldo Carvalho de Melo 	close(child_ready_pipe[1]);
144035b9d88eSArnaldo Carvalho de Melo 	close(go_pipe[0]);
144135b9d88eSArnaldo Carvalho de Melo 	/*
144235b9d88eSArnaldo Carvalho de Melo 	 * wait for child to settle
144335b9d88eSArnaldo Carvalho de Melo 	 */
144435b9d88eSArnaldo Carvalho de Melo 	if (read(child_ready_pipe[0], &bf, 1) == -1) {
144535b9d88eSArnaldo Carvalho de Melo 		perror("unable to read pipe");
144635b9d88eSArnaldo Carvalho de Melo 		goto out_close_pipes;
144735b9d88eSArnaldo Carvalho de Melo 	}
144835b9d88eSArnaldo Carvalho de Melo 
1449bcf3145fSNamhyung Kim 	fcntl(go_pipe[1], F_SETFD, FD_CLOEXEC);
145035b9d88eSArnaldo Carvalho de Melo 	evlist->workload.cork_fd = go_pipe[1];
145135b9d88eSArnaldo Carvalho de Melo 	close(child_ready_pipe[0]);
145235b9d88eSArnaldo Carvalho de Melo 	return 0;
145335b9d88eSArnaldo Carvalho de Melo 
145435b9d88eSArnaldo Carvalho de Melo out_close_pipes:
145535b9d88eSArnaldo Carvalho de Melo 	close(go_pipe[0]);
145635b9d88eSArnaldo Carvalho de Melo 	close(go_pipe[1]);
145735b9d88eSArnaldo Carvalho de Melo out_close_ready_pipe:
145835b9d88eSArnaldo Carvalho de Melo 	close(child_ready_pipe[0]);
145935b9d88eSArnaldo Carvalho de Melo 	close(child_ready_pipe[1]);
146035b9d88eSArnaldo Carvalho de Melo 	return -1;
146135b9d88eSArnaldo Carvalho de Melo }
146235b9d88eSArnaldo Carvalho de Melo 
14637b392ef0SArnaldo Carvalho de Melo int evlist__start_workload(struct evlist *evlist)
146435b9d88eSArnaldo Carvalho de Melo {
146535b9d88eSArnaldo Carvalho de Melo 	if (evlist->workload.cork_fd > 0) {
1466b3824404SDavid Ahern 		char bf = 0;
1467bcf3145fSNamhyung Kim 		int ret;
146835b9d88eSArnaldo Carvalho de Melo 		/*
146935b9d88eSArnaldo Carvalho de Melo 		 * Remove the cork, let it rip!
147035b9d88eSArnaldo Carvalho de Melo 		 */
1471bcf3145fSNamhyung Kim 		ret = write(evlist->workload.cork_fd, &bf, 1);
1472bcf3145fSNamhyung Kim 		if (ret < 0)
1473e978be9eSSoramichi Akiyama 			perror("unable to write to pipe");
1474bcf3145fSNamhyung Kim 
1475bcf3145fSNamhyung Kim 		close(evlist->workload.cork_fd);
1476bcf3145fSNamhyung Kim 		return ret;
147735b9d88eSArnaldo Carvalho de Melo 	}
147835b9d88eSArnaldo Carvalho de Melo 
147935b9d88eSArnaldo Carvalho de Melo 	return 0;
148035b9d88eSArnaldo Carvalho de Melo }
1481cb0b29e0SArnaldo Carvalho de Melo 
14822a6599cdSArnaldo Carvalho de Melo int evlist__parse_sample(struct evlist *evlist, union perf_event *event, struct perf_sample *sample)
1483cb0b29e0SArnaldo Carvalho de Melo {
14843ccf8a7bSArnaldo Carvalho de Melo 	struct evsel *evsel = evlist__event2evsel(evlist, event);
148575562573SAdrian Hunter 
148675562573SAdrian Hunter 	if (!evsel)
148775562573SAdrian Hunter 		return -EFAULT;
14886b6017a2SArnaldo Carvalho de Melo 	return evsel__parse_sample(evsel, event, sample);
1489cb0b29e0SArnaldo Carvalho de Melo }
149078f067b3SArnaldo Carvalho de Melo 
14912a6599cdSArnaldo Carvalho de Melo int evlist__parse_sample_timestamp(struct evlist *evlist, union perf_event *event, u64 *timestamp)
149201468120SJiri Olsa {
14933ccf8a7bSArnaldo Carvalho de Melo 	struct evsel *evsel = evlist__event2evsel(evlist, event);
149401468120SJiri Olsa 
149501468120SJiri Olsa 	if (!evsel)
149601468120SJiri Olsa 		return -EFAULT;
14976b6017a2SArnaldo Carvalho de Melo 	return evsel__parse_sample_timestamp(evsel, event, timestamp);
149801468120SJiri Olsa }
149901468120SJiri Olsa 
1500d1f249ecSArnaldo Carvalho de Melo int evlist__strerror_open(struct evlist *evlist, int err, char *buf, size_t size)
1501a8f23d8fSArnaldo Carvalho de Melo {
1502a8f23d8fSArnaldo Carvalho de Melo 	int printed, value;
1503c8b5f2c9SArnaldo Carvalho de Melo 	char sbuf[STRERR_BUFSIZE], *emsg = str_error_r(err, sbuf, sizeof(sbuf));
1504a8f23d8fSArnaldo Carvalho de Melo 
1505a8f23d8fSArnaldo Carvalho de Melo 	switch (err) {
1506a8f23d8fSArnaldo Carvalho de Melo 	case EACCES:
1507a8f23d8fSArnaldo Carvalho de Melo 	case EPERM:
1508a8f23d8fSArnaldo Carvalho de Melo 		printed = scnprintf(buf, size,
1509a8f23d8fSArnaldo Carvalho de Melo 				    "Error:\t%s.\n"
1510a8f23d8fSArnaldo Carvalho de Melo 				    "Hint:\tCheck /proc/sys/kernel/perf_event_paranoid setting.", emsg);
1511a8f23d8fSArnaldo Carvalho de Melo 
15121a47245dSAdrian Hunter 		value = perf_event_paranoid();
1513a8f23d8fSArnaldo Carvalho de Melo 
1514a8f23d8fSArnaldo Carvalho de Melo 		printed += scnprintf(buf + printed, size - printed, "\nHint:\t");
1515a8f23d8fSArnaldo Carvalho de Melo 
1516a8f23d8fSArnaldo Carvalho de Melo 		if (value >= 2) {
1517a8f23d8fSArnaldo Carvalho de Melo 			printed += scnprintf(buf + printed, size - printed,
1518a8f23d8fSArnaldo Carvalho de Melo 					     "For your workloads it needs to be <= 1\nHint:\t");
1519a8f23d8fSArnaldo Carvalho de Melo 		}
1520a8f23d8fSArnaldo Carvalho de Melo 		printed += scnprintf(buf + printed, size - printed,
15215229e366SArnaldo Carvalho de Melo 				     "For system wide tracing it needs to be set to -1.\n");
1522a8f23d8fSArnaldo Carvalho de Melo 
1523a8f23d8fSArnaldo Carvalho de Melo 		printed += scnprintf(buf + printed, size - printed,
15245229e366SArnaldo Carvalho de Melo 				    "Hint:\tTry: 'sudo sh -c \"echo -1 > /proc/sys/kernel/perf_event_paranoid\"'\n"
15255229e366SArnaldo Carvalho de Melo 				    "Hint:\tThe current value is %d.", value);
1526a8f23d8fSArnaldo Carvalho de Melo 		break;
1527d9aade7fSArnaldo Carvalho de Melo 	case EINVAL: {
1528515dbe48SJiri Olsa 		struct evsel *first = evlist__first(evlist);
1529d9aade7fSArnaldo Carvalho de Melo 		int max_freq;
1530d9aade7fSArnaldo Carvalho de Melo 
1531d9aade7fSArnaldo Carvalho de Melo 		if (sysctl__read_int("kernel/perf_event_max_sample_rate", &max_freq) < 0)
1532d9aade7fSArnaldo Carvalho de Melo 			goto out_default;
1533d9aade7fSArnaldo Carvalho de Melo 
15341fc632ceSJiri Olsa 		if (first->core.attr.sample_freq < (u64)max_freq)
1535d9aade7fSArnaldo Carvalho de Melo 			goto out_default;
1536d9aade7fSArnaldo Carvalho de Melo 
1537d9aade7fSArnaldo Carvalho de Melo 		printed = scnprintf(buf, size,
1538d9aade7fSArnaldo Carvalho de Melo 				    "Error:\t%s.\n"
1539d9aade7fSArnaldo Carvalho de Melo 				    "Hint:\tCheck /proc/sys/kernel/perf_event_max_sample_rate.\n"
1540d9aade7fSArnaldo Carvalho de Melo 				    "Hint:\tThe current value is %d and %" PRIu64 " is being requested.",
15411fc632ceSJiri Olsa 				    emsg, max_freq, first->core.attr.sample_freq);
1542d9aade7fSArnaldo Carvalho de Melo 		break;
1543d9aade7fSArnaldo Carvalho de Melo 	}
1544a8f23d8fSArnaldo Carvalho de Melo 	default:
1545d9aade7fSArnaldo Carvalho de Melo out_default:
1546a8f23d8fSArnaldo Carvalho de Melo 		scnprintf(buf, size, "%s", emsg);
1547a8f23d8fSArnaldo Carvalho de Melo 		break;
1548a8f23d8fSArnaldo Carvalho de Melo 	}
1549a8f23d8fSArnaldo Carvalho de Melo 
1550a8f23d8fSArnaldo Carvalho de Melo 	return 0;
1551a8f23d8fSArnaldo Carvalho de Melo }
1552a025e4f0SAdrian Hunter 
1553d1f249ecSArnaldo Carvalho de Melo int evlist__strerror_mmap(struct evlist *evlist, int err, char *buf, size_t size)
1554956fa571SArnaldo Carvalho de Melo {
1555c8b5f2c9SArnaldo Carvalho de Melo 	char sbuf[STRERR_BUFSIZE], *emsg = str_error_r(err, sbuf, sizeof(sbuf));
1556f6fa4375SJiri Olsa 	int pages_attempted = evlist->core.mmap_len / 1024, pages_max_per_user, printed = 0;
1557956fa571SArnaldo Carvalho de Melo 
1558956fa571SArnaldo Carvalho de Melo 	switch (err) {
1559956fa571SArnaldo Carvalho de Melo 	case EPERM:
1560e5d4a290SArnaldo Carvalho de Melo 		sysctl__read_int("kernel/perf_event_mlock_kb", &pages_max_per_user);
1561e965bea1SArnaldo Carvalho de Melo 		printed += scnprintf(buf + printed, size - printed,
1562e965bea1SArnaldo Carvalho de Melo 				     "Error:\t%s.\n"
1563956fa571SArnaldo Carvalho de Melo 				     "Hint:\tCheck /proc/sys/kernel/perf_event_mlock_kb (%d kB) setting.\n"
1564e965bea1SArnaldo Carvalho de Melo 				     "Hint:\tTried using %zd kB.\n",
1565e5d4a290SArnaldo Carvalho de Melo 				     emsg, pages_max_per_user, pages_attempted);
1566e965bea1SArnaldo Carvalho de Melo 
1567e965bea1SArnaldo Carvalho de Melo 		if (pages_attempted >= pages_max_per_user) {
1568e965bea1SArnaldo Carvalho de Melo 			printed += scnprintf(buf + printed, size - printed,
1569e965bea1SArnaldo Carvalho de Melo 					     "Hint:\tTry 'sudo sh -c \"echo %d > /proc/sys/kernel/perf_event_mlock_kb\"', or\n",
1570e965bea1SArnaldo Carvalho de Melo 					     pages_max_per_user + pages_attempted);
1571e965bea1SArnaldo Carvalho de Melo 		}
1572e965bea1SArnaldo Carvalho de Melo 
1573e965bea1SArnaldo Carvalho de Melo 		printed += scnprintf(buf + printed, size - printed,
1574e965bea1SArnaldo Carvalho de Melo 				     "Hint:\tTry using a smaller -m/--mmap-pages value.");
1575956fa571SArnaldo Carvalho de Melo 		break;
1576956fa571SArnaldo Carvalho de Melo 	default:
1577956fa571SArnaldo Carvalho de Melo 		scnprintf(buf, size, "%s", emsg);
1578956fa571SArnaldo Carvalho de Melo 		break;
1579956fa571SArnaldo Carvalho de Melo 	}
1580956fa571SArnaldo Carvalho de Melo 
1581956fa571SArnaldo Carvalho de Melo 	return 0;
1582956fa571SArnaldo Carvalho de Melo }
1583956fa571SArnaldo Carvalho de Melo 
1584e414fd1aSArnaldo Carvalho de Melo void evlist__to_front(struct evlist *evlist, struct evsel *move_evsel)
1585a025e4f0SAdrian Hunter {
158632dcd021SJiri Olsa 	struct evsel *evsel, *n;
1587a025e4f0SAdrian Hunter 	LIST_HEAD(move);
1588a025e4f0SAdrian Hunter 
1589515dbe48SJiri Olsa 	if (move_evsel == evlist__first(evlist))
1590a025e4f0SAdrian Hunter 		return;
1591a025e4f0SAdrian Hunter 
1592e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry_safe(evlist, n, evsel) {
1593fba7c866SJiri Olsa 		if (evsel__leader(evsel) == evsel__leader(move_evsel))
1594b27c4eceSJiri Olsa 			list_move_tail(&evsel->core.node, &move);
1595a025e4f0SAdrian Hunter 	}
1596a025e4f0SAdrian Hunter 
1597ce9036a6SJiri Olsa 	list_splice(&move, &evlist->core.entries);
1598a025e4f0SAdrian Hunter }
159960b0896cSAdrian Hunter 
1600e80db255SArnaldo Carvalho de Melo struct evsel *evlist__get_tracking_event(struct evlist *evlist)
1601442ad225SAdrian Hunter {
1602442ad225SAdrian Hunter 	struct evsel *evsel;
1603442ad225SAdrian Hunter 
1604442ad225SAdrian Hunter 	evlist__for_each_entry(evlist, evsel) {
1605442ad225SAdrian Hunter 		if (evsel->tracking)
1606442ad225SAdrian Hunter 			return evsel;
1607442ad225SAdrian Hunter 	}
1608442ad225SAdrian Hunter 
1609442ad225SAdrian Hunter 	return evlist__first(evlist);
1610442ad225SAdrian Hunter }
1611442ad225SAdrian Hunter 
1612e80db255SArnaldo Carvalho de Melo void evlist__set_tracking_event(struct evlist *evlist, struct evsel *tracking_evsel)
161360b0896cSAdrian Hunter {
161432dcd021SJiri Olsa 	struct evsel *evsel;
161560b0896cSAdrian Hunter 
161660b0896cSAdrian Hunter 	if (tracking_evsel->tracking)
161760b0896cSAdrian Hunter 		return;
161860b0896cSAdrian Hunter 
1619e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, evsel) {
162060b0896cSAdrian Hunter 		if (evsel != tracking_evsel)
162160b0896cSAdrian Hunter 			evsel->tracking = false;
162260b0896cSAdrian Hunter 	}
162360b0896cSAdrian Hunter 
162460b0896cSAdrian Hunter 	tracking_evsel->tracking = true;
162560b0896cSAdrian Hunter }
16267630b3e2SWang Nan 
1627b02736f7SArnaldo Carvalho de Melo struct evsel *evlist__find_evsel_by_str(struct evlist *evlist, const char *str)
16287630b3e2SWang Nan {
162932dcd021SJiri Olsa 	struct evsel *evsel;
16307630b3e2SWang Nan 
1631e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, evsel) {
16327630b3e2SWang Nan 		if (!evsel->name)
16337630b3e2SWang Nan 			continue;
16347630b3e2SWang Nan 		if (strcmp(str, evsel->name) == 0)
16357630b3e2SWang Nan 			return evsel;
16367630b3e2SWang Nan 	}
16377630b3e2SWang Nan 
16387630b3e2SWang Nan 	return NULL;
16397630b3e2SWang Nan }
164054cc54deSWang Nan 
1641ade9d208SArnaldo Carvalho de Melo void evlist__toggle_bkw_mmap(struct evlist *evlist, enum bkw_mmap_state state)
164254cc54deSWang Nan {
164354cc54deSWang Nan 	enum bkw_mmap_state old_state = evlist->bkw_mmap_state;
164454cc54deSWang Nan 	enum action {
164554cc54deSWang Nan 		NONE,
164654cc54deSWang Nan 		PAUSE,
164754cc54deSWang Nan 		RESUME,
164854cc54deSWang Nan 	} action = NONE;
164954cc54deSWang Nan 
16500b72d69aSWang Nan 	if (!evlist->overwrite_mmap)
165154cc54deSWang Nan 		return;
165254cc54deSWang Nan 
165354cc54deSWang Nan 	switch (old_state) {
165454cc54deSWang Nan 	case BKW_MMAP_NOTREADY: {
165554cc54deSWang Nan 		if (state != BKW_MMAP_RUNNING)
1656dd8bd53aSLuis de Bethencourt 			goto state_err;
165754cc54deSWang Nan 		break;
165854cc54deSWang Nan 	}
165954cc54deSWang Nan 	case BKW_MMAP_RUNNING: {
166054cc54deSWang Nan 		if (state != BKW_MMAP_DATA_PENDING)
166154cc54deSWang Nan 			goto state_err;
166254cc54deSWang Nan 		action = PAUSE;
166354cc54deSWang Nan 		break;
166454cc54deSWang Nan 	}
166554cc54deSWang Nan 	case BKW_MMAP_DATA_PENDING: {
166654cc54deSWang Nan 		if (state != BKW_MMAP_EMPTY)
166754cc54deSWang Nan 			goto state_err;
166854cc54deSWang Nan 		break;
166954cc54deSWang Nan 	}
167054cc54deSWang Nan 	case BKW_MMAP_EMPTY: {
167154cc54deSWang Nan 		if (state != BKW_MMAP_RUNNING)
167254cc54deSWang Nan 			goto state_err;
167354cc54deSWang Nan 		action = RESUME;
167454cc54deSWang Nan 		break;
167554cc54deSWang Nan 	}
167654cc54deSWang Nan 	default:
167754cc54deSWang Nan 		WARN_ONCE(1, "Shouldn't get there\n");
167854cc54deSWang Nan 	}
167954cc54deSWang Nan 
168054cc54deSWang Nan 	evlist->bkw_mmap_state = state;
168154cc54deSWang Nan 
168254cc54deSWang Nan 	switch (action) {
168354cc54deSWang Nan 	case PAUSE:
16840a60b339SArnaldo Carvalho de Melo 		evlist__pause(evlist);
168554cc54deSWang Nan 		break;
168654cc54deSWang Nan 	case RESUME:
16870a60b339SArnaldo Carvalho de Melo 		evlist__resume(evlist);
168854cc54deSWang Nan 		break;
168954cc54deSWang Nan 	case NONE:
169054cc54deSWang Nan 	default:
169154cc54deSWang Nan 		break;
169254cc54deSWang Nan 	}
169354cc54deSWang Nan 
169454cc54deSWang Nan state_err:
169554cc54deSWang Nan 	return;
169654cc54deSWang Nan }
169707d6f446SArnaldo Carvalho de Melo 
169878e1bc25SArnaldo Carvalho de Melo bool evlist__exclude_kernel(struct evlist *evlist)
169907d6f446SArnaldo Carvalho de Melo {
170032dcd021SJiri Olsa 	struct evsel *evsel;
170107d6f446SArnaldo Carvalho de Melo 
170207d6f446SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, evsel) {
17031fc632ceSJiri Olsa 		if (!evsel->core.attr.exclude_kernel)
170407d6f446SArnaldo Carvalho de Melo 			return false;
170507d6f446SArnaldo Carvalho de Melo 	}
170607d6f446SArnaldo Carvalho de Melo 
170707d6f446SArnaldo Carvalho de Melo 	return true;
170807d6f446SArnaldo Carvalho de Melo }
1709e2bdbe80SJin Yao 
1710e2bdbe80SJin Yao /*
1711e2bdbe80SJin Yao  * Events in data file are not collect in groups, but we still want
1712e2bdbe80SJin Yao  * the group display. Set the artificial group and set the leader's
1713e2bdbe80SJin Yao  * forced_leader flag to notify the display code.
1714e2bdbe80SJin Yao  */
171564b4778bSArnaldo Carvalho de Melo void evlist__force_leader(struct evlist *evlist)
1716e2bdbe80SJin Yao {
17173a683120SJiri Olsa 	if (!evlist->core.nr_groups) {
1718515dbe48SJiri Olsa 		struct evsel *leader = evlist__first(evlist);
1719e2bdbe80SJin Yao 
1720a622eafaSArnaldo Carvalho de Melo 		evlist__set_leader(evlist);
1721e2bdbe80SJin Yao 		leader->forced_leader = true;
1722e2bdbe80SJin Yao 	}
1723e2bdbe80SJin Yao }
1724c3537fc2SAndi Kleen 
172564b4778bSArnaldo Carvalho de Melo struct evsel *evlist__reset_weak_group(struct evlist *evsel_list, struct evsel *evsel, bool close)
1726c3537fc2SAndi Kleen {
172732dcd021SJiri Olsa 	struct evsel *c2, *leader;
1728c3537fc2SAndi Kleen 	bool is_open = true;
1729c3537fc2SAndi Kleen 
1730fba7c866SJiri Olsa 	leader = evsel__leader(evsel);
1731fba7c866SJiri Olsa 
1732c3537fc2SAndi Kleen 	pr_debug("Weak group for %s/%d failed\n",
17335643b1a5SJiri Olsa 			leader->name, leader->core.nr_members);
1734c3537fc2SAndi Kleen 
1735c3537fc2SAndi Kleen 	/*
1736c3537fc2SAndi Kleen 	 * for_each_group_member doesn't work here because it doesn't
1737c3537fc2SAndi Kleen 	 * include the first entry.
1738c3537fc2SAndi Kleen 	 */
1739c3537fc2SAndi Kleen 	evlist__for_each_entry(evsel_list, c2) {
1740c3537fc2SAndi Kleen 		if (c2 == evsel)
1741c3537fc2SAndi Kleen 			is_open = false;
1742fba7c866SJiri Olsa 		if (evsel__has_leader(c2, leader)) {
17434804e011SAndi Kleen 			if (is_open && close)
17445a40e199SAndi Kleen 				perf_evsel__close(&c2->core);
1745*d98079c0SIan Rogers 			/*
1746*d98079c0SIan Rogers 			 * We want to close all members of the group and reopen
1747*d98079c0SIan Rogers 			 * them. Some events, like Intel topdown, require being
1748*d98079c0SIan Rogers 			 * in a group and so keep these in the group.
1749*d98079c0SIan Rogers 			 */
1750*d98079c0SIan Rogers 			if (!evsel__must_be_in_group(c2) && c2 != leader) {
1751fba7c866SJiri Olsa 				evsel__set_leader(c2, c2);
17525643b1a5SJiri Olsa 				c2->core.nr_members = 0;
1753*d98079c0SIan Rogers 				leader->core.nr_members--;
1754*d98079c0SIan Rogers 			}
1755*d98079c0SIan Rogers 
17564804e011SAndi Kleen 			/*
17574804e011SAndi Kleen 			 * Set this for all former members of the group
17584804e011SAndi Kleen 			 * to indicate they get reopened.
17594804e011SAndi Kleen 			 */
17604804e011SAndi Kleen 			c2->reset_group = true;
1761c3537fc2SAndi Kleen 		}
1762c3537fc2SAndi Kleen 	}
1763*d98079c0SIan Rogers 	/* Reset the leader count if all entries were removed. */
1764*d98079c0SIan Rogers 	if (leader->core.nr_members == 1)
1765*d98079c0SIan Rogers 		leader->core.nr_members = 0;
1766c3537fc2SAndi Kleen 	return leader;
1767c3537fc2SAndi Kleen }
1768ec886bf5SAlexey Budankov 
1769a8fcbd26SAdrian Hunter static int evlist__parse_control_fifo(const char *str, int *ctl_fd, int *ctl_fd_ack, bool *ctl_fd_close)
1770a8fcbd26SAdrian Hunter {
1771a8fcbd26SAdrian Hunter 	char *s, *p;
1772a8fcbd26SAdrian Hunter 	int ret = 0, fd;
1773a8fcbd26SAdrian Hunter 
1774a8fcbd26SAdrian Hunter 	if (strncmp(str, "fifo:", 5))
1775a8fcbd26SAdrian Hunter 		return -EINVAL;
1776a8fcbd26SAdrian Hunter 
1777a8fcbd26SAdrian Hunter 	str += 5;
1778a8fcbd26SAdrian Hunter 	if (!*str || *str == ',')
1779a8fcbd26SAdrian Hunter 		return -EINVAL;
1780a8fcbd26SAdrian Hunter 
1781a8fcbd26SAdrian Hunter 	s = strdup(str);
1782a8fcbd26SAdrian Hunter 	if (!s)
1783a8fcbd26SAdrian Hunter 		return -ENOMEM;
1784a8fcbd26SAdrian Hunter 
1785a8fcbd26SAdrian Hunter 	p = strchr(s, ',');
1786a8fcbd26SAdrian Hunter 	if (p)
1787a8fcbd26SAdrian Hunter 		*p = '\0';
1788a8fcbd26SAdrian Hunter 
1789a8fcbd26SAdrian Hunter 	/*
1790a8fcbd26SAdrian Hunter 	 * O_RDWR avoids POLLHUPs which is necessary to allow the other
1791a8fcbd26SAdrian Hunter 	 * end of a FIFO to be repeatedly opened and closed.
1792a8fcbd26SAdrian Hunter 	 */
1793a8fcbd26SAdrian Hunter 	fd = open(s, O_RDWR | O_NONBLOCK | O_CLOEXEC);
1794a8fcbd26SAdrian Hunter 	if (fd < 0) {
1795a8fcbd26SAdrian Hunter 		pr_err("Failed to open '%s'\n", s);
1796a8fcbd26SAdrian Hunter 		ret = -errno;
1797a8fcbd26SAdrian Hunter 		goto out_free;
1798a8fcbd26SAdrian Hunter 	}
1799a8fcbd26SAdrian Hunter 	*ctl_fd = fd;
1800a8fcbd26SAdrian Hunter 	*ctl_fd_close = true;
1801a8fcbd26SAdrian Hunter 
1802a8fcbd26SAdrian Hunter 	if (p && *++p) {
1803a8fcbd26SAdrian Hunter 		/* O_RDWR | O_NONBLOCK means the other end need not be open */
1804a8fcbd26SAdrian Hunter 		fd = open(p, O_RDWR | O_NONBLOCK | O_CLOEXEC);
1805a8fcbd26SAdrian Hunter 		if (fd < 0) {
1806a8fcbd26SAdrian Hunter 			pr_err("Failed to open '%s'\n", p);
1807a8fcbd26SAdrian Hunter 			ret = -errno;
1808a8fcbd26SAdrian Hunter 			goto out_free;
1809a8fcbd26SAdrian Hunter 		}
1810a8fcbd26SAdrian Hunter 		*ctl_fd_ack = fd;
1811a8fcbd26SAdrian Hunter 	}
1812a8fcbd26SAdrian Hunter 
1813a8fcbd26SAdrian Hunter out_free:
1814a8fcbd26SAdrian Hunter 	free(s);
1815a8fcbd26SAdrian Hunter 	return ret;
1816a8fcbd26SAdrian Hunter }
1817a8fcbd26SAdrian Hunter 
1818a8fcbd26SAdrian Hunter int evlist__parse_control(const char *str, int *ctl_fd, int *ctl_fd_ack, bool *ctl_fd_close)
18199864a66dSAdrian Hunter {
18209864a66dSAdrian Hunter 	char *comma = NULL, *endptr = NULL;
18219864a66dSAdrian Hunter 
1822a8fcbd26SAdrian Hunter 	*ctl_fd_close = false;
1823a8fcbd26SAdrian Hunter 
18249864a66dSAdrian Hunter 	if (strncmp(str, "fd:", 3))
1825a8fcbd26SAdrian Hunter 		return evlist__parse_control_fifo(str, ctl_fd, ctl_fd_ack, ctl_fd_close);
18269864a66dSAdrian Hunter 
18279864a66dSAdrian Hunter 	*ctl_fd = strtoul(&str[3], &endptr, 0);
18289864a66dSAdrian Hunter 	if (endptr == &str[3])
18299864a66dSAdrian Hunter 		return -EINVAL;
18309864a66dSAdrian Hunter 
18319864a66dSAdrian Hunter 	comma = strchr(str, ',');
18329864a66dSAdrian Hunter 	if (comma) {
18339864a66dSAdrian Hunter 		if (endptr != comma)
18349864a66dSAdrian Hunter 			return -EINVAL;
18359864a66dSAdrian Hunter 
18369864a66dSAdrian Hunter 		*ctl_fd_ack = strtoul(comma + 1, &endptr, 0);
18379864a66dSAdrian Hunter 		if (endptr == comma + 1 || *endptr != '\0')
18389864a66dSAdrian Hunter 			return -EINVAL;
18399864a66dSAdrian Hunter 	}
18409864a66dSAdrian Hunter 
18419864a66dSAdrian Hunter 	return 0;
18429864a66dSAdrian Hunter }
18439864a66dSAdrian Hunter 
1844ee7fe31eSAdrian Hunter void evlist__close_control(int ctl_fd, int ctl_fd_ack, bool *ctl_fd_close)
1845ee7fe31eSAdrian Hunter {
1846ee7fe31eSAdrian Hunter 	if (*ctl_fd_close) {
1847ee7fe31eSAdrian Hunter 		*ctl_fd_close = false;
1848ee7fe31eSAdrian Hunter 		close(ctl_fd);
1849ee7fe31eSAdrian Hunter 		if (ctl_fd_ack >= 0)
1850ee7fe31eSAdrian Hunter 			close(ctl_fd_ack);
1851ee7fe31eSAdrian Hunter 	}
1852ee7fe31eSAdrian Hunter }
1853ee7fe31eSAdrian Hunter 
1854ec886bf5SAlexey Budankov int evlist__initialize_ctlfd(struct evlist *evlist, int fd, int ack)
1855ec886bf5SAlexey Budankov {
1856ec886bf5SAlexey Budankov 	if (fd == -1) {
1857ec886bf5SAlexey Budankov 		pr_debug("Control descriptor is not initialized\n");
1858ec886bf5SAlexey Budankov 		return 0;
1859ec886bf5SAlexey Budankov 	}
1860ec886bf5SAlexey Budankov 
1861ec886bf5SAlexey Budankov 	evlist->ctl_fd.pos = perf_evlist__add_pollfd(&evlist->core, fd, NULL, POLLIN,
1862ec886bf5SAlexey Budankov 						     fdarray_flag__nonfilterable);
1863ec886bf5SAlexey Budankov 	if (evlist->ctl_fd.pos < 0) {
1864ec886bf5SAlexey Budankov 		evlist->ctl_fd.pos = -1;
1865ec886bf5SAlexey Budankov 		pr_err("Failed to add ctl fd entry: %m\n");
1866ec886bf5SAlexey Budankov 		return -1;
1867ec886bf5SAlexey Budankov 	}
1868ec886bf5SAlexey Budankov 
1869ec886bf5SAlexey Budankov 	evlist->ctl_fd.fd = fd;
1870ec886bf5SAlexey Budankov 	evlist->ctl_fd.ack = ack;
1871ec886bf5SAlexey Budankov 
1872ec886bf5SAlexey Budankov 	return 0;
1873ec886bf5SAlexey Budankov }
1874ec886bf5SAlexey Budankov 
1875ec886bf5SAlexey Budankov bool evlist__ctlfd_initialized(struct evlist *evlist)
1876ec886bf5SAlexey Budankov {
1877ec886bf5SAlexey Budankov 	return evlist->ctl_fd.pos >= 0;
1878ec886bf5SAlexey Budankov }
1879ec886bf5SAlexey Budankov 
1880ec886bf5SAlexey Budankov int evlist__finalize_ctlfd(struct evlist *evlist)
1881ec886bf5SAlexey Budankov {
1882ec886bf5SAlexey Budankov 	struct pollfd *entries = evlist->core.pollfd.entries;
1883ec886bf5SAlexey Budankov 
1884ec886bf5SAlexey Budankov 	if (!evlist__ctlfd_initialized(evlist))
1885ec886bf5SAlexey Budankov 		return 0;
1886ec886bf5SAlexey Budankov 
1887ec886bf5SAlexey Budankov 	entries[evlist->ctl_fd.pos].fd = -1;
1888ec886bf5SAlexey Budankov 	entries[evlist->ctl_fd.pos].events = 0;
1889ec886bf5SAlexey Budankov 	entries[evlist->ctl_fd.pos].revents = 0;
1890ec886bf5SAlexey Budankov 
1891ec886bf5SAlexey Budankov 	evlist->ctl_fd.pos = -1;
1892ec886bf5SAlexey Budankov 	evlist->ctl_fd.ack = -1;
1893ec886bf5SAlexey Budankov 	evlist->ctl_fd.fd = -1;
1894ec886bf5SAlexey Budankov 
1895ec886bf5SAlexey Budankov 	return 0;
1896ec886bf5SAlexey Budankov }
1897ec886bf5SAlexey Budankov 
1898ec886bf5SAlexey Budankov static int evlist__ctlfd_recv(struct evlist *evlist, enum evlist_ctl_cmd *cmd,
1899ec886bf5SAlexey Budankov 			      char *cmd_data, size_t data_size)
1900ec886bf5SAlexey Budankov {
1901ec886bf5SAlexey Budankov 	int err;
1902ec886bf5SAlexey Budankov 	char c;
1903ec886bf5SAlexey Budankov 	size_t bytes_read = 0;
1904ec886bf5SAlexey Budankov 
190540db8ff5SAdrian Hunter 	*cmd = EVLIST_CTL_CMD_UNSUPPORTED;
1906ec886bf5SAlexey Budankov 	memset(cmd_data, 0, data_size);
1907ec886bf5SAlexey Budankov 	data_size--;
1908ec886bf5SAlexey Budankov 
1909ec886bf5SAlexey Budankov 	do {
1910ec886bf5SAlexey Budankov 		err = read(evlist->ctl_fd.fd, &c, 1);
1911ec886bf5SAlexey Budankov 		if (err > 0) {
1912ec886bf5SAlexey Budankov 			if (c == '\n' || c == '\0')
1913ec886bf5SAlexey Budankov 				break;
1914ec886bf5SAlexey Budankov 			cmd_data[bytes_read++] = c;
1915ec886bf5SAlexey Budankov 			if (bytes_read == data_size)
1916ec886bf5SAlexey Budankov 				break;
191740db8ff5SAdrian Hunter 			continue;
191840db8ff5SAdrian Hunter 		} else if (err == -1) {
191940db8ff5SAdrian Hunter 			if (errno == EINTR)
192040db8ff5SAdrian Hunter 				continue;
192140db8ff5SAdrian Hunter 			if (errno == EAGAIN || errno == EWOULDBLOCK)
192240db8ff5SAdrian Hunter 				err = 0;
192340db8ff5SAdrian Hunter 			else
1924ec886bf5SAlexey Budankov 				pr_err("Failed to read from ctlfd %d: %m\n", evlist->ctl_fd.fd);
1925ec886bf5SAlexey Budankov 		}
192640db8ff5SAdrian Hunter 		break;
1927ec886bf5SAlexey Budankov 	} while (1);
1928ec886bf5SAlexey Budankov 
1929ec886bf5SAlexey Budankov 	pr_debug("Message from ctl_fd: \"%s%s\"\n", cmd_data,
1930ec886bf5SAlexey Budankov 		 bytes_read == data_size ? "" : c == '\n' ? "\\n" : "\\0");
1931ec886bf5SAlexey Budankov 
193240db8ff5SAdrian Hunter 	if (bytes_read > 0) {
1933ec886bf5SAlexey Budankov 		if (!strncmp(cmd_data, EVLIST_CTL_CMD_ENABLE_TAG,
1934ec886bf5SAlexey Budankov 			     (sizeof(EVLIST_CTL_CMD_ENABLE_TAG)-1))) {
1935ec886bf5SAlexey Budankov 			*cmd = EVLIST_CTL_CMD_ENABLE;
1936ec886bf5SAlexey Budankov 		} else if (!strncmp(cmd_data, EVLIST_CTL_CMD_DISABLE_TAG,
1937ec886bf5SAlexey Budankov 				    (sizeof(EVLIST_CTL_CMD_DISABLE_TAG)-1))) {
1938ec886bf5SAlexey Budankov 			*cmd = EVLIST_CTL_CMD_DISABLE;
1939d20aff15SAdrian Hunter 		} else if (!strncmp(cmd_data, EVLIST_CTL_CMD_SNAPSHOT_TAG,
1940d20aff15SAdrian Hunter 				    (sizeof(EVLIST_CTL_CMD_SNAPSHOT_TAG)-1))) {
1941d20aff15SAdrian Hunter 			*cmd = EVLIST_CTL_CMD_SNAPSHOT;
1942d20aff15SAdrian Hunter 			pr_debug("is snapshot\n");
1943142544a9SJiri Olsa 		} else if (!strncmp(cmd_data, EVLIST_CTL_CMD_EVLIST_TAG,
1944142544a9SJiri Olsa 				    (sizeof(EVLIST_CTL_CMD_EVLIST_TAG)-1))) {
1945142544a9SJiri Olsa 			*cmd = EVLIST_CTL_CMD_EVLIST;
1946f186cd61SJiri Olsa 		} else if (!strncmp(cmd_data, EVLIST_CTL_CMD_STOP_TAG,
1947f186cd61SJiri Olsa 				    (sizeof(EVLIST_CTL_CMD_STOP_TAG)-1))) {
1948f186cd61SJiri Olsa 			*cmd = EVLIST_CTL_CMD_STOP;
194947fddcb4SJiri Olsa 		} else if (!strncmp(cmd_data, EVLIST_CTL_CMD_PING_TAG,
195047fddcb4SJiri Olsa 				    (sizeof(EVLIST_CTL_CMD_PING_TAG)-1))) {
195147fddcb4SJiri Olsa 			*cmd = EVLIST_CTL_CMD_PING;
1952ec886bf5SAlexey Budankov 		}
1953ec886bf5SAlexey Budankov 	}
1954ec886bf5SAlexey Budankov 
195540db8ff5SAdrian Hunter 	return bytes_read ? (int)bytes_read : err;
1956ec886bf5SAlexey Budankov }
1957ec886bf5SAlexey Budankov 
1958d20aff15SAdrian Hunter int evlist__ctlfd_ack(struct evlist *evlist)
1959ec886bf5SAlexey Budankov {
1960ec886bf5SAlexey Budankov 	int err;
1961ec886bf5SAlexey Budankov 
1962ec886bf5SAlexey Budankov 	if (evlist->ctl_fd.ack == -1)
1963ec886bf5SAlexey Budankov 		return 0;
1964ec886bf5SAlexey Budankov 
1965ec886bf5SAlexey Budankov 	err = write(evlist->ctl_fd.ack, EVLIST_CTL_CMD_ACK_TAG,
1966ec886bf5SAlexey Budankov 		    sizeof(EVLIST_CTL_CMD_ACK_TAG));
1967ec886bf5SAlexey Budankov 	if (err == -1)
1968ec886bf5SAlexey Budankov 		pr_err("failed to write to ctl_ack_fd %d: %m\n", evlist->ctl_fd.ack);
1969ec886bf5SAlexey Budankov 
1970ec886bf5SAlexey Budankov 	return err;
1971ec886bf5SAlexey Budankov }
1972ec886bf5SAlexey Budankov 
1973991ae4ebSJiri Olsa static int get_cmd_arg(char *cmd_data, size_t cmd_size, char **arg)
1974991ae4ebSJiri Olsa {
1975991ae4ebSJiri Olsa 	char *data = cmd_data + cmd_size;
1976991ae4ebSJiri Olsa 
1977991ae4ebSJiri Olsa 	/* no argument */
1978991ae4ebSJiri Olsa 	if (!*data)
1979991ae4ebSJiri Olsa 		return 0;
1980991ae4ebSJiri Olsa 
1981991ae4ebSJiri Olsa 	/* there's argument */
1982991ae4ebSJiri Olsa 	if (*data == ' ') {
1983991ae4ebSJiri Olsa 		*arg = data + 1;
1984991ae4ebSJiri Olsa 		return 1;
1985991ae4ebSJiri Olsa 	}
1986991ae4ebSJiri Olsa 
1987991ae4ebSJiri Olsa 	/* malformed */
1988991ae4ebSJiri Olsa 	return -1;
1989991ae4ebSJiri Olsa }
1990991ae4ebSJiri Olsa 
1991991ae4ebSJiri Olsa static int evlist__ctlfd_enable(struct evlist *evlist, char *cmd_data, bool enable)
1992991ae4ebSJiri Olsa {
1993991ae4ebSJiri Olsa 	struct evsel *evsel;
1994991ae4ebSJiri Olsa 	char *name;
1995991ae4ebSJiri Olsa 	int err;
1996991ae4ebSJiri Olsa 
1997991ae4ebSJiri Olsa 	err = get_cmd_arg(cmd_data,
1998991ae4ebSJiri Olsa 			  enable ? sizeof(EVLIST_CTL_CMD_ENABLE_TAG) - 1 :
1999991ae4ebSJiri Olsa 				   sizeof(EVLIST_CTL_CMD_DISABLE_TAG) - 1,
2000991ae4ebSJiri Olsa 			  &name);
2001991ae4ebSJiri Olsa 	if (err < 0) {
2002991ae4ebSJiri Olsa 		pr_info("failed: wrong command\n");
2003991ae4ebSJiri Olsa 		return -1;
2004991ae4ebSJiri Olsa 	}
2005991ae4ebSJiri Olsa 
2006991ae4ebSJiri Olsa 	if (err) {
2007991ae4ebSJiri Olsa 		evsel = evlist__find_evsel_by_str(evlist, name);
2008991ae4ebSJiri Olsa 		if (evsel) {
2009991ae4ebSJiri Olsa 			if (enable)
2010991ae4ebSJiri Olsa 				evlist__enable_evsel(evlist, name);
2011991ae4ebSJiri Olsa 			else
2012991ae4ebSJiri Olsa 				evlist__disable_evsel(evlist, name);
2013991ae4ebSJiri Olsa 			pr_info("Event %s %s\n", evsel->name,
2014991ae4ebSJiri Olsa 				enable ? "enabled" : "disabled");
2015991ae4ebSJiri Olsa 		} else {
2016991ae4ebSJiri Olsa 			pr_info("failed: can't find '%s' event\n", name);
2017991ae4ebSJiri Olsa 		}
2018991ae4ebSJiri Olsa 	} else {
2019991ae4ebSJiri Olsa 		if (enable) {
2020991ae4ebSJiri Olsa 			evlist__enable(evlist);
2021991ae4ebSJiri Olsa 			pr_info(EVLIST_ENABLED_MSG);
2022991ae4ebSJiri Olsa 		} else {
2023991ae4ebSJiri Olsa 			evlist__disable(evlist);
2024991ae4ebSJiri Olsa 			pr_info(EVLIST_DISABLED_MSG);
2025991ae4ebSJiri Olsa 		}
2026991ae4ebSJiri Olsa 	}
2027991ae4ebSJiri Olsa 
2028991ae4ebSJiri Olsa 	return 0;
2029991ae4ebSJiri Olsa }
2030991ae4ebSJiri Olsa 
2031142544a9SJiri Olsa static int evlist__ctlfd_list(struct evlist *evlist, char *cmd_data)
2032142544a9SJiri Olsa {
2033142544a9SJiri Olsa 	struct perf_attr_details details = { .verbose = false, };
2034142544a9SJiri Olsa 	struct evsel *evsel;
2035142544a9SJiri Olsa 	char *arg;
2036142544a9SJiri Olsa 	int err;
2037142544a9SJiri Olsa 
2038142544a9SJiri Olsa 	err = get_cmd_arg(cmd_data,
2039142544a9SJiri Olsa 			  sizeof(EVLIST_CTL_CMD_EVLIST_TAG) - 1,
2040142544a9SJiri Olsa 			  &arg);
2041142544a9SJiri Olsa 	if (err < 0) {
2042142544a9SJiri Olsa 		pr_info("failed: wrong command\n");
2043142544a9SJiri Olsa 		return -1;
2044142544a9SJiri Olsa 	}
2045142544a9SJiri Olsa 
2046142544a9SJiri Olsa 	if (err) {
2047142544a9SJiri Olsa 		if (!strcmp(arg, "-v")) {
2048142544a9SJiri Olsa 			details.verbose = true;
2049142544a9SJiri Olsa 		} else if (!strcmp(arg, "-g")) {
2050142544a9SJiri Olsa 			details.event_group = true;
2051142544a9SJiri Olsa 		} else if (!strcmp(arg, "-F")) {
2052142544a9SJiri Olsa 			details.freq = true;
2053142544a9SJiri Olsa 		} else {
2054142544a9SJiri Olsa 			pr_info("failed: wrong command\n");
2055142544a9SJiri Olsa 			return -1;
2056142544a9SJiri Olsa 		}
2057142544a9SJiri Olsa 	}
2058142544a9SJiri Olsa 
2059142544a9SJiri Olsa 	evlist__for_each_entry(evlist, evsel)
2060142544a9SJiri Olsa 		evsel__fprintf(evsel, &details, stderr);
2061142544a9SJiri Olsa 
2062142544a9SJiri Olsa 	return 0;
2063142544a9SJiri Olsa }
2064142544a9SJiri Olsa 
2065ec886bf5SAlexey Budankov int evlist__ctlfd_process(struct evlist *evlist, enum evlist_ctl_cmd *cmd)
2066ec886bf5SAlexey Budankov {
2067ec886bf5SAlexey Budankov 	int err = 0;
2068ec886bf5SAlexey Budankov 	char cmd_data[EVLIST_CTL_CMD_MAX_LEN];
2069ec886bf5SAlexey Budankov 	int ctlfd_pos = evlist->ctl_fd.pos;
2070ec886bf5SAlexey Budankov 	struct pollfd *entries = evlist->core.pollfd.entries;
2071ec886bf5SAlexey Budankov 
2072ec886bf5SAlexey Budankov 	if (!evlist__ctlfd_initialized(evlist) || !entries[ctlfd_pos].revents)
2073ec886bf5SAlexey Budankov 		return 0;
2074ec886bf5SAlexey Budankov 
2075ec886bf5SAlexey Budankov 	if (entries[ctlfd_pos].revents & POLLIN) {
2076ec886bf5SAlexey Budankov 		err = evlist__ctlfd_recv(evlist, cmd, cmd_data,
2077ec886bf5SAlexey Budankov 					 EVLIST_CTL_CMD_MAX_LEN);
2078ec886bf5SAlexey Budankov 		if (err > 0) {
2079ec886bf5SAlexey Budankov 			switch (*cmd) {
2080ec886bf5SAlexey Budankov 			case EVLIST_CTL_CMD_ENABLE:
2081ec886bf5SAlexey Budankov 			case EVLIST_CTL_CMD_DISABLE:
2082991ae4ebSJiri Olsa 				err = evlist__ctlfd_enable(evlist, cmd_data,
2083991ae4ebSJiri Olsa 							   *cmd == EVLIST_CTL_CMD_ENABLE);
2084ec886bf5SAlexey Budankov 				break;
2085142544a9SJiri Olsa 			case EVLIST_CTL_CMD_EVLIST:
2086142544a9SJiri Olsa 				err = evlist__ctlfd_list(evlist, cmd_data);
2087142544a9SJiri Olsa 				break;
2088d20aff15SAdrian Hunter 			case EVLIST_CTL_CMD_SNAPSHOT:
2089f186cd61SJiri Olsa 			case EVLIST_CTL_CMD_STOP:
209047fddcb4SJiri Olsa 			case EVLIST_CTL_CMD_PING:
2091d20aff15SAdrian Hunter 				break;
2092ec886bf5SAlexey Budankov 			case EVLIST_CTL_CMD_ACK:
2093ec886bf5SAlexey Budankov 			case EVLIST_CTL_CMD_UNSUPPORTED:
2094ec886bf5SAlexey Budankov 			default:
2095ec886bf5SAlexey Budankov 				pr_debug("ctlfd: unsupported %d\n", *cmd);
2096ec886bf5SAlexey Budankov 				break;
2097ec886bf5SAlexey Budankov 			}
2098d20aff15SAdrian Hunter 			if (!(*cmd == EVLIST_CTL_CMD_ACK || *cmd == EVLIST_CTL_CMD_UNSUPPORTED ||
2099d20aff15SAdrian Hunter 			      *cmd == EVLIST_CTL_CMD_SNAPSHOT))
2100ec886bf5SAlexey Budankov 				evlist__ctlfd_ack(evlist);
2101ec886bf5SAlexey Budankov 		}
2102ec886bf5SAlexey Budankov 	}
2103ec886bf5SAlexey Budankov 
2104ec886bf5SAlexey Budankov 	if (entries[ctlfd_pos].revents & (POLLHUP | POLLERR))
2105ec886bf5SAlexey Budankov 		evlist__finalize_ctlfd(evlist);
2106ec886bf5SAlexey Budankov 	else
2107ec886bf5SAlexey Budankov 		entries[ctlfd_pos].revents = 0;
2108ec886bf5SAlexey Budankov 
2109ec886bf5SAlexey Budankov 	return err;
2110ec886bf5SAlexey Budankov }
2111b214ba8cSNamhyung Kim 
21126fbe4f48SAlexey Bayduraev int evlist__ctlfd_update(struct evlist *evlist, struct pollfd *update)
21136fbe4f48SAlexey Bayduraev {
21146fbe4f48SAlexey Bayduraev 	int ctlfd_pos = evlist->ctl_fd.pos;
21156fbe4f48SAlexey Bayduraev 	struct pollfd *entries = evlist->core.pollfd.entries;
21166fbe4f48SAlexey Bayduraev 
21176fbe4f48SAlexey Bayduraev 	if (!evlist__ctlfd_initialized(evlist))
21186fbe4f48SAlexey Bayduraev 		return 0;
21196fbe4f48SAlexey Bayduraev 
21206fbe4f48SAlexey Bayduraev 	if (entries[ctlfd_pos].fd != update->fd ||
21216fbe4f48SAlexey Bayduraev 	    entries[ctlfd_pos].events != update->events)
21226fbe4f48SAlexey Bayduraev 		return -1;
21236fbe4f48SAlexey Bayduraev 
21246fbe4f48SAlexey Bayduraev 	entries[ctlfd_pos].revents = update->revents;
21256fbe4f48SAlexey Bayduraev 	return 0;
21266fbe4f48SAlexey Bayduraev }
21276fbe4f48SAlexey Bayduraev 
2128b214ba8cSNamhyung Kim struct evsel *evlist__find_evsel(struct evlist *evlist, int idx)
2129b214ba8cSNamhyung Kim {
2130b214ba8cSNamhyung Kim 	struct evsel *evsel;
2131b214ba8cSNamhyung Kim 
2132b214ba8cSNamhyung Kim 	evlist__for_each_entry(evlist, evsel) {
213338fe0e01SJiri Olsa 		if (evsel->core.idx == idx)
2134b214ba8cSNamhyung Kim 			return evsel;
2135b214ba8cSNamhyung Kim 	}
2136b214ba8cSNamhyung Kim 	return NULL;
2137b214ba8cSNamhyung Kim }
21389865ea8aSArnaldo Carvalho de Melo 
21399865ea8aSArnaldo Carvalho de Melo int evlist__scnprintf_evsels(struct evlist *evlist, size_t size, char *bf)
21409865ea8aSArnaldo Carvalho de Melo {
21419865ea8aSArnaldo Carvalho de Melo 	struct evsel *evsel;
21429865ea8aSArnaldo Carvalho de Melo 	int printed = 0;
21439865ea8aSArnaldo Carvalho de Melo 
21449865ea8aSArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, evsel) {
21459865ea8aSArnaldo Carvalho de Melo 		if (evsel__is_dummy_event(evsel))
21469865ea8aSArnaldo Carvalho de Melo 			continue;
21479865ea8aSArnaldo Carvalho de Melo 		if (size > (strlen(evsel__name(evsel)) + (printed ? 2 : 1))) {
21489865ea8aSArnaldo Carvalho de Melo 			printed += scnprintf(bf + printed, size - printed, "%s%s", printed ? "," : "", evsel__name(evsel));
21499865ea8aSArnaldo Carvalho de Melo 		} else {
21509865ea8aSArnaldo Carvalho de Melo 			printed += scnprintf(bf + printed, size - printed, "%s...", printed ? "," : "");
21519865ea8aSArnaldo Carvalho de Melo 			break;
21529865ea8aSArnaldo Carvalho de Melo 		}
21539865ea8aSArnaldo Carvalho de Melo 	}
21549865ea8aSArnaldo Carvalho de Melo 
21559865ea8aSArnaldo Carvalho de Melo 	return printed;
21569865ea8aSArnaldo Carvalho de Melo }
2157d5a8bd0fSJin Yao 
2158d5a8bd0fSJin Yao void evlist__check_mem_load_aux(struct evlist *evlist)
2159d5a8bd0fSJin Yao {
2160d5a8bd0fSJin Yao 	struct evsel *leader, *evsel, *pos;
2161d5a8bd0fSJin Yao 
2162d5a8bd0fSJin Yao 	/*
2163d5a8bd0fSJin Yao 	 * For some platforms, the 'mem-loads' event is required to use
2164d5a8bd0fSJin Yao 	 * together with 'mem-loads-aux' within a group and 'mem-loads-aux'
2165d5a8bd0fSJin Yao 	 * must be the group leader. Now we disable this group before reporting
2166d5a8bd0fSJin Yao 	 * because 'mem-loads-aux' is just an auxiliary event. It doesn't carry
2167d5a8bd0fSJin Yao 	 * any valid memory load information.
2168d5a8bd0fSJin Yao 	 */
2169d5a8bd0fSJin Yao 	evlist__for_each_entry(evlist, evsel) {
2170fba7c866SJiri Olsa 		leader = evsel__leader(evsel);
2171d5a8bd0fSJin Yao 		if (leader == evsel)
2172d5a8bd0fSJin Yao 			continue;
2173d5a8bd0fSJin Yao 
2174d5a8bd0fSJin Yao 		if (leader->name && strstr(leader->name, "mem-loads-aux")) {
2175d5a8bd0fSJin Yao 			for_each_group_evsel(pos, leader) {
2176fba7c866SJiri Olsa 				evsel__set_leader(pos, pos);
2177d5a8bd0fSJin Yao 				pos->core.nr_members = 0;
2178d5a8bd0fSJin Yao 			}
2179d5a8bd0fSJin Yao 		}
2180d5a8bd0fSJin Yao 	}
2181d5a8bd0fSJin Yao }
2182