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