1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2011, Red Hat Inc, Arnaldo Carvalho de Melo <acme@redhat.com>
5 * Parts came from builtin-{top,stat,record}.c, see those files for further
13 #include "util/mmap.h"
14 #include "thread_map.h"
20 #include <internal/lib.h> // page_size
23 #include "bpf-event.h"
29 #include "parse-events.h"
30 #include <subcmd/parse-options.h>
33 #include <sys/ioctl.h>
36 #include <linux/bitops.h>
37 #include <linux/hash.h>
38 #include <linux/log2.h>
39 #include <linux/err.h>
40 #include <linux/string.h>
41 #include <linux/zalloc.h>
42 #include <perf/evlist.h>
43 #include <perf/evsel.h>
44 #include <perf/cpumap.h>
46 #include <internal/xyarray.h>
48 #ifdef LACKS_SIGQUEUE_PROTOTYPE
49 int sigqueue(pid_t pid
, int sig
, const union sigval value
);
52 #define FD(e, x, y) (*(int *)xyarray__entry(e->core.fd, x, y))
53 #define SID(e, x, y) xyarray__entry(e->core.sample_id, x, y)
55 void evlist__init(struct evlist
*evlist
, struct perf_cpu_map
*cpus
,
56 struct perf_thread_map
*threads
)
58 perf_evlist__init(&evlist
->core
);
59 perf_evlist__set_maps(&evlist
->core
, cpus
, threads
);
60 fdarray__init(&evlist
->core
.pollfd
, 64);
61 evlist
->workload
.pid
= -1;
62 evlist
->bkw_mmap_state
= BKW_MMAP_NOTREADY
;
65 struct evlist
*evlist__new(void)
67 struct evlist
*evlist
= zalloc(sizeof(*evlist
));
70 evlist__init(evlist
, NULL
, NULL
);
75 struct evlist
*perf_evlist__new_default(void)
77 struct evlist
*evlist
= evlist__new();
79 if (evlist
&& perf_evlist__add_default(evlist
)) {
80 evlist__delete(evlist
);
87 struct evlist
*perf_evlist__new_dummy(void)
89 struct evlist
*evlist
= evlist__new();
91 if (evlist
&& perf_evlist__add_dummy(evlist
)) {
92 evlist__delete(evlist
);
100 * perf_evlist__set_id_pos - set the positions of event ids.
101 * @evlist: selected event list
103 * Events with compatible sample types all have the same id_pos
104 * and is_pos. For convenience, put a copy on evlist.
106 void perf_evlist__set_id_pos(struct evlist
*evlist
)
108 struct evsel
*first
= evlist__first(evlist
);
110 evlist
->id_pos
= first
->id_pos
;
111 evlist
->is_pos
= first
->is_pos
;
114 static void perf_evlist__update_id_pos(struct evlist
*evlist
)
118 evlist__for_each_entry(evlist
, evsel
)
119 perf_evsel__calc_id_pos(evsel
);
121 perf_evlist__set_id_pos(evlist
);
124 static void evlist__purge(struct evlist
*evlist
)
126 struct evsel
*pos
, *n
;
128 evlist__for_each_entry_safe(evlist
, n
, pos
) {
129 list_del_init(&pos
->core
.node
);
134 evlist
->core
.nr_entries
= 0;
137 void evlist__exit(struct evlist
*evlist
)
139 zfree(&evlist
->mmap
);
140 zfree(&evlist
->overwrite_mmap
);
141 fdarray__exit(&evlist
->core
.pollfd
);
144 void evlist__delete(struct evlist
*evlist
)
149 evlist__munmap(evlist
);
150 evlist__close(evlist
);
151 perf_cpu_map__put(evlist
->core
.cpus
);
152 perf_thread_map__put(evlist
->core
.threads
);
153 evlist
->core
.cpus
= NULL
;
154 evlist
->core
.threads
= NULL
;
155 evlist__purge(evlist
);
156 evlist__exit(evlist
);
160 void evlist__add(struct evlist
*evlist
, struct evsel
*entry
)
162 entry
->evlist
= evlist
;
163 entry
->idx
= evlist
->core
.nr_entries
;
164 entry
->tracking
= !entry
->idx
;
166 perf_evlist__add(&evlist
->core
, &entry
->core
);
168 if (evlist
->core
.nr_entries
== 1)
169 perf_evlist__set_id_pos(evlist
);
172 void evlist__remove(struct evlist
*evlist
, struct evsel
*evsel
)
174 evsel
->evlist
= NULL
;
175 perf_evlist__remove(&evlist
->core
, &evsel
->core
);
178 void perf_evlist__splice_list_tail(struct evlist
*evlist
,
179 struct list_head
*list
)
181 struct evsel
*evsel
, *temp
;
183 __evlist__for_each_entry_safe(list
, temp
, evsel
) {
184 list_del_init(&evsel
->core
.node
);
185 evlist__add(evlist
, evsel
);
189 void __perf_evlist__set_leader(struct list_head
*list
)
191 struct evsel
*evsel
, *leader
;
193 leader
= list_entry(list
->next
, struct evsel
, core
.node
);
194 evsel
= list_entry(list
->prev
, struct evsel
, core
.node
);
196 leader
->core
.nr_members
= evsel
->idx
- leader
->idx
+ 1;
198 __evlist__for_each_entry(list
, evsel
) {
199 evsel
->leader
= leader
;
203 void perf_evlist__set_leader(struct evlist
*evlist
)
205 if (evlist
->core
.nr_entries
) {
206 evlist
->nr_groups
= evlist
->core
.nr_entries
> 1 ? 1 : 0;
207 __perf_evlist__set_leader(&evlist
->core
.entries
);
211 int __perf_evlist__add_default(struct evlist
*evlist
, bool precise
)
213 struct evsel
*evsel
= perf_evsel__new_cycles(precise
);
218 evlist__add(evlist
, evsel
);
222 int perf_evlist__add_dummy(struct evlist
*evlist
)
224 struct perf_event_attr attr
= {
225 .type
= PERF_TYPE_SOFTWARE
,
226 .config
= PERF_COUNT_SW_DUMMY
,
227 .size
= sizeof(attr
), /* to capture ABI version */
229 struct evsel
*evsel
= perf_evsel__new_idx(&attr
, evlist
->core
.nr_entries
);
234 evlist__add(evlist
, evsel
);
238 static int evlist__add_attrs(struct evlist
*evlist
,
239 struct perf_event_attr
*attrs
, size_t nr_attrs
)
241 struct evsel
*evsel
, *n
;
245 for (i
= 0; i
< nr_attrs
; i
++) {
246 evsel
= perf_evsel__new_idx(attrs
+ i
, evlist
->core
.nr_entries
+ i
);
248 goto out_delete_partial_list
;
249 list_add_tail(&evsel
->core
.node
, &head
);
252 perf_evlist__splice_list_tail(evlist
, &head
);
256 out_delete_partial_list
:
257 __evlist__for_each_entry_safe(&head
, n
, evsel
)
258 evsel__delete(evsel
);
262 int __perf_evlist__add_default_attrs(struct evlist
*evlist
,
263 struct perf_event_attr
*attrs
, size_t nr_attrs
)
267 for (i
= 0; i
< nr_attrs
; i
++)
268 event_attr_init(attrs
+ i
);
270 return evlist__add_attrs(evlist
, attrs
, nr_attrs
);
274 perf_evlist__find_tracepoint_by_id(struct evlist
*evlist
, int id
)
278 evlist__for_each_entry(evlist
, evsel
) {
279 if (evsel
->core
.attr
.type
== PERF_TYPE_TRACEPOINT
&&
280 (int)evsel
->core
.attr
.config
== id
)
288 perf_evlist__find_tracepoint_by_name(struct evlist
*evlist
,
293 evlist__for_each_entry(evlist
, evsel
) {
294 if ((evsel
->core
.attr
.type
== PERF_TYPE_TRACEPOINT
) &&
295 (strcmp(evsel
->name
, name
) == 0))
302 int perf_evlist__add_newtp(struct evlist
*evlist
,
303 const char *sys
, const char *name
, void *handler
)
305 struct evsel
*evsel
= perf_evsel__newtp(sys
, name
);
310 evsel
->handler
= handler
;
311 evlist__add(evlist
, evsel
);
315 static int perf_evlist__nr_threads(struct evlist
*evlist
,
318 if (evsel
->core
.system_wide
)
321 return perf_thread_map__nr(evlist
->core
.threads
);
324 void evlist__disable(struct evlist
*evlist
)
328 evlist__for_each_entry(evlist
, pos
) {
329 if (pos
->disabled
|| !perf_evsel__is_group_leader(pos
) || !pos
->core
.fd
)
334 evlist
->enabled
= false;
337 void evlist__enable(struct evlist
*evlist
)
341 evlist__for_each_entry(evlist
, pos
) {
342 if (!perf_evsel__is_group_leader(pos
) || !pos
->core
.fd
)
347 evlist
->enabled
= true;
350 void perf_evlist__toggle_enable(struct evlist
*evlist
)
352 (evlist
->enabled
? evlist__disable
: evlist__enable
)(evlist
);
355 static int perf_evlist__enable_event_cpu(struct evlist
*evlist
,
356 struct evsel
*evsel
, int cpu
)
359 int nr_threads
= perf_evlist__nr_threads(evlist
, evsel
);
364 for (thread
= 0; thread
< nr_threads
; thread
++) {
365 int err
= ioctl(FD(evsel
, cpu
, thread
), PERF_EVENT_IOC_ENABLE
, 0);
372 static int perf_evlist__enable_event_thread(struct evlist
*evlist
,
377 int nr_cpus
= perf_cpu_map__nr(evlist
->core
.cpus
);
382 for (cpu
= 0; cpu
< nr_cpus
; cpu
++) {
383 int err
= ioctl(FD(evsel
, cpu
, thread
), PERF_EVENT_IOC_ENABLE
, 0);
390 int perf_evlist__enable_event_idx(struct evlist
*evlist
,
391 struct evsel
*evsel
, int idx
)
393 bool per_cpu_mmaps
= !perf_cpu_map__empty(evlist
->core
.cpus
);
396 return perf_evlist__enable_event_cpu(evlist
, evsel
, idx
);
398 return perf_evlist__enable_event_thread(evlist
, evsel
, idx
);
401 int evlist__add_pollfd(struct evlist
*evlist
, int fd
)
403 return perf_evlist__add_pollfd(&evlist
->core
, fd
, NULL
, POLLIN
);
406 static void perf_evlist__munmap_filtered(struct fdarray
*fda
, int fd
,
407 void *arg __maybe_unused
)
409 struct mmap
*map
= fda
->priv
[fd
].ptr
;
415 int evlist__filter_pollfd(struct evlist
*evlist
, short revents_and_mask
)
417 return fdarray__filter(&evlist
->core
.pollfd
, revents_and_mask
,
418 perf_evlist__munmap_filtered
, NULL
);
421 int evlist__poll(struct evlist
*evlist
, int timeout
)
423 return perf_evlist__poll(&evlist
->core
, timeout
);
426 static void perf_evlist__set_sid_idx(struct evlist
*evlist
,
427 struct evsel
*evsel
, int idx
, int cpu
,
430 struct perf_sample_id
*sid
= SID(evsel
, cpu
, thread
);
432 if (evlist
->core
.cpus
&& cpu
>= 0)
433 sid
->cpu
= evlist
->core
.cpus
->map
[cpu
];
436 if (!evsel
->core
.system_wide
&& evlist
->core
.threads
&& thread
>= 0)
437 sid
->tid
= perf_thread_map__pid(evlist
->core
.threads
, thread
);
442 struct perf_sample_id
*perf_evlist__id2sid(struct evlist
*evlist
, u64 id
)
444 struct hlist_head
*head
;
445 struct perf_sample_id
*sid
;
448 hash
= hash_64(id
, PERF_EVLIST__HLIST_BITS
);
449 head
= &evlist
->core
.heads
[hash
];
451 hlist_for_each_entry(sid
, head
, node
)
458 struct evsel
*perf_evlist__id2evsel(struct evlist
*evlist
, u64 id
)
460 struct perf_sample_id
*sid
;
462 if (evlist
->core
.nr_entries
== 1 || !id
)
463 return evlist__first(evlist
);
465 sid
= perf_evlist__id2sid(evlist
, id
);
467 return container_of(sid
->evsel
, struct evsel
, core
);
469 if (!perf_evlist__sample_id_all(evlist
))
470 return evlist__first(evlist
);
475 struct evsel
*perf_evlist__id2evsel_strict(struct evlist
*evlist
,
478 struct perf_sample_id
*sid
;
483 sid
= perf_evlist__id2sid(evlist
, id
);
485 return container_of(sid
->evsel
, struct evsel
, core
);
490 static int perf_evlist__event2id(struct evlist
*evlist
,
491 union perf_event
*event
, u64
*id
)
493 const __u64
*array
= event
->sample
.array
;
496 n
= (event
->header
.size
- sizeof(event
->header
)) >> 3;
498 if (event
->header
.type
== PERF_RECORD_SAMPLE
) {
499 if (evlist
->id_pos
>= n
)
501 *id
= array
[evlist
->id_pos
];
503 if (evlist
->is_pos
> n
)
511 struct evsel
*perf_evlist__event2evsel(struct evlist
*evlist
,
512 union perf_event
*event
)
514 struct evsel
*first
= evlist__first(evlist
);
515 struct hlist_head
*head
;
516 struct perf_sample_id
*sid
;
520 if (evlist
->core
.nr_entries
== 1)
523 if (!first
->core
.attr
.sample_id_all
&&
524 event
->header
.type
!= PERF_RECORD_SAMPLE
)
527 if (perf_evlist__event2id(evlist
, event
, &id
))
530 /* Synthesized events have an id of zero */
534 hash
= hash_64(id
, PERF_EVLIST__HLIST_BITS
);
535 head
= &evlist
->core
.heads
[hash
];
537 hlist_for_each_entry(sid
, head
, node
) {
539 return container_of(sid
->evsel
, struct evsel
, core
);
544 static int perf_evlist__set_paused(struct evlist
*evlist
, bool value
)
548 if (!evlist
->overwrite_mmap
)
551 for (i
= 0; i
< evlist
->core
.nr_mmaps
; i
++) {
552 int fd
= evlist
->overwrite_mmap
[i
].core
.fd
;
557 err
= ioctl(fd
, PERF_EVENT_IOC_PAUSE_OUTPUT
, value
? 1 : 0);
564 static int perf_evlist__pause(struct evlist
*evlist
)
566 return perf_evlist__set_paused(evlist
, true);
569 static int perf_evlist__resume(struct evlist
*evlist
)
571 return perf_evlist__set_paused(evlist
, false);
574 static void evlist__munmap_nofree(struct evlist
*evlist
)
579 for (i
= 0; i
< evlist
->core
.nr_mmaps
; i
++)
580 perf_mmap__munmap(&evlist
->mmap
[i
]);
582 if (evlist
->overwrite_mmap
)
583 for (i
= 0; i
< evlist
->core
.nr_mmaps
; i
++)
584 perf_mmap__munmap(&evlist
->overwrite_mmap
[i
]);
587 void evlist__munmap(struct evlist
*evlist
)
589 evlist__munmap_nofree(evlist
);
590 zfree(&evlist
->mmap
);
591 zfree(&evlist
->overwrite_mmap
);
594 static struct mmap
*evlist__alloc_mmap(struct evlist
*evlist
,
600 evlist
->core
.nr_mmaps
= perf_cpu_map__nr(evlist
->core
.cpus
);
601 if (perf_cpu_map__empty(evlist
->core
.cpus
))
602 evlist
->core
.nr_mmaps
= perf_thread_map__nr(evlist
->core
.threads
);
603 map
= zalloc(evlist
->core
.nr_mmaps
* sizeof(struct mmap
));
607 for (i
= 0; i
< evlist
->core
.nr_mmaps
; i
++) {
609 map
[i
].core
.overwrite
= overwrite
;
611 * When the perf_mmap() call is made we grab one refcount, plus
612 * one extra to let perf_mmap__consume() get the last
613 * events after all real references (perf_mmap__get()) are
616 * Each PERF_EVENT_IOC_SET_OUTPUT points to this mmap and
617 * thus does perf_mmap__get() on it.
619 refcount_set(&map
[i
].core
.refcnt
, 0);
625 perf_evlist__should_poll(struct evlist
*evlist __maybe_unused
,
628 if (evsel
->core
.attr
.write_backward
)
633 static int evlist__mmap_per_evsel(struct evlist
*evlist
, int idx
,
634 struct mmap_params
*mp
, int cpu_idx
,
635 int thread
, int *_output
, int *_output_overwrite
)
639 int evlist_cpu
= cpu_map__cpu(evlist
->core
.cpus
, cpu_idx
);
641 evlist__for_each_entry(evlist
, evsel
) {
642 struct mmap
*maps
= evlist
->mmap
;
643 int *output
= _output
;
647 mp
->prot
= PROT_READ
| PROT_WRITE
;
648 if (evsel
->core
.attr
.write_backward
) {
649 output
= _output_overwrite
;
650 maps
= evlist
->overwrite_mmap
;
653 maps
= evlist__alloc_mmap(evlist
, true);
656 evlist
->overwrite_mmap
= maps
;
657 if (evlist
->bkw_mmap_state
== BKW_MMAP_NOTREADY
)
658 perf_evlist__toggle_bkw_mmap(evlist
, BKW_MMAP_RUNNING
);
660 mp
->prot
&= ~PROT_WRITE
;
663 if (evsel
->core
.system_wide
&& thread
)
666 cpu
= perf_cpu_map__idx(evsel
->core
.cpus
, evlist_cpu
);
670 fd
= FD(evsel
, cpu
, thread
);
675 if (perf_mmap__mmap(&maps
[idx
], mp
, *output
, evlist_cpu
) < 0)
678 if (ioctl(fd
, PERF_EVENT_IOC_SET_OUTPUT
, *output
) != 0)
681 perf_mmap__get(&maps
[idx
]);
684 revent
= perf_evlist__should_poll(evlist
, evsel
) ? POLLIN
: 0;
687 * The system_wide flag causes a selected event to be opened
688 * always without a pid. Consequently it will never get a
689 * POLLHUP, but it is used for tracking in combination with
690 * other events, so it should not need to be polled anyway.
691 * Therefore don't add it for polling.
693 if (!evsel
->core
.system_wide
&&
694 perf_evlist__add_pollfd(&evlist
->core
, fd
, &maps
[idx
], revent
) < 0) {
695 perf_mmap__put(&maps
[idx
]);
699 if (evsel
->core
.attr
.read_format
& PERF_FORMAT_ID
) {
700 if (perf_evlist__id_add_fd(&evlist
->core
, &evsel
->core
, cpu
, thread
,
703 perf_evlist__set_sid_idx(evlist
, evsel
, idx
, cpu
,
711 static int evlist__mmap_per_cpu(struct evlist
*evlist
,
712 struct mmap_params
*mp
)
715 int nr_cpus
= perf_cpu_map__nr(evlist
->core
.cpus
);
716 int nr_threads
= perf_thread_map__nr(evlist
->core
.threads
);
718 pr_debug2("perf event ring buffer mmapped per cpu\n");
719 for (cpu
= 0; cpu
< nr_cpus
; cpu
++) {
721 int output_overwrite
= -1;
723 auxtrace_mmap_params__set_idx(&mp
->auxtrace_mp
, evlist
, cpu
,
726 for (thread
= 0; thread
< nr_threads
; thread
++) {
727 if (evlist__mmap_per_evsel(evlist
, cpu
, mp
, cpu
,
728 thread
, &output
, &output_overwrite
))
736 evlist__munmap_nofree(evlist
);
740 static int evlist__mmap_per_thread(struct evlist
*evlist
,
741 struct mmap_params
*mp
)
744 int nr_threads
= perf_thread_map__nr(evlist
->core
.threads
);
746 pr_debug2("perf event ring buffer mmapped per thread\n");
747 for (thread
= 0; thread
< nr_threads
; thread
++) {
749 int output_overwrite
= -1;
751 auxtrace_mmap_params__set_idx(&mp
->auxtrace_mp
, evlist
, thread
,
754 if (evlist__mmap_per_evsel(evlist
, thread
, mp
, 0, thread
,
755 &output
, &output_overwrite
))
762 evlist__munmap_nofree(evlist
);
766 unsigned long perf_event_mlock_kb_in_pages(void)
771 if (sysctl__read_int("kernel/perf_event_mlock_kb", &max
) < 0) {
773 * Pick a once upon a time good value, i.e. things look
774 * strange since we can't read a sysctl value, but lets not
779 max
-= (page_size
/ 1024);
782 pages
= (max
* 1024) / page_size
;
783 if (!is_power_of_2(pages
))
784 pages
= rounddown_pow_of_two(pages
);
789 size_t evlist__mmap_size(unsigned long pages
)
791 if (pages
== UINT_MAX
)
792 pages
= perf_event_mlock_kb_in_pages();
793 else if (!is_power_of_2(pages
))
796 return (pages
+ 1) * page_size
;
799 static long parse_pages_arg(const char *str
, unsigned long min
,
802 unsigned long pages
, val
;
803 static struct parse_tag tags
[] = {
804 { .tag
= 'B', .mult
= 1 },
805 { .tag
= 'K', .mult
= 1 << 10 },
806 { .tag
= 'M', .mult
= 1 << 20 },
807 { .tag
= 'G', .mult
= 1 << 30 },
814 val
= parse_tag_value(str
, tags
);
815 if (val
!= (unsigned long) -1) {
816 /* we got file size value */
817 pages
= PERF_ALIGN(val
, page_size
) / page_size
;
819 /* we got pages count value */
821 pages
= strtoul(str
, &eptr
, 10);
826 if (pages
== 0 && min
== 0) {
827 /* leave number of pages at 0 */
828 } else if (!is_power_of_2(pages
)) {
831 /* round pages up to next power of 2 */
832 pages
= roundup_pow_of_two(pages
);
836 unit_number__scnprintf(buf
, sizeof(buf
), pages
* page_size
);
837 pr_info("rounding mmap pages size to %s (%lu pages)\n",
847 int __perf_evlist__parse_mmap_pages(unsigned int *mmap_pages
, const char *str
)
849 unsigned long max
= UINT_MAX
;
852 if (max
> SIZE_MAX
/ page_size
)
853 max
= SIZE_MAX
/ page_size
;
855 pages
= parse_pages_arg(str
, 1, max
);
857 pr_err("Invalid argument for --mmap_pages/-m\n");
865 int perf_evlist__parse_mmap_pages(const struct option
*opt
, const char *str
,
866 int unset __maybe_unused
)
868 return __perf_evlist__parse_mmap_pages(opt
->value
, str
);
872 * evlist__mmap_ex - Create mmaps to receive events.
873 * @evlist: list of events
874 * @pages: map length in pages
875 * @overwrite: overwrite older events?
876 * @auxtrace_pages - auxtrace map length in pages
877 * @auxtrace_overwrite - overwrite older auxtrace data?
879 * If @overwrite is %false the user needs to signal event consumption using
880 * perf_mmap__write_tail(). Using evlist__mmap_read() does this
883 * Similarly, if @auxtrace_overwrite is %false the user needs to signal data
884 * consumption using auxtrace_mmap__write_tail().
886 * Return: %0 on success, negative error code otherwise.
888 int evlist__mmap_ex(struct evlist
*evlist
, unsigned int pages
,
889 unsigned int auxtrace_pages
,
890 bool auxtrace_overwrite
, int nr_cblocks
, int affinity
, int flush
,
894 const struct perf_cpu_map
*cpus
= evlist
->core
.cpus
;
895 const struct perf_thread_map
*threads
= evlist
->core
.threads
;
897 * Delay setting mp.prot: set it before calling perf_mmap__mmap.
898 * Its value is decided by evsel's write_backward.
899 * So &mp should not be passed through const pointer.
901 struct mmap_params mp
= { .nr_cblocks
= nr_cblocks
, .affinity
= affinity
, .flush
= flush
,
902 .comp_level
= comp_level
};
905 evlist
->mmap
= evlist__alloc_mmap(evlist
, false);
909 if (evlist
->core
.pollfd
.entries
== NULL
&& perf_evlist__alloc_pollfd(&evlist
->core
) < 0)
912 evlist
->core
.mmap_len
= evlist__mmap_size(pages
);
913 pr_debug("mmap size %zuB\n", evlist
->core
.mmap_len
);
914 mp
.mask
= evlist
->core
.mmap_len
- page_size
- 1;
916 auxtrace_mmap_params__init(&mp
.auxtrace_mp
, evlist
->core
.mmap_len
,
917 auxtrace_pages
, auxtrace_overwrite
);
919 evlist__for_each_entry(evlist
, evsel
) {
920 if ((evsel
->core
.attr
.read_format
& PERF_FORMAT_ID
) &&
921 evsel
->core
.sample_id
== NULL
&&
922 perf_evsel__alloc_id(&evsel
->core
, perf_cpu_map__nr(cpus
), threads
->nr
) < 0)
926 if (perf_cpu_map__empty(cpus
))
927 return evlist__mmap_per_thread(evlist
, &mp
);
929 return evlist__mmap_per_cpu(evlist
, &mp
);
932 int evlist__mmap(struct evlist
*evlist
, unsigned int pages
)
934 return evlist__mmap_ex(evlist
, pages
, 0, false, 0, PERF_AFFINITY_SYS
, 1, 0);
937 int perf_evlist__create_maps(struct evlist
*evlist
, struct target
*target
)
939 bool all_threads
= (target
->per_thread
&& target
->system_wide
);
940 struct perf_cpu_map
*cpus
;
941 struct perf_thread_map
*threads
;
944 * If specify '-a' and '--per-thread' to perf record, perf record
945 * will override '--per-thread'. target->per_thread = false and
946 * target->system_wide = true.
948 * If specify '--per-thread' only to perf record,
949 * target->per_thread = true and target->system_wide = false.
951 * So target->per_thread && target->system_wide is false.
952 * For perf record, thread_map__new_str doesn't call
953 * thread_map__new_all_cpus. That will keep perf record's
956 * For perf stat, it allows the case that target->per_thread and
957 * target->system_wide are all true. It means to collect system-wide
958 * per-thread data. thread_map__new_str will call
959 * thread_map__new_all_cpus to enumerate all threads.
961 threads
= thread_map__new_str(target
->pid
, target
->tid
, target
->uid
,
967 if (target__uses_dummy_map(target
))
968 cpus
= perf_cpu_map__dummy_new();
970 cpus
= perf_cpu_map__new(target
->cpu_list
);
973 goto out_delete_threads
;
975 evlist
->core
.has_user_cpus
= !!target
->cpu_list
;
977 perf_evlist__set_maps(&evlist
->core
, cpus
, threads
);
982 perf_thread_map__put(threads
);
986 void __perf_evlist__set_sample_bit(struct evlist
*evlist
,
987 enum perf_event_sample_format bit
)
991 evlist__for_each_entry(evlist
, evsel
)
992 __perf_evsel__set_sample_bit(evsel
, bit
);
995 void __perf_evlist__reset_sample_bit(struct evlist
*evlist
,
996 enum perf_event_sample_format bit
)
1000 evlist__for_each_entry(evlist
, evsel
)
1001 __perf_evsel__reset_sample_bit(evsel
, bit
);
1004 int perf_evlist__apply_filters(struct evlist
*evlist
, struct evsel
**err_evsel
)
1006 struct evsel
*evsel
;
1009 evlist__for_each_entry(evlist
, evsel
) {
1010 if (evsel
->filter
== NULL
)
1014 * filters only work for tracepoint event, which doesn't have cpu limit.
1015 * So evlist and evsel should always be same.
1017 err
= perf_evsel__apply_filter(&evsel
->core
, evsel
->filter
);
1027 int perf_evlist__set_tp_filter(struct evlist
*evlist
, const char *filter
)
1029 struct evsel
*evsel
;
1032 evlist__for_each_entry(evlist
, evsel
) {
1033 if (evsel
->core
.attr
.type
!= PERF_TYPE_TRACEPOINT
)
1036 err
= perf_evsel__set_filter(evsel
, filter
);
1044 int perf_evlist__set_tp_filter_pids(struct evlist
*evlist
, size_t npids
, pid_t
*pids
)
1050 for (i
= 0; i
< npids
; ++i
) {
1052 if (asprintf(&filter
, "common_pid != %d", pids
[i
]) < 0)
1057 if (asprintf(&tmp
, "%s && common_pid != %d", filter
, pids
[i
]) < 0)
1065 ret
= perf_evlist__set_tp_filter(evlist
, filter
);
1071 int perf_evlist__set_tp_filter_pid(struct evlist
*evlist
, pid_t pid
)
1073 return perf_evlist__set_tp_filter_pids(evlist
, 1, &pid
);
1076 bool perf_evlist__valid_sample_type(struct evlist
*evlist
)
1080 if (evlist
->core
.nr_entries
== 1)
1083 if (evlist
->id_pos
< 0 || evlist
->is_pos
< 0)
1086 evlist__for_each_entry(evlist
, pos
) {
1087 if (pos
->id_pos
!= evlist
->id_pos
||
1088 pos
->is_pos
!= evlist
->is_pos
)
1095 u64
__perf_evlist__combined_sample_type(struct evlist
*evlist
)
1097 struct evsel
*evsel
;
1099 if (evlist
->combined_sample_type
)
1100 return evlist
->combined_sample_type
;
1102 evlist__for_each_entry(evlist
, evsel
)
1103 evlist
->combined_sample_type
|= evsel
->core
.attr
.sample_type
;
1105 return evlist
->combined_sample_type
;
1108 u64
perf_evlist__combined_sample_type(struct evlist
*evlist
)
1110 evlist
->combined_sample_type
= 0;
1111 return __perf_evlist__combined_sample_type(evlist
);
1114 u64
perf_evlist__combined_branch_type(struct evlist
*evlist
)
1116 struct evsel
*evsel
;
1117 u64 branch_type
= 0;
1119 evlist__for_each_entry(evlist
, evsel
)
1120 branch_type
|= evsel
->core
.attr
.branch_sample_type
;
1124 bool perf_evlist__valid_read_format(struct evlist
*evlist
)
1126 struct evsel
*first
= evlist__first(evlist
), *pos
= first
;
1127 u64 read_format
= first
->core
.attr
.read_format
;
1128 u64 sample_type
= first
->core
.attr
.sample_type
;
1130 evlist__for_each_entry(evlist
, pos
) {
1131 if (read_format
!= pos
->core
.attr
.read_format
)
1135 /* PERF_SAMPLE_READ imples PERF_FORMAT_ID. */
1136 if ((sample_type
& PERF_SAMPLE_READ
) &&
1137 !(read_format
& PERF_FORMAT_ID
)) {
1144 u16
perf_evlist__id_hdr_size(struct evlist
*evlist
)
1146 struct evsel
*first
= evlist__first(evlist
);
1147 struct perf_sample
*data
;
1151 if (!first
->core
.attr
.sample_id_all
)
1154 sample_type
= first
->core
.attr
.sample_type
;
1156 if (sample_type
& PERF_SAMPLE_TID
)
1157 size
+= sizeof(data
->tid
) * 2;
1159 if (sample_type
& PERF_SAMPLE_TIME
)
1160 size
+= sizeof(data
->time
);
1162 if (sample_type
& PERF_SAMPLE_ID
)
1163 size
+= sizeof(data
->id
);
1165 if (sample_type
& PERF_SAMPLE_STREAM_ID
)
1166 size
+= sizeof(data
->stream_id
);
1168 if (sample_type
& PERF_SAMPLE_CPU
)
1169 size
+= sizeof(data
->cpu
) * 2;
1171 if (sample_type
& PERF_SAMPLE_IDENTIFIER
)
1172 size
+= sizeof(data
->id
);
1177 bool perf_evlist__valid_sample_id_all(struct evlist
*evlist
)
1179 struct evsel
*first
= evlist__first(evlist
), *pos
= first
;
1181 evlist__for_each_entry_continue(evlist
, pos
) {
1182 if (first
->core
.attr
.sample_id_all
!= pos
->core
.attr
.sample_id_all
)
1189 bool perf_evlist__sample_id_all(struct evlist
*evlist
)
1191 struct evsel
*first
= evlist__first(evlist
);
1192 return first
->core
.attr
.sample_id_all
;
1195 void perf_evlist__set_selected(struct evlist
*evlist
,
1196 struct evsel
*evsel
)
1198 evlist
->selected
= evsel
;
1201 void evlist__close(struct evlist
*evlist
)
1203 struct evsel
*evsel
;
1205 evlist__for_each_entry_reverse(evlist
, evsel
)
1206 evsel__close(evsel
);
1209 static int perf_evlist__create_syswide_maps(struct evlist
*evlist
)
1211 struct perf_cpu_map
*cpus
;
1212 struct perf_thread_map
*threads
;
1216 * Try reading /sys/devices/system/cpu/online to get
1219 * FIXME: -ENOMEM is the best we can do here, the cpu_map
1220 * code needs an overhaul to properly forward the
1221 * error, and we may not want to do that fallback to a
1222 * default cpu identity map :-\
1224 cpus
= perf_cpu_map__new(NULL
);
1228 threads
= perf_thread_map__new_dummy();
1232 perf_evlist__set_maps(&evlist
->core
, cpus
, threads
);
1236 perf_cpu_map__put(cpus
);
1240 int evlist__open(struct evlist
*evlist
)
1242 struct evsel
*evsel
;
1246 * Default: one fd per CPU, all threads, aka systemwide
1247 * as sys_perf_event_open(cpu = -1, thread = -1) is EINVAL
1249 if (evlist
->core
.threads
== NULL
&& evlist
->core
.cpus
== NULL
) {
1250 err
= perf_evlist__create_syswide_maps(evlist
);
1255 perf_evlist__update_id_pos(evlist
);
1257 evlist__for_each_entry(evlist
, evsel
) {
1258 err
= evsel__open(evsel
, evsel
->core
.cpus
, evsel
->core
.threads
);
1265 evlist__close(evlist
);
1270 int perf_evlist__prepare_workload(struct evlist
*evlist
, struct target
*target
,
1271 const char *argv
[], bool pipe_output
,
1272 void (*exec_error
)(int signo
, siginfo_t
*info
, void *ucontext
))
1274 int child_ready_pipe
[2], go_pipe
[2];
1277 if (pipe(child_ready_pipe
) < 0) {
1278 perror("failed to create 'ready' pipe");
1282 if (pipe(go_pipe
) < 0) {
1283 perror("failed to create 'go' pipe");
1284 goto out_close_ready_pipe
;
1287 evlist
->workload
.pid
= fork();
1288 if (evlist
->workload
.pid
< 0) {
1289 perror("failed to fork");
1290 goto out_close_pipes
;
1293 if (!evlist
->workload
.pid
) {
1299 signal(SIGTERM
, SIG_DFL
);
1301 close(child_ready_pipe
[0]);
1303 fcntl(go_pipe
[0], F_SETFD
, FD_CLOEXEC
);
1306 * Tell the parent we're ready to go
1308 close(child_ready_pipe
[1]);
1311 * Wait until the parent tells us to go.
1313 ret
= read(go_pipe
[0], &bf
, 1);
1315 * The parent will ask for the execvp() to be performed by
1316 * writing exactly one byte, in workload.cork_fd, usually via
1317 * perf_evlist__start_workload().
1319 * For cancelling the workload without actually running it,
1320 * the parent will just close workload.cork_fd, without writing
1321 * anything, i.e. read will return zero and we just exit()
1326 perror("unable to read pipe");
1330 execvp(argv
[0], (char **)argv
);
1335 val
.sival_int
= errno
;
1336 if (sigqueue(getppid(), SIGUSR1
, val
))
1344 struct sigaction act
= {
1345 .sa_flags
= SA_SIGINFO
,
1346 .sa_sigaction
= exec_error
,
1348 sigaction(SIGUSR1
, &act
, NULL
);
1351 if (target__none(target
)) {
1352 if (evlist
->core
.threads
== NULL
) {
1353 fprintf(stderr
, "FATAL: evlist->threads need to be set at this point (%s:%d).\n",
1354 __func__
, __LINE__
);
1355 goto out_close_pipes
;
1357 perf_thread_map__set_pid(evlist
->core
.threads
, 0, evlist
->workload
.pid
);
1360 close(child_ready_pipe
[1]);
1363 * wait for child to settle
1365 if (read(child_ready_pipe
[0], &bf
, 1) == -1) {
1366 perror("unable to read pipe");
1367 goto out_close_pipes
;
1370 fcntl(go_pipe
[1], F_SETFD
, FD_CLOEXEC
);
1371 evlist
->workload
.cork_fd
= go_pipe
[1];
1372 close(child_ready_pipe
[0]);
1378 out_close_ready_pipe
:
1379 close(child_ready_pipe
[0]);
1380 close(child_ready_pipe
[1]);
1384 int perf_evlist__start_workload(struct evlist
*evlist
)
1386 if (evlist
->workload
.cork_fd
> 0) {
1390 * Remove the cork, let it rip!
1392 ret
= write(evlist
->workload
.cork_fd
, &bf
, 1);
1394 perror("unable to write to pipe");
1396 close(evlist
->workload
.cork_fd
);
1403 int perf_evlist__parse_sample(struct evlist
*evlist
, union perf_event
*event
,
1404 struct perf_sample
*sample
)
1406 struct evsel
*evsel
= perf_evlist__event2evsel(evlist
, event
);
1410 return perf_evsel__parse_sample(evsel
, event
, sample
);
1413 int perf_evlist__parse_sample_timestamp(struct evlist
*evlist
,
1414 union perf_event
*event
,
1417 struct evsel
*evsel
= perf_evlist__event2evsel(evlist
, event
);
1421 return perf_evsel__parse_sample_timestamp(evsel
, event
, timestamp
);
1424 int perf_evlist__strerror_open(struct evlist
*evlist
,
1425 int err
, char *buf
, size_t size
)
1428 char sbuf
[STRERR_BUFSIZE
], *emsg
= str_error_r(err
, sbuf
, sizeof(sbuf
));
1433 printed
= scnprintf(buf
, size
,
1435 "Hint:\tCheck /proc/sys/kernel/perf_event_paranoid setting.", emsg
);
1437 value
= perf_event_paranoid();
1439 printed
+= scnprintf(buf
+ printed
, size
- printed
, "\nHint:\t");
1442 printed
+= scnprintf(buf
+ printed
, size
- printed
,
1443 "For your workloads it needs to be <= 1\nHint:\t");
1445 printed
+= scnprintf(buf
+ printed
, size
- printed
,
1446 "For system wide tracing it needs to be set to -1.\n");
1448 printed
+= scnprintf(buf
+ printed
, size
- printed
,
1449 "Hint:\tTry: 'sudo sh -c \"echo -1 > /proc/sys/kernel/perf_event_paranoid\"'\n"
1450 "Hint:\tThe current value is %d.", value
);
1453 struct evsel
*first
= evlist__first(evlist
);
1456 if (sysctl__read_int("kernel/perf_event_max_sample_rate", &max_freq
) < 0)
1459 if (first
->core
.attr
.sample_freq
< (u64
)max_freq
)
1462 printed
= scnprintf(buf
, size
,
1464 "Hint:\tCheck /proc/sys/kernel/perf_event_max_sample_rate.\n"
1465 "Hint:\tThe current value is %d and %" PRIu64
" is being requested.",
1466 emsg
, max_freq
, first
->core
.attr
.sample_freq
);
1471 scnprintf(buf
, size
, "%s", emsg
);
1478 int perf_evlist__strerror_mmap(struct evlist
*evlist
, int err
, char *buf
, size_t size
)
1480 char sbuf
[STRERR_BUFSIZE
], *emsg
= str_error_r(err
, sbuf
, sizeof(sbuf
));
1481 int pages_attempted
= evlist
->core
.mmap_len
/ 1024, pages_max_per_user
, printed
= 0;
1485 sysctl__read_int("kernel/perf_event_mlock_kb", &pages_max_per_user
);
1486 printed
+= scnprintf(buf
+ printed
, size
- printed
,
1488 "Hint:\tCheck /proc/sys/kernel/perf_event_mlock_kb (%d kB) setting.\n"
1489 "Hint:\tTried using %zd kB.\n",
1490 emsg
, pages_max_per_user
, pages_attempted
);
1492 if (pages_attempted
>= pages_max_per_user
) {
1493 printed
+= scnprintf(buf
+ printed
, size
- printed
,
1494 "Hint:\tTry 'sudo sh -c \"echo %d > /proc/sys/kernel/perf_event_mlock_kb\"', or\n",
1495 pages_max_per_user
+ pages_attempted
);
1498 printed
+= scnprintf(buf
+ printed
, size
- printed
,
1499 "Hint:\tTry using a smaller -m/--mmap-pages value.");
1502 scnprintf(buf
, size
, "%s", emsg
);
1509 void perf_evlist__to_front(struct evlist
*evlist
,
1510 struct evsel
*move_evsel
)
1512 struct evsel
*evsel
, *n
;
1515 if (move_evsel
== evlist__first(evlist
))
1518 evlist__for_each_entry_safe(evlist
, n
, evsel
) {
1519 if (evsel
->leader
== move_evsel
->leader
)
1520 list_move_tail(&evsel
->core
.node
, &move
);
1523 list_splice(&move
, &evlist
->core
.entries
);
1526 void perf_evlist__set_tracking_event(struct evlist
*evlist
,
1527 struct evsel
*tracking_evsel
)
1529 struct evsel
*evsel
;
1531 if (tracking_evsel
->tracking
)
1534 evlist__for_each_entry(evlist
, evsel
) {
1535 if (evsel
!= tracking_evsel
)
1536 evsel
->tracking
= false;
1539 tracking_evsel
->tracking
= true;
1543 perf_evlist__find_evsel_by_str(struct evlist
*evlist
,
1546 struct evsel
*evsel
;
1548 evlist__for_each_entry(evlist
, evsel
) {
1551 if (strcmp(str
, evsel
->name
) == 0)
1558 void perf_evlist__toggle_bkw_mmap(struct evlist
*evlist
,
1559 enum bkw_mmap_state state
)
1561 enum bkw_mmap_state old_state
= evlist
->bkw_mmap_state
;
1568 if (!evlist
->overwrite_mmap
)
1571 switch (old_state
) {
1572 case BKW_MMAP_NOTREADY
: {
1573 if (state
!= BKW_MMAP_RUNNING
)
1577 case BKW_MMAP_RUNNING
: {
1578 if (state
!= BKW_MMAP_DATA_PENDING
)
1583 case BKW_MMAP_DATA_PENDING
: {
1584 if (state
!= BKW_MMAP_EMPTY
)
1588 case BKW_MMAP_EMPTY
: {
1589 if (state
!= BKW_MMAP_RUNNING
)
1595 WARN_ONCE(1, "Shouldn't get there\n");
1598 evlist
->bkw_mmap_state
= state
;
1602 perf_evlist__pause(evlist
);
1605 perf_evlist__resume(evlist
);
1616 bool perf_evlist__exclude_kernel(struct evlist
*evlist
)
1618 struct evsel
*evsel
;
1620 evlist__for_each_entry(evlist
, evsel
) {
1621 if (!evsel
->core
.attr
.exclude_kernel
)
1629 * Events in data file are not collect in groups, but we still want
1630 * the group display. Set the artificial group and set the leader's
1631 * forced_leader flag to notify the display code.
1633 void perf_evlist__force_leader(struct evlist
*evlist
)
1635 if (!evlist
->nr_groups
) {
1636 struct evsel
*leader
= evlist__first(evlist
);
1638 perf_evlist__set_leader(evlist
);
1639 leader
->forced_leader
= true;
1643 struct evsel
*perf_evlist__reset_weak_group(struct evlist
*evsel_list
,
1644 struct evsel
*evsel
)
1646 struct evsel
*c2
, *leader
;
1647 bool is_open
= true;
1649 leader
= evsel
->leader
;
1650 pr_debug("Weak group for %s/%d failed\n",
1651 leader
->name
, leader
->core
.nr_members
);
1654 * for_each_group_member doesn't work here because it doesn't
1655 * include the first entry.
1657 evlist__for_each_entry(evsel_list
, c2
) {
1660 if (c2
->leader
== leader
) {
1662 perf_evsel__close(&c2
->core
);
1664 c2
->core
.nr_members
= 0;
1670 int perf_evlist__add_sb_event(struct evlist
**evlist
,
1671 struct perf_event_attr
*attr
,
1672 perf_evsel__sb_cb_t cb
,
1675 struct evsel
*evsel
;
1676 bool new_evlist
= (*evlist
) == NULL
;
1678 if (*evlist
== NULL
)
1679 *evlist
= evlist__new();
1680 if (*evlist
== NULL
)
1683 if (!attr
->sample_id_all
) {
1684 pr_warning("enabling sample_id_all for all side band events\n");
1685 attr
->sample_id_all
= 1;
1688 evsel
= perf_evsel__new_idx(attr
, (*evlist
)->core
.nr_entries
);
1692 evsel
->side_band
.cb
= cb
;
1693 evsel
->side_band
.data
= data
;
1694 evlist__add(*evlist
, evsel
);
1699 evlist__delete(*evlist
);
1705 static void *perf_evlist__poll_thread(void *arg
)
1707 struct evlist
*evlist
= arg
;
1708 bool draining
= false;
1711 * In order to read symbols from other namespaces perf to needs to call
1712 * setns(2). This isn't permitted if the struct_fs has multiple users.
1713 * unshare(2) the fs so that we may continue to setns into namespaces
1714 * that we're observing when, for instance, reading the build-ids at
1715 * the end of a 'perf record' session.
1720 bool got_data
= false;
1722 if (evlist
->thread
.done
)
1726 evlist__poll(evlist
, 1000);
1728 for (i
= 0; i
< evlist
->core
.nr_mmaps
; i
++) {
1729 struct mmap
*map
= &evlist
->mmap
[i
];
1730 union perf_event
*event
;
1732 if (perf_mmap__read_init(map
))
1734 while ((event
= perf_mmap__read_event(map
)) != NULL
) {
1735 struct evsel
*evsel
= perf_evlist__event2evsel(evlist
, event
);
1737 if (evsel
&& evsel
->side_band
.cb
)
1738 evsel
->side_band
.cb(event
, evsel
->side_band
.data
);
1740 pr_warning("cannot locate proper evsel for the side band event\n");
1742 perf_mmap__consume(map
);
1745 perf_mmap__read_done(map
);
1748 if (draining
&& !got_data
)
1754 int perf_evlist__start_sb_thread(struct evlist
*evlist
,
1755 struct target
*target
)
1757 struct evsel
*counter
;
1762 if (perf_evlist__create_maps(evlist
, target
))
1763 goto out_delete_evlist
;
1765 evlist__for_each_entry(evlist
, counter
) {
1766 if (evsel__open(counter
, evlist
->core
.cpus
,
1767 evlist
->core
.threads
) < 0)
1768 goto out_delete_evlist
;
1771 if (evlist__mmap(evlist
, UINT_MAX
))
1772 goto out_delete_evlist
;
1774 evlist__for_each_entry(evlist
, counter
) {
1775 if (evsel__enable(counter
))
1776 goto out_delete_evlist
;
1779 evlist
->thread
.done
= 0;
1780 if (pthread_create(&evlist
->thread
.th
, NULL
, perf_evlist__poll_thread
, evlist
))
1781 goto out_delete_evlist
;
1786 evlist__delete(evlist
);
1791 void perf_evlist__stop_sb_thread(struct evlist
*evlist
)
1795 evlist
->thread
.done
= 1;
1796 pthread_join(evlist
->thread
.th
, NULL
);
1797 evlist__delete(evlist
);