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 "parse-options.h"
25 #include <linux/bitops.h>
26 #include <linux/hash.h>
27 #include <linux/log2.h>
29 static void perf_evlist__mmap_put(struct perf_evlist
*evlist
, int idx
);
30 static void __perf_evlist__munmap(struct perf_evlist
*evlist
, int idx
);
32 #define FD(e, x, y) (*(int *)xyarray__entry(e->fd, x, y))
33 #define SID(e, x, y) xyarray__entry(e->sample_id, x, y)
35 void perf_evlist__init(struct perf_evlist
*evlist
, struct cpu_map
*cpus
,
36 struct thread_map
*threads
)
40 for (i
= 0; i
< PERF_EVLIST__HLIST_SIZE
; ++i
)
41 INIT_HLIST_HEAD(&evlist
->heads
[i
]);
42 INIT_LIST_HEAD(&evlist
->entries
);
43 perf_evlist__set_maps(evlist
, cpus
, threads
);
44 fdarray__init(&evlist
->pollfd
, 64);
45 evlist
->workload
.pid
= -1;
48 struct perf_evlist
*perf_evlist__new(void)
50 struct perf_evlist
*evlist
= zalloc(sizeof(*evlist
));
53 perf_evlist__init(evlist
, NULL
, NULL
);
58 struct perf_evlist
*perf_evlist__new_default(void)
60 struct perf_evlist
*evlist
= perf_evlist__new();
62 if (evlist
&& perf_evlist__add_default(evlist
)) {
63 perf_evlist__delete(evlist
);
71 * perf_evlist__set_id_pos - set the positions of event ids.
72 * @evlist: selected event list
74 * Events with compatible sample types all have the same id_pos
75 * and is_pos. For convenience, put a copy on evlist.
77 void perf_evlist__set_id_pos(struct perf_evlist
*evlist
)
79 struct perf_evsel
*first
= perf_evlist__first(evlist
);
81 evlist
->id_pos
= first
->id_pos
;
82 evlist
->is_pos
= first
->is_pos
;
85 static void perf_evlist__update_id_pos(struct perf_evlist
*evlist
)
87 struct perf_evsel
*evsel
;
89 evlist__for_each(evlist
, evsel
)
90 perf_evsel__calc_id_pos(evsel
);
92 perf_evlist__set_id_pos(evlist
);
95 static void perf_evlist__purge(struct perf_evlist
*evlist
)
97 struct perf_evsel
*pos
, *n
;
99 evlist__for_each_safe(evlist
, n
, pos
) {
100 list_del_init(&pos
->node
);
101 perf_evsel__delete(pos
);
104 evlist
->nr_entries
= 0;
107 void perf_evlist__exit(struct perf_evlist
*evlist
)
109 zfree(&evlist
->mmap
);
110 fdarray__exit(&evlist
->pollfd
);
113 void perf_evlist__delete(struct perf_evlist
*evlist
)
115 perf_evlist__munmap(evlist
);
116 perf_evlist__close(evlist
);
117 cpu_map__delete(evlist
->cpus
);
118 thread_map__delete(evlist
->threads
);
120 evlist
->threads
= NULL
;
121 perf_evlist__purge(evlist
);
122 perf_evlist__exit(evlist
);
126 void perf_evlist__add(struct perf_evlist
*evlist
, struct perf_evsel
*entry
)
128 list_add_tail(&entry
->node
, &evlist
->entries
);
129 entry
->idx
= evlist
->nr_entries
;
130 entry
->tracking
= !entry
->idx
;
132 if (!evlist
->nr_entries
++)
133 perf_evlist__set_id_pos(evlist
);
136 void perf_evlist__splice_list_tail(struct perf_evlist
*evlist
,
137 struct list_head
*list
,
140 bool set_id_pos
= !evlist
->nr_entries
;
142 list_splice_tail(list
, &evlist
->entries
);
143 evlist
->nr_entries
+= nr_entries
;
145 perf_evlist__set_id_pos(evlist
);
148 void __perf_evlist__set_leader(struct list_head
*list
)
150 struct perf_evsel
*evsel
, *leader
;
152 leader
= list_entry(list
->next
, struct perf_evsel
, node
);
153 evsel
= list_entry(list
->prev
, struct perf_evsel
, node
);
155 leader
->nr_members
= evsel
->idx
- leader
->idx
+ 1;
157 __evlist__for_each(list
, evsel
) {
158 evsel
->leader
= leader
;
162 void perf_evlist__set_leader(struct perf_evlist
*evlist
)
164 if (evlist
->nr_entries
) {
165 evlist
->nr_groups
= evlist
->nr_entries
> 1 ? 1 : 0;
166 __perf_evlist__set_leader(&evlist
->entries
);
170 int perf_evlist__add_default(struct perf_evlist
*evlist
)
172 struct perf_event_attr attr
= {
173 .type
= PERF_TYPE_HARDWARE
,
174 .config
= PERF_COUNT_HW_CPU_CYCLES
,
176 struct perf_evsel
*evsel
;
178 event_attr_init(&attr
);
180 evsel
= perf_evsel__new(&attr
);
184 /* use strdup() because free(evsel) assumes name is allocated */
185 evsel
->name
= strdup("cycles");
189 perf_evlist__add(evlist
, evsel
);
192 perf_evsel__delete(evsel
);
197 static int perf_evlist__add_attrs(struct perf_evlist
*evlist
,
198 struct perf_event_attr
*attrs
, size_t nr_attrs
)
200 struct perf_evsel
*evsel
, *n
;
204 for (i
= 0; i
< nr_attrs
; i
++) {
205 evsel
= perf_evsel__new_idx(attrs
+ i
, evlist
->nr_entries
+ i
);
207 goto out_delete_partial_list
;
208 list_add_tail(&evsel
->node
, &head
);
211 perf_evlist__splice_list_tail(evlist
, &head
, nr_attrs
);
215 out_delete_partial_list
:
216 __evlist__for_each_safe(&head
, n
, evsel
)
217 perf_evsel__delete(evsel
);
221 int __perf_evlist__add_default_attrs(struct perf_evlist
*evlist
,
222 struct perf_event_attr
*attrs
, size_t nr_attrs
)
226 for (i
= 0; i
< nr_attrs
; i
++)
227 event_attr_init(attrs
+ i
);
229 return perf_evlist__add_attrs(evlist
, attrs
, nr_attrs
);
233 perf_evlist__find_tracepoint_by_id(struct perf_evlist
*evlist
, int id
)
235 struct perf_evsel
*evsel
;
237 evlist__for_each(evlist
, evsel
) {
238 if (evsel
->attr
.type
== PERF_TYPE_TRACEPOINT
&&
239 (int)evsel
->attr
.config
== id
)
247 perf_evlist__find_tracepoint_by_name(struct perf_evlist
*evlist
,
250 struct perf_evsel
*evsel
;
252 evlist__for_each(evlist
, evsel
) {
253 if ((evsel
->attr
.type
== PERF_TYPE_TRACEPOINT
) &&
254 (strcmp(evsel
->name
, name
) == 0))
261 int perf_evlist__add_newtp(struct perf_evlist
*evlist
,
262 const char *sys
, const char *name
, void *handler
)
264 struct perf_evsel
*evsel
= perf_evsel__newtp(sys
, name
);
269 evsel
->handler
= handler
;
270 perf_evlist__add(evlist
, evsel
);
274 static int perf_evlist__nr_threads(struct perf_evlist
*evlist
,
275 struct perf_evsel
*evsel
)
277 if (evsel
->system_wide
)
280 return thread_map__nr(evlist
->threads
);
283 void perf_evlist__disable(struct perf_evlist
*evlist
)
286 struct perf_evsel
*pos
;
287 int nr_cpus
= cpu_map__nr(evlist
->cpus
);
290 for (cpu
= 0; cpu
< nr_cpus
; cpu
++) {
291 evlist__for_each(evlist
, pos
) {
292 if (!perf_evsel__is_group_leader(pos
) || !pos
->fd
)
294 nr_threads
= perf_evlist__nr_threads(evlist
, pos
);
295 for (thread
= 0; thread
< nr_threads
; thread
++)
296 ioctl(FD(pos
, cpu
, thread
),
297 PERF_EVENT_IOC_DISABLE
, 0);
302 void perf_evlist__enable(struct perf_evlist
*evlist
)
305 struct perf_evsel
*pos
;
306 int nr_cpus
= cpu_map__nr(evlist
->cpus
);
309 for (cpu
= 0; cpu
< nr_cpus
; cpu
++) {
310 evlist__for_each(evlist
, pos
) {
311 if (!perf_evsel__is_group_leader(pos
) || !pos
->fd
)
313 nr_threads
= perf_evlist__nr_threads(evlist
, pos
);
314 for (thread
= 0; thread
< nr_threads
; thread
++)
315 ioctl(FD(pos
, cpu
, thread
),
316 PERF_EVENT_IOC_ENABLE
, 0);
321 int perf_evlist__disable_event(struct perf_evlist
*evlist
,
322 struct perf_evsel
*evsel
)
324 int cpu
, thread
, err
;
325 int nr_cpus
= cpu_map__nr(evlist
->cpus
);
326 int nr_threads
= perf_evlist__nr_threads(evlist
, evsel
);
331 for (cpu
= 0; cpu
< nr_cpus
; cpu
++) {
332 for (thread
= 0; thread
< nr_threads
; thread
++) {
333 err
= ioctl(FD(evsel
, cpu
, thread
),
334 PERF_EVENT_IOC_DISABLE
, 0);
342 int perf_evlist__enable_event(struct perf_evlist
*evlist
,
343 struct perf_evsel
*evsel
)
345 int cpu
, thread
, err
;
346 int nr_cpus
= cpu_map__nr(evlist
->cpus
);
347 int nr_threads
= perf_evlist__nr_threads(evlist
, evsel
);
352 for (cpu
= 0; cpu
< nr_cpus
; cpu
++) {
353 for (thread
= 0; thread
< nr_threads
; thread
++) {
354 err
= ioctl(FD(evsel
, cpu
, thread
),
355 PERF_EVENT_IOC_ENABLE
, 0);
363 static int perf_evlist__enable_event_cpu(struct perf_evlist
*evlist
,
364 struct perf_evsel
*evsel
, int cpu
)
367 int nr_threads
= perf_evlist__nr_threads(evlist
, evsel
);
372 for (thread
= 0; thread
< nr_threads
; thread
++) {
373 err
= ioctl(FD(evsel
, cpu
, thread
),
374 PERF_EVENT_IOC_ENABLE
, 0);
381 static int perf_evlist__enable_event_thread(struct perf_evlist
*evlist
,
382 struct perf_evsel
*evsel
,
386 int nr_cpus
= cpu_map__nr(evlist
->cpus
);
391 for (cpu
= 0; cpu
< nr_cpus
; cpu
++) {
392 err
= ioctl(FD(evsel
, cpu
, thread
), PERF_EVENT_IOC_ENABLE
, 0);
399 int perf_evlist__enable_event_idx(struct perf_evlist
*evlist
,
400 struct perf_evsel
*evsel
, int idx
)
402 bool per_cpu_mmaps
= !cpu_map__empty(evlist
->cpus
);
405 return perf_evlist__enable_event_cpu(evlist
, evsel
, idx
);
407 return perf_evlist__enable_event_thread(evlist
, evsel
, idx
);
410 int perf_evlist__alloc_pollfd(struct perf_evlist
*evlist
)
412 int nr_cpus
= cpu_map__nr(evlist
->cpus
);
413 int nr_threads
= thread_map__nr(evlist
->threads
);
415 struct perf_evsel
*evsel
;
417 evlist__for_each(evlist
, evsel
) {
418 if (evsel
->system_wide
)
421 nfds
+= nr_cpus
* nr_threads
;
424 if (fdarray__available_entries(&evlist
->pollfd
) < nfds
&&
425 fdarray__grow(&evlist
->pollfd
, nfds
) < 0)
431 static int __perf_evlist__add_pollfd(struct perf_evlist
*evlist
, int fd
, int idx
)
433 int pos
= fdarray__add(&evlist
->pollfd
, fd
, POLLIN
| POLLERR
| POLLHUP
);
435 * Save the idx so that when we filter out fds POLLHUP'ed we can
436 * close the associated evlist->mmap[] entry.
439 evlist
->pollfd
.priv
[pos
].idx
= idx
;
441 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
447 int perf_evlist__add_pollfd(struct perf_evlist
*evlist
, int fd
)
449 return __perf_evlist__add_pollfd(evlist
, fd
, -1);
452 static void perf_evlist__munmap_filtered(struct fdarray
*fda
, int fd
)
454 struct perf_evlist
*evlist
= container_of(fda
, struct perf_evlist
, pollfd
);
456 perf_evlist__mmap_put(evlist
, fda
->priv
[fd
].idx
);
459 int perf_evlist__filter_pollfd(struct perf_evlist
*evlist
, short revents_and_mask
)
461 return fdarray__filter(&evlist
->pollfd
, revents_and_mask
,
462 perf_evlist__munmap_filtered
);
465 int perf_evlist__poll(struct perf_evlist
*evlist
, int timeout
)
467 return fdarray__poll(&evlist
->pollfd
, timeout
);
470 static void perf_evlist__id_hash(struct perf_evlist
*evlist
,
471 struct perf_evsel
*evsel
,
472 int cpu
, int thread
, u64 id
)
475 struct perf_sample_id
*sid
= SID(evsel
, cpu
, thread
);
479 hash
= hash_64(sid
->id
, PERF_EVLIST__HLIST_BITS
);
480 hlist_add_head(&sid
->node
, &evlist
->heads
[hash
]);
483 void perf_evlist__id_add(struct perf_evlist
*evlist
, struct perf_evsel
*evsel
,
484 int cpu
, int thread
, u64 id
)
486 perf_evlist__id_hash(evlist
, evsel
, cpu
, thread
, id
);
487 evsel
->id
[evsel
->ids
++] = id
;
490 static int perf_evlist__id_add_fd(struct perf_evlist
*evlist
,
491 struct perf_evsel
*evsel
,
492 int cpu
, int thread
, int fd
)
494 u64 read_data
[4] = { 0, };
495 int id_idx
= 1; /* The first entry is the counter value */
499 ret
= ioctl(fd
, PERF_EVENT_IOC_ID
, &id
);
506 /* Legacy way to get event id.. All hail to old kernels! */
509 * This way does not work with group format read, so bail
512 if (perf_evlist__read_format(evlist
) & PERF_FORMAT_GROUP
)
515 if (!(evsel
->attr
.read_format
& PERF_FORMAT_ID
) ||
516 read(fd
, &read_data
, sizeof(read_data
)) == -1)
519 if (evsel
->attr
.read_format
& PERF_FORMAT_TOTAL_TIME_ENABLED
)
521 if (evsel
->attr
.read_format
& PERF_FORMAT_TOTAL_TIME_RUNNING
)
524 id
= read_data
[id_idx
];
527 perf_evlist__id_add(evlist
, evsel
, cpu
, thread
, id
);
531 static void perf_evlist__set_sid_idx(struct perf_evlist
*evlist
,
532 struct perf_evsel
*evsel
, int idx
, int cpu
,
535 struct perf_sample_id
*sid
= SID(evsel
, cpu
, thread
);
537 if (evlist
->cpus
&& cpu
>= 0)
538 sid
->cpu
= evlist
->cpus
->map
[cpu
];
541 if (!evsel
->system_wide
&& evlist
->threads
&& thread
>= 0)
542 sid
->tid
= evlist
->threads
->map
[thread
];
547 struct perf_sample_id
*perf_evlist__id2sid(struct perf_evlist
*evlist
, u64 id
)
549 struct hlist_head
*head
;
550 struct perf_sample_id
*sid
;
553 hash
= hash_64(id
, PERF_EVLIST__HLIST_BITS
);
554 head
= &evlist
->heads
[hash
];
556 hlist_for_each_entry(sid
, head
, node
)
563 struct perf_evsel
*perf_evlist__id2evsel(struct perf_evlist
*evlist
, u64 id
)
565 struct perf_sample_id
*sid
;
567 if (evlist
->nr_entries
== 1)
568 return perf_evlist__first(evlist
);
570 sid
= perf_evlist__id2sid(evlist
, id
);
574 if (!perf_evlist__sample_id_all(evlist
))
575 return perf_evlist__first(evlist
);
580 static int perf_evlist__event2id(struct perf_evlist
*evlist
,
581 union perf_event
*event
, u64
*id
)
583 const u64
*array
= event
->sample
.array
;
586 n
= (event
->header
.size
- sizeof(event
->header
)) >> 3;
588 if (event
->header
.type
== PERF_RECORD_SAMPLE
) {
589 if (evlist
->id_pos
>= n
)
591 *id
= array
[evlist
->id_pos
];
593 if (evlist
->is_pos
> n
)
601 static struct perf_evsel
*perf_evlist__event2evsel(struct perf_evlist
*evlist
,
602 union perf_event
*event
)
604 struct perf_evsel
*first
= perf_evlist__first(evlist
);
605 struct hlist_head
*head
;
606 struct perf_sample_id
*sid
;
610 if (evlist
->nr_entries
== 1)
613 if (!first
->attr
.sample_id_all
&&
614 event
->header
.type
!= PERF_RECORD_SAMPLE
)
617 if (perf_evlist__event2id(evlist
, event
, &id
))
620 /* Synthesized events have an id of zero */
624 hash
= hash_64(id
, PERF_EVLIST__HLIST_BITS
);
625 head
= &evlist
->heads
[hash
];
627 hlist_for_each_entry(sid
, head
, node
) {
634 union perf_event
*perf_evlist__mmap_read(struct perf_evlist
*evlist
, int idx
)
636 struct perf_mmap
*md
= &evlist
->mmap
[idx
];
637 u64 head
= perf_mmap__read_head(md
);
639 unsigned char *data
= md
->base
+ page_size
;
640 union perf_event
*event
= NULL
;
642 if (evlist
->overwrite
) {
644 * If we're further behind than half the buffer, there's a chance
645 * the writer will bite our tail and mess up the samples under us.
647 * If we somehow ended up ahead of the head, we got messed up.
649 * In either case, truncate and restart at head.
651 int diff
= head
- old
;
652 if (diff
> md
->mask
/ 2 || diff
< 0) {
653 fprintf(stderr
, "WARNING: failed to keep up with mmap data.\n");
656 * head points to a known good entry, start there.
665 event
= (union perf_event
*)&data
[old
& md
->mask
];
666 size
= event
->header
.size
;
669 * Event straddles the mmap boundary -- header should always
670 * be inside due to u64 alignment of output.
672 if ((old
& md
->mask
) + size
!= ((old
+ size
) & md
->mask
)) {
673 unsigned int offset
= old
;
674 unsigned int len
= min(sizeof(*event
), size
), cpy
;
675 void *dst
= md
->event_copy
;
678 cpy
= min(md
->mask
+ 1 - (offset
& md
->mask
), len
);
679 memcpy(dst
, &data
[offset
& md
->mask
], cpy
);
685 event
= (union perf_event
*) md
->event_copy
;
696 static bool perf_mmap__empty(struct perf_mmap
*md
)
698 return perf_mmap__read_head(md
) == md
->prev
&& !md
->auxtrace_mmap
.base
;
701 static void perf_evlist__mmap_get(struct perf_evlist
*evlist
, int idx
)
703 ++evlist
->mmap
[idx
].refcnt
;
706 static void perf_evlist__mmap_put(struct perf_evlist
*evlist
, int idx
)
708 BUG_ON(evlist
->mmap
[idx
].refcnt
== 0);
710 if (--evlist
->mmap
[idx
].refcnt
== 0)
711 __perf_evlist__munmap(evlist
, idx
);
714 void perf_evlist__mmap_consume(struct perf_evlist
*evlist
, int idx
)
716 struct perf_mmap
*md
= &evlist
->mmap
[idx
];
718 if (!evlist
->overwrite
) {
721 perf_mmap__write_tail(md
, old
);
724 if (md
->refcnt
== 1 && perf_mmap__empty(md
))
725 perf_evlist__mmap_put(evlist
, idx
);
728 int __weak
auxtrace_mmap__mmap(struct auxtrace_mmap
*mm __maybe_unused
,
729 struct auxtrace_mmap_params
*mp __maybe_unused
,
730 void *userpg __maybe_unused
,
731 int fd __maybe_unused
)
736 void __weak
auxtrace_mmap__munmap(struct auxtrace_mmap
*mm __maybe_unused
)
740 void __weak
auxtrace_mmap_params__init(
741 struct auxtrace_mmap_params
*mp __maybe_unused
,
742 off_t auxtrace_offset __maybe_unused
,
743 unsigned int auxtrace_pages __maybe_unused
,
744 bool auxtrace_overwrite __maybe_unused
)
748 void __weak
auxtrace_mmap_params__set_idx(
749 struct auxtrace_mmap_params
*mp __maybe_unused
,
750 struct perf_evlist
*evlist __maybe_unused
,
751 int idx __maybe_unused
,
752 bool per_cpu __maybe_unused
)
756 static void __perf_evlist__munmap(struct perf_evlist
*evlist
, int idx
)
758 if (evlist
->mmap
[idx
].base
!= NULL
) {
759 munmap(evlist
->mmap
[idx
].base
, evlist
->mmap_len
);
760 evlist
->mmap
[idx
].base
= NULL
;
761 evlist
->mmap
[idx
].refcnt
= 0;
763 auxtrace_mmap__munmap(&evlist
->mmap
[idx
].auxtrace_mmap
);
766 void perf_evlist__munmap(struct perf_evlist
*evlist
)
770 if (evlist
->mmap
== NULL
)
773 for (i
= 0; i
< evlist
->nr_mmaps
; i
++)
774 __perf_evlist__munmap(evlist
, i
);
776 zfree(&evlist
->mmap
);
779 static int perf_evlist__alloc_mmap(struct perf_evlist
*evlist
)
781 evlist
->nr_mmaps
= cpu_map__nr(evlist
->cpus
);
782 if (cpu_map__empty(evlist
->cpus
))
783 evlist
->nr_mmaps
= thread_map__nr(evlist
->threads
);
784 evlist
->mmap
= zalloc(evlist
->nr_mmaps
* sizeof(struct perf_mmap
));
785 return evlist
->mmap
!= NULL
? 0 : -ENOMEM
;
791 struct auxtrace_mmap_params auxtrace_mp
;
794 static int __perf_evlist__mmap(struct perf_evlist
*evlist
, int idx
,
795 struct mmap_params
*mp
, int fd
)
798 * The last one will be done at perf_evlist__mmap_consume(), so that we
799 * make sure we don't prevent tools from consuming every last event in
802 * I.e. we can get the POLLHUP meaning that the fd doesn't exist
803 * anymore, but the last events for it are still in the ring buffer,
804 * waiting to be consumed.
806 * Tools can chose to ignore this at their own discretion, but the
807 * evlist layer can't just drop it when filtering events in
808 * perf_evlist__filter_pollfd().
810 evlist
->mmap
[idx
].refcnt
= 2;
811 evlist
->mmap
[idx
].prev
= 0;
812 evlist
->mmap
[idx
].mask
= mp
->mask
;
813 evlist
->mmap
[idx
].base
= mmap(NULL
, evlist
->mmap_len
, mp
->prot
,
815 if (evlist
->mmap
[idx
].base
== MAP_FAILED
) {
816 pr_debug2("failed to mmap perf event ring buffer, error %d\n",
818 evlist
->mmap
[idx
].base
= NULL
;
822 if (auxtrace_mmap__mmap(&evlist
->mmap
[idx
].auxtrace_mmap
,
823 &mp
->auxtrace_mp
, evlist
->mmap
[idx
].base
, fd
))
829 static int perf_evlist__mmap_per_evsel(struct perf_evlist
*evlist
, int idx
,
830 struct mmap_params
*mp
, int cpu
,
831 int thread
, int *output
)
833 struct perf_evsel
*evsel
;
835 evlist__for_each(evlist
, evsel
) {
838 if (evsel
->system_wide
&& thread
)
841 fd
= FD(evsel
, cpu
, thread
);
845 if (__perf_evlist__mmap(evlist
, idx
, mp
, *output
) < 0)
848 if (ioctl(fd
, PERF_EVENT_IOC_SET_OUTPUT
, *output
) != 0)
851 perf_evlist__mmap_get(evlist
, idx
);
855 * The system_wide flag causes a selected event to be opened
856 * always without a pid. Consequently it will never get a
857 * POLLHUP, but it is used for tracking in combination with
858 * other events, so it should not need to be polled anyway.
859 * Therefore don't add it for polling.
861 if (!evsel
->system_wide
&&
862 __perf_evlist__add_pollfd(evlist
, fd
, idx
) < 0) {
863 perf_evlist__mmap_put(evlist
, idx
);
867 if (evsel
->attr
.read_format
& PERF_FORMAT_ID
) {
868 if (perf_evlist__id_add_fd(evlist
, evsel
, cpu
, thread
,
871 perf_evlist__set_sid_idx(evlist
, evsel
, idx
, cpu
,
879 static int perf_evlist__mmap_per_cpu(struct perf_evlist
*evlist
,
880 struct mmap_params
*mp
)
883 int nr_cpus
= cpu_map__nr(evlist
->cpus
);
884 int nr_threads
= thread_map__nr(evlist
->threads
);
886 pr_debug2("perf event ring buffer mmapped per cpu\n");
887 for (cpu
= 0; cpu
< nr_cpus
; cpu
++) {
890 auxtrace_mmap_params__set_idx(&mp
->auxtrace_mp
, evlist
, cpu
,
893 for (thread
= 0; thread
< nr_threads
; thread
++) {
894 if (perf_evlist__mmap_per_evsel(evlist
, cpu
, mp
, cpu
,
903 for (cpu
= 0; cpu
< nr_cpus
; cpu
++)
904 __perf_evlist__munmap(evlist
, cpu
);
908 static int perf_evlist__mmap_per_thread(struct perf_evlist
*evlist
,
909 struct mmap_params
*mp
)
912 int nr_threads
= thread_map__nr(evlist
->threads
);
914 pr_debug2("perf event ring buffer mmapped per thread\n");
915 for (thread
= 0; thread
< nr_threads
; thread
++) {
918 auxtrace_mmap_params__set_idx(&mp
->auxtrace_mp
, evlist
, thread
,
921 if (perf_evlist__mmap_per_evsel(evlist
, thread
, mp
, 0, thread
,
929 for (thread
= 0; thread
< nr_threads
; thread
++)
930 __perf_evlist__munmap(evlist
, thread
);
934 static size_t perf_evlist__mmap_size(unsigned long pages
)
936 if (pages
== UINT_MAX
) {
939 if (sysctl__read_int("kernel/perf_event_mlock_kb", &max
) < 0) {
941 * Pick a once upon a time good value, i.e. things look
942 * strange since we can't read a sysctl value, but lets not
947 max
-= (page_size
/ 1024);
950 pages
= (max
* 1024) / page_size
;
951 if (!is_power_of_2(pages
))
952 pages
= rounddown_pow_of_two(pages
);
953 } else if (!is_power_of_2(pages
))
956 return (pages
+ 1) * page_size
;
959 static long parse_pages_arg(const char *str
, unsigned long min
,
962 unsigned long pages
, val
;
963 static struct parse_tag tags
[] = {
964 { .tag
= 'B', .mult
= 1 },
965 { .tag
= 'K', .mult
= 1 << 10 },
966 { .tag
= 'M', .mult
= 1 << 20 },
967 { .tag
= 'G', .mult
= 1 << 30 },
974 val
= parse_tag_value(str
, tags
);
975 if (val
!= (unsigned long) -1) {
976 /* we got file size value */
977 pages
= PERF_ALIGN(val
, page_size
) / page_size
;
979 /* we got pages count value */
981 pages
= strtoul(str
, &eptr
, 10);
986 if (pages
== 0 && min
== 0) {
987 /* leave number of pages at 0 */
988 } else if (!is_power_of_2(pages
)) {
989 /* round pages up to next power of 2 */
990 pages
= roundup_pow_of_two(pages
);
993 pr_info("rounding mmap pages size to %lu bytes (%lu pages)\n",
994 pages
* page_size
, pages
);
1003 int __perf_evlist__parse_mmap_pages(unsigned int *mmap_pages
, const char *str
)
1005 unsigned long max
= UINT_MAX
;
1008 if (max
> SIZE_MAX
/ page_size
)
1009 max
= SIZE_MAX
/ page_size
;
1011 pages
= parse_pages_arg(str
, 1, max
);
1013 pr_err("Invalid argument for --mmap_pages/-m\n");
1017 *mmap_pages
= pages
;
1021 int perf_evlist__parse_mmap_pages(const struct option
*opt
, const char *str
,
1022 int unset __maybe_unused
)
1024 return __perf_evlist__parse_mmap_pages(opt
->value
, str
);
1028 * perf_evlist__mmap_ex - Create mmaps to receive events.
1029 * @evlist: list of events
1030 * @pages: map length in pages
1031 * @overwrite: overwrite older events?
1032 * @auxtrace_pages - auxtrace map length in pages
1033 * @auxtrace_overwrite - overwrite older auxtrace data?
1035 * If @overwrite is %false the user needs to signal event consumption using
1036 * perf_mmap__write_tail(). Using perf_evlist__mmap_read() does this
1039 * Similarly, if @auxtrace_overwrite is %false the user needs to signal data
1040 * consumption using auxtrace_mmap__write_tail().
1042 * Return: %0 on success, negative error code otherwise.
1044 int perf_evlist__mmap_ex(struct perf_evlist
*evlist
, unsigned int pages
,
1045 bool overwrite
, unsigned int auxtrace_pages
,
1046 bool auxtrace_overwrite
)
1048 struct perf_evsel
*evsel
;
1049 const struct cpu_map
*cpus
= evlist
->cpus
;
1050 const struct thread_map
*threads
= evlist
->threads
;
1051 struct mmap_params mp
= {
1052 .prot
= PROT_READ
| (overwrite
? 0 : PROT_WRITE
),
1055 if (evlist
->mmap
== NULL
&& perf_evlist__alloc_mmap(evlist
) < 0)
1058 if (evlist
->pollfd
.entries
== NULL
&& perf_evlist__alloc_pollfd(evlist
) < 0)
1061 evlist
->overwrite
= overwrite
;
1062 evlist
->mmap_len
= perf_evlist__mmap_size(pages
);
1063 pr_debug("mmap size %zuB\n", evlist
->mmap_len
);
1064 mp
.mask
= evlist
->mmap_len
- page_size
- 1;
1066 auxtrace_mmap_params__init(&mp
.auxtrace_mp
, evlist
->mmap_len
,
1067 auxtrace_pages
, auxtrace_overwrite
);
1069 evlist__for_each(evlist
, evsel
) {
1070 if ((evsel
->attr
.read_format
& PERF_FORMAT_ID
) &&
1071 evsel
->sample_id
== NULL
&&
1072 perf_evsel__alloc_id(evsel
, cpu_map__nr(cpus
), threads
->nr
) < 0)
1076 if (cpu_map__empty(cpus
))
1077 return perf_evlist__mmap_per_thread(evlist
, &mp
);
1079 return perf_evlist__mmap_per_cpu(evlist
, &mp
);
1082 int perf_evlist__mmap(struct perf_evlist
*evlist
, unsigned int pages
,
1085 return perf_evlist__mmap_ex(evlist
, pages
, overwrite
, 0, false);
1088 int perf_evlist__create_maps(struct perf_evlist
*evlist
, struct target
*target
)
1090 evlist
->threads
= thread_map__new_str(target
->pid
, target
->tid
,
1093 if (evlist
->threads
== NULL
)
1096 if (target__uses_dummy_map(target
))
1097 evlist
->cpus
= cpu_map__dummy_new();
1099 evlist
->cpus
= cpu_map__new(target
->cpu_list
);
1101 if (evlist
->cpus
== NULL
)
1102 goto out_delete_threads
;
1107 thread_map__delete(evlist
->threads
);
1108 evlist
->threads
= NULL
;
1112 int perf_evlist__apply_filters(struct perf_evlist
*evlist
, struct perf_evsel
**err_evsel
)
1114 struct perf_evsel
*evsel
;
1116 const int ncpus
= cpu_map__nr(evlist
->cpus
),
1117 nthreads
= thread_map__nr(evlist
->threads
);
1119 evlist__for_each(evlist
, evsel
) {
1120 if (evsel
->filter
== NULL
)
1123 err
= perf_evsel__set_filter(evsel
, ncpus
, nthreads
, evsel
->filter
);
1133 int perf_evlist__set_filter(struct perf_evlist
*evlist
, const char *filter
)
1135 struct perf_evsel
*evsel
;
1137 const int ncpus
= cpu_map__nr(evlist
->cpus
),
1138 nthreads
= thread_map__nr(evlist
->threads
);
1140 evlist__for_each(evlist
, evsel
) {
1141 err
= perf_evsel__set_filter(evsel
, ncpus
, nthreads
, filter
);
1149 int perf_evlist__set_filter_pids(struct perf_evlist
*evlist
, size_t npids
, pid_t
*pids
)
1155 for (i
= 0; i
< npids
; ++i
) {
1157 if (asprintf(&filter
, "common_pid != %d", pids
[i
]) < 0)
1162 if (asprintf(&tmp
, "%s && common_pid != %d", filter
, pids
[i
]) < 0)
1170 ret
= perf_evlist__set_filter(evlist
, filter
);
1176 int perf_evlist__set_filter_pid(struct perf_evlist
*evlist
, pid_t pid
)
1178 return perf_evlist__set_filter_pids(evlist
, 1, &pid
);
1181 bool perf_evlist__valid_sample_type(struct perf_evlist
*evlist
)
1183 struct perf_evsel
*pos
;
1185 if (evlist
->nr_entries
== 1)
1188 if (evlist
->id_pos
< 0 || evlist
->is_pos
< 0)
1191 evlist__for_each(evlist
, pos
) {
1192 if (pos
->id_pos
!= evlist
->id_pos
||
1193 pos
->is_pos
!= evlist
->is_pos
)
1200 u64
__perf_evlist__combined_sample_type(struct perf_evlist
*evlist
)
1202 struct perf_evsel
*evsel
;
1204 if (evlist
->combined_sample_type
)
1205 return evlist
->combined_sample_type
;
1207 evlist__for_each(evlist
, evsel
)
1208 evlist
->combined_sample_type
|= evsel
->attr
.sample_type
;
1210 return evlist
->combined_sample_type
;
1213 u64
perf_evlist__combined_sample_type(struct perf_evlist
*evlist
)
1215 evlist
->combined_sample_type
= 0;
1216 return __perf_evlist__combined_sample_type(evlist
);
1219 bool perf_evlist__valid_read_format(struct perf_evlist
*evlist
)
1221 struct perf_evsel
*first
= perf_evlist__first(evlist
), *pos
= first
;
1222 u64 read_format
= first
->attr
.read_format
;
1223 u64 sample_type
= first
->attr
.sample_type
;
1225 evlist__for_each(evlist
, pos
) {
1226 if (read_format
!= pos
->attr
.read_format
)
1230 /* PERF_SAMPLE_READ imples PERF_FORMAT_ID. */
1231 if ((sample_type
& PERF_SAMPLE_READ
) &&
1232 !(read_format
& PERF_FORMAT_ID
)) {
1239 u64
perf_evlist__read_format(struct perf_evlist
*evlist
)
1241 struct perf_evsel
*first
= perf_evlist__first(evlist
);
1242 return first
->attr
.read_format
;
1245 u16
perf_evlist__id_hdr_size(struct perf_evlist
*evlist
)
1247 struct perf_evsel
*first
= perf_evlist__first(evlist
);
1248 struct perf_sample
*data
;
1252 if (!first
->attr
.sample_id_all
)
1255 sample_type
= first
->attr
.sample_type
;
1257 if (sample_type
& PERF_SAMPLE_TID
)
1258 size
+= sizeof(data
->tid
) * 2;
1260 if (sample_type
& PERF_SAMPLE_TIME
)
1261 size
+= sizeof(data
->time
);
1263 if (sample_type
& PERF_SAMPLE_ID
)
1264 size
+= sizeof(data
->id
);
1266 if (sample_type
& PERF_SAMPLE_STREAM_ID
)
1267 size
+= sizeof(data
->stream_id
);
1269 if (sample_type
& PERF_SAMPLE_CPU
)
1270 size
+= sizeof(data
->cpu
) * 2;
1272 if (sample_type
& PERF_SAMPLE_IDENTIFIER
)
1273 size
+= sizeof(data
->id
);
1278 bool perf_evlist__valid_sample_id_all(struct perf_evlist
*evlist
)
1280 struct perf_evsel
*first
= perf_evlist__first(evlist
), *pos
= first
;
1282 evlist__for_each_continue(evlist
, pos
) {
1283 if (first
->attr
.sample_id_all
!= pos
->attr
.sample_id_all
)
1290 bool perf_evlist__sample_id_all(struct perf_evlist
*evlist
)
1292 struct perf_evsel
*first
= perf_evlist__first(evlist
);
1293 return first
->attr
.sample_id_all
;
1296 void perf_evlist__set_selected(struct perf_evlist
*evlist
,
1297 struct perf_evsel
*evsel
)
1299 evlist
->selected
= evsel
;
1302 void perf_evlist__close(struct perf_evlist
*evlist
)
1304 struct perf_evsel
*evsel
;
1305 int ncpus
= cpu_map__nr(evlist
->cpus
);
1306 int nthreads
= thread_map__nr(evlist
->threads
);
1309 evlist__for_each_reverse(evlist
, evsel
) {
1310 n
= evsel
->cpus
? evsel
->cpus
->nr
: ncpus
;
1311 perf_evsel__close(evsel
, n
, nthreads
);
1315 static int perf_evlist__create_syswide_maps(struct perf_evlist
*evlist
)
1320 * Try reading /sys/devices/system/cpu/online to get
1323 * FIXME: -ENOMEM is the best we can do here, the cpu_map
1324 * code needs an overhaul to properly forward the
1325 * error, and we may not want to do that fallback to a
1326 * default cpu identity map :-\
1328 evlist
->cpus
= cpu_map__new(NULL
);
1329 if (evlist
->cpus
== NULL
)
1332 evlist
->threads
= thread_map__new_dummy();
1333 if (evlist
->threads
== NULL
)
1340 cpu_map__delete(evlist
->cpus
);
1341 evlist
->cpus
= NULL
;
1345 int perf_evlist__open(struct perf_evlist
*evlist
)
1347 struct perf_evsel
*evsel
;
1351 * Default: one fd per CPU, all threads, aka systemwide
1352 * as sys_perf_event_open(cpu = -1, thread = -1) is EINVAL
1354 if (evlist
->threads
== NULL
&& evlist
->cpus
== NULL
) {
1355 err
= perf_evlist__create_syswide_maps(evlist
);
1360 perf_evlist__update_id_pos(evlist
);
1362 evlist__for_each(evlist
, evsel
) {
1363 err
= perf_evsel__open(evsel
, evlist
->cpus
, evlist
->threads
);
1370 perf_evlist__close(evlist
);
1375 int perf_evlist__prepare_workload(struct perf_evlist
*evlist
, struct target
*target
,
1376 const char *argv
[], bool pipe_output
,
1377 void (*exec_error
)(int signo
, siginfo_t
*info
, void *ucontext
))
1379 int child_ready_pipe
[2], go_pipe
[2];
1382 if (pipe(child_ready_pipe
) < 0) {
1383 perror("failed to create 'ready' pipe");
1387 if (pipe(go_pipe
) < 0) {
1388 perror("failed to create 'go' pipe");
1389 goto out_close_ready_pipe
;
1392 evlist
->workload
.pid
= fork();
1393 if (evlist
->workload
.pid
< 0) {
1394 perror("failed to fork");
1395 goto out_close_pipes
;
1398 if (!evlist
->workload
.pid
) {
1404 signal(SIGTERM
, SIG_DFL
);
1406 close(child_ready_pipe
[0]);
1408 fcntl(go_pipe
[0], F_SETFD
, FD_CLOEXEC
);
1411 * Tell the parent we're ready to go
1413 close(child_ready_pipe
[1]);
1416 * Wait until the parent tells us to go.
1418 ret
= read(go_pipe
[0], &bf
, 1);
1420 * The parent will ask for the execvp() to be performed by
1421 * writing exactly one byte, in workload.cork_fd, usually via
1422 * perf_evlist__start_workload().
1424 * For cancelling the workload without actually running it,
1425 * the parent will just close workload.cork_fd, without writing
1426 * anything, i.e. read will return zero and we just exit()
1431 perror("unable to read pipe");
1435 execvp(argv
[0], (char **)argv
);
1440 val
.sival_int
= errno
;
1441 if (sigqueue(getppid(), SIGUSR1
, val
))
1449 struct sigaction act
= {
1450 .sa_flags
= SA_SIGINFO
,
1451 .sa_sigaction
= exec_error
,
1453 sigaction(SIGUSR1
, &act
, NULL
);
1456 if (target__none(target
)) {
1457 if (evlist
->threads
== NULL
) {
1458 fprintf(stderr
, "FATAL: evlist->threads need to be set at this point (%s:%d).\n",
1459 __func__
, __LINE__
);
1460 goto out_close_pipes
;
1462 evlist
->threads
->map
[0] = evlist
->workload
.pid
;
1465 close(child_ready_pipe
[1]);
1468 * wait for child to settle
1470 if (read(child_ready_pipe
[0], &bf
, 1) == -1) {
1471 perror("unable to read pipe");
1472 goto out_close_pipes
;
1475 fcntl(go_pipe
[1], F_SETFD
, FD_CLOEXEC
);
1476 evlist
->workload
.cork_fd
= go_pipe
[1];
1477 close(child_ready_pipe
[0]);
1483 out_close_ready_pipe
:
1484 close(child_ready_pipe
[0]);
1485 close(child_ready_pipe
[1]);
1489 int perf_evlist__start_workload(struct perf_evlist
*evlist
)
1491 if (evlist
->workload
.cork_fd
> 0) {
1495 * Remove the cork, let it rip!
1497 ret
= write(evlist
->workload
.cork_fd
, &bf
, 1);
1499 perror("enable to write to pipe");
1501 close(evlist
->workload
.cork_fd
);
1508 int perf_evlist__parse_sample(struct perf_evlist
*evlist
, union perf_event
*event
,
1509 struct perf_sample
*sample
)
1511 struct perf_evsel
*evsel
= perf_evlist__event2evsel(evlist
, event
);
1515 return perf_evsel__parse_sample(evsel
, event
, sample
);
1518 size_t perf_evlist__fprintf(struct perf_evlist
*evlist
, FILE *fp
)
1520 struct perf_evsel
*evsel
;
1523 evlist__for_each(evlist
, evsel
) {
1524 printed
+= fprintf(fp
, "%s%s", evsel
->idx
? ", " : "",
1525 perf_evsel__name(evsel
));
1528 return printed
+ fprintf(fp
, "\n");
1531 int perf_evlist__strerror_open(struct perf_evlist
*evlist __maybe_unused
,
1532 int err
, char *buf
, size_t size
)
1535 char sbuf
[STRERR_BUFSIZE
], *emsg
= strerror_r(err
, sbuf
, sizeof(sbuf
));
1540 printed
= scnprintf(buf
, size
,
1542 "Hint:\tCheck /proc/sys/kernel/perf_event_paranoid setting.", emsg
);
1544 value
= perf_event_paranoid();
1546 printed
+= scnprintf(buf
+ printed
, size
- printed
, "\nHint:\t");
1549 printed
+= scnprintf(buf
+ printed
, size
- printed
,
1550 "For your workloads it needs to be <= 1\nHint:\t");
1552 printed
+= scnprintf(buf
+ printed
, size
- printed
,
1553 "For system wide tracing it needs to be set to -1.\n");
1555 printed
+= scnprintf(buf
+ printed
, size
- printed
,
1556 "Hint:\tTry: 'sudo sh -c \"echo -1 > /proc/sys/kernel/perf_event_paranoid\"'\n"
1557 "Hint:\tThe current value is %d.", value
);
1560 scnprintf(buf
, size
, "%s", emsg
);
1567 int perf_evlist__strerror_mmap(struct perf_evlist
*evlist
, int err
, char *buf
, size_t size
)
1569 char sbuf
[STRERR_BUFSIZE
], *emsg
= strerror_r(err
, sbuf
, sizeof(sbuf
));
1570 int pages_attempted
= evlist
->mmap_len
/ 1024, pages_max_per_user
, printed
= 0;
1574 sysctl__read_int("kernel/perf_event_mlock_kb", &pages_max_per_user
);
1575 printed
+= scnprintf(buf
+ printed
, size
- printed
,
1577 "Hint:\tCheck /proc/sys/kernel/perf_event_mlock_kb (%d kB) setting.\n"
1578 "Hint:\tTried using %zd kB.\n",
1579 emsg
, pages_max_per_user
, pages_attempted
);
1581 if (pages_attempted
>= pages_max_per_user
) {
1582 printed
+= scnprintf(buf
+ printed
, size
- printed
,
1583 "Hint:\tTry 'sudo sh -c \"echo %d > /proc/sys/kernel/perf_event_mlock_kb\"', or\n",
1584 pages_max_per_user
+ pages_attempted
);
1587 printed
+= scnprintf(buf
+ printed
, size
- printed
,
1588 "Hint:\tTry using a smaller -m/--mmap-pages value.");
1591 scnprintf(buf
, size
, "%s", emsg
);
1598 void perf_evlist__to_front(struct perf_evlist
*evlist
,
1599 struct perf_evsel
*move_evsel
)
1601 struct perf_evsel
*evsel
, *n
;
1604 if (move_evsel
== perf_evlist__first(evlist
))
1607 evlist__for_each_safe(evlist
, n
, evsel
) {
1608 if (evsel
->leader
== move_evsel
->leader
)
1609 list_move_tail(&evsel
->node
, &move
);
1612 list_splice(&move
, &evlist
->entries
);
1615 void perf_evlist__set_tracking_event(struct perf_evlist
*evlist
,
1616 struct perf_evsel
*tracking_evsel
)
1618 struct perf_evsel
*evsel
;
1620 if (tracking_evsel
->tracking
)
1623 evlist__for_each(evlist
, evsel
) {
1624 if (evsel
!= tracking_evsel
)
1625 evsel
->tracking
= false;
1628 tracking_evsel
->tracking
= true;