xref: /openbmc/linux/tools/perf/util/evlist.c (revision f5e7150c)
1f8a95309SArnaldo Carvalho de Melo /*
2f8a95309SArnaldo Carvalho de Melo  * Copyright (C) 2011, Red Hat Inc, Arnaldo Carvalho de Melo <acme@redhat.com>
3f8a95309SArnaldo Carvalho de Melo  *
4f8a95309SArnaldo Carvalho de Melo  * Parts came from builtin-{top,stat,record}.c, see those files for further
5f8a95309SArnaldo Carvalho de Melo  * copyright notes.
6f8a95309SArnaldo Carvalho de Melo  *
7f8a95309SArnaldo Carvalho de Melo  * Released under the GPL v2. (and only v2, not any later version)
8f8a95309SArnaldo Carvalho de Melo  */
9a8c9ae18SArnaldo Carvalho de Melo #include "util.h"
10956fa571SArnaldo Carvalho de Melo #include <api/fs/fs.h>
115c581041SArnaldo Carvalho de Melo #include <poll.h>
12f8a95309SArnaldo Carvalho de Melo #include "cpumap.h"
13f8a95309SArnaldo Carvalho de Melo #include "thread_map.h"
1412864b31SNamhyung Kim #include "target.h"
15361c99a6SArnaldo Carvalho de Melo #include "evlist.h"
16361c99a6SArnaldo Carvalho de Melo #include "evsel.h"
17e3e1a54fSAdrian Hunter #include "debug.h"
1835b9d88eSArnaldo Carvalho de Melo #include <unistd.h>
19361c99a6SArnaldo Carvalho de Melo 
2050d08e47SArnaldo Carvalho de Melo #include "parse-events.h"
214b6ab94eSJosh Poimboeuf #include <subcmd/parse-options.h>
2250d08e47SArnaldo Carvalho de Melo 
23f8a95309SArnaldo Carvalho de Melo #include <sys/mman.h>
24f8a95309SArnaldo Carvalho de Melo 
2570db7533SArnaldo Carvalho de Melo #include <linux/bitops.h>
2670db7533SArnaldo Carvalho de Melo #include <linux/hash.h>
270389cd1fSArnaldo Carvalho de Melo #include <linux/log2.h>
288dd2a131SJiri Olsa #include <linux/err.h>
2970db7533SArnaldo Carvalho de Melo 
30e4b356b5SArnaldo Carvalho de Melo static void perf_evlist__mmap_put(struct perf_evlist *evlist, int idx);
31e4b356b5SArnaldo Carvalho de Melo static void __perf_evlist__munmap(struct perf_evlist *evlist, int idx);
32e4b356b5SArnaldo Carvalho de Melo 
33f8a95309SArnaldo Carvalho de Melo #define FD(e, x, y) (*(int *)xyarray__entry(e->fd, x, y))
34a91e5431SArnaldo Carvalho de Melo #define SID(e, x, y) xyarray__entry(e->sample_id, x, y)
35f8a95309SArnaldo Carvalho de Melo 
367e2ed097SArnaldo Carvalho de Melo void perf_evlist__init(struct perf_evlist *evlist, struct cpu_map *cpus,
377e2ed097SArnaldo Carvalho de Melo 		       struct thread_map *threads)
38361c99a6SArnaldo Carvalho de Melo {
3970db7533SArnaldo Carvalho de Melo 	int i;
4070db7533SArnaldo Carvalho de Melo 
4170db7533SArnaldo Carvalho de Melo 	for (i = 0; i < PERF_EVLIST__HLIST_SIZE; ++i)
4270db7533SArnaldo Carvalho de Melo 		INIT_HLIST_HEAD(&evlist->heads[i]);
43361c99a6SArnaldo Carvalho de Melo 	INIT_LIST_HEAD(&evlist->entries);
447e2ed097SArnaldo Carvalho de Melo 	perf_evlist__set_maps(evlist, cpus, threads);
451b85337dSArnaldo Carvalho de Melo 	fdarray__init(&evlist->pollfd, 64);
4635b9d88eSArnaldo Carvalho de Melo 	evlist->workload.pid = -1;
47361c99a6SArnaldo Carvalho de Melo }
48361c99a6SArnaldo Carvalho de Melo 
49334fe7a3SNamhyung Kim struct perf_evlist *perf_evlist__new(void)
50ef1d1af2SArnaldo Carvalho de Melo {
51ef1d1af2SArnaldo Carvalho de Melo 	struct perf_evlist *evlist = zalloc(sizeof(*evlist));
52ef1d1af2SArnaldo Carvalho de Melo 
53ef1d1af2SArnaldo Carvalho de Melo 	if (evlist != NULL)
54334fe7a3SNamhyung Kim 		perf_evlist__init(evlist, NULL, NULL);
55ef1d1af2SArnaldo Carvalho de Melo 
56361c99a6SArnaldo Carvalho de Melo 	return evlist;
57361c99a6SArnaldo Carvalho de Melo }
58361c99a6SArnaldo Carvalho de Melo 
59b22d54b0SJiri Olsa struct perf_evlist *perf_evlist__new_default(void)
60b22d54b0SJiri Olsa {
61b22d54b0SJiri Olsa 	struct perf_evlist *evlist = perf_evlist__new();
62b22d54b0SJiri Olsa 
63b22d54b0SJiri Olsa 	if (evlist && perf_evlist__add_default(evlist)) {
64b22d54b0SJiri Olsa 		perf_evlist__delete(evlist);
65b22d54b0SJiri Olsa 		evlist = NULL;
66b22d54b0SJiri Olsa 	}
67b22d54b0SJiri Olsa 
68b22d54b0SJiri Olsa 	return evlist;
69b22d54b0SJiri Olsa }
70b22d54b0SJiri Olsa 
715bae0250SArnaldo Carvalho de Melo struct perf_evlist *perf_evlist__new_dummy(void)
725bae0250SArnaldo Carvalho de Melo {
735bae0250SArnaldo Carvalho de Melo 	struct perf_evlist *evlist = perf_evlist__new();
745bae0250SArnaldo Carvalho de Melo 
755bae0250SArnaldo Carvalho de Melo 	if (evlist && perf_evlist__add_dummy(evlist)) {
765bae0250SArnaldo Carvalho de Melo 		perf_evlist__delete(evlist);
775bae0250SArnaldo Carvalho de Melo 		evlist = NULL;
785bae0250SArnaldo Carvalho de Melo 	}
795bae0250SArnaldo Carvalho de Melo 
805bae0250SArnaldo Carvalho de Melo 	return evlist;
815bae0250SArnaldo Carvalho de Melo }
825bae0250SArnaldo Carvalho de Melo 
8375562573SAdrian Hunter /**
8475562573SAdrian Hunter  * perf_evlist__set_id_pos - set the positions of event ids.
8575562573SAdrian Hunter  * @evlist: selected event list
8675562573SAdrian Hunter  *
8775562573SAdrian Hunter  * Events with compatible sample types all have the same id_pos
8875562573SAdrian Hunter  * and is_pos.  For convenience, put a copy on evlist.
8975562573SAdrian Hunter  */
9075562573SAdrian Hunter void perf_evlist__set_id_pos(struct perf_evlist *evlist)
9175562573SAdrian Hunter {
9275562573SAdrian Hunter 	struct perf_evsel *first = perf_evlist__first(evlist);
9375562573SAdrian Hunter 
9475562573SAdrian Hunter 	evlist->id_pos = first->id_pos;
9575562573SAdrian Hunter 	evlist->is_pos = first->is_pos;
9675562573SAdrian Hunter }
9775562573SAdrian Hunter 
98733cd2feSAdrian Hunter static void perf_evlist__update_id_pos(struct perf_evlist *evlist)
99733cd2feSAdrian Hunter {
100733cd2feSAdrian Hunter 	struct perf_evsel *evsel;
101733cd2feSAdrian Hunter 
1020050f7aaSArnaldo Carvalho de Melo 	evlist__for_each(evlist, evsel)
103733cd2feSAdrian Hunter 		perf_evsel__calc_id_pos(evsel);
104733cd2feSAdrian Hunter 
105733cd2feSAdrian Hunter 	perf_evlist__set_id_pos(evlist);
106733cd2feSAdrian Hunter }
107733cd2feSAdrian Hunter 
108361c99a6SArnaldo Carvalho de Melo static void perf_evlist__purge(struct perf_evlist *evlist)
109361c99a6SArnaldo Carvalho de Melo {
110361c99a6SArnaldo Carvalho de Melo 	struct perf_evsel *pos, *n;
111361c99a6SArnaldo Carvalho de Melo 
1120050f7aaSArnaldo Carvalho de Melo 	evlist__for_each_safe(evlist, n, pos) {
113361c99a6SArnaldo Carvalho de Melo 		list_del_init(&pos->node);
114d49e4695SArnaldo Carvalho de Melo 		pos->evlist = NULL;
115361c99a6SArnaldo Carvalho de Melo 		perf_evsel__delete(pos);
116361c99a6SArnaldo Carvalho de Melo 	}
117361c99a6SArnaldo Carvalho de Melo 
118361c99a6SArnaldo Carvalho de Melo 	evlist->nr_entries = 0;
119361c99a6SArnaldo Carvalho de Melo }
120361c99a6SArnaldo Carvalho de Melo 
121ef1d1af2SArnaldo Carvalho de Melo void perf_evlist__exit(struct perf_evlist *evlist)
122ef1d1af2SArnaldo Carvalho de Melo {
12304662523SArnaldo Carvalho de Melo 	zfree(&evlist->mmap);
1241b85337dSArnaldo Carvalho de Melo 	fdarray__exit(&evlist->pollfd);
125ef1d1af2SArnaldo Carvalho de Melo }
126ef1d1af2SArnaldo Carvalho de Melo 
127361c99a6SArnaldo Carvalho de Melo void perf_evlist__delete(struct perf_evlist *evlist)
128361c99a6SArnaldo Carvalho de Melo {
129983874d1SArnaldo Carvalho de Melo 	perf_evlist__munmap(evlist);
130f26e1c7cSArnaldo Carvalho de Melo 	perf_evlist__close(evlist);
131f30a79b0SJiri Olsa 	cpu_map__put(evlist->cpus);
132186fbb74SJiri Olsa 	thread_map__put(evlist->threads);
13303ad9747SArnaldo Carvalho de Melo 	evlist->cpus = NULL;
13403ad9747SArnaldo Carvalho de Melo 	evlist->threads = NULL;
135361c99a6SArnaldo Carvalho de Melo 	perf_evlist__purge(evlist);
136ef1d1af2SArnaldo Carvalho de Melo 	perf_evlist__exit(evlist);
137361c99a6SArnaldo Carvalho de Melo 	free(evlist);
138361c99a6SArnaldo Carvalho de Melo }
139361c99a6SArnaldo Carvalho de Melo 
140adc0c3e8SAdrian Hunter static void __perf_evlist__propagate_maps(struct perf_evlist *evlist,
141adc0c3e8SAdrian Hunter 					  struct perf_evsel *evsel)
142adc0c3e8SAdrian Hunter {
143adc0c3e8SAdrian Hunter 	/*
144adc0c3e8SAdrian Hunter 	 * We already have cpus for evsel (via PMU sysfs) so
145adc0c3e8SAdrian Hunter 	 * keep it, if there's no target cpu list defined.
146adc0c3e8SAdrian Hunter 	 */
147adc0c3e8SAdrian Hunter 	if (!evsel->own_cpus || evlist->has_user_cpus) {
148adc0c3e8SAdrian Hunter 		cpu_map__put(evsel->cpus);
149adc0c3e8SAdrian Hunter 		evsel->cpus = cpu_map__get(evlist->cpus);
150adc0c3e8SAdrian Hunter 	} else if (evsel->cpus != evsel->own_cpus) {
151adc0c3e8SAdrian Hunter 		cpu_map__put(evsel->cpus);
152adc0c3e8SAdrian Hunter 		evsel->cpus = cpu_map__get(evsel->own_cpus);
153adc0c3e8SAdrian Hunter 	}
154adc0c3e8SAdrian Hunter 
155adc0c3e8SAdrian Hunter 	thread_map__put(evsel->threads);
156adc0c3e8SAdrian Hunter 	evsel->threads = thread_map__get(evlist->threads);
157adc0c3e8SAdrian Hunter }
158adc0c3e8SAdrian Hunter 
159adc0c3e8SAdrian Hunter static void perf_evlist__propagate_maps(struct perf_evlist *evlist)
160adc0c3e8SAdrian Hunter {
161adc0c3e8SAdrian Hunter 	struct perf_evsel *evsel;
162adc0c3e8SAdrian Hunter 
163adc0c3e8SAdrian Hunter 	evlist__for_each(evlist, evsel)
164adc0c3e8SAdrian Hunter 		__perf_evlist__propagate_maps(evlist, evsel);
165adc0c3e8SAdrian Hunter }
166adc0c3e8SAdrian Hunter 
167361c99a6SArnaldo Carvalho de Melo void perf_evlist__add(struct perf_evlist *evlist, struct perf_evsel *entry)
168361c99a6SArnaldo Carvalho de Melo {
169d49e4695SArnaldo Carvalho de Melo 	entry->evlist = evlist;
170361c99a6SArnaldo Carvalho de Melo 	list_add_tail(&entry->node, &evlist->entries);
171ef503831SArnaldo Carvalho de Melo 	entry->idx = evlist->nr_entries;
17260b0896cSAdrian Hunter 	entry->tracking = !entry->idx;
173ef503831SArnaldo Carvalho de Melo 
17475562573SAdrian Hunter 	if (!evlist->nr_entries++)
17575562573SAdrian Hunter 		perf_evlist__set_id_pos(evlist);
17644c42d71SAdrian Hunter 
17744c42d71SAdrian Hunter 	__perf_evlist__propagate_maps(evlist, entry);
178361c99a6SArnaldo Carvalho de Melo }
179361c99a6SArnaldo Carvalho de Melo 
1804768230aSAdrian Hunter void perf_evlist__remove(struct perf_evlist *evlist, struct perf_evsel *evsel)
1814768230aSAdrian Hunter {
1824768230aSAdrian Hunter 	evsel->evlist = NULL;
1834768230aSAdrian Hunter 	list_del_init(&evsel->node);
1844768230aSAdrian Hunter 	evlist->nr_entries -= 1;
1854768230aSAdrian Hunter }
1864768230aSAdrian Hunter 
1870529bc1fSJiri Olsa void perf_evlist__splice_list_tail(struct perf_evlist *evlist,
188f114d6efSAdrian Hunter 				   struct list_head *list)
18950d08e47SArnaldo Carvalho de Melo {
190f114d6efSAdrian Hunter 	struct perf_evsel *evsel, *temp;
19175562573SAdrian Hunter 
192f114d6efSAdrian Hunter 	__evlist__for_each_safe(list, temp, evsel) {
193f114d6efSAdrian Hunter 		list_del_init(&evsel->node);
194f114d6efSAdrian Hunter 		perf_evlist__add(evlist, evsel);
195f114d6efSAdrian Hunter 	}
19650d08e47SArnaldo Carvalho de Melo }
19750d08e47SArnaldo Carvalho de Melo 
19863dab225SArnaldo Carvalho de Melo void __perf_evlist__set_leader(struct list_head *list)
19963dab225SArnaldo Carvalho de Melo {
20063dab225SArnaldo Carvalho de Melo 	struct perf_evsel *evsel, *leader;
20163dab225SArnaldo Carvalho de Melo 
20263dab225SArnaldo Carvalho de Melo 	leader = list_entry(list->next, struct perf_evsel, node);
20397f63e4aSNamhyung Kim 	evsel = list_entry(list->prev, struct perf_evsel, node);
20497f63e4aSNamhyung Kim 
20597f63e4aSNamhyung Kim 	leader->nr_members = evsel->idx - leader->idx + 1;
20663dab225SArnaldo Carvalho de Melo 
2070050f7aaSArnaldo Carvalho de Melo 	__evlist__for_each(list, evsel) {
20863dab225SArnaldo Carvalho de Melo 		evsel->leader = leader;
20963dab225SArnaldo Carvalho de Melo 	}
21063dab225SArnaldo Carvalho de Melo }
21163dab225SArnaldo Carvalho de Melo 
21263dab225SArnaldo Carvalho de Melo void perf_evlist__set_leader(struct perf_evlist *evlist)
2136a4bb04cSJiri Olsa {
21497f63e4aSNamhyung Kim 	if (evlist->nr_entries) {
21597f63e4aSNamhyung Kim 		evlist->nr_groups = evlist->nr_entries > 1 ? 1 : 0;
21663dab225SArnaldo Carvalho de Melo 		__perf_evlist__set_leader(&evlist->entries);
2176a4bb04cSJiri Olsa 	}
21897f63e4aSNamhyung Kim }
2196a4bb04cSJiri Olsa 
22045cf6c33SJiri Olsa void perf_event_attr__set_max_precise_ip(struct perf_event_attr *attr)
2217f8d1adeSArnaldo Carvalho de Melo {
2227f8d1adeSArnaldo Carvalho de Melo 	attr->precise_ip = 3;
2237f8d1adeSArnaldo Carvalho de Melo 
2247f8d1adeSArnaldo Carvalho de Melo 	while (attr->precise_ip != 0) {
2257f8d1adeSArnaldo Carvalho de Melo 		int fd = sys_perf_event_open(attr, 0, -1, -1, 0);
2267f8d1adeSArnaldo Carvalho de Melo 		if (fd != -1) {
2277f8d1adeSArnaldo Carvalho de Melo 			close(fd);
2287f8d1adeSArnaldo Carvalho de Melo 			break;
2297f8d1adeSArnaldo Carvalho de Melo 		}
2307f8d1adeSArnaldo Carvalho de Melo 		--attr->precise_ip;
2317f8d1adeSArnaldo Carvalho de Melo 	}
2327f8d1adeSArnaldo Carvalho de Melo }
2337f8d1adeSArnaldo Carvalho de Melo 
234361c99a6SArnaldo Carvalho de Melo int perf_evlist__add_default(struct perf_evlist *evlist)
235361c99a6SArnaldo Carvalho de Melo {
236361c99a6SArnaldo Carvalho de Melo 	struct perf_event_attr attr = {
237361c99a6SArnaldo Carvalho de Melo 		.type = PERF_TYPE_HARDWARE,
238361c99a6SArnaldo Carvalho de Melo 		.config = PERF_COUNT_HW_CPU_CYCLES,
239361c99a6SArnaldo Carvalho de Melo 	};
2401aed2671SJoerg Roedel 	struct perf_evsel *evsel;
241361c99a6SArnaldo Carvalho de Melo 
2421aed2671SJoerg Roedel 	event_attr_init(&attr);
2431aed2671SJoerg Roedel 
2447f8d1adeSArnaldo Carvalho de Melo 	perf_event_attr__set_max_precise_ip(&attr);
2457f8d1adeSArnaldo Carvalho de Melo 
246ef503831SArnaldo Carvalho de Melo 	evsel = perf_evsel__new(&attr);
247361c99a6SArnaldo Carvalho de Melo 	if (evsel == NULL)
248cc2d86b0SStephane Eranian 		goto error;
249cc2d86b0SStephane Eranian 
2507f8d1adeSArnaldo Carvalho de Melo 	/* use asprintf() because free(evsel) assumes name is allocated */
2517f8d1adeSArnaldo Carvalho de Melo 	if (asprintf(&evsel->name, "cycles%.*s",
2527f8d1adeSArnaldo Carvalho de Melo 		     attr.precise_ip ? attr.precise_ip + 1 : 0, ":ppp") < 0)
253cc2d86b0SStephane Eranian 		goto error_free;
254361c99a6SArnaldo Carvalho de Melo 
255361c99a6SArnaldo Carvalho de Melo 	perf_evlist__add(evlist, evsel);
256361c99a6SArnaldo Carvalho de Melo 	return 0;
257cc2d86b0SStephane Eranian error_free:
258cc2d86b0SStephane Eranian 	perf_evsel__delete(evsel);
259cc2d86b0SStephane Eranian error:
260cc2d86b0SStephane Eranian 	return -ENOMEM;
261361c99a6SArnaldo Carvalho de Melo }
2625c581041SArnaldo Carvalho de Melo 
2635bae0250SArnaldo Carvalho de Melo int perf_evlist__add_dummy(struct perf_evlist *evlist)
2645bae0250SArnaldo Carvalho de Melo {
2655bae0250SArnaldo Carvalho de Melo 	struct perf_event_attr attr = {
2665bae0250SArnaldo Carvalho de Melo 		.type	= PERF_TYPE_SOFTWARE,
2675bae0250SArnaldo Carvalho de Melo 		.config = PERF_COUNT_SW_DUMMY,
2685bae0250SArnaldo Carvalho de Melo 		.size	= sizeof(attr), /* to capture ABI version */
2695bae0250SArnaldo Carvalho de Melo 	};
2705bae0250SArnaldo Carvalho de Melo 	struct perf_evsel *evsel = perf_evsel__new(&attr);
2715bae0250SArnaldo Carvalho de Melo 
2725bae0250SArnaldo Carvalho de Melo 	if (evsel == NULL)
2735bae0250SArnaldo Carvalho de Melo 		return -ENOMEM;
2745bae0250SArnaldo Carvalho de Melo 
2755bae0250SArnaldo Carvalho de Melo 	perf_evlist__add(evlist, evsel);
2765bae0250SArnaldo Carvalho de Melo 	return 0;
2775bae0250SArnaldo Carvalho de Melo }
2785bae0250SArnaldo Carvalho de Melo 
279e60fc847SArnaldo Carvalho de Melo static int perf_evlist__add_attrs(struct perf_evlist *evlist,
28050d08e47SArnaldo Carvalho de Melo 				  struct perf_event_attr *attrs, size_t nr_attrs)
28150d08e47SArnaldo Carvalho de Melo {
28250d08e47SArnaldo Carvalho de Melo 	struct perf_evsel *evsel, *n;
28350d08e47SArnaldo Carvalho de Melo 	LIST_HEAD(head);
28450d08e47SArnaldo Carvalho de Melo 	size_t i;
28550d08e47SArnaldo Carvalho de Melo 
28650d08e47SArnaldo Carvalho de Melo 	for (i = 0; i < nr_attrs; i++) {
287ef503831SArnaldo Carvalho de Melo 		evsel = perf_evsel__new_idx(attrs + i, evlist->nr_entries + i);
28850d08e47SArnaldo Carvalho de Melo 		if (evsel == NULL)
28950d08e47SArnaldo Carvalho de Melo 			goto out_delete_partial_list;
29050d08e47SArnaldo Carvalho de Melo 		list_add_tail(&evsel->node, &head);
29150d08e47SArnaldo Carvalho de Melo 	}
29250d08e47SArnaldo Carvalho de Melo 
293f114d6efSAdrian Hunter 	perf_evlist__splice_list_tail(evlist, &head);
29450d08e47SArnaldo Carvalho de Melo 
29550d08e47SArnaldo Carvalho de Melo 	return 0;
29650d08e47SArnaldo Carvalho de Melo 
29750d08e47SArnaldo Carvalho de Melo out_delete_partial_list:
2980050f7aaSArnaldo Carvalho de Melo 	__evlist__for_each_safe(&head, n, evsel)
29950d08e47SArnaldo Carvalho de Melo 		perf_evsel__delete(evsel);
30050d08e47SArnaldo Carvalho de Melo 	return -1;
30150d08e47SArnaldo Carvalho de Melo }
30250d08e47SArnaldo Carvalho de Melo 
30379695e1bSArnaldo Carvalho de Melo int __perf_evlist__add_default_attrs(struct perf_evlist *evlist,
30479695e1bSArnaldo Carvalho de Melo 				     struct perf_event_attr *attrs, size_t nr_attrs)
30579695e1bSArnaldo Carvalho de Melo {
30679695e1bSArnaldo Carvalho de Melo 	size_t i;
30779695e1bSArnaldo Carvalho de Melo 
30879695e1bSArnaldo Carvalho de Melo 	for (i = 0; i < nr_attrs; i++)
30979695e1bSArnaldo Carvalho de Melo 		event_attr_init(attrs + i);
31079695e1bSArnaldo Carvalho de Melo 
31179695e1bSArnaldo Carvalho de Melo 	return perf_evlist__add_attrs(evlist, attrs, nr_attrs);
31279695e1bSArnaldo Carvalho de Melo }
31379695e1bSArnaldo Carvalho de Melo 
314da378962SArnaldo Carvalho de Melo struct perf_evsel *
315ee29be62SArnaldo Carvalho de Melo perf_evlist__find_tracepoint_by_id(struct perf_evlist *evlist, int id)
316ee29be62SArnaldo Carvalho de Melo {
317ee29be62SArnaldo Carvalho de Melo 	struct perf_evsel *evsel;
318ee29be62SArnaldo Carvalho de Melo 
3190050f7aaSArnaldo Carvalho de Melo 	evlist__for_each(evlist, evsel) {
320ee29be62SArnaldo Carvalho de Melo 		if (evsel->attr.type   == PERF_TYPE_TRACEPOINT &&
321ee29be62SArnaldo Carvalho de Melo 		    (int)evsel->attr.config == id)
322ee29be62SArnaldo Carvalho de Melo 			return evsel;
323ee29be62SArnaldo Carvalho de Melo 	}
324ee29be62SArnaldo Carvalho de Melo 
325ee29be62SArnaldo Carvalho de Melo 	return NULL;
326ee29be62SArnaldo Carvalho de Melo }
327ee29be62SArnaldo Carvalho de Melo 
328a2f2804aSDavid Ahern struct perf_evsel *
329a2f2804aSDavid Ahern perf_evlist__find_tracepoint_by_name(struct perf_evlist *evlist,
330a2f2804aSDavid Ahern 				     const char *name)
331a2f2804aSDavid Ahern {
332a2f2804aSDavid Ahern 	struct perf_evsel *evsel;
333a2f2804aSDavid Ahern 
3340050f7aaSArnaldo Carvalho de Melo 	evlist__for_each(evlist, evsel) {
335a2f2804aSDavid Ahern 		if ((evsel->attr.type == PERF_TYPE_TRACEPOINT) &&
336a2f2804aSDavid Ahern 		    (strcmp(evsel->name, name) == 0))
337a2f2804aSDavid Ahern 			return evsel;
338a2f2804aSDavid Ahern 	}
339a2f2804aSDavid Ahern 
340a2f2804aSDavid Ahern 	return NULL;
341a2f2804aSDavid Ahern }
342a2f2804aSDavid Ahern 
34339876e7dSArnaldo Carvalho de Melo int perf_evlist__add_newtp(struct perf_evlist *evlist,
34439876e7dSArnaldo Carvalho de Melo 			   const char *sys, const char *name, void *handler)
34539876e7dSArnaldo Carvalho de Melo {
346ef503831SArnaldo Carvalho de Melo 	struct perf_evsel *evsel = perf_evsel__newtp(sys, name);
34739876e7dSArnaldo Carvalho de Melo 
3488dd2a131SJiri Olsa 	if (IS_ERR(evsel))
34939876e7dSArnaldo Carvalho de Melo 		return -1;
35039876e7dSArnaldo Carvalho de Melo 
351744a9719SArnaldo Carvalho de Melo 	evsel->handler = handler;
35239876e7dSArnaldo Carvalho de Melo 	perf_evlist__add(evlist, evsel);
35339876e7dSArnaldo Carvalho de Melo 	return 0;
35439876e7dSArnaldo Carvalho de Melo }
35539876e7dSArnaldo Carvalho de Melo 
356bf8e8f4bSAdrian Hunter static int perf_evlist__nr_threads(struct perf_evlist *evlist,
357bf8e8f4bSAdrian Hunter 				   struct perf_evsel *evsel)
358bf8e8f4bSAdrian Hunter {
359bf8e8f4bSAdrian Hunter 	if (evsel->system_wide)
360bf8e8f4bSAdrian Hunter 		return 1;
361bf8e8f4bSAdrian Hunter 	else
362bf8e8f4bSAdrian Hunter 		return thread_map__nr(evlist->threads);
363bf8e8f4bSAdrian Hunter }
364bf8e8f4bSAdrian Hunter 
3654152ab37SArnaldo Carvalho de Melo void perf_evlist__disable(struct perf_evlist *evlist)
3664152ab37SArnaldo Carvalho de Melo {
3674152ab37SArnaldo Carvalho de Melo 	struct perf_evsel *pos;
3684152ab37SArnaldo Carvalho de Melo 
3690050f7aaSArnaldo Carvalho de Melo 	evlist__for_each(evlist, pos) {
370395c3070SAdrian Hunter 		if (!perf_evsel__is_group_leader(pos) || !pos->fd)
3713fe4430dSJiri Olsa 			continue;
3723e27c920SJiri Olsa 		perf_evsel__disable(pos);
3734152ab37SArnaldo Carvalho de Melo 	}
3742b56bcfbSArnaldo Carvalho de Melo 
3752b56bcfbSArnaldo Carvalho de Melo 	evlist->enabled = false;
3764152ab37SArnaldo Carvalho de Melo }
3774152ab37SArnaldo Carvalho de Melo 
378764e16a3SDavid Ahern void perf_evlist__enable(struct perf_evlist *evlist)
379764e16a3SDavid Ahern {
380764e16a3SDavid Ahern 	struct perf_evsel *pos;
381764e16a3SDavid Ahern 
3820050f7aaSArnaldo Carvalho de Melo 	evlist__for_each(evlist, pos) {
383395c3070SAdrian Hunter 		if (!perf_evsel__is_group_leader(pos) || !pos->fd)
3843fe4430dSJiri Olsa 			continue;
3853e27c920SJiri Olsa 		perf_evsel__enable(pos);
386764e16a3SDavid Ahern 	}
3872b56bcfbSArnaldo Carvalho de Melo 
3882b56bcfbSArnaldo Carvalho de Melo 	evlist->enabled = true;
3892b56bcfbSArnaldo Carvalho de Melo }
3902b56bcfbSArnaldo Carvalho de Melo 
3912b56bcfbSArnaldo Carvalho de Melo void perf_evlist__toggle_enable(struct perf_evlist *evlist)
3922b56bcfbSArnaldo Carvalho de Melo {
3932b56bcfbSArnaldo Carvalho de Melo 	(evlist->enabled ? perf_evlist__disable : perf_evlist__enable)(evlist);
394764e16a3SDavid Ahern }
395764e16a3SDavid Ahern 
3961c65056cSAdrian Hunter static int perf_evlist__enable_event_cpu(struct perf_evlist *evlist,
3971c65056cSAdrian Hunter 					 struct perf_evsel *evsel, int cpu)
3981c65056cSAdrian Hunter {
3991c65056cSAdrian Hunter 	int thread, err;
4001c65056cSAdrian Hunter 	int nr_threads = perf_evlist__nr_threads(evlist, evsel);
4011c65056cSAdrian Hunter 
4021c65056cSAdrian Hunter 	if (!evsel->fd)
4031c65056cSAdrian Hunter 		return -EINVAL;
4041c65056cSAdrian Hunter 
4051c65056cSAdrian Hunter 	for (thread = 0; thread < nr_threads; thread++) {
4061c65056cSAdrian Hunter 		err = ioctl(FD(evsel, cpu, thread),
4071c65056cSAdrian Hunter 			    PERF_EVENT_IOC_ENABLE, 0);
4081c65056cSAdrian Hunter 		if (err)
4091c65056cSAdrian Hunter 			return err;
4101c65056cSAdrian Hunter 	}
4111c65056cSAdrian Hunter 	return 0;
4121c65056cSAdrian Hunter }
4131c65056cSAdrian Hunter 
4141c65056cSAdrian Hunter static int perf_evlist__enable_event_thread(struct perf_evlist *evlist,
4151c65056cSAdrian Hunter 					    struct perf_evsel *evsel,
4161c65056cSAdrian Hunter 					    int thread)
4171c65056cSAdrian Hunter {
4181c65056cSAdrian Hunter 	int cpu, err;
4191c65056cSAdrian Hunter 	int nr_cpus = cpu_map__nr(evlist->cpus);
4201c65056cSAdrian Hunter 
4211c65056cSAdrian Hunter 	if (!evsel->fd)
4221c65056cSAdrian Hunter 		return -EINVAL;
4231c65056cSAdrian Hunter 
4241c65056cSAdrian Hunter 	for (cpu = 0; cpu < nr_cpus; cpu++) {
4251c65056cSAdrian Hunter 		err = ioctl(FD(evsel, cpu, thread), PERF_EVENT_IOC_ENABLE, 0);
4261c65056cSAdrian Hunter 		if (err)
4271c65056cSAdrian Hunter 			return err;
4281c65056cSAdrian Hunter 	}
4291c65056cSAdrian Hunter 	return 0;
4301c65056cSAdrian Hunter }
4311c65056cSAdrian Hunter 
4321c65056cSAdrian Hunter int perf_evlist__enable_event_idx(struct perf_evlist *evlist,
4331c65056cSAdrian Hunter 				  struct perf_evsel *evsel, int idx)
4341c65056cSAdrian Hunter {
4351c65056cSAdrian Hunter 	bool per_cpu_mmaps = !cpu_map__empty(evlist->cpus);
4361c65056cSAdrian Hunter 
4371c65056cSAdrian Hunter 	if (per_cpu_mmaps)
4381c65056cSAdrian Hunter 		return perf_evlist__enable_event_cpu(evlist, evsel, idx);
4391c65056cSAdrian Hunter 	else
4401c65056cSAdrian Hunter 		return perf_evlist__enable_event_thread(evlist, evsel, idx);
4411c65056cSAdrian Hunter }
4421c65056cSAdrian Hunter 
443ad6765ddSArnaldo Carvalho de Melo int perf_evlist__alloc_pollfd(struct perf_evlist *evlist)
4445c581041SArnaldo Carvalho de Melo {
445b3a319d5SNamhyung Kim 	int nr_cpus = cpu_map__nr(evlist->cpus);
446b3a319d5SNamhyung Kim 	int nr_threads = thread_map__nr(evlist->threads);
447bf8e8f4bSAdrian Hunter 	int nfds = 0;
448bf8e8f4bSAdrian Hunter 	struct perf_evsel *evsel;
449bf8e8f4bSAdrian Hunter 
450cba9b847SArnaldo Carvalho de Melo 	evlist__for_each(evlist, evsel) {
451bf8e8f4bSAdrian Hunter 		if (evsel->system_wide)
452bf8e8f4bSAdrian Hunter 			nfds += nr_cpus;
453bf8e8f4bSAdrian Hunter 		else
454bf8e8f4bSAdrian Hunter 			nfds += nr_cpus * nr_threads;
455bf8e8f4bSAdrian Hunter 	}
456bf8e8f4bSAdrian Hunter 
4571b85337dSArnaldo Carvalho de Melo 	if (fdarray__available_entries(&evlist->pollfd) < nfds &&
4581b85337dSArnaldo Carvalho de Melo 	    fdarray__grow(&evlist->pollfd, nfds) < 0)
459ad6765ddSArnaldo Carvalho de Melo 		return -ENOMEM;
460ad6765ddSArnaldo Carvalho de Melo 
461ad6765ddSArnaldo Carvalho de Melo 	return 0;
4625c581041SArnaldo Carvalho de Melo }
46370082dd9SArnaldo Carvalho de Melo 
464e4b356b5SArnaldo Carvalho de Melo static int __perf_evlist__add_pollfd(struct perf_evlist *evlist, int fd, int idx)
465e4b356b5SArnaldo Carvalho de Melo {
466e4b356b5SArnaldo Carvalho de Melo 	int pos = fdarray__add(&evlist->pollfd, fd, POLLIN | POLLERR | POLLHUP);
467e4b356b5SArnaldo Carvalho de Melo 	/*
468e4b356b5SArnaldo Carvalho de Melo 	 * Save the idx so that when we filter out fds POLLHUP'ed we can
469e4b356b5SArnaldo Carvalho de Melo 	 * close the associated evlist->mmap[] entry.
470e4b356b5SArnaldo Carvalho de Melo 	 */
471e4b356b5SArnaldo Carvalho de Melo 	if (pos >= 0) {
472e4b356b5SArnaldo Carvalho de Melo 		evlist->pollfd.priv[pos].idx = idx;
473e4b356b5SArnaldo Carvalho de Melo 
474e4b356b5SArnaldo Carvalho de Melo 		fcntl(fd, F_SETFL, O_NONBLOCK);
475e4b356b5SArnaldo Carvalho de Melo 	}
476e4b356b5SArnaldo Carvalho de Melo 
477e4b356b5SArnaldo Carvalho de Melo 	return pos;
478e4b356b5SArnaldo Carvalho de Melo }
479e4b356b5SArnaldo Carvalho de Melo 
480ad6765ddSArnaldo Carvalho de Melo int perf_evlist__add_pollfd(struct perf_evlist *evlist, int fd)
48170082dd9SArnaldo Carvalho de Melo {
482e4b356b5SArnaldo Carvalho de Melo 	return __perf_evlist__add_pollfd(evlist, fd, -1);
483e4b356b5SArnaldo Carvalho de Melo }
4841b85337dSArnaldo Carvalho de Melo 
485e4b356b5SArnaldo Carvalho de Melo static void perf_evlist__munmap_filtered(struct fdarray *fda, int fd)
486e4b356b5SArnaldo Carvalho de Melo {
487e4b356b5SArnaldo Carvalho de Melo 	struct perf_evlist *evlist = container_of(fda, struct perf_evlist, pollfd);
488e4b356b5SArnaldo Carvalho de Melo 
489e4b356b5SArnaldo Carvalho de Melo 	perf_evlist__mmap_put(evlist, fda->priv[fd].idx);
49070082dd9SArnaldo Carvalho de Melo }
49170db7533SArnaldo Carvalho de Melo 
4921ddec7f0SArnaldo Carvalho de Melo int perf_evlist__filter_pollfd(struct perf_evlist *evlist, short revents_and_mask)
4931ddec7f0SArnaldo Carvalho de Melo {
494e4b356b5SArnaldo Carvalho de Melo 	return fdarray__filter(&evlist->pollfd, revents_and_mask,
495e4b356b5SArnaldo Carvalho de Melo 			       perf_evlist__munmap_filtered);
4961ddec7f0SArnaldo Carvalho de Melo }
4971ddec7f0SArnaldo Carvalho de Melo 
498f66a889dSArnaldo Carvalho de Melo int perf_evlist__poll(struct perf_evlist *evlist, int timeout)
499f66a889dSArnaldo Carvalho de Melo {
5001b85337dSArnaldo Carvalho de Melo 	return fdarray__poll(&evlist->pollfd, timeout);
501f66a889dSArnaldo Carvalho de Melo }
502f66a889dSArnaldo Carvalho de Melo 
503a91e5431SArnaldo Carvalho de Melo static void perf_evlist__id_hash(struct perf_evlist *evlist,
504a91e5431SArnaldo Carvalho de Melo 				 struct perf_evsel *evsel,
5053d3b5e95SArnaldo Carvalho de Melo 				 int cpu, int thread, u64 id)
5063d3b5e95SArnaldo Carvalho de Melo {
5073d3b5e95SArnaldo Carvalho de Melo 	int hash;
5083d3b5e95SArnaldo Carvalho de Melo 	struct perf_sample_id *sid = SID(evsel, cpu, thread);
5093d3b5e95SArnaldo Carvalho de Melo 
5103d3b5e95SArnaldo Carvalho de Melo 	sid->id = id;
5113d3b5e95SArnaldo Carvalho de Melo 	sid->evsel = evsel;
5123d3b5e95SArnaldo Carvalho de Melo 	hash = hash_64(sid->id, PERF_EVLIST__HLIST_BITS);
5133d3b5e95SArnaldo Carvalho de Melo 	hlist_add_head(&sid->node, &evlist->heads[hash]);
5143d3b5e95SArnaldo Carvalho de Melo }
5153d3b5e95SArnaldo Carvalho de Melo 
516a91e5431SArnaldo Carvalho de Melo void perf_evlist__id_add(struct perf_evlist *evlist, struct perf_evsel *evsel,
517a91e5431SArnaldo Carvalho de Melo 			 int cpu, int thread, u64 id)
518a91e5431SArnaldo Carvalho de Melo {
519a91e5431SArnaldo Carvalho de Melo 	perf_evlist__id_hash(evlist, evsel, cpu, thread, id);
520a91e5431SArnaldo Carvalho de Melo 	evsel->id[evsel->ids++] = id;
521a91e5431SArnaldo Carvalho de Melo }
522a91e5431SArnaldo Carvalho de Melo 
5231c59612dSJiri Olsa int perf_evlist__id_add_fd(struct perf_evlist *evlist,
5243d3b5e95SArnaldo Carvalho de Melo 			   struct perf_evsel *evsel,
525f8a95309SArnaldo Carvalho de Melo 			   int cpu, int thread, int fd)
526f8a95309SArnaldo Carvalho de Melo {
527f8a95309SArnaldo Carvalho de Melo 	u64 read_data[4] = { 0, };
5283d3b5e95SArnaldo Carvalho de Melo 	int id_idx = 1; /* The first entry is the counter value */
529e2b5abe0SJiri Olsa 	u64 id;
530e2b5abe0SJiri Olsa 	int ret;
531e2b5abe0SJiri Olsa 
532e2b5abe0SJiri Olsa 	ret = ioctl(fd, PERF_EVENT_IOC_ID, &id);
533e2b5abe0SJiri Olsa 	if (!ret)
534e2b5abe0SJiri Olsa 		goto add;
535e2b5abe0SJiri Olsa 
536e2b5abe0SJiri Olsa 	if (errno != ENOTTY)
537e2b5abe0SJiri Olsa 		return -1;
538e2b5abe0SJiri Olsa 
539e2b5abe0SJiri Olsa 	/* Legacy way to get event id.. All hail to old kernels! */
540f8a95309SArnaldo Carvalho de Melo 
541c4861afeSJiri Olsa 	/*
542c4861afeSJiri Olsa 	 * This way does not work with group format read, so bail
543c4861afeSJiri Olsa 	 * out in that case.
544c4861afeSJiri Olsa 	 */
545c4861afeSJiri Olsa 	if (perf_evlist__read_format(evlist) & PERF_FORMAT_GROUP)
546c4861afeSJiri Olsa 		return -1;
547c4861afeSJiri Olsa 
548f8a95309SArnaldo Carvalho de Melo 	if (!(evsel->attr.read_format & PERF_FORMAT_ID) ||
549f8a95309SArnaldo Carvalho de Melo 	    read(fd, &read_data, sizeof(read_data)) == -1)
550f8a95309SArnaldo Carvalho de Melo 		return -1;
551f8a95309SArnaldo Carvalho de Melo 
552f8a95309SArnaldo Carvalho de Melo 	if (evsel->attr.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
553f8a95309SArnaldo Carvalho de Melo 		++id_idx;
554f8a95309SArnaldo Carvalho de Melo 	if (evsel->attr.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
555f8a95309SArnaldo Carvalho de Melo 		++id_idx;
556f8a95309SArnaldo Carvalho de Melo 
557e2b5abe0SJiri Olsa 	id = read_data[id_idx];
558e2b5abe0SJiri Olsa 
559e2b5abe0SJiri Olsa  add:
560e2b5abe0SJiri Olsa 	perf_evlist__id_add(evlist, evsel, cpu, thread, id);
561f8a95309SArnaldo Carvalho de Melo 	return 0;
562f8a95309SArnaldo Carvalho de Melo }
563f8a95309SArnaldo Carvalho de Melo 
5643c659eedSAdrian Hunter static void perf_evlist__set_sid_idx(struct perf_evlist *evlist,
5653c659eedSAdrian Hunter 				     struct perf_evsel *evsel, int idx, int cpu,
5663c659eedSAdrian Hunter 				     int thread)
5673c659eedSAdrian Hunter {
5683c659eedSAdrian Hunter 	struct perf_sample_id *sid = SID(evsel, cpu, thread);
5693c659eedSAdrian Hunter 	sid->idx = idx;
5703c659eedSAdrian Hunter 	if (evlist->cpus && cpu >= 0)
5713c659eedSAdrian Hunter 		sid->cpu = evlist->cpus->map[cpu];
5723c659eedSAdrian Hunter 	else
5733c659eedSAdrian Hunter 		sid->cpu = -1;
5743c659eedSAdrian Hunter 	if (!evsel->system_wide && evlist->threads && thread >= 0)
575e13798c7SJiri Olsa 		sid->tid = thread_map__pid(evlist->threads, thread);
5763c659eedSAdrian Hunter 	else
5773c659eedSAdrian Hunter 		sid->tid = -1;
5783c659eedSAdrian Hunter }
5793c659eedSAdrian Hunter 
580932a3594SJiri Olsa struct perf_sample_id *perf_evlist__id2sid(struct perf_evlist *evlist, u64 id)
58170db7533SArnaldo Carvalho de Melo {
58270db7533SArnaldo Carvalho de Melo 	struct hlist_head *head;
58370db7533SArnaldo Carvalho de Melo 	struct perf_sample_id *sid;
58470db7533SArnaldo Carvalho de Melo 	int hash;
58570db7533SArnaldo Carvalho de Melo 
58670db7533SArnaldo Carvalho de Melo 	hash = hash_64(id, PERF_EVLIST__HLIST_BITS);
58770db7533SArnaldo Carvalho de Melo 	head = &evlist->heads[hash];
58870db7533SArnaldo Carvalho de Melo 
589b67bfe0dSSasha Levin 	hlist_for_each_entry(sid, head, node)
59070db7533SArnaldo Carvalho de Melo 		if (sid->id == id)
591932a3594SJiri Olsa 			return sid;
592932a3594SJiri Olsa 
593932a3594SJiri Olsa 	return NULL;
594932a3594SJiri Olsa }
595932a3594SJiri Olsa 
596932a3594SJiri Olsa struct perf_evsel *perf_evlist__id2evsel(struct perf_evlist *evlist, u64 id)
597932a3594SJiri Olsa {
598932a3594SJiri Olsa 	struct perf_sample_id *sid;
599932a3594SJiri Olsa 
60005169df5SAdrian Hunter 	if (evlist->nr_entries == 1 || !id)
601932a3594SJiri Olsa 		return perf_evlist__first(evlist);
602932a3594SJiri Olsa 
603932a3594SJiri Olsa 	sid = perf_evlist__id2sid(evlist, id);
604932a3594SJiri Olsa 	if (sid)
60570db7533SArnaldo Carvalho de Melo 		return sid->evsel;
60630e68bccSNamhyung Kim 
60730e68bccSNamhyung Kim 	if (!perf_evlist__sample_id_all(evlist))
6080c21f736SArnaldo Carvalho de Melo 		return perf_evlist__first(evlist);
60930e68bccSNamhyung Kim 
61070db7533SArnaldo Carvalho de Melo 	return NULL;
61170db7533SArnaldo Carvalho de Melo }
61204391debSArnaldo Carvalho de Melo 
613dddcf6abSAdrian Hunter struct perf_evsel *perf_evlist__id2evsel_strict(struct perf_evlist *evlist,
614dddcf6abSAdrian Hunter 						u64 id)
615dddcf6abSAdrian Hunter {
616dddcf6abSAdrian Hunter 	struct perf_sample_id *sid;
617dddcf6abSAdrian Hunter 
618dddcf6abSAdrian Hunter 	if (!id)
619dddcf6abSAdrian Hunter 		return NULL;
620dddcf6abSAdrian Hunter 
621dddcf6abSAdrian Hunter 	sid = perf_evlist__id2sid(evlist, id);
622dddcf6abSAdrian Hunter 	if (sid)
623dddcf6abSAdrian Hunter 		return sid->evsel;
624dddcf6abSAdrian Hunter 
625dddcf6abSAdrian Hunter 	return NULL;
626dddcf6abSAdrian Hunter }
627dddcf6abSAdrian Hunter 
62875562573SAdrian Hunter static int perf_evlist__event2id(struct perf_evlist *evlist,
62975562573SAdrian Hunter 				 union perf_event *event, u64 *id)
63075562573SAdrian Hunter {
63175562573SAdrian Hunter 	const u64 *array = event->sample.array;
63275562573SAdrian Hunter 	ssize_t n;
63375562573SAdrian Hunter 
63475562573SAdrian Hunter 	n = (event->header.size - sizeof(event->header)) >> 3;
63575562573SAdrian Hunter 
63675562573SAdrian Hunter 	if (event->header.type == PERF_RECORD_SAMPLE) {
63775562573SAdrian Hunter 		if (evlist->id_pos >= n)
63875562573SAdrian Hunter 			return -1;
63975562573SAdrian Hunter 		*id = array[evlist->id_pos];
64075562573SAdrian Hunter 	} else {
64175562573SAdrian Hunter 		if (evlist->is_pos > n)
64275562573SAdrian Hunter 			return -1;
64375562573SAdrian Hunter 		n -= evlist->is_pos;
64475562573SAdrian Hunter 		*id = array[n];
64575562573SAdrian Hunter 	}
64675562573SAdrian Hunter 	return 0;
64775562573SAdrian Hunter }
64875562573SAdrian Hunter 
64975562573SAdrian Hunter static struct perf_evsel *perf_evlist__event2evsel(struct perf_evlist *evlist,
65075562573SAdrian Hunter 						   union perf_event *event)
65175562573SAdrian Hunter {
65298be6966SAdrian Hunter 	struct perf_evsel *first = perf_evlist__first(evlist);
65375562573SAdrian Hunter 	struct hlist_head *head;
65475562573SAdrian Hunter 	struct perf_sample_id *sid;
65575562573SAdrian Hunter 	int hash;
65675562573SAdrian Hunter 	u64 id;
65775562573SAdrian Hunter 
65875562573SAdrian Hunter 	if (evlist->nr_entries == 1)
65998be6966SAdrian Hunter 		return first;
66098be6966SAdrian Hunter 
66198be6966SAdrian Hunter 	if (!first->attr.sample_id_all &&
66298be6966SAdrian Hunter 	    event->header.type != PERF_RECORD_SAMPLE)
66398be6966SAdrian Hunter 		return first;
66475562573SAdrian Hunter 
66575562573SAdrian Hunter 	if (perf_evlist__event2id(evlist, event, &id))
66675562573SAdrian Hunter 		return NULL;
66775562573SAdrian Hunter 
66875562573SAdrian Hunter 	/* Synthesized events have an id of zero */
66975562573SAdrian Hunter 	if (!id)
67098be6966SAdrian Hunter 		return first;
67175562573SAdrian Hunter 
67275562573SAdrian Hunter 	hash = hash_64(id, PERF_EVLIST__HLIST_BITS);
67375562573SAdrian Hunter 	head = &evlist->heads[hash];
67475562573SAdrian Hunter 
67575562573SAdrian Hunter 	hlist_for_each_entry(sid, head, node) {
67675562573SAdrian Hunter 		if (sid->id == id)
67775562573SAdrian Hunter 			return sid->evsel;
67875562573SAdrian Hunter 	}
67975562573SAdrian Hunter 	return NULL;
68075562573SAdrian Hunter }
68175562573SAdrian Hunter 
682aece948fSArnaldo Carvalho de Melo union perf_event *perf_evlist__mmap_read(struct perf_evlist *evlist, int idx)
68304391debSArnaldo Carvalho de Melo {
684aece948fSArnaldo Carvalho de Melo 	struct perf_mmap *md = &evlist->mmap[idx];
6857951722dSSukadev Bhattiprolu 	u64 head;
6867b8283b5SDavid Ahern 	u64 old = md->prev;
68704391debSArnaldo Carvalho de Melo 	unsigned char *data = md->base + page_size;
6888115d60cSArnaldo Carvalho de Melo 	union perf_event *event = NULL;
68904391debSArnaldo Carvalho de Melo 
6907951722dSSukadev Bhattiprolu 	/*
6917951722dSSukadev Bhattiprolu 	 * Check if event was unmapped due to a POLLHUP/POLLERR.
6927951722dSSukadev Bhattiprolu 	 */
6937951722dSSukadev Bhattiprolu 	if (!atomic_read(&md->refcnt))
6947951722dSSukadev Bhattiprolu 		return NULL;
6957951722dSSukadev Bhattiprolu 
6967951722dSSukadev Bhattiprolu 	head = perf_mmap__read_head(md);
6977bb41152SArnaldo Carvalho de Melo 	if (evlist->overwrite) {
69804391debSArnaldo Carvalho de Melo 		/*
69904391debSArnaldo Carvalho de Melo 		 * If we're further behind than half the buffer, there's a chance
70004391debSArnaldo Carvalho de Melo 		 * the writer will bite our tail and mess up the samples under us.
70104391debSArnaldo Carvalho de Melo 		 *
70204391debSArnaldo Carvalho de Melo 		 * If we somehow ended up ahead of the head, we got messed up.
70304391debSArnaldo Carvalho de Melo 		 *
70404391debSArnaldo Carvalho de Melo 		 * In either case, truncate and restart at head.
70504391debSArnaldo Carvalho de Melo 		 */
7067bb41152SArnaldo Carvalho de Melo 		int diff = head - old;
70704391debSArnaldo Carvalho de Melo 		if (diff > md->mask / 2 || diff < 0) {
70804391debSArnaldo Carvalho de Melo 			fprintf(stderr, "WARNING: failed to keep up with mmap data.\n");
70904391debSArnaldo Carvalho de Melo 
71004391debSArnaldo Carvalho de Melo 			/*
71104391debSArnaldo Carvalho de Melo 			 * head points to a known good entry, start there.
71204391debSArnaldo Carvalho de Melo 			 */
71304391debSArnaldo Carvalho de Melo 			old = head;
71404391debSArnaldo Carvalho de Melo 		}
7157bb41152SArnaldo Carvalho de Melo 	}
71604391debSArnaldo Carvalho de Melo 
71704391debSArnaldo Carvalho de Melo 	if (old != head) {
71804391debSArnaldo Carvalho de Melo 		size_t size;
71904391debSArnaldo Carvalho de Melo 
7208115d60cSArnaldo Carvalho de Melo 		event = (union perf_event *)&data[old & md->mask];
72104391debSArnaldo Carvalho de Melo 		size = event->header.size;
72204391debSArnaldo Carvalho de Melo 
72304391debSArnaldo Carvalho de Melo 		/*
72404391debSArnaldo Carvalho de Melo 		 * Event straddles the mmap boundary -- header should always
72504391debSArnaldo Carvalho de Melo 		 * be inside due to u64 alignment of output.
72604391debSArnaldo Carvalho de Melo 		 */
72704391debSArnaldo Carvalho de Melo 		if ((old & md->mask) + size != ((old + size) & md->mask)) {
72804391debSArnaldo Carvalho de Melo 			unsigned int offset = old;
72904391debSArnaldo Carvalho de Melo 			unsigned int len = min(sizeof(*event), size), cpy;
730a65cb4b9SJiri Olsa 			void *dst = md->event_copy;
73104391debSArnaldo Carvalho de Melo 
73204391debSArnaldo Carvalho de Melo 			do {
73304391debSArnaldo Carvalho de Melo 				cpy = min(md->mask + 1 - (offset & md->mask), len);
73404391debSArnaldo Carvalho de Melo 				memcpy(dst, &data[offset & md->mask], cpy);
73504391debSArnaldo Carvalho de Melo 				offset += cpy;
73604391debSArnaldo Carvalho de Melo 				dst += cpy;
73704391debSArnaldo Carvalho de Melo 				len -= cpy;
73804391debSArnaldo Carvalho de Melo 			} while (len);
73904391debSArnaldo Carvalho de Melo 
740a65cb4b9SJiri Olsa 			event = (union perf_event *) md->event_copy;
74104391debSArnaldo Carvalho de Melo 		}
74204391debSArnaldo Carvalho de Melo 
74304391debSArnaldo Carvalho de Melo 		old += size;
74404391debSArnaldo Carvalho de Melo 	}
74504391debSArnaldo Carvalho de Melo 
74604391debSArnaldo Carvalho de Melo 	md->prev = old;
7477bb41152SArnaldo Carvalho de Melo 
74804391debSArnaldo Carvalho de Melo 	return event;
74904391debSArnaldo Carvalho de Melo }
750f8a95309SArnaldo Carvalho de Melo 
75182396986SArnaldo Carvalho de Melo static bool perf_mmap__empty(struct perf_mmap *md)
75282396986SArnaldo Carvalho de Melo {
753b72e74d1SAdrian Hunter 	return perf_mmap__read_head(md) == md->prev && !md->auxtrace_mmap.base;
75482396986SArnaldo Carvalho de Melo }
75582396986SArnaldo Carvalho de Melo 
75682396986SArnaldo Carvalho de Melo static void perf_evlist__mmap_get(struct perf_evlist *evlist, int idx)
75782396986SArnaldo Carvalho de Melo {
7587143849aSArnaldo Carvalho de Melo 	atomic_inc(&evlist->mmap[idx].refcnt);
75982396986SArnaldo Carvalho de Melo }
76082396986SArnaldo Carvalho de Melo 
76182396986SArnaldo Carvalho de Melo static void perf_evlist__mmap_put(struct perf_evlist *evlist, int idx)
76282396986SArnaldo Carvalho de Melo {
7637143849aSArnaldo Carvalho de Melo 	BUG_ON(atomic_read(&evlist->mmap[idx].refcnt) == 0);
76482396986SArnaldo Carvalho de Melo 
7657143849aSArnaldo Carvalho de Melo 	if (atomic_dec_and_test(&evlist->mmap[idx].refcnt))
76682396986SArnaldo Carvalho de Melo 		__perf_evlist__munmap(evlist, idx);
76782396986SArnaldo Carvalho de Melo }
76882396986SArnaldo Carvalho de Melo 
7698e50d384SZhouyi Zhou void perf_evlist__mmap_consume(struct perf_evlist *evlist, int idx)
7708e50d384SZhouyi Zhou {
7718e50d384SZhouyi Zhou 	struct perf_mmap *md = &evlist->mmap[idx];
77282396986SArnaldo Carvalho de Melo 
77382396986SArnaldo Carvalho de Melo 	if (!evlist->overwrite) {
7747b8283b5SDavid Ahern 		u64 old = md->prev;
7758e50d384SZhouyi Zhou 
7768e50d384SZhouyi Zhou 		perf_mmap__write_tail(md, old);
7778e50d384SZhouyi Zhou 	}
77882396986SArnaldo Carvalho de Melo 
7797143849aSArnaldo Carvalho de Melo 	if (atomic_read(&md->refcnt) == 1 && perf_mmap__empty(md))
78082396986SArnaldo Carvalho de Melo 		perf_evlist__mmap_put(evlist, idx);
7818e50d384SZhouyi Zhou }
7828e50d384SZhouyi Zhou 
783718c602dSAdrian Hunter int __weak auxtrace_mmap__mmap(struct auxtrace_mmap *mm __maybe_unused,
784718c602dSAdrian Hunter 			       struct auxtrace_mmap_params *mp __maybe_unused,
785718c602dSAdrian Hunter 			       void *userpg __maybe_unused,
786718c602dSAdrian Hunter 			       int fd __maybe_unused)
787718c602dSAdrian Hunter {
788718c602dSAdrian Hunter 	return 0;
789718c602dSAdrian Hunter }
790718c602dSAdrian Hunter 
791718c602dSAdrian Hunter void __weak auxtrace_mmap__munmap(struct auxtrace_mmap *mm __maybe_unused)
792718c602dSAdrian Hunter {
793718c602dSAdrian Hunter }
794718c602dSAdrian Hunter 
795718c602dSAdrian Hunter void __weak auxtrace_mmap_params__init(
796718c602dSAdrian Hunter 			struct auxtrace_mmap_params *mp __maybe_unused,
797718c602dSAdrian Hunter 			off_t auxtrace_offset __maybe_unused,
798718c602dSAdrian Hunter 			unsigned int auxtrace_pages __maybe_unused,
799718c602dSAdrian Hunter 			bool auxtrace_overwrite __maybe_unused)
800718c602dSAdrian Hunter {
801718c602dSAdrian Hunter }
802718c602dSAdrian Hunter 
803718c602dSAdrian Hunter void __weak auxtrace_mmap_params__set_idx(
804718c602dSAdrian Hunter 			struct auxtrace_mmap_params *mp __maybe_unused,
805718c602dSAdrian Hunter 			struct perf_evlist *evlist __maybe_unused,
806718c602dSAdrian Hunter 			int idx __maybe_unused,
807718c602dSAdrian Hunter 			bool per_cpu __maybe_unused)
808718c602dSAdrian Hunter {
809718c602dSAdrian Hunter }
810718c602dSAdrian Hunter 
81193edcbd9SAdrian Hunter static void __perf_evlist__munmap(struct perf_evlist *evlist, int idx)
81293edcbd9SAdrian Hunter {
81393edcbd9SAdrian Hunter 	if (evlist->mmap[idx].base != NULL) {
81493edcbd9SAdrian Hunter 		munmap(evlist->mmap[idx].base, evlist->mmap_len);
81593edcbd9SAdrian Hunter 		evlist->mmap[idx].base = NULL;
8167143849aSArnaldo Carvalho de Melo 		atomic_set(&evlist->mmap[idx].refcnt, 0);
81793edcbd9SAdrian Hunter 	}
818718c602dSAdrian Hunter 	auxtrace_mmap__munmap(&evlist->mmap[idx].auxtrace_mmap);
81993edcbd9SAdrian Hunter }
82093edcbd9SAdrian Hunter 
8217e2ed097SArnaldo Carvalho de Melo void perf_evlist__munmap(struct perf_evlist *evlist)
822f8a95309SArnaldo Carvalho de Melo {
823aece948fSArnaldo Carvalho de Melo 	int i;
824f8a95309SArnaldo Carvalho de Melo 
825983874d1SArnaldo Carvalho de Melo 	if (evlist->mmap == NULL)
826983874d1SArnaldo Carvalho de Melo 		return;
827983874d1SArnaldo Carvalho de Melo 
82893edcbd9SAdrian Hunter 	for (i = 0; i < evlist->nr_mmaps; i++)
82993edcbd9SAdrian Hunter 		__perf_evlist__munmap(evlist, i);
830aece948fSArnaldo Carvalho de Melo 
83104662523SArnaldo Carvalho de Melo 	zfree(&evlist->mmap);
832f8a95309SArnaldo Carvalho de Melo }
833f8a95309SArnaldo Carvalho de Melo 
834806fb630SArnaldo Carvalho de Melo static int perf_evlist__alloc_mmap(struct perf_evlist *evlist)
835f8a95309SArnaldo Carvalho de Melo {
836a14bb7a6SArnaldo Carvalho de Melo 	evlist->nr_mmaps = cpu_map__nr(evlist->cpus);
837ec1e7e43SSukadev Bhattiprolu 	if (cpu_map__empty(evlist->cpus))
838b3a319d5SNamhyung Kim 		evlist->nr_mmaps = thread_map__nr(evlist->threads);
839aece948fSArnaldo Carvalho de Melo 	evlist->mmap = zalloc(evlist->nr_mmaps * sizeof(struct perf_mmap));
840f8a95309SArnaldo Carvalho de Melo 	return evlist->mmap != NULL ? 0 : -ENOMEM;
841f8a95309SArnaldo Carvalho de Melo }
842f8a95309SArnaldo Carvalho de Melo 
843a8a8f3ebSAdrian Hunter struct mmap_params {
844a8a8f3ebSAdrian Hunter 	int prot;
845a8a8f3ebSAdrian Hunter 	int mask;
846718c602dSAdrian Hunter 	struct auxtrace_mmap_params auxtrace_mp;
847a8a8f3ebSAdrian Hunter };
848a8a8f3ebSAdrian Hunter 
849a8a8f3ebSAdrian Hunter static int __perf_evlist__mmap(struct perf_evlist *evlist, int idx,
850a8a8f3ebSAdrian Hunter 			       struct mmap_params *mp, int fd)
851f8a95309SArnaldo Carvalho de Melo {
85282396986SArnaldo Carvalho de Melo 	/*
85382396986SArnaldo Carvalho de Melo 	 * The last one will be done at perf_evlist__mmap_consume(), so that we
85482396986SArnaldo Carvalho de Melo 	 * make sure we don't prevent tools from consuming every last event in
85582396986SArnaldo Carvalho de Melo 	 * the ring buffer.
85682396986SArnaldo Carvalho de Melo 	 *
85782396986SArnaldo Carvalho de Melo 	 * I.e. we can get the POLLHUP meaning that the fd doesn't exist
85882396986SArnaldo Carvalho de Melo 	 * anymore, but the last events for it are still in the ring buffer,
85982396986SArnaldo Carvalho de Melo 	 * waiting to be consumed.
86082396986SArnaldo Carvalho de Melo 	 *
86182396986SArnaldo Carvalho de Melo 	 * Tools can chose to ignore this at their own discretion, but the
86282396986SArnaldo Carvalho de Melo 	 * evlist layer can't just drop it when filtering events in
86382396986SArnaldo Carvalho de Melo 	 * perf_evlist__filter_pollfd().
86482396986SArnaldo Carvalho de Melo 	 */
8657143849aSArnaldo Carvalho de Melo 	atomic_set(&evlist->mmap[idx].refcnt, 2);
866aece948fSArnaldo Carvalho de Melo 	evlist->mmap[idx].prev = 0;
867a8a8f3ebSAdrian Hunter 	evlist->mmap[idx].mask = mp->mask;
868a8a8f3ebSAdrian Hunter 	evlist->mmap[idx].base = mmap(NULL, evlist->mmap_len, mp->prot,
869f8a95309SArnaldo Carvalho de Melo 				      MAP_SHARED, fd, 0);
870301b195dSNelson Elhage 	if (evlist->mmap[idx].base == MAP_FAILED) {
87102635965SAdrian Hunter 		pr_debug2("failed to mmap perf event ring buffer, error %d\n",
87202635965SAdrian Hunter 			  errno);
873301b195dSNelson Elhage 		evlist->mmap[idx].base = NULL;
874f8a95309SArnaldo Carvalho de Melo 		return -1;
875301b195dSNelson Elhage 	}
876ad6765ddSArnaldo Carvalho de Melo 
877718c602dSAdrian Hunter 	if (auxtrace_mmap__mmap(&evlist->mmap[idx].auxtrace_mmap,
878718c602dSAdrian Hunter 				&mp->auxtrace_mp, evlist->mmap[idx].base, fd))
879718c602dSAdrian Hunter 		return -1;
880718c602dSAdrian Hunter 
881f8a95309SArnaldo Carvalho de Melo 	return 0;
882f8a95309SArnaldo Carvalho de Melo }
883f8a95309SArnaldo Carvalho de Melo 
88404e21314SAdrian Hunter static int perf_evlist__mmap_per_evsel(struct perf_evlist *evlist, int idx,
885a8a8f3ebSAdrian Hunter 				       struct mmap_params *mp, int cpu,
886a8a8f3ebSAdrian Hunter 				       int thread, int *output)
887aece948fSArnaldo Carvalho de Melo {
888aece948fSArnaldo Carvalho de Melo 	struct perf_evsel *evsel;
88904e21314SAdrian Hunter 
8900050f7aaSArnaldo Carvalho de Melo 	evlist__for_each(evlist, evsel) {
891bf8e8f4bSAdrian Hunter 		int fd;
892bf8e8f4bSAdrian Hunter 
893bf8e8f4bSAdrian Hunter 		if (evsel->system_wide && thread)
894bf8e8f4bSAdrian Hunter 			continue;
895bf8e8f4bSAdrian Hunter 
896bf8e8f4bSAdrian Hunter 		fd = FD(evsel, cpu, thread);
89704e21314SAdrian Hunter 
89804e21314SAdrian Hunter 		if (*output == -1) {
89904e21314SAdrian Hunter 			*output = fd;
900a8a8f3ebSAdrian Hunter 			if (__perf_evlist__mmap(evlist, idx, mp, *output) < 0)
90104e21314SAdrian Hunter 				return -1;
90204e21314SAdrian Hunter 		} else {
90304e21314SAdrian Hunter 			if (ioctl(fd, PERF_EVENT_IOC_SET_OUTPUT, *output) != 0)
90404e21314SAdrian Hunter 				return -1;
90582396986SArnaldo Carvalho de Melo 
90682396986SArnaldo Carvalho de Melo 			perf_evlist__mmap_get(evlist, idx);
90704e21314SAdrian Hunter 		}
90804e21314SAdrian Hunter 
909f90d194aSAdrian Hunter 		/*
910f90d194aSAdrian Hunter 		 * The system_wide flag causes a selected event to be opened
911f90d194aSAdrian Hunter 		 * always without a pid.  Consequently it will never get a
912f90d194aSAdrian Hunter 		 * POLLHUP, but it is used for tracking in combination with
913f90d194aSAdrian Hunter 		 * other events, so it should not need to be polled anyway.
914f90d194aSAdrian Hunter 		 * Therefore don't add it for polling.
915f90d194aSAdrian Hunter 		 */
916f90d194aSAdrian Hunter 		if (!evsel->system_wide &&
917f90d194aSAdrian Hunter 		    __perf_evlist__add_pollfd(evlist, fd, idx) < 0) {
91882396986SArnaldo Carvalho de Melo 			perf_evlist__mmap_put(evlist, idx);
919ad6765ddSArnaldo Carvalho de Melo 			return -1;
92082396986SArnaldo Carvalho de Melo 		}
921033fa713SArnaldo Carvalho de Melo 
9223c659eedSAdrian Hunter 		if (evsel->attr.read_format & PERF_FORMAT_ID) {
9233c659eedSAdrian Hunter 			if (perf_evlist__id_add_fd(evlist, evsel, cpu, thread,
9243c659eedSAdrian Hunter 						   fd) < 0)
92504e21314SAdrian Hunter 				return -1;
9263c659eedSAdrian Hunter 			perf_evlist__set_sid_idx(evlist, evsel, idx, cpu,
9273c659eedSAdrian Hunter 						 thread);
9283c659eedSAdrian Hunter 		}
92904e21314SAdrian Hunter 	}
93004e21314SAdrian Hunter 
93104e21314SAdrian Hunter 	return 0;
93204e21314SAdrian Hunter }
93304e21314SAdrian Hunter 
934a8a8f3ebSAdrian Hunter static int perf_evlist__mmap_per_cpu(struct perf_evlist *evlist,
935a8a8f3ebSAdrian Hunter 				     struct mmap_params *mp)
93604e21314SAdrian Hunter {
937aece948fSArnaldo Carvalho de Melo 	int cpu, thread;
938b3a319d5SNamhyung Kim 	int nr_cpus = cpu_map__nr(evlist->cpus);
939b3a319d5SNamhyung Kim 	int nr_threads = thread_map__nr(evlist->threads);
940aece948fSArnaldo Carvalho de Melo 
941e3e1a54fSAdrian Hunter 	pr_debug2("perf event ring buffer mmapped per cpu\n");
942b3a319d5SNamhyung Kim 	for (cpu = 0; cpu < nr_cpus; cpu++) {
943aece948fSArnaldo Carvalho de Melo 		int output = -1;
944aece948fSArnaldo Carvalho de Melo 
945718c602dSAdrian Hunter 		auxtrace_mmap_params__set_idx(&mp->auxtrace_mp, evlist, cpu,
946718c602dSAdrian Hunter 					      true);
947718c602dSAdrian Hunter 
948b3a319d5SNamhyung Kim 		for (thread = 0; thread < nr_threads; thread++) {
949a8a8f3ebSAdrian Hunter 			if (perf_evlist__mmap_per_evsel(evlist, cpu, mp, cpu,
950a8a8f3ebSAdrian Hunter 							thread, &output))
951aece948fSArnaldo Carvalho de Melo 				goto out_unmap;
952aece948fSArnaldo Carvalho de Melo 		}
953aece948fSArnaldo Carvalho de Melo 	}
954aece948fSArnaldo Carvalho de Melo 
955aece948fSArnaldo Carvalho de Melo 	return 0;
956aece948fSArnaldo Carvalho de Melo 
957aece948fSArnaldo Carvalho de Melo out_unmap:
95893edcbd9SAdrian Hunter 	for (cpu = 0; cpu < nr_cpus; cpu++)
95993edcbd9SAdrian Hunter 		__perf_evlist__munmap(evlist, cpu);
960aece948fSArnaldo Carvalho de Melo 	return -1;
961aece948fSArnaldo Carvalho de Melo }
962aece948fSArnaldo Carvalho de Melo 
963a8a8f3ebSAdrian Hunter static int perf_evlist__mmap_per_thread(struct perf_evlist *evlist,
964a8a8f3ebSAdrian Hunter 					struct mmap_params *mp)
965aece948fSArnaldo Carvalho de Melo {
966aece948fSArnaldo Carvalho de Melo 	int thread;
967b3a319d5SNamhyung Kim 	int nr_threads = thread_map__nr(evlist->threads);
968aece948fSArnaldo Carvalho de Melo 
969e3e1a54fSAdrian Hunter 	pr_debug2("perf event ring buffer mmapped per thread\n");
970b3a319d5SNamhyung Kim 	for (thread = 0; thread < nr_threads; thread++) {
971aece948fSArnaldo Carvalho de Melo 		int output = -1;
972aece948fSArnaldo Carvalho de Melo 
973718c602dSAdrian Hunter 		auxtrace_mmap_params__set_idx(&mp->auxtrace_mp, evlist, thread,
974718c602dSAdrian Hunter 					      false);
975718c602dSAdrian Hunter 
976a8a8f3ebSAdrian Hunter 		if (perf_evlist__mmap_per_evsel(evlist, thread, mp, 0, thread,
977a8a8f3ebSAdrian Hunter 						&output))
978aece948fSArnaldo Carvalho de Melo 			goto out_unmap;
979aece948fSArnaldo Carvalho de Melo 	}
980aece948fSArnaldo Carvalho de Melo 
981aece948fSArnaldo Carvalho de Melo 	return 0;
982aece948fSArnaldo Carvalho de Melo 
983aece948fSArnaldo Carvalho de Melo out_unmap:
98493edcbd9SAdrian Hunter 	for (thread = 0; thread < nr_threads; thread++)
98593edcbd9SAdrian Hunter 		__perf_evlist__munmap(evlist, thread);
986aece948fSArnaldo Carvalho de Melo 	return -1;
987aece948fSArnaldo Carvalho de Melo }
988aece948fSArnaldo Carvalho de Melo 
989f5e7150cSArnaldo Carvalho de Melo unsigned long perf_event_mlock_kb_in_pages(void)
990994a1f78SJiri Olsa {
991f5e7150cSArnaldo Carvalho de Melo 	unsigned long pages;
9928185e881SArnaldo Carvalho de Melo 	int max;
9938185e881SArnaldo Carvalho de Melo 
9948185e881SArnaldo Carvalho de Melo 	if (sysctl__read_int("kernel/perf_event_mlock_kb", &max) < 0) {
9958185e881SArnaldo Carvalho de Melo 		/*
9968185e881SArnaldo Carvalho de Melo 		 * Pick a once upon a time good value, i.e. things look
9978185e881SArnaldo Carvalho de Melo 		 * strange since we can't read a sysctl value, but lets not
9988185e881SArnaldo Carvalho de Melo 		 * die yet...
9998185e881SArnaldo Carvalho de Melo 		 */
10008185e881SArnaldo Carvalho de Melo 		max = 512;
10018185e881SArnaldo Carvalho de Melo 	} else {
10028185e881SArnaldo Carvalho de Melo 		max -= (page_size / 1024);
10038185e881SArnaldo Carvalho de Melo 	}
10048185e881SArnaldo Carvalho de Melo 
10058185e881SArnaldo Carvalho de Melo 	pages = (max * 1024) / page_size;
10061be300f4SArnaldo Carvalho de Melo 	if (!is_power_of_2(pages))
10071be300f4SArnaldo Carvalho de Melo 		pages = rounddown_pow_of_two(pages);
1008f5e7150cSArnaldo Carvalho de Melo 
1009f5e7150cSArnaldo Carvalho de Melo 	return pages;
1010f5e7150cSArnaldo Carvalho de Melo }
1011f5e7150cSArnaldo Carvalho de Melo 
1012f5e7150cSArnaldo Carvalho de Melo static size_t perf_evlist__mmap_size(unsigned long pages)
1013f5e7150cSArnaldo Carvalho de Melo {
1014f5e7150cSArnaldo Carvalho de Melo 	if (pages == UINT_MAX)
1015f5e7150cSArnaldo Carvalho de Melo 		pages = perf_event_mlock_kb_in_pages();
1016f5e7150cSArnaldo Carvalho de Melo 	else if (!is_power_of_2(pages))
1017994a1f78SJiri Olsa 		return 0;
1018994a1f78SJiri Olsa 
1019994a1f78SJiri Olsa 	return (pages + 1) * page_size;
1020994a1f78SJiri Olsa }
1021994a1f78SJiri Olsa 
102233c2dcfdSDavid Ahern static long parse_pages_arg(const char *str, unsigned long min,
102333c2dcfdSDavid Ahern 			    unsigned long max)
1024994a1f78SJiri Olsa {
10252fbe4abeSAdrian Hunter 	unsigned long pages, val;
102627050f53SJiri Olsa 	static struct parse_tag tags[] = {
102727050f53SJiri Olsa 		{ .tag  = 'B', .mult = 1       },
102827050f53SJiri Olsa 		{ .tag  = 'K', .mult = 1 << 10 },
102927050f53SJiri Olsa 		{ .tag  = 'M', .mult = 1 << 20 },
103027050f53SJiri Olsa 		{ .tag  = 'G', .mult = 1 << 30 },
103127050f53SJiri Olsa 		{ .tag  = 0 },
103227050f53SJiri Olsa 	};
1033994a1f78SJiri Olsa 
10348973504bSDavid Ahern 	if (str == NULL)
103533c2dcfdSDavid Ahern 		return -EINVAL;
10368973504bSDavid Ahern 
103727050f53SJiri Olsa 	val = parse_tag_value(str, tags);
10382fbe4abeSAdrian Hunter 	if (val != (unsigned long) -1) {
103927050f53SJiri Olsa 		/* we got file size value */
104027050f53SJiri Olsa 		pages = PERF_ALIGN(val, page_size) / page_size;
104127050f53SJiri Olsa 	} else {
104227050f53SJiri Olsa 		/* we got pages count value */
104327050f53SJiri Olsa 		char *eptr;
1044994a1f78SJiri Olsa 		pages = strtoul(str, &eptr, 10);
104533c2dcfdSDavid Ahern 		if (*eptr != '\0')
104633c2dcfdSDavid Ahern 			return -EINVAL;
104727050f53SJiri Olsa 	}
1048994a1f78SJiri Olsa 
10492bcab6c1SAdrian Hunter 	if (pages == 0 && min == 0) {
105033c2dcfdSDavid Ahern 		/* leave number of pages at 0 */
10511dbfa938SAdrian Hunter 	} else if (!is_power_of_2(pages)) {
105233c2dcfdSDavid Ahern 		/* round pages up to next power of 2 */
105391529834SArnaldo Carvalho de Melo 		pages = roundup_pow_of_two(pages);
10541dbfa938SAdrian Hunter 		if (!pages)
10551dbfa938SAdrian Hunter 			return -EINVAL;
10569639837eSDavid Ahern 		pr_info("rounding mmap pages size to %lu bytes (%lu pages)\n",
10579639837eSDavid Ahern 			pages * page_size, pages);
10582fbe4abeSAdrian Hunter 	}
10592fbe4abeSAdrian Hunter 
106033c2dcfdSDavid Ahern 	if (pages > max)
106133c2dcfdSDavid Ahern 		return -EINVAL;
106233c2dcfdSDavid Ahern 
106333c2dcfdSDavid Ahern 	return pages;
106433c2dcfdSDavid Ahern }
106533c2dcfdSDavid Ahern 
1066e9db1310SAdrian Hunter int __perf_evlist__parse_mmap_pages(unsigned int *mmap_pages, const char *str)
106733c2dcfdSDavid Ahern {
106833c2dcfdSDavid Ahern 	unsigned long max = UINT_MAX;
106933c2dcfdSDavid Ahern 	long pages;
107033c2dcfdSDavid Ahern 
1071f5ae9c42SAdrian Hunter 	if (max > SIZE_MAX / page_size)
107233c2dcfdSDavid Ahern 		max = SIZE_MAX / page_size;
107333c2dcfdSDavid Ahern 
107433c2dcfdSDavid Ahern 	pages = parse_pages_arg(str, 1, max);
107533c2dcfdSDavid Ahern 	if (pages < 0) {
107633c2dcfdSDavid Ahern 		pr_err("Invalid argument for --mmap_pages/-m\n");
1077994a1f78SJiri Olsa 		return -1;
1078994a1f78SJiri Olsa 	}
1079994a1f78SJiri Olsa 
1080994a1f78SJiri Olsa 	*mmap_pages = pages;
1081994a1f78SJiri Olsa 	return 0;
1082994a1f78SJiri Olsa }
1083994a1f78SJiri Olsa 
1084e9db1310SAdrian Hunter int perf_evlist__parse_mmap_pages(const struct option *opt, const char *str,
1085e9db1310SAdrian Hunter 				  int unset __maybe_unused)
1086e9db1310SAdrian Hunter {
1087e9db1310SAdrian Hunter 	return __perf_evlist__parse_mmap_pages(opt->value, str);
1088e9db1310SAdrian Hunter }
1089e9db1310SAdrian Hunter 
1090c83fa7f2SAdrian Hunter /**
1091718c602dSAdrian Hunter  * perf_evlist__mmap_ex - Create mmaps to receive events.
1092c83fa7f2SAdrian Hunter  * @evlist: list of events
1093c83fa7f2SAdrian Hunter  * @pages: map length in pages
1094c83fa7f2SAdrian Hunter  * @overwrite: overwrite older events?
1095718c602dSAdrian Hunter  * @auxtrace_pages - auxtrace map length in pages
1096718c602dSAdrian Hunter  * @auxtrace_overwrite - overwrite older auxtrace data?
1097f8a95309SArnaldo Carvalho de Melo  *
1098c83fa7f2SAdrian Hunter  * If @overwrite is %false the user needs to signal event consumption using
1099c83fa7f2SAdrian Hunter  * perf_mmap__write_tail().  Using perf_evlist__mmap_read() does this
1100c83fa7f2SAdrian Hunter  * automatically.
1101f8a95309SArnaldo Carvalho de Melo  *
1102718c602dSAdrian Hunter  * Similarly, if @auxtrace_overwrite is %false the user needs to signal data
1103718c602dSAdrian Hunter  * consumption using auxtrace_mmap__write_tail().
1104718c602dSAdrian Hunter  *
1105c83fa7f2SAdrian Hunter  * Return: %0 on success, negative error code otherwise.
1106f8a95309SArnaldo Carvalho de Melo  */
1107718c602dSAdrian Hunter int perf_evlist__mmap_ex(struct perf_evlist *evlist, unsigned int pages,
1108718c602dSAdrian Hunter 			 bool overwrite, unsigned int auxtrace_pages,
1109718c602dSAdrian Hunter 			 bool auxtrace_overwrite)
1110f8a95309SArnaldo Carvalho de Melo {
1111aece948fSArnaldo Carvalho de Melo 	struct perf_evsel *evsel;
11127e2ed097SArnaldo Carvalho de Melo 	const struct cpu_map *cpus = evlist->cpus;
11137e2ed097SArnaldo Carvalho de Melo 	const struct thread_map *threads = evlist->threads;
1114a8a8f3ebSAdrian Hunter 	struct mmap_params mp = {
1115a8a8f3ebSAdrian Hunter 		.prot = PROT_READ | (overwrite ? 0 : PROT_WRITE),
1116a8a8f3ebSAdrian Hunter 	};
111750a682ceSArnaldo Carvalho de Melo 
11187e2ed097SArnaldo Carvalho de Melo 	if (evlist->mmap == NULL && perf_evlist__alloc_mmap(evlist) < 0)
1119f8a95309SArnaldo Carvalho de Melo 		return -ENOMEM;
1120f8a95309SArnaldo Carvalho de Melo 
11211b85337dSArnaldo Carvalho de Melo 	if (evlist->pollfd.entries == NULL && perf_evlist__alloc_pollfd(evlist) < 0)
1122f8a95309SArnaldo Carvalho de Melo 		return -ENOMEM;
1123f8a95309SArnaldo Carvalho de Melo 
1124f8a95309SArnaldo Carvalho de Melo 	evlist->overwrite = overwrite;
1125994a1f78SJiri Olsa 	evlist->mmap_len = perf_evlist__mmap_size(pages);
11262af68ef5SAdrian Hunter 	pr_debug("mmap size %zuB\n", evlist->mmap_len);
1127a8a8f3ebSAdrian Hunter 	mp.mask = evlist->mmap_len - page_size - 1;
1128f8a95309SArnaldo Carvalho de Melo 
1129718c602dSAdrian Hunter 	auxtrace_mmap_params__init(&mp.auxtrace_mp, evlist->mmap_len,
1130718c602dSAdrian Hunter 				   auxtrace_pages, auxtrace_overwrite);
1131718c602dSAdrian Hunter 
11320050f7aaSArnaldo Carvalho de Melo 	evlist__for_each(evlist, evsel) {
1133f8a95309SArnaldo Carvalho de Melo 		if ((evsel->attr.read_format & PERF_FORMAT_ID) &&
1134a91e5431SArnaldo Carvalho de Melo 		    evsel->sample_id == NULL &&
1135a14bb7a6SArnaldo Carvalho de Melo 		    perf_evsel__alloc_id(evsel, cpu_map__nr(cpus), threads->nr) < 0)
1136f8a95309SArnaldo Carvalho de Melo 			return -ENOMEM;
1137f8a95309SArnaldo Carvalho de Melo 	}
1138f8a95309SArnaldo Carvalho de Melo 
1139ec1e7e43SSukadev Bhattiprolu 	if (cpu_map__empty(cpus))
1140a8a8f3ebSAdrian Hunter 		return perf_evlist__mmap_per_thread(evlist, &mp);
1141f8a95309SArnaldo Carvalho de Melo 
1142a8a8f3ebSAdrian Hunter 	return perf_evlist__mmap_per_cpu(evlist, &mp);
1143f8a95309SArnaldo Carvalho de Melo }
11447e2ed097SArnaldo Carvalho de Melo 
1145718c602dSAdrian Hunter int perf_evlist__mmap(struct perf_evlist *evlist, unsigned int pages,
1146718c602dSAdrian Hunter 		      bool overwrite)
1147718c602dSAdrian Hunter {
1148718c602dSAdrian Hunter 	return perf_evlist__mmap_ex(evlist, pages, overwrite, 0, false);
1149718c602dSAdrian Hunter }
1150718c602dSAdrian Hunter 
1151602ad878SArnaldo Carvalho de Melo int perf_evlist__create_maps(struct perf_evlist *evlist, struct target *target)
11527e2ed097SArnaldo Carvalho de Melo {
115374bfd2b2SAdrian Hunter 	struct cpu_map *cpus;
115474bfd2b2SAdrian Hunter 	struct thread_map *threads;
11557e2ed097SArnaldo Carvalho de Melo 
115674bfd2b2SAdrian Hunter 	threads = thread_map__new_str(target->pid, target->tid, target->uid);
115774bfd2b2SAdrian Hunter 
115874bfd2b2SAdrian Hunter 	if (!threads)
11597e2ed097SArnaldo Carvalho de Melo 		return -1;
11607e2ed097SArnaldo Carvalho de Melo 
11619c105fbcSDongsheng Yang 	if (target__uses_dummy_map(target))
116274bfd2b2SAdrian Hunter 		cpus = cpu_map__dummy_new();
1163879d77d0SNamhyung Kim 	else
116474bfd2b2SAdrian Hunter 		cpus = cpu_map__new(target->cpu_list);
11657e2ed097SArnaldo Carvalho de Melo 
116674bfd2b2SAdrian Hunter 	if (!cpus)
11677e2ed097SArnaldo Carvalho de Melo 		goto out_delete_threads;
11687e2ed097SArnaldo Carvalho de Melo 
1169ec9a77a7SAdrian Hunter 	evlist->has_user_cpus = !!target->cpu_list;
1170ec9a77a7SAdrian Hunter 
117174bfd2b2SAdrian Hunter 	perf_evlist__set_maps(evlist, cpus, threads);
1172d5bc056eSAdrian Hunter 
1173d5bc056eSAdrian Hunter 	return 0;
11747e2ed097SArnaldo Carvalho de Melo 
11757e2ed097SArnaldo Carvalho de Melo out_delete_threads:
117674bfd2b2SAdrian Hunter 	thread_map__put(threads);
11777e2ed097SArnaldo Carvalho de Melo 	return -1;
11787e2ed097SArnaldo Carvalho de Melo }
11797e2ed097SArnaldo Carvalho de Melo 
1180d5bc056eSAdrian Hunter void perf_evlist__set_maps(struct perf_evlist *evlist, struct cpu_map *cpus,
11813de5cfb0SJiri Olsa 			   struct thread_map *threads)
11823de5cfb0SJiri Olsa {
1183934e0f20SAdrian Hunter 	/*
1184934e0f20SAdrian Hunter 	 * Allow for the possibility that one or another of the maps isn't being
1185934e0f20SAdrian Hunter 	 * changed i.e. don't put it.  Note we are assuming the maps that are
1186934e0f20SAdrian Hunter 	 * being applied are brand new and evlist is taking ownership of the
1187934e0f20SAdrian Hunter 	 * original reference count of 1.  If that is not the case it is up to
1188934e0f20SAdrian Hunter 	 * the caller to increase the reference count.
1189934e0f20SAdrian Hunter 	 */
1190934e0f20SAdrian Hunter 	if (cpus != evlist->cpus) {
11913de5cfb0SJiri Olsa 		cpu_map__put(evlist->cpus);
1192a55e5663SArnaldo Carvalho de Melo 		evlist->cpus = cpu_map__get(cpus);
1193934e0f20SAdrian Hunter 	}
11943de5cfb0SJiri Olsa 
1195934e0f20SAdrian Hunter 	if (threads != evlist->threads) {
11963de5cfb0SJiri Olsa 		thread_map__put(evlist->threads);
1197a55e5663SArnaldo Carvalho de Melo 		evlist->threads = thread_map__get(threads);
1198934e0f20SAdrian Hunter 	}
11993de5cfb0SJiri Olsa 
1200ec9a77a7SAdrian Hunter 	perf_evlist__propagate_maps(evlist);
12013de5cfb0SJiri Olsa }
12023de5cfb0SJiri Olsa 
120322c8a376SArnaldo Carvalho de Melo void __perf_evlist__set_sample_bit(struct perf_evlist *evlist,
120422c8a376SArnaldo Carvalho de Melo 				   enum perf_event_sample_format bit)
120522c8a376SArnaldo Carvalho de Melo {
120622c8a376SArnaldo Carvalho de Melo 	struct perf_evsel *evsel;
120722c8a376SArnaldo Carvalho de Melo 
120822c8a376SArnaldo Carvalho de Melo 	evlist__for_each(evlist, evsel)
120922c8a376SArnaldo Carvalho de Melo 		__perf_evsel__set_sample_bit(evsel, bit);
121022c8a376SArnaldo Carvalho de Melo }
121122c8a376SArnaldo Carvalho de Melo 
121222c8a376SArnaldo Carvalho de Melo void __perf_evlist__reset_sample_bit(struct perf_evlist *evlist,
121322c8a376SArnaldo Carvalho de Melo 				     enum perf_event_sample_format bit)
121422c8a376SArnaldo Carvalho de Melo {
121522c8a376SArnaldo Carvalho de Melo 	struct perf_evsel *evsel;
121622c8a376SArnaldo Carvalho de Melo 
121722c8a376SArnaldo Carvalho de Melo 	evlist__for_each(evlist, evsel)
121822c8a376SArnaldo Carvalho de Melo 		__perf_evsel__reset_sample_bit(evsel, bit);
121922c8a376SArnaldo Carvalho de Melo }
122022c8a376SArnaldo Carvalho de Melo 
122123d4aad4SArnaldo Carvalho de Melo int perf_evlist__apply_filters(struct perf_evlist *evlist, struct perf_evsel **err_evsel)
12220a102479SFrederic Weisbecker {
12230a102479SFrederic Weisbecker 	struct perf_evsel *evsel;
1224745cefc5SArnaldo Carvalho de Melo 	int err = 0;
1225745cefc5SArnaldo Carvalho de Melo 	const int ncpus = cpu_map__nr(evlist->cpus),
1226b3a319d5SNamhyung Kim 		  nthreads = thread_map__nr(evlist->threads);
12270a102479SFrederic Weisbecker 
12280050f7aaSArnaldo Carvalho de Melo 	evlist__for_each(evlist, evsel) {
1229745cefc5SArnaldo Carvalho de Melo 		if (evsel->filter == NULL)
12300a102479SFrederic Weisbecker 			continue;
1231745cefc5SArnaldo Carvalho de Melo 
1232d988d5eeSKan Liang 		/*
1233d988d5eeSKan Liang 		 * filters only work for tracepoint event, which doesn't have cpu limit.
1234d988d5eeSKan Liang 		 * So evlist and evsel should always be same.
1235d988d5eeSKan Liang 		 */
1236f47805a2SArnaldo Carvalho de Melo 		err = perf_evsel__apply_filter(evsel, ncpus, nthreads, evsel->filter);
123723d4aad4SArnaldo Carvalho de Melo 		if (err) {
123823d4aad4SArnaldo Carvalho de Melo 			*err_evsel = evsel;
1239745cefc5SArnaldo Carvalho de Melo 			break;
12400a102479SFrederic Weisbecker 		}
124123d4aad4SArnaldo Carvalho de Melo 	}
12420a102479SFrederic Weisbecker 
1243745cefc5SArnaldo Carvalho de Melo 	return err;
1244745cefc5SArnaldo Carvalho de Melo }
1245745cefc5SArnaldo Carvalho de Melo 
1246745cefc5SArnaldo Carvalho de Melo int perf_evlist__set_filter(struct perf_evlist *evlist, const char *filter)
1247745cefc5SArnaldo Carvalho de Melo {
1248745cefc5SArnaldo Carvalho de Melo 	struct perf_evsel *evsel;
1249745cefc5SArnaldo Carvalho de Melo 	int err = 0;
1250745cefc5SArnaldo Carvalho de Melo 
12510050f7aaSArnaldo Carvalho de Melo 	evlist__for_each(evlist, evsel) {
1252fdf14720SWang Nan 		if (evsel->attr.type != PERF_TYPE_TRACEPOINT)
1253fdf14720SWang Nan 			continue;
1254fdf14720SWang Nan 
125594ad89bcSArnaldo Carvalho de Melo 		err = perf_evsel__set_filter(evsel, filter);
1256745cefc5SArnaldo Carvalho de Melo 		if (err)
1257745cefc5SArnaldo Carvalho de Melo 			break;
1258745cefc5SArnaldo Carvalho de Melo 	}
1259745cefc5SArnaldo Carvalho de Melo 
1260745cefc5SArnaldo Carvalho de Melo 	return err;
12610a102479SFrederic Weisbecker }
126274429964SFrederic Weisbecker 
1263be199adaSArnaldo Carvalho de Melo int perf_evlist__set_filter_pids(struct perf_evlist *evlist, size_t npids, pid_t *pids)
1264cfd70a26SArnaldo Carvalho de Melo {
1265cfd70a26SArnaldo Carvalho de Melo 	char *filter;
1266be199adaSArnaldo Carvalho de Melo 	int ret = -1;
1267be199adaSArnaldo Carvalho de Melo 	size_t i;
1268cfd70a26SArnaldo Carvalho de Melo 
1269be199adaSArnaldo Carvalho de Melo 	for (i = 0; i < npids; ++i) {
1270be199adaSArnaldo Carvalho de Melo 		if (i == 0) {
1271be199adaSArnaldo Carvalho de Melo 			if (asprintf(&filter, "common_pid != %d", pids[i]) < 0)
1272cfd70a26SArnaldo Carvalho de Melo 				return -1;
1273be199adaSArnaldo Carvalho de Melo 		} else {
1274be199adaSArnaldo Carvalho de Melo 			char *tmp;
1275be199adaSArnaldo Carvalho de Melo 
1276be199adaSArnaldo Carvalho de Melo 			if (asprintf(&tmp, "%s && common_pid != %d", filter, pids[i]) < 0)
1277be199adaSArnaldo Carvalho de Melo 				goto out_free;
1278be199adaSArnaldo Carvalho de Melo 
1279be199adaSArnaldo Carvalho de Melo 			free(filter);
1280be199adaSArnaldo Carvalho de Melo 			filter = tmp;
1281be199adaSArnaldo Carvalho de Melo 		}
1282be199adaSArnaldo Carvalho de Melo 	}
1283cfd70a26SArnaldo Carvalho de Melo 
1284cfd70a26SArnaldo Carvalho de Melo 	ret = perf_evlist__set_filter(evlist, filter);
1285be199adaSArnaldo Carvalho de Melo out_free:
1286cfd70a26SArnaldo Carvalho de Melo 	free(filter);
1287cfd70a26SArnaldo Carvalho de Melo 	return ret;
1288cfd70a26SArnaldo Carvalho de Melo }
1289cfd70a26SArnaldo Carvalho de Melo 
1290be199adaSArnaldo Carvalho de Melo int perf_evlist__set_filter_pid(struct perf_evlist *evlist, pid_t pid)
1291be199adaSArnaldo Carvalho de Melo {
1292be199adaSArnaldo Carvalho de Melo 	return perf_evlist__set_filter_pids(evlist, 1, &pid);
1293be199adaSArnaldo Carvalho de Melo }
1294be199adaSArnaldo Carvalho de Melo 
12950c21f736SArnaldo Carvalho de Melo bool perf_evlist__valid_sample_type(struct perf_evlist *evlist)
129674429964SFrederic Weisbecker {
129775562573SAdrian Hunter 	struct perf_evsel *pos;
1298c2a70653SArnaldo Carvalho de Melo 
129975562573SAdrian Hunter 	if (evlist->nr_entries == 1)
130075562573SAdrian Hunter 		return true;
130175562573SAdrian Hunter 
130275562573SAdrian Hunter 	if (evlist->id_pos < 0 || evlist->is_pos < 0)
130375562573SAdrian Hunter 		return false;
130475562573SAdrian Hunter 
13050050f7aaSArnaldo Carvalho de Melo 	evlist__for_each(evlist, pos) {
130675562573SAdrian Hunter 		if (pos->id_pos != evlist->id_pos ||
130775562573SAdrian Hunter 		    pos->is_pos != evlist->is_pos)
1308c2a70653SArnaldo Carvalho de Melo 			return false;
130974429964SFrederic Weisbecker 	}
131074429964SFrederic Weisbecker 
1311c2a70653SArnaldo Carvalho de Melo 	return true;
1312c2a70653SArnaldo Carvalho de Melo }
1313c2a70653SArnaldo Carvalho de Melo 
131475562573SAdrian Hunter u64 __perf_evlist__combined_sample_type(struct perf_evlist *evlist)
1315c2a70653SArnaldo Carvalho de Melo {
131675562573SAdrian Hunter 	struct perf_evsel *evsel;
131775562573SAdrian Hunter 
131875562573SAdrian Hunter 	if (evlist->combined_sample_type)
131975562573SAdrian Hunter 		return evlist->combined_sample_type;
132075562573SAdrian Hunter 
13210050f7aaSArnaldo Carvalho de Melo 	evlist__for_each(evlist, evsel)
132275562573SAdrian Hunter 		evlist->combined_sample_type |= evsel->attr.sample_type;
132375562573SAdrian Hunter 
132475562573SAdrian Hunter 	return evlist->combined_sample_type;
132575562573SAdrian Hunter }
132675562573SAdrian Hunter 
132775562573SAdrian Hunter u64 perf_evlist__combined_sample_type(struct perf_evlist *evlist)
132875562573SAdrian Hunter {
132975562573SAdrian Hunter 	evlist->combined_sample_type = 0;
133075562573SAdrian Hunter 	return __perf_evlist__combined_sample_type(evlist);
1331c2a70653SArnaldo Carvalho de Melo }
1332c2a70653SArnaldo Carvalho de Melo 
133398df858eSAndi Kleen u64 perf_evlist__combined_branch_type(struct perf_evlist *evlist)
133498df858eSAndi Kleen {
133598df858eSAndi Kleen 	struct perf_evsel *evsel;
133698df858eSAndi Kleen 	u64 branch_type = 0;
133798df858eSAndi Kleen 
133898df858eSAndi Kleen 	evlist__for_each(evlist, evsel)
133998df858eSAndi Kleen 		branch_type |= evsel->attr.branch_sample_type;
134098df858eSAndi Kleen 	return branch_type;
134198df858eSAndi Kleen }
134298df858eSAndi Kleen 
13439ede473cSJiri Olsa bool perf_evlist__valid_read_format(struct perf_evlist *evlist)
13449ede473cSJiri Olsa {
13459ede473cSJiri Olsa 	struct perf_evsel *first = perf_evlist__first(evlist), *pos = first;
13469ede473cSJiri Olsa 	u64 read_format = first->attr.read_format;
13479ede473cSJiri Olsa 	u64 sample_type = first->attr.sample_type;
13489ede473cSJiri Olsa 
13490050f7aaSArnaldo Carvalho de Melo 	evlist__for_each(evlist, pos) {
13509ede473cSJiri Olsa 		if (read_format != pos->attr.read_format)
13519ede473cSJiri Olsa 			return false;
13529ede473cSJiri Olsa 	}
13539ede473cSJiri Olsa 
13549ede473cSJiri Olsa 	/* PERF_SAMPLE_READ imples PERF_FORMAT_ID. */
13559ede473cSJiri Olsa 	if ((sample_type & PERF_SAMPLE_READ) &&
13569ede473cSJiri Olsa 	    !(read_format & PERF_FORMAT_ID)) {
13579ede473cSJiri Olsa 		return false;
13589ede473cSJiri Olsa 	}
13599ede473cSJiri Olsa 
13609ede473cSJiri Olsa 	return true;
13619ede473cSJiri Olsa }
13629ede473cSJiri Olsa 
13639ede473cSJiri Olsa u64 perf_evlist__read_format(struct perf_evlist *evlist)
13649ede473cSJiri Olsa {
13659ede473cSJiri Olsa 	struct perf_evsel *first = perf_evlist__first(evlist);
13669ede473cSJiri Olsa 	return first->attr.read_format;
13679ede473cSJiri Olsa }
13689ede473cSJiri Olsa 
13690c21f736SArnaldo Carvalho de Melo u16 perf_evlist__id_hdr_size(struct perf_evlist *evlist)
137081e36bffSArnaldo Carvalho de Melo {
13710c21f736SArnaldo Carvalho de Melo 	struct perf_evsel *first = perf_evlist__first(evlist);
137281e36bffSArnaldo Carvalho de Melo 	struct perf_sample *data;
137381e36bffSArnaldo Carvalho de Melo 	u64 sample_type;
137481e36bffSArnaldo Carvalho de Melo 	u16 size = 0;
137581e36bffSArnaldo Carvalho de Melo 
137681e36bffSArnaldo Carvalho de Melo 	if (!first->attr.sample_id_all)
137781e36bffSArnaldo Carvalho de Melo 		goto out;
137881e36bffSArnaldo Carvalho de Melo 
137981e36bffSArnaldo Carvalho de Melo 	sample_type = first->attr.sample_type;
138081e36bffSArnaldo Carvalho de Melo 
138181e36bffSArnaldo Carvalho de Melo 	if (sample_type & PERF_SAMPLE_TID)
138281e36bffSArnaldo Carvalho de Melo 		size += sizeof(data->tid) * 2;
138381e36bffSArnaldo Carvalho de Melo 
138481e36bffSArnaldo Carvalho de Melo        if (sample_type & PERF_SAMPLE_TIME)
138581e36bffSArnaldo Carvalho de Melo 		size += sizeof(data->time);
138681e36bffSArnaldo Carvalho de Melo 
138781e36bffSArnaldo Carvalho de Melo 	if (sample_type & PERF_SAMPLE_ID)
138881e36bffSArnaldo Carvalho de Melo 		size += sizeof(data->id);
138981e36bffSArnaldo Carvalho de Melo 
139081e36bffSArnaldo Carvalho de Melo 	if (sample_type & PERF_SAMPLE_STREAM_ID)
139181e36bffSArnaldo Carvalho de Melo 		size += sizeof(data->stream_id);
139281e36bffSArnaldo Carvalho de Melo 
139381e36bffSArnaldo Carvalho de Melo 	if (sample_type & PERF_SAMPLE_CPU)
139481e36bffSArnaldo Carvalho de Melo 		size += sizeof(data->cpu) * 2;
139575562573SAdrian Hunter 
139675562573SAdrian Hunter 	if (sample_type & PERF_SAMPLE_IDENTIFIER)
139775562573SAdrian Hunter 		size += sizeof(data->id);
139881e36bffSArnaldo Carvalho de Melo out:
139981e36bffSArnaldo Carvalho de Melo 	return size;
140081e36bffSArnaldo Carvalho de Melo }
140181e36bffSArnaldo Carvalho de Melo 
14020c21f736SArnaldo Carvalho de Melo bool perf_evlist__valid_sample_id_all(struct perf_evlist *evlist)
1403c2a70653SArnaldo Carvalho de Melo {
14040c21f736SArnaldo Carvalho de Melo 	struct perf_evsel *first = perf_evlist__first(evlist), *pos = first;
1405c2a70653SArnaldo Carvalho de Melo 
14060050f7aaSArnaldo Carvalho de Melo 	evlist__for_each_continue(evlist, pos) {
1407c2a70653SArnaldo Carvalho de Melo 		if (first->attr.sample_id_all != pos->attr.sample_id_all)
1408c2a70653SArnaldo Carvalho de Melo 			return false;
1409c2a70653SArnaldo Carvalho de Melo 	}
1410c2a70653SArnaldo Carvalho de Melo 
1411c2a70653SArnaldo Carvalho de Melo 	return true;
141274429964SFrederic Weisbecker }
141374429964SFrederic Weisbecker 
14140c21f736SArnaldo Carvalho de Melo bool perf_evlist__sample_id_all(struct perf_evlist *evlist)
141574429964SFrederic Weisbecker {
14160c21f736SArnaldo Carvalho de Melo 	struct perf_evsel *first = perf_evlist__first(evlist);
1417c2a70653SArnaldo Carvalho de Melo 	return first->attr.sample_id_all;
141874429964SFrederic Weisbecker }
141981cce8deSArnaldo Carvalho de Melo 
142081cce8deSArnaldo Carvalho de Melo void perf_evlist__set_selected(struct perf_evlist *evlist,
142181cce8deSArnaldo Carvalho de Melo 			       struct perf_evsel *evsel)
142281cce8deSArnaldo Carvalho de Melo {
142381cce8deSArnaldo Carvalho de Melo 	evlist->selected = evsel;
142481cce8deSArnaldo Carvalho de Melo }
1425727ab04eSArnaldo Carvalho de Melo 
1426a74b4b66SNamhyung Kim void perf_evlist__close(struct perf_evlist *evlist)
1427a74b4b66SNamhyung Kim {
1428a74b4b66SNamhyung Kim 	struct perf_evsel *evsel;
1429a74b4b66SNamhyung Kim 	int ncpus = cpu_map__nr(evlist->cpus);
1430a74b4b66SNamhyung Kim 	int nthreads = thread_map__nr(evlist->threads);
14318ad9219eSStephane Eranian 	int n;
1432a74b4b66SNamhyung Kim 
14338ad9219eSStephane Eranian 	evlist__for_each_reverse(evlist, evsel) {
14348ad9219eSStephane Eranian 		n = evsel->cpus ? evsel->cpus->nr : ncpus;
14358ad9219eSStephane Eranian 		perf_evsel__close(evsel, n, nthreads);
14368ad9219eSStephane Eranian 	}
1437a74b4b66SNamhyung Kim }
1438a74b4b66SNamhyung Kim 
14394112eb18SArnaldo Carvalho de Melo static int perf_evlist__create_syswide_maps(struct perf_evlist *evlist)
14404112eb18SArnaldo Carvalho de Melo {
14418c0498b6SAdrian Hunter 	struct cpu_map	  *cpus;
14428c0498b6SAdrian Hunter 	struct thread_map *threads;
14434112eb18SArnaldo Carvalho de Melo 	int err = -ENOMEM;
14444112eb18SArnaldo Carvalho de Melo 
14454112eb18SArnaldo Carvalho de Melo 	/*
14464112eb18SArnaldo Carvalho de Melo 	 * Try reading /sys/devices/system/cpu/online to get
14474112eb18SArnaldo Carvalho de Melo 	 * an all cpus map.
14484112eb18SArnaldo Carvalho de Melo 	 *
14494112eb18SArnaldo Carvalho de Melo 	 * FIXME: -ENOMEM is the best we can do here, the cpu_map
14504112eb18SArnaldo Carvalho de Melo 	 * code needs an overhaul to properly forward the
14514112eb18SArnaldo Carvalho de Melo 	 * error, and we may not want to do that fallback to a
14524112eb18SArnaldo Carvalho de Melo 	 * default cpu identity map :-\
14534112eb18SArnaldo Carvalho de Melo 	 */
14548c0498b6SAdrian Hunter 	cpus = cpu_map__new(NULL);
14558c0498b6SAdrian Hunter 	if (!cpus)
14564112eb18SArnaldo Carvalho de Melo 		goto out;
14574112eb18SArnaldo Carvalho de Melo 
14588c0498b6SAdrian Hunter 	threads = thread_map__new_dummy();
14598c0498b6SAdrian Hunter 	if (!threads)
14608c0498b6SAdrian Hunter 		goto out_put;
14614112eb18SArnaldo Carvalho de Melo 
14628c0498b6SAdrian Hunter 	perf_evlist__set_maps(evlist, cpus, threads);
14634112eb18SArnaldo Carvalho de Melo out:
14644112eb18SArnaldo Carvalho de Melo 	return err;
14658c0498b6SAdrian Hunter out_put:
14668c0498b6SAdrian Hunter 	cpu_map__put(cpus);
14674112eb18SArnaldo Carvalho de Melo 	goto out;
14684112eb18SArnaldo Carvalho de Melo }
14694112eb18SArnaldo Carvalho de Melo 
14706a4bb04cSJiri Olsa int perf_evlist__open(struct perf_evlist *evlist)
1471727ab04eSArnaldo Carvalho de Melo {
14726a4bb04cSJiri Olsa 	struct perf_evsel *evsel;
1473a74b4b66SNamhyung Kim 	int err;
1474727ab04eSArnaldo Carvalho de Melo 
14754112eb18SArnaldo Carvalho de Melo 	/*
14764112eb18SArnaldo Carvalho de Melo 	 * Default: one fd per CPU, all threads, aka systemwide
14774112eb18SArnaldo Carvalho de Melo 	 * as sys_perf_event_open(cpu = -1, thread = -1) is EINVAL
14784112eb18SArnaldo Carvalho de Melo 	 */
14794112eb18SArnaldo Carvalho de Melo 	if (evlist->threads == NULL && evlist->cpus == NULL) {
14804112eb18SArnaldo Carvalho de Melo 		err = perf_evlist__create_syswide_maps(evlist);
14814112eb18SArnaldo Carvalho de Melo 		if (err < 0)
14824112eb18SArnaldo Carvalho de Melo 			goto out_err;
14834112eb18SArnaldo Carvalho de Melo 	}
14844112eb18SArnaldo Carvalho de Melo 
1485733cd2feSAdrian Hunter 	perf_evlist__update_id_pos(evlist);
1486733cd2feSAdrian Hunter 
14870050f7aaSArnaldo Carvalho de Melo 	evlist__for_each(evlist, evsel) {
148823df7f79SAdrian Hunter 		err = perf_evsel__open(evsel, evsel->cpus, evsel->threads);
1489727ab04eSArnaldo Carvalho de Melo 		if (err < 0)
1490727ab04eSArnaldo Carvalho de Melo 			goto out_err;
1491727ab04eSArnaldo Carvalho de Melo 	}
1492727ab04eSArnaldo Carvalho de Melo 
1493727ab04eSArnaldo Carvalho de Melo 	return 0;
1494727ab04eSArnaldo Carvalho de Melo out_err:
1495a74b4b66SNamhyung Kim 	perf_evlist__close(evlist);
149641c21a68SNamhyung Kim 	errno = -err;
1497727ab04eSArnaldo Carvalho de Melo 	return err;
1498727ab04eSArnaldo Carvalho de Melo }
149935b9d88eSArnaldo Carvalho de Melo 
1500602ad878SArnaldo Carvalho de Melo int perf_evlist__prepare_workload(struct perf_evlist *evlist, struct target *target,
150155e162eaSNamhyung Kim 				  const char *argv[], bool pipe_output,
1502735f7e0bSArnaldo Carvalho de Melo 				  void (*exec_error)(int signo, siginfo_t *info, void *ucontext))
150335b9d88eSArnaldo Carvalho de Melo {
150435b9d88eSArnaldo Carvalho de Melo 	int child_ready_pipe[2], go_pipe[2];
150535b9d88eSArnaldo Carvalho de Melo 	char bf;
150635b9d88eSArnaldo Carvalho de Melo 
150735b9d88eSArnaldo Carvalho de Melo 	if (pipe(child_ready_pipe) < 0) {
150835b9d88eSArnaldo Carvalho de Melo 		perror("failed to create 'ready' pipe");
150935b9d88eSArnaldo Carvalho de Melo 		return -1;
151035b9d88eSArnaldo Carvalho de Melo 	}
151135b9d88eSArnaldo Carvalho de Melo 
151235b9d88eSArnaldo Carvalho de Melo 	if (pipe(go_pipe) < 0) {
151335b9d88eSArnaldo Carvalho de Melo 		perror("failed to create 'go' pipe");
151435b9d88eSArnaldo Carvalho de Melo 		goto out_close_ready_pipe;
151535b9d88eSArnaldo Carvalho de Melo 	}
151635b9d88eSArnaldo Carvalho de Melo 
151735b9d88eSArnaldo Carvalho de Melo 	evlist->workload.pid = fork();
151835b9d88eSArnaldo Carvalho de Melo 	if (evlist->workload.pid < 0) {
151935b9d88eSArnaldo Carvalho de Melo 		perror("failed to fork");
152035b9d88eSArnaldo Carvalho de Melo 		goto out_close_pipes;
152135b9d88eSArnaldo Carvalho de Melo 	}
152235b9d88eSArnaldo Carvalho de Melo 
152335b9d88eSArnaldo Carvalho de Melo 	if (!evlist->workload.pid) {
15245f1c4225SArnaldo Carvalho de Melo 		int ret;
15255f1c4225SArnaldo Carvalho de Melo 
1526119fa3c9SNamhyung Kim 		if (pipe_output)
152735b9d88eSArnaldo Carvalho de Melo 			dup2(2, 1);
152835b9d88eSArnaldo Carvalho de Melo 
15290817df08SDavid Ahern 		signal(SIGTERM, SIG_DFL);
15300817df08SDavid Ahern 
153135b9d88eSArnaldo Carvalho de Melo 		close(child_ready_pipe[0]);
153235b9d88eSArnaldo Carvalho de Melo 		close(go_pipe[1]);
153335b9d88eSArnaldo Carvalho de Melo 		fcntl(go_pipe[0], F_SETFD, FD_CLOEXEC);
153435b9d88eSArnaldo Carvalho de Melo 
153535b9d88eSArnaldo Carvalho de Melo 		/*
153635b9d88eSArnaldo Carvalho de Melo 		 * Tell the parent we're ready to go
153735b9d88eSArnaldo Carvalho de Melo 		 */
153835b9d88eSArnaldo Carvalho de Melo 		close(child_ready_pipe[1]);
153935b9d88eSArnaldo Carvalho de Melo 
154035b9d88eSArnaldo Carvalho de Melo 		/*
154135b9d88eSArnaldo Carvalho de Melo 		 * Wait until the parent tells us to go.
154235b9d88eSArnaldo Carvalho de Melo 		 */
15435f1c4225SArnaldo Carvalho de Melo 		ret = read(go_pipe[0], &bf, 1);
15445f1c4225SArnaldo Carvalho de Melo 		/*
15455f1c4225SArnaldo Carvalho de Melo 		 * The parent will ask for the execvp() to be performed by
15465f1c4225SArnaldo Carvalho de Melo 		 * writing exactly one byte, in workload.cork_fd, usually via
15475f1c4225SArnaldo Carvalho de Melo 		 * perf_evlist__start_workload().
15485f1c4225SArnaldo Carvalho de Melo 		 *
154920f86fc1SArnaldo Carvalho de Melo 		 * For cancelling the workload without actually running it,
15505f1c4225SArnaldo Carvalho de Melo 		 * the parent will just close workload.cork_fd, without writing
15515f1c4225SArnaldo Carvalho de Melo 		 * anything, i.e. read will return zero and we just exit()
15525f1c4225SArnaldo Carvalho de Melo 		 * here.
15535f1c4225SArnaldo Carvalho de Melo 		 */
15545f1c4225SArnaldo Carvalho de Melo 		if (ret != 1) {
15555f1c4225SArnaldo Carvalho de Melo 			if (ret == -1)
155635b9d88eSArnaldo Carvalho de Melo 				perror("unable to read pipe");
15575f1c4225SArnaldo Carvalho de Melo 			exit(ret);
15585f1c4225SArnaldo Carvalho de Melo 		}
155935b9d88eSArnaldo Carvalho de Melo 
156035b9d88eSArnaldo Carvalho de Melo 		execvp(argv[0], (char **)argv);
156135b9d88eSArnaldo Carvalho de Melo 
1562735f7e0bSArnaldo Carvalho de Melo 		if (exec_error) {
1563f33cbe72SArnaldo Carvalho de Melo 			union sigval val;
1564f33cbe72SArnaldo Carvalho de Melo 
1565f33cbe72SArnaldo Carvalho de Melo 			val.sival_int = errno;
1566f33cbe72SArnaldo Carvalho de Melo 			if (sigqueue(getppid(), SIGUSR1, val))
156735b9d88eSArnaldo Carvalho de Melo 				perror(argv[0]);
1568f33cbe72SArnaldo Carvalho de Melo 		} else
1569f33cbe72SArnaldo Carvalho de Melo 			perror(argv[0]);
157035b9d88eSArnaldo Carvalho de Melo 		exit(-1);
157135b9d88eSArnaldo Carvalho de Melo 	}
157235b9d88eSArnaldo Carvalho de Melo 
1573735f7e0bSArnaldo Carvalho de Melo 	if (exec_error) {
1574735f7e0bSArnaldo Carvalho de Melo 		struct sigaction act = {
1575735f7e0bSArnaldo Carvalho de Melo 			.sa_flags     = SA_SIGINFO,
1576735f7e0bSArnaldo Carvalho de Melo 			.sa_sigaction = exec_error,
1577735f7e0bSArnaldo Carvalho de Melo 		};
1578735f7e0bSArnaldo Carvalho de Melo 		sigaction(SIGUSR1, &act, NULL);
1579735f7e0bSArnaldo Carvalho de Melo 	}
1580735f7e0bSArnaldo Carvalho de Melo 
15811aaf63b1SArnaldo Carvalho de Melo 	if (target__none(target)) {
15821aaf63b1SArnaldo Carvalho de Melo 		if (evlist->threads == NULL) {
15831aaf63b1SArnaldo Carvalho de Melo 			fprintf(stderr, "FATAL: evlist->threads need to be set at this point (%s:%d).\n",
15841aaf63b1SArnaldo Carvalho de Melo 				__func__, __LINE__);
15851aaf63b1SArnaldo Carvalho de Melo 			goto out_close_pipes;
15861aaf63b1SArnaldo Carvalho de Melo 		}
1587e13798c7SJiri Olsa 		thread_map__set_pid(evlist->threads, 0, evlist->workload.pid);
15881aaf63b1SArnaldo Carvalho de Melo 	}
158935b9d88eSArnaldo Carvalho de Melo 
159035b9d88eSArnaldo Carvalho de Melo 	close(child_ready_pipe[1]);
159135b9d88eSArnaldo Carvalho de Melo 	close(go_pipe[0]);
159235b9d88eSArnaldo Carvalho de Melo 	/*
159335b9d88eSArnaldo Carvalho de Melo 	 * wait for child to settle
159435b9d88eSArnaldo Carvalho de Melo 	 */
159535b9d88eSArnaldo Carvalho de Melo 	if (read(child_ready_pipe[0], &bf, 1) == -1) {
159635b9d88eSArnaldo Carvalho de Melo 		perror("unable to read pipe");
159735b9d88eSArnaldo Carvalho de Melo 		goto out_close_pipes;
159835b9d88eSArnaldo Carvalho de Melo 	}
159935b9d88eSArnaldo Carvalho de Melo 
1600bcf3145fSNamhyung Kim 	fcntl(go_pipe[1], F_SETFD, FD_CLOEXEC);
160135b9d88eSArnaldo Carvalho de Melo 	evlist->workload.cork_fd = go_pipe[1];
160235b9d88eSArnaldo Carvalho de Melo 	close(child_ready_pipe[0]);
160335b9d88eSArnaldo Carvalho de Melo 	return 0;
160435b9d88eSArnaldo Carvalho de Melo 
160535b9d88eSArnaldo Carvalho de Melo out_close_pipes:
160635b9d88eSArnaldo Carvalho de Melo 	close(go_pipe[0]);
160735b9d88eSArnaldo Carvalho de Melo 	close(go_pipe[1]);
160835b9d88eSArnaldo Carvalho de Melo out_close_ready_pipe:
160935b9d88eSArnaldo Carvalho de Melo 	close(child_ready_pipe[0]);
161035b9d88eSArnaldo Carvalho de Melo 	close(child_ready_pipe[1]);
161135b9d88eSArnaldo Carvalho de Melo 	return -1;
161235b9d88eSArnaldo Carvalho de Melo }
161335b9d88eSArnaldo Carvalho de Melo 
161435b9d88eSArnaldo Carvalho de Melo int perf_evlist__start_workload(struct perf_evlist *evlist)
161535b9d88eSArnaldo Carvalho de Melo {
161635b9d88eSArnaldo Carvalho de Melo 	if (evlist->workload.cork_fd > 0) {
1617b3824404SDavid Ahern 		char bf = 0;
1618bcf3145fSNamhyung Kim 		int ret;
161935b9d88eSArnaldo Carvalho de Melo 		/*
162035b9d88eSArnaldo Carvalho de Melo 		 * Remove the cork, let it rip!
162135b9d88eSArnaldo Carvalho de Melo 		 */
1622bcf3145fSNamhyung Kim 		ret = write(evlist->workload.cork_fd, &bf, 1);
1623bcf3145fSNamhyung Kim 		if (ret < 0)
1624bcf3145fSNamhyung Kim 			perror("enable to write to pipe");
1625bcf3145fSNamhyung Kim 
1626bcf3145fSNamhyung Kim 		close(evlist->workload.cork_fd);
1627bcf3145fSNamhyung Kim 		return ret;
162835b9d88eSArnaldo Carvalho de Melo 	}
162935b9d88eSArnaldo Carvalho de Melo 
163035b9d88eSArnaldo Carvalho de Melo 	return 0;
163135b9d88eSArnaldo Carvalho de Melo }
1632cb0b29e0SArnaldo Carvalho de Melo 
1633a3f698feSArnaldo Carvalho de Melo int perf_evlist__parse_sample(struct perf_evlist *evlist, union perf_event *event,
16340807d2d8SArnaldo Carvalho de Melo 			      struct perf_sample *sample)
1635cb0b29e0SArnaldo Carvalho de Melo {
163675562573SAdrian Hunter 	struct perf_evsel *evsel = perf_evlist__event2evsel(evlist, event);
163775562573SAdrian Hunter 
163875562573SAdrian Hunter 	if (!evsel)
163975562573SAdrian Hunter 		return -EFAULT;
16400807d2d8SArnaldo Carvalho de Melo 	return perf_evsel__parse_sample(evsel, event, sample);
1641cb0b29e0SArnaldo Carvalho de Melo }
164278f067b3SArnaldo Carvalho de Melo 
164378f067b3SArnaldo Carvalho de Melo size_t perf_evlist__fprintf(struct perf_evlist *evlist, FILE *fp)
164478f067b3SArnaldo Carvalho de Melo {
164578f067b3SArnaldo Carvalho de Melo 	struct perf_evsel *evsel;
164678f067b3SArnaldo Carvalho de Melo 	size_t printed = 0;
164778f067b3SArnaldo Carvalho de Melo 
16480050f7aaSArnaldo Carvalho de Melo 	evlist__for_each(evlist, evsel) {
164978f067b3SArnaldo Carvalho de Melo 		printed += fprintf(fp, "%s%s", evsel->idx ? ", " : "",
165078f067b3SArnaldo Carvalho de Melo 				   perf_evsel__name(evsel));
165178f067b3SArnaldo Carvalho de Melo 	}
165278f067b3SArnaldo Carvalho de Melo 
1653b2222139SDavidlohr Bueso 	return printed + fprintf(fp, "\n");
165478f067b3SArnaldo Carvalho de Melo }
16556ef068cbSArnaldo Carvalho de Melo 
1656d9aade7fSArnaldo Carvalho de Melo int perf_evlist__strerror_open(struct perf_evlist *evlist,
1657a8f23d8fSArnaldo Carvalho de Melo 			       int err, char *buf, size_t size)
1658a8f23d8fSArnaldo Carvalho de Melo {
1659a8f23d8fSArnaldo Carvalho de Melo 	int printed, value;
16606e81c74cSMasami Hiramatsu 	char sbuf[STRERR_BUFSIZE], *emsg = strerror_r(err, sbuf, sizeof(sbuf));
1661a8f23d8fSArnaldo Carvalho de Melo 
1662a8f23d8fSArnaldo Carvalho de Melo 	switch (err) {
1663a8f23d8fSArnaldo Carvalho de Melo 	case EACCES:
1664a8f23d8fSArnaldo Carvalho de Melo 	case EPERM:
1665a8f23d8fSArnaldo Carvalho de Melo 		printed = scnprintf(buf, size,
1666a8f23d8fSArnaldo Carvalho de Melo 				    "Error:\t%s.\n"
1667a8f23d8fSArnaldo Carvalho de Melo 				    "Hint:\tCheck /proc/sys/kernel/perf_event_paranoid setting.", emsg);
1668a8f23d8fSArnaldo Carvalho de Melo 
16691a47245dSAdrian Hunter 		value = perf_event_paranoid();
1670a8f23d8fSArnaldo Carvalho de Melo 
1671a8f23d8fSArnaldo Carvalho de Melo 		printed += scnprintf(buf + printed, size - printed, "\nHint:\t");
1672a8f23d8fSArnaldo Carvalho de Melo 
1673a8f23d8fSArnaldo Carvalho de Melo 		if (value >= 2) {
1674a8f23d8fSArnaldo Carvalho de Melo 			printed += scnprintf(buf + printed, size - printed,
1675a8f23d8fSArnaldo Carvalho de Melo 					     "For your workloads it needs to be <= 1\nHint:\t");
1676a8f23d8fSArnaldo Carvalho de Melo 		}
1677a8f23d8fSArnaldo Carvalho de Melo 		printed += scnprintf(buf + printed, size - printed,
16785229e366SArnaldo Carvalho de Melo 				     "For system wide tracing it needs to be set to -1.\n");
1679a8f23d8fSArnaldo Carvalho de Melo 
1680a8f23d8fSArnaldo Carvalho de Melo 		printed += scnprintf(buf + printed, size - printed,
16815229e366SArnaldo Carvalho de Melo 				    "Hint:\tTry: 'sudo sh -c \"echo -1 > /proc/sys/kernel/perf_event_paranoid\"'\n"
16825229e366SArnaldo Carvalho de Melo 				    "Hint:\tThe current value is %d.", value);
1683a8f23d8fSArnaldo Carvalho de Melo 		break;
1684d9aade7fSArnaldo Carvalho de Melo 	case EINVAL: {
1685d9aade7fSArnaldo Carvalho de Melo 		struct perf_evsel *first = perf_evlist__first(evlist);
1686d9aade7fSArnaldo Carvalho de Melo 		int max_freq;
1687d9aade7fSArnaldo Carvalho de Melo 
1688d9aade7fSArnaldo Carvalho de Melo 		if (sysctl__read_int("kernel/perf_event_max_sample_rate", &max_freq) < 0)
1689d9aade7fSArnaldo Carvalho de Melo 			goto out_default;
1690d9aade7fSArnaldo Carvalho de Melo 
1691d9aade7fSArnaldo Carvalho de Melo 		if (first->attr.sample_freq < (u64)max_freq)
1692d9aade7fSArnaldo Carvalho de Melo 			goto out_default;
1693d9aade7fSArnaldo Carvalho de Melo 
1694d9aade7fSArnaldo Carvalho de Melo 		printed = scnprintf(buf, size,
1695d9aade7fSArnaldo Carvalho de Melo 				    "Error:\t%s.\n"
1696d9aade7fSArnaldo Carvalho de Melo 				    "Hint:\tCheck /proc/sys/kernel/perf_event_max_sample_rate.\n"
1697d9aade7fSArnaldo Carvalho de Melo 				    "Hint:\tThe current value is %d and %" PRIu64 " is being requested.",
1698d9aade7fSArnaldo Carvalho de Melo 				    emsg, max_freq, first->attr.sample_freq);
1699d9aade7fSArnaldo Carvalho de Melo 		break;
1700d9aade7fSArnaldo Carvalho de Melo 	}
1701a8f23d8fSArnaldo Carvalho de Melo 	default:
1702d9aade7fSArnaldo Carvalho de Melo out_default:
1703a8f23d8fSArnaldo Carvalho de Melo 		scnprintf(buf, size, "%s", emsg);
1704a8f23d8fSArnaldo Carvalho de Melo 		break;
1705a8f23d8fSArnaldo Carvalho de Melo 	}
1706a8f23d8fSArnaldo Carvalho de Melo 
1707a8f23d8fSArnaldo Carvalho de Melo 	return 0;
1708a8f23d8fSArnaldo Carvalho de Melo }
1709a025e4f0SAdrian Hunter 
1710956fa571SArnaldo Carvalho de Melo int perf_evlist__strerror_mmap(struct perf_evlist *evlist, int err, char *buf, size_t size)
1711956fa571SArnaldo Carvalho de Melo {
1712956fa571SArnaldo Carvalho de Melo 	char sbuf[STRERR_BUFSIZE], *emsg = strerror_r(err, sbuf, sizeof(sbuf));
1713e965bea1SArnaldo Carvalho de Melo 	int pages_attempted = evlist->mmap_len / 1024, pages_max_per_user, printed = 0;
1714956fa571SArnaldo Carvalho de Melo 
1715956fa571SArnaldo Carvalho de Melo 	switch (err) {
1716956fa571SArnaldo Carvalho de Melo 	case EPERM:
1717e5d4a290SArnaldo Carvalho de Melo 		sysctl__read_int("kernel/perf_event_mlock_kb", &pages_max_per_user);
1718e965bea1SArnaldo Carvalho de Melo 		printed += scnprintf(buf + printed, size - printed,
1719e965bea1SArnaldo Carvalho de Melo 				     "Error:\t%s.\n"
1720956fa571SArnaldo Carvalho de Melo 				     "Hint:\tCheck /proc/sys/kernel/perf_event_mlock_kb (%d kB) setting.\n"
1721e965bea1SArnaldo Carvalho de Melo 				     "Hint:\tTried using %zd kB.\n",
1722e5d4a290SArnaldo Carvalho de Melo 				     emsg, pages_max_per_user, pages_attempted);
1723e965bea1SArnaldo Carvalho de Melo 
1724e965bea1SArnaldo Carvalho de Melo 		if (pages_attempted >= pages_max_per_user) {
1725e965bea1SArnaldo Carvalho de Melo 			printed += scnprintf(buf + printed, size - printed,
1726e965bea1SArnaldo Carvalho de Melo 					     "Hint:\tTry 'sudo sh -c \"echo %d > /proc/sys/kernel/perf_event_mlock_kb\"', or\n",
1727e965bea1SArnaldo Carvalho de Melo 					     pages_max_per_user + pages_attempted);
1728e965bea1SArnaldo Carvalho de Melo 		}
1729e965bea1SArnaldo Carvalho de Melo 
1730e965bea1SArnaldo Carvalho de Melo 		printed += scnprintf(buf + printed, size - printed,
1731e965bea1SArnaldo Carvalho de Melo 				     "Hint:\tTry using a smaller -m/--mmap-pages value.");
1732956fa571SArnaldo Carvalho de Melo 		break;
1733956fa571SArnaldo Carvalho de Melo 	default:
1734956fa571SArnaldo Carvalho de Melo 		scnprintf(buf, size, "%s", emsg);
1735956fa571SArnaldo Carvalho de Melo 		break;
1736956fa571SArnaldo Carvalho de Melo 	}
1737956fa571SArnaldo Carvalho de Melo 
1738956fa571SArnaldo Carvalho de Melo 	return 0;
1739956fa571SArnaldo Carvalho de Melo }
1740956fa571SArnaldo Carvalho de Melo 
1741a025e4f0SAdrian Hunter void perf_evlist__to_front(struct perf_evlist *evlist,
1742a025e4f0SAdrian Hunter 			   struct perf_evsel *move_evsel)
1743a025e4f0SAdrian Hunter {
1744a025e4f0SAdrian Hunter 	struct perf_evsel *evsel, *n;
1745a025e4f0SAdrian Hunter 	LIST_HEAD(move);
1746a025e4f0SAdrian Hunter 
1747a025e4f0SAdrian Hunter 	if (move_evsel == perf_evlist__first(evlist))
1748a025e4f0SAdrian Hunter 		return;
1749a025e4f0SAdrian Hunter 
17500050f7aaSArnaldo Carvalho de Melo 	evlist__for_each_safe(evlist, n, evsel) {
1751a025e4f0SAdrian Hunter 		if (evsel->leader == move_evsel->leader)
1752a025e4f0SAdrian Hunter 			list_move_tail(&evsel->node, &move);
1753a025e4f0SAdrian Hunter 	}
1754a025e4f0SAdrian Hunter 
1755a025e4f0SAdrian Hunter 	list_splice(&move, &evlist->entries);
1756a025e4f0SAdrian Hunter }
175760b0896cSAdrian Hunter 
175860b0896cSAdrian Hunter void perf_evlist__set_tracking_event(struct perf_evlist *evlist,
175960b0896cSAdrian Hunter 				     struct perf_evsel *tracking_evsel)
176060b0896cSAdrian Hunter {
176160b0896cSAdrian Hunter 	struct perf_evsel *evsel;
176260b0896cSAdrian Hunter 
176360b0896cSAdrian Hunter 	if (tracking_evsel->tracking)
176460b0896cSAdrian Hunter 		return;
176560b0896cSAdrian Hunter 
176660b0896cSAdrian Hunter 	evlist__for_each(evlist, evsel) {
176760b0896cSAdrian Hunter 		if (evsel != tracking_evsel)
176860b0896cSAdrian Hunter 			evsel->tracking = false;
176960b0896cSAdrian Hunter 	}
177060b0896cSAdrian Hunter 
177160b0896cSAdrian Hunter 	tracking_evsel->tracking = true;
177260b0896cSAdrian Hunter }
17737630b3e2SWang Nan 
17747630b3e2SWang Nan struct perf_evsel *
17757630b3e2SWang Nan perf_evlist__find_evsel_by_str(struct perf_evlist *evlist,
17767630b3e2SWang Nan 			       const char *str)
17777630b3e2SWang Nan {
17787630b3e2SWang Nan 	struct perf_evsel *evsel;
17797630b3e2SWang Nan 
17807630b3e2SWang Nan 	evlist__for_each(evlist, evsel) {
17817630b3e2SWang Nan 		if (!evsel->name)
17827630b3e2SWang Nan 			continue;
17837630b3e2SWang Nan 		if (strcmp(str, evsel->name) == 0)
17847630b3e2SWang Nan 			return evsel;
17857630b3e2SWang Nan 	}
17867630b3e2SWang Nan 
17877630b3e2SWang Nan 	return NULL;
17887630b3e2SWang Nan }
1789