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)
16 #include "thread_map.h"
18 #include "../../include/linux/perf_event.h"
20 #define FD(e, x, y) (*(int *)xyarray__entry(e->fd, x, y))
21 #define GROUP_FD(group_fd, cpu) (*(int *)xyarray__entry(group_fd, cpu, 0))
23 int __perf_evsel__sample_size(u64 sample_type
)
25 u64 mask
= sample_type
& PERF_SAMPLE_MASK
;
29 for (i
= 0; i
< 64; i
++) {
30 if (mask
& (1ULL << i
))
39 void hists__init(struct hists
*hists
)
41 memset(hists
, 0, sizeof(*hists
));
42 hists
->entries_in_array
[0] = hists
->entries_in_array
[1] = RB_ROOT
;
43 hists
->entries_in
= &hists
->entries_in_array
[0];
44 hists
->entries_collapsed
= RB_ROOT
;
45 hists
->entries
= RB_ROOT
;
46 pthread_mutex_init(&hists
->lock
, NULL
);
49 void perf_evsel__init(struct perf_evsel
*evsel
,
50 struct perf_event_attr
*attr
, int idx
)
54 INIT_LIST_HEAD(&evsel
->node
);
55 hists__init(&evsel
->hists
);
58 struct perf_evsel
*perf_evsel__new(struct perf_event_attr
*attr
, int idx
)
60 struct perf_evsel
*evsel
= zalloc(sizeof(*evsel
));
63 perf_evsel__init(evsel
, attr
, idx
);
68 static const char *perf_evsel__hw_names
[PERF_COUNT_HW_MAX
] = {
76 "stalled-cycles-frontend",
77 "stalled-cycles-backend",
81 const char *__perf_evsel__hw_name(u64 config
)
83 if (config
< PERF_COUNT_HW_MAX
&& perf_evsel__hw_names
[config
])
84 return perf_evsel__hw_names
[config
];
86 return "unknown-hardware";
89 static int perf_evsel__add_modifiers(struct perf_evsel
*evsel
, char *bf
, size_t size
)
92 struct perf_event_attr
*attr
= &evsel
->attr
;
93 bool exclude_guest_default
= false;
95 #define MOD_PRINT(context, mod) do { \
96 if (!attr->exclude_##context) { \
97 if (!colon) colon = ++r; \
98 r += scnprintf(bf + r, size - r, "%c", mod); \
101 if (attr
->exclude_kernel
|| attr
->exclude_user
|| attr
->exclude_hv
) {
102 MOD_PRINT(kernel
, 'k');
103 MOD_PRINT(user
, 'u');
105 exclude_guest_default
= true;
108 if (attr
->precise_ip
) {
111 r
+= scnprintf(bf
+ r
, size
- r
, "%.*s", attr
->precise_ip
, "ppp");
112 exclude_guest_default
= true;
115 if (attr
->exclude_host
|| attr
->exclude_guest
== exclude_guest_default
) {
116 MOD_PRINT(host
, 'H');
117 MOD_PRINT(guest
, 'G');
125 static int perf_evsel__hw_name(struct perf_evsel
*evsel
, char *bf
, size_t size
)
127 int r
= scnprintf(bf
, size
, "%s", __perf_evsel__hw_name(evsel
->attr
.config
));
128 return r
+ perf_evsel__add_modifiers(evsel
, bf
+ r
, size
- r
);
131 int perf_evsel__name(struct perf_evsel
*evsel
, char *bf
, size_t size
)
135 switch (evsel
->attr
.type
) {
137 ret
= scnprintf(bf
, size
, "raw 0x%" PRIx64
, evsel
->attr
.config
);
140 case PERF_TYPE_HARDWARE
:
141 ret
= perf_evsel__hw_name(evsel
, bf
, size
);
147 * This is the minimal perf_evsel__name so that we can
148 * reconstruct event names taking into account event modifiers.
150 * The old event_name uses it now for raw anr hw events, so that
151 * we don't drag all the parsing stuff into the python binding.
153 * On the next devel cycle the rest of the event naming will be
162 void perf_evsel__config(struct perf_evsel
*evsel
, struct perf_record_opts
*opts
,
163 struct perf_evsel
*first
)
165 struct perf_event_attr
*attr
= &evsel
->attr
;
166 int track
= !evsel
->idx
; /* only the first counter needs these */
169 attr
->sample_id_all
= opts
->sample_id_all_missing
? 0 : 1;
170 attr
->inherit
= !opts
->no_inherit
;
171 attr
->read_format
= PERF_FORMAT_TOTAL_TIME_ENABLED
|
172 PERF_FORMAT_TOTAL_TIME_RUNNING
|
175 attr
->sample_type
|= PERF_SAMPLE_IP
| PERF_SAMPLE_TID
;
178 * We default some events to a 1 default interval. But keep
179 * it a weak assumption overridable by the user.
181 if (!attr
->sample_period
|| (opts
->user_freq
!= UINT_MAX
&&
182 opts
->user_interval
!= ULLONG_MAX
)) {
184 attr
->sample_type
|= PERF_SAMPLE_PERIOD
;
186 attr
->sample_freq
= opts
->freq
;
188 attr
->sample_period
= opts
->default_interval
;
192 if (opts
->no_samples
)
193 attr
->sample_freq
= 0;
195 if (opts
->inherit_stat
)
196 attr
->inherit_stat
= 1;
198 if (opts
->sample_address
) {
199 attr
->sample_type
|= PERF_SAMPLE_ADDR
;
200 attr
->mmap_data
= track
;
203 if (opts
->call_graph
)
204 attr
->sample_type
|= PERF_SAMPLE_CALLCHAIN
;
206 if (perf_target__has_cpu(&opts
->target
))
207 attr
->sample_type
|= PERF_SAMPLE_CPU
;
210 attr
->sample_type
|= PERF_SAMPLE_PERIOD
;
212 if (!opts
->sample_id_all_missing
&&
213 (opts
->sample_time
|| !opts
->no_inherit
||
214 perf_target__has_cpu(&opts
->target
)))
215 attr
->sample_type
|= PERF_SAMPLE_TIME
;
217 if (opts
->raw_samples
) {
218 attr
->sample_type
|= PERF_SAMPLE_TIME
;
219 attr
->sample_type
|= PERF_SAMPLE_RAW
;
220 attr
->sample_type
|= PERF_SAMPLE_CPU
;
223 if (opts
->no_delay
) {
225 attr
->wakeup_events
= 1;
227 if (opts
->branch_stack
) {
228 attr
->sample_type
|= PERF_SAMPLE_BRANCH_STACK
;
229 attr
->branch_sample_type
= opts
->branch_stack
;
235 if (perf_target__none(&opts
->target
) &&
236 (!opts
->group
|| evsel
== first
)) {
237 attr
->enable_on_exec
= 1;
241 int perf_evsel__alloc_fd(struct perf_evsel
*evsel
, int ncpus
, int nthreads
)
244 evsel
->fd
= xyarray__new(ncpus
, nthreads
, sizeof(int));
247 for (cpu
= 0; cpu
< ncpus
; cpu
++) {
248 for (thread
= 0; thread
< nthreads
; thread
++) {
249 FD(evsel
, cpu
, thread
) = -1;
254 return evsel
->fd
!= NULL
? 0 : -ENOMEM
;
257 int perf_evsel__alloc_id(struct perf_evsel
*evsel
, int ncpus
, int nthreads
)
259 evsel
->sample_id
= xyarray__new(ncpus
, nthreads
, sizeof(struct perf_sample_id
));
260 if (evsel
->sample_id
== NULL
)
263 evsel
->id
= zalloc(ncpus
* nthreads
* sizeof(u64
));
264 if (evsel
->id
== NULL
) {
265 xyarray__delete(evsel
->sample_id
);
266 evsel
->sample_id
= NULL
;
273 int perf_evsel__alloc_counts(struct perf_evsel
*evsel
, int ncpus
)
275 evsel
->counts
= zalloc((sizeof(*evsel
->counts
) +
276 (ncpus
* sizeof(struct perf_counts_values
))));
277 return evsel
->counts
!= NULL
? 0 : -ENOMEM
;
280 void perf_evsel__free_fd(struct perf_evsel
*evsel
)
282 xyarray__delete(evsel
->fd
);
286 void perf_evsel__free_id(struct perf_evsel
*evsel
)
288 xyarray__delete(evsel
->sample_id
);
289 evsel
->sample_id
= NULL
;
294 void perf_evsel__close_fd(struct perf_evsel
*evsel
, int ncpus
, int nthreads
)
298 for (cpu
= 0; cpu
< ncpus
; cpu
++)
299 for (thread
= 0; thread
< nthreads
; ++thread
) {
300 close(FD(evsel
, cpu
, thread
));
301 FD(evsel
, cpu
, thread
) = -1;
305 void perf_evsel__exit(struct perf_evsel
*evsel
)
307 assert(list_empty(&evsel
->node
));
308 xyarray__delete(evsel
->fd
);
309 xyarray__delete(evsel
->sample_id
);
313 void perf_evsel__delete(struct perf_evsel
*evsel
)
315 perf_evsel__exit(evsel
);
316 close_cgroup(evsel
->cgrp
);
321 int __perf_evsel__read_on_cpu(struct perf_evsel
*evsel
,
322 int cpu
, int thread
, bool scale
)
324 struct perf_counts_values count
;
325 size_t nv
= scale
? 3 : 1;
327 if (FD(evsel
, cpu
, thread
) < 0)
330 if (evsel
->counts
== NULL
&& perf_evsel__alloc_counts(evsel
, cpu
+ 1) < 0)
333 if (readn(FD(evsel
, cpu
, thread
), &count
, nv
* sizeof(u64
)) < 0)
339 else if (count
.run
< count
.ena
)
340 count
.val
= (u64
)((double)count
.val
* count
.ena
/ count
.run
+ 0.5);
342 count
.ena
= count
.run
= 0;
344 evsel
->counts
->cpu
[cpu
] = count
;
348 int __perf_evsel__read(struct perf_evsel
*evsel
,
349 int ncpus
, int nthreads
, bool scale
)
351 size_t nv
= scale
? 3 : 1;
353 struct perf_counts_values
*aggr
= &evsel
->counts
->aggr
, count
;
355 aggr
->val
= aggr
->ena
= aggr
->run
= 0;
357 for (cpu
= 0; cpu
< ncpus
; cpu
++) {
358 for (thread
= 0; thread
< nthreads
; thread
++) {
359 if (FD(evsel
, cpu
, thread
) < 0)
362 if (readn(FD(evsel
, cpu
, thread
),
363 &count
, nv
* sizeof(u64
)) < 0)
366 aggr
->val
+= count
.val
;
368 aggr
->ena
+= count
.ena
;
369 aggr
->run
+= count
.run
;
374 evsel
->counts
->scaled
= 0;
376 if (aggr
->run
== 0) {
377 evsel
->counts
->scaled
= -1;
382 if (aggr
->run
< aggr
->ena
) {
383 evsel
->counts
->scaled
= 1;
384 aggr
->val
= (u64
)((double)aggr
->val
* aggr
->ena
/ aggr
->run
+ 0.5);
387 aggr
->ena
= aggr
->run
= 0;
392 static int __perf_evsel__open(struct perf_evsel
*evsel
, struct cpu_map
*cpus
,
393 struct thread_map
*threads
, bool group
,
394 struct xyarray
*group_fds
)
397 unsigned long flags
= 0;
400 if (evsel
->fd
== NULL
&&
401 perf_evsel__alloc_fd(evsel
, cpus
->nr
, threads
->nr
) < 0)
405 flags
= PERF_FLAG_PID_CGROUP
;
406 pid
= evsel
->cgrp
->fd
;
409 for (cpu
= 0; cpu
< cpus
->nr
; cpu
++) {
410 int group_fd
= group_fds
? GROUP_FD(group_fds
, cpu
) : -1;
412 for (thread
= 0; thread
< threads
->nr
; thread
++) {
415 pid
= threads
->map
[thread
];
417 FD(evsel
, cpu
, thread
) = sys_perf_event_open(&evsel
->attr
,
421 if (FD(evsel
, cpu
, thread
) < 0) {
426 if (group
&& group_fd
== -1)
427 group_fd
= FD(evsel
, cpu
, thread
);
435 while (--thread
>= 0) {
436 close(FD(evsel
, cpu
, thread
));
437 FD(evsel
, cpu
, thread
) = -1;
439 thread
= threads
->nr
;
440 } while (--cpu
>= 0);
444 void perf_evsel__close(struct perf_evsel
*evsel
, int ncpus
, int nthreads
)
446 if (evsel
->fd
== NULL
)
449 perf_evsel__close_fd(evsel
, ncpus
, nthreads
);
450 perf_evsel__free_fd(evsel
);
463 struct thread_map map
;
465 } empty_thread_map
= {
470 int perf_evsel__open(struct perf_evsel
*evsel
, struct cpu_map
*cpus
,
471 struct thread_map
*threads
, bool group
,
472 struct xyarray
*group_fd
)
475 /* Work around old compiler warnings about strict aliasing */
476 cpus
= &empty_cpu_map
.map
;
480 threads
= &empty_thread_map
.map
;
482 return __perf_evsel__open(evsel
, cpus
, threads
, group
, group_fd
);
485 int perf_evsel__open_per_cpu(struct perf_evsel
*evsel
,
486 struct cpu_map
*cpus
, bool group
,
487 struct xyarray
*group_fd
)
489 return __perf_evsel__open(evsel
, cpus
, &empty_thread_map
.map
, group
,
493 int perf_evsel__open_per_thread(struct perf_evsel
*evsel
,
494 struct thread_map
*threads
, bool group
,
495 struct xyarray
*group_fd
)
497 return __perf_evsel__open(evsel
, &empty_cpu_map
.map
, threads
, group
,
501 static int perf_event__parse_id_sample(const union perf_event
*event
, u64 type
,
502 struct perf_sample
*sample
,
505 const u64
*array
= event
->sample
.array
;
508 array
+= ((event
->header
.size
-
509 sizeof(event
->header
)) / sizeof(u64
)) - 1;
511 if (type
& PERF_SAMPLE_CPU
) {
514 /* undo swap of u64, then swap on individual u32s */
515 u
.val64
= bswap_64(u
.val64
);
516 u
.val32
[0] = bswap_32(u
.val32
[0]);
519 sample
->cpu
= u
.val32
[0];
523 if (type
& PERF_SAMPLE_STREAM_ID
) {
524 sample
->stream_id
= *array
;
528 if (type
& PERF_SAMPLE_ID
) {
533 if (type
& PERF_SAMPLE_TIME
) {
534 sample
->time
= *array
;
538 if (type
& PERF_SAMPLE_TID
) {
541 /* undo swap of u64, then swap on individual u32s */
542 u
.val64
= bswap_64(u
.val64
);
543 u
.val32
[0] = bswap_32(u
.val32
[0]);
544 u
.val32
[1] = bswap_32(u
.val32
[1]);
547 sample
->pid
= u
.val32
[0];
548 sample
->tid
= u
.val32
[1];
554 static bool sample_overlap(const union perf_event
*event
,
555 const void *offset
, u64 size
)
557 const void *base
= event
;
559 if (offset
+ size
> base
+ event
->header
.size
)
565 int perf_event__parse_sample(const union perf_event
*event
, u64 type
,
566 int sample_size
, bool sample_id_all
,
567 struct perf_sample
*data
, bool swapped
)
572 * used for cross-endian analysis. See git commit 65014ab3
573 * for why this goofiness is needed.
577 memset(data
, 0, sizeof(*data
));
578 data
->cpu
= data
->pid
= data
->tid
= -1;
579 data
->stream_id
= data
->id
= data
->time
= -1ULL;
582 if (event
->header
.type
!= PERF_RECORD_SAMPLE
) {
585 return perf_event__parse_id_sample(event
, type
, data
, swapped
);
588 array
= event
->sample
.array
;
590 if (sample_size
+ sizeof(event
->header
) > event
->header
.size
)
593 if (type
& PERF_SAMPLE_IP
) {
594 data
->ip
= event
->ip
.ip
;
598 if (type
& PERF_SAMPLE_TID
) {
601 /* undo swap of u64, then swap on individual u32s */
602 u
.val64
= bswap_64(u
.val64
);
603 u
.val32
[0] = bswap_32(u
.val32
[0]);
604 u
.val32
[1] = bswap_32(u
.val32
[1]);
607 data
->pid
= u
.val32
[0];
608 data
->tid
= u
.val32
[1];
612 if (type
& PERF_SAMPLE_TIME
) {
618 if (type
& PERF_SAMPLE_ADDR
) {
624 if (type
& PERF_SAMPLE_ID
) {
629 if (type
& PERF_SAMPLE_STREAM_ID
) {
630 data
->stream_id
= *array
;
634 if (type
& PERF_SAMPLE_CPU
) {
638 /* undo swap of u64, then swap on individual u32s */
639 u
.val64
= bswap_64(u
.val64
);
640 u
.val32
[0] = bswap_32(u
.val32
[0]);
643 data
->cpu
= u
.val32
[0];
647 if (type
& PERF_SAMPLE_PERIOD
) {
648 data
->period
= *array
;
652 if (type
& PERF_SAMPLE_READ
) {
653 fprintf(stderr
, "PERF_SAMPLE_READ is unsupported for now\n");
657 if (type
& PERF_SAMPLE_CALLCHAIN
) {
658 if (sample_overlap(event
, array
, sizeof(data
->callchain
->nr
)))
661 data
->callchain
= (struct ip_callchain
*)array
;
663 if (sample_overlap(event
, array
, data
->callchain
->nr
))
666 array
+= 1 + data
->callchain
->nr
;
669 if (type
& PERF_SAMPLE_RAW
) {
673 if (WARN_ONCE(swapped
,
674 "Endianness of raw data not corrected!\n")) {
675 /* undo swap of u64, then swap on individual u32s */
676 u
.val64
= bswap_64(u
.val64
);
677 u
.val32
[0] = bswap_32(u
.val32
[0]);
678 u
.val32
[1] = bswap_32(u
.val32
[1]);
681 if (sample_overlap(event
, array
, sizeof(u32
)))
684 data
->raw_size
= u
.val32
[0];
685 pdata
= (void *) array
+ sizeof(u32
);
687 if (sample_overlap(event
, pdata
, data
->raw_size
))
690 data
->raw_data
= (void *) pdata
;
692 array
= (void *)array
+ data
->raw_size
+ sizeof(u32
);
695 if (type
& PERF_SAMPLE_BRANCH_STACK
) {
698 data
->branch_stack
= (struct branch_stack
*)array
;
701 sz
= data
->branch_stack
->nr
* sizeof(struct branch_entry
);
708 int perf_event__synthesize_sample(union perf_event
*event
, u64 type
,
709 const struct perf_sample
*sample
,
715 * used for cross-endian analysis. See git commit 65014ab3
716 * for why this goofiness is needed.
720 array
= event
->sample
.array
;
722 if (type
& PERF_SAMPLE_IP
) {
723 event
->ip
.ip
= sample
->ip
;
727 if (type
& PERF_SAMPLE_TID
) {
728 u
.val32
[0] = sample
->pid
;
729 u
.val32
[1] = sample
->tid
;
732 * Inverse of what is done in perf_event__parse_sample
734 u
.val32
[0] = bswap_32(u
.val32
[0]);
735 u
.val32
[1] = bswap_32(u
.val32
[1]);
736 u
.val64
= bswap_64(u
.val64
);
743 if (type
& PERF_SAMPLE_TIME
) {
744 *array
= sample
->time
;
748 if (type
& PERF_SAMPLE_ADDR
) {
749 *array
= sample
->addr
;
753 if (type
& PERF_SAMPLE_ID
) {
758 if (type
& PERF_SAMPLE_STREAM_ID
) {
759 *array
= sample
->stream_id
;
763 if (type
& PERF_SAMPLE_CPU
) {
764 u
.val32
[0] = sample
->cpu
;
767 * Inverse of what is done in perf_event__parse_sample
769 u
.val32
[0] = bswap_32(u
.val32
[0]);
770 u
.val64
= bswap_64(u
.val64
);
776 if (type
& PERF_SAMPLE_PERIOD
) {
777 *array
= sample
->period
;