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/debugfs.h>
13 #include <traceevent/event-parse.h>
14 #include <linux/hw_breakpoint.h>
15 #include <linux/perf_event.h>
16 #include <sys/resource.h>
18 #include "callchain.h"
24 #include "thread_map.h"
26 #include "perf_regs.h"
28 #include "trace-event.h"
35 } perf_missing_features
;
37 static int perf_evsel__no_extra_init(struct perf_evsel
*evsel __maybe_unused
)
42 static void perf_evsel__no_extra_fini(struct perf_evsel
*evsel __maybe_unused
)
48 int (*init
)(struct perf_evsel
*evsel
);
49 void (*fini
)(struct perf_evsel
*evsel
);
50 } perf_evsel__object
= {
51 .size
= sizeof(struct perf_evsel
),
52 .init
= perf_evsel__no_extra_init
,
53 .fini
= perf_evsel__no_extra_fini
,
56 int perf_evsel__object_config(size_t object_size
,
57 int (*init
)(struct perf_evsel
*evsel
),
58 void (*fini
)(struct perf_evsel
*evsel
))
64 if (perf_evsel__object
.size
> object_size
)
67 perf_evsel__object
.size
= object_size
;
71 perf_evsel__object
.init
= init
;
74 perf_evsel__object
.fini
= fini
;
79 #define FD(e, x, y) (*(int *)xyarray__entry(e->fd, x, y))
81 int __perf_evsel__sample_size(u64 sample_type
)
83 u64 mask
= sample_type
& PERF_SAMPLE_MASK
;
87 for (i
= 0; i
< 64; i
++) {
88 if (mask
& (1ULL << i
))
98 * __perf_evsel__calc_id_pos - calculate id_pos.
99 * @sample_type: sample type
101 * This function returns the position of the event id (PERF_SAMPLE_ID or
102 * PERF_SAMPLE_IDENTIFIER) in a sample event i.e. in the array of struct
105 static int __perf_evsel__calc_id_pos(u64 sample_type
)
109 if (sample_type
& PERF_SAMPLE_IDENTIFIER
)
112 if (!(sample_type
& PERF_SAMPLE_ID
))
115 if (sample_type
& PERF_SAMPLE_IP
)
118 if (sample_type
& PERF_SAMPLE_TID
)
121 if (sample_type
& PERF_SAMPLE_TIME
)
124 if (sample_type
& PERF_SAMPLE_ADDR
)
131 * __perf_evsel__calc_is_pos - calculate is_pos.
132 * @sample_type: sample type
134 * This function returns the position (counting backwards) of the event id
135 * (PERF_SAMPLE_ID or PERF_SAMPLE_IDENTIFIER) in a non-sample event i.e. if
136 * sample_id_all is used there is an id sample appended to non-sample events.
138 static int __perf_evsel__calc_is_pos(u64 sample_type
)
142 if (sample_type
& PERF_SAMPLE_IDENTIFIER
)
145 if (!(sample_type
& PERF_SAMPLE_ID
))
148 if (sample_type
& PERF_SAMPLE_CPU
)
151 if (sample_type
& PERF_SAMPLE_STREAM_ID
)
157 void perf_evsel__calc_id_pos(struct perf_evsel
*evsel
)
159 evsel
->id_pos
= __perf_evsel__calc_id_pos(evsel
->attr
.sample_type
);
160 evsel
->is_pos
= __perf_evsel__calc_is_pos(evsel
->attr
.sample_type
);
163 void __perf_evsel__set_sample_bit(struct perf_evsel
*evsel
,
164 enum perf_event_sample_format bit
)
166 if (!(evsel
->attr
.sample_type
& bit
)) {
167 evsel
->attr
.sample_type
|= bit
;
168 evsel
->sample_size
+= sizeof(u64
);
169 perf_evsel__calc_id_pos(evsel
);
173 void __perf_evsel__reset_sample_bit(struct perf_evsel
*evsel
,
174 enum perf_event_sample_format bit
)
176 if (evsel
->attr
.sample_type
& bit
) {
177 evsel
->attr
.sample_type
&= ~bit
;
178 evsel
->sample_size
-= sizeof(u64
);
179 perf_evsel__calc_id_pos(evsel
);
183 void perf_evsel__set_sample_id(struct perf_evsel
*evsel
,
184 bool can_sample_identifier
)
186 if (can_sample_identifier
) {
187 perf_evsel__reset_sample_bit(evsel
, ID
);
188 perf_evsel__set_sample_bit(evsel
, IDENTIFIER
);
190 perf_evsel__set_sample_bit(evsel
, ID
);
192 evsel
->attr
.read_format
|= PERF_FORMAT_ID
;
195 void perf_evsel__init(struct perf_evsel
*evsel
,
196 struct perf_event_attr
*attr
, int idx
)
199 evsel
->tracking
= !idx
;
201 evsel
->leader
= evsel
;
204 INIT_LIST_HEAD(&evsel
->node
);
205 perf_evsel__object
.init(evsel
);
206 evsel
->sample_size
= __perf_evsel__sample_size(attr
->sample_type
);
207 perf_evsel__calc_id_pos(evsel
);
210 struct perf_evsel
*perf_evsel__new_idx(struct perf_event_attr
*attr
, int idx
)
212 struct perf_evsel
*evsel
= zalloc(perf_evsel__object
.size
);
215 perf_evsel__init(evsel
, attr
, idx
);
220 struct perf_evsel
*perf_evsel__newtp_idx(const char *sys
, const char *name
, int idx
)
222 struct perf_evsel
*evsel
= zalloc(perf_evsel__object
.size
);
225 struct perf_event_attr attr
= {
226 .type
= PERF_TYPE_TRACEPOINT
,
227 .sample_type
= (PERF_SAMPLE_RAW
| PERF_SAMPLE_TIME
|
228 PERF_SAMPLE_CPU
| PERF_SAMPLE_PERIOD
),
231 if (asprintf(&evsel
->name
, "%s:%s", sys
, name
) < 0)
234 evsel
->tp_format
= trace_event__tp_format(sys
, name
);
235 if (evsel
->tp_format
== NULL
)
238 event_attr_init(&attr
);
239 attr
.config
= evsel
->tp_format
->id
;
240 attr
.sample_period
= 1;
241 perf_evsel__init(evsel
, &attr
, idx
);
252 const char *perf_evsel__hw_names
[PERF_COUNT_HW_MAX
] = {
260 "stalled-cycles-frontend",
261 "stalled-cycles-backend",
265 static const char *__perf_evsel__hw_name(u64 config
)
267 if (config
< PERF_COUNT_HW_MAX
&& perf_evsel__hw_names
[config
])
268 return perf_evsel__hw_names
[config
];
270 return "unknown-hardware";
273 static int perf_evsel__add_modifiers(struct perf_evsel
*evsel
, char *bf
, size_t size
)
275 int colon
= 0, r
= 0;
276 struct perf_event_attr
*attr
= &evsel
->attr
;
277 bool exclude_guest_default
= false;
279 #define MOD_PRINT(context, mod) do { \
280 if (!attr->exclude_##context) { \
281 if (!colon) colon = ++r; \
282 r += scnprintf(bf + r, size - r, "%c", mod); \
285 if (attr
->exclude_kernel
|| attr
->exclude_user
|| attr
->exclude_hv
) {
286 MOD_PRINT(kernel
, 'k');
287 MOD_PRINT(user
, 'u');
289 exclude_guest_default
= true;
292 if (attr
->precise_ip
) {
295 r
+= scnprintf(bf
+ r
, size
- r
, "%.*s", attr
->precise_ip
, "ppp");
296 exclude_guest_default
= true;
299 if (attr
->exclude_host
|| attr
->exclude_guest
== exclude_guest_default
) {
300 MOD_PRINT(host
, 'H');
301 MOD_PRINT(guest
, 'G');
309 static int perf_evsel__hw_name(struct perf_evsel
*evsel
, char *bf
, size_t size
)
311 int r
= scnprintf(bf
, size
, "%s", __perf_evsel__hw_name(evsel
->attr
.config
));
312 return r
+ perf_evsel__add_modifiers(evsel
, bf
+ r
, size
- r
);
315 const char *perf_evsel__sw_names
[PERF_COUNT_SW_MAX
] = {
328 static const char *__perf_evsel__sw_name(u64 config
)
330 if (config
< PERF_COUNT_SW_MAX
&& perf_evsel__sw_names
[config
])
331 return perf_evsel__sw_names
[config
];
332 return "unknown-software";
335 static int perf_evsel__sw_name(struct perf_evsel
*evsel
, char *bf
, size_t size
)
337 int r
= scnprintf(bf
, size
, "%s", __perf_evsel__sw_name(evsel
->attr
.config
));
338 return r
+ perf_evsel__add_modifiers(evsel
, bf
+ r
, size
- r
);
341 static int __perf_evsel__bp_name(char *bf
, size_t size
, u64 addr
, u64 type
)
345 r
= scnprintf(bf
, size
, "mem:0x%" PRIx64
":", addr
);
347 if (type
& HW_BREAKPOINT_R
)
348 r
+= scnprintf(bf
+ r
, size
- r
, "r");
350 if (type
& HW_BREAKPOINT_W
)
351 r
+= scnprintf(bf
+ r
, size
- r
, "w");
353 if (type
& HW_BREAKPOINT_X
)
354 r
+= scnprintf(bf
+ r
, size
- r
, "x");
359 static int perf_evsel__bp_name(struct perf_evsel
*evsel
, char *bf
, size_t size
)
361 struct perf_event_attr
*attr
= &evsel
->attr
;
362 int r
= __perf_evsel__bp_name(bf
, size
, attr
->bp_addr
, attr
->bp_type
);
363 return r
+ perf_evsel__add_modifiers(evsel
, bf
+ r
, size
- r
);
366 const char *perf_evsel__hw_cache
[PERF_COUNT_HW_CACHE_MAX
]
367 [PERF_EVSEL__MAX_ALIASES
] = {
368 { "L1-dcache", "l1-d", "l1d", "L1-data", },
369 { "L1-icache", "l1-i", "l1i", "L1-instruction", },
371 { "dTLB", "d-tlb", "Data-TLB", },
372 { "iTLB", "i-tlb", "Instruction-TLB", },
373 { "branch", "branches", "bpu", "btb", "bpc", },
377 const char *perf_evsel__hw_cache_op
[PERF_COUNT_HW_CACHE_OP_MAX
]
378 [PERF_EVSEL__MAX_ALIASES
] = {
379 { "load", "loads", "read", },
380 { "store", "stores", "write", },
381 { "prefetch", "prefetches", "speculative-read", "speculative-load", },
384 const char *perf_evsel__hw_cache_result
[PERF_COUNT_HW_CACHE_RESULT_MAX
]
385 [PERF_EVSEL__MAX_ALIASES
] = {
386 { "refs", "Reference", "ops", "access", },
387 { "misses", "miss", },
390 #define C(x) PERF_COUNT_HW_CACHE_##x
391 #define CACHE_READ (1 << C(OP_READ))
392 #define CACHE_WRITE (1 << C(OP_WRITE))
393 #define CACHE_PREFETCH (1 << C(OP_PREFETCH))
394 #define COP(x) (1 << x)
397 * cache operartion stat
398 * L1I : Read and prefetch only
399 * ITLB and BPU : Read-only
401 static unsigned long perf_evsel__hw_cache_stat
[C(MAX
)] = {
402 [C(L1D
)] = (CACHE_READ
| CACHE_WRITE
| CACHE_PREFETCH
),
403 [C(L1I
)] = (CACHE_READ
| CACHE_PREFETCH
),
404 [C(LL
)] = (CACHE_READ
| CACHE_WRITE
| CACHE_PREFETCH
),
405 [C(DTLB
)] = (CACHE_READ
| CACHE_WRITE
| CACHE_PREFETCH
),
406 [C(ITLB
)] = (CACHE_READ
),
407 [C(BPU
)] = (CACHE_READ
),
408 [C(NODE
)] = (CACHE_READ
| CACHE_WRITE
| CACHE_PREFETCH
),
411 bool perf_evsel__is_cache_op_valid(u8 type
, u8 op
)
413 if (perf_evsel__hw_cache_stat
[type
] & COP(op
))
414 return true; /* valid */
416 return false; /* invalid */
419 int __perf_evsel__hw_cache_type_op_res_name(u8 type
, u8 op
, u8 result
,
420 char *bf
, size_t size
)
423 return scnprintf(bf
, size
, "%s-%s-%s", perf_evsel__hw_cache
[type
][0],
424 perf_evsel__hw_cache_op
[op
][0],
425 perf_evsel__hw_cache_result
[result
][0]);
428 return scnprintf(bf
, size
, "%s-%s", perf_evsel__hw_cache
[type
][0],
429 perf_evsel__hw_cache_op
[op
][1]);
432 static int __perf_evsel__hw_cache_name(u64 config
, char *bf
, size_t size
)
434 u8 op
, result
, type
= (config
>> 0) & 0xff;
435 const char *err
= "unknown-ext-hardware-cache-type";
437 if (type
> PERF_COUNT_HW_CACHE_MAX
)
440 op
= (config
>> 8) & 0xff;
441 err
= "unknown-ext-hardware-cache-op";
442 if (op
> PERF_COUNT_HW_CACHE_OP_MAX
)
445 result
= (config
>> 16) & 0xff;
446 err
= "unknown-ext-hardware-cache-result";
447 if (result
> PERF_COUNT_HW_CACHE_RESULT_MAX
)
450 err
= "invalid-cache";
451 if (!perf_evsel__is_cache_op_valid(type
, op
))
454 return __perf_evsel__hw_cache_type_op_res_name(type
, op
, result
, bf
, size
);
456 return scnprintf(bf
, size
, "%s", err
);
459 static int perf_evsel__hw_cache_name(struct perf_evsel
*evsel
, char *bf
, size_t size
)
461 int ret
= __perf_evsel__hw_cache_name(evsel
->attr
.config
, bf
, size
);
462 return ret
+ perf_evsel__add_modifiers(evsel
, bf
+ ret
, size
- ret
);
465 static int perf_evsel__raw_name(struct perf_evsel
*evsel
, char *bf
, size_t size
)
467 int ret
= scnprintf(bf
, size
, "raw 0x%" PRIx64
, evsel
->attr
.config
);
468 return ret
+ perf_evsel__add_modifiers(evsel
, bf
+ ret
, size
- ret
);
471 const char *perf_evsel__name(struct perf_evsel
*evsel
)
478 switch (evsel
->attr
.type
) {
480 perf_evsel__raw_name(evsel
, bf
, sizeof(bf
));
483 case PERF_TYPE_HARDWARE
:
484 perf_evsel__hw_name(evsel
, bf
, sizeof(bf
));
487 case PERF_TYPE_HW_CACHE
:
488 perf_evsel__hw_cache_name(evsel
, bf
, sizeof(bf
));
491 case PERF_TYPE_SOFTWARE
:
492 perf_evsel__sw_name(evsel
, bf
, sizeof(bf
));
495 case PERF_TYPE_TRACEPOINT
:
496 scnprintf(bf
, sizeof(bf
), "%s", "unknown tracepoint");
499 case PERF_TYPE_BREAKPOINT
:
500 perf_evsel__bp_name(evsel
, bf
, sizeof(bf
));
504 scnprintf(bf
, sizeof(bf
), "unknown attr type: %d",
509 evsel
->name
= strdup(bf
);
511 return evsel
->name
?: "unknown";
514 const char *perf_evsel__group_name(struct perf_evsel
*evsel
)
516 return evsel
->group_name
?: "anon group";
519 int perf_evsel__group_desc(struct perf_evsel
*evsel
, char *buf
, size_t size
)
522 struct perf_evsel
*pos
;
523 const char *group_name
= perf_evsel__group_name(evsel
);
525 ret
= scnprintf(buf
, size
, "%s", group_name
);
527 ret
+= scnprintf(buf
+ ret
, size
- ret
, " { %s",
528 perf_evsel__name(evsel
));
530 for_each_group_member(pos
, evsel
)
531 ret
+= scnprintf(buf
+ ret
, size
- ret
, ", %s",
532 perf_evsel__name(pos
));
534 ret
+= scnprintf(buf
+ ret
, size
- ret
, " }");
540 perf_evsel__config_callgraph(struct perf_evsel
*evsel
)
542 bool function
= perf_evsel__is_function_event(evsel
);
543 struct perf_event_attr
*attr
= &evsel
->attr
;
545 perf_evsel__set_sample_bit(evsel
, CALLCHAIN
);
547 if (callchain_param
.record_mode
== CALLCHAIN_DWARF
) {
549 perf_evsel__set_sample_bit(evsel
, REGS_USER
);
550 perf_evsel__set_sample_bit(evsel
, STACK_USER
);
551 attr
->sample_regs_user
= PERF_REGS_MASK
;
552 attr
->sample_stack_user
= callchain_param
.dump_size
;
553 attr
->exclude_callchain_user
= 1;
555 pr_info("Cannot use DWARF unwind for function trace event,"
556 " falling back to framepointers.\n");
561 pr_info("Disabling user space callchains for function trace event.\n");
562 attr
->exclude_callchain_user
= 1;
567 * The enable_on_exec/disabled value strategy:
569 * 1) For any type of traced program:
570 * - all independent events and group leaders are disabled
571 * - all group members are enabled
573 * Group members are ruled by group leaders. They need to
574 * be enabled, because the group scheduling relies on that.
576 * 2) For traced programs executed by perf:
577 * - all independent events and group leaders have
579 * - we don't specifically enable or disable any event during
582 * Independent events and group leaders are initially disabled
583 * and get enabled by exec. Group members are ruled by group
584 * leaders as stated in 1).
586 * 3) For traced programs attached by perf (pid/tid):
587 * - we specifically enable or disable all events during
590 * When attaching events to already running traced we
591 * enable/disable events specifically, as there's no
592 * initial traced exec call.
594 void perf_evsel__config(struct perf_evsel
*evsel
, struct record_opts
*opts
)
596 struct perf_evsel
*leader
= evsel
->leader
;
597 struct perf_event_attr
*attr
= &evsel
->attr
;
598 int track
= evsel
->tracking
;
599 bool per_cpu
= opts
->target
.default_per_cpu
&& !opts
->target
.per_thread
;
601 attr
->sample_id_all
= perf_missing_features
.sample_id_all
? 0 : 1;
602 attr
->inherit
= !opts
->no_inherit
;
604 perf_evsel__set_sample_bit(evsel
, IP
);
605 perf_evsel__set_sample_bit(evsel
, TID
);
607 if (evsel
->sample_read
) {
608 perf_evsel__set_sample_bit(evsel
, READ
);
611 * We need ID even in case of single event, because
612 * PERF_SAMPLE_READ process ID specific data.
614 perf_evsel__set_sample_id(evsel
, false);
617 * Apply group format only if we belong to group
618 * with more than one members.
620 if (leader
->nr_members
> 1) {
621 attr
->read_format
|= PERF_FORMAT_GROUP
;
627 * We default some events to have a default interval. But keep
628 * it a weak assumption overridable by the user.
630 if (!attr
->sample_period
|| (opts
->user_freq
!= UINT_MAX
||
631 opts
->user_interval
!= ULLONG_MAX
)) {
633 perf_evsel__set_sample_bit(evsel
, PERIOD
);
635 attr
->sample_freq
= opts
->freq
;
637 attr
->sample_period
= opts
->default_interval
;
642 * Disable sampling for all group members other
643 * than leader in case leader 'leads' the sampling.
645 if ((leader
!= evsel
) && leader
->sample_read
) {
646 attr
->sample_freq
= 0;
647 attr
->sample_period
= 0;
650 if (opts
->no_samples
)
651 attr
->sample_freq
= 0;
653 if (opts
->inherit_stat
)
654 attr
->inherit_stat
= 1;
656 if (opts
->sample_address
) {
657 perf_evsel__set_sample_bit(evsel
, ADDR
);
658 attr
->mmap_data
= track
;
662 * We don't allow user space callchains for function trace
663 * event, due to issues with page faults while tracing page
664 * fault handler and its overall trickiness nature.
666 if (perf_evsel__is_function_event(evsel
))
667 evsel
->attr
.exclude_callchain_user
= 1;
669 if (callchain_param
.enabled
&& !evsel
->no_aux_samples
)
670 perf_evsel__config_callgraph(evsel
);
672 if (opts
->sample_intr_regs
) {
673 attr
->sample_regs_intr
= PERF_REGS_MASK
;
674 perf_evsel__set_sample_bit(evsel
, REGS_INTR
);
677 if (target__has_cpu(&opts
->target
))
678 perf_evsel__set_sample_bit(evsel
, CPU
);
681 perf_evsel__set_sample_bit(evsel
, PERIOD
);
684 * When the user explicitely disabled time don't force it here.
686 if (opts
->sample_time
&&
687 (!perf_missing_features
.sample_id_all
&&
688 (!opts
->no_inherit
|| target__has_cpu(&opts
->target
) || per_cpu
)))
689 perf_evsel__set_sample_bit(evsel
, TIME
);
691 if (opts
->raw_samples
&& !evsel
->no_aux_samples
) {
692 perf_evsel__set_sample_bit(evsel
, TIME
);
693 perf_evsel__set_sample_bit(evsel
, RAW
);
694 perf_evsel__set_sample_bit(evsel
, CPU
);
697 if (opts
->sample_address
)
698 perf_evsel__set_sample_bit(evsel
, DATA_SRC
);
700 if (opts
->no_buffering
) {
702 attr
->wakeup_events
= 1;
704 if (opts
->branch_stack
&& !evsel
->no_aux_samples
) {
705 perf_evsel__set_sample_bit(evsel
, BRANCH_STACK
);
706 attr
->branch_sample_type
= opts
->branch_stack
;
709 if (opts
->sample_weight
)
710 perf_evsel__set_sample_bit(evsel
, WEIGHT
);
713 attr
->mmap2
= track
&& !perf_missing_features
.mmap2
;
716 if (opts
->sample_transaction
)
717 perf_evsel__set_sample_bit(evsel
, TRANSACTION
);
720 * XXX see the function comment above
722 * Disabling only independent events or group leaders,
723 * keeping group members enabled.
725 if (perf_evsel__is_group_leader(evsel
))
729 * Setting enable_on_exec for independent events and
730 * group leaders for traced executed by perf.
732 if (target__none(&opts
->target
) && perf_evsel__is_group_leader(evsel
) &&
733 !opts
->initial_delay
)
734 attr
->enable_on_exec
= 1;
736 if (evsel
->immediate
) {
738 attr
->enable_on_exec
= 0;
742 static int perf_evsel__alloc_fd(struct perf_evsel
*evsel
, int ncpus
, int nthreads
)
746 if (evsel
->system_wide
)
749 evsel
->fd
= xyarray__new(ncpus
, nthreads
, sizeof(int));
752 for (cpu
= 0; cpu
< ncpus
; cpu
++) {
753 for (thread
= 0; thread
< nthreads
; thread
++) {
754 FD(evsel
, cpu
, thread
) = -1;
759 return evsel
->fd
!= NULL
? 0 : -ENOMEM
;
762 static int perf_evsel__run_ioctl(struct perf_evsel
*evsel
, int ncpus
, int nthreads
,
767 if (evsel
->system_wide
)
770 for (cpu
= 0; cpu
< ncpus
; cpu
++) {
771 for (thread
= 0; thread
< nthreads
; thread
++) {
772 int fd
= FD(evsel
, cpu
, thread
),
773 err
= ioctl(fd
, ioc
, arg
);
783 int perf_evsel__set_filter(struct perf_evsel
*evsel
, int ncpus
, int nthreads
,
786 return perf_evsel__run_ioctl(evsel
, ncpus
, nthreads
,
787 PERF_EVENT_IOC_SET_FILTER
,
791 int perf_evsel__enable(struct perf_evsel
*evsel
, int ncpus
, int nthreads
)
793 return perf_evsel__run_ioctl(evsel
, ncpus
, nthreads
,
794 PERF_EVENT_IOC_ENABLE
,
798 int perf_evsel__alloc_id(struct perf_evsel
*evsel
, int ncpus
, int nthreads
)
800 if (evsel
->system_wide
)
803 evsel
->sample_id
= xyarray__new(ncpus
, nthreads
, sizeof(struct perf_sample_id
));
804 if (evsel
->sample_id
== NULL
)
807 evsel
->id
= zalloc(ncpus
* nthreads
* sizeof(u64
));
808 if (evsel
->id
== NULL
) {
809 xyarray__delete(evsel
->sample_id
);
810 evsel
->sample_id
= NULL
;
817 void perf_evsel__reset_counts(struct perf_evsel
*evsel
, int ncpus
)
819 memset(evsel
->counts
, 0, (sizeof(*evsel
->counts
) +
820 (ncpus
* sizeof(struct perf_counts_values
))));
823 int perf_evsel__alloc_counts(struct perf_evsel
*evsel
, int ncpus
)
825 evsel
->counts
= zalloc((sizeof(*evsel
->counts
) +
826 (ncpus
* sizeof(struct perf_counts_values
))));
827 return evsel
->counts
!= NULL
? 0 : -ENOMEM
;
830 static void perf_evsel__free_fd(struct perf_evsel
*evsel
)
832 xyarray__delete(evsel
->fd
);
836 static void perf_evsel__free_id(struct perf_evsel
*evsel
)
838 xyarray__delete(evsel
->sample_id
);
839 evsel
->sample_id
= NULL
;
843 void perf_evsel__close_fd(struct perf_evsel
*evsel
, int ncpus
, int nthreads
)
847 if (evsel
->system_wide
)
850 for (cpu
= 0; cpu
< ncpus
; cpu
++)
851 for (thread
= 0; thread
< nthreads
; ++thread
) {
852 close(FD(evsel
, cpu
, thread
));
853 FD(evsel
, cpu
, thread
) = -1;
857 void perf_evsel__free_counts(struct perf_evsel
*evsel
)
859 zfree(&evsel
->counts
);
862 void perf_evsel__exit(struct perf_evsel
*evsel
)
864 assert(list_empty(&evsel
->node
));
865 perf_evsel__free_fd(evsel
);
866 perf_evsel__free_id(evsel
);
867 close_cgroup(evsel
->cgrp
);
868 zfree(&evsel
->group_name
);
870 perf_evsel__object
.fini(evsel
);
873 void perf_evsel__delete(struct perf_evsel
*evsel
)
875 perf_evsel__exit(evsel
);
879 void perf_evsel__compute_deltas(struct perf_evsel
*evsel
, int cpu
,
880 struct perf_counts_values
*count
)
882 struct perf_counts_values tmp
;
884 if (!evsel
->prev_raw_counts
)
888 tmp
= evsel
->prev_raw_counts
->aggr
;
889 evsel
->prev_raw_counts
->aggr
= *count
;
891 tmp
= evsel
->prev_raw_counts
->cpu
[cpu
];
892 evsel
->prev_raw_counts
->cpu
[cpu
] = *count
;
895 count
->val
= count
->val
- tmp
.val
;
896 count
->ena
= count
->ena
- tmp
.ena
;
897 count
->run
= count
->run
- tmp
.run
;
900 void perf_counts_values__scale(struct perf_counts_values
*count
,
901 bool scale
, s8
*pscaled
)
906 if (count
->run
== 0) {
909 } else if (count
->run
< count
->ena
) {
911 count
->val
= (u64
)((double) count
->val
* count
->ena
/ count
->run
+ 0.5);
914 count
->ena
= count
->run
= 0;
920 int perf_evsel__read_cb(struct perf_evsel
*evsel
, int cpu
, int thread
,
921 perf_evsel__read_cb_t cb
)
923 struct perf_counts_values count
;
925 memset(&count
, 0, sizeof(count
));
927 if (FD(evsel
, cpu
, thread
) < 0)
930 if (readn(FD(evsel
, cpu
, thread
), &count
, sizeof(count
)) < 0)
933 return cb(evsel
, cpu
, thread
, &count
);
936 int __perf_evsel__read_on_cpu(struct perf_evsel
*evsel
,
937 int cpu
, int thread
, bool scale
)
939 struct perf_counts_values count
;
940 size_t nv
= scale
? 3 : 1;
942 if (FD(evsel
, cpu
, thread
) < 0)
945 if (evsel
->counts
== NULL
&& perf_evsel__alloc_counts(evsel
, cpu
+ 1) < 0)
948 if (readn(FD(evsel
, cpu
, thread
), &count
, nv
* sizeof(u64
)) < 0)
951 perf_evsel__compute_deltas(evsel
, cpu
, &count
);
952 perf_counts_values__scale(&count
, scale
, NULL
);
953 evsel
->counts
->cpu
[cpu
] = count
;
957 static int get_group_fd(struct perf_evsel
*evsel
, int cpu
, int thread
)
959 struct perf_evsel
*leader
= evsel
->leader
;
962 if (perf_evsel__is_group_leader(evsel
))
966 * Leader must be already processed/open,
971 fd
= FD(leader
, cpu
, thread
);
977 #define __PRINT_ATTR(fmt, cast, field) \
978 fprintf(fp, " %-19s "fmt"\n", #field, cast attr->field)
980 #define PRINT_ATTR_U32(field) __PRINT_ATTR("%u" , , field)
981 #define PRINT_ATTR_X32(field) __PRINT_ATTR("%#x", , field)
982 #define PRINT_ATTR_U64(field) __PRINT_ATTR("%" PRIu64, (uint64_t), field)
983 #define PRINT_ATTR_X64(field) __PRINT_ATTR("%#"PRIx64, (uint64_t), field)
985 #define PRINT_ATTR2N(name1, field1, name2, field2) \
986 fprintf(fp, " %-19s %u %-19s %u\n", \
987 name1, attr->field1, name2, attr->field2)
989 #define PRINT_ATTR2(field1, field2) \
990 PRINT_ATTR2N(#field1, field1, #field2, field2)
992 static size_t perf_event_attr__fprintf(struct perf_event_attr
*attr
, FILE *fp
)
996 ret
+= fprintf(fp
, "%.60s\n", graph_dotted_line
);
997 ret
+= fprintf(fp
, "perf_event_attr:\n");
999 ret
+= PRINT_ATTR_U32(type
);
1000 ret
+= PRINT_ATTR_U32(size
);
1001 ret
+= PRINT_ATTR_X64(config
);
1002 ret
+= PRINT_ATTR_U64(sample_period
);
1003 ret
+= PRINT_ATTR_U64(sample_freq
);
1004 ret
+= PRINT_ATTR_X64(sample_type
);
1005 ret
+= PRINT_ATTR_X64(read_format
);
1007 ret
+= PRINT_ATTR2(disabled
, inherit
);
1008 ret
+= PRINT_ATTR2(pinned
, exclusive
);
1009 ret
+= PRINT_ATTR2(exclude_user
, exclude_kernel
);
1010 ret
+= PRINT_ATTR2(exclude_hv
, exclude_idle
);
1011 ret
+= PRINT_ATTR2(mmap
, comm
);
1012 ret
+= PRINT_ATTR2(mmap2
, comm_exec
);
1013 ret
+= PRINT_ATTR2(freq
, inherit_stat
);
1014 ret
+= PRINT_ATTR2(enable_on_exec
, task
);
1015 ret
+= PRINT_ATTR2(watermark
, precise_ip
);
1016 ret
+= PRINT_ATTR2(mmap_data
, sample_id_all
);
1017 ret
+= PRINT_ATTR2(exclude_host
, exclude_guest
);
1018 ret
+= PRINT_ATTR2N("excl.callchain_kern", exclude_callchain_kernel
,
1019 "excl.callchain_user", exclude_callchain_user
);
1021 ret
+= PRINT_ATTR_U32(wakeup_events
);
1022 ret
+= PRINT_ATTR_U32(wakeup_watermark
);
1023 ret
+= PRINT_ATTR_X32(bp_type
);
1024 ret
+= PRINT_ATTR_X64(bp_addr
);
1025 ret
+= PRINT_ATTR_X64(config1
);
1026 ret
+= PRINT_ATTR_U64(bp_len
);
1027 ret
+= PRINT_ATTR_X64(config2
);
1028 ret
+= PRINT_ATTR_X64(branch_sample_type
);
1029 ret
+= PRINT_ATTR_X64(sample_regs_user
);
1030 ret
+= PRINT_ATTR_U32(sample_stack_user
);
1031 ret
+= PRINT_ATTR_X64(sample_regs_intr
);
1033 ret
+= fprintf(fp
, "%.60s\n", graph_dotted_line
);
1038 static int __perf_evsel__open(struct perf_evsel
*evsel
, struct cpu_map
*cpus
,
1039 struct thread_map
*threads
)
1041 int cpu
, thread
, nthreads
;
1042 unsigned long flags
= PERF_FLAG_FD_CLOEXEC
;
1044 enum { NO_CHANGE
, SET_TO_MAX
, INCREASED_MAX
} set_rlimit
= NO_CHANGE
;
1046 if (evsel
->system_wide
)
1049 nthreads
= threads
->nr
;
1051 if (evsel
->fd
== NULL
&&
1052 perf_evsel__alloc_fd(evsel
, cpus
->nr
, nthreads
) < 0)
1056 flags
|= PERF_FLAG_PID_CGROUP
;
1057 pid
= evsel
->cgrp
->fd
;
1060 fallback_missing_features
:
1061 if (perf_missing_features
.cloexec
)
1062 flags
&= ~(unsigned long)PERF_FLAG_FD_CLOEXEC
;
1063 if (perf_missing_features
.mmap2
)
1064 evsel
->attr
.mmap2
= 0;
1065 if (perf_missing_features
.exclude_guest
)
1066 evsel
->attr
.exclude_guest
= evsel
->attr
.exclude_host
= 0;
1068 if (perf_missing_features
.sample_id_all
)
1069 evsel
->attr
.sample_id_all
= 0;
1072 perf_event_attr__fprintf(&evsel
->attr
, stderr
);
1074 for (cpu
= 0; cpu
< cpus
->nr
; cpu
++) {
1076 for (thread
= 0; thread
< nthreads
; thread
++) {
1079 if (!evsel
->cgrp
&& !evsel
->system_wide
)
1080 pid
= threads
->map
[thread
];
1082 group_fd
= get_group_fd(evsel
, cpu
, thread
);
1084 pr_debug2("sys_perf_event_open: pid %d cpu %d group_fd %d flags %#lx\n",
1085 pid
, cpus
->map
[cpu
], group_fd
, flags
);
1087 FD(evsel
, cpu
, thread
) = sys_perf_event_open(&evsel
->attr
,
1091 if (FD(evsel
, cpu
, thread
) < 0) {
1093 pr_debug2("sys_perf_event_open failed, error %d\n",
1097 set_rlimit
= NO_CHANGE
;
1105 * perf stat needs between 5 and 22 fds per CPU. When we run out
1106 * of them try to increase the limits.
1108 if (err
== -EMFILE
&& set_rlimit
< INCREASED_MAX
) {
1110 int old_errno
= errno
;
1112 if (getrlimit(RLIMIT_NOFILE
, &l
) == 0) {
1113 if (set_rlimit
== NO_CHANGE
)
1114 l
.rlim_cur
= l
.rlim_max
;
1116 l
.rlim_cur
= l
.rlim_max
+ 1000;
1117 l
.rlim_max
= l
.rlim_cur
;
1119 if (setrlimit(RLIMIT_NOFILE
, &l
) == 0) {
1128 if (err
!= -EINVAL
|| cpu
> 0 || thread
> 0)
1131 if (!perf_missing_features
.cloexec
&& (flags
& PERF_FLAG_FD_CLOEXEC
)) {
1132 perf_missing_features
.cloexec
= true;
1133 goto fallback_missing_features
;
1134 } else if (!perf_missing_features
.mmap2
&& evsel
->attr
.mmap2
) {
1135 perf_missing_features
.mmap2
= true;
1136 goto fallback_missing_features
;
1137 } else if (!perf_missing_features
.exclude_guest
&&
1138 (evsel
->attr
.exclude_guest
|| evsel
->attr
.exclude_host
)) {
1139 perf_missing_features
.exclude_guest
= true;
1140 goto fallback_missing_features
;
1141 } else if (!perf_missing_features
.sample_id_all
) {
1142 perf_missing_features
.sample_id_all
= true;
1143 goto retry_sample_id
;
1148 while (--thread
>= 0) {
1149 close(FD(evsel
, cpu
, thread
));
1150 FD(evsel
, cpu
, thread
) = -1;
1153 } while (--cpu
>= 0);
1157 void perf_evsel__close(struct perf_evsel
*evsel
, int ncpus
, int nthreads
)
1159 if (evsel
->fd
== NULL
)
1162 perf_evsel__close_fd(evsel
, ncpus
, nthreads
);
1163 perf_evsel__free_fd(evsel
);
1175 struct thread_map map
;
1177 } empty_thread_map
= {
1182 int perf_evsel__open(struct perf_evsel
*evsel
, struct cpu_map
*cpus
,
1183 struct thread_map
*threads
)
1186 /* Work around old compiler warnings about strict aliasing */
1187 cpus
= &empty_cpu_map
.map
;
1190 if (threads
== NULL
)
1191 threads
= &empty_thread_map
.map
;
1193 return __perf_evsel__open(evsel
, cpus
, threads
);
1196 int perf_evsel__open_per_cpu(struct perf_evsel
*evsel
,
1197 struct cpu_map
*cpus
)
1199 return __perf_evsel__open(evsel
, cpus
, &empty_thread_map
.map
);
1202 int perf_evsel__open_per_thread(struct perf_evsel
*evsel
,
1203 struct thread_map
*threads
)
1205 return __perf_evsel__open(evsel
, &empty_cpu_map
.map
, threads
);
1208 static int perf_evsel__parse_id_sample(const struct perf_evsel
*evsel
,
1209 const union perf_event
*event
,
1210 struct perf_sample
*sample
)
1212 u64 type
= evsel
->attr
.sample_type
;
1213 const u64
*array
= event
->sample
.array
;
1214 bool swapped
= evsel
->needs_swap
;
1217 array
+= ((event
->header
.size
-
1218 sizeof(event
->header
)) / sizeof(u64
)) - 1;
1220 if (type
& PERF_SAMPLE_IDENTIFIER
) {
1221 sample
->id
= *array
;
1225 if (type
& PERF_SAMPLE_CPU
) {
1228 /* undo swap of u64, then swap on individual u32s */
1229 u
.val64
= bswap_64(u
.val64
);
1230 u
.val32
[0] = bswap_32(u
.val32
[0]);
1233 sample
->cpu
= u
.val32
[0];
1237 if (type
& PERF_SAMPLE_STREAM_ID
) {
1238 sample
->stream_id
= *array
;
1242 if (type
& PERF_SAMPLE_ID
) {
1243 sample
->id
= *array
;
1247 if (type
& PERF_SAMPLE_TIME
) {
1248 sample
->time
= *array
;
1252 if (type
& PERF_SAMPLE_TID
) {
1255 /* undo swap of u64, then swap on individual u32s */
1256 u
.val64
= bswap_64(u
.val64
);
1257 u
.val32
[0] = bswap_32(u
.val32
[0]);
1258 u
.val32
[1] = bswap_32(u
.val32
[1]);
1261 sample
->pid
= u
.val32
[0];
1262 sample
->tid
= u
.val32
[1];
1269 static inline bool overflow(const void *endp
, u16 max_size
, const void *offset
,
1272 return size
> max_size
|| offset
+ size
> endp
;
1275 #define OVERFLOW_CHECK(offset, size, max_size) \
1277 if (overflow(endp, (max_size), (offset), (size))) \
1281 #define OVERFLOW_CHECK_u64(offset) \
1282 OVERFLOW_CHECK(offset, sizeof(u64), sizeof(u64))
1284 int perf_evsel__parse_sample(struct perf_evsel
*evsel
, union perf_event
*event
,
1285 struct perf_sample
*data
)
1287 u64 type
= evsel
->attr
.sample_type
;
1288 bool swapped
= evsel
->needs_swap
;
1290 u16 max_size
= event
->header
.size
;
1291 const void *endp
= (void *)event
+ max_size
;
1295 * used for cross-endian analysis. See git commit 65014ab3
1296 * for why this goofiness is needed.
1300 memset(data
, 0, sizeof(*data
));
1301 data
->cpu
= data
->pid
= data
->tid
= -1;
1302 data
->stream_id
= data
->id
= data
->time
= -1ULL;
1303 data
->period
= evsel
->attr
.sample_period
;
1306 if (event
->header
.type
!= PERF_RECORD_SAMPLE
) {
1307 if (!evsel
->attr
.sample_id_all
)
1309 return perf_evsel__parse_id_sample(evsel
, event
, data
);
1312 array
= event
->sample
.array
;
1315 * The evsel's sample_size is based on PERF_SAMPLE_MASK which includes
1316 * up to PERF_SAMPLE_PERIOD. After that overflow() must be used to
1317 * check the format does not go past the end of the event.
1319 if (evsel
->sample_size
+ sizeof(event
->header
) > event
->header
.size
)
1323 if (type
& PERF_SAMPLE_IDENTIFIER
) {
1328 if (type
& PERF_SAMPLE_IP
) {
1333 if (type
& PERF_SAMPLE_TID
) {
1336 /* undo swap of u64, then swap on individual u32s */
1337 u
.val64
= bswap_64(u
.val64
);
1338 u
.val32
[0] = bswap_32(u
.val32
[0]);
1339 u
.val32
[1] = bswap_32(u
.val32
[1]);
1342 data
->pid
= u
.val32
[0];
1343 data
->tid
= u
.val32
[1];
1347 if (type
& PERF_SAMPLE_TIME
) {
1348 data
->time
= *array
;
1353 if (type
& PERF_SAMPLE_ADDR
) {
1354 data
->addr
= *array
;
1358 if (type
& PERF_SAMPLE_ID
) {
1363 if (type
& PERF_SAMPLE_STREAM_ID
) {
1364 data
->stream_id
= *array
;
1368 if (type
& PERF_SAMPLE_CPU
) {
1372 /* undo swap of u64, then swap on individual u32s */
1373 u
.val64
= bswap_64(u
.val64
);
1374 u
.val32
[0] = bswap_32(u
.val32
[0]);
1377 data
->cpu
= u
.val32
[0];
1381 if (type
& PERF_SAMPLE_PERIOD
) {
1382 data
->period
= *array
;
1386 if (type
& PERF_SAMPLE_READ
) {
1387 u64 read_format
= evsel
->attr
.read_format
;
1389 OVERFLOW_CHECK_u64(array
);
1390 if (read_format
& PERF_FORMAT_GROUP
)
1391 data
->read
.group
.nr
= *array
;
1393 data
->read
.one
.value
= *array
;
1397 if (read_format
& PERF_FORMAT_TOTAL_TIME_ENABLED
) {
1398 OVERFLOW_CHECK_u64(array
);
1399 data
->read
.time_enabled
= *array
;
1403 if (read_format
& PERF_FORMAT_TOTAL_TIME_RUNNING
) {
1404 OVERFLOW_CHECK_u64(array
);
1405 data
->read
.time_running
= *array
;
1409 /* PERF_FORMAT_ID is forced for PERF_SAMPLE_READ */
1410 if (read_format
& PERF_FORMAT_GROUP
) {
1411 const u64 max_group_nr
= UINT64_MAX
/
1412 sizeof(struct sample_read_value
);
1414 if (data
->read
.group
.nr
> max_group_nr
)
1416 sz
= data
->read
.group
.nr
*
1417 sizeof(struct sample_read_value
);
1418 OVERFLOW_CHECK(array
, sz
, max_size
);
1419 data
->read
.group
.values
=
1420 (struct sample_read_value
*)array
;
1421 array
= (void *)array
+ sz
;
1423 OVERFLOW_CHECK_u64(array
);
1424 data
->read
.one
.id
= *array
;
1429 if (type
& PERF_SAMPLE_CALLCHAIN
) {
1430 const u64 max_callchain_nr
= UINT64_MAX
/ sizeof(u64
);
1432 OVERFLOW_CHECK_u64(array
);
1433 data
->callchain
= (struct ip_callchain
*)array
++;
1434 if (data
->callchain
->nr
> max_callchain_nr
)
1436 sz
= data
->callchain
->nr
* sizeof(u64
);
1437 OVERFLOW_CHECK(array
, sz
, max_size
);
1438 array
= (void *)array
+ sz
;
1441 if (type
& PERF_SAMPLE_RAW
) {
1442 OVERFLOW_CHECK_u64(array
);
1444 if (WARN_ONCE(swapped
,
1445 "Endianness of raw data not corrected!\n")) {
1446 /* undo swap of u64, then swap on individual u32s */
1447 u
.val64
= bswap_64(u
.val64
);
1448 u
.val32
[0] = bswap_32(u
.val32
[0]);
1449 u
.val32
[1] = bswap_32(u
.val32
[1]);
1451 data
->raw_size
= u
.val32
[0];
1452 array
= (void *)array
+ sizeof(u32
);
1454 OVERFLOW_CHECK(array
, data
->raw_size
, max_size
);
1455 data
->raw_data
= (void *)array
;
1456 array
= (void *)array
+ data
->raw_size
;
1459 if (type
& PERF_SAMPLE_BRANCH_STACK
) {
1460 const u64 max_branch_nr
= UINT64_MAX
/
1461 sizeof(struct branch_entry
);
1463 OVERFLOW_CHECK_u64(array
);
1464 data
->branch_stack
= (struct branch_stack
*)array
++;
1466 if (data
->branch_stack
->nr
> max_branch_nr
)
1468 sz
= data
->branch_stack
->nr
* sizeof(struct branch_entry
);
1469 OVERFLOW_CHECK(array
, sz
, max_size
);
1470 array
= (void *)array
+ sz
;
1473 if (type
& PERF_SAMPLE_REGS_USER
) {
1474 OVERFLOW_CHECK_u64(array
);
1475 data
->user_regs
.abi
= *array
;
1478 if (data
->user_regs
.abi
) {
1479 u64 mask
= evsel
->attr
.sample_regs_user
;
1481 sz
= hweight_long(mask
) * sizeof(u64
);
1482 OVERFLOW_CHECK(array
, sz
, max_size
);
1483 data
->user_regs
.mask
= mask
;
1484 data
->user_regs
.regs
= (u64
*)array
;
1485 array
= (void *)array
+ sz
;
1489 if (type
& PERF_SAMPLE_STACK_USER
) {
1490 OVERFLOW_CHECK_u64(array
);
1493 data
->user_stack
.offset
= ((char *)(array
- 1)
1497 data
->user_stack
.size
= 0;
1499 OVERFLOW_CHECK(array
, sz
, max_size
);
1500 data
->user_stack
.data
= (char *)array
;
1501 array
= (void *)array
+ sz
;
1502 OVERFLOW_CHECK_u64(array
);
1503 data
->user_stack
.size
= *array
++;
1504 if (WARN_ONCE(data
->user_stack
.size
> sz
,
1505 "user stack dump failure\n"))
1511 if (type
& PERF_SAMPLE_WEIGHT
) {
1512 OVERFLOW_CHECK_u64(array
);
1513 data
->weight
= *array
;
1517 data
->data_src
= PERF_MEM_DATA_SRC_NONE
;
1518 if (type
& PERF_SAMPLE_DATA_SRC
) {
1519 OVERFLOW_CHECK_u64(array
);
1520 data
->data_src
= *array
;
1524 data
->transaction
= 0;
1525 if (type
& PERF_SAMPLE_TRANSACTION
) {
1526 OVERFLOW_CHECK_u64(array
);
1527 data
->transaction
= *array
;
1531 data
->intr_regs
.abi
= PERF_SAMPLE_REGS_ABI_NONE
;
1532 if (type
& PERF_SAMPLE_REGS_INTR
) {
1533 OVERFLOW_CHECK_u64(array
);
1534 data
->intr_regs
.abi
= *array
;
1537 if (data
->intr_regs
.abi
!= PERF_SAMPLE_REGS_ABI_NONE
) {
1538 u64 mask
= evsel
->attr
.sample_regs_intr
;
1540 sz
= hweight_long(mask
) * sizeof(u64
);
1541 OVERFLOW_CHECK(array
, sz
, max_size
);
1542 data
->intr_regs
.mask
= mask
;
1543 data
->intr_regs
.regs
= (u64
*)array
;
1544 array
= (void *)array
+ sz
;
1551 size_t perf_event__sample_event_size(const struct perf_sample
*sample
, u64 type
,
1554 size_t sz
, result
= sizeof(struct sample_event
);
1556 if (type
& PERF_SAMPLE_IDENTIFIER
)
1557 result
+= sizeof(u64
);
1559 if (type
& PERF_SAMPLE_IP
)
1560 result
+= sizeof(u64
);
1562 if (type
& PERF_SAMPLE_TID
)
1563 result
+= sizeof(u64
);
1565 if (type
& PERF_SAMPLE_TIME
)
1566 result
+= sizeof(u64
);
1568 if (type
& PERF_SAMPLE_ADDR
)
1569 result
+= sizeof(u64
);
1571 if (type
& PERF_SAMPLE_ID
)
1572 result
+= sizeof(u64
);
1574 if (type
& PERF_SAMPLE_STREAM_ID
)
1575 result
+= sizeof(u64
);
1577 if (type
& PERF_SAMPLE_CPU
)
1578 result
+= sizeof(u64
);
1580 if (type
& PERF_SAMPLE_PERIOD
)
1581 result
+= sizeof(u64
);
1583 if (type
& PERF_SAMPLE_READ
) {
1584 result
+= sizeof(u64
);
1585 if (read_format
& PERF_FORMAT_TOTAL_TIME_ENABLED
)
1586 result
+= sizeof(u64
);
1587 if (read_format
& PERF_FORMAT_TOTAL_TIME_RUNNING
)
1588 result
+= sizeof(u64
);
1589 /* PERF_FORMAT_ID is forced for PERF_SAMPLE_READ */
1590 if (read_format
& PERF_FORMAT_GROUP
) {
1591 sz
= sample
->read
.group
.nr
*
1592 sizeof(struct sample_read_value
);
1595 result
+= sizeof(u64
);
1599 if (type
& PERF_SAMPLE_CALLCHAIN
) {
1600 sz
= (sample
->callchain
->nr
+ 1) * sizeof(u64
);
1604 if (type
& PERF_SAMPLE_RAW
) {
1605 result
+= sizeof(u32
);
1606 result
+= sample
->raw_size
;
1609 if (type
& PERF_SAMPLE_BRANCH_STACK
) {
1610 sz
= sample
->branch_stack
->nr
* sizeof(struct branch_entry
);
1615 if (type
& PERF_SAMPLE_REGS_USER
) {
1616 if (sample
->user_regs
.abi
) {
1617 result
+= sizeof(u64
);
1618 sz
= hweight_long(sample
->user_regs
.mask
) * sizeof(u64
);
1621 result
+= sizeof(u64
);
1625 if (type
& PERF_SAMPLE_STACK_USER
) {
1626 sz
= sample
->user_stack
.size
;
1627 result
+= sizeof(u64
);
1630 result
+= sizeof(u64
);
1634 if (type
& PERF_SAMPLE_WEIGHT
)
1635 result
+= sizeof(u64
);
1637 if (type
& PERF_SAMPLE_DATA_SRC
)
1638 result
+= sizeof(u64
);
1640 if (type
& PERF_SAMPLE_TRANSACTION
)
1641 result
+= sizeof(u64
);
1643 if (type
& PERF_SAMPLE_REGS_INTR
) {
1644 if (sample
->intr_regs
.abi
) {
1645 result
+= sizeof(u64
);
1646 sz
= hweight_long(sample
->intr_regs
.mask
) * sizeof(u64
);
1649 result
+= sizeof(u64
);
1656 int perf_event__synthesize_sample(union perf_event
*event
, u64 type
,
1658 const struct perf_sample
*sample
,
1664 * used for cross-endian analysis. See git commit 65014ab3
1665 * for why this goofiness is needed.
1669 array
= event
->sample
.array
;
1671 if (type
& PERF_SAMPLE_IDENTIFIER
) {
1672 *array
= sample
->id
;
1676 if (type
& PERF_SAMPLE_IP
) {
1677 *array
= sample
->ip
;
1681 if (type
& PERF_SAMPLE_TID
) {
1682 u
.val32
[0] = sample
->pid
;
1683 u
.val32
[1] = sample
->tid
;
1686 * Inverse of what is done in perf_evsel__parse_sample
1688 u
.val32
[0] = bswap_32(u
.val32
[0]);
1689 u
.val32
[1] = bswap_32(u
.val32
[1]);
1690 u
.val64
= bswap_64(u
.val64
);
1697 if (type
& PERF_SAMPLE_TIME
) {
1698 *array
= sample
->time
;
1702 if (type
& PERF_SAMPLE_ADDR
) {
1703 *array
= sample
->addr
;
1707 if (type
& PERF_SAMPLE_ID
) {
1708 *array
= sample
->id
;
1712 if (type
& PERF_SAMPLE_STREAM_ID
) {
1713 *array
= sample
->stream_id
;
1717 if (type
& PERF_SAMPLE_CPU
) {
1718 u
.val32
[0] = sample
->cpu
;
1721 * Inverse of what is done in perf_evsel__parse_sample
1723 u
.val32
[0] = bswap_32(u
.val32
[0]);
1724 u
.val64
= bswap_64(u
.val64
);
1730 if (type
& PERF_SAMPLE_PERIOD
) {
1731 *array
= sample
->period
;
1735 if (type
& PERF_SAMPLE_READ
) {
1736 if (read_format
& PERF_FORMAT_GROUP
)
1737 *array
= sample
->read
.group
.nr
;
1739 *array
= sample
->read
.one
.value
;
1742 if (read_format
& PERF_FORMAT_TOTAL_TIME_ENABLED
) {
1743 *array
= sample
->read
.time_enabled
;
1747 if (read_format
& PERF_FORMAT_TOTAL_TIME_RUNNING
) {
1748 *array
= sample
->read
.time_running
;
1752 /* PERF_FORMAT_ID is forced for PERF_SAMPLE_READ */
1753 if (read_format
& PERF_FORMAT_GROUP
) {
1754 sz
= sample
->read
.group
.nr
*
1755 sizeof(struct sample_read_value
);
1756 memcpy(array
, sample
->read
.group
.values
, sz
);
1757 array
= (void *)array
+ sz
;
1759 *array
= sample
->read
.one
.id
;
1764 if (type
& PERF_SAMPLE_CALLCHAIN
) {
1765 sz
= (sample
->callchain
->nr
+ 1) * sizeof(u64
);
1766 memcpy(array
, sample
->callchain
, sz
);
1767 array
= (void *)array
+ sz
;
1770 if (type
& PERF_SAMPLE_RAW
) {
1771 u
.val32
[0] = sample
->raw_size
;
1772 if (WARN_ONCE(swapped
,
1773 "Endianness of raw data not corrected!\n")) {
1775 * Inverse of what is done in perf_evsel__parse_sample
1777 u
.val32
[0] = bswap_32(u
.val32
[0]);
1778 u
.val32
[1] = bswap_32(u
.val32
[1]);
1779 u
.val64
= bswap_64(u
.val64
);
1782 array
= (void *)array
+ sizeof(u32
);
1784 memcpy(array
, sample
->raw_data
, sample
->raw_size
);
1785 array
= (void *)array
+ sample
->raw_size
;
1788 if (type
& PERF_SAMPLE_BRANCH_STACK
) {
1789 sz
= sample
->branch_stack
->nr
* sizeof(struct branch_entry
);
1791 memcpy(array
, sample
->branch_stack
, sz
);
1792 array
= (void *)array
+ sz
;
1795 if (type
& PERF_SAMPLE_REGS_USER
) {
1796 if (sample
->user_regs
.abi
) {
1797 *array
++ = sample
->user_regs
.abi
;
1798 sz
= hweight_long(sample
->user_regs
.mask
) * sizeof(u64
);
1799 memcpy(array
, sample
->user_regs
.regs
, sz
);
1800 array
= (void *)array
+ sz
;
1806 if (type
& PERF_SAMPLE_STACK_USER
) {
1807 sz
= sample
->user_stack
.size
;
1810 memcpy(array
, sample
->user_stack
.data
, sz
);
1811 array
= (void *)array
+ sz
;
1816 if (type
& PERF_SAMPLE_WEIGHT
) {
1817 *array
= sample
->weight
;
1821 if (type
& PERF_SAMPLE_DATA_SRC
) {
1822 *array
= sample
->data_src
;
1826 if (type
& PERF_SAMPLE_TRANSACTION
) {
1827 *array
= sample
->transaction
;
1831 if (type
& PERF_SAMPLE_REGS_INTR
) {
1832 if (sample
->intr_regs
.abi
) {
1833 *array
++ = sample
->intr_regs
.abi
;
1834 sz
= hweight_long(sample
->intr_regs
.mask
) * sizeof(u64
);
1835 memcpy(array
, sample
->intr_regs
.regs
, sz
);
1836 array
= (void *)array
+ sz
;
1845 struct format_field
*perf_evsel__field(struct perf_evsel
*evsel
, const char *name
)
1847 return pevent_find_field(evsel
->tp_format
, name
);
1850 void *perf_evsel__rawptr(struct perf_evsel
*evsel
, struct perf_sample
*sample
,
1853 struct format_field
*field
= perf_evsel__field(evsel
, name
);
1859 offset
= field
->offset
;
1861 if (field
->flags
& FIELD_IS_DYNAMIC
) {
1862 offset
= *(int *)(sample
->raw_data
+ field
->offset
);
1866 return sample
->raw_data
+ offset
;
1869 u64
perf_evsel__intval(struct perf_evsel
*evsel
, struct perf_sample
*sample
,
1872 struct format_field
*field
= perf_evsel__field(evsel
, name
);
1879 ptr
= sample
->raw_data
+ field
->offset
;
1881 switch (field
->size
) {
1885 value
= *(u16
*)ptr
;
1888 value
= *(u32
*)ptr
;
1891 value
= *(u64
*)ptr
;
1897 if (!evsel
->needs_swap
)
1900 switch (field
->size
) {
1902 return bswap_16(value
);
1904 return bswap_32(value
);
1906 return bswap_64(value
);
1914 static int comma_fprintf(FILE *fp
, bool *first
, const char *fmt
, ...)
1920 ret
+= fprintf(fp
, ",");
1922 ret
+= fprintf(fp
, ":");
1926 va_start(args
, fmt
);
1927 ret
+= vfprintf(fp
, fmt
, args
);
1932 static int __if_fprintf(FILE *fp
, bool *first
, const char *field
, u64 value
)
1937 return comma_fprintf(fp
, first
, " %s: %" PRIu64
, field
, value
);
1940 #define if_print(field) printed += __if_fprintf(fp, &first, #field, evsel->attr.field)
1947 static int bits__fprintf(FILE *fp
, const char *field
, u64 value
,
1948 struct bit_names
*bits
, bool *first
)
1950 int i
= 0, printed
= comma_fprintf(fp
, first
, " %s: ", field
);
1951 bool first_bit
= true;
1954 if (value
& bits
[i
].bit
) {
1955 printed
+= fprintf(fp
, "%s%s", first_bit
? "" : "|", bits
[i
].name
);
1958 } while (bits
[++i
].name
!= NULL
);
1963 static int sample_type__fprintf(FILE *fp
, bool *first
, u64 value
)
1965 #define bit_name(n) { PERF_SAMPLE_##n, #n }
1966 struct bit_names bits
[] = {
1967 bit_name(IP
), bit_name(TID
), bit_name(TIME
), bit_name(ADDR
),
1968 bit_name(READ
), bit_name(CALLCHAIN
), bit_name(ID
), bit_name(CPU
),
1969 bit_name(PERIOD
), bit_name(STREAM_ID
), bit_name(RAW
),
1970 bit_name(BRANCH_STACK
), bit_name(REGS_USER
), bit_name(STACK_USER
),
1971 bit_name(IDENTIFIER
), bit_name(REGS_INTR
),
1975 return bits__fprintf(fp
, "sample_type", value
, bits
, first
);
1978 static int read_format__fprintf(FILE *fp
, bool *first
, u64 value
)
1980 #define bit_name(n) { PERF_FORMAT_##n, #n }
1981 struct bit_names bits
[] = {
1982 bit_name(TOTAL_TIME_ENABLED
), bit_name(TOTAL_TIME_RUNNING
),
1983 bit_name(ID
), bit_name(GROUP
),
1987 return bits__fprintf(fp
, "read_format", value
, bits
, first
);
1990 int perf_evsel__fprintf(struct perf_evsel
*evsel
,
1991 struct perf_attr_details
*details
, FILE *fp
)
1996 if (details
->event_group
) {
1997 struct perf_evsel
*pos
;
1999 if (!perf_evsel__is_group_leader(evsel
))
2002 if (evsel
->nr_members
> 1)
2003 printed
+= fprintf(fp
, "%s{", evsel
->group_name
?: "");
2005 printed
+= fprintf(fp
, "%s", perf_evsel__name(evsel
));
2006 for_each_group_member(pos
, evsel
)
2007 printed
+= fprintf(fp
, ",%s", perf_evsel__name(pos
));
2009 if (evsel
->nr_members
> 1)
2010 printed
+= fprintf(fp
, "}");
2014 printed
+= fprintf(fp
, "%s", perf_evsel__name(evsel
));
2016 if (details
->verbose
|| details
->freq
) {
2017 printed
+= comma_fprintf(fp
, &first
, " sample_freq=%" PRIu64
,
2018 (u64
)evsel
->attr
.sample_freq
);
2021 if (details
->verbose
) {
2027 printed
+= sample_type__fprintf(fp
, &first
, evsel
->attr
.sample_type
);
2028 if (evsel
->attr
.read_format
)
2029 printed
+= read_format__fprintf(fp
, &first
, evsel
->attr
.read_format
);
2033 if_print(exclusive
);
2034 if_print(exclude_user
);
2035 if_print(exclude_kernel
);
2036 if_print(exclude_hv
);
2037 if_print(exclude_idle
);
2041 if_print(comm_exec
);
2043 if_print(inherit_stat
);
2044 if_print(enable_on_exec
);
2046 if_print(watermark
);
2047 if_print(precise_ip
);
2048 if_print(mmap_data
);
2049 if_print(sample_id_all
);
2050 if_print(exclude_host
);
2051 if_print(exclude_guest
);
2052 if_print(__reserved_1
);
2053 if_print(wakeup_events
);
2055 if_print(branch_sample_type
);
2062 bool perf_evsel__fallback(struct perf_evsel
*evsel
, int err
,
2063 char *msg
, size_t msgsize
)
2065 if ((err
== ENOENT
|| err
== ENXIO
|| err
== ENODEV
) &&
2066 evsel
->attr
.type
== PERF_TYPE_HARDWARE
&&
2067 evsel
->attr
.config
== PERF_COUNT_HW_CPU_CYCLES
) {
2069 * If it's cycles then fall back to hrtimer based
2070 * cpu-clock-tick sw counter, which is always available even if
2073 * PPC returns ENXIO until 2.6.37 (behavior changed with commit
2076 scnprintf(msg
, msgsize
, "%s",
2077 "The cycles event is not supported, trying to fall back to cpu-clock-ticks");
2079 evsel
->attr
.type
= PERF_TYPE_SOFTWARE
;
2080 evsel
->attr
.config
= PERF_COUNT_SW_CPU_CLOCK
;
2082 zfree(&evsel
->name
);
2089 int perf_evsel__open_strerror(struct perf_evsel
*evsel
, struct target
*target
,
2090 int err
, char *msg
, size_t size
)
2092 char sbuf
[STRERR_BUFSIZE
];
2097 return scnprintf(msg
, size
,
2098 "You may not have permission to collect %sstats.\n"
2099 "Consider tweaking /proc/sys/kernel/perf_event_paranoid:\n"
2100 " -1 - Not paranoid at all\n"
2101 " 0 - Disallow raw tracepoint access for unpriv\n"
2102 " 1 - Disallow cpu events for unpriv\n"
2103 " 2 - Disallow kernel profiling for unpriv",
2104 target
->system_wide
? "system-wide " : "");
2106 return scnprintf(msg
, size
, "The %s event is not supported.",
2107 perf_evsel__name(evsel
));
2109 return scnprintf(msg
, size
, "%s",
2110 "Too many events are opened.\n"
2111 "Try again after reducing the number of events.");
2113 if (target
->cpu_list
)
2114 return scnprintf(msg
, size
, "%s",
2115 "No such device - did you specify an out-of-range profile CPU?\n");
2118 if (evsel
->attr
.precise_ip
)
2119 return scnprintf(msg
, size
, "%s",
2120 "\'precise\' request may not be supported. Try removing 'p' modifier.");
2121 #if defined(__i386__) || defined(__x86_64__)
2122 if (evsel
->attr
.type
== PERF_TYPE_HARDWARE
)
2123 return scnprintf(msg
, size
, "%s",
2124 "No hardware sampling interrupt available.\n"
2125 "No APIC? If so then you can boot the kernel with the \"lapic\" boot parameter to force-enable it.");
2129 if (find_process("oprofiled"))
2130 return scnprintf(msg
, size
,
2131 "The PMU counters are busy/taken by another profiler.\n"
2132 "We found oprofile daemon running, please stop it and try again.");
2138 return scnprintf(msg
, size
,
2139 "The sys_perf_event_open() syscall returned with %d (%s) for event (%s).\n"
2140 "/bin/dmesg may provide additional information.\n"
2141 "No CONFIG_PERF_EVENTS=y kernel support configured?\n",
2142 err
, strerror_r(err
, sbuf
, sizeof(sbuf
)),
2143 perf_evsel__name(evsel
));