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/debugfs.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>
28 #define FD(e, x, y) (*(int *)xyarray__entry(e->fd, x, y))
29 #define SID(e, x, y) xyarray__entry(e->sample_id, x, y)
31 void perf_evlist__init(struct perf_evlist
*evlist
, struct cpu_map
*cpus
,
32 struct thread_map
*threads
)
36 for (i
= 0; i
< PERF_EVLIST__HLIST_SIZE
; ++i
)
37 INIT_HLIST_HEAD(&evlist
->heads
[i
]);
38 INIT_LIST_HEAD(&evlist
->entries
);
39 perf_evlist__set_maps(evlist
, cpus
, threads
);
40 evlist
->workload
.pid
= -1;
43 struct perf_evlist
*perf_evlist__new(void)
45 struct perf_evlist
*evlist
= zalloc(sizeof(*evlist
));
48 perf_evlist__init(evlist
, NULL
, NULL
);
53 struct perf_evlist
*perf_evlist__new_default(void)
55 struct perf_evlist
*evlist
= perf_evlist__new();
57 if (evlist
&& perf_evlist__add_default(evlist
)) {
58 perf_evlist__delete(evlist
);
66 * perf_evlist__set_id_pos - set the positions of event ids.
67 * @evlist: selected event list
69 * Events with compatible sample types all have the same id_pos
70 * and is_pos. For convenience, put a copy on evlist.
72 void perf_evlist__set_id_pos(struct perf_evlist
*evlist
)
74 struct perf_evsel
*first
= perf_evlist__first(evlist
);
76 evlist
->id_pos
= first
->id_pos
;
77 evlist
->is_pos
= first
->is_pos
;
80 static void perf_evlist__update_id_pos(struct perf_evlist
*evlist
)
82 struct perf_evsel
*evsel
;
84 evlist__for_each(evlist
, evsel
)
85 perf_evsel__calc_id_pos(evsel
);
87 perf_evlist__set_id_pos(evlist
);
90 static void perf_evlist__purge(struct perf_evlist
*evlist
)
92 struct perf_evsel
*pos
, *n
;
94 evlist__for_each_safe(evlist
, n
, pos
) {
95 list_del_init(&pos
->node
);
96 perf_evsel__delete(pos
);
99 evlist
->nr_entries
= 0;
102 void perf_evlist__exit(struct perf_evlist
*evlist
)
104 zfree(&evlist
->mmap
);
105 zfree(&evlist
->pollfd
);
108 void perf_evlist__delete(struct perf_evlist
*evlist
)
110 perf_evlist__munmap(evlist
);
111 perf_evlist__close(evlist
);
112 cpu_map__delete(evlist
->cpus
);
113 thread_map__delete(evlist
->threads
);
115 evlist
->threads
= NULL
;
116 perf_evlist__purge(evlist
);
117 perf_evlist__exit(evlist
);
121 void perf_evlist__add(struct perf_evlist
*evlist
, struct perf_evsel
*entry
)
123 list_add_tail(&entry
->node
, &evlist
->entries
);
124 entry
->idx
= evlist
->nr_entries
;
125 entry
->tracking
= !entry
->idx
;
127 if (!evlist
->nr_entries
++)
128 perf_evlist__set_id_pos(evlist
);
131 void perf_evlist__splice_list_tail(struct perf_evlist
*evlist
,
132 struct list_head
*list
,
135 bool set_id_pos
= !evlist
->nr_entries
;
137 list_splice_tail(list
, &evlist
->entries
);
138 evlist
->nr_entries
+= nr_entries
;
140 perf_evlist__set_id_pos(evlist
);
143 void __perf_evlist__set_leader(struct list_head
*list
)
145 struct perf_evsel
*evsel
, *leader
;
147 leader
= list_entry(list
->next
, struct perf_evsel
, node
);
148 evsel
= list_entry(list
->prev
, struct perf_evsel
, node
);
150 leader
->nr_members
= evsel
->idx
- leader
->idx
+ 1;
152 __evlist__for_each(list
, evsel
) {
153 evsel
->leader
= leader
;
157 void perf_evlist__set_leader(struct perf_evlist
*evlist
)
159 if (evlist
->nr_entries
) {
160 evlist
->nr_groups
= evlist
->nr_entries
> 1 ? 1 : 0;
161 __perf_evlist__set_leader(&evlist
->entries
);
165 int perf_evlist__add_default(struct perf_evlist
*evlist
)
167 struct perf_event_attr attr
= {
168 .type
= PERF_TYPE_HARDWARE
,
169 .config
= PERF_COUNT_HW_CPU_CYCLES
,
171 struct perf_evsel
*evsel
;
173 event_attr_init(&attr
);
175 evsel
= perf_evsel__new(&attr
);
179 /* use strdup() because free(evsel) assumes name is allocated */
180 evsel
->name
= strdup("cycles");
184 perf_evlist__add(evlist
, evsel
);
187 perf_evsel__delete(evsel
);
192 static int perf_evlist__add_attrs(struct perf_evlist
*evlist
,
193 struct perf_event_attr
*attrs
, size_t nr_attrs
)
195 struct perf_evsel
*evsel
, *n
;
199 for (i
= 0; i
< nr_attrs
; i
++) {
200 evsel
= perf_evsel__new_idx(attrs
+ i
, evlist
->nr_entries
+ i
);
202 goto out_delete_partial_list
;
203 list_add_tail(&evsel
->node
, &head
);
206 perf_evlist__splice_list_tail(evlist
, &head
, nr_attrs
);
210 out_delete_partial_list
:
211 __evlist__for_each_safe(&head
, n
, evsel
)
212 perf_evsel__delete(evsel
);
216 int __perf_evlist__add_default_attrs(struct perf_evlist
*evlist
,
217 struct perf_event_attr
*attrs
, size_t nr_attrs
)
221 for (i
= 0; i
< nr_attrs
; i
++)
222 event_attr_init(attrs
+ i
);
224 return perf_evlist__add_attrs(evlist
, attrs
, nr_attrs
);
228 perf_evlist__find_tracepoint_by_id(struct perf_evlist
*evlist
, int id
)
230 struct perf_evsel
*evsel
;
232 evlist__for_each(evlist
, evsel
) {
233 if (evsel
->attr
.type
== PERF_TYPE_TRACEPOINT
&&
234 (int)evsel
->attr
.config
== id
)
242 perf_evlist__find_tracepoint_by_name(struct perf_evlist
*evlist
,
245 struct perf_evsel
*evsel
;
247 evlist__for_each(evlist
, evsel
) {
248 if ((evsel
->attr
.type
== PERF_TYPE_TRACEPOINT
) &&
249 (strcmp(evsel
->name
, name
) == 0))
256 int perf_evlist__add_newtp(struct perf_evlist
*evlist
,
257 const char *sys
, const char *name
, void *handler
)
259 struct perf_evsel
*evsel
= perf_evsel__newtp(sys
, name
);
264 evsel
->handler
= handler
;
265 perf_evlist__add(evlist
, evsel
);
269 static int perf_evlist__nr_threads(struct perf_evlist
*evlist
,
270 struct perf_evsel
*evsel
)
272 if (evsel
->system_wide
)
275 return thread_map__nr(evlist
->threads
);
278 void perf_evlist__disable(struct perf_evlist
*evlist
)
281 struct perf_evsel
*pos
;
282 int nr_cpus
= cpu_map__nr(evlist
->cpus
);
285 for (cpu
= 0; cpu
< nr_cpus
; cpu
++) {
286 evlist__for_each(evlist
, pos
) {
287 if (!perf_evsel__is_group_leader(pos
) || !pos
->fd
)
289 nr_threads
= perf_evlist__nr_threads(evlist
, pos
);
290 for (thread
= 0; thread
< nr_threads
; thread
++)
291 ioctl(FD(pos
, cpu
, thread
),
292 PERF_EVENT_IOC_DISABLE
, 0);
297 void perf_evlist__enable(struct perf_evlist
*evlist
)
300 struct perf_evsel
*pos
;
301 int nr_cpus
= cpu_map__nr(evlist
->cpus
);
304 for (cpu
= 0; cpu
< nr_cpus
; cpu
++) {
305 evlist__for_each(evlist
, pos
) {
306 if (!perf_evsel__is_group_leader(pos
) || !pos
->fd
)
308 nr_threads
= perf_evlist__nr_threads(evlist
, pos
);
309 for (thread
= 0; thread
< nr_threads
; thread
++)
310 ioctl(FD(pos
, cpu
, thread
),
311 PERF_EVENT_IOC_ENABLE
, 0);
316 int perf_evlist__disable_event(struct perf_evlist
*evlist
,
317 struct perf_evsel
*evsel
)
319 int cpu
, thread
, err
;
320 int nr_cpus
= cpu_map__nr(evlist
->cpus
);
321 int nr_threads
= perf_evlist__nr_threads(evlist
, evsel
);
326 for (cpu
= 0; cpu
< nr_cpus
; cpu
++) {
327 for (thread
= 0; thread
< nr_threads
; thread
++) {
328 err
= ioctl(FD(evsel
, cpu
, thread
),
329 PERF_EVENT_IOC_DISABLE
, 0);
337 int perf_evlist__enable_event(struct perf_evlist
*evlist
,
338 struct perf_evsel
*evsel
)
340 int cpu
, thread
, err
;
341 int nr_cpus
= cpu_map__nr(evlist
->cpus
);
342 int nr_threads
= perf_evlist__nr_threads(evlist
, evsel
);
347 for (cpu
= 0; cpu
< nr_cpus
; cpu
++) {
348 for (thread
= 0; thread
< nr_threads
; thread
++) {
349 err
= ioctl(FD(evsel
, cpu
, thread
),
350 PERF_EVENT_IOC_ENABLE
, 0);
358 static int perf_evlist__alloc_pollfd(struct perf_evlist
*evlist
)
360 int nr_cpus
= cpu_map__nr(evlist
->cpus
);
361 int nr_threads
= thread_map__nr(evlist
->threads
);
363 struct perf_evsel
*evsel
;
365 list_for_each_entry(evsel
, &evlist
->entries
, node
) {
366 if (evsel
->system_wide
)
369 nfds
+= nr_cpus
* nr_threads
;
372 evlist
->pollfd
= malloc(sizeof(struct pollfd
) * nfds
);
373 return evlist
->pollfd
!= NULL
? 0 : -ENOMEM
;
376 void perf_evlist__add_pollfd(struct perf_evlist
*evlist
, int fd
)
378 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
379 evlist
->pollfd
[evlist
->nr_fds
].fd
= fd
;
380 evlist
->pollfd
[evlist
->nr_fds
].events
= POLLIN
;
384 static void perf_evlist__id_hash(struct perf_evlist
*evlist
,
385 struct perf_evsel
*evsel
,
386 int cpu
, int thread
, u64 id
)
389 struct perf_sample_id
*sid
= SID(evsel
, cpu
, thread
);
393 hash
= hash_64(sid
->id
, PERF_EVLIST__HLIST_BITS
);
394 hlist_add_head(&sid
->node
, &evlist
->heads
[hash
]);
397 void perf_evlist__id_add(struct perf_evlist
*evlist
, struct perf_evsel
*evsel
,
398 int cpu
, int thread
, u64 id
)
400 perf_evlist__id_hash(evlist
, evsel
, cpu
, thread
, id
);
401 evsel
->id
[evsel
->ids
++] = id
;
404 static int perf_evlist__id_add_fd(struct perf_evlist
*evlist
,
405 struct perf_evsel
*evsel
,
406 int cpu
, int thread
, int fd
)
408 u64 read_data
[4] = { 0, };
409 int id_idx
= 1; /* The first entry is the counter value */
413 ret
= ioctl(fd
, PERF_EVENT_IOC_ID
, &id
);
420 /* Legacy way to get event id.. All hail to old kernels! */
423 * This way does not work with group format read, so bail
426 if (perf_evlist__read_format(evlist
) & PERF_FORMAT_GROUP
)
429 if (!(evsel
->attr
.read_format
& PERF_FORMAT_ID
) ||
430 read(fd
, &read_data
, sizeof(read_data
)) == -1)
433 if (evsel
->attr
.read_format
& PERF_FORMAT_TOTAL_TIME_ENABLED
)
435 if (evsel
->attr
.read_format
& PERF_FORMAT_TOTAL_TIME_RUNNING
)
438 id
= read_data
[id_idx
];
441 perf_evlist__id_add(evlist
, evsel
, cpu
, thread
, id
);
445 struct perf_sample_id
*perf_evlist__id2sid(struct perf_evlist
*evlist
, u64 id
)
447 struct hlist_head
*head
;
448 struct perf_sample_id
*sid
;
451 hash
= hash_64(id
, PERF_EVLIST__HLIST_BITS
);
452 head
= &evlist
->heads
[hash
];
454 hlist_for_each_entry(sid
, head
, node
)
461 struct perf_evsel
*perf_evlist__id2evsel(struct perf_evlist
*evlist
, u64 id
)
463 struct perf_sample_id
*sid
;
465 if (evlist
->nr_entries
== 1)
466 return perf_evlist__first(evlist
);
468 sid
= perf_evlist__id2sid(evlist
, id
);
472 if (!perf_evlist__sample_id_all(evlist
))
473 return perf_evlist__first(evlist
);
478 static int perf_evlist__event2id(struct perf_evlist
*evlist
,
479 union perf_event
*event
, u64
*id
)
481 const u64
*array
= event
->sample
.array
;
484 n
= (event
->header
.size
- sizeof(event
->header
)) >> 3;
486 if (event
->header
.type
== PERF_RECORD_SAMPLE
) {
487 if (evlist
->id_pos
>= n
)
489 *id
= array
[evlist
->id_pos
];
491 if (evlist
->is_pos
> n
)
499 static struct perf_evsel
*perf_evlist__event2evsel(struct perf_evlist
*evlist
,
500 union perf_event
*event
)
502 struct perf_evsel
*first
= perf_evlist__first(evlist
);
503 struct hlist_head
*head
;
504 struct perf_sample_id
*sid
;
508 if (evlist
->nr_entries
== 1)
511 if (!first
->attr
.sample_id_all
&&
512 event
->header
.type
!= PERF_RECORD_SAMPLE
)
515 if (perf_evlist__event2id(evlist
, event
, &id
))
518 /* Synthesized events have an id of zero */
522 hash
= hash_64(id
, PERF_EVLIST__HLIST_BITS
);
523 head
= &evlist
->heads
[hash
];
525 hlist_for_each_entry(sid
, head
, node
) {
532 union perf_event
*perf_evlist__mmap_read(struct perf_evlist
*evlist
, int idx
)
534 struct perf_mmap
*md
= &evlist
->mmap
[idx
];
535 unsigned int head
= perf_mmap__read_head(md
);
536 unsigned int old
= md
->prev
;
537 unsigned char *data
= md
->base
+ page_size
;
538 union perf_event
*event
= NULL
;
540 if (evlist
->overwrite
) {
542 * If we're further behind than half the buffer, there's a chance
543 * the writer will bite our tail and mess up the samples under us.
545 * If we somehow ended up ahead of the head, we got messed up.
547 * In either case, truncate and restart at head.
549 int diff
= head
- old
;
550 if (diff
> md
->mask
/ 2 || diff
< 0) {
551 fprintf(stderr
, "WARNING: failed to keep up with mmap data.\n");
554 * head points to a known good entry, start there.
563 event
= (union perf_event
*)&data
[old
& md
->mask
];
564 size
= event
->header
.size
;
567 * Event straddles the mmap boundary -- header should always
568 * be inside due to u64 alignment of output.
570 if ((old
& md
->mask
) + size
!= ((old
+ size
) & md
->mask
)) {
571 unsigned int offset
= old
;
572 unsigned int len
= min(sizeof(*event
), size
), cpy
;
573 void *dst
= md
->event_copy
;
576 cpy
= min(md
->mask
+ 1 - (offset
& md
->mask
), len
);
577 memcpy(dst
, &data
[offset
& md
->mask
], cpy
);
583 event
= (union perf_event
*) md
->event_copy
;
594 void perf_evlist__mmap_consume(struct perf_evlist
*evlist
, int idx
)
596 if (!evlist
->overwrite
) {
597 struct perf_mmap
*md
= &evlist
->mmap
[idx
];
598 unsigned int old
= md
->prev
;
600 perf_mmap__write_tail(md
, old
);
604 static void __perf_evlist__munmap(struct perf_evlist
*evlist
, int idx
)
606 if (evlist
->mmap
[idx
].base
!= NULL
) {
607 munmap(evlist
->mmap
[idx
].base
, evlist
->mmap_len
);
608 evlist
->mmap
[idx
].base
= NULL
;
612 void perf_evlist__munmap(struct perf_evlist
*evlist
)
616 if (evlist
->mmap
== NULL
)
619 for (i
= 0; i
< evlist
->nr_mmaps
; i
++)
620 __perf_evlist__munmap(evlist
, i
);
622 zfree(&evlist
->mmap
);
625 static int perf_evlist__alloc_mmap(struct perf_evlist
*evlist
)
627 evlist
->nr_mmaps
= cpu_map__nr(evlist
->cpus
);
628 if (cpu_map__empty(evlist
->cpus
))
629 evlist
->nr_mmaps
= thread_map__nr(evlist
->threads
);
630 evlist
->mmap
= zalloc(evlist
->nr_mmaps
* sizeof(struct perf_mmap
));
631 return evlist
->mmap
!= NULL
? 0 : -ENOMEM
;
639 static int __perf_evlist__mmap(struct perf_evlist
*evlist
, int idx
,
640 struct mmap_params
*mp
, int fd
)
642 evlist
->mmap
[idx
].prev
= 0;
643 evlist
->mmap
[idx
].mask
= mp
->mask
;
644 evlist
->mmap
[idx
].base
= mmap(NULL
, evlist
->mmap_len
, mp
->prot
,
646 if (evlist
->mmap
[idx
].base
== MAP_FAILED
) {
647 pr_debug2("failed to mmap perf event ring buffer, error %d\n",
649 evlist
->mmap
[idx
].base
= NULL
;
653 perf_evlist__add_pollfd(evlist
, fd
);
657 static int perf_evlist__mmap_per_evsel(struct perf_evlist
*evlist
, int idx
,
658 struct mmap_params
*mp
, int cpu
,
659 int thread
, int *output
)
661 struct perf_evsel
*evsel
;
663 evlist__for_each(evlist
, evsel
) {
666 if (evsel
->system_wide
&& thread
)
669 fd
= FD(evsel
, cpu
, thread
);
673 if (__perf_evlist__mmap(evlist
, idx
, mp
, *output
) < 0)
676 if (ioctl(fd
, PERF_EVENT_IOC_SET_OUTPUT
, *output
) != 0)
680 if ((evsel
->attr
.read_format
& PERF_FORMAT_ID
) &&
681 perf_evlist__id_add_fd(evlist
, evsel
, cpu
, thread
, fd
) < 0)
688 static int perf_evlist__mmap_per_cpu(struct perf_evlist
*evlist
,
689 struct mmap_params
*mp
)
692 int nr_cpus
= cpu_map__nr(evlist
->cpus
);
693 int nr_threads
= thread_map__nr(evlist
->threads
);
695 pr_debug2("perf event ring buffer mmapped per cpu\n");
696 for (cpu
= 0; cpu
< nr_cpus
; cpu
++) {
699 for (thread
= 0; thread
< nr_threads
; thread
++) {
700 if (perf_evlist__mmap_per_evsel(evlist
, cpu
, mp
, cpu
,
709 for (cpu
= 0; cpu
< nr_cpus
; cpu
++)
710 __perf_evlist__munmap(evlist
, cpu
);
714 static int perf_evlist__mmap_per_thread(struct perf_evlist
*evlist
,
715 struct mmap_params
*mp
)
718 int nr_threads
= thread_map__nr(evlist
->threads
);
720 pr_debug2("perf event ring buffer mmapped per thread\n");
721 for (thread
= 0; thread
< nr_threads
; thread
++) {
724 if (perf_evlist__mmap_per_evsel(evlist
, thread
, mp
, 0, thread
,
732 for (thread
= 0; thread
< nr_threads
; thread
++)
733 __perf_evlist__munmap(evlist
, thread
);
737 static size_t perf_evlist__mmap_size(unsigned long pages
)
739 /* 512 kiB: default amount of unprivileged mlocked memory */
740 if (pages
== UINT_MAX
)
741 pages
= (512 * 1024) / page_size
;
742 else if (!is_power_of_2(pages
))
745 return (pages
+ 1) * page_size
;
748 static long parse_pages_arg(const char *str
, unsigned long min
,
751 unsigned long pages
, val
;
752 static struct parse_tag tags
[] = {
753 { .tag
= 'B', .mult
= 1 },
754 { .tag
= 'K', .mult
= 1 << 10 },
755 { .tag
= 'M', .mult
= 1 << 20 },
756 { .tag
= 'G', .mult
= 1 << 30 },
763 val
= parse_tag_value(str
, tags
);
764 if (val
!= (unsigned long) -1) {
765 /* we got file size value */
766 pages
= PERF_ALIGN(val
, page_size
) / page_size
;
768 /* we got pages count value */
770 pages
= strtoul(str
, &eptr
, 10);
775 if (pages
== 0 && min
== 0) {
776 /* leave number of pages at 0 */
777 } else if (!is_power_of_2(pages
)) {
778 /* round pages up to next power of 2 */
779 pages
= next_pow2_l(pages
);
782 pr_info("rounding mmap pages size to %lu bytes (%lu pages)\n",
783 pages
* page_size
, pages
);
792 int perf_evlist__parse_mmap_pages(const struct option
*opt
, const char *str
,
793 int unset __maybe_unused
)
795 unsigned int *mmap_pages
= opt
->value
;
796 unsigned long max
= UINT_MAX
;
799 if (max
> SIZE_MAX
/ page_size
)
800 max
= SIZE_MAX
/ page_size
;
802 pages
= parse_pages_arg(str
, 1, max
);
804 pr_err("Invalid argument for --mmap_pages/-m\n");
813 * perf_evlist__mmap - Create mmaps to receive events.
814 * @evlist: list of events
815 * @pages: map length in pages
816 * @overwrite: overwrite older events?
818 * If @overwrite is %false the user needs to signal event consumption using
819 * perf_mmap__write_tail(). Using perf_evlist__mmap_read() does this
822 * Return: %0 on success, negative error code otherwise.
824 int perf_evlist__mmap(struct perf_evlist
*evlist
, unsigned int pages
,
827 struct perf_evsel
*evsel
;
828 const struct cpu_map
*cpus
= evlist
->cpus
;
829 const struct thread_map
*threads
= evlist
->threads
;
830 struct mmap_params mp
= {
831 .prot
= PROT_READ
| (overwrite
? 0 : PROT_WRITE
),
834 if (evlist
->mmap
== NULL
&& perf_evlist__alloc_mmap(evlist
) < 0)
837 if (evlist
->pollfd
== NULL
&& perf_evlist__alloc_pollfd(evlist
) < 0)
840 evlist
->overwrite
= overwrite
;
841 evlist
->mmap_len
= perf_evlist__mmap_size(pages
);
842 pr_debug("mmap size %zuB\n", evlist
->mmap_len
);
843 mp
.mask
= evlist
->mmap_len
- page_size
- 1;
845 evlist__for_each(evlist
, evsel
) {
846 if ((evsel
->attr
.read_format
& PERF_FORMAT_ID
) &&
847 evsel
->sample_id
== NULL
&&
848 perf_evsel__alloc_id(evsel
, cpu_map__nr(cpus
), threads
->nr
) < 0)
852 if (cpu_map__empty(cpus
))
853 return perf_evlist__mmap_per_thread(evlist
, &mp
);
855 return perf_evlist__mmap_per_cpu(evlist
, &mp
);
858 int perf_evlist__create_maps(struct perf_evlist
*evlist
, struct target
*target
)
860 evlist
->threads
= thread_map__new_str(target
->pid
, target
->tid
,
863 if (evlist
->threads
== NULL
)
866 if (target__uses_dummy_map(target
))
867 evlist
->cpus
= cpu_map__dummy_new();
869 evlist
->cpus
= cpu_map__new(target
->cpu_list
);
871 if (evlist
->cpus
== NULL
)
872 goto out_delete_threads
;
877 thread_map__delete(evlist
->threads
);
881 int perf_evlist__apply_filters(struct perf_evlist
*evlist
)
883 struct perf_evsel
*evsel
;
885 const int ncpus
= cpu_map__nr(evlist
->cpus
),
886 nthreads
= thread_map__nr(evlist
->threads
);
888 evlist__for_each(evlist
, evsel
) {
889 if (evsel
->filter
== NULL
)
892 err
= perf_evsel__set_filter(evsel
, ncpus
, nthreads
, evsel
->filter
);
900 int perf_evlist__set_filter(struct perf_evlist
*evlist
, const char *filter
)
902 struct perf_evsel
*evsel
;
904 const int ncpus
= cpu_map__nr(evlist
->cpus
),
905 nthreads
= thread_map__nr(evlist
->threads
);
907 evlist__for_each(evlist
, evsel
) {
908 err
= perf_evsel__set_filter(evsel
, ncpus
, nthreads
, filter
);
916 bool perf_evlist__valid_sample_type(struct perf_evlist
*evlist
)
918 struct perf_evsel
*pos
;
920 if (evlist
->nr_entries
== 1)
923 if (evlist
->id_pos
< 0 || evlist
->is_pos
< 0)
926 evlist__for_each(evlist
, pos
) {
927 if (pos
->id_pos
!= evlist
->id_pos
||
928 pos
->is_pos
!= evlist
->is_pos
)
935 u64
__perf_evlist__combined_sample_type(struct perf_evlist
*evlist
)
937 struct perf_evsel
*evsel
;
939 if (evlist
->combined_sample_type
)
940 return evlist
->combined_sample_type
;
942 evlist__for_each(evlist
, evsel
)
943 evlist
->combined_sample_type
|= evsel
->attr
.sample_type
;
945 return evlist
->combined_sample_type
;
948 u64
perf_evlist__combined_sample_type(struct perf_evlist
*evlist
)
950 evlist
->combined_sample_type
= 0;
951 return __perf_evlist__combined_sample_type(evlist
);
954 bool perf_evlist__valid_read_format(struct perf_evlist
*evlist
)
956 struct perf_evsel
*first
= perf_evlist__first(evlist
), *pos
= first
;
957 u64 read_format
= first
->attr
.read_format
;
958 u64 sample_type
= first
->attr
.sample_type
;
960 evlist__for_each(evlist
, pos
) {
961 if (read_format
!= pos
->attr
.read_format
)
965 /* PERF_SAMPLE_READ imples PERF_FORMAT_ID. */
966 if ((sample_type
& PERF_SAMPLE_READ
) &&
967 !(read_format
& PERF_FORMAT_ID
)) {
974 u64
perf_evlist__read_format(struct perf_evlist
*evlist
)
976 struct perf_evsel
*first
= perf_evlist__first(evlist
);
977 return first
->attr
.read_format
;
980 u16
perf_evlist__id_hdr_size(struct perf_evlist
*evlist
)
982 struct perf_evsel
*first
= perf_evlist__first(evlist
);
983 struct perf_sample
*data
;
987 if (!first
->attr
.sample_id_all
)
990 sample_type
= first
->attr
.sample_type
;
992 if (sample_type
& PERF_SAMPLE_TID
)
993 size
+= sizeof(data
->tid
) * 2;
995 if (sample_type
& PERF_SAMPLE_TIME
)
996 size
+= sizeof(data
->time
);
998 if (sample_type
& PERF_SAMPLE_ID
)
999 size
+= sizeof(data
->id
);
1001 if (sample_type
& PERF_SAMPLE_STREAM_ID
)
1002 size
+= sizeof(data
->stream_id
);
1004 if (sample_type
& PERF_SAMPLE_CPU
)
1005 size
+= sizeof(data
->cpu
) * 2;
1007 if (sample_type
& PERF_SAMPLE_IDENTIFIER
)
1008 size
+= sizeof(data
->id
);
1013 bool perf_evlist__valid_sample_id_all(struct perf_evlist
*evlist
)
1015 struct perf_evsel
*first
= perf_evlist__first(evlist
), *pos
= first
;
1017 evlist__for_each_continue(evlist
, pos
) {
1018 if (first
->attr
.sample_id_all
!= pos
->attr
.sample_id_all
)
1025 bool perf_evlist__sample_id_all(struct perf_evlist
*evlist
)
1027 struct perf_evsel
*first
= perf_evlist__first(evlist
);
1028 return first
->attr
.sample_id_all
;
1031 void perf_evlist__set_selected(struct perf_evlist
*evlist
,
1032 struct perf_evsel
*evsel
)
1034 evlist
->selected
= evsel
;
1037 void perf_evlist__close(struct perf_evlist
*evlist
)
1039 struct perf_evsel
*evsel
;
1040 int ncpus
= cpu_map__nr(evlist
->cpus
);
1041 int nthreads
= thread_map__nr(evlist
->threads
);
1044 evlist__for_each_reverse(evlist
, evsel
) {
1045 n
= evsel
->cpus
? evsel
->cpus
->nr
: ncpus
;
1046 perf_evsel__close(evsel
, n
, nthreads
);
1050 int perf_evlist__open(struct perf_evlist
*evlist
)
1052 struct perf_evsel
*evsel
;
1055 perf_evlist__update_id_pos(evlist
);
1057 evlist__for_each(evlist
, evsel
) {
1058 err
= perf_evsel__open(evsel
, evlist
->cpus
, evlist
->threads
);
1065 perf_evlist__close(evlist
);
1070 int perf_evlist__prepare_workload(struct perf_evlist
*evlist
, struct target
*target
,
1071 const char *argv
[], bool pipe_output
,
1072 void (*exec_error
)(int signo
, siginfo_t
*info
, void *ucontext
))
1074 int child_ready_pipe
[2], go_pipe
[2];
1077 if (pipe(child_ready_pipe
) < 0) {
1078 perror("failed to create 'ready' pipe");
1082 if (pipe(go_pipe
) < 0) {
1083 perror("failed to create 'go' pipe");
1084 goto out_close_ready_pipe
;
1087 evlist
->workload
.pid
= fork();
1088 if (evlist
->workload
.pid
< 0) {
1089 perror("failed to fork");
1090 goto out_close_pipes
;
1093 if (!evlist
->workload
.pid
) {
1099 signal(SIGTERM
, SIG_DFL
);
1101 close(child_ready_pipe
[0]);
1103 fcntl(go_pipe
[0], F_SETFD
, FD_CLOEXEC
);
1106 * Tell the parent we're ready to go
1108 close(child_ready_pipe
[1]);
1111 * Wait until the parent tells us to go.
1113 ret
= read(go_pipe
[0], &bf
, 1);
1115 * The parent will ask for the execvp() to be performed by
1116 * writing exactly one byte, in workload.cork_fd, usually via
1117 * perf_evlist__start_workload().
1119 * For cancelling the workload without actuallin running it,
1120 * the parent will just close workload.cork_fd, without writing
1121 * anything, i.e. read will return zero and we just exit()
1126 perror("unable to read pipe");
1130 execvp(argv
[0], (char **)argv
);
1135 val
.sival_int
= errno
;
1136 if (sigqueue(getppid(), SIGUSR1
, val
))
1144 struct sigaction act
= {
1145 .sa_flags
= SA_SIGINFO
,
1146 .sa_sigaction
= exec_error
,
1148 sigaction(SIGUSR1
, &act
, NULL
);
1151 if (target__none(target
))
1152 evlist
->threads
->map
[0] = evlist
->workload
.pid
;
1154 close(child_ready_pipe
[1]);
1157 * wait for child to settle
1159 if (read(child_ready_pipe
[0], &bf
, 1) == -1) {
1160 perror("unable to read pipe");
1161 goto out_close_pipes
;
1164 fcntl(go_pipe
[1], F_SETFD
, FD_CLOEXEC
);
1165 evlist
->workload
.cork_fd
= go_pipe
[1];
1166 close(child_ready_pipe
[0]);
1172 out_close_ready_pipe
:
1173 close(child_ready_pipe
[0]);
1174 close(child_ready_pipe
[1]);
1178 int perf_evlist__start_workload(struct perf_evlist
*evlist
)
1180 if (evlist
->workload
.cork_fd
> 0) {
1184 * Remove the cork, let it rip!
1186 ret
= write(evlist
->workload
.cork_fd
, &bf
, 1);
1188 perror("enable to write to pipe");
1190 close(evlist
->workload
.cork_fd
);
1197 int perf_evlist__parse_sample(struct perf_evlist
*evlist
, union perf_event
*event
,
1198 struct perf_sample
*sample
)
1200 struct perf_evsel
*evsel
= perf_evlist__event2evsel(evlist
, event
);
1204 return perf_evsel__parse_sample(evsel
, event
, sample
);
1207 size_t perf_evlist__fprintf(struct perf_evlist
*evlist
, FILE *fp
)
1209 struct perf_evsel
*evsel
;
1212 evlist__for_each(evlist
, evsel
) {
1213 printed
+= fprintf(fp
, "%s%s", evsel
->idx
? ", " : "",
1214 perf_evsel__name(evsel
));
1217 return printed
+ fprintf(fp
, "\n");
1220 int perf_evlist__strerror_tp(struct perf_evlist
*evlist __maybe_unused
,
1221 int err
, char *buf
, size_t size
)
1227 scnprintf(buf
, size
, "%s",
1228 "Error:\tUnable to find debugfs\n"
1229 "Hint:\tWas your kernel was compiled with debugfs support?\n"
1230 "Hint:\tIs the debugfs filesystem mounted?\n"
1231 "Hint:\tTry 'sudo mount -t debugfs nodev /sys/kernel/debug'");
1234 scnprintf(buf
, size
,
1235 "Error:\tNo permissions to read %s/tracing/events/raw_syscalls\n"
1236 "Hint:\tTry 'sudo mount -o remount,mode=755 %s'\n",
1237 debugfs_mountpoint
, debugfs_mountpoint
);
1240 scnprintf(buf
, size
, "%s", strerror_r(err
, sbuf
, sizeof(sbuf
)));
1247 int perf_evlist__strerror_open(struct perf_evlist
*evlist __maybe_unused
,
1248 int err
, char *buf
, size_t size
)
1251 char sbuf
[128], *emsg
= strerror_r(err
, sbuf
, sizeof(sbuf
));
1256 printed
= scnprintf(buf
, size
,
1258 "Hint:\tCheck /proc/sys/kernel/perf_event_paranoid setting.", emsg
);
1260 value
= perf_event_paranoid();
1262 printed
+= scnprintf(buf
+ printed
, size
- printed
, "\nHint:\t");
1265 printed
+= scnprintf(buf
+ printed
, size
- printed
,
1266 "For your workloads it needs to be <= 1\nHint:\t");
1268 printed
+= scnprintf(buf
+ printed
, size
- printed
,
1269 "For system wide tracing it needs to be set to -1.\n");
1271 printed
+= scnprintf(buf
+ printed
, size
- printed
,
1272 "Hint:\tTry: 'sudo sh -c \"echo -1 > /proc/sys/kernel/perf_event_paranoid\"'\n"
1273 "Hint:\tThe current value is %d.", value
);
1276 scnprintf(buf
, size
, "%s", emsg
);
1283 void perf_evlist__to_front(struct perf_evlist
*evlist
,
1284 struct perf_evsel
*move_evsel
)
1286 struct perf_evsel
*evsel
, *n
;
1289 if (move_evsel
== perf_evlist__first(evlist
))
1292 evlist__for_each_safe(evlist
, n
, evsel
) {
1293 if (evsel
->leader
== move_evsel
->leader
)
1294 list_move_tail(&evsel
->node
, &move
);
1297 list_splice(&move
, &evlist
->entries
);
1300 void perf_evlist__set_tracking_event(struct perf_evlist
*evlist
,
1301 struct perf_evsel
*tracking_evsel
)
1303 struct perf_evsel
*evsel
;
1305 if (tracking_evsel
->tracking
)
1308 evlist__for_each(evlist
, evsel
) {
1309 if (evsel
!= tracking_evsel
)
1310 evsel
->tracking
= false;
1313 tracking_evsel
->tracking
= true;