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