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>
22 #include "thread_map.h"
24 #include "perf_regs.h"
26 #include "trace-event.h"
33 } perf_missing_features
;
35 static int perf_evsel__no_extra_init(struct perf_evsel
*evsel __maybe_unused
)
40 static void perf_evsel__no_extra_fini(struct perf_evsel
*evsel __maybe_unused
)
46 int (*init
)(struct perf_evsel
*evsel
);
47 void (*fini
)(struct perf_evsel
*evsel
);
48 } perf_evsel__object
= {
49 .size
= sizeof(struct perf_evsel
),
50 .init
= perf_evsel__no_extra_init
,
51 .fini
= perf_evsel__no_extra_fini
,
54 int perf_evsel__object_config(size_t object_size
,
55 int (*init
)(struct perf_evsel
*evsel
),
56 void (*fini
)(struct perf_evsel
*evsel
))
62 if (perf_evsel__object
.size
> object_size
)
65 perf_evsel__object
.size
= object_size
;
69 perf_evsel__object
.init
= init
;
72 perf_evsel__object
.fini
= fini
;
77 #define FD(e, x, y) (*(int *)xyarray__entry(e->fd, x, y))
79 int __perf_evsel__sample_size(u64 sample_type
)
81 u64 mask
= sample_type
& PERF_SAMPLE_MASK
;
85 for (i
= 0; i
< 64; i
++) {
86 if (mask
& (1ULL << i
))
96 * __perf_evsel__calc_id_pos - calculate id_pos.
97 * @sample_type: sample type
99 * This function returns the position of the event id (PERF_SAMPLE_ID or
100 * PERF_SAMPLE_IDENTIFIER) in a sample event i.e. in the array of struct
103 static int __perf_evsel__calc_id_pos(u64 sample_type
)
107 if (sample_type
& PERF_SAMPLE_IDENTIFIER
)
110 if (!(sample_type
& PERF_SAMPLE_ID
))
113 if (sample_type
& PERF_SAMPLE_IP
)
116 if (sample_type
& PERF_SAMPLE_TID
)
119 if (sample_type
& PERF_SAMPLE_TIME
)
122 if (sample_type
& PERF_SAMPLE_ADDR
)
129 * __perf_evsel__calc_is_pos - calculate is_pos.
130 * @sample_type: sample type
132 * This function returns the position (counting backwards) of the event id
133 * (PERF_SAMPLE_ID or PERF_SAMPLE_IDENTIFIER) in a non-sample event i.e. if
134 * sample_id_all is used there is an id sample appended to non-sample events.
136 static int __perf_evsel__calc_is_pos(u64 sample_type
)
140 if (sample_type
& PERF_SAMPLE_IDENTIFIER
)
143 if (!(sample_type
& PERF_SAMPLE_ID
))
146 if (sample_type
& PERF_SAMPLE_CPU
)
149 if (sample_type
& PERF_SAMPLE_STREAM_ID
)
155 void perf_evsel__calc_id_pos(struct perf_evsel
*evsel
)
157 evsel
->id_pos
= __perf_evsel__calc_id_pos(evsel
->attr
.sample_type
);
158 evsel
->is_pos
= __perf_evsel__calc_is_pos(evsel
->attr
.sample_type
);
161 void hists__init(struct hists
*hists
)
163 memset(hists
, 0, sizeof(*hists
));
164 hists
->entries_in_array
[0] = hists
->entries_in_array
[1] = RB_ROOT
;
165 hists
->entries_in
= &hists
->entries_in_array
[0];
166 hists
->entries_collapsed
= RB_ROOT
;
167 hists
->entries
= RB_ROOT
;
168 pthread_mutex_init(&hists
->lock
, NULL
);
171 void __perf_evsel__set_sample_bit(struct perf_evsel
*evsel
,
172 enum perf_event_sample_format bit
)
174 if (!(evsel
->attr
.sample_type
& bit
)) {
175 evsel
->attr
.sample_type
|= bit
;
176 evsel
->sample_size
+= sizeof(u64
);
177 perf_evsel__calc_id_pos(evsel
);
181 void __perf_evsel__reset_sample_bit(struct perf_evsel
*evsel
,
182 enum perf_event_sample_format bit
)
184 if (evsel
->attr
.sample_type
& bit
) {
185 evsel
->attr
.sample_type
&= ~bit
;
186 evsel
->sample_size
-= sizeof(u64
);
187 perf_evsel__calc_id_pos(evsel
);
191 void perf_evsel__set_sample_id(struct perf_evsel
*evsel
,
192 bool can_sample_identifier
)
194 if (can_sample_identifier
) {
195 perf_evsel__reset_sample_bit(evsel
, ID
);
196 perf_evsel__set_sample_bit(evsel
, IDENTIFIER
);
198 perf_evsel__set_sample_bit(evsel
, ID
);
200 evsel
->attr
.read_format
|= PERF_FORMAT_ID
;
203 void perf_evsel__init(struct perf_evsel
*evsel
,
204 struct perf_event_attr
*attr
, int idx
)
207 evsel
->tracking
= !idx
;
209 evsel
->leader
= evsel
;
212 INIT_LIST_HEAD(&evsel
->node
);
213 hists__init(&evsel
->hists
);
214 perf_evsel__object
.init(evsel
);
215 evsel
->sample_size
= __perf_evsel__sample_size(attr
->sample_type
);
216 perf_evsel__calc_id_pos(evsel
);
219 struct perf_evsel
*perf_evsel__new_idx(struct perf_event_attr
*attr
, int idx
)
221 struct perf_evsel
*evsel
= zalloc(perf_evsel__object
.size
);
224 perf_evsel__init(evsel
, attr
, idx
);
229 struct perf_evsel
*perf_evsel__newtp_idx(const char *sys
, const char *name
, int idx
)
231 struct perf_evsel
*evsel
= zalloc(perf_evsel__object
.size
);
234 struct perf_event_attr attr
= {
235 .type
= PERF_TYPE_TRACEPOINT
,
236 .sample_type
= (PERF_SAMPLE_RAW
| PERF_SAMPLE_TIME
|
237 PERF_SAMPLE_CPU
| PERF_SAMPLE_PERIOD
),
240 if (asprintf(&evsel
->name
, "%s:%s", sys
, name
) < 0)
243 evsel
->tp_format
= trace_event__tp_format(sys
, name
);
244 if (evsel
->tp_format
== NULL
)
247 event_attr_init(&attr
);
248 attr
.config
= evsel
->tp_format
->id
;
249 attr
.sample_period
= 1;
250 perf_evsel__init(evsel
, &attr
, idx
);
261 const char *perf_evsel__hw_names
[PERF_COUNT_HW_MAX
] = {
269 "stalled-cycles-frontend",
270 "stalled-cycles-backend",
274 static const char *__perf_evsel__hw_name(u64 config
)
276 if (config
< PERF_COUNT_HW_MAX
&& perf_evsel__hw_names
[config
])
277 return perf_evsel__hw_names
[config
];
279 return "unknown-hardware";
282 static int perf_evsel__add_modifiers(struct perf_evsel
*evsel
, char *bf
, size_t size
)
284 int colon
= 0, r
= 0;
285 struct perf_event_attr
*attr
= &evsel
->attr
;
286 bool exclude_guest_default
= false;
288 #define MOD_PRINT(context, mod) do { \
289 if (!attr->exclude_##context) { \
290 if (!colon) colon = ++r; \
291 r += scnprintf(bf + r, size - r, "%c", mod); \
294 if (attr
->exclude_kernel
|| attr
->exclude_user
|| attr
->exclude_hv
) {
295 MOD_PRINT(kernel
, 'k');
296 MOD_PRINT(user
, 'u');
298 exclude_guest_default
= true;
301 if (attr
->precise_ip
) {
304 r
+= scnprintf(bf
+ r
, size
- r
, "%.*s", attr
->precise_ip
, "ppp");
305 exclude_guest_default
= true;
308 if (attr
->exclude_host
|| attr
->exclude_guest
== exclude_guest_default
) {
309 MOD_PRINT(host
, 'H');
310 MOD_PRINT(guest
, 'G');
318 static int perf_evsel__hw_name(struct perf_evsel
*evsel
, char *bf
, size_t size
)
320 int r
= scnprintf(bf
, size
, "%s", __perf_evsel__hw_name(evsel
->attr
.config
));
321 return r
+ perf_evsel__add_modifiers(evsel
, bf
+ r
, size
- r
);
324 const char *perf_evsel__sw_names
[PERF_COUNT_SW_MAX
] = {
337 static const char *__perf_evsel__sw_name(u64 config
)
339 if (config
< PERF_COUNT_SW_MAX
&& perf_evsel__sw_names
[config
])
340 return perf_evsel__sw_names
[config
];
341 return "unknown-software";
344 static int perf_evsel__sw_name(struct perf_evsel
*evsel
, char *bf
, size_t size
)
346 int r
= scnprintf(bf
, size
, "%s", __perf_evsel__sw_name(evsel
->attr
.config
));
347 return r
+ perf_evsel__add_modifiers(evsel
, bf
+ r
, size
- r
);
350 static int __perf_evsel__bp_name(char *bf
, size_t size
, u64 addr
, u64 type
)
354 r
= scnprintf(bf
, size
, "mem:0x%" PRIx64
":", addr
);
356 if (type
& HW_BREAKPOINT_R
)
357 r
+= scnprintf(bf
+ r
, size
- r
, "r");
359 if (type
& HW_BREAKPOINT_W
)
360 r
+= scnprintf(bf
+ r
, size
- r
, "w");
362 if (type
& HW_BREAKPOINT_X
)
363 r
+= scnprintf(bf
+ r
, size
- r
, "x");
368 static int perf_evsel__bp_name(struct perf_evsel
*evsel
, char *bf
, size_t size
)
370 struct perf_event_attr
*attr
= &evsel
->attr
;
371 int r
= __perf_evsel__bp_name(bf
, size
, attr
->bp_addr
, attr
->bp_type
);
372 return r
+ perf_evsel__add_modifiers(evsel
, bf
+ r
, size
- r
);
375 const char *perf_evsel__hw_cache
[PERF_COUNT_HW_CACHE_MAX
]
376 [PERF_EVSEL__MAX_ALIASES
] = {
377 { "L1-dcache", "l1-d", "l1d", "L1-data", },
378 { "L1-icache", "l1-i", "l1i", "L1-instruction", },
380 { "dTLB", "d-tlb", "Data-TLB", },
381 { "iTLB", "i-tlb", "Instruction-TLB", },
382 { "branch", "branches", "bpu", "btb", "bpc", },
386 const char *perf_evsel__hw_cache_op
[PERF_COUNT_HW_CACHE_OP_MAX
]
387 [PERF_EVSEL__MAX_ALIASES
] = {
388 { "load", "loads", "read", },
389 { "store", "stores", "write", },
390 { "prefetch", "prefetches", "speculative-read", "speculative-load", },
393 const char *perf_evsel__hw_cache_result
[PERF_COUNT_HW_CACHE_RESULT_MAX
]
394 [PERF_EVSEL__MAX_ALIASES
] = {
395 { "refs", "Reference", "ops", "access", },
396 { "misses", "miss", },
399 #define C(x) PERF_COUNT_HW_CACHE_##x
400 #define CACHE_READ (1 << C(OP_READ))
401 #define CACHE_WRITE (1 << C(OP_WRITE))
402 #define CACHE_PREFETCH (1 << C(OP_PREFETCH))
403 #define COP(x) (1 << x)
406 * cache operartion stat
407 * L1I : Read and prefetch only
408 * ITLB and BPU : Read-only
410 static unsigned long perf_evsel__hw_cache_stat
[C(MAX
)] = {
411 [C(L1D
)] = (CACHE_READ
| CACHE_WRITE
| CACHE_PREFETCH
),
412 [C(L1I
)] = (CACHE_READ
| CACHE_PREFETCH
),
413 [C(LL
)] = (CACHE_READ
| CACHE_WRITE
| CACHE_PREFETCH
),
414 [C(DTLB
)] = (CACHE_READ
| CACHE_WRITE
| CACHE_PREFETCH
),
415 [C(ITLB
)] = (CACHE_READ
),
416 [C(BPU
)] = (CACHE_READ
),
417 [C(NODE
)] = (CACHE_READ
| CACHE_WRITE
| CACHE_PREFETCH
),
420 bool perf_evsel__is_cache_op_valid(u8 type
, u8 op
)
422 if (perf_evsel__hw_cache_stat
[type
] & COP(op
))
423 return true; /* valid */
425 return false; /* invalid */
428 int __perf_evsel__hw_cache_type_op_res_name(u8 type
, u8 op
, u8 result
,
429 char *bf
, size_t size
)
432 return scnprintf(bf
, size
, "%s-%s-%s", perf_evsel__hw_cache
[type
][0],
433 perf_evsel__hw_cache_op
[op
][0],
434 perf_evsel__hw_cache_result
[result
][0]);
437 return scnprintf(bf
, size
, "%s-%s", perf_evsel__hw_cache
[type
][0],
438 perf_evsel__hw_cache_op
[op
][1]);
441 static int __perf_evsel__hw_cache_name(u64 config
, char *bf
, size_t size
)
443 u8 op
, result
, type
= (config
>> 0) & 0xff;
444 const char *err
= "unknown-ext-hardware-cache-type";
446 if (type
> PERF_COUNT_HW_CACHE_MAX
)
449 op
= (config
>> 8) & 0xff;
450 err
= "unknown-ext-hardware-cache-op";
451 if (op
> PERF_COUNT_HW_CACHE_OP_MAX
)
454 result
= (config
>> 16) & 0xff;
455 err
= "unknown-ext-hardware-cache-result";
456 if (result
> PERF_COUNT_HW_CACHE_RESULT_MAX
)
459 err
= "invalid-cache";
460 if (!perf_evsel__is_cache_op_valid(type
, op
))
463 return __perf_evsel__hw_cache_type_op_res_name(type
, op
, result
, bf
, size
);
465 return scnprintf(bf
, size
, "%s", err
);
468 static int perf_evsel__hw_cache_name(struct perf_evsel
*evsel
, char *bf
, size_t size
)
470 int ret
= __perf_evsel__hw_cache_name(evsel
->attr
.config
, bf
, size
);
471 return ret
+ perf_evsel__add_modifiers(evsel
, bf
+ ret
, size
- ret
);
474 static int perf_evsel__raw_name(struct perf_evsel
*evsel
, char *bf
, size_t size
)
476 int ret
= scnprintf(bf
, size
, "raw 0x%" PRIx64
, evsel
->attr
.config
);
477 return ret
+ perf_evsel__add_modifiers(evsel
, bf
+ ret
, size
- ret
);
480 const char *perf_evsel__name(struct perf_evsel
*evsel
)
487 switch (evsel
->attr
.type
) {
489 perf_evsel__raw_name(evsel
, bf
, sizeof(bf
));
492 case PERF_TYPE_HARDWARE
:
493 perf_evsel__hw_name(evsel
, bf
, sizeof(bf
));
496 case PERF_TYPE_HW_CACHE
:
497 perf_evsel__hw_cache_name(evsel
, bf
, sizeof(bf
));
500 case PERF_TYPE_SOFTWARE
:
501 perf_evsel__sw_name(evsel
, bf
, sizeof(bf
));
504 case PERF_TYPE_TRACEPOINT
:
505 scnprintf(bf
, sizeof(bf
), "%s", "unknown tracepoint");
508 case PERF_TYPE_BREAKPOINT
:
509 perf_evsel__bp_name(evsel
, bf
, sizeof(bf
));
513 scnprintf(bf
, sizeof(bf
), "unknown attr type: %d",
518 evsel
->name
= strdup(bf
);
520 return evsel
->name
?: "unknown";
523 const char *perf_evsel__group_name(struct perf_evsel
*evsel
)
525 return evsel
->group_name
?: "anon group";
528 int perf_evsel__group_desc(struct perf_evsel
*evsel
, char *buf
, size_t size
)
531 struct perf_evsel
*pos
;
532 const char *group_name
= perf_evsel__group_name(evsel
);
534 ret
= scnprintf(buf
, size
, "%s", group_name
);
536 ret
+= scnprintf(buf
+ ret
, size
- ret
, " { %s",
537 perf_evsel__name(evsel
));
539 for_each_group_member(pos
, evsel
)
540 ret
+= scnprintf(buf
+ ret
, size
- ret
, ", %s",
541 perf_evsel__name(pos
));
543 ret
+= scnprintf(buf
+ ret
, size
- ret
, " }");
549 perf_evsel__config_callgraph(struct perf_evsel
*evsel
)
551 bool function
= perf_evsel__is_function_event(evsel
);
552 struct perf_event_attr
*attr
= &evsel
->attr
;
554 perf_evsel__set_sample_bit(evsel
, CALLCHAIN
);
556 if (callchain_param
.record_mode
== CALLCHAIN_DWARF
) {
558 perf_evsel__set_sample_bit(evsel
, REGS_USER
);
559 perf_evsel__set_sample_bit(evsel
, STACK_USER
);
560 attr
->sample_regs_user
= PERF_REGS_MASK
;
561 attr
->sample_stack_user
= callchain_param
.dump_size
;
562 attr
->exclude_callchain_user
= 1;
564 pr_info("Cannot use DWARF unwind for function trace event,"
565 " falling back to framepointers.\n");
570 pr_info("Disabling user space callchains for function trace event.\n");
571 attr
->exclude_callchain_user
= 1;
576 * The enable_on_exec/disabled value strategy:
578 * 1) For any type of traced program:
579 * - all independent events and group leaders are disabled
580 * - all group members are enabled
582 * Group members are ruled by group leaders. They need to
583 * be enabled, because the group scheduling relies on that.
585 * 2) For traced programs executed by perf:
586 * - all independent events and group leaders have
588 * - we don't specifically enable or disable any event during
591 * Independent events and group leaders are initially disabled
592 * and get enabled by exec. Group members are ruled by group
593 * leaders as stated in 1).
595 * 3) For traced programs attached by perf (pid/tid):
596 * - we specifically enable or disable all events during
599 * When attaching events to already running traced we
600 * enable/disable events specifically, as there's no
601 * initial traced exec call.
603 void perf_evsel__config(struct perf_evsel
*evsel
, struct record_opts
*opts
)
605 struct perf_evsel
*leader
= evsel
->leader
;
606 struct perf_event_attr
*attr
= &evsel
->attr
;
607 int track
= evsel
->tracking
;
608 bool per_cpu
= opts
->target
.default_per_cpu
&& !opts
->target
.per_thread
;
610 attr
->sample_id_all
= perf_missing_features
.sample_id_all
? 0 : 1;
611 attr
->inherit
= !opts
->no_inherit
;
613 perf_evsel__set_sample_bit(evsel
, IP
);
614 perf_evsel__set_sample_bit(evsel
, TID
);
616 if (evsel
->sample_read
) {
617 perf_evsel__set_sample_bit(evsel
, READ
);
620 * We need ID even in case of single event, because
621 * PERF_SAMPLE_READ process ID specific data.
623 perf_evsel__set_sample_id(evsel
, false);
626 * Apply group format only if we belong to group
627 * with more than one members.
629 if (leader
->nr_members
> 1) {
630 attr
->read_format
|= PERF_FORMAT_GROUP
;
636 * We default some events to have a default interval. But keep
637 * it a weak assumption overridable by the user.
639 if (!attr
->sample_period
|| (opts
->user_freq
!= UINT_MAX
||
640 opts
->user_interval
!= ULLONG_MAX
)) {
642 perf_evsel__set_sample_bit(evsel
, PERIOD
);
644 attr
->sample_freq
= opts
->freq
;
646 attr
->sample_period
= opts
->default_interval
;
651 * Disable sampling for all group members other
652 * than leader in case leader 'leads' the sampling.
654 if ((leader
!= evsel
) && leader
->sample_read
) {
655 attr
->sample_freq
= 0;
656 attr
->sample_period
= 0;
659 if (opts
->no_samples
)
660 attr
->sample_freq
= 0;
662 if (opts
->inherit_stat
)
663 attr
->inherit_stat
= 1;
665 if (opts
->sample_address
) {
666 perf_evsel__set_sample_bit(evsel
, ADDR
);
667 attr
->mmap_data
= track
;
670 if (callchain_param
.enabled
&& !evsel
->no_aux_samples
)
671 perf_evsel__config_callgraph(evsel
);
673 if (target__has_cpu(&opts
->target
))
674 perf_evsel__set_sample_bit(evsel
, CPU
);
677 perf_evsel__set_sample_bit(evsel
, PERIOD
);
680 * When the user explicitely disabled time don't force it here.
682 if (opts
->sample_time
&&
683 (!perf_missing_features
.sample_id_all
&&
684 (!opts
->no_inherit
|| target__has_cpu(&opts
->target
) || per_cpu
)))
685 perf_evsel__set_sample_bit(evsel
, TIME
);
687 if (opts
->raw_samples
&& !evsel
->no_aux_samples
) {
688 perf_evsel__set_sample_bit(evsel
, TIME
);
689 perf_evsel__set_sample_bit(evsel
, RAW
);
690 perf_evsel__set_sample_bit(evsel
, CPU
);
693 if (opts
->sample_address
)
694 perf_evsel__set_sample_bit(evsel
, DATA_SRC
);
696 if (opts
->no_buffering
) {
698 attr
->wakeup_events
= 1;
700 if (opts
->branch_stack
&& !evsel
->no_aux_samples
) {
701 perf_evsel__set_sample_bit(evsel
, BRANCH_STACK
);
702 attr
->branch_sample_type
= opts
->branch_stack
;
705 if (opts
->sample_weight
)
706 perf_evsel__set_sample_bit(evsel
, WEIGHT
);
709 attr
->mmap2
= track
&& !perf_missing_features
.mmap2
;
712 if (opts
->sample_transaction
)
713 perf_evsel__set_sample_bit(evsel
, TRANSACTION
);
716 * XXX see the function comment above
718 * Disabling only independent events or group leaders,
719 * keeping group members enabled.
721 if (perf_evsel__is_group_leader(evsel
))
725 * Setting enable_on_exec for independent events and
726 * group leaders for traced executed by perf.
728 if (target__none(&opts
->target
) && perf_evsel__is_group_leader(evsel
) &&
729 !opts
->initial_delay
)
730 attr
->enable_on_exec
= 1;
732 if (evsel
->immediate
) {
734 attr
->enable_on_exec
= 0;
738 int perf_evsel__alloc_fd(struct perf_evsel
*evsel
, int ncpus
, int nthreads
)
742 if (evsel
->system_wide
)
745 evsel
->fd
= xyarray__new(ncpus
, nthreads
, sizeof(int));
748 for (cpu
= 0; cpu
< ncpus
; cpu
++) {
749 for (thread
= 0; thread
< nthreads
; thread
++) {
750 FD(evsel
, cpu
, thread
) = -1;
755 return evsel
->fd
!= NULL
? 0 : -ENOMEM
;
758 static int perf_evsel__run_ioctl(struct perf_evsel
*evsel
, int ncpus
, int nthreads
,
763 if (evsel
->system_wide
)
766 for (cpu
= 0; cpu
< ncpus
; cpu
++) {
767 for (thread
= 0; thread
< nthreads
; thread
++) {
768 int fd
= FD(evsel
, cpu
, thread
),
769 err
= ioctl(fd
, ioc
, arg
);
779 int perf_evsel__set_filter(struct perf_evsel
*evsel
, int ncpus
, int nthreads
,
782 return perf_evsel__run_ioctl(evsel
, ncpus
, nthreads
,
783 PERF_EVENT_IOC_SET_FILTER
,
787 int perf_evsel__enable(struct perf_evsel
*evsel
, int ncpus
, int nthreads
)
789 return perf_evsel__run_ioctl(evsel
, ncpus
, nthreads
,
790 PERF_EVENT_IOC_ENABLE
,
794 int perf_evsel__alloc_id(struct perf_evsel
*evsel
, int ncpus
, int nthreads
)
796 if (evsel
->system_wide
)
799 evsel
->sample_id
= xyarray__new(ncpus
, nthreads
, sizeof(struct perf_sample_id
));
800 if (evsel
->sample_id
== NULL
)
803 evsel
->id
= zalloc(ncpus
* nthreads
* sizeof(u64
));
804 if (evsel
->id
== NULL
) {
805 xyarray__delete(evsel
->sample_id
);
806 evsel
->sample_id
= NULL
;
813 void perf_evsel__reset_counts(struct perf_evsel
*evsel
, int ncpus
)
815 memset(evsel
->counts
, 0, (sizeof(*evsel
->counts
) +
816 (ncpus
* sizeof(struct perf_counts_values
))));
819 int perf_evsel__alloc_counts(struct perf_evsel
*evsel
, int ncpus
)
821 evsel
->counts
= zalloc((sizeof(*evsel
->counts
) +
822 (ncpus
* sizeof(struct perf_counts_values
))));
823 return evsel
->counts
!= NULL
? 0 : -ENOMEM
;
826 void perf_evsel__free_fd(struct perf_evsel
*evsel
)
828 xyarray__delete(evsel
->fd
);
832 void perf_evsel__free_id(struct perf_evsel
*evsel
)
834 xyarray__delete(evsel
->sample_id
);
835 evsel
->sample_id
= NULL
;
839 void perf_evsel__close_fd(struct perf_evsel
*evsel
, int ncpus
, int nthreads
)
843 if (evsel
->system_wide
)
846 for (cpu
= 0; cpu
< ncpus
; cpu
++)
847 for (thread
= 0; thread
< nthreads
; ++thread
) {
848 close(FD(evsel
, cpu
, thread
));
849 FD(evsel
, cpu
, thread
) = -1;
853 void perf_evsel__free_counts(struct perf_evsel
*evsel
)
855 zfree(&evsel
->counts
);
858 void perf_evsel__exit(struct perf_evsel
*evsel
)
860 assert(list_empty(&evsel
->node
));
861 perf_evsel__free_fd(evsel
);
862 perf_evsel__free_id(evsel
);
863 perf_evsel__object
.fini(evsel
);
866 void perf_evsel__delete(struct perf_evsel
*evsel
)
868 perf_evsel__exit(evsel
);
869 close_cgroup(evsel
->cgrp
);
870 zfree(&evsel
->group_name
);
871 if (evsel
->tp_format
)
872 pevent_free_format(evsel
->tp_format
);
877 static inline void compute_deltas(struct perf_evsel
*evsel
,
879 struct perf_counts_values
*count
)
881 struct perf_counts_values tmp
;
883 if (!evsel
->prev_raw_counts
)
887 tmp
= evsel
->prev_raw_counts
->aggr
;
888 evsel
->prev_raw_counts
->aggr
= *count
;
890 tmp
= evsel
->prev_raw_counts
->cpu
[cpu
];
891 evsel
->prev_raw_counts
->cpu
[cpu
] = *count
;
894 count
->val
= count
->val
- tmp
.val
;
895 count
->ena
= count
->ena
- tmp
.ena
;
896 count
->run
= count
->run
- tmp
.run
;
899 int __perf_evsel__read_on_cpu(struct perf_evsel
*evsel
,
900 int cpu
, int thread
, bool scale
)
902 struct perf_counts_values count
;
903 size_t nv
= scale
? 3 : 1;
905 if (FD(evsel
, cpu
, thread
) < 0)
908 if (evsel
->counts
== NULL
&& perf_evsel__alloc_counts(evsel
, cpu
+ 1) < 0)
911 if (readn(FD(evsel
, cpu
, thread
), &count
, nv
* sizeof(u64
)) < 0)
914 compute_deltas(evsel
, cpu
, &count
);
919 else if (count
.run
< count
.ena
)
920 count
.val
= (u64
)((double)count
.val
* count
.ena
/ count
.run
+ 0.5);
922 count
.ena
= count
.run
= 0;
924 evsel
->counts
->cpu
[cpu
] = count
;
928 int __perf_evsel__read(struct perf_evsel
*evsel
,
929 int ncpus
, int nthreads
, bool scale
)
931 size_t nv
= scale
? 3 : 1;
933 struct perf_counts_values
*aggr
= &evsel
->counts
->aggr
, count
;
935 if (evsel
->system_wide
)
938 aggr
->val
= aggr
->ena
= aggr
->run
= 0;
940 for (cpu
= 0; cpu
< ncpus
; cpu
++) {
941 for (thread
= 0; thread
< nthreads
; thread
++) {
942 if (FD(evsel
, cpu
, thread
) < 0)
945 if (readn(FD(evsel
, cpu
, thread
),
946 &count
, nv
* sizeof(u64
)) < 0)
949 aggr
->val
+= count
.val
;
951 aggr
->ena
+= count
.ena
;
952 aggr
->run
+= count
.run
;
957 compute_deltas(evsel
, -1, aggr
);
959 evsel
->counts
->scaled
= 0;
961 if (aggr
->run
== 0) {
962 evsel
->counts
->scaled
= -1;
967 if (aggr
->run
< aggr
->ena
) {
968 evsel
->counts
->scaled
= 1;
969 aggr
->val
= (u64
)((double)aggr
->val
* aggr
->ena
/ aggr
->run
+ 0.5);
972 aggr
->ena
= aggr
->run
= 0;
977 static int get_group_fd(struct perf_evsel
*evsel
, int cpu
, int thread
)
979 struct perf_evsel
*leader
= evsel
->leader
;
982 if (perf_evsel__is_group_leader(evsel
))
986 * Leader must be already processed/open,
991 fd
= FD(leader
, cpu
, thread
);
997 #define __PRINT_ATTR(fmt, cast, field) \
998 fprintf(fp, " %-19s "fmt"\n", #field, cast attr->field)
1000 #define PRINT_ATTR_U32(field) __PRINT_ATTR("%u" , , field)
1001 #define PRINT_ATTR_X32(field) __PRINT_ATTR("%#x", , field)
1002 #define PRINT_ATTR_U64(field) __PRINT_ATTR("%" PRIu64, (uint64_t), field)
1003 #define PRINT_ATTR_X64(field) __PRINT_ATTR("%#"PRIx64, (uint64_t), field)
1005 #define PRINT_ATTR2N(name1, field1, name2, field2) \
1006 fprintf(fp, " %-19s %u %-19s %u\n", \
1007 name1, attr->field1, name2, attr->field2)
1009 #define PRINT_ATTR2(field1, field2) \
1010 PRINT_ATTR2N(#field1, field1, #field2, field2)
1012 static size_t perf_event_attr__fprintf(struct perf_event_attr
*attr
, FILE *fp
)
1016 ret
+= fprintf(fp
, "%.60s\n", graph_dotted_line
);
1017 ret
+= fprintf(fp
, "perf_event_attr:\n");
1019 ret
+= PRINT_ATTR_U32(type
);
1020 ret
+= PRINT_ATTR_U32(size
);
1021 ret
+= PRINT_ATTR_X64(config
);
1022 ret
+= PRINT_ATTR_U64(sample_period
);
1023 ret
+= PRINT_ATTR_U64(sample_freq
);
1024 ret
+= PRINT_ATTR_X64(sample_type
);
1025 ret
+= PRINT_ATTR_X64(read_format
);
1027 ret
+= PRINT_ATTR2(disabled
, inherit
);
1028 ret
+= PRINT_ATTR2(pinned
, exclusive
);
1029 ret
+= PRINT_ATTR2(exclude_user
, exclude_kernel
);
1030 ret
+= PRINT_ATTR2(exclude_hv
, exclude_idle
);
1031 ret
+= PRINT_ATTR2(mmap
, comm
);
1032 ret
+= PRINT_ATTR2(mmap2
, comm_exec
);
1033 ret
+= PRINT_ATTR2(freq
, inherit_stat
);
1034 ret
+= PRINT_ATTR2(enable_on_exec
, task
);
1035 ret
+= PRINT_ATTR2(watermark
, precise_ip
);
1036 ret
+= PRINT_ATTR2(mmap_data
, sample_id_all
);
1037 ret
+= PRINT_ATTR2(exclude_host
, exclude_guest
);
1038 ret
+= PRINT_ATTR2N("excl.callchain_kern", exclude_callchain_kernel
,
1039 "excl.callchain_user", exclude_callchain_user
);
1041 ret
+= PRINT_ATTR_U32(wakeup_events
);
1042 ret
+= PRINT_ATTR_U32(wakeup_watermark
);
1043 ret
+= PRINT_ATTR_X32(bp_type
);
1044 ret
+= PRINT_ATTR_X64(bp_addr
);
1045 ret
+= PRINT_ATTR_X64(config1
);
1046 ret
+= PRINT_ATTR_U64(bp_len
);
1047 ret
+= PRINT_ATTR_X64(config2
);
1048 ret
+= PRINT_ATTR_X64(branch_sample_type
);
1049 ret
+= PRINT_ATTR_X64(sample_regs_user
);
1050 ret
+= PRINT_ATTR_U32(sample_stack_user
);
1052 ret
+= fprintf(fp
, "%.60s\n", graph_dotted_line
);
1057 static int __perf_evsel__open(struct perf_evsel
*evsel
, struct cpu_map
*cpus
,
1058 struct thread_map
*threads
)
1060 int cpu
, thread
, nthreads
;
1061 unsigned long flags
= PERF_FLAG_FD_CLOEXEC
;
1063 enum { NO_CHANGE
, SET_TO_MAX
, INCREASED_MAX
} set_rlimit
= NO_CHANGE
;
1065 if (evsel
->system_wide
)
1068 nthreads
= threads
->nr
;
1070 if (evsel
->fd
== NULL
&&
1071 perf_evsel__alloc_fd(evsel
, cpus
->nr
, nthreads
) < 0)
1075 flags
|= PERF_FLAG_PID_CGROUP
;
1076 pid
= evsel
->cgrp
->fd
;
1079 fallback_missing_features
:
1080 if (perf_missing_features
.cloexec
)
1081 flags
&= ~(unsigned long)PERF_FLAG_FD_CLOEXEC
;
1082 if (perf_missing_features
.mmap2
)
1083 evsel
->attr
.mmap2
= 0;
1084 if (perf_missing_features
.exclude_guest
)
1085 evsel
->attr
.exclude_guest
= evsel
->attr
.exclude_host
= 0;
1087 if (perf_missing_features
.sample_id_all
)
1088 evsel
->attr
.sample_id_all
= 0;
1091 perf_event_attr__fprintf(&evsel
->attr
, stderr
);
1093 for (cpu
= 0; cpu
< cpus
->nr
; cpu
++) {
1095 for (thread
= 0; thread
< nthreads
; thread
++) {
1098 if (!evsel
->cgrp
&& !evsel
->system_wide
)
1099 pid
= threads
->map
[thread
];
1101 group_fd
= get_group_fd(evsel
, cpu
, thread
);
1103 pr_debug2("sys_perf_event_open: pid %d cpu %d group_fd %d flags %#lx\n",
1104 pid
, cpus
->map
[cpu
], group_fd
, flags
);
1106 FD(evsel
, cpu
, thread
) = sys_perf_event_open(&evsel
->attr
,
1110 if (FD(evsel
, cpu
, thread
) < 0) {
1112 pr_debug2("sys_perf_event_open failed, error %d\n",
1116 set_rlimit
= NO_CHANGE
;
1124 * perf stat needs between 5 and 22 fds per CPU. When we run out
1125 * of them try to increase the limits.
1127 if (err
== -EMFILE
&& set_rlimit
< INCREASED_MAX
) {
1129 int old_errno
= errno
;
1131 if (getrlimit(RLIMIT_NOFILE
, &l
) == 0) {
1132 if (set_rlimit
== NO_CHANGE
)
1133 l
.rlim_cur
= l
.rlim_max
;
1135 l
.rlim_cur
= l
.rlim_max
+ 1000;
1136 l
.rlim_max
= l
.rlim_cur
;
1138 if (setrlimit(RLIMIT_NOFILE
, &l
) == 0) {
1147 if (err
!= -EINVAL
|| cpu
> 0 || thread
> 0)
1150 if (!perf_missing_features
.cloexec
&& (flags
& PERF_FLAG_FD_CLOEXEC
)) {
1151 perf_missing_features
.cloexec
= true;
1152 goto fallback_missing_features
;
1153 } else if (!perf_missing_features
.mmap2
&& evsel
->attr
.mmap2
) {
1154 perf_missing_features
.mmap2
= true;
1155 goto fallback_missing_features
;
1156 } else if (!perf_missing_features
.exclude_guest
&&
1157 (evsel
->attr
.exclude_guest
|| evsel
->attr
.exclude_host
)) {
1158 perf_missing_features
.exclude_guest
= true;
1159 goto fallback_missing_features
;
1160 } else if (!perf_missing_features
.sample_id_all
) {
1161 perf_missing_features
.sample_id_all
= true;
1162 goto retry_sample_id
;
1167 while (--thread
>= 0) {
1168 close(FD(evsel
, cpu
, thread
));
1169 FD(evsel
, cpu
, thread
) = -1;
1172 } while (--cpu
>= 0);
1176 void perf_evsel__close(struct perf_evsel
*evsel
, int ncpus
, int nthreads
)
1178 if (evsel
->fd
== NULL
)
1181 perf_evsel__close_fd(evsel
, ncpus
, nthreads
);
1182 perf_evsel__free_fd(evsel
);
1194 struct thread_map map
;
1196 } empty_thread_map
= {
1201 int perf_evsel__open(struct perf_evsel
*evsel
, struct cpu_map
*cpus
,
1202 struct thread_map
*threads
)
1205 /* Work around old compiler warnings about strict aliasing */
1206 cpus
= &empty_cpu_map
.map
;
1209 if (threads
== NULL
)
1210 threads
= &empty_thread_map
.map
;
1212 return __perf_evsel__open(evsel
, cpus
, threads
);
1215 int perf_evsel__open_per_cpu(struct perf_evsel
*evsel
,
1216 struct cpu_map
*cpus
)
1218 return __perf_evsel__open(evsel
, cpus
, &empty_thread_map
.map
);
1221 int perf_evsel__open_per_thread(struct perf_evsel
*evsel
,
1222 struct thread_map
*threads
)
1224 return __perf_evsel__open(evsel
, &empty_cpu_map
.map
, threads
);
1227 static int perf_evsel__parse_id_sample(const struct perf_evsel
*evsel
,
1228 const union perf_event
*event
,
1229 struct perf_sample
*sample
)
1231 u64 type
= evsel
->attr
.sample_type
;
1232 const u64
*array
= event
->sample
.array
;
1233 bool swapped
= evsel
->needs_swap
;
1236 array
+= ((event
->header
.size
-
1237 sizeof(event
->header
)) / sizeof(u64
)) - 1;
1239 if (type
& PERF_SAMPLE_IDENTIFIER
) {
1240 sample
->id
= *array
;
1244 if (type
& PERF_SAMPLE_CPU
) {
1247 /* undo swap of u64, then swap on individual u32s */
1248 u
.val64
= bswap_64(u
.val64
);
1249 u
.val32
[0] = bswap_32(u
.val32
[0]);
1252 sample
->cpu
= u
.val32
[0];
1256 if (type
& PERF_SAMPLE_STREAM_ID
) {
1257 sample
->stream_id
= *array
;
1261 if (type
& PERF_SAMPLE_ID
) {
1262 sample
->id
= *array
;
1266 if (type
& PERF_SAMPLE_TIME
) {
1267 sample
->time
= *array
;
1271 if (type
& PERF_SAMPLE_TID
) {
1274 /* undo swap of u64, then swap on individual u32s */
1275 u
.val64
= bswap_64(u
.val64
);
1276 u
.val32
[0] = bswap_32(u
.val32
[0]);
1277 u
.val32
[1] = bswap_32(u
.val32
[1]);
1280 sample
->pid
= u
.val32
[0];
1281 sample
->tid
= u
.val32
[1];
1288 static inline bool overflow(const void *endp
, u16 max_size
, const void *offset
,
1291 return size
> max_size
|| offset
+ size
> endp
;
1294 #define OVERFLOW_CHECK(offset, size, max_size) \
1296 if (overflow(endp, (max_size), (offset), (size))) \
1300 #define OVERFLOW_CHECK_u64(offset) \
1301 OVERFLOW_CHECK(offset, sizeof(u64), sizeof(u64))
1303 int perf_evsel__parse_sample(struct perf_evsel
*evsel
, union perf_event
*event
,
1304 struct perf_sample
*data
)
1306 u64 type
= evsel
->attr
.sample_type
;
1307 bool swapped
= evsel
->needs_swap
;
1309 u16 max_size
= event
->header
.size
;
1310 const void *endp
= (void *)event
+ max_size
;
1314 * used for cross-endian analysis. See git commit 65014ab3
1315 * for why this goofiness is needed.
1319 memset(data
, 0, sizeof(*data
));
1320 data
->cpu
= data
->pid
= data
->tid
= -1;
1321 data
->stream_id
= data
->id
= data
->time
= -1ULL;
1322 data
->period
= evsel
->attr
.sample_period
;
1325 if (event
->header
.type
!= PERF_RECORD_SAMPLE
) {
1326 if (!evsel
->attr
.sample_id_all
)
1328 return perf_evsel__parse_id_sample(evsel
, event
, data
);
1331 array
= event
->sample
.array
;
1334 * The evsel's sample_size is based on PERF_SAMPLE_MASK which includes
1335 * up to PERF_SAMPLE_PERIOD. After that overflow() must be used to
1336 * check the format does not go past the end of the event.
1338 if (evsel
->sample_size
+ sizeof(event
->header
) > event
->header
.size
)
1342 if (type
& PERF_SAMPLE_IDENTIFIER
) {
1347 if (type
& PERF_SAMPLE_IP
) {
1352 if (type
& PERF_SAMPLE_TID
) {
1355 /* undo swap of u64, then swap on individual u32s */
1356 u
.val64
= bswap_64(u
.val64
);
1357 u
.val32
[0] = bswap_32(u
.val32
[0]);
1358 u
.val32
[1] = bswap_32(u
.val32
[1]);
1361 data
->pid
= u
.val32
[0];
1362 data
->tid
= u
.val32
[1];
1366 if (type
& PERF_SAMPLE_TIME
) {
1367 data
->time
= *array
;
1372 if (type
& PERF_SAMPLE_ADDR
) {
1373 data
->addr
= *array
;
1377 if (type
& PERF_SAMPLE_ID
) {
1382 if (type
& PERF_SAMPLE_STREAM_ID
) {
1383 data
->stream_id
= *array
;
1387 if (type
& PERF_SAMPLE_CPU
) {
1391 /* undo swap of u64, then swap on individual u32s */
1392 u
.val64
= bswap_64(u
.val64
);
1393 u
.val32
[0] = bswap_32(u
.val32
[0]);
1396 data
->cpu
= u
.val32
[0];
1400 if (type
& PERF_SAMPLE_PERIOD
) {
1401 data
->period
= *array
;
1405 if (type
& PERF_SAMPLE_READ
) {
1406 u64 read_format
= evsel
->attr
.read_format
;
1408 OVERFLOW_CHECK_u64(array
);
1409 if (read_format
& PERF_FORMAT_GROUP
)
1410 data
->read
.group
.nr
= *array
;
1412 data
->read
.one
.value
= *array
;
1416 if (read_format
& PERF_FORMAT_TOTAL_TIME_ENABLED
) {
1417 OVERFLOW_CHECK_u64(array
);
1418 data
->read
.time_enabled
= *array
;
1422 if (read_format
& PERF_FORMAT_TOTAL_TIME_RUNNING
) {
1423 OVERFLOW_CHECK_u64(array
);
1424 data
->read
.time_running
= *array
;
1428 /* PERF_FORMAT_ID is forced for PERF_SAMPLE_READ */
1429 if (read_format
& PERF_FORMAT_GROUP
) {
1430 const u64 max_group_nr
= UINT64_MAX
/
1431 sizeof(struct sample_read_value
);
1433 if (data
->read
.group
.nr
> max_group_nr
)
1435 sz
= data
->read
.group
.nr
*
1436 sizeof(struct sample_read_value
);
1437 OVERFLOW_CHECK(array
, sz
, max_size
);
1438 data
->read
.group
.values
=
1439 (struct sample_read_value
*)array
;
1440 array
= (void *)array
+ sz
;
1442 OVERFLOW_CHECK_u64(array
);
1443 data
->read
.one
.id
= *array
;
1448 if (type
& PERF_SAMPLE_CALLCHAIN
) {
1449 const u64 max_callchain_nr
= UINT64_MAX
/ sizeof(u64
);
1451 OVERFLOW_CHECK_u64(array
);
1452 data
->callchain
= (struct ip_callchain
*)array
++;
1453 if (data
->callchain
->nr
> max_callchain_nr
)
1455 sz
= data
->callchain
->nr
* sizeof(u64
);
1456 OVERFLOW_CHECK(array
, sz
, max_size
);
1457 array
= (void *)array
+ sz
;
1460 if (type
& PERF_SAMPLE_RAW
) {
1461 OVERFLOW_CHECK_u64(array
);
1463 if (WARN_ONCE(swapped
,
1464 "Endianness of raw data not corrected!\n")) {
1465 /* undo swap of u64, then swap on individual u32s */
1466 u
.val64
= bswap_64(u
.val64
);
1467 u
.val32
[0] = bswap_32(u
.val32
[0]);
1468 u
.val32
[1] = bswap_32(u
.val32
[1]);
1470 data
->raw_size
= u
.val32
[0];
1471 array
= (void *)array
+ sizeof(u32
);
1473 OVERFLOW_CHECK(array
, data
->raw_size
, max_size
);
1474 data
->raw_data
= (void *)array
;
1475 array
= (void *)array
+ data
->raw_size
;
1478 if (type
& PERF_SAMPLE_BRANCH_STACK
) {
1479 const u64 max_branch_nr
= UINT64_MAX
/
1480 sizeof(struct branch_entry
);
1482 OVERFLOW_CHECK_u64(array
);
1483 data
->branch_stack
= (struct branch_stack
*)array
++;
1485 if (data
->branch_stack
->nr
> max_branch_nr
)
1487 sz
= data
->branch_stack
->nr
* sizeof(struct branch_entry
);
1488 OVERFLOW_CHECK(array
, sz
, max_size
);
1489 array
= (void *)array
+ sz
;
1492 if (type
& PERF_SAMPLE_REGS_USER
) {
1493 OVERFLOW_CHECK_u64(array
);
1494 data
->user_regs
.abi
= *array
;
1497 if (data
->user_regs
.abi
) {
1498 u64 mask
= evsel
->attr
.sample_regs_user
;
1500 sz
= hweight_long(mask
) * sizeof(u64
);
1501 OVERFLOW_CHECK(array
, sz
, max_size
);
1502 data
->user_regs
.mask
= mask
;
1503 data
->user_regs
.regs
= (u64
*)array
;
1504 array
= (void *)array
+ sz
;
1508 if (type
& PERF_SAMPLE_STACK_USER
) {
1509 OVERFLOW_CHECK_u64(array
);
1512 data
->user_stack
.offset
= ((char *)(array
- 1)
1516 data
->user_stack
.size
= 0;
1518 OVERFLOW_CHECK(array
, sz
, max_size
);
1519 data
->user_stack
.data
= (char *)array
;
1520 array
= (void *)array
+ sz
;
1521 OVERFLOW_CHECK_u64(array
);
1522 data
->user_stack
.size
= *array
++;
1523 if (WARN_ONCE(data
->user_stack
.size
> sz
,
1524 "user stack dump failure\n"))
1530 if (type
& PERF_SAMPLE_WEIGHT
) {
1531 OVERFLOW_CHECK_u64(array
);
1532 data
->weight
= *array
;
1536 data
->data_src
= PERF_MEM_DATA_SRC_NONE
;
1537 if (type
& PERF_SAMPLE_DATA_SRC
) {
1538 OVERFLOW_CHECK_u64(array
);
1539 data
->data_src
= *array
;
1543 data
->transaction
= 0;
1544 if (type
& PERF_SAMPLE_TRANSACTION
) {
1545 OVERFLOW_CHECK_u64(array
);
1546 data
->transaction
= *array
;
1553 size_t perf_event__sample_event_size(const struct perf_sample
*sample
, u64 type
,
1556 size_t sz
, result
= sizeof(struct sample_event
);
1558 if (type
& PERF_SAMPLE_IDENTIFIER
)
1559 result
+= sizeof(u64
);
1561 if (type
& PERF_SAMPLE_IP
)
1562 result
+= sizeof(u64
);
1564 if (type
& PERF_SAMPLE_TID
)
1565 result
+= sizeof(u64
);
1567 if (type
& PERF_SAMPLE_TIME
)
1568 result
+= sizeof(u64
);
1570 if (type
& PERF_SAMPLE_ADDR
)
1571 result
+= sizeof(u64
);
1573 if (type
& PERF_SAMPLE_ID
)
1574 result
+= sizeof(u64
);
1576 if (type
& PERF_SAMPLE_STREAM_ID
)
1577 result
+= sizeof(u64
);
1579 if (type
& PERF_SAMPLE_CPU
)
1580 result
+= sizeof(u64
);
1582 if (type
& PERF_SAMPLE_PERIOD
)
1583 result
+= sizeof(u64
);
1585 if (type
& PERF_SAMPLE_READ
) {
1586 result
+= sizeof(u64
);
1587 if (read_format
& PERF_FORMAT_TOTAL_TIME_ENABLED
)
1588 result
+= sizeof(u64
);
1589 if (read_format
& PERF_FORMAT_TOTAL_TIME_RUNNING
)
1590 result
+= sizeof(u64
);
1591 /* PERF_FORMAT_ID is forced for PERF_SAMPLE_READ */
1592 if (read_format
& PERF_FORMAT_GROUP
) {
1593 sz
= sample
->read
.group
.nr
*
1594 sizeof(struct sample_read_value
);
1597 result
+= sizeof(u64
);
1601 if (type
& PERF_SAMPLE_CALLCHAIN
) {
1602 sz
= (sample
->callchain
->nr
+ 1) * sizeof(u64
);
1606 if (type
& PERF_SAMPLE_RAW
) {
1607 result
+= sizeof(u32
);
1608 result
+= sample
->raw_size
;
1611 if (type
& PERF_SAMPLE_BRANCH_STACK
) {
1612 sz
= sample
->branch_stack
->nr
* sizeof(struct branch_entry
);
1617 if (type
& PERF_SAMPLE_REGS_USER
) {
1618 if (sample
->user_regs
.abi
) {
1619 result
+= sizeof(u64
);
1620 sz
= hweight_long(sample
->user_regs
.mask
) * sizeof(u64
);
1623 result
+= sizeof(u64
);
1627 if (type
& PERF_SAMPLE_STACK_USER
) {
1628 sz
= sample
->user_stack
.size
;
1629 result
+= sizeof(u64
);
1632 result
+= sizeof(u64
);
1636 if (type
& PERF_SAMPLE_WEIGHT
)
1637 result
+= sizeof(u64
);
1639 if (type
& PERF_SAMPLE_DATA_SRC
)
1640 result
+= sizeof(u64
);
1642 if (type
& PERF_SAMPLE_TRANSACTION
)
1643 result
+= sizeof(u64
);
1648 int perf_event__synthesize_sample(union perf_event
*event
, u64 type
,
1650 const struct perf_sample
*sample
,
1656 * used for cross-endian analysis. See git commit 65014ab3
1657 * for why this goofiness is needed.
1661 array
= event
->sample
.array
;
1663 if (type
& PERF_SAMPLE_IDENTIFIER
) {
1664 *array
= sample
->id
;
1668 if (type
& PERF_SAMPLE_IP
) {
1669 *array
= sample
->ip
;
1673 if (type
& PERF_SAMPLE_TID
) {
1674 u
.val32
[0] = sample
->pid
;
1675 u
.val32
[1] = sample
->tid
;
1678 * Inverse of what is done in perf_evsel__parse_sample
1680 u
.val32
[0] = bswap_32(u
.val32
[0]);
1681 u
.val32
[1] = bswap_32(u
.val32
[1]);
1682 u
.val64
= bswap_64(u
.val64
);
1689 if (type
& PERF_SAMPLE_TIME
) {
1690 *array
= sample
->time
;
1694 if (type
& PERF_SAMPLE_ADDR
) {
1695 *array
= sample
->addr
;
1699 if (type
& PERF_SAMPLE_ID
) {
1700 *array
= sample
->id
;
1704 if (type
& PERF_SAMPLE_STREAM_ID
) {
1705 *array
= sample
->stream_id
;
1709 if (type
& PERF_SAMPLE_CPU
) {
1710 u
.val32
[0] = sample
->cpu
;
1713 * Inverse of what is done in perf_evsel__parse_sample
1715 u
.val32
[0] = bswap_32(u
.val32
[0]);
1716 u
.val64
= bswap_64(u
.val64
);
1722 if (type
& PERF_SAMPLE_PERIOD
) {
1723 *array
= sample
->period
;
1727 if (type
& PERF_SAMPLE_READ
) {
1728 if (read_format
& PERF_FORMAT_GROUP
)
1729 *array
= sample
->read
.group
.nr
;
1731 *array
= sample
->read
.one
.value
;
1734 if (read_format
& PERF_FORMAT_TOTAL_TIME_ENABLED
) {
1735 *array
= sample
->read
.time_enabled
;
1739 if (read_format
& PERF_FORMAT_TOTAL_TIME_RUNNING
) {
1740 *array
= sample
->read
.time_running
;
1744 /* PERF_FORMAT_ID is forced for PERF_SAMPLE_READ */
1745 if (read_format
& PERF_FORMAT_GROUP
) {
1746 sz
= sample
->read
.group
.nr
*
1747 sizeof(struct sample_read_value
);
1748 memcpy(array
, sample
->read
.group
.values
, sz
);
1749 array
= (void *)array
+ sz
;
1751 *array
= sample
->read
.one
.id
;
1756 if (type
& PERF_SAMPLE_CALLCHAIN
) {
1757 sz
= (sample
->callchain
->nr
+ 1) * sizeof(u64
);
1758 memcpy(array
, sample
->callchain
, sz
);
1759 array
= (void *)array
+ sz
;
1762 if (type
& PERF_SAMPLE_RAW
) {
1763 u
.val32
[0] = sample
->raw_size
;
1764 if (WARN_ONCE(swapped
,
1765 "Endianness of raw data not corrected!\n")) {
1767 * Inverse of what is done in perf_evsel__parse_sample
1769 u
.val32
[0] = bswap_32(u
.val32
[0]);
1770 u
.val32
[1] = bswap_32(u
.val32
[1]);
1771 u
.val64
= bswap_64(u
.val64
);
1774 array
= (void *)array
+ sizeof(u32
);
1776 memcpy(array
, sample
->raw_data
, sample
->raw_size
);
1777 array
= (void *)array
+ sample
->raw_size
;
1780 if (type
& PERF_SAMPLE_BRANCH_STACK
) {
1781 sz
= sample
->branch_stack
->nr
* sizeof(struct branch_entry
);
1783 memcpy(array
, sample
->branch_stack
, sz
);
1784 array
= (void *)array
+ sz
;
1787 if (type
& PERF_SAMPLE_REGS_USER
) {
1788 if (sample
->user_regs
.abi
) {
1789 *array
++ = sample
->user_regs
.abi
;
1790 sz
= hweight_long(sample
->user_regs
.mask
) * sizeof(u64
);
1791 memcpy(array
, sample
->user_regs
.regs
, sz
);
1792 array
= (void *)array
+ sz
;
1798 if (type
& PERF_SAMPLE_STACK_USER
) {
1799 sz
= sample
->user_stack
.size
;
1802 memcpy(array
, sample
->user_stack
.data
, sz
);
1803 array
= (void *)array
+ sz
;
1808 if (type
& PERF_SAMPLE_WEIGHT
) {
1809 *array
= sample
->weight
;
1813 if (type
& PERF_SAMPLE_DATA_SRC
) {
1814 *array
= sample
->data_src
;
1818 if (type
& PERF_SAMPLE_TRANSACTION
) {
1819 *array
= sample
->transaction
;
1826 struct format_field
*perf_evsel__field(struct perf_evsel
*evsel
, const char *name
)
1828 return pevent_find_field(evsel
->tp_format
, name
);
1831 void *perf_evsel__rawptr(struct perf_evsel
*evsel
, struct perf_sample
*sample
,
1834 struct format_field
*field
= perf_evsel__field(evsel
, name
);
1840 offset
= field
->offset
;
1842 if (field
->flags
& FIELD_IS_DYNAMIC
) {
1843 offset
= *(int *)(sample
->raw_data
+ field
->offset
);
1847 return sample
->raw_data
+ offset
;
1850 u64
perf_evsel__intval(struct perf_evsel
*evsel
, struct perf_sample
*sample
,
1853 struct format_field
*field
= perf_evsel__field(evsel
, name
);
1860 ptr
= sample
->raw_data
+ field
->offset
;
1862 switch (field
->size
) {
1866 value
= *(u16
*)ptr
;
1869 value
= *(u32
*)ptr
;
1872 value
= *(u64
*)ptr
;
1878 if (!evsel
->needs_swap
)
1881 switch (field
->size
) {
1883 return bswap_16(value
);
1885 return bswap_32(value
);
1887 return bswap_64(value
);
1895 static int comma_fprintf(FILE *fp
, bool *first
, const char *fmt
, ...)
1901 ret
+= fprintf(fp
, ",");
1903 ret
+= fprintf(fp
, ":");
1907 va_start(args
, fmt
);
1908 ret
+= vfprintf(fp
, fmt
, args
);
1913 static int __if_fprintf(FILE *fp
, bool *first
, const char *field
, u64 value
)
1918 return comma_fprintf(fp
, first
, " %s: %" PRIu64
, field
, value
);
1921 #define if_print(field) printed += __if_fprintf(fp, &first, #field, evsel->attr.field)
1928 static int bits__fprintf(FILE *fp
, const char *field
, u64 value
,
1929 struct bit_names
*bits
, bool *first
)
1931 int i
= 0, printed
= comma_fprintf(fp
, first
, " %s: ", field
);
1932 bool first_bit
= true;
1935 if (value
& bits
[i
].bit
) {
1936 printed
+= fprintf(fp
, "%s%s", first_bit
? "" : "|", bits
[i
].name
);
1939 } while (bits
[++i
].name
!= NULL
);
1944 static int sample_type__fprintf(FILE *fp
, bool *first
, u64 value
)
1946 #define bit_name(n) { PERF_SAMPLE_##n, #n }
1947 struct bit_names bits
[] = {
1948 bit_name(IP
), bit_name(TID
), bit_name(TIME
), bit_name(ADDR
),
1949 bit_name(READ
), bit_name(CALLCHAIN
), bit_name(ID
), bit_name(CPU
),
1950 bit_name(PERIOD
), bit_name(STREAM_ID
), bit_name(RAW
),
1951 bit_name(BRANCH_STACK
), bit_name(REGS_USER
), bit_name(STACK_USER
),
1952 bit_name(IDENTIFIER
),
1956 return bits__fprintf(fp
, "sample_type", value
, bits
, first
);
1959 static int read_format__fprintf(FILE *fp
, bool *first
, u64 value
)
1961 #define bit_name(n) { PERF_FORMAT_##n, #n }
1962 struct bit_names bits
[] = {
1963 bit_name(TOTAL_TIME_ENABLED
), bit_name(TOTAL_TIME_RUNNING
),
1964 bit_name(ID
), bit_name(GROUP
),
1968 return bits__fprintf(fp
, "read_format", value
, bits
, first
);
1971 int perf_evsel__fprintf(struct perf_evsel
*evsel
,
1972 struct perf_attr_details
*details
, FILE *fp
)
1977 if (details
->event_group
) {
1978 struct perf_evsel
*pos
;
1980 if (!perf_evsel__is_group_leader(evsel
))
1983 if (evsel
->nr_members
> 1)
1984 printed
+= fprintf(fp
, "%s{", evsel
->group_name
?: "");
1986 printed
+= fprintf(fp
, "%s", perf_evsel__name(evsel
));
1987 for_each_group_member(pos
, evsel
)
1988 printed
+= fprintf(fp
, ",%s", perf_evsel__name(pos
));
1990 if (evsel
->nr_members
> 1)
1991 printed
+= fprintf(fp
, "}");
1995 printed
+= fprintf(fp
, "%s", perf_evsel__name(evsel
));
1997 if (details
->verbose
|| details
->freq
) {
1998 printed
+= comma_fprintf(fp
, &first
, " sample_freq=%" PRIu64
,
1999 (u64
)evsel
->attr
.sample_freq
);
2002 if (details
->verbose
) {
2008 printed
+= sample_type__fprintf(fp
, &first
, evsel
->attr
.sample_type
);
2009 if (evsel
->attr
.read_format
)
2010 printed
+= read_format__fprintf(fp
, &first
, evsel
->attr
.read_format
);
2014 if_print(exclusive
);
2015 if_print(exclude_user
);
2016 if_print(exclude_kernel
);
2017 if_print(exclude_hv
);
2018 if_print(exclude_idle
);
2022 if_print(comm_exec
);
2024 if_print(inherit_stat
);
2025 if_print(enable_on_exec
);
2027 if_print(watermark
);
2028 if_print(precise_ip
);
2029 if_print(mmap_data
);
2030 if_print(sample_id_all
);
2031 if_print(exclude_host
);
2032 if_print(exclude_guest
);
2033 if_print(__reserved_1
);
2034 if_print(wakeup_events
);
2036 if_print(branch_sample_type
);
2043 bool perf_evsel__fallback(struct perf_evsel
*evsel
, int err
,
2044 char *msg
, size_t msgsize
)
2046 if ((err
== ENOENT
|| err
== ENXIO
|| err
== ENODEV
) &&
2047 evsel
->attr
.type
== PERF_TYPE_HARDWARE
&&
2048 evsel
->attr
.config
== PERF_COUNT_HW_CPU_CYCLES
) {
2050 * If it's cycles then fall back to hrtimer based
2051 * cpu-clock-tick sw counter, which is always available even if
2054 * PPC returns ENXIO until 2.6.37 (behavior changed with commit
2057 scnprintf(msg
, msgsize
, "%s",
2058 "The cycles event is not supported, trying to fall back to cpu-clock-ticks");
2060 evsel
->attr
.type
= PERF_TYPE_SOFTWARE
;
2061 evsel
->attr
.config
= PERF_COUNT_SW_CPU_CLOCK
;
2063 zfree(&evsel
->name
);
2070 int perf_evsel__open_strerror(struct perf_evsel
*evsel
, struct target
*target
,
2071 int err
, char *msg
, size_t size
)
2073 char sbuf
[STRERR_BUFSIZE
];
2078 return scnprintf(msg
, size
,
2079 "You may not have permission to collect %sstats.\n"
2080 "Consider tweaking /proc/sys/kernel/perf_event_paranoid:\n"
2081 " -1 - Not paranoid at all\n"
2082 " 0 - Disallow raw tracepoint access for unpriv\n"
2083 " 1 - Disallow cpu events for unpriv\n"
2084 " 2 - Disallow kernel profiling for unpriv",
2085 target
->system_wide
? "system-wide " : "");
2087 return scnprintf(msg
, size
, "The %s event is not supported.",
2088 perf_evsel__name(evsel
));
2090 return scnprintf(msg
, size
, "%s",
2091 "Too many events are opened.\n"
2092 "Try again after reducing the number of events.");
2094 if (target
->cpu_list
)
2095 return scnprintf(msg
, size
, "%s",
2096 "No such device - did you specify an out-of-range profile CPU?\n");
2099 if (evsel
->attr
.precise_ip
)
2100 return scnprintf(msg
, size
, "%s",
2101 "\'precise\' request may not be supported. Try removing 'p' modifier.");
2102 #if defined(__i386__) || defined(__x86_64__)
2103 if (evsel
->attr
.type
== PERF_TYPE_HARDWARE
)
2104 return scnprintf(msg
, size
, "%s",
2105 "No hardware sampling interrupt available.\n"
2106 "No APIC? If so then you can boot the kernel with the \"lapic\" boot parameter to force-enable it.");
2110 if (find_process("oprofiled"))
2111 return scnprintf(msg
, size
,
2112 "The PMU counters are busy/taken by another profiler.\n"
2113 "We found oprofile daemon running, please stop it and try again.");
2119 return scnprintf(msg
, size
,
2120 "The sys_perf_event_open() syscall returned with %d (%s) for event (%s).\n"
2121 "/bin/dmesg may provide additional information.\n"
2122 "No CONFIG_PERF_EVENTS=y kernel support configured?\n",
2123 err
, strerror_r(err
, sbuf
, sizeof(sbuf
)),
2124 perf_evsel__name(evsel
));