xref: /openbmc/linux/tools/perf/util/evlist.c (revision 8cd36f3e)
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"
20fb71c86cSArnaldo Carvalho de Melo #include "util.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 {
5870db7533SArnaldo Carvalho de Melo 	int i;
5970db7533SArnaldo Carvalho de Melo 
6070db7533SArnaldo Carvalho de Melo 	for (i = 0; i < PERF_EVLIST__HLIST_SIZE; ++i)
6170db7533SArnaldo Carvalho de Melo 		INIT_HLIST_HEAD(&evlist->heads[i]);
624562a739SJiri Olsa 	perf_evlist__init(&evlist->core);
63453fa030SJiri Olsa 	perf_evlist__set_maps(&evlist->core, cpus, threads);
6440cb2d51SJiri Olsa 	fdarray__init(&evlist->core.pollfd, 64);
6535b9d88eSArnaldo Carvalho de Melo 	evlist->workload.pid = -1;
6654cc54deSWang Nan 	evlist->bkw_mmap_state = BKW_MMAP_NOTREADY;
67361c99a6SArnaldo Carvalho de Melo }
68361c99a6SArnaldo Carvalho de Melo 
690f98b11cSJiri Olsa struct evlist *evlist__new(void)
70ef1d1af2SArnaldo Carvalho de Melo {
7163503dbaSJiri Olsa 	struct evlist *evlist = zalloc(sizeof(*evlist));
72ef1d1af2SArnaldo Carvalho de Melo 
73ef1d1af2SArnaldo Carvalho de Melo 	if (evlist != NULL)
7452c86bcaSJiri Olsa 		evlist__init(evlist, NULL, NULL);
75ef1d1af2SArnaldo Carvalho de Melo 
76361c99a6SArnaldo Carvalho de Melo 	return evlist;
77361c99a6SArnaldo Carvalho de Melo }
78361c99a6SArnaldo Carvalho de Melo 
7963503dbaSJiri Olsa struct evlist *perf_evlist__new_default(void)
80b22d54b0SJiri Olsa {
810f98b11cSJiri Olsa 	struct evlist *evlist = evlist__new();
82b22d54b0SJiri Olsa 
83b22d54b0SJiri Olsa 	if (evlist && perf_evlist__add_default(evlist)) {
84c12995a5SJiri Olsa 		evlist__delete(evlist);
85b22d54b0SJiri Olsa 		evlist = NULL;
86b22d54b0SJiri Olsa 	}
87b22d54b0SJiri Olsa 
88b22d54b0SJiri Olsa 	return evlist;
89b22d54b0SJiri Olsa }
90b22d54b0SJiri Olsa 
9163503dbaSJiri Olsa struct evlist *perf_evlist__new_dummy(void)
925bae0250SArnaldo Carvalho de Melo {
930f98b11cSJiri Olsa 	struct evlist *evlist = evlist__new();
945bae0250SArnaldo Carvalho de Melo 
955bae0250SArnaldo Carvalho de Melo 	if (evlist && perf_evlist__add_dummy(evlist)) {
96c12995a5SJiri Olsa 		evlist__delete(evlist);
975bae0250SArnaldo Carvalho de Melo 		evlist = NULL;
985bae0250SArnaldo Carvalho de Melo 	}
995bae0250SArnaldo Carvalho de Melo 
1005bae0250SArnaldo Carvalho de Melo 	return evlist;
1015bae0250SArnaldo Carvalho de Melo }
1025bae0250SArnaldo Carvalho de Melo 
10375562573SAdrian Hunter /**
10475562573SAdrian Hunter  * perf_evlist__set_id_pos - set the positions of event ids.
10575562573SAdrian Hunter  * @evlist: selected event list
10675562573SAdrian Hunter  *
10775562573SAdrian Hunter  * Events with compatible sample types all have the same id_pos
10875562573SAdrian Hunter  * and is_pos.  For convenience, put a copy on evlist.
10975562573SAdrian Hunter  */
11063503dbaSJiri Olsa void perf_evlist__set_id_pos(struct evlist *evlist)
11175562573SAdrian Hunter {
11232dcd021SJiri Olsa 	struct evsel *first = perf_evlist__first(evlist);
11375562573SAdrian Hunter 
11475562573SAdrian Hunter 	evlist->id_pos = first->id_pos;
11575562573SAdrian Hunter 	evlist->is_pos = first->is_pos;
11675562573SAdrian Hunter }
11775562573SAdrian Hunter 
11863503dbaSJiri Olsa static void perf_evlist__update_id_pos(struct evlist *evlist)
119733cd2feSAdrian Hunter {
12032dcd021SJiri Olsa 	struct evsel *evsel;
121733cd2feSAdrian Hunter 
122e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, evsel)
123733cd2feSAdrian Hunter 		perf_evsel__calc_id_pos(evsel);
124733cd2feSAdrian Hunter 
125733cd2feSAdrian Hunter 	perf_evlist__set_id_pos(evlist);
126733cd2feSAdrian Hunter }
127733cd2feSAdrian Hunter 
128e6b1878dSJiri Olsa static void evlist__purge(struct evlist *evlist)
129361c99a6SArnaldo Carvalho de Melo {
13032dcd021SJiri Olsa 	struct evsel *pos, *n;
131361c99a6SArnaldo Carvalho de Melo 
132e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry_safe(evlist, n, pos) {
133b27c4eceSJiri Olsa 		list_del_init(&pos->core.node);
134d49e4695SArnaldo Carvalho de Melo 		pos->evlist = NULL;
1355eb2dd2aSJiri Olsa 		evsel__delete(pos);
136361c99a6SArnaldo Carvalho de Melo 	}
137361c99a6SArnaldo Carvalho de Melo 
1386484d2f9SJiri Olsa 	evlist->core.nr_entries = 0;
139361c99a6SArnaldo Carvalho de Melo }
140361c99a6SArnaldo Carvalho de Melo 
141470579b0SJiri Olsa void evlist__exit(struct evlist *evlist)
142ef1d1af2SArnaldo Carvalho de Melo {
14304662523SArnaldo Carvalho de Melo 	zfree(&evlist->mmap);
1440b72d69aSWang Nan 	zfree(&evlist->overwrite_mmap);
14540cb2d51SJiri Olsa 	fdarray__exit(&evlist->core.pollfd);
146ef1d1af2SArnaldo Carvalho de Melo }
147ef1d1af2SArnaldo Carvalho de Melo 
148c12995a5SJiri Olsa void evlist__delete(struct evlist *evlist)
149361c99a6SArnaldo Carvalho de Melo {
1500b04b3dcSArnaldo Carvalho de Melo 	if (evlist == NULL)
1510b04b3dcSArnaldo Carvalho de Melo 		return;
1520b04b3dcSArnaldo Carvalho de Melo 
153db6b7b13SJiri Olsa 	evlist__munmap(evlist);
154750b4edeSJiri Olsa 	evlist__close(evlist);
155f72f901dSJiri Olsa 	perf_cpu_map__put(evlist->core.cpus);
15603617c22SJiri Olsa 	perf_thread_map__put(evlist->core.threads);
157f72f901dSJiri Olsa 	evlist->core.cpus = NULL;
15803617c22SJiri Olsa 	evlist->core.threads = NULL;
159e6b1878dSJiri Olsa 	evlist__purge(evlist);
160470579b0SJiri Olsa 	evlist__exit(evlist);
161361c99a6SArnaldo Carvalho de Melo 	free(evlist);
162361c99a6SArnaldo Carvalho de Melo }
163361c99a6SArnaldo Carvalho de Melo 
164a1cf3a75SJiri Olsa void evlist__add(struct evlist *evlist, struct evsel *entry)
165361c99a6SArnaldo Carvalho de Melo {
166d49e4695SArnaldo Carvalho de Melo 	entry->evlist = evlist;
1676484d2f9SJiri Olsa 	entry->idx = evlist->core.nr_entries;
16860b0896cSAdrian Hunter 	entry->tracking = !entry->idx;
169ef503831SArnaldo Carvalho de Melo 
1706484d2f9SJiri Olsa 	perf_evlist__add(&evlist->core, &entry->core);
1716484d2f9SJiri Olsa 
1726484d2f9SJiri Olsa 	if (evlist->core.nr_entries == 1)
17375562573SAdrian Hunter 		perf_evlist__set_id_pos(evlist);
174361c99a6SArnaldo Carvalho de Melo }
175361c99a6SArnaldo Carvalho de Melo 
17616251027SJiri Olsa void evlist__remove(struct evlist *evlist, struct evsel *evsel)
1774768230aSAdrian Hunter {
1784768230aSAdrian Hunter 	evsel->evlist = NULL;
17952e22fb8SJiri Olsa 	perf_evlist__remove(&evlist->core, &evsel->core);
1804768230aSAdrian Hunter }
1814768230aSAdrian Hunter 
18263503dbaSJiri Olsa void perf_evlist__splice_list_tail(struct evlist *evlist,
183f114d6efSAdrian Hunter 				   struct list_head *list)
18450d08e47SArnaldo Carvalho de Melo {
18532dcd021SJiri Olsa 	struct evsel *evsel, *temp;
18675562573SAdrian Hunter 
187e5cadb93SArnaldo Carvalho de Melo 	__evlist__for_each_entry_safe(list, temp, evsel) {
188b27c4eceSJiri Olsa 		list_del_init(&evsel->core.node);
189a1cf3a75SJiri Olsa 		evlist__add(evlist, evsel);
190f114d6efSAdrian Hunter 	}
19150d08e47SArnaldo Carvalho de Melo }
19250d08e47SArnaldo Carvalho de Melo 
19363dab225SArnaldo Carvalho de Melo void __perf_evlist__set_leader(struct list_head *list)
19463dab225SArnaldo Carvalho de Melo {
19532dcd021SJiri Olsa 	struct evsel *evsel, *leader;
19663dab225SArnaldo Carvalho de Melo 
197b27c4eceSJiri Olsa 	leader = list_entry(list->next, struct evsel, core.node);
198b27c4eceSJiri Olsa 	evsel = list_entry(list->prev, struct evsel, core.node);
19997f63e4aSNamhyung Kim 
2005643b1a5SJiri Olsa 	leader->core.nr_members = evsel->idx - leader->idx + 1;
20163dab225SArnaldo Carvalho de Melo 
202e5cadb93SArnaldo Carvalho de Melo 	__evlist__for_each_entry(list, evsel) {
20363dab225SArnaldo Carvalho de Melo 		evsel->leader = leader;
20463dab225SArnaldo Carvalho de Melo 	}
20563dab225SArnaldo Carvalho de Melo }
20663dab225SArnaldo Carvalho de Melo 
20763503dbaSJiri Olsa void perf_evlist__set_leader(struct evlist *evlist)
2086a4bb04cSJiri Olsa {
2096484d2f9SJiri Olsa 	if (evlist->core.nr_entries) {
2106484d2f9SJiri Olsa 		evlist->nr_groups = evlist->core.nr_entries > 1 ? 1 : 0;
211ce9036a6SJiri Olsa 		__perf_evlist__set_leader(&evlist->core.entries);
2126a4bb04cSJiri Olsa 	}
21397f63e4aSNamhyung Kim }
2146a4bb04cSJiri Olsa 
21563503dbaSJiri Olsa int __perf_evlist__add_default(struct evlist *evlist, bool precise)
216361c99a6SArnaldo Carvalho de Melo {
21732dcd021SJiri Olsa 	struct evsel *evsel = perf_evsel__new_cycles(precise);
218361c99a6SArnaldo Carvalho de Melo 
219361c99a6SArnaldo Carvalho de Melo 	if (evsel == NULL)
2207c48dcfdSArnaldo Carvalho de Melo 		return -ENOMEM;
221361c99a6SArnaldo Carvalho de Melo 
222a1cf3a75SJiri Olsa 	evlist__add(evlist, evsel);
223361c99a6SArnaldo Carvalho de Melo 	return 0;
224361c99a6SArnaldo Carvalho de Melo }
2255c581041SArnaldo Carvalho de Melo 
22663503dbaSJiri Olsa int perf_evlist__add_dummy(struct evlist *evlist)
2275bae0250SArnaldo Carvalho de Melo {
2285bae0250SArnaldo Carvalho de Melo 	struct perf_event_attr attr = {
2295bae0250SArnaldo Carvalho de Melo 		.type	= PERF_TYPE_SOFTWARE,
2305bae0250SArnaldo Carvalho de Melo 		.config = PERF_COUNT_SW_DUMMY,
2315bae0250SArnaldo Carvalho de Melo 		.size	= sizeof(attr), /* to capture ABI version */
2325bae0250SArnaldo Carvalho de Melo 	};
2336484d2f9SJiri Olsa 	struct evsel *evsel = perf_evsel__new_idx(&attr, evlist->core.nr_entries);
2345bae0250SArnaldo Carvalho de Melo 
2355bae0250SArnaldo Carvalho de Melo 	if (evsel == NULL)
2365bae0250SArnaldo Carvalho de Melo 		return -ENOMEM;
2375bae0250SArnaldo Carvalho de Melo 
238a1cf3a75SJiri Olsa 	evlist__add(evlist, evsel);
2395bae0250SArnaldo Carvalho de Melo 	return 0;
2405bae0250SArnaldo Carvalho de Melo }
2415bae0250SArnaldo Carvalho de Melo 
242a1cf3a75SJiri Olsa static int evlist__add_attrs(struct evlist *evlist,
24350d08e47SArnaldo Carvalho de Melo 				  struct perf_event_attr *attrs, size_t nr_attrs)
24450d08e47SArnaldo Carvalho de Melo {
24532dcd021SJiri Olsa 	struct evsel *evsel, *n;
24650d08e47SArnaldo Carvalho de Melo 	LIST_HEAD(head);
24750d08e47SArnaldo Carvalho de Melo 	size_t i;
24850d08e47SArnaldo Carvalho de Melo 
24950d08e47SArnaldo Carvalho de Melo 	for (i = 0; i < nr_attrs; i++) {
2506484d2f9SJiri Olsa 		evsel = perf_evsel__new_idx(attrs + i, evlist->core.nr_entries + i);
25150d08e47SArnaldo Carvalho de Melo 		if (evsel == NULL)
25250d08e47SArnaldo Carvalho de Melo 			goto out_delete_partial_list;
253b27c4eceSJiri Olsa 		list_add_tail(&evsel->core.node, &head);
25450d08e47SArnaldo Carvalho de Melo 	}
25550d08e47SArnaldo Carvalho de Melo 
256f114d6efSAdrian Hunter 	perf_evlist__splice_list_tail(evlist, &head);
25750d08e47SArnaldo Carvalho de Melo 
25850d08e47SArnaldo Carvalho de Melo 	return 0;
25950d08e47SArnaldo Carvalho de Melo 
26050d08e47SArnaldo Carvalho de Melo out_delete_partial_list:
261e5cadb93SArnaldo Carvalho de Melo 	__evlist__for_each_entry_safe(&head, n, evsel)
2625eb2dd2aSJiri Olsa 		evsel__delete(evsel);
26350d08e47SArnaldo Carvalho de Melo 	return -1;
26450d08e47SArnaldo Carvalho de Melo }
26550d08e47SArnaldo Carvalho de Melo 
26663503dbaSJiri Olsa int __perf_evlist__add_default_attrs(struct evlist *evlist,
26779695e1bSArnaldo Carvalho de Melo 				     struct perf_event_attr *attrs, size_t nr_attrs)
26879695e1bSArnaldo Carvalho de Melo {
26979695e1bSArnaldo Carvalho de Melo 	size_t i;
27079695e1bSArnaldo Carvalho de Melo 
27179695e1bSArnaldo Carvalho de Melo 	for (i = 0; i < nr_attrs; i++)
27279695e1bSArnaldo Carvalho de Melo 		event_attr_init(attrs + i);
27379695e1bSArnaldo Carvalho de Melo 
274a1cf3a75SJiri Olsa 	return evlist__add_attrs(evlist, attrs, nr_attrs);
27579695e1bSArnaldo Carvalho de Melo }
27679695e1bSArnaldo Carvalho de Melo 
27732dcd021SJiri Olsa struct evsel *
27863503dbaSJiri Olsa perf_evlist__find_tracepoint_by_id(struct evlist *evlist, int id)
279ee29be62SArnaldo Carvalho de Melo {
28032dcd021SJiri Olsa 	struct evsel *evsel;
281ee29be62SArnaldo Carvalho de Melo 
282e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, evsel) {
2831fc632ceSJiri Olsa 		if (evsel->core.attr.type   == PERF_TYPE_TRACEPOINT &&
2841fc632ceSJiri Olsa 		    (int)evsel->core.attr.config == id)
285ee29be62SArnaldo Carvalho de Melo 			return evsel;
286ee29be62SArnaldo Carvalho de Melo 	}
287ee29be62SArnaldo Carvalho de Melo 
288ee29be62SArnaldo Carvalho de Melo 	return NULL;
289ee29be62SArnaldo Carvalho de Melo }
290ee29be62SArnaldo Carvalho de Melo 
29132dcd021SJiri Olsa struct evsel *
29263503dbaSJiri Olsa perf_evlist__find_tracepoint_by_name(struct evlist *evlist,
293a2f2804aSDavid Ahern 				     const char *name)
294a2f2804aSDavid Ahern {
29532dcd021SJiri Olsa 	struct evsel *evsel;
296a2f2804aSDavid Ahern 
297e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, evsel) {
2981fc632ceSJiri Olsa 		if ((evsel->core.attr.type == PERF_TYPE_TRACEPOINT) &&
299a2f2804aSDavid Ahern 		    (strcmp(evsel->name, name) == 0))
300a2f2804aSDavid Ahern 			return evsel;
301a2f2804aSDavid Ahern 	}
302a2f2804aSDavid Ahern 
303a2f2804aSDavid Ahern 	return NULL;
304a2f2804aSDavid Ahern }
305a2f2804aSDavid Ahern 
30663503dbaSJiri Olsa int perf_evlist__add_newtp(struct evlist *evlist,
30739876e7dSArnaldo Carvalho de Melo 			   const char *sys, const char *name, void *handler)
30839876e7dSArnaldo Carvalho de Melo {
30932dcd021SJiri Olsa 	struct evsel *evsel = perf_evsel__newtp(sys, name);
31039876e7dSArnaldo Carvalho de Melo 
3118dd2a131SJiri Olsa 	if (IS_ERR(evsel))
31239876e7dSArnaldo Carvalho de Melo 		return -1;
31339876e7dSArnaldo Carvalho de Melo 
314744a9719SArnaldo Carvalho de Melo 	evsel->handler = handler;
315a1cf3a75SJiri Olsa 	evlist__add(evlist, evsel);
31639876e7dSArnaldo Carvalho de Melo 	return 0;
31739876e7dSArnaldo Carvalho de Melo }
31839876e7dSArnaldo Carvalho de Melo 
31963503dbaSJiri Olsa static int perf_evlist__nr_threads(struct evlist *evlist,
32032dcd021SJiri Olsa 				   struct evsel *evsel)
321bf8e8f4bSAdrian Hunter {
322648b5af3SJiri Olsa 	if (evsel->core.system_wide)
323bf8e8f4bSAdrian Hunter 		return 1;
324bf8e8f4bSAdrian Hunter 	else
325a2f354e3SJiri Olsa 		return perf_thread_map__nr(evlist->core.threads);
326bf8e8f4bSAdrian Hunter }
327bf8e8f4bSAdrian Hunter 
328e74676deSJiri Olsa void evlist__disable(struct evlist *evlist)
3294152ab37SArnaldo Carvalho de Melo {
33032dcd021SJiri Olsa 	struct evsel *pos;
3314152ab37SArnaldo Carvalho de Melo 
332e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, pos) {
3339dfcb759SJiri Olsa 		if (pos->disabled || !perf_evsel__is_group_leader(pos) || !pos->core.fd)
3343fe4430dSJiri Olsa 			continue;
3359a10bb22SJiri Olsa 		evsel__disable(pos);
3364152ab37SArnaldo Carvalho de Melo 	}
3372b56bcfbSArnaldo Carvalho de Melo 
3382b56bcfbSArnaldo Carvalho de Melo 	evlist->enabled = false;
3394152ab37SArnaldo Carvalho de Melo }
3404152ab37SArnaldo Carvalho de Melo 
3411c87f165SJiri Olsa void evlist__enable(struct evlist *evlist)
342764e16a3SDavid Ahern {
34332dcd021SJiri Olsa 	struct evsel *pos;
344764e16a3SDavid Ahern 
345e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, pos) {
3469dfcb759SJiri Olsa 		if (!perf_evsel__is_group_leader(pos) || !pos->core.fd)
3473fe4430dSJiri Olsa 			continue;
348ec7f24efSJiri Olsa 		evsel__enable(pos);
349764e16a3SDavid Ahern 	}
3502b56bcfbSArnaldo Carvalho de Melo 
3512b56bcfbSArnaldo Carvalho de Melo 	evlist->enabled = true;
3522b56bcfbSArnaldo Carvalho de Melo }
3532b56bcfbSArnaldo Carvalho de Melo 
35463503dbaSJiri Olsa void perf_evlist__toggle_enable(struct evlist *evlist)
3552b56bcfbSArnaldo Carvalho de Melo {
356e74676deSJiri Olsa 	(evlist->enabled ? evlist__disable : evlist__enable)(evlist);
357764e16a3SDavid Ahern }
358764e16a3SDavid Ahern 
35963503dbaSJiri Olsa static int perf_evlist__enable_event_cpu(struct evlist *evlist,
36032dcd021SJiri Olsa 					 struct evsel *evsel, int cpu)
3611c65056cSAdrian Hunter {
36218ef15c6SArnaldo Carvalho de Melo 	int thread;
3631c65056cSAdrian Hunter 	int nr_threads = perf_evlist__nr_threads(evlist, evsel);
3641c65056cSAdrian Hunter 
3659dfcb759SJiri Olsa 	if (!evsel->core.fd)
3661c65056cSAdrian Hunter 		return -EINVAL;
3671c65056cSAdrian Hunter 
3681c65056cSAdrian Hunter 	for (thread = 0; thread < nr_threads; thread++) {
36918ef15c6SArnaldo Carvalho de Melo 		int err = ioctl(FD(evsel, cpu, thread), PERF_EVENT_IOC_ENABLE, 0);
3701c65056cSAdrian Hunter 		if (err)
3711c65056cSAdrian Hunter 			return err;
3721c65056cSAdrian Hunter 	}
3731c65056cSAdrian Hunter 	return 0;
3741c65056cSAdrian Hunter }
3751c65056cSAdrian Hunter 
37663503dbaSJiri Olsa static int perf_evlist__enable_event_thread(struct evlist *evlist,
37732dcd021SJiri Olsa 					    struct evsel *evsel,
3781c65056cSAdrian Hunter 					    int thread)
3791c65056cSAdrian Hunter {
38018ef15c6SArnaldo Carvalho de Melo 	int cpu;
3816549cd8fSJiri Olsa 	int nr_cpus = perf_cpu_map__nr(evlist->core.cpus);
3821c65056cSAdrian Hunter 
3839dfcb759SJiri Olsa 	if (!evsel->core.fd)
3841c65056cSAdrian Hunter 		return -EINVAL;
3851c65056cSAdrian Hunter 
3861c65056cSAdrian Hunter 	for (cpu = 0; cpu < nr_cpus; cpu++) {
38718ef15c6SArnaldo Carvalho de Melo 		int err = ioctl(FD(evsel, cpu, thread), PERF_EVENT_IOC_ENABLE, 0);
3881c65056cSAdrian Hunter 		if (err)
3891c65056cSAdrian Hunter 			return err;
3901c65056cSAdrian Hunter 	}
3911c65056cSAdrian Hunter 	return 0;
3921c65056cSAdrian Hunter }
3931c65056cSAdrian Hunter 
39463503dbaSJiri Olsa int perf_evlist__enable_event_idx(struct evlist *evlist,
39532dcd021SJiri Olsa 				  struct evsel *evsel, int idx)
3961c65056cSAdrian Hunter {
397315c0a1fSJiri Olsa 	bool per_cpu_mmaps = !perf_cpu_map__empty(evlist->core.cpus);
3981c65056cSAdrian Hunter 
3991c65056cSAdrian Hunter 	if (per_cpu_mmaps)
4001c65056cSAdrian Hunter 		return perf_evlist__enable_event_cpu(evlist, evsel, idx);
4011c65056cSAdrian Hunter 	else
4021c65056cSAdrian Hunter 		return perf_evlist__enable_event_thread(evlist, evsel, idx);
4031c65056cSAdrian Hunter }
4041c65056cSAdrian Hunter 
40563503dbaSJiri Olsa int perf_evlist__alloc_pollfd(struct evlist *evlist)
4065c581041SArnaldo Carvalho de Melo {
4076549cd8fSJiri Olsa 	int nr_cpus = perf_cpu_map__nr(evlist->core.cpus);
408a2f354e3SJiri Olsa 	int nr_threads = perf_thread_map__nr(evlist->core.threads);
409bf8e8f4bSAdrian Hunter 	int nfds = 0;
41032dcd021SJiri Olsa 	struct evsel *evsel;
411bf8e8f4bSAdrian Hunter 
412e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, evsel) {
413648b5af3SJiri Olsa 		if (evsel->core.system_wide)
414bf8e8f4bSAdrian Hunter 			nfds += nr_cpus;
415bf8e8f4bSAdrian Hunter 		else
416bf8e8f4bSAdrian Hunter 			nfds += nr_cpus * nr_threads;
417bf8e8f4bSAdrian Hunter 	}
418bf8e8f4bSAdrian Hunter 
41940cb2d51SJiri Olsa 	if (fdarray__available_entries(&evlist->core.pollfd) < nfds &&
42040cb2d51SJiri Olsa 	    fdarray__grow(&evlist->core.pollfd, nfds) < 0)
421ad6765ddSArnaldo Carvalho de Melo 		return -ENOMEM;
422ad6765ddSArnaldo Carvalho de Melo 
423ad6765ddSArnaldo Carvalho de Melo 	return 0;
4245c581041SArnaldo Carvalho de Melo }
42570082dd9SArnaldo Carvalho de Melo 
42663503dbaSJiri Olsa static int __perf_evlist__add_pollfd(struct evlist *evlist, int fd,
427a5830532SJiri Olsa 				     struct mmap *map, short revent)
428e4b356b5SArnaldo Carvalho de Melo {
42940cb2d51SJiri Olsa 	int pos = fdarray__add(&evlist->core.pollfd, fd, revent | POLLERR | POLLHUP);
430e4b356b5SArnaldo Carvalho de Melo 	/*
431e4b356b5SArnaldo Carvalho de Melo 	 * Save the idx so that when we filter out fds POLLHUP'ed we can
432e4b356b5SArnaldo Carvalho de Melo 	 * close the associated evlist->mmap[] entry.
433e4b356b5SArnaldo Carvalho de Melo 	 */
434e4b356b5SArnaldo Carvalho de Melo 	if (pos >= 0) {
43540cb2d51SJiri Olsa 		evlist->core.pollfd.priv[pos].ptr = map;
436e4b356b5SArnaldo Carvalho de Melo 
437e4b356b5SArnaldo Carvalho de Melo 		fcntl(fd, F_SETFL, O_NONBLOCK);
438e4b356b5SArnaldo Carvalho de Melo 	}
439e4b356b5SArnaldo Carvalho de Melo 
440e4b356b5SArnaldo Carvalho de Melo 	return pos;
441e4b356b5SArnaldo Carvalho de Melo }
442e4b356b5SArnaldo Carvalho de Melo 
44363503dbaSJiri Olsa int perf_evlist__add_pollfd(struct evlist *evlist, int fd)
44470082dd9SArnaldo Carvalho de Melo {
4454876075bSWang Nan 	return __perf_evlist__add_pollfd(evlist, fd, NULL, POLLIN);
446e4b356b5SArnaldo Carvalho de Melo }
4471b85337dSArnaldo Carvalho de Melo 
448258e4bfcSWang Nan static void perf_evlist__munmap_filtered(struct fdarray *fda, int fd,
449258e4bfcSWang Nan 					 void *arg __maybe_unused)
450e4b356b5SArnaldo Carvalho de Melo {
451a5830532SJiri Olsa 	struct mmap *map = fda->priv[fd].ptr;
452e4b356b5SArnaldo Carvalho de Melo 
4534876075bSWang Nan 	if (map)
4544876075bSWang Nan 		perf_mmap__put(map);
45570082dd9SArnaldo Carvalho de Melo }
45670db7533SArnaldo Carvalho de Melo 
45763503dbaSJiri Olsa int perf_evlist__filter_pollfd(struct evlist *evlist, short revents_and_mask)
4581ddec7f0SArnaldo Carvalho de Melo {
45940cb2d51SJiri Olsa 	return fdarray__filter(&evlist->core.pollfd, revents_and_mask,
460258e4bfcSWang Nan 			       perf_evlist__munmap_filtered, NULL);
4611ddec7f0SArnaldo Carvalho de Melo }
4621ddec7f0SArnaldo Carvalho de Melo 
46363503dbaSJiri Olsa int perf_evlist__poll(struct evlist *evlist, int timeout)
464f66a889dSArnaldo Carvalho de Melo {
46540cb2d51SJiri Olsa 	return fdarray__poll(&evlist->core.pollfd, timeout);
466f66a889dSArnaldo Carvalho de Melo }
467f66a889dSArnaldo Carvalho de Melo 
46863503dbaSJiri Olsa static void perf_evlist__id_hash(struct evlist *evlist,
46932dcd021SJiri Olsa 				 struct evsel *evsel,
4703d3b5e95SArnaldo Carvalho de Melo 				 int cpu, int thread, u64 id)
4713d3b5e95SArnaldo Carvalho de Melo {
4723d3b5e95SArnaldo Carvalho de Melo 	int hash;
4733d3b5e95SArnaldo Carvalho de Melo 	struct perf_sample_id *sid = SID(evsel, cpu, thread);
4743d3b5e95SArnaldo Carvalho de Melo 
4753d3b5e95SArnaldo Carvalho de Melo 	sid->id = id;
4763d3b5e95SArnaldo Carvalho de Melo 	sid->evsel = evsel;
4773d3b5e95SArnaldo Carvalho de Melo 	hash = hash_64(sid->id, PERF_EVLIST__HLIST_BITS);
4783d3b5e95SArnaldo Carvalho de Melo 	hlist_add_head(&sid->node, &evlist->heads[hash]);
4793d3b5e95SArnaldo Carvalho de Melo }
4803d3b5e95SArnaldo Carvalho de Melo 
48163503dbaSJiri Olsa void perf_evlist__id_add(struct evlist *evlist, struct evsel *evsel,
482a91e5431SArnaldo Carvalho de Melo 			 int cpu, int thread, u64 id)
483a91e5431SArnaldo Carvalho de Melo {
484a91e5431SArnaldo Carvalho de Melo 	perf_evlist__id_hash(evlist, evsel, cpu, thread, id);
485a91e5431SArnaldo Carvalho de Melo 	evsel->id[evsel->ids++] = id;
486a91e5431SArnaldo Carvalho de Melo }
487a91e5431SArnaldo Carvalho de Melo 
48863503dbaSJiri Olsa int perf_evlist__id_add_fd(struct evlist *evlist,
48932dcd021SJiri Olsa 			   struct evsel *evsel,
490f8a95309SArnaldo Carvalho de Melo 			   int cpu, int thread, int fd)
491f8a95309SArnaldo Carvalho de Melo {
492f8a95309SArnaldo Carvalho de Melo 	u64 read_data[4] = { 0, };
4933d3b5e95SArnaldo Carvalho de Melo 	int id_idx = 1; /* The first entry is the counter value */
494e2b5abe0SJiri Olsa 	u64 id;
495e2b5abe0SJiri Olsa 	int ret;
496e2b5abe0SJiri Olsa 
497e2b5abe0SJiri Olsa 	ret = ioctl(fd, PERF_EVENT_IOC_ID, &id);
498e2b5abe0SJiri Olsa 	if (!ret)
499e2b5abe0SJiri Olsa 		goto add;
500e2b5abe0SJiri Olsa 
501e2b5abe0SJiri Olsa 	if (errno != ENOTTY)
502e2b5abe0SJiri Olsa 		return -1;
503e2b5abe0SJiri Olsa 
504e2b5abe0SJiri Olsa 	/* Legacy way to get event id.. All hail to old kernels! */
505f8a95309SArnaldo Carvalho de Melo 
506c4861afeSJiri Olsa 	/*
507c4861afeSJiri Olsa 	 * This way does not work with group format read, so bail
508c4861afeSJiri Olsa 	 * out in that case.
509c4861afeSJiri Olsa 	 */
510c4861afeSJiri Olsa 	if (perf_evlist__read_format(evlist) & PERF_FORMAT_GROUP)
511c4861afeSJiri Olsa 		return -1;
512c4861afeSJiri Olsa 
5131fc632ceSJiri Olsa 	if (!(evsel->core.attr.read_format & PERF_FORMAT_ID) ||
514f8a95309SArnaldo Carvalho de Melo 	    read(fd, &read_data, sizeof(read_data)) == -1)
515f8a95309SArnaldo Carvalho de Melo 		return -1;
516f8a95309SArnaldo Carvalho de Melo 
5171fc632ceSJiri Olsa 	if (evsel->core.attr.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
518f8a95309SArnaldo Carvalho de Melo 		++id_idx;
5191fc632ceSJiri Olsa 	if (evsel->core.attr.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
520f8a95309SArnaldo Carvalho de Melo 		++id_idx;
521f8a95309SArnaldo Carvalho de Melo 
522e2b5abe0SJiri Olsa 	id = read_data[id_idx];
523e2b5abe0SJiri Olsa 
524e2b5abe0SJiri Olsa  add:
525e2b5abe0SJiri Olsa 	perf_evlist__id_add(evlist, evsel, cpu, thread, id);
526f8a95309SArnaldo Carvalho de Melo 	return 0;
527f8a95309SArnaldo Carvalho de Melo }
528f8a95309SArnaldo Carvalho de Melo 
52963503dbaSJiri Olsa static void perf_evlist__set_sid_idx(struct evlist *evlist,
53032dcd021SJiri Olsa 				     struct evsel *evsel, int idx, int cpu,
5313c659eedSAdrian Hunter 				     int thread)
5323c659eedSAdrian Hunter {
5333c659eedSAdrian Hunter 	struct perf_sample_id *sid = SID(evsel, cpu, thread);
5343c659eedSAdrian Hunter 	sid->idx = idx;
535f72f901dSJiri Olsa 	if (evlist->core.cpus && cpu >= 0)
536f72f901dSJiri Olsa 		sid->cpu = evlist->core.cpus->map[cpu];
5373c659eedSAdrian Hunter 	else
5383c659eedSAdrian Hunter 		sid->cpu = -1;
539648b5af3SJiri Olsa 	if (!evsel->core.system_wide && evlist->core.threads && thread >= 0)
540a2f354e3SJiri Olsa 		sid->tid = perf_thread_map__pid(evlist->core.threads, thread);
5413c659eedSAdrian Hunter 	else
5423c659eedSAdrian Hunter 		sid->tid = -1;
5433c659eedSAdrian Hunter }
5443c659eedSAdrian Hunter 
54563503dbaSJiri Olsa struct perf_sample_id *perf_evlist__id2sid(struct evlist *evlist, u64 id)
54670db7533SArnaldo Carvalho de Melo {
54770db7533SArnaldo Carvalho de Melo 	struct hlist_head *head;
54870db7533SArnaldo Carvalho de Melo 	struct perf_sample_id *sid;
54970db7533SArnaldo Carvalho de Melo 	int hash;
55070db7533SArnaldo Carvalho de Melo 
55170db7533SArnaldo Carvalho de Melo 	hash = hash_64(id, PERF_EVLIST__HLIST_BITS);
55270db7533SArnaldo Carvalho de Melo 	head = &evlist->heads[hash];
55370db7533SArnaldo Carvalho de Melo 
554b67bfe0dSSasha Levin 	hlist_for_each_entry(sid, head, node)
55570db7533SArnaldo Carvalho de Melo 		if (sid->id == id)
556932a3594SJiri Olsa 			return sid;
557932a3594SJiri Olsa 
558932a3594SJiri Olsa 	return NULL;
559932a3594SJiri Olsa }
560932a3594SJiri Olsa 
56163503dbaSJiri Olsa struct evsel *perf_evlist__id2evsel(struct evlist *evlist, u64 id)
562932a3594SJiri Olsa {
563932a3594SJiri Olsa 	struct perf_sample_id *sid;
564932a3594SJiri Olsa 
5656484d2f9SJiri Olsa 	if (evlist->core.nr_entries == 1 || !id)
566932a3594SJiri Olsa 		return perf_evlist__first(evlist);
567932a3594SJiri Olsa 
568932a3594SJiri Olsa 	sid = perf_evlist__id2sid(evlist, id);
569932a3594SJiri Olsa 	if (sid)
57070db7533SArnaldo Carvalho de Melo 		return sid->evsel;
57130e68bccSNamhyung Kim 
57230e68bccSNamhyung Kim 	if (!perf_evlist__sample_id_all(evlist))
5730c21f736SArnaldo Carvalho de Melo 		return perf_evlist__first(evlist);
57430e68bccSNamhyung Kim 
57570db7533SArnaldo Carvalho de Melo 	return NULL;
57670db7533SArnaldo Carvalho de Melo }
57704391debSArnaldo Carvalho de Melo 
57863503dbaSJiri Olsa struct evsel *perf_evlist__id2evsel_strict(struct evlist *evlist,
579dddcf6abSAdrian Hunter 						u64 id)
580dddcf6abSAdrian Hunter {
581dddcf6abSAdrian Hunter 	struct perf_sample_id *sid;
582dddcf6abSAdrian Hunter 
583dddcf6abSAdrian Hunter 	if (!id)
584dddcf6abSAdrian Hunter 		return NULL;
585dddcf6abSAdrian Hunter 
586dddcf6abSAdrian Hunter 	sid = perf_evlist__id2sid(evlist, id);
587dddcf6abSAdrian Hunter 	if (sid)
588dddcf6abSAdrian Hunter 		return sid->evsel;
589dddcf6abSAdrian Hunter 
590dddcf6abSAdrian Hunter 	return NULL;
591dddcf6abSAdrian Hunter }
592dddcf6abSAdrian Hunter 
59363503dbaSJiri Olsa static int perf_evlist__event2id(struct evlist *evlist,
59475562573SAdrian Hunter 				 union perf_event *event, u64 *id)
59575562573SAdrian Hunter {
596b1fcd190SJiri Olsa 	const __u64 *array = event->sample.array;
59775562573SAdrian Hunter 	ssize_t n;
59875562573SAdrian Hunter 
59975562573SAdrian Hunter 	n = (event->header.size - sizeof(event->header)) >> 3;
60075562573SAdrian Hunter 
60175562573SAdrian Hunter 	if (event->header.type == PERF_RECORD_SAMPLE) {
60275562573SAdrian Hunter 		if (evlist->id_pos >= n)
60375562573SAdrian Hunter 			return -1;
60475562573SAdrian Hunter 		*id = array[evlist->id_pos];
60575562573SAdrian Hunter 	} else {
60675562573SAdrian Hunter 		if (evlist->is_pos > n)
60775562573SAdrian Hunter 			return -1;
60875562573SAdrian Hunter 		n -= evlist->is_pos;
60975562573SAdrian Hunter 		*id = array[n];
61075562573SAdrian Hunter 	}
61175562573SAdrian Hunter 	return 0;
61275562573SAdrian Hunter }
61375562573SAdrian Hunter 
61463503dbaSJiri Olsa struct evsel *perf_evlist__event2evsel(struct evlist *evlist,
61575562573SAdrian Hunter 					    union perf_event *event)
61675562573SAdrian Hunter {
61732dcd021SJiri Olsa 	struct evsel *first = perf_evlist__first(evlist);
61875562573SAdrian Hunter 	struct hlist_head *head;
61975562573SAdrian Hunter 	struct perf_sample_id *sid;
62075562573SAdrian Hunter 	int hash;
62175562573SAdrian Hunter 	u64 id;
62275562573SAdrian Hunter 
6236484d2f9SJiri Olsa 	if (evlist->core.nr_entries == 1)
62498be6966SAdrian Hunter 		return first;
62598be6966SAdrian Hunter 
6261fc632ceSJiri Olsa 	if (!first->core.attr.sample_id_all &&
62798be6966SAdrian Hunter 	    event->header.type != PERF_RECORD_SAMPLE)
62898be6966SAdrian Hunter 		return first;
62975562573SAdrian Hunter 
63075562573SAdrian Hunter 	if (perf_evlist__event2id(evlist, event, &id))
63175562573SAdrian Hunter 		return NULL;
63275562573SAdrian Hunter 
63375562573SAdrian Hunter 	/* Synthesized events have an id of zero */
63475562573SAdrian Hunter 	if (!id)
63598be6966SAdrian Hunter 		return first;
63675562573SAdrian Hunter 
63775562573SAdrian Hunter 	hash = hash_64(id, PERF_EVLIST__HLIST_BITS);
63875562573SAdrian Hunter 	head = &evlist->heads[hash];
63975562573SAdrian Hunter 
64075562573SAdrian Hunter 	hlist_for_each_entry(sid, head, node) {
64175562573SAdrian Hunter 		if (sid->id == id)
64275562573SAdrian Hunter 			return sid->evsel;
64375562573SAdrian Hunter 	}
64475562573SAdrian Hunter 	return NULL;
64575562573SAdrian Hunter }
64675562573SAdrian Hunter 
64763503dbaSJiri Olsa static int perf_evlist__set_paused(struct evlist *evlist, bool value)
64865aea233SWang Nan {
64965aea233SWang Nan 	int i;
65065aea233SWang Nan 
6510b72d69aSWang Nan 	if (!evlist->overwrite_mmap)
652078c3386SWang Nan 		return 0;
653078c3386SWang Nan 
654c976ee11SJiri Olsa 	for (i = 0; i < evlist->core.nr_mmaps; i++) {
6552cf07b29SJiri Olsa 		int fd = evlist->overwrite_mmap[i].core.fd;
65665aea233SWang Nan 		int err;
65765aea233SWang Nan 
65865aea233SWang Nan 		if (fd < 0)
65965aea233SWang Nan 			continue;
66065aea233SWang Nan 		err = ioctl(fd, PERF_EVENT_IOC_PAUSE_OUTPUT, value ? 1 : 0);
66165aea233SWang Nan 		if (err)
66265aea233SWang Nan 			return err;
66365aea233SWang Nan 	}
66465aea233SWang Nan 	return 0;
66565aea233SWang Nan }
66665aea233SWang Nan 
66763503dbaSJiri Olsa static int perf_evlist__pause(struct evlist *evlist)
66865aea233SWang Nan {
66965aea233SWang Nan 	return perf_evlist__set_paused(evlist, true);
67065aea233SWang Nan }
67165aea233SWang Nan 
67263503dbaSJiri Olsa static int perf_evlist__resume(struct evlist *evlist)
67365aea233SWang Nan {
67465aea233SWang Nan 	return perf_evlist__set_paused(evlist, false);
67565aea233SWang Nan }
67665aea233SWang Nan 
677db6b7b13SJiri Olsa static void evlist__munmap_nofree(struct evlist *evlist)
678f8a95309SArnaldo Carvalho de Melo {
679aece948fSArnaldo Carvalho de Melo 	int i;
680f8a95309SArnaldo Carvalho de Melo 
681b2cb615dSWang Nan 	if (evlist->mmap)
682c976ee11SJiri Olsa 		for (i = 0; i < evlist->core.nr_mmaps; i++)
683a1f72618SWang Nan 			perf_mmap__munmap(&evlist->mmap[i]);
684b2cb615dSWang Nan 
6850b72d69aSWang Nan 	if (evlist->overwrite_mmap)
686c976ee11SJiri Olsa 		for (i = 0; i < evlist->core.nr_mmaps; i++)
6870b72d69aSWang Nan 			perf_mmap__munmap(&evlist->overwrite_mmap[i]);
688a1f72618SWang Nan }
689aece948fSArnaldo Carvalho de Melo 
690db6b7b13SJiri Olsa void evlist__munmap(struct evlist *evlist)
691a1f72618SWang Nan {
692db6b7b13SJiri Olsa 	evlist__munmap_nofree(evlist);
69304662523SArnaldo Carvalho de Melo 	zfree(&evlist->mmap);
6940b72d69aSWang Nan 	zfree(&evlist->overwrite_mmap);
695f8a95309SArnaldo Carvalho de Melo }
696f8a95309SArnaldo Carvalho de Melo 
697d50cf361SJiri Olsa static struct mmap *evlist__alloc_mmap(struct evlist *evlist,
6982c5f6d87SKan Liang 				       bool overwrite)
699f8a95309SArnaldo Carvalho de Melo {
700d4c6fb36SWang Nan 	int i;
701a5830532SJiri Olsa 	struct mmap *map;
702d4c6fb36SWang Nan 
703c976ee11SJiri Olsa 	evlist->core.nr_mmaps = perf_cpu_map__nr(evlist->core.cpus);
704315c0a1fSJiri Olsa 	if (perf_cpu_map__empty(evlist->core.cpus))
705c976ee11SJiri Olsa 		evlist->core.nr_mmaps = perf_thread_map__nr(evlist->core.threads);
706c976ee11SJiri Olsa 	map = zalloc(evlist->core.nr_mmaps * sizeof(struct mmap));
7078db6d6b1SWang Nan 	if (!map)
7088db6d6b1SWang Nan 		return NULL;
709946ae1d4SWang Nan 
710c976ee11SJiri Olsa 	for (i = 0; i < evlist->core.nr_mmaps; i++) {
7112cf07b29SJiri Olsa 		map[i].core.fd = -1;
7128df7a869SJiri Olsa 		map[i].core.overwrite = overwrite;
7134738ca30SArnaldo Carvalho de Melo 		/*
7144738ca30SArnaldo Carvalho de Melo 		 * When the perf_mmap() call is made we grab one refcount, plus
7156afad54dSKan Liang 		 * one extra to let perf_mmap__consume() get the last
7164738ca30SArnaldo Carvalho de Melo 		 * events after all real references (perf_mmap__get()) are
7174738ca30SArnaldo Carvalho de Melo 		 * dropped.
7184738ca30SArnaldo Carvalho de Melo 		 *
7194738ca30SArnaldo Carvalho de Melo 		 * Each PERF_EVENT_IOC_SET_OUTPUT points to this mmap and
7204738ca30SArnaldo Carvalho de Melo 		 * thus does perf_mmap__get() on it.
7214738ca30SArnaldo Carvalho de Melo 		 */
722e03edfeaSJiri Olsa 		refcount_set(&map[i].core.refcnt, 0);
7234738ca30SArnaldo Carvalho de Melo 	}
7248db6d6b1SWang Nan 	return map;
725f8a95309SArnaldo Carvalho de Melo }
726f8a95309SArnaldo Carvalho de Melo 
727f3058a1cSWang Nan static bool
72863503dbaSJiri Olsa perf_evlist__should_poll(struct evlist *evlist __maybe_unused,
72932dcd021SJiri Olsa 			 struct evsel *evsel)
730f3058a1cSWang Nan {
7311fc632ceSJiri Olsa 	if (evsel->core.attr.write_backward)
732f3058a1cSWang Nan 		return false;
733f3058a1cSWang Nan 	return true;
734f3058a1cSWang Nan }
735f3058a1cSWang Nan 
7369521b5f2SJiri Olsa static int evlist__mmap_per_evsel(struct evlist *evlist, int idx,
7379f21b815SMark Rutland 				       struct mmap_params *mp, int cpu_idx,
7380b72d69aSWang Nan 				       int thread, int *_output, int *_output_overwrite)
739aece948fSArnaldo Carvalho de Melo {
74032dcd021SJiri Olsa 	struct evsel *evsel;
741f3058a1cSWang Nan 	int revent;
742f72f901dSJiri Olsa 	int evlist_cpu = cpu_map__cpu(evlist->core.cpus, cpu_idx);
74304e21314SAdrian Hunter 
744e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, evsel) {
745a5830532SJiri Olsa 		struct mmap *maps = evlist->mmap;
746078c3386SWang Nan 		int *output = _output;
747bf8e8f4bSAdrian Hunter 		int fd;
7489f21b815SMark Rutland 		int cpu;
749bf8e8f4bSAdrian Hunter 
75071f566a3SWang Nan 		mp->prot = PROT_READ | PROT_WRITE;
7511fc632ceSJiri Olsa 		if (evsel->core.attr.write_backward) {
7520b72d69aSWang Nan 			output = _output_overwrite;
7530b72d69aSWang Nan 			maps = evlist->overwrite_mmap;
754078c3386SWang Nan 
755078c3386SWang Nan 			if (!maps) {
756d50cf361SJiri Olsa 				maps = evlist__alloc_mmap(evlist, true);
757078c3386SWang Nan 				if (!maps)
758078c3386SWang Nan 					return -1;
7590b72d69aSWang Nan 				evlist->overwrite_mmap = maps;
76054cc54deSWang Nan 				if (evlist->bkw_mmap_state == BKW_MMAP_NOTREADY)
76154cc54deSWang Nan 					perf_evlist__toggle_bkw_mmap(evlist, BKW_MMAP_RUNNING);
762078c3386SWang Nan 			}
76371f566a3SWang Nan 			mp->prot &= ~PROT_WRITE;
764078c3386SWang Nan 		}
765f3058a1cSWang Nan 
766648b5af3SJiri Olsa 		if (evsel->core.system_wide && thread)
767bf8e8f4bSAdrian Hunter 			continue;
768bf8e8f4bSAdrian Hunter 
769b4df75deSJiri Olsa 		cpu = perf_cpu_map__idx(evsel->core.cpus, evlist_cpu);
7709f21b815SMark Rutland 		if (cpu == -1)
7719f21b815SMark Rutland 			continue;
7729f21b815SMark Rutland 
773bf8e8f4bSAdrian Hunter 		fd = FD(evsel, cpu, thread);
77404e21314SAdrian Hunter 
77504e21314SAdrian Hunter 		if (*output == -1) {
77604e21314SAdrian Hunter 			*output = fd;
777078c3386SWang Nan 
77831fb4c0dSJiri Olsa 			if (perf_mmap__mmap(&maps[idx], mp, *output, evlist_cpu) < 0)
77904e21314SAdrian Hunter 				return -1;
78004e21314SAdrian Hunter 		} else {
78104e21314SAdrian Hunter 			if (ioctl(fd, PERF_EVENT_IOC_SET_OUTPUT, *output) != 0)
78204e21314SAdrian Hunter 				return -1;
78382396986SArnaldo Carvalho de Melo 
784078c3386SWang Nan 			perf_mmap__get(&maps[idx]);
78504e21314SAdrian Hunter 		}
78604e21314SAdrian Hunter 
787f3058a1cSWang Nan 		revent = perf_evlist__should_poll(evlist, evsel) ? POLLIN : 0;
788f3058a1cSWang Nan 
789f90d194aSAdrian Hunter 		/*
790f90d194aSAdrian Hunter 		 * The system_wide flag causes a selected event to be opened
791f90d194aSAdrian Hunter 		 * always without a pid.  Consequently it will never get a
792f90d194aSAdrian Hunter 		 * POLLHUP, but it is used for tracking in combination with
793f90d194aSAdrian Hunter 		 * other events, so it should not need to be polled anyway.
794f90d194aSAdrian Hunter 		 * Therefore don't add it for polling.
795f90d194aSAdrian Hunter 		 */
796648b5af3SJiri Olsa 		if (!evsel->core.system_wide &&
797078c3386SWang Nan 		    __perf_evlist__add_pollfd(evlist, fd, &maps[idx], revent) < 0) {
798078c3386SWang Nan 			perf_mmap__put(&maps[idx]);
799ad6765ddSArnaldo Carvalho de Melo 			return -1;
80082396986SArnaldo Carvalho de Melo 		}
801033fa713SArnaldo Carvalho de Melo 
8021fc632ceSJiri Olsa 		if (evsel->core.attr.read_format & PERF_FORMAT_ID) {
8033c659eedSAdrian Hunter 			if (perf_evlist__id_add_fd(evlist, evsel, cpu, thread,
8043c659eedSAdrian Hunter 						   fd) < 0)
80504e21314SAdrian Hunter 				return -1;
8063c659eedSAdrian Hunter 			perf_evlist__set_sid_idx(evlist, evsel, idx, cpu,
8073c659eedSAdrian Hunter 						 thread);
8083c659eedSAdrian Hunter 		}
80904e21314SAdrian Hunter 	}
81004e21314SAdrian Hunter 
81104e21314SAdrian Hunter 	return 0;
81204e21314SAdrian Hunter }
81304e21314SAdrian Hunter 
8149521b5f2SJiri Olsa static int evlist__mmap_per_cpu(struct evlist *evlist,
815a8a8f3ebSAdrian Hunter 				     struct mmap_params *mp)
81604e21314SAdrian Hunter {
817aece948fSArnaldo Carvalho de Melo 	int cpu, thread;
8186549cd8fSJiri Olsa 	int nr_cpus = perf_cpu_map__nr(evlist->core.cpus);
819a2f354e3SJiri Olsa 	int nr_threads = perf_thread_map__nr(evlist->core.threads);
820aece948fSArnaldo Carvalho de Melo 
821e3e1a54fSAdrian Hunter 	pr_debug2("perf event ring buffer mmapped per cpu\n");
822b3a319d5SNamhyung Kim 	for (cpu = 0; cpu < nr_cpus; cpu++) {
823aece948fSArnaldo Carvalho de Melo 		int output = -1;
8240b72d69aSWang Nan 		int output_overwrite = -1;
825aece948fSArnaldo Carvalho de Melo 
826718c602dSAdrian Hunter 		auxtrace_mmap_params__set_idx(&mp->auxtrace_mp, evlist, cpu,
827718c602dSAdrian Hunter 					      true);
828718c602dSAdrian Hunter 
829b3a319d5SNamhyung Kim 		for (thread = 0; thread < nr_threads; thread++) {
8309521b5f2SJiri Olsa 			if (evlist__mmap_per_evsel(evlist, cpu, mp, cpu,
8310b72d69aSWang Nan 							thread, &output, &output_overwrite))
832aece948fSArnaldo Carvalho de Melo 				goto out_unmap;
833aece948fSArnaldo Carvalho de Melo 		}
834aece948fSArnaldo Carvalho de Melo 	}
835aece948fSArnaldo Carvalho de Melo 
836aece948fSArnaldo Carvalho de Melo 	return 0;
837aece948fSArnaldo Carvalho de Melo 
838aece948fSArnaldo Carvalho de Melo out_unmap:
839db6b7b13SJiri Olsa 	evlist__munmap_nofree(evlist);
840aece948fSArnaldo Carvalho de Melo 	return -1;
841aece948fSArnaldo Carvalho de Melo }
842aece948fSArnaldo Carvalho de Melo 
8439521b5f2SJiri Olsa static int evlist__mmap_per_thread(struct evlist *evlist,
844a8a8f3ebSAdrian Hunter 					struct mmap_params *mp)
845aece948fSArnaldo Carvalho de Melo {
846aece948fSArnaldo Carvalho de Melo 	int thread;
847a2f354e3SJiri Olsa 	int nr_threads = perf_thread_map__nr(evlist->core.threads);
848aece948fSArnaldo Carvalho de Melo 
849e3e1a54fSAdrian Hunter 	pr_debug2("perf event ring buffer mmapped per thread\n");
850b3a319d5SNamhyung Kim 	for (thread = 0; thread < nr_threads; thread++) {
851aece948fSArnaldo Carvalho de Melo 		int output = -1;
8520b72d69aSWang Nan 		int output_overwrite = -1;
853aece948fSArnaldo Carvalho de Melo 
854718c602dSAdrian Hunter 		auxtrace_mmap_params__set_idx(&mp->auxtrace_mp, evlist, thread,
855718c602dSAdrian Hunter 					      false);
856718c602dSAdrian Hunter 
8579521b5f2SJiri Olsa 		if (evlist__mmap_per_evsel(evlist, thread, mp, 0, thread,
8580b72d69aSWang Nan 						&output, &output_overwrite))
859aece948fSArnaldo Carvalho de Melo 			goto out_unmap;
860aece948fSArnaldo Carvalho de Melo 	}
861aece948fSArnaldo Carvalho de Melo 
862aece948fSArnaldo Carvalho de Melo 	return 0;
863aece948fSArnaldo Carvalho de Melo 
864aece948fSArnaldo Carvalho de Melo out_unmap:
865db6b7b13SJiri Olsa 	evlist__munmap_nofree(evlist);
866aece948fSArnaldo Carvalho de Melo 	return -1;
867aece948fSArnaldo Carvalho de Melo }
868aece948fSArnaldo Carvalho de Melo 
869f5e7150cSArnaldo Carvalho de Melo unsigned long perf_event_mlock_kb_in_pages(void)
870994a1f78SJiri Olsa {
871f5e7150cSArnaldo Carvalho de Melo 	unsigned long pages;
8728185e881SArnaldo Carvalho de Melo 	int max;
8738185e881SArnaldo Carvalho de Melo 
8748185e881SArnaldo Carvalho de Melo 	if (sysctl__read_int("kernel/perf_event_mlock_kb", &max) < 0) {
8758185e881SArnaldo Carvalho de Melo 		/*
8768185e881SArnaldo Carvalho de Melo 		 * Pick a once upon a time good value, i.e. things look
8778185e881SArnaldo Carvalho de Melo 		 * strange since we can't read a sysctl value, but lets not
8788185e881SArnaldo Carvalho de Melo 		 * die yet...
8798185e881SArnaldo Carvalho de Melo 		 */
8808185e881SArnaldo Carvalho de Melo 		max = 512;
8818185e881SArnaldo Carvalho de Melo 	} else {
8828185e881SArnaldo Carvalho de Melo 		max -= (page_size / 1024);
8838185e881SArnaldo Carvalho de Melo 	}
8848185e881SArnaldo Carvalho de Melo 
8858185e881SArnaldo Carvalho de Melo 	pages = (max * 1024) / page_size;
8861be300f4SArnaldo Carvalho de Melo 	if (!is_power_of_2(pages))
8871be300f4SArnaldo Carvalho de Melo 		pages = rounddown_pow_of_two(pages);
888f5e7150cSArnaldo Carvalho de Melo 
889f5e7150cSArnaldo Carvalho de Melo 	return pages;
890f5e7150cSArnaldo Carvalho de Melo }
891f5e7150cSArnaldo Carvalho de Melo 
8929521b5f2SJiri Olsa size_t evlist__mmap_size(unsigned long pages)
893f5e7150cSArnaldo Carvalho de Melo {
894f5e7150cSArnaldo Carvalho de Melo 	if (pages == UINT_MAX)
895f5e7150cSArnaldo Carvalho de Melo 		pages = perf_event_mlock_kb_in_pages();
896f5e7150cSArnaldo Carvalho de Melo 	else if (!is_power_of_2(pages))
897994a1f78SJiri Olsa 		return 0;
898994a1f78SJiri Olsa 
899994a1f78SJiri Olsa 	return (pages + 1) * page_size;
900994a1f78SJiri Olsa }
901994a1f78SJiri Olsa 
90233c2dcfdSDavid Ahern static long parse_pages_arg(const char *str, unsigned long min,
90333c2dcfdSDavid Ahern 			    unsigned long max)
904994a1f78SJiri Olsa {
9052fbe4abeSAdrian Hunter 	unsigned long pages, val;
90627050f53SJiri Olsa 	static struct parse_tag tags[] = {
90727050f53SJiri Olsa 		{ .tag  = 'B', .mult = 1       },
90827050f53SJiri Olsa 		{ .tag  = 'K', .mult = 1 << 10 },
90927050f53SJiri Olsa 		{ .tag  = 'M', .mult = 1 << 20 },
91027050f53SJiri Olsa 		{ .tag  = 'G', .mult = 1 << 30 },
91127050f53SJiri Olsa 		{ .tag  = 0 },
91227050f53SJiri Olsa 	};
913994a1f78SJiri Olsa 
9148973504bSDavid Ahern 	if (str == NULL)
91533c2dcfdSDavid Ahern 		return -EINVAL;
9168973504bSDavid Ahern 
91727050f53SJiri Olsa 	val = parse_tag_value(str, tags);
9182fbe4abeSAdrian Hunter 	if (val != (unsigned long) -1) {
91927050f53SJiri Olsa 		/* we got file size value */
92027050f53SJiri Olsa 		pages = PERF_ALIGN(val, page_size) / page_size;
92127050f53SJiri Olsa 	} else {
92227050f53SJiri Olsa 		/* we got pages count value */
92327050f53SJiri Olsa 		char *eptr;
924994a1f78SJiri Olsa 		pages = strtoul(str, &eptr, 10);
92533c2dcfdSDavid Ahern 		if (*eptr != '\0')
92633c2dcfdSDavid Ahern 			return -EINVAL;
92727050f53SJiri Olsa 	}
928994a1f78SJiri Olsa 
9292bcab6c1SAdrian Hunter 	if (pages == 0 && min == 0) {
93033c2dcfdSDavid Ahern 		/* leave number of pages at 0 */
9311dbfa938SAdrian Hunter 	} else if (!is_power_of_2(pages)) {
9329808143bSJiri Olsa 		char buf[100];
9339808143bSJiri Olsa 
93433c2dcfdSDavid Ahern 		/* round pages up to next power of 2 */
93591529834SArnaldo Carvalho de Melo 		pages = roundup_pow_of_two(pages);
9361dbfa938SAdrian Hunter 		if (!pages)
9371dbfa938SAdrian Hunter 			return -EINVAL;
9389808143bSJiri Olsa 
9399808143bSJiri Olsa 		unit_number__scnprintf(buf, sizeof(buf), pages * page_size);
9409808143bSJiri Olsa 		pr_info("rounding mmap pages size to %s (%lu pages)\n",
9419808143bSJiri Olsa 			buf, pages);
9422fbe4abeSAdrian Hunter 	}
9432fbe4abeSAdrian Hunter 
94433c2dcfdSDavid Ahern 	if (pages > max)
94533c2dcfdSDavid Ahern 		return -EINVAL;
94633c2dcfdSDavid Ahern 
94733c2dcfdSDavid Ahern 	return pages;
94833c2dcfdSDavid Ahern }
94933c2dcfdSDavid Ahern 
950e9db1310SAdrian Hunter int __perf_evlist__parse_mmap_pages(unsigned int *mmap_pages, const char *str)
95133c2dcfdSDavid Ahern {
95233c2dcfdSDavid Ahern 	unsigned long max = UINT_MAX;
95333c2dcfdSDavid Ahern 	long pages;
95433c2dcfdSDavid Ahern 
955f5ae9c42SAdrian Hunter 	if (max > SIZE_MAX / page_size)
95633c2dcfdSDavid Ahern 		max = SIZE_MAX / page_size;
95733c2dcfdSDavid Ahern 
95833c2dcfdSDavid Ahern 	pages = parse_pages_arg(str, 1, max);
95933c2dcfdSDavid Ahern 	if (pages < 0) {
96033c2dcfdSDavid Ahern 		pr_err("Invalid argument for --mmap_pages/-m\n");
961994a1f78SJiri Olsa 		return -1;
962994a1f78SJiri Olsa 	}
963994a1f78SJiri Olsa 
964994a1f78SJiri Olsa 	*mmap_pages = pages;
965994a1f78SJiri Olsa 	return 0;
966994a1f78SJiri Olsa }
967994a1f78SJiri Olsa 
968e9db1310SAdrian Hunter int perf_evlist__parse_mmap_pages(const struct option *opt, const char *str,
969e9db1310SAdrian Hunter 				  int unset __maybe_unused)
970e9db1310SAdrian Hunter {
971e9db1310SAdrian Hunter 	return __perf_evlist__parse_mmap_pages(opt->value, str);
972e9db1310SAdrian Hunter }
973e9db1310SAdrian Hunter 
974c83fa7f2SAdrian Hunter /**
9759521b5f2SJiri Olsa  * evlist__mmap_ex - Create mmaps to receive events.
976c83fa7f2SAdrian Hunter  * @evlist: list of events
977c83fa7f2SAdrian Hunter  * @pages: map length in pages
978c83fa7f2SAdrian Hunter  * @overwrite: overwrite older events?
979718c602dSAdrian Hunter  * @auxtrace_pages - auxtrace map length in pages
980718c602dSAdrian Hunter  * @auxtrace_overwrite - overwrite older auxtrace data?
981f8a95309SArnaldo Carvalho de Melo  *
982c83fa7f2SAdrian Hunter  * If @overwrite is %false the user needs to signal event consumption using
9839521b5f2SJiri Olsa  * perf_mmap__write_tail().  Using evlist__mmap_read() does this
984c83fa7f2SAdrian Hunter  * automatically.
985f8a95309SArnaldo Carvalho de Melo  *
986718c602dSAdrian Hunter  * Similarly, if @auxtrace_overwrite is %false the user needs to signal data
987718c602dSAdrian Hunter  * consumption using auxtrace_mmap__write_tail().
988718c602dSAdrian Hunter  *
989c83fa7f2SAdrian Hunter  * Return: %0 on success, negative error code otherwise.
990f8a95309SArnaldo Carvalho de Melo  */
9919521b5f2SJiri Olsa int evlist__mmap_ex(struct evlist *evlist, unsigned int pages,
9927a276ff6SWang Nan 			 unsigned int auxtrace_pages,
99351255a8aSAlexey Budankov 			 bool auxtrace_overwrite, int nr_cblocks, int affinity, int flush,
99451255a8aSAlexey Budankov 			 int comp_level)
995f8a95309SArnaldo Carvalho de Melo {
99632dcd021SJiri Olsa 	struct evsel *evsel;
997f72f901dSJiri Olsa 	const struct perf_cpu_map *cpus = evlist->core.cpus;
99803617c22SJiri Olsa 	const struct perf_thread_map *threads = evlist->core.threads;
99971f566a3SWang Nan 	/*
100071f566a3SWang Nan 	 * Delay setting mp.prot: set it before calling perf_mmap__mmap.
100171f566a3SWang Nan 	 * Its value is decided by evsel's write_backward.
100271f566a3SWang Nan 	 * So &mp should not be passed through const pointer.
100371f566a3SWang Nan 	 */
100451255a8aSAlexey Budankov 	struct mmap_params mp = { .nr_cblocks = nr_cblocks, .affinity = affinity, .flush = flush,
100551255a8aSAlexey Budankov 				  .comp_level = comp_level };
100650a682ceSArnaldo Carvalho de Melo 
10078db6d6b1SWang Nan 	if (!evlist->mmap)
1008d50cf361SJiri Olsa 		evlist->mmap = evlist__alloc_mmap(evlist, false);
10098db6d6b1SWang Nan 	if (!evlist->mmap)
1010f8a95309SArnaldo Carvalho de Melo 		return -ENOMEM;
1011f8a95309SArnaldo Carvalho de Melo 
101240cb2d51SJiri Olsa 	if (evlist->core.pollfd.entries == NULL && perf_evlist__alloc_pollfd(evlist) < 0)
1013f8a95309SArnaldo Carvalho de Melo 		return -ENOMEM;
1014f8a95309SArnaldo Carvalho de Melo 
1015f6fa4375SJiri Olsa 	evlist->core.mmap_len = evlist__mmap_size(pages);
1016f6fa4375SJiri Olsa 	pr_debug("mmap size %zuB\n", evlist->core.mmap_len);
1017f6fa4375SJiri Olsa 	mp.mask = evlist->core.mmap_len - page_size - 1;
1018f8a95309SArnaldo Carvalho de Melo 
1019f6fa4375SJiri Olsa 	auxtrace_mmap_params__init(&mp.auxtrace_mp, evlist->core.mmap_len,
1020718c602dSAdrian Hunter 				   auxtrace_pages, auxtrace_overwrite);
1021718c602dSAdrian Hunter 
1022e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, evsel) {
10231fc632ceSJiri Olsa 		if ((evsel->core.attr.read_format & PERF_FORMAT_ID) &&
10248cd36f3eSJiri Olsa 		    evsel->core.sample_id == NULL &&
10256549cd8fSJiri Olsa 		    perf_evsel__alloc_id(evsel, perf_cpu_map__nr(cpus), threads->nr) < 0)
1026f8a95309SArnaldo Carvalho de Melo 			return -ENOMEM;
1027f8a95309SArnaldo Carvalho de Melo 	}
1028f8a95309SArnaldo Carvalho de Melo 
1029315c0a1fSJiri Olsa 	if (perf_cpu_map__empty(cpus))
10309521b5f2SJiri Olsa 		return evlist__mmap_per_thread(evlist, &mp);
1031f8a95309SArnaldo Carvalho de Melo 
10329521b5f2SJiri Olsa 	return evlist__mmap_per_cpu(evlist, &mp);
1033f8a95309SArnaldo Carvalho de Melo }
10347e2ed097SArnaldo Carvalho de Melo 
10359521b5f2SJiri Olsa int evlist__mmap(struct evlist *evlist, unsigned int pages)
1036718c602dSAdrian Hunter {
10379521b5f2SJiri Olsa 	return evlist__mmap_ex(evlist, pages, 0, false, 0, PERF_AFFINITY_SYS, 1, 0);
1038718c602dSAdrian Hunter }
1039718c602dSAdrian Hunter 
104063503dbaSJiri Olsa int perf_evlist__create_maps(struct evlist *evlist, struct target *target)
10417e2ed097SArnaldo Carvalho de Melo {
1042147c508fSJin Yao 	bool all_threads = (target->per_thread && target->system_wide);
1043f854839bSJiri Olsa 	struct perf_cpu_map *cpus;
10449749b90eSJiri Olsa 	struct perf_thread_map *threads;
10457e2ed097SArnaldo Carvalho de Melo 
1046147c508fSJin Yao 	/*
1047147c508fSJin Yao 	 * If specify '-a' and '--per-thread' to perf record, perf record
1048147c508fSJin Yao 	 * will override '--per-thread'. target->per_thread = false and
1049147c508fSJin Yao 	 * target->system_wide = true.
1050147c508fSJin Yao 	 *
1051147c508fSJin Yao 	 * If specify '--per-thread' only to perf record,
1052147c508fSJin Yao 	 * target->per_thread = true and target->system_wide = false.
1053147c508fSJin Yao 	 *
1054147c508fSJin Yao 	 * So target->per_thread && target->system_wide is false.
1055147c508fSJin Yao 	 * For perf record, thread_map__new_str doesn't call
1056147c508fSJin Yao 	 * thread_map__new_all_cpus. That will keep perf record's
1057147c508fSJin Yao 	 * current behavior.
1058147c508fSJin Yao 	 *
1059147c508fSJin Yao 	 * For perf stat, it allows the case that target->per_thread and
1060147c508fSJin Yao 	 * target->system_wide are all true. It means to collect system-wide
1061147c508fSJin Yao 	 * per-thread data. thread_map__new_str will call
1062147c508fSJin Yao 	 * thread_map__new_all_cpus to enumerate all threads.
1063147c508fSJin Yao 	 */
106473c0ca1eSJin Yao 	threads = thread_map__new_str(target->pid, target->tid, target->uid,
1065147c508fSJin Yao 				      all_threads);
106674bfd2b2SAdrian Hunter 
106774bfd2b2SAdrian Hunter 	if (!threads)
10687e2ed097SArnaldo Carvalho de Melo 		return -1;
10697e2ed097SArnaldo Carvalho de Melo 
10709c105fbcSDongsheng Yang 	if (target__uses_dummy_map(target))
1071397721e0SJiri Olsa 		cpus = perf_cpu_map__dummy_new();
1072879d77d0SNamhyung Kim 	else
10739c3516d1SJiri Olsa 		cpus = perf_cpu_map__new(target->cpu_list);
10747e2ed097SArnaldo Carvalho de Melo 
107574bfd2b2SAdrian Hunter 	if (!cpus)
10767e2ed097SArnaldo Carvalho de Melo 		goto out_delete_threads;
10777e2ed097SArnaldo Carvalho de Melo 
1078ec903f26SJiri Olsa 	evlist->core.has_user_cpus = !!target->cpu_list;
1079ec9a77a7SAdrian Hunter 
1080453fa030SJiri Olsa 	perf_evlist__set_maps(&evlist->core, cpus, threads);
1081d5bc056eSAdrian Hunter 
1082d5bc056eSAdrian Hunter 	return 0;
10837e2ed097SArnaldo Carvalho de Melo 
10847e2ed097SArnaldo Carvalho de Melo out_delete_threads:
10857836e52eSJiri Olsa 	perf_thread_map__put(threads);
10867e2ed097SArnaldo Carvalho de Melo 	return -1;
10877e2ed097SArnaldo Carvalho de Melo }
10887e2ed097SArnaldo Carvalho de Melo 
108963503dbaSJiri Olsa void __perf_evlist__set_sample_bit(struct evlist *evlist,
109022c8a376SArnaldo Carvalho de Melo 				   enum perf_event_sample_format bit)
109122c8a376SArnaldo Carvalho de Melo {
109232dcd021SJiri Olsa 	struct evsel *evsel;
109322c8a376SArnaldo Carvalho de Melo 
1094e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, evsel)
109522c8a376SArnaldo Carvalho de Melo 		__perf_evsel__set_sample_bit(evsel, bit);
109622c8a376SArnaldo Carvalho de Melo }
109722c8a376SArnaldo Carvalho de Melo 
109863503dbaSJiri Olsa void __perf_evlist__reset_sample_bit(struct evlist *evlist,
109922c8a376SArnaldo Carvalho de Melo 				     enum perf_event_sample_format bit)
110022c8a376SArnaldo Carvalho de Melo {
110132dcd021SJiri Olsa 	struct evsel *evsel;
110222c8a376SArnaldo Carvalho de Melo 
1103e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, evsel)
110422c8a376SArnaldo Carvalho de Melo 		__perf_evsel__reset_sample_bit(evsel, bit);
110522c8a376SArnaldo Carvalho de Melo }
110622c8a376SArnaldo Carvalho de Melo 
110763503dbaSJiri Olsa int perf_evlist__apply_filters(struct evlist *evlist, struct evsel **err_evsel)
11080a102479SFrederic Weisbecker {
110932dcd021SJiri Olsa 	struct evsel *evsel;
1110745cefc5SArnaldo Carvalho de Melo 	int err = 0;
11110a102479SFrederic Weisbecker 
1112e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, evsel) {
1113745cefc5SArnaldo Carvalho de Melo 		if (evsel->filter == NULL)
11140a102479SFrederic Weisbecker 			continue;
1115745cefc5SArnaldo Carvalho de Melo 
1116d988d5eeSKan Liang 		/*
1117d988d5eeSKan Liang 		 * filters only work for tracepoint event, which doesn't have cpu limit.
1118d988d5eeSKan Liang 		 * So evlist and evsel should always be same.
1119d988d5eeSKan Liang 		 */
1120a00571fdSJiri Olsa 		err = perf_evsel__apply_filter(&evsel->core, evsel->filter);
112123d4aad4SArnaldo Carvalho de Melo 		if (err) {
112223d4aad4SArnaldo Carvalho de Melo 			*err_evsel = evsel;
1123745cefc5SArnaldo Carvalho de Melo 			break;
11240a102479SFrederic Weisbecker 		}
112523d4aad4SArnaldo Carvalho de Melo 	}
11260a102479SFrederic Weisbecker 
1127745cefc5SArnaldo Carvalho de Melo 	return err;
1128745cefc5SArnaldo Carvalho de Melo }
1129745cefc5SArnaldo Carvalho de Melo 
113063503dbaSJiri Olsa int perf_evlist__set_tp_filter(struct evlist *evlist, const char *filter)
1131745cefc5SArnaldo Carvalho de Melo {
113232dcd021SJiri Olsa 	struct evsel *evsel;
1133745cefc5SArnaldo Carvalho de Melo 	int err = 0;
1134745cefc5SArnaldo Carvalho de Melo 
1135e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, evsel) {
11361fc632ceSJiri Olsa 		if (evsel->core.attr.type != PERF_TYPE_TRACEPOINT)
1137fdf14720SWang Nan 			continue;
1138fdf14720SWang Nan 
113994ad89bcSArnaldo Carvalho de Melo 		err = perf_evsel__set_filter(evsel, filter);
1140745cefc5SArnaldo Carvalho de Melo 		if (err)
1141745cefc5SArnaldo Carvalho de Melo 			break;
1142745cefc5SArnaldo Carvalho de Melo 	}
1143745cefc5SArnaldo Carvalho de Melo 
1144745cefc5SArnaldo Carvalho de Melo 	return err;
11450a102479SFrederic Weisbecker }
114674429964SFrederic Weisbecker 
114763503dbaSJiri Olsa int perf_evlist__set_tp_filter_pids(struct evlist *evlist, size_t npids, pid_t *pids)
1148cfd70a26SArnaldo Carvalho de Melo {
1149cfd70a26SArnaldo Carvalho de Melo 	char *filter;
1150be199adaSArnaldo Carvalho de Melo 	int ret = -1;
1151be199adaSArnaldo Carvalho de Melo 	size_t i;
1152cfd70a26SArnaldo Carvalho de Melo 
1153be199adaSArnaldo Carvalho de Melo 	for (i = 0; i < npids; ++i) {
1154be199adaSArnaldo Carvalho de Melo 		if (i == 0) {
1155be199adaSArnaldo Carvalho de Melo 			if (asprintf(&filter, "common_pid != %d", pids[i]) < 0)
1156cfd70a26SArnaldo Carvalho de Melo 				return -1;
1157be199adaSArnaldo Carvalho de Melo 		} else {
1158be199adaSArnaldo Carvalho de Melo 			char *tmp;
1159be199adaSArnaldo Carvalho de Melo 
1160be199adaSArnaldo Carvalho de Melo 			if (asprintf(&tmp, "%s && common_pid != %d", filter, pids[i]) < 0)
1161be199adaSArnaldo Carvalho de Melo 				goto out_free;
1162be199adaSArnaldo Carvalho de Melo 
1163be199adaSArnaldo Carvalho de Melo 			free(filter);
1164be199adaSArnaldo Carvalho de Melo 			filter = tmp;
1165be199adaSArnaldo Carvalho de Melo 		}
1166be199adaSArnaldo Carvalho de Melo 	}
1167cfd70a26SArnaldo Carvalho de Melo 
11687ad92a33SArnaldo Carvalho de Melo 	ret = perf_evlist__set_tp_filter(evlist, filter);
1169be199adaSArnaldo Carvalho de Melo out_free:
1170cfd70a26SArnaldo Carvalho de Melo 	free(filter);
1171cfd70a26SArnaldo Carvalho de Melo 	return ret;
1172cfd70a26SArnaldo Carvalho de Melo }
1173cfd70a26SArnaldo Carvalho de Melo 
117463503dbaSJiri Olsa int perf_evlist__set_tp_filter_pid(struct evlist *evlist, pid_t pid)
1175be199adaSArnaldo Carvalho de Melo {
11767ad92a33SArnaldo Carvalho de Melo 	return perf_evlist__set_tp_filter_pids(evlist, 1, &pid);
1177be199adaSArnaldo Carvalho de Melo }
1178be199adaSArnaldo Carvalho de Melo 
117963503dbaSJiri Olsa bool perf_evlist__valid_sample_type(struct evlist *evlist)
118074429964SFrederic Weisbecker {
118132dcd021SJiri Olsa 	struct evsel *pos;
1182c2a70653SArnaldo Carvalho de Melo 
11836484d2f9SJiri Olsa 	if (evlist->core.nr_entries == 1)
118475562573SAdrian Hunter 		return true;
118575562573SAdrian Hunter 
118675562573SAdrian Hunter 	if (evlist->id_pos < 0 || evlist->is_pos < 0)
118775562573SAdrian Hunter 		return false;
118875562573SAdrian Hunter 
1189e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, pos) {
119075562573SAdrian Hunter 		if (pos->id_pos != evlist->id_pos ||
119175562573SAdrian Hunter 		    pos->is_pos != evlist->is_pos)
1192c2a70653SArnaldo Carvalho de Melo 			return false;
119374429964SFrederic Weisbecker 	}
119474429964SFrederic Weisbecker 
1195c2a70653SArnaldo Carvalho de Melo 	return true;
1196c2a70653SArnaldo Carvalho de Melo }
1197c2a70653SArnaldo Carvalho de Melo 
119863503dbaSJiri Olsa u64 __perf_evlist__combined_sample_type(struct evlist *evlist)
1199c2a70653SArnaldo Carvalho de Melo {
120032dcd021SJiri Olsa 	struct evsel *evsel;
120175562573SAdrian Hunter 
120275562573SAdrian Hunter 	if (evlist->combined_sample_type)
120375562573SAdrian Hunter 		return evlist->combined_sample_type;
120475562573SAdrian Hunter 
1205e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, evsel)
12061fc632ceSJiri Olsa 		evlist->combined_sample_type |= evsel->core.attr.sample_type;
120775562573SAdrian Hunter 
120875562573SAdrian Hunter 	return evlist->combined_sample_type;
120975562573SAdrian Hunter }
121075562573SAdrian Hunter 
121163503dbaSJiri Olsa u64 perf_evlist__combined_sample_type(struct evlist *evlist)
121275562573SAdrian Hunter {
121375562573SAdrian Hunter 	evlist->combined_sample_type = 0;
121475562573SAdrian Hunter 	return __perf_evlist__combined_sample_type(evlist);
1215c2a70653SArnaldo Carvalho de Melo }
1216c2a70653SArnaldo Carvalho de Melo 
121763503dbaSJiri Olsa u64 perf_evlist__combined_branch_type(struct evlist *evlist)
121898df858eSAndi Kleen {
121932dcd021SJiri Olsa 	struct evsel *evsel;
122098df858eSAndi Kleen 	u64 branch_type = 0;
122198df858eSAndi Kleen 
1222e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, evsel)
12231fc632ceSJiri Olsa 		branch_type |= evsel->core.attr.branch_sample_type;
122498df858eSAndi Kleen 	return branch_type;
122598df858eSAndi Kleen }
122698df858eSAndi Kleen 
122763503dbaSJiri Olsa bool perf_evlist__valid_read_format(struct evlist *evlist)
12289ede473cSJiri Olsa {
122932dcd021SJiri Olsa 	struct evsel *first = perf_evlist__first(evlist), *pos = first;
12301fc632ceSJiri Olsa 	u64 read_format = first->core.attr.read_format;
12311fc632ceSJiri Olsa 	u64 sample_type = first->core.attr.sample_type;
12329ede473cSJiri Olsa 
1233e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, pos) {
12341fc632ceSJiri Olsa 		if (read_format != pos->core.attr.read_format)
12359ede473cSJiri Olsa 			return false;
12369ede473cSJiri Olsa 	}
12379ede473cSJiri Olsa 
12389ede473cSJiri Olsa 	/* PERF_SAMPLE_READ imples PERF_FORMAT_ID. */
12399ede473cSJiri Olsa 	if ((sample_type & PERF_SAMPLE_READ) &&
12409ede473cSJiri Olsa 	    !(read_format & PERF_FORMAT_ID)) {
12419ede473cSJiri Olsa 		return false;
12429ede473cSJiri Olsa 	}
12439ede473cSJiri Olsa 
12449ede473cSJiri Olsa 	return true;
12459ede473cSJiri Olsa }
12469ede473cSJiri Olsa 
124763503dbaSJiri Olsa u64 perf_evlist__read_format(struct evlist *evlist)
12489ede473cSJiri Olsa {
124932dcd021SJiri Olsa 	struct evsel *first = perf_evlist__first(evlist);
12501fc632ceSJiri Olsa 	return first->core.attr.read_format;
12519ede473cSJiri Olsa }
12529ede473cSJiri Olsa 
125363503dbaSJiri Olsa u16 perf_evlist__id_hdr_size(struct evlist *evlist)
125481e36bffSArnaldo Carvalho de Melo {
125532dcd021SJiri Olsa 	struct evsel *first = perf_evlist__first(evlist);
125681e36bffSArnaldo Carvalho de Melo 	struct perf_sample *data;
125781e36bffSArnaldo Carvalho de Melo 	u64 sample_type;
125881e36bffSArnaldo Carvalho de Melo 	u16 size = 0;
125981e36bffSArnaldo Carvalho de Melo 
12601fc632ceSJiri Olsa 	if (!first->core.attr.sample_id_all)
126181e36bffSArnaldo Carvalho de Melo 		goto out;
126281e36bffSArnaldo Carvalho de Melo 
12631fc632ceSJiri Olsa 	sample_type = first->core.attr.sample_type;
126481e36bffSArnaldo Carvalho de Melo 
126581e36bffSArnaldo Carvalho de Melo 	if (sample_type & PERF_SAMPLE_TID)
126681e36bffSArnaldo Carvalho de Melo 		size += sizeof(data->tid) * 2;
126781e36bffSArnaldo Carvalho de Melo 
126881e36bffSArnaldo Carvalho de Melo        if (sample_type & PERF_SAMPLE_TIME)
126981e36bffSArnaldo Carvalho de Melo 		size += sizeof(data->time);
127081e36bffSArnaldo Carvalho de Melo 
127181e36bffSArnaldo Carvalho de Melo 	if (sample_type & PERF_SAMPLE_ID)
127281e36bffSArnaldo Carvalho de Melo 		size += sizeof(data->id);
127381e36bffSArnaldo Carvalho de Melo 
127481e36bffSArnaldo Carvalho de Melo 	if (sample_type & PERF_SAMPLE_STREAM_ID)
127581e36bffSArnaldo Carvalho de Melo 		size += sizeof(data->stream_id);
127681e36bffSArnaldo Carvalho de Melo 
127781e36bffSArnaldo Carvalho de Melo 	if (sample_type & PERF_SAMPLE_CPU)
127881e36bffSArnaldo Carvalho de Melo 		size += sizeof(data->cpu) * 2;
127975562573SAdrian Hunter 
128075562573SAdrian Hunter 	if (sample_type & PERF_SAMPLE_IDENTIFIER)
128175562573SAdrian Hunter 		size += sizeof(data->id);
128281e36bffSArnaldo Carvalho de Melo out:
128381e36bffSArnaldo Carvalho de Melo 	return size;
128481e36bffSArnaldo Carvalho de Melo }
128581e36bffSArnaldo Carvalho de Melo 
128663503dbaSJiri Olsa bool perf_evlist__valid_sample_id_all(struct evlist *evlist)
1287c2a70653SArnaldo Carvalho de Melo {
128832dcd021SJiri Olsa 	struct evsel *first = perf_evlist__first(evlist), *pos = first;
1289c2a70653SArnaldo Carvalho de Melo 
1290e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry_continue(evlist, pos) {
12911fc632ceSJiri Olsa 		if (first->core.attr.sample_id_all != pos->core.attr.sample_id_all)
1292c2a70653SArnaldo Carvalho de Melo 			return false;
1293c2a70653SArnaldo Carvalho de Melo 	}
1294c2a70653SArnaldo Carvalho de Melo 
1295c2a70653SArnaldo Carvalho de Melo 	return true;
129674429964SFrederic Weisbecker }
129774429964SFrederic Weisbecker 
129863503dbaSJiri Olsa bool perf_evlist__sample_id_all(struct evlist *evlist)
129974429964SFrederic Weisbecker {
130032dcd021SJiri Olsa 	struct evsel *first = perf_evlist__first(evlist);
13011fc632ceSJiri Olsa 	return first->core.attr.sample_id_all;
130274429964SFrederic Weisbecker }
130381cce8deSArnaldo Carvalho de Melo 
130463503dbaSJiri Olsa void perf_evlist__set_selected(struct evlist *evlist,
130532dcd021SJiri Olsa 			       struct evsel *evsel)
130681cce8deSArnaldo Carvalho de Melo {
130781cce8deSArnaldo Carvalho de Melo 	evlist->selected = evsel;
130881cce8deSArnaldo Carvalho de Melo }
1309727ab04eSArnaldo Carvalho de Melo 
1310750b4edeSJiri Olsa void evlist__close(struct evlist *evlist)
1311a74b4b66SNamhyung Kim {
131232dcd021SJiri Olsa 	struct evsel *evsel;
1313a74b4b66SNamhyung Kim 
1314475fb533SAndi Kleen 	evlist__for_each_entry_reverse(evlist, evsel)
131588761fa1SJiri Olsa 		evsel__close(evsel);
1316a74b4b66SNamhyung Kim }
1317a74b4b66SNamhyung Kim 
131863503dbaSJiri Olsa static int perf_evlist__create_syswide_maps(struct evlist *evlist)
13194112eb18SArnaldo Carvalho de Melo {
1320f854839bSJiri Olsa 	struct perf_cpu_map *cpus;
13219749b90eSJiri Olsa 	struct perf_thread_map *threads;
13224112eb18SArnaldo Carvalho de Melo 	int err = -ENOMEM;
13234112eb18SArnaldo Carvalho de Melo 
13244112eb18SArnaldo Carvalho de Melo 	/*
13254112eb18SArnaldo Carvalho de Melo 	 * Try reading /sys/devices/system/cpu/online to get
13264112eb18SArnaldo Carvalho de Melo 	 * an all cpus map.
13274112eb18SArnaldo Carvalho de Melo 	 *
13284112eb18SArnaldo Carvalho de Melo 	 * FIXME: -ENOMEM is the best we can do here, the cpu_map
13294112eb18SArnaldo Carvalho de Melo 	 * code needs an overhaul to properly forward the
13304112eb18SArnaldo Carvalho de Melo 	 * error, and we may not want to do that fallback to a
13314112eb18SArnaldo Carvalho de Melo 	 * default cpu identity map :-\
13324112eb18SArnaldo Carvalho de Melo 	 */
13339c3516d1SJiri Olsa 	cpus = perf_cpu_map__new(NULL);
13348c0498b6SAdrian Hunter 	if (!cpus)
13354112eb18SArnaldo Carvalho de Melo 		goto out;
13364112eb18SArnaldo Carvalho de Melo 
13374b49cce2SJiri Olsa 	threads = perf_thread_map__new_dummy();
13388c0498b6SAdrian Hunter 	if (!threads)
13398c0498b6SAdrian Hunter 		goto out_put;
13404112eb18SArnaldo Carvalho de Melo 
1341453fa030SJiri Olsa 	perf_evlist__set_maps(&evlist->core, cpus, threads);
13424112eb18SArnaldo Carvalho de Melo out:
13434112eb18SArnaldo Carvalho de Melo 	return err;
13448c0498b6SAdrian Hunter out_put:
134538f01d8dSJiri Olsa 	perf_cpu_map__put(cpus);
13464112eb18SArnaldo Carvalho de Melo 	goto out;
13474112eb18SArnaldo Carvalho de Melo }
13484112eb18SArnaldo Carvalho de Melo 
1349474ddc4cSJiri Olsa int evlist__open(struct evlist *evlist)
1350727ab04eSArnaldo Carvalho de Melo {
135132dcd021SJiri Olsa 	struct evsel *evsel;
1352a74b4b66SNamhyung Kim 	int err;
1353727ab04eSArnaldo Carvalho de Melo 
13544112eb18SArnaldo Carvalho de Melo 	/*
13554112eb18SArnaldo Carvalho de Melo 	 * Default: one fd per CPU, all threads, aka systemwide
13564112eb18SArnaldo Carvalho de Melo 	 * as sys_perf_event_open(cpu = -1, thread = -1) is EINVAL
13574112eb18SArnaldo Carvalho de Melo 	 */
135803617c22SJiri Olsa 	if (evlist->core.threads == NULL && evlist->core.cpus == NULL) {
13594112eb18SArnaldo Carvalho de Melo 		err = perf_evlist__create_syswide_maps(evlist);
13604112eb18SArnaldo Carvalho de Melo 		if (err < 0)
13614112eb18SArnaldo Carvalho de Melo 			goto out_err;
13624112eb18SArnaldo Carvalho de Melo 	}
13634112eb18SArnaldo Carvalho de Melo 
1364733cd2feSAdrian Hunter 	perf_evlist__update_id_pos(evlist);
1365733cd2feSAdrian Hunter 
1366e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, evsel) {
1367af663bd0SJiri Olsa 		err = evsel__open(evsel, evsel->core.cpus, evsel->core.threads);
1368727ab04eSArnaldo Carvalho de Melo 		if (err < 0)
1369727ab04eSArnaldo Carvalho de Melo 			goto out_err;
1370727ab04eSArnaldo Carvalho de Melo 	}
1371727ab04eSArnaldo Carvalho de Melo 
1372727ab04eSArnaldo Carvalho de Melo 	return 0;
1373727ab04eSArnaldo Carvalho de Melo out_err:
1374750b4edeSJiri Olsa 	evlist__close(evlist);
137541c21a68SNamhyung Kim 	errno = -err;
1376727ab04eSArnaldo Carvalho de Melo 	return err;
1377727ab04eSArnaldo Carvalho de Melo }
137835b9d88eSArnaldo Carvalho de Melo 
137963503dbaSJiri Olsa int perf_evlist__prepare_workload(struct evlist *evlist, struct target *target,
138055e162eaSNamhyung Kim 				  const char *argv[], bool pipe_output,
1381735f7e0bSArnaldo Carvalho de Melo 				  void (*exec_error)(int signo, siginfo_t *info, void *ucontext))
138235b9d88eSArnaldo Carvalho de Melo {
138335b9d88eSArnaldo Carvalho de Melo 	int child_ready_pipe[2], go_pipe[2];
138435b9d88eSArnaldo Carvalho de Melo 	char bf;
138535b9d88eSArnaldo Carvalho de Melo 
138635b9d88eSArnaldo Carvalho de Melo 	if (pipe(child_ready_pipe) < 0) {
138735b9d88eSArnaldo Carvalho de Melo 		perror("failed to create 'ready' pipe");
138835b9d88eSArnaldo Carvalho de Melo 		return -1;
138935b9d88eSArnaldo Carvalho de Melo 	}
139035b9d88eSArnaldo Carvalho de Melo 
139135b9d88eSArnaldo Carvalho de Melo 	if (pipe(go_pipe) < 0) {
139235b9d88eSArnaldo Carvalho de Melo 		perror("failed to create 'go' pipe");
139335b9d88eSArnaldo Carvalho de Melo 		goto out_close_ready_pipe;
139435b9d88eSArnaldo Carvalho de Melo 	}
139535b9d88eSArnaldo Carvalho de Melo 
139635b9d88eSArnaldo Carvalho de Melo 	evlist->workload.pid = fork();
139735b9d88eSArnaldo Carvalho de Melo 	if (evlist->workload.pid < 0) {
139835b9d88eSArnaldo Carvalho de Melo 		perror("failed to fork");
139935b9d88eSArnaldo Carvalho de Melo 		goto out_close_pipes;
140035b9d88eSArnaldo Carvalho de Melo 	}
140135b9d88eSArnaldo Carvalho de Melo 
140235b9d88eSArnaldo Carvalho de Melo 	if (!evlist->workload.pid) {
14035f1c4225SArnaldo Carvalho de Melo 		int ret;
14045f1c4225SArnaldo Carvalho de Melo 
1405119fa3c9SNamhyung Kim 		if (pipe_output)
140635b9d88eSArnaldo Carvalho de Melo 			dup2(2, 1);
140735b9d88eSArnaldo Carvalho de Melo 
14080817df08SDavid Ahern 		signal(SIGTERM, SIG_DFL);
14090817df08SDavid Ahern 
141035b9d88eSArnaldo Carvalho de Melo 		close(child_ready_pipe[0]);
141135b9d88eSArnaldo Carvalho de Melo 		close(go_pipe[1]);
141235b9d88eSArnaldo Carvalho de Melo 		fcntl(go_pipe[0], F_SETFD, FD_CLOEXEC);
141335b9d88eSArnaldo Carvalho de Melo 
141435b9d88eSArnaldo Carvalho de Melo 		/*
141535b9d88eSArnaldo Carvalho de Melo 		 * Tell the parent we're ready to go
141635b9d88eSArnaldo Carvalho de Melo 		 */
141735b9d88eSArnaldo Carvalho de Melo 		close(child_ready_pipe[1]);
141835b9d88eSArnaldo Carvalho de Melo 
141935b9d88eSArnaldo Carvalho de Melo 		/*
142035b9d88eSArnaldo Carvalho de Melo 		 * Wait until the parent tells us to go.
142135b9d88eSArnaldo Carvalho de Melo 		 */
14225f1c4225SArnaldo Carvalho de Melo 		ret = read(go_pipe[0], &bf, 1);
14235f1c4225SArnaldo Carvalho de Melo 		/*
14245f1c4225SArnaldo Carvalho de Melo 		 * The parent will ask for the execvp() to be performed by
14255f1c4225SArnaldo Carvalho de Melo 		 * writing exactly one byte, in workload.cork_fd, usually via
14265f1c4225SArnaldo Carvalho de Melo 		 * perf_evlist__start_workload().
14275f1c4225SArnaldo Carvalho de Melo 		 *
142820f86fc1SArnaldo Carvalho de Melo 		 * For cancelling the workload without actually running it,
14295f1c4225SArnaldo Carvalho de Melo 		 * the parent will just close workload.cork_fd, without writing
14305f1c4225SArnaldo Carvalho de Melo 		 * anything, i.e. read will return zero and we just exit()
14315f1c4225SArnaldo Carvalho de Melo 		 * here.
14325f1c4225SArnaldo Carvalho de Melo 		 */
14335f1c4225SArnaldo Carvalho de Melo 		if (ret != 1) {
14345f1c4225SArnaldo Carvalho de Melo 			if (ret == -1)
143535b9d88eSArnaldo Carvalho de Melo 				perror("unable to read pipe");
14365f1c4225SArnaldo Carvalho de Melo 			exit(ret);
14375f1c4225SArnaldo Carvalho de Melo 		}
143835b9d88eSArnaldo Carvalho de Melo 
143935b9d88eSArnaldo Carvalho de Melo 		execvp(argv[0], (char **)argv);
144035b9d88eSArnaldo Carvalho de Melo 
1441735f7e0bSArnaldo Carvalho de Melo 		if (exec_error) {
1442f33cbe72SArnaldo Carvalho de Melo 			union sigval val;
1443f33cbe72SArnaldo Carvalho de Melo 
1444f33cbe72SArnaldo Carvalho de Melo 			val.sival_int = errno;
1445f33cbe72SArnaldo Carvalho de Melo 			if (sigqueue(getppid(), SIGUSR1, val))
144635b9d88eSArnaldo Carvalho de Melo 				perror(argv[0]);
1447f33cbe72SArnaldo Carvalho de Melo 		} else
1448f33cbe72SArnaldo Carvalho de Melo 			perror(argv[0]);
144935b9d88eSArnaldo Carvalho de Melo 		exit(-1);
145035b9d88eSArnaldo Carvalho de Melo 	}
145135b9d88eSArnaldo Carvalho de Melo 
1452735f7e0bSArnaldo Carvalho de Melo 	if (exec_error) {
1453735f7e0bSArnaldo Carvalho de Melo 		struct sigaction act = {
1454735f7e0bSArnaldo Carvalho de Melo 			.sa_flags     = SA_SIGINFO,
1455735f7e0bSArnaldo Carvalho de Melo 			.sa_sigaction = exec_error,
1456735f7e0bSArnaldo Carvalho de Melo 		};
1457735f7e0bSArnaldo Carvalho de Melo 		sigaction(SIGUSR1, &act, NULL);
1458735f7e0bSArnaldo Carvalho de Melo 	}
1459735f7e0bSArnaldo Carvalho de Melo 
14601aaf63b1SArnaldo Carvalho de Melo 	if (target__none(target)) {
146103617c22SJiri Olsa 		if (evlist->core.threads == NULL) {
14621aaf63b1SArnaldo Carvalho de Melo 			fprintf(stderr, "FATAL: evlist->threads need to be set at this point (%s:%d).\n",
14631aaf63b1SArnaldo Carvalho de Melo 				__func__, __LINE__);
14641aaf63b1SArnaldo Carvalho de Melo 			goto out_close_pipes;
14651aaf63b1SArnaldo Carvalho de Melo 		}
146603617c22SJiri Olsa 		perf_thread_map__set_pid(evlist->core.threads, 0, evlist->workload.pid);
14671aaf63b1SArnaldo Carvalho de Melo 	}
146835b9d88eSArnaldo Carvalho de Melo 
146935b9d88eSArnaldo Carvalho de Melo 	close(child_ready_pipe[1]);
147035b9d88eSArnaldo Carvalho de Melo 	close(go_pipe[0]);
147135b9d88eSArnaldo Carvalho de Melo 	/*
147235b9d88eSArnaldo Carvalho de Melo 	 * wait for child to settle
147335b9d88eSArnaldo Carvalho de Melo 	 */
147435b9d88eSArnaldo Carvalho de Melo 	if (read(child_ready_pipe[0], &bf, 1) == -1) {
147535b9d88eSArnaldo Carvalho de Melo 		perror("unable to read pipe");
147635b9d88eSArnaldo Carvalho de Melo 		goto out_close_pipes;
147735b9d88eSArnaldo Carvalho de Melo 	}
147835b9d88eSArnaldo Carvalho de Melo 
1479bcf3145fSNamhyung Kim 	fcntl(go_pipe[1], F_SETFD, FD_CLOEXEC);
148035b9d88eSArnaldo Carvalho de Melo 	evlist->workload.cork_fd = go_pipe[1];
148135b9d88eSArnaldo Carvalho de Melo 	close(child_ready_pipe[0]);
148235b9d88eSArnaldo Carvalho de Melo 	return 0;
148335b9d88eSArnaldo Carvalho de Melo 
148435b9d88eSArnaldo Carvalho de Melo out_close_pipes:
148535b9d88eSArnaldo Carvalho de Melo 	close(go_pipe[0]);
148635b9d88eSArnaldo Carvalho de Melo 	close(go_pipe[1]);
148735b9d88eSArnaldo Carvalho de Melo out_close_ready_pipe:
148835b9d88eSArnaldo Carvalho de Melo 	close(child_ready_pipe[0]);
148935b9d88eSArnaldo Carvalho de Melo 	close(child_ready_pipe[1]);
149035b9d88eSArnaldo Carvalho de Melo 	return -1;
149135b9d88eSArnaldo Carvalho de Melo }
149235b9d88eSArnaldo Carvalho de Melo 
149363503dbaSJiri Olsa int perf_evlist__start_workload(struct evlist *evlist)
149435b9d88eSArnaldo Carvalho de Melo {
149535b9d88eSArnaldo Carvalho de Melo 	if (evlist->workload.cork_fd > 0) {
1496b3824404SDavid Ahern 		char bf = 0;
1497bcf3145fSNamhyung Kim 		int ret;
149835b9d88eSArnaldo Carvalho de Melo 		/*
149935b9d88eSArnaldo Carvalho de Melo 		 * Remove the cork, let it rip!
150035b9d88eSArnaldo Carvalho de Melo 		 */
1501bcf3145fSNamhyung Kim 		ret = write(evlist->workload.cork_fd, &bf, 1);
1502bcf3145fSNamhyung Kim 		if (ret < 0)
1503e978be9eSSoramichi Akiyama 			perror("unable to write to pipe");
1504bcf3145fSNamhyung Kim 
1505bcf3145fSNamhyung Kim 		close(evlist->workload.cork_fd);
1506bcf3145fSNamhyung Kim 		return ret;
150735b9d88eSArnaldo Carvalho de Melo 	}
150835b9d88eSArnaldo Carvalho de Melo 
150935b9d88eSArnaldo Carvalho de Melo 	return 0;
151035b9d88eSArnaldo Carvalho de Melo }
1511cb0b29e0SArnaldo Carvalho de Melo 
151263503dbaSJiri Olsa int perf_evlist__parse_sample(struct evlist *evlist, union perf_event *event,
15130807d2d8SArnaldo Carvalho de Melo 			      struct perf_sample *sample)
1514cb0b29e0SArnaldo Carvalho de Melo {
151532dcd021SJiri Olsa 	struct evsel *evsel = perf_evlist__event2evsel(evlist, event);
151675562573SAdrian Hunter 
151775562573SAdrian Hunter 	if (!evsel)
151875562573SAdrian Hunter 		return -EFAULT;
15190807d2d8SArnaldo Carvalho de Melo 	return perf_evsel__parse_sample(evsel, event, sample);
1520cb0b29e0SArnaldo Carvalho de Melo }
152178f067b3SArnaldo Carvalho de Melo 
152263503dbaSJiri Olsa int perf_evlist__parse_sample_timestamp(struct evlist *evlist,
152301468120SJiri Olsa 					union perf_event *event,
152401468120SJiri Olsa 					u64 *timestamp)
152501468120SJiri Olsa {
152632dcd021SJiri Olsa 	struct evsel *evsel = perf_evlist__event2evsel(evlist, event);
152701468120SJiri Olsa 
152801468120SJiri Olsa 	if (!evsel)
152901468120SJiri Olsa 		return -EFAULT;
153001468120SJiri Olsa 	return perf_evsel__parse_sample_timestamp(evsel, event, timestamp);
153101468120SJiri Olsa }
153201468120SJiri Olsa 
153363503dbaSJiri Olsa size_t perf_evlist__fprintf(struct evlist *evlist, FILE *fp)
153478f067b3SArnaldo Carvalho de Melo {
153532dcd021SJiri Olsa 	struct evsel *evsel;
153678f067b3SArnaldo Carvalho de Melo 	size_t printed = 0;
153778f067b3SArnaldo Carvalho de Melo 
1538e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, evsel) {
153978f067b3SArnaldo Carvalho de Melo 		printed += fprintf(fp, "%s%s", evsel->idx ? ", " : "",
154078f067b3SArnaldo Carvalho de Melo 				   perf_evsel__name(evsel));
154178f067b3SArnaldo Carvalho de Melo 	}
154278f067b3SArnaldo Carvalho de Melo 
1543b2222139SDavidlohr Bueso 	return printed + fprintf(fp, "\n");
154478f067b3SArnaldo Carvalho de Melo }
15456ef068cbSArnaldo Carvalho de Melo 
154663503dbaSJiri Olsa int perf_evlist__strerror_open(struct evlist *evlist,
1547a8f23d8fSArnaldo Carvalho de Melo 			       int err, char *buf, size_t size)
1548a8f23d8fSArnaldo Carvalho de Melo {
1549a8f23d8fSArnaldo Carvalho de Melo 	int printed, value;
1550c8b5f2c9SArnaldo Carvalho de Melo 	char sbuf[STRERR_BUFSIZE], *emsg = str_error_r(err, sbuf, sizeof(sbuf));
1551a8f23d8fSArnaldo Carvalho de Melo 
1552a8f23d8fSArnaldo Carvalho de Melo 	switch (err) {
1553a8f23d8fSArnaldo Carvalho de Melo 	case EACCES:
1554a8f23d8fSArnaldo Carvalho de Melo 	case EPERM:
1555a8f23d8fSArnaldo Carvalho de Melo 		printed = scnprintf(buf, size,
1556a8f23d8fSArnaldo Carvalho de Melo 				    "Error:\t%s.\n"
1557a8f23d8fSArnaldo Carvalho de Melo 				    "Hint:\tCheck /proc/sys/kernel/perf_event_paranoid setting.", emsg);
1558a8f23d8fSArnaldo Carvalho de Melo 
15591a47245dSAdrian Hunter 		value = perf_event_paranoid();
1560a8f23d8fSArnaldo Carvalho de Melo 
1561a8f23d8fSArnaldo Carvalho de Melo 		printed += scnprintf(buf + printed, size - printed, "\nHint:\t");
1562a8f23d8fSArnaldo Carvalho de Melo 
1563a8f23d8fSArnaldo Carvalho de Melo 		if (value >= 2) {
1564a8f23d8fSArnaldo Carvalho de Melo 			printed += scnprintf(buf + printed, size - printed,
1565a8f23d8fSArnaldo Carvalho de Melo 					     "For your workloads it needs to be <= 1\nHint:\t");
1566a8f23d8fSArnaldo Carvalho de Melo 		}
1567a8f23d8fSArnaldo Carvalho de Melo 		printed += scnprintf(buf + printed, size - printed,
15685229e366SArnaldo Carvalho de Melo 				     "For system wide tracing it needs to be set to -1.\n");
1569a8f23d8fSArnaldo Carvalho de Melo 
1570a8f23d8fSArnaldo Carvalho de Melo 		printed += scnprintf(buf + printed, size - printed,
15715229e366SArnaldo Carvalho de Melo 				    "Hint:\tTry: 'sudo sh -c \"echo -1 > /proc/sys/kernel/perf_event_paranoid\"'\n"
15725229e366SArnaldo Carvalho de Melo 				    "Hint:\tThe current value is %d.", value);
1573a8f23d8fSArnaldo Carvalho de Melo 		break;
1574d9aade7fSArnaldo Carvalho de Melo 	case EINVAL: {
157532dcd021SJiri Olsa 		struct evsel *first = perf_evlist__first(evlist);
1576d9aade7fSArnaldo Carvalho de Melo 		int max_freq;
1577d9aade7fSArnaldo Carvalho de Melo 
1578d9aade7fSArnaldo Carvalho de Melo 		if (sysctl__read_int("kernel/perf_event_max_sample_rate", &max_freq) < 0)
1579d9aade7fSArnaldo Carvalho de Melo 			goto out_default;
1580d9aade7fSArnaldo Carvalho de Melo 
15811fc632ceSJiri Olsa 		if (first->core.attr.sample_freq < (u64)max_freq)
1582d9aade7fSArnaldo Carvalho de Melo 			goto out_default;
1583d9aade7fSArnaldo Carvalho de Melo 
1584d9aade7fSArnaldo Carvalho de Melo 		printed = scnprintf(buf, size,
1585d9aade7fSArnaldo Carvalho de Melo 				    "Error:\t%s.\n"
1586d9aade7fSArnaldo Carvalho de Melo 				    "Hint:\tCheck /proc/sys/kernel/perf_event_max_sample_rate.\n"
1587d9aade7fSArnaldo Carvalho de Melo 				    "Hint:\tThe current value is %d and %" PRIu64 " is being requested.",
15881fc632ceSJiri Olsa 				    emsg, max_freq, first->core.attr.sample_freq);
1589d9aade7fSArnaldo Carvalho de Melo 		break;
1590d9aade7fSArnaldo Carvalho de Melo 	}
1591a8f23d8fSArnaldo Carvalho de Melo 	default:
1592d9aade7fSArnaldo Carvalho de Melo out_default:
1593a8f23d8fSArnaldo Carvalho de Melo 		scnprintf(buf, size, "%s", emsg);
1594a8f23d8fSArnaldo Carvalho de Melo 		break;
1595a8f23d8fSArnaldo Carvalho de Melo 	}
1596a8f23d8fSArnaldo Carvalho de Melo 
1597a8f23d8fSArnaldo Carvalho de Melo 	return 0;
1598a8f23d8fSArnaldo Carvalho de Melo }
1599a025e4f0SAdrian Hunter 
160063503dbaSJiri Olsa int perf_evlist__strerror_mmap(struct evlist *evlist, int err, char *buf, size_t size)
1601956fa571SArnaldo Carvalho de Melo {
1602c8b5f2c9SArnaldo Carvalho de Melo 	char sbuf[STRERR_BUFSIZE], *emsg = str_error_r(err, sbuf, sizeof(sbuf));
1603f6fa4375SJiri Olsa 	int pages_attempted = evlist->core.mmap_len / 1024, pages_max_per_user, printed = 0;
1604956fa571SArnaldo Carvalho de Melo 
1605956fa571SArnaldo Carvalho de Melo 	switch (err) {
1606956fa571SArnaldo Carvalho de Melo 	case EPERM:
1607e5d4a290SArnaldo Carvalho de Melo 		sysctl__read_int("kernel/perf_event_mlock_kb", &pages_max_per_user);
1608e965bea1SArnaldo Carvalho de Melo 		printed += scnprintf(buf + printed, size - printed,
1609e965bea1SArnaldo Carvalho de Melo 				     "Error:\t%s.\n"
1610956fa571SArnaldo Carvalho de Melo 				     "Hint:\tCheck /proc/sys/kernel/perf_event_mlock_kb (%d kB) setting.\n"
1611e965bea1SArnaldo Carvalho de Melo 				     "Hint:\tTried using %zd kB.\n",
1612e5d4a290SArnaldo Carvalho de Melo 				     emsg, pages_max_per_user, pages_attempted);
1613e965bea1SArnaldo Carvalho de Melo 
1614e965bea1SArnaldo Carvalho de Melo 		if (pages_attempted >= pages_max_per_user) {
1615e965bea1SArnaldo Carvalho de Melo 			printed += scnprintf(buf + printed, size - printed,
1616e965bea1SArnaldo Carvalho de Melo 					     "Hint:\tTry 'sudo sh -c \"echo %d > /proc/sys/kernel/perf_event_mlock_kb\"', or\n",
1617e965bea1SArnaldo Carvalho de Melo 					     pages_max_per_user + pages_attempted);
1618e965bea1SArnaldo Carvalho de Melo 		}
1619e965bea1SArnaldo Carvalho de Melo 
1620e965bea1SArnaldo Carvalho de Melo 		printed += scnprintf(buf + printed, size - printed,
1621e965bea1SArnaldo Carvalho de Melo 				     "Hint:\tTry using a smaller -m/--mmap-pages value.");
1622956fa571SArnaldo Carvalho de Melo 		break;
1623956fa571SArnaldo Carvalho de Melo 	default:
1624956fa571SArnaldo Carvalho de Melo 		scnprintf(buf, size, "%s", emsg);
1625956fa571SArnaldo Carvalho de Melo 		break;
1626956fa571SArnaldo Carvalho de Melo 	}
1627956fa571SArnaldo Carvalho de Melo 
1628956fa571SArnaldo Carvalho de Melo 	return 0;
1629956fa571SArnaldo Carvalho de Melo }
1630956fa571SArnaldo Carvalho de Melo 
163163503dbaSJiri Olsa void perf_evlist__to_front(struct evlist *evlist,
163232dcd021SJiri Olsa 			   struct evsel *move_evsel)
1633a025e4f0SAdrian Hunter {
163432dcd021SJiri Olsa 	struct evsel *evsel, *n;
1635a025e4f0SAdrian Hunter 	LIST_HEAD(move);
1636a025e4f0SAdrian Hunter 
1637a025e4f0SAdrian Hunter 	if (move_evsel == perf_evlist__first(evlist))
1638a025e4f0SAdrian Hunter 		return;
1639a025e4f0SAdrian Hunter 
1640e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry_safe(evlist, n, evsel) {
1641a025e4f0SAdrian Hunter 		if (evsel->leader == move_evsel->leader)
1642b27c4eceSJiri Olsa 			list_move_tail(&evsel->core.node, &move);
1643a025e4f0SAdrian Hunter 	}
1644a025e4f0SAdrian Hunter 
1645ce9036a6SJiri Olsa 	list_splice(&move, &evlist->core.entries);
1646a025e4f0SAdrian Hunter }
164760b0896cSAdrian Hunter 
164863503dbaSJiri Olsa void perf_evlist__set_tracking_event(struct evlist *evlist,
164932dcd021SJiri Olsa 				     struct evsel *tracking_evsel)
165060b0896cSAdrian Hunter {
165132dcd021SJiri Olsa 	struct evsel *evsel;
165260b0896cSAdrian Hunter 
165360b0896cSAdrian Hunter 	if (tracking_evsel->tracking)
165460b0896cSAdrian Hunter 		return;
165560b0896cSAdrian Hunter 
1656e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, evsel) {
165760b0896cSAdrian Hunter 		if (evsel != tracking_evsel)
165860b0896cSAdrian Hunter 			evsel->tracking = false;
165960b0896cSAdrian Hunter 	}
166060b0896cSAdrian Hunter 
166160b0896cSAdrian Hunter 	tracking_evsel->tracking = true;
166260b0896cSAdrian Hunter }
16637630b3e2SWang Nan 
166432dcd021SJiri Olsa struct evsel *
166563503dbaSJiri Olsa perf_evlist__find_evsel_by_str(struct evlist *evlist,
16667630b3e2SWang Nan 			       const char *str)
16677630b3e2SWang Nan {
166832dcd021SJiri Olsa 	struct evsel *evsel;
16697630b3e2SWang Nan 
1670e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, evsel) {
16717630b3e2SWang Nan 		if (!evsel->name)
16727630b3e2SWang Nan 			continue;
16737630b3e2SWang Nan 		if (strcmp(str, evsel->name) == 0)
16747630b3e2SWang Nan 			return evsel;
16757630b3e2SWang Nan 	}
16767630b3e2SWang Nan 
16777630b3e2SWang Nan 	return NULL;
16787630b3e2SWang Nan }
167954cc54deSWang Nan 
168063503dbaSJiri Olsa void perf_evlist__toggle_bkw_mmap(struct evlist *evlist,
168154cc54deSWang Nan 				  enum bkw_mmap_state state)
168254cc54deSWang Nan {
168354cc54deSWang Nan 	enum bkw_mmap_state old_state = evlist->bkw_mmap_state;
168454cc54deSWang Nan 	enum action {
168554cc54deSWang Nan 		NONE,
168654cc54deSWang Nan 		PAUSE,
168754cc54deSWang Nan 		RESUME,
168854cc54deSWang Nan 	} action = NONE;
168954cc54deSWang Nan 
16900b72d69aSWang Nan 	if (!evlist->overwrite_mmap)
169154cc54deSWang Nan 		return;
169254cc54deSWang Nan 
169354cc54deSWang Nan 	switch (old_state) {
169454cc54deSWang Nan 	case BKW_MMAP_NOTREADY: {
169554cc54deSWang Nan 		if (state != BKW_MMAP_RUNNING)
1696dd8bd53aSLuis de Bethencourt 			goto state_err;
169754cc54deSWang Nan 		break;
169854cc54deSWang Nan 	}
169954cc54deSWang Nan 	case BKW_MMAP_RUNNING: {
170054cc54deSWang Nan 		if (state != BKW_MMAP_DATA_PENDING)
170154cc54deSWang Nan 			goto state_err;
170254cc54deSWang Nan 		action = PAUSE;
170354cc54deSWang Nan 		break;
170454cc54deSWang Nan 	}
170554cc54deSWang Nan 	case BKW_MMAP_DATA_PENDING: {
170654cc54deSWang Nan 		if (state != BKW_MMAP_EMPTY)
170754cc54deSWang Nan 			goto state_err;
170854cc54deSWang Nan 		break;
170954cc54deSWang Nan 	}
171054cc54deSWang Nan 	case BKW_MMAP_EMPTY: {
171154cc54deSWang Nan 		if (state != BKW_MMAP_RUNNING)
171254cc54deSWang Nan 			goto state_err;
171354cc54deSWang Nan 		action = RESUME;
171454cc54deSWang Nan 		break;
171554cc54deSWang Nan 	}
171654cc54deSWang Nan 	default:
171754cc54deSWang Nan 		WARN_ONCE(1, "Shouldn't get there\n");
171854cc54deSWang Nan 	}
171954cc54deSWang Nan 
172054cc54deSWang Nan 	evlist->bkw_mmap_state = state;
172154cc54deSWang Nan 
172254cc54deSWang Nan 	switch (action) {
172354cc54deSWang Nan 	case PAUSE:
172454cc54deSWang Nan 		perf_evlist__pause(evlist);
172554cc54deSWang Nan 		break;
172654cc54deSWang Nan 	case RESUME:
172754cc54deSWang Nan 		perf_evlist__resume(evlist);
172854cc54deSWang Nan 		break;
172954cc54deSWang Nan 	case NONE:
173054cc54deSWang Nan 	default:
173154cc54deSWang Nan 		break;
173254cc54deSWang Nan 	}
173354cc54deSWang Nan 
173454cc54deSWang Nan state_err:
173554cc54deSWang Nan 	return;
173654cc54deSWang Nan }
173707d6f446SArnaldo Carvalho de Melo 
173863503dbaSJiri Olsa bool perf_evlist__exclude_kernel(struct evlist *evlist)
173907d6f446SArnaldo Carvalho de Melo {
174032dcd021SJiri Olsa 	struct evsel *evsel;
174107d6f446SArnaldo Carvalho de Melo 
174207d6f446SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, evsel) {
17431fc632ceSJiri Olsa 		if (!evsel->core.attr.exclude_kernel)
174407d6f446SArnaldo Carvalho de Melo 			return false;
174507d6f446SArnaldo Carvalho de Melo 	}
174607d6f446SArnaldo Carvalho de Melo 
174707d6f446SArnaldo Carvalho de Melo 	return true;
174807d6f446SArnaldo Carvalho de Melo }
1749e2bdbe80SJin Yao 
1750e2bdbe80SJin Yao /*
1751e2bdbe80SJin Yao  * Events in data file are not collect in groups, but we still want
1752e2bdbe80SJin Yao  * the group display. Set the artificial group and set the leader's
1753e2bdbe80SJin Yao  * forced_leader flag to notify the display code.
1754e2bdbe80SJin Yao  */
175563503dbaSJiri Olsa void perf_evlist__force_leader(struct evlist *evlist)
1756e2bdbe80SJin Yao {
1757e2bdbe80SJin Yao 	if (!evlist->nr_groups) {
175832dcd021SJiri Olsa 		struct evsel *leader = perf_evlist__first(evlist);
1759e2bdbe80SJin Yao 
1760e2bdbe80SJin Yao 		perf_evlist__set_leader(evlist);
1761e2bdbe80SJin Yao 		leader->forced_leader = true;
1762e2bdbe80SJin Yao 	}
1763e2bdbe80SJin Yao }
1764c3537fc2SAndi Kleen 
176563503dbaSJiri Olsa struct evsel *perf_evlist__reset_weak_group(struct evlist *evsel_list,
176632dcd021SJiri Olsa 						 struct evsel *evsel)
1767c3537fc2SAndi Kleen {
176832dcd021SJiri Olsa 	struct evsel *c2, *leader;
1769c3537fc2SAndi Kleen 	bool is_open = true;
1770c3537fc2SAndi Kleen 
1771c3537fc2SAndi Kleen 	leader = evsel->leader;
1772c3537fc2SAndi Kleen 	pr_debug("Weak group for %s/%d failed\n",
17735643b1a5SJiri Olsa 			leader->name, leader->core.nr_members);
1774c3537fc2SAndi Kleen 
1775c3537fc2SAndi Kleen 	/*
1776c3537fc2SAndi Kleen 	 * for_each_group_member doesn't work here because it doesn't
1777c3537fc2SAndi Kleen 	 * include the first entry.
1778c3537fc2SAndi Kleen 	 */
1779c3537fc2SAndi Kleen 	evlist__for_each_entry(evsel_list, c2) {
1780c3537fc2SAndi Kleen 		if (c2 == evsel)
1781c3537fc2SAndi Kleen 			is_open = false;
1782c3537fc2SAndi Kleen 		if (c2->leader == leader) {
1783c3537fc2SAndi Kleen 			if (is_open)
178488761fa1SJiri Olsa 				evsel__close(c2);
1785c3537fc2SAndi Kleen 			c2->leader = c2;
17865643b1a5SJiri Olsa 			c2->core.nr_members = 0;
1787c3537fc2SAndi Kleen 		}
1788c3537fc2SAndi Kleen 	}
1789c3537fc2SAndi Kleen 	return leader;
1790c3537fc2SAndi Kleen }
1791657ee553SSong Liu 
179263503dbaSJiri Olsa int perf_evlist__add_sb_event(struct evlist **evlist,
1793657ee553SSong Liu 			      struct perf_event_attr *attr,
1794657ee553SSong Liu 			      perf_evsel__sb_cb_t cb,
1795657ee553SSong Liu 			      void *data)
1796657ee553SSong Liu {
179732dcd021SJiri Olsa 	struct evsel *evsel;
1798657ee553SSong Liu 	bool new_evlist = (*evlist) == NULL;
1799657ee553SSong Liu 
1800657ee553SSong Liu 	if (*evlist == NULL)
18010f98b11cSJiri Olsa 		*evlist = evlist__new();
1802657ee553SSong Liu 	if (*evlist == NULL)
1803657ee553SSong Liu 		return -1;
1804657ee553SSong Liu 
1805657ee553SSong Liu 	if (!attr->sample_id_all) {
1806657ee553SSong Liu 		pr_warning("enabling sample_id_all for all side band events\n");
1807657ee553SSong Liu 		attr->sample_id_all = 1;
1808657ee553SSong Liu 	}
1809657ee553SSong Liu 
18106484d2f9SJiri Olsa 	evsel = perf_evsel__new_idx(attr, (*evlist)->core.nr_entries);
1811657ee553SSong Liu 	if (!evsel)
1812657ee553SSong Liu 		goto out_err;
1813657ee553SSong Liu 
1814657ee553SSong Liu 	evsel->side_band.cb = cb;
1815657ee553SSong Liu 	evsel->side_band.data = data;
1816a1cf3a75SJiri Olsa 	evlist__add(*evlist, evsel);
1817657ee553SSong Liu 	return 0;
1818657ee553SSong Liu 
1819657ee553SSong Liu out_err:
1820657ee553SSong Liu 	if (new_evlist) {
1821c12995a5SJiri Olsa 		evlist__delete(*evlist);
1822657ee553SSong Liu 		*evlist = NULL;
1823657ee553SSong Liu 	}
1824657ee553SSong Liu 	return -1;
1825657ee553SSong Liu }
1826657ee553SSong Liu 
1827657ee553SSong Liu static void *perf_evlist__poll_thread(void *arg)
1828657ee553SSong Liu {
182963503dbaSJiri Olsa 	struct evlist *evlist = arg;
1830657ee553SSong Liu 	bool draining = false;
1831adc6257cSJiri Olsa 	int i, done = 0;
1832b397f846SArnaldo Carvalho de Melo 	/*
1833b397f846SArnaldo Carvalho de Melo 	 * In order to read symbols from other namespaces perf to needs to call
1834b397f846SArnaldo Carvalho de Melo 	 * setns(2).  This isn't permitted if the struct_fs has multiple users.
1835b397f846SArnaldo Carvalho de Melo 	 * unshare(2) the fs so that we may continue to setns into namespaces
1836b397f846SArnaldo Carvalho de Melo 	 * that we're observing when, for instance, reading the build-ids at
1837b397f846SArnaldo Carvalho de Melo 	 * the end of a 'perf record' session.
1838b397f846SArnaldo Carvalho de Melo 	 */
1839b397f846SArnaldo Carvalho de Melo 	unshare(CLONE_FS);
1840657ee553SSong Liu 
1841adc6257cSJiri Olsa 	while (!done) {
1842adc6257cSJiri Olsa 		bool got_data = false;
1843adc6257cSJiri Olsa 
1844adc6257cSJiri Olsa 		if (evlist->thread.done)
1845657ee553SSong Liu 			draining = true;
1846657ee553SSong Liu 
1847657ee553SSong Liu 		if (!draining)
1848657ee553SSong Liu 			perf_evlist__poll(evlist, 1000);
1849657ee553SSong Liu 
1850c976ee11SJiri Olsa 		for (i = 0; i < evlist->core.nr_mmaps; i++) {
1851a5830532SJiri Olsa 			struct mmap *map = &evlist->mmap[i];
1852657ee553SSong Liu 			union perf_event *event;
1853657ee553SSong Liu 
1854657ee553SSong Liu 			if (perf_mmap__read_init(map))
1855657ee553SSong Liu 				continue;
1856657ee553SSong Liu 			while ((event = perf_mmap__read_event(map)) != NULL) {
185732dcd021SJiri Olsa 				struct evsel *evsel = perf_evlist__event2evsel(evlist, event);
1858657ee553SSong Liu 
1859657ee553SSong Liu 				if (evsel && evsel->side_band.cb)
1860657ee553SSong Liu 					evsel->side_band.cb(event, evsel->side_band.data);
1861657ee553SSong Liu 				else
1862657ee553SSong Liu 					pr_warning("cannot locate proper evsel for the side band event\n");
1863657ee553SSong Liu 
1864657ee553SSong Liu 				perf_mmap__consume(map);
1865adc6257cSJiri Olsa 				got_data = true;
1866657ee553SSong Liu 			}
1867657ee553SSong Liu 			perf_mmap__read_done(map);
1868657ee553SSong Liu 		}
1869adc6257cSJiri Olsa 
1870adc6257cSJiri Olsa 		if (draining && !got_data)
1871adc6257cSJiri Olsa 			break;
1872657ee553SSong Liu 	}
1873657ee553SSong Liu 	return NULL;
1874657ee553SSong Liu }
1875657ee553SSong Liu 
187663503dbaSJiri Olsa int perf_evlist__start_sb_thread(struct evlist *evlist,
1877657ee553SSong Liu 				 struct target *target)
1878657ee553SSong Liu {
187932dcd021SJiri Olsa 	struct evsel *counter;
1880657ee553SSong Liu 
1881657ee553SSong Liu 	if (!evlist)
1882657ee553SSong Liu 		return 0;
1883657ee553SSong Liu 
1884657ee553SSong Liu 	if (perf_evlist__create_maps(evlist, target))
1885657ee553SSong Liu 		goto out_delete_evlist;
1886657ee553SSong Liu 
1887657ee553SSong Liu 	evlist__for_each_entry(evlist, counter) {
1888f72f901dSJiri Olsa 		if (evsel__open(counter, evlist->core.cpus,
188903617c22SJiri Olsa 				     evlist->core.threads) < 0)
1890657ee553SSong Liu 			goto out_delete_evlist;
1891657ee553SSong Liu 	}
1892657ee553SSong Liu 
18939521b5f2SJiri Olsa 	if (evlist__mmap(evlist, UINT_MAX))
1894657ee553SSong Liu 		goto out_delete_evlist;
1895657ee553SSong Liu 
1896657ee553SSong Liu 	evlist__for_each_entry(evlist, counter) {
1897ec7f24efSJiri Olsa 		if (evsel__enable(counter))
1898657ee553SSong Liu 			goto out_delete_evlist;
1899657ee553SSong Liu 	}
1900657ee553SSong Liu 
1901657ee553SSong Liu 	evlist->thread.done = 0;
1902657ee553SSong Liu 	if (pthread_create(&evlist->thread.th, NULL, perf_evlist__poll_thread, evlist))
1903657ee553SSong Liu 		goto out_delete_evlist;
1904657ee553SSong Liu 
1905657ee553SSong Liu 	return 0;
1906657ee553SSong Liu 
1907657ee553SSong Liu out_delete_evlist:
1908c12995a5SJiri Olsa 	evlist__delete(evlist);
1909657ee553SSong Liu 	evlist = NULL;
1910657ee553SSong Liu 	return -1;
1911657ee553SSong Liu }
1912657ee553SSong Liu 
191363503dbaSJiri Olsa void perf_evlist__stop_sb_thread(struct evlist *evlist)
1914657ee553SSong Liu {
1915657ee553SSong Liu 	if (!evlist)
1916657ee553SSong Liu 		return;
1917657ee553SSong Liu 	evlist->thread.done = 1;
1918657ee553SSong Liu 	pthread_join(evlist->thread.th, NULL);
1919c12995a5SJiri Olsa 	evlist__delete(evlist);
1920657ee553SSong Liu }
1921