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