Merge branch 'for-linus' of git://github.com/dtor/input
[linux-btrfs-devel.git] / tools / perf / util / evsel.c
blobe389815078d3477412c577b2c4938865f6284027
1 /*
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
5 * copyright notes.
7 * Released under the GPL v2. (and only v2, not any later version)
8 */
10 #include <byteswap.h>
11 #include "asm/bug.h"
12 #include "evsel.h"
13 #include "evlist.h"
14 #include "util.h"
15 #include "cpumap.h"
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;
23 int size = 0;
24 int i;
26 for (i = 0; i < 64; i++) {
27 if (mask & (1ULL << i))
28 size++;
31 size *= sizeof(u64);
33 return size;
36 void perf_evsel__init(struct perf_evsel *evsel,
37 struct perf_event_attr *attr, int idx)
39 evsel->idx = idx;
40 evsel->attr = *attr;
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));
48 if (evsel != NULL)
49 perf_evsel__init(evsel, attr, idx);
51 return evsel;
54 int perf_evsel__alloc_fd(struct perf_evsel *evsel, int ncpus, int nthreads)
56 int cpu, thread;
57 evsel->fd = xyarray__new(ncpus, nthreads, sizeof(int));
59 if (evsel->fd) {
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)
74 return -ENOMEM;
76 evsel->id = zalloc(ncpus * nthreads * sizeof(u64));
77 if (evsel->id == NULL) {
78 xyarray__delete(evsel->sample_id);
79 evsel->sample_id = NULL;
80 return -ENOMEM;
83 return 0;
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);
96 evsel->fd = NULL;
99 void perf_evsel__free_id(struct perf_evsel *evsel)
101 xyarray__delete(evsel->sample_id);
102 evsel->sample_id = NULL;
103 free(evsel->id);
104 evsel->id = NULL;
107 void perf_evsel__close_fd(struct perf_evsel *evsel, int ncpus, int nthreads)
109 int cpu, thread;
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);
123 free(evsel->id);
126 void perf_evsel__delete(struct perf_evsel *evsel)
128 perf_evsel__exit(evsel);
129 close_cgroup(evsel->cgrp);
130 free(evsel->name);
131 free(evsel);
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)
141 return -EINVAL;
143 if (evsel->counts == NULL && perf_evsel__alloc_counts(evsel, cpu + 1) < 0)
144 return -ENOMEM;
146 if (readn(FD(evsel, cpu, thread), &count, nv * sizeof(u64)) < 0)
147 return -errno;
149 if (scale) {
150 if (count.run == 0)
151 count.val = 0;
152 else if (count.run < count.ena)
153 count.val = (u64)((double)count.val * count.ena / count.run + 0.5);
154 } else
155 count.ena = count.run = 0;
157 evsel->counts->cpu[cpu] = count;
158 return 0;
161 int __perf_evsel__read(struct perf_evsel *evsel,
162 int ncpus, int nthreads, bool scale)
164 size_t nv = scale ? 3 : 1;
165 int cpu, thread;
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)
173 continue;
175 if (readn(FD(evsel, cpu, thread),
176 &count, nv * sizeof(u64)) < 0)
177 return -errno;
179 aggr->val += count.val;
180 if (scale) {
181 aggr->ena += count.ena;
182 aggr->run += count.run;
187 evsel->counts->scaled = 0;
188 if (scale) {
189 if (aggr->run == 0) {
190 evsel->counts->scaled = -1;
191 aggr->val = 0;
192 return 0;
195 if (aggr->run < aggr->ena) {
196 evsel->counts->scaled = 1;
197 aggr->val = (u64)((double)aggr->val * aggr->ena / aggr->run + 0.5);
199 } else
200 aggr->ena = aggr->run = 0;
202 return 0;
205 static int __perf_evsel__open(struct perf_evsel *evsel, struct cpu_map *cpus,
206 struct thread_map *threads, bool group)
208 int cpu, thread;
209 unsigned long flags = 0;
210 int pid = -1;
212 if (evsel->fd == NULL &&
213 perf_evsel__alloc_fd(evsel, cpus->nr, threads->nr) < 0)
214 return -1;
216 if (evsel->cgrp) {
217 flags = PERF_FLAG_PID_CGROUP;
218 pid = evsel->cgrp->fd;
221 for (cpu = 0; cpu < cpus->nr; cpu++) {
222 int group_fd = -1;
224 for (thread = 0; thread < threads->nr; thread++) {
226 if (!evsel->cgrp)
227 pid = threads->map[thread];
229 FD(evsel, cpu, thread) = sys_perf_event_open(&evsel->attr,
230 pid,
231 cpus->map[cpu],
232 group_fd, flags);
233 if (FD(evsel, cpu, thread) < 0)
234 goto out_close;
236 if (group && group_fd == -1)
237 group_fd = FD(evsel, cpu, thread);
241 return 0;
243 out_close:
244 do {
245 while (--thread >= 0) {
246 close(FD(evsel, cpu, thread));
247 FD(evsel, cpu, thread) = -1;
249 thread = threads->nr;
250 } while (--cpu >= 0);
251 return -1;
254 static struct {
255 struct cpu_map map;
256 int cpus[1];
257 } empty_cpu_map = {
258 .map.nr = 1,
259 .cpus = { -1, },
262 static struct {
263 struct thread_map map;
264 int threads[1];
265 } empty_thread_map = {
266 .map.nr = 1,
267 .threads = { -1, },
270 int perf_evsel__open(struct perf_evsel *evsel, struct cpu_map *cpus,
271 struct thread_map *threads, bool group)
273 if (cpus == NULL) {
274 /* Work around old compiler warnings about strict aliasing */
275 cpus = &empty_cpu_map.map;
278 if (threads == NULL)
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;
306 sample->cpu = *p;
307 array--;
310 if (type & PERF_SAMPLE_STREAM_ID) {
311 sample->stream_id = *array;
312 array--;
315 if (type & PERF_SAMPLE_ID) {
316 sample->id = *array;
317 array--;
320 if (type & PERF_SAMPLE_TIME) {
321 sample->time = *array;
322 array--;
325 if (type & PERF_SAMPLE_TID) {
326 u32 *p = (u32 *)array;
327 sample->pid = p[0];
328 sample->tid = p[1];
331 return 0;
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)
340 return true;
342 return false;
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)
349 const u64 *array;
352 * used for cross-endian analysis. See git commit 65014ab3
353 * for why this goofiness is needed.
355 union {
356 u64 val64;
357 u32 val32[2];
358 } u;
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) {
365 if (!sample_id_all)
366 return 0;
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)
373 return -EFAULT;
375 if (type & PERF_SAMPLE_IP) {
376 data->ip = event->ip.ip;
377 array++;
380 if (type & PERF_SAMPLE_TID) {
381 u.val64 = *array;
382 if (swapped) {
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];
391 array++;
394 if (type & PERF_SAMPLE_TIME) {
395 data->time = *array;
396 array++;
399 data->addr = 0;
400 if (type & PERF_SAMPLE_ADDR) {
401 data->addr = *array;
402 array++;
405 data->id = -1ULL;
406 if (type & PERF_SAMPLE_ID) {
407 data->id = *array;
408 array++;
411 if (type & PERF_SAMPLE_STREAM_ID) {
412 data->stream_id = *array;
413 array++;
416 if (type & PERF_SAMPLE_CPU) {
418 u.val64 = *array;
419 if (swapped) {
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];
426 array++;
429 if (type & PERF_SAMPLE_PERIOD) {
430 data->period = *array;
431 array++;
434 if (type & PERF_SAMPLE_READ) {
435 fprintf(stderr, "PERF_SAMPLE_READ is unsuported for now\n");
436 return -1;
439 if (type & PERF_SAMPLE_CALLCHAIN) {
440 if (sample_overlap(event, array, sizeof(data->callchain->nr)))
441 return -EFAULT;
443 data->callchain = (struct ip_callchain *)array;
445 if (sample_overlap(event, array, data->callchain->nr))
446 return -EFAULT;
448 array += 1 + data->callchain->nr;
451 if (type & PERF_SAMPLE_RAW) {
452 const u64 *pdata;
454 u.val64 = *array;
455 if (WARN_ONCE(swapped,
456 "Endianness of raw data not corrected!\n")) {
457 /* undo swap of u64, then swap on individual u32s */
458 u.val64 = bswap_64(u.val64);
459 u.val32[0] = bswap_32(u.val32[0]);
460 u.val32[1] = bswap_32(u.val32[1]);
463 if (sample_overlap(event, array, sizeof(u32)))
464 return -EFAULT;
466 data->raw_size = u.val32[0];
467 pdata = (void *) array + sizeof(u32);
469 if (sample_overlap(event, pdata, data->raw_size))
470 return -EFAULT;
472 data->raw_data = (void *) pdata;
475 return 0;