xref: /openbmc/linux/tools/perf/util/evlist.c (revision b2cb615d)
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);
318db6d6b1SWang Nan static void perf_mmap__munmap(struct perf_mmap *map);
324876075bSWang Nan static void perf_mmap__put(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);
126b2cb615dSWang Nan 	zfree(&evlist->backward_mmap);
1271b85337dSArnaldo Carvalho de Melo 	fdarray__exit(&evlist->pollfd);
128ef1d1af2SArnaldo Carvalho de Melo }
129ef1d1af2SArnaldo Carvalho de Melo 
130361c99a6SArnaldo Carvalho de Melo void perf_evlist__delete(struct perf_evlist *evlist)
131361c99a6SArnaldo Carvalho de Melo {
1320b04b3dcSArnaldo Carvalho de Melo 	if (evlist == NULL)
1330b04b3dcSArnaldo Carvalho de Melo 		return;
1340b04b3dcSArnaldo Carvalho de Melo 
135983874d1SArnaldo Carvalho de Melo 	perf_evlist__munmap(evlist);
136f26e1c7cSArnaldo Carvalho de Melo 	perf_evlist__close(evlist);
137f30a79b0SJiri Olsa 	cpu_map__put(evlist->cpus);
138186fbb74SJiri Olsa 	thread_map__put(evlist->threads);
13903ad9747SArnaldo Carvalho de Melo 	evlist->cpus = NULL;
14003ad9747SArnaldo Carvalho de Melo 	evlist->threads = NULL;
141361c99a6SArnaldo Carvalho de Melo 	perf_evlist__purge(evlist);
142ef1d1af2SArnaldo Carvalho de Melo 	perf_evlist__exit(evlist);
143361c99a6SArnaldo Carvalho de Melo 	free(evlist);
144361c99a6SArnaldo Carvalho de Melo }
145361c99a6SArnaldo Carvalho de Melo 
146adc0c3e8SAdrian Hunter static void __perf_evlist__propagate_maps(struct perf_evlist *evlist,
147adc0c3e8SAdrian Hunter 					  struct perf_evsel *evsel)
148adc0c3e8SAdrian Hunter {
149adc0c3e8SAdrian Hunter 	/*
150adc0c3e8SAdrian Hunter 	 * We already have cpus for evsel (via PMU sysfs) so
151adc0c3e8SAdrian Hunter 	 * keep it, if there's no target cpu list defined.
152adc0c3e8SAdrian Hunter 	 */
153adc0c3e8SAdrian Hunter 	if (!evsel->own_cpus || evlist->has_user_cpus) {
154adc0c3e8SAdrian Hunter 		cpu_map__put(evsel->cpus);
155adc0c3e8SAdrian Hunter 		evsel->cpus = cpu_map__get(evlist->cpus);
156adc0c3e8SAdrian Hunter 	} else if (evsel->cpus != evsel->own_cpus) {
157adc0c3e8SAdrian Hunter 		cpu_map__put(evsel->cpus);
158adc0c3e8SAdrian Hunter 		evsel->cpus = cpu_map__get(evsel->own_cpus);
159adc0c3e8SAdrian Hunter 	}
160adc0c3e8SAdrian Hunter 
161adc0c3e8SAdrian Hunter 	thread_map__put(evsel->threads);
162adc0c3e8SAdrian Hunter 	evsel->threads = thread_map__get(evlist->threads);
163adc0c3e8SAdrian Hunter }
164adc0c3e8SAdrian Hunter 
165adc0c3e8SAdrian Hunter static void perf_evlist__propagate_maps(struct perf_evlist *evlist)
166adc0c3e8SAdrian Hunter {
167adc0c3e8SAdrian Hunter 	struct perf_evsel *evsel;
168adc0c3e8SAdrian Hunter 
169e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, evsel)
170adc0c3e8SAdrian Hunter 		__perf_evlist__propagate_maps(evlist, evsel);
171adc0c3e8SAdrian Hunter }
172adc0c3e8SAdrian Hunter 
173361c99a6SArnaldo Carvalho de Melo void perf_evlist__add(struct perf_evlist *evlist, struct perf_evsel *entry)
174361c99a6SArnaldo Carvalho de Melo {
175d49e4695SArnaldo Carvalho de Melo 	entry->evlist = evlist;
176361c99a6SArnaldo Carvalho de Melo 	list_add_tail(&entry->node, &evlist->entries);
177ef503831SArnaldo Carvalho de Melo 	entry->idx = evlist->nr_entries;
17860b0896cSAdrian Hunter 	entry->tracking = !entry->idx;
179ef503831SArnaldo Carvalho de Melo 
18075562573SAdrian Hunter 	if (!evlist->nr_entries++)
18175562573SAdrian Hunter 		perf_evlist__set_id_pos(evlist);
18244c42d71SAdrian Hunter 
18344c42d71SAdrian Hunter 	__perf_evlist__propagate_maps(evlist, entry);
184361c99a6SArnaldo Carvalho de Melo }
185361c99a6SArnaldo Carvalho de Melo 
1864768230aSAdrian Hunter void perf_evlist__remove(struct perf_evlist *evlist, struct perf_evsel *evsel)
1874768230aSAdrian Hunter {
1884768230aSAdrian Hunter 	evsel->evlist = NULL;
1894768230aSAdrian Hunter 	list_del_init(&evsel->node);
1904768230aSAdrian Hunter 	evlist->nr_entries -= 1;
1914768230aSAdrian Hunter }
1924768230aSAdrian Hunter 
1930529bc1fSJiri Olsa void perf_evlist__splice_list_tail(struct perf_evlist *evlist,
194f114d6efSAdrian Hunter 				   struct list_head *list)
19550d08e47SArnaldo Carvalho de Melo {
196f114d6efSAdrian Hunter 	struct perf_evsel *evsel, *temp;
19775562573SAdrian Hunter 
198e5cadb93SArnaldo Carvalho de Melo 	__evlist__for_each_entry_safe(list, temp, evsel) {
199f114d6efSAdrian Hunter 		list_del_init(&evsel->node);
200f114d6efSAdrian Hunter 		perf_evlist__add(evlist, evsel);
201f114d6efSAdrian Hunter 	}
20250d08e47SArnaldo Carvalho de Melo }
20350d08e47SArnaldo Carvalho de Melo 
20463dab225SArnaldo Carvalho de Melo void __perf_evlist__set_leader(struct list_head *list)
20563dab225SArnaldo Carvalho de Melo {
20663dab225SArnaldo Carvalho de Melo 	struct perf_evsel *evsel, *leader;
20763dab225SArnaldo Carvalho de Melo 
20863dab225SArnaldo Carvalho de Melo 	leader = list_entry(list->next, struct perf_evsel, node);
20997f63e4aSNamhyung Kim 	evsel = list_entry(list->prev, struct perf_evsel, node);
21097f63e4aSNamhyung Kim 
21197f63e4aSNamhyung Kim 	leader->nr_members = evsel->idx - leader->idx + 1;
21263dab225SArnaldo Carvalho de Melo 
213e5cadb93SArnaldo Carvalho de Melo 	__evlist__for_each_entry(list, evsel) {
21463dab225SArnaldo Carvalho de Melo 		evsel->leader = leader;
21563dab225SArnaldo Carvalho de Melo 	}
21663dab225SArnaldo Carvalho de Melo }
21763dab225SArnaldo Carvalho de Melo 
21863dab225SArnaldo Carvalho de Melo void perf_evlist__set_leader(struct perf_evlist *evlist)
2196a4bb04cSJiri Olsa {
22097f63e4aSNamhyung Kim 	if (evlist->nr_entries) {
22197f63e4aSNamhyung Kim 		evlist->nr_groups = evlist->nr_entries > 1 ? 1 : 0;
22263dab225SArnaldo Carvalho de Melo 		__perf_evlist__set_leader(&evlist->entries);
2236a4bb04cSJiri Olsa 	}
22497f63e4aSNamhyung Kim }
2256a4bb04cSJiri Olsa 
22645cf6c33SJiri Olsa void perf_event_attr__set_max_precise_ip(struct perf_event_attr *attr)
2277f8d1adeSArnaldo Carvalho de Melo {
2287f8d1adeSArnaldo Carvalho de Melo 	attr->precise_ip = 3;
2297f8d1adeSArnaldo Carvalho de Melo 
2307f8d1adeSArnaldo Carvalho de Melo 	while (attr->precise_ip != 0) {
2317f8d1adeSArnaldo Carvalho de Melo 		int fd = sys_perf_event_open(attr, 0, -1, -1, 0);
2327f8d1adeSArnaldo Carvalho de Melo 		if (fd != -1) {
2337f8d1adeSArnaldo Carvalho de Melo 			close(fd);
2347f8d1adeSArnaldo Carvalho de Melo 			break;
2357f8d1adeSArnaldo Carvalho de Melo 		}
2367f8d1adeSArnaldo Carvalho de Melo 		--attr->precise_ip;
2377f8d1adeSArnaldo Carvalho de Melo 	}
2387f8d1adeSArnaldo Carvalho de Melo }
2397f8d1adeSArnaldo Carvalho de Melo 
240361c99a6SArnaldo Carvalho de Melo int perf_evlist__add_default(struct perf_evlist *evlist)
241361c99a6SArnaldo Carvalho de Melo {
242361c99a6SArnaldo Carvalho de Melo 	struct perf_event_attr attr = {
243361c99a6SArnaldo Carvalho de Melo 		.type = PERF_TYPE_HARDWARE,
244361c99a6SArnaldo Carvalho de Melo 		.config = PERF_COUNT_HW_CPU_CYCLES,
245361c99a6SArnaldo Carvalho de Melo 	};
2461aed2671SJoerg Roedel 	struct perf_evsel *evsel;
247361c99a6SArnaldo Carvalho de Melo 
2481aed2671SJoerg Roedel 	event_attr_init(&attr);
2491aed2671SJoerg Roedel 
2507f8d1adeSArnaldo Carvalho de Melo 	perf_event_attr__set_max_precise_ip(&attr);
2517f8d1adeSArnaldo Carvalho de Melo 
252ef503831SArnaldo Carvalho de Melo 	evsel = perf_evsel__new(&attr);
253361c99a6SArnaldo Carvalho de Melo 	if (evsel == NULL)
254cc2d86b0SStephane Eranian 		goto error;
255cc2d86b0SStephane Eranian 
2567f8d1adeSArnaldo Carvalho de Melo 	/* use asprintf() because free(evsel) assumes name is allocated */
2577f8d1adeSArnaldo Carvalho de Melo 	if (asprintf(&evsel->name, "cycles%.*s",
2587f8d1adeSArnaldo Carvalho de Melo 		     attr.precise_ip ? attr.precise_ip + 1 : 0, ":ppp") < 0)
259cc2d86b0SStephane Eranian 		goto error_free;
260361c99a6SArnaldo Carvalho de Melo 
261361c99a6SArnaldo Carvalho de Melo 	perf_evlist__add(evlist, evsel);
262361c99a6SArnaldo Carvalho de Melo 	return 0;
263cc2d86b0SStephane Eranian error_free:
264cc2d86b0SStephane Eranian 	perf_evsel__delete(evsel);
265cc2d86b0SStephane Eranian error:
266cc2d86b0SStephane Eranian 	return -ENOMEM;
267361c99a6SArnaldo Carvalho de Melo }
2685c581041SArnaldo Carvalho de Melo 
2695bae0250SArnaldo Carvalho de Melo int perf_evlist__add_dummy(struct perf_evlist *evlist)
2705bae0250SArnaldo Carvalho de Melo {
2715bae0250SArnaldo Carvalho de Melo 	struct perf_event_attr attr = {
2725bae0250SArnaldo Carvalho de Melo 		.type	= PERF_TYPE_SOFTWARE,
2735bae0250SArnaldo Carvalho de Melo 		.config = PERF_COUNT_SW_DUMMY,
2745bae0250SArnaldo Carvalho de Melo 		.size	= sizeof(attr), /* to capture ABI version */
2755bae0250SArnaldo Carvalho de Melo 	};
2765bae0250SArnaldo Carvalho de Melo 	struct perf_evsel *evsel = perf_evsel__new(&attr);
2775bae0250SArnaldo Carvalho de Melo 
2785bae0250SArnaldo Carvalho de Melo 	if (evsel == NULL)
2795bae0250SArnaldo Carvalho de Melo 		return -ENOMEM;
2805bae0250SArnaldo Carvalho de Melo 
2815bae0250SArnaldo Carvalho de Melo 	perf_evlist__add(evlist, evsel);
2825bae0250SArnaldo Carvalho de Melo 	return 0;
2835bae0250SArnaldo Carvalho de Melo }
2845bae0250SArnaldo Carvalho de Melo 
285e60fc847SArnaldo Carvalho de Melo static int perf_evlist__add_attrs(struct perf_evlist *evlist,
28650d08e47SArnaldo Carvalho de Melo 				  struct perf_event_attr *attrs, size_t nr_attrs)
28750d08e47SArnaldo Carvalho de Melo {
28850d08e47SArnaldo Carvalho de Melo 	struct perf_evsel *evsel, *n;
28950d08e47SArnaldo Carvalho de Melo 	LIST_HEAD(head);
29050d08e47SArnaldo Carvalho de Melo 	size_t i;
29150d08e47SArnaldo Carvalho de Melo 
29250d08e47SArnaldo Carvalho de Melo 	for (i = 0; i < nr_attrs; i++) {
293ef503831SArnaldo Carvalho de Melo 		evsel = perf_evsel__new_idx(attrs + i, evlist->nr_entries + i);
29450d08e47SArnaldo Carvalho de Melo 		if (evsel == NULL)
29550d08e47SArnaldo Carvalho de Melo 			goto out_delete_partial_list;
29650d08e47SArnaldo Carvalho de Melo 		list_add_tail(&evsel->node, &head);
29750d08e47SArnaldo Carvalho de Melo 	}
29850d08e47SArnaldo Carvalho de Melo 
299f114d6efSAdrian Hunter 	perf_evlist__splice_list_tail(evlist, &head);
30050d08e47SArnaldo Carvalho de Melo 
30150d08e47SArnaldo Carvalho de Melo 	return 0;
30250d08e47SArnaldo Carvalho de Melo 
30350d08e47SArnaldo Carvalho de Melo out_delete_partial_list:
304e5cadb93SArnaldo Carvalho de Melo 	__evlist__for_each_entry_safe(&head, n, evsel)
30550d08e47SArnaldo Carvalho de Melo 		perf_evsel__delete(evsel);
30650d08e47SArnaldo Carvalho de Melo 	return -1;
30750d08e47SArnaldo Carvalho de Melo }
30850d08e47SArnaldo Carvalho de Melo 
30979695e1bSArnaldo Carvalho de Melo int __perf_evlist__add_default_attrs(struct perf_evlist *evlist,
31079695e1bSArnaldo Carvalho de Melo 				     struct perf_event_attr *attrs, size_t nr_attrs)
31179695e1bSArnaldo Carvalho de Melo {
31279695e1bSArnaldo Carvalho de Melo 	size_t i;
31379695e1bSArnaldo Carvalho de Melo 
31479695e1bSArnaldo Carvalho de Melo 	for (i = 0; i < nr_attrs; i++)
31579695e1bSArnaldo Carvalho de Melo 		event_attr_init(attrs + i);
31679695e1bSArnaldo Carvalho de Melo 
31779695e1bSArnaldo Carvalho de Melo 	return perf_evlist__add_attrs(evlist, attrs, nr_attrs);
31879695e1bSArnaldo Carvalho de Melo }
31979695e1bSArnaldo Carvalho de Melo 
320da378962SArnaldo Carvalho de Melo struct perf_evsel *
321ee29be62SArnaldo Carvalho de Melo perf_evlist__find_tracepoint_by_id(struct perf_evlist *evlist, int id)
322ee29be62SArnaldo Carvalho de Melo {
323ee29be62SArnaldo Carvalho de Melo 	struct perf_evsel *evsel;
324ee29be62SArnaldo Carvalho de Melo 
325e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, evsel) {
326ee29be62SArnaldo Carvalho de Melo 		if (evsel->attr.type   == PERF_TYPE_TRACEPOINT &&
327ee29be62SArnaldo Carvalho de Melo 		    (int)evsel->attr.config == id)
328ee29be62SArnaldo Carvalho de Melo 			return evsel;
329ee29be62SArnaldo Carvalho de Melo 	}
330ee29be62SArnaldo Carvalho de Melo 
331ee29be62SArnaldo Carvalho de Melo 	return NULL;
332ee29be62SArnaldo Carvalho de Melo }
333ee29be62SArnaldo Carvalho de Melo 
334a2f2804aSDavid Ahern struct perf_evsel *
335a2f2804aSDavid Ahern perf_evlist__find_tracepoint_by_name(struct perf_evlist *evlist,
336a2f2804aSDavid Ahern 				     const char *name)
337a2f2804aSDavid Ahern {
338a2f2804aSDavid Ahern 	struct perf_evsel *evsel;
339a2f2804aSDavid Ahern 
340e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, evsel) {
341a2f2804aSDavid Ahern 		if ((evsel->attr.type == PERF_TYPE_TRACEPOINT) &&
342a2f2804aSDavid Ahern 		    (strcmp(evsel->name, name) == 0))
343a2f2804aSDavid Ahern 			return evsel;
344a2f2804aSDavid Ahern 	}
345a2f2804aSDavid Ahern 
346a2f2804aSDavid Ahern 	return NULL;
347a2f2804aSDavid Ahern }
348a2f2804aSDavid Ahern 
34939876e7dSArnaldo Carvalho de Melo int perf_evlist__add_newtp(struct perf_evlist *evlist,
35039876e7dSArnaldo Carvalho de Melo 			   const char *sys, const char *name, void *handler)
35139876e7dSArnaldo Carvalho de Melo {
352ef503831SArnaldo Carvalho de Melo 	struct perf_evsel *evsel = perf_evsel__newtp(sys, name);
35339876e7dSArnaldo Carvalho de Melo 
3548dd2a131SJiri Olsa 	if (IS_ERR(evsel))
35539876e7dSArnaldo Carvalho de Melo 		return -1;
35639876e7dSArnaldo Carvalho de Melo 
357744a9719SArnaldo Carvalho de Melo 	evsel->handler = handler;
35839876e7dSArnaldo Carvalho de Melo 	perf_evlist__add(evlist, evsel);
35939876e7dSArnaldo Carvalho de Melo 	return 0;
36039876e7dSArnaldo Carvalho de Melo }
36139876e7dSArnaldo Carvalho de Melo 
362bf8e8f4bSAdrian Hunter static int perf_evlist__nr_threads(struct perf_evlist *evlist,
363bf8e8f4bSAdrian Hunter 				   struct perf_evsel *evsel)
364bf8e8f4bSAdrian Hunter {
365bf8e8f4bSAdrian Hunter 	if (evsel->system_wide)
366bf8e8f4bSAdrian Hunter 		return 1;
367bf8e8f4bSAdrian Hunter 	else
368bf8e8f4bSAdrian Hunter 		return thread_map__nr(evlist->threads);
369bf8e8f4bSAdrian Hunter }
370bf8e8f4bSAdrian Hunter 
3714152ab37SArnaldo Carvalho de Melo void perf_evlist__disable(struct perf_evlist *evlist)
3724152ab37SArnaldo Carvalho de Melo {
3734152ab37SArnaldo Carvalho de Melo 	struct perf_evsel *pos;
3744152ab37SArnaldo Carvalho de Melo 
375e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, pos) {
376395c3070SAdrian Hunter 		if (!perf_evsel__is_group_leader(pos) || !pos->fd)
3773fe4430dSJiri Olsa 			continue;
3783e27c920SJiri Olsa 		perf_evsel__disable(pos);
3794152ab37SArnaldo Carvalho de Melo 	}
3802b56bcfbSArnaldo Carvalho de Melo 
3812b56bcfbSArnaldo Carvalho de Melo 	evlist->enabled = false;
3824152ab37SArnaldo Carvalho de Melo }
3834152ab37SArnaldo Carvalho de Melo 
384764e16a3SDavid Ahern void perf_evlist__enable(struct perf_evlist *evlist)
385764e16a3SDavid Ahern {
386764e16a3SDavid Ahern 	struct perf_evsel *pos;
387764e16a3SDavid Ahern 
388e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, pos) {
389395c3070SAdrian Hunter 		if (!perf_evsel__is_group_leader(pos) || !pos->fd)
3903fe4430dSJiri Olsa 			continue;
3913e27c920SJiri Olsa 		perf_evsel__enable(pos);
392764e16a3SDavid Ahern 	}
3932b56bcfbSArnaldo Carvalho de Melo 
3942b56bcfbSArnaldo Carvalho de Melo 	evlist->enabled = true;
3952b56bcfbSArnaldo Carvalho de Melo }
3962b56bcfbSArnaldo Carvalho de Melo 
3972b56bcfbSArnaldo Carvalho de Melo void perf_evlist__toggle_enable(struct perf_evlist *evlist)
3982b56bcfbSArnaldo Carvalho de Melo {
3992b56bcfbSArnaldo Carvalho de Melo 	(evlist->enabled ? perf_evlist__disable : perf_evlist__enable)(evlist);
400764e16a3SDavid Ahern }
401764e16a3SDavid Ahern 
4021c65056cSAdrian Hunter static int perf_evlist__enable_event_cpu(struct perf_evlist *evlist,
4031c65056cSAdrian Hunter 					 struct perf_evsel *evsel, int cpu)
4041c65056cSAdrian Hunter {
4051c65056cSAdrian Hunter 	int thread, err;
4061c65056cSAdrian Hunter 	int nr_threads = perf_evlist__nr_threads(evlist, evsel);
4071c65056cSAdrian Hunter 
4081c65056cSAdrian Hunter 	if (!evsel->fd)
4091c65056cSAdrian Hunter 		return -EINVAL;
4101c65056cSAdrian Hunter 
4111c65056cSAdrian Hunter 	for (thread = 0; thread < nr_threads; thread++) {
4121c65056cSAdrian Hunter 		err = ioctl(FD(evsel, cpu, thread),
4131c65056cSAdrian Hunter 			    PERF_EVENT_IOC_ENABLE, 0);
4141c65056cSAdrian Hunter 		if (err)
4151c65056cSAdrian Hunter 			return err;
4161c65056cSAdrian Hunter 	}
4171c65056cSAdrian Hunter 	return 0;
4181c65056cSAdrian Hunter }
4191c65056cSAdrian Hunter 
4201c65056cSAdrian Hunter static int perf_evlist__enable_event_thread(struct perf_evlist *evlist,
4211c65056cSAdrian Hunter 					    struct perf_evsel *evsel,
4221c65056cSAdrian Hunter 					    int thread)
4231c65056cSAdrian Hunter {
4241c65056cSAdrian Hunter 	int cpu, err;
4251c65056cSAdrian Hunter 	int nr_cpus = cpu_map__nr(evlist->cpus);
4261c65056cSAdrian Hunter 
4271c65056cSAdrian Hunter 	if (!evsel->fd)
4281c65056cSAdrian Hunter 		return -EINVAL;
4291c65056cSAdrian Hunter 
4301c65056cSAdrian Hunter 	for (cpu = 0; cpu < nr_cpus; cpu++) {
4311c65056cSAdrian Hunter 		err = ioctl(FD(evsel, cpu, thread), PERF_EVENT_IOC_ENABLE, 0);
4321c65056cSAdrian Hunter 		if (err)
4331c65056cSAdrian Hunter 			return err;
4341c65056cSAdrian Hunter 	}
4351c65056cSAdrian Hunter 	return 0;
4361c65056cSAdrian Hunter }
4371c65056cSAdrian Hunter 
4381c65056cSAdrian Hunter int perf_evlist__enable_event_idx(struct perf_evlist *evlist,
4391c65056cSAdrian Hunter 				  struct perf_evsel *evsel, int idx)
4401c65056cSAdrian Hunter {
4411c65056cSAdrian Hunter 	bool per_cpu_mmaps = !cpu_map__empty(evlist->cpus);
4421c65056cSAdrian Hunter 
4431c65056cSAdrian Hunter 	if (per_cpu_mmaps)
4441c65056cSAdrian Hunter 		return perf_evlist__enable_event_cpu(evlist, evsel, idx);
4451c65056cSAdrian Hunter 	else
4461c65056cSAdrian Hunter 		return perf_evlist__enable_event_thread(evlist, evsel, idx);
4471c65056cSAdrian Hunter }
4481c65056cSAdrian Hunter 
449ad6765ddSArnaldo Carvalho de Melo int perf_evlist__alloc_pollfd(struct perf_evlist *evlist)
4505c581041SArnaldo Carvalho de Melo {
451b3a319d5SNamhyung Kim 	int nr_cpus = cpu_map__nr(evlist->cpus);
452b3a319d5SNamhyung Kim 	int nr_threads = thread_map__nr(evlist->threads);
453bf8e8f4bSAdrian Hunter 	int nfds = 0;
454bf8e8f4bSAdrian Hunter 	struct perf_evsel *evsel;
455bf8e8f4bSAdrian Hunter 
456e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, evsel) {
457bf8e8f4bSAdrian Hunter 		if (evsel->system_wide)
458bf8e8f4bSAdrian Hunter 			nfds += nr_cpus;
459bf8e8f4bSAdrian Hunter 		else
460bf8e8f4bSAdrian Hunter 			nfds += nr_cpus * nr_threads;
461bf8e8f4bSAdrian Hunter 	}
462bf8e8f4bSAdrian Hunter 
4631b85337dSArnaldo Carvalho de Melo 	if (fdarray__available_entries(&evlist->pollfd) < nfds &&
4641b85337dSArnaldo Carvalho de Melo 	    fdarray__grow(&evlist->pollfd, nfds) < 0)
465ad6765ddSArnaldo Carvalho de Melo 		return -ENOMEM;
466ad6765ddSArnaldo Carvalho de Melo 
467ad6765ddSArnaldo Carvalho de Melo 	return 0;
4685c581041SArnaldo Carvalho de Melo }
46970082dd9SArnaldo Carvalho de Melo 
4704876075bSWang Nan static int __perf_evlist__add_pollfd(struct perf_evlist *evlist, int fd,
4714876075bSWang Nan 				     struct perf_mmap *map, short revent)
472e4b356b5SArnaldo Carvalho de Melo {
473f3058a1cSWang Nan 	int pos = fdarray__add(&evlist->pollfd, fd, revent | POLLERR | POLLHUP);
474e4b356b5SArnaldo Carvalho de Melo 	/*
475e4b356b5SArnaldo Carvalho de Melo 	 * Save the idx so that when we filter out fds POLLHUP'ed we can
476e4b356b5SArnaldo Carvalho de Melo 	 * close the associated evlist->mmap[] entry.
477e4b356b5SArnaldo Carvalho de Melo 	 */
478e4b356b5SArnaldo Carvalho de Melo 	if (pos >= 0) {
4794876075bSWang Nan 		evlist->pollfd.priv[pos].ptr = map;
480e4b356b5SArnaldo Carvalho de Melo 
481e4b356b5SArnaldo Carvalho de Melo 		fcntl(fd, F_SETFL, O_NONBLOCK);
482e4b356b5SArnaldo Carvalho de Melo 	}
483e4b356b5SArnaldo Carvalho de Melo 
484e4b356b5SArnaldo Carvalho de Melo 	return pos;
485e4b356b5SArnaldo Carvalho de Melo }
486e4b356b5SArnaldo Carvalho de Melo 
487ad6765ddSArnaldo Carvalho de Melo int perf_evlist__add_pollfd(struct perf_evlist *evlist, int fd)
48870082dd9SArnaldo Carvalho de Melo {
4894876075bSWang Nan 	return __perf_evlist__add_pollfd(evlist, fd, NULL, POLLIN);
490e4b356b5SArnaldo Carvalho de Melo }
4911b85337dSArnaldo Carvalho de Melo 
492258e4bfcSWang Nan static void perf_evlist__munmap_filtered(struct fdarray *fda, int fd,
493258e4bfcSWang Nan 					 void *arg __maybe_unused)
494e4b356b5SArnaldo Carvalho de Melo {
4954876075bSWang Nan 	struct perf_mmap *map = fda->priv[fd].ptr;
496e4b356b5SArnaldo Carvalho de Melo 
4974876075bSWang Nan 	if (map)
4984876075bSWang Nan 		perf_mmap__put(map);
49970082dd9SArnaldo Carvalho de Melo }
50070db7533SArnaldo Carvalho de Melo 
5011ddec7f0SArnaldo Carvalho de Melo int perf_evlist__filter_pollfd(struct perf_evlist *evlist, short revents_and_mask)
5021ddec7f0SArnaldo Carvalho de Melo {
503e4b356b5SArnaldo Carvalho de Melo 	return fdarray__filter(&evlist->pollfd, revents_and_mask,
504258e4bfcSWang Nan 			       perf_evlist__munmap_filtered, NULL);
5051ddec7f0SArnaldo Carvalho de Melo }
5061ddec7f0SArnaldo Carvalho de Melo 
507f66a889dSArnaldo Carvalho de Melo int perf_evlist__poll(struct perf_evlist *evlist, int timeout)
508f66a889dSArnaldo Carvalho de Melo {
5091b85337dSArnaldo Carvalho de Melo 	return fdarray__poll(&evlist->pollfd, timeout);
510f66a889dSArnaldo Carvalho de Melo }
511f66a889dSArnaldo Carvalho de Melo 
512a91e5431SArnaldo Carvalho de Melo static void perf_evlist__id_hash(struct perf_evlist *evlist,
513a91e5431SArnaldo Carvalho de Melo 				 struct perf_evsel *evsel,
5143d3b5e95SArnaldo Carvalho de Melo 				 int cpu, int thread, u64 id)
5153d3b5e95SArnaldo Carvalho de Melo {
5163d3b5e95SArnaldo Carvalho de Melo 	int hash;
5173d3b5e95SArnaldo Carvalho de Melo 	struct perf_sample_id *sid = SID(evsel, cpu, thread);
5183d3b5e95SArnaldo Carvalho de Melo 
5193d3b5e95SArnaldo Carvalho de Melo 	sid->id = id;
5203d3b5e95SArnaldo Carvalho de Melo 	sid->evsel = evsel;
5213d3b5e95SArnaldo Carvalho de Melo 	hash = hash_64(sid->id, PERF_EVLIST__HLIST_BITS);
5223d3b5e95SArnaldo Carvalho de Melo 	hlist_add_head(&sid->node, &evlist->heads[hash]);
5233d3b5e95SArnaldo Carvalho de Melo }
5243d3b5e95SArnaldo Carvalho de Melo 
525a91e5431SArnaldo Carvalho de Melo void perf_evlist__id_add(struct perf_evlist *evlist, struct perf_evsel *evsel,
526a91e5431SArnaldo Carvalho de Melo 			 int cpu, int thread, u64 id)
527a91e5431SArnaldo Carvalho de Melo {
528a91e5431SArnaldo Carvalho de Melo 	perf_evlist__id_hash(evlist, evsel, cpu, thread, id);
529a91e5431SArnaldo Carvalho de Melo 	evsel->id[evsel->ids++] = id;
530a91e5431SArnaldo Carvalho de Melo }
531a91e5431SArnaldo Carvalho de Melo 
5321c59612dSJiri Olsa int perf_evlist__id_add_fd(struct perf_evlist *evlist,
5333d3b5e95SArnaldo Carvalho de Melo 			   struct perf_evsel *evsel,
534f8a95309SArnaldo Carvalho de Melo 			   int cpu, int thread, int fd)
535f8a95309SArnaldo Carvalho de Melo {
536f8a95309SArnaldo Carvalho de Melo 	u64 read_data[4] = { 0, };
5373d3b5e95SArnaldo Carvalho de Melo 	int id_idx = 1; /* The first entry is the counter value */
538e2b5abe0SJiri Olsa 	u64 id;
539e2b5abe0SJiri Olsa 	int ret;
540e2b5abe0SJiri Olsa 
541e2b5abe0SJiri Olsa 	ret = ioctl(fd, PERF_EVENT_IOC_ID, &id);
542e2b5abe0SJiri Olsa 	if (!ret)
543e2b5abe0SJiri Olsa 		goto add;
544e2b5abe0SJiri Olsa 
545e2b5abe0SJiri Olsa 	if (errno != ENOTTY)
546e2b5abe0SJiri Olsa 		return -1;
547e2b5abe0SJiri Olsa 
548e2b5abe0SJiri Olsa 	/* Legacy way to get event id.. All hail to old kernels! */
549f8a95309SArnaldo Carvalho de Melo 
550c4861afeSJiri Olsa 	/*
551c4861afeSJiri Olsa 	 * This way does not work with group format read, so bail
552c4861afeSJiri Olsa 	 * out in that case.
553c4861afeSJiri Olsa 	 */
554c4861afeSJiri Olsa 	if (perf_evlist__read_format(evlist) & PERF_FORMAT_GROUP)
555c4861afeSJiri Olsa 		return -1;
556c4861afeSJiri Olsa 
557f8a95309SArnaldo Carvalho de Melo 	if (!(evsel->attr.read_format & PERF_FORMAT_ID) ||
558f8a95309SArnaldo Carvalho de Melo 	    read(fd, &read_data, sizeof(read_data)) == -1)
559f8a95309SArnaldo Carvalho de Melo 		return -1;
560f8a95309SArnaldo Carvalho de Melo 
561f8a95309SArnaldo Carvalho de Melo 	if (evsel->attr.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
562f8a95309SArnaldo Carvalho de Melo 		++id_idx;
563f8a95309SArnaldo Carvalho de Melo 	if (evsel->attr.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
564f8a95309SArnaldo Carvalho de Melo 		++id_idx;
565f8a95309SArnaldo Carvalho de Melo 
566e2b5abe0SJiri Olsa 	id = read_data[id_idx];
567e2b5abe0SJiri Olsa 
568e2b5abe0SJiri Olsa  add:
569e2b5abe0SJiri Olsa 	perf_evlist__id_add(evlist, evsel, cpu, thread, id);
570f8a95309SArnaldo Carvalho de Melo 	return 0;
571f8a95309SArnaldo Carvalho de Melo }
572f8a95309SArnaldo Carvalho de Melo 
5733c659eedSAdrian Hunter static void perf_evlist__set_sid_idx(struct perf_evlist *evlist,
5743c659eedSAdrian Hunter 				     struct perf_evsel *evsel, int idx, int cpu,
5753c659eedSAdrian Hunter 				     int thread)
5763c659eedSAdrian Hunter {
5773c659eedSAdrian Hunter 	struct perf_sample_id *sid = SID(evsel, cpu, thread);
5783c659eedSAdrian Hunter 	sid->idx = idx;
5793c659eedSAdrian Hunter 	if (evlist->cpus && cpu >= 0)
5803c659eedSAdrian Hunter 		sid->cpu = evlist->cpus->map[cpu];
5813c659eedSAdrian Hunter 	else
5823c659eedSAdrian Hunter 		sid->cpu = -1;
5833c659eedSAdrian Hunter 	if (!evsel->system_wide && evlist->threads && thread >= 0)
584e13798c7SJiri Olsa 		sid->tid = thread_map__pid(evlist->threads, thread);
5853c659eedSAdrian Hunter 	else
5863c659eedSAdrian Hunter 		sid->tid = -1;
5873c659eedSAdrian Hunter }
5883c659eedSAdrian Hunter 
589932a3594SJiri Olsa struct perf_sample_id *perf_evlist__id2sid(struct perf_evlist *evlist, u64 id)
59070db7533SArnaldo Carvalho de Melo {
59170db7533SArnaldo Carvalho de Melo 	struct hlist_head *head;
59270db7533SArnaldo Carvalho de Melo 	struct perf_sample_id *sid;
59370db7533SArnaldo Carvalho de Melo 	int hash;
59470db7533SArnaldo Carvalho de Melo 
59570db7533SArnaldo Carvalho de Melo 	hash = hash_64(id, PERF_EVLIST__HLIST_BITS);
59670db7533SArnaldo Carvalho de Melo 	head = &evlist->heads[hash];
59770db7533SArnaldo Carvalho de Melo 
598b67bfe0dSSasha Levin 	hlist_for_each_entry(sid, head, node)
59970db7533SArnaldo Carvalho de Melo 		if (sid->id == id)
600932a3594SJiri Olsa 			return sid;
601932a3594SJiri Olsa 
602932a3594SJiri Olsa 	return NULL;
603932a3594SJiri Olsa }
604932a3594SJiri Olsa 
605932a3594SJiri Olsa struct perf_evsel *perf_evlist__id2evsel(struct perf_evlist *evlist, u64 id)
606932a3594SJiri Olsa {
607932a3594SJiri Olsa 	struct perf_sample_id *sid;
608932a3594SJiri Olsa 
60905169df5SAdrian Hunter 	if (evlist->nr_entries == 1 || !id)
610932a3594SJiri Olsa 		return perf_evlist__first(evlist);
611932a3594SJiri Olsa 
612932a3594SJiri Olsa 	sid = perf_evlist__id2sid(evlist, id);
613932a3594SJiri Olsa 	if (sid)
61470db7533SArnaldo Carvalho de Melo 		return sid->evsel;
61530e68bccSNamhyung Kim 
61630e68bccSNamhyung Kim 	if (!perf_evlist__sample_id_all(evlist))
6170c21f736SArnaldo Carvalho de Melo 		return perf_evlist__first(evlist);
61830e68bccSNamhyung Kim 
61970db7533SArnaldo Carvalho de Melo 	return NULL;
62070db7533SArnaldo Carvalho de Melo }
62104391debSArnaldo Carvalho de Melo 
622dddcf6abSAdrian Hunter struct perf_evsel *perf_evlist__id2evsel_strict(struct perf_evlist *evlist,
623dddcf6abSAdrian Hunter 						u64 id)
624dddcf6abSAdrian Hunter {
625dddcf6abSAdrian Hunter 	struct perf_sample_id *sid;
626dddcf6abSAdrian Hunter 
627dddcf6abSAdrian Hunter 	if (!id)
628dddcf6abSAdrian Hunter 		return NULL;
629dddcf6abSAdrian Hunter 
630dddcf6abSAdrian Hunter 	sid = perf_evlist__id2sid(evlist, id);
631dddcf6abSAdrian Hunter 	if (sid)
632dddcf6abSAdrian Hunter 		return sid->evsel;
633dddcf6abSAdrian Hunter 
634dddcf6abSAdrian Hunter 	return NULL;
635dddcf6abSAdrian Hunter }
636dddcf6abSAdrian Hunter 
63775562573SAdrian Hunter static int perf_evlist__event2id(struct perf_evlist *evlist,
63875562573SAdrian Hunter 				 union perf_event *event, u64 *id)
63975562573SAdrian Hunter {
64075562573SAdrian Hunter 	const u64 *array = event->sample.array;
64175562573SAdrian Hunter 	ssize_t n;
64275562573SAdrian Hunter 
64375562573SAdrian Hunter 	n = (event->header.size - sizeof(event->header)) >> 3;
64475562573SAdrian Hunter 
64575562573SAdrian Hunter 	if (event->header.type == PERF_RECORD_SAMPLE) {
64675562573SAdrian Hunter 		if (evlist->id_pos >= n)
64775562573SAdrian Hunter 			return -1;
64875562573SAdrian Hunter 		*id = array[evlist->id_pos];
64975562573SAdrian Hunter 	} else {
65075562573SAdrian Hunter 		if (evlist->is_pos > n)
65175562573SAdrian Hunter 			return -1;
65275562573SAdrian Hunter 		n -= evlist->is_pos;
65375562573SAdrian Hunter 		*id = array[n];
65475562573SAdrian Hunter 	}
65575562573SAdrian Hunter 	return 0;
65675562573SAdrian Hunter }
65775562573SAdrian Hunter 
6587cb5c5acSJiri Olsa struct perf_evsel *perf_evlist__event2evsel(struct perf_evlist *evlist,
65975562573SAdrian Hunter 					    union perf_event *event)
66075562573SAdrian Hunter {
66198be6966SAdrian Hunter 	struct perf_evsel *first = perf_evlist__first(evlist);
66275562573SAdrian Hunter 	struct hlist_head *head;
66375562573SAdrian Hunter 	struct perf_sample_id *sid;
66475562573SAdrian Hunter 	int hash;
66575562573SAdrian Hunter 	u64 id;
66675562573SAdrian Hunter 
66775562573SAdrian Hunter 	if (evlist->nr_entries == 1)
66898be6966SAdrian Hunter 		return first;
66998be6966SAdrian Hunter 
67098be6966SAdrian Hunter 	if (!first->attr.sample_id_all &&
67198be6966SAdrian Hunter 	    event->header.type != PERF_RECORD_SAMPLE)
67298be6966SAdrian Hunter 		return first;
67375562573SAdrian Hunter 
67475562573SAdrian Hunter 	if (perf_evlist__event2id(evlist, event, &id))
67575562573SAdrian Hunter 		return NULL;
67675562573SAdrian Hunter 
67775562573SAdrian Hunter 	/* Synthesized events have an id of zero */
67875562573SAdrian Hunter 	if (!id)
67998be6966SAdrian Hunter 		return first;
68075562573SAdrian Hunter 
68175562573SAdrian Hunter 	hash = hash_64(id, PERF_EVLIST__HLIST_BITS);
68275562573SAdrian Hunter 	head = &evlist->heads[hash];
68375562573SAdrian Hunter 
68475562573SAdrian Hunter 	hlist_for_each_entry(sid, head, node) {
68575562573SAdrian Hunter 		if (sid->id == id)
68675562573SAdrian Hunter 			return sid->evsel;
68775562573SAdrian Hunter 	}
68875562573SAdrian Hunter 	return NULL;
68975562573SAdrian Hunter }
69075562573SAdrian Hunter 
69165aea233SWang Nan static int perf_evlist__set_paused(struct perf_evlist *evlist, bool value)
69265aea233SWang Nan {
69365aea233SWang Nan 	int i;
69465aea233SWang Nan 
69565aea233SWang Nan 	for (i = 0; i < evlist->nr_mmaps; i++) {
69665aea233SWang Nan 		int fd = evlist->mmap[i].fd;
69765aea233SWang Nan 		int err;
69865aea233SWang Nan 
69965aea233SWang Nan 		if (fd < 0)
70065aea233SWang Nan 			continue;
70165aea233SWang Nan 		err = ioctl(fd, PERF_EVENT_IOC_PAUSE_OUTPUT, value ? 1 : 0);
70265aea233SWang Nan 		if (err)
70365aea233SWang Nan 			return err;
70465aea233SWang Nan 	}
70565aea233SWang Nan 	return 0;
70665aea233SWang Nan }
70765aea233SWang Nan 
70865aea233SWang Nan int perf_evlist__pause(struct perf_evlist *evlist)
70965aea233SWang Nan {
71065aea233SWang Nan 	return perf_evlist__set_paused(evlist, true);
71165aea233SWang Nan }
71265aea233SWang Nan 
71365aea233SWang Nan int perf_evlist__resume(struct perf_evlist *evlist)
71465aea233SWang Nan {
71565aea233SWang Nan 	return perf_evlist__set_paused(evlist, false);
71665aea233SWang Nan }
71765aea233SWang Nan 
718b6b85dadSWang Nan /* When check_messup is true, 'end' must points to a good entry */
7190f4ccd11SWang Nan static union perf_event *
720b6b85dadSWang Nan perf_mmap__read(struct perf_mmap *md, bool check_messup, u64 start,
721b6b85dadSWang Nan 		u64 end, u64 *prev)
72204391debSArnaldo Carvalho de Melo {
72304391debSArnaldo Carvalho de Melo 	unsigned char *data = md->base + page_size;
7248115d60cSArnaldo Carvalho de Melo 	union perf_event *event = NULL;
725b6b85dadSWang Nan 	int diff = end - start;
72604391debSArnaldo Carvalho de Melo 
727b6b85dadSWang Nan 	if (check_messup) {
72804391debSArnaldo Carvalho de Melo 		/*
72904391debSArnaldo Carvalho de Melo 		 * If we're further behind than half the buffer, there's a chance
73004391debSArnaldo Carvalho de Melo 		 * the writer will bite our tail and mess up the samples under us.
73104391debSArnaldo Carvalho de Melo 		 *
732b6b85dadSWang Nan 		 * If we somehow ended up ahead of the 'end', we got messed up.
73304391debSArnaldo Carvalho de Melo 		 *
734b6b85dadSWang Nan 		 * In either case, truncate and restart at 'end'.
73504391debSArnaldo Carvalho de Melo 		 */
73604391debSArnaldo Carvalho de Melo 		if (diff > md->mask / 2 || diff < 0) {
73704391debSArnaldo Carvalho de Melo 			fprintf(stderr, "WARNING: failed to keep up with mmap data.\n");
73804391debSArnaldo Carvalho de Melo 
73904391debSArnaldo Carvalho de Melo 			/*
740b6b85dadSWang Nan 			 * 'end' points to a known good entry, start there.
74104391debSArnaldo Carvalho de Melo 			 */
742b6b85dadSWang Nan 			start = end;
743b04b7023SWang Nan 			diff = 0;
74404391debSArnaldo Carvalho de Melo 		}
7457bb41152SArnaldo Carvalho de Melo 	}
74604391debSArnaldo Carvalho de Melo 
747b04b7023SWang Nan 	if (diff >= (int)sizeof(event->header)) {
74804391debSArnaldo Carvalho de Melo 		size_t size;
74904391debSArnaldo Carvalho de Melo 
750b6b85dadSWang Nan 		event = (union perf_event *)&data[start & md->mask];
75104391debSArnaldo Carvalho de Melo 		size = event->header.size;
75204391debSArnaldo Carvalho de Melo 
753b04b7023SWang Nan 		if (size < sizeof(event->header) || diff < (int)size) {
754b04b7023SWang Nan 			event = NULL;
755b04b7023SWang Nan 			goto broken_event;
756b04b7023SWang Nan 		}
757b04b7023SWang Nan 
75804391debSArnaldo Carvalho de Melo 		/*
75904391debSArnaldo Carvalho de Melo 		 * Event straddles the mmap boundary -- header should always
76004391debSArnaldo Carvalho de Melo 		 * be inside due to u64 alignment of output.
76104391debSArnaldo Carvalho de Melo 		 */
762b6b85dadSWang Nan 		if ((start & md->mask) + size != ((start + size) & md->mask)) {
763b6b85dadSWang Nan 			unsigned int offset = start;
76404391debSArnaldo Carvalho de Melo 			unsigned int len = min(sizeof(*event), size), cpy;
765a65cb4b9SJiri Olsa 			void *dst = md->event_copy;
76604391debSArnaldo Carvalho de Melo 
76704391debSArnaldo Carvalho de Melo 			do {
76804391debSArnaldo Carvalho de Melo 				cpy = min(md->mask + 1 - (offset & md->mask), len);
76904391debSArnaldo Carvalho de Melo 				memcpy(dst, &data[offset & md->mask], cpy);
77004391debSArnaldo Carvalho de Melo 				offset += cpy;
77104391debSArnaldo Carvalho de Melo 				dst += cpy;
77204391debSArnaldo Carvalho de Melo 				len -= cpy;
77304391debSArnaldo Carvalho de Melo 			} while (len);
77404391debSArnaldo Carvalho de Melo 
775a65cb4b9SJiri Olsa 			event = (union perf_event *) md->event_copy;
77604391debSArnaldo Carvalho de Melo 		}
77704391debSArnaldo Carvalho de Melo 
778b6b85dadSWang Nan 		start += size;
77904391debSArnaldo Carvalho de Melo 	}
78004391debSArnaldo Carvalho de Melo 
781b04b7023SWang Nan broken_event:
7820f4ccd11SWang Nan 	if (prev)
783b6b85dadSWang Nan 		*prev = start;
7847bb41152SArnaldo Carvalho de Melo 
78504391debSArnaldo Carvalho de Melo 	return event;
78604391debSArnaldo Carvalho de Melo }
787f8a95309SArnaldo Carvalho de Melo 
7888db6d6b1SWang Nan union perf_event *perf_mmap__read_forward(struct perf_mmap *md, bool check_messup)
7890f4ccd11SWang Nan {
7900f4ccd11SWang Nan 	u64 head;
7910f4ccd11SWang Nan 	u64 old = md->prev;
7920f4ccd11SWang Nan 
7930f4ccd11SWang Nan 	/*
7940f4ccd11SWang Nan 	 * Check if event was unmapped due to a POLLHUP/POLLERR.
7950f4ccd11SWang Nan 	 */
7960f4ccd11SWang Nan 	if (!atomic_read(&md->refcnt))
7970f4ccd11SWang Nan 		return NULL;
7980f4ccd11SWang Nan 
7990f4ccd11SWang Nan 	head = perf_mmap__read_head(md);
8000f4ccd11SWang Nan 
8018db6d6b1SWang Nan 	return perf_mmap__read(md, check_messup, old, head, &md->prev);
8020f4ccd11SWang Nan }
8030f4ccd11SWang Nan 
804e24c7520SWang Nan union perf_event *
8058db6d6b1SWang Nan perf_mmap__read_backward(struct perf_mmap *md)
806e24c7520SWang Nan {
807e24c7520SWang Nan 	u64 head, end;
808e24c7520SWang Nan 	u64 start = md->prev;
809e24c7520SWang Nan 
810e24c7520SWang Nan 	/*
811e24c7520SWang Nan 	 * Check if event was unmapped due to a POLLHUP/POLLERR.
812e24c7520SWang Nan 	 */
813e24c7520SWang Nan 	if (!atomic_read(&md->refcnt))
814e24c7520SWang Nan 		return NULL;
815e24c7520SWang Nan 
816e24c7520SWang Nan 	head = perf_mmap__read_head(md);
817e24c7520SWang Nan 	if (!head)
818e24c7520SWang Nan 		return NULL;
819e24c7520SWang Nan 
820e24c7520SWang Nan 	/*
821e24c7520SWang Nan 	 * 'head' pointer starts from 0. Kernel minus sizeof(record) form
822e24c7520SWang Nan 	 * it each time when kernel writes to it, so in fact 'head' is
823e24c7520SWang Nan 	 * negative. 'end' pointer is made manually by adding the size of
824e24c7520SWang Nan 	 * the ring buffer to 'head' pointer, means the validate data can
825e24c7520SWang Nan 	 * read is the whole ring buffer. If 'end' is positive, the ring
826e24c7520SWang Nan 	 * buffer has not fully filled, so we must adjust 'end' to 0.
827e24c7520SWang Nan 	 *
828e24c7520SWang Nan 	 * However, since both 'head' and 'end' is unsigned, we can't
829e24c7520SWang Nan 	 * simply compare 'end' against 0. Here we compare '-head' and
830e24c7520SWang Nan 	 * the size of the ring buffer, where -head is the number of bytes
831e24c7520SWang Nan 	 * kernel write to the ring buffer.
832e24c7520SWang Nan 	 */
833e24c7520SWang Nan 	if (-head < (u64)(md->mask + 1))
834e24c7520SWang Nan 		end = 0;
835e24c7520SWang Nan 	else
836e24c7520SWang Nan 		end = head + md->mask + 1;
837e24c7520SWang Nan 
838e24c7520SWang Nan 	return perf_mmap__read(md, false, start, end, &md->prev);
839e24c7520SWang Nan }
840e24c7520SWang Nan 
8418db6d6b1SWang Nan union perf_event *perf_evlist__mmap_read_forward(struct perf_evlist *evlist, int idx)
8428db6d6b1SWang Nan {
8438db6d6b1SWang Nan 	struct perf_mmap *md = &evlist->mmap[idx];
8448db6d6b1SWang Nan 
8458db6d6b1SWang Nan 	/*
8468db6d6b1SWang Nan 	 * Check messup is required for forward overwritable ring buffer:
8478db6d6b1SWang Nan 	 * memory pointed by md->prev can be overwritten in this case.
8488db6d6b1SWang Nan 	 * No need for read-write ring buffer: kernel stop outputting when
8498db6d6b1SWang Nan 	 * it hit md->prev (perf_mmap__consume()).
8508db6d6b1SWang Nan 	 */
8518db6d6b1SWang Nan 	return perf_mmap__read_forward(md, evlist->overwrite);
8528db6d6b1SWang Nan }
8538db6d6b1SWang Nan 
8548db6d6b1SWang Nan union perf_event *perf_evlist__mmap_read_backward(struct perf_evlist *evlist, int idx)
8558db6d6b1SWang Nan {
8568db6d6b1SWang Nan 	struct perf_mmap *md = &evlist->mmap[idx];
8578db6d6b1SWang Nan 
8588db6d6b1SWang Nan 	/*
8598db6d6b1SWang Nan 	 * No need to check messup for backward ring buffer:
8608db6d6b1SWang Nan 	 * We can always read arbitrary long data from a backward
8618db6d6b1SWang Nan 	 * ring buffer unless we forget to pause it before reading.
8628db6d6b1SWang Nan 	 */
8638db6d6b1SWang Nan 	return perf_mmap__read_backward(md);
8648db6d6b1SWang Nan }
8658db6d6b1SWang Nan 
8665a5ddeb6SWang Nan union perf_event *perf_evlist__mmap_read(struct perf_evlist *evlist, int idx)
8675a5ddeb6SWang Nan {
8685a5ddeb6SWang Nan 	if (!evlist->backward)
8695a5ddeb6SWang Nan 		return perf_evlist__mmap_read_forward(evlist, idx);
8705a5ddeb6SWang Nan 	return perf_evlist__mmap_read_backward(evlist, idx);
8715a5ddeb6SWang Nan }
8725a5ddeb6SWang Nan 
8738db6d6b1SWang Nan void perf_mmap__read_catchup(struct perf_mmap *md)
874e24c7520SWang Nan {
875e24c7520SWang Nan 	u64 head;
876e24c7520SWang Nan 
877e24c7520SWang Nan 	if (!atomic_read(&md->refcnt))
878e24c7520SWang Nan 		return;
879e24c7520SWang Nan 
880e24c7520SWang Nan 	head = perf_mmap__read_head(md);
881e24c7520SWang Nan 	md->prev = head;
882e24c7520SWang Nan }
883e24c7520SWang Nan 
8848db6d6b1SWang Nan void perf_evlist__mmap_read_catchup(struct perf_evlist *evlist, int idx)
8858db6d6b1SWang Nan {
8868db6d6b1SWang Nan 	perf_mmap__read_catchup(&evlist->mmap[idx]);
8878db6d6b1SWang Nan }
8888db6d6b1SWang Nan 
88982396986SArnaldo Carvalho de Melo static bool perf_mmap__empty(struct perf_mmap *md)
89082396986SArnaldo Carvalho de Melo {
891b72e74d1SAdrian Hunter 	return perf_mmap__read_head(md) == md->prev && !md->auxtrace_mmap.base;
89282396986SArnaldo Carvalho de Melo }
89382396986SArnaldo Carvalho de Melo 
8948db6d6b1SWang Nan static void perf_mmap__get(struct perf_mmap *map)
8958db6d6b1SWang Nan {
8968db6d6b1SWang Nan 	atomic_inc(&map->refcnt);
8978db6d6b1SWang Nan }
8988db6d6b1SWang Nan 
8998db6d6b1SWang Nan static void perf_mmap__put(struct perf_mmap *md)
9008db6d6b1SWang Nan {
9018db6d6b1SWang Nan 	BUG_ON(md->base && atomic_read(&md->refcnt) == 0);
9028db6d6b1SWang Nan 
9038db6d6b1SWang Nan 	if (atomic_dec_and_test(&md->refcnt))
9048db6d6b1SWang Nan 		perf_mmap__munmap(md);
9058db6d6b1SWang Nan }
9068db6d6b1SWang Nan 
90782396986SArnaldo Carvalho de Melo static void perf_evlist__mmap_get(struct perf_evlist *evlist, int idx)
90882396986SArnaldo Carvalho de Melo {
9098db6d6b1SWang Nan 	perf_mmap__get(&evlist->mmap[idx]);
91082396986SArnaldo Carvalho de Melo }
91182396986SArnaldo Carvalho de Melo 
91282396986SArnaldo Carvalho de Melo static void perf_evlist__mmap_put(struct perf_evlist *evlist, int idx)
91382396986SArnaldo Carvalho de Melo {
9148db6d6b1SWang Nan 	perf_mmap__put(&evlist->mmap[idx]);
91582396986SArnaldo Carvalho de Melo }
91682396986SArnaldo Carvalho de Melo 
9178db6d6b1SWang Nan void perf_mmap__consume(struct perf_mmap *md, bool overwrite)
9188e50d384SZhouyi Zhou {
9198db6d6b1SWang Nan 	if (!overwrite) {
9207b8283b5SDavid Ahern 		u64 old = md->prev;
9218e50d384SZhouyi Zhou 
9228e50d384SZhouyi Zhou 		perf_mmap__write_tail(md, old);
9238e50d384SZhouyi Zhou 	}
92482396986SArnaldo Carvalho de Melo 
9257143849aSArnaldo Carvalho de Melo 	if (atomic_read(&md->refcnt) == 1 && perf_mmap__empty(md))
9268db6d6b1SWang Nan 		perf_mmap__put(md);
9278db6d6b1SWang Nan }
9288db6d6b1SWang Nan 
9298db6d6b1SWang Nan void perf_evlist__mmap_consume(struct perf_evlist *evlist, int idx)
9308db6d6b1SWang Nan {
9318db6d6b1SWang Nan 	perf_mmap__consume(&evlist->mmap[idx], evlist->overwrite);
9328e50d384SZhouyi Zhou }
9338e50d384SZhouyi Zhou 
934718c602dSAdrian Hunter int __weak auxtrace_mmap__mmap(struct auxtrace_mmap *mm __maybe_unused,
935718c602dSAdrian Hunter 			       struct auxtrace_mmap_params *mp __maybe_unused,
936718c602dSAdrian Hunter 			       void *userpg __maybe_unused,
937718c602dSAdrian Hunter 			       int fd __maybe_unused)
938718c602dSAdrian Hunter {
939718c602dSAdrian Hunter 	return 0;
940718c602dSAdrian Hunter }
941718c602dSAdrian Hunter 
942718c602dSAdrian Hunter void __weak auxtrace_mmap__munmap(struct auxtrace_mmap *mm __maybe_unused)
943718c602dSAdrian Hunter {
944718c602dSAdrian Hunter }
945718c602dSAdrian Hunter 
946718c602dSAdrian Hunter void __weak auxtrace_mmap_params__init(
947718c602dSAdrian Hunter 			struct auxtrace_mmap_params *mp __maybe_unused,
948718c602dSAdrian Hunter 			off_t auxtrace_offset __maybe_unused,
949718c602dSAdrian Hunter 			unsigned int auxtrace_pages __maybe_unused,
950718c602dSAdrian Hunter 			bool auxtrace_overwrite __maybe_unused)
951718c602dSAdrian Hunter {
952718c602dSAdrian Hunter }
953718c602dSAdrian Hunter 
954718c602dSAdrian Hunter void __weak auxtrace_mmap_params__set_idx(
955718c602dSAdrian Hunter 			struct auxtrace_mmap_params *mp __maybe_unused,
956718c602dSAdrian Hunter 			struct perf_evlist *evlist __maybe_unused,
957718c602dSAdrian Hunter 			int idx __maybe_unused,
958718c602dSAdrian Hunter 			bool per_cpu __maybe_unused)
959718c602dSAdrian Hunter {
960718c602dSAdrian Hunter }
961718c602dSAdrian Hunter 
9628db6d6b1SWang Nan static void perf_mmap__munmap(struct perf_mmap *map)
9638db6d6b1SWang Nan {
9648db6d6b1SWang Nan 	if (map->base != NULL) {
9658db6d6b1SWang Nan 		munmap(map->base, perf_mmap__mmap_len(map));
9668db6d6b1SWang Nan 		map->base = NULL;
9678db6d6b1SWang Nan 		map->fd = -1;
9688db6d6b1SWang Nan 		atomic_set(&map->refcnt, 0);
9698db6d6b1SWang Nan 	}
9708db6d6b1SWang Nan 	auxtrace_mmap__munmap(&map->auxtrace_mmap);
9718db6d6b1SWang Nan }
9728db6d6b1SWang Nan 
973a1f72618SWang Nan static void perf_evlist__munmap_nofree(struct perf_evlist *evlist)
974f8a95309SArnaldo Carvalho de Melo {
975aece948fSArnaldo Carvalho de Melo 	int i;
976f8a95309SArnaldo Carvalho de Melo 
977b2cb615dSWang Nan 	if (evlist->mmap)
97893edcbd9SAdrian Hunter 		for (i = 0; i < evlist->nr_mmaps; i++)
979a1f72618SWang Nan 			perf_mmap__munmap(&evlist->mmap[i]);
980b2cb615dSWang Nan 
981b2cb615dSWang Nan 	if (evlist->backward_mmap)
982b2cb615dSWang Nan 		for (i = 0; i < evlist->nr_mmaps; i++)
983b2cb615dSWang Nan 			perf_mmap__munmap(&evlist->backward_mmap[i]);
984a1f72618SWang Nan }
985aece948fSArnaldo Carvalho de Melo 
986a1f72618SWang Nan void perf_evlist__munmap(struct perf_evlist *evlist)
987a1f72618SWang Nan {
988a1f72618SWang Nan 	perf_evlist__munmap_nofree(evlist);
98904662523SArnaldo Carvalho de Melo 	zfree(&evlist->mmap);
990b2cb615dSWang Nan 	zfree(&evlist->backward_mmap);
991f8a95309SArnaldo Carvalho de Melo }
992f8a95309SArnaldo Carvalho de Melo 
9938db6d6b1SWang Nan static struct perf_mmap *perf_evlist__alloc_mmap(struct perf_evlist *evlist)
994f8a95309SArnaldo Carvalho de Melo {
995d4c6fb36SWang Nan 	int i;
9968db6d6b1SWang Nan 	struct perf_mmap *map;
997d4c6fb36SWang Nan 
998a14bb7a6SArnaldo Carvalho de Melo 	evlist->nr_mmaps = cpu_map__nr(evlist->cpus);
999ec1e7e43SSukadev Bhattiprolu 	if (cpu_map__empty(evlist->cpus))
1000b3a319d5SNamhyung Kim 		evlist->nr_mmaps = thread_map__nr(evlist->threads);
10018db6d6b1SWang Nan 	map = zalloc(evlist->nr_mmaps * sizeof(struct perf_mmap));
10028db6d6b1SWang Nan 	if (!map)
10038db6d6b1SWang Nan 		return NULL;
1004946ae1d4SWang Nan 
1005d4c6fb36SWang Nan 	for (i = 0; i < evlist->nr_mmaps; i++)
10068db6d6b1SWang Nan 		map[i].fd = -1;
10078db6d6b1SWang Nan 	return map;
1008f8a95309SArnaldo Carvalho de Melo }
1009f8a95309SArnaldo Carvalho de Melo 
1010a8a8f3ebSAdrian Hunter struct mmap_params {
1011a8a8f3ebSAdrian Hunter 	int prot;
1012a8a8f3ebSAdrian Hunter 	int mask;
1013718c602dSAdrian Hunter 	struct auxtrace_mmap_params auxtrace_mp;
1014a8a8f3ebSAdrian Hunter };
1015a8a8f3ebSAdrian Hunter 
10168db6d6b1SWang Nan static int perf_mmap__mmap(struct perf_mmap *map,
1017a8a8f3ebSAdrian Hunter 			   struct mmap_params *mp, int fd)
1018f8a95309SArnaldo Carvalho de Melo {
101982396986SArnaldo Carvalho de Melo 	/*
102082396986SArnaldo Carvalho de Melo 	 * The last one will be done at perf_evlist__mmap_consume(), so that we
102182396986SArnaldo Carvalho de Melo 	 * make sure we don't prevent tools from consuming every last event in
102282396986SArnaldo Carvalho de Melo 	 * the ring buffer.
102382396986SArnaldo Carvalho de Melo 	 *
102482396986SArnaldo Carvalho de Melo 	 * I.e. we can get the POLLHUP meaning that the fd doesn't exist
102582396986SArnaldo Carvalho de Melo 	 * anymore, but the last events for it are still in the ring buffer,
102682396986SArnaldo Carvalho de Melo 	 * waiting to be consumed.
102782396986SArnaldo Carvalho de Melo 	 *
102882396986SArnaldo Carvalho de Melo 	 * Tools can chose to ignore this at their own discretion, but the
102982396986SArnaldo Carvalho de Melo 	 * evlist layer can't just drop it when filtering events in
103082396986SArnaldo Carvalho de Melo 	 * perf_evlist__filter_pollfd().
103182396986SArnaldo Carvalho de Melo 	 */
10328db6d6b1SWang Nan 	atomic_set(&map->refcnt, 2);
10338db6d6b1SWang Nan 	map->prev = 0;
10348db6d6b1SWang Nan 	map->mask = mp->mask;
10358db6d6b1SWang Nan 	map->base = mmap(NULL, perf_mmap__mmap_len(map), mp->prot,
1036f8a95309SArnaldo Carvalho de Melo 			 MAP_SHARED, fd, 0);
10378db6d6b1SWang Nan 	if (map->base == MAP_FAILED) {
103802635965SAdrian Hunter 		pr_debug2("failed to mmap perf event ring buffer, error %d\n",
103902635965SAdrian Hunter 			  errno);
10408db6d6b1SWang Nan 		map->base = NULL;
1041f8a95309SArnaldo Carvalho de Melo 		return -1;
1042301b195dSNelson Elhage 	}
10438db6d6b1SWang Nan 	map->fd = fd;
1044ad6765ddSArnaldo Carvalho de Melo 
10458db6d6b1SWang Nan 	if (auxtrace_mmap__mmap(&map->auxtrace_mmap,
10468db6d6b1SWang Nan 				&mp->auxtrace_mp, map->base, fd))
1047718c602dSAdrian Hunter 		return -1;
1048718c602dSAdrian Hunter 
1049f8a95309SArnaldo Carvalho de Melo 	return 0;
1050f8a95309SArnaldo Carvalho de Melo }
1051f8a95309SArnaldo Carvalho de Melo 
10528db6d6b1SWang Nan static int __perf_evlist__mmap(struct perf_evlist *evlist, int idx,
10538db6d6b1SWang Nan 			       struct mmap_params *mp, int fd)
10548db6d6b1SWang Nan {
10558db6d6b1SWang Nan 	return perf_mmap__mmap(&evlist->mmap[idx], mp, fd);
10568db6d6b1SWang Nan }
10578db6d6b1SWang Nan 
1058f3058a1cSWang Nan static bool
1059f3058a1cSWang Nan perf_evlist__should_poll(struct perf_evlist *evlist __maybe_unused,
1060f3058a1cSWang Nan 			 struct perf_evsel *evsel)
1061f3058a1cSWang Nan {
106232a951b4SArnaldo Carvalho de Melo 	if (evsel->attr.write_backward)
1063f3058a1cSWang Nan 		return false;
1064f3058a1cSWang Nan 	return true;
1065f3058a1cSWang Nan }
1066f3058a1cSWang Nan 
106704e21314SAdrian Hunter static int perf_evlist__mmap_per_evsel(struct perf_evlist *evlist, int idx,
1068a8a8f3ebSAdrian Hunter 				       struct mmap_params *mp, int cpu,
1069a8a8f3ebSAdrian Hunter 				       int thread, int *output)
1070aece948fSArnaldo Carvalho de Melo {
1071aece948fSArnaldo Carvalho de Melo 	struct perf_evsel *evsel;
1072f3058a1cSWang Nan 	int revent;
107304e21314SAdrian Hunter 
1074e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, evsel) {
1075bf8e8f4bSAdrian Hunter 		int fd;
1076bf8e8f4bSAdrian Hunter 
107732a951b4SArnaldo Carvalho de Melo 		if (!!evsel->attr.write_backward != (evlist->overwrite && evlist->backward))
1078f3058a1cSWang Nan 			continue;
1079f3058a1cSWang Nan 
1080bf8e8f4bSAdrian Hunter 		if (evsel->system_wide && thread)
1081bf8e8f4bSAdrian Hunter 			continue;
1082bf8e8f4bSAdrian Hunter 
1083bf8e8f4bSAdrian Hunter 		fd = FD(evsel, cpu, thread);
108404e21314SAdrian Hunter 
108504e21314SAdrian Hunter 		if (*output == -1) {
108604e21314SAdrian Hunter 			*output = fd;
1087a8a8f3ebSAdrian Hunter 			if (__perf_evlist__mmap(evlist, idx, mp, *output) < 0)
108804e21314SAdrian Hunter 				return -1;
108904e21314SAdrian Hunter 		} else {
109004e21314SAdrian Hunter 			if (ioctl(fd, PERF_EVENT_IOC_SET_OUTPUT, *output) != 0)
109104e21314SAdrian Hunter 				return -1;
109282396986SArnaldo Carvalho de Melo 
109382396986SArnaldo Carvalho de Melo 			perf_evlist__mmap_get(evlist, idx);
109404e21314SAdrian Hunter 		}
109504e21314SAdrian Hunter 
1096f3058a1cSWang Nan 		revent = perf_evlist__should_poll(evlist, evsel) ? POLLIN : 0;
1097f3058a1cSWang Nan 
1098f90d194aSAdrian Hunter 		/*
1099f90d194aSAdrian Hunter 		 * The system_wide flag causes a selected event to be opened
1100f90d194aSAdrian Hunter 		 * always without a pid.  Consequently it will never get a
1101f90d194aSAdrian Hunter 		 * POLLHUP, but it is used for tracking in combination with
1102f90d194aSAdrian Hunter 		 * other events, so it should not need to be polled anyway.
1103f90d194aSAdrian Hunter 		 * Therefore don't add it for polling.
1104f90d194aSAdrian Hunter 		 */
1105f90d194aSAdrian Hunter 		if (!evsel->system_wide &&
11064876075bSWang Nan 		    __perf_evlist__add_pollfd(evlist, fd, &evlist->mmap[idx], revent) < 0) {
110782396986SArnaldo Carvalho de Melo 			perf_evlist__mmap_put(evlist, idx);
1108ad6765ddSArnaldo Carvalho de Melo 			return -1;
110982396986SArnaldo Carvalho de Melo 		}
1110033fa713SArnaldo Carvalho de Melo 
11113c659eedSAdrian Hunter 		if (evsel->attr.read_format & PERF_FORMAT_ID) {
11123c659eedSAdrian Hunter 			if (perf_evlist__id_add_fd(evlist, evsel, cpu, thread,
11133c659eedSAdrian Hunter 						   fd) < 0)
111404e21314SAdrian Hunter 				return -1;
11153c659eedSAdrian Hunter 			perf_evlist__set_sid_idx(evlist, evsel, idx, cpu,
11163c659eedSAdrian Hunter 						 thread);
11173c659eedSAdrian Hunter 		}
111804e21314SAdrian Hunter 	}
111904e21314SAdrian Hunter 
112004e21314SAdrian Hunter 	return 0;
112104e21314SAdrian Hunter }
112204e21314SAdrian Hunter 
1123a8a8f3ebSAdrian Hunter static int perf_evlist__mmap_per_cpu(struct perf_evlist *evlist,
1124a8a8f3ebSAdrian Hunter 				     struct mmap_params *mp)
112504e21314SAdrian Hunter {
1126aece948fSArnaldo Carvalho de Melo 	int cpu, thread;
1127b3a319d5SNamhyung Kim 	int nr_cpus = cpu_map__nr(evlist->cpus);
1128b3a319d5SNamhyung Kim 	int nr_threads = thread_map__nr(evlist->threads);
1129aece948fSArnaldo Carvalho de Melo 
1130e3e1a54fSAdrian Hunter 	pr_debug2("perf event ring buffer mmapped per cpu\n");
1131b3a319d5SNamhyung Kim 	for (cpu = 0; cpu < nr_cpus; cpu++) {
1132aece948fSArnaldo Carvalho de Melo 		int output = -1;
1133aece948fSArnaldo Carvalho de Melo 
1134718c602dSAdrian Hunter 		auxtrace_mmap_params__set_idx(&mp->auxtrace_mp, evlist, cpu,
1135718c602dSAdrian Hunter 					      true);
1136718c602dSAdrian Hunter 
1137b3a319d5SNamhyung Kim 		for (thread = 0; thread < nr_threads; thread++) {
1138a8a8f3ebSAdrian Hunter 			if (perf_evlist__mmap_per_evsel(evlist, cpu, mp, cpu,
1139a8a8f3ebSAdrian Hunter 							thread, &output))
1140aece948fSArnaldo Carvalho de Melo 				goto out_unmap;
1141aece948fSArnaldo Carvalho de Melo 		}
1142aece948fSArnaldo Carvalho de Melo 	}
1143aece948fSArnaldo Carvalho de Melo 
1144aece948fSArnaldo Carvalho de Melo 	return 0;
1145aece948fSArnaldo Carvalho de Melo 
1146aece948fSArnaldo Carvalho de Melo out_unmap:
1147a1f72618SWang Nan 	perf_evlist__munmap_nofree(evlist);
1148aece948fSArnaldo Carvalho de Melo 	return -1;
1149aece948fSArnaldo Carvalho de Melo }
1150aece948fSArnaldo Carvalho de Melo 
1151a8a8f3ebSAdrian Hunter static int perf_evlist__mmap_per_thread(struct perf_evlist *evlist,
1152a8a8f3ebSAdrian Hunter 					struct mmap_params *mp)
1153aece948fSArnaldo Carvalho de Melo {
1154aece948fSArnaldo Carvalho de Melo 	int thread;
1155b3a319d5SNamhyung Kim 	int nr_threads = thread_map__nr(evlist->threads);
1156aece948fSArnaldo Carvalho de Melo 
1157e3e1a54fSAdrian Hunter 	pr_debug2("perf event ring buffer mmapped per thread\n");
1158b3a319d5SNamhyung Kim 	for (thread = 0; thread < nr_threads; thread++) {
1159aece948fSArnaldo Carvalho de Melo 		int output = -1;
1160aece948fSArnaldo Carvalho de Melo 
1161718c602dSAdrian Hunter 		auxtrace_mmap_params__set_idx(&mp->auxtrace_mp, evlist, thread,
1162718c602dSAdrian Hunter 					      false);
1163718c602dSAdrian Hunter 
1164a8a8f3ebSAdrian Hunter 		if (perf_evlist__mmap_per_evsel(evlist, thread, mp, 0, thread,
1165a8a8f3ebSAdrian Hunter 						&output))
1166aece948fSArnaldo Carvalho de Melo 			goto out_unmap;
1167aece948fSArnaldo Carvalho de Melo 	}
1168aece948fSArnaldo Carvalho de Melo 
1169aece948fSArnaldo Carvalho de Melo 	return 0;
1170aece948fSArnaldo Carvalho de Melo 
1171aece948fSArnaldo Carvalho de Melo out_unmap:
1172a1f72618SWang Nan 	perf_evlist__munmap_nofree(evlist);
1173aece948fSArnaldo Carvalho de Melo 	return -1;
1174aece948fSArnaldo Carvalho de Melo }
1175aece948fSArnaldo Carvalho de Melo 
1176f5e7150cSArnaldo Carvalho de Melo unsigned long perf_event_mlock_kb_in_pages(void)
1177994a1f78SJiri Olsa {
1178f5e7150cSArnaldo Carvalho de Melo 	unsigned long pages;
11798185e881SArnaldo Carvalho de Melo 	int max;
11808185e881SArnaldo Carvalho de Melo 
11818185e881SArnaldo Carvalho de Melo 	if (sysctl__read_int("kernel/perf_event_mlock_kb", &max) < 0) {
11828185e881SArnaldo Carvalho de Melo 		/*
11838185e881SArnaldo Carvalho de Melo 		 * Pick a once upon a time good value, i.e. things look
11848185e881SArnaldo Carvalho de Melo 		 * strange since we can't read a sysctl value, but lets not
11858185e881SArnaldo Carvalho de Melo 		 * die yet...
11868185e881SArnaldo Carvalho de Melo 		 */
11878185e881SArnaldo Carvalho de Melo 		max = 512;
11888185e881SArnaldo Carvalho de Melo 	} else {
11898185e881SArnaldo Carvalho de Melo 		max -= (page_size / 1024);
11908185e881SArnaldo Carvalho de Melo 	}
11918185e881SArnaldo Carvalho de Melo 
11928185e881SArnaldo Carvalho de Melo 	pages = (max * 1024) / page_size;
11931be300f4SArnaldo Carvalho de Melo 	if (!is_power_of_2(pages))
11941be300f4SArnaldo Carvalho de Melo 		pages = rounddown_pow_of_two(pages);
1195f5e7150cSArnaldo Carvalho de Melo 
1196f5e7150cSArnaldo Carvalho de Melo 	return pages;
1197f5e7150cSArnaldo Carvalho de Melo }
1198f5e7150cSArnaldo Carvalho de Melo 
1199f5e7150cSArnaldo Carvalho de Melo static size_t perf_evlist__mmap_size(unsigned long pages)
1200f5e7150cSArnaldo Carvalho de Melo {
1201f5e7150cSArnaldo Carvalho de Melo 	if (pages == UINT_MAX)
1202f5e7150cSArnaldo Carvalho de Melo 		pages = perf_event_mlock_kb_in_pages();
1203f5e7150cSArnaldo Carvalho de Melo 	else if (!is_power_of_2(pages))
1204994a1f78SJiri Olsa 		return 0;
1205994a1f78SJiri Olsa 
1206994a1f78SJiri Olsa 	return (pages + 1) * page_size;
1207994a1f78SJiri Olsa }
1208994a1f78SJiri Olsa 
120933c2dcfdSDavid Ahern static long parse_pages_arg(const char *str, unsigned long min,
121033c2dcfdSDavid Ahern 			    unsigned long max)
1211994a1f78SJiri Olsa {
12122fbe4abeSAdrian Hunter 	unsigned long pages, val;
121327050f53SJiri Olsa 	static struct parse_tag tags[] = {
121427050f53SJiri Olsa 		{ .tag  = 'B', .mult = 1       },
121527050f53SJiri Olsa 		{ .tag  = 'K', .mult = 1 << 10 },
121627050f53SJiri Olsa 		{ .tag  = 'M', .mult = 1 << 20 },
121727050f53SJiri Olsa 		{ .tag  = 'G', .mult = 1 << 30 },
121827050f53SJiri Olsa 		{ .tag  = 0 },
121927050f53SJiri Olsa 	};
1220994a1f78SJiri Olsa 
12218973504bSDavid Ahern 	if (str == NULL)
122233c2dcfdSDavid Ahern 		return -EINVAL;
12238973504bSDavid Ahern 
122427050f53SJiri Olsa 	val = parse_tag_value(str, tags);
12252fbe4abeSAdrian Hunter 	if (val != (unsigned long) -1) {
122627050f53SJiri Olsa 		/* we got file size value */
122727050f53SJiri Olsa 		pages = PERF_ALIGN(val, page_size) / page_size;
122827050f53SJiri Olsa 	} else {
122927050f53SJiri Olsa 		/* we got pages count value */
123027050f53SJiri Olsa 		char *eptr;
1231994a1f78SJiri Olsa 		pages = strtoul(str, &eptr, 10);
123233c2dcfdSDavid Ahern 		if (*eptr != '\0')
123333c2dcfdSDavid Ahern 			return -EINVAL;
123427050f53SJiri Olsa 	}
1235994a1f78SJiri Olsa 
12362bcab6c1SAdrian Hunter 	if (pages == 0 && min == 0) {
123733c2dcfdSDavid Ahern 		/* leave number of pages at 0 */
12381dbfa938SAdrian Hunter 	} else if (!is_power_of_2(pages)) {
123933c2dcfdSDavid Ahern 		/* round pages up to next power of 2 */
124091529834SArnaldo Carvalho de Melo 		pages = roundup_pow_of_two(pages);
12411dbfa938SAdrian Hunter 		if (!pages)
12421dbfa938SAdrian Hunter 			return -EINVAL;
12439639837eSDavid Ahern 		pr_info("rounding mmap pages size to %lu bytes (%lu pages)\n",
12449639837eSDavid Ahern 			pages * page_size, pages);
12452fbe4abeSAdrian Hunter 	}
12462fbe4abeSAdrian Hunter 
124733c2dcfdSDavid Ahern 	if (pages > max)
124833c2dcfdSDavid Ahern 		return -EINVAL;
124933c2dcfdSDavid Ahern 
125033c2dcfdSDavid Ahern 	return pages;
125133c2dcfdSDavid Ahern }
125233c2dcfdSDavid Ahern 
1253e9db1310SAdrian Hunter int __perf_evlist__parse_mmap_pages(unsigned int *mmap_pages, const char *str)
125433c2dcfdSDavid Ahern {
125533c2dcfdSDavid Ahern 	unsigned long max = UINT_MAX;
125633c2dcfdSDavid Ahern 	long pages;
125733c2dcfdSDavid Ahern 
1258f5ae9c42SAdrian Hunter 	if (max > SIZE_MAX / page_size)
125933c2dcfdSDavid Ahern 		max = SIZE_MAX / page_size;
126033c2dcfdSDavid Ahern 
126133c2dcfdSDavid Ahern 	pages = parse_pages_arg(str, 1, max);
126233c2dcfdSDavid Ahern 	if (pages < 0) {
126333c2dcfdSDavid Ahern 		pr_err("Invalid argument for --mmap_pages/-m\n");
1264994a1f78SJiri Olsa 		return -1;
1265994a1f78SJiri Olsa 	}
1266994a1f78SJiri Olsa 
1267994a1f78SJiri Olsa 	*mmap_pages = pages;
1268994a1f78SJiri Olsa 	return 0;
1269994a1f78SJiri Olsa }
1270994a1f78SJiri Olsa 
1271e9db1310SAdrian Hunter int perf_evlist__parse_mmap_pages(const struct option *opt, const char *str,
1272e9db1310SAdrian Hunter 				  int unset __maybe_unused)
1273e9db1310SAdrian Hunter {
1274e9db1310SAdrian Hunter 	return __perf_evlist__parse_mmap_pages(opt->value, str);
1275e9db1310SAdrian Hunter }
1276e9db1310SAdrian Hunter 
1277c83fa7f2SAdrian Hunter /**
1278718c602dSAdrian Hunter  * perf_evlist__mmap_ex - Create mmaps to receive events.
1279c83fa7f2SAdrian Hunter  * @evlist: list of events
1280c83fa7f2SAdrian Hunter  * @pages: map length in pages
1281c83fa7f2SAdrian Hunter  * @overwrite: overwrite older events?
1282718c602dSAdrian Hunter  * @auxtrace_pages - auxtrace map length in pages
1283718c602dSAdrian Hunter  * @auxtrace_overwrite - overwrite older auxtrace data?
1284f8a95309SArnaldo Carvalho de Melo  *
1285c83fa7f2SAdrian Hunter  * If @overwrite is %false the user needs to signal event consumption using
1286c83fa7f2SAdrian Hunter  * perf_mmap__write_tail().  Using perf_evlist__mmap_read() does this
1287c83fa7f2SAdrian Hunter  * automatically.
1288f8a95309SArnaldo Carvalho de Melo  *
1289718c602dSAdrian Hunter  * Similarly, if @auxtrace_overwrite is %false the user needs to signal data
1290718c602dSAdrian Hunter  * consumption using auxtrace_mmap__write_tail().
1291718c602dSAdrian Hunter  *
1292c83fa7f2SAdrian Hunter  * Return: %0 on success, negative error code otherwise.
1293f8a95309SArnaldo Carvalho de Melo  */
1294718c602dSAdrian Hunter int perf_evlist__mmap_ex(struct perf_evlist *evlist, unsigned int pages,
1295718c602dSAdrian Hunter 			 bool overwrite, unsigned int auxtrace_pages,
1296718c602dSAdrian Hunter 			 bool auxtrace_overwrite)
1297f8a95309SArnaldo Carvalho de Melo {
1298aece948fSArnaldo Carvalho de Melo 	struct perf_evsel *evsel;
12997e2ed097SArnaldo Carvalho de Melo 	const struct cpu_map *cpus = evlist->cpus;
13007e2ed097SArnaldo Carvalho de Melo 	const struct thread_map *threads = evlist->threads;
1301a8a8f3ebSAdrian Hunter 	struct mmap_params mp = {
1302a8a8f3ebSAdrian Hunter 		.prot = PROT_READ | (overwrite ? 0 : PROT_WRITE),
1303a8a8f3ebSAdrian Hunter 	};
130450a682ceSArnaldo Carvalho de Melo 
13058db6d6b1SWang Nan 	if (!evlist->mmap)
13068db6d6b1SWang Nan 		evlist->mmap = perf_evlist__alloc_mmap(evlist);
13078db6d6b1SWang Nan 	if (!evlist->mmap)
1308f8a95309SArnaldo Carvalho de Melo 		return -ENOMEM;
1309f8a95309SArnaldo Carvalho de Melo 
13101b85337dSArnaldo Carvalho de Melo 	if (evlist->pollfd.entries == NULL && perf_evlist__alloc_pollfd(evlist) < 0)
1311f8a95309SArnaldo Carvalho de Melo 		return -ENOMEM;
1312f8a95309SArnaldo Carvalho de Melo 
1313f8a95309SArnaldo Carvalho de Melo 	evlist->overwrite = overwrite;
1314994a1f78SJiri Olsa 	evlist->mmap_len = perf_evlist__mmap_size(pages);
13152af68ef5SAdrian Hunter 	pr_debug("mmap size %zuB\n", evlist->mmap_len);
1316a8a8f3ebSAdrian Hunter 	mp.mask = evlist->mmap_len - page_size - 1;
1317f8a95309SArnaldo Carvalho de Melo 
1318718c602dSAdrian Hunter 	auxtrace_mmap_params__init(&mp.auxtrace_mp, evlist->mmap_len,
1319718c602dSAdrian Hunter 				   auxtrace_pages, auxtrace_overwrite);
1320718c602dSAdrian Hunter 
1321e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, evsel) {
1322f8a95309SArnaldo Carvalho de Melo 		if ((evsel->attr.read_format & PERF_FORMAT_ID) &&
1323a91e5431SArnaldo Carvalho de Melo 		    evsel->sample_id == NULL &&
1324a14bb7a6SArnaldo Carvalho de Melo 		    perf_evsel__alloc_id(evsel, cpu_map__nr(cpus), threads->nr) < 0)
1325f8a95309SArnaldo Carvalho de Melo 			return -ENOMEM;
1326f8a95309SArnaldo Carvalho de Melo 	}
1327f8a95309SArnaldo Carvalho de Melo 
1328ec1e7e43SSukadev Bhattiprolu 	if (cpu_map__empty(cpus))
1329a8a8f3ebSAdrian Hunter 		return perf_evlist__mmap_per_thread(evlist, &mp);
1330f8a95309SArnaldo Carvalho de Melo 
1331a8a8f3ebSAdrian Hunter 	return perf_evlist__mmap_per_cpu(evlist, &mp);
1332f8a95309SArnaldo Carvalho de Melo }
13337e2ed097SArnaldo Carvalho de Melo 
1334718c602dSAdrian Hunter int perf_evlist__mmap(struct perf_evlist *evlist, unsigned int pages,
1335718c602dSAdrian Hunter 		      bool overwrite)
1336718c602dSAdrian Hunter {
1337718c602dSAdrian Hunter 	return perf_evlist__mmap_ex(evlist, pages, overwrite, 0, false);
1338718c602dSAdrian Hunter }
1339718c602dSAdrian Hunter 
1340602ad878SArnaldo Carvalho de Melo int perf_evlist__create_maps(struct perf_evlist *evlist, struct target *target)
13417e2ed097SArnaldo Carvalho de Melo {
134274bfd2b2SAdrian Hunter 	struct cpu_map *cpus;
134374bfd2b2SAdrian Hunter 	struct thread_map *threads;
13447e2ed097SArnaldo Carvalho de Melo 
134574bfd2b2SAdrian Hunter 	threads = thread_map__new_str(target->pid, target->tid, target->uid);
134674bfd2b2SAdrian Hunter 
134774bfd2b2SAdrian Hunter 	if (!threads)
13487e2ed097SArnaldo Carvalho de Melo 		return -1;
13497e2ed097SArnaldo Carvalho de Melo 
13509c105fbcSDongsheng Yang 	if (target__uses_dummy_map(target))
135174bfd2b2SAdrian Hunter 		cpus = cpu_map__dummy_new();
1352879d77d0SNamhyung Kim 	else
135374bfd2b2SAdrian Hunter 		cpus = cpu_map__new(target->cpu_list);
13547e2ed097SArnaldo Carvalho de Melo 
135574bfd2b2SAdrian Hunter 	if (!cpus)
13567e2ed097SArnaldo Carvalho de Melo 		goto out_delete_threads;
13577e2ed097SArnaldo Carvalho de Melo 
1358ec9a77a7SAdrian Hunter 	evlist->has_user_cpus = !!target->cpu_list;
1359ec9a77a7SAdrian Hunter 
136074bfd2b2SAdrian Hunter 	perf_evlist__set_maps(evlist, cpus, threads);
1361d5bc056eSAdrian Hunter 
1362d5bc056eSAdrian Hunter 	return 0;
13637e2ed097SArnaldo Carvalho de Melo 
13647e2ed097SArnaldo Carvalho de Melo out_delete_threads:
136574bfd2b2SAdrian Hunter 	thread_map__put(threads);
13667e2ed097SArnaldo Carvalho de Melo 	return -1;
13677e2ed097SArnaldo Carvalho de Melo }
13687e2ed097SArnaldo Carvalho de Melo 
1369d5bc056eSAdrian Hunter void perf_evlist__set_maps(struct perf_evlist *evlist, struct cpu_map *cpus,
13703de5cfb0SJiri Olsa 			   struct thread_map *threads)
13713de5cfb0SJiri Olsa {
1372934e0f20SAdrian Hunter 	/*
1373934e0f20SAdrian Hunter 	 * Allow for the possibility that one or another of the maps isn't being
1374934e0f20SAdrian Hunter 	 * changed i.e. don't put it.  Note we are assuming the maps that are
1375934e0f20SAdrian Hunter 	 * being applied are brand new and evlist is taking ownership of the
1376934e0f20SAdrian Hunter 	 * original reference count of 1.  If that is not the case it is up to
1377934e0f20SAdrian Hunter 	 * the caller to increase the reference count.
1378934e0f20SAdrian Hunter 	 */
1379934e0f20SAdrian Hunter 	if (cpus != evlist->cpus) {
13803de5cfb0SJiri Olsa 		cpu_map__put(evlist->cpus);
1381a55e5663SArnaldo Carvalho de Melo 		evlist->cpus = cpu_map__get(cpus);
1382934e0f20SAdrian Hunter 	}
13833de5cfb0SJiri Olsa 
1384934e0f20SAdrian Hunter 	if (threads != evlist->threads) {
13853de5cfb0SJiri Olsa 		thread_map__put(evlist->threads);
1386a55e5663SArnaldo Carvalho de Melo 		evlist->threads = thread_map__get(threads);
1387934e0f20SAdrian Hunter 	}
13883de5cfb0SJiri Olsa 
1389ec9a77a7SAdrian Hunter 	perf_evlist__propagate_maps(evlist);
13903de5cfb0SJiri Olsa }
13913de5cfb0SJiri Olsa 
139222c8a376SArnaldo Carvalho de Melo void __perf_evlist__set_sample_bit(struct perf_evlist *evlist,
139322c8a376SArnaldo Carvalho de Melo 				   enum perf_event_sample_format bit)
139422c8a376SArnaldo Carvalho de Melo {
139522c8a376SArnaldo Carvalho de Melo 	struct perf_evsel *evsel;
139622c8a376SArnaldo Carvalho de Melo 
1397e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, evsel)
139822c8a376SArnaldo Carvalho de Melo 		__perf_evsel__set_sample_bit(evsel, bit);
139922c8a376SArnaldo Carvalho de Melo }
140022c8a376SArnaldo Carvalho de Melo 
140122c8a376SArnaldo Carvalho de Melo void __perf_evlist__reset_sample_bit(struct perf_evlist *evlist,
140222c8a376SArnaldo Carvalho de Melo 				     enum perf_event_sample_format bit)
140322c8a376SArnaldo Carvalho de Melo {
140422c8a376SArnaldo Carvalho de Melo 	struct perf_evsel *evsel;
140522c8a376SArnaldo Carvalho de Melo 
1406e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, evsel)
140722c8a376SArnaldo Carvalho de Melo 		__perf_evsel__reset_sample_bit(evsel, bit);
140822c8a376SArnaldo Carvalho de Melo }
140922c8a376SArnaldo Carvalho de Melo 
141023d4aad4SArnaldo Carvalho de Melo int perf_evlist__apply_filters(struct perf_evlist *evlist, struct perf_evsel **err_evsel)
14110a102479SFrederic Weisbecker {
14120a102479SFrederic Weisbecker 	struct perf_evsel *evsel;
1413745cefc5SArnaldo Carvalho de Melo 	int err = 0;
1414745cefc5SArnaldo Carvalho de Melo 	const int ncpus = cpu_map__nr(evlist->cpus),
1415b3a319d5SNamhyung Kim 		  nthreads = thread_map__nr(evlist->threads);
14160a102479SFrederic Weisbecker 
1417e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, evsel) {
1418745cefc5SArnaldo Carvalho de Melo 		if (evsel->filter == NULL)
14190a102479SFrederic Weisbecker 			continue;
1420745cefc5SArnaldo Carvalho de Melo 
1421d988d5eeSKan Liang 		/*
1422d988d5eeSKan Liang 		 * filters only work for tracepoint event, which doesn't have cpu limit.
1423d988d5eeSKan Liang 		 * So evlist and evsel should always be same.
1424d988d5eeSKan Liang 		 */
1425f47805a2SArnaldo Carvalho de Melo 		err = perf_evsel__apply_filter(evsel, ncpus, nthreads, evsel->filter);
142623d4aad4SArnaldo Carvalho de Melo 		if (err) {
142723d4aad4SArnaldo Carvalho de Melo 			*err_evsel = evsel;
1428745cefc5SArnaldo Carvalho de Melo 			break;
14290a102479SFrederic Weisbecker 		}
143023d4aad4SArnaldo Carvalho de Melo 	}
14310a102479SFrederic Weisbecker 
1432745cefc5SArnaldo Carvalho de Melo 	return err;
1433745cefc5SArnaldo Carvalho de Melo }
1434745cefc5SArnaldo Carvalho de Melo 
1435745cefc5SArnaldo Carvalho de Melo int perf_evlist__set_filter(struct perf_evlist *evlist, const char *filter)
1436745cefc5SArnaldo Carvalho de Melo {
1437745cefc5SArnaldo Carvalho de Melo 	struct perf_evsel *evsel;
1438745cefc5SArnaldo Carvalho de Melo 	int err = 0;
1439745cefc5SArnaldo Carvalho de Melo 
1440e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, evsel) {
1441fdf14720SWang Nan 		if (evsel->attr.type != PERF_TYPE_TRACEPOINT)
1442fdf14720SWang Nan 			continue;
1443fdf14720SWang Nan 
144494ad89bcSArnaldo Carvalho de Melo 		err = perf_evsel__set_filter(evsel, filter);
1445745cefc5SArnaldo Carvalho de Melo 		if (err)
1446745cefc5SArnaldo Carvalho de Melo 			break;
1447745cefc5SArnaldo Carvalho de Melo 	}
1448745cefc5SArnaldo Carvalho de Melo 
1449745cefc5SArnaldo Carvalho de Melo 	return err;
14500a102479SFrederic Weisbecker }
145174429964SFrederic Weisbecker 
1452be199adaSArnaldo Carvalho de Melo int perf_evlist__set_filter_pids(struct perf_evlist *evlist, size_t npids, pid_t *pids)
1453cfd70a26SArnaldo Carvalho de Melo {
1454cfd70a26SArnaldo Carvalho de Melo 	char *filter;
1455be199adaSArnaldo Carvalho de Melo 	int ret = -1;
1456be199adaSArnaldo Carvalho de Melo 	size_t i;
1457cfd70a26SArnaldo Carvalho de Melo 
1458be199adaSArnaldo Carvalho de Melo 	for (i = 0; i < npids; ++i) {
1459be199adaSArnaldo Carvalho de Melo 		if (i == 0) {
1460be199adaSArnaldo Carvalho de Melo 			if (asprintf(&filter, "common_pid != %d", pids[i]) < 0)
1461cfd70a26SArnaldo Carvalho de Melo 				return -1;
1462be199adaSArnaldo Carvalho de Melo 		} else {
1463be199adaSArnaldo Carvalho de Melo 			char *tmp;
1464be199adaSArnaldo Carvalho de Melo 
1465be199adaSArnaldo Carvalho de Melo 			if (asprintf(&tmp, "%s && common_pid != %d", filter, pids[i]) < 0)
1466be199adaSArnaldo Carvalho de Melo 				goto out_free;
1467be199adaSArnaldo Carvalho de Melo 
1468be199adaSArnaldo Carvalho de Melo 			free(filter);
1469be199adaSArnaldo Carvalho de Melo 			filter = tmp;
1470be199adaSArnaldo Carvalho de Melo 		}
1471be199adaSArnaldo Carvalho de Melo 	}
1472cfd70a26SArnaldo Carvalho de Melo 
1473cfd70a26SArnaldo Carvalho de Melo 	ret = perf_evlist__set_filter(evlist, filter);
1474be199adaSArnaldo Carvalho de Melo out_free:
1475cfd70a26SArnaldo Carvalho de Melo 	free(filter);
1476cfd70a26SArnaldo Carvalho de Melo 	return ret;
1477cfd70a26SArnaldo Carvalho de Melo }
1478cfd70a26SArnaldo Carvalho de Melo 
1479be199adaSArnaldo Carvalho de Melo int perf_evlist__set_filter_pid(struct perf_evlist *evlist, pid_t pid)
1480be199adaSArnaldo Carvalho de Melo {
1481be199adaSArnaldo Carvalho de Melo 	return perf_evlist__set_filter_pids(evlist, 1, &pid);
1482be199adaSArnaldo Carvalho de Melo }
1483be199adaSArnaldo Carvalho de Melo 
14840c21f736SArnaldo Carvalho de Melo bool perf_evlist__valid_sample_type(struct perf_evlist *evlist)
148574429964SFrederic Weisbecker {
148675562573SAdrian Hunter 	struct perf_evsel *pos;
1487c2a70653SArnaldo Carvalho de Melo 
148875562573SAdrian Hunter 	if (evlist->nr_entries == 1)
148975562573SAdrian Hunter 		return true;
149075562573SAdrian Hunter 
149175562573SAdrian Hunter 	if (evlist->id_pos < 0 || evlist->is_pos < 0)
149275562573SAdrian Hunter 		return false;
149375562573SAdrian Hunter 
1494e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, pos) {
149575562573SAdrian Hunter 		if (pos->id_pos != evlist->id_pos ||
149675562573SAdrian Hunter 		    pos->is_pos != evlist->is_pos)
1497c2a70653SArnaldo Carvalho de Melo 			return false;
149874429964SFrederic Weisbecker 	}
149974429964SFrederic Weisbecker 
1500c2a70653SArnaldo Carvalho de Melo 	return true;
1501c2a70653SArnaldo Carvalho de Melo }
1502c2a70653SArnaldo Carvalho de Melo 
150375562573SAdrian Hunter u64 __perf_evlist__combined_sample_type(struct perf_evlist *evlist)
1504c2a70653SArnaldo Carvalho de Melo {
150575562573SAdrian Hunter 	struct perf_evsel *evsel;
150675562573SAdrian Hunter 
150775562573SAdrian Hunter 	if (evlist->combined_sample_type)
150875562573SAdrian Hunter 		return evlist->combined_sample_type;
150975562573SAdrian Hunter 
1510e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, evsel)
151175562573SAdrian Hunter 		evlist->combined_sample_type |= evsel->attr.sample_type;
151275562573SAdrian Hunter 
151375562573SAdrian Hunter 	return evlist->combined_sample_type;
151475562573SAdrian Hunter }
151575562573SAdrian Hunter 
151675562573SAdrian Hunter u64 perf_evlist__combined_sample_type(struct perf_evlist *evlist)
151775562573SAdrian Hunter {
151875562573SAdrian Hunter 	evlist->combined_sample_type = 0;
151975562573SAdrian Hunter 	return __perf_evlist__combined_sample_type(evlist);
1520c2a70653SArnaldo Carvalho de Melo }
1521c2a70653SArnaldo Carvalho de Melo 
152298df858eSAndi Kleen u64 perf_evlist__combined_branch_type(struct perf_evlist *evlist)
152398df858eSAndi Kleen {
152498df858eSAndi Kleen 	struct perf_evsel *evsel;
152598df858eSAndi Kleen 	u64 branch_type = 0;
152698df858eSAndi Kleen 
1527e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, evsel)
152898df858eSAndi Kleen 		branch_type |= evsel->attr.branch_sample_type;
152998df858eSAndi Kleen 	return branch_type;
153098df858eSAndi Kleen }
153198df858eSAndi Kleen 
15329ede473cSJiri Olsa bool perf_evlist__valid_read_format(struct perf_evlist *evlist)
15339ede473cSJiri Olsa {
15349ede473cSJiri Olsa 	struct perf_evsel *first = perf_evlist__first(evlist), *pos = first;
15359ede473cSJiri Olsa 	u64 read_format = first->attr.read_format;
15369ede473cSJiri Olsa 	u64 sample_type = first->attr.sample_type;
15379ede473cSJiri Olsa 
1538e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, pos) {
15399ede473cSJiri Olsa 		if (read_format != pos->attr.read_format)
15409ede473cSJiri Olsa 			return false;
15419ede473cSJiri Olsa 	}
15429ede473cSJiri Olsa 
15439ede473cSJiri Olsa 	/* PERF_SAMPLE_READ imples PERF_FORMAT_ID. */
15449ede473cSJiri Olsa 	if ((sample_type & PERF_SAMPLE_READ) &&
15459ede473cSJiri Olsa 	    !(read_format & PERF_FORMAT_ID)) {
15469ede473cSJiri Olsa 		return false;
15479ede473cSJiri Olsa 	}
15489ede473cSJiri Olsa 
15499ede473cSJiri Olsa 	return true;
15509ede473cSJiri Olsa }
15519ede473cSJiri Olsa 
15529ede473cSJiri Olsa u64 perf_evlist__read_format(struct perf_evlist *evlist)
15539ede473cSJiri Olsa {
15549ede473cSJiri Olsa 	struct perf_evsel *first = perf_evlist__first(evlist);
15559ede473cSJiri Olsa 	return first->attr.read_format;
15569ede473cSJiri Olsa }
15579ede473cSJiri Olsa 
15580c21f736SArnaldo Carvalho de Melo u16 perf_evlist__id_hdr_size(struct perf_evlist *evlist)
155981e36bffSArnaldo Carvalho de Melo {
15600c21f736SArnaldo Carvalho de Melo 	struct perf_evsel *first = perf_evlist__first(evlist);
156181e36bffSArnaldo Carvalho de Melo 	struct perf_sample *data;
156281e36bffSArnaldo Carvalho de Melo 	u64 sample_type;
156381e36bffSArnaldo Carvalho de Melo 	u16 size = 0;
156481e36bffSArnaldo Carvalho de Melo 
156581e36bffSArnaldo Carvalho de Melo 	if (!first->attr.sample_id_all)
156681e36bffSArnaldo Carvalho de Melo 		goto out;
156781e36bffSArnaldo Carvalho de Melo 
156881e36bffSArnaldo Carvalho de Melo 	sample_type = first->attr.sample_type;
156981e36bffSArnaldo Carvalho de Melo 
157081e36bffSArnaldo Carvalho de Melo 	if (sample_type & PERF_SAMPLE_TID)
157181e36bffSArnaldo Carvalho de Melo 		size += sizeof(data->tid) * 2;
157281e36bffSArnaldo Carvalho de Melo 
157381e36bffSArnaldo Carvalho de Melo        if (sample_type & PERF_SAMPLE_TIME)
157481e36bffSArnaldo Carvalho de Melo 		size += sizeof(data->time);
157581e36bffSArnaldo Carvalho de Melo 
157681e36bffSArnaldo Carvalho de Melo 	if (sample_type & PERF_SAMPLE_ID)
157781e36bffSArnaldo Carvalho de Melo 		size += sizeof(data->id);
157881e36bffSArnaldo Carvalho de Melo 
157981e36bffSArnaldo Carvalho de Melo 	if (sample_type & PERF_SAMPLE_STREAM_ID)
158081e36bffSArnaldo Carvalho de Melo 		size += sizeof(data->stream_id);
158181e36bffSArnaldo Carvalho de Melo 
158281e36bffSArnaldo Carvalho de Melo 	if (sample_type & PERF_SAMPLE_CPU)
158381e36bffSArnaldo Carvalho de Melo 		size += sizeof(data->cpu) * 2;
158475562573SAdrian Hunter 
158575562573SAdrian Hunter 	if (sample_type & PERF_SAMPLE_IDENTIFIER)
158675562573SAdrian Hunter 		size += sizeof(data->id);
158781e36bffSArnaldo Carvalho de Melo out:
158881e36bffSArnaldo Carvalho de Melo 	return size;
158981e36bffSArnaldo Carvalho de Melo }
159081e36bffSArnaldo Carvalho de Melo 
15910c21f736SArnaldo Carvalho de Melo bool perf_evlist__valid_sample_id_all(struct perf_evlist *evlist)
1592c2a70653SArnaldo Carvalho de Melo {
15930c21f736SArnaldo Carvalho de Melo 	struct perf_evsel *first = perf_evlist__first(evlist), *pos = first;
1594c2a70653SArnaldo Carvalho de Melo 
1595e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry_continue(evlist, pos) {
1596c2a70653SArnaldo Carvalho de Melo 		if (first->attr.sample_id_all != pos->attr.sample_id_all)
1597c2a70653SArnaldo Carvalho de Melo 			return false;
1598c2a70653SArnaldo Carvalho de Melo 	}
1599c2a70653SArnaldo Carvalho de Melo 
1600c2a70653SArnaldo Carvalho de Melo 	return true;
160174429964SFrederic Weisbecker }
160274429964SFrederic Weisbecker 
16030c21f736SArnaldo Carvalho de Melo bool perf_evlist__sample_id_all(struct perf_evlist *evlist)
160474429964SFrederic Weisbecker {
16050c21f736SArnaldo Carvalho de Melo 	struct perf_evsel *first = perf_evlist__first(evlist);
1606c2a70653SArnaldo Carvalho de Melo 	return first->attr.sample_id_all;
160774429964SFrederic Weisbecker }
160881cce8deSArnaldo Carvalho de Melo 
160981cce8deSArnaldo Carvalho de Melo void perf_evlist__set_selected(struct perf_evlist *evlist,
161081cce8deSArnaldo Carvalho de Melo 			       struct perf_evsel *evsel)
161181cce8deSArnaldo Carvalho de Melo {
161281cce8deSArnaldo Carvalho de Melo 	evlist->selected = evsel;
161381cce8deSArnaldo Carvalho de Melo }
1614727ab04eSArnaldo Carvalho de Melo 
1615a74b4b66SNamhyung Kim void perf_evlist__close(struct perf_evlist *evlist)
1616a74b4b66SNamhyung Kim {
1617a74b4b66SNamhyung Kim 	struct perf_evsel *evsel;
1618a74b4b66SNamhyung Kim 	int ncpus = cpu_map__nr(evlist->cpus);
1619a74b4b66SNamhyung Kim 	int nthreads = thread_map__nr(evlist->threads);
16208ad9219eSStephane Eranian 	int n;
1621a74b4b66SNamhyung Kim 
1622e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry_reverse(evlist, evsel) {
16238ad9219eSStephane Eranian 		n = evsel->cpus ? evsel->cpus->nr : ncpus;
16248ad9219eSStephane Eranian 		perf_evsel__close(evsel, n, nthreads);
16258ad9219eSStephane Eranian 	}
1626a74b4b66SNamhyung Kim }
1627a74b4b66SNamhyung Kim 
16284112eb18SArnaldo Carvalho de Melo static int perf_evlist__create_syswide_maps(struct perf_evlist *evlist)
16294112eb18SArnaldo Carvalho de Melo {
16308c0498b6SAdrian Hunter 	struct cpu_map	  *cpus;
16318c0498b6SAdrian Hunter 	struct thread_map *threads;
16324112eb18SArnaldo Carvalho de Melo 	int err = -ENOMEM;
16334112eb18SArnaldo Carvalho de Melo 
16344112eb18SArnaldo Carvalho de Melo 	/*
16354112eb18SArnaldo Carvalho de Melo 	 * Try reading /sys/devices/system/cpu/online to get
16364112eb18SArnaldo Carvalho de Melo 	 * an all cpus map.
16374112eb18SArnaldo Carvalho de Melo 	 *
16384112eb18SArnaldo Carvalho de Melo 	 * FIXME: -ENOMEM is the best we can do here, the cpu_map
16394112eb18SArnaldo Carvalho de Melo 	 * code needs an overhaul to properly forward the
16404112eb18SArnaldo Carvalho de Melo 	 * error, and we may not want to do that fallback to a
16414112eb18SArnaldo Carvalho de Melo 	 * default cpu identity map :-\
16424112eb18SArnaldo Carvalho de Melo 	 */
16438c0498b6SAdrian Hunter 	cpus = cpu_map__new(NULL);
16448c0498b6SAdrian Hunter 	if (!cpus)
16454112eb18SArnaldo Carvalho de Melo 		goto out;
16464112eb18SArnaldo Carvalho de Melo 
16478c0498b6SAdrian Hunter 	threads = thread_map__new_dummy();
16488c0498b6SAdrian Hunter 	if (!threads)
16498c0498b6SAdrian Hunter 		goto out_put;
16504112eb18SArnaldo Carvalho de Melo 
16518c0498b6SAdrian Hunter 	perf_evlist__set_maps(evlist, cpus, threads);
16524112eb18SArnaldo Carvalho de Melo out:
16534112eb18SArnaldo Carvalho de Melo 	return err;
16548c0498b6SAdrian Hunter out_put:
16558c0498b6SAdrian Hunter 	cpu_map__put(cpus);
16564112eb18SArnaldo Carvalho de Melo 	goto out;
16574112eb18SArnaldo Carvalho de Melo }
16584112eb18SArnaldo Carvalho de Melo 
16596a4bb04cSJiri Olsa int perf_evlist__open(struct perf_evlist *evlist)
1660727ab04eSArnaldo Carvalho de Melo {
16616a4bb04cSJiri Olsa 	struct perf_evsel *evsel;
1662a74b4b66SNamhyung Kim 	int err;
1663727ab04eSArnaldo Carvalho de Melo 
16644112eb18SArnaldo Carvalho de Melo 	/*
16654112eb18SArnaldo Carvalho de Melo 	 * Default: one fd per CPU, all threads, aka systemwide
16664112eb18SArnaldo Carvalho de Melo 	 * as sys_perf_event_open(cpu = -1, thread = -1) is EINVAL
16674112eb18SArnaldo Carvalho de Melo 	 */
16684112eb18SArnaldo Carvalho de Melo 	if (evlist->threads == NULL && evlist->cpus == NULL) {
16694112eb18SArnaldo Carvalho de Melo 		err = perf_evlist__create_syswide_maps(evlist);
16704112eb18SArnaldo Carvalho de Melo 		if (err < 0)
16714112eb18SArnaldo Carvalho de Melo 			goto out_err;
16724112eb18SArnaldo Carvalho de Melo 	}
16734112eb18SArnaldo Carvalho de Melo 
1674733cd2feSAdrian Hunter 	perf_evlist__update_id_pos(evlist);
1675733cd2feSAdrian Hunter 
1676e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, evsel) {
167723df7f79SAdrian Hunter 		err = perf_evsel__open(evsel, evsel->cpus, evsel->threads);
1678727ab04eSArnaldo Carvalho de Melo 		if (err < 0)
1679727ab04eSArnaldo Carvalho de Melo 			goto out_err;
1680727ab04eSArnaldo Carvalho de Melo 	}
1681727ab04eSArnaldo Carvalho de Melo 
1682727ab04eSArnaldo Carvalho de Melo 	return 0;
1683727ab04eSArnaldo Carvalho de Melo out_err:
1684a74b4b66SNamhyung Kim 	perf_evlist__close(evlist);
168541c21a68SNamhyung Kim 	errno = -err;
1686727ab04eSArnaldo Carvalho de Melo 	return err;
1687727ab04eSArnaldo Carvalho de Melo }
168835b9d88eSArnaldo Carvalho de Melo 
1689602ad878SArnaldo Carvalho de Melo int perf_evlist__prepare_workload(struct perf_evlist *evlist, struct target *target,
169055e162eaSNamhyung Kim 				  const char *argv[], bool pipe_output,
1691735f7e0bSArnaldo Carvalho de Melo 				  void (*exec_error)(int signo, siginfo_t *info, void *ucontext))
169235b9d88eSArnaldo Carvalho de Melo {
169335b9d88eSArnaldo Carvalho de Melo 	int child_ready_pipe[2], go_pipe[2];
169435b9d88eSArnaldo Carvalho de Melo 	char bf;
169535b9d88eSArnaldo Carvalho de Melo 
169635b9d88eSArnaldo Carvalho de Melo 	if (pipe(child_ready_pipe) < 0) {
169735b9d88eSArnaldo Carvalho de Melo 		perror("failed to create 'ready' pipe");
169835b9d88eSArnaldo Carvalho de Melo 		return -1;
169935b9d88eSArnaldo Carvalho de Melo 	}
170035b9d88eSArnaldo Carvalho de Melo 
170135b9d88eSArnaldo Carvalho de Melo 	if (pipe(go_pipe) < 0) {
170235b9d88eSArnaldo Carvalho de Melo 		perror("failed to create 'go' pipe");
170335b9d88eSArnaldo Carvalho de Melo 		goto out_close_ready_pipe;
170435b9d88eSArnaldo Carvalho de Melo 	}
170535b9d88eSArnaldo Carvalho de Melo 
170635b9d88eSArnaldo Carvalho de Melo 	evlist->workload.pid = fork();
170735b9d88eSArnaldo Carvalho de Melo 	if (evlist->workload.pid < 0) {
170835b9d88eSArnaldo Carvalho de Melo 		perror("failed to fork");
170935b9d88eSArnaldo Carvalho de Melo 		goto out_close_pipes;
171035b9d88eSArnaldo Carvalho de Melo 	}
171135b9d88eSArnaldo Carvalho de Melo 
171235b9d88eSArnaldo Carvalho de Melo 	if (!evlist->workload.pid) {
17135f1c4225SArnaldo Carvalho de Melo 		int ret;
17145f1c4225SArnaldo Carvalho de Melo 
1715119fa3c9SNamhyung Kim 		if (pipe_output)
171635b9d88eSArnaldo Carvalho de Melo 			dup2(2, 1);
171735b9d88eSArnaldo Carvalho de Melo 
17180817df08SDavid Ahern 		signal(SIGTERM, SIG_DFL);
17190817df08SDavid Ahern 
172035b9d88eSArnaldo Carvalho de Melo 		close(child_ready_pipe[0]);
172135b9d88eSArnaldo Carvalho de Melo 		close(go_pipe[1]);
172235b9d88eSArnaldo Carvalho de Melo 		fcntl(go_pipe[0], F_SETFD, FD_CLOEXEC);
172335b9d88eSArnaldo Carvalho de Melo 
172435b9d88eSArnaldo Carvalho de Melo 		/*
172535b9d88eSArnaldo Carvalho de Melo 		 * Tell the parent we're ready to go
172635b9d88eSArnaldo Carvalho de Melo 		 */
172735b9d88eSArnaldo Carvalho de Melo 		close(child_ready_pipe[1]);
172835b9d88eSArnaldo Carvalho de Melo 
172935b9d88eSArnaldo Carvalho de Melo 		/*
173035b9d88eSArnaldo Carvalho de Melo 		 * Wait until the parent tells us to go.
173135b9d88eSArnaldo Carvalho de Melo 		 */
17325f1c4225SArnaldo Carvalho de Melo 		ret = read(go_pipe[0], &bf, 1);
17335f1c4225SArnaldo Carvalho de Melo 		/*
17345f1c4225SArnaldo Carvalho de Melo 		 * The parent will ask for the execvp() to be performed by
17355f1c4225SArnaldo Carvalho de Melo 		 * writing exactly one byte, in workload.cork_fd, usually via
17365f1c4225SArnaldo Carvalho de Melo 		 * perf_evlist__start_workload().
17375f1c4225SArnaldo Carvalho de Melo 		 *
173820f86fc1SArnaldo Carvalho de Melo 		 * For cancelling the workload without actually running it,
17395f1c4225SArnaldo Carvalho de Melo 		 * the parent will just close workload.cork_fd, without writing
17405f1c4225SArnaldo Carvalho de Melo 		 * anything, i.e. read will return zero and we just exit()
17415f1c4225SArnaldo Carvalho de Melo 		 * here.
17425f1c4225SArnaldo Carvalho de Melo 		 */
17435f1c4225SArnaldo Carvalho de Melo 		if (ret != 1) {
17445f1c4225SArnaldo Carvalho de Melo 			if (ret == -1)
174535b9d88eSArnaldo Carvalho de Melo 				perror("unable to read pipe");
17465f1c4225SArnaldo Carvalho de Melo 			exit(ret);
17475f1c4225SArnaldo Carvalho de Melo 		}
174835b9d88eSArnaldo Carvalho de Melo 
174935b9d88eSArnaldo Carvalho de Melo 		execvp(argv[0], (char **)argv);
175035b9d88eSArnaldo Carvalho de Melo 
1751735f7e0bSArnaldo Carvalho de Melo 		if (exec_error) {
1752f33cbe72SArnaldo Carvalho de Melo 			union sigval val;
1753f33cbe72SArnaldo Carvalho de Melo 
1754f33cbe72SArnaldo Carvalho de Melo 			val.sival_int = errno;
1755f33cbe72SArnaldo Carvalho de Melo 			if (sigqueue(getppid(), SIGUSR1, val))
175635b9d88eSArnaldo Carvalho de Melo 				perror(argv[0]);
1757f33cbe72SArnaldo Carvalho de Melo 		} else
1758f33cbe72SArnaldo Carvalho de Melo 			perror(argv[0]);
175935b9d88eSArnaldo Carvalho de Melo 		exit(-1);
176035b9d88eSArnaldo Carvalho de Melo 	}
176135b9d88eSArnaldo Carvalho de Melo 
1762735f7e0bSArnaldo Carvalho de Melo 	if (exec_error) {
1763735f7e0bSArnaldo Carvalho de Melo 		struct sigaction act = {
1764735f7e0bSArnaldo Carvalho de Melo 			.sa_flags     = SA_SIGINFO,
1765735f7e0bSArnaldo Carvalho de Melo 			.sa_sigaction = exec_error,
1766735f7e0bSArnaldo Carvalho de Melo 		};
1767735f7e0bSArnaldo Carvalho de Melo 		sigaction(SIGUSR1, &act, NULL);
1768735f7e0bSArnaldo Carvalho de Melo 	}
1769735f7e0bSArnaldo Carvalho de Melo 
17701aaf63b1SArnaldo Carvalho de Melo 	if (target__none(target)) {
17711aaf63b1SArnaldo Carvalho de Melo 		if (evlist->threads == NULL) {
17721aaf63b1SArnaldo Carvalho de Melo 			fprintf(stderr, "FATAL: evlist->threads need to be set at this point (%s:%d).\n",
17731aaf63b1SArnaldo Carvalho de Melo 				__func__, __LINE__);
17741aaf63b1SArnaldo Carvalho de Melo 			goto out_close_pipes;
17751aaf63b1SArnaldo Carvalho de Melo 		}
1776e13798c7SJiri Olsa 		thread_map__set_pid(evlist->threads, 0, evlist->workload.pid);
17771aaf63b1SArnaldo Carvalho de Melo 	}
177835b9d88eSArnaldo Carvalho de Melo 
177935b9d88eSArnaldo Carvalho de Melo 	close(child_ready_pipe[1]);
178035b9d88eSArnaldo Carvalho de Melo 	close(go_pipe[0]);
178135b9d88eSArnaldo Carvalho de Melo 	/*
178235b9d88eSArnaldo Carvalho de Melo 	 * wait for child to settle
178335b9d88eSArnaldo Carvalho de Melo 	 */
178435b9d88eSArnaldo Carvalho de Melo 	if (read(child_ready_pipe[0], &bf, 1) == -1) {
178535b9d88eSArnaldo Carvalho de Melo 		perror("unable to read pipe");
178635b9d88eSArnaldo Carvalho de Melo 		goto out_close_pipes;
178735b9d88eSArnaldo Carvalho de Melo 	}
178835b9d88eSArnaldo Carvalho de Melo 
1789bcf3145fSNamhyung Kim 	fcntl(go_pipe[1], F_SETFD, FD_CLOEXEC);
179035b9d88eSArnaldo Carvalho de Melo 	evlist->workload.cork_fd = go_pipe[1];
179135b9d88eSArnaldo Carvalho de Melo 	close(child_ready_pipe[0]);
179235b9d88eSArnaldo Carvalho de Melo 	return 0;
179335b9d88eSArnaldo Carvalho de Melo 
179435b9d88eSArnaldo Carvalho de Melo out_close_pipes:
179535b9d88eSArnaldo Carvalho de Melo 	close(go_pipe[0]);
179635b9d88eSArnaldo Carvalho de Melo 	close(go_pipe[1]);
179735b9d88eSArnaldo Carvalho de Melo out_close_ready_pipe:
179835b9d88eSArnaldo Carvalho de Melo 	close(child_ready_pipe[0]);
179935b9d88eSArnaldo Carvalho de Melo 	close(child_ready_pipe[1]);
180035b9d88eSArnaldo Carvalho de Melo 	return -1;
180135b9d88eSArnaldo Carvalho de Melo }
180235b9d88eSArnaldo Carvalho de Melo 
180335b9d88eSArnaldo Carvalho de Melo int perf_evlist__start_workload(struct perf_evlist *evlist)
180435b9d88eSArnaldo Carvalho de Melo {
180535b9d88eSArnaldo Carvalho de Melo 	if (evlist->workload.cork_fd > 0) {
1806b3824404SDavid Ahern 		char bf = 0;
1807bcf3145fSNamhyung Kim 		int ret;
180835b9d88eSArnaldo Carvalho de Melo 		/*
180935b9d88eSArnaldo Carvalho de Melo 		 * Remove the cork, let it rip!
181035b9d88eSArnaldo Carvalho de Melo 		 */
1811bcf3145fSNamhyung Kim 		ret = write(evlist->workload.cork_fd, &bf, 1);
1812bcf3145fSNamhyung Kim 		if (ret < 0)
1813bcf3145fSNamhyung Kim 			perror("enable to write to pipe");
1814bcf3145fSNamhyung Kim 
1815bcf3145fSNamhyung Kim 		close(evlist->workload.cork_fd);
1816bcf3145fSNamhyung Kim 		return ret;
181735b9d88eSArnaldo Carvalho de Melo 	}
181835b9d88eSArnaldo Carvalho de Melo 
181935b9d88eSArnaldo Carvalho de Melo 	return 0;
182035b9d88eSArnaldo Carvalho de Melo }
1821cb0b29e0SArnaldo Carvalho de Melo 
1822a3f698feSArnaldo Carvalho de Melo int perf_evlist__parse_sample(struct perf_evlist *evlist, union perf_event *event,
18230807d2d8SArnaldo Carvalho de Melo 			      struct perf_sample *sample)
1824cb0b29e0SArnaldo Carvalho de Melo {
182575562573SAdrian Hunter 	struct perf_evsel *evsel = perf_evlist__event2evsel(evlist, event);
182675562573SAdrian Hunter 
182775562573SAdrian Hunter 	if (!evsel)
182875562573SAdrian Hunter 		return -EFAULT;
18290807d2d8SArnaldo Carvalho de Melo 	return perf_evsel__parse_sample(evsel, event, sample);
1830cb0b29e0SArnaldo Carvalho de Melo }
183178f067b3SArnaldo Carvalho de Melo 
183278f067b3SArnaldo Carvalho de Melo size_t perf_evlist__fprintf(struct perf_evlist *evlist, FILE *fp)
183378f067b3SArnaldo Carvalho de Melo {
183478f067b3SArnaldo Carvalho de Melo 	struct perf_evsel *evsel;
183578f067b3SArnaldo Carvalho de Melo 	size_t printed = 0;
183678f067b3SArnaldo Carvalho de Melo 
1837e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, evsel) {
183878f067b3SArnaldo Carvalho de Melo 		printed += fprintf(fp, "%s%s", evsel->idx ? ", " : "",
183978f067b3SArnaldo Carvalho de Melo 				   perf_evsel__name(evsel));
184078f067b3SArnaldo Carvalho de Melo 	}
184178f067b3SArnaldo Carvalho de Melo 
1842b2222139SDavidlohr Bueso 	return printed + fprintf(fp, "\n");
184378f067b3SArnaldo Carvalho de Melo }
18446ef068cbSArnaldo Carvalho de Melo 
1845d9aade7fSArnaldo Carvalho de Melo int perf_evlist__strerror_open(struct perf_evlist *evlist,
1846a8f23d8fSArnaldo Carvalho de Melo 			       int err, char *buf, size_t size)
1847a8f23d8fSArnaldo Carvalho de Melo {
1848a8f23d8fSArnaldo Carvalho de Melo 	int printed, value;
1849c8b5f2c9SArnaldo Carvalho de Melo 	char sbuf[STRERR_BUFSIZE], *emsg = str_error_r(err, sbuf, sizeof(sbuf));
1850a8f23d8fSArnaldo Carvalho de Melo 
1851a8f23d8fSArnaldo Carvalho de Melo 	switch (err) {
1852a8f23d8fSArnaldo Carvalho de Melo 	case EACCES:
1853a8f23d8fSArnaldo Carvalho de Melo 	case EPERM:
1854a8f23d8fSArnaldo Carvalho de Melo 		printed = scnprintf(buf, size,
1855a8f23d8fSArnaldo Carvalho de Melo 				    "Error:\t%s.\n"
1856a8f23d8fSArnaldo Carvalho de Melo 				    "Hint:\tCheck /proc/sys/kernel/perf_event_paranoid setting.", emsg);
1857a8f23d8fSArnaldo Carvalho de Melo 
18581a47245dSAdrian Hunter 		value = perf_event_paranoid();
1859a8f23d8fSArnaldo Carvalho de Melo 
1860a8f23d8fSArnaldo Carvalho de Melo 		printed += scnprintf(buf + printed, size - printed, "\nHint:\t");
1861a8f23d8fSArnaldo Carvalho de Melo 
1862a8f23d8fSArnaldo Carvalho de Melo 		if (value >= 2) {
1863a8f23d8fSArnaldo Carvalho de Melo 			printed += scnprintf(buf + printed, size - printed,
1864a8f23d8fSArnaldo Carvalho de Melo 					     "For your workloads it needs to be <= 1\nHint:\t");
1865a8f23d8fSArnaldo Carvalho de Melo 		}
1866a8f23d8fSArnaldo Carvalho de Melo 		printed += scnprintf(buf + printed, size - printed,
18675229e366SArnaldo Carvalho de Melo 				     "For system wide tracing it needs to be set to -1.\n");
1868a8f23d8fSArnaldo Carvalho de Melo 
1869a8f23d8fSArnaldo Carvalho de Melo 		printed += scnprintf(buf + printed, size - printed,
18705229e366SArnaldo Carvalho de Melo 				    "Hint:\tTry: 'sudo sh -c \"echo -1 > /proc/sys/kernel/perf_event_paranoid\"'\n"
18715229e366SArnaldo Carvalho de Melo 				    "Hint:\tThe current value is %d.", value);
1872a8f23d8fSArnaldo Carvalho de Melo 		break;
1873d9aade7fSArnaldo Carvalho de Melo 	case EINVAL: {
1874d9aade7fSArnaldo Carvalho de Melo 		struct perf_evsel *first = perf_evlist__first(evlist);
1875d9aade7fSArnaldo Carvalho de Melo 		int max_freq;
1876d9aade7fSArnaldo Carvalho de Melo 
1877d9aade7fSArnaldo Carvalho de Melo 		if (sysctl__read_int("kernel/perf_event_max_sample_rate", &max_freq) < 0)
1878d9aade7fSArnaldo Carvalho de Melo 			goto out_default;
1879d9aade7fSArnaldo Carvalho de Melo 
1880d9aade7fSArnaldo Carvalho de Melo 		if (first->attr.sample_freq < (u64)max_freq)
1881d9aade7fSArnaldo Carvalho de Melo 			goto out_default;
1882d9aade7fSArnaldo Carvalho de Melo 
1883d9aade7fSArnaldo Carvalho de Melo 		printed = scnprintf(buf, size,
1884d9aade7fSArnaldo Carvalho de Melo 				    "Error:\t%s.\n"
1885d9aade7fSArnaldo Carvalho de Melo 				    "Hint:\tCheck /proc/sys/kernel/perf_event_max_sample_rate.\n"
1886d9aade7fSArnaldo Carvalho de Melo 				    "Hint:\tThe current value is %d and %" PRIu64 " is being requested.",
1887d9aade7fSArnaldo Carvalho de Melo 				    emsg, max_freq, first->attr.sample_freq);
1888d9aade7fSArnaldo Carvalho de Melo 		break;
1889d9aade7fSArnaldo Carvalho de Melo 	}
1890a8f23d8fSArnaldo Carvalho de Melo 	default:
1891d9aade7fSArnaldo Carvalho de Melo out_default:
1892a8f23d8fSArnaldo Carvalho de Melo 		scnprintf(buf, size, "%s", emsg);
1893a8f23d8fSArnaldo Carvalho de Melo 		break;
1894a8f23d8fSArnaldo Carvalho de Melo 	}
1895a8f23d8fSArnaldo Carvalho de Melo 
1896a8f23d8fSArnaldo Carvalho de Melo 	return 0;
1897a8f23d8fSArnaldo Carvalho de Melo }
1898a025e4f0SAdrian Hunter 
1899956fa571SArnaldo Carvalho de Melo int perf_evlist__strerror_mmap(struct perf_evlist *evlist, int err, char *buf, size_t size)
1900956fa571SArnaldo Carvalho de Melo {
1901c8b5f2c9SArnaldo Carvalho de Melo 	char sbuf[STRERR_BUFSIZE], *emsg = str_error_r(err, sbuf, sizeof(sbuf));
1902e965bea1SArnaldo Carvalho de Melo 	int pages_attempted = evlist->mmap_len / 1024, pages_max_per_user, printed = 0;
1903956fa571SArnaldo Carvalho de Melo 
1904956fa571SArnaldo Carvalho de Melo 	switch (err) {
1905956fa571SArnaldo Carvalho de Melo 	case EPERM:
1906e5d4a290SArnaldo Carvalho de Melo 		sysctl__read_int("kernel/perf_event_mlock_kb", &pages_max_per_user);
1907e965bea1SArnaldo Carvalho de Melo 		printed += scnprintf(buf + printed, size - printed,
1908e965bea1SArnaldo Carvalho de Melo 				     "Error:\t%s.\n"
1909956fa571SArnaldo Carvalho de Melo 				     "Hint:\tCheck /proc/sys/kernel/perf_event_mlock_kb (%d kB) setting.\n"
1910e965bea1SArnaldo Carvalho de Melo 				     "Hint:\tTried using %zd kB.\n",
1911e5d4a290SArnaldo Carvalho de Melo 				     emsg, pages_max_per_user, pages_attempted);
1912e965bea1SArnaldo Carvalho de Melo 
1913e965bea1SArnaldo Carvalho de Melo 		if (pages_attempted >= pages_max_per_user) {
1914e965bea1SArnaldo Carvalho de Melo 			printed += scnprintf(buf + printed, size - printed,
1915e965bea1SArnaldo Carvalho de Melo 					     "Hint:\tTry 'sudo sh -c \"echo %d > /proc/sys/kernel/perf_event_mlock_kb\"', or\n",
1916e965bea1SArnaldo Carvalho de Melo 					     pages_max_per_user + pages_attempted);
1917e965bea1SArnaldo Carvalho de Melo 		}
1918e965bea1SArnaldo Carvalho de Melo 
1919e965bea1SArnaldo Carvalho de Melo 		printed += scnprintf(buf + printed, size - printed,
1920e965bea1SArnaldo Carvalho de Melo 				     "Hint:\tTry using a smaller -m/--mmap-pages value.");
1921956fa571SArnaldo Carvalho de Melo 		break;
1922956fa571SArnaldo Carvalho de Melo 	default:
1923956fa571SArnaldo Carvalho de Melo 		scnprintf(buf, size, "%s", emsg);
1924956fa571SArnaldo Carvalho de Melo 		break;
1925956fa571SArnaldo Carvalho de Melo 	}
1926956fa571SArnaldo Carvalho de Melo 
1927956fa571SArnaldo Carvalho de Melo 	return 0;
1928956fa571SArnaldo Carvalho de Melo }
1929956fa571SArnaldo Carvalho de Melo 
1930a025e4f0SAdrian Hunter void perf_evlist__to_front(struct perf_evlist *evlist,
1931a025e4f0SAdrian Hunter 			   struct perf_evsel *move_evsel)
1932a025e4f0SAdrian Hunter {
1933a025e4f0SAdrian Hunter 	struct perf_evsel *evsel, *n;
1934a025e4f0SAdrian Hunter 	LIST_HEAD(move);
1935a025e4f0SAdrian Hunter 
1936a025e4f0SAdrian Hunter 	if (move_evsel == perf_evlist__first(evlist))
1937a025e4f0SAdrian Hunter 		return;
1938a025e4f0SAdrian Hunter 
1939e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry_safe(evlist, n, evsel) {
1940a025e4f0SAdrian Hunter 		if (evsel->leader == move_evsel->leader)
1941a025e4f0SAdrian Hunter 			list_move_tail(&evsel->node, &move);
1942a025e4f0SAdrian Hunter 	}
1943a025e4f0SAdrian Hunter 
1944a025e4f0SAdrian Hunter 	list_splice(&move, &evlist->entries);
1945a025e4f0SAdrian Hunter }
194660b0896cSAdrian Hunter 
194760b0896cSAdrian Hunter void perf_evlist__set_tracking_event(struct perf_evlist *evlist,
194860b0896cSAdrian Hunter 				     struct perf_evsel *tracking_evsel)
194960b0896cSAdrian Hunter {
195060b0896cSAdrian Hunter 	struct perf_evsel *evsel;
195160b0896cSAdrian Hunter 
195260b0896cSAdrian Hunter 	if (tracking_evsel->tracking)
195360b0896cSAdrian Hunter 		return;
195460b0896cSAdrian Hunter 
1955e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, evsel) {
195660b0896cSAdrian Hunter 		if (evsel != tracking_evsel)
195760b0896cSAdrian Hunter 			evsel->tracking = false;
195860b0896cSAdrian Hunter 	}
195960b0896cSAdrian Hunter 
196060b0896cSAdrian Hunter 	tracking_evsel->tracking = true;
196160b0896cSAdrian Hunter }
19627630b3e2SWang Nan 
19637630b3e2SWang Nan struct perf_evsel *
19647630b3e2SWang Nan perf_evlist__find_evsel_by_str(struct perf_evlist *evlist,
19657630b3e2SWang Nan 			       const char *str)
19667630b3e2SWang Nan {
19677630b3e2SWang Nan 	struct perf_evsel *evsel;
19687630b3e2SWang Nan 
1969e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, evsel) {
19707630b3e2SWang Nan 		if (!evsel->name)
19717630b3e2SWang Nan 			continue;
19727630b3e2SWang Nan 		if (strcmp(str, evsel->name) == 0)
19737630b3e2SWang Nan 			return evsel;
19747630b3e2SWang Nan 	}
19757630b3e2SWang Nan 
19767630b3e2SWang Nan 	return NULL;
19777630b3e2SWang Nan }
1978