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