perf evlist: Use cpu_map__nr() helper
[linux-3.10.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 <lk/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(void)
42 {
43         struct perf_evlist *evlist = zalloc(sizeof(*evlist));
44
45         if (evlist != NULL)
46                 perf_evlist__init(evlist, NULL, NULL);
47
48         return evlist;
49 }
50
51 void perf_evlist__config(struct perf_evlist *evlist,
52                         struct perf_record_opts *opts)
53 {
54         struct perf_evsel *evsel;
55         /*
56          * Set the evsel leader links before we configure attributes,
57          * since some might depend on this info.
58          */
59         if (opts->group)
60                 perf_evlist__set_leader(evlist);
61
62         if (evlist->cpus->map[0] < 0)
63                 opts->no_inherit = true;
64
65         list_for_each_entry(evsel, &evlist->entries, node) {
66                 perf_evsel__config(evsel, opts);
67
68                 if (evlist->nr_entries > 1)
69                         perf_evsel__set_sample_id(evsel);
70         }
71 }
72
73 static void perf_evlist__purge(struct perf_evlist *evlist)
74 {
75         struct perf_evsel *pos, *n;
76
77         list_for_each_entry_safe(pos, n, &evlist->entries, node) {
78                 list_del_init(&pos->node);
79                 perf_evsel__delete(pos);
80         }
81
82         evlist->nr_entries = 0;
83 }
84
85 void perf_evlist__exit(struct perf_evlist *evlist)
86 {
87         free(evlist->mmap);
88         free(evlist->pollfd);
89         evlist->mmap = NULL;
90         evlist->pollfd = NULL;
91 }
92
93 void perf_evlist__delete(struct perf_evlist *evlist)
94 {
95         perf_evlist__purge(evlist);
96         perf_evlist__exit(evlist);
97         free(evlist);
98 }
99
100 void perf_evlist__add(struct perf_evlist *evlist, struct perf_evsel *entry)
101 {
102         list_add_tail(&entry->node, &evlist->entries);
103         ++evlist->nr_entries;
104 }
105
106 void perf_evlist__splice_list_tail(struct perf_evlist *evlist,
107                                    struct list_head *list,
108                                    int nr_entries)
109 {
110         list_splice_tail(list, &evlist->entries);
111         evlist->nr_entries += nr_entries;
112 }
113
114 void __perf_evlist__set_leader(struct list_head *list)
115 {
116         struct perf_evsel *evsel, *leader;
117
118         leader = list_entry(list->next, struct perf_evsel, node);
119         evsel = list_entry(list->prev, struct perf_evsel, node);
120
121         leader->nr_members = evsel->idx - leader->idx + 1;
122
123         list_for_each_entry(evsel, list, node) {
124                 evsel->leader = leader;
125         }
126 }
127
128 void perf_evlist__set_leader(struct perf_evlist *evlist)
129 {
130         if (evlist->nr_entries) {
131                 evlist->nr_groups = evlist->nr_entries > 1 ? 1 : 0;
132                 __perf_evlist__set_leader(&evlist->entries);
133         }
134 }
135
136 int perf_evlist__add_default(struct perf_evlist *evlist)
137 {
138         struct perf_event_attr attr = {
139                 .type = PERF_TYPE_HARDWARE,
140                 .config = PERF_COUNT_HW_CPU_CYCLES,
141         };
142         struct perf_evsel *evsel;
143
144         event_attr_init(&attr);
145
146         evsel = perf_evsel__new(&attr, 0);
147         if (evsel == NULL)
148                 goto error;
149
150         /* use strdup() because free(evsel) assumes name is allocated */
151         evsel->name = strdup("cycles");
152         if (!evsel->name)
153                 goto error_free;
154
155         perf_evlist__add(evlist, evsel);
156         return 0;
157 error_free:
158         perf_evsel__delete(evsel);
159 error:
160         return -ENOMEM;
161 }
162
163 static int perf_evlist__add_attrs(struct perf_evlist *evlist,
164                                   struct perf_event_attr *attrs, size_t nr_attrs)
165 {
166         struct perf_evsel *evsel, *n;
167         LIST_HEAD(head);
168         size_t i;
169
170         for (i = 0; i < nr_attrs; i++) {
171                 evsel = perf_evsel__new(attrs + i, evlist->nr_entries + i);
172                 if (evsel == NULL)
173                         goto out_delete_partial_list;
174                 list_add_tail(&evsel->node, &head);
175         }
176
177         perf_evlist__splice_list_tail(evlist, &head, nr_attrs);
178
179         return 0;
180
181 out_delete_partial_list:
182         list_for_each_entry_safe(evsel, n, &head, node)
183                 perf_evsel__delete(evsel);
184         return -1;
185 }
186
187 int __perf_evlist__add_default_attrs(struct perf_evlist *evlist,
188                                      struct perf_event_attr *attrs, size_t nr_attrs)
189 {
190         size_t i;
191
192         for (i = 0; i < nr_attrs; i++)
193                 event_attr_init(attrs + i);
194
195         return perf_evlist__add_attrs(evlist, attrs, nr_attrs);
196 }
197
198 struct perf_evsel *
199 perf_evlist__find_tracepoint_by_id(struct perf_evlist *evlist, int id)
200 {
201         struct perf_evsel *evsel;
202
203         list_for_each_entry(evsel, &evlist->entries, node) {
204                 if (evsel->attr.type   == PERF_TYPE_TRACEPOINT &&
205                     (int)evsel->attr.config == id)
206                         return evsel;
207         }
208
209         return NULL;
210 }
211
212 int perf_evlist__add_newtp(struct perf_evlist *evlist,
213                            const char *sys, const char *name, void *handler)
214 {
215         struct perf_evsel *evsel;
216
217         evsel = perf_evsel__newtp(sys, name, evlist->nr_entries);
218         if (evsel == NULL)
219                 return -1;
220
221         evsel->handler.func = handler;
222         perf_evlist__add(evlist, evsel);
223         return 0;
224 }
225
226 void perf_evlist__disable(struct perf_evlist *evlist)
227 {
228         int cpu, thread;
229         struct perf_evsel *pos;
230
231         for (cpu = 0; cpu < cpu_map__nr(evlist->cpus); cpu++) {
232                 list_for_each_entry(pos, &evlist->entries, node) {
233                         if (!perf_evsel__is_group_leader(pos))
234                                 continue;
235                         for (thread = 0; thread < evlist->threads->nr; thread++)
236                                 ioctl(FD(pos, cpu, thread),
237                                       PERF_EVENT_IOC_DISABLE, 0);
238                 }
239         }
240 }
241
242 void perf_evlist__enable(struct perf_evlist *evlist)
243 {
244         int cpu, thread;
245         struct perf_evsel *pos;
246
247         for (cpu = 0; cpu < cpu_map__nr(evlist->cpus); cpu++) {
248                 list_for_each_entry(pos, &evlist->entries, node) {
249                         if (!perf_evsel__is_group_leader(pos))
250                                 continue;
251                         for (thread = 0; thread < evlist->threads->nr; thread++)
252                                 ioctl(FD(pos, cpu, thread),
253                                       PERF_EVENT_IOC_ENABLE, 0);
254                 }
255         }
256 }
257
258 static int perf_evlist__alloc_pollfd(struct perf_evlist *evlist)
259 {
260         int nfds = cpu_map__nr(evlist->cpus) * evlist->threads->nr * evlist->nr_entries;
261         evlist->pollfd = malloc(sizeof(struct pollfd) * nfds);
262         return evlist->pollfd != NULL ? 0 : -ENOMEM;
263 }
264
265 void perf_evlist__add_pollfd(struct perf_evlist *evlist, int fd)
266 {
267         fcntl(fd, F_SETFL, O_NONBLOCK);
268         evlist->pollfd[evlist->nr_fds].fd = fd;
269         evlist->pollfd[evlist->nr_fds].events = POLLIN;
270         evlist->nr_fds++;
271 }
272
273 static void perf_evlist__id_hash(struct perf_evlist *evlist,
274                                  struct perf_evsel *evsel,
275                                  int cpu, int thread, u64 id)
276 {
277         int hash;
278         struct perf_sample_id *sid = SID(evsel, cpu, thread);
279
280         sid->id = id;
281         sid->evsel = evsel;
282         hash = hash_64(sid->id, PERF_EVLIST__HLIST_BITS);
283         hlist_add_head(&sid->node, &evlist->heads[hash]);
284 }
285
286 void perf_evlist__id_add(struct perf_evlist *evlist, struct perf_evsel *evsel,
287                          int cpu, int thread, u64 id)
288 {
289         perf_evlist__id_hash(evlist, evsel, cpu, thread, id);
290         evsel->id[evsel->ids++] = id;
291 }
292
293 static int perf_evlist__id_add_fd(struct perf_evlist *evlist,
294                                   struct perf_evsel *evsel,
295                                   int cpu, int thread, int fd)
296 {
297         u64 read_data[4] = { 0, };
298         int id_idx = 1; /* The first entry is the counter value */
299
300         if (!(evsel->attr.read_format & PERF_FORMAT_ID) ||
301             read(fd, &read_data, sizeof(read_data)) == -1)
302                 return -1;
303
304         if (evsel->attr.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
305                 ++id_idx;
306         if (evsel->attr.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
307                 ++id_idx;
308
309         perf_evlist__id_add(evlist, evsel, cpu, thread, read_data[id_idx]);
310         return 0;
311 }
312
313 struct perf_evsel *perf_evlist__id2evsel(struct perf_evlist *evlist, u64 id)
314 {
315         struct hlist_head *head;
316         struct hlist_node *pos;
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, pos, 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 < cpu_map__nr(evlist->cpus); 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 < cpu_map__nr(evlist->cpus); 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 = cpu_map__nr(evlist->cpus);
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 }