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 void perf_evsel__init(struct perf_evsel *evsel,
19 struct perf_event_attr *attr, int idx)
23 INIT_LIST_HEAD(&evsel->node);
26 struct perf_evsel *perf_evsel__new(struct perf_event_attr *attr, int idx)
28 struct perf_evsel *evsel = zalloc(sizeof(*evsel));
31 perf_evsel__init(evsel, attr, idx);
36 int perf_evsel__alloc_fd(struct perf_evsel *evsel, int ncpus, int nthreads)
39 evsel->fd = xyarray__new(ncpus, nthreads, sizeof(int));
42 for (cpu = 0; cpu < ncpus; cpu++) {
43 for (thread = 0; thread < nthreads; thread++) {
44 FD(evsel, cpu, thread) = -1;
49 return evsel->fd != NULL ? 0 : -ENOMEM;
52 int perf_evsel__alloc_id(struct perf_evsel *evsel, int ncpus, int nthreads)
54 evsel->sample_id = xyarray__new(ncpus, nthreads, sizeof(struct perf_sample_id));
55 if (evsel->sample_id == NULL)
58 evsel->id = zalloc(ncpus * nthreads * sizeof(u64));
59 if (evsel->id == NULL) {
60 xyarray__delete(evsel->sample_id);
61 evsel->sample_id = NULL;
68 int perf_evsel__alloc_counts(struct perf_evsel *evsel, int ncpus)
70 evsel->counts = zalloc((sizeof(*evsel->counts) +
71 (ncpus * sizeof(struct perf_counts_values))));
72 return evsel->counts != NULL ? 0 : -ENOMEM;
75 void perf_evsel__free_fd(struct perf_evsel *evsel)
77 xyarray__delete(evsel->fd);
81 void perf_evsel__free_id(struct perf_evsel *evsel)
83 xyarray__delete(evsel->sample_id);
84 evsel->sample_id = NULL;
89 void perf_evsel__close_fd(struct perf_evsel *evsel, int ncpus, int nthreads)
93 for (cpu = 0; cpu < ncpus; cpu++)
94 for (thread = 0; thread < nthreads; ++thread) {
95 close(FD(evsel, cpu, thread));
96 FD(evsel, cpu, thread) = -1;
100 void perf_evsel__exit(struct perf_evsel *evsel)
102 assert(list_empty(&evsel->node));
103 xyarray__delete(evsel->fd);
104 xyarray__delete(evsel->sample_id);
108 void perf_evsel__delete(struct perf_evsel *evsel)
110 perf_evsel__exit(evsel);
111 close_cgroup(evsel->cgrp);
116 int __perf_evsel__read_on_cpu(struct perf_evsel *evsel,
117 int cpu, int thread, bool scale)
119 struct perf_counts_values count;
120 size_t nv = scale ? 3 : 1;
122 if (FD(evsel, cpu, thread) < 0)
125 if (evsel->counts == NULL && perf_evsel__alloc_counts(evsel, cpu + 1) < 0)
128 if (readn(FD(evsel, cpu, thread), &count, nv * sizeof(u64)) < 0)
134 else if (count.run < count.ena)
135 count.val = (u64)((double)count.val * count.ena / count.run + 0.5);
137 count.ena = count.run = 0;
139 evsel->counts->cpu[cpu] = count;
143 int __perf_evsel__read(struct perf_evsel *evsel,
144 int ncpus, int nthreads, bool scale)
146 size_t nv = scale ? 3 : 1;
148 struct perf_counts_values *aggr = &evsel->counts->aggr, count;
150 aggr->val = aggr->ena = aggr->run = 0;
152 for (cpu = 0; cpu < ncpus; cpu++) {
153 for (thread = 0; thread < nthreads; thread++) {
154 if (FD(evsel, cpu, thread) < 0)
157 if (readn(FD(evsel, cpu, thread),
158 &count, nv * sizeof(u64)) < 0)
161 aggr->val += count.val;
163 aggr->ena += count.ena;
164 aggr->run += count.run;
169 evsel->counts->scaled = 0;
171 if (aggr->run == 0) {
172 evsel->counts->scaled = -1;
177 if (aggr->run < aggr->ena) {
178 evsel->counts->scaled = 1;
179 aggr->val = (u64)((double)aggr->val * aggr->ena / aggr->run + 0.5);
182 aggr->ena = aggr->run = 0;
187 static int __perf_evsel__open(struct perf_evsel *evsel, struct cpu_map *cpus,
188 struct thread_map *threads, bool group)
191 unsigned long flags = 0;
194 if (evsel->fd == NULL &&
195 perf_evsel__alloc_fd(evsel, cpus->nr, threads->nr) < 0)
199 flags = PERF_FLAG_PID_CGROUP;
200 pid = evsel->cgrp->fd;
203 for (cpu = 0; cpu < cpus->nr; cpu++) {
206 for (thread = 0; thread < threads->nr; thread++) {
209 pid = threads->map[thread];
211 FD(evsel, cpu, thread) = sys_perf_event_open(&evsel->attr,
215 if (FD(evsel, cpu, thread) < 0)
218 if (group && group_fd == -1)
219 group_fd = FD(evsel, cpu, thread);
227 while (--thread >= 0) {
228 close(FD(evsel, cpu, thread));
229 FD(evsel, cpu, thread) = -1;
231 thread = threads->nr;
232 } while (--cpu >= 0);
245 struct thread_map map;
247 } empty_thread_map = {
252 int perf_evsel__open(struct perf_evsel *evsel, struct cpu_map *cpus,
253 struct thread_map *threads, bool group)
256 /* Work around old compiler warnings about strict aliasing */
257 cpus = &empty_cpu_map.map;
261 threads = &empty_thread_map.map;
263 return __perf_evsel__open(evsel, cpus, threads, group);
266 int perf_evsel__open_per_cpu(struct perf_evsel *evsel,
267 struct cpu_map *cpus, bool group)
269 return __perf_evsel__open(evsel, cpus, &empty_thread_map.map, group);
272 int perf_evsel__open_per_thread(struct perf_evsel *evsel,
273 struct thread_map *threads, bool group)
275 return __perf_evsel__open(evsel, &empty_cpu_map.map, threads, group);
278 static int perf_event__parse_id_sample(const union perf_event *event, u64 type,
279 struct perf_sample *sample)
281 const u64 *array = event->sample.array;
283 array += ((event->header.size -
284 sizeof(event->header)) / sizeof(u64)) - 1;
286 if (type & PERF_SAMPLE_CPU) {
287 u32 *p = (u32 *)array;
292 if (type & PERF_SAMPLE_STREAM_ID) {
293 sample->stream_id = *array;
297 if (type & PERF_SAMPLE_ID) {
302 if (type & PERF_SAMPLE_TIME) {
303 sample->time = *array;
307 if (type & PERF_SAMPLE_TID) {
308 u32 *p = (u32 *)array;
316 static bool sample_overlap(const union perf_event *event,
317 const void *offset, u64 size)
319 const void *base = event;
321 if (offset + size > base + event->header.size)
327 int perf_event__parse_sample(const union perf_event *event, u64 type,
328 int sample_size, bool sample_id_all,
329 struct perf_sample *data)
333 data->cpu = data->pid = data->tid = -1;
334 data->stream_id = data->id = data->time = -1ULL;
336 if (event->header.type != PERF_RECORD_SAMPLE) {
339 return perf_event__parse_id_sample(event, type, data);
342 array = event->sample.array;
344 if (sample_size + sizeof(event->header) > event->header.size)
347 if (type & PERF_SAMPLE_IP) {
348 data->ip = event->ip.ip;
352 if (type & PERF_SAMPLE_TID) {
353 u32 *p = (u32 *)array;
359 if (type & PERF_SAMPLE_TIME) {
364 if (type & PERF_SAMPLE_ADDR) {
370 if (type & PERF_SAMPLE_ID) {
375 if (type & PERF_SAMPLE_STREAM_ID) {
376 data->stream_id = *array;
380 if (type & PERF_SAMPLE_CPU) {
381 u32 *p = (u32 *)array;
386 if (type & PERF_SAMPLE_PERIOD) {
387 data->period = *array;
391 if (type & PERF_SAMPLE_READ) {
392 fprintf(stderr, "PERF_SAMPLE_READ is unsuported for now\n");
396 if (type & PERF_SAMPLE_CALLCHAIN) {
397 if (sample_overlap(event, array, sizeof(data->callchain->nr)))
400 data->callchain = (struct ip_callchain *)array;
402 if (sample_overlap(event, array, data->callchain->nr))
405 array += 1 + data->callchain->nr;
408 if (type & PERF_SAMPLE_RAW) {
409 u32 *p = (u32 *)array;
411 if (sample_overlap(event, array, sizeof(u32)))
417 if (sample_overlap(event, p, data->raw_size))