blob: d0e1e821fe85304145fc691d44ef7d7836789093 [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{
Jiri Olsacac21422012-11-12 18:34:00 +010055 struct perf_evsel *evsel;
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -020056
57 if (evlist->cpus->map[0] < 0)
58 opts->no_inherit = true;
59
60 list_for_each_entry(evsel, &evlist->entries, node) {
Jiri Olsacac21422012-11-12 18:34:00 +010061 perf_evsel__config(evsel, opts);
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -020062
63 if (evlist->nr_entries > 1)
64 evsel->attr.sample_type |= PERF_SAMPLE_ID;
65 }
66}
67
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -020068static void perf_evlist__purge(struct perf_evlist *evlist)
69{
70 struct perf_evsel *pos, *n;
71
72 list_for_each_entry_safe(pos, n, &evlist->entries, node) {
73 list_del_init(&pos->node);
74 perf_evsel__delete(pos);
75 }
76
77 evlist->nr_entries = 0;
78}
79
Arnaldo Carvalho de Meloef1d1af2011-01-18 21:41:45 -020080void perf_evlist__exit(struct perf_evlist *evlist)
81{
82 free(evlist->mmap);
83 free(evlist->pollfd);
84 evlist->mmap = NULL;
85 evlist->pollfd = NULL;
86}
87
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -020088void perf_evlist__delete(struct perf_evlist *evlist)
89{
90 perf_evlist__purge(evlist);
Arnaldo Carvalho de Meloef1d1af2011-01-18 21:41:45 -020091 perf_evlist__exit(evlist);
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -020092 free(evlist);
93}
94
95void perf_evlist__add(struct perf_evlist *evlist, struct perf_evsel *entry)
96{
97 list_add_tail(&entry->node, &evlist->entries);
98 ++evlist->nr_entries;
99}
100
Jiri Olsa0529bc12012-01-27 15:34:20 +0100101void perf_evlist__splice_list_tail(struct perf_evlist *evlist,
102 struct list_head *list,
103 int nr_entries)
Arnaldo Carvalho de Melo50d08e42011-11-04 09:10:59 -0200104{
105 list_splice_tail(list, &evlist->entries);
106 evlist->nr_entries += nr_entries;
107}
108
Arnaldo Carvalho de Melo63dab222012-08-14 16:35:48 -0300109void __perf_evlist__set_leader(struct list_head *list)
110{
111 struct perf_evsel *evsel, *leader;
112
113 leader = list_entry(list->next, struct perf_evsel, node);
Arnaldo Carvalho de Melo63dab222012-08-14 16:35:48 -0300114
115 list_for_each_entry(evsel, list, node) {
116 if (evsel != leader)
117 evsel->leader = leader;
118 }
119}
120
121void perf_evlist__set_leader(struct perf_evlist *evlist)
Jiri Olsa6a4bb042012-08-08 12:22:36 +0200122{
123 if (evlist->nr_entries)
Arnaldo Carvalho de Melo63dab222012-08-14 16:35:48 -0300124 __perf_evlist__set_leader(&evlist->entries);
Jiri Olsa6a4bb042012-08-08 12:22:36 +0200125}
126
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -0200127int perf_evlist__add_default(struct perf_evlist *evlist)
128{
129 struct perf_event_attr attr = {
130 .type = PERF_TYPE_HARDWARE,
131 .config = PERF_COUNT_HW_CPU_CYCLES,
132 };
Joerg Roedel1aed2672012-01-04 17:54:20 +0100133 struct perf_evsel *evsel;
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -0200134
Joerg Roedel1aed2672012-01-04 17:54:20 +0100135 event_attr_init(&attr);
136
137 evsel = perf_evsel__new(&attr, 0);
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -0200138 if (evsel == NULL)
Stephane Eraniancc2d86b2011-06-07 18:19:36 +0200139 goto error;
140
141 /* use strdup() because free(evsel) assumes name is allocated */
142 evsel->name = strdup("cycles");
143 if (!evsel->name)
144 goto error_free;
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -0200145
146 perf_evlist__add(evlist, evsel);
147 return 0;
Stephane Eraniancc2d86b2011-06-07 18:19:36 +0200148error_free:
149 perf_evsel__delete(evsel);
150error:
151 return -ENOMEM;
Arnaldo Carvalho de Melo361c99a2011-01-11 20:56:53 -0200152}
Arnaldo Carvalho de Melo5c581042011-01-11 22:30:02 -0200153
Arnaldo Carvalho de Meloe60fc842012-10-03 11:50:55 -0300154static int perf_evlist__add_attrs(struct perf_evlist *evlist,
155 struct perf_event_attr *attrs, size_t nr_attrs)
Arnaldo Carvalho de Melo50d08e42011-11-04 09:10:59 -0200156{
157 struct perf_evsel *evsel, *n;
158 LIST_HEAD(head);
159 size_t i;
160
161 for (i = 0; i < nr_attrs; i++) {
162 evsel = perf_evsel__new(attrs + i, evlist->nr_entries + i);
163 if (evsel == NULL)
164 goto out_delete_partial_list;
165 list_add_tail(&evsel->node, &head);
166 }
167
168 perf_evlist__splice_list_tail(evlist, &head, nr_attrs);
169
170 return 0;
171
172out_delete_partial_list:
173 list_for_each_entry_safe(evsel, n, &head, node)
174 perf_evsel__delete(evsel);
175 return -1;
176}
177
Arnaldo Carvalho de Melo79695e12012-05-30 13:53:54 -0300178int __perf_evlist__add_default_attrs(struct perf_evlist *evlist,
179 struct perf_event_attr *attrs, size_t nr_attrs)
180{
181 size_t i;
182
183 for (i = 0; i < nr_attrs; i++)
184 event_attr_init(attrs + i);
185
186 return perf_evlist__add_attrs(evlist, attrs, nr_attrs);
187}
188
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -0300189struct perf_evsel *
190perf_evlist__find_tracepoint_by_id(struct perf_evlist *evlist, int id)
Arnaldo Carvalho de Meloee29be62011-11-28 17:57:40 -0200191{
192 struct perf_evsel *evsel;
193
194 list_for_each_entry(evsel, &evlist->entries, node) {
195 if (evsel->attr.type == PERF_TYPE_TRACEPOINT &&
196 (int)evsel->attr.config == id)
197 return evsel;
198 }
199
200 return NULL;
201}
202
Arnaldo Carvalho de Melo39876e72012-10-03 11:40:22 -0300203int perf_evlist__add_newtp(struct perf_evlist *evlist,
204 const char *sys, const char *name, void *handler)
205{
206 struct perf_evsel *evsel;
207
208 evsel = perf_evsel__newtp(sys, name, evlist->nr_entries);
209 if (evsel == NULL)
210 return -1;
211
212 evsel->handler.func = handler;
213 perf_evlist__add(evlist, evsel);
214 return 0;
215}
216
Arnaldo Carvalho de Melo4152ab32011-07-25 11:06:19 -0300217void perf_evlist__disable(struct perf_evlist *evlist)
218{
219 int cpu, thread;
220 struct perf_evsel *pos;
221
222 for (cpu = 0; cpu < evlist->cpus->nr; cpu++) {
223 list_for_each_entry(pos, &evlist->entries, node) {
Namhyung Kim823254e2012-11-29 15:38:30 +0900224 if (!perf_evsel__is_group_leader(pos))
Jiri Olsa3fe4430d2012-11-12 18:34:03 +0100225 continue;
Arnaldo Carvalho de Melo4152ab32011-07-25 11:06:19 -0300226 for (thread = 0; thread < evlist->threads->nr; thread++)
Namhyung Kim55da8002012-05-31 14:51:46 +0900227 ioctl(FD(pos, cpu, thread),
228 PERF_EVENT_IOC_DISABLE, 0);
Arnaldo Carvalho de Melo4152ab32011-07-25 11:06:19 -0300229 }
230 }
231}
232
David Ahern764e16a32011-08-25 10:17:55 -0600233void perf_evlist__enable(struct perf_evlist *evlist)
234{
235 int cpu, thread;
236 struct perf_evsel *pos;
237
Arnaldo Carvalho de Meloa14bb7a2012-09-26 12:41:14 -0300238 for (cpu = 0; cpu < cpu_map__nr(evlist->cpus); cpu++) {
David Ahern764e16a32011-08-25 10:17:55 -0600239 list_for_each_entry(pos, &evlist->entries, node) {
Namhyung Kim823254e2012-11-29 15:38:30 +0900240 if (!perf_evsel__is_group_leader(pos))
Jiri Olsa3fe4430d2012-11-12 18:34:03 +0100241 continue;
David Ahern764e16a32011-08-25 10:17:55 -0600242 for (thread = 0; thread < evlist->threads->nr; thread++)
Namhyung Kim55da8002012-05-31 14:51:46 +0900243 ioctl(FD(pos, cpu, thread),
244 PERF_EVENT_IOC_ENABLE, 0);
David Ahern764e16a32011-08-25 10:17:55 -0600245 }
246 }
247}
248
Arnaldo Carvalho de Melo806fb632011-11-29 08:05:52 -0200249static int perf_evlist__alloc_pollfd(struct perf_evlist *evlist)
Arnaldo Carvalho de Melo5c581042011-01-11 22:30:02 -0200250{
Arnaldo Carvalho de Meloa14bb7a2012-09-26 12:41:14 -0300251 int nfds = cpu_map__nr(evlist->cpus) * evlist->threads->nr * evlist->nr_entries;
Arnaldo Carvalho de Melo5c581042011-01-11 22:30:02 -0200252 evlist->pollfd = malloc(sizeof(struct pollfd) * nfds);
253 return evlist->pollfd != NULL ? 0 : -ENOMEM;
254}
Arnaldo Carvalho de Melo70082dd2011-01-12 17:03:24 -0200255
256void perf_evlist__add_pollfd(struct perf_evlist *evlist, int fd)
257{
258 fcntl(fd, F_SETFL, O_NONBLOCK);
259 evlist->pollfd[evlist->nr_fds].fd = fd;
260 evlist->pollfd[evlist->nr_fds].events = POLLIN;
261 evlist->nr_fds++;
262}
Arnaldo Carvalho de Melo70db7532011-01-12 22:39:13 -0200263
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -0300264static void perf_evlist__id_hash(struct perf_evlist *evlist,
265 struct perf_evsel *evsel,
266 int cpu, int thread, u64 id)
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200267{
Arnaldo Carvalho de Melo3d3b5e92011-03-04 22:29:39 -0300268 int hash;
269 struct perf_sample_id *sid = SID(evsel, cpu, thread);
270
271 sid->id = id;
272 sid->evsel = evsel;
273 hash = hash_64(sid->id, PERF_EVLIST__HLIST_BITS);
274 hlist_add_head(&sid->node, &evlist->heads[hash]);
275}
276
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -0300277void perf_evlist__id_add(struct perf_evlist *evlist, struct perf_evsel *evsel,
278 int cpu, int thread, u64 id)
279{
280 perf_evlist__id_hash(evlist, evsel, cpu, thread, id);
281 evsel->id[evsel->ids++] = id;
282}
283
284static int perf_evlist__id_add_fd(struct perf_evlist *evlist,
285 struct perf_evsel *evsel,
286 int cpu, int thread, int fd)
Arnaldo Carvalho de Melo3d3b5e92011-03-04 22:29:39 -0300287{
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200288 u64 read_data[4] = { 0, };
Arnaldo Carvalho de Melo3d3b5e92011-03-04 22:29:39 -0300289 int id_idx = 1; /* The first entry is the counter value */
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200290
291 if (!(evsel->attr.read_format & PERF_FORMAT_ID) ||
292 read(fd, &read_data, sizeof(read_data)) == -1)
293 return -1;
294
295 if (evsel->attr.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
296 ++id_idx;
297 if (evsel->attr.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
298 ++id_idx;
299
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -0300300 perf_evlist__id_add(evlist, evsel, cpu, thread, read_data[id_idx]);
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200301 return 0;
302}
303
Arnaldo Carvalho de Melo70db7532011-01-12 22:39:13 -0200304struct perf_evsel *perf_evlist__id2evsel(struct perf_evlist *evlist, u64 id)
305{
306 struct hlist_head *head;
307 struct hlist_node *pos;
308 struct perf_sample_id *sid;
309 int hash;
310
311 if (evlist->nr_entries == 1)
Arnaldo Carvalho de Melo0c21f732012-08-14 16:42:15 -0300312 return perf_evlist__first(evlist);
Arnaldo Carvalho de Melo70db7532011-01-12 22:39:13 -0200313
314 hash = hash_64(id, PERF_EVLIST__HLIST_BITS);
315 head = &evlist->heads[hash];
316
317 hlist_for_each_entry(sid, pos, head, node)
318 if (sid->id == id)
319 return sid->evsel;
Namhyung Kim30e68bc2012-02-20 10:47:26 +0900320
321 if (!perf_evlist__sample_id_all(evlist))
Arnaldo Carvalho de Melo0c21f732012-08-14 16:42:15 -0300322 return perf_evlist__first(evlist);
Namhyung Kim30e68bc2012-02-20 10:47:26 +0900323
Arnaldo Carvalho de Melo70db7532011-01-12 22:39:13 -0200324 return NULL;
325}
Arnaldo Carvalho de Melo04391de2011-01-15 10:40:59 -0200326
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -0300327union perf_event *perf_evlist__mmap_read(struct perf_evlist *evlist, int idx)
Arnaldo Carvalho de Melo04391de2011-01-15 10:40:59 -0200328{
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -0300329 struct perf_mmap *md = &evlist->mmap[idx];
Arnaldo Carvalho de Melo04391de2011-01-15 10:40:59 -0200330 unsigned int head = perf_mmap__read_head(md);
331 unsigned int old = md->prev;
332 unsigned char *data = md->base + page_size;
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200333 union perf_event *event = NULL;
Arnaldo Carvalho de Melo04391de2011-01-15 10:40:59 -0200334
Arnaldo Carvalho de Melo7bb41152011-01-29 09:08:13 -0200335 if (evlist->overwrite) {
Arnaldo Carvalho de Melo04391de2011-01-15 10:40:59 -0200336 /*
Arnaldo Carvalho de Melo7bb41152011-01-29 09:08:13 -0200337 * If we're further behind than half the buffer, there's a chance
338 * the writer will bite our tail and mess up the samples under us.
339 *
340 * If we somehow ended up ahead of the head, we got messed up.
341 *
342 * In either case, truncate and restart at head.
Arnaldo Carvalho de Melo04391de2011-01-15 10:40:59 -0200343 */
Arnaldo Carvalho de Melo7bb41152011-01-29 09:08:13 -0200344 int diff = head - old;
345 if (diff > md->mask / 2 || diff < 0) {
346 fprintf(stderr, "WARNING: failed to keep up with mmap data.\n");
347
348 /*
349 * head points to a known good entry, start there.
350 */
351 old = head;
352 }
Arnaldo Carvalho de Melo04391de2011-01-15 10:40:59 -0200353 }
354
355 if (old != head) {
356 size_t size;
357
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200358 event = (union perf_event *)&data[old & md->mask];
Arnaldo Carvalho de Melo04391de2011-01-15 10:40:59 -0200359 size = event->header.size;
360
361 /*
362 * Event straddles the mmap boundary -- header should always
363 * be inside due to u64 alignment of output.
364 */
365 if ((old & md->mask) + size != ((old + size) & md->mask)) {
366 unsigned int offset = old;
367 unsigned int len = min(sizeof(*event), size), cpy;
368 void *dst = &evlist->event_copy;
369
370 do {
371 cpy = min(md->mask + 1 - (offset & md->mask), len);
372 memcpy(dst, &data[offset & md->mask], cpy);
373 offset += cpy;
374 dst += cpy;
375 len -= cpy;
376 } while (len);
377
378 event = &evlist->event_copy;
379 }
380
381 old += size;
382 }
383
384 md->prev = old;
Arnaldo Carvalho de Melo7bb41152011-01-29 09:08:13 -0200385
386 if (!evlist->overwrite)
387 perf_mmap__write_tail(md, old);
388
Arnaldo Carvalho de Melo04391de2011-01-15 10:40:59 -0200389 return event;
390}
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200391
Arnaldo Carvalho de Melo7e2ed092011-01-30 11:59:43 -0200392void perf_evlist__munmap(struct perf_evlist *evlist)
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200393{
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -0300394 int i;
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200395
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -0300396 for (i = 0; i < evlist->nr_mmaps; i++) {
397 if (evlist->mmap[i].base != NULL) {
398 munmap(evlist->mmap[i].base, evlist->mmap_len);
399 evlist->mmap[i].base = NULL;
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200400 }
401 }
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -0300402
403 free(evlist->mmap);
404 evlist->mmap = NULL;
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200405}
406
Arnaldo Carvalho de Melo806fb632011-11-29 08:05:52 -0200407static int perf_evlist__alloc_mmap(struct perf_evlist *evlist)
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200408{
Arnaldo Carvalho de Meloa14bb7a2012-09-26 12:41:14 -0300409 evlist->nr_mmaps = cpu_map__nr(evlist->cpus);
410 if (cpu_map__all(evlist->cpus))
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -0300411 evlist->nr_mmaps = evlist->threads->nr;
412 evlist->mmap = zalloc(evlist->nr_mmaps * sizeof(struct perf_mmap));
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200413 return evlist->mmap != NULL ? 0 : -ENOMEM;
414}
415
Arnaldo Carvalho de Melobccdaba2011-06-02 10:39:43 -0300416static int __perf_evlist__mmap(struct perf_evlist *evlist,
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -0300417 int idx, int prot, int mask, int fd)
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200418{
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -0300419 evlist->mmap[idx].prev = 0;
420 evlist->mmap[idx].mask = mask;
421 evlist->mmap[idx].base = mmap(NULL, evlist->mmap_len, prot,
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200422 MAP_SHARED, fd, 0);
Nelson Elhage301b1952011-12-19 08:39:30 -0500423 if (evlist->mmap[idx].base == MAP_FAILED) {
424 evlist->mmap[idx].base = NULL;
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200425 return -1;
Nelson Elhage301b1952011-12-19 08:39:30 -0500426 }
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200427
428 perf_evlist__add_pollfd(evlist, fd);
429 return 0;
430}
431
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -0300432static int perf_evlist__mmap_per_cpu(struct perf_evlist *evlist, int prot, int mask)
433{
434 struct perf_evsel *evsel;
435 int cpu, thread;
436
437 for (cpu = 0; cpu < evlist->cpus->nr; cpu++) {
438 int output = -1;
439
440 for (thread = 0; thread < evlist->threads->nr; thread++) {
441 list_for_each_entry(evsel, &evlist->entries, node) {
442 int fd = FD(evsel, cpu, thread);
443
444 if (output == -1) {
445 output = fd;
Arnaldo Carvalho de Melobccdaba2011-06-02 10:39:43 -0300446 if (__perf_evlist__mmap(evlist, cpu,
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -0300447 prot, mask, output) < 0)
448 goto out_unmap;
449 } else {
450 if (ioctl(fd, PERF_EVENT_IOC_SET_OUTPUT, output) != 0)
451 goto out_unmap;
452 }
453
454 if ((evsel->attr.read_format & PERF_FORMAT_ID) &&
455 perf_evlist__id_add_fd(evlist, evsel, cpu, thread, fd) < 0)
456 goto out_unmap;
457 }
458 }
459 }
460
461 return 0;
462
463out_unmap:
464 for (cpu = 0; cpu < evlist->cpus->nr; cpu++) {
465 if (evlist->mmap[cpu].base != NULL) {
466 munmap(evlist->mmap[cpu].base, evlist->mmap_len);
467 evlist->mmap[cpu].base = NULL;
468 }
469 }
470 return -1;
471}
472
473static int perf_evlist__mmap_per_thread(struct perf_evlist *evlist, int prot, int mask)
474{
475 struct perf_evsel *evsel;
476 int thread;
477
478 for (thread = 0; thread < evlist->threads->nr; thread++) {
479 int output = -1;
480
481 list_for_each_entry(evsel, &evlist->entries, node) {
482 int fd = FD(evsel, 0, thread);
483
484 if (output == -1) {
485 output = fd;
Arnaldo Carvalho de Melobccdaba2011-06-02 10:39:43 -0300486 if (__perf_evlist__mmap(evlist, thread,
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -0300487 prot, mask, output) < 0)
488 goto out_unmap;
489 } else {
490 if (ioctl(fd, PERF_EVENT_IOC_SET_OUTPUT, output) != 0)
491 goto out_unmap;
492 }
493
494 if ((evsel->attr.read_format & PERF_FORMAT_ID) &&
495 perf_evlist__id_add_fd(evlist, evsel, 0, thread, fd) < 0)
496 goto out_unmap;
497 }
498 }
499
500 return 0;
501
502out_unmap:
503 for (thread = 0; thread < evlist->threads->nr; thread++) {
504 if (evlist->mmap[thread].base != NULL) {
505 munmap(evlist->mmap[thread].base, evlist->mmap_len);
506 evlist->mmap[thread].base = NULL;
507 }
508 }
509 return -1;
510}
511
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200512/** perf_evlist__mmap - Create per cpu maps to receive events
513 *
514 * @evlist - list of events
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200515 * @pages - map length in pages
516 * @overwrite - overwrite older events?
517 *
518 * If overwrite is false the user needs to signal event consuption using:
519 *
520 * struct perf_mmap *m = &evlist->mmap[cpu];
521 * unsigned int head = perf_mmap__read_head(m);
522 *
523 * perf_mmap__write_tail(m, head)
Arnaldo Carvalho de Melo7e2ed092011-01-30 11:59:43 -0200524 *
525 * Using perf_evlist__read_on_cpu does this automatically.
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200526 */
Arnaldo Carvalho de Melo50a682c2011-11-09 09:10:47 -0200527int perf_evlist__mmap(struct perf_evlist *evlist, unsigned int pages,
528 bool overwrite)
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200529{
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -0300530 struct perf_evsel *evsel;
Arnaldo Carvalho de Melo7e2ed092011-01-30 11:59:43 -0200531 const struct cpu_map *cpus = evlist->cpus;
532 const struct thread_map *threads = evlist->threads;
Arnaldo Carvalho de Melo50a682c2011-11-09 09:10:47 -0200533 int prot = PROT_READ | (overwrite ? 0 : PROT_WRITE), mask;
534
535 /* 512 kiB: default amount of unprivileged mlocked memory */
536 if (pages == UINT_MAX)
537 pages = (512 * 1024) / page_size;
Nelson Elhage41d0d932011-12-19 08:39:32 -0500538 else if (!is_power_of_2(pages))
539 return -EINVAL;
Arnaldo Carvalho de Melo50a682c2011-11-09 09:10:47 -0200540
541 mask = pages * page_size - 1;
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200542
Arnaldo Carvalho de Melo7e2ed092011-01-30 11:59:43 -0200543 if (evlist->mmap == NULL && perf_evlist__alloc_mmap(evlist) < 0)
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200544 return -ENOMEM;
545
Arnaldo Carvalho de Melo7e2ed092011-01-30 11:59:43 -0200546 if (evlist->pollfd == NULL && perf_evlist__alloc_pollfd(evlist) < 0)
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200547 return -ENOMEM;
548
549 evlist->overwrite = overwrite;
550 evlist->mmap_len = (pages + 1) * page_size;
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200551
552 list_for_each_entry(evsel, &evlist->entries, node) {
553 if ((evsel->attr.read_format & PERF_FORMAT_ID) &&
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -0300554 evsel->sample_id == NULL &&
Arnaldo Carvalho de Meloa14bb7a2012-09-26 12:41:14 -0300555 perf_evsel__alloc_id(evsel, cpu_map__nr(cpus), threads->nr) < 0)
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200556 return -ENOMEM;
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200557 }
558
Arnaldo Carvalho de Meloa14bb7a2012-09-26 12:41:14 -0300559 if (cpu_map__all(cpus))
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -0300560 return perf_evlist__mmap_per_thread(evlist, prot, mask);
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200561
Arnaldo Carvalho de Meloaece9482011-05-15 09:39:00 -0300562 return perf_evlist__mmap_per_cpu(evlist, prot, mask);
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -0200563}
Arnaldo Carvalho de Melo7e2ed092011-01-30 11:59:43 -0200564
Namhyung Kimb809ac12012-04-26 14:15:19 +0900565int perf_evlist__create_maps(struct perf_evlist *evlist,
566 struct perf_target *target)
Arnaldo Carvalho de Melo7e2ed092011-01-30 11:59:43 -0200567{
Namhyung Kimb809ac12012-04-26 14:15:19 +0900568 evlist->threads = thread_map__new_str(target->pid, target->tid,
569 target->uid);
Arnaldo Carvalho de Melo7e2ed092011-01-30 11:59:43 -0200570
571 if (evlist->threads == NULL)
572 return -1;
573
Namhyung Kim879d77d2012-05-16 18:45:48 +0900574 if (perf_target__has_task(target))
Namhyung Kimd67356e2012-05-07 14:09:03 +0900575 evlist->cpus = cpu_map__dummy_new();
Namhyung Kimd1cb9fc2012-05-16 18:45:49 +0900576 else if (!perf_target__has_cpu(target) && !target->uses_mmap)
577 evlist->cpus = cpu_map__dummy_new();
Namhyung Kim879d77d2012-05-16 18:45:48 +0900578 else
579 evlist->cpus = cpu_map__new(target->cpu_list);
Arnaldo Carvalho de Melo7e2ed092011-01-30 11:59:43 -0200580
581 if (evlist->cpus == NULL)
582 goto out_delete_threads;
583
584 return 0;
585
586out_delete_threads:
587 thread_map__delete(evlist->threads);
588 return -1;
589}
590
591void perf_evlist__delete_maps(struct perf_evlist *evlist)
592{
593 cpu_map__delete(evlist->cpus);
594 thread_map__delete(evlist->threads);
595 evlist->cpus = NULL;
596 evlist->threads = NULL;
597}
Frederic Weisbecker0a102472011-02-26 04:51:54 +0100598
Arnaldo Carvalho de Melo1491a632012-09-26 14:43:13 -0300599int perf_evlist__apply_filters(struct perf_evlist *evlist)
Frederic Weisbecker0a102472011-02-26 04:51:54 +0100600{
Frederic Weisbecker0a102472011-02-26 04:51:54 +0100601 struct perf_evsel *evsel;
Arnaldo Carvalho de Melo745cefc2012-09-26 15:07:39 -0300602 int err = 0;
603 const int ncpus = cpu_map__nr(evlist->cpus),
604 nthreads = evlist->threads->nr;
Frederic Weisbecker0a102472011-02-26 04:51:54 +0100605
606 list_for_each_entry(evsel, &evlist->entries, node) {
Arnaldo Carvalho de Melo745cefc2012-09-26 15:07:39 -0300607 if (evsel->filter == NULL)
Frederic Weisbecker0a102472011-02-26 04:51:54 +0100608 continue;
Arnaldo Carvalho de Melo745cefc2012-09-26 15:07:39 -0300609
610 err = perf_evsel__set_filter(evsel, ncpus, nthreads, evsel->filter);
611 if (err)
612 break;
Frederic Weisbecker0a102472011-02-26 04:51:54 +0100613 }
614
Arnaldo Carvalho de Melo745cefc2012-09-26 15:07:39 -0300615 return err;
616}
617
618int perf_evlist__set_filter(struct perf_evlist *evlist, const char *filter)
619{
620 struct perf_evsel *evsel;
621 int err = 0;
622 const int ncpus = cpu_map__nr(evlist->cpus),
623 nthreads = evlist->threads->nr;
624
625 list_for_each_entry(evsel, &evlist->entries, node) {
626 err = perf_evsel__set_filter(evsel, ncpus, nthreads, filter);
627 if (err)
628 break;
629 }
630
631 return err;
Frederic Weisbecker0a102472011-02-26 04:51:54 +0100632}
Frederic Weisbecker74429962011-05-21 17:49:00 +0200633
Arnaldo Carvalho de Melo0c21f732012-08-14 16:42:15 -0300634bool perf_evlist__valid_sample_type(struct perf_evlist *evlist)
Frederic Weisbecker74429962011-05-21 17:49:00 +0200635{
Arnaldo Carvalho de Melo0c21f732012-08-14 16:42:15 -0300636 struct perf_evsel *first = perf_evlist__first(evlist), *pos = first;
Arnaldo Carvalho de Meloc2a70652011-06-02 11:04:54 -0300637
638 list_for_each_entry_continue(pos, &evlist->entries, node) {
639 if (first->attr.sample_type != pos->attr.sample_type)
640 return false;
Frederic Weisbecker74429962011-05-21 17:49:00 +0200641 }
642
Arnaldo Carvalho de Meloc2a70652011-06-02 11:04:54 -0300643 return true;
644}
645
Arnaldo Carvalho de Melo0c21f732012-08-14 16:42:15 -0300646u64 perf_evlist__sample_type(struct perf_evlist *evlist)
Arnaldo Carvalho de Meloc2a70652011-06-02 11:04:54 -0300647{
Arnaldo Carvalho de Melo0c21f732012-08-14 16:42:15 -0300648 struct perf_evsel *first = perf_evlist__first(evlist);
Arnaldo Carvalho de Meloc2a70652011-06-02 11:04:54 -0300649 return first->attr.sample_type;
650}
651
Arnaldo Carvalho de Melo0c21f732012-08-14 16:42:15 -0300652u16 perf_evlist__id_hdr_size(struct perf_evlist *evlist)
Arnaldo Carvalho de Melo81e36bf2011-11-11 22:28:50 -0200653{
Arnaldo Carvalho de Melo0c21f732012-08-14 16:42:15 -0300654 struct perf_evsel *first = perf_evlist__first(evlist);
Arnaldo Carvalho de Melo81e36bf2011-11-11 22:28:50 -0200655 struct perf_sample *data;
656 u64 sample_type;
657 u16 size = 0;
658
Arnaldo Carvalho de Melo81e36bf2011-11-11 22:28:50 -0200659 if (!first->attr.sample_id_all)
660 goto out;
661
662 sample_type = first->attr.sample_type;
663
664 if (sample_type & PERF_SAMPLE_TID)
665 size += sizeof(data->tid) * 2;
666
667 if (sample_type & PERF_SAMPLE_TIME)
668 size += sizeof(data->time);
669
670 if (sample_type & PERF_SAMPLE_ID)
671 size += sizeof(data->id);
672
673 if (sample_type & PERF_SAMPLE_STREAM_ID)
674 size += sizeof(data->stream_id);
675
676 if (sample_type & PERF_SAMPLE_CPU)
677 size += sizeof(data->cpu) * 2;
678out:
679 return size;
680}
681
Arnaldo Carvalho de Melo0c21f732012-08-14 16:42:15 -0300682bool perf_evlist__valid_sample_id_all(struct perf_evlist *evlist)
Arnaldo Carvalho de Meloc2a70652011-06-02 11:04:54 -0300683{
Arnaldo Carvalho de Melo0c21f732012-08-14 16:42:15 -0300684 struct perf_evsel *first = perf_evlist__first(evlist), *pos = first;
Arnaldo Carvalho de Meloc2a70652011-06-02 11:04:54 -0300685
686 list_for_each_entry_continue(pos, &evlist->entries, node) {
687 if (first->attr.sample_id_all != pos->attr.sample_id_all)
688 return false;
689 }
690
691 return true;
Frederic Weisbecker74429962011-05-21 17:49:00 +0200692}
693
Arnaldo Carvalho de Melo0c21f732012-08-14 16:42:15 -0300694bool perf_evlist__sample_id_all(struct perf_evlist *evlist)
Frederic Weisbecker74429962011-05-21 17:49:00 +0200695{
Arnaldo Carvalho de Melo0c21f732012-08-14 16:42:15 -0300696 struct perf_evsel *first = perf_evlist__first(evlist);
Arnaldo Carvalho de Meloc2a70652011-06-02 11:04:54 -0300697 return first->attr.sample_id_all;
Frederic Weisbecker74429962011-05-21 17:49:00 +0200698}
Arnaldo Carvalho de Melo81cce8d2011-10-05 19:11:32 -0300699
700void perf_evlist__set_selected(struct perf_evlist *evlist,
701 struct perf_evsel *evsel)
702{
703 evlist->selected = evsel;
704}
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -0200705
Jiri Olsa6a4bb042012-08-08 12:22:36 +0200706int perf_evlist__open(struct perf_evlist *evlist)
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -0200707{
Jiri Olsa6a4bb042012-08-08 12:22:36 +0200708 struct perf_evsel *evsel;
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -0200709 int err, ncpus, nthreads;
710
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -0200711 list_for_each_entry(evsel, &evlist->entries, node) {
Jiri Olsa6a4bb042012-08-08 12:22:36 +0200712 err = perf_evsel__open(evsel, evlist->cpus, evlist->threads);
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -0200713 if (err < 0)
714 goto out_err;
715 }
716
717 return 0;
718out_err:
719 ncpus = evlist->cpus ? evlist->cpus->nr : 1;
720 nthreads = evlist->threads ? evlist->threads->nr : 1;
721
722 list_for_each_entry_reverse(evsel, &evlist->entries, node)
723 perf_evsel__close(evsel, ncpus, nthreads);
724
Namhyung Kim41c21a62012-02-23 12:13:36 +0900725 errno = -err;
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -0200726 return err;
727}
Arnaldo Carvalho de Melo35b9d882011-11-09 08:47:15 -0200728
729int perf_evlist__prepare_workload(struct perf_evlist *evlist,
730 struct perf_record_opts *opts,
731 const char *argv[])
732{
733 int child_ready_pipe[2], go_pipe[2];
734 char bf;
735
736 if (pipe(child_ready_pipe) < 0) {
737 perror("failed to create 'ready' pipe");
738 return -1;
739 }
740
741 if (pipe(go_pipe) < 0) {
742 perror("failed to create 'go' pipe");
743 goto out_close_ready_pipe;
744 }
745
746 evlist->workload.pid = fork();
747 if (evlist->workload.pid < 0) {
748 perror("failed to fork");
749 goto out_close_pipes;
750 }
751
752 if (!evlist->workload.pid) {
753 if (opts->pipe_output)
754 dup2(2, 1);
755
756 close(child_ready_pipe[0]);
757 close(go_pipe[1]);
758 fcntl(go_pipe[0], F_SETFD, FD_CLOEXEC);
759
760 /*
761 * Do a dummy execvp to get the PLT entry resolved,
762 * so we avoid the resolver overhead on the real
763 * execvp call.
764 */
765 execvp("", (char **)argv);
766
767 /*
768 * Tell the parent we're ready to go
769 */
770 close(child_ready_pipe[1]);
771
772 /*
773 * Wait until the parent tells us to go.
774 */
775 if (read(go_pipe[0], &bf, 1) == -1)
776 perror("unable to read pipe");
777
778 execvp(argv[0], (char **)argv);
779
780 perror(argv[0]);
781 kill(getppid(), SIGUSR1);
782 exit(-1);
783 }
784
Namhyung Kimd67356e2012-05-07 14:09:03 +0900785 if (perf_target__none(&opts->target))
Arnaldo Carvalho de Melo35b9d882011-11-09 08:47:15 -0200786 evlist->threads->map[0] = evlist->workload.pid;
787
788 close(child_ready_pipe[1]);
789 close(go_pipe[0]);
790 /*
791 * wait for child to settle
792 */
793 if (read(child_ready_pipe[0], &bf, 1) == -1) {
794 perror("unable to read pipe");
795 goto out_close_pipes;
796 }
797
798 evlist->workload.cork_fd = go_pipe[1];
799 close(child_ready_pipe[0]);
800 return 0;
801
802out_close_pipes:
803 close(go_pipe[0]);
804 close(go_pipe[1]);
805out_close_ready_pipe:
806 close(child_ready_pipe[0]);
807 close(child_ready_pipe[1]);
808 return -1;
809}
810
811int perf_evlist__start_workload(struct perf_evlist *evlist)
812{
813 if (evlist->workload.cork_fd > 0) {
814 /*
815 * Remove the cork, let it rip!
816 */
817 return close(evlist->workload.cork_fd);
818 }
819
820 return 0;
821}
Arnaldo Carvalho de Melocb0b29e2012-08-02 11:42:57 -0300822
Arnaldo Carvalho de Meloa3f698f2012-08-02 12:23:46 -0300823int perf_evlist__parse_sample(struct perf_evlist *evlist, union perf_event *event,
Arnaldo Carvalho de Melo0807d2d2012-09-26 12:48:18 -0300824 struct perf_sample *sample)
Arnaldo Carvalho de Melocb0b29e2012-08-02 11:42:57 -0300825{
Arnaldo Carvalho de Melo0c21f732012-08-14 16:42:15 -0300826 struct perf_evsel *evsel = perf_evlist__first(evlist);
Arnaldo Carvalho de Melo0807d2d2012-09-26 12:48:18 -0300827 return perf_evsel__parse_sample(evsel, event, sample);
Arnaldo Carvalho de Melocb0b29e2012-08-02 11:42:57 -0300828}
Arnaldo Carvalho de Melo78f067b2012-09-06 14:54:11 -0300829
830size_t perf_evlist__fprintf(struct perf_evlist *evlist, FILE *fp)
831{
832 struct perf_evsel *evsel;
833 size_t printed = 0;
834
835 list_for_each_entry(evsel, &evlist->entries, node) {
836 printed += fprintf(fp, "%s%s", evsel->idx ? ", " : "",
837 perf_evsel__name(evsel));
838 }
839
840 return printed + fprintf(fp, "\n");;
841}