xref: /openbmc/linux/tools/perf/util/evlist.c (revision 1827ab5b)
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
21c1a604dfSArnaldo Carvalho de Melo #include "../perf.h"
2254cc54deSWang Nan #include "asm/bug.h"
23657ee553SSong Liu #include "bpf-event.h"
249607ad3aSArnaldo Carvalho de Melo #include <signal.h>
2535b9d88eSArnaldo Carvalho de Melo #include <unistd.h>
26b397f846SArnaldo Carvalho de Melo #include <sched.h>
27f2a39fe8SArnaldo Carvalho de Melo #include <stdlib.h>
28361c99a6SArnaldo Carvalho de Melo 
2950d08e47SArnaldo Carvalho de Melo #include "parse-events.h"
304b6ab94eSJosh Poimboeuf #include <subcmd/parse-options.h>
3150d08e47SArnaldo Carvalho de Melo 
32bafae98eSArnaldo Carvalho de Melo #include <fcntl.h>
3386a5e0c2SArnaldo Carvalho de Melo #include <sys/ioctl.h>
34f8a95309SArnaldo Carvalho de Melo #include <sys/mman.h>
35f8a95309SArnaldo Carvalho de Melo 
3670db7533SArnaldo Carvalho de Melo #include <linux/bitops.h>
3770db7533SArnaldo Carvalho de Melo #include <linux/hash.h>
380389cd1fSArnaldo Carvalho de Melo #include <linux/log2.h>
398dd2a131SJiri Olsa #include <linux/err.h>
408520a98dSArnaldo Carvalho de Melo #include <linux/string.h>
417f7c536fSArnaldo Carvalho de Melo #include <linux/zalloc.h>
424562a739SJiri Olsa #include <perf/evlist.h>
4388761fa1SJiri Olsa #include <perf/evsel.h>
449c3516d1SJiri Olsa #include <perf/cpumap.h>
4570db7533SArnaldo Carvalho de Melo 
46e14e5497SArnaldo Carvalho de Melo #include <internal/xyarray.h>
47e14e5497SArnaldo Carvalho de Melo 
48748fe088SArnaldo Carvalho de Melo #ifdef LACKS_SIGQUEUE_PROTOTYPE
49748fe088SArnaldo Carvalho de Melo int sigqueue(pid_t pid, int sig, const union sigval value);
50748fe088SArnaldo Carvalho de Melo #endif
51748fe088SArnaldo Carvalho de Melo 
529dfcb759SJiri Olsa #define FD(e, x, y) (*(int *)xyarray__entry(e->core.fd, x, y))
538cd36f3eSJiri Olsa #define SID(e, x, y) xyarray__entry(e->core.sample_id, x, y)
54f8a95309SArnaldo Carvalho de Melo 
5552c86bcaSJiri Olsa void evlist__init(struct evlist *evlist, struct perf_cpu_map *cpus,
569749b90eSJiri Olsa 		  struct perf_thread_map *threads)
57361c99a6SArnaldo Carvalho de Melo {
584562a739SJiri Olsa 	perf_evlist__init(&evlist->core);
59453fa030SJiri Olsa 	perf_evlist__set_maps(&evlist->core, cpus, threads);
6040cb2d51SJiri Olsa 	fdarray__init(&evlist->core.pollfd, 64);
6135b9d88eSArnaldo Carvalho de Melo 	evlist->workload.pid = -1;
6254cc54deSWang Nan 	evlist->bkw_mmap_state = BKW_MMAP_NOTREADY;
63361c99a6SArnaldo Carvalho de Melo }
64361c99a6SArnaldo Carvalho de Melo 
650f98b11cSJiri Olsa struct evlist *evlist__new(void)
66ef1d1af2SArnaldo Carvalho de Melo {
6763503dbaSJiri Olsa 	struct evlist *evlist = zalloc(sizeof(*evlist));
68ef1d1af2SArnaldo Carvalho de Melo 
69ef1d1af2SArnaldo Carvalho de Melo 	if (evlist != NULL)
7052c86bcaSJiri Olsa 		evlist__init(evlist, NULL, NULL);
71ef1d1af2SArnaldo Carvalho de Melo 
72361c99a6SArnaldo Carvalho de Melo 	return evlist;
73361c99a6SArnaldo Carvalho de Melo }
74361c99a6SArnaldo Carvalho de Melo 
7563503dbaSJiri Olsa struct evlist *perf_evlist__new_default(void)
76b22d54b0SJiri Olsa {
770f98b11cSJiri Olsa 	struct evlist *evlist = evlist__new();
78b22d54b0SJiri Olsa 
79b22d54b0SJiri Olsa 	if (evlist && perf_evlist__add_default(evlist)) {
80c12995a5SJiri Olsa 		evlist__delete(evlist);
81b22d54b0SJiri Olsa 		evlist = NULL;
82b22d54b0SJiri Olsa 	}
83b22d54b0SJiri Olsa 
84b22d54b0SJiri Olsa 	return evlist;
85b22d54b0SJiri Olsa }
86b22d54b0SJiri Olsa 
8763503dbaSJiri Olsa struct evlist *perf_evlist__new_dummy(void)
885bae0250SArnaldo Carvalho de Melo {
890f98b11cSJiri Olsa 	struct evlist *evlist = evlist__new();
905bae0250SArnaldo Carvalho de Melo 
915bae0250SArnaldo Carvalho de Melo 	if (evlist && perf_evlist__add_dummy(evlist)) {
92c12995a5SJiri Olsa 		evlist__delete(evlist);
935bae0250SArnaldo Carvalho de Melo 		evlist = NULL;
945bae0250SArnaldo Carvalho de Melo 	}
955bae0250SArnaldo Carvalho de Melo 
965bae0250SArnaldo Carvalho de Melo 	return evlist;
975bae0250SArnaldo Carvalho de Melo }
985bae0250SArnaldo Carvalho de Melo 
9975562573SAdrian Hunter /**
10075562573SAdrian Hunter  * perf_evlist__set_id_pos - set the positions of event ids.
10175562573SAdrian Hunter  * @evlist: selected event list
10275562573SAdrian Hunter  *
10375562573SAdrian Hunter  * Events with compatible sample types all have the same id_pos
10475562573SAdrian Hunter  * and is_pos.  For convenience, put a copy on evlist.
10575562573SAdrian Hunter  */
10663503dbaSJiri Olsa void perf_evlist__set_id_pos(struct evlist *evlist)
10775562573SAdrian Hunter {
108515dbe48SJiri Olsa 	struct evsel *first = evlist__first(evlist);
10975562573SAdrian Hunter 
11075562573SAdrian Hunter 	evlist->id_pos = first->id_pos;
11175562573SAdrian Hunter 	evlist->is_pos = first->is_pos;
11275562573SAdrian Hunter }
11375562573SAdrian Hunter 
11463503dbaSJiri Olsa static void perf_evlist__update_id_pos(struct evlist *evlist)
115733cd2feSAdrian Hunter {
11632dcd021SJiri Olsa 	struct evsel *evsel;
117733cd2feSAdrian Hunter 
118e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, evsel)
119733cd2feSAdrian Hunter 		perf_evsel__calc_id_pos(evsel);
120733cd2feSAdrian Hunter 
121733cd2feSAdrian Hunter 	perf_evlist__set_id_pos(evlist);
122733cd2feSAdrian Hunter }
123733cd2feSAdrian Hunter 
124e6b1878dSJiri Olsa static void evlist__purge(struct evlist *evlist)
125361c99a6SArnaldo Carvalho de Melo {
12632dcd021SJiri Olsa 	struct evsel *pos, *n;
127361c99a6SArnaldo Carvalho de Melo 
128e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry_safe(evlist, n, pos) {
129b27c4eceSJiri Olsa 		list_del_init(&pos->core.node);
130d49e4695SArnaldo Carvalho de Melo 		pos->evlist = NULL;
1315eb2dd2aSJiri Olsa 		evsel__delete(pos);
132361c99a6SArnaldo Carvalho de Melo 	}
133361c99a6SArnaldo Carvalho de Melo 
1346484d2f9SJiri Olsa 	evlist->core.nr_entries = 0;
135361c99a6SArnaldo Carvalho de Melo }
136361c99a6SArnaldo Carvalho de Melo 
137470579b0SJiri Olsa void evlist__exit(struct evlist *evlist)
138ef1d1af2SArnaldo Carvalho de Melo {
13904662523SArnaldo Carvalho de Melo 	zfree(&evlist->mmap);
1400b72d69aSWang Nan 	zfree(&evlist->overwrite_mmap);
14140cb2d51SJiri Olsa 	fdarray__exit(&evlist->core.pollfd);
142ef1d1af2SArnaldo Carvalho de Melo }
143ef1d1af2SArnaldo Carvalho de Melo 
144c12995a5SJiri Olsa void evlist__delete(struct evlist *evlist)
145361c99a6SArnaldo Carvalho de Melo {
1460b04b3dcSArnaldo Carvalho de Melo 	if (evlist == NULL)
1470b04b3dcSArnaldo Carvalho de Melo 		return;
1480b04b3dcSArnaldo Carvalho de Melo 
149db6b7b13SJiri Olsa 	evlist__munmap(evlist);
150750b4edeSJiri Olsa 	evlist__close(evlist);
151f72f901dSJiri Olsa 	perf_cpu_map__put(evlist->core.cpus);
15203617c22SJiri Olsa 	perf_thread_map__put(evlist->core.threads);
153f72f901dSJiri Olsa 	evlist->core.cpus = NULL;
15403617c22SJiri Olsa 	evlist->core.threads = NULL;
155e6b1878dSJiri Olsa 	evlist__purge(evlist);
156470579b0SJiri Olsa 	evlist__exit(evlist);
157361c99a6SArnaldo Carvalho de Melo 	free(evlist);
158361c99a6SArnaldo Carvalho de Melo }
159361c99a6SArnaldo Carvalho de Melo 
160a1cf3a75SJiri Olsa void evlist__add(struct evlist *evlist, struct evsel *entry)
161361c99a6SArnaldo Carvalho de Melo {
162d49e4695SArnaldo Carvalho de Melo 	entry->evlist = evlist;
1636484d2f9SJiri Olsa 	entry->idx = evlist->core.nr_entries;
16460b0896cSAdrian Hunter 	entry->tracking = !entry->idx;
165ef503831SArnaldo Carvalho de Melo 
1666484d2f9SJiri Olsa 	perf_evlist__add(&evlist->core, &entry->core);
1676484d2f9SJiri Olsa 
1686484d2f9SJiri Olsa 	if (evlist->core.nr_entries == 1)
16975562573SAdrian Hunter 		perf_evlist__set_id_pos(evlist);
170361c99a6SArnaldo Carvalho de Melo }
171361c99a6SArnaldo Carvalho de Melo 
17216251027SJiri Olsa void evlist__remove(struct evlist *evlist, struct evsel *evsel)
1734768230aSAdrian Hunter {
1744768230aSAdrian Hunter 	evsel->evlist = NULL;
17552e22fb8SJiri Olsa 	perf_evlist__remove(&evlist->core, &evsel->core);
1764768230aSAdrian Hunter }
1774768230aSAdrian Hunter 
17863503dbaSJiri Olsa void perf_evlist__splice_list_tail(struct evlist *evlist,
179f114d6efSAdrian Hunter 				   struct list_head *list)
18050d08e47SArnaldo Carvalho de Melo {
18132dcd021SJiri Olsa 	struct evsel *evsel, *temp;
18275562573SAdrian Hunter 
183e5cadb93SArnaldo Carvalho de Melo 	__evlist__for_each_entry_safe(list, temp, evsel) {
184b27c4eceSJiri Olsa 		list_del_init(&evsel->core.node);
185a1cf3a75SJiri Olsa 		evlist__add(evlist, evsel);
186f114d6efSAdrian Hunter 	}
18750d08e47SArnaldo Carvalho de Melo }
18850d08e47SArnaldo Carvalho de Melo 
189c0e53476SArnaldo Carvalho de Melo int __evlist__set_tracepoints_handlers(struct evlist *evlist,
190c0e53476SArnaldo Carvalho de Melo 				       const struct evsel_str_handler *assocs, size_t nr_assocs)
191c0e53476SArnaldo Carvalho de Melo {
192c0e53476SArnaldo Carvalho de Melo 	struct evsel *evsel;
193c0e53476SArnaldo Carvalho de Melo 	size_t i;
194c0e53476SArnaldo Carvalho de Melo 	int err;
195c0e53476SArnaldo Carvalho de Melo 
196c0e53476SArnaldo Carvalho de Melo 	for (i = 0; i < nr_assocs; i++) {
197c0e53476SArnaldo Carvalho de Melo 		// Adding a handler for an event not in this evlist, just ignore it.
198c0e53476SArnaldo Carvalho de Melo 		evsel = perf_evlist__find_tracepoint_by_name(evlist, assocs[i].name);
199c0e53476SArnaldo Carvalho de Melo 		if (evsel == NULL)
200c0e53476SArnaldo Carvalho de Melo 			continue;
201c0e53476SArnaldo Carvalho de Melo 
202c0e53476SArnaldo Carvalho de Melo 		err = -EEXIST;
203c0e53476SArnaldo Carvalho de Melo 		if (evsel->handler != NULL)
204c0e53476SArnaldo Carvalho de Melo 			goto out;
205c0e53476SArnaldo Carvalho de Melo 		evsel->handler = assocs[i].handler;
206c0e53476SArnaldo Carvalho de Melo 	}
207c0e53476SArnaldo Carvalho de Melo 
208c0e53476SArnaldo Carvalho de Melo 	err = 0;
209c0e53476SArnaldo Carvalho de Melo out:
210c0e53476SArnaldo Carvalho de Melo 	return err;
211c0e53476SArnaldo Carvalho de Melo }
212c0e53476SArnaldo Carvalho de Melo 
21363dab225SArnaldo Carvalho de Melo void __perf_evlist__set_leader(struct list_head *list)
21463dab225SArnaldo Carvalho de Melo {
21532dcd021SJiri Olsa 	struct evsel *evsel, *leader;
21663dab225SArnaldo Carvalho de Melo 
217b27c4eceSJiri Olsa 	leader = list_entry(list->next, struct evsel, core.node);
218b27c4eceSJiri Olsa 	evsel = list_entry(list->prev, struct evsel, core.node);
21997f63e4aSNamhyung Kim 
2205643b1a5SJiri Olsa 	leader->core.nr_members = evsel->idx - leader->idx + 1;
22163dab225SArnaldo Carvalho de Melo 
222e5cadb93SArnaldo Carvalho de Melo 	__evlist__for_each_entry(list, evsel) {
22363dab225SArnaldo Carvalho de Melo 		evsel->leader = leader;
22463dab225SArnaldo Carvalho de Melo 	}
22563dab225SArnaldo Carvalho de Melo }
22663dab225SArnaldo Carvalho de Melo 
22763503dbaSJiri Olsa void perf_evlist__set_leader(struct evlist *evlist)
2286a4bb04cSJiri Olsa {
2296484d2f9SJiri Olsa 	if (evlist->core.nr_entries) {
2306484d2f9SJiri Olsa 		evlist->nr_groups = evlist->core.nr_entries > 1 ? 1 : 0;
231ce9036a6SJiri Olsa 		__perf_evlist__set_leader(&evlist->core.entries);
2326a4bb04cSJiri Olsa 	}
23397f63e4aSNamhyung Kim }
2346a4bb04cSJiri Olsa 
23563503dbaSJiri Olsa int __perf_evlist__add_default(struct evlist *evlist, bool precise)
236361c99a6SArnaldo Carvalho de Melo {
23732dcd021SJiri Olsa 	struct evsel *evsel = perf_evsel__new_cycles(precise);
238361c99a6SArnaldo Carvalho de Melo 
239361c99a6SArnaldo Carvalho de Melo 	if (evsel == NULL)
2407c48dcfdSArnaldo Carvalho de Melo 		return -ENOMEM;
241361c99a6SArnaldo Carvalho de Melo 
242a1cf3a75SJiri Olsa 	evlist__add(evlist, evsel);
243361c99a6SArnaldo Carvalho de Melo 	return 0;
244361c99a6SArnaldo Carvalho de Melo }
2455c581041SArnaldo Carvalho de Melo 
24663503dbaSJiri Olsa int perf_evlist__add_dummy(struct evlist *evlist)
2475bae0250SArnaldo Carvalho de Melo {
2485bae0250SArnaldo Carvalho de Melo 	struct perf_event_attr attr = {
2495bae0250SArnaldo Carvalho de Melo 		.type	= PERF_TYPE_SOFTWARE,
2505bae0250SArnaldo Carvalho de Melo 		.config = PERF_COUNT_SW_DUMMY,
2515bae0250SArnaldo Carvalho de Melo 		.size	= sizeof(attr), /* to capture ABI version */
2525bae0250SArnaldo Carvalho de Melo 	};
2536484d2f9SJiri Olsa 	struct evsel *evsel = perf_evsel__new_idx(&attr, evlist->core.nr_entries);
2545bae0250SArnaldo Carvalho de Melo 
2555bae0250SArnaldo Carvalho de Melo 	if (evsel == NULL)
2565bae0250SArnaldo Carvalho de Melo 		return -ENOMEM;
2575bae0250SArnaldo Carvalho de Melo 
258a1cf3a75SJiri Olsa 	evlist__add(evlist, evsel);
2595bae0250SArnaldo Carvalho de Melo 	return 0;
2605bae0250SArnaldo Carvalho de Melo }
2615bae0250SArnaldo Carvalho de Melo 
262a1cf3a75SJiri Olsa static int evlist__add_attrs(struct evlist *evlist,
26350d08e47SArnaldo Carvalho de Melo 				  struct perf_event_attr *attrs, size_t nr_attrs)
26450d08e47SArnaldo Carvalho de Melo {
26532dcd021SJiri Olsa 	struct evsel *evsel, *n;
26650d08e47SArnaldo Carvalho de Melo 	LIST_HEAD(head);
26750d08e47SArnaldo Carvalho de Melo 	size_t i;
26850d08e47SArnaldo Carvalho de Melo 
26950d08e47SArnaldo Carvalho de Melo 	for (i = 0; i < nr_attrs; i++) {
2706484d2f9SJiri Olsa 		evsel = perf_evsel__new_idx(attrs + i, evlist->core.nr_entries + i);
27150d08e47SArnaldo Carvalho de Melo 		if (evsel == NULL)
27250d08e47SArnaldo Carvalho de Melo 			goto out_delete_partial_list;
273b27c4eceSJiri Olsa 		list_add_tail(&evsel->core.node, &head);
27450d08e47SArnaldo Carvalho de Melo 	}
27550d08e47SArnaldo Carvalho de Melo 
276f114d6efSAdrian Hunter 	perf_evlist__splice_list_tail(evlist, &head);
27750d08e47SArnaldo Carvalho de Melo 
27850d08e47SArnaldo Carvalho de Melo 	return 0;
27950d08e47SArnaldo Carvalho de Melo 
28050d08e47SArnaldo Carvalho de Melo out_delete_partial_list:
281e5cadb93SArnaldo Carvalho de Melo 	__evlist__for_each_entry_safe(&head, n, evsel)
2825eb2dd2aSJiri Olsa 		evsel__delete(evsel);
28350d08e47SArnaldo Carvalho de Melo 	return -1;
28450d08e47SArnaldo Carvalho de Melo }
28550d08e47SArnaldo Carvalho de Melo 
28663503dbaSJiri Olsa int __perf_evlist__add_default_attrs(struct evlist *evlist,
28779695e1bSArnaldo Carvalho de Melo 				     struct perf_event_attr *attrs, size_t nr_attrs)
28879695e1bSArnaldo Carvalho de Melo {
28979695e1bSArnaldo Carvalho de Melo 	size_t i;
29079695e1bSArnaldo Carvalho de Melo 
29179695e1bSArnaldo Carvalho de Melo 	for (i = 0; i < nr_attrs; i++)
29279695e1bSArnaldo Carvalho de Melo 		event_attr_init(attrs + i);
29379695e1bSArnaldo Carvalho de Melo 
294a1cf3a75SJiri Olsa 	return evlist__add_attrs(evlist, attrs, nr_attrs);
29579695e1bSArnaldo Carvalho de Melo }
29679695e1bSArnaldo Carvalho de Melo 
29732dcd021SJiri Olsa struct evsel *
29863503dbaSJiri Olsa perf_evlist__find_tracepoint_by_id(struct evlist *evlist, int id)
299ee29be62SArnaldo Carvalho de Melo {
30032dcd021SJiri Olsa 	struct evsel *evsel;
301ee29be62SArnaldo Carvalho de Melo 
302e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, evsel) {
3031fc632ceSJiri Olsa 		if (evsel->core.attr.type   == PERF_TYPE_TRACEPOINT &&
3041fc632ceSJiri Olsa 		    (int)evsel->core.attr.config == id)
305ee29be62SArnaldo Carvalho de Melo 			return evsel;
306ee29be62SArnaldo Carvalho de Melo 	}
307ee29be62SArnaldo Carvalho de Melo 
308ee29be62SArnaldo Carvalho de Melo 	return NULL;
309ee29be62SArnaldo Carvalho de Melo }
310ee29be62SArnaldo Carvalho de Melo 
31132dcd021SJiri Olsa struct evsel *
31263503dbaSJiri Olsa perf_evlist__find_tracepoint_by_name(struct evlist *evlist,
313a2f2804aSDavid Ahern 				     const char *name)
314a2f2804aSDavid Ahern {
31532dcd021SJiri Olsa 	struct evsel *evsel;
316a2f2804aSDavid Ahern 
317e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, evsel) {
3181fc632ceSJiri Olsa 		if ((evsel->core.attr.type == PERF_TYPE_TRACEPOINT) &&
319a2f2804aSDavid Ahern 		    (strcmp(evsel->name, name) == 0))
320a2f2804aSDavid Ahern 			return evsel;
321a2f2804aSDavid Ahern 	}
322a2f2804aSDavid Ahern 
323a2f2804aSDavid Ahern 	return NULL;
324a2f2804aSDavid Ahern }
325a2f2804aSDavid Ahern 
32663503dbaSJiri Olsa int perf_evlist__add_newtp(struct evlist *evlist,
32739876e7dSArnaldo Carvalho de Melo 			   const char *sys, const char *name, void *handler)
32839876e7dSArnaldo Carvalho de Melo {
32932dcd021SJiri Olsa 	struct evsel *evsel = perf_evsel__newtp(sys, name);
33039876e7dSArnaldo Carvalho de Melo 
3318dd2a131SJiri Olsa 	if (IS_ERR(evsel))
33239876e7dSArnaldo Carvalho de Melo 		return -1;
33339876e7dSArnaldo Carvalho de Melo 
334744a9719SArnaldo Carvalho de Melo 	evsel->handler = handler;
335a1cf3a75SJiri Olsa 	evlist__add(evlist, evsel);
33639876e7dSArnaldo Carvalho de Melo 	return 0;
33739876e7dSArnaldo Carvalho de Melo }
33839876e7dSArnaldo Carvalho de Melo 
33963503dbaSJiri Olsa static int perf_evlist__nr_threads(struct evlist *evlist,
34032dcd021SJiri Olsa 				   struct evsel *evsel)
341bf8e8f4bSAdrian Hunter {
342648b5af3SJiri Olsa 	if (evsel->core.system_wide)
343bf8e8f4bSAdrian Hunter 		return 1;
344bf8e8f4bSAdrian Hunter 	else
345a2f354e3SJiri Olsa 		return perf_thread_map__nr(evlist->core.threads);
346bf8e8f4bSAdrian Hunter }
347bf8e8f4bSAdrian Hunter 
348e74676deSJiri Olsa void evlist__disable(struct evlist *evlist)
3494152ab37SArnaldo Carvalho de Melo {
35032dcd021SJiri Olsa 	struct evsel *pos;
3514152ab37SArnaldo Carvalho de Melo 
352e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, pos) {
3539dfcb759SJiri Olsa 		if (pos->disabled || !perf_evsel__is_group_leader(pos) || !pos->core.fd)
3543fe4430dSJiri Olsa 			continue;
3559a10bb22SJiri Olsa 		evsel__disable(pos);
3564152ab37SArnaldo Carvalho de Melo 	}
3572b56bcfbSArnaldo Carvalho de Melo 
3582b56bcfbSArnaldo Carvalho de Melo 	evlist->enabled = false;
3594152ab37SArnaldo Carvalho de Melo }
3604152ab37SArnaldo Carvalho de Melo 
3611c87f165SJiri Olsa void evlist__enable(struct evlist *evlist)
362764e16a3SDavid Ahern {
36332dcd021SJiri Olsa 	struct evsel *pos;
364764e16a3SDavid Ahern 
365e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, pos) {
3669dfcb759SJiri Olsa 		if (!perf_evsel__is_group_leader(pos) || !pos->core.fd)
3673fe4430dSJiri Olsa 			continue;
368ec7f24efSJiri Olsa 		evsel__enable(pos);
369764e16a3SDavid Ahern 	}
3702b56bcfbSArnaldo Carvalho de Melo 
3712b56bcfbSArnaldo Carvalho de Melo 	evlist->enabled = true;
3722b56bcfbSArnaldo Carvalho de Melo }
3732b56bcfbSArnaldo Carvalho de Melo 
37463503dbaSJiri Olsa void perf_evlist__toggle_enable(struct evlist *evlist)
3752b56bcfbSArnaldo Carvalho de Melo {
376e74676deSJiri Olsa 	(evlist->enabled ? evlist__disable : evlist__enable)(evlist);
377764e16a3SDavid Ahern }
378764e16a3SDavid Ahern 
37963503dbaSJiri Olsa static int perf_evlist__enable_event_cpu(struct evlist *evlist,
38032dcd021SJiri Olsa 					 struct evsel *evsel, int cpu)
3811c65056cSAdrian Hunter {
38218ef15c6SArnaldo Carvalho de Melo 	int thread;
3831c65056cSAdrian Hunter 	int nr_threads = perf_evlist__nr_threads(evlist, evsel);
3841c65056cSAdrian Hunter 
3859dfcb759SJiri Olsa 	if (!evsel->core.fd)
3861c65056cSAdrian Hunter 		return -EINVAL;
3871c65056cSAdrian Hunter 
3881c65056cSAdrian Hunter 	for (thread = 0; thread < nr_threads; thread++) {
38918ef15c6SArnaldo Carvalho de Melo 		int err = ioctl(FD(evsel, cpu, thread), PERF_EVENT_IOC_ENABLE, 0);
3901c65056cSAdrian Hunter 		if (err)
3911c65056cSAdrian Hunter 			return err;
3921c65056cSAdrian Hunter 	}
3931c65056cSAdrian Hunter 	return 0;
3941c65056cSAdrian Hunter }
3951c65056cSAdrian Hunter 
39663503dbaSJiri Olsa static int perf_evlist__enable_event_thread(struct evlist *evlist,
39732dcd021SJiri Olsa 					    struct evsel *evsel,
3981c65056cSAdrian Hunter 					    int thread)
3991c65056cSAdrian Hunter {
40018ef15c6SArnaldo Carvalho de Melo 	int cpu;
4016549cd8fSJiri Olsa 	int nr_cpus = perf_cpu_map__nr(evlist->core.cpus);
4021c65056cSAdrian Hunter 
4039dfcb759SJiri Olsa 	if (!evsel->core.fd)
4041c65056cSAdrian Hunter 		return -EINVAL;
4051c65056cSAdrian Hunter 
4061c65056cSAdrian Hunter 	for (cpu = 0; cpu < nr_cpus; cpu++) {
40718ef15c6SArnaldo Carvalho de Melo 		int err = ioctl(FD(evsel, cpu, thread), PERF_EVENT_IOC_ENABLE, 0);
4081c65056cSAdrian Hunter 		if (err)
4091c65056cSAdrian Hunter 			return err;
4101c65056cSAdrian Hunter 	}
4111c65056cSAdrian Hunter 	return 0;
4121c65056cSAdrian Hunter }
4131c65056cSAdrian Hunter 
41463503dbaSJiri Olsa int perf_evlist__enable_event_idx(struct evlist *evlist,
41532dcd021SJiri Olsa 				  struct evsel *evsel, int idx)
4161c65056cSAdrian Hunter {
417315c0a1fSJiri Olsa 	bool per_cpu_mmaps = !perf_cpu_map__empty(evlist->core.cpus);
4181c65056cSAdrian Hunter 
4191c65056cSAdrian Hunter 	if (per_cpu_mmaps)
4201c65056cSAdrian Hunter 		return perf_evlist__enable_event_cpu(evlist, evsel, idx);
4211c65056cSAdrian Hunter 	else
4221c65056cSAdrian Hunter 		return perf_evlist__enable_event_thread(evlist, evsel, idx);
4231c65056cSAdrian Hunter }
4241c65056cSAdrian Hunter 
425f4009e7bSJiri Olsa int evlist__add_pollfd(struct evlist *evlist, int fd)
426e4b356b5SArnaldo Carvalho de Melo {
427f4009e7bSJiri Olsa 	return perf_evlist__add_pollfd(&evlist->core, fd, NULL, POLLIN);
428e4b356b5SArnaldo Carvalho de Melo }
4291b85337dSArnaldo Carvalho de Melo 
430258e4bfcSWang Nan static void perf_evlist__munmap_filtered(struct fdarray *fda, int fd,
431258e4bfcSWang Nan 					 void *arg __maybe_unused)
432e4b356b5SArnaldo Carvalho de Melo {
433a5830532SJiri Olsa 	struct mmap *map = fda->priv[fd].ptr;
434e4b356b5SArnaldo Carvalho de Melo 
4354876075bSWang Nan 	if (map)
4364876075bSWang Nan 		perf_mmap__put(map);
43770082dd9SArnaldo Carvalho de Melo }
43870db7533SArnaldo Carvalho de Melo 
439f4009e7bSJiri Olsa int evlist__filter_pollfd(struct evlist *evlist, short revents_and_mask)
4401ddec7f0SArnaldo Carvalho de Melo {
44140cb2d51SJiri Olsa 	return fdarray__filter(&evlist->core.pollfd, revents_and_mask,
442258e4bfcSWang Nan 			       perf_evlist__munmap_filtered, NULL);
4431ddec7f0SArnaldo Carvalho de Melo }
4441ddec7f0SArnaldo Carvalho de Melo 
44580ab2987SJiri Olsa int evlist__poll(struct evlist *evlist, int timeout)
446f66a889dSArnaldo Carvalho de Melo {
44780ab2987SJiri Olsa 	return perf_evlist__poll(&evlist->core, timeout);
448f66a889dSArnaldo Carvalho de Melo }
449f66a889dSArnaldo Carvalho de Melo 
45063503dbaSJiri Olsa static void perf_evlist__set_sid_idx(struct evlist *evlist,
45132dcd021SJiri Olsa 				     struct evsel *evsel, int idx, int cpu,
4523c659eedSAdrian Hunter 				     int thread)
4533c659eedSAdrian Hunter {
4543c659eedSAdrian Hunter 	struct perf_sample_id *sid = SID(evsel, cpu, thread);
4553c659eedSAdrian Hunter 	sid->idx = idx;
456f72f901dSJiri Olsa 	if (evlist->core.cpus && cpu >= 0)
457f72f901dSJiri Olsa 		sid->cpu = evlist->core.cpus->map[cpu];
4583c659eedSAdrian Hunter 	else
4593c659eedSAdrian Hunter 		sid->cpu = -1;
460648b5af3SJiri Olsa 	if (!evsel->core.system_wide && evlist->core.threads && thread >= 0)
461a2f354e3SJiri Olsa 		sid->tid = perf_thread_map__pid(evlist->core.threads, thread);
4623c659eedSAdrian Hunter 	else
4633c659eedSAdrian Hunter 		sid->tid = -1;
4643c659eedSAdrian Hunter }
4653c659eedSAdrian Hunter 
46663503dbaSJiri Olsa struct perf_sample_id *perf_evlist__id2sid(struct evlist *evlist, u64 id)
46770db7533SArnaldo Carvalho de Melo {
46870db7533SArnaldo Carvalho de Melo 	struct hlist_head *head;
46970db7533SArnaldo Carvalho de Melo 	struct perf_sample_id *sid;
47070db7533SArnaldo Carvalho de Melo 	int hash;
47170db7533SArnaldo Carvalho de Melo 
47270db7533SArnaldo Carvalho de Melo 	hash = hash_64(id, PERF_EVLIST__HLIST_BITS);
4731d5af02dSJiri Olsa 	head = &evlist->core.heads[hash];
47470db7533SArnaldo Carvalho de Melo 
475b67bfe0dSSasha Levin 	hlist_for_each_entry(sid, head, node)
47670db7533SArnaldo Carvalho de Melo 		if (sid->id == id)
477932a3594SJiri Olsa 			return sid;
478932a3594SJiri Olsa 
479932a3594SJiri Olsa 	return NULL;
480932a3594SJiri Olsa }
481932a3594SJiri Olsa 
48263503dbaSJiri Olsa struct evsel *perf_evlist__id2evsel(struct evlist *evlist, u64 id)
483932a3594SJiri Olsa {
484932a3594SJiri Olsa 	struct perf_sample_id *sid;
485932a3594SJiri Olsa 
4866484d2f9SJiri Olsa 	if (evlist->core.nr_entries == 1 || !id)
487515dbe48SJiri Olsa 		return evlist__first(evlist);
488932a3594SJiri Olsa 
489932a3594SJiri Olsa 	sid = perf_evlist__id2sid(evlist, id);
490932a3594SJiri Olsa 	if (sid)
49170c20369SJiri Olsa 		return container_of(sid->evsel, struct evsel, core);
49230e68bccSNamhyung Kim 
49330e68bccSNamhyung Kim 	if (!perf_evlist__sample_id_all(evlist))
494515dbe48SJiri Olsa 		return evlist__first(evlist);
49530e68bccSNamhyung Kim 
49670db7533SArnaldo Carvalho de Melo 	return NULL;
49770db7533SArnaldo Carvalho de Melo }
49804391debSArnaldo Carvalho de Melo 
49963503dbaSJiri Olsa struct evsel *perf_evlist__id2evsel_strict(struct evlist *evlist,
500dddcf6abSAdrian Hunter 						u64 id)
501dddcf6abSAdrian Hunter {
502dddcf6abSAdrian Hunter 	struct perf_sample_id *sid;
503dddcf6abSAdrian Hunter 
504dddcf6abSAdrian Hunter 	if (!id)
505dddcf6abSAdrian Hunter 		return NULL;
506dddcf6abSAdrian Hunter 
507dddcf6abSAdrian Hunter 	sid = perf_evlist__id2sid(evlist, id);
508dddcf6abSAdrian Hunter 	if (sid)
50970c20369SJiri Olsa 		return container_of(sid->evsel, struct evsel, core);
510dddcf6abSAdrian Hunter 
511dddcf6abSAdrian Hunter 	return NULL;
512dddcf6abSAdrian Hunter }
513dddcf6abSAdrian Hunter 
51463503dbaSJiri Olsa static int perf_evlist__event2id(struct evlist *evlist,
51575562573SAdrian Hunter 				 union perf_event *event, u64 *id)
51675562573SAdrian Hunter {
517b1fcd190SJiri Olsa 	const __u64 *array = event->sample.array;
51875562573SAdrian Hunter 	ssize_t n;
51975562573SAdrian Hunter 
52075562573SAdrian Hunter 	n = (event->header.size - sizeof(event->header)) >> 3;
52175562573SAdrian Hunter 
52275562573SAdrian Hunter 	if (event->header.type == PERF_RECORD_SAMPLE) {
52375562573SAdrian Hunter 		if (evlist->id_pos >= n)
52475562573SAdrian Hunter 			return -1;
52575562573SAdrian Hunter 		*id = array[evlist->id_pos];
52675562573SAdrian Hunter 	} else {
52775562573SAdrian Hunter 		if (evlist->is_pos > n)
52875562573SAdrian Hunter 			return -1;
52975562573SAdrian Hunter 		n -= evlist->is_pos;
53075562573SAdrian Hunter 		*id = array[n];
53175562573SAdrian Hunter 	}
53275562573SAdrian Hunter 	return 0;
53375562573SAdrian Hunter }
53475562573SAdrian Hunter 
53563503dbaSJiri Olsa struct evsel *perf_evlist__event2evsel(struct evlist *evlist,
53675562573SAdrian Hunter 					    union perf_event *event)
53775562573SAdrian Hunter {
538515dbe48SJiri Olsa 	struct evsel *first = evlist__first(evlist);
53975562573SAdrian Hunter 	struct hlist_head *head;
54075562573SAdrian Hunter 	struct perf_sample_id *sid;
54175562573SAdrian Hunter 	int hash;
54275562573SAdrian Hunter 	u64 id;
54375562573SAdrian Hunter 
5446484d2f9SJiri Olsa 	if (evlist->core.nr_entries == 1)
54598be6966SAdrian Hunter 		return first;
54698be6966SAdrian Hunter 
5471fc632ceSJiri Olsa 	if (!first->core.attr.sample_id_all &&
54898be6966SAdrian Hunter 	    event->header.type != PERF_RECORD_SAMPLE)
54998be6966SAdrian Hunter 		return first;
55075562573SAdrian Hunter 
55175562573SAdrian Hunter 	if (perf_evlist__event2id(evlist, event, &id))
55275562573SAdrian Hunter 		return NULL;
55375562573SAdrian Hunter 
55475562573SAdrian Hunter 	/* Synthesized events have an id of zero */
55575562573SAdrian Hunter 	if (!id)
55698be6966SAdrian Hunter 		return first;
55775562573SAdrian Hunter 
55875562573SAdrian Hunter 	hash = hash_64(id, PERF_EVLIST__HLIST_BITS);
5591d5af02dSJiri Olsa 	head = &evlist->core.heads[hash];
56075562573SAdrian Hunter 
56175562573SAdrian Hunter 	hlist_for_each_entry(sid, head, node) {
56275562573SAdrian Hunter 		if (sid->id == id)
56370c20369SJiri Olsa 			return container_of(sid->evsel, struct evsel, core);
56475562573SAdrian Hunter 	}
56575562573SAdrian Hunter 	return NULL;
56675562573SAdrian Hunter }
56775562573SAdrian Hunter 
56863503dbaSJiri Olsa static int perf_evlist__set_paused(struct evlist *evlist, bool value)
56965aea233SWang Nan {
57065aea233SWang Nan 	int i;
57165aea233SWang Nan 
5720b72d69aSWang Nan 	if (!evlist->overwrite_mmap)
573078c3386SWang Nan 		return 0;
574078c3386SWang Nan 
575c976ee11SJiri Olsa 	for (i = 0; i < evlist->core.nr_mmaps; i++) {
5762cf07b29SJiri Olsa 		int fd = evlist->overwrite_mmap[i].core.fd;
57765aea233SWang Nan 		int err;
57865aea233SWang Nan 
57965aea233SWang Nan 		if (fd < 0)
58065aea233SWang Nan 			continue;
58165aea233SWang Nan 		err = ioctl(fd, PERF_EVENT_IOC_PAUSE_OUTPUT, value ? 1 : 0);
58265aea233SWang Nan 		if (err)
58365aea233SWang Nan 			return err;
58465aea233SWang Nan 	}
58565aea233SWang Nan 	return 0;
58665aea233SWang Nan }
58765aea233SWang Nan 
58863503dbaSJiri Olsa static int perf_evlist__pause(struct evlist *evlist)
58965aea233SWang Nan {
59065aea233SWang Nan 	return perf_evlist__set_paused(evlist, true);
59165aea233SWang Nan }
59265aea233SWang Nan 
59363503dbaSJiri Olsa static int perf_evlist__resume(struct evlist *evlist)
59465aea233SWang Nan {
59565aea233SWang Nan 	return perf_evlist__set_paused(evlist, false);
59665aea233SWang Nan }
59765aea233SWang Nan 
598db6b7b13SJiri Olsa static void evlist__munmap_nofree(struct evlist *evlist)
599f8a95309SArnaldo Carvalho de Melo {
600aece948fSArnaldo Carvalho de Melo 	int i;
601f8a95309SArnaldo Carvalho de Melo 
602b2cb615dSWang Nan 	if (evlist->mmap)
603c976ee11SJiri Olsa 		for (i = 0; i < evlist->core.nr_mmaps; i++)
604a1f72618SWang Nan 			perf_mmap__munmap(&evlist->mmap[i]);
605b2cb615dSWang Nan 
6060b72d69aSWang Nan 	if (evlist->overwrite_mmap)
607c976ee11SJiri Olsa 		for (i = 0; i < evlist->core.nr_mmaps; i++)
6080b72d69aSWang Nan 			perf_mmap__munmap(&evlist->overwrite_mmap[i]);
609a1f72618SWang Nan }
610aece948fSArnaldo Carvalho de Melo 
611db6b7b13SJiri Olsa void evlist__munmap(struct evlist *evlist)
612a1f72618SWang Nan {
613db6b7b13SJiri Olsa 	evlist__munmap_nofree(evlist);
61404662523SArnaldo Carvalho de Melo 	zfree(&evlist->mmap);
6150b72d69aSWang Nan 	zfree(&evlist->overwrite_mmap);
616f8a95309SArnaldo Carvalho de Melo }
617f8a95309SArnaldo Carvalho de Melo 
618d50cf361SJiri Olsa static struct mmap *evlist__alloc_mmap(struct evlist *evlist,
6192c5f6d87SKan Liang 				       bool overwrite)
620f8a95309SArnaldo Carvalho de Melo {
621d4c6fb36SWang Nan 	int i;
622a5830532SJiri Olsa 	struct mmap *map;
623d4c6fb36SWang Nan 
624c976ee11SJiri Olsa 	evlist->core.nr_mmaps = perf_cpu_map__nr(evlist->core.cpus);
625315c0a1fSJiri Olsa 	if (perf_cpu_map__empty(evlist->core.cpus))
626c976ee11SJiri Olsa 		evlist->core.nr_mmaps = perf_thread_map__nr(evlist->core.threads);
627c976ee11SJiri Olsa 	map = zalloc(evlist->core.nr_mmaps * sizeof(struct mmap));
6288db6d6b1SWang Nan 	if (!map)
6298db6d6b1SWang Nan 		return NULL;
630946ae1d4SWang Nan 
631c976ee11SJiri Olsa 	for (i = 0; i < evlist->core.nr_mmaps; i++) {
6322cf07b29SJiri Olsa 		map[i].core.fd = -1;
6338df7a869SJiri Olsa 		map[i].core.overwrite = overwrite;
6344738ca30SArnaldo Carvalho de Melo 		/*
6354738ca30SArnaldo Carvalho de Melo 		 * When the perf_mmap() call is made we grab one refcount, plus
6366afad54dSKan Liang 		 * one extra to let perf_mmap__consume() get the last
6374738ca30SArnaldo Carvalho de Melo 		 * events after all real references (perf_mmap__get()) are
6384738ca30SArnaldo Carvalho de Melo 		 * dropped.
6394738ca30SArnaldo Carvalho de Melo 		 *
6404738ca30SArnaldo Carvalho de Melo 		 * Each PERF_EVENT_IOC_SET_OUTPUT points to this mmap and
6414738ca30SArnaldo Carvalho de Melo 		 * thus does perf_mmap__get() on it.
6424738ca30SArnaldo Carvalho de Melo 		 */
643e03edfeaSJiri Olsa 		refcount_set(&map[i].core.refcnt, 0);
6444738ca30SArnaldo Carvalho de Melo 	}
6458db6d6b1SWang Nan 	return map;
646f8a95309SArnaldo Carvalho de Melo }
647f8a95309SArnaldo Carvalho de Melo 
648f3058a1cSWang Nan static bool
64963503dbaSJiri Olsa perf_evlist__should_poll(struct evlist *evlist __maybe_unused,
65032dcd021SJiri Olsa 			 struct evsel *evsel)
651f3058a1cSWang Nan {
6521fc632ceSJiri Olsa 	if (evsel->core.attr.write_backward)
653f3058a1cSWang Nan 		return false;
654f3058a1cSWang Nan 	return true;
655f3058a1cSWang Nan }
656f3058a1cSWang Nan 
6579521b5f2SJiri Olsa static int evlist__mmap_per_evsel(struct evlist *evlist, int idx,
6589f21b815SMark Rutland 				       struct mmap_params *mp, int cpu_idx,
6590b72d69aSWang Nan 				       int thread, int *_output, int *_output_overwrite)
660aece948fSArnaldo Carvalho de Melo {
66132dcd021SJiri Olsa 	struct evsel *evsel;
662f3058a1cSWang Nan 	int revent;
663f72f901dSJiri Olsa 	int evlist_cpu = cpu_map__cpu(evlist->core.cpus, cpu_idx);
66404e21314SAdrian Hunter 
665e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, evsel) {
666a5830532SJiri Olsa 		struct mmap *maps = evlist->mmap;
667078c3386SWang Nan 		int *output = _output;
668bf8e8f4bSAdrian Hunter 		int fd;
6699f21b815SMark Rutland 		int cpu;
670bf8e8f4bSAdrian Hunter 
67171f566a3SWang Nan 		mp->prot = PROT_READ | PROT_WRITE;
6721fc632ceSJiri Olsa 		if (evsel->core.attr.write_backward) {
6730b72d69aSWang Nan 			output = _output_overwrite;
6740b72d69aSWang Nan 			maps = evlist->overwrite_mmap;
675078c3386SWang Nan 
676078c3386SWang Nan 			if (!maps) {
677d50cf361SJiri Olsa 				maps = evlist__alloc_mmap(evlist, true);
678078c3386SWang Nan 				if (!maps)
679078c3386SWang Nan 					return -1;
6800b72d69aSWang Nan 				evlist->overwrite_mmap = maps;
68154cc54deSWang Nan 				if (evlist->bkw_mmap_state == BKW_MMAP_NOTREADY)
68254cc54deSWang Nan 					perf_evlist__toggle_bkw_mmap(evlist, BKW_MMAP_RUNNING);
683078c3386SWang Nan 			}
68471f566a3SWang Nan 			mp->prot &= ~PROT_WRITE;
685078c3386SWang Nan 		}
686f3058a1cSWang Nan 
687648b5af3SJiri Olsa 		if (evsel->core.system_wide && thread)
688bf8e8f4bSAdrian Hunter 			continue;
689bf8e8f4bSAdrian Hunter 
690b4df75deSJiri Olsa 		cpu = perf_cpu_map__idx(evsel->core.cpus, evlist_cpu);
6919f21b815SMark Rutland 		if (cpu == -1)
6929f21b815SMark Rutland 			continue;
6939f21b815SMark Rutland 
694bf8e8f4bSAdrian Hunter 		fd = FD(evsel, cpu, thread);
69504e21314SAdrian Hunter 
69604e21314SAdrian Hunter 		if (*output == -1) {
69704e21314SAdrian Hunter 			*output = fd;
698078c3386SWang Nan 
69931fb4c0dSJiri Olsa 			if (perf_mmap__mmap(&maps[idx], mp, *output, evlist_cpu) < 0)
70004e21314SAdrian Hunter 				return -1;
70104e21314SAdrian Hunter 		} else {
70204e21314SAdrian Hunter 			if (ioctl(fd, PERF_EVENT_IOC_SET_OUTPUT, *output) != 0)
70304e21314SAdrian Hunter 				return -1;
70482396986SArnaldo Carvalho de Melo 
705078c3386SWang Nan 			perf_mmap__get(&maps[idx]);
70604e21314SAdrian Hunter 		}
70704e21314SAdrian Hunter 
708f3058a1cSWang Nan 		revent = perf_evlist__should_poll(evlist, evsel) ? POLLIN : 0;
709f3058a1cSWang Nan 
710f90d194aSAdrian Hunter 		/*
711f90d194aSAdrian Hunter 		 * The system_wide flag causes a selected event to be opened
712f90d194aSAdrian Hunter 		 * always without a pid.  Consequently it will never get a
713f90d194aSAdrian Hunter 		 * POLLHUP, but it is used for tracking in combination with
714f90d194aSAdrian Hunter 		 * other events, so it should not need to be polled anyway.
715f90d194aSAdrian Hunter 		 * Therefore don't add it for polling.
716f90d194aSAdrian Hunter 		 */
717648b5af3SJiri Olsa 		if (!evsel->core.system_wide &&
718f4009e7bSJiri Olsa 		     perf_evlist__add_pollfd(&evlist->core, fd, &maps[idx], revent) < 0) {
719078c3386SWang Nan 			perf_mmap__put(&maps[idx]);
720ad6765ddSArnaldo Carvalho de Melo 			return -1;
72182396986SArnaldo Carvalho de Melo 		}
722033fa713SArnaldo Carvalho de Melo 
7231fc632ceSJiri Olsa 		if (evsel->core.attr.read_format & PERF_FORMAT_ID) {
724d5a99483SJiri Olsa 			if (perf_evlist__id_add_fd(&evlist->core, &evsel->core, cpu, thread,
7253c659eedSAdrian Hunter 						   fd) < 0)
72604e21314SAdrian Hunter 				return -1;
7273c659eedSAdrian Hunter 			perf_evlist__set_sid_idx(evlist, evsel, idx, cpu,
7283c659eedSAdrian Hunter 						 thread);
7293c659eedSAdrian Hunter 		}
73004e21314SAdrian Hunter 	}
73104e21314SAdrian Hunter 
73204e21314SAdrian Hunter 	return 0;
73304e21314SAdrian Hunter }
73404e21314SAdrian Hunter 
7359521b5f2SJiri Olsa static int evlist__mmap_per_cpu(struct evlist *evlist,
736a8a8f3ebSAdrian Hunter 				     struct mmap_params *mp)
73704e21314SAdrian Hunter {
738aece948fSArnaldo Carvalho de Melo 	int cpu, thread;
7396549cd8fSJiri Olsa 	int nr_cpus = perf_cpu_map__nr(evlist->core.cpus);
740a2f354e3SJiri Olsa 	int nr_threads = perf_thread_map__nr(evlist->core.threads);
741aece948fSArnaldo Carvalho de Melo 
742e3e1a54fSAdrian Hunter 	pr_debug2("perf event ring buffer mmapped per cpu\n");
743b3a319d5SNamhyung Kim 	for (cpu = 0; cpu < nr_cpus; cpu++) {
744aece948fSArnaldo Carvalho de Melo 		int output = -1;
7450b72d69aSWang Nan 		int output_overwrite = -1;
746aece948fSArnaldo Carvalho de Melo 
747718c602dSAdrian Hunter 		auxtrace_mmap_params__set_idx(&mp->auxtrace_mp, evlist, cpu,
748718c602dSAdrian Hunter 					      true);
749718c602dSAdrian Hunter 
750b3a319d5SNamhyung Kim 		for (thread = 0; thread < nr_threads; thread++) {
7519521b5f2SJiri Olsa 			if (evlist__mmap_per_evsel(evlist, cpu, mp, cpu,
7520b72d69aSWang Nan 							thread, &output, &output_overwrite))
753aece948fSArnaldo Carvalho de Melo 				goto out_unmap;
754aece948fSArnaldo Carvalho de Melo 		}
755aece948fSArnaldo Carvalho de Melo 	}
756aece948fSArnaldo Carvalho de Melo 
757aece948fSArnaldo Carvalho de Melo 	return 0;
758aece948fSArnaldo Carvalho de Melo 
759aece948fSArnaldo Carvalho de Melo out_unmap:
760db6b7b13SJiri Olsa 	evlist__munmap_nofree(evlist);
761aece948fSArnaldo Carvalho de Melo 	return -1;
762aece948fSArnaldo Carvalho de Melo }
763aece948fSArnaldo Carvalho de Melo 
7649521b5f2SJiri Olsa static int evlist__mmap_per_thread(struct evlist *evlist,
765a8a8f3ebSAdrian Hunter 					struct mmap_params *mp)
766aece948fSArnaldo Carvalho de Melo {
767aece948fSArnaldo Carvalho de Melo 	int thread;
768a2f354e3SJiri Olsa 	int nr_threads = perf_thread_map__nr(evlist->core.threads);
769aece948fSArnaldo Carvalho de Melo 
770e3e1a54fSAdrian Hunter 	pr_debug2("perf event ring buffer mmapped per thread\n");
771b3a319d5SNamhyung Kim 	for (thread = 0; thread < nr_threads; thread++) {
772aece948fSArnaldo Carvalho de Melo 		int output = -1;
7730b72d69aSWang Nan 		int output_overwrite = -1;
774aece948fSArnaldo Carvalho de Melo 
775718c602dSAdrian Hunter 		auxtrace_mmap_params__set_idx(&mp->auxtrace_mp, evlist, thread,
776718c602dSAdrian Hunter 					      false);
777718c602dSAdrian Hunter 
7789521b5f2SJiri Olsa 		if (evlist__mmap_per_evsel(evlist, thread, mp, 0, thread,
7790b72d69aSWang Nan 						&output, &output_overwrite))
780aece948fSArnaldo Carvalho de Melo 			goto out_unmap;
781aece948fSArnaldo Carvalho de Melo 	}
782aece948fSArnaldo Carvalho de Melo 
783aece948fSArnaldo Carvalho de Melo 	return 0;
784aece948fSArnaldo Carvalho de Melo 
785aece948fSArnaldo Carvalho de Melo out_unmap:
786db6b7b13SJiri Olsa 	evlist__munmap_nofree(evlist);
787aece948fSArnaldo Carvalho de Melo 	return -1;
788aece948fSArnaldo Carvalho de Melo }
789aece948fSArnaldo Carvalho de Melo 
790f5e7150cSArnaldo Carvalho de Melo unsigned long perf_event_mlock_kb_in_pages(void)
791994a1f78SJiri Olsa {
792f5e7150cSArnaldo Carvalho de Melo 	unsigned long pages;
7938185e881SArnaldo Carvalho de Melo 	int max;
7948185e881SArnaldo Carvalho de Melo 
7958185e881SArnaldo Carvalho de Melo 	if (sysctl__read_int("kernel/perf_event_mlock_kb", &max) < 0) {
7968185e881SArnaldo Carvalho de Melo 		/*
7978185e881SArnaldo Carvalho de Melo 		 * Pick a once upon a time good value, i.e. things look
7988185e881SArnaldo Carvalho de Melo 		 * strange since we can't read a sysctl value, but lets not
7998185e881SArnaldo Carvalho de Melo 		 * die yet...
8008185e881SArnaldo Carvalho de Melo 		 */
8018185e881SArnaldo Carvalho de Melo 		max = 512;
8028185e881SArnaldo Carvalho de Melo 	} else {
8038185e881SArnaldo Carvalho de Melo 		max -= (page_size / 1024);
8048185e881SArnaldo Carvalho de Melo 	}
8058185e881SArnaldo Carvalho de Melo 
8068185e881SArnaldo Carvalho de Melo 	pages = (max * 1024) / page_size;
8071be300f4SArnaldo Carvalho de Melo 	if (!is_power_of_2(pages))
8081be300f4SArnaldo Carvalho de Melo 		pages = rounddown_pow_of_two(pages);
809f5e7150cSArnaldo Carvalho de Melo 
810f5e7150cSArnaldo Carvalho de Melo 	return pages;
811f5e7150cSArnaldo Carvalho de Melo }
812f5e7150cSArnaldo Carvalho de Melo 
8139521b5f2SJiri Olsa size_t evlist__mmap_size(unsigned long pages)
814f5e7150cSArnaldo Carvalho de Melo {
815f5e7150cSArnaldo Carvalho de Melo 	if (pages == UINT_MAX)
816f5e7150cSArnaldo Carvalho de Melo 		pages = perf_event_mlock_kb_in_pages();
817f5e7150cSArnaldo Carvalho de Melo 	else if (!is_power_of_2(pages))
818994a1f78SJiri Olsa 		return 0;
819994a1f78SJiri Olsa 
820994a1f78SJiri Olsa 	return (pages + 1) * page_size;
821994a1f78SJiri Olsa }
822994a1f78SJiri Olsa 
82333c2dcfdSDavid Ahern static long parse_pages_arg(const char *str, unsigned long min,
82433c2dcfdSDavid Ahern 			    unsigned long max)
825994a1f78SJiri Olsa {
8262fbe4abeSAdrian Hunter 	unsigned long pages, val;
82727050f53SJiri Olsa 	static struct parse_tag tags[] = {
82827050f53SJiri Olsa 		{ .tag  = 'B', .mult = 1       },
82927050f53SJiri Olsa 		{ .tag  = 'K', .mult = 1 << 10 },
83027050f53SJiri Olsa 		{ .tag  = 'M', .mult = 1 << 20 },
83127050f53SJiri Olsa 		{ .tag  = 'G', .mult = 1 << 30 },
83227050f53SJiri Olsa 		{ .tag  = 0 },
83327050f53SJiri Olsa 	};
834994a1f78SJiri Olsa 
8358973504bSDavid Ahern 	if (str == NULL)
83633c2dcfdSDavid Ahern 		return -EINVAL;
8378973504bSDavid Ahern 
83827050f53SJiri Olsa 	val = parse_tag_value(str, tags);
8392fbe4abeSAdrian Hunter 	if (val != (unsigned long) -1) {
84027050f53SJiri Olsa 		/* we got file size value */
84127050f53SJiri Olsa 		pages = PERF_ALIGN(val, page_size) / page_size;
84227050f53SJiri Olsa 	} else {
84327050f53SJiri Olsa 		/* we got pages count value */
84427050f53SJiri Olsa 		char *eptr;
845994a1f78SJiri Olsa 		pages = strtoul(str, &eptr, 10);
84633c2dcfdSDavid Ahern 		if (*eptr != '\0')
84733c2dcfdSDavid Ahern 			return -EINVAL;
84827050f53SJiri Olsa 	}
849994a1f78SJiri Olsa 
8502bcab6c1SAdrian Hunter 	if (pages == 0 && min == 0) {
85133c2dcfdSDavid Ahern 		/* leave number of pages at 0 */
8521dbfa938SAdrian Hunter 	} else if (!is_power_of_2(pages)) {
8539808143bSJiri Olsa 		char buf[100];
8549808143bSJiri Olsa 
85533c2dcfdSDavid Ahern 		/* round pages up to next power of 2 */
85691529834SArnaldo Carvalho de Melo 		pages = roundup_pow_of_two(pages);
8571dbfa938SAdrian Hunter 		if (!pages)
8581dbfa938SAdrian Hunter 			return -EINVAL;
8599808143bSJiri Olsa 
8609808143bSJiri Olsa 		unit_number__scnprintf(buf, sizeof(buf), pages * page_size);
8619808143bSJiri Olsa 		pr_info("rounding mmap pages size to %s (%lu pages)\n",
8629808143bSJiri Olsa 			buf, pages);
8632fbe4abeSAdrian Hunter 	}
8642fbe4abeSAdrian Hunter 
86533c2dcfdSDavid Ahern 	if (pages > max)
86633c2dcfdSDavid Ahern 		return -EINVAL;
86733c2dcfdSDavid Ahern 
86833c2dcfdSDavid Ahern 	return pages;
86933c2dcfdSDavid Ahern }
87033c2dcfdSDavid Ahern 
871e9db1310SAdrian Hunter int __perf_evlist__parse_mmap_pages(unsigned int *mmap_pages, const char *str)
87233c2dcfdSDavid Ahern {
87333c2dcfdSDavid Ahern 	unsigned long max = UINT_MAX;
87433c2dcfdSDavid Ahern 	long pages;
87533c2dcfdSDavid Ahern 
876f5ae9c42SAdrian Hunter 	if (max > SIZE_MAX / page_size)
87733c2dcfdSDavid Ahern 		max = SIZE_MAX / page_size;
87833c2dcfdSDavid Ahern 
87933c2dcfdSDavid Ahern 	pages = parse_pages_arg(str, 1, max);
88033c2dcfdSDavid Ahern 	if (pages < 0) {
88133c2dcfdSDavid Ahern 		pr_err("Invalid argument for --mmap_pages/-m\n");
882994a1f78SJiri Olsa 		return -1;
883994a1f78SJiri Olsa 	}
884994a1f78SJiri Olsa 
885994a1f78SJiri Olsa 	*mmap_pages = pages;
886994a1f78SJiri Olsa 	return 0;
887994a1f78SJiri Olsa }
888994a1f78SJiri Olsa 
889e9db1310SAdrian Hunter int perf_evlist__parse_mmap_pages(const struct option *opt, const char *str,
890e9db1310SAdrian Hunter 				  int unset __maybe_unused)
891e9db1310SAdrian Hunter {
892e9db1310SAdrian Hunter 	return __perf_evlist__parse_mmap_pages(opt->value, str);
893e9db1310SAdrian Hunter }
894e9db1310SAdrian Hunter 
895c83fa7f2SAdrian Hunter /**
8969521b5f2SJiri Olsa  * evlist__mmap_ex - Create mmaps to receive events.
897c83fa7f2SAdrian Hunter  * @evlist: list of events
898c83fa7f2SAdrian Hunter  * @pages: map length in pages
899c83fa7f2SAdrian Hunter  * @overwrite: overwrite older events?
900718c602dSAdrian Hunter  * @auxtrace_pages - auxtrace map length in pages
901718c602dSAdrian Hunter  * @auxtrace_overwrite - overwrite older auxtrace data?
902f8a95309SArnaldo Carvalho de Melo  *
903c83fa7f2SAdrian Hunter  * If @overwrite is %false the user needs to signal event consumption using
9049521b5f2SJiri Olsa  * perf_mmap__write_tail().  Using evlist__mmap_read() does this
905c83fa7f2SAdrian Hunter  * automatically.
906f8a95309SArnaldo Carvalho de Melo  *
907718c602dSAdrian Hunter  * Similarly, if @auxtrace_overwrite is %false the user needs to signal data
908718c602dSAdrian Hunter  * consumption using auxtrace_mmap__write_tail().
909718c602dSAdrian Hunter  *
910c83fa7f2SAdrian Hunter  * Return: %0 on success, negative error code otherwise.
911f8a95309SArnaldo Carvalho de Melo  */
9129521b5f2SJiri Olsa int evlist__mmap_ex(struct evlist *evlist, unsigned int pages,
9137a276ff6SWang Nan 			 unsigned int auxtrace_pages,
91451255a8aSAlexey Budankov 			 bool auxtrace_overwrite, int nr_cblocks, int affinity, int flush,
91551255a8aSAlexey Budankov 			 int comp_level)
916f8a95309SArnaldo Carvalho de Melo {
91732dcd021SJiri Olsa 	struct evsel *evsel;
918f72f901dSJiri Olsa 	const struct perf_cpu_map *cpus = evlist->core.cpus;
91903617c22SJiri Olsa 	const struct perf_thread_map *threads = evlist->core.threads;
92071f566a3SWang Nan 	/*
92171f566a3SWang Nan 	 * Delay setting mp.prot: set it before calling perf_mmap__mmap.
92271f566a3SWang Nan 	 * Its value is decided by evsel's write_backward.
92371f566a3SWang Nan 	 * So &mp should not be passed through const pointer.
92471f566a3SWang Nan 	 */
92551255a8aSAlexey Budankov 	struct mmap_params mp = { .nr_cblocks = nr_cblocks, .affinity = affinity, .flush = flush,
92651255a8aSAlexey Budankov 				  .comp_level = comp_level };
92750a682ceSArnaldo Carvalho de Melo 
9288db6d6b1SWang Nan 	if (!evlist->mmap)
929d50cf361SJiri Olsa 		evlist->mmap = evlist__alloc_mmap(evlist, false);
9308db6d6b1SWang Nan 	if (!evlist->mmap)
931f8a95309SArnaldo Carvalho de Melo 		return -ENOMEM;
932f8a95309SArnaldo Carvalho de Melo 
93331f67fc4SJiri Olsa 	if (evlist->core.pollfd.entries == NULL && perf_evlist__alloc_pollfd(&evlist->core) < 0)
934f8a95309SArnaldo Carvalho de Melo 		return -ENOMEM;
935f8a95309SArnaldo Carvalho de Melo 
936f6fa4375SJiri Olsa 	evlist->core.mmap_len = evlist__mmap_size(pages);
937f6fa4375SJiri Olsa 	pr_debug("mmap size %zuB\n", evlist->core.mmap_len);
938f6fa4375SJiri Olsa 	mp.mask = evlist->core.mmap_len - page_size - 1;
939f8a95309SArnaldo Carvalho de Melo 
940f6fa4375SJiri Olsa 	auxtrace_mmap_params__init(&mp.auxtrace_mp, evlist->core.mmap_len,
941718c602dSAdrian Hunter 				   auxtrace_pages, auxtrace_overwrite);
942718c602dSAdrian Hunter 
943e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, evsel) {
9441fc632ceSJiri Olsa 		if ((evsel->core.attr.read_format & PERF_FORMAT_ID) &&
9458cd36f3eSJiri Olsa 		    evsel->core.sample_id == NULL &&
94670c20369SJiri Olsa 		    perf_evsel__alloc_id(&evsel->core, perf_cpu_map__nr(cpus), threads->nr) < 0)
947f8a95309SArnaldo Carvalho de Melo 			return -ENOMEM;
948f8a95309SArnaldo Carvalho de Melo 	}
949f8a95309SArnaldo Carvalho de Melo 
950315c0a1fSJiri Olsa 	if (perf_cpu_map__empty(cpus))
9519521b5f2SJiri Olsa 		return evlist__mmap_per_thread(evlist, &mp);
952f8a95309SArnaldo Carvalho de Melo 
9539521b5f2SJiri Olsa 	return evlist__mmap_per_cpu(evlist, &mp);
954f8a95309SArnaldo Carvalho de Melo }
9557e2ed097SArnaldo Carvalho de Melo 
9569521b5f2SJiri Olsa int evlist__mmap(struct evlist *evlist, unsigned int pages)
957718c602dSAdrian Hunter {
9589521b5f2SJiri Olsa 	return evlist__mmap_ex(evlist, pages, 0, false, 0, PERF_AFFINITY_SYS, 1, 0);
959718c602dSAdrian Hunter }
960718c602dSAdrian Hunter 
96163503dbaSJiri Olsa int perf_evlist__create_maps(struct evlist *evlist, struct target *target)
9627e2ed097SArnaldo Carvalho de Melo {
963147c508fSJin Yao 	bool all_threads = (target->per_thread && target->system_wide);
964f854839bSJiri Olsa 	struct perf_cpu_map *cpus;
9659749b90eSJiri Olsa 	struct perf_thread_map *threads;
9667e2ed097SArnaldo Carvalho de Melo 
967147c508fSJin Yao 	/*
968147c508fSJin Yao 	 * If specify '-a' and '--per-thread' to perf record, perf record
969147c508fSJin Yao 	 * will override '--per-thread'. target->per_thread = false and
970147c508fSJin Yao 	 * target->system_wide = true.
971147c508fSJin Yao 	 *
972147c508fSJin Yao 	 * If specify '--per-thread' only to perf record,
973147c508fSJin Yao 	 * target->per_thread = true and target->system_wide = false.
974147c508fSJin Yao 	 *
975147c508fSJin Yao 	 * So target->per_thread && target->system_wide is false.
976147c508fSJin Yao 	 * For perf record, thread_map__new_str doesn't call
977147c508fSJin Yao 	 * thread_map__new_all_cpus. That will keep perf record's
978147c508fSJin Yao 	 * current behavior.
979147c508fSJin Yao 	 *
980147c508fSJin Yao 	 * For perf stat, it allows the case that target->per_thread and
981147c508fSJin Yao 	 * target->system_wide are all true. It means to collect system-wide
982147c508fSJin Yao 	 * per-thread data. thread_map__new_str will call
983147c508fSJin Yao 	 * thread_map__new_all_cpus to enumerate all threads.
984147c508fSJin Yao 	 */
98573c0ca1eSJin Yao 	threads = thread_map__new_str(target->pid, target->tid, target->uid,
986147c508fSJin Yao 				      all_threads);
98774bfd2b2SAdrian Hunter 
98874bfd2b2SAdrian Hunter 	if (!threads)
9897e2ed097SArnaldo Carvalho de Melo 		return -1;
9907e2ed097SArnaldo Carvalho de Melo 
9919c105fbcSDongsheng Yang 	if (target__uses_dummy_map(target))
992397721e0SJiri Olsa 		cpus = perf_cpu_map__dummy_new();
993879d77d0SNamhyung Kim 	else
9949c3516d1SJiri Olsa 		cpus = perf_cpu_map__new(target->cpu_list);
9957e2ed097SArnaldo Carvalho de Melo 
99674bfd2b2SAdrian Hunter 	if (!cpus)
9977e2ed097SArnaldo Carvalho de Melo 		goto out_delete_threads;
9987e2ed097SArnaldo Carvalho de Melo 
999ec903f26SJiri Olsa 	evlist->core.has_user_cpus = !!target->cpu_list;
1000ec9a77a7SAdrian Hunter 
1001453fa030SJiri Olsa 	perf_evlist__set_maps(&evlist->core, cpus, threads);
1002d5bc056eSAdrian Hunter 
1003d5bc056eSAdrian Hunter 	return 0;
10047e2ed097SArnaldo Carvalho de Melo 
10057e2ed097SArnaldo Carvalho de Melo out_delete_threads:
10067836e52eSJiri Olsa 	perf_thread_map__put(threads);
10077e2ed097SArnaldo Carvalho de Melo 	return -1;
10087e2ed097SArnaldo Carvalho de Melo }
10097e2ed097SArnaldo Carvalho de Melo 
101063503dbaSJiri Olsa void __perf_evlist__set_sample_bit(struct evlist *evlist,
101122c8a376SArnaldo Carvalho de Melo 				   enum perf_event_sample_format bit)
101222c8a376SArnaldo Carvalho de Melo {
101332dcd021SJiri Olsa 	struct evsel *evsel;
101422c8a376SArnaldo Carvalho de Melo 
1015e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, evsel)
101622c8a376SArnaldo Carvalho de Melo 		__perf_evsel__set_sample_bit(evsel, bit);
101722c8a376SArnaldo Carvalho de Melo }
101822c8a376SArnaldo Carvalho de Melo 
101963503dbaSJiri Olsa void __perf_evlist__reset_sample_bit(struct evlist *evlist,
102022c8a376SArnaldo Carvalho de Melo 				     enum perf_event_sample_format bit)
102122c8a376SArnaldo Carvalho de Melo {
102232dcd021SJiri Olsa 	struct evsel *evsel;
102322c8a376SArnaldo Carvalho de Melo 
1024e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, evsel)
102522c8a376SArnaldo Carvalho de Melo 		__perf_evsel__reset_sample_bit(evsel, bit);
102622c8a376SArnaldo Carvalho de Melo }
102722c8a376SArnaldo Carvalho de Melo 
102863503dbaSJiri Olsa int perf_evlist__apply_filters(struct evlist *evlist, struct evsel **err_evsel)
10290a102479SFrederic Weisbecker {
103032dcd021SJiri Olsa 	struct evsel *evsel;
1031745cefc5SArnaldo Carvalho de Melo 	int err = 0;
10320a102479SFrederic Weisbecker 
1033e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, evsel) {
1034745cefc5SArnaldo Carvalho de Melo 		if (evsel->filter == NULL)
10350a102479SFrederic Weisbecker 			continue;
1036745cefc5SArnaldo Carvalho de Melo 
1037d988d5eeSKan Liang 		/*
1038d988d5eeSKan Liang 		 * filters only work for tracepoint event, which doesn't have cpu limit.
1039d988d5eeSKan Liang 		 * So evlist and evsel should always be same.
1040d988d5eeSKan Liang 		 */
1041a00571fdSJiri Olsa 		err = perf_evsel__apply_filter(&evsel->core, evsel->filter);
104223d4aad4SArnaldo Carvalho de Melo 		if (err) {
104323d4aad4SArnaldo Carvalho de Melo 			*err_evsel = evsel;
1044745cefc5SArnaldo Carvalho de Melo 			break;
10450a102479SFrederic Weisbecker 		}
104623d4aad4SArnaldo Carvalho de Melo 	}
10470a102479SFrederic Weisbecker 
1048745cefc5SArnaldo Carvalho de Melo 	return err;
1049745cefc5SArnaldo Carvalho de Melo }
1050745cefc5SArnaldo Carvalho de Melo 
105163503dbaSJiri Olsa int perf_evlist__set_tp_filter(struct evlist *evlist, const char *filter)
1052745cefc5SArnaldo Carvalho de Melo {
105332dcd021SJiri Olsa 	struct evsel *evsel;
1054745cefc5SArnaldo Carvalho de Melo 	int err = 0;
1055745cefc5SArnaldo Carvalho de Melo 
105605cea449SArnaldo Carvalho de Melo 	if (filter == NULL)
105705cea449SArnaldo Carvalho de Melo 		return -1;
105805cea449SArnaldo Carvalho de Melo 
1059e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, evsel) {
10601fc632ceSJiri Olsa 		if (evsel->core.attr.type != PERF_TYPE_TRACEPOINT)
1061fdf14720SWang Nan 			continue;
1062fdf14720SWang Nan 
106394ad89bcSArnaldo Carvalho de Melo 		err = perf_evsel__set_filter(evsel, filter);
1064745cefc5SArnaldo Carvalho de Melo 		if (err)
1065745cefc5SArnaldo Carvalho de Melo 			break;
1066745cefc5SArnaldo Carvalho de Melo 	}
1067745cefc5SArnaldo Carvalho de Melo 
1068745cefc5SArnaldo Carvalho de Melo 	return err;
10690a102479SFrederic Weisbecker }
107074429964SFrederic Weisbecker 
107153c92f73SArnaldo Carvalho de Melo int perf_evlist__append_tp_filter(struct evlist *evlist, const char *filter)
107253c92f73SArnaldo Carvalho de Melo {
107353c92f73SArnaldo Carvalho de Melo 	struct evsel *evsel;
107453c92f73SArnaldo Carvalho de Melo 	int err = 0;
107553c92f73SArnaldo Carvalho de Melo 
107653c92f73SArnaldo Carvalho de Melo 	if (filter == NULL)
107753c92f73SArnaldo Carvalho de Melo 		return -1;
107853c92f73SArnaldo Carvalho de Melo 
107953c92f73SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, evsel) {
108053c92f73SArnaldo Carvalho de Melo 		if (evsel->core.attr.type != PERF_TYPE_TRACEPOINT)
108153c92f73SArnaldo Carvalho de Melo 			continue;
108253c92f73SArnaldo Carvalho de Melo 
108353c92f73SArnaldo Carvalho de Melo 		err = perf_evsel__append_tp_filter(evsel, filter);
108453c92f73SArnaldo Carvalho de Melo 		if (err)
108553c92f73SArnaldo Carvalho de Melo 			break;
108653c92f73SArnaldo Carvalho de Melo 	}
108753c92f73SArnaldo Carvalho de Melo 
108853c92f73SArnaldo Carvalho de Melo 	return err;
108953c92f73SArnaldo Carvalho de Melo }
109053c92f73SArnaldo Carvalho de Melo 
109105cea449SArnaldo Carvalho de Melo static char *asprintf__tp_filter_pids(size_t npids, pid_t *pids)
1092cfd70a26SArnaldo Carvalho de Melo {
1093cfd70a26SArnaldo Carvalho de Melo 	char *filter;
1094be199adaSArnaldo Carvalho de Melo 	size_t i;
1095cfd70a26SArnaldo Carvalho de Melo 
1096be199adaSArnaldo Carvalho de Melo 	for (i = 0; i < npids; ++i) {
1097be199adaSArnaldo Carvalho de Melo 		if (i == 0) {
1098be199adaSArnaldo Carvalho de Melo 			if (asprintf(&filter, "common_pid != %d", pids[i]) < 0)
109905cea449SArnaldo Carvalho de Melo 				return NULL;
1100be199adaSArnaldo Carvalho de Melo 		} else {
1101be199adaSArnaldo Carvalho de Melo 			char *tmp;
1102be199adaSArnaldo Carvalho de Melo 
1103be199adaSArnaldo Carvalho de Melo 			if (asprintf(&tmp, "%s && common_pid != %d", filter, pids[i]) < 0)
1104be199adaSArnaldo Carvalho de Melo 				goto out_free;
1105be199adaSArnaldo Carvalho de Melo 
1106be199adaSArnaldo Carvalho de Melo 			free(filter);
1107be199adaSArnaldo Carvalho de Melo 			filter = tmp;
1108be199adaSArnaldo Carvalho de Melo 		}
1109be199adaSArnaldo Carvalho de Melo 	}
1110cfd70a26SArnaldo Carvalho de Melo 
111105cea449SArnaldo Carvalho de Melo 	return filter;
1112be199adaSArnaldo Carvalho de Melo out_free:
1113cfd70a26SArnaldo Carvalho de Melo 	free(filter);
111405cea449SArnaldo Carvalho de Melo 	return NULL;
111505cea449SArnaldo Carvalho de Melo }
111605cea449SArnaldo Carvalho de Melo 
111705cea449SArnaldo Carvalho de Melo int perf_evlist__set_tp_filter_pids(struct evlist *evlist, size_t npids, pid_t *pids)
111805cea449SArnaldo Carvalho de Melo {
111905cea449SArnaldo Carvalho de Melo 	char *filter = asprintf__tp_filter_pids(npids, pids);
112005cea449SArnaldo Carvalho de Melo 	int ret = perf_evlist__set_tp_filter(evlist, filter);
112105cea449SArnaldo Carvalho de Melo 
112205cea449SArnaldo Carvalho de Melo 	free(filter);
1123cfd70a26SArnaldo Carvalho de Melo 	return ret;
1124cfd70a26SArnaldo Carvalho de Melo }
1125cfd70a26SArnaldo Carvalho de Melo 
112663503dbaSJiri Olsa int perf_evlist__set_tp_filter_pid(struct evlist *evlist, pid_t pid)
1127be199adaSArnaldo Carvalho de Melo {
11287ad92a33SArnaldo Carvalho de Melo 	return perf_evlist__set_tp_filter_pids(evlist, 1, &pid);
1129be199adaSArnaldo Carvalho de Melo }
1130be199adaSArnaldo Carvalho de Melo 
11311827ab5bSArnaldo Carvalho de Melo int perf_evlist__append_tp_filter_pids(struct evlist *evlist, size_t npids, pid_t *pids)
11321827ab5bSArnaldo Carvalho de Melo {
11331827ab5bSArnaldo Carvalho de Melo 	char *filter = asprintf__tp_filter_pids(npids, pids);
11341827ab5bSArnaldo Carvalho de Melo 	int ret = perf_evlist__append_tp_filter(evlist, filter);
11351827ab5bSArnaldo Carvalho de Melo 
11361827ab5bSArnaldo Carvalho de Melo 	free(filter);
11371827ab5bSArnaldo Carvalho de Melo 	return ret;
11381827ab5bSArnaldo Carvalho de Melo }
11391827ab5bSArnaldo Carvalho de Melo 
11401827ab5bSArnaldo Carvalho de Melo int perf_evlist__append_tp_filter_pid(struct evlist *evlist, pid_t pid)
11411827ab5bSArnaldo Carvalho de Melo {
11421827ab5bSArnaldo Carvalho de Melo 	return perf_evlist__append_tp_filter_pids(evlist, 1, &pid);
11431827ab5bSArnaldo Carvalho de Melo }
11441827ab5bSArnaldo Carvalho de Melo 
114563503dbaSJiri Olsa bool perf_evlist__valid_sample_type(struct evlist *evlist)
114674429964SFrederic Weisbecker {
114732dcd021SJiri Olsa 	struct evsel *pos;
1148c2a70653SArnaldo Carvalho de Melo 
11496484d2f9SJiri Olsa 	if (evlist->core.nr_entries == 1)
115075562573SAdrian Hunter 		return true;
115175562573SAdrian Hunter 
115275562573SAdrian Hunter 	if (evlist->id_pos < 0 || evlist->is_pos < 0)
115375562573SAdrian Hunter 		return false;
115475562573SAdrian Hunter 
1155e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, pos) {
115675562573SAdrian Hunter 		if (pos->id_pos != evlist->id_pos ||
115775562573SAdrian Hunter 		    pos->is_pos != evlist->is_pos)
1158c2a70653SArnaldo Carvalho de Melo 			return false;
115974429964SFrederic Weisbecker 	}
116074429964SFrederic Weisbecker 
1161c2a70653SArnaldo Carvalho de Melo 	return true;
1162c2a70653SArnaldo Carvalho de Melo }
1163c2a70653SArnaldo Carvalho de Melo 
116463503dbaSJiri Olsa u64 __perf_evlist__combined_sample_type(struct evlist *evlist)
1165c2a70653SArnaldo Carvalho de Melo {
116632dcd021SJiri Olsa 	struct evsel *evsel;
116775562573SAdrian Hunter 
116875562573SAdrian Hunter 	if (evlist->combined_sample_type)
116975562573SAdrian Hunter 		return evlist->combined_sample_type;
117075562573SAdrian Hunter 
1171e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, evsel)
11721fc632ceSJiri Olsa 		evlist->combined_sample_type |= evsel->core.attr.sample_type;
117375562573SAdrian Hunter 
117475562573SAdrian Hunter 	return evlist->combined_sample_type;
117575562573SAdrian Hunter }
117675562573SAdrian Hunter 
117763503dbaSJiri Olsa u64 perf_evlist__combined_sample_type(struct evlist *evlist)
117875562573SAdrian Hunter {
117975562573SAdrian Hunter 	evlist->combined_sample_type = 0;
118075562573SAdrian Hunter 	return __perf_evlist__combined_sample_type(evlist);
1181c2a70653SArnaldo Carvalho de Melo }
1182c2a70653SArnaldo Carvalho de Melo 
118363503dbaSJiri Olsa u64 perf_evlist__combined_branch_type(struct evlist *evlist)
118498df858eSAndi Kleen {
118532dcd021SJiri Olsa 	struct evsel *evsel;
118698df858eSAndi Kleen 	u64 branch_type = 0;
118798df858eSAndi Kleen 
1188e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, evsel)
11891fc632ceSJiri Olsa 		branch_type |= evsel->core.attr.branch_sample_type;
119098df858eSAndi Kleen 	return branch_type;
119198df858eSAndi Kleen }
119298df858eSAndi Kleen 
119363503dbaSJiri Olsa bool perf_evlist__valid_read_format(struct evlist *evlist)
11949ede473cSJiri Olsa {
1195515dbe48SJiri Olsa 	struct evsel *first = evlist__first(evlist), *pos = first;
11961fc632ceSJiri Olsa 	u64 read_format = first->core.attr.read_format;
11971fc632ceSJiri Olsa 	u64 sample_type = first->core.attr.sample_type;
11989ede473cSJiri Olsa 
1199e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, pos) {
12001fc632ceSJiri Olsa 		if (read_format != pos->core.attr.read_format)
12019ede473cSJiri Olsa 			return false;
12029ede473cSJiri Olsa 	}
12039ede473cSJiri Olsa 
12049ede473cSJiri Olsa 	/* PERF_SAMPLE_READ imples PERF_FORMAT_ID. */
12059ede473cSJiri Olsa 	if ((sample_type & PERF_SAMPLE_READ) &&
12069ede473cSJiri Olsa 	    !(read_format & PERF_FORMAT_ID)) {
12079ede473cSJiri Olsa 		return false;
12089ede473cSJiri Olsa 	}
12099ede473cSJiri Olsa 
12109ede473cSJiri Olsa 	return true;
12119ede473cSJiri Olsa }
12129ede473cSJiri Olsa 
121363503dbaSJiri Olsa u16 perf_evlist__id_hdr_size(struct evlist *evlist)
121481e36bffSArnaldo Carvalho de Melo {
1215515dbe48SJiri Olsa 	struct evsel *first = evlist__first(evlist);
121681e36bffSArnaldo Carvalho de Melo 	struct perf_sample *data;
121781e36bffSArnaldo Carvalho de Melo 	u64 sample_type;
121881e36bffSArnaldo Carvalho de Melo 	u16 size = 0;
121981e36bffSArnaldo Carvalho de Melo 
12201fc632ceSJiri Olsa 	if (!first->core.attr.sample_id_all)
122181e36bffSArnaldo Carvalho de Melo 		goto out;
122281e36bffSArnaldo Carvalho de Melo 
12231fc632ceSJiri Olsa 	sample_type = first->core.attr.sample_type;
122481e36bffSArnaldo Carvalho de Melo 
122581e36bffSArnaldo Carvalho de Melo 	if (sample_type & PERF_SAMPLE_TID)
122681e36bffSArnaldo Carvalho de Melo 		size += sizeof(data->tid) * 2;
122781e36bffSArnaldo Carvalho de Melo 
122881e36bffSArnaldo Carvalho de Melo        if (sample_type & PERF_SAMPLE_TIME)
122981e36bffSArnaldo Carvalho de Melo 		size += sizeof(data->time);
123081e36bffSArnaldo Carvalho de Melo 
123181e36bffSArnaldo Carvalho de Melo 	if (sample_type & PERF_SAMPLE_ID)
123281e36bffSArnaldo Carvalho de Melo 		size += sizeof(data->id);
123381e36bffSArnaldo Carvalho de Melo 
123481e36bffSArnaldo Carvalho de Melo 	if (sample_type & PERF_SAMPLE_STREAM_ID)
123581e36bffSArnaldo Carvalho de Melo 		size += sizeof(data->stream_id);
123681e36bffSArnaldo Carvalho de Melo 
123781e36bffSArnaldo Carvalho de Melo 	if (sample_type & PERF_SAMPLE_CPU)
123881e36bffSArnaldo Carvalho de Melo 		size += sizeof(data->cpu) * 2;
123975562573SAdrian Hunter 
124075562573SAdrian Hunter 	if (sample_type & PERF_SAMPLE_IDENTIFIER)
124175562573SAdrian Hunter 		size += sizeof(data->id);
124281e36bffSArnaldo Carvalho de Melo out:
124381e36bffSArnaldo Carvalho de Melo 	return size;
124481e36bffSArnaldo Carvalho de Melo }
124581e36bffSArnaldo Carvalho de Melo 
124663503dbaSJiri Olsa bool perf_evlist__valid_sample_id_all(struct evlist *evlist)
1247c2a70653SArnaldo Carvalho de Melo {
1248515dbe48SJiri Olsa 	struct evsel *first = evlist__first(evlist), *pos = first;
1249c2a70653SArnaldo Carvalho de Melo 
1250e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry_continue(evlist, pos) {
12511fc632ceSJiri Olsa 		if (first->core.attr.sample_id_all != pos->core.attr.sample_id_all)
1252c2a70653SArnaldo Carvalho de Melo 			return false;
1253c2a70653SArnaldo Carvalho de Melo 	}
1254c2a70653SArnaldo Carvalho de Melo 
1255c2a70653SArnaldo Carvalho de Melo 	return true;
125674429964SFrederic Weisbecker }
125774429964SFrederic Weisbecker 
125863503dbaSJiri Olsa bool perf_evlist__sample_id_all(struct evlist *evlist)
125974429964SFrederic Weisbecker {
1260515dbe48SJiri Olsa 	struct evsel *first = evlist__first(evlist);
12611fc632ceSJiri Olsa 	return first->core.attr.sample_id_all;
126274429964SFrederic Weisbecker }
126381cce8deSArnaldo Carvalho de Melo 
126463503dbaSJiri Olsa void perf_evlist__set_selected(struct evlist *evlist,
126532dcd021SJiri Olsa 			       struct evsel *evsel)
126681cce8deSArnaldo Carvalho de Melo {
126781cce8deSArnaldo Carvalho de Melo 	evlist->selected = evsel;
126881cce8deSArnaldo Carvalho de Melo }
1269727ab04eSArnaldo Carvalho de Melo 
1270750b4edeSJiri Olsa void evlist__close(struct evlist *evlist)
1271a74b4b66SNamhyung Kim {
127232dcd021SJiri Olsa 	struct evsel *evsel;
1273a74b4b66SNamhyung Kim 
1274475fb533SAndi Kleen 	evlist__for_each_entry_reverse(evlist, evsel)
127588761fa1SJiri Olsa 		evsel__close(evsel);
1276a74b4b66SNamhyung Kim }
1277a74b4b66SNamhyung Kim 
127863503dbaSJiri Olsa static int perf_evlist__create_syswide_maps(struct evlist *evlist)
12794112eb18SArnaldo Carvalho de Melo {
1280f854839bSJiri Olsa 	struct perf_cpu_map *cpus;
12819749b90eSJiri Olsa 	struct perf_thread_map *threads;
12824112eb18SArnaldo Carvalho de Melo 	int err = -ENOMEM;
12834112eb18SArnaldo Carvalho de Melo 
12844112eb18SArnaldo Carvalho de Melo 	/*
12854112eb18SArnaldo Carvalho de Melo 	 * Try reading /sys/devices/system/cpu/online to get
12864112eb18SArnaldo Carvalho de Melo 	 * an all cpus map.
12874112eb18SArnaldo Carvalho de Melo 	 *
12884112eb18SArnaldo Carvalho de Melo 	 * FIXME: -ENOMEM is the best we can do here, the cpu_map
12894112eb18SArnaldo Carvalho de Melo 	 * code needs an overhaul to properly forward the
12904112eb18SArnaldo Carvalho de Melo 	 * error, and we may not want to do that fallback to a
12914112eb18SArnaldo Carvalho de Melo 	 * default cpu identity map :-\
12924112eb18SArnaldo Carvalho de Melo 	 */
12939c3516d1SJiri Olsa 	cpus = perf_cpu_map__new(NULL);
12948c0498b6SAdrian Hunter 	if (!cpus)
12954112eb18SArnaldo Carvalho de Melo 		goto out;
12964112eb18SArnaldo Carvalho de Melo 
12974b49cce2SJiri Olsa 	threads = perf_thread_map__new_dummy();
12988c0498b6SAdrian Hunter 	if (!threads)
12998c0498b6SAdrian Hunter 		goto out_put;
13004112eb18SArnaldo Carvalho de Melo 
1301453fa030SJiri Olsa 	perf_evlist__set_maps(&evlist->core, cpus, threads);
13024112eb18SArnaldo Carvalho de Melo out:
13034112eb18SArnaldo Carvalho de Melo 	return err;
13048c0498b6SAdrian Hunter out_put:
130538f01d8dSJiri Olsa 	perf_cpu_map__put(cpus);
13064112eb18SArnaldo Carvalho de Melo 	goto out;
13074112eb18SArnaldo Carvalho de Melo }
13084112eb18SArnaldo Carvalho de Melo 
1309474ddc4cSJiri Olsa int evlist__open(struct evlist *evlist)
1310727ab04eSArnaldo Carvalho de Melo {
131132dcd021SJiri Olsa 	struct evsel *evsel;
1312a74b4b66SNamhyung Kim 	int err;
1313727ab04eSArnaldo Carvalho de Melo 
13144112eb18SArnaldo Carvalho de Melo 	/*
13154112eb18SArnaldo Carvalho de Melo 	 * Default: one fd per CPU, all threads, aka systemwide
13164112eb18SArnaldo Carvalho de Melo 	 * as sys_perf_event_open(cpu = -1, thread = -1) is EINVAL
13174112eb18SArnaldo Carvalho de Melo 	 */
131803617c22SJiri Olsa 	if (evlist->core.threads == NULL && evlist->core.cpus == NULL) {
13194112eb18SArnaldo Carvalho de Melo 		err = perf_evlist__create_syswide_maps(evlist);
13204112eb18SArnaldo Carvalho de Melo 		if (err < 0)
13214112eb18SArnaldo Carvalho de Melo 			goto out_err;
13224112eb18SArnaldo Carvalho de Melo 	}
13234112eb18SArnaldo Carvalho de Melo 
1324733cd2feSAdrian Hunter 	perf_evlist__update_id_pos(evlist);
1325733cd2feSAdrian Hunter 
1326e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, evsel) {
1327af663bd0SJiri Olsa 		err = evsel__open(evsel, evsel->core.cpus, evsel->core.threads);
1328727ab04eSArnaldo Carvalho de Melo 		if (err < 0)
1329727ab04eSArnaldo Carvalho de Melo 			goto out_err;
1330727ab04eSArnaldo Carvalho de Melo 	}
1331727ab04eSArnaldo Carvalho de Melo 
1332727ab04eSArnaldo Carvalho de Melo 	return 0;
1333727ab04eSArnaldo Carvalho de Melo out_err:
1334750b4edeSJiri Olsa 	evlist__close(evlist);
133541c21a68SNamhyung Kim 	errno = -err;
1336727ab04eSArnaldo Carvalho de Melo 	return err;
1337727ab04eSArnaldo Carvalho de Melo }
133835b9d88eSArnaldo Carvalho de Melo 
133963503dbaSJiri Olsa int perf_evlist__prepare_workload(struct evlist *evlist, struct target *target,
134055e162eaSNamhyung Kim 				  const char *argv[], bool pipe_output,
1341735f7e0bSArnaldo Carvalho de Melo 				  void (*exec_error)(int signo, siginfo_t *info, void *ucontext))
134235b9d88eSArnaldo Carvalho de Melo {
134335b9d88eSArnaldo Carvalho de Melo 	int child_ready_pipe[2], go_pipe[2];
134435b9d88eSArnaldo Carvalho de Melo 	char bf;
134535b9d88eSArnaldo Carvalho de Melo 
134635b9d88eSArnaldo Carvalho de Melo 	if (pipe(child_ready_pipe) < 0) {
134735b9d88eSArnaldo Carvalho de Melo 		perror("failed to create 'ready' pipe");
134835b9d88eSArnaldo Carvalho de Melo 		return -1;
134935b9d88eSArnaldo Carvalho de Melo 	}
135035b9d88eSArnaldo Carvalho de Melo 
135135b9d88eSArnaldo Carvalho de Melo 	if (pipe(go_pipe) < 0) {
135235b9d88eSArnaldo Carvalho de Melo 		perror("failed to create 'go' pipe");
135335b9d88eSArnaldo Carvalho de Melo 		goto out_close_ready_pipe;
135435b9d88eSArnaldo Carvalho de Melo 	}
135535b9d88eSArnaldo Carvalho de Melo 
135635b9d88eSArnaldo Carvalho de Melo 	evlist->workload.pid = fork();
135735b9d88eSArnaldo Carvalho de Melo 	if (evlist->workload.pid < 0) {
135835b9d88eSArnaldo Carvalho de Melo 		perror("failed to fork");
135935b9d88eSArnaldo Carvalho de Melo 		goto out_close_pipes;
136035b9d88eSArnaldo Carvalho de Melo 	}
136135b9d88eSArnaldo Carvalho de Melo 
136235b9d88eSArnaldo Carvalho de Melo 	if (!evlist->workload.pid) {
13635f1c4225SArnaldo Carvalho de Melo 		int ret;
13645f1c4225SArnaldo Carvalho de Melo 
1365119fa3c9SNamhyung Kim 		if (pipe_output)
136635b9d88eSArnaldo Carvalho de Melo 			dup2(2, 1);
136735b9d88eSArnaldo Carvalho de Melo 
13680817df08SDavid Ahern 		signal(SIGTERM, SIG_DFL);
13690817df08SDavid Ahern 
137035b9d88eSArnaldo Carvalho de Melo 		close(child_ready_pipe[0]);
137135b9d88eSArnaldo Carvalho de Melo 		close(go_pipe[1]);
137235b9d88eSArnaldo Carvalho de Melo 		fcntl(go_pipe[0], F_SETFD, FD_CLOEXEC);
137335b9d88eSArnaldo Carvalho de Melo 
137435b9d88eSArnaldo Carvalho de Melo 		/*
137535b9d88eSArnaldo Carvalho de Melo 		 * Tell the parent we're ready to go
137635b9d88eSArnaldo Carvalho de Melo 		 */
137735b9d88eSArnaldo Carvalho de Melo 		close(child_ready_pipe[1]);
137835b9d88eSArnaldo Carvalho de Melo 
137935b9d88eSArnaldo Carvalho de Melo 		/*
138035b9d88eSArnaldo Carvalho de Melo 		 * Wait until the parent tells us to go.
138135b9d88eSArnaldo Carvalho de Melo 		 */
13825f1c4225SArnaldo Carvalho de Melo 		ret = read(go_pipe[0], &bf, 1);
13835f1c4225SArnaldo Carvalho de Melo 		/*
13845f1c4225SArnaldo Carvalho de Melo 		 * The parent will ask for the execvp() to be performed by
13855f1c4225SArnaldo Carvalho de Melo 		 * writing exactly one byte, in workload.cork_fd, usually via
13865f1c4225SArnaldo Carvalho de Melo 		 * perf_evlist__start_workload().
13875f1c4225SArnaldo Carvalho de Melo 		 *
138820f86fc1SArnaldo Carvalho de Melo 		 * For cancelling the workload without actually running it,
13895f1c4225SArnaldo Carvalho de Melo 		 * the parent will just close workload.cork_fd, without writing
13905f1c4225SArnaldo Carvalho de Melo 		 * anything, i.e. read will return zero and we just exit()
13915f1c4225SArnaldo Carvalho de Melo 		 * here.
13925f1c4225SArnaldo Carvalho de Melo 		 */
13935f1c4225SArnaldo Carvalho de Melo 		if (ret != 1) {
13945f1c4225SArnaldo Carvalho de Melo 			if (ret == -1)
139535b9d88eSArnaldo Carvalho de Melo 				perror("unable to read pipe");
13965f1c4225SArnaldo Carvalho de Melo 			exit(ret);
13975f1c4225SArnaldo Carvalho de Melo 		}
139835b9d88eSArnaldo Carvalho de Melo 
139935b9d88eSArnaldo Carvalho de Melo 		execvp(argv[0], (char **)argv);
140035b9d88eSArnaldo Carvalho de Melo 
1401735f7e0bSArnaldo Carvalho de Melo 		if (exec_error) {
1402f33cbe72SArnaldo Carvalho de Melo 			union sigval val;
1403f33cbe72SArnaldo Carvalho de Melo 
1404f33cbe72SArnaldo Carvalho de Melo 			val.sival_int = errno;
1405f33cbe72SArnaldo Carvalho de Melo 			if (sigqueue(getppid(), SIGUSR1, val))
140635b9d88eSArnaldo Carvalho de Melo 				perror(argv[0]);
1407f33cbe72SArnaldo Carvalho de Melo 		} else
1408f33cbe72SArnaldo Carvalho de Melo 			perror(argv[0]);
140935b9d88eSArnaldo Carvalho de Melo 		exit(-1);
141035b9d88eSArnaldo Carvalho de Melo 	}
141135b9d88eSArnaldo Carvalho de Melo 
1412735f7e0bSArnaldo Carvalho de Melo 	if (exec_error) {
1413735f7e0bSArnaldo Carvalho de Melo 		struct sigaction act = {
1414735f7e0bSArnaldo Carvalho de Melo 			.sa_flags     = SA_SIGINFO,
1415735f7e0bSArnaldo Carvalho de Melo 			.sa_sigaction = exec_error,
1416735f7e0bSArnaldo Carvalho de Melo 		};
1417735f7e0bSArnaldo Carvalho de Melo 		sigaction(SIGUSR1, &act, NULL);
1418735f7e0bSArnaldo Carvalho de Melo 	}
1419735f7e0bSArnaldo Carvalho de Melo 
14201aaf63b1SArnaldo Carvalho de Melo 	if (target__none(target)) {
142103617c22SJiri Olsa 		if (evlist->core.threads == NULL) {
14221aaf63b1SArnaldo Carvalho de Melo 			fprintf(stderr, "FATAL: evlist->threads need to be set at this point (%s:%d).\n",
14231aaf63b1SArnaldo Carvalho de Melo 				__func__, __LINE__);
14241aaf63b1SArnaldo Carvalho de Melo 			goto out_close_pipes;
14251aaf63b1SArnaldo Carvalho de Melo 		}
142603617c22SJiri Olsa 		perf_thread_map__set_pid(evlist->core.threads, 0, evlist->workload.pid);
14271aaf63b1SArnaldo Carvalho de Melo 	}
142835b9d88eSArnaldo Carvalho de Melo 
142935b9d88eSArnaldo Carvalho de Melo 	close(child_ready_pipe[1]);
143035b9d88eSArnaldo Carvalho de Melo 	close(go_pipe[0]);
143135b9d88eSArnaldo Carvalho de Melo 	/*
143235b9d88eSArnaldo Carvalho de Melo 	 * wait for child to settle
143335b9d88eSArnaldo Carvalho de Melo 	 */
143435b9d88eSArnaldo Carvalho de Melo 	if (read(child_ready_pipe[0], &bf, 1) == -1) {
143535b9d88eSArnaldo Carvalho de Melo 		perror("unable to read pipe");
143635b9d88eSArnaldo Carvalho de Melo 		goto out_close_pipes;
143735b9d88eSArnaldo Carvalho de Melo 	}
143835b9d88eSArnaldo Carvalho de Melo 
1439bcf3145fSNamhyung Kim 	fcntl(go_pipe[1], F_SETFD, FD_CLOEXEC);
144035b9d88eSArnaldo Carvalho de Melo 	evlist->workload.cork_fd = go_pipe[1];
144135b9d88eSArnaldo Carvalho de Melo 	close(child_ready_pipe[0]);
144235b9d88eSArnaldo Carvalho de Melo 	return 0;
144335b9d88eSArnaldo Carvalho de Melo 
144435b9d88eSArnaldo Carvalho de Melo out_close_pipes:
144535b9d88eSArnaldo Carvalho de Melo 	close(go_pipe[0]);
144635b9d88eSArnaldo Carvalho de Melo 	close(go_pipe[1]);
144735b9d88eSArnaldo Carvalho de Melo out_close_ready_pipe:
144835b9d88eSArnaldo Carvalho de Melo 	close(child_ready_pipe[0]);
144935b9d88eSArnaldo Carvalho de Melo 	close(child_ready_pipe[1]);
145035b9d88eSArnaldo Carvalho de Melo 	return -1;
145135b9d88eSArnaldo Carvalho de Melo }
145235b9d88eSArnaldo Carvalho de Melo 
145363503dbaSJiri Olsa int perf_evlist__start_workload(struct evlist *evlist)
145435b9d88eSArnaldo Carvalho de Melo {
145535b9d88eSArnaldo Carvalho de Melo 	if (evlist->workload.cork_fd > 0) {
1456b3824404SDavid Ahern 		char bf = 0;
1457bcf3145fSNamhyung Kim 		int ret;
145835b9d88eSArnaldo Carvalho de Melo 		/*
145935b9d88eSArnaldo Carvalho de Melo 		 * Remove the cork, let it rip!
146035b9d88eSArnaldo Carvalho de Melo 		 */
1461bcf3145fSNamhyung Kim 		ret = write(evlist->workload.cork_fd, &bf, 1);
1462bcf3145fSNamhyung Kim 		if (ret < 0)
1463e978be9eSSoramichi Akiyama 			perror("unable to write to pipe");
1464bcf3145fSNamhyung Kim 
1465bcf3145fSNamhyung Kim 		close(evlist->workload.cork_fd);
1466bcf3145fSNamhyung Kim 		return ret;
146735b9d88eSArnaldo Carvalho de Melo 	}
146835b9d88eSArnaldo Carvalho de Melo 
146935b9d88eSArnaldo Carvalho de Melo 	return 0;
147035b9d88eSArnaldo Carvalho de Melo }
1471cb0b29e0SArnaldo Carvalho de Melo 
147263503dbaSJiri Olsa int perf_evlist__parse_sample(struct evlist *evlist, union perf_event *event,
14730807d2d8SArnaldo Carvalho de Melo 			      struct perf_sample *sample)
1474cb0b29e0SArnaldo Carvalho de Melo {
147532dcd021SJiri Olsa 	struct evsel *evsel = perf_evlist__event2evsel(evlist, event);
147675562573SAdrian Hunter 
147775562573SAdrian Hunter 	if (!evsel)
147875562573SAdrian Hunter 		return -EFAULT;
14790807d2d8SArnaldo Carvalho de Melo 	return perf_evsel__parse_sample(evsel, event, sample);
1480cb0b29e0SArnaldo Carvalho de Melo }
148178f067b3SArnaldo Carvalho de Melo 
148263503dbaSJiri Olsa int perf_evlist__parse_sample_timestamp(struct evlist *evlist,
148301468120SJiri Olsa 					union perf_event *event,
148401468120SJiri Olsa 					u64 *timestamp)
148501468120SJiri Olsa {
148632dcd021SJiri Olsa 	struct evsel *evsel = perf_evlist__event2evsel(evlist, event);
148701468120SJiri Olsa 
148801468120SJiri Olsa 	if (!evsel)
148901468120SJiri Olsa 		return -EFAULT;
149001468120SJiri Olsa 	return perf_evsel__parse_sample_timestamp(evsel, event, timestamp);
149101468120SJiri Olsa }
149201468120SJiri Olsa 
149363503dbaSJiri Olsa int perf_evlist__strerror_open(struct evlist *evlist,
1494a8f23d8fSArnaldo Carvalho de Melo 			       int err, char *buf, size_t size)
1495a8f23d8fSArnaldo Carvalho de Melo {
1496a8f23d8fSArnaldo Carvalho de Melo 	int printed, value;
1497c8b5f2c9SArnaldo Carvalho de Melo 	char sbuf[STRERR_BUFSIZE], *emsg = str_error_r(err, sbuf, sizeof(sbuf));
1498a8f23d8fSArnaldo Carvalho de Melo 
1499a8f23d8fSArnaldo Carvalho de Melo 	switch (err) {
1500a8f23d8fSArnaldo Carvalho de Melo 	case EACCES:
1501a8f23d8fSArnaldo Carvalho de Melo 	case EPERM:
1502a8f23d8fSArnaldo Carvalho de Melo 		printed = scnprintf(buf, size,
1503a8f23d8fSArnaldo Carvalho de Melo 				    "Error:\t%s.\n"
1504a8f23d8fSArnaldo Carvalho de Melo 				    "Hint:\tCheck /proc/sys/kernel/perf_event_paranoid setting.", emsg);
1505a8f23d8fSArnaldo Carvalho de Melo 
15061a47245dSAdrian Hunter 		value = perf_event_paranoid();
1507a8f23d8fSArnaldo Carvalho de Melo 
1508a8f23d8fSArnaldo Carvalho de Melo 		printed += scnprintf(buf + printed, size - printed, "\nHint:\t");
1509a8f23d8fSArnaldo Carvalho de Melo 
1510a8f23d8fSArnaldo Carvalho de Melo 		if (value >= 2) {
1511a8f23d8fSArnaldo Carvalho de Melo 			printed += scnprintf(buf + printed, size - printed,
1512a8f23d8fSArnaldo Carvalho de Melo 					     "For your workloads it needs to be <= 1\nHint:\t");
1513a8f23d8fSArnaldo Carvalho de Melo 		}
1514a8f23d8fSArnaldo Carvalho de Melo 		printed += scnprintf(buf + printed, size - printed,
15155229e366SArnaldo Carvalho de Melo 				     "For system wide tracing it needs to be set to -1.\n");
1516a8f23d8fSArnaldo Carvalho de Melo 
1517a8f23d8fSArnaldo Carvalho de Melo 		printed += scnprintf(buf + printed, size - printed,
15185229e366SArnaldo Carvalho de Melo 				    "Hint:\tTry: 'sudo sh -c \"echo -1 > /proc/sys/kernel/perf_event_paranoid\"'\n"
15195229e366SArnaldo Carvalho de Melo 				    "Hint:\tThe current value is %d.", value);
1520a8f23d8fSArnaldo Carvalho de Melo 		break;
1521d9aade7fSArnaldo Carvalho de Melo 	case EINVAL: {
1522515dbe48SJiri Olsa 		struct evsel *first = evlist__first(evlist);
1523d9aade7fSArnaldo Carvalho de Melo 		int max_freq;
1524d9aade7fSArnaldo Carvalho de Melo 
1525d9aade7fSArnaldo Carvalho de Melo 		if (sysctl__read_int("kernel/perf_event_max_sample_rate", &max_freq) < 0)
1526d9aade7fSArnaldo Carvalho de Melo 			goto out_default;
1527d9aade7fSArnaldo Carvalho de Melo 
15281fc632ceSJiri Olsa 		if (first->core.attr.sample_freq < (u64)max_freq)
1529d9aade7fSArnaldo Carvalho de Melo 			goto out_default;
1530d9aade7fSArnaldo Carvalho de Melo 
1531d9aade7fSArnaldo Carvalho de Melo 		printed = scnprintf(buf, size,
1532d9aade7fSArnaldo Carvalho de Melo 				    "Error:\t%s.\n"
1533d9aade7fSArnaldo Carvalho de Melo 				    "Hint:\tCheck /proc/sys/kernel/perf_event_max_sample_rate.\n"
1534d9aade7fSArnaldo Carvalho de Melo 				    "Hint:\tThe current value is %d and %" PRIu64 " is being requested.",
15351fc632ceSJiri Olsa 				    emsg, max_freq, first->core.attr.sample_freq);
1536d9aade7fSArnaldo Carvalho de Melo 		break;
1537d9aade7fSArnaldo Carvalho de Melo 	}
1538a8f23d8fSArnaldo Carvalho de Melo 	default:
1539d9aade7fSArnaldo Carvalho de Melo out_default:
1540a8f23d8fSArnaldo Carvalho de Melo 		scnprintf(buf, size, "%s", emsg);
1541a8f23d8fSArnaldo Carvalho de Melo 		break;
1542a8f23d8fSArnaldo Carvalho de Melo 	}
1543a8f23d8fSArnaldo Carvalho de Melo 
1544a8f23d8fSArnaldo Carvalho de Melo 	return 0;
1545a8f23d8fSArnaldo Carvalho de Melo }
1546a025e4f0SAdrian Hunter 
154763503dbaSJiri Olsa int perf_evlist__strerror_mmap(struct evlist *evlist, int err, char *buf, size_t size)
1548956fa571SArnaldo Carvalho de Melo {
1549c8b5f2c9SArnaldo Carvalho de Melo 	char sbuf[STRERR_BUFSIZE], *emsg = str_error_r(err, sbuf, sizeof(sbuf));
1550f6fa4375SJiri Olsa 	int pages_attempted = evlist->core.mmap_len / 1024, pages_max_per_user, printed = 0;
1551956fa571SArnaldo Carvalho de Melo 
1552956fa571SArnaldo Carvalho de Melo 	switch (err) {
1553956fa571SArnaldo Carvalho de Melo 	case EPERM:
1554e5d4a290SArnaldo Carvalho de Melo 		sysctl__read_int("kernel/perf_event_mlock_kb", &pages_max_per_user);
1555e965bea1SArnaldo Carvalho de Melo 		printed += scnprintf(buf + printed, size - printed,
1556e965bea1SArnaldo Carvalho de Melo 				     "Error:\t%s.\n"
1557956fa571SArnaldo Carvalho de Melo 				     "Hint:\tCheck /proc/sys/kernel/perf_event_mlock_kb (%d kB) setting.\n"
1558e965bea1SArnaldo Carvalho de Melo 				     "Hint:\tTried using %zd kB.\n",
1559e5d4a290SArnaldo Carvalho de Melo 				     emsg, pages_max_per_user, pages_attempted);
1560e965bea1SArnaldo Carvalho de Melo 
1561e965bea1SArnaldo Carvalho de Melo 		if (pages_attempted >= pages_max_per_user) {
1562e965bea1SArnaldo Carvalho de Melo 			printed += scnprintf(buf + printed, size - printed,
1563e965bea1SArnaldo Carvalho de Melo 					     "Hint:\tTry 'sudo sh -c \"echo %d > /proc/sys/kernel/perf_event_mlock_kb\"', or\n",
1564e965bea1SArnaldo Carvalho de Melo 					     pages_max_per_user + pages_attempted);
1565e965bea1SArnaldo Carvalho de Melo 		}
1566e965bea1SArnaldo Carvalho de Melo 
1567e965bea1SArnaldo Carvalho de Melo 		printed += scnprintf(buf + printed, size - printed,
1568e965bea1SArnaldo Carvalho de Melo 				     "Hint:\tTry using a smaller -m/--mmap-pages value.");
1569956fa571SArnaldo Carvalho de Melo 		break;
1570956fa571SArnaldo Carvalho de Melo 	default:
1571956fa571SArnaldo Carvalho de Melo 		scnprintf(buf, size, "%s", emsg);
1572956fa571SArnaldo Carvalho de Melo 		break;
1573956fa571SArnaldo Carvalho de Melo 	}
1574956fa571SArnaldo Carvalho de Melo 
1575956fa571SArnaldo Carvalho de Melo 	return 0;
1576956fa571SArnaldo Carvalho de Melo }
1577956fa571SArnaldo Carvalho de Melo 
157863503dbaSJiri Olsa void perf_evlist__to_front(struct evlist *evlist,
157932dcd021SJiri Olsa 			   struct evsel *move_evsel)
1580a025e4f0SAdrian Hunter {
158132dcd021SJiri Olsa 	struct evsel *evsel, *n;
1582a025e4f0SAdrian Hunter 	LIST_HEAD(move);
1583a025e4f0SAdrian Hunter 
1584515dbe48SJiri Olsa 	if (move_evsel == evlist__first(evlist))
1585a025e4f0SAdrian Hunter 		return;
1586a025e4f0SAdrian Hunter 
1587e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry_safe(evlist, n, evsel) {
1588a025e4f0SAdrian Hunter 		if (evsel->leader == move_evsel->leader)
1589b27c4eceSJiri Olsa 			list_move_tail(&evsel->core.node, &move);
1590a025e4f0SAdrian Hunter 	}
1591a025e4f0SAdrian Hunter 
1592ce9036a6SJiri Olsa 	list_splice(&move, &evlist->core.entries);
1593a025e4f0SAdrian Hunter }
159460b0896cSAdrian Hunter 
159563503dbaSJiri Olsa void perf_evlist__set_tracking_event(struct evlist *evlist,
159632dcd021SJiri Olsa 				     struct evsel *tracking_evsel)
159760b0896cSAdrian Hunter {
159832dcd021SJiri Olsa 	struct evsel *evsel;
159960b0896cSAdrian Hunter 
160060b0896cSAdrian Hunter 	if (tracking_evsel->tracking)
160160b0896cSAdrian Hunter 		return;
160260b0896cSAdrian Hunter 
1603e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, evsel) {
160460b0896cSAdrian Hunter 		if (evsel != tracking_evsel)
160560b0896cSAdrian Hunter 			evsel->tracking = false;
160660b0896cSAdrian Hunter 	}
160760b0896cSAdrian Hunter 
160860b0896cSAdrian Hunter 	tracking_evsel->tracking = true;
160960b0896cSAdrian Hunter }
16107630b3e2SWang Nan 
161132dcd021SJiri Olsa struct evsel *
161263503dbaSJiri Olsa perf_evlist__find_evsel_by_str(struct evlist *evlist,
16137630b3e2SWang Nan 			       const char *str)
16147630b3e2SWang Nan {
161532dcd021SJiri Olsa 	struct evsel *evsel;
16167630b3e2SWang Nan 
1617e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, evsel) {
16187630b3e2SWang Nan 		if (!evsel->name)
16197630b3e2SWang Nan 			continue;
16207630b3e2SWang Nan 		if (strcmp(str, evsel->name) == 0)
16217630b3e2SWang Nan 			return evsel;
16227630b3e2SWang Nan 	}
16237630b3e2SWang Nan 
16247630b3e2SWang Nan 	return NULL;
16257630b3e2SWang Nan }
162654cc54deSWang Nan 
162763503dbaSJiri Olsa void perf_evlist__toggle_bkw_mmap(struct evlist *evlist,
162854cc54deSWang Nan 				  enum bkw_mmap_state state)
162954cc54deSWang Nan {
163054cc54deSWang Nan 	enum bkw_mmap_state old_state = evlist->bkw_mmap_state;
163154cc54deSWang Nan 	enum action {
163254cc54deSWang Nan 		NONE,
163354cc54deSWang Nan 		PAUSE,
163454cc54deSWang Nan 		RESUME,
163554cc54deSWang Nan 	} action = NONE;
163654cc54deSWang Nan 
16370b72d69aSWang Nan 	if (!evlist->overwrite_mmap)
163854cc54deSWang Nan 		return;
163954cc54deSWang Nan 
164054cc54deSWang Nan 	switch (old_state) {
164154cc54deSWang Nan 	case BKW_MMAP_NOTREADY: {
164254cc54deSWang Nan 		if (state != BKW_MMAP_RUNNING)
1643dd8bd53aSLuis de Bethencourt 			goto state_err;
164454cc54deSWang Nan 		break;
164554cc54deSWang Nan 	}
164654cc54deSWang Nan 	case BKW_MMAP_RUNNING: {
164754cc54deSWang Nan 		if (state != BKW_MMAP_DATA_PENDING)
164854cc54deSWang Nan 			goto state_err;
164954cc54deSWang Nan 		action = PAUSE;
165054cc54deSWang Nan 		break;
165154cc54deSWang Nan 	}
165254cc54deSWang Nan 	case BKW_MMAP_DATA_PENDING: {
165354cc54deSWang Nan 		if (state != BKW_MMAP_EMPTY)
165454cc54deSWang Nan 			goto state_err;
165554cc54deSWang Nan 		break;
165654cc54deSWang Nan 	}
165754cc54deSWang Nan 	case BKW_MMAP_EMPTY: {
165854cc54deSWang Nan 		if (state != BKW_MMAP_RUNNING)
165954cc54deSWang Nan 			goto state_err;
166054cc54deSWang Nan 		action = RESUME;
166154cc54deSWang Nan 		break;
166254cc54deSWang Nan 	}
166354cc54deSWang Nan 	default:
166454cc54deSWang Nan 		WARN_ONCE(1, "Shouldn't get there\n");
166554cc54deSWang Nan 	}
166654cc54deSWang Nan 
166754cc54deSWang Nan 	evlist->bkw_mmap_state = state;
166854cc54deSWang Nan 
166954cc54deSWang Nan 	switch (action) {
167054cc54deSWang Nan 	case PAUSE:
167154cc54deSWang Nan 		perf_evlist__pause(evlist);
167254cc54deSWang Nan 		break;
167354cc54deSWang Nan 	case RESUME:
167454cc54deSWang Nan 		perf_evlist__resume(evlist);
167554cc54deSWang Nan 		break;
167654cc54deSWang Nan 	case NONE:
167754cc54deSWang Nan 	default:
167854cc54deSWang Nan 		break;
167954cc54deSWang Nan 	}
168054cc54deSWang Nan 
168154cc54deSWang Nan state_err:
168254cc54deSWang Nan 	return;
168354cc54deSWang Nan }
168407d6f446SArnaldo Carvalho de Melo 
168563503dbaSJiri Olsa bool perf_evlist__exclude_kernel(struct evlist *evlist)
168607d6f446SArnaldo Carvalho de Melo {
168732dcd021SJiri Olsa 	struct evsel *evsel;
168807d6f446SArnaldo Carvalho de Melo 
168907d6f446SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, evsel) {
16901fc632ceSJiri Olsa 		if (!evsel->core.attr.exclude_kernel)
169107d6f446SArnaldo Carvalho de Melo 			return false;
169207d6f446SArnaldo Carvalho de Melo 	}
169307d6f446SArnaldo Carvalho de Melo 
169407d6f446SArnaldo Carvalho de Melo 	return true;
169507d6f446SArnaldo Carvalho de Melo }
1696e2bdbe80SJin Yao 
1697e2bdbe80SJin Yao /*
1698e2bdbe80SJin Yao  * Events in data file are not collect in groups, but we still want
1699e2bdbe80SJin Yao  * the group display. Set the artificial group and set the leader's
1700e2bdbe80SJin Yao  * forced_leader flag to notify the display code.
1701e2bdbe80SJin Yao  */
170263503dbaSJiri Olsa void perf_evlist__force_leader(struct evlist *evlist)
1703e2bdbe80SJin Yao {
1704e2bdbe80SJin Yao 	if (!evlist->nr_groups) {
1705515dbe48SJiri Olsa 		struct evsel *leader = evlist__first(evlist);
1706e2bdbe80SJin Yao 
1707e2bdbe80SJin Yao 		perf_evlist__set_leader(evlist);
1708e2bdbe80SJin Yao 		leader->forced_leader = true;
1709e2bdbe80SJin Yao 	}
1710e2bdbe80SJin Yao }
1711c3537fc2SAndi Kleen 
171263503dbaSJiri Olsa struct evsel *perf_evlist__reset_weak_group(struct evlist *evsel_list,
171332dcd021SJiri Olsa 						 struct evsel *evsel)
1714c3537fc2SAndi Kleen {
171532dcd021SJiri Olsa 	struct evsel *c2, *leader;
1716c3537fc2SAndi Kleen 	bool is_open = true;
1717c3537fc2SAndi Kleen 
1718c3537fc2SAndi Kleen 	leader = evsel->leader;
1719c3537fc2SAndi Kleen 	pr_debug("Weak group for %s/%d failed\n",
17205643b1a5SJiri Olsa 			leader->name, leader->core.nr_members);
1721c3537fc2SAndi Kleen 
1722c3537fc2SAndi Kleen 	/*
1723c3537fc2SAndi Kleen 	 * for_each_group_member doesn't work here because it doesn't
1724c3537fc2SAndi Kleen 	 * include the first entry.
1725c3537fc2SAndi Kleen 	 */
1726c3537fc2SAndi Kleen 	evlist__for_each_entry(evsel_list, c2) {
1727c3537fc2SAndi Kleen 		if (c2 == evsel)
1728c3537fc2SAndi Kleen 			is_open = false;
1729c3537fc2SAndi Kleen 		if (c2->leader == leader) {
1730c3537fc2SAndi Kleen 			if (is_open)
17317834fa94SAndi Kleen 				perf_evsel__close(&evsel->core);
1732c3537fc2SAndi Kleen 			c2->leader = c2;
17335643b1a5SJiri Olsa 			c2->core.nr_members = 0;
1734c3537fc2SAndi Kleen 		}
1735c3537fc2SAndi Kleen 	}
1736c3537fc2SAndi Kleen 	return leader;
1737c3537fc2SAndi Kleen }
1738657ee553SSong Liu 
173963503dbaSJiri Olsa int perf_evlist__add_sb_event(struct evlist **evlist,
1740657ee553SSong Liu 			      struct perf_event_attr *attr,
1741657ee553SSong Liu 			      perf_evsel__sb_cb_t cb,
1742657ee553SSong Liu 			      void *data)
1743657ee553SSong Liu {
174432dcd021SJiri Olsa 	struct evsel *evsel;
1745657ee553SSong Liu 	bool new_evlist = (*evlist) == NULL;
1746657ee553SSong Liu 
1747657ee553SSong Liu 	if (*evlist == NULL)
17480f98b11cSJiri Olsa 		*evlist = evlist__new();
1749657ee553SSong Liu 	if (*evlist == NULL)
1750657ee553SSong Liu 		return -1;
1751657ee553SSong Liu 
1752657ee553SSong Liu 	if (!attr->sample_id_all) {
1753657ee553SSong Liu 		pr_warning("enabling sample_id_all for all side band events\n");
1754657ee553SSong Liu 		attr->sample_id_all = 1;
1755657ee553SSong Liu 	}
1756657ee553SSong Liu 
17576484d2f9SJiri Olsa 	evsel = perf_evsel__new_idx(attr, (*evlist)->core.nr_entries);
1758657ee553SSong Liu 	if (!evsel)
1759657ee553SSong Liu 		goto out_err;
1760657ee553SSong Liu 
1761657ee553SSong Liu 	evsel->side_band.cb = cb;
1762657ee553SSong Liu 	evsel->side_band.data = data;
1763a1cf3a75SJiri Olsa 	evlist__add(*evlist, evsel);
1764657ee553SSong Liu 	return 0;
1765657ee553SSong Liu 
1766657ee553SSong Liu out_err:
1767657ee553SSong Liu 	if (new_evlist) {
1768c12995a5SJiri Olsa 		evlist__delete(*evlist);
1769657ee553SSong Liu 		*evlist = NULL;
1770657ee553SSong Liu 	}
1771657ee553SSong Liu 	return -1;
1772657ee553SSong Liu }
1773657ee553SSong Liu 
1774657ee553SSong Liu static void *perf_evlist__poll_thread(void *arg)
1775657ee553SSong Liu {
177663503dbaSJiri Olsa 	struct evlist *evlist = arg;
1777657ee553SSong Liu 	bool draining = false;
1778adc6257cSJiri Olsa 	int i, done = 0;
1779b397f846SArnaldo Carvalho de Melo 	/*
1780b397f846SArnaldo Carvalho de Melo 	 * In order to read symbols from other namespaces perf to needs to call
1781b397f846SArnaldo Carvalho de Melo 	 * setns(2).  This isn't permitted if the struct_fs has multiple users.
1782b397f846SArnaldo Carvalho de Melo 	 * unshare(2) the fs so that we may continue to setns into namespaces
1783b397f846SArnaldo Carvalho de Melo 	 * that we're observing when, for instance, reading the build-ids at
1784b397f846SArnaldo Carvalho de Melo 	 * the end of a 'perf record' session.
1785b397f846SArnaldo Carvalho de Melo 	 */
1786b397f846SArnaldo Carvalho de Melo 	unshare(CLONE_FS);
1787657ee553SSong Liu 
1788adc6257cSJiri Olsa 	while (!done) {
1789adc6257cSJiri Olsa 		bool got_data = false;
1790adc6257cSJiri Olsa 
1791adc6257cSJiri Olsa 		if (evlist->thread.done)
1792657ee553SSong Liu 			draining = true;
1793657ee553SSong Liu 
1794657ee553SSong Liu 		if (!draining)
179580ab2987SJiri Olsa 			evlist__poll(evlist, 1000);
1796657ee553SSong Liu 
1797c976ee11SJiri Olsa 		for (i = 0; i < evlist->core.nr_mmaps; i++) {
1798a5830532SJiri Olsa 			struct mmap *map = &evlist->mmap[i];
1799657ee553SSong Liu 			union perf_event *event;
1800657ee553SSong Liu 
1801657ee553SSong Liu 			if (perf_mmap__read_init(map))
1802657ee553SSong Liu 				continue;
1803657ee553SSong Liu 			while ((event = perf_mmap__read_event(map)) != NULL) {
180432dcd021SJiri Olsa 				struct evsel *evsel = perf_evlist__event2evsel(evlist, event);
1805657ee553SSong Liu 
1806657ee553SSong Liu 				if (evsel && evsel->side_band.cb)
1807657ee553SSong Liu 					evsel->side_band.cb(event, evsel->side_band.data);
1808657ee553SSong Liu 				else
1809657ee553SSong Liu 					pr_warning("cannot locate proper evsel for the side band event\n");
1810657ee553SSong Liu 
1811657ee553SSong Liu 				perf_mmap__consume(map);
1812adc6257cSJiri Olsa 				got_data = true;
1813657ee553SSong Liu 			}
1814657ee553SSong Liu 			perf_mmap__read_done(map);
1815657ee553SSong Liu 		}
1816adc6257cSJiri Olsa 
1817adc6257cSJiri Olsa 		if (draining && !got_data)
1818adc6257cSJiri Olsa 			break;
1819657ee553SSong Liu 	}
1820657ee553SSong Liu 	return NULL;
1821657ee553SSong Liu }
1822657ee553SSong Liu 
182363503dbaSJiri Olsa int perf_evlist__start_sb_thread(struct evlist *evlist,
1824657ee553SSong Liu 				 struct target *target)
1825657ee553SSong Liu {
182632dcd021SJiri Olsa 	struct evsel *counter;
1827657ee553SSong Liu 
1828657ee553SSong Liu 	if (!evlist)
1829657ee553SSong Liu 		return 0;
1830657ee553SSong Liu 
1831657ee553SSong Liu 	if (perf_evlist__create_maps(evlist, target))
1832657ee553SSong Liu 		goto out_delete_evlist;
1833657ee553SSong Liu 
1834657ee553SSong Liu 	evlist__for_each_entry(evlist, counter) {
1835f72f901dSJiri Olsa 		if (evsel__open(counter, evlist->core.cpus,
183603617c22SJiri Olsa 				     evlist->core.threads) < 0)
1837657ee553SSong Liu 			goto out_delete_evlist;
1838657ee553SSong Liu 	}
1839657ee553SSong Liu 
18409521b5f2SJiri Olsa 	if (evlist__mmap(evlist, UINT_MAX))
1841657ee553SSong Liu 		goto out_delete_evlist;
1842657ee553SSong Liu 
1843657ee553SSong Liu 	evlist__for_each_entry(evlist, counter) {
1844ec7f24efSJiri Olsa 		if (evsel__enable(counter))
1845657ee553SSong Liu 			goto out_delete_evlist;
1846657ee553SSong Liu 	}
1847657ee553SSong Liu 
1848657ee553SSong Liu 	evlist->thread.done = 0;
1849657ee553SSong Liu 	if (pthread_create(&evlist->thread.th, NULL, perf_evlist__poll_thread, evlist))
1850657ee553SSong Liu 		goto out_delete_evlist;
1851657ee553SSong Liu 
1852657ee553SSong Liu 	return 0;
1853657ee553SSong Liu 
1854657ee553SSong Liu out_delete_evlist:
1855c12995a5SJiri Olsa 	evlist__delete(evlist);
1856657ee553SSong Liu 	evlist = NULL;
1857657ee553SSong Liu 	return -1;
1858657ee553SSong Liu }
1859657ee553SSong Liu 
186063503dbaSJiri Olsa void perf_evlist__stop_sb_thread(struct evlist *evlist)
1861657ee553SSong Liu {
1862657ee553SSong Liu 	if (!evlist)
1863657ee553SSong Liu 		return;
1864657ee553SSong Liu 	evlist->thread.done = 1;
1865657ee553SSong Liu 	pthread_join(evlist->thread.th, NULL);
1866c12995a5SJiri Olsa 	evlist__delete(evlist);
1867657ee553SSong Liu }
1868