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