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