blob: 0d3b73996255fafb72db6f1522eb74d100d1c456 [file] [log] [blame]
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -02001/*
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 */
Arnaldo Carvalho de Meloa8c9ae12011-11-05 08:41:51 -02009#include "util.h"
Borislav Petkov85c66be2013-02-20 16:32:30 +010010#include <lk/debugfs.h>
Arnaldo Carvalho de Melo5c581042011-01-11 22:30:02 -020011#include <poll.h>
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -020012#include "cpumap.h"
13#include "thread_map.h"
Namhyung Kim12864b32012-04-26 14:15:22 +090014#include "target.h"
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -020015#include "evlist.h"
16#include "evsel.h"
Arnaldo Carvalho de Melo35b9d882011-11-09 08:47:15 -020017#include <unistd.h>
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -020018
Arnaldo Carvalho de Melo50d08e42011-11-04 09:10:59 -020019#include "parse-events.h"
20
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -020021#include <sys/mman.h>
22
Arnaldo Carvalho de Melo70db7532011-01-12 22:39:13 -020023#include <linux/bitops.h>
24#include <linux/hash.h>
25
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -020026#define FD(e, x, y) (*(int *)xyarray__entry(e->fd, x, y))
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -030027#define SID(e, x, y) xyarray__entry(e->sample_id, x, y)
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -020028
Arnaldo Carvalho de Melo7e2ed092011-01-30 11:59:43 -020029void perf_evlist__init(struct perf_evlist *evlist, struct cpu_map *cpus,
30 struct thread_map *threads)
Arnaldo Carvalho de Meloef1d1af2011-01-18 21:41:45 -020031{
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);
Arnaldo Carvalho de Melo7e2ed092011-01-30 11:59:43 -020037 perf_evlist__set_maps(evlist, cpus, threads);
Arnaldo Carvalho de Melo35b9d882011-11-09 08:47:15 -020038 evlist->workload.pid = -1;
Arnaldo Carvalho de Meloef1d1af2011-01-18 21:41:45 -020039}
40
Namhyung Kim334fe7a2013-03-11 16:43:12 +090041struct perf_evlist *perf_evlist__new(void)
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -020042{
43 struct perf_evlist *evlist = zalloc(sizeof(*evlist));
44
Arnaldo Carvalho de Meloef1d1af2011-01-18 21:41:45 -020045 if (evlist != NULL)
Namhyung Kim334fe7a2013-03-11 16:43:12 +090046 perf_evlist__init(evlist, NULL, NULL);
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -020047
48 return evlist;
49}
50
Arnaldo Carvalho de Melof77a9512012-12-10 16:41:31 -030051void perf_evlist__config(struct perf_evlist *evlist,
52 struct perf_record_opts *opts)
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -020053{
Jiri Olsacac21422012-11-12 18:34:00 +010054 struct perf_evsel *evsel;
Arnaldo Carvalho de Melof77a9512012-12-10 16:41:31 -030055 /*
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);
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -020061
62 if (evlist->cpus->map[0] < 0)
63 opts->no_inherit = true;
64
65 list_for_each_entry(evsel, &evlist->entries, node) {
Jiri Olsacac21422012-11-12 18:34:00 +010066 perf_evsel__config(evsel, opts);
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -020067
68 if (evlist->nr_entries > 1)
Arnaldo Carvalho de Melo3a5afae2012-12-10 15:29:12 -030069 perf_evsel__set_sample_id(evsel);
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -020070 }
71}
72
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -020073static 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
Arnaldo Carvalho de Meloef1d1af2011-01-18 21:41:45 -020085void 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
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -020093void perf_evlist__delete(struct perf_evlist *evlist)
94{
95 perf_evlist__purge(evlist);
Arnaldo Carvalho de Meloef1d1af2011-01-18 21:41:45 -020096 perf_evlist__exit(evlist);
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -020097 free(evlist);
98}
99
100void 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
Jiri Olsa0529bc12012-01-27 15:34:20 +0100106void perf_evlist__splice_list_tail(struct perf_evlist *evlist,
107 struct list_head *list,
108 int nr_entries)
Arnaldo Carvalho de Melo50d08e42011-11-04 09:10:59 -0200109{
110 list_splice_tail(list, &evlist->entries);
111 evlist->nr_entries += nr_entries;
112}
113
Arnaldo Carvalho de Melo63dab222012-08-14 16:35:48 -0300114void __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);
Namhyung Kim97f63e42013-01-22 18:09:29 +0900119 evsel = list_entry(list->prev, struct perf_evsel, node);
120
121 leader->nr_members = evsel->idx - leader->idx + 1;
Arnaldo Carvalho de Melo63dab222012-08-14 16:35:48 -0300122
123 list_for_each_entry(evsel, list, node) {
Stephane Eranian74b21332013-01-31 13:54:37 +0100124 evsel->leader = leader;
Arnaldo Carvalho de Melo63dab222012-08-14 16:35:48 -0300125 }
126}
127
128void perf_evlist__set_leader(struct perf_evlist *evlist)
Jiri Olsa6a4bb042012-08-08 12:22:36 +0200129{
Namhyung Kim97f63e42013-01-22 18:09:29 +0900130 if (evlist->nr_entries) {
131 evlist->nr_groups = evlist->nr_entries > 1 ? 1 : 0;
Arnaldo Carvalho de Melo63dab222012-08-14 16:35:48 -0300132 __perf_evlist__set_leader(&evlist->entries);
Namhyung Kim97f63e42013-01-22 18:09:29 +0900133 }
Jiri Olsa6a4bb042012-08-08 12:22:36 +0200134}
135
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -0200136int 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 };
Joerg Roedel1aed2672012-01-04 17:54:20 +0100142 struct perf_evsel *evsel;
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -0200143
Joerg Roedel1aed2672012-01-04 17:54:20 +0100144 event_attr_init(&attr);
145
146 evsel = perf_evsel__new(&attr, 0);
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -0200147 if (evsel == NULL)
Stephane Eraniancc2d86b2011-06-07 18:19:36 +0200148 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;
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -0200154
155 perf_evlist__add(evlist, evsel);
156 return 0;
Stephane Eraniancc2d86b2011-06-07 18:19:36 +0200157error_free:
158 perf_evsel__delete(evsel);
159error:
160 return -ENOMEM;
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -0200161}
Arnaldo Carvalho de Melo5c581042011-01-11 22:30:02 -0200162
Arnaldo Carvalho de Meloe60fc842012-10-03 11:50:55 -0300163static int perf_evlist__add_attrs(struct perf_evlist *evlist,
164 struct perf_event_attr *attrs, size_t nr_attrs)
Arnaldo Carvalho de Melo50d08e42011-11-04 09:10:59 -0200165{
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
181out_delete_partial_list:
182 list_for_each_entry_safe(evsel, n, &head, node)
183 perf_evsel__delete(evsel);
184 return -1;
185}
186
Arnaldo Carvalho de Melo79695e12012-05-30 13:53:54 -0300187int __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
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -0300198struct perf_evsel *
199perf_evlist__find_tracepoint_by_id(struct perf_evlist *evlist, int id)
Arnaldo Carvalho de Meloee29be62011-11-28 17:57:40 -0200200{
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
Arnaldo Carvalho de Melo39876e72012-10-03 11:40:22 -0300212int 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
Arnaldo Carvalho de Melo4152ab32011-07-25 11:06:19 -0300226void perf_evlist__disable(struct perf_evlist *evlist)
227{
228 int cpu, thread;
229 struct perf_evsel *pos;
Namhyung Kimb3a319d2013-03-11 16:43:14 +0900230 int nr_cpus = cpu_map__nr(evlist->cpus);
231 int nr_threads = thread_map__nr(evlist->threads);
Arnaldo Carvalho de Melo4152ab32011-07-25 11:06:19 -0300232
Namhyung Kimb3a319d2013-03-11 16:43:14 +0900233 for (cpu = 0; cpu < nr_cpus; cpu++) {
Arnaldo Carvalho de Melo4152ab32011-07-25 11:06:19 -0300234 list_for_each_entry(pos, &evlist->entries, node) {
Namhyung Kim823254e2012-11-29 15:38:30 +0900235 if (!perf_evsel__is_group_leader(pos))
Jiri Olsa3fe4430d2012-11-12 18:34:03 +0100236 continue;
Namhyung Kimb3a319d2013-03-11 16:43:14 +0900237 for (thread = 0; thread < nr_threads; thread++)
Namhyung Kim55da8002012-05-31 14:51:46 +0900238 ioctl(FD(pos, cpu, thread),
239 PERF_EVENT_IOC_DISABLE, 0);
Arnaldo Carvalho de Melo4152ab32011-07-25 11:06:19 -0300240 }
241 }
242}
243
David Ahern764e16a32011-08-25 10:17:55 -0600244void perf_evlist__enable(struct perf_evlist *evlist)
245{
246 int cpu, thread;
247 struct perf_evsel *pos;
Namhyung Kimb3a319d2013-03-11 16:43:14 +0900248 int nr_cpus = cpu_map__nr(evlist->cpus);
249 int nr_threads = thread_map__nr(evlist->threads);
David Ahern764e16a32011-08-25 10:17:55 -0600250
Namhyung Kimb3a319d2013-03-11 16:43:14 +0900251 for (cpu = 0; cpu < nr_cpus; cpu++) {
David Ahern764e16a32011-08-25 10:17:55 -0600252 list_for_each_entry(pos, &evlist->entries, node) {
Namhyung Kim823254e2012-11-29 15:38:30 +0900253 if (!perf_evsel__is_group_leader(pos))
Jiri Olsa3fe4430d2012-11-12 18:34:03 +0100254 continue;
Namhyung Kimb3a319d2013-03-11 16:43:14 +0900255 for (thread = 0; thread < nr_threads; thread++)
Namhyung Kim55da8002012-05-31 14:51:46 +0900256 ioctl(FD(pos, cpu, thread),
257 PERF_EVENT_IOC_ENABLE, 0);
David Ahern764e16a32011-08-25 10:17:55 -0600258 }
259 }
260}
261
Arnaldo Carvalho de Melo806fb632011-11-29 08:05:52 -0200262static int perf_evlist__alloc_pollfd(struct perf_evlist *evlist)
Arnaldo Carvalho de Melo5c581042011-01-11 22:30:02 -0200263{
Namhyung Kimb3a319d2013-03-11 16:43:14 +0900264 int nr_cpus = cpu_map__nr(evlist->cpus);
265 int nr_threads = thread_map__nr(evlist->threads);
266 int nfds = nr_cpus * nr_threads * evlist->nr_entries;
Arnaldo Carvalho de Melo5c581042011-01-11 22:30:02 -0200267 evlist->pollfd = malloc(sizeof(struct pollfd) * nfds);
268 return evlist->pollfd != NULL ? 0 : -ENOMEM;
269}
Arnaldo Carvalho de Melo70082dd2011-01-12 17:03:24 -0200270
271void perf_evlist__add_pollfd(struct perf_evlist *evlist, int fd)
272{
273 fcntl(fd, F_SETFL, O_NONBLOCK);
274 evlist->pollfd[evlist->nr_fds].fd = fd;
275 evlist->pollfd[evlist->nr_fds].events = POLLIN;
276 evlist->nr_fds++;
277}
Arnaldo Carvalho de Melo70db7532011-01-12 22:39:13 -0200278
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -0300279static void perf_evlist__id_hash(struct perf_evlist *evlist,
280 struct perf_evsel *evsel,
281 int cpu, int thread, u64 id)
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200282{
Arnaldo Carvalho de Melo3d3b5e92011-03-04 22:29:39 -0300283 int hash;
284 struct perf_sample_id *sid = SID(evsel, cpu, thread);
285
286 sid->id = id;
287 sid->evsel = evsel;
288 hash = hash_64(sid->id, PERF_EVLIST__HLIST_BITS);
289 hlist_add_head(&sid->node, &evlist->heads[hash]);
290}
291
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -0300292void perf_evlist__id_add(struct perf_evlist *evlist, struct perf_evsel *evsel,
293 int cpu, int thread, u64 id)
294{
295 perf_evlist__id_hash(evlist, evsel, cpu, thread, id);
296 evsel->id[evsel->ids++] = id;
297}
298
299static int perf_evlist__id_add_fd(struct perf_evlist *evlist,
300 struct perf_evsel *evsel,
301 int cpu, int thread, int fd)
Arnaldo Carvalho de Melo3d3b5e92011-03-04 22:29:39 -0300302{
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200303 u64 read_data[4] = { 0, };
Arnaldo Carvalho de Melo3d3b5e92011-03-04 22:29:39 -0300304 int id_idx = 1; /* The first entry is the counter value */
Jiri Olsae2b5abe2012-04-04 19:32:27 +0200305 u64 id;
306 int ret;
307
308 ret = ioctl(fd, PERF_EVENT_IOC_ID, &id);
309 if (!ret)
310 goto add;
311
312 if (errno != ENOTTY)
313 return -1;
314
315 /* Legacy way to get event id.. All hail to old kernels! */
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200316
317 if (!(evsel->attr.read_format & PERF_FORMAT_ID) ||
318 read(fd, &read_data, sizeof(read_data)) == -1)
319 return -1;
320
321 if (evsel->attr.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
322 ++id_idx;
323 if (evsel->attr.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
324 ++id_idx;
325
Jiri Olsae2b5abe2012-04-04 19:32:27 +0200326 id = read_data[id_idx];
327
328 add:
329 perf_evlist__id_add(evlist, evsel, cpu, thread, id);
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200330 return 0;
331}
332
Arnaldo Carvalho de Melo70db7532011-01-12 22:39:13 -0200333struct perf_evsel *perf_evlist__id2evsel(struct perf_evlist *evlist, u64 id)
334{
335 struct hlist_head *head;
Arnaldo Carvalho de Melo70db7532011-01-12 22:39:13 -0200336 struct perf_sample_id *sid;
337 int hash;
338
339 if (evlist->nr_entries == 1)
Arnaldo Carvalho de Melo0c21f732012-08-14 16:42:15 -0300340 return perf_evlist__first(evlist);
Arnaldo Carvalho de Melo70db7532011-01-12 22:39:13 -0200341
342 hash = hash_64(id, PERF_EVLIST__HLIST_BITS);
343 head = &evlist->heads[hash];
344
Sasha Levinb67bfe02013-02-27 17:06:00 -0800345 hlist_for_each_entry(sid, head, node)
Arnaldo Carvalho de Melo70db7532011-01-12 22:39:13 -0200346 if (sid->id == id)
347 return sid->evsel;
Namhyung Kim30e68bc2012-02-20 10:47:26 +0900348
349 if (!perf_evlist__sample_id_all(evlist))
Arnaldo Carvalho de Melo0c21f732012-08-14 16:42:15 -0300350 return perf_evlist__first(evlist);
Namhyung Kim30e68bc2012-02-20 10:47:26 +0900351
Arnaldo Carvalho de Melo70db7532011-01-12 22:39:13 -0200352 return NULL;
353}
Arnaldo Carvalho de Melo04391de2011-01-15 10:40:59 -0200354
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -0300355union perf_event *perf_evlist__mmap_read(struct perf_evlist *evlist, int idx)
Arnaldo Carvalho de Melo04391de2011-01-15 10:40:59 -0200356{
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -0300357 struct perf_mmap *md = &evlist->mmap[idx];
Arnaldo Carvalho de Melo04391de2011-01-15 10:40:59 -0200358 unsigned int head = perf_mmap__read_head(md);
359 unsigned int old = md->prev;
360 unsigned char *data = md->base + page_size;
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200361 union perf_event *event = NULL;
Arnaldo Carvalho de Melo04391de2011-01-15 10:40:59 -0200362
Arnaldo Carvalho de Melo7bb41152011-01-29 09:08:13 -0200363 if (evlist->overwrite) {
Arnaldo Carvalho de Melo04391de2011-01-15 10:40:59 -0200364 /*
Arnaldo Carvalho de Melo7bb41152011-01-29 09:08:13 -0200365 * If we're further behind than half the buffer, there's a chance
366 * the writer will bite our tail and mess up the samples under us.
367 *
368 * If we somehow ended up ahead of the head, we got messed up.
369 *
370 * In either case, truncate and restart at head.
Arnaldo Carvalho de Melo04391de2011-01-15 10:40:59 -0200371 */
Arnaldo Carvalho de Melo7bb41152011-01-29 09:08:13 -0200372 int diff = head - old;
373 if (diff > md->mask / 2 || diff < 0) {
374 fprintf(stderr, "WARNING: failed to keep up with mmap data.\n");
375
376 /*
377 * head points to a known good entry, start there.
378 */
379 old = head;
380 }
Arnaldo Carvalho de Melo04391de2011-01-15 10:40:59 -0200381 }
382
383 if (old != head) {
384 size_t size;
385
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200386 event = (union perf_event *)&data[old & md->mask];
Arnaldo Carvalho de Melo04391de2011-01-15 10:40:59 -0200387 size = event->header.size;
388
389 /*
390 * Event straddles the mmap boundary -- header should always
391 * be inside due to u64 alignment of output.
392 */
393 if ((old & md->mask) + size != ((old + size) & md->mask)) {
394 unsigned int offset = old;
395 unsigned int len = min(sizeof(*event), size), cpy;
David Ahern0479b8b2013-02-05 14:12:42 -0700396 void *dst = &md->event_copy;
Arnaldo Carvalho de Melo04391de2011-01-15 10:40:59 -0200397
398 do {
399 cpy = min(md->mask + 1 - (offset & md->mask), len);
400 memcpy(dst, &data[offset & md->mask], cpy);
401 offset += cpy;
402 dst += cpy;
403 len -= cpy;
404 } while (len);
405
David Ahern0479b8b2013-02-05 14:12:42 -0700406 event = &md->event_copy;
Arnaldo Carvalho de Melo04391de2011-01-15 10:40:59 -0200407 }
408
409 old += size;
410 }
411
412 md->prev = old;
Arnaldo Carvalho de Melo7bb41152011-01-29 09:08:13 -0200413
414 if (!evlist->overwrite)
415 perf_mmap__write_tail(md, old);
416
Arnaldo Carvalho de Melo04391de2011-01-15 10:40:59 -0200417 return event;
418}
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200419
Adrian Hunter93edcbd2013-07-04 16:20:26 +0300420static void __perf_evlist__munmap(struct perf_evlist *evlist, int idx)
421{
422 if (evlist->mmap[idx].base != NULL) {
423 munmap(evlist->mmap[idx].base, evlist->mmap_len);
424 evlist->mmap[idx].base = NULL;
425 }
426}
427
Arnaldo Carvalho de Melo7e2ed092011-01-30 11:59:43 -0200428void perf_evlist__munmap(struct perf_evlist *evlist)
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200429{
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -0300430 int i;
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200431
Adrian Hunter93edcbd2013-07-04 16:20:26 +0300432 for (i = 0; i < evlist->nr_mmaps; i++)
433 __perf_evlist__munmap(evlist, i);
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -0300434
435 free(evlist->mmap);
436 evlist->mmap = NULL;
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200437}
438
Arnaldo Carvalho de Melo806fb632011-11-29 08:05:52 -0200439static int perf_evlist__alloc_mmap(struct perf_evlist *evlist)
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200440{
Arnaldo Carvalho de Meloa14bb7a2012-09-26 12:41:14 -0300441 evlist->nr_mmaps = cpu_map__nr(evlist->cpus);
Sukadev Bhattiproluec1e7e42013-05-22 17:42:38 -0700442 if (cpu_map__empty(evlist->cpus))
Namhyung Kimb3a319d2013-03-11 16:43:14 +0900443 evlist->nr_mmaps = thread_map__nr(evlist->threads);
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -0300444 evlist->mmap = zalloc(evlist->nr_mmaps * sizeof(struct perf_mmap));
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200445 return evlist->mmap != NULL ? 0 : -ENOMEM;
446}
447
Arnaldo Carvalho de Melobccdaba2011-06-02 10:39:43 -0300448static int __perf_evlist__mmap(struct perf_evlist *evlist,
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -0300449 int idx, int prot, int mask, int fd)
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200450{
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -0300451 evlist->mmap[idx].prev = 0;
452 evlist->mmap[idx].mask = mask;
453 evlist->mmap[idx].base = mmap(NULL, evlist->mmap_len, prot,
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200454 MAP_SHARED, fd, 0);
Nelson Elhage301b1952011-12-19 08:39:30 -0500455 if (evlist->mmap[idx].base == MAP_FAILED) {
456 evlist->mmap[idx].base = NULL;
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200457 return -1;
Nelson Elhage301b1952011-12-19 08:39:30 -0500458 }
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200459
460 perf_evlist__add_pollfd(evlist, fd);
461 return 0;
462}
463
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -0300464static int perf_evlist__mmap_per_cpu(struct perf_evlist *evlist, int prot, int mask)
465{
466 struct perf_evsel *evsel;
467 int cpu, thread;
Namhyung Kimb3a319d2013-03-11 16:43:14 +0900468 int nr_cpus = cpu_map__nr(evlist->cpus);
469 int nr_threads = thread_map__nr(evlist->threads);
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -0300470
Namhyung Kimb3a319d2013-03-11 16:43:14 +0900471 for (cpu = 0; cpu < nr_cpus; cpu++) {
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -0300472 int output = -1;
473
Namhyung Kimb3a319d2013-03-11 16:43:14 +0900474 for (thread = 0; thread < nr_threads; thread++) {
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -0300475 list_for_each_entry(evsel, &evlist->entries, node) {
476 int fd = FD(evsel, cpu, thread);
477
478 if (output == -1) {
479 output = fd;
Arnaldo Carvalho de Melobccdaba2011-06-02 10:39:43 -0300480 if (__perf_evlist__mmap(evlist, cpu,
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -0300481 prot, mask, output) < 0)
482 goto out_unmap;
483 } else {
484 if (ioctl(fd, PERF_EVENT_IOC_SET_OUTPUT, output) != 0)
485 goto out_unmap;
486 }
487
488 if ((evsel->attr.read_format & PERF_FORMAT_ID) &&
489 perf_evlist__id_add_fd(evlist, evsel, cpu, thread, fd) < 0)
490 goto out_unmap;
491 }
492 }
493 }
494
495 return 0;
496
497out_unmap:
Adrian Hunter93edcbd2013-07-04 16:20:26 +0300498 for (cpu = 0; cpu < nr_cpus; cpu++)
499 __perf_evlist__munmap(evlist, cpu);
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -0300500 return -1;
501}
502
503static int perf_evlist__mmap_per_thread(struct perf_evlist *evlist, int prot, int mask)
504{
505 struct perf_evsel *evsel;
506 int thread;
Namhyung Kimb3a319d2013-03-11 16:43:14 +0900507 int nr_threads = thread_map__nr(evlist->threads);
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -0300508
Namhyung Kimb3a319d2013-03-11 16:43:14 +0900509 for (thread = 0; thread < nr_threads; thread++) {
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -0300510 int output = -1;
511
512 list_for_each_entry(evsel, &evlist->entries, node) {
513 int fd = FD(evsel, 0, thread);
514
515 if (output == -1) {
516 output = fd;
Arnaldo Carvalho de Melobccdaba2011-06-02 10:39:43 -0300517 if (__perf_evlist__mmap(evlist, thread,
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -0300518 prot, mask, output) < 0)
519 goto out_unmap;
520 } else {
521 if (ioctl(fd, PERF_EVENT_IOC_SET_OUTPUT, output) != 0)
522 goto out_unmap;
523 }
524
525 if ((evsel->attr.read_format & PERF_FORMAT_ID) &&
526 perf_evlist__id_add_fd(evlist, evsel, 0, thread, fd) < 0)
527 goto out_unmap;
528 }
529 }
530
531 return 0;
532
533out_unmap:
Adrian Hunter93edcbd2013-07-04 16:20:26 +0300534 for (thread = 0; thread < nr_threads; thread++)
535 __perf_evlist__munmap(evlist, thread);
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -0300536 return -1;
537}
538
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200539/** perf_evlist__mmap - Create per cpu maps to receive events
540 *
541 * @evlist - list of events
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200542 * @pages - map length in pages
543 * @overwrite - overwrite older events?
544 *
545 * If overwrite is false the user needs to signal event consuption using:
546 *
547 * struct perf_mmap *m = &evlist->mmap[cpu];
548 * unsigned int head = perf_mmap__read_head(m);
549 *
550 * perf_mmap__write_tail(m, head)
Arnaldo Carvalho de Melo7e2ed092011-01-30 11:59:43 -0200551 *
552 * Using perf_evlist__read_on_cpu does this automatically.
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200553 */
Arnaldo Carvalho de Melo50a682c2011-11-09 09:10:47 -0200554int perf_evlist__mmap(struct perf_evlist *evlist, unsigned int pages,
555 bool overwrite)
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200556{
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -0300557 struct perf_evsel *evsel;
Arnaldo Carvalho de Melo7e2ed092011-01-30 11:59:43 -0200558 const struct cpu_map *cpus = evlist->cpus;
559 const struct thread_map *threads = evlist->threads;
Arnaldo Carvalho de Melo50a682c2011-11-09 09:10:47 -0200560 int prot = PROT_READ | (overwrite ? 0 : PROT_WRITE), mask;
561
562 /* 512 kiB: default amount of unprivileged mlocked memory */
563 if (pages == UINT_MAX)
564 pages = (512 * 1024) / page_size;
Nelson Elhage41d0d932011-12-19 08:39:32 -0500565 else if (!is_power_of_2(pages))
566 return -EINVAL;
Arnaldo Carvalho de Melo50a682c2011-11-09 09:10:47 -0200567
568 mask = pages * page_size - 1;
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200569
Arnaldo Carvalho de Melo7e2ed092011-01-30 11:59:43 -0200570 if (evlist->mmap == NULL && perf_evlist__alloc_mmap(evlist) < 0)
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200571 return -ENOMEM;
572
Arnaldo Carvalho de Melo7e2ed092011-01-30 11:59:43 -0200573 if (evlist->pollfd == NULL && perf_evlist__alloc_pollfd(evlist) < 0)
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200574 return -ENOMEM;
575
576 evlist->overwrite = overwrite;
577 evlist->mmap_len = (pages + 1) * page_size;
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200578
579 list_for_each_entry(evsel, &evlist->entries, node) {
580 if ((evsel->attr.read_format & PERF_FORMAT_ID) &&
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -0300581 evsel->sample_id == NULL &&
Arnaldo Carvalho de Meloa14bb7a2012-09-26 12:41:14 -0300582 perf_evsel__alloc_id(evsel, cpu_map__nr(cpus), threads->nr) < 0)
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200583 return -ENOMEM;
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200584 }
585
Sukadev Bhattiproluec1e7e42013-05-22 17:42:38 -0700586 if (cpu_map__empty(cpus))
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -0300587 return perf_evlist__mmap_per_thread(evlist, prot, mask);
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200588
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -0300589 return perf_evlist__mmap_per_cpu(evlist, prot, mask);
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200590}
Arnaldo Carvalho de Melo7e2ed092011-01-30 11:59:43 -0200591
Namhyung Kimb809ac12012-04-26 14:15:19 +0900592int perf_evlist__create_maps(struct perf_evlist *evlist,
593 struct perf_target *target)
Arnaldo Carvalho de Melo7e2ed092011-01-30 11:59:43 -0200594{
Namhyung Kimb809ac12012-04-26 14:15:19 +0900595 evlist->threads = thread_map__new_str(target->pid, target->tid,
596 target->uid);
Arnaldo Carvalho de Melo7e2ed092011-01-30 11:59:43 -0200597
598 if (evlist->threads == NULL)
599 return -1;
600
Namhyung Kim879d77d2012-05-16 18:45:48 +0900601 if (perf_target__has_task(target))
Namhyung Kimd67356e2012-05-07 14:09:03 +0900602 evlist->cpus = cpu_map__dummy_new();
Namhyung Kimd1cb9fc2012-05-16 18:45:49 +0900603 else if (!perf_target__has_cpu(target) && !target->uses_mmap)
604 evlist->cpus = cpu_map__dummy_new();
Namhyung Kim879d77d2012-05-16 18:45:48 +0900605 else
606 evlist->cpus = cpu_map__new(target->cpu_list);
Arnaldo Carvalho de Melo7e2ed092011-01-30 11:59:43 -0200607
608 if (evlist->cpus == NULL)
609 goto out_delete_threads;
610
611 return 0;
612
613out_delete_threads:
614 thread_map__delete(evlist->threads);
615 return -1;
616}
617
618void perf_evlist__delete_maps(struct perf_evlist *evlist)
619{
620 cpu_map__delete(evlist->cpus);
621 thread_map__delete(evlist->threads);
622 evlist->cpus = NULL;
623 evlist->threads = NULL;
624}
Frederic Weisbecker0a102472011-02-26 04:51:54 +0100625
Arnaldo Carvalho de Melo1491a632012-09-26 14:43:13 -0300626int perf_evlist__apply_filters(struct perf_evlist *evlist)
Frederic Weisbecker0a102472011-02-26 04:51:54 +0100627{
Frederic Weisbecker0a102472011-02-26 04:51:54 +0100628 struct perf_evsel *evsel;
Arnaldo Carvalho de Melo745cefc2012-09-26 15:07:39 -0300629 int err = 0;
630 const int ncpus = cpu_map__nr(evlist->cpus),
Namhyung Kimb3a319d2013-03-11 16:43:14 +0900631 nthreads = thread_map__nr(evlist->threads);
Frederic Weisbecker0a102472011-02-26 04:51:54 +0100632
633 list_for_each_entry(evsel, &evlist->entries, node) {
Arnaldo Carvalho de Melo745cefc2012-09-26 15:07:39 -0300634 if (evsel->filter == NULL)
Frederic Weisbecker0a102472011-02-26 04:51:54 +0100635 continue;
Arnaldo Carvalho de Melo745cefc2012-09-26 15:07:39 -0300636
637 err = perf_evsel__set_filter(evsel, ncpus, nthreads, evsel->filter);
638 if (err)
639 break;
Frederic Weisbecker0a102472011-02-26 04:51:54 +0100640 }
641
Arnaldo Carvalho de Melo745cefc2012-09-26 15:07:39 -0300642 return err;
643}
644
645int perf_evlist__set_filter(struct perf_evlist *evlist, const char *filter)
646{
647 struct perf_evsel *evsel;
648 int err = 0;
649 const int ncpus = cpu_map__nr(evlist->cpus),
Namhyung Kimb3a319d2013-03-11 16:43:14 +0900650 nthreads = thread_map__nr(evlist->threads);
Arnaldo Carvalho de Melo745cefc2012-09-26 15:07:39 -0300651
652 list_for_each_entry(evsel, &evlist->entries, node) {
653 err = perf_evsel__set_filter(evsel, ncpus, nthreads, filter);
654 if (err)
655 break;
656 }
657
658 return err;
Frederic Weisbecker0a102472011-02-26 04:51:54 +0100659}
Frederic Weisbecker74429962011-05-21 17:49:00 +0200660
Arnaldo Carvalho de Melo0c21f732012-08-14 16:42:15 -0300661bool perf_evlist__valid_sample_type(struct perf_evlist *evlist)
Frederic Weisbecker74429962011-05-21 17:49:00 +0200662{
Arnaldo Carvalho de Melo0c21f732012-08-14 16:42:15 -0300663 struct perf_evsel *first = perf_evlist__first(evlist), *pos = first;
Arnaldo Carvalho de Meloc2a70652011-06-02 11:04:54 -0300664
665 list_for_each_entry_continue(pos, &evlist->entries, node) {
666 if (first->attr.sample_type != pos->attr.sample_type)
667 return false;
Frederic Weisbecker74429962011-05-21 17:49:00 +0200668 }
669
Arnaldo Carvalho de Meloc2a70652011-06-02 11:04:54 -0300670 return true;
671}
672
Arnaldo Carvalho de Melo0c21f732012-08-14 16:42:15 -0300673u64 perf_evlist__sample_type(struct perf_evlist *evlist)
Arnaldo Carvalho de Meloc2a70652011-06-02 11:04:54 -0300674{
Arnaldo Carvalho de Melo0c21f732012-08-14 16:42:15 -0300675 struct perf_evsel *first = perf_evlist__first(evlist);
Arnaldo Carvalho de Meloc2a70652011-06-02 11:04:54 -0300676 return first->attr.sample_type;
677}
678
Arnaldo Carvalho de Melo0c21f732012-08-14 16:42:15 -0300679u16 perf_evlist__id_hdr_size(struct perf_evlist *evlist)
Arnaldo Carvalho de Melo81e36bf2011-11-11 22:28:50 -0200680{
Arnaldo Carvalho de Melo0c21f732012-08-14 16:42:15 -0300681 struct perf_evsel *first = perf_evlist__first(evlist);
Arnaldo Carvalho de Melo81e36bf2011-11-11 22:28:50 -0200682 struct perf_sample *data;
683 u64 sample_type;
684 u16 size = 0;
685
Arnaldo Carvalho de Melo81e36bf2011-11-11 22:28:50 -0200686 if (!first->attr.sample_id_all)
687 goto out;
688
689 sample_type = first->attr.sample_type;
690
691 if (sample_type & PERF_SAMPLE_TID)
692 size += sizeof(data->tid) * 2;
693
694 if (sample_type & PERF_SAMPLE_TIME)
695 size += sizeof(data->time);
696
697 if (sample_type & PERF_SAMPLE_ID)
698 size += sizeof(data->id);
699
700 if (sample_type & PERF_SAMPLE_STREAM_ID)
701 size += sizeof(data->stream_id);
702
703 if (sample_type & PERF_SAMPLE_CPU)
704 size += sizeof(data->cpu) * 2;
705out:
706 return size;
707}
708
Arnaldo Carvalho de Melo0c21f732012-08-14 16:42:15 -0300709bool perf_evlist__valid_sample_id_all(struct perf_evlist *evlist)
Arnaldo Carvalho de Meloc2a70652011-06-02 11:04:54 -0300710{
Arnaldo Carvalho de Melo0c21f732012-08-14 16:42:15 -0300711 struct perf_evsel *first = perf_evlist__first(evlist), *pos = first;
Arnaldo Carvalho de Meloc2a70652011-06-02 11:04:54 -0300712
713 list_for_each_entry_continue(pos, &evlist->entries, node) {
714 if (first->attr.sample_id_all != pos->attr.sample_id_all)
715 return false;
716 }
717
718 return true;
Frederic Weisbecker74429962011-05-21 17:49:00 +0200719}
720
Arnaldo Carvalho de Melo0c21f732012-08-14 16:42:15 -0300721bool perf_evlist__sample_id_all(struct perf_evlist *evlist)
Frederic Weisbecker74429962011-05-21 17:49:00 +0200722{
Arnaldo Carvalho de Melo0c21f732012-08-14 16:42:15 -0300723 struct perf_evsel *first = perf_evlist__first(evlist);
Arnaldo Carvalho de Meloc2a70652011-06-02 11:04:54 -0300724 return first->attr.sample_id_all;
Frederic Weisbecker74429962011-05-21 17:49:00 +0200725}
Arnaldo Carvalho de Melo81cce8d2011-10-05 19:11:32 -0300726
727void perf_evlist__set_selected(struct perf_evlist *evlist,
728 struct perf_evsel *evsel)
729{
730 evlist->selected = evsel;
731}
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -0200732
Namhyung Kima74b4b62013-03-15 14:48:48 +0900733void perf_evlist__close(struct perf_evlist *evlist)
734{
735 struct perf_evsel *evsel;
736 int ncpus = cpu_map__nr(evlist->cpus);
737 int nthreads = thread_map__nr(evlist->threads);
738
739 list_for_each_entry_reverse(evsel, &evlist->entries, node)
740 perf_evsel__close(evsel, ncpus, nthreads);
741}
742
Jiri Olsa6a4bb042012-08-08 12:22:36 +0200743int perf_evlist__open(struct perf_evlist *evlist)
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -0200744{
Jiri Olsa6a4bb042012-08-08 12:22:36 +0200745 struct perf_evsel *evsel;
Namhyung Kima74b4b62013-03-15 14:48:48 +0900746 int err;
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -0200747
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -0200748 list_for_each_entry(evsel, &evlist->entries, node) {
Jiri Olsa6a4bb042012-08-08 12:22:36 +0200749 err = perf_evsel__open(evsel, evlist->cpus, evlist->threads);
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -0200750 if (err < 0)
751 goto out_err;
752 }
753
754 return 0;
755out_err:
Namhyung Kima74b4b62013-03-15 14:48:48 +0900756 perf_evlist__close(evlist);
Namhyung Kim41c21a62012-02-23 12:13:36 +0900757 errno = -err;
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -0200758 return err;
759}
Arnaldo Carvalho de Melo35b9d882011-11-09 08:47:15 -0200760
761int perf_evlist__prepare_workload(struct perf_evlist *evlist,
Namhyung Kim6ef73ec2013-03-11 16:43:15 +0900762 struct perf_target *target,
Namhyung Kim55e162e2013-03-11 16:43:17 +0900763 const char *argv[], bool pipe_output,
764 bool want_signal)
Arnaldo Carvalho de Melo35b9d882011-11-09 08:47:15 -0200765{
766 int child_ready_pipe[2], go_pipe[2];
767 char bf;
768
769 if (pipe(child_ready_pipe) < 0) {
770 perror("failed to create 'ready' pipe");
771 return -1;
772 }
773
774 if (pipe(go_pipe) < 0) {
775 perror("failed to create 'go' pipe");
776 goto out_close_ready_pipe;
777 }
778
779 evlist->workload.pid = fork();
780 if (evlist->workload.pid < 0) {
781 perror("failed to fork");
782 goto out_close_pipes;
783 }
784
785 if (!evlist->workload.pid) {
Namhyung Kim119fa3c2013-03-11 16:43:16 +0900786 if (pipe_output)
Arnaldo Carvalho de Melo35b9d882011-11-09 08:47:15 -0200787 dup2(2, 1);
788
David Ahern0817df02013-05-25 17:50:39 -0600789 signal(SIGTERM, SIG_DFL);
790
Arnaldo Carvalho de Melo35b9d882011-11-09 08:47:15 -0200791 close(child_ready_pipe[0]);
792 close(go_pipe[1]);
793 fcntl(go_pipe[0], F_SETFD, FD_CLOEXEC);
794
795 /*
796 * Do a dummy execvp to get the PLT entry resolved,
797 * so we avoid the resolver overhead on the real
798 * execvp call.
799 */
800 execvp("", (char **)argv);
801
802 /*
803 * Tell the parent we're ready to go
804 */
805 close(child_ready_pipe[1]);
806
807 /*
808 * Wait until the parent tells us to go.
809 */
810 if (read(go_pipe[0], &bf, 1) == -1)
811 perror("unable to read pipe");
812
813 execvp(argv[0], (char **)argv);
814
815 perror(argv[0]);
Namhyung Kim55e162e2013-03-11 16:43:17 +0900816 if (want_signal)
817 kill(getppid(), SIGUSR1);
Arnaldo Carvalho de Melo35b9d882011-11-09 08:47:15 -0200818 exit(-1);
819 }
820
Namhyung Kim6ef73ec2013-03-11 16:43:15 +0900821 if (perf_target__none(target))
Arnaldo Carvalho de Melo35b9d882011-11-09 08:47:15 -0200822 evlist->threads->map[0] = evlist->workload.pid;
823
824 close(child_ready_pipe[1]);
825 close(go_pipe[0]);
826 /*
827 * wait for child to settle
828 */
829 if (read(child_ready_pipe[0], &bf, 1) == -1) {
830 perror("unable to read pipe");
831 goto out_close_pipes;
832 }
833
Namhyung Kimbcf31452013-06-26 16:14:15 +0900834 fcntl(go_pipe[1], F_SETFD, FD_CLOEXEC);
Arnaldo Carvalho de Melo35b9d882011-11-09 08:47:15 -0200835 evlist->workload.cork_fd = go_pipe[1];
836 close(child_ready_pipe[0]);
837 return 0;
838
839out_close_pipes:
840 close(go_pipe[0]);
841 close(go_pipe[1]);
842out_close_ready_pipe:
843 close(child_ready_pipe[0]);
844 close(child_ready_pipe[1]);
845 return -1;
846}
847
848int perf_evlist__start_workload(struct perf_evlist *evlist)
849{
850 if (evlist->workload.cork_fd > 0) {
David Ahernb3824402013-07-02 13:27:21 -0600851 char bf = 0;
Namhyung Kimbcf31452013-06-26 16:14:15 +0900852 int ret;
Arnaldo Carvalho de Melo35b9d882011-11-09 08:47:15 -0200853 /*
854 * Remove the cork, let it rip!
855 */
Namhyung Kimbcf31452013-06-26 16:14:15 +0900856 ret = write(evlist->workload.cork_fd, &bf, 1);
857 if (ret < 0)
858 perror("enable to write to pipe");
859
860 close(evlist->workload.cork_fd);
861 return ret;
Arnaldo Carvalho de Melo35b9d882011-11-09 08:47:15 -0200862 }
863
864 return 0;
865}
Arnaldo Carvalho de Melocb0b29e2012-08-02 11:42:57 -0300866
Arnaldo Carvalho de Meloa3f698f2012-08-02 12:23:46 -0300867int perf_evlist__parse_sample(struct perf_evlist *evlist, union perf_event *event,
Arnaldo Carvalho de Melo0807d2d2012-09-26 12:48:18 -0300868 struct perf_sample *sample)
Arnaldo Carvalho de Melocb0b29e2012-08-02 11:42:57 -0300869{
Arnaldo Carvalho de Melo0c21f732012-08-14 16:42:15 -0300870 struct perf_evsel *evsel = perf_evlist__first(evlist);
Arnaldo Carvalho de Melo0807d2d2012-09-26 12:48:18 -0300871 return perf_evsel__parse_sample(evsel, event, sample);
Arnaldo Carvalho de Melocb0b29e2012-08-02 11:42:57 -0300872}
Arnaldo Carvalho de Melo78f067b2012-09-06 14:54:11 -0300873
874size_t perf_evlist__fprintf(struct perf_evlist *evlist, FILE *fp)
875{
876 struct perf_evsel *evsel;
877 size_t printed = 0;
878
879 list_for_each_entry(evsel, &evlist->entries, node) {
880 printed += fprintf(fp, "%s%s", evsel->idx ? ", " : "",
881 perf_evsel__name(evsel));
882 }
883
884 return printed + fprintf(fp, "\n");;
885}