xref: /openbmc/linux/tools/perf/util/evlist.c (revision d20aff15)
191007045SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2f8a95309SArnaldo Carvalho de Melo /*
3f8a95309SArnaldo Carvalho de Melo  * Copyright (C) 2011, Red Hat Inc, Arnaldo Carvalho de Melo <acme@redhat.com>
4f8a95309SArnaldo Carvalho de Melo  *
5f8a95309SArnaldo Carvalho de Melo  * Parts came from builtin-{top,stat,record}.c, see those files for further
6f8a95309SArnaldo Carvalho de Melo  * copyright notes.
7f8a95309SArnaldo Carvalho de Melo  */
8956fa571SArnaldo Carvalho de Melo #include <api/fs/fs.h>
9a43783aeSArnaldo Carvalho de Melo #include <errno.h>
10fd20e811SArnaldo Carvalho de Melo #include <inttypes.h>
115c581041SArnaldo Carvalho de Melo #include <poll.h>
12f8a95309SArnaldo Carvalho de Melo #include "cpumap.h"
13e0fcfb08SArnaldo Carvalho de Melo #include "util/mmap.h"
14f8a95309SArnaldo Carvalho de Melo #include "thread_map.h"
1512864b31SNamhyung Kim #include "target.h"
16361c99a6SArnaldo Carvalho de Melo #include "evlist.h"
17361c99a6SArnaldo Carvalho de Melo #include "evsel.h"
18e3e1a54fSAdrian Hunter #include "debug.h"
1958db1d6eSArnaldo Carvalho de Melo #include "units.h"
2020f2be1dSJiri Olsa #include <internal/lib.h> // page_size
217736627bSAndi Kleen #include "affinity.h"
22c1a604dfSArnaldo Carvalho de Melo #include "../perf.h"
2354cc54deSWang Nan #include "asm/bug.h"
24657ee553SSong Liu #include "bpf-event.h"
25da949f50SArnaldo Carvalho de Melo #include "util/string2.h"
2640c7d246SArnaldo Carvalho de Melo #include "util/perf_api_probe.h"
279607ad3aSArnaldo Carvalho de Melo #include <signal.h>
2835b9d88eSArnaldo Carvalho de Melo #include <unistd.h>
29b397f846SArnaldo Carvalho de Melo #include <sched.h>
30f2a39fe8SArnaldo Carvalho de Melo #include <stdlib.h>
31361c99a6SArnaldo Carvalho de Melo 
3250d08e47SArnaldo Carvalho de Melo #include "parse-events.h"
334b6ab94eSJosh Poimboeuf #include <subcmd/parse-options.h>
3450d08e47SArnaldo Carvalho de Melo 
35bafae98eSArnaldo Carvalho de Melo #include <fcntl.h>
3686a5e0c2SArnaldo Carvalho de Melo #include <sys/ioctl.h>
37f8a95309SArnaldo Carvalho de Melo #include <sys/mman.h>
38f8a95309SArnaldo Carvalho de Melo 
3970db7533SArnaldo Carvalho de Melo #include <linux/bitops.h>
4070db7533SArnaldo Carvalho de Melo #include <linux/hash.h>
410389cd1fSArnaldo Carvalho de Melo #include <linux/log2.h>
428dd2a131SJiri Olsa #include <linux/err.h>
438520a98dSArnaldo Carvalho de Melo #include <linux/string.h>
447f7c536fSArnaldo Carvalho de Melo #include <linux/zalloc.h>
454562a739SJiri Olsa #include <perf/evlist.h>
4688761fa1SJiri Olsa #include <perf/evsel.h>
479c3516d1SJiri Olsa #include <perf/cpumap.h>
487728fa0cSJiri Olsa #include <perf/mmap.h>
4970db7533SArnaldo Carvalho de Melo 
50e14e5497SArnaldo Carvalho de Melo #include <internal/xyarray.h>
51e14e5497SArnaldo Carvalho de Melo 
52748fe088SArnaldo Carvalho de Melo #ifdef LACKS_SIGQUEUE_PROTOTYPE
53748fe088SArnaldo Carvalho de Melo int sigqueue(pid_t pid, int sig, const union sigval value);
54748fe088SArnaldo Carvalho de Melo #endif
55748fe088SArnaldo Carvalho de Melo 
569dfcb759SJiri Olsa #define FD(e, x, y) (*(int *)xyarray__entry(e->core.fd, x, y))
578cd36f3eSJiri Olsa #define SID(e, x, y) xyarray__entry(e->core.sample_id, x, y)
58f8a95309SArnaldo Carvalho de Melo 
5952c86bcaSJiri Olsa void evlist__init(struct evlist *evlist, struct perf_cpu_map *cpus,
609749b90eSJiri Olsa 		  struct perf_thread_map *threads)
61361c99a6SArnaldo Carvalho de Melo {
624562a739SJiri Olsa 	perf_evlist__init(&evlist->core);
63453fa030SJiri Olsa 	perf_evlist__set_maps(&evlist->core, cpus, threads);
6435b9d88eSArnaldo Carvalho de Melo 	evlist->workload.pid = -1;
6554cc54deSWang Nan 	evlist->bkw_mmap_state = BKW_MMAP_NOTREADY;
668ab705b5SAlexey Budankov 	evlist->ctl_fd.fd = -1;
678ab705b5SAlexey Budankov 	evlist->ctl_fd.ack = -1;
688ab705b5SAlexey Budankov 	evlist->ctl_fd.pos = -1;
69361c99a6SArnaldo Carvalho de Melo }
70361c99a6SArnaldo Carvalho de Melo 
710f98b11cSJiri Olsa struct evlist *evlist__new(void)
72ef1d1af2SArnaldo Carvalho de Melo {
7363503dbaSJiri Olsa 	struct evlist *evlist = zalloc(sizeof(*evlist));
74ef1d1af2SArnaldo Carvalho de Melo 
75ef1d1af2SArnaldo Carvalho de Melo 	if (evlist != NULL)
7652c86bcaSJiri Olsa 		evlist__init(evlist, NULL, NULL);
77ef1d1af2SArnaldo Carvalho de Melo 
78361c99a6SArnaldo Carvalho de Melo 	return evlist;
79361c99a6SArnaldo Carvalho de Melo }
80361c99a6SArnaldo Carvalho de Melo 
8163503dbaSJiri Olsa struct evlist *perf_evlist__new_default(void)
82b22d54b0SJiri Olsa {
830f98b11cSJiri Olsa 	struct evlist *evlist = evlist__new();
84b22d54b0SJiri Olsa 
85e251abeeSArnaldo Carvalho de Melo 	if (evlist && evlist__add_default(evlist)) {
86c12995a5SJiri Olsa 		evlist__delete(evlist);
87b22d54b0SJiri Olsa 		evlist = NULL;
88b22d54b0SJiri Olsa 	}
89b22d54b0SJiri Olsa 
90b22d54b0SJiri Olsa 	return evlist;
91b22d54b0SJiri Olsa }
92b22d54b0SJiri Olsa 
9363503dbaSJiri Olsa struct evlist *perf_evlist__new_dummy(void)
945bae0250SArnaldo Carvalho de Melo {
950f98b11cSJiri Olsa 	struct evlist *evlist = evlist__new();
965bae0250SArnaldo Carvalho de Melo 
97e251abeeSArnaldo Carvalho de Melo 	if (evlist && evlist__add_dummy(evlist)) {
98c12995a5SJiri Olsa 		evlist__delete(evlist);
995bae0250SArnaldo Carvalho de Melo 		evlist = NULL;
1005bae0250SArnaldo Carvalho de Melo 	}
1015bae0250SArnaldo Carvalho de Melo 
1025bae0250SArnaldo Carvalho de Melo 	return evlist;
1035bae0250SArnaldo Carvalho de Melo }
1045bae0250SArnaldo Carvalho de Melo 
10575562573SAdrian Hunter /**
10675562573SAdrian Hunter  * perf_evlist__set_id_pos - set the positions of event ids.
10775562573SAdrian Hunter  * @evlist: selected event list
10875562573SAdrian Hunter  *
10975562573SAdrian Hunter  * Events with compatible sample types all have the same id_pos
11075562573SAdrian Hunter  * and is_pos.  For convenience, put a copy on evlist.
11175562573SAdrian Hunter  */
11263503dbaSJiri Olsa void perf_evlist__set_id_pos(struct evlist *evlist)
11375562573SAdrian Hunter {
114515dbe48SJiri Olsa 	struct evsel *first = evlist__first(evlist);
11575562573SAdrian Hunter 
11675562573SAdrian Hunter 	evlist->id_pos = first->id_pos;
11775562573SAdrian Hunter 	evlist->is_pos = first->is_pos;
11875562573SAdrian Hunter }
11975562573SAdrian Hunter 
12063503dbaSJiri Olsa static void perf_evlist__update_id_pos(struct evlist *evlist)
121733cd2feSAdrian Hunter {
12232dcd021SJiri Olsa 	struct evsel *evsel;
123733cd2feSAdrian Hunter 
124e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, evsel)
1254b5e87b7SArnaldo Carvalho de Melo 		evsel__calc_id_pos(evsel);
126733cd2feSAdrian Hunter 
127733cd2feSAdrian Hunter 	perf_evlist__set_id_pos(evlist);
128733cd2feSAdrian Hunter }
129733cd2feSAdrian Hunter 
130e6b1878dSJiri Olsa static void evlist__purge(struct evlist *evlist)
131361c99a6SArnaldo Carvalho de Melo {
13232dcd021SJiri Olsa 	struct evsel *pos, *n;
133361c99a6SArnaldo Carvalho de Melo 
134e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry_safe(evlist, n, pos) {
135b27c4eceSJiri Olsa 		list_del_init(&pos->core.node);
136d49e4695SArnaldo Carvalho de Melo 		pos->evlist = NULL;
1375eb2dd2aSJiri Olsa 		evsel__delete(pos);
138361c99a6SArnaldo Carvalho de Melo 	}
139361c99a6SArnaldo Carvalho de Melo 
1406484d2f9SJiri Olsa 	evlist->core.nr_entries = 0;
141361c99a6SArnaldo Carvalho de Melo }
142361c99a6SArnaldo Carvalho de Melo 
143470579b0SJiri Olsa void evlist__exit(struct evlist *evlist)
144ef1d1af2SArnaldo Carvalho de Melo {
14504662523SArnaldo Carvalho de Melo 	zfree(&evlist->mmap);
1460b72d69aSWang Nan 	zfree(&evlist->overwrite_mmap);
14793dd6e28SJiri Olsa 	perf_evlist__exit(&evlist->core);
148ef1d1af2SArnaldo Carvalho de Melo }
149ef1d1af2SArnaldo Carvalho de Melo 
150c12995a5SJiri Olsa void evlist__delete(struct evlist *evlist)
151361c99a6SArnaldo Carvalho de Melo {
1520b04b3dcSArnaldo Carvalho de Melo 	if (evlist == NULL)
1530b04b3dcSArnaldo Carvalho de Melo 		return;
1540b04b3dcSArnaldo Carvalho de Melo 
155db6b7b13SJiri Olsa 	evlist__munmap(evlist);
156750b4edeSJiri Olsa 	evlist__close(evlist);
157e6b1878dSJiri Olsa 	evlist__purge(evlist);
158470579b0SJiri Olsa 	evlist__exit(evlist);
159361c99a6SArnaldo Carvalho de Melo 	free(evlist);
160361c99a6SArnaldo Carvalho de Melo }
161361c99a6SArnaldo Carvalho de Melo 
162a1cf3a75SJiri Olsa void evlist__add(struct evlist *evlist, struct evsel *entry)
163361c99a6SArnaldo Carvalho de Melo {
164d49e4695SArnaldo Carvalho de Melo 	entry->evlist = evlist;
1656484d2f9SJiri Olsa 	entry->idx = evlist->core.nr_entries;
16660b0896cSAdrian Hunter 	entry->tracking = !entry->idx;
167ef503831SArnaldo Carvalho de Melo 
1686484d2f9SJiri Olsa 	perf_evlist__add(&evlist->core, &entry->core);
1696484d2f9SJiri Olsa 
1706484d2f9SJiri Olsa 	if (evlist->core.nr_entries == 1)
17175562573SAdrian Hunter 		perf_evlist__set_id_pos(evlist);
172361c99a6SArnaldo Carvalho de Melo }
173361c99a6SArnaldo Carvalho de Melo 
17416251027SJiri Olsa void evlist__remove(struct evlist *evlist, struct evsel *evsel)
1754768230aSAdrian Hunter {
1764768230aSAdrian Hunter 	evsel->evlist = NULL;
17752e22fb8SJiri Olsa 	perf_evlist__remove(&evlist->core, &evsel->core);
1784768230aSAdrian Hunter }
1794768230aSAdrian Hunter 
18063503dbaSJiri Olsa void perf_evlist__splice_list_tail(struct evlist *evlist,
181f114d6efSAdrian Hunter 				   struct list_head *list)
18250d08e47SArnaldo Carvalho de Melo {
18332dcd021SJiri Olsa 	struct evsel *evsel, *temp;
18475562573SAdrian Hunter 
185e5cadb93SArnaldo Carvalho de Melo 	__evlist__for_each_entry_safe(list, temp, evsel) {
186b27c4eceSJiri Olsa 		list_del_init(&evsel->core.node);
187a1cf3a75SJiri Olsa 		evlist__add(evlist, evsel);
188f114d6efSAdrian Hunter 	}
18950d08e47SArnaldo Carvalho de Melo }
19050d08e47SArnaldo Carvalho de Melo 
191c0e53476SArnaldo Carvalho de Melo int __evlist__set_tracepoints_handlers(struct evlist *evlist,
192c0e53476SArnaldo Carvalho de Melo 				       const struct evsel_str_handler *assocs, size_t nr_assocs)
193c0e53476SArnaldo Carvalho de Melo {
194c0e53476SArnaldo Carvalho de Melo 	struct evsel *evsel;
195c0e53476SArnaldo Carvalho de Melo 	size_t i;
196c0e53476SArnaldo Carvalho de Melo 	int err;
197c0e53476SArnaldo Carvalho de Melo 
198c0e53476SArnaldo Carvalho de Melo 	for (i = 0; i < nr_assocs; i++) {
199c0e53476SArnaldo Carvalho de Melo 		// Adding a handler for an event not in this evlist, just ignore it.
200c0e53476SArnaldo Carvalho de Melo 		evsel = perf_evlist__find_tracepoint_by_name(evlist, assocs[i].name);
201c0e53476SArnaldo Carvalho de Melo 		if (evsel == NULL)
202c0e53476SArnaldo Carvalho de Melo 			continue;
203c0e53476SArnaldo Carvalho de Melo 
204c0e53476SArnaldo Carvalho de Melo 		err = -EEXIST;
205c0e53476SArnaldo Carvalho de Melo 		if (evsel->handler != NULL)
206c0e53476SArnaldo Carvalho de Melo 			goto out;
207c0e53476SArnaldo Carvalho de Melo 		evsel->handler = assocs[i].handler;
208c0e53476SArnaldo Carvalho de Melo 	}
209c0e53476SArnaldo Carvalho de Melo 
210c0e53476SArnaldo Carvalho de Melo 	err = 0;
211c0e53476SArnaldo Carvalho de Melo out:
212c0e53476SArnaldo Carvalho de Melo 	return err;
213c0e53476SArnaldo Carvalho de Melo }
214c0e53476SArnaldo Carvalho de Melo 
21563dab225SArnaldo Carvalho de Melo void __perf_evlist__set_leader(struct list_head *list)
21663dab225SArnaldo Carvalho de Melo {
21732dcd021SJiri Olsa 	struct evsel *evsel, *leader;
21863dab225SArnaldo Carvalho de Melo 
219b27c4eceSJiri Olsa 	leader = list_entry(list->next, struct evsel, core.node);
220b27c4eceSJiri Olsa 	evsel = list_entry(list->prev, struct evsel, core.node);
22197f63e4aSNamhyung Kim 
2225643b1a5SJiri Olsa 	leader->core.nr_members = evsel->idx - leader->idx + 1;
22363dab225SArnaldo Carvalho de Melo 
224e5cadb93SArnaldo Carvalho de Melo 	__evlist__for_each_entry(list, evsel) {
22563dab225SArnaldo Carvalho de Melo 		evsel->leader = leader;
22663dab225SArnaldo Carvalho de Melo 	}
22763dab225SArnaldo Carvalho de Melo }
22863dab225SArnaldo Carvalho de Melo 
22963503dbaSJiri Olsa void perf_evlist__set_leader(struct evlist *evlist)
2306a4bb04cSJiri Olsa {
2316484d2f9SJiri Olsa 	if (evlist->core.nr_entries) {
2326484d2f9SJiri Olsa 		evlist->nr_groups = evlist->core.nr_entries > 1 ? 1 : 0;
233ce9036a6SJiri Olsa 		__perf_evlist__set_leader(&evlist->core.entries);
2346a4bb04cSJiri Olsa 	}
23597f63e4aSNamhyung Kim }
2366a4bb04cSJiri Olsa 
237e251abeeSArnaldo Carvalho de Melo int __evlist__add_default(struct evlist *evlist, bool precise)
238361c99a6SArnaldo Carvalho de Melo {
2398f6725a2SArnaldo Carvalho de Melo 	struct evsel *evsel = evsel__new_cycles(precise);
240361c99a6SArnaldo Carvalho de Melo 
241361c99a6SArnaldo Carvalho de Melo 	if (evsel == NULL)
2427c48dcfdSArnaldo Carvalho de Melo 		return -ENOMEM;
243361c99a6SArnaldo Carvalho de Melo 
244a1cf3a75SJiri Olsa 	evlist__add(evlist, evsel);
245361c99a6SArnaldo Carvalho de Melo 	return 0;
246361c99a6SArnaldo Carvalho de Melo }
2475c581041SArnaldo Carvalho de Melo 
248e251abeeSArnaldo Carvalho de Melo int evlist__add_dummy(struct evlist *evlist)
2495bae0250SArnaldo Carvalho de Melo {
2505bae0250SArnaldo Carvalho de Melo 	struct perf_event_attr attr = {
2515bae0250SArnaldo Carvalho de Melo 		.type	= PERF_TYPE_SOFTWARE,
2525bae0250SArnaldo Carvalho de Melo 		.config = PERF_COUNT_SW_DUMMY,
2535bae0250SArnaldo Carvalho de Melo 		.size	= sizeof(attr), /* to capture ABI version */
2545bae0250SArnaldo Carvalho de Melo 	};
2558f6725a2SArnaldo Carvalho de Melo 	struct evsel *evsel = evsel__new_idx(&attr, evlist->core.nr_entries);
2565bae0250SArnaldo Carvalho de Melo 
2575bae0250SArnaldo Carvalho de Melo 	if (evsel == NULL)
2585bae0250SArnaldo Carvalho de Melo 		return -ENOMEM;
2595bae0250SArnaldo Carvalho de Melo 
260a1cf3a75SJiri Olsa 	evlist__add(evlist, evsel);
2615bae0250SArnaldo Carvalho de Melo 	return 0;
2625bae0250SArnaldo Carvalho de Melo }
2635bae0250SArnaldo Carvalho de Melo 
264e251abeeSArnaldo Carvalho de Melo static int evlist__add_attrs(struct evlist *evlist, struct perf_event_attr *attrs, size_t nr_attrs)
26550d08e47SArnaldo Carvalho de Melo {
26632dcd021SJiri Olsa 	struct evsel *evsel, *n;
26750d08e47SArnaldo Carvalho de Melo 	LIST_HEAD(head);
26850d08e47SArnaldo Carvalho de Melo 	size_t i;
26950d08e47SArnaldo Carvalho de Melo 
27050d08e47SArnaldo Carvalho de Melo 	for (i = 0; i < nr_attrs; i++) {
2718f6725a2SArnaldo Carvalho de Melo 		evsel = evsel__new_idx(attrs + i, evlist->core.nr_entries + i);
27250d08e47SArnaldo Carvalho de Melo 		if (evsel == NULL)
27350d08e47SArnaldo Carvalho de Melo 			goto out_delete_partial_list;
274b27c4eceSJiri Olsa 		list_add_tail(&evsel->core.node, &head);
27550d08e47SArnaldo Carvalho de Melo 	}
27650d08e47SArnaldo Carvalho de Melo 
277f114d6efSAdrian Hunter 	perf_evlist__splice_list_tail(evlist, &head);
27850d08e47SArnaldo Carvalho de Melo 
27950d08e47SArnaldo Carvalho de Melo 	return 0;
28050d08e47SArnaldo Carvalho de Melo 
28150d08e47SArnaldo Carvalho de Melo out_delete_partial_list:
282e5cadb93SArnaldo Carvalho de Melo 	__evlist__for_each_entry_safe(&head, n, evsel)
2835eb2dd2aSJiri Olsa 		evsel__delete(evsel);
28450d08e47SArnaldo Carvalho de Melo 	return -1;
28550d08e47SArnaldo Carvalho de Melo }
28650d08e47SArnaldo Carvalho de Melo 
287e251abeeSArnaldo Carvalho de Melo int __evlist__add_default_attrs(struct evlist *evlist, struct perf_event_attr *attrs, size_t nr_attrs)
28879695e1bSArnaldo Carvalho de Melo {
28979695e1bSArnaldo Carvalho de Melo 	size_t i;
29079695e1bSArnaldo Carvalho de Melo 
29179695e1bSArnaldo Carvalho de Melo 	for (i = 0; i < nr_attrs; i++)
29279695e1bSArnaldo Carvalho de Melo 		event_attr_init(attrs + i);
29379695e1bSArnaldo Carvalho de Melo 
294a1cf3a75SJiri Olsa 	return evlist__add_attrs(evlist, attrs, nr_attrs);
29579695e1bSArnaldo Carvalho de Melo }
29679695e1bSArnaldo Carvalho de Melo 
29732dcd021SJiri Olsa struct evsel *
29863503dbaSJiri Olsa perf_evlist__find_tracepoint_by_id(struct evlist *evlist, int id)
299ee29be62SArnaldo Carvalho de Melo {
30032dcd021SJiri Olsa 	struct evsel *evsel;
301ee29be62SArnaldo Carvalho de Melo 
302e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, evsel) {
3031fc632ceSJiri Olsa 		if (evsel->core.attr.type   == PERF_TYPE_TRACEPOINT &&
3041fc632ceSJiri Olsa 		    (int)evsel->core.attr.config == id)
305ee29be62SArnaldo Carvalho de Melo 			return evsel;
306ee29be62SArnaldo Carvalho de Melo 	}
307ee29be62SArnaldo Carvalho de Melo 
308ee29be62SArnaldo Carvalho de Melo 	return NULL;
309ee29be62SArnaldo Carvalho de Melo }
310ee29be62SArnaldo Carvalho de Melo 
31132dcd021SJiri Olsa struct evsel *
31263503dbaSJiri Olsa perf_evlist__find_tracepoint_by_name(struct evlist *evlist,
313a2f2804aSDavid Ahern 				     const char *name)
314a2f2804aSDavid Ahern {
31532dcd021SJiri Olsa 	struct evsel *evsel;
316a2f2804aSDavid Ahern 
317e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, evsel) {
3181fc632ceSJiri Olsa 		if ((evsel->core.attr.type == PERF_TYPE_TRACEPOINT) &&
319a2f2804aSDavid Ahern 		    (strcmp(evsel->name, name) == 0))
320a2f2804aSDavid Ahern 			return evsel;
321a2f2804aSDavid Ahern 	}
322a2f2804aSDavid Ahern 
323a2f2804aSDavid Ahern 	return NULL;
324a2f2804aSDavid Ahern }
325a2f2804aSDavid Ahern 
326e251abeeSArnaldo Carvalho de Melo int evlist__add_newtp(struct evlist *evlist, const char *sys, const char *name, void *handler)
32739876e7dSArnaldo Carvalho de Melo {
3288f6725a2SArnaldo Carvalho de Melo 	struct evsel *evsel = evsel__newtp(sys, name);
32939876e7dSArnaldo Carvalho de Melo 
3308dd2a131SJiri Olsa 	if (IS_ERR(evsel))
33139876e7dSArnaldo Carvalho de Melo 		return -1;
33239876e7dSArnaldo Carvalho de Melo 
333744a9719SArnaldo Carvalho de Melo 	evsel->handler = handler;
334a1cf3a75SJiri Olsa 	evlist__add(evlist, evsel);
33539876e7dSArnaldo Carvalho de Melo 	return 0;
33639876e7dSArnaldo Carvalho de Melo }
33739876e7dSArnaldo Carvalho de Melo 
33863503dbaSJiri Olsa static int perf_evlist__nr_threads(struct evlist *evlist,
33932dcd021SJiri Olsa 				   struct evsel *evsel)
340bf8e8f4bSAdrian Hunter {
341648b5af3SJiri Olsa 	if (evsel->core.system_wide)
342bf8e8f4bSAdrian Hunter 		return 1;
343bf8e8f4bSAdrian Hunter 	else
344a2f354e3SJiri Olsa 		return perf_thread_map__nr(evlist->core.threads);
345bf8e8f4bSAdrian Hunter }
346bf8e8f4bSAdrian Hunter 
347a8cbe40fSAndi Kleen void evlist__cpu_iter_start(struct evlist *evlist)
348a8cbe40fSAndi Kleen {
349a8cbe40fSAndi Kleen 	struct evsel *pos;
350a8cbe40fSAndi Kleen 
351a8cbe40fSAndi Kleen 	/*
352a8cbe40fSAndi Kleen 	 * Reset the per evsel cpu_iter. This is needed because
353a8cbe40fSAndi Kleen 	 * each evsel's cpumap may have a different index space,
354a8cbe40fSAndi Kleen 	 * and some operations need the index to modify
355a8cbe40fSAndi Kleen 	 * the FD xyarray (e.g. open, close)
356a8cbe40fSAndi Kleen 	 */
357a8cbe40fSAndi Kleen 	evlist__for_each_entry(evlist, pos)
358a8cbe40fSAndi Kleen 		pos->cpu_iter = 0;
359a8cbe40fSAndi Kleen }
360a8cbe40fSAndi Kleen 
361a8cbe40fSAndi Kleen bool evsel__cpu_iter_skip_no_inc(struct evsel *ev, int cpu)
362a8cbe40fSAndi Kleen {
363a8cbe40fSAndi Kleen 	if (ev->cpu_iter >= ev->core.cpus->nr)
364a8cbe40fSAndi Kleen 		return true;
365a8cbe40fSAndi Kleen 	if (cpu >= 0 && ev->core.cpus->map[ev->cpu_iter] != cpu)
366a8cbe40fSAndi Kleen 		return true;
367a8cbe40fSAndi Kleen 	return false;
368a8cbe40fSAndi Kleen }
369a8cbe40fSAndi Kleen 
370a8cbe40fSAndi Kleen bool evsel__cpu_iter_skip(struct evsel *ev, int cpu)
371a8cbe40fSAndi Kleen {
372a8cbe40fSAndi Kleen 	if (!evsel__cpu_iter_skip_no_inc(ev, cpu)) {
373a8cbe40fSAndi Kleen 		ev->cpu_iter++;
374a8cbe40fSAndi Kleen 		return false;
375a8cbe40fSAndi Kleen 	}
376a8cbe40fSAndi Kleen 	return true;
377a8cbe40fSAndi Kleen }
378a8cbe40fSAndi Kleen 
379e74676deSJiri Olsa void evlist__disable(struct evlist *evlist)
3804152ab37SArnaldo Carvalho de Melo {
38132dcd021SJiri Olsa 	struct evsel *pos;
382704e2f5bSAndi Kleen 	struct affinity affinity;
38387cf8360SAdrian Hunter 	int cpu, i, imm = 0;
38487cf8360SAdrian Hunter 	bool has_imm = false;
385704e2f5bSAndi Kleen 
386704e2f5bSAndi Kleen 	if (affinity__setup(&affinity) < 0)
387704e2f5bSAndi Kleen 		return;
388704e2f5bSAndi Kleen 
38987cf8360SAdrian Hunter 	/* Disable 'immediate' events last */
39087cf8360SAdrian Hunter 	for (imm = 0; imm <= 1; imm++) {
391704e2f5bSAndi Kleen 		evlist__for_each_cpu(evlist, i, cpu) {
392704e2f5bSAndi Kleen 			affinity__set(&affinity, cpu);
3934152ab37SArnaldo Carvalho de Melo 
394e5cadb93SArnaldo Carvalho de Melo 			evlist__for_each_entry(evlist, pos) {
395704e2f5bSAndi Kleen 				if (evsel__cpu_iter_skip(pos, cpu))
396704e2f5bSAndi Kleen 					continue;
397c754c382SArnaldo Carvalho de Melo 				if (pos->disabled || !evsel__is_group_leader(pos) || !pos->core.fd)
3983fe4430dSJiri Olsa 					continue;
39987cf8360SAdrian Hunter 				if (pos->immediate)
40087cf8360SAdrian Hunter 					has_imm = true;
40187cf8360SAdrian Hunter 				if (pos->immediate != imm)
40287cf8360SAdrian Hunter 					continue;
403704e2f5bSAndi Kleen 				evsel__disable_cpu(pos, pos->cpu_iter - 1);
404704e2f5bSAndi Kleen 			}
405704e2f5bSAndi Kleen 		}
40687cf8360SAdrian Hunter 		if (!has_imm)
40787cf8360SAdrian Hunter 			break;
40887cf8360SAdrian Hunter 	}
40987cf8360SAdrian Hunter 
410704e2f5bSAndi Kleen 	affinity__cleanup(&affinity);
411704e2f5bSAndi Kleen 	evlist__for_each_entry(evlist, pos) {
412c754c382SArnaldo Carvalho de Melo 		if (!evsel__is_group_leader(pos) || !pos->core.fd)
413704e2f5bSAndi Kleen 			continue;
414704e2f5bSAndi Kleen 		pos->disabled = true;
4154152ab37SArnaldo Carvalho de Melo 	}
4162b56bcfbSArnaldo Carvalho de Melo 
4172b56bcfbSArnaldo Carvalho de Melo 	evlist->enabled = false;
4184152ab37SArnaldo Carvalho de Melo }
4194152ab37SArnaldo Carvalho de Melo 
4201c87f165SJiri Olsa void evlist__enable(struct evlist *evlist)
421764e16a3SDavid Ahern {
42232dcd021SJiri Olsa 	struct evsel *pos;
423704e2f5bSAndi Kleen 	struct affinity affinity;
424704e2f5bSAndi Kleen 	int cpu, i;
425764e16a3SDavid Ahern 
426704e2f5bSAndi Kleen 	if (affinity__setup(&affinity) < 0)
427704e2f5bSAndi Kleen 		return;
428704e2f5bSAndi Kleen 
429704e2f5bSAndi Kleen 	evlist__for_each_cpu(evlist, i, cpu) {
430704e2f5bSAndi Kleen 		affinity__set(&affinity, cpu);
431704e2f5bSAndi Kleen 
432704e2f5bSAndi Kleen 		evlist__for_each_entry(evlist, pos) {
433704e2f5bSAndi Kleen 			if (evsel__cpu_iter_skip(pos, cpu))
434704e2f5bSAndi Kleen 				continue;
435c754c382SArnaldo Carvalho de Melo 			if (!evsel__is_group_leader(pos) || !pos->core.fd)
436704e2f5bSAndi Kleen 				continue;
437704e2f5bSAndi Kleen 			evsel__enable_cpu(pos, pos->cpu_iter - 1);
438704e2f5bSAndi Kleen 		}
439704e2f5bSAndi Kleen 	}
440704e2f5bSAndi Kleen 	affinity__cleanup(&affinity);
441e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, pos) {
442c754c382SArnaldo Carvalho de Melo 		if (!evsel__is_group_leader(pos) || !pos->core.fd)
4433fe4430dSJiri Olsa 			continue;
444704e2f5bSAndi Kleen 		pos->disabled = false;
445764e16a3SDavid Ahern 	}
4462b56bcfbSArnaldo Carvalho de Melo 
4472b56bcfbSArnaldo Carvalho de Melo 	evlist->enabled = true;
4482b56bcfbSArnaldo Carvalho de Melo }
4492b56bcfbSArnaldo Carvalho de Melo 
45063503dbaSJiri Olsa void perf_evlist__toggle_enable(struct evlist *evlist)
4512b56bcfbSArnaldo Carvalho de Melo {
452e74676deSJiri Olsa 	(evlist->enabled ? evlist__disable : evlist__enable)(evlist);
453764e16a3SDavid Ahern }
454764e16a3SDavid Ahern 
45563503dbaSJiri Olsa static int perf_evlist__enable_event_cpu(struct evlist *evlist,
45632dcd021SJiri Olsa 					 struct evsel *evsel, int cpu)
4571c65056cSAdrian Hunter {
45818ef15c6SArnaldo Carvalho de Melo 	int thread;
4591c65056cSAdrian Hunter 	int nr_threads = perf_evlist__nr_threads(evlist, evsel);
4601c65056cSAdrian Hunter 
4619dfcb759SJiri Olsa 	if (!evsel->core.fd)
4621c65056cSAdrian Hunter 		return -EINVAL;
4631c65056cSAdrian Hunter 
4641c65056cSAdrian Hunter 	for (thread = 0; thread < nr_threads; thread++) {
46518ef15c6SArnaldo Carvalho de Melo 		int err = ioctl(FD(evsel, cpu, thread), PERF_EVENT_IOC_ENABLE, 0);
4661c65056cSAdrian Hunter 		if (err)
4671c65056cSAdrian Hunter 			return err;
4681c65056cSAdrian Hunter 	}
4691c65056cSAdrian Hunter 	return 0;
4701c65056cSAdrian Hunter }
4711c65056cSAdrian Hunter 
47263503dbaSJiri Olsa static int perf_evlist__enable_event_thread(struct evlist *evlist,
47332dcd021SJiri Olsa 					    struct evsel *evsel,
4741c65056cSAdrian Hunter 					    int thread)
4751c65056cSAdrian Hunter {
47618ef15c6SArnaldo Carvalho de Melo 	int cpu;
4776549cd8fSJiri Olsa 	int nr_cpus = perf_cpu_map__nr(evlist->core.cpus);
4781c65056cSAdrian Hunter 
4799dfcb759SJiri Olsa 	if (!evsel->core.fd)
4801c65056cSAdrian Hunter 		return -EINVAL;
4811c65056cSAdrian Hunter 
4821c65056cSAdrian Hunter 	for (cpu = 0; cpu < nr_cpus; cpu++) {
48318ef15c6SArnaldo Carvalho de Melo 		int err = ioctl(FD(evsel, cpu, thread), PERF_EVENT_IOC_ENABLE, 0);
4841c65056cSAdrian Hunter 		if (err)
4851c65056cSAdrian Hunter 			return err;
4861c65056cSAdrian Hunter 	}
4871c65056cSAdrian Hunter 	return 0;
4881c65056cSAdrian Hunter }
4891c65056cSAdrian Hunter 
49063503dbaSJiri Olsa int perf_evlist__enable_event_idx(struct evlist *evlist,
49132dcd021SJiri Olsa 				  struct evsel *evsel, int idx)
4921c65056cSAdrian Hunter {
493315c0a1fSJiri Olsa 	bool per_cpu_mmaps = !perf_cpu_map__empty(evlist->core.cpus);
4941c65056cSAdrian Hunter 
4951c65056cSAdrian Hunter 	if (per_cpu_mmaps)
4961c65056cSAdrian Hunter 		return perf_evlist__enable_event_cpu(evlist, evsel, idx);
4971c65056cSAdrian Hunter 	else
4981c65056cSAdrian Hunter 		return perf_evlist__enable_event_thread(evlist, evsel, idx);
4991c65056cSAdrian Hunter }
5001c65056cSAdrian Hunter 
501f4009e7bSJiri Olsa int evlist__add_pollfd(struct evlist *evlist, int fd)
502e4b356b5SArnaldo Carvalho de Melo {
503ab4c1f9fSAlexey Budankov 	return perf_evlist__add_pollfd(&evlist->core, fd, NULL, POLLIN, fdarray_flag__default);
504e4b356b5SArnaldo Carvalho de Melo }
5051b85337dSArnaldo Carvalho de Melo 
506f4009e7bSJiri Olsa int evlist__filter_pollfd(struct evlist *evlist, short revents_and_mask)
5071ddec7f0SArnaldo Carvalho de Melo {
50884227cb1SJiri Olsa 	return perf_evlist__filter_pollfd(&evlist->core, revents_and_mask);
5091ddec7f0SArnaldo Carvalho de Melo }
5101ddec7f0SArnaldo Carvalho de Melo 
51180ab2987SJiri Olsa int evlist__poll(struct evlist *evlist, int timeout)
512f66a889dSArnaldo Carvalho de Melo {
51380ab2987SJiri Olsa 	return perf_evlist__poll(&evlist->core, timeout);
514f66a889dSArnaldo Carvalho de Melo }
515f66a889dSArnaldo Carvalho de Melo 
51663503dbaSJiri Olsa struct perf_sample_id *perf_evlist__id2sid(struct evlist *evlist, u64 id)
51770db7533SArnaldo Carvalho de Melo {
51870db7533SArnaldo Carvalho de Melo 	struct hlist_head *head;
51970db7533SArnaldo Carvalho de Melo 	struct perf_sample_id *sid;
52070db7533SArnaldo Carvalho de Melo 	int hash;
52170db7533SArnaldo Carvalho de Melo 
52270db7533SArnaldo Carvalho de Melo 	hash = hash_64(id, PERF_EVLIST__HLIST_BITS);
5231d5af02dSJiri Olsa 	head = &evlist->core.heads[hash];
52470db7533SArnaldo Carvalho de Melo 
525b67bfe0dSSasha Levin 	hlist_for_each_entry(sid, head, node)
52670db7533SArnaldo Carvalho de Melo 		if (sid->id == id)
527932a3594SJiri Olsa 			return sid;
528932a3594SJiri Olsa 
529932a3594SJiri Olsa 	return NULL;
530932a3594SJiri Olsa }
531932a3594SJiri Olsa 
53263503dbaSJiri Olsa struct evsel *perf_evlist__id2evsel(struct evlist *evlist, u64 id)
533932a3594SJiri Olsa {
534932a3594SJiri Olsa 	struct perf_sample_id *sid;
535932a3594SJiri Olsa 
5366484d2f9SJiri Olsa 	if (evlist->core.nr_entries == 1 || !id)
537515dbe48SJiri Olsa 		return evlist__first(evlist);
538932a3594SJiri Olsa 
539932a3594SJiri Olsa 	sid = perf_evlist__id2sid(evlist, id);
540932a3594SJiri Olsa 	if (sid)
54170c20369SJiri Olsa 		return container_of(sid->evsel, struct evsel, core);
54230e68bccSNamhyung Kim 
5438cedf3a5SArnaldo Carvalho de Melo 	if (!evlist__sample_id_all(evlist))
544515dbe48SJiri Olsa 		return evlist__first(evlist);
54530e68bccSNamhyung Kim 
54670db7533SArnaldo Carvalho de Melo 	return NULL;
54770db7533SArnaldo Carvalho de Melo }
54804391debSArnaldo Carvalho de Melo 
54963503dbaSJiri Olsa struct evsel *perf_evlist__id2evsel_strict(struct evlist *evlist,
550dddcf6abSAdrian Hunter 						u64 id)
551dddcf6abSAdrian Hunter {
552dddcf6abSAdrian Hunter 	struct perf_sample_id *sid;
553dddcf6abSAdrian Hunter 
554dddcf6abSAdrian Hunter 	if (!id)
555dddcf6abSAdrian Hunter 		return NULL;
556dddcf6abSAdrian Hunter 
557dddcf6abSAdrian Hunter 	sid = perf_evlist__id2sid(evlist, id);
558dddcf6abSAdrian Hunter 	if (sid)
55970c20369SJiri Olsa 		return container_of(sid->evsel, struct evsel, core);
560dddcf6abSAdrian Hunter 
561dddcf6abSAdrian Hunter 	return NULL;
562dddcf6abSAdrian Hunter }
563dddcf6abSAdrian Hunter 
56463503dbaSJiri Olsa static int perf_evlist__event2id(struct evlist *evlist,
56575562573SAdrian Hunter 				 union perf_event *event, u64 *id)
56675562573SAdrian Hunter {
567b1fcd190SJiri Olsa 	const __u64 *array = event->sample.array;
56875562573SAdrian Hunter 	ssize_t n;
56975562573SAdrian Hunter 
57075562573SAdrian Hunter 	n = (event->header.size - sizeof(event->header)) >> 3;
57175562573SAdrian Hunter 
57275562573SAdrian Hunter 	if (event->header.type == PERF_RECORD_SAMPLE) {
57375562573SAdrian Hunter 		if (evlist->id_pos >= n)
57475562573SAdrian Hunter 			return -1;
57575562573SAdrian Hunter 		*id = array[evlist->id_pos];
57675562573SAdrian Hunter 	} else {
57775562573SAdrian Hunter 		if (evlist->is_pos > n)
57875562573SAdrian Hunter 			return -1;
57975562573SAdrian Hunter 		n -= evlist->is_pos;
58075562573SAdrian Hunter 		*id = array[n];
58175562573SAdrian Hunter 	}
58275562573SAdrian Hunter 	return 0;
58375562573SAdrian Hunter }
58475562573SAdrian Hunter 
58563503dbaSJiri Olsa struct evsel *perf_evlist__event2evsel(struct evlist *evlist,
58675562573SAdrian Hunter 					    union perf_event *event)
58775562573SAdrian Hunter {
588515dbe48SJiri Olsa 	struct evsel *first = evlist__first(evlist);
58975562573SAdrian Hunter 	struct hlist_head *head;
59075562573SAdrian Hunter 	struct perf_sample_id *sid;
59175562573SAdrian Hunter 	int hash;
59275562573SAdrian Hunter 	u64 id;
59375562573SAdrian Hunter 
5946484d2f9SJiri Olsa 	if (evlist->core.nr_entries == 1)
59598be6966SAdrian Hunter 		return first;
59698be6966SAdrian Hunter 
5971fc632ceSJiri Olsa 	if (!first->core.attr.sample_id_all &&
59898be6966SAdrian Hunter 	    event->header.type != PERF_RECORD_SAMPLE)
59998be6966SAdrian Hunter 		return first;
60075562573SAdrian Hunter 
60175562573SAdrian Hunter 	if (perf_evlist__event2id(evlist, event, &id))
60275562573SAdrian Hunter 		return NULL;
60375562573SAdrian Hunter 
60475562573SAdrian Hunter 	/* Synthesized events have an id of zero */
60575562573SAdrian Hunter 	if (!id)
60698be6966SAdrian Hunter 		return first;
60775562573SAdrian Hunter 
60875562573SAdrian Hunter 	hash = hash_64(id, PERF_EVLIST__HLIST_BITS);
6091d5af02dSJiri Olsa 	head = &evlist->core.heads[hash];
61075562573SAdrian Hunter 
61175562573SAdrian Hunter 	hlist_for_each_entry(sid, head, node) {
61275562573SAdrian Hunter 		if (sid->id == id)
61370c20369SJiri Olsa 			return container_of(sid->evsel, struct evsel, core);
61475562573SAdrian Hunter 	}
61575562573SAdrian Hunter 	return NULL;
61675562573SAdrian Hunter }
61775562573SAdrian Hunter 
61863503dbaSJiri Olsa static int perf_evlist__set_paused(struct evlist *evlist, bool value)
61965aea233SWang Nan {
62065aea233SWang Nan 	int i;
62165aea233SWang Nan 
6220b72d69aSWang Nan 	if (!evlist->overwrite_mmap)
623078c3386SWang Nan 		return 0;
624078c3386SWang Nan 
625c976ee11SJiri Olsa 	for (i = 0; i < evlist->core.nr_mmaps; i++) {
6262cf07b29SJiri Olsa 		int fd = evlist->overwrite_mmap[i].core.fd;
62765aea233SWang Nan 		int err;
62865aea233SWang Nan 
62965aea233SWang Nan 		if (fd < 0)
63065aea233SWang Nan 			continue;
63165aea233SWang Nan 		err = ioctl(fd, PERF_EVENT_IOC_PAUSE_OUTPUT, value ? 1 : 0);
63265aea233SWang Nan 		if (err)
63365aea233SWang Nan 			return err;
63465aea233SWang Nan 	}
63565aea233SWang Nan 	return 0;
63665aea233SWang Nan }
63765aea233SWang Nan 
63863503dbaSJiri Olsa static int perf_evlist__pause(struct evlist *evlist)
63965aea233SWang Nan {
64065aea233SWang Nan 	return perf_evlist__set_paused(evlist, true);
64165aea233SWang Nan }
64265aea233SWang Nan 
64363503dbaSJiri Olsa static int perf_evlist__resume(struct evlist *evlist)
64465aea233SWang Nan {
64565aea233SWang Nan 	return perf_evlist__set_paused(evlist, false);
64665aea233SWang Nan }
64765aea233SWang Nan 
648db6b7b13SJiri Olsa static void evlist__munmap_nofree(struct evlist *evlist)
649f8a95309SArnaldo Carvalho de Melo {
650aece948fSArnaldo Carvalho de Melo 	int i;
651f8a95309SArnaldo Carvalho de Melo 
652b2cb615dSWang Nan 	if (evlist->mmap)
653c976ee11SJiri Olsa 		for (i = 0; i < evlist->core.nr_mmaps; i++)
65480e53d11SJiri Olsa 			perf_mmap__munmap(&evlist->mmap[i].core);
655b2cb615dSWang Nan 
6560b72d69aSWang Nan 	if (evlist->overwrite_mmap)
657c976ee11SJiri Olsa 		for (i = 0; i < evlist->core.nr_mmaps; i++)
65880e53d11SJiri Olsa 			perf_mmap__munmap(&evlist->overwrite_mmap[i].core);
659a1f72618SWang Nan }
660aece948fSArnaldo Carvalho de Melo 
661db6b7b13SJiri Olsa void evlist__munmap(struct evlist *evlist)
662a1f72618SWang Nan {
663db6b7b13SJiri Olsa 	evlist__munmap_nofree(evlist);
66404662523SArnaldo Carvalho de Melo 	zfree(&evlist->mmap);
6650b72d69aSWang Nan 	zfree(&evlist->overwrite_mmap);
666f8a95309SArnaldo Carvalho de Melo }
667f8a95309SArnaldo Carvalho de Melo 
66880e53d11SJiri Olsa static void perf_mmap__unmap_cb(struct perf_mmap *map)
66980e53d11SJiri Olsa {
67080e53d11SJiri Olsa 	struct mmap *m = container_of(map, struct mmap, core);
67180e53d11SJiri Olsa 
67280e53d11SJiri Olsa 	mmap__munmap(m);
67380e53d11SJiri Olsa }
67480e53d11SJiri Olsa 
675d50cf361SJiri Olsa static struct mmap *evlist__alloc_mmap(struct evlist *evlist,
6762c5f6d87SKan Liang 				       bool overwrite)
677f8a95309SArnaldo Carvalho de Melo {
678d4c6fb36SWang Nan 	int i;
679a5830532SJiri Olsa 	struct mmap *map;
680d4c6fb36SWang Nan 
681c976ee11SJiri Olsa 	map = zalloc(evlist->core.nr_mmaps * sizeof(struct mmap));
6828db6d6b1SWang Nan 	if (!map)
6838db6d6b1SWang Nan 		return NULL;
684946ae1d4SWang Nan 
685c976ee11SJiri Olsa 	for (i = 0; i < evlist->core.nr_mmaps; i++) {
6866eb65f7aSJiri Olsa 		struct perf_mmap *prev = i ? &map[i - 1].core : NULL;
6876eb65f7aSJiri Olsa 
6884738ca30SArnaldo Carvalho de Melo 		/*
6894738ca30SArnaldo Carvalho de Melo 		 * When the perf_mmap() call is made we grab one refcount, plus
6906afad54dSKan Liang 		 * one extra to let perf_mmap__consume() get the last
6914738ca30SArnaldo Carvalho de Melo 		 * events after all real references (perf_mmap__get()) are
6924738ca30SArnaldo Carvalho de Melo 		 * dropped.
6934738ca30SArnaldo Carvalho de Melo 		 *
6944738ca30SArnaldo Carvalho de Melo 		 * Each PERF_EVENT_IOC_SET_OUTPUT points to this mmap and
6954738ca30SArnaldo Carvalho de Melo 		 * thus does perf_mmap__get() on it.
6964738ca30SArnaldo Carvalho de Melo 		 */
6976eb65f7aSJiri Olsa 		perf_mmap__init(&map[i].core, prev, overwrite, perf_mmap__unmap_cb);
6984738ca30SArnaldo Carvalho de Melo 	}
699353120b4SJiri Olsa 
7008db6d6b1SWang Nan 	return map;
701f8a95309SArnaldo Carvalho de Melo }
702f8a95309SArnaldo Carvalho de Melo 
7039abd2ab2SJiri Olsa static void
7049abd2ab2SJiri Olsa perf_evlist__mmap_cb_idx(struct perf_evlist *_evlist,
7059abd2ab2SJiri Olsa 			 struct perf_mmap_param *_mp,
7069abd2ab2SJiri Olsa 			 int idx, bool per_cpu)
7079abd2ab2SJiri Olsa {
7089abd2ab2SJiri Olsa 	struct evlist *evlist = container_of(_evlist, struct evlist, core);
7099abd2ab2SJiri Olsa 	struct mmap_params *mp = container_of(_mp, struct mmap_params, core);
7109abd2ab2SJiri Olsa 
7119abd2ab2SJiri Olsa 	auxtrace_mmap_params__set_idx(&mp->auxtrace_mp, evlist, idx, per_cpu);
7129abd2ab2SJiri Olsa }
7139abd2ab2SJiri Olsa 
714bb1b1885SJiri Olsa static struct perf_mmap*
715bb1b1885SJiri Olsa perf_evlist__mmap_cb_get(struct perf_evlist *_evlist, bool overwrite, int idx)
716bb1b1885SJiri Olsa {
717bb1b1885SJiri Olsa 	struct evlist *evlist = container_of(_evlist, struct evlist, core);
7183805e4f3SJiri Olsa 	struct mmap *maps;
719bb1b1885SJiri Olsa 
7203805e4f3SJiri Olsa 	maps = overwrite ? evlist->overwrite_mmap : evlist->mmap;
721bb1b1885SJiri Olsa 
722bb1b1885SJiri Olsa 	if (!maps) {
7233805e4f3SJiri Olsa 		maps = evlist__alloc_mmap(evlist, overwrite);
724bb1b1885SJiri Olsa 		if (!maps)
725bb1b1885SJiri Olsa 			return NULL;
726bb1b1885SJiri Olsa 
7273805e4f3SJiri Olsa 		if (overwrite) {
728bb1b1885SJiri Olsa 			evlist->overwrite_mmap = maps;
729bb1b1885SJiri Olsa 			if (evlist->bkw_mmap_state == BKW_MMAP_NOTREADY)
730bb1b1885SJiri Olsa 				perf_evlist__toggle_bkw_mmap(evlist, BKW_MMAP_RUNNING);
7313805e4f3SJiri Olsa 		} else {
7323805e4f3SJiri Olsa 			evlist->mmap = maps;
733bb1b1885SJiri Olsa 		}
734bb1b1885SJiri Olsa 	}
735bb1b1885SJiri Olsa 
736bb1b1885SJiri Olsa 	return &maps[idx].core;
737bb1b1885SJiri Olsa }
738bb1b1885SJiri Olsa 
739b80132b1SJiri Olsa static int
740b80132b1SJiri Olsa perf_evlist__mmap_cb_mmap(struct perf_mmap *_map, struct perf_mmap_param *_mp,
741b80132b1SJiri Olsa 			  int output, int cpu)
742b80132b1SJiri Olsa {
743b80132b1SJiri Olsa 	struct mmap *map = container_of(_map, struct mmap, core);
744b80132b1SJiri Olsa 	struct mmap_params *mp = container_of(_mp, struct mmap_params, core);
745b80132b1SJiri Olsa 
746b80132b1SJiri Olsa 	return mmap__mmap(map, mp, output, cpu);
747b80132b1SJiri Olsa }
748b80132b1SJiri Olsa 
749f5e7150cSArnaldo Carvalho de Melo unsigned long perf_event_mlock_kb_in_pages(void)
750994a1f78SJiri Olsa {
751f5e7150cSArnaldo Carvalho de Melo 	unsigned long pages;
7528185e881SArnaldo Carvalho de Melo 	int max;
7538185e881SArnaldo Carvalho de Melo 
7548185e881SArnaldo Carvalho de Melo 	if (sysctl__read_int("kernel/perf_event_mlock_kb", &max) < 0) {
7558185e881SArnaldo Carvalho de Melo 		/*
7568185e881SArnaldo Carvalho de Melo 		 * Pick a once upon a time good value, i.e. things look
7578185e881SArnaldo Carvalho de Melo 		 * strange since we can't read a sysctl value, but lets not
7588185e881SArnaldo Carvalho de Melo 		 * die yet...
7598185e881SArnaldo Carvalho de Melo 		 */
7608185e881SArnaldo Carvalho de Melo 		max = 512;
7618185e881SArnaldo Carvalho de Melo 	} else {
7628185e881SArnaldo Carvalho de Melo 		max -= (page_size / 1024);
7638185e881SArnaldo Carvalho de Melo 	}
7648185e881SArnaldo Carvalho de Melo 
7658185e881SArnaldo Carvalho de Melo 	pages = (max * 1024) / page_size;
7661be300f4SArnaldo Carvalho de Melo 	if (!is_power_of_2(pages))
7671be300f4SArnaldo Carvalho de Melo 		pages = rounddown_pow_of_two(pages);
768f5e7150cSArnaldo Carvalho de Melo 
769f5e7150cSArnaldo Carvalho de Melo 	return pages;
770f5e7150cSArnaldo Carvalho de Melo }
771f5e7150cSArnaldo Carvalho de Melo 
7729521b5f2SJiri Olsa size_t evlist__mmap_size(unsigned long pages)
773f5e7150cSArnaldo Carvalho de Melo {
774f5e7150cSArnaldo Carvalho de Melo 	if (pages == UINT_MAX)
775f5e7150cSArnaldo Carvalho de Melo 		pages = perf_event_mlock_kb_in_pages();
776f5e7150cSArnaldo Carvalho de Melo 	else if (!is_power_of_2(pages))
777994a1f78SJiri Olsa 		return 0;
778994a1f78SJiri Olsa 
779994a1f78SJiri Olsa 	return (pages + 1) * page_size;
780994a1f78SJiri Olsa }
781994a1f78SJiri Olsa 
78233c2dcfdSDavid Ahern static long parse_pages_arg(const char *str, unsigned long min,
78333c2dcfdSDavid Ahern 			    unsigned long max)
784994a1f78SJiri Olsa {
7852fbe4abeSAdrian Hunter 	unsigned long pages, val;
78627050f53SJiri Olsa 	static struct parse_tag tags[] = {
78727050f53SJiri Olsa 		{ .tag  = 'B', .mult = 1       },
78827050f53SJiri Olsa 		{ .tag  = 'K', .mult = 1 << 10 },
78927050f53SJiri Olsa 		{ .tag  = 'M', .mult = 1 << 20 },
79027050f53SJiri Olsa 		{ .tag  = 'G', .mult = 1 << 30 },
79127050f53SJiri Olsa 		{ .tag  = 0 },
79227050f53SJiri Olsa 	};
793994a1f78SJiri Olsa 
7948973504bSDavid Ahern 	if (str == NULL)
79533c2dcfdSDavid Ahern 		return -EINVAL;
7968973504bSDavid Ahern 
79727050f53SJiri Olsa 	val = parse_tag_value(str, tags);
7982fbe4abeSAdrian Hunter 	if (val != (unsigned long) -1) {
79927050f53SJiri Olsa 		/* we got file size value */
80027050f53SJiri Olsa 		pages = PERF_ALIGN(val, page_size) / page_size;
80127050f53SJiri Olsa 	} else {
80227050f53SJiri Olsa 		/* we got pages count value */
80327050f53SJiri Olsa 		char *eptr;
804994a1f78SJiri Olsa 		pages = strtoul(str, &eptr, 10);
80533c2dcfdSDavid Ahern 		if (*eptr != '\0')
80633c2dcfdSDavid Ahern 			return -EINVAL;
80727050f53SJiri Olsa 	}
808994a1f78SJiri Olsa 
8092bcab6c1SAdrian Hunter 	if (pages == 0 && min == 0) {
81033c2dcfdSDavid Ahern 		/* leave number of pages at 0 */
8111dbfa938SAdrian Hunter 	} else if (!is_power_of_2(pages)) {
8129808143bSJiri Olsa 		char buf[100];
8139808143bSJiri Olsa 
81433c2dcfdSDavid Ahern 		/* round pages up to next power of 2 */
81591529834SArnaldo Carvalho de Melo 		pages = roundup_pow_of_two(pages);
8161dbfa938SAdrian Hunter 		if (!pages)
8171dbfa938SAdrian Hunter 			return -EINVAL;
8189808143bSJiri Olsa 
8199808143bSJiri Olsa 		unit_number__scnprintf(buf, sizeof(buf), pages * page_size);
8209808143bSJiri Olsa 		pr_info("rounding mmap pages size to %s (%lu pages)\n",
8219808143bSJiri Olsa 			buf, pages);
8222fbe4abeSAdrian Hunter 	}
8232fbe4abeSAdrian Hunter 
82433c2dcfdSDavid Ahern 	if (pages > max)
82533c2dcfdSDavid Ahern 		return -EINVAL;
82633c2dcfdSDavid Ahern 
82733c2dcfdSDavid Ahern 	return pages;
82833c2dcfdSDavid Ahern }
82933c2dcfdSDavid Ahern 
830e9db1310SAdrian Hunter int __perf_evlist__parse_mmap_pages(unsigned int *mmap_pages, const char *str)
83133c2dcfdSDavid Ahern {
83233c2dcfdSDavid Ahern 	unsigned long max = UINT_MAX;
83333c2dcfdSDavid Ahern 	long pages;
83433c2dcfdSDavid Ahern 
835f5ae9c42SAdrian Hunter 	if (max > SIZE_MAX / page_size)
83633c2dcfdSDavid Ahern 		max = SIZE_MAX / page_size;
83733c2dcfdSDavid Ahern 
83833c2dcfdSDavid Ahern 	pages = parse_pages_arg(str, 1, max);
83933c2dcfdSDavid Ahern 	if (pages < 0) {
84033c2dcfdSDavid Ahern 		pr_err("Invalid argument for --mmap_pages/-m\n");
841994a1f78SJiri Olsa 		return -1;
842994a1f78SJiri Olsa 	}
843994a1f78SJiri Olsa 
844994a1f78SJiri Olsa 	*mmap_pages = pages;
845994a1f78SJiri Olsa 	return 0;
846994a1f78SJiri Olsa }
847994a1f78SJiri Olsa 
848e9db1310SAdrian Hunter int perf_evlist__parse_mmap_pages(const struct option *opt, const char *str,
849e9db1310SAdrian Hunter 				  int unset __maybe_unused)
850e9db1310SAdrian Hunter {
851e9db1310SAdrian Hunter 	return __perf_evlist__parse_mmap_pages(opt->value, str);
852e9db1310SAdrian Hunter }
853e9db1310SAdrian Hunter 
854c83fa7f2SAdrian Hunter /**
8559521b5f2SJiri Olsa  * evlist__mmap_ex - Create mmaps to receive events.
856c83fa7f2SAdrian Hunter  * @evlist: list of events
857c83fa7f2SAdrian Hunter  * @pages: map length in pages
858c83fa7f2SAdrian Hunter  * @overwrite: overwrite older events?
859718c602dSAdrian Hunter  * @auxtrace_pages - auxtrace map length in pages
860718c602dSAdrian Hunter  * @auxtrace_overwrite - overwrite older auxtrace data?
861f8a95309SArnaldo Carvalho de Melo  *
862c83fa7f2SAdrian Hunter  * If @overwrite is %false the user needs to signal event consumption using
8639521b5f2SJiri Olsa  * perf_mmap__write_tail().  Using evlist__mmap_read() does this
864c83fa7f2SAdrian Hunter  * automatically.
865f8a95309SArnaldo Carvalho de Melo  *
866718c602dSAdrian Hunter  * Similarly, if @auxtrace_overwrite is %false the user needs to signal data
867718c602dSAdrian Hunter  * consumption using auxtrace_mmap__write_tail().
868718c602dSAdrian Hunter  *
869c83fa7f2SAdrian Hunter  * Return: %0 on success, negative error code otherwise.
870f8a95309SArnaldo Carvalho de Melo  */
8719521b5f2SJiri Olsa int evlist__mmap_ex(struct evlist *evlist, unsigned int pages,
8727a276ff6SWang Nan 			 unsigned int auxtrace_pages,
87351255a8aSAlexey Budankov 			 bool auxtrace_overwrite, int nr_cblocks, int affinity, int flush,
87451255a8aSAlexey Budankov 			 int comp_level)
875f8a95309SArnaldo Carvalho de Melo {
87671f566a3SWang Nan 	/*
87771f566a3SWang Nan 	 * Delay setting mp.prot: set it before calling perf_mmap__mmap.
87871f566a3SWang Nan 	 * Its value is decided by evsel's write_backward.
87971f566a3SWang Nan 	 * So &mp should not be passed through const pointer.
88071f566a3SWang Nan 	 */
881e440979fSJiri Olsa 	struct mmap_params mp = {
882e440979fSJiri Olsa 		.nr_cblocks	= nr_cblocks,
883e440979fSJiri Olsa 		.affinity	= affinity,
884e440979fSJiri Olsa 		.flush		= flush,
885e440979fSJiri Olsa 		.comp_level	= comp_level
886e440979fSJiri Olsa 	};
887923d0f18SJiri Olsa 	struct perf_evlist_mmap_ops ops = {
8889abd2ab2SJiri Olsa 		.idx  = perf_evlist__mmap_cb_idx,
889bb1b1885SJiri Olsa 		.get  = perf_evlist__mmap_cb_get,
890b80132b1SJiri Olsa 		.mmap = perf_evlist__mmap_cb_mmap,
8919abd2ab2SJiri Olsa 	};
89250a682ceSArnaldo Carvalho de Melo 
893f6fa4375SJiri Olsa 	evlist->core.mmap_len = evlist__mmap_size(pages);
894f6fa4375SJiri Olsa 	pr_debug("mmap size %zuB\n", evlist->core.mmap_len);
895f8a95309SArnaldo Carvalho de Melo 
896f6fa4375SJiri Olsa 	auxtrace_mmap_params__init(&mp.auxtrace_mp, evlist->core.mmap_len,
897718c602dSAdrian Hunter 				   auxtrace_pages, auxtrace_overwrite);
898718c602dSAdrian Hunter 
899923d0f18SJiri Olsa 	return perf_evlist__mmap_ops(&evlist->core, &ops, &mp.core);
900f8a95309SArnaldo Carvalho de Melo }
9017e2ed097SArnaldo Carvalho de Melo 
9029521b5f2SJiri Olsa int evlist__mmap(struct evlist *evlist, unsigned int pages)
903718c602dSAdrian Hunter {
9049521b5f2SJiri Olsa 	return evlist__mmap_ex(evlist, pages, 0, false, 0, PERF_AFFINITY_SYS, 1, 0);
905718c602dSAdrian Hunter }
906718c602dSAdrian Hunter 
90763503dbaSJiri Olsa int perf_evlist__create_maps(struct evlist *evlist, struct target *target)
9087e2ed097SArnaldo Carvalho de Melo {
909147c508fSJin Yao 	bool all_threads = (target->per_thread && target->system_wide);
910f854839bSJiri Olsa 	struct perf_cpu_map *cpus;
9119749b90eSJiri Olsa 	struct perf_thread_map *threads;
9127e2ed097SArnaldo Carvalho de Melo 
913147c508fSJin Yao 	/*
914147c508fSJin Yao 	 * If specify '-a' and '--per-thread' to perf record, perf record
915147c508fSJin Yao 	 * will override '--per-thread'. target->per_thread = false and
916147c508fSJin Yao 	 * target->system_wide = true.
917147c508fSJin Yao 	 *
918147c508fSJin Yao 	 * If specify '--per-thread' only to perf record,
919147c508fSJin Yao 	 * target->per_thread = true and target->system_wide = false.
920147c508fSJin Yao 	 *
921147c508fSJin Yao 	 * So target->per_thread && target->system_wide is false.
922147c508fSJin Yao 	 * For perf record, thread_map__new_str doesn't call
923147c508fSJin Yao 	 * thread_map__new_all_cpus. That will keep perf record's
924147c508fSJin Yao 	 * current behavior.
925147c508fSJin Yao 	 *
926147c508fSJin Yao 	 * For perf stat, it allows the case that target->per_thread and
927147c508fSJin Yao 	 * target->system_wide are all true. It means to collect system-wide
928147c508fSJin Yao 	 * per-thread data. thread_map__new_str will call
929147c508fSJin Yao 	 * thread_map__new_all_cpus to enumerate all threads.
930147c508fSJin Yao 	 */
93173c0ca1eSJin Yao 	threads = thread_map__new_str(target->pid, target->tid, target->uid,
932147c508fSJin Yao 				      all_threads);
93374bfd2b2SAdrian Hunter 
93474bfd2b2SAdrian Hunter 	if (!threads)
9357e2ed097SArnaldo Carvalho de Melo 		return -1;
9367e2ed097SArnaldo Carvalho de Melo 
9379c105fbcSDongsheng Yang 	if (target__uses_dummy_map(target))
938397721e0SJiri Olsa 		cpus = perf_cpu_map__dummy_new();
939879d77d0SNamhyung Kim 	else
9409c3516d1SJiri Olsa 		cpus = perf_cpu_map__new(target->cpu_list);
9417e2ed097SArnaldo Carvalho de Melo 
94274bfd2b2SAdrian Hunter 	if (!cpus)
9437e2ed097SArnaldo Carvalho de Melo 		goto out_delete_threads;
9447e2ed097SArnaldo Carvalho de Melo 
945ec903f26SJiri Olsa 	evlist->core.has_user_cpus = !!target->cpu_list;
946ec9a77a7SAdrian Hunter 
947453fa030SJiri Olsa 	perf_evlist__set_maps(&evlist->core, cpus, threads);
948d5bc056eSAdrian Hunter 
949d5bc056eSAdrian Hunter 	return 0;
9507e2ed097SArnaldo Carvalho de Melo 
9517e2ed097SArnaldo Carvalho de Melo out_delete_threads:
9527836e52eSJiri Olsa 	perf_thread_map__put(threads);
9537e2ed097SArnaldo Carvalho de Melo 	return -1;
9547e2ed097SArnaldo Carvalho de Melo }
9557e2ed097SArnaldo Carvalho de Melo 
95663503dbaSJiri Olsa void __perf_evlist__set_sample_bit(struct evlist *evlist,
95722c8a376SArnaldo Carvalho de Melo 				   enum perf_event_sample_format bit)
95822c8a376SArnaldo Carvalho de Melo {
95932dcd021SJiri Olsa 	struct evsel *evsel;
96022c8a376SArnaldo Carvalho de Melo 
961e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, evsel)
962862b2f8fSArnaldo Carvalho de Melo 		__evsel__set_sample_bit(evsel, bit);
96322c8a376SArnaldo Carvalho de Melo }
96422c8a376SArnaldo Carvalho de Melo 
96563503dbaSJiri Olsa void __perf_evlist__reset_sample_bit(struct evlist *evlist,
96622c8a376SArnaldo Carvalho de Melo 				     enum perf_event_sample_format bit)
96722c8a376SArnaldo Carvalho de Melo {
96832dcd021SJiri Olsa 	struct evsel *evsel;
96922c8a376SArnaldo Carvalho de Melo 
970e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, evsel)
971862b2f8fSArnaldo Carvalho de Melo 		__evsel__reset_sample_bit(evsel, bit);
97222c8a376SArnaldo Carvalho de Melo }
97322c8a376SArnaldo Carvalho de Melo 
97463503dbaSJiri Olsa int perf_evlist__apply_filters(struct evlist *evlist, struct evsel **err_evsel)
9750a102479SFrederic Weisbecker {
97632dcd021SJiri Olsa 	struct evsel *evsel;
977745cefc5SArnaldo Carvalho de Melo 	int err = 0;
9780a102479SFrederic Weisbecker 
979e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, evsel) {
980745cefc5SArnaldo Carvalho de Melo 		if (evsel->filter == NULL)
9810a102479SFrederic Weisbecker 			continue;
982745cefc5SArnaldo Carvalho de Melo 
983d988d5eeSKan Liang 		/*
984d988d5eeSKan Liang 		 * filters only work for tracepoint event, which doesn't have cpu limit.
985d988d5eeSKan Liang 		 * So evlist and evsel should always be same.
986d988d5eeSKan Liang 		 */
987a00571fdSJiri Olsa 		err = perf_evsel__apply_filter(&evsel->core, evsel->filter);
98823d4aad4SArnaldo Carvalho de Melo 		if (err) {
98923d4aad4SArnaldo Carvalho de Melo 			*err_evsel = evsel;
990745cefc5SArnaldo Carvalho de Melo 			break;
9910a102479SFrederic Weisbecker 		}
99223d4aad4SArnaldo Carvalho de Melo 	}
9930a102479SFrederic Weisbecker 
994745cefc5SArnaldo Carvalho de Melo 	return err;
995745cefc5SArnaldo Carvalho de Melo }
996745cefc5SArnaldo Carvalho de Melo 
99763503dbaSJiri Olsa int perf_evlist__set_tp_filter(struct evlist *evlist, const char *filter)
998745cefc5SArnaldo Carvalho de Melo {
99932dcd021SJiri Olsa 	struct evsel *evsel;
1000745cefc5SArnaldo Carvalho de Melo 	int err = 0;
1001745cefc5SArnaldo Carvalho de Melo 
100205cea449SArnaldo Carvalho de Melo 	if (filter == NULL)
100305cea449SArnaldo Carvalho de Melo 		return -1;
100405cea449SArnaldo Carvalho de Melo 
1005e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, evsel) {
10061fc632ceSJiri Olsa 		if (evsel->core.attr.type != PERF_TYPE_TRACEPOINT)
1007fdf14720SWang Nan 			continue;
1008fdf14720SWang Nan 
1009ad681adfSArnaldo Carvalho de Melo 		err = evsel__set_filter(evsel, filter);
1010745cefc5SArnaldo Carvalho de Melo 		if (err)
1011745cefc5SArnaldo Carvalho de Melo 			break;
1012745cefc5SArnaldo Carvalho de Melo 	}
1013745cefc5SArnaldo Carvalho de Melo 
1014745cefc5SArnaldo Carvalho de Melo 	return err;
10150a102479SFrederic Weisbecker }
101674429964SFrederic Weisbecker 
101753c92f73SArnaldo Carvalho de Melo int perf_evlist__append_tp_filter(struct evlist *evlist, const char *filter)
101853c92f73SArnaldo Carvalho de Melo {
101953c92f73SArnaldo Carvalho de Melo 	struct evsel *evsel;
102053c92f73SArnaldo Carvalho de Melo 	int err = 0;
102153c92f73SArnaldo Carvalho de Melo 
102253c92f73SArnaldo Carvalho de Melo 	if (filter == NULL)
102353c92f73SArnaldo Carvalho de Melo 		return -1;
102453c92f73SArnaldo Carvalho de Melo 
102553c92f73SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, evsel) {
102653c92f73SArnaldo Carvalho de Melo 		if (evsel->core.attr.type != PERF_TYPE_TRACEPOINT)
102753c92f73SArnaldo Carvalho de Melo 			continue;
102853c92f73SArnaldo Carvalho de Melo 
1029ad681adfSArnaldo Carvalho de Melo 		err = evsel__append_tp_filter(evsel, filter);
103053c92f73SArnaldo Carvalho de Melo 		if (err)
103153c92f73SArnaldo Carvalho de Melo 			break;
103253c92f73SArnaldo Carvalho de Melo 	}
103353c92f73SArnaldo Carvalho de Melo 
103453c92f73SArnaldo Carvalho de Melo 	return err;
103553c92f73SArnaldo Carvalho de Melo }
103653c92f73SArnaldo Carvalho de Melo 
1037da949f50SArnaldo Carvalho de Melo char *asprintf__tp_filter_pids(size_t npids, pid_t *pids)
1038cfd70a26SArnaldo Carvalho de Melo {
1039cfd70a26SArnaldo Carvalho de Melo 	char *filter;
1040be199adaSArnaldo Carvalho de Melo 	size_t i;
1041cfd70a26SArnaldo Carvalho de Melo 
1042be199adaSArnaldo Carvalho de Melo 	for (i = 0; i < npids; ++i) {
1043be199adaSArnaldo Carvalho de Melo 		if (i == 0) {
1044be199adaSArnaldo Carvalho de Melo 			if (asprintf(&filter, "common_pid != %d", pids[i]) < 0)
104505cea449SArnaldo Carvalho de Melo 				return NULL;
1046be199adaSArnaldo Carvalho de Melo 		} else {
1047be199adaSArnaldo Carvalho de Melo 			char *tmp;
1048be199adaSArnaldo Carvalho de Melo 
1049be199adaSArnaldo Carvalho de Melo 			if (asprintf(&tmp, "%s && common_pid != %d", filter, pids[i]) < 0)
1050be199adaSArnaldo Carvalho de Melo 				goto out_free;
1051be199adaSArnaldo Carvalho de Melo 
1052be199adaSArnaldo Carvalho de Melo 			free(filter);
1053be199adaSArnaldo Carvalho de Melo 			filter = tmp;
1054be199adaSArnaldo Carvalho de Melo 		}
1055be199adaSArnaldo Carvalho de Melo 	}
1056cfd70a26SArnaldo Carvalho de Melo 
105705cea449SArnaldo Carvalho de Melo 	return filter;
1058be199adaSArnaldo Carvalho de Melo out_free:
1059cfd70a26SArnaldo Carvalho de Melo 	free(filter);
106005cea449SArnaldo Carvalho de Melo 	return NULL;
106105cea449SArnaldo Carvalho de Melo }
106205cea449SArnaldo Carvalho de Melo 
106305cea449SArnaldo Carvalho de Melo int perf_evlist__set_tp_filter_pids(struct evlist *evlist, size_t npids, pid_t *pids)
106405cea449SArnaldo Carvalho de Melo {
106505cea449SArnaldo Carvalho de Melo 	char *filter = asprintf__tp_filter_pids(npids, pids);
106605cea449SArnaldo Carvalho de Melo 	int ret = perf_evlist__set_tp_filter(evlist, filter);
106705cea449SArnaldo Carvalho de Melo 
106805cea449SArnaldo Carvalho de Melo 	free(filter);
1069cfd70a26SArnaldo Carvalho de Melo 	return ret;
1070cfd70a26SArnaldo Carvalho de Melo }
1071cfd70a26SArnaldo Carvalho de Melo 
107263503dbaSJiri Olsa int perf_evlist__set_tp_filter_pid(struct evlist *evlist, pid_t pid)
1073be199adaSArnaldo Carvalho de Melo {
10747ad92a33SArnaldo Carvalho de Melo 	return perf_evlist__set_tp_filter_pids(evlist, 1, &pid);
1075be199adaSArnaldo Carvalho de Melo }
1076be199adaSArnaldo Carvalho de Melo 
10771827ab5bSArnaldo Carvalho de Melo int perf_evlist__append_tp_filter_pids(struct evlist *evlist, size_t npids, pid_t *pids)
10781827ab5bSArnaldo Carvalho de Melo {
10791827ab5bSArnaldo Carvalho de Melo 	char *filter = asprintf__tp_filter_pids(npids, pids);
10801827ab5bSArnaldo Carvalho de Melo 	int ret = perf_evlist__append_tp_filter(evlist, filter);
10811827ab5bSArnaldo Carvalho de Melo 
10821827ab5bSArnaldo Carvalho de Melo 	free(filter);
10831827ab5bSArnaldo Carvalho de Melo 	return ret;
10841827ab5bSArnaldo Carvalho de Melo }
10851827ab5bSArnaldo Carvalho de Melo 
10861827ab5bSArnaldo Carvalho de Melo int perf_evlist__append_tp_filter_pid(struct evlist *evlist, pid_t pid)
10871827ab5bSArnaldo Carvalho de Melo {
10881827ab5bSArnaldo Carvalho de Melo 	return perf_evlist__append_tp_filter_pids(evlist, 1, &pid);
10891827ab5bSArnaldo Carvalho de Melo }
10901827ab5bSArnaldo Carvalho de Melo 
1091b3c2cc2bSArnaldo Carvalho de Melo bool evlist__valid_sample_type(struct evlist *evlist)
109274429964SFrederic Weisbecker {
109332dcd021SJiri Olsa 	struct evsel *pos;
1094c2a70653SArnaldo Carvalho de Melo 
10956484d2f9SJiri Olsa 	if (evlist->core.nr_entries == 1)
109675562573SAdrian Hunter 		return true;
109775562573SAdrian Hunter 
109875562573SAdrian Hunter 	if (evlist->id_pos < 0 || evlist->is_pos < 0)
109975562573SAdrian Hunter 		return false;
110075562573SAdrian Hunter 
1101e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, pos) {
110275562573SAdrian Hunter 		if (pos->id_pos != evlist->id_pos ||
110375562573SAdrian Hunter 		    pos->is_pos != evlist->is_pos)
1104c2a70653SArnaldo Carvalho de Melo 			return false;
110574429964SFrederic Weisbecker 	}
110674429964SFrederic Weisbecker 
1107c2a70653SArnaldo Carvalho de Melo 	return true;
1108c2a70653SArnaldo Carvalho de Melo }
1109c2a70653SArnaldo Carvalho de Melo 
1110b3c2cc2bSArnaldo Carvalho de Melo u64 __evlist__combined_sample_type(struct evlist *evlist)
1111c2a70653SArnaldo Carvalho de Melo {
111232dcd021SJiri Olsa 	struct evsel *evsel;
111375562573SAdrian Hunter 
111475562573SAdrian Hunter 	if (evlist->combined_sample_type)
111575562573SAdrian Hunter 		return evlist->combined_sample_type;
111675562573SAdrian Hunter 
1117e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, evsel)
11181fc632ceSJiri Olsa 		evlist->combined_sample_type |= evsel->core.attr.sample_type;
111975562573SAdrian Hunter 
112075562573SAdrian Hunter 	return evlist->combined_sample_type;
112175562573SAdrian Hunter }
112275562573SAdrian Hunter 
1123b3c2cc2bSArnaldo Carvalho de Melo u64 evlist__combined_sample_type(struct evlist *evlist)
112475562573SAdrian Hunter {
112575562573SAdrian Hunter 	evlist->combined_sample_type = 0;
1126b3c2cc2bSArnaldo Carvalho de Melo 	return __evlist__combined_sample_type(evlist);
1127c2a70653SArnaldo Carvalho de Melo }
1128c2a70653SArnaldo Carvalho de Melo 
112992c7d7cdSArnaldo Carvalho de Melo u64 evlist__combined_branch_type(struct evlist *evlist)
113098df858eSAndi Kleen {
113132dcd021SJiri Olsa 	struct evsel *evsel;
113298df858eSAndi Kleen 	u64 branch_type = 0;
113398df858eSAndi Kleen 
1134e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, evsel)
11351fc632ceSJiri Olsa 		branch_type |= evsel->core.attr.branch_sample_type;
113698df858eSAndi Kleen 	return branch_type;
113798df858eSAndi Kleen }
113898df858eSAndi Kleen 
113963503dbaSJiri Olsa bool perf_evlist__valid_read_format(struct evlist *evlist)
11409ede473cSJiri Olsa {
1141515dbe48SJiri Olsa 	struct evsel *first = evlist__first(evlist), *pos = first;
11421fc632ceSJiri Olsa 	u64 read_format = first->core.attr.read_format;
11431fc632ceSJiri Olsa 	u64 sample_type = first->core.attr.sample_type;
11449ede473cSJiri Olsa 
1145e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, pos) {
114694d3820fSAdrian Hunter 		if (read_format != pos->core.attr.read_format) {
114794d3820fSAdrian Hunter 			pr_debug("Read format differs %#" PRIx64 " vs %#" PRIx64 "\n",
114894d3820fSAdrian Hunter 				 read_format, (u64)pos->core.attr.read_format);
114994d3820fSAdrian Hunter 		}
11509ede473cSJiri Olsa 	}
11519ede473cSJiri Olsa 
11529ede473cSJiri Olsa 	/* PERF_SAMPLE_READ imples PERF_FORMAT_ID. */
11539ede473cSJiri Olsa 	if ((sample_type & PERF_SAMPLE_READ) &&
11549ede473cSJiri Olsa 	    !(read_format & PERF_FORMAT_ID)) {
11559ede473cSJiri Olsa 		return false;
11569ede473cSJiri Olsa 	}
11579ede473cSJiri Olsa 
11589ede473cSJiri Olsa 	return true;
11599ede473cSJiri Olsa }
11609ede473cSJiri Olsa 
116163503dbaSJiri Olsa u16 perf_evlist__id_hdr_size(struct evlist *evlist)
116281e36bffSArnaldo Carvalho de Melo {
1163515dbe48SJiri Olsa 	struct evsel *first = evlist__first(evlist);
116481e36bffSArnaldo Carvalho de Melo 	struct perf_sample *data;
116581e36bffSArnaldo Carvalho de Melo 	u64 sample_type;
116681e36bffSArnaldo Carvalho de Melo 	u16 size = 0;
116781e36bffSArnaldo Carvalho de Melo 
11681fc632ceSJiri Olsa 	if (!first->core.attr.sample_id_all)
116981e36bffSArnaldo Carvalho de Melo 		goto out;
117081e36bffSArnaldo Carvalho de Melo 
11711fc632ceSJiri Olsa 	sample_type = first->core.attr.sample_type;
117281e36bffSArnaldo Carvalho de Melo 
117381e36bffSArnaldo Carvalho de Melo 	if (sample_type & PERF_SAMPLE_TID)
117481e36bffSArnaldo Carvalho de Melo 		size += sizeof(data->tid) * 2;
117581e36bffSArnaldo Carvalho de Melo 
117681e36bffSArnaldo Carvalho de Melo        if (sample_type & PERF_SAMPLE_TIME)
117781e36bffSArnaldo Carvalho de Melo 		size += sizeof(data->time);
117881e36bffSArnaldo Carvalho de Melo 
117981e36bffSArnaldo Carvalho de Melo 	if (sample_type & PERF_SAMPLE_ID)
118081e36bffSArnaldo Carvalho de Melo 		size += sizeof(data->id);
118181e36bffSArnaldo Carvalho de Melo 
118281e36bffSArnaldo Carvalho de Melo 	if (sample_type & PERF_SAMPLE_STREAM_ID)
118381e36bffSArnaldo Carvalho de Melo 		size += sizeof(data->stream_id);
118481e36bffSArnaldo Carvalho de Melo 
118581e36bffSArnaldo Carvalho de Melo 	if (sample_type & PERF_SAMPLE_CPU)
118681e36bffSArnaldo Carvalho de Melo 		size += sizeof(data->cpu) * 2;
118775562573SAdrian Hunter 
118875562573SAdrian Hunter 	if (sample_type & PERF_SAMPLE_IDENTIFIER)
118975562573SAdrian Hunter 		size += sizeof(data->id);
119081e36bffSArnaldo Carvalho de Melo out:
119181e36bffSArnaldo Carvalho de Melo 	return size;
119281e36bffSArnaldo Carvalho de Melo }
119381e36bffSArnaldo Carvalho de Melo 
11948cedf3a5SArnaldo Carvalho de Melo bool evlist__valid_sample_id_all(struct evlist *evlist)
1195c2a70653SArnaldo Carvalho de Melo {
1196515dbe48SJiri Olsa 	struct evsel *first = evlist__first(evlist), *pos = first;
1197c2a70653SArnaldo Carvalho de Melo 
1198e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry_continue(evlist, pos) {
11991fc632ceSJiri Olsa 		if (first->core.attr.sample_id_all != pos->core.attr.sample_id_all)
1200c2a70653SArnaldo Carvalho de Melo 			return false;
1201c2a70653SArnaldo Carvalho de Melo 	}
1202c2a70653SArnaldo Carvalho de Melo 
1203c2a70653SArnaldo Carvalho de Melo 	return true;
120474429964SFrederic Weisbecker }
120574429964SFrederic Weisbecker 
12068cedf3a5SArnaldo Carvalho de Melo bool evlist__sample_id_all(struct evlist *evlist)
120774429964SFrederic Weisbecker {
1208515dbe48SJiri Olsa 	struct evsel *first = evlist__first(evlist);
12091fc632ceSJiri Olsa 	return first->core.attr.sample_id_all;
121074429964SFrederic Weisbecker }
121181cce8deSArnaldo Carvalho de Melo 
121263503dbaSJiri Olsa void perf_evlist__set_selected(struct evlist *evlist,
121332dcd021SJiri Olsa 			       struct evsel *evsel)
121481cce8deSArnaldo Carvalho de Melo {
121581cce8deSArnaldo Carvalho de Melo 	evlist->selected = evsel;
121681cce8deSArnaldo Carvalho de Melo }
1217727ab04eSArnaldo Carvalho de Melo 
1218750b4edeSJiri Olsa void evlist__close(struct evlist *evlist)
1219a74b4b66SNamhyung Kim {
122032dcd021SJiri Olsa 	struct evsel *evsel;
12217736627bSAndi Kleen 	struct affinity affinity;
12227736627bSAndi Kleen 	int cpu, i;
1223a74b4b66SNamhyung Kim 
12247736627bSAndi Kleen 	/*
12257736627bSAndi Kleen 	 * With perf record core.cpus is usually NULL.
12267736627bSAndi Kleen 	 * Use the old method to handle this for now.
12277736627bSAndi Kleen 	 */
12287736627bSAndi Kleen 	if (!evlist->core.cpus) {
1229475fb533SAndi Kleen 		evlist__for_each_entry_reverse(evlist, evsel)
123088761fa1SJiri Olsa 			evsel__close(evsel);
12317736627bSAndi Kleen 		return;
12327736627bSAndi Kleen 	}
12337736627bSAndi Kleen 
12347736627bSAndi Kleen 	if (affinity__setup(&affinity) < 0)
12357736627bSAndi Kleen 		return;
12367736627bSAndi Kleen 	evlist__for_each_cpu(evlist, i, cpu) {
12377736627bSAndi Kleen 		affinity__set(&affinity, cpu);
12387736627bSAndi Kleen 
12397736627bSAndi Kleen 		evlist__for_each_entry_reverse(evlist, evsel) {
12407736627bSAndi Kleen 			if (evsel__cpu_iter_skip(evsel, cpu))
12417736627bSAndi Kleen 			    continue;
12427736627bSAndi Kleen 			perf_evsel__close_cpu(&evsel->core, evsel->cpu_iter - 1);
12437736627bSAndi Kleen 		}
12447736627bSAndi Kleen 	}
12457736627bSAndi Kleen 	affinity__cleanup(&affinity);
12467736627bSAndi Kleen 	evlist__for_each_entry_reverse(evlist, evsel) {
12477736627bSAndi Kleen 		perf_evsel__free_fd(&evsel->core);
12487736627bSAndi Kleen 		perf_evsel__free_id(&evsel->core);
12497736627bSAndi Kleen 	}
1250a74b4b66SNamhyung Kim }
1251a74b4b66SNamhyung Kim 
125263503dbaSJiri Olsa static int perf_evlist__create_syswide_maps(struct evlist *evlist)
12534112eb18SArnaldo Carvalho de Melo {
1254f854839bSJiri Olsa 	struct perf_cpu_map *cpus;
12559749b90eSJiri Olsa 	struct perf_thread_map *threads;
12564112eb18SArnaldo Carvalho de Melo 	int err = -ENOMEM;
12574112eb18SArnaldo Carvalho de Melo 
12584112eb18SArnaldo Carvalho de Melo 	/*
12594112eb18SArnaldo Carvalho de Melo 	 * Try reading /sys/devices/system/cpu/online to get
12604112eb18SArnaldo Carvalho de Melo 	 * an all cpus map.
12614112eb18SArnaldo Carvalho de Melo 	 *
12624112eb18SArnaldo Carvalho de Melo 	 * FIXME: -ENOMEM is the best we can do here, the cpu_map
12634112eb18SArnaldo Carvalho de Melo 	 * code needs an overhaul to properly forward the
12644112eb18SArnaldo Carvalho de Melo 	 * error, and we may not want to do that fallback to a
12654112eb18SArnaldo Carvalho de Melo 	 * default cpu identity map :-\
12664112eb18SArnaldo Carvalho de Melo 	 */
12679c3516d1SJiri Olsa 	cpus = perf_cpu_map__new(NULL);
12688c0498b6SAdrian Hunter 	if (!cpus)
12694112eb18SArnaldo Carvalho de Melo 		goto out;
12704112eb18SArnaldo Carvalho de Melo 
12714b49cce2SJiri Olsa 	threads = perf_thread_map__new_dummy();
12728c0498b6SAdrian Hunter 	if (!threads)
12738c0498b6SAdrian Hunter 		goto out_put;
12744112eb18SArnaldo Carvalho de Melo 
1275453fa030SJiri Olsa 	perf_evlist__set_maps(&evlist->core, cpus, threads);
12764112eb18SArnaldo Carvalho de Melo out:
12774112eb18SArnaldo Carvalho de Melo 	return err;
12788c0498b6SAdrian Hunter out_put:
127938f01d8dSJiri Olsa 	perf_cpu_map__put(cpus);
12804112eb18SArnaldo Carvalho de Melo 	goto out;
12814112eb18SArnaldo Carvalho de Melo }
12824112eb18SArnaldo Carvalho de Melo 
1283474ddc4cSJiri Olsa int evlist__open(struct evlist *evlist)
1284727ab04eSArnaldo Carvalho de Melo {
128532dcd021SJiri Olsa 	struct evsel *evsel;
1286a74b4b66SNamhyung Kim 	int err;
1287727ab04eSArnaldo Carvalho de Melo 
12884112eb18SArnaldo Carvalho de Melo 	/*
12894112eb18SArnaldo Carvalho de Melo 	 * Default: one fd per CPU, all threads, aka systemwide
12904112eb18SArnaldo Carvalho de Melo 	 * as sys_perf_event_open(cpu = -1, thread = -1) is EINVAL
12914112eb18SArnaldo Carvalho de Melo 	 */
129203617c22SJiri Olsa 	if (evlist->core.threads == NULL && evlist->core.cpus == NULL) {
12934112eb18SArnaldo Carvalho de Melo 		err = perf_evlist__create_syswide_maps(evlist);
12944112eb18SArnaldo Carvalho de Melo 		if (err < 0)
12954112eb18SArnaldo Carvalho de Melo 			goto out_err;
12964112eb18SArnaldo Carvalho de Melo 	}
12974112eb18SArnaldo Carvalho de Melo 
1298733cd2feSAdrian Hunter 	perf_evlist__update_id_pos(evlist);
1299733cd2feSAdrian Hunter 
1300e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, evsel) {
1301af663bd0SJiri Olsa 		err = evsel__open(evsel, evsel->core.cpus, evsel->core.threads);
1302727ab04eSArnaldo Carvalho de Melo 		if (err < 0)
1303727ab04eSArnaldo Carvalho de Melo 			goto out_err;
1304727ab04eSArnaldo Carvalho de Melo 	}
1305727ab04eSArnaldo Carvalho de Melo 
1306727ab04eSArnaldo Carvalho de Melo 	return 0;
1307727ab04eSArnaldo Carvalho de Melo out_err:
1308750b4edeSJiri Olsa 	evlist__close(evlist);
130941c21a68SNamhyung Kim 	errno = -err;
1310727ab04eSArnaldo Carvalho de Melo 	return err;
1311727ab04eSArnaldo Carvalho de Melo }
131235b9d88eSArnaldo Carvalho de Melo 
131363503dbaSJiri Olsa int perf_evlist__prepare_workload(struct evlist *evlist, struct target *target,
131455e162eaSNamhyung Kim 				  const char *argv[], bool pipe_output,
1315735f7e0bSArnaldo Carvalho de Melo 				  void (*exec_error)(int signo, siginfo_t *info, void *ucontext))
131635b9d88eSArnaldo Carvalho de Melo {
131735b9d88eSArnaldo Carvalho de Melo 	int child_ready_pipe[2], go_pipe[2];
131835b9d88eSArnaldo Carvalho de Melo 	char bf;
131935b9d88eSArnaldo Carvalho de Melo 
132035b9d88eSArnaldo Carvalho de Melo 	if (pipe(child_ready_pipe) < 0) {
132135b9d88eSArnaldo Carvalho de Melo 		perror("failed to create 'ready' pipe");
132235b9d88eSArnaldo Carvalho de Melo 		return -1;
132335b9d88eSArnaldo Carvalho de Melo 	}
132435b9d88eSArnaldo Carvalho de Melo 
132535b9d88eSArnaldo Carvalho de Melo 	if (pipe(go_pipe) < 0) {
132635b9d88eSArnaldo Carvalho de Melo 		perror("failed to create 'go' pipe");
132735b9d88eSArnaldo Carvalho de Melo 		goto out_close_ready_pipe;
132835b9d88eSArnaldo Carvalho de Melo 	}
132935b9d88eSArnaldo Carvalho de Melo 
133035b9d88eSArnaldo Carvalho de Melo 	evlist->workload.pid = fork();
133135b9d88eSArnaldo Carvalho de Melo 	if (evlist->workload.pid < 0) {
133235b9d88eSArnaldo Carvalho de Melo 		perror("failed to fork");
133335b9d88eSArnaldo Carvalho de Melo 		goto out_close_pipes;
133435b9d88eSArnaldo Carvalho de Melo 	}
133535b9d88eSArnaldo Carvalho de Melo 
133635b9d88eSArnaldo Carvalho de Melo 	if (!evlist->workload.pid) {
13375f1c4225SArnaldo Carvalho de Melo 		int ret;
13385f1c4225SArnaldo Carvalho de Melo 
1339119fa3c9SNamhyung Kim 		if (pipe_output)
134035b9d88eSArnaldo Carvalho de Melo 			dup2(2, 1);
134135b9d88eSArnaldo Carvalho de Melo 
13420817df08SDavid Ahern 		signal(SIGTERM, SIG_DFL);
13430817df08SDavid Ahern 
134435b9d88eSArnaldo Carvalho de Melo 		close(child_ready_pipe[0]);
134535b9d88eSArnaldo Carvalho de Melo 		close(go_pipe[1]);
134635b9d88eSArnaldo Carvalho de Melo 		fcntl(go_pipe[0], F_SETFD, FD_CLOEXEC);
134735b9d88eSArnaldo Carvalho de Melo 
134835b9d88eSArnaldo Carvalho de Melo 		/*
134935b9d88eSArnaldo Carvalho de Melo 		 * Tell the parent we're ready to go
135035b9d88eSArnaldo Carvalho de Melo 		 */
135135b9d88eSArnaldo Carvalho de Melo 		close(child_ready_pipe[1]);
135235b9d88eSArnaldo Carvalho de Melo 
135335b9d88eSArnaldo Carvalho de Melo 		/*
135435b9d88eSArnaldo Carvalho de Melo 		 * Wait until the parent tells us to go.
135535b9d88eSArnaldo Carvalho de Melo 		 */
13565f1c4225SArnaldo Carvalho de Melo 		ret = read(go_pipe[0], &bf, 1);
13575f1c4225SArnaldo Carvalho de Melo 		/*
13585f1c4225SArnaldo Carvalho de Melo 		 * The parent will ask for the execvp() to be performed by
13595f1c4225SArnaldo Carvalho de Melo 		 * writing exactly one byte, in workload.cork_fd, usually via
13605f1c4225SArnaldo Carvalho de Melo 		 * perf_evlist__start_workload().
13615f1c4225SArnaldo Carvalho de Melo 		 *
136220f86fc1SArnaldo Carvalho de Melo 		 * For cancelling the workload without actually running it,
13635f1c4225SArnaldo Carvalho de Melo 		 * the parent will just close workload.cork_fd, without writing
13645f1c4225SArnaldo Carvalho de Melo 		 * anything, i.e. read will return zero and we just exit()
13655f1c4225SArnaldo Carvalho de Melo 		 * here.
13665f1c4225SArnaldo Carvalho de Melo 		 */
13675f1c4225SArnaldo Carvalho de Melo 		if (ret != 1) {
13685f1c4225SArnaldo Carvalho de Melo 			if (ret == -1)
136935b9d88eSArnaldo Carvalho de Melo 				perror("unable to read pipe");
13705f1c4225SArnaldo Carvalho de Melo 			exit(ret);
13715f1c4225SArnaldo Carvalho de Melo 		}
137235b9d88eSArnaldo Carvalho de Melo 
137335b9d88eSArnaldo Carvalho de Melo 		execvp(argv[0], (char **)argv);
137435b9d88eSArnaldo Carvalho de Melo 
1375735f7e0bSArnaldo Carvalho de Melo 		if (exec_error) {
1376f33cbe72SArnaldo Carvalho de Melo 			union sigval val;
1377f33cbe72SArnaldo Carvalho de Melo 
1378f33cbe72SArnaldo Carvalho de Melo 			val.sival_int = errno;
1379f33cbe72SArnaldo Carvalho de Melo 			if (sigqueue(getppid(), SIGUSR1, val))
138035b9d88eSArnaldo Carvalho de Melo 				perror(argv[0]);
1381f33cbe72SArnaldo Carvalho de Melo 		} else
1382f33cbe72SArnaldo Carvalho de Melo 			perror(argv[0]);
138335b9d88eSArnaldo Carvalho de Melo 		exit(-1);
138435b9d88eSArnaldo Carvalho de Melo 	}
138535b9d88eSArnaldo Carvalho de Melo 
1386735f7e0bSArnaldo Carvalho de Melo 	if (exec_error) {
1387735f7e0bSArnaldo Carvalho de Melo 		struct sigaction act = {
1388735f7e0bSArnaldo Carvalho de Melo 			.sa_flags     = SA_SIGINFO,
1389735f7e0bSArnaldo Carvalho de Melo 			.sa_sigaction = exec_error,
1390735f7e0bSArnaldo Carvalho de Melo 		};
1391735f7e0bSArnaldo Carvalho de Melo 		sigaction(SIGUSR1, &act, NULL);
1392735f7e0bSArnaldo Carvalho de Melo 	}
1393735f7e0bSArnaldo Carvalho de Melo 
13941aaf63b1SArnaldo Carvalho de Melo 	if (target__none(target)) {
139503617c22SJiri Olsa 		if (evlist->core.threads == NULL) {
13961aaf63b1SArnaldo Carvalho de Melo 			fprintf(stderr, "FATAL: evlist->threads need to be set at this point (%s:%d).\n",
13971aaf63b1SArnaldo Carvalho de Melo 				__func__, __LINE__);
13981aaf63b1SArnaldo Carvalho de Melo 			goto out_close_pipes;
13991aaf63b1SArnaldo Carvalho de Melo 		}
140003617c22SJiri Olsa 		perf_thread_map__set_pid(evlist->core.threads, 0, evlist->workload.pid);
14011aaf63b1SArnaldo Carvalho de Melo 	}
140235b9d88eSArnaldo Carvalho de Melo 
140335b9d88eSArnaldo Carvalho de Melo 	close(child_ready_pipe[1]);
140435b9d88eSArnaldo Carvalho de Melo 	close(go_pipe[0]);
140535b9d88eSArnaldo Carvalho de Melo 	/*
140635b9d88eSArnaldo Carvalho de Melo 	 * wait for child to settle
140735b9d88eSArnaldo Carvalho de Melo 	 */
140835b9d88eSArnaldo Carvalho de Melo 	if (read(child_ready_pipe[0], &bf, 1) == -1) {
140935b9d88eSArnaldo Carvalho de Melo 		perror("unable to read pipe");
141035b9d88eSArnaldo Carvalho de Melo 		goto out_close_pipes;
141135b9d88eSArnaldo Carvalho de Melo 	}
141235b9d88eSArnaldo Carvalho de Melo 
1413bcf3145fSNamhyung Kim 	fcntl(go_pipe[1], F_SETFD, FD_CLOEXEC);
141435b9d88eSArnaldo Carvalho de Melo 	evlist->workload.cork_fd = go_pipe[1];
141535b9d88eSArnaldo Carvalho de Melo 	close(child_ready_pipe[0]);
141635b9d88eSArnaldo Carvalho de Melo 	return 0;
141735b9d88eSArnaldo Carvalho de Melo 
141835b9d88eSArnaldo Carvalho de Melo out_close_pipes:
141935b9d88eSArnaldo Carvalho de Melo 	close(go_pipe[0]);
142035b9d88eSArnaldo Carvalho de Melo 	close(go_pipe[1]);
142135b9d88eSArnaldo Carvalho de Melo out_close_ready_pipe:
142235b9d88eSArnaldo Carvalho de Melo 	close(child_ready_pipe[0]);
142335b9d88eSArnaldo Carvalho de Melo 	close(child_ready_pipe[1]);
142435b9d88eSArnaldo Carvalho de Melo 	return -1;
142535b9d88eSArnaldo Carvalho de Melo }
142635b9d88eSArnaldo Carvalho de Melo 
142763503dbaSJiri Olsa int perf_evlist__start_workload(struct evlist *evlist)
142835b9d88eSArnaldo Carvalho de Melo {
142935b9d88eSArnaldo Carvalho de Melo 	if (evlist->workload.cork_fd > 0) {
1430b3824404SDavid Ahern 		char bf = 0;
1431bcf3145fSNamhyung Kim 		int ret;
143235b9d88eSArnaldo Carvalho de Melo 		/*
143335b9d88eSArnaldo Carvalho de Melo 		 * Remove the cork, let it rip!
143435b9d88eSArnaldo Carvalho de Melo 		 */
1435bcf3145fSNamhyung Kim 		ret = write(evlist->workload.cork_fd, &bf, 1);
1436bcf3145fSNamhyung Kim 		if (ret < 0)
1437e978be9eSSoramichi Akiyama 			perror("unable to write to pipe");
1438bcf3145fSNamhyung Kim 
1439bcf3145fSNamhyung Kim 		close(evlist->workload.cork_fd);
1440bcf3145fSNamhyung Kim 		return ret;
144135b9d88eSArnaldo Carvalho de Melo 	}
144235b9d88eSArnaldo Carvalho de Melo 
144335b9d88eSArnaldo Carvalho de Melo 	return 0;
144435b9d88eSArnaldo Carvalho de Melo }
1445cb0b29e0SArnaldo Carvalho de Melo 
144663503dbaSJiri Olsa int perf_evlist__parse_sample(struct evlist *evlist, union perf_event *event,
14470807d2d8SArnaldo Carvalho de Melo 			      struct perf_sample *sample)
1448cb0b29e0SArnaldo Carvalho de Melo {
144932dcd021SJiri Olsa 	struct evsel *evsel = perf_evlist__event2evsel(evlist, event);
145075562573SAdrian Hunter 
145175562573SAdrian Hunter 	if (!evsel)
145275562573SAdrian Hunter 		return -EFAULT;
14536b6017a2SArnaldo Carvalho de Melo 	return evsel__parse_sample(evsel, event, sample);
1454cb0b29e0SArnaldo Carvalho de Melo }
145578f067b3SArnaldo Carvalho de Melo 
145663503dbaSJiri Olsa int perf_evlist__parse_sample_timestamp(struct evlist *evlist,
145701468120SJiri Olsa 					union perf_event *event,
145801468120SJiri Olsa 					u64 *timestamp)
145901468120SJiri Olsa {
146032dcd021SJiri Olsa 	struct evsel *evsel = perf_evlist__event2evsel(evlist, event);
146101468120SJiri Olsa 
146201468120SJiri Olsa 	if (!evsel)
146301468120SJiri Olsa 		return -EFAULT;
14646b6017a2SArnaldo Carvalho de Melo 	return evsel__parse_sample_timestamp(evsel, event, timestamp);
146501468120SJiri Olsa }
146601468120SJiri Olsa 
1467d1f249ecSArnaldo Carvalho de Melo int evlist__strerror_open(struct evlist *evlist, int err, char *buf, size_t size)
1468a8f23d8fSArnaldo Carvalho de Melo {
1469a8f23d8fSArnaldo Carvalho de Melo 	int printed, value;
1470c8b5f2c9SArnaldo Carvalho de Melo 	char sbuf[STRERR_BUFSIZE], *emsg = str_error_r(err, sbuf, sizeof(sbuf));
1471a8f23d8fSArnaldo Carvalho de Melo 
1472a8f23d8fSArnaldo Carvalho de Melo 	switch (err) {
1473a8f23d8fSArnaldo Carvalho de Melo 	case EACCES:
1474a8f23d8fSArnaldo Carvalho de Melo 	case EPERM:
1475a8f23d8fSArnaldo Carvalho de Melo 		printed = scnprintf(buf, size,
1476a8f23d8fSArnaldo Carvalho de Melo 				    "Error:\t%s.\n"
1477a8f23d8fSArnaldo Carvalho de Melo 				    "Hint:\tCheck /proc/sys/kernel/perf_event_paranoid setting.", emsg);
1478a8f23d8fSArnaldo Carvalho de Melo 
14791a47245dSAdrian Hunter 		value = perf_event_paranoid();
1480a8f23d8fSArnaldo Carvalho de Melo 
1481a8f23d8fSArnaldo Carvalho de Melo 		printed += scnprintf(buf + printed, size - printed, "\nHint:\t");
1482a8f23d8fSArnaldo Carvalho de Melo 
1483a8f23d8fSArnaldo Carvalho de Melo 		if (value >= 2) {
1484a8f23d8fSArnaldo Carvalho de Melo 			printed += scnprintf(buf + printed, size - printed,
1485a8f23d8fSArnaldo Carvalho de Melo 					     "For your workloads it needs to be <= 1\nHint:\t");
1486a8f23d8fSArnaldo Carvalho de Melo 		}
1487a8f23d8fSArnaldo Carvalho de Melo 		printed += scnprintf(buf + printed, size - printed,
14885229e366SArnaldo Carvalho de Melo 				     "For system wide tracing it needs to be set to -1.\n");
1489a8f23d8fSArnaldo Carvalho de Melo 
1490a8f23d8fSArnaldo Carvalho de Melo 		printed += scnprintf(buf + printed, size - printed,
14915229e366SArnaldo Carvalho de Melo 				    "Hint:\tTry: 'sudo sh -c \"echo -1 > /proc/sys/kernel/perf_event_paranoid\"'\n"
14925229e366SArnaldo Carvalho de Melo 				    "Hint:\tThe current value is %d.", value);
1493a8f23d8fSArnaldo Carvalho de Melo 		break;
1494d9aade7fSArnaldo Carvalho de Melo 	case EINVAL: {
1495515dbe48SJiri Olsa 		struct evsel *first = evlist__first(evlist);
1496d9aade7fSArnaldo Carvalho de Melo 		int max_freq;
1497d9aade7fSArnaldo Carvalho de Melo 
1498d9aade7fSArnaldo Carvalho de Melo 		if (sysctl__read_int("kernel/perf_event_max_sample_rate", &max_freq) < 0)
1499d9aade7fSArnaldo Carvalho de Melo 			goto out_default;
1500d9aade7fSArnaldo Carvalho de Melo 
15011fc632ceSJiri Olsa 		if (first->core.attr.sample_freq < (u64)max_freq)
1502d9aade7fSArnaldo Carvalho de Melo 			goto out_default;
1503d9aade7fSArnaldo Carvalho de Melo 
1504d9aade7fSArnaldo Carvalho de Melo 		printed = scnprintf(buf, size,
1505d9aade7fSArnaldo Carvalho de Melo 				    "Error:\t%s.\n"
1506d9aade7fSArnaldo Carvalho de Melo 				    "Hint:\tCheck /proc/sys/kernel/perf_event_max_sample_rate.\n"
1507d9aade7fSArnaldo Carvalho de Melo 				    "Hint:\tThe current value is %d and %" PRIu64 " is being requested.",
15081fc632ceSJiri Olsa 				    emsg, max_freq, first->core.attr.sample_freq);
1509d9aade7fSArnaldo Carvalho de Melo 		break;
1510d9aade7fSArnaldo Carvalho de Melo 	}
1511a8f23d8fSArnaldo Carvalho de Melo 	default:
1512d9aade7fSArnaldo Carvalho de Melo out_default:
1513a8f23d8fSArnaldo Carvalho de Melo 		scnprintf(buf, size, "%s", emsg);
1514a8f23d8fSArnaldo Carvalho de Melo 		break;
1515a8f23d8fSArnaldo Carvalho de Melo 	}
1516a8f23d8fSArnaldo Carvalho de Melo 
1517a8f23d8fSArnaldo Carvalho de Melo 	return 0;
1518a8f23d8fSArnaldo Carvalho de Melo }
1519a025e4f0SAdrian Hunter 
1520d1f249ecSArnaldo Carvalho de Melo int evlist__strerror_mmap(struct evlist *evlist, int err, char *buf, size_t size)
1521956fa571SArnaldo Carvalho de Melo {
1522c8b5f2c9SArnaldo Carvalho de Melo 	char sbuf[STRERR_BUFSIZE], *emsg = str_error_r(err, sbuf, sizeof(sbuf));
1523f6fa4375SJiri Olsa 	int pages_attempted = evlist->core.mmap_len / 1024, pages_max_per_user, printed = 0;
1524956fa571SArnaldo Carvalho de Melo 
1525956fa571SArnaldo Carvalho de Melo 	switch (err) {
1526956fa571SArnaldo Carvalho de Melo 	case EPERM:
1527e5d4a290SArnaldo Carvalho de Melo 		sysctl__read_int("kernel/perf_event_mlock_kb", &pages_max_per_user);
1528e965bea1SArnaldo Carvalho de Melo 		printed += scnprintf(buf + printed, size - printed,
1529e965bea1SArnaldo Carvalho de Melo 				     "Error:\t%s.\n"
1530956fa571SArnaldo Carvalho de Melo 				     "Hint:\tCheck /proc/sys/kernel/perf_event_mlock_kb (%d kB) setting.\n"
1531e965bea1SArnaldo Carvalho de Melo 				     "Hint:\tTried using %zd kB.\n",
1532e5d4a290SArnaldo Carvalho de Melo 				     emsg, pages_max_per_user, pages_attempted);
1533e965bea1SArnaldo Carvalho de Melo 
1534e965bea1SArnaldo Carvalho de Melo 		if (pages_attempted >= pages_max_per_user) {
1535e965bea1SArnaldo Carvalho de Melo 			printed += scnprintf(buf + printed, size - printed,
1536e965bea1SArnaldo Carvalho de Melo 					     "Hint:\tTry 'sudo sh -c \"echo %d > /proc/sys/kernel/perf_event_mlock_kb\"', or\n",
1537e965bea1SArnaldo Carvalho de Melo 					     pages_max_per_user + pages_attempted);
1538e965bea1SArnaldo Carvalho de Melo 		}
1539e965bea1SArnaldo Carvalho de Melo 
1540e965bea1SArnaldo Carvalho de Melo 		printed += scnprintf(buf + printed, size - printed,
1541e965bea1SArnaldo Carvalho de Melo 				     "Hint:\tTry using a smaller -m/--mmap-pages value.");
1542956fa571SArnaldo Carvalho de Melo 		break;
1543956fa571SArnaldo Carvalho de Melo 	default:
1544956fa571SArnaldo Carvalho de Melo 		scnprintf(buf, size, "%s", emsg);
1545956fa571SArnaldo Carvalho de Melo 		break;
1546956fa571SArnaldo Carvalho de Melo 	}
1547956fa571SArnaldo Carvalho de Melo 
1548956fa571SArnaldo Carvalho de Melo 	return 0;
1549956fa571SArnaldo Carvalho de Melo }
1550956fa571SArnaldo Carvalho de Melo 
155163503dbaSJiri Olsa void perf_evlist__to_front(struct evlist *evlist,
155232dcd021SJiri Olsa 			   struct evsel *move_evsel)
1553a025e4f0SAdrian Hunter {
155432dcd021SJiri Olsa 	struct evsel *evsel, *n;
1555a025e4f0SAdrian Hunter 	LIST_HEAD(move);
1556a025e4f0SAdrian Hunter 
1557515dbe48SJiri Olsa 	if (move_evsel == evlist__first(evlist))
1558a025e4f0SAdrian Hunter 		return;
1559a025e4f0SAdrian Hunter 
1560e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry_safe(evlist, n, evsel) {
1561a025e4f0SAdrian Hunter 		if (evsel->leader == move_evsel->leader)
1562b27c4eceSJiri Olsa 			list_move_tail(&evsel->core.node, &move);
1563a025e4f0SAdrian Hunter 	}
1564a025e4f0SAdrian Hunter 
1565ce9036a6SJiri Olsa 	list_splice(&move, &evlist->core.entries);
1566a025e4f0SAdrian Hunter }
156760b0896cSAdrian Hunter 
1568442ad225SAdrian Hunter struct evsel *perf_evlist__get_tracking_event(struct evlist *evlist)
1569442ad225SAdrian Hunter {
1570442ad225SAdrian Hunter 	struct evsel *evsel;
1571442ad225SAdrian Hunter 
1572442ad225SAdrian Hunter 	evlist__for_each_entry(evlist, evsel) {
1573442ad225SAdrian Hunter 		if (evsel->tracking)
1574442ad225SAdrian Hunter 			return evsel;
1575442ad225SAdrian Hunter 	}
1576442ad225SAdrian Hunter 
1577442ad225SAdrian Hunter 	return evlist__first(evlist);
1578442ad225SAdrian Hunter }
1579442ad225SAdrian Hunter 
158063503dbaSJiri Olsa void perf_evlist__set_tracking_event(struct evlist *evlist,
158132dcd021SJiri Olsa 				     struct evsel *tracking_evsel)
158260b0896cSAdrian Hunter {
158332dcd021SJiri Olsa 	struct evsel *evsel;
158460b0896cSAdrian Hunter 
158560b0896cSAdrian Hunter 	if (tracking_evsel->tracking)
158660b0896cSAdrian Hunter 		return;
158760b0896cSAdrian Hunter 
1588e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, evsel) {
158960b0896cSAdrian Hunter 		if (evsel != tracking_evsel)
159060b0896cSAdrian Hunter 			evsel->tracking = false;
159160b0896cSAdrian Hunter 	}
159260b0896cSAdrian Hunter 
159360b0896cSAdrian Hunter 	tracking_evsel->tracking = true;
159460b0896cSAdrian Hunter }
15957630b3e2SWang Nan 
159632dcd021SJiri Olsa struct evsel *
159763503dbaSJiri Olsa perf_evlist__find_evsel_by_str(struct evlist *evlist,
15987630b3e2SWang Nan 			       const char *str)
15997630b3e2SWang Nan {
160032dcd021SJiri Olsa 	struct evsel *evsel;
16017630b3e2SWang Nan 
1602e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, evsel) {
16037630b3e2SWang Nan 		if (!evsel->name)
16047630b3e2SWang Nan 			continue;
16057630b3e2SWang Nan 		if (strcmp(str, evsel->name) == 0)
16067630b3e2SWang Nan 			return evsel;
16077630b3e2SWang Nan 	}
16087630b3e2SWang Nan 
16097630b3e2SWang Nan 	return NULL;
16107630b3e2SWang Nan }
161154cc54deSWang Nan 
161263503dbaSJiri Olsa void perf_evlist__toggle_bkw_mmap(struct evlist *evlist,
161354cc54deSWang Nan 				  enum bkw_mmap_state state)
161454cc54deSWang Nan {
161554cc54deSWang Nan 	enum bkw_mmap_state old_state = evlist->bkw_mmap_state;
161654cc54deSWang Nan 	enum action {
161754cc54deSWang Nan 		NONE,
161854cc54deSWang Nan 		PAUSE,
161954cc54deSWang Nan 		RESUME,
162054cc54deSWang Nan 	} action = NONE;
162154cc54deSWang Nan 
16220b72d69aSWang Nan 	if (!evlist->overwrite_mmap)
162354cc54deSWang Nan 		return;
162454cc54deSWang Nan 
162554cc54deSWang Nan 	switch (old_state) {
162654cc54deSWang Nan 	case BKW_MMAP_NOTREADY: {
162754cc54deSWang Nan 		if (state != BKW_MMAP_RUNNING)
1628dd8bd53aSLuis de Bethencourt 			goto state_err;
162954cc54deSWang Nan 		break;
163054cc54deSWang Nan 	}
163154cc54deSWang Nan 	case BKW_MMAP_RUNNING: {
163254cc54deSWang Nan 		if (state != BKW_MMAP_DATA_PENDING)
163354cc54deSWang Nan 			goto state_err;
163454cc54deSWang Nan 		action = PAUSE;
163554cc54deSWang Nan 		break;
163654cc54deSWang Nan 	}
163754cc54deSWang Nan 	case BKW_MMAP_DATA_PENDING: {
163854cc54deSWang Nan 		if (state != BKW_MMAP_EMPTY)
163954cc54deSWang Nan 			goto state_err;
164054cc54deSWang Nan 		break;
164154cc54deSWang Nan 	}
164254cc54deSWang Nan 	case BKW_MMAP_EMPTY: {
164354cc54deSWang Nan 		if (state != BKW_MMAP_RUNNING)
164454cc54deSWang Nan 			goto state_err;
164554cc54deSWang Nan 		action = RESUME;
164654cc54deSWang Nan 		break;
164754cc54deSWang Nan 	}
164854cc54deSWang Nan 	default:
164954cc54deSWang Nan 		WARN_ONCE(1, "Shouldn't get there\n");
165054cc54deSWang Nan 	}
165154cc54deSWang Nan 
165254cc54deSWang Nan 	evlist->bkw_mmap_state = state;
165354cc54deSWang Nan 
165454cc54deSWang Nan 	switch (action) {
165554cc54deSWang Nan 	case PAUSE:
165654cc54deSWang Nan 		perf_evlist__pause(evlist);
165754cc54deSWang Nan 		break;
165854cc54deSWang Nan 	case RESUME:
165954cc54deSWang Nan 		perf_evlist__resume(evlist);
166054cc54deSWang Nan 		break;
166154cc54deSWang Nan 	case NONE:
166254cc54deSWang Nan 	default:
166354cc54deSWang Nan 		break;
166454cc54deSWang Nan 	}
166554cc54deSWang Nan 
166654cc54deSWang Nan state_err:
166754cc54deSWang Nan 	return;
166854cc54deSWang Nan }
166907d6f446SArnaldo Carvalho de Melo 
167063503dbaSJiri Olsa bool perf_evlist__exclude_kernel(struct evlist *evlist)
167107d6f446SArnaldo Carvalho de Melo {
167232dcd021SJiri Olsa 	struct evsel *evsel;
167307d6f446SArnaldo Carvalho de Melo 
167407d6f446SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, evsel) {
16751fc632ceSJiri Olsa 		if (!evsel->core.attr.exclude_kernel)
167607d6f446SArnaldo Carvalho de Melo 			return false;
167707d6f446SArnaldo Carvalho de Melo 	}
167807d6f446SArnaldo Carvalho de Melo 
167907d6f446SArnaldo Carvalho de Melo 	return true;
168007d6f446SArnaldo Carvalho de Melo }
1681e2bdbe80SJin Yao 
1682e2bdbe80SJin Yao /*
1683e2bdbe80SJin Yao  * Events in data file are not collect in groups, but we still want
1684e2bdbe80SJin Yao  * the group display. Set the artificial group and set the leader's
1685e2bdbe80SJin Yao  * forced_leader flag to notify the display code.
1686e2bdbe80SJin Yao  */
168763503dbaSJiri Olsa void perf_evlist__force_leader(struct evlist *evlist)
1688e2bdbe80SJin Yao {
1689e2bdbe80SJin Yao 	if (!evlist->nr_groups) {
1690515dbe48SJiri Olsa 		struct evsel *leader = evlist__first(evlist);
1691e2bdbe80SJin Yao 
1692e2bdbe80SJin Yao 		perf_evlist__set_leader(evlist);
1693e2bdbe80SJin Yao 		leader->forced_leader = true;
1694e2bdbe80SJin Yao 	}
1695e2bdbe80SJin Yao }
1696c3537fc2SAndi Kleen 
169763503dbaSJiri Olsa struct evsel *perf_evlist__reset_weak_group(struct evlist *evsel_list,
16984804e011SAndi Kleen 						 struct evsel *evsel,
16994804e011SAndi Kleen 						bool close)
1700c3537fc2SAndi Kleen {
170132dcd021SJiri Olsa 	struct evsel *c2, *leader;
1702c3537fc2SAndi Kleen 	bool is_open = true;
1703c3537fc2SAndi Kleen 
1704c3537fc2SAndi Kleen 	leader = evsel->leader;
1705c3537fc2SAndi Kleen 	pr_debug("Weak group for %s/%d failed\n",
17065643b1a5SJiri Olsa 			leader->name, leader->core.nr_members);
1707c3537fc2SAndi Kleen 
1708c3537fc2SAndi Kleen 	/*
1709c3537fc2SAndi Kleen 	 * for_each_group_member doesn't work here because it doesn't
1710c3537fc2SAndi Kleen 	 * include the first entry.
1711c3537fc2SAndi Kleen 	 */
1712c3537fc2SAndi Kleen 	evlist__for_each_entry(evsel_list, c2) {
1713c3537fc2SAndi Kleen 		if (c2 == evsel)
1714c3537fc2SAndi Kleen 			is_open = false;
1715c3537fc2SAndi Kleen 		if (c2->leader == leader) {
17164804e011SAndi Kleen 			if (is_open && close)
17175a40e199SAndi Kleen 				perf_evsel__close(&c2->core);
1718c3537fc2SAndi Kleen 			c2->leader = c2;
17195643b1a5SJiri Olsa 			c2->core.nr_members = 0;
17204804e011SAndi Kleen 			/*
17214804e011SAndi Kleen 			 * Set this for all former members of the group
17224804e011SAndi Kleen 			 * to indicate they get reopened.
17234804e011SAndi Kleen 			 */
17244804e011SAndi Kleen 			c2->reset_group = true;
1725c3537fc2SAndi Kleen 		}
1726c3537fc2SAndi Kleen 	}
1727c3537fc2SAndi Kleen 	return leader;
1728c3537fc2SAndi Kleen }
1729ec886bf5SAlexey Budankov 
1730a8fcbd26SAdrian Hunter static int evlist__parse_control_fifo(const char *str, int *ctl_fd, int *ctl_fd_ack, bool *ctl_fd_close)
1731a8fcbd26SAdrian Hunter {
1732a8fcbd26SAdrian Hunter 	char *s, *p;
1733a8fcbd26SAdrian Hunter 	int ret = 0, fd;
1734a8fcbd26SAdrian Hunter 
1735a8fcbd26SAdrian Hunter 	if (strncmp(str, "fifo:", 5))
1736a8fcbd26SAdrian Hunter 		return -EINVAL;
1737a8fcbd26SAdrian Hunter 
1738a8fcbd26SAdrian Hunter 	str += 5;
1739a8fcbd26SAdrian Hunter 	if (!*str || *str == ',')
1740a8fcbd26SAdrian Hunter 		return -EINVAL;
1741a8fcbd26SAdrian Hunter 
1742a8fcbd26SAdrian Hunter 	s = strdup(str);
1743a8fcbd26SAdrian Hunter 	if (!s)
1744a8fcbd26SAdrian Hunter 		return -ENOMEM;
1745a8fcbd26SAdrian Hunter 
1746a8fcbd26SAdrian Hunter 	p = strchr(s, ',');
1747a8fcbd26SAdrian Hunter 	if (p)
1748a8fcbd26SAdrian Hunter 		*p = '\0';
1749a8fcbd26SAdrian Hunter 
1750a8fcbd26SAdrian Hunter 	/*
1751a8fcbd26SAdrian Hunter 	 * O_RDWR avoids POLLHUPs which is necessary to allow the other
1752a8fcbd26SAdrian Hunter 	 * end of a FIFO to be repeatedly opened and closed.
1753a8fcbd26SAdrian Hunter 	 */
1754a8fcbd26SAdrian Hunter 	fd = open(s, O_RDWR | O_NONBLOCK | O_CLOEXEC);
1755a8fcbd26SAdrian Hunter 	if (fd < 0) {
1756a8fcbd26SAdrian Hunter 		pr_err("Failed to open '%s'\n", s);
1757a8fcbd26SAdrian Hunter 		ret = -errno;
1758a8fcbd26SAdrian Hunter 		goto out_free;
1759a8fcbd26SAdrian Hunter 	}
1760a8fcbd26SAdrian Hunter 	*ctl_fd = fd;
1761a8fcbd26SAdrian Hunter 	*ctl_fd_close = true;
1762a8fcbd26SAdrian Hunter 
1763a8fcbd26SAdrian Hunter 	if (p && *++p) {
1764a8fcbd26SAdrian Hunter 		/* O_RDWR | O_NONBLOCK means the other end need not be open */
1765a8fcbd26SAdrian Hunter 		fd = open(p, O_RDWR | O_NONBLOCK | O_CLOEXEC);
1766a8fcbd26SAdrian Hunter 		if (fd < 0) {
1767a8fcbd26SAdrian Hunter 			pr_err("Failed to open '%s'\n", p);
1768a8fcbd26SAdrian Hunter 			ret = -errno;
1769a8fcbd26SAdrian Hunter 			goto out_free;
1770a8fcbd26SAdrian Hunter 		}
1771a8fcbd26SAdrian Hunter 		*ctl_fd_ack = fd;
1772a8fcbd26SAdrian Hunter 	}
1773a8fcbd26SAdrian Hunter 
1774a8fcbd26SAdrian Hunter out_free:
1775a8fcbd26SAdrian Hunter 	free(s);
1776a8fcbd26SAdrian Hunter 	return ret;
1777a8fcbd26SAdrian Hunter }
1778a8fcbd26SAdrian Hunter 
1779a8fcbd26SAdrian Hunter int evlist__parse_control(const char *str, int *ctl_fd, int *ctl_fd_ack, bool *ctl_fd_close)
17809864a66dSAdrian Hunter {
17819864a66dSAdrian Hunter 	char *comma = NULL, *endptr = NULL;
17829864a66dSAdrian Hunter 
1783a8fcbd26SAdrian Hunter 	*ctl_fd_close = false;
1784a8fcbd26SAdrian Hunter 
17859864a66dSAdrian Hunter 	if (strncmp(str, "fd:", 3))
1786a8fcbd26SAdrian Hunter 		return evlist__parse_control_fifo(str, ctl_fd, ctl_fd_ack, ctl_fd_close);
17879864a66dSAdrian Hunter 
17889864a66dSAdrian Hunter 	*ctl_fd = strtoul(&str[3], &endptr, 0);
17899864a66dSAdrian Hunter 	if (endptr == &str[3])
17909864a66dSAdrian Hunter 		return -EINVAL;
17919864a66dSAdrian Hunter 
17929864a66dSAdrian Hunter 	comma = strchr(str, ',');
17939864a66dSAdrian Hunter 	if (comma) {
17949864a66dSAdrian Hunter 		if (endptr != comma)
17959864a66dSAdrian Hunter 			return -EINVAL;
17969864a66dSAdrian Hunter 
17979864a66dSAdrian Hunter 		*ctl_fd_ack = strtoul(comma + 1, &endptr, 0);
17989864a66dSAdrian Hunter 		if (endptr == comma + 1 || *endptr != '\0')
17999864a66dSAdrian Hunter 			return -EINVAL;
18009864a66dSAdrian Hunter 	}
18019864a66dSAdrian Hunter 
18029864a66dSAdrian Hunter 	return 0;
18039864a66dSAdrian Hunter }
18049864a66dSAdrian Hunter 
1805ec886bf5SAlexey Budankov int evlist__initialize_ctlfd(struct evlist *evlist, int fd, int ack)
1806ec886bf5SAlexey Budankov {
1807ec886bf5SAlexey Budankov 	if (fd == -1) {
1808ec886bf5SAlexey Budankov 		pr_debug("Control descriptor is not initialized\n");
1809ec886bf5SAlexey Budankov 		return 0;
1810ec886bf5SAlexey Budankov 	}
1811ec886bf5SAlexey Budankov 
1812ec886bf5SAlexey Budankov 	evlist->ctl_fd.pos = perf_evlist__add_pollfd(&evlist->core, fd, NULL, POLLIN,
1813ec886bf5SAlexey Budankov 						     fdarray_flag__nonfilterable);
1814ec886bf5SAlexey Budankov 	if (evlist->ctl_fd.pos < 0) {
1815ec886bf5SAlexey Budankov 		evlist->ctl_fd.pos = -1;
1816ec886bf5SAlexey Budankov 		pr_err("Failed to add ctl fd entry: %m\n");
1817ec886bf5SAlexey Budankov 		return -1;
1818ec886bf5SAlexey Budankov 	}
1819ec886bf5SAlexey Budankov 
1820ec886bf5SAlexey Budankov 	evlist->ctl_fd.fd = fd;
1821ec886bf5SAlexey Budankov 	evlist->ctl_fd.ack = ack;
1822ec886bf5SAlexey Budankov 
1823ec886bf5SAlexey Budankov 	return 0;
1824ec886bf5SAlexey Budankov }
1825ec886bf5SAlexey Budankov 
1826ec886bf5SAlexey Budankov bool evlist__ctlfd_initialized(struct evlist *evlist)
1827ec886bf5SAlexey Budankov {
1828ec886bf5SAlexey Budankov 	return evlist->ctl_fd.pos >= 0;
1829ec886bf5SAlexey Budankov }
1830ec886bf5SAlexey Budankov 
1831ec886bf5SAlexey Budankov int evlist__finalize_ctlfd(struct evlist *evlist)
1832ec886bf5SAlexey Budankov {
1833ec886bf5SAlexey Budankov 	struct pollfd *entries = evlist->core.pollfd.entries;
1834ec886bf5SAlexey Budankov 
1835ec886bf5SAlexey Budankov 	if (!evlist__ctlfd_initialized(evlist))
1836ec886bf5SAlexey Budankov 		return 0;
1837ec886bf5SAlexey Budankov 
1838ec886bf5SAlexey Budankov 	entries[evlist->ctl_fd.pos].fd = -1;
1839ec886bf5SAlexey Budankov 	entries[evlist->ctl_fd.pos].events = 0;
1840ec886bf5SAlexey Budankov 	entries[evlist->ctl_fd.pos].revents = 0;
1841ec886bf5SAlexey Budankov 
1842ec886bf5SAlexey Budankov 	evlist->ctl_fd.pos = -1;
1843ec886bf5SAlexey Budankov 	evlist->ctl_fd.ack = -1;
1844ec886bf5SAlexey Budankov 	evlist->ctl_fd.fd = -1;
1845ec886bf5SAlexey Budankov 
1846ec886bf5SAlexey Budankov 	return 0;
1847ec886bf5SAlexey Budankov }
1848ec886bf5SAlexey Budankov 
1849ec886bf5SAlexey Budankov static int evlist__ctlfd_recv(struct evlist *evlist, enum evlist_ctl_cmd *cmd,
1850ec886bf5SAlexey Budankov 			      char *cmd_data, size_t data_size)
1851ec886bf5SAlexey Budankov {
1852ec886bf5SAlexey Budankov 	int err;
1853ec886bf5SAlexey Budankov 	char c;
1854ec886bf5SAlexey Budankov 	size_t bytes_read = 0;
1855ec886bf5SAlexey Budankov 
185640db8ff5SAdrian Hunter 	*cmd = EVLIST_CTL_CMD_UNSUPPORTED;
1857ec886bf5SAlexey Budankov 	memset(cmd_data, 0, data_size);
1858ec886bf5SAlexey Budankov 	data_size--;
1859ec886bf5SAlexey Budankov 
1860ec886bf5SAlexey Budankov 	do {
1861ec886bf5SAlexey Budankov 		err = read(evlist->ctl_fd.fd, &c, 1);
1862ec886bf5SAlexey Budankov 		if (err > 0) {
1863ec886bf5SAlexey Budankov 			if (c == '\n' || c == '\0')
1864ec886bf5SAlexey Budankov 				break;
1865ec886bf5SAlexey Budankov 			cmd_data[bytes_read++] = c;
1866ec886bf5SAlexey Budankov 			if (bytes_read == data_size)
1867ec886bf5SAlexey Budankov 				break;
186840db8ff5SAdrian Hunter 			continue;
186940db8ff5SAdrian Hunter 		} else if (err == -1) {
187040db8ff5SAdrian Hunter 			if (errno == EINTR)
187140db8ff5SAdrian Hunter 				continue;
187240db8ff5SAdrian Hunter 			if (errno == EAGAIN || errno == EWOULDBLOCK)
187340db8ff5SAdrian Hunter 				err = 0;
187440db8ff5SAdrian Hunter 			else
1875ec886bf5SAlexey Budankov 				pr_err("Failed to read from ctlfd %d: %m\n", evlist->ctl_fd.fd);
1876ec886bf5SAlexey Budankov 		}
187740db8ff5SAdrian Hunter 		break;
1878ec886bf5SAlexey Budankov 	} while (1);
1879ec886bf5SAlexey Budankov 
1880ec886bf5SAlexey Budankov 	pr_debug("Message from ctl_fd: \"%s%s\"\n", cmd_data,
1881ec886bf5SAlexey Budankov 		 bytes_read == data_size ? "" : c == '\n' ? "\\n" : "\\0");
1882ec886bf5SAlexey Budankov 
188340db8ff5SAdrian Hunter 	if (bytes_read > 0) {
1884ec886bf5SAlexey Budankov 		if (!strncmp(cmd_data, EVLIST_CTL_CMD_ENABLE_TAG,
1885ec886bf5SAlexey Budankov 			     (sizeof(EVLIST_CTL_CMD_ENABLE_TAG)-1))) {
1886ec886bf5SAlexey Budankov 			*cmd = EVLIST_CTL_CMD_ENABLE;
1887ec886bf5SAlexey Budankov 		} else if (!strncmp(cmd_data, EVLIST_CTL_CMD_DISABLE_TAG,
1888ec886bf5SAlexey Budankov 				    (sizeof(EVLIST_CTL_CMD_DISABLE_TAG)-1))) {
1889ec886bf5SAlexey Budankov 			*cmd = EVLIST_CTL_CMD_DISABLE;
1890d20aff15SAdrian Hunter 		} else if (!strncmp(cmd_data, EVLIST_CTL_CMD_SNAPSHOT_TAG,
1891d20aff15SAdrian Hunter 				    (sizeof(EVLIST_CTL_CMD_SNAPSHOT_TAG)-1))) {
1892d20aff15SAdrian Hunter 			*cmd = EVLIST_CTL_CMD_SNAPSHOT;
1893d20aff15SAdrian Hunter 			pr_debug("is snapshot\n");
1894ec886bf5SAlexey Budankov 		}
1895ec886bf5SAlexey Budankov 	}
1896ec886bf5SAlexey Budankov 
189740db8ff5SAdrian Hunter 	return bytes_read ? (int)bytes_read : err;
1898ec886bf5SAlexey Budankov }
1899ec886bf5SAlexey Budankov 
1900d20aff15SAdrian Hunter int evlist__ctlfd_ack(struct evlist *evlist)
1901ec886bf5SAlexey Budankov {
1902ec886bf5SAlexey Budankov 	int err;
1903ec886bf5SAlexey Budankov 
1904ec886bf5SAlexey Budankov 	if (evlist->ctl_fd.ack == -1)
1905ec886bf5SAlexey Budankov 		return 0;
1906ec886bf5SAlexey Budankov 
1907ec886bf5SAlexey Budankov 	err = write(evlist->ctl_fd.ack, EVLIST_CTL_CMD_ACK_TAG,
1908ec886bf5SAlexey Budankov 		    sizeof(EVLIST_CTL_CMD_ACK_TAG));
1909ec886bf5SAlexey Budankov 	if (err == -1)
1910ec886bf5SAlexey Budankov 		pr_err("failed to write to ctl_ack_fd %d: %m\n", evlist->ctl_fd.ack);
1911ec886bf5SAlexey Budankov 
1912ec886bf5SAlexey Budankov 	return err;
1913ec886bf5SAlexey Budankov }
1914ec886bf5SAlexey Budankov 
1915ec886bf5SAlexey Budankov int evlist__ctlfd_process(struct evlist *evlist, enum evlist_ctl_cmd *cmd)
1916ec886bf5SAlexey Budankov {
1917ec886bf5SAlexey Budankov 	int err = 0;
1918ec886bf5SAlexey Budankov 	char cmd_data[EVLIST_CTL_CMD_MAX_LEN];
1919ec886bf5SAlexey Budankov 	int ctlfd_pos = evlist->ctl_fd.pos;
1920ec886bf5SAlexey Budankov 	struct pollfd *entries = evlist->core.pollfd.entries;
1921ec886bf5SAlexey Budankov 
1922ec886bf5SAlexey Budankov 	if (!evlist__ctlfd_initialized(evlist) || !entries[ctlfd_pos].revents)
1923ec886bf5SAlexey Budankov 		return 0;
1924ec886bf5SAlexey Budankov 
1925ec886bf5SAlexey Budankov 	if (entries[ctlfd_pos].revents & POLLIN) {
1926ec886bf5SAlexey Budankov 		err = evlist__ctlfd_recv(evlist, cmd, cmd_data,
1927ec886bf5SAlexey Budankov 					 EVLIST_CTL_CMD_MAX_LEN);
1928ec886bf5SAlexey Budankov 		if (err > 0) {
1929ec886bf5SAlexey Budankov 			switch (*cmd) {
1930ec886bf5SAlexey Budankov 			case EVLIST_CTL_CMD_ENABLE:
1931ec886bf5SAlexey Budankov 				evlist__enable(evlist);
1932ec886bf5SAlexey Budankov 				break;
1933ec886bf5SAlexey Budankov 			case EVLIST_CTL_CMD_DISABLE:
1934ec886bf5SAlexey Budankov 				evlist__disable(evlist);
1935ec886bf5SAlexey Budankov 				break;
1936d20aff15SAdrian Hunter 			case EVLIST_CTL_CMD_SNAPSHOT:
1937d20aff15SAdrian Hunter 				break;
1938ec886bf5SAlexey Budankov 			case EVLIST_CTL_CMD_ACK:
1939ec886bf5SAlexey Budankov 			case EVLIST_CTL_CMD_UNSUPPORTED:
1940ec886bf5SAlexey Budankov 			default:
1941ec886bf5SAlexey Budankov 				pr_debug("ctlfd: unsupported %d\n", *cmd);
1942ec886bf5SAlexey Budankov 				break;
1943ec886bf5SAlexey Budankov 			}
1944d20aff15SAdrian Hunter 			if (!(*cmd == EVLIST_CTL_CMD_ACK || *cmd == EVLIST_CTL_CMD_UNSUPPORTED ||
1945d20aff15SAdrian Hunter 			      *cmd == EVLIST_CTL_CMD_SNAPSHOT))
1946ec886bf5SAlexey Budankov 				evlist__ctlfd_ack(evlist);
1947ec886bf5SAlexey Budankov 		}
1948ec886bf5SAlexey Budankov 	}
1949ec886bf5SAlexey Budankov 
1950ec886bf5SAlexey Budankov 	if (entries[ctlfd_pos].revents & (POLLHUP | POLLERR))
1951ec886bf5SAlexey Budankov 		evlist__finalize_ctlfd(evlist);
1952ec886bf5SAlexey Budankov 	else
1953ec886bf5SAlexey Budankov 		entries[ctlfd_pos].revents = 0;
1954ec886bf5SAlexey Budankov 
1955ec886bf5SAlexey Budankov 	return err;
1956ec886bf5SAlexey Budankov }
1957