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)
14 #include "thread_map.h"
16 #define FD(e, x, y) (*(int *)xyarray__entry(e->fd, x, y))
18 int __perf_evsel__sample_size(u64 sample_type
)
20 u64 mask
= sample_type
& PERF_SAMPLE_MASK
;
24 for (i
= 0; i
< 64; i
++) {
25 if (mask
& (1ULL << i
))
34 void perf_evsel__init(struct perf_evsel
*evsel
,
35 struct perf_event_attr
*attr
, int idx
)
39 INIT_LIST_HEAD(&evsel
->node
);
42 struct perf_evsel
*perf_evsel__new(struct perf_event_attr
*attr
, int idx
)
44 struct perf_evsel
*evsel
= zalloc(sizeof(*evsel
));
47 perf_evsel__init(evsel
, attr
, idx
);
52 int perf_evsel__alloc_fd(struct perf_evsel
*evsel
, int ncpus
, int nthreads
)
55 evsel
->fd
= xyarray__new(ncpus
, nthreads
, sizeof(int));
58 for (cpu
= 0; cpu
< ncpus
; cpu
++) {
59 for (thread
= 0; thread
< nthreads
; thread
++) {
60 FD(evsel
, cpu
, thread
) = -1;
65 return evsel
->fd
!= NULL
? 0 : -ENOMEM
;
68 int perf_evsel__alloc_id(struct perf_evsel
*evsel
, int ncpus
, int nthreads
)
70 evsel
->sample_id
= xyarray__new(ncpus
, nthreads
, sizeof(struct perf_sample_id
));
71 if (evsel
->sample_id
== NULL
)
74 evsel
->id
= zalloc(ncpus
* nthreads
* sizeof(u64
));
75 if (evsel
->id
== NULL
) {
76 xyarray__delete(evsel
->sample_id
);
77 evsel
->sample_id
= NULL
;
84 int perf_evsel__alloc_counts(struct perf_evsel
*evsel
, int ncpus
)
86 evsel
->counts
= zalloc((sizeof(*evsel
->counts
) +
87 (ncpus
* sizeof(struct perf_counts_values
))));
88 return evsel
->counts
!= NULL
? 0 : -ENOMEM
;
91 void perf_evsel__free_fd(struct perf_evsel
*evsel
)
93 xyarray__delete(evsel
->fd
);
97 void perf_evsel__free_id(struct perf_evsel
*evsel
)
99 xyarray__delete(evsel
->sample_id
);
100 evsel
->sample_id
= NULL
;
105 void perf_evsel__close_fd(struct perf_evsel
*evsel
, int ncpus
, int nthreads
)
109 for (cpu
= 0; cpu
< ncpus
; cpu
++)
110 for (thread
= 0; thread
< nthreads
; ++thread
) {
111 close(FD(evsel
, cpu
, thread
));
112 FD(evsel
, cpu
, thread
) = -1;
116 void perf_evsel__exit(struct perf_evsel
*evsel
)
118 assert(list_empty(&evsel
->node
));
119 xyarray__delete(evsel
->fd
);
120 xyarray__delete(evsel
->sample_id
);
124 void perf_evsel__delete(struct perf_evsel
*evsel
)
126 perf_evsel__exit(evsel
);
127 close_cgroup(evsel
->cgrp
);
132 int __perf_evsel__read_on_cpu(struct perf_evsel
*evsel
,
133 int cpu
, int thread
, bool scale
)
135 struct perf_counts_values count
;
136 size_t nv
= scale
? 3 : 1;
138 if (FD(evsel
, cpu
, thread
) < 0)
141 if (evsel
->counts
== NULL
&& perf_evsel__alloc_counts(evsel
, cpu
+ 1) < 0)
144 if (readn(FD(evsel
, cpu
, thread
), &count
, nv
* sizeof(u64
)) < 0)
150 else if (count
.run
< count
.ena
)
151 count
.val
= (u64
)((double)count
.val
* count
.ena
/ count
.run
+ 0.5);
153 count
.ena
= count
.run
= 0;
155 evsel
->counts
->cpu
[cpu
] = count
;
159 int __perf_evsel__read(struct perf_evsel
*evsel
,
160 int ncpus
, int nthreads
, bool scale
)
162 size_t nv
= scale
? 3 : 1;
164 struct perf_counts_values
*aggr
= &evsel
->counts
->aggr
, count
;
166 aggr
->val
= aggr
->ena
= aggr
->run
= 0;
168 for (cpu
= 0; cpu
< ncpus
; cpu
++) {
169 for (thread
= 0; thread
< nthreads
; thread
++) {
170 if (FD(evsel
, cpu
, thread
) < 0)
173 if (readn(FD(evsel
, cpu
, thread
),
174 &count
, nv
* sizeof(u64
)) < 0)
177 aggr
->val
+= count
.val
;
179 aggr
->ena
+= count
.ena
;
180 aggr
->run
+= count
.run
;
185 evsel
->counts
->scaled
= 0;
187 if (aggr
->run
== 0) {
188 evsel
->counts
->scaled
= -1;
193 if (aggr
->run
< aggr
->ena
) {
194 evsel
->counts
->scaled
= 1;
195 aggr
->val
= (u64
)((double)aggr
->val
* aggr
->ena
/ aggr
->run
+ 0.5);
198 aggr
->ena
= aggr
->run
= 0;
203 static int __perf_evsel__open(struct perf_evsel
*evsel
, struct cpu_map
*cpus
,
204 struct thread_map
*threads
, bool group
)
207 unsigned long flags
= 0;
210 if (evsel
->fd
== NULL
&&
211 perf_evsel__alloc_fd(evsel
, cpus
->nr
, threads
->nr
) < 0)
215 flags
= PERF_FLAG_PID_CGROUP
;
216 pid
= evsel
->cgrp
->fd
;
219 for (cpu
= 0; cpu
< cpus
->nr
; cpu
++) {
222 for (thread
= 0; thread
< threads
->nr
; thread
++) {
225 pid
= threads
->map
[thread
];
227 FD(evsel
, cpu
, thread
) = sys_perf_event_open(&evsel
->attr
,
231 if (FD(evsel
, cpu
, thread
) < 0)
234 if (group
&& group_fd
== -1)
235 group_fd
= FD(evsel
, cpu
, thread
);
243 while (--thread
>= 0) {
244 close(FD(evsel
, cpu
, thread
));
245 FD(evsel
, cpu
, thread
) = -1;
247 thread
= threads
->nr
;
248 } while (--cpu
>= 0);
261 struct thread_map map
;
263 } empty_thread_map
= {
268 int perf_evsel__open(struct perf_evsel
*evsel
, struct cpu_map
*cpus
,
269 struct thread_map
*threads
, bool group
)
272 /* Work around old compiler warnings about strict aliasing */
273 cpus
= &empty_cpu_map
.map
;
277 threads
= &empty_thread_map
.map
;
279 return __perf_evsel__open(evsel
, cpus
, threads
, group
);
282 int perf_evsel__open_per_cpu(struct perf_evsel
*evsel
,
283 struct cpu_map
*cpus
, bool group
)
285 return __perf_evsel__open(evsel
, cpus
, &empty_thread_map
.map
, group
);
288 int perf_evsel__open_per_thread(struct perf_evsel
*evsel
,
289 struct thread_map
*threads
, bool group
)
291 return __perf_evsel__open(evsel
, &empty_cpu_map
.map
, threads
, group
);
294 static int perf_event__parse_id_sample(const union perf_event
*event
, u64 type
,
295 struct perf_sample
*sample
)
297 const u64
*array
= event
->sample
.array
;
299 array
+= ((event
->header
.size
-
300 sizeof(event
->header
)) / sizeof(u64
)) - 1;
302 if (type
& PERF_SAMPLE_CPU
) {
303 u32
*p
= (u32
*)array
;
308 if (type
& PERF_SAMPLE_STREAM_ID
) {
309 sample
->stream_id
= *array
;
313 if (type
& PERF_SAMPLE_ID
) {
318 if (type
& PERF_SAMPLE_TIME
) {
319 sample
->time
= *array
;
323 if (type
& PERF_SAMPLE_TID
) {
324 u32
*p
= (u32
*)array
;
332 static bool sample_overlap(const union perf_event
*event
,
333 const void *offset
, u64 size
)
335 const void *base
= event
;
337 if (offset
+ size
> base
+ event
->header
.size
)
343 int perf_event__parse_sample(const union perf_event
*event
, u64 type
,
344 int sample_size
, bool sample_id_all
,
345 struct perf_sample
*data
)
349 data
->cpu
= data
->pid
= data
->tid
= -1;
350 data
->stream_id
= data
->id
= data
->time
= -1ULL;
352 if (event
->header
.type
!= PERF_RECORD_SAMPLE
) {
355 return perf_event__parse_id_sample(event
, type
, data
);
358 array
= event
->sample
.array
;
360 if (sample_size
+ sizeof(event
->header
) > event
->header
.size
)
363 if (type
& PERF_SAMPLE_IP
) {
364 data
->ip
= event
->ip
.ip
;
368 if (type
& PERF_SAMPLE_TID
) {
369 u32
*p
= (u32
*)array
;
375 if (type
& PERF_SAMPLE_TIME
) {
380 if (type
& PERF_SAMPLE_ADDR
) {
386 if (type
& PERF_SAMPLE_ID
) {
391 if (type
& PERF_SAMPLE_STREAM_ID
) {
392 data
->stream_id
= *array
;
396 if (type
& PERF_SAMPLE_CPU
) {
397 u32
*p
= (u32
*)array
;
402 if (type
& PERF_SAMPLE_PERIOD
) {
403 data
->period
= *array
;
407 if (type
& PERF_SAMPLE_READ
) {
408 fprintf(stderr
, "PERF_SAMPLE_READ is unsuported for now\n");
412 if (type
& PERF_SAMPLE_CALLCHAIN
) {
413 if (sample_overlap(event
, array
, sizeof(data
->callchain
->nr
)))
416 data
->callchain
= (struct ip_callchain
*)array
;
418 if (sample_overlap(event
, array
, data
->callchain
->nr
))
421 array
+= 1 + data
->callchain
->nr
;
424 if (type
& PERF_SAMPLE_RAW
) {
425 u32
*p
= (u32
*)array
;
427 if (sample_overlap(event
, array
, sizeof(u32
)))
433 if (sample_overlap(event
, p
, data
->raw_size
))