xref: /openbmc/linux/tools/perf/util/evlist.c (revision da949f50)
191007045SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2f8a95309SArnaldo Carvalho de Melo /*
3f8a95309SArnaldo Carvalho de Melo  * Copyright (C) 2011, Red Hat Inc, Arnaldo Carvalho de Melo <acme@redhat.com>
4f8a95309SArnaldo Carvalho de Melo  *
5f8a95309SArnaldo Carvalho de Melo  * Parts came from builtin-{top,stat,record}.c, see those files for further
6f8a95309SArnaldo Carvalho de Melo  * copyright notes.
7f8a95309SArnaldo Carvalho de Melo  */
8956fa571SArnaldo Carvalho de Melo #include <api/fs/fs.h>
9a43783aeSArnaldo Carvalho de Melo #include <errno.h>
10fd20e811SArnaldo Carvalho de Melo #include <inttypes.h>
115c581041SArnaldo Carvalho de Melo #include <poll.h>
12f8a95309SArnaldo Carvalho de Melo #include "cpumap.h"
13e0fcfb08SArnaldo Carvalho de Melo #include "util/mmap.h"
14f8a95309SArnaldo Carvalho de Melo #include "thread_map.h"
1512864b31SNamhyung Kim #include "target.h"
16361c99a6SArnaldo Carvalho de Melo #include "evlist.h"
17361c99a6SArnaldo Carvalho de Melo #include "evsel.h"
18e3e1a54fSAdrian Hunter #include "debug.h"
1958db1d6eSArnaldo Carvalho de Melo #include "units.h"
2020f2be1dSJiri Olsa #include <internal/lib.h> // page_size
21c1a604dfSArnaldo Carvalho de Melo #include "../perf.h"
2254cc54deSWang Nan #include "asm/bug.h"
23657ee553SSong Liu #include "bpf-event.h"
24da949f50SArnaldo Carvalho de Melo #include "util/string2.h"
259607ad3aSArnaldo Carvalho de Melo #include <signal.h>
2635b9d88eSArnaldo Carvalho de Melo #include <unistd.h>
27b397f846SArnaldo Carvalho de Melo #include <sched.h>
28f2a39fe8SArnaldo Carvalho de Melo #include <stdlib.h>
29361c99a6SArnaldo Carvalho de Melo 
3050d08e47SArnaldo Carvalho de Melo #include "parse-events.h"
314b6ab94eSJosh Poimboeuf #include <subcmd/parse-options.h>
3250d08e47SArnaldo Carvalho de Melo 
33bafae98eSArnaldo Carvalho de Melo #include <fcntl.h>
3486a5e0c2SArnaldo Carvalho de Melo #include <sys/ioctl.h>
35f8a95309SArnaldo Carvalho de Melo #include <sys/mman.h>
36f8a95309SArnaldo Carvalho de Melo 
3770db7533SArnaldo Carvalho de Melo #include <linux/bitops.h>
3870db7533SArnaldo Carvalho de Melo #include <linux/hash.h>
390389cd1fSArnaldo Carvalho de Melo #include <linux/log2.h>
408dd2a131SJiri Olsa #include <linux/err.h>
418520a98dSArnaldo Carvalho de Melo #include <linux/string.h>
427f7c536fSArnaldo Carvalho de Melo #include <linux/zalloc.h>
434562a739SJiri Olsa #include <perf/evlist.h>
4488761fa1SJiri Olsa #include <perf/evsel.h>
459c3516d1SJiri Olsa #include <perf/cpumap.h>
467728fa0cSJiri Olsa #include <perf/mmap.h>
4770db7533SArnaldo Carvalho de Melo 
48e14e5497SArnaldo Carvalho de Melo #include <internal/xyarray.h>
49e14e5497SArnaldo Carvalho de Melo 
50748fe088SArnaldo Carvalho de Melo #ifdef LACKS_SIGQUEUE_PROTOTYPE
51748fe088SArnaldo Carvalho de Melo int sigqueue(pid_t pid, int sig, const union sigval value);
52748fe088SArnaldo Carvalho de Melo #endif
53748fe088SArnaldo Carvalho de Melo 
549dfcb759SJiri Olsa #define FD(e, x, y) (*(int *)xyarray__entry(e->core.fd, x, y))
558cd36f3eSJiri Olsa #define SID(e, x, y) xyarray__entry(e->core.sample_id, x, y)
56f8a95309SArnaldo Carvalho de Melo 
5752c86bcaSJiri Olsa void evlist__init(struct evlist *evlist, struct perf_cpu_map *cpus,
589749b90eSJiri Olsa 		  struct perf_thread_map *threads)
59361c99a6SArnaldo Carvalho de Melo {
604562a739SJiri Olsa 	perf_evlist__init(&evlist->core);
61453fa030SJiri Olsa 	perf_evlist__set_maps(&evlist->core, cpus, threads);
6235b9d88eSArnaldo Carvalho de Melo 	evlist->workload.pid = -1;
6354cc54deSWang Nan 	evlist->bkw_mmap_state = BKW_MMAP_NOTREADY;
64361c99a6SArnaldo Carvalho de Melo }
65361c99a6SArnaldo Carvalho de Melo 
660f98b11cSJiri Olsa struct evlist *evlist__new(void)
67ef1d1af2SArnaldo Carvalho de Melo {
6863503dbaSJiri Olsa 	struct evlist *evlist = zalloc(sizeof(*evlist));
69ef1d1af2SArnaldo Carvalho de Melo 
70ef1d1af2SArnaldo Carvalho de Melo 	if (evlist != NULL)
7152c86bcaSJiri Olsa 		evlist__init(evlist, NULL, NULL);
72ef1d1af2SArnaldo Carvalho de Melo 
73361c99a6SArnaldo Carvalho de Melo 	return evlist;
74361c99a6SArnaldo Carvalho de Melo }
75361c99a6SArnaldo Carvalho de Melo 
7663503dbaSJiri Olsa struct evlist *perf_evlist__new_default(void)
77b22d54b0SJiri Olsa {
780f98b11cSJiri Olsa 	struct evlist *evlist = evlist__new();
79b22d54b0SJiri Olsa 
80b22d54b0SJiri Olsa 	if (evlist && perf_evlist__add_default(evlist)) {
81c12995a5SJiri Olsa 		evlist__delete(evlist);
82b22d54b0SJiri Olsa 		evlist = NULL;
83b22d54b0SJiri Olsa 	}
84b22d54b0SJiri Olsa 
85b22d54b0SJiri Olsa 	return evlist;
86b22d54b0SJiri Olsa }
87b22d54b0SJiri Olsa 
8863503dbaSJiri Olsa struct evlist *perf_evlist__new_dummy(void)
895bae0250SArnaldo Carvalho de Melo {
900f98b11cSJiri Olsa 	struct evlist *evlist = evlist__new();
915bae0250SArnaldo Carvalho de Melo 
925bae0250SArnaldo Carvalho de Melo 	if (evlist && perf_evlist__add_dummy(evlist)) {
93c12995a5SJiri Olsa 		evlist__delete(evlist);
945bae0250SArnaldo Carvalho de Melo 		evlist = NULL;
955bae0250SArnaldo Carvalho de Melo 	}
965bae0250SArnaldo Carvalho de Melo 
975bae0250SArnaldo Carvalho de Melo 	return evlist;
985bae0250SArnaldo Carvalho de Melo }
995bae0250SArnaldo Carvalho de Melo 
10075562573SAdrian Hunter /**
10175562573SAdrian Hunter  * perf_evlist__set_id_pos - set the positions of event ids.
10275562573SAdrian Hunter  * @evlist: selected event list
10375562573SAdrian Hunter  *
10475562573SAdrian Hunter  * Events with compatible sample types all have the same id_pos
10575562573SAdrian Hunter  * and is_pos.  For convenience, put a copy on evlist.
10675562573SAdrian Hunter  */
10763503dbaSJiri Olsa void perf_evlist__set_id_pos(struct evlist *evlist)
10875562573SAdrian Hunter {
109515dbe48SJiri Olsa 	struct evsel *first = evlist__first(evlist);
11075562573SAdrian Hunter 
11175562573SAdrian Hunter 	evlist->id_pos = first->id_pos;
11275562573SAdrian Hunter 	evlist->is_pos = first->is_pos;
11375562573SAdrian Hunter }
11475562573SAdrian Hunter 
11563503dbaSJiri Olsa static void perf_evlist__update_id_pos(struct evlist *evlist)
116733cd2feSAdrian Hunter {
11732dcd021SJiri Olsa 	struct evsel *evsel;
118733cd2feSAdrian Hunter 
119e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, evsel)
120733cd2feSAdrian Hunter 		perf_evsel__calc_id_pos(evsel);
121733cd2feSAdrian Hunter 
122733cd2feSAdrian Hunter 	perf_evlist__set_id_pos(evlist);
123733cd2feSAdrian Hunter }
124733cd2feSAdrian Hunter 
125e6b1878dSJiri Olsa static void evlist__purge(struct evlist *evlist)
126361c99a6SArnaldo Carvalho de Melo {
12732dcd021SJiri Olsa 	struct evsel *pos, *n;
128361c99a6SArnaldo Carvalho de Melo 
129e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry_safe(evlist, n, pos) {
130b27c4eceSJiri Olsa 		list_del_init(&pos->core.node);
131d49e4695SArnaldo Carvalho de Melo 		pos->evlist = NULL;
1325eb2dd2aSJiri Olsa 		evsel__delete(pos);
133361c99a6SArnaldo Carvalho de Melo 	}
134361c99a6SArnaldo Carvalho de Melo 
1356484d2f9SJiri Olsa 	evlist->core.nr_entries = 0;
136361c99a6SArnaldo Carvalho de Melo }
137361c99a6SArnaldo Carvalho de Melo 
138470579b0SJiri Olsa void evlist__exit(struct evlist *evlist)
139ef1d1af2SArnaldo Carvalho de Melo {
14004662523SArnaldo Carvalho de Melo 	zfree(&evlist->mmap);
1410b72d69aSWang Nan 	zfree(&evlist->overwrite_mmap);
14293dd6e28SJiri Olsa 	perf_evlist__exit(&evlist->core);
143ef1d1af2SArnaldo Carvalho de Melo }
144ef1d1af2SArnaldo Carvalho de Melo 
145c12995a5SJiri Olsa void evlist__delete(struct evlist *evlist)
146361c99a6SArnaldo Carvalho de Melo {
1470b04b3dcSArnaldo Carvalho de Melo 	if (evlist == NULL)
1480b04b3dcSArnaldo Carvalho de Melo 		return;
1490b04b3dcSArnaldo Carvalho de Melo 
150db6b7b13SJiri Olsa 	evlist__munmap(evlist);
151750b4edeSJiri Olsa 	evlist__close(evlist);
152e6b1878dSJiri Olsa 	evlist__purge(evlist);
153470579b0SJiri Olsa 	evlist__exit(evlist);
154361c99a6SArnaldo Carvalho de Melo 	free(evlist);
155361c99a6SArnaldo Carvalho de Melo }
156361c99a6SArnaldo Carvalho de Melo 
157a1cf3a75SJiri Olsa void evlist__add(struct evlist *evlist, struct evsel *entry)
158361c99a6SArnaldo Carvalho de Melo {
159d49e4695SArnaldo Carvalho de Melo 	entry->evlist = evlist;
1606484d2f9SJiri Olsa 	entry->idx = evlist->core.nr_entries;
16160b0896cSAdrian Hunter 	entry->tracking = !entry->idx;
162ef503831SArnaldo Carvalho de Melo 
1636484d2f9SJiri Olsa 	perf_evlist__add(&evlist->core, &entry->core);
1646484d2f9SJiri Olsa 
1656484d2f9SJiri Olsa 	if (evlist->core.nr_entries == 1)
16675562573SAdrian Hunter 		perf_evlist__set_id_pos(evlist);
167361c99a6SArnaldo Carvalho de Melo }
168361c99a6SArnaldo Carvalho de Melo 
16916251027SJiri Olsa void evlist__remove(struct evlist *evlist, struct evsel *evsel)
1704768230aSAdrian Hunter {
1714768230aSAdrian Hunter 	evsel->evlist = NULL;
17252e22fb8SJiri Olsa 	perf_evlist__remove(&evlist->core, &evsel->core);
1734768230aSAdrian Hunter }
1744768230aSAdrian Hunter 
17563503dbaSJiri Olsa void perf_evlist__splice_list_tail(struct evlist *evlist,
176f114d6efSAdrian Hunter 				   struct list_head *list)
17750d08e47SArnaldo Carvalho de Melo {
17832dcd021SJiri Olsa 	struct evsel *evsel, *temp;
17975562573SAdrian Hunter 
180e5cadb93SArnaldo Carvalho de Melo 	__evlist__for_each_entry_safe(list, temp, evsel) {
181b27c4eceSJiri Olsa 		list_del_init(&evsel->core.node);
182a1cf3a75SJiri Olsa 		evlist__add(evlist, evsel);
183f114d6efSAdrian Hunter 	}
18450d08e47SArnaldo Carvalho de Melo }
18550d08e47SArnaldo Carvalho de Melo 
186c0e53476SArnaldo Carvalho de Melo int __evlist__set_tracepoints_handlers(struct evlist *evlist,
187c0e53476SArnaldo Carvalho de Melo 				       const struct evsel_str_handler *assocs, size_t nr_assocs)
188c0e53476SArnaldo Carvalho de Melo {
189c0e53476SArnaldo Carvalho de Melo 	struct evsel *evsel;
190c0e53476SArnaldo Carvalho de Melo 	size_t i;
191c0e53476SArnaldo Carvalho de Melo 	int err;
192c0e53476SArnaldo Carvalho de Melo 
193c0e53476SArnaldo Carvalho de Melo 	for (i = 0; i < nr_assocs; i++) {
194c0e53476SArnaldo Carvalho de Melo 		// Adding a handler for an event not in this evlist, just ignore it.
195c0e53476SArnaldo Carvalho de Melo 		evsel = perf_evlist__find_tracepoint_by_name(evlist, assocs[i].name);
196c0e53476SArnaldo Carvalho de Melo 		if (evsel == NULL)
197c0e53476SArnaldo Carvalho de Melo 			continue;
198c0e53476SArnaldo Carvalho de Melo 
199c0e53476SArnaldo Carvalho de Melo 		err = -EEXIST;
200c0e53476SArnaldo Carvalho de Melo 		if (evsel->handler != NULL)
201c0e53476SArnaldo Carvalho de Melo 			goto out;
202c0e53476SArnaldo Carvalho de Melo 		evsel->handler = assocs[i].handler;
203c0e53476SArnaldo Carvalho de Melo 	}
204c0e53476SArnaldo Carvalho de Melo 
205c0e53476SArnaldo Carvalho de Melo 	err = 0;
206c0e53476SArnaldo Carvalho de Melo out:
207c0e53476SArnaldo Carvalho de Melo 	return err;
208c0e53476SArnaldo Carvalho de Melo }
209c0e53476SArnaldo Carvalho de Melo 
21063dab225SArnaldo Carvalho de Melo void __perf_evlist__set_leader(struct list_head *list)
21163dab225SArnaldo Carvalho de Melo {
21232dcd021SJiri Olsa 	struct evsel *evsel, *leader;
21363dab225SArnaldo Carvalho de Melo 
214b27c4eceSJiri Olsa 	leader = list_entry(list->next, struct evsel, core.node);
215b27c4eceSJiri Olsa 	evsel = list_entry(list->prev, struct evsel, core.node);
21697f63e4aSNamhyung Kim 
2175643b1a5SJiri Olsa 	leader->core.nr_members = evsel->idx - leader->idx + 1;
21863dab225SArnaldo Carvalho de Melo 
219e5cadb93SArnaldo Carvalho de Melo 	__evlist__for_each_entry(list, evsel) {
22063dab225SArnaldo Carvalho de Melo 		evsel->leader = leader;
22163dab225SArnaldo Carvalho de Melo 	}
22263dab225SArnaldo Carvalho de Melo }
22363dab225SArnaldo Carvalho de Melo 
22463503dbaSJiri Olsa void perf_evlist__set_leader(struct evlist *evlist)
2256a4bb04cSJiri Olsa {
2266484d2f9SJiri Olsa 	if (evlist->core.nr_entries) {
2276484d2f9SJiri Olsa 		evlist->nr_groups = evlist->core.nr_entries > 1 ? 1 : 0;
228ce9036a6SJiri Olsa 		__perf_evlist__set_leader(&evlist->core.entries);
2296a4bb04cSJiri Olsa 	}
23097f63e4aSNamhyung Kim }
2316a4bb04cSJiri Olsa 
23263503dbaSJiri Olsa int __perf_evlist__add_default(struct evlist *evlist, bool precise)
233361c99a6SArnaldo Carvalho de Melo {
23432dcd021SJiri Olsa 	struct evsel *evsel = perf_evsel__new_cycles(precise);
235361c99a6SArnaldo Carvalho de Melo 
236361c99a6SArnaldo Carvalho de Melo 	if (evsel == NULL)
2377c48dcfdSArnaldo Carvalho de Melo 		return -ENOMEM;
238361c99a6SArnaldo Carvalho de Melo 
239a1cf3a75SJiri Olsa 	evlist__add(evlist, evsel);
240361c99a6SArnaldo Carvalho de Melo 	return 0;
241361c99a6SArnaldo Carvalho de Melo }
2425c581041SArnaldo Carvalho de Melo 
24363503dbaSJiri Olsa int perf_evlist__add_dummy(struct evlist *evlist)
2445bae0250SArnaldo Carvalho de Melo {
2455bae0250SArnaldo Carvalho de Melo 	struct perf_event_attr attr = {
2465bae0250SArnaldo Carvalho de Melo 		.type	= PERF_TYPE_SOFTWARE,
2475bae0250SArnaldo Carvalho de Melo 		.config = PERF_COUNT_SW_DUMMY,
2485bae0250SArnaldo Carvalho de Melo 		.size	= sizeof(attr), /* to capture ABI version */
2495bae0250SArnaldo Carvalho de Melo 	};
2506484d2f9SJiri Olsa 	struct evsel *evsel = perf_evsel__new_idx(&attr, evlist->core.nr_entries);
2515bae0250SArnaldo Carvalho de Melo 
2525bae0250SArnaldo Carvalho de Melo 	if (evsel == NULL)
2535bae0250SArnaldo Carvalho de Melo 		return -ENOMEM;
2545bae0250SArnaldo Carvalho de Melo 
255a1cf3a75SJiri Olsa 	evlist__add(evlist, evsel);
2565bae0250SArnaldo Carvalho de Melo 	return 0;
2575bae0250SArnaldo Carvalho de Melo }
2585bae0250SArnaldo Carvalho de Melo 
259a1cf3a75SJiri Olsa static int evlist__add_attrs(struct evlist *evlist,
26050d08e47SArnaldo Carvalho de Melo 				  struct perf_event_attr *attrs, size_t nr_attrs)
26150d08e47SArnaldo Carvalho de Melo {
26232dcd021SJiri Olsa 	struct evsel *evsel, *n;
26350d08e47SArnaldo Carvalho de Melo 	LIST_HEAD(head);
26450d08e47SArnaldo Carvalho de Melo 	size_t i;
26550d08e47SArnaldo Carvalho de Melo 
26650d08e47SArnaldo Carvalho de Melo 	for (i = 0; i < nr_attrs; i++) {
2676484d2f9SJiri Olsa 		evsel = perf_evsel__new_idx(attrs + i, evlist->core.nr_entries + i);
26850d08e47SArnaldo Carvalho de Melo 		if (evsel == NULL)
26950d08e47SArnaldo Carvalho de Melo 			goto out_delete_partial_list;
270b27c4eceSJiri Olsa 		list_add_tail(&evsel->core.node, &head);
27150d08e47SArnaldo Carvalho de Melo 	}
27250d08e47SArnaldo Carvalho de Melo 
273f114d6efSAdrian Hunter 	perf_evlist__splice_list_tail(evlist, &head);
27450d08e47SArnaldo Carvalho de Melo 
27550d08e47SArnaldo Carvalho de Melo 	return 0;
27650d08e47SArnaldo Carvalho de Melo 
27750d08e47SArnaldo Carvalho de Melo out_delete_partial_list:
278e5cadb93SArnaldo Carvalho de Melo 	__evlist__for_each_entry_safe(&head, n, evsel)
2795eb2dd2aSJiri Olsa 		evsel__delete(evsel);
28050d08e47SArnaldo Carvalho de Melo 	return -1;
28150d08e47SArnaldo Carvalho de Melo }
28250d08e47SArnaldo Carvalho de Melo 
28363503dbaSJiri Olsa int __perf_evlist__add_default_attrs(struct evlist *evlist,
28479695e1bSArnaldo Carvalho de Melo 				     struct perf_event_attr *attrs, size_t nr_attrs)
28579695e1bSArnaldo Carvalho de Melo {
28679695e1bSArnaldo Carvalho de Melo 	size_t i;
28779695e1bSArnaldo Carvalho de Melo 
28879695e1bSArnaldo Carvalho de Melo 	for (i = 0; i < nr_attrs; i++)
28979695e1bSArnaldo Carvalho de Melo 		event_attr_init(attrs + i);
29079695e1bSArnaldo Carvalho de Melo 
291a1cf3a75SJiri Olsa 	return evlist__add_attrs(evlist, attrs, nr_attrs);
29279695e1bSArnaldo Carvalho de Melo }
29379695e1bSArnaldo Carvalho de Melo 
29432dcd021SJiri Olsa struct evsel *
29563503dbaSJiri Olsa perf_evlist__find_tracepoint_by_id(struct evlist *evlist, int id)
296ee29be62SArnaldo Carvalho de Melo {
29732dcd021SJiri Olsa 	struct evsel *evsel;
298ee29be62SArnaldo Carvalho de Melo 
299e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, evsel) {
3001fc632ceSJiri Olsa 		if (evsel->core.attr.type   == PERF_TYPE_TRACEPOINT &&
3011fc632ceSJiri Olsa 		    (int)evsel->core.attr.config == id)
302ee29be62SArnaldo Carvalho de Melo 			return evsel;
303ee29be62SArnaldo Carvalho de Melo 	}
304ee29be62SArnaldo Carvalho de Melo 
305ee29be62SArnaldo Carvalho de Melo 	return NULL;
306ee29be62SArnaldo Carvalho de Melo }
307ee29be62SArnaldo Carvalho de Melo 
30832dcd021SJiri Olsa struct evsel *
30963503dbaSJiri Olsa perf_evlist__find_tracepoint_by_name(struct evlist *evlist,
310a2f2804aSDavid Ahern 				     const char *name)
311a2f2804aSDavid Ahern {
31232dcd021SJiri Olsa 	struct evsel *evsel;
313a2f2804aSDavid Ahern 
314e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, evsel) {
3151fc632ceSJiri Olsa 		if ((evsel->core.attr.type == PERF_TYPE_TRACEPOINT) &&
316a2f2804aSDavid Ahern 		    (strcmp(evsel->name, name) == 0))
317a2f2804aSDavid Ahern 			return evsel;
318a2f2804aSDavid Ahern 	}
319a2f2804aSDavid Ahern 
320a2f2804aSDavid Ahern 	return NULL;
321a2f2804aSDavid Ahern }
322a2f2804aSDavid Ahern 
32363503dbaSJiri Olsa int perf_evlist__add_newtp(struct evlist *evlist,
32439876e7dSArnaldo Carvalho de Melo 			   const char *sys, const char *name, void *handler)
32539876e7dSArnaldo Carvalho de Melo {
32632dcd021SJiri Olsa 	struct evsel *evsel = perf_evsel__newtp(sys, name);
32739876e7dSArnaldo Carvalho de Melo 
3288dd2a131SJiri Olsa 	if (IS_ERR(evsel))
32939876e7dSArnaldo Carvalho de Melo 		return -1;
33039876e7dSArnaldo Carvalho de Melo 
331744a9719SArnaldo Carvalho de Melo 	evsel->handler = handler;
332a1cf3a75SJiri Olsa 	evlist__add(evlist, evsel);
33339876e7dSArnaldo Carvalho de Melo 	return 0;
33439876e7dSArnaldo Carvalho de Melo }
33539876e7dSArnaldo Carvalho de Melo 
33663503dbaSJiri Olsa static int perf_evlist__nr_threads(struct evlist *evlist,
33732dcd021SJiri Olsa 				   struct evsel *evsel)
338bf8e8f4bSAdrian Hunter {
339648b5af3SJiri Olsa 	if (evsel->core.system_wide)
340bf8e8f4bSAdrian Hunter 		return 1;
341bf8e8f4bSAdrian Hunter 	else
342a2f354e3SJiri Olsa 		return perf_thread_map__nr(evlist->core.threads);
343bf8e8f4bSAdrian Hunter }
344bf8e8f4bSAdrian Hunter 
345e74676deSJiri Olsa void evlist__disable(struct evlist *evlist)
3464152ab37SArnaldo Carvalho de Melo {
34732dcd021SJiri Olsa 	struct evsel *pos;
3484152ab37SArnaldo Carvalho de Melo 
349e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, pos) {
3509dfcb759SJiri Olsa 		if (pos->disabled || !perf_evsel__is_group_leader(pos) || !pos->core.fd)
3513fe4430dSJiri Olsa 			continue;
3529a10bb22SJiri Olsa 		evsel__disable(pos);
3534152ab37SArnaldo Carvalho de Melo 	}
3542b56bcfbSArnaldo Carvalho de Melo 
3552b56bcfbSArnaldo Carvalho de Melo 	evlist->enabled = false;
3564152ab37SArnaldo Carvalho de Melo }
3574152ab37SArnaldo Carvalho de Melo 
3581c87f165SJiri Olsa void evlist__enable(struct evlist *evlist)
359764e16a3SDavid Ahern {
36032dcd021SJiri Olsa 	struct evsel *pos;
361764e16a3SDavid Ahern 
362e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, pos) {
3639dfcb759SJiri Olsa 		if (!perf_evsel__is_group_leader(pos) || !pos->core.fd)
3643fe4430dSJiri Olsa 			continue;
365ec7f24efSJiri Olsa 		evsel__enable(pos);
366764e16a3SDavid Ahern 	}
3672b56bcfbSArnaldo Carvalho de Melo 
3682b56bcfbSArnaldo Carvalho de Melo 	evlist->enabled = true;
3692b56bcfbSArnaldo Carvalho de Melo }
3702b56bcfbSArnaldo Carvalho de Melo 
37163503dbaSJiri Olsa void perf_evlist__toggle_enable(struct evlist *evlist)
3722b56bcfbSArnaldo Carvalho de Melo {
373e74676deSJiri Olsa 	(evlist->enabled ? evlist__disable : evlist__enable)(evlist);
374764e16a3SDavid Ahern }
375764e16a3SDavid Ahern 
37663503dbaSJiri Olsa static int perf_evlist__enable_event_cpu(struct evlist *evlist,
37732dcd021SJiri Olsa 					 struct evsel *evsel, int cpu)
3781c65056cSAdrian Hunter {
37918ef15c6SArnaldo Carvalho de Melo 	int thread;
3801c65056cSAdrian Hunter 	int nr_threads = perf_evlist__nr_threads(evlist, evsel);
3811c65056cSAdrian Hunter 
3829dfcb759SJiri Olsa 	if (!evsel->core.fd)
3831c65056cSAdrian Hunter 		return -EINVAL;
3841c65056cSAdrian Hunter 
3851c65056cSAdrian Hunter 	for (thread = 0; thread < nr_threads; thread++) {
38618ef15c6SArnaldo Carvalho de Melo 		int err = ioctl(FD(evsel, cpu, thread), PERF_EVENT_IOC_ENABLE, 0);
3871c65056cSAdrian Hunter 		if (err)
3881c65056cSAdrian Hunter 			return err;
3891c65056cSAdrian Hunter 	}
3901c65056cSAdrian Hunter 	return 0;
3911c65056cSAdrian Hunter }
3921c65056cSAdrian Hunter 
39363503dbaSJiri Olsa static int perf_evlist__enable_event_thread(struct evlist *evlist,
39432dcd021SJiri Olsa 					    struct evsel *evsel,
3951c65056cSAdrian Hunter 					    int thread)
3961c65056cSAdrian Hunter {
39718ef15c6SArnaldo Carvalho de Melo 	int cpu;
3986549cd8fSJiri Olsa 	int nr_cpus = perf_cpu_map__nr(evlist->core.cpus);
3991c65056cSAdrian Hunter 
4009dfcb759SJiri Olsa 	if (!evsel->core.fd)
4011c65056cSAdrian Hunter 		return -EINVAL;
4021c65056cSAdrian Hunter 
4031c65056cSAdrian Hunter 	for (cpu = 0; cpu < nr_cpus; cpu++) {
40418ef15c6SArnaldo Carvalho de Melo 		int err = ioctl(FD(evsel, cpu, thread), PERF_EVENT_IOC_ENABLE, 0);
4051c65056cSAdrian Hunter 		if (err)
4061c65056cSAdrian Hunter 			return err;
4071c65056cSAdrian Hunter 	}
4081c65056cSAdrian Hunter 	return 0;
4091c65056cSAdrian Hunter }
4101c65056cSAdrian Hunter 
41163503dbaSJiri Olsa int perf_evlist__enable_event_idx(struct evlist *evlist,
41232dcd021SJiri Olsa 				  struct evsel *evsel, int idx)
4131c65056cSAdrian Hunter {
414315c0a1fSJiri Olsa 	bool per_cpu_mmaps = !perf_cpu_map__empty(evlist->core.cpus);
4151c65056cSAdrian Hunter 
4161c65056cSAdrian Hunter 	if (per_cpu_mmaps)
4171c65056cSAdrian Hunter 		return perf_evlist__enable_event_cpu(evlist, evsel, idx);
4181c65056cSAdrian Hunter 	else
4191c65056cSAdrian Hunter 		return perf_evlist__enable_event_thread(evlist, evsel, idx);
4201c65056cSAdrian Hunter }
4211c65056cSAdrian Hunter 
422f4009e7bSJiri Olsa int evlist__add_pollfd(struct evlist *evlist, int fd)
423e4b356b5SArnaldo Carvalho de Melo {
424f4009e7bSJiri Olsa 	return perf_evlist__add_pollfd(&evlist->core, fd, NULL, POLLIN);
425e4b356b5SArnaldo Carvalho de Melo }
4261b85337dSArnaldo Carvalho de Melo 
427f4009e7bSJiri Olsa int evlist__filter_pollfd(struct evlist *evlist, short revents_and_mask)
4281ddec7f0SArnaldo Carvalho de Melo {
42984227cb1SJiri Olsa 	return perf_evlist__filter_pollfd(&evlist->core, revents_and_mask);
4301ddec7f0SArnaldo Carvalho de Melo }
4311ddec7f0SArnaldo Carvalho de Melo 
43280ab2987SJiri Olsa int evlist__poll(struct evlist *evlist, int timeout)
433f66a889dSArnaldo Carvalho de Melo {
43480ab2987SJiri Olsa 	return perf_evlist__poll(&evlist->core, timeout);
435f66a889dSArnaldo Carvalho de Melo }
436f66a889dSArnaldo Carvalho de Melo 
43763503dbaSJiri Olsa struct perf_sample_id *perf_evlist__id2sid(struct evlist *evlist, u64 id)
43870db7533SArnaldo Carvalho de Melo {
43970db7533SArnaldo Carvalho de Melo 	struct hlist_head *head;
44070db7533SArnaldo Carvalho de Melo 	struct perf_sample_id *sid;
44170db7533SArnaldo Carvalho de Melo 	int hash;
44270db7533SArnaldo Carvalho de Melo 
44370db7533SArnaldo Carvalho de Melo 	hash = hash_64(id, PERF_EVLIST__HLIST_BITS);
4441d5af02dSJiri Olsa 	head = &evlist->core.heads[hash];
44570db7533SArnaldo Carvalho de Melo 
446b67bfe0dSSasha Levin 	hlist_for_each_entry(sid, head, node)
44770db7533SArnaldo Carvalho de Melo 		if (sid->id == id)
448932a3594SJiri Olsa 			return sid;
449932a3594SJiri Olsa 
450932a3594SJiri Olsa 	return NULL;
451932a3594SJiri Olsa }
452932a3594SJiri Olsa 
45363503dbaSJiri Olsa struct evsel *perf_evlist__id2evsel(struct evlist *evlist, u64 id)
454932a3594SJiri Olsa {
455932a3594SJiri Olsa 	struct perf_sample_id *sid;
456932a3594SJiri Olsa 
4576484d2f9SJiri Olsa 	if (evlist->core.nr_entries == 1 || !id)
458515dbe48SJiri Olsa 		return evlist__first(evlist);
459932a3594SJiri Olsa 
460932a3594SJiri Olsa 	sid = perf_evlist__id2sid(evlist, id);
461932a3594SJiri Olsa 	if (sid)
46270c20369SJiri Olsa 		return container_of(sid->evsel, struct evsel, core);
46330e68bccSNamhyung Kim 
46430e68bccSNamhyung Kim 	if (!perf_evlist__sample_id_all(evlist))
465515dbe48SJiri Olsa 		return evlist__first(evlist);
46630e68bccSNamhyung Kim 
46770db7533SArnaldo Carvalho de Melo 	return NULL;
46870db7533SArnaldo Carvalho de Melo }
46904391debSArnaldo Carvalho de Melo 
47063503dbaSJiri Olsa struct evsel *perf_evlist__id2evsel_strict(struct evlist *evlist,
471dddcf6abSAdrian Hunter 						u64 id)
472dddcf6abSAdrian Hunter {
473dddcf6abSAdrian Hunter 	struct perf_sample_id *sid;
474dddcf6abSAdrian Hunter 
475dddcf6abSAdrian Hunter 	if (!id)
476dddcf6abSAdrian Hunter 		return NULL;
477dddcf6abSAdrian Hunter 
478dddcf6abSAdrian Hunter 	sid = perf_evlist__id2sid(evlist, id);
479dddcf6abSAdrian Hunter 	if (sid)
48070c20369SJiri Olsa 		return container_of(sid->evsel, struct evsel, core);
481dddcf6abSAdrian Hunter 
482dddcf6abSAdrian Hunter 	return NULL;
483dddcf6abSAdrian Hunter }
484dddcf6abSAdrian Hunter 
48563503dbaSJiri Olsa static int perf_evlist__event2id(struct evlist *evlist,
48675562573SAdrian Hunter 				 union perf_event *event, u64 *id)
48775562573SAdrian Hunter {
488b1fcd190SJiri Olsa 	const __u64 *array = event->sample.array;
48975562573SAdrian Hunter 	ssize_t n;
49075562573SAdrian Hunter 
49175562573SAdrian Hunter 	n = (event->header.size - sizeof(event->header)) >> 3;
49275562573SAdrian Hunter 
49375562573SAdrian Hunter 	if (event->header.type == PERF_RECORD_SAMPLE) {
49475562573SAdrian Hunter 		if (evlist->id_pos >= n)
49575562573SAdrian Hunter 			return -1;
49675562573SAdrian Hunter 		*id = array[evlist->id_pos];
49775562573SAdrian Hunter 	} else {
49875562573SAdrian Hunter 		if (evlist->is_pos > n)
49975562573SAdrian Hunter 			return -1;
50075562573SAdrian Hunter 		n -= evlist->is_pos;
50175562573SAdrian Hunter 		*id = array[n];
50275562573SAdrian Hunter 	}
50375562573SAdrian Hunter 	return 0;
50475562573SAdrian Hunter }
50575562573SAdrian Hunter 
50663503dbaSJiri Olsa struct evsel *perf_evlist__event2evsel(struct evlist *evlist,
50775562573SAdrian Hunter 					    union perf_event *event)
50875562573SAdrian Hunter {
509515dbe48SJiri Olsa 	struct evsel *first = evlist__first(evlist);
51075562573SAdrian Hunter 	struct hlist_head *head;
51175562573SAdrian Hunter 	struct perf_sample_id *sid;
51275562573SAdrian Hunter 	int hash;
51375562573SAdrian Hunter 	u64 id;
51475562573SAdrian Hunter 
5156484d2f9SJiri Olsa 	if (evlist->core.nr_entries == 1)
51698be6966SAdrian Hunter 		return first;
51798be6966SAdrian Hunter 
5181fc632ceSJiri Olsa 	if (!first->core.attr.sample_id_all &&
51998be6966SAdrian Hunter 	    event->header.type != PERF_RECORD_SAMPLE)
52098be6966SAdrian Hunter 		return first;
52175562573SAdrian Hunter 
52275562573SAdrian Hunter 	if (perf_evlist__event2id(evlist, event, &id))
52375562573SAdrian Hunter 		return NULL;
52475562573SAdrian Hunter 
52575562573SAdrian Hunter 	/* Synthesized events have an id of zero */
52675562573SAdrian Hunter 	if (!id)
52798be6966SAdrian Hunter 		return first;
52875562573SAdrian Hunter 
52975562573SAdrian Hunter 	hash = hash_64(id, PERF_EVLIST__HLIST_BITS);
5301d5af02dSJiri Olsa 	head = &evlist->core.heads[hash];
53175562573SAdrian Hunter 
53275562573SAdrian Hunter 	hlist_for_each_entry(sid, head, node) {
53375562573SAdrian Hunter 		if (sid->id == id)
53470c20369SJiri Olsa 			return container_of(sid->evsel, struct evsel, core);
53575562573SAdrian Hunter 	}
53675562573SAdrian Hunter 	return NULL;
53775562573SAdrian Hunter }
53875562573SAdrian Hunter 
53963503dbaSJiri Olsa static int perf_evlist__set_paused(struct evlist *evlist, bool value)
54065aea233SWang Nan {
54165aea233SWang Nan 	int i;
54265aea233SWang Nan 
5430b72d69aSWang Nan 	if (!evlist->overwrite_mmap)
544078c3386SWang Nan 		return 0;
545078c3386SWang Nan 
546c976ee11SJiri Olsa 	for (i = 0; i < evlist->core.nr_mmaps; i++) {
5472cf07b29SJiri Olsa 		int fd = evlist->overwrite_mmap[i].core.fd;
54865aea233SWang Nan 		int err;
54965aea233SWang Nan 
55065aea233SWang Nan 		if (fd < 0)
55165aea233SWang Nan 			continue;
55265aea233SWang Nan 		err = ioctl(fd, PERF_EVENT_IOC_PAUSE_OUTPUT, value ? 1 : 0);
55365aea233SWang Nan 		if (err)
55465aea233SWang Nan 			return err;
55565aea233SWang Nan 	}
55665aea233SWang Nan 	return 0;
55765aea233SWang Nan }
55865aea233SWang Nan 
55963503dbaSJiri Olsa static int perf_evlist__pause(struct evlist *evlist)
56065aea233SWang Nan {
56165aea233SWang Nan 	return perf_evlist__set_paused(evlist, true);
56265aea233SWang Nan }
56365aea233SWang Nan 
56463503dbaSJiri Olsa static int perf_evlist__resume(struct evlist *evlist)
56565aea233SWang Nan {
56665aea233SWang Nan 	return perf_evlist__set_paused(evlist, false);
56765aea233SWang Nan }
56865aea233SWang Nan 
569db6b7b13SJiri Olsa static void evlist__munmap_nofree(struct evlist *evlist)
570f8a95309SArnaldo Carvalho de Melo {
571aece948fSArnaldo Carvalho de Melo 	int i;
572f8a95309SArnaldo Carvalho de Melo 
573b2cb615dSWang Nan 	if (evlist->mmap)
574c976ee11SJiri Olsa 		for (i = 0; i < evlist->core.nr_mmaps; i++)
57580e53d11SJiri Olsa 			perf_mmap__munmap(&evlist->mmap[i].core);
576b2cb615dSWang Nan 
5770b72d69aSWang Nan 	if (evlist->overwrite_mmap)
578c976ee11SJiri Olsa 		for (i = 0; i < evlist->core.nr_mmaps; i++)
57980e53d11SJiri Olsa 			perf_mmap__munmap(&evlist->overwrite_mmap[i].core);
580a1f72618SWang Nan }
581aece948fSArnaldo Carvalho de Melo 
582db6b7b13SJiri Olsa void evlist__munmap(struct evlist *evlist)
583a1f72618SWang Nan {
584db6b7b13SJiri Olsa 	evlist__munmap_nofree(evlist);
58504662523SArnaldo Carvalho de Melo 	zfree(&evlist->mmap);
5860b72d69aSWang Nan 	zfree(&evlist->overwrite_mmap);
587f8a95309SArnaldo Carvalho de Melo }
588f8a95309SArnaldo Carvalho de Melo 
58980e53d11SJiri Olsa static void perf_mmap__unmap_cb(struct perf_mmap *map)
59080e53d11SJiri Olsa {
59180e53d11SJiri Olsa 	struct mmap *m = container_of(map, struct mmap, core);
59280e53d11SJiri Olsa 
59380e53d11SJiri Olsa 	mmap__munmap(m);
59480e53d11SJiri Olsa }
59580e53d11SJiri Olsa 
596d50cf361SJiri Olsa static struct mmap *evlist__alloc_mmap(struct evlist *evlist,
5972c5f6d87SKan Liang 				       bool overwrite)
598f8a95309SArnaldo Carvalho de Melo {
599d4c6fb36SWang Nan 	int i;
600a5830532SJiri Olsa 	struct mmap *map;
601d4c6fb36SWang Nan 
602c976ee11SJiri Olsa 	evlist->core.nr_mmaps = perf_cpu_map__nr(evlist->core.cpus);
603315c0a1fSJiri Olsa 	if (perf_cpu_map__empty(evlist->core.cpus))
604c976ee11SJiri Olsa 		evlist->core.nr_mmaps = perf_thread_map__nr(evlist->core.threads);
605c976ee11SJiri Olsa 	map = zalloc(evlist->core.nr_mmaps * sizeof(struct mmap));
6068db6d6b1SWang Nan 	if (!map)
6078db6d6b1SWang Nan 		return NULL;
608946ae1d4SWang Nan 
609c976ee11SJiri Olsa 	for (i = 0; i < evlist->core.nr_mmaps; i++) {
6104738ca30SArnaldo Carvalho de Melo 		/*
6114738ca30SArnaldo Carvalho de Melo 		 * When the perf_mmap() call is made we grab one refcount, plus
6126afad54dSKan Liang 		 * one extra to let perf_mmap__consume() get the last
6134738ca30SArnaldo Carvalho de Melo 		 * events after all real references (perf_mmap__get()) are
6144738ca30SArnaldo Carvalho de Melo 		 * dropped.
6154738ca30SArnaldo Carvalho de Melo 		 *
6164738ca30SArnaldo Carvalho de Melo 		 * Each PERF_EVENT_IOC_SET_OUTPUT points to this mmap and
6174738ca30SArnaldo Carvalho de Melo 		 * thus does perf_mmap__get() on it.
6184738ca30SArnaldo Carvalho de Melo 		 */
61980e53d11SJiri Olsa 		perf_mmap__init(&map[i].core, overwrite, perf_mmap__unmap_cb);
6204738ca30SArnaldo Carvalho de Melo 	}
621353120b4SJiri Olsa 
6228db6d6b1SWang Nan 	return map;
623f8a95309SArnaldo Carvalho de Melo }
624f8a95309SArnaldo Carvalho de Melo 
6259abd2ab2SJiri Olsa static void
6269abd2ab2SJiri Olsa perf_evlist__mmap_cb_idx(struct perf_evlist *_evlist,
6279abd2ab2SJiri Olsa 			 struct perf_mmap_param *_mp,
6289abd2ab2SJiri Olsa 			 int idx, bool per_cpu)
6299abd2ab2SJiri Olsa {
6309abd2ab2SJiri Olsa 	struct evlist *evlist = container_of(_evlist, struct evlist, core);
6319abd2ab2SJiri Olsa 	struct mmap_params *mp = container_of(_mp, struct mmap_params, core);
6329abd2ab2SJiri Olsa 
6339abd2ab2SJiri Olsa 	auxtrace_mmap_params__set_idx(&mp->auxtrace_mp, evlist, idx, per_cpu);
6349abd2ab2SJiri Olsa }
6359abd2ab2SJiri Olsa 
636bb1b1885SJiri Olsa static struct perf_mmap*
637bb1b1885SJiri Olsa perf_evlist__mmap_cb_get(struct perf_evlist *_evlist, bool overwrite, int idx)
638bb1b1885SJiri Olsa {
639bb1b1885SJiri Olsa 	struct evlist *evlist = container_of(_evlist, struct evlist, core);
640bb1b1885SJiri Olsa 	struct mmap *maps = evlist->mmap;
641bb1b1885SJiri Olsa 
642bb1b1885SJiri Olsa 	if (overwrite) {
643bb1b1885SJiri Olsa 		maps = evlist->overwrite_mmap;
644bb1b1885SJiri Olsa 
645bb1b1885SJiri Olsa 		if (!maps) {
646bb1b1885SJiri Olsa 			maps = evlist__alloc_mmap(evlist, true);
647bb1b1885SJiri Olsa 			if (!maps)
648bb1b1885SJiri Olsa 				return NULL;
649bb1b1885SJiri Olsa 
650bb1b1885SJiri Olsa 			evlist->overwrite_mmap = maps;
651bb1b1885SJiri Olsa 			if (evlist->bkw_mmap_state == BKW_MMAP_NOTREADY)
652bb1b1885SJiri Olsa 				perf_evlist__toggle_bkw_mmap(evlist, BKW_MMAP_RUNNING);
653bb1b1885SJiri Olsa 		}
654bb1b1885SJiri Olsa 	}
655bb1b1885SJiri Olsa 
656bb1b1885SJiri Olsa 	return &maps[idx].core;
657bb1b1885SJiri Olsa }
658bb1b1885SJiri Olsa 
659b80132b1SJiri Olsa static int
660b80132b1SJiri Olsa perf_evlist__mmap_cb_mmap(struct perf_mmap *_map, struct perf_mmap_param *_mp,
661b80132b1SJiri Olsa 			  int output, int cpu)
662b80132b1SJiri Olsa {
663b80132b1SJiri Olsa 	struct mmap *map = container_of(_map, struct mmap, core);
664b80132b1SJiri Olsa 	struct mmap_params *mp = container_of(_mp, struct mmap_params, core);
665b80132b1SJiri Olsa 
666b80132b1SJiri Olsa 	return mmap__mmap(map, mp, output, cpu);
667b80132b1SJiri Olsa }
668b80132b1SJiri Olsa 
669f5e7150cSArnaldo Carvalho de Melo unsigned long perf_event_mlock_kb_in_pages(void)
670994a1f78SJiri Olsa {
671f5e7150cSArnaldo Carvalho de Melo 	unsigned long pages;
6728185e881SArnaldo Carvalho de Melo 	int max;
6738185e881SArnaldo Carvalho de Melo 
6748185e881SArnaldo Carvalho de Melo 	if (sysctl__read_int("kernel/perf_event_mlock_kb", &max) < 0) {
6758185e881SArnaldo Carvalho de Melo 		/*
6768185e881SArnaldo Carvalho de Melo 		 * Pick a once upon a time good value, i.e. things look
6778185e881SArnaldo Carvalho de Melo 		 * strange since we can't read a sysctl value, but lets not
6788185e881SArnaldo Carvalho de Melo 		 * die yet...
6798185e881SArnaldo Carvalho de Melo 		 */
6808185e881SArnaldo Carvalho de Melo 		max = 512;
6818185e881SArnaldo Carvalho de Melo 	} else {
6828185e881SArnaldo Carvalho de Melo 		max -= (page_size / 1024);
6838185e881SArnaldo Carvalho de Melo 	}
6848185e881SArnaldo Carvalho de Melo 
6858185e881SArnaldo Carvalho de Melo 	pages = (max * 1024) / page_size;
6861be300f4SArnaldo Carvalho de Melo 	if (!is_power_of_2(pages))
6871be300f4SArnaldo Carvalho de Melo 		pages = rounddown_pow_of_two(pages);
688f5e7150cSArnaldo Carvalho de Melo 
689f5e7150cSArnaldo Carvalho de Melo 	return pages;
690f5e7150cSArnaldo Carvalho de Melo }
691f5e7150cSArnaldo Carvalho de Melo 
6929521b5f2SJiri Olsa size_t evlist__mmap_size(unsigned long pages)
693f5e7150cSArnaldo Carvalho de Melo {
694f5e7150cSArnaldo Carvalho de Melo 	if (pages == UINT_MAX)
695f5e7150cSArnaldo Carvalho de Melo 		pages = perf_event_mlock_kb_in_pages();
696f5e7150cSArnaldo Carvalho de Melo 	else if (!is_power_of_2(pages))
697994a1f78SJiri Olsa 		return 0;
698994a1f78SJiri Olsa 
699994a1f78SJiri Olsa 	return (pages + 1) * page_size;
700994a1f78SJiri Olsa }
701994a1f78SJiri Olsa 
70233c2dcfdSDavid Ahern static long parse_pages_arg(const char *str, unsigned long min,
70333c2dcfdSDavid Ahern 			    unsigned long max)
704994a1f78SJiri Olsa {
7052fbe4abeSAdrian Hunter 	unsigned long pages, val;
70627050f53SJiri Olsa 	static struct parse_tag tags[] = {
70727050f53SJiri Olsa 		{ .tag  = 'B', .mult = 1       },
70827050f53SJiri Olsa 		{ .tag  = 'K', .mult = 1 << 10 },
70927050f53SJiri Olsa 		{ .tag  = 'M', .mult = 1 << 20 },
71027050f53SJiri Olsa 		{ .tag  = 'G', .mult = 1 << 30 },
71127050f53SJiri Olsa 		{ .tag  = 0 },
71227050f53SJiri Olsa 	};
713994a1f78SJiri Olsa 
7148973504bSDavid Ahern 	if (str == NULL)
71533c2dcfdSDavid Ahern 		return -EINVAL;
7168973504bSDavid Ahern 
71727050f53SJiri Olsa 	val = parse_tag_value(str, tags);
7182fbe4abeSAdrian Hunter 	if (val != (unsigned long) -1) {
71927050f53SJiri Olsa 		/* we got file size value */
72027050f53SJiri Olsa 		pages = PERF_ALIGN(val, page_size) / page_size;
72127050f53SJiri Olsa 	} else {
72227050f53SJiri Olsa 		/* we got pages count value */
72327050f53SJiri Olsa 		char *eptr;
724994a1f78SJiri Olsa 		pages = strtoul(str, &eptr, 10);
72533c2dcfdSDavid Ahern 		if (*eptr != '\0')
72633c2dcfdSDavid Ahern 			return -EINVAL;
72727050f53SJiri Olsa 	}
728994a1f78SJiri Olsa 
7292bcab6c1SAdrian Hunter 	if (pages == 0 && min == 0) {
73033c2dcfdSDavid Ahern 		/* leave number of pages at 0 */
7311dbfa938SAdrian Hunter 	} else if (!is_power_of_2(pages)) {
7329808143bSJiri Olsa 		char buf[100];
7339808143bSJiri Olsa 
73433c2dcfdSDavid Ahern 		/* round pages up to next power of 2 */
73591529834SArnaldo Carvalho de Melo 		pages = roundup_pow_of_two(pages);
7361dbfa938SAdrian Hunter 		if (!pages)
7371dbfa938SAdrian Hunter 			return -EINVAL;
7389808143bSJiri Olsa 
7399808143bSJiri Olsa 		unit_number__scnprintf(buf, sizeof(buf), pages * page_size);
7409808143bSJiri Olsa 		pr_info("rounding mmap pages size to %s (%lu pages)\n",
7419808143bSJiri Olsa 			buf, pages);
7422fbe4abeSAdrian Hunter 	}
7432fbe4abeSAdrian Hunter 
74433c2dcfdSDavid Ahern 	if (pages > max)
74533c2dcfdSDavid Ahern 		return -EINVAL;
74633c2dcfdSDavid Ahern 
74733c2dcfdSDavid Ahern 	return pages;
74833c2dcfdSDavid Ahern }
74933c2dcfdSDavid Ahern 
750e9db1310SAdrian Hunter int __perf_evlist__parse_mmap_pages(unsigned int *mmap_pages, const char *str)
75133c2dcfdSDavid Ahern {
75233c2dcfdSDavid Ahern 	unsigned long max = UINT_MAX;
75333c2dcfdSDavid Ahern 	long pages;
75433c2dcfdSDavid Ahern 
755f5ae9c42SAdrian Hunter 	if (max > SIZE_MAX / page_size)
75633c2dcfdSDavid Ahern 		max = SIZE_MAX / page_size;
75733c2dcfdSDavid Ahern 
75833c2dcfdSDavid Ahern 	pages = parse_pages_arg(str, 1, max);
75933c2dcfdSDavid Ahern 	if (pages < 0) {
76033c2dcfdSDavid Ahern 		pr_err("Invalid argument for --mmap_pages/-m\n");
761994a1f78SJiri Olsa 		return -1;
762994a1f78SJiri Olsa 	}
763994a1f78SJiri Olsa 
764994a1f78SJiri Olsa 	*mmap_pages = pages;
765994a1f78SJiri Olsa 	return 0;
766994a1f78SJiri Olsa }
767994a1f78SJiri Olsa 
768e9db1310SAdrian Hunter int perf_evlist__parse_mmap_pages(const struct option *opt, const char *str,
769e9db1310SAdrian Hunter 				  int unset __maybe_unused)
770e9db1310SAdrian Hunter {
771e9db1310SAdrian Hunter 	return __perf_evlist__parse_mmap_pages(opt->value, str);
772e9db1310SAdrian Hunter }
773e9db1310SAdrian Hunter 
774c83fa7f2SAdrian Hunter /**
7759521b5f2SJiri Olsa  * evlist__mmap_ex - Create mmaps to receive events.
776c83fa7f2SAdrian Hunter  * @evlist: list of events
777c83fa7f2SAdrian Hunter  * @pages: map length in pages
778c83fa7f2SAdrian Hunter  * @overwrite: overwrite older events?
779718c602dSAdrian Hunter  * @auxtrace_pages - auxtrace map length in pages
780718c602dSAdrian Hunter  * @auxtrace_overwrite - overwrite older auxtrace data?
781f8a95309SArnaldo Carvalho de Melo  *
782c83fa7f2SAdrian Hunter  * If @overwrite is %false the user needs to signal event consumption using
7839521b5f2SJiri Olsa  * perf_mmap__write_tail().  Using evlist__mmap_read() does this
784c83fa7f2SAdrian Hunter  * automatically.
785f8a95309SArnaldo Carvalho de Melo  *
786718c602dSAdrian Hunter  * Similarly, if @auxtrace_overwrite is %false the user needs to signal data
787718c602dSAdrian Hunter  * consumption using auxtrace_mmap__write_tail().
788718c602dSAdrian Hunter  *
789c83fa7f2SAdrian Hunter  * Return: %0 on success, negative error code otherwise.
790f8a95309SArnaldo Carvalho de Melo  */
7919521b5f2SJiri Olsa int evlist__mmap_ex(struct evlist *evlist, unsigned int pages,
7927a276ff6SWang Nan 			 unsigned int auxtrace_pages,
79351255a8aSAlexey Budankov 			 bool auxtrace_overwrite, int nr_cblocks, int affinity, int flush,
79451255a8aSAlexey Budankov 			 int comp_level)
795f8a95309SArnaldo Carvalho de Melo {
79671f566a3SWang Nan 	/*
79771f566a3SWang Nan 	 * Delay setting mp.prot: set it before calling perf_mmap__mmap.
79871f566a3SWang Nan 	 * Its value is decided by evsel's write_backward.
79971f566a3SWang Nan 	 * So &mp should not be passed through const pointer.
80071f566a3SWang Nan 	 */
801e440979fSJiri Olsa 	struct mmap_params mp = {
802e440979fSJiri Olsa 		.nr_cblocks	= nr_cblocks,
803e440979fSJiri Olsa 		.affinity	= affinity,
804e440979fSJiri Olsa 		.flush		= flush,
805e440979fSJiri Olsa 		.comp_level	= comp_level
806e440979fSJiri Olsa 	};
807923d0f18SJiri Olsa 	struct perf_evlist_mmap_ops ops = {
8089abd2ab2SJiri Olsa 		.idx  = perf_evlist__mmap_cb_idx,
809bb1b1885SJiri Olsa 		.get  = perf_evlist__mmap_cb_get,
810b80132b1SJiri Olsa 		.mmap = perf_evlist__mmap_cb_mmap,
8119abd2ab2SJiri Olsa 	};
81250a682ceSArnaldo Carvalho de Melo 
8138db6d6b1SWang Nan 	if (!evlist->mmap)
814d50cf361SJiri Olsa 		evlist->mmap = evlist__alloc_mmap(evlist, false);
8158db6d6b1SWang Nan 	if (!evlist->mmap)
816f8a95309SArnaldo Carvalho de Melo 		return -ENOMEM;
817f8a95309SArnaldo Carvalho de Melo 
818f6fa4375SJiri Olsa 	evlist->core.mmap_len = evlist__mmap_size(pages);
819f6fa4375SJiri Olsa 	pr_debug("mmap size %zuB\n", evlist->core.mmap_len);
820e440979fSJiri Olsa 	mp.core.mask = evlist->core.mmap_len - page_size - 1;
821f8a95309SArnaldo Carvalho de Melo 
822f6fa4375SJiri Olsa 	auxtrace_mmap_params__init(&mp.auxtrace_mp, evlist->core.mmap_len,
823718c602dSAdrian Hunter 				   auxtrace_pages, auxtrace_overwrite);
824718c602dSAdrian Hunter 
825923d0f18SJiri Olsa 	return perf_evlist__mmap_ops(&evlist->core, &ops, &mp.core);
826f8a95309SArnaldo Carvalho de Melo }
8277e2ed097SArnaldo Carvalho de Melo 
8289521b5f2SJiri Olsa int evlist__mmap(struct evlist *evlist, unsigned int pages)
829718c602dSAdrian Hunter {
8309521b5f2SJiri Olsa 	return evlist__mmap_ex(evlist, pages, 0, false, 0, PERF_AFFINITY_SYS, 1, 0);
831718c602dSAdrian Hunter }
832718c602dSAdrian Hunter 
83363503dbaSJiri Olsa int perf_evlist__create_maps(struct evlist *evlist, struct target *target)
8347e2ed097SArnaldo Carvalho de Melo {
835147c508fSJin Yao 	bool all_threads = (target->per_thread && target->system_wide);
836f854839bSJiri Olsa 	struct perf_cpu_map *cpus;
8379749b90eSJiri Olsa 	struct perf_thread_map *threads;
8387e2ed097SArnaldo Carvalho de Melo 
839147c508fSJin Yao 	/*
840147c508fSJin Yao 	 * If specify '-a' and '--per-thread' to perf record, perf record
841147c508fSJin Yao 	 * will override '--per-thread'. target->per_thread = false and
842147c508fSJin Yao 	 * target->system_wide = true.
843147c508fSJin Yao 	 *
844147c508fSJin Yao 	 * If specify '--per-thread' only to perf record,
845147c508fSJin Yao 	 * target->per_thread = true and target->system_wide = false.
846147c508fSJin Yao 	 *
847147c508fSJin Yao 	 * So target->per_thread && target->system_wide is false.
848147c508fSJin Yao 	 * For perf record, thread_map__new_str doesn't call
849147c508fSJin Yao 	 * thread_map__new_all_cpus. That will keep perf record's
850147c508fSJin Yao 	 * current behavior.
851147c508fSJin Yao 	 *
852147c508fSJin Yao 	 * For perf stat, it allows the case that target->per_thread and
853147c508fSJin Yao 	 * target->system_wide are all true. It means to collect system-wide
854147c508fSJin Yao 	 * per-thread data. thread_map__new_str will call
855147c508fSJin Yao 	 * thread_map__new_all_cpus to enumerate all threads.
856147c508fSJin Yao 	 */
85773c0ca1eSJin Yao 	threads = thread_map__new_str(target->pid, target->tid, target->uid,
858147c508fSJin Yao 				      all_threads);
85974bfd2b2SAdrian Hunter 
86074bfd2b2SAdrian Hunter 	if (!threads)
8617e2ed097SArnaldo Carvalho de Melo 		return -1;
8627e2ed097SArnaldo Carvalho de Melo 
8639c105fbcSDongsheng Yang 	if (target__uses_dummy_map(target))
864397721e0SJiri Olsa 		cpus = perf_cpu_map__dummy_new();
865879d77d0SNamhyung Kim 	else
8669c3516d1SJiri Olsa 		cpus = perf_cpu_map__new(target->cpu_list);
8677e2ed097SArnaldo Carvalho de Melo 
86874bfd2b2SAdrian Hunter 	if (!cpus)
8697e2ed097SArnaldo Carvalho de Melo 		goto out_delete_threads;
8707e2ed097SArnaldo Carvalho de Melo 
871ec903f26SJiri Olsa 	evlist->core.has_user_cpus = !!target->cpu_list;
872ec9a77a7SAdrian Hunter 
873453fa030SJiri Olsa 	perf_evlist__set_maps(&evlist->core, cpus, threads);
874d5bc056eSAdrian Hunter 
875d5bc056eSAdrian Hunter 	return 0;
8767e2ed097SArnaldo Carvalho de Melo 
8777e2ed097SArnaldo Carvalho de Melo out_delete_threads:
8787836e52eSJiri Olsa 	perf_thread_map__put(threads);
8797e2ed097SArnaldo Carvalho de Melo 	return -1;
8807e2ed097SArnaldo Carvalho de Melo }
8817e2ed097SArnaldo Carvalho de Melo 
88263503dbaSJiri Olsa void __perf_evlist__set_sample_bit(struct evlist *evlist,
88322c8a376SArnaldo Carvalho de Melo 				   enum perf_event_sample_format bit)
88422c8a376SArnaldo Carvalho de Melo {
88532dcd021SJiri Olsa 	struct evsel *evsel;
88622c8a376SArnaldo Carvalho de Melo 
887e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, evsel)
88822c8a376SArnaldo Carvalho de Melo 		__perf_evsel__set_sample_bit(evsel, bit);
88922c8a376SArnaldo Carvalho de Melo }
89022c8a376SArnaldo Carvalho de Melo 
89163503dbaSJiri Olsa void __perf_evlist__reset_sample_bit(struct evlist *evlist,
89222c8a376SArnaldo Carvalho de Melo 				     enum perf_event_sample_format bit)
89322c8a376SArnaldo Carvalho de Melo {
89432dcd021SJiri Olsa 	struct evsel *evsel;
89522c8a376SArnaldo Carvalho de Melo 
896e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, evsel)
89722c8a376SArnaldo Carvalho de Melo 		__perf_evsel__reset_sample_bit(evsel, bit);
89822c8a376SArnaldo Carvalho de Melo }
89922c8a376SArnaldo Carvalho de Melo 
90063503dbaSJiri Olsa int perf_evlist__apply_filters(struct evlist *evlist, struct evsel **err_evsel)
9010a102479SFrederic Weisbecker {
90232dcd021SJiri Olsa 	struct evsel *evsel;
903745cefc5SArnaldo Carvalho de Melo 	int err = 0;
9040a102479SFrederic Weisbecker 
905e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, evsel) {
906745cefc5SArnaldo Carvalho de Melo 		if (evsel->filter == NULL)
9070a102479SFrederic Weisbecker 			continue;
908745cefc5SArnaldo Carvalho de Melo 
909d988d5eeSKan Liang 		/*
910d988d5eeSKan Liang 		 * filters only work for tracepoint event, which doesn't have cpu limit.
911d988d5eeSKan Liang 		 * So evlist and evsel should always be same.
912d988d5eeSKan Liang 		 */
913a00571fdSJiri Olsa 		err = perf_evsel__apply_filter(&evsel->core, evsel->filter);
91423d4aad4SArnaldo Carvalho de Melo 		if (err) {
91523d4aad4SArnaldo Carvalho de Melo 			*err_evsel = evsel;
916745cefc5SArnaldo Carvalho de Melo 			break;
9170a102479SFrederic Weisbecker 		}
91823d4aad4SArnaldo Carvalho de Melo 	}
9190a102479SFrederic Weisbecker 
920745cefc5SArnaldo Carvalho de Melo 	return err;
921745cefc5SArnaldo Carvalho de Melo }
922745cefc5SArnaldo Carvalho de Melo 
92363503dbaSJiri Olsa int perf_evlist__set_tp_filter(struct evlist *evlist, const char *filter)
924745cefc5SArnaldo Carvalho de Melo {
92532dcd021SJiri Olsa 	struct evsel *evsel;
926745cefc5SArnaldo Carvalho de Melo 	int err = 0;
927745cefc5SArnaldo Carvalho de Melo 
92805cea449SArnaldo Carvalho de Melo 	if (filter == NULL)
92905cea449SArnaldo Carvalho de Melo 		return -1;
93005cea449SArnaldo Carvalho de Melo 
931e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, evsel) {
9321fc632ceSJiri Olsa 		if (evsel->core.attr.type != PERF_TYPE_TRACEPOINT)
933fdf14720SWang Nan 			continue;
934fdf14720SWang Nan 
93594ad89bcSArnaldo Carvalho de Melo 		err = perf_evsel__set_filter(evsel, filter);
936745cefc5SArnaldo Carvalho de Melo 		if (err)
937745cefc5SArnaldo Carvalho de Melo 			break;
938745cefc5SArnaldo Carvalho de Melo 	}
939745cefc5SArnaldo Carvalho de Melo 
940745cefc5SArnaldo Carvalho de Melo 	return err;
9410a102479SFrederic Weisbecker }
94274429964SFrederic Weisbecker 
94353c92f73SArnaldo Carvalho de Melo int perf_evlist__append_tp_filter(struct evlist *evlist, const char *filter)
94453c92f73SArnaldo Carvalho de Melo {
94553c92f73SArnaldo Carvalho de Melo 	struct evsel *evsel;
94653c92f73SArnaldo Carvalho de Melo 	int err = 0;
94753c92f73SArnaldo Carvalho de Melo 
94853c92f73SArnaldo Carvalho de Melo 	if (filter == NULL)
94953c92f73SArnaldo Carvalho de Melo 		return -1;
95053c92f73SArnaldo Carvalho de Melo 
95153c92f73SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, evsel) {
95253c92f73SArnaldo Carvalho de Melo 		if (evsel->core.attr.type != PERF_TYPE_TRACEPOINT)
95353c92f73SArnaldo Carvalho de Melo 			continue;
95453c92f73SArnaldo Carvalho de Melo 
95553c92f73SArnaldo Carvalho de Melo 		err = perf_evsel__append_tp_filter(evsel, filter);
95653c92f73SArnaldo Carvalho de Melo 		if (err)
95753c92f73SArnaldo Carvalho de Melo 			break;
95853c92f73SArnaldo Carvalho de Melo 	}
95953c92f73SArnaldo Carvalho de Melo 
96053c92f73SArnaldo Carvalho de Melo 	return err;
96153c92f73SArnaldo Carvalho de Melo }
96253c92f73SArnaldo Carvalho de Melo 
963da949f50SArnaldo Carvalho de Melo char *asprintf__tp_filter_pids(size_t npids, pid_t *pids)
964cfd70a26SArnaldo Carvalho de Melo {
965cfd70a26SArnaldo Carvalho de Melo 	char *filter;
966be199adaSArnaldo Carvalho de Melo 	size_t i;
967cfd70a26SArnaldo Carvalho de Melo 
968be199adaSArnaldo Carvalho de Melo 	for (i = 0; i < npids; ++i) {
969be199adaSArnaldo Carvalho de Melo 		if (i == 0) {
970be199adaSArnaldo Carvalho de Melo 			if (asprintf(&filter, "common_pid != %d", pids[i]) < 0)
97105cea449SArnaldo Carvalho de Melo 				return NULL;
972be199adaSArnaldo Carvalho de Melo 		} else {
973be199adaSArnaldo Carvalho de Melo 			char *tmp;
974be199adaSArnaldo Carvalho de Melo 
975be199adaSArnaldo Carvalho de Melo 			if (asprintf(&tmp, "%s && common_pid != %d", filter, pids[i]) < 0)
976be199adaSArnaldo Carvalho de Melo 				goto out_free;
977be199adaSArnaldo Carvalho de Melo 
978be199adaSArnaldo Carvalho de Melo 			free(filter);
979be199adaSArnaldo Carvalho de Melo 			filter = tmp;
980be199adaSArnaldo Carvalho de Melo 		}
981be199adaSArnaldo Carvalho de Melo 	}
982cfd70a26SArnaldo Carvalho de Melo 
98305cea449SArnaldo Carvalho de Melo 	return filter;
984be199adaSArnaldo Carvalho de Melo out_free:
985cfd70a26SArnaldo Carvalho de Melo 	free(filter);
98605cea449SArnaldo Carvalho de Melo 	return NULL;
98705cea449SArnaldo Carvalho de Melo }
98805cea449SArnaldo Carvalho de Melo 
98905cea449SArnaldo Carvalho de Melo int perf_evlist__set_tp_filter_pids(struct evlist *evlist, size_t npids, pid_t *pids)
99005cea449SArnaldo Carvalho de Melo {
99105cea449SArnaldo Carvalho de Melo 	char *filter = asprintf__tp_filter_pids(npids, pids);
99205cea449SArnaldo Carvalho de Melo 	int ret = perf_evlist__set_tp_filter(evlist, filter);
99305cea449SArnaldo Carvalho de Melo 
99405cea449SArnaldo Carvalho de Melo 	free(filter);
995cfd70a26SArnaldo Carvalho de Melo 	return ret;
996cfd70a26SArnaldo Carvalho de Melo }
997cfd70a26SArnaldo Carvalho de Melo 
99863503dbaSJiri Olsa int perf_evlist__set_tp_filter_pid(struct evlist *evlist, pid_t pid)
999be199adaSArnaldo Carvalho de Melo {
10007ad92a33SArnaldo Carvalho de Melo 	return perf_evlist__set_tp_filter_pids(evlist, 1, &pid);
1001be199adaSArnaldo Carvalho de Melo }
1002be199adaSArnaldo Carvalho de Melo 
10031827ab5bSArnaldo Carvalho de Melo int perf_evlist__append_tp_filter_pids(struct evlist *evlist, size_t npids, pid_t *pids)
10041827ab5bSArnaldo Carvalho de Melo {
10051827ab5bSArnaldo Carvalho de Melo 	char *filter = asprintf__tp_filter_pids(npids, pids);
10061827ab5bSArnaldo Carvalho de Melo 	int ret = perf_evlist__append_tp_filter(evlist, filter);
10071827ab5bSArnaldo Carvalho de Melo 
10081827ab5bSArnaldo Carvalho de Melo 	free(filter);
10091827ab5bSArnaldo Carvalho de Melo 	return ret;
10101827ab5bSArnaldo Carvalho de Melo }
10111827ab5bSArnaldo Carvalho de Melo 
10121827ab5bSArnaldo Carvalho de Melo int perf_evlist__append_tp_filter_pid(struct evlist *evlist, pid_t pid)
10131827ab5bSArnaldo Carvalho de Melo {
10141827ab5bSArnaldo Carvalho de Melo 	return perf_evlist__append_tp_filter_pids(evlist, 1, &pid);
10151827ab5bSArnaldo Carvalho de Melo }
10161827ab5bSArnaldo Carvalho de Melo 
101763503dbaSJiri Olsa bool perf_evlist__valid_sample_type(struct evlist *evlist)
101874429964SFrederic Weisbecker {
101932dcd021SJiri Olsa 	struct evsel *pos;
1020c2a70653SArnaldo Carvalho de Melo 
10216484d2f9SJiri Olsa 	if (evlist->core.nr_entries == 1)
102275562573SAdrian Hunter 		return true;
102375562573SAdrian Hunter 
102475562573SAdrian Hunter 	if (evlist->id_pos < 0 || evlist->is_pos < 0)
102575562573SAdrian Hunter 		return false;
102675562573SAdrian Hunter 
1027e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, pos) {
102875562573SAdrian Hunter 		if (pos->id_pos != evlist->id_pos ||
102975562573SAdrian Hunter 		    pos->is_pos != evlist->is_pos)
1030c2a70653SArnaldo Carvalho de Melo 			return false;
103174429964SFrederic Weisbecker 	}
103274429964SFrederic Weisbecker 
1033c2a70653SArnaldo Carvalho de Melo 	return true;
1034c2a70653SArnaldo Carvalho de Melo }
1035c2a70653SArnaldo Carvalho de Melo 
103663503dbaSJiri Olsa u64 __perf_evlist__combined_sample_type(struct evlist *evlist)
1037c2a70653SArnaldo Carvalho de Melo {
103832dcd021SJiri Olsa 	struct evsel *evsel;
103975562573SAdrian Hunter 
104075562573SAdrian Hunter 	if (evlist->combined_sample_type)
104175562573SAdrian Hunter 		return evlist->combined_sample_type;
104275562573SAdrian Hunter 
1043e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, evsel)
10441fc632ceSJiri Olsa 		evlist->combined_sample_type |= evsel->core.attr.sample_type;
104575562573SAdrian Hunter 
104675562573SAdrian Hunter 	return evlist->combined_sample_type;
104775562573SAdrian Hunter }
104875562573SAdrian Hunter 
104963503dbaSJiri Olsa u64 perf_evlist__combined_sample_type(struct evlist *evlist)
105075562573SAdrian Hunter {
105175562573SAdrian Hunter 	evlist->combined_sample_type = 0;
105275562573SAdrian Hunter 	return __perf_evlist__combined_sample_type(evlist);
1053c2a70653SArnaldo Carvalho de Melo }
1054c2a70653SArnaldo Carvalho de Melo 
105563503dbaSJiri Olsa u64 perf_evlist__combined_branch_type(struct evlist *evlist)
105698df858eSAndi Kleen {
105732dcd021SJiri Olsa 	struct evsel *evsel;
105898df858eSAndi Kleen 	u64 branch_type = 0;
105998df858eSAndi Kleen 
1060e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, evsel)
10611fc632ceSJiri Olsa 		branch_type |= evsel->core.attr.branch_sample_type;
106298df858eSAndi Kleen 	return branch_type;
106398df858eSAndi Kleen }
106498df858eSAndi Kleen 
106563503dbaSJiri Olsa bool perf_evlist__valid_read_format(struct evlist *evlist)
10669ede473cSJiri Olsa {
1067515dbe48SJiri Olsa 	struct evsel *first = evlist__first(evlist), *pos = first;
10681fc632ceSJiri Olsa 	u64 read_format = first->core.attr.read_format;
10691fc632ceSJiri Olsa 	u64 sample_type = first->core.attr.sample_type;
10709ede473cSJiri Olsa 
1071e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, pos) {
10721fc632ceSJiri Olsa 		if (read_format != pos->core.attr.read_format)
10739ede473cSJiri Olsa 			return false;
10749ede473cSJiri Olsa 	}
10759ede473cSJiri Olsa 
10769ede473cSJiri Olsa 	/* PERF_SAMPLE_READ imples PERF_FORMAT_ID. */
10779ede473cSJiri Olsa 	if ((sample_type & PERF_SAMPLE_READ) &&
10789ede473cSJiri Olsa 	    !(read_format & PERF_FORMAT_ID)) {
10799ede473cSJiri Olsa 		return false;
10809ede473cSJiri Olsa 	}
10819ede473cSJiri Olsa 
10829ede473cSJiri Olsa 	return true;
10839ede473cSJiri Olsa }
10849ede473cSJiri Olsa 
108563503dbaSJiri Olsa u16 perf_evlist__id_hdr_size(struct evlist *evlist)
108681e36bffSArnaldo Carvalho de Melo {
1087515dbe48SJiri Olsa 	struct evsel *first = evlist__first(evlist);
108881e36bffSArnaldo Carvalho de Melo 	struct perf_sample *data;
108981e36bffSArnaldo Carvalho de Melo 	u64 sample_type;
109081e36bffSArnaldo Carvalho de Melo 	u16 size = 0;
109181e36bffSArnaldo Carvalho de Melo 
10921fc632ceSJiri Olsa 	if (!first->core.attr.sample_id_all)
109381e36bffSArnaldo Carvalho de Melo 		goto out;
109481e36bffSArnaldo Carvalho de Melo 
10951fc632ceSJiri Olsa 	sample_type = first->core.attr.sample_type;
109681e36bffSArnaldo Carvalho de Melo 
109781e36bffSArnaldo Carvalho de Melo 	if (sample_type & PERF_SAMPLE_TID)
109881e36bffSArnaldo Carvalho de Melo 		size += sizeof(data->tid) * 2;
109981e36bffSArnaldo Carvalho de Melo 
110081e36bffSArnaldo Carvalho de Melo        if (sample_type & PERF_SAMPLE_TIME)
110181e36bffSArnaldo Carvalho de Melo 		size += sizeof(data->time);
110281e36bffSArnaldo Carvalho de Melo 
110381e36bffSArnaldo Carvalho de Melo 	if (sample_type & PERF_SAMPLE_ID)
110481e36bffSArnaldo Carvalho de Melo 		size += sizeof(data->id);
110581e36bffSArnaldo Carvalho de Melo 
110681e36bffSArnaldo Carvalho de Melo 	if (sample_type & PERF_SAMPLE_STREAM_ID)
110781e36bffSArnaldo Carvalho de Melo 		size += sizeof(data->stream_id);
110881e36bffSArnaldo Carvalho de Melo 
110981e36bffSArnaldo Carvalho de Melo 	if (sample_type & PERF_SAMPLE_CPU)
111081e36bffSArnaldo Carvalho de Melo 		size += sizeof(data->cpu) * 2;
111175562573SAdrian Hunter 
111275562573SAdrian Hunter 	if (sample_type & PERF_SAMPLE_IDENTIFIER)
111375562573SAdrian Hunter 		size += sizeof(data->id);
111481e36bffSArnaldo Carvalho de Melo out:
111581e36bffSArnaldo Carvalho de Melo 	return size;
111681e36bffSArnaldo Carvalho de Melo }
111781e36bffSArnaldo Carvalho de Melo 
111863503dbaSJiri Olsa bool perf_evlist__valid_sample_id_all(struct evlist *evlist)
1119c2a70653SArnaldo Carvalho de Melo {
1120515dbe48SJiri Olsa 	struct evsel *first = evlist__first(evlist), *pos = first;
1121c2a70653SArnaldo Carvalho de Melo 
1122e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry_continue(evlist, pos) {
11231fc632ceSJiri Olsa 		if (first->core.attr.sample_id_all != pos->core.attr.sample_id_all)
1124c2a70653SArnaldo Carvalho de Melo 			return false;
1125c2a70653SArnaldo Carvalho de Melo 	}
1126c2a70653SArnaldo Carvalho de Melo 
1127c2a70653SArnaldo Carvalho de Melo 	return true;
112874429964SFrederic Weisbecker }
112974429964SFrederic Weisbecker 
113063503dbaSJiri Olsa bool perf_evlist__sample_id_all(struct evlist *evlist)
113174429964SFrederic Weisbecker {
1132515dbe48SJiri Olsa 	struct evsel *first = evlist__first(evlist);
11331fc632ceSJiri Olsa 	return first->core.attr.sample_id_all;
113474429964SFrederic Weisbecker }
113581cce8deSArnaldo Carvalho de Melo 
113663503dbaSJiri Olsa void perf_evlist__set_selected(struct evlist *evlist,
113732dcd021SJiri Olsa 			       struct evsel *evsel)
113881cce8deSArnaldo Carvalho de Melo {
113981cce8deSArnaldo Carvalho de Melo 	evlist->selected = evsel;
114081cce8deSArnaldo Carvalho de Melo }
1141727ab04eSArnaldo Carvalho de Melo 
1142750b4edeSJiri Olsa void evlist__close(struct evlist *evlist)
1143a74b4b66SNamhyung Kim {
114432dcd021SJiri Olsa 	struct evsel *evsel;
1145a74b4b66SNamhyung Kim 
1146475fb533SAndi Kleen 	evlist__for_each_entry_reverse(evlist, evsel)
114788761fa1SJiri Olsa 		evsel__close(evsel);
1148a74b4b66SNamhyung Kim }
1149a74b4b66SNamhyung Kim 
115063503dbaSJiri Olsa static int perf_evlist__create_syswide_maps(struct evlist *evlist)
11514112eb18SArnaldo Carvalho de Melo {
1152f854839bSJiri Olsa 	struct perf_cpu_map *cpus;
11539749b90eSJiri Olsa 	struct perf_thread_map *threads;
11544112eb18SArnaldo Carvalho de Melo 	int err = -ENOMEM;
11554112eb18SArnaldo Carvalho de Melo 
11564112eb18SArnaldo Carvalho de Melo 	/*
11574112eb18SArnaldo Carvalho de Melo 	 * Try reading /sys/devices/system/cpu/online to get
11584112eb18SArnaldo Carvalho de Melo 	 * an all cpus map.
11594112eb18SArnaldo Carvalho de Melo 	 *
11604112eb18SArnaldo Carvalho de Melo 	 * FIXME: -ENOMEM is the best we can do here, the cpu_map
11614112eb18SArnaldo Carvalho de Melo 	 * code needs an overhaul to properly forward the
11624112eb18SArnaldo Carvalho de Melo 	 * error, and we may not want to do that fallback to a
11634112eb18SArnaldo Carvalho de Melo 	 * default cpu identity map :-\
11644112eb18SArnaldo Carvalho de Melo 	 */
11659c3516d1SJiri Olsa 	cpus = perf_cpu_map__new(NULL);
11668c0498b6SAdrian Hunter 	if (!cpus)
11674112eb18SArnaldo Carvalho de Melo 		goto out;
11684112eb18SArnaldo Carvalho de Melo 
11694b49cce2SJiri Olsa 	threads = perf_thread_map__new_dummy();
11708c0498b6SAdrian Hunter 	if (!threads)
11718c0498b6SAdrian Hunter 		goto out_put;
11724112eb18SArnaldo Carvalho de Melo 
1173453fa030SJiri Olsa 	perf_evlist__set_maps(&evlist->core, cpus, threads);
11744112eb18SArnaldo Carvalho de Melo out:
11754112eb18SArnaldo Carvalho de Melo 	return err;
11768c0498b6SAdrian Hunter out_put:
117738f01d8dSJiri Olsa 	perf_cpu_map__put(cpus);
11784112eb18SArnaldo Carvalho de Melo 	goto out;
11794112eb18SArnaldo Carvalho de Melo }
11804112eb18SArnaldo Carvalho de Melo 
1181474ddc4cSJiri Olsa int evlist__open(struct evlist *evlist)
1182727ab04eSArnaldo Carvalho de Melo {
118332dcd021SJiri Olsa 	struct evsel *evsel;
1184a74b4b66SNamhyung Kim 	int err;
1185727ab04eSArnaldo Carvalho de Melo 
11864112eb18SArnaldo Carvalho de Melo 	/*
11874112eb18SArnaldo Carvalho de Melo 	 * Default: one fd per CPU, all threads, aka systemwide
11884112eb18SArnaldo Carvalho de Melo 	 * as sys_perf_event_open(cpu = -1, thread = -1) is EINVAL
11894112eb18SArnaldo Carvalho de Melo 	 */
119003617c22SJiri Olsa 	if (evlist->core.threads == NULL && evlist->core.cpus == NULL) {
11914112eb18SArnaldo Carvalho de Melo 		err = perf_evlist__create_syswide_maps(evlist);
11924112eb18SArnaldo Carvalho de Melo 		if (err < 0)
11934112eb18SArnaldo Carvalho de Melo 			goto out_err;
11944112eb18SArnaldo Carvalho de Melo 	}
11954112eb18SArnaldo Carvalho de Melo 
1196733cd2feSAdrian Hunter 	perf_evlist__update_id_pos(evlist);
1197733cd2feSAdrian Hunter 
1198e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, evsel) {
1199af663bd0SJiri Olsa 		err = evsel__open(evsel, evsel->core.cpus, evsel->core.threads);
1200727ab04eSArnaldo Carvalho de Melo 		if (err < 0)
1201727ab04eSArnaldo Carvalho de Melo 			goto out_err;
1202727ab04eSArnaldo Carvalho de Melo 	}
1203727ab04eSArnaldo Carvalho de Melo 
1204727ab04eSArnaldo Carvalho de Melo 	return 0;
1205727ab04eSArnaldo Carvalho de Melo out_err:
1206750b4edeSJiri Olsa 	evlist__close(evlist);
120741c21a68SNamhyung Kim 	errno = -err;
1208727ab04eSArnaldo Carvalho de Melo 	return err;
1209727ab04eSArnaldo Carvalho de Melo }
121035b9d88eSArnaldo Carvalho de Melo 
121163503dbaSJiri Olsa int perf_evlist__prepare_workload(struct evlist *evlist, struct target *target,
121255e162eaSNamhyung Kim 				  const char *argv[], bool pipe_output,
1213735f7e0bSArnaldo Carvalho de Melo 				  void (*exec_error)(int signo, siginfo_t *info, void *ucontext))
121435b9d88eSArnaldo Carvalho de Melo {
121535b9d88eSArnaldo Carvalho de Melo 	int child_ready_pipe[2], go_pipe[2];
121635b9d88eSArnaldo Carvalho de Melo 	char bf;
121735b9d88eSArnaldo Carvalho de Melo 
121835b9d88eSArnaldo Carvalho de Melo 	if (pipe(child_ready_pipe) < 0) {
121935b9d88eSArnaldo Carvalho de Melo 		perror("failed to create 'ready' pipe");
122035b9d88eSArnaldo Carvalho de Melo 		return -1;
122135b9d88eSArnaldo Carvalho de Melo 	}
122235b9d88eSArnaldo Carvalho de Melo 
122335b9d88eSArnaldo Carvalho de Melo 	if (pipe(go_pipe) < 0) {
122435b9d88eSArnaldo Carvalho de Melo 		perror("failed to create 'go' pipe");
122535b9d88eSArnaldo Carvalho de Melo 		goto out_close_ready_pipe;
122635b9d88eSArnaldo Carvalho de Melo 	}
122735b9d88eSArnaldo Carvalho de Melo 
122835b9d88eSArnaldo Carvalho de Melo 	evlist->workload.pid = fork();
122935b9d88eSArnaldo Carvalho de Melo 	if (evlist->workload.pid < 0) {
123035b9d88eSArnaldo Carvalho de Melo 		perror("failed to fork");
123135b9d88eSArnaldo Carvalho de Melo 		goto out_close_pipes;
123235b9d88eSArnaldo Carvalho de Melo 	}
123335b9d88eSArnaldo Carvalho de Melo 
123435b9d88eSArnaldo Carvalho de Melo 	if (!evlist->workload.pid) {
12355f1c4225SArnaldo Carvalho de Melo 		int ret;
12365f1c4225SArnaldo Carvalho de Melo 
1237119fa3c9SNamhyung Kim 		if (pipe_output)
123835b9d88eSArnaldo Carvalho de Melo 			dup2(2, 1);
123935b9d88eSArnaldo Carvalho de Melo 
12400817df08SDavid Ahern 		signal(SIGTERM, SIG_DFL);
12410817df08SDavid Ahern 
124235b9d88eSArnaldo Carvalho de Melo 		close(child_ready_pipe[0]);
124335b9d88eSArnaldo Carvalho de Melo 		close(go_pipe[1]);
124435b9d88eSArnaldo Carvalho de Melo 		fcntl(go_pipe[0], F_SETFD, FD_CLOEXEC);
124535b9d88eSArnaldo Carvalho de Melo 
124635b9d88eSArnaldo Carvalho de Melo 		/*
124735b9d88eSArnaldo Carvalho de Melo 		 * Tell the parent we're ready to go
124835b9d88eSArnaldo Carvalho de Melo 		 */
124935b9d88eSArnaldo Carvalho de Melo 		close(child_ready_pipe[1]);
125035b9d88eSArnaldo Carvalho de Melo 
125135b9d88eSArnaldo Carvalho de Melo 		/*
125235b9d88eSArnaldo Carvalho de Melo 		 * Wait until the parent tells us to go.
125335b9d88eSArnaldo Carvalho de Melo 		 */
12545f1c4225SArnaldo Carvalho de Melo 		ret = read(go_pipe[0], &bf, 1);
12555f1c4225SArnaldo Carvalho de Melo 		/*
12565f1c4225SArnaldo Carvalho de Melo 		 * The parent will ask for the execvp() to be performed by
12575f1c4225SArnaldo Carvalho de Melo 		 * writing exactly one byte, in workload.cork_fd, usually via
12585f1c4225SArnaldo Carvalho de Melo 		 * perf_evlist__start_workload().
12595f1c4225SArnaldo Carvalho de Melo 		 *
126020f86fc1SArnaldo Carvalho de Melo 		 * For cancelling the workload without actually running it,
12615f1c4225SArnaldo Carvalho de Melo 		 * the parent will just close workload.cork_fd, without writing
12625f1c4225SArnaldo Carvalho de Melo 		 * anything, i.e. read will return zero and we just exit()
12635f1c4225SArnaldo Carvalho de Melo 		 * here.
12645f1c4225SArnaldo Carvalho de Melo 		 */
12655f1c4225SArnaldo Carvalho de Melo 		if (ret != 1) {
12665f1c4225SArnaldo Carvalho de Melo 			if (ret == -1)
126735b9d88eSArnaldo Carvalho de Melo 				perror("unable to read pipe");
12685f1c4225SArnaldo Carvalho de Melo 			exit(ret);
12695f1c4225SArnaldo Carvalho de Melo 		}
127035b9d88eSArnaldo Carvalho de Melo 
127135b9d88eSArnaldo Carvalho de Melo 		execvp(argv[0], (char **)argv);
127235b9d88eSArnaldo Carvalho de Melo 
1273735f7e0bSArnaldo Carvalho de Melo 		if (exec_error) {
1274f33cbe72SArnaldo Carvalho de Melo 			union sigval val;
1275f33cbe72SArnaldo Carvalho de Melo 
1276f33cbe72SArnaldo Carvalho de Melo 			val.sival_int = errno;
1277f33cbe72SArnaldo Carvalho de Melo 			if (sigqueue(getppid(), SIGUSR1, val))
127835b9d88eSArnaldo Carvalho de Melo 				perror(argv[0]);
1279f33cbe72SArnaldo Carvalho de Melo 		} else
1280f33cbe72SArnaldo Carvalho de Melo 			perror(argv[0]);
128135b9d88eSArnaldo Carvalho de Melo 		exit(-1);
128235b9d88eSArnaldo Carvalho de Melo 	}
128335b9d88eSArnaldo Carvalho de Melo 
1284735f7e0bSArnaldo Carvalho de Melo 	if (exec_error) {
1285735f7e0bSArnaldo Carvalho de Melo 		struct sigaction act = {
1286735f7e0bSArnaldo Carvalho de Melo 			.sa_flags     = SA_SIGINFO,
1287735f7e0bSArnaldo Carvalho de Melo 			.sa_sigaction = exec_error,
1288735f7e0bSArnaldo Carvalho de Melo 		};
1289735f7e0bSArnaldo Carvalho de Melo 		sigaction(SIGUSR1, &act, NULL);
1290735f7e0bSArnaldo Carvalho de Melo 	}
1291735f7e0bSArnaldo Carvalho de Melo 
12921aaf63b1SArnaldo Carvalho de Melo 	if (target__none(target)) {
129303617c22SJiri Olsa 		if (evlist->core.threads == NULL) {
12941aaf63b1SArnaldo Carvalho de Melo 			fprintf(stderr, "FATAL: evlist->threads need to be set at this point (%s:%d).\n",
12951aaf63b1SArnaldo Carvalho de Melo 				__func__, __LINE__);
12961aaf63b1SArnaldo Carvalho de Melo 			goto out_close_pipes;
12971aaf63b1SArnaldo Carvalho de Melo 		}
129803617c22SJiri Olsa 		perf_thread_map__set_pid(evlist->core.threads, 0, evlist->workload.pid);
12991aaf63b1SArnaldo Carvalho de Melo 	}
130035b9d88eSArnaldo Carvalho de Melo 
130135b9d88eSArnaldo Carvalho de Melo 	close(child_ready_pipe[1]);
130235b9d88eSArnaldo Carvalho de Melo 	close(go_pipe[0]);
130335b9d88eSArnaldo Carvalho de Melo 	/*
130435b9d88eSArnaldo Carvalho de Melo 	 * wait for child to settle
130535b9d88eSArnaldo Carvalho de Melo 	 */
130635b9d88eSArnaldo Carvalho de Melo 	if (read(child_ready_pipe[0], &bf, 1) == -1) {
130735b9d88eSArnaldo Carvalho de Melo 		perror("unable to read pipe");
130835b9d88eSArnaldo Carvalho de Melo 		goto out_close_pipes;
130935b9d88eSArnaldo Carvalho de Melo 	}
131035b9d88eSArnaldo Carvalho de Melo 
1311bcf3145fSNamhyung Kim 	fcntl(go_pipe[1], F_SETFD, FD_CLOEXEC);
131235b9d88eSArnaldo Carvalho de Melo 	evlist->workload.cork_fd = go_pipe[1];
131335b9d88eSArnaldo Carvalho de Melo 	close(child_ready_pipe[0]);
131435b9d88eSArnaldo Carvalho de Melo 	return 0;
131535b9d88eSArnaldo Carvalho de Melo 
131635b9d88eSArnaldo Carvalho de Melo out_close_pipes:
131735b9d88eSArnaldo Carvalho de Melo 	close(go_pipe[0]);
131835b9d88eSArnaldo Carvalho de Melo 	close(go_pipe[1]);
131935b9d88eSArnaldo Carvalho de Melo out_close_ready_pipe:
132035b9d88eSArnaldo Carvalho de Melo 	close(child_ready_pipe[0]);
132135b9d88eSArnaldo Carvalho de Melo 	close(child_ready_pipe[1]);
132235b9d88eSArnaldo Carvalho de Melo 	return -1;
132335b9d88eSArnaldo Carvalho de Melo }
132435b9d88eSArnaldo Carvalho de Melo 
132563503dbaSJiri Olsa int perf_evlist__start_workload(struct evlist *evlist)
132635b9d88eSArnaldo Carvalho de Melo {
132735b9d88eSArnaldo Carvalho de Melo 	if (evlist->workload.cork_fd > 0) {
1328b3824404SDavid Ahern 		char bf = 0;
1329bcf3145fSNamhyung Kim 		int ret;
133035b9d88eSArnaldo Carvalho de Melo 		/*
133135b9d88eSArnaldo Carvalho de Melo 		 * Remove the cork, let it rip!
133235b9d88eSArnaldo Carvalho de Melo 		 */
1333bcf3145fSNamhyung Kim 		ret = write(evlist->workload.cork_fd, &bf, 1);
1334bcf3145fSNamhyung Kim 		if (ret < 0)
1335e978be9eSSoramichi Akiyama 			perror("unable to write to pipe");
1336bcf3145fSNamhyung Kim 
1337bcf3145fSNamhyung Kim 		close(evlist->workload.cork_fd);
1338bcf3145fSNamhyung Kim 		return ret;
133935b9d88eSArnaldo Carvalho de Melo 	}
134035b9d88eSArnaldo Carvalho de Melo 
134135b9d88eSArnaldo Carvalho de Melo 	return 0;
134235b9d88eSArnaldo Carvalho de Melo }
1343cb0b29e0SArnaldo Carvalho de Melo 
134463503dbaSJiri Olsa int perf_evlist__parse_sample(struct evlist *evlist, union perf_event *event,
13450807d2d8SArnaldo Carvalho de Melo 			      struct perf_sample *sample)
1346cb0b29e0SArnaldo Carvalho de Melo {
134732dcd021SJiri Olsa 	struct evsel *evsel = perf_evlist__event2evsel(evlist, event);
134875562573SAdrian Hunter 
134975562573SAdrian Hunter 	if (!evsel)
135075562573SAdrian Hunter 		return -EFAULT;
13510807d2d8SArnaldo Carvalho de Melo 	return perf_evsel__parse_sample(evsel, event, sample);
1352cb0b29e0SArnaldo Carvalho de Melo }
135378f067b3SArnaldo Carvalho de Melo 
135463503dbaSJiri Olsa int perf_evlist__parse_sample_timestamp(struct evlist *evlist,
135501468120SJiri Olsa 					union perf_event *event,
135601468120SJiri Olsa 					u64 *timestamp)
135701468120SJiri Olsa {
135832dcd021SJiri Olsa 	struct evsel *evsel = perf_evlist__event2evsel(evlist, event);
135901468120SJiri Olsa 
136001468120SJiri Olsa 	if (!evsel)
136101468120SJiri Olsa 		return -EFAULT;
136201468120SJiri Olsa 	return perf_evsel__parse_sample_timestamp(evsel, event, timestamp);
136301468120SJiri Olsa }
136401468120SJiri Olsa 
136563503dbaSJiri Olsa int perf_evlist__strerror_open(struct evlist *evlist,
1366a8f23d8fSArnaldo Carvalho de Melo 			       int err, char *buf, size_t size)
1367a8f23d8fSArnaldo Carvalho de Melo {
1368a8f23d8fSArnaldo Carvalho de Melo 	int printed, value;
1369c8b5f2c9SArnaldo Carvalho de Melo 	char sbuf[STRERR_BUFSIZE], *emsg = str_error_r(err, sbuf, sizeof(sbuf));
1370a8f23d8fSArnaldo Carvalho de Melo 
1371a8f23d8fSArnaldo Carvalho de Melo 	switch (err) {
1372a8f23d8fSArnaldo Carvalho de Melo 	case EACCES:
1373a8f23d8fSArnaldo Carvalho de Melo 	case EPERM:
1374a8f23d8fSArnaldo Carvalho de Melo 		printed = scnprintf(buf, size,
1375a8f23d8fSArnaldo Carvalho de Melo 				    "Error:\t%s.\n"
1376a8f23d8fSArnaldo Carvalho de Melo 				    "Hint:\tCheck /proc/sys/kernel/perf_event_paranoid setting.", emsg);
1377a8f23d8fSArnaldo Carvalho de Melo 
13781a47245dSAdrian Hunter 		value = perf_event_paranoid();
1379a8f23d8fSArnaldo Carvalho de Melo 
1380a8f23d8fSArnaldo Carvalho de Melo 		printed += scnprintf(buf + printed, size - printed, "\nHint:\t");
1381a8f23d8fSArnaldo Carvalho de Melo 
1382a8f23d8fSArnaldo Carvalho de Melo 		if (value >= 2) {
1383a8f23d8fSArnaldo Carvalho de Melo 			printed += scnprintf(buf + printed, size - printed,
1384a8f23d8fSArnaldo Carvalho de Melo 					     "For your workloads it needs to be <= 1\nHint:\t");
1385a8f23d8fSArnaldo Carvalho de Melo 		}
1386a8f23d8fSArnaldo Carvalho de Melo 		printed += scnprintf(buf + printed, size - printed,
13875229e366SArnaldo Carvalho de Melo 				     "For system wide tracing it needs to be set to -1.\n");
1388a8f23d8fSArnaldo Carvalho de Melo 
1389a8f23d8fSArnaldo Carvalho de Melo 		printed += scnprintf(buf + printed, size - printed,
13905229e366SArnaldo Carvalho de Melo 				    "Hint:\tTry: 'sudo sh -c \"echo -1 > /proc/sys/kernel/perf_event_paranoid\"'\n"
13915229e366SArnaldo Carvalho de Melo 				    "Hint:\tThe current value is %d.", value);
1392a8f23d8fSArnaldo Carvalho de Melo 		break;
1393d9aade7fSArnaldo Carvalho de Melo 	case EINVAL: {
1394515dbe48SJiri Olsa 		struct evsel *first = evlist__first(evlist);
1395d9aade7fSArnaldo Carvalho de Melo 		int max_freq;
1396d9aade7fSArnaldo Carvalho de Melo 
1397d9aade7fSArnaldo Carvalho de Melo 		if (sysctl__read_int("kernel/perf_event_max_sample_rate", &max_freq) < 0)
1398d9aade7fSArnaldo Carvalho de Melo 			goto out_default;
1399d9aade7fSArnaldo Carvalho de Melo 
14001fc632ceSJiri Olsa 		if (first->core.attr.sample_freq < (u64)max_freq)
1401d9aade7fSArnaldo Carvalho de Melo 			goto out_default;
1402d9aade7fSArnaldo Carvalho de Melo 
1403d9aade7fSArnaldo Carvalho de Melo 		printed = scnprintf(buf, size,
1404d9aade7fSArnaldo Carvalho de Melo 				    "Error:\t%s.\n"
1405d9aade7fSArnaldo Carvalho de Melo 				    "Hint:\tCheck /proc/sys/kernel/perf_event_max_sample_rate.\n"
1406d9aade7fSArnaldo Carvalho de Melo 				    "Hint:\tThe current value is %d and %" PRIu64 " is being requested.",
14071fc632ceSJiri Olsa 				    emsg, max_freq, first->core.attr.sample_freq);
1408d9aade7fSArnaldo Carvalho de Melo 		break;
1409d9aade7fSArnaldo Carvalho de Melo 	}
1410a8f23d8fSArnaldo Carvalho de Melo 	default:
1411d9aade7fSArnaldo Carvalho de Melo out_default:
1412a8f23d8fSArnaldo Carvalho de Melo 		scnprintf(buf, size, "%s", emsg);
1413a8f23d8fSArnaldo Carvalho de Melo 		break;
1414a8f23d8fSArnaldo Carvalho de Melo 	}
1415a8f23d8fSArnaldo Carvalho de Melo 
1416a8f23d8fSArnaldo Carvalho de Melo 	return 0;
1417a8f23d8fSArnaldo Carvalho de Melo }
1418a025e4f0SAdrian Hunter 
141963503dbaSJiri Olsa int perf_evlist__strerror_mmap(struct evlist *evlist, int err, char *buf, size_t size)
1420956fa571SArnaldo Carvalho de Melo {
1421c8b5f2c9SArnaldo Carvalho de Melo 	char sbuf[STRERR_BUFSIZE], *emsg = str_error_r(err, sbuf, sizeof(sbuf));
1422f6fa4375SJiri Olsa 	int pages_attempted = evlist->core.mmap_len / 1024, pages_max_per_user, printed = 0;
1423956fa571SArnaldo Carvalho de Melo 
1424956fa571SArnaldo Carvalho de Melo 	switch (err) {
1425956fa571SArnaldo Carvalho de Melo 	case EPERM:
1426e5d4a290SArnaldo Carvalho de Melo 		sysctl__read_int("kernel/perf_event_mlock_kb", &pages_max_per_user);
1427e965bea1SArnaldo Carvalho de Melo 		printed += scnprintf(buf + printed, size - printed,
1428e965bea1SArnaldo Carvalho de Melo 				     "Error:\t%s.\n"
1429956fa571SArnaldo Carvalho de Melo 				     "Hint:\tCheck /proc/sys/kernel/perf_event_mlock_kb (%d kB) setting.\n"
1430e965bea1SArnaldo Carvalho de Melo 				     "Hint:\tTried using %zd kB.\n",
1431e5d4a290SArnaldo Carvalho de Melo 				     emsg, pages_max_per_user, pages_attempted);
1432e965bea1SArnaldo Carvalho de Melo 
1433e965bea1SArnaldo Carvalho de Melo 		if (pages_attempted >= pages_max_per_user) {
1434e965bea1SArnaldo Carvalho de Melo 			printed += scnprintf(buf + printed, size - printed,
1435e965bea1SArnaldo Carvalho de Melo 					     "Hint:\tTry 'sudo sh -c \"echo %d > /proc/sys/kernel/perf_event_mlock_kb\"', or\n",
1436e965bea1SArnaldo Carvalho de Melo 					     pages_max_per_user + pages_attempted);
1437e965bea1SArnaldo Carvalho de Melo 		}
1438e965bea1SArnaldo Carvalho de Melo 
1439e965bea1SArnaldo Carvalho de Melo 		printed += scnprintf(buf + printed, size - printed,
1440e965bea1SArnaldo Carvalho de Melo 				     "Hint:\tTry using a smaller -m/--mmap-pages value.");
1441956fa571SArnaldo Carvalho de Melo 		break;
1442956fa571SArnaldo Carvalho de Melo 	default:
1443956fa571SArnaldo Carvalho de Melo 		scnprintf(buf, size, "%s", emsg);
1444956fa571SArnaldo Carvalho de Melo 		break;
1445956fa571SArnaldo Carvalho de Melo 	}
1446956fa571SArnaldo Carvalho de Melo 
1447956fa571SArnaldo Carvalho de Melo 	return 0;
1448956fa571SArnaldo Carvalho de Melo }
1449956fa571SArnaldo Carvalho de Melo 
145063503dbaSJiri Olsa void perf_evlist__to_front(struct evlist *evlist,
145132dcd021SJiri Olsa 			   struct evsel *move_evsel)
1452a025e4f0SAdrian Hunter {
145332dcd021SJiri Olsa 	struct evsel *evsel, *n;
1454a025e4f0SAdrian Hunter 	LIST_HEAD(move);
1455a025e4f0SAdrian Hunter 
1456515dbe48SJiri Olsa 	if (move_evsel == evlist__first(evlist))
1457a025e4f0SAdrian Hunter 		return;
1458a025e4f0SAdrian Hunter 
1459e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry_safe(evlist, n, evsel) {
1460a025e4f0SAdrian Hunter 		if (evsel->leader == move_evsel->leader)
1461b27c4eceSJiri Olsa 			list_move_tail(&evsel->core.node, &move);
1462a025e4f0SAdrian Hunter 	}
1463a025e4f0SAdrian Hunter 
1464ce9036a6SJiri Olsa 	list_splice(&move, &evlist->core.entries);
1465a025e4f0SAdrian Hunter }
146660b0896cSAdrian Hunter 
146763503dbaSJiri Olsa void perf_evlist__set_tracking_event(struct evlist *evlist,
146832dcd021SJiri Olsa 				     struct evsel *tracking_evsel)
146960b0896cSAdrian Hunter {
147032dcd021SJiri Olsa 	struct evsel *evsel;
147160b0896cSAdrian Hunter 
147260b0896cSAdrian Hunter 	if (tracking_evsel->tracking)
147360b0896cSAdrian Hunter 		return;
147460b0896cSAdrian Hunter 
1475e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, evsel) {
147660b0896cSAdrian Hunter 		if (evsel != tracking_evsel)
147760b0896cSAdrian Hunter 			evsel->tracking = false;
147860b0896cSAdrian Hunter 	}
147960b0896cSAdrian Hunter 
148060b0896cSAdrian Hunter 	tracking_evsel->tracking = true;
148160b0896cSAdrian Hunter }
14827630b3e2SWang Nan 
148332dcd021SJiri Olsa struct evsel *
148463503dbaSJiri Olsa perf_evlist__find_evsel_by_str(struct evlist *evlist,
14857630b3e2SWang Nan 			       const char *str)
14867630b3e2SWang Nan {
148732dcd021SJiri Olsa 	struct evsel *evsel;
14887630b3e2SWang Nan 
1489e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, evsel) {
14907630b3e2SWang Nan 		if (!evsel->name)
14917630b3e2SWang Nan 			continue;
14927630b3e2SWang Nan 		if (strcmp(str, evsel->name) == 0)
14937630b3e2SWang Nan 			return evsel;
14947630b3e2SWang Nan 	}
14957630b3e2SWang Nan 
14967630b3e2SWang Nan 	return NULL;
14977630b3e2SWang Nan }
149854cc54deSWang Nan 
149963503dbaSJiri Olsa void perf_evlist__toggle_bkw_mmap(struct evlist *evlist,
150054cc54deSWang Nan 				  enum bkw_mmap_state state)
150154cc54deSWang Nan {
150254cc54deSWang Nan 	enum bkw_mmap_state old_state = evlist->bkw_mmap_state;
150354cc54deSWang Nan 	enum action {
150454cc54deSWang Nan 		NONE,
150554cc54deSWang Nan 		PAUSE,
150654cc54deSWang Nan 		RESUME,
150754cc54deSWang Nan 	} action = NONE;
150854cc54deSWang Nan 
15090b72d69aSWang Nan 	if (!evlist->overwrite_mmap)
151054cc54deSWang Nan 		return;
151154cc54deSWang Nan 
151254cc54deSWang Nan 	switch (old_state) {
151354cc54deSWang Nan 	case BKW_MMAP_NOTREADY: {
151454cc54deSWang Nan 		if (state != BKW_MMAP_RUNNING)
1515dd8bd53aSLuis de Bethencourt 			goto state_err;
151654cc54deSWang Nan 		break;
151754cc54deSWang Nan 	}
151854cc54deSWang Nan 	case BKW_MMAP_RUNNING: {
151954cc54deSWang Nan 		if (state != BKW_MMAP_DATA_PENDING)
152054cc54deSWang Nan 			goto state_err;
152154cc54deSWang Nan 		action = PAUSE;
152254cc54deSWang Nan 		break;
152354cc54deSWang Nan 	}
152454cc54deSWang Nan 	case BKW_MMAP_DATA_PENDING: {
152554cc54deSWang Nan 		if (state != BKW_MMAP_EMPTY)
152654cc54deSWang Nan 			goto state_err;
152754cc54deSWang Nan 		break;
152854cc54deSWang Nan 	}
152954cc54deSWang Nan 	case BKW_MMAP_EMPTY: {
153054cc54deSWang Nan 		if (state != BKW_MMAP_RUNNING)
153154cc54deSWang Nan 			goto state_err;
153254cc54deSWang Nan 		action = RESUME;
153354cc54deSWang Nan 		break;
153454cc54deSWang Nan 	}
153554cc54deSWang Nan 	default:
153654cc54deSWang Nan 		WARN_ONCE(1, "Shouldn't get there\n");
153754cc54deSWang Nan 	}
153854cc54deSWang Nan 
153954cc54deSWang Nan 	evlist->bkw_mmap_state = state;
154054cc54deSWang Nan 
154154cc54deSWang Nan 	switch (action) {
154254cc54deSWang Nan 	case PAUSE:
154354cc54deSWang Nan 		perf_evlist__pause(evlist);
154454cc54deSWang Nan 		break;
154554cc54deSWang Nan 	case RESUME:
154654cc54deSWang Nan 		perf_evlist__resume(evlist);
154754cc54deSWang Nan 		break;
154854cc54deSWang Nan 	case NONE:
154954cc54deSWang Nan 	default:
155054cc54deSWang Nan 		break;
155154cc54deSWang Nan 	}
155254cc54deSWang Nan 
155354cc54deSWang Nan state_err:
155454cc54deSWang Nan 	return;
155554cc54deSWang Nan }
155607d6f446SArnaldo Carvalho de Melo 
155763503dbaSJiri Olsa bool perf_evlist__exclude_kernel(struct evlist *evlist)
155807d6f446SArnaldo Carvalho de Melo {
155932dcd021SJiri Olsa 	struct evsel *evsel;
156007d6f446SArnaldo Carvalho de Melo 
156107d6f446SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, evsel) {
15621fc632ceSJiri Olsa 		if (!evsel->core.attr.exclude_kernel)
156307d6f446SArnaldo Carvalho de Melo 			return false;
156407d6f446SArnaldo Carvalho de Melo 	}
156507d6f446SArnaldo Carvalho de Melo 
156607d6f446SArnaldo Carvalho de Melo 	return true;
156707d6f446SArnaldo Carvalho de Melo }
1568e2bdbe80SJin Yao 
1569e2bdbe80SJin Yao /*
1570e2bdbe80SJin Yao  * Events in data file are not collect in groups, but we still want
1571e2bdbe80SJin Yao  * the group display. Set the artificial group and set the leader's
1572e2bdbe80SJin Yao  * forced_leader flag to notify the display code.
1573e2bdbe80SJin Yao  */
157463503dbaSJiri Olsa void perf_evlist__force_leader(struct evlist *evlist)
1575e2bdbe80SJin Yao {
1576e2bdbe80SJin Yao 	if (!evlist->nr_groups) {
1577515dbe48SJiri Olsa 		struct evsel *leader = evlist__first(evlist);
1578e2bdbe80SJin Yao 
1579e2bdbe80SJin Yao 		perf_evlist__set_leader(evlist);
1580e2bdbe80SJin Yao 		leader->forced_leader = true;
1581e2bdbe80SJin Yao 	}
1582e2bdbe80SJin Yao }
1583c3537fc2SAndi Kleen 
158463503dbaSJiri Olsa struct evsel *perf_evlist__reset_weak_group(struct evlist *evsel_list,
158532dcd021SJiri Olsa 						 struct evsel *evsel)
1586c3537fc2SAndi Kleen {
158732dcd021SJiri Olsa 	struct evsel *c2, *leader;
1588c3537fc2SAndi Kleen 	bool is_open = true;
1589c3537fc2SAndi Kleen 
1590c3537fc2SAndi Kleen 	leader = evsel->leader;
1591c3537fc2SAndi Kleen 	pr_debug("Weak group for %s/%d failed\n",
15925643b1a5SJiri Olsa 			leader->name, leader->core.nr_members);
1593c3537fc2SAndi Kleen 
1594c3537fc2SAndi Kleen 	/*
1595c3537fc2SAndi Kleen 	 * for_each_group_member doesn't work here because it doesn't
1596c3537fc2SAndi Kleen 	 * include the first entry.
1597c3537fc2SAndi Kleen 	 */
1598c3537fc2SAndi Kleen 	evlist__for_each_entry(evsel_list, c2) {
1599c3537fc2SAndi Kleen 		if (c2 == evsel)
1600c3537fc2SAndi Kleen 			is_open = false;
1601c3537fc2SAndi Kleen 		if (c2->leader == leader) {
1602c3537fc2SAndi Kleen 			if (is_open)
16035a40e199SAndi Kleen 				perf_evsel__close(&c2->core);
1604c3537fc2SAndi Kleen 			c2->leader = c2;
16055643b1a5SJiri Olsa 			c2->core.nr_members = 0;
1606c3537fc2SAndi Kleen 		}
1607c3537fc2SAndi Kleen 	}
1608c3537fc2SAndi Kleen 	return leader;
1609c3537fc2SAndi Kleen }
1610657ee553SSong Liu 
161163503dbaSJiri Olsa int perf_evlist__add_sb_event(struct evlist **evlist,
1612657ee553SSong Liu 			      struct perf_event_attr *attr,
1613657ee553SSong Liu 			      perf_evsel__sb_cb_t cb,
1614657ee553SSong Liu 			      void *data)
1615657ee553SSong Liu {
161632dcd021SJiri Olsa 	struct evsel *evsel;
1617657ee553SSong Liu 	bool new_evlist = (*evlist) == NULL;
1618657ee553SSong Liu 
1619657ee553SSong Liu 	if (*evlist == NULL)
16200f98b11cSJiri Olsa 		*evlist = evlist__new();
1621657ee553SSong Liu 	if (*evlist == NULL)
1622657ee553SSong Liu 		return -1;
1623657ee553SSong Liu 
1624657ee553SSong Liu 	if (!attr->sample_id_all) {
1625657ee553SSong Liu 		pr_warning("enabling sample_id_all for all side band events\n");
1626657ee553SSong Liu 		attr->sample_id_all = 1;
1627657ee553SSong Liu 	}
1628657ee553SSong Liu 
16296484d2f9SJiri Olsa 	evsel = perf_evsel__new_idx(attr, (*evlist)->core.nr_entries);
1630657ee553SSong Liu 	if (!evsel)
1631657ee553SSong Liu 		goto out_err;
1632657ee553SSong Liu 
1633657ee553SSong Liu 	evsel->side_band.cb = cb;
1634657ee553SSong Liu 	evsel->side_band.data = data;
1635a1cf3a75SJiri Olsa 	evlist__add(*evlist, evsel);
1636657ee553SSong Liu 	return 0;
1637657ee553SSong Liu 
1638657ee553SSong Liu out_err:
1639657ee553SSong Liu 	if (new_evlist) {
1640c12995a5SJiri Olsa 		evlist__delete(*evlist);
1641657ee553SSong Liu 		*evlist = NULL;
1642657ee553SSong Liu 	}
1643657ee553SSong Liu 	return -1;
1644657ee553SSong Liu }
1645657ee553SSong Liu 
1646657ee553SSong Liu static void *perf_evlist__poll_thread(void *arg)
1647657ee553SSong Liu {
164863503dbaSJiri Olsa 	struct evlist *evlist = arg;
1649657ee553SSong Liu 	bool draining = false;
1650adc6257cSJiri Olsa 	int i, done = 0;
1651b397f846SArnaldo Carvalho de Melo 	/*
1652b397f846SArnaldo Carvalho de Melo 	 * In order to read symbols from other namespaces perf to needs to call
1653b397f846SArnaldo Carvalho de Melo 	 * setns(2).  This isn't permitted if the struct_fs has multiple users.
1654b397f846SArnaldo Carvalho de Melo 	 * unshare(2) the fs so that we may continue to setns into namespaces
1655b397f846SArnaldo Carvalho de Melo 	 * that we're observing when, for instance, reading the build-ids at
1656b397f846SArnaldo Carvalho de Melo 	 * the end of a 'perf record' session.
1657b397f846SArnaldo Carvalho de Melo 	 */
1658b397f846SArnaldo Carvalho de Melo 	unshare(CLONE_FS);
1659657ee553SSong Liu 
1660adc6257cSJiri Olsa 	while (!done) {
1661adc6257cSJiri Olsa 		bool got_data = false;
1662adc6257cSJiri Olsa 
1663adc6257cSJiri Olsa 		if (evlist->thread.done)
1664657ee553SSong Liu 			draining = true;
1665657ee553SSong Liu 
1666657ee553SSong Liu 		if (!draining)
166780ab2987SJiri Olsa 			evlist__poll(evlist, 1000);
1668657ee553SSong Liu 
1669c976ee11SJiri Olsa 		for (i = 0; i < evlist->core.nr_mmaps; i++) {
1670a5830532SJiri Olsa 			struct mmap *map = &evlist->mmap[i];
1671657ee553SSong Liu 			union perf_event *event;
1672657ee553SSong Liu 
16737c4d4182SJiri Olsa 			if (perf_mmap__read_init(&map->core))
1674657ee553SSong Liu 				continue;
1675151ed5d7SJiri Olsa 			while ((event = perf_mmap__read_event(&map->core)) != NULL) {
167632dcd021SJiri Olsa 				struct evsel *evsel = perf_evlist__event2evsel(evlist, event);
1677657ee553SSong Liu 
1678657ee553SSong Liu 				if (evsel && evsel->side_band.cb)
1679657ee553SSong Liu 					evsel->side_band.cb(event, evsel->side_band.data);
1680657ee553SSong Liu 				else
1681657ee553SSong Liu 					pr_warning("cannot locate proper evsel for the side band event\n");
1682657ee553SSong Liu 
16837728fa0cSJiri Olsa 				perf_mmap__consume(&map->core);
1684adc6257cSJiri Olsa 				got_data = true;
1685657ee553SSong Liu 			}
168632fdc2caSJiri Olsa 			perf_mmap__read_done(&map->core);
1687657ee553SSong Liu 		}
1688adc6257cSJiri Olsa 
1689adc6257cSJiri Olsa 		if (draining && !got_data)
1690adc6257cSJiri Olsa 			break;
1691657ee553SSong Liu 	}
1692657ee553SSong Liu 	return NULL;
1693657ee553SSong Liu }
1694657ee553SSong Liu 
169563503dbaSJiri Olsa int perf_evlist__start_sb_thread(struct evlist *evlist,
1696657ee553SSong Liu 				 struct target *target)
1697657ee553SSong Liu {
169832dcd021SJiri Olsa 	struct evsel *counter;
1699657ee553SSong Liu 
1700657ee553SSong Liu 	if (!evlist)
1701657ee553SSong Liu 		return 0;
1702657ee553SSong Liu 
1703657ee553SSong Liu 	if (perf_evlist__create_maps(evlist, target))
1704657ee553SSong Liu 		goto out_delete_evlist;
1705657ee553SSong Liu 
1706657ee553SSong Liu 	evlist__for_each_entry(evlist, counter) {
1707f72f901dSJiri Olsa 		if (evsel__open(counter, evlist->core.cpus,
170803617c22SJiri Olsa 				     evlist->core.threads) < 0)
1709657ee553SSong Liu 			goto out_delete_evlist;
1710657ee553SSong Liu 	}
1711657ee553SSong Liu 
17129521b5f2SJiri Olsa 	if (evlist__mmap(evlist, UINT_MAX))
1713657ee553SSong Liu 		goto out_delete_evlist;
1714657ee553SSong Liu 
1715657ee553SSong Liu 	evlist__for_each_entry(evlist, counter) {
1716ec7f24efSJiri Olsa 		if (evsel__enable(counter))
1717657ee553SSong Liu 			goto out_delete_evlist;
1718657ee553SSong Liu 	}
1719657ee553SSong Liu 
1720657ee553SSong Liu 	evlist->thread.done = 0;
1721657ee553SSong Liu 	if (pthread_create(&evlist->thread.th, NULL, perf_evlist__poll_thread, evlist))
1722657ee553SSong Liu 		goto out_delete_evlist;
1723657ee553SSong Liu 
1724657ee553SSong Liu 	return 0;
1725657ee553SSong Liu 
1726657ee553SSong Liu out_delete_evlist:
1727c12995a5SJiri Olsa 	evlist__delete(evlist);
1728657ee553SSong Liu 	evlist = NULL;
1729657ee553SSong Liu 	return -1;
1730657ee553SSong Liu }
1731657ee553SSong Liu 
173263503dbaSJiri Olsa void perf_evlist__stop_sb_thread(struct evlist *evlist)
1733657ee553SSong Liu {
1734657ee553SSong Liu 	if (!evlist)
1735657ee553SSong Liu 		return;
1736657ee553SSong Liu 	evlist->thread.done = 1;
1737657ee553SSong Liu 	pthread_join(evlist->thread.th, NULL);
1738c12995a5SJiri Olsa 	evlist__delete(evlist);
1739657ee553SSong Liu }
1740