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