blob: 183b199b0d0952e7256fb85db02e35b3ae408711 [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"
10#include "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
Arnaldo Carvalho de Melo7e2ed092011-01-30 11:59:43 -020041struct perf_evlist *perf_evlist__new(struct cpu_map *cpus,
42 struct thread_map *threads)
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)
Arnaldo Carvalho de Melo7e2ed092011-01-30 11:59:43 -020047 perf_evlist__init(evlist, cpus, threads);
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -020048
49 return evlist;
50}
51
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -020052void perf_evlist__config_attrs(struct perf_evlist *evlist,
53 struct perf_record_opts *opts)
54{
Namhyung Kim5090c6a2012-03-16 17:42:20 +090055 struct perf_evsel *evsel, *first;
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -020056
57 if (evlist->cpus->map[0] < 0)
58 opts->no_inherit = true;
59
Namhyung Kim5090c6a2012-03-16 17:42:20 +090060 first = list_entry(evlist->entries.next, struct perf_evsel, node);
61
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -020062 list_for_each_entry(evsel, &evlist->entries, node) {
Namhyung Kim5090c6a2012-03-16 17:42:20 +090063 perf_evsel__config(evsel, opts, first);
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -020064
65 if (evlist->nr_entries > 1)
66 evsel->attr.sample_type |= PERF_SAMPLE_ID;
67 }
68}
69
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -020070static void perf_evlist__purge(struct perf_evlist *evlist)
71{
72 struct perf_evsel *pos, *n;
73
74 list_for_each_entry_safe(pos, n, &evlist->entries, node) {
75 list_del_init(&pos->node);
76 perf_evsel__delete(pos);
77 }
78
79 evlist->nr_entries = 0;
80}
81
Arnaldo Carvalho de Meloef1d1af2011-01-18 21:41:45 -020082void perf_evlist__exit(struct perf_evlist *evlist)
83{
84 free(evlist->mmap);
85 free(evlist->pollfd);
86 evlist->mmap = NULL;
87 evlist->pollfd = NULL;
88}
89
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -020090void perf_evlist__delete(struct perf_evlist *evlist)
91{
92 perf_evlist__purge(evlist);
Arnaldo Carvalho de Meloef1d1af2011-01-18 21:41:45 -020093 perf_evlist__exit(evlist);
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -020094 free(evlist);
95}
96
97void perf_evlist__add(struct perf_evlist *evlist, struct perf_evsel *entry)
98{
99 list_add_tail(&entry->node, &evlist->entries);
100 ++evlist->nr_entries;
101}
102
Jiri Olsa0529bc12012-01-27 15:34:20 +0100103void perf_evlist__splice_list_tail(struct perf_evlist *evlist,
104 struct list_head *list,
105 int nr_entries)
Arnaldo Carvalho de Melo50d08e42011-11-04 09:10:59 -0200106{
107 list_splice_tail(list, &evlist->entries);
108 evlist->nr_entries += nr_entries;
109}
110
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -0200111int perf_evlist__add_default(struct perf_evlist *evlist)
112{
113 struct perf_event_attr attr = {
114 .type = PERF_TYPE_HARDWARE,
115 .config = PERF_COUNT_HW_CPU_CYCLES,
116 };
Joerg Roedel1aed2672012-01-04 17:54:20 +0100117 struct perf_evsel *evsel;
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -0200118
Joerg Roedel1aed2672012-01-04 17:54:20 +0100119 event_attr_init(&attr);
120
121 evsel = perf_evsel__new(&attr, 0);
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -0200122 if (evsel == NULL)
Stephane Eraniancc2d86b2011-06-07 18:19:36 +0200123 goto error;
124
125 /* use strdup() because free(evsel) assumes name is allocated */
126 evsel->name = strdup("cycles");
127 if (!evsel->name)
128 goto error_free;
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -0200129
130 perf_evlist__add(evlist, evsel);
131 return 0;
Stephane Eraniancc2d86b2011-06-07 18:19:36 +0200132error_free:
133 perf_evsel__delete(evsel);
134error:
135 return -ENOMEM;
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -0200136}
Arnaldo Carvalho de Melo5c581042011-01-11 22:30:02 -0200137
Arnaldo Carvalho de Melo50d08e42011-11-04 09:10:59 -0200138int perf_evlist__add_attrs(struct perf_evlist *evlist,
139 struct perf_event_attr *attrs, size_t nr_attrs)
140{
141 struct perf_evsel *evsel, *n;
142 LIST_HEAD(head);
143 size_t i;
144
145 for (i = 0; i < nr_attrs; i++) {
146 evsel = perf_evsel__new(attrs + i, evlist->nr_entries + i);
147 if (evsel == NULL)
148 goto out_delete_partial_list;
149 list_add_tail(&evsel->node, &head);
150 }
151
152 perf_evlist__splice_list_tail(evlist, &head, nr_attrs);
153
154 return 0;
155
156out_delete_partial_list:
157 list_for_each_entry_safe(evsel, n, &head, node)
158 perf_evsel__delete(evsel);
159 return -1;
160}
161
Arnaldo Carvalho de Meloa8c9ae12011-11-05 08:41:51 -0200162static int trace_event__id(const char *evname)
163{
164 char *filename, *colon;
165 int err = -1, fd;
166
167 if (asprintf(&filename, "%s/%s/id", tracing_events_path, evname) < 0)
168 return -1;
169
170 colon = strrchr(filename, ':');
171 if (colon != NULL)
172 *colon = '/';
173
174 fd = open(filename, O_RDONLY);
175 if (fd >= 0) {
176 char id[16];
177 if (read(fd, id, sizeof(id)) > 0)
178 err = atoi(id);
179 close(fd);
180 }
181
182 free(filename);
183 return err;
184}
185
186int perf_evlist__add_tracepoints(struct perf_evlist *evlist,
187 const char *tracepoints[],
188 size_t nr_tracepoints)
189{
190 int err;
191 size_t i;
192 struct perf_event_attr *attrs = zalloc(nr_tracepoints * sizeof(*attrs));
193
194 if (attrs == NULL)
195 return -1;
196
197 for (i = 0; i < nr_tracepoints; i++) {
198 err = trace_event__id(tracepoints[i]);
199
200 if (err < 0)
201 goto out_free_attrs;
202
203 attrs[i].type = PERF_TYPE_TRACEPOINT;
204 attrs[i].config = err;
205 attrs[i].sample_type = (PERF_SAMPLE_RAW | PERF_SAMPLE_TIME |
206 PERF_SAMPLE_CPU);
207 attrs[i].sample_period = 1;
208 }
209
210 err = perf_evlist__add_attrs(evlist, attrs, nr_tracepoints);
211out_free_attrs:
212 free(attrs);
213 return err;
214}
215
Arnaldo Carvalho de Meloee29be62011-11-28 17:57:40 -0200216static struct perf_evsel *
217 perf_evlist__find_tracepoint_by_id(struct perf_evlist *evlist, int id)
218{
219 struct perf_evsel *evsel;
220
221 list_for_each_entry(evsel, &evlist->entries, node) {
222 if (evsel->attr.type == PERF_TYPE_TRACEPOINT &&
223 (int)evsel->attr.config == id)
224 return evsel;
225 }
226
227 return NULL;
228}
229
230int perf_evlist__set_tracepoints_handlers(struct perf_evlist *evlist,
231 const struct perf_evsel_str_handler *assocs,
232 size_t nr_assocs)
233{
234 struct perf_evsel *evsel;
235 int err;
236 size_t i;
237
238 for (i = 0; i < nr_assocs; i++) {
239 err = trace_event__id(assocs[i].name);
240 if (err < 0)
241 goto out;
242
243 evsel = perf_evlist__find_tracepoint_by_id(evlist, err);
244 if (evsel == NULL)
245 continue;
246
247 err = -EEXIST;
248 if (evsel->handler.func != NULL)
249 goto out;
250 evsel->handler.func = assocs[i].handler;
251 }
252
253 err = 0;
254out:
255 return err;
256}
257
Arnaldo Carvalho de Melo4152ab32011-07-25 11:06:19 -0300258void perf_evlist__disable(struct perf_evlist *evlist)
259{
260 int cpu, thread;
261 struct perf_evsel *pos;
262
263 for (cpu = 0; cpu < evlist->cpus->nr; cpu++) {
264 list_for_each_entry(pos, &evlist->entries, node) {
265 for (thread = 0; thread < evlist->threads->nr; thread++)
266 ioctl(FD(pos, cpu, thread), PERF_EVENT_IOC_DISABLE);
267 }
268 }
269}
270
David Ahern764e16a32011-08-25 10:17:55 -0600271void perf_evlist__enable(struct perf_evlist *evlist)
272{
273 int cpu, thread;
274 struct perf_evsel *pos;
275
276 for (cpu = 0; cpu < evlist->cpus->nr; cpu++) {
277 list_for_each_entry(pos, &evlist->entries, node) {
278 for (thread = 0; thread < evlist->threads->nr; thread++)
279 ioctl(FD(pos, cpu, thread), PERF_EVENT_IOC_ENABLE);
280 }
281 }
282}
283
Arnaldo Carvalho de Melo806fb632011-11-29 08:05:52 -0200284static int perf_evlist__alloc_pollfd(struct perf_evlist *evlist)
Arnaldo Carvalho de Melo5c581042011-01-11 22:30:02 -0200285{
Arnaldo Carvalho de Melo7e2ed092011-01-30 11:59:43 -0200286 int nfds = evlist->cpus->nr * evlist->threads->nr * evlist->nr_entries;
Arnaldo Carvalho de Melo5c581042011-01-11 22:30:02 -0200287 evlist->pollfd = malloc(sizeof(struct pollfd) * nfds);
288 return evlist->pollfd != NULL ? 0 : -ENOMEM;
289}
Arnaldo Carvalho de Melo70082dd2011-01-12 17:03:24 -0200290
291void perf_evlist__add_pollfd(struct perf_evlist *evlist, int fd)
292{
293 fcntl(fd, F_SETFL, O_NONBLOCK);
294 evlist->pollfd[evlist->nr_fds].fd = fd;
295 evlist->pollfd[evlist->nr_fds].events = POLLIN;
296 evlist->nr_fds++;
297}
Arnaldo Carvalho de Melo70db7532011-01-12 22:39:13 -0200298
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -0300299static void perf_evlist__id_hash(struct perf_evlist *evlist,
300 struct perf_evsel *evsel,
301 int cpu, int thread, u64 id)
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200302{
Arnaldo Carvalho de Melo3d3b5e92011-03-04 22:29:39 -0300303 int hash;
304 struct perf_sample_id *sid = SID(evsel, cpu, thread);
305
306 sid->id = id;
307 sid->evsel = evsel;
308 hash = hash_64(sid->id, PERF_EVLIST__HLIST_BITS);
309 hlist_add_head(&sid->node, &evlist->heads[hash]);
310}
311
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -0300312void perf_evlist__id_add(struct perf_evlist *evlist, struct perf_evsel *evsel,
313 int cpu, int thread, u64 id)
314{
315 perf_evlist__id_hash(evlist, evsel, cpu, thread, id);
316 evsel->id[evsel->ids++] = id;
317}
318
319static int perf_evlist__id_add_fd(struct perf_evlist *evlist,
320 struct perf_evsel *evsel,
321 int cpu, int thread, int fd)
Arnaldo Carvalho de Melo3d3b5e92011-03-04 22:29:39 -0300322{
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200323 u64 read_data[4] = { 0, };
Arnaldo Carvalho de Melo3d3b5e92011-03-04 22:29:39 -0300324 int id_idx = 1; /* The first entry is the counter value */
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200325
326 if (!(evsel->attr.read_format & PERF_FORMAT_ID) ||
327 read(fd, &read_data, sizeof(read_data)) == -1)
328 return -1;
329
330 if (evsel->attr.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
331 ++id_idx;
332 if (evsel->attr.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
333 ++id_idx;
334
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -0300335 perf_evlist__id_add(evlist, evsel, cpu, thread, read_data[id_idx]);
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200336 return 0;
337}
338
Arnaldo Carvalho de Melo70db7532011-01-12 22:39:13 -0200339struct perf_evsel *perf_evlist__id2evsel(struct perf_evlist *evlist, u64 id)
340{
341 struct hlist_head *head;
342 struct hlist_node *pos;
343 struct perf_sample_id *sid;
344 int hash;
345
346 if (evlist->nr_entries == 1)
347 return list_entry(evlist->entries.next, struct perf_evsel, node);
348
349 hash = hash_64(id, PERF_EVLIST__HLIST_BITS);
350 head = &evlist->heads[hash];
351
352 hlist_for_each_entry(sid, pos, head, node)
353 if (sid->id == id)
354 return sid->evsel;
Namhyung Kim30e68bc2012-02-20 10:47:26 +0900355
356 if (!perf_evlist__sample_id_all(evlist))
357 return list_entry(evlist->entries.next, struct perf_evsel, node);
358
Arnaldo Carvalho de Melo70db7532011-01-12 22:39:13 -0200359 return NULL;
360}
Arnaldo Carvalho de Melo04391de2011-01-15 10:40:59 -0200361
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -0300362union perf_event *perf_evlist__mmap_read(struct perf_evlist *evlist, int idx)
Arnaldo Carvalho de Melo04391de2011-01-15 10:40:59 -0200363{
364 /* XXX Move this to perf.c, making it generally available */
365 unsigned int page_size = sysconf(_SC_PAGE_SIZE);
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -0300366 struct perf_mmap *md = &evlist->mmap[idx];
Arnaldo Carvalho de Melo04391de2011-01-15 10:40:59 -0200367 unsigned int head = perf_mmap__read_head(md);
368 unsigned int old = md->prev;
369 unsigned char *data = md->base + page_size;
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200370 union perf_event *event = NULL;
Arnaldo Carvalho de Melo04391de2011-01-15 10:40:59 -0200371
Arnaldo Carvalho de Melo7bb41152011-01-29 09:08:13 -0200372 if (evlist->overwrite) {
Arnaldo Carvalho de Melo04391de2011-01-15 10:40:59 -0200373 /*
Arnaldo Carvalho de Melo7bb41152011-01-29 09:08:13 -0200374 * If we're further behind than half the buffer, there's a chance
375 * the writer will bite our tail and mess up the samples under us.
376 *
377 * If we somehow ended up ahead of the head, we got messed up.
378 *
379 * In either case, truncate and restart at head.
Arnaldo Carvalho de Melo04391de2011-01-15 10:40:59 -0200380 */
Arnaldo Carvalho de Melo7bb41152011-01-29 09:08:13 -0200381 int diff = head - old;
382 if (diff > md->mask / 2 || diff < 0) {
383 fprintf(stderr, "WARNING: failed to keep up with mmap data.\n");
384
385 /*
386 * head points to a known good entry, start there.
387 */
388 old = head;
389 }
Arnaldo Carvalho de Melo04391de2011-01-15 10:40:59 -0200390 }
391
392 if (old != head) {
393 size_t size;
394
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200395 event = (union perf_event *)&data[old & md->mask];
Arnaldo Carvalho de Melo04391de2011-01-15 10:40:59 -0200396 size = event->header.size;
397
398 /*
399 * Event straddles the mmap boundary -- header should always
400 * be inside due to u64 alignment of output.
401 */
402 if ((old & md->mask) + size != ((old + size) & md->mask)) {
403 unsigned int offset = old;
404 unsigned int len = min(sizeof(*event), size), cpy;
405 void *dst = &evlist->event_copy;
406
407 do {
408 cpy = min(md->mask + 1 - (offset & md->mask), len);
409 memcpy(dst, &data[offset & md->mask], cpy);
410 offset += cpy;
411 dst += cpy;
412 len -= cpy;
413 } while (len);
414
415 event = &evlist->event_copy;
416 }
417
418 old += size;
419 }
420
421 md->prev = old;
Arnaldo Carvalho de Melo7bb41152011-01-29 09:08:13 -0200422
423 if (!evlist->overwrite)
424 perf_mmap__write_tail(md, old);
425
Arnaldo Carvalho de Melo04391de2011-01-15 10:40:59 -0200426 return event;
427}
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200428
Arnaldo Carvalho de Melo7e2ed092011-01-30 11:59:43 -0200429void perf_evlist__munmap(struct perf_evlist *evlist)
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200430{
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -0300431 int i;
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200432
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -0300433 for (i = 0; i < evlist->nr_mmaps; i++) {
434 if (evlist->mmap[i].base != NULL) {
435 munmap(evlist->mmap[i].base, evlist->mmap_len);
436 evlist->mmap[i].base = NULL;
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200437 }
438 }
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -0300439
440 free(evlist->mmap);
441 evlist->mmap = NULL;
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200442}
443
Arnaldo Carvalho de Melo806fb632011-11-29 08:05:52 -0200444static int perf_evlist__alloc_mmap(struct perf_evlist *evlist)
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200445{
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -0300446 evlist->nr_mmaps = evlist->cpus->nr;
447 if (evlist->cpus->map[0] == -1)
448 evlist->nr_mmaps = evlist->threads->nr;
449 evlist->mmap = zalloc(evlist->nr_mmaps * sizeof(struct perf_mmap));
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200450 return evlist->mmap != NULL ? 0 : -ENOMEM;
451}
452
Arnaldo Carvalho de Melobccdaba2011-06-02 10:39:43 -0300453static int __perf_evlist__mmap(struct perf_evlist *evlist,
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -0300454 int idx, int prot, int mask, int fd)
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200455{
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -0300456 evlist->mmap[idx].prev = 0;
457 evlist->mmap[idx].mask = mask;
458 evlist->mmap[idx].base = mmap(NULL, evlist->mmap_len, prot,
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200459 MAP_SHARED, fd, 0);
Nelson Elhage301b1952011-12-19 08:39:30 -0500460 if (evlist->mmap[idx].base == MAP_FAILED) {
461 evlist->mmap[idx].base = NULL;
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200462 return -1;
Nelson Elhage301b1952011-12-19 08:39:30 -0500463 }
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200464
465 perf_evlist__add_pollfd(evlist, fd);
466 return 0;
467}
468
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -0300469static int perf_evlist__mmap_per_cpu(struct perf_evlist *evlist, int prot, int mask)
470{
471 struct perf_evsel *evsel;
472 int cpu, thread;
473
474 for (cpu = 0; cpu < evlist->cpus->nr; cpu++) {
475 int output = -1;
476
477 for (thread = 0; thread < evlist->threads->nr; thread++) {
478 list_for_each_entry(evsel, &evlist->entries, node) {
479 int fd = FD(evsel, cpu, thread);
480
481 if (output == -1) {
482 output = fd;
Arnaldo Carvalho de Melobccdaba2011-06-02 10:39:43 -0300483 if (__perf_evlist__mmap(evlist, cpu,
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -0300484 prot, mask, output) < 0)
485 goto out_unmap;
486 } else {
487 if (ioctl(fd, PERF_EVENT_IOC_SET_OUTPUT, output) != 0)
488 goto out_unmap;
489 }
490
491 if ((evsel->attr.read_format & PERF_FORMAT_ID) &&
492 perf_evlist__id_add_fd(evlist, evsel, cpu, thread, fd) < 0)
493 goto out_unmap;
494 }
495 }
496 }
497
498 return 0;
499
500out_unmap:
501 for (cpu = 0; cpu < evlist->cpus->nr; cpu++) {
502 if (evlist->mmap[cpu].base != NULL) {
503 munmap(evlist->mmap[cpu].base, evlist->mmap_len);
504 evlist->mmap[cpu].base = NULL;
505 }
506 }
507 return -1;
508}
509
510static int perf_evlist__mmap_per_thread(struct perf_evlist *evlist, int prot, int mask)
511{
512 struct perf_evsel *evsel;
513 int thread;
514
515 for (thread = 0; thread < evlist->threads->nr; thread++) {
516 int output = -1;
517
518 list_for_each_entry(evsel, &evlist->entries, node) {
519 int fd = FD(evsel, 0, thread);
520
521 if (output == -1) {
522 output = fd;
Arnaldo Carvalho de Melobccdaba2011-06-02 10:39:43 -0300523 if (__perf_evlist__mmap(evlist, thread,
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -0300524 prot, mask, output) < 0)
525 goto out_unmap;
526 } else {
527 if (ioctl(fd, PERF_EVENT_IOC_SET_OUTPUT, output) != 0)
528 goto out_unmap;
529 }
530
531 if ((evsel->attr.read_format & PERF_FORMAT_ID) &&
532 perf_evlist__id_add_fd(evlist, evsel, 0, thread, fd) < 0)
533 goto out_unmap;
534 }
535 }
536
537 return 0;
538
539out_unmap:
540 for (thread = 0; thread < evlist->threads->nr; thread++) {
541 if (evlist->mmap[thread].base != NULL) {
542 munmap(evlist->mmap[thread].base, evlist->mmap_len);
543 evlist->mmap[thread].base = NULL;
544 }
545 }
546 return -1;
547}
548
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200549/** perf_evlist__mmap - Create per cpu maps to receive events
550 *
551 * @evlist - list of events
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200552 * @pages - map length in pages
553 * @overwrite - overwrite older events?
554 *
555 * If overwrite is false the user needs to signal event consuption using:
556 *
557 * struct perf_mmap *m = &evlist->mmap[cpu];
558 * unsigned int head = perf_mmap__read_head(m);
559 *
560 * perf_mmap__write_tail(m, head)
Arnaldo Carvalho de Melo7e2ed092011-01-30 11:59:43 -0200561 *
562 * Using perf_evlist__read_on_cpu does this automatically.
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200563 */
Arnaldo Carvalho de Melo50a682c2011-11-09 09:10:47 -0200564int perf_evlist__mmap(struct perf_evlist *evlist, unsigned int pages,
565 bool overwrite)
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200566{
567 unsigned int page_size = sysconf(_SC_PAGE_SIZE);
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -0300568 struct perf_evsel *evsel;
Arnaldo Carvalho de Melo7e2ed092011-01-30 11:59:43 -0200569 const struct cpu_map *cpus = evlist->cpus;
570 const struct thread_map *threads = evlist->threads;
Arnaldo Carvalho de Melo50a682c2011-11-09 09:10:47 -0200571 int prot = PROT_READ | (overwrite ? 0 : PROT_WRITE), mask;
572
573 /* 512 kiB: default amount of unprivileged mlocked memory */
574 if (pages == UINT_MAX)
575 pages = (512 * 1024) / page_size;
Nelson Elhage41d0d932011-12-19 08:39:32 -0500576 else if (!is_power_of_2(pages))
577 return -EINVAL;
Arnaldo Carvalho de Melo50a682c2011-11-09 09:10:47 -0200578
579 mask = pages * page_size - 1;
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200580
Arnaldo Carvalho de Melo7e2ed092011-01-30 11:59:43 -0200581 if (evlist->mmap == NULL && perf_evlist__alloc_mmap(evlist) < 0)
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200582 return -ENOMEM;
583
Arnaldo Carvalho de Melo7e2ed092011-01-30 11:59:43 -0200584 if (evlist->pollfd == NULL && perf_evlist__alloc_pollfd(evlist) < 0)
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200585 return -ENOMEM;
586
587 evlist->overwrite = overwrite;
588 evlist->mmap_len = (pages + 1) * page_size;
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200589
590 list_for_each_entry(evsel, &evlist->entries, node) {
591 if ((evsel->attr.read_format & PERF_FORMAT_ID) &&
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -0300592 evsel->sample_id == NULL &&
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200593 perf_evsel__alloc_id(evsel, cpus->nr, threads->nr) < 0)
594 return -ENOMEM;
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200595 }
596
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -0300597 if (evlist->cpus->map[0] == -1)
598 return perf_evlist__mmap_per_thread(evlist, prot, mask);
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200599
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -0300600 return perf_evlist__mmap_per_cpu(evlist, prot, mask);
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200601}
Arnaldo Carvalho de Melo7e2ed092011-01-30 11:59:43 -0200602
Namhyung Kimb809ac12012-04-26 14:15:19 +0900603int perf_evlist__create_maps(struct perf_evlist *evlist,
604 struct perf_target *target)
Arnaldo Carvalho de Melo7e2ed092011-01-30 11:59:43 -0200605{
Namhyung Kimb809ac12012-04-26 14:15:19 +0900606 evlist->threads = thread_map__new_str(target->pid, target->tid,
607 target->uid);
Arnaldo Carvalho de Melo7e2ed092011-01-30 11:59:43 -0200608
609 if (evlist->threads == NULL)
610 return -1;
611
Namhyung Kim55261f42012-05-07 14:08:59 +0900612 if (target->uid != UINT_MAX || target->tid)
613 evlist->cpus = cpu_map__dummy_new();
614 else if (!target->system_wide && target->cpu_list == NULL)
Arnaldo Carvalho de Melo7e2ed092011-01-30 11:59:43 -0200615 evlist->cpus = cpu_map__dummy_new();
616 else
Namhyung Kimb809ac12012-04-26 14:15:19 +0900617 evlist->cpus = cpu_map__new(target->cpu_list);
Arnaldo Carvalho de Melo7e2ed092011-01-30 11:59:43 -0200618
619 if (evlist->cpus == NULL)
620 goto out_delete_threads;
621
622 return 0;
623
624out_delete_threads:
625 thread_map__delete(evlist->threads);
626 return -1;
627}
628
629void perf_evlist__delete_maps(struct perf_evlist *evlist)
630{
631 cpu_map__delete(evlist->cpus);
632 thread_map__delete(evlist->threads);
633 evlist->cpus = NULL;
634 evlist->threads = NULL;
635}
Frederic Weisbecker0a102472011-02-26 04:51:54 +0100636
637int perf_evlist__set_filters(struct perf_evlist *evlist)
638{
639 const struct thread_map *threads = evlist->threads;
640 const struct cpu_map *cpus = evlist->cpus;
641 struct perf_evsel *evsel;
642 char *filter;
643 int thread;
644 int cpu;
645 int err;
646 int fd;
647
648 list_for_each_entry(evsel, &evlist->entries, node) {
649 filter = evsel->filter;
650 if (!filter)
651 continue;
652 for (cpu = 0; cpu < cpus->nr; cpu++) {
653 for (thread = 0; thread < threads->nr; thread++) {
654 fd = FD(evsel, cpu, thread);
655 err = ioctl(fd, PERF_EVENT_IOC_SET_FILTER, filter);
656 if (err)
657 return err;
658 }
659 }
660 }
661
662 return 0;
663}
Frederic Weisbecker74429962011-05-21 17:49:00 +0200664
Arnaldo Carvalho de Meloc2a70652011-06-02 11:04:54 -0300665bool perf_evlist__valid_sample_type(const struct perf_evlist *evlist)
Frederic Weisbecker74429962011-05-21 17:49:00 +0200666{
Arnaldo Carvalho de Meloc2a70652011-06-02 11:04:54 -0300667 struct perf_evsel *pos, *first;
Frederic Weisbecker74429962011-05-21 17:49:00 +0200668
Arnaldo Carvalho de Meloc2a70652011-06-02 11:04:54 -0300669 pos = first = list_entry(evlist->entries.next, struct perf_evsel, node);
670
671 list_for_each_entry_continue(pos, &evlist->entries, node) {
672 if (first->attr.sample_type != pos->attr.sample_type)
673 return false;
Frederic Weisbecker74429962011-05-21 17:49:00 +0200674 }
675
Arnaldo Carvalho de Meloc2a70652011-06-02 11:04:54 -0300676 return true;
677}
678
679u64 perf_evlist__sample_type(const struct perf_evlist *evlist)
680{
681 struct perf_evsel *first;
682
683 first = list_entry(evlist->entries.next, struct perf_evsel, node);
684 return first->attr.sample_type;
685}
686
Arnaldo Carvalho de Melo81e36bff2011-11-11 22:28:50 -0200687u16 perf_evlist__id_hdr_size(const struct perf_evlist *evlist)
688{
689 struct perf_evsel *first;
690 struct perf_sample *data;
691 u64 sample_type;
692 u16 size = 0;
693
694 first = list_entry(evlist->entries.next, struct perf_evsel, node);
695
696 if (!first->attr.sample_id_all)
697 goto out;
698
699 sample_type = first->attr.sample_type;
700
701 if (sample_type & PERF_SAMPLE_TID)
702 size += sizeof(data->tid) * 2;
703
704 if (sample_type & PERF_SAMPLE_TIME)
705 size += sizeof(data->time);
706
707 if (sample_type & PERF_SAMPLE_ID)
708 size += sizeof(data->id);
709
710 if (sample_type & PERF_SAMPLE_STREAM_ID)
711 size += sizeof(data->stream_id);
712
713 if (sample_type & PERF_SAMPLE_CPU)
714 size += sizeof(data->cpu) * 2;
715out:
716 return size;
717}
718
Arnaldo Carvalho de Meloc2a70652011-06-02 11:04:54 -0300719bool perf_evlist__valid_sample_id_all(const struct perf_evlist *evlist)
720{
721 struct perf_evsel *pos, *first;
722
723 pos = first = list_entry(evlist->entries.next, struct perf_evsel, node);
724
725 list_for_each_entry_continue(pos, &evlist->entries, node) {
726 if (first->attr.sample_id_all != pos->attr.sample_id_all)
727 return false;
728 }
729
730 return true;
Frederic Weisbecker74429962011-05-21 17:49:00 +0200731}
732
733bool perf_evlist__sample_id_all(const struct perf_evlist *evlist)
734{
Arnaldo Carvalho de Meloc2a70652011-06-02 11:04:54 -0300735 struct perf_evsel *first;
Frederic Weisbecker74429962011-05-21 17:49:00 +0200736
Arnaldo Carvalho de Meloc2a70652011-06-02 11:04:54 -0300737 first = list_entry(evlist->entries.next, struct perf_evsel, node);
738 return first->attr.sample_id_all;
Frederic Weisbecker74429962011-05-21 17:49:00 +0200739}
Arnaldo Carvalho de Melo81cce8d2011-10-05 19:11:32 -0300740
741void perf_evlist__set_selected(struct perf_evlist *evlist,
742 struct perf_evsel *evsel)
743{
744 evlist->selected = evsel;
745}
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -0200746
747int perf_evlist__open(struct perf_evlist *evlist, bool group)
748{
749 struct perf_evsel *evsel, *first;
750 int err, ncpus, nthreads;
751
752 first = list_entry(evlist->entries.next, struct perf_evsel, node);
753
754 list_for_each_entry(evsel, &evlist->entries, node) {
755 struct xyarray *group_fd = NULL;
756
757 if (group && evsel != first)
758 group_fd = first->fd;
759
760 err = perf_evsel__open(evsel, evlist->cpus, evlist->threads,
761 group, group_fd);
762 if (err < 0)
763 goto out_err;
764 }
765
766 return 0;
767out_err:
768 ncpus = evlist->cpus ? evlist->cpus->nr : 1;
769 nthreads = evlist->threads ? evlist->threads->nr : 1;
770
771 list_for_each_entry_reverse(evsel, &evlist->entries, node)
772 perf_evsel__close(evsel, ncpus, nthreads);
773
Namhyung Kim41c21a62012-02-23 12:13:36 +0900774 errno = -err;
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -0200775 return err;
776}
Arnaldo Carvalho de Melo35b9d882011-11-09 08:47:15 -0200777
778int perf_evlist__prepare_workload(struct perf_evlist *evlist,
779 struct perf_record_opts *opts,
780 const char *argv[])
781{
782 int child_ready_pipe[2], go_pipe[2];
783 char bf;
784
785 if (pipe(child_ready_pipe) < 0) {
786 perror("failed to create 'ready' pipe");
787 return -1;
788 }
789
790 if (pipe(go_pipe) < 0) {
791 perror("failed to create 'go' pipe");
792 goto out_close_ready_pipe;
793 }
794
795 evlist->workload.pid = fork();
796 if (evlist->workload.pid < 0) {
797 perror("failed to fork");
798 goto out_close_pipes;
799 }
800
801 if (!evlist->workload.pid) {
802 if (opts->pipe_output)
803 dup2(2, 1);
804
805 close(child_ready_pipe[0]);
806 close(go_pipe[1]);
807 fcntl(go_pipe[0], F_SETFD, FD_CLOEXEC);
808
809 /*
810 * Do a dummy execvp to get the PLT entry resolved,
811 * so we avoid the resolver overhead on the real
812 * execvp call.
813 */
814 execvp("", (char **)argv);
815
816 /*
817 * Tell the parent we're ready to go
818 */
819 close(child_ready_pipe[1]);
820
821 /*
822 * Wait until the parent tells us to go.
823 */
824 if (read(go_pipe[0], &bf, 1) == -1)
825 perror("unable to read pipe");
826
827 execvp(argv[0], (char **)argv);
828
829 perror(argv[0]);
830 kill(getppid(), SIGUSR1);
831 exit(-1);
832 }
833
Namhyung Kimbea03402012-04-26 14:15:15 +0900834 if (!opts->target.system_wide && !opts->target.tid && !opts->target.pid)
Arnaldo Carvalho de Melo35b9d882011-11-09 08:47:15 -0200835 evlist->threads->map[0] = evlist->workload.pid;
836
837 close(child_ready_pipe[1]);
838 close(go_pipe[0]);
839 /*
840 * wait for child to settle
841 */
842 if (read(child_ready_pipe[0], &bf, 1) == -1) {
843 perror("unable to read pipe");
844 goto out_close_pipes;
845 }
846
847 evlist->workload.cork_fd = go_pipe[1];
848 close(child_ready_pipe[0]);
849 return 0;
850
851out_close_pipes:
852 close(go_pipe[0]);
853 close(go_pipe[1]);
854out_close_ready_pipe:
855 close(child_ready_pipe[0]);
856 close(child_ready_pipe[1]);
857 return -1;
858}
859
860int perf_evlist__start_workload(struct perf_evlist *evlist)
861{
862 if (evlist->workload.cork_fd > 0) {
863 /*
864 * Remove the cork, let it rip!
865 */
866 return close(evlist->workload.cork_fd);
867 }
868
869 return 0;
870}