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