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