blob: eddd5ebcd69018811a62ec67a7911a9701533bb8 [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 Melof77a9512012-12-10 16:41:31 -030052void perf_evlist__config(struct perf_evlist *evlist,
53 struct perf_record_opts *opts)
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -020054{
Jiri Olsacac21422012-11-12 18:34:00 +010055 struct perf_evsel *evsel;
Arnaldo Carvalho de Melof77a9512012-12-10 16:41:31 -030056 /*
57 * Set the evsel leader links before we configure attributes,
58 * since some might depend on this info.
59 */
60 if (opts->group)
61 perf_evlist__set_leader(evlist);
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -020062
63 if (evlist->cpus->map[0] < 0)
64 opts->no_inherit = true;
65
66 list_for_each_entry(evsel, &evlist->entries, node) {
Jiri Olsacac21422012-11-12 18:34:00 +010067 perf_evsel__config(evsel, opts);
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -020068
69 if (evlist->nr_entries > 1)
Arnaldo Carvalho de Melo3a5afae2012-12-10 15:29:12 -030070 perf_evsel__set_sample_id(evsel);
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -020071 }
72}
73
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -020074static void perf_evlist__purge(struct perf_evlist *evlist)
75{
76 struct perf_evsel *pos, *n;
77
78 list_for_each_entry_safe(pos, n, &evlist->entries, node) {
79 list_del_init(&pos->node);
80 perf_evsel__delete(pos);
81 }
82
83 evlist->nr_entries = 0;
84}
85
Arnaldo Carvalho de Meloef1d1af2011-01-18 21:41:45 -020086void perf_evlist__exit(struct perf_evlist *evlist)
87{
88 free(evlist->mmap);
89 free(evlist->pollfd);
90 evlist->mmap = NULL;
91 evlist->pollfd = NULL;
92}
93
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -020094void perf_evlist__delete(struct perf_evlist *evlist)
95{
96 perf_evlist__purge(evlist);
Arnaldo Carvalho de Meloef1d1af2011-01-18 21:41:45 -020097 perf_evlist__exit(evlist);
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -020098 free(evlist);
99}
100
101void perf_evlist__add(struct perf_evlist *evlist, struct perf_evsel *entry)
102{
103 list_add_tail(&entry->node, &evlist->entries);
104 ++evlist->nr_entries;
105}
106
Jiri Olsa0529bc12012-01-27 15:34:20 +0100107void perf_evlist__splice_list_tail(struct perf_evlist *evlist,
108 struct list_head *list,
109 int nr_entries)
Arnaldo Carvalho de Melo50d08e42011-11-04 09:10:59 -0200110{
111 list_splice_tail(list, &evlist->entries);
112 evlist->nr_entries += nr_entries;
113}
114
Arnaldo Carvalho de Melo63dab222012-08-14 16:35:48 -0300115void __perf_evlist__set_leader(struct list_head *list)
116{
117 struct perf_evsel *evsel, *leader;
118
119 leader = list_entry(list->next, struct perf_evsel, node);
Namhyung Kim97f63e42013-01-22 18:09:29 +0900120 evsel = list_entry(list->prev, struct perf_evsel, node);
121
122 leader->nr_members = evsel->idx - leader->idx + 1;
Arnaldo Carvalho de Melo63dab222012-08-14 16:35:48 -0300123
124 list_for_each_entry(evsel, list, node) {
125 if (evsel != leader)
126 evsel->leader = leader;
127 }
128}
129
130void perf_evlist__set_leader(struct perf_evlist *evlist)
Jiri Olsa6a4bb042012-08-08 12:22:36 +0200131{
Namhyung Kim97f63e42013-01-22 18:09:29 +0900132 if (evlist->nr_entries) {
133 evlist->nr_groups = evlist->nr_entries > 1 ? 1 : 0;
Arnaldo Carvalho de Melo63dab222012-08-14 16:35:48 -0300134 __perf_evlist__set_leader(&evlist->entries);
Namhyung Kim97f63e42013-01-22 18:09:29 +0900135 }
Jiri Olsa6a4bb042012-08-08 12:22:36 +0200136}
137
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -0200138int perf_evlist__add_default(struct perf_evlist *evlist)
139{
140 struct perf_event_attr attr = {
141 .type = PERF_TYPE_HARDWARE,
142 .config = PERF_COUNT_HW_CPU_CYCLES,
143 };
Joerg Roedel1aed2672012-01-04 17:54:20 +0100144 struct perf_evsel *evsel;
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -0200145
Joerg Roedel1aed2672012-01-04 17:54:20 +0100146 event_attr_init(&attr);
147
148 evsel = perf_evsel__new(&attr, 0);
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -0200149 if (evsel == NULL)
Stephane Eraniancc2d86b2011-06-07 18:19:36 +0200150 goto error;
151
152 /* use strdup() because free(evsel) assumes name is allocated */
153 evsel->name = strdup("cycles");
154 if (!evsel->name)
155 goto error_free;
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -0200156
157 perf_evlist__add(evlist, evsel);
158 return 0;
Stephane Eraniancc2d86b2011-06-07 18:19:36 +0200159error_free:
160 perf_evsel__delete(evsel);
161error:
162 return -ENOMEM;
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -0200163}
Arnaldo Carvalho de Melo5c581042011-01-11 22:30:02 -0200164
Arnaldo Carvalho de Meloe60fc842012-10-03 11:50:55 -0300165static int perf_evlist__add_attrs(struct perf_evlist *evlist,
166 struct perf_event_attr *attrs, size_t nr_attrs)
Arnaldo Carvalho de Melo50d08e42011-11-04 09:10:59 -0200167{
168 struct perf_evsel *evsel, *n;
169 LIST_HEAD(head);
170 size_t i;
171
172 for (i = 0; i < nr_attrs; i++) {
173 evsel = perf_evsel__new(attrs + i, evlist->nr_entries + i);
174 if (evsel == NULL)
175 goto out_delete_partial_list;
176 list_add_tail(&evsel->node, &head);
177 }
178
179 perf_evlist__splice_list_tail(evlist, &head, nr_attrs);
180
181 return 0;
182
183out_delete_partial_list:
184 list_for_each_entry_safe(evsel, n, &head, node)
185 perf_evsel__delete(evsel);
186 return -1;
187}
188
Arnaldo Carvalho de Melo79695e12012-05-30 13:53:54 -0300189int __perf_evlist__add_default_attrs(struct perf_evlist *evlist,
190 struct perf_event_attr *attrs, size_t nr_attrs)
191{
192 size_t i;
193
194 for (i = 0; i < nr_attrs; i++)
195 event_attr_init(attrs + i);
196
197 return perf_evlist__add_attrs(evlist, attrs, nr_attrs);
198}
199
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -0300200struct perf_evsel *
201perf_evlist__find_tracepoint_by_id(struct perf_evlist *evlist, int id)
Arnaldo Carvalho de Meloee29be62011-11-28 17:57:40 -0200202{
203 struct perf_evsel *evsel;
204
205 list_for_each_entry(evsel, &evlist->entries, node) {
206 if (evsel->attr.type == PERF_TYPE_TRACEPOINT &&
207 (int)evsel->attr.config == id)
208 return evsel;
209 }
210
211 return NULL;
212}
213
Arnaldo Carvalho de Melo39876e72012-10-03 11:40:22 -0300214int perf_evlist__add_newtp(struct perf_evlist *evlist,
215 const char *sys, const char *name, void *handler)
216{
217 struct perf_evsel *evsel;
218
219 evsel = perf_evsel__newtp(sys, name, evlist->nr_entries);
220 if (evsel == NULL)
221 return -1;
222
223 evsel->handler.func = handler;
224 perf_evlist__add(evlist, evsel);
225 return 0;
226}
227
Arnaldo Carvalho de Melo4152ab32011-07-25 11:06:19 -0300228void perf_evlist__disable(struct perf_evlist *evlist)
229{
230 int cpu, thread;
231 struct perf_evsel *pos;
232
233 for (cpu = 0; cpu < evlist->cpus->nr; cpu++) {
234 list_for_each_entry(pos, &evlist->entries, node) {
Namhyung Kim823254e2012-11-29 15:38:30 +0900235 if (!perf_evsel__is_group_leader(pos))
Jiri Olsa3fe4430d2012-11-12 18:34:03 +0100236 continue;
Arnaldo Carvalho de Melo4152ab32011-07-25 11:06:19 -0300237 for (thread = 0; thread < evlist->threads->nr; thread++)
Namhyung Kim55da8002012-05-31 14:51:46 +0900238 ioctl(FD(pos, cpu, thread),
239 PERF_EVENT_IOC_DISABLE, 0);
Arnaldo Carvalho de Melo4152ab32011-07-25 11:06:19 -0300240 }
241 }
242}
243
David Ahern764e16a32011-08-25 10:17:55 -0600244void perf_evlist__enable(struct perf_evlist *evlist)
245{
246 int cpu, thread;
247 struct perf_evsel *pos;
248
Arnaldo Carvalho de Meloa14bb7a2012-09-26 12:41:14 -0300249 for (cpu = 0; cpu < cpu_map__nr(evlist->cpus); cpu++) {
David Ahern764e16a32011-08-25 10:17:55 -0600250 list_for_each_entry(pos, &evlist->entries, node) {
Namhyung Kim823254e2012-11-29 15:38:30 +0900251 if (!perf_evsel__is_group_leader(pos))
Jiri Olsa3fe4430d2012-11-12 18:34:03 +0100252 continue;
David Ahern764e16a32011-08-25 10:17:55 -0600253 for (thread = 0; thread < evlist->threads->nr; thread++)
Namhyung Kim55da8002012-05-31 14:51:46 +0900254 ioctl(FD(pos, cpu, thread),
255 PERF_EVENT_IOC_ENABLE, 0);
David Ahern764e16a32011-08-25 10:17:55 -0600256 }
257 }
258}
259
Arnaldo Carvalho de Melo806fb632011-11-29 08:05:52 -0200260static int perf_evlist__alloc_pollfd(struct perf_evlist *evlist)
Arnaldo Carvalho de Melo5c581042011-01-11 22:30:02 -0200261{
Arnaldo Carvalho de Meloa14bb7a2012-09-26 12:41:14 -0300262 int nfds = cpu_map__nr(evlist->cpus) * evlist->threads->nr * evlist->nr_entries;
Arnaldo Carvalho de Melo5c581042011-01-11 22:30:02 -0200263 evlist->pollfd = malloc(sizeof(struct pollfd) * nfds);
264 return evlist->pollfd != NULL ? 0 : -ENOMEM;
265}
Arnaldo Carvalho de Melo70082dd2011-01-12 17:03:24 -0200266
267void perf_evlist__add_pollfd(struct perf_evlist *evlist, int fd)
268{
269 fcntl(fd, F_SETFL, O_NONBLOCK);
270 evlist->pollfd[evlist->nr_fds].fd = fd;
271 evlist->pollfd[evlist->nr_fds].events = POLLIN;
272 evlist->nr_fds++;
273}
Arnaldo Carvalho de Melo70db7532011-01-12 22:39:13 -0200274
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -0300275static void perf_evlist__id_hash(struct perf_evlist *evlist,
276 struct perf_evsel *evsel,
277 int cpu, int thread, u64 id)
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200278{
Arnaldo Carvalho de Melo3d3b5e92011-03-04 22:29:39 -0300279 int hash;
280 struct perf_sample_id *sid = SID(evsel, cpu, thread);
281
282 sid->id = id;
283 sid->evsel = evsel;
284 hash = hash_64(sid->id, PERF_EVLIST__HLIST_BITS);
285 hlist_add_head(&sid->node, &evlist->heads[hash]);
286}
287
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -0300288void perf_evlist__id_add(struct perf_evlist *evlist, struct perf_evsel *evsel,
289 int cpu, int thread, u64 id)
290{
291 perf_evlist__id_hash(evlist, evsel, cpu, thread, id);
292 evsel->id[evsel->ids++] = id;
293}
294
295static int perf_evlist__id_add_fd(struct perf_evlist *evlist,
296 struct perf_evsel *evsel,
297 int cpu, int thread, int fd)
Arnaldo Carvalho de Melo3d3b5e92011-03-04 22:29:39 -0300298{
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200299 u64 read_data[4] = { 0, };
Arnaldo Carvalho de Melo3d3b5e92011-03-04 22:29:39 -0300300 int id_idx = 1; /* The first entry is the counter value */
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200301
302 if (!(evsel->attr.read_format & PERF_FORMAT_ID) ||
303 read(fd, &read_data, sizeof(read_data)) == -1)
304 return -1;
305
306 if (evsel->attr.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
307 ++id_idx;
308 if (evsel->attr.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
309 ++id_idx;
310
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -0300311 perf_evlist__id_add(evlist, evsel, cpu, thread, read_data[id_idx]);
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200312 return 0;
313}
314
Arnaldo Carvalho de Melo70db7532011-01-12 22:39:13 -0200315struct perf_evsel *perf_evlist__id2evsel(struct perf_evlist *evlist, u64 id)
316{
317 struct hlist_head *head;
318 struct hlist_node *pos;
319 struct perf_sample_id *sid;
320 int hash;
321
322 if (evlist->nr_entries == 1)
Arnaldo Carvalho de Melo0c21f732012-08-14 16:42:15 -0300323 return perf_evlist__first(evlist);
Arnaldo Carvalho de Melo70db7532011-01-12 22:39:13 -0200324
325 hash = hash_64(id, PERF_EVLIST__HLIST_BITS);
326 head = &evlist->heads[hash];
327
328 hlist_for_each_entry(sid, pos, head, node)
329 if (sid->id == id)
330 return sid->evsel;
Namhyung Kim30e68bc2012-02-20 10:47:26 +0900331
332 if (!perf_evlist__sample_id_all(evlist))
Arnaldo Carvalho de Melo0c21f732012-08-14 16:42:15 -0300333 return perf_evlist__first(evlist);
Namhyung Kim30e68bc2012-02-20 10:47:26 +0900334
Arnaldo Carvalho de Melo70db7532011-01-12 22:39:13 -0200335 return NULL;
336}
Arnaldo Carvalho de Melo04391de2011-01-15 10:40:59 -0200337
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -0300338union perf_event *perf_evlist__mmap_read(struct perf_evlist *evlist, int idx)
Arnaldo Carvalho de Melo04391de2011-01-15 10:40:59 -0200339{
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -0300340 struct perf_mmap *md = &evlist->mmap[idx];
Arnaldo Carvalho de Melo04391de2011-01-15 10:40:59 -0200341 unsigned int head = perf_mmap__read_head(md);
342 unsigned int old = md->prev;
343 unsigned char *data = md->base + page_size;
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200344 union perf_event *event = NULL;
Arnaldo Carvalho de Melo04391de2011-01-15 10:40:59 -0200345
Arnaldo Carvalho de Melo7bb41152011-01-29 09:08:13 -0200346 if (evlist->overwrite) {
Arnaldo Carvalho de Melo04391de2011-01-15 10:40:59 -0200347 /*
Arnaldo Carvalho de Melo7bb41152011-01-29 09:08:13 -0200348 * If we're further behind than half the buffer, there's a chance
349 * the writer will bite our tail and mess up the samples under us.
350 *
351 * If we somehow ended up ahead of the head, we got messed up.
352 *
353 * In either case, truncate and restart at head.
Arnaldo Carvalho de Melo04391de2011-01-15 10:40:59 -0200354 */
Arnaldo Carvalho de Melo7bb41152011-01-29 09:08:13 -0200355 int diff = head - old;
356 if (diff > md->mask / 2 || diff < 0) {
357 fprintf(stderr, "WARNING: failed to keep up with mmap data.\n");
358
359 /*
360 * head points to a known good entry, start there.
361 */
362 old = head;
363 }
Arnaldo Carvalho de Melo04391de2011-01-15 10:40:59 -0200364 }
365
366 if (old != head) {
367 size_t size;
368
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200369 event = (union perf_event *)&data[old & md->mask];
Arnaldo Carvalho de Melo04391de2011-01-15 10:40:59 -0200370 size = event->header.size;
371
372 /*
373 * Event straddles the mmap boundary -- header should always
374 * be inside due to u64 alignment of output.
375 */
376 if ((old & md->mask) + size != ((old + size) & md->mask)) {
377 unsigned int offset = old;
378 unsigned int len = min(sizeof(*event), size), cpy;
379 void *dst = &evlist->event_copy;
380
381 do {
382 cpy = min(md->mask + 1 - (offset & md->mask), len);
383 memcpy(dst, &data[offset & md->mask], cpy);
384 offset += cpy;
385 dst += cpy;
386 len -= cpy;
387 } while (len);
388
389 event = &evlist->event_copy;
390 }
391
392 old += size;
393 }
394
395 md->prev = old;
Arnaldo Carvalho de Melo7bb41152011-01-29 09:08:13 -0200396
397 if (!evlist->overwrite)
398 perf_mmap__write_tail(md, old);
399
Arnaldo Carvalho de Melo04391de2011-01-15 10:40:59 -0200400 return event;
401}
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200402
Arnaldo Carvalho de Melo7e2ed092011-01-30 11:59:43 -0200403void perf_evlist__munmap(struct perf_evlist *evlist)
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200404{
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -0300405 int i;
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200406
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -0300407 for (i = 0; i < evlist->nr_mmaps; i++) {
408 if (evlist->mmap[i].base != NULL) {
409 munmap(evlist->mmap[i].base, evlist->mmap_len);
410 evlist->mmap[i].base = NULL;
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200411 }
412 }
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -0300413
414 free(evlist->mmap);
415 evlist->mmap = NULL;
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200416}
417
Arnaldo Carvalho de Melo806fb632011-11-29 08:05:52 -0200418static int perf_evlist__alloc_mmap(struct perf_evlist *evlist)
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200419{
Arnaldo Carvalho de Meloa14bb7a2012-09-26 12:41:14 -0300420 evlist->nr_mmaps = cpu_map__nr(evlist->cpus);
421 if (cpu_map__all(evlist->cpus))
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -0300422 evlist->nr_mmaps = evlist->threads->nr;
423 evlist->mmap = zalloc(evlist->nr_mmaps * sizeof(struct perf_mmap));
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200424 return evlist->mmap != NULL ? 0 : -ENOMEM;
425}
426
Arnaldo Carvalho de Melobccdaba2011-06-02 10:39:43 -0300427static int __perf_evlist__mmap(struct perf_evlist *evlist,
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -0300428 int idx, int prot, int mask, int fd)
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200429{
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -0300430 evlist->mmap[idx].prev = 0;
431 evlist->mmap[idx].mask = mask;
432 evlist->mmap[idx].base = mmap(NULL, evlist->mmap_len, prot,
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200433 MAP_SHARED, fd, 0);
Nelson Elhage301b1952011-12-19 08:39:30 -0500434 if (evlist->mmap[idx].base == MAP_FAILED) {
435 evlist->mmap[idx].base = NULL;
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200436 return -1;
Nelson Elhage301b1952011-12-19 08:39:30 -0500437 }
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200438
439 perf_evlist__add_pollfd(evlist, fd);
440 return 0;
441}
442
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -0300443static int perf_evlist__mmap_per_cpu(struct perf_evlist *evlist, int prot, int mask)
444{
445 struct perf_evsel *evsel;
446 int cpu, thread;
447
448 for (cpu = 0; cpu < evlist->cpus->nr; cpu++) {
449 int output = -1;
450
451 for (thread = 0; thread < evlist->threads->nr; thread++) {
452 list_for_each_entry(evsel, &evlist->entries, node) {
453 int fd = FD(evsel, cpu, thread);
454
455 if (output == -1) {
456 output = fd;
Arnaldo Carvalho de Melobccdaba2011-06-02 10:39:43 -0300457 if (__perf_evlist__mmap(evlist, cpu,
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -0300458 prot, mask, output) < 0)
459 goto out_unmap;
460 } else {
461 if (ioctl(fd, PERF_EVENT_IOC_SET_OUTPUT, output) != 0)
462 goto out_unmap;
463 }
464
465 if ((evsel->attr.read_format & PERF_FORMAT_ID) &&
466 perf_evlist__id_add_fd(evlist, evsel, cpu, thread, fd) < 0)
467 goto out_unmap;
468 }
469 }
470 }
471
472 return 0;
473
474out_unmap:
475 for (cpu = 0; cpu < evlist->cpus->nr; cpu++) {
476 if (evlist->mmap[cpu].base != NULL) {
477 munmap(evlist->mmap[cpu].base, evlist->mmap_len);
478 evlist->mmap[cpu].base = NULL;
479 }
480 }
481 return -1;
482}
483
484static int perf_evlist__mmap_per_thread(struct perf_evlist *evlist, int prot, int mask)
485{
486 struct perf_evsel *evsel;
487 int thread;
488
489 for (thread = 0; thread < evlist->threads->nr; thread++) {
490 int output = -1;
491
492 list_for_each_entry(evsel, &evlist->entries, node) {
493 int fd = FD(evsel, 0, thread);
494
495 if (output == -1) {
496 output = fd;
Arnaldo Carvalho de Melobccdaba2011-06-02 10:39:43 -0300497 if (__perf_evlist__mmap(evlist, thread,
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -0300498 prot, mask, output) < 0)
499 goto out_unmap;
500 } else {
501 if (ioctl(fd, PERF_EVENT_IOC_SET_OUTPUT, output) != 0)
502 goto out_unmap;
503 }
504
505 if ((evsel->attr.read_format & PERF_FORMAT_ID) &&
506 perf_evlist__id_add_fd(evlist, evsel, 0, thread, fd) < 0)
507 goto out_unmap;
508 }
509 }
510
511 return 0;
512
513out_unmap:
514 for (thread = 0; thread < evlist->threads->nr; thread++) {
515 if (evlist->mmap[thread].base != NULL) {
516 munmap(evlist->mmap[thread].base, evlist->mmap_len);
517 evlist->mmap[thread].base = NULL;
518 }
519 }
520 return -1;
521}
522
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200523/** perf_evlist__mmap - Create per cpu maps to receive events
524 *
525 * @evlist - list of events
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200526 * @pages - map length in pages
527 * @overwrite - overwrite older events?
528 *
529 * If overwrite is false the user needs to signal event consuption using:
530 *
531 * struct perf_mmap *m = &evlist->mmap[cpu];
532 * unsigned int head = perf_mmap__read_head(m);
533 *
534 * perf_mmap__write_tail(m, head)
Arnaldo Carvalho de Melo7e2ed092011-01-30 11:59:43 -0200535 *
536 * Using perf_evlist__read_on_cpu does this automatically.
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200537 */
Arnaldo Carvalho de Melo50a682c2011-11-09 09:10:47 -0200538int perf_evlist__mmap(struct perf_evlist *evlist, unsigned int pages,
539 bool overwrite)
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200540{
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -0300541 struct perf_evsel *evsel;
Arnaldo Carvalho de Melo7e2ed092011-01-30 11:59:43 -0200542 const struct cpu_map *cpus = evlist->cpus;
543 const struct thread_map *threads = evlist->threads;
Arnaldo Carvalho de Melo50a682c2011-11-09 09:10:47 -0200544 int prot = PROT_READ | (overwrite ? 0 : PROT_WRITE), mask;
545
546 /* 512 kiB: default amount of unprivileged mlocked memory */
547 if (pages == UINT_MAX)
548 pages = (512 * 1024) / page_size;
Nelson Elhage41d0d932011-12-19 08:39:32 -0500549 else if (!is_power_of_2(pages))
550 return -EINVAL;
Arnaldo Carvalho de Melo50a682c2011-11-09 09:10:47 -0200551
552 mask = pages * page_size - 1;
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200553
Arnaldo Carvalho de Melo7e2ed092011-01-30 11:59:43 -0200554 if (evlist->mmap == NULL && perf_evlist__alloc_mmap(evlist) < 0)
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200555 return -ENOMEM;
556
Arnaldo Carvalho de Melo7e2ed092011-01-30 11:59:43 -0200557 if (evlist->pollfd == NULL && perf_evlist__alloc_pollfd(evlist) < 0)
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200558 return -ENOMEM;
559
560 evlist->overwrite = overwrite;
561 evlist->mmap_len = (pages + 1) * page_size;
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200562
563 list_for_each_entry(evsel, &evlist->entries, node) {
564 if ((evsel->attr.read_format & PERF_FORMAT_ID) &&
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -0300565 evsel->sample_id == NULL &&
Arnaldo Carvalho de Meloa14bb7a2012-09-26 12:41:14 -0300566 perf_evsel__alloc_id(evsel, cpu_map__nr(cpus), threads->nr) < 0)
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200567 return -ENOMEM;
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200568 }
569
Arnaldo Carvalho de Meloa14bb7a2012-09-26 12:41:14 -0300570 if (cpu_map__all(cpus))
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -0300571 return perf_evlist__mmap_per_thread(evlist, prot, mask);
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200572
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -0300573 return perf_evlist__mmap_per_cpu(evlist, prot, mask);
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200574}
Arnaldo Carvalho de Melo7e2ed092011-01-30 11:59:43 -0200575
Namhyung Kimb809ac12012-04-26 14:15:19 +0900576int perf_evlist__create_maps(struct perf_evlist *evlist,
577 struct perf_target *target)
Arnaldo Carvalho de Melo7e2ed092011-01-30 11:59:43 -0200578{
Namhyung Kimb809ac12012-04-26 14:15:19 +0900579 evlist->threads = thread_map__new_str(target->pid, target->tid,
580 target->uid);
Arnaldo Carvalho de Melo7e2ed092011-01-30 11:59:43 -0200581
582 if (evlist->threads == NULL)
583 return -1;
584
Namhyung Kim879d77d2012-05-16 18:45:48 +0900585 if (perf_target__has_task(target))
Namhyung Kimd67356e2012-05-07 14:09:03 +0900586 evlist->cpus = cpu_map__dummy_new();
Namhyung Kimd1cb9fc2012-05-16 18:45:49 +0900587 else if (!perf_target__has_cpu(target) && !target->uses_mmap)
588 evlist->cpus = cpu_map__dummy_new();
Namhyung Kim879d77d2012-05-16 18:45:48 +0900589 else
590 evlist->cpus = cpu_map__new(target->cpu_list);
Arnaldo Carvalho de Melo7e2ed092011-01-30 11:59:43 -0200591
592 if (evlist->cpus == NULL)
593 goto out_delete_threads;
594
595 return 0;
596
597out_delete_threads:
598 thread_map__delete(evlist->threads);
599 return -1;
600}
601
602void perf_evlist__delete_maps(struct perf_evlist *evlist)
603{
604 cpu_map__delete(evlist->cpus);
605 thread_map__delete(evlist->threads);
606 evlist->cpus = NULL;
607 evlist->threads = NULL;
608}
Frederic Weisbecker0a102472011-02-26 04:51:54 +0100609
Arnaldo Carvalho de Melo1491a632012-09-26 14:43:13 -0300610int perf_evlist__apply_filters(struct perf_evlist *evlist)
Frederic Weisbecker0a102472011-02-26 04:51:54 +0100611{
Frederic Weisbecker0a102472011-02-26 04:51:54 +0100612 struct perf_evsel *evsel;
Arnaldo Carvalho de Melo745cefc2012-09-26 15:07:39 -0300613 int err = 0;
614 const int ncpus = cpu_map__nr(evlist->cpus),
615 nthreads = evlist->threads->nr;
Frederic Weisbecker0a102472011-02-26 04:51:54 +0100616
617 list_for_each_entry(evsel, &evlist->entries, node) {
Arnaldo Carvalho de Melo745cefc2012-09-26 15:07:39 -0300618 if (evsel->filter == NULL)
Frederic Weisbecker0a102472011-02-26 04:51:54 +0100619 continue;
Arnaldo Carvalho de Melo745cefc2012-09-26 15:07:39 -0300620
621 err = perf_evsel__set_filter(evsel, ncpus, nthreads, evsel->filter);
622 if (err)
623 break;
Frederic Weisbecker0a102472011-02-26 04:51:54 +0100624 }
625
Arnaldo Carvalho de Melo745cefc2012-09-26 15:07:39 -0300626 return err;
627}
628
629int perf_evlist__set_filter(struct perf_evlist *evlist, const char *filter)
630{
631 struct perf_evsel *evsel;
632 int err = 0;
633 const int ncpus = cpu_map__nr(evlist->cpus),
634 nthreads = evlist->threads->nr;
635
636 list_for_each_entry(evsel, &evlist->entries, node) {
637 err = perf_evsel__set_filter(evsel, ncpus, nthreads, filter);
638 if (err)
639 break;
640 }
641
642 return err;
Frederic Weisbecker0a102472011-02-26 04:51:54 +0100643}
Frederic Weisbecker74429962011-05-21 17:49:00 +0200644
Arnaldo Carvalho de Melo0c21f732012-08-14 16:42:15 -0300645bool perf_evlist__valid_sample_type(struct perf_evlist *evlist)
Frederic Weisbecker74429962011-05-21 17:49:00 +0200646{
Arnaldo Carvalho de Melo0c21f732012-08-14 16:42:15 -0300647 struct perf_evsel *first = perf_evlist__first(evlist), *pos = first;
Arnaldo Carvalho de Meloc2a70652011-06-02 11:04:54 -0300648
649 list_for_each_entry_continue(pos, &evlist->entries, node) {
650 if (first->attr.sample_type != pos->attr.sample_type)
651 return false;
Frederic Weisbecker74429962011-05-21 17:49:00 +0200652 }
653
Arnaldo Carvalho de Meloc2a70652011-06-02 11:04:54 -0300654 return true;
655}
656
Arnaldo Carvalho de Melo0c21f732012-08-14 16:42:15 -0300657u64 perf_evlist__sample_type(struct perf_evlist *evlist)
Arnaldo Carvalho de Meloc2a70652011-06-02 11:04:54 -0300658{
Arnaldo Carvalho de Melo0c21f732012-08-14 16:42:15 -0300659 struct perf_evsel *first = perf_evlist__first(evlist);
Arnaldo Carvalho de Meloc2a70652011-06-02 11:04:54 -0300660 return first->attr.sample_type;
661}
662
Arnaldo Carvalho de Melo0c21f732012-08-14 16:42:15 -0300663u16 perf_evlist__id_hdr_size(struct perf_evlist *evlist)
Arnaldo Carvalho de Melo81e36bf2011-11-11 22:28:50 -0200664{
Arnaldo Carvalho de Melo0c21f732012-08-14 16:42:15 -0300665 struct perf_evsel *first = perf_evlist__first(evlist);
Arnaldo Carvalho de Melo81e36bf2011-11-11 22:28:50 -0200666 struct perf_sample *data;
667 u64 sample_type;
668 u16 size = 0;
669
Arnaldo Carvalho de Melo81e36bf2011-11-11 22:28:50 -0200670 if (!first->attr.sample_id_all)
671 goto out;
672
673 sample_type = first->attr.sample_type;
674
675 if (sample_type & PERF_SAMPLE_TID)
676 size += sizeof(data->tid) * 2;
677
678 if (sample_type & PERF_SAMPLE_TIME)
679 size += sizeof(data->time);
680
681 if (sample_type & PERF_SAMPLE_ID)
682 size += sizeof(data->id);
683
684 if (sample_type & PERF_SAMPLE_STREAM_ID)
685 size += sizeof(data->stream_id);
686
687 if (sample_type & PERF_SAMPLE_CPU)
688 size += sizeof(data->cpu) * 2;
689out:
690 return size;
691}
692
Arnaldo Carvalho de Melo0c21f732012-08-14 16:42:15 -0300693bool perf_evlist__valid_sample_id_all(struct perf_evlist *evlist)
Arnaldo Carvalho de Meloc2a70652011-06-02 11:04:54 -0300694{
Arnaldo Carvalho de Melo0c21f732012-08-14 16:42:15 -0300695 struct perf_evsel *first = perf_evlist__first(evlist), *pos = first;
Arnaldo Carvalho de Meloc2a70652011-06-02 11:04:54 -0300696
697 list_for_each_entry_continue(pos, &evlist->entries, node) {
698 if (first->attr.sample_id_all != pos->attr.sample_id_all)
699 return false;
700 }
701
702 return true;
Frederic Weisbecker74429962011-05-21 17:49:00 +0200703}
704
Arnaldo Carvalho de Melo0c21f732012-08-14 16:42:15 -0300705bool perf_evlist__sample_id_all(struct perf_evlist *evlist)
Frederic Weisbecker74429962011-05-21 17:49:00 +0200706{
Arnaldo Carvalho de Melo0c21f732012-08-14 16:42:15 -0300707 struct perf_evsel *first = perf_evlist__first(evlist);
Arnaldo Carvalho de Meloc2a70652011-06-02 11:04:54 -0300708 return first->attr.sample_id_all;
Frederic Weisbecker74429962011-05-21 17:49:00 +0200709}
Arnaldo Carvalho de Melo81cce8d2011-10-05 19:11:32 -0300710
711void perf_evlist__set_selected(struct perf_evlist *evlist,
712 struct perf_evsel *evsel)
713{
714 evlist->selected = evsel;
715}
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -0200716
Jiri Olsa6a4bb042012-08-08 12:22:36 +0200717int perf_evlist__open(struct perf_evlist *evlist)
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -0200718{
Jiri Olsa6a4bb042012-08-08 12:22:36 +0200719 struct perf_evsel *evsel;
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -0200720 int err, ncpus, nthreads;
721
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -0200722 list_for_each_entry(evsel, &evlist->entries, node) {
Jiri Olsa6a4bb042012-08-08 12:22:36 +0200723 err = perf_evsel__open(evsel, evlist->cpus, evlist->threads);
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -0200724 if (err < 0)
725 goto out_err;
726 }
727
728 return 0;
729out_err:
730 ncpus = evlist->cpus ? evlist->cpus->nr : 1;
731 nthreads = evlist->threads ? evlist->threads->nr : 1;
732
733 list_for_each_entry_reverse(evsel, &evlist->entries, node)
734 perf_evsel__close(evsel, ncpus, nthreads);
735
Namhyung Kim41c21a62012-02-23 12:13:36 +0900736 errno = -err;
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -0200737 return err;
738}
Arnaldo Carvalho de Melo35b9d882011-11-09 08:47:15 -0200739
740int perf_evlist__prepare_workload(struct perf_evlist *evlist,
741 struct perf_record_opts *opts,
742 const char *argv[])
743{
744 int child_ready_pipe[2], go_pipe[2];
745 char bf;
746
747 if (pipe(child_ready_pipe) < 0) {
748 perror("failed to create 'ready' pipe");
749 return -1;
750 }
751
752 if (pipe(go_pipe) < 0) {
753 perror("failed to create 'go' pipe");
754 goto out_close_ready_pipe;
755 }
756
757 evlist->workload.pid = fork();
758 if (evlist->workload.pid < 0) {
759 perror("failed to fork");
760 goto out_close_pipes;
761 }
762
763 if (!evlist->workload.pid) {
764 if (opts->pipe_output)
765 dup2(2, 1);
766
767 close(child_ready_pipe[0]);
768 close(go_pipe[1]);
769 fcntl(go_pipe[0], F_SETFD, FD_CLOEXEC);
770
771 /*
772 * Do a dummy execvp to get the PLT entry resolved,
773 * so we avoid the resolver overhead on the real
774 * execvp call.
775 */
776 execvp("", (char **)argv);
777
778 /*
779 * Tell the parent we're ready to go
780 */
781 close(child_ready_pipe[1]);
782
783 /*
784 * Wait until the parent tells us to go.
785 */
786 if (read(go_pipe[0], &bf, 1) == -1)
787 perror("unable to read pipe");
788
789 execvp(argv[0], (char **)argv);
790
791 perror(argv[0]);
792 kill(getppid(), SIGUSR1);
793 exit(-1);
794 }
795
Namhyung Kimd67356e2012-05-07 14:09:03 +0900796 if (perf_target__none(&opts->target))
Arnaldo Carvalho de Melo35b9d882011-11-09 08:47:15 -0200797 evlist->threads->map[0] = evlist->workload.pid;
798
799 close(child_ready_pipe[1]);
800 close(go_pipe[0]);
801 /*
802 * wait for child to settle
803 */
804 if (read(child_ready_pipe[0], &bf, 1) == -1) {
805 perror("unable to read pipe");
806 goto out_close_pipes;
807 }
808
809 evlist->workload.cork_fd = go_pipe[1];
810 close(child_ready_pipe[0]);
811 return 0;
812
813out_close_pipes:
814 close(go_pipe[0]);
815 close(go_pipe[1]);
816out_close_ready_pipe:
817 close(child_ready_pipe[0]);
818 close(child_ready_pipe[1]);
819 return -1;
820}
821
822int perf_evlist__start_workload(struct perf_evlist *evlist)
823{
824 if (evlist->workload.cork_fd > 0) {
825 /*
826 * Remove the cork, let it rip!
827 */
828 return close(evlist->workload.cork_fd);
829 }
830
831 return 0;
832}
Arnaldo Carvalho de Melocb0b29e2012-08-02 11:42:57 -0300833
Arnaldo Carvalho de Meloa3f698f2012-08-02 12:23:46 -0300834int perf_evlist__parse_sample(struct perf_evlist *evlist, union perf_event *event,
Arnaldo Carvalho de Melo0807d2d2012-09-26 12:48:18 -0300835 struct perf_sample *sample)
Arnaldo Carvalho de Melocb0b29e2012-08-02 11:42:57 -0300836{
Arnaldo Carvalho de Melo0c21f732012-08-14 16:42:15 -0300837 struct perf_evsel *evsel = perf_evlist__first(evlist);
Arnaldo Carvalho de Melo0807d2d2012-09-26 12:48:18 -0300838 return perf_evsel__parse_sample(evsel, event, sample);
Arnaldo Carvalho de Melocb0b29e2012-08-02 11:42:57 -0300839}
Arnaldo Carvalho de Melo78f067b2012-09-06 14:54:11 -0300840
841size_t perf_evlist__fprintf(struct perf_evlist *evlist, FILE *fp)
842{
843 struct perf_evsel *evsel;
844 size_t printed = 0;
845
846 list_for_each_entry(evsel, &evlist->entries, node) {
847 printed += fprintf(fp, "%s%s", evsel->idx ? ", " : "",
848 perf_evsel__name(evsel));
849 }
850
851 return printed + fprintf(fp, "\n");;
852}