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"
31 #include "util/parse-branch-options.h"
42 } perf_missing_features
;
44 static clockid_t clockid
;
46 static int perf_evsel__no_extra_init(struct perf_evsel
*evsel __maybe_unused
)
51 static void perf_evsel__no_extra_fini(struct perf_evsel
*evsel __maybe_unused
)
57 int (*init
)(struct perf_evsel
*evsel
);
58 void (*fini
)(struct perf_evsel
*evsel
);
59 } perf_evsel__object
= {
60 .size
= sizeof(struct perf_evsel
),
61 .init
= perf_evsel__no_extra_init
,
62 .fini
= perf_evsel__no_extra_fini
,
65 int perf_evsel__object_config(size_t object_size
,
66 int (*init
)(struct perf_evsel
*evsel
),
67 void (*fini
)(struct perf_evsel
*evsel
))
73 if (perf_evsel__object
.size
> object_size
)
76 perf_evsel__object
.size
= object_size
;
80 perf_evsel__object
.init
= init
;
83 perf_evsel__object
.fini
= fini
;
88 #define FD(e, x, y) (*(int *)xyarray__entry(e->fd, x, y))
90 int __perf_evsel__sample_size(u64 sample_type
)
92 u64 mask
= sample_type
& PERF_SAMPLE_MASK
;
96 for (i
= 0; i
< 64; i
++) {
97 if (mask
& (1ULL << i
))
107 * __perf_evsel__calc_id_pos - calculate id_pos.
108 * @sample_type: sample type
110 * This function returns the position of the event id (PERF_SAMPLE_ID or
111 * PERF_SAMPLE_IDENTIFIER) in a sample event i.e. in the array of struct
114 static int __perf_evsel__calc_id_pos(u64 sample_type
)
118 if (sample_type
& PERF_SAMPLE_IDENTIFIER
)
121 if (!(sample_type
& PERF_SAMPLE_ID
))
124 if (sample_type
& PERF_SAMPLE_IP
)
127 if (sample_type
& PERF_SAMPLE_TID
)
130 if (sample_type
& PERF_SAMPLE_TIME
)
133 if (sample_type
& PERF_SAMPLE_ADDR
)
140 * __perf_evsel__calc_is_pos - calculate is_pos.
141 * @sample_type: sample type
143 * This function returns the position (counting backwards) of the event id
144 * (PERF_SAMPLE_ID or PERF_SAMPLE_IDENTIFIER) in a non-sample event i.e. if
145 * sample_id_all is used there is an id sample appended to non-sample events.
147 static int __perf_evsel__calc_is_pos(u64 sample_type
)
151 if (sample_type
& PERF_SAMPLE_IDENTIFIER
)
154 if (!(sample_type
& PERF_SAMPLE_ID
))
157 if (sample_type
& PERF_SAMPLE_CPU
)
160 if (sample_type
& PERF_SAMPLE_STREAM_ID
)
166 void perf_evsel__calc_id_pos(struct perf_evsel
*evsel
)
168 evsel
->id_pos
= __perf_evsel__calc_id_pos(evsel
->attr
.sample_type
);
169 evsel
->is_pos
= __perf_evsel__calc_is_pos(evsel
->attr
.sample_type
);
172 void __perf_evsel__set_sample_bit(struct perf_evsel
*evsel
,
173 enum perf_event_sample_format bit
)
175 if (!(evsel
->attr
.sample_type
& bit
)) {
176 evsel
->attr
.sample_type
|= bit
;
177 evsel
->sample_size
+= sizeof(u64
);
178 perf_evsel__calc_id_pos(evsel
);
182 void __perf_evsel__reset_sample_bit(struct perf_evsel
*evsel
,
183 enum perf_event_sample_format bit
)
185 if (evsel
->attr
.sample_type
& bit
) {
186 evsel
->attr
.sample_type
&= ~bit
;
187 evsel
->sample_size
-= sizeof(u64
);
188 perf_evsel__calc_id_pos(evsel
);
192 void perf_evsel__set_sample_id(struct perf_evsel
*evsel
,
193 bool can_sample_identifier
)
195 if (can_sample_identifier
) {
196 perf_evsel__reset_sample_bit(evsel
, ID
);
197 perf_evsel__set_sample_bit(evsel
, IDENTIFIER
);
199 perf_evsel__set_sample_bit(evsel
, ID
);
201 evsel
->attr
.read_format
|= PERF_FORMAT_ID
;
205 * perf_evsel__is_function_event - Return whether given evsel is a function
208 * @evsel - evsel selector to be tested
210 * Return %true if event is function trace event
212 bool perf_evsel__is_function_event(struct perf_evsel
*evsel
)
214 #define FUNCTION_EVENT "ftrace:function"
216 return evsel
->name
&&
217 !strncmp(FUNCTION_EVENT
, evsel
->name
, sizeof(FUNCTION_EVENT
));
219 #undef FUNCTION_EVENT
222 void perf_evsel__init(struct perf_evsel
*evsel
,
223 struct perf_event_attr
*attr
, int idx
)
226 evsel
->tracking
= !idx
;
228 evsel
->leader
= evsel
;
231 evsel
->evlist
= NULL
;
233 INIT_LIST_HEAD(&evsel
->node
);
234 INIT_LIST_HEAD(&evsel
->config_terms
);
235 perf_evsel__object
.init(evsel
);
236 evsel
->sample_size
= __perf_evsel__sample_size(attr
->sample_type
);
237 perf_evsel__calc_id_pos(evsel
);
238 evsel
->cmdline_group_boundary
= false;
239 evsel
->metric_expr
= NULL
;
240 evsel
->metric_name
= NULL
;
241 evsel
->metric_events
= NULL
;
242 evsel
->collect_stat
= false;
245 struct perf_evsel
*perf_evsel__new_idx(struct perf_event_attr
*attr
, int idx
)
247 struct perf_evsel
*evsel
= zalloc(perf_evsel__object
.size
);
250 perf_evsel__init(evsel
, attr
, idx
);
252 if (perf_evsel__is_bpf_output(evsel
)) {
253 evsel
->attr
.sample_type
|= (PERF_SAMPLE_RAW
| PERF_SAMPLE_TIME
|
254 PERF_SAMPLE_CPU
| PERF_SAMPLE_PERIOD
),
255 evsel
->attr
.sample_period
= 1;
261 struct perf_evsel
*perf_evsel__new_cycles(void)
263 struct perf_event_attr attr
= {
264 .type
= PERF_TYPE_HARDWARE
,
265 .config
= PERF_COUNT_HW_CPU_CYCLES
,
267 struct perf_evsel
*evsel
;
269 event_attr_init(&attr
);
271 perf_event_attr__set_max_precise_ip(&attr
);
273 evsel
= perf_evsel__new(&attr
);
277 /* use asprintf() because free(evsel) assumes name is allocated */
278 if (asprintf(&evsel
->name
, "cycles%.*s",
279 attr
.precise_ip
? attr
.precise_ip
+ 1 : 0, ":ppp") < 0)
284 perf_evsel__delete(evsel
);
290 * Returns pointer with encoded error via <linux/err.h> interface.
292 struct perf_evsel
*perf_evsel__newtp_idx(const char *sys
, const char *name
, int idx
)
294 struct perf_evsel
*evsel
= zalloc(perf_evsel__object
.size
);
300 struct perf_event_attr attr
= {
301 .type
= PERF_TYPE_TRACEPOINT
,
302 .sample_type
= (PERF_SAMPLE_RAW
| PERF_SAMPLE_TIME
|
303 PERF_SAMPLE_CPU
| PERF_SAMPLE_PERIOD
),
306 if (asprintf(&evsel
->name
, "%s:%s", sys
, name
) < 0)
309 evsel
->tp_format
= trace_event__tp_format(sys
, name
);
310 if (IS_ERR(evsel
->tp_format
)) {
311 err
= PTR_ERR(evsel
->tp_format
);
315 event_attr_init(&attr
);
316 attr
.config
= evsel
->tp_format
->id
;
317 attr
.sample_period
= 1;
318 perf_evsel__init(evsel
, &attr
, idx
);
330 const char *perf_evsel__hw_names
[PERF_COUNT_HW_MAX
] = {
338 "stalled-cycles-frontend",
339 "stalled-cycles-backend",
343 static const char *__perf_evsel__hw_name(u64 config
)
345 if (config
< PERF_COUNT_HW_MAX
&& perf_evsel__hw_names
[config
])
346 return perf_evsel__hw_names
[config
];
348 return "unknown-hardware";
351 static int perf_evsel__add_modifiers(struct perf_evsel
*evsel
, char *bf
, size_t size
)
353 int colon
= 0, r
= 0;
354 struct perf_event_attr
*attr
= &evsel
->attr
;
355 bool exclude_guest_default
= false;
357 #define MOD_PRINT(context, mod) do { \
358 if (!attr->exclude_##context) { \
359 if (!colon) colon = ++r; \
360 r += scnprintf(bf + r, size - r, "%c", mod); \
363 if (attr
->exclude_kernel
|| attr
->exclude_user
|| attr
->exclude_hv
) {
364 MOD_PRINT(kernel
, 'k');
365 MOD_PRINT(user
, 'u');
367 exclude_guest_default
= true;
370 if (attr
->precise_ip
) {
373 r
+= scnprintf(bf
+ r
, size
- r
, "%.*s", attr
->precise_ip
, "ppp");
374 exclude_guest_default
= true;
377 if (attr
->exclude_host
|| attr
->exclude_guest
== exclude_guest_default
) {
378 MOD_PRINT(host
, 'H');
379 MOD_PRINT(guest
, 'G');
387 static int perf_evsel__hw_name(struct perf_evsel
*evsel
, char *bf
, size_t size
)
389 int r
= scnprintf(bf
, size
, "%s", __perf_evsel__hw_name(evsel
->attr
.config
));
390 return r
+ perf_evsel__add_modifiers(evsel
, bf
+ r
, size
- r
);
393 const char *perf_evsel__sw_names
[PERF_COUNT_SW_MAX
] = {
406 static const char *__perf_evsel__sw_name(u64 config
)
408 if (config
< PERF_COUNT_SW_MAX
&& perf_evsel__sw_names
[config
])
409 return perf_evsel__sw_names
[config
];
410 return "unknown-software";
413 static int perf_evsel__sw_name(struct perf_evsel
*evsel
, char *bf
, size_t size
)
415 int r
= scnprintf(bf
, size
, "%s", __perf_evsel__sw_name(evsel
->attr
.config
));
416 return r
+ perf_evsel__add_modifiers(evsel
, bf
+ r
, size
- r
);
419 static int __perf_evsel__bp_name(char *bf
, size_t size
, u64 addr
, u64 type
)
423 r
= scnprintf(bf
, size
, "mem:0x%" PRIx64
":", addr
);
425 if (type
& HW_BREAKPOINT_R
)
426 r
+= scnprintf(bf
+ r
, size
- r
, "r");
428 if (type
& HW_BREAKPOINT_W
)
429 r
+= scnprintf(bf
+ r
, size
- r
, "w");
431 if (type
& HW_BREAKPOINT_X
)
432 r
+= scnprintf(bf
+ r
, size
- r
, "x");
437 static int perf_evsel__bp_name(struct perf_evsel
*evsel
, char *bf
, size_t size
)
439 struct perf_event_attr
*attr
= &evsel
->attr
;
440 int r
= __perf_evsel__bp_name(bf
, size
, attr
->bp_addr
, attr
->bp_type
);
441 return r
+ perf_evsel__add_modifiers(evsel
, bf
+ r
, size
- r
);
444 const char *perf_evsel__hw_cache
[PERF_COUNT_HW_CACHE_MAX
]
445 [PERF_EVSEL__MAX_ALIASES
] = {
446 { "L1-dcache", "l1-d", "l1d", "L1-data", },
447 { "L1-icache", "l1-i", "l1i", "L1-instruction", },
449 { "dTLB", "d-tlb", "Data-TLB", },
450 { "iTLB", "i-tlb", "Instruction-TLB", },
451 { "branch", "branches", "bpu", "btb", "bpc", },
455 const char *perf_evsel__hw_cache_op
[PERF_COUNT_HW_CACHE_OP_MAX
]
456 [PERF_EVSEL__MAX_ALIASES
] = {
457 { "load", "loads", "read", },
458 { "store", "stores", "write", },
459 { "prefetch", "prefetches", "speculative-read", "speculative-load", },
462 const char *perf_evsel__hw_cache_result
[PERF_COUNT_HW_CACHE_RESULT_MAX
]
463 [PERF_EVSEL__MAX_ALIASES
] = {
464 { "refs", "Reference", "ops", "access", },
465 { "misses", "miss", },
468 #define C(x) PERF_COUNT_HW_CACHE_##x
469 #define CACHE_READ (1 << C(OP_READ))
470 #define CACHE_WRITE (1 << C(OP_WRITE))
471 #define CACHE_PREFETCH (1 << C(OP_PREFETCH))
472 #define COP(x) (1 << x)
475 * cache operartion stat
476 * L1I : Read and prefetch only
477 * ITLB and BPU : Read-only
479 static unsigned long perf_evsel__hw_cache_stat
[C(MAX
)] = {
480 [C(L1D
)] = (CACHE_READ
| CACHE_WRITE
| CACHE_PREFETCH
),
481 [C(L1I
)] = (CACHE_READ
| CACHE_PREFETCH
),
482 [C(LL
)] = (CACHE_READ
| CACHE_WRITE
| CACHE_PREFETCH
),
483 [C(DTLB
)] = (CACHE_READ
| CACHE_WRITE
| CACHE_PREFETCH
),
484 [C(ITLB
)] = (CACHE_READ
),
485 [C(BPU
)] = (CACHE_READ
),
486 [C(NODE
)] = (CACHE_READ
| CACHE_WRITE
| CACHE_PREFETCH
),
489 bool perf_evsel__is_cache_op_valid(u8 type
, u8 op
)
491 if (perf_evsel__hw_cache_stat
[type
] & COP(op
))
492 return true; /* valid */
494 return false; /* invalid */
497 int __perf_evsel__hw_cache_type_op_res_name(u8 type
, u8 op
, u8 result
,
498 char *bf
, size_t size
)
501 return scnprintf(bf
, size
, "%s-%s-%s", perf_evsel__hw_cache
[type
][0],
502 perf_evsel__hw_cache_op
[op
][0],
503 perf_evsel__hw_cache_result
[result
][0]);
506 return scnprintf(bf
, size
, "%s-%s", perf_evsel__hw_cache
[type
][0],
507 perf_evsel__hw_cache_op
[op
][1]);
510 static int __perf_evsel__hw_cache_name(u64 config
, char *bf
, size_t size
)
512 u8 op
, result
, type
= (config
>> 0) & 0xff;
513 const char *err
= "unknown-ext-hardware-cache-type";
515 if (type
>= PERF_COUNT_HW_CACHE_MAX
)
518 op
= (config
>> 8) & 0xff;
519 err
= "unknown-ext-hardware-cache-op";
520 if (op
>= PERF_COUNT_HW_CACHE_OP_MAX
)
523 result
= (config
>> 16) & 0xff;
524 err
= "unknown-ext-hardware-cache-result";
525 if (result
>= PERF_COUNT_HW_CACHE_RESULT_MAX
)
528 err
= "invalid-cache";
529 if (!perf_evsel__is_cache_op_valid(type
, op
))
532 return __perf_evsel__hw_cache_type_op_res_name(type
, op
, result
, bf
, size
);
534 return scnprintf(bf
, size
, "%s", err
);
537 static int perf_evsel__hw_cache_name(struct perf_evsel
*evsel
, char *bf
, size_t size
)
539 int ret
= __perf_evsel__hw_cache_name(evsel
->attr
.config
, bf
, size
);
540 return ret
+ perf_evsel__add_modifiers(evsel
, bf
+ ret
, size
- ret
);
543 static int perf_evsel__raw_name(struct perf_evsel
*evsel
, char *bf
, size_t size
)
545 int ret
= scnprintf(bf
, size
, "raw 0x%" PRIx64
, evsel
->attr
.config
);
546 return ret
+ perf_evsel__add_modifiers(evsel
, bf
+ ret
, size
- ret
);
549 const char *perf_evsel__name(struct perf_evsel
*evsel
)
556 switch (evsel
->attr
.type
) {
558 perf_evsel__raw_name(evsel
, bf
, sizeof(bf
));
561 case PERF_TYPE_HARDWARE
:
562 perf_evsel__hw_name(evsel
, bf
, sizeof(bf
));
565 case PERF_TYPE_HW_CACHE
:
566 perf_evsel__hw_cache_name(evsel
, bf
, sizeof(bf
));
569 case PERF_TYPE_SOFTWARE
:
570 perf_evsel__sw_name(evsel
, bf
, sizeof(bf
));
573 case PERF_TYPE_TRACEPOINT
:
574 scnprintf(bf
, sizeof(bf
), "%s", "unknown tracepoint");
577 case PERF_TYPE_BREAKPOINT
:
578 perf_evsel__bp_name(evsel
, bf
, sizeof(bf
));
582 scnprintf(bf
, sizeof(bf
), "unknown attr type: %d",
587 evsel
->name
= strdup(bf
);
589 return evsel
->name
?: "unknown";
592 const char *perf_evsel__group_name(struct perf_evsel
*evsel
)
594 return evsel
->group_name
?: "anon group";
597 int perf_evsel__group_desc(struct perf_evsel
*evsel
, char *buf
, size_t size
)
600 struct perf_evsel
*pos
;
601 const char *group_name
= perf_evsel__group_name(evsel
);
603 ret
= scnprintf(buf
, size
, "%s", group_name
);
605 ret
+= scnprintf(buf
+ ret
, size
- ret
, " { %s",
606 perf_evsel__name(evsel
));
608 for_each_group_member(pos
, evsel
)
609 ret
+= scnprintf(buf
+ ret
, size
- ret
, ", %s",
610 perf_evsel__name(pos
));
612 ret
+= scnprintf(buf
+ ret
, size
- ret
, " }");
617 void perf_evsel__config_callchain(struct perf_evsel
*evsel
,
618 struct record_opts
*opts
,
619 struct callchain_param
*param
)
621 bool function
= perf_evsel__is_function_event(evsel
);
622 struct perf_event_attr
*attr
= &evsel
->attr
;
624 perf_evsel__set_sample_bit(evsel
, CALLCHAIN
);
626 attr
->sample_max_stack
= param
->max_stack
;
628 if (param
->record_mode
== CALLCHAIN_LBR
) {
629 if (!opts
->branch_stack
) {
630 if (attr
->exclude_user
) {
631 pr_warning("LBR callstack option is only available "
632 "to get user callchain information. "
633 "Falling back to framepointers.\n");
635 perf_evsel__set_sample_bit(evsel
, BRANCH_STACK
);
636 attr
->branch_sample_type
= PERF_SAMPLE_BRANCH_USER
|
637 PERF_SAMPLE_BRANCH_CALL_STACK
|
638 PERF_SAMPLE_BRANCH_NO_CYCLES
|
639 PERF_SAMPLE_BRANCH_NO_FLAGS
;
642 pr_warning("Cannot use LBR callstack with branch stack. "
643 "Falling back to framepointers.\n");
646 if (param
->record_mode
== CALLCHAIN_DWARF
) {
648 perf_evsel__set_sample_bit(evsel
, REGS_USER
);
649 perf_evsel__set_sample_bit(evsel
, STACK_USER
);
650 attr
->sample_regs_user
= PERF_REGS_MASK
;
651 attr
->sample_stack_user
= param
->dump_size
;
652 attr
->exclude_callchain_user
= 1;
654 pr_info("Cannot use DWARF unwind for function trace event,"
655 " falling back to framepointers.\n");
660 pr_info("Disabling user space callchains for function trace event.\n");
661 attr
->exclude_callchain_user
= 1;
666 perf_evsel__reset_callgraph(struct perf_evsel
*evsel
,
667 struct callchain_param
*param
)
669 struct perf_event_attr
*attr
= &evsel
->attr
;
671 perf_evsel__reset_sample_bit(evsel
, CALLCHAIN
);
672 if (param
->record_mode
== CALLCHAIN_LBR
) {
673 perf_evsel__reset_sample_bit(evsel
, BRANCH_STACK
);
674 attr
->branch_sample_type
&= ~(PERF_SAMPLE_BRANCH_USER
|
675 PERF_SAMPLE_BRANCH_CALL_STACK
);
677 if (param
->record_mode
== CALLCHAIN_DWARF
) {
678 perf_evsel__reset_sample_bit(evsel
, REGS_USER
);
679 perf_evsel__reset_sample_bit(evsel
, STACK_USER
);
683 static void apply_config_terms(struct perf_evsel
*evsel
,
684 struct record_opts
*opts
)
686 struct perf_evsel_config_term
*term
;
687 struct list_head
*config_terms
= &evsel
->config_terms
;
688 struct perf_event_attr
*attr
= &evsel
->attr
;
689 struct callchain_param param
;
692 const char *callgraph_buf
= NULL
;
694 /* callgraph default */
695 param
.record_mode
= callchain_param
.record_mode
;
697 list_for_each_entry(term
, config_terms
, list
) {
698 switch (term
->type
) {
699 case PERF_EVSEL__CONFIG_TERM_PERIOD
:
700 attr
->sample_period
= term
->val
.period
;
703 case PERF_EVSEL__CONFIG_TERM_FREQ
:
704 attr
->sample_freq
= term
->val
.freq
;
707 case PERF_EVSEL__CONFIG_TERM_TIME
:
709 perf_evsel__set_sample_bit(evsel
, TIME
);
711 perf_evsel__reset_sample_bit(evsel
, TIME
);
713 case PERF_EVSEL__CONFIG_TERM_CALLGRAPH
:
714 callgraph_buf
= term
->val
.callgraph
;
716 case PERF_EVSEL__CONFIG_TERM_BRANCH
:
717 if (term
->val
.branch
&& strcmp(term
->val
.branch
, "no")) {
718 perf_evsel__set_sample_bit(evsel
, BRANCH_STACK
);
719 parse_branch_str(term
->val
.branch
,
720 &attr
->branch_sample_type
);
722 perf_evsel__reset_sample_bit(evsel
, BRANCH_STACK
);
724 case PERF_EVSEL__CONFIG_TERM_STACK_USER
:
725 dump_size
= term
->val
.stack_user
;
727 case PERF_EVSEL__CONFIG_TERM_MAX_STACK
:
728 max_stack
= term
->val
.max_stack
;
730 case PERF_EVSEL__CONFIG_TERM_INHERIT
:
732 * attr->inherit should has already been set by
733 * perf_evsel__config. If user explicitly set
734 * inherit using config terms, override global
735 * opt->no_inherit setting.
737 attr
->inherit
= term
->val
.inherit
? 1 : 0;
739 case PERF_EVSEL__CONFIG_TERM_OVERWRITE
:
740 attr
->write_backward
= term
->val
.overwrite
? 1 : 0;
747 /* User explicitly set per-event callgraph, clear the old setting and reset. */
748 if ((callgraph_buf
!= NULL
) || (dump_size
> 0) || max_stack
) {
750 param
.max_stack
= max_stack
;
751 if (callgraph_buf
== NULL
)
752 callgraph_buf
= "fp";
755 /* parse callgraph parameters */
756 if (callgraph_buf
!= NULL
) {
757 if (!strcmp(callgraph_buf
, "no")) {
758 param
.enabled
= false;
759 param
.record_mode
= CALLCHAIN_NONE
;
761 param
.enabled
= true;
762 if (parse_callchain_record(callgraph_buf
, ¶m
)) {
763 pr_err("per-event callgraph setting for %s failed. "
764 "Apply callgraph global setting for it\n",
771 dump_size
= round_up(dump_size
, sizeof(u64
));
772 param
.dump_size
= dump_size
;
775 /* If global callgraph set, clear it */
776 if (callchain_param
.enabled
)
777 perf_evsel__reset_callgraph(evsel
, &callchain_param
);
779 /* set perf-event callgraph */
781 perf_evsel__config_callchain(evsel
, opts
, ¶m
);
786 * The enable_on_exec/disabled value strategy:
788 * 1) For any type of traced program:
789 * - all independent events and group leaders are disabled
790 * - all group members are enabled
792 * Group members are ruled by group leaders. They need to
793 * be enabled, because the group scheduling relies on that.
795 * 2) For traced programs executed by perf:
796 * - all independent events and group leaders have
798 * - we don't specifically enable or disable any event during
801 * Independent events and group leaders are initially disabled
802 * and get enabled by exec. Group members are ruled by group
803 * leaders as stated in 1).
805 * 3) For traced programs attached by perf (pid/tid):
806 * - we specifically enable or disable all events during
809 * When attaching events to already running traced we
810 * enable/disable events specifically, as there's no
811 * initial traced exec call.
813 void perf_evsel__config(struct perf_evsel
*evsel
, struct record_opts
*opts
,
814 struct callchain_param
*callchain
)
816 struct perf_evsel
*leader
= evsel
->leader
;
817 struct perf_event_attr
*attr
= &evsel
->attr
;
818 int track
= evsel
->tracking
;
819 bool per_cpu
= opts
->target
.default_per_cpu
&& !opts
->target
.per_thread
;
821 attr
->sample_id_all
= perf_missing_features
.sample_id_all
? 0 : 1;
822 attr
->inherit
= !opts
->no_inherit
;
823 attr
->write_backward
= opts
->overwrite
? 1 : 0;
825 perf_evsel__set_sample_bit(evsel
, IP
);
826 perf_evsel__set_sample_bit(evsel
, TID
);
828 if (evsel
->sample_read
) {
829 perf_evsel__set_sample_bit(evsel
, READ
);
832 * We need ID even in case of single event, because
833 * PERF_SAMPLE_READ process ID specific data.
835 perf_evsel__set_sample_id(evsel
, false);
838 * Apply group format only if we belong to group
839 * with more than one members.
841 if (leader
->nr_members
> 1) {
842 attr
->read_format
|= PERF_FORMAT_GROUP
;
848 * We default some events to have a default interval. But keep
849 * it a weak assumption overridable by the user.
851 if (!attr
->sample_period
|| (opts
->user_freq
!= UINT_MAX
||
852 opts
->user_interval
!= ULLONG_MAX
)) {
854 perf_evsel__set_sample_bit(evsel
, PERIOD
);
856 attr
->sample_freq
= opts
->freq
;
858 attr
->sample_period
= opts
->default_interval
;
863 * Disable sampling for all group members other
864 * than leader in case leader 'leads' the sampling.
866 if ((leader
!= evsel
) && leader
->sample_read
) {
867 attr
->sample_freq
= 0;
868 attr
->sample_period
= 0;
871 if (opts
->no_samples
)
872 attr
->sample_freq
= 0;
874 if (opts
->inherit_stat
)
875 attr
->inherit_stat
= 1;
877 if (opts
->sample_address
) {
878 perf_evsel__set_sample_bit(evsel
, ADDR
);
879 attr
->mmap_data
= track
;
883 * We don't allow user space callchains for function trace
884 * event, due to issues with page faults while tracing page
885 * fault handler and its overall trickiness nature.
887 if (perf_evsel__is_function_event(evsel
))
888 evsel
->attr
.exclude_callchain_user
= 1;
890 if (callchain
&& callchain
->enabled
&& !evsel
->no_aux_samples
)
891 perf_evsel__config_callchain(evsel
, opts
, callchain
);
893 if (opts
->sample_intr_regs
) {
894 attr
->sample_regs_intr
= opts
->sample_intr_regs
;
895 perf_evsel__set_sample_bit(evsel
, REGS_INTR
);
898 if (target__has_cpu(&opts
->target
) || opts
->sample_cpu
)
899 perf_evsel__set_sample_bit(evsel
, CPU
);
902 perf_evsel__set_sample_bit(evsel
, PERIOD
);
905 * When the user explicitly disabled time don't force it here.
907 if (opts
->sample_time
&&
908 (!perf_missing_features
.sample_id_all
&&
909 (!opts
->no_inherit
|| target__has_cpu(&opts
->target
) || per_cpu
||
910 opts
->sample_time_set
)))
911 perf_evsel__set_sample_bit(evsel
, TIME
);
913 if (opts
->raw_samples
&& !evsel
->no_aux_samples
) {
914 perf_evsel__set_sample_bit(evsel
, TIME
);
915 perf_evsel__set_sample_bit(evsel
, RAW
);
916 perf_evsel__set_sample_bit(evsel
, CPU
);
919 if (opts
->sample_address
)
920 perf_evsel__set_sample_bit(evsel
, DATA_SRC
);
922 if (opts
->no_buffering
) {
924 attr
->wakeup_events
= 1;
926 if (opts
->branch_stack
&& !evsel
->no_aux_samples
) {
927 perf_evsel__set_sample_bit(evsel
, BRANCH_STACK
);
928 attr
->branch_sample_type
= opts
->branch_stack
;
931 if (opts
->sample_weight
)
932 perf_evsel__set_sample_bit(evsel
, WEIGHT
);
936 attr
->mmap2
= track
&& !perf_missing_features
.mmap2
;
939 if (opts
->record_namespaces
)
940 attr
->namespaces
= track
;
942 if (opts
->record_switch_events
)
943 attr
->context_switch
= track
;
945 if (opts
->sample_transaction
)
946 perf_evsel__set_sample_bit(evsel
, TRANSACTION
);
948 if (opts
->running_time
) {
949 evsel
->attr
.read_format
|=
950 PERF_FORMAT_TOTAL_TIME_ENABLED
|
951 PERF_FORMAT_TOTAL_TIME_RUNNING
;
955 * XXX see the function comment above
957 * Disabling only independent events or group leaders,
958 * keeping group members enabled.
960 if (perf_evsel__is_group_leader(evsel
))
964 * Setting enable_on_exec for independent events and
965 * group leaders for traced executed by perf.
967 if (target__none(&opts
->target
) && perf_evsel__is_group_leader(evsel
) &&
968 !opts
->initial_delay
)
969 attr
->enable_on_exec
= 1;
971 if (evsel
->immediate
) {
973 attr
->enable_on_exec
= 0;
976 clockid
= opts
->clockid
;
977 if (opts
->use_clockid
) {
978 attr
->use_clockid
= 1;
979 attr
->clockid
= opts
->clockid
;
982 if (evsel
->precise_max
)
983 perf_event_attr__set_max_precise_ip(attr
);
985 if (opts
->all_user
) {
986 attr
->exclude_kernel
= 1;
987 attr
->exclude_user
= 0;
990 if (opts
->all_kernel
) {
991 attr
->exclude_kernel
= 0;
992 attr
->exclude_user
= 1;
996 * Apply event specific term settings,
997 * it overloads any global configuration.
999 apply_config_terms(evsel
, opts
);
1001 evsel
->ignore_missing_thread
= opts
->ignore_missing_thread
;
1004 static int perf_evsel__alloc_fd(struct perf_evsel
*evsel
, int ncpus
, int nthreads
)
1006 if (evsel
->system_wide
)
1009 evsel
->fd
= xyarray__new(ncpus
, nthreads
, sizeof(int));
1013 for (cpu
= 0; cpu
< ncpus
; cpu
++) {
1014 for (thread
= 0; thread
< nthreads
; thread
++) {
1015 FD(evsel
, cpu
, thread
) = -1;
1020 return evsel
->fd
!= NULL
? 0 : -ENOMEM
;
1023 static int perf_evsel__run_ioctl(struct perf_evsel
*evsel
, int ncpus
, int nthreads
,
1028 if (evsel
->system_wide
)
1031 for (cpu
= 0; cpu
< ncpus
; cpu
++) {
1032 for (thread
= 0; thread
< nthreads
; thread
++) {
1033 int fd
= FD(evsel
, cpu
, thread
),
1034 err
= ioctl(fd
, ioc
, arg
);
1044 int perf_evsel__apply_filter(struct perf_evsel
*evsel
, int ncpus
, int nthreads
,
1047 return perf_evsel__run_ioctl(evsel
, ncpus
, nthreads
,
1048 PERF_EVENT_IOC_SET_FILTER
,
1052 int perf_evsel__set_filter(struct perf_evsel
*evsel
, const char *filter
)
1054 char *new_filter
= strdup(filter
);
1056 if (new_filter
!= NULL
) {
1057 free(evsel
->filter
);
1058 evsel
->filter
= new_filter
;
1065 static int perf_evsel__append_filter(struct perf_evsel
*evsel
,
1066 const char *fmt
, const char *filter
)
1070 if (evsel
->filter
== NULL
)
1071 return perf_evsel__set_filter(evsel
, filter
);
1073 if (asprintf(&new_filter
, fmt
, evsel
->filter
, filter
) > 0) {
1074 free(evsel
->filter
);
1075 evsel
->filter
= new_filter
;
1082 int perf_evsel__append_tp_filter(struct perf_evsel
*evsel
, const char *filter
)
1084 return perf_evsel__append_filter(evsel
, "(%s) && (%s)", filter
);
1087 int perf_evsel__append_addr_filter(struct perf_evsel
*evsel
, const char *filter
)
1089 return perf_evsel__append_filter(evsel
, "%s,%s", filter
);
1092 int perf_evsel__enable(struct perf_evsel
*evsel
)
1094 int nthreads
= thread_map__nr(evsel
->threads
);
1095 int ncpus
= cpu_map__nr(evsel
->cpus
);
1097 return perf_evsel__run_ioctl(evsel
, ncpus
, nthreads
,
1098 PERF_EVENT_IOC_ENABLE
,
1102 int perf_evsel__disable(struct perf_evsel
*evsel
)
1104 int nthreads
= thread_map__nr(evsel
->threads
);
1105 int ncpus
= cpu_map__nr(evsel
->cpus
);
1107 return perf_evsel__run_ioctl(evsel
, ncpus
, nthreads
,
1108 PERF_EVENT_IOC_DISABLE
,
1112 int perf_evsel__alloc_id(struct perf_evsel
*evsel
, int ncpus
, int nthreads
)
1114 if (ncpus
== 0 || nthreads
== 0)
1117 if (evsel
->system_wide
)
1120 evsel
->sample_id
= xyarray__new(ncpus
, nthreads
, sizeof(struct perf_sample_id
));
1121 if (evsel
->sample_id
== NULL
)
1124 evsel
->id
= zalloc(ncpus
* nthreads
* sizeof(u64
));
1125 if (evsel
->id
== NULL
) {
1126 xyarray__delete(evsel
->sample_id
);
1127 evsel
->sample_id
= NULL
;
1134 static void perf_evsel__free_fd(struct perf_evsel
*evsel
)
1136 xyarray__delete(evsel
->fd
);
1140 static void perf_evsel__free_id(struct perf_evsel
*evsel
)
1142 xyarray__delete(evsel
->sample_id
);
1143 evsel
->sample_id
= NULL
;
1147 static void perf_evsel__free_config_terms(struct perf_evsel
*evsel
)
1149 struct perf_evsel_config_term
*term
, *h
;
1151 list_for_each_entry_safe(term
, h
, &evsel
->config_terms
, list
) {
1152 list_del(&term
->list
);
1157 void perf_evsel__close_fd(struct perf_evsel
*evsel
, int ncpus
, int nthreads
)
1161 if (evsel
->system_wide
)
1164 for (cpu
= 0; cpu
< ncpus
; cpu
++)
1165 for (thread
= 0; thread
< nthreads
; ++thread
) {
1166 close(FD(evsel
, cpu
, thread
));
1167 FD(evsel
, cpu
, thread
) = -1;
1171 void perf_evsel__exit(struct perf_evsel
*evsel
)
1173 assert(list_empty(&evsel
->node
));
1174 assert(evsel
->evlist
== NULL
);
1175 perf_evsel__free_fd(evsel
);
1176 perf_evsel__free_id(evsel
);
1177 perf_evsel__free_config_terms(evsel
);
1178 close_cgroup(evsel
->cgrp
);
1179 cpu_map__put(evsel
->cpus
);
1180 cpu_map__put(evsel
->own_cpus
);
1181 thread_map__put(evsel
->threads
);
1182 zfree(&evsel
->group_name
);
1183 zfree(&evsel
->name
);
1184 perf_evsel__object
.fini(evsel
);
1187 void perf_evsel__delete(struct perf_evsel
*evsel
)
1189 perf_evsel__exit(evsel
);
1193 void perf_evsel__compute_deltas(struct perf_evsel
*evsel
, int cpu
, int thread
,
1194 struct perf_counts_values
*count
)
1196 struct perf_counts_values tmp
;
1198 if (!evsel
->prev_raw_counts
)
1202 tmp
= evsel
->prev_raw_counts
->aggr
;
1203 evsel
->prev_raw_counts
->aggr
= *count
;
1205 tmp
= *perf_counts(evsel
->prev_raw_counts
, cpu
, thread
);
1206 *perf_counts(evsel
->prev_raw_counts
, cpu
, thread
) = *count
;
1209 count
->val
= count
->val
- tmp
.val
;
1210 count
->ena
= count
->ena
- tmp
.ena
;
1211 count
->run
= count
->run
- tmp
.run
;
1214 void perf_counts_values__scale(struct perf_counts_values
*count
,
1215 bool scale
, s8
*pscaled
)
1220 if (count
->run
== 0) {
1223 } else if (count
->run
< count
->ena
) {
1225 count
->val
= (u64
)((double) count
->val
* count
->ena
/ count
->run
+ 0.5);
1228 count
->ena
= count
->run
= 0;
1234 int perf_evsel__read(struct perf_evsel
*evsel
, int cpu
, int thread
,
1235 struct perf_counts_values
*count
)
1237 memset(count
, 0, sizeof(*count
));
1239 if (FD(evsel
, cpu
, thread
) < 0)
1242 if (readn(FD(evsel
, cpu
, thread
), count
, sizeof(*count
)) <= 0)
1248 int __perf_evsel__read_on_cpu(struct perf_evsel
*evsel
,
1249 int cpu
, int thread
, bool scale
)
1251 struct perf_counts_values count
;
1252 size_t nv
= scale
? 3 : 1;
1254 if (FD(evsel
, cpu
, thread
) < 0)
1257 if (evsel
->counts
== NULL
&& perf_evsel__alloc_counts(evsel
, cpu
+ 1, thread
+ 1) < 0)
1260 if (readn(FD(evsel
, cpu
, thread
), &count
, nv
* sizeof(u64
)) <= 0)
1263 perf_evsel__compute_deltas(evsel
, cpu
, thread
, &count
);
1264 perf_counts_values__scale(&count
, scale
, NULL
);
1265 *perf_counts(evsel
->counts
, cpu
, thread
) = count
;
1269 static int get_group_fd(struct perf_evsel
*evsel
, int cpu
, int thread
)
1271 struct perf_evsel
*leader
= evsel
->leader
;
1274 if (perf_evsel__is_group_leader(evsel
))
1278 * Leader must be already processed/open,
1279 * if not it's a bug.
1281 BUG_ON(!leader
->fd
);
1283 fd
= FD(leader
, cpu
, thread
);
1294 static void __p_bits(char *buf
, size_t size
, u64 value
, struct bit_names
*bits
)
1296 bool first_bit
= true;
1300 if (value
& bits
[i
].bit
) {
1301 buf
+= scnprintf(buf
, size
, "%s%s", first_bit
? "" : "|", bits
[i
].name
);
1304 } while (bits
[++i
].name
!= NULL
);
1307 static void __p_sample_type(char *buf
, size_t size
, u64 value
)
1309 #define bit_name(n) { PERF_SAMPLE_##n, #n }
1310 struct bit_names bits
[] = {
1311 bit_name(IP
), bit_name(TID
), bit_name(TIME
), bit_name(ADDR
),
1312 bit_name(READ
), bit_name(CALLCHAIN
), bit_name(ID
), bit_name(CPU
),
1313 bit_name(PERIOD
), bit_name(STREAM_ID
), bit_name(RAW
),
1314 bit_name(BRANCH_STACK
), bit_name(REGS_USER
), bit_name(STACK_USER
),
1315 bit_name(IDENTIFIER
), bit_name(REGS_INTR
), bit_name(DATA_SRC
),
1320 __p_bits(buf
, size
, value
, bits
);
1323 static void __p_branch_sample_type(char *buf
, size_t size
, u64 value
)
1325 #define bit_name(n) { PERF_SAMPLE_BRANCH_##n, #n }
1326 struct bit_names bits
[] = {
1327 bit_name(USER
), bit_name(KERNEL
), bit_name(HV
), bit_name(ANY
),
1328 bit_name(ANY_CALL
), bit_name(ANY_RETURN
), bit_name(IND_CALL
),
1329 bit_name(ABORT_TX
), bit_name(IN_TX
), bit_name(NO_TX
),
1330 bit_name(COND
), bit_name(CALL_STACK
), bit_name(IND_JUMP
),
1331 bit_name(CALL
), bit_name(NO_FLAGS
), bit_name(NO_CYCLES
),
1335 __p_bits(buf
, size
, value
, bits
);
1338 static void __p_read_format(char *buf
, size_t size
, u64 value
)
1340 #define bit_name(n) { PERF_FORMAT_##n, #n }
1341 struct bit_names bits
[] = {
1342 bit_name(TOTAL_TIME_ENABLED
), bit_name(TOTAL_TIME_RUNNING
),
1343 bit_name(ID
), bit_name(GROUP
),
1347 __p_bits(buf
, size
, value
, bits
);
1350 #define BUF_SIZE 1024
1352 #define p_hex(val) snprintf(buf, BUF_SIZE, "%#"PRIx64, (uint64_t)(val))
1353 #define p_unsigned(val) snprintf(buf, BUF_SIZE, "%"PRIu64, (uint64_t)(val))
1354 #define p_signed(val) snprintf(buf, BUF_SIZE, "%"PRId64, (int64_t)(val))
1355 #define p_sample_type(val) __p_sample_type(buf, BUF_SIZE, val)
1356 #define p_branch_sample_type(val) __p_branch_sample_type(buf, BUF_SIZE, val)
1357 #define p_read_format(val) __p_read_format(buf, BUF_SIZE, val)
1359 #define PRINT_ATTRn(_n, _f, _p) \
1363 ret += attr__fprintf(fp, _n, buf, priv);\
1367 #define PRINT_ATTRf(_f, _p) PRINT_ATTRn(#_f, _f, _p)
1369 int perf_event_attr__fprintf(FILE *fp
, struct perf_event_attr
*attr
,
1370 attr__fprintf_f attr__fprintf
, void *priv
)
1375 PRINT_ATTRf(type
, p_unsigned
);
1376 PRINT_ATTRf(size
, p_unsigned
);
1377 PRINT_ATTRf(config
, p_hex
);
1378 PRINT_ATTRn("{ sample_period, sample_freq }", sample_period
, p_unsigned
);
1379 PRINT_ATTRf(sample_type
, p_sample_type
);
1380 PRINT_ATTRf(read_format
, p_read_format
);
1382 PRINT_ATTRf(disabled
, p_unsigned
);
1383 PRINT_ATTRf(inherit
, p_unsigned
);
1384 PRINT_ATTRf(pinned
, p_unsigned
);
1385 PRINT_ATTRf(exclusive
, p_unsigned
);
1386 PRINT_ATTRf(exclude_user
, p_unsigned
);
1387 PRINT_ATTRf(exclude_kernel
, p_unsigned
);
1388 PRINT_ATTRf(exclude_hv
, p_unsigned
);
1389 PRINT_ATTRf(exclude_idle
, p_unsigned
);
1390 PRINT_ATTRf(mmap
, p_unsigned
);
1391 PRINT_ATTRf(comm
, p_unsigned
);
1392 PRINT_ATTRf(freq
, p_unsigned
);
1393 PRINT_ATTRf(inherit_stat
, p_unsigned
);
1394 PRINT_ATTRf(enable_on_exec
, p_unsigned
);
1395 PRINT_ATTRf(task
, p_unsigned
);
1396 PRINT_ATTRf(watermark
, p_unsigned
);
1397 PRINT_ATTRf(precise_ip
, p_unsigned
);
1398 PRINT_ATTRf(mmap_data
, p_unsigned
);
1399 PRINT_ATTRf(sample_id_all
, p_unsigned
);
1400 PRINT_ATTRf(exclude_host
, p_unsigned
);
1401 PRINT_ATTRf(exclude_guest
, p_unsigned
);
1402 PRINT_ATTRf(exclude_callchain_kernel
, p_unsigned
);
1403 PRINT_ATTRf(exclude_callchain_user
, p_unsigned
);
1404 PRINT_ATTRf(mmap2
, p_unsigned
);
1405 PRINT_ATTRf(comm_exec
, p_unsigned
);
1406 PRINT_ATTRf(use_clockid
, p_unsigned
);
1407 PRINT_ATTRf(context_switch
, p_unsigned
);
1408 PRINT_ATTRf(write_backward
, p_unsigned
);
1410 PRINT_ATTRn("{ wakeup_events, wakeup_watermark }", wakeup_events
, p_unsigned
);
1411 PRINT_ATTRf(bp_type
, p_unsigned
);
1412 PRINT_ATTRn("{ bp_addr, config1 }", bp_addr
, p_hex
);
1413 PRINT_ATTRn("{ bp_len, config2 }", bp_len
, p_hex
);
1414 PRINT_ATTRf(branch_sample_type
, p_branch_sample_type
);
1415 PRINT_ATTRf(sample_regs_user
, p_hex
);
1416 PRINT_ATTRf(sample_stack_user
, p_unsigned
);
1417 PRINT_ATTRf(clockid
, p_signed
);
1418 PRINT_ATTRf(sample_regs_intr
, p_hex
);
1419 PRINT_ATTRf(aux_watermark
, p_unsigned
);
1420 PRINT_ATTRf(sample_max_stack
, p_unsigned
);
1425 static int __open_attr__fprintf(FILE *fp
, const char *name
, const char *val
,
1426 void *priv
__attribute__((unused
)))
1428 return fprintf(fp
, " %-32s %s\n", name
, val
);
1431 static bool ignore_missing_thread(struct perf_evsel
*evsel
,
1432 struct thread_map
*threads
,
1433 int thread
, int err
)
1435 if (!evsel
->ignore_missing_thread
)
1438 /* The system wide setup does not work with threads. */
1439 if (evsel
->system_wide
)
1442 /* The -ESRCH is perf event syscall errno for pid's not found. */
1446 /* If there's only one thread, let it fail. */
1447 if (threads
->nr
== 1)
1450 if (thread_map__remove(threads
, thread
))
1453 pr_warning("WARNING: Ignored open failure for pid %d\n",
1454 thread_map__pid(threads
, thread
));
1458 int perf_evsel__open(struct perf_evsel
*evsel
, struct cpu_map
*cpus
,
1459 struct thread_map
*threads
)
1461 int cpu
, thread
, nthreads
;
1462 unsigned long flags
= PERF_FLAG_FD_CLOEXEC
;
1464 enum { NO_CHANGE
, SET_TO_MAX
, INCREASED_MAX
} set_rlimit
= NO_CHANGE
;
1466 if (perf_missing_features
.write_backward
&& evsel
->attr
.write_backward
)
1470 static struct cpu_map
*empty_cpu_map
;
1472 if (empty_cpu_map
== NULL
) {
1473 empty_cpu_map
= cpu_map__dummy_new();
1474 if (empty_cpu_map
== NULL
)
1478 cpus
= empty_cpu_map
;
1481 if (threads
== NULL
) {
1482 static struct thread_map
*empty_thread_map
;
1484 if (empty_thread_map
== NULL
) {
1485 empty_thread_map
= thread_map__new_by_tid(-1);
1486 if (empty_thread_map
== NULL
)
1490 threads
= empty_thread_map
;
1493 if (evsel
->system_wide
)
1496 nthreads
= threads
->nr
;
1498 if (evsel
->fd
== NULL
&&
1499 perf_evsel__alloc_fd(evsel
, cpus
->nr
, nthreads
) < 0)
1503 flags
|= PERF_FLAG_PID_CGROUP
;
1504 pid
= evsel
->cgrp
->fd
;
1507 fallback_missing_features
:
1508 if (perf_missing_features
.clockid_wrong
)
1509 evsel
->attr
.clockid
= CLOCK_MONOTONIC
; /* should always work */
1510 if (perf_missing_features
.clockid
) {
1511 evsel
->attr
.use_clockid
= 0;
1512 evsel
->attr
.clockid
= 0;
1514 if (perf_missing_features
.cloexec
)
1515 flags
&= ~(unsigned long)PERF_FLAG_FD_CLOEXEC
;
1516 if (perf_missing_features
.mmap2
)
1517 evsel
->attr
.mmap2
= 0;
1518 if (perf_missing_features
.exclude_guest
)
1519 evsel
->attr
.exclude_guest
= evsel
->attr
.exclude_host
= 0;
1520 if (perf_missing_features
.lbr_flags
)
1521 evsel
->attr
.branch_sample_type
&= ~(PERF_SAMPLE_BRANCH_NO_FLAGS
|
1522 PERF_SAMPLE_BRANCH_NO_CYCLES
);
1524 if (perf_missing_features
.sample_id_all
)
1525 evsel
->attr
.sample_id_all
= 0;
1528 fprintf(stderr
, "%.60s\n", graph_dotted_line
);
1529 fprintf(stderr
, "perf_event_attr:\n");
1530 perf_event_attr__fprintf(stderr
, &evsel
->attr
, __open_attr__fprintf
, NULL
);
1531 fprintf(stderr
, "%.60s\n", graph_dotted_line
);
1534 for (cpu
= 0; cpu
< cpus
->nr
; cpu
++) {
1536 for (thread
= 0; thread
< nthreads
; thread
++) {
1539 if (!evsel
->cgrp
&& !evsel
->system_wide
)
1540 pid
= thread_map__pid(threads
, thread
);
1542 group_fd
= get_group_fd(evsel
, cpu
, thread
);
1544 pr_debug2("sys_perf_event_open: pid %d cpu %d group_fd %d flags %#lx",
1545 pid
, cpus
->map
[cpu
], group_fd
, flags
);
1547 fd
= sys_perf_event_open(&evsel
->attr
, pid
, cpus
->map
[cpu
],
1550 FD(evsel
, cpu
, thread
) = fd
;
1555 if (ignore_missing_thread(evsel
, threads
, thread
, err
)) {
1557 * We just removed 1 thread, so take a step
1558 * back on thread index and lower the upper
1564 /* ... and pretend like nothing have happened. */
1569 pr_debug2("\nsys_perf_event_open failed, error %d\n",
1574 pr_debug2(" = %d\n", fd
);
1576 if (evsel
->bpf_fd
>= 0) {
1578 int bpf_fd
= evsel
->bpf_fd
;
1581 PERF_EVENT_IOC_SET_BPF
,
1583 if (err
&& errno
!= EEXIST
) {
1584 pr_err("failed to attach bpf fd %d: %s\n",
1585 bpf_fd
, strerror(errno
));
1591 set_rlimit
= NO_CHANGE
;
1594 * If we succeeded but had to kill clockid, fail and
1595 * have perf_evsel__open_strerror() print us a nice
1598 if (perf_missing_features
.clockid
||
1599 perf_missing_features
.clockid_wrong
) {
1610 * perf stat needs between 5 and 22 fds per CPU. When we run out
1611 * of them try to increase the limits.
1613 if (err
== -EMFILE
&& set_rlimit
< INCREASED_MAX
) {
1615 int old_errno
= errno
;
1617 if (getrlimit(RLIMIT_NOFILE
, &l
) == 0) {
1618 if (set_rlimit
== NO_CHANGE
)
1619 l
.rlim_cur
= l
.rlim_max
;
1621 l
.rlim_cur
= l
.rlim_max
+ 1000;
1622 l
.rlim_max
= l
.rlim_cur
;
1624 if (setrlimit(RLIMIT_NOFILE
, &l
) == 0) {
1633 if (err
!= -EINVAL
|| cpu
> 0 || thread
> 0)
1637 * Must probe features in the order they were added to the
1638 * perf_event_attr interface.
1640 if (!perf_missing_features
.write_backward
&& evsel
->attr
.write_backward
) {
1641 perf_missing_features
.write_backward
= true;
1643 } else if (!perf_missing_features
.clockid_wrong
&& evsel
->attr
.use_clockid
) {
1644 perf_missing_features
.clockid_wrong
= true;
1645 goto fallback_missing_features
;
1646 } else if (!perf_missing_features
.clockid
&& evsel
->attr
.use_clockid
) {
1647 perf_missing_features
.clockid
= true;
1648 goto fallback_missing_features
;
1649 } else if (!perf_missing_features
.cloexec
&& (flags
& PERF_FLAG_FD_CLOEXEC
)) {
1650 perf_missing_features
.cloexec
= true;
1651 goto fallback_missing_features
;
1652 } else if (!perf_missing_features
.mmap2
&& evsel
->attr
.mmap2
) {
1653 perf_missing_features
.mmap2
= true;
1654 goto fallback_missing_features
;
1655 } else if (!perf_missing_features
.exclude_guest
&&
1656 (evsel
->attr
.exclude_guest
|| evsel
->attr
.exclude_host
)) {
1657 perf_missing_features
.exclude_guest
= true;
1658 goto fallback_missing_features
;
1659 } else if (!perf_missing_features
.sample_id_all
) {
1660 perf_missing_features
.sample_id_all
= true;
1661 goto retry_sample_id
;
1662 } else if (!perf_missing_features
.lbr_flags
&&
1663 (evsel
->attr
.branch_sample_type
&
1664 (PERF_SAMPLE_BRANCH_NO_CYCLES
|
1665 PERF_SAMPLE_BRANCH_NO_FLAGS
))) {
1666 perf_missing_features
.lbr_flags
= true;
1667 goto fallback_missing_features
;
1671 while (--thread
>= 0) {
1672 close(FD(evsel
, cpu
, thread
));
1673 FD(evsel
, cpu
, thread
) = -1;
1676 } while (--cpu
>= 0);
1680 void perf_evsel__close(struct perf_evsel
*evsel
, int ncpus
, int nthreads
)
1682 if (evsel
->fd
== NULL
)
1685 perf_evsel__close_fd(evsel
, ncpus
, nthreads
);
1686 perf_evsel__free_fd(evsel
);
1689 int perf_evsel__open_per_cpu(struct perf_evsel
*evsel
,
1690 struct cpu_map
*cpus
)
1692 return perf_evsel__open(evsel
, cpus
, NULL
);
1695 int perf_evsel__open_per_thread(struct perf_evsel
*evsel
,
1696 struct thread_map
*threads
)
1698 return perf_evsel__open(evsel
, NULL
, threads
);
1701 static int perf_evsel__parse_id_sample(const struct perf_evsel
*evsel
,
1702 const union perf_event
*event
,
1703 struct perf_sample
*sample
)
1705 u64 type
= evsel
->attr
.sample_type
;
1706 const u64
*array
= event
->sample
.array
;
1707 bool swapped
= evsel
->needs_swap
;
1710 array
+= ((event
->header
.size
-
1711 sizeof(event
->header
)) / sizeof(u64
)) - 1;
1713 if (type
& PERF_SAMPLE_IDENTIFIER
) {
1714 sample
->id
= *array
;
1718 if (type
& PERF_SAMPLE_CPU
) {
1721 /* undo swap of u64, then swap on individual u32s */
1722 u
.val64
= bswap_64(u
.val64
);
1723 u
.val32
[0] = bswap_32(u
.val32
[0]);
1726 sample
->cpu
= u
.val32
[0];
1730 if (type
& PERF_SAMPLE_STREAM_ID
) {
1731 sample
->stream_id
= *array
;
1735 if (type
& PERF_SAMPLE_ID
) {
1736 sample
->id
= *array
;
1740 if (type
& PERF_SAMPLE_TIME
) {
1741 sample
->time
= *array
;
1745 if (type
& PERF_SAMPLE_TID
) {
1748 /* undo swap of u64, then swap on individual u32s */
1749 u
.val64
= bswap_64(u
.val64
);
1750 u
.val32
[0] = bswap_32(u
.val32
[0]);
1751 u
.val32
[1] = bswap_32(u
.val32
[1]);
1754 sample
->pid
= u
.val32
[0];
1755 sample
->tid
= u
.val32
[1];
1762 static inline bool overflow(const void *endp
, u16 max_size
, const void *offset
,
1765 return size
> max_size
|| offset
+ size
> endp
;
1768 #define OVERFLOW_CHECK(offset, size, max_size) \
1770 if (overflow(endp, (max_size), (offset), (size))) \
1774 #define OVERFLOW_CHECK_u64(offset) \
1775 OVERFLOW_CHECK(offset, sizeof(u64), sizeof(u64))
1777 int perf_evsel__parse_sample(struct perf_evsel
*evsel
, union perf_event
*event
,
1778 struct perf_sample
*data
)
1780 u64 type
= evsel
->attr
.sample_type
;
1781 bool swapped
= evsel
->needs_swap
;
1783 u16 max_size
= event
->header
.size
;
1784 const void *endp
= (void *)event
+ max_size
;
1788 * used for cross-endian analysis. See git commit 65014ab3
1789 * for why this goofiness is needed.
1793 memset(data
, 0, sizeof(*data
));
1794 data
->cpu
= data
->pid
= data
->tid
= -1;
1795 data
->stream_id
= data
->id
= data
->time
= -1ULL;
1796 data
->period
= evsel
->attr
.sample_period
;
1797 data
->cpumode
= event
->header
.misc
& PERF_RECORD_MISC_CPUMODE_MASK
;
1799 if (event
->header
.type
!= PERF_RECORD_SAMPLE
) {
1800 if (!evsel
->attr
.sample_id_all
)
1802 return perf_evsel__parse_id_sample(evsel
, event
, data
);
1805 array
= event
->sample
.array
;
1808 * The evsel's sample_size is based on PERF_SAMPLE_MASK which includes
1809 * up to PERF_SAMPLE_PERIOD. After that overflow() must be used to
1810 * check the format does not go past the end of the event.
1812 if (evsel
->sample_size
+ sizeof(event
->header
) > event
->header
.size
)
1816 if (type
& PERF_SAMPLE_IDENTIFIER
) {
1821 if (type
& PERF_SAMPLE_IP
) {
1826 if (type
& PERF_SAMPLE_TID
) {
1829 /* undo swap of u64, then swap on individual u32s */
1830 u
.val64
= bswap_64(u
.val64
);
1831 u
.val32
[0] = bswap_32(u
.val32
[0]);
1832 u
.val32
[1] = bswap_32(u
.val32
[1]);
1835 data
->pid
= u
.val32
[0];
1836 data
->tid
= u
.val32
[1];
1840 if (type
& PERF_SAMPLE_TIME
) {
1841 data
->time
= *array
;
1846 if (type
& PERF_SAMPLE_ADDR
) {
1847 data
->addr
= *array
;
1851 if (type
& PERF_SAMPLE_ID
) {
1856 if (type
& PERF_SAMPLE_STREAM_ID
) {
1857 data
->stream_id
= *array
;
1861 if (type
& PERF_SAMPLE_CPU
) {
1865 /* undo swap of u64, then swap on individual u32s */
1866 u
.val64
= bswap_64(u
.val64
);
1867 u
.val32
[0] = bswap_32(u
.val32
[0]);
1870 data
->cpu
= u
.val32
[0];
1874 if (type
& PERF_SAMPLE_PERIOD
) {
1875 data
->period
= *array
;
1879 if (type
& PERF_SAMPLE_READ
) {
1880 u64 read_format
= evsel
->attr
.read_format
;
1882 OVERFLOW_CHECK_u64(array
);
1883 if (read_format
& PERF_FORMAT_GROUP
)
1884 data
->read
.group
.nr
= *array
;
1886 data
->read
.one
.value
= *array
;
1890 if (read_format
& PERF_FORMAT_TOTAL_TIME_ENABLED
) {
1891 OVERFLOW_CHECK_u64(array
);
1892 data
->read
.time_enabled
= *array
;
1896 if (read_format
& PERF_FORMAT_TOTAL_TIME_RUNNING
) {
1897 OVERFLOW_CHECK_u64(array
);
1898 data
->read
.time_running
= *array
;
1902 /* PERF_FORMAT_ID is forced for PERF_SAMPLE_READ */
1903 if (read_format
& PERF_FORMAT_GROUP
) {
1904 const u64 max_group_nr
= UINT64_MAX
/
1905 sizeof(struct sample_read_value
);
1907 if (data
->read
.group
.nr
> max_group_nr
)
1909 sz
= data
->read
.group
.nr
*
1910 sizeof(struct sample_read_value
);
1911 OVERFLOW_CHECK(array
, sz
, max_size
);
1912 data
->read
.group
.values
=
1913 (struct sample_read_value
*)array
;
1914 array
= (void *)array
+ sz
;
1916 OVERFLOW_CHECK_u64(array
);
1917 data
->read
.one
.id
= *array
;
1922 if (type
& PERF_SAMPLE_CALLCHAIN
) {
1923 const u64 max_callchain_nr
= UINT64_MAX
/ sizeof(u64
);
1925 OVERFLOW_CHECK_u64(array
);
1926 data
->callchain
= (struct ip_callchain
*)array
++;
1927 if (data
->callchain
->nr
> max_callchain_nr
)
1929 sz
= data
->callchain
->nr
* sizeof(u64
);
1930 OVERFLOW_CHECK(array
, sz
, max_size
);
1931 array
= (void *)array
+ sz
;
1934 if (type
& PERF_SAMPLE_RAW
) {
1935 OVERFLOW_CHECK_u64(array
);
1937 if (WARN_ONCE(swapped
,
1938 "Endianness of raw data not corrected!\n")) {
1939 /* undo swap of u64, then swap on individual u32s */
1940 u
.val64
= bswap_64(u
.val64
);
1941 u
.val32
[0] = bswap_32(u
.val32
[0]);
1942 u
.val32
[1] = bswap_32(u
.val32
[1]);
1944 data
->raw_size
= u
.val32
[0];
1945 array
= (void *)array
+ sizeof(u32
);
1947 OVERFLOW_CHECK(array
, data
->raw_size
, max_size
);
1948 data
->raw_data
= (void *)array
;
1949 array
= (void *)array
+ data
->raw_size
;
1952 if (type
& PERF_SAMPLE_BRANCH_STACK
) {
1953 const u64 max_branch_nr
= UINT64_MAX
/
1954 sizeof(struct branch_entry
);
1956 OVERFLOW_CHECK_u64(array
);
1957 data
->branch_stack
= (struct branch_stack
*)array
++;
1959 if (data
->branch_stack
->nr
> max_branch_nr
)
1961 sz
= data
->branch_stack
->nr
* sizeof(struct branch_entry
);
1962 OVERFLOW_CHECK(array
, sz
, max_size
);
1963 array
= (void *)array
+ sz
;
1966 if (type
& PERF_SAMPLE_REGS_USER
) {
1967 OVERFLOW_CHECK_u64(array
);
1968 data
->user_regs
.abi
= *array
;
1971 if (data
->user_regs
.abi
) {
1972 u64 mask
= evsel
->attr
.sample_regs_user
;
1974 sz
= hweight_long(mask
) * sizeof(u64
);
1975 OVERFLOW_CHECK(array
, sz
, max_size
);
1976 data
->user_regs
.mask
= mask
;
1977 data
->user_regs
.regs
= (u64
*)array
;
1978 array
= (void *)array
+ sz
;
1982 if (type
& PERF_SAMPLE_STACK_USER
) {
1983 OVERFLOW_CHECK_u64(array
);
1986 data
->user_stack
.offset
= ((char *)(array
- 1)
1990 data
->user_stack
.size
= 0;
1992 OVERFLOW_CHECK(array
, sz
, max_size
);
1993 data
->user_stack
.data
= (char *)array
;
1994 array
= (void *)array
+ sz
;
1995 OVERFLOW_CHECK_u64(array
);
1996 data
->user_stack
.size
= *array
++;
1997 if (WARN_ONCE(data
->user_stack
.size
> sz
,
1998 "user stack dump failure\n"))
2003 if (type
& PERF_SAMPLE_WEIGHT
) {
2004 OVERFLOW_CHECK_u64(array
);
2005 data
->weight
= *array
;
2009 data
->data_src
= PERF_MEM_DATA_SRC_NONE
;
2010 if (type
& PERF_SAMPLE_DATA_SRC
) {
2011 OVERFLOW_CHECK_u64(array
);
2012 data
->data_src
= *array
;
2016 data
->transaction
= 0;
2017 if (type
& PERF_SAMPLE_TRANSACTION
) {
2018 OVERFLOW_CHECK_u64(array
);
2019 data
->transaction
= *array
;
2023 data
->intr_regs
.abi
= PERF_SAMPLE_REGS_ABI_NONE
;
2024 if (type
& PERF_SAMPLE_REGS_INTR
) {
2025 OVERFLOW_CHECK_u64(array
);
2026 data
->intr_regs
.abi
= *array
;
2029 if (data
->intr_regs
.abi
!= PERF_SAMPLE_REGS_ABI_NONE
) {
2030 u64 mask
= evsel
->attr
.sample_regs_intr
;
2032 sz
= hweight_long(mask
) * sizeof(u64
);
2033 OVERFLOW_CHECK(array
, sz
, max_size
);
2034 data
->intr_regs
.mask
= mask
;
2035 data
->intr_regs
.regs
= (u64
*)array
;
2036 array
= (void *)array
+ sz
;
2043 size_t perf_event__sample_event_size(const struct perf_sample
*sample
, u64 type
,
2046 size_t sz
, result
= sizeof(struct sample_event
);
2048 if (type
& PERF_SAMPLE_IDENTIFIER
)
2049 result
+= sizeof(u64
);
2051 if (type
& PERF_SAMPLE_IP
)
2052 result
+= sizeof(u64
);
2054 if (type
& PERF_SAMPLE_TID
)
2055 result
+= sizeof(u64
);
2057 if (type
& PERF_SAMPLE_TIME
)
2058 result
+= sizeof(u64
);
2060 if (type
& PERF_SAMPLE_ADDR
)
2061 result
+= sizeof(u64
);
2063 if (type
& PERF_SAMPLE_ID
)
2064 result
+= sizeof(u64
);
2066 if (type
& PERF_SAMPLE_STREAM_ID
)
2067 result
+= sizeof(u64
);
2069 if (type
& PERF_SAMPLE_CPU
)
2070 result
+= sizeof(u64
);
2072 if (type
& PERF_SAMPLE_PERIOD
)
2073 result
+= sizeof(u64
);
2075 if (type
& PERF_SAMPLE_READ
) {
2076 result
+= sizeof(u64
);
2077 if (read_format
& PERF_FORMAT_TOTAL_TIME_ENABLED
)
2078 result
+= sizeof(u64
);
2079 if (read_format
& PERF_FORMAT_TOTAL_TIME_RUNNING
)
2080 result
+= sizeof(u64
);
2081 /* PERF_FORMAT_ID is forced for PERF_SAMPLE_READ */
2082 if (read_format
& PERF_FORMAT_GROUP
) {
2083 sz
= sample
->read
.group
.nr
*
2084 sizeof(struct sample_read_value
);
2087 result
+= sizeof(u64
);
2091 if (type
& PERF_SAMPLE_CALLCHAIN
) {
2092 sz
= (sample
->callchain
->nr
+ 1) * sizeof(u64
);
2096 if (type
& PERF_SAMPLE_RAW
) {
2097 result
+= sizeof(u32
);
2098 result
+= sample
->raw_size
;
2101 if (type
& PERF_SAMPLE_BRANCH_STACK
) {
2102 sz
= sample
->branch_stack
->nr
* sizeof(struct branch_entry
);
2107 if (type
& PERF_SAMPLE_REGS_USER
) {
2108 if (sample
->user_regs
.abi
) {
2109 result
+= sizeof(u64
);
2110 sz
= hweight_long(sample
->user_regs
.mask
) * sizeof(u64
);
2113 result
+= sizeof(u64
);
2117 if (type
& PERF_SAMPLE_STACK_USER
) {
2118 sz
= sample
->user_stack
.size
;
2119 result
+= sizeof(u64
);
2122 result
+= sizeof(u64
);
2126 if (type
& PERF_SAMPLE_WEIGHT
)
2127 result
+= sizeof(u64
);
2129 if (type
& PERF_SAMPLE_DATA_SRC
)
2130 result
+= sizeof(u64
);
2132 if (type
& PERF_SAMPLE_TRANSACTION
)
2133 result
+= sizeof(u64
);
2135 if (type
& PERF_SAMPLE_REGS_INTR
) {
2136 if (sample
->intr_regs
.abi
) {
2137 result
+= sizeof(u64
);
2138 sz
= hweight_long(sample
->intr_regs
.mask
) * sizeof(u64
);
2141 result
+= sizeof(u64
);
2148 int perf_event__synthesize_sample(union perf_event
*event
, u64 type
,
2150 const struct perf_sample
*sample
,
2156 * used for cross-endian analysis. See git commit 65014ab3
2157 * for why this goofiness is needed.
2161 array
= event
->sample
.array
;
2163 if (type
& PERF_SAMPLE_IDENTIFIER
) {
2164 *array
= sample
->id
;
2168 if (type
& PERF_SAMPLE_IP
) {
2169 *array
= sample
->ip
;
2173 if (type
& PERF_SAMPLE_TID
) {
2174 u
.val32
[0] = sample
->pid
;
2175 u
.val32
[1] = sample
->tid
;
2178 * Inverse of what is done in perf_evsel__parse_sample
2180 u
.val32
[0] = bswap_32(u
.val32
[0]);
2181 u
.val32
[1] = bswap_32(u
.val32
[1]);
2182 u
.val64
= bswap_64(u
.val64
);
2189 if (type
& PERF_SAMPLE_TIME
) {
2190 *array
= sample
->time
;
2194 if (type
& PERF_SAMPLE_ADDR
) {
2195 *array
= sample
->addr
;
2199 if (type
& PERF_SAMPLE_ID
) {
2200 *array
= sample
->id
;
2204 if (type
& PERF_SAMPLE_STREAM_ID
) {
2205 *array
= sample
->stream_id
;
2209 if (type
& PERF_SAMPLE_CPU
) {
2210 u
.val32
[0] = sample
->cpu
;
2213 * Inverse of what is done in perf_evsel__parse_sample
2215 u
.val32
[0] = bswap_32(u
.val32
[0]);
2216 u
.val64
= bswap_64(u
.val64
);
2222 if (type
& PERF_SAMPLE_PERIOD
) {
2223 *array
= sample
->period
;
2227 if (type
& PERF_SAMPLE_READ
) {
2228 if (read_format
& PERF_FORMAT_GROUP
)
2229 *array
= sample
->read
.group
.nr
;
2231 *array
= sample
->read
.one
.value
;
2234 if (read_format
& PERF_FORMAT_TOTAL_TIME_ENABLED
) {
2235 *array
= sample
->read
.time_enabled
;
2239 if (read_format
& PERF_FORMAT_TOTAL_TIME_RUNNING
) {
2240 *array
= sample
->read
.time_running
;
2244 /* PERF_FORMAT_ID is forced for PERF_SAMPLE_READ */
2245 if (read_format
& PERF_FORMAT_GROUP
) {
2246 sz
= sample
->read
.group
.nr
*
2247 sizeof(struct sample_read_value
);
2248 memcpy(array
, sample
->read
.group
.values
, sz
);
2249 array
= (void *)array
+ sz
;
2251 *array
= sample
->read
.one
.id
;
2256 if (type
& PERF_SAMPLE_CALLCHAIN
) {
2257 sz
= (sample
->callchain
->nr
+ 1) * sizeof(u64
);
2258 memcpy(array
, sample
->callchain
, sz
);
2259 array
= (void *)array
+ sz
;
2262 if (type
& PERF_SAMPLE_RAW
) {
2263 u
.val32
[0] = sample
->raw_size
;
2264 if (WARN_ONCE(swapped
,
2265 "Endianness of raw data not corrected!\n")) {
2267 * Inverse of what is done in perf_evsel__parse_sample
2269 u
.val32
[0] = bswap_32(u
.val32
[0]);
2270 u
.val32
[1] = bswap_32(u
.val32
[1]);
2271 u
.val64
= bswap_64(u
.val64
);
2274 array
= (void *)array
+ sizeof(u32
);
2276 memcpy(array
, sample
->raw_data
, sample
->raw_size
);
2277 array
= (void *)array
+ sample
->raw_size
;
2280 if (type
& PERF_SAMPLE_BRANCH_STACK
) {
2281 sz
= sample
->branch_stack
->nr
* sizeof(struct branch_entry
);
2283 memcpy(array
, sample
->branch_stack
, sz
);
2284 array
= (void *)array
+ sz
;
2287 if (type
& PERF_SAMPLE_REGS_USER
) {
2288 if (sample
->user_regs
.abi
) {
2289 *array
++ = sample
->user_regs
.abi
;
2290 sz
= hweight_long(sample
->user_regs
.mask
) * sizeof(u64
);
2291 memcpy(array
, sample
->user_regs
.regs
, sz
);
2292 array
= (void *)array
+ sz
;
2298 if (type
& PERF_SAMPLE_STACK_USER
) {
2299 sz
= sample
->user_stack
.size
;
2302 memcpy(array
, sample
->user_stack
.data
, sz
);
2303 array
= (void *)array
+ sz
;
2308 if (type
& PERF_SAMPLE_WEIGHT
) {
2309 *array
= sample
->weight
;
2313 if (type
& PERF_SAMPLE_DATA_SRC
) {
2314 *array
= sample
->data_src
;
2318 if (type
& PERF_SAMPLE_TRANSACTION
) {
2319 *array
= sample
->transaction
;
2323 if (type
& PERF_SAMPLE_REGS_INTR
) {
2324 if (sample
->intr_regs
.abi
) {
2325 *array
++ = sample
->intr_regs
.abi
;
2326 sz
= hweight_long(sample
->intr_regs
.mask
) * sizeof(u64
);
2327 memcpy(array
, sample
->intr_regs
.regs
, sz
);
2328 array
= (void *)array
+ sz
;
2337 struct format_field
*perf_evsel__field(struct perf_evsel
*evsel
, const char *name
)
2339 return pevent_find_field(evsel
->tp_format
, name
);
2342 void *perf_evsel__rawptr(struct perf_evsel
*evsel
, struct perf_sample
*sample
,
2345 struct format_field
*field
= perf_evsel__field(evsel
, name
);
2351 offset
= field
->offset
;
2353 if (field
->flags
& FIELD_IS_DYNAMIC
) {
2354 offset
= *(int *)(sample
->raw_data
+ field
->offset
);
2358 return sample
->raw_data
+ offset
;
2361 u64
format_field__intval(struct format_field
*field
, struct perf_sample
*sample
,
2365 void *ptr
= sample
->raw_data
+ field
->offset
;
2367 switch (field
->size
) {
2371 value
= *(u16
*)ptr
;
2374 value
= *(u32
*)ptr
;
2377 memcpy(&value
, ptr
, sizeof(u64
));
2386 switch (field
->size
) {
2388 return bswap_16(value
);
2390 return bswap_32(value
);
2392 return bswap_64(value
);
2400 u64
perf_evsel__intval(struct perf_evsel
*evsel
, struct perf_sample
*sample
,
2403 struct format_field
*field
= perf_evsel__field(evsel
, name
);
2408 return field
? format_field__intval(field
, sample
, evsel
->needs_swap
) : 0;
2411 bool perf_evsel__fallback(struct perf_evsel
*evsel
, int err
,
2412 char *msg
, size_t msgsize
)
2416 if ((err
== ENOENT
|| err
== ENXIO
|| err
== ENODEV
) &&
2417 evsel
->attr
.type
== PERF_TYPE_HARDWARE
&&
2418 evsel
->attr
.config
== PERF_COUNT_HW_CPU_CYCLES
) {
2420 * If it's cycles then fall back to hrtimer based
2421 * cpu-clock-tick sw counter, which is always available even if
2424 * PPC returns ENXIO until 2.6.37 (behavior changed with commit
2427 scnprintf(msg
, msgsize
, "%s",
2428 "The cycles event is not supported, trying to fall back to cpu-clock-ticks");
2430 evsel
->attr
.type
= PERF_TYPE_SOFTWARE
;
2431 evsel
->attr
.config
= PERF_COUNT_SW_CPU_CLOCK
;
2433 zfree(&evsel
->name
);
2435 } else if (err
== EACCES
&& !evsel
->attr
.exclude_kernel
&&
2436 (paranoid
= perf_event_paranoid()) > 1) {
2437 const char *name
= perf_evsel__name(evsel
);
2440 if (asprintf(&new_name
, "%s%su", name
, strchr(name
, ':') ? "" : ":") < 0)
2445 evsel
->name
= new_name
;
2446 scnprintf(msg
, msgsize
,
2447 "kernel.perf_event_paranoid=%d, trying to fall back to excluding kernel samples", paranoid
);
2448 evsel
->attr
.exclude_kernel
= 1;
2456 int perf_evsel__open_strerror(struct perf_evsel
*evsel
, struct target
*target
,
2457 int err
, char *msg
, size_t size
)
2459 char sbuf
[STRERR_BUFSIZE
];
2466 printed
= scnprintf(msg
, size
,
2467 "No permission to enable %s event.\n\n",
2468 perf_evsel__name(evsel
));
2470 return scnprintf(msg
+ printed
, size
- printed
,
2471 "You may not have permission to collect %sstats.\n\n"
2472 "Consider tweaking /proc/sys/kernel/perf_event_paranoid,\n"
2473 "which controls use of the performance events system by\n"
2474 "unprivileged users (without CAP_SYS_ADMIN).\n\n"
2475 "The current value is %d:\n\n"
2476 " -1: Allow use of (almost) all events by all users\n"
2477 ">= 0: Disallow raw tracepoint access by users without CAP_IOC_LOCK\n"
2478 ">= 1: Disallow CPU event access by users without CAP_SYS_ADMIN\n"
2479 ">= 2: Disallow kernel profiling by users without CAP_SYS_ADMIN\n\n"
2480 "To make this setting permanent, edit /etc/sysctl.conf too, e.g.:\n\n"
2481 " kernel.perf_event_paranoid = -1\n" ,
2482 target
->system_wide
? "system-wide " : "",
2483 perf_event_paranoid());
2485 return scnprintf(msg
, size
, "The %s event is not supported.",
2486 perf_evsel__name(evsel
));
2488 return scnprintf(msg
, size
, "%s",
2489 "Too many events are opened.\n"
2490 "Probably the maximum number of open file descriptors has been reached.\n"
2491 "Hint: Try again after reducing the number of events.\n"
2492 "Hint: Try increasing the limit with 'ulimit -n <limit>'");
2494 if ((evsel
->attr
.sample_type
& PERF_SAMPLE_CALLCHAIN
) != 0 &&
2495 access("/proc/sys/kernel/perf_event_max_stack", F_OK
) == 0)
2496 return scnprintf(msg
, size
,
2497 "Not enough memory to setup event with callchain.\n"
2498 "Hint: Try tweaking /proc/sys/kernel/perf_event_max_stack\n"
2499 "Hint: Current value: %d", sysctl_perf_event_max_stack
);
2502 if (target
->cpu_list
)
2503 return scnprintf(msg
, size
, "%s",
2504 "No such device - did you specify an out-of-range profile CPU?");
2507 if (evsel
->attr
.sample_period
!= 0)
2508 return scnprintf(msg
, size
, "%s",
2509 "PMU Hardware doesn't support sampling/overflow-interrupts.");
2510 if (evsel
->attr
.precise_ip
)
2511 return scnprintf(msg
, size
, "%s",
2512 "\'precise\' request may not be supported. Try removing 'p' modifier.");
2513 #if defined(__i386__) || defined(__x86_64__)
2514 if (evsel
->attr
.type
== PERF_TYPE_HARDWARE
)
2515 return scnprintf(msg
, size
, "%s",
2516 "No hardware sampling interrupt available.\n"
2517 "No APIC? If so then you can boot the kernel with the \"lapic\" boot parameter to force-enable it.");
2521 if (find_process("oprofiled"))
2522 return scnprintf(msg
, size
,
2523 "The PMU counters are busy/taken by another profiler.\n"
2524 "We found oprofile daemon running, please stop it and try again.");
2527 if (evsel
->attr
.write_backward
&& perf_missing_features
.write_backward
)
2528 return scnprintf(msg
, size
, "Reading from overwrite event is not supported by this kernel.");
2529 if (perf_missing_features
.clockid
)
2530 return scnprintf(msg
, size
, "clockid feature not supported.");
2531 if (perf_missing_features
.clockid_wrong
)
2532 return scnprintf(msg
, size
, "wrong clockid (%d).", clockid
);
2538 return scnprintf(msg
, size
,
2539 "The sys_perf_event_open() syscall returned with %d (%s) for event (%s).\n"
2540 "/bin/dmesg may provide additional information.\n"
2541 "No CONFIG_PERF_EVENTS=y kernel support configured?",
2542 err
, str_error_r(err
, sbuf
, sizeof(sbuf
)),
2543 perf_evsel__name(evsel
));
2546 char *perf_evsel__env_arch(struct perf_evsel
*evsel
)
2548 if (evsel
&& evsel
->evlist
&& evsel
->evlist
->env
)
2549 return evsel
->evlist
->env
->arch
;