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 | 9 | #include "util.h" |
956fa571 | 10 | #include <api/fs/fs.h> |
a43783ae | 11 | #include <errno.h> |
fd20e811 | 12 | #include <inttypes.h> |
5c581041 | 13 | #include <poll.h> |
f8a95309 ACM |
14 | #include "cpumap.h" |
15 | #include "thread_map.h" | |
12864b31 | 16 | #include "target.h" |
361c99a6 ACM |
17 | #include "evlist.h" |
18 | #include "evsel.h" | |
e3e1a54f | 19 | #include "debug.h" |
58db1d6e | 20 | #include "units.h" |
54cc54de | 21 | #include "asm/bug.h" |
9607ad3a | 22 | #include <signal.h> |
35b9d88e | 23 | #include <unistd.h> |
361c99a6 | 24 | |
50d08e47 | 25 | #include "parse-events.h" |
4b6ab94e | 26 | #include <subcmd/parse-options.h> |
50d08e47 | 27 | |
bafae98e | 28 | #include <fcntl.h> |
86a5e0c2 | 29 | #include <sys/ioctl.h> |
f8a95309 ACM |
30 | #include <sys/mman.h> |
31 | ||
70db7533 ACM |
32 | #include <linux/bitops.h> |
33 | #include <linux/hash.h> | |
0389cd1f | 34 | #include <linux/log2.h> |
8dd2a131 | 35 | #include <linux/err.h> |
70db7533 | 36 | |
f8a95309 | 37 | #define FD(e, x, y) (*(int *)xyarray__entry(e->fd, x, y)) |
a91e5431 | 38 | #define SID(e, x, y) xyarray__entry(e->sample_id, x, y) |
f8a95309 | 39 | |
7e2ed097 ACM |
40 | void perf_evlist__init(struct perf_evlist *evlist, struct cpu_map *cpus, |
41 | struct thread_map *threads) | |
ef1d1af2 ACM |
42 | { |
43 | int i; | |
44 | ||
45 | for (i = 0; i < PERF_EVLIST__HLIST_SIZE; ++i) | |
46 | INIT_HLIST_HEAD(&evlist->heads[i]); | |
47 | INIT_LIST_HEAD(&evlist->entries); | |
7e2ed097 | 48 | perf_evlist__set_maps(evlist, cpus, threads); |
1b85337d | 49 | fdarray__init(&evlist->pollfd, 64); |
35b9d88e | 50 | evlist->workload.pid = -1; |
54cc54de | 51 | evlist->bkw_mmap_state = BKW_MMAP_NOTREADY; |
ef1d1af2 ACM |
52 | } |
53 | ||
334fe7a3 | 54 | struct perf_evlist *perf_evlist__new(void) |
361c99a6 ACM |
55 | { |
56 | struct perf_evlist *evlist = zalloc(sizeof(*evlist)); | |
57 | ||
ef1d1af2 | 58 | if (evlist != NULL) |
334fe7a3 | 59 | perf_evlist__init(evlist, NULL, NULL); |
361c99a6 ACM |
60 | |
61 | return evlist; | |
62 | } | |
63 | ||
b22d54b0 JO |
64 | struct perf_evlist *perf_evlist__new_default(void) |
65 | { | |
66 | struct perf_evlist *evlist = perf_evlist__new(); | |
67 | ||
68 | if (evlist && perf_evlist__add_default(evlist)) { | |
69 | perf_evlist__delete(evlist); | |
70 | evlist = NULL; | |
71 | } | |
72 | ||
73 | return evlist; | |
74 | } | |
75 | ||
5bae0250 ACM |
76 | struct perf_evlist *perf_evlist__new_dummy(void) |
77 | { | |
78 | struct perf_evlist *evlist = perf_evlist__new(); | |
79 | ||
80 | if (evlist && perf_evlist__add_dummy(evlist)) { | |
81 | perf_evlist__delete(evlist); | |
82 | evlist = NULL; | |
83 | } | |
84 | ||
85 | return evlist; | |
86 | } | |
87 | ||
75562573 AH |
88 | /** |
89 | * perf_evlist__set_id_pos - set the positions of event ids. | |
90 | * @evlist: selected event list | |
91 | * | |
92 | * Events with compatible sample types all have the same id_pos | |
93 | * and is_pos. For convenience, put a copy on evlist. | |
94 | */ | |
95 | void perf_evlist__set_id_pos(struct perf_evlist *evlist) | |
96 | { | |
97 | struct perf_evsel *first = perf_evlist__first(evlist); | |
98 | ||
99 | evlist->id_pos = first->id_pos; | |
100 | evlist->is_pos = first->is_pos; | |
101 | } | |
102 | ||
733cd2fe AH |
103 | static void perf_evlist__update_id_pos(struct perf_evlist *evlist) |
104 | { | |
105 | struct perf_evsel *evsel; | |
106 | ||
e5cadb93 | 107 | evlist__for_each_entry(evlist, evsel) |
733cd2fe AH |
108 | perf_evsel__calc_id_pos(evsel); |
109 | ||
110 | perf_evlist__set_id_pos(evlist); | |
111 | } | |
112 | ||
361c99a6 ACM |
113 | static void perf_evlist__purge(struct perf_evlist *evlist) |
114 | { | |
115 | struct perf_evsel *pos, *n; | |
116 | ||
e5cadb93 | 117 | evlist__for_each_entry_safe(evlist, n, pos) { |
361c99a6 | 118 | list_del_init(&pos->node); |
d49e4695 | 119 | pos->evlist = NULL; |
361c99a6 ACM |
120 | perf_evsel__delete(pos); |
121 | } | |
122 | ||
123 | evlist->nr_entries = 0; | |
124 | } | |
125 | ||
ef1d1af2 | 126 | void perf_evlist__exit(struct perf_evlist *evlist) |
361c99a6 | 127 | { |
04662523 | 128 | zfree(&evlist->mmap); |
0b72d69a | 129 | zfree(&evlist->overwrite_mmap); |
1b85337d | 130 | fdarray__exit(&evlist->pollfd); |
ef1d1af2 ACM |
131 | } |
132 | ||
133 | void perf_evlist__delete(struct perf_evlist *evlist) | |
134 | { | |
0b04b3dc ACM |
135 | if (evlist == NULL) |
136 | return; | |
137 | ||
983874d1 | 138 | perf_evlist__munmap(evlist); |
f26e1c7c | 139 | perf_evlist__close(evlist); |
f30a79b0 | 140 | cpu_map__put(evlist->cpus); |
186fbb74 | 141 | thread_map__put(evlist->threads); |
03ad9747 ACM |
142 | evlist->cpus = NULL; |
143 | evlist->threads = NULL; | |
ef1d1af2 ACM |
144 | perf_evlist__purge(evlist); |
145 | perf_evlist__exit(evlist); | |
361c99a6 ACM |
146 | free(evlist); |
147 | } | |
148 | ||
adc0c3e8 AH |
149 | static void __perf_evlist__propagate_maps(struct perf_evlist *evlist, |
150 | struct perf_evsel *evsel) | |
151 | { | |
152 | /* | |
153 | * We already have cpus for evsel (via PMU sysfs) so | |
154 | * keep it, if there's no target cpu list defined. | |
155 | */ | |
156 | if (!evsel->own_cpus || evlist->has_user_cpus) { | |
157 | cpu_map__put(evsel->cpus); | |
158 | evsel->cpus = cpu_map__get(evlist->cpus); | |
159 | } else if (evsel->cpus != evsel->own_cpus) { | |
160 | cpu_map__put(evsel->cpus); | |
161 | evsel->cpus = cpu_map__get(evsel->own_cpus); | |
162 | } | |
163 | ||
164 | thread_map__put(evsel->threads); | |
165 | evsel->threads = thread_map__get(evlist->threads); | |
166 | } | |
167 | ||
168 | static void perf_evlist__propagate_maps(struct perf_evlist *evlist) | |
169 | { | |
170 | struct perf_evsel *evsel; | |
171 | ||
e5cadb93 | 172 | evlist__for_each_entry(evlist, evsel) |
adc0c3e8 AH |
173 | __perf_evlist__propagate_maps(evlist, evsel); |
174 | } | |
175 | ||
361c99a6 ACM |
176 | void perf_evlist__add(struct perf_evlist *evlist, struct perf_evsel *entry) |
177 | { | |
d49e4695 | 178 | entry->evlist = evlist; |
361c99a6 | 179 | list_add_tail(&entry->node, &evlist->entries); |
ef503831 | 180 | entry->idx = evlist->nr_entries; |
60b0896c | 181 | entry->tracking = !entry->idx; |
ef503831 | 182 | |
75562573 AH |
183 | if (!evlist->nr_entries++) |
184 | perf_evlist__set_id_pos(evlist); | |
44c42d71 AH |
185 | |
186 | __perf_evlist__propagate_maps(evlist, entry); | |
361c99a6 ACM |
187 | } |
188 | ||
4768230a AH |
189 | void perf_evlist__remove(struct perf_evlist *evlist, struct perf_evsel *evsel) |
190 | { | |
191 | evsel->evlist = NULL; | |
192 | list_del_init(&evsel->node); | |
193 | evlist->nr_entries -= 1; | |
194 | } | |
195 | ||
0529bc1f | 196 | void perf_evlist__splice_list_tail(struct perf_evlist *evlist, |
f114d6ef | 197 | struct list_head *list) |
50d08e47 | 198 | { |
f114d6ef | 199 | struct perf_evsel *evsel, *temp; |
75562573 | 200 | |
e5cadb93 | 201 | __evlist__for_each_entry_safe(list, temp, evsel) { |
f114d6ef AH |
202 | list_del_init(&evsel->node); |
203 | perf_evlist__add(evlist, evsel); | |
204 | } | |
50d08e47 ACM |
205 | } |
206 | ||
63dab225 ACM |
207 | void __perf_evlist__set_leader(struct list_head *list) |
208 | { | |
209 | struct perf_evsel *evsel, *leader; | |
210 | ||
211 | leader = list_entry(list->next, struct perf_evsel, node); | |
97f63e4a NK |
212 | evsel = list_entry(list->prev, struct perf_evsel, node); |
213 | ||
214 | leader->nr_members = evsel->idx - leader->idx + 1; | |
63dab225 | 215 | |
e5cadb93 | 216 | __evlist__for_each_entry(list, evsel) { |
74b2133d | 217 | evsel->leader = leader; |
63dab225 ACM |
218 | } |
219 | } | |
220 | ||
221 | void perf_evlist__set_leader(struct perf_evlist *evlist) | |
6a4bb04c | 222 | { |
97f63e4a NK |
223 | if (evlist->nr_entries) { |
224 | evlist->nr_groups = evlist->nr_entries > 1 ? 1 : 0; | |
63dab225 | 225 | __perf_evlist__set_leader(&evlist->entries); |
97f63e4a | 226 | } |
6a4bb04c JO |
227 | } |
228 | ||
45cf6c33 | 229 | void perf_event_attr__set_max_precise_ip(struct perf_event_attr *attr) |
7f8d1ade ACM |
230 | { |
231 | attr->precise_ip = 3; | |
232 | ||
233 | while (attr->precise_ip != 0) { | |
234 | int fd = sys_perf_event_open(attr, 0, -1, -1, 0); | |
235 | if (fd != -1) { | |
236 | close(fd); | |
237 | break; | |
238 | } | |
239 | --attr->precise_ip; | |
240 | } | |
241 | } | |
242 | ||
db918acb | 243 | int __perf_evlist__add_default(struct perf_evlist *evlist, bool precise) |
361c99a6 | 244 | { |
db918acb | 245 | struct perf_evsel *evsel = perf_evsel__new_cycles(precise); |
1aed2671 | 246 | |
361c99a6 | 247 | if (evsel == NULL) |
7c48dcfd | 248 | return -ENOMEM; |
361c99a6 ACM |
249 | |
250 | perf_evlist__add(evlist, evsel); | |
251 | return 0; | |
252 | } | |
5c581041 | 253 | |
5bae0250 ACM |
254 | int perf_evlist__add_dummy(struct perf_evlist *evlist) |
255 | { | |
256 | struct perf_event_attr attr = { | |
257 | .type = PERF_TYPE_SOFTWARE, | |
258 | .config = PERF_COUNT_SW_DUMMY, | |
259 | .size = sizeof(attr), /* to capture ABI version */ | |
260 | }; | |
640d5175 | 261 | struct perf_evsel *evsel = perf_evsel__new_idx(&attr, evlist->nr_entries); |
5bae0250 ACM |
262 | |
263 | if (evsel == NULL) | |
264 | return -ENOMEM; | |
265 | ||
266 | perf_evlist__add(evlist, evsel); | |
267 | return 0; | |
268 | } | |
269 | ||
e60fc847 ACM |
270 | static int perf_evlist__add_attrs(struct perf_evlist *evlist, |
271 | struct perf_event_attr *attrs, size_t nr_attrs) | |
50d08e47 ACM |
272 | { |
273 | struct perf_evsel *evsel, *n; | |
274 | LIST_HEAD(head); | |
275 | size_t i; | |
276 | ||
277 | for (i = 0; i < nr_attrs; i++) { | |
ef503831 | 278 | evsel = perf_evsel__new_idx(attrs + i, evlist->nr_entries + i); |
50d08e47 ACM |
279 | if (evsel == NULL) |
280 | goto out_delete_partial_list; | |
281 | list_add_tail(&evsel->node, &head); | |
282 | } | |
283 | ||
f114d6ef | 284 | perf_evlist__splice_list_tail(evlist, &head); |
50d08e47 ACM |
285 | |
286 | return 0; | |
287 | ||
288 | out_delete_partial_list: | |
e5cadb93 | 289 | __evlist__for_each_entry_safe(&head, n, evsel) |
50d08e47 ACM |
290 | perf_evsel__delete(evsel); |
291 | return -1; | |
292 | } | |
293 | ||
79695e1b ACM |
294 | int __perf_evlist__add_default_attrs(struct perf_evlist *evlist, |
295 | struct perf_event_attr *attrs, size_t nr_attrs) | |
296 | { | |
297 | size_t i; | |
298 | ||
299 | for (i = 0; i < nr_attrs; i++) | |
300 | event_attr_init(attrs + i); | |
301 | ||
302 | return perf_evlist__add_attrs(evlist, attrs, nr_attrs); | |
303 | } | |
304 | ||
da378962 ACM |
305 | struct perf_evsel * |
306 | perf_evlist__find_tracepoint_by_id(struct perf_evlist *evlist, int id) | |
ee29be62 ACM |
307 | { |
308 | struct perf_evsel *evsel; | |
309 | ||
e5cadb93 | 310 | evlist__for_each_entry(evlist, evsel) { |
ee29be62 ACM |
311 | if (evsel->attr.type == PERF_TYPE_TRACEPOINT && |
312 | (int)evsel->attr.config == id) | |
313 | return evsel; | |
314 | } | |
315 | ||
316 | return NULL; | |
317 | } | |
318 | ||
a2f2804a DA |
319 | struct perf_evsel * |
320 | perf_evlist__find_tracepoint_by_name(struct perf_evlist *evlist, | |
321 | const char *name) | |
322 | { | |
323 | struct perf_evsel *evsel; | |
324 | ||
e5cadb93 | 325 | evlist__for_each_entry(evlist, evsel) { |
a2f2804a DA |
326 | if ((evsel->attr.type == PERF_TYPE_TRACEPOINT) && |
327 | (strcmp(evsel->name, name) == 0)) | |
328 | return evsel; | |
329 | } | |
330 | ||
331 | return NULL; | |
332 | } | |
333 | ||
39876e7d ACM |
334 | int perf_evlist__add_newtp(struct perf_evlist *evlist, |
335 | const char *sys, const char *name, void *handler) | |
336 | { | |
ef503831 | 337 | struct perf_evsel *evsel = perf_evsel__newtp(sys, name); |
39876e7d | 338 | |
8dd2a131 | 339 | if (IS_ERR(evsel)) |
39876e7d ACM |
340 | return -1; |
341 | ||
744a9719 | 342 | evsel->handler = handler; |
39876e7d ACM |
343 | perf_evlist__add(evlist, evsel); |
344 | return 0; | |
345 | } | |
346 | ||
bf8e8f4b AH |
347 | static int perf_evlist__nr_threads(struct perf_evlist *evlist, |
348 | struct perf_evsel *evsel) | |
349 | { | |
350 | if (evsel->system_wide) | |
351 | return 1; | |
352 | else | |
353 | return thread_map__nr(evlist->threads); | |
354 | } | |
355 | ||
4152ab37 ACM |
356 | void perf_evlist__disable(struct perf_evlist *evlist) |
357 | { | |
4152ab37 | 358 | struct perf_evsel *pos; |
3e27c920 | 359 | |
e5cadb93 | 360 | evlist__for_each_entry(evlist, pos) { |
3e27c920 JO |
361 | if (!perf_evsel__is_group_leader(pos) || !pos->fd) |
362 | continue; | |
363 | perf_evsel__disable(pos); | |
4152ab37 | 364 | } |
2b56bcfb ACM |
365 | |
366 | evlist->enabled = false; | |
4152ab37 ACM |
367 | } |
368 | ||
764e16a3 DA |
369 | void perf_evlist__enable(struct perf_evlist *evlist) |
370 | { | |
764e16a3 | 371 | struct perf_evsel *pos; |
3e27c920 | 372 | |
e5cadb93 | 373 | evlist__for_each_entry(evlist, pos) { |
3e27c920 JO |
374 | if (!perf_evsel__is_group_leader(pos) || !pos->fd) |
375 | continue; | |
376 | perf_evsel__enable(pos); | |
764e16a3 | 377 | } |
2b56bcfb ACM |
378 | |
379 | evlist->enabled = true; | |
380 | } | |
381 | ||
382 | void perf_evlist__toggle_enable(struct perf_evlist *evlist) | |
383 | { | |
384 | (evlist->enabled ? perf_evlist__disable : perf_evlist__enable)(evlist); | |
764e16a3 DA |
385 | } |
386 | ||
1c65056c AH |
387 | static int perf_evlist__enable_event_cpu(struct perf_evlist *evlist, |
388 | struct perf_evsel *evsel, int cpu) | |
389 | { | |
18ef15c6 | 390 | int thread; |
1c65056c AH |
391 | int nr_threads = perf_evlist__nr_threads(evlist, evsel); |
392 | ||
393 | if (!evsel->fd) | |
394 | return -EINVAL; | |
395 | ||
396 | for (thread = 0; thread < nr_threads; thread++) { | |
18ef15c6 | 397 | int err = ioctl(FD(evsel, cpu, thread), PERF_EVENT_IOC_ENABLE, 0); |
1c65056c AH |
398 | if (err) |
399 | return err; | |
400 | } | |
401 | return 0; | |
402 | } | |
403 | ||
404 | static int perf_evlist__enable_event_thread(struct perf_evlist *evlist, | |
405 | struct perf_evsel *evsel, | |
406 | int thread) | |
407 | { | |
18ef15c6 | 408 | int cpu; |
1c65056c AH |
409 | int nr_cpus = cpu_map__nr(evlist->cpus); |
410 | ||
411 | if (!evsel->fd) | |
412 | return -EINVAL; | |
413 | ||
414 | for (cpu = 0; cpu < nr_cpus; cpu++) { | |
18ef15c6 | 415 | int err = ioctl(FD(evsel, cpu, thread), PERF_EVENT_IOC_ENABLE, 0); |
1c65056c AH |
416 | if (err) |
417 | return err; | |
418 | } | |
419 | return 0; | |
420 | } | |
421 | ||
422 | int perf_evlist__enable_event_idx(struct perf_evlist *evlist, | |
423 | struct perf_evsel *evsel, int idx) | |
424 | { | |
425 | bool per_cpu_mmaps = !cpu_map__empty(evlist->cpus); | |
426 | ||
427 | if (per_cpu_mmaps) | |
428 | return perf_evlist__enable_event_cpu(evlist, evsel, idx); | |
429 | else | |
430 | return perf_evlist__enable_event_thread(evlist, evsel, idx); | |
431 | } | |
432 | ||
ad6765dd | 433 | int perf_evlist__alloc_pollfd(struct perf_evlist *evlist) |
5c581041 | 434 | { |
b3a319d5 NK |
435 | int nr_cpus = cpu_map__nr(evlist->cpus); |
436 | int nr_threads = thread_map__nr(evlist->threads); | |
bf8e8f4b AH |
437 | int nfds = 0; |
438 | struct perf_evsel *evsel; | |
439 | ||
e5cadb93 | 440 | evlist__for_each_entry(evlist, evsel) { |
bf8e8f4b AH |
441 | if (evsel->system_wide) |
442 | nfds += nr_cpus; | |
443 | else | |
444 | nfds += nr_cpus * nr_threads; | |
445 | } | |
446 | ||
1b85337d ACM |
447 | if (fdarray__available_entries(&evlist->pollfd) < nfds && |
448 | fdarray__grow(&evlist->pollfd, nfds) < 0) | |
ad6765dd ACM |
449 | return -ENOMEM; |
450 | ||
451 | return 0; | |
5c581041 | 452 | } |
70082dd9 | 453 | |
4876075b WN |
454 | static int __perf_evlist__add_pollfd(struct perf_evlist *evlist, int fd, |
455 | struct perf_mmap *map, short revent) | |
e4b356b5 | 456 | { |
f3058a1c | 457 | int pos = fdarray__add(&evlist->pollfd, fd, revent | POLLERR | POLLHUP); |
e4b356b5 ACM |
458 | /* |
459 | * Save the idx so that when we filter out fds POLLHUP'ed we can | |
460 | * close the associated evlist->mmap[] entry. | |
461 | */ | |
462 | if (pos >= 0) { | |
4876075b | 463 | evlist->pollfd.priv[pos].ptr = map; |
e4b356b5 ACM |
464 | |
465 | fcntl(fd, F_SETFL, O_NONBLOCK); | |
466 | } | |
467 | ||
468 | return pos; | |
469 | } | |
470 | ||
ad6765dd | 471 | int perf_evlist__add_pollfd(struct perf_evlist *evlist, int fd) |
70082dd9 | 472 | { |
4876075b | 473 | return __perf_evlist__add_pollfd(evlist, fd, NULL, POLLIN); |
e4b356b5 ACM |
474 | } |
475 | ||
258e4bfc WN |
476 | static void perf_evlist__munmap_filtered(struct fdarray *fda, int fd, |
477 | void *arg __maybe_unused) | |
e4b356b5 | 478 | { |
4876075b | 479 | struct perf_mmap *map = fda->priv[fd].ptr; |
1b85337d | 480 | |
4876075b WN |
481 | if (map) |
482 | perf_mmap__put(map); | |
70082dd9 | 483 | } |
70db7533 | 484 | |
1ddec7f0 ACM |
485 | int perf_evlist__filter_pollfd(struct perf_evlist *evlist, short revents_and_mask) |
486 | { | |
e4b356b5 | 487 | return fdarray__filter(&evlist->pollfd, revents_and_mask, |
258e4bfc | 488 | perf_evlist__munmap_filtered, NULL); |
1ddec7f0 ACM |
489 | } |
490 | ||
f66a889d ACM |
491 | int perf_evlist__poll(struct perf_evlist *evlist, int timeout) |
492 | { | |
1b85337d | 493 | return fdarray__poll(&evlist->pollfd, timeout); |
f66a889d ACM |
494 | } |
495 | ||
a91e5431 ACM |
496 | static void perf_evlist__id_hash(struct perf_evlist *evlist, |
497 | struct perf_evsel *evsel, | |
498 | int cpu, int thread, u64 id) | |
3d3b5e95 ACM |
499 | { |
500 | int hash; | |
501 | struct perf_sample_id *sid = SID(evsel, cpu, thread); | |
502 | ||
503 | sid->id = id; | |
504 | sid->evsel = evsel; | |
505 | hash = hash_64(sid->id, PERF_EVLIST__HLIST_BITS); | |
506 | hlist_add_head(&sid->node, &evlist->heads[hash]); | |
507 | } | |
508 | ||
a91e5431 ACM |
509 | void perf_evlist__id_add(struct perf_evlist *evlist, struct perf_evsel *evsel, |
510 | int cpu, int thread, u64 id) | |
511 | { | |
512 | perf_evlist__id_hash(evlist, evsel, cpu, thread, id); | |
513 | evsel->id[evsel->ids++] = id; | |
514 | } | |
515 | ||
1c59612d JO |
516 | int perf_evlist__id_add_fd(struct perf_evlist *evlist, |
517 | struct perf_evsel *evsel, | |
518 | int cpu, int thread, int fd) | |
f8a95309 | 519 | { |
f8a95309 | 520 | u64 read_data[4] = { 0, }; |
3d3b5e95 | 521 | int id_idx = 1; /* The first entry is the counter value */ |
e2b5abe0 JO |
522 | u64 id; |
523 | int ret; | |
524 | ||
525 | ret = ioctl(fd, PERF_EVENT_IOC_ID, &id); | |
526 | if (!ret) | |
527 | goto add; | |
528 | ||
529 | if (errno != ENOTTY) | |
530 | return -1; | |
531 | ||
532 | /* Legacy way to get event id.. All hail to old kernels! */ | |
f8a95309 | 533 | |
c4861afe JO |
534 | /* |
535 | * This way does not work with group format read, so bail | |
536 | * out in that case. | |
537 | */ | |
538 | if (perf_evlist__read_format(evlist) & PERF_FORMAT_GROUP) | |
539 | return -1; | |
540 | ||
f8a95309 ACM |
541 | if (!(evsel->attr.read_format & PERF_FORMAT_ID) || |
542 | read(fd, &read_data, sizeof(read_data)) == -1) | |
543 | return -1; | |
544 | ||
545 | if (evsel->attr.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) | |
546 | ++id_idx; | |
547 | if (evsel->attr.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) | |
548 | ++id_idx; | |
549 | ||
e2b5abe0 JO |
550 | id = read_data[id_idx]; |
551 | ||
552 | add: | |
553 | perf_evlist__id_add(evlist, evsel, cpu, thread, id); | |
f8a95309 ACM |
554 | return 0; |
555 | } | |
556 | ||
3c659eed AH |
557 | static void perf_evlist__set_sid_idx(struct perf_evlist *evlist, |
558 | struct perf_evsel *evsel, int idx, int cpu, | |
559 | int thread) | |
560 | { | |
561 | struct perf_sample_id *sid = SID(evsel, cpu, thread); | |
562 | sid->idx = idx; | |
563 | if (evlist->cpus && cpu >= 0) | |
564 | sid->cpu = evlist->cpus->map[cpu]; | |
565 | else | |
566 | sid->cpu = -1; | |
567 | if (!evsel->system_wide && evlist->threads && thread >= 0) | |
e13798c7 | 568 | sid->tid = thread_map__pid(evlist->threads, thread); |
3c659eed AH |
569 | else |
570 | sid->tid = -1; | |
571 | } | |
572 | ||
932a3594 | 573 | struct perf_sample_id *perf_evlist__id2sid(struct perf_evlist *evlist, u64 id) |
70db7533 ACM |
574 | { |
575 | struct hlist_head *head; | |
70db7533 ACM |
576 | struct perf_sample_id *sid; |
577 | int hash; | |
578 | ||
70db7533 ACM |
579 | hash = hash_64(id, PERF_EVLIST__HLIST_BITS); |
580 | head = &evlist->heads[hash]; | |
581 | ||
b67bfe0d | 582 | hlist_for_each_entry(sid, head, node) |
70db7533 | 583 | if (sid->id == id) |
932a3594 JO |
584 | return sid; |
585 | ||
586 | return NULL; | |
587 | } | |
588 | ||
589 | struct perf_evsel *perf_evlist__id2evsel(struct perf_evlist *evlist, u64 id) | |
590 | { | |
591 | struct perf_sample_id *sid; | |
592 | ||
05169df5 | 593 | if (evlist->nr_entries == 1 || !id) |
932a3594 JO |
594 | return perf_evlist__first(evlist); |
595 | ||
596 | sid = perf_evlist__id2sid(evlist, id); | |
597 | if (sid) | |
598 | return sid->evsel; | |
30e68bcc NK |
599 | |
600 | if (!perf_evlist__sample_id_all(evlist)) | |
0c21f736 | 601 | return perf_evlist__first(evlist); |
30e68bcc | 602 | |
70db7533 ACM |
603 | return NULL; |
604 | } | |
04391deb | 605 | |
dddcf6ab AH |
606 | struct perf_evsel *perf_evlist__id2evsel_strict(struct perf_evlist *evlist, |
607 | u64 id) | |
608 | { | |
609 | struct perf_sample_id *sid; | |
610 | ||
611 | if (!id) | |
612 | return NULL; | |
613 | ||
614 | sid = perf_evlist__id2sid(evlist, id); | |
615 | if (sid) | |
616 | return sid->evsel; | |
617 | ||
618 | return NULL; | |
619 | } | |
620 | ||
75562573 AH |
621 | static int perf_evlist__event2id(struct perf_evlist *evlist, |
622 | union perf_event *event, u64 *id) | |
623 | { | |
624 | const u64 *array = event->sample.array; | |
625 | ssize_t n; | |
626 | ||
627 | n = (event->header.size - sizeof(event->header)) >> 3; | |
628 | ||
629 | if (event->header.type == PERF_RECORD_SAMPLE) { | |
630 | if (evlist->id_pos >= n) | |
631 | return -1; | |
632 | *id = array[evlist->id_pos]; | |
633 | } else { | |
634 | if (evlist->is_pos > n) | |
635 | return -1; | |
636 | n -= evlist->is_pos; | |
637 | *id = array[n]; | |
638 | } | |
639 | return 0; | |
640 | } | |
641 | ||
7cb5c5ac JO |
642 | struct perf_evsel *perf_evlist__event2evsel(struct perf_evlist *evlist, |
643 | union perf_event *event) | |
75562573 | 644 | { |
98be6966 | 645 | struct perf_evsel *first = perf_evlist__first(evlist); |
75562573 AH |
646 | struct hlist_head *head; |
647 | struct perf_sample_id *sid; | |
648 | int hash; | |
649 | u64 id; | |
650 | ||
651 | if (evlist->nr_entries == 1) | |
98be6966 AH |
652 | return first; |
653 | ||
654 | if (!first->attr.sample_id_all && | |
655 | event->header.type != PERF_RECORD_SAMPLE) | |
656 | return first; | |
75562573 AH |
657 | |
658 | if (perf_evlist__event2id(evlist, event, &id)) | |
659 | return NULL; | |
660 | ||
661 | /* Synthesized events have an id of zero */ | |
662 | if (!id) | |
98be6966 | 663 | return first; |
75562573 AH |
664 | |
665 | hash = hash_64(id, PERF_EVLIST__HLIST_BITS); | |
666 | head = &evlist->heads[hash]; | |
667 | ||
668 | hlist_for_each_entry(sid, head, node) { | |
669 | if (sid->id == id) | |
670 | return sid->evsel; | |
671 | } | |
672 | return NULL; | |
673 | } | |
674 | ||
65aea233 WN |
675 | static int perf_evlist__set_paused(struct perf_evlist *evlist, bool value) |
676 | { | |
677 | int i; | |
678 | ||
0b72d69a | 679 | if (!evlist->overwrite_mmap) |
078c3386 WN |
680 | return 0; |
681 | ||
65aea233 | 682 | for (i = 0; i < evlist->nr_mmaps; i++) { |
0b72d69a | 683 | int fd = evlist->overwrite_mmap[i].fd; |
65aea233 WN |
684 | int err; |
685 | ||
686 | if (fd < 0) | |
687 | continue; | |
688 | err = ioctl(fd, PERF_EVENT_IOC_PAUSE_OUTPUT, value ? 1 : 0); | |
689 | if (err) | |
690 | return err; | |
691 | } | |
692 | return 0; | |
693 | } | |
694 | ||
f6cdff83 | 695 | static int perf_evlist__pause(struct perf_evlist *evlist) |
65aea233 WN |
696 | { |
697 | return perf_evlist__set_paused(evlist, true); | |
698 | } | |
699 | ||
f6cdff83 | 700 | static int perf_evlist__resume(struct perf_evlist *evlist) |
65aea233 WN |
701 | { |
702 | return perf_evlist__set_paused(evlist, false); | |
703 | } | |
704 | ||
a1f72618 | 705 | static void perf_evlist__munmap_nofree(struct perf_evlist *evlist) |
f8a95309 | 706 | { |
aece948f | 707 | int i; |
f8a95309 | 708 | |
b2cb615d WN |
709 | if (evlist->mmap) |
710 | for (i = 0; i < evlist->nr_mmaps; i++) | |
711 | perf_mmap__munmap(&evlist->mmap[i]); | |
983874d1 | 712 | |
0b72d69a | 713 | if (evlist->overwrite_mmap) |
b2cb615d | 714 | for (i = 0; i < evlist->nr_mmaps; i++) |
0b72d69a | 715 | perf_mmap__munmap(&evlist->overwrite_mmap[i]); |
a1f72618 | 716 | } |
aece948f | 717 | |
a1f72618 WN |
718 | void perf_evlist__munmap(struct perf_evlist *evlist) |
719 | { | |
720 | perf_evlist__munmap_nofree(evlist); | |
04662523 | 721 | zfree(&evlist->mmap); |
0b72d69a | 722 | zfree(&evlist->overwrite_mmap); |
f8a95309 ACM |
723 | } |
724 | ||
8db6d6b1 | 725 | static struct perf_mmap *perf_evlist__alloc_mmap(struct perf_evlist *evlist) |
f8a95309 | 726 | { |
d4c6fb36 | 727 | int i; |
8db6d6b1 | 728 | struct perf_mmap *map; |
d4c6fb36 | 729 | |
a14bb7a6 | 730 | evlist->nr_mmaps = cpu_map__nr(evlist->cpus); |
ec1e7e43 | 731 | if (cpu_map__empty(evlist->cpus)) |
b3a319d5 | 732 | evlist->nr_mmaps = thread_map__nr(evlist->threads); |
8db6d6b1 WN |
733 | map = zalloc(evlist->nr_mmaps * sizeof(struct perf_mmap)); |
734 | if (!map) | |
735 | return NULL; | |
946ae1d4 | 736 | |
4738ca30 | 737 | for (i = 0; i < evlist->nr_mmaps; i++) { |
8db6d6b1 | 738 | map[i].fd = -1; |
4738ca30 ACM |
739 | /* |
740 | * When the perf_mmap() call is made we grab one refcount, plus | |
6afad54d | 741 | * one extra to let perf_mmap__consume() get the last |
4738ca30 ACM |
742 | * events after all real references (perf_mmap__get()) are |
743 | * dropped. | |
744 | * | |
745 | * Each PERF_EVENT_IOC_SET_OUTPUT points to this mmap and | |
746 | * thus does perf_mmap__get() on it. | |
747 | */ | |
748 | refcount_set(&map[i].refcnt, 0); | |
749 | } | |
8db6d6b1 | 750 | return map; |
f8a95309 ACM |
751 | } |
752 | ||
f3058a1c WN |
753 | static bool |
754 | perf_evlist__should_poll(struct perf_evlist *evlist __maybe_unused, | |
755 | struct perf_evsel *evsel) | |
756 | { | |
32a951b4 | 757 | if (evsel->attr.write_backward) |
f3058a1c WN |
758 | return false; |
759 | return true; | |
760 | } | |
761 | ||
04e21314 | 762 | static int perf_evlist__mmap_per_evsel(struct perf_evlist *evlist, int idx, |
9f21b815 | 763 | struct mmap_params *mp, int cpu_idx, |
0b72d69a | 764 | int thread, int *_output, int *_output_overwrite) |
aece948f ACM |
765 | { |
766 | struct perf_evsel *evsel; | |
f3058a1c | 767 | int revent; |
9f21b815 | 768 | int evlist_cpu = cpu_map__cpu(evlist->cpus, cpu_idx); |
04e21314 | 769 | |
e5cadb93 | 770 | evlist__for_each_entry(evlist, evsel) { |
078c3386 WN |
771 | struct perf_mmap *maps = evlist->mmap; |
772 | int *output = _output; | |
bf8e8f4b | 773 | int fd; |
9f21b815 | 774 | int cpu; |
bf8e8f4b | 775 | |
71f566a3 | 776 | mp->prot = PROT_READ | PROT_WRITE; |
078c3386 | 777 | if (evsel->attr.write_backward) { |
0b72d69a WN |
778 | output = _output_overwrite; |
779 | maps = evlist->overwrite_mmap; | |
078c3386 WN |
780 | |
781 | if (!maps) { | |
782 | maps = perf_evlist__alloc_mmap(evlist); | |
783 | if (!maps) | |
784 | return -1; | |
0b72d69a | 785 | evlist->overwrite_mmap = maps; |
54cc54de WN |
786 | if (evlist->bkw_mmap_state == BKW_MMAP_NOTREADY) |
787 | perf_evlist__toggle_bkw_mmap(evlist, BKW_MMAP_RUNNING); | |
078c3386 | 788 | } |
71f566a3 | 789 | mp->prot &= ~PROT_WRITE; |
078c3386 | 790 | } |
f3058a1c | 791 | |
bf8e8f4b AH |
792 | if (evsel->system_wide && thread) |
793 | continue; | |
794 | ||
9f21b815 MR |
795 | cpu = cpu_map__idx(evsel->cpus, evlist_cpu); |
796 | if (cpu == -1) | |
797 | continue; | |
798 | ||
bf8e8f4b | 799 | fd = FD(evsel, cpu, thread); |
04e21314 AH |
800 | |
801 | if (*output == -1) { | |
802 | *output = fd; | |
078c3386 WN |
803 | |
804 | if (perf_mmap__mmap(&maps[idx], mp, *output) < 0) | |
04e21314 AH |
805 | return -1; |
806 | } else { | |
807 | if (ioctl(fd, PERF_EVENT_IOC_SET_OUTPUT, *output) != 0) | |
808 | return -1; | |
82396986 | 809 | |
078c3386 | 810 | perf_mmap__get(&maps[idx]); |
04e21314 AH |
811 | } |
812 | ||
f3058a1c WN |
813 | revent = perf_evlist__should_poll(evlist, evsel) ? POLLIN : 0; |
814 | ||
f90d194a AH |
815 | /* |
816 | * The system_wide flag causes a selected event to be opened | |
817 | * always without a pid. Consequently it will never get a | |
818 | * POLLHUP, but it is used for tracking in combination with | |
819 | * other events, so it should not need to be polled anyway. | |
820 | * Therefore don't add it for polling. | |
821 | */ | |
822 | if (!evsel->system_wide && | |
078c3386 WN |
823 | __perf_evlist__add_pollfd(evlist, fd, &maps[idx], revent) < 0) { |
824 | perf_mmap__put(&maps[idx]); | |
ad6765dd | 825 | return -1; |
82396986 | 826 | } |
033fa713 | 827 | |
3c659eed AH |
828 | if (evsel->attr.read_format & PERF_FORMAT_ID) { |
829 | if (perf_evlist__id_add_fd(evlist, evsel, cpu, thread, | |
830 | fd) < 0) | |
831 | return -1; | |
832 | perf_evlist__set_sid_idx(evlist, evsel, idx, cpu, | |
833 | thread); | |
834 | } | |
04e21314 AH |
835 | } |
836 | ||
837 | return 0; | |
838 | } | |
839 | ||
a8a8f3eb AH |
840 | static int perf_evlist__mmap_per_cpu(struct perf_evlist *evlist, |
841 | struct mmap_params *mp) | |
04e21314 | 842 | { |
aece948f | 843 | int cpu, thread; |
b3a319d5 NK |
844 | int nr_cpus = cpu_map__nr(evlist->cpus); |
845 | int nr_threads = thread_map__nr(evlist->threads); | |
aece948f | 846 | |
e3e1a54f | 847 | pr_debug2("perf event ring buffer mmapped per cpu\n"); |
b3a319d5 | 848 | for (cpu = 0; cpu < nr_cpus; cpu++) { |
aece948f | 849 | int output = -1; |
0b72d69a | 850 | int output_overwrite = -1; |
aece948f | 851 | |
718c602d AH |
852 | auxtrace_mmap_params__set_idx(&mp->auxtrace_mp, evlist, cpu, |
853 | true); | |
854 | ||
b3a319d5 | 855 | for (thread = 0; thread < nr_threads; thread++) { |
a8a8f3eb | 856 | if (perf_evlist__mmap_per_evsel(evlist, cpu, mp, cpu, |
0b72d69a | 857 | thread, &output, &output_overwrite)) |
04e21314 | 858 | goto out_unmap; |
aece948f ACM |
859 | } |
860 | } | |
861 | ||
862 | return 0; | |
863 | ||
864 | out_unmap: | |
a1f72618 | 865 | perf_evlist__munmap_nofree(evlist); |
aece948f ACM |
866 | return -1; |
867 | } | |
868 | ||
a8a8f3eb AH |
869 | static int perf_evlist__mmap_per_thread(struct perf_evlist *evlist, |
870 | struct mmap_params *mp) | |
aece948f | 871 | { |
aece948f | 872 | int thread; |
b3a319d5 | 873 | int nr_threads = thread_map__nr(evlist->threads); |
aece948f | 874 | |
e3e1a54f | 875 | pr_debug2("perf event ring buffer mmapped per thread\n"); |
b3a319d5 | 876 | for (thread = 0; thread < nr_threads; thread++) { |
aece948f | 877 | int output = -1; |
0b72d69a | 878 | int output_overwrite = -1; |
aece948f | 879 | |
718c602d AH |
880 | auxtrace_mmap_params__set_idx(&mp->auxtrace_mp, evlist, thread, |
881 | false); | |
882 | ||
a8a8f3eb | 883 | if (perf_evlist__mmap_per_evsel(evlist, thread, mp, 0, thread, |
0b72d69a | 884 | &output, &output_overwrite)) |
04e21314 | 885 | goto out_unmap; |
aece948f ACM |
886 | } |
887 | ||
888 | return 0; | |
889 | ||
890 | out_unmap: | |
a1f72618 | 891 | perf_evlist__munmap_nofree(evlist); |
aece948f ACM |
892 | return -1; |
893 | } | |
894 | ||
f5e7150c | 895 | unsigned long perf_event_mlock_kb_in_pages(void) |
994a1f78 | 896 | { |
f5e7150c ACM |
897 | unsigned long pages; |
898 | int max; | |
8185e881 | 899 | |
f5e7150c ACM |
900 | if (sysctl__read_int("kernel/perf_event_mlock_kb", &max) < 0) { |
901 | /* | |
902 | * Pick a once upon a time good value, i.e. things look | |
903 | * strange since we can't read a sysctl value, but lets not | |
904 | * die yet... | |
905 | */ | |
906 | max = 512; | |
907 | } else { | |
908 | max -= (page_size / 1024); | |
909 | } | |
8185e881 | 910 | |
f5e7150c ACM |
911 | pages = (max * 1024) / page_size; |
912 | if (!is_power_of_2(pages)) | |
913 | pages = rounddown_pow_of_two(pages); | |
914 | ||
915 | return pages; | |
916 | } | |
917 | ||
0c582449 | 918 | size_t perf_evlist__mmap_size(unsigned long pages) |
f5e7150c ACM |
919 | { |
920 | if (pages == UINT_MAX) | |
921 | pages = perf_event_mlock_kb_in_pages(); | |
922 | else if (!is_power_of_2(pages)) | |
994a1f78 JO |
923 | return 0; |
924 | ||
925 | return (pages + 1) * page_size; | |
926 | } | |
927 | ||
33c2dcfd DA |
928 | static long parse_pages_arg(const char *str, unsigned long min, |
929 | unsigned long max) | |
994a1f78 | 930 | { |
2fbe4abe | 931 | unsigned long pages, val; |
27050f53 JO |
932 | static struct parse_tag tags[] = { |
933 | { .tag = 'B', .mult = 1 }, | |
934 | { .tag = 'K', .mult = 1 << 10 }, | |
935 | { .tag = 'M', .mult = 1 << 20 }, | |
936 | { .tag = 'G', .mult = 1 << 30 }, | |
937 | { .tag = 0 }, | |
938 | }; | |
994a1f78 | 939 | |
8973504b | 940 | if (str == NULL) |
33c2dcfd | 941 | return -EINVAL; |
8973504b | 942 | |
27050f53 | 943 | val = parse_tag_value(str, tags); |
2fbe4abe | 944 | if (val != (unsigned long) -1) { |
27050f53 JO |
945 | /* we got file size value */ |
946 | pages = PERF_ALIGN(val, page_size) / page_size; | |
27050f53 JO |
947 | } else { |
948 | /* we got pages count value */ | |
949 | char *eptr; | |
950 | pages = strtoul(str, &eptr, 10); | |
33c2dcfd DA |
951 | if (*eptr != '\0') |
952 | return -EINVAL; | |
994a1f78 JO |
953 | } |
954 | ||
2bcab6c1 | 955 | if (pages == 0 && min == 0) { |
33c2dcfd | 956 | /* leave number of pages at 0 */ |
1dbfa938 | 957 | } else if (!is_power_of_2(pages)) { |
9808143b JO |
958 | char buf[100]; |
959 | ||
33c2dcfd | 960 | /* round pages up to next power of 2 */ |
91529834 | 961 | pages = roundup_pow_of_two(pages); |
1dbfa938 AH |
962 | if (!pages) |
963 | return -EINVAL; | |
9808143b JO |
964 | |
965 | unit_number__scnprintf(buf, sizeof(buf), pages * page_size); | |
966 | pr_info("rounding mmap pages size to %s (%lu pages)\n", | |
967 | buf, pages); | |
2fbe4abe AH |
968 | } |
969 | ||
33c2dcfd DA |
970 | if (pages > max) |
971 | return -EINVAL; | |
972 | ||
973 | return pages; | |
974 | } | |
975 | ||
e9db1310 | 976 | int __perf_evlist__parse_mmap_pages(unsigned int *mmap_pages, const char *str) |
33c2dcfd | 977 | { |
33c2dcfd DA |
978 | unsigned long max = UINT_MAX; |
979 | long pages; | |
980 | ||
f5ae9c42 | 981 | if (max > SIZE_MAX / page_size) |
33c2dcfd DA |
982 | max = SIZE_MAX / page_size; |
983 | ||
984 | pages = parse_pages_arg(str, 1, max); | |
985 | if (pages < 0) { | |
986 | pr_err("Invalid argument for --mmap_pages/-m\n"); | |
994a1f78 JO |
987 | return -1; |
988 | } | |
989 | ||
990 | *mmap_pages = pages; | |
991 | return 0; | |
992 | } | |
993 | ||
e9db1310 AH |
994 | int perf_evlist__parse_mmap_pages(const struct option *opt, const char *str, |
995 | int unset __maybe_unused) | |
996 | { | |
997 | return __perf_evlist__parse_mmap_pages(opt->value, str); | |
998 | } | |
999 | ||
c83fa7f2 | 1000 | /** |
718c602d | 1001 | * perf_evlist__mmap_ex - Create mmaps to receive events. |
c83fa7f2 AH |
1002 | * @evlist: list of events |
1003 | * @pages: map length in pages | |
1004 | * @overwrite: overwrite older events? | |
718c602d AH |
1005 | * @auxtrace_pages - auxtrace map length in pages |
1006 | * @auxtrace_overwrite - overwrite older auxtrace data? | |
f8a95309 | 1007 | * |
c83fa7f2 AH |
1008 | * If @overwrite is %false the user needs to signal event consumption using |
1009 | * perf_mmap__write_tail(). Using perf_evlist__mmap_read() does this | |
1010 | * automatically. | |
7e2ed097 | 1011 | * |
718c602d AH |
1012 | * Similarly, if @auxtrace_overwrite is %false the user needs to signal data |
1013 | * consumption using auxtrace_mmap__write_tail(). | |
1014 | * | |
c83fa7f2 | 1015 | * Return: %0 on success, negative error code otherwise. |
f8a95309 | 1016 | */ |
718c602d | 1017 | int perf_evlist__mmap_ex(struct perf_evlist *evlist, unsigned int pages, |
7a276ff6 | 1018 | unsigned int auxtrace_pages, |
718c602d | 1019 | bool auxtrace_overwrite) |
f8a95309 | 1020 | { |
aece948f | 1021 | struct perf_evsel *evsel; |
7e2ed097 ACM |
1022 | const struct cpu_map *cpus = evlist->cpus; |
1023 | const struct thread_map *threads = evlist->threads; | |
71f566a3 WN |
1024 | /* |
1025 | * Delay setting mp.prot: set it before calling perf_mmap__mmap. | |
1026 | * Its value is decided by evsel's write_backward. | |
1027 | * So &mp should not be passed through const pointer. | |
1028 | */ | |
1029 | struct mmap_params mp; | |
50a682ce | 1030 | |
8db6d6b1 WN |
1031 | if (!evlist->mmap) |
1032 | evlist->mmap = perf_evlist__alloc_mmap(evlist); | |
1033 | if (!evlist->mmap) | |
f8a95309 ACM |
1034 | return -ENOMEM; |
1035 | ||
1b85337d | 1036 | if (evlist->pollfd.entries == NULL && perf_evlist__alloc_pollfd(evlist) < 0) |
f8a95309 ACM |
1037 | return -ENOMEM; |
1038 | ||
994a1f78 | 1039 | evlist->mmap_len = perf_evlist__mmap_size(pages); |
2af68ef5 | 1040 | pr_debug("mmap size %zuB\n", evlist->mmap_len); |
a8a8f3eb | 1041 | mp.mask = evlist->mmap_len - page_size - 1; |
f8a95309 | 1042 | |
718c602d AH |
1043 | auxtrace_mmap_params__init(&mp.auxtrace_mp, evlist->mmap_len, |
1044 | auxtrace_pages, auxtrace_overwrite); | |
1045 | ||
e5cadb93 | 1046 | evlist__for_each_entry(evlist, evsel) { |
f8a95309 | 1047 | if ((evsel->attr.read_format & PERF_FORMAT_ID) && |
a91e5431 | 1048 | evsel->sample_id == NULL && |
a14bb7a6 | 1049 | perf_evsel__alloc_id(evsel, cpu_map__nr(cpus), threads->nr) < 0) |
f8a95309 | 1050 | return -ENOMEM; |
f8a95309 ACM |
1051 | } |
1052 | ||
ec1e7e43 | 1053 | if (cpu_map__empty(cpus)) |
a8a8f3eb | 1054 | return perf_evlist__mmap_per_thread(evlist, &mp); |
f8a95309 | 1055 | |
a8a8f3eb | 1056 | return perf_evlist__mmap_per_cpu(evlist, &mp); |
f8a95309 | 1057 | } |
7e2ed097 | 1058 | |
f74b9d3a | 1059 | int perf_evlist__mmap(struct perf_evlist *evlist, unsigned int pages) |
718c602d | 1060 | { |
7a276ff6 | 1061 | return perf_evlist__mmap_ex(evlist, pages, 0, false); |
718c602d AH |
1062 | } |
1063 | ||
602ad878 | 1064 | int perf_evlist__create_maps(struct perf_evlist *evlist, struct target *target) |
7e2ed097 | 1065 | { |
147c508f | 1066 | bool all_threads = (target->per_thread && target->system_wide); |
74bfd2b2 AH |
1067 | struct cpu_map *cpus; |
1068 | struct thread_map *threads; | |
7e2ed097 | 1069 | |
147c508f JY |
1070 | /* |
1071 | * If specify '-a' and '--per-thread' to perf record, perf record | |
1072 | * will override '--per-thread'. target->per_thread = false and | |
1073 | * target->system_wide = true. | |
1074 | * | |
1075 | * If specify '--per-thread' only to perf record, | |
1076 | * target->per_thread = true and target->system_wide = false. | |
1077 | * | |
1078 | * So target->per_thread && target->system_wide is false. | |
1079 | * For perf record, thread_map__new_str doesn't call | |
1080 | * thread_map__new_all_cpus. That will keep perf record's | |
1081 | * current behavior. | |
1082 | * | |
1083 | * For perf stat, it allows the case that target->per_thread and | |
1084 | * target->system_wide are all true. It means to collect system-wide | |
1085 | * per-thread data. thread_map__new_str will call | |
1086 | * thread_map__new_all_cpus to enumerate all threads. | |
1087 | */ | |
73c0ca1e | 1088 | threads = thread_map__new_str(target->pid, target->tid, target->uid, |
147c508f | 1089 | all_threads); |
7e2ed097 | 1090 | |
74bfd2b2 | 1091 | if (!threads) |
7e2ed097 ACM |
1092 | return -1; |
1093 | ||
9c105fbc | 1094 | if (target__uses_dummy_map(target)) |
74bfd2b2 | 1095 | cpus = cpu_map__dummy_new(); |
879d77d0 | 1096 | else |
74bfd2b2 | 1097 | cpus = cpu_map__new(target->cpu_list); |
7e2ed097 | 1098 | |
74bfd2b2 | 1099 | if (!cpus) |
7e2ed097 ACM |
1100 | goto out_delete_threads; |
1101 | ||
ec9a77a7 AH |
1102 | evlist->has_user_cpus = !!target->cpu_list; |
1103 | ||
74bfd2b2 | 1104 | perf_evlist__set_maps(evlist, cpus, threads); |
d5bc056e AH |
1105 | |
1106 | return 0; | |
7e2ed097 ACM |
1107 | |
1108 | out_delete_threads: | |
74bfd2b2 | 1109 | thread_map__put(threads); |
7e2ed097 ACM |
1110 | return -1; |
1111 | } | |
1112 | ||
d5bc056e AH |
1113 | void perf_evlist__set_maps(struct perf_evlist *evlist, struct cpu_map *cpus, |
1114 | struct thread_map *threads) | |
3de5cfb0 | 1115 | { |
934e0f20 AH |
1116 | /* |
1117 | * Allow for the possibility that one or another of the maps isn't being | |
1118 | * changed i.e. don't put it. Note we are assuming the maps that are | |
1119 | * being applied are brand new and evlist is taking ownership of the | |
1120 | * original reference count of 1. If that is not the case it is up to | |
1121 | * the caller to increase the reference count. | |
1122 | */ | |
1123 | if (cpus != evlist->cpus) { | |
3de5cfb0 | 1124 | cpu_map__put(evlist->cpus); |
a55e5663 | 1125 | evlist->cpus = cpu_map__get(cpus); |
934e0f20 | 1126 | } |
3de5cfb0 | 1127 | |
934e0f20 | 1128 | if (threads != evlist->threads) { |
3de5cfb0 | 1129 | thread_map__put(evlist->threads); |
a55e5663 | 1130 | evlist->threads = thread_map__get(threads); |
934e0f20 | 1131 | } |
3de5cfb0 | 1132 | |
ec9a77a7 | 1133 | perf_evlist__propagate_maps(evlist); |
3de5cfb0 JO |
1134 | } |
1135 | ||
22c8a376 ACM |
1136 | void __perf_evlist__set_sample_bit(struct perf_evlist *evlist, |
1137 | enum perf_event_sample_format bit) | |
1138 | { | |
1139 | struct perf_evsel *evsel; | |
1140 | ||
e5cadb93 | 1141 | evlist__for_each_entry(evlist, evsel) |
22c8a376 ACM |
1142 | __perf_evsel__set_sample_bit(evsel, bit); |
1143 | } | |
1144 | ||
1145 | void __perf_evlist__reset_sample_bit(struct perf_evlist *evlist, | |
1146 | enum perf_event_sample_format bit) | |
1147 | { | |
1148 | struct perf_evsel *evsel; | |
1149 | ||
e5cadb93 | 1150 | evlist__for_each_entry(evlist, evsel) |
22c8a376 ACM |
1151 | __perf_evsel__reset_sample_bit(evsel, bit); |
1152 | } | |
1153 | ||
23d4aad4 | 1154 | int perf_evlist__apply_filters(struct perf_evlist *evlist, struct perf_evsel **err_evsel) |
0a102479 | 1155 | { |
0a102479 | 1156 | struct perf_evsel *evsel; |
745cefc5 | 1157 | int err = 0; |
0a102479 | 1158 | |
e5cadb93 | 1159 | evlist__for_each_entry(evlist, evsel) { |
745cefc5 | 1160 | if (evsel->filter == NULL) |
0a102479 | 1161 | continue; |
745cefc5 | 1162 | |
d988d5ee KL |
1163 | /* |
1164 | * filters only work for tracepoint event, which doesn't have cpu limit. | |
1165 | * So evlist and evsel should always be same. | |
1166 | */ | |
475fb533 | 1167 | err = perf_evsel__apply_filter(evsel, evsel->filter); |
23d4aad4 ACM |
1168 | if (err) { |
1169 | *err_evsel = evsel; | |
745cefc5 | 1170 | break; |
23d4aad4 | 1171 | } |
0a102479 FW |
1172 | } |
1173 | ||
745cefc5 ACM |
1174 | return err; |
1175 | } | |
1176 | ||
1177 | int perf_evlist__set_filter(struct perf_evlist *evlist, const char *filter) | |
1178 | { | |
1179 | struct perf_evsel *evsel; | |
1180 | int err = 0; | |
745cefc5 | 1181 | |
e5cadb93 | 1182 | evlist__for_each_entry(evlist, evsel) { |
fdf14720 WN |
1183 | if (evsel->attr.type != PERF_TYPE_TRACEPOINT) |
1184 | continue; | |
1185 | ||
94ad89bc | 1186 | err = perf_evsel__set_filter(evsel, filter); |
745cefc5 ACM |
1187 | if (err) |
1188 | break; | |
1189 | } | |
1190 | ||
1191 | return err; | |
0a102479 | 1192 | } |
74429964 | 1193 | |
be199ada | 1194 | int perf_evlist__set_filter_pids(struct perf_evlist *evlist, size_t npids, pid_t *pids) |
cfd70a26 ACM |
1195 | { |
1196 | char *filter; | |
be199ada ACM |
1197 | int ret = -1; |
1198 | size_t i; | |
cfd70a26 | 1199 | |
be199ada ACM |
1200 | for (i = 0; i < npids; ++i) { |
1201 | if (i == 0) { | |
1202 | if (asprintf(&filter, "common_pid != %d", pids[i]) < 0) | |
1203 | return -1; | |
1204 | } else { | |
1205 | char *tmp; | |
1206 | ||
1207 | if (asprintf(&tmp, "%s && common_pid != %d", filter, pids[i]) < 0) | |
1208 | goto out_free; | |
1209 | ||
1210 | free(filter); | |
1211 | filter = tmp; | |
1212 | } | |
1213 | } | |
cfd70a26 ACM |
1214 | |
1215 | ret = perf_evlist__set_filter(evlist, filter); | |
be199ada | 1216 | out_free: |
cfd70a26 ACM |
1217 | free(filter); |
1218 | return ret; | |
1219 | } | |
1220 | ||
be199ada ACM |
1221 | int perf_evlist__set_filter_pid(struct perf_evlist *evlist, pid_t pid) |
1222 | { | |
1223 | return perf_evlist__set_filter_pids(evlist, 1, &pid); | |
1224 | } | |
1225 | ||
0c21f736 | 1226 | bool perf_evlist__valid_sample_type(struct perf_evlist *evlist) |
74429964 | 1227 | { |
75562573 | 1228 | struct perf_evsel *pos; |
c2a70653 | 1229 | |
75562573 AH |
1230 | if (evlist->nr_entries == 1) |
1231 | return true; | |
1232 | ||
1233 | if (evlist->id_pos < 0 || evlist->is_pos < 0) | |
1234 | return false; | |
1235 | ||
e5cadb93 | 1236 | evlist__for_each_entry(evlist, pos) { |
75562573 AH |
1237 | if (pos->id_pos != evlist->id_pos || |
1238 | pos->is_pos != evlist->is_pos) | |
c2a70653 | 1239 | return false; |
74429964 FW |
1240 | } |
1241 | ||
c2a70653 | 1242 | return true; |
74429964 FW |
1243 | } |
1244 | ||
75562573 | 1245 | u64 __perf_evlist__combined_sample_type(struct perf_evlist *evlist) |
c2a70653 | 1246 | { |
75562573 AH |
1247 | struct perf_evsel *evsel; |
1248 | ||
1249 | if (evlist->combined_sample_type) | |
1250 | return evlist->combined_sample_type; | |
1251 | ||
e5cadb93 | 1252 | evlist__for_each_entry(evlist, evsel) |
75562573 AH |
1253 | evlist->combined_sample_type |= evsel->attr.sample_type; |
1254 | ||
1255 | return evlist->combined_sample_type; | |
1256 | } | |
1257 | ||
1258 | u64 perf_evlist__combined_sample_type(struct perf_evlist *evlist) | |
1259 | { | |
1260 | evlist->combined_sample_type = 0; | |
1261 | return __perf_evlist__combined_sample_type(evlist); | |
c2a70653 ACM |
1262 | } |
1263 | ||
98df858e AK |
1264 | u64 perf_evlist__combined_branch_type(struct perf_evlist *evlist) |
1265 | { | |
1266 | struct perf_evsel *evsel; | |
1267 | u64 branch_type = 0; | |
1268 | ||
e5cadb93 | 1269 | evlist__for_each_entry(evlist, evsel) |
98df858e AK |
1270 | branch_type |= evsel->attr.branch_sample_type; |
1271 | return branch_type; | |
1272 | } | |
1273 | ||
9ede473c JO |
1274 | bool perf_evlist__valid_read_format(struct perf_evlist *evlist) |
1275 | { | |
1276 | struct perf_evsel *first = perf_evlist__first(evlist), *pos = first; | |
1277 | u64 read_format = first->attr.read_format; | |
1278 | u64 sample_type = first->attr.sample_type; | |
1279 | ||
e5cadb93 | 1280 | evlist__for_each_entry(evlist, pos) { |
9ede473c JO |
1281 | if (read_format != pos->attr.read_format) |
1282 | return false; | |
1283 | } | |
1284 | ||
1285 | /* PERF_SAMPLE_READ imples PERF_FORMAT_ID. */ | |
1286 | if ((sample_type & PERF_SAMPLE_READ) && | |
1287 | !(read_format & PERF_FORMAT_ID)) { | |
1288 | return false; | |
1289 | } | |
1290 | ||
1291 | return true; | |
1292 | } | |
1293 | ||
1294 | u64 perf_evlist__read_format(struct perf_evlist *evlist) | |
1295 | { | |
1296 | struct perf_evsel *first = perf_evlist__first(evlist); | |
1297 | return first->attr.read_format; | |
1298 | } | |
1299 | ||
0c21f736 | 1300 | u16 perf_evlist__id_hdr_size(struct perf_evlist *evlist) |
81e36bff | 1301 | { |
0c21f736 | 1302 | struct perf_evsel *first = perf_evlist__first(evlist); |
81e36bff ACM |
1303 | struct perf_sample *data; |
1304 | u64 sample_type; | |
1305 | u16 size = 0; | |
1306 | ||
81e36bff ACM |
1307 | if (!first->attr.sample_id_all) |
1308 | goto out; | |
1309 | ||
1310 | sample_type = first->attr.sample_type; | |
1311 | ||
1312 | if (sample_type & PERF_SAMPLE_TID) | |
1313 | size += sizeof(data->tid) * 2; | |
1314 | ||
1315 | if (sample_type & PERF_SAMPLE_TIME) | |
1316 | size += sizeof(data->time); | |
1317 | ||
1318 | if (sample_type & PERF_SAMPLE_ID) | |
1319 | size += sizeof(data->id); | |
1320 | ||
1321 | if (sample_type & PERF_SAMPLE_STREAM_ID) | |
1322 | size += sizeof(data->stream_id); | |
1323 | ||
1324 | if (sample_type & PERF_SAMPLE_CPU) | |
1325 | size += sizeof(data->cpu) * 2; | |
75562573 AH |
1326 | |
1327 | if (sample_type & PERF_SAMPLE_IDENTIFIER) | |
1328 | size += sizeof(data->id); | |
81e36bff ACM |
1329 | out: |
1330 | return size; | |
1331 | } | |
1332 | ||
0c21f736 | 1333 | bool perf_evlist__valid_sample_id_all(struct perf_evlist *evlist) |
74429964 | 1334 | { |
0c21f736 | 1335 | struct perf_evsel *first = perf_evlist__first(evlist), *pos = first; |
c2a70653 | 1336 | |
e5cadb93 | 1337 | evlist__for_each_entry_continue(evlist, pos) { |
c2a70653 ACM |
1338 | if (first->attr.sample_id_all != pos->attr.sample_id_all) |
1339 | return false; | |
74429964 FW |
1340 | } |
1341 | ||
c2a70653 ACM |
1342 | return true; |
1343 | } | |
1344 | ||
0c21f736 | 1345 | bool perf_evlist__sample_id_all(struct perf_evlist *evlist) |
c2a70653 | 1346 | { |
0c21f736 | 1347 | struct perf_evsel *first = perf_evlist__first(evlist); |
c2a70653 | 1348 | return first->attr.sample_id_all; |
74429964 | 1349 | } |
81cce8de ACM |
1350 | |
1351 | void perf_evlist__set_selected(struct perf_evlist *evlist, | |
1352 | struct perf_evsel *evsel) | |
1353 | { | |
1354 | evlist->selected = evsel; | |
1355 | } | |
727ab04e | 1356 | |
a74b4b66 NK |
1357 | void perf_evlist__close(struct perf_evlist *evlist) |
1358 | { | |
1359 | struct perf_evsel *evsel; | |
a74b4b66 | 1360 | |
475fb533 AK |
1361 | evlist__for_each_entry_reverse(evlist, evsel) |
1362 | perf_evsel__close(evsel); | |
a74b4b66 NK |
1363 | } |
1364 | ||
4112eb18 ACM |
1365 | static int perf_evlist__create_syswide_maps(struct perf_evlist *evlist) |
1366 | { | |
8c0498b6 AH |
1367 | struct cpu_map *cpus; |
1368 | struct thread_map *threads; | |
4112eb18 ACM |
1369 | int err = -ENOMEM; |
1370 | ||
1371 | /* | |
1372 | * Try reading /sys/devices/system/cpu/online to get | |
1373 | * an all cpus map. | |
1374 | * | |
1375 | * FIXME: -ENOMEM is the best we can do here, the cpu_map | |
1376 | * code needs an overhaul to properly forward the | |
1377 | * error, and we may not want to do that fallback to a | |
1378 | * default cpu identity map :-\ | |
1379 | */ | |
8c0498b6 AH |
1380 | cpus = cpu_map__new(NULL); |
1381 | if (!cpus) | |
4112eb18 ACM |
1382 | goto out; |
1383 | ||
8c0498b6 AH |
1384 | threads = thread_map__new_dummy(); |
1385 | if (!threads) | |
1386 | goto out_put; | |
4112eb18 | 1387 | |
8c0498b6 | 1388 | perf_evlist__set_maps(evlist, cpus, threads); |
4112eb18 ACM |
1389 | out: |
1390 | return err; | |
8c0498b6 AH |
1391 | out_put: |
1392 | cpu_map__put(cpus); | |
4112eb18 ACM |
1393 | goto out; |
1394 | } | |
1395 | ||
6a4bb04c | 1396 | int perf_evlist__open(struct perf_evlist *evlist) |
727ab04e | 1397 | { |
6a4bb04c | 1398 | struct perf_evsel *evsel; |
a74b4b66 | 1399 | int err; |
727ab04e | 1400 | |
4112eb18 ACM |
1401 | /* |
1402 | * Default: one fd per CPU, all threads, aka systemwide | |
1403 | * as sys_perf_event_open(cpu = -1, thread = -1) is EINVAL | |
1404 | */ | |
1405 | if (evlist->threads == NULL && evlist->cpus == NULL) { | |
1406 | err = perf_evlist__create_syswide_maps(evlist); | |
1407 | if (err < 0) | |
1408 | goto out_err; | |
1409 | } | |
1410 | ||
733cd2fe AH |
1411 | perf_evlist__update_id_pos(evlist); |
1412 | ||
e5cadb93 | 1413 | evlist__for_each_entry(evlist, evsel) { |
23df7f79 | 1414 | err = perf_evsel__open(evsel, evsel->cpus, evsel->threads); |
727ab04e ACM |
1415 | if (err < 0) |
1416 | goto out_err; | |
1417 | } | |
1418 | ||
1419 | return 0; | |
1420 | out_err: | |
a74b4b66 | 1421 | perf_evlist__close(evlist); |
41c21a68 | 1422 | errno = -err; |
727ab04e ACM |
1423 | return err; |
1424 | } | |
35b9d88e | 1425 | |
602ad878 | 1426 | int perf_evlist__prepare_workload(struct perf_evlist *evlist, struct target *target, |
55e162ea | 1427 | const char *argv[], bool pipe_output, |
735f7e0b | 1428 | void (*exec_error)(int signo, siginfo_t *info, void *ucontext)) |
35b9d88e ACM |
1429 | { |
1430 | int child_ready_pipe[2], go_pipe[2]; | |
1431 | char bf; | |
1432 | ||
1433 | if (pipe(child_ready_pipe) < 0) { | |
1434 | perror("failed to create 'ready' pipe"); | |
1435 | return -1; | |
1436 | } | |
1437 | ||
1438 | if (pipe(go_pipe) < 0) { | |
1439 | perror("failed to create 'go' pipe"); | |
1440 | goto out_close_ready_pipe; | |
1441 | } | |
1442 | ||
1443 | evlist->workload.pid = fork(); | |
1444 | if (evlist->workload.pid < 0) { | |
1445 | perror("failed to fork"); | |
1446 | goto out_close_pipes; | |
1447 | } | |
1448 | ||
1449 | if (!evlist->workload.pid) { | |
5f1c4225 ACM |
1450 | int ret; |
1451 | ||
119fa3c9 | 1452 | if (pipe_output) |
35b9d88e ACM |
1453 | dup2(2, 1); |
1454 | ||
0817df08 DA |
1455 | signal(SIGTERM, SIG_DFL); |
1456 | ||
35b9d88e ACM |
1457 | close(child_ready_pipe[0]); |
1458 | close(go_pipe[1]); | |
1459 | fcntl(go_pipe[0], F_SETFD, FD_CLOEXEC); | |
1460 | ||
35b9d88e ACM |
1461 | /* |
1462 | * Tell the parent we're ready to go | |
1463 | */ | |
1464 | close(child_ready_pipe[1]); | |
1465 | ||
1466 | /* | |
1467 | * Wait until the parent tells us to go. | |
1468 | */ | |
5f1c4225 ACM |
1469 | ret = read(go_pipe[0], &bf, 1); |
1470 | /* | |
1471 | * The parent will ask for the execvp() to be performed by | |
1472 | * writing exactly one byte, in workload.cork_fd, usually via | |
1473 | * perf_evlist__start_workload(). | |
1474 | * | |
20f86fc1 | 1475 | * For cancelling the workload without actually running it, |
5f1c4225 ACM |
1476 | * the parent will just close workload.cork_fd, without writing |
1477 | * anything, i.e. read will return zero and we just exit() | |
1478 | * here. | |
1479 | */ | |
1480 | if (ret != 1) { | |
1481 | if (ret == -1) | |
1482 | perror("unable to read pipe"); | |
1483 | exit(ret); | |
1484 | } | |
35b9d88e ACM |
1485 | |
1486 | execvp(argv[0], (char **)argv); | |
1487 | ||
735f7e0b | 1488 | if (exec_error) { |
f33cbe72 ACM |
1489 | union sigval val; |
1490 | ||
1491 | val.sival_int = errno; | |
1492 | if (sigqueue(getppid(), SIGUSR1, val)) | |
1493 | perror(argv[0]); | |
1494 | } else | |
1495 | perror(argv[0]); | |
35b9d88e ACM |
1496 | exit(-1); |
1497 | } | |
1498 | ||
735f7e0b ACM |
1499 | if (exec_error) { |
1500 | struct sigaction act = { | |
1501 | .sa_flags = SA_SIGINFO, | |
1502 | .sa_sigaction = exec_error, | |
1503 | }; | |
1504 | sigaction(SIGUSR1, &act, NULL); | |
1505 | } | |
1506 | ||
1aaf63b1 ACM |
1507 | if (target__none(target)) { |
1508 | if (evlist->threads == NULL) { | |
1509 | fprintf(stderr, "FATAL: evlist->threads need to be set at this point (%s:%d).\n", | |
1510 | __func__, __LINE__); | |
1511 | goto out_close_pipes; | |
1512 | } | |
e13798c7 | 1513 | thread_map__set_pid(evlist->threads, 0, evlist->workload.pid); |
1aaf63b1 | 1514 | } |
35b9d88e ACM |
1515 | |
1516 | close(child_ready_pipe[1]); | |
1517 | close(go_pipe[0]); | |
1518 | /* | |
1519 | * wait for child to settle | |
1520 | */ | |
1521 | if (read(child_ready_pipe[0], &bf, 1) == -1) { | |
1522 | perror("unable to read pipe"); | |
1523 | goto out_close_pipes; | |
1524 | } | |
1525 | ||
bcf3145f | 1526 | fcntl(go_pipe[1], F_SETFD, FD_CLOEXEC); |
35b9d88e ACM |
1527 | evlist->workload.cork_fd = go_pipe[1]; |
1528 | close(child_ready_pipe[0]); | |
1529 | return 0; | |
1530 | ||
1531 | out_close_pipes: | |
1532 | close(go_pipe[0]); | |
1533 | close(go_pipe[1]); | |
1534 | out_close_ready_pipe: | |
1535 | close(child_ready_pipe[0]); | |
1536 | close(child_ready_pipe[1]); | |
1537 | return -1; | |
1538 | } | |
1539 | ||
1540 | int perf_evlist__start_workload(struct perf_evlist *evlist) | |
1541 | { | |
1542 | if (evlist->workload.cork_fd > 0) { | |
b3824404 | 1543 | char bf = 0; |
bcf3145f | 1544 | int ret; |
35b9d88e ACM |
1545 | /* |
1546 | * Remove the cork, let it rip! | |
1547 | */ | |
bcf3145f NK |
1548 | ret = write(evlist->workload.cork_fd, &bf, 1); |
1549 | if (ret < 0) | |
e978be9e | 1550 | perror("unable to write to pipe"); |
bcf3145f NK |
1551 | |
1552 | close(evlist->workload.cork_fd); | |
1553 | return ret; | |
35b9d88e ACM |
1554 | } |
1555 | ||
1556 | return 0; | |
1557 | } | |
cb0b29e0 | 1558 | |
a3f698fe | 1559 | int perf_evlist__parse_sample(struct perf_evlist *evlist, union perf_event *event, |
0807d2d8 | 1560 | struct perf_sample *sample) |
cb0b29e0 | 1561 | { |
75562573 AH |
1562 | struct perf_evsel *evsel = perf_evlist__event2evsel(evlist, event); |
1563 | ||
1564 | if (!evsel) | |
1565 | return -EFAULT; | |
0807d2d8 | 1566 | return perf_evsel__parse_sample(evsel, event, sample); |
cb0b29e0 | 1567 | } |
78f067b3 | 1568 | |
01468120 JO |
1569 | int perf_evlist__parse_sample_timestamp(struct perf_evlist *evlist, |
1570 | union perf_event *event, | |
1571 | u64 *timestamp) | |
1572 | { | |
1573 | struct perf_evsel *evsel = perf_evlist__event2evsel(evlist, event); | |
1574 | ||
1575 | if (!evsel) | |
1576 | return -EFAULT; | |
1577 | return perf_evsel__parse_sample_timestamp(evsel, event, timestamp); | |
1578 | } | |
1579 | ||
78f067b3 ACM |
1580 | size_t perf_evlist__fprintf(struct perf_evlist *evlist, FILE *fp) |
1581 | { | |
1582 | struct perf_evsel *evsel; | |
1583 | size_t printed = 0; | |
1584 | ||
e5cadb93 | 1585 | evlist__for_each_entry(evlist, evsel) { |
78f067b3 ACM |
1586 | printed += fprintf(fp, "%s%s", evsel->idx ? ", " : "", |
1587 | perf_evsel__name(evsel)); | |
1588 | } | |
1589 | ||
b2222139 | 1590 | return printed + fprintf(fp, "\n"); |
78f067b3 | 1591 | } |
6ef068cb | 1592 | |
d9aade7f | 1593 | int perf_evlist__strerror_open(struct perf_evlist *evlist, |
a8f23d8f ACM |
1594 | int err, char *buf, size_t size) |
1595 | { | |
1596 | int printed, value; | |
c8b5f2c9 | 1597 | char sbuf[STRERR_BUFSIZE], *emsg = str_error_r(err, sbuf, sizeof(sbuf)); |
a8f23d8f ACM |
1598 | |
1599 | switch (err) { | |
1600 | case EACCES: | |
1601 | case EPERM: | |
1602 | printed = scnprintf(buf, size, | |
1603 | "Error:\t%s.\n" | |
1604 | "Hint:\tCheck /proc/sys/kernel/perf_event_paranoid setting.", emsg); | |
1605 | ||
1a47245d | 1606 | value = perf_event_paranoid(); |
a8f23d8f ACM |
1607 | |
1608 | printed += scnprintf(buf + printed, size - printed, "\nHint:\t"); | |
1609 | ||
1610 | if (value >= 2) { | |
1611 | printed += scnprintf(buf + printed, size - printed, | |
1612 | "For your workloads it needs to be <= 1\nHint:\t"); | |
1613 | } | |
1614 | printed += scnprintf(buf + printed, size - printed, | |
5229e366 | 1615 | "For system wide tracing it needs to be set to -1.\n"); |
a8f23d8f ACM |
1616 | |
1617 | printed += scnprintf(buf + printed, size - printed, | |
5229e366 ACM |
1618 | "Hint:\tTry: 'sudo sh -c \"echo -1 > /proc/sys/kernel/perf_event_paranoid\"'\n" |
1619 | "Hint:\tThe current value is %d.", value); | |
a8f23d8f | 1620 | break; |
d9aade7f ACM |
1621 | case EINVAL: { |
1622 | struct perf_evsel *first = perf_evlist__first(evlist); | |
1623 | int max_freq; | |
1624 | ||
1625 | if (sysctl__read_int("kernel/perf_event_max_sample_rate", &max_freq) < 0) | |
1626 | goto out_default; | |
1627 | ||
1628 | if (first->attr.sample_freq < (u64)max_freq) | |
1629 | goto out_default; | |
1630 | ||
1631 | printed = scnprintf(buf, size, | |
1632 | "Error:\t%s.\n" | |
1633 | "Hint:\tCheck /proc/sys/kernel/perf_event_max_sample_rate.\n" | |
1634 | "Hint:\tThe current value is %d and %" PRIu64 " is being requested.", | |
1635 | emsg, max_freq, first->attr.sample_freq); | |
1636 | break; | |
1637 | } | |
a8f23d8f | 1638 | default: |
d9aade7f | 1639 | out_default: |
a8f23d8f ACM |
1640 | scnprintf(buf, size, "%s", emsg); |
1641 | break; | |
1642 | } | |
1643 | ||
1644 | return 0; | |
1645 | } | |
a025e4f0 | 1646 | |
956fa571 ACM |
1647 | int perf_evlist__strerror_mmap(struct perf_evlist *evlist, int err, char *buf, size_t size) |
1648 | { | |
c8b5f2c9 | 1649 | char sbuf[STRERR_BUFSIZE], *emsg = str_error_r(err, sbuf, sizeof(sbuf)); |
e965bea1 | 1650 | int pages_attempted = evlist->mmap_len / 1024, pages_max_per_user, printed = 0; |
956fa571 ACM |
1651 | |
1652 | switch (err) { | |
1653 | case EPERM: | |
e5d4a290 | 1654 | sysctl__read_int("kernel/perf_event_mlock_kb", &pages_max_per_user); |
e965bea1 ACM |
1655 | printed += scnprintf(buf + printed, size - printed, |
1656 | "Error:\t%s.\n" | |
956fa571 | 1657 | "Hint:\tCheck /proc/sys/kernel/perf_event_mlock_kb (%d kB) setting.\n" |
e965bea1 | 1658 | "Hint:\tTried using %zd kB.\n", |
e5d4a290 | 1659 | emsg, pages_max_per_user, pages_attempted); |
e965bea1 ACM |
1660 | |
1661 | if (pages_attempted >= pages_max_per_user) { | |
1662 | printed += scnprintf(buf + printed, size - printed, | |
1663 | "Hint:\tTry 'sudo sh -c \"echo %d > /proc/sys/kernel/perf_event_mlock_kb\"', or\n", | |
1664 | pages_max_per_user + pages_attempted); | |
1665 | } | |
1666 | ||
1667 | printed += scnprintf(buf + printed, size - printed, | |
1668 | "Hint:\tTry using a smaller -m/--mmap-pages value."); | |
956fa571 ACM |
1669 | break; |
1670 | default: | |
1671 | scnprintf(buf, size, "%s", emsg); | |
1672 | break; | |
1673 | } | |
1674 | ||
1675 | return 0; | |
1676 | } | |
1677 | ||
a025e4f0 AH |
1678 | void perf_evlist__to_front(struct perf_evlist *evlist, |
1679 | struct perf_evsel *move_evsel) | |
1680 | { | |
1681 | struct perf_evsel *evsel, *n; | |
1682 | LIST_HEAD(move); | |
1683 | ||
1684 | if (move_evsel == perf_evlist__first(evlist)) | |
1685 | return; | |
1686 | ||
e5cadb93 | 1687 | evlist__for_each_entry_safe(evlist, n, evsel) { |
a025e4f0 AH |
1688 | if (evsel->leader == move_evsel->leader) |
1689 | list_move_tail(&evsel->node, &move); | |
1690 | } | |
1691 | ||
1692 | list_splice(&move, &evlist->entries); | |
1693 | } | |
60b0896c AH |
1694 | |
1695 | void perf_evlist__set_tracking_event(struct perf_evlist *evlist, | |
1696 | struct perf_evsel *tracking_evsel) | |
1697 | { | |
1698 | struct perf_evsel *evsel; | |
1699 | ||
1700 | if (tracking_evsel->tracking) | |
1701 | return; | |
1702 | ||
e5cadb93 | 1703 | evlist__for_each_entry(evlist, evsel) { |
60b0896c AH |
1704 | if (evsel != tracking_evsel) |
1705 | evsel->tracking = false; | |
1706 | } | |
1707 | ||
1708 | tracking_evsel->tracking = true; | |
1709 | } | |
7630b3e2 WN |
1710 | |
1711 | struct perf_evsel * | |
1712 | perf_evlist__find_evsel_by_str(struct perf_evlist *evlist, | |
1713 | const char *str) | |
1714 | { | |
1715 | struct perf_evsel *evsel; | |
1716 | ||
e5cadb93 | 1717 | evlist__for_each_entry(evlist, evsel) { |
7630b3e2 WN |
1718 | if (!evsel->name) |
1719 | continue; | |
1720 | if (strcmp(str, evsel->name) == 0) | |
1721 | return evsel; | |
1722 | } | |
1723 | ||
1724 | return NULL; | |
1725 | } | |
54cc54de WN |
1726 | |
1727 | void perf_evlist__toggle_bkw_mmap(struct perf_evlist *evlist, | |
1728 | enum bkw_mmap_state state) | |
1729 | { | |
1730 | enum bkw_mmap_state old_state = evlist->bkw_mmap_state; | |
1731 | enum action { | |
1732 | NONE, | |
1733 | PAUSE, | |
1734 | RESUME, | |
1735 | } action = NONE; | |
1736 | ||
0b72d69a | 1737 | if (!evlist->overwrite_mmap) |
54cc54de WN |
1738 | return; |
1739 | ||
1740 | switch (old_state) { | |
1741 | case BKW_MMAP_NOTREADY: { | |
1742 | if (state != BKW_MMAP_RUNNING) | |
dd8bd53a | 1743 | goto state_err; |
54cc54de WN |
1744 | break; |
1745 | } | |
1746 | case BKW_MMAP_RUNNING: { | |
1747 | if (state != BKW_MMAP_DATA_PENDING) | |
1748 | goto state_err; | |
1749 | action = PAUSE; | |
1750 | break; | |
1751 | } | |
1752 | case BKW_MMAP_DATA_PENDING: { | |
1753 | if (state != BKW_MMAP_EMPTY) | |
1754 | goto state_err; | |
1755 | break; | |
1756 | } | |
1757 | case BKW_MMAP_EMPTY: { | |
1758 | if (state != BKW_MMAP_RUNNING) | |
1759 | goto state_err; | |
1760 | action = RESUME; | |
1761 | break; | |
1762 | } | |
1763 | default: | |
1764 | WARN_ONCE(1, "Shouldn't get there\n"); | |
1765 | } | |
1766 | ||
1767 | evlist->bkw_mmap_state = state; | |
1768 | ||
1769 | switch (action) { | |
1770 | case PAUSE: | |
1771 | perf_evlist__pause(evlist); | |
1772 | break; | |
1773 | case RESUME: | |
1774 | perf_evlist__resume(evlist); | |
1775 | break; | |
1776 | case NONE: | |
1777 | default: | |
1778 | break; | |
1779 | } | |
1780 | ||
1781 | state_err: | |
1782 | return; | |
1783 | } | |
07d6f446 ACM |
1784 | |
1785 | bool perf_evlist__exclude_kernel(struct perf_evlist *evlist) | |
1786 | { | |
1787 | struct perf_evsel *evsel; | |
1788 | ||
1789 | evlist__for_each_entry(evlist, evsel) { | |
1790 | if (!evsel->attr.exclude_kernel) | |
1791 | return false; | |
1792 | } | |
1793 | ||
1794 | return true; | |
1795 | } |