xref: /openbmc/linux/tools/perf/util/evlist.c (revision 983874d1)
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"
10553873e1SBorislav Petkov #include <api/fs/debugfs.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>
2770db7533SArnaldo Carvalho de Melo 
28f8a95309SArnaldo Carvalho de Melo #define FD(e, x, y) (*(int *)xyarray__entry(e->fd, x, y))
29a91e5431SArnaldo Carvalho de Melo #define SID(e, x, y) xyarray__entry(e->sample_id, x, y)
30f8a95309SArnaldo Carvalho de Melo 
317e2ed097SArnaldo Carvalho de Melo void perf_evlist__init(struct perf_evlist *evlist, struct cpu_map *cpus,
327e2ed097SArnaldo Carvalho de Melo 		       struct thread_map *threads)
33361c99a6SArnaldo Carvalho de Melo {
3470db7533SArnaldo Carvalho de Melo 	int i;
3570db7533SArnaldo Carvalho de Melo 
3670db7533SArnaldo Carvalho de Melo 	for (i = 0; i < PERF_EVLIST__HLIST_SIZE; ++i)
3770db7533SArnaldo Carvalho de Melo 		INIT_HLIST_HEAD(&evlist->heads[i]);
38361c99a6SArnaldo Carvalho de Melo 	INIT_LIST_HEAD(&evlist->entries);
397e2ed097SArnaldo Carvalho de Melo 	perf_evlist__set_maps(evlist, cpus, threads);
4035b9d88eSArnaldo Carvalho de Melo 	evlist->workload.pid = -1;
41361c99a6SArnaldo Carvalho de Melo }
42361c99a6SArnaldo Carvalho de Melo 
43334fe7a3SNamhyung Kim struct perf_evlist *perf_evlist__new(void)
44ef1d1af2SArnaldo Carvalho de Melo {
45ef1d1af2SArnaldo Carvalho de Melo 	struct perf_evlist *evlist = zalloc(sizeof(*evlist));
46ef1d1af2SArnaldo Carvalho de Melo 
47ef1d1af2SArnaldo Carvalho de Melo 	if (evlist != NULL)
48334fe7a3SNamhyung Kim 		perf_evlist__init(evlist, NULL, NULL);
49ef1d1af2SArnaldo Carvalho de Melo 
50361c99a6SArnaldo Carvalho de Melo 	return evlist;
51361c99a6SArnaldo Carvalho de Melo }
52361c99a6SArnaldo Carvalho de Melo 
53b22d54b0SJiri Olsa struct perf_evlist *perf_evlist__new_default(void)
54b22d54b0SJiri Olsa {
55b22d54b0SJiri Olsa 	struct perf_evlist *evlist = perf_evlist__new();
56b22d54b0SJiri Olsa 
57b22d54b0SJiri Olsa 	if (evlist && perf_evlist__add_default(evlist)) {
58b22d54b0SJiri Olsa 		perf_evlist__delete(evlist);
59b22d54b0SJiri Olsa 		evlist = NULL;
60b22d54b0SJiri Olsa 	}
61b22d54b0SJiri Olsa 
62b22d54b0SJiri Olsa 	return evlist;
63b22d54b0SJiri Olsa }
64b22d54b0SJiri Olsa 
6575562573SAdrian Hunter /**
6675562573SAdrian Hunter  * perf_evlist__set_id_pos - set the positions of event ids.
6775562573SAdrian Hunter  * @evlist: selected event list
6875562573SAdrian Hunter  *
6975562573SAdrian Hunter  * Events with compatible sample types all have the same id_pos
7075562573SAdrian Hunter  * and is_pos.  For convenience, put a copy on evlist.
7175562573SAdrian Hunter  */
7275562573SAdrian Hunter void perf_evlist__set_id_pos(struct perf_evlist *evlist)
7375562573SAdrian Hunter {
7475562573SAdrian Hunter 	struct perf_evsel *first = perf_evlist__first(evlist);
7575562573SAdrian Hunter 
7675562573SAdrian Hunter 	evlist->id_pos = first->id_pos;
7775562573SAdrian Hunter 	evlist->is_pos = first->is_pos;
7875562573SAdrian Hunter }
7975562573SAdrian Hunter 
80733cd2feSAdrian Hunter static void perf_evlist__update_id_pos(struct perf_evlist *evlist)
81733cd2feSAdrian Hunter {
82733cd2feSAdrian Hunter 	struct perf_evsel *evsel;
83733cd2feSAdrian Hunter 
84733cd2feSAdrian Hunter 	list_for_each_entry(evsel, &evlist->entries, node)
85733cd2feSAdrian Hunter 		perf_evsel__calc_id_pos(evsel);
86733cd2feSAdrian Hunter 
87733cd2feSAdrian Hunter 	perf_evlist__set_id_pos(evlist);
88733cd2feSAdrian Hunter }
89733cd2feSAdrian Hunter 
90361c99a6SArnaldo Carvalho de Melo static void perf_evlist__purge(struct perf_evlist *evlist)
91361c99a6SArnaldo Carvalho de Melo {
92361c99a6SArnaldo Carvalho de Melo 	struct perf_evsel *pos, *n;
93361c99a6SArnaldo Carvalho de Melo 
94361c99a6SArnaldo Carvalho de Melo 	list_for_each_entry_safe(pos, n, &evlist->entries, node) {
95361c99a6SArnaldo Carvalho de Melo 		list_del_init(&pos->node);
96361c99a6SArnaldo Carvalho de Melo 		perf_evsel__delete(pos);
97361c99a6SArnaldo Carvalho de Melo 	}
98361c99a6SArnaldo Carvalho de Melo 
99361c99a6SArnaldo Carvalho de Melo 	evlist->nr_entries = 0;
100361c99a6SArnaldo Carvalho de Melo }
101361c99a6SArnaldo Carvalho de Melo 
102ef1d1af2SArnaldo Carvalho de Melo void perf_evlist__exit(struct perf_evlist *evlist)
103ef1d1af2SArnaldo Carvalho de Melo {
10404662523SArnaldo Carvalho de Melo 	zfree(&evlist->mmap);
10504662523SArnaldo Carvalho de Melo 	zfree(&evlist->pollfd);
106ef1d1af2SArnaldo Carvalho de Melo }
107ef1d1af2SArnaldo Carvalho de Melo 
108361c99a6SArnaldo Carvalho de Melo void perf_evlist__delete(struct perf_evlist *evlist)
109361c99a6SArnaldo Carvalho de Melo {
110983874d1SArnaldo Carvalho de Melo 	perf_evlist__munmap(evlist);
111f26e1c7cSArnaldo Carvalho de Melo 	perf_evlist__close(evlist);
11203ad9747SArnaldo Carvalho de Melo 	cpu_map__delete(evlist->cpus);
11303ad9747SArnaldo Carvalho de Melo 	thread_map__delete(evlist->threads);
11403ad9747SArnaldo Carvalho de Melo 	evlist->cpus = NULL;
11503ad9747SArnaldo Carvalho de Melo 	evlist->threads = NULL;
116361c99a6SArnaldo Carvalho de Melo 	perf_evlist__purge(evlist);
117ef1d1af2SArnaldo Carvalho de Melo 	perf_evlist__exit(evlist);
118361c99a6SArnaldo Carvalho de Melo 	free(evlist);
119361c99a6SArnaldo Carvalho de Melo }
120361c99a6SArnaldo Carvalho de Melo 
121361c99a6SArnaldo Carvalho de Melo void perf_evlist__add(struct perf_evlist *evlist, struct perf_evsel *entry)
122361c99a6SArnaldo Carvalho de Melo {
123361c99a6SArnaldo Carvalho de Melo 	list_add_tail(&entry->node, &evlist->entries);
124ef503831SArnaldo Carvalho de Melo 	entry->idx = evlist->nr_entries;
125ef503831SArnaldo Carvalho de Melo 
12675562573SAdrian Hunter 	if (!evlist->nr_entries++)
12775562573SAdrian Hunter 		perf_evlist__set_id_pos(evlist);
128361c99a6SArnaldo Carvalho de Melo }
129361c99a6SArnaldo Carvalho de Melo 
1300529bc1fSJiri Olsa void perf_evlist__splice_list_tail(struct perf_evlist *evlist,
13150d08e47SArnaldo Carvalho de Melo 				   struct list_head *list,
13250d08e47SArnaldo Carvalho de Melo 				   int nr_entries)
13350d08e47SArnaldo Carvalho de Melo {
13475562573SAdrian Hunter 	bool set_id_pos = !evlist->nr_entries;
13575562573SAdrian Hunter 
13650d08e47SArnaldo Carvalho de Melo 	list_splice_tail(list, &evlist->entries);
13750d08e47SArnaldo Carvalho de Melo 	evlist->nr_entries += nr_entries;
13875562573SAdrian Hunter 	if (set_id_pos)
13975562573SAdrian Hunter 		perf_evlist__set_id_pos(evlist);
14050d08e47SArnaldo Carvalho de Melo }
14150d08e47SArnaldo Carvalho de Melo 
14263dab225SArnaldo Carvalho de Melo void __perf_evlist__set_leader(struct list_head *list)
14363dab225SArnaldo Carvalho de Melo {
14463dab225SArnaldo Carvalho de Melo 	struct perf_evsel *evsel, *leader;
14563dab225SArnaldo Carvalho de Melo 
14663dab225SArnaldo Carvalho de Melo 	leader = list_entry(list->next, struct perf_evsel, node);
14797f63e4aSNamhyung Kim 	evsel = list_entry(list->prev, struct perf_evsel, node);
14897f63e4aSNamhyung Kim 
14997f63e4aSNamhyung Kim 	leader->nr_members = evsel->idx - leader->idx + 1;
15063dab225SArnaldo Carvalho de Melo 
15163dab225SArnaldo Carvalho de Melo 	list_for_each_entry(evsel, list, node) {
15263dab225SArnaldo Carvalho de Melo 		evsel->leader = leader;
15363dab225SArnaldo Carvalho de Melo 	}
15463dab225SArnaldo Carvalho de Melo }
15563dab225SArnaldo Carvalho de Melo 
15663dab225SArnaldo Carvalho de Melo void perf_evlist__set_leader(struct perf_evlist *evlist)
1576a4bb04cSJiri Olsa {
15897f63e4aSNamhyung Kim 	if (evlist->nr_entries) {
15997f63e4aSNamhyung Kim 		evlist->nr_groups = evlist->nr_entries > 1 ? 1 : 0;
16063dab225SArnaldo Carvalho de Melo 		__perf_evlist__set_leader(&evlist->entries);
1616a4bb04cSJiri Olsa 	}
16297f63e4aSNamhyung Kim }
1636a4bb04cSJiri Olsa 
164361c99a6SArnaldo Carvalho de Melo int perf_evlist__add_default(struct perf_evlist *evlist)
165361c99a6SArnaldo Carvalho de Melo {
166361c99a6SArnaldo Carvalho de Melo 	struct perf_event_attr attr = {
167361c99a6SArnaldo Carvalho de Melo 		.type = PERF_TYPE_HARDWARE,
168361c99a6SArnaldo Carvalho de Melo 		.config = PERF_COUNT_HW_CPU_CYCLES,
169361c99a6SArnaldo Carvalho de Melo 	};
1701aed2671SJoerg Roedel 	struct perf_evsel *evsel;
171361c99a6SArnaldo Carvalho de Melo 
1721aed2671SJoerg Roedel 	event_attr_init(&attr);
1731aed2671SJoerg Roedel 
174ef503831SArnaldo Carvalho de Melo 	evsel = perf_evsel__new(&attr);
175361c99a6SArnaldo Carvalho de Melo 	if (evsel == NULL)
176cc2d86b0SStephane Eranian 		goto error;
177cc2d86b0SStephane Eranian 
178cc2d86b0SStephane Eranian 	/* use strdup() because free(evsel) assumes name is allocated */
179cc2d86b0SStephane Eranian 	evsel->name = strdup("cycles");
180cc2d86b0SStephane Eranian 	if (!evsel->name)
181cc2d86b0SStephane Eranian 		goto error_free;
182361c99a6SArnaldo Carvalho de Melo 
183361c99a6SArnaldo Carvalho de Melo 	perf_evlist__add(evlist, evsel);
184361c99a6SArnaldo Carvalho de Melo 	return 0;
185cc2d86b0SStephane Eranian error_free:
186cc2d86b0SStephane Eranian 	perf_evsel__delete(evsel);
187cc2d86b0SStephane Eranian error:
188cc2d86b0SStephane Eranian 	return -ENOMEM;
189361c99a6SArnaldo Carvalho de Melo }
1905c581041SArnaldo Carvalho de Melo 
191e60fc847SArnaldo Carvalho de Melo static int perf_evlist__add_attrs(struct perf_evlist *evlist,
19250d08e47SArnaldo Carvalho de Melo 				  struct perf_event_attr *attrs, size_t nr_attrs)
19350d08e47SArnaldo Carvalho de Melo {
19450d08e47SArnaldo Carvalho de Melo 	struct perf_evsel *evsel, *n;
19550d08e47SArnaldo Carvalho de Melo 	LIST_HEAD(head);
19650d08e47SArnaldo Carvalho de Melo 	size_t i;
19750d08e47SArnaldo Carvalho de Melo 
19850d08e47SArnaldo Carvalho de Melo 	for (i = 0; i < nr_attrs; i++) {
199ef503831SArnaldo Carvalho de Melo 		evsel = perf_evsel__new_idx(attrs + i, evlist->nr_entries + i);
20050d08e47SArnaldo Carvalho de Melo 		if (evsel == NULL)
20150d08e47SArnaldo Carvalho de Melo 			goto out_delete_partial_list;
20250d08e47SArnaldo Carvalho de Melo 		list_add_tail(&evsel->node, &head);
20350d08e47SArnaldo Carvalho de Melo 	}
20450d08e47SArnaldo Carvalho de Melo 
20550d08e47SArnaldo Carvalho de Melo 	perf_evlist__splice_list_tail(evlist, &head, nr_attrs);
20650d08e47SArnaldo Carvalho de Melo 
20750d08e47SArnaldo Carvalho de Melo 	return 0;
20850d08e47SArnaldo Carvalho de Melo 
20950d08e47SArnaldo Carvalho de Melo out_delete_partial_list:
21050d08e47SArnaldo Carvalho de Melo 	list_for_each_entry_safe(evsel, n, &head, node)
21150d08e47SArnaldo Carvalho de Melo 		perf_evsel__delete(evsel);
21250d08e47SArnaldo Carvalho de Melo 	return -1;
21350d08e47SArnaldo Carvalho de Melo }
21450d08e47SArnaldo Carvalho de Melo 
21579695e1bSArnaldo Carvalho de Melo int __perf_evlist__add_default_attrs(struct perf_evlist *evlist,
21679695e1bSArnaldo Carvalho de Melo 				     struct perf_event_attr *attrs, size_t nr_attrs)
21779695e1bSArnaldo Carvalho de Melo {
21879695e1bSArnaldo Carvalho de Melo 	size_t i;
21979695e1bSArnaldo Carvalho de Melo 
22079695e1bSArnaldo Carvalho de Melo 	for (i = 0; i < nr_attrs; i++)
22179695e1bSArnaldo Carvalho de Melo 		event_attr_init(attrs + i);
22279695e1bSArnaldo Carvalho de Melo 
22379695e1bSArnaldo Carvalho de Melo 	return perf_evlist__add_attrs(evlist, attrs, nr_attrs);
22479695e1bSArnaldo Carvalho de Melo }
22579695e1bSArnaldo Carvalho de Melo 
226da378962SArnaldo Carvalho de Melo struct perf_evsel *
227ee29be62SArnaldo Carvalho de Melo perf_evlist__find_tracepoint_by_id(struct perf_evlist *evlist, int id)
228ee29be62SArnaldo Carvalho de Melo {
229ee29be62SArnaldo Carvalho de Melo 	struct perf_evsel *evsel;
230ee29be62SArnaldo Carvalho de Melo 
231ee29be62SArnaldo Carvalho de Melo 	list_for_each_entry(evsel, &evlist->entries, node) {
232ee29be62SArnaldo Carvalho de Melo 		if (evsel->attr.type   == PERF_TYPE_TRACEPOINT &&
233ee29be62SArnaldo Carvalho de Melo 		    (int)evsel->attr.config == id)
234ee29be62SArnaldo Carvalho de Melo 			return evsel;
235ee29be62SArnaldo Carvalho de Melo 	}
236ee29be62SArnaldo Carvalho de Melo 
237ee29be62SArnaldo Carvalho de Melo 	return NULL;
238ee29be62SArnaldo Carvalho de Melo }
239ee29be62SArnaldo Carvalho de Melo 
240a2f2804aSDavid Ahern struct perf_evsel *
241a2f2804aSDavid Ahern perf_evlist__find_tracepoint_by_name(struct perf_evlist *evlist,
242a2f2804aSDavid Ahern 				     const char *name)
243a2f2804aSDavid Ahern {
244a2f2804aSDavid Ahern 	struct perf_evsel *evsel;
245a2f2804aSDavid Ahern 
246a2f2804aSDavid Ahern 	list_for_each_entry(evsel, &evlist->entries, node) {
247a2f2804aSDavid Ahern 		if ((evsel->attr.type == PERF_TYPE_TRACEPOINT) &&
248a2f2804aSDavid Ahern 		    (strcmp(evsel->name, name) == 0))
249a2f2804aSDavid Ahern 			return evsel;
250a2f2804aSDavid Ahern 	}
251a2f2804aSDavid Ahern 
252a2f2804aSDavid Ahern 	return NULL;
253a2f2804aSDavid Ahern }
254a2f2804aSDavid Ahern 
25539876e7dSArnaldo Carvalho de Melo int perf_evlist__add_newtp(struct perf_evlist *evlist,
25639876e7dSArnaldo Carvalho de Melo 			   const char *sys, const char *name, void *handler)
25739876e7dSArnaldo Carvalho de Melo {
258ef503831SArnaldo Carvalho de Melo 	struct perf_evsel *evsel = perf_evsel__newtp(sys, name);
25939876e7dSArnaldo Carvalho de Melo 
26039876e7dSArnaldo Carvalho de Melo 	if (evsel == NULL)
26139876e7dSArnaldo Carvalho de Melo 		return -1;
26239876e7dSArnaldo Carvalho de Melo 
263744a9719SArnaldo Carvalho de Melo 	evsel->handler = handler;
26439876e7dSArnaldo Carvalho de Melo 	perf_evlist__add(evlist, evsel);
26539876e7dSArnaldo Carvalho de Melo 	return 0;
26639876e7dSArnaldo Carvalho de Melo }
26739876e7dSArnaldo Carvalho de Melo 
2684152ab37SArnaldo Carvalho de Melo void perf_evlist__disable(struct perf_evlist *evlist)
2694152ab37SArnaldo Carvalho de Melo {
2704152ab37SArnaldo Carvalho de Melo 	int cpu, thread;
2714152ab37SArnaldo Carvalho de Melo 	struct perf_evsel *pos;
272b3a319d5SNamhyung Kim 	int nr_cpus = cpu_map__nr(evlist->cpus);
273b3a319d5SNamhyung Kim 	int nr_threads = thread_map__nr(evlist->threads);
2744152ab37SArnaldo Carvalho de Melo 
275b3a319d5SNamhyung Kim 	for (cpu = 0; cpu < nr_cpus; cpu++) {
2764152ab37SArnaldo Carvalho de Melo 		list_for_each_entry(pos, &evlist->entries, node) {
277395c3070SAdrian Hunter 			if (!perf_evsel__is_group_leader(pos) || !pos->fd)
2783fe4430dSJiri Olsa 				continue;
279b3a319d5SNamhyung Kim 			for (thread = 0; thread < nr_threads; thread++)
28055da8005SNamhyung Kim 				ioctl(FD(pos, cpu, thread),
28155da8005SNamhyung Kim 				      PERF_EVENT_IOC_DISABLE, 0);
2824152ab37SArnaldo Carvalho de Melo 		}
2834152ab37SArnaldo Carvalho de Melo 	}
2844152ab37SArnaldo Carvalho de Melo }
2854152ab37SArnaldo Carvalho de Melo 
286764e16a3SDavid Ahern void perf_evlist__enable(struct perf_evlist *evlist)
287764e16a3SDavid Ahern {
288764e16a3SDavid Ahern 	int cpu, thread;
289764e16a3SDavid Ahern 	struct perf_evsel *pos;
290b3a319d5SNamhyung Kim 	int nr_cpus = cpu_map__nr(evlist->cpus);
291b3a319d5SNamhyung Kim 	int nr_threads = thread_map__nr(evlist->threads);
292764e16a3SDavid Ahern 
293b3a319d5SNamhyung Kim 	for (cpu = 0; cpu < nr_cpus; cpu++) {
294764e16a3SDavid Ahern 		list_for_each_entry(pos, &evlist->entries, node) {
295395c3070SAdrian Hunter 			if (!perf_evsel__is_group_leader(pos) || !pos->fd)
2963fe4430dSJiri Olsa 				continue;
297b3a319d5SNamhyung Kim 			for (thread = 0; thread < nr_threads; thread++)
29855da8005SNamhyung Kim 				ioctl(FD(pos, cpu, thread),
29955da8005SNamhyung Kim 				      PERF_EVENT_IOC_ENABLE, 0);
300764e16a3SDavid Ahern 		}
301764e16a3SDavid Ahern 	}
302764e16a3SDavid Ahern }
303764e16a3SDavid Ahern 
304395c3070SAdrian Hunter int perf_evlist__disable_event(struct perf_evlist *evlist,
305395c3070SAdrian Hunter 			       struct perf_evsel *evsel)
306395c3070SAdrian Hunter {
307395c3070SAdrian Hunter 	int cpu, thread, err;
308395c3070SAdrian Hunter 
309395c3070SAdrian Hunter 	if (!evsel->fd)
310395c3070SAdrian Hunter 		return 0;
311395c3070SAdrian Hunter 
312395c3070SAdrian Hunter 	for (cpu = 0; cpu < evlist->cpus->nr; cpu++) {
313395c3070SAdrian Hunter 		for (thread = 0; thread < evlist->threads->nr; thread++) {
314395c3070SAdrian Hunter 			err = ioctl(FD(evsel, cpu, thread),
315395c3070SAdrian Hunter 				    PERF_EVENT_IOC_DISABLE, 0);
316395c3070SAdrian Hunter 			if (err)
317395c3070SAdrian Hunter 				return err;
318395c3070SAdrian Hunter 		}
319395c3070SAdrian Hunter 	}
320395c3070SAdrian Hunter 	return 0;
321395c3070SAdrian Hunter }
322395c3070SAdrian Hunter 
323395c3070SAdrian Hunter int perf_evlist__enable_event(struct perf_evlist *evlist,
324395c3070SAdrian Hunter 			      struct perf_evsel *evsel)
325395c3070SAdrian Hunter {
326395c3070SAdrian Hunter 	int cpu, thread, err;
327395c3070SAdrian Hunter 
328395c3070SAdrian Hunter 	if (!evsel->fd)
329395c3070SAdrian Hunter 		return -EINVAL;
330395c3070SAdrian Hunter 
331395c3070SAdrian Hunter 	for (cpu = 0; cpu < evlist->cpus->nr; cpu++) {
332395c3070SAdrian Hunter 		for (thread = 0; thread < evlist->threads->nr; thread++) {
333395c3070SAdrian Hunter 			err = ioctl(FD(evsel, cpu, thread),
334395c3070SAdrian Hunter 				    PERF_EVENT_IOC_ENABLE, 0);
335395c3070SAdrian Hunter 			if (err)
336395c3070SAdrian Hunter 				return err;
337395c3070SAdrian Hunter 		}
338395c3070SAdrian Hunter 	}
339395c3070SAdrian Hunter 	return 0;
340395c3070SAdrian Hunter }
341395c3070SAdrian Hunter 
342806fb630SArnaldo Carvalho de Melo static int perf_evlist__alloc_pollfd(struct perf_evlist *evlist)
3435c581041SArnaldo Carvalho de Melo {
344b3a319d5SNamhyung Kim 	int nr_cpus = cpu_map__nr(evlist->cpus);
345b3a319d5SNamhyung Kim 	int nr_threads = thread_map__nr(evlist->threads);
346b3a319d5SNamhyung Kim 	int nfds = nr_cpus * nr_threads * evlist->nr_entries;
3475c581041SArnaldo Carvalho de Melo 	evlist->pollfd = malloc(sizeof(struct pollfd) * nfds);
3485c581041SArnaldo Carvalho de Melo 	return evlist->pollfd != NULL ? 0 : -ENOMEM;
3495c581041SArnaldo Carvalho de Melo }
35070082dd9SArnaldo Carvalho de Melo 
35170082dd9SArnaldo Carvalho de Melo void perf_evlist__add_pollfd(struct perf_evlist *evlist, int fd)
35270082dd9SArnaldo Carvalho de Melo {
35370082dd9SArnaldo Carvalho de Melo 	fcntl(fd, F_SETFL, O_NONBLOCK);
35470082dd9SArnaldo Carvalho de Melo 	evlist->pollfd[evlist->nr_fds].fd = fd;
35570082dd9SArnaldo Carvalho de Melo 	evlist->pollfd[evlist->nr_fds].events = POLLIN;
35670082dd9SArnaldo Carvalho de Melo 	evlist->nr_fds++;
35770082dd9SArnaldo Carvalho de Melo }
35870db7533SArnaldo Carvalho de Melo 
359a91e5431SArnaldo Carvalho de Melo static void perf_evlist__id_hash(struct perf_evlist *evlist,
360a91e5431SArnaldo Carvalho de Melo 				 struct perf_evsel *evsel,
3613d3b5e95SArnaldo Carvalho de Melo 				 int cpu, int thread, u64 id)
3623d3b5e95SArnaldo Carvalho de Melo {
3633d3b5e95SArnaldo Carvalho de Melo 	int hash;
3643d3b5e95SArnaldo Carvalho de Melo 	struct perf_sample_id *sid = SID(evsel, cpu, thread);
3653d3b5e95SArnaldo Carvalho de Melo 
3663d3b5e95SArnaldo Carvalho de Melo 	sid->id = id;
3673d3b5e95SArnaldo Carvalho de Melo 	sid->evsel = evsel;
3683d3b5e95SArnaldo Carvalho de Melo 	hash = hash_64(sid->id, PERF_EVLIST__HLIST_BITS);
3693d3b5e95SArnaldo Carvalho de Melo 	hlist_add_head(&sid->node, &evlist->heads[hash]);
3703d3b5e95SArnaldo Carvalho de Melo }
3713d3b5e95SArnaldo Carvalho de Melo 
372a91e5431SArnaldo Carvalho de Melo void perf_evlist__id_add(struct perf_evlist *evlist, struct perf_evsel *evsel,
373a91e5431SArnaldo Carvalho de Melo 			 int cpu, int thread, u64 id)
374a91e5431SArnaldo Carvalho de Melo {
375a91e5431SArnaldo Carvalho de Melo 	perf_evlist__id_hash(evlist, evsel, cpu, thread, id);
376a91e5431SArnaldo Carvalho de Melo 	evsel->id[evsel->ids++] = id;
377a91e5431SArnaldo Carvalho de Melo }
378a91e5431SArnaldo Carvalho de Melo 
379a91e5431SArnaldo Carvalho de Melo static int perf_evlist__id_add_fd(struct perf_evlist *evlist,
3803d3b5e95SArnaldo Carvalho de Melo 				  struct perf_evsel *evsel,
381f8a95309SArnaldo Carvalho de Melo 				  int cpu, int thread, int fd)
382f8a95309SArnaldo Carvalho de Melo {
383f8a95309SArnaldo Carvalho de Melo 	u64 read_data[4] = { 0, };
3843d3b5e95SArnaldo Carvalho de Melo 	int id_idx = 1; /* The first entry is the counter value */
385e2b5abe0SJiri Olsa 	u64 id;
386e2b5abe0SJiri Olsa 	int ret;
387e2b5abe0SJiri Olsa 
388e2b5abe0SJiri Olsa 	ret = ioctl(fd, PERF_EVENT_IOC_ID, &id);
389e2b5abe0SJiri Olsa 	if (!ret)
390e2b5abe0SJiri Olsa 		goto add;
391e2b5abe0SJiri Olsa 
392e2b5abe0SJiri Olsa 	if (errno != ENOTTY)
393e2b5abe0SJiri Olsa 		return -1;
394e2b5abe0SJiri Olsa 
395e2b5abe0SJiri Olsa 	/* Legacy way to get event id.. All hail to old kernels! */
396f8a95309SArnaldo Carvalho de Melo 
397c4861afeSJiri Olsa 	/*
398c4861afeSJiri Olsa 	 * This way does not work with group format read, so bail
399c4861afeSJiri Olsa 	 * out in that case.
400c4861afeSJiri Olsa 	 */
401c4861afeSJiri Olsa 	if (perf_evlist__read_format(evlist) & PERF_FORMAT_GROUP)
402c4861afeSJiri Olsa 		return -1;
403c4861afeSJiri Olsa 
404f8a95309SArnaldo Carvalho de Melo 	if (!(evsel->attr.read_format & PERF_FORMAT_ID) ||
405f8a95309SArnaldo Carvalho de Melo 	    read(fd, &read_data, sizeof(read_data)) == -1)
406f8a95309SArnaldo Carvalho de Melo 		return -1;
407f8a95309SArnaldo Carvalho de Melo 
408f8a95309SArnaldo Carvalho de Melo 	if (evsel->attr.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
409f8a95309SArnaldo Carvalho de Melo 		++id_idx;
410f8a95309SArnaldo Carvalho de Melo 	if (evsel->attr.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
411f8a95309SArnaldo Carvalho de Melo 		++id_idx;
412f8a95309SArnaldo Carvalho de Melo 
413e2b5abe0SJiri Olsa 	id = read_data[id_idx];
414e2b5abe0SJiri Olsa 
415e2b5abe0SJiri Olsa  add:
416e2b5abe0SJiri Olsa 	perf_evlist__id_add(evlist, evsel, cpu, thread, id);
417f8a95309SArnaldo Carvalho de Melo 	return 0;
418f8a95309SArnaldo Carvalho de Melo }
419f8a95309SArnaldo Carvalho de Melo 
420932a3594SJiri Olsa struct perf_sample_id *perf_evlist__id2sid(struct perf_evlist *evlist, u64 id)
42170db7533SArnaldo Carvalho de Melo {
42270db7533SArnaldo Carvalho de Melo 	struct hlist_head *head;
42370db7533SArnaldo Carvalho de Melo 	struct perf_sample_id *sid;
42470db7533SArnaldo Carvalho de Melo 	int hash;
42570db7533SArnaldo Carvalho de Melo 
42670db7533SArnaldo Carvalho de Melo 	hash = hash_64(id, PERF_EVLIST__HLIST_BITS);
42770db7533SArnaldo Carvalho de Melo 	head = &evlist->heads[hash];
42870db7533SArnaldo Carvalho de Melo 
429b67bfe0dSSasha Levin 	hlist_for_each_entry(sid, head, node)
43070db7533SArnaldo Carvalho de Melo 		if (sid->id == id)
431932a3594SJiri Olsa 			return sid;
432932a3594SJiri Olsa 
433932a3594SJiri Olsa 	return NULL;
434932a3594SJiri Olsa }
435932a3594SJiri Olsa 
436932a3594SJiri Olsa struct perf_evsel *perf_evlist__id2evsel(struct perf_evlist *evlist, u64 id)
437932a3594SJiri Olsa {
438932a3594SJiri Olsa 	struct perf_sample_id *sid;
439932a3594SJiri Olsa 
440932a3594SJiri Olsa 	if (evlist->nr_entries == 1)
441932a3594SJiri Olsa 		return perf_evlist__first(evlist);
442932a3594SJiri Olsa 
443932a3594SJiri Olsa 	sid = perf_evlist__id2sid(evlist, id);
444932a3594SJiri Olsa 	if (sid)
44570db7533SArnaldo Carvalho de Melo 		return sid->evsel;
44630e68bccSNamhyung Kim 
44730e68bccSNamhyung Kim 	if (!perf_evlist__sample_id_all(evlist))
4480c21f736SArnaldo Carvalho de Melo 		return perf_evlist__first(evlist);
44930e68bccSNamhyung Kim 
45070db7533SArnaldo Carvalho de Melo 	return NULL;
45170db7533SArnaldo Carvalho de Melo }
45204391debSArnaldo Carvalho de Melo 
45375562573SAdrian Hunter static int perf_evlist__event2id(struct perf_evlist *evlist,
45475562573SAdrian Hunter 				 union perf_event *event, u64 *id)
45575562573SAdrian Hunter {
45675562573SAdrian Hunter 	const u64 *array = event->sample.array;
45775562573SAdrian Hunter 	ssize_t n;
45875562573SAdrian Hunter 
45975562573SAdrian Hunter 	n = (event->header.size - sizeof(event->header)) >> 3;
46075562573SAdrian Hunter 
46175562573SAdrian Hunter 	if (event->header.type == PERF_RECORD_SAMPLE) {
46275562573SAdrian Hunter 		if (evlist->id_pos >= n)
46375562573SAdrian Hunter 			return -1;
46475562573SAdrian Hunter 		*id = array[evlist->id_pos];
46575562573SAdrian Hunter 	} else {
46675562573SAdrian Hunter 		if (evlist->is_pos > n)
46775562573SAdrian Hunter 			return -1;
46875562573SAdrian Hunter 		n -= evlist->is_pos;
46975562573SAdrian Hunter 		*id = array[n];
47075562573SAdrian Hunter 	}
47175562573SAdrian Hunter 	return 0;
47275562573SAdrian Hunter }
47375562573SAdrian Hunter 
47475562573SAdrian Hunter static struct perf_evsel *perf_evlist__event2evsel(struct perf_evlist *evlist,
47575562573SAdrian Hunter 						   union perf_event *event)
47675562573SAdrian Hunter {
47798be6966SAdrian Hunter 	struct perf_evsel *first = perf_evlist__first(evlist);
47875562573SAdrian Hunter 	struct hlist_head *head;
47975562573SAdrian Hunter 	struct perf_sample_id *sid;
48075562573SAdrian Hunter 	int hash;
48175562573SAdrian Hunter 	u64 id;
48275562573SAdrian Hunter 
48375562573SAdrian Hunter 	if (evlist->nr_entries == 1)
48498be6966SAdrian Hunter 		return first;
48598be6966SAdrian Hunter 
48698be6966SAdrian Hunter 	if (!first->attr.sample_id_all &&
48798be6966SAdrian Hunter 	    event->header.type != PERF_RECORD_SAMPLE)
48898be6966SAdrian Hunter 		return first;
48975562573SAdrian Hunter 
49075562573SAdrian Hunter 	if (perf_evlist__event2id(evlist, event, &id))
49175562573SAdrian Hunter 		return NULL;
49275562573SAdrian Hunter 
49375562573SAdrian Hunter 	/* Synthesized events have an id of zero */
49475562573SAdrian Hunter 	if (!id)
49598be6966SAdrian Hunter 		return first;
49675562573SAdrian Hunter 
49775562573SAdrian Hunter 	hash = hash_64(id, PERF_EVLIST__HLIST_BITS);
49875562573SAdrian Hunter 	head = &evlist->heads[hash];
49975562573SAdrian Hunter 
50075562573SAdrian Hunter 	hlist_for_each_entry(sid, head, node) {
50175562573SAdrian Hunter 		if (sid->id == id)
50275562573SAdrian Hunter 			return sid->evsel;
50375562573SAdrian Hunter 	}
50475562573SAdrian Hunter 	return NULL;
50575562573SAdrian Hunter }
50675562573SAdrian Hunter 
507aece948fSArnaldo Carvalho de Melo union perf_event *perf_evlist__mmap_read(struct perf_evlist *evlist, int idx)
50804391debSArnaldo Carvalho de Melo {
509aece948fSArnaldo Carvalho de Melo 	struct perf_mmap *md = &evlist->mmap[idx];
51004391debSArnaldo Carvalho de Melo 	unsigned int head = perf_mmap__read_head(md);
51104391debSArnaldo Carvalho de Melo 	unsigned int old = md->prev;
51204391debSArnaldo Carvalho de Melo 	unsigned char *data = md->base + page_size;
5138115d60cSArnaldo Carvalho de Melo 	union perf_event *event = NULL;
51404391debSArnaldo Carvalho de Melo 
5157bb41152SArnaldo Carvalho de Melo 	if (evlist->overwrite) {
51604391debSArnaldo Carvalho de Melo 		/*
51704391debSArnaldo Carvalho de Melo 		 * If we're further behind than half the buffer, there's a chance
51804391debSArnaldo Carvalho de Melo 		 * the writer will bite our tail and mess up the samples under us.
51904391debSArnaldo Carvalho de Melo 		 *
52004391debSArnaldo Carvalho de Melo 		 * If we somehow ended up ahead of the head, we got messed up.
52104391debSArnaldo Carvalho de Melo 		 *
52204391debSArnaldo Carvalho de Melo 		 * In either case, truncate and restart at head.
52304391debSArnaldo Carvalho de Melo 		 */
5247bb41152SArnaldo Carvalho de Melo 		int diff = head - old;
52504391debSArnaldo Carvalho de Melo 		if (diff > md->mask / 2 || diff < 0) {
52604391debSArnaldo Carvalho de Melo 			fprintf(stderr, "WARNING: failed to keep up with mmap data.\n");
52704391debSArnaldo Carvalho de Melo 
52804391debSArnaldo Carvalho de Melo 			/*
52904391debSArnaldo Carvalho de Melo 			 * head points to a known good entry, start there.
53004391debSArnaldo Carvalho de Melo 			 */
53104391debSArnaldo Carvalho de Melo 			old = head;
53204391debSArnaldo Carvalho de Melo 		}
5337bb41152SArnaldo Carvalho de Melo 	}
53404391debSArnaldo Carvalho de Melo 
53504391debSArnaldo Carvalho de Melo 	if (old != head) {
53604391debSArnaldo Carvalho de Melo 		size_t size;
53704391debSArnaldo Carvalho de Melo 
5388115d60cSArnaldo Carvalho de Melo 		event = (union perf_event *)&data[old & md->mask];
53904391debSArnaldo Carvalho de Melo 		size = event->header.size;
54004391debSArnaldo Carvalho de Melo 
54104391debSArnaldo Carvalho de Melo 		/*
54204391debSArnaldo Carvalho de Melo 		 * Event straddles the mmap boundary -- header should always
54304391debSArnaldo Carvalho de Melo 		 * be inside due to u64 alignment of output.
54404391debSArnaldo Carvalho de Melo 		 */
54504391debSArnaldo Carvalho de Melo 		if ((old & md->mask) + size != ((old + size) & md->mask)) {
54604391debSArnaldo Carvalho de Melo 			unsigned int offset = old;
54704391debSArnaldo Carvalho de Melo 			unsigned int len = min(sizeof(*event), size), cpy;
548a65cb4b9SJiri Olsa 			void *dst = md->event_copy;
54904391debSArnaldo Carvalho de Melo 
55004391debSArnaldo Carvalho de Melo 			do {
55104391debSArnaldo Carvalho de Melo 				cpy = min(md->mask + 1 - (offset & md->mask), len);
55204391debSArnaldo Carvalho de Melo 				memcpy(dst, &data[offset & md->mask], cpy);
55304391debSArnaldo Carvalho de Melo 				offset += cpy;
55404391debSArnaldo Carvalho de Melo 				dst += cpy;
55504391debSArnaldo Carvalho de Melo 				len -= cpy;
55604391debSArnaldo Carvalho de Melo 			} while (len);
55704391debSArnaldo Carvalho de Melo 
558a65cb4b9SJiri Olsa 			event = (union perf_event *) md->event_copy;
55904391debSArnaldo Carvalho de Melo 		}
56004391debSArnaldo Carvalho de Melo 
56104391debSArnaldo Carvalho de Melo 		old += size;
56204391debSArnaldo Carvalho de Melo 	}
56304391debSArnaldo Carvalho de Melo 
56404391debSArnaldo Carvalho de Melo 	md->prev = old;
5657bb41152SArnaldo Carvalho de Melo 
56604391debSArnaldo Carvalho de Melo 	return event;
56704391debSArnaldo Carvalho de Melo }
568f8a95309SArnaldo Carvalho de Melo 
5698e50d384SZhouyi Zhou void perf_evlist__mmap_consume(struct perf_evlist *evlist, int idx)
5708e50d384SZhouyi Zhou {
5718e50d384SZhouyi Zhou 	if (!evlist->overwrite) {
5728e50d384SZhouyi Zhou 		struct perf_mmap *md = &evlist->mmap[idx];
5738e50d384SZhouyi Zhou 		unsigned int old = md->prev;
5748e50d384SZhouyi Zhou 
5758e50d384SZhouyi Zhou 		perf_mmap__write_tail(md, old);
5768e50d384SZhouyi Zhou 	}
5778e50d384SZhouyi Zhou }
5788e50d384SZhouyi Zhou 
57993edcbd9SAdrian Hunter static void __perf_evlist__munmap(struct perf_evlist *evlist, int idx)
58093edcbd9SAdrian Hunter {
58193edcbd9SAdrian Hunter 	if (evlist->mmap[idx].base != NULL) {
58293edcbd9SAdrian Hunter 		munmap(evlist->mmap[idx].base, evlist->mmap_len);
58393edcbd9SAdrian Hunter 		evlist->mmap[idx].base = NULL;
58493edcbd9SAdrian Hunter 	}
58593edcbd9SAdrian Hunter }
58693edcbd9SAdrian Hunter 
5877e2ed097SArnaldo Carvalho de Melo void perf_evlist__munmap(struct perf_evlist *evlist)
588f8a95309SArnaldo Carvalho de Melo {
589aece948fSArnaldo Carvalho de Melo 	int i;
590f8a95309SArnaldo Carvalho de Melo 
591983874d1SArnaldo Carvalho de Melo 	if (evlist->mmap == NULL)
592983874d1SArnaldo Carvalho de Melo 		return;
593983874d1SArnaldo Carvalho de Melo 
59493edcbd9SAdrian Hunter 	for (i = 0; i < evlist->nr_mmaps; i++)
59593edcbd9SAdrian Hunter 		__perf_evlist__munmap(evlist, i);
596aece948fSArnaldo Carvalho de Melo 
59704662523SArnaldo Carvalho de Melo 	zfree(&evlist->mmap);
598f8a95309SArnaldo Carvalho de Melo }
599f8a95309SArnaldo Carvalho de Melo 
600806fb630SArnaldo Carvalho de Melo static int perf_evlist__alloc_mmap(struct perf_evlist *evlist)
601f8a95309SArnaldo Carvalho de Melo {
602a14bb7a6SArnaldo Carvalho de Melo 	evlist->nr_mmaps = cpu_map__nr(evlist->cpus);
603ec1e7e43SSukadev Bhattiprolu 	if (cpu_map__empty(evlist->cpus))
604b3a319d5SNamhyung Kim 		evlist->nr_mmaps = thread_map__nr(evlist->threads);
605aece948fSArnaldo Carvalho de Melo 	evlist->mmap = zalloc(evlist->nr_mmaps * sizeof(struct perf_mmap));
606f8a95309SArnaldo Carvalho de Melo 	return evlist->mmap != NULL ? 0 : -ENOMEM;
607f8a95309SArnaldo Carvalho de Melo }
608f8a95309SArnaldo Carvalho de Melo 
609bccdaba0SArnaldo Carvalho de Melo static int __perf_evlist__mmap(struct perf_evlist *evlist,
610aece948fSArnaldo Carvalho de Melo 			       int idx, int prot, int mask, int fd)
611f8a95309SArnaldo Carvalho de Melo {
612aece948fSArnaldo Carvalho de Melo 	evlist->mmap[idx].prev = 0;
613aece948fSArnaldo Carvalho de Melo 	evlist->mmap[idx].mask = mask;
614aece948fSArnaldo Carvalho de Melo 	evlist->mmap[idx].base = mmap(NULL, evlist->mmap_len, prot,
615f8a95309SArnaldo Carvalho de Melo 				      MAP_SHARED, fd, 0);
616301b195dSNelson Elhage 	if (evlist->mmap[idx].base == MAP_FAILED) {
61702635965SAdrian Hunter 		pr_debug2("failed to mmap perf event ring buffer, error %d\n",
61802635965SAdrian Hunter 			  errno);
619301b195dSNelson Elhage 		evlist->mmap[idx].base = NULL;
620f8a95309SArnaldo Carvalho de Melo 		return -1;
621301b195dSNelson Elhage 	}
622f8a95309SArnaldo Carvalho de Melo 
623f8a95309SArnaldo Carvalho de Melo 	perf_evlist__add_pollfd(evlist, fd);
624f8a95309SArnaldo Carvalho de Melo 	return 0;
625f8a95309SArnaldo Carvalho de Melo }
626f8a95309SArnaldo Carvalho de Melo 
62704e21314SAdrian Hunter static int perf_evlist__mmap_per_evsel(struct perf_evlist *evlist, int idx,
62804e21314SAdrian Hunter 				       int prot, int mask, int cpu, int thread,
62904e21314SAdrian Hunter 				       int *output)
630aece948fSArnaldo Carvalho de Melo {
631aece948fSArnaldo Carvalho de Melo 	struct perf_evsel *evsel;
63204e21314SAdrian Hunter 
63304e21314SAdrian Hunter 	list_for_each_entry(evsel, &evlist->entries, node) {
63404e21314SAdrian Hunter 		int fd = FD(evsel, cpu, thread);
63504e21314SAdrian Hunter 
63604e21314SAdrian Hunter 		if (*output == -1) {
63704e21314SAdrian Hunter 			*output = fd;
63804e21314SAdrian Hunter 			if (__perf_evlist__mmap(evlist, idx, prot, mask,
63904e21314SAdrian Hunter 						*output) < 0)
64004e21314SAdrian Hunter 				return -1;
64104e21314SAdrian Hunter 		} else {
64204e21314SAdrian Hunter 			if (ioctl(fd, PERF_EVENT_IOC_SET_OUTPUT, *output) != 0)
64304e21314SAdrian Hunter 				return -1;
64404e21314SAdrian Hunter 		}
64504e21314SAdrian Hunter 
64604e21314SAdrian Hunter 		if ((evsel->attr.read_format & PERF_FORMAT_ID) &&
64704e21314SAdrian Hunter 		    perf_evlist__id_add_fd(evlist, evsel, cpu, thread, fd) < 0)
64804e21314SAdrian Hunter 			return -1;
64904e21314SAdrian Hunter 	}
65004e21314SAdrian Hunter 
65104e21314SAdrian Hunter 	return 0;
65204e21314SAdrian Hunter }
65304e21314SAdrian Hunter 
65404e21314SAdrian Hunter static int perf_evlist__mmap_per_cpu(struct perf_evlist *evlist, int prot,
65504e21314SAdrian Hunter 				     int mask)
65604e21314SAdrian Hunter {
657aece948fSArnaldo Carvalho de Melo 	int cpu, thread;
658b3a319d5SNamhyung Kim 	int nr_cpus = cpu_map__nr(evlist->cpus);
659b3a319d5SNamhyung Kim 	int nr_threads = thread_map__nr(evlist->threads);
660aece948fSArnaldo Carvalho de Melo 
661e3e1a54fSAdrian Hunter 	pr_debug2("perf event ring buffer mmapped per cpu\n");
662b3a319d5SNamhyung Kim 	for (cpu = 0; cpu < nr_cpus; cpu++) {
663aece948fSArnaldo Carvalho de Melo 		int output = -1;
664aece948fSArnaldo Carvalho de Melo 
665b3a319d5SNamhyung Kim 		for (thread = 0; thread < nr_threads; thread++) {
66604e21314SAdrian Hunter 			if (perf_evlist__mmap_per_evsel(evlist, cpu, prot, mask,
66704e21314SAdrian Hunter 							cpu, thread, &output))
668aece948fSArnaldo Carvalho de Melo 				goto out_unmap;
669aece948fSArnaldo Carvalho de Melo 		}
670aece948fSArnaldo Carvalho de Melo 	}
671aece948fSArnaldo Carvalho de Melo 
672aece948fSArnaldo Carvalho de Melo 	return 0;
673aece948fSArnaldo Carvalho de Melo 
674aece948fSArnaldo Carvalho de Melo out_unmap:
67593edcbd9SAdrian Hunter 	for (cpu = 0; cpu < nr_cpus; cpu++)
67693edcbd9SAdrian Hunter 		__perf_evlist__munmap(evlist, cpu);
677aece948fSArnaldo Carvalho de Melo 	return -1;
678aece948fSArnaldo Carvalho de Melo }
679aece948fSArnaldo Carvalho de Melo 
68004e21314SAdrian Hunter static int perf_evlist__mmap_per_thread(struct perf_evlist *evlist, int prot,
68104e21314SAdrian Hunter 					int mask)
682aece948fSArnaldo Carvalho de Melo {
683aece948fSArnaldo Carvalho de Melo 	int thread;
684b3a319d5SNamhyung Kim 	int nr_threads = thread_map__nr(evlist->threads);
685aece948fSArnaldo Carvalho de Melo 
686e3e1a54fSAdrian Hunter 	pr_debug2("perf event ring buffer mmapped per thread\n");
687b3a319d5SNamhyung Kim 	for (thread = 0; thread < nr_threads; thread++) {
688aece948fSArnaldo Carvalho de Melo 		int output = -1;
689aece948fSArnaldo Carvalho de Melo 
69004e21314SAdrian Hunter 		if (perf_evlist__mmap_per_evsel(evlist, thread, prot, mask, 0,
69104e21314SAdrian Hunter 						thread, &output))
692aece948fSArnaldo Carvalho de Melo 			goto out_unmap;
693aece948fSArnaldo Carvalho de Melo 	}
694aece948fSArnaldo Carvalho de Melo 
695aece948fSArnaldo Carvalho de Melo 	return 0;
696aece948fSArnaldo Carvalho de Melo 
697aece948fSArnaldo Carvalho de Melo out_unmap:
69893edcbd9SAdrian Hunter 	for (thread = 0; thread < nr_threads; thread++)
69993edcbd9SAdrian Hunter 		__perf_evlist__munmap(evlist, thread);
700aece948fSArnaldo Carvalho de Melo 	return -1;
701aece948fSArnaldo Carvalho de Melo }
702aece948fSArnaldo Carvalho de Melo 
703994a1f78SJiri Olsa static size_t perf_evlist__mmap_size(unsigned long pages)
704994a1f78SJiri Olsa {
705994a1f78SJiri Olsa 	/* 512 kiB: default amount of unprivileged mlocked memory */
706994a1f78SJiri Olsa 	if (pages == UINT_MAX)
707994a1f78SJiri Olsa 		pages = (512 * 1024) / page_size;
708994a1f78SJiri Olsa 	else if (!is_power_of_2(pages))
709994a1f78SJiri Olsa 		return 0;
710994a1f78SJiri Olsa 
711994a1f78SJiri Olsa 	return (pages + 1) * page_size;
712994a1f78SJiri Olsa }
713994a1f78SJiri Olsa 
71433c2dcfdSDavid Ahern static long parse_pages_arg(const char *str, unsigned long min,
71533c2dcfdSDavid Ahern 			    unsigned long max)
716994a1f78SJiri Olsa {
7172fbe4abeSAdrian Hunter 	unsigned long pages, val;
71827050f53SJiri Olsa 	static struct parse_tag tags[] = {
71927050f53SJiri Olsa 		{ .tag  = 'B', .mult = 1       },
72027050f53SJiri Olsa 		{ .tag  = 'K', .mult = 1 << 10 },
72127050f53SJiri Olsa 		{ .tag  = 'M', .mult = 1 << 20 },
72227050f53SJiri Olsa 		{ .tag  = 'G', .mult = 1 << 30 },
72327050f53SJiri Olsa 		{ .tag  = 0 },
72427050f53SJiri Olsa 	};
725994a1f78SJiri Olsa 
7268973504bSDavid Ahern 	if (str == NULL)
72733c2dcfdSDavid Ahern 		return -EINVAL;
7288973504bSDavid Ahern 
72927050f53SJiri Olsa 	val = parse_tag_value(str, tags);
7302fbe4abeSAdrian Hunter 	if (val != (unsigned long) -1) {
73127050f53SJiri Olsa 		/* we got file size value */
73227050f53SJiri Olsa 		pages = PERF_ALIGN(val, page_size) / page_size;
73327050f53SJiri Olsa 	} else {
73427050f53SJiri Olsa 		/* we got pages count value */
73527050f53SJiri Olsa 		char *eptr;
736994a1f78SJiri Olsa 		pages = strtoul(str, &eptr, 10);
73733c2dcfdSDavid Ahern 		if (*eptr != '\0')
73833c2dcfdSDavid Ahern 			return -EINVAL;
73927050f53SJiri Olsa 	}
740994a1f78SJiri Olsa 
7412bcab6c1SAdrian Hunter 	if (pages == 0 && min == 0) {
74233c2dcfdSDavid Ahern 		/* leave number of pages at 0 */
7431dbfa938SAdrian Hunter 	} else if (!is_power_of_2(pages)) {
74433c2dcfdSDavid Ahern 		/* round pages up to next power of 2 */
7451dbfa938SAdrian Hunter 		pages = next_pow2_l(pages);
7461dbfa938SAdrian Hunter 		if (!pages)
7471dbfa938SAdrian Hunter 			return -EINVAL;
7489639837eSDavid Ahern 		pr_info("rounding mmap pages size to %lu bytes (%lu pages)\n",
7499639837eSDavid Ahern 			pages * page_size, pages);
7502fbe4abeSAdrian Hunter 	}
7512fbe4abeSAdrian Hunter 
75233c2dcfdSDavid Ahern 	if (pages > max)
75333c2dcfdSDavid Ahern 		return -EINVAL;
75433c2dcfdSDavid Ahern 
75533c2dcfdSDavid Ahern 	return pages;
75633c2dcfdSDavid Ahern }
75733c2dcfdSDavid Ahern 
75833c2dcfdSDavid Ahern int perf_evlist__parse_mmap_pages(const struct option *opt, const char *str,
75933c2dcfdSDavid Ahern 				  int unset __maybe_unused)
76033c2dcfdSDavid Ahern {
76133c2dcfdSDavid Ahern 	unsigned int *mmap_pages = opt->value;
76233c2dcfdSDavid Ahern 	unsigned long max = UINT_MAX;
76333c2dcfdSDavid Ahern 	long pages;
76433c2dcfdSDavid Ahern 
765f5ae9c42SAdrian Hunter 	if (max > SIZE_MAX / page_size)
76633c2dcfdSDavid Ahern 		max = SIZE_MAX / page_size;
76733c2dcfdSDavid Ahern 
76833c2dcfdSDavid Ahern 	pages = parse_pages_arg(str, 1, max);
76933c2dcfdSDavid Ahern 	if (pages < 0) {
77033c2dcfdSDavid Ahern 		pr_err("Invalid argument for --mmap_pages/-m\n");
771994a1f78SJiri Olsa 		return -1;
772994a1f78SJiri Olsa 	}
773994a1f78SJiri Olsa 
774994a1f78SJiri Olsa 	*mmap_pages = pages;
775994a1f78SJiri Olsa 	return 0;
776994a1f78SJiri Olsa }
777994a1f78SJiri Olsa 
778c83fa7f2SAdrian Hunter /**
779c83fa7f2SAdrian Hunter  * perf_evlist__mmap - Create mmaps to receive events.
780c83fa7f2SAdrian Hunter  * @evlist: list of events
781c83fa7f2SAdrian Hunter  * @pages: map length in pages
782c83fa7f2SAdrian Hunter  * @overwrite: overwrite older events?
783f8a95309SArnaldo Carvalho de Melo  *
784c83fa7f2SAdrian Hunter  * If @overwrite is %false the user needs to signal event consumption using
785c83fa7f2SAdrian Hunter  * perf_mmap__write_tail().  Using perf_evlist__mmap_read() does this
786c83fa7f2SAdrian Hunter  * automatically.
787f8a95309SArnaldo Carvalho de Melo  *
788c83fa7f2SAdrian Hunter  * Return: %0 on success, negative error code otherwise.
789f8a95309SArnaldo Carvalho de Melo  */
79050a682ceSArnaldo Carvalho de Melo int perf_evlist__mmap(struct perf_evlist *evlist, unsigned int pages,
79150a682ceSArnaldo Carvalho de Melo 		      bool overwrite)
792f8a95309SArnaldo Carvalho de Melo {
793aece948fSArnaldo Carvalho de Melo 	struct perf_evsel *evsel;
7947e2ed097SArnaldo Carvalho de Melo 	const struct cpu_map *cpus = evlist->cpus;
7957e2ed097SArnaldo Carvalho de Melo 	const struct thread_map *threads = evlist->threads;
79650a682ceSArnaldo Carvalho de Melo 	int prot = PROT_READ | (overwrite ? 0 : PROT_WRITE), mask;
79750a682ceSArnaldo Carvalho de Melo 
7987e2ed097SArnaldo Carvalho de Melo 	if (evlist->mmap == NULL && perf_evlist__alloc_mmap(evlist) < 0)
799f8a95309SArnaldo Carvalho de Melo 		return -ENOMEM;
800f8a95309SArnaldo Carvalho de Melo 
8017e2ed097SArnaldo Carvalho de Melo 	if (evlist->pollfd == NULL && perf_evlist__alloc_pollfd(evlist) < 0)
802f8a95309SArnaldo Carvalho de Melo 		return -ENOMEM;
803f8a95309SArnaldo Carvalho de Melo 
804f8a95309SArnaldo Carvalho de Melo 	evlist->overwrite = overwrite;
805994a1f78SJiri Olsa 	evlist->mmap_len = perf_evlist__mmap_size(pages);
8062af68ef5SAdrian Hunter 	pr_debug("mmap size %zuB\n", evlist->mmap_len);
807994a1f78SJiri Olsa 	mask = evlist->mmap_len - page_size - 1;
808f8a95309SArnaldo Carvalho de Melo 
809f8a95309SArnaldo Carvalho de Melo 	list_for_each_entry(evsel, &evlist->entries, node) {
810f8a95309SArnaldo Carvalho de Melo 		if ((evsel->attr.read_format & PERF_FORMAT_ID) &&
811a91e5431SArnaldo Carvalho de Melo 		    evsel->sample_id == NULL &&
812a14bb7a6SArnaldo Carvalho de Melo 		    perf_evsel__alloc_id(evsel, cpu_map__nr(cpus), threads->nr) < 0)
813f8a95309SArnaldo Carvalho de Melo 			return -ENOMEM;
814f8a95309SArnaldo Carvalho de Melo 	}
815f8a95309SArnaldo Carvalho de Melo 
816ec1e7e43SSukadev Bhattiprolu 	if (cpu_map__empty(cpus))
817aece948fSArnaldo Carvalho de Melo 		return perf_evlist__mmap_per_thread(evlist, prot, mask);
818f8a95309SArnaldo Carvalho de Melo 
819aece948fSArnaldo Carvalho de Melo 	return perf_evlist__mmap_per_cpu(evlist, prot, mask);
820f8a95309SArnaldo Carvalho de Melo }
8217e2ed097SArnaldo Carvalho de Melo 
822602ad878SArnaldo Carvalho de Melo int perf_evlist__create_maps(struct perf_evlist *evlist, struct target *target)
8237e2ed097SArnaldo Carvalho de Melo {
824b809ac10SNamhyung Kim 	evlist->threads = thread_map__new_str(target->pid, target->tid,
825b809ac10SNamhyung Kim 					      target->uid);
8267e2ed097SArnaldo Carvalho de Melo 
8277e2ed097SArnaldo Carvalho de Melo 	if (evlist->threads == NULL)
8287e2ed097SArnaldo Carvalho de Melo 		return -1;
8297e2ed097SArnaldo Carvalho de Melo 
8309c105fbcSDongsheng Yang 	if (target__uses_dummy_map(target))
831d1cb9fceSNamhyung Kim 		evlist->cpus = cpu_map__dummy_new();
832879d77d0SNamhyung Kim 	else
833879d77d0SNamhyung Kim 		evlist->cpus = cpu_map__new(target->cpu_list);
8347e2ed097SArnaldo Carvalho de Melo 
8357e2ed097SArnaldo Carvalho de Melo 	if (evlist->cpus == NULL)
8367e2ed097SArnaldo Carvalho de Melo 		goto out_delete_threads;
8377e2ed097SArnaldo Carvalho de Melo 
8387e2ed097SArnaldo Carvalho de Melo 	return 0;
8397e2ed097SArnaldo Carvalho de Melo 
8407e2ed097SArnaldo Carvalho de Melo out_delete_threads:
8417e2ed097SArnaldo Carvalho de Melo 	thread_map__delete(evlist->threads);
8427e2ed097SArnaldo Carvalho de Melo 	return -1;
8437e2ed097SArnaldo Carvalho de Melo }
8447e2ed097SArnaldo Carvalho de Melo 
8451491a632SArnaldo Carvalho de Melo int perf_evlist__apply_filters(struct perf_evlist *evlist)
8460a102479SFrederic Weisbecker {
8470a102479SFrederic Weisbecker 	struct perf_evsel *evsel;
848745cefc5SArnaldo Carvalho de Melo 	int err = 0;
849745cefc5SArnaldo Carvalho de Melo 	const int ncpus = cpu_map__nr(evlist->cpus),
850b3a319d5SNamhyung Kim 		  nthreads = thread_map__nr(evlist->threads);
8510a102479SFrederic Weisbecker 
8520a102479SFrederic Weisbecker 	list_for_each_entry(evsel, &evlist->entries, node) {
853745cefc5SArnaldo Carvalho de Melo 		if (evsel->filter == NULL)
8540a102479SFrederic Weisbecker 			continue;
855745cefc5SArnaldo Carvalho de Melo 
856745cefc5SArnaldo Carvalho de Melo 		err = perf_evsel__set_filter(evsel, ncpus, nthreads, evsel->filter);
8570a102479SFrederic Weisbecker 		if (err)
858745cefc5SArnaldo Carvalho de Melo 			break;
8590a102479SFrederic Weisbecker 	}
8600a102479SFrederic Weisbecker 
861745cefc5SArnaldo Carvalho de Melo 	return err;
862745cefc5SArnaldo Carvalho de Melo }
863745cefc5SArnaldo Carvalho de Melo 
864745cefc5SArnaldo Carvalho de Melo int perf_evlist__set_filter(struct perf_evlist *evlist, const char *filter)
865745cefc5SArnaldo Carvalho de Melo {
866745cefc5SArnaldo Carvalho de Melo 	struct perf_evsel *evsel;
867745cefc5SArnaldo Carvalho de Melo 	int err = 0;
868745cefc5SArnaldo Carvalho de Melo 	const int ncpus = cpu_map__nr(evlist->cpus),
869b3a319d5SNamhyung Kim 		  nthreads = thread_map__nr(evlist->threads);
870745cefc5SArnaldo Carvalho de Melo 
871745cefc5SArnaldo Carvalho de Melo 	list_for_each_entry(evsel, &evlist->entries, node) {
872745cefc5SArnaldo Carvalho de Melo 		err = perf_evsel__set_filter(evsel, ncpus, nthreads, filter);
873745cefc5SArnaldo Carvalho de Melo 		if (err)
874745cefc5SArnaldo Carvalho de Melo 			break;
875745cefc5SArnaldo Carvalho de Melo 	}
876745cefc5SArnaldo Carvalho de Melo 
877745cefc5SArnaldo Carvalho de Melo 	return err;
8780a102479SFrederic Weisbecker }
87974429964SFrederic Weisbecker 
8800c21f736SArnaldo Carvalho de Melo bool perf_evlist__valid_sample_type(struct perf_evlist *evlist)
88174429964SFrederic Weisbecker {
88275562573SAdrian Hunter 	struct perf_evsel *pos;
883c2a70653SArnaldo Carvalho de Melo 
88475562573SAdrian Hunter 	if (evlist->nr_entries == 1)
88575562573SAdrian Hunter 		return true;
88675562573SAdrian Hunter 
88775562573SAdrian Hunter 	if (evlist->id_pos < 0 || evlist->is_pos < 0)
88875562573SAdrian Hunter 		return false;
88975562573SAdrian Hunter 
89075562573SAdrian Hunter 	list_for_each_entry(pos, &evlist->entries, node) {
89175562573SAdrian Hunter 		if (pos->id_pos != evlist->id_pos ||
89275562573SAdrian Hunter 		    pos->is_pos != evlist->is_pos)
893c2a70653SArnaldo Carvalho de Melo 			return false;
89474429964SFrederic Weisbecker 	}
89574429964SFrederic Weisbecker 
896c2a70653SArnaldo Carvalho de Melo 	return true;
897c2a70653SArnaldo Carvalho de Melo }
898c2a70653SArnaldo Carvalho de Melo 
89975562573SAdrian Hunter u64 __perf_evlist__combined_sample_type(struct perf_evlist *evlist)
900c2a70653SArnaldo Carvalho de Melo {
90175562573SAdrian Hunter 	struct perf_evsel *evsel;
90275562573SAdrian Hunter 
90375562573SAdrian Hunter 	if (evlist->combined_sample_type)
90475562573SAdrian Hunter 		return evlist->combined_sample_type;
90575562573SAdrian Hunter 
90675562573SAdrian Hunter 	list_for_each_entry(evsel, &evlist->entries, node)
90775562573SAdrian Hunter 		evlist->combined_sample_type |= evsel->attr.sample_type;
90875562573SAdrian Hunter 
90975562573SAdrian Hunter 	return evlist->combined_sample_type;
91075562573SAdrian Hunter }
91175562573SAdrian Hunter 
91275562573SAdrian Hunter u64 perf_evlist__combined_sample_type(struct perf_evlist *evlist)
91375562573SAdrian Hunter {
91475562573SAdrian Hunter 	evlist->combined_sample_type = 0;
91575562573SAdrian Hunter 	return __perf_evlist__combined_sample_type(evlist);
916c2a70653SArnaldo Carvalho de Melo }
917c2a70653SArnaldo Carvalho de Melo 
9189ede473cSJiri Olsa bool perf_evlist__valid_read_format(struct perf_evlist *evlist)
9199ede473cSJiri Olsa {
9209ede473cSJiri Olsa 	struct perf_evsel *first = perf_evlist__first(evlist), *pos = first;
9219ede473cSJiri Olsa 	u64 read_format = first->attr.read_format;
9229ede473cSJiri Olsa 	u64 sample_type = first->attr.sample_type;
9239ede473cSJiri Olsa 
9249ede473cSJiri Olsa 	list_for_each_entry_continue(pos, &evlist->entries, node) {
9259ede473cSJiri Olsa 		if (read_format != pos->attr.read_format)
9269ede473cSJiri Olsa 			return false;
9279ede473cSJiri Olsa 	}
9289ede473cSJiri Olsa 
9299ede473cSJiri Olsa 	/* PERF_SAMPLE_READ imples PERF_FORMAT_ID. */
9309ede473cSJiri Olsa 	if ((sample_type & PERF_SAMPLE_READ) &&
9319ede473cSJiri Olsa 	    !(read_format & PERF_FORMAT_ID)) {
9329ede473cSJiri Olsa 		return false;
9339ede473cSJiri Olsa 	}
9349ede473cSJiri Olsa 
9359ede473cSJiri Olsa 	return true;
9369ede473cSJiri Olsa }
9379ede473cSJiri Olsa 
9389ede473cSJiri Olsa u64 perf_evlist__read_format(struct perf_evlist *evlist)
9399ede473cSJiri Olsa {
9409ede473cSJiri Olsa 	struct perf_evsel *first = perf_evlist__first(evlist);
9419ede473cSJiri Olsa 	return first->attr.read_format;
9429ede473cSJiri Olsa }
9439ede473cSJiri Olsa 
9440c21f736SArnaldo Carvalho de Melo u16 perf_evlist__id_hdr_size(struct perf_evlist *evlist)
94581e36bffSArnaldo Carvalho de Melo {
9460c21f736SArnaldo Carvalho de Melo 	struct perf_evsel *first = perf_evlist__first(evlist);
94781e36bffSArnaldo Carvalho de Melo 	struct perf_sample *data;
94881e36bffSArnaldo Carvalho de Melo 	u64 sample_type;
94981e36bffSArnaldo Carvalho de Melo 	u16 size = 0;
95081e36bffSArnaldo Carvalho de Melo 
95181e36bffSArnaldo Carvalho de Melo 	if (!first->attr.sample_id_all)
95281e36bffSArnaldo Carvalho de Melo 		goto out;
95381e36bffSArnaldo Carvalho de Melo 
95481e36bffSArnaldo Carvalho de Melo 	sample_type = first->attr.sample_type;
95581e36bffSArnaldo Carvalho de Melo 
95681e36bffSArnaldo Carvalho de Melo 	if (sample_type & PERF_SAMPLE_TID)
95781e36bffSArnaldo Carvalho de Melo 		size += sizeof(data->tid) * 2;
95881e36bffSArnaldo Carvalho de Melo 
95981e36bffSArnaldo Carvalho de Melo        if (sample_type & PERF_SAMPLE_TIME)
96081e36bffSArnaldo Carvalho de Melo 		size += sizeof(data->time);
96181e36bffSArnaldo Carvalho de Melo 
96281e36bffSArnaldo Carvalho de Melo 	if (sample_type & PERF_SAMPLE_ID)
96381e36bffSArnaldo Carvalho de Melo 		size += sizeof(data->id);
96481e36bffSArnaldo Carvalho de Melo 
96581e36bffSArnaldo Carvalho de Melo 	if (sample_type & PERF_SAMPLE_STREAM_ID)
96681e36bffSArnaldo Carvalho de Melo 		size += sizeof(data->stream_id);
96781e36bffSArnaldo Carvalho de Melo 
96881e36bffSArnaldo Carvalho de Melo 	if (sample_type & PERF_SAMPLE_CPU)
96981e36bffSArnaldo Carvalho de Melo 		size += sizeof(data->cpu) * 2;
97075562573SAdrian Hunter 
97175562573SAdrian Hunter 	if (sample_type & PERF_SAMPLE_IDENTIFIER)
97275562573SAdrian Hunter 		size += sizeof(data->id);
97381e36bffSArnaldo Carvalho de Melo out:
97481e36bffSArnaldo Carvalho de Melo 	return size;
97581e36bffSArnaldo Carvalho de Melo }
97681e36bffSArnaldo Carvalho de Melo 
9770c21f736SArnaldo Carvalho de Melo bool perf_evlist__valid_sample_id_all(struct perf_evlist *evlist)
978c2a70653SArnaldo Carvalho de Melo {
9790c21f736SArnaldo Carvalho de Melo 	struct perf_evsel *first = perf_evlist__first(evlist), *pos = first;
980c2a70653SArnaldo Carvalho de Melo 
981c2a70653SArnaldo Carvalho de Melo 	list_for_each_entry_continue(pos, &evlist->entries, node) {
982c2a70653SArnaldo Carvalho de Melo 		if (first->attr.sample_id_all != pos->attr.sample_id_all)
983c2a70653SArnaldo Carvalho de Melo 			return false;
984c2a70653SArnaldo Carvalho de Melo 	}
985c2a70653SArnaldo Carvalho de Melo 
986c2a70653SArnaldo Carvalho de Melo 	return true;
98774429964SFrederic Weisbecker }
98874429964SFrederic Weisbecker 
9890c21f736SArnaldo Carvalho de Melo bool perf_evlist__sample_id_all(struct perf_evlist *evlist)
99074429964SFrederic Weisbecker {
9910c21f736SArnaldo Carvalho de Melo 	struct perf_evsel *first = perf_evlist__first(evlist);
992c2a70653SArnaldo Carvalho de Melo 	return first->attr.sample_id_all;
99374429964SFrederic Weisbecker }
99481cce8deSArnaldo Carvalho de Melo 
99581cce8deSArnaldo Carvalho de Melo void perf_evlist__set_selected(struct perf_evlist *evlist,
99681cce8deSArnaldo Carvalho de Melo 			       struct perf_evsel *evsel)
99781cce8deSArnaldo Carvalho de Melo {
99881cce8deSArnaldo Carvalho de Melo 	evlist->selected = evsel;
99981cce8deSArnaldo Carvalho de Melo }
1000727ab04eSArnaldo Carvalho de Melo 
1001a74b4b66SNamhyung Kim void perf_evlist__close(struct perf_evlist *evlist)
1002a74b4b66SNamhyung Kim {
1003a74b4b66SNamhyung Kim 	struct perf_evsel *evsel;
1004a74b4b66SNamhyung Kim 	int ncpus = cpu_map__nr(evlist->cpus);
1005a74b4b66SNamhyung Kim 	int nthreads = thread_map__nr(evlist->threads);
1006a74b4b66SNamhyung Kim 
1007a74b4b66SNamhyung Kim 	list_for_each_entry_reverse(evsel, &evlist->entries, node)
1008a74b4b66SNamhyung Kim 		perf_evsel__close(evsel, ncpus, nthreads);
1009a74b4b66SNamhyung Kim }
1010a74b4b66SNamhyung Kim 
10116a4bb04cSJiri Olsa int perf_evlist__open(struct perf_evlist *evlist)
1012727ab04eSArnaldo Carvalho de Melo {
10136a4bb04cSJiri Olsa 	struct perf_evsel *evsel;
1014a74b4b66SNamhyung Kim 	int err;
1015727ab04eSArnaldo Carvalho de Melo 
1016733cd2feSAdrian Hunter 	perf_evlist__update_id_pos(evlist);
1017733cd2feSAdrian Hunter 
1018727ab04eSArnaldo Carvalho de Melo 	list_for_each_entry(evsel, &evlist->entries, node) {
10196a4bb04cSJiri Olsa 		err = perf_evsel__open(evsel, evlist->cpus, evlist->threads);
1020727ab04eSArnaldo Carvalho de Melo 		if (err < 0)
1021727ab04eSArnaldo Carvalho de Melo 			goto out_err;
1022727ab04eSArnaldo Carvalho de Melo 	}
1023727ab04eSArnaldo Carvalho de Melo 
1024727ab04eSArnaldo Carvalho de Melo 	return 0;
1025727ab04eSArnaldo Carvalho de Melo out_err:
1026a74b4b66SNamhyung Kim 	perf_evlist__close(evlist);
102741c21a68SNamhyung Kim 	errno = -err;
1028727ab04eSArnaldo Carvalho de Melo 	return err;
1029727ab04eSArnaldo Carvalho de Melo }
103035b9d88eSArnaldo Carvalho de Melo 
1031602ad878SArnaldo Carvalho de Melo int perf_evlist__prepare_workload(struct perf_evlist *evlist, struct target *target,
103255e162eaSNamhyung Kim 				  const char *argv[], bool pipe_output,
1033735f7e0bSArnaldo Carvalho de Melo 				  void (*exec_error)(int signo, siginfo_t *info, void *ucontext))
103435b9d88eSArnaldo Carvalho de Melo {
103535b9d88eSArnaldo Carvalho de Melo 	int child_ready_pipe[2], go_pipe[2];
103635b9d88eSArnaldo Carvalho de Melo 	char bf;
103735b9d88eSArnaldo Carvalho de Melo 
103835b9d88eSArnaldo Carvalho de Melo 	if (pipe(child_ready_pipe) < 0) {
103935b9d88eSArnaldo Carvalho de Melo 		perror("failed to create 'ready' pipe");
104035b9d88eSArnaldo Carvalho de Melo 		return -1;
104135b9d88eSArnaldo Carvalho de Melo 	}
104235b9d88eSArnaldo Carvalho de Melo 
104335b9d88eSArnaldo Carvalho de Melo 	if (pipe(go_pipe) < 0) {
104435b9d88eSArnaldo Carvalho de Melo 		perror("failed to create 'go' pipe");
104535b9d88eSArnaldo Carvalho de Melo 		goto out_close_ready_pipe;
104635b9d88eSArnaldo Carvalho de Melo 	}
104735b9d88eSArnaldo Carvalho de Melo 
104835b9d88eSArnaldo Carvalho de Melo 	evlist->workload.pid = fork();
104935b9d88eSArnaldo Carvalho de Melo 	if (evlist->workload.pid < 0) {
105035b9d88eSArnaldo Carvalho de Melo 		perror("failed to fork");
105135b9d88eSArnaldo Carvalho de Melo 		goto out_close_pipes;
105235b9d88eSArnaldo Carvalho de Melo 	}
105335b9d88eSArnaldo Carvalho de Melo 
105435b9d88eSArnaldo Carvalho de Melo 	if (!evlist->workload.pid) {
1055119fa3c9SNamhyung Kim 		if (pipe_output)
105635b9d88eSArnaldo Carvalho de Melo 			dup2(2, 1);
105735b9d88eSArnaldo Carvalho de Melo 
10580817df08SDavid Ahern 		signal(SIGTERM, SIG_DFL);
10590817df08SDavid Ahern 
106035b9d88eSArnaldo Carvalho de Melo 		close(child_ready_pipe[0]);
106135b9d88eSArnaldo Carvalho de Melo 		close(go_pipe[1]);
106235b9d88eSArnaldo Carvalho de Melo 		fcntl(go_pipe[0], F_SETFD, FD_CLOEXEC);
106335b9d88eSArnaldo Carvalho de Melo 
106435b9d88eSArnaldo Carvalho de Melo 		/*
106535b9d88eSArnaldo Carvalho de Melo 		 * Tell the parent we're ready to go
106635b9d88eSArnaldo Carvalho de Melo 		 */
106735b9d88eSArnaldo Carvalho de Melo 		close(child_ready_pipe[1]);
106835b9d88eSArnaldo Carvalho de Melo 
106935b9d88eSArnaldo Carvalho de Melo 		/*
107035b9d88eSArnaldo Carvalho de Melo 		 * Wait until the parent tells us to go.
107135b9d88eSArnaldo Carvalho de Melo 		 */
107235b9d88eSArnaldo Carvalho de Melo 		if (read(go_pipe[0], &bf, 1) == -1)
107335b9d88eSArnaldo Carvalho de Melo 			perror("unable to read pipe");
107435b9d88eSArnaldo Carvalho de Melo 
107535b9d88eSArnaldo Carvalho de Melo 		execvp(argv[0], (char **)argv);
107635b9d88eSArnaldo Carvalho de Melo 
1077735f7e0bSArnaldo Carvalho de Melo 		if (exec_error) {
1078f33cbe72SArnaldo Carvalho de Melo 			union sigval val;
1079f33cbe72SArnaldo Carvalho de Melo 
1080f33cbe72SArnaldo Carvalho de Melo 			val.sival_int = errno;
1081f33cbe72SArnaldo Carvalho de Melo 			if (sigqueue(getppid(), SIGUSR1, val))
108235b9d88eSArnaldo Carvalho de Melo 				perror(argv[0]);
1083f33cbe72SArnaldo Carvalho de Melo 		} else
1084f33cbe72SArnaldo Carvalho de Melo 			perror(argv[0]);
108535b9d88eSArnaldo Carvalho de Melo 		exit(-1);
108635b9d88eSArnaldo Carvalho de Melo 	}
108735b9d88eSArnaldo Carvalho de Melo 
1088735f7e0bSArnaldo Carvalho de Melo 	if (exec_error) {
1089735f7e0bSArnaldo Carvalho de Melo 		struct sigaction act = {
1090735f7e0bSArnaldo Carvalho de Melo 			.sa_flags     = SA_SIGINFO,
1091735f7e0bSArnaldo Carvalho de Melo 			.sa_sigaction = exec_error,
1092735f7e0bSArnaldo Carvalho de Melo 		};
1093735f7e0bSArnaldo Carvalho de Melo 		sigaction(SIGUSR1, &act, NULL);
1094735f7e0bSArnaldo Carvalho de Melo 	}
1095735f7e0bSArnaldo Carvalho de Melo 
1096602ad878SArnaldo Carvalho de Melo 	if (target__none(target))
109735b9d88eSArnaldo Carvalho de Melo 		evlist->threads->map[0] = evlist->workload.pid;
109835b9d88eSArnaldo Carvalho de Melo 
109935b9d88eSArnaldo Carvalho de Melo 	close(child_ready_pipe[1]);
110035b9d88eSArnaldo Carvalho de Melo 	close(go_pipe[0]);
110135b9d88eSArnaldo Carvalho de Melo 	/*
110235b9d88eSArnaldo Carvalho de Melo 	 * wait for child to settle
110335b9d88eSArnaldo Carvalho de Melo 	 */
110435b9d88eSArnaldo Carvalho de Melo 	if (read(child_ready_pipe[0], &bf, 1) == -1) {
110535b9d88eSArnaldo Carvalho de Melo 		perror("unable to read pipe");
110635b9d88eSArnaldo Carvalho de Melo 		goto out_close_pipes;
110735b9d88eSArnaldo Carvalho de Melo 	}
110835b9d88eSArnaldo Carvalho de Melo 
1109bcf3145fSNamhyung Kim 	fcntl(go_pipe[1], F_SETFD, FD_CLOEXEC);
111035b9d88eSArnaldo Carvalho de Melo 	evlist->workload.cork_fd = go_pipe[1];
111135b9d88eSArnaldo Carvalho de Melo 	close(child_ready_pipe[0]);
111235b9d88eSArnaldo Carvalho de Melo 	return 0;
111335b9d88eSArnaldo Carvalho de Melo 
111435b9d88eSArnaldo Carvalho de Melo out_close_pipes:
111535b9d88eSArnaldo Carvalho de Melo 	close(go_pipe[0]);
111635b9d88eSArnaldo Carvalho de Melo 	close(go_pipe[1]);
111735b9d88eSArnaldo Carvalho de Melo out_close_ready_pipe:
111835b9d88eSArnaldo Carvalho de Melo 	close(child_ready_pipe[0]);
111935b9d88eSArnaldo Carvalho de Melo 	close(child_ready_pipe[1]);
112035b9d88eSArnaldo Carvalho de Melo 	return -1;
112135b9d88eSArnaldo Carvalho de Melo }
112235b9d88eSArnaldo Carvalho de Melo 
112335b9d88eSArnaldo Carvalho de Melo int perf_evlist__start_workload(struct perf_evlist *evlist)
112435b9d88eSArnaldo Carvalho de Melo {
112535b9d88eSArnaldo Carvalho de Melo 	if (evlist->workload.cork_fd > 0) {
1126b3824404SDavid Ahern 		char bf = 0;
1127bcf3145fSNamhyung Kim 		int ret;
112835b9d88eSArnaldo Carvalho de Melo 		/*
112935b9d88eSArnaldo Carvalho de Melo 		 * Remove the cork, let it rip!
113035b9d88eSArnaldo Carvalho de Melo 		 */
1131bcf3145fSNamhyung Kim 		ret = write(evlist->workload.cork_fd, &bf, 1);
1132bcf3145fSNamhyung Kim 		if (ret < 0)
1133bcf3145fSNamhyung Kim 			perror("enable to write to pipe");
1134bcf3145fSNamhyung Kim 
1135bcf3145fSNamhyung Kim 		close(evlist->workload.cork_fd);
1136bcf3145fSNamhyung Kim 		return ret;
113735b9d88eSArnaldo Carvalho de Melo 	}
113835b9d88eSArnaldo Carvalho de Melo 
113935b9d88eSArnaldo Carvalho de Melo 	return 0;
114035b9d88eSArnaldo Carvalho de Melo }
1141cb0b29e0SArnaldo Carvalho de Melo 
1142a3f698feSArnaldo Carvalho de Melo int perf_evlist__parse_sample(struct perf_evlist *evlist, union perf_event *event,
11430807d2d8SArnaldo Carvalho de Melo 			      struct perf_sample *sample)
1144cb0b29e0SArnaldo Carvalho de Melo {
114575562573SAdrian Hunter 	struct perf_evsel *evsel = perf_evlist__event2evsel(evlist, event);
114675562573SAdrian Hunter 
114775562573SAdrian Hunter 	if (!evsel)
114875562573SAdrian Hunter 		return -EFAULT;
11490807d2d8SArnaldo Carvalho de Melo 	return perf_evsel__parse_sample(evsel, event, sample);
1150cb0b29e0SArnaldo Carvalho de Melo }
115178f067b3SArnaldo Carvalho de Melo 
115278f067b3SArnaldo Carvalho de Melo size_t perf_evlist__fprintf(struct perf_evlist *evlist, FILE *fp)
115378f067b3SArnaldo Carvalho de Melo {
115478f067b3SArnaldo Carvalho de Melo 	struct perf_evsel *evsel;
115578f067b3SArnaldo Carvalho de Melo 	size_t printed = 0;
115678f067b3SArnaldo Carvalho de Melo 
115778f067b3SArnaldo Carvalho de Melo 	list_for_each_entry(evsel, &evlist->entries, node) {
115878f067b3SArnaldo Carvalho de Melo 		printed += fprintf(fp, "%s%s", evsel->idx ? ", " : "",
115978f067b3SArnaldo Carvalho de Melo 				   perf_evsel__name(evsel));
116078f067b3SArnaldo Carvalho de Melo 	}
116178f067b3SArnaldo Carvalho de Melo 
1162b2222139SDavidlohr Bueso 	return printed + fprintf(fp, "\n");
116378f067b3SArnaldo Carvalho de Melo }
11646ef068cbSArnaldo Carvalho de Melo 
11656ef068cbSArnaldo Carvalho de Melo int perf_evlist__strerror_tp(struct perf_evlist *evlist __maybe_unused,
11666ef068cbSArnaldo Carvalho de Melo 			     int err, char *buf, size_t size)
11676ef068cbSArnaldo Carvalho de Melo {
11686ef068cbSArnaldo Carvalho de Melo 	char sbuf[128];
11696ef068cbSArnaldo Carvalho de Melo 
11706ef068cbSArnaldo Carvalho de Melo 	switch (err) {
11716ef068cbSArnaldo Carvalho de Melo 	case ENOENT:
11726ef068cbSArnaldo Carvalho de Melo 		scnprintf(buf, size, "%s",
11736ef068cbSArnaldo Carvalho de Melo 			  "Error:\tUnable to find debugfs\n"
11746ef068cbSArnaldo Carvalho de Melo 			  "Hint:\tWas your kernel was compiled with debugfs support?\n"
11756ef068cbSArnaldo Carvalho de Melo 			  "Hint:\tIs the debugfs filesystem mounted?\n"
11766ef068cbSArnaldo Carvalho de Melo 			  "Hint:\tTry 'sudo mount -t debugfs nodev /sys/kernel/debug'");
11776ef068cbSArnaldo Carvalho de Melo 		break;
11786ef068cbSArnaldo Carvalho de Melo 	case EACCES:
11796ef068cbSArnaldo Carvalho de Melo 		scnprintf(buf, size,
11806ef068cbSArnaldo Carvalho de Melo 			  "Error:\tNo permissions to read %s/tracing/events/raw_syscalls\n"
11816ef068cbSArnaldo Carvalho de Melo 			  "Hint:\tTry 'sudo mount -o remount,mode=755 %s'\n",
11826ef068cbSArnaldo Carvalho de Melo 			  debugfs_mountpoint, debugfs_mountpoint);
11836ef068cbSArnaldo Carvalho de Melo 		break;
11846ef068cbSArnaldo Carvalho de Melo 	default:
11856ef068cbSArnaldo Carvalho de Melo 		scnprintf(buf, size, "%s", strerror_r(err, sbuf, sizeof(sbuf)));
11866ef068cbSArnaldo Carvalho de Melo 		break;
11876ef068cbSArnaldo Carvalho de Melo 	}
11886ef068cbSArnaldo Carvalho de Melo 
11896ef068cbSArnaldo Carvalho de Melo 	return 0;
11906ef068cbSArnaldo Carvalho de Melo }
1191a8f23d8fSArnaldo Carvalho de Melo 
1192a8f23d8fSArnaldo Carvalho de Melo int perf_evlist__strerror_open(struct perf_evlist *evlist __maybe_unused,
1193a8f23d8fSArnaldo Carvalho de Melo 			       int err, char *buf, size_t size)
1194a8f23d8fSArnaldo Carvalho de Melo {
1195a8f23d8fSArnaldo Carvalho de Melo 	int printed, value;
1196a8f23d8fSArnaldo Carvalho de Melo 	char sbuf[128], *emsg = strerror_r(err, sbuf, sizeof(sbuf));
1197a8f23d8fSArnaldo Carvalho de Melo 
1198a8f23d8fSArnaldo Carvalho de Melo 	switch (err) {
1199a8f23d8fSArnaldo Carvalho de Melo 	case EACCES:
1200a8f23d8fSArnaldo Carvalho de Melo 	case EPERM:
1201a8f23d8fSArnaldo Carvalho de Melo 		printed = scnprintf(buf, size,
1202a8f23d8fSArnaldo Carvalho de Melo 				    "Error:\t%s.\n"
1203a8f23d8fSArnaldo Carvalho de Melo 				    "Hint:\tCheck /proc/sys/kernel/perf_event_paranoid setting.", emsg);
1204a8f23d8fSArnaldo Carvalho de Melo 
12051a47245dSAdrian Hunter 		value = perf_event_paranoid();
1206a8f23d8fSArnaldo Carvalho de Melo 
1207a8f23d8fSArnaldo Carvalho de Melo 		printed += scnprintf(buf + printed, size - printed, "\nHint:\t");
1208a8f23d8fSArnaldo Carvalho de Melo 
1209a8f23d8fSArnaldo Carvalho de Melo 		if (value >= 2) {
1210a8f23d8fSArnaldo Carvalho de Melo 			printed += scnprintf(buf + printed, size - printed,
1211a8f23d8fSArnaldo Carvalho de Melo 					     "For your workloads it needs to be <= 1\nHint:\t");
1212a8f23d8fSArnaldo Carvalho de Melo 		}
1213a8f23d8fSArnaldo Carvalho de Melo 		printed += scnprintf(buf + printed, size - printed,
1214a8f23d8fSArnaldo Carvalho de Melo 				     "For system wide tracing it needs to be set to -1");
1215a8f23d8fSArnaldo Carvalho de Melo 
1216a8f23d8fSArnaldo Carvalho de Melo 		printed += scnprintf(buf + printed, size - printed,
1217a8f23d8fSArnaldo Carvalho de Melo 				    ".\nHint:\tThe current value is %d.", value);
1218a8f23d8fSArnaldo Carvalho de Melo 		break;
1219a8f23d8fSArnaldo Carvalho de Melo 	default:
1220a8f23d8fSArnaldo Carvalho de Melo 		scnprintf(buf, size, "%s", emsg);
1221a8f23d8fSArnaldo Carvalho de Melo 		break;
1222a8f23d8fSArnaldo Carvalho de Melo 	}
1223a8f23d8fSArnaldo Carvalho de Melo 
1224a8f23d8fSArnaldo Carvalho de Melo 	return 0;
1225a8f23d8fSArnaldo Carvalho de Melo }
1226a025e4f0SAdrian Hunter 
1227a025e4f0SAdrian Hunter void perf_evlist__to_front(struct perf_evlist *evlist,
1228a025e4f0SAdrian Hunter 			   struct perf_evsel *move_evsel)
1229a025e4f0SAdrian Hunter {
1230a025e4f0SAdrian Hunter 	struct perf_evsel *evsel, *n;
1231a025e4f0SAdrian Hunter 	LIST_HEAD(move);
1232a025e4f0SAdrian Hunter 
1233a025e4f0SAdrian Hunter 	if (move_evsel == perf_evlist__first(evlist))
1234a025e4f0SAdrian Hunter 		return;
1235a025e4f0SAdrian Hunter 
1236a025e4f0SAdrian Hunter 	list_for_each_entry_safe(evsel, n, &evlist->entries, node) {
1237a025e4f0SAdrian Hunter 		if (evsel->leader == move_evsel->leader)
1238a025e4f0SAdrian Hunter 			list_move_tail(&evsel->node, &move);
1239a025e4f0SAdrian Hunter 	}
1240a025e4f0SAdrian Hunter 
1241a025e4f0SAdrian Hunter 	list_splice(&move, &evlist->entries);
1242a025e4f0SAdrian Hunter }
1243