blob: f9f77bee0b1b416414fa3561fb2eee4785e502c4 [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"
Adrian Huntere3e1a542013-08-14 15:48:24 +030017#include "debug.h"
Arnaldo Carvalho de Melo35b9d882011-11-09 08:47:15 -020018#include <unistd.h>
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -020019
Arnaldo Carvalho de Melo50d08e42011-11-04 09:10:59 -020020#include "parse-events.h"
21
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -020022#include <sys/mman.h>
23
Arnaldo Carvalho de Melo70db7532011-01-12 22:39:13 -020024#include <linux/bitops.h>
25#include <linux/hash.h>
26
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -020027#define FD(e, x, y) (*(int *)xyarray__entry(e->fd, x, y))
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -030028#define SID(e, x, y) xyarray__entry(e->sample_id, x, y)
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -020029
Arnaldo Carvalho de Melo7e2ed092011-01-30 11:59:43 -020030void perf_evlist__init(struct perf_evlist *evlist, struct cpu_map *cpus,
31 struct thread_map *threads)
Arnaldo Carvalho de Meloef1d1af2011-01-18 21:41:45 -020032{
33 int i;
34
35 for (i = 0; i < PERF_EVLIST__HLIST_SIZE; ++i)
36 INIT_HLIST_HEAD(&evlist->heads[i]);
37 INIT_LIST_HEAD(&evlist->entries);
Arnaldo Carvalho de Melo7e2ed092011-01-30 11:59:43 -020038 perf_evlist__set_maps(evlist, cpus, threads);
Arnaldo Carvalho de Melo35b9d882011-11-09 08:47:15 -020039 evlist->workload.pid = -1;
Arnaldo Carvalho de Meloef1d1af2011-01-18 21:41:45 -020040}
41
Namhyung Kim334fe7a2013-03-11 16:43:12 +090042struct perf_evlist *perf_evlist__new(void)
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -020043{
44 struct perf_evlist *evlist = zalloc(sizeof(*evlist));
45
Arnaldo Carvalho de Meloef1d1af2011-01-18 21:41:45 -020046 if (evlist != NULL)
Namhyung Kim334fe7a2013-03-11 16:43:12 +090047 perf_evlist__init(evlist, NULL, NULL);
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -020048
49 return evlist;
50}
51
Adrian Hunter75562572013-08-27 11:23:09 +030052/**
53 * perf_evlist__set_id_pos - set the positions of event ids.
54 * @evlist: selected event list
55 *
56 * Events with compatible sample types all have the same id_pos
57 * and is_pos. For convenience, put a copy on evlist.
58 */
59void perf_evlist__set_id_pos(struct perf_evlist *evlist)
60{
61 struct perf_evsel *first = perf_evlist__first(evlist);
62
63 evlist->id_pos = first->id_pos;
64 evlist->is_pos = first->is_pos;
65}
66
Adrian Hunter733cd2f2013-09-06 22:40:11 +030067static void perf_evlist__update_id_pos(struct perf_evlist *evlist)
68{
69 struct perf_evsel *evsel;
70
71 list_for_each_entry(evsel, &evlist->entries, node)
72 perf_evsel__calc_id_pos(evsel);
73
74 perf_evlist__set_id_pos(evlist);
75}
76
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -020077static void perf_evlist__purge(struct perf_evlist *evlist)
78{
79 struct perf_evsel *pos, *n;
80
81 list_for_each_entry_safe(pos, n, &evlist->entries, node) {
82 list_del_init(&pos->node);
83 perf_evsel__delete(pos);
84 }
85
86 evlist->nr_entries = 0;
87}
88
Arnaldo Carvalho de Meloef1d1af2011-01-18 21:41:45 -020089void perf_evlist__exit(struct perf_evlist *evlist)
90{
91 free(evlist->mmap);
92 free(evlist->pollfd);
93 evlist->mmap = NULL;
94 evlist->pollfd = NULL;
95}
96
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -020097void perf_evlist__delete(struct perf_evlist *evlist)
98{
99 perf_evlist__purge(evlist);
Arnaldo Carvalho de Meloef1d1af2011-01-18 21:41:45 -0200100 perf_evlist__exit(evlist);
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -0200101 free(evlist);
102}
103
104void perf_evlist__add(struct perf_evlist *evlist, struct perf_evsel *entry)
105{
106 list_add_tail(&entry->node, &evlist->entries);
Adrian Hunter75562572013-08-27 11:23:09 +0300107 if (!evlist->nr_entries++)
108 perf_evlist__set_id_pos(evlist);
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -0200109}
110
Jiri Olsa0529bc12012-01-27 15:34:20 +0100111void perf_evlist__splice_list_tail(struct perf_evlist *evlist,
112 struct list_head *list,
113 int nr_entries)
Arnaldo Carvalho de Melo50d08e42011-11-04 09:10:59 -0200114{
Adrian Hunter75562572013-08-27 11:23:09 +0300115 bool set_id_pos = !evlist->nr_entries;
116
Arnaldo Carvalho de Melo50d08e42011-11-04 09:10:59 -0200117 list_splice_tail(list, &evlist->entries);
118 evlist->nr_entries += nr_entries;
Adrian Hunter75562572013-08-27 11:23:09 +0300119 if (set_id_pos)
120 perf_evlist__set_id_pos(evlist);
Arnaldo Carvalho de Melo50d08e42011-11-04 09:10:59 -0200121}
122
Arnaldo Carvalho de Melo63dab222012-08-14 16:35:48 -0300123void __perf_evlist__set_leader(struct list_head *list)
124{
125 struct perf_evsel *evsel, *leader;
126
127 leader = list_entry(list->next, struct perf_evsel, node);
Namhyung Kim97f63e42013-01-22 18:09:29 +0900128 evsel = list_entry(list->prev, struct perf_evsel, node);
129
130 leader->nr_members = evsel->idx - leader->idx + 1;
Arnaldo Carvalho de Melo63dab222012-08-14 16:35:48 -0300131
132 list_for_each_entry(evsel, list, node) {
Stephane Eranian74b21332013-01-31 13:54:37 +0100133 evsel->leader = leader;
Arnaldo Carvalho de Melo63dab222012-08-14 16:35:48 -0300134 }
135}
136
137void perf_evlist__set_leader(struct perf_evlist *evlist)
Jiri Olsa6a4bb042012-08-08 12:22:36 +0200138{
Namhyung Kim97f63e42013-01-22 18:09:29 +0900139 if (evlist->nr_entries) {
140 evlist->nr_groups = evlist->nr_entries > 1 ? 1 : 0;
Arnaldo Carvalho de Melo63dab222012-08-14 16:35:48 -0300141 __perf_evlist__set_leader(&evlist->entries);
Namhyung Kim97f63e42013-01-22 18:09:29 +0900142 }
Jiri Olsa6a4bb042012-08-08 12:22:36 +0200143}
144
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -0200145int perf_evlist__add_default(struct perf_evlist *evlist)
146{
147 struct perf_event_attr attr = {
148 .type = PERF_TYPE_HARDWARE,
149 .config = PERF_COUNT_HW_CPU_CYCLES,
150 };
Joerg Roedel1aed2672012-01-04 17:54:20 +0100151 struct perf_evsel *evsel;
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -0200152
Joerg Roedel1aed2672012-01-04 17:54:20 +0100153 event_attr_init(&attr);
154
155 evsel = perf_evsel__new(&attr, 0);
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -0200156 if (evsel == NULL)
Stephane Eraniancc2d86b2011-06-07 18:19:36 +0200157 goto error;
158
159 /* use strdup() because free(evsel) assumes name is allocated */
160 evsel->name = strdup("cycles");
161 if (!evsel->name)
162 goto error_free;
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -0200163
164 perf_evlist__add(evlist, evsel);
165 return 0;
Stephane Eraniancc2d86b2011-06-07 18:19:36 +0200166error_free:
167 perf_evsel__delete(evsel);
168error:
169 return -ENOMEM;
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -0200170}
Arnaldo Carvalho de Melo5c581042011-01-11 22:30:02 -0200171
Arnaldo Carvalho de Meloe60fc842012-10-03 11:50:55 -0300172static int perf_evlist__add_attrs(struct perf_evlist *evlist,
173 struct perf_event_attr *attrs, size_t nr_attrs)
Arnaldo Carvalho de Melo50d08e42011-11-04 09:10:59 -0200174{
175 struct perf_evsel *evsel, *n;
176 LIST_HEAD(head);
177 size_t i;
178
179 for (i = 0; i < nr_attrs; i++) {
180 evsel = perf_evsel__new(attrs + i, evlist->nr_entries + i);
181 if (evsel == NULL)
182 goto out_delete_partial_list;
183 list_add_tail(&evsel->node, &head);
184 }
185
186 perf_evlist__splice_list_tail(evlist, &head, nr_attrs);
187
188 return 0;
189
190out_delete_partial_list:
191 list_for_each_entry_safe(evsel, n, &head, node)
192 perf_evsel__delete(evsel);
193 return -1;
194}
195
Arnaldo Carvalho de Melo79695e12012-05-30 13:53:54 -0300196int __perf_evlist__add_default_attrs(struct perf_evlist *evlist,
197 struct perf_event_attr *attrs, size_t nr_attrs)
198{
199 size_t i;
200
201 for (i = 0; i < nr_attrs; i++)
202 event_attr_init(attrs + i);
203
204 return perf_evlist__add_attrs(evlist, attrs, nr_attrs);
205}
206
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -0300207struct perf_evsel *
208perf_evlist__find_tracepoint_by_id(struct perf_evlist *evlist, int id)
Arnaldo Carvalho de Meloee29be62011-11-28 17:57:40 -0200209{
210 struct perf_evsel *evsel;
211
212 list_for_each_entry(evsel, &evlist->entries, node) {
213 if (evsel->attr.type == PERF_TYPE_TRACEPOINT &&
214 (int)evsel->attr.config == id)
215 return evsel;
216 }
217
218 return NULL;
219}
220
David Aherna2f28042013-08-28 22:29:51 -0600221struct perf_evsel *
222perf_evlist__find_tracepoint_by_name(struct perf_evlist *evlist,
223 const char *name)
224{
225 struct perf_evsel *evsel;
226
227 list_for_each_entry(evsel, &evlist->entries, node) {
228 if ((evsel->attr.type == PERF_TYPE_TRACEPOINT) &&
229 (strcmp(evsel->name, name) == 0))
230 return evsel;
231 }
232
233 return NULL;
234}
235
Arnaldo Carvalho de Melo39876e72012-10-03 11:40:22 -0300236int perf_evlist__add_newtp(struct perf_evlist *evlist,
237 const char *sys, const char *name, void *handler)
238{
239 struct perf_evsel *evsel;
240
241 evsel = perf_evsel__newtp(sys, name, evlist->nr_entries);
242 if (evsel == NULL)
243 return -1;
244
245 evsel->handler.func = handler;
246 perf_evlist__add(evlist, evsel);
247 return 0;
248}
249
Arnaldo Carvalho de Melo4152ab32011-07-25 11:06:19 -0300250void perf_evlist__disable(struct perf_evlist *evlist)
251{
252 int cpu, thread;
253 struct perf_evsel *pos;
Namhyung Kimb3a319d2013-03-11 16:43:14 +0900254 int nr_cpus = cpu_map__nr(evlist->cpus);
255 int nr_threads = thread_map__nr(evlist->threads);
Arnaldo Carvalho de Melo4152ab32011-07-25 11:06:19 -0300256
Namhyung Kimb3a319d2013-03-11 16:43:14 +0900257 for (cpu = 0; cpu < nr_cpus; cpu++) {
Arnaldo Carvalho de Melo4152ab32011-07-25 11:06:19 -0300258 list_for_each_entry(pos, &evlist->entries, node) {
Adrian Hunter395c3072013-08-31 21:50:53 +0300259 if (!perf_evsel__is_group_leader(pos) || !pos->fd)
Jiri Olsa3fe4430d2012-11-12 18:34:03 +0100260 continue;
Namhyung Kimb3a319d2013-03-11 16:43:14 +0900261 for (thread = 0; thread < nr_threads; thread++)
Namhyung Kim55da8002012-05-31 14:51:46 +0900262 ioctl(FD(pos, cpu, thread),
263 PERF_EVENT_IOC_DISABLE, 0);
Arnaldo Carvalho de Melo4152ab32011-07-25 11:06:19 -0300264 }
265 }
266}
267
David Ahern764e16a32011-08-25 10:17:55 -0600268void perf_evlist__enable(struct perf_evlist *evlist)
269{
270 int cpu, thread;
271 struct perf_evsel *pos;
Namhyung Kimb3a319d2013-03-11 16:43:14 +0900272 int nr_cpus = cpu_map__nr(evlist->cpus);
273 int nr_threads = thread_map__nr(evlist->threads);
David Ahern764e16a32011-08-25 10:17:55 -0600274
Namhyung Kimb3a319d2013-03-11 16:43:14 +0900275 for (cpu = 0; cpu < nr_cpus; cpu++) {
David Ahern764e16a32011-08-25 10:17:55 -0600276 list_for_each_entry(pos, &evlist->entries, node) {
Adrian Hunter395c3072013-08-31 21:50:53 +0300277 if (!perf_evsel__is_group_leader(pos) || !pos->fd)
Jiri Olsa3fe4430d2012-11-12 18:34:03 +0100278 continue;
Namhyung Kimb3a319d2013-03-11 16:43:14 +0900279 for (thread = 0; thread < nr_threads; thread++)
Namhyung Kim55da8002012-05-31 14:51:46 +0900280 ioctl(FD(pos, cpu, thread),
281 PERF_EVENT_IOC_ENABLE, 0);
David Ahern764e16a32011-08-25 10:17:55 -0600282 }
283 }
284}
285
Adrian Hunter395c3072013-08-31 21:50:53 +0300286int perf_evlist__disable_event(struct perf_evlist *evlist,
287 struct perf_evsel *evsel)
288{
289 int cpu, thread, err;
290
291 if (!evsel->fd)
292 return 0;
293
294 for (cpu = 0; cpu < evlist->cpus->nr; cpu++) {
295 for (thread = 0; thread < evlist->threads->nr; thread++) {
296 err = ioctl(FD(evsel, cpu, thread),
297 PERF_EVENT_IOC_DISABLE, 0);
298 if (err)
299 return err;
300 }
301 }
302 return 0;
303}
304
305int perf_evlist__enable_event(struct perf_evlist *evlist,
306 struct perf_evsel *evsel)
307{
308 int cpu, thread, err;
309
310 if (!evsel->fd)
311 return -EINVAL;
312
313 for (cpu = 0; cpu < evlist->cpus->nr; cpu++) {
314 for (thread = 0; thread < evlist->threads->nr; thread++) {
315 err = ioctl(FD(evsel, cpu, thread),
316 PERF_EVENT_IOC_ENABLE, 0);
317 if (err)
318 return err;
319 }
320 }
321 return 0;
322}
323
Arnaldo Carvalho de Melo806fb632011-11-29 08:05:52 -0200324static int perf_evlist__alloc_pollfd(struct perf_evlist *evlist)
Arnaldo Carvalho de Melo5c581042011-01-11 22:30:02 -0200325{
Namhyung Kimb3a319d2013-03-11 16:43:14 +0900326 int nr_cpus = cpu_map__nr(evlist->cpus);
327 int nr_threads = thread_map__nr(evlist->threads);
328 int nfds = nr_cpus * nr_threads * evlist->nr_entries;
Arnaldo Carvalho de Melo5c581042011-01-11 22:30:02 -0200329 evlist->pollfd = malloc(sizeof(struct pollfd) * nfds);
330 return evlist->pollfd != NULL ? 0 : -ENOMEM;
331}
Arnaldo Carvalho de Melo70082dd2011-01-12 17:03:24 -0200332
333void perf_evlist__add_pollfd(struct perf_evlist *evlist, int fd)
334{
335 fcntl(fd, F_SETFL, O_NONBLOCK);
336 evlist->pollfd[evlist->nr_fds].fd = fd;
337 evlist->pollfd[evlist->nr_fds].events = POLLIN;
338 evlist->nr_fds++;
339}
Arnaldo Carvalho de Melo70db7532011-01-12 22:39:13 -0200340
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -0300341static void perf_evlist__id_hash(struct perf_evlist *evlist,
342 struct perf_evsel *evsel,
343 int cpu, int thread, u64 id)
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200344{
Arnaldo Carvalho de Melo3d3b5e92011-03-04 22:29:39 -0300345 int hash;
346 struct perf_sample_id *sid = SID(evsel, cpu, thread);
347
348 sid->id = id;
349 sid->evsel = evsel;
350 hash = hash_64(sid->id, PERF_EVLIST__HLIST_BITS);
351 hlist_add_head(&sid->node, &evlist->heads[hash]);
352}
353
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -0300354void perf_evlist__id_add(struct perf_evlist *evlist, struct perf_evsel *evsel,
355 int cpu, int thread, u64 id)
356{
357 perf_evlist__id_hash(evlist, evsel, cpu, thread, id);
358 evsel->id[evsel->ids++] = id;
359}
360
361static int perf_evlist__id_add_fd(struct perf_evlist *evlist,
362 struct perf_evsel *evsel,
363 int cpu, int thread, int fd)
Arnaldo Carvalho de Melo3d3b5e92011-03-04 22:29:39 -0300364{
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200365 u64 read_data[4] = { 0, };
Arnaldo Carvalho de Melo3d3b5e92011-03-04 22:29:39 -0300366 int id_idx = 1; /* The first entry is the counter value */
Jiri Olsae2b5abe2012-04-04 19:32:27 +0200367 u64 id;
368 int ret;
369
370 ret = ioctl(fd, PERF_EVENT_IOC_ID, &id);
371 if (!ret)
372 goto add;
373
374 if (errno != ENOTTY)
375 return -1;
376
377 /* Legacy way to get event id.. All hail to old kernels! */
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200378
Jiri Olsac4861af2012-10-12 13:02:21 +0200379 /*
380 * This way does not work with group format read, so bail
381 * out in that case.
382 */
383 if (perf_evlist__read_format(evlist) & PERF_FORMAT_GROUP)
384 return -1;
385
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200386 if (!(evsel->attr.read_format & PERF_FORMAT_ID) ||
387 read(fd, &read_data, sizeof(read_data)) == -1)
388 return -1;
389
390 if (evsel->attr.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
391 ++id_idx;
392 if (evsel->attr.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
393 ++id_idx;
394
Jiri Olsae2b5abe2012-04-04 19:32:27 +0200395 id = read_data[id_idx];
396
397 add:
398 perf_evlist__id_add(evlist, evsel, cpu, thread, id);
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200399 return 0;
400}
401
Jiri Olsa932a3592012-10-11 14:10:35 +0200402struct perf_sample_id *perf_evlist__id2sid(struct perf_evlist *evlist, u64 id)
Arnaldo Carvalho de Melo70db7532011-01-12 22:39:13 -0200403{
404 struct hlist_head *head;
Arnaldo Carvalho de Melo70db7532011-01-12 22:39:13 -0200405 struct perf_sample_id *sid;
406 int hash;
407
Arnaldo Carvalho de Melo70db7532011-01-12 22:39:13 -0200408 hash = hash_64(id, PERF_EVLIST__HLIST_BITS);
409 head = &evlist->heads[hash];
410
Sasha Levinb67bfe02013-02-27 17:06:00 -0800411 hlist_for_each_entry(sid, head, node)
Arnaldo Carvalho de Melo70db7532011-01-12 22:39:13 -0200412 if (sid->id == id)
Jiri Olsa932a3592012-10-11 14:10:35 +0200413 return sid;
414
415 return NULL;
416}
417
418struct perf_evsel *perf_evlist__id2evsel(struct perf_evlist *evlist, u64 id)
419{
420 struct perf_sample_id *sid;
421
422 if (evlist->nr_entries == 1)
423 return perf_evlist__first(evlist);
424
425 sid = perf_evlist__id2sid(evlist, id);
426 if (sid)
427 return sid->evsel;
Namhyung Kim30e68bc2012-02-20 10:47:26 +0900428
429 if (!perf_evlist__sample_id_all(evlist))
Arnaldo Carvalho de Melo0c21f732012-08-14 16:42:15 -0300430 return perf_evlist__first(evlist);
Namhyung Kim30e68bc2012-02-20 10:47:26 +0900431
Arnaldo Carvalho de Melo70db7532011-01-12 22:39:13 -0200432 return NULL;
433}
Arnaldo Carvalho de Melo04391de2011-01-15 10:40:59 -0200434
Adrian Hunter75562572013-08-27 11:23:09 +0300435static int perf_evlist__event2id(struct perf_evlist *evlist,
436 union perf_event *event, u64 *id)
437{
438 const u64 *array = event->sample.array;
439 ssize_t n;
440
441 n = (event->header.size - sizeof(event->header)) >> 3;
442
443 if (event->header.type == PERF_RECORD_SAMPLE) {
444 if (evlist->id_pos >= n)
445 return -1;
446 *id = array[evlist->id_pos];
447 } else {
448 if (evlist->is_pos > n)
449 return -1;
450 n -= evlist->is_pos;
451 *id = array[n];
452 }
453 return 0;
454}
455
456static struct perf_evsel *perf_evlist__event2evsel(struct perf_evlist *evlist,
457 union perf_event *event)
458{
Adrian Hunter98be6962013-09-04 23:18:17 +0300459 struct perf_evsel *first = perf_evlist__first(evlist);
Adrian Hunter75562572013-08-27 11:23:09 +0300460 struct hlist_head *head;
461 struct perf_sample_id *sid;
462 int hash;
463 u64 id;
464
465 if (evlist->nr_entries == 1)
Adrian Hunter98be6962013-09-04 23:18:17 +0300466 return first;
467
468 if (!first->attr.sample_id_all &&
469 event->header.type != PERF_RECORD_SAMPLE)
470 return first;
Adrian Hunter75562572013-08-27 11:23:09 +0300471
472 if (perf_evlist__event2id(evlist, event, &id))
473 return NULL;
474
475 /* Synthesized events have an id of zero */
476 if (!id)
Adrian Hunter98be6962013-09-04 23:18:17 +0300477 return first;
Adrian Hunter75562572013-08-27 11:23:09 +0300478
479 hash = hash_64(id, PERF_EVLIST__HLIST_BITS);
480 head = &evlist->heads[hash];
481
482 hlist_for_each_entry(sid, head, node) {
483 if (sid->id == id)
484 return sid->evsel;
485 }
486 return NULL;
487}
488
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -0300489union perf_event *perf_evlist__mmap_read(struct perf_evlist *evlist, int idx)
Arnaldo Carvalho de Melo04391de2011-01-15 10:40:59 -0200490{
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -0300491 struct perf_mmap *md = &evlist->mmap[idx];
Arnaldo Carvalho de Melo04391de2011-01-15 10:40:59 -0200492 unsigned int head = perf_mmap__read_head(md);
493 unsigned int old = md->prev;
494 unsigned char *data = md->base + page_size;
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200495 union perf_event *event = NULL;
Arnaldo Carvalho de Melo04391de2011-01-15 10:40:59 -0200496
Arnaldo Carvalho de Melo7bb41152011-01-29 09:08:13 -0200497 if (evlist->overwrite) {
Arnaldo Carvalho de Melo04391de2011-01-15 10:40:59 -0200498 /*
Arnaldo Carvalho de Melo7bb41152011-01-29 09:08:13 -0200499 * If we're further behind than half the buffer, there's a chance
500 * the writer will bite our tail and mess up the samples under us.
501 *
502 * If we somehow ended up ahead of the head, we got messed up.
503 *
504 * In either case, truncate and restart at head.
Arnaldo Carvalho de Melo04391de2011-01-15 10:40:59 -0200505 */
Arnaldo Carvalho de Melo7bb41152011-01-29 09:08:13 -0200506 int diff = head - old;
507 if (diff > md->mask / 2 || diff < 0) {
508 fprintf(stderr, "WARNING: failed to keep up with mmap data.\n");
509
510 /*
511 * head points to a known good entry, start there.
512 */
513 old = head;
514 }
Arnaldo Carvalho de Melo04391de2011-01-15 10:40:59 -0200515 }
516
517 if (old != head) {
518 size_t size;
519
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200520 event = (union perf_event *)&data[old & md->mask];
Arnaldo Carvalho de Melo04391de2011-01-15 10:40:59 -0200521 size = event->header.size;
522
523 /*
524 * Event straddles the mmap boundary -- header should always
525 * be inside due to u64 alignment of output.
526 */
527 if ((old & md->mask) + size != ((old + size) & md->mask)) {
528 unsigned int offset = old;
529 unsigned int len = min(sizeof(*event), size), cpy;
David Ahern0479b8b2013-02-05 14:12:42 -0700530 void *dst = &md->event_copy;
Arnaldo Carvalho de Melo04391de2011-01-15 10:40:59 -0200531
532 do {
533 cpy = min(md->mask + 1 - (offset & md->mask), len);
534 memcpy(dst, &data[offset & md->mask], cpy);
535 offset += cpy;
536 dst += cpy;
537 len -= cpy;
538 } while (len);
539
David Ahern0479b8b2013-02-05 14:12:42 -0700540 event = &md->event_copy;
Arnaldo Carvalho de Melo04391de2011-01-15 10:40:59 -0200541 }
542
543 old += size;
544 }
545
546 md->prev = old;
Arnaldo Carvalho de Melo7bb41152011-01-29 09:08:13 -0200547
548 if (!evlist->overwrite)
549 perf_mmap__write_tail(md, old);
550
Arnaldo Carvalho de Melo04391de2011-01-15 10:40:59 -0200551 return event;
552}
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200553
Adrian Hunter93edcbd2013-07-04 16:20:26 +0300554static void __perf_evlist__munmap(struct perf_evlist *evlist, int idx)
555{
556 if (evlist->mmap[idx].base != NULL) {
557 munmap(evlist->mmap[idx].base, evlist->mmap_len);
558 evlist->mmap[idx].base = NULL;
559 }
560}
561
Arnaldo Carvalho de Melo7e2ed092011-01-30 11:59:43 -0200562void perf_evlist__munmap(struct perf_evlist *evlist)
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200563{
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -0300564 int i;
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200565
Adrian Hunter93edcbd2013-07-04 16:20:26 +0300566 for (i = 0; i < evlist->nr_mmaps; i++)
567 __perf_evlist__munmap(evlist, i);
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -0300568
569 free(evlist->mmap);
570 evlist->mmap = NULL;
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200571}
572
Arnaldo Carvalho de Melo806fb632011-11-29 08:05:52 -0200573static int perf_evlist__alloc_mmap(struct perf_evlist *evlist)
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200574{
Arnaldo Carvalho de Meloa14bb7a2012-09-26 12:41:14 -0300575 evlist->nr_mmaps = cpu_map__nr(evlist->cpus);
Sukadev Bhattiproluec1e7e42013-05-22 17:42:38 -0700576 if (cpu_map__empty(evlist->cpus))
Namhyung Kimb3a319d2013-03-11 16:43:14 +0900577 evlist->nr_mmaps = thread_map__nr(evlist->threads);
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -0300578 evlist->mmap = zalloc(evlist->nr_mmaps * sizeof(struct perf_mmap));
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200579 return evlist->mmap != NULL ? 0 : -ENOMEM;
580}
581
Arnaldo Carvalho de Melobccdaba2011-06-02 10:39:43 -0300582static int __perf_evlist__mmap(struct perf_evlist *evlist,
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -0300583 int idx, int prot, int mask, int fd)
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200584{
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -0300585 evlist->mmap[idx].prev = 0;
586 evlist->mmap[idx].mask = mask;
587 evlist->mmap[idx].base = mmap(NULL, evlist->mmap_len, prot,
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200588 MAP_SHARED, fd, 0);
Nelson Elhage301b1952011-12-19 08:39:30 -0500589 if (evlist->mmap[idx].base == MAP_FAILED) {
590 evlist->mmap[idx].base = NULL;
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200591 return -1;
Nelson Elhage301b1952011-12-19 08:39:30 -0500592 }
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200593
594 perf_evlist__add_pollfd(evlist, fd);
595 return 0;
596}
597
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -0300598static int perf_evlist__mmap_per_cpu(struct perf_evlist *evlist, int prot, int mask)
599{
600 struct perf_evsel *evsel;
601 int cpu, thread;
Namhyung Kimb3a319d2013-03-11 16:43:14 +0900602 int nr_cpus = cpu_map__nr(evlist->cpus);
603 int nr_threads = thread_map__nr(evlist->threads);
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -0300604
Adrian Huntere3e1a542013-08-14 15:48:24 +0300605 pr_debug2("perf event ring buffer mmapped per cpu\n");
Namhyung Kimb3a319d2013-03-11 16:43:14 +0900606 for (cpu = 0; cpu < nr_cpus; cpu++) {
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -0300607 int output = -1;
608
Namhyung Kimb3a319d2013-03-11 16:43:14 +0900609 for (thread = 0; thread < nr_threads; thread++) {
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -0300610 list_for_each_entry(evsel, &evlist->entries, node) {
611 int fd = FD(evsel, cpu, thread);
612
613 if (output == -1) {
614 output = fd;
Arnaldo Carvalho de Melobccdaba2011-06-02 10:39:43 -0300615 if (__perf_evlist__mmap(evlist, cpu,
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -0300616 prot, mask, output) < 0)
617 goto out_unmap;
618 } else {
619 if (ioctl(fd, PERF_EVENT_IOC_SET_OUTPUT, output) != 0)
620 goto out_unmap;
621 }
622
623 if ((evsel->attr.read_format & PERF_FORMAT_ID) &&
624 perf_evlist__id_add_fd(evlist, evsel, cpu, thread, fd) < 0)
625 goto out_unmap;
626 }
627 }
628 }
629
630 return 0;
631
632out_unmap:
Adrian Hunter93edcbd2013-07-04 16:20:26 +0300633 for (cpu = 0; cpu < nr_cpus; cpu++)
634 __perf_evlist__munmap(evlist, cpu);
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -0300635 return -1;
636}
637
638static int perf_evlist__mmap_per_thread(struct perf_evlist *evlist, int prot, int mask)
639{
640 struct perf_evsel *evsel;
641 int thread;
Namhyung Kimb3a319d2013-03-11 16:43:14 +0900642 int nr_threads = thread_map__nr(evlist->threads);
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -0300643
Adrian Huntere3e1a542013-08-14 15:48:24 +0300644 pr_debug2("perf event ring buffer mmapped per thread\n");
Namhyung Kimb3a319d2013-03-11 16:43:14 +0900645 for (thread = 0; thread < nr_threads; thread++) {
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -0300646 int output = -1;
647
648 list_for_each_entry(evsel, &evlist->entries, node) {
649 int fd = FD(evsel, 0, thread);
650
651 if (output == -1) {
652 output = fd;
Arnaldo Carvalho de Melobccdaba2011-06-02 10:39:43 -0300653 if (__perf_evlist__mmap(evlist, thread,
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -0300654 prot, mask, output) < 0)
655 goto out_unmap;
656 } else {
657 if (ioctl(fd, PERF_EVENT_IOC_SET_OUTPUT, output) != 0)
658 goto out_unmap;
659 }
660
661 if ((evsel->attr.read_format & PERF_FORMAT_ID) &&
662 perf_evlist__id_add_fd(evlist, evsel, 0, thread, fd) < 0)
663 goto out_unmap;
664 }
665 }
666
667 return 0;
668
669out_unmap:
Adrian Hunter93edcbd2013-07-04 16:20:26 +0300670 for (thread = 0; thread < nr_threads; thread++)
671 __perf_evlist__munmap(evlist, thread);
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -0300672 return -1;
673}
674
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200675/** perf_evlist__mmap - Create per cpu maps to receive events
676 *
677 * @evlist - list of events
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200678 * @pages - map length in pages
679 * @overwrite - overwrite older events?
680 *
681 * If overwrite is false the user needs to signal event consuption using:
682 *
683 * struct perf_mmap *m = &evlist->mmap[cpu];
684 * unsigned int head = perf_mmap__read_head(m);
685 *
686 * perf_mmap__write_tail(m, head)
Arnaldo Carvalho de Melo7e2ed092011-01-30 11:59:43 -0200687 *
688 * Using perf_evlist__read_on_cpu does this automatically.
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200689 */
Arnaldo Carvalho de Melo50a682c2011-11-09 09:10:47 -0200690int perf_evlist__mmap(struct perf_evlist *evlist, unsigned int pages,
691 bool overwrite)
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200692{
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -0300693 struct perf_evsel *evsel;
Arnaldo Carvalho de Melo7e2ed092011-01-30 11:59:43 -0200694 const struct cpu_map *cpus = evlist->cpus;
695 const struct thread_map *threads = evlist->threads;
Arnaldo Carvalho de Melo50a682c2011-11-09 09:10:47 -0200696 int prot = PROT_READ | (overwrite ? 0 : PROT_WRITE), mask;
697
698 /* 512 kiB: default amount of unprivileged mlocked memory */
699 if (pages == UINT_MAX)
700 pages = (512 * 1024) / page_size;
Nelson Elhage41d0d932011-12-19 08:39:32 -0500701 else if (!is_power_of_2(pages))
702 return -EINVAL;
Arnaldo Carvalho de Melo50a682c2011-11-09 09:10:47 -0200703
704 mask = pages * page_size - 1;
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200705
Arnaldo Carvalho de Melo7e2ed092011-01-30 11:59:43 -0200706 if (evlist->mmap == NULL && perf_evlist__alloc_mmap(evlist) < 0)
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200707 return -ENOMEM;
708
Arnaldo Carvalho de Melo7e2ed092011-01-30 11:59:43 -0200709 if (evlist->pollfd == NULL && perf_evlist__alloc_pollfd(evlist) < 0)
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200710 return -ENOMEM;
711
712 evlist->overwrite = overwrite;
713 evlist->mmap_len = (pages + 1) * page_size;
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200714
715 list_for_each_entry(evsel, &evlist->entries, node) {
716 if ((evsel->attr.read_format & PERF_FORMAT_ID) &&
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -0300717 evsel->sample_id == NULL &&
Arnaldo Carvalho de Meloa14bb7a2012-09-26 12:41:14 -0300718 perf_evsel__alloc_id(evsel, cpu_map__nr(cpus), threads->nr) < 0)
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200719 return -ENOMEM;
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200720 }
721
Sukadev Bhattiproluec1e7e42013-05-22 17:42:38 -0700722 if (cpu_map__empty(cpus))
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -0300723 return perf_evlist__mmap_per_thread(evlist, prot, mask);
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200724
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -0300725 return perf_evlist__mmap_per_cpu(evlist, prot, mask);
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200726}
Arnaldo Carvalho de Melo7e2ed092011-01-30 11:59:43 -0200727
Namhyung Kimb809ac12012-04-26 14:15:19 +0900728int perf_evlist__create_maps(struct perf_evlist *evlist,
729 struct perf_target *target)
Arnaldo Carvalho de Melo7e2ed092011-01-30 11:59:43 -0200730{
Namhyung Kimb809ac12012-04-26 14:15:19 +0900731 evlist->threads = thread_map__new_str(target->pid, target->tid,
732 target->uid);
Arnaldo Carvalho de Melo7e2ed092011-01-30 11:59:43 -0200733
734 if (evlist->threads == NULL)
735 return -1;
736
Namhyung Kim879d77d2012-05-16 18:45:48 +0900737 if (perf_target__has_task(target))
Namhyung Kimd67356e2012-05-07 14:09:03 +0900738 evlist->cpus = cpu_map__dummy_new();
Namhyung Kimd1cb9fc2012-05-16 18:45:49 +0900739 else if (!perf_target__has_cpu(target) && !target->uses_mmap)
740 evlist->cpus = cpu_map__dummy_new();
Namhyung Kim879d77d2012-05-16 18:45:48 +0900741 else
742 evlist->cpus = cpu_map__new(target->cpu_list);
Arnaldo Carvalho de Melo7e2ed092011-01-30 11:59:43 -0200743
744 if (evlist->cpus == NULL)
745 goto out_delete_threads;
746
747 return 0;
748
749out_delete_threads:
750 thread_map__delete(evlist->threads);
751 return -1;
752}
753
754void perf_evlist__delete_maps(struct perf_evlist *evlist)
755{
756 cpu_map__delete(evlist->cpus);
757 thread_map__delete(evlist->threads);
758 evlist->cpus = NULL;
759 evlist->threads = NULL;
760}
Frederic Weisbecker0a102472011-02-26 04:51:54 +0100761
Arnaldo Carvalho de Melo1491a632012-09-26 14:43:13 -0300762int perf_evlist__apply_filters(struct perf_evlist *evlist)
Frederic Weisbecker0a102472011-02-26 04:51:54 +0100763{
Frederic Weisbecker0a102472011-02-26 04:51:54 +0100764 struct perf_evsel *evsel;
Arnaldo Carvalho de Melo745cefc2012-09-26 15:07:39 -0300765 int err = 0;
766 const int ncpus = cpu_map__nr(evlist->cpus),
Namhyung Kimb3a319d2013-03-11 16:43:14 +0900767 nthreads = thread_map__nr(evlist->threads);
Frederic Weisbecker0a102472011-02-26 04:51:54 +0100768
769 list_for_each_entry(evsel, &evlist->entries, node) {
Arnaldo Carvalho de Melo745cefc2012-09-26 15:07:39 -0300770 if (evsel->filter == NULL)
Frederic Weisbecker0a102472011-02-26 04:51:54 +0100771 continue;
Arnaldo Carvalho de Melo745cefc2012-09-26 15:07:39 -0300772
773 err = perf_evsel__set_filter(evsel, ncpus, nthreads, evsel->filter);
774 if (err)
775 break;
Frederic Weisbecker0a102472011-02-26 04:51:54 +0100776 }
777
Arnaldo Carvalho de Melo745cefc2012-09-26 15:07:39 -0300778 return err;
779}
780
781int perf_evlist__set_filter(struct perf_evlist *evlist, const char *filter)
782{
783 struct perf_evsel *evsel;
784 int err = 0;
785 const int ncpus = cpu_map__nr(evlist->cpus),
Namhyung Kimb3a319d2013-03-11 16:43:14 +0900786 nthreads = thread_map__nr(evlist->threads);
Arnaldo Carvalho de Melo745cefc2012-09-26 15:07:39 -0300787
788 list_for_each_entry(evsel, &evlist->entries, node) {
789 err = perf_evsel__set_filter(evsel, ncpus, nthreads, filter);
790 if (err)
791 break;
792 }
793
794 return err;
Frederic Weisbecker0a102472011-02-26 04:51:54 +0100795}
Frederic Weisbecker74429962011-05-21 17:49:00 +0200796
Arnaldo Carvalho de Melo0c21f732012-08-14 16:42:15 -0300797bool perf_evlist__valid_sample_type(struct perf_evlist *evlist)
Frederic Weisbecker74429962011-05-21 17:49:00 +0200798{
Adrian Hunter75562572013-08-27 11:23:09 +0300799 struct perf_evsel *pos;
Arnaldo Carvalho de Meloc2a70652011-06-02 11:04:54 -0300800
Adrian Hunter75562572013-08-27 11:23:09 +0300801 if (evlist->nr_entries == 1)
802 return true;
803
804 if (evlist->id_pos < 0 || evlist->is_pos < 0)
805 return false;
806
807 list_for_each_entry(pos, &evlist->entries, node) {
808 if (pos->id_pos != evlist->id_pos ||
809 pos->is_pos != evlist->is_pos)
Arnaldo Carvalho de Meloc2a70652011-06-02 11:04:54 -0300810 return false;
Frederic Weisbecker74429962011-05-21 17:49:00 +0200811 }
812
Arnaldo Carvalho de Meloc2a70652011-06-02 11:04:54 -0300813 return true;
814}
815
Adrian Hunter75562572013-08-27 11:23:09 +0300816u64 __perf_evlist__combined_sample_type(struct perf_evlist *evlist)
Arnaldo Carvalho de Meloc2a70652011-06-02 11:04:54 -0300817{
Adrian Hunter75562572013-08-27 11:23:09 +0300818 struct perf_evsel *evsel;
819
820 if (evlist->combined_sample_type)
821 return evlist->combined_sample_type;
822
823 list_for_each_entry(evsel, &evlist->entries, node)
824 evlist->combined_sample_type |= evsel->attr.sample_type;
825
826 return evlist->combined_sample_type;
827}
828
829u64 perf_evlist__combined_sample_type(struct perf_evlist *evlist)
830{
831 evlist->combined_sample_type = 0;
832 return __perf_evlist__combined_sample_type(evlist);
Arnaldo Carvalho de Meloc2a70652011-06-02 11:04:54 -0300833}
834
Jiri Olsa9ede4732012-10-10 17:38:13 +0200835bool perf_evlist__valid_read_format(struct perf_evlist *evlist)
836{
837 struct perf_evsel *first = perf_evlist__first(evlist), *pos = first;
838 u64 read_format = first->attr.read_format;
839 u64 sample_type = first->attr.sample_type;
840
841 list_for_each_entry_continue(pos, &evlist->entries, node) {
842 if (read_format != pos->attr.read_format)
843 return false;
844 }
845
846 /* PERF_SAMPLE_READ imples PERF_FORMAT_ID. */
847 if ((sample_type & PERF_SAMPLE_READ) &&
848 !(read_format & PERF_FORMAT_ID)) {
849 return false;
850 }
851
852 return true;
853}
854
855u64 perf_evlist__read_format(struct perf_evlist *evlist)
856{
857 struct perf_evsel *first = perf_evlist__first(evlist);
858 return first->attr.read_format;
859}
860
Arnaldo Carvalho de Melo0c21f732012-08-14 16:42:15 -0300861u16 perf_evlist__id_hdr_size(struct perf_evlist *evlist)
Arnaldo Carvalho de Melo81e36bf2011-11-11 22:28:50 -0200862{
Arnaldo Carvalho de Melo0c21f732012-08-14 16:42:15 -0300863 struct perf_evsel *first = perf_evlist__first(evlist);
Arnaldo Carvalho de Melo81e36bf2011-11-11 22:28:50 -0200864 struct perf_sample *data;
865 u64 sample_type;
866 u16 size = 0;
867
Arnaldo Carvalho de Melo81e36bf2011-11-11 22:28:50 -0200868 if (!first->attr.sample_id_all)
869 goto out;
870
871 sample_type = first->attr.sample_type;
872
873 if (sample_type & PERF_SAMPLE_TID)
874 size += sizeof(data->tid) * 2;
875
876 if (sample_type & PERF_SAMPLE_TIME)
877 size += sizeof(data->time);
878
879 if (sample_type & PERF_SAMPLE_ID)
880 size += sizeof(data->id);
881
882 if (sample_type & PERF_SAMPLE_STREAM_ID)
883 size += sizeof(data->stream_id);
884
885 if (sample_type & PERF_SAMPLE_CPU)
886 size += sizeof(data->cpu) * 2;
Adrian Hunter75562572013-08-27 11:23:09 +0300887
888 if (sample_type & PERF_SAMPLE_IDENTIFIER)
889 size += sizeof(data->id);
Arnaldo Carvalho de Melo81e36bf2011-11-11 22:28:50 -0200890out:
891 return size;
892}
893
Arnaldo Carvalho de Melo0c21f732012-08-14 16:42:15 -0300894bool perf_evlist__valid_sample_id_all(struct perf_evlist *evlist)
Arnaldo Carvalho de Meloc2a70652011-06-02 11:04:54 -0300895{
Arnaldo Carvalho de Melo0c21f732012-08-14 16:42:15 -0300896 struct perf_evsel *first = perf_evlist__first(evlist), *pos = first;
Arnaldo Carvalho de Meloc2a70652011-06-02 11:04:54 -0300897
898 list_for_each_entry_continue(pos, &evlist->entries, node) {
899 if (first->attr.sample_id_all != pos->attr.sample_id_all)
900 return false;
901 }
902
903 return true;
Frederic Weisbecker74429962011-05-21 17:49:00 +0200904}
905
Arnaldo Carvalho de Melo0c21f732012-08-14 16:42:15 -0300906bool perf_evlist__sample_id_all(struct perf_evlist *evlist)
Frederic Weisbecker74429962011-05-21 17:49:00 +0200907{
Arnaldo Carvalho de Melo0c21f732012-08-14 16:42:15 -0300908 struct perf_evsel *first = perf_evlist__first(evlist);
Arnaldo Carvalho de Meloc2a70652011-06-02 11:04:54 -0300909 return first->attr.sample_id_all;
Frederic Weisbecker74429962011-05-21 17:49:00 +0200910}
Arnaldo Carvalho de Melo81cce8d2011-10-05 19:11:32 -0300911
912void perf_evlist__set_selected(struct perf_evlist *evlist,
913 struct perf_evsel *evsel)
914{
915 evlist->selected = evsel;
916}
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -0200917
Namhyung Kima74b4b62013-03-15 14:48:48 +0900918void perf_evlist__close(struct perf_evlist *evlist)
919{
920 struct perf_evsel *evsel;
921 int ncpus = cpu_map__nr(evlist->cpus);
922 int nthreads = thread_map__nr(evlist->threads);
923
924 list_for_each_entry_reverse(evsel, &evlist->entries, node)
925 perf_evsel__close(evsel, ncpus, nthreads);
926}
927
Jiri Olsa6a4bb042012-08-08 12:22:36 +0200928int perf_evlist__open(struct perf_evlist *evlist)
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -0200929{
Jiri Olsa6a4bb042012-08-08 12:22:36 +0200930 struct perf_evsel *evsel;
Namhyung Kima74b4b62013-03-15 14:48:48 +0900931 int err;
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -0200932
Adrian Hunter733cd2f2013-09-06 22:40:11 +0300933 perf_evlist__update_id_pos(evlist);
934
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -0200935 list_for_each_entry(evsel, &evlist->entries, node) {
Jiri Olsa6a4bb042012-08-08 12:22:36 +0200936 err = perf_evsel__open(evsel, evlist->cpus, evlist->threads);
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -0200937 if (err < 0)
938 goto out_err;
939 }
940
941 return 0;
942out_err:
Namhyung Kima74b4b62013-03-15 14:48:48 +0900943 perf_evlist__close(evlist);
Namhyung Kim41c21a62012-02-23 12:13:36 +0900944 errno = -err;
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -0200945 return err;
946}
Arnaldo Carvalho de Melo35b9d882011-11-09 08:47:15 -0200947
948int perf_evlist__prepare_workload(struct perf_evlist *evlist,
Namhyung Kim6ef73ec2013-03-11 16:43:15 +0900949 struct perf_target *target,
Namhyung Kim55e162e2013-03-11 16:43:17 +0900950 const char *argv[], bool pipe_output,
951 bool want_signal)
Arnaldo Carvalho de Melo35b9d882011-11-09 08:47:15 -0200952{
953 int child_ready_pipe[2], go_pipe[2];
954 char bf;
955
956 if (pipe(child_ready_pipe) < 0) {
957 perror("failed to create 'ready' pipe");
958 return -1;
959 }
960
961 if (pipe(go_pipe) < 0) {
962 perror("failed to create 'go' pipe");
963 goto out_close_ready_pipe;
964 }
965
966 evlist->workload.pid = fork();
967 if (evlist->workload.pid < 0) {
968 perror("failed to fork");
969 goto out_close_pipes;
970 }
971
972 if (!evlist->workload.pid) {
Namhyung Kim119fa3c2013-03-11 16:43:16 +0900973 if (pipe_output)
Arnaldo Carvalho de Melo35b9d882011-11-09 08:47:15 -0200974 dup2(2, 1);
975
David Ahern0817df02013-05-25 17:50:39 -0600976 signal(SIGTERM, SIG_DFL);
977
Arnaldo Carvalho de Melo35b9d882011-11-09 08:47:15 -0200978 close(child_ready_pipe[0]);
979 close(go_pipe[1]);
980 fcntl(go_pipe[0], F_SETFD, FD_CLOEXEC);
981
982 /*
Arnaldo Carvalho de Melo35b9d882011-11-09 08:47:15 -0200983 * Tell the parent we're ready to go
984 */
985 close(child_ready_pipe[1]);
986
987 /*
988 * Wait until the parent tells us to go.
989 */
990 if (read(go_pipe[0], &bf, 1) == -1)
991 perror("unable to read pipe");
992
993 execvp(argv[0], (char **)argv);
994
995 perror(argv[0]);
Namhyung Kim55e162e2013-03-11 16:43:17 +0900996 if (want_signal)
997 kill(getppid(), SIGUSR1);
Arnaldo Carvalho de Melo35b9d882011-11-09 08:47:15 -0200998 exit(-1);
999 }
1000
Namhyung Kim6ef73ec2013-03-11 16:43:15 +09001001 if (perf_target__none(target))
Arnaldo Carvalho de Melo35b9d882011-11-09 08:47:15 -02001002 evlist->threads->map[0] = evlist->workload.pid;
1003
1004 close(child_ready_pipe[1]);
1005 close(go_pipe[0]);
1006 /*
1007 * wait for child to settle
1008 */
1009 if (read(child_ready_pipe[0], &bf, 1) == -1) {
1010 perror("unable to read pipe");
1011 goto out_close_pipes;
1012 }
1013
Namhyung Kimbcf31452013-06-26 16:14:15 +09001014 fcntl(go_pipe[1], F_SETFD, FD_CLOEXEC);
Arnaldo Carvalho de Melo35b9d882011-11-09 08:47:15 -02001015 evlist->workload.cork_fd = go_pipe[1];
1016 close(child_ready_pipe[0]);
1017 return 0;
1018
1019out_close_pipes:
1020 close(go_pipe[0]);
1021 close(go_pipe[1]);
1022out_close_ready_pipe:
1023 close(child_ready_pipe[0]);
1024 close(child_ready_pipe[1]);
1025 return -1;
1026}
1027
1028int perf_evlist__start_workload(struct perf_evlist *evlist)
1029{
1030 if (evlist->workload.cork_fd > 0) {
David Ahernb3824402013-07-02 13:27:21 -06001031 char bf = 0;
Namhyung Kimbcf31452013-06-26 16:14:15 +09001032 int ret;
Arnaldo Carvalho de Melo35b9d882011-11-09 08:47:15 -02001033 /*
1034 * Remove the cork, let it rip!
1035 */
Namhyung Kimbcf31452013-06-26 16:14:15 +09001036 ret = write(evlist->workload.cork_fd, &bf, 1);
1037 if (ret < 0)
1038 perror("enable to write to pipe");
1039
1040 close(evlist->workload.cork_fd);
1041 return ret;
Arnaldo Carvalho de Melo35b9d882011-11-09 08:47:15 -02001042 }
1043
1044 return 0;
1045}
Arnaldo Carvalho de Melocb0b29e2012-08-02 11:42:57 -03001046
Arnaldo Carvalho de Meloa3f698f2012-08-02 12:23:46 -03001047int perf_evlist__parse_sample(struct perf_evlist *evlist, union perf_event *event,
Arnaldo Carvalho de Melo0807d2d2012-09-26 12:48:18 -03001048 struct perf_sample *sample)
Arnaldo Carvalho de Melocb0b29e2012-08-02 11:42:57 -03001049{
Adrian Hunter75562572013-08-27 11:23:09 +03001050 struct perf_evsel *evsel = perf_evlist__event2evsel(evlist, event);
1051
1052 if (!evsel)
1053 return -EFAULT;
Arnaldo Carvalho de Melo0807d2d2012-09-26 12:48:18 -03001054 return perf_evsel__parse_sample(evsel, event, sample);
Arnaldo Carvalho de Melocb0b29e2012-08-02 11:42:57 -03001055}
Arnaldo Carvalho de Melo78f067b2012-09-06 14:54:11 -03001056
1057size_t perf_evlist__fprintf(struct perf_evlist *evlist, FILE *fp)
1058{
1059 struct perf_evsel *evsel;
1060 size_t printed = 0;
1061
1062 list_for_each_entry(evsel, &evlist->entries, node) {
1063 printed += fprintf(fp, "%s%s", evsel->idx ? ", " : "",
1064 perf_evsel__name(evsel));
1065 }
1066
1067 return printed + fprintf(fp, "\n");;
1068}