]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - tools/perf/util/evlist.c
perf evlist: Introduce poll method for common code idiom
[mirror_ubuntu-bionic-kernel.git] / tools / perf / util / evlist.c
CommitLineData
f8a95309
ACM
1/*
2 * Copyright (C) 2011, Red Hat Inc, Arnaldo Carvalho de Melo <acme@redhat.com>
3 *
4 * Parts came from builtin-{top,stat,record}.c, see those files for further
5 * copyright notes.
6 *
7 * Released under the GPL v2. (and only v2, not any later version)
8 */
a8c9ae18 9#include "util.h"
553873e1 10#include <api/fs/debugfs.h>
5c581041 11#include <poll.h>
f8a95309
ACM
12#include "cpumap.h"
13#include "thread_map.h"
12864b31 14#include "target.h"
361c99a6
ACM
15#include "evlist.h"
16#include "evsel.h"
e3e1a54f 17#include "debug.h"
35b9d88e 18#include <unistd.h>
361c99a6 19
50d08e47 20#include "parse-events.h"
994a1f78 21#include "parse-options.h"
50d08e47 22
f8a95309
ACM
23#include <sys/mman.h>
24
70db7533
ACM
25#include <linux/bitops.h>
26#include <linux/hash.h>
27
f8a95309 28#define FD(e, x, y) (*(int *)xyarray__entry(e->fd, x, y))
a91e5431 29#define SID(e, x, y) xyarray__entry(e->sample_id, x, y)
f8a95309 30
7e2ed097
ACM
31void perf_evlist__init(struct perf_evlist *evlist, struct cpu_map *cpus,
32 struct thread_map *threads)
ef1d1af2
ACM
33{
34 int i;
35
36 for (i = 0; i < PERF_EVLIST__HLIST_SIZE; ++i)
37 INIT_HLIST_HEAD(&evlist->heads[i]);
38 INIT_LIST_HEAD(&evlist->entries);
7e2ed097 39 perf_evlist__set_maps(evlist, cpus, threads);
35b9d88e 40 evlist->workload.pid = -1;
ef1d1af2
ACM
41}
42
334fe7a3 43struct perf_evlist *perf_evlist__new(void)
361c99a6
ACM
44{
45 struct perf_evlist *evlist = zalloc(sizeof(*evlist));
46
ef1d1af2 47 if (evlist != NULL)
334fe7a3 48 perf_evlist__init(evlist, NULL, NULL);
361c99a6
ACM
49
50 return evlist;
51}
52
b22d54b0
JO
53struct perf_evlist *perf_evlist__new_default(void)
54{
55 struct perf_evlist *evlist = perf_evlist__new();
56
57 if (evlist && perf_evlist__add_default(evlist)) {
58 perf_evlist__delete(evlist);
59 evlist = NULL;
60 }
61
62 return evlist;
63}
64
75562573
AH
65/**
66 * perf_evlist__set_id_pos - set the positions of event ids.
67 * @evlist: selected event list
68 *
69 * Events with compatible sample types all have the same id_pos
70 * and is_pos. For convenience, put a copy on evlist.
71 */
72void perf_evlist__set_id_pos(struct perf_evlist *evlist)
73{
74 struct perf_evsel *first = perf_evlist__first(evlist);
75
76 evlist->id_pos = first->id_pos;
77 evlist->is_pos = first->is_pos;
78}
79
733cd2fe
AH
80static void perf_evlist__update_id_pos(struct perf_evlist *evlist)
81{
82 struct perf_evsel *evsel;
83
0050f7aa 84 evlist__for_each(evlist, evsel)
733cd2fe
AH
85 perf_evsel__calc_id_pos(evsel);
86
87 perf_evlist__set_id_pos(evlist);
88}
89
361c99a6
ACM
90static void perf_evlist__purge(struct perf_evlist *evlist)
91{
92 struct perf_evsel *pos, *n;
93
0050f7aa 94 evlist__for_each_safe(evlist, n, pos) {
361c99a6
ACM
95 list_del_init(&pos->node);
96 perf_evsel__delete(pos);
97 }
98
99 evlist->nr_entries = 0;
100}
101
ef1d1af2 102void perf_evlist__exit(struct perf_evlist *evlist)
361c99a6 103{
04662523
ACM
104 zfree(&evlist->mmap);
105 zfree(&evlist->pollfd);
ef1d1af2
ACM
106}
107
108void perf_evlist__delete(struct perf_evlist *evlist)
109{
983874d1 110 perf_evlist__munmap(evlist);
f26e1c7c 111 perf_evlist__close(evlist);
03ad9747
ACM
112 cpu_map__delete(evlist->cpus);
113 thread_map__delete(evlist->threads);
114 evlist->cpus = NULL;
115 evlist->threads = NULL;
ef1d1af2
ACM
116 perf_evlist__purge(evlist);
117 perf_evlist__exit(evlist);
361c99a6
ACM
118 free(evlist);
119}
120
121void perf_evlist__add(struct perf_evlist *evlist, struct perf_evsel *entry)
122{
123 list_add_tail(&entry->node, &evlist->entries);
ef503831 124 entry->idx = evlist->nr_entries;
60b0896c 125 entry->tracking = !entry->idx;
ef503831 126
75562573
AH
127 if (!evlist->nr_entries++)
128 perf_evlist__set_id_pos(evlist);
361c99a6
ACM
129}
130
0529bc1f
JO
131void perf_evlist__splice_list_tail(struct perf_evlist *evlist,
132 struct list_head *list,
133 int nr_entries)
50d08e47 134{
75562573
AH
135 bool set_id_pos = !evlist->nr_entries;
136
50d08e47
ACM
137 list_splice_tail(list, &evlist->entries);
138 evlist->nr_entries += nr_entries;
75562573
AH
139 if (set_id_pos)
140 perf_evlist__set_id_pos(evlist);
50d08e47
ACM
141}
142
63dab225
ACM
143void __perf_evlist__set_leader(struct list_head *list)
144{
145 struct perf_evsel *evsel, *leader;
146
147 leader = list_entry(list->next, struct perf_evsel, node);
97f63e4a
NK
148 evsel = list_entry(list->prev, struct perf_evsel, node);
149
150 leader->nr_members = evsel->idx - leader->idx + 1;
63dab225 151
0050f7aa 152 __evlist__for_each(list, evsel) {
74b2133d 153 evsel->leader = leader;
63dab225
ACM
154 }
155}
156
157void perf_evlist__set_leader(struct perf_evlist *evlist)
6a4bb04c 158{
97f63e4a
NK
159 if (evlist->nr_entries) {
160 evlist->nr_groups = evlist->nr_entries > 1 ? 1 : 0;
63dab225 161 __perf_evlist__set_leader(&evlist->entries);
97f63e4a 162 }
6a4bb04c
JO
163}
164
361c99a6
ACM
165int perf_evlist__add_default(struct perf_evlist *evlist)
166{
167 struct perf_event_attr attr = {
168 .type = PERF_TYPE_HARDWARE,
169 .config = PERF_COUNT_HW_CPU_CYCLES,
170 };
1aed2671
JR
171 struct perf_evsel *evsel;
172
173 event_attr_init(&attr);
361c99a6 174
ef503831 175 evsel = perf_evsel__new(&attr);
361c99a6 176 if (evsel == NULL)
cc2d86b0
SE
177 goto error;
178
179 /* use strdup() because free(evsel) assumes name is allocated */
180 evsel->name = strdup("cycles");
181 if (!evsel->name)
182 goto error_free;
361c99a6
ACM
183
184 perf_evlist__add(evlist, evsel);
185 return 0;
cc2d86b0
SE
186error_free:
187 perf_evsel__delete(evsel);
188error:
189 return -ENOMEM;
361c99a6 190}
5c581041 191
e60fc847
ACM
192static int perf_evlist__add_attrs(struct perf_evlist *evlist,
193 struct perf_event_attr *attrs, size_t nr_attrs)
50d08e47
ACM
194{
195 struct perf_evsel *evsel, *n;
196 LIST_HEAD(head);
197 size_t i;
198
199 for (i = 0; i < nr_attrs; i++) {
ef503831 200 evsel = perf_evsel__new_idx(attrs + i, evlist->nr_entries + i);
50d08e47
ACM
201 if (evsel == NULL)
202 goto out_delete_partial_list;
203 list_add_tail(&evsel->node, &head);
204 }
205
206 perf_evlist__splice_list_tail(evlist, &head, nr_attrs);
207
208 return 0;
209
210out_delete_partial_list:
0050f7aa 211 __evlist__for_each_safe(&head, n, evsel)
50d08e47
ACM
212 perf_evsel__delete(evsel);
213 return -1;
214}
215
79695e1b
ACM
216int __perf_evlist__add_default_attrs(struct perf_evlist *evlist,
217 struct perf_event_attr *attrs, size_t nr_attrs)
218{
219 size_t i;
220
221 for (i = 0; i < nr_attrs; i++)
222 event_attr_init(attrs + i);
223
224 return perf_evlist__add_attrs(evlist, attrs, nr_attrs);
225}
226
da378962
ACM
227struct perf_evsel *
228perf_evlist__find_tracepoint_by_id(struct perf_evlist *evlist, int id)
ee29be62
ACM
229{
230 struct perf_evsel *evsel;
231
0050f7aa 232 evlist__for_each(evlist, evsel) {
ee29be62
ACM
233 if (evsel->attr.type == PERF_TYPE_TRACEPOINT &&
234 (int)evsel->attr.config == id)
235 return evsel;
236 }
237
238 return NULL;
239}
240
a2f2804a
DA
241struct perf_evsel *
242perf_evlist__find_tracepoint_by_name(struct perf_evlist *evlist,
243 const char *name)
244{
245 struct perf_evsel *evsel;
246
0050f7aa 247 evlist__for_each(evlist, evsel) {
a2f2804a
DA
248 if ((evsel->attr.type == PERF_TYPE_TRACEPOINT) &&
249 (strcmp(evsel->name, name) == 0))
250 return evsel;
251 }
252
253 return NULL;
254}
255
39876e7d
ACM
256int perf_evlist__add_newtp(struct perf_evlist *evlist,
257 const char *sys, const char *name, void *handler)
258{
ef503831 259 struct perf_evsel *evsel = perf_evsel__newtp(sys, name);
39876e7d 260
39876e7d
ACM
261 if (evsel == NULL)
262 return -1;
263
744a9719 264 evsel->handler = handler;
39876e7d
ACM
265 perf_evlist__add(evlist, evsel);
266 return 0;
267}
268
bf8e8f4b
AH
269static int perf_evlist__nr_threads(struct perf_evlist *evlist,
270 struct perf_evsel *evsel)
271{
272 if (evsel->system_wide)
273 return 1;
274 else
275 return thread_map__nr(evlist->threads);
276}
277
4152ab37
ACM
278void perf_evlist__disable(struct perf_evlist *evlist)
279{
280 int cpu, thread;
281 struct perf_evsel *pos;
b3a319d5 282 int nr_cpus = cpu_map__nr(evlist->cpus);
bf8e8f4b 283 int nr_threads;
4152ab37 284
b3a319d5 285 for (cpu = 0; cpu < nr_cpus; cpu++) {
0050f7aa 286 evlist__for_each(evlist, pos) {
395c3070 287 if (!perf_evsel__is_group_leader(pos) || !pos->fd)
3fe4430d 288 continue;
bf8e8f4b 289 nr_threads = perf_evlist__nr_threads(evlist, pos);
b3a319d5 290 for (thread = 0; thread < nr_threads; thread++)
55da8005
NK
291 ioctl(FD(pos, cpu, thread),
292 PERF_EVENT_IOC_DISABLE, 0);
4152ab37
ACM
293 }
294 }
295}
296
764e16a3
DA
297void perf_evlist__enable(struct perf_evlist *evlist)
298{
299 int cpu, thread;
300 struct perf_evsel *pos;
b3a319d5 301 int nr_cpus = cpu_map__nr(evlist->cpus);
bf8e8f4b 302 int nr_threads;
764e16a3 303
b3a319d5 304 for (cpu = 0; cpu < nr_cpus; cpu++) {
0050f7aa 305 evlist__for_each(evlist, pos) {
395c3070 306 if (!perf_evsel__is_group_leader(pos) || !pos->fd)
3fe4430d 307 continue;
bf8e8f4b 308 nr_threads = perf_evlist__nr_threads(evlist, pos);
b3a319d5 309 for (thread = 0; thread < nr_threads; thread++)
55da8005
NK
310 ioctl(FD(pos, cpu, thread),
311 PERF_EVENT_IOC_ENABLE, 0);
764e16a3
DA
312 }
313 }
314}
315
395c3070
AH
316int perf_evlist__disable_event(struct perf_evlist *evlist,
317 struct perf_evsel *evsel)
318{
319 int cpu, thread, err;
bf8e8f4b
AH
320 int nr_cpus = cpu_map__nr(evlist->cpus);
321 int nr_threads = perf_evlist__nr_threads(evlist, evsel);
395c3070
AH
322
323 if (!evsel->fd)
324 return 0;
325
bf8e8f4b
AH
326 for (cpu = 0; cpu < nr_cpus; cpu++) {
327 for (thread = 0; thread < nr_threads; thread++) {
395c3070
AH
328 err = ioctl(FD(evsel, cpu, thread),
329 PERF_EVENT_IOC_DISABLE, 0);
330 if (err)
331 return err;
332 }
333 }
334 return 0;
335}
336
337int perf_evlist__enable_event(struct perf_evlist *evlist,
338 struct perf_evsel *evsel)
339{
340 int cpu, thread, err;
bf8e8f4b
AH
341 int nr_cpus = cpu_map__nr(evlist->cpus);
342 int nr_threads = perf_evlist__nr_threads(evlist, evsel);
395c3070
AH
343
344 if (!evsel->fd)
345 return -EINVAL;
346
bf8e8f4b
AH
347 for (cpu = 0; cpu < nr_cpus; cpu++) {
348 for (thread = 0; thread < nr_threads; thread++) {
395c3070
AH
349 err = ioctl(FD(evsel, cpu, thread),
350 PERF_EVENT_IOC_ENABLE, 0);
351 if (err)
352 return err;
353 }
354 }
355 return 0;
356}
357
1c65056c
AH
358static int perf_evlist__enable_event_cpu(struct perf_evlist *evlist,
359 struct perf_evsel *evsel, int cpu)
360{
361 int thread, err;
362 int nr_threads = perf_evlist__nr_threads(evlist, evsel);
363
364 if (!evsel->fd)
365 return -EINVAL;
366
367 for (thread = 0; thread < nr_threads; thread++) {
368 err = ioctl(FD(evsel, cpu, thread),
369 PERF_EVENT_IOC_ENABLE, 0);
370 if (err)
371 return err;
372 }
373 return 0;
374}
375
376static int perf_evlist__enable_event_thread(struct perf_evlist *evlist,
377 struct perf_evsel *evsel,
378 int thread)
379{
380 int cpu, err;
381 int nr_cpus = cpu_map__nr(evlist->cpus);
382
383 if (!evsel->fd)
384 return -EINVAL;
385
386 for (cpu = 0; cpu < nr_cpus; cpu++) {
387 err = ioctl(FD(evsel, cpu, thread), PERF_EVENT_IOC_ENABLE, 0);
388 if (err)
389 return err;
390 }
391 return 0;
392}
393
394int perf_evlist__enable_event_idx(struct perf_evlist *evlist,
395 struct perf_evsel *evsel, int idx)
396{
397 bool per_cpu_mmaps = !cpu_map__empty(evlist->cpus);
398
399 if (per_cpu_mmaps)
400 return perf_evlist__enable_event_cpu(evlist, evsel, idx);
401 else
402 return perf_evlist__enable_event_thread(evlist, evsel, idx);
403}
404
ad6765dd
ACM
405static int perf_evlist__grow_pollfd(struct perf_evlist *evlist, int hint)
406{
407 int nr_fds_alloc = evlist->nr_fds_alloc + hint;
408 size_t size = sizeof(struct pollfd) * nr_fds_alloc;
409 struct pollfd *pollfd = realloc(evlist->pollfd, size);
410
411 if (pollfd == NULL)
412 return -ENOMEM;
413
414 evlist->nr_fds_alloc = nr_fds_alloc;
415 evlist->pollfd = pollfd;
416 return 0;
417}
418
419int perf_evlist__alloc_pollfd(struct perf_evlist *evlist)
5c581041 420{
b3a319d5
NK
421 int nr_cpus = cpu_map__nr(evlist->cpus);
422 int nr_threads = thread_map__nr(evlist->threads);
bf8e8f4b
AH
423 int nfds = 0;
424 struct perf_evsel *evsel;
425
426 list_for_each_entry(evsel, &evlist->entries, node) {
427 if (evsel->system_wide)
428 nfds += nr_cpus;
429 else
430 nfds += nr_cpus * nr_threads;
431 }
432
ad6765dd
ACM
433 if (evlist->nr_fds_alloc - evlist->nr_fds < nfds &&
434 perf_evlist__grow_pollfd(evlist, nfds) < 0)
435 return -ENOMEM;
436
437 return 0;
5c581041 438}
70082dd9 439
ad6765dd 440int perf_evlist__add_pollfd(struct perf_evlist *evlist, int fd)
70082dd9 441{
ad6765dd
ACM
442 /*
443 * XXX: 64 is arbitrary, just not to call realloc at each fd.
444 * Find a better autogrowing heuristic
445 */
446 if (evlist->nr_fds == evlist->nr_fds_alloc &&
447 perf_evlist__grow_pollfd(evlist, 64) < 0)
448 return -ENOMEM;
449
70082dd9
ACM
450 fcntl(fd, F_SETFL, O_NONBLOCK);
451 evlist->pollfd[evlist->nr_fds].fd = fd;
8179672c 452 evlist->pollfd[evlist->nr_fds].events = POLLIN | POLLERR | POLLHUP;
70082dd9 453 evlist->nr_fds++;
ad6765dd 454 return 0;
70082dd9 455}
70db7533 456
1ddec7f0
ACM
457int perf_evlist__filter_pollfd(struct perf_evlist *evlist, short revents_and_mask)
458{
459 int fd, nr_fds = 0;
460
461 if (evlist->nr_fds == 0)
462 return 0;
463
464 for (fd = 0; fd < evlist->nr_fds; ++fd) {
465 if (evlist->pollfd[fd].revents & revents_and_mask)
466 continue;
467
468 if (fd != nr_fds)
469 evlist->pollfd[nr_fds] = evlist->pollfd[fd];
470
471 ++nr_fds;
472 }
473
474 evlist->nr_fds = nr_fds;
475 return nr_fds;
476}
477
f66a889d
ACM
478int perf_evlist__poll(struct perf_evlist *evlist, int timeout)
479{
480 return poll(evlist->pollfd, evlist->nr_fds, timeout);
481}
482
a91e5431
ACM
483static void perf_evlist__id_hash(struct perf_evlist *evlist,
484 struct perf_evsel *evsel,
485 int cpu, int thread, u64 id)
3d3b5e95
ACM
486{
487 int hash;
488 struct perf_sample_id *sid = SID(evsel, cpu, thread);
489
490 sid->id = id;
491 sid->evsel = evsel;
492 hash = hash_64(sid->id, PERF_EVLIST__HLIST_BITS);
493 hlist_add_head(&sid->node, &evlist->heads[hash]);
494}
495
a91e5431
ACM
496void perf_evlist__id_add(struct perf_evlist *evlist, struct perf_evsel *evsel,
497 int cpu, int thread, u64 id)
498{
499 perf_evlist__id_hash(evlist, evsel, cpu, thread, id);
500 evsel->id[evsel->ids++] = id;
501}
502
503static int perf_evlist__id_add_fd(struct perf_evlist *evlist,
504 struct perf_evsel *evsel,
505 int cpu, int thread, int fd)
f8a95309 506{
f8a95309 507 u64 read_data[4] = { 0, };
3d3b5e95 508 int id_idx = 1; /* The first entry is the counter value */
e2b5abe0
JO
509 u64 id;
510 int ret;
511
512 ret = ioctl(fd, PERF_EVENT_IOC_ID, &id);
513 if (!ret)
514 goto add;
515
516 if (errno != ENOTTY)
517 return -1;
518
519 /* Legacy way to get event id.. All hail to old kernels! */
f8a95309 520
c4861afe
JO
521 /*
522 * This way does not work with group format read, so bail
523 * out in that case.
524 */
525 if (perf_evlist__read_format(evlist) & PERF_FORMAT_GROUP)
526 return -1;
527
f8a95309
ACM
528 if (!(evsel->attr.read_format & PERF_FORMAT_ID) ||
529 read(fd, &read_data, sizeof(read_data)) == -1)
530 return -1;
531
532 if (evsel->attr.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
533 ++id_idx;
534 if (evsel->attr.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
535 ++id_idx;
536
e2b5abe0
JO
537 id = read_data[id_idx];
538
539 add:
540 perf_evlist__id_add(evlist, evsel, cpu, thread, id);
f8a95309
ACM
541 return 0;
542}
543
932a3594 544struct perf_sample_id *perf_evlist__id2sid(struct perf_evlist *evlist, u64 id)
70db7533
ACM
545{
546 struct hlist_head *head;
70db7533
ACM
547 struct perf_sample_id *sid;
548 int hash;
549
70db7533
ACM
550 hash = hash_64(id, PERF_EVLIST__HLIST_BITS);
551 head = &evlist->heads[hash];
552
b67bfe0d 553 hlist_for_each_entry(sid, head, node)
70db7533 554 if (sid->id == id)
932a3594
JO
555 return sid;
556
557 return NULL;
558}
559
560struct perf_evsel *perf_evlist__id2evsel(struct perf_evlist *evlist, u64 id)
561{
562 struct perf_sample_id *sid;
563
564 if (evlist->nr_entries == 1)
565 return perf_evlist__first(evlist);
566
567 sid = perf_evlist__id2sid(evlist, id);
568 if (sid)
569 return sid->evsel;
30e68bcc
NK
570
571 if (!perf_evlist__sample_id_all(evlist))
0c21f736 572 return perf_evlist__first(evlist);
30e68bcc 573
70db7533
ACM
574 return NULL;
575}
04391deb 576
75562573
AH
577static int perf_evlist__event2id(struct perf_evlist *evlist,
578 union perf_event *event, u64 *id)
579{
580 const u64 *array = event->sample.array;
581 ssize_t n;
582
583 n = (event->header.size - sizeof(event->header)) >> 3;
584
585 if (event->header.type == PERF_RECORD_SAMPLE) {
586 if (evlist->id_pos >= n)
587 return -1;
588 *id = array[evlist->id_pos];
589 } else {
590 if (evlist->is_pos > n)
591 return -1;
592 n -= evlist->is_pos;
593 *id = array[n];
594 }
595 return 0;
596}
597
598static struct perf_evsel *perf_evlist__event2evsel(struct perf_evlist *evlist,
599 union perf_event *event)
600{
98be6966 601 struct perf_evsel *first = perf_evlist__first(evlist);
75562573
AH
602 struct hlist_head *head;
603 struct perf_sample_id *sid;
604 int hash;
605 u64 id;
606
607 if (evlist->nr_entries == 1)
98be6966
AH
608 return first;
609
610 if (!first->attr.sample_id_all &&
611 event->header.type != PERF_RECORD_SAMPLE)
612 return first;
75562573
AH
613
614 if (perf_evlist__event2id(evlist, event, &id))
615 return NULL;
616
617 /* Synthesized events have an id of zero */
618 if (!id)
98be6966 619 return first;
75562573
AH
620
621 hash = hash_64(id, PERF_EVLIST__HLIST_BITS);
622 head = &evlist->heads[hash];
623
624 hlist_for_each_entry(sid, head, node) {
625 if (sid->id == id)
626 return sid->evsel;
627 }
628 return NULL;
629}
630
aece948f 631union perf_event *perf_evlist__mmap_read(struct perf_evlist *evlist, int idx)
04391deb 632{
aece948f 633 struct perf_mmap *md = &evlist->mmap[idx];
04391deb
ACM
634 unsigned int head = perf_mmap__read_head(md);
635 unsigned int old = md->prev;
636 unsigned char *data = md->base + page_size;
8115d60c 637 union perf_event *event = NULL;
04391deb 638
7bb41152 639 if (evlist->overwrite) {
04391deb 640 /*
7bb41152
ACM
641 * If we're further behind than half the buffer, there's a chance
642 * the writer will bite our tail and mess up the samples under us.
643 *
644 * If we somehow ended up ahead of the head, we got messed up.
645 *
646 * In either case, truncate and restart at head.
04391deb 647 */
7bb41152
ACM
648 int diff = head - old;
649 if (diff > md->mask / 2 || diff < 0) {
650 fprintf(stderr, "WARNING: failed to keep up with mmap data.\n");
651
652 /*
653 * head points to a known good entry, start there.
654 */
655 old = head;
656 }
04391deb
ACM
657 }
658
659 if (old != head) {
660 size_t size;
661
8115d60c 662 event = (union perf_event *)&data[old & md->mask];
04391deb
ACM
663 size = event->header.size;
664
665 /*
666 * Event straddles the mmap boundary -- header should always
667 * be inside due to u64 alignment of output.
668 */
669 if ((old & md->mask) + size != ((old + size) & md->mask)) {
670 unsigned int offset = old;
671 unsigned int len = min(sizeof(*event), size), cpy;
a65cb4b9 672 void *dst = md->event_copy;
04391deb
ACM
673
674 do {
675 cpy = min(md->mask + 1 - (offset & md->mask), len);
676 memcpy(dst, &data[offset & md->mask], cpy);
677 offset += cpy;
678 dst += cpy;
679 len -= cpy;
680 } while (len);
681
a65cb4b9 682 event = (union perf_event *) md->event_copy;
04391deb
ACM
683 }
684
685 old += size;
686 }
687
688 md->prev = old;
7bb41152 689
04391deb
ACM
690 return event;
691}
f8a95309 692
8e50d384
ZZ
693void perf_evlist__mmap_consume(struct perf_evlist *evlist, int idx)
694{
695 if (!evlist->overwrite) {
696 struct perf_mmap *md = &evlist->mmap[idx];
697 unsigned int old = md->prev;
698
699 perf_mmap__write_tail(md, old);
700 }
701}
702
93edcbd9
AH
703static void __perf_evlist__munmap(struct perf_evlist *evlist, int idx)
704{
705 if (evlist->mmap[idx].base != NULL) {
706 munmap(evlist->mmap[idx].base, evlist->mmap_len);
707 evlist->mmap[idx].base = NULL;
708 }
709}
710
7e2ed097 711void perf_evlist__munmap(struct perf_evlist *evlist)
f8a95309 712{
aece948f 713 int i;
f8a95309 714
983874d1
ACM
715 if (evlist->mmap == NULL)
716 return;
717
93edcbd9
AH
718 for (i = 0; i < evlist->nr_mmaps; i++)
719 __perf_evlist__munmap(evlist, i);
aece948f 720
04662523 721 zfree(&evlist->mmap);
f8a95309
ACM
722}
723
806fb630 724static int perf_evlist__alloc_mmap(struct perf_evlist *evlist)
f8a95309 725{
a14bb7a6 726 evlist->nr_mmaps = cpu_map__nr(evlist->cpus);
ec1e7e43 727 if (cpu_map__empty(evlist->cpus))
b3a319d5 728 evlist->nr_mmaps = thread_map__nr(evlist->threads);
aece948f 729 evlist->mmap = zalloc(evlist->nr_mmaps * sizeof(struct perf_mmap));
f8a95309
ACM
730 return evlist->mmap != NULL ? 0 : -ENOMEM;
731}
732
a8a8f3eb
AH
733struct mmap_params {
734 int prot;
735 int mask;
736};
737
738static int __perf_evlist__mmap(struct perf_evlist *evlist, int idx,
739 struct mmap_params *mp, int fd)
f8a95309 740{
aece948f 741 evlist->mmap[idx].prev = 0;
a8a8f3eb
AH
742 evlist->mmap[idx].mask = mp->mask;
743 evlist->mmap[idx].base = mmap(NULL, evlist->mmap_len, mp->prot,
f8a95309 744 MAP_SHARED, fd, 0);
301b195d 745 if (evlist->mmap[idx].base == MAP_FAILED) {
02635965
AH
746 pr_debug2("failed to mmap perf event ring buffer, error %d\n",
747 errno);
301b195d 748 evlist->mmap[idx].base = NULL;
f8a95309 749 return -1;
301b195d 750 }
ad6765dd 751
f8a95309
ACM
752 return 0;
753}
754
04e21314 755static int perf_evlist__mmap_per_evsel(struct perf_evlist *evlist, int idx,
a8a8f3eb
AH
756 struct mmap_params *mp, int cpu,
757 int thread, int *output)
aece948f
ACM
758{
759 struct perf_evsel *evsel;
04e21314 760
0050f7aa 761 evlist__for_each(evlist, evsel) {
bf8e8f4b
AH
762 int fd;
763
764 if (evsel->system_wide && thread)
765 continue;
766
767 fd = FD(evsel, cpu, thread);
04e21314
AH
768
769 if (*output == -1) {
770 *output = fd;
a8a8f3eb 771 if (__perf_evlist__mmap(evlist, idx, mp, *output) < 0)
04e21314
AH
772 return -1;
773 } else {
774 if (ioctl(fd, PERF_EVENT_IOC_SET_OUTPUT, *output) != 0)
775 return -1;
776 }
777
ad6765dd
ACM
778 if (perf_evlist__add_pollfd(evlist, fd) < 0)
779 return -1;
033fa713 780
04e21314
AH
781 if ((evsel->attr.read_format & PERF_FORMAT_ID) &&
782 perf_evlist__id_add_fd(evlist, evsel, cpu, thread, fd) < 0)
783 return -1;
784 }
785
786 return 0;
787}
788
a8a8f3eb
AH
789static int perf_evlist__mmap_per_cpu(struct perf_evlist *evlist,
790 struct mmap_params *mp)
04e21314 791{
aece948f 792 int cpu, thread;
b3a319d5
NK
793 int nr_cpus = cpu_map__nr(evlist->cpus);
794 int nr_threads = thread_map__nr(evlist->threads);
aece948f 795
e3e1a54f 796 pr_debug2("perf event ring buffer mmapped per cpu\n");
b3a319d5 797 for (cpu = 0; cpu < nr_cpus; cpu++) {
aece948f
ACM
798 int output = -1;
799
b3a319d5 800 for (thread = 0; thread < nr_threads; thread++) {
a8a8f3eb
AH
801 if (perf_evlist__mmap_per_evsel(evlist, cpu, mp, cpu,
802 thread, &output))
04e21314 803 goto out_unmap;
aece948f
ACM
804 }
805 }
806
807 return 0;
808
809out_unmap:
93edcbd9
AH
810 for (cpu = 0; cpu < nr_cpus; cpu++)
811 __perf_evlist__munmap(evlist, cpu);
aece948f
ACM
812 return -1;
813}
814
a8a8f3eb
AH
815static int perf_evlist__mmap_per_thread(struct perf_evlist *evlist,
816 struct mmap_params *mp)
aece948f 817{
aece948f 818 int thread;
b3a319d5 819 int nr_threads = thread_map__nr(evlist->threads);
aece948f 820
e3e1a54f 821 pr_debug2("perf event ring buffer mmapped per thread\n");
b3a319d5 822 for (thread = 0; thread < nr_threads; thread++) {
aece948f
ACM
823 int output = -1;
824
a8a8f3eb
AH
825 if (perf_evlist__mmap_per_evsel(evlist, thread, mp, 0, thread,
826 &output))
04e21314 827 goto out_unmap;
aece948f
ACM
828 }
829
830 return 0;
831
832out_unmap:
93edcbd9
AH
833 for (thread = 0; thread < nr_threads; thread++)
834 __perf_evlist__munmap(evlist, thread);
aece948f
ACM
835 return -1;
836}
837
994a1f78
JO
838static size_t perf_evlist__mmap_size(unsigned long pages)
839{
840 /* 512 kiB: default amount of unprivileged mlocked memory */
841 if (pages == UINT_MAX)
842 pages = (512 * 1024) / page_size;
843 else if (!is_power_of_2(pages))
844 return 0;
845
846 return (pages + 1) * page_size;
847}
848
33c2dcfd
DA
849static long parse_pages_arg(const char *str, unsigned long min,
850 unsigned long max)
994a1f78 851{
2fbe4abe 852 unsigned long pages, val;
27050f53
JO
853 static struct parse_tag tags[] = {
854 { .tag = 'B', .mult = 1 },
855 { .tag = 'K', .mult = 1 << 10 },
856 { .tag = 'M', .mult = 1 << 20 },
857 { .tag = 'G', .mult = 1 << 30 },
858 { .tag = 0 },
859 };
994a1f78 860
8973504b 861 if (str == NULL)
33c2dcfd 862 return -EINVAL;
8973504b 863
27050f53 864 val = parse_tag_value(str, tags);
2fbe4abe 865 if (val != (unsigned long) -1) {
27050f53
JO
866 /* we got file size value */
867 pages = PERF_ALIGN(val, page_size) / page_size;
27050f53
JO
868 } else {
869 /* we got pages count value */
870 char *eptr;
871 pages = strtoul(str, &eptr, 10);
33c2dcfd
DA
872 if (*eptr != '\0')
873 return -EINVAL;
994a1f78
JO
874 }
875
2bcab6c1 876 if (pages == 0 && min == 0) {
33c2dcfd 877 /* leave number of pages at 0 */
1dbfa938 878 } else if (!is_power_of_2(pages)) {
33c2dcfd 879 /* round pages up to next power of 2 */
1dbfa938
AH
880 pages = next_pow2_l(pages);
881 if (!pages)
882 return -EINVAL;
9639837e
DA
883 pr_info("rounding mmap pages size to %lu bytes (%lu pages)\n",
884 pages * page_size, pages);
2fbe4abe
AH
885 }
886
33c2dcfd
DA
887 if (pages > max)
888 return -EINVAL;
889
890 return pages;
891}
892
893int perf_evlist__parse_mmap_pages(const struct option *opt, const char *str,
894 int unset __maybe_unused)
895{
896 unsigned int *mmap_pages = opt->value;
897 unsigned long max = UINT_MAX;
898 long pages;
899
f5ae9c42 900 if (max > SIZE_MAX / page_size)
33c2dcfd
DA
901 max = SIZE_MAX / page_size;
902
903 pages = parse_pages_arg(str, 1, max);
904 if (pages < 0) {
905 pr_err("Invalid argument for --mmap_pages/-m\n");
994a1f78
JO
906 return -1;
907 }
908
909 *mmap_pages = pages;
910 return 0;
911}
912
c83fa7f2
AH
913/**
914 * perf_evlist__mmap - Create mmaps to receive events.
915 * @evlist: list of events
916 * @pages: map length in pages
917 * @overwrite: overwrite older events?
f8a95309 918 *
c83fa7f2
AH
919 * If @overwrite is %false the user needs to signal event consumption using
920 * perf_mmap__write_tail(). Using perf_evlist__mmap_read() does this
921 * automatically.
7e2ed097 922 *
c83fa7f2 923 * Return: %0 on success, negative error code otherwise.
f8a95309 924 */
50a682ce
ACM
925int perf_evlist__mmap(struct perf_evlist *evlist, unsigned int pages,
926 bool overwrite)
f8a95309 927{
aece948f 928 struct perf_evsel *evsel;
7e2ed097
ACM
929 const struct cpu_map *cpus = evlist->cpus;
930 const struct thread_map *threads = evlist->threads;
a8a8f3eb
AH
931 struct mmap_params mp = {
932 .prot = PROT_READ | (overwrite ? 0 : PROT_WRITE),
933 };
50a682ce 934
7e2ed097 935 if (evlist->mmap == NULL && perf_evlist__alloc_mmap(evlist) < 0)
f8a95309
ACM
936 return -ENOMEM;
937
7e2ed097 938 if (evlist->pollfd == NULL && perf_evlist__alloc_pollfd(evlist) < 0)
f8a95309
ACM
939 return -ENOMEM;
940
941 evlist->overwrite = overwrite;
994a1f78 942 evlist->mmap_len = perf_evlist__mmap_size(pages);
2af68ef5 943 pr_debug("mmap size %zuB\n", evlist->mmap_len);
a8a8f3eb 944 mp.mask = evlist->mmap_len - page_size - 1;
f8a95309 945
0050f7aa 946 evlist__for_each(evlist, evsel) {
f8a95309 947 if ((evsel->attr.read_format & PERF_FORMAT_ID) &&
a91e5431 948 evsel->sample_id == NULL &&
a14bb7a6 949 perf_evsel__alloc_id(evsel, cpu_map__nr(cpus), threads->nr) < 0)
f8a95309 950 return -ENOMEM;
f8a95309
ACM
951 }
952
ec1e7e43 953 if (cpu_map__empty(cpus))
a8a8f3eb 954 return perf_evlist__mmap_per_thread(evlist, &mp);
f8a95309 955
a8a8f3eb 956 return perf_evlist__mmap_per_cpu(evlist, &mp);
f8a95309 957}
7e2ed097 958
602ad878 959int perf_evlist__create_maps(struct perf_evlist *evlist, struct target *target)
7e2ed097 960{
b809ac10
NK
961 evlist->threads = thread_map__new_str(target->pid, target->tid,
962 target->uid);
7e2ed097
ACM
963
964 if (evlist->threads == NULL)
965 return -1;
966
9c105fbc 967 if (target__uses_dummy_map(target))
d1cb9fce 968 evlist->cpus = cpu_map__dummy_new();
879d77d0
NK
969 else
970 evlist->cpus = cpu_map__new(target->cpu_list);
7e2ed097
ACM
971
972 if (evlist->cpus == NULL)
973 goto out_delete_threads;
974
975 return 0;
976
977out_delete_threads:
978 thread_map__delete(evlist->threads);
979 return -1;
980}
981
1491a632 982int perf_evlist__apply_filters(struct perf_evlist *evlist)
0a102479 983{
0a102479 984 struct perf_evsel *evsel;
745cefc5
ACM
985 int err = 0;
986 const int ncpus = cpu_map__nr(evlist->cpus),
b3a319d5 987 nthreads = thread_map__nr(evlist->threads);
0a102479 988
0050f7aa 989 evlist__for_each(evlist, evsel) {
745cefc5 990 if (evsel->filter == NULL)
0a102479 991 continue;
745cefc5
ACM
992
993 err = perf_evsel__set_filter(evsel, ncpus, nthreads, evsel->filter);
994 if (err)
995 break;
0a102479
FW
996 }
997
745cefc5
ACM
998 return err;
999}
1000
1001int perf_evlist__set_filter(struct perf_evlist *evlist, const char *filter)
1002{
1003 struct perf_evsel *evsel;
1004 int err = 0;
1005 const int ncpus = cpu_map__nr(evlist->cpus),
b3a319d5 1006 nthreads = thread_map__nr(evlist->threads);
745cefc5 1007
0050f7aa 1008 evlist__for_each(evlist, evsel) {
745cefc5
ACM
1009 err = perf_evsel__set_filter(evsel, ncpus, nthreads, filter);
1010 if (err)
1011 break;
1012 }
1013
1014 return err;
0a102479 1015}
74429964 1016
0c21f736 1017bool perf_evlist__valid_sample_type(struct perf_evlist *evlist)
74429964 1018{
75562573 1019 struct perf_evsel *pos;
c2a70653 1020
75562573
AH
1021 if (evlist->nr_entries == 1)
1022 return true;
1023
1024 if (evlist->id_pos < 0 || evlist->is_pos < 0)
1025 return false;
1026
0050f7aa 1027 evlist__for_each(evlist, pos) {
75562573
AH
1028 if (pos->id_pos != evlist->id_pos ||
1029 pos->is_pos != evlist->is_pos)
c2a70653 1030 return false;
74429964
FW
1031 }
1032
c2a70653 1033 return true;
74429964
FW
1034}
1035
75562573 1036u64 __perf_evlist__combined_sample_type(struct perf_evlist *evlist)
c2a70653 1037{
75562573
AH
1038 struct perf_evsel *evsel;
1039
1040 if (evlist->combined_sample_type)
1041 return evlist->combined_sample_type;
1042
0050f7aa 1043 evlist__for_each(evlist, evsel)
75562573
AH
1044 evlist->combined_sample_type |= evsel->attr.sample_type;
1045
1046 return evlist->combined_sample_type;
1047}
1048
1049u64 perf_evlist__combined_sample_type(struct perf_evlist *evlist)
1050{
1051 evlist->combined_sample_type = 0;
1052 return __perf_evlist__combined_sample_type(evlist);
c2a70653
ACM
1053}
1054
9ede473c
JO
1055bool perf_evlist__valid_read_format(struct perf_evlist *evlist)
1056{
1057 struct perf_evsel *first = perf_evlist__first(evlist), *pos = first;
1058 u64 read_format = first->attr.read_format;
1059 u64 sample_type = first->attr.sample_type;
1060
0050f7aa 1061 evlist__for_each(evlist, pos) {
9ede473c
JO
1062 if (read_format != pos->attr.read_format)
1063 return false;
1064 }
1065
1066 /* PERF_SAMPLE_READ imples PERF_FORMAT_ID. */
1067 if ((sample_type & PERF_SAMPLE_READ) &&
1068 !(read_format & PERF_FORMAT_ID)) {
1069 return false;
1070 }
1071
1072 return true;
1073}
1074
1075u64 perf_evlist__read_format(struct perf_evlist *evlist)
1076{
1077 struct perf_evsel *first = perf_evlist__first(evlist);
1078 return first->attr.read_format;
1079}
1080
0c21f736 1081u16 perf_evlist__id_hdr_size(struct perf_evlist *evlist)
81e36bff 1082{
0c21f736 1083 struct perf_evsel *first = perf_evlist__first(evlist);
81e36bff
ACM
1084 struct perf_sample *data;
1085 u64 sample_type;
1086 u16 size = 0;
1087
81e36bff
ACM
1088 if (!first->attr.sample_id_all)
1089 goto out;
1090
1091 sample_type = first->attr.sample_type;
1092
1093 if (sample_type & PERF_SAMPLE_TID)
1094 size += sizeof(data->tid) * 2;
1095
1096 if (sample_type & PERF_SAMPLE_TIME)
1097 size += sizeof(data->time);
1098
1099 if (sample_type & PERF_SAMPLE_ID)
1100 size += sizeof(data->id);
1101
1102 if (sample_type & PERF_SAMPLE_STREAM_ID)
1103 size += sizeof(data->stream_id);
1104
1105 if (sample_type & PERF_SAMPLE_CPU)
1106 size += sizeof(data->cpu) * 2;
75562573
AH
1107
1108 if (sample_type & PERF_SAMPLE_IDENTIFIER)
1109 size += sizeof(data->id);
81e36bff
ACM
1110out:
1111 return size;
1112}
1113
0c21f736 1114bool perf_evlist__valid_sample_id_all(struct perf_evlist *evlist)
74429964 1115{
0c21f736 1116 struct perf_evsel *first = perf_evlist__first(evlist), *pos = first;
c2a70653 1117
0050f7aa 1118 evlist__for_each_continue(evlist, pos) {
c2a70653
ACM
1119 if (first->attr.sample_id_all != pos->attr.sample_id_all)
1120 return false;
74429964
FW
1121 }
1122
c2a70653
ACM
1123 return true;
1124}
1125
0c21f736 1126bool perf_evlist__sample_id_all(struct perf_evlist *evlist)
c2a70653 1127{
0c21f736 1128 struct perf_evsel *first = perf_evlist__first(evlist);
c2a70653 1129 return first->attr.sample_id_all;
74429964 1130}
81cce8de
ACM
1131
1132void perf_evlist__set_selected(struct perf_evlist *evlist,
1133 struct perf_evsel *evsel)
1134{
1135 evlist->selected = evsel;
1136}
727ab04e 1137
a74b4b66
NK
1138void perf_evlist__close(struct perf_evlist *evlist)
1139{
1140 struct perf_evsel *evsel;
1141 int ncpus = cpu_map__nr(evlist->cpus);
1142 int nthreads = thread_map__nr(evlist->threads);
8ad9219e 1143 int n;
a74b4b66 1144
8ad9219e
SE
1145 evlist__for_each_reverse(evlist, evsel) {
1146 n = evsel->cpus ? evsel->cpus->nr : ncpus;
1147 perf_evsel__close(evsel, n, nthreads);
1148 }
a74b4b66
NK
1149}
1150
6a4bb04c 1151int perf_evlist__open(struct perf_evlist *evlist)
727ab04e 1152{
6a4bb04c 1153 struct perf_evsel *evsel;
a74b4b66 1154 int err;
727ab04e 1155
733cd2fe
AH
1156 perf_evlist__update_id_pos(evlist);
1157
0050f7aa 1158 evlist__for_each(evlist, evsel) {
6a4bb04c 1159 err = perf_evsel__open(evsel, evlist->cpus, evlist->threads);
727ab04e
ACM
1160 if (err < 0)
1161 goto out_err;
1162 }
1163
1164 return 0;
1165out_err:
a74b4b66 1166 perf_evlist__close(evlist);
41c21a68 1167 errno = -err;
727ab04e
ACM
1168 return err;
1169}
35b9d88e 1170
602ad878 1171int perf_evlist__prepare_workload(struct perf_evlist *evlist, struct target *target,
55e162ea 1172 const char *argv[], bool pipe_output,
735f7e0b 1173 void (*exec_error)(int signo, siginfo_t *info, void *ucontext))
35b9d88e
ACM
1174{
1175 int child_ready_pipe[2], go_pipe[2];
1176 char bf;
1177
1178 if (pipe(child_ready_pipe) < 0) {
1179 perror("failed to create 'ready' pipe");
1180 return -1;
1181 }
1182
1183 if (pipe(go_pipe) < 0) {
1184 perror("failed to create 'go' pipe");
1185 goto out_close_ready_pipe;
1186 }
1187
1188 evlist->workload.pid = fork();
1189 if (evlist->workload.pid < 0) {
1190 perror("failed to fork");
1191 goto out_close_pipes;
1192 }
1193
1194 if (!evlist->workload.pid) {
5f1c4225
ACM
1195 int ret;
1196
119fa3c9 1197 if (pipe_output)
35b9d88e
ACM
1198 dup2(2, 1);
1199
0817df08
DA
1200 signal(SIGTERM, SIG_DFL);
1201
35b9d88e
ACM
1202 close(child_ready_pipe[0]);
1203 close(go_pipe[1]);
1204 fcntl(go_pipe[0], F_SETFD, FD_CLOEXEC);
1205
35b9d88e
ACM
1206 /*
1207 * Tell the parent we're ready to go
1208 */
1209 close(child_ready_pipe[1]);
1210
1211 /*
1212 * Wait until the parent tells us to go.
1213 */
5f1c4225
ACM
1214 ret = read(go_pipe[0], &bf, 1);
1215 /*
1216 * The parent will ask for the execvp() to be performed by
1217 * writing exactly one byte, in workload.cork_fd, usually via
1218 * perf_evlist__start_workload().
1219 *
1220 * For cancelling the workload without actuallin running it,
1221 * the parent will just close workload.cork_fd, without writing
1222 * anything, i.e. read will return zero and we just exit()
1223 * here.
1224 */
1225 if (ret != 1) {
1226 if (ret == -1)
1227 perror("unable to read pipe");
1228 exit(ret);
1229 }
35b9d88e
ACM
1230
1231 execvp(argv[0], (char **)argv);
1232
735f7e0b 1233 if (exec_error) {
f33cbe72
ACM
1234 union sigval val;
1235
1236 val.sival_int = errno;
1237 if (sigqueue(getppid(), SIGUSR1, val))
1238 perror(argv[0]);
1239 } else
1240 perror(argv[0]);
35b9d88e
ACM
1241 exit(-1);
1242 }
1243
735f7e0b
ACM
1244 if (exec_error) {
1245 struct sigaction act = {
1246 .sa_flags = SA_SIGINFO,
1247 .sa_sigaction = exec_error,
1248 };
1249 sigaction(SIGUSR1, &act, NULL);
1250 }
1251
602ad878 1252 if (target__none(target))
35b9d88e
ACM
1253 evlist->threads->map[0] = evlist->workload.pid;
1254
1255 close(child_ready_pipe[1]);
1256 close(go_pipe[0]);
1257 /*
1258 * wait for child to settle
1259 */
1260 if (read(child_ready_pipe[0], &bf, 1) == -1) {
1261 perror("unable to read pipe");
1262 goto out_close_pipes;
1263 }
1264
bcf3145f 1265 fcntl(go_pipe[1], F_SETFD, FD_CLOEXEC);
35b9d88e
ACM
1266 evlist->workload.cork_fd = go_pipe[1];
1267 close(child_ready_pipe[0]);
1268 return 0;
1269
1270out_close_pipes:
1271 close(go_pipe[0]);
1272 close(go_pipe[1]);
1273out_close_ready_pipe:
1274 close(child_ready_pipe[0]);
1275 close(child_ready_pipe[1]);
1276 return -1;
1277}
1278
1279int perf_evlist__start_workload(struct perf_evlist *evlist)
1280{
1281 if (evlist->workload.cork_fd > 0) {
b3824404 1282 char bf = 0;
bcf3145f 1283 int ret;
35b9d88e
ACM
1284 /*
1285 * Remove the cork, let it rip!
1286 */
bcf3145f
NK
1287 ret = write(evlist->workload.cork_fd, &bf, 1);
1288 if (ret < 0)
1289 perror("enable to write to pipe");
1290
1291 close(evlist->workload.cork_fd);
1292 return ret;
35b9d88e
ACM
1293 }
1294
1295 return 0;
1296}
cb0b29e0 1297
a3f698fe 1298int perf_evlist__parse_sample(struct perf_evlist *evlist, union perf_event *event,
0807d2d8 1299 struct perf_sample *sample)
cb0b29e0 1300{
75562573
AH
1301 struct perf_evsel *evsel = perf_evlist__event2evsel(evlist, event);
1302
1303 if (!evsel)
1304 return -EFAULT;
0807d2d8 1305 return perf_evsel__parse_sample(evsel, event, sample);
cb0b29e0 1306}
78f067b3
ACM
1307
1308size_t perf_evlist__fprintf(struct perf_evlist *evlist, FILE *fp)
1309{
1310 struct perf_evsel *evsel;
1311 size_t printed = 0;
1312
0050f7aa 1313 evlist__for_each(evlist, evsel) {
78f067b3
ACM
1314 printed += fprintf(fp, "%s%s", evsel->idx ? ", " : "",
1315 perf_evsel__name(evsel));
1316 }
1317
b2222139 1318 return printed + fprintf(fp, "\n");
78f067b3 1319}
6ef068cb
ACM
1320
1321int perf_evlist__strerror_tp(struct perf_evlist *evlist __maybe_unused,
1322 int err, char *buf, size_t size)
1323{
1324 char sbuf[128];
1325
1326 switch (err) {
1327 case ENOENT:
1328 scnprintf(buf, size, "%s",
1329 "Error:\tUnable to find debugfs\n"
1330 "Hint:\tWas your kernel was compiled with debugfs support?\n"
1331 "Hint:\tIs the debugfs filesystem mounted?\n"
1332 "Hint:\tTry 'sudo mount -t debugfs nodev /sys/kernel/debug'");
1333 break;
1334 case EACCES:
1335 scnprintf(buf, size,
1336 "Error:\tNo permissions to read %s/tracing/events/raw_syscalls\n"
1337 "Hint:\tTry 'sudo mount -o remount,mode=755 %s'\n",
1338 debugfs_mountpoint, debugfs_mountpoint);
1339 break;
1340 default:
1341 scnprintf(buf, size, "%s", strerror_r(err, sbuf, sizeof(sbuf)));
1342 break;
1343 }
1344
1345 return 0;
1346}
a8f23d8f
ACM
1347
1348int perf_evlist__strerror_open(struct perf_evlist *evlist __maybe_unused,
1349 int err, char *buf, size_t size)
1350{
1351 int printed, value;
6e81c74c 1352 char sbuf[STRERR_BUFSIZE], *emsg = strerror_r(err, sbuf, sizeof(sbuf));
a8f23d8f
ACM
1353
1354 switch (err) {
1355 case EACCES:
1356 case EPERM:
1357 printed = scnprintf(buf, size,
1358 "Error:\t%s.\n"
1359 "Hint:\tCheck /proc/sys/kernel/perf_event_paranoid setting.", emsg);
1360
1a47245d 1361 value = perf_event_paranoid();
a8f23d8f
ACM
1362
1363 printed += scnprintf(buf + printed, size - printed, "\nHint:\t");
1364
1365 if (value >= 2) {
1366 printed += scnprintf(buf + printed, size - printed,
1367 "For your workloads it needs to be <= 1\nHint:\t");
1368 }
1369 printed += scnprintf(buf + printed, size - printed,
5229e366 1370 "For system wide tracing it needs to be set to -1.\n");
a8f23d8f
ACM
1371
1372 printed += scnprintf(buf + printed, size - printed,
5229e366
ACM
1373 "Hint:\tTry: 'sudo sh -c \"echo -1 > /proc/sys/kernel/perf_event_paranoid\"'\n"
1374 "Hint:\tThe current value is %d.", value);
a8f23d8f
ACM
1375 break;
1376 default:
1377 scnprintf(buf, size, "%s", emsg);
1378 break;
1379 }
1380
1381 return 0;
1382}
a025e4f0
AH
1383
1384void perf_evlist__to_front(struct perf_evlist *evlist,
1385 struct perf_evsel *move_evsel)
1386{
1387 struct perf_evsel *evsel, *n;
1388 LIST_HEAD(move);
1389
1390 if (move_evsel == perf_evlist__first(evlist))
1391 return;
1392
0050f7aa 1393 evlist__for_each_safe(evlist, n, evsel) {
a025e4f0
AH
1394 if (evsel->leader == move_evsel->leader)
1395 list_move_tail(&evsel->node, &move);
1396 }
1397
1398 list_splice(&move, &evlist->entries);
1399}
60b0896c
AH
1400
1401void perf_evlist__set_tracking_event(struct perf_evlist *evlist,
1402 struct perf_evsel *tracking_evsel)
1403{
1404 struct perf_evsel *evsel;
1405
1406 if (tracking_evsel->tracking)
1407 return;
1408
1409 evlist__for_each(evlist, evsel) {
1410 if (evsel != tracking_evsel)
1411 evsel->tracking = false;
1412 }
1413
1414 tracking_evsel->tracking = true;
1415}