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