blob: 1986d8051bd16a9f133159545fb7083df3511d76 [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"
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -020014#include "evlist.h"
15#include "evsel.h"
Arnaldo Carvalho de Melo35b9d882011-11-09 08:47:15 -020016#include <unistd.h>
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -020017
Arnaldo Carvalho de Melo50d08e42011-11-04 09:10:59 -020018#include "parse-events.h"
19
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -020020#include <sys/mman.h>
21
Arnaldo Carvalho de Melo70db7532011-01-12 22:39:13 -020022#include <linux/bitops.h>
23#include <linux/hash.h>
24
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -020025#define FD(e, x, y) (*(int *)xyarray__entry(e->fd, x, y))
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -030026#define SID(e, x, y) xyarray__entry(e->sample_id, x, y)
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -020027
Arnaldo Carvalho de Melo7e2ed092011-01-30 11:59:43 -020028void perf_evlist__init(struct perf_evlist *evlist, struct cpu_map *cpus,
29 struct thread_map *threads)
Arnaldo Carvalho de Meloef1d1af2011-01-18 21:41:45 -020030{
31 int i;
32
33 for (i = 0; i < PERF_EVLIST__HLIST_SIZE; ++i)
34 INIT_HLIST_HEAD(&evlist->heads[i]);
35 INIT_LIST_HEAD(&evlist->entries);
Arnaldo Carvalho de Melo7e2ed092011-01-30 11:59:43 -020036 perf_evlist__set_maps(evlist, cpus, threads);
Arnaldo Carvalho de Melo35b9d882011-11-09 08:47:15 -020037 evlist->workload.pid = -1;
Arnaldo Carvalho de Meloef1d1af2011-01-18 21:41:45 -020038}
39
Arnaldo Carvalho de Melo7e2ed092011-01-30 11:59:43 -020040struct perf_evlist *perf_evlist__new(struct cpu_map *cpus,
41 struct thread_map *threads)
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -020042{
43 struct perf_evlist *evlist = zalloc(sizeof(*evlist));
44
Arnaldo Carvalho de Meloef1d1af2011-01-18 21:41:45 -020045 if (evlist != NULL)
Arnaldo Carvalho de Melo7e2ed092011-01-30 11:59:43 -020046 perf_evlist__init(evlist, cpus, threads);
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -020047
48 return evlist;
49}
50
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -020051void perf_evlist__config_attrs(struct perf_evlist *evlist,
52 struct perf_record_opts *opts)
53{
Namhyung Kim5090c6a2012-03-16 17:42:20 +090054 struct perf_evsel *evsel, *first;
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -020055
56 if (evlist->cpus->map[0] < 0)
57 opts->no_inherit = true;
58
Namhyung Kim5090c6a2012-03-16 17:42:20 +090059 first = list_entry(evlist->entries.next, struct perf_evsel, node);
60
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -020061 list_for_each_entry(evsel, &evlist->entries, node) {
Namhyung Kim5090c6a2012-03-16 17:42:20 +090062 perf_evsel__config(evsel, opts, first);
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -020063
64 if (evlist->nr_entries > 1)
65 evsel->attr.sample_type |= PERF_SAMPLE_ID;
66 }
67}
68
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -020069static void perf_evlist__purge(struct perf_evlist *evlist)
70{
71 struct perf_evsel *pos, *n;
72
73 list_for_each_entry_safe(pos, n, &evlist->entries, node) {
74 list_del_init(&pos->node);
75 perf_evsel__delete(pos);
76 }
77
78 evlist->nr_entries = 0;
79}
80
Arnaldo Carvalho de Meloef1d1af2011-01-18 21:41:45 -020081void perf_evlist__exit(struct perf_evlist *evlist)
82{
83 free(evlist->mmap);
84 free(evlist->pollfd);
85 evlist->mmap = NULL;
86 evlist->pollfd = NULL;
87}
88
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -020089void perf_evlist__delete(struct perf_evlist *evlist)
90{
91 perf_evlist__purge(evlist);
Arnaldo Carvalho de Meloef1d1af2011-01-18 21:41:45 -020092 perf_evlist__exit(evlist);
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -020093 free(evlist);
94}
95
96void perf_evlist__add(struct perf_evlist *evlist, struct perf_evsel *entry)
97{
98 list_add_tail(&entry->node, &evlist->entries);
99 ++evlist->nr_entries;
100}
101
Jiri Olsa0529bc12012-01-27 15:34:20 +0100102void perf_evlist__splice_list_tail(struct perf_evlist *evlist,
103 struct list_head *list,
104 int nr_entries)
Arnaldo Carvalho de Melo50d08e42011-11-04 09:10:59 -0200105{
106 list_splice_tail(list, &evlist->entries);
107 evlist->nr_entries += nr_entries;
108}
109
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -0200110int perf_evlist__add_default(struct perf_evlist *evlist)
111{
112 struct perf_event_attr attr = {
113 .type = PERF_TYPE_HARDWARE,
114 .config = PERF_COUNT_HW_CPU_CYCLES,
115 };
Joerg Roedel1aed2672012-01-04 17:54:20 +0100116 struct perf_evsel *evsel;
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -0200117
Joerg Roedel1aed2672012-01-04 17:54:20 +0100118 event_attr_init(&attr);
119
120 evsel = perf_evsel__new(&attr, 0);
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -0200121 if (evsel == NULL)
Stephane Eraniancc2d86b2011-06-07 18:19:36 +0200122 goto error;
123
124 /* use strdup() because free(evsel) assumes name is allocated */
125 evsel->name = strdup("cycles");
126 if (!evsel->name)
127 goto error_free;
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -0200128
129 perf_evlist__add(evlist, evsel);
130 return 0;
Stephane Eraniancc2d86b2011-06-07 18:19:36 +0200131error_free:
132 perf_evsel__delete(evsel);
133error:
134 return -ENOMEM;
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -0200135}
Arnaldo Carvalho de Melo5c581042011-01-11 22:30:02 -0200136
Arnaldo Carvalho de Melo50d08e42011-11-04 09:10:59 -0200137int perf_evlist__add_attrs(struct perf_evlist *evlist,
138 struct perf_event_attr *attrs, size_t nr_attrs)
139{
140 struct perf_evsel *evsel, *n;
141 LIST_HEAD(head);
142 size_t i;
143
144 for (i = 0; i < nr_attrs; i++) {
145 evsel = perf_evsel__new(attrs + i, evlist->nr_entries + i);
146 if (evsel == NULL)
147 goto out_delete_partial_list;
148 list_add_tail(&evsel->node, &head);
149 }
150
151 perf_evlist__splice_list_tail(evlist, &head, nr_attrs);
152
153 return 0;
154
155out_delete_partial_list:
156 list_for_each_entry_safe(evsel, n, &head, node)
157 perf_evsel__delete(evsel);
158 return -1;
159}
160
Arnaldo Carvalho de Meloa8c9ae12011-11-05 08:41:51 -0200161static int trace_event__id(const char *evname)
162{
163 char *filename, *colon;
164 int err = -1, fd;
165
166 if (asprintf(&filename, "%s/%s/id", tracing_events_path, evname) < 0)
167 return -1;
168
169 colon = strrchr(filename, ':');
170 if (colon != NULL)
171 *colon = '/';
172
173 fd = open(filename, O_RDONLY);
174 if (fd >= 0) {
175 char id[16];
176 if (read(fd, id, sizeof(id)) > 0)
177 err = atoi(id);
178 close(fd);
179 }
180
181 free(filename);
182 return err;
183}
184
185int perf_evlist__add_tracepoints(struct perf_evlist *evlist,
186 const char *tracepoints[],
187 size_t nr_tracepoints)
188{
189 int err;
190 size_t i;
191 struct perf_event_attr *attrs = zalloc(nr_tracepoints * sizeof(*attrs));
192
193 if (attrs == NULL)
194 return -1;
195
196 for (i = 0; i < nr_tracepoints; i++) {
197 err = trace_event__id(tracepoints[i]);
198
199 if (err < 0)
200 goto out_free_attrs;
201
202 attrs[i].type = PERF_TYPE_TRACEPOINT;
203 attrs[i].config = err;
204 attrs[i].sample_type = (PERF_SAMPLE_RAW | PERF_SAMPLE_TIME |
205 PERF_SAMPLE_CPU);
206 attrs[i].sample_period = 1;
207 }
208
209 err = perf_evlist__add_attrs(evlist, attrs, nr_tracepoints);
210out_free_attrs:
211 free(attrs);
212 return err;
213}
214
Arnaldo Carvalho de Meloee29be62011-11-28 17:57:40 -0200215static struct perf_evsel *
216 perf_evlist__find_tracepoint_by_id(struct perf_evlist *evlist, int id)
217{
218 struct perf_evsel *evsel;
219
220 list_for_each_entry(evsel, &evlist->entries, node) {
221 if (evsel->attr.type == PERF_TYPE_TRACEPOINT &&
222 (int)evsel->attr.config == id)
223 return evsel;
224 }
225
226 return NULL;
227}
228
229int perf_evlist__set_tracepoints_handlers(struct perf_evlist *evlist,
230 const struct perf_evsel_str_handler *assocs,
231 size_t nr_assocs)
232{
233 struct perf_evsel *evsel;
234 int err;
235 size_t i;
236
237 for (i = 0; i < nr_assocs; i++) {
238 err = trace_event__id(assocs[i].name);
239 if (err < 0)
240 goto out;
241
242 evsel = perf_evlist__find_tracepoint_by_id(evlist, err);
243 if (evsel == NULL)
244 continue;
245
246 err = -EEXIST;
247 if (evsel->handler.func != NULL)
248 goto out;
249 evsel->handler.func = assocs[i].handler;
250 }
251
252 err = 0;
253out:
254 return err;
255}
256
Arnaldo Carvalho de Melo4152ab32011-07-25 11:06:19 -0300257void perf_evlist__disable(struct perf_evlist *evlist)
258{
259 int cpu, thread;
260 struct perf_evsel *pos;
261
262 for (cpu = 0; cpu < evlist->cpus->nr; cpu++) {
263 list_for_each_entry(pos, &evlist->entries, node) {
264 for (thread = 0; thread < evlist->threads->nr; thread++)
265 ioctl(FD(pos, cpu, thread), PERF_EVENT_IOC_DISABLE);
266 }
267 }
268}
269
David Ahern764e16a32011-08-25 10:17:55 -0600270void perf_evlist__enable(struct perf_evlist *evlist)
271{
272 int cpu, thread;
273 struct perf_evsel *pos;
274
275 for (cpu = 0; cpu < evlist->cpus->nr; cpu++) {
276 list_for_each_entry(pos, &evlist->entries, node) {
277 for (thread = 0; thread < evlist->threads->nr; thread++)
278 ioctl(FD(pos, cpu, thread), PERF_EVENT_IOC_ENABLE);
279 }
280 }
281}
282
Arnaldo Carvalho de Melo806fb632011-11-29 08:05:52 -0200283static int perf_evlist__alloc_pollfd(struct perf_evlist *evlist)
Arnaldo Carvalho de Melo5c581042011-01-11 22:30:02 -0200284{
Arnaldo Carvalho de Melo7e2ed092011-01-30 11:59:43 -0200285 int nfds = evlist->cpus->nr * evlist->threads->nr * evlist->nr_entries;
Arnaldo Carvalho de Melo5c581042011-01-11 22:30:02 -0200286 evlist->pollfd = malloc(sizeof(struct pollfd) * nfds);
287 return evlist->pollfd != NULL ? 0 : -ENOMEM;
288}
Arnaldo Carvalho de Melo70082dd2011-01-12 17:03:24 -0200289
290void perf_evlist__add_pollfd(struct perf_evlist *evlist, int fd)
291{
292 fcntl(fd, F_SETFL, O_NONBLOCK);
293 evlist->pollfd[evlist->nr_fds].fd = fd;
294 evlist->pollfd[evlist->nr_fds].events = POLLIN;
295 evlist->nr_fds++;
296}
Arnaldo Carvalho de Melo70db7532011-01-12 22:39:13 -0200297
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -0300298static void perf_evlist__id_hash(struct perf_evlist *evlist,
299 struct perf_evsel *evsel,
300 int cpu, int thread, u64 id)
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200301{
Arnaldo Carvalho de Melo3d3b5e92011-03-04 22:29:39 -0300302 int hash;
303 struct perf_sample_id *sid = SID(evsel, cpu, thread);
304
305 sid->id = id;
306 sid->evsel = evsel;
307 hash = hash_64(sid->id, PERF_EVLIST__HLIST_BITS);
308 hlist_add_head(&sid->node, &evlist->heads[hash]);
309}
310
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -0300311void perf_evlist__id_add(struct perf_evlist *evlist, struct perf_evsel *evsel,
312 int cpu, int thread, u64 id)
313{
314 perf_evlist__id_hash(evlist, evsel, cpu, thread, id);
315 evsel->id[evsel->ids++] = id;
316}
317
318static int perf_evlist__id_add_fd(struct perf_evlist *evlist,
319 struct perf_evsel *evsel,
320 int cpu, int thread, int fd)
Arnaldo Carvalho de Melo3d3b5e92011-03-04 22:29:39 -0300321{
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200322 u64 read_data[4] = { 0, };
Arnaldo Carvalho de Melo3d3b5e92011-03-04 22:29:39 -0300323 int id_idx = 1; /* The first entry is the counter value */
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200324
325 if (!(evsel->attr.read_format & PERF_FORMAT_ID) ||
326 read(fd, &read_data, sizeof(read_data)) == -1)
327 return -1;
328
329 if (evsel->attr.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
330 ++id_idx;
331 if (evsel->attr.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
332 ++id_idx;
333
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -0300334 perf_evlist__id_add(evlist, evsel, cpu, thread, read_data[id_idx]);
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200335 return 0;
336}
337
Arnaldo Carvalho de Melo70db7532011-01-12 22:39:13 -0200338struct perf_evsel *perf_evlist__id2evsel(struct perf_evlist *evlist, u64 id)
339{
340 struct hlist_head *head;
341 struct hlist_node *pos;
342 struct perf_sample_id *sid;
343 int hash;
344
345 if (evlist->nr_entries == 1)
346 return list_entry(evlist->entries.next, struct perf_evsel, node);
347
348 hash = hash_64(id, PERF_EVLIST__HLIST_BITS);
349 head = &evlist->heads[hash];
350
351 hlist_for_each_entry(sid, pos, head, node)
352 if (sid->id == id)
353 return sid->evsel;
Namhyung Kim30e68bc2012-02-20 10:47:26 +0900354
355 if (!perf_evlist__sample_id_all(evlist))
356 return list_entry(evlist->entries.next, struct perf_evsel, node);
357
Arnaldo Carvalho de Melo70db7532011-01-12 22:39:13 -0200358 return NULL;
359}
Arnaldo Carvalho de Melo04391de2011-01-15 10:40:59 -0200360
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -0300361union perf_event *perf_evlist__mmap_read(struct perf_evlist *evlist, int idx)
Arnaldo Carvalho de Melo04391de2011-01-15 10:40:59 -0200362{
363 /* XXX Move this to perf.c, making it generally available */
364 unsigned int page_size = sysconf(_SC_PAGE_SIZE);
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -0300365 struct perf_mmap *md = &evlist->mmap[idx];
Arnaldo Carvalho de Melo04391de2011-01-15 10:40:59 -0200366 unsigned int head = perf_mmap__read_head(md);
367 unsigned int old = md->prev;
368 unsigned char *data = md->base + page_size;
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200369 union perf_event *event = NULL;
Arnaldo Carvalho de Melo04391de2011-01-15 10:40:59 -0200370
Arnaldo Carvalho de Melo7bb41152011-01-29 09:08:13 -0200371 if (evlist->overwrite) {
Arnaldo Carvalho de Melo04391de2011-01-15 10:40:59 -0200372 /*
Arnaldo Carvalho de Melo7bb41152011-01-29 09:08:13 -0200373 * If we're further behind than half the buffer, there's a chance
374 * the writer will bite our tail and mess up the samples under us.
375 *
376 * If we somehow ended up ahead of the head, we got messed up.
377 *
378 * In either case, truncate and restart at head.
Arnaldo Carvalho de Melo04391de2011-01-15 10:40:59 -0200379 */
Arnaldo Carvalho de Melo7bb41152011-01-29 09:08:13 -0200380 int diff = head - old;
381 if (diff > md->mask / 2 || diff < 0) {
382 fprintf(stderr, "WARNING: failed to keep up with mmap data.\n");
383
384 /*
385 * head points to a known good entry, start there.
386 */
387 old = head;
388 }
Arnaldo Carvalho de Melo04391de2011-01-15 10:40:59 -0200389 }
390
391 if (old != head) {
392 size_t size;
393
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200394 event = (union perf_event *)&data[old & md->mask];
Arnaldo Carvalho de Melo04391de2011-01-15 10:40:59 -0200395 size = event->header.size;
396
397 /*
398 * Event straddles the mmap boundary -- header should always
399 * be inside due to u64 alignment of output.
400 */
401 if ((old & md->mask) + size != ((old + size) & md->mask)) {
402 unsigned int offset = old;
403 unsigned int len = min(sizeof(*event), size), cpy;
404 void *dst = &evlist->event_copy;
405
406 do {
407 cpy = min(md->mask + 1 - (offset & md->mask), len);
408 memcpy(dst, &data[offset & md->mask], cpy);
409 offset += cpy;
410 dst += cpy;
411 len -= cpy;
412 } while (len);
413
414 event = &evlist->event_copy;
415 }
416
417 old += size;
418 }
419
420 md->prev = old;
Arnaldo Carvalho de Melo7bb41152011-01-29 09:08:13 -0200421
422 if (!evlist->overwrite)
423 perf_mmap__write_tail(md, old);
424
Arnaldo Carvalho de Melo04391de2011-01-15 10:40:59 -0200425 return event;
426}
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200427
Arnaldo Carvalho de Melo7e2ed092011-01-30 11:59:43 -0200428void perf_evlist__munmap(struct perf_evlist *evlist)
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200429{
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -0300430 int i;
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200431
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -0300432 for (i = 0; i < evlist->nr_mmaps; i++) {
433 if (evlist->mmap[i].base != NULL) {
434 munmap(evlist->mmap[i].base, evlist->mmap_len);
435 evlist->mmap[i].base = NULL;
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200436 }
437 }
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -0300438
439 free(evlist->mmap);
440 evlist->mmap = NULL;
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200441}
442
Arnaldo Carvalho de Melo806fb632011-11-29 08:05:52 -0200443static int perf_evlist__alloc_mmap(struct perf_evlist *evlist)
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200444{
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -0300445 evlist->nr_mmaps = evlist->cpus->nr;
446 if (evlist->cpus->map[0] == -1)
447 evlist->nr_mmaps = evlist->threads->nr;
448 evlist->mmap = zalloc(evlist->nr_mmaps * sizeof(struct perf_mmap));
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200449 return evlist->mmap != NULL ? 0 : -ENOMEM;
450}
451
Arnaldo Carvalho de Melobccdaba2011-06-02 10:39:43 -0300452static int __perf_evlist__mmap(struct perf_evlist *evlist,
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -0300453 int idx, int prot, int mask, int fd)
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200454{
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -0300455 evlist->mmap[idx].prev = 0;
456 evlist->mmap[idx].mask = mask;
457 evlist->mmap[idx].base = mmap(NULL, evlist->mmap_len, prot,
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200458 MAP_SHARED, fd, 0);
Nelson Elhage301b1952011-12-19 08:39:30 -0500459 if (evlist->mmap[idx].base == MAP_FAILED) {
460 evlist->mmap[idx].base = NULL;
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200461 return -1;
Nelson Elhage301b1952011-12-19 08:39:30 -0500462 }
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200463
464 perf_evlist__add_pollfd(evlist, fd);
465 return 0;
466}
467
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -0300468static int perf_evlist__mmap_per_cpu(struct perf_evlist *evlist, int prot, int mask)
469{
470 struct perf_evsel *evsel;
471 int cpu, thread;
472
473 for (cpu = 0; cpu < evlist->cpus->nr; cpu++) {
474 int output = -1;
475
476 for (thread = 0; thread < evlist->threads->nr; thread++) {
477 list_for_each_entry(evsel, &evlist->entries, node) {
478 int fd = FD(evsel, cpu, thread);
479
480 if (output == -1) {
481 output = fd;
Arnaldo Carvalho de Melobccdaba2011-06-02 10:39:43 -0300482 if (__perf_evlist__mmap(evlist, cpu,
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -0300483 prot, mask, output) < 0)
484 goto out_unmap;
485 } else {
486 if (ioctl(fd, PERF_EVENT_IOC_SET_OUTPUT, output) != 0)
487 goto out_unmap;
488 }
489
490 if ((evsel->attr.read_format & PERF_FORMAT_ID) &&
491 perf_evlist__id_add_fd(evlist, evsel, cpu, thread, fd) < 0)
492 goto out_unmap;
493 }
494 }
495 }
496
497 return 0;
498
499out_unmap:
500 for (cpu = 0; cpu < evlist->cpus->nr; cpu++) {
501 if (evlist->mmap[cpu].base != NULL) {
502 munmap(evlist->mmap[cpu].base, evlist->mmap_len);
503 evlist->mmap[cpu].base = NULL;
504 }
505 }
506 return -1;
507}
508
509static int perf_evlist__mmap_per_thread(struct perf_evlist *evlist, int prot, int mask)
510{
511 struct perf_evsel *evsel;
512 int thread;
513
514 for (thread = 0; thread < evlist->threads->nr; thread++) {
515 int output = -1;
516
517 list_for_each_entry(evsel, &evlist->entries, node) {
518 int fd = FD(evsel, 0, thread);
519
520 if (output == -1) {
521 output = fd;
Arnaldo Carvalho de Melobccdaba2011-06-02 10:39:43 -0300522 if (__perf_evlist__mmap(evlist, thread,
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -0300523 prot, mask, output) < 0)
524 goto out_unmap;
525 } else {
526 if (ioctl(fd, PERF_EVENT_IOC_SET_OUTPUT, output) != 0)
527 goto out_unmap;
528 }
529
530 if ((evsel->attr.read_format & PERF_FORMAT_ID) &&
531 perf_evlist__id_add_fd(evlist, evsel, 0, thread, fd) < 0)
532 goto out_unmap;
533 }
534 }
535
536 return 0;
537
538out_unmap:
539 for (thread = 0; thread < evlist->threads->nr; thread++) {
540 if (evlist->mmap[thread].base != NULL) {
541 munmap(evlist->mmap[thread].base, evlist->mmap_len);
542 evlist->mmap[thread].base = NULL;
543 }
544 }
545 return -1;
546}
547
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200548/** perf_evlist__mmap - Create per cpu maps to receive events
549 *
550 * @evlist - list of events
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200551 * @pages - map length in pages
552 * @overwrite - overwrite older events?
553 *
554 * If overwrite is false the user needs to signal event consuption using:
555 *
556 * struct perf_mmap *m = &evlist->mmap[cpu];
557 * unsigned int head = perf_mmap__read_head(m);
558 *
559 * perf_mmap__write_tail(m, head)
Arnaldo Carvalho de Melo7e2ed092011-01-30 11:59:43 -0200560 *
561 * Using perf_evlist__read_on_cpu does this automatically.
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200562 */
Arnaldo Carvalho de Melo50a682c2011-11-09 09:10:47 -0200563int perf_evlist__mmap(struct perf_evlist *evlist, unsigned int pages,
564 bool overwrite)
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200565{
566 unsigned int page_size = sysconf(_SC_PAGE_SIZE);
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -0300567 struct perf_evsel *evsel;
Arnaldo Carvalho de Melo7e2ed092011-01-30 11:59:43 -0200568 const struct cpu_map *cpus = evlist->cpus;
569 const struct thread_map *threads = evlist->threads;
Arnaldo Carvalho de Melo50a682c2011-11-09 09:10:47 -0200570 int prot = PROT_READ | (overwrite ? 0 : PROT_WRITE), mask;
571
572 /* 512 kiB: default amount of unprivileged mlocked memory */
573 if (pages == UINT_MAX)
574 pages = (512 * 1024) / page_size;
Nelson Elhage41d0d932011-12-19 08:39:32 -0500575 else if (!is_power_of_2(pages))
576 return -EINVAL;
Arnaldo Carvalho de Melo50a682c2011-11-09 09:10:47 -0200577
578 mask = pages * page_size - 1;
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200579
Arnaldo Carvalho de Melo7e2ed092011-01-30 11:59:43 -0200580 if (evlist->mmap == NULL && perf_evlist__alloc_mmap(evlist) < 0)
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200581 return -ENOMEM;
582
Arnaldo Carvalho de Melo7e2ed092011-01-30 11:59:43 -0200583 if (evlist->pollfd == NULL && perf_evlist__alloc_pollfd(evlist) < 0)
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200584 return -ENOMEM;
585
586 evlist->overwrite = overwrite;
587 evlist->mmap_len = (pages + 1) * page_size;
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200588
589 list_for_each_entry(evsel, &evlist->entries, node) {
590 if ((evsel->attr.read_format & PERF_FORMAT_ID) &&
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -0300591 evsel->sample_id == NULL &&
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200592 perf_evsel__alloc_id(evsel, cpus->nr, threads->nr) < 0)
593 return -ENOMEM;
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200594 }
595
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -0300596 if (evlist->cpus->map[0] == -1)
597 return perf_evlist__mmap_per_thread(evlist, prot, mask);
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200598
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -0300599 return perf_evlist__mmap_per_cpu(evlist, prot, mask);
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200600}
Arnaldo Carvalho de Melo7e2ed092011-01-30 11:59:43 -0200601
David Ahernb52956c2012-02-08 09:32:52 -0700602int perf_evlist__create_maps(struct perf_evlist *evlist, const char *target_pid,
603 const char *target_tid, uid_t uid, const char *cpu_list)
Arnaldo Carvalho de Melo7e2ed092011-01-30 11:59:43 -0200604{
David Ahernb52956c2012-02-08 09:32:52 -0700605 evlist->threads = thread_map__new_str(target_pid, target_tid, uid);
Arnaldo Carvalho de Melo7e2ed092011-01-30 11:59:43 -0200606
607 if (evlist->threads == NULL)
608 return -1;
609
David Ahernb52956c2012-02-08 09:32:52 -0700610 if (uid != UINT_MAX || (cpu_list == NULL && target_tid))
Arnaldo Carvalho de Melo7e2ed092011-01-30 11:59:43 -0200611 evlist->cpus = cpu_map__dummy_new();
612 else
613 evlist->cpus = cpu_map__new(cpu_list);
614
615 if (evlist->cpus == NULL)
616 goto out_delete_threads;
617
618 return 0;
619
620out_delete_threads:
621 thread_map__delete(evlist->threads);
622 return -1;
623}
624
625void perf_evlist__delete_maps(struct perf_evlist *evlist)
626{
627 cpu_map__delete(evlist->cpus);
628 thread_map__delete(evlist->threads);
629 evlist->cpus = NULL;
630 evlist->threads = NULL;
631}
Frederic Weisbecker0a102472011-02-26 04:51:54 +0100632
633int perf_evlist__set_filters(struct perf_evlist *evlist)
634{
635 const struct thread_map *threads = evlist->threads;
636 const struct cpu_map *cpus = evlist->cpus;
637 struct perf_evsel *evsel;
638 char *filter;
639 int thread;
640 int cpu;
641 int err;
642 int fd;
643
644 list_for_each_entry(evsel, &evlist->entries, node) {
645 filter = evsel->filter;
646 if (!filter)
647 continue;
648 for (cpu = 0; cpu < cpus->nr; cpu++) {
649 for (thread = 0; thread < threads->nr; thread++) {
650 fd = FD(evsel, cpu, thread);
651 err = ioctl(fd, PERF_EVENT_IOC_SET_FILTER, filter);
652 if (err)
653 return err;
654 }
655 }
656 }
657
658 return 0;
659}
Frederic Weisbecker74429962011-05-21 17:49:00 +0200660
Arnaldo Carvalho de Meloc2a70652011-06-02 11:04:54 -0300661bool perf_evlist__valid_sample_type(const struct perf_evlist *evlist)
Frederic Weisbecker74429962011-05-21 17:49:00 +0200662{
Arnaldo Carvalho de Meloc2a70652011-06-02 11:04:54 -0300663 struct perf_evsel *pos, *first;
Frederic Weisbecker74429962011-05-21 17:49:00 +0200664
Arnaldo Carvalho de Meloc2a70652011-06-02 11:04:54 -0300665 pos = first = list_entry(evlist->entries.next, struct perf_evsel, node);
666
667 list_for_each_entry_continue(pos, &evlist->entries, node) {
668 if (first->attr.sample_type != pos->attr.sample_type)
669 return false;
Frederic Weisbecker74429962011-05-21 17:49:00 +0200670 }
671
Arnaldo Carvalho de Meloc2a70652011-06-02 11:04:54 -0300672 return true;
673}
674
675u64 perf_evlist__sample_type(const struct perf_evlist *evlist)
676{
677 struct perf_evsel *first;
678
679 first = list_entry(evlist->entries.next, struct perf_evsel, node);
680 return first->attr.sample_type;
681}
682
Arnaldo Carvalho de Melo81e36bf2011-11-11 22:28:50 -0200683u16 perf_evlist__id_hdr_size(const struct perf_evlist *evlist)
684{
685 struct perf_evsel *first;
686 struct perf_sample *data;
687 u64 sample_type;
688 u16 size = 0;
689
690 first = list_entry(evlist->entries.next, struct perf_evsel, node);
691
692 if (!first->attr.sample_id_all)
693 goto out;
694
695 sample_type = first->attr.sample_type;
696
697 if (sample_type & PERF_SAMPLE_TID)
698 size += sizeof(data->tid) * 2;
699
700 if (sample_type & PERF_SAMPLE_TIME)
701 size += sizeof(data->time);
702
703 if (sample_type & PERF_SAMPLE_ID)
704 size += sizeof(data->id);
705
706 if (sample_type & PERF_SAMPLE_STREAM_ID)
707 size += sizeof(data->stream_id);
708
709 if (sample_type & PERF_SAMPLE_CPU)
710 size += sizeof(data->cpu) * 2;
711out:
712 return size;
713}
714
Arnaldo Carvalho de Meloc2a70652011-06-02 11:04:54 -0300715bool perf_evlist__valid_sample_id_all(const struct perf_evlist *evlist)
716{
717 struct perf_evsel *pos, *first;
718
719 pos = first = list_entry(evlist->entries.next, struct perf_evsel, node);
720
721 list_for_each_entry_continue(pos, &evlist->entries, node) {
722 if (first->attr.sample_id_all != pos->attr.sample_id_all)
723 return false;
724 }
725
726 return true;
Frederic Weisbecker74429962011-05-21 17:49:00 +0200727}
728
729bool perf_evlist__sample_id_all(const struct perf_evlist *evlist)
730{
Arnaldo Carvalho de Meloc2a70652011-06-02 11:04:54 -0300731 struct perf_evsel *first;
Frederic Weisbecker74429962011-05-21 17:49:00 +0200732
Arnaldo Carvalho de Meloc2a70652011-06-02 11:04:54 -0300733 first = list_entry(evlist->entries.next, struct perf_evsel, node);
734 return first->attr.sample_id_all;
Frederic Weisbecker74429962011-05-21 17:49:00 +0200735}
Arnaldo Carvalho de Melo81cce8d2011-10-05 19:11:32 -0300736
737void perf_evlist__set_selected(struct perf_evlist *evlist,
738 struct perf_evsel *evsel)
739{
740 evlist->selected = evsel;
741}
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -0200742
743int perf_evlist__open(struct perf_evlist *evlist, bool group)
744{
745 struct perf_evsel *evsel, *first;
746 int err, ncpus, nthreads;
747
748 first = list_entry(evlist->entries.next, struct perf_evsel, node);
749
750 list_for_each_entry(evsel, &evlist->entries, node) {
751 struct xyarray *group_fd = NULL;
752
753 if (group && evsel != first)
754 group_fd = first->fd;
755
756 err = perf_evsel__open(evsel, evlist->cpus, evlist->threads,
757 group, group_fd);
758 if (err < 0)
759 goto out_err;
760 }
761
762 return 0;
763out_err:
764 ncpus = evlist->cpus ? evlist->cpus->nr : 1;
765 nthreads = evlist->threads ? evlist->threads->nr : 1;
766
767 list_for_each_entry_reverse(evsel, &evlist->entries, node)
768 perf_evsel__close(evsel, ncpus, nthreads);
769
Namhyung Kim41c21a62012-02-23 12:13:36 +0900770 errno = -err;
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -0200771 return err;
772}
Arnaldo Carvalho de Melo35b9d882011-11-09 08:47:15 -0200773
774int perf_evlist__prepare_workload(struct perf_evlist *evlist,
775 struct perf_record_opts *opts,
776 const char *argv[])
777{
778 int child_ready_pipe[2], go_pipe[2];
779 char bf;
780
781 if (pipe(child_ready_pipe) < 0) {
782 perror("failed to create 'ready' pipe");
783 return -1;
784 }
785
786 if (pipe(go_pipe) < 0) {
787 perror("failed to create 'go' pipe");
788 goto out_close_ready_pipe;
789 }
790
791 evlist->workload.pid = fork();
792 if (evlist->workload.pid < 0) {
793 perror("failed to fork");
794 goto out_close_pipes;
795 }
796
797 if (!evlist->workload.pid) {
798 if (opts->pipe_output)
799 dup2(2, 1);
800
801 close(child_ready_pipe[0]);
802 close(go_pipe[1]);
803 fcntl(go_pipe[0], F_SETFD, FD_CLOEXEC);
804
805 /*
806 * Do a dummy execvp to get the PLT entry resolved,
807 * so we avoid the resolver overhead on the real
808 * execvp call.
809 */
810 execvp("", (char **)argv);
811
812 /*
813 * Tell the parent we're ready to go
814 */
815 close(child_ready_pipe[1]);
816
817 /*
818 * Wait until the parent tells us to go.
819 */
820 if (read(go_pipe[0], &bf, 1) == -1)
821 perror("unable to read pipe");
822
823 execvp(argv[0], (char **)argv);
824
825 perror(argv[0]);
826 kill(getppid(), SIGUSR1);
827 exit(-1);
828 }
829
David Ahernb52956c2012-02-08 09:32:52 -0700830 if (!opts->system_wide && !opts->target_tid && !opts->target_pid)
Arnaldo Carvalho de Melo35b9d882011-11-09 08:47:15 -0200831 evlist->threads->map[0] = evlist->workload.pid;
832
833 close(child_ready_pipe[1]);
834 close(go_pipe[0]);
835 /*
836 * wait for child to settle
837 */
838 if (read(child_ready_pipe[0], &bf, 1) == -1) {
839 perror("unable to read pipe");
840 goto out_close_pipes;
841 }
842
843 evlist->workload.cork_fd = go_pipe[1];
844 close(child_ready_pipe[0]);
845 return 0;
846
847out_close_pipes:
848 close(go_pipe[0]);
849 close(go_pipe[1]);
850out_close_ready_pipe:
851 close(child_ready_pipe[0]);
852 close(child_ready_pipe[1]);
853 return -1;
854}
855
856int perf_evlist__start_workload(struct perf_evlist *evlist)
857{
858 if (evlist->workload.cork_fd > 0) {
859 /*
860 * Remove the cork, let it rip!
861 */
862 return close(evlist->workload.cork_fd);
863 }
864
865 return 0;
866}