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