xref: /openbmc/linux/tools/perf/util/evlist.c (revision b72e74d1)
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>
115c581041SArnaldo Carvalho de Melo #include <poll.h>
12f8a95309SArnaldo Carvalho de Melo #include "cpumap.h"
13f8a95309SArnaldo Carvalho de Melo #include "thread_map.h"
1412864b31SNamhyung Kim #include "target.h"
15361c99a6SArnaldo Carvalho de Melo #include "evlist.h"
16361c99a6SArnaldo Carvalho de Melo #include "evsel.h"
17e3e1a54fSAdrian Hunter #include "debug.h"
1835b9d88eSArnaldo Carvalho de Melo #include <unistd.h>
19361c99a6SArnaldo Carvalho de Melo 
2050d08e47SArnaldo Carvalho de Melo #include "parse-events.h"
21994a1f78SJiri Olsa #include "parse-options.h"
2250d08e47SArnaldo Carvalho de Melo 
23f8a95309SArnaldo Carvalho de Melo #include <sys/mman.h>
24f8a95309SArnaldo Carvalho de Melo 
2570db7533SArnaldo Carvalho de Melo #include <linux/bitops.h>
2670db7533SArnaldo Carvalho de Melo #include <linux/hash.h>
270389cd1fSArnaldo Carvalho de Melo #include <linux/log2.h>
2870db7533SArnaldo Carvalho de Melo 
29e4b356b5SArnaldo Carvalho de Melo static void perf_evlist__mmap_put(struct perf_evlist *evlist, int idx);
30e4b356b5SArnaldo Carvalho de Melo static void __perf_evlist__munmap(struct perf_evlist *evlist, int idx);
31e4b356b5SArnaldo Carvalho de Melo 
32f8a95309SArnaldo Carvalho de Melo #define FD(e, x, y) (*(int *)xyarray__entry(e->fd, x, y))
33a91e5431SArnaldo Carvalho de Melo #define SID(e, x, y) xyarray__entry(e->sample_id, x, y)
34f8a95309SArnaldo Carvalho de Melo 
357e2ed097SArnaldo Carvalho de Melo void perf_evlist__init(struct perf_evlist *evlist, struct cpu_map *cpus,
367e2ed097SArnaldo Carvalho de Melo 		       struct thread_map *threads)
37361c99a6SArnaldo Carvalho de Melo {
3870db7533SArnaldo Carvalho de Melo 	int i;
3970db7533SArnaldo Carvalho de Melo 
4070db7533SArnaldo Carvalho de Melo 	for (i = 0; i < PERF_EVLIST__HLIST_SIZE; ++i)
4170db7533SArnaldo Carvalho de Melo 		INIT_HLIST_HEAD(&evlist->heads[i]);
42361c99a6SArnaldo Carvalho de Melo 	INIT_LIST_HEAD(&evlist->entries);
437e2ed097SArnaldo Carvalho de Melo 	perf_evlist__set_maps(evlist, cpus, threads);
441b85337dSArnaldo Carvalho de Melo 	fdarray__init(&evlist->pollfd, 64);
4535b9d88eSArnaldo Carvalho de Melo 	evlist->workload.pid = -1;
46361c99a6SArnaldo Carvalho de Melo }
47361c99a6SArnaldo Carvalho de Melo 
48334fe7a3SNamhyung Kim struct perf_evlist *perf_evlist__new(void)
49ef1d1af2SArnaldo Carvalho de Melo {
50ef1d1af2SArnaldo Carvalho de Melo 	struct perf_evlist *evlist = zalloc(sizeof(*evlist));
51ef1d1af2SArnaldo Carvalho de Melo 
52ef1d1af2SArnaldo Carvalho de Melo 	if (evlist != NULL)
53334fe7a3SNamhyung Kim 		perf_evlist__init(evlist, NULL, NULL);
54ef1d1af2SArnaldo Carvalho de Melo 
55361c99a6SArnaldo Carvalho de Melo 	return evlist;
56361c99a6SArnaldo Carvalho de Melo }
57361c99a6SArnaldo Carvalho de Melo 
58b22d54b0SJiri Olsa struct perf_evlist *perf_evlist__new_default(void)
59b22d54b0SJiri Olsa {
60b22d54b0SJiri Olsa 	struct perf_evlist *evlist = perf_evlist__new();
61b22d54b0SJiri Olsa 
62b22d54b0SJiri Olsa 	if (evlist && perf_evlist__add_default(evlist)) {
63b22d54b0SJiri Olsa 		perf_evlist__delete(evlist);
64b22d54b0SJiri Olsa 		evlist = NULL;
65b22d54b0SJiri Olsa 	}
66b22d54b0SJiri Olsa 
67b22d54b0SJiri Olsa 	return evlist;
68b22d54b0SJiri Olsa }
69b22d54b0SJiri Olsa 
7075562573SAdrian Hunter /**
7175562573SAdrian Hunter  * perf_evlist__set_id_pos - set the positions of event ids.
7275562573SAdrian Hunter  * @evlist: selected event list
7375562573SAdrian Hunter  *
7475562573SAdrian Hunter  * Events with compatible sample types all have the same id_pos
7575562573SAdrian Hunter  * and is_pos.  For convenience, put a copy on evlist.
7675562573SAdrian Hunter  */
7775562573SAdrian Hunter void perf_evlist__set_id_pos(struct perf_evlist *evlist)
7875562573SAdrian Hunter {
7975562573SAdrian Hunter 	struct perf_evsel *first = perf_evlist__first(evlist);
8075562573SAdrian Hunter 
8175562573SAdrian Hunter 	evlist->id_pos = first->id_pos;
8275562573SAdrian Hunter 	evlist->is_pos = first->is_pos;
8375562573SAdrian Hunter }
8475562573SAdrian Hunter 
85733cd2feSAdrian Hunter static void perf_evlist__update_id_pos(struct perf_evlist *evlist)
86733cd2feSAdrian Hunter {
87733cd2feSAdrian Hunter 	struct perf_evsel *evsel;
88733cd2feSAdrian Hunter 
890050f7aaSArnaldo Carvalho de Melo 	evlist__for_each(evlist, evsel)
90733cd2feSAdrian Hunter 		perf_evsel__calc_id_pos(evsel);
91733cd2feSAdrian Hunter 
92733cd2feSAdrian Hunter 	perf_evlist__set_id_pos(evlist);
93733cd2feSAdrian Hunter }
94733cd2feSAdrian Hunter 
95361c99a6SArnaldo Carvalho de Melo static void perf_evlist__purge(struct perf_evlist *evlist)
96361c99a6SArnaldo Carvalho de Melo {
97361c99a6SArnaldo Carvalho de Melo 	struct perf_evsel *pos, *n;
98361c99a6SArnaldo Carvalho de Melo 
990050f7aaSArnaldo Carvalho de Melo 	evlist__for_each_safe(evlist, n, pos) {
100361c99a6SArnaldo Carvalho de Melo 		list_del_init(&pos->node);
101361c99a6SArnaldo Carvalho de Melo 		perf_evsel__delete(pos);
102361c99a6SArnaldo Carvalho de Melo 	}
103361c99a6SArnaldo Carvalho de Melo 
104361c99a6SArnaldo Carvalho de Melo 	evlist->nr_entries = 0;
105361c99a6SArnaldo Carvalho de Melo }
106361c99a6SArnaldo Carvalho de Melo 
107ef1d1af2SArnaldo Carvalho de Melo void perf_evlist__exit(struct perf_evlist *evlist)
108ef1d1af2SArnaldo Carvalho de Melo {
10904662523SArnaldo Carvalho de Melo 	zfree(&evlist->mmap);
1101b85337dSArnaldo Carvalho de Melo 	fdarray__exit(&evlist->pollfd);
111ef1d1af2SArnaldo Carvalho de Melo }
112ef1d1af2SArnaldo Carvalho de Melo 
113361c99a6SArnaldo Carvalho de Melo void perf_evlist__delete(struct perf_evlist *evlist)
114361c99a6SArnaldo Carvalho de Melo {
115983874d1SArnaldo Carvalho de Melo 	perf_evlist__munmap(evlist);
116f26e1c7cSArnaldo Carvalho de Melo 	perf_evlist__close(evlist);
11703ad9747SArnaldo Carvalho de Melo 	cpu_map__delete(evlist->cpus);
11803ad9747SArnaldo Carvalho de Melo 	thread_map__delete(evlist->threads);
11903ad9747SArnaldo Carvalho de Melo 	evlist->cpus = NULL;
12003ad9747SArnaldo Carvalho de Melo 	evlist->threads = NULL;
121361c99a6SArnaldo Carvalho de Melo 	perf_evlist__purge(evlist);
122ef1d1af2SArnaldo Carvalho de Melo 	perf_evlist__exit(evlist);
123361c99a6SArnaldo Carvalho de Melo 	free(evlist);
124361c99a6SArnaldo Carvalho de Melo }
125361c99a6SArnaldo Carvalho de Melo 
126361c99a6SArnaldo Carvalho de Melo void perf_evlist__add(struct perf_evlist *evlist, struct perf_evsel *entry)
127361c99a6SArnaldo Carvalho de Melo {
128361c99a6SArnaldo Carvalho de Melo 	list_add_tail(&entry->node, &evlist->entries);
129ef503831SArnaldo Carvalho de Melo 	entry->idx = evlist->nr_entries;
13060b0896cSAdrian Hunter 	entry->tracking = !entry->idx;
131ef503831SArnaldo Carvalho de Melo 
13275562573SAdrian Hunter 	if (!evlist->nr_entries++)
13375562573SAdrian Hunter 		perf_evlist__set_id_pos(evlist);
134361c99a6SArnaldo Carvalho de Melo }
135361c99a6SArnaldo Carvalho de Melo 
1360529bc1fSJiri Olsa void perf_evlist__splice_list_tail(struct perf_evlist *evlist,
13750d08e47SArnaldo Carvalho de Melo 				   struct list_head *list,
13850d08e47SArnaldo Carvalho de Melo 				   int nr_entries)
13950d08e47SArnaldo Carvalho de Melo {
14075562573SAdrian Hunter 	bool set_id_pos = !evlist->nr_entries;
14175562573SAdrian Hunter 
14250d08e47SArnaldo Carvalho de Melo 	list_splice_tail(list, &evlist->entries);
14350d08e47SArnaldo Carvalho de Melo 	evlist->nr_entries += nr_entries;
14475562573SAdrian Hunter 	if (set_id_pos)
14575562573SAdrian Hunter 		perf_evlist__set_id_pos(evlist);
14650d08e47SArnaldo Carvalho de Melo }
14750d08e47SArnaldo Carvalho de Melo 
14863dab225SArnaldo Carvalho de Melo void __perf_evlist__set_leader(struct list_head *list)
14963dab225SArnaldo Carvalho de Melo {
15063dab225SArnaldo Carvalho de Melo 	struct perf_evsel *evsel, *leader;
15163dab225SArnaldo Carvalho de Melo 
15263dab225SArnaldo Carvalho de Melo 	leader = list_entry(list->next, struct perf_evsel, node);
15397f63e4aSNamhyung Kim 	evsel = list_entry(list->prev, struct perf_evsel, node);
15497f63e4aSNamhyung Kim 
15597f63e4aSNamhyung Kim 	leader->nr_members = evsel->idx - leader->idx + 1;
15663dab225SArnaldo Carvalho de Melo 
1570050f7aaSArnaldo Carvalho de Melo 	__evlist__for_each(list, evsel) {
15863dab225SArnaldo Carvalho de Melo 		evsel->leader = leader;
15963dab225SArnaldo Carvalho de Melo 	}
16063dab225SArnaldo Carvalho de Melo }
16163dab225SArnaldo Carvalho de Melo 
16263dab225SArnaldo Carvalho de Melo void perf_evlist__set_leader(struct perf_evlist *evlist)
1636a4bb04cSJiri Olsa {
16497f63e4aSNamhyung Kim 	if (evlist->nr_entries) {
16597f63e4aSNamhyung Kim 		evlist->nr_groups = evlist->nr_entries > 1 ? 1 : 0;
16663dab225SArnaldo Carvalho de Melo 		__perf_evlist__set_leader(&evlist->entries);
1676a4bb04cSJiri Olsa 	}
16897f63e4aSNamhyung Kim }
1696a4bb04cSJiri Olsa 
170361c99a6SArnaldo Carvalho de Melo int perf_evlist__add_default(struct perf_evlist *evlist)
171361c99a6SArnaldo Carvalho de Melo {
172361c99a6SArnaldo Carvalho de Melo 	struct perf_event_attr attr = {
173361c99a6SArnaldo Carvalho de Melo 		.type = PERF_TYPE_HARDWARE,
174361c99a6SArnaldo Carvalho de Melo 		.config = PERF_COUNT_HW_CPU_CYCLES,
175361c99a6SArnaldo Carvalho de Melo 	};
1761aed2671SJoerg Roedel 	struct perf_evsel *evsel;
177361c99a6SArnaldo Carvalho de Melo 
1781aed2671SJoerg Roedel 	event_attr_init(&attr);
1791aed2671SJoerg Roedel 
180ef503831SArnaldo Carvalho de Melo 	evsel = perf_evsel__new(&attr);
181361c99a6SArnaldo Carvalho de Melo 	if (evsel == NULL)
182cc2d86b0SStephane Eranian 		goto error;
183cc2d86b0SStephane Eranian 
184cc2d86b0SStephane Eranian 	/* use strdup() because free(evsel) assumes name is allocated */
185cc2d86b0SStephane Eranian 	evsel->name = strdup("cycles");
186cc2d86b0SStephane Eranian 	if (!evsel->name)
187cc2d86b0SStephane Eranian 		goto error_free;
188361c99a6SArnaldo Carvalho de Melo 
189361c99a6SArnaldo Carvalho de Melo 	perf_evlist__add(evlist, evsel);
190361c99a6SArnaldo Carvalho de Melo 	return 0;
191cc2d86b0SStephane Eranian error_free:
192cc2d86b0SStephane Eranian 	perf_evsel__delete(evsel);
193cc2d86b0SStephane Eranian error:
194cc2d86b0SStephane Eranian 	return -ENOMEM;
195361c99a6SArnaldo Carvalho de Melo }
1965c581041SArnaldo Carvalho de Melo 
197e60fc847SArnaldo Carvalho de Melo static int perf_evlist__add_attrs(struct perf_evlist *evlist,
19850d08e47SArnaldo Carvalho de Melo 				  struct perf_event_attr *attrs, size_t nr_attrs)
19950d08e47SArnaldo Carvalho de Melo {
20050d08e47SArnaldo Carvalho de Melo 	struct perf_evsel *evsel, *n;
20150d08e47SArnaldo Carvalho de Melo 	LIST_HEAD(head);
20250d08e47SArnaldo Carvalho de Melo 	size_t i;
20350d08e47SArnaldo Carvalho de Melo 
20450d08e47SArnaldo Carvalho de Melo 	for (i = 0; i < nr_attrs; i++) {
205ef503831SArnaldo Carvalho de Melo 		evsel = perf_evsel__new_idx(attrs + i, evlist->nr_entries + i);
20650d08e47SArnaldo Carvalho de Melo 		if (evsel == NULL)
20750d08e47SArnaldo Carvalho de Melo 			goto out_delete_partial_list;
20850d08e47SArnaldo Carvalho de Melo 		list_add_tail(&evsel->node, &head);
20950d08e47SArnaldo Carvalho de Melo 	}
21050d08e47SArnaldo Carvalho de Melo 
21150d08e47SArnaldo Carvalho de Melo 	perf_evlist__splice_list_tail(evlist, &head, nr_attrs);
21250d08e47SArnaldo Carvalho de Melo 
21350d08e47SArnaldo Carvalho de Melo 	return 0;
21450d08e47SArnaldo Carvalho de Melo 
21550d08e47SArnaldo Carvalho de Melo out_delete_partial_list:
2160050f7aaSArnaldo Carvalho de Melo 	__evlist__for_each_safe(&head, n, evsel)
21750d08e47SArnaldo Carvalho de Melo 		perf_evsel__delete(evsel);
21850d08e47SArnaldo Carvalho de Melo 	return -1;
21950d08e47SArnaldo Carvalho de Melo }
22050d08e47SArnaldo Carvalho de Melo 
22179695e1bSArnaldo Carvalho de Melo int __perf_evlist__add_default_attrs(struct perf_evlist *evlist,
22279695e1bSArnaldo Carvalho de Melo 				     struct perf_event_attr *attrs, size_t nr_attrs)
22379695e1bSArnaldo Carvalho de Melo {
22479695e1bSArnaldo Carvalho de Melo 	size_t i;
22579695e1bSArnaldo Carvalho de Melo 
22679695e1bSArnaldo Carvalho de Melo 	for (i = 0; i < nr_attrs; i++)
22779695e1bSArnaldo Carvalho de Melo 		event_attr_init(attrs + i);
22879695e1bSArnaldo Carvalho de Melo 
22979695e1bSArnaldo Carvalho de Melo 	return perf_evlist__add_attrs(evlist, attrs, nr_attrs);
23079695e1bSArnaldo Carvalho de Melo }
23179695e1bSArnaldo Carvalho de Melo 
232da378962SArnaldo Carvalho de Melo struct perf_evsel *
233ee29be62SArnaldo Carvalho de Melo perf_evlist__find_tracepoint_by_id(struct perf_evlist *evlist, int id)
234ee29be62SArnaldo Carvalho de Melo {
235ee29be62SArnaldo Carvalho de Melo 	struct perf_evsel *evsel;
236ee29be62SArnaldo Carvalho de Melo 
2370050f7aaSArnaldo Carvalho de Melo 	evlist__for_each(evlist, evsel) {
238ee29be62SArnaldo Carvalho de Melo 		if (evsel->attr.type   == PERF_TYPE_TRACEPOINT &&
239ee29be62SArnaldo Carvalho de Melo 		    (int)evsel->attr.config == id)
240ee29be62SArnaldo Carvalho de Melo 			return evsel;
241ee29be62SArnaldo Carvalho de Melo 	}
242ee29be62SArnaldo Carvalho de Melo 
243ee29be62SArnaldo Carvalho de Melo 	return NULL;
244ee29be62SArnaldo Carvalho de Melo }
245ee29be62SArnaldo Carvalho de Melo 
246a2f2804aSDavid Ahern struct perf_evsel *
247a2f2804aSDavid Ahern perf_evlist__find_tracepoint_by_name(struct perf_evlist *evlist,
248a2f2804aSDavid Ahern 				     const char *name)
249a2f2804aSDavid Ahern {
250a2f2804aSDavid Ahern 	struct perf_evsel *evsel;
251a2f2804aSDavid Ahern 
2520050f7aaSArnaldo Carvalho de Melo 	evlist__for_each(evlist, evsel) {
253a2f2804aSDavid Ahern 		if ((evsel->attr.type == PERF_TYPE_TRACEPOINT) &&
254a2f2804aSDavid Ahern 		    (strcmp(evsel->name, name) == 0))
255a2f2804aSDavid Ahern 			return evsel;
256a2f2804aSDavid Ahern 	}
257a2f2804aSDavid Ahern 
258a2f2804aSDavid Ahern 	return NULL;
259a2f2804aSDavid Ahern }
260a2f2804aSDavid Ahern 
26139876e7dSArnaldo Carvalho de Melo int perf_evlist__add_newtp(struct perf_evlist *evlist,
26239876e7dSArnaldo Carvalho de Melo 			   const char *sys, const char *name, void *handler)
26339876e7dSArnaldo Carvalho de Melo {
264ef503831SArnaldo Carvalho de Melo 	struct perf_evsel *evsel = perf_evsel__newtp(sys, name);
26539876e7dSArnaldo Carvalho de Melo 
26639876e7dSArnaldo Carvalho de Melo 	if (evsel == NULL)
26739876e7dSArnaldo Carvalho de Melo 		return -1;
26839876e7dSArnaldo Carvalho de Melo 
269744a9719SArnaldo Carvalho de Melo 	evsel->handler = handler;
27039876e7dSArnaldo Carvalho de Melo 	perf_evlist__add(evlist, evsel);
27139876e7dSArnaldo Carvalho de Melo 	return 0;
27239876e7dSArnaldo Carvalho de Melo }
27339876e7dSArnaldo Carvalho de Melo 
274bf8e8f4bSAdrian Hunter static int perf_evlist__nr_threads(struct perf_evlist *evlist,
275bf8e8f4bSAdrian Hunter 				   struct perf_evsel *evsel)
276bf8e8f4bSAdrian Hunter {
277bf8e8f4bSAdrian Hunter 	if (evsel->system_wide)
278bf8e8f4bSAdrian Hunter 		return 1;
279bf8e8f4bSAdrian Hunter 	else
280bf8e8f4bSAdrian Hunter 		return thread_map__nr(evlist->threads);
281bf8e8f4bSAdrian Hunter }
282bf8e8f4bSAdrian Hunter 
2834152ab37SArnaldo Carvalho de Melo void perf_evlist__disable(struct perf_evlist *evlist)
2844152ab37SArnaldo Carvalho de Melo {
2854152ab37SArnaldo Carvalho de Melo 	int cpu, thread;
2864152ab37SArnaldo Carvalho de Melo 	struct perf_evsel *pos;
287b3a319d5SNamhyung Kim 	int nr_cpus = cpu_map__nr(evlist->cpus);
288bf8e8f4bSAdrian Hunter 	int nr_threads;
2894152ab37SArnaldo Carvalho de Melo 
290b3a319d5SNamhyung Kim 	for (cpu = 0; cpu < nr_cpus; cpu++) {
2910050f7aaSArnaldo Carvalho de Melo 		evlist__for_each(evlist, pos) {
292395c3070SAdrian Hunter 			if (!perf_evsel__is_group_leader(pos) || !pos->fd)
2933fe4430dSJiri Olsa 				continue;
294bf8e8f4bSAdrian Hunter 			nr_threads = perf_evlist__nr_threads(evlist, pos);
295b3a319d5SNamhyung Kim 			for (thread = 0; thread < nr_threads; thread++)
29655da8005SNamhyung Kim 				ioctl(FD(pos, cpu, thread),
29755da8005SNamhyung Kim 				      PERF_EVENT_IOC_DISABLE, 0);
2984152ab37SArnaldo Carvalho de Melo 		}
2994152ab37SArnaldo Carvalho de Melo 	}
3004152ab37SArnaldo Carvalho de Melo }
3014152ab37SArnaldo Carvalho de Melo 
302764e16a3SDavid Ahern void perf_evlist__enable(struct perf_evlist *evlist)
303764e16a3SDavid Ahern {
304764e16a3SDavid Ahern 	int cpu, thread;
305764e16a3SDavid Ahern 	struct perf_evsel *pos;
306b3a319d5SNamhyung Kim 	int nr_cpus = cpu_map__nr(evlist->cpus);
307bf8e8f4bSAdrian Hunter 	int nr_threads;
308764e16a3SDavid Ahern 
309b3a319d5SNamhyung Kim 	for (cpu = 0; cpu < nr_cpus; cpu++) {
3100050f7aaSArnaldo Carvalho de Melo 		evlist__for_each(evlist, pos) {
311395c3070SAdrian Hunter 			if (!perf_evsel__is_group_leader(pos) || !pos->fd)
3123fe4430dSJiri Olsa 				continue;
313bf8e8f4bSAdrian Hunter 			nr_threads = perf_evlist__nr_threads(evlist, pos);
314b3a319d5SNamhyung Kim 			for (thread = 0; thread < nr_threads; thread++)
31555da8005SNamhyung Kim 				ioctl(FD(pos, cpu, thread),
31655da8005SNamhyung Kim 				      PERF_EVENT_IOC_ENABLE, 0);
317764e16a3SDavid Ahern 		}
318764e16a3SDavid Ahern 	}
319764e16a3SDavid Ahern }
320764e16a3SDavid Ahern 
321395c3070SAdrian Hunter int perf_evlist__disable_event(struct perf_evlist *evlist,
322395c3070SAdrian Hunter 			       struct perf_evsel *evsel)
323395c3070SAdrian Hunter {
324395c3070SAdrian Hunter 	int cpu, thread, err;
325bf8e8f4bSAdrian Hunter 	int nr_cpus = cpu_map__nr(evlist->cpus);
326bf8e8f4bSAdrian Hunter 	int nr_threads = perf_evlist__nr_threads(evlist, evsel);
327395c3070SAdrian Hunter 
328395c3070SAdrian Hunter 	if (!evsel->fd)
329395c3070SAdrian Hunter 		return 0;
330395c3070SAdrian Hunter 
331bf8e8f4bSAdrian Hunter 	for (cpu = 0; cpu < nr_cpus; cpu++) {
332bf8e8f4bSAdrian Hunter 		for (thread = 0; thread < nr_threads; thread++) {
333395c3070SAdrian Hunter 			err = ioctl(FD(evsel, cpu, thread),
334395c3070SAdrian Hunter 				    PERF_EVENT_IOC_DISABLE, 0);
335395c3070SAdrian Hunter 			if (err)
336395c3070SAdrian Hunter 				return err;
337395c3070SAdrian Hunter 		}
338395c3070SAdrian Hunter 	}
339395c3070SAdrian Hunter 	return 0;
340395c3070SAdrian Hunter }
341395c3070SAdrian Hunter 
342395c3070SAdrian Hunter int perf_evlist__enable_event(struct perf_evlist *evlist,
343395c3070SAdrian Hunter 			      struct perf_evsel *evsel)
344395c3070SAdrian Hunter {
345395c3070SAdrian Hunter 	int cpu, thread, err;
346bf8e8f4bSAdrian Hunter 	int nr_cpus = cpu_map__nr(evlist->cpus);
347bf8e8f4bSAdrian Hunter 	int nr_threads = perf_evlist__nr_threads(evlist, evsel);
348395c3070SAdrian Hunter 
349395c3070SAdrian Hunter 	if (!evsel->fd)
350395c3070SAdrian Hunter 		return -EINVAL;
351395c3070SAdrian Hunter 
352bf8e8f4bSAdrian Hunter 	for (cpu = 0; cpu < nr_cpus; cpu++) {
353bf8e8f4bSAdrian Hunter 		for (thread = 0; thread < nr_threads; thread++) {
354395c3070SAdrian Hunter 			err = ioctl(FD(evsel, cpu, thread),
355395c3070SAdrian Hunter 				    PERF_EVENT_IOC_ENABLE, 0);
356395c3070SAdrian Hunter 			if (err)
357395c3070SAdrian Hunter 				return err;
358395c3070SAdrian Hunter 		}
359395c3070SAdrian Hunter 	}
360395c3070SAdrian Hunter 	return 0;
361395c3070SAdrian Hunter }
362395c3070SAdrian Hunter 
3631c65056cSAdrian Hunter static int perf_evlist__enable_event_cpu(struct perf_evlist *evlist,
3641c65056cSAdrian Hunter 					 struct perf_evsel *evsel, int cpu)
3651c65056cSAdrian Hunter {
3661c65056cSAdrian Hunter 	int thread, err;
3671c65056cSAdrian Hunter 	int nr_threads = perf_evlist__nr_threads(evlist, evsel);
3681c65056cSAdrian Hunter 
3691c65056cSAdrian Hunter 	if (!evsel->fd)
3701c65056cSAdrian Hunter 		return -EINVAL;
3711c65056cSAdrian Hunter 
3721c65056cSAdrian Hunter 	for (thread = 0; thread < nr_threads; thread++) {
3731c65056cSAdrian Hunter 		err = ioctl(FD(evsel, cpu, thread),
3741c65056cSAdrian Hunter 			    PERF_EVENT_IOC_ENABLE, 0);
3751c65056cSAdrian Hunter 		if (err)
3761c65056cSAdrian Hunter 			return err;
3771c65056cSAdrian Hunter 	}
3781c65056cSAdrian Hunter 	return 0;
3791c65056cSAdrian Hunter }
3801c65056cSAdrian Hunter 
3811c65056cSAdrian Hunter static int perf_evlist__enable_event_thread(struct perf_evlist *evlist,
3821c65056cSAdrian Hunter 					    struct perf_evsel *evsel,
3831c65056cSAdrian Hunter 					    int thread)
3841c65056cSAdrian Hunter {
3851c65056cSAdrian Hunter 	int cpu, err;
3861c65056cSAdrian Hunter 	int nr_cpus = cpu_map__nr(evlist->cpus);
3871c65056cSAdrian Hunter 
3881c65056cSAdrian Hunter 	if (!evsel->fd)
3891c65056cSAdrian Hunter 		return -EINVAL;
3901c65056cSAdrian Hunter 
3911c65056cSAdrian Hunter 	for (cpu = 0; cpu < nr_cpus; cpu++) {
3921c65056cSAdrian Hunter 		err = ioctl(FD(evsel, cpu, thread), PERF_EVENT_IOC_ENABLE, 0);
3931c65056cSAdrian Hunter 		if (err)
3941c65056cSAdrian Hunter 			return err;
3951c65056cSAdrian Hunter 	}
3961c65056cSAdrian Hunter 	return 0;
3971c65056cSAdrian Hunter }
3981c65056cSAdrian Hunter 
3991c65056cSAdrian Hunter int perf_evlist__enable_event_idx(struct perf_evlist *evlist,
4001c65056cSAdrian Hunter 				  struct perf_evsel *evsel, int idx)
4011c65056cSAdrian Hunter {
4021c65056cSAdrian Hunter 	bool per_cpu_mmaps = !cpu_map__empty(evlist->cpus);
4031c65056cSAdrian Hunter 
4041c65056cSAdrian Hunter 	if (per_cpu_mmaps)
4051c65056cSAdrian Hunter 		return perf_evlist__enable_event_cpu(evlist, evsel, idx);
4061c65056cSAdrian Hunter 	else
4071c65056cSAdrian Hunter 		return perf_evlist__enable_event_thread(evlist, evsel, idx);
4081c65056cSAdrian Hunter }
4091c65056cSAdrian Hunter 
410ad6765ddSArnaldo Carvalho de Melo int perf_evlist__alloc_pollfd(struct perf_evlist *evlist)
4115c581041SArnaldo Carvalho de Melo {
412b3a319d5SNamhyung Kim 	int nr_cpus = cpu_map__nr(evlist->cpus);
413b3a319d5SNamhyung Kim 	int nr_threads = thread_map__nr(evlist->threads);
414bf8e8f4bSAdrian Hunter 	int nfds = 0;
415bf8e8f4bSAdrian Hunter 	struct perf_evsel *evsel;
416bf8e8f4bSAdrian Hunter 
417cba9b847SArnaldo Carvalho de Melo 	evlist__for_each(evlist, evsel) {
418bf8e8f4bSAdrian Hunter 		if (evsel->system_wide)
419bf8e8f4bSAdrian Hunter 			nfds += nr_cpus;
420bf8e8f4bSAdrian Hunter 		else
421bf8e8f4bSAdrian Hunter 			nfds += nr_cpus * nr_threads;
422bf8e8f4bSAdrian Hunter 	}
423bf8e8f4bSAdrian Hunter 
4241b85337dSArnaldo Carvalho de Melo 	if (fdarray__available_entries(&evlist->pollfd) < nfds &&
4251b85337dSArnaldo Carvalho de Melo 	    fdarray__grow(&evlist->pollfd, nfds) < 0)
426ad6765ddSArnaldo Carvalho de Melo 		return -ENOMEM;
427ad6765ddSArnaldo Carvalho de Melo 
428ad6765ddSArnaldo Carvalho de Melo 	return 0;
4295c581041SArnaldo Carvalho de Melo }
43070082dd9SArnaldo Carvalho de Melo 
431e4b356b5SArnaldo Carvalho de Melo static int __perf_evlist__add_pollfd(struct perf_evlist *evlist, int fd, int idx)
432e4b356b5SArnaldo Carvalho de Melo {
433e4b356b5SArnaldo Carvalho de Melo 	int pos = fdarray__add(&evlist->pollfd, fd, POLLIN | POLLERR | POLLHUP);
434e4b356b5SArnaldo Carvalho de Melo 	/*
435e4b356b5SArnaldo Carvalho de Melo 	 * Save the idx so that when we filter out fds POLLHUP'ed we can
436e4b356b5SArnaldo Carvalho de Melo 	 * close the associated evlist->mmap[] entry.
437e4b356b5SArnaldo Carvalho de Melo 	 */
438e4b356b5SArnaldo Carvalho de Melo 	if (pos >= 0) {
439e4b356b5SArnaldo Carvalho de Melo 		evlist->pollfd.priv[pos].idx = idx;
440e4b356b5SArnaldo Carvalho de Melo 
441e4b356b5SArnaldo Carvalho de Melo 		fcntl(fd, F_SETFL, O_NONBLOCK);
442e4b356b5SArnaldo Carvalho de Melo 	}
443e4b356b5SArnaldo Carvalho de Melo 
444e4b356b5SArnaldo Carvalho de Melo 	return pos;
445e4b356b5SArnaldo Carvalho de Melo }
446e4b356b5SArnaldo Carvalho de Melo 
447ad6765ddSArnaldo Carvalho de Melo int perf_evlist__add_pollfd(struct perf_evlist *evlist, int fd)
44870082dd9SArnaldo Carvalho de Melo {
449e4b356b5SArnaldo Carvalho de Melo 	return __perf_evlist__add_pollfd(evlist, fd, -1);
450e4b356b5SArnaldo Carvalho de Melo }
4511b85337dSArnaldo Carvalho de Melo 
452e4b356b5SArnaldo Carvalho de Melo static void perf_evlist__munmap_filtered(struct fdarray *fda, int fd)
453e4b356b5SArnaldo Carvalho de Melo {
454e4b356b5SArnaldo Carvalho de Melo 	struct perf_evlist *evlist = container_of(fda, struct perf_evlist, pollfd);
455e4b356b5SArnaldo Carvalho de Melo 
456e4b356b5SArnaldo Carvalho de Melo 	perf_evlist__mmap_put(evlist, fda->priv[fd].idx);
45770082dd9SArnaldo Carvalho de Melo }
45870db7533SArnaldo Carvalho de Melo 
4591ddec7f0SArnaldo Carvalho de Melo int perf_evlist__filter_pollfd(struct perf_evlist *evlist, short revents_and_mask)
4601ddec7f0SArnaldo Carvalho de Melo {
461e4b356b5SArnaldo Carvalho de Melo 	return fdarray__filter(&evlist->pollfd, revents_and_mask,
462e4b356b5SArnaldo Carvalho de Melo 			       perf_evlist__munmap_filtered);
4631ddec7f0SArnaldo Carvalho de Melo }
4641ddec7f0SArnaldo Carvalho de Melo 
465f66a889dSArnaldo Carvalho de Melo int perf_evlist__poll(struct perf_evlist *evlist, int timeout)
466f66a889dSArnaldo Carvalho de Melo {
4671b85337dSArnaldo Carvalho de Melo 	return fdarray__poll(&evlist->pollfd, timeout);
468f66a889dSArnaldo Carvalho de Melo }
469f66a889dSArnaldo Carvalho de Melo 
470a91e5431SArnaldo Carvalho de Melo static void perf_evlist__id_hash(struct perf_evlist *evlist,
471a91e5431SArnaldo Carvalho de Melo 				 struct perf_evsel *evsel,
4723d3b5e95SArnaldo Carvalho de Melo 				 int cpu, int thread, u64 id)
4733d3b5e95SArnaldo Carvalho de Melo {
4743d3b5e95SArnaldo Carvalho de Melo 	int hash;
4753d3b5e95SArnaldo Carvalho de Melo 	struct perf_sample_id *sid = SID(evsel, cpu, thread);
4763d3b5e95SArnaldo Carvalho de Melo 
4773d3b5e95SArnaldo Carvalho de Melo 	sid->id = id;
4783d3b5e95SArnaldo Carvalho de Melo 	sid->evsel = evsel;
4793d3b5e95SArnaldo Carvalho de Melo 	hash = hash_64(sid->id, PERF_EVLIST__HLIST_BITS);
4803d3b5e95SArnaldo Carvalho de Melo 	hlist_add_head(&sid->node, &evlist->heads[hash]);
4813d3b5e95SArnaldo Carvalho de Melo }
4823d3b5e95SArnaldo Carvalho de Melo 
483a91e5431SArnaldo Carvalho de Melo void perf_evlist__id_add(struct perf_evlist *evlist, struct perf_evsel *evsel,
484a91e5431SArnaldo Carvalho de Melo 			 int cpu, int thread, u64 id)
485a91e5431SArnaldo Carvalho de Melo {
486a91e5431SArnaldo Carvalho de Melo 	perf_evlist__id_hash(evlist, evsel, cpu, thread, id);
487a91e5431SArnaldo Carvalho de Melo 	evsel->id[evsel->ids++] = id;
488a91e5431SArnaldo Carvalho de Melo }
489a91e5431SArnaldo Carvalho de Melo 
490a91e5431SArnaldo Carvalho de Melo static int perf_evlist__id_add_fd(struct perf_evlist *evlist,
4913d3b5e95SArnaldo Carvalho de Melo 				  struct perf_evsel *evsel,
492f8a95309SArnaldo Carvalho de Melo 				  int cpu, int thread, int fd)
493f8a95309SArnaldo Carvalho de Melo {
494f8a95309SArnaldo Carvalho de Melo 	u64 read_data[4] = { 0, };
4953d3b5e95SArnaldo Carvalho de Melo 	int id_idx = 1; /* The first entry is the counter value */
496e2b5abe0SJiri Olsa 	u64 id;
497e2b5abe0SJiri Olsa 	int ret;
498e2b5abe0SJiri Olsa 
499e2b5abe0SJiri Olsa 	ret = ioctl(fd, PERF_EVENT_IOC_ID, &id);
500e2b5abe0SJiri Olsa 	if (!ret)
501e2b5abe0SJiri Olsa 		goto add;
502e2b5abe0SJiri Olsa 
503e2b5abe0SJiri Olsa 	if (errno != ENOTTY)
504e2b5abe0SJiri Olsa 		return -1;
505e2b5abe0SJiri Olsa 
506e2b5abe0SJiri Olsa 	/* Legacy way to get event id.. All hail to old kernels! */
507f8a95309SArnaldo Carvalho de Melo 
508c4861afeSJiri Olsa 	/*
509c4861afeSJiri Olsa 	 * This way does not work with group format read, so bail
510c4861afeSJiri Olsa 	 * out in that case.
511c4861afeSJiri Olsa 	 */
512c4861afeSJiri Olsa 	if (perf_evlist__read_format(evlist) & PERF_FORMAT_GROUP)
513c4861afeSJiri Olsa 		return -1;
514c4861afeSJiri Olsa 
515f8a95309SArnaldo Carvalho de Melo 	if (!(evsel->attr.read_format & PERF_FORMAT_ID) ||
516f8a95309SArnaldo Carvalho de Melo 	    read(fd, &read_data, sizeof(read_data)) == -1)
517f8a95309SArnaldo Carvalho de Melo 		return -1;
518f8a95309SArnaldo Carvalho de Melo 
519f8a95309SArnaldo Carvalho de Melo 	if (evsel->attr.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
520f8a95309SArnaldo Carvalho de Melo 		++id_idx;
521f8a95309SArnaldo Carvalho de Melo 	if (evsel->attr.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
522f8a95309SArnaldo Carvalho de Melo 		++id_idx;
523f8a95309SArnaldo Carvalho de Melo 
524e2b5abe0SJiri Olsa 	id = read_data[id_idx];
525e2b5abe0SJiri Olsa 
526e2b5abe0SJiri Olsa  add:
527e2b5abe0SJiri Olsa 	perf_evlist__id_add(evlist, evsel, cpu, thread, id);
528f8a95309SArnaldo Carvalho de Melo 	return 0;
529f8a95309SArnaldo Carvalho de Melo }
530f8a95309SArnaldo Carvalho de Melo 
5313c659eedSAdrian Hunter static void perf_evlist__set_sid_idx(struct perf_evlist *evlist,
5323c659eedSAdrian Hunter 				     struct perf_evsel *evsel, int idx, int cpu,
5333c659eedSAdrian Hunter 				     int thread)
5343c659eedSAdrian Hunter {
5353c659eedSAdrian Hunter 	struct perf_sample_id *sid = SID(evsel, cpu, thread);
5363c659eedSAdrian Hunter 	sid->idx = idx;
5373c659eedSAdrian Hunter 	if (evlist->cpus && cpu >= 0)
5383c659eedSAdrian Hunter 		sid->cpu = evlist->cpus->map[cpu];
5393c659eedSAdrian Hunter 	else
5403c659eedSAdrian Hunter 		sid->cpu = -1;
5413c659eedSAdrian Hunter 	if (!evsel->system_wide && evlist->threads && thread >= 0)
5423c659eedSAdrian Hunter 		sid->tid = evlist->threads->map[thread];
5433c659eedSAdrian Hunter 	else
5443c659eedSAdrian Hunter 		sid->tid = -1;
5453c659eedSAdrian Hunter }
5463c659eedSAdrian Hunter 
547932a3594SJiri Olsa struct perf_sample_id *perf_evlist__id2sid(struct perf_evlist *evlist, u64 id)
54870db7533SArnaldo Carvalho de Melo {
54970db7533SArnaldo Carvalho de Melo 	struct hlist_head *head;
55070db7533SArnaldo Carvalho de Melo 	struct perf_sample_id *sid;
55170db7533SArnaldo Carvalho de Melo 	int hash;
55270db7533SArnaldo Carvalho de Melo 
55370db7533SArnaldo Carvalho de Melo 	hash = hash_64(id, PERF_EVLIST__HLIST_BITS);
55470db7533SArnaldo Carvalho de Melo 	head = &evlist->heads[hash];
55570db7533SArnaldo Carvalho de Melo 
556b67bfe0dSSasha Levin 	hlist_for_each_entry(sid, head, node)
55770db7533SArnaldo Carvalho de Melo 		if (sid->id == id)
558932a3594SJiri Olsa 			return sid;
559932a3594SJiri Olsa 
560932a3594SJiri Olsa 	return NULL;
561932a3594SJiri Olsa }
562932a3594SJiri Olsa 
563932a3594SJiri Olsa struct perf_evsel *perf_evlist__id2evsel(struct perf_evlist *evlist, u64 id)
564932a3594SJiri Olsa {
565932a3594SJiri Olsa 	struct perf_sample_id *sid;
566932a3594SJiri Olsa 
567932a3594SJiri Olsa 	if (evlist->nr_entries == 1)
568932a3594SJiri Olsa 		return perf_evlist__first(evlist);
569932a3594SJiri Olsa 
570932a3594SJiri Olsa 	sid = perf_evlist__id2sid(evlist, id);
571932a3594SJiri Olsa 	if (sid)
57270db7533SArnaldo Carvalho de Melo 		return sid->evsel;
57330e68bccSNamhyung Kim 
57430e68bccSNamhyung Kim 	if (!perf_evlist__sample_id_all(evlist))
5750c21f736SArnaldo Carvalho de Melo 		return perf_evlist__first(evlist);
57630e68bccSNamhyung Kim 
57770db7533SArnaldo Carvalho de Melo 	return NULL;
57870db7533SArnaldo Carvalho de Melo }
57904391debSArnaldo Carvalho de Melo 
58075562573SAdrian Hunter static int perf_evlist__event2id(struct perf_evlist *evlist,
58175562573SAdrian Hunter 				 union perf_event *event, u64 *id)
58275562573SAdrian Hunter {
58375562573SAdrian Hunter 	const u64 *array = event->sample.array;
58475562573SAdrian Hunter 	ssize_t n;
58575562573SAdrian Hunter 
58675562573SAdrian Hunter 	n = (event->header.size - sizeof(event->header)) >> 3;
58775562573SAdrian Hunter 
58875562573SAdrian Hunter 	if (event->header.type == PERF_RECORD_SAMPLE) {
58975562573SAdrian Hunter 		if (evlist->id_pos >= n)
59075562573SAdrian Hunter 			return -1;
59175562573SAdrian Hunter 		*id = array[evlist->id_pos];
59275562573SAdrian Hunter 	} else {
59375562573SAdrian Hunter 		if (evlist->is_pos > n)
59475562573SAdrian Hunter 			return -1;
59575562573SAdrian Hunter 		n -= evlist->is_pos;
59675562573SAdrian Hunter 		*id = array[n];
59775562573SAdrian Hunter 	}
59875562573SAdrian Hunter 	return 0;
59975562573SAdrian Hunter }
60075562573SAdrian Hunter 
60175562573SAdrian Hunter static struct perf_evsel *perf_evlist__event2evsel(struct perf_evlist *evlist,
60275562573SAdrian Hunter 						   union perf_event *event)
60375562573SAdrian Hunter {
60498be6966SAdrian Hunter 	struct perf_evsel *first = perf_evlist__first(evlist);
60575562573SAdrian Hunter 	struct hlist_head *head;
60675562573SAdrian Hunter 	struct perf_sample_id *sid;
60775562573SAdrian Hunter 	int hash;
60875562573SAdrian Hunter 	u64 id;
60975562573SAdrian Hunter 
61075562573SAdrian Hunter 	if (evlist->nr_entries == 1)
61198be6966SAdrian Hunter 		return first;
61298be6966SAdrian Hunter 
61398be6966SAdrian Hunter 	if (!first->attr.sample_id_all &&
61498be6966SAdrian Hunter 	    event->header.type != PERF_RECORD_SAMPLE)
61598be6966SAdrian Hunter 		return first;
61675562573SAdrian Hunter 
61775562573SAdrian Hunter 	if (perf_evlist__event2id(evlist, event, &id))
61875562573SAdrian Hunter 		return NULL;
61975562573SAdrian Hunter 
62075562573SAdrian Hunter 	/* Synthesized events have an id of zero */
62175562573SAdrian Hunter 	if (!id)
62298be6966SAdrian Hunter 		return first;
62375562573SAdrian Hunter 
62475562573SAdrian Hunter 	hash = hash_64(id, PERF_EVLIST__HLIST_BITS);
62575562573SAdrian Hunter 	head = &evlist->heads[hash];
62675562573SAdrian Hunter 
62775562573SAdrian Hunter 	hlist_for_each_entry(sid, head, node) {
62875562573SAdrian Hunter 		if (sid->id == id)
62975562573SAdrian Hunter 			return sid->evsel;
63075562573SAdrian Hunter 	}
63175562573SAdrian Hunter 	return NULL;
63275562573SAdrian Hunter }
63375562573SAdrian Hunter 
634aece948fSArnaldo Carvalho de Melo union perf_event *perf_evlist__mmap_read(struct perf_evlist *evlist, int idx)
63504391debSArnaldo Carvalho de Melo {
636aece948fSArnaldo Carvalho de Melo 	struct perf_mmap *md = &evlist->mmap[idx];
6377b8283b5SDavid Ahern 	u64 head = perf_mmap__read_head(md);
6387b8283b5SDavid Ahern 	u64 old = md->prev;
63904391debSArnaldo Carvalho de Melo 	unsigned char *data = md->base + page_size;
6408115d60cSArnaldo Carvalho de Melo 	union perf_event *event = NULL;
64104391debSArnaldo Carvalho de Melo 
6427bb41152SArnaldo Carvalho de Melo 	if (evlist->overwrite) {
64304391debSArnaldo Carvalho de Melo 		/*
64404391debSArnaldo Carvalho de Melo 		 * If we're further behind than half the buffer, there's a chance
64504391debSArnaldo Carvalho de Melo 		 * the writer will bite our tail and mess up the samples under us.
64604391debSArnaldo Carvalho de Melo 		 *
64704391debSArnaldo Carvalho de Melo 		 * If we somehow ended up ahead of the head, we got messed up.
64804391debSArnaldo Carvalho de Melo 		 *
64904391debSArnaldo Carvalho de Melo 		 * In either case, truncate and restart at head.
65004391debSArnaldo Carvalho de Melo 		 */
6517bb41152SArnaldo Carvalho de Melo 		int diff = head - old;
65204391debSArnaldo Carvalho de Melo 		if (diff > md->mask / 2 || diff < 0) {
65304391debSArnaldo Carvalho de Melo 			fprintf(stderr, "WARNING: failed to keep up with mmap data.\n");
65404391debSArnaldo Carvalho de Melo 
65504391debSArnaldo Carvalho de Melo 			/*
65604391debSArnaldo Carvalho de Melo 			 * head points to a known good entry, start there.
65704391debSArnaldo Carvalho de Melo 			 */
65804391debSArnaldo Carvalho de Melo 			old = head;
65904391debSArnaldo Carvalho de Melo 		}
6607bb41152SArnaldo Carvalho de Melo 	}
66104391debSArnaldo Carvalho de Melo 
66204391debSArnaldo Carvalho de Melo 	if (old != head) {
66304391debSArnaldo Carvalho de Melo 		size_t size;
66404391debSArnaldo Carvalho de Melo 
6658115d60cSArnaldo Carvalho de Melo 		event = (union perf_event *)&data[old & md->mask];
66604391debSArnaldo Carvalho de Melo 		size = event->header.size;
66704391debSArnaldo Carvalho de Melo 
66804391debSArnaldo Carvalho de Melo 		/*
66904391debSArnaldo Carvalho de Melo 		 * Event straddles the mmap boundary -- header should always
67004391debSArnaldo Carvalho de Melo 		 * be inside due to u64 alignment of output.
67104391debSArnaldo Carvalho de Melo 		 */
67204391debSArnaldo Carvalho de Melo 		if ((old & md->mask) + size != ((old + size) & md->mask)) {
67304391debSArnaldo Carvalho de Melo 			unsigned int offset = old;
67404391debSArnaldo Carvalho de Melo 			unsigned int len = min(sizeof(*event), size), cpy;
675a65cb4b9SJiri Olsa 			void *dst = md->event_copy;
67604391debSArnaldo Carvalho de Melo 
67704391debSArnaldo Carvalho de Melo 			do {
67804391debSArnaldo Carvalho de Melo 				cpy = min(md->mask + 1 - (offset & md->mask), len);
67904391debSArnaldo Carvalho de Melo 				memcpy(dst, &data[offset & md->mask], cpy);
68004391debSArnaldo Carvalho de Melo 				offset += cpy;
68104391debSArnaldo Carvalho de Melo 				dst += cpy;
68204391debSArnaldo Carvalho de Melo 				len -= cpy;
68304391debSArnaldo Carvalho de Melo 			} while (len);
68404391debSArnaldo Carvalho de Melo 
685a65cb4b9SJiri Olsa 			event = (union perf_event *) md->event_copy;
68604391debSArnaldo Carvalho de Melo 		}
68704391debSArnaldo Carvalho de Melo 
68804391debSArnaldo Carvalho de Melo 		old += size;
68904391debSArnaldo Carvalho de Melo 	}
69004391debSArnaldo Carvalho de Melo 
69104391debSArnaldo Carvalho de Melo 	md->prev = old;
6927bb41152SArnaldo Carvalho de Melo 
69304391debSArnaldo Carvalho de Melo 	return event;
69404391debSArnaldo Carvalho de Melo }
695f8a95309SArnaldo Carvalho de Melo 
69682396986SArnaldo Carvalho de Melo static bool perf_mmap__empty(struct perf_mmap *md)
69782396986SArnaldo Carvalho de Melo {
698b72e74d1SAdrian Hunter 	return perf_mmap__read_head(md) == md->prev && !md->auxtrace_mmap.base;
69982396986SArnaldo Carvalho de Melo }
70082396986SArnaldo Carvalho de Melo 
70182396986SArnaldo Carvalho de Melo static void perf_evlist__mmap_get(struct perf_evlist *evlist, int idx)
70282396986SArnaldo Carvalho de Melo {
70382396986SArnaldo Carvalho de Melo 	++evlist->mmap[idx].refcnt;
70482396986SArnaldo Carvalho de Melo }
70582396986SArnaldo Carvalho de Melo 
70682396986SArnaldo Carvalho de Melo static void perf_evlist__mmap_put(struct perf_evlist *evlist, int idx)
70782396986SArnaldo Carvalho de Melo {
70882396986SArnaldo Carvalho de Melo 	BUG_ON(evlist->mmap[idx].refcnt == 0);
70982396986SArnaldo Carvalho de Melo 
71082396986SArnaldo Carvalho de Melo 	if (--evlist->mmap[idx].refcnt == 0)
71182396986SArnaldo Carvalho de Melo 		__perf_evlist__munmap(evlist, idx);
71282396986SArnaldo Carvalho de Melo }
71382396986SArnaldo Carvalho de Melo 
7148e50d384SZhouyi Zhou void perf_evlist__mmap_consume(struct perf_evlist *evlist, int idx)
7158e50d384SZhouyi Zhou {
7168e50d384SZhouyi Zhou 	struct perf_mmap *md = &evlist->mmap[idx];
71782396986SArnaldo Carvalho de Melo 
71882396986SArnaldo Carvalho de Melo 	if (!evlist->overwrite) {
7197b8283b5SDavid Ahern 		u64 old = md->prev;
7208e50d384SZhouyi Zhou 
7218e50d384SZhouyi Zhou 		perf_mmap__write_tail(md, old);
7228e50d384SZhouyi Zhou 	}
72382396986SArnaldo Carvalho de Melo 
72482396986SArnaldo Carvalho de Melo 	if (md->refcnt == 1 && perf_mmap__empty(md))
72582396986SArnaldo Carvalho de Melo 		perf_evlist__mmap_put(evlist, idx);
7268e50d384SZhouyi Zhou }
7278e50d384SZhouyi Zhou 
728718c602dSAdrian Hunter int __weak auxtrace_mmap__mmap(struct auxtrace_mmap *mm __maybe_unused,
729718c602dSAdrian Hunter 			       struct auxtrace_mmap_params *mp __maybe_unused,
730718c602dSAdrian Hunter 			       void *userpg __maybe_unused,
731718c602dSAdrian Hunter 			       int fd __maybe_unused)
732718c602dSAdrian Hunter {
733718c602dSAdrian Hunter 	return 0;
734718c602dSAdrian Hunter }
735718c602dSAdrian Hunter 
736718c602dSAdrian Hunter void __weak auxtrace_mmap__munmap(struct auxtrace_mmap *mm __maybe_unused)
737718c602dSAdrian Hunter {
738718c602dSAdrian Hunter }
739718c602dSAdrian Hunter 
740718c602dSAdrian Hunter void __weak auxtrace_mmap_params__init(
741718c602dSAdrian Hunter 			struct auxtrace_mmap_params *mp __maybe_unused,
742718c602dSAdrian Hunter 			off_t auxtrace_offset __maybe_unused,
743718c602dSAdrian Hunter 			unsigned int auxtrace_pages __maybe_unused,
744718c602dSAdrian Hunter 			bool auxtrace_overwrite __maybe_unused)
745718c602dSAdrian Hunter {
746718c602dSAdrian Hunter }
747718c602dSAdrian Hunter 
748718c602dSAdrian Hunter void __weak auxtrace_mmap_params__set_idx(
749718c602dSAdrian Hunter 			struct auxtrace_mmap_params *mp __maybe_unused,
750718c602dSAdrian Hunter 			struct perf_evlist *evlist __maybe_unused,
751718c602dSAdrian Hunter 			int idx __maybe_unused,
752718c602dSAdrian Hunter 			bool per_cpu __maybe_unused)
753718c602dSAdrian Hunter {
754718c602dSAdrian Hunter }
755718c602dSAdrian Hunter 
75693edcbd9SAdrian Hunter static void __perf_evlist__munmap(struct perf_evlist *evlist, int idx)
75793edcbd9SAdrian Hunter {
75893edcbd9SAdrian Hunter 	if (evlist->mmap[idx].base != NULL) {
75993edcbd9SAdrian Hunter 		munmap(evlist->mmap[idx].base, evlist->mmap_len);
76093edcbd9SAdrian Hunter 		evlist->mmap[idx].base = NULL;
76182396986SArnaldo Carvalho de Melo 		evlist->mmap[idx].refcnt = 0;
76293edcbd9SAdrian Hunter 	}
763718c602dSAdrian Hunter 	auxtrace_mmap__munmap(&evlist->mmap[idx].auxtrace_mmap);
76493edcbd9SAdrian Hunter }
76593edcbd9SAdrian Hunter 
7667e2ed097SArnaldo Carvalho de Melo void perf_evlist__munmap(struct perf_evlist *evlist)
767f8a95309SArnaldo Carvalho de Melo {
768aece948fSArnaldo Carvalho de Melo 	int i;
769f8a95309SArnaldo Carvalho de Melo 
770983874d1SArnaldo Carvalho de Melo 	if (evlist->mmap == NULL)
771983874d1SArnaldo Carvalho de Melo 		return;
772983874d1SArnaldo Carvalho de Melo 
77393edcbd9SAdrian Hunter 	for (i = 0; i < evlist->nr_mmaps; i++)
77493edcbd9SAdrian Hunter 		__perf_evlist__munmap(evlist, i);
775aece948fSArnaldo Carvalho de Melo 
77604662523SArnaldo Carvalho de Melo 	zfree(&evlist->mmap);
777f8a95309SArnaldo Carvalho de Melo }
778f8a95309SArnaldo Carvalho de Melo 
779806fb630SArnaldo Carvalho de Melo static int perf_evlist__alloc_mmap(struct perf_evlist *evlist)
780f8a95309SArnaldo Carvalho de Melo {
781a14bb7a6SArnaldo Carvalho de Melo 	evlist->nr_mmaps = cpu_map__nr(evlist->cpus);
782ec1e7e43SSukadev Bhattiprolu 	if (cpu_map__empty(evlist->cpus))
783b3a319d5SNamhyung Kim 		evlist->nr_mmaps = thread_map__nr(evlist->threads);
784aece948fSArnaldo Carvalho de Melo 	evlist->mmap = zalloc(evlist->nr_mmaps * sizeof(struct perf_mmap));
785f8a95309SArnaldo Carvalho de Melo 	return evlist->mmap != NULL ? 0 : -ENOMEM;
786f8a95309SArnaldo Carvalho de Melo }
787f8a95309SArnaldo Carvalho de Melo 
788a8a8f3ebSAdrian Hunter struct mmap_params {
789a8a8f3ebSAdrian Hunter 	int prot;
790a8a8f3ebSAdrian Hunter 	int mask;
791718c602dSAdrian Hunter 	struct auxtrace_mmap_params auxtrace_mp;
792a8a8f3ebSAdrian Hunter };
793a8a8f3ebSAdrian Hunter 
794a8a8f3ebSAdrian Hunter static int __perf_evlist__mmap(struct perf_evlist *evlist, int idx,
795a8a8f3ebSAdrian Hunter 			       struct mmap_params *mp, int fd)
796f8a95309SArnaldo Carvalho de Melo {
79782396986SArnaldo Carvalho de Melo 	/*
79882396986SArnaldo Carvalho de Melo 	 * The last one will be done at perf_evlist__mmap_consume(), so that we
79982396986SArnaldo Carvalho de Melo 	 * make sure we don't prevent tools from consuming every last event in
80082396986SArnaldo Carvalho de Melo 	 * the ring buffer.
80182396986SArnaldo Carvalho de Melo 	 *
80282396986SArnaldo Carvalho de Melo 	 * I.e. we can get the POLLHUP meaning that the fd doesn't exist
80382396986SArnaldo Carvalho de Melo 	 * anymore, but the last events for it are still in the ring buffer,
80482396986SArnaldo Carvalho de Melo 	 * waiting to be consumed.
80582396986SArnaldo Carvalho de Melo 	 *
80682396986SArnaldo Carvalho de Melo 	 * Tools can chose to ignore this at their own discretion, but the
80782396986SArnaldo Carvalho de Melo 	 * evlist layer can't just drop it when filtering events in
80882396986SArnaldo Carvalho de Melo 	 * perf_evlist__filter_pollfd().
80982396986SArnaldo Carvalho de Melo 	 */
81082396986SArnaldo Carvalho de Melo 	evlist->mmap[idx].refcnt = 2;
811aece948fSArnaldo Carvalho de Melo 	evlist->mmap[idx].prev = 0;
812a8a8f3ebSAdrian Hunter 	evlist->mmap[idx].mask = mp->mask;
813a8a8f3ebSAdrian Hunter 	evlist->mmap[idx].base = mmap(NULL, evlist->mmap_len, mp->prot,
814f8a95309SArnaldo Carvalho de Melo 				      MAP_SHARED, fd, 0);
815301b195dSNelson Elhage 	if (evlist->mmap[idx].base == MAP_FAILED) {
81602635965SAdrian Hunter 		pr_debug2("failed to mmap perf event ring buffer, error %d\n",
81702635965SAdrian Hunter 			  errno);
818301b195dSNelson Elhage 		evlist->mmap[idx].base = NULL;
819f8a95309SArnaldo Carvalho de Melo 		return -1;
820301b195dSNelson Elhage 	}
821ad6765ddSArnaldo Carvalho de Melo 
822718c602dSAdrian Hunter 	if (auxtrace_mmap__mmap(&evlist->mmap[idx].auxtrace_mmap,
823718c602dSAdrian Hunter 				&mp->auxtrace_mp, evlist->mmap[idx].base, fd))
824718c602dSAdrian Hunter 		return -1;
825718c602dSAdrian Hunter 
826f8a95309SArnaldo Carvalho de Melo 	return 0;
827f8a95309SArnaldo Carvalho de Melo }
828f8a95309SArnaldo Carvalho de Melo 
82904e21314SAdrian Hunter static int perf_evlist__mmap_per_evsel(struct perf_evlist *evlist, int idx,
830a8a8f3ebSAdrian Hunter 				       struct mmap_params *mp, int cpu,
831a8a8f3ebSAdrian Hunter 				       int thread, int *output)
832aece948fSArnaldo Carvalho de Melo {
833aece948fSArnaldo Carvalho de Melo 	struct perf_evsel *evsel;
83404e21314SAdrian Hunter 
8350050f7aaSArnaldo Carvalho de Melo 	evlist__for_each(evlist, evsel) {
836bf8e8f4bSAdrian Hunter 		int fd;
837bf8e8f4bSAdrian Hunter 
838bf8e8f4bSAdrian Hunter 		if (evsel->system_wide && thread)
839bf8e8f4bSAdrian Hunter 			continue;
840bf8e8f4bSAdrian Hunter 
841bf8e8f4bSAdrian Hunter 		fd = FD(evsel, cpu, thread);
84204e21314SAdrian Hunter 
84304e21314SAdrian Hunter 		if (*output == -1) {
84404e21314SAdrian Hunter 			*output = fd;
845a8a8f3ebSAdrian Hunter 			if (__perf_evlist__mmap(evlist, idx, mp, *output) < 0)
84604e21314SAdrian Hunter 				return -1;
84704e21314SAdrian Hunter 		} else {
84804e21314SAdrian Hunter 			if (ioctl(fd, PERF_EVENT_IOC_SET_OUTPUT, *output) != 0)
84904e21314SAdrian Hunter 				return -1;
85082396986SArnaldo Carvalho de Melo 
85182396986SArnaldo Carvalho de Melo 			perf_evlist__mmap_get(evlist, idx);
85204e21314SAdrian Hunter 		}
85304e21314SAdrian Hunter 
854f90d194aSAdrian Hunter 		/*
855f90d194aSAdrian Hunter 		 * The system_wide flag causes a selected event to be opened
856f90d194aSAdrian Hunter 		 * always without a pid.  Consequently it will never get a
857f90d194aSAdrian Hunter 		 * POLLHUP, but it is used for tracking in combination with
858f90d194aSAdrian Hunter 		 * other events, so it should not need to be polled anyway.
859f90d194aSAdrian Hunter 		 * Therefore don't add it for polling.
860f90d194aSAdrian Hunter 		 */
861f90d194aSAdrian Hunter 		if (!evsel->system_wide &&
862f90d194aSAdrian Hunter 		    __perf_evlist__add_pollfd(evlist, fd, idx) < 0) {
86382396986SArnaldo Carvalho de Melo 			perf_evlist__mmap_put(evlist, idx);
864ad6765ddSArnaldo Carvalho de Melo 			return -1;
86582396986SArnaldo Carvalho de Melo 		}
866033fa713SArnaldo Carvalho de Melo 
8673c659eedSAdrian Hunter 		if (evsel->attr.read_format & PERF_FORMAT_ID) {
8683c659eedSAdrian Hunter 			if (perf_evlist__id_add_fd(evlist, evsel, cpu, thread,
8693c659eedSAdrian Hunter 						   fd) < 0)
87004e21314SAdrian Hunter 				return -1;
8713c659eedSAdrian Hunter 			perf_evlist__set_sid_idx(evlist, evsel, idx, cpu,
8723c659eedSAdrian Hunter 						 thread);
8733c659eedSAdrian Hunter 		}
87404e21314SAdrian Hunter 	}
87504e21314SAdrian Hunter 
87604e21314SAdrian Hunter 	return 0;
87704e21314SAdrian Hunter }
87804e21314SAdrian Hunter 
879a8a8f3ebSAdrian Hunter static int perf_evlist__mmap_per_cpu(struct perf_evlist *evlist,
880a8a8f3ebSAdrian Hunter 				     struct mmap_params *mp)
88104e21314SAdrian Hunter {
882aece948fSArnaldo Carvalho de Melo 	int cpu, thread;
883b3a319d5SNamhyung Kim 	int nr_cpus = cpu_map__nr(evlist->cpus);
884b3a319d5SNamhyung Kim 	int nr_threads = thread_map__nr(evlist->threads);
885aece948fSArnaldo Carvalho de Melo 
886e3e1a54fSAdrian Hunter 	pr_debug2("perf event ring buffer mmapped per cpu\n");
887b3a319d5SNamhyung Kim 	for (cpu = 0; cpu < nr_cpus; cpu++) {
888aece948fSArnaldo Carvalho de Melo 		int output = -1;
889aece948fSArnaldo Carvalho de Melo 
890718c602dSAdrian Hunter 		auxtrace_mmap_params__set_idx(&mp->auxtrace_mp, evlist, cpu,
891718c602dSAdrian Hunter 					      true);
892718c602dSAdrian Hunter 
893b3a319d5SNamhyung Kim 		for (thread = 0; thread < nr_threads; thread++) {
894a8a8f3ebSAdrian Hunter 			if (perf_evlist__mmap_per_evsel(evlist, cpu, mp, cpu,
895a8a8f3ebSAdrian Hunter 							thread, &output))
896aece948fSArnaldo Carvalho de Melo 				goto out_unmap;
897aece948fSArnaldo Carvalho de Melo 		}
898aece948fSArnaldo Carvalho de Melo 	}
899aece948fSArnaldo Carvalho de Melo 
900aece948fSArnaldo Carvalho de Melo 	return 0;
901aece948fSArnaldo Carvalho de Melo 
902aece948fSArnaldo Carvalho de Melo out_unmap:
90393edcbd9SAdrian Hunter 	for (cpu = 0; cpu < nr_cpus; cpu++)
90493edcbd9SAdrian Hunter 		__perf_evlist__munmap(evlist, cpu);
905aece948fSArnaldo Carvalho de Melo 	return -1;
906aece948fSArnaldo Carvalho de Melo }
907aece948fSArnaldo Carvalho de Melo 
908a8a8f3ebSAdrian Hunter static int perf_evlist__mmap_per_thread(struct perf_evlist *evlist,
909a8a8f3ebSAdrian Hunter 					struct mmap_params *mp)
910aece948fSArnaldo Carvalho de Melo {
911aece948fSArnaldo Carvalho de Melo 	int thread;
912b3a319d5SNamhyung Kim 	int nr_threads = thread_map__nr(evlist->threads);
913aece948fSArnaldo Carvalho de Melo 
914e3e1a54fSAdrian Hunter 	pr_debug2("perf event ring buffer mmapped per thread\n");
915b3a319d5SNamhyung Kim 	for (thread = 0; thread < nr_threads; thread++) {
916aece948fSArnaldo Carvalho de Melo 		int output = -1;
917aece948fSArnaldo Carvalho de Melo 
918718c602dSAdrian Hunter 		auxtrace_mmap_params__set_idx(&mp->auxtrace_mp, evlist, thread,
919718c602dSAdrian Hunter 					      false);
920718c602dSAdrian Hunter 
921a8a8f3ebSAdrian Hunter 		if (perf_evlist__mmap_per_evsel(evlist, thread, mp, 0, thread,
922a8a8f3ebSAdrian Hunter 						&output))
923aece948fSArnaldo Carvalho de Melo 			goto out_unmap;
924aece948fSArnaldo Carvalho de Melo 	}
925aece948fSArnaldo Carvalho de Melo 
926aece948fSArnaldo Carvalho de Melo 	return 0;
927aece948fSArnaldo Carvalho de Melo 
928aece948fSArnaldo Carvalho de Melo out_unmap:
92993edcbd9SAdrian Hunter 	for (thread = 0; thread < nr_threads; thread++)
93093edcbd9SAdrian Hunter 		__perf_evlist__munmap(evlist, thread);
931aece948fSArnaldo Carvalho de Melo 	return -1;
932aece948fSArnaldo Carvalho de Melo }
933aece948fSArnaldo Carvalho de Melo 
934994a1f78SJiri Olsa static size_t perf_evlist__mmap_size(unsigned long pages)
935994a1f78SJiri Olsa {
9368185e881SArnaldo Carvalho de Melo 	if (pages == UINT_MAX) {
9378185e881SArnaldo Carvalho de Melo 		int max;
9388185e881SArnaldo Carvalho de Melo 
9398185e881SArnaldo Carvalho de Melo 		if (sysctl__read_int("kernel/perf_event_mlock_kb", &max) < 0) {
9408185e881SArnaldo Carvalho de Melo 			/*
9418185e881SArnaldo Carvalho de Melo 			 * Pick a once upon a time good value, i.e. things look
9428185e881SArnaldo Carvalho de Melo 			 * strange since we can't read a sysctl value, but lets not
9438185e881SArnaldo Carvalho de Melo 			 * die yet...
9448185e881SArnaldo Carvalho de Melo 			 */
9458185e881SArnaldo Carvalho de Melo 			max = 512;
9468185e881SArnaldo Carvalho de Melo 		} else {
9478185e881SArnaldo Carvalho de Melo 			max -= (page_size / 1024);
9488185e881SArnaldo Carvalho de Melo 		}
9498185e881SArnaldo Carvalho de Melo 
9508185e881SArnaldo Carvalho de Melo 		pages = (max * 1024) / page_size;
9511be300f4SArnaldo Carvalho de Melo 		if (!is_power_of_2(pages))
9521be300f4SArnaldo Carvalho de Melo 			pages = rounddown_pow_of_two(pages);
9538185e881SArnaldo Carvalho de Melo 	} else if (!is_power_of_2(pages))
954994a1f78SJiri Olsa 		return 0;
955994a1f78SJiri Olsa 
956994a1f78SJiri Olsa 	return (pages + 1) * page_size;
957994a1f78SJiri Olsa }
958994a1f78SJiri Olsa 
95933c2dcfdSDavid Ahern static long parse_pages_arg(const char *str, unsigned long min,
96033c2dcfdSDavid Ahern 			    unsigned long max)
961994a1f78SJiri Olsa {
9622fbe4abeSAdrian Hunter 	unsigned long pages, val;
96327050f53SJiri Olsa 	static struct parse_tag tags[] = {
96427050f53SJiri Olsa 		{ .tag  = 'B', .mult = 1       },
96527050f53SJiri Olsa 		{ .tag  = 'K', .mult = 1 << 10 },
96627050f53SJiri Olsa 		{ .tag  = 'M', .mult = 1 << 20 },
96727050f53SJiri Olsa 		{ .tag  = 'G', .mult = 1 << 30 },
96827050f53SJiri Olsa 		{ .tag  = 0 },
96927050f53SJiri Olsa 	};
970994a1f78SJiri Olsa 
9718973504bSDavid Ahern 	if (str == NULL)
97233c2dcfdSDavid Ahern 		return -EINVAL;
9738973504bSDavid Ahern 
97427050f53SJiri Olsa 	val = parse_tag_value(str, tags);
9752fbe4abeSAdrian Hunter 	if (val != (unsigned long) -1) {
97627050f53SJiri Olsa 		/* we got file size value */
97727050f53SJiri Olsa 		pages = PERF_ALIGN(val, page_size) / page_size;
97827050f53SJiri Olsa 	} else {
97927050f53SJiri Olsa 		/* we got pages count value */
98027050f53SJiri Olsa 		char *eptr;
981994a1f78SJiri Olsa 		pages = strtoul(str, &eptr, 10);
98233c2dcfdSDavid Ahern 		if (*eptr != '\0')
98333c2dcfdSDavid Ahern 			return -EINVAL;
98427050f53SJiri Olsa 	}
985994a1f78SJiri Olsa 
9862bcab6c1SAdrian Hunter 	if (pages == 0 && min == 0) {
98733c2dcfdSDavid Ahern 		/* leave number of pages at 0 */
9881dbfa938SAdrian Hunter 	} else if (!is_power_of_2(pages)) {
98933c2dcfdSDavid Ahern 		/* round pages up to next power of 2 */
99091529834SArnaldo Carvalho de Melo 		pages = roundup_pow_of_two(pages);
9911dbfa938SAdrian Hunter 		if (!pages)
9921dbfa938SAdrian Hunter 			return -EINVAL;
9939639837eSDavid Ahern 		pr_info("rounding mmap pages size to %lu bytes (%lu pages)\n",
9949639837eSDavid Ahern 			pages * page_size, pages);
9952fbe4abeSAdrian Hunter 	}
9962fbe4abeSAdrian Hunter 
99733c2dcfdSDavid Ahern 	if (pages > max)
99833c2dcfdSDavid Ahern 		return -EINVAL;
99933c2dcfdSDavid Ahern 
100033c2dcfdSDavid Ahern 	return pages;
100133c2dcfdSDavid Ahern }
100233c2dcfdSDavid Ahern 
1003e9db1310SAdrian Hunter int __perf_evlist__parse_mmap_pages(unsigned int *mmap_pages, const char *str)
100433c2dcfdSDavid Ahern {
100533c2dcfdSDavid Ahern 	unsigned long max = UINT_MAX;
100633c2dcfdSDavid Ahern 	long pages;
100733c2dcfdSDavid Ahern 
1008f5ae9c42SAdrian Hunter 	if (max > SIZE_MAX / page_size)
100933c2dcfdSDavid Ahern 		max = SIZE_MAX / page_size;
101033c2dcfdSDavid Ahern 
101133c2dcfdSDavid Ahern 	pages = parse_pages_arg(str, 1, max);
101233c2dcfdSDavid Ahern 	if (pages < 0) {
101333c2dcfdSDavid Ahern 		pr_err("Invalid argument for --mmap_pages/-m\n");
1014994a1f78SJiri Olsa 		return -1;
1015994a1f78SJiri Olsa 	}
1016994a1f78SJiri Olsa 
1017994a1f78SJiri Olsa 	*mmap_pages = pages;
1018994a1f78SJiri Olsa 	return 0;
1019994a1f78SJiri Olsa }
1020994a1f78SJiri Olsa 
1021e9db1310SAdrian Hunter int perf_evlist__parse_mmap_pages(const struct option *opt, const char *str,
1022e9db1310SAdrian Hunter 				  int unset __maybe_unused)
1023e9db1310SAdrian Hunter {
1024e9db1310SAdrian Hunter 	return __perf_evlist__parse_mmap_pages(opt->value, str);
1025e9db1310SAdrian Hunter }
1026e9db1310SAdrian Hunter 
1027c83fa7f2SAdrian Hunter /**
1028718c602dSAdrian Hunter  * perf_evlist__mmap_ex - Create mmaps to receive events.
1029c83fa7f2SAdrian Hunter  * @evlist: list of events
1030c83fa7f2SAdrian Hunter  * @pages: map length in pages
1031c83fa7f2SAdrian Hunter  * @overwrite: overwrite older events?
1032718c602dSAdrian Hunter  * @auxtrace_pages - auxtrace map length in pages
1033718c602dSAdrian Hunter  * @auxtrace_overwrite - overwrite older auxtrace data?
1034f8a95309SArnaldo Carvalho de Melo  *
1035c83fa7f2SAdrian Hunter  * If @overwrite is %false the user needs to signal event consumption using
1036c83fa7f2SAdrian Hunter  * perf_mmap__write_tail().  Using perf_evlist__mmap_read() does this
1037c83fa7f2SAdrian Hunter  * automatically.
1038f8a95309SArnaldo Carvalho de Melo  *
1039718c602dSAdrian Hunter  * Similarly, if @auxtrace_overwrite is %false the user needs to signal data
1040718c602dSAdrian Hunter  * consumption using auxtrace_mmap__write_tail().
1041718c602dSAdrian Hunter  *
1042c83fa7f2SAdrian Hunter  * Return: %0 on success, negative error code otherwise.
1043f8a95309SArnaldo Carvalho de Melo  */
1044718c602dSAdrian Hunter int perf_evlist__mmap_ex(struct perf_evlist *evlist, unsigned int pages,
1045718c602dSAdrian Hunter 			 bool overwrite, unsigned int auxtrace_pages,
1046718c602dSAdrian Hunter 			 bool auxtrace_overwrite)
1047f8a95309SArnaldo Carvalho de Melo {
1048aece948fSArnaldo Carvalho de Melo 	struct perf_evsel *evsel;
10497e2ed097SArnaldo Carvalho de Melo 	const struct cpu_map *cpus = evlist->cpus;
10507e2ed097SArnaldo Carvalho de Melo 	const struct thread_map *threads = evlist->threads;
1051a8a8f3ebSAdrian Hunter 	struct mmap_params mp = {
1052a8a8f3ebSAdrian Hunter 		.prot = PROT_READ | (overwrite ? 0 : PROT_WRITE),
1053a8a8f3ebSAdrian Hunter 	};
105450a682ceSArnaldo Carvalho de Melo 
10557e2ed097SArnaldo Carvalho de Melo 	if (evlist->mmap == NULL && perf_evlist__alloc_mmap(evlist) < 0)
1056f8a95309SArnaldo Carvalho de Melo 		return -ENOMEM;
1057f8a95309SArnaldo Carvalho de Melo 
10581b85337dSArnaldo Carvalho de Melo 	if (evlist->pollfd.entries == NULL && perf_evlist__alloc_pollfd(evlist) < 0)
1059f8a95309SArnaldo Carvalho de Melo 		return -ENOMEM;
1060f8a95309SArnaldo Carvalho de Melo 
1061f8a95309SArnaldo Carvalho de Melo 	evlist->overwrite = overwrite;
1062994a1f78SJiri Olsa 	evlist->mmap_len = perf_evlist__mmap_size(pages);
10632af68ef5SAdrian Hunter 	pr_debug("mmap size %zuB\n", evlist->mmap_len);
1064a8a8f3ebSAdrian Hunter 	mp.mask = evlist->mmap_len - page_size - 1;
1065f8a95309SArnaldo Carvalho de Melo 
1066718c602dSAdrian Hunter 	auxtrace_mmap_params__init(&mp.auxtrace_mp, evlist->mmap_len,
1067718c602dSAdrian Hunter 				   auxtrace_pages, auxtrace_overwrite);
1068718c602dSAdrian Hunter 
10690050f7aaSArnaldo Carvalho de Melo 	evlist__for_each(evlist, evsel) {
1070f8a95309SArnaldo Carvalho de Melo 		if ((evsel->attr.read_format & PERF_FORMAT_ID) &&
1071a91e5431SArnaldo Carvalho de Melo 		    evsel->sample_id == NULL &&
1072a14bb7a6SArnaldo Carvalho de Melo 		    perf_evsel__alloc_id(evsel, cpu_map__nr(cpus), threads->nr) < 0)
1073f8a95309SArnaldo Carvalho de Melo 			return -ENOMEM;
1074f8a95309SArnaldo Carvalho de Melo 	}
1075f8a95309SArnaldo Carvalho de Melo 
1076ec1e7e43SSukadev Bhattiprolu 	if (cpu_map__empty(cpus))
1077a8a8f3ebSAdrian Hunter 		return perf_evlist__mmap_per_thread(evlist, &mp);
1078f8a95309SArnaldo Carvalho de Melo 
1079a8a8f3ebSAdrian Hunter 	return perf_evlist__mmap_per_cpu(evlist, &mp);
1080f8a95309SArnaldo Carvalho de Melo }
10817e2ed097SArnaldo Carvalho de Melo 
1082718c602dSAdrian Hunter int perf_evlist__mmap(struct perf_evlist *evlist, unsigned int pages,
1083718c602dSAdrian Hunter 		      bool overwrite)
1084718c602dSAdrian Hunter {
1085718c602dSAdrian Hunter 	return perf_evlist__mmap_ex(evlist, pages, overwrite, 0, false);
1086718c602dSAdrian Hunter }
1087718c602dSAdrian Hunter 
1088602ad878SArnaldo Carvalho de Melo int perf_evlist__create_maps(struct perf_evlist *evlist, struct target *target)
10897e2ed097SArnaldo Carvalho de Melo {
1090b809ac10SNamhyung Kim 	evlist->threads = thread_map__new_str(target->pid, target->tid,
1091b809ac10SNamhyung Kim 					      target->uid);
10927e2ed097SArnaldo Carvalho de Melo 
10937e2ed097SArnaldo Carvalho de Melo 	if (evlist->threads == NULL)
10947e2ed097SArnaldo Carvalho de Melo 		return -1;
10957e2ed097SArnaldo Carvalho de Melo 
10969c105fbcSDongsheng Yang 	if (target__uses_dummy_map(target))
1097d1cb9fceSNamhyung Kim 		evlist->cpus = cpu_map__dummy_new();
1098879d77d0SNamhyung Kim 	else
1099879d77d0SNamhyung Kim 		evlist->cpus = cpu_map__new(target->cpu_list);
11007e2ed097SArnaldo Carvalho de Melo 
11017e2ed097SArnaldo Carvalho de Melo 	if (evlist->cpus == NULL)
11027e2ed097SArnaldo Carvalho de Melo 		goto out_delete_threads;
11037e2ed097SArnaldo Carvalho de Melo 
11047e2ed097SArnaldo Carvalho de Melo 	return 0;
11057e2ed097SArnaldo Carvalho de Melo 
11067e2ed097SArnaldo Carvalho de Melo out_delete_threads:
11077e2ed097SArnaldo Carvalho de Melo 	thread_map__delete(evlist->threads);
1108b2e19a93SYasser Shalabi 	evlist->threads = NULL;
11097e2ed097SArnaldo Carvalho de Melo 	return -1;
11107e2ed097SArnaldo Carvalho de Melo }
11117e2ed097SArnaldo Carvalho de Melo 
111223d4aad4SArnaldo Carvalho de Melo int perf_evlist__apply_filters(struct perf_evlist *evlist, struct perf_evsel **err_evsel)
11130a102479SFrederic Weisbecker {
11140a102479SFrederic Weisbecker 	struct perf_evsel *evsel;
1115745cefc5SArnaldo Carvalho de Melo 	int err = 0;
1116745cefc5SArnaldo Carvalho de Melo 	const int ncpus = cpu_map__nr(evlist->cpus),
1117b3a319d5SNamhyung Kim 		  nthreads = thread_map__nr(evlist->threads);
11180a102479SFrederic Weisbecker 
11190050f7aaSArnaldo Carvalho de Melo 	evlist__for_each(evlist, evsel) {
1120745cefc5SArnaldo Carvalho de Melo 		if (evsel->filter == NULL)
11210a102479SFrederic Weisbecker 			continue;
1122745cefc5SArnaldo Carvalho de Melo 
1123745cefc5SArnaldo Carvalho de Melo 		err = perf_evsel__set_filter(evsel, ncpus, nthreads, evsel->filter);
112423d4aad4SArnaldo Carvalho de Melo 		if (err) {
112523d4aad4SArnaldo Carvalho de Melo 			*err_evsel = evsel;
1126745cefc5SArnaldo Carvalho de Melo 			break;
11270a102479SFrederic Weisbecker 		}
112823d4aad4SArnaldo Carvalho de Melo 	}
11290a102479SFrederic Weisbecker 
1130745cefc5SArnaldo Carvalho de Melo 	return err;
1131745cefc5SArnaldo Carvalho de Melo }
1132745cefc5SArnaldo Carvalho de Melo 
1133745cefc5SArnaldo Carvalho de Melo int perf_evlist__set_filter(struct perf_evlist *evlist, const char *filter)
1134745cefc5SArnaldo Carvalho de Melo {
1135745cefc5SArnaldo Carvalho de Melo 	struct perf_evsel *evsel;
1136745cefc5SArnaldo Carvalho de Melo 	int err = 0;
1137745cefc5SArnaldo Carvalho de Melo 	const int ncpus = cpu_map__nr(evlist->cpus),
1138b3a319d5SNamhyung Kim 		  nthreads = thread_map__nr(evlist->threads);
1139745cefc5SArnaldo Carvalho de Melo 
11400050f7aaSArnaldo Carvalho de Melo 	evlist__for_each(evlist, evsel) {
1141745cefc5SArnaldo Carvalho de Melo 		err = perf_evsel__set_filter(evsel, ncpus, nthreads, filter);
1142745cefc5SArnaldo Carvalho de Melo 		if (err)
1143745cefc5SArnaldo Carvalho de Melo 			break;
1144745cefc5SArnaldo Carvalho de Melo 	}
1145745cefc5SArnaldo Carvalho de Melo 
1146745cefc5SArnaldo Carvalho de Melo 	return err;
11470a102479SFrederic Weisbecker }
114874429964SFrederic Weisbecker 
1149be199adaSArnaldo Carvalho de Melo int perf_evlist__set_filter_pids(struct perf_evlist *evlist, size_t npids, pid_t *pids)
1150cfd70a26SArnaldo Carvalho de Melo {
1151cfd70a26SArnaldo Carvalho de Melo 	char *filter;
1152be199adaSArnaldo Carvalho de Melo 	int ret = -1;
1153be199adaSArnaldo Carvalho de Melo 	size_t i;
1154cfd70a26SArnaldo Carvalho de Melo 
1155be199adaSArnaldo Carvalho de Melo 	for (i = 0; i < npids; ++i) {
1156be199adaSArnaldo Carvalho de Melo 		if (i == 0) {
1157be199adaSArnaldo Carvalho de Melo 			if (asprintf(&filter, "common_pid != %d", pids[i]) < 0)
1158cfd70a26SArnaldo Carvalho de Melo 				return -1;
1159be199adaSArnaldo Carvalho de Melo 		} else {
1160be199adaSArnaldo Carvalho de Melo 			char *tmp;
1161be199adaSArnaldo Carvalho de Melo 
1162be199adaSArnaldo Carvalho de Melo 			if (asprintf(&tmp, "%s && common_pid != %d", filter, pids[i]) < 0)
1163be199adaSArnaldo Carvalho de Melo 				goto out_free;
1164be199adaSArnaldo Carvalho de Melo 
1165be199adaSArnaldo Carvalho de Melo 			free(filter);
1166be199adaSArnaldo Carvalho de Melo 			filter = tmp;
1167be199adaSArnaldo Carvalho de Melo 		}
1168be199adaSArnaldo Carvalho de Melo 	}
1169cfd70a26SArnaldo Carvalho de Melo 
1170cfd70a26SArnaldo Carvalho de Melo 	ret = perf_evlist__set_filter(evlist, filter);
1171be199adaSArnaldo Carvalho de Melo out_free:
1172cfd70a26SArnaldo Carvalho de Melo 	free(filter);
1173cfd70a26SArnaldo Carvalho de Melo 	return ret;
1174cfd70a26SArnaldo Carvalho de Melo }
1175cfd70a26SArnaldo Carvalho de Melo 
1176be199adaSArnaldo Carvalho de Melo int perf_evlist__set_filter_pid(struct perf_evlist *evlist, pid_t pid)
1177be199adaSArnaldo Carvalho de Melo {
1178be199adaSArnaldo Carvalho de Melo 	return perf_evlist__set_filter_pids(evlist, 1, &pid);
1179be199adaSArnaldo Carvalho de Melo }
1180be199adaSArnaldo Carvalho de Melo 
11810c21f736SArnaldo Carvalho de Melo bool perf_evlist__valid_sample_type(struct perf_evlist *evlist)
118274429964SFrederic Weisbecker {
118375562573SAdrian Hunter 	struct perf_evsel *pos;
1184c2a70653SArnaldo Carvalho de Melo 
118575562573SAdrian Hunter 	if (evlist->nr_entries == 1)
118675562573SAdrian Hunter 		return true;
118775562573SAdrian Hunter 
118875562573SAdrian Hunter 	if (evlist->id_pos < 0 || evlist->is_pos < 0)
118975562573SAdrian Hunter 		return false;
119075562573SAdrian Hunter 
11910050f7aaSArnaldo Carvalho de Melo 	evlist__for_each(evlist, pos) {
119275562573SAdrian Hunter 		if (pos->id_pos != evlist->id_pos ||
119375562573SAdrian Hunter 		    pos->is_pos != evlist->is_pos)
1194c2a70653SArnaldo Carvalho de Melo 			return false;
119574429964SFrederic Weisbecker 	}
119674429964SFrederic Weisbecker 
1197c2a70653SArnaldo Carvalho de Melo 	return true;
1198c2a70653SArnaldo Carvalho de Melo }
1199c2a70653SArnaldo Carvalho de Melo 
120075562573SAdrian Hunter u64 __perf_evlist__combined_sample_type(struct perf_evlist *evlist)
1201c2a70653SArnaldo Carvalho de Melo {
120275562573SAdrian Hunter 	struct perf_evsel *evsel;
120375562573SAdrian Hunter 
120475562573SAdrian Hunter 	if (evlist->combined_sample_type)
120575562573SAdrian Hunter 		return evlist->combined_sample_type;
120675562573SAdrian Hunter 
12070050f7aaSArnaldo Carvalho de Melo 	evlist__for_each(evlist, evsel)
120875562573SAdrian Hunter 		evlist->combined_sample_type |= evsel->attr.sample_type;
120975562573SAdrian Hunter 
121075562573SAdrian Hunter 	return evlist->combined_sample_type;
121175562573SAdrian Hunter }
121275562573SAdrian Hunter 
121375562573SAdrian Hunter u64 perf_evlist__combined_sample_type(struct perf_evlist *evlist)
121475562573SAdrian Hunter {
121575562573SAdrian Hunter 	evlist->combined_sample_type = 0;
121675562573SAdrian Hunter 	return __perf_evlist__combined_sample_type(evlist);
1217c2a70653SArnaldo Carvalho de Melo }
1218c2a70653SArnaldo Carvalho de Melo 
12199ede473cSJiri Olsa bool perf_evlist__valid_read_format(struct perf_evlist *evlist)
12209ede473cSJiri Olsa {
12219ede473cSJiri Olsa 	struct perf_evsel *first = perf_evlist__first(evlist), *pos = first;
12229ede473cSJiri Olsa 	u64 read_format = first->attr.read_format;
12239ede473cSJiri Olsa 	u64 sample_type = first->attr.sample_type;
12249ede473cSJiri Olsa 
12250050f7aaSArnaldo Carvalho de Melo 	evlist__for_each(evlist, pos) {
12269ede473cSJiri Olsa 		if (read_format != pos->attr.read_format)
12279ede473cSJiri Olsa 			return false;
12289ede473cSJiri Olsa 	}
12299ede473cSJiri Olsa 
12309ede473cSJiri Olsa 	/* PERF_SAMPLE_READ imples PERF_FORMAT_ID. */
12319ede473cSJiri Olsa 	if ((sample_type & PERF_SAMPLE_READ) &&
12329ede473cSJiri Olsa 	    !(read_format & PERF_FORMAT_ID)) {
12339ede473cSJiri Olsa 		return false;
12349ede473cSJiri Olsa 	}
12359ede473cSJiri Olsa 
12369ede473cSJiri Olsa 	return true;
12379ede473cSJiri Olsa }
12389ede473cSJiri Olsa 
12399ede473cSJiri Olsa u64 perf_evlist__read_format(struct perf_evlist *evlist)
12409ede473cSJiri Olsa {
12419ede473cSJiri Olsa 	struct perf_evsel *first = perf_evlist__first(evlist);
12429ede473cSJiri Olsa 	return first->attr.read_format;
12439ede473cSJiri Olsa }
12449ede473cSJiri Olsa 
12450c21f736SArnaldo Carvalho de Melo u16 perf_evlist__id_hdr_size(struct perf_evlist *evlist)
124681e36bffSArnaldo Carvalho de Melo {
12470c21f736SArnaldo Carvalho de Melo 	struct perf_evsel *first = perf_evlist__first(evlist);
124881e36bffSArnaldo Carvalho de Melo 	struct perf_sample *data;
124981e36bffSArnaldo Carvalho de Melo 	u64 sample_type;
125081e36bffSArnaldo Carvalho de Melo 	u16 size = 0;
125181e36bffSArnaldo Carvalho de Melo 
125281e36bffSArnaldo Carvalho de Melo 	if (!first->attr.sample_id_all)
125381e36bffSArnaldo Carvalho de Melo 		goto out;
125481e36bffSArnaldo Carvalho de Melo 
125581e36bffSArnaldo Carvalho de Melo 	sample_type = first->attr.sample_type;
125681e36bffSArnaldo Carvalho de Melo 
125781e36bffSArnaldo Carvalho de Melo 	if (sample_type & PERF_SAMPLE_TID)
125881e36bffSArnaldo Carvalho de Melo 		size += sizeof(data->tid) * 2;
125981e36bffSArnaldo Carvalho de Melo 
126081e36bffSArnaldo Carvalho de Melo        if (sample_type & PERF_SAMPLE_TIME)
126181e36bffSArnaldo Carvalho de Melo 		size += sizeof(data->time);
126281e36bffSArnaldo Carvalho de Melo 
126381e36bffSArnaldo Carvalho de Melo 	if (sample_type & PERF_SAMPLE_ID)
126481e36bffSArnaldo Carvalho de Melo 		size += sizeof(data->id);
126581e36bffSArnaldo Carvalho de Melo 
126681e36bffSArnaldo Carvalho de Melo 	if (sample_type & PERF_SAMPLE_STREAM_ID)
126781e36bffSArnaldo Carvalho de Melo 		size += sizeof(data->stream_id);
126881e36bffSArnaldo Carvalho de Melo 
126981e36bffSArnaldo Carvalho de Melo 	if (sample_type & PERF_SAMPLE_CPU)
127081e36bffSArnaldo Carvalho de Melo 		size += sizeof(data->cpu) * 2;
127175562573SAdrian Hunter 
127275562573SAdrian Hunter 	if (sample_type & PERF_SAMPLE_IDENTIFIER)
127375562573SAdrian Hunter 		size += sizeof(data->id);
127481e36bffSArnaldo Carvalho de Melo out:
127581e36bffSArnaldo Carvalho de Melo 	return size;
127681e36bffSArnaldo Carvalho de Melo }
127781e36bffSArnaldo Carvalho de Melo 
12780c21f736SArnaldo Carvalho de Melo bool perf_evlist__valid_sample_id_all(struct perf_evlist *evlist)
1279c2a70653SArnaldo Carvalho de Melo {
12800c21f736SArnaldo Carvalho de Melo 	struct perf_evsel *first = perf_evlist__first(evlist), *pos = first;
1281c2a70653SArnaldo Carvalho de Melo 
12820050f7aaSArnaldo Carvalho de Melo 	evlist__for_each_continue(evlist, pos) {
1283c2a70653SArnaldo Carvalho de Melo 		if (first->attr.sample_id_all != pos->attr.sample_id_all)
1284c2a70653SArnaldo Carvalho de Melo 			return false;
1285c2a70653SArnaldo Carvalho de Melo 	}
1286c2a70653SArnaldo Carvalho de Melo 
1287c2a70653SArnaldo Carvalho de Melo 	return true;
128874429964SFrederic Weisbecker }
128974429964SFrederic Weisbecker 
12900c21f736SArnaldo Carvalho de Melo bool perf_evlist__sample_id_all(struct perf_evlist *evlist)
129174429964SFrederic Weisbecker {
12920c21f736SArnaldo Carvalho de Melo 	struct perf_evsel *first = perf_evlist__first(evlist);
1293c2a70653SArnaldo Carvalho de Melo 	return first->attr.sample_id_all;
129474429964SFrederic Weisbecker }
129581cce8deSArnaldo Carvalho de Melo 
129681cce8deSArnaldo Carvalho de Melo void perf_evlist__set_selected(struct perf_evlist *evlist,
129781cce8deSArnaldo Carvalho de Melo 			       struct perf_evsel *evsel)
129881cce8deSArnaldo Carvalho de Melo {
129981cce8deSArnaldo Carvalho de Melo 	evlist->selected = evsel;
130081cce8deSArnaldo Carvalho de Melo }
1301727ab04eSArnaldo Carvalho de Melo 
1302a74b4b66SNamhyung Kim void perf_evlist__close(struct perf_evlist *evlist)
1303a74b4b66SNamhyung Kim {
1304a74b4b66SNamhyung Kim 	struct perf_evsel *evsel;
1305a74b4b66SNamhyung Kim 	int ncpus = cpu_map__nr(evlist->cpus);
1306a74b4b66SNamhyung Kim 	int nthreads = thread_map__nr(evlist->threads);
13078ad9219eSStephane Eranian 	int n;
1308a74b4b66SNamhyung Kim 
13098ad9219eSStephane Eranian 	evlist__for_each_reverse(evlist, evsel) {
13108ad9219eSStephane Eranian 		n = evsel->cpus ? evsel->cpus->nr : ncpus;
13118ad9219eSStephane Eranian 		perf_evsel__close(evsel, n, nthreads);
13128ad9219eSStephane Eranian 	}
1313a74b4b66SNamhyung Kim }
1314a74b4b66SNamhyung Kim 
13154112eb18SArnaldo Carvalho de Melo static int perf_evlist__create_syswide_maps(struct perf_evlist *evlist)
13164112eb18SArnaldo Carvalho de Melo {
13174112eb18SArnaldo Carvalho de Melo 	int err = -ENOMEM;
13184112eb18SArnaldo Carvalho de Melo 
13194112eb18SArnaldo Carvalho de Melo 	/*
13204112eb18SArnaldo Carvalho de Melo 	 * Try reading /sys/devices/system/cpu/online to get
13214112eb18SArnaldo Carvalho de Melo 	 * an all cpus map.
13224112eb18SArnaldo Carvalho de Melo 	 *
13234112eb18SArnaldo Carvalho de Melo 	 * FIXME: -ENOMEM is the best we can do here, the cpu_map
13244112eb18SArnaldo Carvalho de Melo 	 * code needs an overhaul to properly forward the
13254112eb18SArnaldo Carvalho de Melo 	 * error, and we may not want to do that fallback to a
13264112eb18SArnaldo Carvalho de Melo 	 * default cpu identity map :-\
13274112eb18SArnaldo Carvalho de Melo 	 */
13284112eb18SArnaldo Carvalho de Melo 	evlist->cpus = cpu_map__new(NULL);
13294112eb18SArnaldo Carvalho de Melo 	if (evlist->cpus == NULL)
13304112eb18SArnaldo Carvalho de Melo 		goto out;
13314112eb18SArnaldo Carvalho de Melo 
13324112eb18SArnaldo Carvalho de Melo 	evlist->threads = thread_map__new_dummy();
13334112eb18SArnaldo Carvalho de Melo 	if (evlist->threads == NULL)
13344112eb18SArnaldo Carvalho de Melo 		goto out_free_cpus;
13354112eb18SArnaldo Carvalho de Melo 
13364112eb18SArnaldo Carvalho de Melo 	err = 0;
13374112eb18SArnaldo Carvalho de Melo out:
13384112eb18SArnaldo Carvalho de Melo 	return err;
13394112eb18SArnaldo Carvalho de Melo out_free_cpus:
13404112eb18SArnaldo Carvalho de Melo 	cpu_map__delete(evlist->cpus);
13414112eb18SArnaldo Carvalho de Melo 	evlist->cpus = NULL;
13424112eb18SArnaldo Carvalho de Melo 	goto out;
13434112eb18SArnaldo Carvalho de Melo }
13444112eb18SArnaldo Carvalho de Melo 
13456a4bb04cSJiri Olsa int perf_evlist__open(struct perf_evlist *evlist)
1346727ab04eSArnaldo Carvalho de Melo {
13476a4bb04cSJiri Olsa 	struct perf_evsel *evsel;
1348a74b4b66SNamhyung Kim 	int err;
1349727ab04eSArnaldo Carvalho de Melo 
13504112eb18SArnaldo Carvalho de Melo 	/*
13514112eb18SArnaldo Carvalho de Melo 	 * Default: one fd per CPU, all threads, aka systemwide
13524112eb18SArnaldo Carvalho de Melo 	 * as sys_perf_event_open(cpu = -1, thread = -1) is EINVAL
13534112eb18SArnaldo Carvalho de Melo 	 */
13544112eb18SArnaldo Carvalho de Melo 	if (evlist->threads == NULL && evlist->cpus == NULL) {
13554112eb18SArnaldo Carvalho de Melo 		err = perf_evlist__create_syswide_maps(evlist);
13564112eb18SArnaldo Carvalho de Melo 		if (err < 0)
13574112eb18SArnaldo Carvalho de Melo 			goto out_err;
13584112eb18SArnaldo Carvalho de Melo 	}
13594112eb18SArnaldo Carvalho de Melo 
1360733cd2feSAdrian Hunter 	perf_evlist__update_id_pos(evlist);
1361733cd2feSAdrian Hunter 
13620050f7aaSArnaldo Carvalho de Melo 	evlist__for_each(evlist, evsel) {
13636a4bb04cSJiri Olsa 		err = perf_evsel__open(evsel, evlist->cpus, evlist->threads);
1364727ab04eSArnaldo Carvalho de Melo 		if (err < 0)
1365727ab04eSArnaldo Carvalho de Melo 			goto out_err;
1366727ab04eSArnaldo Carvalho de Melo 	}
1367727ab04eSArnaldo Carvalho de Melo 
1368727ab04eSArnaldo Carvalho de Melo 	return 0;
1369727ab04eSArnaldo Carvalho de Melo out_err:
1370a74b4b66SNamhyung Kim 	perf_evlist__close(evlist);
137141c21a68SNamhyung Kim 	errno = -err;
1372727ab04eSArnaldo Carvalho de Melo 	return err;
1373727ab04eSArnaldo Carvalho de Melo }
137435b9d88eSArnaldo Carvalho de Melo 
1375602ad878SArnaldo Carvalho de Melo int perf_evlist__prepare_workload(struct perf_evlist *evlist, struct target *target,
137655e162eaSNamhyung Kim 				  const char *argv[], bool pipe_output,
1377735f7e0bSArnaldo Carvalho de Melo 				  void (*exec_error)(int signo, siginfo_t *info, void *ucontext))
137835b9d88eSArnaldo Carvalho de Melo {
137935b9d88eSArnaldo Carvalho de Melo 	int child_ready_pipe[2], go_pipe[2];
138035b9d88eSArnaldo Carvalho de Melo 	char bf;
138135b9d88eSArnaldo Carvalho de Melo 
138235b9d88eSArnaldo Carvalho de Melo 	if (pipe(child_ready_pipe) < 0) {
138335b9d88eSArnaldo Carvalho de Melo 		perror("failed to create 'ready' pipe");
138435b9d88eSArnaldo Carvalho de Melo 		return -1;
138535b9d88eSArnaldo Carvalho de Melo 	}
138635b9d88eSArnaldo Carvalho de Melo 
138735b9d88eSArnaldo Carvalho de Melo 	if (pipe(go_pipe) < 0) {
138835b9d88eSArnaldo Carvalho de Melo 		perror("failed to create 'go' pipe");
138935b9d88eSArnaldo Carvalho de Melo 		goto out_close_ready_pipe;
139035b9d88eSArnaldo Carvalho de Melo 	}
139135b9d88eSArnaldo Carvalho de Melo 
139235b9d88eSArnaldo Carvalho de Melo 	evlist->workload.pid = fork();
139335b9d88eSArnaldo Carvalho de Melo 	if (evlist->workload.pid < 0) {
139435b9d88eSArnaldo Carvalho de Melo 		perror("failed to fork");
139535b9d88eSArnaldo Carvalho de Melo 		goto out_close_pipes;
139635b9d88eSArnaldo Carvalho de Melo 	}
139735b9d88eSArnaldo Carvalho de Melo 
139835b9d88eSArnaldo Carvalho de Melo 	if (!evlist->workload.pid) {
13995f1c4225SArnaldo Carvalho de Melo 		int ret;
14005f1c4225SArnaldo Carvalho de Melo 
1401119fa3c9SNamhyung Kim 		if (pipe_output)
140235b9d88eSArnaldo Carvalho de Melo 			dup2(2, 1);
140335b9d88eSArnaldo Carvalho de Melo 
14040817df08SDavid Ahern 		signal(SIGTERM, SIG_DFL);
14050817df08SDavid Ahern 
140635b9d88eSArnaldo Carvalho de Melo 		close(child_ready_pipe[0]);
140735b9d88eSArnaldo Carvalho de Melo 		close(go_pipe[1]);
140835b9d88eSArnaldo Carvalho de Melo 		fcntl(go_pipe[0], F_SETFD, FD_CLOEXEC);
140935b9d88eSArnaldo Carvalho de Melo 
141035b9d88eSArnaldo Carvalho de Melo 		/*
141135b9d88eSArnaldo Carvalho de Melo 		 * Tell the parent we're ready to go
141235b9d88eSArnaldo Carvalho de Melo 		 */
141335b9d88eSArnaldo Carvalho de Melo 		close(child_ready_pipe[1]);
141435b9d88eSArnaldo Carvalho de Melo 
141535b9d88eSArnaldo Carvalho de Melo 		/*
141635b9d88eSArnaldo Carvalho de Melo 		 * Wait until the parent tells us to go.
141735b9d88eSArnaldo Carvalho de Melo 		 */
14185f1c4225SArnaldo Carvalho de Melo 		ret = read(go_pipe[0], &bf, 1);
14195f1c4225SArnaldo Carvalho de Melo 		/*
14205f1c4225SArnaldo Carvalho de Melo 		 * The parent will ask for the execvp() to be performed by
14215f1c4225SArnaldo Carvalho de Melo 		 * writing exactly one byte, in workload.cork_fd, usually via
14225f1c4225SArnaldo Carvalho de Melo 		 * perf_evlist__start_workload().
14235f1c4225SArnaldo Carvalho de Melo 		 *
142420f86fc1SArnaldo Carvalho de Melo 		 * For cancelling the workload without actually running it,
14255f1c4225SArnaldo Carvalho de Melo 		 * the parent will just close workload.cork_fd, without writing
14265f1c4225SArnaldo Carvalho de Melo 		 * anything, i.e. read will return zero and we just exit()
14275f1c4225SArnaldo Carvalho de Melo 		 * here.
14285f1c4225SArnaldo Carvalho de Melo 		 */
14295f1c4225SArnaldo Carvalho de Melo 		if (ret != 1) {
14305f1c4225SArnaldo Carvalho de Melo 			if (ret == -1)
143135b9d88eSArnaldo Carvalho de Melo 				perror("unable to read pipe");
14325f1c4225SArnaldo Carvalho de Melo 			exit(ret);
14335f1c4225SArnaldo Carvalho de Melo 		}
143435b9d88eSArnaldo Carvalho de Melo 
143535b9d88eSArnaldo Carvalho de Melo 		execvp(argv[0], (char **)argv);
143635b9d88eSArnaldo Carvalho de Melo 
1437735f7e0bSArnaldo Carvalho de Melo 		if (exec_error) {
1438f33cbe72SArnaldo Carvalho de Melo 			union sigval val;
1439f33cbe72SArnaldo Carvalho de Melo 
1440f33cbe72SArnaldo Carvalho de Melo 			val.sival_int = errno;
1441f33cbe72SArnaldo Carvalho de Melo 			if (sigqueue(getppid(), SIGUSR1, val))
144235b9d88eSArnaldo Carvalho de Melo 				perror(argv[0]);
1443f33cbe72SArnaldo Carvalho de Melo 		} else
1444f33cbe72SArnaldo Carvalho de Melo 			perror(argv[0]);
144535b9d88eSArnaldo Carvalho de Melo 		exit(-1);
144635b9d88eSArnaldo Carvalho de Melo 	}
144735b9d88eSArnaldo Carvalho de Melo 
1448735f7e0bSArnaldo Carvalho de Melo 	if (exec_error) {
1449735f7e0bSArnaldo Carvalho de Melo 		struct sigaction act = {
1450735f7e0bSArnaldo Carvalho de Melo 			.sa_flags     = SA_SIGINFO,
1451735f7e0bSArnaldo Carvalho de Melo 			.sa_sigaction = exec_error,
1452735f7e0bSArnaldo Carvalho de Melo 		};
1453735f7e0bSArnaldo Carvalho de Melo 		sigaction(SIGUSR1, &act, NULL);
1454735f7e0bSArnaldo Carvalho de Melo 	}
1455735f7e0bSArnaldo Carvalho de Melo 
14561aaf63b1SArnaldo Carvalho de Melo 	if (target__none(target)) {
14571aaf63b1SArnaldo Carvalho de Melo 		if (evlist->threads == NULL) {
14581aaf63b1SArnaldo Carvalho de Melo 			fprintf(stderr, "FATAL: evlist->threads need to be set at this point (%s:%d).\n",
14591aaf63b1SArnaldo Carvalho de Melo 				__func__, __LINE__);
14601aaf63b1SArnaldo Carvalho de Melo 			goto out_close_pipes;
14611aaf63b1SArnaldo Carvalho de Melo 		}
146235b9d88eSArnaldo Carvalho de Melo 		evlist->threads->map[0] = evlist->workload.pid;
14631aaf63b1SArnaldo Carvalho de Melo 	}
146435b9d88eSArnaldo Carvalho de Melo 
146535b9d88eSArnaldo Carvalho de Melo 	close(child_ready_pipe[1]);
146635b9d88eSArnaldo Carvalho de Melo 	close(go_pipe[0]);
146735b9d88eSArnaldo Carvalho de Melo 	/*
146835b9d88eSArnaldo Carvalho de Melo 	 * wait for child to settle
146935b9d88eSArnaldo Carvalho de Melo 	 */
147035b9d88eSArnaldo Carvalho de Melo 	if (read(child_ready_pipe[0], &bf, 1) == -1) {
147135b9d88eSArnaldo Carvalho de Melo 		perror("unable to read pipe");
147235b9d88eSArnaldo Carvalho de Melo 		goto out_close_pipes;
147335b9d88eSArnaldo Carvalho de Melo 	}
147435b9d88eSArnaldo Carvalho de Melo 
1475bcf3145fSNamhyung Kim 	fcntl(go_pipe[1], F_SETFD, FD_CLOEXEC);
147635b9d88eSArnaldo Carvalho de Melo 	evlist->workload.cork_fd = go_pipe[1];
147735b9d88eSArnaldo Carvalho de Melo 	close(child_ready_pipe[0]);
147835b9d88eSArnaldo Carvalho de Melo 	return 0;
147935b9d88eSArnaldo Carvalho de Melo 
148035b9d88eSArnaldo Carvalho de Melo out_close_pipes:
148135b9d88eSArnaldo Carvalho de Melo 	close(go_pipe[0]);
148235b9d88eSArnaldo Carvalho de Melo 	close(go_pipe[1]);
148335b9d88eSArnaldo Carvalho de Melo out_close_ready_pipe:
148435b9d88eSArnaldo Carvalho de Melo 	close(child_ready_pipe[0]);
148535b9d88eSArnaldo Carvalho de Melo 	close(child_ready_pipe[1]);
148635b9d88eSArnaldo Carvalho de Melo 	return -1;
148735b9d88eSArnaldo Carvalho de Melo }
148835b9d88eSArnaldo Carvalho de Melo 
148935b9d88eSArnaldo Carvalho de Melo int perf_evlist__start_workload(struct perf_evlist *evlist)
149035b9d88eSArnaldo Carvalho de Melo {
149135b9d88eSArnaldo Carvalho de Melo 	if (evlist->workload.cork_fd > 0) {
1492b3824404SDavid Ahern 		char bf = 0;
1493bcf3145fSNamhyung Kim 		int ret;
149435b9d88eSArnaldo Carvalho de Melo 		/*
149535b9d88eSArnaldo Carvalho de Melo 		 * Remove the cork, let it rip!
149635b9d88eSArnaldo Carvalho de Melo 		 */
1497bcf3145fSNamhyung Kim 		ret = write(evlist->workload.cork_fd, &bf, 1);
1498bcf3145fSNamhyung Kim 		if (ret < 0)
1499bcf3145fSNamhyung Kim 			perror("enable to write to pipe");
1500bcf3145fSNamhyung Kim 
1501bcf3145fSNamhyung Kim 		close(evlist->workload.cork_fd);
1502bcf3145fSNamhyung Kim 		return ret;
150335b9d88eSArnaldo Carvalho de Melo 	}
150435b9d88eSArnaldo Carvalho de Melo 
150535b9d88eSArnaldo Carvalho de Melo 	return 0;
150635b9d88eSArnaldo Carvalho de Melo }
1507cb0b29e0SArnaldo Carvalho de Melo 
1508a3f698feSArnaldo Carvalho de Melo int perf_evlist__parse_sample(struct perf_evlist *evlist, union perf_event *event,
15090807d2d8SArnaldo Carvalho de Melo 			      struct perf_sample *sample)
1510cb0b29e0SArnaldo Carvalho de Melo {
151175562573SAdrian Hunter 	struct perf_evsel *evsel = perf_evlist__event2evsel(evlist, event);
151275562573SAdrian Hunter 
151375562573SAdrian Hunter 	if (!evsel)
151475562573SAdrian Hunter 		return -EFAULT;
15150807d2d8SArnaldo Carvalho de Melo 	return perf_evsel__parse_sample(evsel, event, sample);
1516cb0b29e0SArnaldo Carvalho de Melo }
151778f067b3SArnaldo Carvalho de Melo 
151878f067b3SArnaldo Carvalho de Melo size_t perf_evlist__fprintf(struct perf_evlist *evlist, FILE *fp)
151978f067b3SArnaldo Carvalho de Melo {
152078f067b3SArnaldo Carvalho de Melo 	struct perf_evsel *evsel;
152178f067b3SArnaldo Carvalho de Melo 	size_t printed = 0;
152278f067b3SArnaldo Carvalho de Melo 
15230050f7aaSArnaldo Carvalho de Melo 	evlist__for_each(evlist, evsel) {
152478f067b3SArnaldo Carvalho de Melo 		printed += fprintf(fp, "%s%s", evsel->idx ? ", " : "",
152578f067b3SArnaldo Carvalho de Melo 				   perf_evsel__name(evsel));
152678f067b3SArnaldo Carvalho de Melo 	}
152778f067b3SArnaldo Carvalho de Melo 
1528b2222139SDavidlohr Bueso 	return printed + fprintf(fp, "\n");
152978f067b3SArnaldo Carvalho de Melo }
15306ef068cbSArnaldo Carvalho de Melo 
1531a8f23d8fSArnaldo Carvalho de Melo int perf_evlist__strerror_open(struct perf_evlist *evlist __maybe_unused,
1532a8f23d8fSArnaldo Carvalho de Melo 			       int err, char *buf, size_t size)
1533a8f23d8fSArnaldo Carvalho de Melo {
1534a8f23d8fSArnaldo Carvalho de Melo 	int printed, value;
15356e81c74cSMasami Hiramatsu 	char sbuf[STRERR_BUFSIZE], *emsg = strerror_r(err, sbuf, sizeof(sbuf));
1536a8f23d8fSArnaldo Carvalho de Melo 
1537a8f23d8fSArnaldo Carvalho de Melo 	switch (err) {
1538a8f23d8fSArnaldo Carvalho de Melo 	case EACCES:
1539a8f23d8fSArnaldo Carvalho de Melo 	case EPERM:
1540a8f23d8fSArnaldo Carvalho de Melo 		printed = scnprintf(buf, size,
1541a8f23d8fSArnaldo Carvalho de Melo 				    "Error:\t%s.\n"
1542a8f23d8fSArnaldo Carvalho de Melo 				    "Hint:\tCheck /proc/sys/kernel/perf_event_paranoid setting.", emsg);
1543a8f23d8fSArnaldo Carvalho de Melo 
15441a47245dSAdrian Hunter 		value = perf_event_paranoid();
1545a8f23d8fSArnaldo Carvalho de Melo 
1546a8f23d8fSArnaldo Carvalho de Melo 		printed += scnprintf(buf + printed, size - printed, "\nHint:\t");
1547a8f23d8fSArnaldo Carvalho de Melo 
1548a8f23d8fSArnaldo Carvalho de Melo 		if (value >= 2) {
1549a8f23d8fSArnaldo Carvalho de Melo 			printed += scnprintf(buf + printed, size - printed,
1550a8f23d8fSArnaldo Carvalho de Melo 					     "For your workloads it needs to be <= 1\nHint:\t");
1551a8f23d8fSArnaldo Carvalho de Melo 		}
1552a8f23d8fSArnaldo Carvalho de Melo 		printed += scnprintf(buf + printed, size - printed,
15535229e366SArnaldo Carvalho de Melo 				     "For system wide tracing it needs to be set to -1.\n");
1554a8f23d8fSArnaldo Carvalho de Melo 
1555a8f23d8fSArnaldo Carvalho de Melo 		printed += scnprintf(buf + printed, size - printed,
15565229e366SArnaldo Carvalho de Melo 				    "Hint:\tTry: 'sudo sh -c \"echo -1 > /proc/sys/kernel/perf_event_paranoid\"'\n"
15575229e366SArnaldo Carvalho de Melo 				    "Hint:\tThe current value is %d.", value);
1558a8f23d8fSArnaldo Carvalho de Melo 		break;
1559a8f23d8fSArnaldo Carvalho de Melo 	default:
1560a8f23d8fSArnaldo Carvalho de Melo 		scnprintf(buf, size, "%s", emsg);
1561a8f23d8fSArnaldo Carvalho de Melo 		break;
1562a8f23d8fSArnaldo Carvalho de Melo 	}
1563a8f23d8fSArnaldo Carvalho de Melo 
1564a8f23d8fSArnaldo Carvalho de Melo 	return 0;
1565a8f23d8fSArnaldo Carvalho de Melo }
1566a025e4f0SAdrian Hunter 
1567956fa571SArnaldo Carvalho de Melo int perf_evlist__strerror_mmap(struct perf_evlist *evlist, int err, char *buf, size_t size)
1568956fa571SArnaldo Carvalho de Melo {
1569956fa571SArnaldo Carvalho de Melo 	char sbuf[STRERR_BUFSIZE], *emsg = strerror_r(err, sbuf, sizeof(sbuf));
1570e965bea1SArnaldo Carvalho de Melo 	int pages_attempted = evlist->mmap_len / 1024, pages_max_per_user, printed = 0;
1571956fa571SArnaldo Carvalho de Melo 
1572956fa571SArnaldo Carvalho de Melo 	switch (err) {
1573956fa571SArnaldo Carvalho de Melo 	case EPERM:
1574e5d4a290SArnaldo Carvalho de Melo 		sysctl__read_int("kernel/perf_event_mlock_kb", &pages_max_per_user);
1575e965bea1SArnaldo Carvalho de Melo 		printed += scnprintf(buf + printed, size - printed,
1576e965bea1SArnaldo Carvalho de Melo 				     "Error:\t%s.\n"
1577956fa571SArnaldo Carvalho de Melo 				     "Hint:\tCheck /proc/sys/kernel/perf_event_mlock_kb (%d kB) setting.\n"
1578e965bea1SArnaldo Carvalho de Melo 				     "Hint:\tTried using %zd kB.\n",
1579e5d4a290SArnaldo Carvalho de Melo 				     emsg, pages_max_per_user, pages_attempted);
1580e965bea1SArnaldo Carvalho de Melo 
1581e965bea1SArnaldo Carvalho de Melo 		if (pages_attempted >= pages_max_per_user) {
1582e965bea1SArnaldo Carvalho de Melo 			printed += scnprintf(buf + printed, size - printed,
1583e965bea1SArnaldo Carvalho de Melo 					     "Hint:\tTry 'sudo sh -c \"echo %d > /proc/sys/kernel/perf_event_mlock_kb\"', or\n",
1584e965bea1SArnaldo Carvalho de Melo 					     pages_max_per_user + pages_attempted);
1585e965bea1SArnaldo Carvalho de Melo 		}
1586e965bea1SArnaldo Carvalho de Melo 
1587e965bea1SArnaldo Carvalho de Melo 		printed += scnprintf(buf + printed, size - printed,
1588e965bea1SArnaldo Carvalho de Melo 				     "Hint:\tTry using a smaller -m/--mmap-pages value.");
1589956fa571SArnaldo Carvalho de Melo 		break;
1590956fa571SArnaldo Carvalho de Melo 	default:
1591956fa571SArnaldo Carvalho de Melo 		scnprintf(buf, size, "%s", emsg);
1592956fa571SArnaldo Carvalho de Melo 		break;
1593956fa571SArnaldo Carvalho de Melo 	}
1594956fa571SArnaldo Carvalho de Melo 
1595956fa571SArnaldo Carvalho de Melo 	return 0;
1596956fa571SArnaldo Carvalho de Melo }
1597956fa571SArnaldo Carvalho de Melo 
1598a025e4f0SAdrian Hunter void perf_evlist__to_front(struct perf_evlist *evlist,
1599a025e4f0SAdrian Hunter 			   struct perf_evsel *move_evsel)
1600a025e4f0SAdrian Hunter {
1601a025e4f0SAdrian Hunter 	struct perf_evsel *evsel, *n;
1602a025e4f0SAdrian Hunter 	LIST_HEAD(move);
1603a025e4f0SAdrian Hunter 
1604a025e4f0SAdrian Hunter 	if (move_evsel == perf_evlist__first(evlist))
1605a025e4f0SAdrian Hunter 		return;
1606a025e4f0SAdrian Hunter 
16070050f7aaSArnaldo Carvalho de Melo 	evlist__for_each_safe(evlist, n, evsel) {
1608a025e4f0SAdrian Hunter 		if (evsel->leader == move_evsel->leader)
1609a025e4f0SAdrian Hunter 			list_move_tail(&evsel->node, &move);
1610a025e4f0SAdrian Hunter 	}
1611a025e4f0SAdrian Hunter 
1612a025e4f0SAdrian Hunter 	list_splice(&move, &evlist->entries);
1613a025e4f0SAdrian Hunter }
161460b0896cSAdrian Hunter 
161560b0896cSAdrian Hunter void perf_evlist__set_tracking_event(struct perf_evlist *evlist,
161660b0896cSAdrian Hunter 				     struct perf_evsel *tracking_evsel)
161760b0896cSAdrian Hunter {
161860b0896cSAdrian Hunter 	struct perf_evsel *evsel;
161960b0896cSAdrian Hunter 
162060b0896cSAdrian Hunter 	if (tracking_evsel->tracking)
162160b0896cSAdrian Hunter 		return;
162260b0896cSAdrian Hunter 
162360b0896cSAdrian Hunter 	evlist__for_each(evlist, evsel) {
162460b0896cSAdrian Hunter 		if (evsel != tracking_evsel)
162560b0896cSAdrian Hunter 			evsel->tracking = false;
162660b0896cSAdrian Hunter 	}
162760b0896cSAdrian Hunter 
162860b0896cSAdrian Hunter 	tracking_evsel->tracking = true;
162960b0896cSAdrian Hunter }
1630