perf evlist: Make set_maps() more resilient
[linux-2.6-block.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/fs.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 #include <linux/log2.h>
28
29 static void perf_evlist__mmap_put(struct perf_evlist *evlist, int idx);
30 static void __perf_evlist__munmap(struct perf_evlist *evlist, int idx);
31
32 #define FD(e, x, y) (*(int *)xyarray__entry(e->fd, x, y))
33 #define SID(e, x, y) xyarray__entry(e->sample_id, x, y)
34
35 void perf_evlist__init(struct perf_evlist *evlist, struct cpu_map *cpus,
36                        struct thread_map *threads)
37 {
38         int i;
39
40         for (i = 0; i < PERF_EVLIST__HLIST_SIZE; ++i)
41                 INIT_HLIST_HEAD(&evlist->heads[i]);
42         INIT_LIST_HEAD(&evlist->entries);
43         perf_evlist__set_maps(evlist, cpus, threads);
44         fdarray__init(&evlist->pollfd, 64);
45         evlist->workload.pid = -1;
46 }
47
48 struct perf_evlist *perf_evlist__new(void)
49 {
50         struct perf_evlist *evlist = zalloc(sizeof(*evlist));
51
52         if (evlist != NULL)
53                 perf_evlist__init(evlist, NULL, NULL);
54
55         return evlist;
56 }
57
58 struct perf_evlist *perf_evlist__new_default(void)
59 {
60         struct perf_evlist *evlist = perf_evlist__new();
61
62         if (evlist && perf_evlist__add_default(evlist)) {
63                 perf_evlist__delete(evlist);
64                 evlist = NULL;
65         }
66
67         return evlist;
68 }
69
70 /**
71  * perf_evlist__set_id_pos - set the positions of event ids.
72  * @evlist: selected event list
73  *
74  * Events with compatible sample types all have the same id_pos
75  * and is_pos.  For convenience, put a copy on evlist.
76  */
77 void perf_evlist__set_id_pos(struct perf_evlist *evlist)
78 {
79         struct perf_evsel *first = perf_evlist__first(evlist);
80
81         evlist->id_pos = first->id_pos;
82         evlist->is_pos = first->is_pos;
83 }
84
85 static void perf_evlist__update_id_pos(struct perf_evlist *evlist)
86 {
87         struct perf_evsel *evsel;
88
89         evlist__for_each(evlist, evsel)
90                 perf_evsel__calc_id_pos(evsel);
91
92         perf_evlist__set_id_pos(evlist);
93 }
94
95 static void perf_evlist__purge(struct perf_evlist *evlist)
96 {
97         struct perf_evsel *pos, *n;
98
99         evlist__for_each_safe(evlist, n, pos) {
100                 list_del_init(&pos->node);
101                 pos->evlist = NULL;
102                 perf_evsel__delete(pos);
103         }
104
105         evlist->nr_entries = 0;
106 }
107
108 void perf_evlist__exit(struct perf_evlist *evlist)
109 {
110         zfree(&evlist->mmap);
111         fdarray__exit(&evlist->pollfd);
112 }
113
114 void perf_evlist__delete(struct perf_evlist *evlist)
115 {
116         perf_evlist__munmap(evlist);
117         perf_evlist__close(evlist);
118         cpu_map__put(evlist->cpus);
119         thread_map__put(evlist->threads);
120         evlist->cpus = NULL;
121         evlist->threads = NULL;
122         perf_evlist__purge(evlist);
123         perf_evlist__exit(evlist);
124         free(evlist);
125 }
126
127 void perf_evlist__add(struct perf_evlist *evlist, struct perf_evsel *entry)
128 {
129         entry->evlist = evlist;
130         list_add_tail(&entry->node, &evlist->entries);
131         entry->idx = evlist->nr_entries;
132         entry->tracking = !entry->idx;
133
134         if (!evlist->nr_entries++)
135                 perf_evlist__set_id_pos(evlist);
136 }
137
138 void perf_evlist__splice_list_tail(struct perf_evlist *evlist,
139                                    struct list_head *list)
140 {
141         struct perf_evsel *evsel, *temp;
142
143         __evlist__for_each_safe(list, temp, evsel) {
144                 list_del_init(&evsel->node);
145                 perf_evlist__add(evlist, evsel);
146         }
147 }
148
149 void __perf_evlist__set_leader(struct list_head *list)
150 {
151         struct perf_evsel *evsel, *leader;
152
153         leader = list_entry(list->next, struct perf_evsel, node);
154         evsel = list_entry(list->prev, struct perf_evsel, node);
155
156         leader->nr_members = evsel->idx - leader->idx + 1;
157
158         __evlist__for_each(list, evsel) {
159                 evsel->leader = leader;
160         }
161 }
162
163 void perf_evlist__set_leader(struct perf_evlist *evlist)
164 {
165         if (evlist->nr_entries) {
166                 evlist->nr_groups = evlist->nr_entries > 1 ? 1 : 0;
167                 __perf_evlist__set_leader(&evlist->entries);
168         }
169 }
170
171 int perf_evlist__add_default(struct perf_evlist *evlist)
172 {
173         struct perf_event_attr attr = {
174                 .type = PERF_TYPE_HARDWARE,
175                 .config = PERF_COUNT_HW_CPU_CYCLES,
176         };
177         struct perf_evsel *evsel;
178
179         event_attr_init(&attr);
180
181         evsel = perf_evsel__new(&attr);
182         if (evsel == NULL)
183                 goto error;
184
185         /* use strdup() because free(evsel) assumes name is allocated */
186         evsel->name = strdup("cycles");
187         if (!evsel->name)
188                 goto error_free;
189
190         perf_evlist__add(evlist, evsel);
191         return 0;
192 error_free:
193         perf_evsel__delete(evsel);
194 error:
195         return -ENOMEM;
196 }
197
198 static int perf_evlist__add_attrs(struct perf_evlist *evlist,
199                                   struct perf_event_attr *attrs, size_t nr_attrs)
200 {
201         struct perf_evsel *evsel, *n;
202         LIST_HEAD(head);
203         size_t i;
204
205         for (i = 0; i < nr_attrs; i++) {
206                 evsel = perf_evsel__new_idx(attrs + i, evlist->nr_entries + i);
207                 if (evsel == NULL)
208                         goto out_delete_partial_list;
209                 list_add_tail(&evsel->node, &head);
210         }
211
212         perf_evlist__splice_list_tail(evlist, &head);
213
214         return 0;
215
216 out_delete_partial_list:
217         __evlist__for_each_safe(&head, n, evsel)
218                 perf_evsel__delete(evsel);
219         return -1;
220 }
221
222 int __perf_evlist__add_default_attrs(struct perf_evlist *evlist,
223                                      struct perf_event_attr *attrs, size_t nr_attrs)
224 {
225         size_t i;
226
227         for (i = 0; i < nr_attrs; i++)
228                 event_attr_init(attrs + i);
229
230         return perf_evlist__add_attrs(evlist, attrs, nr_attrs);
231 }
232
233 struct perf_evsel *
234 perf_evlist__find_tracepoint_by_id(struct perf_evlist *evlist, int id)
235 {
236         struct perf_evsel *evsel;
237
238         evlist__for_each(evlist, evsel) {
239                 if (evsel->attr.type   == PERF_TYPE_TRACEPOINT &&
240                     (int)evsel->attr.config == id)
241                         return evsel;
242         }
243
244         return NULL;
245 }
246
247 struct perf_evsel *
248 perf_evlist__find_tracepoint_by_name(struct perf_evlist *evlist,
249                                      const char *name)
250 {
251         struct perf_evsel *evsel;
252
253         evlist__for_each(evlist, evsel) {
254                 if ((evsel->attr.type == PERF_TYPE_TRACEPOINT) &&
255                     (strcmp(evsel->name, name) == 0))
256                         return evsel;
257         }
258
259         return NULL;
260 }
261
262 int perf_evlist__add_newtp(struct perf_evlist *evlist,
263                            const char *sys, const char *name, void *handler)
264 {
265         struct perf_evsel *evsel = perf_evsel__newtp(sys, name);
266
267         if (evsel == NULL)
268                 return -1;
269
270         evsel->handler = handler;
271         perf_evlist__add(evlist, evsel);
272         return 0;
273 }
274
275 static int perf_evlist__nr_threads(struct perf_evlist *evlist,
276                                    struct perf_evsel *evsel)
277 {
278         if (evsel->system_wide)
279                 return 1;
280         else
281                 return thread_map__nr(evlist->threads);
282 }
283
284 void perf_evlist__disable(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;
290
291         for (cpu = 0; cpu < nr_cpus; cpu++) {
292                 evlist__for_each(evlist, pos) {
293                         if (!perf_evsel__is_group_leader(pos) || !pos->fd)
294                                 continue;
295                         nr_threads = perf_evlist__nr_threads(evlist, pos);
296                         for (thread = 0; thread < nr_threads; thread++)
297                                 ioctl(FD(pos, cpu, thread),
298                                       PERF_EVENT_IOC_DISABLE, 0);
299                 }
300         }
301
302         evlist->enabled = false;
303 }
304
305 void perf_evlist__enable(struct perf_evlist *evlist)
306 {
307         int cpu, thread;
308         struct perf_evsel *pos;
309         int nr_cpus = cpu_map__nr(evlist->cpus);
310         int nr_threads;
311
312         for (cpu = 0; cpu < nr_cpus; cpu++) {
313                 evlist__for_each(evlist, pos) {
314                         if (!perf_evsel__is_group_leader(pos) || !pos->fd)
315                                 continue;
316                         nr_threads = perf_evlist__nr_threads(evlist, pos);
317                         for (thread = 0; thread < nr_threads; thread++)
318                                 ioctl(FD(pos, cpu, thread),
319                                       PERF_EVENT_IOC_ENABLE, 0);
320                 }
321         }
322
323         evlist->enabled = true;
324 }
325
326 void perf_evlist__toggle_enable(struct perf_evlist *evlist)
327 {
328         (evlist->enabled ? perf_evlist__disable : perf_evlist__enable)(evlist);
329 }
330
331 int perf_evlist__disable_event(struct perf_evlist *evlist,
332                                struct perf_evsel *evsel)
333 {
334         int cpu, thread, err;
335         int nr_cpus = cpu_map__nr(evlist->cpus);
336         int nr_threads = perf_evlist__nr_threads(evlist, evsel);
337
338         if (!evsel->fd)
339                 return 0;
340
341         for (cpu = 0; cpu < nr_cpus; cpu++) {
342                 for (thread = 0; thread < nr_threads; thread++) {
343                         err = ioctl(FD(evsel, cpu, thread),
344                                     PERF_EVENT_IOC_DISABLE, 0);
345                         if (err)
346                                 return err;
347                 }
348         }
349         return 0;
350 }
351
352 int perf_evlist__enable_event(struct perf_evlist *evlist,
353                               struct perf_evsel *evsel)
354 {
355         int cpu, thread, err;
356         int nr_cpus = cpu_map__nr(evlist->cpus);
357         int nr_threads = perf_evlist__nr_threads(evlist, evsel);
358
359         if (!evsel->fd)
360                 return -EINVAL;
361
362         for (cpu = 0; cpu < nr_cpus; cpu++) {
363                 for (thread = 0; thread < nr_threads; thread++) {
364                         err = ioctl(FD(evsel, cpu, thread),
365                                     PERF_EVENT_IOC_ENABLE, 0);
366                         if (err)
367                                 return err;
368                 }
369         }
370         return 0;
371 }
372
373 static int perf_evlist__enable_event_cpu(struct perf_evlist *evlist,
374                                          struct perf_evsel *evsel, int cpu)
375 {
376         int thread, err;
377         int nr_threads = perf_evlist__nr_threads(evlist, evsel);
378
379         if (!evsel->fd)
380                 return -EINVAL;
381
382         for (thread = 0; thread < nr_threads; thread++) {
383                 err = ioctl(FD(evsel, cpu, thread),
384                             PERF_EVENT_IOC_ENABLE, 0);
385                 if (err)
386                         return err;
387         }
388         return 0;
389 }
390
391 static int perf_evlist__enable_event_thread(struct perf_evlist *evlist,
392                                             struct perf_evsel *evsel,
393                                             int thread)
394 {
395         int cpu, err;
396         int nr_cpus = cpu_map__nr(evlist->cpus);
397
398         if (!evsel->fd)
399                 return -EINVAL;
400
401         for (cpu = 0; cpu < nr_cpus; cpu++) {
402                 err = ioctl(FD(evsel, cpu, thread), PERF_EVENT_IOC_ENABLE, 0);
403                 if (err)
404                         return err;
405         }
406         return 0;
407 }
408
409 int perf_evlist__enable_event_idx(struct perf_evlist *evlist,
410                                   struct perf_evsel *evsel, int idx)
411 {
412         bool per_cpu_mmaps = !cpu_map__empty(evlist->cpus);
413
414         if (per_cpu_mmaps)
415                 return perf_evlist__enable_event_cpu(evlist, evsel, idx);
416         else
417                 return perf_evlist__enable_event_thread(evlist, evsel, idx);
418 }
419
420 int perf_evlist__alloc_pollfd(struct perf_evlist *evlist)
421 {
422         int nr_cpus = cpu_map__nr(evlist->cpus);
423         int nr_threads = thread_map__nr(evlist->threads);
424         int nfds = 0;
425         struct perf_evsel *evsel;
426
427         evlist__for_each(evlist, evsel) {
428                 if (evsel->system_wide)
429                         nfds += nr_cpus;
430                 else
431                         nfds += nr_cpus * nr_threads;
432         }
433
434         if (fdarray__available_entries(&evlist->pollfd) < nfds &&
435             fdarray__grow(&evlist->pollfd, nfds) < 0)
436                 return -ENOMEM;
437
438         return 0;
439 }
440
441 static int __perf_evlist__add_pollfd(struct perf_evlist *evlist, int fd, int idx)
442 {
443         int pos = fdarray__add(&evlist->pollfd, fd, POLLIN | POLLERR | POLLHUP);
444         /*
445          * Save the idx so that when we filter out fds POLLHUP'ed we can
446          * close the associated evlist->mmap[] entry.
447          */
448         if (pos >= 0) {
449                 evlist->pollfd.priv[pos].idx = idx;
450
451                 fcntl(fd, F_SETFL, O_NONBLOCK);
452         }
453
454         return pos;
455 }
456
457 int perf_evlist__add_pollfd(struct perf_evlist *evlist, int fd)
458 {
459         return __perf_evlist__add_pollfd(evlist, fd, -1);
460 }
461
462 static void perf_evlist__munmap_filtered(struct fdarray *fda, int fd)
463 {
464         struct perf_evlist *evlist = container_of(fda, struct perf_evlist, pollfd);
465
466         perf_evlist__mmap_put(evlist, fda->priv[fd].idx);
467 }
468
469 int perf_evlist__filter_pollfd(struct perf_evlist *evlist, short revents_and_mask)
470 {
471         return fdarray__filter(&evlist->pollfd, revents_and_mask,
472                                perf_evlist__munmap_filtered);
473 }
474
475 int perf_evlist__poll(struct perf_evlist *evlist, int timeout)
476 {
477         return fdarray__poll(&evlist->pollfd, timeout);
478 }
479
480 static void perf_evlist__id_hash(struct perf_evlist *evlist,
481                                  struct perf_evsel *evsel,
482                                  int cpu, int thread, u64 id)
483 {
484         int hash;
485         struct perf_sample_id *sid = SID(evsel, cpu, thread);
486
487         sid->id = id;
488         sid->evsel = evsel;
489         hash = hash_64(sid->id, PERF_EVLIST__HLIST_BITS);
490         hlist_add_head(&sid->node, &evlist->heads[hash]);
491 }
492
493 void perf_evlist__id_add(struct perf_evlist *evlist, struct perf_evsel *evsel,
494                          int cpu, int thread, u64 id)
495 {
496         perf_evlist__id_hash(evlist, evsel, cpu, thread, id);
497         evsel->id[evsel->ids++] = id;
498 }
499
500 static int perf_evlist__id_add_fd(struct perf_evlist *evlist,
501                                   struct perf_evsel *evsel,
502                                   int cpu, int thread, int fd)
503 {
504         u64 read_data[4] = { 0, };
505         int id_idx = 1; /* The first entry is the counter value */
506         u64 id;
507         int ret;
508
509         ret = ioctl(fd, PERF_EVENT_IOC_ID, &id);
510         if (!ret)
511                 goto add;
512
513         if (errno != ENOTTY)
514                 return -1;
515
516         /* Legacy way to get event id.. All hail to old kernels! */
517
518         /*
519          * This way does not work with group format read, so bail
520          * out in that case.
521          */
522         if (perf_evlist__read_format(evlist) & PERF_FORMAT_GROUP)
523                 return -1;
524
525         if (!(evsel->attr.read_format & PERF_FORMAT_ID) ||
526             read(fd, &read_data, sizeof(read_data)) == -1)
527                 return -1;
528
529         if (evsel->attr.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
530                 ++id_idx;
531         if (evsel->attr.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
532                 ++id_idx;
533
534         id = read_data[id_idx];
535
536  add:
537         perf_evlist__id_add(evlist, evsel, cpu, thread, id);
538         return 0;
539 }
540
541 static void perf_evlist__set_sid_idx(struct perf_evlist *evlist,
542                                      struct perf_evsel *evsel, int idx, int cpu,
543                                      int thread)
544 {
545         struct perf_sample_id *sid = SID(evsel, cpu, thread);
546         sid->idx = idx;
547         if (evlist->cpus && cpu >= 0)
548                 sid->cpu = evlist->cpus->map[cpu];
549         else
550                 sid->cpu = -1;
551         if (!evsel->system_wide && evlist->threads && thread >= 0)
552                 sid->tid = thread_map__pid(evlist->threads, thread);
553         else
554                 sid->tid = -1;
555 }
556
557 struct perf_sample_id *perf_evlist__id2sid(struct perf_evlist *evlist, u64 id)
558 {
559         struct hlist_head *head;
560         struct perf_sample_id *sid;
561         int hash;
562
563         hash = hash_64(id, PERF_EVLIST__HLIST_BITS);
564         head = &evlist->heads[hash];
565
566         hlist_for_each_entry(sid, head, node)
567                 if (sid->id == id)
568                         return sid;
569
570         return NULL;
571 }
572
573 struct perf_evsel *perf_evlist__id2evsel(struct perf_evlist *evlist, u64 id)
574 {
575         struct perf_sample_id *sid;
576
577         if (evlist->nr_entries == 1 || !id)
578                 return perf_evlist__first(evlist);
579
580         sid = perf_evlist__id2sid(evlist, id);
581         if (sid)
582                 return sid->evsel;
583
584         if (!perf_evlist__sample_id_all(evlist))
585                 return perf_evlist__first(evlist);
586
587         return NULL;
588 }
589
590 static int perf_evlist__event2id(struct perf_evlist *evlist,
591                                  union perf_event *event, u64 *id)
592 {
593         const u64 *array = event->sample.array;
594         ssize_t n;
595
596         n = (event->header.size - sizeof(event->header)) >> 3;
597
598         if (event->header.type == PERF_RECORD_SAMPLE) {
599                 if (evlist->id_pos >= n)
600                         return -1;
601                 *id = array[evlist->id_pos];
602         } else {
603                 if (evlist->is_pos > n)
604                         return -1;
605                 n -= evlist->is_pos;
606                 *id = array[n];
607         }
608         return 0;
609 }
610
611 static struct perf_evsel *perf_evlist__event2evsel(struct perf_evlist *evlist,
612                                                    union perf_event *event)
613 {
614         struct perf_evsel *first = perf_evlist__first(evlist);
615         struct hlist_head *head;
616         struct perf_sample_id *sid;
617         int hash;
618         u64 id;
619
620         if (evlist->nr_entries == 1)
621                 return first;
622
623         if (!first->attr.sample_id_all &&
624             event->header.type != PERF_RECORD_SAMPLE)
625                 return first;
626
627         if (perf_evlist__event2id(evlist, event, &id))
628                 return NULL;
629
630         /* Synthesized events have an id of zero */
631         if (!id)
632                 return first;
633
634         hash = hash_64(id, PERF_EVLIST__HLIST_BITS);
635         head = &evlist->heads[hash];
636
637         hlist_for_each_entry(sid, head, node) {
638                 if (sid->id == id)
639                         return sid->evsel;
640         }
641         return NULL;
642 }
643
644 union perf_event *perf_evlist__mmap_read(struct perf_evlist *evlist, int idx)
645 {
646         struct perf_mmap *md = &evlist->mmap[idx];
647         u64 head;
648         u64 old = md->prev;
649         unsigned char *data = md->base + page_size;
650         union perf_event *event = NULL;
651
652         /*
653          * Check if event was unmapped due to a POLLHUP/POLLERR.
654          */
655         if (!atomic_read(&md->refcnt))
656                 return NULL;
657
658         head = perf_mmap__read_head(md);
659         if (evlist->overwrite) {
660                 /*
661                  * If we're further behind than half the buffer, there's a chance
662                  * the writer will bite our tail and mess up the samples under us.
663                  *
664                  * If we somehow ended up ahead of the head, we got messed up.
665                  *
666                  * In either case, truncate and restart at head.
667                  */
668                 int diff = head - old;
669                 if (diff > md->mask / 2 || diff < 0) {
670                         fprintf(stderr, "WARNING: failed to keep up with mmap data.\n");
671
672                         /*
673                          * head points to a known good entry, start there.
674                          */
675                         old = head;
676                 }
677         }
678
679         if (old != head) {
680                 size_t size;
681
682                 event = (union perf_event *)&data[old & md->mask];
683                 size = event->header.size;
684
685                 /*
686                  * Event straddles the mmap boundary -- header should always
687                  * be inside due to u64 alignment of output.
688                  */
689                 if ((old & md->mask) + size != ((old + size) & md->mask)) {
690                         unsigned int offset = old;
691                         unsigned int len = min(sizeof(*event), size), cpy;
692                         void *dst = md->event_copy;
693
694                         do {
695                                 cpy = min(md->mask + 1 - (offset & md->mask), len);
696                                 memcpy(dst, &data[offset & md->mask], cpy);
697                                 offset += cpy;
698                                 dst += cpy;
699                                 len -= cpy;
700                         } while (len);
701
702                         event = (union perf_event *) md->event_copy;
703                 }
704
705                 old += size;
706         }
707
708         md->prev = old;
709
710         return event;
711 }
712
713 static bool perf_mmap__empty(struct perf_mmap *md)
714 {
715         return perf_mmap__read_head(md) == md->prev && !md->auxtrace_mmap.base;
716 }
717
718 static void perf_evlist__mmap_get(struct perf_evlist *evlist, int idx)
719 {
720         atomic_inc(&evlist->mmap[idx].refcnt);
721 }
722
723 static void perf_evlist__mmap_put(struct perf_evlist *evlist, int idx)
724 {
725         BUG_ON(atomic_read(&evlist->mmap[idx].refcnt) == 0);
726
727         if (atomic_dec_and_test(&evlist->mmap[idx].refcnt))
728                 __perf_evlist__munmap(evlist, idx);
729 }
730
731 void perf_evlist__mmap_consume(struct perf_evlist *evlist, int idx)
732 {
733         struct perf_mmap *md = &evlist->mmap[idx];
734
735         if (!evlist->overwrite) {
736                 u64 old = md->prev;
737
738                 perf_mmap__write_tail(md, old);
739         }
740
741         if (atomic_read(&md->refcnt) == 1 && perf_mmap__empty(md))
742                 perf_evlist__mmap_put(evlist, idx);
743 }
744
745 int __weak auxtrace_mmap__mmap(struct auxtrace_mmap *mm __maybe_unused,
746                                struct auxtrace_mmap_params *mp __maybe_unused,
747                                void *userpg __maybe_unused,
748                                int fd __maybe_unused)
749 {
750         return 0;
751 }
752
753 void __weak auxtrace_mmap__munmap(struct auxtrace_mmap *mm __maybe_unused)
754 {
755 }
756
757 void __weak auxtrace_mmap_params__init(
758                         struct auxtrace_mmap_params *mp __maybe_unused,
759                         off_t auxtrace_offset __maybe_unused,
760                         unsigned int auxtrace_pages __maybe_unused,
761                         bool auxtrace_overwrite __maybe_unused)
762 {
763 }
764
765 void __weak auxtrace_mmap_params__set_idx(
766                         struct auxtrace_mmap_params *mp __maybe_unused,
767                         struct perf_evlist *evlist __maybe_unused,
768                         int idx __maybe_unused,
769                         bool per_cpu __maybe_unused)
770 {
771 }
772
773 static void __perf_evlist__munmap(struct perf_evlist *evlist, int idx)
774 {
775         if (evlist->mmap[idx].base != NULL) {
776                 munmap(evlist->mmap[idx].base, evlist->mmap_len);
777                 evlist->mmap[idx].base = NULL;
778                 atomic_set(&evlist->mmap[idx].refcnt, 0);
779         }
780         auxtrace_mmap__munmap(&evlist->mmap[idx].auxtrace_mmap);
781 }
782
783 void perf_evlist__munmap(struct perf_evlist *evlist)
784 {
785         int i;
786
787         if (evlist->mmap == NULL)
788                 return;
789
790         for (i = 0; i < evlist->nr_mmaps; i++)
791                 __perf_evlist__munmap(evlist, i);
792
793         zfree(&evlist->mmap);
794 }
795
796 static int perf_evlist__alloc_mmap(struct perf_evlist *evlist)
797 {
798         evlist->nr_mmaps = cpu_map__nr(evlist->cpus);
799         if (cpu_map__empty(evlist->cpus))
800                 evlist->nr_mmaps = thread_map__nr(evlist->threads);
801         evlist->mmap = zalloc(evlist->nr_mmaps * sizeof(struct perf_mmap));
802         return evlist->mmap != NULL ? 0 : -ENOMEM;
803 }
804
805 struct mmap_params {
806         int prot;
807         int mask;
808         struct auxtrace_mmap_params auxtrace_mp;
809 };
810
811 static int __perf_evlist__mmap(struct perf_evlist *evlist, int idx,
812                                struct mmap_params *mp, int fd)
813 {
814         /*
815          * The last one will be done at perf_evlist__mmap_consume(), so that we
816          * make sure we don't prevent tools from consuming every last event in
817          * the ring buffer.
818          *
819          * I.e. we can get the POLLHUP meaning that the fd doesn't exist
820          * anymore, but the last events for it are still in the ring buffer,
821          * waiting to be consumed.
822          *
823          * Tools can chose to ignore this at their own discretion, but the
824          * evlist layer can't just drop it when filtering events in
825          * perf_evlist__filter_pollfd().
826          */
827         atomic_set(&evlist->mmap[idx].refcnt, 2);
828         evlist->mmap[idx].prev = 0;
829         evlist->mmap[idx].mask = mp->mask;
830         evlist->mmap[idx].base = mmap(NULL, evlist->mmap_len, mp->prot,
831                                       MAP_SHARED, fd, 0);
832         if (evlist->mmap[idx].base == MAP_FAILED) {
833                 pr_debug2("failed to mmap perf event ring buffer, error %d\n",
834                           errno);
835                 evlist->mmap[idx].base = NULL;
836                 return -1;
837         }
838
839         if (auxtrace_mmap__mmap(&evlist->mmap[idx].auxtrace_mmap,
840                                 &mp->auxtrace_mp, evlist->mmap[idx].base, fd))
841                 return -1;
842
843         return 0;
844 }
845
846 static int perf_evlist__mmap_per_evsel(struct perf_evlist *evlist, int idx,
847                                        struct mmap_params *mp, int cpu,
848                                        int thread, int *output)
849 {
850         struct perf_evsel *evsel;
851
852         evlist__for_each(evlist, evsel) {
853                 int fd;
854
855                 if (evsel->system_wide && thread)
856                         continue;
857
858                 fd = FD(evsel, cpu, thread);
859
860                 if (*output == -1) {
861                         *output = fd;
862                         if (__perf_evlist__mmap(evlist, idx, mp, *output) < 0)
863                                 return -1;
864                 } else {
865                         if (ioctl(fd, PERF_EVENT_IOC_SET_OUTPUT, *output) != 0)
866                                 return -1;
867
868                         perf_evlist__mmap_get(evlist, idx);
869                 }
870
871                 /*
872                  * The system_wide flag causes a selected event to be opened
873                  * always without a pid.  Consequently it will never get a
874                  * POLLHUP, but it is used for tracking in combination with
875                  * other events, so it should not need to be polled anyway.
876                  * Therefore don't add it for polling.
877                  */
878                 if (!evsel->system_wide &&
879                     __perf_evlist__add_pollfd(evlist, fd, idx) < 0) {
880                         perf_evlist__mmap_put(evlist, idx);
881                         return -1;
882                 }
883
884                 if (evsel->attr.read_format & PERF_FORMAT_ID) {
885                         if (perf_evlist__id_add_fd(evlist, evsel, cpu, thread,
886                                                    fd) < 0)
887                                 return -1;
888                         perf_evlist__set_sid_idx(evlist, evsel, idx, cpu,
889                                                  thread);
890                 }
891         }
892
893         return 0;
894 }
895
896 static int perf_evlist__mmap_per_cpu(struct perf_evlist *evlist,
897                                      struct mmap_params *mp)
898 {
899         int cpu, thread;
900         int nr_cpus = cpu_map__nr(evlist->cpus);
901         int nr_threads = thread_map__nr(evlist->threads);
902
903         pr_debug2("perf event ring buffer mmapped per cpu\n");
904         for (cpu = 0; cpu < nr_cpus; cpu++) {
905                 int output = -1;
906
907                 auxtrace_mmap_params__set_idx(&mp->auxtrace_mp, evlist, cpu,
908                                               true);
909
910                 for (thread = 0; thread < nr_threads; thread++) {
911                         if (perf_evlist__mmap_per_evsel(evlist, cpu, mp, cpu,
912                                                         thread, &output))
913                                 goto out_unmap;
914                 }
915         }
916
917         return 0;
918
919 out_unmap:
920         for (cpu = 0; cpu < nr_cpus; cpu++)
921                 __perf_evlist__munmap(evlist, cpu);
922         return -1;
923 }
924
925 static int perf_evlist__mmap_per_thread(struct perf_evlist *evlist,
926                                         struct mmap_params *mp)
927 {
928         int thread;
929         int nr_threads = thread_map__nr(evlist->threads);
930
931         pr_debug2("perf event ring buffer mmapped per thread\n");
932         for (thread = 0; thread < nr_threads; thread++) {
933                 int output = -1;
934
935                 auxtrace_mmap_params__set_idx(&mp->auxtrace_mp, evlist, thread,
936                                               false);
937
938                 if (perf_evlist__mmap_per_evsel(evlist, thread, mp, 0, thread,
939                                                 &output))
940                         goto out_unmap;
941         }
942
943         return 0;
944
945 out_unmap:
946         for (thread = 0; thread < nr_threads; thread++)
947                 __perf_evlist__munmap(evlist, thread);
948         return -1;
949 }
950
951 static size_t perf_evlist__mmap_size(unsigned long pages)
952 {
953         if (pages == UINT_MAX) {
954                 int max;
955
956                 if (sysctl__read_int("kernel/perf_event_mlock_kb", &max) < 0) {
957                         /*
958                          * Pick a once upon a time good value, i.e. things look
959                          * strange since we can't read a sysctl value, but lets not
960                          * die yet...
961                          */
962                         max = 512;
963                 } else {
964                         max -= (page_size / 1024);
965                 }
966
967                 pages = (max * 1024) / page_size;
968                 if (!is_power_of_2(pages))
969                         pages = rounddown_pow_of_two(pages);
970         } else if (!is_power_of_2(pages))
971                 return 0;
972
973         return (pages + 1) * page_size;
974 }
975
976 static long parse_pages_arg(const char *str, unsigned long min,
977                             unsigned long max)
978 {
979         unsigned long pages, val;
980         static struct parse_tag tags[] = {
981                 { .tag  = 'B', .mult = 1       },
982                 { .tag  = 'K', .mult = 1 << 10 },
983                 { .tag  = 'M', .mult = 1 << 20 },
984                 { .tag  = 'G', .mult = 1 << 30 },
985                 { .tag  = 0 },
986         };
987
988         if (str == NULL)
989                 return -EINVAL;
990
991         val = parse_tag_value(str, tags);
992         if (val != (unsigned long) -1) {
993                 /* we got file size value */
994                 pages = PERF_ALIGN(val, page_size) / page_size;
995         } else {
996                 /* we got pages count value */
997                 char *eptr;
998                 pages = strtoul(str, &eptr, 10);
999                 if (*eptr != '\0')
1000                         return -EINVAL;
1001         }
1002
1003         if (pages == 0 && min == 0) {
1004                 /* leave number of pages at 0 */
1005         } else if (!is_power_of_2(pages)) {
1006                 /* round pages up to next power of 2 */
1007                 pages = roundup_pow_of_two(pages);
1008                 if (!pages)
1009                         return -EINVAL;
1010                 pr_info("rounding mmap pages size to %lu bytes (%lu pages)\n",
1011                         pages * page_size, pages);
1012         }
1013
1014         if (pages > max)
1015                 return -EINVAL;
1016
1017         return pages;
1018 }
1019
1020 int __perf_evlist__parse_mmap_pages(unsigned int *mmap_pages, const char *str)
1021 {
1022         unsigned long max = UINT_MAX;
1023         long pages;
1024
1025         if (max > SIZE_MAX / page_size)
1026                 max = SIZE_MAX / page_size;
1027
1028         pages = parse_pages_arg(str, 1, max);
1029         if (pages < 0) {
1030                 pr_err("Invalid argument for --mmap_pages/-m\n");
1031                 return -1;
1032         }
1033
1034         *mmap_pages = pages;
1035         return 0;
1036 }
1037
1038 int perf_evlist__parse_mmap_pages(const struct option *opt, const char *str,
1039                                   int unset __maybe_unused)
1040 {
1041         return __perf_evlist__parse_mmap_pages(opt->value, str);
1042 }
1043
1044 /**
1045  * perf_evlist__mmap_ex - Create mmaps to receive events.
1046  * @evlist: list of events
1047  * @pages: map length in pages
1048  * @overwrite: overwrite older events?
1049  * @auxtrace_pages - auxtrace map length in pages
1050  * @auxtrace_overwrite - overwrite older auxtrace data?
1051  *
1052  * If @overwrite is %false the user needs to signal event consumption using
1053  * perf_mmap__write_tail().  Using perf_evlist__mmap_read() does this
1054  * automatically.
1055  *
1056  * Similarly, if @auxtrace_overwrite is %false the user needs to signal data
1057  * consumption using auxtrace_mmap__write_tail().
1058  *
1059  * Return: %0 on success, negative error code otherwise.
1060  */
1061 int perf_evlist__mmap_ex(struct perf_evlist *evlist, unsigned int pages,
1062                          bool overwrite, unsigned int auxtrace_pages,
1063                          bool auxtrace_overwrite)
1064 {
1065         struct perf_evsel *evsel;
1066         const struct cpu_map *cpus = evlist->cpus;
1067         const struct thread_map *threads = evlist->threads;
1068         struct mmap_params mp = {
1069                 .prot = PROT_READ | (overwrite ? 0 : PROT_WRITE),
1070         };
1071
1072         if (evlist->mmap == NULL && perf_evlist__alloc_mmap(evlist) < 0)
1073                 return -ENOMEM;
1074
1075         if (evlist->pollfd.entries == NULL && perf_evlist__alloc_pollfd(evlist) < 0)
1076                 return -ENOMEM;
1077
1078         evlist->overwrite = overwrite;
1079         evlist->mmap_len = perf_evlist__mmap_size(pages);
1080         pr_debug("mmap size %zuB\n", evlist->mmap_len);
1081         mp.mask = evlist->mmap_len - page_size - 1;
1082
1083         auxtrace_mmap_params__init(&mp.auxtrace_mp, evlist->mmap_len,
1084                                    auxtrace_pages, auxtrace_overwrite);
1085
1086         evlist__for_each(evlist, evsel) {
1087                 if ((evsel->attr.read_format & PERF_FORMAT_ID) &&
1088                     evsel->sample_id == NULL &&
1089                     perf_evsel__alloc_id(evsel, cpu_map__nr(cpus), threads->nr) < 0)
1090                         return -ENOMEM;
1091         }
1092
1093         if (cpu_map__empty(cpus))
1094                 return perf_evlist__mmap_per_thread(evlist, &mp);
1095
1096         return perf_evlist__mmap_per_cpu(evlist, &mp);
1097 }
1098
1099 int perf_evlist__mmap(struct perf_evlist *evlist, unsigned int pages,
1100                       bool overwrite)
1101 {
1102         return perf_evlist__mmap_ex(evlist, pages, overwrite, 0, false);
1103 }
1104
1105 static void perf_evlist__propagate_maps(struct perf_evlist *evlist)
1106 {
1107         struct perf_evsel *evsel;
1108
1109         evlist__for_each(evlist, evsel) {
1110                 /*
1111                  * We already have cpus for evsel (via PMU sysfs) so
1112                  * keep it, if there's no target cpu list defined.
1113                  */
1114                 if (!evsel->own_cpus || evlist->has_user_cpus) {
1115                         cpu_map__put(evsel->cpus);
1116                         evsel->cpus = cpu_map__get(evlist->cpus);
1117                 } else if (evsel->cpus != evsel->own_cpus) {
1118                         cpu_map__put(evsel->cpus);
1119                         evsel->cpus = cpu_map__get(evsel->own_cpus);
1120                 }
1121
1122                 thread_map__put(evsel->threads);
1123                 evsel->threads = thread_map__get(evlist->threads);
1124         }
1125 }
1126
1127 int perf_evlist__create_maps(struct perf_evlist *evlist, struct target *target)
1128 {
1129         evlist->threads = thread_map__new_str(target->pid, target->tid,
1130                                               target->uid);
1131
1132         if (evlist->threads == NULL)
1133                 return -1;
1134
1135         if (target__uses_dummy_map(target))
1136                 evlist->cpus = cpu_map__dummy_new();
1137         else
1138                 evlist->cpus = cpu_map__new(target->cpu_list);
1139
1140         if (evlist->cpus == NULL)
1141                 goto out_delete_threads;
1142
1143         evlist->has_user_cpus = !!target->cpu_list;
1144
1145         perf_evlist__propagate_maps(evlist);
1146
1147         return 0;
1148
1149 out_delete_threads:
1150         thread_map__put(evlist->threads);
1151         evlist->threads = NULL;
1152         return -1;
1153 }
1154
1155 void perf_evlist__set_maps(struct perf_evlist *evlist, struct cpu_map *cpus,
1156                            struct thread_map *threads)
1157 {
1158         /*
1159          * Allow for the possibility that one or another of the maps isn't being
1160          * changed i.e. don't put it.  Note we are assuming the maps that are
1161          * being applied are brand new and evlist is taking ownership of the
1162          * original reference count of 1.  If that is not the case it is up to
1163          * the caller to increase the reference count.
1164          */
1165         if (cpus != evlist->cpus) {
1166                 cpu_map__put(evlist->cpus);
1167                 evlist->cpus = cpus;
1168         }
1169
1170         if (threads != evlist->threads) {
1171                 thread_map__put(evlist->threads);
1172                 evlist->threads = threads;
1173         }
1174
1175         perf_evlist__propagate_maps(evlist);
1176 }
1177
1178 int perf_evlist__apply_filters(struct perf_evlist *evlist, struct perf_evsel **err_evsel)
1179 {
1180         struct perf_evsel *evsel;
1181         int err = 0;
1182         const int ncpus = cpu_map__nr(evlist->cpus),
1183                   nthreads = thread_map__nr(evlist->threads);
1184
1185         evlist__for_each(evlist, evsel) {
1186                 if (evsel->filter == NULL)
1187                         continue;
1188
1189                 /*
1190                  * filters only work for tracepoint event, which doesn't have cpu limit.
1191                  * So evlist and evsel should always be same.
1192                  */
1193                 err = perf_evsel__apply_filter(evsel, ncpus, nthreads, evsel->filter);
1194                 if (err) {
1195                         *err_evsel = evsel;
1196                         break;
1197                 }
1198         }
1199
1200         return err;
1201 }
1202
1203 int perf_evlist__set_filter(struct perf_evlist *evlist, const char *filter)
1204 {
1205         struct perf_evsel *evsel;
1206         int err = 0;
1207
1208         evlist__for_each(evlist, evsel) {
1209                 err = perf_evsel__set_filter(evsel, filter);
1210                 if (err)
1211                         break;
1212         }
1213
1214         return err;
1215 }
1216
1217 int perf_evlist__set_filter_pids(struct perf_evlist *evlist, size_t npids, pid_t *pids)
1218 {
1219         char *filter;
1220         int ret = -1;
1221         size_t i;
1222
1223         for (i = 0; i < npids; ++i) {
1224                 if (i == 0) {
1225                         if (asprintf(&filter, "common_pid != %d", pids[i]) < 0)
1226                                 return -1;
1227                 } else {
1228                         char *tmp;
1229
1230                         if (asprintf(&tmp, "%s && common_pid != %d", filter, pids[i]) < 0)
1231                                 goto out_free;
1232
1233                         free(filter);
1234                         filter = tmp;
1235                 }
1236         }
1237
1238         ret = perf_evlist__set_filter(evlist, filter);
1239 out_free:
1240         free(filter);
1241         return ret;
1242 }
1243
1244 int perf_evlist__set_filter_pid(struct perf_evlist *evlist, pid_t pid)
1245 {
1246         return perf_evlist__set_filter_pids(evlist, 1, &pid);
1247 }
1248
1249 bool perf_evlist__valid_sample_type(struct perf_evlist *evlist)
1250 {
1251         struct perf_evsel *pos;
1252
1253         if (evlist->nr_entries == 1)
1254                 return true;
1255
1256         if (evlist->id_pos < 0 || evlist->is_pos < 0)
1257                 return false;
1258
1259         evlist__for_each(evlist, pos) {
1260                 if (pos->id_pos != evlist->id_pos ||
1261                     pos->is_pos != evlist->is_pos)
1262                         return false;
1263         }
1264
1265         return true;
1266 }
1267
1268 u64 __perf_evlist__combined_sample_type(struct perf_evlist *evlist)
1269 {
1270         struct perf_evsel *evsel;
1271
1272         if (evlist->combined_sample_type)
1273                 return evlist->combined_sample_type;
1274
1275         evlist__for_each(evlist, evsel)
1276                 evlist->combined_sample_type |= evsel->attr.sample_type;
1277
1278         return evlist->combined_sample_type;
1279 }
1280
1281 u64 perf_evlist__combined_sample_type(struct perf_evlist *evlist)
1282 {
1283         evlist->combined_sample_type = 0;
1284         return __perf_evlist__combined_sample_type(evlist);
1285 }
1286
1287 u64 perf_evlist__combined_branch_type(struct perf_evlist *evlist)
1288 {
1289         struct perf_evsel *evsel;
1290         u64 branch_type = 0;
1291
1292         evlist__for_each(evlist, evsel)
1293                 branch_type |= evsel->attr.branch_sample_type;
1294         return branch_type;
1295 }
1296
1297 bool perf_evlist__valid_read_format(struct perf_evlist *evlist)
1298 {
1299         struct perf_evsel *first = perf_evlist__first(evlist), *pos = first;
1300         u64 read_format = first->attr.read_format;
1301         u64 sample_type = first->attr.sample_type;
1302
1303         evlist__for_each(evlist, pos) {
1304                 if (read_format != pos->attr.read_format)
1305                         return false;
1306         }
1307
1308         /* PERF_SAMPLE_READ imples PERF_FORMAT_ID. */
1309         if ((sample_type & PERF_SAMPLE_READ) &&
1310             !(read_format & PERF_FORMAT_ID)) {
1311                 return false;
1312         }
1313
1314         return true;
1315 }
1316
1317 u64 perf_evlist__read_format(struct perf_evlist *evlist)
1318 {
1319         struct perf_evsel *first = perf_evlist__first(evlist);
1320         return first->attr.read_format;
1321 }
1322
1323 u16 perf_evlist__id_hdr_size(struct perf_evlist *evlist)
1324 {
1325         struct perf_evsel *first = perf_evlist__first(evlist);
1326         struct perf_sample *data;
1327         u64 sample_type;
1328         u16 size = 0;
1329
1330         if (!first->attr.sample_id_all)
1331                 goto out;
1332
1333         sample_type = first->attr.sample_type;
1334
1335         if (sample_type & PERF_SAMPLE_TID)
1336                 size += sizeof(data->tid) * 2;
1337
1338        if (sample_type & PERF_SAMPLE_TIME)
1339                 size += sizeof(data->time);
1340
1341         if (sample_type & PERF_SAMPLE_ID)
1342                 size += sizeof(data->id);
1343
1344         if (sample_type & PERF_SAMPLE_STREAM_ID)
1345                 size += sizeof(data->stream_id);
1346
1347         if (sample_type & PERF_SAMPLE_CPU)
1348                 size += sizeof(data->cpu) * 2;
1349
1350         if (sample_type & PERF_SAMPLE_IDENTIFIER)
1351                 size += sizeof(data->id);
1352 out:
1353         return size;
1354 }
1355
1356 bool perf_evlist__valid_sample_id_all(struct perf_evlist *evlist)
1357 {
1358         struct perf_evsel *first = perf_evlist__first(evlist), *pos = first;
1359
1360         evlist__for_each_continue(evlist, pos) {
1361                 if (first->attr.sample_id_all != pos->attr.sample_id_all)
1362                         return false;
1363         }
1364
1365         return true;
1366 }
1367
1368 bool perf_evlist__sample_id_all(struct perf_evlist *evlist)
1369 {
1370         struct perf_evsel *first = perf_evlist__first(evlist);
1371         return first->attr.sample_id_all;
1372 }
1373
1374 void perf_evlist__set_selected(struct perf_evlist *evlist,
1375                                struct perf_evsel *evsel)
1376 {
1377         evlist->selected = evsel;
1378 }
1379
1380 void perf_evlist__close(struct perf_evlist *evlist)
1381 {
1382         struct perf_evsel *evsel;
1383         int ncpus = cpu_map__nr(evlist->cpus);
1384         int nthreads = thread_map__nr(evlist->threads);
1385         int n;
1386
1387         evlist__for_each_reverse(evlist, evsel) {
1388                 n = evsel->cpus ? evsel->cpus->nr : ncpus;
1389                 perf_evsel__close(evsel, n, nthreads);
1390         }
1391 }
1392
1393 static int perf_evlist__create_syswide_maps(struct perf_evlist *evlist)
1394 {
1395         int err = -ENOMEM;
1396
1397         /*
1398          * Try reading /sys/devices/system/cpu/online to get
1399          * an all cpus map.
1400          *
1401          * FIXME: -ENOMEM is the best we can do here, the cpu_map
1402          * code needs an overhaul to properly forward the
1403          * error, and we may not want to do that fallback to a
1404          * default cpu identity map :-\
1405          */
1406         evlist->cpus = cpu_map__new(NULL);
1407         if (evlist->cpus == NULL)
1408                 goto out;
1409
1410         evlist->threads = thread_map__new_dummy();
1411         if (evlist->threads == NULL)
1412                 goto out_free_cpus;
1413
1414         err = 0;
1415 out:
1416         return err;
1417 out_free_cpus:
1418         cpu_map__put(evlist->cpus);
1419         evlist->cpus = NULL;
1420         goto out;
1421 }
1422
1423 int perf_evlist__open(struct perf_evlist *evlist)
1424 {
1425         struct perf_evsel *evsel;
1426         int err;
1427
1428         /*
1429          * Default: one fd per CPU, all threads, aka systemwide
1430          * as sys_perf_event_open(cpu = -1, thread = -1) is EINVAL
1431          */
1432         if (evlist->threads == NULL && evlist->cpus == NULL) {
1433                 err = perf_evlist__create_syswide_maps(evlist);
1434                 if (err < 0)
1435                         goto out_err;
1436         }
1437
1438         perf_evlist__update_id_pos(evlist);
1439
1440         evlist__for_each(evlist, evsel) {
1441                 err = perf_evsel__open(evsel, evlist->cpus, evlist->threads);
1442                 if (err < 0)
1443                         goto out_err;
1444         }
1445
1446         return 0;
1447 out_err:
1448         perf_evlist__close(evlist);
1449         errno = -err;
1450         return err;
1451 }
1452
1453 int perf_evlist__prepare_workload(struct perf_evlist *evlist, struct target *target,
1454                                   const char *argv[], bool pipe_output,
1455                                   void (*exec_error)(int signo, siginfo_t *info, void *ucontext))
1456 {
1457         int child_ready_pipe[2], go_pipe[2];
1458         char bf;
1459
1460         if (pipe(child_ready_pipe) < 0) {
1461                 perror("failed to create 'ready' pipe");
1462                 return -1;
1463         }
1464
1465         if (pipe(go_pipe) < 0) {
1466                 perror("failed to create 'go' pipe");
1467                 goto out_close_ready_pipe;
1468         }
1469
1470         evlist->workload.pid = fork();
1471         if (evlist->workload.pid < 0) {
1472                 perror("failed to fork");
1473                 goto out_close_pipes;
1474         }
1475
1476         if (!evlist->workload.pid) {
1477                 int ret;
1478
1479                 if (pipe_output)
1480                         dup2(2, 1);
1481
1482                 signal(SIGTERM, SIG_DFL);
1483
1484                 close(child_ready_pipe[0]);
1485                 close(go_pipe[1]);
1486                 fcntl(go_pipe[0], F_SETFD, FD_CLOEXEC);
1487
1488                 /*
1489                  * Tell the parent we're ready to go
1490                  */
1491                 close(child_ready_pipe[1]);
1492
1493                 /*
1494                  * Wait until the parent tells us to go.
1495                  */
1496                 ret = read(go_pipe[0], &bf, 1);
1497                 /*
1498                  * The parent will ask for the execvp() to be performed by
1499                  * writing exactly one byte, in workload.cork_fd, usually via
1500                  * perf_evlist__start_workload().
1501                  *
1502                  * For cancelling the workload without actually running it,
1503                  * the parent will just close workload.cork_fd, without writing
1504                  * anything, i.e. read will return zero and we just exit()
1505                  * here.
1506                  */
1507                 if (ret != 1) {
1508                         if (ret == -1)
1509                                 perror("unable to read pipe");
1510                         exit(ret);
1511                 }
1512
1513                 execvp(argv[0], (char **)argv);
1514
1515                 if (exec_error) {
1516                         union sigval val;
1517
1518                         val.sival_int = errno;
1519                         if (sigqueue(getppid(), SIGUSR1, val))
1520                                 perror(argv[0]);
1521                 } else
1522                         perror(argv[0]);
1523                 exit(-1);
1524         }
1525
1526         if (exec_error) {
1527                 struct sigaction act = {
1528                         .sa_flags     = SA_SIGINFO,
1529                         .sa_sigaction = exec_error,
1530                 };
1531                 sigaction(SIGUSR1, &act, NULL);
1532         }
1533
1534         if (target__none(target)) {
1535                 if (evlist->threads == NULL) {
1536                         fprintf(stderr, "FATAL: evlist->threads need to be set at this point (%s:%d).\n",
1537                                 __func__, __LINE__);
1538                         goto out_close_pipes;
1539                 }
1540                 thread_map__set_pid(evlist->threads, 0, evlist->workload.pid);
1541         }
1542
1543         close(child_ready_pipe[1]);
1544         close(go_pipe[0]);
1545         /*
1546          * wait for child to settle
1547          */
1548         if (read(child_ready_pipe[0], &bf, 1) == -1) {
1549                 perror("unable to read pipe");
1550                 goto out_close_pipes;
1551         }
1552
1553         fcntl(go_pipe[1], F_SETFD, FD_CLOEXEC);
1554         evlist->workload.cork_fd = go_pipe[1];
1555         close(child_ready_pipe[0]);
1556         return 0;
1557
1558 out_close_pipes:
1559         close(go_pipe[0]);
1560         close(go_pipe[1]);
1561 out_close_ready_pipe:
1562         close(child_ready_pipe[0]);
1563         close(child_ready_pipe[1]);
1564         return -1;
1565 }
1566
1567 int perf_evlist__start_workload(struct perf_evlist *evlist)
1568 {
1569         if (evlist->workload.cork_fd > 0) {
1570                 char bf = 0;
1571                 int ret;
1572                 /*
1573                  * Remove the cork, let it rip!
1574                  */
1575                 ret = write(evlist->workload.cork_fd, &bf, 1);
1576                 if (ret < 0)
1577                         perror("enable to write to pipe");
1578
1579                 close(evlist->workload.cork_fd);
1580                 return ret;
1581         }
1582
1583         return 0;
1584 }
1585
1586 int perf_evlist__parse_sample(struct perf_evlist *evlist, union perf_event *event,
1587                               struct perf_sample *sample)
1588 {
1589         struct perf_evsel *evsel = perf_evlist__event2evsel(evlist, event);
1590
1591         if (!evsel)
1592                 return -EFAULT;
1593         return perf_evsel__parse_sample(evsel, event, sample);
1594 }
1595
1596 size_t perf_evlist__fprintf(struct perf_evlist *evlist, FILE *fp)
1597 {
1598         struct perf_evsel *evsel;
1599         size_t printed = 0;
1600
1601         evlist__for_each(evlist, evsel) {
1602                 printed += fprintf(fp, "%s%s", evsel->idx ? ", " : "",
1603                                    perf_evsel__name(evsel));
1604         }
1605
1606         return printed + fprintf(fp, "\n");
1607 }
1608
1609 int perf_evlist__strerror_open(struct perf_evlist *evlist __maybe_unused,
1610                                int err, char *buf, size_t size)
1611 {
1612         int printed, value;
1613         char sbuf[STRERR_BUFSIZE], *emsg = strerror_r(err, sbuf, sizeof(sbuf));
1614
1615         switch (err) {
1616         case EACCES:
1617         case EPERM:
1618                 printed = scnprintf(buf, size,
1619                                     "Error:\t%s.\n"
1620                                     "Hint:\tCheck /proc/sys/kernel/perf_event_paranoid setting.", emsg);
1621
1622                 value = perf_event_paranoid();
1623
1624                 printed += scnprintf(buf + printed, size - printed, "\nHint:\t");
1625
1626                 if (value >= 2) {
1627                         printed += scnprintf(buf + printed, size - printed,
1628                                              "For your workloads it needs to be <= 1\nHint:\t");
1629                 }
1630                 printed += scnprintf(buf + printed, size - printed,
1631                                      "For system wide tracing it needs to be set to -1.\n");
1632
1633                 printed += scnprintf(buf + printed, size - printed,
1634                                     "Hint:\tTry: 'sudo sh -c \"echo -1 > /proc/sys/kernel/perf_event_paranoid\"'\n"
1635                                     "Hint:\tThe current value is %d.", value);
1636                 break;
1637         default:
1638                 scnprintf(buf, size, "%s", emsg);
1639                 break;
1640         }
1641
1642         return 0;
1643 }
1644
1645 int perf_evlist__strerror_mmap(struct perf_evlist *evlist, int err, char *buf, size_t size)
1646 {
1647         char sbuf[STRERR_BUFSIZE], *emsg = strerror_r(err, sbuf, sizeof(sbuf));
1648         int pages_attempted = evlist->mmap_len / 1024, pages_max_per_user, printed = 0;
1649
1650         switch (err) {
1651         case EPERM:
1652                 sysctl__read_int("kernel/perf_event_mlock_kb", &pages_max_per_user);
1653                 printed += scnprintf(buf + printed, size - printed,
1654                                      "Error:\t%s.\n"
1655                                      "Hint:\tCheck /proc/sys/kernel/perf_event_mlock_kb (%d kB) setting.\n"
1656                                      "Hint:\tTried using %zd kB.\n",
1657                                      emsg, pages_max_per_user, pages_attempted);
1658
1659                 if (pages_attempted >= pages_max_per_user) {
1660                         printed += scnprintf(buf + printed, size - printed,
1661                                              "Hint:\tTry 'sudo sh -c \"echo %d > /proc/sys/kernel/perf_event_mlock_kb\"', or\n",
1662                                              pages_max_per_user + pages_attempted);
1663                 }
1664
1665                 printed += scnprintf(buf + printed, size - printed,
1666                                      "Hint:\tTry using a smaller -m/--mmap-pages value.");
1667                 break;
1668         default:
1669                 scnprintf(buf, size, "%s", emsg);
1670                 break;
1671         }
1672
1673         return 0;
1674 }
1675
1676 void perf_evlist__to_front(struct perf_evlist *evlist,
1677                            struct perf_evsel *move_evsel)
1678 {
1679         struct perf_evsel *evsel, *n;
1680         LIST_HEAD(move);
1681
1682         if (move_evsel == perf_evlist__first(evlist))
1683                 return;
1684
1685         evlist__for_each_safe(evlist, n, evsel) {
1686                 if (evsel->leader == move_evsel->leader)
1687                         list_move_tail(&evsel->node, &move);
1688         }
1689
1690         list_splice(&move, &evlist->entries);
1691 }
1692
1693 void perf_evlist__set_tracking_event(struct perf_evlist *evlist,
1694                                      struct perf_evsel *tracking_evsel)
1695 {
1696         struct perf_evsel *evsel;
1697
1698         if (tracking_evsel->tracking)
1699                 return;
1700
1701         evlist__for_each(evlist, evsel) {
1702                 if (evsel != tracking_evsel)
1703                         evsel->tracking = false;
1704         }
1705
1706         tracking_evsel->tracking = true;
1707 }