• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (C) 2011, Red Hat Inc, Arnaldo Carvalho de Melo <acme@redhat.com>
4  *
5  * Parts came from builtin-{top,stat,record}.c, see those files for further
6  * copyright notes.
7  */
8 #include <api/fs/fs.h>
9 #include <errno.h>
10 #include <inttypes.h>
11 #include <poll.h>
12 #include "cpumap.h"
13 #include "util/mmap.h"
14 #include "thread_map.h"
15 #include "target.h"
16 #include "evlist.h"
17 #include "evsel.h"
18 #include "debug.h"
19 #include "units.h"
20 #include <internal/lib.h> // page_size
21 #include "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 
511 #ifdef HAVE_EVENTFD_SUPPORT
evlist__add_wakeup_eventfd(struct evlist * evlist,int fd)512 int evlist__add_wakeup_eventfd(struct evlist *evlist, int fd)
513 {
514 	return perf_evlist__add_pollfd(&evlist->core, fd, NULL, POLLIN,
515 				       fdarray_flag__nonfilterable);
516 }
517 #endif
518 
evlist__poll(struct evlist * evlist,int timeout)519 int evlist__poll(struct evlist *evlist, int timeout)
520 {
521 	return perf_evlist__poll(&evlist->core, timeout);
522 }
523 
perf_evlist__id2sid(struct evlist * evlist,u64 id)524 struct perf_sample_id *perf_evlist__id2sid(struct evlist *evlist, u64 id)
525 {
526 	struct hlist_head *head;
527 	struct perf_sample_id *sid;
528 	int hash;
529 
530 	hash = hash_64(id, PERF_EVLIST__HLIST_BITS);
531 	head = &evlist->core.heads[hash];
532 
533 	hlist_for_each_entry(sid, head, node)
534 		if (sid->id == id)
535 			return sid;
536 
537 	return NULL;
538 }
539 
perf_evlist__id2evsel(struct evlist * evlist,u64 id)540 struct evsel *perf_evlist__id2evsel(struct evlist *evlist, u64 id)
541 {
542 	struct perf_sample_id *sid;
543 
544 	if (evlist->core.nr_entries == 1 || !id)
545 		return evlist__first(evlist);
546 
547 	sid = perf_evlist__id2sid(evlist, id);
548 	if (sid)
549 		return container_of(sid->evsel, struct evsel, core);
550 
551 	if (!evlist__sample_id_all(evlist))
552 		return evlist__first(evlist);
553 
554 	return NULL;
555 }
556 
perf_evlist__id2evsel_strict(struct evlist * evlist,u64 id)557 struct evsel *perf_evlist__id2evsel_strict(struct evlist *evlist,
558 						u64 id)
559 {
560 	struct perf_sample_id *sid;
561 
562 	if (!id)
563 		return NULL;
564 
565 	sid = perf_evlist__id2sid(evlist, id);
566 	if (sid)
567 		return container_of(sid->evsel, struct evsel, core);
568 
569 	return NULL;
570 }
571 
perf_evlist__event2id(struct evlist * evlist,union perf_event * event,u64 * id)572 static int perf_evlist__event2id(struct evlist *evlist,
573 				 union perf_event *event, u64 *id)
574 {
575 	const __u64 *array = event->sample.array;
576 	ssize_t n;
577 
578 	n = (event->header.size - sizeof(event->header)) >> 3;
579 
580 	if (event->header.type == PERF_RECORD_SAMPLE) {
581 		if (evlist->id_pos >= n)
582 			return -1;
583 		*id = array[evlist->id_pos];
584 	} else {
585 		if (evlist->is_pos > n)
586 			return -1;
587 		n -= evlist->is_pos;
588 		*id = array[n];
589 	}
590 	return 0;
591 }
592 
perf_evlist__event2evsel(struct evlist * evlist,union perf_event * event)593 struct evsel *perf_evlist__event2evsel(struct evlist *evlist,
594 					    union perf_event *event)
595 {
596 	struct evsel *first = evlist__first(evlist);
597 	struct hlist_head *head;
598 	struct perf_sample_id *sid;
599 	int hash;
600 	u64 id;
601 
602 	if (evlist->core.nr_entries == 1)
603 		return first;
604 
605 	if (!first->core.attr.sample_id_all &&
606 	    event->header.type != PERF_RECORD_SAMPLE)
607 		return first;
608 
609 	if (perf_evlist__event2id(evlist, event, &id))
610 		return NULL;
611 
612 	/* Synthesized events have an id of zero */
613 	if (!id)
614 		return first;
615 
616 	hash = hash_64(id, PERF_EVLIST__HLIST_BITS);
617 	head = &evlist->core.heads[hash];
618 
619 	hlist_for_each_entry(sid, head, node) {
620 		if (sid->id == id)
621 			return container_of(sid->evsel, struct evsel, core);
622 	}
623 	return NULL;
624 }
625 
perf_evlist__set_paused(struct evlist * evlist,bool value)626 static int perf_evlist__set_paused(struct evlist *evlist, bool value)
627 {
628 	int i;
629 
630 	if (!evlist->overwrite_mmap)
631 		return 0;
632 
633 	for (i = 0; i < evlist->core.nr_mmaps; i++) {
634 		int fd = evlist->overwrite_mmap[i].core.fd;
635 		int err;
636 
637 		if (fd < 0)
638 			continue;
639 		err = ioctl(fd, PERF_EVENT_IOC_PAUSE_OUTPUT, value ? 1 : 0);
640 		if (err)
641 			return err;
642 	}
643 	return 0;
644 }
645 
perf_evlist__pause(struct evlist * evlist)646 static int perf_evlist__pause(struct evlist *evlist)
647 {
648 	return perf_evlist__set_paused(evlist, true);
649 }
650 
perf_evlist__resume(struct evlist * evlist)651 static int perf_evlist__resume(struct evlist *evlist)
652 {
653 	return perf_evlist__set_paused(evlist, false);
654 }
655 
evlist__munmap_nofree(struct evlist * evlist)656 static void evlist__munmap_nofree(struct evlist *evlist)
657 {
658 	int i;
659 
660 	if (evlist->mmap)
661 		for (i = 0; i < evlist->core.nr_mmaps; i++)
662 			perf_mmap__munmap(&evlist->mmap[i].core);
663 
664 	if (evlist->overwrite_mmap)
665 		for (i = 0; i < evlist->core.nr_mmaps; i++)
666 			perf_mmap__munmap(&evlist->overwrite_mmap[i].core);
667 }
668 
evlist__munmap(struct evlist * evlist)669 void evlist__munmap(struct evlist *evlist)
670 {
671 	evlist__munmap_nofree(evlist);
672 	zfree(&evlist->mmap);
673 	zfree(&evlist->overwrite_mmap);
674 }
675 
perf_mmap__unmap_cb(struct perf_mmap * map)676 static void perf_mmap__unmap_cb(struct perf_mmap *map)
677 {
678 	struct mmap *m = container_of(map, struct mmap, core);
679 
680 	mmap__munmap(m);
681 }
682 
evlist__alloc_mmap(struct evlist * evlist,bool overwrite)683 static struct mmap *evlist__alloc_mmap(struct evlist *evlist,
684 				       bool overwrite)
685 {
686 	int i;
687 	struct mmap *map;
688 
689 	map = zalloc(evlist->core.nr_mmaps * sizeof(struct mmap));
690 	if (!map)
691 		return NULL;
692 
693 	for (i = 0; i < evlist->core.nr_mmaps; i++) {
694 		struct perf_mmap *prev = i ? &map[i - 1].core : NULL;
695 
696 		/*
697 		 * When the perf_mmap() call is made we grab one refcount, plus
698 		 * one extra to let perf_mmap__consume() get the last
699 		 * events after all real references (perf_mmap__get()) are
700 		 * dropped.
701 		 *
702 		 * Each PERF_EVENT_IOC_SET_OUTPUT points to this mmap and
703 		 * thus does perf_mmap__get() on it.
704 		 */
705 		perf_mmap__init(&map[i].core, prev, overwrite, perf_mmap__unmap_cb);
706 	}
707 
708 	return map;
709 }
710 
711 static void
perf_evlist__mmap_cb_idx(struct perf_evlist * _evlist,struct perf_mmap_param * _mp,int idx,bool per_cpu)712 perf_evlist__mmap_cb_idx(struct perf_evlist *_evlist,
713 			 struct perf_mmap_param *_mp,
714 			 int idx, bool per_cpu)
715 {
716 	struct evlist *evlist = container_of(_evlist, struct evlist, core);
717 	struct mmap_params *mp = container_of(_mp, struct mmap_params, core);
718 
719 	auxtrace_mmap_params__set_idx(&mp->auxtrace_mp, evlist, idx, per_cpu);
720 }
721 
722 static struct perf_mmap*
perf_evlist__mmap_cb_get(struct perf_evlist * _evlist,bool overwrite,int idx)723 perf_evlist__mmap_cb_get(struct perf_evlist *_evlist, bool overwrite, int idx)
724 {
725 	struct evlist *evlist = container_of(_evlist, struct evlist, core);
726 	struct mmap *maps;
727 
728 	maps = overwrite ? evlist->overwrite_mmap : evlist->mmap;
729 
730 	if (!maps) {
731 		maps = evlist__alloc_mmap(evlist, overwrite);
732 		if (!maps)
733 			return NULL;
734 
735 		if (overwrite) {
736 			evlist->overwrite_mmap = maps;
737 			if (evlist->bkw_mmap_state == BKW_MMAP_NOTREADY)
738 				perf_evlist__toggle_bkw_mmap(evlist, BKW_MMAP_RUNNING);
739 		} else {
740 			evlist->mmap = maps;
741 		}
742 	}
743 
744 	return &maps[idx].core;
745 }
746 
747 static int
perf_evlist__mmap_cb_mmap(struct perf_mmap * _map,struct perf_mmap_param * _mp,int output,int cpu)748 perf_evlist__mmap_cb_mmap(struct perf_mmap *_map, struct perf_mmap_param *_mp,
749 			  int output, int cpu)
750 {
751 	struct mmap *map = container_of(_map, struct mmap, core);
752 	struct mmap_params *mp = container_of(_mp, struct mmap_params, core);
753 
754 	return mmap__mmap(map, mp, output, cpu);
755 }
756 
perf_event_mlock_kb_in_pages(void)757 unsigned long perf_event_mlock_kb_in_pages(void)
758 {
759 	unsigned long pages;
760 	int max;
761 
762 	if (sysctl__read_int("kernel/perf_event_mlock_kb", &max) < 0) {
763 		/*
764 		 * Pick a once upon a time good value, i.e. things look
765 		 * strange since we can't read a sysctl value, but lets not
766 		 * die yet...
767 		 */
768 		max = 512;
769 	} else {
770 		max -= (page_size / 1024);
771 	}
772 
773 	pages = (max * 1024) / page_size;
774 	if (!is_power_of_2(pages))
775 		pages = rounddown_pow_of_two(pages);
776 
777 	return pages;
778 }
779 
evlist__mmap_size(unsigned long pages)780 size_t evlist__mmap_size(unsigned long pages)
781 {
782 	if (pages == UINT_MAX)
783 		pages = perf_event_mlock_kb_in_pages();
784 	else if (!is_power_of_2(pages))
785 		return 0;
786 
787 	return (pages + 1) * page_size;
788 }
789 
parse_pages_arg(const char * str,unsigned long min,unsigned long max)790 static long parse_pages_arg(const char *str, unsigned long min,
791 			    unsigned long max)
792 {
793 	unsigned long pages, val;
794 	static struct parse_tag tags[] = {
795 		{ .tag  = 'B', .mult = 1       },
796 		{ .tag  = 'K', .mult = 1 << 10 },
797 		{ .tag  = 'M', .mult = 1 << 20 },
798 		{ .tag  = 'G', .mult = 1 << 30 },
799 		{ .tag  = 0 },
800 	};
801 
802 	if (str == NULL)
803 		return -EINVAL;
804 
805 	val = parse_tag_value(str, tags);
806 	if (val != (unsigned long) -1) {
807 		/* we got file size value */
808 		pages = PERF_ALIGN(val, page_size) / page_size;
809 	} else {
810 		/* we got pages count value */
811 		char *eptr;
812 		pages = strtoul(str, &eptr, 10);
813 		if (*eptr != '\0')
814 			return -EINVAL;
815 	}
816 
817 	if (pages == 0 && min == 0) {
818 		/* leave number of pages at 0 */
819 	} else if (!is_power_of_2(pages)) {
820 		char buf[100];
821 
822 		/* round pages up to next power of 2 */
823 		pages = roundup_pow_of_two(pages);
824 		if (!pages)
825 			return -EINVAL;
826 
827 		unit_number__scnprintf(buf, sizeof(buf), pages * page_size);
828 		pr_info("rounding mmap pages size to %s (%lu pages)\n",
829 			buf, pages);
830 	}
831 
832 	if (pages > max)
833 		return -EINVAL;
834 
835 	return pages;
836 }
837 
__perf_evlist__parse_mmap_pages(unsigned int * mmap_pages,const char * str)838 int __perf_evlist__parse_mmap_pages(unsigned int *mmap_pages, const char *str)
839 {
840 	unsigned long max = UINT_MAX;
841 	long pages;
842 
843 	if (max > SIZE_MAX / page_size)
844 		max = SIZE_MAX / page_size;
845 
846 	pages = parse_pages_arg(str, 1, max);
847 	if (pages < 0) {
848 		pr_err("Invalid argument for --mmap_pages/-m\n");
849 		return -1;
850 	}
851 
852 	*mmap_pages = pages;
853 	return 0;
854 }
855 
perf_evlist__parse_mmap_pages(const struct option * opt,const char * str,int unset __maybe_unused)856 int perf_evlist__parse_mmap_pages(const struct option *opt, const char *str,
857 				  int unset __maybe_unused)
858 {
859 	return __perf_evlist__parse_mmap_pages(opt->value, str);
860 }
861 
862 /**
863  * evlist__mmap_ex - Create mmaps to receive events.
864  * @evlist: list of events
865  * @pages: map length in pages
866  * @overwrite: overwrite older events?
867  * @auxtrace_pages - auxtrace map length in pages
868  * @auxtrace_overwrite - overwrite older auxtrace data?
869  *
870  * If @overwrite is %false the user needs to signal event consumption using
871  * perf_mmap__write_tail().  Using evlist__mmap_read() does this
872  * automatically.
873  *
874  * Similarly, if @auxtrace_overwrite is %false the user needs to signal data
875  * consumption using auxtrace_mmap__write_tail().
876  *
877  * Return: %0 on success, negative error code otherwise.
878  */
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)879 int evlist__mmap_ex(struct evlist *evlist, unsigned int pages,
880 			 unsigned int auxtrace_pages,
881 			 bool auxtrace_overwrite, int nr_cblocks, int affinity, int flush,
882 			 int comp_level)
883 {
884 	/*
885 	 * Delay setting mp.prot: set it before calling perf_mmap__mmap.
886 	 * Its value is decided by evsel's write_backward.
887 	 * So &mp should not be passed through const pointer.
888 	 */
889 	struct mmap_params mp = {
890 		.nr_cblocks	= nr_cblocks,
891 		.affinity	= affinity,
892 		.flush		= flush,
893 		.comp_level	= comp_level
894 	};
895 	struct perf_evlist_mmap_ops ops = {
896 		.idx  = perf_evlist__mmap_cb_idx,
897 		.get  = perf_evlist__mmap_cb_get,
898 		.mmap = perf_evlist__mmap_cb_mmap,
899 	};
900 
901 	evlist->core.mmap_len = evlist__mmap_size(pages);
902 	pr_debug("mmap size %zuB\n", evlist->core.mmap_len);
903 
904 	auxtrace_mmap_params__init(&mp.auxtrace_mp, evlist->core.mmap_len,
905 				   auxtrace_pages, auxtrace_overwrite);
906 
907 	return perf_evlist__mmap_ops(&evlist->core, &ops, &mp.core);
908 }
909 
evlist__mmap(struct evlist * evlist,unsigned int pages)910 int evlist__mmap(struct evlist *evlist, unsigned int pages)
911 {
912 	return evlist__mmap_ex(evlist, pages, 0, false, 0, PERF_AFFINITY_SYS, 1, 0);
913 }
914 
perf_evlist__create_maps(struct evlist * evlist,struct target * target)915 int perf_evlist__create_maps(struct evlist *evlist, struct target *target)
916 {
917 	bool all_threads = (target->per_thread && target->system_wide);
918 	struct perf_cpu_map *cpus;
919 	struct perf_thread_map *threads;
920 
921 	/*
922 	 * If specify '-a' and '--per-thread' to perf record, perf record
923 	 * will override '--per-thread'. target->per_thread = false and
924 	 * target->system_wide = true.
925 	 *
926 	 * If specify '--per-thread' only to perf record,
927 	 * target->per_thread = true and target->system_wide = false.
928 	 *
929 	 * So target->per_thread && target->system_wide is false.
930 	 * For perf record, thread_map__new_str doesn't call
931 	 * thread_map__new_all_cpus. That will keep perf record's
932 	 * current behavior.
933 	 *
934 	 * For perf stat, it allows the case that target->per_thread and
935 	 * target->system_wide are all true. It means to collect system-wide
936 	 * per-thread data. thread_map__new_str will call
937 	 * thread_map__new_all_cpus to enumerate all threads.
938 	 */
939 	threads = thread_map__new_str(target->pid, target->tid, target->uid,
940 				      all_threads);
941 
942 	if (!threads)
943 		return -1;
944 
945 	if (target__uses_dummy_map(target))
946 		cpus = perf_cpu_map__dummy_new();
947 	else
948 		cpus = perf_cpu_map__new(target->cpu_list);
949 
950 	if (!cpus)
951 		goto out_delete_threads;
952 
953 	evlist->core.has_user_cpus = !!target->cpu_list;
954 
955 	perf_evlist__set_maps(&evlist->core, cpus, threads);
956 
957 	/* as evlist now has references, put count here */
958 	perf_cpu_map__put(cpus);
959 	perf_thread_map__put(threads);
960 
961 	return 0;
962 
963 out_delete_threads:
964 	perf_thread_map__put(threads);
965 	return -1;
966 }
967 
__perf_evlist__set_sample_bit(struct evlist * evlist,enum perf_event_sample_format bit)968 void __perf_evlist__set_sample_bit(struct evlist *evlist,
969 				   enum perf_event_sample_format bit)
970 {
971 	struct evsel *evsel;
972 
973 	evlist__for_each_entry(evlist, evsel)
974 		__evsel__set_sample_bit(evsel, bit);
975 }
976 
__perf_evlist__reset_sample_bit(struct evlist * evlist,enum perf_event_sample_format bit)977 void __perf_evlist__reset_sample_bit(struct evlist *evlist,
978 				     enum perf_event_sample_format bit)
979 {
980 	struct evsel *evsel;
981 
982 	evlist__for_each_entry(evlist, evsel)
983 		__evsel__reset_sample_bit(evsel, bit);
984 }
985 
perf_evlist__apply_filters(struct evlist * evlist,struct evsel ** err_evsel)986 int perf_evlist__apply_filters(struct evlist *evlist, struct evsel **err_evsel)
987 {
988 	struct evsel *evsel;
989 	int err = 0;
990 
991 	evlist__for_each_entry(evlist, evsel) {
992 		if (evsel->filter == NULL)
993 			continue;
994 
995 		/*
996 		 * filters only work for tracepoint event, which doesn't have cpu limit.
997 		 * So evlist and evsel should always be same.
998 		 */
999 		err = perf_evsel__apply_filter(&evsel->core, evsel->filter);
1000 		if (err) {
1001 			*err_evsel = evsel;
1002 			break;
1003 		}
1004 	}
1005 
1006 	return err;
1007 }
1008 
perf_evlist__set_tp_filter(struct evlist * evlist,const char * filter)1009 int perf_evlist__set_tp_filter(struct evlist *evlist, const char *filter)
1010 {
1011 	struct evsel *evsel;
1012 	int err = 0;
1013 
1014 	if (filter == NULL)
1015 		return -1;
1016 
1017 	evlist__for_each_entry(evlist, evsel) {
1018 		if (evsel->core.attr.type != PERF_TYPE_TRACEPOINT)
1019 			continue;
1020 
1021 		err = evsel__set_filter(evsel, filter);
1022 		if (err)
1023 			break;
1024 	}
1025 
1026 	return err;
1027 }
1028 
perf_evlist__append_tp_filter(struct evlist * evlist,const char * filter)1029 int perf_evlist__append_tp_filter(struct evlist *evlist, const char *filter)
1030 {
1031 	struct evsel *evsel;
1032 	int err = 0;
1033 
1034 	if (filter == NULL)
1035 		return -1;
1036 
1037 	evlist__for_each_entry(evlist, evsel) {
1038 		if (evsel->core.attr.type != PERF_TYPE_TRACEPOINT)
1039 			continue;
1040 
1041 		err = evsel__append_tp_filter(evsel, filter);
1042 		if (err)
1043 			break;
1044 	}
1045 
1046 	return err;
1047 }
1048 
asprintf__tp_filter_pids(size_t npids,pid_t * pids)1049 char *asprintf__tp_filter_pids(size_t npids, pid_t *pids)
1050 {
1051 	char *filter;
1052 	size_t i;
1053 
1054 	for (i = 0; i < npids; ++i) {
1055 		if (i == 0) {
1056 			if (asprintf(&filter, "common_pid != %d", pids[i]) < 0)
1057 				return NULL;
1058 		} else {
1059 			char *tmp;
1060 
1061 			if (asprintf(&tmp, "%s && common_pid != %d", filter, pids[i]) < 0)
1062 				goto out_free;
1063 
1064 			free(filter);
1065 			filter = tmp;
1066 		}
1067 	}
1068 
1069 	return filter;
1070 out_free:
1071 	free(filter);
1072 	return NULL;
1073 }
1074 
perf_evlist__set_tp_filter_pids(struct evlist * evlist,size_t npids,pid_t * pids)1075 int perf_evlist__set_tp_filter_pids(struct evlist *evlist, size_t npids, pid_t *pids)
1076 {
1077 	char *filter = asprintf__tp_filter_pids(npids, pids);
1078 	int ret = perf_evlist__set_tp_filter(evlist, filter);
1079 
1080 	free(filter);
1081 	return ret;
1082 }
1083 
perf_evlist__set_tp_filter_pid(struct evlist * evlist,pid_t pid)1084 int perf_evlist__set_tp_filter_pid(struct evlist *evlist, pid_t pid)
1085 {
1086 	return perf_evlist__set_tp_filter_pids(evlist, 1, &pid);
1087 }
1088 
perf_evlist__append_tp_filter_pids(struct evlist * evlist,size_t npids,pid_t * pids)1089 int perf_evlist__append_tp_filter_pids(struct evlist *evlist, size_t npids, pid_t *pids)
1090 {
1091 	char *filter = asprintf__tp_filter_pids(npids, pids);
1092 	int ret = perf_evlist__append_tp_filter(evlist, filter);
1093 
1094 	free(filter);
1095 	return ret;
1096 }
1097 
perf_evlist__append_tp_filter_pid(struct evlist * evlist,pid_t pid)1098 int perf_evlist__append_tp_filter_pid(struct evlist *evlist, pid_t pid)
1099 {
1100 	return perf_evlist__append_tp_filter_pids(evlist, 1, &pid);
1101 }
1102 
evlist__valid_sample_type(struct evlist * evlist)1103 bool evlist__valid_sample_type(struct evlist *evlist)
1104 {
1105 	struct evsel *pos;
1106 
1107 	if (evlist->core.nr_entries == 1)
1108 		return true;
1109 
1110 	if (evlist->id_pos < 0 || evlist->is_pos < 0)
1111 		return false;
1112 
1113 	evlist__for_each_entry(evlist, pos) {
1114 		if (pos->id_pos != evlist->id_pos ||
1115 		    pos->is_pos != evlist->is_pos)
1116 			return false;
1117 	}
1118 
1119 	return true;
1120 }
1121 
__evlist__combined_sample_type(struct evlist * evlist)1122 u64 __evlist__combined_sample_type(struct evlist *evlist)
1123 {
1124 	struct evsel *evsel;
1125 
1126 	if (evlist->combined_sample_type)
1127 		return evlist->combined_sample_type;
1128 
1129 	evlist__for_each_entry(evlist, evsel)
1130 		evlist->combined_sample_type |= evsel->core.attr.sample_type;
1131 
1132 	return evlist->combined_sample_type;
1133 }
1134 
evlist__combined_sample_type(struct evlist * evlist)1135 u64 evlist__combined_sample_type(struct evlist *evlist)
1136 {
1137 	evlist->combined_sample_type = 0;
1138 	return __evlist__combined_sample_type(evlist);
1139 }
1140 
evlist__combined_branch_type(struct evlist * evlist)1141 u64 evlist__combined_branch_type(struct evlist *evlist)
1142 {
1143 	struct evsel *evsel;
1144 	u64 branch_type = 0;
1145 
1146 	evlist__for_each_entry(evlist, evsel)
1147 		branch_type |= evsel->core.attr.branch_sample_type;
1148 	return branch_type;
1149 }
1150 
perf_evlist__valid_read_format(struct evlist * evlist)1151 bool perf_evlist__valid_read_format(struct evlist *evlist)
1152 {
1153 	struct evsel *first = evlist__first(evlist), *pos = first;
1154 	u64 read_format = first->core.attr.read_format;
1155 	u64 sample_type = first->core.attr.sample_type;
1156 
1157 	evlist__for_each_entry(evlist, pos) {
1158 		if (read_format != pos->core.attr.read_format) {
1159 			pr_debug("Read format differs %#" PRIx64 " vs %#" PRIx64 "\n",
1160 				 read_format, (u64)pos->core.attr.read_format);
1161 		}
1162 	}
1163 
1164 	/* PERF_SAMPLE_READ imples PERF_FORMAT_ID. */
1165 	if ((sample_type & PERF_SAMPLE_READ) &&
1166 	    !(read_format & PERF_FORMAT_ID)) {
1167 		return false;
1168 	}
1169 
1170 	return true;
1171 }
1172 
perf_evlist__id_hdr_size(struct evlist * evlist)1173 u16 perf_evlist__id_hdr_size(struct evlist *evlist)
1174 {
1175 	struct evsel *first = evlist__first(evlist);
1176 	struct perf_sample *data;
1177 	u64 sample_type;
1178 	u16 size = 0;
1179 
1180 	if (!first->core.attr.sample_id_all)
1181 		goto out;
1182 
1183 	sample_type = first->core.attr.sample_type;
1184 
1185 	if (sample_type & PERF_SAMPLE_TID)
1186 		size += sizeof(data->tid) * 2;
1187 
1188        if (sample_type & PERF_SAMPLE_TIME)
1189 		size += sizeof(data->time);
1190 
1191 	if (sample_type & PERF_SAMPLE_ID)
1192 		size += sizeof(data->id);
1193 
1194 	if (sample_type & PERF_SAMPLE_STREAM_ID)
1195 		size += sizeof(data->stream_id);
1196 
1197 	if (sample_type & PERF_SAMPLE_CPU)
1198 		size += sizeof(data->cpu) * 2;
1199 
1200 	if (sample_type & PERF_SAMPLE_IDENTIFIER)
1201 		size += sizeof(data->id);
1202 out:
1203 	return size;
1204 }
1205 
evlist__valid_sample_id_all(struct evlist * evlist)1206 bool evlist__valid_sample_id_all(struct evlist *evlist)
1207 {
1208 	struct evsel *first = evlist__first(evlist), *pos = first;
1209 
1210 	evlist__for_each_entry_continue(evlist, pos) {
1211 		if (first->core.attr.sample_id_all != pos->core.attr.sample_id_all)
1212 			return false;
1213 	}
1214 
1215 	return true;
1216 }
1217 
evlist__sample_id_all(struct evlist * evlist)1218 bool evlist__sample_id_all(struct evlist *evlist)
1219 {
1220 	struct evsel *first = evlist__first(evlist);
1221 	return first->core.attr.sample_id_all;
1222 }
1223 
perf_evlist__set_selected(struct evlist * evlist,struct evsel * evsel)1224 void perf_evlist__set_selected(struct evlist *evlist,
1225 			       struct evsel *evsel)
1226 {
1227 	evlist->selected = evsel;
1228 }
1229 
evlist__close(struct evlist * evlist)1230 void evlist__close(struct evlist *evlist)
1231 {
1232 	struct evsel *evsel;
1233 	struct affinity affinity;
1234 	int cpu, i;
1235 
1236 	/*
1237 	 * With perf record core.cpus is usually NULL.
1238 	 * Use the old method to handle this for now.
1239 	 */
1240 	if (!evlist->core.cpus) {
1241 		evlist__for_each_entry_reverse(evlist, evsel)
1242 			evsel__close(evsel);
1243 		return;
1244 	}
1245 
1246 	if (affinity__setup(&affinity) < 0)
1247 		return;
1248 	evlist__for_each_cpu(evlist, i, cpu) {
1249 		affinity__set(&affinity, cpu);
1250 
1251 		evlist__for_each_entry_reverse(evlist, evsel) {
1252 			if (evsel__cpu_iter_skip(evsel, cpu))
1253 			    continue;
1254 			perf_evsel__close_cpu(&evsel->core, evsel->cpu_iter - 1);
1255 		}
1256 	}
1257 	affinity__cleanup(&affinity);
1258 	evlist__for_each_entry_reverse(evlist, evsel) {
1259 		perf_evsel__free_fd(&evsel->core);
1260 		perf_evsel__free_id(&evsel->core);
1261 	}
1262 }
1263 
perf_evlist__create_syswide_maps(struct evlist * evlist)1264 static int perf_evlist__create_syswide_maps(struct evlist *evlist)
1265 {
1266 	struct perf_cpu_map *cpus;
1267 	struct perf_thread_map *threads;
1268 	int err = -ENOMEM;
1269 
1270 	/*
1271 	 * Try reading /sys/devices/system/cpu/online to get
1272 	 * an all cpus map.
1273 	 *
1274 	 * FIXME: -ENOMEM is the best we can do here, the cpu_map
1275 	 * code needs an overhaul to properly forward the
1276 	 * error, and we may not want to do that fallback to a
1277 	 * default cpu identity map :-\
1278 	 */
1279 	cpus = perf_cpu_map__new(NULL);
1280 	if (!cpus)
1281 		goto out;
1282 
1283 	threads = perf_thread_map__new_dummy();
1284 	if (!threads)
1285 		goto out_put;
1286 
1287 	perf_evlist__set_maps(&evlist->core, cpus, threads);
1288 
1289 	perf_thread_map__put(threads);
1290 out_put:
1291 	perf_cpu_map__put(cpus);
1292 out:
1293 	return err;
1294 }
1295 
evlist__open(struct evlist * evlist)1296 int evlist__open(struct evlist *evlist)
1297 {
1298 	struct evsel *evsel;
1299 	int err;
1300 
1301 	/*
1302 	 * Default: one fd per CPU, all threads, aka systemwide
1303 	 * as sys_perf_event_open(cpu = -1, thread = -1) is EINVAL
1304 	 */
1305 	if (evlist->core.threads == NULL && evlist->core.cpus == NULL) {
1306 		err = perf_evlist__create_syswide_maps(evlist);
1307 		if (err < 0)
1308 			goto out_err;
1309 	}
1310 
1311 	perf_evlist__update_id_pos(evlist);
1312 
1313 	evlist__for_each_entry(evlist, evsel) {
1314 		err = evsel__open(evsel, evsel->core.cpus, evsel->core.threads);
1315 		if (err < 0)
1316 			goto out_err;
1317 	}
1318 
1319 	return 0;
1320 out_err:
1321 	evlist__close(evlist);
1322 	errno = -err;
1323 	return err;
1324 }
1325 
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))1326 int perf_evlist__prepare_workload(struct evlist *evlist, struct target *target,
1327 				  const char *argv[], bool pipe_output,
1328 				  void (*exec_error)(int signo, siginfo_t *info, void *ucontext))
1329 {
1330 	int child_ready_pipe[2], go_pipe[2];
1331 	char bf;
1332 
1333 	if (pipe(child_ready_pipe) < 0) {
1334 		perror("failed to create 'ready' pipe");
1335 		return -1;
1336 	}
1337 
1338 	if (pipe(go_pipe) < 0) {
1339 		perror("failed to create 'go' pipe");
1340 		goto out_close_ready_pipe;
1341 	}
1342 
1343 	evlist->workload.pid = fork();
1344 	if (evlist->workload.pid < 0) {
1345 		perror("failed to fork");
1346 		goto out_close_pipes;
1347 	}
1348 
1349 	if (!evlist->workload.pid) {
1350 		int ret;
1351 
1352 		if (pipe_output)
1353 			dup2(2, 1);
1354 
1355 		signal(SIGTERM, SIG_DFL);
1356 
1357 		close(child_ready_pipe[0]);
1358 		close(go_pipe[1]);
1359 		fcntl(go_pipe[0], F_SETFD, FD_CLOEXEC);
1360 
1361 		/*
1362 		 * Tell the parent we're ready to go
1363 		 */
1364 		close(child_ready_pipe[1]);
1365 
1366 		/*
1367 		 * Wait until the parent tells us to go.
1368 		 */
1369 		ret = read(go_pipe[0], &bf, 1);
1370 		/*
1371 		 * The parent will ask for the execvp() to be performed by
1372 		 * writing exactly one byte, in workload.cork_fd, usually via
1373 		 * perf_evlist__start_workload().
1374 		 *
1375 		 * For cancelling the workload without actually running it,
1376 		 * the parent will just close workload.cork_fd, without writing
1377 		 * anything, i.e. read will return zero and we just exit()
1378 		 * here.
1379 		 */
1380 		if (ret != 1) {
1381 			if (ret == -1)
1382 				perror("unable to read pipe");
1383 			exit(ret);
1384 		}
1385 
1386 		execvp(argv[0], (char **)argv);
1387 
1388 		if (exec_error) {
1389 			union sigval val;
1390 
1391 			val.sival_int = errno;
1392 			if (sigqueue(getppid(), SIGUSR1, val))
1393 				perror(argv[0]);
1394 		} else
1395 			perror(argv[0]);
1396 		exit(-1);
1397 	}
1398 
1399 	if (exec_error) {
1400 		struct sigaction act = {
1401 			.sa_flags     = SA_SIGINFO,
1402 			.sa_sigaction = exec_error,
1403 		};
1404 		sigaction(SIGUSR1, &act, NULL);
1405 	}
1406 
1407 	if (target__none(target)) {
1408 		if (evlist->core.threads == NULL) {
1409 			fprintf(stderr, "FATAL: evlist->threads need to be set at this point (%s:%d).\n",
1410 				__func__, __LINE__);
1411 			goto out_close_pipes;
1412 		}
1413 		perf_thread_map__set_pid(evlist->core.threads, 0, evlist->workload.pid);
1414 	}
1415 
1416 	close(child_ready_pipe[1]);
1417 	close(go_pipe[0]);
1418 	/*
1419 	 * wait for child to settle
1420 	 */
1421 	if (read(child_ready_pipe[0], &bf, 1) == -1) {
1422 		perror("unable to read pipe");
1423 		goto out_close_pipes;
1424 	}
1425 
1426 	fcntl(go_pipe[1], F_SETFD, FD_CLOEXEC);
1427 	evlist->workload.cork_fd = go_pipe[1];
1428 	close(child_ready_pipe[0]);
1429 	return 0;
1430 
1431 out_close_pipes:
1432 	close(go_pipe[0]);
1433 	close(go_pipe[1]);
1434 out_close_ready_pipe:
1435 	close(child_ready_pipe[0]);
1436 	close(child_ready_pipe[1]);
1437 	return -1;
1438 }
1439 
perf_evlist__start_workload(struct evlist * evlist)1440 int perf_evlist__start_workload(struct evlist *evlist)
1441 {
1442 	if (evlist->workload.cork_fd > 0) {
1443 		char bf = 0;
1444 		int ret;
1445 		/*
1446 		 * Remove the cork, let it rip!
1447 		 */
1448 		ret = write(evlist->workload.cork_fd, &bf, 1);
1449 		if (ret < 0)
1450 			perror("unable to write to pipe");
1451 
1452 		close(evlist->workload.cork_fd);
1453 		return ret;
1454 	}
1455 
1456 	return 0;
1457 }
1458 
perf_evlist__parse_sample(struct evlist * evlist,union perf_event * event,struct perf_sample * sample)1459 int perf_evlist__parse_sample(struct evlist *evlist, union perf_event *event,
1460 			      struct perf_sample *sample)
1461 {
1462 	struct evsel *evsel = perf_evlist__event2evsel(evlist, event);
1463 
1464 	if (!evsel)
1465 		return -EFAULT;
1466 	return evsel__parse_sample(evsel, event, sample);
1467 }
1468 
perf_evlist__parse_sample_timestamp(struct evlist * evlist,union perf_event * event,u64 * timestamp)1469 int perf_evlist__parse_sample_timestamp(struct evlist *evlist,
1470 					union perf_event *event,
1471 					u64 *timestamp)
1472 {
1473 	struct evsel *evsel = perf_evlist__event2evsel(evlist, event);
1474 
1475 	if (!evsel)
1476 		return -EFAULT;
1477 	return evsel__parse_sample_timestamp(evsel, event, timestamp);
1478 }
1479 
evlist__strerror_open(struct evlist * evlist,int err,char * buf,size_t size)1480 int evlist__strerror_open(struct evlist *evlist, int err, char *buf, size_t size)
1481 {
1482 	int printed, value;
1483 	char sbuf[STRERR_BUFSIZE], *emsg = str_error_r(err, sbuf, sizeof(sbuf));
1484 
1485 	switch (err) {
1486 	case EACCES:
1487 	case EPERM:
1488 		printed = scnprintf(buf, size,
1489 				    "Error:\t%s.\n"
1490 				    "Hint:\tCheck /proc/sys/kernel/perf_event_paranoid setting.", emsg);
1491 
1492 		value = perf_event_paranoid();
1493 
1494 		printed += scnprintf(buf + printed, size - printed, "\nHint:\t");
1495 
1496 		if (value >= 2) {
1497 			printed += scnprintf(buf + printed, size - printed,
1498 					     "For your workloads it needs to be <= 1\nHint:\t");
1499 		}
1500 		printed += scnprintf(buf + printed, size - printed,
1501 				     "For system wide tracing it needs to be set to -1.\n");
1502 
1503 		printed += scnprintf(buf + printed, size - printed,
1504 				    "Hint:\tTry: 'sudo sh -c \"echo -1 > /proc/sys/kernel/perf_event_paranoid\"'\n"
1505 				    "Hint:\tThe current value is %d.", value);
1506 		break;
1507 	case EINVAL: {
1508 		struct evsel *first = evlist__first(evlist);
1509 		int max_freq;
1510 
1511 		if (sysctl__read_int("kernel/perf_event_max_sample_rate", &max_freq) < 0)
1512 			goto out_default;
1513 
1514 		if (first->core.attr.sample_freq < (u64)max_freq)
1515 			goto out_default;
1516 
1517 		printed = scnprintf(buf, size,
1518 				    "Error:\t%s.\n"
1519 				    "Hint:\tCheck /proc/sys/kernel/perf_event_max_sample_rate.\n"
1520 				    "Hint:\tThe current value is %d and %" PRIu64 " is being requested.",
1521 				    emsg, max_freq, first->core.attr.sample_freq);
1522 		break;
1523 	}
1524 	default:
1525 out_default:
1526 		scnprintf(buf, size, "%s", emsg);
1527 		break;
1528 	}
1529 
1530 	return 0;
1531 }
1532 
evlist__strerror_mmap(struct evlist * evlist,int err,char * buf,size_t size)1533 int evlist__strerror_mmap(struct evlist *evlist, int err, char *buf, size_t size)
1534 {
1535 	char sbuf[STRERR_BUFSIZE], *emsg = str_error_r(err, sbuf, sizeof(sbuf));
1536 	int pages_attempted = evlist->core.mmap_len / 1024, pages_max_per_user, printed = 0;
1537 
1538 	switch (err) {
1539 	case EPERM:
1540 		sysctl__read_int("kernel/perf_event_mlock_kb", &pages_max_per_user);
1541 		printed += scnprintf(buf + printed, size - printed,
1542 				     "Error:\t%s.\n"
1543 				     "Hint:\tCheck /proc/sys/kernel/perf_event_mlock_kb (%d kB) setting.\n"
1544 				     "Hint:\tTried using %zd kB.\n",
1545 				     emsg, pages_max_per_user, pages_attempted);
1546 
1547 		if (pages_attempted >= pages_max_per_user) {
1548 			printed += scnprintf(buf + printed, size - printed,
1549 					     "Hint:\tTry 'sudo sh -c \"echo %d > /proc/sys/kernel/perf_event_mlock_kb\"', or\n",
1550 					     pages_max_per_user + pages_attempted);
1551 		}
1552 
1553 		printed += scnprintf(buf + printed, size - printed,
1554 				     "Hint:\tTry using a smaller -m/--mmap-pages value.");
1555 		break;
1556 	default:
1557 		scnprintf(buf, size, "%s", emsg);
1558 		break;
1559 	}
1560 
1561 	return 0;
1562 }
1563 
perf_evlist__to_front(struct evlist * evlist,struct evsel * move_evsel)1564 void perf_evlist__to_front(struct evlist *evlist,
1565 			   struct evsel *move_evsel)
1566 {
1567 	struct evsel *evsel, *n;
1568 	LIST_HEAD(move);
1569 
1570 	if (move_evsel == evlist__first(evlist))
1571 		return;
1572 
1573 	evlist__for_each_entry_safe(evlist, n, evsel) {
1574 		if (evsel->leader == move_evsel->leader)
1575 			list_move_tail(&evsel->core.node, &move);
1576 	}
1577 
1578 	list_splice(&move, &evlist->core.entries);
1579 }
1580 
perf_evlist__get_tracking_event(struct evlist * evlist)1581 struct evsel *perf_evlist__get_tracking_event(struct evlist *evlist)
1582 {
1583 	struct evsel *evsel;
1584 
1585 	evlist__for_each_entry(evlist, evsel) {
1586 		if (evsel->tracking)
1587 			return evsel;
1588 	}
1589 
1590 	return evlist__first(evlist);
1591 }
1592 
perf_evlist__set_tracking_event(struct evlist * evlist,struct evsel * tracking_evsel)1593 void perf_evlist__set_tracking_event(struct evlist *evlist,
1594 				     struct evsel *tracking_evsel)
1595 {
1596 	struct evsel *evsel;
1597 
1598 	if (tracking_evsel->tracking)
1599 		return;
1600 
1601 	evlist__for_each_entry(evlist, evsel) {
1602 		if (evsel != tracking_evsel)
1603 			evsel->tracking = false;
1604 	}
1605 
1606 	tracking_evsel->tracking = true;
1607 }
1608 
1609 struct evsel *
perf_evlist__find_evsel_by_str(struct evlist * evlist,const char * str)1610 perf_evlist__find_evsel_by_str(struct evlist *evlist,
1611 			       const char *str)
1612 {
1613 	struct evsel *evsel;
1614 
1615 	evlist__for_each_entry(evlist, evsel) {
1616 		if (!evsel->name)
1617 			continue;
1618 		if (strcmp(str, evsel->name) == 0)
1619 			return evsel;
1620 	}
1621 
1622 	return NULL;
1623 }
1624 
perf_evlist__toggle_bkw_mmap(struct evlist * evlist,enum bkw_mmap_state state)1625 void perf_evlist__toggle_bkw_mmap(struct evlist *evlist,
1626 				  enum bkw_mmap_state state)
1627 {
1628 	enum bkw_mmap_state old_state = evlist->bkw_mmap_state;
1629 	enum action {
1630 		NONE,
1631 		PAUSE,
1632 		RESUME,
1633 	} action = NONE;
1634 
1635 	if (!evlist->overwrite_mmap)
1636 		return;
1637 
1638 	switch (old_state) {
1639 	case BKW_MMAP_NOTREADY: {
1640 		if (state != BKW_MMAP_RUNNING)
1641 			goto state_err;
1642 		break;
1643 	}
1644 	case BKW_MMAP_RUNNING: {
1645 		if (state != BKW_MMAP_DATA_PENDING)
1646 			goto state_err;
1647 		action = PAUSE;
1648 		break;
1649 	}
1650 	case BKW_MMAP_DATA_PENDING: {
1651 		if (state != BKW_MMAP_EMPTY)
1652 			goto state_err;
1653 		break;
1654 	}
1655 	case BKW_MMAP_EMPTY: {
1656 		if (state != BKW_MMAP_RUNNING)
1657 			goto state_err;
1658 		action = RESUME;
1659 		break;
1660 	}
1661 	default:
1662 		WARN_ONCE(1, "Shouldn't get there\n");
1663 	}
1664 
1665 	evlist->bkw_mmap_state = state;
1666 
1667 	switch (action) {
1668 	case PAUSE:
1669 		perf_evlist__pause(evlist);
1670 		break;
1671 	case RESUME:
1672 		perf_evlist__resume(evlist);
1673 		break;
1674 	case NONE:
1675 	default:
1676 		break;
1677 	}
1678 
1679 state_err:
1680 	return;
1681 }
1682 
perf_evlist__exclude_kernel(struct evlist * evlist)1683 bool perf_evlist__exclude_kernel(struct evlist *evlist)
1684 {
1685 	struct evsel *evsel;
1686 
1687 	evlist__for_each_entry(evlist, evsel) {
1688 		if (!evsel->core.attr.exclude_kernel)
1689 			return false;
1690 	}
1691 
1692 	return true;
1693 }
1694 
1695 /*
1696  * Events in data file are not collect in groups, but we still want
1697  * the group display. Set the artificial group and set the leader's
1698  * forced_leader flag to notify the display code.
1699  */
perf_evlist__force_leader(struct evlist * evlist)1700 void perf_evlist__force_leader(struct evlist *evlist)
1701 {
1702 	if (!evlist->nr_groups) {
1703 		struct evsel *leader = evlist__first(evlist);
1704 
1705 		perf_evlist__set_leader(evlist);
1706 		leader->forced_leader = true;
1707 	}
1708 }
1709 
perf_evlist__reset_weak_group(struct evlist * evsel_list,struct evsel * evsel,bool close)1710 struct evsel *perf_evlist__reset_weak_group(struct evlist *evsel_list,
1711 						 struct evsel *evsel,
1712 						bool close)
1713 {
1714 	struct evsel *c2, *leader;
1715 	bool is_open = true;
1716 
1717 	leader = evsel->leader;
1718 	pr_debug("Weak group for %s/%d failed\n",
1719 			leader->name, leader->core.nr_members);
1720 
1721 	/*
1722 	 * for_each_group_member doesn't work here because it doesn't
1723 	 * include the first entry.
1724 	 */
1725 	evlist__for_each_entry(evsel_list, c2) {
1726 		if (c2 == evsel)
1727 			is_open = false;
1728 		if (c2->leader == leader) {
1729 			if (is_open && close)
1730 				perf_evsel__close(&c2->core);
1731 			c2->leader = c2;
1732 			c2->core.nr_members = 0;
1733 			/*
1734 			 * Set this for all former members of the group
1735 			 * to indicate they get reopened.
1736 			 */
1737 			c2->reset_group = true;
1738 		}
1739 	}
1740 	return leader;
1741 }
1742 
evlist__parse_control_fifo(const char * str,int * ctl_fd,int * ctl_fd_ack,bool * ctl_fd_close)1743 static int evlist__parse_control_fifo(const char *str, int *ctl_fd, int *ctl_fd_ack, bool *ctl_fd_close)
1744 {
1745 	char *s, *p;
1746 	int ret = 0, fd;
1747 
1748 	if (strncmp(str, "fifo:", 5))
1749 		return -EINVAL;
1750 
1751 	str += 5;
1752 	if (!*str || *str == ',')
1753 		return -EINVAL;
1754 
1755 	s = strdup(str);
1756 	if (!s)
1757 		return -ENOMEM;
1758 
1759 	p = strchr(s, ',');
1760 	if (p)
1761 		*p = '\0';
1762 
1763 	/*
1764 	 * O_RDWR avoids POLLHUPs which is necessary to allow the other
1765 	 * end of a FIFO to be repeatedly opened and closed.
1766 	 */
1767 	fd = open(s, O_RDWR | O_NONBLOCK | O_CLOEXEC);
1768 	if (fd < 0) {
1769 		pr_err("Failed to open '%s'\n", s);
1770 		ret = -errno;
1771 		goto out_free;
1772 	}
1773 	*ctl_fd = fd;
1774 	*ctl_fd_close = true;
1775 
1776 	if (p && *++p) {
1777 		/* O_RDWR | O_NONBLOCK means the other end need not be open */
1778 		fd = open(p, O_RDWR | O_NONBLOCK | O_CLOEXEC);
1779 		if (fd < 0) {
1780 			pr_err("Failed to open '%s'\n", p);
1781 			ret = -errno;
1782 			goto out_free;
1783 		}
1784 		*ctl_fd_ack = fd;
1785 	}
1786 
1787 out_free:
1788 	free(s);
1789 	return ret;
1790 }
1791 
evlist__parse_control(const char * str,int * ctl_fd,int * ctl_fd_ack,bool * ctl_fd_close)1792 int evlist__parse_control(const char *str, int *ctl_fd, int *ctl_fd_ack, bool *ctl_fd_close)
1793 {
1794 	char *comma = NULL, *endptr = NULL;
1795 
1796 	*ctl_fd_close = false;
1797 
1798 	if (strncmp(str, "fd:", 3))
1799 		return evlist__parse_control_fifo(str, ctl_fd, ctl_fd_ack, ctl_fd_close);
1800 
1801 	*ctl_fd = strtoul(&str[3], &endptr, 0);
1802 	if (endptr == &str[3])
1803 		return -EINVAL;
1804 
1805 	comma = strchr(str, ',');
1806 	if (comma) {
1807 		if (endptr != comma)
1808 			return -EINVAL;
1809 
1810 		*ctl_fd_ack = strtoul(comma + 1, &endptr, 0);
1811 		if (endptr == comma + 1 || *endptr != '\0')
1812 			return -EINVAL;
1813 	}
1814 
1815 	return 0;
1816 }
1817 
evlist__close_control(int ctl_fd,int ctl_fd_ack,bool * ctl_fd_close)1818 void evlist__close_control(int ctl_fd, int ctl_fd_ack, bool *ctl_fd_close)
1819 {
1820 	if (*ctl_fd_close) {
1821 		*ctl_fd_close = false;
1822 		close(ctl_fd);
1823 		if (ctl_fd_ack >= 0)
1824 			close(ctl_fd_ack);
1825 	}
1826 }
1827 
evlist__initialize_ctlfd(struct evlist * evlist,int fd,int ack)1828 int evlist__initialize_ctlfd(struct evlist *evlist, int fd, int ack)
1829 {
1830 	if (fd == -1) {
1831 		pr_debug("Control descriptor is not initialized\n");
1832 		return 0;
1833 	}
1834 
1835 	evlist->ctl_fd.pos = perf_evlist__add_pollfd(&evlist->core, fd, NULL, POLLIN,
1836 						     fdarray_flag__nonfilterable);
1837 	if (evlist->ctl_fd.pos < 0) {
1838 		evlist->ctl_fd.pos = -1;
1839 		pr_err("Failed to add ctl fd entry: %m\n");
1840 		return -1;
1841 	}
1842 
1843 	evlist->ctl_fd.fd = fd;
1844 	evlist->ctl_fd.ack = ack;
1845 
1846 	return 0;
1847 }
1848 
evlist__ctlfd_initialized(struct evlist * evlist)1849 bool evlist__ctlfd_initialized(struct evlist *evlist)
1850 {
1851 	return evlist->ctl_fd.pos >= 0;
1852 }
1853 
evlist__finalize_ctlfd(struct evlist * evlist)1854 int evlist__finalize_ctlfd(struct evlist *evlist)
1855 {
1856 	struct pollfd *entries = evlist->core.pollfd.entries;
1857 
1858 	if (!evlist__ctlfd_initialized(evlist))
1859 		return 0;
1860 
1861 	entries[evlist->ctl_fd.pos].fd = -1;
1862 	entries[evlist->ctl_fd.pos].events = 0;
1863 	entries[evlist->ctl_fd.pos].revents = 0;
1864 
1865 	evlist->ctl_fd.pos = -1;
1866 	evlist->ctl_fd.ack = -1;
1867 	evlist->ctl_fd.fd = -1;
1868 
1869 	return 0;
1870 }
1871 
evlist__ctlfd_recv(struct evlist * evlist,enum evlist_ctl_cmd * cmd,char * cmd_data,size_t data_size)1872 static int evlist__ctlfd_recv(struct evlist *evlist, enum evlist_ctl_cmd *cmd,
1873 			      char *cmd_data, size_t data_size)
1874 {
1875 	int err;
1876 	char c;
1877 	size_t bytes_read = 0;
1878 
1879 	*cmd = EVLIST_CTL_CMD_UNSUPPORTED;
1880 	memset(cmd_data, 0, data_size);
1881 	data_size--;
1882 
1883 	do {
1884 		err = read(evlist->ctl_fd.fd, &c, 1);
1885 		if (err > 0) {
1886 			if (c == '\n' || c == '\0')
1887 				break;
1888 			cmd_data[bytes_read++] = c;
1889 			if (bytes_read == data_size)
1890 				break;
1891 			continue;
1892 		} else if (err == -1) {
1893 			if (errno == EINTR)
1894 				continue;
1895 			if (errno == EAGAIN || errno == EWOULDBLOCK)
1896 				err = 0;
1897 			else
1898 				pr_err("Failed to read from ctlfd %d: %m\n", evlist->ctl_fd.fd);
1899 		}
1900 		break;
1901 	} while (1);
1902 
1903 	pr_debug("Message from ctl_fd: \"%s%s\"\n", cmd_data,
1904 		 bytes_read == data_size ? "" : c == '\n' ? "\\n" : "\\0");
1905 
1906 	if (bytes_read > 0) {
1907 		if (!strncmp(cmd_data, EVLIST_CTL_CMD_ENABLE_TAG,
1908 			     (sizeof(EVLIST_CTL_CMD_ENABLE_TAG)-1))) {
1909 			*cmd = EVLIST_CTL_CMD_ENABLE;
1910 		} else if (!strncmp(cmd_data, EVLIST_CTL_CMD_DISABLE_TAG,
1911 				    (sizeof(EVLIST_CTL_CMD_DISABLE_TAG)-1))) {
1912 			*cmd = EVLIST_CTL_CMD_DISABLE;
1913 		} else if (!strncmp(cmd_data, EVLIST_CTL_CMD_SNAPSHOT_TAG,
1914 				    (sizeof(EVLIST_CTL_CMD_SNAPSHOT_TAG)-1))) {
1915 			*cmd = EVLIST_CTL_CMD_SNAPSHOT;
1916 			pr_debug("is snapshot\n");
1917 		}
1918 	}
1919 
1920 	return bytes_read ? (int)bytes_read : err;
1921 }
1922 
evlist__ctlfd_ack(struct evlist * evlist)1923 int evlist__ctlfd_ack(struct evlist *evlist)
1924 {
1925 	int err;
1926 
1927 	if (evlist->ctl_fd.ack == -1)
1928 		return 0;
1929 
1930 	err = write(evlist->ctl_fd.ack, EVLIST_CTL_CMD_ACK_TAG,
1931 		    sizeof(EVLIST_CTL_CMD_ACK_TAG));
1932 	if (err == -1)
1933 		pr_err("failed to write to ctl_ack_fd %d: %m\n", evlist->ctl_fd.ack);
1934 
1935 	return err;
1936 }
1937 
evlist__ctlfd_process(struct evlist * evlist,enum evlist_ctl_cmd * cmd)1938 int evlist__ctlfd_process(struct evlist *evlist, enum evlist_ctl_cmd *cmd)
1939 {
1940 	int err = 0;
1941 	char cmd_data[EVLIST_CTL_CMD_MAX_LEN];
1942 	int ctlfd_pos = evlist->ctl_fd.pos;
1943 	struct pollfd *entries = evlist->core.pollfd.entries;
1944 
1945 	if (!evlist__ctlfd_initialized(evlist) || !entries[ctlfd_pos].revents)
1946 		return 0;
1947 
1948 	if (entries[ctlfd_pos].revents & POLLIN) {
1949 		err = evlist__ctlfd_recv(evlist, cmd, cmd_data,
1950 					 EVLIST_CTL_CMD_MAX_LEN);
1951 		if (err > 0) {
1952 			switch (*cmd) {
1953 			case EVLIST_CTL_CMD_ENABLE:
1954 				evlist__enable(evlist);
1955 				break;
1956 			case EVLIST_CTL_CMD_DISABLE:
1957 				evlist__disable(evlist);
1958 				break;
1959 			case EVLIST_CTL_CMD_SNAPSHOT:
1960 				break;
1961 			case EVLIST_CTL_CMD_ACK:
1962 			case EVLIST_CTL_CMD_UNSUPPORTED:
1963 			default:
1964 				pr_debug("ctlfd: unsupported %d\n", *cmd);
1965 				break;
1966 			}
1967 			if (!(*cmd == EVLIST_CTL_CMD_ACK || *cmd == EVLIST_CTL_CMD_UNSUPPORTED ||
1968 			      *cmd == EVLIST_CTL_CMD_SNAPSHOT))
1969 				evlist__ctlfd_ack(evlist);
1970 		}
1971 	}
1972 
1973 	if (entries[ctlfd_pos].revents & (POLLHUP | POLLERR))
1974 		evlist__finalize_ctlfd(evlist);
1975 	else
1976 		entries[ctlfd_pos].revents = 0;
1977 
1978 	return err;
1979 }
1980 
evlist__find_evsel(struct evlist * evlist,int idx)1981 struct evsel *evlist__find_evsel(struct evlist *evlist, int idx)
1982 {
1983 	struct evsel *evsel;
1984 
1985 	evlist__for_each_entry(evlist, evsel) {
1986 		if (evsel->idx == idx)
1987 			return evsel;
1988 	}
1989 	return NULL;
1990 }
1991