xref: /openbmc/linux/tools/perf/util/evlist.c (revision 8db6d6b1)
1f8a95309SArnaldo Carvalho de Melo /*
2f8a95309SArnaldo Carvalho de Melo  * Copyright (C) 2011, Red Hat Inc, Arnaldo Carvalho de Melo <acme@redhat.com>
3f8a95309SArnaldo Carvalho de Melo  *
4f8a95309SArnaldo Carvalho de Melo  * Parts came from builtin-{top,stat,record}.c, see those files for further
5f8a95309SArnaldo Carvalho de Melo  * copyright notes.
6f8a95309SArnaldo Carvalho de Melo  *
7f8a95309SArnaldo Carvalho de Melo  * Released under the GPL v2. (and only v2, not any later version)
8f8a95309SArnaldo Carvalho de Melo  */
9a8c9ae18SArnaldo Carvalho de Melo #include "util.h"
10956fa571SArnaldo Carvalho de Melo #include <api/fs/fs.h>
115c581041SArnaldo Carvalho de Melo #include <poll.h>
12f8a95309SArnaldo Carvalho de Melo #include "cpumap.h"
13f8a95309SArnaldo Carvalho de Melo #include "thread_map.h"
1412864b31SNamhyung Kim #include "target.h"
15361c99a6SArnaldo Carvalho de Melo #include "evlist.h"
16361c99a6SArnaldo Carvalho de Melo #include "evsel.h"
17e3e1a54fSAdrian Hunter #include "debug.h"
1835b9d88eSArnaldo Carvalho de Melo #include <unistd.h>
19361c99a6SArnaldo Carvalho de Melo 
2050d08e47SArnaldo Carvalho de Melo #include "parse-events.h"
214b6ab94eSJosh Poimboeuf #include <subcmd/parse-options.h>
2250d08e47SArnaldo Carvalho de Melo 
23f8a95309SArnaldo Carvalho de Melo #include <sys/mman.h>
24f8a95309SArnaldo Carvalho de Melo 
2570db7533SArnaldo Carvalho de Melo #include <linux/bitops.h>
2670db7533SArnaldo Carvalho de Melo #include <linux/hash.h>
270389cd1fSArnaldo Carvalho de Melo #include <linux/log2.h>
288dd2a131SJiri Olsa #include <linux/err.h>
2970db7533SArnaldo Carvalho de Melo 
30e4b356b5SArnaldo Carvalho de Melo static void perf_evlist__mmap_put(struct perf_evlist *evlist, int idx);
31e4b356b5SArnaldo Carvalho de Melo static void __perf_evlist__munmap(struct perf_evlist *evlist, int idx);
328db6d6b1SWang Nan static void perf_mmap__munmap(struct perf_mmap *map);
33e4b356b5SArnaldo Carvalho de Melo 
34f8a95309SArnaldo Carvalho de Melo #define FD(e, x, y) (*(int *)xyarray__entry(e->fd, x, y))
35a91e5431SArnaldo Carvalho de Melo #define SID(e, x, y) xyarray__entry(e->sample_id, x, y)
36f8a95309SArnaldo Carvalho de Melo 
377e2ed097SArnaldo Carvalho de Melo void perf_evlist__init(struct perf_evlist *evlist, struct cpu_map *cpus,
387e2ed097SArnaldo Carvalho de Melo 		       struct thread_map *threads)
39361c99a6SArnaldo Carvalho de Melo {
4070db7533SArnaldo Carvalho de Melo 	int i;
4170db7533SArnaldo Carvalho de Melo 
4270db7533SArnaldo Carvalho de Melo 	for (i = 0; i < PERF_EVLIST__HLIST_SIZE; ++i)
4370db7533SArnaldo Carvalho de Melo 		INIT_HLIST_HEAD(&evlist->heads[i]);
44361c99a6SArnaldo Carvalho de Melo 	INIT_LIST_HEAD(&evlist->entries);
457e2ed097SArnaldo Carvalho de Melo 	perf_evlist__set_maps(evlist, cpus, threads);
461b85337dSArnaldo Carvalho de Melo 	fdarray__init(&evlist->pollfd, 64);
4735b9d88eSArnaldo Carvalho de Melo 	evlist->workload.pid = -1;
483a62a7b8SWang Nan 	evlist->backward = false;
49361c99a6SArnaldo Carvalho de Melo }
50361c99a6SArnaldo Carvalho de Melo 
51334fe7a3SNamhyung Kim struct perf_evlist *perf_evlist__new(void)
52ef1d1af2SArnaldo Carvalho de Melo {
53ef1d1af2SArnaldo Carvalho de Melo 	struct perf_evlist *evlist = zalloc(sizeof(*evlist));
54ef1d1af2SArnaldo Carvalho de Melo 
55ef1d1af2SArnaldo Carvalho de Melo 	if (evlist != NULL)
56334fe7a3SNamhyung Kim 		perf_evlist__init(evlist, NULL, NULL);
57ef1d1af2SArnaldo Carvalho de Melo 
58361c99a6SArnaldo Carvalho de Melo 	return evlist;
59361c99a6SArnaldo Carvalho de Melo }
60361c99a6SArnaldo Carvalho de Melo 
61b22d54b0SJiri Olsa struct perf_evlist *perf_evlist__new_default(void)
62b22d54b0SJiri Olsa {
63b22d54b0SJiri Olsa 	struct perf_evlist *evlist = perf_evlist__new();
64b22d54b0SJiri Olsa 
65b22d54b0SJiri Olsa 	if (evlist && perf_evlist__add_default(evlist)) {
66b22d54b0SJiri Olsa 		perf_evlist__delete(evlist);
67b22d54b0SJiri Olsa 		evlist = NULL;
68b22d54b0SJiri Olsa 	}
69b22d54b0SJiri Olsa 
70b22d54b0SJiri Olsa 	return evlist;
71b22d54b0SJiri Olsa }
72b22d54b0SJiri Olsa 
735bae0250SArnaldo Carvalho de Melo struct perf_evlist *perf_evlist__new_dummy(void)
745bae0250SArnaldo Carvalho de Melo {
755bae0250SArnaldo Carvalho de Melo 	struct perf_evlist *evlist = perf_evlist__new();
765bae0250SArnaldo Carvalho de Melo 
775bae0250SArnaldo Carvalho de Melo 	if (evlist && perf_evlist__add_dummy(evlist)) {
785bae0250SArnaldo Carvalho de Melo 		perf_evlist__delete(evlist);
795bae0250SArnaldo Carvalho de Melo 		evlist = NULL;
805bae0250SArnaldo Carvalho de Melo 	}
815bae0250SArnaldo Carvalho de Melo 
825bae0250SArnaldo Carvalho de Melo 	return evlist;
835bae0250SArnaldo Carvalho de Melo }
845bae0250SArnaldo Carvalho de Melo 
8575562573SAdrian Hunter /**
8675562573SAdrian Hunter  * perf_evlist__set_id_pos - set the positions of event ids.
8775562573SAdrian Hunter  * @evlist: selected event list
8875562573SAdrian Hunter  *
8975562573SAdrian Hunter  * Events with compatible sample types all have the same id_pos
9075562573SAdrian Hunter  * and is_pos.  For convenience, put a copy on evlist.
9175562573SAdrian Hunter  */
9275562573SAdrian Hunter void perf_evlist__set_id_pos(struct perf_evlist *evlist)
9375562573SAdrian Hunter {
9475562573SAdrian Hunter 	struct perf_evsel *first = perf_evlist__first(evlist);
9575562573SAdrian Hunter 
9675562573SAdrian Hunter 	evlist->id_pos = first->id_pos;
9775562573SAdrian Hunter 	evlist->is_pos = first->is_pos;
9875562573SAdrian Hunter }
9975562573SAdrian Hunter 
100733cd2feSAdrian Hunter static void perf_evlist__update_id_pos(struct perf_evlist *evlist)
101733cd2feSAdrian Hunter {
102733cd2feSAdrian Hunter 	struct perf_evsel *evsel;
103733cd2feSAdrian Hunter 
104e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, evsel)
105733cd2feSAdrian Hunter 		perf_evsel__calc_id_pos(evsel);
106733cd2feSAdrian Hunter 
107733cd2feSAdrian Hunter 	perf_evlist__set_id_pos(evlist);
108733cd2feSAdrian Hunter }
109733cd2feSAdrian Hunter 
110361c99a6SArnaldo Carvalho de Melo static void perf_evlist__purge(struct perf_evlist *evlist)
111361c99a6SArnaldo Carvalho de Melo {
112361c99a6SArnaldo Carvalho de Melo 	struct perf_evsel *pos, *n;
113361c99a6SArnaldo Carvalho de Melo 
114e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry_safe(evlist, n, pos) {
115361c99a6SArnaldo Carvalho de Melo 		list_del_init(&pos->node);
116d49e4695SArnaldo Carvalho de Melo 		pos->evlist = NULL;
117361c99a6SArnaldo Carvalho de Melo 		perf_evsel__delete(pos);
118361c99a6SArnaldo Carvalho de Melo 	}
119361c99a6SArnaldo Carvalho de Melo 
120361c99a6SArnaldo Carvalho de Melo 	evlist->nr_entries = 0;
121361c99a6SArnaldo Carvalho de Melo }
122361c99a6SArnaldo Carvalho de Melo 
123ef1d1af2SArnaldo Carvalho de Melo void perf_evlist__exit(struct perf_evlist *evlist)
124ef1d1af2SArnaldo Carvalho de Melo {
12504662523SArnaldo Carvalho de Melo 	zfree(&evlist->mmap);
1261b85337dSArnaldo Carvalho de Melo 	fdarray__exit(&evlist->pollfd);
127ef1d1af2SArnaldo Carvalho de Melo }
128ef1d1af2SArnaldo Carvalho de Melo 
129361c99a6SArnaldo Carvalho de Melo void perf_evlist__delete(struct perf_evlist *evlist)
130361c99a6SArnaldo Carvalho de Melo {
1310b04b3dcSArnaldo Carvalho de Melo 	if (evlist == NULL)
1320b04b3dcSArnaldo Carvalho de Melo 		return;
1330b04b3dcSArnaldo Carvalho de Melo 
134983874d1SArnaldo Carvalho de Melo 	perf_evlist__munmap(evlist);
135f26e1c7cSArnaldo Carvalho de Melo 	perf_evlist__close(evlist);
136f30a79b0SJiri Olsa 	cpu_map__put(evlist->cpus);
137186fbb74SJiri Olsa 	thread_map__put(evlist->threads);
13803ad9747SArnaldo Carvalho de Melo 	evlist->cpus = NULL;
13903ad9747SArnaldo Carvalho de Melo 	evlist->threads = NULL;
140361c99a6SArnaldo Carvalho de Melo 	perf_evlist__purge(evlist);
141ef1d1af2SArnaldo Carvalho de Melo 	perf_evlist__exit(evlist);
142361c99a6SArnaldo Carvalho de Melo 	free(evlist);
143361c99a6SArnaldo Carvalho de Melo }
144361c99a6SArnaldo Carvalho de Melo 
145adc0c3e8SAdrian Hunter static void __perf_evlist__propagate_maps(struct perf_evlist *evlist,
146adc0c3e8SAdrian Hunter 					  struct perf_evsel *evsel)
147adc0c3e8SAdrian Hunter {
148adc0c3e8SAdrian Hunter 	/*
149adc0c3e8SAdrian Hunter 	 * We already have cpus for evsel (via PMU sysfs) so
150adc0c3e8SAdrian Hunter 	 * keep it, if there's no target cpu list defined.
151adc0c3e8SAdrian Hunter 	 */
152adc0c3e8SAdrian Hunter 	if (!evsel->own_cpus || evlist->has_user_cpus) {
153adc0c3e8SAdrian Hunter 		cpu_map__put(evsel->cpus);
154adc0c3e8SAdrian Hunter 		evsel->cpus = cpu_map__get(evlist->cpus);
155adc0c3e8SAdrian Hunter 	} else if (evsel->cpus != evsel->own_cpus) {
156adc0c3e8SAdrian Hunter 		cpu_map__put(evsel->cpus);
157adc0c3e8SAdrian Hunter 		evsel->cpus = cpu_map__get(evsel->own_cpus);
158adc0c3e8SAdrian Hunter 	}
159adc0c3e8SAdrian Hunter 
160adc0c3e8SAdrian Hunter 	thread_map__put(evsel->threads);
161adc0c3e8SAdrian Hunter 	evsel->threads = thread_map__get(evlist->threads);
162adc0c3e8SAdrian Hunter }
163adc0c3e8SAdrian Hunter 
164adc0c3e8SAdrian Hunter static void perf_evlist__propagate_maps(struct perf_evlist *evlist)
165adc0c3e8SAdrian Hunter {
166adc0c3e8SAdrian Hunter 	struct perf_evsel *evsel;
167adc0c3e8SAdrian Hunter 
168e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, evsel)
169adc0c3e8SAdrian Hunter 		__perf_evlist__propagate_maps(evlist, evsel);
170adc0c3e8SAdrian Hunter }
171adc0c3e8SAdrian Hunter 
172361c99a6SArnaldo Carvalho de Melo void perf_evlist__add(struct perf_evlist *evlist, struct perf_evsel *entry)
173361c99a6SArnaldo Carvalho de Melo {
174d49e4695SArnaldo Carvalho de Melo 	entry->evlist = evlist;
175361c99a6SArnaldo Carvalho de Melo 	list_add_tail(&entry->node, &evlist->entries);
176ef503831SArnaldo Carvalho de Melo 	entry->idx = evlist->nr_entries;
17760b0896cSAdrian Hunter 	entry->tracking = !entry->idx;
178ef503831SArnaldo Carvalho de Melo 
17975562573SAdrian Hunter 	if (!evlist->nr_entries++)
18075562573SAdrian Hunter 		perf_evlist__set_id_pos(evlist);
18144c42d71SAdrian Hunter 
18244c42d71SAdrian Hunter 	__perf_evlist__propagate_maps(evlist, entry);
183361c99a6SArnaldo Carvalho de Melo }
184361c99a6SArnaldo Carvalho de Melo 
1854768230aSAdrian Hunter void perf_evlist__remove(struct perf_evlist *evlist, struct perf_evsel *evsel)
1864768230aSAdrian Hunter {
1874768230aSAdrian Hunter 	evsel->evlist = NULL;
1884768230aSAdrian Hunter 	list_del_init(&evsel->node);
1894768230aSAdrian Hunter 	evlist->nr_entries -= 1;
1904768230aSAdrian Hunter }
1914768230aSAdrian Hunter 
1920529bc1fSJiri Olsa void perf_evlist__splice_list_tail(struct perf_evlist *evlist,
193f114d6efSAdrian Hunter 				   struct list_head *list)
19450d08e47SArnaldo Carvalho de Melo {
195f114d6efSAdrian Hunter 	struct perf_evsel *evsel, *temp;
19675562573SAdrian Hunter 
197e5cadb93SArnaldo Carvalho de Melo 	__evlist__for_each_entry_safe(list, temp, evsel) {
198f114d6efSAdrian Hunter 		list_del_init(&evsel->node);
199f114d6efSAdrian Hunter 		perf_evlist__add(evlist, evsel);
200f114d6efSAdrian Hunter 	}
20150d08e47SArnaldo Carvalho de Melo }
20250d08e47SArnaldo Carvalho de Melo 
20363dab225SArnaldo Carvalho de Melo void __perf_evlist__set_leader(struct list_head *list)
20463dab225SArnaldo Carvalho de Melo {
20563dab225SArnaldo Carvalho de Melo 	struct perf_evsel *evsel, *leader;
20663dab225SArnaldo Carvalho de Melo 
20763dab225SArnaldo Carvalho de Melo 	leader = list_entry(list->next, struct perf_evsel, node);
20897f63e4aSNamhyung Kim 	evsel = list_entry(list->prev, struct perf_evsel, node);
20997f63e4aSNamhyung Kim 
21097f63e4aSNamhyung Kim 	leader->nr_members = evsel->idx - leader->idx + 1;
21163dab225SArnaldo Carvalho de Melo 
212e5cadb93SArnaldo Carvalho de Melo 	__evlist__for_each_entry(list, evsel) {
21363dab225SArnaldo Carvalho de Melo 		evsel->leader = leader;
21463dab225SArnaldo Carvalho de Melo 	}
21563dab225SArnaldo Carvalho de Melo }
21663dab225SArnaldo Carvalho de Melo 
21763dab225SArnaldo Carvalho de Melo void perf_evlist__set_leader(struct perf_evlist *evlist)
2186a4bb04cSJiri Olsa {
21997f63e4aSNamhyung Kim 	if (evlist->nr_entries) {
22097f63e4aSNamhyung Kim 		evlist->nr_groups = evlist->nr_entries > 1 ? 1 : 0;
22163dab225SArnaldo Carvalho de Melo 		__perf_evlist__set_leader(&evlist->entries);
2226a4bb04cSJiri Olsa 	}
22397f63e4aSNamhyung Kim }
2246a4bb04cSJiri Olsa 
22545cf6c33SJiri Olsa void perf_event_attr__set_max_precise_ip(struct perf_event_attr *attr)
2267f8d1adeSArnaldo Carvalho de Melo {
2277f8d1adeSArnaldo Carvalho de Melo 	attr->precise_ip = 3;
2287f8d1adeSArnaldo Carvalho de Melo 
2297f8d1adeSArnaldo Carvalho de Melo 	while (attr->precise_ip != 0) {
2307f8d1adeSArnaldo Carvalho de Melo 		int fd = sys_perf_event_open(attr, 0, -1, -1, 0);
2317f8d1adeSArnaldo Carvalho de Melo 		if (fd != -1) {
2327f8d1adeSArnaldo Carvalho de Melo 			close(fd);
2337f8d1adeSArnaldo Carvalho de Melo 			break;
2347f8d1adeSArnaldo Carvalho de Melo 		}
2357f8d1adeSArnaldo Carvalho de Melo 		--attr->precise_ip;
2367f8d1adeSArnaldo Carvalho de Melo 	}
2377f8d1adeSArnaldo Carvalho de Melo }
2387f8d1adeSArnaldo Carvalho de Melo 
239361c99a6SArnaldo Carvalho de Melo int perf_evlist__add_default(struct perf_evlist *evlist)
240361c99a6SArnaldo Carvalho de Melo {
241361c99a6SArnaldo Carvalho de Melo 	struct perf_event_attr attr = {
242361c99a6SArnaldo Carvalho de Melo 		.type = PERF_TYPE_HARDWARE,
243361c99a6SArnaldo Carvalho de Melo 		.config = PERF_COUNT_HW_CPU_CYCLES,
244361c99a6SArnaldo Carvalho de Melo 	};
2451aed2671SJoerg Roedel 	struct perf_evsel *evsel;
246361c99a6SArnaldo Carvalho de Melo 
2471aed2671SJoerg Roedel 	event_attr_init(&attr);
2481aed2671SJoerg Roedel 
2497f8d1adeSArnaldo Carvalho de Melo 	perf_event_attr__set_max_precise_ip(&attr);
2507f8d1adeSArnaldo Carvalho de Melo 
251ef503831SArnaldo Carvalho de Melo 	evsel = perf_evsel__new(&attr);
252361c99a6SArnaldo Carvalho de Melo 	if (evsel == NULL)
253cc2d86b0SStephane Eranian 		goto error;
254cc2d86b0SStephane Eranian 
2557f8d1adeSArnaldo Carvalho de Melo 	/* use asprintf() because free(evsel) assumes name is allocated */
2567f8d1adeSArnaldo Carvalho de Melo 	if (asprintf(&evsel->name, "cycles%.*s",
2577f8d1adeSArnaldo Carvalho de Melo 		     attr.precise_ip ? attr.precise_ip + 1 : 0, ":ppp") < 0)
258cc2d86b0SStephane Eranian 		goto error_free;
259361c99a6SArnaldo Carvalho de Melo 
260361c99a6SArnaldo Carvalho de Melo 	perf_evlist__add(evlist, evsel);
261361c99a6SArnaldo Carvalho de Melo 	return 0;
262cc2d86b0SStephane Eranian error_free:
263cc2d86b0SStephane Eranian 	perf_evsel__delete(evsel);
264cc2d86b0SStephane Eranian error:
265cc2d86b0SStephane Eranian 	return -ENOMEM;
266361c99a6SArnaldo Carvalho de Melo }
2675c581041SArnaldo Carvalho de Melo 
2685bae0250SArnaldo Carvalho de Melo int perf_evlist__add_dummy(struct perf_evlist *evlist)
2695bae0250SArnaldo Carvalho de Melo {
2705bae0250SArnaldo Carvalho de Melo 	struct perf_event_attr attr = {
2715bae0250SArnaldo Carvalho de Melo 		.type	= PERF_TYPE_SOFTWARE,
2725bae0250SArnaldo Carvalho de Melo 		.config = PERF_COUNT_SW_DUMMY,
2735bae0250SArnaldo Carvalho de Melo 		.size	= sizeof(attr), /* to capture ABI version */
2745bae0250SArnaldo Carvalho de Melo 	};
2755bae0250SArnaldo Carvalho de Melo 	struct perf_evsel *evsel = perf_evsel__new(&attr);
2765bae0250SArnaldo Carvalho de Melo 
2775bae0250SArnaldo Carvalho de Melo 	if (evsel == NULL)
2785bae0250SArnaldo Carvalho de Melo 		return -ENOMEM;
2795bae0250SArnaldo Carvalho de Melo 
2805bae0250SArnaldo Carvalho de Melo 	perf_evlist__add(evlist, evsel);
2815bae0250SArnaldo Carvalho de Melo 	return 0;
2825bae0250SArnaldo Carvalho de Melo }
2835bae0250SArnaldo Carvalho de Melo 
284e60fc847SArnaldo Carvalho de Melo static int perf_evlist__add_attrs(struct perf_evlist *evlist,
28550d08e47SArnaldo Carvalho de Melo 				  struct perf_event_attr *attrs, size_t nr_attrs)
28650d08e47SArnaldo Carvalho de Melo {
28750d08e47SArnaldo Carvalho de Melo 	struct perf_evsel *evsel, *n;
28850d08e47SArnaldo Carvalho de Melo 	LIST_HEAD(head);
28950d08e47SArnaldo Carvalho de Melo 	size_t i;
29050d08e47SArnaldo Carvalho de Melo 
29150d08e47SArnaldo Carvalho de Melo 	for (i = 0; i < nr_attrs; i++) {
292ef503831SArnaldo Carvalho de Melo 		evsel = perf_evsel__new_idx(attrs + i, evlist->nr_entries + i);
29350d08e47SArnaldo Carvalho de Melo 		if (evsel == NULL)
29450d08e47SArnaldo Carvalho de Melo 			goto out_delete_partial_list;
29550d08e47SArnaldo Carvalho de Melo 		list_add_tail(&evsel->node, &head);
29650d08e47SArnaldo Carvalho de Melo 	}
29750d08e47SArnaldo Carvalho de Melo 
298f114d6efSAdrian Hunter 	perf_evlist__splice_list_tail(evlist, &head);
29950d08e47SArnaldo Carvalho de Melo 
30050d08e47SArnaldo Carvalho de Melo 	return 0;
30150d08e47SArnaldo Carvalho de Melo 
30250d08e47SArnaldo Carvalho de Melo out_delete_partial_list:
303e5cadb93SArnaldo Carvalho de Melo 	__evlist__for_each_entry_safe(&head, n, evsel)
30450d08e47SArnaldo Carvalho de Melo 		perf_evsel__delete(evsel);
30550d08e47SArnaldo Carvalho de Melo 	return -1;
30650d08e47SArnaldo Carvalho de Melo }
30750d08e47SArnaldo Carvalho de Melo 
30879695e1bSArnaldo Carvalho de Melo int __perf_evlist__add_default_attrs(struct perf_evlist *evlist,
30979695e1bSArnaldo Carvalho de Melo 				     struct perf_event_attr *attrs, size_t nr_attrs)
31079695e1bSArnaldo Carvalho de Melo {
31179695e1bSArnaldo Carvalho de Melo 	size_t i;
31279695e1bSArnaldo Carvalho de Melo 
31379695e1bSArnaldo Carvalho de Melo 	for (i = 0; i < nr_attrs; i++)
31479695e1bSArnaldo Carvalho de Melo 		event_attr_init(attrs + i);
31579695e1bSArnaldo Carvalho de Melo 
31679695e1bSArnaldo Carvalho de Melo 	return perf_evlist__add_attrs(evlist, attrs, nr_attrs);
31779695e1bSArnaldo Carvalho de Melo }
31879695e1bSArnaldo Carvalho de Melo 
319da378962SArnaldo Carvalho de Melo struct perf_evsel *
320ee29be62SArnaldo Carvalho de Melo perf_evlist__find_tracepoint_by_id(struct perf_evlist *evlist, int id)
321ee29be62SArnaldo Carvalho de Melo {
322ee29be62SArnaldo Carvalho de Melo 	struct perf_evsel *evsel;
323ee29be62SArnaldo Carvalho de Melo 
324e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, evsel) {
325ee29be62SArnaldo Carvalho de Melo 		if (evsel->attr.type   == PERF_TYPE_TRACEPOINT &&
326ee29be62SArnaldo Carvalho de Melo 		    (int)evsel->attr.config == id)
327ee29be62SArnaldo Carvalho de Melo 			return evsel;
328ee29be62SArnaldo Carvalho de Melo 	}
329ee29be62SArnaldo Carvalho de Melo 
330ee29be62SArnaldo Carvalho de Melo 	return NULL;
331ee29be62SArnaldo Carvalho de Melo }
332ee29be62SArnaldo Carvalho de Melo 
333a2f2804aSDavid Ahern struct perf_evsel *
334a2f2804aSDavid Ahern perf_evlist__find_tracepoint_by_name(struct perf_evlist *evlist,
335a2f2804aSDavid Ahern 				     const char *name)
336a2f2804aSDavid Ahern {
337a2f2804aSDavid Ahern 	struct perf_evsel *evsel;
338a2f2804aSDavid Ahern 
339e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, evsel) {
340a2f2804aSDavid Ahern 		if ((evsel->attr.type == PERF_TYPE_TRACEPOINT) &&
341a2f2804aSDavid Ahern 		    (strcmp(evsel->name, name) == 0))
342a2f2804aSDavid Ahern 			return evsel;
343a2f2804aSDavid Ahern 	}
344a2f2804aSDavid Ahern 
345a2f2804aSDavid Ahern 	return NULL;
346a2f2804aSDavid Ahern }
347a2f2804aSDavid Ahern 
34839876e7dSArnaldo Carvalho de Melo int perf_evlist__add_newtp(struct perf_evlist *evlist,
34939876e7dSArnaldo Carvalho de Melo 			   const char *sys, const char *name, void *handler)
35039876e7dSArnaldo Carvalho de Melo {
351ef503831SArnaldo Carvalho de Melo 	struct perf_evsel *evsel = perf_evsel__newtp(sys, name);
35239876e7dSArnaldo Carvalho de Melo 
3538dd2a131SJiri Olsa 	if (IS_ERR(evsel))
35439876e7dSArnaldo Carvalho de Melo 		return -1;
35539876e7dSArnaldo Carvalho de Melo 
356744a9719SArnaldo Carvalho de Melo 	evsel->handler = handler;
35739876e7dSArnaldo Carvalho de Melo 	perf_evlist__add(evlist, evsel);
35839876e7dSArnaldo Carvalho de Melo 	return 0;
35939876e7dSArnaldo Carvalho de Melo }
36039876e7dSArnaldo Carvalho de Melo 
361bf8e8f4bSAdrian Hunter static int perf_evlist__nr_threads(struct perf_evlist *evlist,
362bf8e8f4bSAdrian Hunter 				   struct perf_evsel *evsel)
363bf8e8f4bSAdrian Hunter {
364bf8e8f4bSAdrian Hunter 	if (evsel->system_wide)
365bf8e8f4bSAdrian Hunter 		return 1;
366bf8e8f4bSAdrian Hunter 	else
367bf8e8f4bSAdrian Hunter 		return thread_map__nr(evlist->threads);
368bf8e8f4bSAdrian Hunter }
369bf8e8f4bSAdrian Hunter 
3704152ab37SArnaldo Carvalho de Melo void perf_evlist__disable(struct perf_evlist *evlist)
3714152ab37SArnaldo Carvalho de Melo {
3724152ab37SArnaldo Carvalho de Melo 	struct perf_evsel *pos;
3734152ab37SArnaldo Carvalho de Melo 
374e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, pos) {
375395c3070SAdrian Hunter 		if (!perf_evsel__is_group_leader(pos) || !pos->fd)
3763fe4430dSJiri Olsa 			continue;
3773e27c920SJiri Olsa 		perf_evsel__disable(pos);
3784152ab37SArnaldo Carvalho de Melo 	}
3792b56bcfbSArnaldo Carvalho de Melo 
3802b56bcfbSArnaldo Carvalho de Melo 	evlist->enabled = false;
3814152ab37SArnaldo Carvalho de Melo }
3824152ab37SArnaldo Carvalho de Melo 
383764e16a3SDavid Ahern void perf_evlist__enable(struct perf_evlist *evlist)
384764e16a3SDavid Ahern {
385764e16a3SDavid Ahern 	struct perf_evsel *pos;
386764e16a3SDavid Ahern 
387e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, pos) {
388395c3070SAdrian Hunter 		if (!perf_evsel__is_group_leader(pos) || !pos->fd)
3893fe4430dSJiri Olsa 			continue;
3903e27c920SJiri Olsa 		perf_evsel__enable(pos);
391764e16a3SDavid Ahern 	}
3922b56bcfbSArnaldo Carvalho de Melo 
3932b56bcfbSArnaldo Carvalho de Melo 	evlist->enabled = true;
3942b56bcfbSArnaldo Carvalho de Melo }
3952b56bcfbSArnaldo Carvalho de Melo 
3962b56bcfbSArnaldo Carvalho de Melo void perf_evlist__toggle_enable(struct perf_evlist *evlist)
3972b56bcfbSArnaldo Carvalho de Melo {
3982b56bcfbSArnaldo Carvalho de Melo 	(evlist->enabled ? perf_evlist__disable : perf_evlist__enable)(evlist);
399764e16a3SDavid Ahern }
400764e16a3SDavid Ahern 
4011c65056cSAdrian Hunter static int perf_evlist__enable_event_cpu(struct perf_evlist *evlist,
4021c65056cSAdrian Hunter 					 struct perf_evsel *evsel, int cpu)
4031c65056cSAdrian Hunter {
4041c65056cSAdrian Hunter 	int thread, err;
4051c65056cSAdrian Hunter 	int nr_threads = perf_evlist__nr_threads(evlist, evsel);
4061c65056cSAdrian Hunter 
4071c65056cSAdrian Hunter 	if (!evsel->fd)
4081c65056cSAdrian Hunter 		return -EINVAL;
4091c65056cSAdrian Hunter 
4101c65056cSAdrian Hunter 	for (thread = 0; thread < nr_threads; thread++) {
4111c65056cSAdrian Hunter 		err = ioctl(FD(evsel, cpu, thread),
4121c65056cSAdrian Hunter 			    PERF_EVENT_IOC_ENABLE, 0);
4131c65056cSAdrian Hunter 		if (err)
4141c65056cSAdrian Hunter 			return err;
4151c65056cSAdrian Hunter 	}
4161c65056cSAdrian Hunter 	return 0;
4171c65056cSAdrian Hunter }
4181c65056cSAdrian Hunter 
4191c65056cSAdrian Hunter static int perf_evlist__enable_event_thread(struct perf_evlist *evlist,
4201c65056cSAdrian Hunter 					    struct perf_evsel *evsel,
4211c65056cSAdrian Hunter 					    int thread)
4221c65056cSAdrian Hunter {
4231c65056cSAdrian Hunter 	int cpu, err;
4241c65056cSAdrian Hunter 	int nr_cpus = cpu_map__nr(evlist->cpus);
4251c65056cSAdrian Hunter 
4261c65056cSAdrian Hunter 	if (!evsel->fd)
4271c65056cSAdrian Hunter 		return -EINVAL;
4281c65056cSAdrian Hunter 
4291c65056cSAdrian Hunter 	for (cpu = 0; cpu < nr_cpus; cpu++) {
4301c65056cSAdrian Hunter 		err = ioctl(FD(evsel, cpu, thread), PERF_EVENT_IOC_ENABLE, 0);
4311c65056cSAdrian Hunter 		if (err)
4321c65056cSAdrian Hunter 			return err;
4331c65056cSAdrian Hunter 	}
4341c65056cSAdrian Hunter 	return 0;
4351c65056cSAdrian Hunter }
4361c65056cSAdrian Hunter 
4371c65056cSAdrian Hunter int perf_evlist__enable_event_idx(struct perf_evlist *evlist,
4381c65056cSAdrian Hunter 				  struct perf_evsel *evsel, int idx)
4391c65056cSAdrian Hunter {
4401c65056cSAdrian Hunter 	bool per_cpu_mmaps = !cpu_map__empty(evlist->cpus);
4411c65056cSAdrian Hunter 
4421c65056cSAdrian Hunter 	if (per_cpu_mmaps)
4431c65056cSAdrian Hunter 		return perf_evlist__enable_event_cpu(evlist, evsel, idx);
4441c65056cSAdrian Hunter 	else
4451c65056cSAdrian Hunter 		return perf_evlist__enable_event_thread(evlist, evsel, idx);
4461c65056cSAdrian Hunter }
4471c65056cSAdrian Hunter 
448ad6765ddSArnaldo Carvalho de Melo int perf_evlist__alloc_pollfd(struct perf_evlist *evlist)
4495c581041SArnaldo Carvalho de Melo {
450b3a319d5SNamhyung Kim 	int nr_cpus = cpu_map__nr(evlist->cpus);
451b3a319d5SNamhyung Kim 	int nr_threads = thread_map__nr(evlist->threads);
452bf8e8f4bSAdrian Hunter 	int nfds = 0;
453bf8e8f4bSAdrian Hunter 	struct perf_evsel *evsel;
454bf8e8f4bSAdrian Hunter 
455e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, evsel) {
456bf8e8f4bSAdrian Hunter 		if (evsel->system_wide)
457bf8e8f4bSAdrian Hunter 			nfds += nr_cpus;
458bf8e8f4bSAdrian Hunter 		else
459bf8e8f4bSAdrian Hunter 			nfds += nr_cpus * nr_threads;
460bf8e8f4bSAdrian Hunter 	}
461bf8e8f4bSAdrian Hunter 
4621b85337dSArnaldo Carvalho de Melo 	if (fdarray__available_entries(&evlist->pollfd) < nfds &&
4631b85337dSArnaldo Carvalho de Melo 	    fdarray__grow(&evlist->pollfd, nfds) < 0)
464ad6765ddSArnaldo Carvalho de Melo 		return -ENOMEM;
465ad6765ddSArnaldo Carvalho de Melo 
466ad6765ddSArnaldo Carvalho de Melo 	return 0;
4675c581041SArnaldo Carvalho de Melo }
46870082dd9SArnaldo Carvalho de Melo 
469f3058a1cSWang Nan static int __perf_evlist__add_pollfd(struct perf_evlist *evlist, int fd, int idx, short revent)
470e4b356b5SArnaldo Carvalho de Melo {
471f3058a1cSWang Nan 	int pos = fdarray__add(&evlist->pollfd, fd, revent | POLLERR | POLLHUP);
472e4b356b5SArnaldo Carvalho de Melo 	/*
473e4b356b5SArnaldo Carvalho de Melo 	 * Save the idx so that when we filter out fds POLLHUP'ed we can
474e4b356b5SArnaldo Carvalho de Melo 	 * close the associated evlist->mmap[] entry.
475e4b356b5SArnaldo Carvalho de Melo 	 */
476e4b356b5SArnaldo Carvalho de Melo 	if (pos >= 0) {
477e4b356b5SArnaldo Carvalho de Melo 		evlist->pollfd.priv[pos].idx = idx;
478e4b356b5SArnaldo Carvalho de Melo 
479e4b356b5SArnaldo Carvalho de Melo 		fcntl(fd, F_SETFL, O_NONBLOCK);
480e4b356b5SArnaldo Carvalho de Melo 	}
481e4b356b5SArnaldo Carvalho de Melo 
482e4b356b5SArnaldo Carvalho de Melo 	return pos;
483e4b356b5SArnaldo Carvalho de Melo }
484e4b356b5SArnaldo Carvalho de Melo 
485ad6765ddSArnaldo Carvalho de Melo int perf_evlist__add_pollfd(struct perf_evlist *evlist, int fd)
48670082dd9SArnaldo Carvalho de Melo {
487f3058a1cSWang Nan 	return __perf_evlist__add_pollfd(evlist, fd, -1, POLLIN);
488e4b356b5SArnaldo Carvalho de Melo }
4891b85337dSArnaldo Carvalho de Melo 
490258e4bfcSWang Nan static void perf_evlist__munmap_filtered(struct fdarray *fda, int fd,
491258e4bfcSWang Nan 					 void *arg __maybe_unused)
492e4b356b5SArnaldo Carvalho de Melo {
493e4b356b5SArnaldo Carvalho de Melo 	struct perf_evlist *evlist = container_of(fda, struct perf_evlist, pollfd);
494e4b356b5SArnaldo Carvalho de Melo 
495e4b356b5SArnaldo Carvalho de Melo 	perf_evlist__mmap_put(evlist, fda->priv[fd].idx);
49670082dd9SArnaldo Carvalho de Melo }
49770db7533SArnaldo Carvalho de Melo 
4981ddec7f0SArnaldo Carvalho de Melo int perf_evlist__filter_pollfd(struct perf_evlist *evlist, short revents_and_mask)
4991ddec7f0SArnaldo Carvalho de Melo {
500e4b356b5SArnaldo Carvalho de Melo 	return fdarray__filter(&evlist->pollfd, revents_and_mask,
501258e4bfcSWang Nan 			       perf_evlist__munmap_filtered, NULL);
5021ddec7f0SArnaldo Carvalho de Melo }
5031ddec7f0SArnaldo Carvalho de Melo 
504f66a889dSArnaldo Carvalho de Melo int perf_evlist__poll(struct perf_evlist *evlist, int timeout)
505f66a889dSArnaldo Carvalho de Melo {
5061b85337dSArnaldo Carvalho de Melo 	return fdarray__poll(&evlist->pollfd, timeout);
507f66a889dSArnaldo Carvalho de Melo }
508f66a889dSArnaldo Carvalho de Melo 
509a91e5431SArnaldo Carvalho de Melo static void perf_evlist__id_hash(struct perf_evlist *evlist,
510a91e5431SArnaldo Carvalho de Melo 				 struct perf_evsel *evsel,
5113d3b5e95SArnaldo Carvalho de Melo 				 int cpu, int thread, u64 id)
5123d3b5e95SArnaldo Carvalho de Melo {
5133d3b5e95SArnaldo Carvalho de Melo 	int hash;
5143d3b5e95SArnaldo Carvalho de Melo 	struct perf_sample_id *sid = SID(evsel, cpu, thread);
5153d3b5e95SArnaldo Carvalho de Melo 
5163d3b5e95SArnaldo Carvalho de Melo 	sid->id = id;
5173d3b5e95SArnaldo Carvalho de Melo 	sid->evsel = evsel;
5183d3b5e95SArnaldo Carvalho de Melo 	hash = hash_64(sid->id, PERF_EVLIST__HLIST_BITS);
5193d3b5e95SArnaldo Carvalho de Melo 	hlist_add_head(&sid->node, &evlist->heads[hash]);
5203d3b5e95SArnaldo Carvalho de Melo }
5213d3b5e95SArnaldo Carvalho de Melo 
522a91e5431SArnaldo Carvalho de Melo void perf_evlist__id_add(struct perf_evlist *evlist, struct perf_evsel *evsel,
523a91e5431SArnaldo Carvalho de Melo 			 int cpu, int thread, u64 id)
524a91e5431SArnaldo Carvalho de Melo {
525a91e5431SArnaldo Carvalho de Melo 	perf_evlist__id_hash(evlist, evsel, cpu, thread, id);
526a91e5431SArnaldo Carvalho de Melo 	evsel->id[evsel->ids++] = id;
527a91e5431SArnaldo Carvalho de Melo }
528a91e5431SArnaldo Carvalho de Melo 
5291c59612dSJiri Olsa int perf_evlist__id_add_fd(struct perf_evlist *evlist,
5303d3b5e95SArnaldo Carvalho de Melo 			   struct perf_evsel *evsel,
531f8a95309SArnaldo Carvalho de Melo 			   int cpu, int thread, int fd)
532f8a95309SArnaldo Carvalho de Melo {
533f8a95309SArnaldo Carvalho de Melo 	u64 read_data[4] = { 0, };
5343d3b5e95SArnaldo Carvalho de Melo 	int id_idx = 1; /* The first entry is the counter value */
535e2b5abe0SJiri Olsa 	u64 id;
536e2b5abe0SJiri Olsa 	int ret;
537e2b5abe0SJiri Olsa 
538e2b5abe0SJiri Olsa 	ret = ioctl(fd, PERF_EVENT_IOC_ID, &id);
539e2b5abe0SJiri Olsa 	if (!ret)
540e2b5abe0SJiri Olsa 		goto add;
541e2b5abe0SJiri Olsa 
542e2b5abe0SJiri Olsa 	if (errno != ENOTTY)
543e2b5abe0SJiri Olsa 		return -1;
544e2b5abe0SJiri Olsa 
545e2b5abe0SJiri Olsa 	/* Legacy way to get event id.. All hail to old kernels! */
546f8a95309SArnaldo Carvalho de Melo 
547c4861afeSJiri Olsa 	/*
548c4861afeSJiri Olsa 	 * This way does not work with group format read, so bail
549c4861afeSJiri Olsa 	 * out in that case.
550c4861afeSJiri Olsa 	 */
551c4861afeSJiri Olsa 	if (perf_evlist__read_format(evlist) & PERF_FORMAT_GROUP)
552c4861afeSJiri Olsa 		return -1;
553c4861afeSJiri Olsa 
554f8a95309SArnaldo Carvalho de Melo 	if (!(evsel->attr.read_format & PERF_FORMAT_ID) ||
555f8a95309SArnaldo Carvalho de Melo 	    read(fd, &read_data, sizeof(read_data)) == -1)
556f8a95309SArnaldo Carvalho de Melo 		return -1;
557f8a95309SArnaldo Carvalho de Melo 
558f8a95309SArnaldo Carvalho de Melo 	if (evsel->attr.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
559f8a95309SArnaldo Carvalho de Melo 		++id_idx;
560f8a95309SArnaldo Carvalho de Melo 	if (evsel->attr.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
561f8a95309SArnaldo Carvalho de Melo 		++id_idx;
562f8a95309SArnaldo Carvalho de Melo 
563e2b5abe0SJiri Olsa 	id = read_data[id_idx];
564e2b5abe0SJiri Olsa 
565e2b5abe0SJiri Olsa  add:
566e2b5abe0SJiri Olsa 	perf_evlist__id_add(evlist, evsel, cpu, thread, id);
567f8a95309SArnaldo Carvalho de Melo 	return 0;
568f8a95309SArnaldo Carvalho de Melo }
569f8a95309SArnaldo Carvalho de Melo 
5703c659eedSAdrian Hunter static void perf_evlist__set_sid_idx(struct perf_evlist *evlist,
5713c659eedSAdrian Hunter 				     struct perf_evsel *evsel, int idx, int cpu,
5723c659eedSAdrian Hunter 				     int thread)
5733c659eedSAdrian Hunter {
5743c659eedSAdrian Hunter 	struct perf_sample_id *sid = SID(evsel, cpu, thread);
5753c659eedSAdrian Hunter 	sid->idx = idx;
5763c659eedSAdrian Hunter 	if (evlist->cpus && cpu >= 0)
5773c659eedSAdrian Hunter 		sid->cpu = evlist->cpus->map[cpu];
5783c659eedSAdrian Hunter 	else
5793c659eedSAdrian Hunter 		sid->cpu = -1;
5803c659eedSAdrian Hunter 	if (!evsel->system_wide && evlist->threads && thread >= 0)
581e13798c7SJiri Olsa 		sid->tid = thread_map__pid(evlist->threads, thread);
5823c659eedSAdrian Hunter 	else
5833c659eedSAdrian Hunter 		sid->tid = -1;
5843c659eedSAdrian Hunter }
5853c659eedSAdrian Hunter 
586932a3594SJiri Olsa struct perf_sample_id *perf_evlist__id2sid(struct perf_evlist *evlist, u64 id)
58770db7533SArnaldo Carvalho de Melo {
58870db7533SArnaldo Carvalho de Melo 	struct hlist_head *head;
58970db7533SArnaldo Carvalho de Melo 	struct perf_sample_id *sid;
59070db7533SArnaldo Carvalho de Melo 	int hash;
59170db7533SArnaldo Carvalho de Melo 
59270db7533SArnaldo Carvalho de Melo 	hash = hash_64(id, PERF_EVLIST__HLIST_BITS);
59370db7533SArnaldo Carvalho de Melo 	head = &evlist->heads[hash];
59470db7533SArnaldo Carvalho de Melo 
595b67bfe0dSSasha Levin 	hlist_for_each_entry(sid, head, node)
59670db7533SArnaldo Carvalho de Melo 		if (sid->id == id)
597932a3594SJiri Olsa 			return sid;
598932a3594SJiri Olsa 
599932a3594SJiri Olsa 	return NULL;
600932a3594SJiri Olsa }
601932a3594SJiri Olsa 
602932a3594SJiri Olsa struct perf_evsel *perf_evlist__id2evsel(struct perf_evlist *evlist, u64 id)
603932a3594SJiri Olsa {
604932a3594SJiri Olsa 	struct perf_sample_id *sid;
605932a3594SJiri Olsa 
60605169df5SAdrian Hunter 	if (evlist->nr_entries == 1 || !id)
607932a3594SJiri Olsa 		return perf_evlist__first(evlist);
608932a3594SJiri Olsa 
609932a3594SJiri Olsa 	sid = perf_evlist__id2sid(evlist, id);
610932a3594SJiri Olsa 	if (sid)
61170db7533SArnaldo Carvalho de Melo 		return sid->evsel;
61230e68bccSNamhyung Kim 
61330e68bccSNamhyung Kim 	if (!perf_evlist__sample_id_all(evlist))
6140c21f736SArnaldo Carvalho de Melo 		return perf_evlist__first(evlist);
61530e68bccSNamhyung Kim 
61670db7533SArnaldo Carvalho de Melo 	return NULL;
61770db7533SArnaldo Carvalho de Melo }
61804391debSArnaldo Carvalho de Melo 
619dddcf6abSAdrian Hunter struct perf_evsel *perf_evlist__id2evsel_strict(struct perf_evlist *evlist,
620dddcf6abSAdrian Hunter 						u64 id)
621dddcf6abSAdrian Hunter {
622dddcf6abSAdrian Hunter 	struct perf_sample_id *sid;
623dddcf6abSAdrian Hunter 
624dddcf6abSAdrian Hunter 	if (!id)
625dddcf6abSAdrian Hunter 		return NULL;
626dddcf6abSAdrian Hunter 
627dddcf6abSAdrian Hunter 	sid = perf_evlist__id2sid(evlist, id);
628dddcf6abSAdrian Hunter 	if (sid)
629dddcf6abSAdrian Hunter 		return sid->evsel;
630dddcf6abSAdrian Hunter 
631dddcf6abSAdrian Hunter 	return NULL;
632dddcf6abSAdrian Hunter }
633dddcf6abSAdrian Hunter 
63475562573SAdrian Hunter static int perf_evlist__event2id(struct perf_evlist *evlist,
63575562573SAdrian Hunter 				 union perf_event *event, u64 *id)
63675562573SAdrian Hunter {
63775562573SAdrian Hunter 	const u64 *array = event->sample.array;
63875562573SAdrian Hunter 	ssize_t n;
63975562573SAdrian Hunter 
64075562573SAdrian Hunter 	n = (event->header.size - sizeof(event->header)) >> 3;
64175562573SAdrian Hunter 
64275562573SAdrian Hunter 	if (event->header.type == PERF_RECORD_SAMPLE) {
64375562573SAdrian Hunter 		if (evlist->id_pos >= n)
64475562573SAdrian Hunter 			return -1;
64575562573SAdrian Hunter 		*id = array[evlist->id_pos];
64675562573SAdrian Hunter 	} else {
64775562573SAdrian Hunter 		if (evlist->is_pos > n)
64875562573SAdrian Hunter 			return -1;
64975562573SAdrian Hunter 		n -= evlist->is_pos;
65075562573SAdrian Hunter 		*id = array[n];
65175562573SAdrian Hunter 	}
65275562573SAdrian Hunter 	return 0;
65375562573SAdrian Hunter }
65475562573SAdrian Hunter 
6557cb5c5acSJiri Olsa struct perf_evsel *perf_evlist__event2evsel(struct perf_evlist *evlist,
65675562573SAdrian Hunter 					    union perf_event *event)
65775562573SAdrian Hunter {
65898be6966SAdrian Hunter 	struct perf_evsel *first = perf_evlist__first(evlist);
65975562573SAdrian Hunter 	struct hlist_head *head;
66075562573SAdrian Hunter 	struct perf_sample_id *sid;
66175562573SAdrian Hunter 	int hash;
66275562573SAdrian Hunter 	u64 id;
66375562573SAdrian Hunter 
66475562573SAdrian Hunter 	if (evlist->nr_entries == 1)
66598be6966SAdrian Hunter 		return first;
66698be6966SAdrian Hunter 
66798be6966SAdrian Hunter 	if (!first->attr.sample_id_all &&
66898be6966SAdrian Hunter 	    event->header.type != PERF_RECORD_SAMPLE)
66998be6966SAdrian Hunter 		return first;
67075562573SAdrian Hunter 
67175562573SAdrian Hunter 	if (perf_evlist__event2id(evlist, event, &id))
67275562573SAdrian Hunter 		return NULL;
67375562573SAdrian Hunter 
67475562573SAdrian Hunter 	/* Synthesized events have an id of zero */
67575562573SAdrian Hunter 	if (!id)
67698be6966SAdrian Hunter 		return first;
67775562573SAdrian Hunter 
67875562573SAdrian Hunter 	hash = hash_64(id, PERF_EVLIST__HLIST_BITS);
67975562573SAdrian Hunter 	head = &evlist->heads[hash];
68075562573SAdrian Hunter 
68175562573SAdrian Hunter 	hlist_for_each_entry(sid, head, node) {
68275562573SAdrian Hunter 		if (sid->id == id)
68375562573SAdrian Hunter 			return sid->evsel;
68475562573SAdrian Hunter 	}
68575562573SAdrian Hunter 	return NULL;
68675562573SAdrian Hunter }
68775562573SAdrian Hunter 
68865aea233SWang Nan static int perf_evlist__set_paused(struct perf_evlist *evlist, bool value)
68965aea233SWang Nan {
69065aea233SWang Nan 	int i;
69165aea233SWang Nan 
69265aea233SWang Nan 	for (i = 0; i < evlist->nr_mmaps; i++) {
69365aea233SWang Nan 		int fd = evlist->mmap[i].fd;
69465aea233SWang Nan 		int err;
69565aea233SWang Nan 
69665aea233SWang Nan 		if (fd < 0)
69765aea233SWang Nan 			continue;
69865aea233SWang Nan 		err = ioctl(fd, PERF_EVENT_IOC_PAUSE_OUTPUT, value ? 1 : 0);
69965aea233SWang Nan 		if (err)
70065aea233SWang Nan 			return err;
70165aea233SWang Nan 	}
70265aea233SWang Nan 	return 0;
70365aea233SWang Nan }
70465aea233SWang Nan 
70565aea233SWang Nan int perf_evlist__pause(struct perf_evlist *evlist)
70665aea233SWang Nan {
70765aea233SWang Nan 	return perf_evlist__set_paused(evlist, true);
70865aea233SWang Nan }
70965aea233SWang Nan 
71065aea233SWang Nan int perf_evlist__resume(struct perf_evlist *evlist)
71165aea233SWang Nan {
71265aea233SWang Nan 	return perf_evlist__set_paused(evlist, false);
71365aea233SWang Nan }
71465aea233SWang Nan 
715b6b85dadSWang Nan /* When check_messup is true, 'end' must points to a good entry */
7160f4ccd11SWang Nan static union perf_event *
717b6b85dadSWang Nan perf_mmap__read(struct perf_mmap *md, bool check_messup, u64 start,
718b6b85dadSWang Nan 		u64 end, u64 *prev)
71904391debSArnaldo Carvalho de Melo {
72004391debSArnaldo Carvalho de Melo 	unsigned char *data = md->base + page_size;
7218115d60cSArnaldo Carvalho de Melo 	union perf_event *event = NULL;
722b6b85dadSWang Nan 	int diff = end - start;
72304391debSArnaldo Carvalho de Melo 
724b6b85dadSWang Nan 	if (check_messup) {
72504391debSArnaldo Carvalho de Melo 		/*
72604391debSArnaldo Carvalho de Melo 		 * If we're further behind than half the buffer, there's a chance
72704391debSArnaldo Carvalho de Melo 		 * the writer will bite our tail and mess up the samples under us.
72804391debSArnaldo Carvalho de Melo 		 *
729b6b85dadSWang Nan 		 * If we somehow ended up ahead of the 'end', we got messed up.
73004391debSArnaldo Carvalho de Melo 		 *
731b6b85dadSWang Nan 		 * In either case, truncate and restart at 'end'.
73204391debSArnaldo Carvalho de Melo 		 */
73304391debSArnaldo Carvalho de Melo 		if (diff > md->mask / 2 || diff < 0) {
73404391debSArnaldo Carvalho de Melo 			fprintf(stderr, "WARNING: failed to keep up with mmap data.\n");
73504391debSArnaldo Carvalho de Melo 
73604391debSArnaldo Carvalho de Melo 			/*
737b6b85dadSWang Nan 			 * 'end' points to a known good entry, start there.
73804391debSArnaldo Carvalho de Melo 			 */
739b6b85dadSWang Nan 			start = end;
740b04b7023SWang Nan 			diff = 0;
74104391debSArnaldo Carvalho de Melo 		}
7427bb41152SArnaldo Carvalho de Melo 	}
74304391debSArnaldo Carvalho de Melo 
744b04b7023SWang Nan 	if (diff >= (int)sizeof(event->header)) {
74504391debSArnaldo Carvalho de Melo 		size_t size;
74604391debSArnaldo Carvalho de Melo 
747b6b85dadSWang Nan 		event = (union perf_event *)&data[start & md->mask];
74804391debSArnaldo Carvalho de Melo 		size = event->header.size;
74904391debSArnaldo Carvalho de Melo 
750b04b7023SWang Nan 		if (size < sizeof(event->header) || diff < (int)size) {
751b04b7023SWang Nan 			event = NULL;
752b04b7023SWang Nan 			goto broken_event;
753b04b7023SWang Nan 		}
754b04b7023SWang Nan 
75504391debSArnaldo Carvalho de Melo 		/*
75604391debSArnaldo Carvalho de Melo 		 * Event straddles the mmap boundary -- header should always
75704391debSArnaldo Carvalho de Melo 		 * be inside due to u64 alignment of output.
75804391debSArnaldo Carvalho de Melo 		 */
759b6b85dadSWang Nan 		if ((start & md->mask) + size != ((start + size) & md->mask)) {
760b6b85dadSWang Nan 			unsigned int offset = start;
76104391debSArnaldo Carvalho de Melo 			unsigned int len = min(sizeof(*event), size), cpy;
762a65cb4b9SJiri Olsa 			void *dst = md->event_copy;
76304391debSArnaldo Carvalho de Melo 
76404391debSArnaldo Carvalho de Melo 			do {
76504391debSArnaldo Carvalho de Melo 				cpy = min(md->mask + 1 - (offset & md->mask), len);
76604391debSArnaldo Carvalho de Melo 				memcpy(dst, &data[offset & md->mask], cpy);
76704391debSArnaldo Carvalho de Melo 				offset += cpy;
76804391debSArnaldo Carvalho de Melo 				dst += cpy;
76904391debSArnaldo Carvalho de Melo 				len -= cpy;
77004391debSArnaldo Carvalho de Melo 			} while (len);
77104391debSArnaldo Carvalho de Melo 
772a65cb4b9SJiri Olsa 			event = (union perf_event *) md->event_copy;
77304391debSArnaldo Carvalho de Melo 		}
77404391debSArnaldo Carvalho de Melo 
775b6b85dadSWang Nan 		start += size;
77604391debSArnaldo Carvalho de Melo 	}
77704391debSArnaldo Carvalho de Melo 
778b04b7023SWang Nan broken_event:
7790f4ccd11SWang Nan 	if (prev)
780b6b85dadSWang Nan 		*prev = start;
7817bb41152SArnaldo Carvalho de Melo 
78204391debSArnaldo Carvalho de Melo 	return event;
78304391debSArnaldo Carvalho de Melo }
784f8a95309SArnaldo Carvalho de Melo 
7858db6d6b1SWang Nan union perf_event *perf_mmap__read_forward(struct perf_mmap *md, bool check_messup)
7860f4ccd11SWang Nan {
7870f4ccd11SWang Nan 	u64 head;
7880f4ccd11SWang Nan 	u64 old = md->prev;
7890f4ccd11SWang Nan 
7900f4ccd11SWang Nan 	/*
7910f4ccd11SWang Nan 	 * Check if event was unmapped due to a POLLHUP/POLLERR.
7920f4ccd11SWang Nan 	 */
7930f4ccd11SWang Nan 	if (!atomic_read(&md->refcnt))
7940f4ccd11SWang Nan 		return NULL;
7950f4ccd11SWang Nan 
7960f4ccd11SWang Nan 	head = perf_mmap__read_head(md);
7970f4ccd11SWang Nan 
7988db6d6b1SWang Nan 	return perf_mmap__read(md, check_messup, old, head, &md->prev);
7990f4ccd11SWang Nan }
8000f4ccd11SWang Nan 
801e24c7520SWang Nan union perf_event *
8028db6d6b1SWang Nan perf_mmap__read_backward(struct perf_mmap *md)
803e24c7520SWang Nan {
804e24c7520SWang Nan 	u64 head, end;
805e24c7520SWang Nan 	u64 start = md->prev;
806e24c7520SWang Nan 
807e24c7520SWang Nan 	/*
808e24c7520SWang Nan 	 * Check if event was unmapped due to a POLLHUP/POLLERR.
809e24c7520SWang Nan 	 */
810e24c7520SWang Nan 	if (!atomic_read(&md->refcnt))
811e24c7520SWang Nan 		return NULL;
812e24c7520SWang Nan 
813e24c7520SWang Nan 	head = perf_mmap__read_head(md);
814e24c7520SWang Nan 	if (!head)
815e24c7520SWang Nan 		return NULL;
816e24c7520SWang Nan 
817e24c7520SWang Nan 	/*
818e24c7520SWang Nan 	 * 'head' pointer starts from 0. Kernel minus sizeof(record) form
819e24c7520SWang Nan 	 * it each time when kernel writes to it, so in fact 'head' is
820e24c7520SWang Nan 	 * negative. 'end' pointer is made manually by adding the size of
821e24c7520SWang Nan 	 * the ring buffer to 'head' pointer, means the validate data can
822e24c7520SWang Nan 	 * read is the whole ring buffer. If 'end' is positive, the ring
823e24c7520SWang Nan 	 * buffer has not fully filled, so we must adjust 'end' to 0.
824e24c7520SWang Nan 	 *
825e24c7520SWang Nan 	 * However, since both 'head' and 'end' is unsigned, we can't
826e24c7520SWang Nan 	 * simply compare 'end' against 0. Here we compare '-head' and
827e24c7520SWang Nan 	 * the size of the ring buffer, where -head is the number of bytes
828e24c7520SWang Nan 	 * kernel write to the ring buffer.
829e24c7520SWang Nan 	 */
830e24c7520SWang Nan 	if (-head < (u64)(md->mask + 1))
831e24c7520SWang Nan 		end = 0;
832e24c7520SWang Nan 	else
833e24c7520SWang Nan 		end = head + md->mask + 1;
834e24c7520SWang Nan 
835e24c7520SWang Nan 	return perf_mmap__read(md, false, start, end, &md->prev);
836e24c7520SWang Nan }
837e24c7520SWang Nan 
8388db6d6b1SWang Nan union perf_event *perf_evlist__mmap_read_forward(struct perf_evlist *evlist, int idx)
8398db6d6b1SWang Nan {
8408db6d6b1SWang Nan 	struct perf_mmap *md = &evlist->mmap[idx];
8418db6d6b1SWang Nan 
8428db6d6b1SWang Nan 	/*
8438db6d6b1SWang Nan 	 * Check messup is required for forward overwritable ring buffer:
8448db6d6b1SWang Nan 	 * memory pointed by md->prev can be overwritten in this case.
8458db6d6b1SWang Nan 	 * No need for read-write ring buffer: kernel stop outputting when
8468db6d6b1SWang Nan 	 * it hit md->prev (perf_mmap__consume()).
8478db6d6b1SWang Nan 	 */
8488db6d6b1SWang Nan 	return perf_mmap__read_forward(md, evlist->overwrite);
8498db6d6b1SWang Nan }
8508db6d6b1SWang Nan 
8518db6d6b1SWang Nan union perf_event *perf_evlist__mmap_read_backward(struct perf_evlist *evlist, int idx)
8528db6d6b1SWang Nan {
8538db6d6b1SWang Nan 	struct perf_mmap *md = &evlist->mmap[idx];
8548db6d6b1SWang Nan 
8558db6d6b1SWang Nan 	/*
8568db6d6b1SWang Nan 	 * No need to check messup for backward ring buffer:
8578db6d6b1SWang Nan 	 * We can always read arbitrary long data from a backward
8588db6d6b1SWang Nan 	 * ring buffer unless we forget to pause it before reading.
8598db6d6b1SWang Nan 	 */
8608db6d6b1SWang Nan 	return perf_mmap__read_backward(md);
8618db6d6b1SWang Nan }
8628db6d6b1SWang Nan 
8635a5ddeb6SWang Nan union perf_event *perf_evlist__mmap_read(struct perf_evlist *evlist, int idx)
8645a5ddeb6SWang Nan {
8655a5ddeb6SWang Nan 	if (!evlist->backward)
8665a5ddeb6SWang Nan 		return perf_evlist__mmap_read_forward(evlist, idx);
8675a5ddeb6SWang Nan 	return perf_evlist__mmap_read_backward(evlist, idx);
8685a5ddeb6SWang Nan }
8695a5ddeb6SWang Nan 
8708db6d6b1SWang Nan void perf_mmap__read_catchup(struct perf_mmap *md)
871e24c7520SWang Nan {
872e24c7520SWang Nan 	u64 head;
873e24c7520SWang Nan 
874e24c7520SWang Nan 	if (!atomic_read(&md->refcnt))
875e24c7520SWang Nan 		return;
876e24c7520SWang Nan 
877e24c7520SWang Nan 	head = perf_mmap__read_head(md);
878e24c7520SWang Nan 	md->prev = head;
879e24c7520SWang Nan }
880e24c7520SWang Nan 
8818db6d6b1SWang Nan void perf_evlist__mmap_read_catchup(struct perf_evlist *evlist, int idx)
8828db6d6b1SWang Nan {
8838db6d6b1SWang Nan 	perf_mmap__read_catchup(&evlist->mmap[idx]);
8848db6d6b1SWang Nan }
8858db6d6b1SWang Nan 
88682396986SArnaldo Carvalho de Melo static bool perf_mmap__empty(struct perf_mmap *md)
88782396986SArnaldo Carvalho de Melo {
888b72e74d1SAdrian Hunter 	return perf_mmap__read_head(md) == md->prev && !md->auxtrace_mmap.base;
88982396986SArnaldo Carvalho de Melo }
89082396986SArnaldo Carvalho de Melo 
8918db6d6b1SWang Nan static void perf_mmap__get(struct perf_mmap *map)
8928db6d6b1SWang Nan {
8938db6d6b1SWang Nan 	atomic_inc(&map->refcnt);
8948db6d6b1SWang Nan }
8958db6d6b1SWang Nan 
8968db6d6b1SWang Nan static void perf_mmap__put(struct perf_mmap *md)
8978db6d6b1SWang Nan {
8988db6d6b1SWang Nan 	BUG_ON(md->base && atomic_read(&md->refcnt) == 0);
8998db6d6b1SWang Nan 
9008db6d6b1SWang Nan 	if (atomic_dec_and_test(&md->refcnt))
9018db6d6b1SWang Nan 		perf_mmap__munmap(md);
9028db6d6b1SWang Nan }
9038db6d6b1SWang Nan 
90482396986SArnaldo Carvalho de Melo static void perf_evlist__mmap_get(struct perf_evlist *evlist, int idx)
90582396986SArnaldo Carvalho de Melo {
9068db6d6b1SWang Nan 	perf_mmap__get(&evlist->mmap[idx]);
90782396986SArnaldo Carvalho de Melo }
90882396986SArnaldo Carvalho de Melo 
90982396986SArnaldo Carvalho de Melo static void perf_evlist__mmap_put(struct perf_evlist *evlist, int idx)
91082396986SArnaldo Carvalho de Melo {
9118db6d6b1SWang Nan 	perf_mmap__put(&evlist->mmap[idx]);
91282396986SArnaldo Carvalho de Melo }
91382396986SArnaldo Carvalho de Melo 
9148db6d6b1SWang Nan void perf_mmap__consume(struct perf_mmap *md, bool overwrite)
9158e50d384SZhouyi Zhou {
9168db6d6b1SWang Nan 	if (!overwrite) {
9177b8283b5SDavid Ahern 		u64 old = md->prev;
9188e50d384SZhouyi Zhou 
9198e50d384SZhouyi Zhou 		perf_mmap__write_tail(md, old);
9208e50d384SZhouyi Zhou 	}
92182396986SArnaldo Carvalho de Melo 
9227143849aSArnaldo Carvalho de Melo 	if (atomic_read(&md->refcnt) == 1 && perf_mmap__empty(md))
9238db6d6b1SWang Nan 		perf_mmap__put(md);
9248db6d6b1SWang Nan }
9258db6d6b1SWang Nan 
9268db6d6b1SWang Nan void perf_evlist__mmap_consume(struct perf_evlist *evlist, int idx)
9278db6d6b1SWang Nan {
9288db6d6b1SWang Nan 	perf_mmap__consume(&evlist->mmap[idx], evlist->overwrite);
9298e50d384SZhouyi Zhou }
9308e50d384SZhouyi Zhou 
931718c602dSAdrian Hunter int __weak auxtrace_mmap__mmap(struct auxtrace_mmap *mm __maybe_unused,
932718c602dSAdrian Hunter 			       struct auxtrace_mmap_params *mp __maybe_unused,
933718c602dSAdrian Hunter 			       void *userpg __maybe_unused,
934718c602dSAdrian Hunter 			       int fd __maybe_unused)
935718c602dSAdrian Hunter {
936718c602dSAdrian Hunter 	return 0;
937718c602dSAdrian Hunter }
938718c602dSAdrian Hunter 
939718c602dSAdrian Hunter void __weak auxtrace_mmap__munmap(struct auxtrace_mmap *mm __maybe_unused)
940718c602dSAdrian Hunter {
941718c602dSAdrian Hunter }
942718c602dSAdrian Hunter 
943718c602dSAdrian Hunter void __weak auxtrace_mmap_params__init(
944718c602dSAdrian Hunter 			struct auxtrace_mmap_params *mp __maybe_unused,
945718c602dSAdrian Hunter 			off_t auxtrace_offset __maybe_unused,
946718c602dSAdrian Hunter 			unsigned int auxtrace_pages __maybe_unused,
947718c602dSAdrian Hunter 			bool auxtrace_overwrite __maybe_unused)
948718c602dSAdrian Hunter {
949718c602dSAdrian Hunter }
950718c602dSAdrian Hunter 
951718c602dSAdrian Hunter void __weak auxtrace_mmap_params__set_idx(
952718c602dSAdrian Hunter 			struct auxtrace_mmap_params *mp __maybe_unused,
953718c602dSAdrian Hunter 			struct perf_evlist *evlist __maybe_unused,
954718c602dSAdrian Hunter 			int idx __maybe_unused,
955718c602dSAdrian Hunter 			bool per_cpu __maybe_unused)
956718c602dSAdrian Hunter {
957718c602dSAdrian Hunter }
958718c602dSAdrian Hunter 
9598db6d6b1SWang Nan static void perf_mmap__munmap(struct perf_mmap *map)
9608db6d6b1SWang Nan {
9618db6d6b1SWang Nan 	if (map->base != NULL) {
9628db6d6b1SWang Nan 		munmap(map->base, perf_mmap__mmap_len(map));
9638db6d6b1SWang Nan 		map->base = NULL;
9648db6d6b1SWang Nan 		map->fd = -1;
9658db6d6b1SWang Nan 		atomic_set(&map->refcnt, 0);
9668db6d6b1SWang Nan 	}
9678db6d6b1SWang Nan 	auxtrace_mmap__munmap(&map->auxtrace_mmap);
9688db6d6b1SWang Nan }
9698db6d6b1SWang Nan 
97093edcbd9SAdrian Hunter static void __perf_evlist__munmap(struct perf_evlist *evlist, int idx)
97193edcbd9SAdrian Hunter {
9728db6d6b1SWang Nan 	perf_mmap__munmap(&evlist->mmap[idx]);
97393edcbd9SAdrian Hunter }
97493edcbd9SAdrian Hunter 
9757e2ed097SArnaldo Carvalho de Melo void perf_evlist__munmap(struct perf_evlist *evlist)
976f8a95309SArnaldo Carvalho de Melo {
977aece948fSArnaldo Carvalho de Melo 	int i;
978f8a95309SArnaldo Carvalho de Melo 
979983874d1SArnaldo Carvalho de Melo 	if (evlist->mmap == NULL)
980983874d1SArnaldo Carvalho de Melo 		return;
981983874d1SArnaldo Carvalho de Melo 
98293edcbd9SAdrian Hunter 	for (i = 0; i < evlist->nr_mmaps; i++)
98393edcbd9SAdrian Hunter 		__perf_evlist__munmap(evlist, i);
984aece948fSArnaldo Carvalho de Melo 
98504662523SArnaldo Carvalho de Melo 	zfree(&evlist->mmap);
986f8a95309SArnaldo Carvalho de Melo }
987f8a95309SArnaldo Carvalho de Melo 
9888db6d6b1SWang Nan static struct perf_mmap *perf_evlist__alloc_mmap(struct perf_evlist *evlist)
989f8a95309SArnaldo Carvalho de Melo {
990d4c6fb36SWang Nan 	int i;
9918db6d6b1SWang Nan 	struct perf_mmap *map;
992d4c6fb36SWang Nan 
993a14bb7a6SArnaldo Carvalho de Melo 	evlist->nr_mmaps = cpu_map__nr(evlist->cpus);
994ec1e7e43SSukadev Bhattiprolu 	if (cpu_map__empty(evlist->cpus))
995b3a319d5SNamhyung Kim 		evlist->nr_mmaps = thread_map__nr(evlist->threads);
9968db6d6b1SWang Nan 	map = zalloc(evlist->nr_mmaps * sizeof(struct perf_mmap));
9978db6d6b1SWang Nan 	if (!map)
9988db6d6b1SWang Nan 		return NULL;
999946ae1d4SWang Nan 
1000d4c6fb36SWang Nan 	for (i = 0; i < evlist->nr_mmaps; i++)
10018db6d6b1SWang Nan 		map[i].fd = -1;
10028db6d6b1SWang Nan 	return map;
1003f8a95309SArnaldo Carvalho de Melo }
1004f8a95309SArnaldo Carvalho de Melo 
1005a8a8f3ebSAdrian Hunter struct mmap_params {
1006a8a8f3ebSAdrian Hunter 	int prot;
1007a8a8f3ebSAdrian Hunter 	int mask;
1008718c602dSAdrian Hunter 	struct auxtrace_mmap_params auxtrace_mp;
1009a8a8f3ebSAdrian Hunter };
1010a8a8f3ebSAdrian Hunter 
10118db6d6b1SWang Nan static int perf_mmap__mmap(struct perf_mmap *map,
1012a8a8f3ebSAdrian Hunter 			   struct mmap_params *mp, int fd)
1013f8a95309SArnaldo Carvalho de Melo {
101482396986SArnaldo Carvalho de Melo 	/*
101582396986SArnaldo Carvalho de Melo 	 * The last one will be done at perf_evlist__mmap_consume(), so that we
101682396986SArnaldo Carvalho de Melo 	 * make sure we don't prevent tools from consuming every last event in
101782396986SArnaldo Carvalho de Melo 	 * the ring buffer.
101882396986SArnaldo Carvalho de Melo 	 *
101982396986SArnaldo Carvalho de Melo 	 * I.e. we can get the POLLHUP meaning that the fd doesn't exist
102082396986SArnaldo Carvalho de Melo 	 * anymore, but the last events for it are still in the ring buffer,
102182396986SArnaldo Carvalho de Melo 	 * waiting to be consumed.
102282396986SArnaldo Carvalho de Melo 	 *
102382396986SArnaldo Carvalho de Melo 	 * Tools can chose to ignore this at their own discretion, but the
102482396986SArnaldo Carvalho de Melo 	 * evlist layer can't just drop it when filtering events in
102582396986SArnaldo Carvalho de Melo 	 * perf_evlist__filter_pollfd().
102682396986SArnaldo Carvalho de Melo 	 */
10278db6d6b1SWang Nan 	atomic_set(&map->refcnt, 2);
10288db6d6b1SWang Nan 	map->prev = 0;
10298db6d6b1SWang Nan 	map->mask = mp->mask;
10308db6d6b1SWang Nan 	map->base = mmap(NULL, perf_mmap__mmap_len(map), mp->prot,
1031f8a95309SArnaldo Carvalho de Melo 			 MAP_SHARED, fd, 0);
10328db6d6b1SWang Nan 	if (map->base == MAP_FAILED) {
103302635965SAdrian Hunter 		pr_debug2("failed to mmap perf event ring buffer, error %d\n",
103402635965SAdrian Hunter 			  errno);
10358db6d6b1SWang Nan 		map->base = NULL;
1036f8a95309SArnaldo Carvalho de Melo 		return -1;
1037301b195dSNelson Elhage 	}
10388db6d6b1SWang Nan 	map->fd = fd;
1039ad6765ddSArnaldo Carvalho de Melo 
10408db6d6b1SWang Nan 	if (auxtrace_mmap__mmap(&map->auxtrace_mmap,
10418db6d6b1SWang Nan 				&mp->auxtrace_mp, map->base, fd))
1042718c602dSAdrian Hunter 		return -1;
1043718c602dSAdrian Hunter 
1044f8a95309SArnaldo Carvalho de Melo 	return 0;
1045f8a95309SArnaldo Carvalho de Melo }
1046f8a95309SArnaldo Carvalho de Melo 
10478db6d6b1SWang Nan static int __perf_evlist__mmap(struct perf_evlist *evlist, int idx,
10488db6d6b1SWang Nan 			       struct mmap_params *mp, int fd)
10498db6d6b1SWang Nan {
10508db6d6b1SWang Nan 	return perf_mmap__mmap(&evlist->mmap[idx], mp, fd);
10518db6d6b1SWang Nan }
10528db6d6b1SWang Nan 
1053f3058a1cSWang Nan static bool
1054f3058a1cSWang Nan perf_evlist__should_poll(struct perf_evlist *evlist __maybe_unused,
1055f3058a1cSWang Nan 			 struct perf_evsel *evsel)
1056f3058a1cSWang Nan {
105732a951b4SArnaldo Carvalho de Melo 	if (evsel->attr.write_backward)
1058f3058a1cSWang Nan 		return false;
1059f3058a1cSWang Nan 	return true;
1060f3058a1cSWang Nan }
1061f3058a1cSWang Nan 
106204e21314SAdrian Hunter static int perf_evlist__mmap_per_evsel(struct perf_evlist *evlist, int idx,
1063a8a8f3ebSAdrian Hunter 				       struct mmap_params *mp, int cpu,
1064a8a8f3ebSAdrian Hunter 				       int thread, int *output)
1065aece948fSArnaldo Carvalho de Melo {
1066aece948fSArnaldo Carvalho de Melo 	struct perf_evsel *evsel;
1067f3058a1cSWang Nan 	int revent;
106804e21314SAdrian Hunter 
1069e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, evsel) {
1070bf8e8f4bSAdrian Hunter 		int fd;
1071bf8e8f4bSAdrian Hunter 
107232a951b4SArnaldo Carvalho de Melo 		if (!!evsel->attr.write_backward != (evlist->overwrite && evlist->backward))
1073f3058a1cSWang Nan 			continue;
1074f3058a1cSWang Nan 
1075bf8e8f4bSAdrian Hunter 		if (evsel->system_wide && thread)
1076bf8e8f4bSAdrian Hunter 			continue;
1077bf8e8f4bSAdrian Hunter 
1078bf8e8f4bSAdrian Hunter 		fd = FD(evsel, cpu, thread);
107904e21314SAdrian Hunter 
108004e21314SAdrian Hunter 		if (*output == -1) {
108104e21314SAdrian Hunter 			*output = fd;
1082a8a8f3ebSAdrian Hunter 			if (__perf_evlist__mmap(evlist, idx, mp, *output) < 0)
108304e21314SAdrian Hunter 				return -1;
108404e21314SAdrian Hunter 		} else {
108504e21314SAdrian Hunter 			if (ioctl(fd, PERF_EVENT_IOC_SET_OUTPUT, *output) != 0)
108604e21314SAdrian Hunter 				return -1;
108782396986SArnaldo Carvalho de Melo 
108882396986SArnaldo Carvalho de Melo 			perf_evlist__mmap_get(evlist, idx);
108904e21314SAdrian Hunter 		}
109004e21314SAdrian Hunter 
1091f3058a1cSWang Nan 		revent = perf_evlist__should_poll(evlist, evsel) ? POLLIN : 0;
1092f3058a1cSWang Nan 
1093f90d194aSAdrian Hunter 		/*
1094f90d194aSAdrian Hunter 		 * The system_wide flag causes a selected event to be opened
1095f90d194aSAdrian Hunter 		 * always without a pid.  Consequently it will never get a
1096f90d194aSAdrian Hunter 		 * POLLHUP, but it is used for tracking in combination with
1097f90d194aSAdrian Hunter 		 * other events, so it should not need to be polled anyway.
1098f90d194aSAdrian Hunter 		 * Therefore don't add it for polling.
1099f90d194aSAdrian Hunter 		 */
1100f90d194aSAdrian Hunter 		if (!evsel->system_wide &&
1101f3058a1cSWang Nan 		    __perf_evlist__add_pollfd(evlist, fd, idx, revent) < 0) {
110282396986SArnaldo Carvalho de Melo 			perf_evlist__mmap_put(evlist, idx);
1103ad6765ddSArnaldo Carvalho de Melo 			return -1;
110482396986SArnaldo Carvalho de Melo 		}
1105033fa713SArnaldo Carvalho de Melo 
11063c659eedSAdrian Hunter 		if (evsel->attr.read_format & PERF_FORMAT_ID) {
11073c659eedSAdrian Hunter 			if (perf_evlist__id_add_fd(evlist, evsel, cpu, thread,
11083c659eedSAdrian Hunter 						   fd) < 0)
110904e21314SAdrian Hunter 				return -1;
11103c659eedSAdrian Hunter 			perf_evlist__set_sid_idx(evlist, evsel, idx, cpu,
11113c659eedSAdrian Hunter 						 thread);
11123c659eedSAdrian Hunter 		}
111304e21314SAdrian Hunter 	}
111404e21314SAdrian Hunter 
111504e21314SAdrian Hunter 	return 0;
111604e21314SAdrian Hunter }
111704e21314SAdrian Hunter 
1118a8a8f3ebSAdrian Hunter static int perf_evlist__mmap_per_cpu(struct perf_evlist *evlist,
1119a8a8f3ebSAdrian Hunter 				     struct mmap_params *mp)
112004e21314SAdrian Hunter {
1121aece948fSArnaldo Carvalho de Melo 	int cpu, thread;
1122b3a319d5SNamhyung Kim 	int nr_cpus = cpu_map__nr(evlist->cpus);
1123b3a319d5SNamhyung Kim 	int nr_threads = thread_map__nr(evlist->threads);
1124aece948fSArnaldo Carvalho de Melo 
1125e3e1a54fSAdrian Hunter 	pr_debug2("perf event ring buffer mmapped per cpu\n");
1126b3a319d5SNamhyung Kim 	for (cpu = 0; cpu < nr_cpus; cpu++) {
1127aece948fSArnaldo Carvalho de Melo 		int output = -1;
1128aece948fSArnaldo Carvalho de Melo 
1129718c602dSAdrian Hunter 		auxtrace_mmap_params__set_idx(&mp->auxtrace_mp, evlist, cpu,
1130718c602dSAdrian Hunter 					      true);
1131718c602dSAdrian Hunter 
1132b3a319d5SNamhyung Kim 		for (thread = 0; thread < nr_threads; thread++) {
1133a8a8f3ebSAdrian Hunter 			if (perf_evlist__mmap_per_evsel(evlist, cpu, mp, cpu,
1134a8a8f3ebSAdrian Hunter 							thread, &output))
1135aece948fSArnaldo Carvalho de Melo 				goto out_unmap;
1136aece948fSArnaldo Carvalho de Melo 		}
1137aece948fSArnaldo Carvalho de Melo 	}
1138aece948fSArnaldo Carvalho de Melo 
1139aece948fSArnaldo Carvalho de Melo 	return 0;
1140aece948fSArnaldo Carvalho de Melo 
1141aece948fSArnaldo Carvalho de Melo out_unmap:
114293edcbd9SAdrian Hunter 	for (cpu = 0; cpu < nr_cpus; cpu++)
114393edcbd9SAdrian Hunter 		__perf_evlist__munmap(evlist, cpu);
1144aece948fSArnaldo Carvalho de Melo 	return -1;
1145aece948fSArnaldo Carvalho de Melo }
1146aece948fSArnaldo Carvalho de Melo 
1147a8a8f3ebSAdrian Hunter static int perf_evlist__mmap_per_thread(struct perf_evlist *evlist,
1148a8a8f3ebSAdrian Hunter 					struct mmap_params *mp)
1149aece948fSArnaldo Carvalho de Melo {
1150aece948fSArnaldo Carvalho de Melo 	int thread;
1151b3a319d5SNamhyung Kim 	int nr_threads = thread_map__nr(evlist->threads);
1152aece948fSArnaldo Carvalho de Melo 
1153e3e1a54fSAdrian Hunter 	pr_debug2("perf event ring buffer mmapped per thread\n");
1154b3a319d5SNamhyung Kim 	for (thread = 0; thread < nr_threads; thread++) {
1155aece948fSArnaldo Carvalho de Melo 		int output = -1;
1156aece948fSArnaldo Carvalho de Melo 
1157718c602dSAdrian Hunter 		auxtrace_mmap_params__set_idx(&mp->auxtrace_mp, evlist, thread,
1158718c602dSAdrian Hunter 					      false);
1159718c602dSAdrian Hunter 
1160a8a8f3ebSAdrian Hunter 		if (perf_evlist__mmap_per_evsel(evlist, thread, mp, 0, thread,
1161a8a8f3ebSAdrian Hunter 						&output))
1162aece948fSArnaldo Carvalho de Melo 			goto out_unmap;
1163aece948fSArnaldo Carvalho de Melo 	}
1164aece948fSArnaldo Carvalho de Melo 
1165aece948fSArnaldo Carvalho de Melo 	return 0;
1166aece948fSArnaldo Carvalho de Melo 
1167aece948fSArnaldo Carvalho de Melo out_unmap:
116893edcbd9SAdrian Hunter 	for (thread = 0; thread < nr_threads; thread++)
116993edcbd9SAdrian Hunter 		__perf_evlist__munmap(evlist, thread);
1170aece948fSArnaldo Carvalho de Melo 	return -1;
1171aece948fSArnaldo Carvalho de Melo }
1172aece948fSArnaldo Carvalho de Melo 
1173f5e7150cSArnaldo Carvalho de Melo unsigned long perf_event_mlock_kb_in_pages(void)
1174994a1f78SJiri Olsa {
1175f5e7150cSArnaldo Carvalho de Melo 	unsigned long pages;
11768185e881SArnaldo Carvalho de Melo 	int max;
11778185e881SArnaldo Carvalho de Melo 
11788185e881SArnaldo Carvalho de Melo 	if (sysctl__read_int("kernel/perf_event_mlock_kb", &max) < 0) {
11798185e881SArnaldo Carvalho de Melo 		/*
11808185e881SArnaldo Carvalho de Melo 		 * Pick a once upon a time good value, i.e. things look
11818185e881SArnaldo Carvalho de Melo 		 * strange since we can't read a sysctl value, but lets not
11828185e881SArnaldo Carvalho de Melo 		 * die yet...
11838185e881SArnaldo Carvalho de Melo 		 */
11848185e881SArnaldo Carvalho de Melo 		max = 512;
11858185e881SArnaldo Carvalho de Melo 	} else {
11868185e881SArnaldo Carvalho de Melo 		max -= (page_size / 1024);
11878185e881SArnaldo Carvalho de Melo 	}
11888185e881SArnaldo Carvalho de Melo 
11898185e881SArnaldo Carvalho de Melo 	pages = (max * 1024) / page_size;
11901be300f4SArnaldo Carvalho de Melo 	if (!is_power_of_2(pages))
11911be300f4SArnaldo Carvalho de Melo 		pages = rounddown_pow_of_two(pages);
1192f5e7150cSArnaldo Carvalho de Melo 
1193f5e7150cSArnaldo Carvalho de Melo 	return pages;
1194f5e7150cSArnaldo Carvalho de Melo }
1195f5e7150cSArnaldo Carvalho de Melo 
1196f5e7150cSArnaldo Carvalho de Melo static size_t perf_evlist__mmap_size(unsigned long pages)
1197f5e7150cSArnaldo Carvalho de Melo {
1198f5e7150cSArnaldo Carvalho de Melo 	if (pages == UINT_MAX)
1199f5e7150cSArnaldo Carvalho de Melo 		pages = perf_event_mlock_kb_in_pages();
1200f5e7150cSArnaldo Carvalho de Melo 	else if (!is_power_of_2(pages))
1201994a1f78SJiri Olsa 		return 0;
1202994a1f78SJiri Olsa 
1203994a1f78SJiri Olsa 	return (pages + 1) * page_size;
1204994a1f78SJiri Olsa }
1205994a1f78SJiri Olsa 
120633c2dcfdSDavid Ahern static long parse_pages_arg(const char *str, unsigned long min,
120733c2dcfdSDavid Ahern 			    unsigned long max)
1208994a1f78SJiri Olsa {
12092fbe4abeSAdrian Hunter 	unsigned long pages, val;
121027050f53SJiri Olsa 	static struct parse_tag tags[] = {
121127050f53SJiri Olsa 		{ .tag  = 'B', .mult = 1       },
121227050f53SJiri Olsa 		{ .tag  = 'K', .mult = 1 << 10 },
121327050f53SJiri Olsa 		{ .tag  = 'M', .mult = 1 << 20 },
121427050f53SJiri Olsa 		{ .tag  = 'G', .mult = 1 << 30 },
121527050f53SJiri Olsa 		{ .tag  = 0 },
121627050f53SJiri Olsa 	};
1217994a1f78SJiri Olsa 
12188973504bSDavid Ahern 	if (str == NULL)
121933c2dcfdSDavid Ahern 		return -EINVAL;
12208973504bSDavid Ahern 
122127050f53SJiri Olsa 	val = parse_tag_value(str, tags);
12222fbe4abeSAdrian Hunter 	if (val != (unsigned long) -1) {
122327050f53SJiri Olsa 		/* we got file size value */
122427050f53SJiri Olsa 		pages = PERF_ALIGN(val, page_size) / page_size;
122527050f53SJiri Olsa 	} else {
122627050f53SJiri Olsa 		/* we got pages count value */
122727050f53SJiri Olsa 		char *eptr;
1228994a1f78SJiri Olsa 		pages = strtoul(str, &eptr, 10);
122933c2dcfdSDavid Ahern 		if (*eptr != '\0')
123033c2dcfdSDavid Ahern 			return -EINVAL;
123127050f53SJiri Olsa 	}
1232994a1f78SJiri Olsa 
12332bcab6c1SAdrian Hunter 	if (pages == 0 && min == 0) {
123433c2dcfdSDavid Ahern 		/* leave number of pages at 0 */
12351dbfa938SAdrian Hunter 	} else if (!is_power_of_2(pages)) {
123633c2dcfdSDavid Ahern 		/* round pages up to next power of 2 */
123791529834SArnaldo Carvalho de Melo 		pages = roundup_pow_of_two(pages);
12381dbfa938SAdrian Hunter 		if (!pages)
12391dbfa938SAdrian Hunter 			return -EINVAL;
12409639837eSDavid Ahern 		pr_info("rounding mmap pages size to %lu bytes (%lu pages)\n",
12419639837eSDavid Ahern 			pages * page_size, pages);
12422fbe4abeSAdrian Hunter 	}
12432fbe4abeSAdrian Hunter 
124433c2dcfdSDavid Ahern 	if (pages > max)
124533c2dcfdSDavid Ahern 		return -EINVAL;
124633c2dcfdSDavid Ahern 
124733c2dcfdSDavid Ahern 	return pages;
124833c2dcfdSDavid Ahern }
124933c2dcfdSDavid Ahern 
1250e9db1310SAdrian Hunter int __perf_evlist__parse_mmap_pages(unsigned int *mmap_pages, const char *str)
125133c2dcfdSDavid Ahern {
125233c2dcfdSDavid Ahern 	unsigned long max = UINT_MAX;
125333c2dcfdSDavid Ahern 	long pages;
125433c2dcfdSDavid Ahern 
1255f5ae9c42SAdrian Hunter 	if (max > SIZE_MAX / page_size)
125633c2dcfdSDavid Ahern 		max = SIZE_MAX / page_size;
125733c2dcfdSDavid Ahern 
125833c2dcfdSDavid Ahern 	pages = parse_pages_arg(str, 1, max);
125933c2dcfdSDavid Ahern 	if (pages < 0) {
126033c2dcfdSDavid Ahern 		pr_err("Invalid argument for --mmap_pages/-m\n");
1261994a1f78SJiri Olsa 		return -1;
1262994a1f78SJiri Olsa 	}
1263994a1f78SJiri Olsa 
1264994a1f78SJiri Olsa 	*mmap_pages = pages;
1265994a1f78SJiri Olsa 	return 0;
1266994a1f78SJiri Olsa }
1267994a1f78SJiri Olsa 
1268e9db1310SAdrian Hunter int perf_evlist__parse_mmap_pages(const struct option *opt, const char *str,
1269e9db1310SAdrian Hunter 				  int unset __maybe_unused)
1270e9db1310SAdrian Hunter {
1271e9db1310SAdrian Hunter 	return __perf_evlist__parse_mmap_pages(opt->value, str);
1272e9db1310SAdrian Hunter }
1273e9db1310SAdrian Hunter 
1274c83fa7f2SAdrian Hunter /**
1275718c602dSAdrian Hunter  * perf_evlist__mmap_ex - Create mmaps to receive events.
1276c83fa7f2SAdrian Hunter  * @evlist: list of events
1277c83fa7f2SAdrian Hunter  * @pages: map length in pages
1278c83fa7f2SAdrian Hunter  * @overwrite: overwrite older events?
1279718c602dSAdrian Hunter  * @auxtrace_pages - auxtrace map length in pages
1280718c602dSAdrian Hunter  * @auxtrace_overwrite - overwrite older auxtrace data?
1281f8a95309SArnaldo Carvalho de Melo  *
1282c83fa7f2SAdrian Hunter  * If @overwrite is %false the user needs to signal event consumption using
1283c83fa7f2SAdrian Hunter  * perf_mmap__write_tail().  Using perf_evlist__mmap_read() does this
1284c83fa7f2SAdrian Hunter  * automatically.
1285f8a95309SArnaldo Carvalho de Melo  *
1286718c602dSAdrian Hunter  * Similarly, if @auxtrace_overwrite is %false the user needs to signal data
1287718c602dSAdrian Hunter  * consumption using auxtrace_mmap__write_tail().
1288718c602dSAdrian Hunter  *
1289c83fa7f2SAdrian Hunter  * Return: %0 on success, negative error code otherwise.
1290f8a95309SArnaldo Carvalho de Melo  */
1291718c602dSAdrian Hunter int perf_evlist__mmap_ex(struct perf_evlist *evlist, unsigned int pages,
1292718c602dSAdrian Hunter 			 bool overwrite, unsigned int auxtrace_pages,
1293718c602dSAdrian Hunter 			 bool auxtrace_overwrite)
1294f8a95309SArnaldo Carvalho de Melo {
1295aece948fSArnaldo Carvalho de Melo 	struct perf_evsel *evsel;
12967e2ed097SArnaldo Carvalho de Melo 	const struct cpu_map *cpus = evlist->cpus;
12977e2ed097SArnaldo Carvalho de Melo 	const struct thread_map *threads = evlist->threads;
1298a8a8f3ebSAdrian Hunter 	struct mmap_params mp = {
1299a8a8f3ebSAdrian Hunter 		.prot = PROT_READ | (overwrite ? 0 : PROT_WRITE),
1300a8a8f3ebSAdrian Hunter 	};
130150a682ceSArnaldo Carvalho de Melo 
13028db6d6b1SWang Nan 	if (!evlist->mmap)
13038db6d6b1SWang Nan 		evlist->mmap = perf_evlist__alloc_mmap(evlist);
13048db6d6b1SWang Nan 	if (!evlist->mmap)
1305f8a95309SArnaldo Carvalho de Melo 		return -ENOMEM;
1306f8a95309SArnaldo Carvalho de Melo 
13071b85337dSArnaldo Carvalho de Melo 	if (evlist->pollfd.entries == NULL && perf_evlist__alloc_pollfd(evlist) < 0)
1308f8a95309SArnaldo Carvalho de Melo 		return -ENOMEM;
1309f8a95309SArnaldo Carvalho de Melo 
1310f8a95309SArnaldo Carvalho de Melo 	evlist->overwrite = overwrite;
1311994a1f78SJiri Olsa 	evlist->mmap_len = perf_evlist__mmap_size(pages);
13122af68ef5SAdrian Hunter 	pr_debug("mmap size %zuB\n", evlist->mmap_len);
1313a8a8f3ebSAdrian Hunter 	mp.mask = evlist->mmap_len - page_size - 1;
1314f8a95309SArnaldo Carvalho de Melo 
1315718c602dSAdrian Hunter 	auxtrace_mmap_params__init(&mp.auxtrace_mp, evlist->mmap_len,
1316718c602dSAdrian Hunter 				   auxtrace_pages, auxtrace_overwrite);
1317718c602dSAdrian Hunter 
1318e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, evsel) {
1319f8a95309SArnaldo Carvalho de Melo 		if ((evsel->attr.read_format & PERF_FORMAT_ID) &&
1320a91e5431SArnaldo Carvalho de Melo 		    evsel->sample_id == NULL &&
1321a14bb7a6SArnaldo Carvalho de Melo 		    perf_evsel__alloc_id(evsel, cpu_map__nr(cpus), threads->nr) < 0)
1322f8a95309SArnaldo Carvalho de Melo 			return -ENOMEM;
1323f8a95309SArnaldo Carvalho de Melo 	}
1324f8a95309SArnaldo Carvalho de Melo 
1325ec1e7e43SSukadev Bhattiprolu 	if (cpu_map__empty(cpus))
1326a8a8f3ebSAdrian Hunter 		return perf_evlist__mmap_per_thread(evlist, &mp);
1327f8a95309SArnaldo Carvalho de Melo 
1328a8a8f3ebSAdrian Hunter 	return perf_evlist__mmap_per_cpu(evlist, &mp);
1329f8a95309SArnaldo Carvalho de Melo }
13307e2ed097SArnaldo Carvalho de Melo 
1331718c602dSAdrian Hunter int perf_evlist__mmap(struct perf_evlist *evlist, unsigned int pages,
1332718c602dSAdrian Hunter 		      bool overwrite)
1333718c602dSAdrian Hunter {
1334718c602dSAdrian Hunter 	return perf_evlist__mmap_ex(evlist, pages, overwrite, 0, false);
1335718c602dSAdrian Hunter }
1336718c602dSAdrian Hunter 
1337602ad878SArnaldo Carvalho de Melo int perf_evlist__create_maps(struct perf_evlist *evlist, struct target *target)
13387e2ed097SArnaldo Carvalho de Melo {
133974bfd2b2SAdrian Hunter 	struct cpu_map *cpus;
134074bfd2b2SAdrian Hunter 	struct thread_map *threads;
13417e2ed097SArnaldo Carvalho de Melo 
134274bfd2b2SAdrian Hunter 	threads = thread_map__new_str(target->pid, target->tid, target->uid);
134374bfd2b2SAdrian Hunter 
134474bfd2b2SAdrian Hunter 	if (!threads)
13457e2ed097SArnaldo Carvalho de Melo 		return -1;
13467e2ed097SArnaldo Carvalho de Melo 
13479c105fbcSDongsheng Yang 	if (target__uses_dummy_map(target))
134874bfd2b2SAdrian Hunter 		cpus = cpu_map__dummy_new();
1349879d77d0SNamhyung Kim 	else
135074bfd2b2SAdrian Hunter 		cpus = cpu_map__new(target->cpu_list);
13517e2ed097SArnaldo Carvalho de Melo 
135274bfd2b2SAdrian Hunter 	if (!cpus)
13537e2ed097SArnaldo Carvalho de Melo 		goto out_delete_threads;
13547e2ed097SArnaldo Carvalho de Melo 
1355ec9a77a7SAdrian Hunter 	evlist->has_user_cpus = !!target->cpu_list;
1356ec9a77a7SAdrian Hunter 
135774bfd2b2SAdrian Hunter 	perf_evlist__set_maps(evlist, cpus, threads);
1358d5bc056eSAdrian Hunter 
1359d5bc056eSAdrian Hunter 	return 0;
13607e2ed097SArnaldo Carvalho de Melo 
13617e2ed097SArnaldo Carvalho de Melo out_delete_threads:
136274bfd2b2SAdrian Hunter 	thread_map__put(threads);
13637e2ed097SArnaldo Carvalho de Melo 	return -1;
13647e2ed097SArnaldo Carvalho de Melo }
13657e2ed097SArnaldo Carvalho de Melo 
1366d5bc056eSAdrian Hunter void perf_evlist__set_maps(struct perf_evlist *evlist, struct cpu_map *cpus,
13673de5cfb0SJiri Olsa 			   struct thread_map *threads)
13683de5cfb0SJiri Olsa {
1369934e0f20SAdrian Hunter 	/*
1370934e0f20SAdrian Hunter 	 * Allow for the possibility that one or another of the maps isn't being
1371934e0f20SAdrian Hunter 	 * changed i.e. don't put it.  Note we are assuming the maps that are
1372934e0f20SAdrian Hunter 	 * being applied are brand new and evlist is taking ownership of the
1373934e0f20SAdrian Hunter 	 * original reference count of 1.  If that is not the case it is up to
1374934e0f20SAdrian Hunter 	 * the caller to increase the reference count.
1375934e0f20SAdrian Hunter 	 */
1376934e0f20SAdrian Hunter 	if (cpus != evlist->cpus) {
13773de5cfb0SJiri Olsa 		cpu_map__put(evlist->cpus);
1378a55e5663SArnaldo Carvalho de Melo 		evlist->cpus = cpu_map__get(cpus);
1379934e0f20SAdrian Hunter 	}
13803de5cfb0SJiri Olsa 
1381934e0f20SAdrian Hunter 	if (threads != evlist->threads) {
13823de5cfb0SJiri Olsa 		thread_map__put(evlist->threads);
1383a55e5663SArnaldo Carvalho de Melo 		evlist->threads = thread_map__get(threads);
1384934e0f20SAdrian Hunter 	}
13853de5cfb0SJiri Olsa 
1386ec9a77a7SAdrian Hunter 	perf_evlist__propagate_maps(evlist);
13873de5cfb0SJiri Olsa }
13883de5cfb0SJiri Olsa 
138922c8a376SArnaldo Carvalho de Melo void __perf_evlist__set_sample_bit(struct perf_evlist *evlist,
139022c8a376SArnaldo Carvalho de Melo 				   enum perf_event_sample_format bit)
139122c8a376SArnaldo Carvalho de Melo {
139222c8a376SArnaldo Carvalho de Melo 	struct perf_evsel *evsel;
139322c8a376SArnaldo Carvalho de Melo 
1394e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, evsel)
139522c8a376SArnaldo Carvalho de Melo 		__perf_evsel__set_sample_bit(evsel, bit);
139622c8a376SArnaldo Carvalho de Melo }
139722c8a376SArnaldo Carvalho de Melo 
139822c8a376SArnaldo Carvalho de Melo void __perf_evlist__reset_sample_bit(struct perf_evlist *evlist,
139922c8a376SArnaldo Carvalho de Melo 				     enum perf_event_sample_format bit)
140022c8a376SArnaldo Carvalho de Melo {
140122c8a376SArnaldo Carvalho de Melo 	struct perf_evsel *evsel;
140222c8a376SArnaldo Carvalho de Melo 
1403e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, evsel)
140422c8a376SArnaldo Carvalho de Melo 		__perf_evsel__reset_sample_bit(evsel, bit);
140522c8a376SArnaldo Carvalho de Melo }
140622c8a376SArnaldo Carvalho de Melo 
140723d4aad4SArnaldo Carvalho de Melo int perf_evlist__apply_filters(struct perf_evlist *evlist, struct perf_evsel **err_evsel)
14080a102479SFrederic Weisbecker {
14090a102479SFrederic Weisbecker 	struct perf_evsel *evsel;
1410745cefc5SArnaldo Carvalho de Melo 	int err = 0;
1411745cefc5SArnaldo Carvalho de Melo 	const int ncpus = cpu_map__nr(evlist->cpus),
1412b3a319d5SNamhyung Kim 		  nthreads = thread_map__nr(evlist->threads);
14130a102479SFrederic Weisbecker 
1414e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, evsel) {
1415745cefc5SArnaldo Carvalho de Melo 		if (evsel->filter == NULL)
14160a102479SFrederic Weisbecker 			continue;
1417745cefc5SArnaldo Carvalho de Melo 
1418d988d5eeSKan Liang 		/*
1419d988d5eeSKan Liang 		 * filters only work for tracepoint event, which doesn't have cpu limit.
1420d988d5eeSKan Liang 		 * So evlist and evsel should always be same.
1421d988d5eeSKan Liang 		 */
1422f47805a2SArnaldo Carvalho de Melo 		err = perf_evsel__apply_filter(evsel, ncpus, nthreads, evsel->filter);
142323d4aad4SArnaldo Carvalho de Melo 		if (err) {
142423d4aad4SArnaldo Carvalho de Melo 			*err_evsel = evsel;
1425745cefc5SArnaldo Carvalho de Melo 			break;
14260a102479SFrederic Weisbecker 		}
142723d4aad4SArnaldo Carvalho de Melo 	}
14280a102479SFrederic Weisbecker 
1429745cefc5SArnaldo Carvalho de Melo 	return err;
1430745cefc5SArnaldo Carvalho de Melo }
1431745cefc5SArnaldo Carvalho de Melo 
1432745cefc5SArnaldo Carvalho de Melo int perf_evlist__set_filter(struct perf_evlist *evlist, const char *filter)
1433745cefc5SArnaldo Carvalho de Melo {
1434745cefc5SArnaldo Carvalho de Melo 	struct perf_evsel *evsel;
1435745cefc5SArnaldo Carvalho de Melo 	int err = 0;
1436745cefc5SArnaldo Carvalho de Melo 
1437e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, evsel) {
1438fdf14720SWang Nan 		if (evsel->attr.type != PERF_TYPE_TRACEPOINT)
1439fdf14720SWang Nan 			continue;
1440fdf14720SWang Nan 
144194ad89bcSArnaldo Carvalho de Melo 		err = perf_evsel__set_filter(evsel, filter);
1442745cefc5SArnaldo Carvalho de Melo 		if (err)
1443745cefc5SArnaldo Carvalho de Melo 			break;
1444745cefc5SArnaldo Carvalho de Melo 	}
1445745cefc5SArnaldo Carvalho de Melo 
1446745cefc5SArnaldo Carvalho de Melo 	return err;
14470a102479SFrederic Weisbecker }
144874429964SFrederic Weisbecker 
1449be199adaSArnaldo Carvalho de Melo int perf_evlist__set_filter_pids(struct perf_evlist *evlist, size_t npids, pid_t *pids)
1450cfd70a26SArnaldo Carvalho de Melo {
1451cfd70a26SArnaldo Carvalho de Melo 	char *filter;
1452be199adaSArnaldo Carvalho de Melo 	int ret = -1;
1453be199adaSArnaldo Carvalho de Melo 	size_t i;
1454cfd70a26SArnaldo Carvalho de Melo 
1455be199adaSArnaldo Carvalho de Melo 	for (i = 0; i < npids; ++i) {
1456be199adaSArnaldo Carvalho de Melo 		if (i == 0) {
1457be199adaSArnaldo Carvalho de Melo 			if (asprintf(&filter, "common_pid != %d", pids[i]) < 0)
1458cfd70a26SArnaldo Carvalho de Melo 				return -1;
1459be199adaSArnaldo Carvalho de Melo 		} else {
1460be199adaSArnaldo Carvalho de Melo 			char *tmp;
1461be199adaSArnaldo Carvalho de Melo 
1462be199adaSArnaldo Carvalho de Melo 			if (asprintf(&tmp, "%s && common_pid != %d", filter, pids[i]) < 0)
1463be199adaSArnaldo Carvalho de Melo 				goto out_free;
1464be199adaSArnaldo Carvalho de Melo 
1465be199adaSArnaldo Carvalho de Melo 			free(filter);
1466be199adaSArnaldo Carvalho de Melo 			filter = tmp;
1467be199adaSArnaldo Carvalho de Melo 		}
1468be199adaSArnaldo Carvalho de Melo 	}
1469cfd70a26SArnaldo Carvalho de Melo 
1470cfd70a26SArnaldo Carvalho de Melo 	ret = perf_evlist__set_filter(evlist, filter);
1471be199adaSArnaldo Carvalho de Melo out_free:
1472cfd70a26SArnaldo Carvalho de Melo 	free(filter);
1473cfd70a26SArnaldo Carvalho de Melo 	return ret;
1474cfd70a26SArnaldo Carvalho de Melo }
1475cfd70a26SArnaldo Carvalho de Melo 
1476be199adaSArnaldo Carvalho de Melo int perf_evlist__set_filter_pid(struct perf_evlist *evlist, pid_t pid)
1477be199adaSArnaldo Carvalho de Melo {
1478be199adaSArnaldo Carvalho de Melo 	return perf_evlist__set_filter_pids(evlist, 1, &pid);
1479be199adaSArnaldo Carvalho de Melo }
1480be199adaSArnaldo Carvalho de Melo 
14810c21f736SArnaldo Carvalho de Melo bool perf_evlist__valid_sample_type(struct perf_evlist *evlist)
148274429964SFrederic Weisbecker {
148375562573SAdrian Hunter 	struct perf_evsel *pos;
1484c2a70653SArnaldo Carvalho de Melo 
148575562573SAdrian Hunter 	if (evlist->nr_entries == 1)
148675562573SAdrian Hunter 		return true;
148775562573SAdrian Hunter 
148875562573SAdrian Hunter 	if (evlist->id_pos < 0 || evlist->is_pos < 0)
148975562573SAdrian Hunter 		return false;
149075562573SAdrian Hunter 
1491e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, pos) {
149275562573SAdrian Hunter 		if (pos->id_pos != evlist->id_pos ||
149375562573SAdrian Hunter 		    pos->is_pos != evlist->is_pos)
1494c2a70653SArnaldo Carvalho de Melo 			return false;
149574429964SFrederic Weisbecker 	}
149674429964SFrederic Weisbecker 
1497c2a70653SArnaldo Carvalho de Melo 	return true;
1498c2a70653SArnaldo Carvalho de Melo }
1499c2a70653SArnaldo Carvalho de Melo 
150075562573SAdrian Hunter u64 __perf_evlist__combined_sample_type(struct perf_evlist *evlist)
1501c2a70653SArnaldo Carvalho de Melo {
150275562573SAdrian Hunter 	struct perf_evsel *evsel;
150375562573SAdrian Hunter 
150475562573SAdrian Hunter 	if (evlist->combined_sample_type)
150575562573SAdrian Hunter 		return evlist->combined_sample_type;
150675562573SAdrian Hunter 
1507e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, evsel)
150875562573SAdrian Hunter 		evlist->combined_sample_type |= evsel->attr.sample_type;
150975562573SAdrian Hunter 
151075562573SAdrian Hunter 	return evlist->combined_sample_type;
151175562573SAdrian Hunter }
151275562573SAdrian Hunter 
151375562573SAdrian Hunter u64 perf_evlist__combined_sample_type(struct perf_evlist *evlist)
151475562573SAdrian Hunter {
151575562573SAdrian Hunter 	evlist->combined_sample_type = 0;
151675562573SAdrian Hunter 	return __perf_evlist__combined_sample_type(evlist);
1517c2a70653SArnaldo Carvalho de Melo }
1518c2a70653SArnaldo Carvalho de Melo 
151998df858eSAndi Kleen u64 perf_evlist__combined_branch_type(struct perf_evlist *evlist)
152098df858eSAndi Kleen {
152198df858eSAndi Kleen 	struct perf_evsel *evsel;
152298df858eSAndi Kleen 	u64 branch_type = 0;
152398df858eSAndi Kleen 
1524e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, evsel)
152598df858eSAndi Kleen 		branch_type |= evsel->attr.branch_sample_type;
152698df858eSAndi Kleen 	return branch_type;
152798df858eSAndi Kleen }
152898df858eSAndi Kleen 
15299ede473cSJiri Olsa bool perf_evlist__valid_read_format(struct perf_evlist *evlist)
15309ede473cSJiri Olsa {
15319ede473cSJiri Olsa 	struct perf_evsel *first = perf_evlist__first(evlist), *pos = first;
15329ede473cSJiri Olsa 	u64 read_format = first->attr.read_format;
15339ede473cSJiri Olsa 	u64 sample_type = first->attr.sample_type;
15349ede473cSJiri Olsa 
1535e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, pos) {
15369ede473cSJiri Olsa 		if (read_format != pos->attr.read_format)
15379ede473cSJiri Olsa 			return false;
15389ede473cSJiri Olsa 	}
15399ede473cSJiri Olsa 
15409ede473cSJiri Olsa 	/* PERF_SAMPLE_READ imples PERF_FORMAT_ID. */
15419ede473cSJiri Olsa 	if ((sample_type & PERF_SAMPLE_READ) &&
15429ede473cSJiri Olsa 	    !(read_format & PERF_FORMAT_ID)) {
15439ede473cSJiri Olsa 		return false;
15449ede473cSJiri Olsa 	}
15459ede473cSJiri Olsa 
15469ede473cSJiri Olsa 	return true;
15479ede473cSJiri Olsa }
15489ede473cSJiri Olsa 
15499ede473cSJiri Olsa u64 perf_evlist__read_format(struct perf_evlist *evlist)
15509ede473cSJiri Olsa {
15519ede473cSJiri Olsa 	struct perf_evsel *first = perf_evlist__first(evlist);
15529ede473cSJiri Olsa 	return first->attr.read_format;
15539ede473cSJiri Olsa }
15549ede473cSJiri Olsa 
15550c21f736SArnaldo Carvalho de Melo u16 perf_evlist__id_hdr_size(struct perf_evlist *evlist)
155681e36bffSArnaldo Carvalho de Melo {
15570c21f736SArnaldo Carvalho de Melo 	struct perf_evsel *first = perf_evlist__first(evlist);
155881e36bffSArnaldo Carvalho de Melo 	struct perf_sample *data;
155981e36bffSArnaldo Carvalho de Melo 	u64 sample_type;
156081e36bffSArnaldo Carvalho de Melo 	u16 size = 0;
156181e36bffSArnaldo Carvalho de Melo 
156281e36bffSArnaldo Carvalho de Melo 	if (!first->attr.sample_id_all)
156381e36bffSArnaldo Carvalho de Melo 		goto out;
156481e36bffSArnaldo Carvalho de Melo 
156581e36bffSArnaldo Carvalho de Melo 	sample_type = first->attr.sample_type;
156681e36bffSArnaldo Carvalho de Melo 
156781e36bffSArnaldo Carvalho de Melo 	if (sample_type & PERF_SAMPLE_TID)
156881e36bffSArnaldo Carvalho de Melo 		size += sizeof(data->tid) * 2;
156981e36bffSArnaldo Carvalho de Melo 
157081e36bffSArnaldo Carvalho de Melo        if (sample_type & PERF_SAMPLE_TIME)
157181e36bffSArnaldo Carvalho de Melo 		size += sizeof(data->time);
157281e36bffSArnaldo Carvalho de Melo 
157381e36bffSArnaldo Carvalho de Melo 	if (sample_type & PERF_SAMPLE_ID)
157481e36bffSArnaldo Carvalho de Melo 		size += sizeof(data->id);
157581e36bffSArnaldo Carvalho de Melo 
157681e36bffSArnaldo Carvalho de Melo 	if (sample_type & PERF_SAMPLE_STREAM_ID)
157781e36bffSArnaldo Carvalho de Melo 		size += sizeof(data->stream_id);
157881e36bffSArnaldo Carvalho de Melo 
157981e36bffSArnaldo Carvalho de Melo 	if (sample_type & PERF_SAMPLE_CPU)
158081e36bffSArnaldo Carvalho de Melo 		size += sizeof(data->cpu) * 2;
158175562573SAdrian Hunter 
158275562573SAdrian Hunter 	if (sample_type & PERF_SAMPLE_IDENTIFIER)
158375562573SAdrian Hunter 		size += sizeof(data->id);
158481e36bffSArnaldo Carvalho de Melo out:
158581e36bffSArnaldo Carvalho de Melo 	return size;
158681e36bffSArnaldo Carvalho de Melo }
158781e36bffSArnaldo Carvalho de Melo 
15880c21f736SArnaldo Carvalho de Melo bool perf_evlist__valid_sample_id_all(struct perf_evlist *evlist)
1589c2a70653SArnaldo Carvalho de Melo {
15900c21f736SArnaldo Carvalho de Melo 	struct perf_evsel *first = perf_evlist__first(evlist), *pos = first;
1591c2a70653SArnaldo Carvalho de Melo 
1592e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry_continue(evlist, pos) {
1593c2a70653SArnaldo Carvalho de Melo 		if (first->attr.sample_id_all != pos->attr.sample_id_all)
1594c2a70653SArnaldo Carvalho de Melo 			return false;
1595c2a70653SArnaldo Carvalho de Melo 	}
1596c2a70653SArnaldo Carvalho de Melo 
1597c2a70653SArnaldo Carvalho de Melo 	return true;
159874429964SFrederic Weisbecker }
159974429964SFrederic Weisbecker 
16000c21f736SArnaldo Carvalho de Melo bool perf_evlist__sample_id_all(struct perf_evlist *evlist)
160174429964SFrederic Weisbecker {
16020c21f736SArnaldo Carvalho de Melo 	struct perf_evsel *first = perf_evlist__first(evlist);
1603c2a70653SArnaldo Carvalho de Melo 	return first->attr.sample_id_all;
160474429964SFrederic Weisbecker }
160581cce8deSArnaldo Carvalho de Melo 
160681cce8deSArnaldo Carvalho de Melo void perf_evlist__set_selected(struct perf_evlist *evlist,
160781cce8deSArnaldo Carvalho de Melo 			       struct perf_evsel *evsel)
160881cce8deSArnaldo Carvalho de Melo {
160981cce8deSArnaldo Carvalho de Melo 	evlist->selected = evsel;
161081cce8deSArnaldo Carvalho de Melo }
1611727ab04eSArnaldo Carvalho de Melo 
1612a74b4b66SNamhyung Kim void perf_evlist__close(struct perf_evlist *evlist)
1613a74b4b66SNamhyung Kim {
1614a74b4b66SNamhyung Kim 	struct perf_evsel *evsel;
1615a74b4b66SNamhyung Kim 	int ncpus = cpu_map__nr(evlist->cpus);
1616a74b4b66SNamhyung Kim 	int nthreads = thread_map__nr(evlist->threads);
16178ad9219eSStephane Eranian 	int n;
1618a74b4b66SNamhyung Kim 
1619e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry_reverse(evlist, evsel) {
16208ad9219eSStephane Eranian 		n = evsel->cpus ? evsel->cpus->nr : ncpus;
16218ad9219eSStephane Eranian 		perf_evsel__close(evsel, n, nthreads);
16228ad9219eSStephane Eranian 	}
1623a74b4b66SNamhyung Kim }
1624a74b4b66SNamhyung Kim 
16254112eb18SArnaldo Carvalho de Melo static int perf_evlist__create_syswide_maps(struct perf_evlist *evlist)
16264112eb18SArnaldo Carvalho de Melo {
16278c0498b6SAdrian Hunter 	struct cpu_map	  *cpus;
16288c0498b6SAdrian Hunter 	struct thread_map *threads;
16294112eb18SArnaldo Carvalho de Melo 	int err = -ENOMEM;
16304112eb18SArnaldo Carvalho de Melo 
16314112eb18SArnaldo Carvalho de Melo 	/*
16324112eb18SArnaldo Carvalho de Melo 	 * Try reading /sys/devices/system/cpu/online to get
16334112eb18SArnaldo Carvalho de Melo 	 * an all cpus map.
16344112eb18SArnaldo Carvalho de Melo 	 *
16354112eb18SArnaldo Carvalho de Melo 	 * FIXME: -ENOMEM is the best we can do here, the cpu_map
16364112eb18SArnaldo Carvalho de Melo 	 * code needs an overhaul to properly forward the
16374112eb18SArnaldo Carvalho de Melo 	 * error, and we may not want to do that fallback to a
16384112eb18SArnaldo Carvalho de Melo 	 * default cpu identity map :-\
16394112eb18SArnaldo Carvalho de Melo 	 */
16408c0498b6SAdrian Hunter 	cpus = cpu_map__new(NULL);
16418c0498b6SAdrian Hunter 	if (!cpus)
16424112eb18SArnaldo Carvalho de Melo 		goto out;
16434112eb18SArnaldo Carvalho de Melo 
16448c0498b6SAdrian Hunter 	threads = thread_map__new_dummy();
16458c0498b6SAdrian Hunter 	if (!threads)
16468c0498b6SAdrian Hunter 		goto out_put;
16474112eb18SArnaldo Carvalho de Melo 
16488c0498b6SAdrian Hunter 	perf_evlist__set_maps(evlist, cpus, threads);
16494112eb18SArnaldo Carvalho de Melo out:
16504112eb18SArnaldo Carvalho de Melo 	return err;
16518c0498b6SAdrian Hunter out_put:
16528c0498b6SAdrian Hunter 	cpu_map__put(cpus);
16534112eb18SArnaldo Carvalho de Melo 	goto out;
16544112eb18SArnaldo Carvalho de Melo }
16554112eb18SArnaldo Carvalho de Melo 
16566a4bb04cSJiri Olsa int perf_evlist__open(struct perf_evlist *evlist)
1657727ab04eSArnaldo Carvalho de Melo {
16586a4bb04cSJiri Olsa 	struct perf_evsel *evsel;
1659a74b4b66SNamhyung Kim 	int err;
1660727ab04eSArnaldo Carvalho de Melo 
16614112eb18SArnaldo Carvalho de Melo 	/*
16624112eb18SArnaldo Carvalho de Melo 	 * Default: one fd per CPU, all threads, aka systemwide
16634112eb18SArnaldo Carvalho de Melo 	 * as sys_perf_event_open(cpu = -1, thread = -1) is EINVAL
16644112eb18SArnaldo Carvalho de Melo 	 */
16654112eb18SArnaldo Carvalho de Melo 	if (evlist->threads == NULL && evlist->cpus == NULL) {
16664112eb18SArnaldo Carvalho de Melo 		err = perf_evlist__create_syswide_maps(evlist);
16674112eb18SArnaldo Carvalho de Melo 		if (err < 0)
16684112eb18SArnaldo Carvalho de Melo 			goto out_err;
16694112eb18SArnaldo Carvalho de Melo 	}
16704112eb18SArnaldo Carvalho de Melo 
1671733cd2feSAdrian Hunter 	perf_evlist__update_id_pos(evlist);
1672733cd2feSAdrian Hunter 
1673e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, evsel) {
167423df7f79SAdrian Hunter 		err = perf_evsel__open(evsel, evsel->cpus, evsel->threads);
1675727ab04eSArnaldo Carvalho de Melo 		if (err < 0)
1676727ab04eSArnaldo Carvalho de Melo 			goto out_err;
1677727ab04eSArnaldo Carvalho de Melo 	}
1678727ab04eSArnaldo Carvalho de Melo 
1679727ab04eSArnaldo Carvalho de Melo 	return 0;
1680727ab04eSArnaldo Carvalho de Melo out_err:
1681a74b4b66SNamhyung Kim 	perf_evlist__close(evlist);
168241c21a68SNamhyung Kim 	errno = -err;
1683727ab04eSArnaldo Carvalho de Melo 	return err;
1684727ab04eSArnaldo Carvalho de Melo }
168535b9d88eSArnaldo Carvalho de Melo 
1686602ad878SArnaldo Carvalho de Melo int perf_evlist__prepare_workload(struct perf_evlist *evlist, struct target *target,
168755e162eaSNamhyung Kim 				  const char *argv[], bool pipe_output,
1688735f7e0bSArnaldo Carvalho de Melo 				  void (*exec_error)(int signo, siginfo_t *info, void *ucontext))
168935b9d88eSArnaldo Carvalho de Melo {
169035b9d88eSArnaldo Carvalho de Melo 	int child_ready_pipe[2], go_pipe[2];
169135b9d88eSArnaldo Carvalho de Melo 	char bf;
169235b9d88eSArnaldo Carvalho de Melo 
169335b9d88eSArnaldo Carvalho de Melo 	if (pipe(child_ready_pipe) < 0) {
169435b9d88eSArnaldo Carvalho de Melo 		perror("failed to create 'ready' pipe");
169535b9d88eSArnaldo Carvalho de Melo 		return -1;
169635b9d88eSArnaldo Carvalho de Melo 	}
169735b9d88eSArnaldo Carvalho de Melo 
169835b9d88eSArnaldo Carvalho de Melo 	if (pipe(go_pipe) < 0) {
169935b9d88eSArnaldo Carvalho de Melo 		perror("failed to create 'go' pipe");
170035b9d88eSArnaldo Carvalho de Melo 		goto out_close_ready_pipe;
170135b9d88eSArnaldo Carvalho de Melo 	}
170235b9d88eSArnaldo Carvalho de Melo 
170335b9d88eSArnaldo Carvalho de Melo 	evlist->workload.pid = fork();
170435b9d88eSArnaldo Carvalho de Melo 	if (evlist->workload.pid < 0) {
170535b9d88eSArnaldo Carvalho de Melo 		perror("failed to fork");
170635b9d88eSArnaldo Carvalho de Melo 		goto out_close_pipes;
170735b9d88eSArnaldo Carvalho de Melo 	}
170835b9d88eSArnaldo Carvalho de Melo 
170935b9d88eSArnaldo Carvalho de Melo 	if (!evlist->workload.pid) {
17105f1c4225SArnaldo Carvalho de Melo 		int ret;
17115f1c4225SArnaldo Carvalho de Melo 
1712119fa3c9SNamhyung Kim 		if (pipe_output)
171335b9d88eSArnaldo Carvalho de Melo 			dup2(2, 1);
171435b9d88eSArnaldo Carvalho de Melo 
17150817df08SDavid Ahern 		signal(SIGTERM, SIG_DFL);
17160817df08SDavid Ahern 
171735b9d88eSArnaldo Carvalho de Melo 		close(child_ready_pipe[0]);
171835b9d88eSArnaldo Carvalho de Melo 		close(go_pipe[1]);
171935b9d88eSArnaldo Carvalho de Melo 		fcntl(go_pipe[0], F_SETFD, FD_CLOEXEC);
172035b9d88eSArnaldo Carvalho de Melo 
172135b9d88eSArnaldo Carvalho de Melo 		/*
172235b9d88eSArnaldo Carvalho de Melo 		 * Tell the parent we're ready to go
172335b9d88eSArnaldo Carvalho de Melo 		 */
172435b9d88eSArnaldo Carvalho de Melo 		close(child_ready_pipe[1]);
172535b9d88eSArnaldo Carvalho de Melo 
172635b9d88eSArnaldo Carvalho de Melo 		/*
172735b9d88eSArnaldo Carvalho de Melo 		 * Wait until the parent tells us to go.
172835b9d88eSArnaldo Carvalho de Melo 		 */
17295f1c4225SArnaldo Carvalho de Melo 		ret = read(go_pipe[0], &bf, 1);
17305f1c4225SArnaldo Carvalho de Melo 		/*
17315f1c4225SArnaldo Carvalho de Melo 		 * The parent will ask for the execvp() to be performed by
17325f1c4225SArnaldo Carvalho de Melo 		 * writing exactly one byte, in workload.cork_fd, usually via
17335f1c4225SArnaldo Carvalho de Melo 		 * perf_evlist__start_workload().
17345f1c4225SArnaldo Carvalho de Melo 		 *
173520f86fc1SArnaldo Carvalho de Melo 		 * For cancelling the workload without actually running it,
17365f1c4225SArnaldo Carvalho de Melo 		 * the parent will just close workload.cork_fd, without writing
17375f1c4225SArnaldo Carvalho de Melo 		 * anything, i.e. read will return zero and we just exit()
17385f1c4225SArnaldo Carvalho de Melo 		 * here.
17395f1c4225SArnaldo Carvalho de Melo 		 */
17405f1c4225SArnaldo Carvalho de Melo 		if (ret != 1) {
17415f1c4225SArnaldo Carvalho de Melo 			if (ret == -1)
174235b9d88eSArnaldo Carvalho de Melo 				perror("unable to read pipe");
17435f1c4225SArnaldo Carvalho de Melo 			exit(ret);
17445f1c4225SArnaldo Carvalho de Melo 		}
174535b9d88eSArnaldo Carvalho de Melo 
174635b9d88eSArnaldo Carvalho de Melo 		execvp(argv[0], (char **)argv);
174735b9d88eSArnaldo Carvalho de Melo 
1748735f7e0bSArnaldo Carvalho de Melo 		if (exec_error) {
1749f33cbe72SArnaldo Carvalho de Melo 			union sigval val;
1750f33cbe72SArnaldo Carvalho de Melo 
1751f33cbe72SArnaldo Carvalho de Melo 			val.sival_int = errno;
1752f33cbe72SArnaldo Carvalho de Melo 			if (sigqueue(getppid(), SIGUSR1, val))
175335b9d88eSArnaldo Carvalho de Melo 				perror(argv[0]);
1754f33cbe72SArnaldo Carvalho de Melo 		} else
1755f33cbe72SArnaldo Carvalho de Melo 			perror(argv[0]);
175635b9d88eSArnaldo Carvalho de Melo 		exit(-1);
175735b9d88eSArnaldo Carvalho de Melo 	}
175835b9d88eSArnaldo Carvalho de Melo 
1759735f7e0bSArnaldo Carvalho de Melo 	if (exec_error) {
1760735f7e0bSArnaldo Carvalho de Melo 		struct sigaction act = {
1761735f7e0bSArnaldo Carvalho de Melo 			.sa_flags     = SA_SIGINFO,
1762735f7e0bSArnaldo Carvalho de Melo 			.sa_sigaction = exec_error,
1763735f7e0bSArnaldo Carvalho de Melo 		};
1764735f7e0bSArnaldo Carvalho de Melo 		sigaction(SIGUSR1, &act, NULL);
1765735f7e0bSArnaldo Carvalho de Melo 	}
1766735f7e0bSArnaldo Carvalho de Melo 
17671aaf63b1SArnaldo Carvalho de Melo 	if (target__none(target)) {
17681aaf63b1SArnaldo Carvalho de Melo 		if (evlist->threads == NULL) {
17691aaf63b1SArnaldo Carvalho de Melo 			fprintf(stderr, "FATAL: evlist->threads need to be set at this point (%s:%d).\n",
17701aaf63b1SArnaldo Carvalho de Melo 				__func__, __LINE__);
17711aaf63b1SArnaldo Carvalho de Melo 			goto out_close_pipes;
17721aaf63b1SArnaldo Carvalho de Melo 		}
1773e13798c7SJiri Olsa 		thread_map__set_pid(evlist->threads, 0, evlist->workload.pid);
17741aaf63b1SArnaldo Carvalho de Melo 	}
177535b9d88eSArnaldo Carvalho de Melo 
177635b9d88eSArnaldo Carvalho de Melo 	close(child_ready_pipe[1]);
177735b9d88eSArnaldo Carvalho de Melo 	close(go_pipe[0]);
177835b9d88eSArnaldo Carvalho de Melo 	/*
177935b9d88eSArnaldo Carvalho de Melo 	 * wait for child to settle
178035b9d88eSArnaldo Carvalho de Melo 	 */
178135b9d88eSArnaldo Carvalho de Melo 	if (read(child_ready_pipe[0], &bf, 1) == -1) {
178235b9d88eSArnaldo Carvalho de Melo 		perror("unable to read pipe");
178335b9d88eSArnaldo Carvalho de Melo 		goto out_close_pipes;
178435b9d88eSArnaldo Carvalho de Melo 	}
178535b9d88eSArnaldo Carvalho de Melo 
1786bcf3145fSNamhyung Kim 	fcntl(go_pipe[1], F_SETFD, FD_CLOEXEC);
178735b9d88eSArnaldo Carvalho de Melo 	evlist->workload.cork_fd = go_pipe[1];
178835b9d88eSArnaldo Carvalho de Melo 	close(child_ready_pipe[0]);
178935b9d88eSArnaldo Carvalho de Melo 	return 0;
179035b9d88eSArnaldo Carvalho de Melo 
179135b9d88eSArnaldo Carvalho de Melo out_close_pipes:
179235b9d88eSArnaldo Carvalho de Melo 	close(go_pipe[0]);
179335b9d88eSArnaldo Carvalho de Melo 	close(go_pipe[1]);
179435b9d88eSArnaldo Carvalho de Melo out_close_ready_pipe:
179535b9d88eSArnaldo Carvalho de Melo 	close(child_ready_pipe[0]);
179635b9d88eSArnaldo Carvalho de Melo 	close(child_ready_pipe[1]);
179735b9d88eSArnaldo Carvalho de Melo 	return -1;
179835b9d88eSArnaldo Carvalho de Melo }
179935b9d88eSArnaldo Carvalho de Melo 
180035b9d88eSArnaldo Carvalho de Melo int perf_evlist__start_workload(struct perf_evlist *evlist)
180135b9d88eSArnaldo Carvalho de Melo {
180235b9d88eSArnaldo Carvalho de Melo 	if (evlist->workload.cork_fd > 0) {
1803b3824404SDavid Ahern 		char bf = 0;
1804bcf3145fSNamhyung Kim 		int ret;
180535b9d88eSArnaldo Carvalho de Melo 		/*
180635b9d88eSArnaldo Carvalho de Melo 		 * Remove the cork, let it rip!
180735b9d88eSArnaldo Carvalho de Melo 		 */
1808bcf3145fSNamhyung Kim 		ret = write(evlist->workload.cork_fd, &bf, 1);
1809bcf3145fSNamhyung Kim 		if (ret < 0)
1810bcf3145fSNamhyung Kim 			perror("enable to write to pipe");
1811bcf3145fSNamhyung Kim 
1812bcf3145fSNamhyung Kim 		close(evlist->workload.cork_fd);
1813bcf3145fSNamhyung Kim 		return ret;
181435b9d88eSArnaldo Carvalho de Melo 	}
181535b9d88eSArnaldo Carvalho de Melo 
181635b9d88eSArnaldo Carvalho de Melo 	return 0;
181735b9d88eSArnaldo Carvalho de Melo }
1818cb0b29e0SArnaldo Carvalho de Melo 
1819a3f698feSArnaldo Carvalho de Melo int perf_evlist__parse_sample(struct perf_evlist *evlist, union perf_event *event,
18200807d2d8SArnaldo Carvalho de Melo 			      struct perf_sample *sample)
1821cb0b29e0SArnaldo Carvalho de Melo {
182275562573SAdrian Hunter 	struct perf_evsel *evsel = perf_evlist__event2evsel(evlist, event);
182375562573SAdrian Hunter 
182475562573SAdrian Hunter 	if (!evsel)
182575562573SAdrian Hunter 		return -EFAULT;
18260807d2d8SArnaldo Carvalho de Melo 	return perf_evsel__parse_sample(evsel, event, sample);
1827cb0b29e0SArnaldo Carvalho de Melo }
182878f067b3SArnaldo Carvalho de Melo 
182978f067b3SArnaldo Carvalho de Melo size_t perf_evlist__fprintf(struct perf_evlist *evlist, FILE *fp)
183078f067b3SArnaldo Carvalho de Melo {
183178f067b3SArnaldo Carvalho de Melo 	struct perf_evsel *evsel;
183278f067b3SArnaldo Carvalho de Melo 	size_t printed = 0;
183378f067b3SArnaldo Carvalho de Melo 
1834e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, evsel) {
183578f067b3SArnaldo Carvalho de Melo 		printed += fprintf(fp, "%s%s", evsel->idx ? ", " : "",
183678f067b3SArnaldo Carvalho de Melo 				   perf_evsel__name(evsel));
183778f067b3SArnaldo Carvalho de Melo 	}
183878f067b3SArnaldo Carvalho de Melo 
1839b2222139SDavidlohr Bueso 	return printed + fprintf(fp, "\n");
184078f067b3SArnaldo Carvalho de Melo }
18416ef068cbSArnaldo Carvalho de Melo 
1842d9aade7fSArnaldo Carvalho de Melo int perf_evlist__strerror_open(struct perf_evlist *evlist,
1843a8f23d8fSArnaldo Carvalho de Melo 			       int err, char *buf, size_t size)
1844a8f23d8fSArnaldo Carvalho de Melo {
1845a8f23d8fSArnaldo Carvalho de Melo 	int printed, value;
1846c8b5f2c9SArnaldo Carvalho de Melo 	char sbuf[STRERR_BUFSIZE], *emsg = str_error_r(err, sbuf, sizeof(sbuf));
1847a8f23d8fSArnaldo Carvalho de Melo 
1848a8f23d8fSArnaldo Carvalho de Melo 	switch (err) {
1849a8f23d8fSArnaldo Carvalho de Melo 	case EACCES:
1850a8f23d8fSArnaldo Carvalho de Melo 	case EPERM:
1851a8f23d8fSArnaldo Carvalho de Melo 		printed = scnprintf(buf, size,
1852a8f23d8fSArnaldo Carvalho de Melo 				    "Error:\t%s.\n"
1853a8f23d8fSArnaldo Carvalho de Melo 				    "Hint:\tCheck /proc/sys/kernel/perf_event_paranoid setting.", emsg);
1854a8f23d8fSArnaldo Carvalho de Melo 
18551a47245dSAdrian Hunter 		value = perf_event_paranoid();
1856a8f23d8fSArnaldo Carvalho de Melo 
1857a8f23d8fSArnaldo Carvalho de Melo 		printed += scnprintf(buf + printed, size - printed, "\nHint:\t");
1858a8f23d8fSArnaldo Carvalho de Melo 
1859a8f23d8fSArnaldo Carvalho de Melo 		if (value >= 2) {
1860a8f23d8fSArnaldo Carvalho de Melo 			printed += scnprintf(buf + printed, size - printed,
1861a8f23d8fSArnaldo Carvalho de Melo 					     "For your workloads it needs to be <= 1\nHint:\t");
1862a8f23d8fSArnaldo Carvalho de Melo 		}
1863a8f23d8fSArnaldo Carvalho de Melo 		printed += scnprintf(buf + printed, size - printed,
18645229e366SArnaldo Carvalho de Melo 				     "For system wide tracing it needs to be set to -1.\n");
1865a8f23d8fSArnaldo Carvalho de Melo 
1866a8f23d8fSArnaldo Carvalho de Melo 		printed += scnprintf(buf + printed, size - printed,
18675229e366SArnaldo Carvalho de Melo 				    "Hint:\tTry: 'sudo sh -c \"echo -1 > /proc/sys/kernel/perf_event_paranoid\"'\n"
18685229e366SArnaldo Carvalho de Melo 				    "Hint:\tThe current value is %d.", value);
1869a8f23d8fSArnaldo Carvalho de Melo 		break;
1870d9aade7fSArnaldo Carvalho de Melo 	case EINVAL: {
1871d9aade7fSArnaldo Carvalho de Melo 		struct perf_evsel *first = perf_evlist__first(evlist);
1872d9aade7fSArnaldo Carvalho de Melo 		int max_freq;
1873d9aade7fSArnaldo Carvalho de Melo 
1874d9aade7fSArnaldo Carvalho de Melo 		if (sysctl__read_int("kernel/perf_event_max_sample_rate", &max_freq) < 0)
1875d9aade7fSArnaldo Carvalho de Melo 			goto out_default;
1876d9aade7fSArnaldo Carvalho de Melo 
1877d9aade7fSArnaldo Carvalho de Melo 		if (first->attr.sample_freq < (u64)max_freq)
1878d9aade7fSArnaldo Carvalho de Melo 			goto out_default;
1879d9aade7fSArnaldo Carvalho de Melo 
1880d9aade7fSArnaldo Carvalho de Melo 		printed = scnprintf(buf, size,
1881d9aade7fSArnaldo Carvalho de Melo 				    "Error:\t%s.\n"
1882d9aade7fSArnaldo Carvalho de Melo 				    "Hint:\tCheck /proc/sys/kernel/perf_event_max_sample_rate.\n"
1883d9aade7fSArnaldo Carvalho de Melo 				    "Hint:\tThe current value is %d and %" PRIu64 " is being requested.",
1884d9aade7fSArnaldo Carvalho de Melo 				    emsg, max_freq, first->attr.sample_freq);
1885d9aade7fSArnaldo Carvalho de Melo 		break;
1886d9aade7fSArnaldo Carvalho de Melo 	}
1887a8f23d8fSArnaldo Carvalho de Melo 	default:
1888d9aade7fSArnaldo Carvalho de Melo out_default:
1889a8f23d8fSArnaldo Carvalho de Melo 		scnprintf(buf, size, "%s", emsg);
1890a8f23d8fSArnaldo Carvalho de Melo 		break;
1891a8f23d8fSArnaldo Carvalho de Melo 	}
1892a8f23d8fSArnaldo Carvalho de Melo 
1893a8f23d8fSArnaldo Carvalho de Melo 	return 0;
1894a8f23d8fSArnaldo Carvalho de Melo }
1895a025e4f0SAdrian Hunter 
1896956fa571SArnaldo Carvalho de Melo int perf_evlist__strerror_mmap(struct perf_evlist *evlist, int err, char *buf, size_t size)
1897956fa571SArnaldo Carvalho de Melo {
1898c8b5f2c9SArnaldo Carvalho de Melo 	char sbuf[STRERR_BUFSIZE], *emsg = str_error_r(err, sbuf, sizeof(sbuf));
1899e965bea1SArnaldo Carvalho de Melo 	int pages_attempted = evlist->mmap_len / 1024, pages_max_per_user, printed = 0;
1900956fa571SArnaldo Carvalho de Melo 
1901956fa571SArnaldo Carvalho de Melo 	switch (err) {
1902956fa571SArnaldo Carvalho de Melo 	case EPERM:
1903e5d4a290SArnaldo Carvalho de Melo 		sysctl__read_int("kernel/perf_event_mlock_kb", &pages_max_per_user);
1904e965bea1SArnaldo Carvalho de Melo 		printed += scnprintf(buf + printed, size - printed,
1905e965bea1SArnaldo Carvalho de Melo 				     "Error:\t%s.\n"
1906956fa571SArnaldo Carvalho de Melo 				     "Hint:\tCheck /proc/sys/kernel/perf_event_mlock_kb (%d kB) setting.\n"
1907e965bea1SArnaldo Carvalho de Melo 				     "Hint:\tTried using %zd kB.\n",
1908e5d4a290SArnaldo Carvalho de Melo 				     emsg, pages_max_per_user, pages_attempted);
1909e965bea1SArnaldo Carvalho de Melo 
1910e965bea1SArnaldo Carvalho de Melo 		if (pages_attempted >= pages_max_per_user) {
1911e965bea1SArnaldo Carvalho de Melo 			printed += scnprintf(buf + printed, size - printed,
1912e965bea1SArnaldo Carvalho de Melo 					     "Hint:\tTry 'sudo sh -c \"echo %d > /proc/sys/kernel/perf_event_mlock_kb\"', or\n",
1913e965bea1SArnaldo Carvalho de Melo 					     pages_max_per_user + pages_attempted);
1914e965bea1SArnaldo Carvalho de Melo 		}
1915e965bea1SArnaldo Carvalho de Melo 
1916e965bea1SArnaldo Carvalho de Melo 		printed += scnprintf(buf + printed, size - printed,
1917e965bea1SArnaldo Carvalho de Melo 				     "Hint:\tTry using a smaller -m/--mmap-pages value.");
1918956fa571SArnaldo Carvalho de Melo 		break;
1919956fa571SArnaldo Carvalho de Melo 	default:
1920956fa571SArnaldo Carvalho de Melo 		scnprintf(buf, size, "%s", emsg);
1921956fa571SArnaldo Carvalho de Melo 		break;
1922956fa571SArnaldo Carvalho de Melo 	}
1923956fa571SArnaldo Carvalho de Melo 
1924956fa571SArnaldo Carvalho de Melo 	return 0;
1925956fa571SArnaldo Carvalho de Melo }
1926956fa571SArnaldo Carvalho de Melo 
1927a025e4f0SAdrian Hunter void perf_evlist__to_front(struct perf_evlist *evlist,
1928a025e4f0SAdrian Hunter 			   struct perf_evsel *move_evsel)
1929a025e4f0SAdrian Hunter {
1930a025e4f0SAdrian Hunter 	struct perf_evsel *evsel, *n;
1931a025e4f0SAdrian Hunter 	LIST_HEAD(move);
1932a025e4f0SAdrian Hunter 
1933a025e4f0SAdrian Hunter 	if (move_evsel == perf_evlist__first(evlist))
1934a025e4f0SAdrian Hunter 		return;
1935a025e4f0SAdrian Hunter 
1936e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry_safe(evlist, n, evsel) {
1937a025e4f0SAdrian Hunter 		if (evsel->leader == move_evsel->leader)
1938a025e4f0SAdrian Hunter 			list_move_tail(&evsel->node, &move);
1939a025e4f0SAdrian Hunter 	}
1940a025e4f0SAdrian Hunter 
1941a025e4f0SAdrian Hunter 	list_splice(&move, &evlist->entries);
1942a025e4f0SAdrian Hunter }
194360b0896cSAdrian Hunter 
194460b0896cSAdrian Hunter void perf_evlist__set_tracking_event(struct perf_evlist *evlist,
194560b0896cSAdrian Hunter 				     struct perf_evsel *tracking_evsel)
194660b0896cSAdrian Hunter {
194760b0896cSAdrian Hunter 	struct perf_evsel *evsel;
194860b0896cSAdrian Hunter 
194960b0896cSAdrian Hunter 	if (tracking_evsel->tracking)
195060b0896cSAdrian Hunter 		return;
195160b0896cSAdrian Hunter 
1952e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, evsel) {
195360b0896cSAdrian Hunter 		if (evsel != tracking_evsel)
195460b0896cSAdrian Hunter 			evsel->tracking = false;
195560b0896cSAdrian Hunter 	}
195660b0896cSAdrian Hunter 
195760b0896cSAdrian Hunter 	tracking_evsel->tracking = true;
195860b0896cSAdrian Hunter }
19597630b3e2SWang Nan 
19607630b3e2SWang Nan struct perf_evsel *
19617630b3e2SWang Nan perf_evlist__find_evsel_by_str(struct perf_evlist *evlist,
19627630b3e2SWang Nan 			       const char *str)
19637630b3e2SWang Nan {
19647630b3e2SWang Nan 	struct perf_evsel *evsel;
19657630b3e2SWang Nan 
1966e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, evsel) {
19677630b3e2SWang Nan 		if (!evsel->name)
19687630b3e2SWang Nan 			continue;
19697630b3e2SWang Nan 		if (strcmp(str, evsel->name) == 0)
19707630b3e2SWang Nan 			return evsel;
19717630b3e2SWang Nan 	}
19727630b3e2SWang Nan 
19737630b3e2SWang Nan 	return NULL;
19747630b3e2SWang Nan }
1975