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