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