xref: /linux/tools/perf/util/evlist.c (revision 9e906a9dead17d81d6c2687f65e159231d0e3286)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (C) 2011, Red Hat Inc, Arnaldo Carvalho de Melo <acme@redhat.com>
4  *
5  * Parts came from builtin-{top,stat,record}.c, see those files for further
6  * copyright notes.
7  */
8 #include <api/fs/fs.h>
9 #include <errno.h>
10 #include <inttypes.h>
11 #include <poll.h>
12 #include "cpumap.h"
13 #include "util/mmap.h"
14 #include "thread_map.h"
15 #include "target.h"
16 #include "evlist.h"
17 #include "evsel.h"
18 #include "record.h"
19 #include "debug.h"
20 #include "units.h"
21 #include "bpf_counter.h"
22 #include <internal/lib.h> // page_size
23 #include "affinity.h"
24 #include "../perf.h"
25 #include "asm/bug.h"
26 #include "bpf-event.h"
27 #include "util/event.h"
28 #include "util/string2.h"
29 #include "util/perf_api_probe.h"
30 #include "util/evsel_fprintf.h"
31 #include "util/pmu.h"
32 #include "util/sample.h"
33 #include "util/bpf-filter.h"
34 #include "util/stat.h"
35 #include "util/util.h"
36 #include "util/env.h"
37 #include "util/intel-tpebs.h"
38 #include "util/metricgroup.h"
39 #include "util/strbuf.h"
40 #include <signal.h>
41 #include <unistd.h>
42 #include <sched.h>
43 #include <stdlib.h>
44 
45 #include "parse-events.h"
46 #include <subcmd/parse-options.h>
47 
48 #include <fcntl.h>
49 #include <sys/ioctl.h>
50 #include <sys/mman.h>
51 #include <sys/prctl.h>
52 #include <sys/timerfd.h>
53 #include <sys/wait.h>
54 
55 #include <linux/bitops.h>
56 #include <linux/hash.h>
57 #include <linux/log2.h>
58 #include <linux/err.h>
59 #include <linux/string.h>
60 #include <linux/time64.h>
61 #include <linux/zalloc.h>
62 #include <perf/evlist.h>
63 #include <perf/evsel.h>
64 #include <perf/cpumap.h>
65 #include <perf/mmap.h>
66 
67 #include <internal/xyarray.h>
68 
69 #ifdef LACKS_SIGQUEUE_PROTOTYPE
70 int sigqueue(pid_t pid, int sig, const union sigval value);
71 #endif
72 
73 #define FD(e, x, y) (*(int *)xyarray__entry(e->core.fd, x, y))
74 #define SID(e, x, y) xyarray__entry(e->core.sample_id, x, y)
75 
evlist__init(struct evlist * evlist,struct perf_cpu_map * cpus,struct perf_thread_map * threads)76 void evlist__init(struct evlist *evlist, struct perf_cpu_map *cpus,
77 		  struct perf_thread_map *threads)
78 {
79 	perf_evlist__init(&evlist->core);
80 	perf_evlist__set_maps(&evlist->core, cpus, threads);
81 	evlist->workload.pid = -1;
82 	evlist->bkw_mmap_state = BKW_MMAP_NOTREADY;
83 	evlist->ctl_fd.fd = -1;
84 	evlist->ctl_fd.ack = -1;
85 	evlist->ctl_fd.pos = -1;
86 	evlist->nr_br_cntr = -1;
87 	metricgroup__rblist_init(&evlist->metric_events);
88 	INIT_LIST_HEAD(&evlist->deferred_samples);
89 }
90 
evlist__new(void)91 struct evlist *evlist__new(void)
92 {
93 	struct evlist *evlist = zalloc(sizeof(*evlist));
94 
95 	if (evlist != NULL)
96 		evlist__init(evlist, NULL, NULL);
97 
98 	return evlist;
99 }
100 
evlist__new_default(void)101 struct evlist *evlist__new_default(void)
102 {
103 	struct evlist *evlist = evlist__new();
104 	bool can_profile_kernel;
105 	struct perf_pmu *pmu = NULL;
106 
107 	if (!evlist)
108 		return NULL;
109 
110 	can_profile_kernel = perf_event_paranoid_check(1);
111 
112 	while ((pmu = perf_pmus__scan_core(pmu)) != NULL) {
113 		char buf[256];
114 		int err;
115 
116 		snprintf(buf, sizeof(buf), "%s/cycles/%s", pmu->name,
117 			 can_profile_kernel ? "P" : "Pu");
118 		err = parse_event(evlist, buf);
119 		if (err) {
120 			evlist__delete(evlist);
121 			return NULL;
122 		}
123 	}
124 
125 	if (evlist->core.nr_entries > 1) {
126 		struct evsel *evsel;
127 
128 		evlist__for_each_entry(evlist, evsel)
129 			evsel__set_sample_id(evsel, /*can_sample_identifier=*/false);
130 	}
131 
132 	return evlist;
133 }
134 
evlist__new_dummy(void)135 struct evlist *evlist__new_dummy(void)
136 {
137 	struct evlist *evlist = evlist__new();
138 
139 	if (evlist && evlist__add_dummy(evlist)) {
140 		evlist__delete(evlist);
141 		evlist = NULL;
142 	}
143 
144 	return evlist;
145 }
146 
147 /**
148  * evlist__set_id_pos - set the positions of event ids.
149  * @evlist: selected event list
150  *
151  * Events with compatible sample types all have the same id_pos
152  * and is_pos.  For convenience, put a copy on evlist.
153  */
evlist__set_id_pos(struct evlist * evlist)154 void evlist__set_id_pos(struct evlist *evlist)
155 {
156 	struct evsel *first = evlist__first(evlist);
157 
158 	evlist->id_pos = first->id_pos;
159 	evlist->is_pos = first->is_pos;
160 }
161 
evlist__update_id_pos(struct evlist * evlist)162 static void evlist__update_id_pos(struct evlist *evlist)
163 {
164 	struct evsel *evsel;
165 
166 	evlist__for_each_entry(evlist, evsel)
167 		evsel__calc_id_pos(evsel);
168 
169 	evlist__set_id_pos(evlist);
170 }
171 
evlist__purge(struct evlist * evlist)172 static void evlist__purge(struct evlist *evlist)
173 {
174 	struct evsel *pos, *n;
175 
176 	evlist__for_each_entry_safe(evlist, n, pos) {
177 		list_del_init(&pos->core.node);
178 		pos->evlist = NULL;
179 		evsel__delete(pos);
180 	}
181 
182 	evlist->core.nr_entries = 0;
183 }
184 
evlist__exit(struct evlist * evlist)185 void evlist__exit(struct evlist *evlist)
186 {
187 	metricgroup__rblist_exit(&evlist->metric_events);
188 	event_enable_timer__exit(&evlist->eet);
189 	zfree(&evlist->mmap);
190 	zfree(&evlist->overwrite_mmap);
191 	perf_evlist__exit(&evlist->core);
192 }
193 
evlist__delete(struct evlist * evlist)194 void evlist__delete(struct evlist *evlist)
195 {
196 	if (evlist == NULL)
197 		return;
198 
199 	evlist__free_stats(evlist);
200 	evlist__munmap(evlist);
201 	evlist__close(evlist);
202 	evlist__purge(evlist);
203 	evlist__exit(evlist);
204 	free(evlist);
205 }
206 
evlist__add(struct evlist * evlist,struct evsel * entry)207 void evlist__add(struct evlist *evlist, struct evsel *entry)
208 {
209 	perf_evlist__add(&evlist->core, &entry->core);
210 	entry->evlist = evlist;
211 	entry->tracking = !entry->core.idx;
212 
213 	if (evlist->core.nr_entries == 1)
214 		evlist__set_id_pos(evlist);
215 }
216 
evlist__remove(struct evlist * evlist,struct evsel * evsel)217 void evlist__remove(struct evlist *evlist, struct evsel *evsel)
218 {
219 	evsel->evlist = NULL;
220 	perf_evlist__remove(&evlist->core, &evsel->core);
221 }
222 
evlist__splice_list_tail(struct evlist * evlist,struct list_head * list)223 void evlist__splice_list_tail(struct evlist *evlist, struct list_head *list)
224 {
225 	while (!list_empty(list)) {
226 		struct evsel *evsel, *temp, *leader = NULL;
227 
228 		__evlist__for_each_entry_safe(list, temp, evsel) {
229 			list_del_init(&evsel->core.node);
230 			evlist__add(evlist, evsel);
231 			leader = evsel;
232 			break;
233 		}
234 
235 		__evlist__for_each_entry_safe(list, temp, evsel) {
236 			if (evsel__has_leader(evsel, leader)) {
237 				list_del_init(&evsel->core.node);
238 				evlist__add(evlist, evsel);
239 			}
240 		}
241 	}
242 }
243 
__evlist__set_tracepoints_handlers(struct evlist * evlist,const struct evsel_str_handler * assocs,size_t nr_assocs)244 int __evlist__set_tracepoints_handlers(struct evlist *evlist,
245 				       const struct evsel_str_handler *assocs, size_t nr_assocs)
246 {
247 	size_t i;
248 	int err;
249 
250 	for (i = 0; i < nr_assocs; i++) {
251 		// Adding a handler for an event not in this evlist, just ignore it.
252 		struct evsel *evsel = evlist__find_tracepoint_by_name(evlist, assocs[i].name);
253 		if (evsel == NULL)
254 			continue;
255 
256 		err = -EEXIST;
257 		if (evsel->handler != NULL)
258 			goto out;
259 		evsel->handler = assocs[i].handler;
260 	}
261 
262 	err = 0;
263 out:
264 	return err;
265 }
266 
evlist__set_leader(struct evlist * evlist)267 static void evlist__set_leader(struct evlist *evlist)
268 {
269 	perf_evlist__set_leader(&evlist->core);
270 }
271 
evlist__dummy_event(struct evlist * evlist)272 static struct evsel *evlist__dummy_event(struct evlist *evlist)
273 {
274 	struct perf_event_attr attr = {
275 		.type	= PERF_TYPE_SOFTWARE,
276 		.config = PERF_COUNT_SW_DUMMY,
277 		.size	= sizeof(attr), /* to capture ABI version */
278 		/* Avoid frequency mode for dummy events to avoid associated timers. */
279 		.freq = 0,
280 		.sample_period = 1,
281 	};
282 
283 	return evsel__new_idx(&attr, evlist->core.nr_entries);
284 }
285 
evlist__add_dummy(struct evlist * evlist)286 int evlist__add_dummy(struct evlist *evlist)
287 {
288 	struct evsel *evsel = evlist__dummy_event(evlist);
289 
290 	if (evsel == NULL)
291 		return -ENOMEM;
292 
293 	evlist__add(evlist, evsel);
294 	return 0;
295 }
296 
evlist__add_aux_dummy(struct evlist * evlist,bool system_wide)297 struct evsel *evlist__add_aux_dummy(struct evlist *evlist, bool system_wide)
298 {
299 	struct evsel *evsel = evlist__dummy_event(evlist);
300 
301 	if (!evsel)
302 		return NULL;
303 
304 	evsel->core.attr.exclude_kernel = 1;
305 	evsel->core.attr.exclude_guest = 1;
306 	evsel->core.attr.exclude_hv = 1;
307 	evsel->core.system_wide = system_wide;
308 	evsel->no_aux_samples = true;
309 	evsel->name = strdup("dummy:u");
310 
311 	evlist__add(evlist, evsel);
312 	return evsel;
313 }
314 
315 #ifdef HAVE_LIBTRACEEVENT
evlist__add_sched_switch(struct evlist * evlist,bool system_wide)316 struct evsel *evlist__add_sched_switch(struct evlist *evlist, bool system_wide)
317 {
318 	struct evsel *evsel = evsel__newtp_idx("sched", "sched_switch", 0,
319 					       /*format=*/true);
320 
321 	if (IS_ERR(evsel))
322 		return evsel;
323 
324 	evsel__set_sample_bit(evsel, CPU);
325 	evsel__set_sample_bit(evsel, TIME);
326 
327 	evsel->core.system_wide = system_wide;
328 	evsel->no_aux_samples = true;
329 
330 	evlist__add(evlist, evsel);
331 	return evsel;
332 }
333 #endif
334 
evlist__find_tracepoint_by_name(struct evlist * evlist,const char * name)335 struct evsel *evlist__find_tracepoint_by_name(struct evlist *evlist, const char *name)
336 {
337 	struct evsel *evsel;
338 
339 	evlist__for_each_entry(evlist, evsel) {
340 		if ((evsel->core.attr.type == PERF_TYPE_TRACEPOINT) &&
341 		    (strcmp(evsel->name, name) == 0))
342 			return evsel;
343 	}
344 
345 	return NULL;
346 }
347 
348 #ifdef HAVE_LIBTRACEEVENT
evlist__add_newtp(struct evlist * evlist,const char * sys,const char * name,void * handler)349 int evlist__add_newtp(struct evlist *evlist, const char *sys, const char *name, void *handler)
350 {
351 	struct evsel *evsel = evsel__newtp(sys, name);
352 
353 	if (IS_ERR(evsel))
354 		return -1;
355 
356 	evsel->handler = handler;
357 	evlist__add(evlist, evsel);
358 	return 0;
359 }
360 #endif
361 
evlist__cpu_begin(struct evlist * evlist,struct affinity * affinity)362 struct evlist_cpu_iterator evlist__cpu_begin(struct evlist *evlist, struct affinity *affinity)
363 {
364 	struct evlist_cpu_iterator itr = {
365 		.container = evlist,
366 		.evsel = NULL,
367 		.cpu_map_idx = 0,
368 		.evlist_cpu_map_idx = 0,
369 		.evlist_cpu_map_nr = perf_cpu_map__nr(evlist->core.all_cpus),
370 		.cpu = (struct perf_cpu){ .cpu = -1},
371 		.affinity = affinity,
372 	};
373 
374 	if (evlist__empty(evlist)) {
375 		/* Ensure the empty list doesn't iterate. */
376 		itr.evlist_cpu_map_idx = itr.evlist_cpu_map_nr;
377 	} else {
378 		itr.evsel = evlist__first(evlist);
379 		if (itr.affinity) {
380 			itr.cpu = perf_cpu_map__cpu(evlist->core.all_cpus, 0);
381 			affinity__set(itr.affinity, itr.cpu.cpu);
382 			itr.cpu_map_idx = perf_cpu_map__idx(itr.evsel->core.cpus, itr.cpu);
383 			/*
384 			 * If this CPU isn't in the evsel's cpu map then advance
385 			 * through the list.
386 			 */
387 			if (itr.cpu_map_idx == -1)
388 				evlist_cpu_iterator__next(&itr);
389 		}
390 	}
391 	return itr;
392 }
393 
evlist_cpu_iterator__next(struct evlist_cpu_iterator * evlist_cpu_itr)394 void evlist_cpu_iterator__next(struct evlist_cpu_iterator *evlist_cpu_itr)
395 {
396 	while (evlist_cpu_itr->evsel != evlist__last(evlist_cpu_itr->container)) {
397 		evlist_cpu_itr->evsel = evsel__next(evlist_cpu_itr->evsel);
398 		evlist_cpu_itr->cpu_map_idx =
399 			perf_cpu_map__idx(evlist_cpu_itr->evsel->core.cpus,
400 					  evlist_cpu_itr->cpu);
401 		if (evlist_cpu_itr->cpu_map_idx != -1)
402 			return;
403 	}
404 	evlist_cpu_itr->evlist_cpu_map_idx++;
405 	if (evlist_cpu_itr->evlist_cpu_map_idx < evlist_cpu_itr->evlist_cpu_map_nr) {
406 		evlist_cpu_itr->evsel = evlist__first(evlist_cpu_itr->container);
407 		evlist_cpu_itr->cpu =
408 			perf_cpu_map__cpu(evlist_cpu_itr->container->core.all_cpus,
409 					  evlist_cpu_itr->evlist_cpu_map_idx);
410 		if (evlist_cpu_itr->affinity)
411 			affinity__set(evlist_cpu_itr->affinity, evlist_cpu_itr->cpu.cpu);
412 		evlist_cpu_itr->cpu_map_idx =
413 			perf_cpu_map__idx(evlist_cpu_itr->evsel->core.cpus,
414 					  evlist_cpu_itr->cpu);
415 		/*
416 		 * If this CPU isn't in the evsel's cpu map then advance through
417 		 * the list.
418 		 */
419 		if (evlist_cpu_itr->cpu_map_idx == -1)
420 			evlist_cpu_iterator__next(evlist_cpu_itr);
421 	}
422 }
423 
evlist_cpu_iterator__end(const struct evlist_cpu_iterator * evlist_cpu_itr)424 bool evlist_cpu_iterator__end(const struct evlist_cpu_iterator *evlist_cpu_itr)
425 {
426 	return evlist_cpu_itr->evlist_cpu_map_idx >= evlist_cpu_itr->evlist_cpu_map_nr;
427 }
428 
evsel__strcmp(struct evsel * pos,char * evsel_name)429 static int evsel__strcmp(struct evsel *pos, char *evsel_name)
430 {
431 	if (!evsel_name)
432 		return 0;
433 	if (evsel__is_dummy_event(pos))
434 		return 1;
435 	return !evsel__name_is(pos, evsel_name);
436 }
437 
evlist__is_enabled(struct evlist * evlist)438 static int evlist__is_enabled(struct evlist *evlist)
439 {
440 	struct evsel *pos;
441 
442 	evlist__for_each_entry(evlist, pos) {
443 		if (!evsel__is_group_leader(pos) || !pos->core.fd)
444 			continue;
445 		/* If at least one event is enabled, evlist is enabled. */
446 		if (!pos->disabled)
447 			return true;
448 	}
449 	return false;
450 }
451 
__evlist__disable(struct evlist * evlist,char * evsel_name,bool excl_dummy)452 static void __evlist__disable(struct evlist *evlist, char *evsel_name, bool excl_dummy)
453 {
454 	struct evsel *pos;
455 	struct evlist_cpu_iterator evlist_cpu_itr;
456 	struct affinity saved_affinity, *affinity = NULL;
457 	bool has_imm = false;
458 
459 	// See explanation in evlist__close()
460 	if (!cpu_map__is_dummy(evlist->core.user_requested_cpus)) {
461 		if (affinity__setup(&saved_affinity) < 0)
462 			return;
463 		affinity = &saved_affinity;
464 	}
465 
466 	/* Disable 'immediate' events last */
467 	for (int imm = 0; imm <= 1; imm++) {
468 		evlist__for_each_cpu(evlist_cpu_itr, evlist, affinity) {
469 			pos = evlist_cpu_itr.evsel;
470 			if (evsel__strcmp(pos, evsel_name))
471 				continue;
472 			if (pos->disabled || !evsel__is_group_leader(pos) || !pos->core.fd)
473 				continue;
474 			if (excl_dummy && evsel__is_dummy_event(pos))
475 				continue;
476 			if (pos->immediate)
477 				has_imm = true;
478 			if (pos->immediate != imm)
479 				continue;
480 			evsel__disable_cpu(pos, evlist_cpu_itr.cpu_map_idx);
481 		}
482 		if (!has_imm)
483 			break;
484 	}
485 
486 	affinity__cleanup(affinity);
487 	evlist__for_each_entry(evlist, pos) {
488 		if (evsel__strcmp(pos, evsel_name))
489 			continue;
490 		if (!evsel__is_group_leader(pos) || !pos->core.fd)
491 			continue;
492 		if (excl_dummy && evsel__is_dummy_event(pos))
493 			continue;
494 		pos->disabled = true;
495 	}
496 
497 	/*
498 	 * If we disabled only single event, we need to check
499 	 * the enabled state of the evlist manually.
500 	 */
501 	if (evsel_name)
502 		evlist->enabled = evlist__is_enabled(evlist);
503 	else
504 		evlist->enabled = false;
505 }
506 
evlist__disable(struct evlist * evlist)507 void evlist__disable(struct evlist *evlist)
508 {
509 	__evlist__disable(evlist, NULL, false);
510 }
511 
evlist__disable_non_dummy(struct evlist * evlist)512 void evlist__disable_non_dummy(struct evlist *evlist)
513 {
514 	__evlist__disable(evlist, NULL, true);
515 }
516 
evlist__disable_evsel(struct evlist * evlist,char * evsel_name)517 void evlist__disable_evsel(struct evlist *evlist, char *evsel_name)
518 {
519 	__evlist__disable(evlist, evsel_name, false);
520 }
521 
__evlist__enable(struct evlist * evlist,char * evsel_name,bool excl_dummy)522 static void __evlist__enable(struct evlist *evlist, char *evsel_name, bool excl_dummy)
523 {
524 	struct evsel *pos;
525 	struct evlist_cpu_iterator evlist_cpu_itr;
526 	struct affinity saved_affinity, *affinity = NULL;
527 
528 	// See explanation in evlist__close()
529 	if (!cpu_map__is_dummy(evlist->core.user_requested_cpus)) {
530 		if (affinity__setup(&saved_affinity) < 0)
531 			return;
532 		affinity = &saved_affinity;
533 	}
534 
535 	evlist__for_each_cpu(evlist_cpu_itr, evlist, affinity) {
536 		pos = evlist_cpu_itr.evsel;
537 		if (evsel__strcmp(pos, evsel_name))
538 			continue;
539 		if (!evsel__is_group_leader(pos) || !pos->core.fd)
540 			continue;
541 		if (excl_dummy && evsel__is_dummy_event(pos))
542 			continue;
543 		evsel__enable_cpu(pos, evlist_cpu_itr.cpu_map_idx);
544 	}
545 	affinity__cleanup(affinity);
546 	evlist__for_each_entry(evlist, pos) {
547 		if (evsel__strcmp(pos, evsel_name))
548 			continue;
549 		if (!evsel__is_group_leader(pos) || !pos->core.fd)
550 			continue;
551 		if (excl_dummy && evsel__is_dummy_event(pos))
552 			continue;
553 		pos->disabled = false;
554 	}
555 
556 	/*
557 	 * Even single event sets the 'enabled' for evlist,
558 	 * so the toggle can work properly and toggle to
559 	 * 'disabled' state.
560 	 */
561 	evlist->enabled = true;
562 }
563 
evlist__enable(struct evlist * evlist)564 void evlist__enable(struct evlist *evlist)
565 {
566 	__evlist__enable(evlist, NULL, false);
567 }
568 
evlist__enable_non_dummy(struct evlist * evlist)569 void evlist__enable_non_dummy(struct evlist *evlist)
570 {
571 	__evlist__enable(evlist, NULL, true);
572 }
573 
evlist__enable_evsel(struct evlist * evlist,char * evsel_name)574 void evlist__enable_evsel(struct evlist *evlist, char *evsel_name)
575 {
576 	__evlist__enable(evlist, evsel_name, false);
577 }
578 
evlist__toggle_enable(struct evlist * evlist)579 void evlist__toggle_enable(struct evlist *evlist)
580 {
581 	(evlist->enabled ? evlist__disable : evlist__enable)(evlist);
582 }
583 
evlist__add_pollfd(struct evlist * evlist,int fd)584 int evlist__add_pollfd(struct evlist *evlist, int fd)
585 {
586 	return perf_evlist__add_pollfd(&evlist->core, fd, NULL, POLLIN, fdarray_flag__default);
587 }
588 
evlist__filter_pollfd(struct evlist * evlist,short revents_and_mask)589 int evlist__filter_pollfd(struct evlist *evlist, short revents_and_mask)
590 {
591 	return perf_evlist__filter_pollfd(&evlist->core, revents_and_mask);
592 }
593 
594 #ifdef HAVE_EVENTFD_SUPPORT
evlist__add_wakeup_eventfd(struct evlist * evlist,int fd)595 int evlist__add_wakeup_eventfd(struct evlist *evlist, int fd)
596 {
597 	return perf_evlist__add_pollfd(&evlist->core, fd, NULL, POLLIN,
598 				       fdarray_flag__nonfilterable |
599 				       fdarray_flag__non_perf_event);
600 }
601 #endif
602 
evlist__poll(struct evlist * evlist,int timeout)603 int evlist__poll(struct evlist *evlist, int timeout)
604 {
605 	return perf_evlist__poll(&evlist->core, timeout);
606 }
607 
evlist__id2sid(struct evlist * evlist,u64 id)608 struct perf_sample_id *evlist__id2sid(struct evlist *evlist, u64 id)
609 {
610 	struct hlist_head *head;
611 	struct perf_sample_id *sid;
612 	int hash;
613 
614 	hash = hash_64(id, PERF_EVLIST__HLIST_BITS);
615 	head = &evlist->core.heads[hash];
616 
617 	hlist_for_each_entry(sid, head, node)
618 		if (sid->id == id)
619 			return sid;
620 
621 	return NULL;
622 }
623 
evlist__id2evsel(struct evlist * evlist,u64 id)624 struct evsel *evlist__id2evsel(struct evlist *evlist, u64 id)
625 {
626 	struct perf_sample_id *sid;
627 
628 	if (evlist->core.nr_entries == 1 || !id)
629 		return evlist__first(evlist);
630 
631 	sid = evlist__id2sid(evlist, id);
632 	if (sid)
633 		return container_of(sid->evsel, struct evsel, core);
634 
635 	if (!evlist__sample_id_all(evlist))
636 		return evlist__first(evlist);
637 
638 	return NULL;
639 }
640 
evlist__id2evsel_strict(struct evlist * evlist,u64 id)641 struct evsel *evlist__id2evsel_strict(struct evlist *evlist, u64 id)
642 {
643 	struct perf_sample_id *sid;
644 
645 	if (!id)
646 		return NULL;
647 
648 	sid = evlist__id2sid(evlist, id);
649 	if (sid)
650 		return container_of(sid->evsel, struct evsel, core);
651 
652 	return NULL;
653 }
654 
evlist__event2id(struct evlist * evlist,union perf_event * event,u64 * id)655 static int evlist__event2id(struct evlist *evlist, union perf_event *event, u64 *id)
656 {
657 	const __u64 *array = event->sample.array;
658 	ssize_t n;
659 
660 	n = (event->header.size - sizeof(event->header)) >> 3;
661 
662 	if (event->header.type == PERF_RECORD_SAMPLE) {
663 		if (evlist->id_pos >= n)
664 			return -1;
665 		*id = array[evlist->id_pos];
666 	} else {
667 		if (evlist->is_pos > n)
668 			return -1;
669 		n -= evlist->is_pos;
670 		*id = array[n];
671 	}
672 	return 0;
673 }
674 
evlist__event2evsel(struct evlist * evlist,union perf_event * event)675 struct evsel *evlist__event2evsel(struct evlist *evlist, union perf_event *event)
676 {
677 	struct evsel *first = evlist__first(evlist);
678 	struct hlist_head *head;
679 	struct perf_sample_id *sid;
680 	int hash;
681 	u64 id;
682 
683 	if (evlist->core.nr_entries == 1)
684 		return first;
685 
686 	if (!first->core.attr.sample_id_all &&
687 	    event->header.type != PERF_RECORD_SAMPLE)
688 		return first;
689 
690 	if (evlist__event2id(evlist, event, &id))
691 		return NULL;
692 
693 	/* Synthesized events have an id of zero */
694 	if (!id)
695 		return first;
696 
697 	hash = hash_64(id, PERF_EVLIST__HLIST_BITS);
698 	head = &evlist->core.heads[hash];
699 
700 	hlist_for_each_entry(sid, head, node) {
701 		if (sid->id == id)
702 			return container_of(sid->evsel, struct evsel, core);
703 	}
704 	return NULL;
705 }
706 
evlist__set_paused(struct evlist * evlist,bool value)707 static int evlist__set_paused(struct evlist *evlist, bool value)
708 {
709 	int i;
710 
711 	if (!evlist->overwrite_mmap)
712 		return 0;
713 
714 	for (i = 0; i < evlist->core.nr_mmaps; i++) {
715 		int fd = evlist->overwrite_mmap[i].core.fd;
716 		int err;
717 
718 		if (fd < 0)
719 			continue;
720 		err = ioctl(fd, PERF_EVENT_IOC_PAUSE_OUTPUT, value ? 1 : 0);
721 		if (err)
722 			return err;
723 	}
724 	return 0;
725 }
726 
evlist__pause(struct evlist * evlist)727 static int evlist__pause(struct evlist *evlist)
728 {
729 	return evlist__set_paused(evlist, true);
730 }
731 
evlist__resume(struct evlist * evlist)732 static int evlist__resume(struct evlist *evlist)
733 {
734 	return evlist__set_paused(evlist, false);
735 }
736 
evlist__munmap_nofree(struct evlist * evlist)737 static void evlist__munmap_nofree(struct evlist *evlist)
738 {
739 	int i;
740 
741 	if (evlist->mmap)
742 		for (i = 0; i < evlist->core.nr_mmaps; i++)
743 			perf_mmap__munmap(&evlist->mmap[i].core);
744 
745 	if (evlist->overwrite_mmap)
746 		for (i = 0; i < evlist->core.nr_mmaps; i++)
747 			perf_mmap__munmap(&evlist->overwrite_mmap[i].core);
748 }
749 
evlist__munmap(struct evlist * evlist)750 void evlist__munmap(struct evlist *evlist)
751 {
752 	evlist__munmap_nofree(evlist);
753 	zfree(&evlist->mmap);
754 	zfree(&evlist->overwrite_mmap);
755 }
756 
perf_mmap__unmap_cb(struct perf_mmap * map)757 static void perf_mmap__unmap_cb(struct perf_mmap *map)
758 {
759 	struct mmap *m = container_of(map, struct mmap, core);
760 
761 	mmap__munmap(m);
762 }
763 
evlist__alloc_mmap(struct evlist * evlist,bool overwrite)764 static struct mmap *evlist__alloc_mmap(struct evlist *evlist,
765 				       bool overwrite)
766 {
767 	int i;
768 	struct mmap *map;
769 
770 	map = zalloc(evlist->core.nr_mmaps * sizeof(struct mmap));
771 	if (!map)
772 		return NULL;
773 
774 	for (i = 0; i < evlist->core.nr_mmaps; i++) {
775 		struct perf_mmap *prev = i ? &map[i - 1].core : NULL;
776 
777 		/*
778 		 * When the perf_mmap() call is made we grab one refcount, plus
779 		 * one extra to let perf_mmap__consume() get the last
780 		 * events after all real references (perf_mmap__get()) are
781 		 * dropped.
782 		 *
783 		 * Each PERF_EVENT_IOC_SET_OUTPUT points to this mmap and
784 		 * thus does perf_mmap__get() on it.
785 		 */
786 		perf_mmap__init(&map[i].core, prev, overwrite, perf_mmap__unmap_cb);
787 	}
788 
789 	return map;
790 }
791 
792 static void
perf_evlist__mmap_cb_idx(struct perf_evlist * _evlist,struct perf_evsel * _evsel,struct perf_mmap_param * _mp,int idx)793 perf_evlist__mmap_cb_idx(struct perf_evlist *_evlist,
794 			 struct perf_evsel *_evsel,
795 			 struct perf_mmap_param *_mp,
796 			 int idx)
797 {
798 	struct evlist *evlist = container_of(_evlist, struct evlist, core);
799 	struct mmap_params *mp = container_of(_mp, struct mmap_params, core);
800 	struct evsel *evsel = container_of(_evsel, struct evsel, core);
801 
802 	auxtrace_mmap_params__set_idx(&mp->auxtrace_mp, evlist, evsel, idx);
803 }
804 
805 static struct perf_mmap*
perf_evlist__mmap_cb_get(struct perf_evlist * _evlist,bool overwrite,int idx)806 perf_evlist__mmap_cb_get(struct perf_evlist *_evlist, bool overwrite, int idx)
807 {
808 	struct evlist *evlist = container_of(_evlist, struct evlist, core);
809 	struct mmap *maps;
810 
811 	maps = overwrite ? evlist->overwrite_mmap : evlist->mmap;
812 
813 	if (!maps) {
814 		maps = evlist__alloc_mmap(evlist, overwrite);
815 		if (!maps)
816 			return NULL;
817 
818 		if (overwrite) {
819 			evlist->overwrite_mmap = maps;
820 			if (evlist->bkw_mmap_state == BKW_MMAP_NOTREADY)
821 				evlist__toggle_bkw_mmap(evlist, BKW_MMAP_RUNNING);
822 		} else {
823 			evlist->mmap = maps;
824 		}
825 	}
826 
827 	return &maps[idx].core;
828 }
829 
830 static int
perf_evlist__mmap_cb_mmap(struct perf_mmap * _map,struct perf_mmap_param * _mp,int output,struct perf_cpu cpu)831 perf_evlist__mmap_cb_mmap(struct perf_mmap *_map, struct perf_mmap_param *_mp,
832 			  int output, struct perf_cpu cpu)
833 {
834 	struct mmap *map = container_of(_map, struct mmap, core);
835 	struct mmap_params *mp = container_of(_mp, struct mmap_params, core);
836 
837 	return mmap__mmap(map, mp, output, cpu);
838 }
839 
perf_event_mlock_kb_in_pages(void)840 unsigned long perf_event_mlock_kb_in_pages(void)
841 {
842 	unsigned long pages;
843 	int max;
844 
845 	if (sysctl__read_int("kernel/perf_event_mlock_kb", &max) < 0) {
846 		/*
847 		 * Pick a once upon a time good value, i.e. things look
848 		 * strange since we can't read a sysctl value, but lets not
849 		 * die yet...
850 		 */
851 		max = 512;
852 	} else {
853 		max -= (page_size / 1024);
854 	}
855 
856 	pages = (max * 1024) / page_size;
857 	if (!is_power_of_2(pages))
858 		pages = rounddown_pow_of_two(pages);
859 
860 	return pages;
861 }
862 
evlist__mmap_size(unsigned long pages)863 size_t evlist__mmap_size(unsigned long pages)
864 {
865 	if (pages == UINT_MAX)
866 		pages = perf_event_mlock_kb_in_pages();
867 	else if (!is_power_of_2(pages))
868 		return 0;
869 
870 	return (pages + 1) * page_size;
871 }
872 
parse_pages_arg(const char * str,unsigned long min,unsigned long max)873 static long parse_pages_arg(const char *str, unsigned long min,
874 			    unsigned long max)
875 {
876 	unsigned long pages, val;
877 	static struct parse_tag tags[] = {
878 		{ .tag  = 'B', .mult = 1       },
879 		{ .tag  = 'K', .mult = 1 << 10 },
880 		{ .tag  = 'M', .mult = 1 << 20 },
881 		{ .tag  = 'G', .mult = 1 << 30 },
882 		{ .tag  = 0 },
883 	};
884 
885 	if (str == NULL)
886 		return -EINVAL;
887 
888 	val = parse_tag_value(str, tags);
889 	if (val != (unsigned long) -1) {
890 		/* we got file size value */
891 		pages = PERF_ALIGN(val, page_size) / page_size;
892 	} else {
893 		/* we got pages count value */
894 		char *eptr;
895 		pages = strtoul(str, &eptr, 10);
896 		if (*eptr != '\0')
897 			return -EINVAL;
898 	}
899 
900 	if (pages == 0 && min == 0) {
901 		/* leave number of pages at 0 */
902 	} else if (!is_power_of_2(pages)) {
903 		char buf[100];
904 
905 		/* round pages up to next power of 2 */
906 		pages = roundup_pow_of_two(pages);
907 		if (!pages)
908 			return -EINVAL;
909 
910 		unit_number__scnprintf(buf, sizeof(buf), pages * page_size);
911 		pr_info("rounding mmap pages size to %s (%lu pages)\n",
912 			buf, pages);
913 	}
914 
915 	if (pages > max)
916 		return -EINVAL;
917 
918 	return pages;
919 }
920 
__evlist__parse_mmap_pages(unsigned int * mmap_pages,const char * str)921 int __evlist__parse_mmap_pages(unsigned int *mmap_pages, const char *str)
922 {
923 	unsigned long max = UINT_MAX;
924 	long pages;
925 
926 	if (max > SIZE_MAX / page_size)
927 		max = SIZE_MAX / page_size;
928 
929 	pages = parse_pages_arg(str, 1, max);
930 	if (pages < 0) {
931 		pr_err("Invalid argument for --mmap_pages/-m\n");
932 		return -1;
933 	}
934 
935 	*mmap_pages = pages;
936 	return 0;
937 }
938 
evlist__parse_mmap_pages(const struct option * opt,const char * str,int unset __maybe_unused)939 int evlist__parse_mmap_pages(const struct option *opt, const char *str, int unset __maybe_unused)
940 {
941 	return __evlist__parse_mmap_pages(opt->value, str);
942 }
943 
944 /**
945  * evlist__mmap_ex - Create mmaps to receive events.
946  * @evlist: list of events
947  * @pages: map length in pages
948  * @overwrite: overwrite older events?
949  * @auxtrace_pages - auxtrace map length in pages
950  * @auxtrace_overwrite - overwrite older auxtrace data?
951  *
952  * If @overwrite is %false the user needs to signal event consumption using
953  * perf_mmap__write_tail().  Using evlist__mmap_read() does this
954  * automatically.
955  *
956  * Similarly, if @auxtrace_overwrite is %false the user needs to signal data
957  * consumption using auxtrace_mmap__write_tail().
958  *
959  * Return: %0 on success, negative error code otherwise.
960  */
evlist__mmap_ex(struct evlist * evlist,unsigned int pages,unsigned int auxtrace_pages,bool auxtrace_overwrite,int nr_cblocks,int affinity,int flush,int comp_level)961 int evlist__mmap_ex(struct evlist *evlist, unsigned int pages,
962 			 unsigned int auxtrace_pages,
963 			 bool auxtrace_overwrite, int nr_cblocks, int affinity, int flush,
964 			 int comp_level)
965 {
966 	/*
967 	 * Delay setting mp.prot: set it before calling perf_mmap__mmap.
968 	 * Its value is decided by evsel's write_backward.
969 	 * So &mp should not be passed through const pointer.
970 	 */
971 	struct mmap_params mp = {
972 		.nr_cblocks	= nr_cblocks,
973 		.affinity	= affinity,
974 		.flush		= flush,
975 		.comp_level	= comp_level
976 	};
977 	struct perf_evlist_mmap_ops ops = {
978 		.idx  = perf_evlist__mmap_cb_idx,
979 		.get  = perf_evlist__mmap_cb_get,
980 		.mmap = perf_evlist__mmap_cb_mmap,
981 	};
982 
983 	evlist->core.mmap_len = evlist__mmap_size(pages);
984 	pr_debug("mmap size %zuB\n", evlist->core.mmap_len);
985 
986 	auxtrace_mmap_params__init(&mp.auxtrace_mp, evlist->core.mmap_len,
987 				   auxtrace_pages, auxtrace_overwrite);
988 
989 	return perf_evlist__mmap_ops(&evlist->core, &ops, &mp.core);
990 }
991 
evlist__mmap(struct evlist * evlist,unsigned int pages)992 int evlist__mmap(struct evlist *evlist, unsigned int pages)
993 {
994 	return evlist__mmap_ex(evlist, pages, 0, false, 0, PERF_AFFINITY_SYS, 1, 0);
995 }
996 
evlist__create_maps(struct evlist * evlist,struct target * target)997 int evlist__create_maps(struct evlist *evlist, struct target *target)
998 {
999 	bool all_threads = (target->per_thread && target->system_wide);
1000 	struct perf_cpu_map *cpus;
1001 	struct perf_thread_map *threads;
1002 
1003 	/*
1004 	 * If specify '-a' and '--per-thread' to perf record, perf record
1005 	 * will override '--per-thread'. target->per_thread = false and
1006 	 * target->system_wide = true.
1007 	 *
1008 	 * If specify '--per-thread' only to perf record,
1009 	 * target->per_thread = true and target->system_wide = false.
1010 	 *
1011 	 * So target->per_thread && target->system_wide is false.
1012 	 * For perf record, thread_map__new_str doesn't call
1013 	 * thread_map__new_all_cpus. That will keep perf record's
1014 	 * current behavior.
1015 	 *
1016 	 * For perf stat, it allows the case that target->per_thread and
1017 	 * target->system_wide are all true. It means to collect system-wide
1018 	 * per-thread data. thread_map__new_str will call
1019 	 * thread_map__new_all_cpus to enumerate all threads.
1020 	 */
1021 	threads = thread_map__new_str(target->pid, target->tid, all_threads);
1022 
1023 	if (!threads)
1024 		return -1;
1025 
1026 	if (target__uses_dummy_map(target) && !evlist__has_bpf_output(evlist))
1027 		cpus = perf_cpu_map__new_any_cpu();
1028 	else
1029 		cpus = perf_cpu_map__new(target->cpu_list);
1030 
1031 	if (!cpus)
1032 		goto out_delete_threads;
1033 
1034 	evlist->core.has_user_cpus = !!target->cpu_list;
1035 
1036 	perf_evlist__set_maps(&evlist->core, cpus, threads);
1037 
1038 	/* as evlist now has references, put count here */
1039 	perf_cpu_map__put(cpus);
1040 	perf_thread_map__put(threads);
1041 
1042 	return 0;
1043 
1044 out_delete_threads:
1045 	perf_thread_map__put(threads);
1046 	return -1;
1047 }
1048 
evlist__apply_filters(struct evlist * evlist,struct evsel ** err_evsel,struct target * target)1049 int evlist__apply_filters(struct evlist *evlist, struct evsel **err_evsel,
1050 			  struct target *target)
1051 {
1052 	struct evsel *evsel;
1053 	int err = 0;
1054 
1055 	evlist__for_each_entry(evlist, evsel) {
1056 		/*
1057 		 * filters only work for tracepoint event, which doesn't have cpu limit.
1058 		 * So evlist and evsel should always be same.
1059 		 */
1060 		if (evsel->filter) {
1061 			err = perf_evsel__apply_filter(&evsel->core, evsel->filter);
1062 			if (err) {
1063 				*err_evsel = evsel;
1064 				break;
1065 			}
1066 		}
1067 
1068 		/*
1069 		 * non-tracepoint events can have BPF filters.
1070 		 */
1071 		if (!list_empty(&evsel->bpf_filters)) {
1072 			err = perf_bpf_filter__prepare(evsel, target);
1073 			if (err) {
1074 				*err_evsel = evsel;
1075 				break;
1076 			}
1077 		}
1078 	}
1079 
1080 	return err;
1081 }
1082 
evlist__set_tp_filter(struct evlist * evlist,const char * filter)1083 int evlist__set_tp_filter(struct evlist *evlist, const char *filter)
1084 {
1085 	struct evsel *evsel;
1086 	int err = 0;
1087 
1088 	if (filter == NULL)
1089 		return -1;
1090 
1091 	evlist__for_each_entry(evlist, evsel) {
1092 		if (evsel->core.attr.type != PERF_TYPE_TRACEPOINT)
1093 			continue;
1094 
1095 		err = evsel__set_filter(evsel, filter);
1096 		if (err)
1097 			break;
1098 	}
1099 
1100 	return err;
1101 }
1102 
evlist__append_tp_filter(struct evlist * evlist,const char * filter)1103 int evlist__append_tp_filter(struct evlist *evlist, const char *filter)
1104 {
1105 	struct evsel *evsel;
1106 	int err = 0;
1107 
1108 	if (filter == NULL)
1109 		return -1;
1110 
1111 	evlist__for_each_entry(evlist, evsel) {
1112 		if (evsel->core.attr.type != PERF_TYPE_TRACEPOINT)
1113 			continue;
1114 
1115 		err = evsel__append_tp_filter(evsel, filter);
1116 		if (err)
1117 			break;
1118 	}
1119 
1120 	return err;
1121 }
1122 
asprintf__tp_filter_pids(size_t npids,pid_t * pids)1123 char *asprintf__tp_filter_pids(size_t npids, pid_t *pids)
1124 {
1125 	char *filter;
1126 	size_t i;
1127 
1128 	for (i = 0; i < npids; ++i) {
1129 		if (i == 0) {
1130 			if (asprintf(&filter, "common_pid != %d", pids[i]) < 0)
1131 				return NULL;
1132 		} else {
1133 			char *tmp;
1134 
1135 			if (asprintf(&tmp, "%s && common_pid != %d", filter, pids[i]) < 0)
1136 				goto out_free;
1137 
1138 			free(filter);
1139 			filter = tmp;
1140 		}
1141 	}
1142 
1143 	return filter;
1144 out_free:
1145 	free(filter);
1146 	return NULL;
1147 }
1148 
evlist__set_tp_filter_pids(struct evlist * evlist,size_t npids,pid_t * pids)1149 int evlist__set_tp_filter_pids(struct evlist *evlist, size_t npids, pid_t *pids)
1150 {
1151 	char *filter = asprintf__tp_filter_pids(npids, pids);
1152 	int ret = evlist__set_tp_filter(evlist, filter);
1153 
1154 	free(filter);
1155 	return ret;
1156 }
1157 
evlist__append_tp_filter_pids(struct evlist * evlist,size_t npids,pid_t * pids)1158 int evlist__append_tp_filter_pids(struct evlist *evlist, size_t npids, pid_t *pids)
1159 {
1160 	char *filter = asprintf__tp_filter_pids(npids, pids);
1161 	int ret = evlist__append_tp_filter(evlist, filter);
1162 
1163 	free(filter);
1164 	return ret;
1165 }
1166 
evlist__append_tp_filter_pid(struct evlist * evlist,pid_t pid)1167 int evlist__append_tp_filter_pid(struct evlist *evlist, pid_t pid)
1168 {
1169 	return evlist__append_tp_filter_pids(evlist, 1, &pid);
1170 }
1171 
evlist__valid_sample_type(struct evlist * evlist)1172 bool evlist__valid_sample_type(struct evlist *evlist)
1173 {
1174 	struct evsel *pos;
1175 
1176 	if (evlist->core.nr_entries == 1)
1177 		return true;
1178 
1179 	if (evlist->id_pos < 0 || evlist->is_pos < 0)
1180 		return false;
1181 
1182 	evlist__for_each_entry(evlist, pos) {
1183 		if (pos->id_pos != evlist->id_pos ||
1184 		    pos->is_pos != evlist->is_pos)
1185 			return false;
1186 	}
1187 
1188 	return true;
1189 }
1190 
__evlist__combined_sample_type(struct evlist * evlist)1191 u64 __evlist__combined_sample_type(struct evlist *evlist)
1192 {
1193 	struct evsel *evsel;
1194 
1195 	if (evlist->combined_sample_type)
1196 		return evlist->combined_sample_type;
1197 
1198 	evlist__for_each_entry(evlist, evsel)
1199 		evlist->combined_sample_type |= evsel->core.attr.sample_type;
1200 
1201 	return evlist->combined_sample_type;
1202 }
1203 
evlist__combined_sample_type(struct evlist * evlist)1204 u64 evlist__combined_sample_type(struct evlist *evlist)
1205 {
1206 	evlist->combined_sample_type = 0;
1207 	return __evlist__combined_sample_type(evlist);
1208 }
1209 
evlist__combined_branch_type(struct evlist * evlist)1210 u64 evlist__combined_branch_type(struct evlist *evlist)
1211 {
1212 	struct evsel *evsel;
1213 	u64 branch_type = 0;
1214 
1215 	evlist__for_each_entry(evlist, evsel)
1216 		branch_type |= evsel->core.attr.branch_sample_type;
1217 	return branch_type;
1218 }
1219 
1220 static struct evsel *
evlist__find_dup_event_from_prev(struct evlist * evlist,struct evsel * event)1221 evlist__find_dup_event_from_prev(struct evlist *evlist, struct evsel *event)
1222 {
1223 	struct evsel *pos;
1224 
1225 	evlist__for_each_entry(evlist, pos) {
1226 		if (event == pos)
1227 			break;
1228 		if ((pos->core.attr.branch_sample_type & PERF_SAMPLE_BRANCH_COUNTERS) &&
1229 		    !strcmp(pos->name, event->name))
1230 			return pos;
1231 	}
1232 	return NULL;
1233 }
1234 
1235 #define MAX_NR_ABBR_NAME	(26 * 11)
1236 
1237 /*
1238  * The abbr name is from A to Z9. If the number of event
1239  * which requires the branch counter > MAX_NR_ABBR_NAME,
1240  * return NA.
1241  */
evlist__new_abbr_name(char * name)1242 static void evlist__new_abbr_name(char *name)
1243 {
1244 	static int idx;
1245 	int i = idx / 26;
1246 
1247 	if (idx >= MAX_NR_ABBR_NAME) {
1248 		name[0] = 'N';
1249 		name[1] = 'A';
1250 		name[2] = '\0';
1251 		return;
1252 	}
1253 
1254 	name[0] = 'A' + (idx % 26);
1255 
1256 	if (!i)
1257 		name[1] = '\0';
1258 	else {
1259 		name[1] = '0' + i - 1;
1260 		name[2] = '\0';
1261 	}
1262 
1263 	idx++;
1264 }
1265 
evlist__update_br_cntr(struct evlist * evlist)1266 void evlist__update_br_cntr(struct evlist *evlist)
1267 {
1268 	struct evsel *evsel, *dup;
1269 	int i = 0;
1270 
1271 	evlist__for_each_entry(evlist, evsel) {
1272 		if (evsel->core.attr.branch_sample_type & PERF_SAMPLE_BRANCH_COUNTERS) {
1273 			evsel->br_cntr_idx = i++;
1274 			evsel__leader(evsel)->br_cntr_nr++;
1275 
1276 			dup = evlist__find_dup_event_from_prev(evlist, evsel);
1277 			if (dup)
1278 				memcpy(evsel->abbr_name, dup->abbr_name, 3 * sizeof(char));
1279 			else
1280 				evlist__new_abbr_name(evsel->abbr_name);
1281 		}
1282 	}
1283 	evlist->nr_br_cntr = i;
1284 }
1285 
evlist__valid_read_format(struct evlist * evlist)1286 bool evlist__valid_read_format(struct evlist *evlist)
1287 {
1288 	struct evsel *first = evlist__first(evlist), *pos = first;
1289 	u64 read_format = first->core.attr.read_format;
1290 	u64 sample_type = first->core.attr.sample_type;
1291 
1292 	evlist__for_each_entry(evlist, pos) {
1293 		if (read_format != pos->core.attr.read_format) {
1294 			pr_debug("Read format differs %#" PRIx64 " vs %#" PRIx64 "\n",
1295 				 read_format, (u64)pos->core.attr.read_format);
1296 		}
1297 	}
1298 
1299 	/* PERF_SAMPLE_READ implies PERF_FORMAT_ID. */
1300 	if ((sample_type & PERF_SAMPLE_READ) &&
1301 	    !(read_format & PERF_FORMAT_ID)) {
1302 		return false;
1303 	}
1304 
1305 	return true;
1306 }
1307 
evlist__id_hdr_size(struct evlist * evlist)1308 u16 evlist__id_hdr_size(struct evlist *evlist)
1309 {
1310 	struct evsel *first = evlist__first(evlist);
1311 
1312 	return first->core.attr.sample_id_all ? evsel__id_hdr_size(first) : 0;
1313 }
1314 
evlist__valid_sample_id_all(struct evlist * evlist)1315 bool evlist__valid_sample_id_all(struct evlist *evlist)
1316 {
1317 	struct evsel *first = evlist__first(evlist), *pos = first;
1318 
1319 	evlist__for_each_entry_continue(evlist, pos) {
1320 		if (first->core.attr.sample_id_all != pos->core.attr.sample_id_all)
1321 			return false;
1322 	}
1323 
1324 	return true;
1325 }
1326 
evlist__sample_id_all(struct evlist * evlist)1327 bool evlist__sample_id_all(struct evlist *evlist)
1328 {
1329 	struct evsel *first = evlist__first(evlist);
1330 	return first->core.attr.sample_id_all;
1331 }
1332 
evlist__set_selected(struct evlist * evlist,struct evsel * evsel)1333 void evlist__set_selected(struct evlist *evlist, struct evsel *evsel)
1334 {
1335 	evlist->selected = evsel;
1336 }
1337 
evlist__close(struct evlist * evlist)1338 void evlist__close(struct evlist *evlist)
1339 {
1340 	struct evsel *evsel;
1341 	struct evlist_cpu_iterator evlist_cpu_itr;
1342 	struct affinity affinity;
1343 
1344 	/*
1345 	 * With perf record core.user_requested_cpus is usually NULL.
1346 	 * Use the old method to handle this for now.
1347 	 */
1348 	if (!evlist->core.user_requested_cpus ||
1349 	    cpu_map__is_dummy(evlist->core.user_requested_cpus)) {
1350 		evlist__for_each_entry_reverse(evlist, evsel)
1351 			evsel__close(evsel);
1352 		return;
1353 	}
1354 
1355 	if (affinity__setup(&affinity) < 0)
1356 		return;
1357 
1358 	evlist__for_each_cpu(evlist_cpu_itr, evlist, &affinity) {
1359 		perf_evsel__close_cpu(&evlist_cpu_itr.evsel->core,
1360 				      evlist_cpu_itr.cpu_map_idx);
1361 	}
1362 
1363 	affinity__cleanup(&affinity);
1364 	evlist__for_each_entry_reverse(evlist, evsel) {
1365 		perf_evsel__free_fd(&evsel->core);
1366 		perf_evsel__free_id(&evsel->core);
1367 	}
1368 	perf_evlist__reset_id_hash(&evlist->core);
1369 }
1370 
evlist__create_syswide_maps(struct evlist * evlist)1371 static int evlist__create_syswide_maps(struct evlist *evlist)
1372 {
1373 	struct perf_cpu_map *cpus;
1374 	struct perf_thread_map *threads;
1375 
1376 	/*
1377 	 * Try reading /sys/devices/system/cpu/online to get
1378 	 * an all cpus map.
1379 	 *
1380 	 * FIXME: -ENOMEM is the best we can do here, the cpu_map
1381 	 * code needs an overhaul to properly forward the
1382 	 * error, and we may not want to do that fallback to a
1383 	 * default cpu identity map :-\
1384 	 */
1385 	cpus = perf_cpu_map__new_online_cpus();
1386 	if (!cpus)
1387 		return -ENOMEM;
1388 
1389 	threads = perf_thread_map__new_dummy();
1390 	if (!threads) {
1391 		perf_cpu_map__put(cpus);
1392 		return -ENOMEM;
1393 	}
1394 
1395 	perf_evlist__set_maps(&evlist->core, cpus, threads);
1396 	perf_thread_map__put(threads);
1397 	perf_cpu_map__put(cpus);
1398 	return 0;
1399 }
1400 
evlist__open(struct evlist * evlist)1401 int evlist__open(struct evlist *evlist)
1402 {
1403 	struct evsel *evsel;
1404 	int err;
1405 
1406 	/*
1407 	 * Default: one fd per CPU, all threads, aka systemwide
1408 	 * as sys_perf_event_open(cpu = -1, thread = -1) is EINVAL
1409 	 */
1410 	if (evlist->core.threads == NULL && evlist->core.user_requested_cpus == NULL) {
1411 		err = evlist__create_syswide_maps(evlist);
1412 		if (err < 0)
1413 			goto out_err;
1414 	}
1415 
1416 	evlist__update_id_pos(evlist);
1417 
1418 	evlist__for_each_entry(evlist, evsel) {
1419 		err = evsel__open(evsel, evsel->core.cpus, evsel->core.threads);
1420 		if (err < 0)
1421 			goto out_err;
1422 	}
1423 
1424 	return 0;
1425 out_err:
1426 	evlist__close(evlist);
1427 	errno = -err;
1428 	return err;
1429 }
1430 
evlist__prepare_workload(struct evlist * evlist,struct target * target,const char * argv[],bool pipe_output,void (* exec_error)(int signo,siginfo_t * info,void * ucontext))1431 int evlist__prepare_workload(struct evlist *evlist, struct target *target, const char *argv[],
1432 			     bool pipe_output, void (*exec_error)(int signo, siginfo_t *info, void *ucontext))
1433 {
1434 	int child_ready_pipe[2], go_pipe[2];
1435 	char bf;
1436 
1437 	evlist->workload.cork_fd = -1;
1438 
1439 	if (pipe(child_ready_pipe) < 0) {
1440 		perror("failed to create 'ready' pipe");
1441 		return -1;
1442 	}
1443 
1444 	if (pipe(go_pipe) < 0) {
1445 		perror("failed to create 'go' pipe");
1446 		goto out_close_ready_pipe;
1447 	}
1448 
1449 	evlist->workload.pid = fork();
1450 	if (evlist->workload.pid < 0) {
1451 		perror("failed to fork");
1452 		goto out_close_pipes;
1453 	}
1454 
1455 	if (!evlist->workload.pid) {
1456 		int ret;
1457 
1458 		if (pipe_output)
1459 			dup2(2, 1);
1460 
1461 		signal(SIGTERM, SIG_DFL);
1462 
1463 		close(child_ready_pipe[0]);
1464 		close(go_pipe[1]);
1465 		fcntl(go_pipe[0], F_SETFD, FD_CLOEXEC);
1466 
1467 		/*
1468 		 * Change the name of this process not to confuse --exclude-perf users
1469 		 * that sees 'perf' in the window up to the execvp() and thinks that
1470 		 * perf samples are not being excluded.
1471 		 */
1472 		prctl(PR_SET_NAME, "perf-exec");
1473 
1474 		/*
1475 		 * Tell the parent we're ready to go
1476 		 */
1477 		close(child_ready_pipe[1]);
1478 
1479 		/*
1480 		 * Wait until the parent tells us to go.
1481 		 */
1482 		ret = read(go_pipe[0], &bf, 1);
1483 		/*
1484 		 * The parent will ask for the execvp() to be performed by
1485 		 * writing exactly one byte, in workload.cork_fd, usually via
1486 		 * evlist__start_workload().
1487 		 *
1488 		 * For cancelling the workload without actually running it,
1489 		 * the parent will just close workload.cork_fd, without writing
1490 		 * anything, i.e. read will return zero and we just exit()
1491 		 * here (See evlist__cancel_workload()).
1492 		 */
1493 		if (ret != 1) {
1494 			if (ret == -1)
1495 				perror("unable to read pipe");
1496 			exit(ret);
1497 		}
1498 
1499 		execvp(argv[0], (char **)argv);
1500 
1501 		if (exec_error) {
1502 			union sigval val;
1503 
1504 			val.sival_int = errno;
1505 			if (sigqueue(getppid(), SIGUSR1, val))
1506 				perror(argv[0]);
1507 		} else
1508 			perror(argv[0]);
1509 		exit(-1);
1510 	}
1511 
1512 	if (exec_error) {
1513 		struct sigaction act = {
1514 			.sa_flags     = SA_SIGINFO,
1515 			.sa_sigaction = exec_error,
1516 		};
1517 		sigaction(SIGUSR1, &act, NULL);
1518 	}
1519 
1520 	if (target__none(target)) {
1521 		if (evlist->core.threads == NULL) {
1522 			fprintf(stderr, "FATAL: evlist->threads need to be set at this point (%s:%d).\n",
1523 				__func__, __LINE__);
1524 			goto out_close_pipes;
1525 		}
1526 		perf_thread_map__set_pid(evlist->core.threads, 0, evlist->workload.pid);
1527 	}
1528 
1529 	close(child_ready_pipe[1]);
1530 	close(go_pipe[0]);
1531 	/*
1532 	 * wait for child to settle
1533 	 */
1534 	if (read(child_ready_pipe[0], &bf, 1) == -1) {
1535 		perror("unable to read pipe");
1536 		goto out_close_pipes;
1537 	}
1538 
1539 	fcntl(go_pipe[1], F_SETFD, FD_CLOEXEC);
1540 	evlist->workload.cork_fd = go_pipe[1];
1541 	close(child_ready_pipe[0]);
1542 	return 0;
1543 
1544 out_close_pipes:
1545 	close(go_pipe[0]);
1546 	close(go_pipe[1]);
1547 out_close_ready_pipe:
1548 	close(child_ready_pipe[0]);
1549 	close(child_ready_pipe[1]);
1550 	return -1;
1551 }
1552 
evlist__start_workload(struct evlist * evlist)1553 int evlist__start_workload(struct evlist *evlist)
1554 {
1555 	if (evlist->workload.cork_fd >= 0) {
1556 		char bf = 0;
1557 		int ret;
1558 		/*
1559 		 * Remove the cork, let it rip!
1560 		 */
1561 		ret = write(evlist->workload.cork_fd, &bf, 1);
1562 		if (ret < 0)
1563 			perror("unable to write to pipe");
1564 
1565 		close(evlist->workload.cork_fd);
1566 		evlist->workload.cork_fd = -1;
1567 		return ret;
1568 	}
1569 
1570 	return 0;
1571 }
1572 
evlist__cancel_workload(struct evlist * evlist)1573 void evlist__cancel_workload(struct evlist *evlist)
1574 {
1575 	int status;
1576 
1577 	if (evlist->workload.cork_fd >= 0) {
1578 		close(evlist->workload.cork_fd);
1579 		evlist->workload.cork_fd = -1;
1580 		waitpid(evlist->workload.pid, &status, WNOHANG);
1581 	}
1582 }
1583 
evlist__parse_sample(struct evlist * evlist,union perf_event * event,struct perf_sample * sample)1584 int evlist__parse_sample(struct evlist *evlist, union perf_event *event, struct perf_sample *sample)
1585 {
1586 	struct evsel *evsel = evlist__event2evsel(evlist, event);
1587 	int ret;
1588 
1589 	if (!evsel)
1590 		return -EFAULT;
1591 	ret = evsel__parse_sample(evsel, event, sample);
1592 	if (ret)
1593 		return ret;
1594 	if (perf_guest && sample->id) {
1595 		struct perf_sample_id *sid = evlist__id2sid(evlist, sample->id);
1596 
1597 		if (sid) {
1598 			sample->machine_pid = sid->machine_pid;
1599 			sample->vcpu = sid->vcpu.cpu;
1600 		}
1601 	}
1602 	return 0;
1603 }
1604 
evlist__parse_sample_timestamp(struct evlist * evlist,union perf_event * event,u64 * timestamp)1605 int evlist__parse_sample_timestamp(struct evlist *evlist, union perf_event *event, u64 *timestamp)
1606 {
1607 	struct evsel *evsel = evlist__event2evsel(evlist, event);
1608 
1609 	if (!evsel)
1610 		return -EFAULT;
1611 	return evsel__parse_sample_timestamp(evsel, event, timestamp);
1612 }
1613 
evlist__strerror_open(struct evlist * evlist,int err,char * buf,size_t size)1614 int evlist__strerror_open(struct evlist *evlist, int err, char *buf, size_t size)
1615 {
1616 	int printed, value;
1617 	char sbuf[STRERR_BUFSIZE], *emsg = str_error_r(err, sbuf, sizeof(sbuf));
1618 
1619 	switch (err) {
1620 	case EACCES:
1621 	case EPERM:
1622 		printed = scnprintf(buf, size,
1623 				    "Error:\t%s.\n"
1624 				    "Hint:\tCheck /proc/sys/kernel/perf_event_paranoid setting.", emsg);
1625 
1626 		value = perf_event_paranoid();
1627 
1628 		printed += scnprintf(buf + printed, size - printed, "\nHint:\t");
1629 
1630 		if (value >= 2) {
1631 			printed += scnprintf(buf + printed, size - printed,
1632 					     "For your workloads it needs to be <= 1\nHint:\t");
1633 		}
1634 		printed += scnprintf(buf + printed, size - printed,
1635 				     "For system wide tracing it needs to be set to -1.\n");
1636 
1637 		printed += scnprintf(buf + printed, size - printed,
1638 				    "Hint:\tTry: 'sudo sh -c \"echo -1 > /proc/sys/kernel/perf_event_paranoid\"'\n"
1639 				    "Hint:\tThe current value is %d.", value);
1640 		break;
1641 	case EINVAL: {
1642 		struct evsel *first = evlist__first(evlist);
1643 		int max_freq;
1644 
1645 		if (sysctl__read_int("kernel/perf_event_max_sample_rate", &max_freq) < 0)
1646 			goto out_default;
1647 
1648 		if (first->core.attr.sample_freq < (u64)max_freq)
1649 			goto out_default;
1650 
1651 		printed = scnprintf(buf, size,
1652 				    "Error:\t%s.\n"
1653 				    "Hint:\tCheck /proc/sys/kernel/perf_event_max_sample_rate.\n"
1654 				    "Hint:\tThe current value is %d and %" PRIu64 " is being requested.",
1655 				    emsg, max_freq, first->core.attr.sample_freq);
1656 		break;
1657 	}
1658 	default:
1659 out_default:
1660 		scnprintf(buf, size, "%s", emsg);
1661 		break;
1662 	}
1663 
1664 	return 0;
1665 }
1666 
evlist__strerror_mmap(struct evlist * evlist,int err,char * buf,size_t size)1667 int evlist__strerror_mmap(struct evlist *evlist, int err, char *buf, size_t size)
1668 {
1669 	char sbuf[STRERR_BUFSIZE], *emsg = str_error_r(err, sbuf, sizeof(sbuf));
1670 	int pages_attempted = evlist->core.mmap_len / 1024, pages_max_per_user, printed = 0;
1671 
1672 	switch (err) {
1673 	case EPERM:
1674 		sysctl__read_int("kernel/perf_event_mlock_kb", &pages_max_per_user);
1675 		printed += scnprintf(buf + printed, size - printed,
1676 				     "Error:\t%s.\n"
1677 				     "Hint:\tCheck /proc/sys/kernel/perf_event_mlock_kb (%d kB) setting.\n"
1678 				     "Hint:\tTried using %zd kB.\n",
1679 				     emsg, pages_max_per_user, pages_attempted);
1680 
1681 		if (pages_attempted >= pages_max_per_user) {
1682 			printed += scnprintf(buf + printed, size - printed,
1683 					     "Hint:\tTry 'sudo sh -c \"echo %d > /proc/sys/kernel/perf_event_mlock_kb\"', or\n",
1684 					     pages_max_per_user + pages_attempted);
1685 		}
1686 
1687 		printed += scnprintf(buf + printed, size - printed,
1688 				     "Hint:\tTry using a smaller -m/--mmap-pages value.");
1689 		break;
1690 	default:
1691 		scnprintf(buf, size, "%s", emsg);
1692 		break;
1693 	}
1694 
1695 	return 0;
1696 }
1697 
evlist__to_front(struct evlist * evlist,struct evsel * move_evsel)1698 void evlist__to_front(struct evlist *evlist, struct evsel *move_evsel)
1699 {
1700 	struct evsel *evsel, *n;
1701 	LIST_HEAD(move);
1702 
1703 	if (move_evsel == evlist__first(evlist))
1704 		return;
1705 
1706 	evlist__for_each_entry_safe(evlist, n, evsel) {
1707 		if (evsel__leader(evsel) == evsel__leader(move_evsel))
1708 			list_move_tail(&evsel->core.node, &move);
1709 	}
1710 
1711 	list_splice(&move, &evlist->core.entries);
1712 }
1713 
evlist__get_tracking_event(struct evlist * evlist)1714 struct evsel *evlist__get_tracking_event(struct evlist *evlist)
1715 {
1716 	struct evsel *evsel;
1717 
1718 	evlist__for_each_entry(evlist, evsel) {
1719 		if (evsel->tracking)
1720 			return evsel;
1721 	}
1722 
1723 	return evlist__first(evlist);
1724 }
1725 
evlist__set_tracking_event(struct evlist * evlist,struct evsel * tracking_evsel)1726 void evlist__set_tracking_event(struct evlist *evlist, struct evsel *tracking_evsel)
1727 {
1728 	struct evsel *evsel;
1729 
1730 	if (tracking_evsel->tracking)
1731 		return;
1732 
1733 	evlist__for_each_entry(evlist, evsel) {
1734 		if (evsel != tracking_evsel)
1735 			evsel->tracking = false;
1736 	}
1737 
1738 	tracking_evsel->tracking = true;
1739 }
1740 
evlist__findnew_tracking_event(struct evlist * evlist,bool system_wide)1741 struct evsel *evlist__findnew_tracking_event(struct evlist *evlist, bool system_wide)
1742 {
1743 	struct evsel *evsel;
1744 
1745 	evsel = evlist__get_tracking_event(evlist);
1746 	if (!evsel__is_dummy_event(evsel)) {
1747 		evsel = evlist__add_aux_dummy(evlist, system_wide);
1748 		if (!evsel)
1749 			return NULL;
1750 
1751 		evlist__set_tracking_event(evlist, evsel);
1752 	} else if (system_wide) {
1753 		perf_evlist__go_system_wide(&evlist->core, &evsel->core);
1754 	}
1755 
1756 	return evsel;
1757 }
1758 
evlist__find_evsel_by_str(struct evlist * evlist,const char * str)1759 struct evsel *evlist__find_evsel_by_str(struct evlist *evlist, const char *str)
1760 {
1761 	struct evsel *evsel;
1762 
1763 	evlist__for_each_entry(evlist, evsel) {
1764 		if (!evsel->name)
1765 			continue;
1766 		if (evsel__name_is(evsel, str))
1767 			return evsel;
1768 	}
1769 
1770 	return NULL;
1771 }
1772 
evlist__toggle_bkw_mmap(struct evlist * evlist,enum bkw_mmap_state state)1773 void evlist__toggle_bkw_mmap(struct evlist *evlist, enum bkw_mmap_state state)
1774 {
1775 	enum bkw_mmap_state old_state = evlist->bkw_mmap_state;
1776 	enum action {
1777 		NONE,
1778 		PAUSE,
1779 		RESUME,
1780 	} action = NONE;
1781 
1782 	if (!evlist->overwrite_mmap)
1783 		return;
1784 
1785 	switch (old_state) {
1786 	case BKW_MMAP_NOTREADY: {
1787 		if (state != BKW_MMAP_RUNNING)
1788 			goto state_err;
1789 		break;
1790 	}
1791 	case BKW_MMAP_RUNNING: {
1792 		if (state != BKW_MMAP_DATA_PENDING)
1793 			goto state_err;
1794 		action = PAUSE;
1795 		break;
1796 	}
1797 	case BKW_MMAP_DATA_PENDING: {
1798 		if (state != BKW_MMAP_EMPTY)
1799 			goto state_err;
1800 		break;
1801 	}
1802 	case BKW_MMAP_EMPTY: {
1803 		if (state != BKW_MMAP_RUNNING)
1804 			goto state_err;
1805 		action = RESUME;
1806 		break;
1807 	}
1808 	default:
1809 		WARN_ONCE(1, "Shouldn't get there\n");
1810 	}
1811 
1812 	evlist->bkw_mmap_state = state;
1813 
1814 	switch (action) {
1815 	case PAUSE:
1816 		evlist__pause(evlist);
1817 		break;
1818 	case RESUME:
1819 		evlist__resume(evlist);
1820 		break;
1821 	case NONE:
1822 	default:
1823 		break;
1824 	}
1825 
1826 state_err:
1827 	return;
1828 }
1829 
evlist__exclude_kernel(struct evlist * evlist)1830 bool evlist__exclude_kernel(struct evlist *evlist)
1831 {
1832 	struct evsel *evsel;
1833 
1834 	evlist__for_each_entry(evlist, evsel) {
1835 		if (!evsel->core.attr.exclude_kernel)
1836 			return false;
1837 	}
1838 
1839 	return true;
1840 }
1841 
1842 /*
1843  * Events in data file are not collect in groups, but we still want
1844  * the group display. Set the artificial group and set the leader's
1845  * forced_leader flag to notify the display code.
1846  */
evlist__force_leader(struct evlist * evlist)1847 void evlist__force_leader(struct evlist *evlist)
1848 {
1849 	if (evlist__nr_groups(evlist) == 0) {
1850 		struct evsel *leader = evlist__first(evlist);
1851 
1852 		evlist__set_leader(evlist);
1853 		leader->forced_leader = true;
1854 	}
1855 }
1856 
evlist__reset_weak_group(struct evlist * evsel_list,struct evsel * evsel,bool close)1857 struct evsel *evlist__reset_weak_group(struct evlist *evsel_list, struct evsel *evsel, bool close)
1858 {
1859 	struct evsel *c2, *leader;
1860 	bool is_open = true;
1861 
1862 	leader = evsel__leader(evsel);
1863 
1864 	pr_debug("Weak group for %s/%d failed\n",
1865 			leader->name, leader->core.nr_members);
1866 
1867 	/*
1868 	 * for_each_group_member doesn't work here because it doesn't
1869 	 * include the first entry.
1870 	 */
1871 	evlist__for_each_entry(evsel_list, c2) {
1872 		if (c2 == evsel)
1873 			is_open = false;
1874 		if (evsel__has_leader(c2, leader)) {
1875 			if (is_open && close)
1876 				perf_evsel__close(&c2->core);
1877 			/*
1878 			 * We want to close all members of the group and reopen
1879 			 * them. Some events, like Intel topdown, require being
1880 			 * in a group and so keep these in the group.
1881 			 */
1882 			evsel__remove_from_group(c2, leader);
1883 
1884 			/*
1885 			 * Set this for all former members of the group
1886 			 * to indicate they get reopened.
1887 			 */
1888 			c2->reset_group = true;
1889 		}
1890 	}
1891 	/* Reset the leader count if all entries were removed. */
1892 	if (leader->core.nr_members == 1)
1893 		leader->core.nr_members = 0;
1894 	return leader;
1895 }
1896 
evlist__parse_control_fifo(const char * str,int * ctl_fd,int * ctl_fd_ack,bool * ctl_fd_close)1897 static int evlist__parse_control_fifo(const char *str, int *ctl_fd, int *ctl_fd_ack, bool *ctl_fd_close)
1898 {
1899 	char *s, *p;
1900 	int ret = 0, fd;
1901 
1902 	if (strncmp(str, "fifo:", 5))
1903 		return -EINVAL;
1904 
1905 	str += 5;
1906 	if (!*str || *str == ',')
1907 		return -EINVAL;
1908 
1909 	s = strdup(str);
1910 	if (!s)
1911 		return -ENOMEM;
1912 
1913 	p = strchr(s, ',');
1914 	if (p)
1915 		*p = '\0';
1916 
1917 	/*
1918 	 * O_RDWR avoids POLLHUPs which is necessary to allow the other
1919 	 * end of a FIFO to be repeatedly opened and closed.
1920 	 */
1921 	fd = open(s, O_RDWR | O_NONBLOCK | O_CLOEXEC);
1922 	if (fd < 0) {
1923 		pr_err("Failed to open '%s'\n", s);
1924 		ret = -errno;
1925 		goto out_free;
1926 	}
1927 	*ctl_fd = fd;
1928 	*ctl_fd_close = true;
1929 
1930 	if (p && *++p) {
1931 		/* O_RDWR | O_NONBLOCK means the other end need not be open */
1932 		fd = open(p, O_RDWR | O_NONBLOCK | O_CLOEXEC);
1933 		if (fd < 0) {
1934 			pr_err("Failed to open '%s'\n", p);
1935 			ret = -errno;
1936 			goto out_free;
1937 		}
1938 		*ctl_fd_ack = fd;
1939 	}
1940 
1941 out_free:
1942 	free(s);
1943 	return ret;
1944 }
1945 
evlist__parse_control(const char * str,int * ctl_fd,int * ctl_fd_ack,bool * ctl_fd_close)1946 int evlist__parse_control(const char *str, int *ctl_fd, int *ctl_fd_ack, bool *ctl_fd_close)
1947 {
1948 	char *comma = NULL, *endptr = NULL;
1949 
1950 	*ctl_fd_close = false;
1951 
1952 	if (strncmp(str, "fd:", 3))
1953 		return evlist__parse_control_fifo(str, ctl_fd, ctl_fd_ack, ctl_fd_close);
1954 
1955 	*ctl_fd = strtoul(&str[3], &endptr, 0);
1956 	if (endptr == &str[3])
1957 		return -EINVAL;
1958 
1959 	comma = strchr(str, ',');
1960 	if (comma) {
1961 		if (endptr != comma)
1962 			return -EINVAL;
1963 
1964 		*ctl_fd_ack = strtoul(comma + 1, &endptr, 0);
1965 		if (endptr == comma + 1 || *endptr != '\0')
1966 			return -EINVAL;
1967 	}
1968 
1969 	return 0;
1970 }
1971 
evlist__close_control(int ctl_fd,int ctl_fd_ack,bool * ctl_fd_close)1972 void evlist__close_control(int ctl_fd, int ctl_fd_ack, bool *ctl_fd_close)
1973 {
1974 	if (*ctl_fd_close) {
1975 		*ctl_fd_close = false;
1976 		close(ctl_fd);
1977 		if (ctl_fd_ack >= 0)
1978 			close(ctl_fd_ack);
1979 	}
1980 }
1981 
evlist__initialize_ctlfd(struct evlist * evlist,int fd,int ack)1982 int evlist__initialize_ctlfd(struct evlist *evlist, int fd, int ack)
1983 {
1984 	if (fd == -1) {
1985 		pr_debug("Control descriptor is not initialized\n");
1986 		return 0;
1987 	}
1988 
1989 	evlist->ctl_fd.pos = perf_evlist__add_pollfd(&evlist->core, fd, NULL, POLLIN,
1990 						     fdarray_flag__nonfilterable |
1991 						     fdarray_flag__non_perf_event);
1992 	if (evlist->ctl_fd.pos < 0) {
1993 		evlist->ctl_fd.pos = -1;
1994 		pr_err("Failed to add ctl fd entry: %m\n");
1995 		return -1;
1996 	}
1997 
1998 	evlist->ctl_fd.fd = fd;
1999 	evlist->ctl_fd.ack = ack;
2000 
2001 	return 0;
2002 }
2003 
evlist__ctlfd_initialized(struct evlist * evlist)2004 bool evlist__ctlfd_initialized(struct evlist *evlist)
2005 {
2006 	return evlist->ctl_fd.pos >= 0;
2007 }
2008 
evlist__finalize_ctlfd(struct evlist * evlist)2009 int evlist__finalize_ctlfd(struct evlist *evlist)
2010 {
2011 	struct pollfd *entries = evlist->core.pollfd.entries;
2012 
2013 	if (!evlist__ctlfd_initialized(evlist))
2014 		return 0;
2015 
2016 	entries[evlist->ctl_fd.pos].fd = -1;
2017 	entries[evlist->ctl_fd.pos].events = 0;
2018 	entries[evlist->ctl_fd.pos].revents = 0;
2019 
2020 	evlist->ctl_fd.pos = -1;
2021 	evlist->ctl_fd.ack = -1;
2022 	evlist->ctl_fd.fd = -1;
2023 
2024 	return 0;
2025 }
2026 
evlist__ctlfd_recv(struct evlist * evlist,enum evlist_ctl_cmd * cmd,char * cmd_data,size_t data_size)2027 static int evlist__ctlfd_recv(struct evlist *evlist, enum evlist_ctl_cmd *cmd,
2028 			      char *cmd_data, size_t data_size)
2029 {
2030 	int err;
2031 	char c;
2032 	size_t bytes_read = 0;
2033 
2034 	*cmd = EVLIST_CTL_CMD_UNSUPPORTED;
2035 	memset(cmd_data, 0, data_size);
2036 	data_size--;
2037 
2038 	do {
2039 		err = read(evlist->ctl_fd.fd, &c, 1);
2040 		if (err > 0) {
2041 			if (c == '\n' || c == '\0')
2042 				break;
2043 			cmd_data[bytes_read++] = c;
2044 			if (bytes_read == data_size)
2045 				break;
2046 			continue;
2047 		} else if (err == -1) {
2048 			if (errno == EINTR)
2049 				continue;
2050 			if (errno == EAGAIN || errno == EWOULDBLOCK)
2051 				err = 0;
2052 			else
2053 				pr_err("Failed to read from ctlfd %d: %m\n", evlist->ctl_fd.fd);
2054 		}
2055 		break;
2056 	} while (1);
2057 
2058 	pr_debug("Message from ctl_fd: \"%s%s\"\n", cmd_data,
2059 		 bytes_read == data_size ? "" : c == '\n' ? "\\n" : "\\0");
2060 
2061 	if (bytes_read > 0) {
2062 		if (!strncmp(cmd_data, EVLIST_CTL_CMD_ENABLE_TAG,
2063 			     (sizeof(EVLIST_CTL_CMD_ENABLE_TAG)-1))) {
2064 			*cmd = EVLIST_CTL_CMD_ENABLE;
2065 		} else if (!strncmp(cmd_data, EVLIST_CTL_CMD_DISABLE_TAG,
2066 				    (sizeof(EVLIST_CTL_CMD_DISABLE_TAG)-1))) {
2067 			*cmd = EVLIST_CTL_CMD_DISABLE;
2068 		} else if (!strncmp(cmd_data, EVLIST_CTL_CMD_SNAPSHOT_TAG,
2069 				    (sizeof(EVLIST_CTL_CMD_SNAPSHOT_TAG)-1))) {
2070 			*cmd = EVLIST_CTL_CMD_SNAPSHOT;
2071 			pr_debug("is snapshot\n");
2072 		} else if (!strncmp(cmd_data, EVLIST_CTL_CMD_EVLIST_TAG,
2073 				    (sizeof(EVLIST_CTL_CMD_EVLIST_TAG)-1))) {
2074 			*cmd = EVLIST_CTL_CMD_EVLIST;
2075 		} else if (!strncmp(cmd_data, EVLIST_CTL_CMD_STOP_TAG,
2076 				    (sizeof(EVLIST_CTL_CMD_STOP_TAG)-1))) {
2077 			*cmd = EVLIST_CTL_CMD_STOP;
2078 		} else if (!strncmp(cmd_data, EVLIST_CTL_CMD_PING_TAG,
2079 				    (sizeof(EVLIST_CTL_CMD_PING_TAG)-1))) {
2080 			*cmd = EVLIST_CTL_CMD_PING;
2081 		}
2082 	}
2083 
2084 	return bytes_read ? (int)bytes_read : err;
2085 }
2086 
evlist__ctlfd_ack(struct evlist * evlist)2087 int evlist__ctlfd_ack(struct evlist *evlist)
2088 {
2089 	int err;
2090 
2091 	if (evlist->ctl_fd.ack == -1)
2092 		return 0;
2093 
2094 	err = write(evlist->ctl_fd.ack, EVLIST_CTL_CMD_ACK_TAG,
2095 		    sizeof(EVLIST_CTL_CMD_ACK_TAG));
2096 	if (err == -1)
2097 		pr_err("failed to write to ctl_ack_fd %d: %m\n", evlist->ctl_fd.ack);
2098 
2099 	return err;
2100 }
2101 
get_cmd_arg(char * cmd_data,size_t cmd_size,char ** arg)2102 static int get_cmd_arg(char *cmd_data, size_t cmd_size, char **arg)
2103 {
2104 	char *data = cmd_data + cmd_size;
2105 
2106 	/* no argument */
2107 	if (!*data)
2108 		return 0;
2109 
2110 	/* there's argument */
2111 	if (*data == ' ') {
2112 		*arg = data + 1;
2113 		return 1;
2114 	}
2115 
2116 	/* malformed */
2117 	return -1;
2118 }
2119 
evlist__ctlfd_enable(struct evlist * evlist,char * cmd_data,bool enable)2120 static int evlist__ctlfd_enable(struct evlist *evlist, char *cmd_data, bool enable)
2121 {
2122 	struct evsel *evsel;
2123 	char *name;
2124 	int err;
2125 
2126 	err = get_cmd_arg(cmd_data,
2127 			  enable ? sizeof(EVLIST_CTL_CMD_ENABLE_TAG) - 1 :
2128 				   sizeof(EVLIST_CTL_CMD_DISABLE_TAG) - 1,
2129 			  &name);
2130 	if (err < 0) {
2131 		pr_info("failed: wrong command\n");
2132 		return -1;
2133 	}
2134 
2135 	if (err) {
2136 		evsel = evlist__find_evsel_by_str(evlist, name);
2137 		if (evsel) {
2138 			if (enable)
2139 				evlist__enable_evsel(evlist, name);
2140 			else
2141 				evlist__disable_evsel(evlist, name);
2142 			pr_info("Event %s %s\n", evsel->name,
2143 				enable ? "enabled" : "disabled");
2144 		} else {
2145 			pr_info("failed: can't find '%s' event\n", name);
2146 		}
2147 	} else {
2148 		if (enable) {
2149 			evlist__enable(evlist);
2150 			pr_info(EVLIST_ENABLED_MSG);
2151 		} else {
2152 			evlist__disable(evlist);
2153 			pr_info(EVLIST_DISABLED_MSG);
2154 		}
2155 	}
2156 
2157 	return 0;
2158 }
2159 
evlist__ctlfd_list(struct evlist * evlist,char * cmd_data)2160 static int evlist__ctlfd_list(struct evlist *evlist, char *cmd_data)
2161 {
2162 	struct perf_attr_details details = { .verbose = false, };
2163 	struct evsel *evsel;
2164 	char *arg;
2165 	int err;
2166 
2167 	err = get_cmd_arg(cmd_data,
2168 			  sizeof(EVLIST_CTL_CMD_EVLIST_TAG) - 1,
2169 			  &arg);
2170 	if (err < 0) {
2171 		pr_info("failed: wrong command\n");
2172 		return -1;
2173 	}
2174 
2175 	if (err) {
2176 		if (!strcmp(arg, "-v")) {
2177 			details.verbose = true;
2178 		} else if (!strcmp(arg, "-g")) {
2179 			details.event_group = true;
2180 		} else if (!strcmp(arg, "-F")) {
2181 			details.freq = true;
2182 		} else {
2183 			pr_info("failed: wrong command\n");
2184 			return -1;
2185 		}
2186 	}
2187 
2188 	evlist__for_each_entry(evlist, evsel)
2189 		evsel__fprintf(evsel, &details, stderr);
2190 
2191 	return 0;
2192 }
2193 
evlist__ctlfd_process(struct evlist * evlist,enum evlist_ctl_cmd * cmd)2194 int evlist__ctlfd_process(struct evlist *evlist, enum evlist_ctl_cmd *cmd)
2195 {
2196 	int err = 0;
2197 	char cmd_data[EVLIST_CTL_CMD_MAX_LEN];
2198 	int ctlfd_pos = evlist->ctl_fd.pos;
2199 	struct pollfd *entries = evlist->core.pollfd.entries;
2200 
2201 	if (!evlist__ctlfd_initialized(evlist) || !entries[ctlfd_pos].revents)
2202 		return 0;
2203 
2204 	if (entries[ctlfd_pos].revents & POLLIN) {
2205 		err = evlist__ctlfd_recv(evlist, cmd, cmd_data,
2206 					 EVLIST_CTL_CMD_MAX_LEN);
2207 		if (err > 0) {
2208 			switch (*cmd) {
2209 			case EVLIST_CTL_CMD_ENABLE:
2210 			case EVLIST_CTL_CMD_DISABLE:
2211 				err = evlist__ctlfd_enable(evlist, cmd_data,
2212 							   *cmd == EVLIST_CTL_CMD_ENABLE);
2213 				break;
2214 			case EVLIST_CTL_CMD_EVLIST:
2215 				err = evlist__ctlfd_list(evlist, cmd_data);
2216 				break;
2217 			case EVLIST_CTL_CMD_SNAPSHOT:
2218 			case EVLIST_CTL_CMD_STOP:
2219 			case EVLIST_CTL_CMD_PING:
2220 				break;
2221 			case EVLIST_CTL_CMD_ACK:
2222 			case EVLIST_CTL_CMD_UNSUPPORTED:
2223 			default:
2224 				pr_debug("ctlfd: unsupported %d\n", *cmd);
2225 				break;
2226 			}
2227 			if (!(*cmd == EVLIST_CTL_CMD_ACK || *cmd == EVLIST_CTL_CMD_UNSUPPORTED ||
2228 			      *cmd == EVLIST_CTL_CMD_SNAPSHOT))
2229 				evlist__ctlfd_ack(evlist);
2230 		}
2231 	}
2232 
2233 	if (entries[ctlfd_pos].revents & (POLLHUP | POLLERR))
2234 		evlist__finalize_ctlfd(evlist);
2235 	else
2236 		entries[ctlfd_pos].revents = 0;
2237 
2238 	return err;
2239 }
2240 
2241 /**
2242  * struct event_enable_time - perf record -D/--delay single time range.
2243  * @start: start of time range to enable events in milliseconds
2244  * @end: end of time range to enable events in milliseconds
2245  *
2246  * N.B. this structure is also accessed as an array of int.
2247  */
2248 struct event_enable_time {
2249 	int	start;
2250 	int	end;
2251 };
2252 
parse_event_enable_time(const char * str,struct event_enable_time * range,bool first)2253 static int parse_event_enable_time(const char *str, struct event_enable_time *range, bool first)
2254 {
2255 	const char *fmt = first ? "%u - %u %n" : " , %u - %u %n";
2256 	int ret, start, end, n;
2257 
2258 	ret = sscanf(str, fmt, &start, &end, &n);
2259 	if (ret != 2 || end <= start)
2260 		return -EINVAL;
2261 	if (range) {
2262 		range->start = start;
2263 		range->end = end;
2264 	}
2265 	return n;
2266 }
2267 
parse_event_enable_times(const char * str,struct event_enable_time * range)2268 static ssize_t parse_event_enable_times(const char *str, struct event_enable_time *range)
2269 {
2270 	int incr = !!range;
2271 	bool first = true;
2272 	ssize_t ret, cnt;
2273 
2274 	for (cnt = 0; *str; cnt++) {
2275 		ret = parse_event_enable_time(str, range, first);
2276 		if (ret < 0)
2277 			return ret;
2278 		/* Check no overlap */
2279 		if (!first && range && range->start <= range[-1].end)
2280 			return -EINVAL;
2281 		str += ret;
2282 		range += incr;
2283 		first = false;
2284 	}
2285 	return cnt;
2286 }
2287 
2288 /**
2289  * struct event_enable_timer - control structure for perf record -D/--delay.
2290  * @evlist: event list
2291  * @times: time ranges that events are enabled (N.B. this is also accessed as an
2292  *         array of int)
2293  * @times_cnt: number of time ranges
2294  * @timerfd: timer file descriptor
2295  * @pollfd_pos: position in @evlist array of file descriptors to poll (fdarray)
2296  * @times_step: current position in (int *)@times)[],
2297  *              refer event_enable_timer__process()
2298  *
2299  * Note, this structure is only used when there are time ranges, not when there
2300  * is only an initial delay.
2301  */
2302 struct event_enable_timer {
2303 	struct evlist *evlist;
2304 	struct event_enable_time *times;
2305 	size_t	times_cnt;
2306 	int	timerfd;
2307 	int	pollfd_pos;
2308 	size_t	times_step;
2309 };
2310 
str_to_delay(const char * str)2311 static int str_to_delay(const char *str)
2312 {
2313 	char *endptr;
2314 	long d;
2315 
2316 	d = strtol(str, &endptr, 10);
2317 	if (*endptr || d > INT_MAX || d < -1)
2318 		return 0;
2319 	return d;
2320 }
2321 
evlist__parse_event_enable_time(struct evlist * evlist,struct record_opts * opts,const char * str,int unset)2322 int evlist__parse_event_enable_time(struct evlist *evlist, struct record_opts *opts,
2323 				    const char *str, int unset)
2324 {
2325 	enum fdarray_flags flags = fdarray_flag__nonfilterable | fdarray_flag__non_perf_event;
2326 	struct event_enable_timer *eet;
2327 	ssize_t times_cnt;
2328 	ssize_t ret;
2329 	int err;
2330 
2331 	if (unset)
2332 		return 0;
2333 
2334 	opts->target.initial_delay = str_to_delay(str);
2335 	if (opts->target.initial_delay)
2336 		return 0;
2337 
2338 	ret = parse_event_enable_times(str, NULL);
2339 	if (ret < 0)
2340 		return ret;
2341 
2342 	times_cnt = ret;
2343 	if (times_cnt == 0)
2344 		return -EINVAL;
2345 
2346 	eet = zalloc(sizeof(*eet));
2347 	if (!eet)
2348 		return -ENOMEM;
2349 
2350 	eet->times = calloc(times_cnt, sizeof(*eet->times));
2351 	if (!eet->times) {
2352 		err = -ENOMEM;
2353 		goto free_eet;
2354 	}
2355 
2356 	if (parse_event_enable_times(str, eet->times) != times_cnt) {
2357 		err = -EINVAL;
2358 		goto free_eet_times;
2359 	}
2360 
2361 	eet->times_cnt = times_cnt;
2362 
2363 	eet->timerfd = timerfd_create(CLOCK_MONOTONIC, TFD_CLOEXEC);
2364 	if (eet->timerfd == -1) {
2365 		err = -errno;
2366 		pr_err("timerfd_create failed: %s\n", strerror(errno));
2367 		goto free_eet_times;
2368 	}
2369 
2370 	eet->pollfd_pos = perf_evlist__add_pollfd(&evlist->core, eet->timerfd, NULL, POLLIN, flags);
2371 	if (eet->pollfd_pos < 0) {
2372 		err = eet->pollfd_pos;
2373 		goto close_timerfd;
2374 	}
2375 
2376 	eet->evlist = evlist;
2377 	evlist->eet = eet;
2378 	opts->target.initial_delay = eet->times[0].start;
2379 
2380 	return 0;
2381 
2382 close_timerfd:
2383 	close(eet->timerfd);
2384 free_eet_times:
2385 	zfree(&eet->times);
2386 free_eet:
2387 	free(eet);
2388 	return err;
2389 }
2390 
event_enable_timer__set_timer(struct event_enable_timer * eet,int ms)2391 static int event_enable_timer__set_timer(struct event_enable_timer *eet, int ms)
2392 {
2393 	struct itimerspec its = {
2394 		.it_value.tv_sec = ms / MSEC_PER_SEC,
2395 		.it_value.tv_nsec = (ms % MSEC_PER_SEC) * NSEC_PER_MSEC,
2396 	};
2397 	int err = 0;
2398 
2399 	if (timerfd_settime(eet->timerfd, 0, &its, NULL) < 0) {
2400 		err = -errno;
2401 		pr_err("timerfd_settime failed: %s\n", strerror(errno));
2402 	}
2403 	return err;
2404 }
2405 
event_enable_timer__start(struct event_enable_timer * eet)2406 int event_enable_timer__start(struct event_enable_timer *eet)
2407 {
2408 	int ms;
2409 
2410 	if (!eet)
2411 		return 0;
2412 
2413 	ms = eet->times[0].end - eet->times[0].start;
2414 	eet->times_step = 1;
2415 
2416 	return event_enable_timer__set_timer(eet, ms);
2417 }
2418 
event_enable_timer__process(struct event_enable_timer * eet)2419 int event_enable_timer__process(struct event_enable_timer *eet)
2420 {
2421 	struct pollfd *entries;
2422 	short revents;
2423 
2424 	if (!eet)
2425 		return 0;
2426 
2427 	entries = eet->evlist->core.pollfd.entries;
2428 	revents = entries[eet->pollfd_pos].revents;
2429 	entries[eet->pollfd_pos].revents = 0;
2430 
2431 	if (revents & POLLIN) {
2432 		size_t step = eet->times_step;
2433 		size_t pos = step / 2;
2434 
2435 		if (step & 1) {
2436 			evlist__disable_non_dummy(eet->evlist);
2437 			pr_info(EVLIST_DISABLED_MSG);
2438 			if (pos >= eet->times_cnt - 1) {
2439 				/* Disarm timer */
2440 				event_enable_timer__set_timer(eet, 0);
2441 				return 1; /* Stop */
2442 			}
2443 		} else {
2444 			evlist__enable_non_dummy(eet->evlist);
2445 			pr_info(EVLIST_ENABLED_MSG);
2446 		}
2447 
2448 		step += 1;
2449 		pos = step / 2;
2450 
2451 		if (pos < eet->times_cnt) {
2452 			int *times = (int *)eet->times; /* Accessing 'times' as array of int */
2453 			int ms = times[step] - times[step - 1];
2454 
2455 			eet->times_step = step;
2456 			return event_enable_timer__set_timer(eet, ms);
2457 		}
2458 	}
2459 
2460 	return 0;
2461 }
2462 
event_enable_timer__exit(struct event_enable_timer ** ep)2463 void event_enable_timer__exit(struct event_enable_timer **ep)
2464 {
2465 	if (!ep || !*ep)
2466 		return;
2467 	zfree(&(*ep)->times);
2468 	zfree(ep);
2469 }
2470 
evlist__find_evsel(struct evlist * evlist,int idx)2471 struct evsel *evlist__find_evsel(struct evlist *evlist, int idx)
2472 {
2473 	struct evsel *evsel;
2474 
2475 	evlist__for_each_entry(evlist, evsel) {
2476 		if (evsel->core.idx == idx)
2477 			return evsel;
2478 	}
2479 	return NULL;
2480 }
2481 
evlist__format_evsels(struct evlist * evlist,struct strbuf * sb,size_t max_length)2482 void evlist__format_evsels(struct evlist *evlist, struct strbuf *sb, size_t max_length)
2483 {
2484 	struct evsel *evsel, *leader = NULL;
2485 	bool first = true;
2486 
2487 	evlist__for_each_entry(evlist, evsel) {
2488 		struct evsel *new_leader = evsel__leader(evsel);
2489 
2490 		if (evsel__is_dummy_event(evsel))
2491 			continue;
2492 
2493 		if (leader != new_leader && leader && leader->core.nr_members > 1)
2494 			strbuf_addch(sb, '}');
2495 
2496 		if (!first)
2497 			strbuf_addch(sb, ',');
2498 
2499 		if (sb->len > max_length) {
2500 			strbuf_addstr(sb, "...");
2501 			return;
2502 		}
2503 		if (leader != new_leader && new_leader->core.nr_members > 1)
2504 			strbuf_addch(sb, '{');
2505 
2506 		strbuf_addstr(sb, evsel__name(evsel));
2507 		first = false;
2508 		leader = new_leader;
2509 	}
2510 	if (leader && leader->core.nr_members > 1)
2511 		strbuf_addch(sb, '}');
2512 }
2513 
evlist__check_mem_load_aux(struct evlist * evlist)2514 void evlist__check_mem_load_aux(struct evlist *evlist)
2515 {
2516 	struct evsel *leader, *evsel, *pos;
2517 
2518 	/*
2519 	 * For some platforms, the 'mem-loads' event is required to use
2520 	 * together with 'mem-loads-aux' within a group and 'mem-loads-aux'
2521 	 * must be the group leader. Now we disable this group before reporting
2522 	 * because 'mem-loads-aux' is just an auxiliary event. It doesn't carry
2523 	 * any valid memory load information.
2524 	 */
2525 	evlist__for_each_entry(evlist, evsel) {
2526 		leader = evsel__leader(evsel);
2527 		if (leader == evsel)
2528 			continue;
2529 
2530 		if (leader->name && strstr(leader->name, "mem-loads-aux")) {
2531 			for_each_group_evsel(pos, leader) {
2532 				evsel__set_leader(pos, pos);
2533 				pos->core.nr_members = 0;
2534 			}
2535 		}
2536 	}
2537 }
2538 
2539 /**
2540  * evlist__warn_user_requested_cpus() - Check each evsel against requested CPUs
2541  *     and warn if the user CPU list is inapplicable for the event's PMU's
2542  *     CPUs. Not core PMUs list a CPU in sysfs, but this may be overwritten by a
2543  *     user requested CPU and so any online CPU is applicable. Core PMUs handle
2544  *     events on the CPUs in their list and otherwise the event isn't supported.
2545  * @evlist: The list of events being checked.
2546  * @cpu_list: The user provided list of CPUs.
2547  */
evlist__warn_user_requested_cpus(struct evlist * evlist,const char * cpu_list)2548 void evlist__warn_user_requested_cpus(struct evlist *evlist, const char *cpu_list)
2549 {
2550 	struct perf_cpu_map *user_requested_cpus;
2551 	struct evsel *pos;
2552 
2553 	if (!cpu_list)
2554 		return;
2555 
2556 	user_requested_cpus = perf_cpu_map__new(cpu_list);
2557 	if (!user_requested_cpus)
2558 		return;
2559 
2560 	evlist__for_each_entry(evlist, pos) {
2561 		evsel__warn_user_requested_cpus(pos, user_requested_cpus);
2562 	}
2563 	perf_cpu_map__put(user_requested_cpus);
2564 }
2565 
2566 /* Should uniquify be disabled for the evlist? */
evlist__disable_uniquify(const struct evlist * evlist)2567 static bool evlist__disable_uniquify(const struct evlist *evlist)
2568 {
2569 	struct evsel *counter;
2570 	struct perf_pmu *last_pmu = NULL;
2571 	bool first = true;
2572 
2573 	evlist__for_each_entry(evlist, counter) {
2574 		/* If PMUs vary then uniquify can be useful. */
2575 		if (!first && counter->pmu != last_pmu)
2576 			return false;
2577 		first = false;
2578 		if (counter->pmu) {
2579 			/* Allow uniquify for uncore PMUs. */
2580 			if (!counter->pmu->is_core)
2581 				return false;
2582 			/* Keep hybrid event names uniquified for clarity. */
2583 			if (perf_pmus__num_core_pmus() > 1)
2584 				return false;
2585 		}
2586 		last_pmu = counter->pmu;
2587 	}
2588 	return true;
2589 }
2590 
evlist__set_needs_uniquify(struct evlist * evlist,const struct perf_stat_config * config)2591 static bool evlist__set_needs_uniquify(struct evlist *evlist, const struct perf_stat_config *config)
2592 {
2593 	struct evsel *counter;
2594 	bool needs_uniquify = false;
2595 
2596 	if (evlist__disable_uniquify(evlist)) {
2597 		evlist__for_each_entry(evlist, counter)
2598 			counter->uniquified_name = true;
2599 		return false;
2600 	}
2601 
2602 	evlist__for_each_entry(evlist, counter) {
2603 		if (evsel__set_needs_uniquify(counter, config))
2604 			needs_uniquify = true;
2605 	}
2606 	return needs_uniquify;
2607 }
2608 
evlist__uniquify_evsel_names(struct evlist * evlist,const struct perf_stat_config * config)2609 void evlist__uniquify_evsel_names(struct evlist *evlist, const struct perf_stat_config *config)
2610 {
2611 	if (evlist__set_needs_uniquify(evlist, config)) {
2612 		struct evsel *pos;
2613 
2614 		evlist__for_each_entry(evlist, pos)
2615 			evsel__uniquify_counter(pos);
2616 	}
2617 }
2618 
evlist__has_bpf_output(struct evlist * evlist)2619 bool evlist__has_bpf_output(struct evlist *evlist)
2620 {
2621 	struct evsel *evsel;
2622 
2623 	evlist__for_each_entry(evlist, evsel) {
2624 		if (evsel__is_bpf_output(evsel))
2625 			return true;
2626 	}
2627 
2628 	return false;
2629 }
2630 
evlist__needs_bpf_sb_event(struct evlist * evlist)2631 bool evlist__needs_bpf_sb_event(struct evlist *evlist)
2632 {
2633 	struct evsel *evsel;
2634 
2635 	evlist__for_each_entry(evlist, evsel) {
2636 		if (evsel__is_dummy_event(evsel))
2637 			continue;
2638 		if (!evsel->core.attr.exclude_kernel)
2639 			return true;
2640 	}
2641 
2642 	return false;
2643 }
2644