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