blob: f0d71a9d49f467ebbc27551c46ddd12b648863cf [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"
Jiri Olsa994a1f72013-09-01 12:36:12 +020021#include "parse-options.h"
Arnaldo Carvalho de Melo50d08e42011-11-04 09:10:59 -020022
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -020023#include <sys/mman.h>
24
Arnaldo Carvalho de Melo70db7532011-01-12 22:39:13 -020025#include <linux/bitops.h>
26#include <linux/hash.h>
27
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -020028#define FD(e, x, y) (*(int *)xyarray__entry(e->fd, x, y))
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -030029#define SID(e, x, y) xyarray__entry(e->sample_id, x, y)
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -020030
Arnaldo Carvalho de Melo7e2ed092011-01-30 11:59:43 -020031void perf_evlist__init(struct perf_evlist *evlist, struct cpu_map *cpus,
32 struct thread_map *threads)
Arnaldo Carvalho de Meloef1d1af2011-01-18 21:41:45 -020033{
34 int i;
35
36 for (i = 0; i < PERF_EVLIST__HLIST_SIZE; ++i)
37 INIT_HLIST_HEAD(&evlist->heads[i]);
38 INIT_LIST_HEAD(&evlist->entries);
Arnaldo Carvalho de Melo7e2ed092011-01-30 11:59:43 -020039 perf_evlist__set_maps(evlist, cpus, threads);
Arnaldo Carvalho de Melo35b9d882011-11-09 08:47:15 -020040 evlist->workload.pid = -1;
Arnaldo Carvalho de Meloef1d1af2011-01-18 21:41:45 -020041}
42
Namhyung Kim334fe7a2013-03-11 16:43:12 +090043struct perf_evlist *perf_evlist__new(void)
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -020044{
45 struct perf_evlist *evlist = zalloc(sizeof(*evlist));
46
Arnaldo Carvalho de Meloef1d1af2011-01-18 21:41:45 -020047 if (evlist != NULL)
Namhyung Kim334fe7a2013-03-11 16:43:12 +090048 perf_evlist__init(evlist, NULL, NULL);
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -020049
50 return evlist;
51}
52
Jiri Olsab22d54b2013-09-01 12:36:14 +020053struct perf_evlist *perf_evlist__new_default(void)
54{
55 struct perf_evlist *evlist = perf_evlist__new();
56
57 if (evlist && perf_evlist__add_default(evlist)) {
58 perf_evlist__delete(evlist);
59 evlist = NULL;
60 }
61
62 return evlist;
63}
64
Adrian Hunter75562572013-08-27 11:23:09 +030065/**
66 * perf_evlist__set_id_pos - set the positions of event ids.
67 * @evlist: selected event list
68 *
69 * Events with compatible sample types all have the same id_pos
70 * and is_pos. For convenience, put a copy on evlist.
71 */
72void perf_evlist__set_id_pos(struct perf_evlist *evlist)
73{
74 struct perf_evsel *first = perf_evlist__first(evlist);
75
76 evlist->id_pos = first->id_pos;
77 evlist->is_pos = first->is_pos;
78}
79
Adrian Hunter733cd2f2013-09-06 22:40:11 +030080static void perf_evlist__update_id_pos(struct perf_evlist *evlist)
81{
82 struct perf_evsel *evsel;
83
84 list_for_each_entry(evsel, &evlist->entries, node)
85 perf_evsel__calc_id_pos(evsel);
86
87 perf_evlist__set_id_pos(evlist);
88}
89
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -020090static void perf_evlist__purge(struct perf_evlist *evlist)
91{
92 struct perf_evsel *pos, *n;
93
94 list_for_each_entry_safe(pos, n, &evlist->entries, node) {
95 list_del_init(&pos->node);
96 perf_evsel__delete(pos);
97 }
98
99 evlist->nr_entries = 0;
100}
101
Arnaldo Carvalho de Meloef1d1af2011-01-18 21:41:45 -0200102void perf_evlist__exit(struct perf_evlist *evlist)
103{
104 free(evlist->mmap);
105 free(evlist->pollfd);
106 evlist->mmap = NULL;
107 evlist->pollfd = NULL;
108}
109
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -0200110void perf_evlist__delete(struct perf_evlist *evlist)
111{
112 perf_evlist__purge(evlist);
Arnaldo Carvalho de Meloef1d1af2011-01-18 21:41:45 -0200113 perf_evlist__exit(evlist);
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -0200114 free(evlist);
115}
116
117void perf_evlist__add(struct perf_evlist *evlist, struct perf_evsel *entry)
118{
119 list_add_tail(&entry->node, &evlist->entries);
Adrian Hunter75562572013-08-27 11:23:09 +0300120 if (!evlist->nr_entries++)
121 perf_evlist__set_id_pos(evlist);
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -0200122}
123
Jiri Olsa0529bc12012-01-27 15:34:20 +0100124void perf_evlist__splice_list_tail(struct perf_evlist *evlist,
125 struct list_head *list,
126 int nr_entries)
Arnaldo Carvalho de Melo50d08e42011-11-04 09:10:59 -0200127{
Adrian Hunter75562572013-08-27 11:23:09 +0300128 bool set_id_pos = !evlist->nr_entries;
129
Arnaldo Carvalho de Melo50d08e42011-11-04 09:10:59 -0200130 list_splice_tail(list, &evlist->entries);
131 evlist->nr_entries += nr_entries;
Adrian Hunter75562572013-08-27 11:23:09 +0300132 if (set_id_pos)
133 perf_evlist__set_id_pos(evlist);
Arnaldo Carvalho de Melo50d08e42011-11-04 09:10:59 -0200134}
135
Arnaldo Carvalho de Melo63dab222012-08-14 16:35:48 -0300136void __perf_evlist__set_leader(struct list_head *list)
137{
138 struct perf_evsel *evsel, *leader;
139
140 leader = list_entry(list->next, struct perf_evsel, node);
Namhyung Kim97f63e42013-01-22 18:09:29 +0900141 evsel = list_entry(list->prev, struct perf_evsel, node);
142
143 leader->nr_members = evsel->idx - leader->idx + 1;
Arnaldo Carvalho de Melo63dab222012-08-14 16:35:48 -0300144
145 list_for_each_entry(evsel, list, node) {
Stephane Eranian74b21332013-01-31 13:54:37 +0100146 evsel->leader = leader;
Arnaldo Carvalho de Melo63dab222012-08-14 16:35:48 -0300147 }
148}
149
150void perf_evlist__set_leader(struct perf_evlist *evlist)
Jiri Olsa6a4bb042012-08-08 12:22:36 +0200151{
Namhyung Kim97f63e42013-01-22 18:09:29 +0900152 if (evlist->nr_entries) {
153 evlist->nr_groups = evlist->nr_entries > 1 ? 1 : 0;
Arnaldo Carvalho de Melo63dab222012-08-14 16:35:48 -0300154 __perf_evlist__set_leader(&evlist->entries);
Namhyung Kim97f63e42013-01-22 18:09:29 +0900155 }
Jiri Olsa6a4bb042012-08-08 12:22:36 +0200156}
157
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -0200158int perf_evlist__add_default(struct perf_evlist *evlist)
159{
160 struct perf_event_attr attr = {
161 .type = PERF_TYPE_HARDWARE,
162 .config = PERF_COUNT_HW_CPU_CYCLES,
163 };
Joerg Roedel1aed2672012-01-04 17:54:20 +0100164 struct perf_evsel *evsel;
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -0200165
Joerg Roedel1aed2672012-01-04 17:54:20 +0100166 event_attr_init(&attr);
167
168 evsel = perf_evsel__new(&attr, 0);
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -0200169 if (evsel == NULL)
Stephane Eraniancc2d86b2011-06-07 18:19:36 +0200170 goto error;
171
172 /* use strdup() because free(evsel) assumes name is allocated */
173 evsel->name = strdup("cycles");
174 if (!evsel->name)
175 goto error_free;
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -0200176
177 perf_evlist__add(evlist, evsel);
178 return 0;
Stephane Eraniancc2d86b2011-06-07 18:19:36 +0200179error_free:
180 perf_evsel__delete(evsel);
181error:
182 return -ENOMEM;
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -0200183}
Arnaldo Carvalho de Melo5c581042011-01-11 22:30:02 -0200184
Arnaldo Carvalho de Meloe60fc842012-10-03 11:50:55 -0300185static int perf_evlist__add_attrs(struct perf_evlist *evlist,
186 struct perf_event_attr *attrs, size_t nr_attrs)
Arnaldo Carvalho de Melo50d08e42011-11-04 09:10:59 -0200187{
188 struct perf_evsel *evsel, *n;
189 LIST_HEAD(head);
190 size_t i;
191
192 for (i = 0; i < nr_attrs; i++) {
193 evsel = perf_evsel__new(attrs + i, evlist->nr_entries + i);
194 if (evsel == NULL)
195 goto out_delete_partial_list;
196 list_add_tail(&evsel->node, &head);
197 }
198
199 perf_evlist__splice_list_tail(evlist, &head, nr_attrs);
200
201 return 0;
202
203out_delete_partial_list:
204 list_for_each_entry_safe(evsel, n, &head, node)
205 perf_evsel__delete(evsel);
206 return -1;
207}
208
Arnaldo Carvalho de Melo79695e12012-05-30 13:53:54 -0300209int __perf_evlist__add_default_attrs(struct perf_evlist *evlist,
210 struct perf_event_attr *attrs, size_t nr_attrs)
211{
212 size_t i;
213
214 for (i = 0; i < nr_attrs; i++)
215 event_attr_init(attrs + i);
216
217 return perf_evlist__add_attrs(evlist, attrs, nr_attrs);
218}
219
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -0300220struct perf_evsel *
221perf_evlist__find_tracepoint_by_id(struct perf_evlist *evlist, int id)
Arnaldo Carvalho de Meloee29be62011-11-28 17:57:40 -0200222{
223 struct perf_evsel *evsel;
224
225 list_for_each_entry(evsel, &evlist->entries, node) {
226 if (evsel->attr.type == PERF_TYPE_TRACEPOINT &&
227 (int)evsel->attr.config == id)
228 return evsel;
229 }
230
231 return NULL;
232}
233
David Aherna2f28042013-08-28 22:29:51 -0600234struct perf_evsel *
235perf_evlist__find_tracepoint_by_name(struct perf_evlist *evlist,
236 const char *name)
237{
238 struct perf_evsel *evsel;
239
240 list_for_each_entry(evsel, &evlist->entries, node) {
241 if ((evsel->attr.type == PERF_TYPE_TRACEPOINT) &&
242 (strcmp(evsel->name, name) == 0))
243 return evsel;
244 }
245
246 return NULL;
247}
248
Arnaldo Carvalho de Melo39876e72012-10-03 11:40:22 -0300249int perf_evlist__add_newtp(struct perf_evlist *evlist,
250 const char *sys, const char *name, void *handler)
251{
252 struct perf_evsel *evsel;
253
254 evsel = perf_evsel__newtp(sys, name, evlist->nr_entries);
255 if (evsel == NULL)
256 return -1;
257
258 evsel->handler.func = handler;
259 perf_evlist__add(evlist, evsel);
260 return 0;
261}
262
Arnaldo Carvalho de Melo4152ab32011-07-25 11:06:19 -0300263void perf_evlist__disable(struct perf_evlist *evlist)
264{
265 int cpu, thread;
266 struct perf_evsel *pos;
Namhyung Kimb3a319d2013-03-11 16:43:14 +0900267 int nr_cpus = cpu_map__nr(evlist->cpus);
268 int nr_threads = thread_map__nr(evlist->threads);
Arnaldo Carvalho de Melo4152ab32011-07-25 11:06:19 -0300269
Namhyung Kimb3a319d2013-03-11 16:43:14 +0900270 for (cpu = 0; cpu < nr_cpus; cpu++) {
Arnaldo Carvalho de Melo4152ab32011-07-25 11:06:19 -0300271 list_for_each_entry(pos, &evlist->entries, node) {
Adrian Hunter395c3072013-08-31 21:50:53 +0300272 if (!perf_evsel__is_group_leader(pos) || !pos->fd)
Jiri Olsa3fe4430d2012-11-12 18:34:03 +0100273 continue;
Namhyung Kimb3a319d2013-03-11 16:43:14 +0900274 for (thread = 0; thread < nr_threads; thread++)
Namhyung Kim55da8002012-05-31 14:51:46 +0900275 ioctl(FD(pos, cpu, thread),
276 PERF_EVENT_IOC_DISABLE, 0);
Arnaldo Carvalho de Melo4152ab32011-07-25 11:06:19 -0300277 }
278 }
279}
280
David Ahern764e16a32011-08-25 10:17:55 -0600281void perf_evlist__enable(struct perf_evlist *evlist)
282{
283 int cpu, thread;
284 struct perf_evsel *pos;
Namhyung Kimb3a319d2013-03-11 16:43:14 +0900285 int nr_cpus = cpu_map__nr(evlist->cpus);
286 int nr_threads = thread_map__nr(evlist->threads);
David Ahern764e16a32011-08-25 10:17:55 -0600287
Namhyung Kimb3a319d2013-03-11 16:43:14 +0900288 for (cpu = 0; cpu < nr_cpus; cpu++) {
David Ahern764e16a32011-08-25 10:17:55 -0600289 list_for_each_entry(pos, &evlist->entries, node) {
Adrian Hunter395c3072013-08-31 21:50:53 +0300290 if (!perf_evsel__is_group_leader(pos) || !pos->fd)
Jiri Olsa3fe4430d2012-11-12 18:34:03 +0100291 continue;
Namhyung Kimb3a319d2013-03-11 16:43:14 +0900292 for (thread = 0; thread < nr_threads; thread++)
Namhyung Kim55da8002012-05-31 14:51:46 +0900293 ioctl(FD(pos, cpu, thread),
294 PERF_EVENT_IOC_ENABLE, 0);
David Ahern764e16a32011-08-25 10:17:55 -0600295 }
296 }
297}
298
Adrian Hunter395c3072013-08-31 21:50:53 +0300299int perf_evlist__disable_event(struct perf_evlist *evlist,
300 struct perf_evsel *evsel)
301{
302 int cpu, thread, err;
303
304 if (!evsel->fd)
305 return 0;
306
307 for (cpu = 0; cpu < evlist->cpus->nr; cpu++) {
308 for (thread = 0; thread < evlist->threads->nr; thread++) {
309 err = ioctl(FD(evsel, cpu, thread),
310 PERF_EVENT_IOC_DISABLE, 0);
311 if (err)
312 return err;
313 }
314 }
315 return 0;
316}
317
318int perf_evlist__enable_event(struct perf_evlist *evlist,
319 struct perf_evsel *evsel)
320{
321 int cpu, thread, err;
322
323 if (!evsel->fd)
324 return -EINVAL;
325
326 for (cpu = 0; cpu < evlist->cpus->nr; cpu++) {
327 for (thread = 0; thread < evlist->threads->nr; thread++) {
328 err = ioctl(FD(evsel, cpu, thread),
329 PERF_EVENT_IOC_ENABLE, 0);
330 if (err)
331 return err;
332 }
333 }
334 return 0;
335}
336
Arnaldo Carvalho de Melo806fb632011-11-29 08:05:52 -0200337static int perf_evlist__alloc_pollfd(struct perf_evlist *evlist)
Arnaldo Carvalho de Melo5c581042011-01-11 22:30:02 -0200338{
Namhyung Kimb3a319d2013-03-11 16:43:14 +0900339 int nr_cpus = cpu_map__nr(evlist->cpus);
340 int nr_threads = thread_map__nr(evlist->threads);
341 int nfds = nr_cpus * nr_threads * evlist->nr_entries;
Arnaldo Carvalho de Melo5c581042011-01-11 22:30:02 -0200342 evlist->pollfd = malloc(sizeof(struct pollfd) * nfds);
343 return evlist->pollfd != NULL ? 0 : -ENOMEM;
344}
Arnaldo Carvalho de Melo70082dd2011-01-12 17:03:24 -0200345
346void perf_evlist__add_pollfd(struct perf_evlist *evlist, int fd)
347{
348 fcntl(fd, F_SETFL, O_NONBLOCK);
349 evlist->pollfd[evlist->nr_fds].fd = fd;
350 evlist->pollfd[evlist->nr_fds].events = POLLIN;
351 evlist->nr_fds++;
352}
Arnaldo Carvalho de Melo70db7532011-01-12 22:39:13 -0200353
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -0300354static void perf_evlist__id_hash(struct perf_evlist *evlist,
355 struct perf_evsel *evsel,
356 int cpu, int thread, u64 id)
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200357{
Arnaldo Carvalho de Melo3d3b5e92011-03-04 22:29:39 -0300358 int hash;
359 struct perf_sample_id *sid = SID(evsel, cpu, thread);
360
361 sid->id = id;
362 sid->evsel = evsel;
363 hash = hash_64(sid->id, PERF_EVLIST__HLIST_BITS);
364 hlist_add_head(&sid->node, &evlist->heads[hash]);
365}
366
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -0300367void perf_evlist__id_add(struct perf_evlist *evlist, struct perf_evsel *evsel,
368 int cpu, int thread, u64 id)
369{
370 perf_evlist__id_hash(evlist, evsel, cpu, thread, id);
371 evsel->id[evsel->ids++] = id;
372}
373
374static int perf_evlist__id_add_fd(struct perf_evlist *evlist,
375 struct perf_evsel *evsel,
376 int cpu, int thread, int fd)
Arnaldo Carvalho de Melo3d3b5e92011-03-04 22:29:39 -0300377{
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200378 u64 read_data[4] = { 0, };
Arnaldo Carvalho de Melo3d3b5e92011-03-04 22:29:39 -0300379 int id_idx = 1; /* The first entry is the counter value */
Jiri Olsae2b5abe2012-04-04 19:32:27 +0200380 u64 id;
381 int ret;
382
383 ret = ioctl(fd, PERF_EVENT_IOC_ID, &id);
384 if (!ret)
385 goto add;
386
387 if (errno != ENOTTY)
388 return -1;
389
390 /* Legacy way to get event id.. All hail to old kernels! */
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200391
Jiri Olsac4861af2012-10-12 13:02:21 +0200392 /*
393 * This way does not work with group format read, so bail
394 * out in that case.
395 */
396 if (perf_evlist__read_format(evlist) & PERF_FORMAT_GROUP)
397 return -1;
398
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200399 if (!(evsel->attr.read_format & PERF_FORMAT_ID) ||
400 read(fd, &read_data, sizeof(read_data)) == -1)
401 return -1;
402
403 if (evsel->attr.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
404 ++id_idx;
405 if (evsel->attr.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
406 ++id_idx;
407
Jiri Olsae2b5abe2012-04-04 19:32:27 +0200408 id = read_data[id_idx];
409
410 add:
411 perf_evlist__id_add(evlist, evsel, cpu, thread, id);
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200412 return 0;
413}
414
Jiri Olsa932a3592012-10-11 14:10:35 +0200415struct perf_sample_id *perf_evlist__id2sid(struct perf_evlist *evlist, u64 id)
Arnaldo Carvalho de Melo70db7532011-01-12 22:39:13 -0200416{
417 struct hlist_head *head;
Arnaldo Carvalho de Melo70db7532011-01-12 22:39:13 -0200418 struct perf_sample_id *sid;
419 int hash;
420
Arnaldo Carvalho de Melo70db7532011-01-12 22:39:13 -0200421 hash = hash_64(id, PERF_EVLIST__HLIST_BITS);
422 head = &evlist->heads[hash];
423
Sasha Levinb67bfe02013-02-27 17:06:00 -0800424 hlist_for_each_entry(sid, head, node)
Arnaldo Carvalho de Melo70db7532011-01-12 22:39:13 -0200425 if (sid->id == id)
Jiri Olsa932a3592012-10-11 14:10:35 +0200426 return sid;
427
428 return NULL;
429}
430
431struct perf_evsel *perf_evlist__id2evsel(struct perf_evlist *evlist, u64 id)
432{
433 struct perf_sample_id *sid;
434
435 if (evlist->nr_entries == 1)
436 return perf_evlist__first(evlist);
437
438 sid = perf_evlist__id2sid(evlist, id);
439 if (sid)
440 return sid->evsel;
Namhyung Kim30e68bc2012-02-20 10:47:26 +0900441
442 if (!perf_evlist__sample_id_all(evlist))
Arnaldo Carvalho de Melo0c21f732012-08-14 16:42:15 -0300443 return perf_evlist__first(evlist);
Namhyung Kim30e68bc2012-02-20 10:47:26 +0900444
Arnaldo Carvalho de Melo70db7532011-01-12 22:39:13 -0200445 return NULL;
446}
Arnaldo Carvalho de Melo04391de2011-01-15 10:40:59 -0200447
Adrian Hunter75562572013-08-27 11:23:09 +0300448static int perf_evlist__event2id(struct perf_evlist *evlist,
449 union perf_event *event, u64 *id)
450{
451 const u64 *array = event->sample.array;
452 ssize_t n;
453
454 n = (event->header.size - sizeof(event->header)) >> 3;
455
456 if (event->header.type == PERF_RECORD_SAMPLE) {
457 if (evlist->id_pos >= n)
458 return -1;
459 *id = array[evlist->id_pos];
460 } else {
461 if (evlist->is_pos > n)
462 return -1;
463 n -= evlist->is_pos;
464 *id = array[n];
465 }
466 return 0;
467}
468
469static struct perf_evsel *perf_evlist__event2evsel(struct perf_evlist *evlist,
470 union perf_event *event)
471{
Adrian Hunter98be6962013-09-04 23:18:17 +0300472 struct perf_evsel *first = perf_evlist__first(evlist);
Adrian Hunter75562572013-08-27 11:23:09 +0300473 struct hlist_head *head;
474 struct perf_sample_id *sid;
475 int hash;
476 u64 id;
477
478 if (evlist->nr_entries == 1)
Adrian Hunter98be6962013-09-04 23:18:17 +0300479 return first;
480
481 if (!first->attr.sample_id_all &&
482 event->header.type != PERF_RECORD_SAMPLE)
483 return first;
Adrian Hunter75562572013-08-27 11:23:09 +0300484
485 if (perf_evlist__event2id(evlist, event, &id))
486 return NULL;
487
488 /* Synthesized events have an id of zero */
489 if (!id)
Adrian Hunter98be6962013-09-04 23:18:17 +0300490 return first;
Adrian Hunter75562572013-08-27 11:23:09 +0300491
492 hash = hash_64(id, PERF_EVLIST__HLIST_BITS);
493 head = &evlist->heads[hash];
494
495 hlist_for_each_entry(sid, head, node) {
496 if (sid->id == id)
497 return sid->evsel;
498 }
499 return NULL;
500}
501
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -0300502union perf_event *perf_evlist__mmap_read(struct perf_evlist *evlist, int idx)
Arnaldo Carvalho de Melo04391de2011-01-15 10:40:59 -0200503{
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -0300504 struct perf_mmap *md = &evlist->mmap[idx];
Arnaldo Carvalho de Melo04391de2011-01-15 10:40:59 -0200505 unsigned int head = perf_mmap__read_head(md);
506 unsigned int old = md->prev;
507 unsigned char *data = md->base + page_size;
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200508 union perf_event *event = NULL;
Arnaldo Carvalho de Melo04391de2011-01-15 10:40:59 -0200509
Arnaldo Carvalho de Melo7bb41152011-01-29 09:08:13 -0200510 if (evlist->overwrite) {
Arnaldo Carvalho de Melo04391de2011-01-15 10:40:59 -0200511 /*
Arnaldo Carvalho de Melo7bb41152011-01-29 09:08:13 -0200512 * If we're further behind than half the buffer, there's a chance
513 * the writer will bite our tail and mess up the samples under us.
514 *
515 * If we somehow ended up ahead of the head, we got messed up.
516 *
517 * In either case, truncate and restart at head.
Arnaldo Carvalho de Melo04391de2011-01-15 10:40:59 -0200518 */
Arnaldo Carvalho de Melo7bb41152011-01-29 09:08:13 -0200519 int diff = head - old;
520 if (diff > md->mask / 2 || diff < 0) {
521 fprintf(stderr, "WARNING: failed to keep up with mmap data.\n");
522
523 /*
524 * head points to a known good entry, start there.
525 */
526 old = head;
527 }
Arnaldo Carvalho de Melo04391de2011-01-15 10:40:59 -0200528 }
529
530 if (old != head) {
531 size_t size;
532
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200533 event = (union perf_event *)&data[old & md->mask];
Arnaldo Carvalho de Melo04391de2011-01-15 10:40:59 -0200534 size = event->header.size;
535
536 /*
537 * Event straddles the mmap boundary -- header should always
538 * be inside due to u64 alignment of output.
539 */
540 if ((old & md->mask) + size != ((old + size) & md->mask)) {
541 unsigned int offset = old;
542 unsigned int len = min(sizeof(*event), size), cpy;
David Ahern0479b8b2013-02-05 14:12:42 -0700543 void *dst = &md->event_copy;
Arnaldo Carvalho de Melo04391de2011-01-15 10:40:59 -0200544
545 do {
546 cpy = min(md->mask + 1 - (offset & md->mask), len);
547 memcpy(dst, &data[offset & md->mask], cpy);
548 offset += cpy;
549 dst += cpy;
550 len -= cpy;
551 } while (len);
552
David Ahern0479b8b2013-02-05 14:12:42 -0700553 event = &md->event_copy;
Arnaldo Carvalho de Melo04391de2011-01-15 10:40:59 -0200554 }
555
556 old += size;
557 }
558
559 md->prev = old;
Arnaldo Carvalho de Melo7bb41152011-01-29 09:08:13 -0200560
561 if (!evlist->overwrite)
562 perf_mmap__write_tail(md, old);
563
Arnaldo Carvalho de Melo04391de2011-01-15 10:40:59 -0200564 return event;
565}
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200566
Adrian Hunter93edcbd2013-07-04 16:20:26 +0300567static void __perf_evlist__munmap(struct perf_evlist *evlist, int idx)
568{
569 if (evlist->mmap[idx].base != NULL) {
570 munmap(evlist->mmap[idx].base, evlist->mmap_len);
571 evlist->mmap[idx].base = NULL;
572 }
573}
574
Arnaldo Carvalho de Melo7e2ed092011-01-30 11:59:43 -0200575void perf_evlist__munmap(struct perf_evlist *evlist)
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200576{
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -0300577 int i;
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200578
Adrian Hunter93edcbd2013-07-04 16:20:26 +0300579 for (i = 0; i < evlist->nr_mmaps; i++)
580 __perf_evlist__munmap(evlist, i);
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -0300581
582 free(evlist->mmap);
583 evlist->mmap = NULL;
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200584}
585
Arnaldo Carvalho de Melo806fb632011-11-29 08:05:52 -0200586static int perf_evlist__alloc_mmap(struct perf_evlist *evlist)
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200587{
Arnaldo Carvalho de Meloa14bb7a2012-09-26 12:41:14 -0300588 evlist->nr_mmaps = cpu_map__nr(evlist->cpus);
Sukadev Bhattiproluec1e7e42013-05-22 17:42:38 -0700589 if (cpu_map__empty(evlist->cpus))
Namhyung Kimb3a319d2013-03-11 16:43:14 +0900590 evlist->nr_mmaps = thread_map__nr(evlist->threads);
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -0300591 evlist->mmap = zalloc(evlist->nr_mmaps * sizeof(struct perf_mmap));
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200592 return evlist->mmap != NULL ? 0 : -ENOMEM;
593}
594
Arnaldo Carvalho de Melobccdaba2011-06-02 10:39:43 -0300595static int __perf_evlist__mmap(struct perf_evlist *evlist,
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -0300596 int idx, int prot, int mask, int fd)
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200597{
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -0300598 evlist->mmap[idx].prev = 0;
599 evlist->mmap[idx].mask = mask;
600 evlist->mmap[idx].base = mmap(NULL, evlist->mmap_len, prot,
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200601 MAP_SHARED, fd, 0);
Nelson Elhage301b1952011-12-19 08:39:30 -0500602 if (evlist->mmap[idx].base == MAP_FAILED) {
603 evlist->mmap[idx].base = NULL;
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200604 return -1;
Nelson Elhage301b1952011-12-19 08:39:30 -0500605 }
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200606
607 perf_evlist__add_pollfd(evlist, fd);
608 return 0;
609}
610
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -0300611static int perf_evlist__mmap_per_cpu(struct perf_evlist *evlist, int prot, int mask)
612{
613 struct perf_evsel *evsel;
614 int cpu, thread;
Namhyung Kimb3a319d2013-03-11 16:43:14 +0900615 int nr_cpus = cpu_map__nr(evlist->cpus);
616 int nr_threads = thread_map__nr(evlist->threads);
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -0300617
Adrian Huntere3e1a542013-08-14 15:48:24 +0300618 pr_debug2("perf event ring buffer mmapped per cpu\n");
Namhyung Kimb3a319d2013-03-11 16:43:14 +0900619 for (cpu = 0; cpu < nr_cpus; cpu++) {
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -0300620 int output = -1;
621
Namhyung Kimb3a319d2013-03-11 16:43:14 +0900622 for (thread = 0; thread < nr_threads; thread++) {
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -0300623 list_for_each_entry(evsel, &evlist->entries, node) {
624 int fd = FD(evsel, cpu, thread);
625
626 if (output == -1) {
627 output = fd;
Arnaldo Carvalho de Melobccdaba2011-06-02 10:39:43 -0300628 if (__perf_evlist__mmap(evlist, cpu,
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -0300629 prot, mask, output) < 0)
630 goto out_unmap;
631 } else {
632 if (ioctl(fd, PERF_EVENT_IOC_SET_OUTPUT, output) != 0)
633 goto out_unmap;
634 }
635
636 if ((evsel->attr.read_format & PERF_FORMAT_ID) &&
637 perf_evlist__id_add_fd(evlist, evsel, cpu, thread, fd) < 0)
638 goto out_unmap;
639 }
640 }
641 }
642
643 return 0;
644
645out_unmap:
Adrian Hunter93edcbd2013-07-04 16:20:26 +0300646 for (cpu = 0; cpu < nr_cpus; cpu++)
647 __perf_evlist__munmap(evlist, cpu);
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -0300648 return -1;
649}
650
651static int perf_evlist__mmap_per_thread(struct perf_evlist *evlist, int prot, int mask)
652{
653 struct perf_evsel *evsel;
654 int thread;
Namhyung Kimb3a319d2013-03-11 16:43:14 +0900655 int nr_threads = thread_map__nr(evlist->threads);
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -0300656
Adrian Huntere3e1a542013-08-14 15:48:24 +0300657 pr_debug2("perf event ring buffer mmapped per thread\n");
Namhyung Kimb3a319d2013-03-11 16:43:14 +0900658 for (thread = 0; thread < nr_threads; thread++) {
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -0300659 int output = -1;
660
661 list_for_each_entry(evsel, &evlist->entries, node) {
662 int fd = FD(evsel, 0, thread);
663
664 if (output == -1) {
665 output = fd;
Arnaldo Carvalho de Melobccdaba2011-06-02 10:39:43 -0300666 if (__perf_evlist__mmap(evlist, thread,
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -0300667 prot, mask, output) < 0)
668 goto out_unmap;
669 } else {
670 if (ioctl(fd, PERF_EVENT_IOC_SET_OUTPUT, output) != 0)
671 goto out_unmap;
672 }
673
674 if ((evsel->attr.read_format & PERF_FORMAT_ID) &&
675 perf_evlist__id_add_fd(evlist, evsel, 0, thread, fd) < 0)
676 goto out_unmap;
677 }
678 }
679
680 return 0;
681
682out_unmap:
Adrian Hunter93edcbd2013-07-04 16:20:26 +0300683 for (thread = 0; thread < nr_threads; thread++)
684 __perf_evlist__munmap(evlist, thread);
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -0300685 return -1;
686}
687
Jiri Olsa994a1f72013-09-01 12:36:12 +0200688static size_t perf_evlist__mmap_size(unsigned long pages)
689{
690 /* 512 kiB: default amount of unprivileged mlocked memory */
691 if (pages == UINT_MAX)
692 pages = (512 * 1024) / page_size;
693 else if (!is_power_of_2(pages))
694 return 0;
695
696 return (pages + 1) * page_size;
697}
698
699int perf_evlist__parse_mmap_pages(const struct option *opt, const char *str,
700 int unset __maybe_unused)
701{
Jiri Olsa27050f52013-09-01 12:36:13 +0200702 unsigned int pages, val, *mmap_pages = opt->value;
Jiri Olsa994a1f72013-09-01 12:36:12 +0200703 size_t size;
Jiri Olsa27050f52013-09-01 12:36:13 +0200704 static struct parse_tag tags[] = {
705 { .tag = 'B', .mult = 1 },
706 { .tag = 'K', .mult = 1 << 10 },
707 { .tag = 'M', .mult = 1 << 20 },
708 { .tag = 'G', .mult = 1 << 30 },
709 { .tag = 0 },
710 };
Jiri Olsa994a1f72013-09-01 12:36:12 +0200711
Jiri Olsa27050f52013-09-01 12:36:13 +0200712 val = parse_tag_value(str, tags);
713 if (val != (unsigned int) -1) {
714 /* we got file size value */
715 pages = PERF_ALIGN(val, page_size) / page_size;
716 if (!is_power_of_2(pages)) {
717 pages = next_pow2(pages);
718 pr_info("rounding mmap pages size to %u (%u pages)\n",
719 pages * page_size, pages);
720 }
721 } else {
722 /* we got pages count value */
723 char *eptr;
724 pages = strtoul(str, &eptr, 10);
725 if (*eptr != '\0') {
726 pr_err("failed to parse --mmap_pages/-m value\n");
727 return -1;
728 }
Jiri Olsa994a1f72013-09-01 12:36:12 +0200729 }
730
731 size = perf_evlist__mmap_size(pages);
732 if (!size) {
733 pr_err("--mmap_pages/-m value must be a power of two.");
734 return -1;
735 }
736
737 *mmap_pages = pages;
738 return 0;
739}
740
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200741/** perf_evlist__mmap - Create per cpu maps to receive events
742 *
743 * @evlist - list of events
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200744 * @pages - map length in pages
745 * @overwrite - overwrite older events?
746 *
747 * If overwrite is false the user needs to signal event consuption using:
748 *
749 * struct perf_mmap *m = &evlist->mmap[cpu];
750 * unsigned int head = perf_mmap__read_head(m);
751 *
752 * perf_mmap__write_tail(m, head)
Arnaldo Carvalho de Melo7e2ed092011-01-30 11:59:43 -0200753 *
754 * Using perf_evlist__read_on_cpu does this automatically.
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200755 */
Arnaldo Carvalho de Melo50a682c2011-11-09 09:10:47 -0200756int perf_evlist__mmap(struct perf_evlist *evlist, unsigned int pages,
757 bool overwrite)
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200758{
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -0300759 struct perf_evsel *evsel;
Arnaldo Carvalho de Melo7e2ed092011-01-30 11:59:43 -0200760 const struct cpu_map *cpus = evlist->cpus;
761 const struct thread_map *threads = evlist->threads;
Arnaldo Carvalho de Melo50a682c2011-11-09 09:10:47 -0200762 int prot = PROT_READ | (overwrite ? 0 : PROT_WRITE), mask;
763
Arnaldo Carvalho de Melo7e2ed092011-01-30 11:59:43 -0200764 if (evlist->mmap == NULL && perf_evlist__alloc_mmap(evlist) < 0)
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200765 return -ENOMEM;
766
Arnaldo Carvalho de Melo7e2ed092011-01-30 11:59:43 -0200767 if (evlist->pollfd == NULL && perf_evlist__alloc_pollfd(evlist) < 0)
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200768 return -ENOMEM;
769
770 evlist->overwrite = overwrite;
Jiri Olsa994a1f72013-09-01 12:36:12 +0200771 evlist->mmap_len = perf_evlist__mmap_size(pages);
Jiri Olsa27050f52013-09-01 12:36:13 +0200772 pr_debug("mmap size %luB\n", evlist->mmap_len);
Jiri Olsa994a1f72013-09-01 12:36:12 +0200773 mask = evlist->mmap_len - page_size - 1;
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200774
775 list_for_each_entry(evsel, &evlist->entries, node) {
776 if ((evsel->attr.read_format & PERF_FORMAT_ID) &&
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -0300777 evsel->sample_id == NULL &&
Arnaldo Carvalho de Meloa14bb7a2012-09-26 12:41:14 -0300778 perf_evsel__alloc_id(evsel, cpu_map__nr(cpus), threads->nr) < 0)
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200779 return -ENOMEM;
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200780 }
781
Sukadev Bhattiproluec1e7e42013-05-22 17:42:38 -0700782 if (cpu_map__empty(cpus))
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -0300783 return perf_evlist__mmap_per_thread(evlist, prot, mask);
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200784
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -0300785 return perf_evlist__mmap_per_cpu(evlist, prot, mask);
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200786}
Arnaldo Carvalho de Melo7e2ed092011-01-30 11:59:43 -0200787
Namhyung Kimb809ac12012-04-26 14:15:19 +0900788int perf_evlist__create_maps(struct perf_evlist *evlist,
789 struct perf_target *target)
Arnaldo Carvalho de Melo7e2ed092011-01-30 11:59:43 -0200790{
Namhyung Kimb809ac12012-04-26 14:15:19 +0900791 evlist->threads = thread_map__new_str(target->pid, target->tid,
792 target->uid);
Arnaldo Carvalho de Melo7e2ed092011-01-30 11:59:43 -0200793
794 if (evlist->threads == NULL)
795 return -1;
796
Namhyung Kim879d77d2012-05-16 18:45:48 +0900797 if (perf_target__has_task(target))
Namhyung Kimd67356e2012-05-07 14:09:03 +0900798 evlist->cpus = cpu_map__dummy_new();
Namhyung Kimd1cb9fc2012-05-16 18:45:49 +0900799 else if (!perf_target__has_cpu(target) && !target->uses_mmap)
800 evlist->cpus = cpu_map__dummy_new();
Namhyung Kim879d77d2012-05-16 18:45:48 +0900801 else
802 evlist->cpus = cpu_map__new(target->cpu_list);
Arnaldo Carvalho de Melo7e2ed092011-01-30 11:59:43 -0200803
804 if (evlist->cpus == NULL)
805 goto out_delete_threads;
806
807 return 0;
808
809out_delete_threads:
810 thread_map__delete(evlist->threads);
811 return -1;
812}
813
814void perf_evlist__delete_maps(struct perf_evlist *evlist)
815{
816 cpu_map__delete(evlist->cpus);
817 thread_map__delete(evlist->threads);
818 evlist->cpus = NULL;
819 evlist->threads = NULL;
820}
Frederic Weisbecker0a102472011-02-26 04:51:54 +0100821
Arnaldo Carvalho de Melo1491a632012-09-26 14:43:13 -0300822int perf_evlist__apply_filters(struct perf_evlist *evlist)
Frederic Weisbecker0a102472011-02-26 04:51:54 +0100823{
Frederic Weisbecker0a102472011-02-26 04:51:54 +0100824 struct perf_evsel *evsel;
Arnaldo Carvalho de Melo745cefc2012-09-26 15:07:39 -0300825 int err = 0;
826 const int ncpus = cpu_map__nr(evlist->cpus),
Namhyung Kimb3a319d2013-03-11 16:43:14 +0900827 nthreads = thread_map__nr(evlist->threads);
Frederic Weisbecker0a102472011-02-26 04:51:54 +0100828
829 list_for_each_entry(evsel, &evlist->entries, node) {
Arnaldo Carvalho de Melo745cefc2012-09-26 15:07:39 -0300830 if (evsel->filter == NULL)
Frederic Weisbecker0a102472011-02-26 04:51:54 +0100831 continue;
Arnaldo Carvalho de Melo745cefc2012-09-26 15:07:39 -0300832
833 err = perf_evsel__set_filter(evsel, ncpus, nthreads, evsel->filter);
834 if (err)
835 break;
Frederic Weisbecker0a102472011-02-26 04:51:54 +0100836 }
837
Arnaldo Carvalho de Melo745cefc2012-09-26 15:07:39 -0300838 return err;
839}
840
841int perf_evlist__set_filter(struct perf_evlist *evlist, const char *filter)
842{
843 struct perf_evsel *evsel;
844 int err = 0;
845 const int ncpus = cpu_map__nr(evlist->cpus),
Namhyung Kimb3a319d2013-03-11 16:43:14 +0900846 nthreads = thread_map__nr(evlist->threads);
Arnaldo Carvalho de Melo745cefc2012-09-26 15:07:39 -0300847
848 list_for_each_entry(evsel, &evlist->entries, node) {
849 err = perf_evsel__set_filter(evsel, ncpus, nthreads, filter);
850 if (err)
851 break;
852 }
853
854 return err;
Frederic Weisbecker0a102472011-02-26 04:51:54 +0100855}
Frederic Weisbecker74429962011-05-21 17:49:00 +0200856
Arnaldo Carvalho de Melo0c21f732012-08-14 16:42:15 -0300857bool perf_evlist__valid_sample_type(struct perf_evlist *evlist)
Frederic Weisbecker74429962011-05-21 17:49:00 +0200858{
Adrian Hunter75562572013-08-27 11:23:09 +0300859 struct perf_evsel *pos;
Arnaldo Carvalho de Meloc2a70652011-06-02 11:04:54 -0300860
Adrian Hunter75562572013-08-27 11:23:09 +0300861 if (evlist->nr_entries == 1)
862 return true;
863
864 if (evlist->id_pos < 0 || evlist->is_pos < 0)
865 return false;
866
867 list_for_each_entry(pos, &evlist->entries, node) {
868 if (pos->id_pos != evlist->id_pos ||
869 pos->is_pos != evlist->is_pos)
Arnaldo Carvalho de Meloc2a70652011-06-02 11:04:54 -0300870 return false;
Frederic Weisbecker74429962011-05-21 17:49:00 +0200871 }
872
Arnaldo Carvalho de Meloc2a70652011-06-02 11:04:54 -0300873 return true;
874}
875
Adrian Hunter75562572013-08-27 11:23:09 +0300876u64 __perf_evlist__combined_sample_type(struct perf_evlist *evlist)
Arnaldo Carvalho de Meloc2a70652011-06-02 11:04:54 -0300877{
Adrian Hunter75562572013-08-27 11:23:09 +0300878 struct perf_evsel *evsel;
879
880 if (evlist->combined_sample_type)
881 return evlist->combined_sample_type;
882
883 list_for_each_entry(evsel, &evlist->entries, node)
884 evlist->combined_sample_type |= evsel->attr.sample_type;
885
886 return evlist->combined_sample_type;
887}
888
889u64 perf_evlist__combined_sample_type(struct perf_evlist *evlist)
890{
891 evlist->combined_sample_type = 0;
892 return __perf_evlist__combined_sample_type(evlist);
Arnaldo Carvalho de Meloc2a70652011-06-02 11:04:54 -0300893}
894
Jiri Olsa9ede4732012-10-10 17:38:13 +0200895bool perf_evlist__valid_read_format(struct perf_evlist *evlist)
896{
897 struct perf_evsel *first = perf_evlist__first(evlist), *pos = first;
898 u64 read_format = first->attr.read_format;
899 u64 sample_type = first->attr.sample_type;
900
901 list_for_each_entry_continue(pos, &evlist->entries, node) {
902 if (read_format != pos->attr.read_format)
903 return false;
904 }
905
906 /* PERF_SAMPLE_READ imples PERF_FORMAT_ID. */
907 if ((sample_type & PERF_SAMPLE_READ) &&
908 !(read_format & PERF_FORMAT_ID)) {
909 return false;
910 }
911
912 return true;
913}
914
915u64 perf_evlist__read_format(struct perf_evlist *evlist)
916{
917 struct perf_evsel *first = perf_evlist__first(evlist);
918 return first->attr.read_format;
919}
920
Arnaldo Carvalho de Melo0c21f732012-08-14 16:42:15 -0300921u16 perf_evlist__id_hdr_size(struct perf_evlist *evlist)
Arnaldo Carvalho de Melo81e36bf2011-11-11 22:28:50 -0200922{
Arnaldo Carvalho de Melo0c21f732012-08-14 16:42:15 -0300923 struct perf_evsel *first = perf_evlist__first(evlist);
Arnaldo Carvalho de Melo81e36bf2011-11-11 22:28:50 -0200924 struct perf_sample *data;
925 u64 sample_type;
926 u16 size = 0;
927
Arnaldo Carvalho de Melo81e36bf2011-11-11 22:28:50 -0200928 if (!first->attr.sample_id_all)
929 goto out;
930
931 sample_type = first->attr.sample_type;
932
933 if (sample_type & PERF_SAMPLE_TID)
934 size += sizeof(data->tid) * 2;
935
936 if (sample_type & PERF_SAMPLE_TIME)
937 size += sizeof(data->time);
938
939 if (sample_type & PERF_SAMPLE_ID)
940 size += sizeof(data->id);
941
942 if (sample_type & PERF_SAMPLE_STREAM_ID)
943 size += sizeof(data->stream_id);
944
945 if (sample_type & PERF_SAMPLE_CPU)
946 size += sizeof(data->cpu) * 2;
Adrian Hunter75562572013-08-27 11:23:09 +0300947
948 if (sample_type & PERF_SAMPLE_IDENTIFIER)
949 size += sizeof(data->id);
Arnaldo Carvalho de Melo81e36bf2011-11-11 22:28:50 -0200950out:
951 return size;
952}
953
Arnaldo Carvalho de Melo0c21f732012-08-14 16:42:15 -0300954bool perf_evlist__valid_sample_id_all(struct perf_evlist *evlist)
Arnaldo Carvalho de Meloc2a70652011-06-02 11:04:54 -0300955{
Arnaldo Carvalho de Melo0c21f732012-08-14 16:42:15 -0300956 struct perf_evsel *first = perf_evlist__first(evlist), *pos = first;
Arnaldo Carvalho de Meloc2a70652011-06-02 11:04:54 -0300957
958 list_for_each_entry_continue(pos, &evlist->entries, node) {
959 if (first->attr.sample_id_all != pos->attr.sample_id_all)
960 return false;
961 }
962
963 return true;
Frederic Weisbecker74429962011-05-21 17:49:00 +0200964}
965
Arnaldo Carvalho de Melo0c21f732012-08-14 16:42:15 -0300966bool perf_evlist__sample_id_all(struct perf_evlist *evlist)
Frederic Weisbecker74429962011-05-21 17:49:00 +0200967{
Arnaldo Carvalho de Melo0c21f732012-08-14 16:42:15 -0300968 struct perf_evsel *first = perf_evlist__first(evlist);
Arnaldo Carvalho de Meloc2a70652011-06-02 11:04:54 -0300969 return first->attr.sample_id_all;
Frederic Weisbecker74429962011-05-21 17:49:00 +0200970}
Arnaldo Carvalho de Melo81cce8d2011-10-05 19:11:32 -0300971
972void perf_evlist__set_selected(struct perf_evlist *evlist,
973 struct perf_evsel *evsel)
974{
975 evlist->selected = evsel;
976}
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -0200977
Namhyung Kima74b4b62013-03-15 14:48:48 +0900978void perf_evlist__close(struct perf_evlist *evlist)
979{
980 struct perf_evsel *evsel;
981 int ncpus = cpu_map__nr(evlist->cpus);
982 int nthreads = thread_map__nr(evlist->threads);
983
984 list_for_each_entry_reverse(evsel, &evlist->entries, node)
985 perf_evsel__close(evsel, ncpus, nthreads);
986}
987
Jiri Olsa6a4bb042012-08-08 12:22:36 +0200988int perf_evlist__open(struct perf_evlist *evlist)
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -0200989{
Jiri Olsa6a4bb042012-08-08 12:22:36 +0200990 struct perf_evsel *evsel;
Namhyung Kima74b4b62013-03-15 14:48:48 +0900991 int err;
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -0200992
Adrian Hunter733cd2f2013-09-06 22:40:11 +0300993 perf_evlist__update_id_pos(evlist);
994
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -0200995 list_for_each_entry(evsel, &evlist->entries, node) {
Jiri Olsa6a4bb042012-08-08 12:22:36 +0200996 err = perf_evsel__open(evsel, evlist->cpus, evlist->threads);
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -0200997 if (err < 0)
998 goto out_err;
999 }
1000
1001 return 0;
1002out_err:
Namhyung Kima74b4b62013-03-15 14:48:48 +09001003 perf_evlist__close(evlist);
Namhyung Kim41c21a62012-02-23 12:13:36 +09001004 errno = -err;
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -02001005 return err;
1006}
Arnaldo Carvalho de Melo35b9d882011-11-09 08:47:15 -02001007
1008int perf_evlist__prepare_workload(struct perf_evlist *evlist,
Namhyung Kim6ef73ec2013-03-11 16:43:15 +09001009 struct perf_target *target,
Namhyung Kim55e162e2013-03-11 16:43:17 +09001010 const char *argv[], bool pipe_output,
1011 bool want_signal)
Arnaldo Carvalho de Melo35b9d882011-11-09 08:47:15 -02001012{
1013 int child_ready_pipe[2], go_pipe[2];
1014 char bf;
1015
1016 if (pipe(child_ready_pipe) < 0) {
1017 perror("failed to create 'ready' pipe");
1018 return -1;
1019 }
1020
1021 if (pipe(go_pipe) < 0) {
1022 perror("failed to create 'go' pipe");
1023 goto out_close_ready_pipe;
1024 }
1025
1026 evlist->workload.pid = fork();
1027 if (evlist->workload.pid < 0) {
1028 perror("failed to fork");
1029 goto out_close_pipes;
1030 }
1031
1032 if (!evlist->workload.pid) {
Namhyung Kim119fa3c2013-03-11 16:43:16 +09001033 if (pipe_output)
Arnaldo Carvalho de Melo35b9d882011-11-09 08:47:15 -02001034 dup2(2, 1);
1035
David Ahern0817df02013-05-25 17:50:39 -06001036 signal(SIGTERM, SIG_DFL);
1037
Arnaldo Carvalho de Melo35b9d882011-11-09 08:47:15 -02001038 close(child_ready_pipe[0]);
1039 close(go_pipe[1]);
1040 fcntl(go_pipe[0], F_SETFD, FD_CLOEXEC);
1041
1042 /*
Arnaldo Carvalho de Melo35b9d882011-11-09 08:47:15 -02001043 * Tell the parent we're ready to go
1044 */
1045 close(child_ready_pipe[1]);
1046
1047 /*
1048 * Wait until the parent tells us to go.
1049 */
1050 if (read(go_pipe[0], &bf, 1) == -1)
1051 perror("unable to read pipe");
1052
1053 execvp(argv[0], (char **)argv);
1054
1055 perror(argv[0]);
Namhyung Kim55e162e2013-03-11 16:43:17 +09001056 if (want_signal)
1057 kill(getppid(), SIGUSR1);
Arnaldo Carvalho de Melo35b9d882011-11-09 08:47:15 -02001058 exit(-1);
1059 }
1060
Namhyung Kim6ef73ec2013-03-11 16:43:15 +09001061 if (perf_target__none(target))
Arnaldo Carvalho de Melo35b9d882011-11-09 08:47:15 -02001062 evlist->threads->map[0] = evlist->workload.pid;
1063
1064 close(child_ready_pipe[1]);
1065 close(go_pipe[0]);
1066 /*
1067 * wait for child to settle
1068 */
1069 if (read(child_ready_pipe[0], &bf, 1) == -1) {
1070 perror("unable to read pipe");
1071 goto out_close_pipes;
1072 }
1073
Namhyung Kimbcf31452013-06-26 16:14:15 +09001074 fcntl(go_pipe[1], F_SETFD, FD_CLOEXEC);
Arnaldo Carvalho de Melo35b9d882011-11-09 08:47:15 -02001075 evlist->workload.cork_fd = go_pipe[1];
1076 close(child_ready_pipe[0]);
1077 return 0;
1078
1079out_close_pipes:
1080 close(go_pipe[0]);
1081 close(go_pipe[1]);
1082out_close_ready_pipe:
1083 close(child_ready_pipe[0]);
1084 close(child_ready_pipe[1]);
1085 return -1;
1086}
1087
1088int perf_evlist__start_workload(struct perf_evlist *evlist)
1089{
1090 if (evlist->workload.cork_fd > 0) {
David Ahernb3824402013-07-02 13:27:21 -06001091 char bf = 0;
Namhyung Kimbcf31452013-06-26 16:14:15 +09001092 int ret;
Arnaldo Carvalho de Melo35b9d882011-11-09 08:47:15 -02001093 /*
1094 * Remove the cork, let it rip!
1095 */
Namhyung Kimbcf31452013-06-26 16:14:15 +09001096 ret = write(evlist->workload.cork_fd, &bf, 1);
1097 if (ret < 0)
1098 perror("enable to write to pipe");
1099
1100 close(evlist->workload.cork_fd);
1101 return ret;
Arnaldo Carvalho de Melo35b9d882011-11-09 08:47:15 -02001102 }
1103
1104 return 0;
1105}
Arnaldo Carvalho de Melocb0b29e2012-08-02 11:42:57 -03001106
Arnaldo Carvalho de Meloa3f698f2012-08-02 12:23:46 -03001107int perf_evlist__parse_sample(struct perf_evlist *evlist, union perf_event *event,
Arnaldo Carvalho de Melo0807d2d2012-09-26 12:48:18 -03001108 struct perf_sample *sample)
Arnaldo Carvalho de Melocb0b29e2012-08-02 11:42:57 -03001109{
Adrian Hunter75562572013-08-27 11:23:09 +03001110 struct perf_evsel *evsel = perf_evlist__event2evsel(evlist, event);
1111
1112 if (!evsel)
1113 return -EFAULT;
Arnaldo Carvalho de Melo0807d2d2012-09-26 12:48:18 -03001114 return perf_evsel__parse_sample(evsel, event, sample);
Arnaldo Carvalho de Melocb0b29e2012-08-02 11:42:57 -03001115}
Arnaldo Carvalho de Melo78f067b2012-09-06 14:54:11 -03001116
1117size_t perf_evlist__fprintf(struct perf_evlist *evlist, FILE *fp)
1118{
1119 struct perf_evsel *evsel;
1120 size_t printed = 0;
1121
1122 list_for_each_entry(evsel, &evlist->entries, node) {
1123 printed += fprintf(fp, "%s%s", evsel->idx ? ", " : "",
1124 perf_evsel__name(evsel));
1125 }
1126
1127 return printed + fprintf(fp, "\n");;
1128}