Commit | Line | Data |
---|---|---|
f8a95309 ACM |
1 | /* |
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 | */ | |
a8c9ae18 ACM |
9 | #include "util.h" |
10 | #include "debugfs.h" | |
5c581041 | 11 | #include <poll.h> |
f8a95309 ACM |
12 | #include "cpumap.h" |
13 | #include "thread_map.h" | |
12864b31 | 14 | #include "target.h" |
361c99a6 ACM |
15 | #include "evlist.h" |
16 | #include "evsel.h" | |
35b9d88e | 17 | #include <unistd.h> |
361c99a6 | 18 | |
50d08e47 ACM |
19 | #include "parse-events.h" |
20 | ||
f8a95309 ACM |
21 | #include <sys/mman.h> |
22 | ||
70db7533 ACM |
23 | #include <linux/bitops.h> |
24 | #include <linux/hash.h> | |
25 | ||
f8a95309 | 26 | #define FD(e, x, y) (*(int *)xyarray__entry(e->fd, x, y)) |
a91e5431 | 27 | #define SID(e, x, y) xyarray__entry(e->sample_id, x, y) |
f8a95309 | 28 | |
7e2ed097 ACM |
29 | void perf_evlist__init(struct perf_evlist *evlist, struct cpu_map *cpus, |
30 | struct thread_map *threads) | |
ef1d1af2 ACM |
31 | { |
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); | |
7e2ed097 | 37 | perf_evlist__set_maps(evlist, cpus, threads); |
35b9d88e | 38 | evlist->workload.pid = -1; |
ef1d1af2 ACM |
39 | } |
40 | ||
7e2ed097 ACM |
41 | struct perf_evlist *perf_evlist__new(struct cpu_map *cpus, |
42 | struct thread_map *threads) | |
361c99a6 ACM |
43 | { |
44 | struct perf_evlist *evlist = zalloc(sizeof(*evlist)); | |
45 | ||
ef1d1af2 | 46 | if (evlist != NULL) |
7e2ed097 | 47 | perf_evlist__init(evlist, cpus, threads); |
361c99a6 ACM |
48 | |
49 | return evlist; | |
50 | } | |
51 | ||
0f82ebc4 ACM |
52 | void perf_evlist__config_attrs(struct perf_evlist *evlist, |
53 | struct perf_record_opts *opts) | |
54 | { | |
5090c6ae | 55 | struct perf_evsel *evsel, *first; |
0f82ebc4 ACM |
56 | |
57 | if (evlist->cpus->map[0] < 0) | |
58 | opts->no_inherit = true; | |
59 | ||
5090c6ae NK |
60 | first = list_entry(evlist->entries.next, struct perf_evsel, node); |
61 | ||
0f82ebc4 | 62 | list_for_each_entry(evsel, &evlist->entries, node) { |
5090c6ae | 63 | perf_evsel__config(evsel, opts, first); |
0f82ebc4 ACM |
64 | |
65 | if (evlist->nr_entries > 1) | |
66 | evsel->attr.sample_type |= PERF_SAMPLE_ID; | |
67 | } | |
68 | } | |
69 | ||
361c99a6 ACM |
70 | static 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 | ||
ef1d1af2 | 82 | void perf_evlist__exit(struct perf_evlist *evlist) |
361c99a6 | 83 | { |
70db7533 | 84 | free(evlist->mmap); |
5c581041 | 85 | free(evlist->pollfd); |
ef1d1af2 ACM |
86 | evlist->mmap = NULL; |
87 | evlist->pollfd = NULL; | |
88 | } | |
89 | ||
90 | void perf_evlist__delete(struct perf_evlist *evlist) | |
91 | { | |
92 | perf_evlist__purge(evlist); | |
93 | perf_evlist__exit(evlist); | |
361c99a6 ACM |
94 | free(evlist); |
95 | } | |
96 | ||
97 | void 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 | ||
0529bc1f JO |
103 | void perf_evlist__splice_list_tail(struct perf_evlist *evlist, |
104 | struct list_head *list, | |
105 | int nr_entries) | |
50d08e47 ACM |
106 | { |
107 | list_splice_tail(list, &evlist->entries); | |
108 | evlist->nr_entries += nr_entries; | |
109 | } | |
110 | ||
361c99a6 ACM |
111 | int perf_evlist__add_default(struct perf_evlist *evlist) |
112 | { | |
113 | struct perf_event_attr attr = { | |
114 | .type = PERF_TYPE_HARDWARE, | |
115 | .config = PERF_COUNT_HW_CPU_CYCLES, | |
116 | }; | |
1aed2671 JR |
117 | struct perf_evsel *evsel; |
118 | ||
119 | event_attr_init(&attr); | |
361c99a6 | 120 | |
1aed2671 | 121 | evsel = perf_evsel__new(&attr, 0); |
361c99a6 | 122 | if (evsel == NULL) |
cc2d86b0 SE |
123 | goto error; |
124 | ||
125 | /* use strdup() because free(evsel) assumes name is allocated */ | |
126 | evsel->name = strdup("cycles"); | |
127 | if (!evsel->name) | |
128 | goto error_free; | |
361c99a6 ACM |
129 | |
130 | perf_evlist__add(evlist, evsel); | |
131 | return 0; | |
cc2d86b0 SE |
132 | error_free: |
133 | perf_evsel__delete(evsel); | |
134 | error: | |
135 | return -ENOMEM; | |
361c99a6 | 136 | } |
5c581041 | 137 | |
50d08e47 ACM |
138 | int perf_evlist__add_attrs(struct perf_evlist *evlist, |
139 | struct perf_event_attr *attrs, size_t nr_attrs) | |
140 | { | |
141 | struct perf_evsel *evsel, *n; | |
142 | LIST_HEAD(head); | |
143 | size_t i; | |
144 | ||
145 | for (i = 0; i < nr_attrs; i++) { | |
146 | evsel = perf_evsel__new(attrs + i, evlist->nr_entries + i); | |
147 | if (evsel == NULL) | |
148 | goto out_delete_partial_list; | |
149 | list_add_tail(&evsel->node, &head); | |
150 | } | |
151 | ||
152 | perf_evlist__splice_list_tail(evlist, &head, nr_attrs); | |
153 | ||
154 | return 0; | |
155 | ||
156 | out_delete_partial_list: | |
157 | list_for_each_entry_safe(evsel, n, &head, node) | |
158 | perf_evsel__delete(evsel); | |
159 | return -1; | |
160 | } | |
161 | ||
79695e1b ACM |
162 | int __perf_evlist__add_default_attrs(struct perf_evlist *evlist, |
163 | struct perf_event_attr *attrs, size_t nr_attrs) | |
164 | { | |
165 | size_t i; | |
166 | ||
167 | for (i = 0; i < nr_attrs; i++) | |
168 | event_attr_init(attrs + i); | |
169 | ||
170 | return perf_evlist__add_attrs(evlist, attrs, nr_attrs); | |
171 | } | |
172 | ||
a8c9ae18 ACM |
173 | static int trace_event__id(const char *evname) |
174 | { | |
175 | char *filename, *colon; | |
176 | int err = -1, fd; | |
177 | ||
178 | if (asprintf(&filename, "%s/%s/id", tracing_events_path, evname) < 0) | |
179 | return -1; | |
180 | ||
181 | colon = strrchr(filename, ':'); | |
182 | if (colon != NULL) | |
183 | *colon = '/'; | |
184 | ||
185 | fd = open(filename, O_RDONLY); | |
186 | if (fd >= 0) { | |
187 | char id[16]; | |
188 | if (read(fd, id, sizeof(id)) > 0) | |
189 | err = atoi(id); | |
190 | close(fd); | |
191 | } | |
192 | ||
193 | free(filename); | |
194 | return err; | |
195 | } | |
196 | ||
197 | int perf_evlist__add_tracepoints(struct perf_evlist *evlist, | |
198 | const char *tracepoints[], | |
199 | size_t nr_tracepoints) | |
200 | { | |
201 | int err; | |
202 | size_t i; | |
203 | struct perf_event_attr *attrs = zalloc(nr_tracepoints * sizeof(*attrs)); | |
204 | ||
205 | if (attrs == NULL) | |
206 | return -1; | |
207 | ||
208 | for (i = 0; i < nr_tracepoints; i++) { | |
209 | err = trace_event__id(tracepoints[i]); | |
210 | ||
211 | if (err < 0) | |
212 | goto out_free_attrs; | |
213 | ||
214 | attrs[i].type = PERF_TYPE_TRACEPOINT; | |
215 | attrs[i].config = err; | |
216 | attrs[i].sample_type = (PERF_SAMPLE_RAW | PERF_SAMPLE_TIME | | |
0983cc0d | 217 | PERF_SAMPLE_CPU | PERF_SAMPLE_PERIOD); |
a8c9ae18 ACM |
218 | attrs[i].sample_period = 1; |
219 | } | |
220 | ||
221 | err = perf_evlist__add_attrs(evlist, attrs, nr_tracepoints); | |
222 | out_free_attrs: | |
223 | free(attrs); | |
224 | return err; | |
225 | } | |
226 | ||
da378962 ACM |
227 | struct perf_evsel * |
228 | perf_evlist__find_tracepoint_by_id(struct perf_evlist *evlist, int id) | |
ee29be62 ACM |
229 | { |
230 | struct perf_evsel *evsel; | |
231 | ||
232 | list_for_each_entry(evsel, &evlist->entries, node) { | |
233 | if (evsel->attr.type == PERF_TYPE_TRACEPOINT && | |
234 | (int)evsel->attr.config == id) | |
235 | return evsel; | |
236 | } | |
237 | ||
238 | return NULL; | |
239 | } | |
240 | ||
241 | int perf_evlist__set_tracepoints_handlers(struct perf_evlist *evlist, | |
242 | const struct perf_evsel_str_handler *assocs, | |
243 | size_t nr_assocs) | |
244 | { | |
245 | struct perf_evsel *evsel; | |
246 | int err; | |
247 | size_t i; | |
248 | ||
249 | for (i = 0; i < nr_assocs; i++) { | |
250 | err = trace_event__id(assocs[i].name); | |
251 | if (err < 0) | |
252 | goto out; | |
253 | ||
254 | evsel = perf_evlist__find_tracepoint_by_id(evlist, err); | |
255 | if (evsel == NULL) | |
256 | continue; | |
257 | ||
258 | err = -EEXIST; | |
259 | if (evsel->handler.func != NULL) | |
260 | goto out; | |
261 | evsel->handler.func = assocs[i].handler; | |
262 | } | |
263 | ||
264 | err = 0; | |
265 | out: | |
266 | return err; | |
267 | } | |
268 | ||
4152ab37 ACM |
269 | void perf_evlist__disable(struct perf_evlist *evlist) |
270 | { | |
271 | int cpu, thread; | |
272 | struct perf_evsel *pos; | |
273 | ||
274 | for (cpu = 0; cpu < evlist->cpus->nr; cpu++) { | |
275 | list_for_each_entry(pos, &evlist->entries, node) { | |
276 | for (thread = 0; thread < evlist->threads->nr; thread++) | |
55da8005 NK |
277 | ioctl(FD(pos, cpu, thread), |
278 | PERF_EVENT_IOC_DISABLE, 0); | |
4152ab37 ACM |
279 | } |
280 | } | |
281 | } | |
282 | ||
764e16a3 DA |
283 | void perf_evlist__enable(struct perf_evlist *evlist) |
284 | { | |
285 | int cpu, thread; | |
286 | struct perf_evsel *pos; | |
287 | ||
288 | for (cpu = 0; cpu < evlist->cpus->nr; cpu++) { | |
289 | list_for_each_entry(pos, &evlist->entries, node) { | |
290 | for (thread = 0; thread < evlist->threads->nr; thread++) | |
55da8005 NK |
291 | ioctl(FD(pos, cpu, thread), |
292 | PERF_EVENT_IOC_ENABLE, 0); | |
764e16a3 DA |
293 | } |
294 | } | |
295 | } | |
296 | ||
806fb630 | 297 | static int perf_evlist__alloc_pollfd(struct perf_evlist *evlist) |
5c581041 | 298 | { |
7e2ed097 | 299 | int nfds = evlist->cpus->nr * evlist->threads->nr * evlist->nr_entries; |
5c581041 ACM |
300 | evlist->pollfd = malloc(sizeof(struct pollfd) * nfds); |
301 | return evlist->pollfd != NULL ? 0 : -ENOMEM; | |
302 | } | |
70082dd9 ACM |
303 | |
304 | void perf_evlist__add_pollfd(struct perf_evlist *evlist, int fd) | |
305 | { | |
306 | fcntl(fd, F_SETFL, O_NONBLOCK); | |
307 | evlist->pollfd[evlist->nr_fds].fd = fd; | |
308 | evlist->pollfd[evlist->nr_fds].events = POLLIN; | |
309 | evlist->nr_fds++; | |
310 | } | |
70db7533 | 311 | |
a91e5431 ACM |
312 | static void perf_evlist__id_hash(struct perf_evlist *evlist, |
313 | struct perf_evsel *evsel, | |
314 | int cpu, int thread, u64 id) | |
3d3b5e95 ACM |
315 | { |
316 | int hash; | |
317 | struct perf_sample_id *sid = SID(evsel, cpu, thread); | |
318 | ||
319 | sid->id = id; | |
320 | sid->evsel = evsel; | |
321 | hash = hash_64(sid->id, PERF_EVLIST__HLIST_BITS); | |
322 | hlist_add_head(&sid->node, &evlist->heads[hash]); | |
323 | } | |
324 | ||
a91e5431 ACM |
325 | void perf_evlist__id_add(struct perf_evlist *evlist, struct perf_evsel *evsel, |
326 | int cpu, int thread, u64 id) | |
327 | { | |
328 | perf_evlist__id_hash(evlist, evsel, cpu, thread, id); | |
329 | evsel->id[evsel->ids++] = id; | |
330 | } | |
331 | ||
332 | static int perf_evlist__id_add_fd(struct perf_evlist *evlist, | |
333 | struct perf_evsel *evsel, | |
334 | int cpu, int thread, int fd) | |
f8a95309 | 335 | { |
f8a95309 | 336 | u64 read_data[4] = { 0, }; |
3d3b5e95 | 337 | int id_idx = 1; /* The first entry is the counter value */ |
f8a95309 ACM |
338 | |
339 | if (!(evsel->attr.read_format & PERF_FORMAT_ID) || | |
340 | read(fd, &read_data, sizeof(read_data)) == -1) | |
341 | return -1; | |
342 | ||
343 | if (evsel->attr.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) | |
344 | ++id_idx; | |
345 | if (evsel->attr.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) | |
346 | ++id_idx; | |
347 | ||
a91e5431 | 348 | perf_evlist__id_add(evlist, evsel, cpu, thread, read_data[id_idx]); |
f8a95309 ACM |
349 | return 0; |
350 | } | |
351 | ||
70db7533 ACM |
352 | struct perf_evsel *perf_evlist__id2evsel(struct perf_evlist *evlist, u64 id) |
353 | { | |
354 | struct hlist_head *head; | |
355 | struct hlist_node *pos; | |
356 | struct perf_sample_id *sid; | |
357 | int hash; | |
358 | ||
359 | if (evlist->nr_entries == 1) | |
360 | return list_entry(evlist->entries.next, struct perf_evsel, node); | |
361 | ||
362 | hash = hash_64(id, PERF_EVLIST__HLIST_BITS); | |
363 | head = &evlist->heads[hash]; | |
364 | ||
365 | hlist_for_each_entry(sid, pos, head, node) | |
366 | if (sid->id == id) | |
367 | return sid->evsel; | |
30e68bcc NK |
368 | |
369 | if (!perf_evlist__sample_id_all(evlist)) | |
370 | return list_entry(evlist->entries.next, struct perf_evsel, node); | |
371 | ||
70db7533 ACM |
372 | return NULL; |
373 | } | |
04391deb | 374 | |
aece948f | 375 | union perf_event *perf_evlist__mmap_read(struct perf_evlist *evlist, int idx) |
04391deb ACM |
376 | { |
377 | /* XXX Move this to perf.c, making it generally available */ | |
378 | unsigned int page_size = sysconf(_SC_PAGE_SIZE); | |
aece948f | 379 | struct perf_mmap *md = &evlist->mmap[idx]; |
04391deb ACM |
380 | unsigned int head = perf_mmap__read_head(md); |
381 | unsigned int old = md->prev; | |
382 | unsigned char *data = md->base + page_size; | |
8115d60c | 383 | union perf_event *event = NULL; |
04391deb | 384 | |
7bb41152 | 385 | if (evlist->overwrite) { |
04391deb | 386 | /* |
7bb41152 ACM |
387 | * If we're further behind than half the buffer, there's a chance |
388 | * the writer will bite our tail and mess up the samples under us. | |
389 | * | |
390 | * If we somehow ended up ahead of the head, we got messed up. | |
391 | * | |
392 | * In either case, truncate and restart at head. | |
04391deb | 393 | */ |
7bb41152 ACM |
394 | int diff = head - old; |
395 | if (diff > md->mask / 2 || diff < 0) { | |
396 | fprintf(stderr, "WARNING: failed to keep up with mmap data.\n"); | |
397 | ||
398 | /* | |
399 | * head points to a known good entry, start there. | |
400 | */ | |
401 | old = head; | |
402 | } | |
04391deb ACM |
403 | } |
404 | ||
405 | if (old != head) { | |
406 | size_t size; | |
407 | ||
8115d60c | 408 | event = (union perf_event *)&data[old & md->mask]; |
04391deb ACM |
409 | size = event->header.size; |
410 | ||
411 | /* | |
412 | * Event straddles the mmap boundary -- header should always | |
413 | * be inside due to u64 alignment of output. | |
414 | */ | |
415 | if ((old & md->mask) + size != ((old + size) & md->mask)) { | |
416 | unsigned int offset = old; | |
417 | unsigned int len = min(sizeof(*event), size), cpy; | |
418 | void *dst = &evlist->event_copy; | |
419 | ||
420 | do { | |
421 | cpy = min(md->mask + 1 - (offset & md->mask), len); | |
422 | memcpy(dst, &data[offset & md->mask], cpy); | |
423 | offset += cpy; | |
424 | dst += cpy; | |
425 | len -= cpy; | |
426 | } while (len); | |
427 | ||
428 | event = &evlist->event_copy; | |
429 | } | |
430 | ||
431 | old += size; | |
432 | } | |
433 | ||
434 | md->prev = old; | |
7bb41152 ACM |
435 | |
436 | if (!evlist->overwrite) | |
437 | perf_mmap__write_tail(md, old); | |
438 | ||
04391deb ACM |
439 | return event; |
440 | } | |
f8a95309 | 441 | |
7e2ed097 | 442 | void perf_evlist__munmap(struct perf_evlist *evlist) |
f8a95309 | 443 | { |
aece948f | 444 | int i; |
f8a95309 | 445 | |
aece948f ACM |
446 | for (i = 0; i < evlist->nr_mmaps; i++) { |
447 | if (evlist->mmap[i].base != NULL) { | |
448 | munmap(evlist->mmap[i].base, evlist->mmap_len); | |
449 | evlist->mmap[i].base = NULL; | |
f8a95309 ACM |
450 | } |
451 | } | |
aece948f ACM |
452 | |
453 | free(evlist->mmap); | |
454 | evlist->mmap = NULL; | |
f8a95309 ACM |
455 | } |
456 | ||
806fb630 | 457 | static int perf_evlist__alloc_mmap(struct perf_evlist *evlist) |
f8a95309 | 458 | { |
aece948f ACM |
459 | evlist->nr_mmaps = evlist->cpus->nr; |
460 | if (evlist->cpus->map[0] == -1) | |
461 | evlist->nr_mmaps = evlist->threads->nr; | |
462 | evlist->mmap = zalloc(evlist->nr_mmaps * sizeof(struct perf_mmap)); | |
f8a95309 ACM |
463 | return evlist->mmap != NULL ? 0 : -ENOMEM; |
464 | } | |
465 | ||
bccdaba0 | 466 | static int __perf_evlist__mmap(struct perf_evlist *evlist, |
aece948f | 467 | int idx, int prot, int mask, int fd) |
f8a95309 | 468 | { |
aece948f ACM |
469 | evlist->mmap[idx].prev = 0; |
470 | evlist->mmap[idx].mask = mask; | |
471 | evlist->mmap[idx].base = mmap(NULL, evlist->mmap_len, prot, | |
f8a95309 | 472 | MAP_SHARED, fd, 0); |
301b195d NE |
473 | if (evlist->mmap[idx].base == MAP_FAILED) { |
474 | evlist->mmap[idx].base = NULL; | |
f8a95309 | 475 | return -1; |
301b195d | 476 | } |
f8a95309 ACM |
477 | |
478 | perf_evlist__add_pollfd(evlist, fd); | |
479 | return 0; | |
480 | } | |
481 | ||
aece948f ACM |
482 | static int perf_evlist__mmap_per_cpu(struct perf_evlist *evlist, int prot, int mask) |
483 | { | |
484 | struct perf_evsel *evsel; | |
485 | int cpu, thread; | |
486 | ||
487 | for (cpu = 0; cpu < evlist->cpus->nr; cpu++) { | |
488 | int output = -1; | |
489 | ||
490 | for (thread = 0; thread < evlist->threads->nr; thread++) { | |
491 | list_for_each_entry(evsel, &evlist->entries, node) { | |
492 | int fd = FD(evsel, cpu, thread); | |
493 | ||
494 | if (output == -1) { | |
495 | output = fd; | |
bccdaba0 | 496 | if (__perf_evlist__mmap(evlist, cpu, |
aece948f ACM |
497 | prot, mask, output) < 0) |
498 | goto out_unmap; | |
499 | } else { | |
500 | if (ioctl(fd, PERF_EVENT_IOC_SET_OUTPUT, output) != 0) | |
501 | goto out_unmap; | |
502 | } | |
503 | ||
504 | if ((evsel->attr.read_format & PERF_FORMAT_ID) && | |
505 | perf_evlist__id_add_fd(evlist, evsel, cpu, thread, fd) < 0) | |
506 | goto out_unmap; | |
507 | } | |
508 | } | |
509 | } | |
510 | ||
511 | return 0; | |
512 | ||
513 | out_unmap: | |
514 | for (cpu = 0; cpu < evlist->cpus->nr; cpu++) { | |
515 | if (evlist->mmap[cpu].base != NULL) { | |
516 | munmap(evlist->mmap[cpu].base, evlist->mmap_len); | |
517 | evlist->mmap[cpu].base = NULL; | |
518 | } | |
519 | } | |
520 | return -1; | |
521 | } | |
522 | ||
523 | static int perf_evlist__mmap_per_thread(struct perf_evlist *evlist, int prot, int mask) | |
524 | { | |
525 | struct perf_evsel *evsel; | |
526 | int thread; | |
527 | ||
528 | for (thread = 0; thread < evlist->threads->nr; thread++) { | |
529 | int output = -1; | |
530 | ||
531 | list_for_each_entry(evsel, &evlist->entries, node) { | |
532 | int fd = FD(evsel, 0, thread); | |
533 | ||
534 | if (output == -1) { | |
535 | output = fd; | |
bccdaba0 | 536 | if (__perf_evlist__mmap(evlist, thread, |
aece948f ACM |
537 | prot, mask, output) < 0) |
538 | goto out_unmap; | |
539 | } else { | |
540 | if (ioctl(fd, PERF_EVENT_IOC_SET_OUTPUT, output) != 0) | |
541 | goto out_unmap; | |
542 | } | |
543 | ||
544 | if ((evsel->attr.read_format & PERF_FORMAT_ID) && | |
545 | perf_evlist__id_add_fd(evlist, evsel, 0, thread, fd) < 0) | |
546 | goto out_unmap; | |
547 | } | |
548 | } | |
549 | ||
550 | return 0; | |
551 | ||
552 | out_unmap: | |
553 | for (thread = 0; thread < evlist->threads->nr; thread++) { | |
554 | if (evlist->mmap[thread].base != NULL) { | |
555 | munmap(evlist->mmap[thread].base, evlist->mmap_len); | |
556 | evlist->mmap[thread].base = NULL; | |
557 | } | |
558 | } | |
559 | return -1; | |
560 | } | |
561 | ||
f8a95309 ACM |
562 | /** perf_evlist__mmap - Create per cpu maps to receive events |
563 | * | |
564 | * @evlist - list of events | |
f8a95309 ACM |
565 | * @pages - map length in pages |
566 | * @overwrite - overwrite older events? | |
567 | * | |
568 | * If overwrite is false the user needs to signal event consuption using: | |
569 | * | |
570 | * struct perf_mmap *m = &evlist->mmap[cpu]; | |
571 | * unsigned int head = perf_mmap__read_head(m); | |
572 | * | |
573 | * perf_mmap__write_tail(m, head) | |
7e2ed097 ACM |
574 | * |
575 | * Using perf_evlist__read_on_cpu does this automatically. | |
f8a95309 | 576 | */ |
50a682ce ACM |
577 | int perf_evlist__mmap(struct perf_evlist *evlist, unsigned int pages, |
578 | bool overwrite) | |
f8a95309 ACM |
579 | { |
580 | unsigned int page_size = sysconf(_SC_PAGE_SIZE); | |
aece948f | 581 | struct perf_evsel *evsel; |
7e2ed097 ACM |
582 | const struct cpu_map *cpus = evlist->cpus; |
583 | const struct thread_map *threads = evlist->threads; | |
50a682ce ACM |
584 | int prot = PROT_READ | (overwrite ? 0 : PROT_WRITE), mask; |
585 | ||
586 | /* 512 kiB: default amount of unprivileged mlocked memory */ | |
587 | if (pages == UINT_MAX) | |
588 | pages = (512 * 1024) / page_size; | |
41d0d933 NE |
589 | else if (!is_power_of_2(pages)) |
590 | return -EINVAL; | |
50a682ce ACM |
591 | |
592 | mask = pages * page_size - 1; | |
f8a95309 | 593 | |
7e2ed097 | 594 | if (evlist->mmap == NULL && perf_evlist__alloc_mmap(evlist) < 0) |
f8a95309 ACM |
595 | return -ENOMEM; |
596 | ||
7e2ed097 | 597 | if (evlist->pollfd == NULL && perf_evlist__alloc_pollfd(evlist) < 0) |
f8a95309 ACM |
598 | return -ENOMEM; |
599 | ||
600 | evlist->overwrite = overwrite; | |
601 | evlist->mmap_len = (pages + 1) * page_size; | |
f8a95309 ACM |
602 | |
603 | list_for_each_entry(evsel, &evlist->entries, node) { | |
604 | if ((evsel->attr.read_format & PERF_FORMAT_ID) && | |
a91e5431 | 605 | evsel->sample_id == NULL && |
f8a95309 ACM |
606 | perf_evsel__alloc_id(evsel, cpus->nr, threads->nr) < 0) |
607 | return -ENOMEM; | |
f8a95309 ACM |
608 | } |
609 | ||
aece948f ACM |
610 | if (evlist->cpus->map[0] == -1) |
611 | return perf_evlist__mmap_per_thread(evlist, prot, mask); | |
f8a95309 | 612 | |
aece948f | 613 | return perf_evlist__mmap_per_cpu(evlist, prot, mask); |
f8a95309 | 614 | } |
7e2ed097 | 615 | |
b809ac10 NK |
616 | int perf_evlist__create_maps(struct perf_evlist *evlist, |
617 | struct perf_target *target) | |
7e2ed097 | 618 | { |
b809ac10 NK |
619 | evlist->threads = thread_map__new_str(target->pid, target->tid, |
620 | target->uid); | |
7e2ed097 ACM |
621 | |
622 | if (evlist->threads == NULL) | |
623 | return -1; | |
624 | ||
879d77d0 | 625 | if (perf_target__has_task(target)) |
d67356e7 | 626 | evlist->cpus = cpu_map__dummy_new(); |
d1cb9fce NK |
627 | else if (!perf_target__has_cpu(target) && !target->uses_mmap) |
628 | evlist->cpus = cpu_map__dummy_new(); | |
879d77d0 NK |
629 | else |
630 | evlist->cpus = cpu_map__new(target->cpu_list); | |
7e2ed097 ACM |
631 | |
632 | if (evlist->cpus == NULL) | |
633 | goto out_delete_threads; | |
634 | ||
635 | return 0; | |
636 | ||
637 | out_delete_threads: | |
638 | thread_map__delete(evlist->threads); | |
639 | return -1; | |
640 | } | |
641 | ||
642 | void perf_evlist__delete_maps(struct perf_evlist *evlist) | |
643 | { | |
644 | cpu_map__delete(evlist->cpus); | |
645 | thread_map__delete(evlist->threads); | |
646 | evlist->cpus = NULL; | |
647 | evlist->threads = NULL; | |
648 | } | |
0a102479 FW |
649 | |
650 | int perf_evlist__set_filters(struct perf_evlist *evlist) | |
651 | { | |
652 | const struct thread_map *threads = evlist->threads; | |
653 | const struct cpu_map *cpus = evlist->cpus; | |
654 | struct perf_evsel *evsel; | |
655 | char *filter; | |
656 | int thread; | |
657 | int cpu; | |
658 | int err; | |
659 | int fd; | |
660 | ||
661 | list_for_each_entry(evsel, &evlist->entries, node) { | |
662 | filter = evsel->filter; | |
663 | if (!filter) | |
664 | continue; | |
665 | for (cpu = 0; cpu < cpus->nr; cpu++) { | |
666 | for (thread = 0; thread < threads->nr; thread++) { | |
667 | fd = FD(evsel, cpu, thread); | |
668 | err = ioctl(fd, PERF_EVENT_IOC_SET_FILTER, filter); | |
669 | if (err) | |
670 | return err; | |
671 | } | |
672 | } | |
673 | } | |
674 | ||
675 | return 0; | |
676 | } | |
74429964 | 677 | |
c2a70653 | 678 | bool perf_evlist__valid_sample_type(const struct perf_evlist *evlist) |
74429964 | 679 | { |
c2a70653 ACM |
680 | struct perf_evsel *pos, *first; |
681 | ||
682 | pos = first = list_entry(evlist->entries.next, struct perf_evsel, node); | |
683 | ||
684 | list_for_each_entry_continue(pos, &evlist->entries, node) { | |
685 | if (first->attr.sample_type != pos->attr.sample_type) | |
686 | return false; | |
74429964 FW |
687 | } |
688 | ||
c2a70653 | 689 | return true; |
74429964 FW |
690 | } |
691 | ||
c2a70653 ACM |
692 | u64 perf_evlist__sample_type(const struct perf_evlist *evlist) |
693 | { | |
694 | struct perf_evsel *first; | |
695 | ||
696 | first = list_entry(evlist->entries.next, struct perf_evsel, node); | |
697 | return first->attr.sample_type; | |
698 | } | |
699 | ||
81e36bff ACM |
700 | u16 perf_evlist__id_hdr_size(const struct perf_evlist *evlist) |
701 | { | |
702 | struct perf_evsel *first; | |
703 | struct perf_sample *data; | |
704 | u64 sample_type; | |
705 | u16 size = 0; | |
706 | ||
707 | first = list_entry(evlist->entries.next, struct perf_evsel, node); | |
708 | ||
709 | if (!first->attr.sample_id_all) | |
710 | goto out; | |
711 | ||
712 | sample_type = first->attr.sample_type; | |
713 | ||
714 | if (sample_type & PERF_SAMPLE_TID) | |
715 | size += sizeof(data->tid) * 2; | |
716 | ||
717 | if (sample_type & PERF_SAMPLE_TIME) | |
718 | size += sizeof(data->time); | |
719 | ||
720 | if (sample_type & PERF_SAMPLE_ID) | |
721 | size += sizeof(data->id); | |
722 | ||
723 | if (sample_type & PERF_SAMPLE_STREAM_ID) | |
724 | size += sizeof(data->stream_id); | |
725 | ||
726 | if (sample_type & PERF_SAMPLE_CPU) | |
727 | size += sizeof(data->cpu) * 2; | |
728 | out: | |
729 | return size; | |
730 | } | |
731 | ||
c2a70653 | 732 | bool perf_evlist__valid_sample_id_all(const struct perf_evlist *evlist) |
74429964 | 733 | { |
c2a70653 ACM |
734 | struct perf_evsel *pos, *first; |
735 | ||
736 | pos = first = list_entry(evlist->entries.next, struct perf_evsel, node); | |
737 | ||
738 | list_for_each_entry_continue(pos, &evlist->entries, node) { | |
739 | if (first->attr.sample_id_all != pos->attr.sample_id_all) | |
740 | return false; | |
74429964 FW |
741 | } |
742 | ||
c2a70653 ACM |
743 | return true; |
744 | } | |
745 | ||
746 | bool perf_evlist__sample_id_all(const struct perf_evlist *evlist) | |
747 | { | |
748 | struct perf_evsel *first; | |
749 | ||
750 | first = list_entry(evlist->entries.next, struct perf_evsel, node); | |
751 | return first->attr.sample_id_all; | |
74429964 | 752 | } |
81cce8de ACM |
753 | |
754 | void perf_evlist__set_selected(struct perf_evlist *evlist, | |
755 | struct perf_evsel *evsel) | |
756 | { | |
757 | evlist->selected = evsel; | |
758 | } | |
727ab04e ACM |
759 | |
760 | int perf_evlist__open(struct perf_evlist *evlist, bool group) | |
761 | { | |
762 | struct perf_evsel *evsel, *first; | |
763 | int err, ncpus, nthreads; | |
764 | ||
765 | first = list_entry(evlist->entries.next, struct perf_evsel, node); | |
766 | ||
767 | list_for_each_entry(evsel, &evlist->entries, node) { | |
768 | struct xyarray *group_fd = NULL; | |
769 | ||
770 | if (group && evsel != first) | |
771 | group_fd = first->fd; | |
772 | ||
773 | err = perf_evsel__open(evsel, evlist->cpus, evlist->threads, | |
774 | group, group_fd); | |
775 | if (err < 0) | |
776 | goto out_err; | |
777 | } | |
778 | ||
779 | return 0; | |
780 | out_err: | |
781 | ncpus = evlist->cpus ? evlist->cpus->nr : 1; | |
782 | nthreads = evlist->threads ? evlist->threads->nr : 1; | |
783 | ||
784 | list_for_each_entry_reverse(evsel, &evlist->entries, node) | |
785 | perf_evsel__close(evsel, ncpus, nthreads); | |
786 | ||
41c21a68 | 787 | errno = -err; |
727ab04e ACM |
788 | return err; |
789 | } | |
35b9d88e ACM |
790 | |
791 | int perf_evlist__prepare_workload(struct perf_evlist *evlist, | |
792 | struct perf_record_opts *opts, | |
793 | const char *argv[]) | |
794 | { | |
795 | int child_ready_pipe[2], go_pipe[2]; | |
796 | char bf; | |
797 | ||
798 | if (pipe(child_ready_pipe) < 0) { | |
799 | perror("failed to create 'ready' pipe"); | |
800 | return -1; | |
801 | } | |
802 | ||
803 | if (pipe(go_pipe) < 0) { | |
804 | perror("failed to create 'go' pipe"); | |
805 | goto out_close_ready_pipe; | |
806 | } | |
807 | ||
808 | evlist->workload.pid = fork(); | |
809 | if (evlist->workload.pid < 0) { | |
810 | perror("failed to fork"); | |
811 | goto out_close_pipes; | |
812 | } | |
813 | ||
814 | if (!evlist->workload.pid) { | |
815 | if (opts->pipe_output) | |
816 | dup2(2, 1); | |
817 | ||
818 | close(child_ready_pipe[0]); | |
819 | close(go_pipe[1]); | |
820 | fcntl(go_pipe[0], F_SETFD, FD_CLOEXEC); | |
821 | ||
822 | /* | |
823 | * Do a dummy execvp to get the PLT entry resolved, | |
824 | * so we avoid the resolver overhead on the real | |
825 | * execvp call. | |
826 | */ | |
827 | execvp("", (char **)argv); | |
828 | ||
829 | /* | |
830 | * Tell the parent we're ready to go | |
831 | */ | |
832 | close(child_ready_pipe[1]); | |
833 | ||
834 | /* | |
835 | * Wait until the parent tells us to go. | |
836 | */ | |
837 | if (read(go_pipe[0], &bf, 1) == -1) | |
838 | perror("unable to read pipe"); | |
839 | ||
840 | execvp(argv[0], (char **)argv); | |
841 | ||
842 | perror(argv[0]); | |
843 | kill(getppid(), SIGUSR1); | |
844 | exit(-1); | |
845 | } | |
846 | ||
d67356e7 | 847 | if (perf_target__none(&opts->target)) |
35b9d88e ACM |
848 | evlist->threads->map[0] = evlist->workload.pid; |
849 | ||
850 | close(child_ready_pipe[1]); | |
851 | close(go_pipe[0]); | |
852 | /* | |
853 | * wait for child to settle | |
854 | */ | |
855 | if (read(child_ready_pipe[0], &bf, 1) == -1) { | |
856 | perror("unable to read pipe"); | |
857 | goto out_close_pipes; | |
858 | } | |
859 | ||
860 | evlist->workload.cork_fd = go_pipe[1]; | |
861 | close(child_ready_pipe[0]); | |
862 | return 0; | |
863 | ||
864 | out_close_pipes: | |
865 | close(go_pipe[0]); | |
866 | close(go_pipe[1]); | |
867 | out_close_ready_pipe: | |
868 | close(child_ready_pipe[0]); | |
869 | close(child_ready_pipe[1]); | |
870 | return -1; | |
871 | } | |
872 | ||
873 | int perf_evlist__start_workload(struct perf_evlist *evlist) | |
874 | { | |
875 | if (evlist->workload.cork_fd > 0) { | |
876 | /* | |
877 | * Remove the cork, let it rip! | |
878 | */ | |
879 | return close(evlist->workload.cork_fd); | |
880 | } | |
881 | ||
882 | return 0; | |
883 | } | |
cb0b29e0 ACM |
884 | |
885 | int perf_evlist__parse_sample(struct perf_evlist *evlist, | |
886 | const union perf_event *event, | |
887 | struct perf_sample *sample, bool swapped) | |
888 | { | |
889 | struct perf_evsel *e = list_entry(evlist->entries.next, struct perf_evsel, node); | |
890 | return perf_event__parse_sample(event, e->attr.sample_type, e->sample_size, | |
891 | e->attr.sample_id_all, sample, swapped); | |
892 | } |