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