]> Pileus Git - ~andy/linux/blob - tools/perf/util/evlist.c
143eaf05178f10414a15fad82bcd9c26ac69df13
[~andy/linux] / tools / perf / util / evlist.c
1 /*
2  * Copyright (C) 2011, Red Hat Inc, Arnaldo Carvalho de Melo <acme@redhat.com>
3  *
4  * Parts came from builtin-{top,stat,record}.c, see those files for further
5  * copyright notes.
6  *
7  * Released under the GPL v2. (and only v2, not any later version)
8  */
9 #include "util.h"
10 #include <api/fs/debugfs.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 <unistd.h>
19
20 #include "parse-events.h"
21 #include "parse-options.h"
22
23 #include <sys/mman.h>
24
25 #include <linux/bitops.h>
26 #include <linux/hash.h>
27
28 #define FD(e, x, y) (*(int *)xyarray__entry(e->fd, x, y))
29 #define SID(e, x, y) xyarray__entry(e->sample_id, x, y)
30
31 void perf_evlist__init(struct perf_evlist *evlist, struct cpu_map *cpus,
32                        struct thread_map *threads)
33 {
34         int i;
35
36         for (i = 0; i < PERF_EVLIST__HLIST_SIZE; ++i)
37                 INIT_HLIST_HEAD(&evlist->heads[i]);
38         INIT_LIST_HEAD(&evlist->entries);
39         perf_evlist__set_maps(evlist, cpus, threads);
40         evlist->workload.pid = -1;
41 }
42
43 struct perf_evlist *perf_evlist__new(void)
44 {
45         struct perf_evlist *evlist = zalloc(sizeof(*evlist));
46
47         if (evlist != NULL)
48                 perf_evlist__init(evlist, NULL, NULL);
49
50         return evlist;
51 }
52
53 struct perf_evlist *perf_evlist__new_default(void)
54 {
55         struct perf_evlist *evlist = perf_evlist__new();
56
57         if (evlist && perf_evlist__add_default(evlist)) {
58                 perf_evlist__delete(evlist);
59                 evlist = NULL;
60         }
61
62         return evlist;
63 }
64
65 /**
66  * perf_evlist__set_id_pos - set the positions of event ids.
67  * @evlist: selected event list
68  *
69  * Events with compatible sample types all have the same id_pos
70  * and is_pos.  For convenience, put a copy on evlist.
71  */
72 void perf_evlist__set_id_pos(struct perf_evlist *evlist)
73 {
74         struct perf_evsel *first = perf_evlist__first(evlist);
75
76         evlist->id_pos = first->id_pos;
77         evlist->is_pos = first->is_pos;
78 }
79
80 static void perf_evlist__update_id_pos(struct perf_evlist *evlist)
81 {
82         struct perf_evsel *evsel;
83
84         list_for_each_entry(evsel, &evlist->entries, node)
85                 perf_evsel__calc_id_pos(evsel);
86
87         perf_evlist__set_id_pos(evlist);
88 }
89
90 static void perf_evlist__purge(struct perf_evlist *evlist)
91 {
92         struct perf_evsel *pos, *n;
93
94         list_for_each_entry_safe(pos, n, &evlist->entries, node) {
95                 list_del_init(&pos->node);
96                 perf_evsel__delete(pos);
97         }
98
99         evlist->nr_entries = 0;
100 }
101
102 void perf_evlist__exit(struct perf_evlist *evlist)
103 {
104         zfree(&evlist->mmap);
105         zfree(&evlist->pollfd);
106 }
107
108 void perf_evlist__delete(struct perf_evlist *evlist)
109 {
110         cpu_map__delete(evlist->cpus);
111         thread_map__delete(evlist->threads);
112         evlist->cpus = NULL;
113         evlist->threads = NULL;
114         perf_evlist__purge(evlist);
115         perf_evlist__exit(evlist);
116         free(evlist);
117 }
118
119 void perf_evlist__add(struct perf_evlist *evlist, struct perf_evsel *entry)
120 {
121         list_add_tail(&entry->node, &evlist->entries);
122         entry->idx = evlist->nr_entries;
123
124         if (!evlist->nr_entries++)
125                 perf_evlist__set_id_pos(evlist);
126 }
127
128 void perf_evlist__splice_list_tail(struct perf_evlist *evlist,
129                                    struct list_head *list,
130                                    int nr_entries)
131 {
132         bool set_id_pos = !evlist->nr_entries;
133
134         list_splice_tail(list, &evlist->entries);
135         evlist->nr_entries += nr_entries;
136         if (set_id_pos)
137                 perf_evlist__set_id_pos(evlist);
138 }
139
140 void __perf_evlist__set_leader(struct list_head *list)
141 {
142         struct perf_evsel *evsel, *leader;
143
144         leader = list_entry(list->next, struct perf_evsel, node);
145         evsel = list_entry(list->prev, struct perf_evsel, node);
146
147         leader->nr_members = evsel->idx - leader->idx + 1;
148
149         list_for_each_entry(evsel, list, node) {
150                 evsel->leader = leader;
151         }
152 }
153
154 void perf_evlist__set_leader(struct perf_evlist *evlist)
155 {
156         if (evlist->nr_entries) {
157                 evlist->nr_groups = evlist->nr_entries > 1 ? 1 : 0;
158                 __perf_evlist__set_leader(&evlist->entries);
159         }
160 }
161
162 int perf_evlist__add_default(struct perf_evlist *evlist)
163 {
164         struct perf_event_attr attr = {
165                 .type = PERF_TYPE_HARDWARE,
166                 .config = PERF_COUNT_HW_CPU_CYCLES,
167         };
168         struct perf_evsel *evsel;
169
170         event_attr_init(&attr);
171
172         evsel = perf_evsel__new(&attr);
173         if (evsel == NULL)
174                 goto error;
175
176         /* use strdup() because free(evsel) assumes name is allocated */
177         evsel->name = strdup("cycles");
178         if (!evsel->name)
179                 goto error_free;
180
181         perf_evlist__add(evlist, evsel);
182         return 0;
183 error_free:
184         perf_evsel__delete(evsel);
185 error:
186         return -ENOMEM;
187 }
188
189 static int perf_evlist__add_attrs(struct perf_evlist *evlist,
190                                   struct perf_event_attr *attrs, size_t nr_attrs)
191 {
192         struct perf_evsel *evsel, *n;
193         LIST_HEAD(head);
194         size_t i;
195
196         for (i = 0; i < nr_attrs; i++) {
197                 evsel = perf_evsel__new_idx(attrs + i, evlist->nr_entries + i);
198                 if (evsel == NULL)
199                         goto out_delete_partial_list;
200                 list_add_tail(&evsel->node, &head);
201         }
202
203         perf_evlist__splice_list_tail(evlist, &head, nr_attrs);
204
205         return 0;
206
207 out_delete_partial_list:
208         list_for_each_entry_safe(evsel, n, &head, node)
209                 perf_evsel__delete(evsel);
210         return -1;
211 }
212
213 int __perf_evlist__add_default_attrs(struct perf_evlist *evlist,
214                                      struct perf_event_attr *attrs, size_t nr_attrs)
215 {
216         size_t i;
217
218         for (i = 0; i < nr_attrs; i++)
219                 event_attr_init(attrs + i);
220
221         return perf_evlist__add_attrs(evlist, attrs, nr_attrs);
222 }
223
224 struct perf_evsel *
225 perf_evlist__find_tracepoint_by_id(struct perf_evlist *evlist, int id)
226 {
227         struct perf_evsel *evsel;
228
229         list_for_each_entry(evsel, &evlist->entries, node) {
230                 if (evsel->attr.type   == PERF_TYPE_TRACEPOINT &&
231                     (int)evsel->attr.config == id)
232                         return evsel;
233         }
234
235         return NULL;
236 }
237
238 struct perf_evsel *
239 perf_evlist__find_tracepoint_by_name(struct perf_evlist *evlist,
240                                      const char *name)
241 {
242         struct perf_evsel *evsel;
243
244         list_for_each_entry(evsel, &evlist->entries, node) {
245                 if ((evsel->attr.type == PERF_TYPE_TRACEPOINT) &&
246                     (strcmp(evsel->name, name) == 0))
247                         return evsel;
248         }
249
250         return NULL;
251 }
252
253 int perf_evlist__add_newtp(struct perf_evlist *evlist,
254                            const char *sys, const char *name, void *handler)
255 {
256         struct perf_evsel *evsel = perf_evsel__newtp(sys, name);
257
258         if (evsel == NULL)
259                 return -1;
260
261         evsel->handler = handler;
262         perf_evlist__add(evlist, evsel);
263         return 0;
264 }
265
266 void perf_evlist__disable(struct perf_evlist *evlist)
267 {
268         int cpu, thread;
269         struct perf_evsel *pos;
270         int nr_cpus = cpu_map__nr(evlist->cpus);
271         int nr_threads = thread_map__nr(evlist->threads);
272
273         for (cpu = 0; cpu < nr_cpus; cpu++) {
274                 list_for_each_entry(pos, &evlist->entries, node) {
275                         if (!perf_evsel__is_group_leader(pos) || !pos->fd)
276                                 continue;
277                         for (thread = 0; thread < nr_threads; thread++)
278                                 ioctl(FD(pos, cpu, thread),
279                                       PERF_EVENT_IOC_DISABLE, 0);
280                 }
281         }
282 }
283
284 void perf_evlist__enable(struct perf_evlist *evlist)
285 {
286         int cpu, thread;
287         struct perf_evsel *pos;
288         int nr_cpus = cpu_map__nr(evlist->cpus);
289         int nr_threads = thread_map__nr(evlist->threads);
290
291         for (cpu = 0; cpu < nr_cpus; cpu++) {
292                 list_for_each_entry(pos, &evlist->entries, node) {
293                         if (!perf_evsel__is_group_leader(pos) || !pos->fd)
294                                 continue;
295                         for (thread = 0; thread < nr_threads; thread++)
296                                 ioctl(FD(pos, cpu, thread),
297                                       PERF_EVENT_IOC_ENABLE, 0);
298                 }
299         }
300 }
301
302 int perf_evlist__disable_event(struct perf_evlist *evlist,
303                                struct perf_evsel *evsel)
304 {
305         int cpu, thread, err;
306
307         if (!evsel->fd)
308                 return 0;
309
310         for (cpu = 0; cpu < evlist->cpus->nr; cpu++) {
311                 for (thread = 0; thread < evlist->threads->nr; thread++) {
312                         err = ioctl(FD(evsel, cpu, thread),
313                                     PERF_EVENT_IOC_DISABLE, 0);
314                         if (err)
315                                 return err;
316                 }
317         }
318         return 0;
319 }
320
321 int perf_evlist__enable_event(struct perf_evlist *evlist,
322                               struct perf_evsel *evsel)
323 {
324         int cpu, thread, err;
325
326         if (!evsel->fd)
327                 return -EINVAL;
328
329         for (cpu = 0; cpu < evlist->cpus->nr; cpu++) {
330                 for (thread = 0; thread < evlist->threads->nr; thread++) {
331                         err = ioctl(FD(evsel, cpu, thread),
332                                     PERF_EVENT_IOC_ENABLE, 0);
333                         if (err)
334                                 return err;
335                 }
336         }
337         return 0;
338 }
339
340 static int perf_evlist__alloc_pollfd(struct perf_evlist *evlist)
341 {
342         int nr_cpus = cpu_map__nr(evlist->cpus);
343         int nr_threads = thread_map__nr(evlist->threads);
344         int nfds = nr_cpus * nr_threads * evlist->nr_entries;
345         evlist->pollfd = malloc(sizeof(struct pollfd) * nfds);
346         return evlist->pollfd != NULL ? 0 : -ENOMEM;
347 }
348
349 void perf_evlist__add_pollfd(struct perf_evlist *evlist, int fd)
350 {
351         fcntl(fd, F_SETFL, O_NONBLOCK);
352         evlist->pollfd[evlist->nr_fds].fd = fd;
353         evlist->pollfd[evlist->nr_fds].events = POLLIN;
354         evlist->nr_fds++;
355 }
356
357 static void perf_evlist__id_hash(struct perf_evlist *evlist,
358                                  struct perf_evsel *evsel,
359                                  int cpu, int thread, u64 id)
360 {
361         int hash;
362         struct perf_sample_id *sid = SID(evsel, cpu, thread);
363
364         sid->id = id;
365         sid->evsel = evsel;
366         hash = hash_64(sid->id, PERF_EVLIST__HLIST_BITS);
367         hlist_add_head(&sid->node, &evlist->heads[hash]);
368 }
369
370 void perf_evlist__id_add(struct perf_evlist *evlist, struct perf_evsel *evsel,
371                          int cpu, int thread, u64 id)
372 {
373         perf_evlist__id_hash(evlist, evsel, cpu, thread, id);
374         evsel->id[evsel->ids++] = id;
375 }
376
377 static int perf_evlist__id_add_fd(struct perf_evlist *evlist,
378                                   struct perf_evsel *evsel,
379                                   int cpu, int thread, int fd)
380 {
381         u64 read_data[4] = { 0, };
382         int id_idx = 1; /* The first entry is the counter value */
383         u64 id;
384         int ret;
385
386         ret = ioctl(fd, PERF_EVENT_IOC_ID, &id);
387         if (!ret)
388                 goto add;
389
390         if (errno != ENOTTY)
391                 return -1;
392
393         /* Legacy way to get event id.. All hail to old kernels! */
394
395         /*
396          * This way does not work with group format read, so bail
397          * out in that case.
398          */
399         if (perf_evlist__read_format(evlist) & PERF_FORMAT_GROUP)
400                 return -1;
401
402         if (!(evsel->attr.read_format & PERF_FORMAT_ID) ||
403             read(fd, &read_data, sizeof(read_data)) == -1)
404                 return -1;
405
406         if (evsel->attr.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
407                 ++id_idx;
408         if (evsel->attr.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
409                 ++id_idx;
410
411         id = read_data[id_idx];
412
413  add:
414         perf_evlist__id_add(evlist, evsel, cpu, thread, id);
415         return 0;
416 }
417
418 struct perf_sample_id *perf_evlist__id2sid(struct perf_evlist *evlist, u64 id)
419 {
420         struct hlist_head *head;
421         struct perf_sample_id *sid;
422         int hash;
423
424         hash = hash_64(id, PERF_EVLIST__HLIST_BITS);
425         head = &evlist->heads[hash];
426
427         hlist_for_each_entry(sid, head, node)
428                 if (sid->id == id)
429                         return sid;
430
431         return NULL;
432 }
433
434 struct perf_evsel *perf_evlist__id2evsel(struct perf_evlist *evlist, u64 id)
435 {
436         struct perf_sample_id *sid;
437
438         if (evlist->nr_entries == 1)
439                 return perf_evlist__first(evlist);
440
441         sid = perf_evlist__id2sid(evlist, id);
442         if (sid)
443                 return sid->evsel;
444
445         if (!perf_evlist__sample_id_all(evlist))
446                 return perf_evlist__first(evlist);
447
448         return NULL;
449 }
450
451 static int perf_evlist__event2id(struct perf_evlist *evlist,
452                                  union perf_event *event, u64 *id)
453 {
454         const u64 *array = event->sample.array;
455         ssize_t n;
456
457         n = (event->header.size - sizeof(event->header)) >> 3;
458
459         if (event->header.type == PERF_RECORD_SAMPLE) {
460                 if (evlist->id_pos >= n)
461                         return -1;
462                 *id = array[evlist->id_pos];
463         } else {
464                 if (evlist->is_pos > n)
465                         return -1;
466                 n -= evlist->is_pos;
467                 *id = array[n];
468         }
469         return 0;
470 }
471
472 static struct perf_evsel *perf_evlist__event2evsel(struct perf_evlist *evlist,
473                                                    union perf_event *event)
474 {
475         struct perf_evsel *first = perf_evlist__first(evlist);
476         struct hlist_head *head;
477         struct perf_sample_id *sid;
478         int hash;
479         u64 id;
480
481         if (evlist->nr_entries == 1)
482                 return first;
483
484         if (!first->attr.sample_id_all &&
485             event->header.type != PERF_RECORD_SAMPLE)
486                 return first;
487
488         if (perf_evlist__event2id(evlist, event, &id))
489                 return NULL;
490
491         /* Synthesized events have an id of zero */
492         if (!id)
493                 return first;
494
495         hash = hash_64(id, PERF_EVLIST__HLIST_BITS);
496         head = &evlist->heads[hash];
497
498         hlist_for_each_entry(sid, head, node) {
499                 if (sid->id == id)
500                         return sid->evsel;
501         }
502         return NULL;
503 }
504
505 union perf_event *perf_evlist__mmap_read(struct perf_evlist *evlist, int idx)
506 {
507         struct perf_mmap *md = &evlist->mmap[idx];
508         unsigned int head = perf_mmap__read_head(md);
509         unsigned int old = md->prev;
510         unsigned char *data = md->base + page_size;
511         union perf_event *event = NULL;
512
513         if (evlist->overwrite) {
514                 /*
515                  * If we're further behind than half the buffer, there's a chance
516                  * the writer will bite our tail and mess up the samples under us.
517                  *
518                  * If we somehow ended up ahead of the head, we got messed up.
519                  *
520                  * In either case, truncate and restart at head.
521                  */
522                 int diff = head - old;
523                 if (diff > md->mask / 2 || diff < 0) {
524                         fprintf(stderr, "WARNING: failed to keep up with mmap data.\n");
525
526                         /*
527                          * head points to a known good entry, start there.
528                          */
529                         old = head;
530                 }
531         }
532
533         if (old != head) {
534                 size_t size;
535
536                 event = (union perf_event *)&data[old & md->mask];
537                 size = event->header.size;
538
539                 /*
540                  * Event straddles the mmap boundary -- header should always
541                  * be inside due to u64 alignment of output.
542                  */
543                 if ((old & md->mask) + size != ((old + size) & md->mask)) {
544                         unsigned int offset = old;
545                         unsigned int len = min(sizeof(*event), size), cpy;
546                         void *dst = md->event_copy;
547
548                         do {
549                                 cpy = min(md->mask + 1 - (offset & md->mask), len);
550                                 memcpy(dst, &data[offset & md->mask], cpy);
551                                 offset += cpy;
552                                 dst += cpy;
553                                 len -= cpy;
554                         } while (len);
555
556                         event = (union perf_event *) md->event_copy;
557                 }
558
559                 old += size;
560         }
561
562         md->prev = old;
563
564         return event;
565 }
566
567 void perf_evlist__mmap_consume(struct perf_evlist *evlist, int idx)
568 {
569         if (!evlist->overwrite) {
570                 struct perf_mmap *md = &evlist->mmap[idx];
571                 unsigned int old = md->prev;
572
573                 perf_mmap__write_tail(md, old);
574         }
575 }
576
577 static void __perf_evlist__munmap(struct perf_evlist *evlist, int idx)
578 {
579         if (evlist->mmap[idx].base != NULL) {
580                 munmap(evlist->mmap[idx].base, evlist->mmap_len);
581                 evlist->mmap[idx].base = NULL;
582         }
583 }
584
585 void perf_evlist__munmap(struct perf_evlist *evlist)
586 {
587         int i;
588
589         for (i = 0; i < evlist->nr_mmaps; i++)
590                 __perf_evlist__munmap(evlist, i);
591
592         zfree(&evlist->mmap);
593 }
594
595 static int perf_evlist__alloc_mmap(struct perf_evlist *evlist)
596 {
597         evlist->nr_mmaps = cpu_map__nr(evlist->cpus);
598         if (cpu_map__empty(evlist->cpus))
599                 evlist->nr_mmaps = thread_map__nr(evlist->threads);
600         evlist->mmap = zalloc(evlist->nr_mmaps * sizeof(struct perf_mmap));
601         return evlist->mmap != NULL ? 0 : -ENOMEM;
602 }
603
604 static int __perf_evlist__mmap(struct perf_evlist *evlist,
605                                int idx, int prot, int mask, int fd)
606 {
607         evlist->mmap[idx].prev = 0;
608         evlist->mmap[idx].mask = mask;
609         evlist->mmap[idx].base = mmap(NULL, evlist->mmap_len, prot,
610                                       MAP_SHARED, fd, 0);
611         if (evlist->mmap[idx].base == MAP_FAILED) {
612                 pr_debug2("failed to mmap perf event ring buffer, error %d\n",
613                           errno);
614                 evlist->mmap[idx].base = NULL;
615                 return -1;
616         }
617
618         perf_evlist__add_pollfd(evlist, fd);
619         return 0;
620 }
621
622 static int perf_evlist__mmap_per_evsel(struct perf_evlist *evlist, int idx,
623                                        int prot, int mask, int cpu, int thread,
624                                        int *output)
625 {
626         struct perf_evsel *evsel;
627
628         list_for_each_entry(evsel, &evlist->entries, node) {
629                 int fd = FD(evsel, cpu, thread);
630
631                 if (*output == -1) {
632                         *output = fd;
633                         if (__perf_evlist__mmap(evlist, idx, prot, mask,
634                                                 *output) < 0)
635                                 return -1;
636                 } else {
637                         if (ioctl(fd, PERF_EVENT_IOC_SET_OUTPUT, *output) != 0)
638                                 return -1;
639                 }
640
641                 if ((evsel->attr.read_format & PERF_FORMAT_ID) &&
642                     perf_evlist__id_add_fd(evlist, evsel, cpu, thread, fd) < 0)
643                         return -1;
644         }
645
646         return 0;
647 }
648
649 static int perf_evlist__mmap_per_cpu(struct perf_evlist *evlist, int prot,
650                                      int mask)
651 {
652         int cpu, thread;
653         int nr_cpus = cpu_map__nr(evlist->cpus);
654         int nr_threads = thread_map__nr(evlist->threads);
655
656         pr_debug2("perf event ring buffer mmapped per cpu\n");
657         for (cpu = 0; cpu < nr_cpus; cpu++) {
658                 int output = -1;
659
660                 for (thread = 0; thread < nr_threads; thread++) {
661                         if (perf_evlist__mmap_per_evsel(evlist, cpu, prot, mask,
662                                                         cpu, thread, &output))
663                                 goto out_unmap;
664                 }
665         }
666
667         return 0;
668
669 out_unmap:
670         for (cpu = 0; cpu < nr_cpus; cpu++)
671                 __perf_evlist__munmap(evlist, cpu);
672         return -1;
673 }
674
675 static int perf_evlist__mmap_per_thread(struct perf_evlist *evlist, int prot,
676                                         int mask)
677 {
678         int thread;
679         int nr_threads = thread_map__nr(evlist->threads);
680
681         pr_debug2("perf event ring buffer mmapped per thread\n");
682         for (thread = 0; thread < nr_threads; thread++) {
683                 int output = -1;
684
685                 if (perf_evlist__mmap_per_evsel(evlist, thread, prot, mask, 0,
686                                                 thread, &output))
687                         goto out_unmap;
688         }
689
690         return 0;
691
692 out_unmap:
693         for (thread = 0; thread < nr_threads; thread++)
694                 __perf_evlist__munmap(evlist, thread);
695         return -1;
696 }
697
698 static size_t perf_evlist__mmap_size(unsigned long pages)
699 {
700         /* 512 kiB: default amount of unprivileged mlocked memory */
701         if (pages == UINT_MAX)
702                 pages = (512 * 1024) / page_size;
703         else if (!is_power_of_2(pages))
704                 return 0;
705
706         return (pages + 1) * page_size;
707 }
708
709 static long parse_pages_arg(const char *str, unsigned long min,
710                             unsigned long max)
711 {
712         unsigned long pages, val;
713         static struct parse_tag tags[] = {
714                 { .tag  = 'B', .mult = 1       },
715                 { .tag  = 'K', .mult = 1 << 10 },
716                 { .tag  = 'M', .mult = 1 << 20 },
717                 { .tag  = 'G', .mult = 1 << 30 },
718                 { .tag  = 0 },
719         };
720
721         if (str == NULL)
722                 return -EINVAL;
723
724         val = parse_tag_value(str, tags);
725         if (val != (unsigned long) -1) {
726                 /* we got file size value */
727                 pages = PERF_ALIGN(val, page_size) / page_size;
728         } else {
729                 /* we got pages count value */
730                 char *eptr;
731                 pages = strtoul(str, &eptr, 10);
732                 if (*eptr != '\0')
733                         return -EINVAL;
734         }
735
736         if (pages == 0 && min == 0) {
737                 /* leave number of pages at 0 */
738         } else if (!is_power_of_2(pages)) {
739                 /* round pages up to next power of 2 */
740                 pages = next_pow2_l(pages);
741                 if (!pages)
742                         return -EINVAL;
743                 pr_info("rounding mmap pages size to %lu bytes (%lu pages)\n",
744                         pages * page_size, pages);
745         }
746
747         if (pages > max)
748                 return -EINVAL;
749
750         return pages;
751 }
752
753 int perf_evlist__parse_mmap_pages(const struct option *opt, const char *str,
754                                   int unset __maybe_unused)
755 {
756         unsigned int *mmap_pages = opt->value;
757         unsigned long max = UINT_MAX;
758         long pages;
759
760         if (max > SIZE_MAX / page_size)
761                 max = SIZE_MAX / page_size;
762
763         pages = parse_pages_arg(str, 1, max);
764         if (pages < 0) {
765                 pr_err("Invalid argument for --mmap_pages/-m\n");
766                 return -1;
767         }
768
769         *mmap_pages = pages;
770         return 0;
771 }
772
773 /**
774  * perf_evlist__mmap - Create mmaps to receive events.
775  * @evlist: list of events
776  * @pages: map length in pages
777  * @overwrite: overwrite older events?
778  *
779  * If @overwrite is %false the user needs to signal event consumption using
780  * perf_mmap__write_tail().  Using perf_evlist__mmap_read() does this
781  * automatically.
782  *
783  * Return: %0 on success, negative error code otherwise.
784  */
785 int perf_evlist__mmap(struct perf_evlist *evlist, unsigned int pages,
786                       bool overwrite)
787 {
788         struct perf_evsel *evsel;
789         const struct cpu_map *cpus = evlist->cpus;
790         const struct thread_map *threads = evlist->threads;
791         int prot = PROT_READ | (overwrite ? 0 : PROT_WRITE), mask;
792
793         if (evlist->mmap == NULL && perf_evlist__alloc_mmap(evlist) < 0)
794                 return -ENOMEM;
795
796         if (evlist->pollfd == NULL && perf_evlist__alloc_pollfd(evlist) < 0)
797                 return -ENOMEM;
798
799         evlist->overwrite = overwrite;
800         evlist->mmap_len = perf_evlist__mmap_size(pages);
801         pr_debug("mmap size %zuB\n", evlist->mmap_len);
802         mask = evlist->mmap_len - page_size - 1;
803
804         list_for_each_entry(evsel, &evlist->entries, node) {
805                 if ((evsel->attr.read_format & PERF_FORMAT_ID) &&
806                     evsel->sample_id == NULL &&
807                     perf_evsel__alloc_id(evsel, cpu_map__nr(cpus), threads->nr) < 0)
808                         return -ENOMEM;
809         }
810
811         if (cpu_map__empty(cpus))
812                 return perf_evlist__mmap_per_thread(evlist, prot, mask);
813
814         return perf_evlist__mmap_per_cpu(evlist, prot, mask);
815 }
816
817 int perf_evlist__create_maps(struct perf_evlist *evlist, struct target *target)
818 {
819         evlist->threads = thread_map__new_str(target->pid, target->tid,
820                                               target->uid);
821
822         if (evlist->threads == NULL)
823                 return -1;
824
825         if (target__uses_dummy_map(target))
826                 evlist->cpus = cpu_map__dummy_new();
827         else
828                 evlist->cpus = cpu_map__new(target->cpu_list);
829
830         if (evlist->cpus == NULL)
831                 goto out_delete_threads;
832
833         return 0;
834
835 out_delete_threads:
836         thread_map__delete(evlist->threads);
837         return -1;
838 }
839
840 int perf_evlist__apply_filters(struct perf_evlist *evlist)
841 {
842         struct perf_evsel *evsel;
843         int err = 0;
844         const int ncpus = cpu_map__nr(evlist->cpus),
845                   nthreads = thread_map__nr(evlist->threads);
846
847         list_for_each_entry(evsel, &evlist->entries, node) {
848                 if (evsel->filter == NULL)
849                         continue;
850
851                 err = perf_evsel__set_filter(evsel, ncpus, nthreads, evsel->filter);
852                 if (err)
853                         break;
854         }
855
856         return err;
857 }
858
859 int perf_evlist__set_filter(struct perf_evlist *evlist, const char *filter)
860 {
861         struct perf_evsel *evsel;
862         int err = 0;
863         const int ncpus = cpu_map__nr(evlist->cpus),
864                   nthreads = thread_map__nr(evlist->threads);
865
866         list_for_each_entry(evsel, &evlist->entries, node) {
867                 err = perf_evsel__set_filter(evsel, ncpus, nthreads, filter);
868                 if (err)
869                         break;
870         }
871
872         return err;
873 }
874
875 bool perf_evlist__valid_sample_type(struct perf_evlist *evlist)
876 {
877         struct perf_evsel *pos;
878
879         if (evlist->nr_entries == 1)
880                 return true;
881
882         if (evlist->id_pos < 0 || evlist->is_pos < 0)
883                 return false;
884
885         list_for_each_entry(pos, &evlist->entries, node) {
886                 if (pos->id_pos != evlist->id_pos ||
887                     pos->is_pos != evlist->is_pos)
888                         return false;
889         }
890
891         return true;
892 }
893
894 u64 __perf_evlist__combined_sample_type(struct perf_evlist *evlist)
895 {
896         struct perf_evsel *evsel;
897
898         if (evlist->combined_sample_type)
899                 return evlist->combined_sample_type;
900
901         list_for_each_entry(evsel, &evlist->entries, node)
902                 evlist->combined_sample_type |= evsel->attr.sample_type;
903
904         return evlist->combined_sample_type;
905 }
906
907 u64 perf_evlist__combined_sample_type(struct perf_evlist *evlist)
908 {
909         evlist->combined_sample_type = 0;
910         return __perf_evlist__combined_sample_type(evlist);
911 }
912
913 bool perf_evlist__valid_read_format(struct perf_evlist *evlist)
914 {
915         struct perf_evsel *first = perf_evlist__first(evlist), *pos = first;
916         u64 read_format = first->attr.read_format;
917         u64 sample_type = first->attr.sample_type;
918
919         list_for_each_entry_continue(pos, &evlist->entries, node) {
920                 if (read_format != pos->attr.read_format)
921                         return false;
922         }
923
924         /* PERF_SAMPLE_READ imples PERF_FORMAT_ID. */
925         if ((sample_type & PERF_SAMPLE_READ) &&
926             !(read_format & PERF_FORMAT_ID)) {
927                 return false;
928         }
929
930         return true;
931 }
932
933 u64 perf_evlist__read_format(struct perf_evlist *evlist)
934 {
935         struct perf_evsel *first = perf_evlist__first(evlist);
936         return first->attr.read_format;
937 }
938
939 u16 perf_evlist__id_hdr_size(struct perf_evlist *evlist)
940 {
941         struct perf_evsel *first = perf_evlist__first(evlist);
942         struct perf_sample *data;
943         u64 sample_type;
944         u16 size = 0;
945
946         if (!first->attr.sample_id_all)
947                 goto out;
948
949         sample_type = first->attr.sample_type;
950
951         if (sample_type & PERF_SAMPLE_TID)
952                 size += sizeof(data->tid) * 2;
953
954        if (sample_type & PERF_SAMPLE_TIME)
955                 size += sizeof(data->time);
956
957         if (sample_type & PERF_SAMPLE_ID)
958                 size += sizeof(data->id);
959
960         if (sample_type & PERF_SAMPLE_STREAM_ID)
961                 size += sizeof(data->stream_id);
962
963         if (sample_type & PERF_SAMPLE_CPU)
964                 size += sizeof(data->cpu) * 2;
965
966         if (sample_type & PERF_SAMPLE_IDENTIFIER)
967                 size += sizeof(data->id);
968 out:
969         return size;
970 }
971
972 bool perf_evlist__valid_sample_id_all(struct perf_evlist *evlist)
973 {
974         struct perf_evsel *first = perf_evlist__first(evlist), *pos = first;
975
976         list_for_each_entry_continue(pos, &evlist->entries, node) {
977                 if (first->attr.sample_id_all != pos->attr.sample_id_all)
978                         return false;
979         }
980
981         return true;
982 }
983
984 bool perf_evlist__sample_id_all(struct perf_evlist *evlist)
985 {
986         struct perf_evsel *first = perf_evlist__first(evlist);
987         return first->attr.sample_id_all;
988 }
989
990 void perf_evlist__set_selected(struct perf_evlist *evlist,
991                                struct perf_evsel *evsel)
992 {
993         evlist->selected = evsel;
994 }
995
996 void perf_evlist__close(struct perf_evlist *evlist)
997 {
998         struct perf_evsel *evsel;
999         int ncpus = cpu_map__nr(evlist->cpus);
1000         int nthreads = thread_map__nr(evlist->threads);
1001
1002         list_for_each_entry_reverse(evsel, &evlist->entries, node)
1003                 perf_evsel__close(evsel, ncpus, nthreads);
1004 }
1005
1006 int perf_evlist__open(struct perf_evlist *evlist)
1007 {
1008         struct perf_evsel *evsel;
1009         int err;
1010
1011         perf_evlist__update_id_pos(evlist);
1012
1013         list_for_each_entry(evsel, &evlist->entries, node) {
1014                 err = perf_evsel__open(evsel, evlist->cpus, evlist->threads);
1015                 if (err < 0)
1016                         goto out_err;
1017         }
1018
1019         return 0;
1020 out_err:
1021         perf_evlist__close(evlist);
1022         errno = -err;
1023         return err;
1024 }
1025
1026 int perf_evlist__prepare_workload(struct perf_evlist *evlist, struct target *target,
1027                                   const char *argv[], bool pipe_output,
1028                                   void (*exec_error)(int signo, siginfo_t *info, void *ucontext))
1029 {
1030         int child_ready_pipe[2], go_pipe[2];
1031         char bf;
1032
1033         if (pipe(child_ready_pipe) < 0) {
1034                 perror("failed to create 'ready' pipe");
1035                 return -1;
1036         }
1037
1038         if (pipe(go_pipe) < 0) {
1039                 perror("failed to create 'go' pipe");
1040                 goto out_close_ready_pipe;
1041         }
1042
1043         evlist->workload.pid = fork();
1044         if (evlist->workload.pid < 0) {
1045                 perror("failed to fork");
1046                 goto out_close_pipes;
1047         }
1048
1049         if (!evlist->workload.pid) {
1050                 if (pipe_output)
1051                         dup2(2, 1);
1052
1053                 signal(SIGTERM, SIG_DFL);
1054
1055                 close(child_ready_pipe[0]);
1056                 close(go_pipe[1]);
1057                 fcntl(go_pipe[0], F_SETFD, FD_CLOEXEC);
1058
1059                 /*
1060                  * Tell the parent we're ready to go
1061                  */
1062                 close(child_ready_pipe[1]);
1063
1064                 /*
1065                  * Wait until the parent tells us to go.
1066                  */
1067                 if (read(go_pipe[0], &bf, 1) == -1)
1068                         perror("unable to read pipe");
1069
1070                 execvp(argv[0], (char **)argv);
1071
1072                 if (exec_error) {
1073                         union sigval val;
1074
1075                         val.sival_int = errno;
1076                         if (sigqueue(getppid(), SIGUSR1, val))
1077                                 perror(argv[0]);
1078                 } else
1079                         perror(argv[0]);
1080                 exit(-1);
1081         }
1082
1083         if (exec_error) {
1084                 struct sigaction act = {
1085                         .sa_flags     = SA_SIGINFO,
1086                         .sa_sigaction = exec_error,
1087                 };
1088                 sigaction(SIGUSR1, &act, NULL);
1089         }
1090
1091         if (target__none(target))
1092                 evlist->threads->map[0] = evlist->workload.pid;
1093
1094         close(child_ready_pipe[1]);
1095         close(go_pipe[0]);
1096         /*
1097          * wait for child to settle
1098          */
1099         if (read(child_ready_pipe[0], &bf, 1) == -1) {
1100                 perror("unable to read pipe");
1101                 goto out_close_pipes;
1102         }
1103
1104         fcntl(go_pipe[1], F_SETFD, FD_CLOEXEC);
1105         evlist->workload.cork_fd = go_pipe[1];
1106         close(child_ready_pipe[0]);
1107         return 0;
1108
1109 out_close_pipes:
1110         close(go_pipe[0]);
1111         close(go_pipe[1]);
1112 out_close_ready_pipe:
1113         close(child_ready_pipe[0]);
1114         close(child_ready_pipe[1]);
1115         return -1;
1116 }
1117
1118 int perf_evlist__start_workload(struct perf_evlist *evlist)
1119 {
1120         if (evlist->workload.cork_fd > 0) {
1121                 char bf = 0;
1122                 int ret;
1123                 /*
1124                  * Remove the cork, let it rip!
1125                  */
1126                 ret = write(evlist->workload.cork_fd, &bf, 1);
1127                 if (ret < 0)
1128                         perror("enable to write to pipe");
1129
1130                 close(evlist->workload.cork_fd);
1131                 return ret;
1132         }
1133
1134         return 0;
1135 }
1136
1137 int perf_evlist__parse_sample(struct perf_evlist *evlist, union perf_event *event,
1138                               struct perf_sample *sample)
1139 {
1140         struct perf_evsel *evsel = perf_evlist__event2evsel(evlist, event);
1141
1142         if (!evsel)
1143                 return -EFAULT;
1144         return perf_evsel__parse_sample(evsel, event, sample);
1145 }
1146
1147 size_t perf_evlist__fprintf(struct perf_evlist *evlist, FILE *fp)
1148 {
1149         struct perf_evsel *evsel;
1150         size_t printed = 0;
1151
1152         list_for_each_entry(evsel, &evlist->entries, node) {
1153                 printed += fprintf(fp, "%s%s", evsel->idx ? ", " : "",
1154                                    perf_evsel__name(evsel));
1155         }
1156
1157         return printed + fprintf(fp, "\n");
1158 }
1159
1160 int perf_evlist__strerror_tp(struct perf_evlist *evlist __maybe_unused,
1161                              int err, char *buf, size_t size)
1162 {
1163         char sbuf[128];
1164
1165         switch (err) {
1166         case ENOENT:
1167                 scnprintf(buf, size, "%s",
1168                           "Error:\tUnable to find debugfs\n"
1169                           "Hint:\tWas your kernel was compiled with debugfs support?\n"
1170                           "Hint:\tIs the debugfs filesystem mounted?\n"
1171                           "Hint:\tTry 'sudo mount -t debugfs nodev /sys/kernel/debug'");
1172                 break;
1173         case EACCES:
1174                 scnprintf(buf, size,
1175                           "Error:\tNo permissions to read %s/tracing/events/raw_syscalls\n"
1176                           "Hint:\tTry 'sudo mount -o remount,mode=755 %s'\n",
1177                           debugfs_mountpoint, debugfs_mountpoint);
1178                 break;
1179         default:
1180                 scnprintf(buf, size, "%s", strerror_r(err, sbuf, sizeof(sbuf)));
1181                 break;
1182         }
1183
1184         return 0;
1185 }
1186
1187 int perf_evlist__strerror_open(struct perf_evlist *evlist __maybe_unused,
1188                                int err, char *buf, size_t size)
1189 {
1190         int printed, value;
1191         char sbuf[128], *emsg = strerror_r(err, sbuf, sizeof(sbuf));
1192
1193         switch (err) {
1194         case EACCES:
1195         case EPERM:
1196                 printed = scnprintf(buf, size,
1197                                     "Error:\t%s.\n"
1198                                     "Hint:\tCheck /proc/sys/kernel/perf_event_paranoid setting.", emsg);
1199
1200                 value = perf_event_paranoid();
1201
1202                 printed += scnprintf(buf + printed, size - printed, "\nHint:\t");
1203
1204                 if (value >= 2) {
1205                         printed += scnprintf(buf + printed, size - printed,
1206                                              "For your workloads it needs to be <= 1\nHint:\t");
1207                 }
1208                 printed += scnprintf(buf + printed, size - printed,
1209                                      "For system wide tracing it needs to be set to -1");
1210
1211                 printed += scnprintf(buf + printed, size - printed,
1212                                     ".\nHint:\tThe current value is %d.", value);
1213                 break;
1214         default:
1215                 scnprintf(buf, size, "%s", emsg);
1216                 break;
1217         }
1218
1219         return 0;
1220 }
1221
1222 void perf_evlist__to_front(struct perf_evlist *evlist,
1223                            struct perf_evsel *move_evsel)
1224 {
1225         struct perf_evsel *evsel, *n;
1226         LIST_HEAD(move);
1227
1228         if (move_evsel == perf_evlist__first(evlist))
1229                 return;
1230
1231         list_for_each_entry_safe(evsel, n, &evlist->entries, node) {
1232                 if (evsel->leader == move_evsel->leader)
1233                         list_move_tail(&evsel->node, &move);
1234         }
1235
1236         list_splice(&move, &evlist->entries);
1237 }