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 "../../../include/linux/hw_breakpoint.h"
22 #include "../../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__init(struct perf_evsel
*evsel
,
54 struct perf_event_attr
*attr
, int idx
)
58 INIT_LIST_HEAD(&evsel
->node
);
59 hists__init(&evsel
->hists
);
60 evsel
->sample_size
= __perf_evsel__sample_size(attr
->sample_type
);
63 struct perf_evsel
*perf_evsel__new(struct perf_event_attr
*attr
, int idx
)
65 struct perf_evsel
*evsel
= zalloc(sizeof(*evsel
));
68 perf_evsel__init(evsel
, attr
, idx
);
73 struct event_format
*event_format__new(const char *sys
, const char *name
)
77 void *bf
= NULL
, *nbf
;
78 size_t size
= 0, alloc_size
= 0;
79 struct event_format
*format
= NULL
;
81 if (asprintf(&filename
, "%s/%s/%s/format", tracing_events_path
, sys
, name
) < 0)
84 fd
= open(filename
, O_RDONLY
);
86 goto out_free_filename
;
89 if (size
== alloc_size
) {
91 nbf
= realloc(bf
, alloc_size
);
97 n
= read(fd
, bf
+ size
, BUFSIZ
);
103 pevent_parse_format(&format
, bf
, size
, sys
);
114 struct perf_evsel
*perf_evsel__newtp(const char *sys
, const char *name
, int idx
)
116 struct perf_evsel
*evsel
= zalloc(sizeof(*evsel
));
119 struct perf_event_attr attr
= {
120 .type
= PERF_TYPE_TRACEPOINT
,
121 .sample_type
= (PERF_SAMPLE_RAW
| PERF_SAMPLE_TIME
|
122 PERF_SAMPLE_CPU
| PERF_SAMPLE_PERIOD
),
125 if (asprintf(&evsel
->name
, "%s:%s", sys
, name
) < 0)
128 evsel
->tp_format
= event_format__new(sys
, name
);
129 if (evsel
->tp_format
== NULL
)
132 event_attr_init(&attr
);
133 attr
.config
= evsel
->tp_format
->id
;
134 attr
.sample_period
= 1;
135 perf_evsel__init(evsel
, &attr
, idx
);
146 const char *perf_evsel__hw_names
[PERF_COUNT_HW_MAX
] = {
154 "stalled-cycles-frontend",
155 "stalled-cycles-backend",
159 static const char *__perf_evsel__hw_name(u64 config
)
161 if (config
< PERF_COUNT_HW_MAX
&& perf_evsel__hw_names
[config
])
162 return perf_evsel__hw_names
[config
];
164 return "unknown-hardware";
167 static int perf_evsel__add_modifiers(struct perf_evsel
*evsel
, char *bf
, size_t size
)
169 int colon
= 0, r
= 0;
170 struct perf_event_attr
*attr
= &evsel
->attr
;
171 bool exclude_guest_default
= false;
173 #define MOD_PRINT(context, mod) do { \
174 if (!attr->exclude_##context) { \
175 if (!colon) colon = ++r; \
176 r += scnprintf(bf + r, size - r, "%c", mod); \
179 if (attr
->exclude_kernel
|| attr
->exclude_user
|| attr
->exclude_hv
) {
180 MOD_PRINT(kernel
, 'k');
181 MOD_PRINT(user
, 'u');
183 exclude_guest_default
= true;
186 if (attr
->precise_ip
) {
189 r
+= scnprintf(bf
+ r
, size
- r
, "%.*s", attr
->precise_ip
, "ppp");
190 exclude_guest_default
= true;
193 if (attr
->exclude_host
|| attr
->exclude_guest
== exclude_guest_default
) {
194 MOD_PRINT(host
, 'H');
195 MOD_PRINT(guest
, 'G');
203 static int perf_evsel__hw_name(struct perf_evsel
*evsel
, char *bf
, size_t size
)
205 int r
= scnprintf(bf
, size
, "%s", __perf_evsel__hw_name(evsel
->attr
.config
));
206 return r
+ perf_evsel__add_modifiers(evsel
, bf
+ r
, size
- r
);
209 const char *perf_evsel__sw_names
[PERF_COUNT_SW_MAX
] = {
221 static const char *__perf_evsel__sw_name(u64 config
)
223 if (config
< PERF_COUNT_SW_MAX
&& perf_evsel__sw_names
[config
])
224 return perf_evsel__sw_names
[config
];
225 return "unknown-software";
228 static int perf_evsel__sw_name(struct perf_evsel
*evsel
, char *bf
, size_t size
)
230 int r
= scnprintf(bf
, size
, "%s", __perf_evsel__sw_name(evsel
->attr
.config
));
231 return r
+ perf_evsel__add_modifiers(evsel
, bf
+ r
, size
- r
);
234 static int __perf_evsel__bp_name(char *bf
, size_t size
, u64 addr
, u64 type
)
238 r
= scnprintf(bf
, size
, "mem:0x%" PRIx64
":", addr
);
240 if (type
& HW_BREAKPOINT_R
)
241 r
+= scnprintf(bf
+ r
, size
- r
, "r");
243 if (type
& HW_BREAKPOINT_W
)
244 r
+= scnprintf(bf
+ r
, size
- r
, "w");
246 if (type
& HW_BREAKPOINT_X
)
247 r
+= scnprintf(bf
+ r
, size
- r
, "x");
252 static int perf_evsel__bp_name(struct perf_evsel
*evsel
, char *bf
, size_t size
)
254 struct perf_event_attr
*attr
= &evsel
->attr
;
255 int r
= __perf_evsel__bp_name(bf
, size
, attr
->bp_addr
, attr
->bp_type
);
256 return r
+ perf_evsel__add_modifiers(evsel
, bf
+ r
, size
- r
);
259 const char *perf_evsel__hw_cache
[PERF_COUNT_HW_CACHE_MAX
]
260 [PERF_EVSEL__MAX_ALIASES
] = {
261 { "L1-dcache", "l1-d", "l1d", "L1-data", },
262 { "L1-icache", "l1-i", "l1i", "L1-instruction", },
264 { "dTLB", "d-tlb", "Data-TLB", },
265 { "iTLB", "i-tlb", "Instruction-TLB", },
266 { "branch", "branches", "bpu", "btb", "bpc", },
270 const char *perf_evsel__hw_cache_op
[PERF_COUNT_HW_CACHE_OP_MAX
]
271 [PERF_EVSEL__MAX_ALIASES
] = {
272 { "load", "loads", "read", },
273 { "store", "stores", "write", },
274 { "prefetch", "prefetches", "speculative-read", "speculative-load", },
277 const char *perf_evsel__hw_cache_result
[PERF_COUNT_HW_CACHE_RESULT_MAX
]
278 [PERF_EVSEL__MAX_ALIASES
] = {
279 { "refs", "Reference", "ops", "access", },
280 { "misses", "miss", },
283 #define C(x) PERF_COUNT_HW_CACHE_##x
284 #define CACHE_READ (1 << C(OP_READ))
285 #define CACHE_WRITE (1 << C(OP_WRITE))
286 #define CACHE_PREFETCH (1 << C(OP_PREFETCH))
287 #define COP(x) (1 << x)
290 * cache operartion stat
291 * L1I : Read and prefetch only
292 * ITLB and BPU : Read-only
294 static unsigned long perf_evsel__hw_cache_stat
[C(MAX
)] = {
295 [C(L1D
)] = (CACHE_READ
| CACHE_WRITE
| CACHE_PREFETCH
),
296 [C(L1I
)] = (CACHE_READ
| CACHE_PREFETCH
),
297 [C(LL
)] = (CACHE_READ
| CACHE_WRITE
| CACHE_PREFETCH
),
298 [C(DTLB
)] = (CACHE_READ
| CACHE_WRITE
| CACHE_PREFETCH
),
299 [C(ITLB
)] = (CACHE_READ
),
300 [C(BPU
)] = (CACHE_READ
),
301 [C(NODE
)] = (CACHE_READ
| CACHE_WRITE
| CACHE_PREFETCH
),
304 bool perf_evsel__is_cache_op_valid(u8 type
, u8 op
)
306 if (perf_evsel__hw_cache_stat
[type
] & COP(op
))
307 return true; /* valid */
309 return false; /* invalid */
312 int __perf_evsel__hw_cache_type_op_res_name(u8 type
, u8 op
, u8 result
,
313 char *bf
, size_t size
)
316 return scnprintf(bf
, size
, "%s-%s-%s", perf_evsel__hw_cache
[type
][0],
317 perf_evsel__hw_cache_op
[op
][0],
318 perf_evsel__hw_cache_result
[result
][0]);
321 return scnprintf(bf
, size
, "%s-%s", perf_evsel__hw_cache
[type
][0],
322 perf_evsel__hw_cache_op
[op
][1]);
325 static int __perf_evsel__hw_cache_name(u64 config
, char *bf
, size_t size
)
327 u8 op
, result
, type
= (config
>> 0) & 0xff;
328 const char *err
= "unknown-ext-hardware-cache-type";
330 if (type
> PERF_COUNT_HW_CACHE_MAX
)
333 op
= (config
>> 8) & 0xff;
334 err
= "unknown-ext-hardware-cache-op";
335 if (op
> PERF_COUNT_HW_CACHE_OP_MAX
)
338 result
= (config
>> 16) & 0xff;
339 err
= "unknown-ext-hardware-cache-result";
340 if (result
> PERF_COUNT_HW_CACHE_RESULT_MAX
)
343 err
= "invalid-cache";
344 if (!perf_evsel__is_cache_op_valid(type
, op
))
347 return __perf_evsel__hw_cache_type_op_res_name(type
, op
, result
, bf
, size
);
349 return scnprintf(bf
, size
, "%s", err
);
352 static int perf_evsel__hw_cache_name(struct perf_evsel
*evsel
, char *bf
, size_t size
)
354 int ret
= __perf_evsel__hw_cache_name(evsel
->attr
.config
, bf
, size
);
355 return ret
+ perf_evsel__add_modifiers(evsel
, bf
+ ret
, size
- ret
);
358 static int perf_evsel__raw_name(struct perf_evsel
*evsel
, char *bf
, size_t size
)
360 int ret
= scnprintf(bf
, size
, "raw 0x%" PRIx64
, evsel
->attr
.config
);
361 return ret
+ perf_evsel__add_modifiers(evsel
, bf
+ ret
, size
- ret
);
364 const char *perf_evsel__name(struct perf_evsel
*evsel
)
371 switch (evsel
->attr
.type
) {
373 perf_evsel__raw_name(evsel
, bf
, sizeof(bf
));
376 case PERF_TYPE_HARDWARE
:
377 perf_evsel__hw_name(evsel
, bf
, sizeof(bf
));
380 case PERF_TYPE_HW_CACHE
:
381 perf_evsel__hw_cache_name(evsel
, bf
, sizeof(bf
));
384 case PERF_TYPE_SOFTWARE
:
385 perf_evsel__sw_name(evsel
, bf
, sizeof(bf
));
388 case PERF_TYPE_TRACEPOINT
:
389 scnprintf(bf
, sizeof(bf
), "%s", "unknown tracepoint");
392 case PERF_TYPE_BREAKPOINT
:
393 perf_evsel__bp_name(evsel
, bf
, sizeof(bf
));
397 scnprintf(bf
, sizeof(bf
), "unknown attr type: %d",
402 evsel
->name
= strdup(bf
);
404 return evsel
->name
?: "unknown";
407 void perf_evsel__config(struct perf_evsel
*evsel
, struct perf_record_opts
*opts
,
408 struct perf_evsel
*first
)
410 struct perf_event_attr
*attr
= &evsel
->attr
;
411 int track
= !evsel
->idx
; /* only the first counter needs these */
414 attr
->sample_id_all
= opts
->sample_id_all_missing
? 0 : 1;
415 attr
->inherit
= !opts
->no_inherit
;
416 attr
->read_format
= PERF_FORMAT_TOTAL_TIME_ENABLED
|
417 PERF_FORMAT_TOTAL_TIME_RUNNING
|
420 attr
->sample_type
|= PERF_SAMPLE_IP
| PERF_SAMPLE_TID
;
423 * We default some events to a 1 default interval. But keep
424 * it a weak assumption overridable by the user.
426 if (!attr
->sample_period
|| (opts
->user_freq
!= UINT_MAX
&&
427 opts
->user_interval
!= ULLONG_MAX
)) {
429 attr
->sample_type
|= PERF_SAMPLE_PERIOD
;
431 attr
->sample_freq
= opts
->freq
;
433 attr
->sample_period
= opts
->default_interval
;
437 if (opts
->no_samples
)
438 attr
->sample_freq
= 0;
440 if (opts
->inherit_stat
)
441 attr
->inherit_stat
= 1;
443 if (opts
->sample_address
) {
444 attr
->sample_type
|= PERF_SAMPLE_ADDR
;
445 attr
->mmap_data
= track
;
448 if (opts
->call_graph
) {
449 attr
->sample_type
|= PERF_SAMPLE_CALLCHAIN
;
451 if (opts
->call_graph
== CALLCHAIN_DWARF
) {
452 attr
->sample_type
|= PERF_SAMPLE_REGS_USER
|
453 PERF_SAMPLE_STACK_USER
;
454 attr
->sample_regs_user
= PERF_REGS_MASK
;
455 attr
->sample_stack_user
= opts
->stack_dump_size
;
456 attr
->exclude_callchain_user
= 1;
460 if (perf_target__has_cpu(&opts
->target
))
461 attr
->sample_type
|= PERF_SAMPLE_CPU
;
464 attr
->sample_type
|= PERF_SAMPLE_PERIOD
;
466 if (!opts
->sample_id_all_missing
&&
467 (opts
->sample_time
|| !opts
->no_inherit
||
468 perf_target__has_cpu(&opts
->target
)))
469 attr
->sample_type
|= PERF_SAMPLE_TIME
;
471 if (opts
->raw_samples
) {
472 attr
->sample_type
|= PERF_SAMPLE_TIME
;
473 attr
->sample_type
|= PERF_SAMPLE_RAW
;
474 attr
->sample_type
|= PERF_SAMPLE_CPU
;
477 if (opts
->no_delay
) {
479 attr
->wakeup_events
= 1;
481 if (opts
->branch_stack
) {
482 attr
->sample_type
|= PERF_SAMPLE_BRANCH_STACK
;
483 attr
->branch_sample_type
= opts
->branch_stack
;
489 if (perf_target__none(&opts
->target
) &&
490 (!opts
->group
|| evsel
== first
)) {
491 attr
->enable_on_exec
= 1;
495 int perf_evsel__alloc_fd(struct perf_evsel
*evsel
, int ncpus
, int nthreads
)
498 evsel
->fd
= xyarray__new(ncpus
, nthreads
, sizeof(int));
501 for (cpu
= 0; cpu
< ncpus
; cpu
++) {
502 for (thread
= 0; thread
< nthreads
; thread
++) {
503 FD(evsel
, cpu
, thread
) = -1;
508 return evsel
->fd
!= NULL
? 0 : -ENOMEM
;
511 int perf_evsel__set_filter(struct perf_evsel
*evsel
, int ncpus
, int nthreads
,
516 for (cpu
= 0; cpu
< ncpus
; cpu
++) {
517 for (thread
= 0; thread
< nthreads
; thread
++) {
518 int fd
= FD(evsel
, cpu
, thread
),
519 err
= ioctl(fd
, PERF_EVENT_IOC_SET_FILTER
, filter
);
529 int perf_evsel__alloc_id(struct perf_evsel
*evsel
, int ncpus
, int nthreads
)
531 evsel
->sample_id
= xyarray__new(ncpus
, nthreads
, sizeof(struct perf_sample_id
));
532 if (evsel
->sample_id
== NULL
)
535 evsel
->id
= zalloc(ncpus
* nthreads
* sizeof(u64
));
536 if (evsel
->id
== NULL
) {
537 xyarray__delete(evsel
->sample_id
);
538 evsel
->sample_id
= NULL
;
545 int perf_evsel__alloc_counts(struct perf_evsel
*evsel
, int ncpus
)
547 evsel
->counts
= zalloc((sizeof(*evsel
->counts
) +
548 (ncpus
* sizeof(struct perf_counts_values
))));
549 return evsel
->counts
!= NULL
? 0 : -ENOMEM
;
552 void perf_evsel__free_fd(struct perf_evsel
*evsel
)
554 xyarray__delete(evsel
->fd
);
558 void perf_evsel__free_id(struct perf_evsel
*evsel
)
560 xyarray__delete(evsel
->sample_id
);
561 evsel
->sample_id
= NULL
;
566 void perf_evsel__close_fd(struct perf_evsel
*evsel
, int ncpus
, int nthreads
)
570 for (cpu
= 0; cpu
< ncpus
; cpu
++)
571 for (thread
= 0; thread
< nthreads
; ++thread
) {
572 close(FD(evsel
, cpu
, thread
));
573 FD(evsel
, cpu
, thread
) = -1;
577 void perf_evsel__exit(struct perf_evsel
*evsel
)
579 assert(list_empty(&evsel
->node
));
580 xyarray__delete(evsel
->fd
);
581 xyarray__delete(evsel
->sample_id
);
585 void perf_evsel__delete(struct perf_evsel
*evsel
)
587 perf_evsel__exit(evsel
);
588 close_cgroup(evsel
->cgrp
);
589 free(evsel
->group_name
);
590 if (evsel
->tp_format
)
591 pevent_free_format(evsel
->tp_format
);
596 int __perf_evsel__read_on_cpu(struct perf_evsel
*evsel
,
597 int cpu
, int thread
, bool scale
)
599 struct perf_counts_values count
;
600 size_t nv
= scale
? 3 : 1;
602 if (FD(evsel
, cpu
, thread
) < 0)
605 if (evsel
->counts
== NULL
&& perf_evsel__alloc_counts(evsel
, cpu
+ 1) < 0)
608 if (readn(FD(evsel
, cpu
, thread
), &count
, nv
* sizeof(u64
)) < 0)
614 else if (count
.run
< count
.ena
)
615 count
.val
= (u64
)((double)count
.val
* count
.ena
/ count
.run
+ 0.5);
617 count
.ena
= count
.run
= 0;
619 evsel
->counts
->cpu
[cpu
] = count
;
623 int __perf_evsel__read(struct perf_evsel
*evsel
,
624 int ncpus
, int nthreads
, bool scale
)
626 size_t nv
= scale
? 3 : 1;
628 struct perf_counts_values
*aggr
= &evsel
->counts
->aggr
, count
;
630 aggr
->val
= aggr
->ena
= aggr
->run
= 0;
632 for (cpu
= 0; cpu
< ncpus
; cpu
++) {
633 for (thread
= 0; thread
< nthreads
; thread
++) {
634 if (FD(evsel
, cpu
, thread
) < 0)
637 if (readn(FD(evsel
, cpu
, thread
),
638 &count
, nv
* sizeof(u64
)) < 0)
641 aggr
->val
+= count
.val
;
643 aggr
->ena
+= count
.ena
;
644 aggr
->run
+= count
.run
;
649 evsel
->counts
->scaled
= 0;
651 if (aggr
->run
== 0) {
652 evsel
->counts
->scaled
= -1;
657 if (aggr
->run
< aggr
->ena
) {
658 evsel
->counts
->scaled
= 1;
659 aggr
->val
= (u64
)((double)aggr
->val
* aggr
->ena
/ aggr
->run
+ 0.5);
662 aggr
->ena
= aggr
->run
= 0;
667 static int get_group_fd(struct perf_evsel
*evsel
, int cpu
, int thread
)
669 struct perf_evsel
*leader
= evsel
->leader
;
676 * Leader must be already processed/open,
681 fd
= FD(leader
, cpu
, thread
);
687 static int __perf_evsel__open(struct perf_evsel
*evsel
, struct cpu_map
*cpus
,
688 struct thread_map
*threads
)
691 unsigned long flags
= 0;
694 if (evsel
->fd
== NULL
&&
695 perf_evsel__alloc_fd(evsel
, cpus
->nr
, threads
->nr
) < 0)
699 flags
= PERF_FLAG_PID_CGROUP
;
700 pid
= evsel
->cgrp
->fd
;
703 for (cpu
= 0; cpu
< cpus
->nr
; cpu
++) {
705 for (thread
= 0; thread
< threads
->nr
; thread
++) {
709 pid
= threads
->map
[thread
];
711 group_fd
= get_group_fd(evsel
, cpu
, thread
);
713 FD(evsel
, cpu
, thread
) = sys_perf_event_open(&evsel
->attr
,
717 if (FD(evsel
, cpu
, thread
) < 0) {
728 while (--thread
>= 0) {
729 close(FD(evsel
, cpu
, thread
));
730 FD(evsel
, cpu
, thread
) = -1;
732 thread
= threads
->nr
;
733 } while (--cpu
>= 0);
737 void perf_evsel__close(struct perf_evsel
*evsel
, int ncpus
, int nthreads
)
739 if (evsel
->fd
== NULL
)
742 perf_evsel__close_fd(evsel
, ncpus
, nthreads
);
743 perf_evsel__free_fd(evsel
);
756 struct thread_map map
;
758 } empty_thread_map
= {
763 int perf_evsel__open(struct perf_evsel
*evsel
, struct cpu_map
*cpus
,
764 struct thread_map
*threads
)
767 /* Work around old compiler warnings about strict aliasing */
768 cpus
= &empty_cpu_map
.map
;
772 threads
= &empty_thread_map
.map
;
774 return __perf_evsel__open(evsel
, cpus
, threads
);
777 int perf_evsel__open_per_cpu(struct perf_evsel
*evsel
,
778 struct cpu_map
*cpus
)
780 return __perf_evsel__open(evsel
, cpus
, &empty_thread_map
.map
);
783 int perf_evsel__open_per_thread(struct perf_evsel
*evsel
,
784 struct thread_map
*threads
)
786 return __perf_evsel__open(evsel
, &empty_cpu_map
.map
, threads
);
789 static int perf_evsel__parse_id_sample(const struct perf_evsel
*evsel
,
790 const union perf_event
*event
,
791 struct perf_sample
*sample
)
793 u64 type
= evsel
->attr
.sample_type
;
794 const u64
*array
= event
->sample
.array
;
795 bool swapped
= evsel
->needs_swap
;
798 array
+= ((event
->header
.size
-
799 sizeof(event
->header
)) / sizeof(u64
)) - 1;
801 if (type
& PERF_SAMPLE_CPU
) {
804 /* undo swap of u64, then swap on individual u32s */
805 u
.val64
= bswap_64(u
.val64
);
806 u
.val32
[0] = bswap_32(u
.val32
[0]);
809 sample
->cpu
= u
.val32
[0];
813 if (type
& PERF_SAMPLE_STREAM_ID
) {
814 sample
->stream_id
= *array
;
818 if (type
& PERF_SAMPLE_ID
) {
823 if (type
& PERF_SAMPLE_TIME
) {
824 sample
->time
= *array
;
828 if (type
& PERF_SAMPLE_TID
) {
831 /* undo swap of u64, then swap on individual u32s */
832 u
.val64
= bswap_64(u
.val64
);
833 u
.val32
[0] = bswap_32(u
.val32
[0]);
834 u
.val32
[1] = bswap_32(u
.val32
[1]);
837 sample
->pid
= u
.val32
[0];
838 sample
->tid
= u
.val32
[1];
844 static bool sample_overlap(const union perf_event
*event
,
845 const void *offset
, u64 size
)
847 const void *base
= event
;
849 if (offset
+ size
> base
+ event
->header
.size
)
855 int perf_evsel__parse_sample(struct perf_evsel
*evsel
, union perf_event
*event
,
856 struct perf_sample
*data
)
858 u64 type
= evsel
->attr
.sample_type
;
859 u64 regs_user
= evsel
->attr
.sample_regs_user
;
860 bool swapped
= evsel
->needs_swap
;
864 * used for cross-endian analysis. See git commit 65014ab3
865 * for why this goofiness is needed.
869 memset(data
, 0, sizeof(*data
));
870 data
->cpu
= data
->pid
= data
->tid
= -1;
871 data
->stream_id
= data
->id
= data
->time
= -1ULL;
874 if (event
->header
.type
!= PERF_RECORD_SAMPLE
) {
875 if (!evsel
->attr
.sample_id_all
)
877 return perf_evsel__parse_id_sample(evsel
, event
, data
);
880 array
= event
->sample
.array
;
882 if (evsel
->sample_size
+ sizeof(event
->header
) > event
->header
.size
)
885 if (type
& PERF_SAMPLE_IP
) {
886 data
->ip
= event
->ip
.ip
;
890 if (type
& PERF_SAMPLE_TID
) {
893 /* undo swap of u64, then swap on individual u32s */
894 u
.val64
= bswap_64(u
.val64
);
895 u
.val32
[0] = bswap_32(u
.val32
[0]);
896 u
.val32
[1] = bswap_32(u
.val32
[1]);
899 data
->pid
= u
.val32
[0];
900 data
->tid
= u
.val32
[1];
904 if (type
& PERF_SAMPLE_TIME
) {
910 if (type
& PERF_SAMPLE_ADDR
) {
916 if (type
& PERF_SAMPLE_ID
) {
921 if (type
& PERF_SAMPLE_STREAM_ID
) {
922 data
->stream_id
= *array
;
926 if (type
& PERF_SAMPLE_CPU
) {
930 /* undo swap of u64, then swap on individual u32s */
931 u
.val64
= bswap_64(u
.val64
);
932 u
.val32
[0] = bswap_32(u
.val32
[0]);
935 data
->cpu
= u
.val32
[0];
939 if (type
& PERF_SAMPLE_PERIOD
) {
940 data
->period
= *array
;
944 if (type
& PERF_SAMPLE_READ
) {
945 fprintf(stderr
, "PERF_SAMPLE_READ is unsupported for now\n");
949 if (type
& PERF_SAMPLE_CALLCHAIN
) {
950 if (sample_overlap(event
, array
, sizeof(data
->callchain
->nr
)))
953 data
->callchain
= (struct ip_callchain
*)array
;
955 if (sample_overlap(event
, array
, data
->callchain
->nr
))
958 array
+= 1 + data
->callchain
->nr
;
961 if (type
& PERF_SAMPLE_RAW
) {
965 if (WARN_ONCE(swapped
,
966 "Endianness of raw data not corrected!\n")) {
967 /* undo swap of u64, then swap on individual u32s */
968 u
.val64
= bswap_64(u
.val64
);
969 u
.val32
[0] = bswap_32(u
.val32
[0]);
970 u
.val32
[1] = bswap_32(u
.val32
[1]);
973 if (sample_overlap(event
, array
, sizeof(u32
)))
976 data
->raw_size
= u
.val32
[0];
977 pdata
= (void *) array
+ sizeof(u32
);
979 if (sample_overlap(event
, pdata
, data
->raw_size
))
982 data
->raw_data
= (void *) pdata
;
984 array
= (void *)array
+ data
->raw_size
+ sizeof(u32
);
987 if (type
& PERF_SAMPLE_BRANCH_STACK
) {
990 data
->branch_stack
= (struct branch_stack
*)array
;
993 sz
= data
->branch_stack
->nr
* sizeof(struct branch_entry
);
998 if (type
& PERF_SAMPLE_REGS_USER
) {
999 /* First u64 tells us if we have any regs in sample. */
1000 u64 avail
= *array
++;
1003 data
->user_regs
.regs
= (u64
*)array
;
1004 array
+= hweight_long(regs_user
);
1008 if (type
& PERF_SAMPLE_STACK_USER
) {
1009 u64 size
= *array
++;
1011 data
->user_stack
.offset
= ((char *)(array
- 1)
1015 data
->user_stack
.size
= 0;
1017 data
->user_stack
.data
= (char *)array
;
1018 array
+= size
/ sizeof(*array
);
1019 data
->user_stack
.size
= *array
;
1026 int perf_event__synthesize_sample(union perf_event
*event
, u64 type
,
1027 const struct perf_sample
*sample
,
1033 * used for cross-endian analysis. See git commit 65014ab3
1034 * for why this goofiness is needed.
1038 array
= event
->sample
.array
;
1040 if (type
& PERF_SAMPLE_IP
) {
1041 event
->ip
.ip
= sample
->ip
;
1045 if (type
& PERF_SAMPLE_TID
) {
1046 u
.val32
[0] = sample
->pid
;
1047 u
.val32
[1] = sample
->tid
;
1050 * Inverse of what is done in perf_evsel__parse_sample
1052 u
.val32
[0] = bswap_32(u
.val32
[0]);
1053 u
.val32
[1] = bswap_32(u
.val32
[1]);
1054 u
.val64
= bswap_64(u
.val64
);
1061 if (type
& PERF_SAMPLE_TIME
) {
1062 *array
= sample
->time
;
1066 if (type
& PERF_SAMPLE_ADDR
) {
1067 *array
= sample
->addr
;
1071 if (type
& PERF_SAMPLE_ID
) {
1072 *array
= sample
->id
;
1076 if (type
& PERF_SAMPLE_STREAM_ID
) {
1077 *array
= sample
->stream_id
;
1081 if (type
& PERF_SAMPLE_CPU
) {
1082 u
.val32
[0] = sample
->cpu
;
1085 * Inverse of what is done in perf_evsel__parse_sample
1087 u
.val32
[0] = bswap_32(u
.val32
[0]);
1088 u
.val64
= bswap_64(u
.val64
);
1094 if (type
& PERF_SAMPLE_PERIOD
) {
1095 *array
= sample
->period
;
1102 struct format_field
*perf_evsel__field(struct perf_evsel
*evsel
, const char *name
)
1104 return pevent_find_field(evsel
->tp_format
, name
);
1107 void *perf_evsel__rawptr(struct perf_evsel
*evsel
, struct perf_sample
*sample
,
1110 struct format_field
*field
= perf_evsel__field(evsel
, name
);
1116 offset
= field
->offset
;
1118 if (field
->flags
& FIELD_IS_DYNAMIC
) {
1119 offset
= *(int *)(sample
->raw_data
+ field
->offset
);
1123 return sample
->raw_data
+ offset
;
1126 u64
perf_evsel__intval(struct perf_evsel
*evsel
, struct perf_sample
*sample
,
1129 struct format_field
*field
= perf_evsel__field(evsel
, name
);
1136 ptr
= sample
->raw_data
+ field
->offset
;
1138 switch (field
->size
) {
1142 value
= *(u16
*)ptr
;
1145 value
= *(u32
*)ptr
;
1148 value
= *(u64
*)ptr
;
1154 if (!evsel
->needs_swap
)
1157 switch (field
->size
) {
1159 return bswap_16(value
);
1161 return bswap_32(value
);
1163 return bswap_64(value
);