2 * Copyright (C) 2011, Red Hat Inc, Arnaldo Carvalho de Melo <acme@redhat.com>
4 * Parts came from builtin-{top,stat,record}.c, see those files for further
7 * Released under the GPL v2. (and only v2, not any later version)
10 #include <api/fs/fs.h>
13 #include "thread_map.h"
20 #include "parse-events.h"
21 #include <subcmd/parse-options.h>
25 #include <linux/bitops.h>
26 #include <linux/hash.h>
27 #include <linux/log2.h>
28 #include <linux/err.h>
30 static void perf_evlist__mmap_put(struct perf_evlist
*evlist
, int idx
);
31 static void __perf_evlist__munmap(struct perf_evlist
*evlist
, int idx
);
33 #define FD(e, x, y) (*(int *)xyarray__entry(e->fd, x, y))
34 #define SID(e, x, y) xyarray__entry(e->sample_id, x, y)
36 void perf_evlist__init(struct perf_evlist
*evlist
, struct cpu_map
*cpus
,
37 struct thread_map
*threads
)
41 for (i
= 0; i
< PERF_EVLIST__HLIST_SIZE
; ++i
)
42 INIT_HLIST_HEAD(&evlist
->heads
[i
]);
43 INIT_LIST_HEAD(&evlist
->entries
);
44 perf_evlist__set_maps(evlist
, cpus
, threads
);
45 fdarray__init(&evlist
->pollfd
, 64);
46 evlist
->workload
.pid
= -1;
47 evlist
->backward
= false;
50 struct perf_evlist
*perf_evlist__new(void)
52 struct perf_evlist
*evlist
= zalloc(sizeof(*evlist
));
55 perf_evlist__init(evlist
, NULL
, NULL
);
60 struct perf_evlist
*perf_evlist__new_default(void)
62 struct perf_evlist
*evlist
= perf_evlist__new();
64 if (evlist
&& perf_evlist__add_default(evlist
)) {
65 perf_evlist__delete(evlist
);
72 struct perf_evlist
*perf_evlist__new_dummy(void)
74 struct perf_evlist
*evlist
= perf_evlist__new();
76 if (evlist
&& perf_evlist__add_dummy(evlist
)) {
77 perf_evlist__delete(evlist
);
85 * perf_evlist__set_id_pos - set the positions of event ids.
86 * @evlist: selected event list
88 * Events with compatible sample types all have the same id_pos
89 * and is_pos. For convenience, put a copy on evlist.
91 void perf_evlist__set_id_pos(struct perf_evlist
*evlist
)
93 struct perf_evsel
*first
= perf_evlist__first(evlist
);
95 evlist
->id_pos
= first
->id_pos
;
96 evlist
->is_pos
= first
->is_pos
;
99 static void perf_evlist__update_id_pos(struct perf_evlist
*evlist
)
101 struct perf_evsel
*evsel
;
103 evlist__for_each(evlist
, evsel
)
104 perf_evsel__calc_id_pos(evsel
);
106 perf_evlist__set_id_pos(evlist
);
109 static void perf_evlist__purge(struct perf_evlist
*evlist
)
111 struct perf_evsel
*pos
, *n
;
113 evlist__for_each_safe(evlist
, n
, pos
) {
114 list_del_init(&pos
->node
);
116 perf_evsel__delete(pos
);
119 evlist
->nr_entries
= 0;
122 void perf_evlist__exit(struct perf_evlist
*evlist
)
124 zfree(&evlist
->mmap
);
125 fdarray__exit(&evlist
->pollfd
);
128 void perf_evlist__delete(struct perf_evlist
*evlist
)
130 perf_evlist__munmap(evlist
);
131 perf_evlist__close(evlist
);
132 cpu_map__put(evlist
->cpus
);
133 thread_map__put(evlist
->threads
);
135 evlist
->threads
= NULL
;
136 perf_evlist__purge(evlist
);
137 perf_evlist__exit(evlist
);
141 static void __perf_evlist__propagate_maps(struct perf_evlist
*evlist
,
142 struct perf_evsel
*evsel
)
145 * We already have cpus for evsel (via PMU sysfs) so
146 * keep it, if there's no target cpu list defined.
148 if (!evsel
->own_cpus
|| evlist
->has_user_cpus
) {
149 cpu_map__put(evsel
->cpus
);
150 evsel
->cpus
= cpu_map__get(evlist
->cpus
);
151 } else if (evsel
->cpus
!= evsel
->own_cpus
) {
152 cpu_map__put(evsel
->cpus
);
153 evsel
->cpus
= cpu_map__get(evsel
->own_cpus
);
156 thread_map__put(evsel
->threads
);
157 evsel
->threads
= thread_map__get(evlist
->threads
);
160 static void perf_evlist__propagate_maps(struct perf_evlist
*evlist
)
162 struct perf_evsel
*evsel
;
164 evlist__for_each(evlist
, evsel
)
165 __perf_evlist__propagate_maps(evlist
, evsel
);
168 void perf_evlist__add(struct perf_evlist
*evlist
, struct perf_evsel
*entry
)
170 entry
->evlist
= evlist
;
171 list_add_tail(&entry
->node
, &evlist
->entries
);
172 entry
->idx
= evlist
->nr_entries
;
173 entry
->tracking
= !entry
->idx
;
175 if (!evlist
->nr_entries
++)
176 perf_evlist__set_id_pos(evlist
);
178 __perf_evlist__propagate_maps(evlist
, entry
);
181 void perf_evlist__remove(struct perf_evlist
*evlist
, struct perf_evsel
*evsel
)
183 evsel
->evlist
= NULL
;
184 list_del_init(&evsel
->node
);
185 evlist
->nr_entries
-= 1;
188 void perf_evlist__splice_list_tail(struct perf_evlist
*evlist
,
189 struct list_head
*list
)
191 struct perf_evsel
*evsel
, *temp
;
193 __evlist__for_each_safe(list
, temp
, evsel
) {
194 list_del_init(&evsel
->node
);
195 perf_evlist__add(evlist
, evsel
);
199 void __perf_evlist__set_leader(struct list_head
*list
)
201 struct perf_evsel
*evsel
, *leader
;
203 leader
= list_entry(list
->next
, struct perf_evsel
, node
);
204 evsel
= list_entry(list
->prev
, struct perf_evsel
, node
);
206 leader
->nr_members
= evsel
->idx
- leader
->idx
+ 1;
208 __evlist__for_each(list
, evsel
) {
209 evsel
->leader
= leader
;
213 void perf_evlist__set_leader(struct perf_evlist
*evlist
)
215 if (evlist
->nr_entries
) {
216 evlist
->nr_groups
= evlist
->nr_entries
> 1 ? 1 : 0;
217 __perf_evlist__set_leader(&evlist
->entries
);
221 void perf_event_attr__set_max_precise_ip(struct perf_event_attr
*attr
)
223 attr
->precise_ip
= 3;
225 while (attr
->precise_ip
!= 0) {
226 int fd
= sys_perf_event_open(attr
, 0, -1, -1, 0);
235 int perf_evlist__add_default(struct perf_evlist
*evlist
)
237 struct perf_event_attr attr
= {
238 .type
= PERF_TYPE_HARDWARE
,
239 .config
= PERF_COUNT_HW_CPU_CYCLES
,
241 struct perf_evsel
*evsel
;
243 event_attr_init(&attr
);
245 perf_event_attr__set_max_precise_ip(&attr
);
247 evsel
= perf_evsel__new(&attr
);
251 /* use asprintf() because free(evsel) assumes name is allocated */
252 if (asprintf(&evsel
->name
, "cycles%.*s",
253 attr
.precise_ip
? attr
.precise_ip
+ 1 : 0, ":ppp") < 0)
256 perf_evlist__add(evlist
, evsel
);
259 perf_evsel__delete(evsel
);
264 int perf_evlist__add_dummy(struct perf_evlist
*evlist
)
266 struct perf_event_attr attr
= {
267 .type
= PERF_TYPE_SOFTWARE
,
268 .config
= PERF_COUNT_SW_DUMMY
,
269 .size
= sizeof(attr
), /* to capture ABI version */
271 struct perf_evsel
*evsel
= perf_evsel__new(&attr
);
276 perf_evlist__add(evlist
, evsel
);
280 static int perf_evlist__add_attrs(struct perf_evlist
*evlist
,
281 struct perf_event_attr
*attrs
, size_t nr_attrs
)
283 struct perf_evsel
*evsel
, *n
;
287 for (i
= 0; i
< nr_attrs
; i
++) {
288 evsel
= perf_evsel__new_idx(attrs
+ i
, evlist
->nr_entries
+ i
);
290 goto out_delete_partial_list
;
291 list_add_tail(&evsel
->node
, &head
);
294 perf_evlist__splice_list_tail(evlist
, &head
);
298 out_delete_partial_list
:
299 __evlist__for_each_safe(&head
, n
, evsel
)
300 perf_evsel__delete(evsel
);
304 int __perf_evlist__add_default_attrs(struct perf_evlist
*evlist
,
305 struct perf_event_attr
*attrs
, size_t nr_attrs
)
309 for (i
= 0; i
< nr_attrs
; i
++)
310 event_attr_init(attrs
+ i
);
312 return perf_evlist__add_attrs(evlist
, attrs
, nr_attrs
);
316 perf_evlist__find_tracepoint_by_id(struct perf_evlist
*evlist
, int id
)
318 struct perf_evsel
*evsel
;
320 evlist__for_each(evlist
, evsel
) {
321 if (evsel
->attr
.type
== PERF_TYPE_TRACEPOINT
&&
322 (int)evsel
->attr
.config
== id
)
330 perf_evlist__find_tracepoint_by_name(struct perf_evlist
*evlist
,
333 struct perf_evsel
*evsel
;
335 evlist__for_each(evlist
, evsel
) {
336 if ((evsel
->attr
.type
== PERF_TYPE_TRACEPOINT
) &&
337 (strcmp(evsel
->name
, name
) == 0))
344 int perf_evlist__add_newtp(struct perf_evlist
*evlist
,
345 const char *sys
, const char *name
, void *handler
)
347 struct perf_evsel
*evsel
= perf_evsel__newtp(sys
, name
);
352 evsel
->handler
= handler
;
353 perf_evlist__add(evlist
, evsel
);
357 static int perf_evlist__nr_threads(struct perf_evlist
*evlist
,
358 struct perf_evsel
*evsel
)
360 if (evsel
->system_wide
)
363 return thread_map__nr(evlist
->threads
);
366 void perf_evlist__disable(struct perf_evlist
*evlist
)
368 struct perf_evsel
*pos
;
370 evlist__for_each(evlist
, pos
) {
371 if (!perf_evsel__is_group_leader(pos
) || !pos
->fd
)
373 perf_evsel__disable(pos
);
376 evlist
->enabled
= false;
379 void perf_evlist__enable(struct perf_evlist
*evlist
)
381 struct perf_evsel
*pos
;
383 evlist__for_each(evlist
, pos
) {
384 if (!perf_evsel__is_group_leader(pos
) || !pos
->fd
)
386 perf_evsel__enable(pos
);
389 evlist
->enabled
= true;
392 void perf_evlist__toggle_enable(struct perf_evlist
*evlist
)
394 (evlist
->enabled
? perf_evlist__disable
: perf_evlist__enable
)(evlist
);
397 static int perf_evlist__enable_event_cpu(struct perf_evlist
*evlist
,
398 struct perf_evsel
*evsel
, int cpu
)
401 int nr_threads
= perf_evlist__nr_threads(evlist
, evsel
);
406 for (thread
= 0; thread
< nr_threads
; thread
++) {
407 err
= ioctl(FD(evsel
, cpu
, thread
),
408 PERF_EVENT_IOC_ENABLE
, 0);
415 static int perf_evlist__enable_event_thread(struct perf_evlist
*evlist
,
416 struct perf_evsel
*evsel
,
420 int nr_cpus
= cpu_map__nr(evlist
->cpus
);
425 for (cpu
= 0; cpu
< nr_cpus
; cpu
++) {
426 err
= ioctl(FD(evsel
, cpu
, thread
), PERF_EVENT_IOC_ENABLE
, 0);
433 int perf_evlist__enable_event_idx(struct perf_evlist
*evlist
,
434 struct perf_evsel
*evsel
, int idx
)
436 bool per_cpu_mmaps
= !cpu_map__empty(evlist
->cpus
);
439 return perf_evlist__enable_event_cpu(evlist
, evsel
, idx
);
441 return perf_evlist__enable_event_thread(evlist
, evsel
, idx
);
444 int perf_evlist__alloc_pollfd(struct perf_evlist
*evlist
)
446 int nr_cpus
= cpu_map__nr(evlist
->cpus
);
447 int nr_threads
= thread_map__nr(evlist
->threads
);
449 struct perf_evsel
*evsel
;
451 evlist__for_each(evlist
, evsel
) {
452 if (evsel
->system_wide
)
455 nfds
+= nr_cpus
* nr_threads
;
458 if (fdarray__available_entries(&evlist
->pollfd
) < nfds
&&
459 fdarray__grow(&evlist
->pollfd
, nfds
) < 0)
465 static int __perf_evlist__add_pollfd(struct perf_evlist
*evlist
, int fd
, int idx
, short revent
)
467 int pos
= fdarray__add(&evlist
->pollfd
, fd
, revent
| POLLERR
| POLLHUP
);
469 * Save the idx so that when we filter out fds POLLHUP'ed we can
470 * close the associated evlist->mmap[] entry.
473 evlist
->pollfd
.priv
[pos
].idx
= idx
;
475 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
481 int perf_evlist__add_pollfd(struct perf_evlist
*evlist
, int fd
)
483 return __perf_evlist__add_pollfd(evlist
, fd
, -1, POLLIN
);
486 static void perf_evlist__munmap_filtered(struct fdarray
*fda
, int fd
,
487 void *arg __maybe_unused
)
489 struct perf_evlist
*evlist
= container_of(fda
, struct perf_evlist
, pollfd
);
491 perf_evlist__mmap_put(evlist
, fda
->priv
[fd
].idx
);
494 int perf_evlist__filter_pollfd(struct perf_evlist
*evlist
, short revents_and_mask
)
496 return fdarray__filter(&evlist
->pollfd
, revents_and_mask
,
497 perf_evlist__munmap_filtered
, NULL
);
500 int perf_evlist__poll(struct perf_evlist
*evlist
, int timeout
)
502 return fdarray__poll(&evlist
->pollfd
, timeout
);
505 static void perf_evlist__id_hash(struct perf_evlist
*evlist
,
506 struct perf_evsel
*evsel
,
507 int cpu
, int thread
, u64 id
)
510 struct perf_sample_id
*sid
= SID(evsel
, cpu
, thread
);
514 hash
= hash_64(sid
->id
, PERF_EVLIST__HLIST_BITS
);
515 hlist_add_head(&sid
->node
, &evlist
->heads
[hash
]);
518 void perf_evlist__id_add(struct perf_evlist
*evlist
, struct perf_evsel
*evsel
,
519 int cpu
, int thread
, u64 id
)
521 perf_evlist__id_hash(evlist
, evsel
, cpu
, thread
, id
);
522 evsel
->id
[evsel
->ids
++] = id
;
525 int perf_evlist__id_add_fd(struct perf_evlist
*evlist
,
526 struct perf_evsel
*evsel
,
527 int cpu
, int thread
, int fd
)
529 u64 read_data
[4] = { 0, };
530 int id_idx
= 1; /* The first entry is the counter value */
534 ret
= ioctl(fd
, PERF_EVENT_IOC_ID
, &id
);
541 /* Legacy way to get event id.. All hail to old kernels! */
544 * This way does not work with group format read, so bail
547 if (perf_evlist__read_format(evlist
) & PERF_FORMAT_GROUP
)
550 if (!(evsel
->attr
.read_format
& PERF_FORMAT_ID
) ||
551 read(fd
, &read_data
, sizeof(read_data
)) == -1)
554 if (evsel
->attr
.read_format
& PERF_FORMAT_TOTAL_TIME_ENABLED
)
556 if (evsel
->attr
.read_format
& PERF_FORMAT_TOTAL_TIME_RUNNING
)
559 id
= read_data
[id_idx
];
562 perf_evlist__id_add(evlist
, evsel
, cpu
, thread
, id
);
566 static void perf_evlist__set_sid_idx(struct perf_evlist
*evlist
,
567 struct perf_evsel
*evsel
, int idx
, int cpu
,
570 struct perf_sample_id
*sid
= SID(evsel
, cpu
, thread
);
572 if (evlist
->cpus
&& cpu
>= 0)
573 sid
->cpu
= evlist
->cpus
->map
[cpu
];
576 if (!evsel
->system_wide
&& evlist
->threads
&& thread
>= 0)
577 sid
->tid
= thread_map__pid(evlist
->threads
, thread
);
582 struct perf_sample_id
*perf_evlist__id2sid(struct perf_evlist
*evlist
, u64 id
)
584 struct hlist_head
*head
;
585 struct perf_sample_id
*sid
;
588 hash
= hash_64(id
, PERF_EVLIST__HLIST_BITS
);
589 head
= &evlist
->heads
[hash
];
591 hlist_for_each_entry(sid
, head
, node
)
598 struct perf_evsel
*perf_evlist__id2evsel(struct perf_evlist
*evlist
, u64 id
)
600 struct perf_sample_id
*sid
;
602 if (evlist
->nr_entries
== 1 || !id
)
603 return perf_evlist__first(evlist
);
605 sid
= perf_evlist__id2sid(evlist
, id
);
609 if (!perf_evlist__sample_id_all(evlist
))
610 return perf_evlist__first(evlist
);
615 struct perf_evsel
*perf_evlist__id2evsel_strict(struct perf_evlist
*evlist
,
618 struct perf_sample_id
*sid
;
623 sid
= perf_evlist__id2sid(evlist
, id
);
630 static int perf_evlist__event2id(struct perf_evlist
*evlist
,
631 union perf_event
*event
, u64
*id
)
633 const u64
*array
= event
->sample
.array
;
636 n
= (event
->header
.size
- sizeof(event
->header
)) >> 3;
638 if (event
->header
.type
== PERF_RECORD_SAMPLE
) {
639 if (evlist
->id_pos
>= n
)
641 *id
= array
[evlist
->id_pos
];
643 if (evlist
->is_pos
> n
)
651 static struct perf_evsel
*perf_evlist__event2evsel(struct perf_evlist
*evlist
,
652 union perf_event
*event
)
654 struct perf_evsel
*first
= perf_evlist__first(evlist
);
655 struct hlist_head
*head
;
656 struct perf_sample_id
*sid
;
660 if (evlist
->nr_entries
== 1)
663 if (!first
->attr
.sample_id_all
&&
664 event
->header
.type
!= PERF_RECORD_SAMPLE
)
667 if (perf_evlist__event2id(evlist
, event
, &id
))
670 /* Synthesized events have an id of zero */
674 hash
= hash_64(id
, PERF_EVLIST__HLIST_BITS
);
675 head
= &evlist
->heads
[hash
];
677 hlist_for_each_entry(sid
, head
, node
) {
684 static int perf_evlist__set_paused(struct perf_evlist
*evlist
, bool value
)
688 for (i
= 0; i
< evlist
->nr_mmaps
; i
++) {
689 int fd
= evlist
->mmap
[i
].fd
;
694 err
= ioctl(fd
, PERF_EVENT_IOC_PAUSE_OUTPUT
, value
? 1 : 0);
701 int perf_evlist__pause(struct perf_evlist
*evlist
)
703 return perf_evlist__set_paused(evlist
, true);
706 int perf_evlist__resume(struct perf_evlist
*evlist
)
708 return perf_evlist__set_paused(evlist
, false);
711 /* When check_messup is true, 'end' must points to a good entry */
712 static union perf_event
*
713 perf_mmap__read(struct perf_mmap
*md
, bool check_messup
, u64 start
,
716 unsigned char *data
= md
->base
+ page_size
;
717 union perf_event
*event
= NULL
;
718 int diff
= end
- start
;
722 * If we're further behind than half the buffer, there's a chance
723 * the writer will bite our tail and mess up the samples under us.
725 * If we somehow ended up ahead of the 'end', we got messed up.
727 * In either case, truncate and restart at 'end'.
729 if (diff
> md
->mask
/ 2 || diff
< 0) {
730 fprintf(stderr
, "WARNING: failed to keep up with mmap data.\n");
733 * 'end' points to a known good entry, start there.
740 if (diff
>= (int)sizeof(event
->header
)) {
743 event
= (union perf_event
*)&data
[start
& md
->mask
];
744 size
= event
->header
.size
;
746 if (size
< sizeof(event
->header
) || diff
< (int)size
) {
752 * Event straddles the mmap boundary -- header should always
753 * be inside due to u64 alignment of output.
755 if ((start
& md
->mask
) + size
!= ((start
+ size
) & md
->mask
)) {
756 unsigned int offset
= start
;
757 unsigned int len
= min(sizeof(*event
), size
), cpy
;
758 void *dst
= md
->event_copy
;
761 cpy
= min(md
->mask
+ 1 - (offset
& md
->mask
), len
);
762 memcpy(dst
, &data
[offset
& md
->mask
], cpy
);
768 event
= (union perf_event
*) md
->event_copy
;
781 union perf_event
*perf_evlist__mmap_read_forward(struct perf_evlist
*evlist
, int idx
)
783 struct perf_mmap
*md
= &evlist
->mmap
[idx
];
788 * Check if event was unmapped due to a POLLHUP/POLLERR.
790 if (!atomic_read(&md
->refcnt
))
793 head
= perf_mmap__read_head(md
);
795 return perf_mmap__read(md
, evlist
->overwrite
, old
, head
, &md
->prev
);
799 perf_evlist__mmap_read_backward(struct perf_evlist
*evlist
, int idx
)
801 struct perf_mmap
*md
= &evlist
->mmap
[idx
];
803 u64 start
= md
->prev
;
806 * Check if event was unmapped due to a POLLHUP/POLLERR.
808 if (!atomic_read(&md
->refcnt
))
811 head
= perf_mmap__read_head(md
);
816 * 'head' pointer starts from 0. Kernel minus sizeof(record) form
817 * it each time when kernel writes to it, so in fact 'head' is
818 * negative. 'end' pointer is made manually by adding the size of
819 * the ring buffer to 'head' pointer, means the validate data can
820 * read is the whole ring buffer. If 'end' is positive, the ring
821 * buffer has not fully filled, so we must adjust 'end' to 0.
823 * However, since both 'head' and 'end' is unsigned, we can't
824 * simply compare 'end' against 0. Here we compare '-head' and
825 * the size of the ring buffer, where -head is the number of bytes
826 * kernel write to the ring buffer.
828 if (-head
< (u64
)(md
->mask
+ 1))
831 end
= head
+ md
->mask
+ 1;
833 return perf_mmap__read(md
, false, start
, end
, &md
->prev
);
836 union perf_event
*perf_evlist__mmap_read(struct perf_evlist
*evlist
, int idx
)
838 if (!evlist
->backward
)
839 return perf_evlist__mmap_read_forward(evlist
, idx
);
840 return perf_evlist__mmap_read_backward(evlist
, idx
);
843 void perf_evlist__mmap_read_catchup(struct perf_evlist
*evlist
, int idx
)
845 struct perf_mmap
*md
= &evlist
->mmap
[idx
];
848 if (!atomic_read(&md
->refcnt
))
851 head
= perf_mmap__read_head(md
);
855 static bool perf_mmap__empty(struct perf_mmap
*md
)
857 return perf_mmap__read_head(md
) == md
->prev
&& !md
->auxtrace_mmap
.base
;
860 static void perf_evlist__mmap_get(struct perf_evlist
*evlist
, int idx
)
862 atomic_inc(&evlist
->mmap
[idx
].refcnt
);
865 static void perf_evlist__mmap_put(struct perf_evlist
*evlist
, int idx
)
867 struct perf_mmap
*md
= &evlist
->mmap
[idx
];
869 BUG_ON(md
->base
&& atomic_read(&md
->refcnt
) == 0);
871 if (atomic_dec_and_test(&md
->refcnt
))
872 __perf_evlist__munmap(evlist
, idx
);
875 void perf_evlist__mmap_consume(struct perf_evlist
*evlist
, int idx
)
877 struct perf_mmap
*md
= &evlist
->mmap
[idx
];
879 if (!evlist
->overwrite
) {
882 perf_mmap__write_tail(md
, old
);
885 if (atomic_read(&md
->refcnt
) == 1 && perf_mmap__empty(md
))
886 perf_evlist__mmap_put(evlist
, idx
);
889 int __weak
auxtrace_mmap__mmap(struct auxtrace_mmap
*mm __maybe_unused
,
890 struct auxtrace_mmap_params
*mp __maybe_unused
,
891 void *userpg __maybe_unused
,
892 int fd __maybe_unused
)
897 void __weak
auxtrace_mmap__munmap(struct auxtrace_mmap
*mm __maybe_unused
)
901 void __weak
auxtrace_mmap_params__init(
902 struct auxtrace_mmap_params
*mp __maybe_unused
,
903 off_t auxtrace_offset __maybe_unused
,
904 unsigned int auxtrace_pages __maybe_unused
,
905 bool auxtrace_overwrite __maybe_unused
)
909 void __weak
auxtrace_mmap_params__set_idx(
910 struct auxtrace_mmap_params
*mp __maybe_unused
,
911 struct perf_evlist
*evlist __maybe_unused
,
912 int idx __maybe_unused
,
913 bool per_cpu __maybe_unused
)
917 static void __perf_evlist__munmap(struct perf_evlist
*evlist
, int idx
)
919 if (evlist
->mmap
[idx
].base
!= NULL
) {
920 munmap(evlist
->mmap
[idx
].base
, evlist
->mmap_len
);
921 evlist
->mmap
[idx
].base
= NULL
;
922 evlist
->mmap
[idx
].fd
= -1;
923 atomic_set(&evlist
->mmap
[idx
].refcnt
, 0);
925 auxtrace_mmap__munmap(&evlist
->mmap
[idx
].auxtrace_mmap
);
928 void perf_evlist__munmap(struct perf_evlist
*evlist
)
932 if (evlist
->mmap
== NULL
)
935 for (i
= 0; i
< evlist
->nr_mmaps
; i
++)
936 __perf_evlist__munmap(evlist
, i
);
938 zfree(&evlist
->mmap
);
941 static int perf_evlist__alloc_mmap(struct perf_evlist
*evlist
)
945 evlist
->nr_mmaps
= cpu_map__nr(evlist
->cpus
);
946 if (cpu_map__empty(evlist
->cpus
))
947 evlist
->nr_mmaps
= thread_map__nr(evlist
->threads
);
948 evlist
->mmap
= zalloc(evlist
->nr_mmaps
* sizeof(struct perf_mmap
));
949 for (i
= 0; i
< evlist
->nr_mmaps
; i
++)
950 evlist
->mmap
[i
].fd
= -1;
951 return evlist
->mmap
!= NULL
? 0 : -ENOMEM
;
957 struct auxtrace_mmap_params auxtrace_mp
;
960 static int __perf_evlist__mmap(struct perf_evlist
*evlist
, int idx
,
961 struct mmap_params
*mp
, int fd
)
964 * The last one will be done at perf_evlist__mmap_consume(), so that we
965 * make sure we don't prevent tools from consuming every last event in
968 * I.e. we can get the POLLHUP meaning that the fd doesn't exist
969 * anymore, but the last events for it are still in the ring buffer,
970 * waiting to be consumed.
972 * Tools can chose to ignore this at their own discretion, but the
973 * evlist layer can't just drop it when filtering events in
974 * perf_evlist__filter_pollfd().
976 atomic_set(&evlist
->mmap
[idx
].refcnt
, 2);
977 evlist
->mmap
[idx
].prev
= 0;
978 evlist
->mmap
[idx
].mask
= mp
->mask
;
979 evlist
->mmap
[idx
].base
= mmap(NULL
, evlist
->mmap_len
, mp
->prot
,
981 if (evlist
->mmap
[idx
].base
== MAP_FAILED
) {
982 pr_debug2("failed to mmap perf event ring buffer, error %d\n",
984 evlist
->mmap
[idx
].base
= NULL
;
987 evlist
->mmap
[idx
].fd
= fd
;
989 if (auxtrace_mmap__mmap(&evlist
->mmap
[idx
].auxtrace_mmap
,
990 &mp
->auxtrace_mp
, evlist
->mmap
[idx
].base
, fd
))
997 perf_evlist__should_poll(struct perf_evlist
*evlist __maybe_unused
,
998 struct perf_evsel
*evsel
)
1000 if (evsel
->overwrite
)
1005 static int perf_evlist__mmap_per_evsel(struct perf_evlist
*evlist
, int idx
,
1006 struct mmap_params
*mp
, int cpu
,
1007 int thread
, int *output
)
1009 struct perf_evsel
*evsel
;
1012 evlist__for_each(evlist
, evsel
) {
1015 if (evsel
->overwrite
!= (evlist
->overwrite
&& evlist
->backward
))
1018 if (evsel
->system_wide
&& thread
)
1021 fd
= FD(evsel
, cpu
, thread
);
1023 if (*output
== -1) {
1025 if (__perf_evlist__mmap(evlist
, idx
, mp
, *output
) < 0)
1028 if (ioctl(fd
, PERF_EVENT_IOC_SET_OUTPUT
, *output
) != 0)
1031 perf_evlist__mmap_get(evlist
, idx
);
1034 revent
= perf_evlist__should_poll(evlist
, evsel
) ? POLLIN
: 0;
1037 * The system_wide flag causes a selected event to be opened
1038 * always without a pid. Consequently it will never get a
1039 * POLLHUP, but it is used for tracking in combination with
1040 * other events, so it should not need to be polled anyway.
1041 * Therefore don't add it for polling.
1043 if (!evsel
->system_wide
&&
1044 __perf_evlist__add_pollfd(evlist
, fd
, idx
, revent
) < 0) {
1045 perf_evlist__mmap_put(evlist
, idx
);
1049 if (evsel
->attr
.read_format
& PERF_FORMAT_ID
) {
1050 if (perf_evlist__id_add_fd(evlist
, evsel
, cpu
, thread
,
1053 perf_evlist__set_sid_idx(evlist
, evsel
, idx
, cpu
,
1061 static int perf_evlist__mmap_per_cpu(struct perf_evlist
*evlist
,
1062 struct mmap_params
*mp
)
1065 int nr_cpus
= cpu_map__nr(evlist
->cpus
);
1066 int nr_threads
= thread_map__nr(evlist
->threads
);
1068 pr_debug2("perf event ring buffer mmapped per cpu\n");
1069 for (cpu
= 0; cpu
< nr_cpus
; cpu
++) {
1072 auxtrace_mmap_params__set_idx(&mp
->auxtrace_mp
, evlist
, cpu
,
1075 for (thread
= 0; thread
< nr_threads
; thread
++) {
1076 if (perf_evlist__mmap_per_evsel(evlist
, cpu
, mp
, cpu
,
1085 for (cpu
= 0; cpu
< nr_cpus
; cpu
++)
1086 __perf_evlist__munmap(evlist
, cpu
);
1090 static int perf_evlist__mmap_per_thread(struct perf_evlist
*evlist
,
1091 struct mmap_params
*mp
)
1094 int nr_threads
= thread_map__nr(evlist
->threads
);
1096 pr_debug2("perf event ring buffer mmapped per thread\n");
1097 for (thread
= 0; thread
< nr_threads
; thread
++) {
1100 auxtrace_mmap_params__set_idx(&mp
->auxtrace_mp
, evlist
, thread
,
1103 if (perf_evlist__mmap_per_evsel(evlist
, thread
, mp
, 0, thread
,
1111 for (thread
= 0; thread
< nr_threads
; thread
++)
1112 __perf_evlist__munmap(evlist
, thread
);
1116 unsigned long perf_event_mlock_kb_in_pages(void)
1118 unsigned long pages
;
1121 if (sysctl__read_int("kernel/perf_event_mlock_kb", &max
) < 0) {
1123 * Pick a once upon a time good value, i.e. things look
1124 * strange since we can't read a sysctl value, but lets not
1129 max
-= (page_size
/ 1024);
1132 pages
= (max
* 1024) / page_size
;
1133 if (!is_power_of_2(pages
))
1134 pages
= rounddown_pow_of_two(pages
);
1139 static size_t perf_evlist__mmap_size(unsigned long pages
)
1141 if (pages
== UINT_MAX
)
1142 pages
= perf_event_mlock_kb_in_pages();
1143 else if (!is_power_of_2(pages
))
1146 return (pages
+ 1) * page_size
;
1149 static long parse_pages_arg(const char *str
, unsigned long min
,
1152 unsigned long pages
, val
;
1153 static struct parse_tag tags
[] = {
1154 { .tag
= 'B', .mult
= 1 },
1155 { .tag
= 'K', .mult
= 1 << 10 },
1156 { .tag
= 'M', .mult
= 1 << 20 },
1157 { .tag
= 'G', .mult
= 1 << 30 },
1164 val
= parse_tag_value(str
, tags
);
1165 if (val
!= (unsigned long) -1) {
1166 /* we got file size value */
1167 pages
= PERF_ALIGN(val
, page_size
) / page_size
;
1169 /* we got pages count value */
1171 pages
= strtoul(str
, &eptr
, 10);
1176 if (pages
== 0 && min
== 0) {
1177 /* leave number of pages at 0 */
1178 } else if (!is_power_of_2(pages
)) {
1179 /* round pages up to next power of 2 */
1180 pages
= roundup_pow_of_two(pages
);
1183 pr_info("rounding mmap pages size to %lu bytes (%lu pages)\n",
1184 pages
* page_size
, pages
);
1193 int __perf_evlist__parse_mmap_pages(unsigned int *mmap_pages
, const char *str
)
1195 unsigned long max
= UINT_MAX
;
1198 if (max
> SIZE_MAX
/ page_size
)
1199 max
= SIZE_MAX
/ page_size
;
1201 pages
= parse_pages_arg(str
, 1, max
);
1203 pr_err("Invalid argument for --mmap_pages/-m\n");
1207 *mmap_pages
= pages
;
1211 int perf_evlist__parse_mmap_pages(const struct option
*opt
, const char *str
,
1212 int unset __maybe_unused
)
1214 return __perf_evlist__parse_mmap_pages(opt
->value
, str
);
1218 * perf_evlist__mmap_ex - Create mmaps to receive events.
1219 * @evlist: list of events
1220 * @pages: map length in pages
1221 * @overwrite: overwrite older events?
1222 * @auxtrace_pages - auxtrace map length in pages
1223 * @auxtrace_overwrite - overwrite older auxtrace data?
1225 * If @overwrite is %false the user needs to signal event consumption using
1226 * perf_mmap__write_tail(). Using perf_evlist__mmap_read() does this
1229 * Similarly, if @auxtrace_overwrite is %false the user needs to signal data
1230 * consumption using auxtrace_mmap__write_tail().
1232 * Return: %0 on success, negative error code otherwise.
1234 int perf_evlist__mmap_ex(struct perf_evlist
*evlist
, unsigned int pages
,
1235 bool overwrite
, unsigned int auxtrace_pages
,
1236 bool auxtrace_overwrite
)
1238 struct perf_evsel
*evsel
;
1239 const struct cpu_map
*cpus
= evlist
->cpus
;
1240 const struct thread_map
*threads
= evlist
->threads
;
1241 struct mmap_params mp
= {
1242 .prot
= PROT_READ
| (overwrite
? 0 : PROT_WRITE
),
1245 if (evlist
->mmap
== NULL
&& perf_evlist__alloc_mmap(evlist
) < 0)
1248 if (evlist
->pollfd
.entries
== NULL
&& perf_evlist__alloc_pollfd(evlist
) < 0)
1251 evlist
->overwrite
= overwrite
;
1252 evlist
->mmap_len
= perf_evlist__mmap_size(pages
);
1253 pr_debug("mmap size %zuB\n", evlist
->mmap_len
);
1254 mp
.mask
= evlist
->mmap_len
- page_size
- 1;
1256 auxtrace_mmap_params__init(&mp
.auxtrace_mp
, evlist
->mmap_len
,
1257 auxtrace_pages
, auxtrace_overwrite
);
1259 evlist__for_each(evlist
, evsel
) {
1260 if ((evsel
->attr
.read_format
& PERF_FORMAT_ID
) &&
1261 evsel
->sample_id
== NULL
&&
1262 perf_evsel__alloc_id(evsel
, cpu_map__nr(cpus
), threads
->nr
) < 0)
1266 if (cpu_map__empty(cpus
))
1267 return perf_evlist__mmap_per_thread(evlist
, &mp
);
1269 return perf_evlist__mmap_per_cpu(evlist
, &mp
);
1272 int perf_evlist__mmap(struct perf_evlist
*evlist
, unsigned int pages
,
1275 return perf_evlist__mmap_ex(evlist
, pages
, overwrite
, 0, false);
1278 int perf_evlist__create_maps(struct perf_evlist
*evlist
, struct target
*target
)
1280 struct cpu_map
*cpus
;
1281 struct thread_map
*threads
;
1283 threads
= thread_map__new_str(target
->pid
, target
->tid
, target
->uid
);
1288 if (target__uses_dummy_map(target
))
1289 cpus
= cpu_map__dummy_new();
1291 cpus
= cpu_map__new(target
->cpu_list
);
1294 goto out_delete_threads
;
1296 evlist
->has_user_cpus
= !!target
->cpu_list
;
1298 perf_evlist__set_maps(evlist
, cpus
, threads
);
1303 thread_map__put(threads
);
1307 void perf_evlist__set_maps(struct perf_evlist
*evlist
, struct cpu_map
*cpus
,
1308 struct thread_map
*threads
)
1311 * Allow for the possibility that one or another of the maps isn't being
1312 * changed i.e. don't put it. Note we are assuming the maps that are
1313 * being applied are brand new and evlist is taking ownership of the
1314 * original reference count of 1. If that is not the case it is up to
1315 * the caller to increase the reference count.
1317 if (cpus
!= evlist
->cpus
) {
1318 cpu_map__put(evlist
->cpus
);
1319 evlist
->cpus
= cpu_map__get(cpus
);
1322 if (threads
!= evlist
->threads
) {
1323 thread_map__put(evlist
->threads
);
1324 evlist
->threads
= thread_map__get(threads
);
1327 perf_evlist__propagate_maps(evlist
);
1330 void __perf_evlist__set_sample_bit(struct perf_evlist
*evlist
,
1331 enum perf_event_sample_format bit
)
1333 struct perf_evsel
*evsel
;
1335 evlist__for_each(evlist
, evsel
)
1336 __perf_evsel__set_sample_bit(evsel
, bit
);
1339 void __perf_evlist__reset_sample_bit(struct perf_evlist
*evlist
,
1340 enum perf_event_sample_format bit
)
1342 struct perf_evsel
*evsel
;
1344 evlist__for_each(evlist
, evsel
)
1345 __perf_evsel__reset_sample_bit(evsel
, bit
);
1348 int perf_evlist__apply_filters(struct perf_evlist
*evlist
, struct perf_evsel
**err_evsel
)
1350 struct perf_evsel
*evsel
;
1352 const int ncpus
= cpu_map__nr(evlist
->cpus
),
1353 nthreads
= thread_map__nr(evlist
->threads
);
1355 evlist__for_each(evlist
, evsel
) {
1356 if (evsel
->filter
== NULL
)
1360 * filters only work for tracepoint event, which doesn't have cpu limit.
1361 * So evlist and evsel should always be same.
1363 err
= perf_evsel__apply_filter(evsel
, ncpus
, nthreads
, evsel
->filter
);
1373 int perf_evlist__set_filter(struct perf_evlist
*evlist
, const char *filter
)
1375 struct perf_evsel
*evsel
;
1378 evlist__for_each(evlist
, evsel
) {
1379 if (evsel
->attr
.type
!= PERF_TYPE_TRACEPOINT
)
1382 err
= perf_evsel__set_filter(evsel
, filter
);
1390 int perf_evlist__set_filter_pids(struct perf_evlist
*evlist
, size_t npids
, pid_t
*pids
)
1396 for (i
= 0; i
< npids
; ++i
) {
1398 if (asprintf(&filter
, "common_pid != %d", pids
[i
]) < 0)
1403 if (asprintf(&tmp
, "%s && common_pid != %d", filter
, pids
[i
]) < 0)
1411 ret
= perf_evlist__set_filter(evlist
, filter
);
1417 int perf_evlist__set_filter_pid(struct perf_evlist
*evlist
, pid_t pid
)
1419 return perf_evlist__set_filter_pids(evlist
, 1, &pid
);
1422 bool perf_evlist__valid_sample_type(struct perf_evlist
*evlist
)
1424 struct perf_evsel
*pos
;
1426 if (evlist
->nr_entries
== 1)
1429 if (evlist
->id_pos
< 0 || evlist
->is_pos
< 0)
1432 evlist__for_each(evlist
, pos
) {
1433 if (pos
->id_pos
!= evlist
->id_pos
||
1434 pos
->is_pos
!= evlist
->is_pos
)
1441 u64
__perf_evlist__combined_sample_type(struct perf_evlist
*evlist
)
1443 struct perf_evsel
*evsel
;
1445 if (evlist
->combined_sample_type
)
1446 return evlist
->combined_sample_type
;
1448 evlist__for_each(evlist
, evsel
)
1449 evlist
->combined_sample_type
|= evsel
->attr
.sample_type
;
1451 return evlist
->combined_sample_type
;
1454 u64
perf_evlist__combined_sample_type(struct perf_evlist
*evlist
)
1456 evlist
->combined_sample_type
= 0;
1457 return __perf_evlist__combined_sample_type(evlist
);
1460 u64
perf_evlist__combined_branch_type(struct perf_evlist
*evlist
)
1462 struct perf_evsel
*evsel
;
1463 u64 branch_type
= 0;
1465 evlist__for_each(evlist
, evsel
)
1466 branch_type
|= evsel
->attr
.branch_sample_type
;
1470 bool perf_evlist__valid_read_format(struct perf_evlist
*evlist
)
1472 struct perf_evsel
*first
= perf_evlist__first(evlist
), *pos
= first
;
1473 u64 read_format
= first
->attr
.read_format
;
1474 u64 sample_type
= first
->attr
.sample_type
;
1476 evlist__for_each(evlist
, pos
) {
1477 if (read_format
!= pos
->attr
.read_format
)
1481 /* PERF_SAMPLE_READ imples PERF_FORMAT_ID. */
1482 if ((sample_type
& PERF_SAMPLE_READ
) &&
1483 !(read_format
& PERF_FORMAT_ID
)) {
1490 u64
perf_evlist__read_format(struct perf_evlist
*evlist
)
1492 struct perf_evsel
*first
= perf_evlist__first(evlist
);
1493 return first
->attr
.read_format
;
1496 u16
perf_evlist__id_hdr_size(struct perf_evlist
*evlist
)
1498 struct perf_evsel
*first
= perf_evlist__first(evlist
);
1499 struct perf_sample
*data
;
1503 if (!first
->attr
.sample_id_all
)
1506 sample_type
= first
->attr
.sample_type
;
1508 if (sample_type
& PERF_SAMPLE_TID
)
1509 size
+= sizeof(data
->tid
) * 2;
1511 if (sample_type
& PERF_SAMPLE_TIME
)
1512 size
+= sizeof(data
->time
);
1514 if (sample_type
& PERF_SAMPLE_ID
)
1515 size
+= sizeof(data
->id
);
1517 if (sample_type
& PERF_SAMPLE_STREAM_ID
)
1518 size
+= sizeof(data
->stream_id
);
1520 if (sample_type
& PERF_SAMPLE_CPU
)
1521 size
+= sizeof(data
->cpu
) * 2;
1523 if (sample_type
& PERF_SAMPLE_IDENTIFIER
)
1524 size
+= sizeof(data
->id
);
1529 bool perf_evlist__valid_sample_id_all(struct perf_evlist
*evlist
)
1531 struct perf_evsel
*first
= perf_evlist__first(evlist
), *pos
= first
;
1533 evlist__for_each_continue(evlist
, pos
) {
1534 if (first
->attr
.sample_id_all
!= pos
->attr
.sample_id_all
)
1541 bool perf_evlist__sample_id_all(struct perf_evlist
*evlist
)
1543 struct perf_evsel
*first
= perf_evlist__first(evlist
);
1544 return first
->attr
.sample_id_all
;
1547 void perf_evlist__set_selected(struct perf_evlist
*evlist
,
1548 struct perf_evsel
*evsel
)
1550 evlist
->selected
= evsel
;
1553 void perf_evlist__close(struct perf_evlist
*evlist
)
1555 struct perf_evsel
*evsel
;
1556 int ncpus
= cpu_map__nr(evlist
->cpus
);
1557 int nthreads
= thread_map__nr(evlist
->threads
);
1560 evlist__for_each_reverse(evlist
, evsel
) {
1561 n
= evsel
->cpus
? evsel
->cpus
->nr
: ncpus
;
1562 perf_evsel__close(evsel
, n
, nthreads
);
1566 static int perf_evlist__create_syswide_maps(struct perf_evlist
*evlist
)
1568 struct cpu_map
*cpus
;
1569 struct thread_map
*threads
;
1573 * Try reading /sys/devices/system/cpu/online to get
1576 * FIXME: -ENOMEM is the best we can do here, the cpu_map
1577 * code needs an overhaul to properly forward the
1578 * error, and we may not want to do that fallback to a
1579 * default cpu identity map :-\
1581 cpus
= cpu_map__new(NULL
);
1585 threads
= thread_map__new_dummy();
1589 perf_evlist__set_maps(evlist
, cpus
, threads
);
1597 int perf_evlist__open(struct perf_evlist
*evlist
)
1599 struct perf_evsel
*evsel
;
1603 * Default: one fd per CPU, all threads, aka systemwide
1604 * as sys_perf_event_open(cpu = -1, thread = -1) is EINVAL
1606 if (evlist
->threads
== NULL
&& evlist
->cpus
== NULL
) {
1607 err
= perf_evlist__create_syswide_maps(evlist
);
1612 perf_evlist__update_id_pos(evlist
);
1614 evlist__for_each(evlist
, evsel
) {
1615 err
= perf_evsel__open(evsel
, evsel
->cpus
, evsel
->threads
);
1622 perf_evlist__close(evlist
);
1627 int perf_evlist__prepare_workload(struct perf_evlist
*evlist
, struct target
*target
,
1628 const char *argv
[], bool pipe_output
,
1629 void (*exec_error
)(int signo
, siginfo_t
*info
, void *ucontext
))
1631 int child_ready_pipe
[2], go_pipe
[2];
1634 if (pipe(child_ready_pipe
) < 0) {
1635 perror("failed to create 'ready' pipe");
1639 if (pipe(go_pipe
) < 0) {
1640 perror("failed to create 'go' pipe");
1641 goto out_close_ready_pipe
;
1644 evlist
->workload
.pid
= fork();
1645 if (evlist
->workload
.pid
< 0) {
1646 perror("failed to fork");
1647 goto out_close_pipes
;
1650 if (!evlist
->workload
.pid
) {
1656 signal(SIGTERM
, SIG_DFL
);
1658 close(child_ready_pipe
[0]);
1660 fcntl(go_pipe
[0], F_SETFD
, FD_CLOEXEC
);
1663 * Tell the parent we're ready to go
1665 close(child_ready_pipe
[1]);
1668 * Wait until the parent tells us to go.
1670 ret
= read(go_pipe
[0], &bf
, 1);
1672 * The parent will ask for the execvp() to be performed by
1673 * writing exactly one byte, in workload.cork_fd, usually via
1674 * perf_evlist__start_workload().
1676 * For cancelling the workload without actually running it,
1677 * the parent will just close workload.cork_fd, without writing
1678 * anything, i.e. read will return zero and we just exit()
1683 perror("unable to read pipe");
1687 execvp(argv
[0], (char **)argv
);
1692 val
.sival_int
= errno
;
1693 if (sigqueue(getppid(), SIGUSR1
, val
))
1701 struct sigaction act
= {
1702 .sa_flags
= SA_SIGINFO
,
1703 .sa_sigaction
= exec_error
,
1705 sigaction(SIGUSR1
, &act
, NULL
);
1708 if (target__none(target
)) {
1709 if (evlist
->threads
== NULL
) {
1710 fprintf(stderr
, "FATAL: evlist->threads need to be set at this point (%s:%d).\n",
1711 __func__
, __LINE__
);
1712 goto out_close_pipes
;
1714 thread_map__set_pid(evlist
->threads
, 0, evlist
->workload
.pid
);
1717 close(child_ready_pipe
[1]);
1720 * wait for child to settle
1722 if (read(child_ready_pipe
[0], &bf
, 1) == -1) {
1723 perror("unable to read pipe");
1724 goto out_close_pipes
;
1727 fcntl(go_pipe
[1], F_SETFD
, FD_CLOEXEC
);
1728 evlist
->workload
.cork_fd
= go_pipe
[1];
1729 close(child_ready_pipe
[0]);
1735 out_close_ready_pipe
:
1736 close(child_ready_pipe
[0]);
1737 close(child_ready_pipe
[1]);
1741 int perf_evlist__start_workload(struct perf_evlist
*evlist
)
1743 if (evlist
->workload
.cork_fd
> 0) {
1747 * Remove the cork, let it rip!
1749 ret
= write(evlist
->workload
.cork_fd
, &bf
, 1);
1751 perror("enable to write to pipe");
1753 close(evlist
->workload
.cork_fd
);
1760 int perf_evlist__parse_sample(struct perf_evlist
*evlist
, union perf_event
*event
,
1761 struct perf_sample
*sample
)
1763 struct perf_evsel
*evsel
= perf_evlist__event2evsel(evlist
, event
);
1767 return perf_evsel__parse_sample(evsel
, event
, sample
);
1770 size_t perf_evlist__fprintf(struct perf_evlist
*evlist
, FILE *fp
)
1772 struct perf_evsel
*evsel
;
1775 evlist__for_each(evlist
, evsel
) {
1776 printed
+= fprintf(fp
, "%s%s", evsel
->idx
? ", " : "",
1777 perf_evsel__name(evsel
));
1780 return printed
+ fprintf(fp
, "\n");
1783 int perf_evlist__strerror_open(struct perf_evlist
*evlist
,
1784 int err
, char *buf
, size_t size
)
1787 char sbuf
[STRERR_BUFSIZE
], *emsg
= strerror_r(err
, sbuf
, sizeof(sbuf
));
1792 printed
= scnprintf(buf
, size
,
1794 "Hint:\tCheck /proc/sys/kernel/perf_event_paranoid setting.", emsg
);
1796 value
= perf_event_paranoid();
1798 printed
+= scnprintf(buf
+ printed
, size
- printed
, "\nHint:\t");
1801 printed
+= scnprintf(buf
+ printed
, size
- printed
,
1802 "For your workloads it needs to be <= 1\nHint:\t");
1804 printed
+= scnprintf(buf
+ printed
, size
- printed
,
1805 "For system wide tracing it needs to be set to -1.\n");
1807 printed
+= scnprintf(buf
+ printed
, size
- printed
,
1808 "Hint:\tTry: 'sudo sh -c \"echo -1 > /proc/sys/kernel/perf_event_paranoid\"'\n"
1809 "Hint:\tThe current value is %d.", value
);
1812 struct perf_evsel
*first
= perf_evlist__first(evlist
);
1815 if (sysctl__read_int("kernel/perf_event_max_sample_rate", &max_freq
) < 0)
1818 if (first
->attr
.sample_freq
< (u64
)max_freq
)
1821 printed
= scnprintf(buf
, size
,
1823 "Hint:\tCheck /proc/sys/kernel/perf_event_max_sample_rate.\n"
1824 "Hint:\tThe current value is %d and %" PRIu64
" is being requested.",
1825 emsg
, max_freq
, first
->attr
.sample_freq
);
1830 scnprintf(buf
, size
, "%s", emsg
);
1837 int perf_evlist__strerror_mmap(struct perf_evlist
*evlist
, int err
, char *buf
, size_t size
)
1839 char sbuf
[STRERR_BUFSIZE
], *emsg
= strerror_r(err
, sbuf
, sizeof(sbuf
));
1840 int pages_attempted
= evlist
->mmap_len
/ 1024, pages_max_per_user
, printed
= 0;
1844 sysctl__read_int("kernel/perf_event_mlock_kb", &pages_max_per_user
);
1845 printed
+= scnprintf(buf
+ printed
, size
- printed
,
1847 "Hint:\tCheck /proc/sys/kernel/perf_event_mlock_kb (%d kB) setting.\n"
1848 "Hint:\tTried using %zd kB.\n",
1849 emsg
, pages_max_per_user
, pages_attempted
);
1851 if (pages_attempted
>= pages_max_per_user
) {
1852 printed
+= scnprintf(buf
+ printed
, size
- printed
,
1853 "Hint:\tTry 'sudo sh -c \"echo %d > /proc/sys/kernel/perf_event_mlock_kb\"', or\n",
1854 pages_max_per_user
+ pages_attempted
);
1857 printed
+= scnprintf(buf
+ printed
, size
- printed
,
1858 "Hint:\tTry using a smaller -m/--mmap-pages value.");
1861 scnprintf(buf
, size
, "%s", emsg
);
1868 void perf_evlist__to_front(struct perf_evlist
*evlist
,
1869 struct perf_evsel
*move_evsel
)
1871 struct perf_evsel
*evsel
, *n
;
1874 if (move_evsel
== perf_evlist__first(evlist
))
1877 evlist__for_each_safe(evlist
, n
, evsel
) {
1878 if (evsel
->leader
== move_evsel
->leader
)
1879 list_move_tail(&evsel
->node
, &move
);
1882 list_splice(&move
, &evlist
->entries
);
1885 void perf_evlist__set_tracking_event(struct perf_evlist
*evlist
,
1886 struct perf_evsel
*tracking_evsel
)
1888 struct perf_evsel
*evsel
;
1890 if (tracking_evsel
->tracking
)
1893 evlist__for_each(evlist
, evsel
) {
1894 if (evsel
!= tracking_evsel
)
1895 evsel
->tracking
= false;
1898 tracking_evsel
->tracking
= true;
1902 perf_evlist__find_evsel_by_str(struct perf_evlist
*evlist
,
1905 struct perf_evsel
*evsel
;
1907 evlist__for_each(evlist
, evsel
) {
1910 if (strcmp(str
, evsel
->name
) == 0)