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