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