xref: /openbmc/linux/tools/perf/util/evlist.c (revision 93dd6e28)
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>
457728fa0cSJiri Olsa #include <perf/mmap.h>
4670db7533SArnaldo Carvalho de Melo 
47e14e5497SArnaldo Carvalho de Melo #include <internal/xyarray.h>
48e14e5497SArnaldo Carvalho de Melo 
49748fe088SArnaldo Carvalho de Melo #ifdef LACKS_SIGQUEUE_PROTOTYPE
50748fe088SArnaldo Carvalho de Melo int sigqueue(pid_t pid, int sig, const union sigval value);
51748fe088SArnaldo Carvalho de Melo #endif
52748fe088SArnaldo Carvalho de Melo 
539dfcb759SJiri Olsa #define FD(e, x, y) (*(int *)xyarray__entry(e->core.fd, x, y))
548cd36f3eSJiri Olsa #define SID(e, x, y) xyarray__entry(e->core.sample_id, x, y)
55f8a95309SArnaldo Carvalho de Melo 
5652c86bcaSJiri Olsa void evlist__init(struct evlist *evlist, struct perf_cpu_map *cpus,
579749b90eSJiri Olsa 		  struct perf_thread_map *threads)
58361c99a6SArnaldo Carvalho de Melo {
594562a739SJiri Olsa 	perf_evlist__init(&evlist->core);
60453fa030SJiri Olsa 	perf_evlist__set_maps(&evlist->core, cpus, threads);
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);
14193dd6e28SJiri Olsa 	perf_evlist__exit(&evlist->core);
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);
151e6b1878dSJiri Olsa 	evlist__purge(evlist);
152470579b0SJiri Olsa 	evlist__exit(evlist);
153361c99a6SArnaldo Carvalho de Melo 	free(evlist);
154361c99a6SArnaldo Carvalho de Melo }
155361c99a6SArnaldo Carvalho de Melo 
156a1cf3a75SJiri Olsa void evlist__add(struct evlist *evlist, struct evsel *entry)
157361c99a6SArnaldo Carvalho de Melo {
158d49e4695SArnaldo Carvalho de Melo 	entry->evlist = evlist;
1596484d2f9SJiri Olsa 	entry->idx = evlist->core.nr_entries;
16060b0896cSAdrian Hunter 	entry->tracking = !entry->idx;
161ef503831SArnaldo Carvalho de Melo 
1626484d2f9SJiri Olsa 	perf_evlist__add(&evlist->core, &entry->core);
1636484d2f9SJiri Olsa 
1646484d2f9SJiri Olsa 	if (evlist->core.nr_entries == 1)
16575562573SAdrian Hunter 		perf_evlist__set_id_pos(evlist);
166361c99a6SArnaldo Carvalho de Melo }
167361c99a6SArnaldo Carvalho de Melo 
16816251027SJiri Olsa void evlist__remove(struct evlist *evlist, struct evsel *evsel)
1694768230aSAdrian Hunter {
1704768230aSAdrian Hunter 	evsel->evlist = NULL;
17152e22fb8SJiri Olsa 	perf_evlist__remove(&evlist->core, &evsel->core);
1724768230aSAdrian Hunter }
1734768230aSAdrian Hunter 
17463503dbaSJiri Olsa void perf_evlist__splice_list_tail(struct evlist *evlist,
175f114d6efSAdrian Hunter 				   struct list_head *list)
17650d08e47SArnaldo Carvalho de Melo {
17732dcd021SJiri Olsa 	struct evsel *evsel, *temp;
17875562573SAdrian Hunter 
179e5cadb93SArnaldo Carvalho de Melo 	__evlist__for_each_entry_safe(list, temp, evsel) {
180b27c4eceSJiri Olsa 		list_del_init(&evsel->core.node);
181a1cf3a75SJiri Olsa 		evlist__add(evlist, evsel);
182f114d6efSAdrian Hunter 	}
18350d08e47SArnaldo Carvalho de Melo }
18450d08e47SArnaldo Carvalho de Melo 
185c0e53476SArnaldo Carvalho de Melo int __evlist__set_tracepoints_handlers(struct evlist *evlist,
186c0e53476SArnaldo Carvalho de Melo 				       const struct evsel_str_handler *assocs, size_t nr_assocs)
187c0e53476SArnaldo Carvalho de Melo {
188c0e53476SArnaldo Carvalho de Melo 	struct evsel *evsel;
189c0e53476SArnaldo Carvalho de Melo 	size_t i;
190c0e53476SArnaldo Carvalho de Melo 	int err;
191c0e53476SArnaldo Carvalho de Melo 
192c0e53476SArnaldo Carvalho de Melo 	for (i = 0; i < nr_assocs; i++) {
193c0e53476SArnaldo Carvalho de Melo 		// Adding a handler for an event not in this evlist, just ignore it.
194c0e53476SArnaldo Carvalho de Melo 		evsel = perf_evlist__find_tracepoint_by_name(evlist, assocs[i].name);
195c0e53476SArnaldo Carvalho de Melo 		if (evsel == NULL)
196c0e53476SArnaldo Carvalho de Melo 			continue;
197c0e53476SArnaldo Carvalho de Melo 
198c0e53476SArnaldo Carvalho de Melo 		err = -EEXIST;
199c0e53476SArnaldo Carvalho de Melo 		if (evsel->handler != NULL)
200c0e53476SArnaldo Carvalho de Melo 			goto out;
201c0e53476SArnaldo Carvalho de Melo 		evsel->handler = assocs[i].handler;
202c0e53476SArnaldo Carvalho de Melo 	}
203c0e53476SArnaldo Carvalho de Melo 
204c0e53476SArnaldo Carvalho de Melo 	err = 0;
205c0e53476SArnaldo Carvalho de Melo out:
206c0e53476SArnaldo Carvalho de Melo 	return err;
207c0e53476SArnaldo Carvalho de Melo }
208c0e53476SArnaldo Carvalho de Melo 
20963dab225SArnaldo Carvalho de Melo void __perf_evlist__set_leader(struct list_head *list)
21063dab225SArnaldo Carvalho de Melo {
21132dcd021SJiri Olsa 	struct evsel *evsel, *leader;
21263dab225SArnaldo Carvalho de Melo 
213b27c4eceSJiri Olsa 	leader = list_entry(list->next, struct evsel, core.node);
214b27c4eceSJiri Olsa 	evsel = list_entry(list->prev, struct evsel, core.node);
21597f63e4aSNamhyung Kim 
2165643b1a5SJiri Olsa 	leader->core.nr_members = evsel->idx - leader->idx + 1;
21763dab225SArnaldo Carvalho de Melo 
218e5cadb93SArnaldo Carvalho de Melo 	__evlist__for_each_entry(list, evsel) {
21963dab225SArnaldo Carvalho de Melo 		evsel->leader = leader;
22063dab225SArnaldo Carvalho de Melo 	}
22163dab225SArnaldo Carvalho de Melo }
22263dab225SArnaldo Carvalho de Melo 
22363503dbaSJiri Olsa void perf_evlist__set_leader(struct evlist *evlist)
2246a4bb04cSJiri Olsa {
2256484d2f9SJiri Olsa 	if (evlist->core.nr_entries) {
2266484d2f9SJiri Olsa 		evlist->nr_groups = evlist->core.nr_entries > 1 ? 1 : 0;
227ce9036a6SJiri Olsa 		__perf_evlist__set_leader(&evlist->core.entries);
2286a4bb04cSJiri Olsa 	}
22997f63e4aSNamhyung Kim }
2306a4bb04cSJiri Olsa 
23163503dbaSJiri Olsa int __perf_evlist__add_default(struct evlist *evlist, bool precise)
232361c99a6SArnaldo Carvalho de Melo {
23332dcd021SJiri Olsa 	struct evsel *evsel = perf_evsel__new_cycles(precise);
234361c99a6SArnaldo Carvalho de Melo 
235361c99a6SArnaldo Carvalho de Melo 	if (evsel == NULL)
2367c48dcfdSArnaldo Carvalho de Melo 		return -ENOMEM;
237361c99a6SArnaldo Carvalho de Melo 
238a1cf3a75SJiri Olsa 	evlist__add(evlist, evsel);
239361c99a6SArnaldo Carvalho de Melo 	return 0;
240361c99a6SArnaldo Carvalho de Melo }
2415c581041SArnaldo Carvalho de Melo 
24263503dbaSJiri Olsa int perf_evlist__add_dummy(struct evlist *evlist)
2435bae0250SArnaldo Carvalho de Melo {
2445bae0250SArnaldo Carvalho de Melo 	struct perf_event_attr attr = {
2455bae0250SArnaldo Carvalho de Melo 		.type	= PERF_TYPE_SOFTWARE,
2465bae0250SArnaldo Carvalho de Melo 		.config = PERF_COUNT_SW_DUMMY,
2475bae0250SArnaldo Carvalho de Melo 		.size	= sizeof(attr), /* to capture ABI version */
2485bae0250SArnaldo Carvalho de Melo 	};
2496484d2f9SJiri Olsa 	struct evsel *evsel = perf_evsel__new_idx(&attr, evlist->core.nr_entries);
2505bae0250SArnaldo Carvalho de Melo 
2515bae0250SArnaldo Carvalho de Melo 	if (evsel == NULL)
2525bae0250SArnaldo Carvalho de Melo 		return -ENOMEM;
2535bae0250SArnaldo Carvalho de Melo 
254a1cf3a75SJiri Olsa 	evlist__add(evlist, evsel);
2555bae0250SArnaldo Carvalho de Melo 	return 0;
2565bae0250SArnaldo Carvalho de Melo }
2575bae0250SArnaldo Carvalho de Melo 
258a1cf3a75SJiri Olsa static int evlist__add_attrs(struct evlist *evlist,
25950d08e47SArnaldo Carvalho de Melo 				  struct perf_event_attr *attrs, size_t nr_attrs)
26050d08e47SArnaldo Carvalho de Melo {
26132dcd021SJiri Olsa 	struct evsel *evsel, *n;
26250d08e47SArnaldo Carvalho de Melo 	LIST_HEAD(head);
26350d08e47SArnaldo Carvalho de Melo 	size_t i;
26450d08e47SArnaldo Carvalho de Melo 
26550d08e47SArnaldo Carvalho de Melo 	for (i = 0; i < nr_attrs; i++) {
2666484d2f9SJiri Olsa 		evsel = perf_evsel__new_idx(attrs + i, evlist->core.nr_entries + i);
26750d08e47SArnaldo Carvalho de Melo 		if (evsel == NULL)
26850d08e47SArnaldo Carvalho de Melo 			goto out_delete_partial_list;
269b27c4eceSJiri Olsa 		list_add_tail(&evsel->core.node, &head);
27050d08e47SArnaldo Carvalho de Melo 	}
27150d08e47SArnaldo Carvalho de Melo 
272f114d6efSAdrian Hunter 	perf_evlist__splice_list_tail(evlist, &head);
27350d08e47SArnaldo Carvalho de Melo 
27450d08e47SArnaldo Carvalho de Melo 	return 0;
27550d08e47SArnaldo Carvalho de Melo 
27650d08e47SArnaldo Carvalho de Melo out_delete_partial_list:
277e5cadb93SArnaldo Carvalho de Melo 	__evlist__for_each_entry_safe(&head, n, evsel)
2785eb2dd2aSJiri Olsa 		evsel__delete(evsel);
27950d08e47SArnaldo Carvalho de Melo 	return -1;
28050d08e47SArnaldo Carvalho de Melo }
28150d08e47SArnaldo Carvalho de Melo 
28263503dbaSJiri Olsa int __perf_evlist__add_default_attrs(struct evlist *evlist,
28379695e1bSArnaldo Carvalho de Melo 				     struct perf_event_attr *attrs, size_t nr_attrs)
28479695e1bSArnaldo Carvalho de Melo {
28579695e1bSArnaldo Carvalho de Melo 	size_t i;
28679695e1bSArnaldo Carvalho de Melo 
28779695e1bSArnaldo Carvalho de Melo 	for (i = 0; i < nr_attrs; i++)
28879695e1bSArnaldo Carvalho de Melo 		event_attr_init(attrs + i);
28979695e1bSArnaldo Carvalho de Melo 
290a1cf3a75SJiri Olsa 	return evlist__add_attrs(evlist, attrs, nr_attrs);
29179695e1bSArnaldo Carvalho de Melo }
29279695e1bSArnaldo Carvalho de Melo 
29332dcd021SJiri Olsa struct evsel *
29463503dbaSJiri Olsa perf_evlist__find_tracepoint_by_id(struct evlist *evlist, int id)
295ee29be62SArnaldo Carvalho de Melo {
29632dcd021SJiri Olsa 	struct evsel *evsel;
297ee29be62SArnaldo Carvalho de Melo 
298e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, evsel) {
2991fc632ceSJiri Olsa 		if (evsel->core.attr.type   == PERF_TYPE_TRACEPOINT &&
3001fc632ceSJiri Olsa 		    (int)evsel->core.attr.config == id)
301ee29be62SArnaldo Carvalho de Melo 			return evsel;
302ee29be62SArnaldo Carvalho de Melo 	}
303ee29be62SArnaldo Carvalho de Melo 
304ee29be62SArnaldo Carvalho de Melo 	return NULL;
305ee29be62SArnaldo Carvalho de Melo }
306ee29be62SArnaldo Carvalho de Melo 
30732dcd021SJiri Olsa struct evsel *
30863503dbaSJiri Olsa perf_evlist__find_tracepoint_by_name(struct evlist *evlist,
309a2f2804aSDavid Ahern 				     const char *name)
310a2f2804aSDavid Ahern {
31132dcd021SJiri Olsa 	struct evsel *evsel;
312a2f2804aSDavid Ahern 
313e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, evsel) {
3141fc632ceSJiri Olsa 		if ((evsel->core.attr.type == PERF_TYPE_TRACEPOINT) &&
315a2f2804aSDavid Ahern 		    (strcmp(evsel->name, name) == 0))
316a2f2804aSDavid Ahern 			return evsel;
317a2f2804aSDavid Ahern 	}
318a2f2804aSDavid Ahern 
319a2f2804aSDavid Ahern 	return NULL;
320a2f2804aSDavid Ahern }
321a2f2804aSDavid Ahern 
32263503dbaSJiri Olsa int perf_evlist__add_newtp(struct evlist *evlist,
32339876e7dSArnaldo Carvalho de Melo 			   const char *sys, const char *name, void *handler)
32439876e7dSArnaldo Carvalho de Melo {
32532dcd021SJiri Olsa 	struct evsel *evsel = perf_evsel__newtp(sys, name);
32639876e7dSArnaldo Carvalho de Melo 
3278dd2a131SJiri Olsa 	if (IS_ERR(evsel))
32839876e7dSArnaldo Carvalho de Melo 		return -1;
32939876e7dSArnaldo Carvalho de Melo 
330744a9719SArnaldo Carvalho de Melo 	evsel->handler = handler;
331a1cf3a75SJiri Olsa 	evlist__add(evlist, evsel);
33239876e7dSArnaldo Carvalho de Melo 	return 0;
33339876e7dSArnaldo Carvalho de Melo }
33439876e7dSArnaldo Carvalho de Melo 
33563503dbaSJiri Olsa static int perf_evlist__nr_threads(struct evlist *evlist,
33632dcd021SJiri Olsa 				   struct evsel *evsel)
337bf8e8f4bSAdrian Hunter {
338648b5af3SJiri Olsa 	if (evsel->core.system_wide)
339bf8e8f4bSAdrian Hunter 		return 1;
340bf8e8f4bSAdrian Hunter 	else
341a2f354e3SJiri Olsa 		return perf_thread_map__nr(evlist->core.threads);
342bf8e8f4bSAdrian Hunter }
343bf8e8f4bSAdrian Hunter 
344e74676deSJiri Olsa void evlist__disable(struct evlist *evlist)
3454152ab37SArnaldo Carvalho de Melo {
34632dcd021SJiri Olsa 	struct evsel *pos;
3474152ab37SArnaldo Carvalho de Melo 
348e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, pos) {
3499dfcb759SJiri Olsa 		if (pos->disabled || !perf_evsel__is_group_leader(pos) || !pos->core.fd)
3503fe4430dSJiri Olsa 			continue;
3519a10bb22SJiri Olsa 		evsel__disable(pos);
3524152ab37SArnaldo Carvalho de Melo 	}
3532b56bcfbSArnaldo Carvalho de Melo 
3542b56bcfbSArnaldo Carvalho de Melo 	evlist->enabled = false;
3554152ab37SArnaldo Carvalho de Melo }
3564152ab37SArnaldo Carvalho de Melo 
3571c87f165SJiri Olsa void evlist__enable(struct evlist *evlist)
358764e16a3SDavid Ahern {
35932dcd021SJiri Olsa 	struct evsel *pos;
360764e16a3SDavid Ahern 
361e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, pos) {
3629dfcb759SJiri Olsa 		if (!perf_evsel__is_group_leader(pos) || !pos->core.fd)
3633fe4430dSJiri Olsa 			continue;
364ec7f24efSJiri Olsa 		evsel__enable(pos);
365764e16a3SDavid Ahern 	}
3662b56bcfbSArnaldo Carvalho de Melo 
3672b56bcfbSArnaldo Carvalho de Melo 	evlist->enabled = true;
3682b56bcfbSArnaldo Carvalho de Melo }
3692b56bcfbSArnaldo Carvalho de Melo 
37063503dbaSJiri Olsa void perf_evlist__toggle_enable(struct evlist *evlist)
3712b56bcfbSArnaldo Carvalho de Melo {
372e74676deSJiri Olsa 	(evlist->enabled ? evlist__disable : evlist__enable)(evlist);
373764e16a3SDavid Ahern }
374764e16a3SDavid Ahern 
37563503dbaSJiri Olsa static int perf_evlist__enable_event_cpu(struct evlist *evlist,
37632dcd021SJiri Olsa 					 struct evsel *evsel, int cpu)
3771c65056cSAdrian Hunter {
37818ef15c6SArnaldo Carvalho de Melo 	int thread;
3791c65056cSAdrian Hunter 	int nr_threads = perf_evlist__nr_threads(evlist, evsel);
3801c65056cSAdrian Hunter 
3819dfcb759SJiri Olsa 	if (!evsel->core.fd)
3821c65056cSAdrian Hunter 		return -EINVAL;
3831c65056cSAdrian Hunter 
3841c65056cSAdrian Hunter 	for (thread = 0; thread < nr_threads; thread++) {
38518ef15c6SArnaldo Carvalho de Melo 		int err = ioctl(FD(evsel, cpu, thread), PERF_EVENT_IOC_ENABLE, 0);
3861c65056cSAdrian Hunter 		if (err)
3871c65056cSAdrian Hunter 			return err;
3881c65056cSAdrian Hunter 	}
3891c65056cSAdrian Hunter 	return 0;
3901c65056cSAdrian Hunter }
3911c65056cSAdrian Hunter 
39263503dbaSJiri Olsa static int perf_evlist__enable_event_thread(struct evlist *evlist,
39332dcd021SJiri Olsa 					    struct evsel *evsel,
3941c65056cSAdrian Hunter 					    int thread)
3951c65056cSAdrian Hunter {
39618ef15c6SArnaldo Carvalho de Melo 	int cpu;
3976549cd8fSJiri Olsa 	int nr_cpus = perf_cpu_map__nr(evlist->core.cpus);
3981c65056cSAdrian Hunter 
3999dfcb759SJiri Olsa 	if (!evsel->core.fd)
4001c65056cSAdrian Hunter 		return -EINVAL;
4011c65056cSAdrian Hunter 
4021c65056cSAdrian Hunter 	for (cpu = 0; cpu < nr_cpus; cpu++) {
40318ef15c6SArnaldo Carvalho de Melo 		int err = ioctl(FD(evsel, cpu, thread), PERF_EVENT_IOC_ENABLE, 0);
4041c65056cSAdrian Hunter 		if (err)
4051c65056cSAdrian Hunter 			return err;
4061c65056cSAdrian Hunter 	}
4071c65056cSAdrian Hunter 	return 0;
4081c65056cSAdrian Hunter }
4091c65056cSAdrian Hunter 
41063503dbaSJiri Olsa int perf_evlist__enable_event_idx(struct evlist *evlist,
41132dcd021SJiri Olsa 				  struct evsel *evsel, int idx)
4121c65056cSAdrian Hunter {
413315c0a1fSJiri Olsa 	bool per_cpu_mmaps = !perf_cpu_map__empty(evlist->core.cpus);
4141c65056cSAdrian Hunter 
4151c65056cSAdrian Hunter 	if (per_cpu_mmaps)
4161c65056cSAdrian Hunter 		return perf_evlist__enable_event_cpu(evlist, evsel, idx);
4171c65056cSAdrian Hunter 	else
4181c65056cSAdrian Hunter 		return perf_evlist__enable_event_thread(evlist, evsel, idx);
4191c65056cSAdrian Hunter }
4201c65056cSAdrian Hunter 
421f4009e7bSJiri Olsa int evlist__add_pollfd(struct evlist *evlist, int fd)
422e4b356b5SArnaldo Carvalho de Melo {
423f4009e7bSJiri Olsa 	return perf_evlist__add_pollfd(&evlist->core, fd, NULL, POLLIN);
424e4b356b5SArnaldo Carvalho de Melo }
4251b85337dSArnaldo Carvalho de Melo 
426258e4bfcSWang Nan static void perf_evlist__munmap_filtered(struct fdarray *fda, int fd,
427258e4bfcSWang Nan 					 void *arg __maybe_unused)
428e4b356b5SArnaldo Carvalho de Melo {
429923d0f18SJiri Olsa 	struct perf_mmap *map = fda->priv[fd].ptr;
430e4b356b5SArnaldo Carvalho de Melo 
4314876075bSWang Nan 	if (map)
432923d0f18SJiri Olsa 		perf_mmap__put(map);
43370082dd9SArnaldo Carvalho de Melo }
43470db7533SArnaldo Carvalho de Melo 
435f4009e7bSJiri Olsa int evlist__filter_pollfd(struct evlist *evlist, short revents_and_mask)
4361ddec7f0SArnaldo Carvalho de Melo {
43740cb2d51SJiri Olsa 	return fdarray__filter(&evlist->core.pollfd, revents_and_mask,
438258e4bfcSWang Nan 			       perf_evlist__munmap_filtered, NULL);
4391ddec7f0SArnaldo Carvalho de Melo }
4401ddec7f0SArnaldo Carvalho de Melo 
44180ab2987SJiri Olsa int evlist__poll(struct evlist *evlist, int timeout)
442f66a889dSArnaldo Carvalho de Melo {
44380ab2987SJiri Olsa 	return perf_evlist__poll(&evlist->core, timeout);
444f66a889dSArnaldo Carvalho de Melo }
445f66a889dSArnaldo Carvalho de Melo 
44663503dbaSJiri Olsa struct perf_sample_id *perf_evlist__id2sid(struct evlist *evlist, u64 id)
44770db7533SArnaldo Carvalho de Melo {
44870db7533SArnaldo Carvalho de Melo 	struct hlist_head *head;
44970db7533SArnaldo Carvalho de Melo 	struct perf_sample_id *sid;
45070db7533SArnaldo Carvalho de Melo 	int hash;
45170db7533SArnaldo Carvalho de Melo 
45270db7533SArnaldo Carvalho de Melo 	hash = hash_64(id, PERF_EVLIST__HLIST_BITS);
4531d5af02dSJiri Olsa 	head = &evlist->core.heads[hash];
45470db7533SArnaldo Carvalho de Melo 
455b67bfe0dSSasha Levin 	hlist_for_each_entry(sid, head, node)
45670db7533SArnaldo Carvalho de Melo 		if (sid->id == id)
457932a3594SJiri Olsa 			return sid;
458932a3594SJiri Olsa 
459932a3594SJiri Olsa 	return NULL;
460932a3594SJiri Olsa }
461932a3594SJiri Olsa 
46263503dbaSJiri Olsa struct evsel *perf_evlist__id2evsel(struct evlist *evlist, u64 id)
463932a3594SJiri Olsa {
464932a3594SJiri Olsa 	struct perf_sample_id *sid;
465932a3594SJiri Olsa 
4666484d2f9SJiri Olsa 	if (evlist->core.nr_entries == 1 || !id)
467515dbe48SJiri Olsa 		return evlist__first(evlist);
468932a3594SJiri Olsa 
469932a3594SJiri Olsa 	sid = perf_evlist__id2sid(evlist, id);
470932a3594SJiri Olsa 	if (sid)
47170c20369SJiri Olsa 		return container_of(sid->evsel, struct evsel, core);
47230e68bccSNamhyung Kim 
47330e68bccSNamhyung Kim 	if (!perf_evlist__sample_id_all(evlist))
474515dbe48SJiri Olsa 		return evlist__first(evlist);
47530e68bccSNamhyung Kim 
47670db7533SArnaldo Carvalho de Melo 	return NULL;
47770db7533SArnaldo Carvalho de Melo }
47804391debSArnaldo Carvalho de Melo 
47963503dbaSJiri Olsa struct evsel *perf_evlist__id2evsel_strict(struct evlist *evlist,
480dddcf6abSAdrian Hunter 						u64 id)
481dddcf6abSAdrian Hunter {
482dddcf6abSAdrian Hunter 	struct perf_sample_id *sid;
483dddcf6abSAdrian Hunter 
484dddcf6abSAdrian Hunter 	if (!id)
485dddcf6abSAdrian Hunter 		return NULL;
486dddcf6abSAdrian Hunter 
487dddcf6abSAdrian Hunter 	sid = perf_evlist__id2sid(evlist, id);
488dddcf6abSAdrian Hunter 	if (sid)
48970c20369SJiri Olsa 		return container_of(sid->evsel, struct evsel, core);
490dddcf6abSAdrian Hunter 
491dddcf6abSAdrian Hunter 	return NULL;
492dddcf6abSAdrian Hunter }
493dddcf6abSAdrian Hunter 
49463503dbaSJiri Olsa static int perf_evlist__event2id(struct evlist *evlist,
49575562573SAdrian Hunter 				 union perf_event *event, u64 *id)
49675562573SAdrian Hunter {
497b1fcd190SJiri Olsa 	const __u64 *array = event->sample.array;
49875562573SAdrian Hunter 	ssize_t n;
49975562573SAdrian Hunter 
50075562573SAdrian Hunter 	n = (event->header.size - sizeof(event->header)) >> 3;
50175562573SAdrian Hunter 
50275562573SAdrian Hunter 	if (event->header.type == PERF_RECORD_SAMPLE) {
50375562573SAdrian Hunter 		if (evlist->id_pos >= n)
50475562573SAdrian Hunter 			return -1;
50575562573SAdrian Hunter 		*id = array[evlist->id_pos];
50675562573SAdrian Hunter 	} else {
50775562573SAdrian Hunter 		if (evlist->is_pos > n)
50875562573SAdrian Hunter 			return -1;
50975562573SAdrian Hunter 		n -= evlist->is_pos;
51075562573SAdrian Hunter 		*id = array[n];
51175562573SAdrian Hunter 	}
51275562573SAdrian Hunter 	return 0;
51375562573SAdrian Hunter }
51475562573SAdrian Hunter 
51563503dbaSJiri Olsa struct evsel *perf_evlist__event2evsel(struct evlist *evlist,
51675562573SAdrian Hunter 					    union perf_event *event)
51775562573SAdrian Hunter {
518515dbe48SJiri Olsa 	struct evsel *first = evlist__first(evlist);
51975562573SAdrian Hunter 	struct hlist_head *head;
52075562573SAdrian Hunter 	struct perf_sample_id *sid;
52175562573SAdrian Hunter 	int hash;
52275562573SAdrian Hunter 	u64 id;
52375562573SAdrian Hunter 
5246484d2f9SJiri Olsa 	if (evlist->core.nr_entries == 1)
52598be6966SAdrian Hunter 		return first;
52698be6966SAdrian Hunter 
5271fc632ceSJiri Olsa 	if (!first->core.attr.sample_id_all &&
52898be6966SAdrian Hunter 	    event->header.type != PERF_RECORD_SAMPLE)
52998be6966SAdrian Hunter 		return first;
53075562573SAdrian Hunter 
53175562573SAdrian Hunter 	if (perf_evlist__event2id(evlist, event, &id))
53275562573SAdrian Hunter 		return NULL;
53375562573SAdrian Hunter 
53475562573SAdrian Hunter 	/* Synthesized events have an id of zero */
53575562573SAdrian Hunter 	if (!id)
53698be6966SAdrian Hunter 		return first;
53775562573SAdrian Hunter 
53875562573SAdrian Hunter 	hash = hash_64(id, PERF_EVLIST__HLIST_BITS);
5391d5af02dSJiri Olsa 	head = &evlist->core.heads[hash];
54075562573SAdrian Hunter 
54175562573SAdrian Hunter 	hlist_for_each_entry(sid, head, node) {
54275562573SAdrian Hunter 		if (sid->id == id)
54370c20369SJiri Olsa 			return container_of(sid->evsel, struct evsel, core);
54475562573SAdrian Hunter 	}
54575562573SAdrian Hunter 	return NULL;
54675562573SAdrian Hunter }
54775562573SAdrian Hunter 
54863503dbaSJiri Olsa static int perf_evlist__set_paused(struct evlist *evlist, bool value)
54965aea233SWang Nan {
55065aea233SWang Nan 	int i;
55165aea233SWang Nan 
5520b72d69aSWang Nan 	if (!evlist->overwrite_mmap)
553078c3386SWang Nan 		return 0;
554078c3386SWang Nan 
555c976ee11SJiri Olsa 	for (i = 0; i < evlist->core.nr_mmaps; i++) {
5562cf07b29SJiri Olsa 		int fd = evlist->overwrite_mmap[i].core.fd;
55765aea233SWang Nan 		int err;
55865aea233SWang Nan 
55965aea233SWang Nan 		if (fd < 0)
56065aea233SWang Nan 			continue;
56165aea233SWang Nan 		err = ioctl(fd, PERF_EVENT_IOC_PAUSE_OUTPUT, value ? 1 : 0);
56265aea233SWang Nan 		if (err)
56365aea233SWang Nan 			return err;
56465aea233SWang Nan 	}
56565aea233SWang Nan 	return 0;
56665aea233SWang Nan }
56765aea233SWang Nan 
56863503dbaSJiri Olsa static int perf_evlist__pause(struct evlist *evlist)
56965aea233SWang Nan {
57065aea233SWang Nan 	return perf_evlist__set_paused(evlist, true);
57165aea233SWang Nan }
57265aea233SWang Nan 
57363503dbaSJiri Olsa static int perf_evlist__resume(struct evlist *evlist)
57465aea233SWang Nan {
57565aea233SWang Nan 	return perf_evlist__set_paused(evlist, false);
57665aea233SWang Nan }
57765aea233SWang Nan 
578db6b7b13SJiri Olsa static void evlist__munmap_nofree(struct evlist *evlist)
579f8a95309SArnaldo Carvalho de Melo {
580aece948fSArnaldo Carvalho de Melo 	int i;
581f8a95309SArnaldo Carvalho de Melo 
582b2cb615dSWang Nan 	if (evlist->mmap)
583c976ee11SJiri Olsa 		for (i = 0; i < evlist->core.nr_mmaps; i++)
58480e53d11SJiri Olsa 			perf_mmap__munmap(&evlist->mmap[i].core);
585b2cb615dSWang Nan 
5860b72d69aSWang Nan 	if (evlist->overwrite_mmap)
587c976ee11SJiri Olsa 		for (i = 0; i < evlist->core.nr_mmaps; i++)
58880e53d11SJiri Olsa 			perf_mmap__munmap(&evlist->overwrite_mmap[i].core);
589a1f72618SWang Nan }
590aece948fSArnaldo Carvalho de Melo 
591db6b7b13SJiri Olsa void evlist__munmap(struct evlist *evlist)
592a1f72618SWang Nan {
593db6b7b13SJiri Olsa 	evlist__munmap_nofree(evlist);
59404662523SArnaldo Carvalho de Melo 	zfree(&evlist->mmap);
5950b72d69aSWang Nan 	zfree(&evlist->overwrite_mmap);
596f8a95309SArnaldo Carvalho de Melo }
597f8a95309SArnaldo Carvalho de Melo 
59880e53d11SJiri Olsa static void perf_mmap__unmap_cb(struct perf_mmap *map)
59980e53d11SJiri Olsa {
60080e53d11SJiri Olsa 	struct mmap *m = container_of(map, struct mmap, core);
60180e53d11SJiri Olsa 
60280e53d11SJiri Olsa 	mmap__munmap(m);
60380e53d11SJiri Olsa }
60480e53d11SJiri Olsa 
605d50cf361SJiri Olsa static struct mmap *evlist__alloc_mmap(struct evlist *evlist,
6062c5f6d87SKan Liang 				       bool overwrite)
607f8a95309SArnaldo Carvalho de Melo {
608d4c6fb36SWang Nan 	int i;
609a5830532SJiri Olsa 	struct mmap *map;
610d4c6fb36SWang Nan 
611c976ee11SJiri Olsa 	evlist->core.nr_mmaps = perf_cpu_map__nr(evlist->core.cpus);
612315c0a1fSJiri Olsa 	if (perf_cpu_map__empty(evlist->core.cpus))
613c976ee11SJiri Olsa 		evlist->core.nr_mmaps = perf_thread_map__nr(evlist->core.threads);
614c976ee11SJiri Olsa 	map = zalloc(evlist->core.nr_mmaps * sizeof(struct mmap));
6158db6d6b1SWang Nan 	if (!map)
6168db6d6b1SWang Nan 		return NULL;
617946ae1d4SWang Nan 
618c976ee11SJiri Olsa 	for (i = 0; i < evlist->core.nr_mmaps; i++) {
6194738ca30SArnaldo Carvalho de Melo 		/*
6204738ca30SArnaldo Carvalho de Melo 		 * When the perf_mmap() call is made we grab one refcount, plus
6216afad54dSKan Liang 		 * one extra to let perf_mmap__consume() get the last
6224738ca30SArnaldo Carvalho de Melo 		 * events after all real references (perf_mmap__get()) are
6234738ca30SArnaldo Carvalho de Melo 		 * dropped.
6244738ca30SArnaldo Carvalho de Melo 		 *
6254738ca30SArnaldo Carvalho de Melo 		 * Each PERF_EVENT_IOC_SET_OUTPUT points to this mmap and
6264738ca30SArnaldo Carvalho de Melo 		 * thus does perf_mmap__get() on it.
6274738ca30SArnaldo Carvalho de Melo 		 */
62880e53d11SJiri Olsa 		perf_mmap__init(&map[i].core, overwrite, perf_mmap__unmap_cb);
6294738ca30SArnaldo Carvalho de Melo 	}
630353120b4SJiri Olsa 
6318db6d6b1SWang Nan 	return map;
632f8a95309SArnaldo Carvalho de Melo }
633f8a95309SArnaldo Carvalho de Melo 
6349abd2ab2SJiri Olsa static void
6359abd2ab2SJiri Olsa perf_evlist__mmap_cb_idx(struct perf_evlist *_evlist,
6369abd2ab2SJiri Olsa 			 struct perf_mmap_param *_mp,
6379abd2ab2SJiri Olsa 			 int idx, bool per_cpu)
6389abd2ab2SJiri Olsa {
6399abd2ab2SJiri Olsa 	struct evlist *evlist = container_of(_evlist, struct evlist, core);
6409abd2ab2SJiri Olsa 	struct mmap_params *mp = container_of(_mp, struct mmap_params, core);
6419abd2ab2SJiri Olsa 
6429abd2ab2SJiri Olsa 	auxtrace_mmap_params__set_idx(&mp->auxtrace_mp, evlist, idx, per_cpu);
6439abd2ab2SJiri Olsa }
6449abd2ab2SJiri Olsa 
645bb1b1885SJiri Olsa static struct perf_mmap*
646bb1b1885SJiri Olsa perf_evlist__mmap_cb_get(struct perf_evlist *_evlist, bool overwrite, int idx)
647bb1b1885SJiri Olsa {
648bb1b1885SJiri Olsa 	struct evlist *evlist = container_of(_evlist, struct evlist, core);
649bb1b1885SJiri Olsa 	struct mmap *maps = evlist->mmap;
650bb1b1885SJiri Olsa 
651bb1b1885SJiri Olsa 	if (overwrite) {
652bb1b1885SJiri Olsa 		maps = evlist->overwrite_mmap;
653bb1b1885SJiri Olsa 
654bb1b1885SJiri Olsa 		if (!maps) {
655bb1b1885SJiri Olsa 			maps = evlist__alloc_mmap(evlist, true);
656bb1b1885SJiri Olsa 			if (!maps)
657bb1b1885SJiri Olsa 				return NULL;
658bb1b1885SJiri Olsa 
659bb1b1885SJiri Olsa 			evlist->overwrite_mmap = maps;
660bb1b1885SJiri Olsa 			if (evlist->bkw_mmap_state == BKW_MMAP_NOTREADY)
661bb1b1885SJiri Olsa 				perf_evlist__toggle_bkw_mmap(evlist, BKW_MMAP_RUNNING);
662bb1b1885SJiri Olsa 		}
663bb1b1885SJiri Olsa 	}
664bb1b1885SJiri Olsa 
665bb1b1885SJiri Olsa 	return &maps[idx].core;
666bb1b1885SJiri Olsa }
667bb1b1885SJiri Olsa 
668b80132b1SJiri Olsa static int
669b80132b1SJiri Olsa perf_evlist__mmap_cb_mmap(struct perf_mmap *_map, struct perf_mmap_param *_mp,
670b80132b1SJiri Olsa 			  int output, int cpu)
671b80132b1SJiri Olsa {
672b80132b1SJiri Olsa 	struct mmap *map = container_of(_map, struct mmap, core);
673b80132b1SJiri Olsa 	struct mmap_params *mp = container_of(_mp, struct mmap_params, core);
674b80132b1SJiri Olsa 
675b80132b1SJiri Olsa 	return mmap__mmap(map, mp, output, cpu);
676b80132b1SJiri Olsa }
677b80132b1SJiri Olsa 
678f5e7150cSArnaldo Carvalho de Melo unsigned long perf_event_mlock_kb_in_pages(void)
679994a1f78SJiri Olsa {
680f5e7150cSArnaldo Carvalho de Melo 	unsigned long pages;
6818185e881SArnaldo Carvalho de Melo 	int max;
6828185e881SArnaldo Carvalho de Melo 
6838185e881SArnaldo Carvalho de Melo 	if (sysctl__read_int("kernel/perf_event_mlock_kb", &max) < 0) {
6848185e881SArnaldo Carvalho de Melo 		/*
6858185e881SArnaldo Carvalho de Melo 		 * Pick a once upon a time good value, i.e. things look
6868185e881SArnaldo Carvalho de Melo 		 * strange since we can't read a sysctl value, but lets not
6878185e881SArnaldo Carvalho de Melo 		 * die yet...
6888185e881SArnaldo Carvalho de Melo 		 */
6898185e881SArnaldo Carvalho de Melo 		max = 512;
6908185e881SArnaldo Carvalho de Melo 	} else {
6918185e881SArnaldo Carvalho de Melo 		max -= (page_size / 1024);
6928185e881SArnaldo Carvalho de Melo 	}
6938185e881SArnaldo Carvalho de Melo 
6948185e881SArnaldo Carvalho de Melo 	pages = (max * 1024) / page_size;
6951be300f4SArnaldo Carvalho de Melo 	if (!is_power_of_2(pages))
6961be300f4SArnaldo Carvalho de Melo 		pages = rounddown_pow_of_two(pages);
697f5e7150cSArnaldo Carvalho de Melo 
698f5e7150cSArnaldo Carvalho de Melo 	return pages;
699f5e7150cSArnaldo Carvalho de Melo }
700f5e7150cSArnaldo Carvalho de Melo 
7019521b5f2SJiri Olsa size_t evlist__mmap_size(unsigned long pages)
702f5e7150cSArnaldo Carvalho de Melo {
703f5e7150cSArnaldo Carvalho de Melo 	if (pages == UINT_MAX)
704f5e7150cSArnaldo Carvalho de Melo 		pages = perf_event_mlock_kb_in_pages();
705f5e7150cSArnaldo Carvalho de Melo 	else if (!is_power_of_2(pages))
706994a1f78SJiri Olsa 		return 0;
707994a1f78SJiri Olsa 
708994a1f78SJiri Olsa 	return (pages + 1) * page_size;
709994a1f78SJiri Olsa }
710994a1f78SJiri Olsa 
71133c2dcfdSDavid Ahern static long parse_pages_arg(const char *str, unsigned long min,
71233c2dcfdSDavid Ahern 			    unsigned long max)
713994a1f78SJiri Olsa {
7142fbe4abeSAdrian Hunter 	unsigned long pages, val;
71527050f53SJiri Olsa 	static struct parse_tag tags[] = {
71627050f53SJiri Olsa 		{ .tag  = 'B', .mult = 1       },
71727050f53SJiri Olsa 		{ .tag  = 'K', .mult = 1 << 10 },
71827050f53SJiri Olsa 		{ .tag  = 'M', .mult = 1 << 20 },
71927050f53SJiri Olsa 		{ .tag  = 'G', .mult = 1 << 30 },
72027050f53SJiri Olsa 		{ .tag  = 0 },
72127050f53SJiri Olsa 	};
722994a1f78SJiri Olsa 
7238973504bSDavid Ahern 	if (str == NULL)
72433c2dcfdSDavid Ahern 		return -EINVAL;
7258973504bSDavid Ahern 
72627050f53SJiri Olsa 	val = parse_tag_value(str, tags);
7272fbe4abeSAdrian Hunter 	if (val != (unsigned long) -1) {
72827050f53SJiri Olsa 		/* we got file size value */
72927050f53SJiri Olsa 		pages = PERF_ALIGN(val, page_size) / page_size;
73027050f53SJiri Olsa 	} else {
73127050f53SJiri Olsa 		/* we got pages count value */
73227050f53SJiri Olsa 		char *eptr;
733994a1f78SJiri Olsa 		pages = strtoul(str, &eptr, 10);
73433c2dcfdSDavid Ahern 		if (*eptr != '\0')
73533c2dcfdSDavid Ahern 			return -EINVAL;
73627050f53SJiri Olsa 	}
737994a1f78SJiri Olsa 
7382bcab6c1SAdrian Hunter 	if (pages == 0 && min == 0) {
73933c2dcfdSDavid Ahern 		/* leave number of pages at 0 */
7401dbfa938SAdrian Hunter 	} else if (!is_power_of_2(pages)) {
7419808143bSJiri Olsa 		char buf[100];
7429808143bSJiri Olsa 
74333c2dcfdSDavid Ahern 		/* round pages up to next power of 2 */
74491529834SArnaldo Carvalho de Melo 		pages = roundup_pow_of_two(pages);
7451dbfa938SAdrian Hunter 		if (!pages)
7461dbfa938SAdrian Hunter 			return -EINVAL;
7479808143bSJiri Olsa 
7489808143bSJiri Olsa 		unit_number__scnprintf(buf, sizeof(buf), pages * page_size);
7499808143bSJiri Olsa 		pr_info("rounding mmap pages size to %s (%lu pages)\n",
7509808143bSJiri Olsa 			buf, pages);
7512fbe4abeSAdrian Hunter 	}
7522fbe4abeSAdrian Hunter 
75333c2dcfdSDavid Ahern 	if (pages > max)
75433c2dcfdSDavid Ahern 		return -EINVAL;
75533c2dcfdSDavid Ahern 
75633c2dcfdSDavid Ahern 	return pages;
75733c2dcfdSDavid Ahern }
75833c2dcfdSDavid Ahern 
759e9db1310SAdrian Hunter int __perf_evlist__parse_mmap_pages(unsigned int *mmap_pages, const char *str)
76033c2dcfdSDavid Ahern {
76133c2dcfdSDavid Ahern 	unsigned long max = UINT_MAX;
76233c2dcfdSDavid Ahern 	long pages;
76333c2dcfdSDavid Ahern 
764f5ae9c42SAdrian Hunter 	if (max > SIZE_MAX / page_size)
76533c2dcfdSDavid Ahern 		max = SIZE_MAX / page_size;
76633c2dcfdSDavid Ahern 
76733c2dcfdSDavid Ahern 	pages = parse_pages_arg(str, 1, max);
76833c2dcfdSDavid Ahern 	if (pages < 0) {
76933c2dcfdSDavid Ahern 		pr_err("Invalid argument for --mmap_pages/-m\n");
770994a1f78SJiri Olsa 		return -1;
771994a1f78SJiri Olsa 	}
772994a1f78SJiri Olsa 
773994a1f78SJiri Olsa 	*mmap_pages = pages;
774994a1f78SJiri Olsa 	return 0;
775994a1f78SJiri Olsa }
776994a1f78SJiri Olsa 
777e9db1310SAdrian Hunter int perf_evlist__parse_mmap_pages(const struct option *opt, const char *str,
778e9db1310SAdrian Hunter 				  int unset __maybe_unused)
779e9db1310SAdrian Hunter {
780e9db1310SAdrian Hunter 	return __perf_evlist__parse_mmap_pages(opt->value, str);
781e9db1310SAdrian Hunter }
782e9db1310SAdrian Hunter 
783c83fa7f2SAdrian Hunter /**
7849521b5f2SJiri Olsa  * evlist__mmap_ex - Create mmaps to receive events.
785c83fa7f2SAdrian Hunter  * @evlist: list of events
786c83fa7f2SAdrian Hunter  * @pages: map length in pages
787c83fa7f2SAdrian Hunter  * @overwrite: overwrite older events?
788718c602dSAdrian Hunter  * @auxtrace_pages - auxtrace map length in pages
789718c602dSAdrian Hunter  * @auxtrace_overwrite - overwrite older auxtrace data?
790f8a95309SArnaldo Carvalho de Melo  *
791c83fa7f2SAdrian Hunter  * If @overwrite is %false the user needs to signal event consumption using
7929521b5f2SJiri Olsa  * perf_mmap__write_tail().  Using evlist__mmap_read() does this
793c83fa7f2SAdrian Hunter  * automatically.
794f8a95309SArnaldo Carvalho de Melo  *
795718c602dSAdrian Hunter  * Similarly, if @auxtrace_overwrite is %false the user needs to signal data
796718c602dSAdrian Hunter  * consumption using auxtrace_mmap__write_tail().
797718c602dSAdrian Hunter  *
798c83fa7f2SAdrian Hunter  * Return: %0 on success, negative error code otherwise.
799f8a95309SArnaldo Carvalho de Melo  */
8009521b5f2SJiri Olsa int evlist__mmap_ex(struct evlist *evlist, unsigned int pages,
8017a276ff6SWang Nan 			 unsigned int auxtrace_pages,
80251255a8aSAlexey Budankov 			 bool auxtrace_overwrite, int nr_cblocks, int affinity, int flush,
80351255a8aSAlexey Budankov 			 int comp_level)
804f8a95309SArnaldo Carvalho de Melo {
80571f566a3SWang Nan 	/*
80671f566a3SWang Nan 	 * Delay setting mp.prot: set it before calling perf_mmap__mmap.
80771f566a3SWang Nan 	 * Its value is decided by evsel's write_backward.
80871f566a3SWang Nan 	 * So &mp should not be passed through const pointer.
80971f566a3SWang Nan 	 */
810e440979fSJiri Olsa 	struct mmap_params mp = {
811e440979fSJiri Olsa 		.nr_cblocks	= nr_cblocks,
812e440979fSJiri Olsa 		.affinity	= affinity,
813e440979fSJiri Olsa 		.flush		= flush,
814e440979fSJiri Olsa 		.comp_level	= comp_level
815e440979fSJiri Olsa 	};
816923d0f18SJiri Olsa 	struct perf_evlist_mmap_ops ops = {
8179abd2ab2SJiri Olsa 		.idx  = perf_evlist__mmap_cb_idx,
818bb1b1885SJiri Olsa 		.get  = perf_evlist__mmap_cb_get,
819b80132b1SJiri Olsa 		.mmap = perf_evlist__mmap_cb_mmap,
8209abd2ab2SJiri Olsa 	};
82150a682ceSArnaldo Carvalho de Melo 
8228db6d6b1SWang Nan 	if (!evlist->mmap)
823d50cf361SJiri Olsa 		evlist->mmap = evlist__alloc_mmap(evlist, false);
8248db6d6b1SWang Nan 	if (!evlist->mmap)
825f8a95309SArnaldo Carvalho de Melo 		return -ENOMEM;
826f8a95309SArnaldo Carvalho de Melo 
827f6fa4375SJiri Olsa 	evlist->core.mmap_len = evlist__mmap_size(pages);
828f6fa4375SJiri Olsa 	pr_debug("mmap size %zuB\n", evlist->core.mmap_len);
829e440979fSJiri Olsa 	mp.core.mask = evlist->core.mmap_len - page_size - 1;
830f8a95309SArnaldo Carvalho de Melo 
831f6fa4375SJiri Olsa 	auxtrace_mmap_params__init(&mp.auxtrace_mp, evlist->core.mmap_len,
832718c602dSAdrian Hunter 				   auxtrace_pages, auxtrace_overwrite);
833718c602dSAdrian Hunter 
834923d0f18SJiri Olsa 	return perf_evlist__mmap_ops(&evlist->core, &ops, &mp.core);
835f8a95309SArnaldo Carvalho de Melo }
8367e2ed097SArnaldo Carvalho de Melo 
8379521b5f2SJiri Olsa int evlist__mmap(struct evlist *evlist, unsigned int pages)
838718c602dSAdrian Hunter {
8399521b5f2SJiri Olsa 	return evlist__mmap_ex(evlist, pages, 0, false, 0, PERF_AFFINITY_SYS, 1, 0);
840718c602dSAdrian Hunter }
841718c602dSAdrian Hunter 
84263503dbaSJiri Olsa int perf_evlist__create_maps(struct evlist *evlist, struct target *target)
8437e2ed097SArnaldo Carvalho de Melo {
844147c508fSJin Yao 	bool all_threads = (target->per_thread && target->system_wide);
845f854839bSJiri Olsa 	struct perf_cpu_map *cpus;
8469749b90eSJiri Olsa 	struct perf_thread_map *threads;
8477e2ed097SArnaldo Carvalho de Melo 
848147c508fSJin Yao 	/*
849147c508fSJin Yao 	 * If specify '-a' and '--per-thread' to perf record, perf record
850147c508fSJin Yao 	 * will override '--per-thread'. target->per_thread = false and
851147c508fSJin Yao 	 * target->system_wide = true.
852147c508fSJin Yao 	 *
853147c508fSJin Yao 	 * If specify '--per-thread' only to perf record,
854147c508fSJin Yao 	 * target->per_thread = true and target->system_wide = false.
855147c508fSJin Yao 	 *
856147c508fSJin Yao 	 * So target->per_thread && target->system_wide is false.
857147c508fSJin Yao 	 * For perf record, thread_map__new_str doesn't call
858147c508fSJin Yao 	 * thread_map__new_all_cpus. That will keep perf record's
859147c508fSJin Yao 	 * current behavior.
860147c508fSJin Yao 	 *
861147c508fSJin Yao 	 * For perf stat, it allows the case that target->per_thread and
862147c508fSJin Yao 	 * target->system_wide are all true. It means to collect system-wide
863147c508fSJin Yao 	 * per-thread data. thread_map__new_str will call
864147c508fSJin Yao 	 * thread_map__new_all_cpus to enumerate all threads.
865147c508fSJin Yao 	 */
86673c0ca1eSJin Yao 	threads = thread_map__new_str(target->pid, target->tid, target->uid,
867147c508fSJin Yao 				      all_threads);
86874bfd2b2SAdrian Hunter 
86974bfd2b2SAdrian Hunter 	if (!threads)
8707e2ed097SArnaldo Carvalho de Melo 		return -1;
8717e2ed097SArnaldo Carvalho de Melo 
8729c105fbcSDongsheng Yang 	if (target__uses_dummy_map(target))
873397721e0SJiri Olsa 		cpus = perf_cpu_map__dummy_new();
874879d77d0SNamhyung Kim 	else
8759c3516d1SJiri Olsa 		cpus = perf_cpu_map__new(target->cpu_list);
8767e2ed097SArnaldo Carvalho de Melo 
87774bfd2b2SAdrian Hunter 	if (!cpus)
8787e2ed097SArnaldo Carvalho de Melo 		goto out_delete_threads;
8797e2ed097SArnaldo Carvalho de Melo 
880ec903f26SJiri Olsa 	evlist->core.has_user_cpus = !!target->cpu_list;
881ec9a77a7SAdrian Hunter 
882453fa030SJiri Olsa 	perf_evlist__set_maps(&evlist->core, cpus, threads);
883d5bc056eSAdrian Hunter 
884d5bc056eSAdrian Hunter 	return 0;
8857e2ed097SArnaldo Carvalho de Melo 
8867e2ed097SArnaldo Carvalho de Melo out_delete_threads:
8877836e52eSJiri Olsa 	perf_thread_map__put(threads);
8887e2ed097SArnaldo Carvalho de Melo 	return -1;
8897e2ed097SArnaldo Carvalho de Melo }
8907e2ed097SArnaldo Carvalho de Melo 
89163503dbaSJiri Olsa void __perf_evlist__set_sample_bit(struct evlist *evlist,
89222c8a376SArnaldo Carvalho de Melo 				   enum perf_event_sample_format bit)
89322c8a376SArnaldo Carvalho de Melo {
89432dcd021SJiri Olsa 	struct evsel *evsel;
89522c8a376SArnaldo Carvalho de Melo 
896e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, evsel)
89722c8a376SArnaldo Carvalho de Melo 		__perf_evsel__set_sample_bit(evsel, bit);
89822c8a376SArnaldo Carvalho de Melo }
89922c8a376SArnaldo Carvalho de Melo 
90063503dbaSJiri Olsa void __perf_evlist__reset_sample_bit(struct evlist *evlist,
90122c8a376SArnaldo Carvalho de Melo 				     enum perf_event_sample_format bit)
90222c8a376SArnaldo Carvalho de Melo {
90332dcd021SJiri Olsa 	struct evsel *evsel;
90422c8a376SArnaldo Carvalho de Melo 
905e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, evsel)
90622c8a376SArnaldo Carvalho de Melo 		__perf_evsel__reset_sample_bit(evsel, bit);
90722c8a376SArnaldo Carvalho de Melo }
90822c8a376SArnaldo Carvalho de Melo 
90963503dbaSJiri Olsa int perf_evlist__apply_filters(struct evlist *evlist, struct evsel **err_evsel)
9100a102479SFrederic Weisbecker {
91132dcd021SJiri Olsa 	struct evsel *evsel;
912745cefc5SArnaldo Carvalho de Melo 	int err = 0;
9130a102479SFrederic Weisbecker 
914e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, evsel) {
915745cefc5SArnaldo Carvalho de Melo 		if (evsel->filter == NULL)
9160a102479SFrederic Weisbecker 			continue;
917745cefc5SArnaldo Carvalho de Melo 
918d988d5eeSKan Liang 		/*
919d988d5eeSKan Liang 		 * filters only work for tracepoint event, which doesn't have cpu limit.
920d988d5eeSKan Liang 		 * So evlist and evsel should always be same.
921d988d5eeSKan Liang 		 */
922a00571fdSJiri Olsa 		err = perf_evsel__apply_filter(&evsel->core, evsel->filter);
92323d4aad4SArnaldo Carvalho de Melo 		if (err) {
92423d4aad4SArnaldo Carvalho de Melo 			*err_evsel = evsel;
925745cefc5SArnaldo Carvalho de Melo 			break;
9260a102479SFrederic Weisbecker 		}
92723d4aad4SArnaldo Carvalho de Melo 	}
9280a102479SFrederic Weisbecker 
929745cefc5SArnaldo Carvalho de Melo 	return err;
930745cefc5SArnaldo Carvalho de Melo }
931745cefc5SArnaldo Carvalho de Melo 
93263503dbaSJiri Olsa int perf_evlist__set_tp_filter(struct evlist *evlist, const char *filter)
933745cefc5SArnaldo Carvalho de Melo {
93432dcd021SJiri Olsa 	struct evsel *evsel;
935745cefc5SArnaldo Carvalho de Melo 	int err = 0;
936745cefc5SArnaldo Carvalho de Melo 
93705cea449SArnaldo Carvalho de Melo 	if (filter == NULL)
93805cea449SArnaldo Carvalho de Melo 		return -1;
93905cea449SArnaldo Carvalho de Melo 
940e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, evsel) {
9411fc632ceSJiri Olsa 		if (evsel->core.attr.type != PERF_TYPE_TRACEPOINT)
942fdf14720SWang Nan 			continue;
943fdf14720SWang Nan 
94494ad89bcSArnaldo Carvalho de Melo 		err = perf_evsel__set_filter(evsel, filter);
945745cefc5SArnaldo Carvalho de Melo 		if (err)
946745cefc5SArnaldo Carvalho de Melo 			break;
947745cefc5SArnaldo Carvalho de Melo 	}
948745cefc5SArnaldo Carvalho de Melo 
949745cefc5SArnaldo Carvalho de Melo 	return err;
9500a102479SFrederic Weisbecker }
95174429964SFrederic Weisbecker 
95253c92f73SArnaldo Carvalho de Melo int perf_evlist__append_tp_filter(struct evlist *evlist, const char *filter)
95353c92f73SArnaldo Carvalho de Melo {
95453c92f73SArnaldo Carvalho de Melo 	struct evsel *evsel;
95553c92f73SArnaldo Carvalho de Melo 	int err = 0;
95653c92f73SArnaldo Carvalho de Melo 
95753c92f73SArnaldo Carvalho de Melo 	if (filter == NULL)
95853c92f73SArnaldo Carvalho de Melo 		return -1;
95953c92f73SArnaldo Carvalho de Melo 
96053c92f73SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, evsel) {
96153c92f73SArnaldo Carvalho de Melo 		if (evsel->core.attr.type != PERF_TYPE_TRACEPOINT)
96253c92f73SArnaldo Carvalho de Melo 			continue;
96353c92f73SArnaldo Carvalho de Melo 
96453c92f73SArnaldo Carvalho de Melo 		err = perf_evsel__append_tp_filter(evsel, filter);
96553c92f73SArnaldo Carvalho de Melo 		if (err)
96653c92f73SArnaldo Carvalho de Melo 			break;
96753c92f73SArnaldo Carvalho de Melo 	}
96853c92f73SArnaldo Carvalho de Melo 
96953c92f73SArnaldo Carvalho de Melo 	return err;
97053c92f73SArnaldo Carvalho de Melo }
97153c92f73SArnaldo Carvalho de Melo 
97205cea449SArnaldo Carvalho de Melo static char *asprintf__tp_filter_pids(size_t npids, pid_t *pids)
973cfd70a26SArnaldo Carvalho de Melo {
974cfd70a26SArnaldo Carvalho de Melo 	char *filter;
975be199adaSArnaldo Carvalho de Melo 	size_t i;
976cfd70a26SArnaldo Carvalho de Melo 
977be199adaSArnaldo Carvalho de Melo 	for (i = 0; i < npids; ++i) {
978be199adaSArnaldo Carvalho de Melo 		if (i == 0) {
979be199adaSArnaldo Carvalho de Melo 			if (asprintf(&filter, "common_pid != %d", pids[i]) < 0)
98005cea449SArnaldo Carvalho de Melo 				return NULL;
981be199adaSArnaldo Carvalho de Melo 		} else {
982be199adaSArnaldo Carvalho de Melo 			char *tmp;
983be199adaSArnaldo Carvalho de Melo 
984be199adaSArnaldo Carvalho de Melo 			if (asprintf(&tmp, "%s && common_pid != %d", filter, pids[i]) < 0)
985be199adaSArnaldo Carvalho de Melo 				goto out_free;
986be199adaSArnaldo Carvalho de Melo 
987be199adaSArnaldo Carvalho de Melo 			free(filter);
988be199adaSArnaldo Carvalho de Melo 			filter = tmp;
989be199adaSArnaldo Carvalho de Melo 		}
990be199adaSArnaldo Carvalho de Melo 	}
991cfd70a26SArnaldo Carvalho de Melo 
99205cea449SArnaldo Carvalho de Melo 	return filter;
993be199adaSArnaldo Carvalho de Melo out_free:
994cfd70a26SArnaldo Carvalho de Melo 	free(filter);
99505cea449SArnaldo Carvalho de Melo 	return NULL;
99605cea449SArnaldo Carvalho de Melo }
99705cea449SArnaldo Carvalho de Melo 
99805cea449SArnaldo Carvalho de Melo int perf_evlist__set_tp_filter_pids(struct evlist *evlist, size_t npids, pid_t *pids)
99905cea449SArnaldo Carvalho de Melo {
100005cea449SArnaldo Carvalho de Melo 	char *filter = asprintf__tp_filter_pids(npids, pids);
100105cea449SArnaldo Carvalho de Melo 	int ret = perf_evlist__set_tp_filter(evlist, filter);
100205cea449SArnaldo Carvalho de Melo 
100305cea449SArnaldo Carvalho de Melo 	free(filter);
1004cfd70a26SArnaldo Carvalho de Melo 	return ret;
1005cfd70a26SArnaldo Carvalho de Melo }
1006cfd70a26SArnaldo Carvalho de Melo 
100763503dbaSJiri Olsa int perf_evlist__set_tp_filter_pid(struct evlist *evlist, pid_t pid)
1008be199adaSArnaldo Carvalho de Melo {
10097ad92a33SArnaldo Carvalho de Melo 	return perf_evlist__set_tp_filter_pids(evlist, 1, &pid);
1010be199adaSArnaldo Carvalho de Melo }
1011be199adaSArnaldo Carvalho de Melo 
10121827ab5bSArnaldo Carvalho de Melo int perf_evlist__append_tp_filter_pids(struct evlist *evlist, size_t npids, pid_t *pids)
10131827ab5bSArnaldo Carvalho de Melo {
10141827ab5bSArnaldo Carvalho de Melo 	char *filter = asprintf__tp_filter_pids(npids, pids);
10151827ab5bSArnaldo Carvalho de Melo 	int ret = perf_evlist__append_tp_filter(evlist, filter);
10161827ab5bSArnaldo Carvalho de Melo 
10171827ab5bSArnaldo Carvalho de Melo 	free(filter);
10181827ab5bSArnaldo Carvalho de Melo 	return ret;
10191827ab5bSArnaldo Carvalho de Melo }
10201827ab5bSArnaldo Carvalho de Melo 
10211827ab5bSArnaldo Carvalho de Melo int perf_evlist__append_tp_filter_pid(struct evlist *evlist, pid_t pid)
10221827ab5bSArnaldo Carvalho de Melo {
10231827ab5bSArnaldo Carvalho de Melo 	return perf_evlist__append_tp_filter_pids(evlist, 1, &pid);
10241827ab5bSArnaldo Carvalho de Melo }
10251827ab5bSArnaldo Carvalho de Melo 
102663503dbaSJiri Olsa bool perf_evlist__valid_sample_type(struct evlist *evlist)
102774429964SFrederic Weisbecker {
102832dcd021SJiri Olsa 	struct evsel *pos;
1029c2a70653SArnaldo Carvalho de Melo 
10306484d2f9SJiri Olsa 	if (evlist->core.nr_entries == 1)
103175562573SAdrian Hunter 		return true;
103275562573SAdrian Hunter 
103375562573SAdrian Hunter 	if (evlist->id_pos < 0 || evlist->is_pos < 0)
103475562573SAdrian Hunter 		return false;
103575562573SAdrian Hunter 
1036e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, pos) {
103775562573SAdrian Hunter 		if (pos->id_pos != evlist->id_pos ||
103875562573SAdrian Hunter 		    pos->is_pos != evlist->is_pos)
1039c2a70653SArnaldo Carvalho de Melo 			return false;
104074429964SFrederic Weisbecker 	}
104174429964SFrederic Weisbecker 
1042c2a70653SArnaldo Carvalho de Melo 	return true;
1043c2a70653SArnaldo Carvalho de Melo }
1044c2a70653SArnaldo Carvalho de Melo 
104563503dbaSJiri Olsa u64 __perf_evlist__combined_sample_type(struct evlist *evlist)
1046c2a70653SArnaldo Carvalho de Melo {
104732dcd021SJiri Olsa 	struct evsel *evsel;
104875562573SAdrian Hunter 
104975562573SAdrian Hunter 	if (evlist->combined_sample_type)
105075562573SAdrian Hunter 		return evlist->combined_sample_type;
105175562573SAdrian Hunter 
1052e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, evsel)
10531fc632ceSJiri Olsa 		evlist->combined_sample_type |= evsel->core.attr.sample_type;
105475562573SAdrian Hunter 
105575562573SAdrian Hunter 	return evlist->combined_sample_type;
105675562573SAdrian Hunter }
105775562573SAdrian Hunter 
105863503dbaSJiri Olsa u64 perf_evlist__combined_sample_type(struct evlist *evlist)
105975562573SAdrian Hunter {
106075562573SAdrian Hunter 	evlist->combined_sample_type = 0;
106175562573SAdrian Hunter 	return __perf_evlist__combined_sample_type(evlist);
1062c2a70653SArnaldo Carvalho de Melo }
1063c2a70653SArnaldo Carvalho de Melo 
106463503dbaSJiri Olsa u64 perf_evlist__combined_branch_type(struct evlist *evlist)
106598df858eSAndi Kleen {
106632dcd021SJiri Olsa 	struct evsel *evsel;
106798df858eSAndi Kleen 	u64 branch_type = 0;
106898df858eSAndi Kleen 
1069e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, evsel)
10701fc632ceSJiri Olsa 		branch_type |= evsel->core.attr.branch_sample_type;
107198df858eSAndi Kleen 	return branch_type;
107298df858eSAndi Kleen }
107398df858eSAndi Kleen 
107463503dbaSJiri Olsa bool perf_evlist__valid_read_format(struct evlist *evlist)
10759ede473cSJiri Olsa {
1076515dbe48SJiri Olsa 	struct evsel *first = evlist__first(evlist), *pos = first;
10771fc632ceSJiri Olsa 	u64 read_format = first->core.attr.read_format;
10781fc632ceSJiri Olsa 	u64 sample_type = first->core.attr.sample_type;
10799ede473cSJiri Olsa 
1080e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, pos) {
10811fc632ceSJiri Olsa 		if (read_format != pos->core.attr.read_format)
10829ede473cSJiri Olsa 			return false;
10839ede473cSJiri Olsa 	}
10849ede473cSJiri Olsa 
10859ede473cSJiri Olsa 	/* PERF_SAMPLE_READ imples PERF_FORMAT_ID. */
10869ede473cSJiri Olsa 	if ((sample_type & PERF_SAMPLE_READ) &&
10879ede473cSJiri Olsa 	    !(read_format & PERF_FORMAT_ID)) {
10889ede473cSJiri Olsa 		return false;
10899ede473cSJiri Olsa 	}
10909ede473cSJiri Olsa 
10919ede473cSJiri Olsa 	return true;
10929ede473cSJiri Olsa }
10939ede473cSJiri Olsa 
109463503dbaSJiri Olsa u16 perf_evlist__id_hdr_size(struct evlist *evlist)
109581e36bffSArnaldo Carvalho de Melo {
1096515dbe48SJiri Olsa 	struct evsel *first = evlist__first(evlist);
109781e36bffSArnaldo Carvalho de Melo 	struct perf_sample *data;
109881e36bffSArnaldo Carvalho de Melo 	u64 sample_type;
109981e36bffSArnaldo Carvalho de Melo 	u16 size = 0;
110081e36bffSArnaldo Carvalho de Melo 
11011fc632ceSJiri Olsa 	if (!first->core.attr.sample_id_all)
110281e36bffSArnaldo Carvalho de Melo 		goto out;
110381e36bffSArnaldo Carvalho de Melo 
11041fc632ceSJiri Olsa 	sample_type = first->core.attr.sample_type;
110581e36bffSArnaldo Carvalho de Melo 
110681e36bffSArnaldo Carvalho de Melo 	if (sample_type & PERF_SAMPLE_TID)
110781e36bffSArnaldo Carvalho de Melo 		size += sizeof(data->tid) * 2;
110881e36bffSArnaldo Carvalho de Melo 
110981e36bffSArnaldo Carvalho de Melo        if (sample_type & PERF_SAMPLE_TIME)
111081e36bffSArnaldo Carvalho de Melo 		size += sizeof(data->time);
111181e36bffSArnaldo Carvalho de Melo 
111281e36bffSArnaldo Carvalho de Melo 	if (sample_type & PERF_SAMPLE_ID)
111381e36bffSArnaldo Carvalho de Melo 		size += sizeof(data->id);
111481e36bffSArnaldo Carvalho de Melo 
111581e36bffSArnaldo Carvalho de Melo 	if (sample_type & PERF_SAMPLE_STREAM_ID)
111681e36bffSArnaldo Carvalho de Melo 		size += sizeof(data->stream_id);
111781e36bffSArnaldo Carvalho de Melo 
111881e36bffSArnaldo Carvalho de Melo 	if (sample_type & PERF_SAMPLE_CPU)
111981e36bffSArnaldo Carvalho de Melo 		size += sizeof(data->cpu) * 2;
112075562573SAdrian Hunter 
112175562573SAdrian Hunter 	if (sample_type & PERF_SAMPLE_IDENTIFIER)
112275562573SAdrian Hunter 		size += sizeof(data->id);
112381e36bffSArnaldo Carvalho de Melo out:
112481e36bffSArnaldo Carvalho de Melo 	return size;
112581e36bffSArnaldo Carvalho de Melo }
112681e36bffSArnaldo Carvalho de Melo 
112763503dbaSJiri Olsa bool perf_evlist__valid_sample_id_all(struct evlist *evlist)
1128c2a70653SArnaldo Carvalho de Melo {
1129515dbe48SJiri Olsa 	struct evsel *first = evlist__first(evlist), *pos = first;
1130c2a70653SArnaldo Carvalho de Melo 
1131e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry_continue(evlist, pos) {
11321fc632ceSJiri Olsa 		if (first->core.attr.sample_id_all != pos->core.attr.sample_id_all)
1133c2a70653SArnaldo Carvalho de Melo 			return false;
1134c2a70653SArnaldo Carvalho de Melo 	}
1135c2a70653SArnaldo Carvalho de Melo 
1136c2a70653SArnaldo Carvalho de Melo 	return true;
113774429964SFrederic Weisbecker }
113874429964SFrederic Weisbecker 
113963503dbaSJiri Olsa bool perf_evlist__sample_id_all(struct evlist *evlist)
114074429964SFrederic Weisbecker {
1141515dbe48SJiri Olsa 	struct evsel *first = evlist__first(evlist);
11421fc632ceSJiri Olsa 	return first->core.attr.sample_id_all;
114374429964SFrederic Weisbecker }
114481cce8deSArnaldo Carvalho de Melo 
114563503dbaSJiri Olsa void perf_evlist__set_selected(struct evlist *evlist,
114632dcd021SJiri Olsa 			       struct evsel *evsel)
114781cce8deSArnaldo Carvalho de Melo {
114881cce8deSArnaldo Carvalho de Melo 	evlist->selected = evsel;
114981cce8deSArnaldo Carvalho de Melo }
1150727ab04eSArnaldo Carvalho de Melo 
1151750b4edeSJiri Olsa void evlist__close(struct evlist *evlist)
1152a74b4b66SNamhyung Kim {
115332dcd021SJiri Olsa 	struct evsel *evsel;
1154a74b4b66SNamhyung Kim 
1155475fb533SAndi Kleen 	evlist__for_each_entry_reverse(evlist, evsel)
115688761fa1SJiri Olsa 		evsel__close(evsel);
1157a74b4b66SNamhyung Kim }
1158a74b4b66SNamhyung Kim 
115963503dbaSJiri Olsa static int perf_evlist__create_syswide_maps(struct evlist *evlist)
11604112eb18SArnaldo Carvalho de Melo {
1161f854839bSJiri Olsa 	struct perf_cpu_map *cpus;
11629749b90eSJiri Olsa 	struct perf_thread_map *threads;
11634112eb18SArnaldo Carvalho de Melo 	int err = -ENOMEM;
11644112eb18SArnaldo Carvalho de Melo 
11654112eb18SArnaldo Carvalho de Melo 	/*
11664112eb18SArnaldo Carvalho de Melo 	 * Try reading /sys/devices/system/cpu/online to get
11674112eb18SArnaldo Carvalho de Melo 	 * an all cpus map.
11684112eb18SArnaldo Carvalho de Melo 	 *
11694112eb18SArnaldo Carvalho de Melo 	 * FIXME: -ENOMEM is the best we can do here, the cpu_map
11704112eb18SArnaldo Carvalho de Melo 	 * code needs an overhaul to properly forward the
11714112eb18SArnaldo Carvalho de Melo 	 * error, and we may not want to do that fallback to a
11724112eb18SArnaldo Carvalho de Melo 	 * default cpu identity map :-\
11734112eb18SArnaldo Carvalho de Melo 	 */
11749c3516d1SJiri Olsa 	cpus = perf_cpu_map__new(NULL);
11758c0498b6SAdrian Hunter 	if (!cpus)
11764112eb18SArnaldo Carvalho de Melo 		goto out;
11774112eb18SArnaldo Carvalho de Melo 
11784b49cce2SJiri Olsa 	threads = perf_thread_map__new_dummy();
11798c0498b6SAdrian Hunter 	if (!threads)
11808c0498b6SAdrian Hunter 		goto out_put;
11814112eb18SArnaldo Carvalho de Melo 
1182453fa030SJiri Olsa 	perf_evlist__set_maps(&evlist->core, cpus, threads);
11834112eb18SArnaldo Carvalho de Melo out:
11844112eb18SArnaldo Carvalho de Melo 	return err;
11858c0498b6SAdrian Hunter out_put:
118638f01d8dSJiri Olsa 	perf_cpu_map__put(cpus);
11874112eb18SArnaldo Carvalho de Melo 	goto out;
11884112eb18SArnaldo Carvalho de Melo }
11894112eb18SArnaldo Carvalho de Melo 
1190474ddc4cSJiri Olsa int evlist__open(struct evlist *evlist)
1191727ab04eSArnaldo Carvalho de Melo {
119232dcd021SJiri Olsa 	struct evsel *evsel;
1193a74b4b66SNamhyung Kim 	int err;
1194727ab04eSArnaldo Carvalho de Melo 
11954112eb18SArnaldo Carvalho de Melo 	/*
11964112eb18SArnaldo Carvalho de Melo 	 * Default: one fd per CPU, all threads, aka systemwide
11974112eb18SArnaldo Carvalho de Melo 	 * as sys_perf_event_open(cpu = -1, thread = -1) is EINVAL
11984112eb18SArnaldo Carvalho de Melo 	 */
119903617c22SJiri Olsa 	if (evlist->core.threads == NULL && evlist->core.cpus == NULL) {
12004112eb18SArnaldo Carvalho de Melo 		err = perf_evlist__create_syswide_maps(evlist);
12014112eb18SArnaldo Carvalho de Melo 		if (err < 0)
12024112eb18SArnaldo Carvalho de Melo 			goto out_err;
12034112eb18SArnaldo Carvalho de Melo 	}
12044112eb18SArnaldo Carvalho de Melo 
1205733cd2feSAdrian Hunter 	perf_evlist__update_id_pos(evlist);
1206733cd2feSAdrian Hunter 
1207e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, evsel) {
1208af663bd0SJiri Olsa 		err = evsel__open(evsel, evsel->core.cpus, evsel->core.threads);
1209727ab04eSArnaldo Carvalho de Melo 		if (err < 0)
1210727ab04eSArnaldo Carvalho de Melo 			goto out_err;
1211727ab04eSArnaldo Carvalho de Melo 	}
1212727ab04eSArnaldo Carvalho de Melo 
1213727ab04eSArnaldo Carvalho de Melo 	return 0;
1214727ab04eSArnaldo Carvalho de Melo out_err:
1215750b4edeSJiri Olsa 	evlist__close(evlist);
121641c21a68SNamhyung Kim 	errno = -err;
1217727ab04eSArnaldo Carvalho de Melo 	return err;
1218727ab04eSArnaldo Carvalho de Melo }
121935b9d88eSArnaldo Carvalho de Melo 
122063503dbaSJiri Olsa int perf_evlist__prepare_workload(struct evlist *evlist, struct target *target,
122155e162eaSNamhyung Kim 				  const char *argv[], bool pipe_output,
1222735f7e0bSArnaldo Carvalho de Melo 				  void (*exec_error)(int signo, siginfo_t *info, void *ucontext))
122335b9d88eSArnaldo Carvalho de Melo {
122435b9d88eSArnaldo Carvalho de Melo 	int child_ready_pipe[2], go_pipe[2];
122535b9d88eSArnaldo Carvalho de Melo 	char bf;
122635b9d88eSArnaldo Carvalho de Melo 
122735b9d88eSArnaldo Carvalho de Melo 	if (pipe(child_ready_pipe) < 0) {
122835b9d88eSArnaldo Carvalho de Melo 		perror("failed to create 'ready' pipe");
122935b9d88eSArnaldo Carvalho de Melo 		return -1;
123035b9d88eSArnaldo Carvalho de Melo 	}
123135b9d88eSArnaldo Carvalho de Melo 
123235b9d88eSArnaldo Carvalho de Melo 	if (pipe(go_pipe) < 0) {
123335b9d88eSArnaldo Carvalho de Melo 		perror("failed to create 'go' pipe");
123435b9d88eSArnaldo Carvalho de Melo 		goto out_close_ready_pipe;
123535b9d88eSArnaldo Carvalho de Melo 	}
123635b9d88eSArnaldo Carvalho de Melo 
123735b9d88eSArnaldo Carvalho de Melo 	evlist->workload.pid = fork();
123835b9d88eSArnaldo Carvalho de Melo 	if (evlist->workload.pid < 0) {
123935b9d88eSArnaldo Carvalho de Melo 		perror("failed to fork");
124035b9d88eSArnaldo Carvalho de Melo 		goto out_close_pipes;
124135b9d88eSArnaldo Carvalho de Melo 	}
124235b9d88eSArnaldo Carvalho de Melo 
124335b9d88eSArnaldo Carvalho de Melo 	if (!evlist->workload.pid) {
12445f1c4225SArnaldo Carvalho de Melo 		int ret;
12455f1c4225SArnaldo Carvalho de Melo 
1246119fa3c9SNamhyung Kim 		if (pipe_output)
124735b9d88eSArnaldo Carvalho de Melo 			dup2(2, 1);
124835b9d88eSArnaldo Carvalho de Melo 
12490817df08SDavid Ahern 		signal(SIGTERM, SIG_DFL);
12500817df08SDavid Ahern 
125135b9d88eSArnaldo Carvalho de Melo 		close(child_ready_pipe[0]);
125235b9d88eSArnaldo Carvalho de Melo 		close(go_pipe[1]);
125335b9d88eSArnaldo Carvalho de Melo 		fcntl(go_pipe[0], F_SETFD, FD_CLOEXEC);
125435b9d88eSArnaldo Carvalho de Melo 
125535b9d88eSArnaldo Carvalho de Melo 		/*
125635b9d88eSArnaldo Carvalho de Melo 		 * Tell the parent we're ready to go
125735b9d88eSArnaldo Carvalho de Melo 		 */
125835b9d88eSArnaldo Carvalho de Melo 		close(child_ready_pipe[1]);
125935b9d88eSArnaldo Carvalho de Melo 
126035b9d88eSArnaldo Carvalho de Melo 		/*
126135b9d88eSArnaldo Carvalho de Melo 		 * Wait until the parent tells us to go.
126235b9d88eSArnaldo Carvalho de Melo 		 */
12635f1c4225SArnaldo Carvalho de Melo 		ret = read(go_pipe[0], &bf, 1);
12645f1c4225SArnaldo Carvalho de Melo 		/*
12655f1c4225SArnaldo Carvalho de Melo 		 * The parent will ask for the execvp() to be performed by
12665f1c4225SArnaldo Carvalho de Melo 		 * writing exactly one byte, in workload.cork_fd, usually via
12675f1c4225SArnaldo Carvalho de Melo 		 * perf_evlist__start_workload().
12685f1c4225SArnaldo Carvalho de Melo 		 *
126920f86fc1SArnaldo Carvalho de Melo 		 * For cancelling the workload without actually running it,
12705f1c4225SArnaldo Carvalho de Melo 		 * the parent will just close workload.cork_fd, without writing
12715f1c4225SArnaldo Carvalho de Melo 		 * anything, i.e. read will return zero and we just exit()
12725f1c4225SArnaldo Carvalho de Melo 		 * here.
12735f1c4225SArnaldo Carvalho de Melo 		 */
12745f1c4225SArnaldo Carvalho de Melo 		if (ret != 1) {
12755f1c4225SArnaldo Carvalho de Melo 			if (ret == -1)
127635b9d88eSArnaldo Carvalho de Melo 				perror("unable to read pipe");
12775f1c4225SArnaldo Carvalho de Melo 			exit(ret);
12785f1c4225SArnaldo Carvalho de Melo 		}
127935b9d88eSArnaldo Carvalho de Melo 
128035b9d88eSArnaldo Carvalho de Melo 		execvp(argv[0], (char **)argv);
128135b9d88eSArnaldo Carvalho de Melo 
1282735f7e0bSArnaldo Carvalho de Melo 		if (exec_error) {
1283f33cbe72SArnaldo Carvalho de Melo 			union sigval val;
1284f33cbe72SArnaldo Carvalho de Melo 
1285f33cbe72SArnaldo Carvalho de Melo 			val.sival_int = errno;
1286f33cbe72SArnaldo Carvalho de Melo 			if (sigqueue(getppid(), SIGUSR1, val))
128735b9d88eSArnaldo Carvalho de Melo 				perror(argv[0]);
1288f33cbe72SArnaldo Carvalho de Melo 		} else
1289f33cbe72SArnaldo Carvalho de Melo 			perror(argv[0]);
129035b9d88eSArnaldo Carvalho de Melo 		exit(-1);
129135b9d88eSArnaldo Carvalho de Melo 	}
129235b9d88eSArnaldo Carvalho de Melo 
1293735f7e0bSArnaldo Carvalho de Melo 	if (exec_error) {
1294735f7e0bSArnaldo Carvalho de Melo 		struct sigaction act = {
1295735f7e0bSArnaldo Carvalho de Melo 			.sa_flags     = SA_SIGINFO,
1296735f7e0bSArnaldo Carvalho de Melo 			.sa_sigaction = exec_error,
1297735f7e0bSArnaldo Carvalho de Melo 		};
1298735f7e0bSArnaldo Carvalho de Melo 		sigaction(SIGUSR1, &act, NULL);
1299735f7e0bSArnaldo Carvalho de Melo 	}
1300735f7e0bSArnaldo Carvalho de Melo 
13011aaf63b1SArnaldo Carvalho de Melo 	if (target__none(target)) {
130203617c22SJiri Olsa 		if (evlist->core.threads == NULL) {
13031aaf63b1SArnaldo Carvalho de Melo 			fprintf(stderr, "FATAL: evlist->threads need to be set at this point (%s:%d).\n",
13041aaf63b1SArnaldo Carvalho de Melo 				__func__, __LINE__);
13051aaf63b1SArnaldo Carvalho de Melo 			goto out_close_pipes;
13061aaf63b1SArnaldo Carvalho de Melo 		}
130703617c22SJiri Olsa 		perf_thread_map__set_pid(evlist->core.threads, 0, evlist->workload.pid);
13081aaf63b1SArnaldo Carvalho de Melo 	}
130935b9d88eSArnaldo Carvalho de Melo 
131035b9d88eSArnaldo Carvalho de Melo 	close(child_ready_pipe[1]);
131135b9d88eSArnaldo Carvalho de Melo 	close(go_pipe[0]);
131235b9d88eSArnaldo Carvalho de Melo 	/*
131335b9d88eSArnaldo Carvalho de Melo 	 * wait for child to settle
131435b9d88eSArnaldo Carvalho de Melo 	 */
131535b9d88eSArnaldo Carvalho de Melo 	if (read(child_ready_pipe[0], &bf, 1) == -1) {
131635b9d88eSArnaldo Carvalho de Melo 		perror("unable to read pipe");
131735b9d88eSArnaldo Carvalho de Melo 		goto out_close_pipes;
131835b9d88eSArnaldo Carvalho de Melo 	}
131935b9d88eSArnaldo Carvalho de Melo 
1320bcf3145fSNamhyung Kim 	fcntl(go_pipe[1], F_SETFD, FD_CLOEXEC);
132135b9d88eSArnaldo Carvalho de Melo 	evlist->workload.cork_fd = go_pipe[1];
132235b9d88eSArnaldo Carvalho de Melo 	close(child_ready_pipe[0]);
132335b9d88eSArnaldo Carvalho de Melo 	return 0;
132435b9d88eSArnaldo Carvalho de Melo 
132535b9d88eSArnaldo Carvalho de Melo out_close_pipes:
132635b9d88eSArnaldo Carvalho de Melo 	close(go_pipe[0]);
132735b9d88eSArnaldo Carvalho de Melo 	close(go_pipe[1]);
132835b9d88eSArnaldo Carvalho de Melo out_close_ready_pipe:
132935b9d88eSArnaldo Carvalho de Melo 	close(child_ready_pipe[0]);
133035b9d88eSArnaldo Carvalho de Melo 	close(child_ready_pipe[1]);
133135b9d88eSArnaldo Carvalho de Melo 	return -1;
133235b9d88eSArnaldo Carvalho de Melo }
133335b9d88eSArnaldo Carvalho de Melo 
133463503dbaSJiri Olsa int perf_evlist__start_workload(struct evlist *evlist)
133535b9d88eSArnaldo Carvalho de Melo {
133635b9d88eSArnaldo Carvalho de Melo 	if (evlist->workload.cork_fd > 0) {
1337b3824404SDavid Ahern 		char bf = 0;
1338bcf3145fSNamhyung Kim 		int ret;
133935b9d88eSArnaldo Carvalho de Melo 		/*
134035b9d88eSArnaldo Carvalho de Melo 		 * Remove the cork, let it rip!
134135b9d88eSArnaldo Carvalho de Melo 		 */
1342bcf3145fSNamhyung Kim 		ret = write(evlist->workload.cork_fd, &bf, 1);
1343bcf3145fSNamhyung Kim 		if (ret < 0)
1344e978be9eSSoramichi Akiyama 			perror("unable to write to pipe");
1345bcf3145fSNamhyung Kim 
1346bcf3145fSNamhyung Kim 		close(evlist->workload.cork_fd);
1347bcf3145fSNamhyung Kim 		return ret;
134835b9d88eSArnaldo Carvalho de Melo 	}
134935b9d88eSArnaldo Carvalho de Melo 
135035b9d88eSArnaldo Carvalho de Melo 	return 0;
135135b9d88eSArnaldo Carvalho de Melo }
1352cb0b29e0SArnaldo Carvalho de Melo 
135363503dbaSJiri Olsa int perf_evlist__parse_sample(struct evlist *evlist, union perf_event *event,
13540807d2d8SArnaldo Carvalho de Melo 			      struct perf_sample *sample)
1355cb0b29e0SArnaldo Carvalho de Melo {
135632dcd021SJiri Olsa 	struct evsel *evsel = perf_evlist__event2evsel(evlist, event);
135775562573SAdrian Hunter 
135875562573SAdrian Hunter 	if (!evsel)
135975562573SAdrian Hunter 		return -EFAULT;
13600807d2d8SArnaldo Carvalho de Melo 	return perf_evsel__parse_sample(evsel, event, sample);
1361cb0b29e0SArnaldo Carvalho de Melo }
136278f067b3SArnaldo Carvalho de Melo 
136363503dbaSJiri Olsa int perf_evlist__parse_sample_timestamp(struct evlist *evlist,
136401468120SJiri Olsa 					union perf_event *event,
136501468120SJiri Olsa 					u64 *timestamp)
136601468120SJiri Olsa {
136732dcd021SJiri Olsa 	struct evsel *evsel = perf_evlist__event2evsel(evlist, event);
136801468120SJiri Olsa 
136901468120SJiri Olsa 	if (!evsel)
137001468120SJiri Olsa 		return -EFAULT;
137101468120SJiri Olsa 	return perf_evsel__parse_sample_timestamp(evsel, event, timestamp);
137201468120SJiri Olsa }
137301468120SJiri Olsa 
137463503dbaSJiri Olsa int perf_evlist__strerror_open(struct evlist *evlist,
1375a8f23d8fSArnaldo Carvalho de Melo 			       int err, char *buf, size_t size)
1376a8f23d8fSArnaldo Carvalho de Melo {
1377a8f23d8fSArnaldo Carvalho de Melo 	int printed, value;
1378c8b5f2c9SArnaldo Carvalho de Melo 	char sbuf[STRERR_BUFSIZE], *emsg = str_error_r(err, sbuf, sizeof(sbuf));
1379a8f23d8fSArnaldo Carvalho de Melo 
1380a8f23d8fSArnaldo Carvalho de Melo 	switch (err) {
1381a8f23d8fSArnaldo Carvalho de Melo 	case EACCES:
1382a8f23d8fSArnaldo Carvalho de Melo 	case EPERM:
1383a8f23d8fSArnaldo Carvalho de Melo 		printed = scnprintf(buf, size,
1384a8f23d8fSArnaldo Carvalho de Melo 				    "Error:\t%s.\n"
1385a8f23d8fSArnaldo Carvalho de Melo 				    "Hint:\tCheck /proc/sys/kernel/perf_event_paranoid setting.", emsg);
1386a8f23d8fSArnaldo Carvalho de Melo 
13871a47245dSAdrian Hunter 		value = perf_event_paranoid();
1388a8f23d8fSArnaldo Carvalho de Melo 
1389a8f23d8fSArnaldo Carvalho de Melo 		printed += scnprintf(buf + printed, size - printed, "\nHint:\t");
1390a8f23d8fSArnaldo Carvalho de Melo 
1391a8f23d8fSArnaldo Carvalho de Melo 		if (value >= 2) {
1392a8f23d8fSArnaldo Carvalho de Melo 			printed += scnprintf(buf + printed, size - printed,
1393a8f23d8fSArnaldo Carvalho de Melo 					     "For your workloads it needs to be <= 1\nHint:\t");
1394a8f23d8fSArnaldo Carvalho de Melo 		}
1395a8f23d8fSArnaldo Carvalho de Melo 		printed += scnprintf(buf + printed, size - printed,
13965229e366SArnaldo Carvalho de Melo 				     "For system wide tracing it needs to be set to -1.\n");
1397a8f23d8fSArnaldo Carvalho de Melo 
1398a8f23d8fSArnaldo Carvalho de Melo 		printed += scnprintf(buf + printed, size - printed,
13995229e366SArnaldo Carvalho de Melo 				    "Hint:\tTry: 'sudo sh -c \"echo -1 > /proc/sys/kernel/perf_event_paranoid\"'\n"
14005229e366SArnaldo Carvalho de Melo 				    "Hint:\tThe current value is %d.", value);
1401a8f23d8fSArnaldo Carvalho de Melo 		break;
1402d9aade7fSArnaldo Carvalho de Melo 	case EINVAL: {
1403515dbe48SJiri Olsa 		struct evsel *first = evlist__first(evlist);
1404d9aade7fSArnaldo Carvalho de Melo 		int max_freq;
1405d9aade7fSArnaldo Carvalho de Melo 
1406d9aade7fSArnaldo Carvalho de Melo 		if (sysctl__read_int("kernel/perf_event_max_sample_rate", &max_freq) < 0)
1407d9aade7fSArnaldo Carvalho de Melo 			goto out_default;
1408d9aade7fSArnaldo Carvalho de Melo 
14091fc632ceSJiri Olsa 		if (first->core.attr.sample_freq < (u64)max_freq)
1410d9aade7fSArnaldo Carvalho de Melo 			goto out_default;
1411d9aade7fSArnaldo Carvalho de Melo 
1412d9aade7fSArnaldo Carvalho de Melo 		printed = scnprintf(buf, size,
1413d9aade7fSArnaldo Carvalho de Melo 				    "Error:\t%s.\n"
1414d9aade7fSArnaldo Carvalho de Melo 				    "Hint:\tCheck /proc/sys/kernel/perf_event_max_sample_rate.\n"
1415d9aade7fSArnaldo Carvalho de Melo 				    "Hint:\tThe current value is %d and %" PRIu64 " is being requested.",
14161fc632ceSJiri Olsa 				    emsg, max_freq, first->core.attr.sample_freq);
1417d9aade7fSArnaldo Carvalho de Melo 		break;
1418d9aade7fSArnaldo Carvalho de Melo 	}
1419a8f23d8fSArnaldo Carvalho de Melo 	default:
1420d9aade7fSArnaldo Carvalho de Melo out_default:
1421a8f23d8fSArnaldo Carvalho de Melo 		scnprintf(buf, size, "%s", emsg);
1422a8f23d8fSArnaldo Carvalho de Melo 		break;
1423a8f23d8fSArnaldo Carvalho de Melo 	}
1424a8f23d8fSArnaldo Carvalho de Melo 
1425a8f23d8fSArnaldo Carvalho de Melo 	return 0;
1426a8f23d8fSArnaldo Carvalho de Melo }
1427a025e4f0SAdrian Hunter 
142863503dbaSJiri Olsa int perf_evlist__strerror_mmap(struct evlist *evlist, int err, char *buf, size_t size)
1429956fa571SArnaldo Carvalho de Melo {
1430c8b5f2c9SArnaldo Carvalho de Melo 	char sbuf[STRERR_BUFSIZE], *emsg = str_error_r(err, sbuf, sizeof(sbuf));
1431f6fa4375SJiri Olsa 	int pages_attempted = evlist->core.mmap_len / 1024, pages_max_per_user, printed = 0;
1432956fa571SArnaldo Carvalho de Melo 
1433956fa571SArnaldo Carvalho de Melo 	switch (err) {
1434956fa571SArnaldo Carvalho de Melo 	case EPERM:
1435e5d4a290SArnaldo Carvalho de Melo 		sysctl__read_int("kernel/perf_event_mlock_kb", &pages_max_per_user);
1436e965bea1SArnaldo Carvalho de Melo 		printed += scnprintf(buf + printed, size - printed,
1437e965bea1SArnaldo Carvalho de Melo 				     "Error:\t%s.\n"
1438956fa571SArnaldo Carvalho de Melo 				     "Hint:\tCheck /proc/sys/kernel/perf_event_mlock_kb (%d kB) setting.\n"
1439e965bea1SArnaldo Carvalho de Melo 				     "Hint:\tTried using %zd kB.\n",
1440e5d4a290SArnaldo Carvalho de Melo 				     emsg, pages_max_per_user, pages_attempted);
1441e965bea1SArnaldo Carvalho de Melo 
1442e965bea1SArnaldo Carvalho de Melo 		if (pages_attempted >= pages_max_per_user) {
1443e965bea1SArnaldo Carvalho de Melo 			printed += scnprintf(buf + printed, size - printed,
1444e965bea1SArnaldo Carvalho de Melo 					     "Hint:\tTry 'sudo sh -c \"echo %d > /proc/sys/kernel/perf_event_mlock_kb\"', or\n",
1445e965bea1SArnaldo Carvalho de Melo 					     pages_max_per_user + pages_attempted);
1446e965bea1SArnaldo Carvalho de Melo 		}
1447e965bea1SArnaldo Carvalho de Melo 
1448e965bea1SArnaldo Carvalho de Melo 		printed += scnprintf(buf + printed, size - printed,
1449e965bea1SArnaldo Carvalho de Melo 				     "Hint:\tTry using a smaller -m/--mmap-pages value.");
1450956fa571SArnaldo Carvalho de Melo 		break;
1451956fa571SArnaldo Carvalho de Melo 	default:
1452956fa571SArnaldo Carvalho de Melo 		scnprintf(buf, size, "%s", emsg);
1453956fa571SArnaldo Carvalho de Melo 		break;
1454956fa571SArnaldo Carvalho de Melo 	}
1455956fa571SArnaldo Carvalho de Melo 
1456956fa571SArnaldo Carvalho de Melo 	return 0;
1457956fa571SArnaldo Carvalho de Melo }
1458956fa571SArnaldo Carvalho de Melo 
145963503dbaSJiri Olsa void perf_evlist__to_front(struct evlist *evlist,
146032dcd021SJiri Olsa 			   struct evsel *move_evsel)
1461a025e4f0SAdrian Hunter {
146232dcd021SJiri Olsa 	struct evsel *evsel, *n;
1463a025e4f0SAdrian Hunter 	LIST_HEAD(move);
1464a025e4f0SAdrian Hunter 
1465515dbe48SJiri Olsa 	if (move_evsel == evlist__first(evlist))
1466a025e4f0SAdrian Hunter 		return;
1467a025e4f0SAdrian Hunter 
1468e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry_safe(evlist, n, evsel) {
1469a025e4f0SAdrian Hunter 		if (evsel->leader == move_evsel->leader)
1470b27c4eceSJiri Olsa 			list_move_tail(&evsel->core.node, &move);
1471a025e4f0SAdrian Hunter 	}
1472a025e4f0SAdrian Hunter 
1473ce9036a6SJiri Olsa 	list_splice(&move, &evlist->core.entries);
1474a025e4f0SAdrian Hunter }
147560b0896cSAdrian Hunter 
147663503dbaSJiri Olsa void perf_evlist__set_tracking_event(struct evlist *evlist,
147732dcd021SJiri Olsa 				     struct evsel *tracking_evsel)
147860b0896cSAdrian Hunter {
147932dcd021SJiri Olsa 	struct evsel *evsel;
148060b0896cSAdrian Hunter 
148160b0896cSAdrian Hunter 	if (tracking_evsel->tracking)
148260b0896cSAdrian Hunter 		return;
148360b0896cSAdrian Hunter 
1484e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, evsel) {
148560b0896cSAdrian Hunter 		if (evsel != tracking_evsel)
148660b0896cSAdrian Hunter 			evsel->tracking = false;
148760b0896cSAdrian Hunter 	}
148860b0896cSAdrian Hunter 
148960b0896cSAdrian Hunter 	tracking_evsel->tracking = true;
149060b0896cSAdrian Hunter }
14917630b3e2SWang Nan 
149232dcd021SJiri Olsa struct evsel *
149363503dbaSJiri Olsa perf_evlist__find_evsel_by_str(struct evlist *evlist,
14947630b3e2SWang Nan 			       const char *str)
14957630b3e2SWang Nan {
149632dcd021SJiri Olsa 	struct evsel *evsel;
14977630b3e2SWang Nan 
1498e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, evsel) {
14997630b3e2SWang Nan 		if (!evsel->name)
15007630b3e2SWang Nan 			continue;
15017630b3e2SWang Nan 		if (strcmp(str, evsel->name) == 0)
15027630b3e2SWang Nan 			return evsel;
15037630b3e2SWang Nan 	}
15047630b3e2SWang Nan 
15057630b3e2SWang Nan 	return NULL;
15067630b3e2SWang Nan }
150754cc54deSWang Nan 
150863503dbaSJiri Olsa void perf_evlist__toggle_bkw_mmap(struct evlist *evlist,
150954cc54deSWang Nan 				  enum bkw_mmap_state state)
151054cc54deSWang Nan {
151154cc54deSWang Nan 	enum bkw_mmap_state old_state = evlist->bkw_mmap_state;
151254cc54deSWang Nan 	enum action {
151354cc54deSWang Nan 		NONE,
151454cc54deSWang Nan 		PAUSE,
151554cc54deSWang Nan 		RESUME,
151654cc54deSWang Nan 	} action = NONE;
151754cc54deSWang Nan 
15180b72d69aSWang Nan 	if (!evlist->overwrite_mmap)
151954cc54deSWang Nan 		return;
152054cc54deSWang Nan 
152154cc54deSWang Nan 	switch (old_state) {
152254cc54deSWang Nan 	case BKW_MMAP_NOTREADY: {
152354cc54deSWang Nan 		if (state != BKW_MMAP_RUNNING)
1524dd8bd53aSLuis de Bethencourt 			goto state_err;
152554cc54deSWang Nan 		break;
152654cc54deSWang Nan 	}
152754cc54deSWang Nan 	case BKW_MMAP_RUNNING: {
152854cc54deSWang Nan 		if (state != BKW_MMAP_DATA_PENDING)
152954cc54deSWang Nan 			goto state_err;
153054cc54deSWang Nan 		action = PAUSE;
153154cc54deSWang Nan 		break;
153254cc54deSWang Nan 	}
153354cc54deSWang Nan 	case BKW_MMAP_DATA_PENDING: {
153454cc54deSWang Nan 		if (state != BKW_MMAP_EMPTY)
153554cc54deSWang Nan 			goto state_err;
153654cc54deSWang Nan 		break;
153754cc54deSWang Nan 	}
153854cc54deSWang Nan 	case BKW_MMAP_EMPTY: {
153954cc54deSWang Nan 		if (state != BKW_MMAP_RUNNING)
154054cc54deSWang Nan 			goto state_err;
154154cc54deSWang Nan 		action = RESUME;
154254cc54deSWang Nan 		break;
154354cc54deSWang Nan 	}
154454cc54deSWang Nan 	default:
154554cc54deSWang Nan 		WARN_ONCE(1, "Shouldn't get there\n");
154654cc54deSWang Nan 	}
154754cc54deSWang Nan 
154854cc54deSWang Nan 	evlist->bkw_mmap_state = state;
154954cc54deSWang Nan 
155054cc54deSWang Nan 	switch (action) {
155154cc54deSWang Nan 	case PAUSE:
155254cc54deSWang Nan 		perf_evlist__pause(evlist);
155354cc54deSWang Nan 		break;
155454cc54deSWang Nan 	case RESUME:
155554cc54deSWang Nan 		perf_evlist__resume(evlist);
155654cc54deSWang Nan 		break;
155754cc54deSWang Nan 	case NONE:
155854cc54deSWang Nan 	default:
155954cc54deSWang Nan 		break;
156054cc54deSWang Nan 	}
156154cc54deSWang Nan 
156254cc54deSWang Nan state_err:
156354cc54deSWang Nan 	return;
156454cc54deSWang Nan }
156507d6f446SArnaldo Carvalho de Melo 
156663503dbaSJiri Olsa bool perf_evlist__exclude_kernel(struct evlist *evlist)
156707d6f446SArnaldo Carvalho de Melo {
156832dcd021SJiri Olsa 	struct evsel *evsel;
156907d6f446SArnaldo Carvalho de Melo 
157007d6f446SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, evsel) {
15711fc632ceSJiri Olsa 		if (!evsel->core.attr.exclude_kernel)
157207d6f446SArnaldo Carvalho de Melo 			return false;
157307d6f446SArnaldo Carvalho de Melo 	}
157407d6f446SArnaldo Carvalho de Melo 
157507d6f446SArnaldo Carvalho de Melo 	return true;
157607d6f446SArnaldo Carvalho de Melo }
1577e2bdbe80SJin Yao 
1578e2bdbe80SJin Yao /*
1579e2bdbe80SJin Yao  * Events in data file are not collect in groups, but we still want
1580e2bdbe80SJin Yao  * the group display. Set the artificial group and set the leader's
1581e2bdbe80SJin Yao  * forced_leader flag to notify the display code.
1582e2bdbe80SJin Yao  */
158363503dbaSJiri Olsa void perf_evlist__force_leader(struct evlist *evlist)
1584e2bdbe80SJin Yao {
1585e2bdbe80SJin Yao 	if (!evlist->nr_groups) {
1586515dbe48SJiri Olsa 		struct evsel *leader = evlist__first(evlist);
1587e2bdbe80SJin Yao 
1588e2bdbe80SJin Yao 		perf_evlist__set_leader(evlist);
1589e2bdbe80SJin Yao 		leader->forced_leader = true;
1590e2bdbe80SJin Yao 	}
1591e2bdbe80SJin Yao }
1592c3537fc2SAndi Kleen 
159363503dbaSJiri Olsa struct evsel *perf_evlist__reset_weak_group(struct evlist *evsel_list,
159432dcd021SJiri Olsa 						 struct evsel *evsel)
1595c3537fc2SAndi Kleen {
159632dcd021SJiri Olsa 	struct evsel *c2, *leader;
1597c3537fc2SAndi Kleen 	bool is_open = true;
1598c3537fc2SAndi Kleen 
1599c3537fc2SAndi Kleen 	leader = evsel->leader;
1600c3537fc2SAndi Kleen 	pr_debug("Weak group for %s/%d failed\n",
16015643b1a5SJiri Olsa 			leader->name, leader->core.nr_members);
1602c3537fc2SAndi Kleen 
1603c3537fc2SAndi Kleen 	/*
1604c3537fc2SAndi Kleen 	 * for_each_group_member doesn't work here because it doesn't
1605c3537fc2SAndi Kleen 	 * include the first entry.
1606c3537fc2SAndi Kleen 	 */
1607c3537fc2SAndi Kleen 	evlist__for_each_entry(evsel_list, c2) {
1608c3537fc2SAndi Kleen 		if (c2 == evsel)
1609c3537fc2SAndi Kleen 			is_open = false;
1610c3537fc2SAndi Kleen 		if (c2->leader == leader) {
1611c3537fc2SAndi Kleen 			if (is_open)
16127834fa94SAndi Kleen 				perf_evsel__close(&evsel->core);
1613c3537fc2SAndi Kleen 			c2->leader = c2;
16145643b1a5SJiri Olsa 			c2->core.nr_members = 0;
1615c3537fc2SAndi Kleen 		}
1616c3537fc2SAndi Kleen 	}
1617c3537fc2SAndi Kleen 	return leader;
1618c3537fc2SAndi Kleen }
1619657ee553SSong Liu 
162063503dbaSJiri Olsa int perf_evlist__add_sb_event(struct evlist **evlist,
1621657ee553SSong Liu 			      struct perf_event_attr *attr,
1622657ee553SSong Liu 			      perf_evsel__sb_cb_t cb,
1623657ee553SSong Liu 			      void *data)
1624657ee553SSong Liu {
162532dcd021SJiri Olsa 	struct evsel *evsel;
1626657ee553SSong Liu 	bool new_evlist = (*evlist) == NULL;
1627657ee553SSong Liu 
1628657ee553SSong Liu 	if (*evlist == NULL)
16290f98b11cSJiri Olsa 		*evlist = evlist__new();
1630657ee553SSong Liu 	if (*evlist == NULL)
1631657ee553SSong Liu 		return -1;
1632657ee553SSong Liu 
1633657ee553SSong Liu 	if (!attr->sample_id_all) {
1634657ee553SSong Liu 		pr_warning("enabling sample_id_all for all side band events\n");
1635657ee553SSong Liu 		attr->sample_id_all = 1;
1636657ee553SSong Liu 	}
1637657ee553SSong Liu 
16386484d2f9SJiri Olsa 	evsel = perf_evsel__new_idx(attr, (*evlist)->core.nr_entries);
1639657ee553SSong Liu 	if (!evsel)
1640657ee553SSong Liu 		goto out_err;
1641657ee553SSong Liu 
1642657ee553SSong Liu 	evsel->side_band.cb = cb;
1643657ee553SSong Liu 	evsel->side_band.data = data;
1644a1cf3a75SJiri Olsa 	evlist__add(*evlist, evsel);
1645657ee553SSong Liu 	return 0;
1646657ee553SSong Liu 
1647657ee553SSong Liu out_err:
1648657ee553SSong Liu 	if (new_evlist) {
1649c12995a5SJiri Olsa 		evlist__delete(*evlist);
1650657ee553SSong Liu 		*evlist = NULL;
1651657ee553SSong Liu 	}
1652657ee553SSong Liu 	return -1;
1653657ee553SSong Liu }
1654657ee553SSong Liu 
1655657ee553SSong Liu static void *perf_evlist__poll_thread(void *arg)
1656657ee553SSong Liu {
165763503dbaSJiri Olsa 	struct evlist *evlist = arg;
1658657ee553SSong Liu 	bool draining = false;
1659adc6257cSJiri Olsa 	int i, done = 0;
1660b397f846SArnaldo Carvalho de Melo 	/*
1661b397f846SArnaldo Carvalho de Melo 	 * In order to read symbols from other namespaces perf to needs to call
1662b397f846SArnaldo Carvalho de Melo 	 * setns(2).  This isn't permitted if the struct_fs has multiple users.
1663b397f846SArnaldo Carvalho de Melo 	 * unshare(2) the fs so that we may continue to setns into namespaces
1664b397f846SArnaldo Carvalho de Melo 	 * that we're observing when, for instance, reading the build-ids at
1665b397f846SArnaldo Carvalho de Melo 	 * the end of a 'perf record' session.
1666b397f846SArnaldo Carvalho de Melo 	 */
1667b397f846SArnaldo Carvalho de Melo 	unshare(CLONE_FS);
1668657ee553SSong Liu 
1669adc6257cSJiri Olsa 	while (!done) {
1670adc6257cSJiri Olsa 		bool got_data = false;
1671adc6257cSJiri Olsa 
1672adc6257cSJiri Olsa 		if (evlist->thread.done)
1673657ee553SSong Liu 			draining = true;
1674657ee553SSong Liu 
1675657ee553SSong Liu 		if (!draining)
167680ab2987SJiri Olsa 			evlist__poll(evlist, 1000);
1677657ee553SSong Liu 
1678c976ee11SJiri Olsa 		for (i = 0; i < evlist->core.nr_mmaps; i++) {
1679a5830532SJiri Olsa 			struct mmap *map = &evlist->mmap[i];
1680657ee553SSong Liu 			union perf_event *event;
1681657ee553SSong Liu 
16827c4d4182SJiri Olsa 			if (perf_mmap__read_init(&map->core))
1683657ee553SSong Liu 				continue;
1684151ed5d7SJiri Olsa 			while ((event = perf_mmap__read_event(&map->core)) != NULL) {
168532dcd021SJiri Olsa 				struct evsel *evsel = perf_evlist__event2evsel(evlist, event);
1686657ee553SSong Liu 
1687657ee553SSong Liu 				if (evsel && evsel->side_band.cb)
1688657ee553SSong Liu 					evsel->side_band.cb(event, evsel->side_band.data);
1689657ee553SSong Liu 				else
1690657ee553SSong Liu 					pr_warning("cannot locate proper evsel for the side band event\n");
1691657ee553SSong Liu 
16927728fa0cSJiri Olsa 				perf_mmap__consume(&map->core);
1693adc6257cSJiri Olsa 				got_data = true;
1694657ee553SSong Liu 			}
169532fdc2caSJiri Olsa 			perf_mmap__read_done(&map->core);
1696657ee553SSong Liu 		}
1697adc6257cSJiri Olsa 
1698adc6257cSJiri Olsa 		if (draining && !got_data)
1699adc6257cSJiri Olsa 			break;
1700657ee553SSong Liu 	}
1701657ee553SSong Liu 	return NULL;
1702657ee553SSong Liu }
1703657ee553SSong Liu 
170463503dbaSJiri Olsa int perf_evlist__start_sb_thread(struct evlist *evlist,
1705657ee553SSong Liu 				 struct target *target)
1706657ee553SSong Liu {
170732dcd021SJiri Olsa 	struct evsel *counter;
1708657ee553SSong Liu 
1709657ee553SSong Liu 	if (!evlist)
1710657ee553SSong Liu 		return 0;
1711657ee553SSong Liu 
1712657ee553SSong Liu 	if (perf_evlist__create_maps(evlist, target))
1713657ee553SSong Liu 		goto out_delete_evlist;
1714657ee553SSong Liu 
1715657ee553SSong Liu 	evlist__for_each_entry(evlist, counter) {
1716f72f901dSJiri Olsa 		if (evsel__open(counter, evlist->core.cpus,
171703617c22SJiri Olsa 				     evlist->core.threads) < 0)
1718657ee553SSong Liu 			goto out_delete_evlist;
1719657ee553SSong Liu 	}
1720657ee553SSong Liu 
17219521b5f2SJiri Olsa 	if (evlist__mmap(evlist, UINT_MAX))
1722657ee553SSong Liu 		goto out_delete_evlist;
1723657ee553SSong Liu 
1724657ee553SSong Liu 	evlist__for_each_entry(evlist, counter) {
1725ec7f24efSJiri Olsa 		if (evsel__enable(counter))
1726657ee553SSong Liu 			goto out_delete_evlist;
1727657ee553SSong Liu 	}
1728657ee553SSong Liu 
1729657ee553SSong Liu 	evlist->thread.done = 0;
1730657ee553SSong Liu 	if (pthread_create(&evlist->thread.th, NULL, perf_evlist__poll_thread, evlist))
1731657ee553SSong Liu 		goto out_delete_evlist;
1732657ee553SSong Liu 
1733657ee553SSong Liu 	return 0;
1734657ee553SSong Liu 
1735657ee553SSong Liu out_delete_evlist:
1736c12995a5SJiri Olsa 	evlist__delete(evlist);
1737657ee553SSong Liu 	evlist = NULL;
1738657ee553SSong Liu 	return -1;
1739657ee553SSong Liu }
1740657ee553SSong Liu 
174163503dbaSJiri Olsa void perf_evlist__stop_sb_thread(struct evlist *evlist)
1742657ee553SSong Liu {
1743657ee553SSong Liu 	if (!evlist)
1744657ee553SSong Liu 		return;
1745657ee553SSong Liu 	evlist->thread.done = 1;
1746657ee553SSong Liu 	pthread_join(evlist->thread.th, NULL);
1747c12995a5SJiri Olsa 	evlist__delete(evlist);
1748657ee553SSong Liu }
1749