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
12 #include <linux/bitops.h>
13 #include <api/fs/fs.h>
14 #include <api/fs/tracing_path.h>
15 #include <traceevent/event-parse.h>
16 #include <linux/hw_breakpoint.h>
17 #include <linux/perf_event.h>
18 #include <linux/compiler.h>
19 #include <linux/err.h>
20 #include <linux/zalloc.h>
21 #include <sys/ioctl.h>
22 #include <sys/resource.h>
23 #include <sys/types.h>
26 #include "callchain.h"
32 #include "thread_map.h"
34 #include "perf_regs.h"
36 #include "trace-event.h"
40 #include "util/parse-branch-options.h"
42 #include <linux/ctype.h>
44 struct perf_missing_features perf_missing_features
;
46 static clockid_t clockid
;
48 static int perf_evsel__no_extra_init(struct evsel
*evsel __maybe_unused
)
53 void __weak
test_attr__ready(void) { }
55 static void perf_evsel__no_extra_fini(struct evsel
*evsel __maybe_unused
)
61 int (*init
)(struct evsel
*evsel
);
62 void (*fini
)(struct evsel
*evsel
);
63 } perf_evsel__object
= {
64 .size
= sizeof(struct evsel
),
65 .init
= perf_evsel__no_extra_init
,
66 .fini
= perf_evsel__no_extra_fini
,
69 int perf_evsel__object_config(size_t object_size
,
70 int (*init
)(struct evsel
*evsel
),
71 void (*fini
)(struct evsel
*evsel
))
77 if (perf_evsel__object
.size
> object_size
)
80 perf_evsel__object
.size
= object_size
;
84 perf_evsel__object
.init
= init
;
87 perf_evsel__object
.fini
= fini
;
92 #define FD(e, x, y) (*(int *)xyarray__entry(e->fd, x, y))
94 int __perf_evsel__sample_size(u64 sample_type
)
96 u64 mask
= sample_type
& PERF_SAMPLE_MASK
;
100 for (i
= 0; i
< 64; i
++) {
101 if (mask
& (1ULL << i
))
111 * __perf_evsel__calc_id_pos - calculate id_pos.
112 * @sample_type: sample type
114 * This function returns the position of the event id (PERF_SAMPLE_ID or
115 * PERF_SAMPLE_IDENTIFIER) in a sample event i.e. in the array of struct
118 static int __perf_evsel__calc_id_pos(u64 sample_type
)
122 if (sample_type
& PERF_SAMPLE_IDENTIFIER
)
125 if (!(sample_type
& PERF_SAMPLE_ID
))
128 if (sample_type
& PERF_SAMPLE_IP
)
131 if (sample_type
& PERF_SAMPLE_TID
)
134 if (sample_type
& PERF_SAMPLE_TIME
)
137 if (sample_type
& PERF_SAMPLE_ADDR
)
144 * __perf_evsel__calc_is_pos - calculate is_pos.
145 * @sample_type: sample type
147 * This function returns the position (counting backwards) of the event id
148 * (PERF_SAMPLE_ID or PERF_SAMPLE_IDENTIFIER) in a non-sample event i.e. if
149 * sample_id_all is used there is an id sample appended to non-sample events.
151 static int __perf_evsel__calc_is_pos(u64 sample_type
)
155 if (sample_type
& PERF_SAMPLE_IDENTIFIER
)
158 if (!(sample_type
& PERF_SAMPLE_ID
))
161 if (sample_type
& PERF_SAMPLE_CPU
)
164 if (sample_type
& PERF_SAMPLE_STREAM_ID
)
170 void perf_evsel__calc_id_pos(struct evsel
*evsel
)
172 evsel
->id_pos
= __perf_evsel__calc_id_pos(evsel
->attr
.sample_type
);
173 evsel
->is_pos
= __perf_evsel__calc_is_pos(evsel
->attr
.sample_type
);
176 void __perf_evsel__set_sample_bit(struct evsel
*evsel
,
177 enum perf_event_sample_format bit
)
179 if (!(evsel
->attr
.sample_type
& bit
)) {
180 evsel
->attr
.sample_type
|= bit
;
181 evsel
->sample_size
+= sizeof(u64
);
182 perf_evsel__calc_id_pos(evsel
);
186 void __perf_evsel__reset_sample_bit(struct evsel
*evsel
,
187 enum perf_event_sample_format bit
)
189 if (evsel
->attr
.sample_type
& bit
) {
190 evsel
->attr
.sample_type
&= ~bit
;
191 evsel
->sample_size
-= sizeof(u64
);
192 perf_evsel__calc_id_pos(evsel
);
196 void perf_evsel__set_sample_id(struct evsel
*evsel
,
197 bool can_sample_identifier
)
199 if (can_sample_identifier
) {
200 perf_evsel__reset_sample_bit(evsel
, ID
);
201 perf_evsel__set_sample_bit(evsel
, IDENTIFIER
);
203 perf_evsel__set_sample_bit(evsel
, ID
);
205 evsel
->attr
.read_format
|= PERF_FORMAT_ID
;
209 * perf_evsel__is_function_event - Return whether given evsel is a function
212 * @evsel - evsel selector to be tested
214 * Return %true if event is function trace event
216 bool perf_evsel__is_function_event(struct evsel
*evsel
)
218 #define FUNCTION_EVENT "ftrace:function"
220 return evsel
->name
&&
221 !strncmp(FUNCTION_EVENT
, evsel
->name
, sizeof(FUNCTION_EVENT
));
223 #undef FUNCTION_EVENT
226 void evsel__init(struct evsel
*evsel
,
227 struct perf_event_attr
*attr
, int idx
)
230 evsel
->tracking
= !idx
;
232 evsel
->leader
= evsel
;
235 evsel
->max_events
= ULONG_MAX
;
236 evsel
->evlist
= NULL
;
237 evsel
->bpf_obj
= NULL
;
239 INIT_LIST_HEAD(&evsel
->node
);
240 INIT_LIST_HEAD(&evsel
->config_terms
);
241 perf_evsel__object
.init(evsel
);
242 evsel
->sample_size
= __perf_evsel__sample_size(attr
->sample_type
);
243 perf_evsel__calc_id_pos(evsel
);
244 evsel
->cmdline_group_boundary
= false;
245 evsel
->metric_expr
= NULL
;
246 evsel
->metric_name
= NULL
;
247 evsel
->metric_events
= NULL
;
248 evsel
->collect_stat
= false;
249 evsel
->pmu_name
= NULL
;
252 struct evsel
*perf_evsel__new_idx(struct perf_event_attr
*attr
, int idx
)
254 struct evsel
*evsel
= zalloc(perf_evsel__object
.size
);
258 evsel__init(evsel
, attr
, idx
);
260 if (perf_evsel__is_bpf_output(evsel
)) {
261 evsel
->attr
.sample_type
|= (PERF_SAMPLE_RAW
| PERF_SAMPLE_TIME
|
262 PERF_SAMPLE_CPU
| PERF_SAMPLE_PERIOD
),
263 evsel
->attr
.sample_period
= 1;
266 if (perf_evsel__is_clock(evsel
)) {
268 * The evsel->unit points to static alias->unit
269 * so it's ok to use static string in here.
271 static const char *unit
= "msec";
280 static bool perf_event_can_profile_kernel(void)
282 return geteuid() == 0 || perf_event_paranoid() == -1;
285 struct evsel
*perf_evsel__new_cycles(bool precise
)
287 struct perf_event_attr attr
= {
288 .type
= PERF_TYPE_HARDWARE
,
289 .config
= PERF_COUNT_HW_CPU_CYCLES
,
290 .exclude_kernel
= !perf_event_can_profile_kernel(),
294 event_attr_init(&attr
);
300 * Now let the usual logic to set up the perf_event_attr defaults
301 * to kick in when we return and before perf_evsel__open() is called.
304 evsel
= evsel__new(&attr
);
308 evsel
->precise_max
= true;
310 /* use asprintf() because free(evsel) assumes name is allocated */
311 if (asprintf(&evsel
->name
, "cycles%s%s%.*s",
312 (attr
.precise_ip
|| attr
.exclude_kernel
) ? ":" : "",
313 attr
.exclude_kernel
? "u" : "",
314 attr
.precise_ip
? attr
.precise_ip
+ 1 : 0, "ppp") < 0)
319 evsel__delete(evsel
);
325 * Returns pointer with encoded error via <linux/err.h> interface.
327 struct evsel
*perf_evsel__newtp_idx(const char *sys
, const char *name
, int idx
)
329 struct evsel
*evsel
= zalloc(perf_evsel__object
.size
);
335 struct perf_event_attr attr
= {
336 .type
= PERF_TYPE_TRACEPOINT
,
337 .sample_type
= (PERF_SAMPLE_RAW
| PERF_SAMPLE_TIME
|
338 PERF_SAMPLE_CPU
| PERF_SAMPLE_PERIOD
),
341 if (asprintf(&evsel
->name
, "%s:%s", sys
, name
) < 0)
344 evsel
->tp_format
= trace_event__tp_format(sys
, name
);
345 if (IS_ERR(evsel
->tp_format
)) {
346 err
= PTR_ERR(evsel
->tp_format
);
350 event_attr_init(&attr
);
351 attr
.config
= evsel
->tp_format
->id
;
352 attr
.sample_period
= 1;
353 evsel__init(evsel
, &attr
, idx
);
365 const char *perf_evsel__hw_names
[PERF_COUNT_HW_MAX
] = {
373 "stalled-cycles-frontend",
374 "stalled-cycles-backend",
378 static const char *__perf_evsel__hw_name(u64 config
)
380 if (config
< PERF_COUNT_HW_MAX
&& perf_evsel__hw_names
[config
])
381 return perf_evsel__hw_names
[config
];
383 return "unknown-hardware";
386 static int perf_evsel__add_modifiers(struct evsel
*evsel
, char *bf
, size_t size
)
388 int colon
= 0, r
= 0;
389 struct perf_event_attr
*attr
= &evsel
->attr
;
390 bool exclude_guest_default
= false;
392 #define MOD_PRINT(context, mod) do { \
393 if (!attr->exclude_##context) { \
394 if (!colon) colon = ++r; \
395 r += scnprintf(bf + r, size - r, "%c", mod); \
398 if (attr
->exclude_kernel
|| attr
->exclude_user
|| attr
->exclude_hv
) {
399 MOD_PRINT(kernel
, 'k');
400 MOD_PRINT(user
, 'u');
402 exclude_guest_default
= true;
405 if (attr
->precise_ip
) {
408 r
+= scnprintf(bf
+ r
, size
- r
, "%.*s", attr
->precise_ip
, "ppp");
409 exclude_guest_default
= true;
412 if (attr
->exclude_host
|| attr
->exclude_guest
== exclude_guest_default
) {
413 MOD_PRINT(host
, 'H');
414 MOD_PRINT(guest
, 'G');
422 static int perf_evsel__hw_name(struct evsel
*evsel
, char *bf
, size_t size
)
424 int r
= scnprintf(bf
, size
, "%s", __perf_evsel__hw_name(evsel
->attr
.config
));
425 return r
+ perf_evsel__add_modifiers(evsel
, bf
+ r
, size
- r
);
428 const char *perf_evsel__sw_names
[PERF_COUNT_SW_MAX
] = {
441 static const char *__perf_evsel__sw_name(u64 config
)
443 if (config
< PERF_COUNT_SW_MAX
&& perf_evsel__sw_names
[config
])
444 return perf_evsel__sw_names
[config
];
445 return "unknown-software";
448 static int perf_evsel__sw_name(struct evsel
*evsel
, char *bf
, size_t size
)
450 int r
= scnprintf(bf
, size
, "%s", __perf_evsel__sw_name(evsel
->attr
.config
));
451 return r
+ perf_evsel__add_modifiers(evsel
, bf
+ r
, size
- r
);
454 static int __perf_evsel__bp_name(char *bf
, size_t size
, u64 addr
, u64 type
)
458 r
= scnprintf(bf
, size
, "mem:0x%" PRIx64
":", addr
);
460 if (type
& HW_BREAKPOINT_R
)
461 r
+= scnprintf(bf
+ r
, size
- r
, "r");
463 if (type
& HW_BREAKPOINT_W
)
464 r
+= scnprintf(bf
+ r
, size
- r
, "w");
466 if (type
& HW_BREAKPOINT_X
)
467 r
+= scnprintf(bf
+ r
, size
- r
, "x");
472 static int perf_evsel__bp_name(struct evsel
*evsel
, char *bf
, size_t size
)
474 struct perf_event_attr
*attr
= &evsel
->attr
;
475 int r
= __perf_evsel__bp_name(bf
, size
, attr
->bp_addr
, attr
->bp_type
);
476 return r
+ perf_evsel__add_modifiers(evsel
, bf
+ r
, size
- r
);
479 const char *perf_evsel__hw_cache
[PERF_COUNT_HW_CACHE_MAX
]
480 [PERF_EVSEL__MAX_ALIASES
] = {
481 { "L1-dcache", "l1-d", "l1d", "L1-data", },
482 { "L1-icache", "l1-i", "l1i", "L1-instruction", },
484 { "dTLB", "d-tlb", "Data-TLB", },
485 { "iTLB", "i-tlb", "Instruction-TLB", },
486 { "branch", "branches", "bpu", "btb", "bpc", },
490 const char *perf_evsel__hw_cache_op
[PERF_COUNT_HW_CACHE_OP_MAX
]
491 [PERF_EVSEL__MAX_ALIASES
] = {
492 { "load", "loads", "read", },
493 { "store", "stores", "write", },
494 { "prefetch", "prefetches", "speculative-read", "speculative-load", },
497 const char *perf_evsel__hw_cache_result
[PERF_COUNT_HW_CACHE_RESULT_MAX
]
498 [PERF_EVSEL__MAX_ALIASES
] = {
499 { "refs", "Reference", "ops", "access", },
500 { "misses", "miss", },
503 #define C(x) PERF_COUNT_HW_CACHE_##x
504 #define CACHE_READ (1 << C(OP_READ))
505 #define CACHE_WRITE (1 << C(OP_WRITE))
506 #define CACHE_PREFETCH (1 << C(OP_PREFETCH))
507 #define COP(x) (1 << x)
510 * cache operartion stat
511 * L1I : Read and prefetch only
512 * ITLB and BPU : Read-only
514 static unsigned long perf_evsel__hw_cache_stat
[C(MAX
)] = {
515 [C(L1D
)] = (CACHE_READ
| CACHE_WRITE
| CACHE_PREFETCH
),
516 [C(L1I
)] = (CACHE_READ
| CACHE_PREFETCH
),
517 [C(LL
)] = (CACHE_READ
| CACHE_WRITE
| CACHE_PREFETCH
),
518 [C(DTLB
)] = (CACHE_READ
| CACHE_WRITE
| CACHE_PREFETCH
),
519 [C(ITLB
)] = (CACHE_READ
),
520 [C(BPU
)] = (CACHE_READ
),
521 [C(NODE
)] = (CACHE_READ
| CACHE_WRITE
| CACHE_PREFETCH
),
524 bool perf_evsel__is_cache_op_valid(u8 type
, u8 op
)
526 if (perf_evsel__hw_cache_stat
[type
] & COP(op
))
527 return true; /* valid */
529 return false; /* invalid */
532 int __perf_evsel__hw_cache_type_op_res_name(u8 type
, u8 op
, u8 result
,
533 char *bf
, size_t size
)
536 return scnprintf(bf
, size
, "%s-%s-%s", perf_evsel__hw_cache
[type
][0],
537 perf_evsel__hw_cache_op
[op
][0],
538 perf_evsel__hw_cache_result
[result
][0]);
541 return scnprintf(bf
, size
, "%s-%s", perf_evsel__hw_cache
[type
][0],
542 perf_evsel__hw_cache_op
[op
][1]);
545 static int __perf_evsel__hw_cache_name(u64 config
, char *bf
, size_t size
)
547 u8 op
, result
, type
= (config
>> 0) & 0xff;
548 const char *err
= "unknown-ext-hardware-cache-type";
550 if (type
>= PERF_COUNT_HW_CACHE_MAX
)
553 op
= (config
>> 8) & 0xff;
554 err
= "unknown-ext-hardware-cache-op";
555 if (op
>= PERF_COUNT_HW_CACHE_OP_MAX
)
558 result
= (config
>> 16) & 0xff;
559 err
= "unknown-ext-hardware-cache-result";
560 if (result
>= PERF_COUNT_HW_CACHE_RESULT_MAX
)
563 err
= "invalid-cache";
564 if (!perf_evsel__is_cache_op_valid(type
, op
))
567 return __perf_evsel__hw_cache_type_op_res_name(type
, op
, result
, bf
, size
);
569 return scnprintf(bf
, size
, "%s", err
);
572 static int perf_evsel__hw_cache_name(struct evsel
*evsel
, char *bf
, size_t size
)
574 int ret
= __perf_evsel__hw_cache_name(evsel
->attr
.config
, bf
, size
);
575 return ret
+ perf_evsel__add_modifiers(evsel
, bf
+ ret
, size
- ret
);
578 static int perf_evsel__raw_name(struct evsel
*evsel
, char *bf
, size_t size
)
580 int ret
= scnprintf(bf
, size
, "raw 0x%" PRIx64
, evsel
->attr
.config
);
581 return ret
+ perf_evsel__add_modifiers(evsel
, bf
+ ret
, size
- ret
);
584 static int perf_evsel__tool_name(char *bf
, size_t size
)
586 int ret
= scnprintf(bf
, size
, "duration_time");
590 const char *perf_evsel__name(struct evsel
*evsel
)
600 switch (evsel
->attr
.type
) {
602 perf_evsel__raw_name(evsel
, bf
, sizeof(bf
));
605 case PERF_TYPE_HARDWARE
:
606 perf_evsel__hw_name(evsel
, bf
, sizeof(bf
));
609 case PERF_TYPE_HW_CACHE
:
610 perf_evsel__hw_cache_name(evsel
, bf
, sizeof(bf
));
613 case PERF_TYPE_SOFTWARE
:
614 if (evsel
->tool_event
)
615 perf_evsel__tool_name(bf
, sizeof(bf
));
617 perf_evsel__sw_name(evsel
, bf
, sizeof(bf
));
620 case PERF_TYPE_TRACEPOINT
:
621 scnprintf(bf
, sizeof(bf
), "%s", "unknown tracepoint");
624 case PERF_TYPE_BREAKPOINT
:
625 perf_evsel__bp_name(evsel
, bf
, sizeof(bf
));
629 scnprintf(bf
, sizeof(bf
), "unknown attr type: %d",
634 evsel
->name
= strdup(bf
);
642 const char *perf_evsel__group_name(struct evsel
*evsel
)
644 return evsel
->group_name
?: "anon group";
648 * Returns the group details for the specified leader,
649 * with following rules.
651 * For record -e '{cycles,instructions}'
652 * 'anon group { cycles:u, instructions:u }'
654 * For record -e 'cycles,instructions' and report --group
655 * 'cycles:u, instructions:u'
657 int perf_evsel__group_desc(struct evsel
*evsel
, char *buf
, size_t size
)
661 const char *group_name
= perf_evsel__group_name(evsel
);
663 if (!evsel
->forced_leader
)
664 ret
= scnprintf(buf
, size
, "%s { ", group_name
);
666 ret
+= scnprintf(buf
+ ret
, size
- ret
, "%s",
667 perf_evsel__name(evsel
));
669 for_each_group_member(pos
, evsel
)
670 ret
+= scnprintf(buf
+ ret
, size
- ret
, ", %s",
671 perf_evsel__name(pos
));
673 if (!evsel
->forced_leader
)
674 ret
+= scnprintf(buf
+ ret
, size
- ret
, " }");
679 static void __perf_evsel__config_callchain(struct evsel
*evsel
,
680 struct record_opts
*opts
,
681 struct callchain_param
*param
)
683 bool function
= perf_evsel__is_function_event(evsel
);
684 struct perf_event_attr
*attr
= &evsel
->attr
;
686 perf_evsel__set_sample_bit(evsel
, CALLCHAIN
);
688 attr
->sample_max_stack
= param
->max_stack
;
690 if (opts
->kernel_callchains
)
691 attr
->exclude_callchain_user
= 1;
692 if (opts
->user_callchains
)
693 attr
->exclude_callchain_kernel
= 1;
694 if (param
->record_mode
== CALLCHAIN_LBR
) {
695 if (!opts
->branch_stack
) {
696 if (attr
->exclude_user
) {
697 pr_warning("LBR callstack option is only available "
698 "to get user callchain information. "
699 "Falling back to framepointers.\n");
701 perf_evsel__set_sample_bit(evsel
, BRANCH_STACK
);
702 attr
->branch_sample_type
= PERF_SAMPLE_BRANCH_USER
|
703 PERF_SAMPLE_BRANCH_CALL_STACK
|
704 PERF_SAMPLE_BRANCH_NO_CYCLES
|
705 PERF_SAMPLE_BRANCH_NO_FLAGS
;
708 pr_warning("Cannot use LBR callstack with branch stack. "
709 "Falling back to framepointers.\n");
712 if (param
->record_mode
== CALLCHAIN_DWARF
) {
714 perf_evsel__set_sample_bit(evsel
, REGS_USER
);
715 perf_evsel__set_sample_bit(evsel
, STACK_USER
);
716 if (opts
->sample_user_regs
&& DWARF_MINIMAL_REGS
!= PERF_REGS_MASK
) {
717 attr
->sample_regs_user
|= DWARF_MINIMAL_REGS
;
718 pr_warning("WARNING: The use of --call-graph=dwarf may require all the user registers, "
719 "specifying a subset with --user-regs may render DWARF unwinding unreliable, "
720 "so the minimal registers set (IP, SP) is explicitly forced.\n");
722 attr
->sample_regs_user
|= PERF_REGS_MASK
;
724 attr
->sample_stack_user
= param
->dump_size
;
725 attr
->exclude_callchain_user
= 1;
727 pr_info("Cannot use DWARF unwind for function trace event,"
728 " falling back to framepointers.\n");
733 pr_info("Disabling user space callchains for function trace event.\n");
734 attr
->exclude_callchain_user
= 1;
738 void perf_evsel__config_callchain(struct evsel
*evsel
,
739 struct record_opts
*opts
,
740 struct callchain_param
*param
)
743 return __perf_evsel__config_callchain(evsel
, opts
, param
);
747 perf_evsel__reset_callgraph(struct evsel
*evsel
,
748 struct callchain_param
*param
)
750 struct perf_event_attr
*attr
= &evsel
->attr
;
752 perf_evsel__reset_sample_bit(evsel
, CALLCHAIN
);
753 if (param
->record_mode
== CALLCHAIN_LBR
) {
754 perf_evsel__reset_sample_bit(evsel
, BRANCH_STACK
);
755 attr
->branch_sample_type
&= ~(PERF_SAMPLE_BRANCH_USER
|
756 PERF_SAMPLE_BRANCH_CALL_STACK
);
758 if (param
->record_mode
== CALLCHAIN_DWARF
) {
759 perf_evsel__reset_sample_bit(evsel
, REGS_USER
);
760 perf_evsel__reset_sample_bit(evsel
, STACK_USER
);
764 static void apply_config_terms(struct evsel
*evsel
,
765 struct record_opts
*opts
, bool track
)
767 struct perf_evsel_config_term
*term
;
768 struct list_head
*config_terms
= &evsel
->config_terms
;
769 struct perf_event_attr
*attr
= &evsel
->attr
;
770 /* callgraph default */
771 struct callchain_param param
= {
772 .record_mode
= callchain_param
.record_mode
,
776 const char *callgraph_buf
= NULL
;
778 list_for_each_entry(term
, config_terms
, list
) {
779 switch (term
->type
) {
780 case PERF_EVSEL__CONFIG_TERM_PERIOD
:
781 if (!(term
->weak
&& opts
->user_interval
!= ULLONG_MAX
)) {
782 attr
->sample_period
= term
->val
.period
;
784 perf_evsel__reset_sample_bit(evsel
, PERIOD
);
787 case PERF_EVSEL__CONFIG_TERM_FREQ
:
788 if (!(term
->weak
&& opts
->user_freq
!= UINT_MAX
)) {
789 attr
->sample_freq
= term
->val
.freq
;
791 perf_evsel__set_sample_bit(evsel
, PERIOD
);
794 case PERF_EVSEL__CONFIG_TERM_TIME
:
796 perf_evsel__set_sample_bit(evsel
, TIME
);
798 perf_evsel__reset_sample_bit(evsel
, TIME
);
800 case PERF_EVSEL__CONFIG_TERM_CALLGRAPH
:
801 callgraph_buf
= term
->val
.callgraph
;
803 case PERF_EVSEL__CONFIG_TERM_BRANCH
:
804 if (term
->val
.branch
&& strcmp(term
->val
.branch
, "no")) {
805 perf_evsel__set_sample_bit(evsel
, BRANCH_STACK
);
806 parse_branch_str(term
->val
.branch
,
807 &attr
->branch_sample_type
);
809 perf_evsel__reset_sample_bit(evsel
, BRANCH_STACK
);
811 case PERF_EVSEL__CONFIG_TERM_STACK_USER
:
812 dump_size
= term
->val
.stack_user
;
814 case PERF_EVSEL__CONFIG_TERM_MAX_STACK
:
815 max_stack
= term
->val
.max_stack
;
817 case PERF_EVSEL__CONFIG_TERM_MAX_EVENTS
:
818 evsel
->max_events
= term
->val
.max_events
;
820 case PERF_EVSEL__CONFIG_TERM_INHERIT
:
822 * attr->inherit should has already been set by
823 * perf_evsel__config. If user explicitly set
824 * inherit using config terms, override global
825 * opt->no_inherit setting.
827 attr
->inherit
= term
->val
.inherit
? 1 : 0;
829 case PERF_EVSEL__CONFIG_TERM_OVERWRITE
:
830 attr
->write_backward
= term
->val
.overwrite
? 1 : 0;
832 case PERF_EVSEL__CONFIG_TERM_DRV_CFG
:
834 case PERF_EVSEL__CONFIG_TERM_PERCORE
:
841 /* User explicitly set per-event callgraph, clear the old setting and reset. */
842 if ((callgraph_buf
!= NULL
) || (dump_size
> 0) || max_stack
) {
843 bool sample_address
= false;
846 param
.max_stack
= max_stack
;
847 if (callgraph_buf
== NULL
)
848 callgraph_buf
= "fp";
851 /* parse callgraph parameters */
852 if (callgraph_buf
!= NULL
) {
853 if (!strcmp(callgraph_buf
, "no")) {
854 param
.enabled
= false;
855 param
.record_mode
= CALLCHAIN_NONE
;
857 param
.enabled
= true;
858 if (parse_callchain_record(callgraph_buf
, ¶m
)) {
859 pr_err("per-event callgraph setting for %s failed. "
860 "Apply callgraph global setting for it\n",
864 if (param
.record_mode
== CALLCHAIN_DWARF
)
865 sample_address
= true;
869 dump_size
= round_up(dump_size
, sizeof(u64
));
870 param
.dump_size
= dump_size
;
873 /* If global callgraph set, clear it */
874 if (callchain_param
.enabled
)
875 perf_evsel__reset_callgraph(evsel
, &callchain_param
);
877 /* set perf-event callgraph */
879 if (sample_address
) {
880 perf_evsel__set_sample_bit(evsel
, ADDR
);
881 perf_evsel__set_sample_bit(evsel
, DATA_SRC
);
882 evsel
->attr
.mmap_data
= track
;
884 perf_evsel__config_callchain(evsel
, opts
, ¶m
);
889 static bool is_dummy_event(struct evsel
*evsel
)
891 return (evsel
->attr
.type
== PERF_TYPE_SOFTWARE
) &&
892 (evsel
->attr
.config
== PERF_COUNT_SW_DUMMY
);
896 * The enable_on_exec/disabled value strategy:
898 * 1) For any type of traced program:
899 * - all independent events and group leaders are disabled
900 * - all group members are enabled
902 * Group members are ruled by group leaders. They need to
903 * be enabled, because the group scheduling relies on that.
905 * 2) For traced programs executed by perf:
906 * - all independent events and group leaders have
908 * - we don't specifically enable or disable any event during
911 * Independent events and group leaders are initially disabled
912 * and get enabled by exec. Group members are ruled by group
913 * leaders as stated in 1).
915 * 3) For traced programs attached by perf (pid/tid):
916 * - we specifically enable or disable all events during
919 * When attaching events to already running traced we
920 * enable/disable events specifically, as there's no
921 * initial traced exec call.
923 void perf_evsel__config(struct evsel
*evsel
, struct record_opts
*opts
,
924 struct callchain_param
*callchain
)
926 struct evsel
*leader
= evsel
->leader
;
927 struct perf_event_attr
*attr
= &evsel
->attr
;
928 int track
= evsel
->tracking
;
929 bool per_cpu
= opts
->target
.default_per_cpu
&& !opts
->target
.per_thread
;
931 attr
->sample_id_all
= perf_missing_features
.sample_id_all
? 0 : 1;
932 attr
->inherit
= !opts
->no_inherit
;
933 attr
->write_backward
= opts
->overwrite
? 1 : 0;
935 perf_evsel__set_sample_bit(evsel
, IP
);
936 perf_evsel__set_sample_bit(evsel
, TID
);
938 if (evsel
->sample_read
) {
939 perf_evsel__set_sample_bit(evsel
, READ
);
942 * We need ID even in case of single event, because
943 * PERF_SAMPLE_READ process ID specific data.
945 perf_evsel__set_sample_id(evsel
, false);
948 * Apply group format only if we belong to group
949 * with more than one members.
951 if (leader
->nr_members
> 1) {
952 attr
->read_format
|= PERF_FORMAT_GROUP
;
958 * We default some events to have a default interval. But keep
959 * it a weak assumption overridable by the user.
961 if (!attr
->sample_period
|| (opts
->user_freq
!= UINT_MAX
||
962 opts
->user_interval
!= ULLONG_MAX
)) {
964 perf_evsel__set_sample_bit(evsel
, PERIOD
);
966 attr
->sample_freq
= opts
->freq
;
968 attr
->sample_period
= opts
->default_interval
;
973 * Disable sampling for all group members other
974 * than leader in case leader 'leads' the sampling.
976 if ((leader
!= evsel
) && leader
->sample_read
) {
978 attr
->sample_freq
= 0;
979 attr
->sample_period
= 0;
980 attr
->write_backward
= 0;
983 * We don't get sample for slave events, we make them
984 * when delivering group leader sample. Set the slave
985 * event to follow the master sample_type to ease up
988 attr
->sample_type
= leader
->attr
.sample_type
;
991 if (opts
->no_samples
)
992 attr
->sample_freq
= 0;
994 if (opts
->inherit_stat
) {
995 evsel
->attr
.read_format
|=
996 PERF_FORMAT_TOTAL_TIME_ENABLED
|
997 PERF_FORMAT_TOTAL_TIME_RUNNING
|
999 attr
->inherit_stat
= 1;
1002 if (opts
->sample_address
) {
1003 perf_evsel__set_sample_bit(evsel
, ADDR
);
1004 attr
->mmap_data
= track
;
1008 * We don't allow user space callchains for function trace
1009 * event, due to issues with page faults while tracing page
1010 * fault handler and its overall trickiness nature.
1012 if (perf_evsel__is_function_event(evsel
))
1013 evsel
->attr
.exclude_callchain_user
= 1;
1015 if (callchain
&& callchain
->enabled
&& !evsel
->no_aux_samples
)
1016 perf_evsel__config_callchain(evsel
, opts
, callchain
);
1018 if (opts
->sample_intr_regs
) {
1019 attr
->sample_regs_intr
= opts
->sample_intr_regs
;
1020 perf_evsel__set_sample_bit(evsel
, REGS_INTR
);
1023 if (opts
->sample_user_regs
) {
1024 attr
->sample_regs_user
|= opts
->sample_user_regs
;
1025 perf_evsel__set_sample_bit(evsel
, REGS_USER
);
1028 if (target__has_cpu(&opts
->target
) || opts
->sample_cpu
)
1029 perf_evsel__set_sample_bit(evsel
, CPU
);
1032 * When the user explicitly disabled time don't force it here.
1034 if (opts
->sample_time
&&
1035 (!perf_missing_features
.sample_id_all
&&
1036 (!opts
->no_inherit
|| target__has_cpu(&opts
->target
) || per_cpu
||
1037 opts
->sample_time_set
)))
1038 perf_evsel__set_sample_bit(evsel
, TIME
);
1040 if (opts
->raw_samples
&& !evsel
->no_aux_samples
) {
1041 perf_evsel__set_sample_bit(evsel
, TIME
);
1042 perf_evsel__set_sample_bit(evsel
, RAW
);
1043 perf_evsel__set_sample_bit(evsel
, CPU
);
1046 if (opts
->sample_address
)
1047 perf_evsel__set_sample_bit(evsel
, DATA_SRC
);
1049 if (opts
->sample_phys_addr
)
1050 perf_evsel__set_sample_bit(evsel
, PHYS_ADDR
);
1052 if (opts
->no_buffering
) {
1053 attr
->watermark
= 0;
1054 attr
->wakeup_events
= 1;
1056 if (opts
->branch_stack
&& !evsel
->no_aux_samples
) {
1057 perf_evsel__set_sample_bit(evsel
, BRANCH_STACK
);
1058 attr
->branch_sample_type
= opts
->branch_stack
;
1061 if (opts
->sample_weight
)
1062 perf_evsel__set_sample_bit(evsel
, WEIGHT
);
1066 attr
->mmap2
= track
&& !perf_missing_features
.mmap2
;
1068 attr
->ksymbol
= track
&& !perf_missing_features
.ksymbol
;
1069 attr
->bpf_event
= track
&& !opts
->no_bpf_event
&&
1070 !perf_missing_features
.bpf_event
;
1072 if (opts
->record_namespaces
)
1073 attr
->namespaces
= track
;
1075 if (opts
->record_switch_events
)
1076 attr
->context_switch
= track
;
1078 if (opts
->sample_transaction
)
1079 perf_evsel__set_sample_bit(evsel
, TRANSACTION
);
1081 if (opts
->running_time
) {
1082 evsel
->attr
.read_format
|=
1083 PERF_FORMAT_TOTAL_TIME_ENABLED
|
1084 PERF_FORMAT_TOTAL_TIME_RUNNING
;
1088 * XXX see the function comment above
1090 * Disabling only independent events or group leaders,
1091 * keeping group members enabled.
1093 if (perf_evsel__is_group_leader(evsel
))
1097 * Setting enable_on_exec for independent events and
1098 * group leaders for traced executed by perf.
1100 if (target__none(&opts
->target
) && perf_evsel__is_group_leader(evsel
) &&
1101 !opts
->initial_delay
)
1102 attr
->enable_on_exec
= 1;
1104 if (evsel
->immediate
) {
1106 attr
->enable_on_exec
= 0;
1109 clockid
= opts
->clockid
;
1110 if (opts
->use_clockid
) {
1111 attr
->use_clockid
= 1;
1112 attr
->clockid
= opts
->clockid
;
1115 if (evsel
->precise_max
)
1116 attr
->precise_ip
= 3;
1118 if (opts
->all_user
) {
1119 attr
->exclude_kernel
= 1;
1120 attr
->exclude_user
= 0;
1123 if (opts
->all_kernel
) {
1124 attr
->exclude_kernel
= 0;
1125 attr
->exclude_user
= 1;
1128 if (evsel
->own_cpus
|| evsel
->unit
)
1129 evsel
->attr
.read_format
|= PERF_FORMAT_ID
;
1132 * Apply event specific term settings,
1133 * it overloads any global configuration.
1135 apply_config_terms(evsel
, opts
, track
);
1137 evsel
->ignore_missing_thread
= opts
->ignore_missing_thread
;
1139 /* The --period option takes the precedence. */
1140 if (opts
->period_set
) {
1142 perf_evsel__set_sample_bit(evsel
, PERIOD
);
1144 perf_evsel__reset_sample_bit(evsel
, PERIOD
);
1148 * For initial_delay, a dummy event is added implicitly.
1149 * The software event will trigger -EOPNOTSUPP error out,
1150 * if BRANCH_STACK bit is set.
1152 if (opts
->initial_delay
&& is_dummy_event(evsel
))
1153 perf_evsel__reset_sample_bit(evsel
, BRANCH_STACK
);
1156 static int perf_evsel__alloc_fd(struct evsel
*evsel
, int ncpus
, int nthreads
)
1158 evsel
->fd
= xyarray__new(ncpus
, nthreads
, sizeof(int));
1162 for (cpu
= 0; cpu
< ncpus
; cpu
++) {
1163 for (thread
= 0; thread
< nthreads
; thread
++) {
1164 FD(evsel
, cpu
, thread
) = -1;
1169 return evsel
->fd
!= NULL
? 0 : -ENOMEM
;
1172 static int perf_evsel__run_ioctl(struct evsel
*evsel
,
1177 for (cpu
= 0; cpu
< xyarray__max_x(evsel
->fd
); cpu
++) {
1178 for (thread
= 0; thread
< xyarray__max_y(evsel
->fd
); thread
++) {
1179 int fd
= FD(evsel
, cpu
, thread
),
1180 err
= ioctl(fd
, ioc
, arg
);
1190 int evsel__apply_filter(struct evsel
*evsel
, const char *filter
)
1192 return perf_evsel__run_ioctl(evsel
,
1193 PERF_EVENT_IOC_SET_FILTER
,
1197 int perf_evsel__set_filter(struct evsel
*evsel
, const char *filter
)
1199 char *new_filter
= strdup(filter
);
1201 if (new_filter
!= NULL
) {
1202 free(evsel
->filter
);
1203 evsel
->filter
= new_filter
;
1210 static int perf_evsel__append_filter(struct evsel
*evsel
,
1211 const char *fmt
, const char *filter
)
1215 if (evsel
->filter
== NULL
)
1216 return perf_evsel__set_filter(evsel
, filter
);
1218 if (asprintf(&new_filter
, fmt
, evsel
->filter
, filter
) > 0) {
1219 free(evsel
->filter
);
1220 evsel
->filter
= new_filter
;
1227 int perf_evsel__append_tp_filter(struct evsel
*evsel
, const char *filter
)
1229 return perf_evsel__append_filter(evsel
, "(%s) && (%s)", filter
);
1232 int perf_evsel__append_addr_filter(struct evsel
*evsel
, const char *filter
)
1234 return perf_evsel__append_filter(evsel
, "%s,%s", filter
);
1237 int evsel__enable(struct evsel
*evsel
)
1239 int err
= perf_evsel__run_ioctl(evsel
, PERF_EVENT_IOC_ENABLE
, 0);
1242 evsel
->disabled
= false;
1247 int evsel__disable(struct evsel
*evsel
)
1249 int err
= perf_evsel__run_ioctl(evsel
, PERF_EVENT_IOC_DISABLE
, 0);
1251 * We mark it disabled here so that tools that disable a event can
1252 * ignore events after they disable it. I.e. the ring buffer may have
1253 * already a few more events queued up before the kernel got the stop
1257 evsel
->disabled
= true;
1262 int perf_evsel__alloc_id(struct evsel
*evsel
, int ncpus
, int nthreads
)
1264 if (ncpus
== 0 || nthreads
== 0)
1267 if (evsel
->system_wide
)
1270 evsel
->sample_id
= xyarray__new(ncpus
, nthreads
, sizeof(struct perf_sample_id
));
1271 if (evsel
->sample_id
== NULL
)
1274 evsel
->id
= zalloc(ncpus
* nthreads
* sizeof(u64
));
1275 if (evsel
->id
== NULL
) {
1276 xyarray__delete(evsel
->sample_id
);
1277 evsel
->sample_id
= NULL
;
1284 static void perf_evsel__free_fd(struct evsel
*evsel
)
1286 xyarray__delete(evsel
->fd
);
1290 static void perf_evsel__free_id(struct evsel
*evsel
)
1292 xyarray__delete(evsel
->sample_id
);
1293 evsel
->sample_id
= NULL
;
1298 static void perf_evsel__free_config_terms(struct evsel
*evsel
)
1300 struct perf_evsel_config_term
*term
, *h
;
1302 list_for_each_entry_safe(term
, h
, &evsel
->config_terms
, list
) {
1303 list_del_init(&term
->list
);
1308 void perf_evsel__close_fd(struct evsel
*evsel
)
1312 for (cpu
= 0; cpu
< xyarray__max_x(evsel
->fd
); cpu
++)
1313 for (thread
= 0; thread
< xyarray__max_y(evsel
->fd
); ++thread
) {
1314 close(FD(evsel
, cpu
, thread
));
1315 FD(evsel
, cpu
, thread
) = -1;
1319 void perf_evsel__exit(struct evsel
*evsel
)
1321 assert(list_empty(&evsel
->node
));
1322 assert(evsel
->evlist
== NULL
);
1323 perf_evsel__free_counts(evsel
);
1324 perf_evsel__free_fd(evsel
);
1325 perf_evsel__free_id(evsel
);
1326 perf_evsel__free_config_terms(evsel
);
1327 cgroup__put(evsel
->cgrp
);
1328 cpu_map__put(evsel
->cpus
);
1329 cpu_map__put(evsel
->own_cpus
);
1330 thread_map__put(evsel
->threads
);
1331 zfree(&evsel
->group_name
);
1332 zfree(&evsel
->name
);
1333 perf_evsel__object
.fini(evsel
);
1336 void evsel__delete(struct evsel
*evsel
)
1338 perf_evsel__exit(evsel
);
1342 void perf_evsel__compute_deltas(struct evsel
*evsel
, int cpu
, int thread
,
1343 struct perf_counts_values
*count
)
1345 struct perf_counts_values tmp
;
1347 if (!evsel
->prev_raw_counts
)
1351 tmp
= evsel
->prev_raw_counts
->aggr
;
1352 evsel
->prev_raw_counts
->aggr
= *count
;
1354 tmp
= *perf_counts(evsel
->prev_raw_counts
, cpu
, thread
);
1355 *perf_counts(evsel
->prev_raw_counts
, cpu
, thread
) = *count
;
1358 count
->val
= count
->val
- tmp
.val
;
1359 count
->ena
= count
->ena
- tmp
.ena
;
1360 count
->run
= count
->run
- tmp
.run
;
1363 void perf_counts_values__scale(struct perf_counts_values
*count
,
1364 bool scale
, s8
*pscaled
)
1369 if (count
->run
== 0) {
1372 } else if (count
->run
< count
->ena
) {
1374 count
->val
= (u64
)((double) count
->val
* count
->ena
/ count
->run
);
1382 static int perf_evsel__read_size(struct evsel
*evsel
)
1384 u64 read_format
= evsel
->attr
.read_format
;
1385 int entry
= sizeof(u64
); /* value */
1389 if (read_format
& PERF_FORMAT_TOTAL_TIME_ENABLED
)
1390 size
+= sizeof(u64
);
1392 if (read_format
& PERF_FORMAT_TOTAL_TIME_RUNNING
)
1393 size
+= sizeof(u64
);
1395 if (read_format
& PERF_FORMAT_ID
)
1396 entry
+= sizeof(u64
);
1398 if (read_format
& PERF_FORMAT_GROUP
) {
1399 nr
= evsel
->nr_members
;
1400 size
+= sizeof(u64
);
1407 int perf_evsel__read(struct evsel
*evsel
, int cpu
, int thread
,
1408 struct perf_counts_values
*count
)
1410 size_t size
= perf_evsel__read_size(evsel
);
1412 memset(count
, 0, sizeof(*count
));
1414 if (FD(evsel
, cpu
, thread
) < 0)
1417 if (readn(FD(evsel
, cpu
, thread
), count
->values
, size
) <= 0)
1424 perf_evsel__read_one(struct evsel
*evsel
, int cpu
, int thread
)
1426 struct perf_counts_values
*count
= perf_counts(evsel
->counts
, cpu
, thread
);
1428 return perf_evsel__read(evsel
, cpu
, thread
, count
);
1432 perf_evsel__set_count(struct evsel
*counter
, int cpu
, int thread
,
1433 u64 val
, u64 ena
, u64 run
)
1435 struct perf_counts_values
*count
;
1437 count
= perf_counts(counter
->counts
, cpu
, thread
);
1443 perf_counts__set_loaded(counter
->counts
, cpu
, thread
, true);
1447 perf_evsel__process_group_data(struct evsel
*leader
,
1448 int cpu
, int thread
, u64
*data
)
1450 u64 read_format
= leader
->attr
.read_format
;
1451 struct sample_read_value
*v
;
1452 u64 nr
, ena
= 0, run
= 0, i
;
1456 if (nr
!= (u64
) leader
->nr_members
)
1459 if (read_format
& PERF_FORMAT_TOTAL_TIME_ENABLED
)
1462 if (read_format
& PERF_FORMAT_TOTAL_TIME_RUNNING
)
1465 v
= (struct sample_read_value
*) data
;
1467 perf_evsel__set_count(leader
, cpu
, thread
,
1468 v
[0].value
, ena
, run
);
1470 for (i
= 1; i
< nr
; i
++) {
1471 struct evsel
*counter
;
1473 counter
= perf_evlist__id2evsel(leader
->evlist
, v
[i
].id
);
1477 perf_evsel__set_count(counter
, cpu
, thread
,
1478 v
[i
].value
, ena
, run
);
1485 perf_evsel__read_group(struct evsel
*leader
, int cpu
, int thread
)
1487 struct perf_stat_evsel
*ps
= leader
->stats
;
1488 u64 read_format
= leader
->attr
.read_format
;
1489 int size
= perf_evsel__read_size(leader
);
1490 u64
*data
= ps
->group_data
;
1492 if (!(read_format
& PERF_FORMAT_ID
))
1495 if (!perf_evsel__is_group_leader(leader
))
1499 data
= zalloc(size
);
1503 ps
->group_data
= data
;
1506 if (FD(leader
, cpu
, thread
) < 0)
1509 if (readn(FD(leader
, cpu
, thread
), data
, size
) <= 0)
1512 return perf_evsel__process_group_data(leader
, cpu
, thread
, data
);
1515 int perf_evsel__read_counter(struct evsel
*evsel
, int cpu
, int thread
)
1517 u64 read_format
= evsel
->attr
.read_format
;
1519 if (read_format
& PERF_FORMAT_GROUP
)
1520 return perf_evsel__read_group(evsel
, cpu
, thread
);
1522 return perf_evsel__read_one(evsel
, cpu
, thread
);
1525 int __perf_evsel__read_on_cpu(struct evsel
*evsel
,
1526 int cpu
, int thread
, bool scale
)
1528 struct perf_counts_values count
;
1529 size_t nv
= scale
? 3 : 1;
1531 if (FD(evsel
, cpu
, thread
) < 0)
1534 if (evsel
->counts
== NULL
&& perf_evsel__alloc_counts(evsel
, cpu
+ 1, thread
+ 1) < 0)
1537 if (readn(FD(evsel
, cpu
, thread
), &count
, nv
* sizeof(u64
)) <= 0)
1540 perf_evsel__compute_deltas(evsel
, cpu
, thread
, &count
);
1541 perf_counts_values__scale(&count
, scale
, NULL
);
1542 *perf_counts(evsel
->counts
, cpu
, thread
) = count
;
1546 static int get_group_fd(struct evsel
*evsel
, int cpu
, int thread
)
1548 struct evsel
*leader
= evsel
->leader
;
1551 if (perf_evsel__is_group_leader(evsel
))
1555 * Leader must be already processed/open,
1556 * if not it's a bug.
1558 BUG_ON(!leader
->fd
);
1560 fd
= FD(leader
, cpu
, thread
);
1571 static void __p_bits(char *buf
, size_t size
, u64 value
, struct bit_names
*bits
)
1573 bool first_bit
= true;
1577 if (value
& bits
[i
].bit
) {
1578 buf
+= scnprintf(buf
, size
, "%s%s", first_bit
? "" : "|", bits
[i
].name
);
1581 } while (bits
[++i
].name
!= NULL
);
1584 static void __p_sample_type(char *buf
, size_t size
, u64 value
)
1586 #define bit_name(n) { PERF_SAMPLE_##n, #n }
1587 struct bit_names bits
[] = {
1588 bit_name(IP
), bit_name(TID
), bit_name(TIME
), bit_name(ADDR
),
1589 bit_name(READ
), bit_name(CALLCHAIN
), bit_name(ID
), bit_name(CPU
),
1590 bit_name(PERIOD
), bit_name(STREAM_ID
), bit_name(RAW
),
1591 bit_name(BRANCH_STACK
), bit_name(REGS_USER
), bit_name(STACK_USER
),
1592 bit_name(IDENTIFIER
), bit_name(REGS_INTR
), bit_name(DATA_SRC
),
1593 bit_name(WEIGHT
), bit_name(PHYS_ADDR
),
1597 __p_bits(buf
, size
, value
, bits
);
1600 static void __p_branch_sample_type(char *buf
, size_t size
, u64 value
)
1602 #define bit_name(n) { PERF_SAMPLE_BRANCH_##n, #n }
1603 struct bit_names bits
[] = {
1604 bit_name(USER
), bit_name(KERNEL
), bit_name(HV
), bit_name(ANY
),
1605 bit_name(ANY_CALL
), bit_name(ANY_RETURN
), bit_name(IND_CALL
),
1606 bit_name(ABORT_TX
), bit_name(IN_TX
), bit_name(NO_TX
),
1607 bit_name(COND
), bit_name(CALL_STACK
), bit_name(IND_JUMP
),
1608 bit_name(CALL
), bit_name(NO_FLAGS
), bit_name(NO_CYCLES
),
1612 __p_bits(buf
, size
, value
, bits
);
1615 static void __p_read_format(char *buf
, size_t size
, u64 value
)
1617 #define bit_name(n) { PERF_FORMAT_##n, #n }
1618 struct bit_names bits
[] = {
1619 bit_name(TOTAL_TIME_ENABLED
), bit_name(TOTAL_TIME_RUNNING
),
1620 bit_name(ID
), bit_name(GROUP
),
1624 __p_bits(buf
, size
, value
, bits
);
1627 #define BUF_SIZE 1024
1629 #define p_hex(val) snprintf(buf, BUF_SIZE, "%#"PRIx64, (uint64_t)(val))
1630 #define p_unsigned(val) snprintf(buf, BUF_SIZE, "%"PRIu64, (uint64_t)(val))
1631 #define p_signed(val) snprintf(buf, BUF_SIZE, "%"PRId64, (int64_t)(val))
1632 #define p_sample_type(val) __p_sample_type(buf, BUF_SIZE, val)
1633 #define p_branch_sample_type(val) __p_branch_sample_type(buf, BUF_SIZE, val)
1634 #define p_read_format(val) __p_read_format(buf, BUF_SIZE, val)
1636 #define PRINT_ATTRn(_n, _f, _p) \
1640 ret += attr__fprintf(fp, _n, buf, priv);\
1644 #define PRINT_ATTRf(_f, _p) PRINT_ATTRn(#_f, _f, _p)
1646 int perf_event_attr__fprintf(FILE *fp
, struct perf_event_attr
*attr
,
1647 attr__fprintf_f attr__fprintf
, void *priv
)
1652 PRINT_ATTRf(type
, p_unsigned
);
1653 PRINT_ATTRf(size
, p_unsigned
);
1654 PRINT_ATTRf(config
, p_hex
);
1655 PRINT_ATTRn("{ sample_period, sample_freq }", sample_period
, p_unsigned
);
1656 PRINT_ATTRf(sample_type
, p_sample_type
);
1657 PRINT_ATTRf(read_format
, p_read_format
);
1659 PRINT_ATTRf(disabled
, p_unsigned
);
1660 PRINT_ATTRf(inherit
, p_unsigned
);
1661 PRINT_ATTRf(pinned
, p_unsigned
);
1662 PRINT_ATTRf(exclusive
, p_unsigned
);
1663 PRINT_ATTRf(exclude_user
, p_unsigned
);
1664 PRINT_ATTRf(exclude_kernel
, p_unsigned
);
1665 PRINT_ATTRf(exclude_hv
, p_unsigned
);
1666 PRINT_ATTRf(exclude_idle
, p_unsigned
);
1667 PRINT_ATTRf(mmap
, p_unsigned
);
1668 PRINT_ATTRf(comm
, p_unsigned
);
1669 PRINT_ATTRf(freq
, p_unsigned
);
1670 PRINT_ATTRf(inherit_stat
, p_unsigned
);
1671 PRINT_ATTRf(enable_on_exec
, p_unsigned
);
1672 PRINT_ATTRf(task
, p_unsigned
);
1673 PRINT_ATTRf(watermark
, p_unsigned
);
1674 PRINT_ATTRf(precise_ip
, p_unsigned
);
1675 PRINT_ATTRf(mmap_data
, p_unsigned
);
1676 PRINT_ATTRf(sample_id_all
, p_unsigned
);
1677 PRINT_ATTRf(exclude_host
, p_unsigned
);
1678 PRINT_ATTRf(exclude_guest
, p_unsigned
);
1679 PRINT_ATTRf(exclude_callchain_kernel
, p_unsigned
);
1680 PRINT_ATTRf(exclude_callchain_user
, p_unsigned
);
1681 PRINT_ATTRf(mmap2
, p_unsigned
);
1682 PRINT_ATTRf(comm_exec
, p_unsigned
);
1683 PRINT_ATTRf(use_clockid
, p_unsigned
);
1684 PRINT_ATTRf(context_switch
, p_unsigned
);
1685 PRINT_ATTRf(write_backward
, p_unsigned
);
1686 PRINT_ATTRf(namespaces
, p_unsigned
);
1687 PRINT_ATTRf(ksymbol
, p_unsigned
);
1688 PRINT_ATTRf(bpf_event
, p_unsigned
);
1690 PRINT_ATTRn("{ wakeup_events, wakeup_watermark }", wakeup_events
, p_unsigned
);
1691 PRINT_ATTRf(bp_type
, p_unsigned
);
1692 PRINT_ATTRn("{ bp_addr, config1 }", bp_addr
, p_hex
);
1693 PRINT_ATTRn("{ bp_len, config2 }", bp_len
, p_hex
);
1694 PRINT_ATTRf(branch_sample_type
, p_branch_sample_type
);
1695 PRINT_ATTRf(sample_regs_user
, p_hex
);
1696 PRINT_ATTRf(sample_stack_user
, p_unsigned
);
1697 PRINT_ATTRf(clockid
, p_signed
);
1698 PRINT_ATTRf(sample_regs_intr
, p_hex
);
1699 PRINT_ATTRf(aux_watermark
, p_unsigned
);
1700 PRINT_ATTRf(sample_max_stack
, p_unsigned
);
1705 static int __open_attr__fprintf(FILE *fp
, const char *name
, const char *val
,
1706 void *priv __maybe_unused
)
1708 return fprintf(fp
, " %-32s %s\n", name
, val
);
1711 static void perf_evsel__remove_fd(struct evsel
*pos
,
1712 int nr_cpus
, int nr_threads
,
1715 for (int cpu
= 0; cpu
< nr_cpus
; cpu
++)
1716 for (int thread
= thread_idx
; thread
< nr_threads
- 1; thread
++)
1717 FD(pos
, cpu
, thread
) = FD(pos
, cpu
, thread
+ 1);
1720 static int update_fds(struct evsel
*evsel
,
1721 int nr_cpus
, int cpu_idx
,
1722 int nr_threads
, int thread_idx
)
1726 if (cpu_idx
>= nr_cpus
|| thread_idx
>= nr_threads
)
1729 evlist__for_each_entry(evsel
->evlist
, pos
) {
1730 nr_cpus
= pos
!= evsel
? nr_cpus
: cpu_idx
;
1732 perf_evsel__remove_fd(pos
, nr_cpus
, nr_threads
, thread_idx
);
1735 * Since fds for next evsel has not been created,
1736 * there is no need to iterate whole event list.
1744 static bool ignore_missing_thread(struct evsel
*evsel
,
1745 int nr_cpus
, int cpu
,
1746 struct perf_thread_map
*threads
,
1747 int thread
, int err
)
1749 pid_t ignore_pid
= thread_map__pid(threads
, thread
);
1751 if (!evsel
->ignore_missing_thread
)
1754 /* The system wide setup does not work with threads. */
1755 if (evsel
->system_wide
)
1758 /* The -ESRCH is perf event syscall errno for pid's not found. */
1762 /* If there's only one thread, let it fail. */
1763 if (threads
->nr
== 1)
1767 * We should remove fd for missing_thread first
1768 * because thread_map__remove() will decrease threads->nr.
1770 if (update_fds(evsel
, nr_cpus
, cpu
, threads
->nr
, thread
))
1773 if (thread_map__remove(threads
, thread
))
1776 pr_warning("WARNING: Ignored open failure for pid %d\n",
1781 static void display_attr(struct perf_event_attr
*attr
)
1784 fprintf(stderr
, "%.60s\n", graph_dotted_line
);
1785 fprintf(stderr
, "perf_event_attr:\n");
1786 perf_event_attr__fprintf(stderr
, attr
, __open_attr__fprintf
, NULL
);
1787 fprintf(stderr
, "%.60s\n", graph_dotted_line
);
1791 static int perf_event_open(struct evsel
*evsel
,
1792 pid_t pid
, int cpu
, int group_fd
,
1793 unsigned long flags
)
1795 int precise_ip
= evsel
->attr
.precise_ip
;
1799 pr_debug2("sys_perf_event_open: pid %d cpu %d group_fd %d flags %#lx",
1800 pid
, cpu
, group_fd
, flags
);
1802 fd
= sys_perf_event_open(&evsel
->attr
, pid
, cpu
, group_fd
, flags
);
1806 /* Do not try less precise if not requested. */
1807 if (!evsel
->precise_max
)
1811 * We tried all the precise_ip values, and it's
1812 * still failing, so leave it to standard fallback.
1814 if (!evsel
->attr
.precise_ip
) {
1815 evsel
->attr
.precise_ip
= precise_ip
;
1819 pr_debug2("\nsys_perf_event_open failed, error %d\n", -ENOTSUP
);
1820 evsel
->attr
.precise_ip
--;
1821 pr_debug2("decreasing precise_ip by one (%d)\n", evsel
->attr
.precise_ip
);
1822 display_attr(&evsel
->attr
);
1828 int evsel__open(struct evsel
*evsel
, struct perf_cpu_map
*cpus
,
1829 struct perf_thread_map
*threads
)
1831 int cpu
, thread
, nthreads
;
1832 unsigned long flags
= PERF_FLAG_FD_CLOEXEC
;
1834 enum { NO_CHANGE
, SET_TO_MAX
, INCREASED_MAX
} set_rlimit
= NO_CHANGE
;
1836 if (perf_missing_features
.write_backward
&& evsel
->attr
.write_backward
)
1840 static struct perf_cpu_map
*empty_cpu_map
;
1842 if (empty_cpu_map
== NULL
) {
1843 empty_cpu_map
= cpu_map__dummy_new();
1844 if (empty_cpu_map
== NULL
)
1848 cpus
= empty_cpu_map
;
1851 if (threads
== NULL
) {
1852 static struct perf_thread_map
*empty_thread_map
;
1854 if (empty_thread_map
== NULL
) {
1855 empty_thread_map
= thread_map__new_by_tid(-1);
1856 if (empty_thread_map
== NULL
)
1860 threads
= empty_thread_map
;
1863 if (evsel
->system_wide
)
1866 nthreads
= threads
->nr
;
1868 if (evsel
->fd
== NULL
&&
1869 perf_evsel__alloc_fd(evsel
, cpus
->nr
, nthreads
) < 0)
1873 flags
|= PERF_FLAG_PID_CGROUP
;
1874 pid
= evsel
->cgrp
->fd
;
1877 fallback_missing_features
:
1878 if (perf_missing_features
.clockid_wrong
)
1879 evsel
->attr
.clockid
= CLOCK_MONOTONIC
; /* should always work */
1880 if (perf_missing_features
.clockid
) {
1881 evsel
->attr
.use_clockid
= 0;
1882 evsel
->attr
.clockid
= 0;
1884 if (perf_missing_features
.cloexec
)
1885 flags
&= ~(unsigned long)PERF_FLAG_FD_CLOEXEC
;
1886 if (perf_missing_features
.mmap2
)
1887 evsel
->attr
.mmap2
= 0;
1888 if (perf_missing_features
.exclude_guest
)
1889 evsel
->attr
.exclude_guest
= evsel
->attr
.exclude_host
= 0;
1890 if (perf_missing_features
.lbr_flags
)
1891 evsel
->attr
.branch_sample_type
&= ~(PERF_SAMPLE_BRANCH_NO_FLAGS
|
1892 PERF_SAMPLE_BRANCH_NO_CYCLES
);
1893 if (perf_missing_features
.group_read
&& evsel
->attr
.inherit
)
1894 evsel
->attr
.read_format
&= ~(PERF_FORMAT_GROUP
|PERF_FORMAT_ID
);
1895 if (perf_missing_features
.ksymbol
)
1896 evsel
->attr
.ksymbol
= 0;
1897 if (perf_missing_features
.bpf_event
)
1898 evsel
->attr
.bpf_event
= 0;
1900 if (perf_missing_features
.sample_id_all
)
1901 evsel
->attr
.sample_id_all
= 0;
1903 display_attr(&evsel
->attr
);
1905 for (cpu
= 0; cpu
< cpus
->nr
; cpu
++) {
1907 for (thread
= 0; thread
< nthreads
; thread
++) {
1910 if (!evsel
->cgrp
&& !evsel
->system_wide
)
1911 pid
= thread_map__pid(threads
, thread
);
1913 group_fd
= get_group_fd(evsel
, cpu
, thread
);
1917 fd
= perf_event_open(evsel
, pid
, cpus
->map
[cpu
],
1920 FD(evsel
, cpu
, thread
) = fd
;
1925 if (ignore_missing_thread(evsel
, cpus
->nr
, cpu
, threads
, thread
, err
)) {
1927 * We just removed 1 thread, so take a step
1928 * back on thread index and lower the upper
1934 /* ... and pretend like nothing have happened. */
1939 pr_debug2("\nsys_perf_event_open failed, error %d\n",
1944 pr_debug2(" = %d\n", fd
);
1946 if (evsel
->bpf_fd
>= 0) {
1948 int bpf_fd
= evsel
->bpf_fd
;
1951 PERF_EVENT_IOC_SET_BPF
,
1953 if (err
&& errno
!= EEXIST
) {
1954 pr_err("failed to attach bpf fd %d: %s\n",
1955 bpf_fd
, strerror(errno
));
1961 set_rlimit
= NO_CHANGE
;
1964 * If we succeeded but had to kill clockid, fail and
1965 * have perf_evsel__open_strerror() print us a nice
1968 if (perf_missing_features
.clockid
||
1969 perf_missing_features
.clockid_wrong
) {
1980 * perf stat needs between 5 and 22 fds per CPU. When we run out
1981 * of them try to increase the limits.
1983 if (err
== -EMFILE
&& set_rlimit
< INCREASED_MAX
) {
1985 int old_errno
= errno
;
1987 if (getrlimit(RLIMIT_NOFILE
, &l
) == 0) {
1988 if (set_rlimit
== NO_CHANGE
)
1989 l
.rlim_cur
= l
.rlim_max
;
1991 l
.rlim_cur
= l
.rlim_max
+ 1000;
1992 l
.rlim_max
= l
.rlim_cur
;
1994 if (setrlimit(RLIMIT_NOFILE
, &l
) == 0) {
2003 if (err
!= -EINVAL
|| cpu
> 0 || thread
> 0)
2007 * Must probe features in the order they were added to the
2008 * perf_event_attr interface.
2010 if (!perf_missing_features
.bpf_event
&& evsel
->attr
.bpf_event
) {
2011 perf_missing_features
.bpf_event
= true;
2012 pr_debug2("switching off bpf_event\n");
2013 goto fallback_missing_features
;
2014 } else if (!perf_missing_features
.ksymbol
&& evsel
->attr
.ksymbol
) {
2015 perf_missing_features
.ksymbol
= true;
2016 pr_debug2("switching off ksymbol\n");
2017 goto fallback_missing_features
;
2018 } else if (!perf_missing_features
.write_backward
&& evsel
->attr
.write_backward
) {
2019 perf_missing_features
.write_backward
= true;
2020 pr_debug2("switching off write_backward\n");
2022 } else if (!perf_missing_features
.clockid_wrong
&& evsel
->attr
.use_clockid
) {
2023 perf_missing_features
.clockid_wrong
= true;
2024 pr_debug2("switching off clockid\n");
2025 goto fallback_missing_features
;
2026 } else if (!perf_missing_features
.clockid
&& evsel
->attr
.use_clockid
) {
2027 perf_missing_features
.clockid
= true;
2028 pr_debug2("switching off use_clockid\n");
2029 goto fallback_missing_features
;
2030 } else if (!perf_missing_features
.cloexec
&& (flags
& PERF_FLAG_FD_CLOEXEC
)) {
2031 perf_missing_features
.cloexec
= true;
2032 pr_debug2("switching off cloexec flag\n");
2033 goto fallback_missing_features
;
2034 } else if (!perf_missing_features
.mmap2
&& evsel
->attr
.mmap2
) {
2035 perf_missing_features
.mmap2
= true;
2036 pr_debug2("switching off mmap2\n");
2037 goto fallback_missing_features
;
2038 } else if (!perf_missing_features
.exclude_guest
&&
2039 (evsel
->attr
.exclude_guest
|| evsel
->attr
.exclude_host
)) {
2040 perf_missing_features
.exclude_guest
= true;
2041 pr_debug2("switching off exclude_guest, exclude_host\n");
2042 goto fallback_missing_features
;
2043 } else if (!perf_missing_features
.sample_id_all
) {
2044 perf_missing_features
.sample_id_all
= true;
2045 pr_debug2("switching off sample_id_all\n");
2046 goto retry_sample_id
;
2047 } else if (!perf_missing_features
.lbr_flags
&&
2048 (evsel
->attr
.branch_sample_type
&
2049 (PERF_SAMPLE_BRANCH_NO_CYCLES
|
2050 PERF_SAMPLE_BRANCH_NO_FLAGS
))) {
2051 perf_missing_features
.lbr_flags
= true;
2052 pr_debug2("switching off branch sample type no (cycles/flags)\n");
2053 goto fallback_missing_features
;
2054 } else if (!perf_missing_features
.group_read
&&
2055 evsel
->attr
.inherit
&&
2056 (evsel
->attr
.read_format
& PERF_FORMAT_GROUP
) &&
2057 perf_evsel__is_group_leader(evsel
)) {
2058 perf_missing_features
.group_read
= true;
2059 pr_debug2("switching off group read\n");
2060 goto fallback_missing_features
;
2064 threads
->err_thread
= thread
;
2067 while (--thread
>= 0) {
2068 close(FD(evsel
, cpu
, thread
));
2069 FD(evsel
, cpu
, thread
) = -1;
2072 } while (--cpu
>= 0);
2076 void perf_evsel__close(struct evsel
*evsel
)
2078 if (evsel
->fd
== NULL
)
2081 perf_evsel__close_fd(evsel
);
2082 perf_evsel__free_fd(evsel
);
2083 perf_evsel__free_id(evsel
);
2086 int perf_evsel__open_per_cpu(struct evsel
*evsel
,
2087 struct perf_cpu_map
*cpus
)
2089 return evsel__open(evsel
, cpus
, NULL
);
2092 int perf_evsel__open_per_thread(struct evsel
*evsel
,
2093 struct perf_thread_map
*threads
)
2095 return evsel__open(evsel
, NULL
, threads
);
2098 static int perf_evsel__parse_id_sample(const struct evsel
*evsel
,
2099 const union perf_event
*event
,
2100 struct perf_sample
*sample
)
2102 u64 type
= evsel
->attr
.sample_type
;
2103 const u64
*array
= event
->sample
.array
;
2104 bool swapped
= evsel
->needs_swap
;
2107 array
+= ((event
->header
.size
-
2108 sizeof(event
->header
)) / sizeof(u64
)) - 1;
2110 if (type
& PERF_SAMPLE_IDENTIFIER
) {
2111 sample
->id
= *array
;
2115 if (type
& PERF_SAMPLE_CPU
) {
2118 /* undo swap of u64, then swap on individual u32s */
2119 u
.val64
= bswap_64(u
.val64
);
2120 u
.val32
[0] = bswap_32(u
.val32
[0]);
2123 sample
->cpu
= u
.val32
[0];
2127 if (type
& PERF_SAMPLE_STREAM_ID
) {
2128 sample
->stream_id
= *array
;
2132 if (type
& PERF_SAMPLE_ID
) {
2133 sample
->id
= *array
;
2137 if (type
& PERF_SAMPLE_TIME
) {
2138 sample
->time
= *array
;
2142 if (type
& PERF_SAMPLE_TID
) {
2145 /* undo swap of u64, then swap on individual u32s */
2146 u
.val64
= bswap_64(u
.val64
);
2147 u
.val32
[0] = bswap_32(u
.val32
[0]);
2148 u
.val32
[1] = bswap_32(u
.val32
[1]);
2151 sample
->pid
= u
.val32
[0];
2152 sample
->tid
= u
.val32
[1];
2159 static inline bool overflow(const void *endp
, u16 max_size
, const void *offset
,
2162 return size
> max_size
|| offset
+ size
> endp
;
2165 #define OVERFLOW_CHECK(offset, size, max_size) \
2167 if (overflow(endp, (max_size), (offset), (size))) \
2171 #define OVERFLOW_CHECK_u64(offset) \
2172 OVERFLOW_CHECK(offset, sizeof(u64), sizeof(u64))
2175 perf_event__check_size(union perf_event
*event
, unsigned int sample_size
)
2178 * The evsel's sample_size is based on PERF_SAMPLE_MASK which includes
2179 * up to PERF_SAMPLE_PERIOD. After that overflow() must be used to
2180 * check the format does not go past the end of the event.
2182 if (sample_size
+ sizeof(event
->header
) > event
->header
.size
)
2188 int perf_evsel__parse_sample(struct evsel
*evsel
, union perf_event
*event
,
2189 struct perf_sample
*data
)
2191 u64 type
= evsel
->attr
.sample_type
;
2192 bool swapped
= evsel
->needs_swap
;
2194 u16 max_size
= event
->header
.size
;
2195 const void *endp
= (void *)event
+ max_size
;
2199 * used for cross-endian analysis. See git commit 65014ab3
2200 * for why this goofiness is needed.
2204 memset(data
, 0, sizeof(*data
));
2205 data
->cpu
= data
->pid
= data
->tid
= -1;
2206 data
->stream_id
= data
->id
= data
->time
= -1ULL;
2207 data
->period
= evsel
->attr
.sample_period
;
2208 data
->cpumode
= event
->header
.misc
& PERF_RECORD_MISC_CPUMODE_MASK
;
2209 data
->misc
= event
->header
.misc
;
2211 data
->data_src
= PERF_MEM_DATA_SRC_NONE
;
2213 if (event
->header
.type
!= PERF_RECORD_SAMPLE
) {
2214 if (!evsel
->attr
.sample_id_all
)
2216 return perf_evsel__parse_id_sample(evsel
, event
, data
);
2219 array
= event
->sample
.array
;
2221 if (perf_event__check_size(event
, evsel
->sample_size
))
2224 if (type
& PERF_SAMPLE_IDENTIFIER
) {
2229 if (type
& PERF_SAMPLE_IP
) {
2234 if (type
& PERF_SAMPLE_TID
) {
2237 /* undo swap of u64, then swap on individual u32s */
2238 u
.val64
= bswap_64(u
.val64
);
2239 u
.val32
[0] = bswap_32(u
.val32
[0]);
2240 u
.val32
[1] = bswap_32(u
.val32
[1]);
2243 data
->pid
= u
.val32
[0];
2244 data
->tid
= u
.val32
[1];
2248 if (type
& PERF_SAMPLE_TIME
) {
2249 data
->time
= *array
;
2253 if (type
& PERF_SAMPLE_ADDR
) {
2254 data
->addr
= *array
;
2258 if (type
& PERF_SAMPLE_ID
) {
2263 if (type
& PERF_SAMPLE_STREAM_ID
) {
2264 data
->stream_id
= *array
;
2268 if (type
& PERF_SAMPLE_CPU
) {
2272 /* undo swap of u64, then swap on individual u32s */
2273 u
.val64
= bswap_64(u
.val64
);
2274 u
.val32
[0] = bswap_32(u
.val32
[0]);
2277 data
->cpu
= u
.val32
[0];
2281 if (type
& PERF_SAMPLE_PERIOD
) {
2282 data
->period
= *array
;
2286 if (type
& PERF_SAMPLE_READ
) {
2287 u64 read_format
= evsel
->attr
.read_format
;
2289 OVERFLOW_CHECK_u64(array
);
2290 if (read_format
& PERF_FORMAT_GROUP
)
2291 data
->read
.group
.nr
= *array
;
2293 data
->read
.one
.value
= *array
;
2297 if (read_format
& PERF_FORMAT_TOTAL_TIME_ENABLED
) {
2298 OVERFLOW_CHECK_u64(array
);
2299 data
->read
.time_enabled
= *array
;
2303 if (read_format
& PERF_FORMAT_TOTAL_TIME_RUNNING
) {
2304 OVERFLOW_CHECK_u64(array
);
2305 data
->read
.time_running
= *array
;
2309 /* PERF_FORMAT_ID is forced for PERF_SAMPLE_READ */
2310 if (read_format
& PERF_FORMAT_GROUP
) {
2311 const u64 max_group_nr
= UINT64_MAX
/
2312 sizeof(struct sample_read_value
);
2314 if (data
->read
.group
.nr
> max_group_nr
)
2316 sz
= data
->read
.group
.nr
*
2317 sizeof(struct sample_read_value
);
2318 OVERFLOW_CHECK(array
, sz
, max_size
);
2319 data
->read
.group
.values
=
2320 (struct sample_read_value
*)array
;
2321 array
= (void *)array
+ sz
;
2323 OVERFLOW_CHECK_u64(array
);
2324 data
->read
.one
.id
= *array
;
2329 if (evsel__has_callchain(evsel
)) {
2330 const u64 max_callchain_nr
= UINT64_MAX
/ sizeof(u64
);
2332 OVERFLOW_CHECK_u64(array
);
2333 data
->callchain
= (struct ip_callchain
*)array
++;
2334 if (data
->callchain
->nr
> max_callchain_nr
)
2336 sz
= data
->callchain
->nr
* sizeof(u64
);
2337 OVERFLOW_CHECK(array
, sz
, max_size
);
2338 array
= (void *)array
+ sz
;
2341 if (type
& PERF_SAMPLE_RAW
) {
2342 OVERFLOW_CHECK_u64(array
);
2346 * Undo swap of u64, then swap on individual u32s,
2347 * get the size of the raw area and undo all of the
2348 * swap. The pevent interface handles endianity by
2352 u
.val64
= bswap_64(u
.val64
);
2353 u
.val32
[0] = bswap_32(u
.val32
[0]);
2354 u
.val32
[1] = bswap_32(u
.val32
[1]);
2356 data
->raw_size
= u
.val32
[0];
2359 * The raw data is aligned on 64bits including the
2360 * u32 size, so it's safe to use mem_bswap_64.
2363 mem_bswap_64((void *) array
, data
->raw_size
);
2365 array
= (void *)array
+ sizeof(u32
);
2367 OVERFLOW_CHECK(array
, data
->raw_size
, max_size
);
2368 data
->raw_data
= (void *)array
;
2369 array
= (void *)array
+ data
->raw_size
;
2372 if (type
& PERF_SAMPLE_BRANCH_STACK
) {
2373 const u64 max_branch_nr
= UINT64_MAX
/
2374 sizeof(struct branch_entry
);
2376 OVERFLOW_CHECK_u64(array
);
2377 data
->branch_stack
= (struct branch_stack
*)array
++;
2379 if (data
->branch_stack
->nr
> max_branch_nr
)
2381 sz
= data
->branch_stack
->nr
* sizeof(struct branch_entry
);
2382 OVERFLOW_CHECK(array
, sz
, max_size
);
2383 array
= (void *)array
+ sz
;
2386 if (type
& PERF_SAMPLE_REGS_USER
) {
2387 OVERFLOW_CHECK_u64(array
);
2388 data
->user_regs
.abi
= *array
;
2391 if (data
->user_regs
.abi
) {
2392 u64 mask
= evsel
->attr
.sample_regs_user
;
2394 sz
= hweight64(mask
) * sizeof(u64
);
2395 OVERFLOW_CHECK(array
, sz
, max_size
);
2396 data
->user_regs
.mask
= mask
;
2397 data
->user_regs
.regs
= (u64
*)array
;
2398 array
= (void *)array
+ sz
;
2402 if (type
& PERF_SAMPLE_STACK_USER
) {
2403 OVERFLOW_CHECK_u64(array
);
2406 data
->user_stack
.offset
= ((char *)(array
- 1)
2410 data
->user_stack
.size
= 0;
2412 OVERFLOW_CHECK(array
, sz
, max_size
);
2413 data
->user_stack
.data
= (char *)array
;
2414 array
= (void *)array
+ sz
;
2415 OVERFLOW_CHECK_u64(array
);
2416 data
->user_stack
.size
= *array
++;
2417 if (WARN_ONCE(data
->user_stack
.size
> sz
,
2418 "user stack dump failure\n"))
2423 if (type
& PERF_SAMPLE_WEIGHT
) {
2424 OVERFLOW_CHECK_u64(array
);
2425 data
->weight
= *array
;
2429 if (type
& PERF_SAMPLE_DATA_SRC
) {
2430 OVERFLOW_CHECK_u64(array
);
2431 data
->data_src
= *array
;
2435 if (type
& PERF_SAMPLE_TRANSACTION
) {
2436 OVERFLOW_CHECK_u64(array
);
2437 data
->transaction
= *array
;
2441 data
->intr_regs
.abi
= PERF_SAMPLE_REGS_ABI_NONE
;
2442 if (type
& PERF_SAMPLE_REGS_INTR
) {
2443 OVERFLOW_CHECK_u64(array
);
2444 data
->intr_regs
.abi
= *array
;
2447 if (data
->intr_regs
.abi
!= PERF_SAMPLE_REGS_ABI_NONE
) {
2448 u64 mask
= evsel
->attr
.sample_regs_intr
;
2450 sz
= hweight64(mask
) * sizeof(u64
);
2451 OVERFLOW_CHECK(array
, sz
, max_size
);
2452 data
->intr_regs
.mask
= mask
;
2453 data
->intr_regs
.regs
= (u64
*)array
;
2454 array
= (void *)array
+ sz
;
2458 data
->phys_addr
= 0;
2459 if (type
& PERF_SAMPLE_PHYS_ADDR
) {
2460 data
->phys_addr
= *array
;
2467 int perf_evsel__parse_sample_timestamp(struct evsel
*evsel
,
2468 union perf_event
*event
,
2471 u64 type
= evsel
->attr
.sample_type
;
2474 if (!(type
& PERF_SAMPLE_TIME
))
2477 if (event
->header
.type
!= PERF_RECORD_SAMPLE
) {
2478 struct perf_sample data
= {
2482 if (!evsel
->attr
.sample_id_all
)
2484 if (perf_evsel__parse_id_sample(evsel
, event
, &data
))
2487 *timestamp
= data
.time
;
2491 array
= event
->sample
.array
;
2493 if (perf_event__check_size(event
, evsel
->sample_size
))
2496 if (type
& PERF_SAMPLE_IDENTIFIER
)
2499 if (type
& PERF_SAMPLE_IP
)
2502 if (type
& PERF_SAMPLE_TID
)
2505 if (type
& PERF_SAMPLE_TIME
)
2506 *timestamp
= *array
;
2511 size_t perf_event__sample_event_size(const struct perf_sample
*sample
, u64 type
,
2514 size_t sz
, result
= sizeof(struct sample_event
);
2516 if (type
& PERF_SAMPLE_IDENTIFIER
)
2517 result
+= sizeof(u64
);
2519 if (type
& PERF_SAMPLE_IP
)
2520 result
+= sizeof(u64
);
2522 if (type
& PERF_SAMPLE_TID
)
2523 result
+= sizeof(u64
);
2525 if (type
& PERF_SAMPLE_TIME
)
2526 result
+= sizeof(u64
);
2528 if (type
& PERF_SAMPLE_ADDR
)
2529 result
+= sizeof(u64
);
2531 if (type
& PERF_SAMPLE_ID
)
2532 result
+= sizeof(u64
);
2534 if (type
& PERF_SAMPLE_STREAM_ID
)
2535 result
+= sizeof(u64
);
2537 if (type
& PERF_SAMPLE_CPU
)
2538 result
+= sizeof(u64
);
2540 if (type
& PERF_SAMPLE_PERIOD
)
2541 result
+= sizeof(u64
);
2543 if (type
& PERF_SAMPLE_READ
) {
2544 result
+= sizeof(u64
);
2545 if (read_format
& PERF_FORMAT_TOTAL_TIME_ENABLED
)
2546 result
+= sizeof(u64
);
2547 if (read_format
& PERF_FORMAT_TOTAL_TIME_RUNNING
)
2548 result
+= sizeof(u64
);
2549 /* PERF_FORMAT_ID is forced for PERF_SAMPLE_READ */
2550 if (read_format
& PERF_FORMAT_GROUP
) {
2551 sz
= sample
->read
.group
.nr
*
2552 sizeof(struct sample_read_value
);
2555 result
+= sizeof(u64
);
2559 if (type
& PERF_SAMPLE_CALLCHAIN
) {
2560 sz
= (sample
->callchain
->nr
+ 1) * sizeof(u64
);
2564 if (type
& PERF_SAMPLE_RAW
) {
2565 result
+= sizeof(u32
);
2566 result
+= sample
->raw_size
;
2569 if (type
& PERF_SAMPLE_BRANCH_STACK
) {
2570 sz
= sample
->branch_stack
->nr
* sizeof(struct branch_entry
);
2575 if (type
& PERF_SAMPLE_REGS_USER
) {
2576 if (sample
->user_regs
.abi
) {
2577 result
+= sizeof(u64
);
2578 sz
= hweight64(sample
->user_regs
.mask
) * sizeof(u64
);
2581 result
+= sizeof(u64
);
2585 if (type
& PERF_SAMPLE_STACK_USER
) {
2586 sz
= sample
->user_stack
.size
;
2587 result
+= sizeof(u64
);
2590 result
+= sizeof(u64
);
2594 if (type
& PERF_SAMPLE_WEIGHT
)
2595 result
+= sizeof(u64
);
2597 if (type
& PERF_SAMPLE_DATA_SRC
)
2598 result
+= sizeof(u64
);
2600 if (type
& PERF_SAMPLE_TRANSACTION
)
2601 result
+= sizeof(u64
);
2603 if (type
& PERF_SAMPLE_REGS_INTR
) {
2604 if (sample
->intr_regs
.abi
) {
2605 result
+= sizeof(u64
);
2606 sz
= hweight64(sample
->intr_regs
.mask
) * sizeof(u64
);
2609 result
+= sizeof(u64
);
2613 if (type
& PERF_SAMPLE_PHYS_ADDR
)
2614 result
+= sizeof(u64
);
2619 int perf_event__synthesize_sample(union perf_event
*event
, u64 type
,
2621 const struct perf_sample
*sample
)
2626 * used for cross-endian analysis. See git commit 65014ab3
2627 * for why this goofiness is needed.
2631 array
= event
->sample
.array
;
2633 if (type
& PERF_SAMPLE_IDENTIFIER
) {
2634 *array
= sample
->id
;
2638 if (type
& PERF_SAMPLE_IP
) {
2639 *array
= sample
->ip
;
2643 if (type
& PERF_SAMPLE_TID
) {
2644 u
.val32
[0] = sample
->pid
;
2645 u
.val32
[1] = sample
->tid
;
2650 if (type
& PERF_SAMPLE_TIME
) {
2651 *array
= sample
->time
;
2655 if (type
& PERF_SAMPLE_ADDR
) {
2656 *array
= sample
->addr
;
2660 if (type
& PERF_SAMPLE_ID
) {
2661 *array
= sample
->id
;
2665 if (type
& PERF_SAMPLE_STREAM_ID
) {
2666 *array
= sample
->stream_id
;
2670 if (type
& PERF_SAMPLE_CPU
) {
2671 u
.val32
[0] = sample
->cpu
;
2677 if (type
& PERF_SAMPLE_PERIOD
) {
2678 *array
= sample
->period
;
2682 if (type
& PERF_SAMPLE_READ
) {
2683 if (read_format
& PERF_FORMAT_GROUP
)
2684 *array
= sample
->read
.group
.nr
;
2686 *array
= sample
->read
.one
.value
;
2689 if (read_format
& PERF_FORMAT_TOTAL_TIME_ENABLED
) {
2690 *array
= sample
->read
.time_enabled
;
2694 if (read_format
& PERF_FORMAT_TOTAL_TIME_RUNNING
) {
2695 *array
= sample
->read
.time_running
;
2699 /* PERF_FORMAT_ID is forced for PERF_SAMPLE_READ */
2700 if (read_format
& PERF_FORMAT_GROUP
) {
2701 sz
= sample
->read
.group
.nr
*
2702 sizeof(struct sample_read_value
);
2703 memcpy(array
, sample
->read
.group
.values
, sz
);
2704 array
= (void *)array
+ sz
;
2706 *array
= sample
->read
.one
.id
;
2711 if (type
& PERF_SAMPLE_CALLCHAIN
) {
2712 sz
= (sample
->callchain
->nr
+ 1) * sizeof(u64
);
2713 memcpy(array
, sample
->callchain
, sz
);
2714 array
= (void *)array
+ sz
;
2717 if (type
& PERF_SAMPLE_RAW
) {
2718 u
.val32
[0] = sample
->raw_size
;
2720 array
= (void *)array
+ sizeof(u32
);
2722 memcpy(array
, sample
->raw_data
, sample
->raw_size
);
2723 array
= (void *)array
+ sample
->raw_size
;
2726 if (type
& PERF_SAMPLE_BRANCH_STACK
) {
2727 sz
= sample
->branch_stack
->nr
* sizeof(struct branch_entry
);
2729 memcpy(array
, sample
->branch_stack
, sz
);
2730 array
= (void *)array
+ sz
;
2733 if (type
& PERF_SAMPLE_REGS_USER
) {
2734 if (sample
->user_regs
.abi
) {
2735 *array
++ = sample
->user_regs
.abi
;
2736 sz
= hweight64(sample
->user_regs
.mask
) * sizeof(u64
);
2737 memcpy(array
, sample
->user_regs
.regs
, sz
);
2738 array
= (void *)array
+ sz
;
2744 if (type
& PERF_SAMPLE_STACK_USER
) {
2745 sz
= sample
->user_stack
.size
;
2748 memcpy(array
, sample
->user_stack
.data
, sz
);
2749 array
= (void *)array
+ sz
;
2754 if (type
& PERF_SAMPLE_WEIGHT
) {
2755 *array
= sample
->weight
;
2759 if (type
& PERF_SAMPLE_DATA_SRC
) {
2760 *array
= sample
->data_src
;
2764 if (type
& PERF_SAMPLE_TRANSACTION
) {
2765 *array
= sample
->transaction
;
2769 if (type
& PERF_SAMPLE_REGS_INTR
) {
2770 if (sample
->intr_regs
.abi
) {
2771 *array
++ = sample
->intr_regs
.abi
;
2772 sz
= hweight64(sample
->intr_regs
.mask
) * sizeof(u64
);
2773 memcpy(array
, sample
->intr_regs
.regs
, sz
);
2774 array
= (void *)array
+ sz
;
2780 if (type
& PERF_SAMPLE_PHYS_ADDR
) {
2781 *array
= sample
->phys_addr
;
2788 struct tep_format_field
*perf_evsel__field(struct evsel
*evsel
, const char *name
)
2790 return tep_find_field(evsel
->tp_format
, name
);
2793 void *perf_evsel__rawptr(struct evsel
*evsel
, struct perf_sample
*sample
,
2796 struct tep_format_field
*field
= perf_evsel__field(evsel
, name
);
2802 offset
= field
->offset
;
2804 if (field
->flags
& TEP_FIELD_IS_DYNAMIC
) {
2805 offset
= *(int *)(sample
->raw_data
+ field
->offset
);
2809 return sample
->raw_data
+ offset
;
2812 u64
format_field__intval(struct tep_format_field
*field
, struct perf_sample
*sample
,
2816 void *ptr
= sample
->raw_data
+ field
->offset
;
2818 switch (field
->size
) {
2822 value
= *(u16
*)ptr
;
2825 value
= *(u32
*)ptr
;
2828 memcpy(&value
, ptr
, sizeof(u64
));
2837 switch (field
->size
) {
2839 return bswap_16(value
);
2841 return bswap_32(value
);
2843 return bswap_64(value
);
2851 u64
perf_evsel__intval(struct evsel
*evsel
, struct perf_sample
*sample
,
2854 struct tep_format_field
*field
= perf_evsel__field(evsel
, name
);
2859 return field
? format_field__intval(field
, sample
, evsel
->needs_swap
) : 0;
2862 bool perf_evsel__fallback(struct evsel
*evsel
, int err
,
2863 char *msg
, size_t msgsize
)
2867 if ((err
== ENOENT
|| err
== ENXIO
|| err
== ENODEV
) &&
2868 evsel
->attr
.type
== PERF_TYPE_HARDWARE
&&
2869 evsel
->attr
.config
== PERF_COUNT_HW_CPU_CYCLES
) {
2871 * If it's cycles then fall back to hrtimer based
2872 * cpu-clock-tick sw counter, which is always available even if
2875 * PPC returns ENXIO until 2.6.37 (behavior changed with commit
2878 scnprintf(msg
, msgsize
, "%s",
2879 "The cycles event is not supported, trying to fall back to cpu-clock-ticks");
2881 evsel
->attr
.type
= PERF_TYPE_SOFTWARE
;
2882 evsel
->attr
.config
= PERF_COUNT_SW_CPU_CLOCK
;
2884 zfree(&evsel
->name
);
2886 } else if (err
== EACCES
&& !evsel
->attr
.exclude_kernel
&&
2887 (paranoid
= perf_event_paranoid()) > 1) {
2888 const char *name
= perf_evsel__name(evsel
);
2890 const char *sep
= ":";
2892 /* Is there already the separator in the name. */
2893 if (strchr(name
, '/') ||
2897 if (asprintf(&new_name
, "%s%su", name
, sep
) < 0)
2902 evsel
->name
= new_name
;
2903 scnprintf(msg
, msgsize
,
2904 "kernel.perf_event_paranoid=%d, trying to fall back to excluding kernel samples", paranoid
);
2905 evsel
->attr
.exclude_kernel
= 1;
2913 static bool find_process(const char *name
)
2915 size_t len
= strlen(name
);
2920 dir
= opendir(procfs__mountpoint());
2924 /* Walk through the directory. */
2925 while (ret
&& (d
= readdir(dir
)) != NULL
) {
2926 char path
[PATH_MAX
];
2930 if ((d
->d_type
!= DT_DIR
) ||
2931 !strcmp(".", d
->d_name
) ||
2932 !strcmp("..", d
->d_name
))
2935 scnprintf(path
, sizeof(path
), "%s/%s/comm",
2936 procfs__mountpoint(), d
->d_name
);
2938 if (filename__read_str(path
, &data
, &size
))
2941 ret
= strncmp(name
, data
, len
);
2946 return ret
? false : true;
2949 int perf_evsel__open_strerror(struct evsel
*evsel
, struct target
*target
,
2950 int err
, char *msg
, size_t size
)
2952 char sbuf
[STRERR_BUFSIZE
];
2959 printed
= scnprintf(msg
, size
,
2960 "No permission to enable %s event.\n\n",
2961 perf_evsel__name(evsel
));
2963 return scnprintf(msg
+ printed
, size
- printed
,
2964 "You may not have permission to collect %sstats.\n\n"
2965 "Consider tweaking /proc/sys/kernel/perf_event_paranoid,\n"
2966 "which controls use of the performance events system by\n"
2967 "unprivileged users (without CAP_SYS_ADMIN).\n\n"
2968 "The current value is %d:\n\n"
2969 " -1: Allow use of (almost) all events by all users\n"
2970 " Ignore mlock limit after perf_event_mlock_kb without CAP_IPC_LOCK\n"
2971 ">= 0: Disallow ftrace function tracepoint by users without CAP_SYS_ADMIN\n"
2972 " Disallow raw tracepoint access by users without CAP_SYS_ADMIN\n"
2973 ">= 1: Disallow CPU event access by users without CAP_SYS_ADMIN\n"
2974 ">= 2: Disallow kernel profiling by users without CAP_SYS_ADMIN\n\n"
2975 "To make this setting permanent, edit /etc/sysctl.conf too, e.g.:\n\n"
2976 " kernel.perf_event_paranoid = -1\n" ,
2977 target
->system_wide
? "system-wide " : "",
2978 perf_event_paranoid());
2980 return scnprintf(msg
, size
, "The %s event is not supported.",
2981 perf_evsel__name(evsel
));
2983 return scnprintf(msg
, size
, "%s",
2984 "Too many events are opened.\n"
2985 "Probably the maximum number of open file descriptors has been reached.\n"
2986 "Hint: Try again after reducing the number of events.\n"
2987 "Hint: Try increasing the limit with 'ulimit -n <limit>'");
2989 if (evsel__has_callchain(evsel
) &&
2990 access("/proc/sys/kernel/perf_event_max_stack", F_OK
) == 0)
2991 return scnprintf(msg
, size
,
2992 "Not enough memory to setup event with callchain.\n"
2993 "Hint: Try tweaking /proc/sys/kernel/perf_event_max_stack\n"
2994 "Hint: Current value: %d", sysctl__max_stack());
2997 if (target
->cpu_list
)
2998 return scnprintf(msg
, size
, "%s",
2999 "No such device - did you specify an out-of-range profile CPU?");
3002 if (evsel
->attr
.sample_period
!= 0)
3003 return scnprintf(msg
, size
,
3004 "%s: PMU Hardware doesn't support sampling/overflow-interrupts. Try 'perf stat'",
3005 perf_evsel__name(evsel
));
3006 if (evsel
->attr
.precise_ip
)
3007 return scnprintf(msg
, size
, "%s",
3008 "\'precise\' request may not be supported. Try removing 'p' modifier.");
3009 #if defined(__i386__) || defined(__x86_64__)
3010 if (evsel
->attr
.type
== PERF_TYPE_HARDWARE
)
3011 return scnprintf(msg
, size
, "%s",
3012 "No hardware sampling interrupt available.\n");
3016 if (find_process("oprofiled"))
3017 return scnprintf(msg
, size
,
3018 "The PMU counters are busy/taken by another profiler.\n"
3019 "We found oprofile daemon running, please stop it and try again.");
3022 if (evsel
->attr
.write_backward
&& perf_missing_features
.write_backward
)
3023 return scnprintf(msg
, size
, "Reading from overwrite event is not supported by this kernel.");
3024 if (perf_missing_features
.clockid
)
3025 return scnprintf(msg
, size
, "clockid feature not supported.");
3026 if (perf_missing_features
.clockid_wrong
)
3027 return scnprintf(msg
, size
, "wrong clockid (%d).", clockid
);
3033 return scnprintf(msg
, size
,
3034 "The sys_perf_event_open() syscall returned with %d (%s) for event (%s).\n"
3035 "/bin/dmesg | grep -i perf may provide additional information.\n",
3036 err
, str_error_r(err
, sbuf
, sizeof(sbuf
)),
3037 perf_evsel__name(evsel
));
3040 struct perf_env
*perf_evsel__env(struct evsel
*evsel
)
3042 if (evsel
&& evsel
->evlist
)
3043 return evsel
->evlist
->env
;
3047 static int store_evsel_ids(struct evsel
*evsel
, struct evlist
*evlist
)
3051 for (cpu
= 0; cpu
< xyarray__max_x(evsel
->fd
); cpu
++) {
3052 for (thread
= 0; thread
< xyarray__max_y(evsel
->fd
);
3054 int fd
= FD(evsel
, cpu
, thread
);
3056 if (perf_evlist__id_add_fd(evlist
, evsel
,
3057 cpu
, thread
, fd
) < 0)
3065 int perf_evsel__store_ids(struct evsel
*evsel
, struct evlist
*evlist
)
3067 struct perf_cpu_map
*cpus
= evsel
->cpus
;
3068 struct perf_thread_map
*threads
= evsel
->threads
;
3070 if (perf_evsel__alloc_id(evsel
, cpus
->nr
, threads
->nr
))
3073 return store_evsel_ids(evsel
, evlist
);