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