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>
14 #include "event-parse.h"
19 #include "thread_map.h"
21 #include <linux/hw_breakpoint.h>
22 #include <linux/perf_event.h>
23 #include "perf_regs.h"
25 #define FD(e, x, y) (*(int *)xyarray__entry(e->fd, x, y))
27 static int __perf_evsel__sample_size(u64 sample_type
)
29 u64 mask
= sample_type
& PERF_SAMPLE_MASK
;
33 for (i
= 0; i
< 64; i
++) {
34 if (mask
& (1ULL << i
))
43 void hists__init(struct hists
*hists
)
45 memset(hists
, 0, sizeof(*hists
));
46 hists
->entries_in_array
[0] = hists
->entries_in_array
[1] = RB_ROOT
;
47 hists
->entries_in
= &hists
->entries_in_array
[0];
48 hists
->entries_collapsed
= RB_ROOT
;
49 hists
->entries
= RB_ROOT
;
50 pthread_mutex_init(&hists
->lock
, NULL
);
53 void __perf_evsel__set_sample_bit(struct perf_evsel
*evsel
,
54 enum perf_event_sample_format bit
)
56 if (!(evsel
->attr
.sample_type
& bit
)) {
57 evsel
->attr
.sample_type
|= bit
;
58 evsel
->sample_size
+= sizeof(u64
);
62 void __perf_evsel__reset_sample_bit(struct perf_evsel
*evsel
,
63 enum perf_event_sample_format bit
)
65 if (evsel
->attr
.sample_type
& bit
) {
66 evsel
->attr
.sample_type
&= ~bit
;
67 evsel
->sample_size
-= sizeof(u64
);
71 void perf_evsel__set_sample_id(struct perf_evsel
*evsel
)
73 perf_evsel__set_sample_bit(evsel
, ID
);
74 evsel
->attr
.read_format
|= PERF_FORMAT_ID
;
77 void perf_evsel__init(struct perf_evsel
*evsel
,
78 struct perf_event_attr
*attr
, int idx
)
82 evsel
->leader
= evsel
;
83 INIT_LIST_HEAD(&evsel
->node
);
84 hists__init(&evsel
->hists
);
85 evsel
->sample_size
= __perf_evsel__sample_size(attr
->sample_type
);
88 struct perf_evsel
*perf_evsel__new(struct perf_event_attr
*attr
, int idx
)
90 struct perf_evsel
*evsel
= zalloc(sizeof(*evsel
));
93 perf_evsel__init(evsel
, attr
, idx
);
98 struct event_format
*event_format__new(const char *sys
, const char *name
)
102 void *bf
= NULL
, *nbf
;
103 size_t size
= 0, alloc_size
= 0;
104 struct event_format
*format
= NULL
;
106 if (asprintf(&filename
, "%s/%s/%s/format", tracing_events_path
, sys
, name
) < 0)
109 fd
= open(filename
, O_RDONLY
);
111 goto out_free_filename
;
114 if (size
== alloc_size
) {
115 alloc_size
+= BUFSIZ
;
116 nbf
= realloc(bf
, alloc_size
);
122 n
= read(fd
, bf
+ size
, BUFSIZ
);
128 pevent_parse_format(&format
, bf
, size
, sys
);
139 struct perf_evsel
*perf_evsel__newtp(const char *sys
, const char *name
, int idx
)
141 struct perf_evsel
*evsel
= zalloc(sizeof(*evsel
));
144 struct perf_event_attr attr
= {
145 .type
= PERF_TYPE_TRACEPOINT
,
146 .sample_type
= (PERF_SAMPLE_RAW
| PERF_SAMPLE_TIME
|
147 PERF_SAMPLE_CPU
| PERF_SAMPLE_PERIOD
),
150 if (asprintf(&evsel
->name
, "%s:%s", sys
, name
) < 0)
153 evsel
->tp_format
= event_format__new(sys
, name
);
154 if (evsel
->tp_format
== NULL
)
157 event_attr_init(&attr
);
158 attr
.config
= evsel
->tp_format
->id
;
159 attr
.sample_period
= 1;
160 perf_evsel__init(evsel
, &attr
, idx
);
171 const char *perf_evsel__hw_names
[PERF_COUNT_HW_MAX
] = {
179 "stalled-cycles-frontend",
180 "stalled-cycles-backend",
184 static const char *__perf_evsel__hw_name(u64 config
)
186 if (config
< PERF_COUNT_HW_MAX
&& perf_evsel__hw_names
[config
])
187 return perf_evsel__hw_names
[config
];
189 return "unknown-hardware";
192 static int perf_evsel__add_modifiers(struct perf_evsel
*evsel
, char *bf
, size_t size
)
194 int colon
= 0, r
= 0;
195 struct perf_event_attr
*attr
= &evsel
->attr
;
196 bool exclude_guest_default
= false;
198 #define MOD_PRINT(context, mod) do { \
199 if (!attr->exclude_##context) { \
200 if (!colon) colon = ++r; \
201 r += scnprintf(bf + r, size - r, "%c", mod); \
204 if (attr
->exclude_kernel
|| attr
->exclude_user
|| attr
->exclude_hv
) {
205 MOD_PRINT(kernel
, 'k');
206 MOD_PRINT(user
, 'u');
208 exclude_guest_default
= true;
211 if (attr
->precise_ip
) {
214 r
+= scnprintf(bf
+ r
, size
- r
, "%.*s", attr
->precise_ip
, "ppp");
215 exclude_guest_default
= true;
218 if (attr
->exclude_host
|| attr
->exclude_guest
== exclude_guest_default
) {
219 MOD_PRINT(host
, 'H');
220 MOD_PRINT(guest
, 'G');
228 static int perf_evsel__hw_name(struct perf_evsel
*evsel
, char *bf
, size_t size
)
230 int r
= scnprintf(bf
, size
, "%s", __perf_evsel__hw_name(evsel
->attr
.config
));
231 return r
+ perf_evsel__add_modifiers(evsel
, bf
+ r
, size
- r
);
234 const char *perf_evsel__sw_names
[PERF_COUNT_SW_MAX
] = {
246 static const char *__perf_evsel__sw_name(u64 config
)
248 if (config
< PERF_COUNT_SW_MAX
&& perf_evsel__sw_names
[config
])
249 return perf_evsel__sw_names
[config
];
250 return "unknown-software";
253 static int perf_evsel__sw_name(struct perf_evsel
*evsel
, char *bf
, size_t size
)
255 int r
= scnprintf(bf
, size
, "%s", __perf_evsel__sw_name(evsel
->attr
.config
));
256 return r
+ perf_evsel__add_modifiers(evsel
, bf
+ r
, size
- r
);
259 static int __perf_evsel__bp_name(char *bf
, size_t size
, u64 addr
, u64 type
)
263 r
= scnprintf(bf
, size
, "mem:0x%" PRIx64
":", addr
);
265 if (type
& HW_BREAKPOINT_R
)
266 r
+= scnprintf(bf
+ r
, size
- r
, "r");
268 if (type
& HW_BREAKPOINT_W
)
269 r
+= scnprintf(bf
+ r
, size
- r
, "w");
271 if (type
& HW_BREAKPOINT_X
)
272 r
+= scnprintf(bf
+ r
, size
- r
, "x");
277 static int perf_evsel__bp_name(struct perf_evsel
*evsel
, char *bf
, size_t size
)
279 struct perf_event_attr
*attr
= &evsel
->attr
;
280 int r
= __perf_evsel__bp_name(bf
, size
, attr
->bp_addr
, attr
->bp_type
);
281 return r
+ perf_evsel__add_modifiers(evsel
, bf
+ r
, size
- r
);
284 const char *perf_evsel__hw_cache
[PERF_COUNT_HW_CACHE_MAX
]
285 [PERF_EVSEL__MAX_ALIASES
] = {
286 { "L1-dcache", "l1-d", "l1d", "L1-data", },
287 { "L1-icache", "l1-i", "l1i", "L1-instruction", },
289 { "dTLB", "d-tlb", "Data-TLB", },
290 { "iTLB", "i-tlb", "Instruction-TLB", },
291 { "branch", "branches", "bpu", "btb", "bpc", },
295 const char *perf_evsel__hw_cache_op
[PERF_COUNT_HW_CACHE_OP_MAX
]
296 [PERF_EVSEL__MAX_ALIASES
] = {
297 { "load", "loads", "read", },
298 { "store", "stores", "write", },
299 { "prefetch", "prefetches", "speculative-read", "speculative-load", },
302 const char *perf_evsel__hw_cache_result
[PERF_COUNT_HW_CACHE_RESULT_MAX
]
303 [PERF_EVSEL__MAX_ALIASES
] = {
304 { "refs", "Reference", "ops", "access", },
305 { "misses", "miss", },
308 #define C(x) PERF_COUNT_HW_CACHE_##x
309 #define CACHE_READ (1 << C(OP_READ))
310 #define CACHE_WRITE (1 << C(OP_WRITE))
311 #define CACHE_PREFETCH (1 << C(OP_PREFETCH))
312 #define COP(x) (1 << x)
315 * cache operartion stat
316 * L1I : Read and prefetch only
317 * ITLB and BPU : Read-only
319 static unsigned long perf_evsel__hw_cache_stat
[C(MAX
)] = {
320 [C(L1D
)] = (CACHE_READ
| CACHE_WRITE
| CACHE_PREFETCH
),
321 [C(L1I
)] = (CACHE_READ
| CACHE_PREFETCH
),
322 [C(LL
)] = (CACHE_READ
| CACHE_WRITE
| CACHE_PREFETCH
),
323 [C(DTLB
)] = (CACHE_READ
| CACHE_WRITE
| CACHE_PREFETCH
),
324 [C(ITLB
)] = (CACHE_READ
),
325 [C(BPU
)] = (CACHE_READ
),
326 [C(NODE
)] = (CACHE_READ
| CACHE_WRITE
| CACHE_PREFETCH
),
329 bool perf_evsel__is_cache_op_valid(u8 type
, u8 op
)
331 if (perf_evsel__hw_cache_stat
[type
] & COP(op
))
332 return true; /* valid */
334 return false; /* invalid */
337 int __perf_evsel__hw_cache_type_op_res_name(u8 type
, u8 op
, u8 result
,
338 char *bf
, size_t size
)
341 return scnprintf(bf
, size
, "%s-%s-%s", perf_evsel__hw_cache
[type
][0],
342 perf_evsel__hw_cache_op
[op
][0],
343 perf_evsel__hw_cache_result
[result
][0]);
346 return scnprintf(bf
, size
, "%s-%s", perf_evsel__hw_cache
[type
][0],
347 perf_evsel__hw_cache_op
[op
][1]);
350 static int __perf_evsel__hw_cache_name(u64 config
, char *bf
, size_t size
)
352 u8 op
, result
, type
= (config
>> 0) & 0xff;
353 const char *err
= "unknown-ext-hardware-cache-type";
355 if (type
> PERF_COUNT_HW_CACHE_MAX
)
358 op
= (config
>> 8) & 0xff;
359 err
= "unknown-ext-hardware-cache-op";
360 if (op
> PERF_COUNT_HW_CACHE_OP_MAX
)
363 result
= (config
>> 16) & 0xff;
364 err
= "unknown-ext-hardware-cache-result";
365 if (result
> PERF_COUNT_HW_CACHE_RESULT_MAX
)
368 err
= "invalid-cache";
369 if (!perf_evsel__is_cache_op_valid(type
, op
))
372 return __perf_evsel__hw_cache_type_op_res_name(type
, op
, result
, bf
, size
);
374 return scnprintf(bf
, size
, "%s", err
);
377 static int perf_evsel__hw_cache_name(struct perf_evsel
*evsel
, char *bf
, size_t size
)
379 int ret
= __perf_evsel__hw_cache_name(evsel
->attr
.config
, bf
, size
);
380 return ret
+ perf_evsel__add_modifiers(evsel
, bf
+ ret
, size
- ret
);
383 static int perf_evsel__raw_name(struct perf_evsel
*evsel
, char *bf
, size_t size
)
385 int ret
= scnprintf(bf
, size
, "raw 0x%" PRIx64
, evsel
->attr
.config
);
386 return ret
+ perf_evsel__add_modifiers(evsel
, bf
+ ret
, size
- ret
);
389 const char *perf_evsel__name(struct perf_evsel
*evsel
)
396 switch (evsel
->attr
.type
) {
398 perf_evsel__raw_name(evsel
, bf
, sizeof(bf
));
401 case PERF_TYPE_HARDWARE
:
402 perf_evsel__hw_name(evsel
, bf
, sizeof(bf
));
405 case PERF_TYPE_HW_CACHE
:
406 perf_evsel__hw_cache_name(evsel
, bf
, sizeof(bf
));
409 case PERF_TYPE_SOFTWARE
:
410 perf_evsel__sw_name(evsel
, bf
, sizeof(bf
));
413 case PERF_TYPE_TRACEPOINT
:
414 scnprintf(bf
, sizeof(bf
), "%s", "unknown tracepoint");
417 case PERF_TYPE_BREAKPOINT
:
418 perf_evsel__bp_name(evsel
, bf
, sizeof(bf
));
422 scnprintf(bf
, sizeof(bf
), "unknown attr type: %d",
427 evsel
->name
= strdup(bf
);
429 return evsel
->name
?: "unknown";
433 * The enable_on_exec/disabled value strategy:
435 * 1) For any type of traced program:
436 * - all independent events and group leaders are disabled
437 * - all group members are enabled
439 * Group members are ruled by group leaders. They need to
440 * be enabled, because the group scheduling relies on that.
442 * 2) For traced programs executed by perf:
443 * - all independent events and group leaders have
445 * - we don't specifically enable or disable any event during
448 * Independent events and group leaders are initially disabled
449 * and get enabled by exec. Group members are ruled by group
450 * leaders as stated in 1).
452 * 3) For traced programs attached by perf (pid/tid):
453 * - we specifically enable or disable all events during
456 * When attaching events to already running traced we
457 * enable/disable events specifically, as there's no
458 * initial traced exec call.
460 void perf_evsel__config(struct perf_evsel
*evsel
,
461 struct perf_record_opts
*opts
)
463 struct perf_event_attr
*attr
= &evsel
->attr
;
464 int track
= !evsel
->idx
; /* only the first counter needs these */
466 attr
->sample_id_all
= opts
->sample_id_all_missing
? 0 : 1;
467 attr
->inherit
= !opts
->no_inherit
;
468 attr
->read_format
= PERF_FORMAT_TOTAL_TIME_ENABLED
|
469 PERF_FORMAT_TOTAL_TIME_RUNNING
|
472 perf_evsel__set_sample_bit(evsel
, IP
);
473 perf_evsel__set_sample_bit(evsel
, TID
);
476 * We default some events to a 1 default interval. But keep
477 * it a weak assumption overridable by the user.
479 if (!attr
->sample_period
|| (opts
->user_freq
!= UINT_MAX
&&
480 opts
->user_interval
!= ULLONG_MAX
)) {
482 perf_evsel__set_sample_bit(evsel
, PERIOD
);
484 attr
->sample_freq
= opts
->freq
;
486 attr
->sample_period
= opts
->default_interval
;
490 if (opts
->no_samples
)
491 attr
->sample_freq
= 0;
493 if (opts
->inherit_stat
)
494 attr
->inherit_stat
= 1;
496 if (opts
->sample_address
) {
497 perf_evsel__set_sample_bit(evsel
, ADDR
);
498 attr
->mmap_data
= track
;
501 if (opts
->call_graph
) {
502 perf_evsel__set_sample_bit(evsel
, CALLCHAIN
);
504 if (opts
->call_graph
== CALLCHAIN_DWARF
) {
505 perf_evsel__set_sample_bit(evsel
, REGS_USER
);
506 perf_evsel__set_sample_bit(evsel
, STACK_USER
);
507 attr
->sample_regs_user
= PERF_REGS_MASK
;
508 attr
->sample_stack_user
= opts
->stack_dump_size
;
509 attr
->exclude_callchain_user
= 1;
513 if (perf_target__has_cpu(&opts
->target
))
514 perf_evsel__set_sample_bit(evsel
, CPU
);
517 perf_evsel__set_sample_bit(evsel
, PERIOD
);
519 if (!opts
->sample_id_all_missing
&&
520 (opts
->sample_time
|| !opts
->no_inherit
||
521 perf_target__has_cpu(&opts
->target
)))
522 perf_evsel__set_sample_bit(evsel
, TIME
);
524 if (opts
->raw_samples
) {
525 perf_evsel__set_sample_bit(evsel
, TIME
);
526 perf_evsel__set_sample_bit(evsel
, RAW
);
527 perf_evsel__set_sample_bit(evsel
, CPU
);
530 if (opts
->no_delay
) {
532 attr
->wakeup_events
= 1;
534 if (opts
->branch_stack
) {
535 perf_evsel__set_sample_bit(evsel
, BRANCH_STACK
);
536 attr
->branch_sample_type
= opts
->branch_stack
;
543 * XXX see the function comment above
545 * Disabling only independent events or group leaders,
546 * keeping group members enabled.
548 if (perf_evsel__is_group_leader(evsel
))
552 * Setting enable_on_exec for independent events and
553 * group leaders for traced executed by perf.
555 if (perf_target__none(&opts
->target
) && perf_evsel__is_group_leader(evsel
))
556 attr
->enable_on_exec
= 1;
559 int perf_evsel__alloc_fd(struct perf_evsel
*evsel
, int ncpus
, int nthreads
)
562 evsel
->fd
= xyarray__new(ncpus
, nthreads
, sizeof(int));
565 for (cpu
= 0; cpu
< ncpus
; cpu
++) {
566 for (thread
= 0; thread
< nthreads
; thread
++) {
567 FD(evsel
, cpu
, thread
) = -1;
572 return evsel
->fd
!= NULL
? 0 : -ENOMEM
;
575 int perf_evsel__set_filter(struct perf_evsel
*evsel
, int ncpus
, int nthreads
,
580 for (cpu
= 0; cpu
< ncpus
; cpu
++) {
581 for (thread
= 0; thread
< nthreads
; thread
++) {
582 int fd
= FD(evsel
, cpu
, thread
),
583 err
= ioctl(fd
, PERF_EVENT_IOC_SET_FILTER
, filter
);
593 int perf_evsel__alloc_id(struct perf_evsel
*evsel
, int ncpus
, int nthreads
)
595 evsel
->sample_id
= xyarray__new(ncpus
, nthreads
, sizeof(struct perf_sample_id
));
596 if (evsel
->sample_id
== NULL
)
599 evsel
->id
= zalloc(ncpus
* nthreads
* sizeof(u64
));
600 if (evsel
->id
== NULL
) {
601 xyarray__delete(evsel
->sample_id
);
602 evsel
->sample_id
= NULL
;
609 int perf_evsel__alloc_counts(struct perf_evsel
*evsel
, int ncpus
)
611 evsel
->counts
= zalloc((sizeof(*evsel
->counts
) +
612 (ncpus
* sizeof(struct perf_counts_values
))));
613 return evsel
->counts
!= NULL
? 0 : -ENOMEM
;
616 void perf_evsel__free_fd(struct perf_evsel
*evsel
)
618 xyarray__delete(evsel
->fd
);
622 void perf_evsel__free_id(struct perf_evsel
*evsel
)
624 xyarray__delete(evsel
->sample_id
);
625 evsel
->sample_id
= NULL
;
630 void perf_evsel__close_fd(struct perf_evsel
*evsel
, int ncpus
, int nthreads
)
634 for (cpu
= 0; cpu
< ncpus
; cpu
++)
635 for (thread
= 0; thread
< nthreads
; ++thread
) {
636 close(FD(evsel
, cpu
, thread
));
637 FD(evsel
, cpu
, thread
) = -1;
641 void perf_evsel__exit(struct perf_evsel
*evsel
)
643 assert(list_empty(&evsel
->node
));
644 xyarray__delete(evsel
->fd
);
645 xyarray__delete(evsel
->sample_id
);
649 void perf_evsel__delete(struct perf_evsel
*evsel
)
651 perf_evsel__exit(evsel
);
652 close_cgroup(evsel
->cgrp
);
653 free(evsel
->group_name
);
654 if (evsel
->tp_format
)
655 pevent_free_format(evsel
->tp_format
);
660 int __perf_evsel__read_on_cpu(struct perf_evsel
*evsel
,
661 int cpu
, int thread
, bool scale
)
663 struct perf_counts_values count
;
664 size_t nv
= scale
? 3 : 1;
666 if (FD(evsel
, cpu
, thread
) < 0)
669 if (evsel
->counts
== NULL
&& perf_evsel__alloc_counts(evsel
, cpu
+ 1) < 0)
672 if (readn(FD(evsel
, cpu
, thread
), &count
, nv
* sizeof(u64
)) < 0)
678 else if (count
.run
< count
.ena
)
679 count
.val
= (u64
)((double)count
.val
* count
.ena
/ count
.run
+ 0.5);
681 count
.ena
= count
.run
= 0;
683 evsel
->counts
->cpu
[cpu
] = count
;
687 int __perf_evsel__read(struct perf_evsel
*evsel
,
688 int ncpus
, int nthreads
, bool scale
)
690 size_t nv
= scale
? 3 : 1;
692 struct perf_counts_values
*aggr
= &evsel
->counts
->aggr
, count
;
694 aggr
->val
= aggr
->ena
= aggr
->run
= 0;
696 for (cpu
= 0; cpu
< ncpus
; cpu
++) {
697 for (thread
= 0; thread
< nthreads
; thread
++) {
698 if (FD(evsel
, cpu
, thread
) < 0)
701 if (readn(FD(evsel
, cpu
, thread
),
702 &count
, nv
* sizeof(u64
)) < 0)
705 aggr
->val
+= count
.val
;
707 aggr
->ena
+= count
.ena
;
708 aggr
->run
+= count
.run
;
713 evsel
->counts
->scaled
= 0;
715 if (aggr
->run
== 0) {
716 evsel
->counts
->scaled
= -1;
721 if (aggr
->run
< aggr
->ena
) {
722 evsel
->counts
->scaled
= 1;
723 aggr
->val
= (u64
)((double)aggr
->val
* aggr
->ena
/ aggr
->run
+ 0.5);
726 aggr
->ena
= aggr
->run
= 0;
731 static int get_group_fd(struct perf_evsel
*evsel
, int cpu
, int thread
)
733 struct perf_evsel
*leader
= evsel
->leader
;
736 if (perf_evsel__is_group_leader(evsel
))
740 * Leader must be already processed/open,
745 fd
= FD(leader
, cpu
, thread
);
751 static int __perf_evsel__open(struct perf_evsel
*evsel
, struct cpu_map
*cpus
,
752 struct thread_map
*threads
)
755 unsigned long flags
= 0;
758 if (evsel
->fd
== NULL
&&
759 perf_evsel__alloc_fd(evsel
, cpus
->nr
, threads
->nr
) < 0)
763 flags
= PERF_FLAG_PID_CGROUP
;
764 pid
= evsel
->cgrp
->fd
;
767 for (cpu
= 0; cpu
< cpus
->nr
; cpu
++) {
769 for (thread
= 0; thread
< threads
->nr
; thread
++) {
773 pid
= threads
->map
[thread
];
775 group_fd
= get_group_fd(evsel
, cpu
, thread
);
777 FD(evsel
, cpu
, thread
) = sys_perf_event_open(&evsel
->attr
,
781 if (FD(evsel
, cpu
, thread
) < 0) {
792 while (--thread
>= 0) {
793 close(FD(evsel
, cpu
, thread
));
794 FD(evsel
, cpu
, thread
) = -1;
796 thread
= threads
->nr
;
797 } while (--cpu
>= 0);
801 void perf_evsel__close(struct perf_evsel
*evsel
, int ncpus
, int nthreads
)
803 if (evsel
->fd
== NULL
)
806 perf_evsel__close_fd(evsel
, ncpus
, nthreads
);
807 perf_evsel__free_fd(evsel
);
820 struct thread_map map
;
822 } empty_thread_map
= {
827 int perf_evsel__open(struct perf_evsel
*evsel
, struct cpu_map
*cpus
,
828 struct thread_map
*threads
)
831 /* Work around old compiler warnings about strict aliasing */
832 cpus
= &empty_cpu_map
.map
;
836 threads
= &empty_thread_map
.map
;
838 return __perf_evsel__open(evsel
, cpus
, threads
);
841 int perf_evsel__open_per_cpu(struct perf_evsel
*evsel
,
842 struct cpu_map
*cpus
)
844 return __perf_evsel__open(evsel
, cpus
, &empty_thread_map
.map
);
847 int perf_evsel__open_per_thread(struct perf_evsel
*evsel
,
848 struct thread_map
*threads
)
850 return __perf_evsel__open(evsel
, &empty_cpu_map
.map
, threads
);
853 static int perf_evsel__parse_id_sample(const struct perf_evsel
*evsel
,
854 const union perf_event
*event
,
855 struct perf_sample
*sample
)
857 u64 type
= evsel
->attr
.sample_type
;
858 const u64
*array
= event
->sample
.array
;
859 bool swapped
= evsel
->needs_swap
;
862 array
+= ((event
->header
.size
-
863 sizeof(event
->header
)) / sizeof(u64
)) - 1;
865 if (type
& PERF_SAMPLE_CPU
) {
868 /* undo swap of u64, then swap on individual u32s */
869 u
.val64
= bswap_64(u
.val64
);
870 u
.val32
[0] = bswap_32(u
.val32
[0]);
873 sample
->cpu
= u
.val32
[0];
877 if (type
& PERF_SAMPLE_STREAM_ID
) {
878 sample
->stream_id
= *array
;
882 if (type
& PERF_SAMPLE_ID
) {
887 if (type
& PERF_SAMPLE_TIME
) {
888 sample
->time
= *array
;
892 if (type
& PERF_SAMPLE_TID
) {
895 /* undo swap of u64, then swap on individual u32s */
896 u
.val64
= bswap_64(u
.val64
);
897 u
.val32
[0] = bswap_32(u
.val32
[0]);
898 u
.val32
[1] = bswap_32(u
.val32
[1]);
901 sample
->pid
= u
.val32
[0];
902 sample
->tid
= u
.val32
[1];
908 static bool sample_overlap(const union perf_event
*event
,
909 const void *offset
, u64 size
)
911 const void *base
= event
;
913 if (offset
+ size
> base
+ event
->header
.size
)
919 int perf_evsel__parse_sample(struct perf_evsel
*evsel
, union perf_event
*event
,
920 struct perf_sample
*data
)
922 u64 type
= evsel
->attr
.sample_type
;
923 u64 regs_user
= evsel
->attr
.sample_regs_user
;
924 bool swapped
= evsel
->needs_swap
;
928 * used for cross-endian analysis. See git commit 65014ab3
929 * for why this goofiness is needed.
933 memset(data
, 0, sizeof(*data
));
934 data
->cpu
= data
->pid
= data
->tid
= -1;
935 data
->stream_id
= data
->id
= data
->time
= -1ULL;
938 if (event
->header
.type
!= PERF_RECORD_SAMPLE
) {
939 if (!evsel
->attr
.sample_id_all
)
941 return perf_evsel__parse_id_sample(evsel
, event
, data
);
944 array
= event
->sample
.array
;
946 if (evsel
->sample_size
+ sizeof(event
->header
) > event
->header
.size
)
949 if (type
& PERF_SAMPLE_IP
) {
950 data
->ip
= event
->ip
.ip
;
954 if (type
& PERF_SAMPLE_TID
) {
957 /* undo swap of u64, then swap on individual u32s */
958 u
.val64
= bswap_64(u
.val64
);
959 u
.val32
[0] = bswap_32(u
.val32
[0]);
960 u
.val32
[1] = bswap_32(u
.val32
[1]);
963 data
->pid
= u
.val32
[0];
964 data
->tid
= u
.val32
[1];
968 if (type
& PERF_SAMPLE_TIME
) {
974 if (type
& PERF_SAMPLE_ADDR
) {
980 if (type
& PERF_SAMPLE_ID
) {
985 if (type
& PERF_SAMPLE_STREAM_ID
) {
986 data
->stream_id
= *array
;
990 if (type
& PERF_SAMPLE_CPU
) {
994 /* undo swap of u64, then swap on individual u32s */
995 u
.val64
= bswap_64(u
.val64
);
996 u
.val32
[0] = bswap_32(u
.val32
[0]);
999 data
->cpu
= u
.val32
[0];
1003 if (type
& PERF_SAMPLE_PERIOD
) {
1004 data
->period
= *array
;
1008 if (type
& PERF_SAMPLE_READ
) {
1009 fprintf(stderr
, "PERF_SAMPLE_READ is unsupported for now\n");
1013 if (type
& PERF_SAMPLE_CALLCHAIN
) {
1014 if (sample_overlap(event
, array
, sizeof(data
->callchain
->nr
)))
1017 data
->callchain
= (struct ip_callchain
*)array
;
1019 if (sample_overlap(event
, array
, data
->callchain
->nr
))
1022 array
+= 1 + data
->callchain
->nr
;
1025 if (type
& PERF_SAMPLE_RAW
) {
1029 if (WARN_ONCE(swapped
,
1030 "Endianness of raw data not corrected!\n")) {
1031 /* undo swap of u64, then swap on individual u32s */
1032 u
.val64
= bswap_64(u
.val64
);
1033 u
.val32
[0] = bswap_32(u
.val32
[0]);
1034 u
.val32
[1] = bswap_32(u
.val32
[1]);
1037 if (sample_overlap(event
, array
, sizeof(u32
)))
1040 data
->raw_size
= u
.val32
[0];
1041 pdata
= (void *) array
+ sizeof(u32
);
1043 if (sample_overlap(event
, pdata
, data
->raw_size
))
1046 data
->raw_data
= (void *) pdata
;
1048 array
= (void *)array
+ data
->raw_size
+ sizeof(u32
);
1051 if (type
& PERF_SAMPLE_BRANCH_STACK
) {
1054 data
->branch_stack
= (struct branch_stack
*)array
;
1057 sz
= data
->branch_stack
->nr
* sizeof(struct branch_entry
);
1062 if (type
& PERF_SAMPLE_REGS_USER
) {
1063 /* First u64 tells us if we have any regs in sample. */
1064 u64 avail
= *array
++;
1067 data
->user_regs
.regs
= (u64
*)array
;
1068 array
+= hweight_long(regs_user
);
1072 if (type
& PERF_SAMPLE_STACK_USER
) {
1073 u64 size
= *array
++;
1075 data
->user_stack
.offset
= ((char *)(array
- 1)
1079 data
->user_stack
.size
= 0;
1081 data
->user_stack
.data
= (char *)array
;
1082 array
+= size
/ sizeof(*array
);
1083 data
->user_stack
.size
= *array
;
1090 int perf_event__synthesize_sample(union perf_event
*event
, u64 type
,
1091 const struct perf_sample
*sample
,
1097 * used for cross-endian analysis. See git commit 65014ab3
1098 * for why this goofiness is needed.
1102 array
= event
->sample
.array
;
1104 if (type
& PERF_SAMPLE_IP
) {
1105 event
->ip
.ip
= sample
->ip
;
1109 if (type
& PERF_SAMPLE_TID
) {
1110 u
.val32
[0] = sample
->pid
;
1111 u
.val32
[1] = sample
->tid
;
1114 * Inverse of what is done in perf_evsel__parse_sample
1116 u
.val32
[0] = bswap_32(u
.val32
[0]);
1117 u
.val32
[1] = bswap_32(u
.val32
[1]);
1118 u
.val64
= bswap_64(u
.val64
);
1125 if (type
& PERF_SAMPLE_TIME
) {
1126 *array
= sample
->time
;
1130 if (type
& PERF_SAMPLE_ADDR
) {
1131 *array
= sample
->addr
;
1135 if (type
& PERF_SAMPLE_ID
) {
1136 *array
= sample
->id
;
1140 if (type
& PERF_SAMPLE_STREAM_ID
) {
1141 *array
= sample
->stream_id
;
1145 if (type
& PERF_SAMPLE_CPU
) {
1146 u
.val32
[0] = sample
->cpu
;
1149 * Inverse of what is done in perf_evsel__parse_sample
1151 u
.val32
[0] = bswap_32(u
.val32
[0]);
1152 u
.val64
= bswap_64(u
.val64
);
1158 if (type
& PERF_SAMPLE_PERIOD
) {
1159 *array
= sample
->period
;
1166 struct format_field
*perf_evsel__field(struct perf_evsel
*evsel
, const char *name
)
1168 return pevent_find_field(evsel
->tp_format
, name
);
1171 void *perf_evsel__rawptr(struct perf_evsel
*evsel
, struct perf_sample
*sample
,
1174 struct format_field
*field
= perf_evsel__field(evsel
, name
);
1180 offset
= field
->offset
;
1182 if (field
->flags
& FIELD_IS_DYNAMIC
) {
1183 offset
= *(int *)(sample
->raw_data
+ field
->offset
);
1187 return sample
->raw_data
+ offset
;
1190 u64
perf_evsel__intval(struct perf_evsel
*evsel
, struct perf_sample
*sample
,
1193 struct format_field
*field
= perf_evsel__field(evsel
, name
);
1200 ptr
= sample
->raw_data
+ field
->offset
;
1202 switch (field
->size
) {
1206 value
= *(u16
*)ptr
;
1209 value
= *(u32
*)ptr
;
1212 value
= *(u64
*)ptr
;
1218 if (!evsel
->needs_swap
)
1221 switch (field
->size
) {
1223 return bswap_16(value
);
1225 return bswap_32(value
);
1227 return bswap_64(value
);