xref: /openbmc/linux/tools/perf/util/evlist.c (revision ad6765dd)
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 
840050f7aaSArnaldo Carvalho de Melo 	evlist__for_each(evlist, evsel)
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 
940050f7aaSArnaldo Carvalho de Melo 	evlist__for_each_safe(evlist, n, pos) {
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;
12560b0896cSAdrian Hunter 	entry->tracking = !entry->idx;
126ef503831SArnaldo Carvalho de Melo 
12775562573SAdrian Hunter 	if (!evlist->nr_entries++)
12875562573SAdrian Hunter 		perf_evlist__set_id_pos(evlist);
129361c99a6SArnaldo Carvalho de Melo }
130361c99a6SArnaldo Carvalho de Melo 
1310529bc1fSJiri Olsa void perf_evlist__splice_list_tail(struct perf_evlist *evlist,
13250d08e47SArnaldo Carvalho de Melo 				   struct list_head *list,
13350d08e47SArnaldo Carvalho de Melo 				   int nr_entries)
13450d08e47SArnaldo Carvalho de Melo {
13575562573SAdrian Hunter 	bool set_id_pos = !evlist->nr_entries;
13675562573SAdrian Hunter 
13750d08e47SArnaldo Carvalho de Melo 	list_splice_tail(list, &evlist->entries);
13850d08e47SArnaldo Carvalho de Melo 	evlist->nr_entries += nr_entries;
13975562573SAdrian Hunter 	if (set_id_pos)
14075562573SAdrian Hunter 		perf_evlist__set_id_pos(evlist);
14150d08e47SArnaldo Carvalho de Melo }
14250d08e47SArnaldo Carvalho de Melo 
14363dab225SArnaldo Carvalho de Melo void __perf_evlist__set_leader(struct list_head *list)
14463dab225SArnaldo Carvalho de Melo {
14563dab225SArnaldo Carvalho de Melo 	struct perf_evsel *evsel, *leader;
14663dab225SArnaldo Carvalho de Melo 
14763dab225SArnaldo Carvalho de Melo 	leader = list_entry(list->next, struct perf_evsel, node);
14897f63e4aSNamhyung Kim 	evsel = list_entry(list->prev, struct perf_evsel, node);
14997f63e4aSNamhyung Kim 
15097f63e4aSNamhyung Kim 	leader->nr_members = evsel->idx - leader->idx + 1;
15163dab225SArnaldo Carvalho de Melo 
1520050f7aaSArnaldo Carvalho de Melo 	__evlist__for_each(list, evsel) {
15363dab225SArnaldo Carvalho de Melo 		evsel->leader = leader;
15463dab225SArnaldo Carvalho de Melo 	}
15563dab225SArnaldo Carvalho de Melo }
15663dab225SArnaldo Carvalho de Melo 
15763dab225SArnaldo Carvalho de Melo void perf_evlist__set_leader(struct perf_evlist *evlist)
1586a4bb04cSJiri Olsa {
15997f63e4aSNamhyung Kim 	if (evlist->nr_entries) {
16097f63e4aSNamhyung Kim 		evlist->nr_groups = evlist->nr_entries > 1 ? 1 : 0;
16163dab225SArnaldo Carvalho de Melo 		__perf_evlist__set_leader(&evlist->entries);
1626a4bb04cSJiri Olsa 	}
16397f63e4aSNamhyung Kim }
1646a4bb04cSJiri Olsa 
165361c99a6SArnaldo Carvalho de Melo int perf_evlist__add_default(struct perf_evlist *evlist)
166361c99a6SArnaldo Carvalho de Melo {
167361c99a6SArnaldo Carvalho de Melo 	struct perf_event_attr attr = {
168361c99a6SArnaldo Carvalho de Melo 		.type = PERF_TYPE_HARDWARE,
169361c99a6SArnaldo Carvalho de Melo 		.config = PERF_COUNT_HW_CPU_CYCLES,
170361c99a6SArnaldo Carvalho de Melo 	};
1711aed2671SJoerg Roedel 	struct perf_evsel *evsel;
172361c99a6SArnaldo Carvalho de Melo 
1731aed2671SJoerg Roedel 	event_attr_init(&attr);
1741aed2671SJoerg Roedel 
175ef503831SArnaldo Carvalho de Melo 	evsel = perf_evsel__new(&attr);
176361c99a6SArnaldo Carvalho de Melo 	if (evsel == NULL)
177cc2d86b0SStephane Eranian 		goto error;
178cc2d86b0SStephane Eranian 
179cc2d86b0SStephane Eranian 	/* use strdup() because free(evsel) assumes name is allocated */
180cc2d86b0SStephane Eranian 	evsel->name = strdup("cycles");
181cc2d86b0SStephane Eranian 	if (!evsel->name)
182cc2d86b0SStephane Eranian 		goto error_free;
183361c99a6SArnaldo Carvalho de Melo 
184361c99a6SArnaldo Carvalho de Melo 	perf_evlist__add(evlist, evsel);
185361c99a6SArnaldo Carvalho de Melo 	return 0;
186cc2d86b0SStephane Eranian error_free:
187cc2d86b0SStephane Eranian 	perf_evsel__delete(evsel);
188cc2d86b0SStephane Eranian error:
189cc2d86b0SStephane Eranian 	return -ENOMEM;
190361c99a6SArnaldo Carvalho de Melo }
1915c581041SArnaldo Carvalho de Melo 
192e60fc847SArnaldo Carvalho de Melo static int perf_evlist__add_attrs(struct perf_evlist *evlist,
19350d08e47SArnaldo Carvalho de Melo 				  struct perf_event_attr *attrs, size_t nr_attrs)
19450d08e47SArnaldo Carvalho de Melo {
19550d08e47SArnaldo Carvalho de Melo 	struct perf_evsel *evsel, *n;
19650d08e47SArnaldo Carvalho de Melo 	LIST_HEAD(head);
19750d08e47SArnaldo Carvalho de Melo 	size_t i;
19850d08e47SArnaldo Carvalho de Melo 
19950d08e47SArnaldo Carvalho de Melo 	for (i = 0; i < nr_attrs; i++) {
200ef503831SArnaldo Carvalho de Melo 		evsel = perf_evsel__new_idx(attrs + i, evlist->nr_entries + i);
20150d08e47SArnaldo Carvalho de Melo 		if (evsel == NULL)
20250d08e47SArnaldo Carvalho de Melo 			goto out_delete_partial_list;
20350d08e47SArnaldo Carvalho de Melo 		list_add_tail(&evsel->node, &head);
20450d08e47SArnaldo Carvalho de Melo 	}
20550d08e47SArnaldo Carvalho de Melo 
20650d08e47SArnaldo Carvalho de Melo 	perf_evlist__splice_list_tail(evlist, &head, nr_attrs);
20750d08e47SArnaldo Carvalho de Melo 
20850d08e47SArnaldo Carvalho de Melo 	return 0;
20950d08e47SArnaldo Carvalho de Melo 
21050d08e47SArnaldo Carvalho de Melo out_delete_partial_list:
2110050f7aaSArnaldo Carvalho de Melo 	__evlist__for_each_safe(&head, n, evsel)
21250d08e47SArnaldo Carvalho de Melo 		perf_evsel__delete(evsel);
21350d08e47SArnaldo Carvalho de Melo 	return -1;
21450d08e47SArnaldo Carvalho de Melo }
21550d08e47SArnaldo Carvalho de Melo 
21679695e1bSArnaldo Carvalho de Melo int __perf_evlist__add_default_attrs(struct perf_evlist *evlist,
21779695e1bSArnaldo Carvalho de Melo 				     struct perf_event_attr *attrs, size_t nr_attrs)
21879695e1bSArnaldo Carvalho de Melo {
21979695e1bSArnaldo Carvalho de Melo 	size_t i;
22079695e1bSArnaldo Carvalho de Melo 
22179695e1bSArnaldo Carvalho de Melo 	for (i = 0; i < nr_attrs; i++)
22279695e1bSArnaldo Carvalho de Melo 		event_attr_init(attrs + i);
22379695e1bSArnaldo Carvalho de Melo 
22479695e1bSArnaldo Carvalho de Melo 	return perf_evlist__add_attrs(evlist, attrs, nr_attrs);
22579695e1bSArnaldo Carvalho de Melo }
22679695e1bSArnaldo Carvalho de Melo 
227da378962SArnaldo Carvalho de Melo struct perf_evsel *
228ee29be62SArnaldo Carvalho de Melo perf_evlist__find_tracepoint_by_id(struct perf_evlist *evlist, int id)
229ee29be62SArnaldo Carvalho de Melo {
230ee29be62SArnaldo Carvalho de Melo 	struct perf_evsel *evsel;
231ee29be62SArnaldo Carvalho de Melo 
2320050f7aaSArnaldo Carvalho de Melo 	evlist__for_each(evlist, evsel) {
233ee29be62SArnaldo Carvalho de Melo 		if (evsel->attr.type   == PERF_TYPE_TRACEPOINT &&
234ee29be62SArnaldo Carvalho de Melo 		    (int)evsel->attr.config == id)
235ee29be62SArnaldo Carvalho de Melo 			return evsel;
236ee29be62SArnaldo Carvalho de Melo 	}
237ee29be62SArnaldo Carvalho de Melo 
238ee29be62SArnaldo Carvalho de Melo 	return NULL;
239ee29be62SArnaldo Carvalho de Melo }
240ee29be62SArnaldo Carvalho de Melo 
241a2f2804aSDavid Ahern struct perf_evsel *
242a2f2804aSDavid Ahern perf_evlist__find_tracepoint_by_name(struct perf_evlist *evlist,
243a2f2804aSDavid Ahern 				     const char *name)
244a2f2804aSDavid Ahern {
245a2f2804aSDavid Ahern 	struct perf_evsel *evsel;
246a2f2804aSDavid Ahern 
2470050f7aaSArnaldo Carvalho de Melo 	evlist__for_each(evlist, evsel) {
248a2f2804aSDavid Ahern 		if ((evsel->attr.type == PERF_TYPE_TRACEPOINT) &&
249a2f2804aSDavid Ahern 		    (strcmp(evsel->name, name) == 0))
250a2f2804aSDavid Ahern 			return evsel;
251a2f2804aSDavid Ahern 	}
252a2f2804aSDavid Ahern 
253a2f2804aSDavid Ahern 	return NULL;
254a2f2804aSDavid Ahern }
255a2f2804aSDavid Ahern 
25639876e7dSArnaldo Carvalho de Melo int perf_evlist__add_newtp(struct perf_evlist *evlist,
25739876e7dSArnaldo Carvalho de Melo 			   const char *sys, const char *name, void *handler)
25839876e7dSArnaldo Carvalho de Melo {
259ef503831SArnaldo Carvalho de Melo 	struct perf_evsel *evsel = perf_evsel__newtp(sys, name);
26039876e7dSArnaldo Carvalho de Melo 
26139876e7dSArnaldo Carvalho de Melo 	if (evsel == NULL)
26239876e7dSArnaldo Carvalho de Melo 		return -1;
26339876e7dSArnaldo Carvalho de Melo 
264744a9719SArnaldo Carvalho de Melo 	evsel->handler = handler;
26539876e7dSArnaldo Carvalho de Melo 	perf_evlist__add(evlist, evsel);
26639876e7dSArnaldo Carvalho de Melo 	return 0;
26739876e7dSArnaldo Carvalho de Melo }
26839876e7dSArnaldo Carvalho de Melo 
269bf8e8f4bSAdrian Hunter static int perf_evlist__nr_threads(struct perf_evlist *evlist,
270bf8e8f4bSAdrian Hunter 				   struct perf_evsel *evsel)
271bf8e8f4bSAdrian Hunter {
272bf8e8f4bSAdrian Hunter 	if (evsel->system_wide)
273bf8e8f4bSAdrian Hunter 		return 1;
274bf8e8f4bSAdrian Hunter 	else
275bf8e8f4bSAdrian Hunter 		return thread_map__nr(evlist->threads);
276bf8e8f4bSAdrian Hunter }
277bf8e8f4bSAdrian Hunter 
2784152ab37SArnaldo Carvalho de Melo void perf_evlist__disable(struct perf_evlist *evlist)
2794152ab37SArnaldo Carvalho de Melo {
2804152ab37SArnaldo Carvalho de Melo 	int cpu, thread;
2814152ab37SArnaldo Carvalho de Melo 	struct perf_evsel *pos;
282b3a319d5SNamhyung Kim 	int nr_cpus = cpu_map__nr(evlist->cpus);
283bf8e8f4bSAdrian Hunter 	int nr_threads;
2844152ab37SArnaldo Carvalho de Melo 
285b3a319d5SNamhyung Kim 	for (cpu = 0; cpu < nr_cpus; cpu++) {
2860050f7aaSArnaldo Carvalho de Melo 		evlist__for_each(evlist, pos) {
287395c3070SAdrian Hunter 			if (!perf_evsel__is_group_leader(pos) || !pos->fd)
2883fe4430dSJiri Olsa 				continue;
289bf8e8f4bSAdrian Hunter 			nr_threads = perf_evlist__nr_threads(evlist, pos);
290b3a319d5SNamhyung Kim 			for (thread = 0; thread < nr_threads; thread++)
29155da8005SNamhyung Kim 				ioctl(FD(pos, cpu, thread),
29255da8005SNamhyung Kim 				      PERF_EVENT_IOC_DISABLE, 0);
2934152ab37SArnaldo Carvalho de Melo 		}
2944152ab37SArnaldo Carvalho de Melo 	}
2954152ab37SArnaldo Carvalho de Melo }
2964152ab37SArnaldo Carvalho de Melo 
297764e16a3SDavid Ahern void perf_evlist__enable(struct perf_evlist *evlist)
298764e16a3SDavid Ahern {
299764e16a3SDavid Ahern 	int cpu, thread;
300764e16a3SDavid Ahern 	struct perf_evsel *pos;
301b3a319d5SNamhyung Kim 	int nr_cpus = cpu_map__nr(evlist->cpus);
302bf8e8f4bSAdrian Hunter 	int nr_threads;
303764e16a3SDavid Ahern 
304b3a319d5SNamhyung Kim 	for (cpu = 0; cpu < nr_cpus; cpu++) {
3050050f7aaSArnaldo Carvalho de Melo 		evlist__for_each(evlist, pos) {
306395c3070SAdrian Hunter 			if (!perf_evsel__is_group_leader(pos) || !pos->fd)
3073fe4430dSJiri Olsa 				continue;
308bf8e8f4bSAdrian Hunter 			nr_threads = perf_evlist__nr_threads(evlist, pos);
309b3a319d5SNamhyung Kim 			for (thread = 0; thread < nr_threads; thread++)
31055da8005SNamhyung Kim 				ioctl(FD(pos, cpu, thread),
31155da8005SNamhyung Kim 				      PERF_EVENT_IOC_ENABLE, 0);
312764e16a3SDavid Ahern 		}
313764e16a3SDavid Ahern 	}
314764e16a3SDavid Ahern }
315764e16a3SDavid Ahern 
316395c3070SAdrian Hunter int perf_evlist__disable_event(struct perf_evlist *evlist,
317395c3070SAdrian Hunter 			       struct perf_evsel *evsel)
318395c3070SAdrian Hunter {
319395c3070SAdrian Hunter 	int cpu, thread, err;
320bf8e8f4bSAdrian Hunter 	int nr_cpus = cpu_map__nr(evlist->cpus);
321bf8e8f4bSAdrian Hunter 	int nr_threads = perf_evlist__nr_threads(evlist, evsel);
322395c3070SAdrian Hunter 
323395c3070SAdrian Hunter 	if (!evsel->fd)
324395c3070SAdrian Hunter 		return 0;
325395c3070SAdrian Hunter 
326bf8e8f4bSAdrian Hunter 	for (cpu = 0; cpu < nr_cpus; cpu++) {
327bf8e8f4bSAdrian Hunter 		for (thread = 0; thread < nr_threads; thread++) {
328395c3070SAdrian Hunter 			err = ioctl(FD(evsel, cpu, thread),
329395c3070SAdrian Hunter 				    PERF_EVENT_IOC_DISABLE, 0);
330395c3070SAdrian Hunter 			if (err)
331395c3070SAdrian Hunter 				return err;
332395c3070SAdrian Hunter 		}
333395c3070SAdrian Hunter 	}
334395c3070SAdrian Hunter 	return 0;
335395c3070SAdrian Hunter }
336395c3070SAdrian Hunter 
337395c3070SAdrian Hunter int perf_evlist__enable_event(struct perf_evlist *evlist,
338395c3070SAdrian Hunter 			      struct perf_evsel *evsel)
339395c3070SAdrian Hunter {
340395c3070SAdrian Hunter 	int cpu, thread, err;
341bf8e8f4bSAdrian Hunter 	int nr_cpus = cpu_map__nr(evlist->cpus);
342bf8e8f4bSAdrian Hunter 	int nr_threads = perf_evlist__nr_threads(evlist, evsel);
343395c3070SAdrian Hunter 
344395c3070SAdrian Hunter 	if (!evsel->fd)
345395c3070SAdrian Hunter 		return -EINVAL;
346395c3070SAdrian Hunter 
347bf8e8f4bSAdrian Hunter 	for (cpu = 0; cpu < nr_cpus; cpu++) {
348bf8e8f4bSAdrian Hunter 		for (thread = 0; thread < nr_threads; thread++) {
349395c3070SAdrian Hunter 			err = ioctl(FD(evsel, cpu, thread),
350395c3070SAdrian Hunter 				    PERF_EVENT_IOC_ENABLE, 0);
351395c3070SAdrian Hunter 			if (err)
352395c3070SAdrian Hunter 				return err;
353395c3070SAdrian Hunter 		}
354395c3070SAdrian Hunter 	}
355395c3070SAdrian Hunter 	return 0;
356395c3070SAdrian Hunter }
357395c3070SAdrian Hunter 
3581c65056cSAdrian Hunter static int perf_evlist__enable_event_cpu(struct perf_evlist *evlist,
3591c65056cSAdrian Hunter 					 struct perf_evsel *evsel, int cpu)
3601c65056cSAdrian Hunter {
3611c65056cSAdrian Hunter 	int thread, err;
3621c65056cSAdrian Hunter 	int nr_threads = perf_evlist__nr_threads(evlist, evsel);
3631c65056cSAdrian Hunter 
3641c65056cSAdrian Hunter 	if (!evsel->fd)
3651c65056cSAdrian Hunter 		return -EINVAL;
3661c65056cSAdrian Hunter 
3671c65056cSAdrian Hunter 	for (thread = 0; thread < nr_threads; thread++) {
3681c65056cSAdrian Hunter 		err = ioctl(FD(evsel, cpu, thread),
3691c65056cSAdrian Hunter 			    PERF_EVENT_IOC_ENABLE, 0);
3701c65056cSAdrian Hunter 		if (err)
3711c65056cSAdrian Hunter 			return err;
3721c65056cSAdrian Hunter 	}
3731c65056cSAdrian Hunter 	return 0;
3741c65056cSAdrian Hunter }
3751c65056cSAdrian Hunter 
3761c65056cSAdrian Hunter static int perf_evlist__enable_event_thread(struct perf_evlist *evlist,
3771c65056cSAdrian Hunter 					    struct perf_evsel *evsel,
3781c65056cSAdrian Hunter 					    int thread)
3791c65056cSAdrian Hunter {
3801c65056cSAdrian Hunter 	int cpu, err;
3811c65056cSAdrian Hunter 	int nr_cpus = cpu_map__nr(evlist->cpus);
3821c65056cSAdrian Hunter 
3831c65056cSAdrian Hunter 	if (!evsel->fd)
3841c65056cSAdrian Hunter 		return -EINVAL;
3851c65056cSAdrian Hunter 
3861c65056cSAdrian Hunter 	for (cpu = 0; cpu < nr_cpus; cpu++) {
3871c65056cSAdrian Hunter 		err = ioctl(FD(evsel, cpu, thread), PERF_EVENT_IOC_ENABLE, 0);
3881c65056cSAdrian Hunter 		if (err)
3891c65056cSAdrian Hunter 			return err;
3901c65056cSAdrian Hunter 	}
3911c65056cSAdrian Hunter 	return 0;
3921c65056cSAdrian Hunter }
3931c65056cSAdrian Hunter 
3941c65056cSAdrian Hunter int perf_evlist__enable_event_idx(struct perf_evlist *evlist,
3951c65056cSAdrian Hunter 				  struct perf_evsel *evsel, int idx)
3961c65056cSAdrian Hunter {
3971c65056cSAdrian Hunter 	bool per_cpu_mmaps = !cpu_map__empty(evlist->cpus);
3981c65056cSAdrian Hunter 
3991c65056cSAdrian Hunter 	if (per_cpu_mmaps)
4001c65056cSAdrian Hunter 		return perf_evlist__enable_event_cpu(evlist, evsel, idx);
4011c65056cSAdrian Hunter 	else
4021c65056cSAdrian Hunter 		return perf_evlist__enable_event_thread(evlist, evsel, idx);
4031c65056cSAdrian Hunter }
4041c65056cSAdrian Hunter 
405ad6765ddSArnaldo Carvalho de Melo static int perf_evlist__grow_pollfd(struct perf_evlist *evlist, int hint)
406ad6765ddSArnaldo Carvalho de Melo {
407ad6765ddSArnaldo Carvalho de Melo 	int nr_fds_alloc = evlist->nr_fds_alloc + hint;
408ad6765ddSArnaldo Carvalho de Melo 	size_t size = sizeof(struct pollfd) * nr_fds_alloc;
409ad6765ddSArnaldo Carvalho de Melo 	struct pollfd *pollfd = realloc(evlist->pollfd, size);
410ad6765ddSArnaldo Carvalho de Melo 
411ad6765ddSArnaldo Carvalho de Melo 	if (pollfd == NULL)
412ad6765ddSArnaldo Carvalho de Melo 		return -ENOMEM;
413ad6765ddSArnaldo Carvalho de Melo 
414ad6765ddSArnaldo Carvalho de Melo 	evlist->nr_fds_alloc = nr_fds_alloc;
415ad6765ddSArnaldo Carvalho de Melo 	evlist->pollfd	     = pollfd;
416ad6765ddSArnaldo Carvalho de Melo 	return 0;
417ad6765ddSArnaldo Carvalho de Melo }
418ad6765ddSArnaldo Carvalho de Melo 
419ad6765ddSArnaldo Carvalho de Melo int perf_evlist__alloc_pollfd(struct perf_evlist *evlist)
4205c581041SArnaldo Carvalho de Melo {
421b3a319d5SNamhyung Kim 	int nr_cpus = cpu_map__nr(evlist->cpus);
422b3a319d5SNamhyung Kim 	int nr_threads = thread_map__nr(evlist->threads);
423bf8e8f4bSAdrian Hunter 	int nfds = 0;
424bf8e8f4bSAdrian Hunter 	struct perf_evsel *evsel;
425bf8e8f4bSAdrian Hunter 
426bf8e8f4bSAdrian Hunter 	list_for_each_entry(evsel, &evlist->entries, node) {
427bf8e8f4bSAdrian Hunter 		if (evsel->system_wide)
428bf8e8f4bSAdrian Hunter 			nfds += nr_cpus;
429bf8e8f4bSAdrian Hunter 		else
430bf8e8f4bSAdrian Hunter 			nfds += nr_cpus * nr_threads;
431bf8e8f4bSAdrian Hunter 	}
432bf8e8f4bSAdrian Hunter 
433ad6765ddSArnaldo Carvalho de Melo 	if (evlist->nr_fds_alloc - evlist->nr_fds < nfds &&
434ad6765ddSArnaldo Carvalho de Melo 	    perf_evlist__grow_pollfd(evlist, nfds) < 0)
435ad6765ddSArnaldo Carvalho de Melo 		return -ENOMEM;
436ad6765ddSArnaldo Carvalho de Melo 
437ad6765ddSArnaldo Carvalho de Melo 	return 0;
4385c581041SArnaldo Carvalho de Melo }
43970082dd9SArnaldo Carvalho de Melo 
440ad6765ddSArnaldo Carvalho de Melo int perf_evlist__add_pollfd(struct perf_evlist *evlist, int fd)
44170082dd9SArnaldo Carvalho de Melo {
442ad6765ddSArnaldo Carvalho de Melo 	/*
443ad6765ddSArnaldo Carvalho de Melo 	 * XXX: 64 is arbitrary, just not to call realloc at each fd.
444ad6765ddSArnaldo Carvalho de Melo 	 *	Find a better autogrowing heuristic
445ad6765ddSArnaldo Carvalho de Melo 	 */
446ad6765ddSArnaldo Carvalho de Melo 	if (evlist->nr_fds == evlist->nr_fds_alloc &&
447ad6765ddSArnaldo Carvalho de Melo 	    perf_evlist__grow_pollfd(evlist, 64) < 0)
448ad6765ddSArnaldo Carvalho de Melo 		return -ENOMEM;
449ad6765ddSArnaldo Carvalho de Melo 
45070082dd9SArnaldo Carvalho de Melo 	fcntl(fd, F_SETFL, O_NONBLOCK);
45170082dd9SArnaldo Carvalho de Melo 	evlist->pollfd[evlist->nr_fds].fd = fd;
4528179672cSArnaldo Carvalho de Melo 	evlist->pollfd[evlist->nr_fds].events = POLLIN | POLLERR | POLLHUP;
45370082dd9SArnaldo Carvalho de Melo 	evlist->nr_fds++;
454ad6765ddSArnaldo Carvalho de Melo 	return 0;
45570082dd9SArnaldo Carvalho de Melo }
45670db7533SArnaldo Carvalho de Melo 
4571ddec7f0SArnaldo Carvalho de Melo int perf_evlist__filter_pollfd(struct perf_evlist *evlist, short revents_and_mask)
4581ddec7f0SArnaldo Carvalho de Melo {
4591ddec7f0SArnaldo Carvalho de Melo 	int fd, nr_fds = 0;
4601ddec7f0SArnaldo Carvalho de Melo 
4611ddec7f0SArnaldo Carvalho de Melo 	if (evlist->nr_fds == 0)
4621ddec7f0SArnaldo Carvalho de Melo 		return 0;
4631ddec7f0SArnaldo Carvalho de Melo 
4641ddec7f0SArnaldo Carvalho de Melo 	for (fd = 0; fd < evlist->nr_fds; ++fd) {
4651ddec7f0SArnaldo Carvalho de Melo 		if (evlist->pollfd[fd].revents & revents_and_mask)
4661ddec7f0SArnaldo Carvalho de Melo 			continue;
4671ddec7f0SArnaldo Carvalho de Melo 
4681ddec7f0SArnaldo Carvalho de Melo 		if (fd != nr_fds)
4691ddec7f0SArnaldo Carvalho de Melo 			evlist->pollfd[nr_fds] = evlist->pollfd[fd];
4701ddec7f0SArnaldo Carvalho de Melo 
4711ddec7f0SArnaldo Carvalho de Melo 		++nr_fds;
4721ddec7f0SArnaldo Carvalho de Melo 	}
4731ddec7f0SArnaldo Carvalho de Melo 
4741ddec7f0SArnaldo Carvalho de Melo 	evlist->nr_fds = nr_fds;
4751ddec7f0SArnaldo Carvalho de Melo 	return nr_fds;
4761ddec7f0SArnaldo Carvalho de Melo }
4771ddec7f0SArnaldo Carvalho de Melo 
478a91e5431SArnaldo Carvalho de Melo static void perf_evlist__id_hash(struct perf_evlist *evlist,
479a91e5431SArnaldo Carvalho de Melo 				 struct perf_evsel *evsel,
4803d3b5e95SArnaldo Carvalho de Melo 				 int cpu, int thread, u64 id)
4813d3b5e95SArnaldo Carvalho de Melo {
4823d3b5e95SArnaldo Carvalho de Melo 	int hash;
4833d3b5e95SArnaldo Carvalho de Melo 	struct perf_sample_id *sid = SID(evsel, cpu, thread);
4843d3b5e95SArnaldo Carvalho de Melo 
4853d3b5e95SArnaldo Carvalho de Melo 	sid->id = id;
4863d3b5e95SArnaldo Carvalho de Melo 	sid->evsel = evsel;
4873d3b5e95SArnaldo Carvalho de Melo 	hash = hash_64(sid->id, PERF_EVLIST__HLIST_BITS);
4883d3b5e95SArnaldo Carvalho de Melo 	hlist_add_head(&sid->node, &evlist->heads[hash]);
4893d3b5e95SArnaldo Carvalho de Melo }
4903d3b5e95SArnaldo Carvalho de Melo 
491a91e5431SArnaldo Carvalho de Melo void perf_evlist__id_add(struct perf_evlist *evlist, struct perf_evsel *evsel,
492a91e5431SArnaldo Carvalho de Melo 			 int cpu, int thread, u64 id)
493a91e5431SArnaldo Carvalho de Melo {
494a91e5431SArnaldo Carvalho de Melo 	perf_evlist__id_hash(evlist, evsel, cpu, thread, id);
495a91e5431SArnaldo Carvalho de Melo 	evsel->id[evsel->ids++] = id;
496a91e5431SArnaldo Carvalho de Melo }
497a91e5431SArnaldo Carvalho de Melo 
498a91e5431SArnaldo Carvalho de Melo static int perf_evlist__id_add_fd(struct perf_evlist *evlist,
4993d3b5e95SArnaldo Carvalho de Melo 				  struct perf_evsel *evsel,
500f8a95309SArnaldo Carvalho de Melo 				  int cpu, int thread, int fd)
501f8a95309SArnaldo Carvalho de Melo {
502f8a95309SArnaldo Carvalho de Melo 	u64 read_data[4] = { 0, };
5033d3b5e95SArnaldo Carvalho de Melo 	int id_idx = 1; /* The first entry is the counter value */
504e2b5abe0SJiri Olsa 	u64 id;
505e2b5abe0SJiri Olsa 	int ret;
506e2b5abe0SJiri Olsa 
507e2b5abe0SJiri Olsa 	ret = ioctl(fd, PERF_EVENT_IOC_ID, &id);
508e2b5abe0SJiri Olsa 	if (!ret)
509e2b5abe0SJiri Olsa 		goto add;
510e2b5abe0SJiri Olsa 
511e2b5abe0SJiri Olsa 	if (errno != ENOTTY)
512e2b5abe0SJiri Olsa 		return -1;
513e2b5abe0SJiri Olsa 
514e2b5abe0SJiri Olsa 	/* Legacy way to get event id.. All hail to old kernels! */
515f8a95309SArnaldo Carvalho de Melo 
516c4861afeSJiri Olsa 	/*
517c4861afeSJiri Olsa 	 * This way does not work with group format read, so bail
518c4861afeSJiri Olsa 	 * out in that case.
519c4861afeSJiri Olsa 	 */
520c4861afeSJiri Olsa 	if (perf_evlist__read_format(evlist) & PERF_FORMAT_GROUP)
521c4861afeSJiri Olsa 		return -1;
522c4861afeSJiri Olsa 
523f8a95309SArnaldo Carvalho de Melo 	if (!(evsel->attr.read_format & PERF_FORMAT_ID) ||
524f8a95309SArnaldo Carvalho de Melo 	    read(fd, &read_data, sizeof(read_data)) == -1)
525f8a95309SArnaldo Carvalho de Melo 		return -1;
526f8a95309SArnaldo Carvalho de Melo 
527f8a95309SArnaldo Carvalho de Melo 	if (evsel->attr.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
528f8a95309SArnaldo Carvalho de Melo 		++id_idx;
529f8a95309SArnaldo Carvalho de Melo 	if (evsel->attr.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
530f8a95309SArnaldo Carvalho de Melo 		++id_idx;
531f8a95309SArnaldo Carvalho de Melo 
532e2b5abe0SJiri Olsa 	id = read_data[id_idx];
533e2b5abe0SJiri Olsa 
534e2b5abe0SJiri Olsa  add:
535e2b5abe0SJiri Olsa 	perf_evlist__id_add(evlist, evsel, cpu, thread, id);
536f8a95309SArnaldo Carvalho de Melo 	return 0;
537f8a95309SArnaldo Carvalho de Melo }
538f8a95309SArnaldo Carvalho de Melo 
539932a3594SJiri Olsa struct perf_sample_id *perf_evlist__id2sid(struct perf_evlist *evlist, u64 id)
54070db7533SArnaldo Carvalho de Melo {
54170db7533SArnaldo Carvalho de Melo 	struct hlist_head *head;
54270db7533SArnaldo Carvalho de Melo 	struct perf_sample_id *sid;
54370db7533SArnaldo Carvalho de Melo 	int hash;
54470db7533SArnaldo Carvalho de Melo 
54570db7533SArnaldo Carvalho de Melo 	hash = hash_64(id, PERF_EVLIST__HLIST_BITS);
54670db7533SArnaldo Carvalho de Melo 	head = &evlist->heads[hash];
54770db7533SArnaldo Carvalho de Melo 
548b67bfe0dSSasha Levin 	hlist_for_each_entry(sid, head, node)
54970db7533SArnaldo Carvalho de Melo 		if (sid->id == id)
550932a3594SJiri Olsa 			return sid;
551932a3594SJiri Olsa 
552932a3594SJiri Olsa 	return NULL;
553932a3594SJiri Olsa }
554932a3594SJiri Olsa 
555932a3594SJiri Olsa struct perf_evsel *perf_evlist__id2evsel(struct perf_evlist *evlist, u64 id)
556932a3594SJiri Olsa {
557932a3594SJiri Olsa 	struct perf_sample_id *sid;
558932a3594SJiri Olsa 
559932a3594SJiri Olsa 	if (evlist->nr_entries == 1)
560932a3594SJiri Olsa 		return perf_evlist__first(evlist);
561932a3594SJiri Olsa 
562932a3594SJiri Olsa 	sid = perf_evlist__id2sid(evlist, id);
563932a3594SJiri Olsa 	if (sid)
56470db7533SArnaldo Carvalho de Melo 		return sid->evsel;
56530e68bccSNamhyung Kim 
56630e68bccSNamhyung Kim 	if (!perf_evlist__sample_id_all(evlist))
5670c21f736SArnaldo Carvalho de Melo 		return perf_evlist__first(evlist);
56830e68bccSNamhyung Kim 
56970db7533SArnaldo Carvalho de Melo 	return NULL;
57070db7533SArnaldo Carvalho de Melo }
57104391debSArnaldo Carvalho de Melo 
57275562573SAdrian Hunter static int perf_evlist__event2id(struct perf_evlist *evlist,
57375562573SAdrian Hunter 				 union perf_event *event, u64 *id)
57475562573SAdrian Hunter {
57575562573SAdrian Hunter 	const u64 *array = event->sample.array;
57675562573SAdrian Hunter 	ssize_t n;
57775562573SAdrian Hunter 
57875562573SAdrian Hunter 	n = (event->header.size - sizeof(event->header)) >> 3;
57975562573SAdrian Hunter 
58075562573SAdrian Hunter 	if (event->header.type == PERF_RECORD_SAMPLE) {
58175562573SAdrian Hunter 		if (evlist->id_pos >= n)
58275562573SAdrian Hunter 			return -1;
58375562573SAdrian Hunter 		*id = array[evlist->id_pos];
58475562573SAdrian Hunter 	} else {
58575562573SAdrian Hunter 		if (evlist->is_pos > n)
58675562573SAdrian Hunter 			return -1;
58775562573SAdrian Hunter 		n -= evlist->is_pos;
58875562573SAdrian Hunter 		*id = array[n];
58975562573SAdrian Hunter 	}
59075562573SAdrian Hunter 	return 0;
59175562573SAdrian Hunter }
59275562573SAdrian Hunter 
59375562573SAdrian Hunter static struct perf_evsel *perf_evlist__event2evsel(struct perf_evlist *evlist,
59475562573SAdrian Hunter 						   union perf_event *event)
59575562573SAdrian Hunter {
59698be6966SAdrian Hunter 	struct perf_evsel *first = perf_evlist__first(evlist);
59775562573SAdrian Hunter 	struct hlist_head *head;
59875562573SAdrian Hunter 	struct perf_sample_id *sid;
59975562573SAdrian Hunter 	int hash;
60075562573SAdrian Hunter 	u64 id;
60175562573SAdrian Hunter 
60275562573SAdrian Hunter 	if (evlist->nr_entries == 1)
60398be6966SAdrian Hunter 		return first;
60498be6966SAdrian Hunter 
60598be6966SAdrian Hunter 	if (!first->attr.sample_id_all &&
60698be6966SAdrian Hunter 	    event->header.type != PERF_RECORD_SAMPLE)
60798be6966SAdrian Hunter 		return first;
60875562573SAdrian Hunter 
60975562573SAdrian Hunter 	if (perf_evlist__event2id(evlist, event, &id))
61075562573SAdrian Hunter 		return NULL;
61175562573SAdrian Hunter 
61275562573SAdrian Hunter 	/* Synthesized events have an id of zero */
61375562573SAdrian Hunter 	if (!id)
61498be6966SAdrian Hunter 		return first;
61575562573SAdrian Hunter 
61675562573SAdrian Hunter 	hash = hash_64(id, PERF_EVLIST__HLIST_BITS);
61775562573SAdrian Hunter 	head = &evlist->heads[hash];
61875562573SAdrian Hunter 
61975562573SAdrian Hunter 	hlist_for_each_entry(sid, head, node) {
62075562573SAdrian Hunter 		if (sid->id == id)
62175562573SAdrian Hunter 			return sid->evsel;
62275562573SAdrian Hunter 	}
62375562573SAdrian Hunter 	return NULL;
62475562573SAdrian Hunter }
62575562573SAdrian Hunter 
626aece948fSArnaldo Carvalho de Melo union perf_event *perf_evlist__mmap_read(struct perf_evlist *evlist, int idx)
62704391debSArnaldo Carvalho de Melo {
628aece948fSArnaldo Carvalho de Melo 	struct perf_mmap *md = &evlist->mmap[idx];
62904391debSArnaldo Carvalho de Melo 	unsigned int head = perf_mmap__read_head(md);
63004391debSArnaldo Carvalho de Melo 	unsigned int old = md->prev;
63104391debSArnaldo Carvalho de Melo 	unsigned char *data = md->base + page_size;
6328115d60cSArnaldo Carvalho de Melo 	union perf_event *event = NULL;
63304391debSArnaldo Carvalho de Melo 
6347bb41152SArnaldo Carvalho de Melo 	if (evlist->overwrite) {
63504391debSArnaldo Carvalho de Melo 		/*
63604391debSArnaldo Carvalho de Melo 		 * If we're further behind than half the buffer, there's a chance
63704391debSArnaldo Carvalho de Melo 		 * the writer will bite our tail and mess up the samples under us.
63804391debSArnaldo Carvalho de Melo 		 *
63904391debSArnaldo Carvalho de Melo 		 * If we somehow ended up ahead of the head, we got messed up.
64004391debSArnaldo Carvalho de Melo 		 *
64104391debSArnaldo Carvalho de Melo 		 * In either case, truncate and restart at head.
64204391debSArnaldo Carvalho de Melo 		 */
6437bb41152SArnaldo Carvalho de Melo 		int diff = head - old;
64404391debSArnaldo Carvalho de Melo 		if (diff > md->mask / 2 || diff < 0) {
64504391debSArnaldo Carvalho de Melo 			fprintf(stderr, "WARNING: failed to keep up with mmap data.\n");
64604391debSArnaldo Carvalho de Melo 
64704391debSArnaldo Carvalho de Melo 			/*
64804391debSArnaldo Carvalho de Melo 			 * head points to a known good entry, start there.
64904391debSArnaldo Carvalho de Melo 			 */
65004391debSArnaldo Carvalho de Melo 			old = head;
65104391debSArnaldo Carvalho de Melo 		}
6527bb41152SArnaldo Carvalho de Melo 	}
65304391debSArnaldo Carvalho de Melo 
65404391debSArnaldo Carvalho de Melo 	if (old != head) {
65504391debSArnaldo Carvalho de Melo 		size_t size;
65604391debSArnaldo Carvalho de Melo 
6578115d60cSArnaldo Carvalho de Melo 		event = (union perf_event *)&data[old & md->mask];
65804391debSArnaldo Carvalho de Melo 		size = event->header.size;
65904391debSArnaldo Carvalho de Melo 
66004391debSArnaldo Carvalho de Melo 		/*
66104391debSArnaldo Carvalho de Melo 		 * Event straddles the mmap boundary -- header should always
66204391debSArnaldo Carvalho de Melo 		 * be inside due to u64 alignment of output.
66304391debSArnaldo Carvalho de Melo 		 */
66404391debSArnaldo Carvalho de Melo 		if ((old & md->mask) + size != ((old + size) & md->mask)) {
66504391debSArnaldo Carvalho de Melo 			unsigned int offset = old;
66604391debSArnaldo Carvalho de Melo 			unsigned int len = min(sizeof(*event), size), cpy;
667a65cb4b9SJiri Olsa 			void *dst = md->event_copy;
66804391debSArnaldo Carvalho de Melo 
66904391debSArnaldo Carvalho de Melo 			do {
67004391debSArnaldo Carvalho de Melo 				cpy = min(md->mask + 1 - (offset & md->mask), len);
67104391debSArnaldo Carvalho de Melo 				memcpy(dst, &data[offset & md->mask], cpy);
67204391debSArnaldo Carvalho de Melo 				offset += cpy;
67304391debSArnaldo Carvalho de Melo 				dst += cpy;
67404391debSArnaldo Carvalho de Melo 				len -= cpy;
67504391debSArnaldo Carvalho de Melo 			} while (len);
67604391debSArnaldo Carvalho de Melo 
677a65cb4b9SJiri Olsa 			event = (union perf_event *) md->event_copy;
67804391debSArnaldo Carvalho de Melo 		}
67904391debSArnaldo Carvalho de Melo 
68004391debSArnaldo Carvalho de Melo 		old += size;
68104391debSArnaldo Carvalho de Melo 	}
68204391debSArnaldo Carvalho de Melo 
68304391debSArnaldo Carvalho de Melo 	md->prev = old;
6847bb41152SArnaldo Carvalho de Melo 
68504391debSArnaldo Carvalho de Melo 	return event;
68604391debSArnaldo Carvalho de Melo }
687f8a95309SArnaldo Carvalho de Melo 
6888e50d384SZhouyi Zhou void perf_evlist__mmap_consume(struct perf_evlist *evlist, int idx)
6898e50d384SZhouyi Zhou {
6908e50d384SZhouyi Zhou 	if (!evlist->overwrite) {
6918e50d384SZhouyi Zhou 		struct perf_mmap *md = &evlist->mmap[idx];
6928e50d384SZhouyi Zhou 		unsigned int old = md->prev;
6938e50d384SZhouyi Zhou 
6948e50d384SZhouyi Zhou 		perf_mmap__write_tail(md, old);
6958e50d384SZhouyi Zhou 	}
6968e50d384SZhouyi Zhou }
6978e50d384SZhouyi Zhou 
69893edcbd9SAdrian Hunter static void __perf_evlist__munmap(struct perf_evlist *evlist, int idx)
69993edcbd9SAdrian Hunter {
70093edcbd9SAdrian Hunter 	if (evlist->mmap[idx].base != NULL) {
70193edcbd9SAdrian Hunter 		munmap(evlist->mmap[idx].base, evlist->mmap_len);
70293edcbd9SAdrian Hunter 		evlist->mmap[idx].base = NULL;
70393edcbd9SAdrian Hunter 	}
70493edcbd9SAdrian Hunter }
70593edcbd9SAdrian Hunter 
7067e2ed097SArnaldo Carvalho de Melo void perf_evlist__munmap(struct perf_evlist *evlist)
707f8a95309SArnaldo Carvalho de Melo {
708aece948fSArnaldo Carvalho de Melo 	int i;
709f8a95309SArnaldo Carvalho de Melo 
710983874d1SArnaldo Carvalho de Melo 	if (evlist->mmap == NULL)
711983874d1SArnaldo Carvalho de Melo 		return;
712983874d1SArnaldo Carvalho de Melo 
71393edcbd9SAdrian Hunter 	for (i = 0; i < evlist->nr_mmaps; i++)
71493edcbd9SAdrian Hunter 		__perf_evlist__munmap(evlist, i);
715aece948fSArnaldo Carvalho de Melo 
71604662523SArnaldo Carvalho de Melo 	zfree(&evlist->mmap);
717f8a95309SArnaldo Carvalho de Melo }
718f8a95309SArnaldo Carvalho de Melo 
719806fb630SArnaldo Carvalho de Melo static int perf_evlist__alloc_mmap(struct perf_evlist *evlist)
720f8a95309SArnaldo Carvalho de Melo {
721a14bb7a6SArnaldo Carvalho de Melo 	evlist->nr_mmaps = cpu_map__nr(evlist->cpus);
722ec1e7e43SSukadev Bhattiprolu 	if (cpu_map__empty(evlist->cpus))
723b3a319d5SNamhyung Kim 		evlist->nr_mmaps = thread_map__nr(evlist->threads);
724aece948fSArnaldo Carvalho de Melo 	evlist->mmap = zalloc(evlist->nr_mmaps * sizeof(struct perf_mmap));
725f8a95309SArnaldo Carvalho de Melo 	return evlist->mmap != NULL ? 0 : -ENOMEM;
726f8a95309SArnaldo Carvalho de Melo }
727f8a95309SArnaldo Carvalho de Melo 
728a8a8f3ebSAdrian Hunter struct mmap_params {
729a8a8f3ebSAdrian Hunter 	int prot;
730a8a8f3ebSAdrian Hunter 	int mask;
731a8a8f3ebSAdrian Hunter };
732a8a8f3ebSAdrian Hunter 
733a8a8f3ebSAdrian Hunter static int __perf_evlist__mmap(struct perf_evlist *evlist, int idx,
734a8a8f3ebSAdrian Hunter 			       struct mmap_params *mp, int fd)
735f8a95309SArnaldo Carvalho de Melo {
736aece948fSArnaldo Carvalho de Melo 	evlist->mmap[idx].prev = 0;
737a8a8f3ebSAdrian Hunter 	evlist->mmap[idx].mask = mp->mask;
738a8a8f3ebSAdrian Hunter 	evlist->mmap[idx].base = mmap(NULL, evlist->mmap_len, mp->prot,
739f8a95309SArnaldo Carvalho de Melo 				      MAP_SHARED, fd, 0);
740301b195dSNelson Elhage 	if (evlist->mmap[idx].base == MAP_FAILED) {
74102635965SAdrian Hunter 		pr_debug2("failed to mmap perf event ring buffer, error %d\n",
74202635965SAdrian Hunter 			  errno);
743301b195dSNelson Elhage 		evlist->mmap[idx].base = NULL;
744f8a95309SArnaldo Carvalho de Melo 		return -1;
745301b195dSNelson Elhage 	}
746ad6765ddSArnaldo Carvalho de Melo 
747f8a95309SArnaldo Carvalho de Melo 	return 0;
748f8a95309SArnaldo Carvalho de Melo }
749f8a95309SArnaldo Carvalho de Melo 
75004e21314SAdrian Hunter static int perf_evlist__mmap_per_evsel(struct perf_evlist *evlist, int idx,
751a8a8f3ebSAdrian Hunter 				       struct mmap_params *mp, int cpu,
752a8a8f3ebSAdrian Hunter 				       int thread, int *output)
753aece948fSArnaldo Carvalho de Melo {
754aece948fSArnaldo Carvalho de Melo 	struct perf_evsel *evsel;
75504e21314SAdrian Hunter 
7560050f7aaSArnaldo Carvalho de Melo 	evlist__for_each(evlist, evsel) {
757bf8e8f4bSAdrian Hunter 		int fd;
758bf8e8f4bSAdrian Hunter 
759bf8e8f4bSAdrian Hunter 		if (evsel->system_wide && thread)
760bf8e8f4bSAdrian Hunter 			continue;
761bf8e8f4bSAdrian Hunter 
762bf8e8f4bSAdrian Hunter 		fd = FD(evsel, cpu, thread);
76304e21314SAdrian Hunter 
76404e21314SAdrian Hunter 		if (*output == -1) {
76504e21314SAdrian Hunter 			*output = fd;
766a8a8f3ebSAdrian Hunter 			if (__perf_evlist__mmap(evlist, idx, mp, *output) < 0)
76704e21314SAdrian Hunter 				return -1;
76804e21314SAdrian Hunter 		} else {
76904e21314SAdrian Hunter 			if (ioctl(fd, PERF_EVENT_IOC_SET_OUTPUT, *output) != 0)
77004e21314SAdrian Hunter 				return -1;
77104e21314SAdrian Hunter 		}
77204e21314SAdrian Hunter 
773ad6765ddSArnaldo Carvalho de Melo 		if (perf_evlist__add_pollfd(evlist, fd) < 0)
774ad6765ddSArnaldo Carvalho de Melo 			return -1;
775033fa713SArnaldo Carvalho de Melo 
77604e21314SAdrian Hunter 		if ((evsel->attr.read_format & PERF_FORMAT_ID) &&
77704e21314SAdrian Hunter 		    perf_evlist__id_add_fd(evlist, evsel, cpu, thread, fd) < 0)
77804e21314SAdrian Hunter 			return -1;
77904e21314SAdrian Hunter 	}
78004e21314SAdrian Hunter 
78104e21314SAdrian Hunter 	return 0;
78204e21314SAdrian Hunter }
78304e21314SAdrian Hunter 
784a8a8f3ebSAdrian Hunter static int perf_evlist__mmap_per_cpu(struct perf_evlist *evlist,
785a8a8f3ebSAdrian Hunter 				     struct mmap_params *mp)
78604e21314SAdrian Hunter {
787aece948fSArnaldo Carvalho de Melo 	int cpu, thread;
788b3a319d5SNamhyung Kim 	int nr_cpus = cpu_map__nr(evlist->cpus);
789b3a319d5SNamhyung Kim 	int nr_threads = thread_map__nr(evlist->threads);
790aece948fSArnaldo Carvalho de Melo 
791e3e1a54fSAdrian Hunter 	pr_debug2("perf event ring buffer mmapped per cpu\n");
792b3a319d5SNamhyung Kim 	for (cpu = 0; cpu < nr_cpus; cpu++) {
793aece948fSArnaldo Carvalho de Melo 		int output = -1;
794aece948fSArnaldo Carvalho de Melo 
795b3a319d5SNamhyung Kim 		for (thread = 0; thread < nr_threads; thread++) {
796a8a8f3ebSAdrian Hunter 			if (perf_evlist__mmap_per_evsel(evlist, cpu, mp, cpu,
797a8a8f3ebSAdrian Hunter 							thread, &output))
798aece948fSArnaldo Carvalho de Melo 				goto out_unmap;
799aece948fSArnaldo Carvalho de Melo 		}
800aece948fSArnaldo Carvalho de Melo 	}
801aece948fSArnaldo Carvalho de Melo 
802aece948fSArnaldo Carvalho de Melo 	return 0;
803aece948fSArnaldo Carvalho de Melo 
804aece948fSArnaldo Carvalho de Melo out_unmap:
80593edcbd9SAdrian Hunter 	for (cpu = 0; cpu < nr_cpus; cpu++)
80693edcbd9SAdrian Hunter 		__perf_evlist__munmap(evlist, cpu);
807aece948fSArnaldo Carvalho de Melo 	return -1;
808aece948fSArnaldo Carvalho de Melo }
809aece948fSArnaldo Carvalho de Melo 
810a8a8f3ebSAdrian Hunter static int perf_evlist__mmap_per_thread(struct perf_evlist *evlist,
811a8a8f3ebSAdrian Hunter 					struct mmap_params *mp)
812aece948fSArnaldo Carvalho de Melo {
813aece948fSArnaldo Carvalho de Melo 	int thread;
814b3a319d5SNamhyung Kim 	int nr_threads = thread_map__nr(evlist->threads);
815aece948fSArnaldo Carvalho de Melo 
816e3e1a54fSAdrian Hunter 	pr_debug2("perf event ring buffer mmapped per thread\n");
817b3a319d5SNamhyung Kim 	for (thread = 0; thread < nr_threads; thread++) {
818aece948fSArnaldo Carvalho de Melo 		int output = -1;
819aece948fSArnaldo Carvalho de Melo 
820a8a8f3ebSAdrian Hunter 		if (perf_evlist__mmap_per_evsel(evlist, thread, mp, 0, thread,
821a8a8f3ebSAdrian Hunter 						&output))
822aece948fSArnaldo Carvalho de Melo 			goto out_unmap;
823aece948fSArnaldo Carvalho de Melo 	}
824aece948fSArnaldo Carvalho de Melo 
825aece948fSArnaldo Carvalho de Melo 	return 0;
826aece948fSArnaldo Carvalho de Melo 
827aece948fSArnaldo Carvalho de Melo out_unmap:
82893edcbd9SAdrian Hunter 	for (thread = 0; thread < nr_threads; thread++)
82993edcbd9SAdrian Hunter 		__perf_evlist__munmap(evlist, thread);
830aece948fSArnaldo Carvalho de Melo 	return -1;
831aece948fSArnaldo Carvalho de Melo }
832aece948fSArnaldo Carvalho de Melo 
833994a1f78SJiri Olsa static size_t perf_evlist__mmap_size(unsigned long pages)
834994a1f78SJiri Olsa {
835994a1f78SJiri Olsa 	/* 512 kiB: default amount of unprivileged mlocked memory */
836994a1f78SJiri Olsa 	if (pages == UINT_MAX)
837994a1f78SJiri Olsa 		pages = (512 * 1024) / page_size;
838994a1f78SJiri Olsa 	else if (!is_power_of_2(pages))
839994a1f78SJiri Olsa 		return 0;
840994a1f78SJiri Olsa 
841994a1f78SJiri Olsa 	return (pages + 1) * page_size;
842994a1f78SJiri Olsa }
843994a1f78SJiri Olsa 
84433c2dcfdSDavid Ahern static long parse_pages_arg(const char *str, unsigned long min,
84533c2dcfdSDavid Ahern 			    unsigned long max)
846994a1f78SJiri Olsa {
8472fbe4abeSAdrian Hunter 	unsigned long pages, val;
84827050f53SJiri Olsa 	static struct parse_tag tags[] = {
84927050f53SJiri Olsa 		{ .tag  = 'B', .mult = 1       },
85027050f53SJiri Olsa 		{ .tag  = 'K', .mult = 1 << 10 },
85127050f53SJiri Olsa 		{ .tag  = 'M', .mult = 1 << 20 },
85227050f53SJiri Olsa 		{ .tag  = 'G', .mult = 1 << 30 },
85327050f53SJiri Olsa 		{ .tag  = 0 },
85427050f53SJiri Olsa 	};
855994a1f78SJiri Olsa 
8568973504bSDavid Ahern 	if (str == NULL)
85733c2dcfdSDavid Ahern 		return -EINVAL;
8588973504bSDavid Ahern 
85927050f53SJiri Olsa 	val = parse_tag_value(str, tags);
8602fbe4abeSAdrian Hunter 	if (val != (unsigned long) -1) {
86127050f53SJiri Olsa 		/* we got file size value */
86227050f53SJiri Olsa 		pages = PERF_ALIGN(val, page_size) / page_size;
86327050f53SJiri Olsa 	} else {
86427050f53SJiri Olsa 		/* we got pages count value */
86527050f53SJiri Olsa 		char *eptr;
866994a1f78SJiri Olsa 		pages = strtoul(str, &eptr, 10);
86733c2dcfdSDavid Ahern 		if (*eptr != '\0')
86833c2dcfdSDavid Ahern 			return -EINVAL;
86927050f53SJiri Olsa 	}
870994a1f78SJiri Olsa 
8712bcab6c1SAdrian Hunter 	if (pages == 0 && min == 0) {
87233c2dcfdSDavid Ahern 		/* leave number of pages at 0 */
8731dbfa938SAdrian Hunter 	} else if (!is_power_of_2(pages)) {
87433c2dcfdSDavid Ahern 		/* round pages up to next power of 2 */
8751dbfa938SAdrian Hunter 		pages = next_pow2_l(pages);
8761dbfa938SAdrian Hunter 		if (!pages)
8771dbfa938SAdrian Hunter 			return -EINVAL;
8789639837eSDavid Ahern 		pr_info("rounding mmap pages size to %lu bytes (%lu pages)\n",
8799639837eSDavid Ahern 			pages * page_size, pages);
8802fbe4abeSAdrian Hunter 	}
8812fbe4abeSAdrian Hunter 
88233c2dcfdSDavid Ahern 	if (pages > max)
88333c2dcfdSDavid Ahern 		return -EINVAL;
88433c2dcfdSDavid Ahern 
88533c2dcfdSDavid Ahern 	return pages;
88633c2dcfdSDavid Ahern }
88733c2dcfdSDavid Ahern 
88833c2dcfdSDavid Ahern int perf_evlist__parse_mmap_pages(const struct option *opt, const char *str,
88933c2dcfdSDavid Ahern 				  int unset __maybe_unused)
89033c2dcfdSDavid Ahern {
89133c2dcfdSDavid Ahern 	unsigned int *mmap_pages = opt->value;
89233c2dcfdSDavid Ahern 	unsigned long max = UINT_MAX;
89333c2dcfdSDavid Ahern 	long pages;
89433c2dcfdSDavid Ahern 
895f5ae9c42SAdrian Hunter 	if (max > SIZE_MAX / page_size)
89633c2dcfdSDavid Ahern 		max = SIZE_MAX / page_size;
89733c2dcfdSDavid Ahern 
89833c2dcfdSDavid Ahern 	pages = parse_pages_arg(str, 1, max);
89933c2dcfdSDavid Ahern 	if (pages < 0) {
90033c2dcfdSDavid Ahern 		pr_err("Invalid argument for --mmap_pages/-m\n");
901994a1f78SJiri Olsa 		return -1;
902994a1f78SJiri Olsa 	}
903994a1f78SJiri Olsa 
904994a1f78SJiri Olsa 	*mmap_pages = pages;
905994a1f78SJiri Olsa 	return 0;
906994a1f78SJiri Olsa }
907994a1f78SJiri Olsa 
908c83fa7f2SAdrian Hunter /**
909c83fa7f2SAdrian Hunter  * perf_evlist__mmap - Create mmaps to receive events.
910c83fa7f2SAdrian Hunter  * @evlist: list of events
911c83fa7f2SAdrian Hunter  * @pages: map length in pages
912c83fa7f2SAdrian Hunter  * @overwrite: overwrite older events?
913f8a95309SArnaldo Carvalho de Melo  *
914c83fa7f2SAdrian Hunter  * If @overwrite is %false the user needs to signal event consumption using
915c83fa7f2SAdrian Hunter  * perf_mmap__write_tail().  Using perf_evlist__mmap_read() does this
916c83fa7f2SAdrian Hunter  * automatically.
917f8a95309SArnaldo Carvalho de Melo  *
918c83fa7f2SAdrian Hunter  * Return: %0 on success, negative error code otherwise.
919f8a95309SArnaldo Carvalho de Melo  */
92050a682ceSArnaldo Carvalho de Melo int perf_evlist__mmap(struct perf_evlist *evlist, unsigned int pages,
92150a682ceSArnaldo Carvalho de Melo 		      bool overwrite)
922f8a95309SArnaldo Carvalho de Melo {
923aece948fSArnaldo Carvalho de Melo 	struct perf_evsel *evsel;
9247e2ed097SArnaldo Carvalho de Melo 	const struct cpu_map *cpus = evlist->cpus;
9257e2ed097SArnaldo Carvalho de Melo 	const struct thread_map *threads = evlist->threads;
926a8a8f3ebSAdrian Hunter 	struct mmap_params mp = {
927a8a8f3ebSAdrian Hunter 		.prot = PROT_READ | (overwrite ? 0 : PROT_WRITE),
928a8a8f3ebSAdrian Hunter 	};
92950a682ceSArnaldo Carvalho de Melo 
9307e2ed097SArnaldo Carvalho de Melo 	if (evlist->mmap == NULL && perf_evlist__alloc_mmap(evlist) < 0)
931f8a95309SArnaldo Carvalho de Melo 		return -ENOMEM;
932f8a95309SArnaldo Carvalho de Melo 
9337e2ed097SArnaldo Carvalho de Melo 	if (evlist->pollfd == NULL && perf_evlist__alloc_pollfd(evlist) < 0)
934f8a95309SArnaldo Carvalho de Melo 		return -ENOMEM;
935f8a95309SArnaldo Carvalho de Melo 
936f8a95309SArnaldo Carvalho de Melo 	evlist->overwrite = overwrite;
937994a1f78SJiri Olsa 	evlist->mmap_len = perf_evlist__mmap_size(pages);
9382af68ef5SAdrian Hunter 	pr_debug("mmap size %zuB\n", evlist->mmap_len);
939a8a8f3ebSAdrian Hunter 	mp.mask = evlist->mmap_len - page_size - 1;
940f8a95309SArnaldo Carvalho de Melo 
9410050f7aaSArnaldo Carvalho de Melo 	evlist__for_each(evlist, evsel) {
942f8a95309SArnaldo Carvalho de Melo 		if ((evsel->attr.read_format & PERF_FORMAT_ID) &&
943a91e5431SArnaldo Carvalho de Melo 		    evsel->sample_id == NULL &&
944a14bb7a6SArnaldo Carvalho de Melo 		    perf_evsel__alloc_id(evsel, cpu_map__nr(cpus), threads->nr) < 0)
945f8a95309SArnaldo Carvalho de Melo 			return -ENOMEM;
946f8a95309SArnaldo Carvalho de Melo 	}
947f8a95309SArnaldo Carvalho de Melo 
948ec1e7e43SSukadev Bhattiprolu 	if (cpu_map__empty(cpus))
949a8a8f3ebSAdrian Hunter 		return perf_evlist__mmap_per_thread(evlist, &mp);
950f8a95309SArnaldo Carvalho de Melo 
951a8a8f3ebSAdrian Hunter 	return perf_evlist__mmap_per_cpu(evlist, &mp);
952f8a95309SArnaldo Carvalho de Melo }
9537e2ed097SArnaldo Carvalho de Melo 
954602ad878SArnaldo Carvalho de Melo int perf_evlist__create_maps(struct perf_evlist *evlist, struct target *target)
9557e2ed097SArnaldo Carvalho de Melo {
956b809ac10SNamhyung Kim 	evlist->threads = thread_map__new_str(target->pid, target->tid,
957b809ac10SNamhyung Kim 					      target->uid);
9587e2ed097SArnaldo Carvalho de Melo 
9597e2ed097SArnaldo Carvalho de Melo 	if (evlist->threads == NULL)
9607e2ed097SArnaldo Carvalho de Melo 		return -1;
9617e2ed097SArnaldo Carvalho de Melo 
9629c105fbcSDongsheng Yang 	if (target__uses_dummy_map(target))
963d1cb9fceSNamhyung Kim 		evlist->cpus = cpu_map__dummy_new();
964879d77d0SNamhyung Kim 	else
965879d77d0SNamhyung Kim 		evlist->cpus = cpu_map__new(target->cpu_list);
9667e2ed097SArnaldo Carvalho de Melo 
9677e2ed097SArnaldo Carvalho de Melo 	if (evlist->cpus == NULL)
9687e2ed097SArnaldo Carvalho de Melo 		goto out_delete_threads;
9697e2ed097SArnaldo Carvalho de Melo 
9707e2ed097SArnaldo Carvalho de Melo 	return 0;
9717e2ed097SArnaldo Carvalho de Melo 
9727e2ed097SArnaldo Carvalho de Melo out_delete_threads:
9737e2ed097SArnaldo Carvalho de Melo 	thread_map__delete(evlist->threads);
9747e2ed097SArnaldo Carvalho de Melo 	return -1;
9757e2ed097SArnaldo Carvalho de Melo }
9767e2ed097SArnaldo Carvalho de Melo 
9771491a632SArnaldo Carvalho de Melo int perf_evlist__apply_filters(struct perf_evlist *evlist)
9780a102479SFrederic Weisbecker {
9790a102479SFrederic Weisbecker 	struct perf_evsel *evsel;
980745cefc5SArnaldo Carvalho de Melo 	int err = 0;
981745cefc5SArnaldo Carvalho de Melo 	const int ncpus = cpu_map__nr(evlist->cpus),
982b3a319d5SNamhyung Kim 		  nthreads = thread_map__nr(evlist->threads);
9830a102479SFrederic Weisbecker 
9840050f7aaSArnaldo Carvalho de Melo 	evlist__for_each(evlist, evsel) {
985745cefc5SArnaldo Carvalho de Melo 		if (evsel->filter == NULL)
9860a102479SFrederic Weisbecker 			continue;
987745cefc5SArnaldo Carvalho de Melo 
988745cefc5SArnaldo Carvalho de Melo 		err = perf_evsel__set_filter(evsel, ncpus, nthreads, evsel->filter);
9890a102479SFrederic Weisbecker 		if (err)
990745cefc5SArnaldo Carvalho de Melo 			break;
9910a102479SFrederic Weisbecker 	}
9920a102479SFrederic Weisbecker 
993745cefc5SArnaldo Carvalho de Melo 	return err;
994745cefc5SArnaldo Carvalho de Melo }
995745cefc5SArnaldo Carvalho de Melo 
996745cefc5SArnaldo Carvalho de Melo int perf_evlist__set_filter(struct perf_evlist *evlist, const char *filter)
997745cefc5SArnaldo Carvalho de Melo {
998745cefc5SArnaldo Carvalho de Melo 	struct perf_evsel *evsel;
999745cefc5SArnaldo Carvalho de Melo 	int err = 0;
1000745cefc5SArnaldo Carvalho de Melo 	const int ncpus = cpu_map__nr(evlist->cpus),
1001b3a319d5SNamhyung Kim 		  nthreads = thread_map__nr(evlist->threads);
1002745cefc5SArnaldo Carvalho de Melo 
10030050f7aaSArnaldo Carvalho de Melo 	evlist__for_each(evlist, evsel) {
1004745cefc5SArnaldo Carvalho de Melo 		err = perf_evsel__set_filter(evsel, ncpus, nthreads, filter);
1005745cefc5SArnaldo Carvalho de Melo 		if (err)
1006745cefc5SArnaldo Carvalho de Melo 			break;
1007745cefc5SArnaldo Carvalho de Melo 	}
1008745cefc5SArnaldo Carvalho de Melo 
1009745cefc5SArnaldo Carvalho de Melo 	return err;
10100a102479SFrederic Weisbecker }
101174429964SFrederic Weisbecker 
10120c21f736SArnaldo Carvalho de Melo bool perf_evlist__valid_sample_type(struct perf_evlist *evlist)
101374429964SFrederic Weisbecker {
101475562573SAdrian Hunter 	struct perf_evsel *pos;
1015c2a70653SArnaldo Carvalho de Melo 
101675562573SAdrian Hunter 	if (evlist->nr_entries == 1)
101775562573SAdrian Hunter 		return true;
101875562573SAdrian Hunter 
101975562573SAdrian Hunter 	if (evlist->id_pos < 0 || evlist->is_pos < 0)
102075562573SAdrian Hunter 		return false;
102175562573SAdrian Hunter 
10220050f7aaSArnaldo Carvalho de Melo 	evlist__for_each(evlist, pos) {
102375562573SAdrian Hunter 		if (pos->id_pos != evlist->id_pos ||
102475562573SAdrian Hunter 		    pos->is_pos != evlist->is_pos)
1025c2a70653SArnaldo Carvalho de Melo 			return false;
102674429964SFrederic Weisbecker 	}
102774429964SFrederic Weisbecker 
1028c2a70653SArnaldo Carvalho de Melo 	return true;
1029c2a70653SArnaldo Carvalho de Melo }
1030c2a70653SArnaldo Carvalho de Melo 
103175562573SAdrian Hunter u64 __perf_evlist__combined_sample_type(struct perf_evlist *evlist)
1032c2a70653SArnaldo Carvalho de Melo {
103375562573SAdrian Hunter 	struct perf_evsel *evsel;
103475562573SAdrian Hunter 
103575562573SAdrian Hunter 	if (evlist->combined_sample_type)
103675562573SAdrian Hunter 		return evlist->combined_sample_type;
103775562573SAdrian Hunter 
10380050f7aaSArnaldo Carvalho de Melo 	evlist__for_each(evlist, evsel)
103975562573SAdrian Hunter 		evlist->combined_sample_type |= evsel->attr.sample_type;
104075562573SAdrian Hunter 
104175562573SAdrian Hunter 	return evlist->combined_sample_type;
104275562573SAdrian Hunter }
104375562573SAdrian Hunter 
104475562573SAdrian Hunter u64 perf_evlist__combined_sample_type(struct perf_evlist *evlist)
104575562573SAdrian Hunter {
104675562573SAdrian Hunter 	evlist->combined_sample_type = 0;
104775562573SAdrian Hunter 	return __perf_evlist__combined_sample_type(evlist);
1048c2a70653SArnaldo Carvalho de Melo }
1049c2a70653SArnaldo Carvalho de Melo 
10509ede473cSJiri Olsa bool perf_evlist__valid_read_format(struct perf_evlist *evlist)
10519ede473cSJiri Olsa {
10529ede473cSJiri Olsa 	struct perf_evsel *first = perf_evlist__first(evlist), *pos = first;
10539ede473cSJiri Olsa 	u64 read_format = first->attr.read_format;
10549ede473cSJiri Olsa 	u64 sample_type = first->attr.sample_type;
10559ede473cSJiri Olsa 
10560050f7aaSArnaldo Carvalho de Melo 	evlist__for_each(evlist, pos) {
10579ede473cSJiri Olsa 		if (read_format != pos->attr.read_format)
10589ede473cSJiri Olsa 			return false;
10599ede473cSJiri Olsa 	}
10609ede473cSJiri Olsa 
10619ede473cSJiri Olsa 	/* PERF_SAMPLE_READ imples PERF_FORMAT_ID. */
10629ede473cSJiri Olsa 	if ((sample_type & PERF_SAMPLE_READ) &&
10639ede473cSJiri Olsa 	    !(read_format & PERF_FORMAT_ID)) {
10649ede473cSJiri Olsa 		return false;
10659ede473cSJiri Olsa 	}
10669ede473cSJiri Olsa 
10679ede473cSJiri Olsa 	return true;
10689ede473cSJiri Olsa }
10699ede473cSJiri Olsa 
10709ede473cSJiri Olsa u64 perf_evlist__read_format(struct perf_evlist *evlist)
10719ede473cSJiri Olsa {
10729ede473cSJiri Olsa 	struct perf_evsel *first = perf_evlist__first(evlist);
10739ede473cSJiri Olsa 	return first->attr.read_format;
10749ede473cSJiri Olsa }
10759ede473cSJiri Olsa 
10760c21f736SArnaldo Carvalho de Melo u16 perf_evlist__id_hdr_size(struct perf_evlist *evlist)
107781e36bffSArnaldo Carvalho de Melo {
10780c21f736SArnaldo Carvalho de Melo 	struct perf_evsel *first = perf_evlist__first(evlist);
107981e36bffSArnaldo Carvalho de Melo 	struct perf_sample *data;
108081e36bffSArnaldo Carvalho de Melo 	u64 sample_type;
108181e36bffSArnaldo Carvalho de Melo 	u16 size = 0;
108281e36bffSArnaldo Carvalho de Melo 
108381e36bffSArnaldo Carvalho de Melo 	if (!first->attr.sample_id_all)
108481e36bffSArnaldo Carvalho de Melo 		goto out;
108581e36bffSArnaldo Carvalho de Melo 
108681e36bffSArnaldo Carvalho de Melo 	sample_type = first->attr.sample_type;
108781e36bffSArnaldo Carvalho de Melo 
108881e36bffSArnaldo Carvalho de Melo 	if (sample_type & PERF_SAMPLE_TID)
108981e36bffSArnaldo Carvalho de Melo 		size += sizeof(data->tid) * 2;
109081e36bffSArnaldo Carvalho de Melo 
109181e36bffSArnaldo Carvalho de Melo        if (sample_type & PERF_SAMPLE_TIME)
109281e36bffSArnaldo Carvalho de Melo 		size += sizeof(data->time);
109381e36bffSArnaldo Carvalho de Melo 
109481e36bffSArnaldo Carvalho de Melo 	if (sample_type & PERF_SAMPLE_ID)
109581e36bffSArnaldo Carvalho de Melo 		size += sizeof(data->id);
109681e36bffSArnaldo Carvalho de Melo 
109781e36bffSArnaldo Carvalho de Melo 	if (sample_type & PERF_SAMPLE_STREAM_ID)
109881e36bffSArnaldo Carvalho de Melo 		size += sizeof(data->stream_id);
109981e36bffSArnaldo Carvalho de Melo 
110081e36bffSArnaldo Carvalho de Melo 	if (sample_type & PERF_SAMPLE_CPU)
110181e36bffSArnaldo Carvalho de Melo 		size += sizeof(data->cpu) * 2;
110275562573SAdrian Hunter 
110375562573SAdrian Hunter 	if (sample_type & PERF_SAMPLE_IDENTIFIER)
110475562573SAdrian Hunter 		size += sizeof(data->id);
110581e36bffSArnaldo Carvalho de Melo out:
110681e36bffSArnaldo Carvalho de Melo 	return size;
110781e36bffSArnaldo Carvalho de Melo }
110881e36bffSArnaldo Carvalho de Melo 
11090c21f736SArnaldo Carvalho de Melo bool perf_evlist__valid_sample_id_all(struct perf_evlist *evlist)
1110c2a70653SArnaldo Carvalho de Melo {
11110c21f736SArnaldo Carvalho de Melo 	struct perf_evsel *first = perf_evlist__first(evlist), *pos = first;
1112c2a70653SArnaldo Carvalho de Melo 
11130050f7aaSArnaldo Carvalho de Melo 	evlist__for_each_continue(evlist, pos) {
1114c2a70653SArnaldo Carvalho de Melo 		if (first->attr.sample_id_all != pos->attr.sample_id_all)
1115c2a70653SArnaldo Carvalho de Melo 			return false;
1116c2a70653SArnaldo Carvalho de Melo 	}
1117c2a70653SArnaldo Carvalho de Melo 
1118c2a70653SArnaldo Carvalho de Melo 	return true;
111974429964SFrederic Weisbecker }
112074429964SFrederic Weisbecker 
11210c21f736SArnaldo Carvalho de Melo bool perf_evlist__sample_id_all(struct perf_evlist *evlist)
112274429964SFrederic Weisbecker {
11230c21f736SArnaldo Carvalho de Melo 	struct perf_evsel *first = perf_evlist__first(evlist);
1124c2a70653SArnaldo Carvalho de Melo 	return first->attr.sample_id_all;
112574429964SFrederic Weisbecker }
112681cce8deSArnaldo Carvalho de Melo 
112781cce8deSArnaldo Carvalho de Melo void perf_evlist__set_selected(struct perf_evlist *evlist,
112881cce8deSArnaldo Carvalho de Melo 			       struct perf_evsel *evsel)
112981cce8deSArnaldo Carvalho de Melo {
113081cce8deSArnaldo Carvalho de Melo 	evlist->selected = evsel;
113181cce8deSArnaldo Carvalho de Melo }
1132727ab04eSArnaldo Carvalho de Melo 
1133a74b4b66SNamhyung Kim void perf_evlist__close(struct perf_evlist *evlist)
1134a74b4b66SNamhyung Kim {
1135a74b4b66SNamhyung Kim 	struct perf_evsel *evsel;
1136a74b4b66SNamhyung Kim 	int ncpus = cpu_map__nr(evlist->cpus);
1137a74b4b66SNamhyung Kim 	int nthreads = thread_map__nr(evlist->threads);
11388ad9219eSStephane Eranian 	int n;
1139a74b4b66SNamhyung Kim 
11408ad9219eSStephane Eranian 	evlist__for_each_reverse(evlist, evsel) {
11418ad9219eSStephane Eranian 		n = evsel->cpus ? evsel->cpus->nr : ncpus;
11428ad9219eSStephane Eranian 		perf_evsel__close(evsel, n, nthreads);
11438ad9219eSStephane Eranian 	}
1144a74b4b66SNamhyung Kim }
1145a74b4b66SNamhyung Kim 
11466a4bb04cSJiri Olsa int perf_evlist__open(struct perf_evlist *evlist)
1147727ab04eSArnaldo Carvalho de Melo {
11486a4bb04cSJiri Olsa 	struct perf_evsel *evsel;
1149a74b4b66SNamhyung Kim 	int err;
1150727ab04eSArnaldo Carvalho de Melo 
1151733cd2feSAdrian Hunter 	perf_evlist__update_id_pos(evlist);
1152733cd2feSAdrian Hunter 
11530050f7aaSArnaldo Carvalho de Melo 	evlist__for_each(evlist, evsel) {
11546a4bb04cSJiri Olsa 		err = perf_evsel__open(evsel, evlist->cpus, evlist->threads);
1155727ab04eSArnaldo Carvalho de Melo 		if (err < 0)
1156727ab04eSArnaldo Carvalho de Melo 			goto out_err;
1157727ab04eSArnaldo Carvalho de Melo 	}
1158727ab04eSArnaldo Carvalho de Melo 
1159727ab04eSArnaldo Carvalho de Melo 	return 0;
1160727ab04eSArnaldo Carvalho de Melo out_err:
1161a74b4b66SNamhyung Kim 	perf_evlist__close(evlist);
116241c21a68SNamhyung Kim 	errno = -err;
1163727ab04eSArnaldo Carvalho de Melo 	return err;
1164727ab04eSArnaldo Carvalho de Melo }
116535b9d88eSArnaldo Carvalho de Melo 
1166602ad878SArnaldo Carvalho de Melo int perf_evlist__prepare_workload(struct perf_evlist *evlist, struct target *target,
116755e162eaSNamhyung Kim 				  const char *argv[], bool pipe_output,
1168735f7e0bSArnaldo Carvalho de Melo 				  void (*exec_error)(int signo, siginfo_t *info, void *ucontext))
116935b9d88eSArnaldo Carvalho de Melo {
117035b9d88eSArnaldo Carvalho de Melo 	int child_ready_pipe[2], go_pipe[2];
117135b9d88eSArnaldo Carvalho de Melo 	char bf;
117235b9d88eSArnaldo Carvalho de Melo 
117335b9d88eSArnaldo Carvalho de Melo 	if (pipe(child_ready_pipe) < 0) {
117435b9d88eSArnaldo Carvalho de Melo 		perror("failed to create 'ready' pipe");
117535b9d88eSArnaldo Carvalho de Melo 		return -1;
117635b9d88eSArnaldo Carvalho de Melo 	}
117735b9d88eSArnaldo Carvalho de Melo 
117835b9d88eSArnaldo Carvalho de Melo 	if (pipe(go_pipe) < 0) {
117935b9d88eSArnaldo Carvalho de Melo 		perror("failed to create 'go' pipe");
118035b9d88eSArnaldo Carvalho de Melo 		goto out_close_ready_pipe;
118135b9d88eSArnaldo Carvalho de Melo 	}
118235b9d88eSArnaldo Carvalho de Melo 
118335b9d88eSArnaldo Carvalho de Melo 	evlist->workload.pid = fork();
118435b9d88eSArnaldo Carvalho de Melo 	if (evlist->workload.pid < 0) {
118535b9d88eSArnaldo Carvalho de Melo 		perror("failed to fork");
118635b9d88eSArnaldo Carvalho de Melo 		goto out_close_pipes;
118735b9d88eSArnaldo Carvalho de Melo 	}
118835b9d88eSArnaldo Carvalho de Melo 
118935b9d88eSArnaldo Carvalho de Melo 	if (!evlist->workload.pid) {
11905f1c4225SArnaldo Carvalho de Melo 		int ret;
11915f1c4225SArnaldo Carvalho de Melo 
1192119fa3c9SNamhyung Kim 		if (pipe_output)
119335b9d88eSArnaldo Carvalho de Melo 			dup2(2, 1);
119435b9d88eSArnaldo Carvalho de Melo 
11950817df08SDavid Ahern 		signal(SIGTERM, SIG_DFL);
11960817df08SDavid Ahern 
119735b9d88eSArnaldo Carvalho de Melo 		close(child_ready_pipe[0]);
119835b9d88eSArnaldo Carvalho de Melo 		close(go_pipe[1]);
119935b9d88eSArnaldo Carvalho de Melo 		fcntl(go_pipe[0], F_SETFD, FD_CLOEXEC);
120035b9d88eSArnaldo Carvalho de Melo 
120135b9d88eSArnaldo Carvalho de Melo 		/*
120235b9d88eSArnaldo Carvalho de Melo 		 * Tell the parent we're ready to go
120335b9d88eSArnaldo Carvalho de Melo 		 */
120435b9d88eSArnaldo Carvalho de Melo 		close(child_ready_pipe[1]);
120535b9d88eSArnaldo Carvalho de Melo 
120635b9d88eSArnaldo Carvalho de Melo 		/*
120735b9d88eSArnaldo Carvalho de Melo 		 * Wait until the parent tells us to go.
120835b9d88eSArnaldo Carvalho de Melo 		 */
12095f1c4225SArnaldo Carvalho de Melo 		ret = read(go_pipe[0], &bf, 1);
12105f1c4225SArnaldo Carvalho de Melo 		/*
12115f1c4225SArnaldo Carvalho de Melo 		 * The parent will ask for the execvp() to be performed by
12125f1c4225SArnaldo Carvalho de Melo 		 * writing exactly one byte, in workload.cork_fd, usually via
12135f1c4225SArnaldo Carvalho de Melo 		 * perf_evlist__start_workload().
12145f1c4225SArnaldo Carvalho de Melo 		 *
12155f1c4225SArnaldo Carvalho de Melo 		 * For cancelling the workload without actuallin running it,
12165f1c4225SArnaldo Carvalho de Melo 		 * the parent will just close workload.cork_fd, without writing
12175f1c4225SArnaldo Carvalho de Melo 		 * anything, i.e. read will return zero and we just exit()
12185f1c4225SArnaldo Carvalho de Melo 		 * here.
12195f1c4225SArnaldo Carvalho de Melo 		 */
12205f1c4225SArnaldo Carvalho de Melo 		if (ret != 1) {
12215f1c4225SArnaldo Carvalho de Melo 			if (ret == -1)
122235b9d88eSArnaldo Carvalho de Melo 				perror("unable to read pipe");
12235f1c4225SArnaldo Carvalho de Melo 			exit(ret);
12245f1c4225SArnaldo Carvalho de Melo 		}
122535b9d88eSArnaldo Carvalho de Melo 
122635b9d88eSArnaldo Carvalho de Melo 		execvp(argv[0], (char **)argv);
122735b9d88eSArnaldo Carvalho de Melo 
1228735f7e0bSArnaldo Carvalho de Melo 		if (exec_error) {
1229f33cbe72SArnaldo Carvalho de Melo 			union sigval val;
1230f33cbe72SArnaldo Carvalho de Melo 
1231f33cbe72SArnaldo Carvalho de Melo 			val.sival_int = errno;
1232f33cbe72SArnaldo Carvalho de Melo 			if (sigqueue(getppid(), SIGUSR1, val))
123335b9d88eSArnaldo Carvalho de Melo 				perror(argv[0]);
1234f33cbe72SArnaldo Carvalho de Melo 		} else
1235f33cbe72SArnaldo Carvalho de Melo 			perror(argv[0]);
123635b9d88eSArnaldo Carvalho de Melo 		exit(-1);
123735b9d88eSArnaldo Carvalho de Melo 	}
123835b9d88eSArnaldo Carvalho de Melo 
1239735f7e0bSArnaldo Carvalho de Melo 	if (exec_error) {
1240735f7e0bSArnaldo Carvalho de Melo 		struct sigaction act = {
1241735f7e0bSArnaldo Carvalho de Melo 			.sa_flags     = SA_SIGINFO,
1242735f7e0bSArnaldo Carvalho de Melo 			.sa_sigaction = exec_error,
1243735f7e0bSArnaldo Carvalho de Melo 		};
1244735f7e0bSArnaldo Carvalho de Melo 		sigaction(SIGUSR1, &act, NULL);
1245735f7e0bSArnaldo Carvalho de Melo 	}
1246735f7e0bSArnaldo Carvalho de Melo 
1247602ad878SArnaldo Carvalho de Melo 	if (target__none(target))
124835b9d88eSArnaldo Carvalho de Melo 		evlist->threads->map[0] = evlist->workload.pid;
124935b9d88eSArnaldo Carvalho de Melo 
125035b9d88eSArnaldo Carvalho de Melo 	close(child_ready_pipe[1]);
125135b9d88eSArnaldo Carvalho de Melo 	close(go_pipe[0]);
125235b9d88eSArnaldo Carvalho de Melo 	/*
125335b9d88eSArnaldo Carvalho de Melo 	 * wait for child to settle
125435b9d88eSArnaldo Carvalho de Melo 	 */
125535b9d88eSArnaldo Carvalho de Melo 	if (read(child_ready_pipe[0], &bf, 1) == -1) {
125635b9d88eSArnaldo Carvalho de Melo 		perror("unable to read pipe");
125735b9d88eSArnaldo Carvalho de Melo 		goto out_close_pipes;
125835b9d88eSArnaldo Carvalho de Melo 	}
125935b9d88eSArnaldo Carvalho de Melo 
1260bcf3145fSNamhyung Kim 	fcntl(go_pipe[1], F_SETFD, FD_CLOEXEC);
126135b9d88eSArnaldo Carvalho de Melo 	evlist->workload.cork_fd = go_pipe[1];
126235b9d88eSArnaldo Carvalho de Melo 	close(child_ready_pipe[0]);
126335b9d88eSArnaldo Carvalho de Melo 	return 0;
126435b9d88eSArnaldo Carvalho de Melo 
126535b9d88eSArnaldo Carvalho de Melo out_close_pipes:
126635b9d88eSArnaldo Carvalho de Melo 	close(go_pipe[0]);
126735b9d88eSArnaldo Carvalho de Melo 	close(go_pipe[1]);
126835b9d88eSArnaldo Carvalho de Melo out_close_ready_pipe:
126935b9d88eSArnaldo Carvalho de Melo 	close(child_ready_pipe[0]);
127035b9d88eSArnaldo Carvalho de Melo 	close(child_ready_pipe[1]);
127135b9d88eSArnaldo Carvalho de Melo 	return -1;
127235b9d88eSArnaldo Carvalho de Melo }
127335b9d88eSArnaldo Carvalho de Melo 
127435b9d88eSArnaldo Carvalho de Melo int perf_evlist__start_workload(struct perf_evlist *evlist)
127535b9d88eSArnaldo Carvalho de Melo {
127635b9d88eSArnaldo Carvalho de Melo 	if (evlist->workload.cork_fd > 0) {
1277b3824404SDavid Ahern 		char bf = 0;
1278bcf3145fSNamhyung Kim 		int ret;
127935b9d88eSArnaldo Carvalho de Melo 		/*
128035b9d88eSArnaldo Carvalho de Melo 		 * Remove the cork, let it rip!
128135b9d88eSArnaldo Carvalho de Melo 		 */
1282bcf3145fSNamhyung Kim 		ret = write(evlist->workload.cork_fd, &bf, 1);
1283bcf3145fSNamhyung Kim 		if (ret < 0)
1284bcf3145fSNamhyung Kim 			perror("enable to write to pipe");
1285bcf3145fSNamhyung Kim 
1286bcf3145fSNamhyung Kim 		close(evlist->workload.cork_fd);
1287bcf3145fSNamhyung Kim 		return ret;
128835b9d88eSArnaldo Carvalho de Melo 	}
128935b9d88eSArnaldo Carvalho de Melo 
129035b9d88eSArnaldo Carvalho de Melo 	return 0;
129135b9d88eSArnaldo Carvalho de Melo }
1292cb0b29e0SArnaldo Carvalho de Melo 
1293a3f698feSArnaldo Carvalho de Melo int perf_evlist__parse_sample(struct perf_evlist *evlist, union perf_event *event,
12940807d2d8SArnaldo Carvalho de Melo 			      struct perf_sample *sample)
1295cb0b29e0SArnaldo Carvalho de Melo {
129675562573SAdrian Hunter 	struct perf_evsel *evsel = perf_evlist__event2evsel(evlist, event);
129775562573SAdrian Hunter 
129875562573SAdrian Hunter 	if (!evsel)
129975562573SAdrian Hunter 		return -EFAULT;
13000807d2d8SArnaldo Carvalho de Melo 	return perf_evsel__parse_sample(evsel, event, sample);
1301cb0b29e0SArnaldo Carvalho de Melo }
130278f067b3SArnaldo Carvalho de Melo 
130378f067b3SArnaldo Carvalho de Melo size_t perf_evlist__fprintf(struct perf_evlist *evlist, FILE *fp)
130478f067b3SArnaldo Carvalho de Melo {
130578f067b3SArnaldo Carvalho de Melo 	struct perf_evsel *evsel;
130678f067b3SArnaldo Carvalho de Melo 	size_t printed = 0;
130778f067b3SArnaldo Carvalho de Melo 
13080050f7aaSArnaldo Carvalho de Melo 	evlist__for_each(evlist, evsel) {
130978f067b3SArnaldo Carvalho de Melo 		printed += fprintf(fp, "%s%s", evsel->idx ? ", " : "",
131078f067b3SArnaldo Carvalho de Melo 				   perf_evsel__name(evsel));
131178f067b3SArnaldo Carvalho de Melo 	}
131278f067b3SArnaldo Carvalho de Melo 
1313b2222139SDavidlohr Bueso 	return printed + fprintf(fp, "\n");
131478f067b3SArnaldo Carvalho de Melo }
13156ef068cbSArnaldo Carvalho de Melo 
13166ef068cbSArnaldo Carvalho de Melo int perf_evlist__strerror_tp(struct perf_evlist *evlist __maybe_unused,
13176ef068cbSArnaldo Carvalho de Melo 			     int err, char *buf, size_t size)
13186ef068cbSArnaldo Carvalho de Melo {
13196ef068cbSArnaldo Carvalho de Melo 	char sbuf[128];
13206ef068cbSArnaldo Carvalho de Melo 
13216ef068cbSArnaldo Carvalho de Melo 	switch (err) {
13226ef068cbSArnaldo Carvalho de Melo 	case ENOENT:
13236ef068cbSArnaldo Carvalho de Melo 		scnprintf(buf, size, "%s",
13246ef068cbSArnaldo Carvalho de Melo 			  "Error:\tUnable to find debugfs\n"
13256ef068cbSArnaldo Carvalho de Melo 			  "Hint:\tWas your kernel was compiled with debugfs support?\n"
13266ef068cbSArnaldo Carvalho de Melo 			  "Hint:\tIs the debugfs filesystem mounted?\n"
13276ef068cbSArnaldo Carvalho de Melo 			  "Hint:\tTry 'sudo mount -t debugfs nodev /sys/kernel/debug'");
13286ef068cbSArnaldo Carvalho de Melo 		break;
13296ef068cbSArnaldo Carvalho de Melo 	case EACCES:
13306ef068cbSArnaldo Carvalho de Melo 		scnprintf(buf, size,
13316ef068cbSArnaldo Carvalho de Melo 			  "Error:\tNo permissions to read %s/tracing/events/raw_syscalls\n"
13326ef068cbSArnaldo Carvalho de Melo 			  "Hint:\tTry 'sudo mount -o remount,mode=755 %s'\n",
13336ef068cbSArnaldo Carvalho de Melo 			  debugfs_mountpoint, debugfs_mountpoint);
13346ef068cbSArnaldo Carvalho de Melo 		break;
13356ef068cbSArnaldo Carvalho de Melo 	default:
13366ef068cbSArnaldo Carvalho de Melo 		scnprintf(buf, size, "%s", strerror_r(err, sbuf, sizeof(sbuf)));
13376ef068cbSArnaldo Carvalho de Melo 		break;
13386ef068cbSArnaldo Carvalho de Melo 	}
13396ef068cbSArnaldo Carvalho de Melo 
13406ef068cbSArnaldo Carvalho de Melo 	return 0;
13416ef068cbSArnaldo Carvalho de Melo }
1342a8f23d8fSArnaldo Carvalho de Melo 
1343a8f23d8fSArnaldo Carvalho de Melo int perf_evlist__strerror_open(struct perf_evlist *evlist __maybe_unused,
1344a8f23d8fSArnaldo Carvalho de Melo 			       int err, char *buf, size_t size)
1345a8f23d8fSArnaldo Carvalho de Melo {
1346a8f23d8fSArnaldo Carvalho de Melo 	int printed, value;
13476e81c74cSMasami Hiramatsu 	char sbuf[STRERR_BUFSIZE], *emsg = strerror_r(err, sbuf, sizeof(sbuf));
1348a8f23d8fSArnaldo Carvalho de Melo 
1349a8f23d8fSArnaldo Carvalho de Melo 	switch (err) {
1350a8f23d8fSArnaldo Carvalho de Melo 	case EACCES:
1351a8f23d8fSArnaldo Carvalho de Melo 	case EPERM:
1352a8f23d8fSArnaldo Carvalho de Melo 		printed = scnprintf(buf, size,
1353a8f23d8fSArnaldo Carvalho de Melo 				    "Error:\t%s.\n"
1354a8f23d8fSArnaldo Carvalho de Melo 				    "Hint:\tCheck /proc/sys/kernel/perf_event_paranoid setting.", emsg);
1355a8f23d8fSArnaldo Carvalho de Melo 
13561a47245dSAdrian Hunter 		value = perf_event_paranoid();
1357a8f23d8fSArnaldo Carvalho de Melo 
1358a8f23d8fSArnaldo Carvalho de Melo 		printed += scnprintf(buf + printed, size - printed, "\nHint:\t");
1359a8f23d8fSArnaldo Carvalho de Melo 
1360a8f23d8fSArnaldo Carvalho de Melo 		if (value >= 2) {
1361a8f23d8fSArnaldo Carvalho de Melo 			printed += scnprintf(buf + printed, size - printed,
1362a8f23d8fSArnaldo Carvalho de Melo 					     "For your workloads it needs to be <= 1\nHint:\t");
1363a8f23d8fSArnaldo Carvalho de Melo 		}
1364a8f23d8fSArnaldo Carvalho de Melo 		printed += scnprintf(buf + printed, size - printed,
13655229e366SArnaldo Carvalho de Melo 				     "For system wide tracing it needs to be set to -1.\n");
1366a8f23d8fSArnaldo Carvalho de Melo 
1367a8f23d8fSArnaldo Carvalho de Melo 		printed += scnprintf(buf + printed, size - printed,
13685229e366SArnaldo Carvalho de Melo 				    "Hint:\tTry: 'sudo sh -c \"echo -1 > /proc/sys/kernel/perf_event_paranoid\"'\n"
13695229e366SArnaldo Carvalho de Melo 				    "Hint:\tThe current value is %d.", value);
1370a8f23d8fSArnaldo Carvalho de Melo 		break;
1371a8f23d8fSArnaldo Carvalho de Melo 	default:
1372a8f23d8fSArnaldo Carvalho de Melo 		scnprintf(buf, size, "%s", emsg);
1373a8f23d8fSArnaldo Carvalho de Melo 		break;
1374a8f23d8fSArnaldo Carvalho de Melo 	}
1375a8f23d8fSArnaldo Carvalho de Melo 
1376a8f23d8fSArnaldo Carvalho de Melo 	return 0;
1377a8f23d8fSArnaldo Carvalho de Melo }
1378a025e4f0SAdrian Hunter 
1379a025e4f0SAdrian Hunter void perf_evlist__to_front(struct perf_evlist *evlist,
1380a025e4f0SAdrian Hunter 			   struct perf_evsel *move_evsel)
1381a025e4f0SAdrian Hunter {
1382a025e4f0SAdrian Hunter 	struct perf_evsel *evsel, *n;
1383a025e4f0SAdrian Hunter 	LIST_HEAD(move);
1384a025e4f0SAdrian Hunter 
1385a025e4f0SAdrian Hunter 	if (move_evsel == perf_evlist__first(evlist))
1386a025e4f0SAdrian Hunter 		return;
1387a025e4f0SAdrian Hunter 
13880050f7aaSArnaldo Carvalho de Melo 	evlist__for_each_safe(evlist, n, evsel) {
1389a025e4f0SAdrian Hunter 		if (evsel->leader == move_evsel->leader)
1390a025e4f0SAdrian Hunter 			list_move_tail(&evsel->node, &move);
1391a025e4f0SAdrian Hunter 	}
1392a025e4f0SAdrian Hunter 
1393a025e4f0SAdrian Hunter 	list_splice(&move, &evlist->entries);
1394a025e4f0SAdrian Hunter }
139560b0896cSAdrian Hunter 
139660b0896cSAdrian Hunter void perf_evlist__set_tracking_event(struct perf_evlist *evlist,
139760b0896cSAdrian Hunter 				     struct perf_evsel *tracking_evsel)
139860b0896cSAdrian Hunter {
139960b0896cSAdrian Hunter 	struct perf_evsel *evsel;
140060b0896cSAdrian Hunter 
140160b0896cSAdrian Hunter 	if (tracking_evsel->tracking)
140260b0896cSAdrian Hunter 		return;
140360b0896cSAdrian Hunter 
140460b0896cSAdrian Hunter 	evlist__for_each(evlist, evsel) {
140560b0896cSAdrian Hunter 		if (evsel != tracking_evsel)
140660b0896cSAdrian Hunter 			evsel->tracking = false;
140760b0896cSAdrian Hunter 	}
140860b0896cSAdrian Hunter 
140960b0896cSAdrian Hunter 	tracking_evsel->tracking = true;
141060b0896cSAdrian Hunter }
1411