xref: /openbmc/linux/tools/perf/util/evlist.c (revision e16c2ce7)
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"
27142544a9SJiri Olsa #include "util/evsel_fprintf.h"
289607ad3aSArnaldo Carvalho de Melo #include <signal.h>
2935b9d88eSArnaldo Carvalho de Melo #include <unistd.h>
30b397f846SArnaldo Carvalho de Melo #include <sched.h>
31f2a39fe8SArnaldo Carvalho de Melo #include <stdlib.h>
32361c99a6SArnaldo Carvalho de Melo 
3350d08e47SArnaldo Carvalho de Melo #include "parse-events.h"
344b6ab94eSJosh Poimboeuf #include <subcmd/parse-options.h>
3550d08e47SArnaldo Carvalho de Melo 
36bafae98eSArnaldo Carvalho de Melo #include <fcntl.h>
3786a5e0c2SArnaldo Carvalho de Melo #include <sys/ioctl.h>
38f8a95309SArnaldo Carvalho de Melo #include <sys/mman.h>
39f8a95309SArnaldo Carvalho de Melo 
4070db7533SArnaldo Carvalho de Melo #include <linux/bitops.h>
4170db7533SArnaldo Carvalho de Melo #include <linux/hash.h>
420389cd1fSArnaldo Carvalho de Melo #include <linux/log2.h>
438dd2a131SJiri Olsa #include <linux/err.h>
448520a98dSArnaldo Carvalho de Melo #include <linux/string.h>
457f7c536fSArnaldo Carvalho de Melo #include <linux/zalloc.h>
464562a739SJiri Olsa #include <perf/evlist.h>
4788761fa1SJiri Olsa #include <perf/evsel.h>
489c3516d1SJiri Olsa #include <perf/cpumap.h>
497728fa0cSJiri Olsa #include <perf/mmap.h>
5070db7533SArnaldo Carvalho de Melo 
51e14e5497SArnaldo Carvalho de Melo #include <internal/xyarray.h>
52e14e5497SArnaldo Carvalho de Melo 
53748fe088SArnaldo Carvalho de Melo #ifdef LACKS_SIGQUEUE_PROTOTYPE
54748fe088SArnaldo Carvalho de Melo int sigqueue(pid_t pid, int sig, const union sigval value);
55748fe088SArnaldo Carvalho de Melo #endif
56748fe088SArnaldo Carvalho de Melo 
579dfcb759SJiri Olsa #define FD(e, x, y) (*(int *)xyarray__entry(e->core.fd, x, y))
588cd36f3eSJiri Olsa #define SID(e, x, y) xyarray__entry(e->core.sample_id, x, y)
59f8a95309SArnaldo Carvalho de Melo 
6052c86bcaSJiri Olsa void evlist__init(struct evlist *evlist, struct perf_cpu_map *cpus,
619749b90eSJiri Olsa 		  struct perf_thread_map *threads)
62361c99a6SArnaldo Carvalho de Melo {
634562a739SJiri Olsa 	perf_evlist__init(&evlist->core);
64453fa030SJiri Olsa 	perf_evlist__set_maps(&evlist->core, cpus, threads);
6535b9d88eSArnaldo Carvalho de Melo 	evlist->workload.pid = -1;
6654cc54deSWang Nan 	evlist->bkw_mmap_state = BKW_MMAP_NOTREADY;
678ab705b5SAlexey Budankov 	evlist->ctl_fd.fd = -1;
688ab705b5SAlexey Budankov 	evlist->ctl_fd.ack = -1;
698ab705b5SAlexey Budankov 	evlist->ctl_fd.pos = -1;
70361c99a6SArnaldo Carvalho de Melo }
71361c99a6SArnaldo Carvalho de Melo 
720f98b11cSJiri Olsa struct evlist *evlist__new(void)
73ef1d1af2SArnaldo Carvalho de Melo {
7463503dbaSJiri Olsa 	struct evlist *evlist = zalloc(sizeof(*evlist));
75ef1d1af2SArnaldo Carvalho de Melo 
76ef1d1af2SArnaldo Carvalho de Melo 	if (evlist != NULL)
7752c86bcaSJiri Olsa 		evlist__init(evlist, NULL, NULL);
78ef1d1af2SArnaldo Carvalho de Melo 
79361c99a6SArnaldo Carvalho de Melo 	return evlist;
80361c99a6SArnaldo Carvalho de Melo }
81361c99a6SArnaldo Carvalho de Melo 
82606e2c29SArnaldo Carvalho de Melo struct evlist *evlist__new_default(void)
83b22d54b0SJiri Olsa {
840f98b11cSJiri Olsa 	struct evlist *evlist = evlist__new();
85b22d54b0SJiri Olsa 
86e251abeeSArnaldo Carvalho de Melo 	if (evlist && evlist__add_default(evlist)) {
87c12995a5SJiri Olsa 		evlist__delete(evlist);
88b22d54b0SJiri Olsa 		evlist = NULL;
89b22d54b0SJiri Olsa 	}
90b22d54b0SJiri Olsa 
91b22d54b0SJiri Olsa 	return evlist;
92b22d54b0SJiri Olsa }
93b22d54b0SJiri Olsa 
94606e2c29SArnaldo Carvalho de Melo struct evlist *evlist__new_dummy(void)
955bae0250SArnaldo Carvalho de Melo {
960f98b11cSJiri Olsa 	struct evlist *evlist = evlist__new();
975bae0250SArnaldo Carvalho de Melo 
98e251abeeSArnaldo Carvalho de Melo 	if (evlist && evlist__add_dummy(evlist)) {
99c12995a5SJiri Olsa 		evlist__delete(evlist);
1005bae0250SArnaldo Carvalho de Melo 		evlist = NULL;
1015bae0250SArnaldo Carvalho de Melo 	}
1025bae0250SArnaldo Carvalho de Melo 
1035bae0250SArnaldo Carvalho de Melo 	return evlist;
1045bae0250SArnaldo Carvalho de Melo }
1055bae0250SArnaldo Carvalho de Melo 
10675562573SAdrian Hunter /**
1070a7e7ec9SArnaldo Carvalho de Melo  * evlist__set_id_pos - set the positions of event ids.
10875562573SAdrian Hunter  * @evlist: selected event list
10975562573SAdrian Hunter  *
11075562573SAdrian Hunter  * Events with compatible sample types all have the same id_pos
11175562573SAdrian Hunter  * and is_pos.  For convenience, put a copy on evlist.
11275562573SAdrian Hunter  */
1130a7e7ec9SArnaldo Carvalho de Melo void evlist__set_id_pos(struct evlist *evlist)
11475562573SAdrian Hunter {
115515dbe48SJiri Olsa 	struct evsel *first = evlist__first(evlist);
11675562573SAdrian Hunter 
11775562573SAdrian Hunter 	evlist->id_pos = first->id_pos;
11875562573SAdrian Hunter 	evlist->is_pos = first->is_pos;
11975562573SAdrian Hunter }
12075562573SAdrian Hunter 
1210a7e7ec9SArnaldo Carvalho de Melo static void evlist__update_id_pos(struct evlist *evlist)
122733cd2feSAdrian Hunter {
12332dcd021SJiri Olsa 	struct evsel *evsel;
124733cd2feSAdrian Hunter 
125e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, evsel)
1264b5e87b7SArnaldo Carvalho de Melo 		evsel__calc_id_pos(evsel);
127733cd2feSAdrian Hunter 
1280a7e7ec9SArnaldo Carvalho de Melo 	evlist__set_id_pos(evlist);
129733cd2feSAdrian Hunter }
130733cd2feSAdrian Hunter 
131e6b1878dSJiri Olsa static void evlist__purge(struct evlist *evlist)
132361c99a6SArnaldo Carvalho de Melo {
13332dcd021SJiri Olsa 	struct evsel *pos, *n;
134361c99a6SArnaldo Carvalho de Melo 
135e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry_safe(evlist, n, pos) {
136b27c4eceSJiri Olsa 		list_del_init(&pos->core.node);
137d49e4695SArnaldo Carvalho de Melo 		pos->evlist = NULL;
1385eb2dd2aSJiri Olsa 		evsel__delete(pos);
139361c99a6SArnaldo Carvalho de Melo 	}
140361c99a6SArnaldo Carvalho de Melo 
1416484d2f9SJiri Olsa 	evlist->core.nr_entries = 0;
142361c99a6SArnaldo Carvalho de Melo }
143361c99a6SArnaldo Carvalho de Melo 
144470579b0SJiri Olsa void evlist__exit(struct evlist *evlist)
145ef1d1af2SArnaldo Carvalho de Melo {
14604662523SArnaldo Carvalho de Melo 	zfree(&evlist->mmap);
1470b72d69aSWang Nan 	zfree(&evlist->overwrite_mmap);
14893dd6e28SJiri Olsa 	perf_evlist__exit(&evlist->core);
149ef1d1af2SArnaldo Carvalho de Melo }
150ef1d1af2SArnaldo Carvalho de Melo 
151c12995a5SJiri Olsa void evlist__delete(struct evlist *evlist)
152361c99a6SArnaldo Carvalho de Melo {
1530b04b3dcSArnaldo Carvalho de Melo 	if (evlist == NULL)
1540b04b3dcSArnaldo Carvalho de Melo 		return;
1550b04b3dcSArnaldo Carvalho de Melo 
156db6b7b13SJiri Olsa 	evlist__munmap(evlist);
157750b4edeSJiri Olsa 	evlist__close(evlist);
158e6b1878dSJiri Olsa 	evlist__purge(evlist);
159470579b0SJiri Olsa 	evlist__exit(evlist);
160361c99a6SArnaldo Carvalho de Melo 	free(evlist);
161361c99a6SArnaldo Carvalho de Melo }
162361c99a6SArnaldo Carvalho de Melo 
163a1cf3a75SJiri Olsa void evlist__add(struct evlist *evlist, struct evsel *entry)
164361c99a6SArnaldo Carvalho de Melo {
165d49e4695SArnaldo Carvalho de Melo 	entry->evlist = evlist;
1666484d2f9SJiri Olsa 	entry->idx = evlist->core.nr_entries;
16760b0896cSAdrian Hunter 	entry->tracking = !entry->idx;
168ef503831SArnaldo Carvalho de Melo 
1696484d2f9SJiri Olsa 	perf_evlist__add(&evlist->core, &entry->core);
1706484d2f9SJiri Olsa 
1716484d2f9SJiri Olsa 	if (evlist->core.nr_entries == 1)
1720a7e7ec9SArnaldo Carvalho de Melo 		evlist__set_id_pos(evlist);
173361c99a6SArnaldo Carvalho de Melo }
174361c99a6SArnaldo Carvalho de Melo 
17516251027SJiri Olsa void evlist__remove(struct evlist *evlist, struct evsel *evsel)
1764768230aSAdrian Hunter {
1774768230aSAdrian Hunter 	evsel->evlist = NULL;
17852e22fb8SJiri Olsa 	perf_evlist__remove(&evlist->core, &evsel->core);
1794768230aSAdrian Hunter }
1804768230aSAdrian Hunter 
181e414fd1aSArnaldo Carvalho de Melo void evlist__splice_list_tail(struct evlist *evlist, struct list_head *list)
18250d08e47SArnaldo Carvalho de Melo {
1836d2783feSJohn Garry 	while (!list_empty(list)) {
1846d2783feSJohn Garry 		struct evsel *evsel, *temp, *leader = NULL;
18575562573SAdrian Hunter 
186e5cadb93SArnaldo Carvalho de Melo 		__evlist__for_each_entry_safe(list, temp, evsel) {
187b27c4eceSJiri Olsa 			list_del_init(&evsel->core.node);
188a1cf3a75SJiri Olsa 			evlist__add(evlist, evsel);
1896d2783feSJohn Garry 			leader = evsel;
1906d2783feSJohn Garry 			break;
1916d2783feSJohn Garry 		}
1926d2783feSJohn Garry 
1936d2783feSJohn Garry 		__evlist__for_each_entry_safe(list, temp, evsel) {
1946d2783feSJohn Garry 			if (evsel->leader == leader) {
1956d2783feSJohn Garry 				list_del_init(&evsel->core.node);
1966d2783feSJohn Garry 				evlist__add(evlist, evsel);
1976d2783feSJohn Garry 			}
1986d2783feSJohn Garry 		}
199f114d6efSAdrian Hunter 	}
20050d08e47SArnaldo Carvalho de Melo }
20150d08e47SArnaldo Carvalho de Melo 
202c0e53476SArnaldo Carvalho de Melo int __evlist__set_tracepoints_handlers(struct evlist *evlist,
203c0e53476SArnaldo Carvalho de Melo 				       const struct evsel_str_handler *assocs, size_t nr_assocs)
204c0e53476SArnaldo Carvalho de Melo {
205c0e53476SArnaldo Carvalho de Melo 	size_t i;
206c0e53476SArnaldo Carvalho de Melo 	int err;
207c0e53476SArnaldo Carvalho de Melo 
208c0e53476SArnaldo Carvalho de Melo 	for (i = 0; i < nr_assocs; i++) {
209c0e53476SArnaldo Carvalho de Melo 		// Adding a handler for an event not in this evlist, just ignore it.
210b02736f7SArnaldo Carvalho de Melo 		struct evsel *evsel = evlist__find_tracepoint_by_name(evlist, assocs[i].name);
211c0e53476SArnaldo Carvalho de Melo 		if (evsel == NULL)
212c0e53476SArnaldo Carvalho de Melo 			continue;
213c0e53476SArnaldo Carvalho de Melo 
214c0e53476SArnaldo Carvalho de Melo 		err = -EEXIST;
215c0e53476SArnaldo Carvalho de Melo 		if (evsel->handler != NULL)
216c0e53476SArnaldo Carvalho de Melo 			goto out;
217c0e53476SArnaldo Carvalho de Melo 		evsel->handler = assocs[i].handler;
218c0e53476SArnaldo Carvalho de Melo 	}
219c0e53476SArnaldo Carvalho de Melo 
220c0e53476SArnaldo Carvalho de Melo 	err = 0;
221c0e53476SArnaldo Carvalho de Melo out:
222c0e53476SArnaldo Carvalho de Melo 	return err;
223c0e53476SArnaldo Carvalho de Melo }
224c0e53476SArnaldo Carvalho de Melo 
225a622eafaSArnaldo Carvalho de Melo void __evlist__set_leader(struct list_head *list)
22663dab225SArnaldo Carvalho de Melo {
22732dcd021SJiri Olsa 	struct evsel *evsel, *leader;
22863dab225SArnaldo Carvalho de Melo 
229b27c4eceSJiri Olsa 	leader = list_entry(list->next, struct evsel, core.node);
230b27c4eceSJiri Olsa 	evsel = list_entry(list->prev, struct evsel, core.node);
23197f63e4aSNamhyung Kim 
2325643b1a5SJiri Olsa 	leader->core.nr_members = evsel->idx - leader->idx + 1;
23363dab225SArnaldo Carvalho de Melo 
234e5cadb93SArnaldo Carvalho de Melo 	__evlist__for_each_entry(list, evsel) {
23563dab225SArnaldo Carvalho de Melo 		evsel->leader = leader;
23663dab225SArnaldo Carvalho de Melo 	}
23763dab225SArnaldo Carvalho de Melo }
23863dab225SArnaldo Carvalho de Melo 
239a622eafaSArnaldo Carvalho de Melo void evlist__set_leader(struct evlist *evlist)
2406a4bb04cSJiri Olsa {
2416484d2f9SJiri Olsa 	if (evlist->core.nr_entries) {
2426484d2f9SJiri Olsa 		evlist->nr_groups = evlist->core.nr_entries > 1 ? 1 : 0;
243a622eafaSArnaldo Carvalho de Melo 		__evlist__set_leader(&evlist->core.entries);
2446a4bb04cSJiri Olsa 	}
24597f63e4aSNamhyung Kim }
2466a4bb04cSJiri Olsa 
247e251abeeSArnaldo Carvalho de Melo int __evlist__add_default(struct evlist *evlist, bool precise)
248361c99a6SArnaldo Carvalho de Melo {
2498f6725a2SArnaldo Carvalho de Melo 	struct evsel *evsel = evsel__new_cycles(precise);
250361c99a6SArnaldo Carvalho de Melo 
251361c99a6SArnaldo Carvalho de Melo 	if (evsel == NULL)
2527c48dcfdSArnaldo Carvalho de Melo 		return -ENOMEM;
253361c99a6SArnaldo Carvalho de Melo 
254a1cf3a75SJiri Olsa 	evlist__add(evlist, evsel);
255361c99a6SArnaldo Carvalho de Melo 	return 0;
256361c99a6SArnaldo Carvalho de Melo }
2575c581041SArnaldo Carvalho de Melo 
258e251abeeSArnaldo Carvalho de Melo int evlist__add_dummy(struct evlist *evlist)
2595bae0250SArnaldo Carvalho de Melo {
2605bae0250SArnaldo Carvalho de Melo 	struct perf_event_attr attr = {
2615bae0250SArnaldo Carvalho de Melo 		.type	= PERF_TYPE_SOFTWARE,
2625bae0250SArnaldo Carvalho de Melo 		.config = PERF_COUNT_SW_DUMMY,
2635bae0250SArnaldo Carvalho de Melo 		.size	= sizeof(attr), /* to capture ABI version */
2645bae0250SArnaldo Carvalho de Melo 	};
2658f6725a2SArnaldo Carvalho de Melo 	struct evsel *evsel = evsel__new_idx(&attr, evlist->core.nr_entries);
2665bae0250SArnaldo Carvalho de Melo 
2675bae0250SArnaldo Carvalho de Melo 	if (evsel == NULL)
2685bae0250SArnaldo Carvalho de Melo 		return -ENOMEM;
2695bae0250SArnaldo Carvalho de Melo 
270a1cf3a75SJiri Olsa 	evlist__add(evlist, evsel);
2715bae0250SArnaldo Carvalho de Melo 	return 0;
2725bae0250SArnaldo Carvalho de Melo }
2735bae0250SArnaldo Carvalho de Melo 
274e251abeeSArnaldo Carvalho de Melo static int evlist__add_attrs(struct evlist *evlist, struct perf_event_attr *attrs, size_t nr_attrs)
27550d08e47SArnaldo Carvalho de Melo {
27632dcd021SJiri Olsa 	struct evsel *evsel, *n;
27750d08e47SArnaldo Carvalho de Melo 	LIST_HEAD(head);
27850d08e47SArnaldo Carvalho de Melo 	size_t i;
27950d08e47SArnaldo Carvalho de Melo 
28050d08e47SArnaldo Carvalho de Melo 	for (i = 0; i < nr_attrs; i++) {
2818f6725a2SArnaldo Carvalho de Melo 		evsel = evsel__new_idx(attrs + i, evlist->core.nr_entries + i);
28250d08e47SArnaldo Carvalho de Melo 		if (evsel == NULL)
28350d08e47SArnaldo Carvalho de Melo 			goto out_delete_partial_list;
284b27c4eceSJiri Olsa 		list_add_tail(&evsel->core.node, &head);
28550d08e47SArnaldo Carvalho de Melo 	}
28650d08e47SArnaldo Carvalho de Melo 
287e414fd1aSArnaldo Carvalho de Melo 	evlist__splice_list_tail(evlist, &head);
28850d08e47SArnaldo Carvalho de Melo 
28950d08e47SArnaldo Carvalho de Melo 	return 0;
29050d08e47SArnaldo Carvalho de Melo 
29150d08e47SArnaldo Carvalho de Melo out_delete_partial_list:
292e5cadb93SArnaldo Carvalho de Melo 	__evlist__for_each_entry_safe(&head, n, evsel)
2935eb2dd2aSJiri Olsa 		evsel__delete(evsel);
29450d08e47SArnaldo Carvalho de Melo 	return -1;
29550d08e47SArnaldo Carvalho de Melo }
29650d08e47SArnaldo Carvalho de Melo 
297e251abeeSArnaldo Carvalho de Melo int __evlist__add_default_attrs(struct evlist *evlist, struct perf_event_attr *attrs, size_t nr_attrs)
29879695e1bSArnaldo Carvalho de Melo {
29979695e1bSArnaldo Carvalho de Melo 	size_t i;
30079695e1bSArnaldo Carvalho de Melo 
30179695e1bSArnaldo Carvalho de Melo 	for (i = 0; i < nr_attrs; i++)
30279695e1bSArnaldo Carvalho de Melo 		event_attr_init(attrs + i);
30379695e1bSArnaldo Carvalho de Melo 
304a1cf3a75SJiri Olsa 	return evlist__add_attrs(evlist, attrs, nr_attrs);
30579695e1bSArnaldo Carvalho de Melo }
30679695e1bSArnaldo Carvalho de Melo 
30742641d6fSKan Liang __weak int arch_evlist__add_default_attrs(struct evlist *evlist __maybe_unused)
30842641d6fSKan Liang {
30942641d6fSKan Liang 	return 0;
31042641d6fSKan Liang }
31142641d6fSKan Liang 
312b02736f7SArnaldo Carvalho de Melo struct evsel *evlist__find_tracepoint_by_id(struct evlist *evlist, int id)
313ee29be62SArnaldo Carvalho de Melo {
31432dcd021SJiri Olsa 	struct evsel *evsel;
315ee29be62SArnaldo Carvalho de Melo 
316e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, evsel) {
3171fc632ceSJiri Olsa 		if (evsel->core.attr.type   == PERF_TYPE_TRACEPOINT &&
3181fc632ceSJiri Olsa 		    (int)evsel->core.attr.config == id)
319ee29be62SArnaldo Carvalho de Melo 			return evsel;
320ee29be62SArnaldo Carvalho de Melo 	}
321ee29be62SArnaldo Carvalho de Melo 
322ee29be62SArnaldo Carvalho de Melo 	return NULL;
323ee29be62SArnaldo Carvalho de Melo }
324ee29be62SArnaldo Carvalho de Melo 
325b02736f7SArnaldo Carvalho de Melo struct evsel *evlist__find_tracepoint_by_name(struct evlist *evlist, const char *name)
326a2f2804aSDavid Ahern {
32732dcd021SJiri Olsa 	struct evsel *evsel;
328a2f2804aSDavid Ahern 
329e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, evsel) {
3301fc632ceSJiri Olsa 		if ((evsel->core.attr.type == PERF_TYPE_TRACEPOINT) &&
331a2f2804aSDavid Ahern 		    (strcmp(evsel->name, name) == 0))
332a2f2804aSDavid Ahern 			return evsel;
333a2f2804aSDavid Ahern 	}
334a2f2804aSDavid Ahern 
335a2f2804aSDavid Ahern 	return NULL;
336a2f2804aSDavid Ahern }
337a2f2804aSDavid Ahern 
338e251abeeSArnaldo Carvalho de Melo int evlist__add_newtp(struct evlist *evlist, const char *sys, const char *name, void *handler)
33939876e7dSArnaldo Carvalho de Melo {
3408f6725a2SArnaldo Carvalho de Melo 	struct evsel *evsel = evsel__newtp(sys, name);
34139876e7dSArnaldo Carvalho de Melo 
3428dd2a131SJiri Olsa 	if (IS_ERR(evsel))
34339876e7dSArnaldo Carvalho de Melo 		return -1;
34439876e7dSArnaldo Carvalho de Melo 
345744a9719SArnaldo Carvalho de Melo 	evsel->handler = handler;
346a1cf3a75SJiri Olsa 	evlist__add(evlist, evsel);
34739876e7dSArnaldo Carvalho de Melo 	return 0;
34839876e7dSArnaldo Carvalho de Melo }
34939876e7dSArnaldo Carvalho de Melo 
350f63c2f5aSArnaldo Carvalho de Melo static int evlist__nr_threads(struct evlist *evlist, struct evsel *evsel)
351bf8e8f4bSAdrian Hunter {
352648b5af3SJiri Olsa 	if (evsel->core.system_wide)
353bf8e8f4bSAdrian Hunter 		return 1;
354bf8e8f4bSAdrian Hunter 	else
355a2f354e3SJiri Olsa 		return perf_thread_map__nr(evlist->core.threads);
356bf8e8f4bSAdrian Hunter }
357bf8e8f4bSAdrian Hunter 
358a8cbe40fSAndi Kleen void evlist__cpu_iter_start(struct evlist *evlist)
359a8cbe40fSAndi Kleen {
360a8cbe40fSAndi Kleen 	struct evsel *pos;
361a8cbe40fSAndi Kleen 
362a8cbe40fSAndi Kleen 	/*
363a8cbe40fSAndi Kleen 	 * Reset the per evsel cpu_iter. This is needed because
364a8cbe40fSAndi Kleen 	 * each evsel's cpumap may have a different index space,
365a8cbe40fSAndi Kleen 	 * and some operations need the index to modify
366a8cbe40fSAndi Kleen 	 * the FD xyarray (e.g. open, close)
367a8cbe40fSAndi Kleen 	 */
368a8cbe40fSAndi Kleen 	evlist__for_each_entry(evlist, pos)
369a8cbe40fSAndi Kleen 		pos->cpu_iter = 0;
370a8cbe40fSAndi Kleen }
371a8cbe40fSAndi Kleen 
372a8cbe40fSAndi Kleen bool evsel__cpu_iter_skip_no_inc(struct evsel *ev, int cpu)
373a8cbe40fSAndi Kleen {
374a8cbe40fSAndi Kleen 	if (ev->cpu_iter >= ev->core.cpus->nr)
375a8cbe40fSAndi Kleen 		return true;
376a8cbe40fSAndi Kleen 	if (cpu >= 0 && ev->core.cpus->map[ev->cpu_iter] != cpu)
377a8cbe40fSAndi Kleen 		return true;
378a8cbe40fSAndi Kleen 	return false;
379a8cbe40fSAndi Kleen }
380a8cbe40fSAndi Kleen 
381a8cbe40fSAndi Kleen bool evsel__cpu_iter_skip(struct evsel *ev, int cpu)
382a8cbe40fSAndi Kleen {
383a8cbe40fSAndi Kleen 	if (!evsel__cpu_iter_skip_no_inc(ev, cpu)) {
384a8cbe40fSAndi Kleen 		ev->cpu_iter++;
385a8cbe40fSAndi Kleen 		return false;
386a8cbe40fSAndi Kleen 	}
387a8cbe40fSAndi Kleen 	return true;
388a8cbe40fSAndi Kleen }
389a8cbe40fSAndi Kleen 
3907cfcd1e0SJiri Olsa static int evsel__strcmp(struct evsel *pos, char *evsel_name)
3917cfcd1e0SJiri Olsa {
3927cfcd1e0SJiri Olsa 	if (!evsel_name)
3937cfcd1e0SJiri Olsa 		return 0;
3947cfcd1e0SJiri Olsa 	if (evsel__is_dummy_event(pos))
3957cfcd1e0SJiri Olsa 		return 1;
3967cfcd1e0SJiri Olsa 	return strcmp(pos->name, evsel_name);
3977cfcd1e0SJiri Olsa }
3987cfcd1e0SJiri Olsa 
3997cfcd1e0SJiri Olsa static int evlist__is_enabled(struct evlist *evlist)
4007cfcd1e0SJiri Olsa {
4017cfcd1e0SJiri Olsa 	struct evsel *pos;
4027cfcd1e0SJiri Olsa 
4037cfcd1e0SJiri Olsa 	evlist__for_each_entry(evlist, pos) {
4047cfcd1e0SJiri Olsa 		if (!evsel__is_group_leader(pos) || !pos->core.fd)
4057cfcd1e0SJiri Olsa 			continue;
4067cfcd1e0SJiri Olsa 		/* If at least one event is enabled, evlist is enabled. */
4077cfcd1e0SJiri Olsa 		if (!pos->disabled)
4087cfcd1e0SJiri Olsa 			return true;
4097cfcd1e0SJiri Olsa 	}
4107cfcd1e0SJiri Olsa 	return false;
4117cfcd1e0SJiri Olsa }
4127cfcd1e0SJiri Olsa 
4137cfcd1e0SJiri Olsa static void __evlist__disable(struct evlist *evlist, char *evsel_name)
4144152ab37SArnaldo Carvalho de Melo {
41532dcd021SJiri Olsa 	struct evsel *pos;
416704e2f5bSAndi Kleen 	struct affinity affinity;
41787cf8360SAdrian Hunter 	int cpu, i, imm = 0;
41887cf8360SAdrian Hunter 	bool has_imm = false;
419704e2f5bSAndi Kleen 
420704e2f5bSAndi Kleen 	if (affinity__setup(&affinity) < 0)
421704e2f5bSAndi Kleen 		return;
422704e2f5bSAndi Kleen 
42387cf8360SAdrian Hunter 	/* Disable 'immediate' events last */
42487cf8360SAdrian Hunter 	for (imm = 0; imm <= 1; imm++) {
425704e2f5bSAndi Kleen 		evlist__for_each_cpu(evlist, i, cpu) {
426704e2f5bSAndi Kleen 			affinity__set(&affinity, cpu);
4274152ab37SArnaldo Carvalho de Melo 
428e5cadb93SArnaldo Carvalho de Melo 			evlist__for_each_entry(evlist, pos) {
4297cfcd1e0SJiri Olsa 				if (evsel__strcmp(pos, evsel_name))
4307cfcd1e0SJiri Olsa 					continue;
431704e2f5bSAndi Kleen 				if (evsel__cpu_iter_skip(pos, cpu))
432704e2f5bSAndi Kleen 					continue;
433c754c382SArnaldo Carvalho de Melo 				if (pos->disabled || !evsel__is_group_leader(pos) || !pos->core.fd)
4343fe4430dSJiri Olsa 					continue;
43587cf8360SAdrian Hunter 				if (pos->immediate)
43687cf8360SAdrian Hunter 					has_imm = true;
43787cf8360SAdrian Hunter 				if (pos->immediate != imm)
43887cf8360SAdrian Hunter 					continue;
439704e2f5bSAndi Kleen 				evsel__disable_cpu(pos, pos->cpu_iter - 1);
440704e2f5bSAndi Kleen 			}
441704e2f5bSAndi Kleen 		}
44287cf8360SAdrian Hunter 		if (!has_imm)
44387cf8360SAdrian Hunter 			break;
44487cf8360SAdrian Hunter 	}
44587cf8360SAdrian Hunter 
446704e2f5bSAndi Kleen 	affinity__cleanup(&affinity);
447704e2f5bSAndi Kleen 	evlist__for_each_entry(evlist, pos) {
4487cfcd1e0SJiri Olsa 		if (evsel__strcmp(pos, evsel_name))
4497cfcd1e0SJiri Olsa 			continue;
450c754c382SArnaldo Carvalho de Melo 		if (!evsel__is_group_leader(pos) || !pos->core.fd)
451704e2f5bSAndi Kleen 			continue;
452704e2f5bSAndi Kleen 		pos->disabled = true;
4534152ab37SArnaldo Carvalho de Melo 	}
4542b56bcfbSArnaldo Carvalho de Melo 
4557cfcd1e0SJiri Olsa 	/*
4567cfcd1e0SJiri Olsa 	 * If we disabled only single event, we need to check
4577cfcd1e0SJiri Olsa 	 * the enabled state of the evlist manually.
4587cfcd1e0SJiri Olsa 	 */
4597cfcd1e0SJiri Olsa 	if (evsel_name)
4607cfcd1e0SJiri Olsa 		evlist->enabled = evlist__is_enabled(evlist);
4617cfcd1e0SJiri Olsa 	else
4622b56bcfbSArnaldo Carvalho de Melo 		evlist->enabled = false;
4634152ab37SArnaldo Carvalho de Melo }
4644152ab37SArnaldo Carvalho de Melo 
4657cfcd1e0SJiri Olsa void evlist__disable(struct evlist *evlist)
4667cfcd1e0SJiri Olsa {
4677cfcd1e0SJiri Olsa 	__evlist__disable(evlist, NULL);
4687cfcd1e0SJiri Olsa }
4697cfcd1e0SJiri Olsa 
4707cfcd1e0SJiri Olsa void evlist__disable_evsel(struct evlist *evlist, char *evsel_name)
4717cfcd1e0SJiri Olsa {
4727cfcd1e0SJiri Olsa 	__evlist__disable(evlist, evsel_name);
4737cfcd1e0SJiri Olsa }
4747cfcd1e0SJiri Olsa 
4757cfcd1e0SJiri Olsa static void __evlist__enable(struct evlist *evlist, char *evsel_name)
476764e16a3SDavid Ahern {
47732dcd021SJiri Olsa 	struct evsel *pos;
478704e2f5bSAndi Kleen 	struct affinity affinity;
479704e2f5bSAndi Kleen 	int cpu, i;
480764e16a3SDavid Ahern 
481704e2f5bSAndi Kleen 	if (affinity__setup(&affinity) < 0)
482704e2f5bSAndi Kleen 		return;
483704e2f5bSAndi Kleen 
484704e2f5bSAndi Kleen 	evlist__for_each_cpu(evlist, i, cpu) {
485704e2f5bSAndi Kleen 		affinity__set(&affinity, cpu);
486704e2f5bSAndi Kleen 
487704e2f5bSAndi Kleen 		evlist__for_each_entry(evlist, pos) {
4887cfcd1e0SJiri Olsa 			if (evsel__strcmp(pos, evsel_name))
4897cfcd1e0SJiri Olsa 				continue;
490704e2f5bSAndi Kleen 			if (evsel__cpu_iter_skip(pos, cpu))
491704e2f5bSAndi Kleen 				continue;
492c754c382SArnaldo Carvalho de Melo 			if (!evsel__is_group_leader(pos) || !pos->core.fd)
493704e2f5bSAndi Kleen 				continue;
494704e2f5bSAndi Kleen 			evsel__enable_cpu(pos, pos->cpu_iter - 1);
495704e2f5bSAndi Kleen 		}
496704e2f5bSAndi Kleen 	}
497704e2f5bSAndi Kleen 	affinity__cleanup(&affinity);
498e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, pos) {
4997cfcd1e0SJiri Olsa 		if (evsel__strcmp(pos, evsel_name))
5007cfcd1e0SJiri Olsa 			continue;
501c754c382SArnaldo Carvalho de Melo 		if (!evsel__is_group_leader(pos) || !pos->core.fd)
5023fe4430dSJiri Olsa 			continue;
503704e2f5bSAndi Kleen 		pos->disabled = false;
504764e16a3SDavid Ahern 	}
5052b56bcfbSArnaldo Carvalho de Melo 
5067cfcd1e0SJiri Olsa 	/*
5077cfcd1e0SJiri Olsa 	 * Even single event sets the 'enabled' for evlist,
5087cfcd1e0SJiri Olsa 	 * so the toggle can work properly and toggle to
5097cfcd1e0SJiri Olsa 	 * 'disabled' state.
5107cfcd1e0SJiri Olsa 	 */
5112b56bcfbSArnaldo Carvalho de Melo 	evlist->enabled = true;
5122b56bcfbSArnaldo Carvalho de Melo }
5132b56bcfbSArnaldo Carvalho de Melo 
5147cfcd1e0SJiri Olsa void evlist__enable(struct evlist *evlist)
5157cfcd1e0SJiri Olsa {
5167cfcd1e0SJiri Olsa 	__evlist__enable(evlist, NULL);
5177cfcd1e0SJiri Olsa }
5187cfcd1e0SJiri Olsa 
5197cfcd1e0SJiri Olsa void evlist__enable_evsel(struct evlist *evlist, char *evsel_name)
5207cfcd1e0SJiri Olsa {
5217cfcd1e0SJiri Olsa 	__evlist__enable(evlist, evsel_name);
5227cfcd1e0SJiri Olsa }
5237cfcd1e0SJiri Olsa 
524ade9d208SArnaldo Carvalho de Melo void evlist__toggle_enable(struct evlist *evlist)
5252b56bcfbSArnaldo Carvalho de Melo {
526e74676deSJiri Olsa 	(evlist->enabled ? evlist__disable : evlist__enable)(evlist);
527764e16a3SDavid Ahern }
528764e16a3SDavid Ahern 
52937b01abeSArnaldo Carvalho de Melo static int evlist__enable_event_cpu(struct evlist *evlist, struct evsel *evsel, int cpu)
5301c65056cSAdrian Hunter {
53118ef15c6SArnaldo Carvalho de Melo 	int thread;
532f63c2f5aSArnaldo Carvalho de Melo 	int nr_threads = evlist__nr_threads(evlist, evsel);
5331c65056cSAdrian Hunter 
5349dfcb759SJiri Olsa 	if (!evsel->core.fd)
5351c65056cSAdrian Hunter 		return -EINVAL;
5361c65056cSAdrian Hunter 
5371c65056cSAdrian Hunter 	for (thread = 0; thread < nr_threads; thread++) {
53818ef15c6SArnaldo Carvalho de Melo 		int err = ioctl(FD(evsel, cpu, thread), PERF_EVENT_IOC_ENABLE, 0);
5391c65056cSAdrian Hunter 		if (err)
5401c65056cSAdrian Hunter 			return err;
5411c65056cSAdrian Hunter 	}
5421c65056cSAdrian Hunter 	return 0;
5431c65056cSAdrian Hunter }
5441c65056cSAdrian Hunter 
54537b01abeSArnaldo Carvalho de Melo static int evlist__enable_event_thread(struct evlist *evlist, struct evsel *evsel, int thread)
5461c65056cSAdrian Hunter {
54718ef15c6SArnaldo Carvalho de Melo 	int cpu;
5486549cd8fSJiri Olsa 	int nr_cpus = perf_cpu_map__nr(evlist->core.cpus);
5491c65056cSAdrian Hunter 
5509dfcb759SJiri Olsa 	if (!evsel->core.fd)
5511c65056cSAdrian Hunter 		return -EINVAL;
5521c65056cSAdrian Hunter 
5531c65056cSAdrian Hunter 	for (cpu = 0; cpu < nr_cpus; cpu++) {
55418ef15c6SArnaldo Carvalho de Melo 		int err = ioctl(FD(evsel, cpu, thread), PERF_EVENT_IOC_ENABLE, 0);
5551c65056cSAdrian Hunter 		if (err)
5561c65056cSAdrian Hunter 			return err;
5571c65056cSAdrian Hunter 	}
5581c65056cSAdrian Hunter 	return 0;
5591c65056cSAdrian Hunter }
5601c65056cSAdrian Hunter 
56137b01abeSArnaldo Carvalho de Melo int evlist__enable_event_idx(struct evlist *evlist, struct evsel *evsel, int idx)
5621c65056cSAdrian Hunter {
563315c0a1fSJiri Olsa 	bool per_cpu_mmaps = !perf_cpu_map__empty(evlist->core.cpus);
5641c65056cSAdrian Hunter 
5651c65056cSAdrian Hunter 	if (per_cpu_mmaps)
56637b01abeSArnaldo Carvalho de Melo 		return evlist__enable_event_cpu(evlist, evsel, idx);
56737b01abeSArnaldo Carvalho de Melo 
56837b01abeSArnaldo Carvalho de Melo 	return evlist__enable_event_thread(evlist, evsel, idx);
5691c65056cSAdrian Hunter }
5701c65056cSAdrian Hunter 
571f4009e7bSJiri Olsa int evlist__add_pollfd(struct evlist *evlist, int fd)
572e4b356b5SArnaldo Carvalho de Melo {
573ab4c1f9fSAlexey Budankov 	return perf_evlist__add_pollfd(&evlist->core, fd, NULL, POLLIN, fdarray_flag__default);
574e4b356b5SArnaldo Carvalho de Melo }
5751b85337dSArnaldo Carvalho de Melo 
576f4009e7bSJiri Olsa int evlist__filter_pollfd(struct evlist *evlist, short revents_and_mask)
5771ddec7f0SArnaldo Carvalho de Melo {
57884227cb1SJiri Olsa 	return perf_evlist__filter_pollfd(&evlist->core, revents_and_mask);
5791ddec7f0SArnaldo Carvalho de Melo }
5801ddec7f0SArnaldo Carvalho de Melo 
581*e16c2ce7SYang Jihong #ifdef HAVE_EVENTFD_SUPPORT
582*e16c2ce7SYang Jihong int evlist__add_wakeup_eventfd(struct evlist *evlist, int fd)
583*e16c2ce7SYang Jihong {
584*e16c2ce7SYang Jihong 	return perf_evlist__add_pollfd(&evlist->core, fd, NULL, POLLIN,
585*e16c2ce7SYang Jihong 				       fdarray_flag__nonfilterable);
586*e16c2ce7SYang Jihong }
587*e16c2ce7SYang Jihong #endif
588*e16c2ce7SYang Jihong 
58980ab2987SJiri Olsa int evlist__poll(struct evlist *evlist, int timeout)
590f66a889dSArnaldo Carvalho de Melo {
59180ab2987SJiri Olsa 	return perf_evlist__poll(&evlist->core, timeout);
592f66a889dSArnaldo Carvalho de Melo }
593f66a889dSArnaldo Carvalho de Melo 
5943ccf8a7bSArnaldo Carvalho de Melo struct perf_sample_id *evlist__id2sid(struct evlist *evlist, u64 id)
59570db7533SArnaldo Carvalho de Melo {
59670db7533SArnaldo Carvalho de Melo 	struct hlist_head *head;
59770db7533SArnaldo Carvalho de Melo 	struct perf_sample_id *sid;
59870db7533SArnaldo Carvalho de Melo 	int hash;
59970db7533SArnaldo Carvalho de Melo 
60070db7533SArnaldo Carvalho de Melo 	hash = hash_64(id, PERF_EVLIST__HLIST_BITS);
6011d5af02dSJiri Olsa 	head = &evlist->core.heads[hash];
60270db7533SArnaldo Carvalho de Melo 
603b67bfe0dSSasha Levin 	hlist_for_each_entry(sid, head, node)
60470db7533SArnaldo Carvalho de Melo 		if (sid->id == id)
605932a3594SJiri Olsa 			return sid;
606932a3594SJiri Olsa 
607932a3594SJiri Olsa 	return NULL;
608932a3594SJiri Olsa }
609932a3594SJiri Olsa 
6103ccf8a7bSArnaldo Carvalho de Melo struct evsel *evlist__id2evsel(struct evlist *evlist, u64 id)
611932a3594SJiri Olsa {
612932a3594SJiri Olsa 	struct perf_sample_id *sid;
613932a3594SJiri Olsa 
6146484d2f9SJiri Olsa 	if (evlist->core.nr_entries == 1 || !id)
615515dbe48SJiri Olsa 		return evlist__first(evlist);
616932a3594SJiri Olsa 
6173ccf8a7bSArnaldo Carvalho de Melo 	sid = evlist__id2sid(evlist, id);
618932a3594SJiri Olsa 	if (sid)
61970c20369SJiri Olsa 		return container_of(sid->evsel, struct evsel, core);
62030e68bccSNamhyung Kim 
6218cedf3a5SArnaldo Carvalho de Melo 	if (!evlist__sample_id_all(evlist))
622515dbe48SJiri Olsa 		return evlist__first(evlist);
62330e68bccSNamhyung Kim 
62470db7533SArnaldo Carvalho de Melo 	return NULL;
62570db7533SArnaldo Carvalho de Melo }
62604391debSArnaldo Carvalho de Melo 
6273ccf8a7bSArnaldo Carvalho de Melo struct evsel *evlist__id2evsel_strict(struct evlist *evlist, u64 id)
628dddcf6abSAdrian Hunter {
629dddcf6abSAdrian Hunter 	struct perf_sample_id *sid;
630dddcf6abSAdrian Hunter 
631dddcf6abSAdrian Hunter 	if (!id)
632dddcf6abSAdrian Hunter 		return NULL;
633dddcf6abSAdrian Hunter 
6343ccf8a7bSArnaldo Carvalho de Melo 	sid = evlist__id2sid(evlist, id);
635dddcf6abSAdrian Hunter 	if (sid)
63670c20369SJiri Olsa 		return container_of(sid->evsel, struct evsel, core);
637dddcf6abSAdrian Hunter 
638dddcf6abSAdrian Hunter 	return NULL;
639dddcf6abSAdrian Hunter }
640dddcf6abSAdrian Hunter 
6413ccf8a7bSArnaldo Carvalho de Melo static int evlist__event2id(struct evlist *evlist, union perf_event *event, u64 *id)
64275562573SAdrian Hunter {
643b1fcd190SJiri Olsa 	const __u64 *array = event->sample.array;
64475562573SAdrian Hunter 	ssize_t n;
64575562573SAdrian Hunter 
64675562573SAdrian Hunter 	n = (event->header.size - sizeof(event->header)) >> 3;
64775562573SAdrian Hunter 
64875562573SAdrian Hunter 	if (event->header.type == PERF_RECORD_SAMPLE) {
64975562573SAdrian Hunter 		if (evlist->id_pos >= n)
65075562573SAdrian Hunter 			return -1;
65175562573SAdrian Hunter 		*id = array[evlist->id_pos];
65275562573SAdrian Hunter 	} else {
65375562573SAdrian Hunter 		if (evlist->is_pos > n)
65475562573SAdrian Hunter 			return -1;
65575562573SAdrian Hunter 		n -= evlist->is_pos;
65675562573SAdrian Hunter 		*id = array[n];
65775562573SAdrian Hunter 	}
65875562573SAdrian Hunter 	return 0;
65975562573SAdrian Hunter }
66075562573SAdrian Hunter 
6613ccf8a7bSArnaldo Carvalho de Melo struct evsel *evlist__event2evsel(struct evlist *evlist, union perf_event *event)
66275562573SAdrian Hunter {
663515dbe48SJiri Olsa 	struct evsel *first = evlist__first(evlist);
66475562573SAdrian Hunter 	struct hlist_head *head;
66575562573SAdrian Hunter 	struct perf_sample_id *sid;
66675562573SAdrian Hunter 	int hash;
66775562573SAdrian Hunter 	u64 id;
66875562573SAdrian Hunter 
6696484d2f9SJiri Olsa 	if (evlist->core.nr_entries == 1)
67098be6966SAdrian Hunter 		return first;
67198be6966SAdrian Hunter 
6721fc632ceSJiri Olsa 	if (!first->core.attr.sample_id_all &&
67398be6966SAdrian Hunter 	    event->header.type != PERF_RECORD_SAMPLE)
67498be6966SAdrian Hunter 		return first;
67575562573SAdrian Hunter 
6763ccf8a7bSArnaldo Carvalho de Melo 	if (evlist__event2id(evlist, event, &id))
67775562573SAdrian Hunter 		return NULL;
67875562573SAdrian Hunter 
67975562573SAdrian Hunter 	/* Synthesized events have an id of zero */
68075562573SAdrian Hunter 	if (!id)
68198be6966SAdrian Hunter 		return first;
68275562573SAdrian Hunter 
68375562573SAdrian Hunter 	hash = hash_64(id, PERF_EVLIST__HLIST_BITS);
6841d5af02dSJiri Olsa 	head = &evlist->core.heads[hash];
68575562573SAdrian Hunter 
68675562573SAdrian Hunter 	hlist_for_each_entry(sid, head, node) {
68775562573SAdrian Hunter 		if (sid->id == id)
68870c20369SJiri Olsa 			return container_of(sid->evsel, struct evsel, core);
68975562573SAdrian Hunter 	}
69075562573SAdrian Hunter 	return NULL;
69175562573SAdrian Hunter }
69275562573SAdrian Hunter 
6930a60b339SArnaldo Carvalho de Melo static int evlist__set_paused(struct evlist *evlist, bool value)
69465aea233SWang Nan {
69565aea233SWang Nan 	int i;
69665aea233SWang Nan 
6970b72d69aSWang Nan 	if (!evlist->overwrite_mmap)
698078c3386SWang Nan 		return 0;
699078c3386SWang Nan 
700c976ee11SJiri Olsa 	for (i = 0; i < evlist->core.nr_mmaps; i++) {
7012cf07b29SJiri Olsa 		int fd = evlist->overwrite_mmap[i].core.fd;
70265aea233SWang Nan 		int err;
70365aea233SWang Nan 
70465aea233SWang Nan 		if (fd < 0)
70565aea233SWang Nan 			continue;
70665aea233SWang Nan 		err = ioctl(fd, PERF_EVENT_IOC_PAUSE_OUTPUT, value ? 1 : 0);
70765aea233SWang Nan 		if (err)
70865aea233SWang Nan 			return err;
70965aea233SWang Nan 	}
71065aea233SWang Nan 	return 0;
71165aea233SWang Nan }
71265aea233SWang Nan 
7130a60b339SArnaldo Carvalho de Melo static int evlist__pause(struct evlist *evlist)
71465aea233SWang Nan {
7150a60b339SArnaldo Carvalho de Melo 	return evlist__set_paused(evlist, true);
71665aea233SWang Nan }
71765aea233SWang Nan 
7180a60b339SArnaldo Carvalho de Melo static int evlist__resume(struct evlist *evlist)
71965aea233SWang Nan {
7200a60b339SArnaldo Carvalho de Melo 	return evlist__set_paused(evlist, false);
72165aea233SWang Nan }
72265aea233SWang Nan 
723db6b7b13SJiri Olsa static void evlist__munmap_nofree(struct evlist *evlist)
724f8a95309SArnaldo Carvalho de Melo {
725aece948fSArnaldo Carvalho de Melo 	int i;
726f8a95309SArnaldo Carvalho de Melo 
727b2cb615dSWang Nan 	if (evlist->mmap)
728c976ee11SJiri Olsa 		for (i = 0; i < evlist->core.nr_mmaps; i++)
72980e53d11SJiri Olsa 			perf_mmap__munmap(&evlist->mmap[i].core);
730b2cb615dSWang Nan 
7310b72d69aSWang Nan 	if (evlist->overwrite_mmap)
732c976ee11SJiri Olsa 		for (i = 0; i < evlist->core.nr_mmaps; i++)
73380e53d11SJiri Olsa 			perf_mmap__munmap(&evlist->overwrite_mmap[i].core);
734a1f72618SWang Nan }
735aece948fSArnaldo Carvalho de Melo 
736db6b7b13SJiri Olsa void evlist__munmap(struct evlist *evlist)
737a1f72618SWang Nan {
738db6b7b13SJiri Olsa 	evlist__munmap_nofree(evlist);
73904662523SArnaldo Carvalho de Melo 	zfree(&evlist->mmap);
7400b72d69aSWang Nan 	zfree(&evlist->overwrite_mmap);
741f8a95309SArnaldo Carvalho de Melo }
742f8a95309SArnaldo Carvalho de Melo 
74380e53d11SJiri Olsa static void perf_mmap__unmap_cb(struct perf_mmap *map)
74480e53d11SJiri Olsa {
74580e53d11SJiri Olsa 	struct mmap *m = container_of(map, struct mmap, core);
74680e53d11SJiri Olsa 
74780e53d11SJiri Olsa 	mmap__munmap(m);
74880e53d11SJiri Olsa }
74980e53d11SJiri Olsa 
750d50cf361SJiri Olsa static struct mmap *evlist__alloc_mmap(struct evlist *evlist,
7512c5f6d87SKan Liang 				       bool overwrite)
752f8a95309SArnaldo Carvalho de Melo {
753d4c6fb36SWang Nan 	int i;
754a5830532SJiri Olsa 	struct mmap *map;
755d4c6fb36SWang Nan 
756c976ee11SJiri Olsa 	map = zalloc(evlist->core.nr_mmaps * sizeof(struct mmap));
7578db6d6b1SWang Nan 	if (!map)
7588db6d6b1SWang Nan 		return NULL;
759946ae1d4SWang Nan 
760c976ee11SJiri Olsa 	for (i = 0; i < evlist->core.nr_mmaps; i++) {
7616eb65f7aSJiri Olsa 		struct perf_mmap *prev = i ? &map[i - 1].core : NULL;
7626eb65f7aSJiri Olsa 
7634738ca30SArnaldo Carvalho de Melo 		/*
7644738ca30SArnaldo Carvalho de Melo 		 * When the perf_mmap() call is made we grab one refcount, plus
7656afad54dSKan Liang 		 * one extra to let perf_mmap__consume() get the last
7664738ca30SArnaldo Carvalho de Melo 		 * events after all real references (perf_mmap__get()) are
7674738ca30SArnaldo Carvalho de Melo 		 * dropped.
7684738ca30SArnaldo Carvalho de Melo 		 *
7694738ca30SArnaldo Carvalho de Melo 		 * Each PERF_EVENT_IOC_SET_OUTPUT points to this mmap and
7704738ca30SArnaldo Carvalho de Melo 		 * thus does perf_mmap__get() on it.
7714738ca30SArnaldo Carvalho de Melo 		 */
7726eb65f7aSJiri Olsa 		perf_mmap__init(&map[i].core, prev, overwrite, perf_mmap__unmap_cb);
7734738ca30SArnaldo Carvalho de Melo 	}
774353120b4SJiri Olsa 
7758db6d6b1SWang Nan 	return map;
776f8a95309SArnaldo Carvalho de Melo }
777f8a95309SArnaldo Carvalho de Melo 
7789abd2ab2SJiri Olsa static void
7799abd2ab2SJiri Olsa perf_evlist__mmap_cb_idx(struct perf_evlist *_evlist,
7809abd2ab2SJiri Olsa 			 struct perf_mmap_param *_mp,
7819abd2ab2SJiri Olsa 			 int idx, bool per_cpu)
7829abd2ab2SJiri Olsa {
7839abd2ab2SJiri Olsa 	struct evlist *evlist = container_of(_evlist, struct evlist, core);
7849abd2ab2SJiri Olsa 	struct mmap_params *mp = container_of(_mp, struct mmap_params, core);
7859abd2ab2SJiri Olsa 
7869abd2ab2SJiri Olsa 	auxtrace_mmap_params__set_idx(&mp->auxtrace_mp, evlist, idx, per_cpu);
7879abd2ab2SJiri Olsa }
7889abd2ab2SJiri Olsa 
789bb1b1885SJiri Olsa static struct perf_mmap*
790bb1b1885SJiri Olsa perf_evlist__mmap_cb_get(struct perf_evlist *_evlist, bool overwrite, int idx)
791bb1b1885SJiri Olsa {
792bb1b1885SJiri Olsa 	struct evlist *evlist = container_of(_evlist, struct evlist, core);
7933805e4f3SJiri Olsa 	struct mmap *maps;
794bb1b1885SJiri Olsa 
7953805e4f3SJiri Olsa 	maps = overwrite ? evlist->overwrite_mmap : evlist->mmap;
796bb1b1885SJiri Olsa 
797bb1b1885SJiri Olsa 	if (!maps) {
7983805e4f3SJiri Olsa 		maps = evlist__alloc_mmap(evlist, overwrite);
799bb1b1885SJiri Olsa 		if (!maps)
800bb1b1885SJiri Olsa 			return NULL;
801bb1b1885SJiri Olsa 
8023805e4f3SJiri Olsa 		if (overwrite) {
803bb1b1885SJiri Olsa 			evlist->overwrite_mmap = maps;
804bb1b1885SJiri Olsa 			if (evlist->bkw_mmap_state == BKW_MMAP_NOTREADY)
805ade9d208SArnaldo Carvalho de Melo 				evlist__toggle_bkw_mmap(evlist, BKW_MMAP_RUNNING);
8063805e4f3SJiri Olsa 		} else {
8073805e4f3SJiri Olsa 			evlist->mmap = maps;
808bb1b1885SJiri Olsa 		}
809bb1b1885SJiri Olsa 	}
810bb1b1885SJiri Olsa 
811bb1b1885SJiri Olsa 	return &maps[idx].core;
812bb1b1885SJiri Olsa }
813bb1b1885SJiri Olsa 
814b80132b1SJiri Olsa static int
815b80132b1SJiri Olsa perf_evlist__mmap_cb_mmap(struct perf_mmap *_map, struct perf_mmap_param *_mp,
816b80132b1SJiri Olsa 			  int output, int cpu)
817b80132b1SJiri Olsa {
818b80132b1SJiri Olsa 	struct mmap *map = container_of(_map, struct mmap, core);
819b80132b1SJiri Olsa 	struct mmap_params *mp = container_of(_mp, struct mmap_params, core);
820b80132b1SJiri Olsa 
821b80132b1SJiri Olsa 	return mmap__mmap(map, mp, output, cpu);
822b80132b1SJiri Olsa }
823b80132b1SJiri Olsa 
824f5e7150cSArnaldo Carvalho de Melo unsigned long perf_event_mlock_kb_in_pages(void)
825994a1f78SJiri Olsa {
826f5e7150cSArnaldo Carvalho de Melo 	unsigned long pages;
8278185e881SArnaldo Carvalho de Melo 	int max;
8288185e881SArnaldo Carvalho de Melo 
8298185e881SArnaldo Carvalho de Melo 	if (sysctl__read_int("kernel/perf_event_mlock_kb", &max) < 0) {
8308185e881SArnaldo Carvalho de Melo 		/*
8318185e881SArnaldo Carvalho de Melo 		 * Pick a once upon a time good value, i.e. things look
8328185e881SArnaldo Carvalho de Melo 		 * strange since we can't read a sysctl value, but lets not
8338185e881SArnaldo Carvalho de Melo 		 * die yet...
8348185e881SArnaldo Carvalho de Melo 		 */
8358185e881SArnaldo Carvalho de Melo 		max = 512;
8368185e881SArnaldo Carvalho de Melo 	} else {
8378185e881SArnaldo Carvalho de Melo 		max -= (page_size / 1024);
8388185e881SArnaldo Carvalho de Melo 	}
8398185e881SArnaldo Carvalho de Melo 
8408185e881SArnaldo Carvalho de Melo 	pages = (max * 1024) / page_size;
8411be300f4SArnaldo Carvalho de Melo 	if (!is_power_of_2(pages))
8421be300f4SArnaldo Carvalho de Melo 		pages = rounddown_pow_of_two(pages);
843f5e7150cSArnaldo Carvalho de Melo 
844f5e7150cSArnaldo Carvalho de Melo 	return pages;
845f5e7150cSArnaldo Carvalho de Melo }
846f5e7150cSArnaldo Carvalho de Melo 
8479521b5f2SJiri Olsa size_t evlist__mmap_size(unsigned long pages)
848f5e7150cSArnaldo Carvalho de Melo {
849f5e7150cSArnaldo Carvalho de Melo 	if (pages == UINT_MAX)
850f5e7150cSArnaldo Carvalho de Melo 		pages = perf_event_mlock_kb_in_pages();
851f5e7150cSArnaldo Carvalho de Melo 	else if (!is_power_of_2(pages))
852994a1f78SJiri Olsa 		return 0;
853994a1f78SJiri Olsa 
854994a1f78SJiri Olsa 	return (pages + 1) * page_size;
855994a1f78SJiri Olsa }
856994a1f78SJiri Olsa 
85733c2dcfdSDavid Ahern static long parse_pages_arg(const char *str, unsigned long min,
85833c2dcfdSDavid Ahern 			    unsigned long max)
859994a1f78SJiri Olsa {
8602fbe4abeSAdrian Hunter 	unsigned long pages, val;
86127050f53SJiri Olsa 	static struct parse_tag tags[] = {
86227050f53SJiri Olsa 		{ .tag  = 'B', .mult = 1       },
86327050f53SJiri Olsa 		{ .tag  = 'K', .mult = 1 << 10 },
86427050f53SJiri Olsa 		{ .tag  = 'M', .mult = 1 << 20 },
86527050f53SJiri Olsa 		{ .tag  = 'G', .mult = 1 << 30 },
86627050f53SJiri Olsa 		{ .tag  = 0 },
86727050f53SJiri Olsa 	};
868994a1f78SJiri Olsa 
8698973504bSDavid Ahern 	if (str == NULL)
87033c2dcfdSDavid Ahern 		return -EINVAL;
8718973504bSDavid Ahern 
87227050f53SJiri Olsa 	val = parse_tag_value(str, tags);
8732fbe4abeSAdrian Hunter 	if (val != (unsigned long) -1) {
87427050f53SJiri Olsa 		/* we got file size value */
87527050f53SJiri Olsa 		pages = PERF_ALIGN(val, page_size) / page_size;
87627050f53SJiri Olsa 	} else {
87727050f53SJiri Olsa 		/* we got pages count value */
87827050f53SJiri Olsa 		char *eptr;
879994a1f78SJiri Olsa 		pages = strtoul(str, &eptr, 10);
88033c2dcfdSDavid Ahern 		if (*eptr != '\0')
88133c2dcfdSDavid Ahern 			return -EINVAL;
88227050f53SJiri Olsa 	}
883994a1f78SJiri Olsa 
8842bcab6c1SAdrian Hunter 	if (pages == 0 && min == 0) {
88533c2dcfdSDavid Ahern 		/* leave number of pages at 0 */
8861dbfa938SAdrian Hunter 	} else if (!is_power_of_2(pages)) {
8879808143bSJiri Olsa 		char buf[100];
8889808143bSJiri Olsa 
88933c2dcfdSDavid Ahern 		/* round pages up to next power of 2 */
89091529834SArnaldo Carvalho de Melo 		pages = roundup_pow_of_two(pages);
8911dbfa938SAdrian Hunter 		if (!pages)
8921dbfa938SAdrian Hunter 			return -EINVAL;
8939808143bSJiri Olsa 
8949808143bSJiri Olsa 		unit_number__scnprintf(buf, sizeof(buf), pages * page_size);
8959808143bSJiri Olsa 		pr_info("rounding mmap pages size to %s (%lu pages)\n",
8969808143bSJiri Olsa 			buf, pages);
8972fbe4abeSAdrian Hunter 	}
8982fbe4abeSAdrian Hunter 
89933c2dcfdSDavid Ahern 	if (pages > max)
90033c2dcfdSDavid Ahern 		return -EINVAL;
90133c2dcfdSDavid Ahern 
90233c2dcfdSDavid Ahern 	return pages;
90333c2dcfdSDavid Ahern }
90433c2dcfdSDavid Ahern 
90525f84702SArnaldo Carvalho de Melo int __evlist__parse_mmap_pages(unsigned int *mmap_pages, const char *str)
90633c2dcfdSDavid Ahern {
90733c2dcfdSDavid Ahern 	unsigned long max = UINT_MAX;
90833c2dcfdSDavid Ahern 	long pages;
90933c2dcfdSDavid Ahern 
910f5ae9c42SAdrian Hunter 	if (max > SIZE_MAX / page_size)
91133c2dcfdSDavid Ahern 		max = SIZE_MAX / page_size;
91233c2dcfdSDavid Ahern 
91333c2dcfdSDavid Ahern 	pages = parse_pages_arg(str, 1, max);
91433c2dcfdSDavid Ahern 	if (pages < 0) {
91533c2dcfdSDavid Ahern 		pr_err("Invalid argument for --mmap_pages/-m\n");
916994a1f78SJiri Olsa 		return -1;
917994a1f78SJiri Olsa 	}
918994a1f78SJiri Olsa 
919994a1f78SJiri Olsa 	*mmap_pages = pages;
920994a1f78SJiri Olsa 	return 0;
921994a1f78SJiri Olsa }
922994a1f78SJiri Olsa 
92325f84702SArnaldo Carvalho de Melo int evlist__parse_mmap_pages(const struct option *opt, const char *str, int unset __maybe_unused)
924e9db1310SAdrian Hunter {
92525f84702SArnaldo Carvalho de Melo 	return __evlist__parse_mmap_pages(opt->value, str);
926e9db1310SAdrian Hunter }
927e9db1310SAdrian Hunter 
928c83fa7f2SAdrian Hunter /**
9299521b5f2SJiri Olsa  * evlist__mmap_ex - Create mmaps to receive events.
930c83fa7f2SAdrian Hunter  * @evlist: list of events
931c83fa7f2SAdrian Hunter  * @pages: map length in pages
932c83fa7f2SAdrian Hunter  * @overwrite: overwrite older events?
933718c602dSAdrian Hunter  * @auxtrace_pages - auxtrace map length in pages
934718c602dSAdrian Hunter  * @auxtrace_overwrite - overwrite older auxtrace data?
935f8a95309SArnaldo Carvalho de Melo  *
936c83fa7f2SAdrian Hunter  * If @overwrite is %false the user needs to signal event consumption using
9379521b5f2SJiri Olsa  * perf_mmap__write_tail().  Using evlist__mmap_read() does this
938c83fa7f2SAdrian Hunter  * automatically.
939f8a95309SArnaldo Carvalho de Melo  *
940718c602dSAdrian Hunter  * Similarly, if @auxtrace_overwrite is %false the user needs to signal data
941718c602dSAdrian Hunter  * consumption using auxtrace_mmap__write_tail().
942718c602dSAdrian Hunter  *
943c83fa7f2SAdrian Hunter  * Return: %0 on success, negative error code otherwise.
944f8a95309SArnaldo Carvalho de Melo  */
9459521b5f2SJiri Olsa int evlist__mmap_ex(struct evlist *evlist, unsigned int pages,
9467a276ff6SWang Nan 			 unsigned int auxtrace_pages,
94751255a8aSAlexey Budankov 			 bool auxtrace_overwrite, int nr_cblocks, int affinity, int flush,
94851255a8aSAlexey Budankov 			 int comp_level)
949f8a95309SArnaldo Carvalho de Melo {
95071f566a3SWang Nan 	/*
95171f566a3SWang Nan 	 * Delay setting mp.prot: set it before calling perf_mmap__mmap.
95271f566a3SWang Nan 	 * Its value is decided by evsel's write_backward.
95371f566a3SWang Nan 	 * So &mp should not be passed through const pointer.
95471f566a3SWang Nan 	 */
955e440979fSJiri Olsa 	struct mmap_params mp = {
956e440979fSJiri Olsa 		.nr_cblocks	= nr_cblocks,
957e440979fSJiri Olsa 		.affinity	= affinity,
958e440979fSJiri Olsa 		.flush		= flush,
959e440979fSJiri Olsa 		.comp_level	= comp_level
960e440979fSJiri Olsa 	};
961923d0f18SJiri Olsa 	struct perf_evlist_mmap_ops ops = {
9629abd2ab2SJiri Olsa 		.idx  = perf_evlist__mmap_cb_idx,
963bb1b1885SJiri Olsa 		.get  = perf_evlist__mmap_cb_get,
964b80132b1SJiri Olsa 		.mmap = perf_evlist__mmap_cb_mmap,
9659abd2ab2SJiri Olsa 	};
96650a682ceSArnaldo Carvalho de Melo 
967f6fa4375SJiri Olsa 	evlist->core.mmap_len = evlist__mmap_size(pages);
968f6fa4375SJiri Olsa 	pr_debug("mmap size %zuB\n", evlist->core.mmap_len);
969f8a95309SArnaldo Carvalho de Melo 
970f6fa4375SJiri Olsa 	auxtrace_mmap_params__init(&mp.auxtrace_mp, evlist->core.mmap_len,
971718c602dSAdrian Hunter 				   auxtrace_pages, auxtrace_overwrite);
972718c602dSAdrian Hunter 
973923d0f18SJiri Olsa 	return perf_evlist__mmap_ops(&evlist->core, &ops, &mp.core);
974f8a95309SArnaldo Carvalho de Melo }
9757e2ed097SArnaldo Carvalho de Melo 
9769521b5f2SJiri Olsa int evlist__mmap(struct evlist *evlist, unsigned int pages)
977718c602dSAdrian Hunter {
9789521b5f2SJiri Olsa 	return evlist__mmap_ex(evlist, pages, 0, false, 0, PERF_AFFINITY_SYS, 1, 0);
979718c602dSAdrian Hunter }
980718c602dSAdrian Hunter 
9817748bb71SArnaldo Carvalho de Melo int evlist__create_maps(struct evlist *evlist, struct target *target)
9827e2ed097SArnaldo Carvalho de Melo {
983147c508fSJin Yao 	bool all_threads = (target->per_thread && target->system_wide);
984f854839bSJiri Olsa 	struct perf_cpu_map *cpus;
9859749b90eSJiri Olsa 	struct perf_thread_map *threads;
9867e2ed097SArnaldo Carvalho de Melo 
987147c508fSJin Yao 	/*
988147c508fSJin Yao 	 * If specify '-a' and '--per-thread' to perf record, perf record
989147c508fSJin Yao 	 * will override '--per-thread'. target->per_thread = false and
990147c508fSJin Yao 	 * target->system_wide = true.
991147c508fSJin Yao 	 *
992147c508fSJin Yao 	 * If specify '--per-thread' only to perf record,
993147c508fSJin Yao 	 * target->per_thread = true and target->system_wide = false.
994147c508fSJin Yao 	 *
995147c508fSJin Yao 	 * So target->per_thread && target->system_wide is false.
996147c508fSJin Yao 	 * For perf record, thread_map__new_str doesn't call
997147c508fSJin Yao 	 * thread_map__new_all_cpus. That will keep perf record's
998147c508fSJin Yao 	 * current behavior.
999147c508fSJin Yao 	 *
1000147c508fSJin Yao 	 * For perf stat, it allows the case that target->per_thread and
1001147c508fSJin Yao 	 * target->system_wide are all true. It means to collect system-wide
1002147c508fSJin Yao 	 * per-thread data. thread_map__new_str will call
1003147c508fSJin Yao 	 * thread_map__new_all_cpus to enumerate all threads.
1004147c508fSJin Yao 	 */
100573c0ca1eSJin Yao 	threads = thread_map__new_str(target->pid, target->tid, target->uid,
1006147c508fSJin Yao 				      all_threads);
100774bfd2b2SAdrian Hunter 
100874bfd2b2SAdrian Hunter 	if (!threads)
10097e2ed097SArnaldo Carvalho de Melo 		return -1;
10107e2ed097SArnaldo Carvalho de Melo 
10119c105fbcSDongsheng Yang 	if (target__uses_dummy_map(target))
1012397721e0SJiri Olsa 		cpus = perf_cpu_map__dummy_new();
1013879d77d0SNamhyung Kim 	else
10149c3516d1SJiri Olsa 		cpus = perf_cpu_map__new(target->cpu_list);
10157e2ed097SArnaldo Carvalho de Melo 
101674bfd2b2SAdrian Hunter 	if (!cpus)
10177e2ed097SArnaldo Carvalho de Melo 		goto out_delete_threads;
10187e2ed097SArnaldo Carvalho de Melo 
1019ec903f26SJiri Olsa 	evlist->core.has_user_cpus = !!target->cpu_list;
1020ec9a77a7SAdrian Hunter 
1021453fa030SJiri Olsa 	perf_evlist__set_maps(&evlist->core, cpus, threads);
1022d5bc056eSAdrian Hunter 
1023bfd1b83dSNamhyung Kim 	/* as evlist now has references, put count here */
1024bfd1b83dSNamhyung Kim 	perf_cpu_map__put(cpus);
1025bfd1b83dSNamhyung Kim 	perf_thread_map__put(threads);
1026bfd1b83dSNamhyung Kim 
1027d5bc056eSAdrian Hunter 	return 0;
10287e2ed097SArnaldo Carvalho de Melo 
10297e2ed097SArnaldo Carvalho de Melo out_delete_threads:
10307836e52eSJiri Olsa 	perf_thread_map__put(threads);
10317e2ed097SArnaldo Carvalho de Melo 	return -1;
10327e2ed097SArnaldo Carvalho de Melo }
10337e2ed097SArnaldo Carvalho de Melo 
103424bf91a7SArnaldo Carvalho de Melo int evlist__apply_filters(struct evlist *evlist, struct evsel **err_evsel)
10350a102479SFrederic Weisbecker {
103632dcd021SJiri Olsa 	struct evsel *evsel;
1037745cefc5SArnaldo Carvalho de Melo 	int err = 0;
10380a102479SFrederic Weisbecker 
1039e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, evsel) {
1040745cefc5SArnaldo Carvalho de Melo 		if (evsel->filter == NULL)
10410a102479SFrederic Weisbecker 			continue;
1042745cefc5SArnaldo Carvalho de Melo 
1043d988d5eeSKan Liang 		/*
1044d988d5eeSKan Liang 		 * filters only work for tracepoint event, which doesn't have cpu limit.
1045d988d5eeSKan Liang 		 * So evlist and evsel should always be same.
1046d988d5eeSKan Liang 		 */
1047a00571fdSJiri Olsa 		err = perf_evsel__apply_filter(&evsel->core, evsel->filter);
104823d4aad4SArnaldo Carvalho de Melo 		if (err) {
104923d4aad4SArnaldo Carvalho de Melo 			*err_evsel = evsel;
1050745cefc5SArnaldo Carvalho de Melo 			break;
10510a102479SFrederic Weisbecker 		}
105223d4aad4SArnaldo Carvalho de Melo 	}
10530a102479SFrederic Weisbecker 
1054745cefc5SArnaldo Carvalho de Melo 	return err;
1055745cefc5SArnaldo Carvalho de Melo }
1056745cefc5SArnaldo Carvalho de Melo 
105724bf91a7SArnaldo Carvalho de Melo int evlist__set_tp_filter(struct evlist *evlist, const char *filter)
1058745cefc5SArnaldo Carvalho de Melo {
105932dcd021SJiri Olsa 	struct evsel *evsel;
1060745cefc5SArnaldo Carvalho de Melo 	int err = 0;
1061745cefc5SArnaldo Carvalho de Melo 
106205cea449SArnaldo Carvalho de Melo 	if (filter == NULL)
106305cea449SArnaldo Carvalho de Melo 		return -1;
106405cea449SArnaldo Carvalho de Melo 
1065e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, evsel) {
10661fc632ceSJiri Olsa 		if (evsel->core.attr.type != PERF_TYPE_TRACEPOINT)
1067fdf14720SWang Nan 			continue;
1068fdf14720SWang Nan 
1069ad681adfSArnaldo Carvalho de Melo 		err = evsel__set_filter(evsel, filter);
1070745cefc5SArnaldo Carvalho de Melo 		if (err)
1071745cefc5SArnaldo Carvalho de Melo 			break;
1072745cefc5SArnaldo Carvalho de Melo 	}
1073745cefc5SArnaldo Carvalho de Melo 
1074745cefc5SArnaldo Carvalho de Melo 	return err;
10750a102479SFrederic Weisbecker }
107674429964SFrederic Weisbecker 
107724bf91a7SArnaldo Carvalho de Melo int evlist__append_tp_filter(struct evlist *evlist, const char *filter)
107853c92f73SArnaldo Carvalho de Melo {
107953c92f73SArnaldo Carvalho de Melo 	struct evsel *evsel;
108053c92f73SArnaldo Carvalho de Melo 	int err = 0;
108153c92f73SArnaldo Carvalho de Melo 
108253c92f73SArnaldo Carvalho de Melo 	if (filter == NULL)
108353c92f73SArnaldo Carvalho de Melo 		return -1;
108453c92f73SArnaldo Carvalho de Melo 
108553c92f73SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, evsel) {
108653c92f73SArnaldo Carvalho de Melo 		if (evsel->core.attr.type != PERF_TYPE_TRACEPOINT)
108753c92f73SArnaldo Carvalho de Melo 			continue;
108853c92f73SArnaldo Carvalho de Melo 
1089ad681adfSArnaldo Carvalho de Melo 		err = evsel__append_tp_filter(evsel, filter);
109053c92f73SArnaldo Carvalho de Melo 		if (err)
109153c92f73SArnaldo Carvalho de Melo 			break;
109253c92f73SArnaldo Carvalho de Melo 	}
109353c92f73SArnaldo Carvalho de Melo 
109453c92f73SArnaldo Carvalho de Melo 	return err;
109553c92f73SArnaldo Carvalho de Melo }
109653c92f73SArnaldo Carvalho de Melo 
1097da949f50SArnaldo Carvalho de Melo char *asprintf__tp_filter_pids(size_t npids, pid_t *pids)
1098cfd70a26SArnaldo Carvalho de Melo {
1099cfd70a26SArnaldo Carvalho de Melo 	char *filter;
1100be199adaSArnaldo Carvalho de Melo 	size_t i;
1101cfd70a26SArnaldo Carvalho de Melo 
1102be199adaSArnaldo Carvalho de Melo 	for (i = 0; i < npids; ++i) {
1103be199adaSArnaldo Carvalho de Melo 		if (i == 0) {
1104be199adaSArnaldo Carvalho de Melo 			if (asprintf(&filter, "common_pid != %d", pids[i]) < 0)
110505cea449SArnaldo Carvalho de Melo 				return NULL;
1106be199adaSArnaldo Carvalho de Melo 		} else {
1107be199adaSArnaldo Carvalho de Melo 			char *tmp;
1108be199adaSArnaldo Carvalho de Melo 
1109be199adaSArnaldo Carvalho de Melo 			if (asprintf(&tmp, "%s && common_pid != %d", filter, pids[i]) < 0)
1110be199adaSArnaldo Carvalho de Melo 				goto out_free;
1111be199adaSArnaldo Carvalho de Melo 
1112be199adaSArnaldo Carvalho de Melo 			free(filter);
1113be199adaSArnaldo Carvalho de Melo 			filter = tmp;
1114be199adaSArnaldo Carvalho de Melo 		}
1115be199adaSArnaldo Carvalho de Melo 	}
1116cfd70a26SArnaldo Carvalho de Melo 
111705cea449SArnaldo Carvalho de Melo 	return filter;
1118be199adaSArnaldo Carvalho de Melo out_free:
1119cfd70a26SArnaldo Carvalho de Melo 	free(filter);
112005cea449SArnaldo Carvalho de Melo 	return NULL;
112105cea449SArnaldo Carvalho de Melo }
112205cea449SArnaldo Carvalho de Melo 
112324bf91a7SArnaldo Carvalho de Melo int evlist__set_tp_filter_pids(struct evlist *evlist, size_t npids, pid_t *pids)
112405cea449SArnaldo Carvalho de Melo {
112505cea449SArnaldo Carvalho de Melo 	char *filter = asprintf__tp_filter_pids(npids, pids);
112624bf91a7SArnaldo Carvalho de Melo 	int ret = evlist__set_tp_filter(evlist, filter);
112705cea449SArnaldo Carvalho de Melo 
112805cea449SArnaldo Carvalho de Melo 	free(filter);
1129cfd70a26SArnaldo Carvalho de Melo 	return ret;
1130cfd70a26SArnaldo Carvalho de Melo }
1131cfd70a26SArnaldo Carvalho de Melo 
113224bf91a7SArnaldo Carvalho de Melo int evlist__set_tp_filter_pid(struct evlist *evlist, pid_t pid)
1133be199adaSArnaldo Carvalho de Melo {
113424bf91a7SArnaldo Carvalho de Melo 	return evlist__set_tp_filter_pids(evlist, 1, &pid);
1135be199adaSArnaldo Carvalho de Melo }
1136be199adaSArnaldo Carvalho de Melo 
113724bf91a7SArnaldo Carvalho de Melo int evlist__append_tp_filter_pids(struct evlist *evlist, size_t npids, pid_t *pids)
11381827ab5bSArnaldo Carvalho de Melo {
11391827ab5bSArnaldo Carvalho de Melo 	char *filter = asprintf__tp_filter_pids(npids, pids);
114024bf91a7SArnaldo Carvalho de Melo 	int ret = evlist__append_tp_filter(evlist, filter);
11411827ab5bSArnaldo Carvalho de Melo 
11421827ab5bSArnaldo Carvalho de Melo 	free(filter);
11431827ab5bSArnaldo Carvalho de Melo 	return ret;
11441827ab5bSArnaldo Carvalho de Melo }
11451827ab5bSArnaldo Carvalho de Melo 
114624bf91a7SArnaldo Carvalho de Melo int evlist__append_tp_filter_pid(struct evlist *evlist, pid_t pid)
11471827ab5bSArnaldo Carvalho de Melo {
114824bf91a7SArnaldo Carvalho de Melo 	return evlist__append_tp_filter_pids(evlist, 1, &pid);
11491827ab5bSArnaldo Carvalho de Melo }
11501827ab5bSArnaldo Carvalho de Melo 
1151b3c2cc2bSArnaldo Carvalho de Melo bool evlist__valid_sample_type(struct evlist *evlist)
115274429964SFrederic Weisbecker {
115332dcd021SJiri Olsa 	struct evsel *pos;
1154c2a70653SArnaldo Carvalho de Melo 
11556484d2f9SJiri Olsa 	if (evlist->core.nr_entries == 1)
115675562573SAdrian Hunter 		return true;
115775562573SAdrian Hunter 
115875562573SAdrian Hunter 	if (evlist->id_pos < 0 || evlist->is_pos < 0)
115975562573SAdrian Hunter 		return false;
116075562573SAdrian Hunter 
1161e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, pos) {
116275562573SAdrian Hunter 		if (pos->id_pos != evlist->id_pos ||
116375562573SAdrian Hunter 		    pos->is_pos != evlist->is_pos)
1164c2a70653SArnaldo Carvalho de Melo 			return false;
116574429964SFrederic Weisbecker 	}
116674429964SFrederic Weisbecker 
1167c2a70653SArnaldo Carvalho de Melo 	return true;
1168c2a70653SArnaldo Carvalho de Melo }
1169c2a70653SArnaldo Carvalho de Melo 
1170b3c2cc2bSArnaldo Carvalho de Melo u64 __evlist__combined_sample_type(struct evlist *evlist)
1171c2a70653SArnaldo Carvalho de Melo {
117232dcd021SJiri Olsa 	struct evsel *evsel;
117375562573SAdrian Hunter 
117475562573SAdrian Hunter 	if (evlist->combined_sample_type)
117575562573SAdrian Hunter 		return evlist->combined_sample_type;
117675562573SAdrian Hunter 
1177e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, evsel)
11781fc632ceSJiri Olsa 		evlist->combined_sample_type |= evsel->core.attr.sample_type;
117975562573SAdrian Hunter 
118075562573SAdrian Hunter 	return evlist->combined_sample_type;
118175562573SAdrian Hunter }
118275562573SAdrian Hunter 
1183b3c2cc2bSArnaldo Carvalho de Melo u64 evlist__combined_sample_type(struct evlist *evlist)
118475562573SAdrian Hunter {
118575562573SAdrian Hunter 	evlist->combined_sample_type = 0;
1186b3c2cc2bSArnaldo Carvalho de Melo 	return __evlist__combined_sample_type(evlist);
1187c2a70653SArnaldo Carvalho de Melo }
1188c2a70653SArnaldo Carvalho de Melo 
118992c7d7cdSArnaldo Carvalho de Melo u64 evlist__combined_branch_type(struct evlist *evlist)
119098df858eSAndi Kleen {
119132dcd021SJiri Olsa 	struct evsel *evsel;
119298df858eSAndi Kleen 	u64 branch_type = 0;
119398df858eSAndi Kleen 
1194e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, evsel)
11951fc632ceSJiri Olsa 		branch_type |= evsel->core.attr.branch_sample_type;
119698df858eSAndi Kleen 	return branch_type;
119798df858eSAndi Kleen }
119898df858eSAndi Kleen 
119978e1bc25SArnaldo Carvalho de Melo bool evlist__valid_read_format(struct evlist *evlist)
12009ede473cSJiri Olsa {
1201515dbe48SJiri Olsa 	struct evsel *first = evlist__first(evlist), *pos = first;
12021fc632ceSJiri Olsa 	u64 read_format = first->core.attr.read_format;
12031fc632ceSJiri Olsa 	u64 sample_type = first->core.attr.sample_type;
12049ede473cSJiri Olsa 
1205e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, pos) {
120694d3820fSAdrian Hunter 		if (read_format != pos->core.attr.read_format) {
120794d3820fSAdrian Hunter 			pr_debug("Read format differs %#" PRIx64 " vs %#" PRIx64 "\n",
120894d3820fSAdrian Hunter 				 read_format, (u64)pos->core.attr.read_format);
120994d3820fSAdrian Hunter 		}
12109ede473cSJiri Olsa 	}
12119ede473cSJiri Olsa 
12129ede473cSJiri Olsa 	/* PERF_SAMPLE_READ imples PERF_FORMAT_ID. */
12139ede473cSJiri Olsa 	if ((sample_type & PERF_SAMPLE_READ) &&
12149ede473cSJiri Olsa 	    !(read_format & PERF_FORMAT_ID)) {
12159ede473cSJiri Olsa 		return false;
12169ede473cSJiri Olsa 	}
12179ede473cSJiri Olsa 
12189ede473cSJiri Olsa 	return true;
12199ede473cSJiri Olsa }
12209ede473cSJiri Olsa 
12211420ba2fSArnaldo Carvalho de Melo u16 evlist__id_hdr_size(struct evlist *evlist)
122281e36bffSArnaldo Carvalho de Melo {
1223515dbe48SJiri Olsa 	struct evsel *first = evlist__first(evlist);
122481e36bffSArnaldo Carvalho de Melo 	struct perf_sample *data;
122581e36bffSArnaldo Carvalho de Melo 	u64 sample_type;
122681e36bffSArnaldo Carvalho de Melo 	u16 size = 0;
122781e36bffSArnaldo Carvalho de Melo 
12281fc632ceSJiri Olsa 	if (!first->core.attr.sample_id_all)
122981e36bffSArnaldo Carvalho de Melo 		goto out;
123081e36bffSArnaldo Carvalho de Melo 
12311fc632ceSJiri Olsa 	sample_type = first->core.attr.sample_type;
123281e36bffSArnaldo Carvalho de Melo 
123381e36bffSArnaldo Carvalho de Melo 	if (sample_type & PERF_SAMPLE_TID)
123481e36bffSArnaldo Carvalho de Melo 		size += sizeof(data->tid) * 2;
123581e36bffSArnaldo Carvalho de Melo 
123681e36bffSArnaldo Carvalho de Melo        if (sample_type & PERF_SAMPLE_TIME)
123781e36bffSArnaldo Carvalho de Melo 		size += sizeof(data->time);
123881e36bffSArnaldo Carvalho de Melo 
123981e36bffSArnaldo Carvalho de Melo 	if (sample_type & PERF_SAMPLE_ID)
124081e36bffSArnaldo Carvalho de Melo 		size += sizeof(data->id);
124181e36bffSArnaldo Carvalho de Melo 
124281e36bffSArnaldo Carvalho de Melo 	if (sample_type & PERF_SAMPLE_STREAM_ID)
124381e36bffSArnaldo Carvalho de Melo 		size += sizeof(data->stream_id);
124481e36bffSArnaldo Carvalho de Melo 
124581e36bffSArnaldo Carvalho de Melo 	if (sample_type & PERF_SAMPLE_CPU)
124681e36bffSArnaldo Carvalho de Melo 		size += sizeof(data->cpu) * 2;
124775562573SAdrian Hunter 
124875562573SAdrian Hunter 	if (sample_type & PERF_SAMPLE_IDENTIFIER)
124975562573SAdrian Hunter 		size += sizeof(data->id);
125081e36bffSArnaldo Carvalho de Melo out:
125181e36bffSArnaldo Carvalho de Melo 	return size;
125281e36bffSArnaldo Carvalho de Melo }
125381e36bffSArnaldo Carvalho de Melo 
12548cedf3a5SArnaldo Carvalho de Melo bool evlist__valid_sample_id_all(struct evlist *evlist)
1255c2a70653SArnaldo Carvalho de Melo {
1256515dbe48SJiri Olsa 	struct evsel *first = evlist__first(evlist), *pos = first;
1257c2a70653SArnaldo Carvalho de Melo 
1258e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry_continue(evlist, pos) {
12591fc632ceSJiri Olsa 		if (first->core.attr.sample_id_all != pos->core.attr.sample_id_all)
1260c2a70653SArnaldo Carvalho de Melo 			return false;
1261c2a70653SArnaldo Carvalho de Melo 	}
1262c2a70653SArnaldo Carvalho de Melo 
1263c2a70653SArnaldo Carvalho de Melo 	return true;
126474429964SFrederic Weisbecker }
126574429964SFrederic Weisbecker 
12668cedf3a5SArnaldo Carvalho de Melo bool evlist__sample_id_all(struct evlist *evlist)
126774429964SFrederic Weisbecker {
1268515dbe48SJiri Olsa 	struct evsel *first = evlist__first(evlist);
12691fc632ceSJiri Olsa 	return first->core.attr.sample_id_all;
127074429964SFrederic Weisbecker }
127181cce8deSArnaldo Carvalho de Melo 
1272900c8eadSArnaldo Carvalho de Melo void evlist__set_selected(struct evlist *evlist, struct evsel *evsel)
127381cce8deSArnaldo Carvalho de Melo {
127481cce8deSArnaldo Carvalho de Melo 	evlist->selected = evsel;
127581cce8deSArnaldo Carvalho de Melo }
1276727ab04eSArnaldo Carvalho de Melo 
1277750b4edeSJiri Olsa void evlist__close(struct evlist *evlist)
1278a74b4b66SNamhyung Kim {
127932dcd021SJiri Olsa 	struct evsel *evsel;
12807736627bSAndi Kleen 	struct affinity affinity;
12817736627bSAndi Kleen 	int cpu, i;
1282a74b4b66SNamhyung Kim 
12837736627bSAndi Kleen 	/*
12847736627bSAndi Kleen 	 * With perf record core.cpus is usually NULL.
12857736627bSAndi Kleen 	 * Use the old method to handle this for now.
12867736627bSAndi Kleen 	 */
12877736627bSAndi Kleen 	if (!evlist->core.cpus) {
1288475fb533SAndi Kleen 		evlist__for_each_entry_reverse(evlist, evsel)
128988761fa1SJiri Olsa 			evsel__close(evsel);
12907736627bSAndi Kleen 		return;
12917736627bSAndi Kleen 	}
12927736627bSAndi Kleen 
12937736627bSAndi Kleen 	if (affinity__setup(&affinity) < 0)
12947736627bSAndi Kleen 		return;
12957736627bSAndi Kleen 	evlist__for_each_cpu(evlist, i, cpu) {
12967736627bSAndi Kleen 		affinity__set(&affinity, cpu);
12977736627bSAndi Kleen 
12987736627bSAndi Kleen 		evlist__for_each_entry_reverse(evlist, evsel) {
12997736627bSAndi Kleen 			if (evsel__cpu_iter_skip(evsel, cpu))
13007736627bSAndi Kleen 			    continue;
13017736627bSAndi Kleen 			perf_evsel__close_cpu(&evsel->core, evsel->cpu_iter - 1);
13027736627bSAndi Kleen 		}
13037736627bSAndi Kleen 	}
13047736627bSAndi Kleen 	affinity__cleanup(&affinity);
13057736627bSAndi Kleen 	evlist__for_each_entry_reverse(evlist, evsel) {
13067736627bSAndi Kleen 		perf_evsel__free_fd(&evsel->core);
13077736627bSAndi Kleen 		perf_evsel__free_id(&evsel->core);
13087736627bSAndi Kleen 	}
1309a74b4b66SNamhyung Kim }
1310a74b4b66SNamhyung Kim 
13117748bb71SArnaldo Carvalho de Melo static int evlist__create_syswide_maps(struct evlist *evlist)
13124112eb18SArnaldo Carvalho de Melo {
1313f854839bSJiri Olsa 	struct perf_cpu_map *cpus;
13149749b90eSJiri Olsa 	struct perf_thread_map *threads;
13154112eb18SArnaldo Carvalho de Melo 	int err = -ENOMEM;
13164112eb18SArnaldo Carvalho de Melo 
13174112eb18SArnaldo Carvalho de Melo 	/*
13184112eb18SArnaldo Carvalho de Melo 	 * Try reading /sys/devices/system/cpu/online to get
13194112eb18SArnaldo Carvalho de Melo 	 * an all cpus map.
13204112eb18SArnaldo Carvalho de Melo 	 *
13214112eb18SArnaldo Carvalho de Melo 	 * FIXME: -ENOMEM is the best we can do here, the cpu_map
13224112eb18SArnaldo Carvalho de Melo 	 * code needs an overhaul to properly forward the
13234112eb18SArnaldo Carvalho de Melo 	 * error, and we may not want to do that fallback to a
13244112eb18SArnaldo Carvalho de Melo 	 * default cpu identity map :-\
13254112eb18SArnaldo Carvalho de Melo 	 */
13269c3516d1SJiri Olsa 	cpus = perf_cpu_map__new(NULL);
13278c0498b6SAdrian Hunter 	if (!cpus)
13284112eb18SArnaldo Carvalho de Melo 		goto out;
13294112eb18SArnaldo Carvalho de Melo 
13304b49cce2SJiri Olsa 	threads = perf_thread_map__new_dummy();
13318c0498b6SAdrian Hunter 	if (!threads)
13328c0498b6SAdrian Hunter 		goto out_put;
13334112eb18SArnaldo Carvalho de Melo 
1334453fa030SJiri Olsa 	perf_evlist__set_maps(&evlist->core, cpus, threads);
1335bfd1b83dSNamhyung Kim 
1336bfd1b83dSNamhyung Kim 	perf_thread_map__put(threads);
13378c0498b6SAdrian Hunter out_put:
133838f01d8dSJiri Olsa 	perf_cpu_map__put(cpus);
1339bfd1b83dSNamhyung Kim out:
1340bfd1b83dSNamhyung Kim 	return err;
13414112eb18SArnaldo Carvalho de Melo }
13424112eb18SArnaldo Carvalho de Melo 
1343474ddc4cSJiri Olsa int evlist__open(struct evlist *evlist)
1344727ab04eSArnaldo Carvalho de Melo {
134532dcd021SJiri Olsa 	struct evsel *evsel;
1346a74b4b66SNamhyung Kim 	int err;
1347727ab04eSArnaldo Carvalho de Melo 
13484112eb18SArnaldo Carvalho de Melo 	/*
13494112eb18SArnaldo Carvalho de Melo 	 * Default: one fd per CPU, all threads, aka systemwide
13504112eb18SArnaldo Carvalho de Melo 	 * as sys_perf_event_open(cpu = -1, thread = -1) is EINVAL
13514112eb18SArnaldo Carvalho de Melo 	 */
135203617c22SJiri Olsa 	if (evlist->core.threads == NULL && evlist->core.cpus == NULL) {
13537748bb71SArnaldo Carvalho de Melo 		err = evlist__create_syswide_maps(evlist);
13544112eb18SArnaldo Carvalho de Melo 		if (err < 0)
13554112eb18SArnaldo Carvalho de Melo 			goto out_err;
13564112eb18SArnaldo Carvalho de Melo 	}
13574112eb18SArnaldo Carvalho de Melo 
13580a7e7ec9SArnaldo Carvalho de Melo 	evlist__update_id_pos(evlist);
1359733cd2feSAdrian Hunter 
1360e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, evsel) {
1361af663bd0SJiri Olsa 		err = evsel__open(evsel, evsel->core.cpus, evsel->core.threads);
1362727ab04eSArnaldo Carvalho de Melo 		if (err < 0)
1363727ab04eSArnaldo Carvalho de Melo 			goto out_err;
1364727ab04eSArnaldo Carvalho de Melo 	}
1365727ab04eSArnaldo Carvalho de Melo 
1366727ab04eSArnaldo Carvalho de Melo 	return 0;
1367727ab04eSArnaldo Carvalho de Melo out_err:
1368750b4edeSJiri Olsa 	evlist__close(evlist);
136941c21a68SNamhyung Kim 	errno = -err;
1370727ab04eSArnaldo Carvalho de Melo 	return err;
1371727ab04eSArnaldo Carvalho de Melo }
137235b9d88eSArnaldo Carvalho de Melo 
13737b392ef0SArnaldo Carvalho de Melo int evlist__prepare_workload(struct evlist *evlist, struct target *target, const char *argv[],
13747b392ef0SArnaldo Carvalho de Melo 			     bool pipe_output, void (*exec_error)(int signo, siginfo_t *info, void *ucontext))
137535b9d88eSArnaldo Carvalho de Melo {
137635b9d88eSArnaldo Carvalho de Melo 	int child_ready_pipe[2], go_pipe[2];
137735b9d88eSArnaldo Carvalho de Melo 	char bf;
137835b9d88eSArnaldo Carvalho de Melo 
137935b9d88eSArnaldo Carvalho de Melo 	if (pipe(child_ready_pipe) < 0) {
138035b9d88eSArnaldo Carvalho de Melo 		perror("failed to create 'ready' pipe");
138135b9d88eSArnaldo Carvalho de Melo 		return -1;
138235b9d88eSArnaldo Carvalho de Melo 	}
138335b9d88eSArnaldo Carvalho de Melo 
138435b9d88eSArnaldo Carvalho de Melo 	if (pipe(go_pipe) < 0) {
138535b9d88eSArnaldo Carvalho de Melo 		perror("failed to create 'go' pipe");
138635b9d88eSArnaldo Carvalho de Melo 		goto out_close_ready_pipe;
138735b9d88eSArnaldo Carvalho de Melo 	}
138835b9d88eSArnaldo Carvalho de Melo 
138935b9d88eSArnaldo Carvalho de Melo 	evlist->workload.pid = fork();
139035b9d88eSArnaldo Carvalho de Melo 	if (evlist->workload.pid < 0) {
139135b9d88eSArnaldo Carvalho de Melo 		perror("failed to fork");
139235b9d88eSArnaldo Carvalho de Melo 		goto out_close_pipes;
139335b9d88eSArnaldo Carvalho de Melo 	}
139435b9d88eSArnaldo Carvalho de Melo 
139535b9d88eSArnaldo Carvalho de Melo 	if (!evlist->workload.pid) {
13965f1c4225SArnaldo Carvalho de Melo 		int ret;
13975f1c4225SArnaldo Carvalho de Melo 
1398119fa3c9SNamhyung Kim 		if (pipe_output)
139935b9d88eSArnaldo Carvalho de Melo 			dup2(2, 1);
140035b9d88eSArnaldo Carvalho de Melo 
14010817df08SDavid Ahern 		signal(SIGTERM, SIG_DFL);
14020817df08SDavid Ahern 
140335b9d88eSArnaldo Carvalho de Melo 		close(child_ready_pipe[0]);
140435b9d88eSArnaldo Carvalho de Melo 		close(go_pipe[1]);
140535b9d88eSArnaldo Carvalho de Melo 		fcntl(go_pipe[0], F_SETFD, FD_CLOEXEC);
140635b9d88eSArnaldo Carvalho de Melo 
140735b9d88eSArnaldo Carvalho de Melo 		/*
140835b9d88eSArnaldo Carvalho de Melo 		 * Tell the parent we're ready to go
140935b9d88eSArnaldo Carvalho de Melo 		 */
141035b9d88eSArnaldo Carvalho de Melo 		close(child_ready_pipe[1]);
141135b9d88eSArnaldo Carvalho de Melo 
141235b9d88eSArnaldo Carvalho de Melo 		/*
141335b9d88eSArnaldo Carvalho de Melo 		 * Wait until the parent tells us to go.
141435b9d88eSArnaldo Carvalho de Melo 		 */
14155f1c4225SArnaldo Carvalho de Melo 		ret = read(go_pipe[0], &bf, 1);
14165f1c4225SArnaldo Carvalho de Melo 		/*
14175f1c4225SArnaldo Carvalho de Melo 		 * The parent will ask for the execvp() to be performed by
14185f1c4225SArnaldo Carvalho de Melo 		 * writing exactly one byte, in workload.cork_fd, usually via
14197b392ef0SArnaldo Carvalho de Melo 		 * evlist__start_workload().
14205f1c4225SArnaldo Carvalho de Melo 		 *
142120f86fc1SArnaldo Carvalho de Melo 		 * For cancelling the workload without actually running it,
14225f1c4225SArnaldo Carvalho de Melo 		 * the parent will just close workload.cork_fd, without writing
14235f1c4225SArnaldo Carvalho de Melo 		 * anything, i.e. read will return zero and we just exit()
14245f1c4225SArnaldo Carvalho de Melo 		 * here.
14255f1c4225SArnaldo Carvalho de Melo 		 */
14265f1c4225SArnaldo Carvalho de Melo 		if (ret != 1) {
14275f1c4225SArnaldo Carvalho de Melo 			if (ret == -1)
142835b9d88eSArnaldo Carvalho de Melo 				perror("unable to read pipe");
14295f1c4225SArnaldo Carvalho de Melo 			exit(ret);
14305f1c4225SArnaldo Carvalho de Melo 		}
143135b9d88eSArnaldo Carvalho de Melo 
143235b9d88eSArnaldo Carvalho de Melo 		execvp(argv[0], (char **)argv);
143335b9d88eSArnaldo Carvalho de Melo 
1434735f7e0bSArnaldo Carvalho de Melo 		if (exec_error) {
1435f33cbe72SArnaldo Carvalho de Melo 			union sigval val;
1436f33cbe72SArnaldo Carvalho de Melo 
1437f33cbe72SArnaldo Carvalho de Melo 			val.sival_int = errno;
1438f33cbe72SArnaldo Carvalho de Melo 			if (sigqueue(getppid(), SIGUSR1, val))
143935b9d88eSArnaldo Carvalho de Melo 				perror(argv[0]);
1440f33cbe72SArnaldo Carvalho de Melo 		} else
1441f33cbe72SArnaldo Carvalho de Melo 			perror(argv[0]);
144235b9d88eSArnaldo Carvalho de Melo 		exit(-1);
144335b9d88eSArnaldo Carvalho de Melo 	}
144435b9d88eSArnaldo Carvalho de Melo 
1445735f7e0bSArnaldo Carvalho de Melo 	if (exec_error) {
1446735f7e0bSArnaldo Carvalho de Melo 		struct sigaction act = {
1447735f7e0bSArnaldo Carvalho de Melo 			.sa_flags     = SA_SIGINFO,
1448735f7e0bSArnaldo Carvalho de Melo 			.sa_sigaction = exec_error,
1449735f7e0bSArnaldo Carvalho de Melo 		};
1450735f7e0bSArnaldo Carvalho de Melo 		sigaction(SIGUSR1, &act, NULL);
1451735f7e0bSArnaldo Carvalho de Melo 	}
1452735f7e0bSArnaldo Carvalho de Melo 
14531aaf63b1SArnaldo Carvalho de Melo 	if (target__none(target)) {
145403617c22SJiri Olsa 		if (evlist->core.threads == NULL) {
14551aaf63b1SArnaldo Carvalho de Melo 			fprintf(stderr, "FATAL: evlist->threads need to be set at this point (%s:%d).\n",
14561aaf63b1SArnaldo Carvalho de Melo 				__func__, __LINE__);
14571aaf63b1SArnaldo Carvalho de Melo 			goto out_close_pipes;
14581aaf63b1SArnaldo Carvalho de Melo 		}
145903617c22SJiri Olsa 		perf_thread_map__set_pid(evlist->core.threads, 0, evlist->workload.pid);
14601aaf63b1SArnaldo Carvalho de Melo 	}
146135b9d88eSArnaldo Carvalho de Melo 
146235b9d88eSArnaldo Carvalho de Melo 	close(child_ready_pipe[1]);
146335b9d88eSArnaldo Carvalho de Melo 	close(go_pipe[0]);
146435b9d88eSArnaldo Carvalho de Melo 	/*
146535b9d88eSArnaldo Carvalho de Melo 	 * wait for child to settle
146635b9d88eSArnaldo Carvalho de Melo 	 */
146735b9d88eSArnaldo Carvalho de Melo 	if (read(child_ready_pipe[0], &bf, 1) == -1) {
146835b9d88eSArnaldo Carvalho de Melo 		perror("unable to read pipe");
146935b9d88eSArnaldo Carvalho de Melo 		goto out_close_pipes;
147035b9d88eSArnaldo Carvalho de Melo 	}
147135b9d88eSArnaldo Carvalho de Melo 
1472bcf3145fSNamhyung Kim 	fcntl(go_pipe[1], F_SETFD, FD_CLOEXEC);
147335b9d88eSArnaldo Carvalho de Melo 	evlist->workload.cork_fd = go_pipe[1];
147435b9d88eSArnaldo Carvalho de Melo 	close(child_ready_pipe[0]);
147535b9d88eSArnaldo Carvalho de Melo 	return 0;
147635b9d88eSArnaldo Carvalho de Melo 
147735b9d88eSArnaldo Carvalho de Melo out_close_pipes:
147835b9d88eSArnaldo Carvalho de Melo 	close(go_pipe[0]);
147935b9d88eSArnaldo Carvalho de Melo 	close(go_pipe[1]);
148035b9d88eSArnaldo Carvalho de Melo out_close_ready_pipe:
148135b9d88eSArnaldo Carvalho de Melo 	close(child_ready_pipe[0]);
148235b9d88eSArnaldo Carvalho de Melo 	close(child_ready_pipe[1]);
148335b9d88eSArnaldo Carvalho de Melo 	return -1;
148435b9d88eSArnaldo Carvalho de Melo }
148535b9d88eSArnaldo Carvalho de Melo 
14867b392ef0SArnaldo Carvalho de Melo int evlist__start_workload(struct evlist *evlist)
148735b9d88eSArnaldo Carvalho de Melo {
148835b9d88eSArnaldo Carvalho de Melo 	if (evlist->workload.cork_fd > 0) {
1489b3824404SDavid Ahern 		char bf = 0;
1490bcf3145fSNamhyung Kim 		int ret;
149135b9d88eSArnaldo Carvalho de Melo 		/*
149235b9d88eSArnaldo Carvalho de Melo 		 * Remove the cork, let it rip!
149335b9d88eSArnaldo Carvalho de Melo 		 */
1494bcf3145fSNamhyung Kim 		ret = write(evlist->workload.cork_fd, &bf, 1);
1495bcf3145fSNamhyung Kim 		if (ret < 0)
1496e978be9eSSoramichi Akiyama 			perror("unable to write to pipe");
1497bcf3145fSNamhyung Kim 
1498bcf3145fSNamhyung Kim 		close(evlist->workload.cork_fd);
1499bcf3145fSNamhyung Kim 		return ret;
150035b9d88eSArnaldo Carvalho de Melo 	}
150135b9d88eSArnaldo Carvalho de Melo 
150235b9d88eSArnaldo Carvalho de Melo 	return 0;
150335b9d88eSArnaldo Carvalho de Melo }
1504cb0b29e0SArnaldo Carvalho de Melo 
15052a6599cdSArnaldo Carvalho de Melo int evlist__parse_sample(struct evlist *evlist, union perf_event *event, struct perf_sample *sample)
1506cb0b29e0SArnaldo Carvalho de Melo {
15073ccf8a7bSArnaldo Carvalho de Melo 	struct evsel *evsel = evlist__event2evsel(evlist, event);
150875562573SAdrian Hunter 
150975562573SAdrian Hunter 	if (!evsel)
151075562573SAdrian Hunter 		return -EFAULT;
15116b6017a2SArnaldo Carvalho de Melo 	return evsel__parse_sample(evsel, event, sample);
1512cb0b29e0SArnaldo Carvalho de Melo }
151378f067b3SArnaldo Carvalho de Melo 
15142a6599cdSArnaldo Carvalho de Melo int evlist__parse_sample_timestamp(struct evlist *evlist, union perf_event *event, u64 *timestamp)
151501468120SJiri Olsa {
15163ccf8a7bSArnaldo Carvalho de Melo 	struct evsel *evsel = evlist__event2evsel(evlist, event);
151701468120SJiri Olsa 
151801468120SJiri Olsa 	if (!evsel)
151901468120SJiri Olsa 		return -EFAULT;
15206b6017a2SArnaldo Carvalho de Melo 	return evsel__parse_sample_timestamp(evsel, event, timestamp);
152101468120SJiri Olsa }
152201468120SJiri Olsa 
1523d1f249ecSArnaldo Carvalho de Melo int evlist__strerror_open(struct evlist *evlist, int err, char *buf, size_t size)
1524a8f23d8fSArnaldo Carvalho de Melo {
1525a8f23d8fSArnaldo Carvalho de Melo 	int printed, value;
1526c8b5f2c9SArnaldo Carvalho de Melo 	char sbuf[STRERR_BUFSIZE], *emsg = str_error_r(err, sbuf, sizeof(sbuf));
1527a8f23d8fSArnaldo Carvalho de Melo 
1528a8f23d8fSArnaldo Carvalho de Melo 	switch (err) {
1529a8f23d8fSArnaldo Carvalho de Melo 	case EACCES:
1530a8f23d8fSArnaldo Carvalho de Melo 	case EPERM:
1531a8f23d8fSArnaldo Carvalho de Melo 		printed = scnprintf(buf, size,
1532a8f23d8fSArnaldo Carvalho de Melo 				    "Error:\t%s.\n"
1533a8f23d8fSArnaldo Carvalho de Melo 				    "Hint:\tCheck /proc/sys/kernel/perf_event_paranoid setting.", emsg);
1534a8f23d8fSArnaldo Carvalho de Melo 
15351a47245dSAdrian Hunter 		value = perf_event_paranoid();
1536a8f23d8fSArnaldo Carvalho de Melo 
1537a8f23d8fSArnaldo Carvalho de Melo 		printed += scnprintf(buf + printed, size - printed, "\nHint:\t");
1538a8f23d8fSArnaldo Carvalho de Melo 
1539a8f23d8fSArnaldo Carvalho de Melo 		if (value >= 2) {
1540a8f23d8fSArnaldo Carvalho de Melo 			printed += scnprintf(buf + printed, size - printed,
1541a8f23d8fSArnaldo Carvalho de Melo 					     "For your workloads it needs to be <= 1\nHint:\t");
1542a8f23d8fSArnaldo Carvalho de Melo 		}
1543a8f23d8fSArnaldo Carvalho de Melo 		printed += scnprintf(buf + printed, size - printed,
15445229e366SArnaldo Carvalho de Melo 				     "For system wide tracing it needs to be set to -1.\n");
1545a8f23d8fSArnaldo Carvalho de Melo 
1546a8f23d8fSArnaldo Carvalho de Melo 		printed += scnprintf(buf + printed, size - printed,
15475229e366SArnaldo Carvalho de Melo 				    "Hint:\tTry: 'sudo sh -c \"echo -1 > /proc/sys/kernel/perf_event_paranoid\"'\n"
15485229e366SArnaldo Carvalho de Melo 				    "Hint:\tThe current value is %d.", value);
1549a8f23d8fSArnaldo Carvalho de Melo 		break;
1550d9aade7fSArnaldo Carvalho de Melo 	case EINVAL: {
1551515dbe48SJiri Olsa 		struct evsel *first = evlist__first(evlist);
1552d9aade7fSArnaldo Carvalho de Melo 		int max_freq;
1553d9aade7fSArnaldo Carvalho de Melo 
1554d9aade7fSArnaldo Carvalho de Melo 		if (sysctl__read_int("kernel/perf_event_max_sample_rate", &max_freq) < 0)
1555d9aade7fSArnaldo Carvalho de Melo 			goto out_default;
1556d9aade7fSArnaldo Carvalho de Melo 
15571fc632ceSJiri Olsa 		if (first->core.attr.sample_freq < (u64)max_freq)
1558d9aade7fSArnaldo Carvalho de Melo 			goto out_default;
1559d9aade7fSArnaldo Carvalho de Melo 
1560d9aade7fSArnaldo Carvalho de Melo 		printed = scnprintf(buf, size,
1561d9aade7fSArnaldo Carvalho de Melo 				    "Error:\t%s.\n"
1562d9aade7fSArnaldo Carvalho de Melo 				    "Hint:\tCheck /proc/sys/kernel/perf_event_max_sample_rate.\n"
1563d9aade7fSArnaldo Carvalho de Melo 				    "Hint:\tThe current value is %d and %" PRIu64 " is being requested.",
15641fc632ceSJiri Olsa 				    emsg, max_freq, first->core.attr.sample_freq);
1565d9aade7fSArnaldo Carvalho de Melo 		break;
1566d9aade7fSArnaldo Carvalho de Melo 	}
1567a8f23d8fSArnaldo Carvalho de Melo 	default:
1568d9aade7fSArnaldo Carvalho de Melo out_default:
1569a8f23d8fSArnaldo Carvalho de Melo 		scnprintf(buf, size, "%s", emsg);
1570a8f23d8fSArnaldo Carvalho de Melo 		break;
1571a8f23d8fSArnaldo Carvalho de Melo 	}
1572a8f23d8fSArnaldo Carvalho de Melo 
1573a8f23d8fSArnaldo Carvalho de Melo 	return 0;
1574a8f23d8fSArnaldo Carvalho de Melo }
1575a025e4f0SAdrian Hunter 
1576d1f249ecSArnaldo Carvalho de Melo int evlist__strerror_mmap(struct evlist *evlist, int err, char *buf, size_t size)
1577956fa571SArnaldo Carvalho de Melo {
1578c8b5f2c9SArnaldo Carvalho de Melo 	char sbuf[STRERR_BUFSIZE], *emsg = str_error_r(err, sbuf, sizeof(sbuf));
1579f6fa4375SJiri Olsa 	int pages_attempted = evlist->core.mmap_len / 1024, pages_max_per_user, printed = 0;
1580956fa571SArnaldo Carvalho de Melo 
1581956fa571SArnaldo Carvalho de Melo 	switch (err) {
1582956fa571SArnaldo Carvalho de Melo 	case EPERM:
1583e5d4a290SArnaldo Carvalho de Melo 		sysctl__read_int("kernel/perf_event_mlock_kb", &pages_max_per_user);
1584e965bea1SArnaldo Carvalho de Melo 		printed += scnprintf(buf + printed, size - printed,
1585e965bea1SArnaldo Carvalho de Melo 				     "Error:\t%s.\n"
1586956fa571SArnaldo Carvalho de Melo 				     "Hint:\tCheck /proc/sys/kernel/perf_event_mlock_kb (%d kB) setting.\n"
1587e965bea1SArnaldo Carvalho de Melo 				     "Hint:\tTried using %zd kB.\n",
1588e5d4a290SArnaldo Carvalho de Melo 				     emsg, pages_max_per_user, pages_attempted);
1589e965bea1SArnaldo Carvalho de Melo 
1590e965bea1SArnaldo Carvalho de Melo 		if (pages_attempted >= pages_max_per_user) {
1591e965bea1SArnaldo Carvalho de Melo 			printed += scnprintf(buf + printed, size - printed,
1592e965bea1SArnaldo Carvalho de Melo 					     "Hint:\tTry 'sudo sh -c \"echo %d > /proc/sys/kernel/perf_event_mlock_kb\"', or\n",
1593e965bea1SArnaldo Carvalho de Melo 					     pages_max_per_user + pages_attempted);
1594e965bea1SArnaldo Carvalho de Melo 		}
1595e965bea1SArnaldo Carvalho de Melo 
1596e965bea1SArnaldo Carvalho de Melo 		printed += scnprintf(buf + printed, size - printed,
1597e965bea1SArnaldo Carvalho de Melo 				     "Hint:\tTry using a smaller -m/--mmap-pages value.");
1598956fa571SArnaldo Carvalho de Melo 		break;
1599956fa571SArnaldo Carvalho de Melo 	default:
1600956fa571SArnaldo Carvalho de Melo 		scnprintf(buf, size, "%s", emsg);
1601956fa571SArnaldo Carvalho de Melo 		break;
1602956fa571SArnaldo Carvalho de Melo 	}
1603956fa571SArnaldo Carvalho de Melo 
1604956fa571SArnaldo Carvalho de Melo 	return 0;
1605956fa571SArnaldo Carvalho de Melo }
1606956fa571SArnaldo Carvalho de Melo 
1607e414fd1aSArnaldo Carvalho de Melo void evlist__to_front(struct evlist *evlist, struct evsel *move_evsel)
1608a025e4f0SAdrian Hunter {
160932dcd021SJiri Olsa 	struct evsel *evsel, *n;
1610a025e4f0SAdrian Hunter 	LIST_HEAD(move);
1611a025e4f0SAdrian Hunter 
1612515dbe48SJiri Olsa 	if (move_evsel == evlist__first(evlist))
1613a025e4f0SAdrian Hunter 		return;
1614a025e4f0SAdrian Hunter 
1615e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry_safe(evlist, n, evsel) {
1616a025e4f0SAdrian Hunter 		if (evsel->leader == move_evsel->leader)
1617b27c4eceSJiri Olsa 			list_move_tail(&evsel->core.node, &move);
1618a025e4f0SAdrian Hunter 	}
1619a025e4f0SAdrian Hunter 
1620ce9036a6SJiri Olsa 	list_splice(&move, &evlist->core.entries);
1621a025e4f0SAdrian Hunter }
162260b0896cSAdrian Hunter 
1623e80db255SArnaldo Carvalho de Melo struct evsel *evlist__get_tracking_event(struct evlist *evlist)
1624442ad225SAdrian Hunter {
1625442ad225SAdrian Hunter 	struct evsel *evsel;
1626442ad225SAdrian Hunter 
1627442ad225SAdrian Hunter 	evlist__for_each_entry(evlist, evsel) {
1628442ad225SAdrian Hunter 		if (evsel->tracking)
1629442ad225SAdrian Hunter 			return evsel;
1630442ad225SAdrian Hunter 	}
1631442ad225SAdrian Hunter 
1632442ad225SAdrian Hunter 	return evlist__first(evlist);
1633442ad225SAdrian Hunter }
1634442ad225SAdrian Hunter 
1635e80db255SArnaldo Carvalho de Melo void evlist__set_tracking_event(struct evlist *evlist, struct evsel *tracking_evsel)
163660b0896cSAdrian Hunter {
163732dcd021SJiri Olsa 	struct evsel *evsel;
163860b0896cSAdrian Hunter 
163960b0896cSAdrian Hunter 	if (tracking_evsel->tracking)
164060b0896cSAdrian Hunter 		return;
164160b0896cSAdrian Hunter 
1642e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, evsel) {
164360b0896cSAdrian Hunter 		if (evsel != tracking_evsel)
164460b0896cSAdrian Hunter 			evsel->tracking = false;
164560b0896cSAdrian Hunter 	}
164660b0896cSAdrian Hunter 
164760b0896cSAdrian Hunter 	tracking_evsel->tracking = true;
164860b0896cSAdrian Hunter }
16497630b3e2SWang Nan 
1650b02736f7SArnaldo Carvalho de Melo struct evsel *evlist__find_evsel_by_str(struct evlist *evlist, const char *str)
16517630b3e2SWang Nan {
165232dcd021SJiri Olsa 	struct evsel *evsel;
16537630b3e2SWang Nan 
1654e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, evsel) {
16557630b3e2SWang Nan 		if (!evsel->name)
16567630b3e2SWang Nan 			continue;
16577630b3e2SWang Nan 		if (strcmp(str, evsel->name) == 0)
16587630b3e2SWang Nan 			return evsel;
16597630b3e2SWang Nan 	}
16607630b3e2SWang Nan 
16617630b3e2SWang Nan 	return NULL;
16627630b3e2SWang Nan }
166354cc54deSWang Nan 
1664ade9d208SArnaldo Carvalho de Melo void evlist__toggle_bkw_mmap(struct evlist *evlist, enum bkw_mmap_state state)
166554cc54deSWang Nan {
166654cc54deSWang Nan 	enum bkw_mmap_state old_state = evlist->bkw_mmap_state;
166754cc54deSWang Nan 	enum action {
166854cc54deSWang Nan 		NONE,
166954cc54deSWang Nan 		PAUSE,
167054cc54deSWang Nan 		RESUME,
167154cc54deSWang Nan 	} action = NONE;
167254cc54deSWang Nan 
16730b72d69aSWang Nan 	if (!evlist->overwrite_mmap)
167454cc54deSWang Nan 		return;
167554cc54deSWang Nan 
167654cc54deSWang Nan 	switch (old_state) {
167754cc54deSWang Nan 	case BKW_MMAP_NOTREADY: {
167854cc54deSWang Nan 		if (state != BKW_MMAP_RUNNING)
1679dd8bd53aSLuis de Bethencourt 			goto state_err;
168054cc54deSWang Nan 		break;
168154cc54deSWang Nan 	}
168254cc54deSWang Nan 	case BKW_MMAP_RUNNING: {
168354cc54deSWang Nan 		if (state != BKW_MMAP_DATA_PENDING)
168454cc54deSWang Nan 			goto state_err;
168554cc54deSWang Nan 		action = PAUSE;
168654cc54deSWang Nan 		break;
168754cc54deSWang Nan 	}
168854cc54deSWang Nan 	case BKW_MMAP_DATA_PENDING: {
168954cc54deSWang Nan 		if (state != BKW_MMAP_EMPTY)
169054cc54deSWang Nan 			goto state_err;
169154cc54deSWang Nan 		break;
169254cc54deSWang Nan 	}
169354cc54deSWang Nan 	case BKW_MMAP_EMPTY: {
169454cc54deSWang Nan 		if (state != BKW_MMAP_RUNNING)
169554cc54deSWang Nan 			goto state_err;
169654cc54deSWang Nan 		action = RESUME;
169754cc54deSWang Nan 		break;
169854cc54deSWang Nan 	}
169954cc54deSWang Nan 	default:
170054cc54deSWang Nan 		WARN_ONCE(1, "Shouldn't get there\n");
170154cc54deSWang Nan 	}
170254cc54deSWang Nan 
170354cc54deSWang Nan 	evlist->bkw_mmap_state = state;
170454cc54deSWang Nan 
170554cc54deSWang Nan 	switch (action) {
170654cc54deSWang Nan 	case PAUSE:
17070a60b339SArnaldo Carvalho de Melo 		evlist__pause(evlist);
170854cc54deSWang Nan 		break;
170954cc54deSWang Nan 	case RESUME:
17100a60b339SArnaldo Carvalho de Melo 		evlist__resume(evlist);
171154cc54deSWang Nan 		break;
171254cc54deSWang Nan 	case NONE:
171354cc54deSWang Nan 	default:
171454cc54deSWang Nan 		break;
171554cc54deSWang Nan 	}
171654cc54deSWang Nan 
171754cc54deSWang Nan state_err:
171854cc54deSWang Nan 	return;
171954cc54deSWang Nan }
172007d6f446SArnaldo Carvalho de Melo 
172178e1bc25SArnaldo Carvalho de Melo bool evlist__exclude_kernel(struct evlist *evlist)
172207d6f446SArnaldo Carvalho de Melo {
172332dcd021SJiri Olsa 	struct evsel *evsel;
172407d6f446SArnaldo Carvalho de Melo 
172507d6f446SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, evsel) {
17261fc632ceSJiri Olsa 		if (!evsel->core.attr.exclude_kernel)
172707d6f446SArnaldo Carvalho de Melo 			return false;
172807d6f446SArnaldo Carvalho de Melo 	}
172907d6f446SArnaldo Carvalho de Melo 
173007d6f446SArnaldo Carvalho de Melo 	return true;
173107d6f446SArnaldo Carvalho de Melo }
1732e2bdbe80SJin Yao 
1733e2bdbe80SJin Yao /*
1734e2bdbe80SJin Yao  * Events in data file are not collect in groups, but we still want
1735e2bdbe80SJin Yao  * the group display. Set the artificial group and set the leader's
1736e2bdbe80SJin Yao  * forced_leader flag to notify the display code.
1737e2bdbe80SJin Yao  */
173864b4778bSArnaldo Carvalho de Melo void evlist__force_leader(struct evlist *evlist)
1739e2bdbe80SJin Yao {
1740e2bdbe80SJin Yao 	if (!evlist->nr_groups) {
1741515dbe48SJiri Olsa 		struct evsel *leader = evlist__first(evlist);
1742e2bdbe80SJin Yao 
1743a622eafaSArnaldo Carvalho de Melo 		evlist__set_leader(evlist);
1744e2bdbe80SJin Yao 		leader->forced_leader = true;
1745e2bdbe80SJin Yao 	}
1746e2bdbe80SJin Yao }
1747c3537fc2SAndi Kleen 
174864b4778bSArnaldo Carvalho de Melo struct evsel *evlist__reset_weak_group(struct evlist *evsel_list, struct evsel *evsel, bool close)
1749c3537fc2SAndi Kleen {
175032dcd021SJiri Olsa 	struct evsel *c2, *leader;
1751c3537fc2SAndi Kleen 	bool is_open = true;
1752c3537fc2SAndi Kleen 
1753c3537fc2SAndi Kleen 	leader = evsel->leader;
1754c3537fc2SAndi Kleen 	pr_debug("Weak group for %s/%d failed\n",
17555643b1a5SJiri Olsa 			leader->name, leader->core.nr_members);
1756c3537fc2SAndi Kleen 
1757c3537fc2SAndi Kleen 	/*
1758c3537fc2SAndi Kleen 	 * for_each_group_member doesn't work here because it doesn't
1759c3537fc2SAndi Kleen 	 * include the first entry.
1760c3537fc2SAndi Kleen 	 */
1761c3537fc2SAndi Kleen 	evlist__for_each_entry(evsel_list, c2) {
1762c3537fc2SAndi Kleen 		if (c2 == evsel)
1763c3537fc2SAndi Kleen 			is_open = false;
1764c3537fc2SAndi Kleen 		if (c2->leader == leader) {
17654804e011SAndi Kleen 			if (is_open && close)
17665a40e199SAndi Kleen 				perf_evsel__close(&c2->core);
1767c3537fc2SAndi Kleen 			c2->leader = c2;
17685643b1a5SJiri Olsa 			c2->core.nr_members = 0;
17694804e011SAndi Kleen 			/*
17704804e011SAndi Kleen 			 * Set this for all former members of the group
17714804e011SAndi Kleen 			 * to indicate they get reopened.
17724804e011SAndi Kleen 			 */
17734804e011SAndi Kleen 			c2->reset_group = true;
1774c3537fc2SAndi Kleen 		}
1775c3537fc2SAndi Kleen 	}
1776c3537fc2SAndi Kleen 	return leader;
1777c3537fc2SAndi Kleen }
1778ec886bf5SAlexey Budankov 
1779a8fcbd26SAdrian Hunter static int evlist__parse_control_fifo(const char *str, int *ctl_fd, int *ctl_fd_ack, bool *ctl_fd_close)
1780a8fcbd26SAdrian Hunter {
1781a8fcbd26SAdrian Hunter 	char *s, *p;
1782a8fcbd26SAdrian Hunter 	int ret = 0, fd;
1783a8fcbd26SAdrian Hunter 
1784a8fcbd26SAdrian Hunter 	if (strncmp(str, "fifo:", 5))
1785a8fcbd26SAdrian Hunter 		return -EINVAL;
1786a8fcbd26SAdrian Hunter 
1787a8fcbd26SAdrian Hunter 	str += 5;
1788a8fcbd26SAdrian Hunter 	if (!*str || *str == ',')
1789a8fcbd26SAdrian Hunter 		return -EINVAL;
1790a8fcbd26SAdrian Hunter 
1791a8fcbd26SAdrian Hunter 	s = strdup(str);
1792a8fcbd26SAdrian Hunter 	if (!s)
1793a8fcbd26SAdrian Hunter 		return -ENOMEM;
1794a8fcbd26SAdrian Hunter 
1795a8fcbd26SAdrian Hunter 	p = strchr(s, ',');
1796a8fcbd26SAdrian Hunter 	if (p)
1797a8fcbd26SAdrian Hunter 		*p = '\0';
1798a8fcbd26SAdrian Hunter 
1799a8fcbd26SAdrian Hunter 	/*
1800a8fcbd26SAdrian Hunter 	 * O_RDWR avoids POLLHUPs which is necessary to allow the other
1801a8fcbd26SAdrian Hunter 	 * end of a FIFO to be repeatedly opened and closed.
1802a8fcbd26SAdrian Hunter 	 */
1803a8fcbd26SAdrian Hunter 	fd = open(s, O_RDWR | O_NONBLOCK | O_CLOEXEC);
1804a8fcbd26SAdrian Hunter 	if (fd < 0) {
1805a8fcbd26SAdrian Hunter 		pr_err("Failed to open '%s'\n", s);
1806a8fcbd26SAdrian Hunter 		ret = -errno;
1807a8fcbd26SAdrian Hunter 		goto out_free;
1808a8fcbd26SAdrian Hunter 	}
1809a8fcbd26SAdrian Hunter 	*ctl_fd = fd;
1810a8fcbd26SAdrian Hunter 	*ctl_fd_close = true;
1811a8fcbd26SAdrian Hunter 
1812a8fcbd26SAdrian Hunter 	if (p && *++p) {
1813a8fcbd26SAdrian Hunter 		/* O_RDWR | O_NONBLOCK means the other end need not be open */
1814a8fcbd26SAdrian Hunter 		fd = open(p, O_RDWR | O_NONBLOCK | O_CLOEXEC);
1815a8fcbd26SAdrian Hunter 		if (fd < 0) {
1816a8fcbd26SAdrian Hunter 			pr_err("Failed to open '%s'\n", p);
1817a8fcbd26SAdrian Hunter 			ret = -errno;
1818a8fcbd26SAdrian Hunter 			goto out_free;
1819a8fcbd26SAdrian Hunter 		}
1820a8fcbd26SAdrian Hunter 		*ctl_fd_ack = fd;
1821a8fcbd26SAdrian Hunter 	}
1822a8fcbd26SAdrian Hunter 
1823a8fcbd26SAdrian Hunter out_free:
1824a8fcbd26SAdrian Hunter 	free(s);
1825a8fcbd26SAdrian Hunter 	return ret;
1826a8fcbd26SAdrian Hunter }
1827a8fcbd26SAdrian Hunter 
1828a8fcbd26SAdrian Hunter int evlist__parse_control(const char *str, int *ctl_fd, int *ctl_fd_ack, bool *ctl_fd_close)
18299864a66dSAdrian Hunter {
18309864a66dSAdrian Hunter 	char *comma = NULL, *endptr = NULL;
18319864a66dSAdrian Hunter 
1832a8fcbd26SAdrian Hunter 	*ctl_fd_close = false;
1833a8fcbd26SAdrian Hunter 
18349864a66dSAdrian Hunter 	if (strncmp(str, "fd:", 3))
1835a8fcbd26SAdrian Hunter 		return evlist__parse_control_fifo(str, ctl_fd, ctl_fd_ack, ctl_fd_close);
18369864a66dSAdrian Hunter 
18379864a66dSAdrian Hunter 	*ctl_fd = strtoul(&str[3], &endptr, 0);
18389864a66dSAdrian Hunter 	if (endptr == &str[3])
18399864a66dSAdrian Hunter 		return -EINVAL;
18409864a66dSAdrian Hunter 
18419864a66dSAdrian Hunter 	comma = strchr(str, ',');
18429864a66dSAdrian Hunter 	if (comma) {
18439864a66dSAdrian Hunter 		if (endptr != comma)
18449864a66dSAdrian Hunter 			return -EINVAL;
18459864a66dSAdrian Hunter 
18469864a66dSAdrian Hunter 		*ctl_fd_ack = strtoul(comma + 1, &endptr, 0);
18479864a66dSAdrian Hunter 		if (endptr == comma + 1 || *endptr != '\0')
18489864a66dSAdrian Hunter 			return -EINVAL;
18499864a66dSAdrian Hunter 	}
18509864a66dSAdrian Hunter 
18519864a66dSAdrian Hunter 	return 0;
18529864a66dSAdrian Hunter }
18539864a66dSAdrian Hunter 
1854ee7fe31eSAdrian Hunter void evlist__close_control(int ctl_fd, int ctl_fd_ack, bool *ctl_fd_close)
1855ee7fe31eSAdrian Hunter {
1856ee7fe31eSAdrian Hunter 	if (*ctl_fd_close) {
1857ee7fe31eSAdrian Hunter 		*ctl_fd_close = false;
1858ee7fe31eSAdrian Hunter 		close(ctl_fd);
1859ee7fe31eSAdrian Hunter 		if (ctl_fd_ack >= 0)
1860ee7fe31eSAdrian Hunter 			close(ctl_fd_ack);
1861ee7fe31eSAdrian Hunter 	}
1862ee7fe31eSAdrian Hunter }
1863ee7fe31eSAdrian Hunter 
1864ec886bf5SAlexey Budankov int evlist__initialize_ctlfd(struct evlist *evlist, int fd, int ack)
1865ec886bf5SAlexey Budankov {
1866ec886bf5SAlexey Budankov 	if (fd == -1) {
1867ec886bf5SAlexey Budankov 		pr_debug("Control descriptor is not initialized\n");
1868ec886bf5SAlexey Budankov 		return 0;
1869ec886bf5SAlexey Budankov 	}
1870ec886bf5SAlexey Budankov 
1871ec886bf5SAlexey Budankov 	evlist->ctl_fd.pos = perf_evlist__add_pollfd(&evlist->core, fd, NULL, POLLIN,
1872ec886bf5SAlexey Budankov 						     fdarray_flag__nonfilterable);
1873ec886bf5SAlexey Budankov 	if (evlist->ctl_fd.pos < 0) {
1874ec886bf5SAlexey Budankov 		evlist->ctl_fd.pos = -1;
1875ec886bf5SAlexey Budankov 		pr_err("Failed to add ctl fd entry: %m\n");
1876ec886bf5SAlexey Budankov 		return -1;
1877ec886bf5SAlexey Budankov 	}
1878ec886bf5SAlexey Budankov 
1879ec886bf5SAlexey Budankov 	evlist->ctl_fd.fd = fd;
1880ec886bf5SAlexey Budankov 	evlist->ctl_fd.ack = ack;
1881ec886bf5SAlexey Budankov 
1882ec886bf5SAlexey Budankov 	return 0;
1883ec886bf5SAlexey Budankov }
1884ec886bf5SAlexey Budankov 
1885ec886bf5SAlexey Budankov bool evlist__ctlfd_initialized(struct evlist *evlist)
1886ec886bf5SAlexey Budankov {
1887ec886bf5SAlexey Budankov 	return evlist->ctl_fd.pos >= 0;
1888ec886bf5SAlexey Budankov }
1889ec886bf5SAlexey Budankov 
1890ec886bf5SAlexey Budankov int evlist__finalize_ctlfd(struct evlist *evlist)
1891ec886bf5SAlexey Budankov {
1892ec886bf5SAlexey Budankov 	struct pollfd *entries = evlist->core.pollfd.entries;
1893ec886bf5SAlexey Budankov 
1894ec886bf5SAlexey Budankov 	if (!evlist__ctlfd_initialized(evlist))
1895ec886bf5SAlexey Budankov 		return 0;
1896ec886bf5SAlexey Budankov 
1897ec886bf5SAlexey Budankov 	entries[evlist->ctl_fd.pos].fd = -1;
1898ec886bf5SAlexey Budankov 	entries[evlist->ctl_fd.pos].events = 0;
1899ec886bf5SAlexey Budankov 	entries[evlist->ctl_fd.pos].revents = 0;
1900ec886bf5SAlexey Budankov 
1901ec886bf5SAlexey Budankov 	evlist->ctl_fd.pos = -1;
1902ec886bf5SAlexey Budankov 	evlist->ctl_fd.ack = -1;
1903ec886bf5SAlexey Budankov 	evlist->ctl_fd.fd = -1;
1904ec886bf5SAlexey Budankov 
1905ec886bf5SAlexey Budankov 	return 0;
1906ec886bf5SAlexey Budankov }
1907ec886bf5SAlexey Budankov 
1908ec886bf5SAlexey Budankov static int evlist__ctlfd_recv(struct evlist *evlist, enum evlist_ctl_cmd *cmd,
1909ec886bf5SAlexey Budankov 			      char *cmd_data, size_t data_size)
1910ec886bf5SAlexey Budankov {
1911ec886bf5SAlexey Budankov 	int err;
1912ec886bf5SAlexey Budankov 	char c;
1913ec886bf5SAlexey Budankov 	size_t bytes_read = 0;
1914ec886bf5SAlexey Budankov 
191540db8ff5SAdrian Hunter 	*cmd = EVLIST_CTL_CMD_UNSUPPORTED;
1916ec886bf5SAlexey Budankov 	memset(cmd_data, 0, data_size);
1917ec886bf5SAlexey Budankov 	data_size--;
1918ec886bf5SAlexey Budankov 
1919ec886bf5SAlexey Budankov 	do {
1920ec886bf5SAlexey Budankov 		err = read(evlist->ctl_fd.fd, &c, 1);
1921ec886bf5SAlexey Budankov 		if (err > 0) {
1922ec886bf5SAlexey Budankov 			if (c == '\n' || c == '\0')
1923ec886bf5SAlexey Budankov 				break;
1924ec886bf5SAlexey Budankov 			cmd_data[bytes_read++] = c;
1925ec886bf5SAlexey Budankov 			if (bytes_read == data_size)
1926ec886bf5SAlexey Budankov 				break;
192740db8ff5SAdrian Hunter 			continue;
192840db8ff5SAdrian Hunter 		} else if (err == -1) {
192940db8ff5SAdrian Hunter 			if (errno == EINTR)
193040db8ff5SAdrian Hunter 				continue;
193140db8ff5SAdrian Hunter 			if (errno == EAGAIN || errno == EWOULDBLOCK)
193240db8ff5SAdrian Hunter 				err = 0;
193340db8ff5SAdrian Hunter 			else
1934ec886bf5SAlexey Budankov 				pr_err("Failed to read from ctlfd %d: %m\n", evlist->ctl_fd.fd);
1935ec886bf5SAlexey Budankov 		}
193640db8ff5SAdrian Hunter 		break;
1937ec886bf5SAlexey Budankov 	} while (1);
1938ec886bf5SAlexey Budankov 
1939ec886bf5SAlexey Budankov 	pr_debug("Message from ctl_fd: \"%s%s\"\n", cmd_data,
1940ec886bf5SAlexey Budankov 		 bytes_read == data_size ? "" : c == '\n' ? "\\n" : "\\0");
1941ec886bf5SAlexey Budankov 
194240db8ff5SAdrian Hunter 	if (bytes_read > 0) {
1943ec886bf5SAlexey Budankov 		if (!strncmp(cmd_data, EVLIST_CTL_CMD_ENABLE_TAG,
1944ec886bf5SAlexey Budankov 			     (sizeof(EVLIST_CTL_CMD_ENABLE_TAG)-1))) {
1945ec886bf5SAlexey Budankov 			*cmd = EVLIST_CTL_CMD_ENABLE;
1946ec886bf5SAlexey Budankov 		} else if (!strncmp(cmd_data, EVLIST_CTL_CMD_DISABLE_TAG,
1947ec886bf5SAlexey Budankov 				    (sizeof(EVLIST_CTL_CMD_DISABLE_TAG)-1))) {
1948ec886bf5SAlexey Budankov 			*cmd = EVLIST_CTL_CMD_DISABLE;
1949d20aff15SAdrian Hunter 		} else if (!strncmp(cmd_data, EVLIST_CTL_CMD_SNAPSHOT_TAG,
1950d20aff15SAdrian Hunter 				    (sizeof(EVLIST_CTL_CMD_SNAPSHOT_TAG)-1))) {
1951d20aff15SAdrian Hunter 			*cmd = EVLIST_CTL_CMD_SNAPSHOT;
1952d20aff15SAdrian Hunter 			pr_debug("is snapshot\n");
1953142544a9SJiri Olsa 		} else if (!strncmp(cmd_data, EVLIST_CTL_CMD_EVLIST_TAG,
1954142544a9SJiri Olsa 				    (sizeof(EVLIST_CTL_CMD_EVLIST_TAG)-1))) {
1955142544a9SJiri Olsa 			*cmd = EVLIST_CTL_CMD_EVLIST;
1956f186cd61SJiri Olsa 		} else if (!strncmp(cmd_data, EVLIST_CTL_CMD_STOP_TAG,
1957f186cd61SJiri Olsa 				    (sizeof(EVLIST_CTL_CMD_STOP_TAG)-1))) {
1958f186cd61SJiri Olsa 			*cmd = EVLIST_CTL_CMD_STOP;
195947fddcb4SJiri Olsa 		} else if (!strncmp(cmd_data, EVLIST_CTL_CMD_PING_TAG,
196047fddcb4SJiri Olsa 				    (sizeof(EVLIST_CTL_CMD_PING_TAG)-1))) {
196147fddcb4SJiri Olsa 			*cmd = EVLIST_CTL_CMD_PING;
1962ec886bf5SAlexey Budankov 		}
1963ec886bf5SAlexey Budankov 	}
1964ec886bf5SAlexey Budankov 
196540db8ff5SAdrian Hunter 	return bytes_read ? (int)bytes_read : err;
1966ec886bf5SAlexey Budankov }
1967ec886bf5SAlexey Budankov 
1968d20aff15SAdrian Hunter int evlist__ctlfd_ack(struct evlist *evlist)
1969ec886bf5SAlexey Budankov {
1970ec886bf5SAlexey Budankov 	int err;
1971ec886bf5SAlexey Budankov 
1972ec886bf5SAlexey Budankov 	if (evlist->ctl_fd.ack == -1)
1973ec886bf5SAlexey Budankov 		return 0;
1974ec886bf5SAlexey Budankov 
1975ec886bf5SAlexey Budankov 	err = write(evlist->ctl_fd.ack, EVLIST_CTL_CMD_ACK_TAG,
1976ec886bf5SAlexey Budankov 		    sizeof(EVLIST_CTL_CMD_ACK_TAG));
1977ec886bf5SAlexey Budankov 	if (err == -1)
1978ec886bf5SAlexey Budankov 		pr_err("failed to write to ctl_ack_fd %d: %m\n", evlist->ctl_fd.ack);
1979ec886bf5SAlexey Budankov 
1980ec886bf5SAlexey Budankov 	return err;
1981ec886bf5SAlexey Budankov }
1982ec886bf5SAlexey Budankov 
1983991ae4ebSJiri Olsa static int get_cmd_arg(char *cmd_data, size_t cmd_size, char **arg)
1984991ae4ebSJiri Olsa {
1985991ae4ebSJiri Olsa 	char *data = cmd_data + cmd_size;
1986991ae4ebSJiri Olsa 
1987991ae4ebSJiri Olsa 	/* no argument */
1988991ae4ebSJiri Olsa 	if (!*data)
1989991ae4ebSJiri Olsa 		return 0;
1990991ae4ebSJiri Olsa 
1991991ae4ebSJiri Olsa 	/* there's argument */
1992991ae4ebSJiri Olsa 	if (*data == ' ') {
1993991ae4ebSJiri Olsa 		*arg = data + 1;
1994991ae4ebSJiri Olsa 		return 1;
1995991ae4ebSJiri Olsa 	}
1996991ae4ebSJiri Olsa 
1997991ae4ebSJiri Olsa 	/* malformed */
1998991ae4ebSJiri Olsa 	return -1;
1999991ae4ebSJiri Olsa }
2000991ae4ebSJiri Olsa 
2001991ae4ebSJiri Olsa static int evlist__ctlfd_enable(struct evlist *evlist, char *cmd_data, bool enable)
2002991ae4ebSJiri Olsa {
2003991ae4ebSJiri Olsa 	struct evsel *evsel;
2004991ae4ebSJiri Olsa 	char *name;
2005991ae4ebSJiri Olsa 	int err;
2006991ae4ebSJiri Olsa 
2007991ae4ebSJiri Olsa 	err = get_cmd_arg(cmd_data,
2008991ae4ebSJiri Olsa 			  enable ? sizeof(EVLIST_CTL_CMD_ENABLE_TAG) - 1 :
2009991ae4ebSJiri Olsa 				   sizeof(EVLIST_CTL_CMD_DISABLE_TAG) - 1,
2010991ae4ebSJiri Olsa 			  &name);
2011991ae4ebSJiri Olsa 	if (err < 0) {
2012991ae4ebSJiri Olsa 		pr_info("failed: wrong command\n");
2013991ae4ebSJiri Olsa 		return -1;
2014991ae4ebSJiri Olsa 	}
2015991ae4ebSJiri Olsa 
2016991ae4ebSJiri Olsa 	if (err) {
2017991ae4ebSJiri Olsa 		evsel = evlist__find_evsel_by_str(evlist, name);
2018991ae4ebSJiri Olsa 		if (evsel) {
2019991ae4ebSJiri Olsa 			if (enable)
2020991ae4ebSJiri Olsa 				evlist__enable_evsel(evlist, name);
2021991ae4ebSJiri Olsa 			else
2022991ae4ebSJiri Olsa 				evlist__disable_evsel(evlist, name);
2023991ae4ebSJiri Olsa 			pr_info("Event %s %s\n", evsel->name,
2024991ae4ebSJiri Olsa 				enable ? "enabled" : "disabled");
2025991ae4ebSJiri Olsa 		} else {
2026991ae4ebSJiri Olsa 			pr_info("failed: can't find '%s' event\n", name);
2027991ae4ebSJiri Olsa 		}
2028991ae4ebSJiri Olsa 	} else {
2029991ae4ebSJiri Olsa 		if (enable) {
2030991ae4ebSJiri Olsa 			evlist__enable(evlist);
2031991ae4ebSJiri Olsa 			pr_info(EVLIST_ENABLED_MSG);
2032991ae4ebSJiri Olsa 		} else {
2033991ae4ebSJiri Olsa 			evlist__disable(evlist);
2034991ae4ebSJiri Olsa 			pr_info(EVLIST_DISABLED_MSG);
2035991ae4ebSJiri Olsa 		}
2036991ae4ebSJiri Olsa 	}
2037991ae4ebSJiri Olsa 
2038991ae4ebSJiri Olsa 	return 0;
2039991ae4ebSJiri Olsa }
2040991ae4ebSJiri Olsa 
2041142544a9SJiri Olsa static int evlist__ctlfd_list(struct evlist *evlist, char *cmd_data)
2042142544a9SJiri Olsa {
2043142544a9SJiri Olsa 	struct perf_attr_details details = { .verbose = false, };
2044142544a9SJiri Olsa 	struct evsel *evsel;
2045142544a9SJiri Olsa 	char *arg;
2046142544a9SJiri Olsa 	int err;
2047142544a9SJiri Olsa 
2048142544a9SJiri Olsa 	err = get_cmd_arg(cmd_data,
2049142544a9SJiri Olsa 			  sizeof(EVLIST_CTL_CMD_EVLIST_TAG) - 1,
2050142544a9SJiri Olsa 			  &arg);
2051142544a9SJiri Olsa 	if (err < 0) {
2052142544a9SJiri Olsa 		pr_info("failed: wrong command\n");
2053142544a9SJiri Olsa 		return -1;
2054142544a9SJiri Olsa 	}
2055142544a9SJiri Olsa 
2056142544a9SJiri Olsa 	if (err) {
2057142544a9SJiri Olsa 		if (!strcmp(arg, "-v")) {
2058142544a9SJiri Olsa 			details.verbose = true;
2059142544a9SJiri Olsa 		} else if (!strcmp(arg, "-g")) {
2060142544a9SJiri Olsa 			details.event_group = true;
2061142544a9SJiri Olsa 		} else if (!strcmp(arg, "-F")) {
2062142544a9SJiri Olsa 			details.freq = true;
2063142544a9SJiri Olsa 		} else {
2064142544a9SJiri Olsa 			pr_info("failed: wrong command\n");
2065142544a9SJiri Olsa 			return -1;
2066142544a9SJiri Olsa 		}
2067142544a9SJiri Olsa 	}
2068142544a9SJiri Olsa 
2069142544a9SJiri Olsa 	evlist__for_each_entry(evlist, evsel)
2070142544a9SJiri Olsa 		evsel__fprintf(evsel, &details, stderr);
2071142544a9SJiri Olsa 
2072142544a9SJiri Olsa 	return 0;
2073142544a9SJiri Olsa }
2074142544a9SJiri Olsa 
2075ec886bf5SAlexey Budankov int evlist__ctlfd_process(struct evlist *evlist, enum evlist_ctl_cmd *cmd)
2076ec886bf5SAlexey Budankov {
2077ec886bf5SAlexey Budankov 	int err = 0;
2078ec886bf5SAlexey Budankov 	char cmd_data[EVLIST_CTL_CMD_MAX_LEN];
2079ec886bf5SAlexey Budankov 	int ctlfd_pos = evlist->ctl_fd.pos;
2080ec886bf5SAlexey Budankov 	struct pollfd *entries = evlist->core.pollfd.entries;
2081ec886bf5SAlexey Budankov 
2082ec886bf5SAlexey Budankov 	if (!evlist__ctlfd_initialized(evlist) || !entries[ctlfd_pos].revents)
2083ec886bf5SAlexey Budankov 		return 0;
2084ec886bf5SAlexey Budankov 
2085ec886bf5SAlexey Budankov 	if (entries[ctlfd_pos].revents & POLLIN) {
2086ec886bf5SAlexey Budankov 		err = evlist__ctlfd_recv(evlist, cmd, cmd_data,
2087ec886bf5SAlexey Budankov 					 EVLIST_CTL_CMD_MAX_LEN);
2088ec886bf5SAlexey Budankov 		if (err > 0) {
2089ec886bf5SAlexey Budankov 			switch (*cmd) {
2090ec886bf5SAlexey Budankov 			case EVLIST_CTL_CMD_ENABLE:
2091ec886bf5SAlexey Budankov 			case EVLIST_CTL_CMD_DISABLE:
2092991ae4ebSJiri Olsa 				err = evlist__ctlfd_enable(evlist, cmd_data,
2093991ae4ebSJiri Olsa 							   *cmd == EVLIST_CTL_CMD_ENABLE);
2094ec886bf5SAlexey Budankov 				break;
2095142544a9SJiri Olsa 			case EVLIST_CTL_CMD_EVLIST:
2096142544a9SJiri Olsa 				err = evlist__ctlfd_list(evlist, cmd_data);
2097142544a9SJiri Olsa 				break;
2098d20aff15SAdrian Hunter 			case EVLIST_CTL_CMD_SNAPSHOT:
2099f186cd61SJiri Olsa 			case EVLIST_CTL_CMD_STOP:
210047fddcb4SJiri Olsa 			case EVLIST_CTL_CMD_PING:
2101d20aff15SAdrian Hunter 				break;
2102ec886bf5SAlexey Budankov 			case EVLIST_CTL_CMD_ACK:
2103ec886bf5SAlexey Budankov 			case EVLIST_CTL_CMD_UNSUPPORTED:
2104ec886bf5SAlexey Budankov 			default:
2105ec886bf5SAlexey Budankov 				pr_debug("ctlfd: unsupported %d\n", *cmd);
2106ec886bf5SAlexey Budankov 				break;
2107ec886bf5SAlexey Budankov 			}
2108d20aff15SAdrian Hunter 			if (!(*cmd == EVLIST_CTL_CMD_ACK || *cmd == EVLIST_CTL_CMD_UNSUPPORTED ||
2109d20aff15SAdrian Hunter 			      *cmd == EVLIST_CTL_CMD_SNAPSHOT))
2110ec886bf5SAlexey Budankov 				evlist__ctlfd_ack(evlist);
2111ec886bf5SAlexey Budankov 		}
2112ec886bf5SAlexey Budankov 	}
2113ec886bf5SAlexey Budankov 
2114ec886bf5SAlexey Budankov 	if (entries[ctlfd_pos].revents & (POLLHUP | POLLERR))
2115ec886bf5SAlexey Budankov 		evlist__finalize_ctlfd(evlist);
2116ec886bf5SAlexey Budankov 	else
2117ec886bf5SAlexey Budankov 		entries[ctlfd_pos].revents = 0;
2118ec886bf5SAlexey Budankov 
2119ec886bf5SAlexey Budankov 	return err;
2120ec886bf5SAlexey Budankov }
2121b214ba8cSNamhyung Kim 
2122b214ba8cSNamhyung Kim struct evsel *evlist__find_evsel(struct evlist *evlist, int idx)
2123b214ba8cSNamhyung Kim {
2124b214ba8cSNamhyung Kim 	struct evsel *evsel;
2125b214ba8cSNamhyung Kim 
2126b214ba8cSNamhyung Kim 	evlist__for_each_entry(evlist, evsel) {
2127b214ba8cSNamhyung Kim 		if (evsel->idx == idx)
2128b214ba8cSNamhyung Kim 			return evsel;
2129b214ba8cSNamhyung Kim 	}
2130b214ba8cSNamhyung Kim 	return NULL;
2131b214ba8cSNamhyung Kim }
2132