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