blob: 6a2809fd257ef518a65cd1a28659c8f431d54cf7 [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
Arnaldo Carvalho de Melo0c21f732012-08-14 16:42:15 -030060 first = perf_evlist__first(evlist);
Namhyung Kim5090c6a2012-03-16 17:42:20 +090061
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 Melo63dab222012-08-14 16:35:48 -0300111void __perf_evlist__set_leader(struct list_head *list)
112{
113 struct perf_evsel *evsel, *leader;
114
115 leader = list_entry(list->next, struct perf_evsel, node);
116 leader->leader = NULL;
117
118 list_for_each_entry(evsel, list, node) {
119 if (evsel != leader)
120 evsel->leader = leader;
121 }
122}
123
124void perf_evlist__set_leader(struct perf_evlist *evlist)
Jiri Olsa6a4bb042012-08-08 12:22:36 +0200125{
126 if (evlist->nr_entries)
Arnaldo Carvalho de Melo63dab222012-08-14 16:35:48 -0300127 __perf_evlist__set_leader(&evlist->entries);
Jiri Olsa6a4bb042012-08-08 12:22:36 +0200128}
129
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -0200130int perf_evlist__add_default(struct perf_evlist *evlist)
131{
132 struct perf_event_attr attr = {
133 .type = PERF_TYPE_HARDWARE,
134 .config = PERF_COUNT_HW_CPU_CYCLES,
135 };
Joerg Roedel1aed2672012-01-04 17:54:20 +0100136 struct perf_evsel *evsel;
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -0200137
Joerg Roedel1aed2672012-01-04 17:54:20 +0100138 event_attr_init(&attr);
139
140 evsel = perf_evsel__new(&attr, 0);
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -0200141 if (evsel == NULL)
Stephane Eraniancc2d86b2011-06-07 18:19:36 +0200142 goto error;
143
144 /* use strdup() because free(evsel) assumes name is allocated */
145 evsel->name = strdup("cycles");
146 if (!evsel->name)
147 goto error_free;
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -0200148
149 perf_evlist__add(evlist, evsel);
150 return 0;
Stephane Eraniancc2d86b2011-06-07 18:19:36 +0200151error_free:
152 perf_evsel__delete(evsel);
153error:
154 return -ENOMEM;
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -0200155}
Arnaldo Carvalho de Melo5c581042011-01-11 22:30:02 -0200156
Arnaldo Carvalho de Melo50d08e42011-11-04 09:10:59 -0200157int perf_evlist__add_attrs(struct perf_evlist *evlist,
158 struct perf_event_attr *attrs, size_t nr_attrs)
159{
160 struct perf_evsel *evsel, *n;
161 LIST_HEAD(head);
162 size_t i;
163
164 for (i = 0; i < nr_attrs; i++) {
165 evsel = perf_evsel__new(attrs + i, evlist->nr_entries + i);
166 if (evsel == NULL)
167 goto out_delete_partial_list;
168 list_add_tail(&evsel->node, &head);
169 }
170
171 perf_evlist__splice_list_tail(evlist, &head, nr_attrs);
172
173 return 0;
174
175out_delete_partial_list:
176 list_for_each_entry_safe(evsel, n, &head, node)
177 perf_evsel__delete(evsel);
178 return -1;
179}
180
Arnaldo Carvalho de Melo79695e12012-05-30 13:53:54 -0300181int __perf_evlist__add_default_attrs(struct perf_evlist *evlist,
182 struct perf_event_attr *attrs, size_t nr_attrs)
183{
184 size_t i;
185
186 for (i = 0; i < nr_attrs; i++)
187 event_attr_init(attrs + i);
188
189 return perf_evlist__add_attrs(evlist, attrs, nr_attrs);
190}
191
Arnaldo Carvalho de Meloa8c9ae12011-11-05 08:41:51 -0200192static int trace_event__id(const char *evname)
193{
194 char *filename, *colon;
195 int err = -1, fd;
196
197 if (asprintf(&filename, "%s/%s/id", tracing_events_path, evname) < 0)
198 return -1;
199
200 colon = strrchr(filename, ':');
201 if (colon != NULL)
202 *colon = '/';
203
204 fd = open(filename, O_RDONLY);
205 if (fd >= 0) {
206 char id[16];
207 if (read(fd, id, sizeof(id)) > 0)
208 err = atoi(id);
209 close(fd);
210 }
211
212 free(filename);
213 return err;
214}
215
216int perf_evlist__add_tracepoints(struct perf_evlist *evlist,
217 const char *tracepoints[],
218 size_t nr_tracepoints)
219{
220 int err;
221 size_t i;
222 struct perf_event_attr *attrs = zalloc(nr_tracepoints * sizeof(*attrs));
223
224 if (attrs == NULL)
225 return -1;
226
227 for (i = 0; i < nr_tracepoints; i++) {
228 err = trace_event__id(tracepoints[i]);
229
230 if (err < 0)
231 goto out_free_attrs;
232
233 attrs[i].type = PERF_TYPE_TRACEPOINT;
234 attrs[i].config = err;
235 attrs[i].sample_type = (PERF_SAMPLE_RAW | PERF_SAMPLE_TIME |
Frederic Weisbecker0983cc02012-07-18 19:10:54 +0200236 PERF_SAMPLE_CPU | PERF_SAMPLE_PERIOD);
Arnaldo Carvalho de Meloa8c9ae12011-11-05 08:41:51 -0200237 attrs[i].sample_period = 1;
238 }
239
240 err = perf_evlist__add_attrs(evlist, attrs, nr_tracepoints);
241out_free_attrs:
242 free(attrs);
243 return err;
244}
245
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -0300246struct perf_evsel *
247perf_evlist__find_tracepoint_by_id(struct perf_evlist *evlist, int id)
Arnaldo Carvalho de Meloee29be62011-11-28 17:57:40 -0200248{
249 struct perf_evsel *evsel;
250
251 list_for_each_entry(evsel, &evlist->entries, node) {
252 if (evsel->attr.type == PERF_TYPE_TRACEPOINT &&
253 (int)evsel->attr.config == id)
254 return evsel;
255 }
256
257 return NULL;
258}
259
260int perf_evlist__set_tracepoints_handlers(struct perf_evlist *evlist,
261 const struct perf_evsel_str_handler *assocs,
262 size_t nr_assocs)
263{
264 struct perf_evsel *evsel;
265 int err;
266 size_t i;
267
268 for (i = 0; i < nr_assocs; i++) {
269 err = trace_event__id(assocs[i].name);
270 if (err < 0)
271 goto out;
272
273 evsel = perf_evlist__find_tracepoint_by_id(evlist, err);
274 if (evsel == NULL)
275 continue;
276
277 err = -EEXIST;
278 if (evsel->handler.func != NULL)
279 goto out;
280 evsel->handler.func = assocs[i].handler;
281 }
282
283 err = 0;
284out:
285 return err;
286}
287
Arnaldo Carvalho de Melo39876e72012-10-03 11:40:22 -0300288int perf_evlist__add_newtp(struct perf_evlist *evlist,
289 const char *sys, const char *name, void *handler)
290{
291 struct perf_evsel *evsel;
292
293 evsel = perf_evsel__newtp(sys, name, evlist->nr_entries);
294 if (evsel == NULL)
295 return -1;
296
297 evsel->handler.func = handler;
298 perf_evlist__add(evlist, evsel);
299 return 0;
300}
301
Arnaldo Carvalho de Melo4152ab32011-07-25 11:06:19 -0300302void perf_evlist__disable(struct perf_evlist *evlist)
303{
304 int cpu, thread;
305 struct perf_evsel *pos;
306
307 for (cpu = 0; cpu < evlist->cpus->nr; cpu++) {
308 list_for_each_entry(pos, &evlist->entries, node) {
309 for (thread = 0; thread < evlist->threads->nr; thread++)
Namhyung Kim55da8002012-05-31 14:51:46 +0900310 ioctl(FD(pos, cpu, thread),
311 PERF_EVENT_IOC_DISABLE, 0);
Arnaldo Carvalho de Melo4152ab32011-07-25 11:06:19 -0300312 }
313 }
314}
315
David Ahern764e16a32011-08-25 10:17:55 -0600316void perf_evlist__enable(struct perf_evlist *evlist)
317{
318 int cpu, thread;
319 struct perf_evsel *pos;
320
Arnaldo Carvalho de Meloa14bb7a2012-09-26 12:41:14 -0300321 for (cpu = 0; cpu < cpu_map__nr(evlist->cpus); cpu++) {
David Ahern764e16a32011-08-25 10:17:55 -0600322 list_for_each_entry(pos, &evlist->entries, node) {
323 for (thread = 0; thread < evlist->threads->nr; thread++)
Namhyung Kim55da8002012-05-31 14:51:46 +0900324 ioctl(FD(pos, cpu, thread),
325 PERF_EVENT_IOC_ENABLE, 0);
David Ahern764e16a32011-08-25 10:17:55 -0600326 }
327 }
328}
329
Arnaldo Carvalho de Melo806fb632011-11-29 08:05:52 -0200330static int perf_evlist__alloc_pollfd(struct perf_evlist *evlist)
Arnaldo Carvalho de Melo5c581042011-01-11 22:30:02 -0200331{
Arnaldo Carvalho de Meloa14bb7a2012-09-26 12:41:14 -0300332 int nfds = cpu_map__nr(evlist->cpus) * evlist->threads->nr * evlist->nr_entries;
Arnaldo Carvalho de Melo5c581042011-01-11 22:30:02 -0200333 evlist->pollfd = malloc(sizeof(struct pollfd) * nfds);
334 return evlist->pollfd != NULL ? 0 : -ENOMEM;
335}
Arnaldo Carvalho de Melo70082dd2011-01-12 17:03:24 -0200336
337void perf_evlist__add_pollfd(struct perf_evlist *evlist, int fd)
338{
339 fcntl(fd, F_SETFL, O_NONBLOCK);
340 evlist->pollfd[evlist->nr_fds].fd = fd;
341 evlist->pollfd[evlist->nr_fds].events = POLLIN;
342 evlist->nr_fds++;
343}
Arnaldo Carvalho de Melo70db7532011-01-12 22:39:13 -0200344
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -0300345static void perf_evlist__id_hash(struct perf_evlist *evlist,
346 struct perf_evsel *evsel,
347 int cpu, int thread, u64 id)
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200348{
Arnaldo Carvalho de Melo3d3b5e92011-03-04 22:29:39 -0300349 int hash;
350 struct perf_sample_id *sid = SID(evsel, cpu, thread);
351
352 sid->id = id;
353 sid->evsel = evsel;
354 hash = hash_64(sid->id, PERF_EVLIST__HLIST_BITS);
355 hlist_add_head(&sid->node, &evlist->heads[hash]);
356}
357
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -0300358void perf_evlist__id_add(struct perf_evlist *evlist, struct perf_evsel *evsel,
359 int cpu, int thread, u64 id)
360{
361 perf_evlist__id_hash(evlist, evsel, cpu, thread, id);
362 evsel->id[evsel->ids++] = id;
363}
364
365static int perf_evlist__id_add_fd(struct perf_evlist *evlist,
366 struct perf_evsel *evsel,
367 int cpu, int thread, int fd)
Arnaldo Carvalho de Melo3d3b5e92011-03-04 22:29:39 -0300368{
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200369 u64 read_data[4] = { 0, };
Arnaldo Carvalho de Melo3d3b5e92011-03-04 22:29:39 -0300370 int id_idx = 1; /* The first entry is the counter value */
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200371
372 if (!(evsel->attr.read_format & PERF_FORMAT_ID) ||
373 read(fd, &read_data, sizeof(read_data)) == -1)
374 return -1;
375
376 if (evsel->attr.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
377 ++id_idx;
378 if (evsel->attr.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
379 ++id_idx;
380
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -0300381 perf_evlist__id_add(evlist, evsel, cpu, thread, read_data[id_idx]);
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200382 return 0;
383}
384
Arnaldo Carvalho de Melo70db7532011-01-12 22:39:13 -0200385struct perf_evsel *perf_evlist__id2evsel(struct perf_evlist *evlist, u64 id)
386{
387 struct hlist_head *head;
388 struct hlist_node *pos;
389 struct perf_sample_id *sid;
390 int hash;
391
392 if (evlist->nr_entries == 1)
Arnaldo Carvalho de Melo0c21f732012-08-14 16:42:15 -0300393 return perf_evlist__first(evlist);
Arnaldo Carvalho de Melo70db7532011-01-12 22:39:13 -0200394
395 hash = hash_64(id, PERF_EVLIST__HLIST_BITS);
396 head = &evlist->heads[hash];
397
398 hlist_for_each_entry(sid, pos, head, node)
399 if (sid->id == id)
400 return sid->evsel;
Namhyung Kim30e68bc2012-02-20 10:47:26 +0900401
402 if (!perf_evlist__sample_id_all(evlist))
Arnaldo Carvalho de Melo0c21f732012-08-14 16:42:15 -0300403 return perf_evlist__first(evlist);
Namhyung Kim30e68bc2012-02-20 10:47:26 +0900404
Arnaldo Carvalho de Melo70db7532011-01-12 22:39:13 -0200405 return NULL;
406}
Arnaldo Carvalho de Melo04391de2011-01-15 10:40:59 -0200407
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -0300408union perf_event *perf_evlist__mmap_read(struct perf_evlist *evlist, int idx)
Arnaldo Carvalho de Melo04391de2011-01-15 10:40:59 -0200409{
410 /* XXX Move this to perf.c, making it generally available */
411 unsigned int page_size = sysconf(_SC_PAGE_SIZE);
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -0300412 struct perf_mmap *md = &evlist->mmap[idx];
Arnaldo Carvalho de Melo04391de2011-01-15 10:40:59 -0200413 unsigned int head = perf_mmap__read_head(md);
414 unsigned int old = md->prev;
415 unsigned char *data = md->base + page_size;
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200416 union perf_event *event = NULL;
Arnaldo Carvalho de Melo04391de2011-01-15 10:40:59 -0200417
Arnaldo Carvalho de Melo7bb41152011-01-29 09:08:13 -0200418 if (evlist->overwrite) {
Arnaldo Carvalho de Melo04391de2011-01-15 10:40:59 -0200419 /*
Arnaldo Carvalho de Melo7bb41152011-01-29 09:08:13 -0200420 * If we're further behind than half the buffer, there's a chance
421 * the writer will bite our tail and mess up the samples under us.
422 *
423 * If we somehow ended up ahead of the head, we got messed up.
424 *
425 * In either case, truncate and restart at head.
Arnaldo Carvalho de Melo04391de2011-01-15 10:40:59 -0200426 */
Arnaldo Carvalho de Melo7bb41152011-01-29 09:08:13 -0200427 int diff = head - old;
428 if (diff > md->mask / 2 || diff < 0) {
429 fprintf(stderr, "WARNING: failed to keep up with mmap data.\n");
430
431 /*
432 * head points to a known good entry, start there.
433 */
434 old = head;
435 }
Arnaldo Carvalho de Melo04391de2011-01-15 10:40:59 -0200436 }
437
438 if (old != head) {
439 size_t size;
440
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200441 event = (union perf_event *)&data[old & md->mask];
Arnaldo Carvalho de Melo04391de2011-01-15 10:40:59 -0200442 size = event->header.size;
443
444 /*
445 * Event straddles the mmap boundary -- header should always
446 * be inside due to u64 alignment of output.
447 */
448 if ((old & md->mask) + size != ((old + size) & md->mask)) {
449 unsigned int offset = old;
450 unsigned int len = min(sizeof(*event), size), cpy;
451 void *dst = &evlist->event_copy;
452
453 do {
454 cpy = min(md->mask + 1 - (offset & md->mask), len);
455 memcpy(dst, &data[offset & md->mask], cpy);
456 offset += cpy;
457 dst += cpy;
458 len -= cpy;
459 } while (len);
460
461 event = &evlist->event_copy;
462 }
463
464 old += size;
465 }
466
467 md->prev = old;
Arnaldo Carvalho de Melo7bb41152011-01-29 09:08:13 -0200468
469 if (!evlist->overwrite)
470 perf_mmap__write_tail(md, old);
471
Arnaldo Carvalho de Melo04391de2011-01-15 10:40:59 -0200472 return event;
473}
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200474
Arnaldo Carvalho de Melo7e2ed092011-01-30 11:59:43 -0200475void perf_evlist__munmap(struct perf_evlist *evlist)
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200476{
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -0300477 int i;
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200478
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -0300479 for (i = 0; i < evlist->nr_mmaps; i++) {
480 if (evlist->mmap[i].base != NULL) {
481 munmap(evlist->mmap[i].base, evlist->mmap_len);
482 evlist->mmap[i].base = NULL;
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200483 }
484 }
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -0300485
486 free(evlist->mmap);
487 evlist->mmap = NULL;
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200488}
489
Arnaldo Carvalho de Melo806fb632011-11-29 08:05:52 -0200490static int perf_evlist__alloc_mmap(struct perf_evlist *evlist)
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200491{
Arnaldo Carvalho de Meloa14bb7a2012-09-26 12:41:14 -0300492 evlist->nr_mmaps = cpu_map__nr(evlist->cpus);
493 if (cpu_map__all(evlist->cpus))
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -0300494 evlist->nr_mmaps = evlist->threads->nr;
495 evlist->mmap = zalloc(evlist->nr_mmaps * sizeof(struct perf_mmap));
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200496 return evlist->mmap != NULL ? 0 : -ENOMEM;
497}
498
Arnaldo Carvalho de Melobccdaba2011-06-02 10:39:43 -0300499static int __perf_evlist__mmap(struct perf_evlist *evlist,
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -0300500 int idx, int prot, int mask, int fd)
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200501{
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -0300502 evlist->mmap[idx].prev = 0;
503 evlist->mmap[idx].mask = mask;
504 evlist->mmap[idx].base = mmap(NULL, evlist->mmap_len, prot,
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200505 MAP_SHARED, fd, 0);
Nelson Elhage301b1952011-12-19 08:39:30 -0500506 if (evlist->mmap[idx].base == MAP_FAILED) {
507 evlist->mmap[idx].base = NULL;
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200508 return -1;
Nelson Elhage301b1952011-12-19 08:39:30 -0500509 }
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200510
511 perf_evlist__add_pollfd(evlist, fd);
512 return 0;
513}
514
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -0300515static int perf_evlist__mmap_per_cpu(struct perf_evlist *evlist, int prot, int mask)
516{
517 struct perf_evsel *evsel;
518 int cpu, thread;
519
520 for (cpu = 0; cpu < evlist->cpus->nr; cpu++) {
521 int output = -1;
522
523 for (thread = 0; thread < evlist->threads->nr; thread++) {
524 list_for_each_entry(evsel, &evlist->entries, node) {
525 int fd = FD(evsel, cpu, thread);
526
527 if (output == -1) {
528 output = fd;
Arnaldo Carvalho de Melobccdaba2011-06-02 10:39:43 -0300529 if (__perf_evlist__mmap(evlist, cpu,
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -0300530 prot, mask, output) < 0)
531 goto out_unmap;
532 } else {
533 if (ioctl(fd, PERF_EVENT_IOC_SET_OUTPUT, output) != 0)
534 goto out_unmap;
535 }
536
537 if ((evsel->attr.read_format & PERF_FORMAT_ID) &&
538 perf_evlist__id_add_fd(evlist, evsel, cpu, thread, fd) < 0)
539 goto out_unmap;
540 }
541 }
542 }
543
544 return 0;
545
546out_unmap:
547 for (cpu = 0; cpu < evlist->cpus->nr; cpu++) {
548 if (evlist->mmap[cpu].base != NULL) {
549 munmap(evlist->mmap[cpu].base, evlist->mmap_len);
550 evlist->mmap[cpu].base = NULL;
551 }
552 }
553 return -1;
554}
555
556static int perf_evlist__mmap_per_thread(struct perf_evlist *evlist, int prot, int mask)
557{
558 struct perf_evsel *evsel;
559 int thread;
560
561 for (thread = 0; thread < evlist->threads->nr; thread++) {
562 int output = -1;
563
564 list_for_each_entry(evsel, &evlist->entries, node) {
565 int fd = FD(evsel, 0, thread);
566
567 if (output == -1) {
568 output = fd;
Arnaldo Carvalho de Melobccdaba2011-06-02 10:39:43 -0300569 if (__perf_evlist__mmap(evlist, thread,
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -0300570 prot, mask, output) < 0)
571 goto out_unmap;
572 } else {
573 if (ioctl(fd, PERF_EVENT_IOC_SET_OUTPUT, output) != 0)
574 goto out_unmap;
575 }
576
577 if ((evsel->attr.read_format & PERF_FORMAT_ID) &&
578 perf_evlist__id_add_fd(evlist, evsel, 0, thread, fd) < 0)
579 goto out_unmap;
580 }
581 }
582
583 return 0;
584
585out_unmap:
586 for (thread = 0; thread < evlist->threads->nr; thread++) {
587 if (evlist->mmap[thread].base != NULL) {
588 munmap(evlist->mmap[thread].base, evlist->mmap_len);
589 evlist->mmap[thread].base = NULL;
590 }
591 }
592 return -1;
593}
594
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200595/** perf_evlist__mmap - Create per cpu maps to receive events
596 *
597 * @evlist - list of events
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200598 * @pages - map length in pages
599 * @overwrite - overwrite older events?
600 *
601 * If overwrite is false the user needs to signal event consuption using:
602 *
603 * struct perf_mmap *m = &evlist->mmap[cpu];
604 * unsigned int head = perf_mmap__read_head(m);
605 *
606 * perf_mmap__write_tail(m, head)
Arnaldo Carvalho de Melo7e2ed092011-01-30 11:59:43 -0200607 *
608 * Using perf_evlist__read_on_cpu does this automatically.
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200609 */
Arnaldo Carvalho de Melo50a682c2011-11-09 09:10:47 -0200610int perf_evlist__mmap(struct perf_evlist *evlist, unsigned int pages,
611 bool overwrite)
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200612{
613 unsigned int page_size = sysconf(_SC_PAGE_SIZE);
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -0300614 struct perf_evsel *evsel;
Arnaldo Carvalho de Melo7e2ed092011-01-30 11:59:43 -0200615 const struct cpu_map *cpus = evlist->cpus;
616 const struct thread_map *threads = evlist->threads;
Arnaldo Carvalho de Melo50a682c2011-11-09 09:10:47 -0200617 int prot = PROT_READ | (overwrite ? 0 : PROT_WRITE), mask;
618
619 /* 512 kiB: default amount of unprivileged mlocked memory */
620 if (pages == UINT_MAX)
621 pages = (512 * 1024) / page_size;
Nelson Elhage41d0d932011-12-19 08:39:32 -0500622 else if (!is_power_of_2(pages))
623 return -EINVAL;
Arnaldo Carvalho de Melo50a682c2011-11-09 09:10:47 -0200624
625 mask = pages * page_size - 1;
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200626
Arnaldo Carvalho de Melo7e2ed092011-01-30 11:59:43 -0200627 if (evlist->mmap == NULL && perf_evlist__alloc_mmap(evlist) < 0)
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200628 return -ENOMEM;
629
Arnaldo Carvalho de Melo7e2ed092011-01-30 11:59:43 -0200630 if (evlist->pollfd == NULL && perf_evlist__alloc_pollfd(evlist) < 0)
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200631 return -ENOMEM;
632
633 evlist->overwrite = overwrite;
634 evlist->mmap_len = (pages + 1) * page_size;
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200635
636 list_for_each_entry(evsel, &evlist->entries, node) {
637 if ((evsel->attr.read_format & PERF_FORMAT_ID) &&
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -0300638 evsel->sample_id == NULL &&
Arnaldo Carvalho de Meloa14bb7a2012-09-26 12:41:14 -0300639 perf_evsel__alloc_id(evsel, cpu_map__nr(cpus), threads->nr) < 0)
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200640 return -ENOMEM;
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200641 }
642
Arnaldo Carvalho de Meloa14bb7a2012-09-26 12:41:14 -0300643 if (cpu_map__all(cpus))
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -0300644 return perf_evlist__mmap_per_thread(evlist, prot, mask);
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200645
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -0300646 return perf_evlist__mmap_per_cpu(evlist, prot, mask);
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200647}
Arnaldo Carvalho de Melo7e2ed092011-01-30 11:59:43 -0200648
Namhyung Kimb809ac12012-04-26 14:15:19 +0900649int perf_evlist__create_maps(struct perf_evlist *evlist,
650 struct perf_target *target)
Arnaldo Carvalho de Melo7e2ed092011-01-30 11:59:43 -0200651{
Namhyung Kimb809ac12012-04-26 14:15:19 +0900652 evlist->threads = thread_map__new_str(target->pid, target->tid,
653 target->uid);
Arnaldo Carvalho de Melo7e2ed092011-01-30 11:59:43 -0200654
655 if (evlist->threads == NULL)
656 return -1;
657
Namhyung Kim879d77d2012-05-16 18:45:48 +0900658 if (perf_target__has_task(target))
Namhyung Kimd67356e2012-05-07 14:09:03 +0900659 evlist->cpus = cpu_map__dummy_new();
Namhyung Kimd1cb9fc2012-05-16 18:45:49 +0900660 else if (!perf_target__has_cpu(target) && !target->uses_mmap)
661 evlist->cpus = cpu_map__dummy_new();
Namhyung Kim879d77d2012-05-16 18:45:48 +0900662 else
663 evlist->cpus = cpu_map__new(target->cpu_list);
Arnaldo Carvalho de Melo7e2ed092011-01-30 11:59:43 -0200664
665 if (evlist->cpus == NULL)
666 goto out_delete_threads;
667
668 return 0;
669
670out_delete_threads:
671 thread_map__delete(evlist->threads);
672 return -1;
673}
674
675void perf_evlist__delete_maps(struct perf_evlist *evlist)
676{
677 cpu_map__delete(evlist->cpus);
678 thread_map__delete(evlist->threads);
679 evlist->cpus = NULL;
680 evlist->threads = NULL;
681}
Frederic Weisbecker0a102472011-02-26 04:51:54 +0100682
Arnaldo Carvalho de Melo1491a632012-09-26 14:43:13 -0300683int perf_evlist__apply_filters(struct perf_evlist *evlist)
Frederic Weisbecker0a102472011-02-26 04:51:54 +0100684{
Frederic Weisbecker0a102472011-02-26 04:51:54 +0100685 struct perf_evsel *evsel;
Arnaldo Carvalho de Melo745cefc2012-09-26 15:07:39 -0300686 int err = 0;
687 const int ncpus = cpu_map__nr(evlist->cpus),
688 nthreads = evlist->threads->nr;
Frederic Weisbecker0a102472011-02-26 04:51:54 +0100689
690 list_for_each_entry(evsel, &evlist->entries, node) {
Arnaldo Carvalho de Melo745cefc2012-09-26 15:07:39 -0300691 if (evsel->filter == NULL)
Frederic Weisbecker0a102472011-02-26 04:51:54 +0100692 continue;
Arnaldo Carvalho de Melo745cefc2012-09-26 15:07:39 -0300693
694 err = perf_evsel__set_filter(evsel, ncpus, nthreads, evsel->filter);
695 if (err)
696 break;
Frederic Weisbecker0a102472011-02-26 04:51:54 +0100697 }
698
Arnaldo Carvalho de Melo745cefc2012-09-26 15:07:39 -0300699 return err;
700}
701
702int perf_evlist__set_filter(struct perf_evlist *evlist, const char *filter)
703{
704 struct perf_evsel *evsel;
705 int err = 0;
706 const int ncpus = cpu_map__nr(evlist->cpus),
707 nthreads = evlist->threads->nr;
708
709 list_for_each_entry(evsel, &evlist->entries, node) {
710 err = perf_evsel__set_filter(evsel, ncpus, nthreads, filter);
711 if (err)
712 break;
713 }
714
715 return err;
Frederic Weisbecker0a102472011-02-26 04:51:54 +0100716}
Frederic Weisbecker74429962011-05-21 17:49:00 +0200717
Arnaldo Carvalho de Melo0c21f732012-08-14 16:42:15 -0300718bool perf_evlist__valid_sample_type(struct perf_evlist *evlist)
Frederic Weisbecker74429962011-05-21 17:49:00 +0200719{
Arnaldo Carvalho de Melo0c21f732012-08-14 16:42:15 -0300720 struct perf_evsel *first = perf_evlist__first(evlist), *pos = first;
Arnaldo Carvalho de Meloc2a70652011-06-02 11:04:54 -0300721
722 list_for_each_entry_continue(pos, &evlist->entries, node) {
723 if (first->attr.sample_type != pos->attr.sample_type)
724 return false;
Frederic Weisbecker74429962011-05-21 17:49:00 +0200725 }
726
Arnaldo Carvalho de Meloc2a70652011-06-02 11:04:54 -0300727 return true;
728}
729
Arnaldo Carvalho de Melo0c21f732012-08-14 16:42:15 -0300730u64 perf_evlist__sample_type(struct perf_evlist *evlist)
Arnaldo Carvalho de Meloc2a70652011-06-02 11:04:54 -0300731{
Arnaldo Carvalho de Melo0c21f732012-08-14 16:42:15 -0300732 struct perf_evsel *first = perf_evlist__first(evlist);
Arnaldo Carvalho de Meloc2a70652011-06-02 11:04:54 -0300733 return first->attr.sample_type;
734}
735
Arnaldo Carvalho de Melo0c21f732012-08-14 16:42:15 -0300736u16 perf_evlist__id_hdr_size(struct perf_evlist *evlist)
Arnaldo Carvalho de Melo81e36bf2011-11-11 22:28:50 -0200737{
Arnaldo Carvalho de Melo0c21f732012-08-14 16:42:15 -0300738 struct perf_evsel *first = perf_evlist__first(evlist);
Arnaldo Carvalho de Melo81e36bf2011-11-11 22:28:50 -0200739 struct perf_sample *data;
740 u64 sample_type;
741 u16 size = 0;
742
Arnaldo Carvalho de Melo81e36bf2011-11-11 22:28:50 -0200743 if (!first->attr.sample_id_all)
744 goto out;
745
746 sample_type = first->attr.sample_type;
747
748 if (sample_type & PERF_SAMPLE_TID)
749 size += sizeof(data->tid) * 2;
750
751 if (sample_type & PERF_SAMPLE_TIME)
752 size += sizeof(data->time);
753
754 if (sample_type & PERF_SAMPLE_ID)
755 size += sizeof(data->id);
756
757 if (sample_type & PERF_SAMPLE_STREAM_ID)
758 size += sizeof(data->stream_id);
759
760 if (sample_type & PERF_SAMPLE_CPU)
761 size += sizeof(data->cpu) * 2;
762out:
763 return size;
764}
765
Arnaldo Carvalho de Melo0c21f732012-08-14 16:42:15 -0300766bool perf_evlist__valid_sample_id_all(struct perf_evlist *evlist)
Arnaldo Carvalho de Meloc2a70652011-06-02 11:04:54 -0300767{
Arnaldo Carvalho de Melo0c21f732012-08-14 16:42:15 -0300768 struct perf_evsel *first = perf_evlist__first(evlist), *pos = first;
Arnaldo Carvalho de Meloc2a70652011-06-02 11:04:54 -0300769
770 list_for_each_entry_continue(pos, &evlist->entries, node) {
771 if (first->attr.sample_id_all != pos->attr.sample_id_all)
772 return false;
773 }
774
775 return true;
Frederic Weisbecker74429962011-05-21 17:49:00 +0200776}
777
Arnaldo Carvalho de Melo0c21f732012-08-14 16:42:15 -0300778bool perf_evlist__sample_id_all(struct perf_evlist *evlist)
Frederic Weisbecker74429962011-05-21 17:49:00 +0200779{
Arnaldo Carvalho de Melo0c21f732012-08-14 16:42:15 -0300780 struct perf_evsel *first = perf_evlist__first(evlist);
Arnaldo Carvalho de Meloc2a70652011-06-02 11:04:54 -0300781 return first->attr.sample_id_all;
Frederic Weisbecker74429962011-05-21 17:49:00 +0200782}
Arnaldo Carvalho de Melo81cce8d2011-10-05 19:11:32 -0300783
784void perf_evlist__set_selected(struct perf_evlist *evlist,
785 struct perf_evsel *evsel)
786{
787 evlist->selected = evsel;
788}
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -0200789
Jiri Olsa6a4bb042012-08-08 12:22:36 +0200790int perf_evlist__open(struct perf_evlist *evlist)
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -0200791{
Jiri Olsa6a4bb042012-08-08 12:22:36 +0200792 struct perf_evsel *evsel;
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -0200793 int err, ncpus, nthreads;
794
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -0200795 list_for_each_entry(evsel, &evlist->entries, node) {
Jiri Olsa6a4bb042012-08-08 12:22:36 +0200796 err = perf_evsel__open(evsel, evlist->cpus, evlist->threads);
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -0200797 if (err < 0)
798 goto out_err;
799 }
800
801 return 0;
802out_err:
803 ncpus = evlist->cpus ? evlist->cpus->nr : 1;
804 nthreads = evlist->threads ? evlist->threads->nr : 1;
805
806 list_for_each_entry_reverse(evsel, &evlist->entries, node)
807 perf_evsel__close(evsel, ncpus, nthreads);
808
Namhyung Kim41c21a62012-02-23 12:13:36 +0900809 errno = -err;
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -0200810 return err;
811}
Arnaldo Carvalho de Melo35b9d882011-11-09 08:47:15 -0200812
813int perf_evlist__prepare_workload(struct perf_evlist *evlist,
814 struct perf_record_opts *opts,
815 const char *argv[])
816{
817 int child_ready_pipe[2], go_pipe[2];
818 char bf;
819
820 if (pipe(child_ready_pipe) < 0) {
821 perror("failed to create 'ready' pipe");
822 return -1;
823 }
824
825 if (pipe(go_pipe) < 0) {
826 perror("failed to create 'go' pipe");
827 goto out_close_ready_pipe;
828 }
829
830 evlist->workload.pid = fork();
831 if (evlist->workload.pid < 0) {
832 perror("failed to fork");
833 goto out_close_pipes;
834 }
835
836 if (!evlist->workload.pid) {
837 if (opts->pipe_output)
838 dup2(2, 1);
839
840 close(child_ready_pipe[0]);
841 close(go_pipe[1]);
842 fcntl(go_pipe[0], F_SETFD, FD_CLOEXEC);
843
844 /*
845 * Do a dummy execvp to get the PLT entry resolved,
846 * so we avoid the resolver overhead on the real
847 * execvp call.
848 */
849 execvp("", (char **)argv);
850
851 /*
852 * Tell the parent we're ready to go
853 */
854 close(child_ready_pipe[1]);
855
856 /*
857 * Wait until the parent tells us to go.
858 */
859 if (read(go_pipe[0], &bf, 1) == -1)
860 perror("unable to read pipe");
861
862 execvp(argv[0], (char **)argv);
863
864 perror(argv[0]);
865 kill(getppid(), SIGUSR1);
866 exit(-1);
867 }
868
Namhyung Kimd67356e2012-05-07 14:09:03 +0900869 if (perf_target__none(&opts->target))
Arnaldo Carvalho de Melo35b9d882011-11-09 08:47:15 -0200870 evlist->threads->map[0] = evlist->workload.pid;
871
872 close(child_ready_pipe[1]);
873 close(go_pipe[0]);
874 /*
875 * wait for child to settle
876 */
877 if (read(child_ready_pipe[0], &bf, 1) == -1) {
878 perror("unable to read pipe");
879 goto out_close_pipes;
880 }
881
882 evlist->workload.cork_fd = go_pipe[1];
883 close(child_ready_pipe[0]);
884 return 0;
885
886out_close_pipes:
887 close(go_pipe[0]);
888 close(go_pipe[1]);
889out_close_ready_pipe:
890 close(child_ready_pipe[0]);
891 close(child_ready_pipe[1]);
892 return -1;
893}
894
895int perf_evlist__start_workload(struct perf_evlist *evlist)
896{
897 if (evlist->workload.cork_fd > 0) {
898 /*
899 * Remove the cork, let it rip!
900 */
901 return close(evlist->workload.cork_fd);
902 }
903
904 return 0;
905}
Arnaldo Carvalho de Melocb0b29e2012-08-02 11:42:57 -0300906
Arnaldo Carvalho de Meloa3f698f2012-08-02 12:23:46 -0300907int perf_evlist__parse_sample(struct perf_evlist *evlist, union perf_event *event,
Arnaldo Carvalho de Melo0807d2d2012-09-26 12:48:18 -0300908 struct perf_sample *sample)
Arnaldo Carvalho de Melocb0b29e2012-08-02 11:42:57 -0300909{
Arnaldo Carvalho de Melo0c21f732012-08-14 16:42:15 -0300910 struct perf_evsel *evsel = perf_evlist__first(evlist);
Arnaldo Carvalho de Melo0807d2d2012-09-26 12:48:18 -0300911 return perf_evsel__parse_sample(evsel, event, sample);
Arnaldo Carvalho de Melocb0b29e2012-08-02 11:42:57 -0300912}
Arnaldo Carvalho de Melo78f067b2012-09-06 14:54:11 -0300913
914size_t perf_evlist__fprintf(struct perf_evlist *evlist, FILE *fp)
915{
916 struct perf_evsel *evsel;
917 size_t printed = 0;
918
919 list_for_each_entry(evsel, &evlist->entries, node) {
920 printed += fprintf(fp, "%s%s", evsel->idx ? ", " : "",
921 perf_evsel__name(evsel));
922 }
923
924 return printed + fprintf(fp, "\n");;
925}