xref: /openbmc/linux/tools/perf/util/evlist.c (revision 7cfcd1e0)
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 
81606e2c29SArnaldo Carvalho de Melo struct evlist *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 
93606e2c29SArnaldo Carvalho de Melo struct evlist *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 /**
1060a7e7ec9SArnaldo Carvalho de Melo  * 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  */
1120a7e7ec9SArnaldo Carvalho de Melo void 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 
1200a7e7ec9SArnaldo Carvalho de Melo static void 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 
1270a7e7ec9SArnaldo Carvalho de Melo 	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)
1710a7e7ec9SArnaldo Carvalho de Melo 		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 
180e414fd1aSArnaldo Carvalho de Melo void evlist__splice_list_tail(struct evlist *evlist, struct list_head *list)
18150d08e47SArnaldo Carvalho de Melo {
1826d2783feSJohn Garry 	while (!list_empty(list)) {
1836d2783feSJohn Garry 		struct evsel *evsel, *temp, *leader = NULL;
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);
1886d2783feSJohn Garry 			leader = evsel;
1896d2783feSJohn Garry 			break;
1906d2783feSJohn Garry 		}
1916d2783feSJohn Garry 
1926d2783feSJohn Garry 		__evlist__for_each_entry_safe(list, temp, evsel) {
1936d2783feSJohn Garry 			if (evsel->leader == leader) {
1946d2783feSJohn Garry 				list_del_init(&evsel->core.node);
1956d2783feSJohn Garry 				evlist__add(evlist, evsel);
1966d2783feSJohn Garry 			}
1976d2783feSJohn Garry 		}
198f114d6efSAdrian Hunter 	}
19950d08e47SArnaldo Carvalho de Melo }
20050d08e47SArnaldo Carvalho de Melo 
201c0e53476SArnaldo Carvalho de Melo int __evlist__set_tracepoints_handlers(struct evlist *evlist,
202c0e53476SArnaldo Carvalho de Melo 				       const struct evsel_str_handler *assocs, size_t nr_assocs)
203c0e53476SArnaldo Carvalho de Melo {
204c0e53476SArnaldo Carvalho de Melo 	size_t i;
205c0e53476SArnaldo Carvalho de Melo 	int err;
206c0e53476SArnaldo Carvalho de Melo 
207c0e53476SArnaldo Carvalho de Melo 	for (i = 0; i < nr_assocs; i++) {
208c0e53476SArnaldo Carvalho de Melo 		// Adding a handler for an event not in this evlist, just ignore it.
209b02736f7SArnaldo Carvalho de Melo 		struct evsel *evsel = evlist__find_tracepoint_by_name(evlist, assocs[i].name);
210c0e53476SArnaldo Carvalho de Melo 		if (evsel == NULL)
211c0e53476SArnaldo Carvalho de Melo 			continue;
212c0e53476SArnaldo Carvalho de Melo 
213c0e53476SArnaldo Carvalho de Melo 		err = -EEXIST;
214c0e53476SArnaldo Carvalho de Melo 		if (evsel->handler != NULL)
215c0e53476SArnaldo Carvalho de Melo 			goto out;
216c0e53476SArnaldo Carvalho de Melo 		evsel->handler = assocs[i].handler;
217c0e53476SArnaldo Carvalho de Melo 	}
218c0e53476SArnaldo Carvalho de Melo 
219c0e53476SArnaldo Carvalho de Melo 	err = 0;
220c0e53476SArnaldo Carvalho de Melo out:
221c0e53476SArnaldo Carvalho de Melo 	return err;
222c0e53476SArnaldo Carvalho de Melo }
223c0e53476SArnaldo Carvalho de Melo 
224a622eafaSArnaldo Carvalho de Melo void __evlist__set_leader(struct list_head *list)
22563dab225SArnaldo Carvalho de Melo {
22632dcd021SJiri Olsa 	struct evsel *evsel, *leader;
22763dab225SArnaldo Carvalho de Melo 
228b27c4eceSJiri Olsa 	leader = list_entry(list->next, struct evsel, core.node);
229b27c4eceSJiri Olsa 	evsel = list_entry(list->prev, struct evsel, core.node);
23097f63e4aSNamhyung Kim 
2315643b1a5SJiri Olsa 	leader->core.nr_members = evsel->idx - leader->idx + 1;
23263dab225SArnaldo Carvalho de Melo 
233e5cadb93SArnaldo Carvalho de Melo 	__evlist__for_each_entry(list, evsel) {
23463dab225SArnaldo Carvalho de Melo 		evsel->leader = leader;
23563dab225SArnaldo Carvalho de Melo 	}
23663dab225SArnaldo Carvalho de Melo }
23763dab225SArnaldo Carvalho de Melo 
238a622eafaSArnaldo Carvalho de Melo void evlist__set_leader(struct evlist *evlist)
2396a4bb04cSJiri Olsa {
2406484d2f9SJiri Olsa 	if (evlist->core.nr_entries) {
2416484d2f9SJiri Olsa 		evlist->nr_groups = evlist->core.nr_entries > 1 ? 1 : 0;
242a622eafaSArnaldo Carvalho de Melo 		__evlist__set_leader(&evlist->core.entries);
2436a4bb04cSJiri Olsa 	}
24497f63e4aSNamhyung Kim }
2456a4bb04cSJiri Olsa 
246e251abeeSArnaldo Carvalho de Melo int __evlist__add_default(struct evlist *evlist, bool precise)
247361c99a6SArnaldo Carvalho de Melo {
2488f6725a2SArnaldo Carvalho de Melo 	struct evsel *evsel = evsel__new_cycles(precise);
249361c99a6SArnaldo Carvalho de Melo 
250361c99a6SArnaldo Carvalho de Melo 	if (evsel == NULL)
2517c48dcfdSArnaldo Carvalho de Melo 		return -ENOMEM;
252361c99a6SArnaldo Carvalho de Melo 
253a1cf3a75SJiri Olsa 	evlist__add(evlist, evsel);
254361c99a6SArnaldo Carvalho de Melo 	return 0;
255361c99a6SArnaldo Carvalho de Melo }
2565c581041SArnaldo Carvalho de Melo 
257e251abeeSArnaldo Carvalho de Melo int evlist__add_dummy(struct evlist *evlist)
2585bae0250SArnaldo Carvalho de Melo {
2595bae0250SArnaldo Carvalho de Melo 	struct perf_event_attr attr = {
2605bae0250SArnaldo Carvalho de Melo 		.type	= PERF_TYPE_SOFTWARE,
2615bae0250SArnaldo Carvalho de Melo 		.config = PERF_COUNT_SW_DUMMY,
2625bae0250SArnaldo Carvalho de Melo 		.size	= sizeof(attr), /* to capture ABI version */
2635bae0250SArnaldo Carvalho de Melo 	};
2648f6725a2SArnaldo Carvalho de Melo 	struct evsel *evsel = evsel__new_idx(&attr, evlist->core.nr_entries);
2655bae0250SArnaldo Carvalho de Melo 
2665bae0250SArnaldo Carvalho de Melo 	if (evsel == NULL)
2675bae0250SArnaldo Carvalho de Melo 		return -ENOMEM;
2685bae0250SArnaldo Carvalho de Melo 
269a1cf3a75SJiri Olsa 	evlist__add(evlist, evsel);
2705bae0250SArnaldo Carvalho de Melo 	return 0;
2715bae0250SArnaldo Carvalho de Melo }
2725bae0250SArnaldo Carvalho de Melo 
273e251abeeSArnaldo Carvalho de Melo static int evlist__add_attrs(struct evlist *evlist, struct perf_event_attr *attrs, size_t nr_attrs)
27450d08e47SArnaldo Carvalho de Melo {
27532dcd021SJiri Olsa 	struct evsel *evsel, *n;
27650d08e47SArnaldo Carvalho de Melo 	LIST_HEAD(head);
27750d08e47SArnaldo Carvalho de Melo 	size_t i;
27850d08e47SArnaldo Carvalho de Melo 
27950d08e47SArnaldo Carvalho de Melo 	for (i = 0; i < nr_attrs; i++) {
2808f6725a2SArnaldo Carvalho de Melo 		evsel = evsel__new_idx(attrs + i, evlist->core.nr_entries + i);
28150d08e47SArnaldo Carvalho de Melo 		if (evsel == NULL)
28250d08e47SArnaldo Carvalho de Melo 			goto out_delete_partial_list;
283b27c4eceSJiri Olsa 		list_add_tail(&evsel->core.node, &head);
28450d08e47SArnaldo Carvalho de Melo 	}
28550d08e47SArnaldo Carvalho de Melo 
286e414fd1aSArnaldo Carvalho de Melo 	evlist__splice_list_tail(evlist, &head);
28750d08e47SArnaldo Carvalho de Melo 
28850d08e47SArnaldo Carvalho de Melo 	return 0;
28950d08e47SArnaldo Carvalho de Melo 
29050d08e47SArnaldo Carvalho de Melo out_delete_partial_list:
291e5cadb93SArnaldo Carvalho de Melo 	__evlist__for_each_entry_safe(&head, n, evsel)
2925eb2dd2aSJiri Olsa 		evsel__delete(evsel);
29350d08e47SArnaldo Carvalho de Melo 	return -1;
29450d08e47SArnaldo Carvalho de Melo }
29550d08e47SArnaldo Carvalho de Melo 
296e251abeeSArnaldo Carvalho de Melo int __evlist__add_default_attrs(struct evlist *evlist, struct perf_event_attr *attrs, size_t nr_attrs)
29779695e1bSArnaldo Carvalho de Melo {
29879695e1bSArnaldo Carvalho de Melo 	size_t i;
29979695e1bSArnaldo Carvalho de Melo 
30079695e1bSArnaldo Carvalho de Melo 	for (i = 0; i < nr_attrs; i++)
30179695e1bSArnaldo Carvalho de Melo 		event_attr_init(attrs + i);
30279695e1bSArnaldo Carvalho de Melo 
303a1cf3a75SJiri Olsa 	return evlist__add_attrs(evlist, attrs, nr_attrs);
30479695e1bSArnaldo Carvalho de Melo }
30579695e1bSArnaldo Carvalho de Melo 
306b02736f7SArnaldo Carvalho de Melo struct evsel *evlist__find_tracepoint_by_id(struct evlist *evlist, int id)
307ee29be62SArnaldo Carvalho de Melo {
30832dcd021SJiri Olsa 	struct evsel *evsel;
309ee29be62SArnaldo Carvalho de Melo 
310e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, evsel) {
3111fc632ceSJiri Olsa 		if (evsel->core.attr.type   == PERF_TYPE_TRACEPOINT &&
3121fc632ceSJiri Olsa 		    (int)evsel->core.attr.config == id)
313ee29be62SArnaldo Carvalho de Melo 			return evsel;
314ee29be62SArnaldo Carvalho de Melo 	}
315ee29be62SArnaldo Carvalho de Melo 
316ee29be62SArnaldo Carvalho de Melo 	return NULL;
317ee29be62SArnaldo Carvalho de Melo }
318ee29be62SArnaldo Carvalho de Melo 
319b02736f7SArnaldo Carvalho de Melo struct evsel *evlist__find_tracepoint_by_name(struct evlist *evlist, const char *name)
320a2f2804aSDavid Ahern {
32132dcd021SJiri Olsa 	struct evsel *evsel;
322a2f2804aSDavid Ahern 
323e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, evsel) {
3241fc632ceSJiri Olsa 		if ((evsel->core.attr.type == PERF_TYPE_TRACEPOINT) &&
325a2f2804aSDavid Ahern 		    (strcmp(evsel->name, name) == 0))
326a2f2804aSDavid Ahern 			return evsel;
327a2f2804aSDavid Ahern 	}
328a2f2804aSDavid Ahern 
329a2f2804aSDavid Ahern 	return NULL;
330a2f2804aSDavid Ahern }
331a2f2804aSDavid Ahern 
332e251abeeSArnaldo Carvalho de Melo int evlist__add_newtp(struct evlist *evlist, const char *sys, const char *name, void *handler)
33339876e7dSArnaldo Carvalho de Melo {
3348f6725a2SArnaldo Carvalho de Melo 	struct evsel *evsel = evsel__newtp(sys, name);
33539876e7dSArnaldo Carvalho de Melo 
3368dd2a131SJiri Olsa 	if (IS_ERR(evsel))
33739876e7dSArnaldo Carvalho de Melo 		return -1;
33839876e7dSArnaldo Carvalho de Melo 
339744a9719SArnaldo Carvalho de Melo 	evsel->handler = handler;
340a1cf3a75SJiri Olsa 	evlist__add(evlist, evsel);
34139876e7dSArnaldo Carvalho de Melo 	return 0;
34239876e7dSArnaldo Carvalho de Melo }
34339876e7dSArnaldo Carvalho de Melo 
344f63c2f5aSArnaldo Carvalho de Melo static int evlist__nr_threads(struct evlist *evlist, struct evsel *evsel)
345bf8e8f4bSAdrian Hunter {
346648b5af3SJiri Olsa 	if (evsel->core.system_wide)
347bf8e8f4bSAdrian Hunter 		return 1;
348bf8e8f4bSAdrian Hunter 	else
349a2f354e3SJiri Olsa 		return perf_thread_map__nr(evlist->core.threads);
350bf8e8f4bSAdrian Hunter }
351bf8e8f4bSAdrian Hunter 
352a8cbe40fSAndi Kleen void evlist__cpu_iter_start(struct evlist *evlist)
353a8cbe40fSAndi Kleen {
354a8cbe40fSAndi Kleen 	struct evsel *pos;
355a8cbe40fSAndi Kleen 
356a8cbe40fSAndi Kleen 	/*
357a8cbe40fSAndi Kleen 	 * Reset the per evsel cpu_iter. This is needed because
358a8cbe40fSAndi Kleen 	 * each evsel's cpumap may have a different index space,
359a8cbe40fSAndi Kleen 	 * and some operations need the index to modify
360a8cbe40fSAndi Kleen 	 * the FD xyarray (e.g. open, close)
361a8cbe40fSAndi Kleen 	 */
362a8cbe40fSAndi Kleen 	evlist__for_each_entry(evlist, pos)
363a8cbe40fSAndi Kleen 		pos->cpu_iter = 0;
364a8cbe40fSAndi Kleen }
365a8cbe40fSAndi Kleen 
366a8cbe40fSAndi Kleen bool evsel__cpu_iter_skip_no_inc(struct evsel *ev, int cpu)
367a8cbe40fSAndi Kleen {
368a8cbe40fSAndi Kleen 	if (ev->cpu_iter >= ev->core.cpus->nr)
369a8cbe40fSAndi Kleen 		return true;
370a8cbe40fSAndi Kleen 	if (cpu >= 0 && ev->core.cpus->map[ev->cpu_iter] != cpu)
371a8cbe40fSAndi Kleen 		return true;
372a8cbe40fSAndi Kleen 	return false;
373a8cbe40fSAndi Kleen }
374a8cbe40fSAndi Kleen 
375a8cbe40fSAndi Kleen bool evsel__cpu_iter_skip(struct evsel *ev, int cpu)
376a8cbe40fSAndi Kleen {
377a8cbe40fSAndi Kleen 	if (!evsel__cpu_iter_skip_no_inc(ev, cpu)) {
378a8cbe40fSAndi Kleen 		ev->cpu_iter++;
379a8cbe40fSAndi Kleen 		return false;
380a8cbe40fSAndi Kleen 	}
381a8cbe40fSAndi Kleen 	return true;
382a8cbe40fSAndi Kleen }
383a8cbe40fSAndi Kleen 
384*7cfcd1e0SJiri Olsa static int evsel__strcmp(struct evsel *pos, char *evsel_name)
385*7cfcd1e0SJiri Olsa {
386*7cfcd1e0SJiri Olsa 	if (!evsel_name)
387*7cfcd1e0SJiri Olsa 		return 0;
388*7cfcd1e0SJiri Olsa 	if (evsel__is_dummy_event(pos))
389*7cfcd1e0SJiri Olsa 		return 1;
390*7cfcd1e0SJiri Olsa 	return strcmp(pos->name, evsel_name);
391*7cfcd1e0SJiri Olsa }
392*7cfcd1e0SJiri Olsa 
393*7cfcd1e0SJiri Olsa static int evlist__is_enabled(struct evlist *evlist)
394*7cfcd1e0SJiri Olsa {
395*7cfcd1e0SJiri Olsa 	struct evsel *pos;
396*7cfcd1e0SJiri Olsa 
397*7cfcd1e0SJiri Olsa 	evlist__for_each_entry(evlist, pos) {
398*7cfcd1e0SJiri Olsa 		if (!evsel__is_group_leader(pos) || !pos->core.fd)
399*7cfcd1e0SJiri Olsa 			continue;
400*7cfcd1e0SJiri Olsa 		/* If at least one event is enabled, evlist is enabled. */
401*7cfcd1e0SJiri Olsa 		if (!pos->disabled)
402*7cfcd1e0SJiri Olsa 			return true;
403*7cfcd1e0SJiri Olsa 	}
404*7cfcd1e0SJiri Olsa 	return false;
405*7cfcd1e0SJiri Olsa }
406*7cfcd1e0SJiri Olsa 
407*7cfcd1e0SJiri Olsa static void __evlist__disable(struct evlist *evlist, char *evsel_name)
4084152ab37SArnaldo Carvalho de Melo {
40932dcd021SJiri Olsa 	struct evsel *pos;
410704e2f5bSAndi Kleen 	struct affinity affinity;
41187cf8360SAdrian Hunter 	int cpu, i, imm = 0;
41287cf8360SAdrian Hunter 	bool has_imm = false;
413704e2f5bSAndi Kleen 
414704e2f5bSAndi Kleen 	if (affinity__setup(&affinity) < 0)
415704e2f5bSAndi Kleen 		return;
416704e2f5bSAndi Kleen 
41787cf8360SAdrian Hunter 	/* Disable 'immediate' events last */
41887cf8360SAdrian Hunter 	for (imm = 0; imm <= 1; imm++) {
419704e2f5bSAndi Kleen 		evlist__for_each_cpu(evlist, i, cpu) {
420704e2f5bSAndi Kleen 			affinity__set(&affinity, cpu);
4214152ab37SArnaldo Carvalho de Melo 
422e5cadb93SArnaldo Carvalho de Melo 			evlist__for_each_entry(evlist, pos) {
423*7cfcd1e0SJiri Olsa 				if (evsel__strcmp(pos, evsel_name))
424*7cfcd1e0SJiri Olsa 					continue;
425704e2f5bSAndi Kleen 				if (evsel__cpu_iter_skip(pos, cpu))
426704e2f5bSAndi Kleen 					continue;
427c754c382SArnaldo Carvalho de Melo 				if (pos->disabled || !evsel__is_group_leader(pos) || !pos->core.fd)
4283fe4430dSJiri Olsa 					continue;
42987cf8360SAdrian Hunter 				if (pos->immediate)
43087cf8360SAdrian Hunter 					has_imm = true;
43187cf8360SAdrian Hunter 				if (pos->immediate != imm)
43287cf8360SAdrian Hunter 					continue;
433704e2f5bSAndi Kleen 				evsel__disable_cpu(pos, pos->cpu_iter - 1);
434704e2f5bSAndi Kleen 			}
435704e2f5bSAndi Kleen 		}
43687cf8360SAdrian Hunter 		if (!has_imm)
43787cf8360SAdrian Hunter 			break;
43887cf8360SAdrian Hunter 	}
43987cf8360SAdrian Hunter 
440704e2f5bSAndi Kleen 	affinity__cleanup(&affinity);
441704e2f5bSAndi Kleen 	evlist__for_each_entry(evlist, pos) {
442*7cfcd1e0SJiri Olsa 		if (evsel__strcmp(pos, evsel_name))
443*7cfcd1e0SJiri Olsa 			continue;
444c754c382SArnaldo Carvalho de Melo 		if (!evsel__is_group_leader(pos) || !pos->core.fd)
445704e2f5bSAndi Kleen 			continue;
446704e2f5bSAndi Kleen 		pos->disabled = true;
4474152ab37SArnaldo Carvalho de Melo 	}
4482b56bcfbSArnaldo Carvalho de Melo 
449*7cfcd1e0SJiri Olsa 	/*
450*7cfcd1e0SJiri Olsa 	 * If we disabled only single event, we need to check
451*7cfcd1e0SJiri Olsa 	 * the enabled state of the evlist manually.
452*7cfcd1e0SJiri Olsa 	 */
453*7cfcd1e0SJiri Olsa 	if (evsel_name)
454*7cfcd1e0SJiri Olsa 		evlist->enabled = evlist__is_enabled(evlist);
455*7cfcd1e0SJiri Olsa 	else
4562b56bcfbSArnaldo Carvalho de Melo 		evlist->enabled = false;
4574152ab37SArnaldo Carvalho de Melo }
4584152ab37SArnaldo Carvalho de Melo 
459*7cfcd1e0SJiri Olsa void evlist__disable(struct evlist *evlist)
460*7cfcd1e0SJiri Olsa {
461*7cfcd1e0SJiri Olsa 	__evlist__disable(evlist, NULL);
462*7cfcd1e0SJiri Olsa }
463*7cfcd1e0SJiri Olsa 
464*7cfcd1e0SJiri Olsa void evlist__disable_evsel(struct evlist *evlist, char *evsel_name)
465*7cfcd1e0SJiri Olsa {
466*7cfcd1e0SJiri Olsa 	__evlist__disable(evlist, evsel_name);
467*7cfcd1e0SJiri Olsa }
468*7cfcd1e0SJiri Olsa 
469*7cfcd1e0SJiri Olsa static void __evlist__enable(struct evlist *evlist, char *evsel_name)
470764e16a3SDavid Ahern {
47132dcd021SJiri Olsa 	struct evsel *pos;
472704e2f5bSAndi Kleen 	struct affinity affinity;
473704e2f5bSAndi Kleen 	int cpu, i;
474764e16a3SDavid Ahern 
475704e2f5bSAndi Kleen 	if (affinity__setup(&affinity) < 0)
476704e2f5bSAndi Kleen 		return;
477704e2f5bSAndi Kleen 
478704e2f5bSAndi Kleen 	evlist__for_each_cpu(evlist, i, cpu) {
479704e2f5bSAndi Kleen 		affinity__set(&affinity, cpu);
480704e2f5bSAndi Kleen 
481704e2f5bSAndi Kleen 		evlist__for_each_entry(evlist, pos) {
482*7cfcd1e0SJiri Olsa 			if (evsel__strcmp(pos, evsel_name))
483*7cfcd1e0SJiri Olsa 				continue;
484704e2f5bSAndi Kleen 			if (evsel__cpu_iter_skip(pos, cpu))
485704e2f5bSAndi Kleen 				continue;
486c754c382SArnaldo Carvalho de Melo 			if (!evsel__is_group_leader(pos) || !pos->core.fd)
487704e2f5bSAndi Kleen 				continue;
488704e2f5bSAndi Kleen 			evsel__enable_cpu(pos, pos->cpu_iter - 1);
489704e2f5bSAndi Kleen 		}
490704e2f5bSAndi Kleen 	}
491704e2f5bSAndi Kleen 	affinity__cleanup(&affinity);
492e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, pos) {
493*7cfcd1e0SJiri Olsa 		if (evsel__strcmp(pos, evsel_name))
494*7cfcd1e0SJiri Olsa 			continue;
495c754c382SArnaldo Carvalho de Melo 		if (!evsel__is_group_leader(pos) || !pos->core.fd)
4963fe4430dSJiri Olsa 			continue;
497704e2f5bSAndi Kleen 		pos->disabled = false;
498764e16a3SDavid Ahern 	}
4992b56bcfbSArnaldo Carvalho de Melo 
500*7cfcd1e0SJiri Olsa 	/*
501*7cfcd1e0SJiri Olsa 	 * Even single event sets the 'enabled' for evlist,
502*7cfcd1e0SJiri Olsa 	 * so the toggle can work properly and toggle to
503*7cfcd1e0SJiri Olsa 	 * 'disabled' state.
504*7cfcd1e0SJiri Olsa 	 */
5052b56bcfbSArnaldo Carvalho de Melo 	evlist->enabled = true;
5062b56bcfbSArnaldo Carvalho de Melo }
5072b56bcfbSArnaldo Carvalho de Melo 
508*7cfcd1e0SJiri Olsa void evlist__enable(struct evlist *evlist)
509*7cfcd1e0SJiri Olsa {
510*7cfcd1e0SJiri Olsa 	__evlist__enable(evlist, NULL);
511*7cfcd1e0SJiri Olsa }
512*7cfcd1e0SJiri Olsa 
513*7cfcd1e0SJiri Olsa void evlist__enable_evsel(struct evlist *evlist, char *evsel_name)
514*7cfcd1e0SJiri Olsa {
515*7cfcd1e0SJiri Olsa 	__evlist__enable(evlist, evsel_name);
516*7cfcd1e0SJiri Olsa }
517*7cfcd1e0SJiri Olsa 
518ade9d208SArnaldo Carvalho de Melo void evlist__toggle_enable(struct evlist *evlist)
5192b56bcfbSArnaldo Carvalho de Melo {
520e74676deSJiri Olsa 	(evlist->enabled ? evlist__disable : evlist__enable)(evlist);
521764e16a3SDavid Ahern }
522764e16a3SDavid Ahern 
52337b01abeSArnaldo Carvalho de Melo static int evlist__enable_event_cpu(struct evlist *evlist, struct evsel *evsel, int cpu)
5241c65056cSAdrian Hunter {
52518ef15c6SArnaldo Carvalho de Melo 	int thread;
526f63c2f5aSArnaldo Carvalho de Melo 	int nr_threads = evlist__nr_threads(evlist, evsel);
5271c65056cSAdrian Hunter 
5289dfcb759SJiri Olsa 	if (!evsel->core.fd)
5291c65056cSAdrian Hunter 		return -EINVAL;
5301c65056cSAdrian Hunter 
5311c65056cSAdrian Hunter 	for (thread = 0; thread < nr_threads; thread++) {
53218ef15c6SArnaldo Carvalho de Melo 		int err = ioctl(FD(evsel, cpu, thread), PERF_EVENT_IOC_ENABLE, 0);
5331c65056cSAdrian Hunter 		if (err)
5341c65056cSAdrian Hunter 			return err;
5351c65056cSAdrian Hunter 	}
5361c65056cSAdrian Hunter 	return 0;
5371c65056cSAdrian Hunter }
5381c65056cSAdrian Hunter 
53937b01abeSArnaldo Carvalho de Melo static int evlist__enable_event_thread(struct evlist *evlist, struct evsel *evsel, int thread)
5401c65056cSAdrian Hunter {
54118ef15c6SArnaldo Carvalho de Melo 	int cpu;
5426549cd8fSJiri Olsa 	int nr_cpus = perf_cpu_map__nr(evlist->core.cpus);
5431c65056cSAdrian Hunter 
5449dfcb759SJiri Olsa 	if (!evsel->core.fd)
5451c65056cSAdrian Hunter 		return -EINVAL;
5461c65056cSAdrian Hunter 
5471c65056cSAdrian Hunter 	for (cpu = 0; cpu < nr_cpus; cpu++) {
54818ef15c6SArnaldo Carvalho de Melo 		int err = ioctl(FD(evsel, cpu, thread), PERF_EVENT_IOC_ENABLE, 0);
5491c65056cSAdrian Hunter 		if (err)
5501c65056cSAdrian Hunter 			return err;
5511c65056cSAdrian Hunter 	}
5521c65056cSAdrian Hunter 	return 0;
5531c65056cSAdrian Hunter }
5541c65056cSAdrian Hunter 
55537b01abeSArnaldo Carvalho de Melo int evlist__enable_event_idx(struct evlist *evlist, struct evsel *evsel, int idx)
5561c65056cSAdrian Hunter {
557315c0a1fSJiri Olsa 	bool per_cpu_mmaps = !perf_cpu_map__empty(evlist->core.cpus);
5581c65056cSAdrian Hunter 
5591c65056cSAdrian Hunter 	if (per_cpu_mmaps)
56037b01abeSArnaldo Carvalho de Melo 		return evlist__enable_event_cpu(evlist, evsel, idx);
56137b01abeSArnaldo Carvalho de Melo 
56237b01abeSArnaldo Carvalho de Melo 	return evlist__enable_event_thread(evlist, evsel, idx);
5631c65056cSAdrian Hunter }
5641c65056cSAdrian Hunter 
565f4009e7bSJiri Olsa int evlist__add_pollfd(struct evlist *evlist, int fd)
566e4b356b5SArnaldo Carvalho de Melo {
567ab4c1f9fSAlexey Budankov 	return perf_evlist__add_pollfd(&evlist->core, fd, NULL, POLLIN, fdarray_flag__default);
568e4b356b5SArnaldo Carvalho de Melo }
5691b85337dSArnaldo Carvalho de Melo 
570f4009e7bSJiri Olsa int evlist__filter_pollfd(struct evlist *evlist, short revents_and_mask)
5711ddec7f0SArnaldo Carvalho de Melo {
57284227cb1SJiri Olsa 	return perf_evlist__filter_pollfd(&evlist->core, revents_and_mask);
5731ddec7f0SArnaldo Carvalho de Melo }
5741ddec7f0SArnaldo Carvalho de Melo 
57580ab2987SJiri Olsa int evlist__poll(struct evlist *evlist, int timeout)
576f66a889dSArnaldo Carvalho de Melo {
57780ab2987SJiri Olsa 	return perf_evlist__poll(&evlist->core, timeout);
578f66a889dSArnaldo Carvalho de Melo }
579f66a889dSArnaldo Carvalho de Melo 
5803ccf8a7bSArnaldo Carvalho de Melo struct perf_sample_id *evlist__id2sid(struct evlist *evlist, u64 id)
58170db7533SArnaldo Carvalho de Melo {
58270db7533SArnaldo Carvalho de Melo 	struct hlist_head *head;
58370db7533SArnaldo Carvalho de Melo 	struct perf_sample_id *sid;
58470db7533SArnaldo Carvalho de Melo 	int hash;
58570db7533SArnaldo Carvalho de Melo 
58670db7533SArnaldo Carvalho de Melo 	hash = hash_64(id, PERF_EVLIST__HLIST_BITS);
5871d5af02dSJiri Olsa 	head = &evlist->core.heads[hash];
58870db7533SArnaldo Carvalho de Melo 
589b67bfe0dSSasha Levin 	hlist_for_each_entry(sid, head, node)
59070db7533SArnaldo Carvalho de Melo 		if (sid->id == id)
591932a3594SJiri Olsa 			return sid;
592932a3594SJiri Olsa 
593932a3594SJiri Olsa 	return NULL;
594932a3594SJiri Olsa }
595932a3594SJiri Olsa 
5963ccf8a7bSArnaldo Carvalho de Melo struct evsel *evlist__id2evsel(struct evlist *evlist, u64 id)
597932a3594SJiri Olsa {
598932a3594SJiri Olsa 	struct perf_sample_id *sid;
599932a3594SJiri Olsa 
6006484d2f9SJiri Olsa 	if (evlist->core.nr_entries == 1 || !id)
601515dbe48SJiri Olsa 		return evlist__first(evlist);
602932a3594SJiri Olsa 
6033ccf8a7bSArnaldo Carvalho de Melo 	sid = evlist__id2sid(evlist, id);
604932a3594SJiri Olsa 	if (sid)
60570c20369SJiri Olsa 		return container_of(sid->evsel, struct evsel, core);
60630e68bccSNamhyung Kim 
6078cedf3a5SArnaldo Carvalho de Melo 	if (!evlist__sample_id_all(evlist))
608515dbe48SJiri Olsa 		return evlist__first(evlist);
60930e68bccSNamhyung Kim 
61070db7533SArnaldo Carvalho de Melo 	return NULL;
61170db7533SArnaldo Carvalho de Melo }
61204391debSArnaldo Carvalho de Melo 
6133ccf8a7bSArnaldo Carvalho de Melo struct evsel *evlist__id2evsel_strict(struct evlist *evlist, u64 id)
614dddcf6abSAdrian Hunter {
615dddcf6abSAdrian Hunter 	struct perf_sample_id *sid;
616dddcf6abSAdrian Hunter 
617dddcf6abSAdrian Hunter 	if (!id)
618dddcf6abSAdrian Hunter 		return NULL;
619dddcf6abSAdrian Hunter 
6203ccf8a7bSArnaldo Carvalho de Melo 	sid = evlist__id2sid(evlist, id);
621dddcf6abSAdrian Hunter 	if (sid)
62270c20369SJiri Olsa 		return container_of(sid->evsel, struct evsel, core);
623dddcf6abSAdrian Hunter 
624dddcf6abSAdrian Hunter 	return NULL;
625dddcf6abSAdrian Hunter }
626dddcf6abSAdrian Hunter 
6273ccf8a7bSArnaldo Carvalho de Melo static int evlist__event2id(struct evlist *evlist, union perf_event *event, u64 *id)
62875562573SAdrian Hunter {
629b1fcd190SJiri Olsa 	const __u64 *array = event->sample.array;
63075562573SAdrian Hunter 	ssize_t n;
63175562573SAdrian Hunter 
63275562573SAdrian Hunter 	n = (event->header.size - sizeof(event->header)) >> 3;
63375562573SAdrian Hunter 
63475562573SAdrian Hunter 	if (event->header.type == PERF_RECORD_SAMPLE) {
63575562573SAdrian Hunter 		if (evlist->id_pos >= n)
63675562573SAdrian Hunter 			return -1;
63775562573SAdrian Hunter 		*id = array[evlist->id_pos];
63875562573SAdrian Hunter 	} else {
63975562573SAdrian Hunter 		if (evlist->is_pos > n)
64075562573SAdrian Hunter 			return -1;
64175562573SAdrian Hunter 		n -= evlist->is_pos;
64275562573SAdrian Hunter 		*id = array[n];
64375562573SAdrian Hunter 	}
64475562573SAdrian Hunter 	return 0;
64575562573SAdrian Hunter }
64675562573SAdrian Hunter 
6473ccf8a7bSArnaldo Carvalho de Melo struct evsel *evlist__event2evsel(struct evlist *evlist, union perf_event *event)
64875562573SAdrian Hunter {
649515dbe48SJiri Olsa 	struct evsel *first = evlist__first(evlist);
65075562573SAdrian Hunter 	struct hlist_head *head;
65175562573SAdrian Hunter 	struct perf_sample_id *sid;
65275562573SAdrian Hunter 	int hash;
65375562573SAdrian Hunter 	u64 id;
65475562573SAdrian Hunter 
6556484d2f9SJiri Olsa 	if (evlist->core.nr_entries == 1)
65698be6966SAdrian Hunter 		return first;
65798be6966SAdrian Hunter 
6581fc632ceSJiri Olsa 	if (!first->core.attr.sample_id_all &&
65998be6966SAdrian Hunter 	    event->header.type != PERF_RECORD_SAMPLE)
66098be6966SAdrian Hunter 		return first;
66175562573SAdrian Hunter 
6623ccf8a7bSArnaldo Carvalho de Melo 	if (evlist__event2id(evlist, event, &id))
66375562573SAdrian Hunter 		return NULL;
66475562573SAdrian Hunter 
66575562573SAdrian Hunter 	/* Synthesized events have an id of zero */
66675562573SAdrian Hunter 	if (!id)
66798be6966SAdrian Hunter 		return first;
66875562573SAdrian Hunter 
66975562573SAdrian Hunter 	hash = hash_64(id, PERF_EVLIST__HLIST_BITS);
6701d5af02dSJiri Olsa 	head = &evlist->core.heads[hash];
67175562573SAdrian Hunter 
67275562573SAdrian Hunter 	hlist_for_each_entry(sid, head, node) {
67375562573SAdrian Hunter 		if (sid->id == id)
67470c20369SJiri Olsa 			return container_of(sid->evsel, struct evsel, core);
67575562573SAdrian Hunter 	}
67675562573SAdrian Hunter 	return NULL;
67775562573SAdrian Hunter }
67875562573SAdrian Hunter 
6790a60b339SArnaldo Carvalho de Melo static int evlist__set_paused(struct evlist *evlist, bool value)
68065aea233SWang Nan {
68165aea233SWang Nan 	int i;
68265aea233SWang Nan 
6830b72d69aSWang Nan 	if (!evlist->overwrite_mmap)
684078c3386SWang Nan 		return 0;
685078c3386SWang Nan 
686c976ee11SJiri Olsa 	for (i = 0; i < evlist->core.nr_mmaps; i++) {
6872cf07b29SJiri Olsa 		int fd = evlist->overwrite_mmap[i].core.fd;
68865aea233SWang Nan 		int err;
68965aea233SWang Nan 
69065aea233SWang Nan 		if (fd < 0)
69165aea233SWang Nan 			continue;
69265aea233SWang Nan 		err = ioctl(fd, PERF_EVENT_IOC_PAUSE_OUTPUT, value ? 1 : 0);
69365aea233SWang Nan 		if (err)
69465aea233SWang Nan 			return err;
69565aea233SWang Nan 	}
69665aea233SWang Nan 	return 0;
69765aea233SWang Nan }
69865aea233SWang Nan 
6990a60b339SArnaldo Carvalho de Melo static int evlist__pause(struct evlist *evlist)
70065aea233SWang Nan {
7010a60b339SArnaldo Carvalho de Melo 	return evlist__set_paused(evlist, true);
70265aea233SWang Nan }
70365aea233SWang Nan 
7040a60b339SArnaldo Carvalho de Melo static int evlist__resume(struct evlist *evlist)
70565aea233SWang Nan {
7060a60b339SArnaldo Carvalho de Melo 	return evlist__set_paused(evlist, false);
70765aea233SWang Nan }
70865aea233SWang Nan 
709db6b7b13SJiri Olsa static void evlist__munmap_nofree(struct evlist *evlist)
710f8a95309SArnaldo Carvalho de Melo {
711aece948fSArnaldo Carvalho de Melo 	int i;
712f8a95309SArnaldo Carvalho de Melo 
713b2cb615dSWang Nan 	if (evlist->mmap)
714c976ee11SJiri Olsa 		for (i = 0; i < evlist->core.nr_mmaps; i++)
71580e53d11SJiri Olsa 			perf_mmap__munmap(&evlist->mmap[i].core);
716b2cb615dSWang Nan 
7170b72d69aSWang Nan 	if (evlist->overwrite_mmap)
718c976ee11SJiri Olsa 		for (i = 0; i < evlist->core.nr_mmaps; i++)
71980e53d11SJiri Olsa 			perf_mmap__munmap(&evlist->overwrite_mmap[i].core);
720a1f72618SWang Nan }
721aece948fSArnaldo Carvalho de Melo 
722db6b7b13SJiri Olsa void evlist__munmap(struct evlist *evlist)
723a1f72618SWang Nan {
724db6b7b13SJiri Olsa 	evlist__munmap_nofree(evlist);
72504662523SArnaldo Carvalho de Melo 	zfree(&evlist->mmap);
7260b72d69aSWang Nan 	zfree(&evlist->overwrite_mmap);
727f8a95309SArnaldo Carvalho de Melo }
728f8a95309SArnaldo Carvalho de Melo 
72980e53d11SJiri Olsa static void perf_mmap__unmap_cb(struct perf_mmap *map)
73080e53d11SJiri Olsa {
73180e53d11SJiri Olsa 	struct mmap *m = container_of(map, struct mmap, core);
73280e53d11SJiri Olsa 
73380e53d11SJiri Olsa 	mmap__munmap(m);
73480e53d11SJiri Olsa }
73580e53d11SJiri Olsa 
736d50cf361SJiri Olsa static struct mmap *evlist__alloc_mmap(struct evlist *evlist,
7372c5f6d87SKan Liang 				       bool overwrite)
738f8a95309SArnaldo Carvalho de Melo {
739d4c6fb36SWang Nan 	int i;
740a5830532SJiri Olsa 	struct mmap *map;
741d4c6fb36SWang Nan 
742c976ee11SJiri Olsa 	map = zalloc(evlist->core.nr_mmaps * sizeof(struct mmap));
7438db6d6b1SWang Nan 	if (!map)
7448db6d6b1SWang Nan 		return NULL;
745946ae1d4SWang Nan 
746c976ee11SJiri Olsa 	for (i = 0; i < evlist->core.nr_mmaps; i++) {
7476eb65f7aSJiri Olsa 		struct perf_mmap *prev = i ? &map[i - 1].core : NULL;
7486eb65f7aSJiri Olsa 
7494738ca30SArnaldo Carvalho de Melo 		/*
7504738ca30SArnaldo Carvalho de Melo 		 * When the perf_mmap() call is made we grab one refcount, plus
7516afad54dSKan Liang 		 * one extra to let perf_mmap__consume() get the last
7524738ca30SArnaldo Carvalho de Melo 		 * events after all real references (perf_mmap__get()) are
7534738ca30SArnaldo Carvalho de Melo 		 * dropped.
7544738ca30SArnaldo Carvalho de Melo 		 *
7554738ca30SArnaldo Carvalho de Melo 		 * Each PERF_EVENT_IOC_SET_OUTPUT points to this mmap and
7564738ca30SArnaldo Carvalho de Melo 		 * thus does perf_mmap__get() on it.
7574738ca30SArnaldo Carvalho de Melo 		 */
7586eb65f7aSJiri Olsa 		perf_mmap__init(&map[i].core, prev, overwrite, perf_mmap__unmap_cb);
7594738ca30SArnaldo Carvalho de Melo 	}
760353120b4SJiri Olsa 
7618db6d6b1SWang Nan 	return map;
762f8a95309SArnaldo Carvalho de Melo }
763f8a95309SArnaldo Carvalho de Melo 
7649abd2ab2SJiri Olsa static void
7659abd2ab2SJiri Olsa perf_evlist__mmap_cb_idx(struct perf_evlist *_evlist,
7669abd2ab2SJiri Olsa 			 struct perf_mmap_param *_mp,
7679abd2ab2SJiri Olsa 			 int idx, bool per_cpu)
7689abd2ab2SJiri Olsa {
7699abd2ab2SJiri Olsa 	struct evlist *evlist = container_of(_evlist, struct evlist, core);
7709abd2ab2SJiri Olsa 	struct mmap_params *mp = container_of(_mp, struct mmap_params, core);
7719abd2ab2SJiri Olsa 
7729abd2ab2SJiri Olsa 	auxtrace_mmap_params__set_idx(&mp->auxtrace_mp, evlist, idx, per_cpu);
7739abd2ab2SJiri Olsa }
7749abd2ab2SJiri Olsa 
775bb1b1885SJiri Olsa static struct perf_mmap*
776bb1b1885SJiri Olsa perf_evlist__mmap_cb_get(struct perf_evlist *_evlist, bool overwrite, int idx)
777bb1b1885SJiri Olsa {
778bb1b1885SJiri Olsa 	struct evlist *evlist = container_of(_evlist, struct evlist, core);
7793805e4f3SJiri Olsa 	struct mmap *maps;
780bb1b1885SJiri Olsa 
7813805e4f3SJiri Olsa 	maps = overwrite ? evlist->overwrite_mmap : evlist->mmap;
782bb1b1885SJiri Olsa 
783bb1b1885SJiri Olsa 	if (!maps) {
7843805e4f3SJiri Olsa 		maps = evlist__alloc_mmap(evlist, overwrite);
785bb1b1885SJiri Olsa 		if (!maps)
786bb1b1885SJiri Olsa 			return NULL;
787bb1b1885SJiri Olsa 
7883805e4f3SJiri Olsa 		if (overwrite) {
789bb1b1885SJiri Olsa 			evlist->overwrite_mmap = maps;
790bb1b1885SJiri Olsa 			if (evlist->bkw_mmap_state == BKW_MMAP_NOTREADY)
791ade9d208SArnaldo Carvalho de Melo 				evlist__toggle_bkw_mmap(evlist, BKW_MMAP_RUNNING);
7923805e4f3SJiri Olsa 		} else {
7933805e4f3SJiri Olsa 			evlist->mmap = maps;
794bb1b1885SJiri Olsa 		}
795bb1b1885SJiri Olsa 	}
796bb1b1885SJiri Olsa 
797bb1b1885SJiri Olsa 	return &maps[idx].core;
798bb1b1885SJiri Olsa }
799bb1b1885SJiri Olsa 
800b80132b1SJiri Olsa static int
801b80132b1SJiri Olsa perf_evlist__mmap_cb_mmap(struct perf_mmap *_map, struct perf_mmap_param *_mp,
802b80132b1SJiri Olsa 			  int output, int cpu)
803b80132b1SJiri Olsa {
804b80132b1SJiri Olsa 	struct mmap *map = container_of(_map, struct mmap, core);
805b80132b1SJiri Olsa 	struct mmap_params *mp = container_of(_mp, struct mmap_params, core);
806b80132b1SJiri Olsa 
807b80132b1SJiri Olsa 	return mmap__mmap(map, mp, output, cpu);
808b80132b1SJiri Olsa }
809b80132b1SJiri Olsa 
810f5e7150cSArnaldo Carvalho de Melo unsigned long perf_event_mlock_kb_in_pages(void)
811994a1f78SJiri Olsa {
812f5e7150cSArnaldo Carvalho de Melo 	unsigned long pages;
8138185e881SArnaldo Carvalho de Melo 	int max;
8148185e881SArnaldo Carvalho de Melo 
8158185e881SArnaldo Carvalho de Melo 	if (sysctl__read_int("kernel/perf_event_mlock_kb", &max) < 0) {
8168185e881SArnaldo Carvalho de Melo 		/*
8178185e881SArnaldo Carvalho de Melo 		 * Pick a once upon a time good value, i.e. things look
8188185e881SArnaldo Carvalho de Melo 		 * strange since we can't read a sysctl value, but lets not
8198185e881SArnaldo Carvalho de Melo 		 * die yet...
8208185e881SArnaldo Carvalho de Melo 		 */
8218185e881SArnaldo Carvalho de Melo 		max = 512;
8228185e881SArnaldo Carvalho de Melo 	} else {
8238185e881SArnaldo Carvalho de Melo 		max -= (page_size / 1024);
8248185e881SArnaldo Carvalho de Melo 	}
8258185e881SArnaldo Carvalho de Melo 
8268185e881SArnaldo Carvalho de Melo 	pages = (max * 1024) / page_size;
8271be300f4SArnaldo Carvalho de Melo 	if (!is_power_of_2(pages))
8281be300f4SArnaldo Carvalho de Melo 		pages = rounddown_pow_of_two(pages);
829f5e7150cSArnaldo Carvalho de Melo 
830f5e7150cSArnaldo Carvalho de Melo 	return pages;
831f5e7150cSArnaldo Carvalho de Melo }
832f5e7150cSArnaldo Carvalho de Melo 
8339521b5f2SJiri Olsa size_t evlist__mmap_size(unsigned long pages)
834f5e7150cSArnaldo Carvalho de Melo {
835f5e7150cSArnaldo Carvalho de Melo 	if (pages == UINT_MAX)
836f5e7150cSArnaldo Carvalho de Melo 		pages = perf_event_mlock_kb_in_pages();
837f5e7150cSArnaldo Carvalho de Melo 	else if (!is_power_of_2(pages))
838994a1f78SJiri Olsa 		return 0;
839994a1f78SJiri Olsa 
840994a1f78SJiri Olsa 	return (pages + 1) * page_size;
841994a1f78SJiri Olsa }
842994a1f78SJiri Olsa 
84333c2dcfdSDavid Ahern static long parse_pages_arg(const char *str, unsigned long min,
84433c2dcfdSDavid Ahern 			    unsigned long max)
845994a1f78SJiri Olsa {
8462fbe4abeSAdrian Hunter 	unsigned long pages, val;
84727050f53SJiri Olsa 	static struct parse_tag tags[] = {
84827050f53SJiri Olsa 		{ .tag  = 'B', .mult = 1       },
84927050f53SJiri Olsa 		{ .tag  = 'K', .mult = 1 << 10 },
85027050f53SJiri Olsa 		{ .tag  = 'M', .mult = 1 << 20 },
85127050f53SJiri Olsa 		{ .tag  = 'G', .mult = 1 << 30 },
85227050f53SJiri Olsa 		{ .tag  = 0 },
85327050f53SJiri Olsa 	};
854994a1f78SJiri Olsa 
8558973504bSDavid Ahern 	if (str == NULL)
85633c2dcfdSDavid Ahern 		return -EINVAL;
8578973504bSDavid Ahern 
85827050f53SJiri Olsa 	val = parse_tag_value(str, tags);
8592fbe4abeSAdrian Hunter 	if (val != (unsigned long) -1) {
86027050f53SJiri Olsa 		/* we got file size value */
86127050f53SJiri Olsa 		pages = PERF_ALIGN(val, page_size) / page_size;
86227050f53SJiri Olsa 	} else {
86327050f53SJiri Olsa 		/* we got pages count value */
86427050f53SJiri Olsa 		char *eptr;
865994a1f78SJiri Olsa 		pages = strtoul(str, &eptr, 10);
86633c2dcfdSDavid Ahern 		if (*eptr != '\0')
86733c2dcfdSDavid Ahern 			return -EINVAL;
86827050f53SJiri Olsa 	}
869994a1f78SJiri Olsa 
8702bcab6c1SAdrian Hunter 	if (pages == 0 && min == 0) {
87133c2dcfdSDavid Ahern 		/* leave number of pages at 0 */
8721dbfa938SAdrian Hunter 	} else if (!is_power_of_2(pages)) {
8739808143bSJiri Olsa 		char buf[100];
8749808143bSJiri Olsa 
87533c2dcfdSDavid Ahern 		/* round pages up to next power of 2 */
87691529834SArnaldo Carvalho de Melo 		pages = roundup_pow_of_two(pages);
8771dbfa938SAdrian Hunter 		if (!pages)
8781dbfa938SAdrian Hunter 			return -EINVAL;
8799808143bSJiri Olsa 
8809808143bSJiri Olsa 		unit_number__scnprintf(buf, sizeof(buf), pages * page_size);
8819808143bSJiri Olsa 		pr_info("rounding mmap pages size to %s (%lu pages)\n",
8829808143bSJiri Olsa 			buf, pages);
8832fbe4abeSAdrian Hunter 	}
8842fbe4abeSAdrian Hunter 
88533c2dcfdSDavid Ahern 	if (pages > max)
88633c2dcfdSDavid Ahern 		return -EINVAL;
88733c2dcfdSDavid Ahern 
88833c2dcfdSDavid Ahern 	return pages;
88933c2dcfdSDavid Ahern }
89033c2dcfdSDavid Ahern 
89125f84702SArnaldo Carvalho de Melo int __evlist__parse_mmap_pages(unsigned int *mmap_pages, const char *str)
89233c2dcfdSDavid Ahern {
89333c2dcfdSDavid Ahern 	unsigned long max = UINT_MAX;
89433c2dcfdSDavid Ahern 	long pages;
89533c2dcfdSDavid Ahern 
896f5ae9c42SAdrian Hunter 	if (max > SIZE_MAX / page_size)
89733c2dcfdSDavid Ahern 		max = SIZE_MAX / page_size;
89833c2dcfdSDavid Ahern 
89933c2dcfdSDavid Ahern 	pages = parse_pages_arg(str, 1, max);
90033c2dcfdSDavid Ahern 	if (pages < 0) {
90133c2dcfdSDavid Ahern 		pr_err("Invalid argument for --mmap_pages/-m\n");
902994a1f78SJiri Olsa 		return -1;
903994a1f78SJiri Olsa 	}
904994a1f78SJiri Olsa 
905994a1f78SJiri Olsa 	*mmap_pages = pages;
906994a1f78SJiri Olsa 	return 0;
907994a1f78SJiri Olsa }
908994a1f78SJiri Olsa 
90925f84702SArnaldo Carvalho de Melo int evlist__parse_mmap_pages(const struct option *opt, const char *str, int unset __maybe_unused)
910e9db1310SAdrian Hunter {
91125f84702SArnaldo Carvalho de Melo 	return __evlist__parse_mmap_pages(opt->value, str);
912e9db1310SAdrian Hunter }
913e9db1310SAdrian Hunter 
914c83fa7f2SAdrian Hunter /**
9159521b5f2SJiri Olsa  * evlist__mmap_ex - Create mmaps to receive events.
916c83fa7f2SAdrian Hunter  * @evlist: list of events
917c83fa7f2SAdrian Hunter  * @pages: map length in pages
918c83fa7f2SAdrian Hunter  * @overwrite: overwrite older events?
919718c602dSAdrian Hunter  * @auxtrace_pages - auxtrace map length in pages
920718c602dSAdrian Hunter  * @auxtrace_overwrite - overwrite older auxtrace data?
921f8a95309SArnaldo Carvalho de Melo  *
922c83fa7f2SAdrian Hunter  * If @overwrite is %false the user needs to signal event consumption using
9239521b5f2SJiri Olsa  * perf_mmap__write_tail().  Using evlist__mmap_read() does this
924c83fa7f2SAdrian Hunter  * automatically.
925f8a95309SArnaldo Carvalho de Melo  *
926718c602dSAdrian Hunter  * Similarly, if @auxtrace_overwrite is %false the user needs to signal data
927718c602dSAdrian Hunter  * consumption using auxtrace_mmap__write_tail().
928718c602dSAdrian Hunter  *
929c83fa7f2SAdrian Hunter  * Return: %0 on success, negative error code otherwise.
930f8a95309SArnaldo Carvalho de Melo  */
9319521b5f2SJiri Olsa int evlist__mmap_ex(struct evlist *evlist, unsigned int pages,
9327a276ff6SWang Nan 			 unsigned int auxtrace_pages,
93351255a8aSAlexey Budankov 			 bool auxtrace_overwrite, int nr_cblocks, int affinity, int flush,
93451255a8aSAlexey Budankov 			 int comp_level)
935f8a95309SArnaldo Carvalho de Melo {
93671f566a3SWang Nan 	/*
93771f566a3SWang Nan 	 * Delay setting mp.prot: set it before calling perf_mmap__mmap.
93871f566a3SWang Nan 	 * Its value is decided by evsel's write_backward.
93971f566a3SWang Nan 	 * So &mp should not be passed through const pointer.
94071f566a3SWang Nan 	 */
941e440979fSJiri Olsa 	struct mmap_params mp = {
942e440979fSJiri Olsa 		.nr_cblocks	= nr_cblocks,
943e440979fSJiri Olsa 		.affinity	= affinity,
944e440979fSJiri Olsa 		.flush		= flush,
945e440979fSJiri Olsa 		.comp_level	= comp_level
946e440979fSJiri Olsa 	};
947923d0f18SJiri Olsa 	struct perf_evlist_mmap_ops ops = {
9489abd2ab2SJiri Olsa 		.idx  = perf_evlist__mmap_cb_idx,
949bb1b1885SJiri Olsa 		.get  = perf_evlist__mmap_cb_get,
950b80132b1SJiri Olsa 		.mmap = perf_evlist__mmap_cb_mmap,
9519abd2ab2SJiri Olsa 	};
95250a682ceSArnaldo Carvalho de Melo 
953f6fa4375SJiri Olsa 	evlist->core.mmap_len = evlist__mmap_size(pages);
954f6fa4375SJiri Olsa 	pr_debug("mmap size %zuB\n", evlist->core.mmap_len);
955f8a95309SArnaldo Carvalho de Melo 
956f6fa4375SJiri Olsa 	auxtrace_mmap_params__init(&mp.auxtrace_mp, evlist->core.mmap_len,
957718c602dSAdrian Hunter 				   auxtrace_pages, auxtrace_overwrite);
958718c602dSAdrian Hunter 
959923d0f18SJiri Olsa 	return perf_evlist__mmap_ops(&evlist->core, &ops, &mp.core);
960f8a95309SArnaldo Carvalho de Melo }
9617e2ed097SArnaldo Carvalho de Melo 
9629521b5f2SJiri Olsa int evlist__mmap(struct evlist *evlist, unsigned int pages)
963718c602dSAdrian Hunter {
9649521b5f2SJiri Olsa 	return evlist__mmap_ex(evlist, pages, 0, false, 0, PERF_AFFINITY_SYS, 1, 0);
965718c602dSAdrian Hunter }
966718c602dSAdrian Hunter 
9677748bb71SArnaldo Carvalho de Melo int evlist__create_maps(struct evlist *evlist, struct target *target)
9687e2ed097SArnaldo Carvalho de Melo {
969147c508fSJin Yao 	bool all_threads = (target->per_thread && target->system_wide);
970f854839bSJiri Olsa 	struct perf_cpu_map *cpus;
9719749b90eSJiri Olsa 	struct perf_thread_map *threads;
9727e2ed097SArnaldo Carvalho de Melo 
973147c508fSJin Yao 	/*
974147c508fSJin Yao 	 * If specify '-a' and '--per-thread' to perf record, perf record
975147c508fSJin Yao 	 * will override '--per-thread'. target->per_thread = false and
976147c508fSJin Yao 	 * target->system_wide = true.
977147c508fSJin Yao 	 *
978147c508fSJin Yao 	 * If specify '--per-thread' only to perf record,
979147c508fSJin Yao 	 * target->per_thread = true and target->system_wide = false.
980147c508fSJin Yao 	 *
981147c508fSJin Yao 	 * So target->per_thread && target->system_wide is false.
982147c508fSJin Yao 	 * For perf record, thread_map__new_str doesn't call
983147c508fSJin Yao 	 * thread_map__new_all_cpus. That will keep perf record's
984147c508fSJin Yao 	 * current behavior.
985147c508fSJin Yao 	 *
986147c508fSJin Yao 	 * For perf stat, it allows the case that target->per_thread and
987147c508fSJin Yao 	 * target->system_wide are all true. It means to collect system-wide
988147c508fSJin Yao 	 * per-thread data. thread_map__new_str will call
989147c508fSJin Yao 	 * thread_map__new_all_cpus to enumerate all threads.
990147c508fSJin Yao 	 */
99173c0ca1eSJin Yao 	threads = thread_map__new_str(target->pid, target->tid, target->uid,
992147c508fSJin Yao 				      all_threads);
99374bfd2b2SAdrian Hunter 
99474bfd2b2SAdrian Hunter 	if (!threads)
9957e2ed097SArnaldo Carvalho de Melo 		return -1;
9967e2ed097SArnaldo Carvalho de Melo 
9979c105fbcSDongsheng Yang 	if (target__uses_dummy_map(target))
998397721e0SJiri Olsa 		cpus = perf_cpu_map__dummy_new();
999879d77d0SNamhyung Kim 	else
10009c3516d1SJiri Olsa 		cpus = perf_cpu_map__new(target->cpu_list);
10017e2ed097SArnaldo Carvalho de Melo 
100274bfd2b2SAdrian Hunter 	if (!cpus)
10037e2ed097SArnaldo Carvalho de Melo 		goto out_delete_threads;
10047e2ed097SArnaldo Carvalho de Melo 
1005ec903f26SJiri Olsa 	evlist->core.has_user_cpus = !!target->cpu_list;
1006ec9a77a7SAdrian Hunter 
1007453fa030SJiri Olsa 	perf_evlist__set_maps(&evlist->core, cpus, threads);
1008d5bc056eSAdrian Hunter 
1009bfd1b83dSNamhyung Kim 	/* as evlist now has references, put count here */
1010bfd1b83dSNamhyung Kim 	perf_cpu_map__put(cpus);
1011bfd1b83dSNamhyung Kim 	perf_thread_map__put(threads);
1012bfd1b83dSNamhyung Kim 
1013d5bc056eSAdrian Hunter 	return 0;
10147e2ed097SArnaldo Carvalho de Melo 
10157e2ed097SArnaldo Carvalho de Melo out_delete_threads:
10167836e52eSJiri Olsa 	perf_thread_map__put(threads);
10177e2ed097SArnaldo Carvalho de Melo 	return -1;
10187e2ed097SArnaldo Carvalho de Melo }
10197e2ed097SArnaldo Carvalho de Melo 
102024bf91a7SArnaldo Carvalho de Melo int evlist__apply_filters(struct evlist *evlist, struct evsel **err_evsel)
10210a102479SFrederic Weisbecker {
102232dcd021SJiri Olsa 	struct evsel *evsel;
1023745cefc5SArnaldo Carvalho de Melo 	int err = 0;
10240a102479SFrederic Weisbecker 
1025e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, evsel) {
1026745cefc5SArnaldo Carvalho de Melo 		if (evsel->filter == NULL)
10270a102479SFrederic Weisbecker 			continue;
1028745cefc5SArnaldo Carvalho de Melo 
1029d988d5eeSKan Liang 		/*
1030d988d5eeSKan Liang 		 * filters only work for tracepoint event, which doesn't have cpu limit.
1031d988d5eeSKan Liang 		 * So evlist and evsel should always be same.
1032d988d5eeSKan Liang 		 */
1033a00571fdSJiri Olsa 		err = perf_evsel__apply_filter(&evsel->core, evsel->filter);
103423d4aad4SArnaldo Carvalho de Melo 		if (err) {
103523d4aad4SArnaldo Carvalho de Melo 			*err_evsel = evsel;
1036745cefc5SArnaldo Carvalho de Melo 			break;
10370a102479SFrederic Weisbecker 		}
103823d4aad4SArnaldo Carvalho de Melo 	}
10390a102479SFrederic Weisbecker 
1040745cefc5SArnaldo Carvalho de Melo 	return err;
1041745cefc5SArnaldo Carvalho de Melo }
1042745cefc5SArnaldo Carvalho de Melo 
104324bf91a7SArnaldo Carvalho de Melo int evlist__set_tp_filter(struct evlist *evlist, const char *filter)
1044745cefc5SArnaldo Carvalho de Melo {
104532dcd021SJiri Olsa 	struct evsel *evsel;
1046745cefc5SArnaldo Carvalho de Melo 	int err = 0;
1047745cefc5SArnaldo Carvalho de Melo 
104805cea449SArnaldo Carvalho de Melo 	if (filter == NULL)
104905cea449SArnaldo Carvalho de Melo 		return -1;
105005cea449SArnaldo Carvalho de Melo 
1051e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, evsel) {
10521fc632ceSJiri Olsa 		if (evsel->core.attr.type != PERF_TYPE_TRACEPOINT)
1053fdf14720SWang Nan 			continue;
1054fdf14720SWang Nan 
1055ad681adfSArnaldo Carvalho de Melo 		err = evsel__set_filter(evsel, filter);
1056745cefc5SArnaldo Carvalho de Melo 		if (err)
1057745cefc5SArnaldo Carvalho de Melo 			break;
1058745cefc5SArnaldo Carvalho de Melo 	}
1059745cefc5SArnaldo Carvalho de Melo 
1060745cefc5SArnaldo Carvalho de Melo 	return err;
10610a102479SFrederic Weisbecker }
106274429964SFrederic Weisbecker 
106324bf91a7SArnaldo Carvalho de Melo int evlist__append_tp_filter(struct evlist *evlist, const char *filter)
106453c92f73SArnaldo Carvalho de Melo {
106553c92f73SArnaldo Carvalho de Melo 	struct evsel *evsel;
106653c92f73SArnaldo Carvalho de Melo 	int err = 0;
106753c92f73SArnaldo Carvalho de Melo 
106853c92f73SArnaldo Carvalho de Melo 	if (filter == NULL)
106953c92f73SArnaldo Carvalho de Melo 		return -1;
107053c92f73SArnaldo Carvalho de Melo 
107153c92f73SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, evsel) {
107253c92f73SArnaldo Carvalho de Melo 		if (evsel->core.attr.type != PERF_TYPE_TRACEPOINT)
107353c92f73SArnaldo Carvalho de Melo 			continue;
107453c92f73SArnaldo Carvalho de Melo 
1075ad681adfSArnaldo Carvalho de Melo 		err = evsel__append_tp_filter(evsel, filter);
107653c92f73SArnaldo Carvalho de Melo 		if (err)
107753c92f73SArnaldo Carvalho de Melo 			break;
107853c92f73SArnaldo Carvalho de Melo 	}
107953c92f73SArnaldo Carvalho de Melo 
108053c92f73SArnaldo Carvalho de Melo 	return err;
108153c92f73SArnaldo Carvalho de Melo }
108253c92f73SArnaldo Carvalho de Melo 
1083da949f50SArnaldo Carvalho de Melo char *asprintf__tp_filter_pids(size_t npids, pid_t *pids)
1084cfd70a26SArnaldo Carvalho de Melo {
1085cfd70a26SArnaldo Carvalho de Melo 	char *filter;
1086be199adaSArnaldo Carvalho de Melo 	size_t i;
1087cfd70a26SArnaldo Carvalho de Melo 
1088be199adaSArnaldo Carvalho de Melo 	for (i = 0; i < npids; ++i) {
1089be199adaSArnaldo Carvalho de Melo 		if (i == 0) {
1090be199adaSArnaldo Carvalho de Melo 			if (asprintf(&filter, "common_pid != %d", pids[i]) < 0)
109105cea449SArnaldo Carvalho de Melo 				return NULL;
1092be199adaSArnaldo Carvalho de Melo 		} else {
1093be199adaSArnaldo Carvalho de Melo 			char *tmp;
1094be199adaSArnaldo Carvalho de Melo 
1095be199adaSArnaldo Carvalho de Melo 			if (asprintf(&tmp, "%s && common_pid != %d", filter, pids[i]) < 0)
1096be199adaSArnaldo Carvalho de Melo 				goto out_free;
1097be199adaSArnaldo Carvalho de Melo 
1098be199adaSArnaldo Carvalho de Melo 			free(filter);
1099be199adaSArnaldo Carvalho de Melo 			filter = tmp;
1100be199adaSArnaldo Carvalho de Melo 		}
1101be199adaSArnaldo Carvalho de Melo 	}
1102cfd70a26SArnaldo Carvalho de Melo 
110305cea449SArnaldo Carvalho de Melo 	return filter;
1104be199adaSArnaldo Carvalho de Melo out_free:
1105cfd70a26SArnaldo Carvalho de Melo 	free(filter);
110605cea449SArnaldo Carvalho de Melo 	return NULL;
110705cea449SArnaldo Carvalho de Melo }
110805cea449SArnaldo Carvalho de Melo 
110924bf91a7SArnaldo Carvalho de Melo int evlist__set_tp_filter_pids(struct evlist *evlist, size_t npids, pid_t *pids)
111005cea449SArnaldo Carvalho de Melo {
111105cea449SArnaldo Carvalho de Melo 	char *filter = asprintf__tp_filter_pids(npids, pids);
111224bf91a7SArnaldo Carvalho de Melo 	int ret = evlist__set_tp_filter(evlist, filter);
111305cea449SArnaldo Carvalho de Melo 
111405cea449SArnaldo Carvalho de Melo 	free(filter);
1115cfd70a26SArnaldo Carvalho de Melo 	return ret;
1116cfd70a26SArnaldo Carvalho de Melo }
1117cfd70a26SArnaldo Carvalho de Melo 
111824bf91a7SArnaldo Carvalho de Melo int evlist__set_tp_filter_pid(struct evlist *evlist, pid_t pid)
1119be199adaSArnaldo Carvalho de Melo {
112024bf91a7SArnaldo Carvalho de Melo 	return evlist__set_tp_filter_pids(evlist, 1, &pid);
1121be199adaSArnaldo Carvalho de Melo }
1122be199adaSArnaldo Carvalho de Melo 
112324bf91a7SArnaldo Carvalho de Melo int evlist__append_tp_filter_pids(struct evlist *evlist, size_t npids, pid_t *pids)
11241827ab5bSArnaldo Carvalho de Melo {
11251827ab5bSArnaldo Carvalho de Melo 	char *filter = asprintf__tp_filter_pids(npids, pids);
112624bf91a7SArnaldo Carvalho de Melo 	int ret = evlist__append_tp_filter(evlist, filter);
11271827ab5bSArnaldo Carvalho de Melo 
11281827ab5bSArnaldo Carvalho de Melo 	free(filter);
11291827ab5bSArnaldo Carvalho de Melo 	return ret;
11301827ab5bSArnaldo Carvalho de Melo }
11311827ab5bSArnaldo Carvalho de Melo 
113224bf91a7SArnaldo Carvalho de Melo int evlist__append_tp_filter_pid(struct evlist *evlist, pid_t pid)
11331827ab5bSArnaldo Carvalho de Melo {
113424bf91a7SArnaldo Carvalho de Melo 	return evlist__append_tp_filter_pids(evlist, 1, &pid);
11351827ab5bSArnaldo Carvalho de Melo }
11361827ab5bSArnaldo Carvalho de Melo 
1137b3c2cc2bSArnaldo Carvalho de Melo bool evlist__valid_sample_type(struct evlist *evlist)
113874429964SFrederic Weisbecker {
113932dcd021SJiri Olsa 	struct evsel *pos;
1140c2a70653SArnaldo Carvalho de Melo 
11416484d2f9SJiri Olsa 	if (evlist->core.nr_entries == 1)
114275562573SAdrian Hunter 		return true;
114375562573SAdrian Hunter 
114475562573SAdrian Hunter 	if (evlist->id_pos < 0 || evlist->is_pos < 0)
114575562573SAdrian Hunter 		return false;
114675562573SAdrian Hunter 
1147e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, pos) {
114875562573SAdrian Hunter 		if (pos->id_pos != evlist->id_pos ||
114975562573SAdrian Hunter 		    pos->is_pos != evlist->is_pos)
1150c2a70653SArnaldo Carvalho de Melo 			return false;
115174429964SFrederic Weisbecker 	}
115274429964SFrederic Weisbecker 
1153c2a70653SArnaldo Carvalho de Melo 	return true;
1154c2a70653SArnaldo Carvalho de Melo }
1155c2a70653SArnaldo Carvalho de Melo 
1156b3c2cc2bSArnaldo Carvalho de Melo u64 __evlist__combined_sample_type(struct evlist *evlist)
1157c2a70653SArnaldo Carvalho de Melo {
115832dcd021SJiri Olsa 	struct evsel *evsel;
115975562573SAdrian Hunter 
116075562573SAdrian Hunter 	if (evlist->combined_sample_type)
116175562573SAdrian Hunter 		return evlist->combined_sample_type;
116275562573SAdrian Hunter 
1163e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, evsel)
11641fc632ceSJiri Olsa 		evlist->combined_sample_type |= evsel->core.attr.sample_type;
116575562573SAdrian Hunter 
116675562573SAdrian Hunter 	return evlist->combined_sample_type;
116775562573SAdrian Hunter }
116875562573SAdrian Hunter 
1169b3c2cc2bSArnaldo Carvalho de Melo u64 evlist__combined_sample_type(struct evlist *evlist)
117075562573SAdrian Hunter {
117175562573SAdrian Hunter 	evlist->combined_sample_type = 0;
1172b3c2cc2bSArnaldo Carvalho de Melo 	return __evlist__combined_sample_type(evlist);
1173c2a70653SArnaldo Carvalho de Melo }
1174c2a70653SArnaldo Carvalho de Melo 
117592c7d7cdSArnaldo Carvalho de Melo u64 evlist__combined_branch_type(struct evlist *evlist)
117698df858eSAndi Kleen {
117732dcd021SJiri Olsa 	struct evsel *evsel;
117898df858eSAndi Kleen 	u64 branch_type = 0;
117998df858eSAndi Kleen 
1180e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, evsel)
11811fc632ceSJiri Olsa 		branch_type |= evsel->core.attr.branch_sample_type;
118298df858eSAndi Kleen 	return branch_type;
118398df858eSAndi Kleen }
118498df858eSAndi Kleen 
118578e1bc25SArnaldo Carvalho de Melo bool evlist__valid_read_format(struct evlist *evlist)
11869ede473cSJiri Olsa {
1187515dbe48SJiri Olsa 	struct evsel *first = evlist__first(evlist), *pos = first;
11881fc632ceSJiri Olsa 	u64 read_format = first->core.attr.read_format;
11891fc632ceSJiri Olsa 	u64 sample_type = first->core.attr.sample_type;
11909ede473cSJiri Olsa 
1191e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, pos) {
119294d3820fSAdrian Hunter 		if (read_format != pos->core.attr.read_format) {
119394d3820fSAdrian Hunter 			pr_debug("Read format differs %#" PRIx64 " vs %#" PRIx64 "\n",
119494d3820fSAdrian Hunter 				 read_format, (u64)pos->core.attr.read_format);
119594d3820fSAdrian Hunter 		}
11969ede473cSJiri Olsa 	}
11979ede473cSJiri Olsa 
11989ede473cSJiri Olsa 	/* PERF_SAMPLE_READ imples PERF_FORMAT_ID. */
11999ede473cSJiri Olsa 	if ((sample_type & PERF_SAMPLE_READ) &&
12009ede473cSJiri Olsa 	    !(read_format & PERF_FORMAT_ID)) {
12019ede473cSJiri Olsa 		return false;
12029ede473cSJiri Olsa 	}
12039ede473cSJiri Olsa 
12049ede473cSJiri Olsa 	return true;
12059ede473cSJiri Olsa }
12069ede473cSJiri Olsa 
12071420ba2fSArnaldo Carvalho de Melo u16 evlist__id_hdr_size(struct evlist *evlist)
120881e36bffSArnaldo Carvalho de Melo {
1209515dbe48SJiri Olsa 	struct evsel *first = evlist__first(evlist);
121081e36bffSArnaldo Carvalho de Melo 	struct perf_sample *data;
121181e36bffSArnaldo Carvalho de Melo 	u64 sample_type;
121281e36bffSArnaldo Carvalho de Melo 	u16 size = 0;
121381e36bffSArnaldo Carvalho de Melo 
12141fc632ceSJiri Olsa 	if (!first->core.attr.sample_id_all)
121581e36bffSArnaldo Carvalho de Melo 		goto out;
121681e36bffSArnaldo Carvalho de Melo 
12171fc632ceSJiri Olsa 	sample_type = first->core.attr.sample_type;
121881e36bffSArnaldo Carvalho de Melo 
121981e36bffSArnaldo Carvalho de Melo 	if (sample_type & PERF_SAMPLE_TID)
122081e36bffSArnaldo Carvalho de Melo 		size += sizeof(data->tid) * 2;
122181e36bffSArnaldo Carvalho de Melo 
122281e36bffSArnaldo Carvalho de Melo        if (sample_type & PERF_SAMPLE_TIME)
122381e36bffSArnaldo Carvalho de Melo 		size += sizeof(data->time);
122481e36bffSArnaldo Carvalho de Melo 
122581e36bffSArnaldo Carvalho de Melo 	if (sample_type & PERF_SAMPLE_ID)
122681e36bffSArnaldo Carvalho de Melo 		size += sizeof(data->id);
122781e36bffSArnaldo Carvalho de Melo 
122881e36bffSArnaldo Carvalho de Melo 	if (sample_type & PERF_SAMPLE_STREAM_ID)
122981e36bffSArnaldo Carvalho de Melo 		size += sizeof(data->stream_id);
123081e36bffSArnaldo Carvalho de Melo 
123181e36bffSArnaldo Carvalho de Melo 	if (sample_type & PERF_SAMPLE_CPU)
123281e36bffSArnaldo Carvalho de Melo 		size += sizeof(data->cpu) * 2;
123375562573SAdrian Hunter 
123475562573SAdrian Hunter 	if (sample_type & PERF_SAMPLE_IDENTIFIER)
123575562573SAdrian Hunter 		size += sizeof(data->id);
123681e36bffSArnaldo Carvalho de Melo out:
123781e36bffSArnaldo Carvalho de Melo 	return size;
123881e36bffSArnaldo Carvalho de Melo }
123981e36bffSArnaldo Carvalho de Melo 
12408cedf3a5SArnaldo Carvalho de Melo bool evlist__valid_sample_id_all(struct evlist *evlist)
1241c2a70653SArnaldo Carvalho de Melo {
1242515dbe48SJiri Olsa 	struct evsel *first = evlist__first(evlist), *pos = first;
1243c2a70653SArnaldo Carvalho de Melo 
1244e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry_continue(evlist, pos) {
12451fc632ceSJiri Olsa 		if (first->core.attr.sample_id_all != pos->core.attr.sample_id_all)
1246c2a70653SArnaldo Carvalho de Melo 			return false;
1247c2a70653SArnaldo Carvalho de Melo 	}
1248c2a70653SArnaldo Carvalho de Melo 
1249c2a70653SArnaldo Carvalho de Melo 	return true;
125074429964SFrederic Weisbecker }
125174429964SFrederic Weisbecker 
12528cedf3a5SArnaldo Carvalho de Melo bool evlist__sample_id_all(struct evlist *evlist)
125374429964SFrederic Weisbecker {
1254515dbe48SJiri Olsa 	struct evsel *first = evlist__first(evlist);
12551fc632ceSJiri Olsa 	return first->core.attr.sample_id_all;
125674429964SFrederic Weisbecker }
125781cce8deSArnaldo Carvalho de Melo 
1258900c8eadSArnaldo Carvalho de Melo void evlist__set_selected(struct evlist *evlist, struct evsel *evsel)
125981cce8deSArnaldo Carvalho de Melo {
126081cce8deSArnaldo Carvalho de Melo 	evlist->selected = evsel;
126181cce8deSArnaldo Carvalho de Melo }
1262727ab04eSArnaldo Carvalho de Melo 
1263750b4edeSJiri Olsa void evlist__close(struct evlist *evlist)
1264a74b4b66SNamhyung Kim {
126532dcd021SJiri Olsa 	struct evsel *evsel;
12667736627bSAndi Kleen 	struct affinity affinity;
12677736627bSAndi Kleen 	int cpu, i;
1268a74b4b66SNamhyung Kim 
12697736627bSAndi Kleen 	/*
12707736627bSAndi Kleen 	 * With perf record core.cpus is usually NULL.
12717736627bSAndi Kleen 	 * Use the old method to handle this for now.
12727736627bSAndi Kleen 	 */
12737736627bSAndi Kleen 	if (!evlist->core.cpus) {
1274475fb533SAndi Kleen 		evlist__for_each_entry_reverse(evlist, evsel)
127588761fa1SJiri Olsa 			evsel__close(evsel);
12767736627bSAndi Kleen 		return;
12777736627bSAndi Kleen 	}
12787736627bSAndi Kleen 
12797736627bSAndi Kleen 	if (affinity__setup(&affinity) < 0)
12807736627bSAndi Kleen 		return;
12817736627bSAndi Kleen 	evlist__for_each_cpu(evlist, i, cpu) {
12827736627bSAndi Kleen 		affinity__set(&affinity, cpu);
12837736627bSAndi Kleen 
12847736627bSAndi Kleen 		evlist__for_each_entry_reverse(evlist, evsel) {
12857736627bSAndi Kleen 			if (evsel__cpu_iter_skip(evsel, cpu))
12867736627bSAndi Kleen 			    continue;
12877736627bSAndi Kleen 			perf_evsel__close_cpu(&evsel->core, evsel->cpu_iter - 1);
12887736627bSAndi Kleen 		}
12897736627bSAndi Kleen 	}
12907736627bSAndi Kleen 	affinity__cleanup(&affinity);
12917736627bSAndi Kleen 	evlist__for_each_entry_reverse(evlist, evsel) {
12927736627bSAndi Kleen 		perf_evsel__free_fd(&evsel->core);
12937736627bSAndi Kleen 		perf_evsel__free_id(&evsel->core);
12947736627bSAndi Kleen 	}
1295a74b4b66SNamhyung Kim }
1296a74b4b66SNamhyung Kim 
12977748bb71SArnaldo Carvalho de Melo static int evlist__create_syswide_maps(struct evlist *evlist)
12984112eb18SArnaldo Carvalho de Melo {
1299f854839bSJiri Olsa 	struct perf_cpu_map *cpus;
13009749b90eSJiri Olsa 	struct perf_thread_map *threads;
13014112eb18SArnaldo Carvalho de Melo 	int err = -ENOMEM;
13024112eb18SArnaldo Carvalho de Melo 
13034112eb18SArnaldo Carvalho de Melo 	/*
13044112eb18SArnaldo Carvalho de Melo 	 * Try reading /sys/devices/system/cpu/online to get
13054112eb18SArnaldo Carvalho de Melo 	 * an all cpus map.
13064112eb18SArnaldo Carvalho de Melo 	 *
13074112eb18SArnaldo Carvalho de Melo 	 * FIXME: -ENOMEM is the best we can do here, the cpu_map
13084112eb18SArnaldo Carvalho de Melo 	 * code needs an overhaul to properly forward the
13094112eb18SArnaldo Carvalho de Melo 	 * error, and we may not want to do that fallback to a
13104112eb18SArnaldo Carvalho de Melo 	 * default cpu identity map :-\
13114112eb18SArnaldo Carvalho de Melo 	 */
13129c3516d1SJiri Olsa 	cpus = perf_cpu_map__new(NULL);
13138c0498b6SAdrian Hunter 	if (!cpus)
13144112eb18SArnaldo Carvalho de Melo 		goto out;
13154112eb18SArnaldo Carvalho de Melo 
13164b49cce2SJiri Olsa 	threads = perf_thread_map__new_dummy();
13178c0498b6SAdrian Hunter 	if (!threads)
13188c0498b6SAdrian Hunter 		goto out_put;
13194112eb18SArnaldo Carvalho de Melo 
1320453fa030SJiri Olsa 	perf_evlist__set_maps(&evlist->core, cpus, threads);
1321bfd1b83dSNamhyung Kim 
1322bfd1b83dSNamhyung Kim 	perf_thread_map__put(threads);
13238c0498b6SAdrian Hunter out_put:
132438f01d8dSJiri Olsa 	perf_cpu_map__put(cpus);
1325bfd1b83dSNamhyung Kim out:
1326bfd1b83dSNamhyung Kim 	return err;
13274112eb18SArnaldo Carvalho de Melo }
13284112eb18SArnaldo Carvalho de Melo 
1329474ddc4cSJiri Olsa int evlist__open(struct evlist *evlist)
1330727ab04eSArnaldo Carvalho de Melo {
133132dcd021SJiri Olsa 	struct evsel *evsel;
1332a74b4b66SNamhyung Kim 	int err;
1333727ab04eSArnaldo Carvalho de Melo 
13344112eb18SArnaldo Carvalho de Melo 	/*
13354112eb18SArnaldo Carvalho de Melo 	 * Default: one fd per CPU, all threads, aka systemwide
13364112eb18SArnaldo Carvalho de Melo 	 * as sys_perf_event_open(cpu = -1, thread = -1) is EINVAL
13374112eb18SArnaldo Carvalho de Melo 	 */
133803617c22SJiri Olsa 	if (evlist->core.threads == NULL && evlist->core.cpus == NULL) {
13397748bb71SArnaldo Carvalho de Melo 		err = evlist__create_syswide_maps(evlist);
13404112eb18SArnaldo Carvalho de Melo 		if (err < 0)
13414112eb18SArnaldo Carvalho de Melo 			goto out_err;
13424112eb18SArnaldo Carvalho de Melo 	}
13434112eb18SArnaldo Carvalho de Melo 
13440a7e7ec9SArnaldo Carvalho de Melo 	evlist__update_id_pos(evlist);
1345733cd2feSAdrian Hunter 
1346e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, evsel) {
1347af663bd0SJiri Olsa 		err = evsel__open(evsel, evsel->core.cpus, evsel->core.threads);
1348727ab04eSArnaldo Carvalho de Melo 		if (err < 0)
1349727ab04eSArnaldo Carvalho de Melo 			goto out_err;
1350727ab04eSArnaldo Carvalho de Melo 	}
1351727ab04eSArnaldo Carvalho de Melo 
1352727ab04eSArnaldo Carvalho de Melo 	return 0;
1353727ab04eSArnaldo Carvalho de Melo out_err:
1354750b4edeSJiri Olsa 	evlist__close(evlist);
135541c21a68SNamhyung Kim 	errno = -err;
1356727ab04eSArnaldo Carvalho de Melo 	return err;
1357727ab04eSArnaldo Carvalho de Melo }
135835b9d88eSArnaldo Carvalho de Melo 
13597b392ef0SArnaldo Carvalho de Melo int evlist__prepare_workload(struct evlist *evlist, struct target *target, const char *argv[],
13607b392ef0SArnaldo Carvalho de Melo 			     bool pipe_output, void (*exec_error)(int signo, siginfo_t *info, void *ucontext))
136135b9d88eSArnaldo Carvalho de Melo {
136235b9d88eSArnaldo Carvalho de Melo 	int child_ready_pipe[2], go_pipe[2];
136335b9d88eSArnaldo Carvalho de Melo 	char bf;
136435b9d88eSArnaldo Carvalho de Melo 
136535b9d88eSArnaldo Carvalho de Melo 	if (pipe(child_ready_pipe) < 0) {
136635b9d88eSArnaldo Carvalho de Melo 		perror("failed to create 'ready' pipe");
136735b9d88eSArnaldo Carvalho de Melo 		return -1;
136835b9d88eSArnaldo Carvalho de Melo 	}
136935b9d88eSArnaldo Carvalho de Melo 
137035b9d88eSArnaldo Carvalho de Melo 	if (pipe(go_pipe) < 0) {
137135b9d88eSArnaldo Carvalho de Melo 		perror("failed to create 'go' pipe");
137235b9d88eSArnaldo Carvalho de Melo 		goto out_close_ready_pipe;
137335b9d88eSArnaldo Carvalho de Melo 	}
137435b9d88eSArnaldo Carvalho de Melo 
137535b9d88eSArnaldo Carvalho de Melo 	evlist->workload.pid = fork();
137635b9d88eSArnaldo Carvalho de Melo 	if (evlist->workload.pid < 0) {
137735b9d88eSArnaldo Carvalho de Melo 		perror("failed to fork");
137835b9d88eSArnaldo Carvalho de Melo 		goto out_close_pipes;
137935b9d88eSArnaldo Carvalho de Melo 	}
138035b9d88eSArnaldo Carvalho de Melo 
138135b9d88eSArnaldo Carvalho de Melo 	if (!evlist->workload.pid) {
13825f1c4225SArnaldo Carvalho de Melo 		int ret;
13835f1c4225SArnaldo Carvalho de Melo 
1384119fa3c9SNamhyung Kim 		if (pipe_output)
138535b9d88eSArnaldo Carvalho de Melo 			dup2(2, 1);
138635b9d88eSArnaldo Carvalho de Melo 
13870817df08SDavid Ahern 		signal(SIGTERM, SIG_DFL);
13880817df08SDavid Ahern 
138935b9d88eSArnaldo Carvalho de Melo 		close(child_ready_pipe[0]);
139035b9d88eSArnaldo Carvalho de Melo 		close(go_pipe[1]);
139135b9d88eSArnaldo Carvalho de Melo 		fcntl(go_pipe[0], F_SETFD, FD_CLOEXEC);
139235b9d88eSArnaldo Carvalho de Melo 
139335b9d88eSArnaldo Carvalho de Melo 		/*
139435b9d88eSArnaldo Carvalho de Melo 		 * Tell the parent we're ready to go
139535b9d88eSArnaldo Carvalho de Melo 		 */
139635b9d88eSArnaldo Carvalho de Melo 		close(child_ready_pipe[1]);
139735b9d88eSArnaldo Carvalho de Melo 
139835b9d88eSArnaldo Carvalho de Melo 		/*
139935b9d88eSArnaldo Carvalho de Melo 		 * Wait until the parent tells us to go.
140035b9d88eSArnaldo Carvalho de Melo 		 */
14015f1c4225SArnaldo Carvalho de Melo 		ret = read(go_pipe[0], &bf, 1);
14025f1c4225SArnaldo Carvalho de Melo 		/*
14035f1c4225SArnaldo Carvalho de Melo 		 * The parent will ask for the execvp() to be performed by
14045f1c4225SArnaldo Carvalho de Melo 		 * writing exactly one byte, in workload.cork_fd, usually via
14057b392ef0SArnaldo Carvalho de Melo 		 * evlist__start_workload().
14065f1c4225SArnaldo Carvalho de Melo 		 *
140720f86fc1SArnaldo Carvalho de Melo 		 * For cancelling the workload without actually running it,
14085f1c4225SArnaldo Carvalho de Melo 		 * the parent will just close workload.cork_fd, without writing
14095f1c4225SArnaldo Carvalho de Melo 		 * anything, i.e. read will return zero and we just exit()
14105f1c4225SArnaldo Carvalho de Melo 		 * here.
14115f1c4225SArnaldo Carvalho de Melo 		 */
14125f1c4225SArnaldo Carvalho de Melo 		if (ret != 1) {
14135f1c4225SArnaldo Carvalho de Melo 			if (ret == -1)
141435b9d88eSArnaldo Carvalho de Melo 				perror("unable to read pipe");
14155f1c4225SArnaldo Carvalho de Melo 			exit(ret);
14165f1c4225SArnaldo Carvalho de Melo 		}
141735b9d88eSArnaldo Carvalho de Melo 
141835b9d88eSArnaldo Carvalho de Melo 		execvp(argv[0], (char **)argv);
141935b9d88eSArnaldo Carvalho de Melo 
1420735f7e0bSArnaldo Carvalho de Melo 		if (exec_error) {
1421f33cbe72SArnaldo Carvalho de Melo 			union sigval val;
1422f33cbe72SArnaldo Carvalho de Melo 
1423f33cbe72SArnaldo Carvalho de Melo 			val.sival_int = errno;
1424f33cbe72SArnaldo Carvalho de Melo 			if (sigqueue(getppid(), SIGUSR1, val))
142535b9d88eSArnaldo Carvalho de Melo 				perror(argv[0]);
1426f33cbe72SArnaldo Carvalho de Melo 		} else
1427f33cbe72SArnaldo Carvalho de Melo 			perror(argv[0]);
142835b9d88eSArnaldo Carvalho de Melo 		exit(-1);
142935b9d88eSArnaldo Carvalho de Melo 	}
143035b9d88eSArnaldo Carvalho de Melo 
1431735f7e0bSArnaldo Carvalho de Melo 	if (exec_error) {
1432735f7e0bSArnaldo Carvalho de Melo 		struct sigaction act = {
1433735f7e0bSArnaldo Carvalho de Melo 			.sa_flags     = SA_SIGINFO,
1434735f7e0bSArnaldo Carvalho de Melo 			.sa_sigaction = exec_error,
1435735f7e0bSArnaldo Carvalho de Melo 		};
1436735f7e0bSArnaldo Carvalho de Melo 		sigaction(SIGUSR1, &act, NULL);
1437735f7e0bSArnaldo Carvalho de Melo 	}
1438735f7e0bSArnaldo Carvalho de Melo 
14391aaf63b1SArnaldo Carvalho de Melo 	if (target__none(target)) {
144003617c22SJiri Olsa 		if (evlist->core.threads == NULL) {
14411aaf63b1SArnaldo Carvalho de Melo 			fprintf(stderr, "FATAL: evlist->threads need to be set at this point (%s:%d).\n",
14421aaf63b1SArnaldo Carvalho de Melo 				__func__, __LINE__);
14431aaf63b1SArnaldo Carvalho de Melo 			goto out_close_pipes;
14441aaf63b1SArnaldo Carvalho de Melo 		}
144503617c22SJiri Olsa 		perf_thread_map__set_pid(evlist->core.threads, 0, evlist->workload.pid);
14461aaf63b1SArnaldo Carvalho de Melo 	}
144735b9d88eSArnaldo Carvalho de Melo 
144835b9d88eSArnaldo Carvalho de Melo 	close(child_ready_pipe[1]);
144935b9d88eSArnaldo Carvalho de Melo 	close(go_pipe[0]);
145035b9d88eSArnaldo Carvalho de Melo 	/*
145135b9d88eSArnaldo Carvalho de Melo 	 * wait for child to settle
145235b9d88eSArnaldo Carvalho de Melo 	 */
145335b9d88eSArnaldo Carvalho de Melo 	if (read(child_ready_pipe[0], &bf, 1) == -1) {
145435b9d88eSArnaldo Carvalho de Melo 		perror("unable to read pipe");
145535b9d88eSArnaldo Carvalho de Melo 		goto out_close_pipes;
145635b9d88eSArnaldo Carvalho de Melo 	}
145735b9d88eSArnaldo Carvalho de Melo 
1458bcf3145fSNamhyung Kim 	fcntl(go_pipe[1], F_SETFD, FD_CLOEXEC);
145935b9d88eSArnaldo Carvalho de Melo 	evlist->workload.cork_fd = go_pipe[1];
146035b9d88eSArnaldo Carvalho de Melo 	close(child_ready_pipe[0]);
146135b9d88eSArnaldo Carvalho de Melo 	return 0;
146235b9d88eSArnaldo Carvalho de Melo 
146335b9d88eSArnaldo Carvalho de Melo out_close_pipes:
146435b9d88eSArnaldo Carvalho de Melo 	close(go_pipe[0]);
146535b9d88eSArnaldo Carvalho de Melo 	close(go_pipe[1]);
146635b9d88eSArnaldo Carvalho de Melo out_close_ready_pipe:
146735b9d88eSArnaldo Carvalho de Melo 	close(child_ready_pipe[0]);
146835b9d88eSArnaldo Carvalho de Melo 	close(child_ready_pipe[1]);
146935b9d88eSArnaldo Carvalho de Melo 	return -1;
147035b9d88eSArnaldo Carvalho de Melo }
147135b9d88eSArnaldo Carvalho de Melo 
14727b392ef0SArnaldo Carvalho de Melo int evlist__start_workload(struct evlist *evlist)
147335b9d88eSArnaldo Carvalho de Melo {
147435b9d88eSArnaldo Carvalho de Melo 	if (evlist->workload.cork_fd > 0) {
1475b3824404SDavid Ahern 		char bf = 0;
1476bcf3145fSNamhyung Kim 		int ret;
147735b9d88eSArnaldo Carvalho de Melo 		/*
147835b9d88eSArnaldo Carvalho de Melo 		 * Remove the cork, let it rip!
147935b9d88eSArnaldo Carvalho de Melo 		 */
1480bcf3145fSNamhyung Kim 		ret = write(evlist->workload.cork_fd, &bf, 1);
1481bcf3145fSNamhyung Kim 		if (ret < 0)
1482e978be9eSSoramichi Akiyama 			perror("unable to write to pipe");
1483bcf3145fSNamhyung Kim 
1484bcf3145fSNamhyung Kim 		close(evlist->workload.cork_fd);
1485bcf3145fSNamhyung Kim 		return ret;
148635b9d88eSArnaldo Carvalho de Melo 	}
148735b9d88eSArnaldo Carvalho de Melo 
148835b9d88eSArnaldo Carvalho de Melo 	return 0;
148935b9d88eSArnaldo Carvalho de Melo }
1490cb0b29e0SArnaldo Carvalho de Melo 
14912a6599cdSArnaldo Carvalho de Melo int evlist__parse_sample(struct evlist *evlist, union perf_event *event, struct perf_sample *sample)
1492cb0b29e0SArnaldo Carvalho de Melo {
14933ccf8a7bSArnaldo Carvalho de Melo 	struct evsel *evsel = evlist__event2evsel(evlist, event);
149475562573SAdrian Hunter 
149575562573SAdrian Hunter 	if (!evsel)
149675562573SAdrian Hunter 		return -EFAULT;
14976b6017a2SArnaldo Carvalho de Melo 	return evsel__parse_sample(evsel, event, sample);
1498cb0b29e0SArnaldo Carvalho de Melo }
149978f067b3SArnaldo Carvalho de Melo 
15002a6599cdSArnaldo Carvalho de Melo int evlist__parse_sample_timestamp(struct evlist *evlist, union perf_event *event, u64 *timestamp)
150101468120SJiri Olsa {
15023ccf8a7bSArnaldo Carvalho de Melo 	struct evsel *evsel = evlist__event2evsel(evlist, event);
150301468120SJiri Olsa 
150401468120SJiri Olsa 	if (!evsel)
150501468120SJiri Olsa 		return -EFAULT;
15066b6017a2SArnaldo Carvalho de Melo 	return evsel__parse_sample_timestamp(evsel, event, timestamp);
150701468120SJiri Olsa }
150801468120SJiri Olsa 
1509d1f249ecSArnaldo Carvalho de Melo int evlist__strerror_open(struct evlist *evlist, int err, char *buf, size_t size)
1510a8f23d8fSArnaldo Carvalho de Melo {
1511a8f23d8fSArnaldo Carvalho de Melo 	int printed, value;
1512c8b5f2c9SArnaldo Carvalho de Melo 	char sbuf[STRERR_BUFSIZE], *emsg = str_error_r(err, sbuf, sizeof(sbuf));
1513a8f23d8fSArnaldo Carvalho de Melo 
1514a8f23d8fSArnaldo Carvalho de Melo 	switch (err) {
1515a8f23d8fSArnaldo Carvalho de Melo 	case EACCES:
1516a8f23d8fSArnaldo Carvalho de Melo 	case EPERM:
1517a8f23d8fSArnaldo Carvalho de Melo 		printed = scnprintf(buf, size,
1518a8f23d8fSArnaldo Carvalho de Melo 				    "Error:\t%s.\n"
1519a8f23d8fSArnaldo Carvalho de Melo 				    "Hint:\tCheck /proc/sys/kernel/perf_event_paranoid setting.", emsg);
1520a8f23d8fSArnaldo Carvalho de Melo 
15211a47245dSAdrian Hunter 		value = perf_event_paranoid();
1522a8f23d8fSArnaldo Carvalho de Melo 
1523a8f23d8fSArnaldo Carvalho de Melo 		printed += scnprintf(buf + printed, size - printed, "\nHint:\t");
1524a8f23d8fSArnaldo Carvalho de Melo 
1525a8f23d8fSArnaldo Carvalho de Melo 		if (value >= 2) {
1526a8f23d8fSArnaldo Carvalho de Melo 			printed += scnprintf(buf + printed, size - printed,
1527a8f23d8fSArnaldo Carvalho de Melo 					     "For your workloads it needs to be <= 1\nHint:\t");
1528a8f23d8fSArnaldo Carvalho de Melo 		}
1529a8f23d8fSArnaldo Carvalho de Melo 		printed += scnprintf(buf + printed, size - printed,
15305229e366SArnaldo Carvalho de Melo 				     "For system wide tracing it needs to be set to -1.\n");
1531a8f23d8fSArnaldo Carvalho de Melo 
1532a8f23d8fSArnaldo Carvalho de Melo 		printed += scnprintf(buf + printed, size - printed,
15335229e366SArnaldo Carvalho de Melo 				    "Hint:\tTry: 'sudo sh -c \"echo -1 > /proc/sys/kernel/perf_event_paranoid\"'\n"
15345229e366SArnaldo Carvalho de Melo 				    "Hint:\tThe current value is %d.", value);
1535a8f23d8fSArnaldo Carvalho de Melo 		break;
1536d9aade7fSArnaldo Carvalho de Melo 	case EINVAL: {
1537515dbe48SJiri Olsa 		struct evsel *first = evlist__first(evlist);
1538d9aade7fSArnaldo Carvalho de Melo 		int max_freq;
1539d9aade7fSArnaldo Carvalho de Melo 
1540d9aade7fSArnaldo Carvalho de Melo 		if (sysctl__read_int("kernel/perf_event_max_sample_rate", &max_freq) < 0)
1541d9aade7fSArnaldo Carvalho de Melo 			goto out_default;
1542d9aade7fSArnaldo Carvalho de Melo 
15431fc632ceSJiri Olsa 		if (first->core.attr.sample_freq < (u64)max_freq)
1544d9aade7fSArnaldo Carvalho de Melo 			goto out_default;
1545d9aade7fSArnaldo Carvalho de Melo 
1546d9aade7fSArnaldo Carvalho de Melo 		printed = scnprintf(buf, size,
1547d9aade7fSArnaldo Carvalho de Melo 				    "Error:\t%s.\n"
1548d9aade7fSArnaldo Carvalho de Melo 				    "Hint:\tCheck /proc/sys/kernel/perf_event_max_sample_rate.\n"
1549d9aade7fSArnaldo Carvalho de Melo 				    "Hint:\tThe current value is %d and %" PRIu64 " is being requested.",
15501fc632ceSJiri Olsa 				    emsg, max_freq, first->core.attr.sample_freq);
1551d9aade7fSArnaldo Carvalho de Melo 		break;
1552d9aade7fSArnaldo Carvalho de Melo 	}
1553a8f23d8fSArnaldo Carvalho de Melo 	default:
1554d9aade7fSArnaldo Carvalho de Melo out_default:
1555a8f23d8fSArnaldo Carvalho de Melo 		scnprintf(buf, size, "%s", emsg);
1556a8f23d8fSArnaldo Carvalho de Melo 		break;
1557a8f23d8fSArnaldo Carvalho de Melo 	}
1558a8f23d8fSArnaldo Carvalho de Melo 
1559a8f23d8fSArnaldo Carvalho de Melo 	return 0;
1560a8f23d8fSArnaldo Carvalho de Melo }
1561a025e4f0SAdrian Hunter 
1562d1f249ecSArnaldo Carvalho de Melo int evlist__strerror_mmap(struct evlist *evlist, int err, char *buf, size_t size)
1563956fa571SArnaldo Carvalho de Melo {
1564c8b5f2c9SArnaldo Carvalho de Melo 	char sbuf[STRERR_BUFSIZE], *emsg = str_error_r(err, sbuf, sizeof(sbuf));
1565f6fa4375SJiri Olsa 	int pages_attempted = evlist->core.mmap_len / 1024, pages_max_per_user, printed = 0;
1566956fa571SArnaldo Carvalho de Melo 
1567956fa571SArnaldo Carvalho de Melo 	switch (err) {
1568956fa571SArnaldo Carvalho de Melo 	case EPERM:
1569e5d4a290SArnaldo Carvalho de Melo 		sysctl__read_int("kernel/perf_event_mlock_kb", &pages_max_per_user);
1570e965bea1SArnaldo Carvalho de Melo 		printed += scnprintf(buf + printed, size - printed,
1571e965bea1SArnaldo Carvalho de Melo 				     "Error:\t%s.\n"
1572956fa571SArnaldo Carvalho de Melo 				     "Hint:\tCheck /proc/sys/kernel/perf_event_mlock_kb (%d kB) setting.\n"
1573e965bea1SArnaldo Carvalho de Melo 				     "Hint:\tTried using %zd kB.\n",
1574e5d4a290SArnaldo Carvalho de Melo 				     emsg, pages_max_per_user, pages_attempted);
1575e965bea1SArnaldo Carvalho de Melo 
1576e965bea1SArnaldo Carvalho de Melo 		if (pages_attempted >= pages_max_per_user) {
1577e965bea1SArnaldo Carvalho de Melo 			printed += scnprintf(buf + printed, size - printed,
1578e965bea1SArnaldo Carvalho de Melo 					     "Hint:\tTry 'sudo sh -c \"echo %d > /proc/sys/kernel/perf_event_mlock_kb\"', or\n",
1579e965bea1SArnaldo Carvalho de Melo 					     pages_max_per_user + pages_attempted);
1580e965bea1SArnaldo Carvalho de Melo 		}
1581e965bea1SArnaldo Carvalho de Melo 
1582e965bea1SArnaldo Carvalho de Melo 		printed += scnprintf(buf + printed, size - printed,
1583e965bea1SArnaldo Carvalho de Melo 				     "Hint:\tTry using a smaller -m/--mmap-pages value.");
1584956fa571SArnaldo Carvalho de Melo 		break;
1585956fa571SArnaldo Carvalho de Melo 	default:
1586956fa571SArnaldo Carvalho de Melo 		scnprintf(buf, size, "%s", emsg);
1587956fa571SArnaldo Carvalho de Melo 		break;
1588956fa571SArnaldo Carvalho de Melo 	}
1589956fa571SArnaldo Carvalho de Melo 
1590956fa571SArnaldo Carvalho de Melo 	return 0;
1591956fa571SArnaldo Carvalho de Melo }
1592956fa571SArnaldo Carvalho de Melo 
1593e414fd1aSArnaldo Carvalho de Melo void evlist__to_front(struct evlist *evlist, struct evsel *move_evsel)
1594a025e4f0SAdrian Hunter {
159532dcd021SJiri Olsa 	struct evsel *evsel, *n;
1596a025e4f0SAdrian Hunter 	LIST_HEAD(move);
1597a025e4f0SAdrian Hunter 
1598515dbe48SJiri Olsa 	if (move_evsel == evlist__first(evlist))
1599a025e4f0SAdrian Hunter 		return;
1600a025e4f0SAdrian Hunter 
1601e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry_safe(evlist, n, evsel) {
1602a025e4f0SAdrian Hunter 		if (evsel->leader == move_evsel->leader)
1603b27c4eceSJiri Olsa 			list_move_tail(&evsel->core.node, &move);
1604a025e4f0SAdrian Hunter 	}
1605a025e4f0SAdrian Hunter 
1606ce9036a6SJiri Olsa 	list_splice(&move, &evlist->core.entries);
1607a025e4f0SAdrian Hunter }
160860b0896cSAdrian Hunter 
1609e80db255SArnaldo Carvalho de Melo struct evsel *evlist__get_tracking_event(struct evlist *evlist)
1610442ad225SAdrian Hunter {
1611442ad225SAdrian Hunter 	struct evsel *evsel;
1612442ad225SAdrian Hunter 
1613442ad225SAdrian Hunter 	evlist__for_each_entry(evlist, evsel) {
1614442ad225SAdrian Hunter 		if (evsel->tracking)
1615442ad225SAdrian Hunter 			return evsel;
1616442ad225SAdrian Hunter 	}
1617442ad225SAdrian Hunter 
1618442ad225SAdrian Hunter 	return evlist__first(evlist);
1619442ad225SAdrian Hunter }
1620442ad225SAdrian Hunter 
1621e80db255SArnaldo Carvalho de Melo void evlist__set_tracking_event(struct evlist *evlist, struct evsel *tracking_evsel)
162260b0896cSAdrian Hunter {
162332dcd021SJiri Olsa 	struct evsel *evsel;
162460b0896cSAdrian Hunter 
162560b0896cSAdrian Hunter 	if (tracking_evsel->tracking)
162660b0896cSAdrian Hunter 		return;
162760b0896cSAdrian Hunter 
1628e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, evsel) {
162960b0896cSAdrian Hunter 		if (evsel != tracking_evsel)
163060b0896cSAdrian Hunter 			evsel->tracking = false;
163160b0896cSAdrian Hunter 	}
163260b0896cSAdrian Hunter 
163360b0896cSAdrian Hunter 	tracking_evsel->tracking = true;
163460b0896cSAdrian Hunter }
16357630b3e2SWang Nan 
1636b02736f7SArnaldo Carvalho de Melo struct evsel *evlist__find_evsel_by_str(struct evlist *evlist, const char *str)
16377630b3e2SWang Nan {
163832dcd021SJiri Olsa 	struct evsel *evsel;
16397630b3e2SWang Nan 
1640e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, evsel) {
16417630b3e2SWang Nan 		if (!evsel->name)
16427630b3e2SWang Nan 			continue;
16437630b3e2SWang Nan 		if (strcmp(str, evsel->name) == 0)
16447630b3e2SWang Nan 			return evsel;
16457630b3e2SWang Nan 	}
16467630b3e2SWang Nan 
16477630b3e2SWang Nan 	return NULL;
16487630b3e2SWang Nan }
164954cc54deSWang Nan 
1650ade9d208SArnaldo Carvalho de Melo void evlist__toggle_bkw_mmap(struct evlist *evlist, enum bkw_mmap_state state)
165154cc54deSWang Nan {
165254cc54deSWang Nan 	enum bkw_mmap_state old_state = evlist->bkw_mmap_state;
165354cc54deSWang Nan 	enum action {
165454cc54deSWang Nan 		NONE,
165554cc54deSWang Nan 		PAUSE,
165654cc54deSWang Nan 		RESUME,
165754cc54deSWang Nan 	} action = NONE;
165854cc54deSWang Nan 
16590b72d69aSWang Nan 	if (!evlist->overwrite_mmap)
166054cc54deSWang Nan 		return;
166154cc54deSWang Nan 
166254cc54deSWang Nan 	switch (old_state) {
166354cc54deSWang Nan 	case BKW_MMAP_NOTREADY: {
166454cc54deSWang Nan 		if (state != BKW_MMAP_RUNNING)
1665dd8bd53aSLuis de Bethencourt 			goto state_err;
166654cc54deSWang Nan 		break;
166754cc54deSWang Nan 	}
166854cc54deSWang Nan 	case BKW_MMAP_RUNNING: {
166954cc54deSWang Nan 		if (state != BKW_MMAP_DATA_PENDING)
167054cc54deSWang Nan 			goto state_err;
167154cc54deSWang Nan 		action = PAUSE;
167254cc54deSWang Nan 		break;
167354cc54deSWang Nan 	}
167454cc54deSWang Nan 	case BKW_MMAP_DATA_PENDING: {
167554cc54deSWang Nan 		if (state != BKW_MMAP_EMPTY)
167654cc54deSWang Nan 			goto state_err;
167754cc54deSWang Nan 		break;
167854cc54deSWang Nan 	}
167954cc54deSWang Nan 	case BKW_MMAP_EMPTY: {
168054cc54deSWang Nan 		if (state != BKW_MMAP_RUNNING)
168154cc54deSWang Nan 			goto state_err;
168254cc54deSWang Nan 		action = RESUME;
168354cc54deSWang Nan 		break;
168454cc54deSWang Nan 	}
168554cc54deSWang Nan 	default:
168654cc54deSWang Nan 		WARN_ONCE(1, "Shouldn't get there\n");
168754cc54deSWang Nan 	}
168854cc54deSWang Nan 
168954cc54deSWang Nan 	evlist->bkw_mmap_state = state;
169054cc54deSWang Nan 
169154cc54deSWang Nan 	switch (action) {
169254cc54deSWang Nan 	case PAUSE:
16930a60b339SArnaldo Carvalho de Melo 		evlist__pause(evlist);
169454cc54deSWang Nan 		break;
169554cc54deSWang Nan 	case RESUME:
16960a60b339SArnaldo Carvalho de Melo 		evlist__resume(evlist);
169754cc54deSWang Nan 		break;
169854cc54deSWang Nan 	case NONE:
169954cc54deSWang Nan 	default:
170054cc54deSWang Nan 		break;
170154cc54deSWang Nan 	}
170254cc54deSWang Nan 
170354cc54deSWang Nan state_err:
170454cc54deSWang Nan 	return;
170554cc54deSWang Nan }
170607d6f446SArnaldo Carvalho de Melo 
170778e1bc25SArnaldo Carvalho de Melo bool evlist__exclude_kernel(struct evlist *evlist)
170807d6f446SArnaldo Carvalho de Melo {
170932dcd021SJiri Olsa 	struct evsel *evsel;
171007d6f446SArnaldo Carvalho de Melo 
171107d6f446SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, evsel) {
17121fc632ceSJiri Olsa 		if (!evsel->core.attr.exclude_kernel)
171307d6f446SArnaldo Carvalho de Melo 			return false;
171407d6f446SArnaldo Carvalho de Melo 	}
171507d6f446SArnaldo Carvalho de Melo 
171607d6f446SArnaldo Carvalho de Melo 	return true;
171707d6f446SArnaldo Carvalho de Melo }
1718e2bdbe80SJin Yao 
1719e2bdbe80SJin Yao /*
1720e2bdbe80SJin Yao  * Events in data file are not collect in groups, but we still want
1721e2bdbe80SJin Yao  * the group display. Set the artificial group and set the leader's
1722e2bdbe80SJin Yao  * forced_leader flag to notify the display code.
1723e2bdbe80SJin Yao  */
172464b4778bSArnaldo Carvalho de Melo void evlist__force_leader(struct evlist *evlist)
1725e2bdbe80SJin Yao {
1726e2bdbe80SJin Yao 	if (!evlist->nr_groups) {
1727515dbe48SJiri Olsa 		struct evsel *leader = evlist__first(evlist);
1728e2bdbe80SJin Yao 
1729a622eafaSArnaldo Carvalho de Melo 		evlist__set_leader(evlist);
1730e2bdbe80SJin Yao 		leader->forced_leader = true;
1731e2bdbe80SJin Yao 	}
1732e2bdbe80SJin Yao }
1733c3537fc2SAndi Kleen 
173464b4778bSArnaldo Carvalho de Melo struct evsel *evlist__reset_weak_group(struct evlist *evsel_list, struct evsel *evsel, bool close)
1735c3537fc2SAndi Kleen {
173632dcd021SJiri Olsa 	struct evsel *c2, *leader;
1737c3537fc2SAndi Kleen 	bool is_open = true;
1738c3537fc2SAndi Kleen 
1739c3537fc2SAndi Kleen 	leader = evsel->leader;
1740c3537fc2SAndi Kleen 	pr_debug("Weak group for %s/%d failed\n",
17415643b1a5SJiri Olsa 			leader->name, leader->core.nr_members);
1742c3537fc2SAndi Kleen 
1743c3537fc2SAndi Kleen 	/*
1744c3537fc2SAndi Kleen 	 * for_each_group_member doesn't work here because it doesn't
1745c3537fc2SAndi Kleen 	 * include the first entry.
1746c3537fc2SAndi Kleen 	 */
1747c3537fc2SAndi Kleen 	evlist__for_each_entry(evsel_list, c2) {
1748c3537fc2SAndi Kleen 		if (c2 == evsel)
1749c3537fc2SAndi Kleen 			is_open = false;
1750c3537fc2SAndi Kleen 		if (c2->leader == leader) {
17514804e011SAndi Kleen 			if (is_open && close)
17525a40e199SAndi Kleen 				perf_evsel__close(&c2->core);
1753c3537fc2SAndi Kleen 			c2->leader = c2;
17545643b1a5SJiri Olsa 			c2->core.nr_members = 0;
17554804e011SAndi Kleen 			/*
17564804e011SAndi Kleen 			 * Set this for all former members of the group
17574804e011SAndi Kleen 			 * to indicate they get reopened.
17584804e011SAndi Kleen 			 */
17594804e011SAndi Kleen 			c2->reset_group = true;
1760c3537fc2SAndi Kleen 		}
1761c3537fc2SAndi Kleen 	}
1762c3537fc2SAndi Kleen 	return leader;
1763c3537fc2SAndi Kleen }
1764ec886bf5SAlexey Budankov 
1765a8fcbd26SAdrian Hunter static int evlist__parse_control_fifo(const char *str, int *ctl_fd, int *ctl_fd_ack, bool *ctl_fd_close)
1766a8fcbd26SAdrian Hunter {
1767a8fcbd26SAdrian Hunter 	char *s, *p;
1768a8fcbd26SAdrian Hunter 	int ret = 0, fd;
1769a8fcbd26SAdrian Hunter 
1770a8fcbd26SAdrian Hunter 	if (strncmp(str, "fifo:", 5))
1771a8fcbd26SAdrian Hunter 		return -EINVAL;
1772a8fcbd26SAdrian Hunter 
1773a8fcbd26SAdrian Hunter 	str += 5;
1774a8fcbd26SAdrian Hunter 	if (!*str || *str == ',')
1775a8fcbd26SAdrian Hunter 		return -EINVAL;
1776a8fcbd26SAdrian Hunter 
1777a8fcbd26SAdrian Hunter 	s = strdup(str);
1778a8fcbd26SAdrian Hunter 	if (!s)
1779a8fcbd26SAdrian Hunter 		return -ENOMEM;
1780a8fcbd26SAdrian Hunter 
1781a8fcbd26SAdrian Hunter 	p = strchr(s, ',');
1782a8fcbd26SAdrian Hunter 	if (p)
1783a8fcbd26SAdrian Hunter 		*p = '\0';
1784a8fcbd26SAdrian Hunter 
1785a8fcbd26SAdrian Hunter 	/*
1786a8fcbd26SAdrian Hunter 	 * O_RDWR avoids POLLHUPs which is necessary to allow the other
1787a8fcbd26SAdrian Hunter 	 * end of a FIFO to be repeatedly opened and closed.
1788a8fcbd26SAdrian Hunter 	 */
1789a8fcbd26SAdrian Hunter 	fd = open(s, O_RDWR | O_NONBLOCK | O_CLOEXEC);
1790a8fcbd26SAdrian Hunter 	if (fd < 0) {
1791a8fcbd26SAdrian Hunter 		pr_err("Failed to open '%s'\n", s);
1792a8fcbd26SAdrian Hunter 		ret = -errno;
1793a8fcbd26SAdrian Hunter 		goto out_free;
1794a8fcbd26SAdrian Hunter 	}
1795a8fcbd26SAdrian Hunter 	*ctl_fd = fd;
1796a8fcbd26SAdrian Hunter 	*ctl_fd_close = true;
1797a8fcbd26SAdrian Hunter 
1798a8fcbd26SAdrian Hunter 	if (p && *++p) {
1799a8fcbd26SAdrian Hunter 		/* O_RDWR | O_NONBLOCK means the other end need not be open */
1800a8fcbd26SAdrian Hunter 		fd = open(p, O_RDWR | O_NONBLOCK | O_CLOEXEC);
1801a8fcbd26SAdrian Hunter 		if (fd < 0) {
1802a8fcbd26SAdrian Hunter 			pr_err("Failed to open '%s'\n", p);
1803a8fcbd26SAdrian Hunter 			ret = -errno;
1804a8fcbd26SAdrian Hunter 			goto out_free;
1805a8fcbd26SAdrian Hunter 		}
1806a8fcbd26SAdrian Hunter 		*ctl_fd_ack = fd;
1807a8fcbd26SAdrian Hunter 	}
1808a8fcbd26SAdrian Hunter 
1809a8fcbd26SAdrian Hunter out_free:
1810a8fcbd26SAdrian Hunter 	free(s);
1811a8fcbd26SAdrian Hunter 	return ret;
1812a8fcbd26SAdrian Hunter }
1813a8fcbd26SAdrian Hunter 
1814a8fcbd26SAdrian Hunter int evlist__parse_control(const char *str, int *ctl_fd, int *ctl_fd_ack, bool *ctl_fd_close)
18159864a66dSAdrian Hunter {
18169864a66dSAdrian Hunter 	char *comma = NULL, *endptr = NULL;
18179864a66dSAdrian Hunter 
1818a8fcbd26SAdrian Hunter 	*ctl_fd_close = false;
1819a8fcbd26SAdrian Hunter 
18209864a66dSAdrian Hunter 	if (strncmp(str, "fd:", 3))
1821a8fcbd26SAdrian Hunter 		return evlist__parse_control_fifo(str, ctl_fd, ctl_fd_ack, ctl_fd_close);
18229864a66dSAdrian Hunter 
18239864a66dSAdrian Hunter 	*ctl_fd = strtoul(&str[3], &endptr, 0);
18249864a66dSAdrian Hunter 	if (endptr == &str[3])
18259864a66dSAdrian Hunter 		return -EINVAL;
18269864a66dSAdrian Hunter 
18279864a66dSAdrian Hunter 	comma = strchr(str, ',');
18289864a66dSAdrian Hunter 	if (comma) {
18299864a66dSAdrian Hunter 		if (endptr != comma)
18309864a66dSAdrian Hunter 			return -EINVAL;
18319864a66dSAdrian Hunter 
18329864a66dSAdrian Hunter 		*ctl_fd_ack = strtoul(comma + 1, &endptr, 0);
18339864a66dSAdrian Hunter 		if (endptr == comma + 1 || *endptr != '\0')
18349864a66dSAdrian Hunter 			return -EINVAL;
18359864a66dSAdrian Hunter 	}
18369864a66dSAdrian Hunter 
18379864a66dSAdrian Hunter 	return 0;
18389864a66dSAdrian Hunter }
18399864a66dSAdrian Hunter 
1840ee7fe31eSAdrian Hunter void evlist__close_control(int ctl_fd, int ctl_fd_ack, bool *ctl_fd_close)
1841ee7fe31eSAdrian Hunter {
1842ee7fe31eSAdrian Hunter 	if (*ctl_fd_close) {
1843ee7fe31eSAdrian Hunter 		*ctl_fd_close = false;
1844ee7fe31eSAdrian Hunter 		close(ctl_fd);
1845ee7fe31eSAdrian Hunter 		if (ctl_fd_ack >= 0)
1846ee7fe31eSAdrian Hunter 			close(ctl_fd_ack);
1847ee7fe31eSAdrian Hunter 	}
1848ee7fe31eSAdrian Hunter }
1849ee7fe31eSAdrian Hunter 
1850ec886bf5SAlexey Budankov int evlist__initialize_ctlfd(struct evlist *evlist, int fd, int ack)
1851ec886bf5SAlexey Budankov {
1852ec886bf5SAlexey Budankov 	if (fd == -1) {
1853ec886bf5SAlexey Budankov 		pr_debug("Control descriptor is not initialized\n");
1854ec886bf5SAlexey Budankov 		return 0;
1855ec886bf5SAlexey Budankov 	}
1856ec886bf5SAlexey Budankov 
1857ec886bf5SAlexey Budankov 	evlist->ctl_fd.pos = perf_evlist__add_pollfd(&evlist->core, fd, NULL, POLLIN,
1858ec886bf5SAlexey Budankov 						     fdarray_flag__nonfilterable);
1859ec886bf5SAlexey Budankov 	if (evlist->ctl_fd.pos < 0) {
1860ec886bf5SAlexey Budankov 		evlist->ctl_fd.pos = -1;
1861ec886bf5SAlexey Budankov 		pr_err("Failed to add ctl fd entry: %m\n");
1862ec886bf5SAlexey Budankov 		return -1;
1863ec886bf5SAlexey Budankov 	}
1864ec886bf5SAlexey Budankov 
1865ec886bf5SAlexey Budankov 	evlist->ctl_fd.fd = fd;
1866ec886bf5SAlexey Budankov 	evlist->ctl_fd.ack = ack;
1867ec886bf5SAlexey Budankov 
1868ec886bf5SAlexey Budankov 	return 0;
1869ec886bf5SAlexey Budankov }
1870ec886bf5SAlexey Budankov 
1871ec886bf5SAlexey Budankov bool evlist__ctlfd_initialized(struct evlist *evlist)
1872ec886bf5SAlexey Budankov {
1873ec886bf5SAlexey Budankov 	return evlist->ctl_fd.pos >= 0;
1874ec886bf5SAlexey Budankov }
1875ec886bf5SAlexey Budankov 
1876ec886bf5SAlexey Budankov int evlist__finalize_ctlfd(struct evlist *evlist)
1877ec886bf5SAlexey Budankov {
1878ec886bf5SAlexey Budankov 	struct pollfd *entries = evlist->core.pollfd.entries;
1879ec886bf5SAlexey Budankov 
1880ec886bf5SAlexey Budankov 	if (!evlist__ctlfd_initialized(evlist))
1881ec886bf5SAlexey Budankov 		return 0;
1882ec886bf5SAlexey Budankov 
1883ec886bf5SAlexey Budankov 	entries[evlist->ctl_fd.pos].fd = -1;
1884ec886bf5SAlexey Budankov 	entries[evlist->ctl_fd.pos].events = 0;
1885ec886bf5SAlexey Budankov 	entries[evlist->ctl_fd.pos].revents = 0;
1886ec886bf5SAlexey Budankov 
1887ec886bf5SAlexey Budankov 	evlist->ctl_fd.pos = -1;
1888ec886bf5SAlexey Budankov 	evlist->ctl_fd.ack = -1;
1889ec886bf5SAlexey Budankov 	evlist->ctl_fd.fd = -1;
1890ec886bf5SAlexey Budankov 
1891ec886bf5SAlexey Budankov 	return 0;
1892ec886bf5SAlexey Budankov }
1893ec886bf5SAlexey Budankov 
1894ec886bf5SAlexey Budankov static int evlist__ctlfd_recv(struct evlist *evlist, enum evlist_ctl_cmd *cmd,
1895ec886bf5SAlexey Budankov 			      char *cmd_data, size_t data_size)
1896ec886bf5SAlexey Budankov {
1897ec886bf5SAlexey Budankov 	int err;
1898ec886bf5SAlexey Budankov 	char c;
1899ec886bf5SAlexey Budankov 	size_t bytes_read = 0;
1900ec886bf5SAlexey Budankov 
190140db8ff5SAdrian Hunter 	*cmd = EVLIST_CTL_CMD_UNSUPPORTED;
1902ec886bf5SAlexey Budankov 	memset(cmd_data, 0, data_size);
1903ec886bf5SAlexey Budankov 	data_size--;
1904ec886bf5SAlexey Budankov 
1905ec886bf5SAlexey Budankov 	do {
1906ec886bf5SAlexey Budankov 		err = read(evlist->ctl_fd.fd, &c, 1);
1907ec886bf5SAlexey Budankov 		if (err > 0) {
1908ec886bf5SAlexey Budankov 			if (c == '\n' || c == '\0')
1909ec886bf5SAlexey Budankov 				break;
1910ec886bf5SAlexey Budankov 			cmd_data[bytes_read++] = c;
1911ec886bf5SAlexey Budankov 			if (bytes_read == data_size)
1912ec886bf5SAlexey Budankov 				break;
191340db8ff5SAdrian Hunter 			continue;
191440db8ff5SAdrian Hunter 		} else if (err == -1) {
191540db8ff5SAdrian Hunter 			if (errno == EINTR)
191640db8ff5SAdrian Hunter 				continue;
191740db8ff5SAdrian Hunter 			if (errno == EAGAIN || errno == EWOULDBLOCK)
191840db8ff5SAdrian Hunter 				err = 0;
191940db8ff5SAdrian Hunter 			else
1920ec886bf5SAlexey Budankov 				pr_err("Failed to read from ctlfd %d: %m\n", evlist->ctl_fd.fd);
1921ec886bf5SAlexey Budankov 		}
192240db8ff5SAdrian Hunter 		break;
1923ec886bf5SAlexey Budankov 	} while (1);
1924ec886bf5SAlexey Budankov 
1925ec886bf5SAlexey Budankov 	pr_debug("Message from ctl_fd: \"%s%s\"\n", cmd_data,
1926ec886bf5SAlexey Budankov 		 bytes_read == data_size ? "" : c == '\n' ? "\\n" : "\\0");
1927ec886bf5SAlexey Budankov 
192840db8ff5SAdrian Hunter 	if (bytes_read > 0) {
1929ec886bf5SAlexey Budankov 		if (!strncmp(cmd_data, EVLIST_CTL_CMD_ENABLE_TAG,
1930ec886bf5SAlexey Budankov 			     (sizeof(EVLIST_CTL_CMD_ENABLE_TAG)-1))) {
1931ec886bf5SAlexey Budankov 			*cmd = EVLIST_CTL_CMD_ENABLE;
1932ec886bf5SAlexey Budankov 		} else if (!strncmp(cmd_data, EVLIST_CTL_CMD_DISABLE_TAG,
1933ec886bf5SAlexey Budankov 				    (sizeof(EVLIST_CTL_CMD_DISABLE_TAG)-1))) {
1934ec886bf5SAlexey Budankov 			*cmd = EVLIST_CTL_CMD_DISABLE;
1935d20aff15SAdrian Hunter 		} else if (!strncmp(cmd_data, EVLIST_CTL_CMD_SNAPSHOT_TAG,
1936d20aff15SAdrian Hunter 				    (sizeof(EVLIST_CTL_CMD_SNAPSHOT_TAG)-1))) {
1937d20aff15SAdrian Hunter 			*cmd = EVLIST_CTL_CMD_SNAPSHOT;
1938d20aff15SAdrian Hunter 			pr_debug("is snapshot\n");
1939ec886bf5SAlexey Budankov 		}
1940ec886bf5SAlexey Budankov 	}
1941ec886bf5SAlexey Budankov 
194240db8ff5SAdrian Hunter 	return bytes_read ? (int)bytes_read : err;
1943ec886bf5SAlexey Budankov }
1944ec886bf5SAlexey Budankov 
1945d20aff15SAdrian Hunter int evlist__ctlfd_ack(struct evlist *evlist)
1946ec886bf5SAlexey Budankov {
1947ec886bf5SAlexey Budankov 	int err;
1948ec886bf5SAlexey Budankov 
1949ec886bf5SAlexey Budankov 	if (evlist->ctl_fd.ack == -1)
1950ec886bf5SAlexey Budankov 		return 0;
1951ec886bf5SAlexey Budankov 
1952ec886bf5SAlexey Budankov 	err = write(evlist->ctl_fd.ack, EVLIST_CTL_CMD_ACK_TAG,
1953ec886bf5SAlexey Budankov 		    sizeof(EVLIST_CTL_CMD_ACK_TAG));
1954ec886bf5SAlexey Budankov 	if (err == -1)
1955ec886bf5SAlexey Budankov 		pr_err("failed to write to ctl_ack_fd %d: %m\n", evlist->ctl_fd.ack);
1956ec886bf5SAlexey Budankov 
1957ec886bf5SAlexey Budankov 	return err;
1958ec886bf5SAlexey Budankov }
1959ec886bf5SAlexey Budankov 
1960ec886bf5SAlexey Budankov int evlist__ctlfd_process(struct evlist *evlist, enum evlist_ctl_cmd *cmd)
1961ec886bf5SAlexey Budankov {
1962ec886bf5SAlexey Budankov 	int err = 0;
1963ec886bf5SAlexey Budankov 	char cmd_data[EVLIST_CTL_CMD_MAX_LEN];
1964ec886bf5SAlexey Budankov 	int ctlfd_pos = evlist->ctl_fd.pos;
1965ec886bf5SAlexey Budankov 	struct pollfd *entries = evlist->core.pollfd.entries;
1966ec886bf5SAlexey Budankov 
1967ec886bf5SAlexey Budankov 	if (!evlist__ctlfd_initialized(evlist) || !entries[ctlfd_pos].revents)
1968ec886bf5SAlexey Budankov 		return 0;
1969ec886bf5SAlexey Budankov 
1970ec886bf5SAlexey Budankov 	if (entries[ctlfd_pos].revents & POLLIN) {
1971ec886bf5SAlexey Budankov 		err = evlist__ctlfd_recv(evlist, cmd, cmd_data,
1972ec886bf5SAlexey Budankov 					 EVLIST_CTL_CMD_MAX_LEN);
1973ec886bf5SAlexey Budankov 		if (err > 0) {
1974ec886bf5SAlexey Budankov 			switch (*cmd) {
1975ec886bf5SAlexey Budankov 			case EVLIST_CTL_CMD_ENABLE:
1976ec886bf5SAlexey Budankov 				evlist__enable(evlist);
1977ec886bf5SAlexey Budankov 				break;
1978ec886bf5SAlexey Budankov 			case EVLIST_CTL_CMD_DISABLE:
1979ec886bf5SAlexey Budankov 				evlist__disable(evlist);
1980ec886bf5SAlexey Budankov 				break;
1981d20aff15SAdrian Hunter 			case EVLIST_CTL_CMD_SNAPSHOT:
1982d20aff15SAdrian Hunter 				break;
1983ec886bf5SAlexey Budankov 			case EVLIST_CTL_CMD_ACK:
1984ec886bf5SAlexey Budankov 			case EVLIST_CTL_CMD_UNSUPPORTED:
1985ec886bf5SAlexey Budankov 			default:
1986ec886bf5SAlexey Budankov 				pr_debug("ctlfd: unsupported %d\n", *cmd);
1987ec886bf5SAlexey Budankov 				break;
1988ec886bf5SAlexey Budankov 			}
1989d20aff15SAdrian Hunter 			if (!(*cmd == EVLIST_CTL_CMD_ACK || *cmd == EVLIST_CTL_CMD_UNSUPPORTED ||
1990d20aff15SAdrian Hunter 			      *cmd == EVLIST_CTL_CMD_SNAPSHOT))
1991ec886bf5SAlexey Budankov 				evlist__ctlfd_ack(evlist);
1992ec886bf5SAlexey Budankov 		}
1993ec886bf5SAlexey Budankov 	}
1994ec886bf5SAlexey Budankov 
1995ec886bf5SAlexey Budankov 	if (entries[ctlfd_pos].revents & (POLLHUP | POLLERR))
1996ec886bf5SAlexey Budankov 		evlist__finalize_ctlfd(evlist);
1997ec886bf5SAlexey Budankov 	else
1998ec886bf5SAlexey Budankov 		entries[ctlfd_pos].revents = 0;
1999ec886bf5SAlexey Budankov 
2000ec886bf5SAlexey Budankov 	return err;
2001ec886bf5SAlexey Budankov }
2002b214ba8cSNamhyung Kim 
2003b214ba8cSNamhyung Kim struct evsel *evlist__find_evsel(struct evlist *evlist, int idx)
2004b214ba8cSNamhyung Kim {
2005b214ba8cSNamhyung Kim 	struct evsel *evsel;
2006b214ba8cSNamhyung Kim 
2007b214ba8cSNamhyung Kim 	evlist__for_each_entry(evlist, evsel) {
2008b214ba8cSNamhyung Kim 		if (evsel->idx == idx)
2009b214ba8cSNamhyung Kim 			return evsel;
2010b214ba8cSNamhyung Kim 	}
2011b214ba8cSNamhyung Kim 	return NULL;
2012b214ba8cSNamhyung Kim }
2013