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