blob: ae89686102f4a7224d0f425fb26ad3712f17c616 [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{
Frederic Weisbecker0a102472011-02-26 04:51:54 +0100671 struct perf_evsel *evsel;
Arnaldo Carvalho de Melo745cefc2012-09-26 15:07:39 -0300672 int err = 0;
673 const int ncpus = cpu_map__nr(evlist->cpus),
674 nthreads = evlist->threads->nr;
Frederic Weisbecker0a102472011-02-26 04:51:54 +0100675
676 list_for_each_entry(evsel, &evlist->entries, node) {
Arnaldo Carvalho de Melo745cefc2012-09-26 15:07:39 -0300677 if (evsel->filter == NULL)
Frederic Weisbecker0a102472011-02-26 04:51:54 +0100678 continue;
Arnaldo Carvalho de Melo745cefc2012-09-26 15:07:39 -0300679
680 err = perf_evsel__set_filter(evsel, ncpus, nthreads, evsel->filter);
681 if (err)
682 break;
Frederic Weisbecker0a102472011-02-26 04:51:54 +0100683 }
684
Arnaldo Carvalho de Melo745cefc2012-09-26 15:07:39 -0300685 return err;
686}
687
688int perf_evlist__set_filter(struct perf_evlist *evlist, const char *filter)
689{
690 struct perf_evsel *evsel;
691 int err = 0;
692 const int ncpus = cpu_map__nr(evlist->cpus),
693 nthreads = evlist->threads->nr;
694
695 list_for_each_entry(evsel, &evlist->entries, node) {
696 err = perf_evsel__set_filter(evsel, ncpus, nthreads, filter);
697 if (err)
698 break;
699 }
700
701 return err;
Frederic Weisbecker0a102472011-02-26 04:51:54 +0100702}
Frederic Weisbecker74429962011-05-21 17:49:00 +0200703
Arnaldo Carvalho de Melo0c21f732012-08-14 16:42:15 -0300704bool perf_evlist__valid_sample_type(struct perf_evlist *evlist)
Frederic Weisbecker74429962011-05-21 17:49:00 +0200705{
Arnaldo Carvalho de Melo0c21f732012-08-14 16:42:15 -0300706 struct perf_evsel *first = perf_evlist__first(evlist), *pos = first;
Arnaldo Carvalho de Meloc2a70652011-06-02 11:04:54 -0300707
708 list_for_each_entry_continue(pos, &evlist->entries, node) {
709 if (first->attr.sample_type != pos->attr.sample_type)
710 return false;
Frederic Weisbecker74429962011-05-21 17:49:00 +0200711 }
712
Arnaldo Carvalho de Meloc2a70652011-06-02 11:04:54 -0300713 return true;
714}
715
Arnaldo Carvalho de Melo0c21f732012-08-14 16:42:15 -0300716u64 perf_evlist__sample_type(struct perf_evlist *evlist)
Arnaldo Carvalho de Meloc2a70652011-06-02 11:04:54 -0300717{
Arnaldo Carvalho de Melo0c21f732012-08-14 16:42:15 -0300718 struct perf_evsel *first = perf_evlist__first(evlist);
Arnaldo Carvalho de Meloc2a70652011-06-02 11:04:54 -0300719 return first->attr.sample_type;
720}
721
Arnaldo Carvalho de Melo0c21f732012-08-14 16:42:15 -0300722u16 perf_evlist__id_hdr_size(struct perf_evlist *evlist)
Arnaldo Carvalho de Melo81e36bf2011-11-11 22:28:50 -0200723{
Arnaldo Carvalho de Melo0c21f732012-08-14 16:42:15 -0300724 struct perf_evsel *first = perf_evlist__first(evlist);
Arnaldo Carvalho de Melo81e36bf2011-11-11 22:28:50 -0200725 struct perf_sample *data;
726 u64 sample_type;
727 u16 size = 0;
728
Arnaldo Carvalho de Melo81e36bf2011-11-11 22:28:50 -0200729 if (!first->attr.sample_id_all)
730 goto out;
731
732 sample_type = first->attr.sample_type;
733
734 if (sample_type & PERF_SAMPLE_TID)
735 size += sizeof(data->tid) * 2;
736
737 if (sample_type & PERF_SAMPLE_TIME)
738 size += sizeof(data->time);
739
740 if (sample_type & PERF_SAMPLE_ID)
741 size += sizeof(data->id);
742
743 if (sample_type & PERF_SAMPLE_STREAM_ID)
744 size += sizeof(data->stream_id);
745
746 if (sample_type & PERF_SAMPLE_CPU)
747 size += sizeof(data->cpu) * 2;
748out:
749 return size;
750}
751
Arnaldo Carvalho de Melo0c21f732012-08-14 16:42:15 -0300752bool perf_evlist__valid_sample_id_all(struct perf_evlist *evlist)
Arnaldo Carvalho de Meloc2a70652011-06-02 11:04:54 -0300753{
Arnaldo Carvalho de Melo0c21f732012-08-14 16:42:15 -0300754 struct perf_evsel *first = perf_evlist__first(evlist), *pos = first;
Arnaldo Carvalho de Meloc2a70652011-06-02 11:04:54 -0300755
756 list_for_each_entry_continue(pos, &evlist->entries, node) {
757 if (first->attr.sample_id_all != pos->attr.sample_id_all)
758 return false;
759 }
760
761 return true;
Frederic Weisbecker74429962011-05-21 17:49:00 +0200762}
763
Arnaldo Carvalho de Melo0c21f732012-08-14 16:42:15 -0300764bool perf_evlist__sample_id_all(struct perf_evlist *evlist)
Frederic Weisbecker74429962011-05-21 17:49:00 +0200765{
Arnaldo Carvalho de Melo0c21f732012-08-14 16:42:15 -0300766 struct perf_evsel *first = perf_evlist__first(evlist);
Arnaldo Carvalho de Meloc2a70652011-06-02 11:04:54 -0300767 return first->attr.sample_id_all;
Frederic Weisbecker74429962011-05-21 17:49:00 +0200768}
Arnaldo Carvalho de Melo81cce8d2011-10-05 19:11:32 -0300769
770void perf_evlist__set_selected(struct perf_evlist *evlist,
771 struct perf_evsel *evsel)
772{
773 evlist->selected = evsel;
774}
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -0200775
Jiri Olsa6a4bb042012-08-08 12:22:36 +0200776int perf_evlist__open(struct perf_evlist *evlist)
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -0200777{
Jiri Olsa6a4bb042012-08-08 12:22:36 +0200778 struct perf_evsel *evsel;
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -0200779 int err, ncpus, nthreads;
780
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -0200781 list_for_each_entry(evsel, &evlist->entries, node) {
Jiri Olsa6a4bb042012-08-08 12:22:36 +0200782 err = perf_evsel__open(evsel, evlist->cpus, evlist->threads);
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -0200783 if (err < 0)
784 goto out_err;
785 }
786
787 return 0;
788out_err:
789 ncpus = evlist->cpus ? evlist->cpus->nr : 1;
790 nthreads = evlist->threads ? evlist->threads->nr : 1;
791
792 list_for_each_entry_reverse(evsel, &evlist->entries, node)
793 perf_evsel__close(evsel, ncpus, nthreads);
794
Namhyung Kim41c21a62012-02-23 12:13:36 +0900795 errno = -err;
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -0200796 return err;
797}
Arnaldo Carvalho de Melo35b9d882011-11-09 08:47:15 -0200798
799int perf_evlist__prepare_workload(struct perf_evlist *evlist,
800 struct perf_record_opts *opts,
801 const char *argv[])
802{
803 int child_ready_pipe[2], go_pipe[2];
804 char bf;
805
806 if (pipe(child_ready_pipe) < 0) {
807 perror("failed to create 'ready' pipe");
808 return -1;
809 }
810
811 if (pipe(go_pipe) < 0) {
812 perror("failed to create 'go' pipe");
813 goto out_close_ready_pipe;
814 }
815
816 evlist->workload.pid = fork();
817 if (evlist->workload.pid < 0) {
818 perror("failed to fork");
819 goto out_close_pipes;
820 }
821
822 if (!evlist->workload.pid) {
823 if (opts->pipe_output)
824 dup2(2, 1);
825
826 close(child_ready_pipe[0]);
827 close(go_pipe[1]);
828 fcntl(go_pipe[0], F_SETFD, FD_CLOEXEC);
829
830 /*
831 * Do a dummy execvp to get the PLT entry resolved,
832 * so we avoid the resolver overhead on the real
833 * execvp call.
834 */
835 execvp("", (char **)argv);
836
837 /*
838 * Tell the parent we're ready to go
839 */
840 close(child_ready_pipe[1]);
841
842 /*
843 * Wait until the parent tells us to go.
844 */
845 if (read(go_pipe[0], &bf, 1) == -1)
846 perror("unable to read pipe");
847
848 execvp(argv[0], (char **)argv);
849
850 perror(argv[0]);
851 kill(getppid(), SIGUSR1);
852 exit(-1);
853 }
854
Namhyung Kimd67356e2012-05-07 14:09:03 +0900855 if (perf_target__none(&opts->target))
Arnaldo Carvalho de Melo35b9d882011-11-09 08:47:15 -0200856 evlist->threads->map[0] = evlist->workload.pid;
857
858 close(child_ready_pipe[1]);
859 close(go_pipe[0]);
860 /*
861 * wait for child to settle
862 */
863 if (read(child_ready_pipe[0], &bf, 1) == -1) {
864 perror("unable to read pipe");
865 goto out_close_pipes;
866 }
867
868 evlist->workload.cork_fd = go_pipe[1];
869 close(child_ready_pipe[0]);
870 return 0;
871
872out_close_pipes:
873 close(go_pipe[0]);
874 close(go_pipe[1]);
875out_close_ready_pipe:
876 close(child_ready_pipe[0]);
877 close(child_ready_pipe[1]);
878 return -1;
879}
880
881int perf_evlist__start_workload(struct perf_evlist *evlist)
882{
883 if (evlist->workload.cork_fd > 0) {
884 /*
885 * Remove the cork, let it rip!
886 */
887 return close(evlist->workload.cork_fd);
888 }
889
890 return 0;
891}
Arnaldo Carvalho de Melocb0b29e2012-08-02 11:42:57 -0300892
Arnaldo Carvalho de Meloa3f698f2012-08-02 12:23:46 -0300893int perf_evlist__parse_sample(struct perf_evlist *evlist, union perf_event *event,
Arnaldo Carvalho de Melo0807d2d2012-09-26 12:48:18 -0300894 struct perf_sample *sample)
Arnaldo Carvalho de Melocb0b29e2012-08-02 11:42:57 -0300895{
Arnaldo Carvalho de Melo0c21f732012-08-14 16:42:15 -0300896 struct perf_evsel *evsel = perf_evlist__first(evlist);
Arnaldo Carvalho de Melo0807d2d2012-09-26 12:48:18 -0300897 return perf_evsel__parse_sample(evsel, event, sample);
Arnaldo Carvalho de Melocb0b29e2012-08-02 11:42:57 -0300898}
Arnaldo Carvalho de Melo78f067b2012-09-06 14:54:11 -0300899
900size_t perf_evlist__fprintf(struct perf_evlist *evlist, FILE *fp)
901{
902 struct perf_evsel *evsel;
903 size_t printed = 0;
904
905 list_for_each_entry(evsel, &evlist->entries, node) {
906 printed += fprintf(fp, "%s%s", evsel->idx ? ", " : "",
907 perf_evsel__name(evsel));
908 }
909
910 return printed + fprintf(fp, "\n");;
911}