Merge remote-tracking branch 'asoc/fix/wm8960' into tmp
[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 "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 <unistd.h>
18
19 #include "parse-events.h"
20
21 #include <sys/mman.h>
22
23 #include <linux/bitops.h>
24 #include <linux/hash.h>
25
26 #define FD(e, x, y) (*(int *)xyarray__entry(e->fd, x, y))
27 #define SID(e, x, y) xyarray__entry(e->sample_id, x, y)
28
29 void perf_evlist__init(struct perf_evlist *evlist, struct cpu_map *cpus,
30                        struct thread_map *threads)
31 {
32         int i;
33
34         for (i = 0; i < PERF_EVLIST__HLIST_SIZE; ++i)
35                 INIT_HLIST_HEAD(&evlist->heads[i]);
36         INIT_LIST_HEAD(&evlist->entries);
37         perf_evlist__set_maps(evlist, cpus, threads);
38         evlist->workload.pid = -1;
39 }
40
41 struct perf_evlist *perf_evlist__new(struct cpu_map *cpus,
42                                      struct thread_map *threads)
43 {
44         struct perf_evlist *evlist = zalloc(sizeof(*evlist));
45
46         if (evlist != NULL)
47                 perf_evlist__init(evlist, cpus, threads);
48
49         return evlist;
50 }
51
52 void perf_evlist__config(struct perf_evlist *evlist,
53                         struct perf_record_opts *opts)
54 {
55         struct perf_evsel *evsel;
56         /*
57          * Set the evsel leader links before we configure attributes,
58          * since some might depend on this info.
59          */
60         if (opts->group)
61                 perf_evlist__set_leader(evlist);
62
63         if (evlist->cpus->map[0] < 0)
64                 opts->no_inherit = true;
65
66         list_for_each_entry(evsel, &evlist->entries, node) {
67                 perf_evsel__config(evsel, opts);
68
69                 if (evlist->nr_entries > 1)
70                         perf_evsel__set_sample_id(evsel);
71         }
72 }
73
74 static void perf_evlist__purge(struct perf_evlist *evlist)
75 {
76         struct perf_evsel *pos, *n;
77
78         list_for_each_entry_safe(pos, n, &evlist->entries, node) {
79                 list_del_init(&pos->node);
80                 perf_evsel__delete(pos);
81         }
82
83         evlist->nr_entries = 0;
84 }
85
86 void perf_evlist__exit(struct perf_evlist *evlist)
87 {
88         free(evlist->mmap);
89         free(evlist->pollfd);
90         evlist->mmap = NULL;
91         evlist->pollfd = NULL;
92 }
93
94 void perf_evlist__delete(struct perf_evlist *evlist)
95 {
96         perf_evlist__purge(evlist);
97         perf_evlist__exit(evlist);
98         free(evlist);
99 }
100
101 void perf_evlist__add(struct perf_evlist *evlist, struct perf_evsel *entry)
102 {
103         list_add_tail(&entry->node, &evlist->entries);
104         ++evlist->nr_entries;
105 }
106
107 void perf_evlist__splice_list_tail(struct perf_evlist *evlist,
108                                    struct list_head *list,
109                                    int nr_entries)
110 {
111         list_splice_tail(list, &evlist->entries);
112         evlist->nr_entries += nr_entries;
113 }
114
115 void __perf_evlist__set_leader(struct list_head *list)
116 {
117         struct perf_evsel *evsel, *leader;
118
119         leader = list_entry(list->next, struct perf_evsel, node);
120         evsel = list_entry(list->prev, struct perf_evsel, node);
121
122         leader->nr_members = evsel->idx - leader->idx + 1;
123
124         list_for_each_entry(evsel, list, node) {
125                 evsel->leader = leader;
126         }
127 }
128
129 void perf_evlist__set_leader(struct perf_evlist *evlist)
130 {
131         if (evlist->nr_entries) {
132                 evlist->nr_groups = evlist->nr_entries > 1 ? 1 : 0;
133                 __perf_evlist__set_leader(&evlist->entries);
134         }
135 }
136
137 int perf_evlist__add_default(struct perf_evlist *evlist)
138 {
139         struct perf_event_attr attr = {
140                 .type = PERF_TYPE_HARDWARE,
141                 .config = PERF_COUNT_HW_CPU_CYCLES,
142         };
143         struct perf_evsel *evsel;
144
145         event_attr_init(&attr);
146
147         evsel = perf_evsel__new(&attr, 0);
148         if (evsel == NULL)
149                 goto error;
150
151         /* use strdup() because free(evsel) assumes name is allocated */
152         evsel->name = strdup("cycles");
153         if (!evsel->name)
154                 goto error_free;
155
156         perf_evlist__add(evlist, evsel);
157         return 0;
158 error_free:
159         perf_evsel__delete(evsel);
160 error:
161         return -ENOMEM;
162 }
163
164 static int perf_evlist__add_attrs(struct perf_evlist *evlist,
165                                   struct perf_event_attr *attrs, size_t nr_attrs)
166 {
167         struct perf_evsel *evsel, *n;
168         LIST_HEAD(head);
169         size_t i;
170
171         for (i = 0; i < nr_attrs; i++) {
172                 evsel = perf_evsel__new(attrs + i, evlist->nr_entries + i);
173                 if (evsel == NULL)
174                         goto out_delete_partial_list;
175                 list_add_tail(&evsel->node, &head);
176         }
177
178         perf_evlist__splice_list_tail(evlist, &head, nr_attrs);
179
180         return 0;
181
182 out_delete_partial_list:
183         list_for_each_entry_safe(evsel, n, &head, node)
184                 perf_evsel__delete(evsel);
185         return -1;
186 }
187
188 int __perf_evlist__add_default_attrs(struct perf_evlist *evlist,
189                                      struct perf_event_attr *attrs, size_t nr_attrs)
190 {
191         size_t i;
192
193         for (i = 0; i < nr_attrs; i++)
194                 event_attr_init(attrs + i);
195
196         return perf_evlist__add_attrs(evlist, attrs, nr_attrs);
197 }
198
199 struct perf_evsel *
200 perf_evlist__find_tracepoint_by_id(struct perf_evlist *evlist, int id)
201 {
202         struct perf_evsel *evsel;
203
204         list_for_each_entry(evsel, &evlist->entries, node) {
205                 if (evsel->attr.type   == PERF_TYPE_TRACEPOINT &&
206                     (int)evsel->attr.config == id)
207                         return evsel;
208         }
209
210         return NULL;
211 }
212
213 int perf_evlist__add_newtp(struct perf_evlist *evlist,
214                            const char *sys, const char *name, void *handler)
215 {
216         struct perf_evsel *evsel;
217
218         evsel = perf_evsel__newtp(sys, name, evlist->nr_entries);
219         if (evsel == NULL)
220                 return -1;
221
222         evsel->handler.func = handler;
223         perf_evlist__add(evlist, evsel);
224         return 0;
225 }
226
227 void perf_evlist__disable(struct perf_evlist *evlist)
228 {
229         int cpu, thread;
230         struct perf_evsel *pos;
231
232         for (cpu = 0; cpu < evlist->cpus->nr; cpu++) {
233                 list_for_each_entry(pos, &evlist->entries, node) {
234                         if (!perf_evsel__is_group_leader(pos))
235                                 continue;
236                         for (thread = 0; thread < evlist->threads->nr; thread++)
237                                 ioctl(FD(pos, cpu, thread),
238                                       PERF_EVENT_IOC_DISABLE, 0);
239                 }
240         }
241 }
242
243 void perf_evlist__enable(struct perf_evlist *evlist)
244 {
245         int cpu, thread;
246         struct perf_evsel *pos;
247
248         for (cpu = 0; cpu < cpu_map__nr(evlist->cpus); cpu++) {
249                 list_for_each_entry(pos, &evlist->entries, node) {
250                         if (!perf_evsel__is_group_leader(pos))
251                                 continue;
252                         for (thread = 0; thread < evlist->threads->nr; thread++)
253                                 ioctl(FD(pos, cpu, thread),
254                                       PERF_EVENT_IOC_ENABLE, 0);
255                 }
256         }
257 }
258
259 static int perf_evlist__alloc_pollfd(struct perf_evlist *evlist)
260 {
261         int nfds = cpu_map__nr(evlist->cpus) * evlist->threads->nr * evlist->nr_entries;
262         evlist->pollfd = malloc(sizeof(struct pollfd) * nfds);
263         return evlist->pollfd != NULL ? 0 : -ENOMEM;
264 }
265
266 void perf_evlist__add_pollfd(struct perf_evlist *evlist, int fd)
267 {
268         fcntl(fd, F_SETFL, O_NONBLOCK);
269         evlist->pollfd[evlist->nr_fds].fd = fd;
270         evlist->pollfd[evlist->nr_fds].events = POLLIN;
271         evlist->nr_fds++;
272 }
273
274 static void perf_evlist__id_hash(struct perf_evlist *evlist,
275                                  struct perf_evsel *evsel,
276                                  int cpu, int thread, u64 id)
277 {
278         int hash;
279         struct perf_sample_id *sid = SID(evsel, cpu, thread);
280
281         sid->id = id;
282         sid->evsel = evsel;
283         hash = hash_64(sid->id, PERF_EVLIST__HLIST_BITS);
284         hlist_add_head(&sid->node, &evlist->heads[hash]);
285 }
286
287 void perf_evlist__id_add(struct perf_evlist *evlist, struct perf_evsel *evsel,
288                          int cpu, int thread, u64 id)
289 {
290         perf_evlist__id_hash(evlist, evsel, cpu, thread, id);
291         evsel->id[evsel->ids++] = id;
292 }
293
294 static int perf_evlist__id_add_fd(struct perf_evlist *evlist,
295                                   struct perf_evsel *evsel,
296                                   int cpu, int thread, int fd)
297 {
298         u64 read_data[4] = { 0, };
299         int id_idx = 1; /* The first entry is the counter value */
300
301         if (!(evsel->attr.read_format & PERF_FORMAT_ID) ||
302             read(fd, &read_data, sizeof(read_data)) == -1)
303                 return -1;
304
305         if (evsel->attr.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
306                 ++id_idx;
307         if (evsel->attr.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
308                 ++id_idx;
309
310         perf_evlist__id_add(evlist, evsel, cpu, thread, read_data[id_idx]);
311         return 0;
312 }
313
314 struct perf_evsel *perf_evlist__id2evsel(struct perf_evlist *evlist, u64 id)
315 {
316         struct hlist_head *head;
317         struct perf_sample_id *sid;
318         int hash;
319
320         if (evlist->nr_entries == 1)
321                 return perf_evlist__first(evlist);
322
323         hash = hash_64(id, PERF_EVLIST__HLIST_BITS);
324         head = &evlist->heads[hash];
325
326         hlist_for_each_entry(sid, head, node)
327                 if (sid->id == id)
328                         return sid->evsel;
329
330         if (!perf_evlist__sample_id_all(evlist))
331                 return perf_evlist__first(evlist);
332
333         return NULL;
334 }
335
336 union perf_event *perf_evlist__mmap_read(struct perf_evlist *evlist, int idx)
337 {
338         struct perf_mmap *md = &evlist->mmap[idx];
339         unsigned int head = perf_mmap__read_head(md);
340         unsigned int old = md->prev;
341         unsigned char *data = md->base + page_size;
342         union perf_event *event = NULL;
343
344         if (evlist->overwrite) {
345                 /*
346                  * If we're further behind than half the buffer, there's a chance
347                  * the writer will bite our tail and mess up the samples under us.
348                  *
349                  * If we somehow ended up ahead of the head, we got messed up.
350                  *
351                  * In either case, truncate and restart at head.
352                  */
353                 int diff = head - old;
354                 if (diff > md->mask / 2 || diff < 0) {
355                         fprintf(stderr, "WARNING: failed to keep up with mmap data.\n");
356
357                         /*
358                          * head points to a known good entry, start there.
359                          */
360                         old = head;
361                 }
362         }
363
364         if (old != head) {
365                 size_t size;
366
367                 event = (union perf_event *)&data[old & md->mask];
368                 size = event->header.size;
369
370                 /*
371                  * Event straddles the mmap boundary -- header should always
372                  * be inside due to u64 alignment of output.
373                  */
374                 if ((old & md->mask) + size != ((old + size) & md->mask)) {
375                         unsigned int offset = old;
376                         unsigned int len = min(sizeof(*event), size), cpy;
377                         void *dst = &md->event_copy;
378
379                         do {
380                                 cpy = min(md->mask + 1 - (offset & md->mask), len);
381                                 memcpy(dst, &data[offset & md->mask], cpy);
382                                 offset += cpy;
383                                 dst += cpy;
384                                 len -= cpy;
385                         } while (len);
386
387                         event = &md->event_copy;
388                 }
389
390                 old += size;
391         }
392
393         md->prev = old;
394
395         if (!evlist->overwrite)
396                 perf_mmap__write_tail(md, old);
397
398         return event;
399 }
400
401 void perf_evlist__munmap(struct perf_evlist *evlist)
402 {
403         int i;
404
405         for (i = 0; i < evlist->nr_mmaps; i++) {
406                 if (evlist->mmap[i].base != NULL) {
407                         munmap(evlist->mmap[i].base, evlist->mmap_len);
408                         evlist->mmap[i].base = NULL;
409                 }
410         }
411
412         free(evlist->mmap);
413         evlist->mmap = NULL;
414 }
415
416 static int perf_evlist__alloc_mmap(struct perf_evlist *evlist)
417 {
418         evlist->nr_mmaps = cpu_map__nr(evlist->cpus);
419         if (cpu_map__all(evlist->cpus))
420                 evlist->nr_mmaps = evlist->threads->nr;
421         evlist->mmap = zalloc(evlist->nr_mmaps * sizeof(struct perf_mmap));
422         return evlist->mmap != NULL ? 0 : -ENOMEM;
423 }
424
425 static int __perf_evlist__mmap(struct perf_evlist *evlist,
426                                int idx, int prot, int mask, int fd)
427 {
428         evlist->mmap[idx].prev = 0;
429         evlist->mmap[idx].mask = mask;
430         evlist->mmap[idx].base = mmap(NULL, evlist->mmap_len, prot,
431                                       MAP_SHARED, fd, 0);
432         if (evlist->mmap[idx].base == MAP_FAILED) {
433                 evlist->mmap[idx].base = NULL;
434                 return -1;
435         }
436
437         perf_evlist__add_pollfd(evlist, fd);
438         return 0;
439 }
440
441 static int perf_evlist__mmap_per_cpu(struct perf_evlist *evlist, int prot, int mask)
442 {
443         struct perf_evsel *evsel;
444         int cpu, thread;
445
446         for (cpu = 0; cpu < evlist->cpus->nr; cpu++) {
447                 int output = -1;
448
449                 for (thread = 0; thread < evlist->threads->nr; thread++) {
450                         list_for_each_entry(evsel, &evlist->entries, node) {
451                                 int fd = FD(evsel, cpu, thread);
452
453                                 if (output == -1) {
454                                         output = fd;
455                                         if (__perf_evlist__mmap(evlist, cpu,
456                                                                 prot, mask, output) < 0)
457                                                 goto out_unmap;
458                                 } else {
459                                         if (ioctl(fd, PERF_EVENT_IOC_SET_OUTPUT, output) != 0)
460                                                 goto out_unmap;
461                                 }
462
463                                 if ((evsel->attr.read_format & PERF_FORMAT_ID) &&
464                                     perf_evlist__id_add_fd(evlist, evsel, cpu, thread, fd) < 0)
465                                         goto out_unmap;
466                         }
467                 }
468         }
469
470         return 0;
471
472 out_unmap:
473         for (cpu = 0; cpu < evlist->cpus->nr; cpu++) {
474                 if (evlist->mmap[cpu].base != NULL) {
475                         munmap(evlist->mmap[cpu].base, evlist->mmap_len);
476                         evlist->mmap[cpu].base = NULL;
477                 }
478         }
479         return -1;
480 }
481
482 static int perf_evlist__mmap_per_thread(struct perf_evlist *evlist, int prot, int mask)
483 {
484         struct perf_evsel *evsel;
485         int thread;
486
487         for (thread = 0; thread < evlist->threads->nr; thread++) {
488                 int output = -1;
489
490                 list_for_each_entry(evsel, &evlist->entries, node) {
491                         int fd = FD(evsel, 0, thread);
492
493                         if (output == -1) {
494                                 output = fd;
495                                 if (__perf_evlist__mmap(evlist, thread,
496                                                         prot, mask, output) < 0)
497                                         goto out_unmap;
498                         } else {
499                                 if (ioctl(fd, PERF_EVENT_IOC_SET_OUTPUT, output) != 0)
500                                         goto out_unmap;
501                         }
502
503                         if ((evsel->attr.read_format & PERF_FORMAT_ID) &&
504                             perf_evlist__id_add_fd(evlist, evsel, 0, thread, fd) < 0)
505                                 goto out_unmap;
506                 }
507         }
508
509         return 0;
510
511 out_unmap:
512         for (thread = 0; thread < evlist->threads->nr; thread++) {
513                 if (evlist->mmap[thread].base != NULL) {
514                         munmap(evlist->mmap[thread].base, evlist->mmap_len);
515                         evlist->mmap[thread].base = NULL;
516                 }
517         }
518         return -1;
519 }
520
521 /** perf_evlist__mmap - Create per cpu maps to receive events
522  *
523  * @evlist - list of events
524  * @pages - map length in pages
525  * @overwrite - overwrite older events?
526  *
527  * If overwrite is false the user needs to signal event consuption using:
528  *
529  *      struct perf_mmap *m = &evlist->mmap[cpu];
530  *      unsigned int head = perf_mmap__read_head(m);
531  *
532  *      perf_mmap__write_tail(m, head)
533  *
534  * Using perf_evlist__read_on_cpu does this automatically.
535  */
536 int perf_evlist__mmap(struct perf_evlist *evlist, unsigned int pages,
537                       bool overwrite)
538 {
539         struct perf_evsel *evsel;
540         const struct cpu_map *cpus = evlist->cpus;
541         const struct thread_map *threads = evlist->threads;
542         int prot = PROT_READ | (overwrite ? 0 : PROT_WRITE), mask;
543
544         /* 512 kiB: default amount of unprivileged mlocked memory */
545         if (pages == UINT_MAX)
546                 pages = (512 * 1024) / page_size;
547         else if (!is_power_of_2(pages))
548                 return -EINVAL;
549
550         mask = pages * page_size - 1;
551
552         if (evlist->mmap == NULL && perf_evlist__alloc_mmap(evlist) < 0)
553                 return -ENOMEM;
554
555         if (evlist->pollfd == NULL && perf_evlist__alloc_pollfd(evlist) < 0)
556                 return -ENOMEM;
557
558         evlist->overwrite = overwrite;
559         evlist->mmap_len = (pages + 1) * page_size;
560
561         list_for_each_entry(evsel, &evlist->entries, node) {
562                 if ((evsel->attr.read_format & PERF_FORMAT_ID) &&
563                     evsel->sample_id == NULL &&
564                     perf_evsel__alloc_id(evsel, cpu_map__nr(cpus), threads->nr) < 0)
565                         return -ENOMEM;
566         }
567
568         if (cpu_map__all(cpus))
569                 return perf_evlist__mmap_per_thread(evlist, prot, mask);
570
571         return perf_evlist__mmap_per_cpu(evlist, prot, mask);
572 }
573
574 int perf_evlist__create_maps(struct perf_evlist *evlist,
575                              struct perf_target *target)
576 {
577         evlist->threads = thread_map__new_str(target->pid, target->tid,
578                                               target->uid);
579
580         if (evlist->threads == NULL)
581                 return -1;
582
583         if (perf_target__has_task(target))
584                 evlist->cpus = cpu_map__dummy_new();
585         else if (!perf_target__has_cpu(target) && !target->uses_mmap)
586                 evlist->cpus = cpu_map__dummy_new();
587         else
588                 evlist->cpus = cpu_map__new(target->cpu_list);
589
590         if (evlist->cpus == NULL)
591                 goto out_delete_threads;
592
593         return 0;
594
595 out_delete_threads:
596         thread_map__delete(evlist->threads);
597         return -1;
598 }
599
600 void perf_evlist__delete_maps(struct perf_evlist *evlist)
601 {
602         cpu_map__delete(evlist->cpus);
603         thread_map__delete(evlist->threads);
604         evlist->cpus    = NULL;
605         evlist->threads = NULL;
606 }
607
608 int perf_evlist__apply_filters(struct perf_evlist *evlist)
609 {
610         struct perf_evsel *evsel;
611         int err = 0;
612         const int ncpus = cpu_map__nr(evlist->cpus),
613                   nthreads = evlist->threads->nr;
614
615         list_for_each_entry(evsel, &evlist->entries, node) {
616                 if (evsel->filter == NULL)
617                         continue;
618
619                 err = perf_evsel__set_filter(evsel, ncpus, nthreads, evsel->filter);
620                 if (err)
621                         break;
622         }
623
624         return err;
625 }
626
627 int perf_evlist__set_filter(struct perf_evlist *evlist, const char *filter)
628 {
629         struct perf_evsel *evsel;
630         int err = 0;
631         const int ncpus = cpu_map__nr(evlist->cpus),
632                   nthreads = evlist->threads->nr;
633
634         list_for_each_entry(evsel, &evlist->entries, node) {
635                 err = perf_evsel__set_filter(evsel, ncpus, nthreads, filter);
636                 if (err)
637                         break;
638         }
639
640         return err;
641 }
642
643 bool perf_evlist__valid_sample_type(struct perf_evlist *evlist)
644 {
645         struct perf_evsel *first = perf_evlist__first(evlist), *pos = first;
646
647         list_for_each_entry_continue(pos, &evlist->entries, node) {
648                 if (first->attr.sample_type != pos->attr.sample_type)
649                         return false;
650         }
651
652         return true;
653 }
654
655 u64 perf_evlist__sample_type(struct perf_evlist *evlist)
656 {
657         struct perf_evsel *first = perf_evlist__first(evlist);
658         return first->attr.sample_type;
659 }
660
661 u16 perf_evlist__id_hdr_size(struct perf_evlist *evlist)
662 {
663         struct perf_evsel *first = perf_evlist__first(evlist);
664         struct perf_sample *data;
665         u64 sample_type;
666         u16 size = 0;
667
668         if (!first->attr.sample_id_all)
669                 goto out;
670
671         sample_type = first->attr.sample_type;
672
673         if (sample_type & PERF_SAMPLE_TID)
674                 size += sizeof(data->tid) * 2;
675
676        if (sample_type & PERF_SAMPLE_TIME)
677                 size += sizeof(data->time);
678
679         if (sample_type & PERF_SAMPLE_ID)
680                 size += sizeof(data->id);
681
682         if (sample_type & PERF_SAMPLE_STREAM_ID)
683                 size += sizeof(data->stream_id);
684
685         if (sample_type & PERF_SAMPLE_CPU)
686                 size += sizeof(data->cpu) * 2;
687 out:
688         return size;
689 }
690
691 bool perf_evlist__valid_sample_id_all(struct perf_evlist *evlist)
692 {
693         struct perf_evsel *first = perf_evlist__first(evlist), *pos = first;
694
695         list_for_each_entry_continue(pos, &evlist->entries, node) {
696                 if (first->attr.sample_id_all != pos->attr.sample_id_all)
697                         return false;
698         }
699
700         return true;
701 }
702
703 bool perf_evlist__sample_id_all(struct perf_evlist *evlist)
704 {
705         struct perf_evsel *first = perf_evlist__first(evlist);
706         return first->attr.sample_id_all;
707 }
708
709 void perf_evlist__set_selected(struct perf_evlist *evlist,
710                                struct perf_evsel *evsel)
711 {
712         evlist->selected = evsel;
713 }
714
715 int perf_evlist__open(struct perf_evlist *evlist)
716 {
717         struct perf_evsel *evsel;
718         int err, ncpus, nthreads;
719
720         list_for_each_entry(evsel, &evlist->entries, node) {
721                 err = perf_evsel__open(evsel, evlist->cpus, evlist->threads);
722                 if (err < 0)
723                         goto out_err;
724         }
725
726         return 0;
727 out_err:
728         ncpus = evlist->cpus ? evlist->cpus->nr : 1;
729         nthreads = evlist->threads ? evlist->threads->nr : 1;
730
731         list_for_each_entry_reverse(evsel, &evlist->entries, node)
732                 perf_evsel__close(evsel, ncpus, nthreads);
733
734         errno = -err;
735         return err;
736 }
737
738 int perf_evlist__prepare_workload(struct perf_evlist *evlist,
739                                   struct perf_record_opts *opts,
740                                   const char *argv[])
741 {
742         int child_ready_pipe[2], go_pipe[2];
743         char bf;
744
745         if (pipe(child_ready_pipe) < 0) {
746                 perror("failed to create 'ready' pipe");
747                 return -1;
748         }
749
750         if (pipe(go_pipe) < 0) {
751                 perror("failed to create 'go' pipe");
752                 goto out_close_ready_pipe;
753         }
754
755         evlist->workload.pid = fork();
756         if (evlist->workload.pid < 0) {
757                 perror("failed to fork");
758                 goto out_close_pipes;
759         }
760
761         if (!evlist->workload.pid) {
762                 if (opts->pipe_output)
763                         dup2(2, 1);
764
765                 close(child_ready_pipe[0]);
766                 close(go_pipe[1]);
767                 fcntl(go_pipe[0], F_SETFD, FD_CLOEXEC);
768
769                 /*
770                  * Do a dummy execvp to get the PLT entry resolved,
771                  * so we avoid the resolver overhead on the real
772                  * execvp call.
773                  */
774                 execvp("", (char **)argv);
775
776                 /*
777                  * Tell the parent we're ready to go
778                  */
779                 close(child_ready_pipe[1]);
780
781                 /*
782                  * Wait until the parent tells us to go.
783                  */
784                 if (read(go_pipe[0], &bf, 1) == -1)
785                         perror("unable to read pipe");
786
787                 execvp(argv[0], (char **)argv);
788
789                 perror(argv[0]);
790                 kill(getppid(), SIGUSR1);
791                 exit(-1);
792         }
793
794         if (perf_target__none(&opts->target))
795                 evlist->threads->map[0] = evlist->workload.pid;
796
797         close(child_ready_pipe[1]);
798         close(go_pipe[0]);
799         /*
800          * wait for child to settle
801          */
802         if (read(child_ready_pipe[0], &bf, 1) == -1) {
803                 perror("unable to read pipe");
804                 goto out_close_pipes;
805         }
806
807         evlist->workload.cork_fd = go_pipe[1];
808         close(child_ready_pipe[0]);
809         return 0;
810
811 out_close_pipes:
812         close(go_pipe[0]);
813         close(go_pipe[1]);
814 out_close_ready_pipe:
815         close(child_ready_pipe[0]);
816         close(child_ready_pipe[1]);
817         return -1;
818 }
819
820 int perf_evlist__start_workload(struct perf_evlist *evlist)
821 {
822         if (evlist->workload.cork_fd > 0) {
823                 /*
824                  * Remove the cork, let it rip!
825                  */
826                 return close(evlist->workload.cork_fd);
827         }
828
829         return 0;
830 }
831
832 int perf_evlist__parse_sample(struct perf_evlist *evlist, union perf_event *event,
833                               struct perf_sample *sample)
834 {
835         struct perf_evsel *evsel = perf_evlist__first(evlist);
836         return perf_evsel__parse_sample(evsel, event, sample);
837 }
838
839 size_t perf_evlist__fprintf(struct perf_evlist *evlist, FILE *fp)
840 {
841         struct perf_evsel *evsel;
842         size_t printed = 0;
843
844         list_for_each_entry(evsel, &evlist->entries, node) {
845                 printed += fprintf(fp, "%s%s", evsel->idx ? ", " : "",
846                                    perf_evsel__name(evsel));
847         }
848
849         return printed + fprintf(fp, "\n");;
850 }