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))
19 #define GROUP_FD(group_fd, cpu) (*(int *)xyarray__entry(group_fd, cpu, 0))
21 int __perf_evsel__sample_size(u64 sample_type)
23 u64 mask = sample_type & PERF_SAMPLE_MASK;
27 for (i = 0; i < 64; i++) {
28 if (mask & (1ULL << i))
37 static void hists__init(struct hists *hists)
39 memset(hists, 0, sizeof(*hists));
40 hists->entries_in_array[0] = hists->entries_in_array[1] = RB_ROOT;
41 hists->entries_in = &hists->entries_in_array[0];
42 hists->entries_collapsed = RB_ROOT;
43 hists->entries = RB_ROOT;
44 pthread_mutex_init(&hists->lock, NULL);
47 void perf_evsel__init(struct perf_evsel *evsel,
48 struct perf_event_attr *attr, int idx)
52 INIT_LIST_HEAD(&evsel->node);
53 hists__init(&evsel->hists);
56 struct perf_evsel *perf_evsel__new(struct perf_event_attr *attr, int idx)
58 struct perf_evsel *evsel = zalloc(sizeof(*evsel));
61 perf_evsel__init(evsel, attr, idx);
66 int perf_evsel__alloc_fd(struct perf_evsel *evsel, int ncpus, int nthreads)
69 evsel->fd = xyarray__new(ncpus, nthreads, sizeof(int));
72 for (cpu = 0; cpu < ncpus; cpu++) {
73 for (thread = 0; thread < nthreads; thread++) {
74 FD(evsel, cpu, thread) = -1;
79 return evsel->fd != NULL ? 0 : -ENOMEM;
82 int perf_evsel__alloc_id(struct perf_evsel *evsel, int ncpus, int nthreads)
84 evsel->sample_id = xyarray__new(ncpus, nthreads, sizeof(struct perf_sample_id));
85 if (evsel->sample_id == NULL)
88 evsel->id = zalloc(ncpus * nthreads * sizeof(u64));
89 if (evsel->id == NULL) {
90 xyarray__delete(evsel->sample_id);
91 evsel->sample_id = NULL;
98 int perf_evsel__alloc_counts(struct perf_evsel *evsel, int ncpus)
100 evsel->counts = zalloc((sizeof(*evsel->counts) +
101 (ncpus * sizeof(struct perf_counts_values))));
102 return evsel->counts != NULL ? 0 : -ENOMEM;
105 void perf_evsel__free_fd(struct perf_evsel *evsel)
107 xyarray__delete(evsel->fd);
111 void perf_evsel__free_id(struct perf_evsel *evsel)
113 xyarray__delete(evsel->sample_id);
114 evsel->sample_id = NULL;
119 void perf_evsel__close_fd(struct perf_evsel *evsel, int ncpus, int nthreads)
123 for (cpu = 0; cpu < ncpus; cpu++)
124 for (thread = 0; thread < nthreads; ++thread) {
125 close(FD(evsel, cpu, thread));
126 FD(evsel, cpu, thread) = -1;
130 void perf_evsel__exit(struct perf_evsel *evsel)
132 assert(list_empty(&evsel->node));
133 xyarray__delete(evsel->fd);
134 xyarray__delete(evsel->sample_id);
138 void perf_evsel__delete(struct perf_evsel *evsel)
140 perf_evsel__exit(evsel);
141 close_cgroup(evsel->cgrp);
146 int __perf_evsel__read_on_cpu(struct perf_evsel *evsel,
147 int cpu, int thread, bool scale)
149 struct perf_counts_values count;
150 size_t nv = scale ? 3 : 1;
152 if (FD(evsel, cpu, thread) < 0)
155 if (evsel->counts == NULL && perf_evsel__alloc_counts(evsel, cpu + 1) < 0)
158 if (readn(FD(evsel, cpu, thread), &count, nv * sizeof(u64)) < 0)
164 else if (count.run < count.ena)
165 count.val = (u64)((double)count.val * count.ena / count.run + 0.5);
167 count.ena = count.run = 0;
169 evsel->counts->cpu[cpu] = count;
173 int __perf_evsel__read(struct perf_evsel *evsel,
174 int ncpus, int nthreads, bool scale)
176 size_t nv = scale ? 3 : 1;
178 struct perf_counts_values *aggr = &evsel->counts->aggr, count;
180 aggr->val = aggr->ena = aggr->run = 0;
182 for (cpu = 0; cpu < ncpus; cpu++) {
183 for (thread = 0; thread < nthreads; thread++) {
184 if (FD(evsel, cpu, thread) < 0)
187 if (readn(FD(evsel, cpu, thread),
188 &count, nv * sizeof(u64)) < 0)
191 aggr->val += count.val;
193 aggr->ena += count.ena;
194 aggr->run += count.run;
199 evsel->counts->scaled = 0;
201 if (aggr->run == 0) {
202 evsel->counts->scaled = -1;
207 if (aggr->run < aggr->ena) {
208 evsel->counts->scaled = 1;
209 aggr->val = (u64)((double)aggr->val * aggr->ena / aggr->run + 0.5);
212 aggr->ena = aggr->run = 0;
217 static int __perf_evsel__open(struct perf_evsel *evsel, struct cpu_map *cpus,
218 struct thread_map *threads, bool group,
219 struct xyarray *group_fds)
222 unsigned long flags = 0;
225 if (evsel->fd == NULL &&
226 perf_evsel__alloc_fd(evsel, cpus->nr, threads->nr) < 0)
230 flags = PERF_FLAG_PID_CGROUP;
231 pid = evsel->cgrp->fd;
234 for (cpu = 0; cpu < cpus->nr; cpu++) {
235 int group_fd = group_fds ? GROUP_FD(group_fds, cpu) : -1;
237 for (thread = 0; thread < threads->nr; thread++) {
240 pid = threads->map[thread];
242 FD(evsel, cpu, thread) = sys_perf_event_open(&evsel->attr,
246 if (FD(evsel, cpu, thread) < 0) {
251 if (group && group_fd == -1)
252 group_fd = FD(evsel, cpu, thread);
260 while (--thread >= 0) {
261 close(FD(evsel, cpu, thread));
262 FD(evsel, cpu, thread) = -1;
264 thread = threads->nr;
265 } while (--cpu >= 0);
269 void perf_evsel__close(struct perf_evsel *evsel, int ncpus, int nthreads)
271 if (evsel->fd == NULL)
274 perf_evsel__close_fd(evsel, ncpus, nthreads);
275 perf_evsel__free_fd(evsel);
288 struct thread_map map;
290 } empty_thread_map = {
295 int perf_evsel__open(struct perf_evsel *evsel, struct cpu_map *cpus,
296 struct thread_map *threads, bool group,
297 struct xyarray *group_fd)
300 /* Work around old compiler warnings about strict aliasing */
301 cpus = &empty_cpu_map.map;
305 threads = &empty_thread_map.map;
307 return __perf_evsel__open(evsel, cpus, threads, group, group_fd);
310 int perf_evsel__open_per_cpu(struct perf_evsel *evsel,
311 struct cpu_map *cpus, bool group,
312 struct xyarray *group_fd)
314 return __perf_evsel__open(evsel, cpus, &empty_thread_map.map, group,
318 int perf_evsel__open_per_thread(struct perf_evsel *evsel,
319 struct thread_map *threads, bool group,
320 struct xyarray *group_fd)
322 return __perf_evsel__open(evsel, &empty_cpu_map.map, threads, group,
326 static int perf_event__parse_id_sample(const union perf_event *event, u64 type,
327 struct perf_sample *sample)
329 const u64 *array = event->sample.array;
331 array += ((event->header.size -
332 sizeof(event->header)) / sizeof(u64)) - 1;
334 if (type & PERF_SAMPLE_CPU) {
335 u32 *p = (u32 *)array;
340 if (type & PERF_SAMPLE_STREAM_ID) {
341 sample->stream_id = *array;
345 if (type & PERF_SAMPLE_ID) {
350 if (type & PERF_SAMPLE_TIME) {
351 sample->time = *array;
355 if (type & PERF_SAMPLE_TID) {
356 u32 *p = (u32 *)array;
364 static bool sample_overlap(const union perf_event *event,
365 const void *offset, u64 size)
367 const void *base = event;
369 if (offset + size > base + event->header.size)
375 int perf_event__parse_sample(const union perf_event *event, u64 type,
376 int sample_size, bool sample_id_all,
377 struct perf_sample *data, bool swapped)
382 * used for cross-endian analysis. See git commit 65014ab3
383 * for why this goofiness is needed.
391 data->cpu = data->pid = data->tid = -1;
392 data->stream_id = data->id = data->time = -1ULL;
394 if (event->header.type != PERF_RECORD_SAMPLE) {
397 return perf_event__parse_id_sample(event, type, data);
400 array = event->sample.array;
402 if (sample_size + sizeof(event->header) > event->header.size)
405 if (type & PERF_SAMPLE_IP) {
406 data->ip = event->ip.ip;
410 if (type & PERF_SAMPLE_TID) {
413 /* undo swap of u64, then swap on individual u32s */
414 u.val64 = bswap_64(u.val64);
415 u.val32[0] = bswap_32(u.val32[0]);
416 u.val32[1] = bswap_32(u.val32[1]);
419 data->pid = u.val32[0];
420 data->tid = u.val32[1];
424 if (type & PERF_SAMPLE_TIME) {
430 if (type & PERF_SAMPLE_ADDR) {
436 if (type & PERF_SAMPLE_ID) {
441 if (type & PERF_SAMPLE_STREAM_ID) {
442 data->stream_id = *array;
446 if (type & PERF_SAMPLE_CPU) {
450 /* undo swap of u64, then swap on individual u32s */
451 u.val64 = bswap_64(u.val64);
452 u.val32[0] = bswap_32(u.val32[0]);
455 data->cpu = u.val32[0];
459 if (type & PERF_SAMPLE_PERIOD) {
460 data->period = *array;
464 if (type & PERF_SAMPLE_READ) {
465 fprintf(stderr, "PERF_SAMPLE_READ is unsuported for now\n");
469 if (type & PERF_SAMPLE_CALLCHAIN) {
470 if (sample_overlap(event, array, sizeof(data->callchain->nr)))
473 data->callchain = (struct ip_callchain *)array;
475 if (sample_overlap(event, array, data->callchain->nr))
478 array += 1 + data->callchain->nr;
481 if (type & PERF_SAMPLE_RAW) {
485 if (WARN_ONCE(swapped,
486 "Endianness of raw data not corrected!\n")) {
487 /* undo swap of u64, then swap on individual u32s */
488 u.val64 = bswap_64(u.val64);
489 u.val32[0] = bswap_32(u.val32[0]);
490 u.val32[1] = bswap_32(u.val32[1]);
493 if (sample_overlap(event, array, sizeof(u32)))
496 data->raw_size = u.val32[0];
497 pdata = (void *) array + sizeof(u32);
499 if (sample_overlap(event, pdata, data->raw_size))
502 data->raw_data = (void *) pdata;