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