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