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