xref: /openbmc/linux/tools/perf/util/evlist.c (revision 1dbfa938)
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"
1085c66be1SBorislav Petkov #include <lk/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 {
104ef1d1af2SArnaldo Carvalho de Melo 	free(evlist->mmap);
105ef1d1af2SArnaldo Carvalho de Melo 	free(evlist->pollfd);
106ef1d1af2SArnaldo Carvalho de Melo 	evlist->mmap = NULL;
107ef1d1af2SArnaldo Carvalho de Melo 	evlist->pollfd = NULL;
108ef1d1af2SArnaldo Carvalho de Melo }
109ef1d1af2SArnaldo Carvalho de Melo 
110361c99a6SArnaldo Carvalho de Melo void perf_evlist__delete(struct perf_evlist *evlist)
111361c99a6SArnaldo Carvalho de Melo {
112361c99a6SArnaldo Carvalho de Melo 	perf_evlist__purge(evlist);
113ef1d1af2SArnaldo Carvalho de Melo 	perf_evlist__exit(evlist);
114361c99a6SArnaldo Carvalho de Melo 	free(evlist);
115361c99a6SArnaldo Carvalho de Melo }
116361c99a6SArnaldo Carvalho de Melo 
117361c99a6SArnaldo Carvalho de Melo void perf_evlist__add(struct perf_evlist *evlist, struct perf_evsel *entry)
118361c99a6SArnaldo Carvalho de Melo {
119361c99a6SArnaldo Carvalho de Melo 	list_add_tail(&entry->node, &evlist->entries);
120ef503831SArnaldo Carvalho de Melo 	entry->idx = evlist->nr_entries;
121ef503831SArnaldo Carvalho de Melo 
12275562573SAdrian Hunter 	if (!evlist->nr_entries++)
12375562573SAdrian Hunter 		perf_evlist__set_id_pos(evlist);
124361c99a6SArnaldo Carvalho de Melo }
125361c99a6SArnaldo Carvalho de Melo 
1260529bc1fSJiri Olsa void perf_evlist__splice_list_tail(struct perf_evlist *evlist,
12750d08e47SArnaldo Carvalho de Melo 				   struct list_head *list,
12850d08e47SArnaldo Carvalho de Melo 				   int nr_entries)
12950d08e47SArnaldo Carvalho de Melo {
13075562573SAdrian Hunter 	bool set_id_pos = !evlist->nr_entries;
13175562573SAdrian Hunter 
13250d08e47SArnaldo Carvalho de Melo 	list_splice_tail(list, &evlist->entries);
13350d08e47SArnaldo Carvalho de Melo 	evlist->nr_entries += nr_entries;
13475562573SAdrian Hunter 	if (set_id_pos)
13575562573SAdrian Hunter 		perf_evlist__set_id_pos(evlist);
13650d08e47SArnaldo Carvalho de Melo }
13750d08e47SArnaldo Carvalho de Melo 
13863dab225SArnaldo Carvalho de Melo void __perf_evlist__set_leader(struct list_head *list)
13963dab225SArnaldo Carvalho de Melo {
14063dab225SArnaldo Carvalho de Melo 	struct perf_evsel *evsel, *leader;
14163dab225SArnaldo Carvalho de Melo 
14263dab225SArnaldo Carvalho de Melo 	leader = list_entry(list->next, struct perf_evsel, node);
14397f63e4aSNamhyung Kim 	evsel = list_entry(list->prev, struct perf_evsel, node);
14497f63e4aSNamhyung Kim 
14597f63e4aSNamhyung Kim 	leader->nr_members = evsel->idx - leader->idx + 1;
14663dab225SArnaldo Carvalho de Melo 
14763dab225SArnaldo Carvalho de Melo 	list_for_each_entry(evsel, list, node) {
14863dab225SArnaldo Carvalho de Melo 		evsel->leader = leader;
14963dab225SArnaldo Carvalho de Melo 	}
15063dab225SArnaldo Carvalho de Melo }
15163dab225SArnaldo Carvalho de Melo 
15263dab225SArnaldo Carvalho de Melo void perf_evlist__set_leader(struct perf_evlist *evlist)
1536a4bb04cSJiri Olsa {
15497f63e4aSNamhyung Kim 	if (evlist->nr_entries) {
15597f63e4aSNamhyung Kim 		evlist->nr_groups = evlist->nr_entries > 1 ? 1 : 0;
15663dab225SArnaldo Carvalho de Melo 		__perf_evlist__set_leader(&evlist->entries);
1576a4bb04cSJiri Olsa 	}
15897f63e4aSNamhyung Kim }
1596a4bb04cSJiri Olsa 
160361c99a6SArnaldo Carvalho de Melo int perf_evlist__add_default(struct perf_evlist *evlist)
161361c99a6SArnaldo Carvalho de Melo {
162361c99a6SArnaldo Carvalho de Melo 	struct perf_event_attr attr = {
163361c99a6SArnaldo Carvalho de Melo 		.type = PERF_TYPE_HARDWARE,
164361c99a6SArnaldo Carvalho de Melo 		.config = PERF_COUNT_HW_CPU_CYCLES,
165361c99a6SArnaldo Carvalho de Melo 	};
1661aed2671SJoerg Roedel 	struct perf_evsel *evsel;
167361c99a6SArnaldo Carvalho de Melo 
1681aed2671SJoerg Roedel 	event_attr_init(&attr);
1691aed2671SJoerg Roedel 
170ef503831SArnaldo Carvalho de Melo 	evsel = perf_evsel__new(&attr);
171361c99a6SArnaldo Carvalho de Melo 	if (evsel == NULL)
172cc2d86b0SStephane Eranian 		goto error;
173cc2d86b0SStephane Eranian 
174cc2d86b0SStephane Eranian 	/* use strdup() because free(evsel) assumes name is allocated */
175cc2d86b0SStephane Eranian 	evsel->name = strdup("cycles");
176cc2d86b0SStephane Eranian 	if (!evsel->name)
177cc2d86b0SStephane Eranian 		goto error_free;
178361c99a6SArnaldo Carvalho de Melo 
179361c99a6SArnaldo Carvalho de Melo 	perf_evlist__add(evlist, evsel);
180361c99a6SArnaldo Carvalho de Melo 	return 0;
181cc2d86b0SStephane Eranian error_free:
182cc2d86b0SStephane Eranian 	perf_evsel__delete(evsel);
183cc2d86b0SStephane Eranian error:
184cc2d86b0SStephane Eranian 	return -ENOMEM;
185361c99a6SArnaldo Carvalho de Melo }
1865c581041SArnaldo Carvalho de Melo 
187e60fc847SArnaldo Carvalho de Melo static int perf_evlist__add_attrs(struct perf_evlist *evlist,
18850d08e47SArnaldo Carvalho de Melo 				  struct perf_event_attr *attrs, size_t nr_attrs)
18950d08e47SArnaldo Carvalho de Melo {
19050d08e47SArnaldo Carvalho de Melo 	struct perf_evsel *evsel, *n;
19150d08e47SArnaldo Carvalho de Melo 	LIST_HEAD(head);
19250d08e47SArnaldo Carvalho de Melo 	size_t i;
19350d08e47SArnaldo Carvalho de Melo 
19450d08e47SArnaldo Carvalho de Melo 	for (i = 0; i < nr_attrs; i++) {
195ef503831SArnaldo Carvalho de Melo 		evsel = perf_evsel__new_idx(attrs + i, evlist->nr_entries + i);
19650d08e47SArnaldo Carvalho de Melo 		if (evsel == NULL)
19750d08e47SArnaldo Carvalho de Melo 			goto out_delete_partial_list;
19850d08e47SArnaldo Carvalho de Melo 		list_add_tail(&evsel->node, &head);
19950d08e47SArnaldo Carvalho de Melo 	}
20050d08e47SArnaldo Carvalho de Melo 
20150d08e47SArnaldo Carvalho de Melo 	perf_evlist__splice_list_tail(evlist, &head, nr_attrs);
20250d08e47SArnaldo Carvalho de Melo 
20350d08e47SArnaldo Carvalho de Melo 	return 0;
20450d08e47SArnaldo Carvalho de Melo 
20550d08e47SArnaldo Carvalho de Melo out_delete_partial_list:
20650d08e47SArnaldo Carvalho de Melo 	list_for_each_entry_safe(evsel, n, &head, node)
20750d08e47SArnaldo Carvalho de Melo 		perf_evsel__delete(evsel);
20850d08e47SArnaldo Carvalho de Melo 	return -1;
20950d08e47SArnaldo Carvalho de Melo }
21050d08e47SArnaldo Carvalho de Melo 
21179695e1bSArnaldo Carvalho de Melo int __perf_evlist__add_default_attrs(struct perf_evlist *evlist,
21279695e1bSArnaldo Carvalho de Melo 				     struct perf_event_attr *attrs, size_t nr_attrs)
21379695e1bSArnaldo Carvalho de Melo {
21479695e1bSArnaldo Carvalho de Melo 	size_t i;
21579695e1bSArnaldo Carvalho de Melo 
21679695e1bSArnaldo Carvalho de Melo 	for (i = 0; i < nr_attrs; i++)
21779695e1bSArnaldo Carvalho de Melo 		event_attr_init(attrs + i);
21879695e1bSArnaldo Carvalho de Melo 
21979695e1bSArnaldo Carvalho de Melo 	return perf_evlist__add_attrs(evlist, attrs, nr_attrs);
22079695e1bSArnaldo Carvalho de Melo }
22179695e1bSArnaldo Carvalho de Melo 
222da378962SArnaldo Carvalho de Melo struct perf_evsel *
223ee29be62SArnaldo Carvalho de Melo perf_evlist__find_tracepoint_by_id(struct perf_evlist *evlist, int id)
224ee29be62SArnaldo Carvalho de Melo {
225ee29be62SArnaldo Carvalho de Melo 	struct perf_evsel *evsel;
226ee29be62SArnaldo Carvalho de Melo 
227ee29be62SArnaldo Carvalho de Melo 	list_for_each_entry(evsel, &evlist->entries, node) {
228ee29be62SArnaldo Carvalho de Melo 		if (evsel->attr.type   == PERF_TYPE_TRACEPOINT &&
229ee29be62SArnaldo Carvalho de Melo 		    (int)evsel->attr.config == id)
230ee29be62SArnaldo Carvalho de Melo 			return evsel;
231ee29be62SArnaldo Carvalho de Melo 	}
232ee29be62SArnaldo Carvalho de Melo 
233ee29be62SArnaldo Carvalho de Melo 	return NULL;
234ee29be62SArnaldo Carvalho de Melo }
235ee29be62SArnaldo Carvalho de Melo 
236a2f2804aSDavid Ahern struct perf_evsel *
237a2f2804aSDavid Ahern perf_evlist__find_tracepoint_by_name(struct perf_evlist *evlist,
238a2f2804aSDavid Ahern 				     const char *name)
239a2f2804aSDavid Ahern {
240a2f2804aSDavid Ahern 	struct perf_evsel *evsel;
241a2f2804aSDavid Ahern 
242a2f2804aSDavid Ahern 	list_for_each_entry(evsel, &evlist->entries, node) {
243a2f2804aSDavid Ahern 		if ((evsel->attr.type == PERF_TYPE_TRACEPOINT) &&
244a2f2804aSDavid Ahern 		    (strcmp(evsel->name, name) == 0))
245a2f2804aSDavid Ahern 			return evsel;
246a2f2804aSDavid Ahern 	}
247a2f2804aSDavid Ahern 
248a2f2804aSDavid Ahern 	return NULL;
249a2f2804aSDavid Ahern }
250a2f2804aSDavid Ahern 
25139876e7dSArnaldo Carvalho de Melo int perf_evlist__add_newtp(struct perf_evlist *evlist,
25239876e7dSArnaldo Carvalho de Melo 			   const char *sys, const char *name, void *handler)
25339876e7dSArnaldo Carvalho de Melo {
254ef503831SArnaldo Carvalho de Melo 	struct perf_evsel *evsel = perf_evsel__newtp(sys, name);
25539876e7dSArnaldo Carvalho de Melo 
25639876e7dSArnaldo Carvalho de Melo 	if (evsel == NULL)
25739876e7dSArnaldo Carvalho de Melo 		return -1;
25839876e7dSArnaldo Carvalho de Melo 
259744a9719SArnaldo Carvalho de Melo 	evsel->handler = handler;
26039876e7dSArnaldo Carvalho de Melo 	perf_evlist__add(evlist, evsel);
26139876e7dSArnaldo Carvalho de Melo 	return 0;
26239876e7dSArnaldo Carvalho de Melo }
26339876e7dSArnaldo Carvalho de Melo 
2644152ab37SArnaldo Carvalho de Melo void perf_evlist__disable(struct perf_evlist *evlist)
2654152ab37SArnaldo Carvalho de Melo {
2664152ab37SArnaldo Carvalho de Melo 	int cpu, thread;
2674152ab37SArnaldo Carvalho de Melo 	struct perf_evsel *pos;
268b3a319d5SNamhyung Kim 	int nr_cpus = cpu_map__nr(evlist->cpus);
269b3a319d5SNamhyung Kim 	int nr_threads = thread_map__nr(evlist->threads);
2704152ab37SArnaldo Carvalho de Melo 
271b3a319d5SNamhyung Kim 	for (cpu = 0; cpu < nr_cpus; cpu++) {
2724152ab37SArnaldo Carvalho de Melo 		list_for_each_entry(pos, &evlist->entries, node) {
273395c3070SAdrian Hunter 			if (!perf_evsel__is_group_leader(pos) || !pos->fd)
2743fe4430dSJiri Olsa 				continue;
275b3a319d5SNamhyung Kim 			for (thread = 0; thread < nr_threads; thread++)
27655da8005SNamhyung Kim 				ioctl(FD(pos, cpu, thread),
27755da8005SNamhyung Kim 				      PERF_EVENT_IOC_DISABLE, 0);
2784152ab37SArnaldo Carvalho de Melo 		}
2794152ab37SArnaldo Carvalho de Melo 	}
2804152ab37SArnaldo Carvalho de Melo }
2814152ab37SArnaldo Carvalho de Melo 
282764e16a3SDavid Ahern void perf_evlist__enable(struct perf_evlist *evlist)
283764e16a3SDavid Ahern {
284764e16a3SDavid Ahern 	int cpu, thread;
285764e16a3SDavid Ahern 	struct perf_evsel *pos;
286b3a319d5SNamhyung Kim 	int nr_cpus = cpu_map__nr(evlist->cpus);
287b3a319d5SNamhyung Kim 	int nr_threads = thread_map__nr(evlist->threads);
288764e16a3SDavid Ahern 
289b3a319d5SNamhyung Kim 	for (cpu = 0; cpu < nr_cpus; cpu++) {
290764e16a3SDavid Ahern 		list_for_each_entry(pos, &evlist->entries, node) {
291395c3070SAdrian Hunter 			if (!perf_evsel__is_group_leader(pos) || !pos->fd)
2923fe4430dSJiri Olsa 				continue;
293b3a319d5SNamhyung Kim 			for (thread = 0; thread < nr_threads; thread++)
29455da8005SNamhyung Kim 				ioctl(FD(pos, cpu, thread),
29555da8005SNamhyung Kim 				      PERF_EVENT_IOC_ENABLE, 0);
296764e16a3SDavid Ahern 		}
297764e16a3SDavid Ahern 	}
298764e16a3SDavid Ahern }
299764e16a3SDavid Ahern 
300395c3070SAdrian Hunter int perf_evlist__disable_event(struct perf_evlist *evlist,
301395c3070SAdrian Hunter 			       struct perf_evsel *evsel)
302395c3070SAdrian Hunter {
303395c3070SAdrian Hunter 	int cpu, thread, err;
304395c3070SAdrian Hunter 
305395c3070SAdrian Hunter 	if (!evsel->fd)
306395c3070SAdrian Hunter 		return 0;
307395c3070SAdrian Hunter 
308395c3070SAdrian Hunter 	for (cpu = 0; cpu < evlist->cpus->nr; cpu++) {
309395c3070SAdrian Hunter 		for (thread = 0; thread < evlist->threads->nr; thread++) {
310395c3070SAdrian Hunter 			err = ioctl(FD(evsel, cpu, thread),
311395c3070SAdrian Hunter 				    PERF_EVENT_IOC_DISABLE, 0);
312395c3070SAdrian Hunter 			if (err)
313395c3070SAdrian Hunter 				return err;
314395c3070SAdrian Hunter 		}
315395c3070SAdrian Hunter 	}
316395c3070SAdrian Hunter 	return 0;
317395c3070SAdrian Hunter }
318395c3070SAdrian Hunter 
319395c3070SAdrian Hunter int perf_evlist__enable_event(struct perf_evlist *evlist,
320395c3070SAdrian Hunter 			      struct perf_evsel *evsel)
321395c3070SAdrian Hunter {
322395c3070SAdrian Hunter 	int cpu, thread, err;
323395c3070SAdrian Hunter 
324395c3070SAdrian Hunter 	if (!evsel->fd)
325395c3070SAdrian Hunter 		return -EINVAL;
326395c3070SAdrian Hunter 
327395c3070SAdrian Hunter 	for (cpu = 0; cpu < evlist->cpus->nr; cpu++) {
328395c3070SAdrian Hunter 		for (thread = 0; thread < evlist->threads->nr; thread++) {
329395c3070SAdrian Hunter 			err = ioctl(FD(evsel, cpu, thread),
330395c3070SAdrian Hunter 				    PERF_EVENT_IOC_ENABLE, 0);
331395c3070SAdrian Hunter 			if (err)
332395c3070SAdrian Hunter 				return err;
333395c3070SAdrian Hunter 		}
334395c3070SAdrian Hunter 	}
335395c3070SAdrian Hunter 	return 0;
336395c3070SAdrian Hunter }
337395c3070SAdrian Hunter 
338806fb630SArnaldo Carvalho de Melo static int perf_evlist__alloc_pollfd(struct perf_evlist *evlist)
3395c581041SArnaldo Carvalho de Melo {
340b3a319d5SNamhyung Kim 	int nr_cpus = cpu_map__nr(evlist->cpus);
341b3a319d5SNamhyung Kim 	int nr_threads = thread_map__nr(evlist->threads);
342b3a319d5SNamhyung Kim 	int nfds = nr_cpus * nr_threads * evlist->nr_entries;
3435c581041SArnaldo Carvalho de Melo 	evlist->pollfd = malloc(sizeof(struct pollfd) * nfds);
3445c581041SArnaldo Carvalho de Melo 	return evlist->pollfd != NULL ? 0 : -ENOMEM;
3455c581041SArnaldo Carvalho de Melo }
34670082dd9SArnaldo Carvalho de Melo 
34770082dd9SArnaldo Carvalho de Melo void perf_evlist__add_pollfd(struct perf_evlist *evlist, int fd)
34870082dd9SArnaldo Carvalho de Melo {
34970082dd9SArnaldo Carvalho de Melo 	fcntl(fd, F_SETFL, O_NONBLOCK);
35070082dd9SArnaldo Carvalho de Melo 	evlist->pollfd[evlist->nr_fds].fd = fd;
35170082dd9SArnaldo Carvalho de Melo 	evlist->pollfd[evlist->nr_fds].events = POLLIN;
35270082dd9SArnaldo Carvalho de Melo 	evlist->nr_fds++;
35370082dd9SArnaldo Carvalho de Melo }
35470db7533SArnaldo Carvalho de Melo 
355a91e5431SArnaldo Carvalho de Melo static void perf_evlist__id_hash(struct perf_evlist *evlist,
356a91e5431SArnaldo Carvalho de Melo 				 struct perf_evsel *evsel,
3573d3b5e95SArnaldo Carvalho de Melo 				 int cpu, int thread, u64 id)
3583d3b5e95SArnaldo Carvalho de Melo {
3593d3b5e95SArnaldo Carvalho de Melo 	int hash;
3603d3b5e95SArnaldo Carvalho de Melo 	struct perf_sample_id *sid = SID(evsel, cpu, thread);
3613d3b5e95SArnaldo Carvalho de Melo 
3623d3b5e95SArnaldo Carvalho de Melo 	sid->id = id;
3633d3b5e95SArnaldo Carvalho de Melo 	sid->evsel = evsel;
3643d3b5e95SArnaldo Carvalho de Melo 	hash = hash_64(sid->id, PERF_EVLIST__HLIST_BITS);
3653d3b5e95SArnaldo Carvalho de Melo 	hlist_add_head(&sid->node, &evlist->heads[hash]);
3663d3b5e95SArnaldo Carvalho de Melo }
3673d3b5e95SArnaldo Carvalho de Melo 
368a91e5431SArnaldo Carvalho de Melo void perf_evlist__id_add(struct perf_evlist *evlist, struct perf_evsel *evsel,
369a91e5431SArnaldo Carvalho de Melo 			 int cpu, int thread, u64 id)
370a91e5431SArnaldo Carvalho de Melo {
371a91e5431SArnaldo Carvalho de Melo 	perf_evlist__id_hash(evlist, evsel, cpu, thread, id);
372a91e5431SArnaldo Carvalho de Melo 	evsel->id[evsel->ids++] = id;
373a91e5431SArnaldo Carvalho de Melo }
374a91e5431SArnaldo Carvalho de Melo 
375a91e5431SArnaldo Carvalho de Melo static int perf_evlist__id_add_fd(struct perf_evlist *evlist,
3763d3b5e95SArnaldo Carvalho de Melo 				  struct perf_evsel *evsel,
377f8a95309SArnaldo Carvalho de Melo 				  int cpu, int thread, int fd)
378f8a95309SArnaldo Carvalho de Melo {
379f8a95309SArnaldo Carvalho de Melo 	u64 read_data[4] = { 0, };
3803d3b5e95SArnaldo Carvalho de Melo 	int id_idx = 1; /* The first entry is the counter value */
381e2b5abe0SJiri Olsa 	u64 id;
382e2b5abe0SJiri Olsa 	int ret;
383e2b5abe0SJiri Olsa 
384e2b5abe0SJiri Olsa 	ret = ioctl(fd, PERF_EVENT_IOC_ID, &id);
385e2b5abe0SJiri Olsa 	if (!ret)
386e2b5abe0SJiri Olsa 		goto add;
387e2b5abe0SJiri Olsa 
388e2b5abe0SJiri Olsa 	if (errno != ENOTTY)
389e2b5abe0SJiri Olsa 		return -1;
390e2b5abe0SJiri Olsa 
391e2b5abe0SJiri Olsa 	/* Legacy way to get event id.. All hail to old kernels! */
392f8a95309SArnaldo Carvalho de Melo 
393c4861afeSJiri Olsa 	/*
394c4861afeSJiri Olsa 	 * This way does not work with group format read, so bail
395c4861afeSJiri Olsa 	 * out in that case.
396c4861afeSJiri Olsa 	 */
397c4861afeSJiri Olsa 	if (perf_evlist__read_format(evlist) & PERF_FORMAT_GROUP)
398c4861afeSJiri Olsa 		return -1;
399c4861afeSJiri Olsa 
400f8a95309SArnaldo Carvalho de Melo 	if (!(evsel->attr.read_format & PERF_FORMAT_ID) ||
401f8a95309SArnaldo Carvalho de Melo 	    read(fd, &read_data, sizeof(read_data)) == -1)
402f8a95309SArnaldo Carvalho de Melo 		return -1;
403f8a95309SArnaldo Carvalho de Melo 
404f8a95309SArnaldo Carvalho de Melo 	if (evsel->attr.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
405f8a95309SArnaldo Carvalho de Melo 		++id_idx;
406f8a95309SArnaldo Carvalho de Melo 	if (evsel->attr.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
407f8a95309SArnaldo Carvalho de Melo 		++id_idx;
408f8a95309SArnaldo Carvalho de Melo 
409e2b5abe0SJiri Olsa 	id = read_data[id_idx];
410e2b5abe0SJiri Olsa 
411e2b5abe0SJiri Olsa  add:
412e2b5abe0SJiri Olsa 	perf_evlist__id_add(evlist, evsel, cpu, thread, id);
413f8a95309SArnaldo Carvalho de Melo 	return 0;
414f8a95309SArnaldo Carvalho de Melo }
415f8a95309SArnaldo Carvalho de Melo 
416932a3594SJiri Olsa struct perf_sample_id *perf_evlist__id2sid(struct perf_evlist *evlist, u64 id)
41770db7533SArnaldo Carvalho de Melo {
41870db7533SArnaldo Carvalho de Melo 	struct hlist_head *head;
41970db7533SArnaldo Carvalho de Melo 	struct perf_sample_id *sid;
42070db7533SArnaldo Carvalho de Melo 	int hash;
42170db7533SArnaldo Carvalho de Melo 
42270db7533SArnaldo Carvalho de Melo 	hash = hash_64(id, PERF_EVLIST__HLIST_BITS);
42370db7533SArnaldo Carvalho de Melo 	head = &evlist->heads[hash];
42470db7533SArnaldo Carvalho de Melo 
425b67bfe0dSSasha Levin 	hlist_for_each_entry(sid, head, node)
42670db7533SArnaldo Carvalho de Melo 		if (sid->id == id)
427932a3594SJiri Olsa 			return sid;
428932a3594SJiri Olsa 
429932a3594SJiri Olsa 	return NULL;
430932a3594SJiri Olsa }
431932a3594SJiri Olsa 
432932a3594SJiri Olsa struct perf_evsel *perf_evlist__id2evsel(struct perf_evlist *evlist, u64 id)
433932a3594SJiri Olsa {
434932a3594SJiri Olsa 	struct perf_sample_id *sid;
435932a3594SJiri Olsa 
436932a3594SJiri Olsa 	if (evlist->nr_entries == 1)
437932a3594SJiri Olsa 		return perf_evlist__first(evlist);
438932a3594SJiri Olsa 
439932a3594SJiri Olsa 	sid = perf_evlist__id2sid(evlist, id);
440932a3594SJiri Olsa 	if (sid)
44170db7533SArnaldo Carvalho de Melo 		return sid->evsel;
44230e68bccSNamhyung Kim 
44330e68bccSNamhyung Kim 	if (!perf_evlist__sample_id_all(evlist))
4440c21f736SArnaldo Carvalho de Melo 		return perf_evlist__first(evlist);
44530e68bccSNamhyung Kim 
44670db7533SArnaldo Carvalho de Melo 	return NULL;
44770db7533SArnaldo Carvalho de Melo }
44804391debSArnaldo Carvalho de Melo 
44975562573SAdrian Hunter static int perf_evlist__event2id(struct perf_evlist *evlist,
45075562573SAdrian Hunter 				 union perf_event *event, u64 *id)
45175562573SAdrian Hunter {
45275562573SAdrian Hunter 	const u64 *array = event->sample.array;
45375562573SAdrian Hunter 	ssize_t n;
45475562573SAdrian Hunter 
45575562573SAdrian Hunter 	n = (event->header.size - sizeof(event->header)) >> 3;
45675562573SAdrian Hunter 
45775562573SAdrian Hunter 	if (event->header.type == PERF_RECORD_SAMPLE) {
45875562573SAdrian Hunter 		if (evlist->id_pos >= n)
45975562573SAdrian Hunter 			return -1;
46075562573SAdrian Hunter 		*id = array[evlist->id_pos];
46175562573SAdrian Hunter 	} else {
46275562573SAdrian Hunter 		if (evlist->is_pos > n)
46375562573SAdrian Hunter 			return -1;
46475562573SAdrian Hunter 		n -= evlist->is_pos;
46575562573SAdrian Hunter 		*id = array[n];
46675562573SAdrian Hunter 	}
46775562573SAdrian Hunter 	return 0;
46875562573SAdrian Hunter }
46975562573SAdrian Hunter 
47075562573SAdrian Hunter static struct perf_evsel *perf_evlist__event2evsel(struct perf_evlist *evlist,
47175562573SAdrian Hunter 						   union perf_event *event)
47275562573SAdrian Hunter {
47398be6966SAdrian Hunter 	struct perf_evsel *first = perf_evlist__first(evlist);
47475562573SAdrian Hunter 	struct hlist_head *head;
47575562573SAdrian Hunter 	struct perf_sample_id *sid;
47675562573SAdrian Hunter 	int hash;
47775562573SAdrian Hunter 	u64 id;
47875562573SAdrian Hunter 
47975562573SAdrian Hunter 	if (evlist->nr_entries == 1)
48098be6966SAdrian Hunter 		return first;
48198be6966SAdrian Hunter 
48298be6966SAdrian Hunter 	if (!first->attr.sample_id_all &&
48398be6966SAdrian Hunter 	    event->header.type != PERF_RECORD_SAMPLE)
48498be6966SAdrian Hunter 		return first;
48575562573SAdrian Hunter 
48675562573SAdrian Hunter 	if (perf_evlist__event2id(evlist, event, &id))
48775562573SAdrian Hunter 		return NULL;
48875562573SAdrian Hunter 
48975562573SAdrian Hunter 	/* Synthesized events have an id of zero */
49075562573SAdrian Hunter 	if (!id)
49198be6966SAdrian Hunter 		return first;
49275562573SAdrian Hunter 
49375562573SAdrian Hunter 	hash = hash_64(id, PERF_EVLIST__HLIST_BITS);
49475562573SAdrian Hunter 	head = &evlist->heads[hash];
49575562573SAdrian Hunter 
49675562573SAdrian Hunter 	hlist_for_each_entry(sid, head, node) {
49775562573SAdrian Hunter 		if (sid->id == id)
49875562573SAdrian Hunter 			return sid->evsel;
49975562573SAdrian Hunter 	}
50075562573SAdrian Hunter 	return NULL;
50175562573SAdrian Hunter }
50275562573SAdrian Hunter 
503aece948fSArnaldo Carvalho de Melo union perf_event *perf_evlist__mmap_read(struct perf_evlist *evlist, int idx)
50404391debSArnaldo Carvalho de Melo {
505aece948fSArnaldo Carvalho de Melo 	struct perf_mmap *md = &evlist->mmap[idx];
50604391debSArnaldo Carvalho de Melo 	unsigned int head = perf_mmap__read_head(md);
50704391debSArnaldo Carvalho de Melo 	unsigned int old = md->prev;
50804391debSArnaldo Carvalho de Melo 	unsigned char *data = md->base + page_size;
5098115d60cSArnaldo Carvalho de Melo 	union perf_event *event = NULL;
51004391debSArnaldo Carvalho de Melo 
5117bb41152SArnaldo Carvalho de Melo 	if (evlist->overwrite) {
51204391debSArnaldo Carvalho de Melo 		/*
51304391debSArnaldo Carvalho de Melo 		 * If we're further behind than half the buffer, there's a chance
51404391debSArnaldo Carvalho de Melo 		 * the writer will bite our tail and mess up the samples under us.
51504391debSArnaldo Carvalho de Melo 		 *
51604391debSArnaldo Carvalho de Melo 		 * If we somehow ended up ahead of the head, we got messed up.
51704391debSArnaldo Carvalho de Melo 		 *
51804391debSArnaldo Carvalho de Melo 		 * In either case, truncate and restart at head.
51904391debSArnaldo Carvalho de Melo 		 */
5207bb41152SArnaldo Carvalho de Melo 		int diff = head - old;
52104391debSArnaldo Carvalho de Melo 		if (diff > md->mask / 2 || diff < 0) {
52204391debSArnaldo Carvalho de Melo 			fprintf(stderr, "WARNING: failed to keep up with mmap data.\n");
52304391debSArnaldo Carvalho de Melo 
52404391debSArnaldo Carvalho de Melo 			/*
52504391debSArnaldo Carvalho de Melo 			 * head points to a known good entry, start there.
52604391debSArnaldo Carvalho de Melo 			 */
52704391debSArnaldo Carvalho de Melo 			old = head;
52804391debSArnaldo Carvalho de Melo 		}
5297bb41152SArnaldo Carvalho de Melo 	}
53004391debSArnaldo Carvalho de Melo 
53104391debSArnaldo Carvalho de Melo 	if (old != head) {
53204391debSArnaldo Carvalho de Melo 		size_t size;
53304391debSArnaldo Carvalho de Melo 
5348115d60cSArnaldo Carvalho de Melo 		event = (union perf_event *)&data[old & md->mask];
53504391debSArnaldo Carvalho de Melo 		size = event->header.size;
53604391debSArnaldo Carvalho de Melo 
53704391debSArnaldo Carvalho de Melo 		/*
53804391debSArnaldo Carvalho de Melo 		 * Event straddles the mmap boundary -- header should always
53904391debSArnaldo Carvalho de Melo 		 * be inside due to u64 alignment of output.
54004391debSArnaldo Carvalho de Melo 		 */
54104391debSArnaldo Carvalho de Melo 		if ((old & md->mask) + size != ((old + size) & md->mask)) {
54204391debSArnaldo Carvalho de Melo 			unsigned int offset = old;
54304391debSArnaldo Carvalho de Melo 			unsigned int len = min(sizeof(*event), size), cpy;
544a65cb4b9SJiri Olsa 			void *dst = md->event_copy;
54504391debSArnaldo Carvalho de Melo 
54604391debSArnaldo Carvalho de Melo 			do {
54704391debSArnaldo Carvalho de Melo 				cpy = min(md->mask + 1 - (offset & md->mask), len);
54804391debSArnaldo Carvalho de Melo 				memcpy(dst, &data[offset & md->mask], cpy);
54904391debSArnaldo Carvalho de Melo 				offset += cpy;
55004391debSArnaldo Carvalho de Melo 				dst += cpy;
55104391debSArnaldo Carvalho de Melo 				len -= cpy;
55204391debSArnaldo Carvalho de Melo 			} while (len);
55304391debSArnaldo Carvalho de Melo 
554a65cb4b9SJiri Olsa 			event = (union perf_event *) md->event_copy;
55504391debSArnaldo Carvalho de Melo 		}
55604391debSArnaldo Carvalho de Melo 
55704391debSArnaldo Carvalho de Melo 		old += size;
55804391debSArnaldo Carvalho de Melo 	}
55904391debSArnaldo Carvalho de Melo 
56004391debSArnaldo Carvalho de Melo 	md->prev = old;
5617bb41152SArnaldo Carvalho de Melo 
56204391debSArnaldo Carvalho de Melo 	return event;
56304391debSArnaldo Carvalho de Melo }
564f8a95309SArnaldo Carvalho de Melo 
5658e50d384SZhouyi Zhou void perf_evlist__mmap_consume(struct perf_evlist *evlist, int idx)
5668e50d384SZhouyi Zhou {
5678e50d384SZhouyi Zhou 	if (!evlist->overwrite) {
5688e50d384SZhouyi Zhou 		struct perf_mmap *md = &evlist->mmap[idx];
5698e50d384SZhouyi Zhou 		unsigned int old = md->prev;
5708e50d384SZhouyi Zhou 
5718e50d384SZhouyi Zhou 		perf_mmap__write_tail(md, old);
5728e50d384SZhouyi Zhou 	}
5738e50d384SZhouyi Zhou }
5748e50d384SZhouyi Zhou 
57593edcbd9SAdrian Hunter static void __perf_evlist__munmap(struct perf_evlist *evlist, int idx)
57693edcbd9SAdrian Hunter {
57793edcbd9SAdrian Hunter 	if (evlist->mmap[idx].base != NULL) {
57893edcbd9SAdrian Hunter 		munmap(evlist->mmap[idx].base, evlist->mmap_len);
57993edcbd9SAdrian Hunter 		evlist->mmap[idx].base = NULL;
58093edcbd9SAdrian Hunter 	}
58193edcbd9SAdrian Hunter }
58293edcbd9SAdrian Hunter 
5837e2ed097SArnaldo Carvalho de Melo void perf_evlist__munmap(struct perf_evlist *evlist)
584f8a95309SArnaldo Carvalho de Melo {
585aece948fSArnaldo Carvalho de Melo 	int i;
586f8a95309SArnaldo Carvalho de Melo 
58793edcbd9SAdrian Hunter 	for (i = 0; i < evlist->nr_mmaps; i++)
58893edcbd9SAdrian Hunter 		__perf_evlist__munmap(evlist, i);
589aece948fSArnaldo Carvalho de Melo 
590aece948fSArnaldo Carvalho de Melo 	free(evlist->mmap);
591aece948fSArnaldo Carvalho de Melo 	evlist->mmap = NULL;
592f8a95309SArnaldo Carvalho de Melo }
593f8a95309SArnaldo Carvalho de Melo 
594806fb630SArnaldo Carvalho de Melo static int perf_evlist__alloc_mmap(struct perf_evlist *evlist)
595f8a95309SArnaldo Carvalho de Melo {
596a14bb7a6SArnaldo Carvalho de Melo 	evlist->nr_mmaps = cpu_map__nr(evlist->cpus);
597ec1e7e43SSukadev Bhattiprolu 	if (cpu_map__empty(evlist->cpus))
598b3a319d5SNamhyung Kim 		evlist->nr_mmaps = thread_map__nr(evlist->threads);
599aece948fSArnaldo Carvalho de Melo 	evlist->mmap = zalloc(evlist->nr_mmaps * sizeof(struct perf_mmap));
600f8a95309SArnaldo Carvalho de Melo 	return evlist->mmap != NULL ? 0 : -ENOMEM;
601f8a95309SArnaldo Carvalho de Melo }
602f8a95309SArnaldo Carvalho de Melo 
603bccdaba0SArnaldo Carvalho de Melo static int __perf_evlist__mmap(struct perf_evlist *evlist,
604aece948fSArnaldo Carvalho de Melo 			       int idx, int prot, int mask, int fd)
605f8a95309SArnaldo Carvalho de Melo {
606aece948fSArnaldo Carvalho de Melo 	evlist->mmap[idx].prev = 0;
607aece948fSArnaldo Carvalho de Melo 	evlist->mmap[idx].mask = mask;
608aece948fSArnaldo Carvalho de Melo 	evlist->mmap[idx].base = mmap(NULL, evlist->mmap_len, prot,
609f8a95309SArnaldo Carvalho de Melo 				      MAP_SHARED, fd, 0);
610301b195dSNelson Elhage 	if (evlist->mmap[idx].base == MAP_FAILED) {
61102635965SAdrian Hunter 		pr_debug2("failed to mmap perf event ring buffer, error %d\n",
61202635965SAdrian Hunter 			  errno);
613301b195dSNelson Elhage 		evlist->mmap[idx].base = NULL;
614f8a95309SArnaldo Carvalho de Melo 		return -1;
615301b195dSNelson Elhage 	}
616f8a95309SArnaldo Carvalho de Melo 
617f8a95309SArnaldo Carvalho de Melo 	perf_evlist__add_pollfd(evlist, fd);
618f8a95309SArnaldo Carvalho de Melo 	return 0;
619f8a95309SArnaldo Carvalho de Melo }
620f8a95309SArnaldo Carvalho de Melo 
62104e21314SAdrian Hunter static int perf_evlist__mmap_per_evsel(struct perf_evlist *evlist, int idx,
62204e21314SAdrian Hunter 				       int prot, int mask, int cpu, int thread,
62304e21314SAdrian Hunter 				       int *output)
624aece948fSArnaldo Carvalho de Melo {
625aece948fSArnaldo Carvalho de Melo 	struct perf_evsel *evsel;
62604e21314SAdrian Hunter 
62704e21314SAdrian Hunter 	list_for_each_entry(evsel, &evlist->entries, node) {
62804e21314SAdrian Hunter 		int fd = FD(evsel, cpu, thread);
62904e21314SAdrian Hunter 
63004e21314SAdrian Hunter 		if (*output == -1) {
63104e21314SAdrian Hunter 			*output = fd;
63204e21314SAdrian Hunter 			if (__perf_evlist__mmap(evlist, idx, prot, mask,
63304e21314SAdrian Hunter 						*output) < 0)
63404e21314SAdrian Hunter 				return -1;
63504e21314SAdrian Hunter 		} else {
63604e21314SAdrian Hunter 			if (ioctl(fd, PERF_EVENT_IOC_SET_OUTPUT, *output) != 0)
63704e21314SAdrian Hunter 				return -1;
63804e21314SAdrian Hunter 		}
63904e21314SAdrian Hunter 
64004e21314SAdrian Hunter 		if ((evsel->attr.read_format & PERF_FORMAT_ID) &&
64104e21314SAdrian Hunter 		    perf_evlist__id_add_fd(evlist, evsel, cpu, thread, fd) < 0)
64204e21314SAdrian Hunter 			return -1;
64304e21314SAdrian Hunter 	}
64404e21314SAdrian Hunter 
64504e21314SAdrian Hunter 	return 0;
64604e21314SAdrian Hunter }
64704e21314SAdrian Hunter 
64804e21314SAdrian Hunter static int perf_evlist__mmap_per_cpu(struct perf_evlist *evlist, int prot,
64904e21314SAdrian Hunter 				     int mask)
65004e21314SAdrian Hunter {
651aece948fSArnaldo Carvalho de Melo 	int cpu, thread;
652b3a319d5SNamhyung Kim 	int nr_cpus = cpu_map__nr(evlist->cpus);
653b3a319d5SNamhyung Kim 	int nr_threads = thread_map__nr(evlist->threads);
654aece948fSArnaldo Carvalho de Melo 
655e3e1a54fSAdrian Hunter 	pr_debug2("perf event ring buffer mmapped per cpu\n");
656b3a319d5SNamhyung Kim 	for (cpu = 0; cpu < nr_cpus; cpu++) {
657aece948fSArnaldo Carvalho de Melo 		int output = -1;
658aece948fSArnaldo Carvalho de Melo 
659b3a319d5SNamhyung Kim 		for (thread = 0; thread < nr_threads; thread++) {
66004e21314SAdrian Hunter 			if (perf_evlist__mmap_per_evsel(evlist, cpu, prot, mask,
66104e21314SAdrian Hunter 							cpu, thread, &output))
662aece948fSArnaldo Carvalho de Melo 				goto out_unmap;
663aece948fSArnaldo Carvalho de Melo 		}
664aece948fSArnaldo Carvalho de Melo 	}
665aece948fSArnaldo Carvalho de Melo 
666aece948fSArnaldo Carvalho de Melo 	return 0;
667aece948fSArnaldo Carvalho de Melo 
668aece948fSArnaldo Carvalho de Melo out_unmap:
66993edcbd9SAdrian Hunter 	for (cpu = 0; cpu < nr_cpus; cpu++)
67093edcbd9SAdrian Hunter 		__perf_evlist__munmap(evlist, cpu);
671aece948fSArnaldo Carvalho de Melo 	return -1;
672aece948fSArnaldo Carvalho de Melo }
673aece948fSArnaldo Carvalho de Melo 
67404e21314SAdrian Hunter static int perf_evlist__mmap_per_thread(struct perf_evlist *evlist, int prot,
67504e21314SAdrian Hunter 					int mask)
676aece948fSArnaldo Carvalho de Melo {
677aece948fSArnaldo Carvalho de Melo 	int thread;
678b3a319d5SNamhyung Kim 	int nr_threads = thread_map__nr(evlist->threads);
679aece948fSArnaldo Carvalho de Melo 
680e3e1a54fSAdrian Hunter 	pr_debug2("perf event ring buffer mmapped per thread\n");
681b3a319d5SNamhyung Kim 	for (thread = 0; thread < nr_threads; thread++) {
682aece948fSArnaldo Carvalho de Melo 		int output = -1;
683aece948fSArnaldo Carvalho de Melo 
68404e21314SAdrian Hunter 		if (perf_evlist__mmap_per_evsel(evlist, thread, prot, mask, 0,
68504e21314SAdrian Hunter 						thread, &output))
686aece948fSArnaldo Carvalho de Melo 			goto out_unmap;
687aece948fSArnaldo Carvalho de Melo 	}
688aece948fSArnaldo Carvalho de Melo 
689aece948fSArnaldo Carvalho de Melo 	return 0;
690aece948fSArnaldo Carvalho de Melo 
691aece948fSArnaldo Carvalho de Melo out_unmap:
69293edcbd9SAdrian Hunter 	for (thread = 0; thread < nr_threads; thread++)
69393edcbd9SAdrian Hunter 		__perf_evlist__munmap(evlist, thread);
694aece948fSArnaldo Carvalho de Melo 	return -1;
695aece948fSArnaldo Carvalho de Melo }
696aece948fSArnaldo Carvalho de Melo 
697994a1f78SJiri Olsa static size_t perf_evlist__mmap_size(unsigned long pages)
698994a1f78SJiri Olsa {
699994a1f78SJiri Olsa 	/* 512 kiB: default amount of unprivileged mlocked memory */
700994a1f78SJiri Olsa 	if (pages == UINT_MAX)
701994a1f78SJiri Olsa 		pages = (512 * 1024) / page_size;
702994a1f78SJiri Olsa 	else if (!is_power_of_2(pages))
703994a1f78SJiri Olsa 		return 0;
704994a1f78SJiri Olsa 
705994a1f78SJiri Olsa 	return (pages + 1) * page_size;
706994a1f78SJiri Olsa }
707994a1f78SJiri Olsa 
70833c2dcfdSDavid Ahern static long parse_pages_arg(const char *str, unsigned long min,
70933c2dcfdSDavid Ahern 			    unsigned long max)
710994a1f78SJiri Olsa {
7112fbe4abeSAdrian Hunter 	unsigned long pages, val;
71227050f53SJiri Olsa 	static struct parse_tag tags[] = {
71327050f53SJiri Olsa 		{ .tag  = 'B', .mult = 1       },
71427050f53SJiri Olsa 		{ .tag  = 'K', .mult = 1 << 10 },
71527050f53SJiri Olsa 		{ .tag  = 'M', .mult = 1 << 20 },
71627050f53SJiri Olsa 		{ .tag  = 'G', .mult = 1 << 30 },
71727050f53SJiri Olsa 		{ .tag  = 0 },
71827050f53SJiri Olsa 	};
719994a1f78SJiri Olsa 
7208973504bSDavid Ahern 	if (str == NULL)
72133c2dcfdSDavid Ahern 		return -EINVAL;
7228973504bSDavid Ahern 
72327050f53SJiri Olsa 	val = parse_tag_value(str, tags);
7242fbe4abeSAdrian Hunter 	if (val != (unsigned long) -1) {
72527050f53SJiri Olsa 		/* we got file size value */
72627050f53SJiri Olsa 		pages = PERF_ALIGN(val, page_size) / page_size;
72727050f53SJiri Olsa 	} else {
72827050f53SJiri Olsa 		/* we got pages count value */
72927050f53SJiri Olsa 		char *eptr;
730994a1f78SJiri Olsa 		pages = strtoul(str, &eptr, 10);
73133c2dcfdSDavid Ahern 		if (*eptr != '\0')
73233c2dcfdSDavid Ahern 			return -EINVAL;
73327050f53SJiri Olsa 	}
734994a1f78SJiri Olsa 
7352bcab6c1SAdrian Hunter 	if (pages == 0 && min == 0) {
73633c2dcfdSDavid Ahern 		/* leave number of pages at 0 */
7371dbfa938SAdrian Hunter 	} else if (!is_power_of_2(pages)) {
73833c2dcfdSDavid Ahern 		/* round pages up to next power of 2 */
7391dbfa938SAdrian Hunter 		pages = next_pow2_l(pages);
7401dbfa938SAdrian Hunter 		if (!pages)
7411dbfa938SAdrian Hunter 			return -EINVAL;
7429639837eSDavid Ahern 		pr_info("rounding mmap pages size to %lu bytes (%lu pages)\n",
7439639837eSDavid Ahern 			pages * page_size, pages);
7442fbe4abeSAdrian Hunter 	}
7452fbe4abeSAdrian Hunter 
74633c2dcfdSDavid Ahern 	if (pages > max)
74733c2dcfdSDavid Ahern 		return -EINVAL;
74833c2dcfdSDavid Ahern 
74933c2dcfdSDavid Ahern 	return pages;
75033c2dcfdSDavid Ahern }
75133c2dcfdSDavid Ahern 
75233c2dcfdSDavid Ahern int perf_evlist__parse_mmap_pages(const struct option *opt, const char *str,
75333c2dcfdSDavid Ahern 				  int unset __maybe_unused)
75433c2dcfdSDavid Ahern {
75533c2dcfdSDavid Ahern 	unsigned int *mmap_pages = opt->value;
75633c2dcfdSDavid Ahern 	unsigned long max = UINT_MAX;
75733c2dcfdSDavid Ahern 	long pages;
75833c2dcfdSDavid Ahern 
759f5ae9c42SAdrian Hunter 	if (max > SIZE_MAX / page_size)
76033c2dcfdSDavid Ahern 		max = SIZE_MAX / page_size;
76133c2dcfdSDavid Ahern 
76233c2dcfdSDavid Ahern 	pages = parse_pages_arg(str, 1, max);
76333c2dcfdSDavid Ahern 	if (pages < 0) {
76433c2dcfdSDavid Ahern 		pr_err("Invalid argument for --mmap_pages/-m\n");
765994a1f78SJiri Olsa 		return -1;
766994a1f78SJiri Olsa 	}
767994a1f78SJiri Olsa 
768994a1f78SJiri Olsa 	*mmap_pages = pages;
769994a1f78SJiri Olsa 	return 0;
770994a1f78SJiri Olsa }
771994a1f78SJiri Olsa 
772c83fa7f2SAdrian Hunter /**
773c83fa7f2SAdrian Hunter  * perf_evlist__mmap - Create mmaps to receive events.
774c83fa7f2SAdrian Hunter  * @evlist: list of events
775c83fa7f2SAdrian Hunter  * @pages: map length in pages
776c83fa7f2SAdrian Hunter  * @overwrite: overwrite older events?
777f8a95309SArnaldo Carvalho de Melo  *
778c83fa7f2SAdrian Hunter  * If @overwrite is %false the user needs to signal event consumption using
779c83fa7f2SAdrian Hunter  * perf_mmap__write_tail().  Using perf_evlist__mmap_read() does this
780c83fa7f2SAdrian Hunter  * automatically.
781f8a95309SArnaldo Carvalho de Melo  *
782c83fa7f2SAdrian Hunter  * Return: %0 on success, negative error code otherwise.
783f8a95309SArnaldo Carvalho de Melo  */
78450a682ceSArnaldo Carvalho de Melo int perf_evlist__mmap(struct perf_evlist *evlist, unsigned int pages,
78550a682ceSArnaldo Carvalho de Melo 		      bool overwrite)
786f8a95309SArnaldo Carvalho de Melo {
787aece948fSArnaldo Carvalho de Melo 	struct perf_evsel *evsel;
7887e2ed097SArnaldo Carvalho de Melo 	const struct cpu_map *cpus = evlist->cpus;
7897e2ed097SArnaldo Carvalho de Melo 	const struct thread_map *threads = evlist->threads;
79050a682ceSArnaldo Carvalho de Melo 	int prot = PROT_READ | (overwrite ? 0 : PROT_WRITE), mask;
79150a682ceSArnaldo Carvalho de Melo 
7927e2ed097SArnaldo Carvalho de Melo 	if (evlist->mmap == NULL && perf_evlist__alloc_mmap(evlist) < 0)
793f8a95309SArnaldo Carvalho de Melo 		return -ENOMEM;
794f8a95309SArnaldo Carvalho de Melo 
7957e2ed097SArnaldo Carvalho de Melo 	if (evlist->pollfd == NULL && perf_evlist__alloc_pollfd(evlist) < 0)
796f8a95309SArnaldo Carvalho de Melo 		return -ENOMEM;
797f8a95309SArnaldo Carvalho de Melo 
798f8a95309SArnaldo Carvalho de Melo 	evlist->overwrite = overwrite;
799994a1f78SJiri Olsa 	evlist->mmap_len = perf_evlist__mmap_size(pages);
8002af68ef5SAdrian Hunter 	pr_debug("mmap size %zuB\n", evlist->mmap_len);
801994a1f78SJiri Olsa 	mask = evlist->mmap_len - page_size - 1;
802f8a95309SArnaldo Carvalho de Melo 
803f8a95309SArnaldo Carvalho de Melo 	list_for_each_entry(evsel, &evlist->entries, node) {
804f8a95309SArnaldo Carvalho de Melo 		if ((evsel->attr.read_format & PERF_FORMAT_ID) &&
805a91e5431SArnaldo Carvalho de Melo 		    evsel->sample_id == NULL &&
806a14bb7a6SArnaldo Carvalho de Melo 		    perf_evsel__alloc_id(evsel, cpu_map__nr(cpus), threads->nr) < 0)
807f8a95309SArnaldo Carvalho de Melo 			return -ENOMEM;
808f8a95309SArnaldo Carvalho de Melo 	}
809f8a95309SArnaldo Carvalho de Melo 
810ec1e7e43SSukadev Bhattiprolu 	if (cpu_map__empty(cpus))
811aece948fSArnaldo Carvalho de Melo 		return perf_evlist__mmap_per_thread(evlist, prot, mask);
812f8a95309SArnaldo Carvalho de Melo 
813aece948fSArnaldo Carvalho de Melo 	return perf_evlist__mmap_per_cpu(evlist, prot, mask);
814f8a95309SArnaldo Carvalho de Melo }
8157e2ed097SArnaldo Carvalho de Melo 
816602ad878SArnaldo Carvalho de Melo int perf_evlist__create_maps(struct perf_evlist *evlist, struct target *target)
8177e2ed097SArnaldo Carvalho de Melo {
818b809ac10SNamhyung Kim 	evlist->threads = thread_map__new_str(target->pid, target->tid,
819b809ac10SNamhyung Kim 					      target->uid);
8207e2ed097SArnaldo Carvalho de Melo 
8217e2ed097SArnaldo Carvalho de Melo 	if (evlist->threads == NULL)
8227e2ed097SArnaldo Carvalho de Melo 		return -1;
8237e2ed097SArnaldo Carvalho de Melo 
8249c105fbcSDongsheng Yang 	if (target__uses_dummy_map(target))
825d1cb9fceSNamhyung Kim 		evlist->cpus = cpu_map__dummy_new();
826879d77d0SNamhyung Kim 	else
827879d77d0SNamhyung Kim 		evlist->cpus = cpu_map__new(target->cpu_list);
8287e2ed097SArnaldo Carvalho de Melo 
8297e2ed097SArnaldo Carvalho de Melo 	if (evlist->cpus == NULL)
8307e2ed097SArnaldo Carvalho de Melo 		goto out_delete_threads;
8317e2ed097SArnaldo Carvalho de Melo 
8327e2ed097SArnaldo Carvalho de Melo 	return 0;
8337e2ed097SArnaldo Carvalho de Melo 
8347e2ed097SArnaldo Carvalho de Melo out_delete_threads:
8357e2ed097SArnaldo Carvalho de Melo 	thread_map__delete(evlist->threads);
8367e2ed097SArnaldo Carvalho de Melo 	return -1;
8377e2ed097SArnaldo Carvalho de Melo }
8387e2ed097SArnaldo Carvalho de Melo 
8397e2ed097SArnaldo Carvalho de Melo void perf_evlist__delete_maps(struct perf_evlist *evlist)
8407e2ed097SArnaldo Carvalho de Melo {
8417e2ed097SArnaldo Carvalho de Melo 	cpu_map__delete(evlist->cpus);
8427e2ed097SArnaldo Carvalho de Melo 	thread_map__delete(evlist->threads);
8437e2ed097SArnaldo Carvalho de Melo 	evlist->cpus	= NULL;
8447e2ed097SArnaldo Carvalho de Melo 	evlist->threads = NULL;
8457e2ed097SArnaldo Carvalho de Melo }
8460a102479SFrederic Weisbecker 
8471491a632SArnaldo Carvalho de Melo int perf_evlist__apply_filters(struct perf_evlist *evlist)
8480a102479SFrederic Weisbecker {
8490a102479SFrederic Weisbecker 	struct perf_evsel *evsel;
850745cefc5SArnaldo Carvalho de Melo 	int err = 0;
851745cefc5SArnaldo Carvalho de Melo 	const int ncpus = cpu_map__nr(evlist->cpus),
852b3a319d5SNamhyung Kim 		  nthreads = thread_map__nr(evlist->threads);
8530a102479SFrederic Weisbecker 
8540a102479SFrederic Weisbecker 	list_for_each_entry(evsel, &evlist->entries, node) {
855745cefc5SArnaldo Carvalho de Melo 		if (evsel->filter == NULL)
8560a102479SFrederic Weisbecker 			continue;
857745cefc5SArnaldo Carvalho de Melo 
858745cefc5SArnaldo Carvalho de Melo 		err = perf_evsel__set_filter(evsel, ncpus, nthreads, evsel->filter);
8590a102479SFrederic Weisbecker 		if (err)
860745cefc5SArnaldo Carvalho de Melo 			break;
8610a102479SFrederic Weisbecker 	}
8620a102479SFrederic Weisbecker 
863745cefc5SArnaldo Carvalho de Melo 	return err;
864745cefc5SArnaldo Carvalho de Melo }
865745cefc5SArnaldo Carvalho de Melo 
866745cefc5SArnaldo Carvalho de Melo int perf_evlist__set_filter(struct perf_evlist *evlist, const char *filter)
867745cefc5SArnaldo Carvalho de Melo {
868745cefc5SArnaldo Carvalho de Melo 	struct perf_evsel *evsel;
869745cefc5SArnaldo Carvalho de Melo 	int err = 0;
870745cefc5SArnaldo Carvalho de Melo 	const int ncpus = cpu_map__nr(evlist->cpus),
871b3a319d5SNamhyung Kim 		  nthreads = thread_map__nr(evlist->threads);
872745cefc5SArnaldo Carvalho de Melo 
873745cefc5SArnaldo Carvalho de Melo 	list_for_each_entry(evsel, &evlist->entries, node) {
874745cefc5SArnaldo Carvalho de Melo 		err = perf_evsel__set_filter(evsel, ncpus, nthreads, filter);
875745cefc5SArnaldo Carvalho de Melo 		if (err)
876745cefc5SArnaldo Carvalho de Melo 			break;
877745cefc5SArnaldo Carvalho de Melo 	}
878745cefc5SArnaldo Carvalho de Melo 
879745cefc5SArnaldo Carvalho de Melo 	return err;
8800a102479SFrederic Weisbecker }
88174429964SFrederic Weisbecker 
8820c21f736SArnaldo Carvalho de Melo bool perf_evlist__valid_sample_type(struct perf_evlist *evlist)
88374429964SFrederic Weisbecker {
88475562573SAdrian Hunter 	struct perf_evsel *pos;
885c2a70653SArnaldo Carvalho de Melo 
88675562573SAdrian Hunter 	if (evlist->nr_entries == 1)
88775562573SAdrian Hunter 		return true;
88875562573SAdrian Hunter 
88975562573SAdrian Hunter 	if (evlist->id_pos < 0 || evlist->is_pos < 0)
89075562573SAdrian Hunter 		return false;
89175562573SAdrian Hunter 
89275562573SAdrian Hunter 	list_for_each_entry(pos, &evlist->entries, node) {
89375562573SAdrian Hunter 		if (pos->id_pos != evlist->id_pos ||
89475562573SAdrian Hunter 		    pos->is_pos != evlist->is_pos)
895c2a70653SArnaldo Carvalho de Melo 			return false;
89674429964SFrederic Weisbecker 	}
89774429964SFrederic Weisbecker 
898c2a70653SArnaldo Carvalho de Melo 	return true;
899c2a70653SArnaldo Carvalho de Melo }
900c2a70653SArnaldo Carvalho de Melo 
90175562573SAdrian Hunter u64 __perf_evlist__combined_sample_type(struct perf_evlist *evlist)
902c2a70653SArnaldo Carvalho de Melo {
90375562573SAdrian Hunter 	struct perf_evsel *evsel;
90475562573SAdrian Hunter 
90575562573SAdrian Hunter 	if (evlist->combined_sample_type)
90675562573SAdrian Hunter 		return evlist->combined_sample_type;
90775562573SAdrian Hunter 
90875562573SAdrian Hunter 	list_for_each_entry(evsel, &evlist->entries, node)
90975562573SAdrian Hunter 		evlist->combined_sample_type |= evsel->attr.sample_type;
91075562573SAdrian Hunter 
91175562573SAdrian Hunter 	return evlist->combined_sample_type;
91275562573SAdrian Hunter }
91375562573SAdrian Hunter 
91475562573SAdrian Hunter u64 perf_evlist__combined_sample_type(struct perf_evlist *evlist)
91575562573SAdrian Hunter {
91675562573SAdrian Hunter 	evlist->combined_sample_type = 0;
91775562573SAdrian Hunter 	return __perf_evlist__combined_sample_type(evlist);
918c2a70653SArnaldo Carvalho de Melo }
919c2a70653SArnaldo Carvalho de Melo 
9209ede473cSJiri Olsa bool perf_evlist__valid_read_format(struct perf_evlist *evlist)
9219ede473cSJiri Olsa {
9229ede473cSJiri Olsa 	struct perf_evsel *first = perf_evlist__first(evlist), *pos = first;
9239ede473cSJiri Olsa 	u64 read_format = first->attr.read_format;
9249ede473cSJiri Olsa 	u64 sample_type = first->attr.sample_type;
9259ede473cSJiri Olsa 
9269ede473cSJiri Olsa 	list_for_each_entry_continue(pos, &evlist->entries, node) {
9279ede473cSJiri Olsa 		if (read_format != pos->attr.read_format)
9289ede473cSJiri Olsa 			return false;
9299ede473cSJiri Olsa 	}
9309ede473cSJiri Olsa 
9319ede473cSJiri Olsa 	/* PERF_SAMPLE_READ imples PERF_FORMAT_ID. */
9329ede473cSJiri Olsa 	if ((sample_type & PERF_SAMPLE_READ) &&
9339ede473cSJiri Olsa 	    !(read_format & PERF_FORMAT_ID)) {
9349ede473cSJiri Olsa 		return false;
9359ede473cSJiri Olsa 	}
9369ede473cSJiri Olsa 
9379ede473cSJiri Olsa 	return true;
9389ede473cSJiri Olsa }
9399ede473cSJiri Olsa 
9409ede473cSJiri Olsa u64 perf_evlist__read_format(struct perf_evlist *evlist)
9419ede473cSJiri Olsa {
9429ede473cSJiri Olsa 	struct perf_evsel *first = perf_evlist__first(evlist);
9439ede473cSJiri Olsa 	return first->attr.read_format;
9449ede473cSJiri Olsa }
9459ede473cSJiri Olsa 
9460c21f736SArnaldo Carvalho de Melo u16 perf_evlist__id_hdr_size(struct perf_evlist *evlist)
94781e36bffSArnaldo Carvalho de Melo {
9480c21f736SArnaldo Carvalho de Melo 	struct perf_evsel *first = perf_evlist__first(evlist);
94981e36bffSArnaldo Carvalho de Melo 	struct perf_sample *data;
95081e36bffSArnaldo Carvalho de Melo 	u64 sample_type;
95181e36bffSArnaldo Carvalho de Melo 	u16 size = 0;
95281e36bffSArnaldo Carvalho de Melo 
95381e36bffSArnaldo Carvalho de Melo 	if (!first->attr.sample_id_all)
95481e36bffSArnaldo Carvalho de Melo 		goto out;
95581e36bffSArnaldo Carvalho de Melo 
95681e36bffSArnaldo Carvalho de Melo 	sample_type = first->attr.sample_type;
95781e36bffSArnaldo Carvalho de Melo 
95881e36bffSArnaldo Carvalho de Melo 	if (sample_type & PERF_SAMPLE_TID)
95981e36bffSArnaldo Carvalho de Melo 		size += sizeof(data->tid) * 2;
96081e36bffSArnaldo Carvalho de Melo 
96181e36bffSArnaldo Carvalho de Melo        if (sample_type & PERF_SAMPLE_TIME)
96281e36bffSArnaldo Carvalho de Melo 		size += sizeof(data->time);
96381e36bffSArnaldo Carvalho de Melo 
96481e36bffSArnaldo Carvalho de Melo 	if (sample_type & PERF_SAMPLE_ID)
96581e36bffSArnaldo Carvalho de Melo 		size += sizeof(data->id);
96681e36bffSArnaldo Carvalho de Melo 
96781e36bffSArnaldo Carvalho de Melo 	if (sample_type & PERF_SAMPLE_STREAM_ID)
96881e36bffSArnaldo Carvalho de Melo 		size += sizeof(data->stream_id);
96981e36bffSArnaldo Carvalho de Melo 
97081e36bffSArnaldo Carvalho de Melo 	if (sample_type & PERF_SAMPLE_CPU)
97181e36bffSArnaldo Carvalho de Melo 		size += sizeof(data->cpu) * 2;
97275562573SAdrian Hunter 
97375562573SAdrian Hunter 	if (sample_type & PERF_SAMPLE_IDENTIFIER)
97475562573SAdrian Hunter 		size += sizeof(data->id);
97581e36bffSArnaldo Carvalho de Melo out:
97681e36bffSArnaldo Carvalho de Melo 	return size;
97781e36bffSArnaldo Carvalho de Melo }
97881e36bffSArnaldo Carvalho de Melo 
9790c21f736SArnaldo Carvalho de Melo bool perf_evlist__valid_sample_id_all(struct perf_evlist *evlist)
980c2a70653SArnaldo Carvalho de Melo {
9810c21f736SArnaldo Carvalho de Melo 	struct perf_evsel *first = perf_evlist__first(evlist), *pos = first;
982c2a70653SArnaldo Carvalho de Melo 
983c2a70653SArnaldo Carvalho de Melo 	list_for_each_entry_continue(pos, &evlist->entries, node) {
984c2a70653SArnaldo Carvalho de Melo 		if (first->attr.sample_id_all != pos->attr.sample_id_all)
985c2a70653SArnaldo Carvalho de Melo 			return false;
986c2a70653SArnaldo Carvalho de Melo 	}
987c2a70653SArnaldo Carvalho de Melo 
988c2a70653SArnaldo Carvalho de Melo 	return true;
98974429964SFrederic Weisbecker }
99074429964SFrederic Weisbecker 
9910c21f736SArnaldo Carvalho de Melo bool perf_evlist__sample_id_all(struct perf_evlist *evlist)
99274429964SFrederic Weisbecker {
9930c21f736SArnaldo Carvalho de Melo 	struct perf_evsel *first = perf_evlist__first(evlist);
994c2a70653SArnaldo Carvalho de Melo 	return first->attr.sample_id_all;
99574429964SFrederic Weisbecker }
99681cce8deSArnaldo Carvalho de Melo 
99781cce8deSArnaldo Carvalho de Melo void perf_evlist__set_selected(struct perf_evlist *evlist,
99881cce8deSArnaldo Carvalho de Melo 			       struct perf_evsel *evsel)
99981cce8deSArnaldo Carvalho de Melo {
100081cce8deSArnaldo Carvalho de Melo 	evlist->selected = evsel;
100181cce8deSArnaldo Carvalho de Melo }
1002727ab04eSArnaldo Carvalho de Melo 
1003a74b4b66SNamhyung Kim void perf_evlist__close(struct perf_evlist *evlist)
1004a74b4b66SNamhyung Kim {
1005a74b4b66SNamhyung Kim 	struct perf_evsel *evsel;
1006a74b4b66SNamhyung Kim 	int ncpus = cpu_map__nr(evlist->cpus);
1007a74b4b66SNamhyung Kim 	int nthreads = thread_map__nr(evlist->threads);
1008a74b4b66SNamhyung Kim 
1009a74b4b66SNamhyung Kim 	list_for_each_entry_reverse(evsel, &evlist->entries, node)
1010a74b4b66SNamhyung Kim 		perf_evsel__close(evsel, ncpus, nthreads);
1011a74b4b66SNamhyung Kim }
1012a74b4b66SNamhyung Kim 
10136a4bb04cSJiri Olsa int perf_evlist__open(struct perf_evlist *evlist)
1014727ab04eSArnaldo Carvalho de Melo {
10156a4bb04cSJiri Olsa 	struct perf_evsel *evsel;
1016a74b4b66SNamhyung Kim 	int err;
1017727ab04eSArnaldo Carvalho de Melo 
1018733cd2feSAdrian Hunter 	perf_evlist__update_id_pos(evlist);
1019733cd2feSAdrian Hunter 
1020727ab04eSArnaldo Carvalho de Melo 	list_for_each_entry(evsel, &evlist->entries, node) {
10216a4bb04cSJiri Olsa 		err = perf_evsel__open(evsel, evlist->cpus, evlist->threads);
1022727ab04eSArnaldo Carvalho de Melo 		if (err < 0)
1023727ab04eSArnaldo Carvalho de Melo 			goto out_err;
1024727ab04eSArnaldo Carvalho de Melo 	}
1025727ab04eSArnaldo Carvalho de Melo 
1026727ab04eSArnaldo Carvalho de Melo 	return 0;
1027727ab04eSArnaldo Carvalho de Melo out_err:
1028a74b4b66SNamhyung Kim 	perf_evlist__close(evlist);
102941c21a68SNamhyung Kim 	errno = -err;
1030727ab04eSArnaldo Carvalho de Melo 	return err;
1031727ab04eSArnaldo Carvalho de Melo }
103235b9d88eSArnaldo Carvalho de Melo 
1033602ad878SArnaldo Carvalho de Melo int perf_evlist__prepare_workload(struct perf_evlist *evlist, struct target *target,
103455e162eaSNamhyung Kim 				  const char *argv[], bool pipe_output,
103555e162eaSNamhyung Kim 				  bool want_signal)
103635b9d88eSArnaldo Carvalho de Melo {
103735b9d88eSArnaldo Carvalho de Melo 	int child_ready_pipe[2], go_pipe[2];
103835b9d88eSArnaldo Carvalho de Melo 	char bf;
103935b9d88eSArnaldo Carvalho de Melo 
104035b9d88eSArnaldo Carvalho de Melo 	if (pipe(child_ready_pipe) < 0) {
104135b9d88eSArnaldo Carvalho de Melo 		perror("failed to create 'ready' pipe");
104235b9d88eSArnaldo Carvalho de Melo 		return -1;
104335b9d88eSArnaldo Carvalho de Melo 	}
104435b9d88eSArnaldo Carvalho de Melo 
104535b9d88eSArnaldo Carvalho de Melo 	if (pipe(go_pipe) < 0) {
104635b9d88eSArnaldo Carvalho de Melo 		perror("failed to create 'go' pipe");
104735b9d88eSArnaldo Carvalho de Melo 		goto out_close_ready_pipe;
104835b9d88eSArnaldo Carvalho de Melo 	}
104935b9d88eSArnaldo Carvalho de Melo 
105035b9d88eSArnaldo Carvalho de Melo 	evlist->workload.pid = fork();
105135b9d88eSArnaldo Carvalho de Melo 	if (evlist->workload.pid < 0) {
105235b9d88eSArnaldo Carvalho de Melo 		perror("failed to fork");
105335b9d88eSArnaldo Carvalho de Melo 		goto out_close_pipes;
105435b9d88eSArnaldo Carvalho de Melo 	}
105535b9d88eSArnaldo Carvalho de Melo 
105635b9d88eSArnaldo Carvalho de Melo 	if (!evlist->workload.pid) {
1057119fa3c9SNamhyung Kim 		if (pipe_output)
105835b9d88eSArnaldo Carvalho de Melo 			dup2(2, 1);
105935b9d88eSArnaldo Carvalho de Melo 
10600817df08SDavid Ahern 		signal(SIGTERM, SIG_DFL);
10610817df08SDavid Ahern 
106235b9d88eSArnaldo Carvalho de Melo 		close(child_ready_pipe[0]);
106335b9d88eSArnaldo Carvalho de Melo 		close(go_pipe[1]);
106435b9d88eSArnaldo Carvalho de Melo 		fcntl(go_pipe[0], F_SETFD, FD_CLOEXEC);
106535b9d88eSArnaldo Carvalho de Melo 
106635b9d88eSArnaldo Carvalho de Melo 		/*
106735b9d88eSArnaldo Carvalho de Melo 		 * Tell the parent we're ready to go
106835b9d88eSArnaldo Carvalho de Melo 		 */
106935b9d88eSArnaldo Carvalho de Melo 		close(child_ready_pipe[1]);
107035b9d88eSArnaldo Carvalho de Melo 
107135b9d88eSArnaldo Carvalho de Melo 		/*
107235b9d88eSArnaldo Carvalho de Melo 		 * Wait until the parent tells us to go.
107335b9d88eSArnaldo Carvalho de Melo 		 */
107435b9d88eSArnaldo Carvalho de Melo 		if (read(go_pipe[0], &bf, 1) == -1)
107535b9d88eSArnaldo Carvalho de Melo 			perror("unable to read pipe");
107635b9d88eSArnaldo Carvalho de Melo 
107735b9d88eSArnaldo Carvalho de Melo 		execvp(argv[0], (char **)argv);
107835b9d88eSArnaldo Carvalho de Melo 
107935b9d88eSArnaldo Carvalho de Melo 		perror(argv[0]);
108055e162eaSNamhyung Kim 		if (want_signal)
108135b9d88eSArnaldo Carvalho de Melo 			kill(getppid(), SIGUSR1);
108235b9d88eSArnaldo Carvalho de Melo 		exit(-1);
108335b9d88eSArnaldo Carvalho de Melo 	}
108435b9d88eSArnaldo Carvalho de Melo 
1085602ad878SArnaldo Carvalho de Melo 	if (target__none(target))
108635b9d88eSArnaldo Carvalho de Melo 		evlist->threads->map[0] = evlist->workload.pid;
108735b9d88eSArnaldo Carvalho de Melo 
108835b9d88eSArnaldo Carvalho de Melo 	close(child_ready_pipe[1]);
108935b9d88eSArnaldo Carvalho de Melo 	close(go_pipe[0]);
109035b9d88eSArnaldo Carvalho de Melo 	/*
109135b9d88eSArnaldo Carvalho de Melo 	 * wait for child to settle
109235b9d88eSArnaldo Carvalho de Melo 	 */
109335b9d88eSArnaldo Carvalho de Melo 	if (read(child_ready_pipe[0], &bf, 1) == -1) {
109435b9d88eSArnaldo Carvalho de Melo 		perror("unable to read pipe");
109535b9d88eSArnaldo Carvalho de Melo 		goto out_close_pipes;
109635b9d88eSArnaldo Carvalho de Melo 	}
109735b9d88eSArnaldo Carvalho de Melo 
1098bcf3145fSNamhyung Kim 	fcntl(go_pipe[1], F_SETFD, FD_CLOEXEC);
109935b9d88eSArnaldo Carvalho de Melo 	evlist->workload.cork_fd = go_pipe[1];
110035b9d88eSArnaldo Carvalho de Melo 	close(child_ready_pipe[0]);
110135b9d88eSArnaldo Carvalho de Melo 	return 0;
110235b9d88eSArnaldo Carvalho de Melo 
110335b9d88eSArnaldo Carvalho de Melo out_close_pipes:
110435b9d88eSArnaldo Carvalho de Melo 	close(go_pipe[0]);
110535b9d88eSArnaldo Carvalho de Melo 	close(go_pipe[1]);
110635b9d88eSArnaldo Carvalho de Melo out_close_ready_pipe:
110735b9d88eSArnaldo Carvalho de Melo 	close(child_ready_pipe[0]);
110835b9d88eSArnaldo Carvalho de Melo 	close(child_ready_pipe[1]);
110935b9d88eSArnaldo Carvalho de Melo 	return -1;
111035b9d88eSArnaldo Carvalho de Melo }
111135b9d88eSArnaldo Carvalho de Melo 
111235b9d88eSArnaldo Carvalho de Melo int perf_evlist__start_workload(struct perf_evlist *evlist)
111335b9d88eSArnaldo Carvalho de Melo {
111435b9d88eSArnaldo Carvalho de Melo 	if (evlist->workload.cork_fd > 0) {
1115b3824404SDavid Ahern 		char bf = 0;
1116bcf3145fSNamhyung Kim 		int ret;
111735b9d88eSArnaldo Carvalho de Melo 		/*
111835b9d88eSArnaldo Carvalho de Melo 		 * Remove the cork, let it rip!
111935b9d88eSArnaldo Carvalho de Melo 		 */
1120bcf3145fSNamhyung Kim 		ret = write(evlist->workload.cork_fd, &bf, 1);
1121bcf3145fSNamhyung Kim 		if (ret < 0)
1122bcf3145fSNamhyung Kim 			perror("enable to write to pipe");
1123bcf3145fSNamhyung Kim 
1124bcf3145fSNamhyung Kim 		close(evlist->workload.cork_fd);
1125bcf3145fSNamhyung Kim 		return ret;
112635b9d88eSArnaldo Carvalho de Melo 	}
112735b9d88eSArnaldo Carvalho de Melo 
112835b9d88eSArnaldo Carvalho de Melo 	return 0;
112935b9d88eSArnaldo Carvalho de Melo }
1130cb0b29e0SArnaldo Carvalho de Melo 
1131a3f698feSArnaldo Carvalho de Melo int perf_evlist__parse_sample(struct perf_evlist *evlist, union perf_event *event,
11320807d2d8SArnaldo Carvalho de Melo 			      struct perf_sample *sample)
1133cb0b29e0SArnaldo Carvalho de Melo {
113475562573SAdrian Hunter 	struct perf_evsel *evsel = perf_evlist__event2evsel(evlist, event);
113575562573SAdrian Hunter 
113675562573SAdrian Hunter 	if (!evsel)
113775562573SAdrian Hunter 		return -EFAULT;
11380807d2d8SArnaldo Carvalho de Melo 	return perf_evsel__parse_sample(evsel, event, sample);
1139cb0b29e0SArnaldo Carvalho de Melo }
114078f067b3SArnaldo Carvalho de Melo 
114178f067b3SArnaldo Carvalho de Melo size_t perf_evlist__fprintf(struct perf_evlist *evlist, FILE *fp)
114278f067b3SArnaldo Carvalho de Melo {
114378f067b3SArnaldo Carvalho de Melo 	struct perf_evsel *evsel;
114478f067b3SArnaldo Carvalho de Melo 	size_t printed = 0;
114578f067b3SArnaldo Carvalho de Melo 
114678f067b3SArnaldo Carvalho de Melo 	list_for_each_entry(evsel, &evlist->entries, node) {
114778f067b3SArnaldo Carvalho de Melo 		printed += fprintf(fp, "%s%s", evsel->idx ? ", " : "",
114878f067b3SArnaldo Carvalho de Melo 				   perf_evsel__name(evsel));
114978f067b3SArnaldo Carvalho de Melo 	}
115078f067b3SArnaldo Carvalho de Melo 
1151b2222139SDavidlohr Bueso 	return printed + fprintf(fp, "\n");
115278f067b3SArnaldo Carvalho de Melo }
11536ef068cbSArnaldo Carvalho de Melo 
11546ef068cbSArnaldo Carvalho de Melo int perf_evlist__strerror_tp(struct perf_evlist *evlist __maybe_unused,
11556ef068cbSArnaldo Carvalho de Melo 			     int err, char *buf, size_t size)
11566ef068cbSArnaldo Carvalho de Melo {
11576ef068cbSArnaldo Carvalho de Melo 	char sbuf[128];
11586ef068cbSArnaldo Carvalho de Melo 
11596ef068cbSArnaldo Carvalho de Melo 	switch (err) {
11606ef068cbSArnaldo Carvalho de Melo 	case ENOENT:
11616ef068cbSArnaldo Carvalho de Melo 		scnprintf(buf, size, "%s",
11626ef068cbSArnaldo Carvalho de Melo 			  "Error:\tUnable to find debugfs\n"
11636ef068cbSArnaldo Carvalho de Melo 			  "Hint:\tWas your kernel was compiled with debugfs support?\n"
11646ef068cbSArnaldo Carvalho de Melo 			  "Hint:\tIs the debugfs filesystem mounted?\n"
11656ef068cbSArnaldo Carvalho de Melo 			  "Hint:\tTry 'sudo mount -t debugfs nodev /sys/kernel/debug'");
11666ef068cbSArnaldo Carvalho de Melo 		break;
11676ef068cbSArnaldo Carvalho de Melo 	case EACCES:
11686ef068cbSArnaldo Carvalho de Melo 		scnprintf(buf, size,
11696ef068cbSArnaldo Carvalho de Melo 			  "Error:\tNo permissions to read %s/tracing/events/raw_syscalls\n"
11706ef068cbSArnaldo Carvalho de Melo 			  "Hint:\tTry 'sudo mount -o remount,mode=755 %s'\n",
11716ef068cbSArnaldo Carvalho de Melo 			  debugfs_mountpoint, debugfs_mountpoint);
11726ef068cbSArnaldo Carvalho de Melo 		break;
11736ef068cbSArnaldo Carvalho de Melo 	default:
11746ef068cbSArnaldo Carvalho de Melo 		scnprintf(buf, size, "%s", strerror_r(err, sbuf, sizeof(sbuf)));
11756ef068cbSArnaldo Carvalho de Melo 		break;
11766ef068cbSArnaldo Carvalho de Melo 	}
11776ef068cbSArnaldo Carvalho de Melo 
11786ef068cbSArnaldo Carvalho de Melo 	return 0;
11796ef068cbSArnaldo Carvalho de Melo }
1180a8f23d8fSArnaldo Carvalho de Melo 
1181a8f23d8fSArnaldo Carvalho de Melo int perf_evlist__strerror_open(struct perf_evlist *evlist __maybe_unused,
1182a8f23d8fSArnaldo Carvalho de Melo 			       int err, char *buf, size_t size)
1183a8f23d8fSArnaldo Carvalho de Melo {
1184a8f23d8fSArnaldo Carvalho de Melo 	int printed, value;
1185a8f23d8fSArnaldo Carvalho de Melo 	char sbuf[128], *emsg = strerror_r(err, sbuf, sizeof(sbuf));
1186a8f23d8fSArnaldo Carvalho de Melo 
1187a8f23d8fSArnaldo Carvalho de Melo 	switch (err) {
1188a8f23d8fSArnaldo Carvalho de Melo 	case EACCES:
1189a8f23d8fSArnaldo Carvalho de Melo 	case EPERM:
1190a8f23d8fSArnaldo Carvalho de Melo 		printed = scnprintf(buf, size,
1191a8f23d8fSArnaldo Carvalho de Melo 				    "Error:\t%s.\n"
1192a8f23d8fSArnaldo Carvalho de Melo 				    "Hint:\tCheck /proc/sys/kernel/perf_event_paranoid setting.", emsg);
1193a8f23d8fSArnaldo Carvalho de Melo 
1194a8f23d8fSArnaldo Carvalho de Melo 		if (filename__read_int("/proc/sys/kernel/perf_event_paranoid", &value))
1195a8f23d8fSArnaldo Carvalho de Melo 			break;
1196a8f23d8fSArnaldo Carvalho de Melo 
1197a8f23d8fSArnaldo Carvalho de Melo 		printed += scnprintf(buf + printed, size - printed, "\nHint:\t");
1198a8f23d8fSArnaldo Carvalho de Melo 
1199a8f23d8fSArnaldo Carvalho de Melo 		if (value >= 2) {
1200a8f23d8fSArnaldo Carvalho de Melo 			printed += scnprintf(buf + printed, size - printed,
1201a8f23d8fSArnaldo Carvalho de Melo 					     "For your workloads it needs to be <= 1\nHint:\t");
1202a8f23d8fSArnaldo Carvalho de Melo 		}
1203a8f23d8fSArnaldo Carvalho de Melo 		printed += scnprintf(buf + printed, size - printed,
1204a8f23d8fSArnaldo Carvalho de Melo 				     "For system wide tracing it needs to be set to -1");
1205a8f23d8fSArnaldo Carvalho de Melo 
1206a8f23d8fSArnaldo Carvalho de Melo 		printed += scnprintf(buf + printed, size - printed,
1207a8f23d8fSArnaldo Carvalho de Melo 				    ".\nHint:\tThe current value is %d.", value);
1208a8f23d8fSArnaldo Carvalho de Melo 		break;
1209a8f23d8fSArnaldo Carvalho de Melo 	default:
1210a8f23d8fSArnaldo Carvalho de Melo 		scnprintf(buf, size, "%s", emsg);
1211a8f23d8fSArnaldo Carvalho de Melo 		break;
1212a8f23d8fSArnaldo Carvalho de Melo 	}
1213a8f23d8fSArnaldo Carvalho de Melo 
1214a8f23d8fSArnaldo Carvalho de Melo 	return 0;
1215a8f23d8fSArnaldo Carvalho de Melo }
1216