xref: /openbmc/linux/tools/perf/util/evlist.c (revision 86a5e0c2)
1f8a95309SArnaldo Carvalho de Melo /*
2f8a95309SArnaldo Carvalho de Melo  * Copyright (C) 2011, Red Hat Inc, Arnaldo Carvalho de Melo <acme@redhat.com>
3f8a95309SArnaldo Carvalho de Melo  *
4f8a95309SArnaldo Carvalho de Melo  * Parts came from builtin-{top,stat,record}.c, see those files for further
5f8a95309SArnaldo Carvalho de Melo  * copyright notes.
6f8a95309SArnaldo Carvalho de Melo  *
7f8a95309SArnaldo Carvalho de Melo  * Released under the GPL v2. (and only v2, not any later version)
8f8a95309SArnaldo Carvalho de Melo  */
9a8c9ae18SArnaldo Carvalho de Melo #include "util.h"
10956fa571SArnaldo Carvalho de Melo #include <api/fs/fs.h>
11a43783aeSArnaldo Carvalho de Melo #include <errno.h>
12fd20e811SArnaldo Carvalho de Melo #include <inttypes.h>
135c581041SArnaldo Carvalho de Melo #include <poll.h>
14f8a95309SArnaldo Carvalho de Melo #include "cpumap.h"
15f8a95309SArnaldo Carvalho de Melo #include "thread_map.h"
1612864b31SNamhyung Kim #include "target.h"
17361c99a6SArnaldo Carvalho de Melo #include "evlist.h"
18361c99a6SArnaldo Carvalho de Melo #include "evsel.h"
19e3e1a54fSAdrian Hunter #include "debug.h"
2058db1d6eSArnaldo Carvalho de Melo #include "units.h"
2154cc54deSWang Nan #include "asm/bug.h"
229607ad3aSArnaldo Carvalho de Melo #include <signal.h>
2335b9d88eSArnaldo Carvalho de Melo #include <unistd.h>
24361c99a6SArnaldo Carvalho de Melo 
2550d08e47SArnaldo Carvalho de Melo #include "parse-events.h"
264b6ab94eSJosh Poimboeuf #include <subcmd/parse-options.h>
2750d08e47SArnaldo Carvalho de Melo 
2886a5e0c2SArnaldo Carvalho de Melo #include <sys/ioctl.h>
29f8a95309SArnaldo Carvalho de Melo #include <sys/mman.h>
30f8a95309SArnaldo Carvalho de Melo 
3170db7533SArnaldo Carvalho de Melo #include <linux/bitops.h>
3270db7533SArnaldo Carvalho de Melo #include <linux/hash.h>
330389cd1fSArnaldo Carvalho de Melo #include <linux/log2.h>
348dd2a131SJiri Olsa #include <linux/err.h>
3570db7533SArnaldo Carvalho de Melo 
368db6d6b1SWang Nan static void perf_mmap__munmap(struct perf_mmap *map);
374876075bSWang Nan static void perf_mmap__put(struct perf_mmap *map);
38e4b356b5SArnaldo Carvalho de Melo 
39f8a95309SArnaldo Carvalho de Melo #define FD(e, x, y) (*(int *)xyarray__entry(e->fd, x, y))
40a91e5431SArnaldo Carvalho de Melo #define SID(e, x, y) xyarray__entry(e->sample_id, x, y)
41f8a95309SArnaldo Carvalho de Melo 
427e2ed097SArnaldo Carvalho de Melo void perf_evlist__init(struct perf_evlist *evlist, struct cpu_map *cpus,
437e2ed097SArnaldo Carvalho de Melo 		       struct thread_map *threads)
44361c99a6SArnaldo Carvalho de Melo {
4570db7533SArnaldo Carvalho de Melo 	int i;
4670db7533SArnaldo Carvalho de Melo 
4770db7533SArnaldo Carvalho de Melo 	for (i = 0; i < PERF_EVLIST__HLIST_SIZE; ++i)
4870db7533SArnaldo Carvalho de Melo 		INIT_HLIST_HEAD(&evlist->heads[i]);
49361c99a6SArnaldo Carvalho de Melo 	INIT_LIST_HEAD(&evlist->entries);
507e2ed097SArnaldo Carvalho de Melo 	perf_evlist__set_maps(evlist, cpus, threads);
511b85337dSArnaldo Carvalho de Melo 	fdarray__init(&evlist->pollfd, 64);
5235b9d88eSArnaldo Carvalho de Melo 	evlist->workload.pid = -1;
5354cc54deSWang Nan 	evlist->bkw_mmap_state = BKW_MMAP_NOTREADY;
54361c99a6SArnaldo Carvalho de Melo }
55361c99a6SArnaldo Carvalho de Melo 
56334fe7a3SNamhyung Kim struct perf_evlist *perf_evlist__new(void)
57ef1d1af2SArnaldo Carvalho de Melo {
58ef1d1af2SArnaldo Carvalho de Melo 	struct perf_evlist *evlist = zalloc(sizeof(*evlist));
59ef1d1af2SArnaldo Carvalho de Melo 
60ef1d1af2SArnaldo Carvalho de Melo 	if (evlist != NULL)
61334fe7a3SNamhyung Kim 		perf_evlist__init(evlist, NULL, NULL);
62ef1d1af2SArnaldo Carvalho de Melo 
63361c99a6SArnaldo Carvalho de Melo 	return evlist;
64361c99a6SArnaldo Carvalho de Melo }
65361c99a6SArnaldo Carvalho de Melo 
66b22d54b0SJiri Olsa struct perf_evlist *perf_evlist__new_default(void)
67b22d54b0SJiri Olsa {
68b22d54b0SJiri Olsa 	struct perf_evlist *evlist = perf_evlist__new();
69b22d54b0SJiri Olsa 
70b22d54b0SJiri Olsa 	if (evlist && perf_evlist__add_default(evlist)) {
71b22d54b0SJiri Olsa 		perf_evlist__delete(evlist);
72b22d54b0SJiri Olsa 		evlist = NULL;
73b22d54b0SJiri Olsa 	}
74b22d54b0SJiri Olsa 
75b22d54b0SJiri Olsa 	return evlist;
76b22d54b0SJiri Olsa }
77b22d54b0SJiri Olsa 
785bae0250SArnaldo Carvalho de Melo struct perf_evlist *perf_evlist__new_dummy(void)
795bae0250SArnaldo Carvalho de Melo {
805bae0250SArnaldo Carvalho de Melo 	struct perf_evlist *evlist = perf_evlist__new();
815bae0250SArnaldo Carvalho de Melo 
825bae0250SArnaldo Carvalho de Melo 	if (evlist && perf_evlist__add_dummy(evlist)) {
835bae0250SArnaldo Carvalho de Melo 		perf_evlist__delete(evlist);
845bae0250SArnaldo Carvalho de Melo 		evlist = NULL;
855bae0250SArnaldo Carvalho de Melo 	}
865bae0250SArnaldo Carvalho de Melo 
875bae0250SArnaldo Carvalho de Melo 	return evlist;
885bae0250SArnaldo Carvalho de Melo }
895bae0250SArnaldo Carvalho de Melo 
9075562573SAdrian Hunter /**
9175562573SAdrian Hunter  * perf_evlist__set_id_pos - set the positions of event ids.
9275562573SAdrian Hunter  * @evlist: selected event list
9375562573SAdrian Hunter  *
9475562573SAdrian Hunter  * Events with compatible sample types all have the same id_pos
9575562573SAdrian Hunter  * and is_pos.  For convenience, put a copy on evlist.
9675562573SAdrian Hunter  */
9775562573SAdrian Hunter void perf_evlist__set_id_pos(struct perf_evlist *evlist)
9875562573SAdrian Hunter {
9975562573SAdrian Hunter 	struct perf_evsel *first = perf_evlist__first(evlist);
10075562573SAdrian Hunter 
10175562573SAdrian Hunter 	evlist->id_pos = first->id_pos;
10275562573SAdrian Hunter 	evlist->is_pos = first->is_pos;
10375562573SAdrian Hunter }
10475562573SAdrian Hunter 
105733cd2feSAdrian Hunter static void perf_evlist__update_id_pos(struct perf_evlist *evlist)
106733cd2feSAdrian Hunter {
107733cd2feSAdrian Hunter 	struct perf_evsel *evsel;
108733cd2feSAdrian Hunter 
109e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, evsel)
110733cd2feSAdrian Hunter 		perf_evsel__calc_id_pos(evsel);
111733cd2feSAdrian Hunter 
112733cd2feSAdrian Hunter 	perf_evlist__set_id_pos(evlist);
113733cd2feSAdrian Hunter }
114733cd2feSAdrian Hunter 
115361c99a6SArnaldo Carvalho de Melo static void perf_evlist__purge(struct perf_evlist *evlist)
116361c99a6SArnaldo Carvalho de Melo {
117361c99a6SArnaldo Carvalho de Melo 	struct perf_evsel *pos, *n;
118361c99a6SArnaldo Carvalho de Melo 
119e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry_safe(evlist, n, pos) {
120361c99a6SArnaldo Carvalho de Melo 		list_del_init(&pos->node);
121d49e4695SArnaldo Carvalho de Melo 		pos->evlist = NULL;
122361c99a6SArnaldo Carvalho de Melo 		perf_evsel__delete(pos);
123361c99a6SArnaldo Carvalho de Melo 	}
124361c99a6SArnaldo Carvalho de Melo 
125361c99a6SArnaldo Carvalho de Melo 	evlist->nr_entries = 0;
126361c99a6SArnaldo Carvalho de Melo }
127361c99a6SArnaldo Carvalho de Melo 
128ef1d1af2SArnaldo Carvalho de Melo void perf_evlist__exit(struct perf_evlist *evlist)
129ef1d1af2SArnaldo Carvalho de Melo {
13004662523SArnaldo Carvalho de Melo 	zfree(&evlist->mmap);
131b2cb615dSWang Nan 	zfree(&evlist->backward_mmap);
1321b85337dSArnaldo Carvalho de Melo 	fdarray__exit(&evlist->pollfd);
133ef1d1af2SArnaldo Carvalho de Melo }
134ef1d1af2SArnaldo Carvalho de Melo 
135361c99a6SArnaldo Carvalho de Melo void perf_evlist__delete(struct perf_evlist *evlist)
136361c99a6SArnaldo Carvalho de Melo {
1370b04b3dcSArnaldo Carvalho de Melo 	if (evlist == NULL)
1380b04b3dcSArnaldo Carvalho de Melo 		return;
1390b04b3dcSArnaldo Carvalho de Melo 
140983874d1SArnaldo Carvalho de Melo 	perf_evlist__munmap(evlist);
141f26e1c7cSArnaldo Carvalho de Melo 	perf_evlist__close(evlist);
142f30a79b0SJiri Olsa 	cpu_map__put(evlist->cpus);
143186fbb74SJiri Olsa 	thread_map__put(evlist->threads);
14403ad9747SArnaldo Carvalho de Melo 	evlist->cpus = NULL;
14503ad9747SArnaldo Carvalho de Melo 	evlist->threads = NULL;
146361c99a6SArnaldo Carvalho de Melo 	perf_evlist__purge(evlist);
147ef1d1af2SArnaldo Carvalho de Melo 	perf_evlist__exit(evlist);
148361c99a6SArnaldo Carvalho de Melo 	free(evlist);
149361c99a6SArnaldo Carvalho de Melo }
150361c99a6SArnaldo Carvalho de Melo 
151adc0c3e8SAdrian Hunter static void __perf_evlist__propagate_maps(struct perf_evlist *evlist,
152adc0c3e8SAdrian Hunter 					  struct perf_evsel *evsel)
153adc0c3e8SAdrian Hunter {
154adc0c3e8SAdrian Hunter 	/*
155adc0c3e8SAdrian Hunter 	 * We already have cpus for evsel (via PMU sysfs) so
156adc0c3e8SAdrian Hunter 	 * keep it, if there's no target cpu list defined.
157adc0c3e8SAdrian Hunter 	 */
158adc0c3e8SAdrian Hunter 	if (!evsel->own_cpus || evlist->has_user_cpus) {
159adc0c3e8SAdrian Hunter 		cpu_map__put(evsel->cpus);
160adc0c3e8SAdrian Hunter 		evsel->cpus = cpu_map__get(evlist->cpus);
161adc0c3e8SAdrian Hunter 	} else if (evsel->cpus != evsel->own_cpus) {
162adc0c3e8SAdrian Hunter 		cpu_map__put(evsel->cpus);
163adc0c3e8SAdrian Hunter 		evsel->cpus = cpu_map__get(evsel->own_cpus);
164adc0c3e8SAdrian Hunter 	}
165adc0c3e8SAdrian Hunter 
166adc0c3e8SAdrian Hunter 	thread_map__put(evsel->threads);
167adc0c3e8SAdrian Hunter 	evsel->threads = thread_map__get(evlist->threads);
168adc0c3e8SAdrian Hunter }
169adc0c3e8SAdrian Hunter 
170adc0c3e8SAdrian Hunter static void perf_evlist__propagate_maps(struct perf_evlist *evlist)
171adc0c3e8SAdrian Hunter {
172adc0c3e8SAdrian Hunter 	struct perf_evsel *evsel;
173adc0c3e8SAdrian Hunter 
174e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, evsel)
175adc0c3e8SAdrian Hunter 		__perf_evlist__propagate_maps(evlist, evsel);
176adc0c3e8SAdrian Hunter }
177adc0c3e8SAdrian Hunter 
178361c99a6SArnaldo Carvalho de Melo void perf_evlist__add(struct perf_evlist *evlist, struct perf_evsel *entry)
179361c99a6SArnaldo Carvalho de Melo {
180d49e4695SArnaldo Carvalho de Melo 	entry->evlist = evlist;
181361c99a6SArnaldo Carvalho de Melo 	list_add_tail(&entry->node, &evlist->entries);
182ef503831SArnaldo Carvalho de Melo 	entry->idx = evlist->nr_entries;
18360b0896cSAdrian Hunter 	entry->tracking = !entry->idx;
184ef503831SArnaldo Carvalho de Melo 
18575562573SAdrian Hunter 	if (!evlist->nr_entries++)
18675562573SAdrian Hunter 		perf_evlist__set_id_pos(evlist);
18744c42d71SAdrian Hunter 
18844c42d71SAdrian Hunter 	__perf_evlist__propagate_maps(evlist, entry);
189361c99a6SArnaldo Carvalho de Melo }
190361c99a6SArnaldo Carvalho de Melo 
1914768230aSAdrian Hunter void perf_evlist__remove(struct perf_evlist *evlist, struct perf_evsel *evsel)
1924768230aSAdrian Hunter {
1934768230aSAdrian Hunter 	evsel->evlist = NULL;
1944768230aSAdrian Hunter 	list_del_init(&evsel->node);
1954768230aSAdrian Hunter 	evlist->nr_entries -= 1;
1964768230aSAdrian Hunter }
1974768230aSAdrian Hunter 
1980529bc1fSJiri Olsa void perf_evlist__splice_list_tail(struct perf_evlist *evlist,
199f114d6efSAdrian Hunter 				   struct list_head *list)
20050d08e47SArnaldo Carvalho de Melo {
201f114d6efSAdrian Hunter 	struct perf_evsel *evsel, *temp;
20275562573SAdrian Hunter 
203e5cadb93SArnaldo Carvalho de Melo 	__evlist__for_each_entry_safe(list, temp, evsel) {
204f114d6efSAdrian Hunter 		list_del_init(&evsel->node);
205f114d6efSAdrian Hunter 		perf_evlist__add(evlist, evsel);
206f114d6efSAdrian Hunter 	}
20750d08e47SArnaldo Carvalho de Melo }
20850d08e47SArnaldo Carvalho de Melo 
20963dab225SArnaldo Carvalho de Melo void __perf_evlist__set_leader(struct list_head *list)
21063dab225SArnaldo Carvalho de Melo {
21163dab225SArnaldo Carvalho de Melo 	struct perf_evsel *evsel, *leader;
21263dab225SArnaldo Carvalho de Melo 
21363dab225SArnaldo Carvalho de Melo 	leader = list_entry(list->next, struct perf_evsel, node);
21497f63e4aSNamhyung Kim 	evsel = list_entry(list->prev, struct perf_evsel, node);
21597f63e4aSNamhyung Kim 
21697f63e4aSNamhyung Kim 	leader->nr_members = evsel->idx - leader->idx + 1;
21763dab225SArnaldo Carvalho de Melo 
218e5cadb93SArnaldo Carvalho de Melo 	__evlist__for_each_entry(list, evsel) {
21963dab225SArnaldo Carvalho de Melo 		evsel->leader = leader;
22063dab225SArnaldo Carvalho de Melo 	}
22163dab225SArnaldo Carvalho de Melo }
22263dab225SArnaldo Carvalho de Melo 
22363dab225SArnaldo Carvalho de Melo void perf_evlist__set_leader(struct perf_evlist *evlist)
2246a4bb04cSJiri Olsa {
22597f63e4aSNamhyung Kim 	if (evlist->nr_entries) {
22697f63e4aSNamhyung Kim 		evlist->nr_groups = evlist->nr_entries > 1 ? 1 : 0;
22763dab225SArnaldo Carvalho de Melo 		__perf_evlist__set_leader(&evlist->entries);
2286a4bb04cSJiri Olsa 	}
22997f63e4aSNamhyung Kim }
2306a4bb04cSJiri Olsa 
23145cf6c33SJiri Olsa void perf_event_attr__set_max_precise_ip(struct perf_event_attr *attr)
2327f8d1adeSArnaldo Carvalho de Melo {
2337f8d1adeSArnaldo Carvalho de Melo 	attr->precise_ip = 3;
2347f8d1adeSArnaldo Carvalho de Melo 
2357f8d1adeSArnaldo Carvalho de Melo 	while (attr->precise_ip != 0) {
2367f8d1adeSArnaldo Carvalho de Melo 		int fd = sys_perf_event_open(attr, 0, -1, -1, 0);
2377f8d1adeSArnaldo Carvalho de Melo 		if (fd != -1) {
2387f8d1adeSArnaldo Carvalho de Melo 			close(fd);
2397f8d1adeSArnaldo Carvalho de Melo 			break;
2407f8d1adeSArnaldo Carvalho de Melo 		}
2417f8d1adeSArnaldo Carvalho de Melo 		--attr->precise_ip;
2427f8d1adeSArnaldo Carvalho de Melo 	}
2437f8d1adeSArnaldo Carvalho de Melo }
2447f8d1adeSArnaldo Carvalho de Melo 
245361c99a6SArnaldo Carvalho de Melo int perf_evlist__add_default(struct perf_evlist *evlist)
246361c99a6SArnaldo Carvalho de Melo {
2477c48dcfdSArnaldo Carvalho de Melo 	struct perf_evsel *evsel = perf_evsel__new_cycles();
248361c99a6SArnaldo Carvalho de Melo 
249361c99a6SArnaldo Carvalho de Melo 	if (evsel == NULL)
2507c48dcfdSArnaldo Carvalho de Melo 		return -ENOMEM;
251361c99a6SArnaldo Carvalho de Melo 
252361c99a6SArnaldo Carvalho de Melo 	perf_evlist__add(evlist, evsel);
253361c99a6SArnaldo Carvalho de Melo 	return 0;
254361c99a6SArnaldo Carvalho de Melo }
2555c581041SArnaldo Carvalho de Melo 
2565bae0250SArnaldo Carvalho de Melo int perf_evlist__add_dummy(struct perf_evlist *evlist)
2575bae0250SArnaldo Carvalho de Melo {
2585bae0250SArnaldo Carvalho de Melo 	struct perf_event_attr attr = {
2595bae0250SArnaldo Carvalho de Melo 		.type	= PERF_TYPE_SOFTWARE,
2605bae0250SArnaldo Carvalho de Melo 		.config = PERF_COUNT_SW_DUMMY,
2615bae0250SArnaldo Carvalho de Melo 		.size	= sizeof(attr), /* to capture ABI version */
2625bae0250SArnaldo Carvalho de Melo 	};
2635bae0250SArnaldo Carvalho de Melo 	struct perf_evsel *evsel = perf_evsel__new(&attr);
2645bae0250SArnaldo Carvalho de Melo 
2655bae0250SArnaldo Carvalho de Melo 	if (evsel == NULL)
2665bae0250SArnaldo Carvalho de Melo 		return -ENOMEM;
2675bae0250SArnaldo Carvalho de Melo 
2685bae0250SArnaldo Carvalho de Melo 	perf_evlist__add(evlist, evsel);
2695bae0250SArnaldo Carvalho de Melo 	return 0;
2705bae0250SArnaldo Carvalho de Melo }
2715bae0250SArnaldo Carvalho de Melo 
272e60fc847SArnaldo Carvalho de Melo static int perf_evlist__add_attrs(struct perf_evlist *evlist,
27350d08e47SArnaldo Carvalho de Melo 				  struct perf_event_attr *attrs, size_t nr_attrs)
27450d08e47SArnaldo Carvalho de Melo {
27550d08e47SArnaldo Carvalho de Melo 	struct perf_evsel *evsel, *n;
27650d08e47SArnaldo Carvalho de Melo 	LIST_HEAD(head);
27750d08e47SArnaldo Carvalho de Melo 	size_t i;
27850d08e47SArnaldo Carvalho de Melo 
27950d08e47SArnaldo Carvalho de Melo 	for (i = 0; i < nr_attrs; i++) {
280ef503831SArnaldo Carvalho de Melo 		evsel = perf_evsel__new_idx(attrs + i, evlist->nr_entries + i);
28150d08e47SArnaldo Carvalho de Melo 		if (evsel == NULL)
28250d08e47SArnaldo Carvalho de Melo 			goto out_delete_partial_list;
28350d08e47SArnaldo Carvalho de Melo 		list_add_tail(&evsel->node, &head);
28450d08e47SArnaldo Carvalho de Melo 	}
28550d08e47SArnaldo Carvalho de Melo 
286f114d6efSAdrian Hunter 	perf_evlist__splice_list_tail(evlist, &head);
28750d08e47SArnaldo Carvalho de Melo 
28850d08e47SArnaldo Carvalho de Melo 	return 0;
28950d08e47SArnaldo Carvalho de Melo 
29050d08e47SArnaldo Carvalho de Melo out_delete_partial_list:
291e5cadb93SArnaldo Carvalho de Melo 	__evlist__for_each_entry_safe(&head, n, evsel)
29250d08e47SArnaldo Carvalho de Melo 		perf_evsel__delete(evsel);
29350d08e47SArnaldo Carvalho de Melo 	return -1;
29450d08e47SArnaldo Carvalho de Melo }
29550d08e47SArnaldo Carvalho de Melo 
29679695e1bSArnaldo Carvalho de Melo int __perf_evlist__add_default_attrs(struct perf_evlist *evlist,
29779695e1bSArnaldo Carvalho de Melo 				     struct perf_event_attr *attrs, size_t nr_attrs)
29879695e1bSArnaldo Carvalho de Melo {
29979695e1bSArnaldo Carvalho de Melo 	size_t i;
30079695e1bSArnaldo Carvalho de Melo 
30179695e1bSArnaldo Carvalho de Melo 	for (i = 0; i < nr_attrs; i++)
30279695e1bSArnaldo Carvalho de Melo 		event_attr_init(attrs + i);
30379695e1bSArnaldo Carvalho de Melo 
30479695e1bSArnaldo Carvalho de Melo 	return perf_evlist__add_attrs(evlist, attrs, nr_attrs);
30579695e1bSArnaldo Carvalho de Melo }
30679695e1bSArnaldo Carvalho de Melo 
307da378962SArnaldo Carvalho de Melo struct perf_evsel *
308ee29be62SArnaldo Carvalho de Melo perf_evlist__find_tracepoint_by_id(struct perf_evlist *evlist, int id)
309ee29be62SArnaldo Carvalho de Melo {
310ee29be62SArnaldo Carvalho de Melo 	struct perf_evsel *evsel;
311ee29be62SArnaldo Carvalho de Melo 
312e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, evsel) {
313ee29be62SArnaldo Carvalho de Melo 		if (evsel->attr.type   == PERF_TYPE_TRACEPOINT &&
314ee29be62SArnaldo Carvalho de Melo 		    (int)evsel->attr.config == id)
315ee29be62SArnaldo Carvalho de Melo 			return evsel;
316ee29be62SArnaldo Carvalho de Melo 	}
317ee29be62SArnaldo Carvalho de Melo 
318ee29be62SArnaldo Carvalho de Melo 	return NULL;
319ee29be62SArnaldo Carvalho de Melo }
320ee29be62SArnaldo Carvalho de Melo 
321a2f2804aSDavid Ahern struct perf_evsel *
322a2f2804aSDavid Ahern perf_evlist__find_tracepoint_by_name(struct perf_evlist *evlist,
323a2f2804aSDavid Ahern 				     const char *name)
324a2f2804aSDavid Ahern {
325a2f2804aSDavid Ahern 	struct perf_evsel *evsel;
326a2f2804aSDavid Ahern 
327e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, evsel) {
328a2f2804aSDavid Ahern 		if ((evsel->attr.type == PERF_TYPE_TRACEPOINT) &&
329a2f2804aSDavid Ahern 		    (strcmp(evsel->name, name) == 0))
330a2f2804aSDavid Ahern 			return evsel;
331a2f2804aSDavid Ahern 	}
332a2f2804aSDavid Ahern 
333a2f2804aSDavid Ahern 	return NULL;
334a2f2804aSDavid Ahern }
335a2f2804aSDavid Ahern 
33639876e7dSArnaldo Carvalho de Melo int perf_evlist__add_newtp(struct perf_evlist *evlist,
33739876e7dSArnaldo Carvalho de Melo 			   const char *sys, const char *name, void *handler)
33839876e7dSArnaldo Carvalho de Melo {
339ef503831SArnaldo Carvalho de Melo 	struct perf_evsel *evsel = perf_evsel__newtp(sys, name);
34039876e7dSArnaldo Carvalho de Melo 
3418dd2a131SJiri Olsa 	if (IS_ERR(evsel))
34239876e7dSArnaldo Carvalho de Melo 		return -1;
34339876e7dSArnaldo Carvalho de Melo 
344744a9719SArnaldo Carvalho de Melo 	evsel->handler = handler;
34539876e7dSArnaldo Carvalho de Melo 	perf_evlist__add(evlist, evsel);
34639876e7dSArnaldo Carvalho de Melo 	return 0;
34739876e7dSArnaldo Carvalho de Melo }
34839876e7dSArnaldo Carvalho de Melo 
349bf8e8f4bSAdrian Hunter static int perf_evlist__nr_threads(struct perf_evlist *evlist,
350bf8e8f4bSAdrian Hunter 				   struct perf_evsel *evsel)
351bf8e8f4bSAdrian Hunter {
352bf8e8f4bSAdrian Hunter 	if (evsel->system_wide)
353bf8e8f4bSAdrian Hunter 		return 1;
354bf8e8f4bSAdrian Hunter 	else
355bf8e8f4bSAdrian Hunter 		return thread_map__nr(evlist->threads);
356bf8e8f4bSAdrian Hunter }
357bf8e8f4bSAdrian Hunter 
3584152ab37SArnaldo Carvalho de Melo void perf_evlist__disable(struct perf_evlist *evlist)
3594152ab37SArnaldo Carvalho de Melo {
3604152ab37SArnaldo Carvalho de Melo 	struct perf_evsel *pos;
3614152ab37SArnaldo Carvalho de Melo 
362e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, pos) {
363395c3070SAdrian Hunter 		if (!perf_evsel__is_group_leader(pos) || !pos->fd)
3643fe4430dSJiri Olsa 			continue;
3653e27c920SJiri Olsa 		perf_evsel__disable(pos);
3664152ab37SArnaldo Carvalho de Melo 	}
3672b56bcfbSArnaldo Carvalho de Melo 
3682b56bcfbSArnaldo Carvalho de Melo 	evlist->enabled = false;
3694152ab37SArnaldo Carvalho de Melo }
3704152ab37SArnaldo Carvalho de Melo 
371764e16a3SDavid Ahern void perf_evlist__enable(struct perf_evlist *evlist)
372764e16a3SDavid Ahern {
373764e16a3SDavid Ahern 	struct perf_evsel *pos;
374764e16a3SDavid Ahern 
375e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, pos) {
376395c3070SAdrian Hunter 		if (!perf_evsel__is_group_leader(pos) || !pos->fd)
3773fe4430dSJiri Olsa 			continue;
3783e27c920SJiri Olsa 		perf_evsel__enable(pos);
379764e16a3SDavid Ahern 	}
3802b56bcfbSArnaldo Carvalho de Melo 
3812b56bcfbSArnaldo Carvalho de Melo 	evlist->enabled = true;
3822b56bcfbSArnaldo Carvalho de Melo }
3832b56bcfbSArnaldo Carvalho de Melo 
3842b56bcfbSArnaldo Carvalho de Melo void perf_evlist__toggle_enable(struct perf_evlist *evlist)
3852b56bcfbSArnaldo Carvalho de Melo {
3862b56bcfbSArnaldo Carvalho de Melo 	(evlist->enabled ? perf_evlist__disable : perf_evlist__enable)(evlist);
387764e16a3SDavid Ahern }
388764e16a3SDavid Ahern 
3891c65056cSAdrian Hunter static int perf_evlist__enable_event_cpu(struct perf_evlist *evlist,
3901c65056cSAdrian Hunter 					 struct perf_evsel *evsel, int cpu)
3911c65056cSAdrian Hunter {
39218ef15c6SArnaldo Carvalho de Melo 	int thread;
3931c65056cSAdrian Hunter 	int nr_threads = perf_evlist__nr_threads(evlist, evsel);
3941c65056cSAdrian Hunter 
3951c65056cSAdrian Hunter 	if (!evsel->fd)
3961c65056cSAdrian Hunter 		return -EINVAL;
3971c65056cSAdrian Hunter 
3981c65056cSAdrian Hunter 	for (thread = 0; thread < nr_threads; thread++) {
39918ef15c6SArnaldo Carvalho de Melo 		int err = ioctl(FD(evsel, cpu, thread), PERF_EVENT_IOC_ENABLE, 0);
4001c65056cSAdrian Hunter 		if (err)
4011c65056cSAdrian Hunter 			return err;
4021c65056cSAdrian Hunter 	}
4031c65056cSAdrian Hunter 	return 0;
4041c65056cSAdrian Hunter }
4051c65056cSAdrian Hunter 
4061c65056cSAdrian Hunter static int perf_evlist__enable_event_thread(struct perf_evlist *evlist,
4071c65056cSAdrian Hunter 					    struct perf_evsel *evsel,
4081c65056cSAdrian Hunter 					    int thread)
4091c65056cSAdrian Hunter {
41018ef15c6SArnaldo Carvalho de Melo 	int cpu;
4111c65056cSAdrian Hunter 	int nr_cpus = cpu_map__nr(evlist->cpus);
4121c65056cSAdrian Hunter 
4131c65056cSAdrian Hunter 	if (!evsel->fd)
4141c65056cSAdrian Hunter 		return -EINVAL;
4151c65056cSAdrian Hunter 
4161c65056cSAdrian Hunter 	for (cpu = 0; cpu < nr_cpus; cpu++) {
41718ef15c6SArnaldo Carvalho de Melo 		int err = ioctl(FD(evsel, cpu, thread), PERF_EVENT_IOC_ENABLE, 0);
4181c65056cSAdrian Hunter 		if (err)
4191c65056cSAdrian Hunter 			return err;
4201c65056cSAdrian Hunter 	}
4211c65056cSAdrian Hunter 	return 0;
4221c65056cSAdrian Hunter }
4231c65056cSAdrian Hunter 
4241c65056cSAdrian Hunter int perf_evlist__enable_event_idx(struct perf_evlist *evlist,
4251c65056cSAdrian Hunter 				  struct perf_evsel *evsel, int idx)
4261c65056cSAdrian Hunter {
4271c65056cSAdrian Hunter 	bool per_cpu_mmaps = !cpu_map__empty(evlist->cpus);
4281c65056cSAdrian Hunter 
4291c65056cSAdrian Hunter 	if (per_cpu_mmaps)
4301c65056cSAdrian Hunter 		return perf_evlist__enable_event_cpu(evlist, evsel, idx);
4311c65056cSAdrian Hunter 	else
4321c65056cSAdrian Hunter 		return perf_evlist__enable_event_thread(evlist, evsel, idx);
4331c65056cSAdrian Hunter }
4341c65056cSAdrian Hunter 
435ad6765ddSArnaldo Carvalho de Melo int perf_evlist__alloc_pollfd(struct perf_evlist *evlist)
4365c581041SArnaldo Carvalho de Melo {
437b3a319d5SNamhyung Kim 	int nr_cpus = cpu_map__nr(evlist->cpus);
438b3a319d5SNamhyung Kim 	int nr_threads = thread_map__nr(evlist->threads);
439bf8e8f4bSAdrian Hunter 	int nfds = 0;
440bf8e8f4bSAdrian Hunter 	struct perf_evsel *evsel;
441bf8e8f4bSAdrian Hunter 
442e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, evsel) {
443bf8e8f4bSAdrian Hunter 		if (evsel->system_wide)
444bf8e8f4bSAdrian Hunter 			nfds += nr_cpus;
445bf8e8f4bSAdrian Hunter 		else
446bf8e8f4bSAdrian Hunter 			nfds += nr_cpus * nr_threads;
447bf8e8f4bSAdrian Hunter 	}
448bf8e8f4bSAdrian Hunter 
4491b85337dSArnaldo Carvalho de Melo 	if (fdarray__available_entries(&evlist->pollfd) < nfds &&
4501b85337dSArnaldo Carvalho de Melo 	    fdarray__grow(&evlist->pollfd, nfds) < 0)
451ad6765ddSArnaldo Carvalho de Melo 		return -ENOMEM;
452ad6765ddSArnaldo Carvalho de Melo 
453ad6765ddSArnaldo Carvalho de Melo 	return 0;
4545c581041SArnaldo Carvalho de Melo }
45570082dd9SArnaldo Carvalho de Melo 
4564876075bSWang Nan static int __perf_evlist__add_pollfd(struct perf_evlist *evlist, int fd,
4574876075bSWang Nan 				     struct perf_mmap *map, short revent)
458e4b356b5SArnaldo Carvalho de Melo {
459f3058a1cSWang Nan 	int pos = fdarray__add(&evlist->pollfd, fd, revent | POLLERR | POLLHUP);
460e4b356b5SArnaldo Carvalho de Melo 	/*
461e4b356b5SArnaldo Carvalho de Melo 	 * Save the idx so that when we filter out fds POLLHUP'ed we can
462e4b356b5SArnaldo Carvalho de Melo 	 * close the associated evlist->mmap[] entry.
463e4b356b5SArnaldo Carvalho de Melo 	 */
464e4b356b5SArnaldo Carvalho de Melo 	if (pos >= 0) {
4654876075bSWang Nan 		evlist->pollfd.priv[pos].ptr = map;
466e4b356b5SArnaldo Carvalho de Melo 
467e4b356b5SArnaldo Carvalho de Melo 		fcntl(fd, F_SETFL, O_NONBLOCK);
468e4b356b5SArnaldo Carvalho de Melo 	}
469e4b356b5SArnaldo Carvalho de Melo 
470e4b356b5SArnaldo Carvalho de Melo 	return pos;
471e4b356b5SArnaldo Carvalho de Melo }
472e4b356b5SArnaldo Carvalho de Melo 
473ad6765ddSArnaldo Carvalho de Melo int perf_evlist__add_pollfd(struct perf_evlist *evlist, int fd)
47470082dd9SArnaldo Carvalho de Melo {
4754876075bSWang Nan 	return __perf_evlist__add_pollfd(evlist, fd, NULL, POLLIN);
476e4b356b5SArnaldo Carvalho de Melo }
4771b85337dSArnaldo Carvalho de Melo 
478258e4bfcSWang Nan static void perf_evlist__munmap_filtered(struct fdarray *fda, int fd,
479258e4bfcSWang Nan 					 void *arg __maybe_unused)
480e4b356b5SArnaldo Carvalho de Melo {
4814876075bSWang Nan 	struct perf_mmap *map = fda->priv[fd].ptr;
482e4b356b5SArnaldo Carvalho de Melo 
4834876075bSWang Nan 	if (map)
4844876075bSWang Nan 		perf_mmap__put(map);
48570082dd9SArnaldo Carvalho de Melo }
48670db7533SArnaldo Carvalho de Melo 
4871ddec7f0SArnaldo Carvalho de Melo int perf_evlist__filter_pollfd(struct perf_evlist *evlist, short revents_and_mask)
4881ddec7f0SArnaldo Carvalho de Melo {
489e4b356b5SArnaldo Carvalho de Melo 	return fdarray__filter(&evlist->pollfd, revents_and_mask,
490258e4bfcSWang Nan 			       perf_evlist__munmap_filtered, NULL);
4911ddec7f0SArnaldo Carvalho de Melo }
4921ddec7f0SArnaldo Carvalho de Melo 
493f66a889dSArnaldo Carvalho de Melo int perf_evlist__poll(struct perf_evlist *evlist, int timeout)
494f66a889dSArnaldo Carvalho de Melo {
4951b85337dSArnaldo Carvalho de Melo 	return fdarray__poll(&evlist->pollfd, timeout);
496f66a889dSArnaldo Carvalho de Melo }
497f66a889dSArnaldo Carvalho de Melo 
498a91e5431SArnaldo Carvalho de Melo static void perf_evlist__id_hash(struct perf_evlist *evlist,
499a91e5431SArnaldo Carvalho de Melo 				 struct perf_evsel *evsel,
5003d3b5e95SArnaldo Carvalho de Melo 				 int cpu, int thread, u64 id)
5013d3b5e95SArnaldo Carvalho de Melo {
5023d3b5e95SArnaldo Carvalho de Melo 	int hash;
5033d3b5e95SArnaldo Carvalho de Melo 	struct perf_sample_id *sid = SID(evsel, cpu, thread);
5043d3b5e95SArnaldo Carvalho de Melo 
5053d3b5e95SArnaldo Carvalho de Melo 	sid->id = id;
5063d3b5e95SArnaldo Carvalho de Melo 	sid->evsel = evsel;
5073d3b5e95SArnaldo Carvalho de Melo 	hash = hash_64(sid->id, PERF_EVLIST__HLIST_BITS);
5083d3b5e95SArnaldo Carvalho de Melo 	hlist_add_head(&sid->node, &evlist->heads[hash]);
5093d3b5e95SArnaldo Carvalho de Melo }
5103d3b5e95SArnaldo Carvalho de Melo 
511a91e5431SArnaldo Carvalho de Melo void perf_evlist__id_add(struct perf_evlist *evlist, struct perf_evsel *evsel,
512a91e5431SArnaldo Carvalho de Melo 			 int cpu, int thread, u64 id)
513a91e5431SArnaldo Carvalho de Melo {
514a91e5431SArnaldo Carvalho de Melo 	perf_evlist__id_hash(evlist, evsel, cpu, thread, id);
515a91e5431SArnaldo Carvalho de Melo 	evsel->id[evsel->ids++] = id;
516a91e5431SArnaldo Carvalho de Melo }
517a91e5431SArnaldo Carvalho de Melo 
5181c59612dSJiri Olsa int perf_evlist__id_add_fd(struct perf_evlist *evlist,
5193d3b5e95SArnaldo Carvalho de Melo 			   struct perf_evsel *evsel,
520f8a95309SArnaldo Carvalho de Melo 			   int cpu, int thread, int fd)
521f8a95309SArnaldo Carvalho de Melo {
522f8a95309SArnaldo Carvalho de Melo 	u64 read_data[4] = { 0, };
5233d3b5e95SArnaldo Carvalho de Melo 	int id_idx = 1; /* The first entry is the counter value */
524e2b5abe0SJiri Olsa 	u64 id;
525e2b5abe0SJiri Olsa 	int ret;
526e2b5abe0SJiri Olsa 
527e2b5abe0SJiri Olsa 	ret = ioctl(fd, PERF_EVENT_IOC_ID, &id);
528e2b5abe0SJiri Olsa 	if (!ret)
529e2b5abe0SJiri Olsa 		goto add;
530e2b5abe0SJiri Olsa 
531e2b5abe0SJiri Olsa 	if (errno != ENOTTY)
532e2b5abe0SJiri Olsa 		return -1;
533e2b5abe0SJiri Olsa 
534e2b5abe0SJiri Olsa 	/* Legacy way to get event id.. All hail to old kernels! */
535f8a95309SArnaldo Carvalho de Melo 
536c4861afeSJiri Olsa 	/*
537c4861afeSJiri Olsa 	 * This way does not work with group format read, so bail
538c4861afeSJiri Olsa 	 * out in that case.
539c4861afeSJiri Olsa 	 */
540c4861afeSJiri Olsa 	if (perf_evlist__read_format(evlist) & PERF_FORMAT_GROUP)
541c4861afeSJiri Olsa 		return -1;
542c4861afeSJiri Olsa 
543f8a95309SArnaldo Carvalho de Melo 	if (!(evsel->attr.read_format & PERF_FORMAT_ID) ||
544f8a95309SArnaldo Carvalho de Melo 	    read(fd, &read_data, sizeof(read_data)) == -1)
545f8a95309SArnaldo Carvalho de Melo 		return -1;
546f8a95309SArnaldo Carvalho de Melo 
547f8a95309SArnaldo Carvalho de Melo 	if (evsel->attr.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
548f8a95309SArnaldo Carvalho de Melo 		++id_idx;
549f8a95309SArnaldo Carvalho de Melo 	if (evsel->attr.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
550f8a95309SArnaldo Carvalho de Melo 		++id_idx;
551f8a95309SArnaldo Carvalho de Melo 
552e2b5abe0SJiri Olsa 	id = read_data[id_idx];
553e2b5abe0SJiri Olsa 
554e2b5abe0SJiri Olsa  add:
555e2b5abe0SJiri Olsa 	perf_evlist__id_add(evlist, evsel, cpu, thread, id);
556f8a95309SArnaldo Carvalho de Melo 	return 0;
557f8a95309SArnaldo Carvalho de Melo }
558f8a95309SArnaldo Carvalho de Melo 
5593c659eedSAdrian Hunter static void perf_evlist__set_sid_idx(struct perf_evlist *evlist,
5603c659eedSAdrian Hunter 				     struct perf_evsel *evsel, int idx, int cpu,
5613c659eedSAdrian Hunter 				     int thread)
5623c659eedSAdrian Hunter {
5633c659eedSAdrian Hunter 	struct perf_sample_id *sid = SID(evsel, cpu, thread);
5643c659eedSAdrian Hunter 	sid->idx = idx;
5653c659eedSAdrian Hunter 	if (evlist->cpus && cpu >= 0)
5663c659eedSAdrian Hunter 		sid->cpu = evlist->cpus->map[cpu];
5673c659eedSAdrian Hunter 	else
5683c659eedSAdrian Hunter 		sid->cpu = -1;
5693c659eedSAdrian Hunter 	if (!evsel->system_wide && evlist->threads && thread >= 0)
570e13798c7SJiri Olsa 		sid->tid = thread_map__pid(evlist->threads, thread);
5713c659eedSAdrian Hunter 	else
5723c659eedSAdrian Hunter 		sid->tid = -1;
5733c659eedSAdrian Hunter }
5743c659eedSAdrian Hunter 
575932a3594SJiri Olsa struct perf_sample_id *perf_evlist__id2sid(struct perf_evlist *evlist, u64 id)
57670db7533SArnaldo Carvalho de Melo {
57770db7533SArnaldo Carvalho de Melo 	struct hlist_head *head;
57870db7533SArnaldo Carvalho de Melo 	struct perf_sample_id *sid;
57970db7533SArnaldo Carvalho de Melo 	int hash;
58070db7533SArnaldo Carvalho de Melo 
58170db7533SArnaldo Carvalho de Melo 	hash = hash_64(id, PERF_EVLIST__HLIST_BITS);
58270db7533SArnaldo Carvalho de Melo 	head = &evlist->heads[hash];
58370db7533SArnaldo Carvalho de Melo 
584b67bfe0dSSasha Levin 	hlist_for_each_entry(sid, head, node)
58570db7533SArnaldo Carvalho de Melo 		if (sid->id == id)
586932a3594SJiri Olsa 			return sid;
587932a3594SJiri Olsa 
588932a3594SJiri Olsa 	return NULL;
589932a3594SJiri Olsa }
590932a3594SJiri Olsa 
591932a3594SJiri Olsa struct perf_evsel *perf_evlist__id2evsel(struct perf_evlist *evlist, u64 id)
592932a3594SJiri Olsa {
593932a3594SJiri Olsa 	struct perf_sample_id *sid;
594932a3594SJiri Olsa 
59505169df5SAdrian Hunter 	if (evlist->nr_entries == 1 || !id)
596932a3594SJiri Olsa 		return perf_evlist__first(evlist);
597932a3594SJiri Olsa 
598932a3594SJiri Olsa 	sid = perf_evlist__id2sid(evlist, id);
599932a3594SJiri Olsa 	if (sid)
60070db7533SArnaldo Carvalho de Melo 		return sid->evsel;
60130e68bccSNamhyung Kim 
60230e68bccSNamhyung Kim 	if (!perf_evlist__sample_id_all(evlist))
6030c21f736SArnaldo Carvalho de Melo 		return perf_evlist__first(evlist);
60430e68bccSNamhyung Kim 
60570db7533SArnaldo Carvalho de Melo 	return NULL;
60670db7533SArnaldo Carvalho de Melo }
60704391debSArnaldo Carvalho de Melo 
608dddcf6abSAdrian Hunter struct perf_evsel *perf_evlist__id2evsel_strict(struct perf_evlist *evlist,
609dddcf6abSAdrian Hunter 						u64 id)
610dddcf6abSAdrian Hunter {
611dddcf6abSAdrian Hunter 	struct perf_sample_id *sid;
612dddcf6abSAdrian Hunter 
613dddcf6abSAdrian Hunter 	if (!id)
614dddcf6abSAdrian Hunter 		return NULL;
615dddcf6abSAdrian Hunter 
616dddcf6abSAdrian Hunter 	sid = perf_evlist__id2sid(evlist, id);
617dddcf6abSAdrian Hunter 	if (sid)
618dddcf6abSAdrian Hunter 		return sid->evsel;
619dddcf6abSAdrian Hunter 
620dddcf6abSAdrian Hunter 	return NULL;
621dddcf6abSAdrian Hunter }
622dddcf6abSAdrian Hunter 
62375562573SAdrian Hunter static int perf_evlist__event2id(struct perf_evlist *evlist,
62475562573SAdrian Hunter 				 union perf_event *event, u64 *id)
62575562573SAdrian Hunter {
62675562573SAdrian Hunter 	const u64 *array = event->sample.array;
62775562573SAdrian Hunter 	ssize_t n;
62875562573SAdrian Hunter 
62975562573SAdrian Hunter 	n = (event->header.size - sizeof(event->header)) >> 3;
63075562573SAdrian Hunter 
63175562573SAdrian Hunter 	if (event->header.type == PERF_RECORD_SAMPLE) {
63275562573SAdrian Hunter 		if (evlist->id_pos >= n)
63375562573SAdrian Hunter 			return -1;
63475562573SAdrian Hunter 		*id = array[evlist->id_pos];
63575562573SAdrian Hunter 	} else {
63675562573SAdrian Hunter 		if (evlist->is_pos > n)
63775562573SAdrian Hunter 			return -1;
63875562573SAdrian Hunter 		n -= evlist->is_pos;
63975562573SAdrian Hunter 		*id = array[n];
64075562573SAdrian Hunter 	}
64175562573SAdrian Hunter 	return 0;
64275562573SAdrian Hunter }
64375562573SAdrian Hunter 
6447cb5c5acSJiri Olsa struct perf_evsel *perf_evlist__event2evsel(struct perf_evlist *evlist,
64575562573SAdrian Hunter 					    union perf_event *event)
64675562573SAdrian Hunter {
64798be6966SAdrian Hunter 	struct perf_evsel *first = perf_evlist__first(evlist);
64875562573SAdrian Hunter 	struct hlist_head *head;
64975562573SAdrian Hunter 	struct perf_sample_id *sid;
65075562573SAdrian Hunter 	int hash;
65175562573SAdrian Hunter 	u64 id;
65275562573SAdrian Hunter 
65375562573SAdrian Hunter 	if (evlist->nr_entries == 1)
65498be6966SAdrian Hunter 		return first;
65598be6966SAdrian Hunter 
65698be6966SAdrian Hunter 	if (!first->attr.sample_id_all &&
65798be6966SAdrian Hunter 	    event->header.type != PERF_RECORD_SAMPLE)
65898be6966SAdrian Hunter 		return first;
65975562573SAdrian Hunter 
66075562573SAdrian Hunter 	if (perf_evlist__event2id(evlist, event, &id))
66175562573SAdrian Hunter 		return NULL;
66275562573SAdrian Hunter 
66375562573SAdrian Hunter 	/* Synthesized events have an id of zero */
66475562573SAdrian Hunter 	if (!id)
66598be6966SAdrian Hunter 		return first;
66675562573SAdrian Hunter 
66775562573SAdrian Hunter 	hash = hash_64(id, PERF_EVLIST__HLIST_BITS);
66875562573SAdrian Hunter 	head = &evlist->heads[hash];
66975562573SAdrian Hunter 
67075562573SAdrian Hunter 	hlist_for_each_entry(sid, head, node) {
67175562573SAdrian Hunter 		if (sid->id == id)
67275562573SAdrian Hunter 			return sid->evsel;
67375562573SAdrian Hunter 	}
67475562573SAdrian Hunter 	return NULL;
67575562573SAdrian Hunter }
67675562573SAdrian Hunter 
67765aea233SWang Nan static int perf_evlist__set_paused(struct perf_evlist *evlist, bool value)
67865aea233SWang Nan {
67965aea233SWang Nan 	int i;
68065aea233SWang Nan 
681078c3386SWang Nan 	if (!evlist->backward_mmap)
682078c3386SWang Nan 		return 0;
683078c3386SWang Nan 
68465aea233SWang Nan 	for (i = 0; i < evlist->nr_mmaps; i++) {
685078c3386SWang Nan 		int fd = evlist->backward_mmap[i].fd;
68665aea233SWang Nan 		int err;
68765aea233SWang Nan 
68865aea233SWang Nan 		if (fd < 0)
68965aea233SWang Nan 			continue;
69065aea233SWang Nan 		err = ioctl(fd, PERF_EVENT_IOC_PAUSE_OUTPUT, value ? 1 : 0);
69165aea233SWang Nan 		if (err)
69265aea233SWang Nan 			return err;
69365aea233SWang Nan 	}
69465aea233SWang Nan 	return 0;
69565aea233SWang Nan }
69665aea233SWang Nan 
697f6cdff83SWang Nan static int perf_evlist__pause(struct perf_evlist *evlist)
69865aea233SWang Nan {
69965aea233SWang Nan 	return perf_evlist__set_paused(evlist, true);
70065aea233SWang Nan }
70165aea233SWang Nan 
702f6cdff83SWang Nan static int perf_evlist__resume(struct perf_evlist *evlist)
70365aea233SWang Nan {
70465aea233SWang Nan 	return perf_evlist__set_paused(evlist, false);
70565aea233SWang Nan }
70665aea233SWang Nan 
707b6b85dadSWang Nan /* When check_messup is true, 'end' must points to a good entry */
7080f4ccd11SWang Nan static union perf_event *
709b6b85dadSWang Nan perf_mmap__read(struct perf_mmap *md, bool check_messup, u64 start,
710b6b85dadSWang Nan 		u64 end, u64 *prev)
71104391debSArnaldo Carvalho de Melo {
71204391debSArnaldo Carvalho de Melo 	unsigned char *data = md->base + page_size;
7138115d60cSArnaldo Carvalho de Melo 	union perf_event *event = NULL;
714b6b85dadSWang Nan 	int diff = end - start;
71504391debSArnaldo Carvalho de Melo 
716b6b85dadSWang Nan 	if (check_messup) {
71704391debSArnaldo Carvalho de Melo 		/*
71804391debSArnaldo Carvalho de Melo 		 * If we're further behind than half the buffer, there's a chance
71904391debSArnaldo Carvalho de Melo 		 * the writer will bite our tail and mess up the samples under us.
72004391debSArnaldo Carvalho de Melo 		 *
721b6b85dadSWang Nan 		 * If we somehow ended up ahead of the 'end', we got messed up.
72204391debSArnaldo Carvalho de Melo 		 *
723b6b85dadSWang Nan 		 * In either case, truncate and restart at 'end'.
72404391debSArnaldo Carvalho de Melo 		 */
72504391debSArnaldo Carvalho de Melo 		if (diff > md->mask / 2 || diff < 0) {
72604391debSArnaldo Carvalho de Melo 			fprintf(stderr, "WARNING: failed to keep up with mmap data.\n");
72704391debSArnaldo Carvalho de Melo 
72804391debSArnaldo Carvalho de Melo 			/*
729b6b85dadSWang Nan 			 * 'end' points to a known good entry, start there.
73004391debSArnaldo Carvalho de Melo 			 */
731b6b85dadSWang Nan 			start = end;
732b04b7023SWang Nan 			diff = 0;
73304391debSArnaldo Carvalho de Melo 		}
7347bb41152SArnaldo Carvalho de Melo 	}
73504391debSArnaldo Carvalho de Melo 
736b04b7023SWang Nan 	if (diff >= (int)sizeof(event->header)) {
73704391debSArnaldo Carvalho de Melo 		size_t size;
73804391debSArnaldo Carvalho de Melo 
739b6b85dadSWang Nan 		event = (union perf_event *)&data[start & md->mask];
74004391debSArnaldo Carvalho de Melo 		size = event->header.size;
74104391debSArnaldo Carvalho de Melo 
742b04b7023SWang Nan 		if (size < sizeof(event->header) || diff < (int)size) {
743b04b7023SWang Nan 			event = NULL;
744b04b7023SWang Nan 			goto broken_event;
745b04b7023SWang Nan 		}
746b04b7023SWang Nan 
74704391debSArnaldo Carvalho de Melo 		/*
74804391debSArnaldo Carvalho de Melo 		 * Event straddles the mmap boundary -- header should always
74904391debSArnaldo Carvalho de Melo 		 * be inside due to u64 alignment of output.
75004391debSArnaldo Carvalho de Melo 		 */
751b6b85dadSWang Nan 		if ((start & md->mask) + size != ((start + size) & md->mask)) {
752b6b85dadSWang Nan 			unsigned int offset = start;
75304391debSArnaldo Carvalho de Melo 			unsigned int len = min(sizeof(*event), size), cpy;
754a65cb4b9SJiri Olsa 			void *dst = md->event_copy;
75504391debSArnaldo Carvalho de Melo 
75604391debSArnaldo Carvalho de Melo 			do {
75704391debSArnaldo Carvalho de Melo 				cpy = min(md->mask + 1 - (offset & md->mask), len);
75804391debSArnaldo Carvalho de Melo 				memcpy(dst, &data[offset & md->mask], cpy);
75904391debSArnaldo Carvalho de Melo 				offset += cpy;
76004391debSArnaldo Carvalho de Melo 				dst += cpy;
76104391debSArnaldo Carvalho de Melo 				len -= cpy;
76204391debSArnaldo Carvalho de Melo 			} while (len);
76304391debSArnaldo Carvalho de Melo 
764a65cb4b9SJiri Olsa 			event = (union perf_event *) md->event_copy;
76504391debSArnaldo Carvalho de Melo 		}
76604391debSArnaldo Carvalho de Melo 
767b6b85dadSWang Nan 		start += size;
76804391debSArnaldo Carvalho de Melo 	}
76904391debSArnaldo Carvalho de Melo 
770b04b7023SWang Nan broken_event:
7710f4ccd11SWang Nan 	if (prev)
772b6b85dadSWang Nan 		*prev = start;
7737bb41152SArnaldo Carvalho de Melo 
77404391debSArnaldo Carvalho de Melo 	return event;
77504391debSArnaldo Carvalho de Melo }
776f8a95309SArnaldo Carvalho de Melo 
7778db6d6b1SWang Nan union perf_event *perf_mmap__read_forward(struct perf_mmap *md, bool check_messup)
7780f4ccd11SWang Nan {
7790f4ccd11SWang Nan 	u64 head;
7800f4ccd11SWang Nan 	u64 old = md->prev;
7810f4ccd11SWang Nan 
7820f4ccd11SWang Nan 	/*
7830f4ccd11SWang Nan 	 * Check if event was unmapped due to a POLLHUP/POLLERR.
7840f4ccd11SWang Nan 	 */
78525a3720cSElena Reshetova 	if (!refcount_read(&md->refcnt))
7860f4ccd11SWang Nan 		return NULL;
7870f4ccd11SWang Nan 
7880f4ccd11SWang Nan 	head = perf_mmap__read_head(md);
7890f4ccd11SWang Nan 
7908db6d6b1SWang Nan 	return perf_mmap__read(md, check_messup, old, head, &md->prev);
7910f4ccd11SWang Nan }
7920f4ccd11SWang Nan 
793e24c7520SWang Nan union perf_event *
7948db6d6b1SWang Nan perf_mmap__read_backward(struct perf_mmap *md)
795e24c7520SWang Nan {
796e24c7520SWang Nan 	u64 head, end;
797e24c7520SWang Nan 	u64 start = md->prev;
798e24c7520SWang Nan 
799e24c7520SWang Nan 	/*
800e24c7520SWang Nan 	 * Check if event was unmapped due to a POLLHUP/POLLERR.
801e24c7520SWang Nan 	 */
80225a3720cSElena Reshetova 	if (!refcount_read(&md->refcnt))
803e24c7520SWang Nan 		return NULL;
804e24c7520SWang Nan 
805e24c7520SWang Nan 	head = perf_mmap__read_head(md);
806e24c7520SWang Nan 	if (!head)
807e24c7520SWang Nan 		return NULL;
808e24c7520SWang Nan 
809e24c7520SWang Nan 	/*
810e24c7520SWang Nan 	 * 'head' pointer starts from 0. Kernel minus sizeof(record) form
811e24c7520SWang Nan 	 * it each time when kernel writes to it, so in fact 'head' is
812e24c7520SWang Nan 	 * negative. 'end' pointer is made manually by adding the size of
813e24c7520SWang Nan 	 * the ring buffer to 'head' pointer, means the validate data can
814e24c7520SWang Nan 	 * read is the whole ring buffer. If 'end' is positive, the ring
815e24c7520SWang Nan 	 * buffer has not fully filled, so we must adjust 'end' to 0.
816e24c7520SWang Nan 	 *
817e24c7520SWang Nan 	 * However, since both 'head' and 'end' is unsigned, we can't
818e24c7520SWang Nan 	 * simply compare 'end' against 0. Here we compare '-head' and
819e24c7520SWang Nan 	 * the size of the ring buffer, where -head is the number of bytes
820e24c7520SWang Nan 	 * kernel write to the ring buffer.
821e24c7520SWang Nan 	 */
822e24c7520SWang Nan 	if (-head < (u64)(md->mask + 1))
823e24c7520SWang Nan 		end = 0;
824e24c7520SWang Nan 	else
825e24c7520SWang Nan 		end = head + md->mask + 1;
826e24c7520SWang Nan 
827e24c7520SWang Nan 	return perf_mmap__read(md, false, start, end, &md->prev);
828e24c7520SWang Nan }
829e24c7520SWang Nan 
8308db6d6b1SWang Nan union perf_event *perf_evlist__mmap_read_forward(struct perf_evlist *evlist, int idx)
8318db6d6b1SWang Nan {
8328db6d6b1SWang Nan 	struct perf_mmap *md = &evlist->mmap[idx];
8338db6d6b1SWang Nan 
8348db6d6b1SWang Nan 	/*
8358db6d6b1SWang Nan 	 * Check messup is required for forward overwritable ring buffer:
8368db6d6b1SWang Nan 	 * memory pointed by md->prev can be overwritten in this case.
8378db6d6b1SWang Nan 	 * No need for read-write ring buffer: kernel stop outputting when
8388db6d6b1SWang Nan 	 * it hit md->prev (perf_mmap__consume()).
8398db6d6b1SWang Nan 	 */
8408db6d6b1SWang Nan 	return perf_mmap__read_forward(md, evlist->overwrite);
8418db6d6b1SWang Nan }
8428db6d6b1SWang Nan 
8438db6d6b1SWang Nan union perf_event *perf_evlist__mmap_read_backward(struct perf_evlist *evlist, int idx)
8448db6d6b1SWang Nan {
8458db6d6b1SWang Nan 	struct perf_mmap *md = &evlist->mmap[idx];
8468db6d6b1SWang Nan 
8478db6d6b1SWang Nan 	/*
8488db6d6b1SWang Nan 	 * No need to check messup for backward ring buffer:
8498db6d6b1SWang Nan 	 * We can always read arbitrary long data from a backward
8508db6d6b1SWang Nan 	 * ring buffer unless we forget to pause it before reading.
8518db6d6b1SWang Nan 	 */
8528db6d6b1SWang Nan 	return perf_mmap__read_backward(md);
8538db6d6b1SWang Nan }
8548db6d6b1SWang Nan 
8555a5ddeb6SWang Nan union perf_event *perf_evlist__mmap_read(struct perf_evlist *evlist, int idx)
8565a5ddeb6SWang Nan {
8575a5ddeb6SWang Nan 	return perf_evlist__mmap_read_forward(evlist, idx);
8585a5ddeb6SWang Nan }
8595a5ddeb6SWang Nan 
8608db6d6b1SWang Nan void perf_mmap__read_catchup(struct perf_mmap *md)
861e24c7520SWang Nan {
862e24c7520SWang Nan 	u64 head;
863e24c7520SWang Nan 
86425a3720cSElena Reshetova 	if (!refcount_read(&md->refcnt))
865e24c7520SWang Nan 		return;
866e24c7520SWang Nan 
867e24c7520SWang Nan 	head = perf_mmap__read_head(md);
868e24c7520SWang Nan 	md->prev = head;
869e24c7520SWang Nan }
870e24c7520SWang Nan 
8718db6d6b1SWang Nan void perf_evlist__mmap_read_catchup(struct perf_evlist *evlist, int idx)
8728db6d6b1SWang Nan {
8738db6d6b1SWang Nan 	perf_mmap__read_catchup(&evlist->mmap[idx]);
8748db6d6b1SWang Nan }
8758db6d6b1SWang Nan 
87682396986SArnaldo Carvalho de Melo static bool perf_mmap__empty(struct perf_mmap *md)
87782396986SArnaldo Carvalho de Melo {
878b72e74d1SAdrian Hunter 	return perf_mmap__read_head(md) == md->prev && !md->auxtrace_mmap.base;
87982396986SArnaldo Carvalho de Melo }
88082396986SArnaldo Carvalho de Melo 
8818db6d6b1SWang Nan static void perf_mmap__get(struct perf_mmap *map)
8828db6d6b1SWang Nan {
88325a3720cSElena Reshetova 	refcount_inc(&map->refcnt);
8848db6d6b1SWang Nan }
8858db6d6b1SWang Nan 
8868db6d6b1SWang Nan static void perf_mmap__put(struct perf_mmap *md)
8878db6d6b1SWang Nan {
88825a3720cSElena Reshetova 	BUG_ON(md->base && refcount_read(&md->refcnt) == 0);
8898db6d6b1SWang Nan 
89025a3720cSElena Reshetova 	if (refcount_dec_and_test(&md->refcnt))
8918db6d6b1SWang Nan 		perf_mmap__munmap(md);
8928db6d6b1SWang Nan }
8938db6d6b1SWang Nan 
8948db6d6b1SWang Nan void perf_mmap__consume(struct perf_mmap *md, bool overwrite)
8958e50d384SZhouyi Zhou {
8968db6d6b1SWang Nan 	if (!overwrite) {
8977b8283b5SDavid Ahern 		u64 old = md->prev;
8988e50d384SZhouyi Zhou 
8998e50d384SZhouyi Zhou 		perf_mmap__write_tail(md, old);
9008e50d384SZhouyi Zhou 	}
90182396986SArnaldo Carvalho de Melo 
90225a3720cSElena Reshetova 	if (refcount_read(&md->refcnt) == 1 && perf_mmap__empty(md))
9038db6d6b1SWang Nan 		perf_mmap__put(md);
9048db6d6b1SWang Nan }
9058db6d6b1SWang Nan 
9068db6d6b1SWang Nan void perf_evlist__mmap_consume(struct perf_evlist *evlist, int idx)
9078db6d6b1SWang Nan {
9088db6d6b1SWang Nan 	perf_mmap__consume(&evlist->mmap[idx], evlist->overwrite);
9098e50d384SZhouyi Zhou }
9108e50d384SZhouyi Zhou 
911718c602dSAdrian Hunter int __weak auxtrace_mmap__mmap(struct auxtrace_mmap *mm __maybe_unused,
912718c602dSAdrian Hunter 			       struct auxtrace_mmap_params *mp __maybe_unused,
913718c602dSAdrian Hunter 			       void *userpg __maybe_unused,
914718c602dSAdrian Hunter 			       int fd __maybe_unused)
915718c602dSAdrian Hunter {
916718c602dSAdrian Hunter 	return 0;
917718c602dSAdrian Hunter }
918718c602dSAdrian Hunter 
919718c602dSAdrian Hunter void __weak auxtrace_mmap__munmap(struct auxtrace_mmap *mm __maybe_unused)
920718c602dSAdrian Hunter {
921718c602dSAdrian Hunter }
922718c602dSAdrian Hunter 
923718c602dSAdrian Hunter void __weak auxtrace_mmap_params__init(
924718c602dSAdrian Hunter 			struct auxtrace_mmap_params *mp __maybe_unused,
925718c602dSAdrian Hunter 			off_t auxtrace_offset __maybe_unused,
926718c602dSAdrian Hunter 			unsigned int auxtrace_pages __maybe_unused,
927718c602dSAdrian Hunter 			bool auxtrace_overwrite __maybe_unused)
928718c602dSAdrian Hunter {
929718c602dSAdrian Hunter }
930718c602dSAdrian Hunter 
931718c602dSAdrian Hunter void __weak auxtrace_mmap_params__set_idx(
932718c602dSAdrian Hunter 			struct auxtrace_mmap_params *mp __maybe_unused,
933718c602dSAdrian Hunter 			struct perf_evlist *evlist __maybe_unused,
934718c602dSAdrian Hunter 			int idx __maybe_unused,
935718c602dSAdrian Hunter 			bool per_cpu __maybe_unused)
936718c602dSAdrian Hunter {
937718c602dSAdrian Hunter }
938718c602dSAdrian Hunter 
9398db6d6b1SWang Nan static void perf_mmap__munmap(struct perf_mmap *map)
9408db6d6b1SWang Nan {
9418db6d6b1SWang Nan 	if (map->base != NULL) {
9428db6d6b1SWang Nan 		munmap(map->base, perf_mmap__mmap_len(map));
9438db6d6b1SWang Nan 		map->base = NULL;
9448db6d6b1SWang Nan 		map->fd = -1;
94525a3720cSElena Reshetova 		refcount_set(&map->refcnt, 0);
9468db6d6b1SWang Nan 	}
9478db6d6b1SWang Nan 	auxtrace_mmap__munmap(&map->auxtrace_mmap);
9488db6d6b1SWang Nan }
9498db6d6b1SWang Nan 
950a1f72618SWang Nan static void perf_evlist__munmap_nofree(struct perf_evlist *evlist)
951f8a95309SArnaldo Carvalho de Melo {
952aece948fSArnaldo Carvalho de Melo 	int i;
953f8a95309SArnaldo Carvalho de Melo 
954b2cb615dSWang Nan 	if (evlist->mmap)
95593edcbd9SAdrian Hunter 		for (i = 0; i < evlist->nr_mmaps; i++)
956a1f72618SWang Nan 			perf_mmap__munmap(&evlist->mmap[i]);
957b2cb615dSWang Nan 
958b2cb615dSWang Nan 	if (evlist->backward_mmap)
959b2cb615dSWang Nan 		for (i = 0; i < evlist->nr_mmaps; i++)
960b2cb615dSWang Nan 			perf_mmap__munmap(&evlist->backward_mmap[i]);
961a1f72618SWang Nan }
962aece948fSArnaldo Carvalho de Melo 
963a1f72618SWang Nan void perf_evlist__munmap(struct perf_evlist *evlist)
964a1f72618SWang Nan {
965a1f72618SWang Nan 	perf_evlist__munmap_nofree(evlist);
96604662523SArnaldo Carvalho de Melo 	zfree(&evlist->mmap);
967b2cb615dSWang Nan 	zfree(&evlist->backward_mmap);
968f8a95309SArnaldo Carvalho de Melo }
969f8a95309SArnaldo Carvalho de Melo 
9708db6d6b1SWang Nan static struct perf_mmap *perf_evlist__alloc_mmap(struct perf_evlist *evlist)
971f8a95309SArnaldo Carvalho de Melo {
972d4c6fb36SWang Nan 	int i;
9738db6d6b1SWang Nan 	struct perf_mmap *map;
974d4c6fb36SWang Nan 
975a14bb7a6SArnaldo Carvalho de Melo 	evlist->nr_mmaps = cpu_map__nr(evlist->cpus);
976ec1e7e43SSukadev Bhattiprolu 	if (cpu_map__empty(evlist->cpus))
977b3a319d5SNamhyung Kim 		evlist->nr_mmaps = thread_map__nr(evlist->threads);
9788db6d6b1SWang Nan 	map = zalloc(evlist->nr_mmaps * sizeof(struct perf_mmap));
9798db6d6b1SWang Nan 	if (!map)
9808db6d6b1SWang Nan 		return NULL;
981946ae1d4SWang Nan 
9824738ca30SArnaldo Carvalho de Melo 	for (i = 0; i < evlist->nr_mmaps; i++) {
9838db6d6b1SWang Nan 		map[i].fd = -1;
9844738ca30SArnaldo Carvalho de Melo 		/*
9854738ca30SArnaldo Carvalho de Melo 		 * When the perf_mmap() call is made we grab one refcount, plus
9864738ca30SArnaldo Carvalho de Melo 		 * one extra to let perf_evlist__mmap_consume() get the last
9874738ca30SArnaldo Carvalho de Melo 		 * events after all real references (perf_mmap__get()) are
9884738ca30SArnaldo Carvalho de Melo 		 * dropped.
9894738ca30SArnaldo Carvalho de Melo 		 *
9904738ca30SArnaldo Carvalho de Melo 		 * Each PERF_EVENT_IOC_SET_OUTPUT points to this mmap and
9914738ca30SArnaldo Carvalho de Melo 		 * thus does perf_mmap__get() on it.
9924738ca30SArnaldo Carvalho de Melo 		 */
9934738ca30SArnaldo Carvalho de Melo 		refcount_set(&map[i].refcnt, 0);
9944738ca30SArnaldo Carvalho de Melo 	}
9958db6d6b1SWang Nan 	return map;
996f8a95309SArnaldo Carvalho de Melo }
997f8a95309SArnaldo Carvalho de Melo 
998a8a8f3ebSAdrian Hunter struct mmap_params {
999a8a8f3ebSAdrian Hunter 	int prot;
1000a8a8f3ebSAdrian Hunter 	int mask;
1001718c602dSAdrian Hunter 	struct auxtrace_mmap_params auxtrace_mp;
1002a8a8f3ebSAdrian Hunter };
1003a8a8f3ebSAdrian Hunter 
10048db6d6b1SWang Nan static int perf_mmap__mmap(struct perf_mmap *map,
1005a8a8f3ebSAdrian Hunter 			   struct mmap_params *mp, int fd)
1006f8a95309SArnaldo Carvalho de Melo {
100782396986SArnaldo Carvalho de Melo 	/*
100882396986SArnaldo Carvalho de Melo 	 * The last one will be done at perf_evlist__mmap_consume(), so that we
100982396986SArnaldo Carvalho de Melo 	 * make sure we don't prevent tools from consuming every last event in
101082396986SArnaldo Carvalho de Melo 	 * the ring buffer.
101182396986SArnaldo Carvalho de Melo 	 *
101282396986SArnaldo Carvalho de Melo 	 * I.e. we can get the POLLHUP meaning that the fd doesn't exist
101382396986SArnaldo Carvalho de Melo 	 * anymore, but the last events for it are still in the ring buffer,
101482396986SArnaldo Carvalho de Melo 	 * waiting to be consumed.
101582396986SArnaldo Carvalho de Melo 	 *
101682396986SArnaldo Carvalho de Melo 	 * Tools can chose to ignore this at their own discretion, but the
101782396986SArnaldo Carvalho de Melo 	 * evlist layer can't just drop it when filtering events in
101882396986SArnaldo Carvalho de Melo 	 * perf_evlist__filter_pollfd().
101982396986SArnaldo Carvalho de Melo 	 */
102025a3720cSElena Reshetova 	refcount_set(&map->refcnt, 2);
10218db6d6b1SWang Nan 	map->prev = 0;
10228db6d6b1SWang Nan 	map->mask = mp->mask;
10238db6d6b1SWang Nan 	map->base = mmap(NULL, perf_mmap__mmap_len(map), mp->prot,
1024f8a95309SArnaldo Carvalho de Melo 			 MAP_SHARED, fd, 0);
10258db6d6b1SWang Nan 	if (map->base == MAP_FAILED) {
102602635965SAdrian Hunter 		pr_debug2("failed to mmap perf event ring buffer, error %d\n",
102702635965SAdrian Hunter 			  errno);
10288db6d6b1SWang Nan 		map->base = NULL;
1029f8a95309SArnaldo Carvalho de Melo 		return -1;
1030301b195dSNelson Elhage 	}
10318db6d6b1SWang Nan 	map->fd = fd;
1032ad6765ddSArnaldo Carvalho de Melo 
10338db6d6b1SWang Nan 	if (auxtrace_mmap__mmap(&map->auxtrace_mmap,
10348db6d6b1SWang Nan 				&mp->auxtrace_mp, map->base, fd))
1035718c602dSAdrian Hunter 		return -1;
1036718c602dSAdrian Hunter 
1037f8a95309SArnaldo Carvalho de Melo 	return 0;
1038f8a95309SArnaldo Carvalho de Melo }
1039f8a95309SArnaldo Carvalho de Melo 
1040f3058a1cSWang Nan static bool
1041f3058a1cSWang Nan perf_evlist__should_poll(struct perf_evlist *evlist __maybe_unused,
1042f3058a1cSWang Nan 			 struct perf_evsel *evsel)
1043f3058a1cSWang Nan {
104432a951b4SArnaldo Carvalho de Melo 	if (evsel->attr.write_backward)
1045f3058a1cSWang Nan 		return false;
1046f3058a1cSWang Nan 	return true;
1047f3058a1cSWang Nan }
1048f3058a1cSWang Nan 
104904e21314SAdrian Hunter static int perf_evlist__mmap_per_evsel(struct perf_evlist *evlist, int idx,
10509f21b815SMark Rutland 				       struct mmap_params *mp, int cpu_idx,
1051078c3386SWang Nan 				       int thread, int *_output, int *_output_backward)
1052aece948fSArnaldo Carvalho de Melo {
1053aece948fSArnaldo Carvalho de Melo 	struct perf_evsel *evsel;
1054f3058a1cSWang Nan 	int revent;
10559f21b815SMark Rutland 	int evlist_cpu = cpu_map__cpu(evlist->cpus, cpu_idx);
105604e21314SAdrian Hunter 
1057e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, evsel) {
1058078c3386SWang Nan 		struct perf_mmap *maps = evlist->mmap;
1059078c3386SWang Nan 		int *output = _output;
1060bf8e8f4bSAdrian Hunter 		int fd;
10619f21b815SMark Rutland 		int cpu;
1062bf8e8f4bSAdrian Hunter 
1063078c3386SWang Nan 		if (evsel->attr.write_backward) {
1064078c3386SWang Nan 			output = _output_backward;
1065078c3386SWang Nan 			maps = evlist->backward_mmap;
1066078c3386SWang Nan 
1067078c3386SWang Nan 			if (!maps) {
1068078c3386SWang Nan 				maps = perf_evlist__alloc_mmap(evlist);
1069078c3386SWang Nan 				if (!maps)
1070078c3386SWang Nan 					return -1;
1071078c3386SWang Nan 				evlist->backward_mmap = maps;
107254cc54deSWang Nan 				if (evlist->bkw_mmap_state == BKW_MMAP_NOTREADY)
107354cc54deSWang Nan 					perf_evlist__toggle_bkw_mmap(evlist, BKW_MMAP_RUNNING);
1074078c3386SWang Nan 			}
1075078c3386SWang Nan 		}
1076f3058a1cSWang Nan 
1077bf8e8f4bSAdrian Hunter 		if (evsel->system_wide && thread)
1078bf8e8f4bSAdrian Hunter 			continue;
1079bf8e8f4bSAdrian Hunter 
10809f21b815SMark Rutland 		cpu = cpu_map__idx(evsel->cpus, evlist_cpu);
10819f21b815SMark Rutland 		if (cpu == -1)
10829f21b815SMark Rutland 			continue;
10839f21b815SMark Rutland 
1084bf8e8f4bSAdrian Hunter 		fd = FD(evsel, cpu, thread);
108504e21314SAdrian Hunter 
108604e21314SAdrian Hunter 		if (*output == -1) {
108704e21314SAdrian Hunter 			*output = fd;
1088078c3386SWang Nan 
1089078c3386SWang Nan 			if (perf_mmap__mmap(&maps[idx], mp, *output)  < 0)
109004e21314SAdrian Hunter 				return -1;
109104e21314SAdrian Hunter 		} else {
109204e21314SAdrian Hunter 			if (ioctl(fd, PERF_EVENT_IOC_SET_OUTPUT, *output) != 0)
109304e21314SAdrian Hunter 				return -1;
109482396986SArnaldo Carvalho de Melo 
1095078c3386SWang Nan 			perf_mmap__get(&maps[idx]);
109604e21314SAdrian Hunter 		}
109704e21314SAdrian Hunter 
1098f3058a1cSWang Nan 		revent = perf_evlist__should_poll(evlist, evsel) ? POLLIN : 0;
1099f3058a1cSWang Nan 
1100f90d194aSAdrian Hunter 		/*
1101f90d194aSAdrian Hunter 		 * The system_wide flag causes a selected event to be opened
1102f90d194aSAdrian Hunter 		 * always without a pid.  Consequently it will never get a
1103f90d194aSAdrian Hunter 		 * POLLHUP, but it is used for tracking in combination with
1104f90d194aSAdrian Hunter 		 * other events, so it should not need to be polled anyway.
1105f90d194aSAdrian Hunter 		 * Therefore don't add it for polling.
1106f90d194aSAdrian Hunter 		 */
1107f90d194aSAdrian Hunter 		if (!evsel->system_wide &&
1108078c3386SWang Nan 		    __perf_evlist__add_pollfd(evlist, fd, &maps[idx], revent) < 0) {
1109078c3386SWang Nan 			perf_mmap__put(&maps[idx]);
1110ad6765ddSArnaldo Carvalho de Melo 			return -1;
111182396986SArnaldo Carvalho de Melo 		}
1112033fa713SArnaldo Carvalho de Melo 
11133c659eedSAdrian Hunter 		if (evsel->attr.read_format & PERF_FORMAT_ID) {
11143c659eedSAdrian Hunter 			if (perf_evlist__id_add_fd(evlist, evsel, cpu, thread,
11153c659eedSAdrian Hunter 						   fd) < 0)
111604e21314SAdrian Hunter 				return -1;
11173c659eedSAdrian Hunter 			perf_evlist__set_sid_idx(evlist, evsel, idx, cpu,
11183c659eedSAdrian Hunter 						 thread);
11193c659eedSAdrian Hunter 		}
112004e21314SAdrian Hunter 	}
112104e21314SAdrian Hunter 
112204e21314SAdrian Hunter 	return 0;
112304e21314SAdrian Hunter }
112404e21314SAdrian Hunter 
1125a8a8f3ebSAdrian Hunter static int perf_evlist__mmap_per_cpu(struct perf_evlist *evlist,
1126a8a8f3ebSAdrian Hunter 				     struct mmap_params *mp)
112704e21314SAdrian Hunter {
1128aece948fSArnaldo Carvalho de Melo 	int cpu, thread;
1129b3a319d5SNamhyung Kim 	int nr_cpus = cpu_map__nr(evlist->cpus);
1130b3a319d5SNamhyung Kim 	int nr_threads = thread_map__nr(evlist->threads);
1131aece948fSArnaldo Carvalho de Melo 
1132e3e1a54fSAdrian Hunter 	pr_debug2("perf event ring buffer mmapped per cpu\n");
1133b3a319d5SNamhyung Kim 	for (cpu = 0; cpu < nr_cpus; cpu++) {
1134aece948fSArnaldo Carvalho de Melo 		int output = -1;
1135078c3386SWang Nan 		int output_backward = -1;
1136aece948fSArnaldo Carvalho de Melo 
1137718c602dSAdrian Hunter 		auxtrace_mmap_params__set_idx(&mp->auxtrace_mp, evlist, cpu,
1138718c602dSAdrian Hunter 					      true);
1139718c602dSAdrian Hunter 
1140b3a319d5SNamhyung Kim 		for (thread = 0; thread < nr_threads; thread++) {
1141a8a8f3ebSAdrian Hunter 			if (perf_evlist__mmap_per_evsel(evlist, cpu, mp, cpu,
1142078c3386SWang Nan 							thread, &output, &output_backward))
1143aece948fSArnaldo Carvalho de Melo 				goto out_unmap;
1144aece948fSArnaldo Carvalho de Melo 		}
1145aece948fSArnaldo Carvalho de Melo 	}
1146aece948fSArnaldo Carvalho de Melo 
1147aece948fSArnaldo Carvalho de Melo 	return 0;
1148aece948fSArnaldo Carvalho de Melo 
1149aece948fSArnaldo Carvalho de Melo out_unmap:
1150a1f72618SWang Nan 	perf_evlist__munmap_nofree(evlist);
1151aece948fSArnaldo Carvalho de Melo 	return -1;
1152aece948fSArnaldo Carvalho de Melo }
1153aece948fSArnaldo Carvalho de Melo 
1154a8a8f3ebSAdrian Hunter static int perf_evlist__mmap_per_thread(struct perf_evlist *evlist,
1155a8a8f3ebSAdrian Hunter 					struct mmap_params *mp)
1156aece948fSArnaldo Carvalho de Melo {
1157aece948fSArnaldo Carvalho de Melo 	int thread;
1158b3a319d5SNamhyung Kim 	int nr_threads = thread_map__nr(evlist->threads);
1159aece948fSArnaldo Carvalho de Melo 
1160e3e1a54fSAdrian Hunter 	pr_debug2("perf event ring buffer mmapped per thread\n");
1161b3a319d5SNamhyung Kim 	for (thread = 0; thread < nr_threads; thread++) {
1162aece948fSArnaldo Carvalho de Melo 		int output = -1;
1163078c3386SWang Nan 		int output_backward = -1;
1164aece948fSArnaldo Carvalho de Melo 
1165718c602dSAdrian Hunter 		auxtrace_mmap_params__set_idx(&mp->auxtrace_mp, evlist, thread,
1166718c602dSAdrian Hunter 					      false);
1167718c602dSAdrian Hunter 
1168a8a8f3ebSAdrian Hunter 		if (perf_evlist__mmap_per_evsel(evlist, thread, mp, 0, thread,
1169078c3386SWang Nan 						&output, &output_backward))
1170aece948fSArnaldo Carvalho de Melo 			goto out_unmap;
1171aece948fSArnaldo Carvalho de Melo 	}
1172aece948fSArnaldo Carvalho de Melo 
1173aece948fSArnaldo Carvalho de Melo 	return 0;
1174aece948fSArnaldo Carvalho de Melo 
1175aece948fSArnaldo Carvalho de Melo out_unmap:
1176a1f72618SWang Nan 	perf_evlist__munmap_nofree(evlist);
1177aece948fSArnaldo Carvalho de Melo 	return -1;
1178aece948fSArnaldo Carvalho de Melo }
1179aece948fSArnaldo Carvalho de Melo 
1180f5e7150cSArnaldo Carvalho de Melo unsigned long perf_event_mlock_kb_in_pages(void)
1181994a1f78SJiri Olsa {
1182f5e7150cSArnaldo Carvalho de Melo 	unsigned long pages;
11838185e881SArnaldo Carvalho de Melo 	int max;
11848185e881SArnaldo Carvalho de Melo 
11858185e881SArnaldo Carvalho de Melo 	if (sysctl__read_int("kernel/perf_event_mlock_kb", &max) < 0) {
11868185e881SArnaldo Carvalho de Melo 		/*
11878185e881SArnaldo Carvalho de Melo 		 * Pick a once upon a time good value, i.e. things look
11888185e881SArnaldo Carvalho de Melo 		 * strange since we can't read a sysctl value, but lets not
11898185e881SArnaldo Carvalho de Melo 		 * die yet...
11908185e881SArnaldo Carvalho de Melo 		 */
11918185e881SArnaldo Carvalho de Melo 		max = 512;
11928185e881SArnaldo Carvalho de Melo 	} else {
11938185e881SArnaldo Carvalho de Melo 		max -= (page_size / 1024);
11948185e881SArnaldo Carvalho de Melo 	}
11958185e881SArnaldo Carvalho de Melo 
11968185e881SArnaldo Carvalho de Melo 	pages = (max * 1024) / page_size;
11971be300f4SArnaldo Carvalho de Melo 	if (!is_power_of_2(pages))
11981be300f4SArnaldo Carvalho de Melo 		pages = rounddown_pow_of_two(pages);
1199f5e7150cSArnaldo Carvalho de Melo 
1200f5e7150cSArnaldo Carvalho de Melo 	return pages;
1201f5e7150cSArnaldo Carvalho de Melo }
1202f5e7150cSArnaldo Carvalho de Melo 
12030c582449SJiri Olsa size_t perf_evlist__mmap_size(unsigned long pages)
1204f5e7150cSArnaldo Carvalho de Melo {
1205f5e7150cSArnaldo Carvalho de Melo 	if (pages == UINT_MAX)
1206f5e7150cSArnaldo Carvalho de Melo 		pages = perf_event_mlock_kb_in_pages();
1207f5e7150cSArnaldo Carvalho de Melo 	else if (!is_power_of_2(pages))
1208994a1f78SJiri Olsa 		return 0;
1209994a1f78SJiri Olsa 
1210994a1f78SJiri Olsa 	return (pages + 1) * page_size;
1211994a1f78SJiri Olsa }
1212994a1f78SJiri Olsa 
121333c2dcfdSDavid Ahern static long parse_pages_arg(const char *str, unsigned long min,
121433c2dcfdSDavid Ahern 			    unsigned long max)
1215994a1f78SJiri Olsa {
12162fbe4abeSAdrian Hunter 	unsigned long pages, val;
121727050f53SJiri Olsa 	static struct parse_tag tags[] = {
121827050f53SJiri Olsa 		{ .tag  = 'B', .mult = 1       },
121927050f53SJiri Olsa 		{ .tag  = 'K', .mult = 1 << 10 },
122027050f53SJiri Olsa 		{ .tag  = 'M', .mult = 1 << 20 },
122127050f53SJiri Olsa 		{ .tag  = 'G', .mult = 1 << 30 },
122227050f53SJiri Olsa 		{ .tag  = 0 },
122327050f53SJiri Olsa 	};
1224994a1f78SJiri Olsa 
12258973504bSDavid Ahern 	if (str == NULL)
122633c2dcfdSDavid Ahern 		return -EINVAL;
12278973504bSDavid Ahern 
122827050f53SJiri Olsa 	val = parse_tag_value(str, tags);
12292fbe4abeSAdrian Hunter 	if (val != (unsigned long) -1) {
123027050f53SJiri Olsa 		/* we got file size value */
123127050f53SJiri Olsa 		pages = PERF_ALIGN(val, page_size) / page_size;
123227050f53SJiri Olsa 	} else {
123327050f53SJiri Olsa 		/* we got pages count value */
123427050f53SJiri Olsa 		char *eptr;
1235994a1f78SJiri Olsa 		pages = strtoul(str, &eptr, 10);
123633c2dcfdSDavid Ahern 		if (*eptr != '\0')
123733c2dcfdSDavid Ahern 			return -EINVAL;
123827050f53SJiri Olsa 	}
1239994a1f78SJiri Olsa 
12402bcab6c1SAdrian Hunter 	if (pages == 0 && min == 0) {
124133c2dcfdSDavid Ahern 		/* leave number of pages at 0 */
12421dbfa938SAdrian Hunter 	} else if (!is_power_of_2(pages)) {
12439808143bSJiri Olsa 		char buf[100];
12449808143bSJiri Olsa 
124533c2dcfdSDavid Ahern 		/* round pages up to next power of 2 */
124691529834SArnaldo Carvalho de Melo 		pages = roundup_pow_of_two(pages);
12471dbfa938SAdrian Hunter 		if (!pages)
12481dbfa938SAdrian Hunter 			return -EINVAL;
12499808143bSJiri Olsa 
12509808143bSJiri Olsa 		unit_number__scnprintf(buf, sizeof(buf), pages * page_size);
12519808143bSJiri Olsa 		pr_info("rounding mmap pages size to %s (%lu pages)\n",
12529808143bSJiri Olsa 			buf, pages);
12532fbe4abeSAdrian Hunter 	}
12542fbe4abeSAdrian Hunter 
125533c2dcfdSDavid Ahern 	if (pages > max)
125633c2dcfdSDavid Ahern 		return -EINVAL;
125733c2dcfdSDavid Ahern 
125833c2dcfdSDavid Ahern 	return pages;
125933c2dcfdSDavid Ahern }
126033c2dcfdSDavid Ahern 
1261e9db1310SAdrian Hunter int __perf_evlist__parse_mmap_pages(unsigned int *mmap_pages, const char *str)
126233c2dcfdSDavid Ahern {
126333c2dcfdSDavid Ahern 	unsigned long max = UINT_MAX;
126433c2dcfdSDavid Ahern 	long pages;
126533c2dcfdSDavid Ahern 
1266f5ae9c42SAdrian Hunter 	if (max > SIZE_MAX / page_size)
126733c2dcfdSDavid Ahern 		max = SIZE_MAX / page_size;
126833c2dcfdSDavid Ahern 
126933c2dcfdSDavid Ahern 	pages = parse_pages_arg(str, 1, max);
127033c2dcfdSDavid Ahern 	if (pages < 0) {
127133c2dcfdSDavid Ahern 		pr_err("Invalid argument for --mmap_pages/-m\n");
1272994a1f78SJiri Olsa 		return -1;
1273994a1f78SJiri Olsa 	}
1274994a1f78SJiri Olsa 
1275994a1f78SJiri Olsa 	*mmap_pages = pages;
1276994a1f78SJiri Olsa 	return 0;
1277994a1f78SJiri Olsa }
1278994a1f78SJiri Olsa 
1279e9db1310SAdrian Hunter int perf_evlist__parse_mmap_pages(const struct option *opt, const char *str,
1280e9db1310SAdrian Hunter 				  int unset __maybe_unused)
1281e9db1310SAdrian Hunter {
1282e9db1310SAdrian Hunter 	return __perf_evlist__parse_mmap_pages(opt->value, str);
1283e9db1310SAdrian Hunter }
1284e9db1310SAdrian Hunter 
1285c83fa7f2SAdrian Hunter /**
1286718c602dSAdrian Hunter  * perf_evlist__mmap_ex - Create mmaps to receive events.
1287c83fa7f2SAdrian Hunter  * @evlist: list of events
1288c83fa7f2SAdrian Hunter  * @pages: map length in pages
1289c83fa7f2SAdrian Hunter  * @overwrite: overwrite older events?
1290718c602dSAdrian Hunter  * @auxtrace_pages - auxtrace map length in pages
1291718c602dSAdrian Hunter  * @auxtrace_overwrite - overwrite older auxtrace data?
1292f8a95309SArnaldo Carvalho de Melo  *
1293c83fa7f2SAdrian Hunter  * If @overwrite is %false the user needs to signal event consumption using
1294c83fa7f2SAdrian Hunter  * perf_mmap__write_tail().  Using perf_evlist__mmap_read() does this
1295c83fa7f2SAdrian Hunter  * automatically.
1296f8a95309SArnaldo Carvalho de Melo  *
1297718c602dSAdrian Hunter  * Similarly, if @auxtrace_overwrite is %false the user needs to signal data
1298718c602dSAdrian Hunter  * consumption using auxtrace_mmap__write_tail().
1299718c602dSAdrian Hunter  *
1300c83fa7f2SAdrian Hunter  * Return: %0 on success, negative error code otherwise.
1301f8a95309SArnaldo Carvalho de Melo  */
1302718c602dSAdrian Hunter int perf_evlist__mmap_ex(struct perf_evlist *evlist, unsigned int pages,
1303718c602dSAdrian Hunter 			 bool overwrite, unsigned int auxtrace_pages,
1304718c602dSAdrian Hunter 			 bool auxtrace_overwrite)
1305f8a95309SArnaldo Carvalho de Melo {
1306aece948fSArnaldo Carvalho de Melo 	struct perf_evsel *evsel;
13077e2ed097SArnaldo Carvalho de Melo 	const struct cpu_map *cpus = evlist->cpus;
13087e2ed097SArnaldo Carvalho de Melo 	const struct thread_map *threads = evlist->threads;
1309a8a8f3ebSAdrian Hunter 	struct mmap_params mp = {
1310a8a8f3ebSAdrian Hunter 		.prot = PROT_READ | (overwrite ? 0 : PROT_WRITE),
1311a8a8f3ebSAdrian Hunter 	};
131250a682ceSArnaldo Carvalho de Melo 
13138db6d6b1SWang Nan 	if (!evlist->mmap)
13148db6d6b1SWang Nan 		evlist->mmap = perf_evlist__alloc_mmap(evlist);
13158db6d6b1SWang Nan 	if (!evlist->mmap)
1316f8a95309SArnaldo Carvalho de Melo 		return -ENOMEM;
1317f8a95309SArnaldo Carvalho de Melo 
13181b85337dSArnaldo Carvalho de Melo 	if (evlist->pollfd.entries == NULL && perf_evlist__alloc_pollfd(evlist) < 0)
1319f8a95309SArnaldo Carvalho de Melo 		return -ENOMEM;
1320f8a95309SArnaldo Carvalho de Melo 
1321f8a95309SArnaldo Carvalho de Melo 	evlist->overwrite = overwrite;
1322994a1f78SJiri Olsa 	evlist->mmap_len = perf_evlist__mmap_size(pages);
13232af68ef5SAdrian Hunter 	pr_debug("mmap size %zuB\n", evlist->mmap_len);
1324a8a8f3ebSAdrian Hunter 	mp.mask = evlist->mmap_len - page_size - 1;
1325f8a95309SArnaldo Carvalho de Melo 
1326718c602dSAdrian Hunter 	auxtrace_mmap_params__init(&mp.auxtrace_mp, evlist->mmap_len,
1327718c602dSAdrian Hunter 				   auxtrace_pages, auxtrace_overwrite);
1328718c602dSAdrian Hunter 
1329e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, evsel) {
1330f8a95309SArnaldo Carvalho de Melo 		if ((evsel->attr.read_format & PERF_FORMAT_ID) &&
1331a91e5431SArnaldo Carvalho de Melo 		    evsel->sample_id == NULL &&
1332a14bb7a6SArnaldo Carvalho de Melo 		    perf_evsel__alloc_id(evsel, cpu_map__nr(cpus), threads->nr) < 0)
1333f8a95309SArnaldo Carvalho de Melo 			return -ENOMEM;
1334f8a95309SArnaldo Carvalho de Melo 	}
1335f8a95309SArnaldo Carvalho de Melo 
1336ec1e7e43SSukadev Bhattiprolu 	if (cpu_map__empty(cpus))
1337a8a8f3ebSAdrian Hunter 		return perf_evlist__mmap_per_thread(evlist, &mp);
1338f8a95309SArnaldo Carvalho de Melo 
1339a8a8f3ebSAdrian Hunter 	return perf_evlist__mmap_per_cpu(evlist, &mp);
1340f8a95309SArnaldo Carvalho de Melo }
13417e2ed097SArnaldo Carvalho de Melo 
1342718c602dSAdrian Hunter int perf_evlist__mmap(struct perf_evlist *evlist, unsigned int pages,
1343718c602dSAdrian Hunter 		      bool overwrite)
1344718c602dSAdrian Hunter {
1345718c602dSAdrian Hunter 	return perf_evlist__mmap_ex(evlist, pages, overwrite, 0, false);
1346718c602dSAdrian Hunter }
1347718c602dSAdrian Hunter 
1348602ad878SArnaldo Carvalho de Melo int perf_evlist__create_maps(struct perf_evlist *evlist, struct target *target)
13497e2ed097SArnaldo Carvalho de Melo {
135074bfd2b2SAdrian Hunter 	struct cpu_map *cpus;
135174bfd2b2SAdrian Hunter 	struct thread_map *threads;
13527e2ed097SArnaldo Carvalho de Melo 
135374bfd2b2SAdrian Hunter 	threads = thread_map__new_str(target->pid, target->tid, target->uid);
135474bfd2b2SAdrian Hunter 
135574bfd2b2SAdrian Hunter 	if (!threads)
13567e2ed097SArnaldo Carvalho de Melo 		return -1;
13577e2ed097SArnaldo Carvalho de Melo 
13589c105fbcSDongsheng Yang 	if (target__uses_dummy_map(target))
135974bfd2b2SAdrian Hunter 		cpus = cpu_map__dummy_new();
1360879d77d0SNamhyung Kim 	else
136174bfd2b2SAdrian Hunter 		cpus = cpu_map__new(target->cpu_list);
13627e2ed097SArnaldo Carvalho de Melo 
136374bfd2b2SAdrian Hunter 	if (!cpus)
13647e2ed097SArnaldo Carvalho de Melo 		goto out_delete_threads;
13657e2ed097SArnaldo Carvalho de Melo 
1366ec9a77a7SAdrian Hunter 	evlist->has_user_cpus = !!target->cpu_list;
1367ec9a77a7SAdrian Hunter 
136874bfd2b2SAdrian Hunter 	perf_evlist__set_maps(evlist, cpus, threads);
1369d5bc056eSAdrian Hunter 
1370d5bc056eSAdrian Hunter 	return 0;
13717e2ed097SArnaldo Carvalho de Melo 
13727e2ed097SArnaldo Carvalho de Melo out_delete_threads:
137374bfd2b2SAdrian Hunter 	thread_map__put(threads);
13747e2ed097SArnaldo Carvalho de Melo 	return -1;
13757e2ed097SArnaldo Carvalho de Melo }
13767e2ed097SArnaldo Carvalho de Melo 
1377d5bc056eSAdrian Hunter void perf_evlist__set_maps(struct perf_evlist *evlist, struct cpu_map *cpus,
13783de5cfb0SJiri Olsa 			   struct thread_map *threads)
13793de5cfb0SJiri Olsa {
1380934e0f20SAdrian Hunter 	/*
1381934e0f20SAdrian Hunter 	 * Allow for the possibility that one or another of the maps isn't being
1382934e0f20SAdrian Hunter 	 * changed i.e. don't put it.  Note we are assuming the maps that are
1383934e0f20SAdrian Hunter 	 * being applied are brand new and evlist is taking ownership of the
1384934e0f20SAdrian Hunter 	 * original reference count of 1.  If that is not the case it is up to
1385934e0f20SAdrian Hunter 	 * the caller to increase the reference count.
1386934e0f20SAdrian Hunter 	 */
1387934e0f20SAdrian Hunter 	if (cpus != evlist->cpus) {
13883de5cfb0SJiri Olsa 		cpu_map__put(evlist->cpus);
1389a55e5663SArnaldo Carvalho de Melo 		evlist->cpus = cpu_map__get(cpus);
1390934e0f20SAdrian Hunter 	}
13913de5cfb0SJiri Olsa 
1392934e0f20SAdrian Hunter 	if (threads != evlist->threads) {
13933de5cfb0SJiri Olsa 		thread_map__put(evlist->threads);
1394a55e5663SArnaldo Carvalho de Melo 		evlist->threads = thread_map__get(threads);
1395934e0f20SAdrian Hunter 	}
13963de5cfb0SJiri Olsa 
1397ec9a77a7SAdrian Hunter 	perf_evlist__propagate_maps(evlist);
13983de5cfb0SJiri Olsa }
13993de5cfb0SJiri Olsa 
140022c8a376SArnaldo Carvalho de Melo void __perf_evlist__set_sample_bit(struct perf_evlist *evlist,
140122c8a376SArnaldo Carvalho de Melo 				   enum perf_event_sample_format bit)
140222c8a376SArnaldo Carvalho de Melo {
140322c8a376SArnaldo Carvalho de Melo 	struct perf_evsel *evsel;
140422c8a376SArnaldo Carvalho de Melo 
1405e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, evsel)
140622c8a376SArnaldo Carvalho de Melo 		__perf_evsel__set_sample_bit(evsel, bit);
140722c8a376SArnaldo Carvalho de Melo }
140822c8a376SArnaldo Carvalho de Melo 
140922c8a376SArnaldo Carvalho de Melo void __perf_evlist__reset_sample_bit(struct perf_evlist *evlist,
141022c8a376SArnaldo Carvalho de Melo 				     enum perf_event_sample_format bit)
141122c8a376SArnaldo Carvalho de Melo {
141222c8a376SArnaldo Carvalho de Melo 	struct perf_evsel *evsel;
141322c8a376SArnaldo Carvalho de Melo 
1414e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, evsel)
141522c8a376SArnaldo Carvalho de Melo 		__perf_evsel__reset_sample_bit(evsel, bit);
141622c8a376SArnaldo Carvalho de Melo }
141722c8a376SArnaldo Carvalho de Melo 
141823d4aad4SArnaldo Carvalho de Melo int perf_evlist__apply_filters(struct perf_evlist *evlist, struct perf_evsel **err_evsel)
14190a102479SFrederic Weisbecker {
14200a102479SFrederic Weisbecker 	struct perf_evsel *evsel;
1421745cefc5SArnaldo Carvalho de Melo 	int err = 0;
1422745cefc5SArnaldo Carvalho de Melo 	const int ncpus = cpu_map__nr(evlist->cpus),
1423b3a319d5SNamhyung Kim 		  nthreads = thread_map__nr(evlist->threads);
14240a102479SFrederic Weisbecker 
1425e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, evsel) {
1426745cefc5SArnaldo Carvalho de Melo 		if (evsel->filter == NULL)
14270a102479SFrederic Weisbecker 			continue;
1428745cefc5SArnaldo Carvalho de Melo 
1429d988d5eeSKan Liang 		/*
1430d988d5eeSKan Liang 		 * filters only work for tracepoint event, which doesn't have cpu limit.
1431d988d5eeSKan Liang 		 * So evlist and evsel should always be same.
1432d988d5eeSKan Liang 		 */
1433f47805a2SArnaldo Carvalho de Melo 		err = perf_evsel__apply_filter(evsel, ncpus, nthreads, evsel->filter);
143423d4aad4SArnaldo Carvalho de Melo 		if (err) {
143523d4aad4SArnaldo Carvalho de Melo 			*err_evsel = evsel;
1436745cefc5SArnaldo Carvalho de Melo 			break;
14370a102479SFrederic Weisbecker 		}
143823d4aad4SArnaldo Carvalho de Melo 	}
14390a102479SFrederic Weisbecker 
1440745cefc5SArnaldo Carvalho de Melo 	return err;
1441745cefc5SArnaldo Carvalho de Melo }
1442745cefc5SArnaldo Carvalho de Melo 
1443745cefc5SArnaldo Carvalho de Melo int perf_evlist__set_filter(struct perf_evlist *evlist, const char *filter)
1444745cefc5SArnaldo Carvalho de Melo {
1445745cefc5SArnaldo Carvalho de Melo 	struct perf_evsel *evsel;
1446745cefc5SArnaldo Carvalho de Melo 	int err = 0;
1447745cefc5SArnaldo Carvalho de Melo 
1448e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, evsel) {
1449fdf14720SWang Nan 		if (evsel->attr.type != PERF_TYPE_TRACEPOINT)
1450fdf14720SWang Nan 			continue;
1451fdf14720SWang Nan 
145294ad89bcSArnaldo Carvalho de Melo 		err = perf_evsel__set_filter(evsel, filter);
1453745cefc5SArnaldo Carvalho de Melo 		if (err)
1454745cefc5SArnaldo Carvalho de Melo 			break;
1455745cefc5SArnaldo Carvalho de Melo 	}
1456745cefc5SArnaldo Carvalho de Melo 
1457745cefc5SArnaldo Carvalho de Melo 	return err;
14580a102479SFrederic Weisbecker }
145974429964SFrederic Weisbecker 
1460be199adaSArnaldo Carvalho de Melo int perf_evlist__set_filter_pids(struct perf_evlist *evlist, size_t npids, pid_t *pids)
1461cfd70a26SArnaldo Carvalho de Melo {
1462cfd70a26SArnaldo Carvalho de Melo 	char *filter;
1463be199adaSArnaldo Carvalho de Melo 	int ret = -1;
1464be199adaSArnaldo Carvalho de Melo 	size_t i;
1465cfd70a26SArnaldo Carvalho de Melo 
1466be199adaSArnaldo Carvalho de Melo 	for (i = 0; i < npids; ++i) {
1467be199adaSArnaldo Carvalho de Melo 		if (i == 0) {
1468be199adaSArnaldo Carvalho de Melo 			if (asprintf(&filter, "common_pid != %d", pids[i]) < 0)
1469cfd70a26SArnaldo Carvalho de Melo 				return -1;
1470be199adaSArnaldo Carvalho de Melo 		} else {
1471be199adaSArnaldo Carvalho de Melo 			char *tmp;
1472be199adaSArnaldo Carvalho de Melo 
1473be199adaSArnaldo Carvalho de Melo 			if (asprintf(&tmp, "%s && common_pid != %d", filter, pids[i]) < 0)
1474be199adaSArnaldo Carvalho de Melo 				goto out_free;
1475be199adaSArnaldo Carvalho de Melo 
1476be199adaSArnaldo Carvalho de Melo 			free(filter);
1477be199adaSArnaldo Carvalho de Melo 			filter = tmp;
1478be199adaSArnaldo Carvalho de Melo 		}
1479be199adaSArnaldo Carvalho de Melo 	}
1480cfd70a26SArnaldo Carvalho de Melo 
1481cfd70a26SArnaldo Carvalho de Melo 	ret = perf_evlist__set_filter(evlist, filter);
1482be199adaSArnaldo Carvalho de Melo out_free:
1483cfd70a26SArnaldo Carvalho de Melo 	free(filter);
1484cfd70a26SArnaldo Carvalho de Melo 	return ret;
1485cfd70a26SArnaldo Carvalho de Melo }
1486cfd70a26SArnaldo Carvalho de Melo 
1487be199adaSArnaldo Carvalho de Melo int perf_evlist__set_filter_pid(struct perf_evlist *evlist, pid_t pid)
1488be199adaSArnaldo Carvalho de Melo {
1489be199adaSArnaldo Carvalho de Melo 	return perf_evlist__set_filter_pids(evlist, 1, &pid);
1490be199adaSArnaldo Carvalho de Melo }
1491be199adaSArnaldo Carvalho de Melo 
14920c21f736SArnaldo Carvalho de Melo bool perf_evlist__valid_sample_type(struct perf_evlist *evlist)
149374429964SFrederic Weisbecker {
149475562573SAdrian Hunter 	struct perf_evsel *pos;
1495c2a70653SArnaldo Carvalho de Melo 
149675562573SAdrian Hunter 	if (evlist->nr_entries == 1)
149775562573SAdrian Hunter 		return true;
149875562573SAdrian Hunter 
149975562573SAdrian Hunter 	if (evlist->id_pos < 0 || evlist->is_pos < 0)
150075562573SAdrian Hunter 		return false;
150175562573SAdrian Hunter 
1502e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, pos) {
150375562573SAdrian Hunter 		if (pos->id_pos != evlist->id_pos ||
150475562573SAdrian Hunter 		    pos->is_pos != evlist->is_pos)
1505c2a70653SArnaldo Carvalho de Melo 			return false;
150674429964SFrederic Weisbecker 	}
150774429964SFrederic Weisbecker 
1508c2a70653SArnaldo Carvalho de Melo 	return true;
1509c2a70653SArnaldo Carvalho de Melo }
1510c2a70653SArnaldo Carvalho de Melo 
151175562573SAdrian Hunter u64 __perf_evlist__combined_sample_type(struct perf_evlist *evlist)
1512c2a70653SArnaldo Carvalho de Melo {
151375562573SAdrian Hunter 	struct perf_evsel *evsel;
151475562573SAdrian Hunter 
151575562573SAdrian Hunter 	if (evlist->combined_sample_type)
151675562573SAdrian Hunter 		return evlist->combined_sample_type;
151775562573SAdrian Hunter 
1518e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, evsel)
151975562573SAdrian Hunter 		evlist->combined_sample_type |= evsel->attr.sample_type;
152075562573SAdrian Hunter 
152175562573SAdrian Hunter 	return evlist->combined_sample_type;
152275562573SAdrian Hunter }
152375562573SAdrian Hunter 
152475562573SAdrian Hunter u64 perf_evlist__combined_sample_type(struct perf_evlist *evlist)
152575562573SAdrian Hunter {
152675562573SAdrian Hunter 	evlist->combined_sample_type = 0;
152775562573SAdrian Hunter 	return __perf_evlist__combined_sample_type(evlist);
1528c2a70653SArnaldo Carvalho de Melo }
1529c2a70653SArnaldo Carvalho de Melo 
153098df858eSAndi Kleen u64 perf_evlist__combined_branch_type(struct perf_evlist *evlist)
153198df858eSAndi Kleen {
153298df858eSAndi Kleen 	struct perf_evsel *evsel;
153398df858eSAndi Kleen 	u64 branch_type = 0;
153498df858eSAndi Kleen 
1535e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, evsel)
153698df858eSAndi Kleen 		branch_type |= evsel->attr.branch_sample_type;
153798df858eSAndi Kleen 	return branch_type;
153898df858eSAndi Kleen }
153998df858eSAndi Kleen 
15409ede473cSJiri Olsa bool perf_evlist__valid_read_format(struct perf_evlist *evlist)
15419ede473cSJiri Olsa {
15429ede473cSJiri Olsa 	struct perf_evsel *first = perf_evlist__first(evlist), *pos = first;
15439ede473cSJiri Olsa 	u64 read_format = first->attr.read_format;
15449ede473cSJiri Olsa 	u64 sample_type = first->attr.sample_type;
15459ede473cSJiri Olsa 
1546e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, pos) {
15479ede473cSJiri Olsa 		if (read_format != pos->attr.read_format)
15489ede473cSJiri Olsa 			return false;
15499ede473cSJiri Olsa 	}
15509ede473cSJiri Olsa 
15519ede473cSJiri Olsa 	/* PERF_SAMPLE_READ imples PERF_FORMAT_ID. */
15529ede473cSJiri Olsa 	if ((sample_type & PERF_SAMPLE_READ) &&
15539ede473cSJiri Olsa 	    !(read_format & PERF_FORMAT_ID)) {
15549ede473cSJiri Olsa 		return false;
15559ede473cSJiri Olsa 	}
15569ede473cSJiri Olsa 
15579ede473cSJiri Olsa 	return true;
15589ede473cSJiri Olsa }
15599ede473cSJiri Olsa 
15609ede473cSJiri Olsa u64 perf_evlist__read_format(struct perf_evlist *evlist)
15619ede473cSJiri Olsa {
15629ede473cSJiri Olsa 	struct perf_evsel *first = perf_evlist__first(evlist);
15639ede473cSJiri Olsa 	return first->attr.read_format;
15649ede473cSJiri Olsa }
15659ede473cSJiri Olsa 
15660c21f736SArnaldo Carvalho de Melo u16 perf_evlist__id_hdr_size(struct perf_evlist *evlist)
156781e36bffSArnaldo Carvalho de Melo {
15680c21f736SArnaldo Carvalho de Melo 	struct perf_evsel *first = perf_evlist__first(evlist);
156981e36bffSArnaldo Carvalho de Melo 	struct perf_sample *data;
157081e36bffSArnaldo Carvalho de Melo 	u64 sample_type;
157181e36bffSArnaldo Carvalho de Melo 	u16 size = 0;
157281e36bffSArnaldo Carvalho de Melo 
157381e36bffSArnaldo Carvalho de Melo 	if (!first->attr.sample_id_all)
157481e36bffSArnaldo Carvalho de Melo 		goto out;
157581e36bffSArnaldo Carvalho de Melo 
157681e36bffSArnaldo Carvalho de Melo 	sample_type = first->attr.sample_type;
157781e36bffSArnaldo Carvalho de Melo 
157881e36bffSArnaldo Carvalho de Melo 	if (sample_type & PERF_SAMPLE_TID)
157981e36bffSArnaldo Carvalho de Melo 		size += sizeof(data->tid) * 2;
158081e36bffSArnaldo Carvalho de Melo 
158181e36bffSArnaldo Carvalho de Melo        if (sample_type & PERF_SAMPLE_TIME)
158281e36bffSArnaldo Carvalho de Melo 		size += sizeof(data->time);
158381e36bffSArnaldo Carvalho de Melo 
158481e36bffSArnaldo Carvalho de Melo 	if (sample_type & PERF_SAMPLE_ID)
158581e36bffSArnaldo Carvalho de Melo 		size += sizeof(data->id);
158681e36bffSArnaldo Carvalho de Melo 
158781e36bffSArnaldo Carvalho de Melo 	if (sample_type & PERF_SAMPLE_STREAM_ID)
158881e36bffSArnaldo Carvalho de Melo 		size += sizeof(data->stream_id);
158981e36bffSArnaldo Carvalho de Melo 
159081e36bffSArnaldo Carvalho de Melo 	if (sample_type & PERF_SAMPLE_CPU)
159181e36bffSArnaldo Carvalho de Melo 		size += sizeof(data->cpu) * 2;
159275562573SAdrian Hunter 
159375562573SAdrian Hunter 	if (sample_type & PERF_SAMPLE_IDENTIFIER)
159475562573SAdrian Hunter 		size += sizeof(data->id);
159581e36bffSArnaldo Carvalho de Melo out:
159681e36bffSArnaldo Carvalho de Melo 	return size;
159781e36bffSArnaldo Carvalho de Melo }
159881e36bffSArnaldo Carvalho de Melo 
15990c21f736SArnaldo Carvalho de Melo bool perf_evlist__valid_sample_id_all(struct perf_evlist *evlist)
1600c2a70653SArnaldo Carvalho de Melo {
16010c21f736SArnaldo Carvalho de Melo 	struct perf_evsel *first = perf_evlist__first(evlist), *pos = first;
1602c2a70653SArnaldo Carvalho de Melo 
1603e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry_continue(evlist, pos) {
1604c2a70653SArnaldo Carvalho de Melo 		if (first->attr.sample_id_all != pos->attr.sample_id_all)
1605c2a70653SArnaldo Carvalho de Melo 			return false;
1606c2a70653SArnaldo Carvalho de Melo 	}
1607c2a70653SArnaldo Carvalho de Melo 
1608c2a70653SArnaldo Carvalho de Melo 	return true;
160974429964SFrederic Weisbecker }
161074429964SFrederic Weisbecker 
16110c21f736SArnaldo Carvalho de Melo bool perf_evlist__sample_id_all(struct perf_evlist *evlist)
161274429964SFrederic Weisbecker {
16130c21f736SArnaldo Carvalho de Melo 	struct perf_evsel *first = perf_evlist__first(evlist);
1614c2a70653SArnaldo Carvalho de Melo 	return first->attr.sample_id_all;
161574429964SFrederic Weisbecker }
161681cce8deSArnaldo Carvalho de Melo 
161781cce8deSArnaldo Carvalho de Melo void perf_evlist__set_selected(struct perf_evlist *evlist,
161881cce8deSArnaldo Carvalho de Melo 			       struct perf_evsel *evsel)
161981cce8deSArnaldo Carvalho de Melo {
162081cce8deSArnaldo Carvalho de Melo 	evlist->selected = evsel;
162181cce8deSArnaldo Carvalho de Melo }
1622727ab04eSArnaldo Carvalho de Melo 
1623a74b4b66SNamhyung Kim void perf_evlist__close(struct perf_evlist *evlist)
1624a74b4b66SNamhyung Kim {
1625a74b4b66SNamhyung Kim 	struct perf_evsel *evsel;
1626a74b4b66SNamhyung Kim 	int ncpus = cpu_map__nr(evlist->cpus);
1627a74b4b66SNamhyung Kim 	int nthreads = thread_map__nr(evlist->threads);
1628a74b4b66SNamhyung Kim 
1629e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry_reverse(evlist, evsel) {
163018ef15c6SArnaldo Carvalho de Melo 		int n = evsel->cpus ? evsel->cpus->nr : ncpus;
16318ad9219eSStephane Eranian 		perf_evsel__close(evsel, n, nthreads);
16328ad9219eSStephane Eranian 	}
1633a74b4b66SNamhyung Kim }
1634a74b4b66SNamhyung Kim 
16354112eb18SArnaldo Carvalho de Melo static int perf_evlist__create_syswide_maps(struct perf_evlist *evlist)
16364112eb18SArnaldo Carvalho de Melo {
16378c0498b6SAdrian Hunter 	struct cpu_map	  *cpus;
16388c0498b6SAdrian Hunter 	struct thread_map *threads;
16394112eb18SArnaldo Carvalho de Melo 	int err = -ENOMEM;
16404112eb18SArnaldo Carvalho de Melo 
16414112eb18SArnaldo Carvalho de Melo 	/*
16424112eb18SArnaldo Carvalho de Melo 	 * Try reading /sys/devices/system/cpu/online to get
16434112eb18SArnaldo Carvalho de Melo 	 * an all cpus map.
16444112eb18SArnaldo Carvalho de Melo 	 *
16454112eb18SArnaldo Carvalho de Melo 	 * FIXME: -ENOMEM is the best we can do here, the cpu_map
16464112eb18SArnaldo Carvalho de Melo 	 * code needs an overhaul to properly forward the
16474112eb18SArnaldo Carvalho de Melo 	 * error, and we may not want to do that fallback to a
16484112eb18SArnaldo Carvalho de Melo 	 * default cpu identity map :-\
16494112eb18SArnaldo Carvalho de Melo 	 */
16508c0498b6SAdrian Hunter 	cpus = cpu_map__new(NULL);
16518c0498b6SAdrian Hunter 	if (!cpus)
16524112eb18SArnaldo Carvalho de Melo 		goto out;
16534112eb18SArnaldo Carvalho de Melo 
16548c0498b6SAdrian Hunter 	threads = thread_map__new_dummy();
16558c0498b6SAdrian Hunter 	if (!threads)
16568c0498b6SAdrian Hunter 		goto out_put;
16574112eb18SArnaldo Carvalho de Melo 
16588c0498b6SAdrian Hunter 	perf_evlist__set_maps(evlist, cpus, threads);
16594112eb18SArnaldo Carvalho de Melo out:
16604112eb18SArnaldo Carvalho de Melo 	return err;
16618c0498b6SAdrian Hunter out_put:
16628c0498b6SAdrian Hunter 	cpu_map__put(cpus);
16634112eb18SArnaldo Carvalho de Melo 	goto out;
16644112eb18SArnaldo Carvalho de Melo }
16654112eb18SArnaldo Carvalho de Melo 
16666a4bb04cSJiri Olsa int perf_evlist__open(struct perf_evlist *evlist)
1667727ab04eSArnaldo Carvalho de Melo {
16686a4bb04cSJiri Olsa 	struct perf_evsel *evsel;
1669a74b4b66SNamhyung Kim 	int err;
1670727ab04eSArnaldo Carvalho de Melo 
16714112eb18SArnaldo Carvalho de Melo 	/*
16724112eb18SArnaldo Carvalho de Melo 	 * Default: one fd per CPU, all threads, aka systemwide
16734112eb18SArnaldo Carvalho de Melo 	 * as sys_perf_event_open(cpu = -1, thread = -1) is EINVAL
16744112eb18SArnaldo Carvalho de Melo 	 */
16754112eb18SArnaldo Carvalho de Melo 	if (evlist->threads == NULL && evlist->cpus == NULL) {
16764112eb18SArnaldo Carvalho de Melo 		err = perf_evlist__create_syswide_maps(evlist);
16774112eb18SArnaldo Carvalho de Melo 		if (err < 0)
16784112eb18SArnaldo Carvalho de Melo 			goto out_err;
16794112eb18SArnaldo Carvalho de Melo 	}
16804112eb18SArnaldo Carvalho de Melo 
1681733cd2feSAdrian Hunter 	perf_evlist__update_id_pos(evlist);
1682733cd2feSAdrian Hunter 
1683e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, evsel) {
168423df7f79SAdrian Hunter 		err = perf_evsel__open(evsel, evsel->cpus, evsel->threads);
1685727ab04eSArnaldo Carvalho de Melo 		if (err < 0)
1686727ab04eSArnaldo Carvalho de Melo 			goto out_err;
1687727ab04eSArnaldo Carvalho de Melo 	}
1688727ab04eSArnaldo Carvalho de Melo 
1689727ab04eSArnaldo Carvalho de Melo 	return 0;
1690727ab04eSArnaldo Carvalho de Melo out_err:
1691a74b4b66SNamhyung Kim 	perf_evlist__close(evlist);
169241c21a68SNamhyung Kim 	errno = -err;
1693727ab04eSArnaldo Carvalho de Melo 	return err;
1694727ab04eSArnaldo Carvalho de Melo }
169535b9d88eSArnaldo Carvalho de Melo 
1696602ad878SArnaldo Carvalho de Melo int perf_evlist__prepare_workload(struct perf_evlist *evlist, struct target *target,
169755e162eaSNamhyung Kim 				  const char *argv[], bool pipe_output,
1698735f7e0bSArnaldo Carvalho de Melo 				  void (*exec_error)(int signo, siginfo_t *info, void *ucontext))
169935b9d88eSArnaldo Carvalho de Melo {
170035b9d88eSArnaldo Carvalho de Melo 	int child_ready_pipe[2], go_pipe[2];
170135b9d88eSArnaldo Carvalho de Melo 	char bf;
170235b9d88eSArnaldo Carvalho de Melo 
170335b9d88eSArnaldo Carvalho de Melo 	if (pipe(child_ready_pipe) < 0) {
170435b9d88eSArnaldo Carvalho de Melo 		perror("failed to create 'ready' pipe");
170535b9d88eSArnaldo Carvalho de Melo 		return -1;
170635b9d88eSArnaldo Carvalho de Melo 	}
170735b9d88eSArnaldo Carvalho de Melo 
170835b9d88eSArnaldo Carvalho de Melo 	if (pipe(go_pipe) < 0) {
170935b9d88eSArnaldo Carvalho de Melo 		perror("failed to create 'go' pipe");
171035b9d88eSArnaldo Carvalho de Melo 		goto out_close_ready_pipe;
171135b9d88eSArnaldo Carvalho de Melo 	}
171235b9d88eSArnaldo Carvalho de Melo 
171335b9d88eSArnaldo Carvalho de Melo 	evlist->workload.pid = fork();
171435b9d88eSArnaldo Carvalho de Melo 	if (evlist->workload.pid < 0) {
171535b9d88eSArnaldo Carvalho de Melo 		perror("failed to fork");
171635b9d88eSArnaldo Carvalho de Melo 		goto out_close_pipes;
171735b9d88eSArnaldo Carvalho de Melo 	}
171835b9d88eSArnaldo Carvalho de Melo 
171935b9d88eSArnaldo Carvalho de Melo 	if (!evlist->workload.pid) {
17205f1c4225SArnaldo Carvalho de Melo 		int ret;
17215f1c4225SArnaldo Carvalho de Melo 
1722119fa3c9SNamhyung Kim 		if (pipe_output)
172335b9d88eSArnaldo Carvalho de Melo 			dup2(2, 1);
172435b9d88eSArnaldo Carvalho de Melo 
17250817df08SDavid Ahern 		signal(SIGTERM, SIG_DFL);
17260817df08SDavid Ahern 
172735b9d88eSArnaldo Carvalho de Melo 		close(child_ready_pipe[0]);
172835b9d88eSArnaldo Carvalho de Melo 		close(go_pipe[1]);
172935b9d88eSArnaldo Carvalho de Melo 		fcntl(go_pipe[0], F_SETFD, FD_CLOEXEC);
173035b9d88eSArnaldo Carvalho de Melo 
173135b9d88eSArnaldo Carvalho de Melo 		/*
173235b9d88eSArnaldo Carvalho de Melo 		 * Tell the parent we're ready to go
173335b9d88eSArnaldo Carvalho de Melo 		 */
173435b9d88eSArnaldo Carvalho de Melo 		close(child_ready_pipe[1]);
173535b9d88eSArnaldo Carvalho de Melo 
173635b9d88eSArnaldo Carvalho de Melo 		/*
173735b9d88eSArnaldo Carvalho de Melo 		 * Wait until the parent tells us to go.
173835b9d88eSArnaldo Carvalho de Melo 		 */
17395f1c4225SArnaldo Carvalho de Melo 		ret = read(go_pipe[0], &bf, 1);
17405f1c4225SArnaldo Carvalho de Melo 		/*
17415f1c4225SArnaldo Carvalho de Melo 		 * The parent will ask for the execvp() to be performed by
17425f1c4225SArnaldo Carvalho de Melo 		 * writing exactly one byte, in workload.cork_fd, usually via
17435f1c4225SArnaldo Carvalho de Melo 		 * perf_evlist__start_workload().
17445f1c4225SArnaldo Carvalho de Melo 		 *
174520f86fc1SArnaldo Carvalho de Melo 		 * For cancelling the workload without actually running it,
17465f1c4225SArnaldo Carvalho de Melo 		 * the parent will just close workload.cork_fd, without writing
17475f1c4225SArnaldo Carvalho de Melo 		 * anything, i.e. read will return zero and we just exit()
17485f1c4225SArnaldo Carvalho de Melo 		 * here.
17495f1c4225SArnaldo Carvalho de Melo 		 */
17505f1c4225SArnaldo Carvalho de Melo 		if (ret != 1) {
17515f1c4225SArnaldo Carvalho de Melo 			if (ret == -1)
175235b9d88eSArnaldo Carvalho de Melo 				perror("unable to read pipe");
17535f1c4225SArnaldo Carvalho de Melo 			exit(ret);
17545f1c4225SArnaldo Carvalho de Melo 		}
175535b9d88eSArnaldo Carvalho de Melo 
175635b9d88eSArnaldo Carvalho de Melo 		execvp(argv[0], (char **)argv);
175735b9d88eSArnaldo Carvalho de Melo 
1758735f7e0bSArnaldo Carvalho de Melo 		if (exec_error) {
1759f33cbe72SArnaldo Carvalho de Melo 			union sigval val;
1760f33cbe72SArnaldo Carvalho de Melo 
1761f33cbe72SArnaldo Carvalho de Melo 			val.sival_int = errno;
1762f33cbe72SArnaldo Carvalho de Melo 			if (sigqueue(getppid(), SIGUSR1, val))
176335b9d88eSArnaldo Carvalho de Melo 				perror(argv[0]);
1764f33cbe72SArnaldo Carvalho de Melo 		} else
1765f33cbe72SArnaldo Carvalho de Melo 			perror(argv[0]);
176635b9d88eSArnaldo Carvalho de Melo 		exit(-1);
176735b9d88eSArnaldo Carvalho de Melo 	}
176835b9d88eSArnaldo Carvalho de Melo 
1769735f7e0bSArnaldo Carvalho de Melo 	if (exec_error) {
1770735f7e0bSArnaldo Carvalho de Melo 		struct sigaction act = {
1771735f7e0bSArnaldo Carvalho de Melo 			.sa_flags     = SA_SIGINFO,
1772735f7e0bSArnaldo Carvalho de Melo 			.sa_sigaction = exec_error,
1773735f7e0bSArnaldo Carvalho de Melo 		};
1774735f7e0bSArnaldo Carvalho de Melo 		sigaction(SIGUSR1, &act, NULL);
1775735f7e0bSArnaldo Carvalho de Melo 	}
1776735f7e0bSArnaldo Carvalho de Melo 
17771aaf63b1SArnaldo Carvalho de Melo 	if (target__none(target)) {
17781aaf63b1SArnaldo Carvalho de Melo 		if (evlist->threads == NULL) {
17791aaf63b1SArnaldo Carvalho de Melo 			fprintf(stderr, "FATAL: evlist->threads need to be set at this point (%s:%d).\n",
17801aaf63b1SArnaldo Carvalho de Melo 				__func__, __LINE__);
17811aaf63b1SArnaldo Carvalho de Melo 			goto out_close_pipes;
17821aaf63b1SArnaldo Carvalho de Melo 		}
1783e13798c7SJiri Olsa 		thread_map__set_pid(evlist->threads, 0, evlist->workload.pid);
17841aaf63b1SArnaldo Carvalho de Melo 	}
178535b9d88eSArnaldo Carvalho de Melo 
178635b9d88eSArnaldo Carvalho de Melo 	close(child_ready_pipe[1]);
178735b9d88eSArnaldo Carvalho de Melo 	close(go_pipe[0]);
178835b9d88eSArnaldo Carvalho de Melo 	/*
178935b9d88eSArnaldo Carvalho de Melo 	 * wait for child to settle
179035b9d88eSArnaldo Carvalho de Melo 	 */
179135b9d88eSArnaldo Carvalho de Melo 	if (read(child_ready_pipe[0], &bf, 1) == -1) {
179235b9d88eSArnaldo Carvalho de Melo 		perror("unable to read pipe");
179335b9d88eSArnaldo Carvalho de Melo 		goto out_close_pipes;
179435b9d88eSArnaldo Carvalho de Melo 	}
179535b9d88eSArnaldo Carvalho de Melo 
1796bcf3145fSNamhyung Kim 	fcntl(go_pipe[1], F_SETFD, FD_CLOEXEC);
179735b9d88eSArnaldo Carvalho de Melo 	evlist->workload.cork_fd = go_pipe[1];
179835b9d88eSArnaldo Carvalho de Melo 	close(child_ready_pipe[0]);
179935b9d88eSArnaldo Carvalho de Melo 	return 0;
180035b9d88eSArnaldo Carvalho de Melo 
180135b9d88eSArnaldo Carvalho de Melo out_close_pipes:
180235b9d88eSArnaldo Carvalho de Melo 	close(go_pipe[0]);
180335b9d88eSArnaldo Carvalho de Melo 	close(go_pipe[1]);
180435b9d88eSArnaldo Carvalho de Melo out_close_ready_pipe:
180535b9d88eSArnaldo Carvalho de Melo 	close(child_ready_pipe[0]);
180635b9d88eSArnaldo Carvalho de Melo 	close(child_ready_pipe[1]);
180735b9d88eSArnaldo Carvalho de Melo 	return -1;
180835b9d88eSArnaldo Carvalho de Melo }
180935b9d88eSArnaldo Carvalho de Melo 
181035b9d88eSArnaldo Carvalho de Melo int perf_evlist__start_workload(struct perf_evlist *evlist)
181135b9d88eSArnaldo Carvalho de Melo {
181235b9d88eSArnaldo Carvalho de Melo 	if (evlist->workload.cork_fd > 0) {
1813b3824404SDavid Ahern 		char bf = 0;
1814bcf3145fSNamhyung Kim 		int ret;
181535b9d88eSArnaldo Carvalho de Melo 		/*
181635b9d88eSArnaldo Carvalho de Melo 		 * Remove the cork, let it rip!
181735b9d88eSArnaldo Carvalho de Melo 		 */
1818bcf3145fSNamhyung Kim 		ret = write(evlist->workload.cork_fd, &bf, 1);
1819bcf3145fSNamhyung Kim 		if (ret < 0)
1820e978be9eSSoramichi Akiyama 			perror("unable to write to pipe");
1821bcf3145fSNamhyung Kim 
1822bcf3145fSNamhyung Kim 		close(evlist->workload.cork_fd);
1823bcf3145fSNamhyung Kim 		return ret;
182435b9d88eSArnaldo Carvalho de Melo 	}
182535b9d88eSArnaldo Carvalho de Melo 
182635b9d88eSArnaldo Carvalho de Melo 	return 0;
182735b9d88eSArnaldo Carvalho de Melo }
1828cb0b29e0SArnaldo Carvalho de Melo 
1829a3f698feSArnaldo Carvalho de Melo int perf_evlist__parse_sample(struct perf_evlist *evlist, union perf_event *event,
18300807d2d8SArnaldo Carvalho de Melo 			      struct perf_sample *sample)
1831cb0b29e0SArnaldo Carvalho de Melo {
183275562573SAdrian Hunter 	struct perf_evsel *evsel = perf_evlist__event2evsel(evlist, event);
183375562573SAdrian Hunter 
183475562573SAdrian Hunter 	if (!evsel)
183575562573SAdrian Hunter 		return -EFAULT;
18360807d2d8SArnaldo Carvalho de Melo 	return perf_evsel__parse_sample(evsel, event, sample);
1837cb0b29e0SArnaldo Carvalho de Melo }
183878f067b3SArnaldo Carvalho de Melo 
183978f067b3SArnaldo Carvalho de Melo size_t perf_evlist__fprintf(struct perf_evlist *evlist, FILE *fp)
184078f067b3SArnaldo Carvalho de Melo {
184178f067b3SArnaldo Carvalho de Melo 	struct perf_evsel *evsel;
184278f067b3SArnaldo Carvalho de Melo 	size_t printed = 0;
184378f067b3SArnaldo Carvalho de Melo 
1844e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, evsel) {
184578f067b3SArnaldo Carvalho de Melo 		printed += fprintf(fp, "%s%s", evsel->idx ? ", " : "",
184678f067b3SArnaldo Carvalho de Melo 				   perf_evsel__name(evsel));
184778f067b3SArnaldo Carvalho de Melo 	}
184878f067b3SArnaldo Carvalho de Melo 
1849b2222139SDavidlohr Bueso 	return printed + fprintf(fp, "\n");
185078f067b3SArnaldo Carvalho de Melo }
18516ef068cbSArnaldo Carvalho de Melo 
1852d9aade7fSArnaldo Carvalho de Melo int perf_evlist__strerror_open(struct perf_evlist *evlist,
1853a8f23d8fSArnaldo Carvalho de Melo 			       int err, char *buf, size_t size)
1854a8f23d8fSArnaldo Carvalho de Melo {
1855a8f23d8fSArnaldo Carvalho de Melo 	int printed, value;
1856c8b5f2c9SArnaldo Carvalho de Melo 	char sbuf[STRERR_BUFSIZE], *emsg = str_error_r(err, sbuf, sizeof(sbuf));
1857a8f23d8fSArnaldo Carvalho de Melo 
1858a8f23d8fSArnaldo Carvalho de Melo 	switch (err) {
1859a8f23d8fSArnaldo Carvalho de Melo 	case EACCES:
1860a8f23d8fSArnaldo Carvalho de Melo 	case EPERM:
1861a8f23d8fSArnaldo Carvalho de Melo 		printed = scnprintf(buf, size,
1862a8f23d8fSArnaldo Carvalho de Melo 				    "Error:\t%s.\n"
1863a8f23d8fSArnaldo Carvalho de Melo 				    "Hint:\tCheck /proc/sys/kernel/perf_event_paranoid setting.", emsg);
1864a8f23d8fSArnaldo Carvalho de Melo 
18651a47245dSAdrian Hunter 		value = perf_event_paranoid();
1866a8f23d8fSArnaldo Carvalho de Melo 
1867a8f23d8fSArnaldo Carvalho de Melo 		printed += scnprintf(buf + printed, size - printed, "\nHint:\t");
1868a8f23d8fSArnaldo Carvalho de Melo 
1869a8f23d8fSArnaldo Carvalho de Melo 		if (value >= 2) {
1870a8f23d8fSArnaldo Carvalho de Melo 			printed += scnprintf(buf + printed, size - printed,
1871a8f23d8fSArnaldo Carvalho de Melo 					     "For your workloads it needs to be <= 1\nHint:\t");
1872a8f23d8fSArnaldo Carvalho de Melo 		}
1873a8f23d8fSArnaldo Carvalho de Melo 		printed += scnprintf(buf + printed, size - printed,
18745229e366SArnaldo Carvalho de Melo 				     "For system wide tracing it needs to be set to -1.\n");
1875a8f23d8fSArnaldo Carvalho de Melo 
1876a8f23d8fSArnaldo Carvalho de Melo 		printed += scnprintf(buf + printed, size - printed,
18775229e366SArnaldo Carvalho de Melo 				    "Hint:\tTry: 'sudo sh -c \"echo -1 > /proc/sys/kernel/perf_event_paranoid\"'\n"
18785229e366SArnaldo Carvalho de Melo 				    "Hint:\tThe current value is %d.", value);
1879a8f23d8fSArnaldo Carvalho de Melo 		break;
1880d9aade7fSArnaldo Carvalho de Melo 	case EINVAL: {
1881d9aade7fSArnaldo Carvalho de Melo 		struct perf_evsel *first = perf_evlist__first(evlist);
1882d9aade7fSArnaldo Carvalho de Melo 		int max_freq;
1883d9aade7fSArnaldo Carvalho de Melo 
1884d9aade7fSArnaldo Carvalho de Melo 		if (sysctl__read_int("kernel/perf_event_max_sample_rate", &max_freq) < 0)
1885d9aade7fSArnaldo Carvalho de Melo 			goto out_default;
1886d9aade7fSArnaldo Carvalho de Melo 
1887d9aade7fSArnaldo Carvalho de Melo 		if (first->attr.sample_freq < (u64)max_freq)
1888d9aade7fSArnaldo Carvalho de Melo 			goto out_default;
1889d9aade7fSArnaldo Carvalho de Melo 
1890d9aade7fSArnaldo Carvalho de Melo 		printed = scnprintf(buf, size,
1891d9aade7fSArnaldo Carvalho de Melo 				    "Error:\t%s.\n"
1892d9aade7fSArnaldo Carvalho de Melo 				    "Hint:\tCheck /proc/sys/kernel/perf_event_max_sample_rate.\n"
1893d9aade7fSArnaldo Carvalho de Melo 				    "Hint:\tThe current value is %d and %" PRIu64 " is being requested.",
1894d9aade7fSArnaldo Carvalho de Melo 				    emsg, max_freq, first->attr.sample_freq);
1895d9aade7fSArnaldo Carvalho de Melo 		break;
1896d9aade7fSArnaldo Carvalho de Melo 	}
1897a8f23d8fSArnaldo Carvalho de Melo 	default:
1898d9aade7fSArnaldo Carvalho de Melo out_default:
1899a8f23d8fSArnaldo Carvalho de Melo 		scnprintf(buf, size, "%s", emsg);
1900a8f23d8fSArnaldo Carvalho de Melo 		break;
1901a8f23d8fSArnaldo Carvalho de Melo 	}
1902a8f23d8fSArnaldo Carvalho de Melo 
1903a8f23d8fSArnaldo Carvalho de Melo 	return 0;
1904a8f23d8fSArnaldo Carvalho de Melo }
1905a025e4f0SAdrian Hunter 
1906956fa571SArnaldo Carvalho de Melo int perf_evlist__strerror_mmap(struct perf_evlist *evlist, int err, char *buf, size_t size)
1907956fa571SArnaldo Carvalho de Melo {
1908c8b5f2c9SArnaldo Carvalho de Melo 	char sbuf[STRERR_BUFSIZE], *emsg = str_error_r(err, sbuf, sizeof(sbuf));
1909e965bea1SArnaldo Carvalho de Melo 	int pages_attempted = evlist->mmap_len / 1024, pages_max_per_user, printed = 0;
1910956fa571SArnaldo Carvalho de Melo 
1911956fa571SArnaldo Carvalho de Melo 	switch (err) {
1912956fa571SArnaldo Carvalho de Melo 	case EPERM:
1913e5d4a290SArnaldo Carvalho de Melo 		sysctl__read_int("kernel/perf_event_mlock_kb", &pages_max_per_user);
1914e965bea1SArnaldo Carvalho de Melo 		printed += scnprintf(buf + printed, size - printed,
1915e965bea1SArnaldo Carvalho de Melo 				     "Error:\t%s.\n"
1916956fa571SArnaldo Carvalho de Melo 				     "Hint:\tCheck /proc/sys/kernel/perf_event_mlock_kb (%d kB) setting.\n"
1917e965bea1SArnaldo Carvalho de Melo 				     "Hint:\tTried using %zd kB.\n",
1918e5d4a290SArnaldo Carvalho de Melo 				     emsg, pages_max_per_user, pages_attempted);
1919e965bea1SArnaldo Carvalho de Melo 
1920e965bea1SArnaldo Carvalho de Melo 		if (pages_attempted >= pages_max_per_user) {
1921e965bea1SArnaldo Carvalho de Melo 			printed += scnprintf(buf + printed, size - printed,
1922e965bea1SArnaldo Carvalho de Melo 					     "Hint:\tTry 'sudo sh -c \"echo %d > /proc/sys/kernel/perf_event_mlock_kb\"', or\n",
1923e965bea1SArnaldo Carvalho de Melo 					     pages_max_per_user + pages_attempted);
1924e965bea1SArnaldo Carvalho de Melo 		}
1925e965bea1SArnaldo Carvalho de Melo 
1926e965bea1SArnaldo Carvalho de Melo 		printed += scnprintf(buf + printed, size - printed,
1927e965bea1SArnaldo Carvalho de Melo 				     "Hint:\tTry using a smaller -m/--mmap-pages value.");
1928956fa571SArnaldo Carvalho de Melo 		break;
1929956fa571SArnaldo Carvalho de Melo 	default:
1930956fa571SArnaldo Carvalho de Melo 		scnprintf(buf, size, "%s", emsg);
1931956fa571SArnaldo Carvalho de Melo 		break;
1932956fa571SArnaldo Carvalho de Melo 	}
1933956fa571SArnaldo Carvalho de Melo 
1934956fa571SArnaldo Carvalho de Melo 	return 0;
1935956fa571SArnaldo Carvalho de Melo }
1936956fa571SArnaldo Carvalho de Melo 
1937a025e4f0SAdrian Hunter void perf_evlist__to_front(struct perf_evlist *evlist,
1938a025e4f0SAdrian Hunter 			   struct perf_evsel *move_evsel)
1939a025e4f0SAdrian Hunter {
1940a025e4f0SAdrian Hunter 	struct perf_evsel *evsel, *n;
1941a025e4f0SAdrian Hunter 	LIST_HEAD(move);
1942a025e4f0SAdrian Hunter 
1943a025e4f0SAdrian Hunter 	if (move_evsel == perf_evlist__first(evlist))
1944a025e4f0SAdrian Hunter 		return;
1945a025e4f0SAdrian Hunter 
1946e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry_safe(evlist, n, evsel) {
1947a025e4f0SAdrian Hunter 		if (evsel->leader == move_evsel->leader)
1948a025e4f0SAdrian Hunter 			list_move_tail(&evsel->node, &move);
1949a025e4f0SAdrian Hunter 	}
1950a025e4f0SAdrian Hunter 
1951a025e4f0SAdrian Hunter 	list_splice(&move, &evlist->entries);
1952a025e4f0SAdrian Hunter }
195360b0896cSAdrian Hunter 
195460b0896cSAdrian Hunter void perf_evlist__set_tracking_event(struct perf_evlist *evlist,
195560b0896cSAdrian Hunter 				     struct perf_evsel *tracking_evsel)
195660b0896cSAdrian Hunter {
195760b0896cSAdrian Hunter 	struct perf_evsel *evsel;
195860b0896cSAdrian Hunter 
195960b0896cSAdrian Hunter 	if (tracking_evsel->tracking)
196060b0896cSAdrian Hunter 		return;
196160b0896cSAdrian Hunter 
1962e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, evsel) {
196360b0896cSAdrian Hunter 		if (evsel != tracking_evsel)
196460b0896cSAdrian Hunter 			evsel->tracking = false;
196560b0896cSAdrian Hunter 	}
196660b0896cSAdrian Hunter 
196760b0896cSAdrian Hunter 	tracking_evsel->tracking = true;
196860b0896cSAdrian Hunter }
19697630b3e2SWang Nan 
19707630b3e2SWang Nan struct perf_evsel *
19717630b3e2SWang Nan perf_evlist__find_evsel_by_str(struct perf_evlist *evlist,
19727630b3e2SWang Nan 			       const char *str)
19737630b3e2SWang Nan {
19747630b3e2SWang Nan 	struct perf_evsel *evsel;
19757630b3e2SWang Nan 
1976e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, evsel) {
19777630b3e2SWang Nan 		if (!evsel->name)
19787630b3e2SWang Nan 			continue;
19797630b3e2SWang Nan 		if (strcmp(str, evsel->name) == 0)
19807630b3e2SWang Nan 			return evsel;
19817630b3e2SWang Nan 	}
19827630b3e2SWang Nan 
19837630b3e2SWang Nan 	return NULL;
19847630b3e2SWang Nan }
198554cc54deSWang Nan 
198654cc54deSWang Nan void perf_evlist__toggle_bkw_mmap(struct perf_evlist *evlist,
198754cc54deSWang Nan 				  enum bkw_mmap_state state)
198854cc54deSWang Nan {
198954cc54deSWang Nan 	enum bkw_mmap_state old_state = evlist->bkw_mmap_state;
199054cc54deSWang Nan 	enum action {
199154cc54deSWang Nan 		NONE,
199254cc54deSWang Nan 		PAUSE,
199354cc54deSWang Nan 		RESUME,
199454cc54deSWang Nan 	} action = NONE;
199554cc54deSWang Nan 
199654cc54deSWang Nan 	if (!evlist->backward_mmap)
199754cc54deSWang Nan 		return;
199854cc54deSWang Nan 
199954cc54deSWang Nan 	switch (old_state) {
200054cc54deSWang Nan 	case BKW_MMAP_NOTREADY: {
200154cc54deSWang Nan 		if (state != BKW_MMAP_RUNNING)
200254cc54deSWang Nan 			goto state_err;;
200354cc54deSWang Nan 		break;
200454cc54deSWang Nan 	}
200554cc54deSWang Nan 	case BKW_MMAP_RUNNING: {
200654cc54deSWang Nan 		if (state != BKW_MMAP_DATA_PENDING)
200754cc54deSWang Nan 			goto state_err;
200854cc54deSWang Nan 		action = PAUSE;
200954cc54deSWang Nan 		break;
201054cc54deSWang Nan 	}
201154cc54deSWang Nan 	case BKW_MMAP_DATA_PENDING: {
201254cc54deSWang Nan 		if (state != BKW_MMAP_EMPTY)
201354cc54deSWang Nan 			goto state_err;
201454cc54deSWang Nan 		break;
201554cc54deSWang Nan 	}
201654cc54deSWang Nan 	case BKW_MMAP_EMPTY: {
201754cc54deSWang Nan 		if (state != BKW_MMAP_RUNNING)
201854cc54deSWang Nan 			goto state_err;
201954cc54deSWang Nan 		action = RESUME;
202054cc54deSWang Nan 		break;
202154cc54deSWang Nan 	}
202254cc54deSWang Nan 	default:
202354cc54deSWang Nan 		WARN_ONCE(1, "Shouldn't get there\n");
202454cc54deSWang Nan 	}
202554cc54deSWang Nan 
202654cc54deSWang Nan 	evlist->bkw_mmap_state = state;
202754cc54deSWang Nan 
202854cc54deSWang Nan 	switch (action) {
202954cc54deSWang Nan 	case PAUSE:
203054cc54deSWang Nan 		perf_evlist__pause(evlist);
203154cc54deSWang Nan 		break;
203254cc54deSWang Nan 	case RESUME:
203354cc54deSWang Nan 		perf_evlist__resume(evlist);
203454cc54deSWang Nan 		break;
203554cc54deSWang Nan 	case NONE:
203654cc54deSWang Nan 	default:
203754cc54deSWang Nan 		break;
203854cc54deSWang Nan 	}
203954cc54deSWang Nan 
204054cc54deSWang Nan state_err:
204154cc54deSWang Nan 	return;
204254cc54deSWang Nan }
2043