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