2 * Copyright (C) 2011, Red Hat Inc, Arnaldo Carvalho de Melo <acme@redhat.com>
4 * Parts came from builtin-{top,stat,record}.c, see those files for further
7 * Released under the GPL v2. (and only v2, not any later version)
11 #include <linux/bitops.h>
12 #include <api/fs/tracing_path.h>
13 #include <traceevent/event-parse.h>
14 #include <linux/hw_breakpoint.h>
15 #include <linux/perf_event.h>
16 #include <linux/err.h>
17 #include <sys/resource.h>
19 #include "callchain.h"
25 #include "thread_map.h"
27 #include "perf_regs.h"
29 #include "trace-event.h"
41 } perf_missing_features
;
43 static clockid_t clockid
;
45 static int perf_evsel__no_extra_init(struct perf_evsel
*evsel __maybe_unused
)
50 static void perf_evsel__no_extra_fini(struct perf_evsel
*evsel __maybe_unused
)
56 int (*init
)(struct perf_evsel
*evsel
);
57 void (*fini
)(struct perf_evsel
*evsel
);
58 } perf_evsel__object
= {
59 .size
= sizeof(struct perf_evsel
),
60 .init
= perf_evsel__no_extra_init
,
61 .fini
= perf_evsel__no_extra_fini
,
64 int perf_evsel__object_config(size_t object_size
,
65 int (*init
)(struct perf_evsel
*evsel
),
66 void (*fini
)(struct perf_evsel
*evsel
))
72 if (perf_evsel__object
.size
> object_size
)
75 perf_evsel__object
.size
= object_size
;
79 perf_evsel__object
.init
= init
;
82 perf_evsel__object
.fini
= fini
;
87 #define FD(e, x, y) (*(int *)xyarray__entry(e->fd, x, y))
89 int __perf_evsel__sample_size(u64 sample_type
)
91 u64 mask
= sample_type
& PERF_SAMPLE_MASK
;
95 for (i
= 0; i
< 64; i
++) {
96 if (mask
& (1ULL << i
))
106 * __perf_evsel__calc_id_pos - calculate id_pos.
107 * @sample_type: sample type
109 * This function returns the position of the event id (PERF_SAMPLE_ID or
110 * PERF_SAMPLE_IDENTIFIER) in a sample event i.e. in the array of struct
113 static int __perf_evsel__calc_id_pos(u64 sample_type
)
117 if (sample_type
& PERF_SAMPLE_IDENTIFIER
)
120 if (!(sample_type
& PERF_SAMPLE_ID
))
123 if (sample_type
& PERF_SAMPLE_IP
)
126 if (sample_type
& PERF_SAMPLE_TID
)
129 if (sample_type
& PERF_SAMPLE_TIME
)
132 if (sample_type
& PERF_SAMPLE_ADDR
)
139 * __perf_evsel__calc_is_pos - calculate is_pos.
140 * @sample_type: sample type
142 * This function returns the position (counting backwards) of the event id
143 * (PERF_SAMPLE_ID or PERF_SAMPLE_IDENTIFIER) in a non-sample event i.e. if
144 * sample_id_all is used there is an id sample appended to non-sample events.
146 static int __perf_evsel__calc_is_pos(u64 sample_type
)
150 if (sample_type
& PERF_SAMPLE_IDENTIFIER
)
153 if (!(sample_type
& PERF_SAMPLE_ID
))
156 if (sample_type
& PERF_SAMPLE_CPU
)
159 if (sample_type
& PERF_SAMPLE_STREAM_ID
)
165 void perf_evsel__calc_id_pos(struct perf_evsel
*evsel
)
167 evsel
->id_pos
= __perf_evsel__calc_id_pos(evsel
->attr
.sample_type
);
168 evsel
->is_pos
= __perf_evsel__calc_is_pos(evsel
->attr
.sample_type
);
171 void __perf_evsel__set_sample_bit(struct perf_evsel
*evsel
,
172 enum perf_event_sample_format bit
)
174 if (!(evsel
->attr
.sample_type
& bit
)) {
175 evsel
->attr
.sample_type
|= bit
;
176 evsel
->sample_size
+= sizeof(u64
);
177 perf_evsel__calc_id_pos(evsel
);
181 void __perf_evsel__reset_sample_bit(struct perf_evsel
*evsel
,
182 enum perf_event_sample_format bit
)
184 if (evsel
->attr
.sample_type
& bit
) {
185 evsel
->attr
.sample_type
&= ~bit
;
186 evsel
->sample_size
-= sizeof(u64
);
187 perf_evsel__calc_id_pos(evsel
);
191 void perf_evsel__set_sample_id(struct perf_evsel
*evsel
,
192 bool can_sample_identifier
)
194 if (can_sample_identifier
) {
195 perf_evsel__reset_sample_bit(evsel
, ID
);
196 perf_evsel__set_sample_bit(evsel
, IDENTIFIER
);
198 perf_evsel__set_sample_bit(evsel
, ID
);
200 evsel
->attr
.read_format
|= PERF_FORMAT_ID
;
204 * perf_evsel__is_function_event - Return whether given evsel is a function
207 * @evsel - evsel selector to be tested
209 * Return %true if event is function trace event
211 bool perf_evsel__is_function_event(struct perf_evsel
*evsel
)
213 #define FUNCTION_EVENT "ftrace:function"
215 return evsel
->name
&&
216 !strncmp(FUNCTION_EVENT
, evsel
->name
, sizeof(FUNCTION_EVENT
));
218 #undef FUNCTION_EVENT
221 void perf_evsel__init(struct perf_evsel
*evsel
,
222 struct perf_event_attr
*attr
, int idx
)
225 evsel
->tracking
= !idx
;
227 evsel
->leader
= evsel
;
230 evsel
->evlist
= NULL
;
232 INIT_LIST_HEAD(&evsel
->node
);
233 INIT_LIST_HEAD(&evsel
->config_terms
);
234 perf_evsel__object
.init(evsel
);
235 evsel
->sample_size
= __perf_evsel__sample_size(attr
->sample_type
);
236 perf_evsel__calc_id_pos(evsel
);
237 evsel
->cmdline_group_boundary
= false;
240 struct perf_evsel
*perf_evsel__new_idx(struct perf_event_attr
*attr
, int idx
)
242 struct perf_evsel
*evsel
= zalloc(perf_evsel__object
.size
);
245 perf_evsel__init(evsel
, attr
, idx
);
247 if (perf_evsel__is_bpf_output(evsel
)) {
248 evsel
->attr
.sample_type
|= (PERF_SAMPLE_RAW
| PERF_SAMPLE_TIME
|
249 PERF_SAMPLE_CPU
| PERF_SAMPLE_PERIOD
),
250 evsel
->attr
.sample_period
= 1;
257 * Returns pointer with encoded error via <linux/err.h> interface.
259 struct perf_evsel
*perf_evsel__newtp_idx(const char *sys
, const char *name
, int idx
)
261 struct perf_evsel
*evsel
= zalloc(perf_evsel__object
.size
);
267 struct perf_event_attr attr
= {
268 .type
= PERF_TYPE_TRACEPOINT
,
269 .sample_type
= (PERF_SAMPLE_RAW
| PERF_SAMPLE_TIME
|
270 PERF_SAMPLE_CPU
| PERF_SAMPLE_PERIOD
),
273 if (asprintf(&evsel
->name
, "%s:%s", sys
, name
) < 0)
276 evsel
->tp_format
= trace_event__tp_format(sys
, name
);
277 if (IS_ERR(evsel
->tp_format
)) {
278 err
= PTR_ERR(evsel
->tp_format
);
282 event_attr_init(&attr
);
283 attr
.config
= evsel
->tp_format
->id
;
284 attr
.sample_period
= 1;
285 perf_evsel__init(evsel
, &attr
, idx
);
297 const char *perf_evsel__hw_names
[PERF_COUNT_HW_MAX
] = {
305 "stalled-cycles-frontend",
306 "stalled-cycles-backend",
310 static const char *__perf_evsel__hw_name(u64 config
)
312 if (config
< PERF_COUNT_HW_MAX
&& perf_evsel__hw_names
[config
])
313 return perf_evsel__hw_names
[config
];
315 return "unknown-hardware";
318 static int perf_evsel__add_modifiers(struct perf_evsel
*evsel
, char *bf
, size_t size
)
320 int colon
= 0, r
= 0;
321 struct perf_event_attr
*attr
= &evsel
->attr
;
322 bool exclude_guest_default
= false;
324 #define MOD_PRINT(context, mod) do { \
325 if (!attr->exclude_##context) { \
326 if (!colon) colon = ++r; \
327 r += scnprintf(bf + r, size - r, "%c", mod); \
330 if (attr
->exclude_kernel
|| attr
->exclude_user
|| attr
->exclude_hv
) {
331 MOD_PRINT(kernel
, 'k');
332 MOD_PRINT(user
, 'u');
334 exclude_guest_default
= true;
337 if (attr
->precise_ip
) {
340 r
+= scnprintf(bf
+ r
, size
- r
, "%.*s", attr
->precise_ip
, "ppp");
341 exclude_guest_default
= true;
344 if (attr
->exclude_host
|| attr
->exclude_guest
== exclude_guest_default
) {
345 MOD_PRINT(host
, 'H');
346 MOD_PRINT(guest
, 'G');
354 static int perf_evsel__hw_name(struct perf_evsel
*evsel
, char *bf
, size_t size
)
356 int r
= scnprintf(bf
, size
, "%s", __perf_evsel__hw_name(evsel
->attr
.config
));
357 return r
+ perf_evsel__add_modifiers(evsel
, bf
+ r
, size
- r
);
360 const char *perf_evsel__sw_names
[PERF_COUNT_SW_MAX
] = {
373 static const char *__perf_evsel__sw_name(u64 config
)
375 if (config
< PERF_COUNT_SW_MAX
&& perf_evsel__sw_names
[config
])
376 return perf_evsel__sw_names
[config
];
377 return "unknown-software";
380 static int perf_evsel__sw_name(struct perf_evsel
*evsel
, char *bf
, size_t size
)
382 int r
= scnprintf(bf
, size
, "%s", __perf_evsel__sw_name(evsel
->attr
.config
));
383 return r
+ perf_evsel__add_modifiers(evsel
, bf
+ r
, size
- r
);
386 static int __perf_evsel__bp_name(char *bf
, size_t size
, u64 addr
, u64 type
)
390 r
= scnprintf(bf
, size
, "mem:0x%" PRIx64
":", addr
);
392 if (type
& HW_BREAKPOINT_R
)
393 r
+= scnprintf(bf
+ r
, size
- r
, "r");
395 if (type
& HW_BREAKPOINT_W
)
396 r
+= scnprintf(bf
+ r
, size
- r
, "w");
398 if (type
& HW_BREAKPOINT_X
)
399 r
+= scnprintf(bf
+ r
, size
- r
, "x");
404 static int perf_evsel__bp_name(struct perf_evsel
*evsel
, char *bf
, size_t size
)
406 struct perf_event_attr
*attr
= &evsel
->attr
;
407 int r
= __perf_evsel__bp_name(bf
, size
, attr
->bp_addr
, attr
->bp_type
);
408 return r
+ perf_evsel__add_modifiers(evsel
, bf
+ r
, size
- r
);
411 const char *perf_evsel__hw_cache
[PERF_COUNT_HW_CACHE_MAX
]
412 [PERF_EVSEL__MAX_ALIASES
] = {
413 { "L1-dcache", "l1-d", "l1d", "L1-data", },
414 { "L1-icache", "l1-i", "l1i", "L1-instruction", },
416 { "dTLB", "d-tlb", "Data-TLB", },
417 { "iTLB", "i-tlb", "Instruction-TLB", },
418 { "branch", "branches", "bpu", "btb", "bpc", },
422 const char *perf_evsel__hw_cache_op
[PERF_COUNT_HW_CACHE_OP_MAX
]
423 [PERF_EVSEL__MAX_ALIASES
] = {
424 { "load", "loads", "read", },
425 { "store", "stores", "write", },
426 { "prefetch", "prefetches", "speculative-read", "speculative-load", },
429 const char *perf_evsel__hw_cache_result
[PERF_COUNT_HW_CACHE_RESULT_MAX
]
430 [PERF_EVSEL__MAX_ALIASES
] = {
431 { "refs", "Reference", "ops", "access", },
432 { "misses", "miss", },
435 #define C(x) PERF_COUNT_HW_CACHE_##x
436 #define CACHE_READ (1 << C(OP_READ))
437 #define CACHE_WRITE (1 << C(OP_WRITE))
438 #define CACHE_PREFETCH (1 << C(OP_PREFETCH))
439 #define COP(x) (1 << x)
442 * cache operartion stat
443 * L1I : Read and prefetch only
444 * ITLB and BPU : Read-only
446 static unsigned long perf_evsel__hw_cache_stat
[C(MAX
)] = {
447 [C(L1D
)] = (CACHE_READ
| CACHE_WRITE
| CACHE_PREFETCH
),
448 [C(L1I
)] = (CACHE_READ
| CACHE_PREFETCH
),
449 [C(LL
)] = (CACHE_READ
| CACHE_WRITE
| CACHE_PREFETCH
),
450 [C(DTLB
)] = (CACHE_READ
| CACHE_WRITE
| CACHE_PREFETCH
),
451 [C(ITLB
)] = (CACHE_READ
),
452 [C(BPU
)] = (CACHE_READ
),
453 [C(NODE
)] = (CACHE_READ
| CACHE_WRITE
| CACHE_PREFETCH
),
456 bool perf_evsel__is_cache_op_valid(u8 type
, u8 op
)
458 if (perf_evsel__hw_cache_stat
[type
] & COP(op
))
459 return true; /* valid */
461 return false; /* invalid */
464 int __perf_evsel__hw_cache_type_op_res_name(u8 type
, u8 op
, u8 result
,
465 char *bf
, size_t size
)
468 return scnprintf(bf
, size
, "%s-%s-%s", perf_evsel__hw_cache
[type
][0],
469 perf_evsel__hw_cache_op
[op
][0],
470 perf_evsel__hw_cache_result
[result
][0]);
473 return scnprintf(bf
, size
, "%s-%s", perf_evsel__hw_cache
[type
][0],
474 perf_evsel__hw_cache_op
[op
][1]);
477 static int __perf_evsel__hw_cache_name(u64 config
, char *bf
, size_t size
)
479 u8 op
, result
, type
= (config
>> 0) & 0xff;
480 const char *err
= "unknown-ext-hardware-cache-type";
482 if (type
> PERF_COUNT_HW_CACHE_MAX
)
485 op
= (config
>> 8) & 0xff;
486 err
= "unknown-ext-hardware-cache-op";
487 if (op
> PERF_COUNT_HW_CACHE_OP_MAX
)
490 result
= (config
>> 16) & 0xff;
491 err
= "unknown-ext-hardware-cache-result";
492 if (result
> PERF_COUNT_HW_CACHE_RESULT_MAX
)
495 err
= "invalid-cache";
496 if (!perf_evsel__is_cache_op_valid(type
, op
))
499 return __perf_evsel__hw_cache_type_op_res_name(type
, op
, result
, bf
, size
);
501 return scnprintf(bf
, size
, "%s", err
);
504 static int perf_evsel__hw_cache_name(struct perf_evsel
*evsel
, char *bf
, size_t size
)
506 int ret
= __perf_evsel__hw_cache_name(evsel
->attr
.config
, bf
, size
);
507 return ret
+ perf_evsel__add_modifiers(evsel
, bf
+ ret
, size
- ret
);
510 static int perf_evsel__raw_name(struct perf_evsel
*evsel
, char *bf
, size_t size
)
512 int ret
= scnprintf(bf
, size
, "raw 0x%" PRIx64
, evsel
->attr
.config
);
513 return ret
+ perf_evsel__add_modifiers(evsel
, bf
+ ret
, size
- ret
);
516 const char *perf_evsel__name(struct perf_evsel
*evsel
)
523 switch (evsel
->attr
.type
) {
525 perf_evsel__raw_name(evsel
, bf
, sizeof(bf
));
528 case PERF_TYPE_HARDWARE
:
529 perf_evsel__hw_name(evsel
, bf
, sizeof(bf
));
532 case PERF_TYPE_HW_CACHE
:
533 perf_evsel__hw_cache_name(evsel
, bf
, sizeof(bf
));
536 case PERF_TYPE_SOFTWARE
:
537 perf_evsel__sw_name(evsel
, bf
, sizeof(bf
));
540 case PERF_TYPE_TRACEPOINT
:
541 scnprintf(bf
, sizeof(bf
), "%s", "unknown tracepoint");
544 case PERF_TYPE_BREAKPOINT
:
545 perf_evsel__bp_name(evsel
, bf
, sizeof(bf
));
549 scnprintf(bf
, sizeof(bf
), "unknown attr type: %d",
554 evsel
->name
= strdup(bf
);
556 return evsel
->name
?: "unknown";
559 const char *perf_evsel__group_name(struct perf_evsel
*evsel
)
561 return evsel
->group_name
?: "anon group";
564 int perf_evsel__group_desc(struct perf_evsel
*evsel
, char *buf
, size_t size
)
567 struct perf_evsel
*pos
;
568 const char *group_name
= perf_evsel__group_name(evsel
);
570 ret
= scnprintf(buf
, size
, "%s", group_name
);
572 ret
+= scnprintf(buf
+ ret
, size
- ret
, " { %s",
573 perf_evsel__name(evsel
));
575 for_each_group_member(pos
, evsel
)
576 ret
+= scnprintf(buf
+ ret
, size
- ret
, ", %s",
577 perf_evsel__name(pos
));
579 ret
+= scnprintf(buf
+ ret
, size
- ret
, " }");
584 void perf_evsel__config_callchain(struct perf_evsel
*evsel
,
585 struct record_opts
*opts
,
586 struct callchain_param
*param
)
588 bool function
= perf_evsel__is_function_event(evsel
);
589 struct perf_event_attr
*attr
= &evsel
->attr
;
591 perf_evsel__set_sample_bit(evsel
, CALLCHAIN
);
593 attr
->sample_max_stack
= param
->max_stack
;
595 if (param
->record_mode
== CALLCHAIN_LBR
) {
596 if (!opts
->branch_stack
) {
597 if (attr
->exclude_user
) {
598 pr_warning("LBR callstack option is only available "
599 "to get user callchain information. "
600 "Falling back to framepointers.\n");
602 perf_evsel__set_sample_bit(evsel
, BRANCH_STACK
);
603 attr
->branch_sample_type
= PERF_SAMPLE_BRANCH_USER
|
604 PERF_SAMPLE_BRANCH_CALL_STACK
|
605 PERF_SAMPLE_BRANCH_NO_CYCLES
|
606 PERF_SAMPLE_BRANCH_NO_FLAGS
;
609 pr_warning("Cannot use LBR callstack with branch stack. "
610 "Falling back to framepointers.\n");
613 if (param
->record_mode
== CALLCHAIN_DWARF
) {
615 perf_evsel__set_sample_bit(evsel
, REGS_USER
);
616 perf_evsel__set_sample_bit(evsel
, STACK_USER
);
617 attr
->sample_regs_user
= PERF_REGS_MASK
;
618 attr
->sample_stack_user
= param
->dump_size
;
619 attr
->exclude_callchain_user
= 1;
621 pr_info("Cannot use DWARF unwind for function trace event,"
622 " falling back to framepointers.\n");
627 pr_info("Disabling user space callchains for function trace event.\n");
628 attr
->exclude_callchain_user
= 1;
633 perf_evsel__reset_callgraph(struct perf_evsel
*evsel
,
634 struct callchain_param
*param
)
636 struct perf_event_attr
*attr
= &evsel
->attr
;
638 perf_evsel__reset_sample_bit(evsel
, CALLCHAIN
);
639 if (param
->record_mode
== CALLCHAIN_LBR
) {
640 perf_evsel__reset_sample_bit(evsel
, BRANCH_STACK
);
641 attr
->branch_sample_type
&= ~(PERF_SAMPLE_BRANCH_USER
|
642 PERF_SAMPLE_BRANCH_CALL_STACK
);
644 if (param
->record_mode
== CALLCHAIN_DWARF
) {
645 perf_evsel__reset_sample_bit(evsel
, REGS_USER
);
646 perf_evsel__reset_sample_bit(evsel
, STACK_USER
);
650 static void apply_config_terms(struct perf_evsel
*evsel
,
651 struct record_opts
*opts
)
653 struct perf_evsel_config_term
*term
;
654 struct list_head
*config_terms
= &evsel
->config_terms
;
655 struct perf_event_attr
*attr
= &evsel
->attr
;
656 struct callchain_param param
;
659 const char *callgraph_buf
= NULL
;
661 /* callgraph default */
662 param
.record_mode
= callchain_param
.record_mode
;
664 list_for_each_entry(term
, config_terms
, list
) {
665 switch (term
->type
) {
666 case PERF_EVSEL__CONFIG_TERM_PERIOD
:
667 attr
->sample_period
= term
->val
.period
;
670 case PERF_EVSEL__CONFIG_TERM_FREQ
:
671 attr
->sample_freq
= term
->val
.freq
;
674 case PERF_EVSEL__CONFIG_TERM_TIME
:
676 perf_evsel__set_sample_bit(evsel
, TIME
);
678 perf_evsel__reset_sample_bit(evsel
, TIME
);
680 case PERF_EVSEL__CONFIG_TERM_CALLGRAPH
:
681 callgraph_buf
= term
->val
.callgraph
;
683 case PERF_EVSEL__CONFIG_TERM_STACK_USER
:
684 dump_size
= term
->val
.stack_user
;
686 case PERF_EVSEL__CONFIG_TERM_MAX_STACK
:
687 max_stack
= term
->val
.max_stack
;
689 case PERF_EVSEL__CONFIG_TERM_INHERIT
:
691 * attr->inherit should has already been set by
692 * perf_evsel__config. If user explicitly set
693 * inherit using config terms, override global
694 * opt->no_inherit setting.
696 attr
->inherit
= term
->val
.inherit
? 1 : 0;
698 case PERF_EVSEL__CONFIG_TERM_OVERWRITE
:
699 attr
->write_backward
= term
->val
.overwrite
? 1 : 0;
706 /* User explicitly set per-event callgraph, clear the old setting and reset. */
707 if ((callgraph_buf
!= NULL
) || (dump_size
> 0) || max_stack
) {
709 param
.max_stack
= max_stack
;
710 if (callgraph_buf
== NULL
)
711 callgraph_buf
= "fp";
714 /* parse callgraph parameters */
715 if (callgraph_buf
!= NULL
) {
716 if (!strcmp(callgraph_buf
, "no")) {
717 param
.enabled
= false;
718 param
.record_mode
= CALLCHAIN_NONE
;
720 param
.enabled
= true;
721 if (parse_callchain_record(callgraph_buf
, ¶m
)) {
722 pr_err("per-event callgraph setting for %s failed. "
723 "Apply callgraph global setting for it\n",
730 dump_size
= round_up(dump_size
, sizeof(u64
));
731 param
.dump_size
= dump_size
;
734 /* If global callgraph set, clear it */
735 if (callchain_param
.enabled
)
736 perf_evsel__reset_callgraph(evsel
, &callchain_param
);
738 /* set perf-event callgraph */
740 perf_evsel__config_callchain(evsel
, opts
, ¶m
);
745 * The enable_on_exec/disabled value strategy:
747 * 1) For any type of traced program:
748 * - all independent events and group leaders are disabled
749 * - all group members are enabled
751 * Group members are ruled by group leaders. They need to
752 * be enabled, because the group scheduling relies on that.
754 * 2) For traced programs executed by perf:
755 * - all independent events and group leaders have
757 * - we don't specifically enable or disable any event during
760 * Independent events and group leaders are initially disabled
761 * and get enabled by exec. Group members are ruled by group
762 * leaders as stated in 1).
764 * 3) For traced programs attached by perf (pid/tid):
765 * - we specifically enable or disable all events during
768 * When attaching events to already running traced we
769 * enable/disable events specifically, as there's no
770 * initial traced exec call.
772 void perf_evsel__config(struct perf_evsel
*evsel
, struct record_opts
*opts
,
773 struct callchain_param
*callchain
)
775 struct perf_evsel
*leader
= evsel
->leader
;
776 struct perf_event_attr
*attr
= &evsel
->attr
;
777 int track
= evsel
->tracking
;
778 bool per_cpu
= opts
->target
.default_per_cpu
&& !opts
->target
.per_thread
;
780 attr
->sample_id_all
= perf_missing_features
.sample_id_all
? 0 : 1;
781 attr
->inherit
= !opts
->no_inherit
;
782 attr
->write_backward
= opts
->overwrite
? 1 : 0;
784 perf_evsel__set_sample_bit(evsel
, IP
);
785 perf_evsel__set_sample_bit(evsel
, TID
);
787 if (evsel
->sample_read
) {
788 perf_evsel__set_sample_bit(evsel
, READ
);
791 * We need ID even in case of single event, because
792 * PERF_SAMPLE_READ process ID specific data.
794 perf_evsel__set_sample_id(evsel
, false);
797 * Apply group format only if we belong to group
798 * with more than one members.
800 if (leader
->nr_members
> 1) {
801 attr
->read_format
|= PERF_FORMAT_GROUP
;
807 * We default some events to have a default interval. But keep
808 * it a weak assumption overridable by the user.
810 if (!attr
->sample_period
|| (opts
->user_freq
!= UINT_MAX
||
811 opts
->user_interval
!= ULLONG_MAX
)) {
813 perf_evsel__set_sample_bit(evsel
, PERIOD
);
815 attr
->sample_freq
= opts
->freq
;
817 attr
->sample_period
= opts
->default_interval
;
822 * Disable sampling for all group members other
823 * than leader in case leader 'leads' the sampling.
825 if ((leader
!= evsel
) && leader
->sample_read
) {
826 attr
->sample_freq
= 0;
827 attr
->sample_period
= 0;
830 if (opts
->no_samples
)
831 attr
->sample_freq
= 0;
833 if (opts
->inherit_stat
)
834 attr
->inherit_stat
= 1;
836 if (opts
->sample_address
) {
837 perf_evsel__set_sample_bit(evsel
, ADDR
);
838 attr
->mmap_data
= track
;
842 * We don't allow user space callchains for function trace
843 * event, due to issues with page faults while tracing page
844 * fault handler and its overall trickiness nature.
846 if (perf_evsel__is_function_event(evsel
))
847 evsel
->attr
.exclude_callchain_user
= 1;
849 if (callchain
&& callchain
->enabled
&& !evsel
->no_aux_samples
)
850 perf_evsel__config_callchain(evsel
, opts
, callchain
);
852 if (opts
->sample_intr_regs
) {
853 attr
->sample_regs_intr
= opts
->sample_intr_regs
;
854 perf_evsel__set_sample_bit(evsel
, REGS_INTR
);
857 if (target__has_cpu(&opts
->target
))
858 perf_evsel__set_sample_bit(evsel
, CPU
);
861 perf_evsel__set_sample_bit(evsel
, PERIOD
);
864 * When the user explicitly disabled time don't force it here.
866 if (opts
->sample_time
&&
867 (!perf_missing_features
.sample_id_all
&&
868 (!opts
->no_inherit
|| target__has_cpu(&opts
->target
) || per_cpu
||
869 opts
->sample_time_set
)))
870 perf_evsel__set_sample_bit(evsel
, TIME
);
872 if (opts
->raw_samples
&& !evsel
->no_aux_samples
) {
873 perf_evsel__set_sample_bit(evsel
, TIME
);
874 perf_evsel__set_sample_bit(evsel
, RAW
);
875 perf_evsel__set_sample_bit(evsel
, CPU
);
878 if (opts
->sample_address
)
879 perf_evsel__set_sample_bit(evsel
, DATA_SRC
);
881 if (opts
->no_buffering
) {
883 attr
->wakeup_events
= 1;
885 if (opts
->branch_stack
&& !evsel
->no_aux_samples
) {
886 perf_evsel__set_sample_bit(evsel
, BRANCH_STACK
);
887 attr
->branch_sample_type
= opts
->branch_stack
;
890 if (opts
->sample_weight
)
891 perf_evsel__set_sample_bit(evsel
, WEIGHT
);
895 attr
->mmap2
= track
&& !perf_missing_features
.mmap2
;
898 if (opts
->record_switch_events
)
899 attr
->context_switch
= track
;
901 if (opts
->sample_transaction
)
902 perf_evsel__set_sample_bit(evsel
, TRANSACTION
);
904 if (opts
->running_time
) {
905 evsel
->attr
.read_format
|=
906 PERF_FORMAT_TOTAL_TIME_ENABLED
|
907 PERF_FORMAT_TOTAL_TIME_RUNNING
;
911 * XXX see the function comment above
913 * Disabling only independent events or group leaders,
914 * keeping group members enabled.
916 if (perf_evsel__is_group_leader(evsel
))
920 * Setting enable_on_exec for independent events and
921 * group leaders for traced executed by perf.
923 if (target__none(&opts
->target
) && perf_evsel__is_group_leader(evsel
) &&
924 !opts
->initial_delay
)
925 attr
->enable_on_exec
= 1;
927 if (evsel
->immediate
) {
929 attr
->enable_on_exec
= 0;
932 clockid
= opts
->clockid
;
933 if (opts
->use_clockid
) {
934 attr
->use_clockid
= 1;
935 attr
->clockid
= opts
->clockid
;
938 if (evsel
->precise_max
)
939 perf_event_attr__set_max_precise_ip(attr
);
941 if (opts
->all_user
) {
942 attr
->exclude_kernel
= 1;
943 attr
->exclude_user
= 0;
946 if (opts
->all_kernel
) {
947 attr
->exclude_kernel
= 0;
948 attr
->exclude_user
= 1;
952 * Apply event specific term settings,
953 * it overloads any global configuration.
955 apply_config_terms(evsel
, opts
);
958 static int perf_evsel__alloc_fd(struct perf_evsel
*evsel
, int ncpus
, int nthreads
)
962 if (evsel
->system_wide
)
965 evsel
->fd
= xyarray__new(ncpus
, nthreads
, sizeof(int));
968 for (cpu
= 0; cpu
< ncpus
; cpu
++) {
969 for (thread
= 0; thread
< nthreads
; thread
++) {
970 FD(evsel
, cpu
, thread
) = -1;
975 return evsel
->fd
!= NULL
? 0 : -ENOMEM
;
978 static int perf_evsel__run_ioctl(struct perf_evsel
*evsel
, int ncpus
, int nthreads
,
983 if (evsel
->system_wide
)
986 for (cpu
= 0; cpu
< ncpus
; cpu
++) {
987 for (thread
= 0; thread
< nthreads
; thread
++) {
988 int fd
= FD(evsel
, cpu
, thread
),
989 err
= ioctl(fd
, ioc
, arg
);
999 int perf_evsel__apply_filter(struct perf_evsel
*evsel
, int ncpus
, int nthreads
,
1002 return perf_evsel__run_ioctl(evsel
, ncpus
, nthreads
,
1003 PERF_EVENT_IOC_SET_FILTER
,
1007 int perf_evsel__set_filter(struct perf_evsel
*evsel
, const char *filter
)
1009 char *new_filter
= strdup(filter
);
1011 if (new_filter
!= NULL
) {
1012 free(evsel
->filter
);
1013 evsel
->filter
= new_filter
;
1020 int perf_evsel__append_filter(struct perf_evsel
*evsel
,
1021 const char *op
, const char *filter
)
1025 if (evsel
->filter
== NULL
)
1026 return perf_evsel__set_filter(evsel
, filter
);
1028 if (asprintf(&new_filter
,"(%s) %s (%s)", evsel
->filter
, op
, filter
) > 0) {
1029 free(evsel
->filter
);
1030 evsel
->filter
= new_filter
;
1037 int perf_evsel__enable(struct perf_evsel
*evsel
)
1039 int nthreads
= thread_map__nr(evsel
->threads
);
1040 int ncpus
= cpu_map__nr(evsel
->cpus
);
1042 return perf_evsel__run_ioctl(evsel
, ncpus
, nthreads
,
1043 PERF_EVENT_IOC_ENABLE
,
1047 int perf_evsel__disable(struct perf_evsel
*evsel
)
1049 int nthreads
= thread_map__nr(evsel
->threads
);
1050 int ncpus
= cpu_map__nr(evsel
->cpus
);
1052 return perf_evsel__run_ioctl(evsel
, ncpus
, nthreads
,
1053 PERF_EVENT_IOC_DISABLE
,
1057 int perf_evsel__alloc_id(struct perf_evsel
*evsel
, int ncpus
, int nthreads
)
1059 if (ncpus
== 0 || nthreads
== 0)
1062 if (evsel
->system_wide
)
1065 evsel
->sample_id
= xyarray__new(ncpus
, nthreads
, sizeof(struct perf_sample_id
));
1066 if (evsel
->sample_id
== NULL
)
1069 evsel
->id
= zalloc(ncpus
* nthreads
* sizeof(u64
));
1070 if (evsel
->id
== NULL
) {
1071 xyarray__delete(evsel
->sample_id
);
1072 evsel
->sample_id
= NULL
;
1079 static void perf_evsel__free_fd(struct perf_evsel
*evsel
)
1081 xyarray__delete(evsel
->fd
);
1085 static void perf_evsel__free_id(struct perf_evsel
*evsel
)
1087 xyarray__delete(evsel
->sample_id
);
1088 evsel
->sample_id
= NULL
;
1092 static void perf_evsel__free_config_terms(struct perf_evsel
*evsel
)
1094 struct perf_evsel_config_term
*term
, *h
;
1096 list_for_each_entry_safe(term
, h
, &evsel
->config_terms
, list
) {
1097 list_del(&term
->list
);
1102 void perf_evsel__close_fd(struct perf_evsel
*evsel
, int ncpus
, int nthreads
)
1106 if (evsel
->system_wide
)
1109 for (cpu
= 0; cpu
< ncpus
; cpu
++)
1110 for (thread
= 0; thread
< nthreads
; ++thread
) {
1111 close(FD(evsel
, cpu
, thread
));
1112 FD(evsel
, cpu
, thread
) = -1;
1116 void perf_evsel__exit(struct perf_evsel
*evsel
)
1118 assert(list_empty(&evsel
->node
));
1119 assert(evsel
->evlist
== NULL
);
1120 perf_evsel__free_fd(evsel
);
1121 perf_evsel__free_id(evsel
);
1122 perf_evsel__free_config_terms(evsel
);
1123 close_cgroup(evsel
->cgrp
);
1124 cpu_map__put(evsel
->cpus
);
1125 cpu_map__put(evsel
->own_cpus
);
1126 thread_map__put(evsel
->threads
);
1127 zfree(&evsel
->group_name
);
1128 zfree(&evsel
->name
);
1129 perf_evsel__object
.fini(evsel
);
1132 void perf_evsel__delete(struct perf_evsel
*evsel
)
1134 perf_evsel__exit(evsel
);
1138 void perf_evsel__compute_deltas(struct perf_evsel
*evsel
, int cpu
, int thread
,
1139 struct perf_counts_values
*count
)
1141 struct perf_counts_values tmp
;
1143 if (!evsel
->prev_raw_counts
)
1147 tmp
= evsel
->prev_raw_counts
->aggr
;
1148 evsel
->prev_raw_counts
->aggr
= *count
;
1150 tmp
= *perf_counts(evsel
->prev_raw_counts
, cpu
, thread
);
1151 *perf_counts(evsel
->prev_raw_counts
, cpu
, thread
) = *count
;
1154 count
->val
= count
->val
- tmp
.val
;
1155 count
->ena
= count
->ena
- tmp
.ena
;
1156 count
->run
= count
->run
- tmp
.run
;
1159 void perf_counts_values__scale(struct perf_counts_values
*count
,
1160 bool scale
, s8
*pscaled
)
1165 if (count
->run
== 0) {
1168 } else if (count
->run
< count
->ena
) {
1170 count
->val
= (u64
)((double) count
->val
* count
->ena
/ count
->run
+ 0.5);
1173 count
->ena
= count
->run
= 0;
1179 int perf_evsel__read(struct perf_evsel
*evsel
, int cpu
, int thread
,
1180 struct perf_counts_values
*count
)
1182 memset(count
, 0, sizeof(*count
));
1184 if (FD(evsel
, cpu
, thread
) < 0)
1187 if (readn(FD(evsel
, cpu
, thread
), count
, sizeof(*count
)) < 0)
1193 int __perf_evsel__read_on_cpu(struct perf_evsel
*evsel
,
1194 int cpu
, int thread
, bool scale
)
1196 struct perf_counts_values count
;
1197 size_t nv
= scale
? 3 : 1;
1199 if (FD(evsel
, cpu
, thread
) < 0)
1202 if (evsel
->counts
== NULL
&& perf_evsel__alloc_counts(evsel
, cpu
+ 1, thread
+ 1) < 0)
1205 if (readn(FD(evsel
, cpu
, thread
), &count
, nv
* sizeof(u64
)) < 0)
1208 perf_evsel__compute_deltas(evsel
, cpu
, thread
, &count
);
1209 perf_counts_values__scale(&count
, scale
, NULL
);
1210 *perf_counts(evsel
->counts
, cpu
, thread
) = count
;
1214 static int get_group_fd(struct perf_evsel
*evsel
, int cpu
, int thread
)
1216 struct perf_evsel
*leader
= evsel
->leader
;
1219 if (perf_evsel__is_group_leader(evsel
))
1223 * Leader must be already processed/open,
1224 * if not it's a bug.
1226 BUG_ON(!leader
->fd
);
1228 fd
= FD(leader
, cpu
, thread
);
1239 static void __p_bits(char *buf
, size_t size
, u64 value
, struct bit_names
*bits
)
1241 bool first_bit
= true;
1245 if (value
& bits
[i
].bit
) {
1246 buf
+= scnprintf(buf
, size
, "%s%s", first_bit
? "" : "|", bits
[i
].name
);
1249 } while (bits
[++i
].name
!= NULL
);
1252 static void __p_sample_type(char *buf
, size_t size
, u64 value
)
1254 #define bit_name(n) { PERF_SAMPLE_##n, #n }
1255 struct bit_names bits
[] = {
1256 bit_name(IP
), bit_name(TID
), bit_name(TIME
), bit_name(ADDR
),
1257 bit_name(READ
), bit_name(CALLCHAIN
), bit_name(ID
), bit_name(CPU
),
1258 bit_name(PERIOD
), bit_name(STREAM_ID
), bit_name(RAW
),
1259 bit_name(BRANCH_STACK
), bit_name(REGS_USER
), bit_name(STACK_USER
),
1260 bit_name(IDENTIFIER
), bit_name(REGS_INTR
), bit_name(DATA_SRC
),
1265 __p_bits(buf
, size
, value
, bits
);
1268 static void __p_branch_sample_type(char *buf
, size_t size
, u64 value
)
1270 #define bit_name(n) { PERF_SAMPLE_BRANCH_##n, #n }
1271 struct bit_names bits
[] = {
1272 bit_name(USER
), bit_name(KERNEL
), bit_name(HV
), bit_name(ANY
),
1273 bit_name(ANY_CALL
), bit_name(ANY_RETURN
), bit_name(IND_CALL
),
1274 bit_name(ABORT_TX
), bit_name(IN_TX
), bit_name(NO_TX
),
1275 bit_name(COND
), bit_name(CALL_STACK
), bit_name(IND_JUMP
),
1276 bit_name(CALL
), bit_name(NO_FLAGS
), bit_name(NO_CYCLES
),
1280 __p_bits(buf
, size
, value
, bits
);
1283 static void __p_read_format(char *buf
, size_t size
, u64 value
)
1285 #define bit_name(n) { PERF_FORMAT_##n, #n }
1286 struct bit_names bits
[] = {
1287 bit_name(TOTAL_TIME_ENABLED
), bit_name(TOTAL_TIME_RUNNING
),
1288 bit_name(ID
), bit_name(GROUP
),
1292 __p_bits(buf
, size
, value
, bits
);
1295 #define BUF_SIZE 1024
1297 #define p_hex(val) snprintf(buf, BUF_SIZE, "%#"PRIx64, (uint64_t)(val))
1298 #define p_unsigned(val) snprintf(buf, BUF_SIZE, "%"PRIu64, (uint64_t)(val))
1299 #define p_signed(val) snprintf(buf, BUF_SIZE, "%"PRId64, (int64_t)(val))
1300 #define p_sample_type(val) __p_sample_type(buf, BUF_SIZE, val)
1301 #define p_branch_sample_type(val) __p_branch_sample_type(buf, BUF_SIZE, val)
1302 #define p_read_format(val) __p_read_format(buf, BUF_SIZE, val)
1304 #define PRINT_ATTRn(_n, _f, _p) \
1308 ret += attr__fprintf(fp, _n, buf, priv);\
1312 #define PRINT_ATTRf(_f, _p) PRINT_ATTRn(#_f, _f, _p)
1314 int perf_event_attr__fprintf(FILE *fp
, struct perf_event_attr
*attr
,
1315 attr__fprintf_f attr__fprintf
, void *priv
)
1320 PRINT_ATTRf(type
, p_unsigned
);
1321 PRINT_ATTRf(size
, p_unsigned
);
1322 PRINT_ATTRf(config
, p_hex
);
1323 PRINT_ATTRn("{ sample_period, sample_freq }", sample_period
, p_unsigned
);
1324 PRINT_ATTRf(sample_type
, p_sample_type
);
1325 PRINT_ATTRf(read_format
, p_read_format
);
1327 PRINT_ATTRf(disabled
, p_unsigned
);
1328 PRINT_ATTRf(inherit
, p_unsigned
);
1329 PRINT_ATTRf(pinned
, p_unsigned
);
1330 PRINT_ATTRf(exclusive
, p_unsigned
);
1331 PRINT_ATTRf(exclude_user
, p_unsigned
);
1332 PRINT_ATTRf(exclude_kernel
, p_unsigned
);
1333 PRINT_ATTRf(exclude_hv
, p_unsigned
);
1334 PRINT_ATTRf(exclude_idle
, p_unsigned
);
1335 PRINT_ATTRf(mmap
, p_unsigned
);
1336 PRINT_ATTRf(comm
, p_unsigned
);
1337 PRINT_ATTRf(freq
, p_unsigned
);
1338 PRINT_ATTRf(inherit_stat
, p_unsigned
);
1339 PRINT_ATTRf(enable_on_exec
, p_unsigned
);
1340 PRINT_ATTRf(task
, p_unsigned
);
1341 PRINT_ATTRf(watermark
, p_unsigned
);
1342 PRINT_ATTRf(precise_ip
, p_unsigned
);
1343 PRINT_ATTRf(mmap_data
, p_unsigned
);
1344 PRINT_ATTRf(sample_id_all
, p_unsigned
);
1345 PRINT_ATTRf(exclude_host
, p_unsigned
);
1346 PRINT_ATTRf(exclude_guest
, p_unsigned
);
1347 PRINT_ATTRf(exclude_callchain_kernel
, p_unsigned
);
1348 PRINT_ATTRf(exclude_callchain_user
, p_unsigned
);
1349 PRINT_ATTRf(mmap2
, p_unsigned
);
1350 PRINT_ATTRf(comm_exec
, p_unsigned
);
1351 PRINT_ATTRf(use_clockid
, p_unsigned
);
1352 PRINT_ATTRf(context_switch
, p_unsigned
);
1353 PRINT_ATTRf(write_backward
, p_unsigned
);
1355 PRINT_ATTRn("{ wakeup_events, wakeup_watermark }", wakeup_events
, p_unsigned
);
1356 PRINT_ATTRf(bp_type
, p_unsigned
);
1357 PRINT_ATTRn("{ bp_addr, config1 }", bp_addr
, p_hex
);
1358 PRINT_ATTRn("{ bp_len, config2 }", bp_len
, p_hex
);
1359 PRINT_ATTRf(branch_sample_type
, p_branch_sample_type
);
1360 PRINT_ATTRf(sample_regs_user
, p_hex
);
1361 PRINT_ATTRf(sample_stack_user
, p_unsigned
);
1362 PRINT_ATTRf(clockid
, p_signed
);
1363 PRINT_ATTRf(sample_regs_intr
, p_hex
);
1364 PRINT_ATTRf(aux_watermark
, p_unsigned
);
1365 PRINT_ATTRf(sample_max_stack
, p_unsigned
);
1370 static int __open_attr__fprintf(FILE *fp
, const char *name
, const char *val
,
1371 void *priv
__attribute__((unused
)))
1373 return fprintf(fp
, " %-32s %s\n", name
, val
);
1376 static int __perf_evsel__open(struct perf_evsel
*evsel
, struct cpu_map
*cpus
,
1377 struct thread_map
*threads
)
1379 int cpu
, thread
, nthreads
;
1380 unsigned long flags
= PERF_FLAG_FD_CLOEXEC
;
1382 enum { NO_CHANGE
, SET_TO_MAX
, INCREASED_MAX
} set_rlimit
= NO_CHANGE
;
1384 if (perf_missing_features
.write_backward
&& evsel
->attr
.write_backward
)
1387 if (evsel
->system_wide
)
1390 nthreads
= threads
->nr
;
1392 if (evsel
->fd
== NULL
&&
1393 perf_evsel__alloc_fd(evsel
, cpus
->nr
, nthreads
) < 0)
1397 flags
|= PERF_FLAG_PID_CGROUP
;
1398 pid
= evsel
->cgrp
->fd
;
1401 fallback_missing_features
:
1402 if (perf_missing_features
.clockid_wrong
)
1403 evsel
->attr
.clockid
= CLOCK_MONOTONIC
; /* should always work */
1404 if (perf_missing_features
.clockid
) {
1405 evsel
->attr
.use_clockid
= 0;
1406 evsel
->attr
.clockid
= 0;
1408 if (perf_missing_features
.cloexec
)
1409 flags
&= ~(unsigned long)PERF_FLAG_FD_CLOEXEC
;
1410 if (perf_missing_features
.mmap2
)
1411 evsel
->attr
.mmap2
= 0;
1412 if (perf_missing_features
.exclude_guest
)
1413 evsel
->attr
.exclude_guest
= evsel
->attr
.exclude_host
= 0;
1414 if (perf_missing_features
.lbr_flags
)
1415 evsel
->attr
.branch_sample_type
&= ~(PERF_SAMPLE_BRANCH_NO_FLAGS
|
1416 PERF_SAMPLE_BRANCH_NO_CYCLES
);
1418 if (perf_missing_features
.sample_id_all
)
1419 evsel
->attr
.sample_id_all
= 0;
1422 fprintf(stderr
, "%.60s\n", graph_dotted_line
);
1423 fprintf(stderr
, "perf_event_attr:\n");
1424 perf_event_attr__fprintf(stderr
, &evsel
->attr
, __open_attr__fprintf
, NULL
);
1425 fprintf(stderr
, "%.60s\n", graph_dotted_line
);
1428 for (cpu
= 0; cpu
< cpus
->nr
; cpu
++) {
1430 for (thread
= 0; thread
< nthreads
; thread
++) {
1433 if (!evsel
->cgrp
&& !evsel
->system_wide
)
1434 pid
= thread_map__pid(threads
, thread
);
1436 group_fd
= get_group_fd(evsel
, cpu
, thread
);
1438 pr_debug2("sys_perf_event_open: pid %d cpu %d group_fd %d flags %#lx\n",
1439 pid
, cpus
->map
[cpu
], group_fd
, flags
);
1441 FD(evsel
, cpu
, thread
) = sys_perf_event_open(&evsel
->attr
,
1445 if (FD(evsel
, cpu
, thread
) < 0) {
1447 pr_debug2("sys_perf_event_open failed, error %d\n",
1452 if (evsel
->bpf_fd
>= 0) {
1453 int evt_fd
= FD(evsel
, cpu
, thread
);
1454 int bpf_fd
= evsel
->bpf_fd
;
1457 PERF_EVENT_IOC_SET_BPF
,
1459 if (err
&& errno
!= EEXIST
) {
1460 pr_err("failed to attach bpf fd %d: %s\n",
1461 bpf_fd
, strerror(errno
));
1467 set_rlimit
= NO_CHANGE
;
1470 * If we succeeded but had to kill clockid, fail and
1471 * have perf_evsel__open_strerror() print us a nice
1474 if (perf_missing_features
.clockid
||
1475 perf_missing_features
.clockid_wrong
) {
1486 * perf stat needs between 5 and 22 fds per CPU. When we run out
1487 * of them try to increase the limits.
1489 if (err
== -EMFILE
&& set_rlimit
< INCREASED_MAX
) {
1491 int old_errno
= errno
;
1493 if (getrlimit(RLIMIT_NOFILE
, &l
) == 0) {
1494 if (set_rlimit
== NO_CHANGE
)
1495 l
.rlim_cur
= l
.rlim_max
;
1497 l
.rlim_cur
= l
.rlim_max
+ 1000;
1498 l
.rlim_max
= l
.rlim_cur
;
1500 if (setrlimit(RLIMIT_NOFILE
, &l
) == 0) {
1509 if (err
!= -EINVAL
|| cpu
> 0 || thread
> 0)
1513 * Must probe features in the order they were added to the
1514 * perf_event_attr interface.
1516 if (!perf_missing_features
.write_backward
&& evsel
->attr
.write_backward
) {
1517 perf_missing_features
.write_backward
= true;
1519 } else if (!perf_missing_features
.clockid_wrong
&& evsel
->attr
.use_clockid
) {
1520 perf_missing_features
.clockid_wrong
= true;
1521 goto fallback_missing_features
;
1522 } else if (!perf_missing_features
.clockid
&& evsel
->attr
.use_clockid
) {
1523 perf_missing_features
.clockid
= true;
1524 goto fallback_missing_features
;
1525 } else if (!perf_missing_features
.cloexec
&& (flags
& PERF_FLAG_FD_CLOEXEC
)) {
1526 perf_missing_features
.cloexec
= true;
1527 goto fallback_missing_features
;
1528 } else if (!perf_missing_features
.mmap2
&& evsel
->attr
.mmap2
) {
1529 perf_missing_features
.mmap2
= true;
1530 goto fallback_missing_features
;
1531 } else if (!perf_missing_features
.exclude_guest
&&
1532 (evsel
->attr
.exclude_guest
|| evsel
->attr
.exclude_host
)) {
1533 perf_missing_features
.exclude_guest
= true;
1534 goto fallback_missing_features
;
1535 } else if (!perf_missing_features
.sample_id_all
) {
1536 perf_missing_features
.sample_id_all
= true;
1537 goto retry_sample_id
;
1538 } else if (!perf_missing_features
.lbr_flags
&&
1539 (evsel
->attr
.branch_sample_type
&
1540 (PERF_SAMPLE_BRANCH_NO_CYCLES
|
1541 PERF_SAMPLE_BRANCH_NO_FLAGS
))) {
1542 perf_missing_features
.lbr_flags
= true;
1543 goto fallback_missing_features
;
1547 while (--thread
>= 0) {
1548 close(FD(evsel
, cpu
, thread
));
1549 FD(evsel
, cpu
, thread
) = -1;
1552 } while (--cpu
>= 0);
1556 void perf_evsel__close(struct perf_evsel
*evsel
, int ncpus
, int nthreads
)
1558 if (evsel
->fd
== NULL
)
1561 perf_evsel__close_fd(evsel
, ncpus
, nthreads
);
1562 perf_evsel__free_fd(evsel
);
1574 struct thread_map map
;
1576 } empty_thread_map
= {
1581 int perf_evsel__open(struct perf_evsel
*evsel
, struct cpu_map
*cpus
,
1582 struct thread_map
*threads
)
1585 /* Work around old compiler warnings about strict aliasing */
1586 cpus
= &empty_cpu_map
.map
;
1589 if (threads
== NULL
)
1590 threads
= &empty_thread_map
.map
;
1592 return __perf_evsel__open(evsel
, cpus
, threads
);
1595 int perf_evsel__open_per_cpu(struct perf_evsel
*evsel
,
1596 struct cpu_map
*cpus
)
1598 return __perf_evsel__open(evsel
, cpus
, &empty_thread_map
.map
);
1601 int perf_evsel__open_per_thread(struct perf_evsel
*evsel
,
1602 struct thread_map
*threads
)
1604 return __perf_evsel__open(evsel
, &empty_cpu_map
.map
, threads
);
1607 static int perf_evsel__parse_id_sample(const struct perf_evsel
*evsel
,
1608 const union perf_event
*event
,
1609 struct perf_sample
*sample
)
1611 u64 type
= evsel
->attr
.sample_type
;
1612 const u64
*array
= event
->sample
.array
;
1613 bool swapped
= evsel
->needs_swap
;
1616 array
+= ((event
->header
.size
-
1617 sizeof(event
->header
)) / sizeof(u64
)) - 1;
1619 if (type
& PERF_SAMPLE_IDENTIFIER
) {
1620 sample
->id
= *array
;
1624 if (type
& PERF_SAMPLE_CPU
) {
1627 /* undo swap of u64, then swap on individual u32s */
1628 u
.val64
= bswap_64(u
.val64
);
1629 u
.val32
[0] = bswap_32(u
.val32
[0]);
1632 sample
->cpu
= u
.val32
[0];
1636 if (type
& PERF_SAMPLE_STREAM_ID
) {
1637 sample
->stream_id
= *array
;
1641 if (type
& PERF_SAMPLE_ID
) {
1642 sample
->id
= *array
;
1646 if (type
& PERF_SAMPLE_TIME
) {
1647 sample
->time
= *array
;
1651 if (type
& PERF_SAMPLE_TID
) {
1654 /* undo swap of u64, then swap on individual u32s */
1655 u
.val64
= bswap_64(u
.val64
);
1656 u
.val32
[0] = bswap_32(u
.val32
[0]);
1657 u
.val32
[1] = bswap_32(u
.val32
[1]);
1660 sample
->pid
= u
.val32
[0];
1661 sample
->tid
= u
.val32
[1];
1668 static inline bool overflow(const void *endp
, u16 max_size
, const void *offset
,
1671 return size
> max_size
|| offset
+ size
> endp
;
1674 #define OVERFLOW_CHECK(offset, size, max_size) \
1676 if (overflow(endp, (max_size), (offset), (size))) \
1680 #define OVERFLOW_CHECK_u64(offset) \
1681 OVERFLOW_CHECK(offset, sizeof(u64), sizeof(u64))
1683 int perf_evsel__parse_sample(struct perf_evsel
*evsel
, union perf_event
*event
,
1684 struct perf_sample
*data
)
1686 u64 type
= evsel
->attr
.sample_type
;
1687 bool swapped
= evsel
->needs_swap
;
1689 u16 max_size
= event
->header
.size
;
1690 const void *endp
= (void *)event
+ max_size
;
1694 * used for cross-endian analysis. See git commit 65014ab3
1695 * for why this goofiness is needed.
1699 memset(data
, 0, sizeof(*data
));
1700 data
->cpu
= data
->pid
= data
->tid
= -1;
1701 data
->stream_id
= data
->id
= data
->time
= -1ULL;
1702 data
->period
= evsel
->attr
.sample_period
;
1704 data
->cpumode
= event
->header
.misc
& PERF_RECORD_MISC_CPUMODE_MASK
;
1706 if (event
->header
.type
!= PERF_RECORD_SAMPLE
) {
1707 if (!evsel
->attr
.sample_id_all
)
1709 return perf_evsel__parse_id_sample(evsel
, event
, data
);
1712 array
= event
->sample
.array
;
1715 * The evsel's sample_size is based on PERF_SAMPLE_MASK which includes
1716 * up to PERF_SAMPLE_PERIOD. After that overflow() must be used to
1717 * check the format does not go past the end of the event.
1719 if (evsel
->sample_size
+ sizeof(event
->header
) > event
->header
.size
)
1723 if (type
& PERF_SAMPLE_IDENTIFIER
) {
1728 if (type
& PERF_SAMPLE_IP
) {
1733 if (type
& PERF_SAMPLE_TID
) {
1736 /* undo swap of u64, then swap on individual u32s */
1737 u
.val64
= bswap_64(u
.val64
);
1738 u
.val32
[0] = bswap_32(u
.val32
[0]);
1739 u
.val32
[1] = bswap_32(u
.val32
[1]);
1742 data
->pid
= u
.val32
[0];
1743 data
->tid
= u
.val32
[1];
1747 if (type
& PERF_SAMPLE_TIME
) {
1748 data
->time
= *array
;
1753 if (type
& PERF_SAMPLE_ADDR
) {
1754 data
->addr
= *array
;
1758 if (type
& PERF_SAMPLE_ID
) {
1763 if (type
& PERF_SAMPLE_STREAM_ID
) {
1764 data
->stream_id
= *array
;
1768 if (type
& PERF_SAMPLE_CPU
) {
1772 /* undo swap of u64, then swap on individual u32s */
1773 u
.val64
= bswap_64(u
.val64
);
1774 u
.val32
[0] = bswap_32(u
.val32
[0]);
1777 data
->cpu
= u
.val32
[0];
1781 if (type
& PERF_SAMPLE_PERIOD
) {
1782 data
->period
= *array
;
1786 if (type
& PERF_SAMPLE_READ
) {
1787 u64 read_format
= evsel
->attr
.read_format
;
1789 OVERFLOW_CHECK_u64(array
);
1790 if (read_format
& PERF_FORMAT_GROUP
)
1791 data
->read
.group
.nr
= *array
;
1793 data
->read
.one
.value
= *array
;
1797 if (read_format
& PERF_FORMAT_TOTAL_TIME_ENABLED
) {
1798 OVERFLOW_CHECK_u64(array
);
1799 data
->read
.time_enabled
= *array
;
1803 if (read_format
& PERF_FORMAT_TOTAL_TIME_RUNNING
) {
1804 OVERFLOW_CHECK_u64(array
);
1805 data
->read
.time_running
= *array
;
1809 /* PERF_FORMAT_ID is forced for PERF_SAMPLE_READ */
1810 if (read_format
& PERF_FORMAT_GROUP
) {
1811 const u64 max_group_nr
= UINT64_MAX
/
1812 sizeof(struct sample_read_value
);
1814 if (data
->read
.group
.nr
> max_group_nr
)
1816 sz
= data
->read
.group
.nr
*
1817 sizeof(struct sample_read_value
);
1818 OVERFLOW_CHECK(array
, sz
, max_size
);
1819 data
->read
.group
.values
=
1820 (struct sample_read_value
*)array
;
1821 array
= (void *)array
+ sz
;
1823 OVERFLOW_CHECK_u64(array
);
1824 data
->read
.one
.id
= *array
;
1829 if (type
& PERF_SAMPLE_CALLCHAIN
) {
1830 const u64 max_callchain_nr
= UINT64_MAX
/ sizeof(u64
);
1832 OVERFLOW_CHECK_u64(array
);
1833 data
->callchain
= (struct ip_callchain
*)array
++;
1834 if (data
->callchain
->nr
> max_callchain_nr
)
1836 sz
= data
->callchain
->nr
* sizeof(u64
);
1837 OVERFLOW_CHECK(array
, sz
, max_size
);
1838 array
= (void *)array
+ sz
;
1841 if (type
& PERF_SAMPLE_RAW
) {
1842 OVERFLOW_CHECK_u64(array
);
1844 if (WARN_ONCE(swapped
,
1845 "Endianness of raw data not corrected!\n")) {
1846 /* undo swap of u64, then swap on individual u32s */
1847 u
.val64
= bswap_64(u
.val64
);
1848 u
.val32
[0] = bswap_32(u
.val32
[0]);
1849 u
.val32
[1] = bswap_32(u
.val32
[1]);
1851 data
->raw_size
= u
.val32
[0];
1852 array
= (void *)array
+ sizeof(u32
);
1854 OVERFLOW_CHECK(array
, data
->raw_size
, max_size
);
1855 data
->raw_data
= (void *)array
;
1856 array
= (void *)array
+ data
->raw_size
;
1859 if (type
& PERF_SAMPLE_BRANCH_STACK
) {
1860 const u64 max_branch_nr
= UINT64_MAX
/
1861 sizeof(struct branch_entry
);
1863 OVERFLOW_CHECK_u64(array
);
1864 data
->branch_stack
= (struct branch_stack
*)array
++;
1866 if (data
->branch_stack
->nr
> max_branch_nr
)
1868 sz
= data
->branch_stack
->nr
* sizeof(struct branch_entry
);
1869 OVERFLOW_CHECK(array
, sz
, max_size
);
1870 array
= (void *)array
+ sz
;
1873 if (type
& PERF_SAMPLE_REGS_USER
) {
1874 OVERFLOW_CHECK_u64(array
);
1875 data
->user_regs
.abi
= *array
;
1878 if (data
->user_regs
.abi
) {
1879 u64 mask
= evsel
->attr
.sample_regs_user
;
1881 sz
= hweight_long(mask
) * sizeof(u64
);
1882 OVERFLOW_CHECK(array
, sz
, max_size
);
1883 data
->user_regs
.mask
= mask
;
1884 data
->user_regs
.regs
= (u64
*)array
;
1885 array
= (void *)array
+ sz
;
1889 if (type
& PERF_SAMPLE_STACK_USER
) {
1890 OVERFLOW_CHECK_u64(array
);
1893 data
->user_stack
.offset
= ((char *)(array
- 1)
1897 data
->user_stack
.size
= 0;
1899 OVERFLOW_CHECK(array
, sz
, max_size
);
1900 data
->user_stack
.data
= (char *)array
;
1901 array
= (void *)array
+ sz
;
1902 OVERFLOW_CHECK_u64(array
);
1903 data
->user_stack
.size
= *array
++;
1904 if (WARN_ONCE(data
->user_stack
.size
> sz
,
1905 "user stack dump failure\n"))
1911 if (type
& PERF_SAMPLE_WEIGHT
) {
1912 OVERFLOW_CHECK_u64(array
);
1913 data
->weight
= *array
;
1917 data
->data_src
= PERF_MEM_DATA_SRC_NONE
;
1918 if (type
& PERF_SAMPLE_DATA_SRC
) {
1919 OVERFLOW_CHECK_u64(array
);
1920 data
->data_src
= *array
;
1924 data
->transaction
= 0;
1925 if (type
& PERF_SAMPLE_TRANSACTION
) {
1926 OVERFLOW_CHECK_u64(array
);
1927 data
->transaction
= *array
;
1931 data
->intr_regs
.abi
= PERF_SAMPLE_REGS_ABI_NONE
;
1932 if (type
& PERF_SAMPLE_REGS_INTR
) {
1933 OVERFLOW_CHECK_u64(array
);
1934 data
->intr_regs
.abi
= *array
;
1937 if (data
->intr_regs
.abi
!= PERF_SAMPLE_REGS_ABI_NONE
) {
1938 u64 mask
= evsel
->attr
.sample_regs_intr
;
1940 sz
= hweight_long(mask
) * sizeof(u64
);
1941 OVERFLOW_CHECK(array
, sz
, max_size
);
1942 data
->intr_regs
.mask
= mask
;
1943 data
->intr_regs
.regs
= (u64
*)array
;
1944 array
= (void *)array
+ sz
;
1951 size_t perf_event__sample_event_size(const struct perf_sample
*sample
, u64 type
,
1954 size_t sz
, result
= sizeof(struct sample_event
);
1956 if (type
& PERF_SAMPLE_IDENTIFIER
)
1957 result
+= sizeof(u64
);
1959 if (type
& PERF_SAMPLE_IP
)
1960 result
+= sizeof(u64
);
1962 if (type
& PERF_SAMPLE_TID
)
1963 result
+= sizeof(u64
);
1965 if (type
& PERF_SAMPLE_TIME
)
1966 result
+= sizeof(u64
);
1968 if (type
& PERF_SAMPLE_ADDR
)
1969 result
+= sizeof(u64
);
1971 if (type
& PERF_SAMPLE_ID
)
1972 result
+= sizeof(u64
);
1974 if (type
& PERF_SAMPLE_STREAM_ID
)
1975 result
+= sizeof(u64
);
1977 if (type
& PERF_SAMPLE_CPU
)
1978 result
+= sizeof(u64
);
1980 if (type
& PERF_SAMPLE_PERIOD
)
1981 result
+= sizeof(u64
);
1983 if (type
& PERF_SAMPLE_READ
) {
1984 result
+= sizeof(u64
);
1985 if (read_format
& PERF_FORMAT_TOTAL_TIME_ENABLED
)
1986 result
+= sizeof(u64
);
1987 if (read_format
& PERF_FORMAT_TOTAL_TIME_RUNNING
)
1988 result
+= sizeof(u64
);
1989 /* PERF_FORMAT_ID is forced for PERF_SAMPLE_READ */
1990 if (read_format
& PERF_FORMAT_GROUP
) {
1991 sz
= sample
->read
.group
.nr
*
1992 sizeof(struct sample_read_value
);
1995 result
+= sizeof(u64
);
1999 if (type
& PERF_SAMPLE_CALLCHAIN
) {
2000 sz
= (sample
->callchain
->nr
+ 1) * sizeof(u64
);
2004 if (type
& PERF_SAMPLE_RAW
) {
2005 result
+= sizeof(u32
);
2006 result
+= sample
->raw_size
;
2009 if (type
& PERF_SAMPLE_BRANCH_STACK
) {
2010 sz
= sample
->branch_stack
->nr
* sizeof(struct branch_entry
);
2015 if (type
& PERF_SAMPLE_REGS_USER
) {
2016 if (sample
->user_regs
.abi
) {
2017 result
+= sizeof(u64
);
2018 sz
= hweight_long(sample
->user_regs
.mask
) * sizeof(u64
);
2021 result
+= sizeof(u64
);
2025 if (type
& PERF_SAMPLE_STACK_USER
) {
2026 sz
= sample
->user_stack
.size
;
2027 result
+= sizeof(u64
);
2030 result
+= sizeof(u64
);
2034 if (type
& PERF_SAMPLE_WEIGHT
)
2035 result
+= sizeof(u64
);
2037 if (type
& PERF_SAMPLE_DATA_SRC
)
2038 result
+= sizeof(u64
);
2040 if (type
& PERF_SAMPLE_TRANSACTION
)
2041 result
+= sizeof(u64
);
2043 if (type
& PERF_SAMPLE_REGS_INTR
) {
2044 if (sample
->intr_regs
.abi
) {
2045 result
+= sizeof(u64
);
2046 sz
= hweight_long(sample
->intr_regs
.mask
) * sizeof(u64
);
2049 result
+= sizeof(u64
);
2056 int perf_event__synthesize_sample(union perf_event
*event
, u64 type
,
2058 const struct perf_sample
*sample
,
2064 * used for cross-endian analysis. See git commit 65014ab3
2065 * for why this goofiness is needed.
2069 array
= event
->sample
.array
;
2071 if (type
& PERF_SAMPLE_IDENTIFIER
) {
2072 *array
= sample
->id
;
2076 if (type
& PERF_SAMPLE_IP
) {
2077 *array
= sample
->ip
;
2081 if (type
& PERF_SAMPLE_TID
) {
2082 u
.val32
[0] = sample
->pid
;
2083 u
.val32
[1] = sample
->tid
;
2086 * Inverse of what is done in perf_evsel__parse_sample
2088 u
.val32
[0] = bswap_32(u
.val32
[0]);
2089 u
.val32
[1] = bswap_32(u
.val32
[1]);
2090 u
.val64
= bswap_64(u
.val64
);
2097 if (type
& PERF_SAMPLE_TIME
) {
2098 *array
= sample
->time
;
2102 if (type
& PERF_SAMPLE_ADDR
) {
2103 *array
= sample
->addr
;
2107 if (type
& PERF_SAMPLE_ID
) {
2108 *array
= sample
->id
;
2112 if (type
& PERF_SAMPLE_STREAM_ID
) {
2113 *array
= sample
->stream_id
;
2117 if (type
& PERF_SAMPLE_CPU
) {
2118 u
.val32
[0] = sample
->cpu
;
2121 * Inverse of what is done in perf_evsel__parse_sample
2123 u
.val32
[0] = bswap_32(u
.val32
[0]);
2124 u
.val64
= bswap_64(u
.val64
);
2130 if (type
& PERF_SAMPLE_PERIOD
) {
2131 *array
= sample
->period
;
2135 if (type
& PERF_SAMPLE_READ
) {
2136 if (read_format
& PERF_FORMAT_GROUP
)
2137 *array
= sample
->read
.group
.nr
;
2139 *array
= sample
->read
.one
.value
;
2142 if (read_format
& PERF_FORMAT_TOTAL_TIME_ENABLED
) {
2143 *array
= sample
->read
.time_enabled
;
2147 if (read_format
& PERF_FORMAT_TOTAL_TIME_RUNNING
) {
2148 *array
= sample
->read
.time_running
;
2152 /* PERF_FORMAT_ID is forced for PERF_SAMPLE_READ */
2153 if (read_format
& PERF_FORMAT_GROUP
) {
2154 sz
= sample
->read
.group
.nr
*
2155 sizeof(struct sample_read_value
);
2156 memcpy(array
, sample
->read
.group
.values
, sz
);
2157 array
= (void *)array
+ sz
;
2159 *array
= sample
->read
.one
.id
;
2164 if (type
& PERF_SAMPLE_CALLCHAIN
) {
2165 sz
= (sample
->callchain
->nr
+ 1) * sizeof(u64
);
2166 memcpy(array
, sample
->callchain
, sz
);
2167 array
= (void *)array
+ sz
;
2170 if (type
& PERF_SAMPLE_RAW
) {
2171 u
.val32
[0] = sample
->raw_size
;
2172 if (WARN_ONCE(swapped
,
2173 "Endianness of raw data not corrected!\n")) {
2175 * Inverse of what is done in perf_evsel__parse_sample
2177 u
.val32
[0] = bswap_32(u
.val32
[0]);
2178 u
.val32
[1] = bswap_32(u
.val32
[1]);
2179 u
.val64
= bswap_64(u
.val64
);
2182 array
= (void *)array
+ sizeof(u32
);
2184 memcpy(array
, sample
->raw_data
, sample
->raw_size
);
2185 array
= (void *)array
+ sample
->raw_size
;
2188 if (type
& PERF_SAMPLE_BRANCH_STACK
) {
2189 sz
= sample
->branch_stack
->nr
* sizeof(struct branch_entry
);
2191 memcpy(array
, sample
->branch_stack
, sz
);
2192 array
= (void *)array
+ sz
;
2195 if (type
& PERF_SAMPLE_REGS_USER
) {
2196 if (sample
->user_regs
.abi
) {
2197 *array
++ = sample
->user_regs
.abi
;
2198 sz
= hweight_long(sample
->user_regs
.mask
) * sizeof(u64
);
2199 memcpy(array
, sample
->user_regs
.regs
, sz
);
2200 array
= (void *)array
+ sz
;
2206 if (type
& PERF_SAMPLE_STACK_USER
) {
2207 sz
= sample
->user_stack
.size
;
2210 memcpy(array
, sample
->user_stack
.data
, sz
);
2211 array
= (void *)array
+ sz
;
2216 if (type
& PERF_SAMPLE_WEIGHT
) {
2217 *array
= sample
->weight
;
2221 if (type
& PERF_SAMPLE_DATA_SRC
) {
2222 *array
= sample
->data_src
;
2226 if (type
& PERF_SAMPLE_TRANSACTION
) {
2227 *array
= sample
->transaction
;
2231 if (type
& PERF_SAMPLE_REGS_INTR
) {
2232 if (sample
->intr_regs
.abi
) {
2233 *array
++ = sample
->intr_regs
.abi
;
2234 sz
= hweight_long(sample
->intr_regs
.mask
) * sizeof(u64
);
2235 memcpy(array
, sample
->intr_regs
.regs
, sz
);
2236 array
= (void *)array
+ sz
;
2245 struct format_field
*perf_evsel__field(struct perf_evsel
*evsel
, const char *name
)
2247 return pevent_find_field(evsel
->tp_format
, name
);
2250 void *perf_evsel__rawptr(struct perf_evsel
*evsel
, struct perf_sample
*sample
,
2253 struct format_field
*field
= perf_evsel__field(evsel
, name
);
2259 offset
= field
->offset
;
2261 if (field
->flags
& FIELD_IS_DYNAMIC
) {
2262 offset
= *(int *)(sample
->raw_data
+ field
->offset
);
2266 return sample
->raw_data
+ offset
;
2269 u64
format_field__intval(struct format_field
*field
, struct perf_sample
*sample
,
2273 void *ptr
= sample
->raw_data
+ field
->offset
;
2275 switch (field
->size
) {
2279 value
= *(u16
*)ptr
;
2282 value
= *(u32
*)ptr
;
2285 memcpy(&value
, ptr
, sizeof(u64
));
2294 switch (field
->size
) {
2296 return bswap_16(value
);
2298 return bswap_32(value
);
2300 return bswap_64(value
);
2308 u64
perf_evsel__intval(struct perf_evsel
*evsel
, struct perf_sample
*sample
,
2311 struct format_field
*field
= perf_evsel__field(evsel
, name
);
2316 return field
? format_field__intval(field
, sample
, evsel
->needs_swap
) : 0;
2319 bool perf_evsel__fallback(struct perf_evsel
*evsel
, int err
,
2320 char *msg
, size_t msgsize
)
2324 if ((err
== ENOENT
|| err
== ENXIO
|| err
== ENODEV
) &&
2325 evsel
->attr
.type
== PERF_TYPE_HARDWARE
&&
2326 evsel
->attr
.config
== PERF_COUNT_HW_CPU_CYCLES
) {
2328 * If it's cycles then fall back to hrtimer based
2329 * cpu-clock-tick sw counter, which is always available even if
2332 * PPC returns ENXIO until 2.6.37 (behavior changed with commit
2335 scnprintf(msg
, msgsize
, "%s",
2336 "The cycles event is not supported, trying to fall back to cpu-clock-ticks");
2338 evsel
->attr
.type
= PERF_TYPE_SOFTWARE
;
2339 evsel
->attr
.config
= PERF_COUNT_SW_CPU_CLOCK
;
2341 zfree(&evsel
->name
);
2343 } else if (err
== EACCES
&& !evsel
->attr
.exclude_kernel
&&
2344 (paranoid
= perf_event_paranoid()) > 1) {
2345 const char *name
= perf_evsel__name(evsel
);
2348 if (asprintf(&new_name
, "%s%su", name
, strchr(name
, ':') ? "" : ":") < 0)
2353 evsel
->name
= new_name
;
2354 scnprintf(msg
, msgsize
,
2355 "kernel.perf_event_paranoid=%d, trying to fall back to excluding kernel samples", paranoid
);
2356 evsel
->attr
.exclude_kernel
= 1;
2364 int perf_evsel__open_strerror(struct perf_evsel
*evsel
, struct target
*target
,
2365 int err
, char *msg
, size_t size
)
2367 char sbuf
[STRERR_BUFSIZE
];
2372 return scnprintf(msg
, size
,
2373 "You may not have permission to collect %sstats.\n\n"
2374 "Consider tweaking /proc/sys/kernel/perf_event_paranoid,\n"
2375 "which controls use of the performance events system by\n"
2376 "unprivileged users (without CAP_SYS_ADMIN).\n\n"
2377 "The current value is %d:\n\n"
2378 " -1: Allow use of (almost) all events by all users\n"
2379 ">= 0: Disallow raw tracepoint access by users without CAP_IOC_LOCK\n"
2380 ">= 1: Disallow CPU event access by users without CAP_SYS_ADMIN\n"
2381 ">= 2: Disallow kernel profiling by users without CAP_SYS_ADMIN",
2382 target
->system_wide
? "system-wide " : "",
2383 perf_event_paranoid());
2385 return scnprintf(msg
, size
, "The %s event is not supported.",
2386 perf_evsel__name(evsel
));
2388 return scnprintf(msg
, size
, "%s",
2389 "Too many events are opened.\n"
2390 "Probably the maximum number of open file descriptors has been reached.\n"
2391 "Hint: Try again after reducing the number of events.\n"
2392 "Hint: Try increasing the limit with 'ulimit -n <limit>'");
2394 if ((evsel
->attr
.sample_type
& PERF_SAMPLE_CALLCHAIN
) != 0 &&
2395 access("/proc/sys/kernel/perf_event_max_stack", F_OK
) == 0)
2396 return scnprintf(msg
, size
,
2397 "Not enough memory to setup event with callchain.\n"
2398 "Hint: Try tweaking /proc/sys/kernel/perf_event_max_stack\n"
2399 "Hint: Current value: %d", sysctl_perf_event_max_stack
);
2402 if (target
->cpu_list
)
2403 return scnprintf(msg
, size
, "%s",
2404 "No such device - did you specify an out-of-range profile CPU?");
2407 if (evsel
->attr
.sample_period
!= 0)
2408 return scnprintf(msg
, size
, "%s",
2409 "PMU Hardware doesn't support sampling/overflow-interrupts.");
2410 if (evsel
->attr
.precise_ip
)
2411 return scnprintf(msg
, size
, "%s",
2412 "\'precise\' request may not be supported. Try removing 'p' modifier.");
2413 #if defined(__i386__) || defined(__x86_64__)
2414 if (evsel
->attr
.type
== PERF_TYPE_HARDWARE
)
2415 return scnprintf(msg
, size
, "%s",
2416 "No hardware sampling interrupt available.\n"
2417 "No APIC? If so then you can boot the kernel with the \"lapic\" boot parameter to force-enable it.");
2421 if (find_process("oprofiled"))
2422 return scnprintf(msg
, size
,
2423 "The PMU counters are busy/taken by another profiler.\n"
2424 "We found oprofile daemon running, please stop it and try again.");
2427 if (evsel
->attr
.write_backward
&& perf_missing_features
.write_backward
)
2428 return scnprintf(msg
, size
, "Reading from overwrite event is not supported by this kernel.");
2429 if (perf_missing_features
.clockid
)
2430 return scnprintf(msg
, size
, "clockid feature not supported.");
2431 if (perf_missing_features
.clockid_wrong
)
2432 return scnprintf(msg
, size
, "wrong clockid (%d).", clockid
);
2438 return scnprintf(msg
, size
,
2439 "The sys_perf_event_open() syscall returned with %d (%s) for event (%s).\n"
2440 "/bin/dmesg may provide additional information.\n"
2441 "No CONFIG_PERF_EVENTS=y kernel support configured?",
2442 err
, str_error_r(err
, sbuf
, sizeof(sbuf
)),
2443 perf_evsel__name(evsel
));
2446 char *perf_evsel__env_arch(struct perf_evsel
*evsel
)
2448 if (evsel
&& evsel
->evlist
&& evsel
->evlist
->env
)
2449 return evsel
->evlist
->env
->arch
;