blob: c3ec7878985baa9cf84058a39bb9a7d375afded5 [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 Melo4152ab32011-07-25 11:06:19 -0300288void perf_evlist__disable(struct perf_evlist *evlist)
289{
290 int cpu, thread;
291 struct perf_evsel *pos;
292
293 for (cpu = 0; cpu < evlist->cpus->nr; cpu++) {
294 list_for_each_entry(pos, &evlist->entries, node) {
295 for (thread = 0; thread < evlist->threads->nr; thread++)
Namhyung Kim55da8002012-05-31 14:51:46 +0900296 ioctl(FD(pos, cpu, thread),
297 PERF_EVENT_IOC_DISABLE, 0);
Arnaldo Carvalho de Melo4152ab32011-07-25 11:06:19 -0300298 }
299 }
300}
301
David Ahern764e16a32011-08-25 10:17:55 -0600302void perf_evlist__enable(struct perf_evlist *evlist)
303{
304 int cpu, thread;
305 struct perf_evsel *pos;
306
Arnaldo Carvalho de Meloa14bb7a2012-09-26 12:41:14 -0300307 for (cpu = 0; cpu < cpu_map__nr(evlist->cpus); cpu++) {
David Ahern764e16a32011-08-25 10:17:55 -0600308 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_ENABLE, 0);
David Ahern764e16a32011-08-25 10:17:55 -0600312 }
313 }
314}
315
Arnaldo Carvalho de Melo806fb632011-11-29 08:05:52 -0200316static int perf_evlist__alloc_pollfd(struct perf_evlist *evlist)
Arnaldo Carvalho de Melo5c581042011-01-11 22:30:02 -0200317{
Arnaldo Carvalho de Meloa14bb7a2012-09-26 12:41:14 -0300318 int nfds = cpu_map__nr(evlist->cpus) * evlist->threads->nr * evlist->nr_entries;
Arnaldo Carvalho de Melo5c581042011-01-11 22:30:02 -0200319 evlist->pollfd = malloc(sizeof(struct pollfd) * nfds);
320 return evlist->pollfd != NULL ? 0 : -ENOMEM;
321}
Arnaldo Carvalho de Melo70082dd2011-01-12 17:03:24 -0200322
323void perf_evlist__add_pollfd(struct perf_evlist *evlist, int fd)
324{
325 fcntl(fd, F_SETFL, O_NONBLOCK);
326 evlist->pollfd[evlist->nr_fds].fd = fd;
327 evlist->pollfd[evlist->nr_fds].events = POLLIN;
328 evlist->nr_fds++;
329}
Arnaldo Carvalho de Melo70db7532011-01-12 22:39:13 -0200330
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -0300331static void perf_evlist__id_hash(struct perf_evlist *evlist,
332 struct perf_evsel *evsel,
333 int cpu, int thread, u64 id)
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200334{
Arnaldo Carvalho de Melo3d3b5e92011-03-04 22:29:39 -0300335 int hash;
336 struct perf_sample_id *sid = SID(evsel, cpu, thread);
337
338 sid->id = id;
339 sid->evsel = evsel;
340 hash = hash_64(sid->id, PERF_EVLIST__HLIST_BITS);
341 hlist_add_head(&sid->node, &evlist->heads[hash]);
342}
343
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -0300344void perf_evlist__id_add(struct perf_evlist *evlist, struct perf_evsel *evsel,
345 int cpu, int thread, u64 id)
346{
347 perf_evlist__id_hash(evlist, evsel, cpu, thread, id);
348 evsel->id[evsel->ids++] = id;
349}
350
351static int perf_evlist__id_add_fd(struct perf_evlist *evlist,
352 struct perf_evsel *evsel,
353 int cpu, int thread, int fd)
Arnaldo Carvalho de Melo3d3b5e92011-03-04 22:29:39 -0300354{
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200355 u64 read_data[4] = { 0, };
Arnaldo Carvalho de Melo3d3b5e92011-03-04 22:29:39 -0300356 int id_idx = 1; /* The first entry is the counter value */
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200357
358 if (!(evsel->attr.read_format & PERF_FORMAT_ID) ||
359 read(fd, &read_data, sizeof(read_data)) == -1)
360 return -1;
361
362 if (evsel->attr.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
363 ++id_idx;
364 if (evsel->attr.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
365 ++id_idx;
366
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -0300367 perf_evlist__id_add(evlist, evsel, cpu, thread, read_data[id_idx]);
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200368 return 0;
369}
370
Arnaldo Carvalho de Melo70db7532011-01-12 22:39:13 -0200371struct perf_evsel *perf_evlist__id2evsel(struct perf_evlist *evlist, u64 id)
372{
373 struct hlist_head *head;
374 struct hlist_node *pos;
375 struct perf_sample_id *sid;
376 int hash;
377
378 if (evlist->nr_entries == 1)
Arnaldo Carvalho de Melo0c21f732012-08-14 16:42:15 -0300379 return perf_evlist__first(evlist);
Arnaldo Carvalho de Melo70db7532011-01-12 22:39:13 -0200380
381 hash = hash_64(id, PERF_EVLIST__HLIST_BITS);
382 head = &evlist->heads[hash];
383
384 hlist_for_each_entry(sid, pos, head, node)
385 if (sid->id == id)
386 return sid->evsel;
Namhyung Kim30e68bc2012-02-20 10:47:26 +0900387
388 if (!perf_evlist__sample_id_all(evlist))
Arnaldo Carvalho de Melo0c21f732012-08-14 16:42:15 -0300389 return perf_evlist__first(evlist);
Namhyung Kim30e68bc2012-02-20 10:47:26 +0900390
Arnaldo Carvalho de Melo70db7532011-01-12 22:39:13 -0200391 return NULL;
392}
Arnaldo Carvalho de Melo04391de2011-01-15 10:40:59 -0200393
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -0300394union perf_event *perf_evlist__mmap_read(struct perf_evlist *evlist, int idx)
Arnaldo Carvalho de Melo04391de2011-01-15 10:40:59 -0200395{
396 /* XXX Move this to perf.c, making it generally available */
397 unsigned int page_size = sysconf(_SC_PAGE_SIZE);
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -0300398 struct perf_mmap *md = &evlist->mmap[idx];
Arnaldo Carvalho de Melo04391de2011-01-15 10:40:59 -0200399 unsigned int head = perf_mmap__read_head(md);
400 unsigned int old = md->prev;
401 unsigned char *data = md->base + page_size;
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200402 union perf_event *event = NULL;
Arnaldo Carvalho de Melo04391de2011-01-15 10:40:59 -0200403
Arnaldo Carvalho de Melo7bb41152011-01-29 09:08:13 -0200404 if (evlist->overwrite) {
Arnaldo Carvalho de Melo04391de2011-01-15 10:40:59 -0200405 /*
Arnaldo Carvalho de Melo7bb41152011-01-29 09:08:13 -0200406 * If we're further behind than half the buffer, there's a chance
407 * the writer will bite our tail and mess up the samples under us.
408 *
409 * If we somehow ended up ahead of the head, we got messed up.
410 *
411 * In either case, truncate and restart at head.
Arnaldo Carvalho de Melo04391de2011-01-15 10:40:59 -0200412 */
Arnaldo Carvalho de Melo7bb41152011-01-29 09:08:13 -0200413 int diff = head - old;
414 if (diff > md->mask / 2 || diff < 0) {
415 fprintf(stderr, "WARNING: failed to keep up with mmap data.\n");
416
417 /*
418 * head points to a known good entry, start there.
419 */
420 old = head;
421 }
Arnaldo Carvalho de Melo04391de2011-01-15 10:40:59 -0200422 }
423
424 if (old != head) {
425 size_t size;
426
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200427 event = (union perf_event *)&data[old & md->mask];
Arnaldo Carvalho de Melo04391de2011-01-15 10:40:59 -0200428 size = event->header.size;
429
430 /*
431 * Event straddles the mmap boundary -- header should always
432 * be inside due to u64 alignment of output.
433 */
434 if ((old & md->mask) + size != ((old + size) & md->mask)) {
435 unsigned int offset = old;
436 unsigned int len = min(sizeof(*event), size), cpy;
437 void *dst = &evlist->event_copy;
438
439 do {
440 cpy = min(md->mask + 1 - (offset & md->mask), len);
441 memcpy(dst, &data[offset & md->mask], cpy);
442 offset += cpy;
443 dst += cpy;
444 len -= cpy;
445 } while (len);
446
447 event = &evlist->event_copy;
448 }
449
450 old += size;
451 }
452
453 md->prev = old;
Arnaldo Carvalho de Melo7bb41152011-01-29 09:08:13 -0200454
455 if (!evlist->overwrite)
456 perf_mmap__write_tail(md, old);
457
Arnaldo Carvalho de Melo04391de2011-01-15 10:40:59 -0200458 return event;
459}
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200460
Arnaldo Carvalho de Melo7e2ed092011-01-30 11:59:43 -0200461void perf_evlist__munmap(struct perf_evlist *evlist)
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200462{
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -0300463 int i;
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200464
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -0300465 for (i = 0; i < evlist->nr_mmaps; i++) {
466 if (evlist->mmap[i].base != NULL) {
467 munmap(evlist->mmap[i].base, evlist->mmap_len);
468 evlist->mmap[i].base = NULL;
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200469 }
470 }
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -0300471
472 free(evlist->mmap);
473 evlist->mmap = NULL;
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200474}
475
Arnaldo Carvalho de Melo806fb632011-11-29 08:05:52 -0200476static int perf_evlist__alloc_mmap(struct perf_evlist *evlist)
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200477{
Arnaldo Carvalho de Meloa14bb7a2012-09-26 12:41:14 -0300478 evlist->nr_mmaps = cpu_map__nr(evlist->cpus);
479 if (cpu_map__all(evlist->cpus))
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -0300480 evlist->nr_mmaps = evlist->threads->nr;
481 evlist->mmap = zalloc(evlist->nr_mmaps * sizeof(struct perf_mmap));
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200482 return evlist->mmap != NULL ? 0 : -ENOMEM;
483}
484
Arnaldo Carvalho de Melobccdaba2011-06-02 10:39:43 -0300485static int __perf_evlist__mmap(struct perf_evlist *evlist,
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -0300486 int idx, int prot, int mask, int fd)
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200487{
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -0300488 evlist->mmap[idx].prev = 0;
489 evlist->mmap[idx].mask = mask;
490 evlist->mmap[idx].base = mmap(NULL, evlist->mmap_len, prot,
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200491 MAP_SHARED, fd, 0);
Nelson Elhage301b1952011-12-19 08:39:30 -0500492 if (evlist->mmap[idx].base == MAP_FAILED) {
493 evlist->mmap[idx].base = NULL;
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200494 return -1;
Nelson Elhage301b1952011-12-19 08:39:30 -0500495 }
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200496
497 perf_evlist__add_pollfd(evlist, fd);
498 return 0;
499}
500
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -0300501static int perf_evlist__mmap_per_cpu(struct perf_evlist *evlist, int prot, int mask)
502{
503 struct perf_evsel *evsel;
504 int cpu, thread;
505
506 for (cpu = 0; cpu < evlist->cpus->nr; cpu++) {
507 int output = -1;
508
509 for (thread = 0; thread < evlist->threads->nr; thread++) {
510 list_for_each_entry(evsel, &evlist->entries, node) {
511 int fd = FD(evsel, cpu, thread);
512
513 if (output == -1) {
514 output = fd;
Arnaldo Carvalho de Melobccdaba2011-06-02 10:39:43 -0300515 if (__perf_evlist__mmap(evlist, cpu,
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -0300516 prot, mask, output) < 0)
517 goto out_unmap;
518 } else {
519 if (ioctl(fd, PERF_EVENT_IOC_SET_OUTPUT, output) != 0)
520 goto out_unmap;
521 }
522
523 if ((evsel->attr.read_format & PERF_FORMAT_ID) &&
524 perf_evlist__id_add_fd(evlist, evsel, cpu, thread, fd) < 0)
525 goto out_unmap;
526 }
527 }
528 }
529
530 return 0;
531
532out_unmap:
533 for (cpu = 0; cpu < evlist->cpus->nr; cpu++) {
534 if (evlist->mmap[cpu].base != NULL) {
535 munmap(evlist->mmap[cpu].base, evlist->mmap_len);
536 evlist->mmap[cpu].base = NULL;
537 }
538 }
539 return -1;
540}
541
542static int perf_evlist__mmap_per_thread(struct perf_evlist *evlist, int prot, int mask)
543{
544 struct perf_evsel *evsel;
545 int thread;
546
547 for (thread = 0; thread < evlist->threads->nr; thread++) {
548 int output = -1;
549
550 list_for_each_entry(evsel, &evlist->entries, node) {
551 int fd = FD(evsel, 0, thread);
552
553 if (output == -1) {
554 output = fd;
Arnaldo Carvalho de Melobccdaba2011-06-02 10:39:43 -0300555 if (__perf_evlist__mmap(evlist, thread,
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -0300556 prot, mask, output) < 0)
557 goto out_unmap;
558 } else {
559 if (ioctl(fd, PERF_EVENT_IOC_SET_OUTPUT, output) != 0)
560 goto out_unmap;
561 }
562
563 if ((evsel->attr.read_format & PERF_FORMAT_ID) &&
564 perf_evlist__id_add_fd(evlist, evsel, 0, thread, fd) < 0)
565 goto out_unmap;
566 }
567 }
568
569 return 0;
570
571out_unmap:
572 for (thread = 0; thread < evlist->threads->nr; thread++) {
573 if (evlist->mmap[thread].base != NULL) {
574 munmap(evlist->mmap[thread].base, evlist->mmap_len);
575 evlist->mmap[thread].base = NULL;
576 }
577 }
578 return -1;
579}
580
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200581/** perf_evlist__mmap - Create per cpu maps to receive events
582 *
583 * @evlist - list of events
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200584 * @pages - map length in pages
585 * @overwrite - overwrite older events?
586 *
587 * If overwrite is false the user needs to signal event consuption using:
588 *
589 * struct perf_mmap *m = &evlist->mmap[cpu];
590 * unsigned int head = perf_mmap__read_head(m);
591 *
592 * perf_mmap__write_tail(m, head)
Arnaldo Carvalho de Melo7e2ed092011-01-30 11:59:43 -0200593 *
594 * Using perf_evlist__read_on_cpu does this automatically.
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200595 */
Arnaldo Carvalho de Melo50a682c2011-11-09 09:10:47 -0200596int perf_evlist__mmap(struct perf_evlist *evlist, unsigned int pages,
597 bool overwrite)
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200598{
599 unsigned int page_size = sysconf(_SC_PAGE_SIZE);
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -0300600 struct perf_evsel *evsel;
Arnaldo Carvalho de Melo7e2ed092011-01-30 11:59:43 -0200601 const struct cpu_map *cpus = evlist->cpus;
602 const struct thread_map *threads = evlist->threads;
Arnaldo Carvalho de Melo50a682c2011-11-09 09:10:47 -0200603 int prot = PROT_READ | (overwrite ? 0 : PROT_WRITE), mask;
604
605 /* 512 kiB: default amount of unprivileged mlocked memory */
606 if (pages == UINT_MAX)
607 pages = (512 * 1024) / page_size;
Nelson Elhage41d0d932011-12-19 08:39:32 -0500608 else if (!is_power_of_2(pages))
609 return -EINVAL;
Arnaldo Carvalho de Melo50a682c2011-11-09 09:10:47 -0200610
611 mask = pages * page_size - 1;
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200612
Arnaldo Carvalho de Melo7e2ed092011-01-30 11:59:43 -0200613 if (evlist->mmap == NULL && perf_evlist__alloc_mmap(evlist) < 0)
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200614 return -ENOMEM;
615
Arnaldo Carvalho de Melo7e2ed092011-01-30 11:59:43 -0200616 if (evlist->pollfd == NULL && perf_evlist__alloc_pollfd(evlist) < 0)
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200617 return -ENOMEM;
618
619 evlist->overwrite = overwrite;
620 evlist->mmap_len = (pages + 1) * page_size;
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200621
622 list_for_each_entry(evsel, &evlist->entries, node) {
623 if ((evsel->attr.read_format & PERF_FORMAT_ID) &&
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -0300624 evsel->sample_id == NULL &&
Arnaldo Carvalho de Meloa14bb7a2012-09-26 12:41:14 -0300625 perf_evsel__alloc_id(evsel, cpu_map__nr(cpus), threads->nr) < 0)
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200626 return -ENOMEM;
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200627 }
628
Arnaldo Carvalho de Meloa14bb7a2012-09-26 12:41:14 -0300629 if (cpu_map__all(cpus))
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -0300630 return perf_evlist__mmap_per_thread(evlist, prot, mask);
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200631
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -0300632 return perf_evlist__mmap_per_cpu(evlist, prot, mask);
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200633}
Arnaldo Carvalho de Melo7e2ed092011-01-30 11:59:43 -0200634
Namhyung Kimb809ac12012-04-26 14:15:19 +0900635int perf_evlist__create_maps(struct perf_evlist *evlist,
636 struct perf_target *target)
Arnaldo Carvalho de Melo7e2ed092011-01-30 11:59:43 -0200637{
Namhyung Kimb809ac12012-04-26 14:15:19 +0900638 evlist->threads = thread_map__new_str(target->pid, target->tid,
639 target->uid);
Arnaldo Carvalho de Melo7e2ed092011-01-30 11:59:43 -0200640
641 if (evlist->threads == NULL)
642 return -1;
643
Namhyung Kim879d77d2012-05-16 18:45:48 +0900644 if (perf_target__has_task(target))
Namhyung Kimd67356e2012-05-07 14:09:03 +0900645 evlist->cpus = cpu_map__dummy_new();
Namhyung Kimd1cb9fc2012-05-16 18:45:49 +0900646 else if (!perf_target__has_cpu(target) && !target->uses_mmap)
647 evlist->cpus = cpu_map__dummy_new();
Namhyung Kim879d77d2012-05-16 18:45:48 +0900648 else
649 evlist->cpus = cpu_map__new(target->cpu_list);
Arnaldo Carvalho de Melo7e2ed092011-01-30 11:59:43 -0200650
651 if (evlist->cpus == NULL)
652 goto out_delete_threads;
653
654 return 0;
655
656out_delete_threads:
657 thread_map__delete(evlist->threads);
658 return -1;
659}
660
661void perf_evlist__delete_maps(struct perf_evlist *evlist)
662{
663 cpu_map__delete(evlist->cpus);
664 thread_map__delete(evlist->threads);
665 evlist->cpus = NULL;
666 evlist->threads = NULL;
667}
Frederic Weisbecker0a102472011-02-26 04:51:54 +0100668
Arnaldo Carvalho de Melo1491a632012-09-26 14:43:13 -0300669int perf_evlist__apply_filters(struct perf_evlist *evlist)
Frederic Weisbecker0a102472011-02-26 04:51:54 +0100670{
671 const struct thread_map *threads = evlist->threads;
672 const struct cpu_map *cpus = evlist->cpus;
673 struct perf_evsel *evsel;
674 char *filter;
675 int thread;
676 int cpu;
677 int err;
678 int fd;
679
680 list_for_each_entry(evsel, &evlist->entries, node) {
681 filter = evsel->filter;
682 if (!filter)
683 continue;
684 for (cpu = 0; cpu < cpus->nr; cpu++) {
685 for (thread = 0; thread < threads->nr; thread++) {
686 fd = FD(evsel, cpu, thread);
687 err = ioctl(fd, PERF_EVENT_IOC_SET_FILTER, filter);
688 if (err)
689 return err;
690 }
691 }
692 }
693
694 return 0;
695}
Frederic Weisbecker74429962011-05-21 17:49:00 +0200696
Arnaldo Carvalho de Melo0c21f732012-08-14 16:42:15 -0300697bool perf_evlist__valid_sample_type(struct perf_evlist *evlist)
Frederic Weisbecker74429962011-05-21 17:49:00 +0200698{
Arnaldo Carvalho de Melo0c21f732012-08-14 16:42:15 -0300699 struct perf_evsel *first = perf_evlist__first(evlist), *pos = first;
Arnaldo Carvalho de Meloc2a70652011-06-02 11:04:54 -0300700
701 list_for_each_entry_continue(pos, &evlist->entries, node) {
702 if (first->attr.sample_type != pos->attr.sample_type)
703 return false;
Frederic Weisbecker74429962011-05-21 17:49:00 +0200704 }
705
Arnaldo Carvalho de Meloc2a70652011-06-02 11:04:54 -0300706 return true;
707}
708
Arnaldo Carvalho de Melo0c21f732012-08-14 16:42:15 -0300709u64 perf_evlist__sample_type(struct perf_evlist *evlist)
Arnaldo Carvalho de Meloc2a70652011-06-02 11:04:54 -0300710{
Arnaldo Carvalho de Melo0c21f732012-08-14 16:42:15 -0300711 struct perf_evsel *first = perf_evlist__first(evlist);
Arnaldo Carvalho de Meloc2a70652011-06-02 11:04:54 -0300712 return first->attr.sample_type;
713}
714
Arnaldo Carvalho de Melo0c21f732012-08-14 16:42:15 -0300715u16 perf_evlist__id_hdr_size(struct perf_evlist *evlist)
Arnaldo Carvalho de Melo81e36bf2011-11-11 22:28:50 -0200716{
Arnaldo Carvalho de Melo0c21f732012-08-14 16:42:15 -0300717 struct perf_evsel *first = perf_evlist__first(evlist);
Arnaldo Carvalho de Melo81e36bf2011-11-11 22:28:50 -0200718 struct perf_sample *data;
719 u64 sample_type;
720 u16 size = 0;
721
Arnaldo Carvalho de Melo81e36bf2011-11-11 22:28:50 -0200722 if (!first->attr.sample_id_all)
723 goto out;
724
725 sample_type = first->attr.sample_type;
726
727 if (sample_type & PERF_SAMPLE_TID)
728 size += sizeof(data->tid) * 2;
729
730 if (sample_type & PERF_SAMPLE_TIME)
731 size += sizeof(data->time);
732
733 if (sample_type & PERF_SAMPLE_ID)
734 size += sizeof(data->id);
735
736 if (sample_type & PERF_SAMPLE_STREAM_ID)
737 size += sizeof(data->stream_id);
738
739 if (sample_type & PERF_SAMPLE_CPU)
740 size += sizeof(data->cpu) * 2;
741out:
742 return size;
743}
744
Arnaldo Carvalho de Melo0c21f732012-08-14 16:42:15 -0300745bool perf_evlist__valid_sample_id_all(struct perf_evlist *evlist)
Arnaldo Carvalho de Meloc2a70652011-06-02 11:04:54 -0300746{
Arnaldo Carvalho de Melo0c21f732012-08-14 16:42:15 -0300747 struct perf_evsel *first = perf_evlist__first(evlist), *pos = first;
Arnaldo Carvalho de Meloc2a70652011-06-02 11:04:54 -0300748
749 list_for_each_entry_continue(pos, &evlist->entries, node) {
750 if (first->attr.sample_id_all != pos->attr.sample_id_all)
751 return false;
752 }
753
754 return true;
Frederic Weisbecker74429962011-05-21 17:49:00 +0200755}
756
Arnaldo Carvalho de Melo0c21f732012-08-14 16:42:15 -0300757bool perf_evlist__sample_id_all(struct perf_evlist *evlist)
Frederic Weisbecker74429962011-05-21 17:49:00 +0200758{
Arnaldo Carvalho de Melo0c21f732012-08-14 16:42:15 -0300759 struct perf_evsel *first = perf_evlist__first(evlist);
Arnaldo Carvalho de Meloc2a70652011-06-02 11:04:54 -0300760 return first->attr.sample_id_all;
Frederic Weisbecker74429962011-05-21 17:49:00 +0200761}
Arnaldo Carvalho de Melo81cce8d2011-10-05 19:11:32 -0300762
763void perf_evlist__set_selected(struct perf_evlist *evlist,
764 struct perf_evsel *evsel)
765{
766 evlist->selected = evsel;
767}
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -0200768
Jiri Olsa6a4bb042012-08-08 12:22:36 +0200769int perf_evlist__open(struct perf_evlist *evlist)
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -0200770{
Jiri Olsa6a4bb042012-08-08 12:22:36 +0200771 struct perf_evsel *evsel;
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -0200772 int err, ncpus, nthreads;
773
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -0200774 list_for_each_entry(evsel, &evlist->entries, node) {
Jiri Olsa6a4bb042012-08-08 12:22:36 +0200775 err = perf_evsel__open(evsel, evlist->cpus, evlist->threads);
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -0200776 if (err < 0)
777 goto out_err;
778 }
779
780 return 0;
781out_err:
782 ncpus = evlist->cpus ? evlist->cpus->nr : 1;
783 nthreads = evlist->threads ? evlist->threads->nr : 1;
784
785 list_for_each_entry_reverse(evsel, &evlist->entries, node)
786 perf_evsel__close(evsel, ncpus, nthreads);
787
Namhyung Kim41c21a62012-02-23 12:13:36 +0900788 errno = -err;
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -0200789 return err;
790}
Arnaldo Carvalho de Melo35b9d882011-11-09 08:47:15 -0200791
792int perf_evlist__prepare_workload(struct perf_evlist *evlist,
793 struct perf_record_opts *opts,
794 const char *argv[])
795{
796 int child_ready_pipe[2], go_pipe[2];
797 char bf;
798
799 if (pipe(child_ready_pipe) < 0) {
800 perror("failed to create 'ready' pipe");
801 return -1;
802 }
803
804 if (pipe(go_pipe) < 0) {
805 perror("failed to create 'go' pipe");
806 goto out_close_ready_pipe;
807 }
808
809 evlist->workload.pid = fork();
810 if (evlist->workload.pid < 0) {
811 perror("failed to fork");
812 goto out_close_pipes;
813 }
814
815 if (!evlist->workload.pid) {
816 if (opts->pipe_output)
817 dup2(2, 1);
818
819 close(child_ready_pipe[0]);
820 close(go_pipe[1]);
821 fcntl(go_pipe[0], F_SETFD, FD_CLOEXEC);
822
823 /*
824 * Do a dummy execvp to get the PLT entry resolved,
825 * so we avoid the resolver overhead on the real
826 * execvp call.
827 */
828 execvp("", (char **)argv);
829
830 /*
831 * Tell the parent we're ready to go
832 */
833 close(child_ready_pipe[1]);
834
835 /*
836 * Wait until the parent tells us to go.
837 */
838 if (read(go_pipe[0], &bf, 1) == -1)
839 perror("unable to read pipe");
840
841 execvp(argv[0], (char **)argv);
842
843 perror(argv[0]);
844 kill(getppid(), SIGUSR1);
845 exit(-1);
846 }
847
Namhyung Kimd67356e2012-05-07 14:09:03 +0900848 if (perf_target__none(&opts->target))
Arnaldo Carvalho de Melo35b9d882011-11-09 08:47:15 -0200849 evlist->threads->map[0] = evlist->workload.pid;
850
851 close(child_ready_pipe[1]);
852 close(go_pipe[0]);
853 /*
854 * wait for child to settle
855 */
856 if (read(child_ready_pipe[0], &bf, 1) == -1) {
857 perror("unable to read pipe");
858 goto out_close_pipes;
859 }
860
861 evlist->workload.cork_fd = go_pipe[1];
862 close(child_ready_pipe[0]);
863 return 0;
864
865out_close_pipes:
866 close(go_pipe[0]);
867 close(go_pipe[1]);
868out_close_ready_pipe:
869 close(child_ready_pipe[0]);
870 close(child_ready_pipe[1]);
871 return -1;
872}
873
874int perf_evlist__start_workload(struct perf_evlist *evlist)
875{
876 if (evlist->workload.cork_fd > 0) {
877 /*
878 * Remove the cork, let it rip!
879 */
880 return close(evlist->workload.cork_fd);
881 }
882
883 return 0;
884}
Arnaldo Carvalho de Melocb0b29e2012-08-02 11:42:57 -0300885
Arnaldo Carvalho de Meloa3f698f2012-08-02 12:23:46 -0300886int perf_evlist__parse_sample(struct perf_evlist *evlist, union perf_event *event,
Arnaldo Carvalho de Melo0807d2d2012-09-26 12:48:18 -0300887 struct perf_sample *sample)
Arnaldo Carvalho de Melocb0b29e2012-08-02 11:42:57 -0300888{
Arnaldo Carvalho de Melo0c21f732012-08-14 16:42:15 -0300889 struct perf_evsel *evsel = perf_evlist__first(evlist);
Arnaldo Carvalho de Melo0807d2d2012-09-26 12:48:18 -0300890 return perf_evsel__parse_sample(evsel, event, sample);
Arnaldo Carvalho de Melocb0b29e2012-08-02 11:42:57 -0300891}
Arnaldo Carvalho de Melo78f067b2012-09-06 14:54:11 -0300892
893size_t perf_evlist__fprintf(struct perf_evlist *evlist, FILE *fp)
894{
895 struct perf_evsel *evsel;
896 size_t printed = 0;
897
898 list_for_each_entry(evsel, &evlist->entries, node) {
899 printed += fprintf(fp, "%s%s", evsel->idx ? ", " : "",
900 perf_evsel__name(evsel));
901 }
902
903 return printed + fprintf(fp, "\n");;
904}