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