xref: /openbmc/linux/tools/perf/util/evlist.c (revision 1f9f6a78)
1 /*
2  * Copyright (C) 2011, Red Hat Inc, Arnaldo Carvalho de Melo <acme@redhat.com>
3  *
4  * Parts came from builtin-{top,stat,record}.c, see those files for further
5  * copyright notes.
6  *
7  * Released under the GPL v2. (and only v2, not any later version)
8  */
9 #include "util.h"
10 #include <api/fs/debugfs.h>
11 #include <api/fs/fs.h>
12 #include <poll.h>
13 #include "cpumap.h"
14 #include "thread_map.h"
15 #include "target.h"
16 #include "evlist.h"
17 #include "evsel.h"
18 #include "debug.h"
19 #include <unistd.h>
20 
21 #include "parse-events.h"
22 #include "parse-options.h"
23 
24 #include <sys/mman.h>
25 
26 #include <linux/bitops.h>
27 #include <linux/hash.h>
28 #include <linux/log2.h>
29 
30 static void perf_evlist__mmap_put(struct perf_evlist *evlist, int idx);
31 static void __perf_evlist__munmap(struct perf_evlist *evlist, int idx);
32 
33 #define FD(e, x, y) (*(int *)xyarray__entry(e->fd, x, y))
34 #define SID(e, x, y) xyarray__entry(e->sample_id, x, y)
35 
36 void perf_evlist__init(struct perf_evlist *evlist, struct cpu_map *cpus,
37 		       struct thread_map *threads)
38 {
39 	int i;
40 
41 	for (i = 0; i < PERF_EVLIST__HLIST_SIZE; ++i)
42 		INIT_HLIST_HEAD(&evlist->heads[i]);
43 	INIT_LIST_HEAD(&evlist->entries);
44 	perf_evlist__set_maps(evlist, cpus, threads);
45 	fdarray__init(&evlist->pollfd, 64);
46 	evlist->workload.pid = -1;
47 }
48 
49 struct perf_evlist *perf_evlist__new(void)
50 {
51 	struct perf_evlist *evlist = zalloc(sizeof(*evlist));
52 
53 	if (evlist != NULL)
54 		perf_evlist__init(evlist, NULL, NULL);
55 
56 	return evlist;
57 }
58 
59 struct perf_evlist *perf_evlist__new_default(void)
60 {
61 	struct perf_evlist *evlist = perf_evlist__new();
62 
63 	if (evlist && perf_evlist__add_default(evlist)) {
64 		perf_evlist__delete(evlist);
65 		evlist = NULL;
66 	}
67 
68 	return evlist;
69 }
70 
71 /**
72  * perf_evlist__set_id_pos - set the positions of event ids.
73  * @evlist: selected event list
74  *
75  * Events with compatible sample types all have the same id_pos
76  * and is_pos.  For convenience, put a copy on evlist.
77  */
78 void perf_evlist__set_id_pos(struct perf_evlist *evlist)
79 {
80 	struct perf_evsel *first = perf_evlist__first(evlist);
81 
82 	evlist->id_pos = first->id_pos;
83 	evlist->is_pos = first->is_pos;
84 }
85 
86 static void perf_evlist__update_id_pos(struct perf_evlist *evlist)
87 {
88 	struct perf_evsel *evsel;
89 
90 	evlist__for_each(evlist, evsel)
91 		perf_evsel__calc_id_pos(evsel);
92 
93 	perf_evlist__set_id_pos(evlist);
94 }
95 
96 static void perf_evlist__purge(struct perf_evlist *evlist)
97 {
98 	struct perf_evsel *pos, *n;
99 
100 	evlist__for_each_safe(evlist, n, pos) {
101 		list_del_init(&pos->node);
102 		perf_evsel__delete(pos);
103 	}
104 
105 	evlist->nr_entries = 0;
106 }
107 
108 void perf_evlist__exit(struct perf_evlist *evlist)
109 {
110 	zfree(&evlist->mmap);
111 	fdarray__exit(&evlist->pollfd);
112 }
113 
114 void perf_evlist__delete(struct perf_evlist *evlist)
115 {
116 	perf_evlist__munmap(evlist);
117 	perf_evlist__close(evlist);
118 	cpu_map__delete(evlist->cpus);
119 	thread_map__delete(evlist->threads);
120 	evlist->cpus = NULL;
121 	evlist->threads = NULL;
122 	perf_evlist__purge(evlist);
123 	perf_evlist__exit(evlist);
124 	free(evlist);
125 }
126 
127 void perf_evlist__add(struct perf_evlist *evlist, struct perf_evsel *entry)
128 {
129 	list_add_tail(&entry->node, &evlist->entries);
130 	entry->idx = evlist->nr_entries;
131 	entry->tracking = !entry->idx;
132 
133 	if (!evlist->nr_entries++)
134 		perf_evlist__set_id_pos(evlist);
135 }
136 
137 void perf_evlist__splice_list_tail(struct perf_evlist *evlist,
138 				   struct list_head *list,
139 				   int nr_entries)
140 {
141 	bool set_id_pos = !evlist->nr_entries;
142 
143 	list_splice_tail(list, &evlist->entries);
144 	evlist->nr_entries += nr_entries;
145 	if (set_id_pos)
146 		perf_evlist__set_id_pos(evlist);
147 }
148 
149 void __perf_evlist__set_leader(struct list_head *list)
150 {
151 	struct perf_evsel *evsel, *leader;
152 
153 	leader = list_entry(list->next, struct perf_evsel, node);
154 	evsel = list_entry(list->prev, struct perf_evsel, node);
155 
156 	leader->nr_members = evsel->idx - leader->idx + 1;
157 
158 	__evlist__for_each(list, evsel) {
159 		evsel->leader = leader;
160 	}
161 }
162 
163 void perf_evlist__set_leader(struct perf_evlist *evlist)
164 {
165 	if (evlist->nr_entries) {
166 		evlist->nr_groups = evlist->nr_entries > 1 ? 1 : 0;
167 		__perf_evlist__set_leader(&evlist->entries);
168 	}
169 }
170 
171 int perf_evlist__add_default(struct perf_evlist *evlist)
172 {
173 	struct perf_event_attr attr = {
174 		.type = PERF_TYPE_HARDWARE,
175 		.config = PERF_COUNT_HW_CPU_CYCLES,
176 	};
177 	struct perf_evsel *evsel;
178 
179 	event_attr_init(&attr);
180 
181 	evsel = perf_evsel__new(&attr);
182 	if (evsel == NULL)
183 		goto error;
184 
185 	/* use strdup() because free(evsel) assumes name is allocated */
186 	evsel->name = strdup("cycles");
187 	if (!evsel->name)
188 		goto error_free;
189 
190 	perf_evlist__add(evlist, evsel);
191 	return 0;
192 error_free:
193 	perf_evsel__delete(evsel);
194 error:
195 	return -ENOMEM;
196 }
197 
198 static int perf_evlist__add_attrs(struct perf_evlist *evlist,
199 				  struct perf_event_attr *attrs, size_t nr_attrs)
200 {
201 	struct perf_evsel *evsel, *n;
202 	LIST_HEAD(head);
203 	size_t i;
204 
205 	for (i = 0; i < nr_attrs; i++) {
206 		evsel = perf_evsel__new_idx(attrs + i, evlist->nr_entries + i);
207 		if (evsel == NULL)
208 			goto out_delete_partial_list;
209 		list_add_tail(&evsel->node, &head);
210 	}
211 
212 	perf_evlist__splice_list_tail(evlist, &head, nr_attrs);
213 
214 	return 0;
215 
216 out_delete_partial_list:
217 	__evlist__for_each_safe(&head, n, evsel)
218 		perf_evsel__delete(evsel);
219 	return -1;
220 }
221 
222 int __perf_evlist__add_default_attrs(struct perf_evlist *evlist,
223 				     struct perf_event_attr *attrs, size_t nr_attrs)
224 {
225 	size_t i;
226 
227 	for (i = 0; i < nr_attrs; i++)
228 		event_attr_init(attrs + i);
229 
230 	return perf_evlist__add_attrs(evlist, attrs, nr_attrs);
231 }
232 
233 struct perf_evsel *
234 perf_evlist__find_tracepoint_by_id(struct perf_evlist *evlist, int id)
235 {
236 	struct perf_evsel *evsel;
237 
238 	evlist__for_each(evlist, evsel) {
239 		if (evsel->attr.type   == PERF_TYPE_TRACEPOINT &&
240 		    (int)evsel->attr.config == id)
241 			return evsel;
242 	}
243 
244 	return NULL;
245 }
246 
247 struct perf_evsel *
248 perf_evlist__find_tracepoint_by_name(struct perf_evlist *evlist,
249 				     const char *name)
250 {
251 	struct perf_evsel *evsel;
252 
253 	evlist__for_each(evlist, evsel) {
254 		if ((evsel->attr.type == PERF_TYPE_TRACEPOINT) &&
255 		    (strcmp(evsel->name, name) == 0))
256 			return evsel;
257 	}
258 
259 	return NULL;
260 }
261 
262 int perf_evlist__add_newtp(struct perf_evlist *evlist,
263 			   const char *sys, const char *name, void *handler)
264 {
265 	struct perf_evsel *evsel = perf_evsel__newtp(sys, name);
266 
267 	if (evsel == NULL)
268 		return -1;
269 
270 	evsel->handler = handler;
271 	perf_evlist__add(evlist, evsel);
272 	return 0;
273 }
274 
275 static int perf_evlist__nr_threads(struct perf_evlist *evlist,
276 				   struct perf_evsel *evsel)
277 {
278 	if (evsel->system_wide)
279 		return 1;
280 	else
281 		return thread_map__nr(evlist->threads);
282 }
283 
284 void perf_evlist__disable(struct perf_evlist *evlist)
285 {
286 	int cpu, thread;
287 	struct perf_evsel *pos;
288 	int nr_cpus = cpu_map__nr(evlist->cpus);
289 	int nr_threads;
290 
291 	for (cpu = 0; cpu < nr_cpus; cpu++) {
292 		evlist__for_each(evlist, pos) {
293 			if (!perf_evsel__is_group_leader(pos) || !pos->fd)
294 				continue;
295 			nr_threads = perf_evlist__nr_threads(evlist, pos);
296 			for (thread = 0; thread < nr_threads; thread++)
297 				ioctl(FD(pos, cpu, thread),
298 				      PERF_EVENT_IOC_DISABLE, 0);
299 		}
300 	}
301 }
302 
303 void perf_evlist__enable(struct perf_evlist *evlist)
304 {
305 	int cpu, thread;
306 	struct perf_evsel *pos;
307 	int nr_cpus = cpu_map__nr(evlist->cpus);
308 	int nr_threads;
309 
310 	for (cpu = 0; cpu < nr_cpus; cpu++) {
311 		evlist__for_each(evlist, pos) {
312 			if (!perf_evsel__is_group_leader(pos) || !pos->fd)
313 				continue;
314 			nr_threads = perf_evlist__nr_threads(evlist, pos);
315 			for (thread = 0; thread < nr_threads; thread++)
316 				ioctl(FD(pos, cpu, thread),
317 				      PERF_EVENT_IOC_ENABLE, 0);
318 		}
319 	}
320 }
321 
322 int perf_evlist__disable_event(struct perf_evlist *evlist,
323 			       struct perf_evsel *evsel)
324 {
325 	int cpu, thread, err;
326 	int nr_cpus = cpu_map__nr(evlist->cpus);
327 	int nr_threads = perf_evlist__nr_threads(evlist, evsel);
328 
329 	if (!evsel->fd)
330 		return 0;
331 
332 	for (cpu = 0; cpu < nr_cpus; cpu++) {
333 		for (thread = 0; thread < nr_threads; thread++) {
334 			err = ioctl(FD(evsel, cpu, thread),
335 				    PERF_EVENT_IOC_DISABLE, 0);
336 			if (err)
337 				return err;
338 		}
339 	}
340 	return 0;
341 }
342 
343 int perf_evlist__enable_event(struct perf_evlist *evlist,
344 			      struct perf_evsel *evsel)
345 {
346 	int cpu, thread, err;
347 	int nr_cpus = cpu_map__nr(evlist->cpus);
348 	int nr_threads = perf_evlist__nr_threads(evlist, evsel);
349 
350 	if (!evsel->fd)
351 		return -EINVAL;
352 
353 	for (cpu = 0; cpu < nr_cpus; cpu++) {
354 		for (thread = 0; thread < nr_threads; thread++) {
355 			err = ioctl(FD(evsel, cpu, thread),
356 				    PERF_EVENT_IOC_ENABLE, 0);
357 			if (err)
358 				return err;
359 		}
360 	}
361 	return 0;
362 }
363 
364 static int perf_evlist__enable_event_cpu(struct perf_evlist *evlist,
365 					 struct perf_evsel *evsel, int cpu)
366 {
367 	int thread, err;
368 	int nr_threads = perf_evlist__nr_threads(evlist, evsel);
369 
370 	if (!evsel->fd)
371 		return -EINVAL;
372 
373 	for (thread = 0; thread < nr_threads; thread++) {
374 		err = ioctl(FD(evsel, cpu, thread),
375 			    PERF_EVENT_IOC_ENABLE, 0);
376 		if (err)
377 			return err;
378 	}
379 	return 0;
380 }
381 
382 static int perf_evlist__enable_event_thread(struct perf_evlist *evlist,
383 					    struct perf_evsel *evsel,
384 					    int thread)
385 {
386 	int cpu, err;
387 	int nr_cpus = cpu_map__nr(evlist->cpus);
388 
389 	if (!evsel->fd)
390 		return -EINVAL;
391 
392 	for (cpu = 0; cpu < nr_cpus; cpu++) {
393 		err = ioctl(FD(evsel, cpu, thread), PERF_EVENT_IOC_ENABLE, 0);
394 		if (err)
395 			return err;
396 	}
397 	return 0;
398 }
399 
400 int perf_evlist__enable_event_idx(struct perf_evlist *evlist,
401 				  struct perf_evsel *evsel, int idx)
402 {
403 	bool per_cpu_mmaps = !cpu_map__empty(evlist->cpus);
404 
405 	if (per_cpu_mmaps)
406 		return perf_evlist__enable_event_cpu(evlist, evsel, idx);
407 	else
408 		return perf_evlist__enable_event_thread(evlist, evsel, idx);
409 }
410 
411 int perf_evlist__alloc_pollfd(struct perf_evlist *evlist)
412 {
413 	int nr_cpus = cpu_map__nr(evlist->cpus);
414 	int nr_threads = thread_map__nr(evlist->threads);
415 	int nfds = 0;
416 	struct perf_evsel *evsel;
417 
418 	evlist__for_each(evlist, evsel) {
419 		if (evsel->system_wide)
420 			nfds += nr_cpus;
421 		else
422 			nfds += nr_cpus * nr_threads;
423 	}
424 
425 	if (fdarray__available_entries(&evlist->pollfd) < nfds &&
426 	    fdarray__grow(&evlist->pollfd, nfds) < 0)
427 		return -ENOMEM;
428 
429 	return 0;
430 }
431 
432 static int __perf_evlist__add_pollfd(struct perf_evlist *evlist, int fd, int idx)
433 {
434 	int pos = fdarray__add(&evlist->pollfd, fd, POLLIN | POLLERR | POLLHUP);
435 	/*
436 	 * Save the idx so that when we filter out fds POLLHUP'ed we can
437 	 * close the associated evlist->mmap[] entry.
438 	 */
439 	if (pos >= 0) {
440 		evlist->pollfd.priv[pos].idx = idx;
441 
442 		fcntl(fd, F_SETFL, O_NONBLOCK);
443 	}
444 
445 	return pos;
446 }
447 
448 int perf_evlist__add_pollfd(struct perf_evlist *evlist, int fd)
449 {
450 	return __perf_evlist__add_pollfd(evlist, fd, -1);
451 }
452 
453 static void perf_evlist__munmap_filtered(struct fdarray *fda, int fd)
454 {
455 	struct perf_evlist *evlist = container_of(fda, struct perf_evlist, pollfd);
456 
457 	perf_evlist__mmap_put(evlist, fda->priv[fd].idx);
458 }
459 
460 int perf_evlist__filter_pollfd(struct perf_evlist *evlist, short revents_and_mask)
461 {
462 	return fdarray__filter(&evlist->pollfd, revents_and_mask,
463 			       perf_evlist__munmap_filtered);
464 }
465 
466 int perf_evlist__poll(struct perf_evlist *evlist, int timeout)
467 {
468 	return fdarray__poll(&evlist->pollfd, timeout);
469 }
470 
471 static void perf_evlist__id_hash(struct perf_evlist *evlist,
472 				 struct perf_evsel *evsel,
473 				 int cpu, int thread, u64 id)
474 {
475 	int hash;
476 	struct perf_sample_id *sid = SID(evsel, cpu, thread);
477 
478 	sid->id = id;
479 	sid->evsel = evsel;
480 	hash = hash_64(sid->id, PERF_EVLIST__HLIST_BITS);
481 	hlist_add_head(&sid->node, &evlist->heads[hash]);
482 }
483 
484 void perf_evlist__id_add(struct perf_evlist *evlist, struct perf_evsel *evsel,
485 			 int cpu, int thread, u64 id)
486 {
487 	perf_evlist__id_hash(evlist, evsel, cpu, thread, id);
488 	evsel->id[evsel->ids++] = id;
489 }
490 
491 static int perf_evlist__id_add_fd(struct perf_evlist *evlist,
492 				  struct perf_evsel *evsel,
493 				  int cpu, int thread, int fd)
494 {
495 	u64 read_data[4] = { 0, };
496 	int id_idx = 1; /* The first entry is the counter value */
497 	u64 id;
498 	int ret;
499 
500 	ret = ioctl(fd, PERF_EVENT_IOC_ID, &id);
501 	if (!ret)
502 		goto add;
503 
504 	if (errno != ENOTTY)
505 		return -1;
506 
507 	/* Legacy way to get event id.. All hail to old kernels! */
508 
509 	/*
510 	 * This way does not work with group format read, so bail
511 	 * out in that case.
512 	 */
513 	if (perf_evlist__read_format(evlist) & PERF_FORMAT_GROUP)
514 		return -1;
515 
516 	if (!(evsel->attr.read_format & PERF_FORMAT_ID) ||
517 	    read(fd, &read_data, sizeof(read_data)) == -1)
518 		return -1;
519 
520 	if (evsel->attr.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
521 		++id_idx;
522 	if (evsel->attr.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
523 		++id_idx;
524 
525 	id = read_data[id_idx];
526 
527  add:
528 	perf_evlist__id_add(evlist, evsel, cpu, thread, id);
529 	return 0;
530 }
531 
532 static void perf_evlist__set_sid_idx(struct perf_evlist *evlist,
533 				     struct perf_evsel *evsel, int idx, int cpu,
534 				     int thread)
535 {
536 	struct perf_sample_id *sid = SID(evsel, cpu, thread);
537 	sid->idx = idx;
538 	if (evlist->cpus && cpu >= 0)
539 		sid->cpu = evlist->cpus->map[cpu];
540 	else
541 		sid->cpu = -1;
542 	if (!evsel->system_wide && evlist->threads && thread >= 0)
543 		sid->tid = evlist->threads->map[thread];
544 	else
545 		sid->tid = -1;
546 }
547 
548 struct perf_sample_id *perf_evlist__id2sid(struct perf_evlist *evlist, u64 id)
549 {
550 	struct hlist_head *head;
551 	struct perf_sample_id *sid;
552 	int hash;
553 
554 	hash = hash_64(id, PERF_EVLIST__HLIST_BITS);
555 	head = &evlist->heads[hash];
556 
557 	hlist_for_each_entry(sid, head, node)
558 		if (sid->id == id)
559 			return sid;
560 
561 	return NULL;
562 }
563 
564 struct perf_evsel *perf_evlist__id2evsel(struct perf_evlist *evlist, u64 id)
565 {
566 	struct perf_sample_id *sid;
567 
568 	if (evlist->nr_entries == 1)
569 		return perf_evlist__first(evlist);
570 
571 	sid = perf_evlist__id2sid(evlist, id);
572 	if (sid)
573 		return sid->evsel;
574 
575 	if (!perf_evlist__sample_id_all(evlist))
576 		return perf_evlist__first(evlist);
577 
578 	return NULL;
579 }
580 
581 static int perf_evlist__event2id(struct perf_evlist *evlist,
582 				 union perf_event *event, u64 *id)
583 {
584 	const u64 *array = event->sample.array;
585 	ssize_t n;
586 
587 	n = (event->header.size - sizeof(event->header)) >> 3;
588 
589 	if (event->header.type == PERF_RECORD_SAMPLE) {
590 		if (evlist->id_pos >= n)
591 			return -1;
592 		*id = array[evlist->id_pos];
593 	} else {
594 		if (evlist->is_pos > n)
595 			return -1;
596 		n -= evlist->is_pos;
597 		*id = array[n];
598 	}
599 	return 0;
600 }
601 
602 static struct perf_evsel *perf_evlist__event2evsel(struct perf_evlist *evlist,
603 						   union perf_event *event)
604 {
605 	struct perf_evsel *first = perf_evlist__first(evlist);
606 	struct hlist_head *head;
607 	struct perf_sample_id *sid;
608 	int hash;
609 	u64 id;
610 
611 	if (evlist->nr_entries == 1)
612 		return first;
613 
614 	if (!first->attr.sample_id_all &&
615 	    event->header.type != PERF_RECORD_SAMPLE)
616 		return first;
617 
618 	if (perf_evlist__event2id(evlist, event, &id))
619 		return NULL;
620 
621 	/* Synthesized events have an id of zero */
622 	if (!id)
623 		return first;
624 
625 	hash = hash_64(id, PERF_EVLIST__HLIST_BITS);
626 	head = &evlist->heads[hash];
627 
628 	hlist_for_each_entry(sid, head, node) {
629 		if (sid->id == id)
630 			return sid->evsel;
631 	}
632 	return NULL;
633 }
634 
635 union perf_event *perf_evlist__mmap_read(struct perf_evlist *evlist, int idx)
636 {
637 	struct perf_mmap *md = &evlist->mmap[idx];
638 	unsigned int head = perf_mmap__read_head(md);
639 	unsigned int old = md->prev;
640 	unsigned char *data = md->base + page_size;
641 	union perf_event *event = NULL;
642 
643 	if (evlist->overwrite) {
644 		/*
645 		 * If we're further behind than half the buffer, there's a chance
646 		 * the writer will bite our tail and mess up the samples under us.
647 		 *
648 		 * If we somehow ended up ahead of the head, we got messed up.
649 		 *
650 		 * In either case, truncate and restart at head.
651 		 */
652 		int diff = head - old;
653 		if (diff > md->mask / 2 || diff < 0) {
654 			fprintf(stderr, "WARNING: failed to keep up with mmap data.\n");
655 
656 			/*
657 			 * head points to a known good entry, start there.
658 			 */
659 			old = head;
660 		}
661 	}
662 
663 	if (old != head) {
664 		size_t size;
665 
666 		event = (union perf_event *)&data[old & md->mask];
667 		size = event->header.size;
668 
669 		/*
670 		 * Event straddles the mmap boundary -- header should always
671 		 * be inside due to u64 alignment of output.
672 		 */
673 		if ((old & md->mask) + size != ((old + size) & md->mask)) {
674 			unsigned int offset = old;
675 			unsigned int len = min(sizeof(*event), size), cpy;
676 			void *dst = md->event_copy;
677 
678 			do {
679 				cpy = min(md->mask + 1 - (offset & md->mask), len);
680 				memcpy(dst, &data[offset & md->mask], cpy);
681 				offset += cpy;
682 				dst += cpy;
683 				len -= cpy;
684 			} while (len);
685 
686 			event = (union perf_event *) md->event_copy;
687 		}
688 
689 		old += size;
690 	}
691 
692 	md->prev = old;
693 
694 	return event;
695 }
696 
697 static bool perf_mmap__empty(struct perf_mmap *md)
698 {
699 	return perf_mmap__read_head(md) != md->prev;
700 }
701 
702 static void perf_evlist__mmap_get(struct perf_evlist *evlist, int idx)
703 {
704 	++evlist->mmap[idx].refcnt;
705 }
706 
707 static void perf_evlist__mmap_put(struct perf_evlist *evlist, int idx)
708 {
709 	BUG_ON(evlist->mmap[idx].refcnt == 0);
710 
711 	if (--evlist->mmap[idx].refcnt == 0)
712 		__perf_evlist__munmap(evlist, idx);
713 }
714 
715 void perf_evlist__mmap_consume(struct perf_evlist *evlist, int idx)
716 {
717 	struct perf_mmap *md = &evlist->mmap[idx];
718 
719 	if (!evlist->overwrite) {
720 		unsigned int old = md->prev;
721 
722 		perf_mmap__write_tail(md, old);
723 	}
724 
725 	if (md->refcnt == 1 && perf_mmap__empty(md))
726 		perf_evlist__mmap_put(evlist, idx);
727 }
728 
729 static void __perf_evlist__munmap(struct perf_evlist *evlist, int idx)
730 {
731 	if (evlist->mmap[idx].base != NULL) {
732 		munmap(evlist->mmap[idx].base, evlist->mmap_len);
733 		evlist->mmap[idx].base = NULL;
734 		evlist->mmap[idx].refcnt = 0;
735 	}
736 }
737 
738 void perf_evlist__munmap(struct perf_evlist *evlist)
739 {
740 	int i;
741 
742 	if (evlist->mmap == NULL)
743 		return;
744 
745 	for (i = 0; i < evlist->nr_mmaps; i++)
746 		__perf_evlist__munmap(evlist, i);
747 
748 	zfree(&evlist->mmap);
749 }
750 
751 static int perf_evlist__alloc_mmap(struct perf_evlist *evlist)
752 {
753 	evlist->nr_mmaps = cpu_map__nr(evlist->cpus);
754 	if (cpu_map__empty(evlist->cpus))
755 		evlist->nr_mmaps = thread_map__nr(evlist->threads);
756 	evlist->mmap = zalloc(evlist->nr_mmaps * sizeof(struct perf_mmap));
757 	return evlist->mmap != NULL ? 0 : -ENOMEM;
758 }
759 
760 struct mmap_params {
761 	int prot;
762 	int mask;
763 };
764 
765 static int __perf_evlist__mmap(struct perf_evlist *evlist, int idx,
766 			       struct mmap_params *mp, int fd)
767 {
768 	/*
769 	 * The last one will be done at perf_evlist__mmap_consume(), so that we
770 	 * make sure we don't prevent tools from consuming every last event in
771 	 * the ring buffer.
772 	 *
773 	 * I.e. we can get the POLLHUP meaning that the fd doesn't exist
774 	 * anymore, but the last events for it are still in the ring buffer,
775 	 * waiting to be consumed.
776 	 *
777 	 * Tools can chose to ignore this at their own discretion, but the
778 	 * evlist layer can't just drop it when filtering events in
779 	 * perf_evlist__filter_pollfd().
780 	 */
781 	evlist->mmap[idx].refcnt = 2;
782 	evlist->mmap[idx].prev = 0;
783 	evlist->mmap[idx].mask = mp->mask;
784 	evlist->mmap[idx].base = mmap(NULL, evlist->mmap_len, mp->prot,
785 				      MAP_SHARED, fd, 0);
786 	if (evlist->mmap[idx].base == MAP_FAILED) {
787 		pr_debug2("failed to mmap perf event ring buffer, error %d\n",
788 			  errno);
789 		evlist->mmap[idx].base = NULL;
790 		return -1;
791 	}
792 
793 	return 0;
794 }
795 
796 static int perf_evlist__mmap_per_evsel(struct perf_evlist *evlist, int idx,
797 				       struct mmap_params *mp, int cpu,
798 				       int thread, int *output)
799 {
800 	struct perf_evsel *evsel;
801 
802 	evlist__for_each(evlist, evsel) {
803 		int fd;
804 
805 		if (evsel->system_wide && thread)
806 			continue;
807 
808 		fd = FD(evsel, cpu, thread);
809 
810 		if (*output == -1) {
811 			*output = fd;
812 			if (__perf_evlist__mmap(evlist, idx, mp, *output) < 0)
813 				return -1;
814 		} else {
815 			if (ioctl(fd, PERF_EVENT_IOC_SET_OUTPUT, *output) != 0)
816 				return -1;
817 
818 			perf_evlist__mmap_get(evlist, idx);
819 		}
820 
821 		/*
822 		 * The system_wide flag causes a selected event to be opened
823 		 * always without a pid.  Consequently it will never get a
824 		 * POLLHUP, but it is used for tracking in combination with
825 		 * other events, so it should not need to be polled anyway.
826 		 * Therefore don't add it for polling.
827 		 */
828 		if (!evsel->system_wide &&
829 		    __perf_evlist__add_pollfd(evlist, fd, idx) < 0) {
830 			perf_evlist__mmap_put(evlist, idx);
831 			return -1;
832 		}
833 
834 		if (evsel->attr.read_format & PERF_FORMAT_ID) {
835 			if (perf_evlist__id_add_fd(evlist, evsel, cpu, thread,
836 						   fd) < 0)
837 				return -1;
838 			perf_evlist__set_sid_idx(evlist, evsel, idx, cpu,
839 						 thread);
840 		}
841 	}
842 
843 	return 0;
844 }
845 
846 static int perf_evlist__mmap_per_cpu(struct perf_evlist *evlist,
847 				     struct mmap_params *mp)
848 {
849 	int cpu, thread;
850 	int nr_cpus = cpu_map__nr(evlist->cpus);
851 	int nr_threads = thread_map__nr(evlist->threads);
852 
853 	pr_debug2("perf event ring buffer mmapped per cpu\n");
854 	for (cpu = 0; cpu < nr_cpus; cpu++) {
855 		int output = -1;
856 
857 		for (thread = 0; thread < nr_threads; thread++) {
858 			if (perf_evlist__mmap_per_evsel(evlist, cpu, mp, cpu,
859 							thread, &output))
860 				goto out_unmap;
861 		}
862 	}
863 
864 	return 0;
865 
866 out_unmap:
867 	for (cpu = 0; cpu < nr_cpus; cpu++)
868 		__perf_evlist__munmap(evlist, cpu);
869 	return -1;
870 }
871 
872 static int perf_evlist__mmap_per_thread(struct perf_evlist *evlist,
873 					struct mmap_params *mp)
874 {
875 	int thread;
876 	int nr_threads = thread_map__nr(evlist->threads);
877 
878 	pr_debug2("perf event ring buffer mmapped per thread\n");
879 	for (thread = 0; thread < nr_threads; thread++) {
880 		int output = -1;
881 
882 		if (perf_evlist__mmap_per_evsel(evlist, thread, mp, 0, thread,
883 						&output))
884 			goto out_unmap;
885 	}
886 
887 	return 0;
888 
889 out_unmap:
890 	for (thread = 0; thread < nr_threads; thread++)
891 		__perf_evlist__munmap(evlist, thread);
892 	return -1;
893 }
894 
895 static size_t perf_evlist__mmap_size(unsigned long pages)
896 {
897 	if (pages == UINT_MAX) {
898 		int max;
899 
900 		if (sysctl__read_int("kernel/perf_event_mlock_kb", &max) < 0) {
901 			/*
902 			 * Pick a once upon a time good value, i.e. things look
903 			 * strange since we can't read a sysctl value, but lets not
904 			 * die yet...
905 			 */
906 			max = 512;
907 		} else {
908 			max -= (page_size / 1024);
909 		}
910 
911 		pages = (max * 1024) / page_size;
912 		if (!is_power_of_2(pages))
913 			pages = rounddown_pow_of_two(pages);
914 	} else if (!is_power_of_2(pages))
915 		return 0;
916 
917 	return (pages + 1) * page_size;
918 }
919 
920 static long parse_pages_arg(const char *str, unsigned long min,
921 			    unsigned long max)
922 {
923 	unsigned long pages, val;
924 	static struct parse_tag tags[] = {
925 		{ .tag  = 'B', .mult = 1       },
926 		{ .tag  = 'K', .mult = 1 << 10 },
927 		{ .tag  = 'M', .mult = 1 << 20 },
928 		{ .tag  = 'G', .mult = 1 << 30 },
929 		{ .tag  = 0 },
930 	};
931 
932 	if (str == NULL)
933 		return -EINVAL;
934 
935 	val = parse_tag_value(str, tags);
936 	if (val != (unsigned long) -1) {
937 		/* we got file size value */
938 		pages = PERF_ALIGN(val, page_size) / page_size;
939 	} else {
940 		/* we got pages count value */
941 		char *eptr;
942 		pages = strtoul(str, &eptr, 10);
943 		if (*eptr != '\0')
944 			return -EINVAL;
945 	}
946 
947 	if (pages == 0 && min == 0) {
948 		/* leave number of pages at 0 */
949 	} else if (!is_power_of_2(pages)) {
950 		/* round pages up to next power of 2 */
951 		pages = roundup_pow_of_two(pages);
952 		if (!pages)
953 			return -EINVAL;
954 		pr_info("rounding mmap pages size to %lu bytes (%lu pages)\n",
955 			pages * page_size, pages);
956 	}
957 
958 	if (pages > max)
959 		return -EINVAL;
960 
961 	return pages;
962 }
963 
964 int perf_evlist__parse_mmap_pages(const struct option *opt, const char *str,
965 				  int unset __maybe_unused)
966 {
967 	unsigned int *mmap_pages = opt->value;
968 	unsigned long max = UINT_MAX;
969 	long pages;
970 
971 	if (max > SIZE_MAX / page_size)
972 		max = SIZE_MAX / page_size;
973 
974 	pages = parse_pages_arg(str, 1, max);
975 	if (pages < 0) {
976 		pr_err("Invalid argument for --mmap_pages/-m\n");
977 		return -1;
978 	}
979 
980 	*mmap_pages = pages;
981 	return 0;
982 }
983 
984 /**
985  * perf_evlist__mmap - Create mmaps to receive events.
986  * @evlist: list of events
987  * @pages: map length in pages
988  * @overwrite: overwrite older events?
989  *
990  * If @overwrite is %false the user needs to signal event consumption using
991  * perf_mmap__write_tail().  Using perf_evlist__mmap_read() does this
992  * automatically.
993  *
994  * Return: %0 on success, negative error code otherwise.
995  */
996 int perf_evlist__mmap(struct perf_evlist *evlist, unsigned int pages,
997 		      bool overwrite)
998 {
999 	struct perf_evsel *evsel;
1000 	const struct cpu_map *cpus = evlist->cpus;
1001 	const struct thread_map *threads = evlist->threads;
1002 	struct mmap_params mp = {
1003 		.prot = PROT_READ | (overwrite ? 0 : PROT_WRITE),
1004 	};
1005 
1006 	if (evlist->mmap == NULL && perf_evlist__alloc_mmap(evlist) < 0)
1007 		return -ENOMEM;
1008 
1009 	if (evlist->pollfd.entries == NULL && perf_evlist__alloc_pollfd(evlist) < 0)
1010 		return -ENOMEM;
1011 
1012 	evlist->overwrite = overwrite;
1013 	evlist->mmap_len = perf_evlist__mmap_size(pages);
1014 	pr_debug("mmap size %zuB\n", evlist->mmap_len);
1015 	mp.mask = evlist->mmap_len - page_size - 1;
1016 
1017 	evlist__for_each(evlist, evsel) {
1018 		if ((evsel->attr.read_format & PERF_FORMAT_ID) &&
1019 		    evsel->sample_id == NULL &&
1020 		    perf_evsel__alloc_id(evsel, cpu_map__nr(cpus), threads->nr) < 0)
1021 			return -ENOMEM;
1022 	}
1023 
1024 	if (cpu_map__empty(cpus))
1025 		return perf_evlist__mmap_per_thread(evlist, &mp);
1026 
1027 	return perf_evlist__mmap_per_cpu(evlist, &mp);
1028 }
1029 
1030 int perf_evlist__create_maps(struct perf_evlist *evlist, struct target *target)
1031 {
1032 	evlist->threads = thread_map__new_str(target->pid, target->tid,
1033 					      target->uid);
1034 
1035 	if (evlist->threads == NULL)
1036 		return -1;
1037 
1038 	if (target__uses_dummy_map(target))
1039 		evlist->cpus = cpu_map__dummy_new();
1040 	else
1041 		evlist->cpus = cpu_map__new(target->cpu_list);
1042 
1043 	if (evlist->cpus == NULL)
1044 		goto out_delete_threads;
1045 
1046 	return 0;
1047 
1048 out_delete_threads:
1049 	thread_map__delete(evlist->threads);
1050 	evlist->threads = NULL;
1051 	return -1;
1052 }
1053 
1054 int perf_evlist__apply_filters(struct perf_evlist *evlist)
1055 {
1056 	struct perf_evsel *evsel;
1057 	int err = 0;
1058 	const int ncpus = cpu_map__nr(evlist->cpus),
1059 		  nthreads = thread_map__nr(evlist->threads);
1060 
1061 	evlist__for_each(evlist, evsel) {
1062 		if (evsel->filter == NULL)
1063 			continue;
1064 
1065 		err = perf_evsel__set_filter(evsel, ncpus, nthreads, evsel->filter);
1066 		if (err)
1067 			break;
1068 	}
1069 
1070 	return err;
1071 }
1072 
1073 int perf_evlist__set_filter(struct perf_evlist *evlist, const char *filter)
1074 {
1075 	struct perf_evsel *evsel;
1076 	int err = 0;
1077 	const int ncpus = cpu_map__nr(evlist->cpus),
1078 		  nthreads = thread_map__nr(evlist->threads);
1079 
1080 	evlist__for_each(evlist, evsel) {
1081 		err = perf_evsel__set_filter(evsel, ncpus, nthreads, filter);
1082 		if (err)
1083 			break;
1084 	}
1085 
1086 	return err;
1087 }
1088 
1089 bool perf_evlist__valid_sample_type(struct perf_evlist *evlist)
1090 {
1091 	struct perf_evsel *pos;
1092 
1093 	if (evlist->nr_entries == 1)
1094 		return true;
1095 
1096 	if (evlist->id_pos < 0 || evlist->is_pos < 0)
1097 		return false;
1098 
1099 	evlist__for_each(evlist, pos) {
1100 		if (pos->id_pos != evlist->id_pos ||
1101 		    pos->is_pos != evlist->is_pos)
1102 			return false;
1103 	}
1104 
1105 	return true;
1106 }
1107 
1108 u64 __perf_evlist__combined_sample_type(struct perf_evlist *evlist)
1109 {
1110 	struct perf_evsel *evsel;
1111 
1112 	if (evlist->combined_sample_type)
1113 		return evlist->combined_sample_type;
1114 
1115 	evlist__for_each(evlist, evsel)
1116 		evlist->combined_sample_type |= evsel->attr.sample_type;
1117 
1118 	return evlist->combined_sample_type;
1119 }
1120 
1121 u64 perf_evlist__combined_sample_type(struct perf_evlist *evlist)
1122 {
1123 	evlist->combined_sample_type = 0;
1124 	return __perf_evlist__combined_sample_type(evlist);
1125 }
1126 
1127 bool perf_evlist__valid_read_format(struct perf_evlist *evlist)
1128 {
1129 	struct perf_evsel *first = perf_evlist__first(evlist), *pos = first;
1130 	u64 read_format = first->attr.read_format;
1131 	u64 sample_type = first->attr.sample_type;
1132 
1133 	evlist__for_each(evlist, pos) {
1134 		if (read_format != pos->attr.read_format)
1135 			return false;
1136 	}
1137 
1138 	/* PERF_SAMPLE_READ imples PERF_FORMAT_ID. */
1139 	if ((sample_type & PERF_SAMPLE_READ) &&
1140 	    !(read_format & PERF_FORMAT_ID)) {
1141 		return false;
1142 	}
1143 
1144 	return true;
1145 }
1146 
1147 u64 perf_evlist__read_format(struct perf_evlist *evlist)
1148 {
1149 	struct perf_evsel *first = perf_evlist__first(evlist);
1150 	return first->attr.read_format;
1151 }
1152 
1153 u16 perf_evlist__id_hdr_size(struct perf_evlist *evlist)
1154 {
1155 	struct perf_evsel *first = perf_evlist__first(evlist);
1156 	struct perf_sample *data;
1157 	u64 sample_type;
1158 	u16 size = 0;
1159 
1160 	if (!first->attr.sample_id_all)
1161 		goto out;
1162 
1163 	sample_type = first->attr.sample_type;
1164 
1165 	if (sample_type & PERF_SAMPLE_TID)
1166 		size += sizeof(data->tid) * 2;
1167 
1168        if (sample_type & PERF_SAMPLE_TIME)
1169 		size += sizeof(data->time);
1170 
1171 	if (sample_type & PERF_SAMPLE_ID)
1172 		size += sizeof(data->id);
1173 
1174 	if (sample_type & PERF_SAMPLE_STREAM_ID)
1175 		size += sizeof(data->stream_id);
1176 
1177 	if (sample_type & PERF_SAMPLE_CPU)
1178 		size += sizeof(data->cpu) * 2;
1179 
1180 	if (sample_type & PERF_SAMPLE_IDENTIFIER)
1181 		size += sizeof(data->id);
1182 out:
1183 	return size;
1184 }
1185 
1186 bool perf_evlist__valid_sample_id_all(struct perf_evlist *evlist)
1187 {
1188 	struct perf_evsel *first = perf_evlist__first(evlist), *pos = first;
1189 
1190 	evlist__for_each_continue(evlist, pos) {
1191 		if (first->attr.sample_id_all != pos->attr.sample_id_all)
1192 			return false;
1193 	}
1194 
1195 	return true;
1196 }
1197 
1198 bool perf_evlist__sample_id_all(struct perf_evlist *evlist)
1199 {
1200 	struct perf_evsel *first = perf_evlist__first(evlist);
1201 	return first->attr.sample_id_all;
1202 }
1203 
1204 void perf_evlist__set_selected(struct perf_evlist *evlist,
1205 			       struct perf_evsel *evsel)
1206 {
1207 	evlist->selected = evsel;
1208 }
1209 
1210 void perf_evlist__close(struct perf_evlist *evlist)
1211 {
1212 	struct perf_evsel *evsel;
1213 	int ncpus = cpu_map__nr(evlist->cpus);
1214 	int nthreads = thread_map__nr(evlist->threads);
1215 	int n;
1216 
1217 	evlist__for_each_reverse(evlist, evsel) {
1218 		n = evsel->cpus ? evsel->cpus->nr : ncpus;
1219 		perf_evsel__close(evsel, n, nthreads);
1220 	}
1221 }
1222 
1223 static int perf_evlist__create_syswide_maps(struct perf_evlist *evlist)
1224 {
1225 	int err = -ENOMEM;
1226 
1227 	/*
1228 	 * Try reading /sys/devices/system/cpu/online to get
1229 	 * an all cpus map.
1230 	 *
1231 	 * FIXME: -ENOMEM is the best we can do here, the cpu_map
1232 	 * code needs an overhaul to properly forward the
1233 	 * error, and we may not want to do that fallback to a
1234 	 * default cpu identity map :-\
1235 	 */
1236 	evlist->cpus = cpu_map__new(NULL);
1237 	if (evlist->cpus == NULL)
1238 		goto out;
1239 
1240 	evlist->threads = thread_map__new_dummy();
1241 	if (evlist->threads == NULL)
1242 		goto out_free_cpus;
1243 
1244 	err = 0;
1245 out:
1246 	return err;
1247 out_free_cpus:
1248 	cpu_map__delete(evlist->cpus);
1249 	evlist->cpus = NULL;
1250 	goto out;
1251 }
1252 
1253 int perf_evlist__open(struct perf_evlist *evlist)
1254 {
1255 	struct perf_evsel *evsel;
1256 	int err;
1257 
1258 	/*
1259 	 * Default: one fd per CPU, all threads, aka systemwide
1260 	 * as sys_perf_event_open(cpu = -1, thread = -1) is EINVAL
1261 	 */
1262 	if (evlist->threads == NULL && evlist->cpus == NULL) {
1263 		err = perf_evlist__create_syswide_maps(evlist);
1264 		if (err < 0)
1265 			goto out_err;
1266 	}
1267 
1268 	perf_evlist__update_id_pos(evlist);
1269 
1270 	evlist__for_each(evlist, evsel) {
1271 		err = perf_evsel__open(evsel, evlist->cpus, evlist->threads);
1272 		if (err < 0)
1273 			goto out_err;
1274 	}
1275 
1276 	return 0;
1277 out_err:
1278 	perf_evlist__close(evlist);
1279 	errno = -err;
1280 	return err;
1281 }
1282 
1283 int perf_evlist__prepare_workload(struct perf_evlist *evlist, struct target *target,
1284 				  const char *argv[], bool pipe_output,
1285 				  void (*exec_error)(int signo, siginfo_t *info, void *ucontext))
1286 {
1287 	int child_ready_pipe[2], go_pipe[2];
1288 	char bf;
1289 
1290 	if (pipe(child_ready_pipe) < 0) {
1291 		perror("failed to create 'ready' pipe");
1292 		return -1;
1293 	}
1294 
1295 	if (pipe(go_pipe) < 0) {
1296 		perror("failed to create 'go' pipe");
1297 		goto out_close_ready_pipe;
1298 	}
1299 
1300 	evlist->workload.pid = fork();
1301 	if (evlist->workload.pid < 0) {
1302 		perror("failed to fork");
1303 		goto out_close_pipes;
1304 	}
1305 
1306 	if (!evlist->workload.pid) {
1307 		int ret;
1308 
1309 		if (pipe_output)
1310 			dup2(2, 1);
1311 
1312 		signal(SIGTERM, SIG_DFL);
1313 
1314 		close(child_ready_pipe[0]);
1315 		close(go_pipe[1]);
1316 		fcntl(go_pipe[0], F_SETFD, FD_CLOEXEC);
1317 
1318 		/*
1319 		 * Tell the parent we're ready to go
1320 		 */
1321 		close(child_ready_pipe[1]);
1322 
1323 		/*
1324 		 * Wait until the parent tells us to go.
1325 		 */
1326 		ret = read(go_pipe[0], &bf, 1);
1327 		/*
1328 		 * The parent will ask for the execvp() to be performed by
1329 		 * writing exactly one byte, in workload.cork_fd, usually via
1330 		 * perf_evlist__start_workload().
1331 		 *
1332 		 * For cancelling the workload without actuallin running it,
1333 		 * the parent will just close workload.cork_fd, without writing
1334 		 * anything, i.e. read will return zero and we just exit()
1335 		 * here.
1336 		 */
1337 		if (ret != 1) {
1338 			if (ret == -1)
1339 				perror("unable to read pipe");
1340 			exit(ret);
1341 		}
1342 
1343 		execvp(argv[0], (char **)argv);
1344 
1345 		if (exec_error) {
1346 			union sigval val;
1347 
1348 			val.sival_int = errno;
1349 			if (sigqueue(getppid(), SIGUSR1, val))
1350 				perror(argv[0]);
1351 		} else
1352 			perror(argv[0]);
1353 		exit(-1);
1354 	}
1355 
1356 	if (exec_error) {
1357 		struct sigaction act = {
1358 			.sa_flags     = SA_SIGINFO,
1359 			.sa_sigaction = exec_error,
1360 		};
1361 		sigaction(SIGUSR1, &act, NULL);
1362 	}
1363 
1364 	if (target__none(target)) {
1365 		if (evlist->threads == NULL) {
1366 			fprintf(stderr, "FATAL: evlist->threads need to be set at this point (%s:%d).\n",
1367 				__func__, __LINE__);
1368 			goto out_close_pipes;
1369 		}
1370 		evlist->threads->map[0] = evlist->workload.pid;
1371 	}
1372 
1373 	close(child_ready_pipe[1]);
1374 	close(go_pipe[0]);
1375 	/*
1376 	 * wait for child to settle
1377 	 */
1378 	if (read(child_ready_pipe[0], &bf, 1) == -1) {
1379 		perror("unable to read pipe");
1380 		goto out_close_pipes;
1381 	}
1382 
1383 	fcntl(go_pipe[1], F_SETFD, FD_CLOEXEC);
1384 	evlist->workload.cork_fd = go_pipe[1];
1385 	close(child_ready_pipe[0]);
1386 	return 0;
1387 
1388 out_close_pipes:
1389 	close(go_pipe[0]);
1390 	close(go_pipe[1]);
1391 out_close_ready_pipe:
1392 	close(child_ready_pipe[0]);
1393 	close(child_ready_pipe[1]);
1394 	return -1;
1395 }
1396 
1397 int perf_evlist__start_workload(struct perf_evlist *evlist)
1398 {
1399 	if (evlist->workload.cork_fd > 0) {
1400 		char bf = 0;
1401 		int ret;
1402 		/*
1403 		 * Remove the cork, let it rip!
1404 		 */
1405 		ret = write(evlist->workload.cork_fd, &bf, 1);
1406 		if (ret < 0)
1407 			perror("enable to write to pipe");
1408 
1409 		close(evlist->workload.cork_fd);
1410 		return ret;
1411 	}
1412 
1413 	return 0;
1414 }
1415 
1416 int perf_evlist__parse_sample(struct perf_evlist *evlist, union perf_event *event,
1417 			      struct perf_sample *sample)
1418 {
1419 	struct perf_evsel *evsel = perf_evlist__event2evsel(evlist, event);
1420 
1421 	if (!evsel)
1422 		return -EFAULT;
1423 	return perf_evsel__parse_sample(evsel, event, sample);
1424 }
1425 
1426 size_t perf_evlist__fprintf(struct perf_evlist *evlist, FILE *fp)
1427 {
1428 	struct perf_evsel *evsel;
1429 	size_t printed = 0;
1430 
1431 	evlist__for_each(evlist, evsel) {
1432 		printed += fprintf(fp, "%s%s", evsel->idx ? ", " : "",
1433 				   perf_evsel__name(evsel));
1434 	}
1435 
1436 	return printed + fprintf(fp, "\n");
1437 }
1438 
1439 int perf_evlist__strerror_tp(struct perf_evlist *evlist __maybe_unused,
1440 			     int err, char *buf, size_t size)
1441 {
1442 	char sbuf[128];
1443 
1444 	switch (err) {
1445 	case ENOENT:
1446 		scnprintf(buf, size, "%s",
1447 			  "Error:\tUnable to find debugfs\n"
1448 			  "Hint:\tWas your kernel was compiled with debugfs support?\n"
1449 			  "Hint:\tIs the debugfs filesystem mounted?\n"
1450 			  "Hint:\tTry 'sudo mount -t debugfs nodev /sys/kernel/debug'");
1451 		break;
1452 	case EACCES:
1453 		scnprintf(buf, size,
1454 			  "Error:\tNo permissions to read %s/tracing/events/raw_syscalls\n"
1455 			  "Hint:\tTry 'sudo mount -o remount,mode=755 %s'\n",
1456 			  debugfs_mountpoint, debugfs_mountpoint);
1457 		break;
1458 	default:
1459 		scnprintf(buf, size, "%s", strerror_r(err, sbuf, sizeof(sbuf)));
1460 		break;
1461 	}
1462 
1463 	return 0;
1464 }
1465 
1466 int perf_evlist__strerror_open(struct perf_evlist *evlist __maybe_unused,
1467 			       int err, char *buf, size_t size)
1468 {
1469 	int printed, value;
1470 	char sbuf[STRERR_BUFSIZE], *emsg = strerror_r(err, sbuf, sizeof(sbuf));
1471 
1472 	switch (err) {
1473 	case EACCES:
1474 	case EPERM:
1475 		printed = scnprintf(buf, size,
1476 				    "Error:\t%s.\n"
1477 				    "Hint:\tCheck /proc/sys/kernel/perf_event_paranoid setting.", emsg);
1478 
1479 		value = perf_event_paranoid();
1480 
1481 		printed += scnprintf(buf + printed, size - printed, "\nHint:\t");
1482 
1483 		if (value >= 2) {
1484 			printed += scnprintf(buf + printed, size - printed,
1485 					     "For your workloads it needs to be <= 1\nHint:\t");
1486 		}
1487 		printed += scnprintf(buf + printed, size - printed,
1488 				     "For system wide tracing it needs to be set to -1.\n");
1489 
1490 		printed += scnprintf(buf + printed, size - printed,
1491 				    "Hint:\tTry: 'sudo sh -c \"echo -1 > /proc/sys/kernel/perf_event_paranoid\"'\n"
1492 				    "Hint:\tThe current value is %d.", value);
1493 		break;
1494 	default:
1495 		scnprintf(buf, size, "%s", emsg);
1496 		break;
1497 	}
1498 
1499 	return 0;
1500 }
1501 
1502 int perf_evlist__strerror_mmap(struct perf_evlist *evlist, int err, char *buf, size_t size)
1503 {
1504 	char sbuf[STRERR_BUFSIZE], *emsg = strerror_r(err, sbuf, sizeof(sbuf));
1505 	int pages_attempted = evlist->mmap_len / 1024, pages_max_per_user, printed = 0;
1506 
1507 	switch (err) {
1508 	case EPERM:
1509 		sysctl__read_int("kernel/perf_event_mlock_kb", &pages_max_per_user);
1510 		printed += scnprintf(buf + printed, size - printed,
1511 				     "Error:\t%s.\n"
1512 				     "Hint:\tCheck /proc/sys/kernel/perf_event_mlock_kb (%d kB) setting.\n"
1513 				     "Hint:\tTried using %zd kB.\n",
1514 				     emsg, pages_max_per_user, pages_attempted);
1515 
1516 		if (pages_attempted >= pages_max_per_user) {
1517 			printed += scnprintf(buf + printed, size - printed,
1518 					     "Hint:\tTry 'sudo sh -c \"echo %d > /proc/sys/kernel/perf_event_mlock_kb\"', or\n",
1519 					     pages_max_per_user + pages_attempted);
1520 		}
1521 
1522 		printed += scnprintf(buf + printed, size - printed,
1523 				     "Hint:\tTry using a smaller -m/--mmap-pages value.");
1524 		break;
1525 	default:
1526 		scnprintf(buf, size, "%s", emsg);
1527 		break;
1528 	}
1529 
1530 	return 0;
1531 }
1532 
1533 void perf_evlist__to_front(struct perf_evlist *evlist,
1534 			   struct perf_evsel *move_evsel)
1535 {
1536 	struct perf_evsel *evsel, *n;
1537 	LIST_HEAD(move);
1538 
1539 	if (move_evsel == perf_evlist__first(evlist))
1540 		return;
1541 
1542 	evlist__for_each_safe(evlist, n, evsel) {
1543 		if (evsel->leader == move_evsel->leader)
1544 			list_move_tail(&evsel->node, &move);
1545 	}
1546 
1547 	list_splice(&move, &evlist->entries);
1548 }
1549 
1550 void perf_evlist__set_tracking_event(struct perf_evlist *evlist,
1551 				     struct perf_evsel *tracking_evsel)
1552 {
1553 	struct perf_evsel *evsel;
1554 
1555 	if (tracking_evsel->tracking)
1556 		return;
1557 
1558 	evlist__for_each(evlist, evsel) {
1559 		if (evsel != tracking_evsel)
1560 			evsel->tracking = false;
1561 	}
1562 
1563 	tracking_evsel->tracking = true;
1564 }
1565