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