xref: /openbmc/linux/tools/perf/util/evlist.c (revision 3ccf8a7b)
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"
2020f2be1dSJiri Olsa #include <internal/lib.h> // page_size
217736627bSAndi Kleen #include "affinity.h"
22c1a604dfSArnaldo Carvalho de Melo #include "../perf.h"
2354cc54deSWang Nan #include "asm/bug.h"
24657ee553SSong Liu #include "bpf-event.h"
25da949f50SArnaldo Carvalho de Melo #include "util/string2.h"
2640c7d246SArnaldo Carvalho de Melo #include "util/perf_api_probe.h"
279607ad3aSArnaldo Carvalho de Melo #include <signal.h>
2835b9d88eSArnaldo Carvalho de Melo #include <unistd.h>
29b397f846SArnaldo Carvalho de Melo #include <sched.h>
30f2a39fe8SArnaldo Carvalho de Melo #include <stdlib.h>
31361c99a6SArnaldo Carvalho de Melo 
3250d08e47SArnaldo Carvalho de Melo #include "parse-events.h"
334b6ab94eSJosh Poimboeuf #include <subcmd/parse-options.h>
3450d08e47SArnaldo Carvalho de Melo 
35bafae98eSArnaldo Carvalho de Melo #include <fcntl.h>
3686a5e0c2SArnaldo Carvalho de Melo #include <sys/ioctl.h>
37f8a95309SArnaldo Carvalho de Melo #include <sys/mman.h>
38f8a95309SArnaldo Carvalho de Melo 
3970db7533SArnaldo Carvalho de Melo #include <linux/bitops.h>
4070db7533SArnaldo Carvalho de Melo #include <linux/hash.h>
410389cd1fSArnaldo Carvalho de Melo #include <linux/log2.h>
428dd2a131SJiri Olsa #include <linux/err.h>
438520a98dSArnaldo Carvalho de Melo #include <linux/string.h>
447f7c536fSArnaldo Carvalho de Melo #include <linux/zalloc.h>
454562a739SJiri Olsa #include <perf/evlist.h>
4688761fa1SJiri Olsa #include <perf/evsel.h>
479c3516d1SJiri Olsa #include <perf/cpumap.h>
487728fa0cSJiri Olsa #include <perf/mmap.h>
4970db7533SArnaldo Carvalho de Melo 
50e14e5497SArnaldo Carvalho de Melo #include <internal/xyarray.h>
51e14e5497SArnaldo Carvalho de Melo 
52748fe088SArnaldo Carvalho de Melo #ifdef LACKS_SIGQUEUE_PROTOTYPE
53748fe088SArnaldo Carvalho de Melo int sigqueue(pid_t pid, int sig, const union sigval value);
54748fe088SArnaldo Carvalho de Melo #endif
55748fe088SArnaldo Carvalho de Melo 
569dfcb759SJiri Olsa #define FD(e, x, y) (*(int *)xyarray__entry(e->core.fd, x, y))
578cd36f3eSJiri Olsa #define SID(e, x, y) xyarray__entry(e->core.sample_id, x, y)
58f8a95309SArnaldo Carvalho de Melo 
5952c86bcaSJiri Olsa void evlist__init(struct evlist *evlist, struct perf_cpu_map *cpus,
609749b90eSJiri Olsa 		  struct perf_thread_map *threads)
61361c99a6SArnaldo Carvalho de Melo {
624562a739SJiri Olsa 	perf_evlist__init(&evlist->core);
63453fa030SJiri Olsa 	perf_evlist__set_maps(&evlist->core, cpus, threads);
6435b9d88eSArnaldo Carvalho de Melo 	evlist->workload.pid = -1;
6554cc54deSWang Nan 	evlist->bkw_mmap_state = BKW_MMAP_NOTREADY;
668ab705b5SAlexey Budankov 	evlist->ctl_fd.fd = -1;
678ab705b5SAlexey Budankov 	evlist->ctl_fd.ack = -1;
688ab705b5SAlexey Budankov 	evlist->ctl_fd.pos = -1;
69361c99a6SArnaldo Carvalho de Melo }
70361c99a6SArnaldo Carvalho de Melo 
710f98b11cSJiri Olsa struct evlist *evlist__new(void)
72ef1d1af2SArnaldo Carvalho de Melo {
7363503dbaSJiri Olsa 	struct evlist *evlist = zalloc(sizeof(*evlist));
74ef1d1af2SArnaldo Carvalho de Melo 
75ef1d1af2SArnaldo Carvalho de Melo 	if (evlist != NULL)
7652c86bcaSJiri Olsa 		evlist__init(evlist, NULL, NULL);
77ef1d1af2SArnaldo Carvalho de Melo 
78361c99a6SArnaldo Carvalho de Melo 	return evlist;
79361c99a6SArnaldo Carvalho de Melo }
80361c99a6SArnaldo Carvalho de Melo 
8163503dbaSJiri Olsa struct evlist *perf_evlist__new_default(void)
82b22d54b0SJiri Olsa {
830f98b11cSJiri Olsa 	struct evlist *evlist = evlist__new();
84b22d54b0SJiri Olsa 
85e251abeeSArnaldo Carvalho de Melo 	if (evlist && evlist__add_default(evlist)) {
86c12995a5SJiri Olsa 		evlist__delete(evlist);
87b22d54b0SJiri Olsa 		evlist = NULL;
88b22d54b0SJiri Olsa 	}
89b22d54b0SJiri Olsa 
90b22d54b0SJiri Olsa 	return evlist;
91b22d54b0SJiri Olsa }
92b22d54b0SJiri Olsa 
9363503dbaSJiri Olsa struct evlist *perf_evlist__new_dummy(void)
945bae0250SArnaldo Carvalho de Melo {
950f98b11cSJiri Olsa 	struct evlist *evlist = evlist__new();
965bae0250SArnaldo Carvalho de Melo 
97e251abeeSArnaldo Carvalho de Melo 	if (evlist && evlist__add_dummy(evlist)) {
98c12995a5SJiri Olsa 		evlist__delete(evlist);
995bae0250SArnaldo Carvalho de Melo 		evlist = NULL;
1005bae0250SArnaldo Carvalho de Melo 	}
1015bae0250SArnaldo Carvalho de Melo 
1025bae0250SArnaldo Carvalho de Melo 	return evlist;
1035bae0250SArnaldo Carvalho de Melo }
1045bae0250SArnaldo Carvalho de Melo 
10575562573SAdrian Hunter /**
10675562573SAdrian Hunter  * perf_evlist__set_id_pos - set the positions of event ids.
10775562573SAdrian Hunter  * @evlist: selected event list
10875562573SAdrian Hunter  *
10975562573SAdrian Hunter  * Events with compatible sample types all have the same id_pos
11075562573SAdrian Hunter  * and is_pos.  For convenience, put a copy on evlist.
11175562573SAdrian Hunter  */
11263503dbaSJiri Olsa void perf_evlist__set_id_pos(struct evlist *evlist)
11375562573SAdrian Hunter {
114515dbe48SJiri Olsa 	struct evsel *first = evlist__first(evlist);
11575562573SAdrian Hunter 
11675562573SAdrian Hunter 	evlist->id_pos = first->id_pos;
11775562573SAdrian Hunter 	evlist->is_pos = first->is_pos;
11875562573SAdrian Hunter }
11975562573SAdrian Hunter 
12063503dbaSJiri Olsa static void perf_evlist__update_id_pos(struct evlist *evlist)
121733cd2feSAdrian Hunter {
12232dcd021SJiri Olsa 	struct evsel *evsel;
123733cd2feSAdrian Hunter 
124e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, evsel)
1254b5e87b7SArnaldo Carvalho de Melo 		evsel__calc_id_pos(evsel);
126733cd2feSAdrian Hunter 
127733cd2feSAdrian Hunter 	perf_evlist__set_id_pos(evlist);
128733cd2feSAdrian Hunter }
129733cd2feSAdrian Hunter 
130e6b1878dSJiri Olsa static void evlist__purge(struct evlist *evlist)
131361c99a6SArnaldo Carvalho de Melo {
13232dcd021SJiri Olsa 	struct evsel *pos, *n;
133361c99a6SArnaldo Carvalho de Melo 
134e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry_safe(evlist, n, pos) {
135b27c4eceSJiri Olsa 		list_del_init(&pos->core.node);
136d49e4695SArnaldo Carvalho de Melo 		pos->evlist = NULL;
1375eb2dd2aSJiri Olsa 		evsel__delete(pos);
138361c99a6SArnaldo Carvalho de Melo 	}
139361c99a6SArnaldo Carvalho de Melo 
1406484d2f9SJiri Olsa 	evlist->core.nr_entries = 0;
141361c99a6SArnaldo Carvalho de Melo }
142361c99a6SArnaldo Carvalho de Melo 
143470579b0SJiri Olsa void evlist__exit(struct evlist *evlist)
144ef1d1af2SArnaldo Carvalho de Melo {
14504662523SArnaldo Carvalho de Melo 	zfree(&evlist->mmap);
1460b72d69aSWang Nan 	zfree(&evlist->overwrite_mmap);
14793dd6e28SJiri Olsa 	perf_evlist__exit(&evlist->core);
148ef1d1af2SArnaldo Carvalho de Melo }
149ef1d1af2SArnaldo Carvalho de Melo 
150c12995a5SJiri Olsa void evlist__delete(struct evlist *evlist)
151361c99a6SArnaldo Carvalho de Melo {
1520b04b3dcSArnaldo Carvalho de Melo 	if (evlist == NULL)
1530b04b3dcSArnaldo Carvalho de Melo 		return;
1540b04b3dcSArnaldo Carvalho de Melo 
155db6b7b13SJiri Olsa 	evlist__munmap(evlist);
156750b4edeSJiri Olsa 	evlist__close(evlist);
157e6b1878dSJiri Olsa 	evlist__purge(evlist);
158470579b0SJiri Olsa 	evlist__exit(evlist);
159361c99a6SArnaldo Carvalho de Melo 	free(evlist);
160361c99a6SArnaldo Carvalho de Melo }
161361c99a6SArnaldo Carvalho de Melo 
162a1cf3a75SJiri Olsa void evlist__add(struct evlist *evlist, struct evsel *entry)
163361c99a6SArnaldo Carvalho de Melo {
164d49e4695SArnaldo Carvalho de Melo 	entry->evlist = evlist;
1656484d2f9SJiri Olsa 	entry->idx = evlist->core.nr_entries;
16660b0896cSAdrian Hunter 	entry->tracking = !entry->idx;
167ef503831SArnaldo Carvalho de Melo 
1686484d2f9SJiri Olsa 	perf_evlist__add(&evlist->core, &entry->core);
1696484d2f9SJiri Olsa 
1706484d2f9SJiri Olsa 	if (evlist->core.nr_entries == 1)
17175562573SAdrian Hunter 		perf_evlist__set_id_pos(evlist);
172361c99a6SArnaldo Carvalho de Melo }
173361c99a6SArnaldo Carvalho de Melo 
17416251027SJiri Olsa void evlist__remove(struct evlist *evlist, struct evsel *evsel)
1754768230aSAdrian Hunter {
1764768230aSAdrian Hunter 	evsel->evlist = NULL;
17752e22fb8SJiri Olsa 	perf_evlist__remove(&evlist->core, &evsel->core);
1784768230aSAdrian Hunter }
1794768230aSAdrian Hunter 
18063503dbaSJiri Olsa void perf_evlist__splice_list_tail(struct evlist *evlist,
181f114d6efSAdrian Hunter 				   struct list_head *list)
18250d08e47SArnaldo Carvalho de Melo {
18332dcd021SJiri Olsa 	struct evsel *evsel, *temp;
18475562573SAdrian Hunter 
185e5cadb93SArnaldo Carvalho de Melo 	__evlist__for_each_entry_safe(list, temp, evsel) {
186b27c4eceSJiri Olsa 		list_del_init(&evsel->core.node);
187a1cf3a75SJiri Olsa 		evlist__add(evlist, evsel);
188f114d6efSAdrian Hunter 	}
18950d08e47SArnaldo Carvalho de Melo }
19050d08e47SArnaldo Carvalho de Melo 
191c0e53476SArnaldo Carvalho de Melo int __evlist__set_tracepoints_handlers(struct evlist *evlist,
192c0e53476SArnaldo Carvalho de Melo 				       const struct evsel_str_handler *assocs, size_t nr_assocs)
193c0e53476SArnaldo Carvalho de Melo {
194c0e53476SArnaldo Carvalho de Melo 	size_t i;
195c0e53476SArnaldo Carvalho de Melo 	int err;
196c0e53476SArnaldo Carvalho de Melo 
197c0e53476SArnaldo Carvalho de Melo 	for (i = 0; i < nr_assocs; i++) {
198c0e53476SArnaldo Carvalho de Melo 		// Adding a handler for an event not in this evlist, just ignore it.
199b02736f7SArnaldo Carvalho de Melo 		struct evsel *evsel = evlist__find_tracepoint_by_name(evlist, assocs[i].name);
200c0e53476SArnaldo Carvalho de Melo 		if (evsel == NULL)
201c0e53476SArnaldo Carvalho de Melo 			continue;
202c0e53476SArnaldo Carvalho de Melo 
203c0e53476SArnaldo Carvalho de Melo 		err = -EEXIST;
204c0e53476SArnaldo Carvalho de Melo 		if (evsel->handler != NULL)
205c0e53476SArnaldo Carvalho de Melo 			goto out;
206c0e53476SArnaldo Carvalho de Melo 		evsel->handler = assocs[i].handler;
207c0e53476SArnaldo Carvalho de Melo 	}
208c0e53476SArnaldo Carvalho de Melo 
209c0e53476SArnaldo Carvalho de Melo 	err = 0;
210c0e53476SArnaldo Carvalho de Melo out:
211c0e53476SArnaldo Carvalho de Melo 	return err;
212c0e53476SArnaldo Carvalho de Melo }
213c0e53476SArnaldo Carvalho de Melo 
214a622eafaSArnaldo Carvalho de Melo void __evlist__set_leader(struct list_head *list)
21563dab225SArnaldo Carvalho de Melo {
21632dcd021SJiri Olsa 	struct evsel *evsel, *leader;
21763dab225SArnaldo Carvalho de Melo 
218b27c4eceSJiri Olsa 	leader = list_entry(list->next, struct evsel, core.node);
219b27c4eceSJiri Olsa 	evsel = list_entry(list->prev, struct evsel, core.node);
22097f63e4aSNamhyung Kim 
2215643b1a5SJiri Olsa 	leader->core.nr_members = evsel->idx - leader->idx + 1;
22263dab225SArnaldo Carvalho de Melo 
223e5cadb93SArnaldo Carvalho de Melo 	__evlist__for_each_entry(list, evsel) {
22463dab225SArnaldo Carvalho de Melo 		evsel->leader = leader;
22563dab225SArnaldo Carvalho de Melo 	}
22663dab225SArnaldo Carvalho de Melo }
22763dab225SArnaldo Carvalho de Melo 
228a622eafaSArnaldo Carvalho de Melo void evlist__set_leader(struct evlist *evlist)
2296a4bb04cSJiri Olsa {
2306484d2f9SJiri Olsa 	if (evlist->core.nr_entries) {
2316484d2f9SJiri Olsa 		evlist->nr_groups = evlist->core.nr_entries > 1 ? 1 : 0;
232a622eafaSArnaldo Carvalho de Melo 		__evlist__set_leader(&evlist->core.entries);
2336a4bb04cSJiri Olsa 	}
23497f63e4aSNamhyung Kim }
2356a4bb04cSJiri Olsa 
236e251abeeSArnaldo Carvalho de Melo int __evlist__add_default(struct evlist *evlist, bool precise)
237361c99a6SArnaldo Carvalho de Melo {
2388f6725a2SArnaldo Carvalho de Melo 	struct evsel *evsel = evsel__new_cycles(precise);
239361c99a6SArnaldo Carvalho de Melo 
240361c99a6SArnaldo Carvalho de Melo 	if (evsel == NULL)
2417c48dcfdSArnaldo Carvalho de Melo 		return -ENOMEM;
242361c99a6SArnaldo Carvalho de Melo 
243a1cf3a75SJiri Olsa 	evlist__add(evlist, evsel);
244361c99a6SArnaldo Carvalho de Melo 	return 0;
245361c99a6SArnaldo Carvalho de Melo }
2465c581041SArnaldo Carvalho de Melo 
247e251abeeSArnaldo Carvalho de Melo int evlist__add_dummy(struct evlist *evlist)
2485bae0250SArnaldo Carvalho de Melo {
2495bae0250SArnaldo Carvalho de Melo 	struct perf_event_attr attr = {
2505bae0250SArnaldo Carvalho de Melo 		.type	= PERF_TYPE_SOFTWARE,
2515bae0250SArnaldo Carvalho de Melo 		.config = PERF_COUNT_SW_DUMMY,
2525bae0250SArnaldo Carvalho de Melo 		.size	= sizeof(attr), /* to capture ABI version */
2535bae0250SArnaldo Carvalho de Melo 	};
2548f6725a2SArnaldo Carvalho de Melo 	struct evsel *evsel = evsel__new_idx(&attr, evlist->core.nr_entries);
2555bae0250SArnaldo Carvalho de Melo 
2565bae0250SArnaldo Carvalho de Melo 	if (evsel == NULL)
2575bae0250SArnaldo Carvalho de Melo 		return -ENOMEM;
2585bae0250SArnaldo Carvalho de Melo 
259a1cf3a75SJiri Olsa 	evlist__add(evlist, evsel);
2605bae0250SArnaldo Carvalho de Melo 	return 0;
2615bae0250SArnaldo Carvalho de Melo }
2625bae0250SArnaldo Carvalho de Melo 
263e251abeeSArnaldo Carvalho de Melo static int evlist__add_attrs(struct evlist *evlist, struct perf_event_attr *attrs, size_t nr_attrs)
26450d08e47SArnaldo Carvalho de Melo {
26532dcd021SJiri Olsa 	struct evsel *evsel, *n;
26650d08e47SArnaldo Carvalho de Melo 	LIST_HEAD(head);
26750d08e47SArnaldo Carvalho de Melo 	size_t i;
26850d08e47SArnaldo Carvalho de Melo 
26950d08e47SArnaldo Carvalho de Melo 	for (i = 0; i < nr_attrs; i++) {
2708f6725a2SArnaldo Carvalho de Melo 		evsel = evsel__new_idx(attrs + i, evlist->core.nr_entries + i);
27150d08e47SArnaldo Carvalho de Melo 		if (evsel == NULL)
27250d08e47SArnaldo Carvalho de Melo 			goto out_delete_partial_list;
273b27c4eceSJiri Olsa 		list_add_tail(&evsel->core.node, &head);
27450d08e47SArnaldo Carvalho de Melo 	}
27550d08e47SArnaldo Carvalho de Melo 
276f114d6efSAdrian Hunter 	perf_evlist__splice_list_tail(evlist, &head);
27750d08e47SArnaldo Carvalho de Melo 
27850d08e47SArnaldo Carvalho de Melo 	return 0;
27950d08e47SArnaldo Carvalho de Melo 
28050d08e47SArnaldo Carvalho de Melo out_delete_partial_list:
281e5cadb93SArnaldo Carvalho de Melo 	__evlist__for_each_entry_safe(&head, n, evsel)
2825eb2dd2aSJiri Olsa 		evsel__delete(evsel);
28350d08e47SArnaldo Carvalho de Melo 	return -1;
28450d08e47SArnaldo Carvalho de Melo }
28550d08e47SArnaldo Carvalho de Melo 
286e251abeeSArnaldo Carvalho de Melo int __evlist__add_default_attrs(struct evlist *evlist, struct perf_event_attr *attrs, size_t nr_attrs)
28779695e1bSArnaldo Carvalho de Melo {
28879695e1bSArnaldo Carvalho de Melo 	size_t i;
28979695e1bSArnaldo Carvalho de Melo 
29079695e1bSArnaldo Carvalho de Melo 	for (i = 0; i < nr_attrs; i++)
29179695e1bSArnaldo Carvalho de Melo 		event_attr_init(attrs + i);
29279695e1bSArnaldo Carvalho de Melo 
293a1cf3a75SJiri Olsa 	return evlist__add_attrs(evlist, attrs, nr_attrs);
29479695e1bSArnaldo Carvalho de Melo }
29579695e1bSArnaldo Carvalho de Melo 
296b02736f7SArnaldo Carvalho de Melo struct evsel *evlist__find_tracepoint_by_id(struct evlist *evlist, int id)
297ee29be62SArnaldo Carvalho de Melo {
29832dcd021SJiri Olsa 	struct evsel *evsel;
299ee29be62SArnaldo Carvalho de Melo 
300e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, evsel) {
3011fc632ceSJiri Olsa 		if (evsel->core.attr.type   == PERF_TYPE_TRACEPOINT &&
3021fc632ceSJiri Olsa 		    (int)evsel->core.attr.config == id)
303ee29be62SArnaldo Carvalho de Melo 			return evsel;
304ee29be62SArnaldo Carvalho de Melo 	}
305ee29be62SArnaldo Carvalho de Melo 
306ee29be62SArnaldo Carvalho de Melo 	return NULL;
307ee29be62SArnaldo Carvalho de Melo }
308ee29be62SArnaldo Carvalho de Melo 
309b02736f7SArnaldo Carvalho de Melo struct evsel *evlist__find_tracepoint_by_name(struct evlist *evlist, const char *name)
310a2f2804aSDavid Ahern {
31132dcd021SJiri Olsa 	struct evsel *evsel;
312a2f2804aSDavid Ahern 
313e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, evsel) {
3141fc632ceSJiri Olsa 		if ((evsel->core.attr.type == PERF_TYPE_TRACEPOINT) &&
315a2f2804aSDavid Ahern 		    (strcmp(evsel->name, name) == 0))
316a2f2804aSDavid Ahern 			return evsel;
317a2f2804aSDavid Ahern 	}
318a2f2804aSDavid Ahern 
319a2f2804aSDavid Ahern 	return NULL;
320a2f2804aSDavid Ahern }
321a2f2804aSDavid Ahern 
322e251abeeSArnaldo Carvalho de Melo int evlist__add_newtp(struct evlist *evlist, const char *sys, const char *name, void *handler)
32339876e7dSArnaldo Carvalho de Melo {
3248f6725a2SArnaldo Carvalho de Melo 	struct evsel *evsel = evsel__newtp(sys, name);
32539876e7dSArnaldo Carvalho de Melo 
3268dd2a131SJiri Olsa 	if (IS_ERR(evsel))
32739876e7dSArnaldo Carvalho de Melo 		return -1;
32839876e7dSArnaldo Carvalho de Melo 
329744a9719SArnaldo Carvalho de Melo 	evsel->handler = handler;
330a1cf3a75SJiri Olsa 	evlist__add(evlist, evsel);
33139876e7dSArnaldo Carvalho de Melo 	return 0;
33239876e7dSArnaldo Carvalho de Melo }
33339876e7dSArnaldo Carvalho de Melo 
33463503dbaSJiri Olsa static int perf_evlist__nr_threads(struct evlist *evlist,
33532dcd021SJiri Olsa 				   struct evsel *evsel)
336bf8e8f4bSAdrian Hunter {
337648b5af3SJiri Olsa 	if (evsel->core.system_wide)
338bf8e8f4bSAdrian Hunter 		return 1;
339bf8e8f4bSAdrian Hunter 	else
340a2f354e3SJiri Olsa 		return perf_thread_map__nr(evlist->core.threads);
341bf8e8f4bSAdrian Hunter }
342bf8e8f4bSAdrian Hunter 
343a8cbe40fSAndi Kleen void evlist__cpu_iter_start(struct evlist *evlist)
344a8cbe40fSAndi Kleen {
345a8cbe40fSAndi Kleen 	struct evsel *pos;
346a8cbe40fSAndi Kleen 
347a8cbe40fSAndi Kleen 	/*
348a8cbe40fSAndi Kleen 	 * Reset the per evsel cpu_iter. This is needed because
349a8cbe40fSAndi Kleen 	 * each evsel's cpumap may have a different index space,
350a8cbe40fSAndi Kleen 	 * and some operations need the index to modify
351a8cbe40fSAndi Kleen 	 * the FD xyarray (e.g. open, close)
352a8cbe40fSAndi Kleen 	 */
353a8cbe40fSAndi Kleen 	evlist__for_each_entry(evlist, pos)
354a8cbe40fSAndi Kleen 		pos->cpu_iter = 0;
355a8cbe40fSAndi Kleen }
356a8cbe40fSAndi Kleen 
357a8cbe40fSAndi Kleen bool evsel__cpu_iter_skip_no_inc(struct evsel *ev, int cpu)
358a8cbe40fSAndi Kleen {
359a8cbe40fSAndi Kleen 	if (ev->cpu_iter >= ev->core.cpus->nr)
360a8cbe40fSAndi Kleen 		return true;
361a8cbe40fSAndi Kleen 	if (cpu >= 0 && ev->core.cpus->map[ev->cpu_iter] != cpu)
362a8cbe40fSAndi Kleen 		return true;
363a8cbe40fSAndi Kleen 	return false;
364a8cbe40fSAndi Kleen }
365a8cbe40fSAndi Kleen 
366a8cbe40fSAndi Kleen bool evsel__cpu_iter_skip(struct evsel *ev, int cpu)
367a8cbe40fSAndi Kleen {
368a8cbe40fSAndi Kleen 	if (!evsel__cpu_iter_skip_no_inc(ev, cpu)) {
369a8cbe40fSAndi Kleen 		ev->cpu_iter++;
370a8cbe40fSAndi Kleen 		return false;
371a8cbe40fSAndi Kleen 	}
372a8cbe40fSAndi Kleen 	return true;
373a8cbe40fSAndi Kleen }
374a8cbe40fSAndi Kleen 
375e74676deSJiri Olsa void evlist__disable(struct evlist *evlist)
3764152ab37SArnaldo Carvalho de Melo {
37732dcd021SJiri Olsa 	struct evsel *pos;
378704e2f5bSAndi Kleen 	struct affinity affinity;
37987cf8360SAdrian Hunter 	int cpu, i, imm = 0;
38087cf8360SAdrian Hunter 	bool has_imm = false;
381704e2f5bSAndi Kleen 
382704e2f5bSAndi Kleen 	if (affinity__setup(&affinity) < 0)
383704e2f5bSAndi Kleen 		return;
384704e2f5bSAndi Kleen 
38587cf8360SAdrian Hunter 	/* Disable 'immediate' events last */
38687cf8360SAdrian Hunter 	for (imm = 0; imm <= 1; imm++) {
387704e2f5bSAndi Kleen 		evlist__for_each_cpu(evlist, i, cpu) {
388704e2f5bSAndi Kleen 			affinity__set(&affinity, cpu);
3894152ab37SArnaldo Carvalho de Melo 
390e5cadb93SArnaldo Carvalho de Melo 			evlist__for_each_entry(evlist, pos) {
391704e2f5bSAndi Kleen 				if (evsel__cpu_iter_skip(pos, cpu))
392704e2f5bSAndi Kleen 					continue;
393c754c382SArnaldo Carvalho de Melo 				if (pos->disabled || !evsel__is_group_leader(pos) || !pos->core.fd)
3943fe4430dSJiri Olsa 					continue;
39587cf8360SAdrian Hunter 				if (pos->immediate)
39687cf8360SAdrian Hunter 					has_imm = true;
39787cf8360SAdrian Hunter 				if (pos->immediate != imm)
39887cf8360SAdrian Hunter 					continue;
399704e2f5bSAndi Kleen 				evsel__disable_cpu(pos, pos->cpu_iter - 1);
400704e2f5bSAndi Kleen 			}
401704e2f5bSAndi Kleen 		}
40287cf8360SAdrian Hunter 		if (!has_imm)
40387cf8360SAdrian Hunter 			break;
40487cf8360SAdrian Hunter 	}
40587cf8360SAdrian Hunter 
406704e2f5bSAndi Kleen 	affinity__cleanup(&affinity);
407704e2f5bSAndi Kleen 	evlist__for_each_entry(evlist, pos) {
408c754c382SArnaldo Carvalho de Melo 		if (!evsel__is_group_leader(pos) || !pos->core.fd)
409704e2f5bSAndi Kleen 			continue;
410704e2f5bSAndi Kleen 		pos->disabled = true;
4114152ab37SArnaldo Carvalho de Melo 	}
4122b56bcfbSArnaldo Carvalho de Melo 
4132b56bcfbSArnaldo Carvalho de Melo 	evlist->enabled = false;
4144152ab37SArnaldo Carvalho de Melo }
4154152ab37SArnaldo Carvalho de Melo 
4161c87f165SJiri Olsa void evlist__enable(struct evlist *evlist)
417764e16a3SDavid Ahern {
41832dcd021SJiri Olsa 	struct evsel *pos;
419704e2f5bSAndi Kleen 	struct affinity affinity;
420704e2f5bSAndi Kleen 	int cpu, i;
421764e16a3SDavid Ahern 
422704e2f5bSAndi Kleen 	if (affinity__setup(&affinity) < 0)
423704e2f5bSAndi Kleen 		return;
424704e2f5bSAndi Kleen 
425704e2f5bSAndi Kleen 	evlist__for_each_cpu(evlist, i, cpu) {
426704e2f5bSAndi Kleen 		affinity__set(&affinity, cpu);
427704e2f5bSAndi Kleen 
428704e2f5bSAndi Kleen 		evlist__for_each_entry(evlist, pos) {
429704e2f5bSAndi Kleen 			if (evsel__cpu_iter_skip(pos, cpu))
430704e2f5bSAndi Kleen 				continue;
431c754c382SArnaldo Carvalho de Melo 			if (!evsel__is_group_leader(pos) || !pos->core.fd)
432704e2f5bSAndi Kleen 				continue;
433704e2f5bSAndi Kleen 			evsel__enable_cpu(pos, pos->cpu_iter - 1);
434704e2f5bSAndi Kleen 		}
435704e2f5bSAndi Kleen 	}
436704e2f5bSAndi Kleen 	affinity__cleanup(&affinity);
437e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, pos) {
438c754c382SArnaldo Carvalho de Melo 		if (!evsel__is_group_leader(pos) || !pos->core.fd)
4393fe4430dSJiri Olsa 			continue;
440704e2f5bSAndi Kleen 		pos->disabled = false;
441764e16a3SDavid Ahern 	}
4422b56bcfbSArnaldo Carvalho de Melo 
4432b56bcfbSArnaldo Carvalho de Melo 	evlist->enabled = true;
4442b56bcfbSArnaldo Carvalho de Melo }
4452b56bcfbSArnaldo Carvalho de Melo 
446ade9d208SArnaldo Carvalho de Melo void evlist__toggle_enable(struct evlist *evlist)
4472b56bcfbSArnaldo Carvalho de Melo {
448e74676deSJiri Olsa 	(evlist->enabled ? evlist__disable : evlist__enable)(evlist);
449764e16a3SDavid Ahern }
450764e16a3SDavid Ahern 
45163503dbaSJiri Olsa static int perf_evlist__enable_event_cpu(struct evlist *evlist,
45232dcd021SJiri Olsa 					 struct evsel *evsel, int cpu)
4531c65056cSAdrian Hunter {
45418ef15c6SArnaldo Carvalho de Melo 	int thread;
4551c65056cSAdrian Hunter 	int nr_threads = perf_evlist__nr_threads(evlist, evsel);
4561c65056cSAdrian Hunter 
4579dfcb759SJiri Olsa 	if (!evsel->core.fd)
4581c65056cSAdrian Hunter 		return -EINVAL;
4591c65056cSAdrian Hunter 
4601c65056cSAdrian Hunter 	for (thread = 0; thread < nr_threads; thread++) {
46118ef15c6SArnaldo Carvalho de Melo 		int err = ioctl(FD(evsel, cpu, thread), PERF_EVENT_IOC_ENABLE, 0);
4621c65056cSAdrian Hunter 		if (err)
4631c65056cSAdrian Hunter 			return err;
4641c65056cSAdrian Hunter 	}
4651c65056cSAdrian Hunter 	return 0;
4661c65056cSAdrian Hunter }
4671c65056cSAdrian Hunter 
46863503dbaSJiri Olsa static int perf_evlist__enable_event_thread(struct evlist *evlist,
46932dcd021SJiri Olsa 					    struct evsel *evsel,
4701c65056cSAdrian Hunter 					    int thread)
4711c65056cSAdrian Hunter {
47218ef15c6SArnaldo Carvalho de Melo 	int cpu;
4736549cd8fSJiri Olsa 	int nr_cpus = perf_cpu_map__nr(evlist->core.cpus);
4741c65056cSAdrian Hunter 
4759dfcb759SJiri Olsa 	if (!evsel->core.fd)
4761c65056cSAdrian Hunter 		return -EINVAL;
4771c65056cSAdrian Hunter 
4781c65056cSAdrian Hunter 	for (cpu = 0; cpu < nr_cpus; cpu++) {
47918ef15c6SArnaldo Carvalho de Melo 		int err = ioctl(FD(evsel, cpu, thread), PERF_EVENT_IOC_ENABLE, 0);
4801c65056cSAdrian Hunter 		if (err)
4811c65056cSAdrian Hunter 			return err;
4821c65056cSAdrian Hunter 	}
4831c65056cSAdrian Hunter 	return 0;
4841c65056cSAdrian Hunter }
4851c65056cSAdrian Hunter 
48663503dbaSJiri Olsa int perf_evlist__enable_event_idx(struct evlist *evlist,
48732dcd021SJiri Olsa 				  struct evsel *evsel, int idx)
4881c65056cSAdrian Hunter {
489315c0a1fSJiri Olsa 	bool per_cpu_mmaps = !perf_cpu_map__empty(evlist->core.cpus);
4901c65056cSAdrian Hunter 
4911c65056cSAdrian Hunter 	if (per_cpu_mmaps)
4921c65056cSAdrian Hunter 		return perf_evlist__enable_event_cpu(evlist, evsel, idx);
4931c65056cSAdrian Hunter 	else
4941c65056cSAdrian Hunter 		return perf_evlist__enable_event_thread(evlist, evsel, idx);
4951c65056cSAdrian Hunter }
4961c65056cSAdrian Hunter 
497f4009e7bSJiri Olsa int evlist__add_pollfd(struct evlist *evlist, int fd)
498e4b356b5SArnaldo Carvalho de Melo {
499ab4c1f9fSAlexey Budankov 	return perf_evlist__add_pollfd(&evlist->core, fd, NULL, POLLIN, fdarray_flag__default);
500e4b356b5SArnaldo Carvalho de Melo }
5011b85337dSArnaldo Carvalho de Melo 
502f4009e7bSJiri Olsa int evlist__filter_pollfd(struct evlist *evlist, short revents_and_mask)
5031ddec7f0SArnaldo Carvalho de Melo {
50484227cb1SJiri Olsa 	return perf_evlist__filter_pollfd(&evlist->core, revents_and_mask);
5051ddec7f0SArnaldo Carvalho de Melo }
5061ddec7f0SArnaldo Carvalho de Melo 
50780ab2987SJiri Olsa int evlist__poll(struct evlist *evlist, int timeout)
508f66a889dSArnaldo Carvalho de Melo {
50980ab2987SJiri Olsa 	return perf_evlist__poll(&evlist->core, timeout);
510f66a889dSArnaldo Carvalho de Melo }
511f66a889dSArnaldo Carvalho de Melo 
512*3ccf8a7bSArnaldo Carvalho de Melo struct perf_sample_id *evlist__id2sid(struct evlist *evlist, u64 id)
51370db7533SArnaldo Carvalho de Melo {
51470db7533SArnaldo Carvalho de Melo 	struct hlist_head *head;
51570db7533SArnaldo Carvalho de Melo 	struct perf_sample_id *sid;
51670db7533SArnaldo Carvalho de Melo 	int hash;
51770db7533SArnaldo Carvalho de Melo 
51870db7533SArnaldo Carvalho de Melo 	hash = hash_64(id, PERF_EVLIST__HLIST_BITS);
5191d5af02dSJiri Olsa 	head = &evlist->core.heads[hash];
52070db7533SArnaldo Carvalho de Melo 
521b67bfe0dSSasha Levin 	hlist_for_each_entry(sid, head, node)
52270db7533SArnaldo Carvalho de Melo 		if (sid->id == id)
523932a3594SJiri Olsa 			return sid;
524932a3594SJiri Olsa 
525932a3594SJiri Olsa 	return NULL;
526932a3594SJiri Olsa }
527932a3594SJiri Olsa 
528*3ccf8a7bSArnaldo Carvalho de Melo struct evsel *evlist__id2evsel(struct evlist *evlist, u64 id)
529932a3594SJiri Olsa {
530932a3594SJiri Olsa 	struct perf_sample_id *sid;
531932a3594SJiri Olsa 
5326484d2f9SJiri Olsa 	if (evlist->core.nr_entries == 1 || !id)
533515dbe48SJiri Olsa 		return evlist__first(evlist);
534932a3594SJiri Olsa 
535*3ccf8a7bSArnaldo Carvalho de Melo 	sid = evlist__id2sid(evlist, id);
536932a3594SJiri Olsa 	if (sid)
53770c20369SJiri Olsa 		return container_of(sid->evsel, struct evsel, core);
53830e68bccSNamhyung Kim 
5398cedf3a5SArnaldo Carvalho de Melo 	if (!evlist__sample_id_all(evlist))
540515dbe48SJiri Olsa 		return evlist__first(evlist);
54130e68bccSNamhyung Kim 
54270db7533SArnaldo Carvalho de Melo 	return NULL;
54370db7533SArnaldo Carvalho de Melo }
54404391debSArnaldo Carvalho de Melo 
545*3ccf8a7bSArnaldo Carvalho de Melo struct evsel *evlist__id2evsel_strict(struct evlist *evlist, u64 id)
546dddcf6abSAdrian Hunter {
547dddcf6abSAdrian Hunter 	struct perf_sample_id *sid;
548dddcf6abSAdrian Hunter 
549dddcf6abSAdrian Hunter 	if (!id)
550dddcf6abSAdrian Hunter 		return NULL;
551dddcf6abSAdrian Hunter 
552*3ccf8a7bSArnaldo Carvalho de Melo 	sid = evlist__id2sid(evlist, id);
553dddcf6abSAdrian Hunter 	if (sid)
55470c20369SJiri Olsa 		return container_of(sid->evsel, struct evsel, core);
555dddcf6abSAdrian Hunter 
556dddcf6abSAdrian Hunter 	return NULL;
557dddcf6abSAdrian Hunter }
558dddcf6abSAdrian Hunter 
559*3ccf8a7bSArnaldo Carvalho de Melo static int evlist__event2id(struct evlist *evlist, union perf_event *event, u64 *id)
56075562573SAdrian Hunter {
561b1fcd190SJiri Olsa 	const __u64 *array = event->sample.array;
56275562573SAdrian Hunter 	ssize_t n;
56375562573SAdrian Hunter 
56475562573SAdrian Hunter 	n = (event->header.size - sizeof(event->header)) >> 3;
56575562573SAdrian Hunter 
56675562573SAdrian Hunter 	if (event->header.type == PERF_RECORD_SAMPLE) {
56775562573SAdrian Hunter 		if (evlist->id_pos >= n)
56875562573SAdrian Hunter 			return -1;
56975562573SAdrian Hunter 		*id = array[evlist->id_pos];
57075562573SAdrian Hunter 	} else {
57175562573SAdrian Hunter 		if (evlist->is_pos > n)
57275562573SAdrian Hunter 			return -1;
57375562573SAdrian Hunter 		n -= evlist->is_pos;
57475562573SAdrian Hunter 		*id = array[n];
57575562573SAdrian Hunter 	}
57675562573SAdrian Hunter 	return 0;
57775562573SAdrian Hunter }
57875562573SAdrian Hunter 
579*3ccf8a7bSArnaldo Carvalho de Melo struct evsel *evlist__event2evsel(struct evlist *evlist, union perf_event *event)
58075562573SAdrian Hunter {
581515dbe48SJiri Olsa 	struct evsel *first = evlist__first(evlist);
58275562573SAdrian Hunter 	struct hlist_head *head;
58375562573SAdrian Hunter 	struct perf_sample_id *sid;
58475562573SAdrian Hunter 	int hash;
58575562573SAdrian Hunter 	u64 id;
58675562573SAdrian Hunter 
5876484d2f9SJiri Olsa 	if (evlist->core.nr_entries == 1)
58898be6966SAdrian Hunter 		return first;
58998be6966SAdrian Hunter 
5901fc632ceSJiri Olsa 	if (!first->core.attr.sample_id_all &&
59198be6966SAdrian Hunter 	    event->header.type != PERF_RECORD_SAMPLE)
59298be6966SAdrian Hunter 		return first;
59375562573SAdrian Hunter 
594*3ccf8a7bSArnaldo Carvalho de Melo 	if (evlist__event2id(evlist, event, &id))
59575562573SAdrian Hunter 		return NULL;
59675562573SAdrian Hunter 
59775562573SAdrian Hunter 	/* Synthesized events have an id of zero */
59875562573SAdrian Hunter 	if (!id)
59998be6966SAdrian Hunter 		return first;
60075562573SAdrian Hunter 
60175562573SAdrian Hunter 	hash = hash_64(id, PERF_EVLIST__HLIST_BITS);
6021d5af02dSJiri Olsa 	head = &evlist->core.heads[hash];
60375562573SAdrian Hunter 
60475562573SAdrian Hunter 	hlist_for_each_entry(sid, head, node) {
60575562573SAdrian Hunter 		if (sid->id == id)
60670c20369SJiri Olsa 			return container_of(sid->evsel, struct evsel, core);
60775562573SAdrian Hunter 	}
60875562573SAdrian Hunter 	return NULL;
60975562573SAdrian Hunter }
61075562573SAdrian Hunter 
61163503dbaSJiri Olsa static int perf_evlist__set_paused(struct evlist *evlist, bool value)
61265aea233SWang Nan {
61365aea233SWang Nan 	int i;
61465aea233SWang Nan 
6150b72d69aSWang Nan 	if (!evlist->overwrite_mmap)
616078c3386SWang Nan 		return 0;
617078c3386SWang Nan 
618c976ee11SJiri Olsa 	for (i = 0; i < evlist->core.nr_mmaps; i++) {
6192cf07b29SJiri Olsa 		int fd = evlist->overwrite_mmap[i].core.fd;
62065aea233SWang Nan 		int err;
62165aea233SWang Nan 
62265aea233SWang Nan 		if (fd < 0)
62365aea233SWang Nan 			continue;
62465aea233SWang Nan 		err = ioctl(fd, PERF_EVENT_IOC_PAUSE_OUTPUT, value ? 1 : 0);
62565aea233SWang Nan 		if (err)
62665aea233SWang Nan 			return err;
62765aea233SWang Nan 	}
62865aea233SWang Nan 	return 0;
62965aea233SWang Nan }
63065aea233SWang Nan 
63163503dbaSJiri Olsa static int perf_evlist__pause(struct evlist *evlist)
63265aea233SWang Nan {
63365aea233SWang Nan 	return perf_evlist__set_paused(evlist, true);
63465aea233SWang Nan }
63565aea233SWang Nan 
63663503dbaSJiri Olsa static int perf_evlist__resume(struct evlist *evlist)
63765aea233SWang Nan {
63865aea233SWang Nan 	return perf_evlist__set_paused(evlist, false);
63965aea233SWang Nan }
64065aea233SWang Nan 
641db6b7b13SJiri Olsa static void evlist__munmap_nofree(struct evlist *evlist)
642f8a95309SArnaldo Carvalho de Melo {
643aece948fSArnaldo Carvalho de Melo 	int i;
644f8a95309SArnaldo Carvalho de Melo 
645b2cb615dSWang Nan 	if (evlist->mmap)
646c976ee11SJiri Olsa 		for (i = 0; i < evlist->core.nr_mmaps; i++)
64780e53d11SJiri Olsa 			perf_mmap__munmap(&evlist->mmap[i].core);
648b2cb615dSWang Nan 
6490b72d69aSWang Nan 	if (evlist->overwrite_mmap)
650c976ee11SJiri Olsa 		for (i = 0; i < evlist->core.nr_mmaps; i++)
65180e53d11SJiri Olsa 			perf_mmap__munmap(&evlist->overwrite_mmap[i].core);
652a1f72618SWang Nan }
653aece948fSArnaldo Carvalho de Melo 
654db6b7b13SJiri Olsa void evlist__munmap(struct evlist *evlist)
655a1f72618SWang Nan {
656db6b7b13SJiri Olsa 	evlist__munmap_nofree(evlist);
65704662523SArnaldo Carvalho de Melo 	zfree(&evlist->mmap);
6580b72d69aSWang Nan 	zfree(&evlist->overwrite_mmap);
659f8a95309SArnaldo Carvalho de Melo }
660f8a95309SArnaldo Carvalho de Melo 
66180e53d11SJiri Olsa static void perf_mmap__unmap_cb(struct perf_mmap *map)
66280e53d11SJiri Olsa {
66380e53d11SJiri Olsa 	struct mmap *m = container_of(map, struct mmap, core);
66480e53d11SJiri Olsa 
66580e53d11SJiri Olsa 	mmap__munmap(m);
66680e53d11SJiri Olsa }
66780e53d11SJiri Olsa 
668d50cf361SJiri Olsa static struct mmap *evlist__alloc_mmap(struct evlist *evlist,
6692c5f6d87SKan Liang 				       bool overwrite)
670f8a95309SArnaldo Carvalho de Melo {
671d4c6fb36SWang Nan 	int i;
672a5830532SJiri Olsa 	struct mmap *map;
673d4c6fb36SWang Nan 
674c976ee11SJiri Olsa 	map = zalloc(evlist->core.nr_mmaps * sizeof(struct mmap));
6758db6d6b1SWang Nan 	if (!map)
6768db6d6b1SWang Nan 		return NULL;
677946ae1d4SWang Nan 
678c976ee11SJiri Olsa 	for (i = 0; i < evlist->core.nr_mmaps; i++) {
6796eb65f7aSJiri Olsa 		struct perf_mmap *prev = i ? &map[i - 1].core : NULL;
6806eb65f7aSJiri Olsa 
6814738ca30SArnaldo Carvalho de Melo 		/*
6824738ca30SArnaldo Carvalho de Melo 		 * When the perf_mmap() call is made we grab one refcount, plus
6836afad54dSKan Liang 		 * one extra to let perf_mmap__consume() get the last
6844738ca30SArnaldo Carvalho de Melo 		 * events after all real references (perf_mmap__get()) are
6854738ca30SArnaldo Carvalho de Melo 		 * dropped.
6864738ca30SArnaldo Carvalho de Melo 		 *
6874738ca30SArnaldo Carvalho de Melo 		 * Each PERF_EVENT_IOC_SET_OUTPUT points to this mmap and
6884738ca30SArnaldo Carvalho de Melo 		 * thus does perf_mmap__get() on it.
6894738ca30SArnaldo Carvalho de Melo 		 */
6906eb65f7aSJiri Olsa 		perf_mmap__init(&map[i].core, prev, overwrite, perf_mmap__unmap_cb);
6914738ca30SArnaldo Carvalho de Melo 	}
692353120b4SJiri Olsa 
6938db6d6b1SWang Nan 	return map;
694f8a95309SArnaldo Carvalho de Melo }
695f8a95309SArnaldo Carvalho de Melo 
6969abd2ab2SJiri Olsa static void
6979abd2ab2SJiri Olsa perf_evlist__mmap_cb_idx(struct perf_evlist *_evlist,
6989abd2ab2SJiri Olsa 			 struct perf_mmap_param *_mp,
6999abd2ab2SJiri Olsa 			 int idx, bool per_cpu)
7009abd2ab2SJiri Olsa {
7019abd2ab2SJiri Olsa 	struct evlist *evlist = container_of(_evlist, struct evlist, core);
7029abd2ab2SJiri Olsa 	struct mmap_params *mp = container_of(_mp, struct mmap_params, core);
7039abd2ab2SJiri Olsa 
7049abd2ab2SJiri Olsa 	auxtrace_mmap_params__set_idx(&mp->auxtrace_mp, evlist, idx, per_cpu);
7059abd2ab2SJiri Olsa }
7069abd2ab2SJiri Olsa 
707bb1b1885SJiri Olsa static struct perf_mmap*
708bb1b1885SJiri Olsa perf_evlist__mmap_cb_get(struct perf_evlist *_evlist, bool overwrite, int idx)
709bb1b1885SJiri Olsa {
710bb1b1885SJiri Olsa 	struct evlist *evlist = container_of(_evlist, struct evlist, core);
7113805e4f3SJiri Olsa 	struct mmap *maps;
712bb1b1885SJiri Olsa 
7133805e4f3SJiri Olsa 	maps = overwrite ? evlist->overwrite_mmap : evlist->mmap;
714bb1b1885SJiri Olsa 
715bb1b1885SJiri Olsa 	if (!maps) {
7163805e4f3SJiri Olsa 		maps = evlist__alloc_mmap(evlist, overwrite);
717bb1b1885SJiri Olsa 		if (!maps)
718bb1b1885SJiri Olsa 			return NULL;
719bb1b1885SJiri Olsa 
7203805e4f3SJiri Olsa 		if (overwrite) {
721bb1b1885SJiri Olsa 			evlist->overwrite_mmap = maps;
722bb1b1885SJiri Olsa 			if (evlist->bkw_mmap_state == BKW_MMAP_NOTREADY)
723ade9d208SArnaldo Carvalho de Melo 				evlist__toggle_bkw_mmap(evlist, BKW_MMAP_RUNNING);
7243805e4f3SJiri Olsa 		} else {
7253805e4f3SJiri Olsa 			evlist->mmap = maps;
726bb1b1885SJiri Olsa 		}
727bb1b1885SJiri Olsa 	}
728bb1b1885SJiri Olsa 
729bb1b1885SJiri Olsa 	return &maps[idx].core;
730bb1b1885SJiri Olsa }
731bb1b1885SJiri Olsa 
732b80132b1SJiri Olsa static int
733b80132b1SJiri Olsa perf_evlist__mmap_cb_mmap(struct perf_mmap *_map, struct perf_mmap_param *_mp,
734b80132b1SJiri Olsa 			  int output, int cpu)
735b80132b1SJiri Olsa {
736b80132b1SJiri Olsa 	struct mmap *map = container_of(_map, struct mmap, core);
737b80132b1SJiri Olsa 	struct mmap_params *mp = container_of(_mp, struct mmap_params, core);
738b80132b1SJiri Olsa 
739b80132b1SJiri Olsa 	return mmap__mmap(map, mp, output, cpu);
740b80132b1SJiri Olsa }
741b80132b1SJiri Olsa 
742f5e7150cSArnaldo Carvalho de Melo unsigned long perf_event_mlock_kb_in_pages(void)
743994a1f78SJiri Olsa {
744f5e7150cSArnaldo Carvalho de Melo 	unsigned long pages;
7458185e881SArnaldo Carvalho de Melo 	int max;
7468185e881SArnaldo Carvalho de Melo 
7478185e881SArnaldo Carvalho de Melo 	if (sysctl__read_int("kernel/perf_event_mlock_kb", &max) < 0) {
7488185e881SArnaldo Carvalho de Melo 		/*
7498185e881SArnaldo Carvalho de Melo 		 * Pick a once upon a time good value, i.e. things look
7508185e881SArnaldo Carvalho de Melo 		 * strange since we can't read a sysctl value, but lets not
7518185e881SArnaldo Carvalho de Melo 		 * die yet...
7528185e881SArnaldo Carvalho de Melo 		 */
7538185e881SArnaldo Carvalho de Melo 		max = 512;
7548185e881SArnaldo Carvalho de Melo 	} else {
7558185e881SArnaldo Carvalho de Melo 		max -= (page_size / 1024);
7568185e881SArnaldo Carvalho de Melo 	}
7578185e881SArnaldo Carvalho de Melo 
7588185e881SArnaldo Carvalho de Melo 	pages = (max * 1024) / page_size;
7591be300f4SArnaldo Carvalho de Melo 	if (!is_power_of_2(pages))
7601be300f4SArnaldo Carvalho de Melo 		pages = rounddown_pow_of_two(pages);
761f5e7150cSArnaldo Carvalho de Melo 
762f5e7150cSArnaldo Carvalho de Melo 	return pages;
763f5e7150cSArnaldo Carvalho de Melo }
764f5e7150cSArnaldo Carvalho de Melo 
7659521b5f2SJiri Olsa size_t evlist__mmap_size(unsigned long pages)
766f5e7150cSArnaldo Carvalho de Melo {
767f5e7150cSArnaldo Carvalho de Melo 	if (pages == UINT_MAX)
768f5e7150cSArnaldo Carvalho de Melo 		pages = perf_event_mlock_kb_in_pages();
769f5e7150cSArnaldo Carvalho de Melo 	else if (!is_power_of_2(pages))
770994a1f78SJiri Olsa 		return 0;
771994a1f78SJiri Olsa 
772994a1f78SJiri Olsa 	return (pages + 1) * page_size;
773994a1f78SJiri Olsa }
774994a1f78SJiri Olsa 
77533c2dcfdSDavid Ahern static long parse_pages_arg(const char *str, unsigned long min,
77633c2dcfdSDavid Ahern 			    unsigned long max)
777994a1f78SJiri Olsa {
7782fbe4abeSAdrian Hunter 	unsigned long pages, val;
77927050f53SJiri Olsa 	static struct parse_tag tags[] = {
78027050f53SJiri Olsa 		{ .tag  = 'B', .mult = 1       },
78127050f53SJiri Olsa 		{ .tag  = 'K', .mult = 1 << 10 },
78227050f53SJiri Olsa 		{ .tag  = 'M', .mult = 1 << 20 },
78327050f53SJiri Olsa 		{ .tag  = 'G', .mult = 1 << 30 },
78427050f53SJiri Olsa 		{ .tag  = 0 },
78527050f53SJiri Olsa 	};
786994a1f78SJiri Olsa 
7878973504bSDavid Ahern 	if (str == NULL)
78833c2dcfdSDavid Ahern 		return -EINVAL;
7898973504bSDavid Ahern 
79027050f53SJiri Olsa 	val = parse_tag_value(str, tags);
7912fbe4abeSAdrian Hunter 	if (val != (unsigned long) -1) {
79227050f53SJiri Olsa 		/* we got file size value */
79327050f53SJiri Olsa 		pages = PERF_ALIGN(val, page_size) / page_size;
79427050f53SJiri Olsa 	} else {
79527050f53SJiri Olsa 		/* we got pages count value */
79627050f53SJiri Olsa 		char *eptr;
797994a1f78SJiri Olsa 		pages = strtoul(str, &eptr, 10);
79833c2dcfdSDavid Ahern 		if (*eptr != '\0')
79933c2dcfdSDavid Ahern 			return -EINVAL;
80027050f53SJiri Olsa 	}
801994a1f78SJiri Olsa 
8022bcab6c1SAdrian Hunter 	if (pages == 0 && min == 0) {
80333c2dcfdSDavid Ahern 		/* leave number of pages at 0 */
8041dbfa938SAdrian Hunter 	} else if (!is_power_of_2(pages)) {
8059808143bSJiri Olsa 		char buf[100];
8069808143bSJiri Olsa 
80733c2dcfdSDavid Ahern 		/* round pages up to next power of 2 */
80891529834SArnaldo Carvalho de Melo 		pages = roundup_pow_of_two(pages);
8091dbfa938SAdrian Hunter 		if (!pages)
8101dbfa938SAdrian Hunter 			return -EINVAL;
8119808143bSJiri Olsa 
8129808143bSJiri Olsa 		unit_number__scnprintf(buf, sizeof(buf), pages * page_size);
8139808143bSJiri Olsa 		pr_info("rounding mmap pages size to %s (%lu pages)\n",
8149808143bSJiri Olsa 			buf, pages);
8152fbe4abeSAdrian Hunter 	}
8162fbe4abeSAdrian Hunter 
81733c2dcfdSDavid Ahern 	if (pages > max)
81833c2dcfdSDavid Ahern 		return -EINVAL;
81933c2dcfdSDavid Ahern 
82033c2dcfdSDavid Ahern 	return pages;
82133c2dcfdSDavid Ahern }
82233c2dcfdSDavid Ahern 
823e9db1310SAdrian Hunter int __perf_evlist__parse_mmap_pages(unsigned int *mmap_pages, const char *str)
82433c2dcfdSDavid Ahern {
82533c2dcfdSDavid Ahern 	unsigned long max = UINT_MAX;
82633c2dcfdSDavid Ahern 	long pages;
82733c2dcfdSDavid Ahern 
828f5ae9c42SAdrian Hunter 	if (max > SIZE_MAX / page_size)
82933c2dcfdSDavid Ahern 		max = SIZE_MAX / page_size;
83033c2dcfdSDavid Ahern 
83133c2dcfdSDavid Ahern 	pages = parse_pages_arg(str, 1, max);
83233c2dcfdSDavid Ahern 	if (pages < 0) {
83333c2dcfdSDavid Ahern 		pr_err("Invalid argument for --mmap_pages/-m\n");
834994a1f78SJiri Olsa 		return -1;
835994a1f78SJiri Olsa 	}
836994a1f78SJiri Olsa 
837994a1f78SJiri Olsa 	*mmap_pages = pages;
838994a1f78SJiri Olsa 	return 0;
839994a1f78SJiri Olsa }
840994a1f78SJiri Olsa 
841e9db1310SAdrian Hunter int perf_evlist__parse_mmap_pages(const struct option *opt, const char *str,
842e9db1310SAdrian Hunter 				  int unset __maybe_unused)
843e9db1310SAdrian Hunter {
844e9db1310SAdrian Hunter 	return __perf_evlist__parse_mmap_pages(opt->value, str);
845e9db1310SAdrian Hunter }
846e9db1310SAdrian Hunter 
847c83fa7f2SAdrian Hunter /**
8489521b5f2SJiri Olsa  * evlist__mmap_ex - Create mmaps to receive events.
849c83fa7f2SAdrian Hunter  * @evlist: list of events
850c83fa7f2SAdrian Hunter  * @pages: map length in pages
851c83fa7f2SAdrian Hunter  * @overwrite: overwrite older events?
852718c602dSAdrian Hunter  * @auxtrace_pages - auxtrace map length in pages
853718c602dSAdrian Hunter  * @auxtrace_overwrite - overwrite older auxtrace data?
854f8a95309SArnaldo Carvalho de Melo  *
855c83fa7f2SAdrian Hunter  * If @overwrite is %false the user needs to signal event consumption using
8569521b5f2SJiri Olsa  * perf_mmap__write_tail().  Using evlist__mmap_read() does this
857c83fa7f2SAdrian Hunter  * automatically.
858f8a95309SArnaldo Carvalho de Melo  *
859718c602dSAdrian Hunter  * Similarly, if @auxtrace_overwrite is %false the user needs to signal data
860718c602dSAdrian Hunter  * consumption using auxtrace_mmap__write_tail().
861718c602dSAdrian Hunter  *
862c83fa7f2SAdrian Hunter  * Return: %0 on success, negative error code otherwise.
863f8a95309SArnaldo Carvalho de Melo  */
8649521b5f2SJiri Olsa int evlist__mmap_ex(struct evlist *evlist, unsigned int pages,
8657a276ff6SWang Nan 			 unsigned int auxtrace_pages,
86651255a8aSAlexey Budankov 			 bool auxtrace_overwrite, int nr_cblocks, int affinity, int flush,
86751255a8aSAlexey Budankov 			 int comp_level)
868f8a95309SArnaldo Carvalho de Melo {
86971f566a3SWang Nan 	/*
87071f566a3SWang Nan 	 * Delay setting mp.prot: set it before calling perf_mmap__mmap.
87171f566a3SWang Nan 	 * Its value is decided by evsel's write_backward.
87271f566a3SWang Nan 	 * So &mp should not be passed through const pointer.
87371f566a3SWang Nan 	 */
874e440979fSJiri Olsa 	struct mmap_params mp = {
875e440979fSJiri Olsa 		.nr_cblocks	= nr_cblocks,
876e440979fSJiri Olsa 		.affinity	= affinity,
877e440979fSJiri Olsa 		.flush		= flush,
878e440979fSJiri Olsa 		.comp_level	= comp_level
879e440979fSJiri Olsa 	};
880923d0f18SJiri Olsa 	struct perf_evlist_mmap_ops ops = {
8819abd2ab2SJiri Olsa 		.idx  = perf_evlist__mmap_cb_idx,
882bb1b1885SJiri Olsa 		.get  = perf_evlist__mmap_cb_get,
883b80132b1SJiri Olsa 		.mmap = perf_evlist__mmap_cb_mmap,
8849abd2ab2SJiri Olsa 	};
88550a682ceSArnaldo Carvalho de Melo 
886f6fa4375SJiri Olsa 	evlist->core.mmap_len = evlist__mmap_size(pages);
887f6fa4375SJiri Olsa 	pr_debug("mmap size %zuB\n", evlist->core.mmap_len);
888f8a95309SArnaldo Carvalho de Melo 
889f6fa4375SJiri Olsa 	auxtrace_mmap_params__init(&mp.auxtrace_mp, evlist->core.mmap_len,
890718c602dSAdrian Hunter 				   auxtrace_pages, auxtrace_overwrite);
891718c602dSAdrian Hunter 
892923d0f18SJiri Olsa 	return perf_evlist__mmap_ops(&evlist->core, &ops, &mp.core);
893f8a95309SArnaldo Carvalho de Melo }
8947e2ed097SArnaldo Carvalho de Melo 
8959521b5f2SJiri Olsa int evlist__mmap(struct evlist *evlist, unsigned int pages)
896718c602dSAdrian Hunter {
8979521b5f2SJiri Olsa 	return evlist__mmap_ex(evlist, pages, 0, false, 0, PERF_AFFINITY_SYS, 1, 0);
898718c602dSAdrian Hunter }
899718c602dSAdrian Hunter 
90063503dbaSJiri Olsa int perf_evlist__create_maps(struct evlist *evlist, struct target *target)
9017e2ed097SArnaldo Carvalho de Melo {
902147c508fSJin Yao 	bool all_threads = (target->per_thread && target->system_wide);
903f854839bSJiri Olsa 	struct perf_cpu_map *cpus;
9049749b90eSJiri Olsa 	struct perf_thread_map *threads;
9057e2ed097SArnaldo Carvalho de Melo 
906147c508fSJin Yao 	/*
907147c508fSJin Yao 	 * If specify '-a' and '--per-thread' to perf record, perf record
908147c508fSJin Yao 	 * will override '--per-thread'. target->per_thread = false and
909147c508fSJin Yao 	 * target->system_wide = true.
910147c508fSJin Yao 	 *
911147c508fSJin Yao 	 * If specify '--per-thread' only to perf record,
912147c508fSJin Yao 	 * target->per_thread = true and target->system_wide = false.
913147c508fSJin Yao 	 *
914147c508fSJin Yao 	 * So target->per_thread && target->system_wide is false.
915147c508fSJin Yao 	 * For perf record, thread_map__new_str doesn't call
916147c508fSJin Yao 	 * thread_map__new_all_cpus. That will keep perf record's
917147c508fSJin Yao 	 * current behavior.
918147c508fSJin Yao 	 *
919147c508fSJin Yao 	 * For perf stat, it allows the case that target->per_thread and
920147c508fSJin Yao 	 * target->system_wide are all true. It means to collect system-wide
921147c508fSJin Yao 	 * per-thread data. thread_map__new_str will call
922147c508fSJin Yao 	 * thread_map__new_all_cpus to enumerate all threads.
923147c508fSJin Yao 	 */
92473c0ca1eSJin Yao 	threads = thread_map__new_str(target->pid, target->tid, target->uid,
925147c508fSJin Yao 				      all_threads);
92674bfd2b2SAdrian Hunter 
92774bfd2b2SAdrian Hunter 	if (!threads)
9287e2ed097SArnaldo Carvalho de Melo 		return -1;
9297e2ed097SArnaldo Carvalho de Melo 
9309c105fbcSDongsheng Yang 	if (target__uses_dummy_map(target))
931397721e0SJiri Olsa 		cpus = perf_cpu_map__dummy_new();
932879d77d0SNamhyung Kim 	else
9339c3516d1SJiri Olsa 		cpus = perf_cpu_map__new(target->cpu_list);
9347e2ed097SArnaldo Carvalho de Melo 
93574bfd2b2SAdrian Hunter 	if (!cpus)
9367e2ed097SArnaldo Carvalho de Melo 		goto out_delete_threads;
9377e2ed097SArnaldo Carvalho de Melo 
938ec903f26SJiri Olsa 	evlist->core.has_user_cpus = !!target->cpu_list;
939ec9a77a7SAdrian Hunter 
940453fa030SJiri Olsa 	perf_evlist__set_maps(&evlist->core, cpus, threads);
941d5bc056eSAdrian Hunter 
942bfd1b83dSNamhyung Kim 	/* as evlist now has references, put count here */
943bfd1b83dSNamhyung Kim 	perf_cpu_map__put(cpus);
944bfd1b83dSNamhyung Kim 	perf_thread_map__put(threads);
945bfd1b83dSNamhyung Kim 
946d5bc056eSAdrian Hunter 	return 0;
9477e2ed097SArnaldo Carvalho de Melo 
9487e2ed097SArnaldo Carvalho de Melo out_delete_threads:
9497836e52eSJiri Olsa 	perf_thread_map__put(threads);
9507e2ed097SArnaldo Carvalho de Melo 	return -1;
9517e2ed097SArnaldo Carvalho de Melo }
9527e2ed097SArnaldo Carvalho de Melo 
95324bf91a7SArnaldo Carvalho de Melo int evlist__apply_filters(struct evlist *evlist, struct evsel **err_evsel)
9540a102479SFrederic Weisbecker {
95532dcd021SJiri Olsa 	struct evsel *evsel;
956745cefc5SArnaldo Carvalho de Melo 	int err = 0;
9570a102479SFrederic Weisbecker 
958e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, evsel) {
959745cefc5SArnaldo Carvalho de Melo 		if (evsel->filter == NULL)
9600a102479SFrederic Weisbecker 			continue;
961745cefc5SArnaldo Carvalho de Melo 
962d988d5eeSKan Liang 		/*
963d988d5eeSKan Liang 		 * filters only work for tracepoint event, which doesn't have cpu limit.
964d988d5eeSKan Liang 		 * So evlist and evsel should always be same.
965d988d5eeSKan Liang 		 */
966a00571fdSJiri Olsa 		err = perf_evsel__apply_filter(&evsel->core, evsel->filter);
96723d4aad4SArnaldo Carvalho de Melo 		if (err) {
96823d4aad4SArnaldo Carvalho de Melo 			*err_evsel = evsel;
969745cefc5SArnaldo Carvalho de Melo 			break;
9700a102479SFrederic Weisbecker 		}
97123d4aad4SArnaldo Carvalho de Melo 	}
9720a102479SFrederic Weisbecker 
973745cefc5SArnaldo Carvalho de Melo 	return err;
974745cefc5SArnaldo Carvalho de Melo }
975745cefc5SArnaldo Carvalho de Melo 
97624bf91a7SArnaldo Carvalho de Melo int evlist__set_tp_filter(struct evlist *evlist, const char *filter)
977745cefc5SArnaldo Carvalho de Melo {
97832dcd021SJiri Olsa 	struct evsel *evsel;
979745cefc5SArnaldo Carvalho de Melo 	int err = 0;
980745cefc5SArnaldo Carvalho de Melo 
98105cea449SArnaldo Carvalho de Melo 	if (filter == NULL)
98205cea449SArnaldo Carvalho de Melo 		return -1;
98305cea449SArnaldo Carvalho de Melo 
984e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, evsel) {
9851fc632ceSJiri Olsa 		if (evsel->core.attr.type != PERF_TYPE_TRACEPOINT)
986fdf14720SWang Nan 			continue;
987fdf14720SWang Nan 
988ad681adfSArnaldo Carvalho de Melo 		err = evsel__set_filter(evsel, filter);
989745cefc5SArnaldo Carvalho de Melo 		if (err)
990745cefc5SArnaldo Carvalho de Melo 			break;
991745cefc5SArnaldo Carvalho de Melo 	}
992745cefc5SArnaldo Carvalho de Melo 
993745cefc5SArnaldo Carvalho de Melo 	return err;
9940a102479SFrederic Weisbecker }
99574429964SFrederic Weisbecker 
99624bf91a7SArnaldo Carvalho de Melo int evlist__append_tp_filter(struct evlist *evlist, const char *filter)
99753c92f73SArnaldo Carvalho de Melo {
99853c92f73SArnaldo Carvalho de Melo 	struct evsel *evsel;
99953c92f73SArnaldo Carvalho de Melo 	int err = 0;
100053c92f73SArnaldo Carvalho de Melo 
100153c92f73SArnaldo Carvalho de Melo 	if (filter == NULL)
100253c92f73SArnaldo Carvalho de Melo 		return -1;
100353c92f73SArnaldo Carvalho de Melo 
100453c92f73SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, evsel) {
100553c92f73SArnaldo Carvalho de Melo 		if (evsel->core.attr.type != PERF_TYPE_TRACEPOINT)
100653c92f73SArnaldo Carvalho de Melo 			continue;
100753c92f73SArnaldo Carvalho de Melo 
1008ad681adfSArnaldo Carvalho de Melo 		err = evsel__append_tp_filter(evsel, filter);
100953c92f73SArnaldo Carvalho de Melo 		if (err)
101053c92f73SArnaldo Carvalho de Melo 			break;
101153c92f73SArnaldo Carvalho de Melo 	}
101253c92f73SArnaldo Carvalho de Melo 
101353c92f73SArnaldo Carvalho de Melo 	return err;
101453c92f73SArnaldo Carvalho de Melo }
101553c92f73SArnaldo Carvalho de Melo 
1016da949f50SArnaldo Carvalho de Melo char *asprintf__tp_filter_pids(size_t npids, pid_t *pids)
1017cfd70a26SArnaldo Carvalho de Melo {
1018cfd70a26SArnaldo Carvalho de Melo 	char *filter;
1019be199adaSArnaldo Carvalho de Melo 	size_t i;
1020cfd70a26SArnaldo Carvalho de Melo 
1021be199adaSArnaldo Carvalho de Melo 	for (i = 0; i < npids; ++i) {
1022be199adaSArnaldo Carvalho de Melo 		if (i == 0) {
1023be199adaSArnaldo Carvalho de Melo 			if (asprintf(&filter, "common_pid != %d", pids[i]) < 0)
102405cea449SArnaldo Carvalho de Melo 				return NULL;
1025be199adaSArnaldo Carvalho de Melo 		} else {
1026be199adaSArnaldo Carvalho de Melo 			char *tmp;
1027be199adaSArnaldo Carvalho de Melo 
1028be199adaSArnaldo Carvalho de Melo 			if (asprintf(&tmp, "%s && common_pid != %d", filter, pids[i]) < 0)
1029be199adaSArnaldo Carvalho de Melo 				goto out_free;
1030be199adaSArnaldo Carvalho de Melo 
1031be199adaSArnaldo Carvalho de Melo 			free(filter);
1032be199adaSArnaldo Carvalho de Melo 			filter = tmp;
1033be199adaSArnaldo Carvalho de Melo 		}
1034be199adaSArnaldo Carvalho de Melo 	}
1035cfd70a26SArnaldo Carvalho de Melo 
103605cea449SArnaldo Carvalho de Melo 	return filter;
1037be199adaSArnaldo Carvalho de Melo out_free:
1038cfd70a26SArnaldo Carvalho de Melo 	free(filter);
103905cea449SArnaldo Carvalho de Melo 	return NULL;
104005cea449SArnaldo Carvalho de Melo }
104105cea449SArnaldo Carvalho de Melo 
104224bf91a7SArnaldo Carvalho de Melo int evlist__set_tp_filter_pids(struct evlist *evlist, size_t npids, pid_t *pids)
104305cea449SArnaldo Carvalho de Melo {
104405cea449SArnaldo Carvalho de Melo 	char *filter = asprintf__tp_filter_pids(npids, pids);
104524bf91a7SArnaldo Carvalho de Melo 	int ret = evlist__set_tp_filter(evlist, filter);
104605cea449SArnaldo Carvalho de Melo 
104705cea449SArnaldo Carvalho de Melo 	free(filter);
1048cfd70a26SArnaldo Carvalho de Melo 	return ret;
1049cfd70a26SArnaldo Carvalho de Melo }
1050cfd70a26SArnaldo Carvalho de Melo 
105124bf91a7SArnaldo Carvalho de Melo int evlist__set_tp_filter_pid(struct evlist *evlist, pid_t pid)
1052be199adaSArnaldo Carvalho de Melo {
105324bf91a7SArnaldo Carvalho de Melo 	return evlist__set_tp_filter_pids(evlist, 1, &pid);
1054be199adaSArnaldo Carvalho de Melo }
1055be199adaSArnaldo Carvalho de Melo 
105624bf91a7SArnaldo Carvalho de Melo int evlist__append_tp_filter_pids(struct evlist *evlist, size_t npids, pid_t *pids)
10571827ab5bSArnaldo Carvalho de Melo {
10581827ab5bSArnaldo Carvalho de Melo 	char *filter = asprintf__tp_filter_pids(npids, pids);
105924bf91a7SArnaldo Carvalho de Melo 	int ret = evlist__append_tp_filter(evlist, filter);
10601827ab5bSArnaldo Carvalho de Melo 
10611827ab5bSArnaldo Carvalho de Melo 	free(filter);
10621827ab5bSArnaldo Carvalho de Melo 	return ret;
10631827ab5bSArnaldo Carvalho de Melo }
10641827ab5bSArnaldo Carvalho de Melo 
106524bf91a7SArnaldo Carvalho de Melo int evlist__append_tp_filter_pid(struct evlist *evlist, pid_t pid)
10661827ab5bSArnaldo Carvalho de Melo {
106724bf91a7SArnaldo Carvalho de Melo 	return evlist__append_tp_filter_pids(evlist, 1, &pid);
10681827ab5bSArnaldo Carvalho de Melo }
10691827ab5bSArnaldo Carvalho de Melo 
1070b3c2cc2bSArnaldo Carvalho de Melo bool evlist__valid_sample_type(struct evlist *evlist)
107174429964SFrederic Weisbecker {
107232dcd021SJiri Olsa 	struct evsel *pos;
1073c2a70653SArnaldo Carvalho de Melo 
10746484d2f9SJiri Olsa 	if (evlist->core.nr_entries == 1)
107575562573SAdrian Hunter 		return true;
107675562573SAdrian Hunter 
107775562573SAdrian Hunter 	if (evlist->id_pos < 0 || evlist->is_pos < 0)
107875562573SAdrian Hunter 		return false;
107975562573SAdrian Hunter 
1080e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, pos) {
108175562573SAdrian Hunter 		if (pos->id_pos != evlist->id_pos ||
108275562573SAdrian Hunter 		    pos->is_pos != evlist->is_pos)
1083c2a70653SArnaldo Carvalho de Melo 			return false;
108474429964SFrederic Weisbecker 	}
108574429964SFrederic Weisbecker 
1086c2a70653SArnaldo Carvalho de Melo 	return true;
1087c2a70653SArnaldo Carvalho de Melo }
1088c2a70653SArnaldo Carvalho de Melo 
1089b3c2cc2bSArnaldo Carvalho de Melo u64 __evlist__combined_sample_type(struct evlist *evlist)
1090c2a70653SArnaldo Carvalho de Melo {
109132dcd021SJiri Olsa 	struct evsel *evsel;
109275562573SAdrian Hunter 
109375562573SAdrian Hunter 	if (evlist->combined_sample_type)
109475562573SAdrian Hunter 		return evlist->combined_sample_type;
109575562573SAdrian Hunter 
1096e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, evsel)
10971fc632ceSJiri Olsa 		evlist->combined_sample_type |= evsel->core.attr.sample_type;
109875562573SAdrian Hunter 
109975562573SAdrian Hunter 	return evlist->combined_sample_type;
110075562573SAdrian Hunter }
110175562573SAdrian Hunter 
1102b3c2cc2bSArnaldo Carvalho de Melo u64 evlist__combined_sample_type(struct evlist *evlist)
110375562573SAdrian Hunter {
110475562573SAdrian Hunter 	evlist->combined_sample_type = 0;
1105b3c2cc2bSArnaldo Carvalho de Melo 	return __evlist__combined_sample_type(evlist);
1106c2a70653SArnaldo Carvalho de Melo }
1107c2a70653SArnaldo Carvalho de Melo 
110892c7d7cdSArnaldo Carvalho de Melo u64 evlist__combined_branch_type(struct evlist *evlist)
110998df858eSAndi Kleen {
111032dcd021SJiri Olsa 	struct evsel *evsel;
111198df858eSAndi Kleen 	u64 branch_type = 0;
111298df858eSAndi Kleen 
1113e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, evsel)
11141fc632ceSJiri Olsa 		branch_type |= evsel->core.attr.branch_sample_type;
111598df858eSAndi Kleen 	return branch_type;
111698df858eSAndi Kleen }
111798df858eSAndi Kleen 
111863503dbaSJiri Olsa bool perf_evlist__valid_read_format(struct evlist *evlist)
11199ede473cSJiri Olsa {
1120515dbe48SJiri Olsa 	struct evsel *first = evlist__first(evlist), *pos = first;
11211fc632ceSJiri Olsa 	u64 read_format = first->core.attr.read_format;
11221fc632ceSJiri Olsa 	u64 sample_type = first->core.attr.sample_type;
11239ede473cSJiri Olsa 
1124e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, pos) {
112594d3820fSAdrian Hunter 		if (read_format != pos->core.attr.read_format) {
112694d3820fSAdrian Hunter 			pr_debug("Read format differs %#" PRIx64 " vs %#" PRIx64 "\n",
112794d3820fSAdrian Hunter 				 read_format, (u64)pos->core.attr.read_format);
112894d3820fSAdrian Hunter 		}
11299ede473cSJiri Olsa 	}
11309ede473cSJiri Olsa 
11319ede473cSJiri Olsa 	/* PERF_SAMPLE_READ imples PERF_FORMAT_ID. */
11329ede473cSJiri Olsa 	if ((sample_type & PERF_SAMPLE_READ) &&
11339ede473cSJiri Olsa 	    !(read_format & PERF_FORMAT_ID)) {
11349ede473cSJiri Olsa 		return false;
11359ede473cSJiri Olsa 	}
11369ede473cSJiri Olsa 
11379ede473cSJiri Olsa 	return true;
11389ede473cSJiri Olsa }
11399ede473cSJiri Olsa 
114063503dbaSJiri Olsa u16 perf_evlist__id_hdr_size(struct evlist *evlist)
114181e36bffSArnaldo Carvalho de Melo {
1142515dbe48SJiri Olsa 	struct evsel *first = evlist__first(evlist);
114381e36bffSArnaldo Carvalho de Melo 	struct perf_sample *data;
114481e36bffSArnaldo Carvalho de Melo 	u64 sample_type;
114581e36bffSArnaldo Carvalho de Melo 	u16 size = 0;
114681e36bffSArnaldo Carvalho de Melo 
11471fc632ceSJiri Olsa 	if (!first->core.attr.sample_id_all)
114881e36bffSArnaldo Carvalho de Melo 		goto out;
114981e36bffSArnaldo Carvalho de Melo 
11501fc632ceSJiri Olsa 	sample_type = first->core.attr.sample_type;
115181e36bffSArnaldo Carvalho de Melo 
115281e36bffSArnaldo Carvalho de Melo 	if (sample_type & PERF_SAMPLE_TID)
115381e36bffSArnaldo Carvalho de Melo 		size += sizeof(data->tid) * 2;
115481e36bffSArnaldo Carvalho de Melo 
115581e36bffSArnaldo Carvalho de Melo        if (sample_type & PERF_SAMPLE_TIME)
115681e36bffSArnaldo Carvalho de Melo 		size += sizeof(data->time);
115781e36bffSArnaldo Carvalho de Melo 
115881e36bffSArnaldo Carvalho de Melo 	if (sample_type & PERF_SAMPLE_ID)
115981e36bffSArnaldo Carvalho de Melo 		size += sizeof(data->id);
116081e36bffSArnaldo Carvalho de Melo 
116181e36bffSArnaldo Carvalho de Melo 	if (sample_type & PERF_SAMPLE_STREAM_ID)
116281e36bffSArnaldo Carvalho de Melo 		size += sizeof(data->stream_id);
116381e36bffSArnaldo Carvalho de Melo 
116481e36bffSArnaldo Carvalho de Melo 	if (sample_type & PERF_SAMPLE_CPU)
116581e36bffSArnaldo Carvalho de Melo 		size += sizeof(data->cpu) * 2;
116675562573SAdrian Hunter 
116775562573SAdrian Hunter 	if (sample_type & PERF_SAMPLE_IDENTIFIER)
116875562573SAdrian Hunter 		size += sizeof(data->id);
116981e36bffSArnaldo Carvalho de Melo out:
117081e36bffSArnaldo Carvalho de Melo 	return size;
117181e36bffSArnaldo Carvalho de Melo }
117281e36bffSArnaldo Carvalho de Melo 
11738cedf3a5SArnaldo Carvalho de Melo bool evlist__valid_sample_id_all(struct evlist *evlist)
1174c2a70653SArnaldo Carvalho de Melo {
1175515dbe48SJiri Olsa 	struct evsel *first = evlist__first(evlist), *pos = first;
1176c2a70653SArnaldo Carvalho de Melo 
1177e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry_continue(evlist, pos) {
11781fc632ceSJiri Olsa 		if (first->core.attr.sample_id_all != pos->core.attr.sample_id_all)
1179c2a70653SArnaldo Carvalho de Melo 			return false;
1180c2a70653SArnaldo Carvalho de Melo 	}
1181c2a70653SArnaldo Carvalho de Melo 
1182c2a70653SArnaldo Carvalho de Melo 	return true;
118374429964SFrederic Weisbecker }
118474429964SFrederic Weisbecker 
11858cedf3a5SArnaldo Carvalho de Melo bool evlist__sample_id_all(struct evlist *evlist)
118674429964SFrederic Weisbecker {
1187515dbe48SJiri Olsa 	struct evsel *first = evlist__first(evlist);
11881fc632ceSJiri Olsa 	return first->core.attr.sample_id_all;
118974429964SFrederic Weisbecker }
119081cce8deSArnaldo Carvalho de Melo 
119163503dbaSJiri Olsa void perf_evlist__set_selected(struct evlist *evlist,
119232dcd021SJiri Olsa 			       struct evsel *evsel)
119381cce8deSArnaldo Carvalho de Melo {
119481cce8deSArnaldo Carvalho de Melo 	evlist->selected = evsel;
119581cce8deSArnaldo Carvalho de Melo }
1196727ab04eSArnaldo Carvalho de Melo 
1197750b4edeSJiri Olsa void evlist__close(struct evlist *evlist)
1198a74b4b66SNamhyung Kim {
119932dcd021SJiri Olsa 	struct evsel *evsel;
12007736627bSAndi Kleen 	struct affinity affinity;
12017736627bSAndi Kleen 	int cpu, i;
1202a74b4b66SNamhyung Kim 
12037736627bSAndi Kleen 	/*
12047736627bSAndi Kleen 	 * With perf record core.cpus is usually NULL.
12057736627bSAndi Kleen 	 * Use the old method to handle this for now.
12067736627bSAndi Kleen 	 */
12077736627bSAndi Kleen 	if (!evlist->core.cpus) {
1208475fb533SAndi Kleen 		evlist__for_each_entry_reverse(evlist, evsel)
120988761fa1SJiri Olsa 			evsel__close(evsel);
12107736627bSAndi Kleen 		return;
12117736627bSAndi Kleen 	}
12127736627bSAndi Kleen 
12137736627bSAndi Kleen 	if (affinity__setup(&affinity) < 0)
12147736627bSAndi Kleen 		return;
12157736627bSAndi Kleen 	evlist__for_each_cpu(evlist, i, cpu) {
12167736627bSAndi Kleen 		affinity__set(&affinity, cpu);
12177736627bSAndi Kleen 
12187736627bSAndi Kleen 		evlist__for_each_entry_reverse(evlist, evsel) {
12197736627bSAndi Kleen 			if (evsel__cpu_iter_skip(evsel, cpu))
12207736627bSAndi Kleen 			    continue;
12217736627bSAndi Kleen 			perf_evsel__close_cpu(&evsel->core, evsel->cpu_iter - 1);
12227736627bSAndi Kleen 		}
12237736627bSAndi Kleen 	}
12247736627bSAndi Kleen 	affinity__cleanup(&affinity);
12257736627bSAndi Kleen 	evlist__for_each_entry_reverse(evlist, evsel) {
12267736627bSAndi Kleen 		perf_evsel__free_fd(&evsel->core);
12277736627bSAndi Kleen 		perf_evsel__free_id(&evsel->core);
12287736627bSAndi Kleen 	}
1229a74b4b66SNamhyung Kim }
1230a74b4b66SNamhyung Kim 
123163503dbaSJiri Olsa static int perf_evlist__create_syswide_maps(struct evlist *evlist)
12324112eb18SArnaldo Carvalho de Melo {
1233f854839bSJiri Olsa 	struct perf_cpu_map *cpus;
12349749b90eSJiri Olsa 	struct perf_thread_map *threads;
12354112eb18SArnaldo Carvalho de Melo 	int err = -ENOMEM;
12364112eb18SArnaldo Carvalho de Melo 
12374112eb18SArnaldo Carvalho de Melo 	/*
12384112eb18SArnaldo Carvalho de Melo 	 * Try reading /sys/devices/system/cpu/online to get
12394112eb18SArnaldo Carvalho de Melo 	 * an all cpus map.
12404112eb18SArnaldo Carvalho de Melo 	 *
12414112eb18SArnaldo Carvalho de Melo 	 * FIXME: -ENOMEM is the best we can do here, the cpu_map
12424112eb18SArnaldo Carvalho de Melo 	 * code needs an overhaul to properly forward the
12434112eb18SArnaldo Carvalho de Melo 	 * error, and we may not want to do that fallback to a
12444112eb18SArnaldo Carvalho de Melo 	 * default cpu identity map :-\
12454112eb18SArnaldo Carvalho de Melo 	 */
12469c3516d1SJiri Olsa 	cpus = perf_cpu_map__new(NULL);
12478c0498b6SAdrian Hunter 	if (!cpus)
12484112eb18SArnaldo Carvalho de Melo 		goto out;
12494112eb18SArnaldo Carvalho de Melo 
12504b49cce2SJiri Olsa 	threads = perf_thread_map__new_dummy();
12518c0498b6SAdrian Hunter 	if (!threads)
12528c0498b6SAdrian Hunter 		goto out_put;
12534112eb18SArnaldo Carvalho de Melo 
1254453fa030SJiri Olsa 	perf_evlist__set_maps(&evlist->core, cpus, threads);
1255bfd1b83dSNamhyung Kim 
1256bfd1b83dSNamhyung Kim 	perf_thread_map__put(threads);
12578c0498b6SAdrian Hunter out_put:
125838f01d8dSJiri Olsa 	perf_cpu_map__put(cpus);
1259bfd1b83dSNamhyung Kim out:
1260bfd1b83dSNamhyung Kim 	return err;
12614112eb18SArnaldo Carvalho de Melo }
12624112eb18SArnaldo Carvalho de Melo 
1263474ddc4cSJiri Olsa int evlist__open(struct evlist *evlist)
1264727ab04eSArnaldo Carvalho de Melo {
126532dcd021SJiri Olsa 	struct evsel *evsel;
1266a74b4b66SNamhyung Kim 	int err;
1267727ab04eSArnaldo Carvalho de Melo 
12684112eb18SArnaldo Carvalho de Melo 	/*
12694112eb18SArnaldo Carvalho de Melo 	 * Default: one fd per CPU, all threads, aka systemwide
12704112eb18SArnaldo Carvalho de Melo 	 * as sys_perf_event_open(cpu = -1, thread = -1) is EINVAL
12714112eb18SArnaldo Carvalho de Melo 	 */
127203617c22SJiri Olsa 	if (evlist->core.threads == NULL && evlist->core.cpus == NULL) {
12734112eb18SArnaldo Carvalho de Melo 		err = perf_evlist__create_syswide_maps(evlist);
12744112eb18SArnaldo Carvalho de Melo 		if (err < 0)
12754112eb18SArnaldo Carvalho de Melo 			goto out_err;
12764112eb18SArnaldo Carvalho de Melo 	}
12774112eb18SArnaldo Carvalho de Melo 
1278733cd2feSAdrian Hunter 	perf_evlist__update_id_pos(evlist);
1279733cd2feSAdrian Hunter 
1280e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, evsel) {
1281af663bd0SJiri Olsa 		err = evsel__open(evsel, evsel->core.cpus, evsel->core.threads);
1282727ab04eSArnaldo Carvalho de Melo 		if (err < 0)
1283727ab04eSArnaldo Carvalho de Melo 			goto out_err;
1284727ab04eSArnaldo Carvalho de Melo 	}
1285727ab04eSArnaldo Carvalho de Melo 
1286727ab04eSArnaldo Carvalho de Melo 	return 0;
1287727ab04eSArnaldo Carvalho de Melo out_err:
1288750b4edeSJiri Olsa 	evlist__close(evlist);
128941c21a68SNamhyung Kim 	errno = -err;
1290727ab04eSArnaldo Carvalho de Melo 	return err;
1291727ab04eSArnaldo Carvalho de Melo }
129235b9d88eSArnaldo Carvalho de Melo 
12937b392ef0SArnaldo Carvalho de Melo int evlist__prepare_workload(struct evlist *evlist, struct target *target, const char *argv[],
12947b392ef0SArnaldo Carvalho de Melo 			     bool pipe_output, void (*exec_error)(int signo, siginfo_t *info, void *ucontext))
129535b9d88eSArnaldo Carvalho de Melo {
129635b9d88eSArnaldo Carvalho de Melo 	int child_ready_pipe[2], go_pipe[2];
129735b9d88eSArnaldo Carvalho de Melo 	char bf;
129835b9d88eSArnaldo Carvalho de Melo 
129935b9d88eSArnaldo Carvalho de Melo 	if (pipe(child_ready_pipe) < 0) {
130035b9d88eSArnaldo Carvalho de Melo 		perror("failed to create 'ready' pipe");
130135b9d88eSArnaldo Carvalho de Melo 		return -1;
130235b9d88eSArnaldo Carvalho de Melo 	}
130335b9d88eSArnaldo Carvalho de Melo 
130435b9d88eSArnaldo Carvalho de Melo 	if (pipe(go_pipe) < 0) {
130535b9d88eSArnaldo Carvalho de Melo 		perror("failed to create 'go' pipe");
130635b9d88eSArnaldo Carvalho de Melo 		goto out_close_ready_pipe;
130735b9d88eSArnaldo Carvalho de Melo 	}
130835b9d88eSArnaldo Carvalho de Melo 
130935b9d88eSArnaldo Carvalho de Melo 	evlist->workload.pid = fork();
131035b9d88eSArnaldo Carvalho de Melo 	if (evlist->workload.pid < 0) {
131135b9d88eSArnaldo Carvalho de Melo 		perror("failed to fork");
131235b9d88eSArnaldo Carvalho de Melo 		goto out_close_pipes;
131335b9d88eSArnaldo Carvalho de Melo 	}
131435b9d88eSArnaldo Carvalho de Melo 
131535b9d88eSArnaldo Carvalho de Melo 	if (!evlist->workload.pid) {
13165f1c4225SArnaldo Carvalho de Melo 		int ret;
13175f1c4225SArnaldo Carvalho de Melo 
1318119fa3c9SNamhyung Kim 		if (pipe_output)
131935b9d88eSArnaldo Carvalho de Melo 			dup2(2, 1);
132035b9d88eSArnaldo Carvalho de Melo 
13210817df08SDavid Ahern 		signal(SIGTERM, SIG_DFL);
13220817df08SDavid Ahern 
132335b9d88eSArnaldo Carvalho de Melo 		close(child_ready_pipe[0]);
132435b9d88eSArnaldo Carvalho de Melo 		close(go_pipe[1]);
132535b9d88eSArnaldo Carvalho de Melo 		fcntl(go_pipe[0], F_SETFD, FD_CLOEXEC);
132635b9d88eSArnaldo Carvalho de Melo 
132735b9d88eSArnaldo Carvalho de Melo 		/*
132835b9d88eSArnaldo Carvalho de Melo 		 * Tell the parent we're ready to go
132935b9d88eSArnaldo Carvalho de Melo 		 */
133035b9d88eSArnaldo Carvalho de Melo 		close(child_ready_pipe[1]);
133135b9d88eSArnaldo Carvalho de Melo 
133235b9d88eSArnaldo Carvalho de Melo 		/*
133335b9d88eSArnaldo Carvalho de Melo 		 * Wait until the parent tells us to go.
133435b9d88eSArnaldo Carvalho de Melo 		 */
13355f1c4225SArnaldo Carvalho de Melo 		ret = read(go_pipe[0], &bf, 1);
13365f1c4225SArnaldo Carvalho de Melo 		/*
13375f1c4225SArnaldo Carvalho de Melo 		 * The parent will ask for the execvp() to be performed by
13385f1c4225SArnaldo Carvalho de Melo 		 * writing exactly one byte, in workload.cork_fd, usually via
13397b392ef0SArnaldo Carvalho de Melo 		 * evlist__start_workload().
13405f1c4225SArnaldo Carvalho de Melo 		 *
134120f86fc1SArnaldo Carvalho de Melo 		 * For cancelling the workload without actually running it,
13425f1c4225SArnaldo Carvalho de Melo 		 * the parent will just close workload.cork_fd, without writing
13435f1c4225SArnaldo Carvalho de Melo 		 * anything, i.e. read will return zero and we just exit()
13445f1c4225SArnaldo Carvalho de Melo 		 * here.
13455f1c4225SArnaldo Carvalho de Melo 		 */
13465f1c4225SArnaldo Carvalho de Melo 		if (ret != 1) {
13475f1c4225SArnaldo Carvalho de Melo 			if (ret == -1)
134835b9d88eSArnaldo Carvalho de Melo 				perror("unable to read pipe");
13495f1c4225SArnaldo Carvalho de Melo 			exit(ret);
13505f1c4225SArnaldo Carvalho de Melo 		}
135135b9d88eSArnaldo Carvalho de Melo 
135235b9d88eSArnaldo Carvalho de Melo 		execvp(argv[0], (char **)argv);
135335b9d88eSArnaldo Carvalho de Melo 
1354735f7e0bSArnaldo Carvalho de Melo 		if (exec_error) {
1355f33cbe72SArnaldo Carvalho de Melo 			union sigval val;
1356f33cbe72SArnaldo Carvalho de Melo 
1357f33cbe72SArnaldo Carvalho de Melo 			val.sival_int = errno;
1358f33cbe72SArnaldo Carvalho de Melo 			if (sigqueue(getppid(), SIGUSR1, val))
135935b9d88eSArnaldo Carvalho de Melo 				perror(argv[0]);
1360f33cbe72SArnaldo Carvalho de Melo 		} else
1361f33cbe72SArnaldo Carvalho de Melo 			perror(argv[0]);
136235b9d88eSArnaldo Carvalho de Melo 		exit(-1);
136335b9d88eSArnaldo Carvalho de Melo 	}
136435b9d88eSArnaldo Carvalho de Melo 
1365735f7e0bSArnaldo Carvalho de Melo 	if (exec_error) {
1366735f7e0bSArnaldo Carvalho de Melo 		struct sigaction act = {
1367735f7e0bSArnaldo Carvalho de Melo 			.sa_flags     = SA_SIGINFO,
1368735f7e0bSArnaldo Carvalho de Melo 			.sa_sigaction = exec_error,
1369735f7e0bSArnaldo Carvalho de Melo 		};
1370735f7e0bSArnaldo Carvalho de Melo 		sigaction(SIGUSR1, &act, NULL);
1371735f7e0bSArnaldo Carvalho de Melo 	}
1372735f7e0bSArnaldo Carvalho de Melo 
13731aaf63b1SArnaldo Carvalho de Melo 	if (target__none(target)) {
137403617c22SJiri Olsa 		if (evlist->core.threads == NULL) {
13751aaf63b1SArnaldo Carvalho de Melo 			fprintf(stderr, "FATAL: evlist->threads need to be set at this point (%s:%d).\n",
13761aaf63b1SArnaldo Carvalho de Melo 				__func__, __LINE__);
13771aaf63b1SArnaldo Carvalho de Melo 			goto out_close_pipes;
13781aaf63b1SArnaldo Carvalho de Melo 		}
137903617c22SJiri Olsa 		perf_thread_map__set_pid(evlist->core.threads, 0, evlist->workload.pid);
13801aaf63b1SArnaldo Carvalho de Melo 	}
138135b9d88eSArnaldo Carvalho de Melo 
138235b9d88eSArnaldo Carvalho de Melo 	close(child_ready_pipe[1]);
138335b9d88eSArnaldo Carvalho de Melo 	close(go_pipe[0]);
138435b9d88eSArnaldo Carvalho de Melo 	/*
138535b9d88eSArnaldo Carvalho de Melo 	 * wait for child to settle
138635b9d88eSArnaldo Carvalho de Melo 	 */
138735b9d88eSArnaldo Carvalho de Melo 	if (read(child_ready_pipe[0], &bf, 1) == -1) {
138835b9d88eSArnaldo Carvalho de Melo 		perror("unable to read pipe");
138935b9d88eSArnaldo Carvalho de Melo 		goto out_close_pipes;
139035b9d88eSArnaldo Carvalho de Melo 	}
139135b9d88eSArnaldo Carvalho de Melo 
1392bcf3145fSNamhyung Kim 	fcntl(go_pipe[1], F_SETFD, FD_CLOEXEC);
139335b9d88eSArnaldo Carvalho de Melo 	evlist->workload.cork_fd = go_pipe[1];
139435b9d88eSArnaldo Carvalho de Melo 	close(child_ready_pipe[0]);
139535b9d88eSArnaldo Carvalho de Melo 	return 0;
139635b9d88eSArnaldo Carvalho de Melo 
139735b9d88eSArnaldo Carvalho de Melo out_close_pipes:
139835b9d88eSArnaldo Carvalho de Melo 	close(go_pipe[0]);
139935b9d88eSArnaldo Carvalho de Melo 	close(go_pipe[1]);
140035b9d88eSArnaldo Carvalho de Melo out_close_ready_pipe:
140135b9d88eSArnaldo Carvalho de Melo 	close(child_ready_pipe[0]);
140235b9d88eSArnaldo Carvalho de Melo 	close(child_ready_pipe[1]);
140335b9d88eSArnaldo Carvalho de Melo 	return -1;
140435b9d88eSArnaldo Carvalho de Melo }
140535b9d88eSArnaldo Carvalho de Melo 
14067b392ef0SArnaldo Carvalho de Melo int evlist__start_workload(struct evlist *evlist)
140735b9d88eSArnaldo Carvalho de Melo {
140835b9d88eSArnaldo Carvalho de Melo 	if (evlist->workload.cork_fd > 0) {
1409b3824404SDavid Ahern 		char bf = 0;
1410bcf3145fSNamhyung Kim 		int ret;
141135b9d88eSArnaldo Carvalho de Melo 		/*
141235b9d88eSArnaldo Carvalho de Melo 		 * Remove the cork, let it rip!
141335b9d88eSArnaldo Carvalho de Melo 		 */
1414bcf3145fSNamhyung Kim 		ret = write(evlist->workload.cork_fd, &bf, 1);
1415bcf3145fSNamhyung Kim 		if (ret < 0)
1416e978be9eSSoramichi Akiyama 			perror("unable to write to pipe");
1417bcf3145fSNamhyung Kim 
1418bcf3145fSNamhyung Kim 		close(evlist->workload.cork_fd);
1419bcf3145fSNamhyung Kim 		return ret;
142035b9d88eSArnaldo Carvalho de Melo 	}
142135b9d88eSArnaldo Carvalho de Melo 
142235b9d88eSArnaldo Carvalho de Melo 	return 0;
142335b9d88eSArnaldo Carvalho de Melo }
1424cb0b29e0SArnaldo Carvalho de Melo 
14252a6599cdSArnaldo Carvalho de Melo int evlist__parse_sample(struct evlist *evlist, union perf_event *event, struct perf_sample *sample)
1426cb0b29e0SArnaldo Carvalho de Melo {
1427*3ccf8a7bSArnaldo Carvalho de Melo 	struct evsel *evsel = evlist__event2evsel(evlist, event);
142875562573SAdrian Hunter 
142975562573SAdrian Hunter 	if (!evsel)
143075562573SAdrian Hunter 		return -EFAULT;
14316b6017a2SArnaldo Carvalho de Melo 	return evsel__parse_sample(evsel, event, sample);
1432cb0b29e0SArnaldo Carvalho de Melo }
143378f067b3SArnaldo Carvalho de Melo 
14342a6599cdSArnaldo Carvalho de Melo int evlist__parse_sample_timestamp(struct evlist *evlist, union perf_event *event, u64 *timestamp)
143501468120SJiri Olsa {
1436*3ccf8a7bSArnaldo Carvalho de Melo 	struct evsel *evsel = evlist__event2evsel(evlist, event);
143701468120SJiri Olsa 
143801468120SJiri Olsa 	if (!evsel)
143901468120SJiri Olsa 		return -EFAULT;
14406b6017a2SArnaldo Carvalho de Melo 	return evsel__parse_sample_timestamp(evsel, event, timestamp);
144101468120SJiri Olsa }
144201468120SJiri Olsa 
1443d1f249ecSArnaldo Carvalho de Melo int evlist__strerror_open(struct evlist *evlist, int err, char *buf, size_t size)
1444a8f23d8fSArnaldo Carvalho de Melo {
1445a8f23d8fSArnaldo Carvalho de Melo 	int printed, value;
1446c8b5f2c9SArnaldo Carvalho de Melo 	char sbuf[STRERR_BUFSIZE], *emsg = str_error_r(err, sbuf, sizeof(sbuf));
1447a8f23d8fSArnaldo Carvalho de Melo 
1448a8f23d8fSArnaldo Carvalho de Melo 	switch (err) {
1449a8f23d8fSArnaldo Carvalho de Melo 	case EACCES:
1450a8f23d8fSArnaldo Carvalho de Melo 	case EPERM:
1451a8f23d8fSArnaldo Carvalho de Melo 		printed = scnprintf(buf, size,
1452a8f23d8fSArnaldo Carvalho de Melo 				    "Error:\t%s.\n"
1453a8f23d8fSArnaldo Carvalho de Melo 				    "Hint:\tCheck /proc/sys/kernel/perf_event_paranoid setting.", emsg);
1454a8f23d8fSArnaldo Carvalho de Melo 
14551a47245dSAdrian Hunter 		value = perf_event_paranoid();
1456a8f23d8fSArnaldo Carvalho de Melo 
1457a8f23d8fSArnaldo Carvalho de Melo 		printed += scnprintf(buf + printed, size - printed, "\nHint:\t");
1458a8f23d8fSArnaldo Carvalho de Melo 
1459a8f23d8fSArnaldo Carvalho de Melo 		if (value >= 2) {
1460a8f23d8fSArnaldo Carvalho de Melo 			printed += scnprintf(buf + printed, size - printed,
1461a8f23d8fSArnaldo Carvalho de Melo 					     "For your workloads it needs to be <= 1\nHint:\t");
1462a8f23d8fSArnaldo Carvalho de Melo 		}
1463a8f23d8fSArnaldo Carvalho de Melo 		printed += scnprintf(buf + printed, size - printed,
14645229e366SArnaldo Carvalho de Melo 				     "For system wide tracing it needs to be set to -1.\n");
1465a8f23d8fSArnaldo Carvalho de Melo 
1466a8f23d8fSArnaldo Carvalho de Melo 		printed += scnprintf(buf + printed, size - printed,
14675229e366SArnaldo Carvalho de Melo 				    "Hint:\tTry: 'sudo sh -c \"echo -1 > /proc/sys/kernel/perf_event_paranoid\"'\n"
14685229e366SArnaldo Carvalho de Melo 				    "Hint:\tThe current value is %d.", value);
1469a8f23d8fSArnaldo Carvalho de Melo 		break;
1470d9aade7fSArnaldo Carvalho de Melo 	case EINVAL: {
1471515dbe48SJiri Olsa 		struct evsel *first = evlist__first(evlist);
1472d9aade7fSArnaldo Carvalho de Melo 		int max_freq;
1473d9aade7fSArnaldo Carvalho de Melo 
1474d9aade7fSArnaldo Carvalho de Melo 		if (sysctl__read_int("kernel/perf_event_max_sample_rate", &max_freq) < 0)
1475d9aade7fSArnaldo Carvalho de Melo 			goto out_default;
1476d9aade7fSArnaldo Carvalho de Melo 
14771fc632ceSJiri Olsa 		if (first->core.attr.sample_freq < (u64)max_freq)
1478d9aade7fSArnaldo Carvalho de Melo 			goto out_default;
1479d9aade7fSArnaldo Carvalho de Melo 
1480d9aade7fSArnaldo Carvalho de Melo 		printed = scnprintf(buf, size,
1481d9aade7fSArnaldo Carvalho de Melo 				    "Error:\t%s.\n"
1482d9aade7fSArnaldo Carvalho de Melo 				    "Hint:\tCheck /proc/sys/kernel/perf_event_max_sample_rate.\n"
1483d9aade7fSArnaldo Carvalho de Melo 				    "Hint:\tThe current value is %d and %" PRIu64 " is being requested.",
14841fc632ceSJiri Olsa 				    emsg, max_freq, first->core.attr.sample_freq);
1485d9aade7fSArnaldo Carvalho de Melo 		break;
1486d9aade7fSArnaldo Carvalho de Melo 	}
1487a8f23d8fSArnaldo Carvalho de Melo 	default:
1488d9aade7fSArnaldo Carvalho de Melo out_default:
1489a8f23d8fSArnaldo Carvalho de Melo 		scnprintf(buf, size, "%s", emsg);
1490a8f23d8fSArnaldo Carvalho de Melo 		break;
1491a8f23d8fSArnaldo Carvalho de Melo 	}
1492a8f23d8fSArnaldo Carvalho de Melo 
1493a8f23d8fSArnaldo Carvalho de Melo 	return 0;
1494a8f23d8fSArnaldo Carvalho de Melo }
1495a025e4f0SAdrian Hunter 
1496d1f249ecSArnaldo Carvalho de Melo int evlist__strerror_mmap(struct evlist *evlist, int err, char *buf, size_t size)
1497956fa571SArnaldo Carvalho de Melo {
1498c8b5f2c9SArnaldo Carvalho de Melo 	char sbuf[STRERR_BUFSIZE], *emsg = str_error_r(err, sbuf, sizeof(sbuf));
1499f6fa4375SJiri Olsa 	int pages_attempted = evlist->core.mmap_len / 1024, pages_max_per_user, printed = 0;
1500956fa571SArnaldo Carvalho de Melo 
1501956fa571SArnaldo Carvalho de Melo 	switch (err) {
1502956fa571SArnaldo Carvalho de Melo 	case EPERM:
1503e5d4a290SArnaldo Carvalho de Melo 		sysctl__read_int("kernel/perf_event_mlock_kb", &pages_max_per_user);
1504e965bea1SArnaldo Carvalho de Melo 		printed += scnprintf(buf + printed, size - printed,
1505e965bea1SArnaldo Carvalho de Melo 				     "Error:\t%s.\n"
1506956fa571SArnaldo Carvalho de Melo 				     "Hint:\tCheck /proc/sys/kernel/perf_event_mlock_kb (%d kB) setting.\n"
1507e965bea1SArnaldo Carvalho de Melo 				     "Hint:\tTried using %zd kB.\n",
1508e5d4a290SArnaldo Carvalho de Melo 				     emsg, pages_max_per_user, pages_attempted);
1509e965bea1SArnaldo Carvalho de Melo 
1510e965bea1SArnaldo Carvalho de Melo 		if (pages_attempted >= pages_max_per_user) {
1511e965bea1SArnaldo Carvalho de Melo 			printed += scnprintf(buf + printed, size - printed,
1512e965bea1SArnaldo Carvalho de Melo 					     "Hint:\tTry 'sudo sh -c \"echo %d > /proc/sys/kernel/perf_event_mlock_kb\"', or\n",
1513e965bea1SArnaldo Carvalho de Melo 					     pages_max_per_user + pages_attempted);
1514e965bea1SArnaldo Carvalho de Melo 		}
1515e965bea1SArnaldo Carvalho de Melo 
1516e965bea1SArnaldo Carvalho de Melo 		printed += scnprintf(buf + printed, size - printed,
1517e965bea1SArnaldo Carvalho de Melo 				     "Hint:\tTry using a smaller -m/--mmap-pages value.");
1518956fa571SArnaldo Carvalho de Melo 		break;
1519956fa571SArnaldo Carvalho de Melo 	default:
1520956fa571SArnaldo Carvalho de Melo 		scnprintf(buf, size, "%s", emsg);
1521956fa571SArnaldo Carvalho de Melo 		break;
1522956fa571SArnaldo Carvalho de Melo 	}
1523956fa571SArnaldo Carvalho de Melo 
1524956fa571SArnaldo Carvalho de Melo 	return 0;
1525956fa571SArnaldo Carvalho de Melo }
1526956fa571SArnaldo Carvalho de Melo 
152763503dbaSJiri Olsa void perf_evlist__to_front(struct evlist *evlist,
152832dcd021SJiri Olsa 			   struct evsel *move_evsel)
1529a025e4f0SAdrian Hunter {
153032dcd021SJiri Olsa 	struct evsel *evsel, *n;
1531a025e4f0SAdrian Hunter 	LIST_HEAD(move);
1532a025e4f0SAdrian Hunter 
1533515dbe48SJiri Olsa 	if (move_evsel == evlist__first(evlist))
1534a025e4f0SAdrian Hunter 		return;
1535a025e4f0SAdrian Hunter 
1536e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry_safe(evlist, n, evsel) {
1537a025e4f0SAdrian Hunter 		if (evsel->leader == move_evsel->leader)
1538b27c4eceSJiri Olsa 			list_move_tail(&evsel->core.node, &move);
1539a025e4f0SAdrian Hunter 	}
1540a025e4f0SAdrian Hunter 
1541ce9036a6SJiri Olsa 	list_splice(&move, &evlist->core.entries);
1542a025e4f0SAdrian Hunter }
154360b0896cSAdrian Hunter 
1544442ad225SAdrian Hunter struct evsel *perf_evlist__get_tracking_event(struct evlist *evlist)
1545442ad225SAdrian Hunter {
1546442ad225SAdrian Hunter 	struct evsel *evsel;
1547442ad225SAdrian Hunter 
1548442ad225SAdrian Hunter 	evlist__for_each_entry(evlist, evsel) {
1549442ad225SAdrian Hunter 		if (evsel->tracking)
1550442ad225SAdrian Hunter 			return evsel;
1551442ad225SAdrian Hunter 	}
1552442ad225SAdrian Hunter 
1553442ad225SAdrian Hunter 	return evlist__first(evlist);
1554442ad225SAdrian Hunter }
1555442ad225SAdrian Hunter 
155663503dbaSJiri Olsa void perf_evlist__set_tracking_event(struct evlist *evlist,
155732dcd021SJiri Olsa 				     struct evsel *tracking_evsel)
155860b0896cSAdrian Hunter {
155932dcd021SJiri Olsa 	struct evsel *evsel;
156060b0896cSAdrian Hunter 
156160b0896cSAdrian Hunter 	if (tracking_evsel->tracking)
156260b0896cSAdrian Hunter 		return;
156360b0896cSAdrian Hunter 
1564e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, evsel) {
156560b0896cSAdrian Hunter 		if (evsel != tracking_evsel)
156660b0896cSAdrian Hunter 			evsel->tracking = false;
156760b0896cSAdrian Hunter 	}
156860b0896cSAdrian Hunter 
156960b0896cSAdrian Hunter 	tracking_evsel->tracking = true;
157060b0896cSAdrian Hunter }
15717630b3e2SWang Nan 
1572b02736f7SArnaldo Carvalho de Melo struct evsel *evlist__find_evsel_by_str(struct evlist *evlist, const char *str)
15737630b3e2SWang Nan {
157432dcd021SJiri Olsa 	struct evsel *evsel;
15757630b3e2SWang Nan 
1576e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, evsel) {
15777630b3e2SWang Nan 		if (!evsel->name)
15787630b3e2SWang Nan 			continue;
15797630b3e2SWang Nan 		if (strcmp(str, evsel->name) == 0)
15807630b3e2SWang Nan 			return evsel;
15817630b3e2SWang Nan 	}
15827630b3e2SWang Nan 
15837630b3e2SWang Nan 	return NULL;
15847630b3e2SWang Nan }
158554cc54deSWang Nan 
1586ade9d208SArnaldo Carvalho de Melo void evlist__toggle_bkw_mmap(struct evlist *evlist, enum bkw_mmap_state state)
158754cc54deSWang Nan {
158854cc54deSWang Nan 	enum bkw_mmap_state old_state = evlist->bkw_mmap_state;
158954cc54deSWang Nan 	enum action {
159054cc54deSWang Nan 		NONE,
159154cc54deSWang Nan 		PAUSE,
159254cc54deSWang Nan 		RESUME,
159354cc54deSWang Nan 	} action = NONE;
159454cc54deSWang Nan 
15950b72d69aSWang Nan 	if (!evlist->overwrite_mmap)
159654cc54deSWang Nan 		return;
159754cc54deSWang Nan 
159854cc54deSWang Nan 	switch (old_state) {
159954cc54deSWang Nan 	case BKW_MMAP_NOTREADY: {
160054cc54deSWang Nan 		if (state != BKW_MMAP_RUNNING)
1601dd8bd53aSLuis de Bethencourt 			goto state_err;
160254cc54deSWang Nan 		break;
160354cc54deSWang Nan 	}
160454cc54deSWang Nan 	case BKW_MMAP_RUNNING: {
160554cc54deSWang Nan 		if (state != BKW_MMAP_DATA_PENDING)
160654cc54deSWang Nan 			goto state_err;
160754cc54deSWang Nan 		action = PAUSE;
160854cc54deSWang Nan 		break;
160954cc54deSWang Nan 	}
161054cc54deSWang Nan 	case BKW_MMAP_DATA_PENDING: {
161154cc54deSWang Nan 		if (state != BKW_MMAP_EMPTY)
161254cc54deSWang Nan 			goto state_err;
161354cc54deSWang Nan 		break;
161454cc54deSWang Nan 	}
161554cc54deSWang Nan 	case BKW_MMAP_EMPTY: {
161654cc54deSWang Nan 		if (state != BKW_MMAP_RUNNING)
161754cc54deSWang Nan 			goto state_err;
161854cc54deSWang Nan 		action = RESUME;
161954cc54deSWang Nan 		break;
162054cc54deSWang Nan 	}
162154cc54deSWang Nan 	default:
162254cc54deSWang Nan 		WARN_ONCE(1, "Shouldn't get there\n");
162354cc54deSWang Nan 	}
162454cc54deSWang Nan 
162554cc54deSWang Nan 	evlist->bkw_mmap_state = state;
162654cc54deSWang Nan 
162754cc54deSWang Nan 	switch (action) {
162854cc54deSWang Nan 	case PAUSE:
162954cc54deSWang Nan 		perf_evlist__pause(evlist);
163054cc54deSWang Nan 		break;
163154cc54deSWang Nan 	case RESUME:
163254cc54deSWang Nan 		perf_evlist__resume(evlist);
163354cc54deSWang Nan 		break;
163454cc54deSWang Nan 	case NONE:
163554cc54deSWang Nan 	default:
163654cc54deSWang Nan 		break;
163754cc54deSWang Nan 	}
163854cc54deSWang Nan 
163954cc54deSWang Nan state_err:
164054cc54deSWang Nan 	return;
164154cc54deSWang Nan }
164207d6f446SArnaldo Carvalho de Melo 
164363503dbaSJiri Olsa bool perf_evlist__exclude_kernel(struct evlist *evlist)
164407d6f446SArnaldo Carvalho de Melo {
164532dcd021SJiri Olsa 	struct evsel *evsel;
164607d6f446SArnaldo Carvalho de Melo 
164707d6f446SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, evsel) {
16481fc632ceSJiri Olsa 		if (!evsel->core.attr.exclude_kernel)
164907d6f446SArnaldo Carvalho de Melo 			return false;
165007d6f446SArnaldo Carvalho de Melo 	}
165107d6f446SArnaldo Carvalho de Melo 
165207d6f446SArnaldo Carvalho de Melo 	return true;
165307d6f446SArnaldo Carvalho de Melo }
1654e2bdbe80SJin Yao 
1655e2bdbe80SJin Yao /*
1656e2bdbe80SJin Yao  * Events in data file are not collect in groups, but we still want
1657e2bdbe80SJin Yao  * the group display. Set the artificial group and set the leader's
1658e2bdbe80SJin Yao  * forced_leader flag to notify the display code.
1659e2bdbe80SJin Yao  */
166063503dbaSJiri Olsa void perf_evlist__force_leader(struct evlist *evlist)
1661e2bdbe80SJin Yao {
1662e2bdbe80SJin Yao 	if (!evlist->nr_groups) {
1663515dbe48SJiri Olsa 		struct evsel *leader = evlist__first(evlist);
1664e2bdbe80SJin Yao 
1665a622eafaSArnaldo Carvalho de Melo 		evlist__set_leader(evlist);
1666e2bdbe80SJin Yao 		leader->forced_leader = true;
1667e2bdbe80SJin Yao 	}
1668e2bdbe80SJin Yao }
1669c3537fc2SAndi Kleen 
167063503dbaSJiri Olsa struct evsel *perf_evlist__reset_weak_group(struct evlist *evsel_list,
16714804e011SAndi Kleen 						 struct evsel *evsel,
16724804e011SAndi Kleen 						bool close)
1673c3537fc2SAndi Kleen {
167432dcd021SJiri Olsa 	struct evsel *c2, *leader;
1675c3537fc2SAndi Kleen 	bool is_open = true;
1676c3537fc2SAndi Kleen 
1677c3537fc2SAndi Kleen 	leader = evsel->leader;
1678c3537fc2SAndi Kleen 	pr_debug("Weak group for %s/%d failed\n",
16795643b1a5SJiri Olsa 			leader->name, leader->core.nr_members);
1680c3537fc2SAndi Kleen 
1681c3537fc2SAndi Kleen 	/*
1682c3537fc2SAndi Kleen 	 * for_each_group_member doesn't work here because it doesn't
1683c3537fc2SAndi Kleen 	 * include the first entry.
1684c3537fc2SAndi Kleen 	 */
1685c3537fc2SAndi Kleen 	evlist__for_each_entry(evsel_list, c2) {
1686c3537fc2SAndi Kleen 		if (c2 == evsel)
1687c3537fc2SAndi Kleen 			is_open = false;
1688c3537fc2SAndi Kleen 		if (c2->leader == leader) {
16894804e011SAndi Kleen 			if (is_open && close)
16905a40e199SAndi Kleen 				perf_evsel__close(&c2->core);
1691c3537fc2SAndi Kleen 			c2->leader = c2;
16925643b1a5SJiri Olsa 			c2->core.nr_members = 0;
16934804e011SAndi Kleen 			/*
16944804e011SAndi Kleen 			 * Set this for all former members of the group
16954804e011SAndi Kleen 			 * to indicate they get reopened.
16964804e011SAndi Kleen 			 */
16974804e011SAndi Kleen 			c2->reset_group = true;
1698c3537fc2SAndi Kleen 		}
1699c3537fc2SAndi Kleen 	}
1700c3537fc2SAndi Kleen 	return leader;
1701c3537fc2SAndi Kleen }
1702ec886bf5SAlexey Budankov 
1703a8fcbd26SAdrian Hunter static int evlist__parse_control_fifo(const char *str, int *ctl_fd, int *ctl_fd_ack, bool *ctl_fd_close)
1704a8fcbd26SAdrian Hunter {
1705a8fcbd26SAdrian Hunter 	char *s, *p;
1706a8fcbd26SAdrian Hunter 	int ret = 0, fd;
1707a8fcbd26SAdrian Hunter 
1708a8fcbd26SAdrian Hunter 	if (strncmp(str, "fifo:", 5))
1709a8fcbd26SAdrian Hunter 		return -EINVAL;
1710a8fcbd26SAdrian Hunter 
1711a8fcbd26SAdrian Hunter 	str += 5;
1712a8fcbd26SAdrian Hunter 	if (!*str || *str == ',')
1713a8fcbd26SAdrian Hunter 		return -EINVAL;
1714a8fcbd26SAdrian Hunter 
1715a8fcbd26SAdrian Hunter 	s = strdup(str);
1716a8fcbd26SAdrian Hunter 	if (!s)
1717a8fcbd26SAdrian Hunter 		return -ENOMEM;
1718a8fcbd26SAdrian Hunter 
1719a8fcbd26SAdrian Hunter 	p = strchr(s, ',');
1720a8fcbd26SAdrian Hunter 	if (p)
1721a8fcbd26SAdrian Hunter 		*p = '\0';
1722a8fcbd26SAdrian Hunter 
1723a8fcbd26SAdrian Hunter 	/*
1724a8fcbd26SAdrian Hunter 	 * O_RDWR avoids POLLHUPs which is necessary to allow the other
1725a8fcbd26SAdrian Hunter 	 * end of a FIFO to be repeatedly opened and closed.
1726a8fcbd26SAdrian Hunter 	 */
1727a8fcbd26SAdrian Hunter 	fd = open(s, O_RDWR | O_NONBLOCK | O_CLOEXEC);
1728a8fcbd26SAdrian Hunter 	if (fd < 0) {
1729a8fcbd26SAdrian Hunter 		pr_err("Failed to open '%s'\n", s);
1730a8fcbd26SAdrian Hunter 		ret = -errno;
1731a8fcbd26SAdrian Hunter 		goto out_free;
1732a8fcbd26SAdrian Hunter 	}
1733a8fcbd26SAdrian Hunter 	*ctl_fd = fd;
1734a8fcbd26SAdrian Hunter 	*ctl_fd_close = true;
1735a8fcbd26SAdrian Hunter 
1736a8fcbd26SAdrian Hunter 	if (p && *++p) {
1737a8fcbd26SAdrian Hunter 		/* O_RDWR | O_NONBLOCK means the other end need not be open */
1738a8fcbd26SAdrian Hunter 		fd = open(p, O_RDWR | O_NONBLOCK | O_CLOEXEC);
1739a8fcbd26SAdrian Hunter 		if (fd < 0) {
1740a8fcbd26SAdrian Hunter 			pr_err("Failed to open '%s'\n", p);
1741a8fcbd26SAdrian Hunter 			ret = -errno;
1742a8fcbd26SAdrian Hunter 			goto out_free;
1743a8fcbd26SAdrian Hunter 		}
1744a8fcbd26SAdrian Hunter 		*ctl_fd_ack = fd;
1745a8fcbd26SAdrian Hunter 	}
1746a8fcbd26SAdrian Hunter 
1747a8fcbd26SAdrian Hunter out_free:
1748a8fcbd26SAdrian Hunter 	free(s);
1749a8fcbd26SAdrian Hunter 	return ret;
1750a8fcbd26SAdrian Hunter }
1751a8fcbd26SAdrian Hunter 
1752a8fcbd26SAdrian Hunter int evlist__parse_control(const char *str, int *ctl_fd, int *ctl_fd_ack, bool *ctl_fd_close)
17539864a66dSAdrian Hunter {
17549864a66dSAdrian Hunter 	char *comma = NULL, *endptr = NULL;
17559864a66dSAdrian Hunter 
1756a8fcbd26SAdrian Hunter 	*ctl_fd_close = false;
1757a8fcbd26SAdrian Hunter 
17589864a66dSAdrian Hunter 	if (strncmp(str, "fd:", 3))
1759a8fcbd26SAdrian Hunter 		return evlist__parse_control_fifo(str, ctl_fd, ctl_fd_ack, ctl_fd_close);
17609864a66dSAdrian Hunter 
17619864a66dSAdrian Hunter 	*ctl_fd = strtoul(&str[3], &endptr, 0);
17629864a66dSAdrian Hunter 	if (endptr == &str[3])
17639864a66dSAdrian Hunter 		return -EINVAL;
17649864a66dSAdrian Hunter 
17659864a66dSAdrian Hunter 	comma = strchr(str, ',');
17669864a66dSAdrian Hunter 	if (comma) {
17679864a66dSAdrian Hunter 		if (endptr != comma)
17689864a66dSAdrian Hunter 			return -EINVAL;
17699864a66dSAdrian Hunter 
17709864a66dSAdrian Hunter 		*ctl_fd_ack = strtoul(comma + 1, &endptr, 0);
17719864a66dSAdrian Hunter 		if (endptr == comma + 1 || *endptr != '\0')
17729864a66dSAdrian Hunter 			return -EINVAL;
17739864a66dSAdrian Hunter 	}
17749864a66dSAdrian Hunter 
17759864a66dSAdrian Hunter 	return 0;
17769864a66dSAdrian Hunter }
17779864a66dSAdrian Hunter 
1778ee7fe31eSAdrian Hunter void evlist__close_control(int ctl_fd, int ctl_fd_ack, bool *ctl_fd_close)
1779ee7fe31eSAdrian Hunter {
1780ee7fe31eSAdrian Hunter 	if (*ctl_fd_close) {
1781ee7fe31eSAdrian Hunter 		*ctl_fd_close = false;
1782ee7fe31eSAdrian Hunter 		close(ctl_fd);
1783ee7fe31eSAdrian Hunter 		if (ctl_fd_ack >= 0)
1784ee7fe31eSAdrian Hunter 			close(ctl_fd_ack);
1785ee7fe31eSAdrian Hunter 	}
1786ee7fe31eSAdrian Hunter }
1787ee7fe31eSAdrian Hunter 
1788ec886bf5SAlexey Budankov int evlist__initialize_ctlfd(struct evlist *evlist, int fd, int ack)
1789ec886bf5SAlexey Budankov {
1790ec886bf5SAlexey Budankov 	if (fd == -1) {
1791ec886bf5SAlexey Budankov 		pr_debug("Control descriptor is not initialized\n");
1792ec886bf5SAlexey Budankov 		return 0;
1793ec886bf5SAlexey Budankov 	}
1794ec886bf5SAlexey Budankov 
1795ec886bf5SAlexey Budankov 	evlist->ctl_fd.pos = perf_evlist__add_pollfd(&evlist->core, fd, NULL, POLLIN,
1796ec886bf5SAlexey Budankov 						     fdarray_flag__nonfilterable);
1797ec886bf5SAlexey Budankov 	if (evlist->ctl_fd.pos < 0) {
1798ec886bf5SAlexey Budankov 		evlist->ctl_fd.pos = -1;
1799ec886bf5SAlexey Budankov 		pr_err("Failed to add ctl fd entry: %m\n");
1800ec886bf5SAlexey Budankov 		return -1;
1801ec886bf5SAlexey Budankov 	}
1802ec886bf5SAlexey Budankov 
1803ec886bf5SAlexey Budankov 	evlist->ctl_fd.fd = fd;
1804ec886bf5SAlexey Budankov 	evlist->ctl_fd.ack = ack;
1805ec886bf5SAlexey Budankov 
1806ec886bf5SAlexey Budankov 	return 0;
1807ec886bf5SAlexey Budankov }
1808ec886bf5SAlexey Budankov 
1809ec886bf5SAlexey Budankov bool evlist__ctlfd_initialized(struct evlist *evlist)
1810ec886bf5SAlexey Budankov {
1811ec886bf5SAlexey Budankov 	return evlist->ctl_fd.pos >= 0;
1812ec886bf5SAlexey Budankov }
1813ec886bf5SAlexey Budankov 
1814ec886bf5SAlexey Budankov int evlist__finalize_ctlfd(struct evlist *evlist)
1815ec886bf5SAlexey Budankov {
1816ec886bf5SAlexey Budankov 	struct pollfd *entries = evlist->core.pollfd.entries;
1817ec886bf5SAlexey Budankov 
1818ec886bf5SAlexey Budankov 	if (!evlist__ctlfd_initialized(evlist))
1819ec886bf5SAlexey Budankov 		return 0;
1820ec886bf5SAlexey Budankov 
1821ec886bf5SAlexey Budankov 	entries[evlist->ctl_fd.pos].fd = -1;
1822ec886bf5SAlexey Budankov 	entries[evlist->ctl_fd.pos].events = 0;
1823ec886bf5SAlexey Budankov 	entries[evlist->ctl_fd.pos].revents = 0;
1824ec886bf5SAlexey Budankov 
1825ec886bf5SAlexey Budankov 	evlist->ctl_fd.pos = -1;
1826ec886bf5SAlexey Budankov 	evlist->ctl_fd.ack = -1;
1827ec886bf5SAlexey Budankov 	evlist->ctl_fd.fd = -1;
1828ec886bf5SAlexey Budankov 
1829ec886bf5SAlexey Budankov 	return 0;
1830ec886bf5SAlexey Budankov }
1831ec886bf5SAlexey Budankov 
1832ec886bf5SAlexey Budankov static int evlist__ctlfd_recv(struct evlist *evlist, enum evlist_ctl_cmd *cmd,
1833ec886bf5SAlexey Budankov 			      char *cmd_data, size_t data_size)
1834ec886bf5SAlexey Budankov {
1835ec886bf5SAlexey Budankov 	int err;
1836ec886bf5SAlexey Budankov 	char c;
1837ec886bf5SAlexey Budankov 	size_t bytes_read = 0;
1838ec886bf5SAlexey Budankov 
183940db8ff5SAdrian Hunter 	*cmd = EVLIST_CTL_CMD_UNSUPPORTED;
1840ec886bf5SAlexey Budankov 	memset(cmd_data, 0, data_size);
1841ec886bf5SAlexey Budankov 	data_size--;
1842ec886bf5SAlexey Budankov 
1843ec886bf5SAlexey Budankov 	do {
1844ec886bf5SAlexey Budankov 		err = read(evlist->ctl_fd.fd, &c, 1);
1845ec886bf5SAlexey Budankov 		if (err > 0) {
1846ec886bf5SAlexey Budankov 			if (c == '\n' || c == '\0')
1847ec886bf5SAlexey Budankov 				break;
1848ec886bf5SAlexey Budankov 			cmd_data[bytes_read++] = c;
1849ec886bf5SAlexey Budankov 			if (bytes_read == data_size)
1850ec886bf5SAlexey Budankov 				break;
185140db8ff5SAdrian Hunter 			continue;
185240db8ff5SAdrian Hunter 		} else if (err == -1) {
185340db8ff5SAdrian Hunter 			if (errno == EINTR)
185440db8ff5SAdrian Hunter 				continue;
185540db8ff5SAdrian Hunter 			if (errno == EAGAIN || errno == EWOULDBLOCK)
185640db8ff5SAdrian Hunter 				err = 0;
185740db8ff5SAdrian Hunter 			else
1858ec886bf5SAlexey Budankov 				pr_err("Failed to read from ctlfd %d: %m\n", evlist->ctl_fd.fd);
1859ec886bf5SAlexey Budankov 		}
186040db8ff5SAdrian Hunter 		break;
1861ec886bf5SAlexey Budankov 	} while (1);
1862ec886bf5SAlexey Budankov 
1863ec886bf5SAlexey Budankov 	pr_debug("Message from ctl_fd: \"%s%s\"\n", cmd_data,
1864ec886bf5SAlexey Budankov 		 bytes_read == data_size ? "" : c == '\n' ? "\\n" : "\\0");
1865ec886bf5SAlexey Budankov 
186640db8ff5SAdrian Hunter 	if (bytes_read > 0) {
1867ec886bf5SAlexey Budankov 		if (!strncmp(cmd_data, EVLIST_CTL_CMD_ENABLE_TAG,
1868ec886bf5SAlexey Budankov 			     (sizeof(EVLIST_CTL_CMD_ENABLE_TAG)-1))) {
1869ec886bf5SAlexey Budankov 			*cmd = EVLIST_CTL_CMD_ENABLE;
1870ec886bf5SAlexey Budankov 		} else if (!strncmp(cmd_data, EVLIST_CTL_CMD_DISABLE_TAG,
1871ec886bf5SAlexey Budankov 				    (sizeof(EVLIST_CTL_CMD_DISABLE_TAG)-1))) {
1872ec886bf5SAlexey Budankov 			*cmd = EVLIST_CTL_CMD_DISABLE;
1873d20aff15SAdrian Hunter 		} else if (!strncmp(cmd_data, EVLIST_CTL_CMD_SNAPSHOT_TAG,
1874d20aff15SAdrian Hunter 				    (sizeof(EVLIST_CTL_CMD_SNAPSHOT_TAG)-1))) {
1875d20aff15SAdrian Hunter 			*cmd = EVLIST_CTL_CMD_SNAPSHOT;
1876d20aff15SAdrian Hunter 			pr_debug("is snapshot\n");
1877ec886bf5SAlexey Budankov 		}
1878ec886bf5SAlexey Budankov 	}
1879ec886bf5SAlexey Budankov 
188040db8ff5SAdrian Hunter 	return bytes_read ? (int)bytes_read : err;
1881ec886bf5SAlexey Budankov }
1882ec886bf5SAlexey Budankov 
1883d20aff15SAdrian Hunter int evlist__ctlfd_ack(struct evlist *evlist)
1884ec886bf5SAlexey Budankov {
1885ec886bf5SAlexey Budankov 	int err;
1886ec886bf5SAlexey Budankov 
1887ec886bf5SAlexey Budankov 	if (evlist->ctl_fd.ack == -1)
1888ec886bf5SAlexey Budankov 		return 0;
1889ec886bf5SAlexey Budankov 
1890ec886bf5SAlexey Budankov 	err = write(evlist->ctl_fd.ack, EVLIST_CTL_CMD_ACK_TAG,
1891ec886bf5SAlexey Budankov 		    sizeof(EVLIST_CTL_CMD_ACK_TAG));
1892ec886bf5SAlexey Budankov 	if (err == -1)
1893ec886bf5SAlexey Budankov 		pr_err("failed to write to ctl_ack_fd %d: %m\n", evlist->ctl_fd.ack);
1894ec886bf5SAlexey Budankov 
1895ec886bf5SAlexey Budankov 	return err;
1896ec886bf5SAlexey Budankov }
1897ec886bf5SAlexey Budankov 
1898ec886bf5SAlexey Budankov int evlist__ctlfd_process(struct evlist *evlist, enum evlist_ctl_cmd *cmd)
1899ec886bf5SAlexey Budankov {
1900ec886bf5SAlexey Budankov 	int err = 0;
1901ec886bf5SAlexey Budankov 	char cmd_data[EVLIST_CTL_CMD_MAX_LEN];
1902ec886bf5SAlexey Budankov 	int ctlfd_pos = evlist->ctl_fd.pos;
1903ec886bf5SAlexey Budankov 	struct pollfd *entries = evlist->core.pollfd.entries;
1904ec886bf5SAlexey Budankov 
1905ec886bf5SAlexey Budankov 	if (!evlist__ctlfd_initialized(evlist) || !entries[ctlfd_pos].revents)
1906ec886bf5SAlexey Budankov 		return 0;
1907ec886bf5SAlexey Budankov 
1908ec886bf5SAlexey Budankov 	if (entries[ctlfd_pos].revents & POLLIN) {
1909ec886bf5SAlexey Budankov 		err = evlist__ctlfd_recv(evlist, cmd, cmd_data,
1910ec886bf5SAlexey Budankov 					 EVLIST_CTL_CMD_MAX_LEN);
1911ec886bf5SAlexey Budankov 		if (err > 0) {
1912ec886bf5SAlexey Budankov 			switch (*cmd) {
1913ec886bf5SAlexey Budankov 			case EVLIST_CTL_CMD_ENABLE:
1914ec886bf5SAlexey Budankov 				evlist__enable(evlist);
1915ec886bf5SAlexey Budankov 				break;
1916ec886bf5SAlexey Budankov 			case EVLIST_CTL_CMD_DISABLE:
1917ec886bf5SAlexey Budankov 				evlist__disable(evlist);
1918ec886bf5SAlexey Budankov 				break;
1919d20aff15SAdrian Hunter 			case EVLIST_CTL_CMD_SNAPSHOT:
1920d20aff15SAdrian Hunter 				break;
1921ec886bf5SAlexey Budankov 			case EVLIST_CTL_CMD_ACK:
1922ec886bf5SAlexey Budankov 			case EVLIST_CTL_CMD_UNSUPPORTED:
1923ec886bf5SAlexey Budankov 			default:
1924ec886bf5SAlexey Budankov 				pr_debug("ctlfd: unsupported %d\n", *cmd);
1925ec886bf5SAlexey Budankov 				break;
1926ec886bf5SAlexey Budankov 			}
1927d20aff15SAdrian Hunter 			if (!(*cmd == EVLIST_CTL_CMD_ACK || *cmd == EVLIST_CTL_CMD_UNSUPPORTED ||
1928d20aff15SAdrian Hunter 			      *cmd == EVLIST_CTL_CMD_SNAPSHOT))
1929ec886bf5SAlexey Budankov 				evlist__ctlfd_ack(evlist);
1930ec886bf5SAlexey Budankov 		}
1931ec886bf5SAlexey Budankov 	}
1932ec886bf5SAlexey Budankov 
1933ec886bf5SAlexey Budankov 	if (entries[ctlfd_pos].revents & (POLLHUP | POLLERR))
1934ec886bf5SAlexey Budankov 		evlist__finalize_ctlfd(evlist);
1935ec886bf5SAlexey Budankov 	else
1936ec886bf5SAlexey Budankov 		entries[ctlfd_pos].revents = 0;
1937ec886bf5SAlexey Budankov 
1938ec886bf5SAlexey Budankov 	return err;
1939ec886bf5SAlexey Budankov }
1940b214ba8cSNamhyung Kim 
1941b214ba8cSNamhyung Kim struct evsel *evlist__find_evsel(struct evlist *evlist, int idx)
1942b214ba8cSNamhyung Kim {
1943b214ba8cSNamhyung Kim 	struct evsel *evsel;
1944b214ba8cSNamhyung Kim 
1945b214ba8cSNamhyung Kim 	evlist__for_each_entry(evlist, evsel) {
1946b214ba8cSNamhyung Kim 		if (evsel->idx == idx)
1947b214ba8cSNamhyung Kim 			return evsel;
1948b214ba8cSNamhyung Kim 	}
1949b214ba8cSNamhyung Kim 	return NULL;
1950b214ba8cSNamhyung Kim }
1951