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 #define FD(e, x, y) (*(int *)xyarray__entry(e->fd, x, y))
20 int __perf_evsel__sample_size(u64 sample_type)
22 u64 mask = sample_type & PERF_SAMPLE_MASK;
26 for (i = 0; i < 64; i++) {
27 if (mask & (1ULL << i))
36 void perf_evsel__init(struct perf_evsel *evsel,
37 struct perf_event_attr *attr, int idx)
41 INIT_LIST_HEAD(&evsel->node);
44 struct perf_evsel *perf_evsel__new(struct perf_event_attr *attr, int idx)
46 struct perf_evsel *evsel = zalloc(sizeof(*evsel));
49 perf_evsel__init(evsel, attr, idx);
54 int perf_evsel__alloc_fd(struct perf_evsel *evsel, int ncpus, int nthreads)
57 evsel->fd = xyarray__new(ncpus, nthreads, sizeof(int));
60 for (cpu = 0; cpu < ncpus; cpu++) {
61 for (thread = 0; thread < nthreads; thread++) {
62 FD(evsel, cpu, thread) = -1;
67 return evsel->fd != NULL ? 0 : -ENOMEM;
70 int perf_evsel__alloc_id(struct perf_evsel *evsel, int ncpus, int nthreads)
72 evsel->sample_id = xyarray__new(ncpus, nthreads, sizeof(struct perf_sample_id));
73 if (evsel->sample_id == NULL)
76 evsel->id = zalloc(ncpus * nthreads * sizeof(u64));
77 if (evsel->id == NULL) {
78 xyarray__delete(evsel->sample_id);
79 evsel->sample_id = NULL;
86 int perf_evsel__alloc_counts(struct perf_evsel *evsel, int ncpus)
88 evsel->counts = zalloc((sizeof(*evsel->counts) +
89 (ncpus * sizeof(struct perf_counts_values))));
90 return evsel->counts != NULL ? 0 : -ENOMEM;
93 void perf_evsel__free_fd(struct perf_evsel *evsel)
95 xyarray__delete(evsel->fd);
99 void perf_evsel__free_id(struct perf_evsel *evsel)
101 xyarray__delete(evsel->sample_id);
102 evsel->sample_id = NULL;
107 void perf_evsel__close_fd(struct perf_evsel *evsel, int ncpus, int nthreads)
111 for (cpu = 0; cpu < ncpus; cpu++)
112 for (thread = 0; thread < nthreads; ++thread) {
113 close(FD(evsel, cpu, thread));
114 FD(evsel, cpu, thread) = -1;
118 void perf_evsel__exit(struct perf_evsel *evsel)
120 assert(list_empty(&evsel->node));
121 xyarray__delete(evsel->fd);
122 xyarray__delete(evsel->sample_id);
126 void perf_evsel__delete(struct perf_evsel *evsel)
128 perf_evsel__exit(evsel);
129 close_cgroup(evsel->cgrp);
134 int __perf_evsel__read_on_cpu(struct perf_evsel *evsel,
135 int cpu, int thread, bool scale)
137 struct perf_counts_values count;
138 size_t nv = scale ? 3 : 1;
140 if (FD(evsel, cpu, thread) < 0)
143 if (evsel->counts == NULL && perf_evsel__alloc_counts(evsel, cpu + 1) < 0)
146 if (readn(FD(evsel, cpu, thread), &count, nv * sizeof(u64)) < 0)
152 else if (count.run < count.ena)
153 count.val = (u64)((double)count.val * count.ena / count.run + 0.5);
155 count.ena = count.run = 0;
157 evsel->counts->cpu[cpu] = count;
161 int __perf_evsel__read(struct perf_evsel *evsel,
162 int ncpus, int nthreads, bool scale)
164 size_t nv = scale ? 3 : 1;
166 struct perf_counts_values *aggr = &evsel->counts->aggr, count;
168 aggr->val = aggr->ena = aggr->run = 0;
170 for (cpu = 0; cpu < ncpus; cpu++) {
171 for (thread = 0; thread < nthreads; thread++) {
172 if (FD(evsel, cpu, thread) < 0)
175 if (readn(FD(evsel, cpu, thread),
176 &count, nv * sizeof(u64)) < 0)
179 aggr->val += count.val;
181 aggr->ena += count.ena;
182 aggr->run += count.run;
187 evsel->counts->scaled = 0;
189 if (aggr->run == 0) {
190 evsel->counts->scaled = -1;
195 if (aggr->run < aggr->ena) {
196 evsel->counts->scaled = 1;
197 aggr->val = (u64)((double)aggr->val * aggr->ena / aggr->run + 0.5);
200 aggr->ena = aggr->run = 0;
205 static int __perf_evsel__open(struct perf_evsel *evsel, struct cpu_map *cpus,
206 struct thread_map *threads, bool group)
209 unsigned long flags = 0;
212 if (evsel->fd == NULL &&
213 perf_evsel__alloc_fd(evsel, cpus->nr, threads->nr) < 0)
217 flags = PERF_FLAG_PID_CGROUP;
218 pid = evsel->cgrp->fd;
221 for (cpu = 0; cpu < cpus->nr; cpu++) {
224 for (thread = 0; thread < threads->nr; thread++) {
227 pid = threads->map[thread];
229 FD(evsel, cpu, thread) = sys_perf_event_open(&evsel->attr,
233 if (FD(evsel, cpu, thread) < 0)
236 if (group && group_fd == -1)
237 group_fd = FD(evsel, cpu, thread);
245 while (--thread >= 0) {
246 close(FD(evsel, cpu, thread));
247 FD(evsel, cpu, thread) = -1;
249 thread = threads->nr;
250 } while (--cpu >= 0);
263 struct thread_map map;
265 } empty_thread_map = {
270 int perf_evsel__open(struct perf_evsel *evsel, struct cpu_map *cpus,
271 struct thread_map *threads, bool group)
274 /* Work around old compiler warnings about strict aliasing */
275 cpus = &empty_cpu_map.map;
279 threads = &empty_thread_map.map;
281 return __perf_evsel__open(evsel, cpus, threads, group);
284 int perf_evsel__open_per_cpu(struct perf_evsel *evsel,
285 struct cpu_map *cpus, bool group)
287 return __perf_evsel__open(evsel, cpus, &empty_thread_map.map, group);
290 int perf_evsel__open_per_thread(struct perf_evsel *evsel,
291 struct thread_map *threads, bool group)
293 return __perf_evsel__open(evsel, &empty_cpu_map.map, threads, group);
296 static int perf_event__parse_id_sample(const union perf_event *event, u64 type,
297 struct perf_sample *sample)
299 const u64 *array = event->sample.array;
301 array += ((event->header.size -
302 sizeof(event->header)) / sizeof(u64)) - 1;
304 if (type & PERF_SAMPLE_CPU) {
305 u32 *p = (u32 *)array;
310 if (type & PERF_SAMPLE_STREAM_ID) {
311 sample->stream_id = *array;
315 if (type & PERF_SAMPLE_ID) {
320 if (type & PERF_SAMPLE_TIME) {
321 sample->time = *array;
325 if (type & PERF_SAMPLE_TID) {
326 u32 *p = (u32 *)array;
334 static bool sample_overlap(const union perf_event *event,
335 const void *offset, u64 size)
337 const void *base = event;
339 if (offset + size > base + event->header.size)
345 int perf_event__parse_sample(const union perf_event *event, u64 type,
346 int sample_size, bool sample_id_all,
347 struct perf_sample *data, bool swapped)
352 * used for cross-endian analysis. See git commit 65014ab3
353 * for why this goofiness is needed.
361 data->cpu = data->pid = data->tid = -1;
362 data->stream_id = data->id = data->time = -1ULL;
364 if (event->header.type != PERF_RECORD_SAMPLE) {
367 return perf_event__parse_id_sample(event, type, data);
370 array = event->sample.array;
372 if (sample_size + sizeof(event->header) > event->header.size)
375 if (type & PERF_SAMPLE_IP) {
376 data->ip = event->ip.ip;
380 if (type & PERF_SAMPLE_TID) {
383 /* undo swap of u64, then swap on individual u32s */
384 u.val64 = bswap_64(u.val64);
385 u.val32[0] = bswap_32(u.val32[0]);
386 u.val32[1] = bswap_32(u.val32[1]);
389 data->pid = u.val32[0];
390 data->tid = u.val32[1];
394 if (type & PERF_SAMPLE_TIME) {
400 if (type & PERF_SAMPLE_ADDR) {
406 if (type & PERF_SAMPLE_ID) {
411 if (type & PERF_SAMPLE_STREAM_ID) {
412 data->stream_id = *array;
416 if (type & PERF_SAMPLE_CPU) {
420 /* undo swap of u64, then swap on individual u32s */
421 u.val64 = bswap_64(u.val64);
422 u.val32[0] = bswap_32(u.val32[0]);
425 data->cpu = u.val32[0];
429 if (type & PERF_SAMPLE_PERIOD) {
430 data->period = *array;
434 if (type & PERF_SAMPLE_READ) {
435 fprintf(stderr, "PERF_SAMPLE_READ is unsuported for now\n");
439 if (type & PERF_SAMPLE_CALLCHAIN) {
440 if (sample_overlap(event, array, sizeof(data->callchain->nr)))
443 data->callchain = (struct ip_callchain *)array;
445 if (sample_overlap(event, array, data->callchain->nr))
448 array += 1 + data->callchain->nr;
451 if (type & PERF_SAMPLE_RAW) {
453 if (WARN_ONCE(swapped,
454 "Endianness of raw data not corrected!\n")) {
455 /* undo swap of u64, then swap on individual u32s */
456 u.val64 = bswap_64(u.val64);
457 u.val32[0] = bswap_32(u.val32[0]);
458 u.val32[1] = bswap_32(u.val32[1]);
461 if (sample_overlap(event, array, sizeof(u32)))
464 data->raw_size = u.val32[0];
466 if (sample_overlap(event, &u.val32[1], data->raw_size))
469 data->raw_data = &u.val32[1];