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