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