Merge branch 'drm-radeon-testing' of ../drm-radeon-next into drm-core-next
[linux-2.6-block.git] / tools / perf / util / evsel.c
CommitLineData
f8a95309
ACM
1/*
2 * Copyright (C) 2011, Red Hat Inc, Arnaldo Carvalho de Melo <acme@redhat.com>
3 *
4 * Parts came from builtin-{top,stat,record}.c, see those files for further
5 * copyright notes.
6 *
7 * Released under the GPL v2. (and only v2, not any later version)
8 */
9
936be503
DA
10#include <byteswap.h>
11#include "asm/bug.h"
69aad6f1 12#include "evsel.h"
70082dd9 13#include "evlist.h"
69aad6f1 14#include "util.h"
86bd5e86 15#include "cpumap.h"
fd78260b 16#include "thread_map.h"
69aad6f1 17
c52b12ed 18#define FD(e, x, y) (*(int *)xyarray__entry(e->fd, x, y))
727ab04e 19#define GROUP_FD(group_fd, cpu) (*(int *)xyarray__entry(group_fd, cpu, 0))
c52b12ed 20
c2a70653
ACM
21int __perf_evsel__sample_size(u64 sample_type)
22{
23 u64 mask = sample_type & PERF_SAMPLE_MASK;
24 int size = 0;
25 int i;
26
27 for (i = 0; i < 64; i++) {
28 if (mask & (1ULL << i))
29 size++;
30 }
31
32 size *= sizeof(u64);
33
34 return size;
35}
36
0e2a5f10
ACM
37static void hists__init(struct hists *hists)
38{
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);
45}
46
ef1d1af2
ACM
47void perf_evsel__init(struct perf_evsel *evsel,
48 struct perf_event_attr *attr, int idx)
49{
50 evsel->idx = idx;
51 evsel->attr = *attr;
52 INIT_LIST_HEAD(&evsel->node);
1980c2eb 53 hists__init(&evsel->hists);
ef1d1af2
ACM
54}
55
23a2f3ab 56struct perf_evsel *perf_evsel__new(struct perf_event_attr *attr, int idx)
69aad6f1
ACM
57{
58 struct perf_evsel *evsel = zalloc(sizeof(*evsel));
59
ef1d1af2
ACM
60 if (evsel != NULL)
61 perf_evsel__init(evsel, attr, idx);
69aad6f1
ACM
62
63 return evsel;
64}
65
66int perf_evsel__alloc_fd(struct perf_evsel *evsel, int ncpus, int nthreads)
67{
4af4c955 68 int cpu, thread;
69aad6f1 69 evsel->fd = xyarray__new(ncpus, nthreads, sizeof(int));
4af4c955
DA
70
71 if (evsel->fd) {
72 for (cpu = 0; cpu < ncpus; cpu++) {
73 for (thread = 0; thread < nthreads; thread++) {
74 FD(evsel, cpu, thread) = -1;
75 }
76 }
77 }
78
69aad6f1
ACM
79 return evsel->fd != NULL ? 0 : -ENOMEM;
80}
81
70db7533
ACM
82int perf_evsel__alloc_id(struct perf_evsel *evsel, int ncpus, int nthreads)
83{
a91e5431
ACM
84 evsel->sample_id = xyarray__new(ncpus, nthreads, sizeof(struct perf_sample_id));
85 if (evsel->sample_id == NULL)
86 return -ENOMEM;
87
88 evsel->id = zalloc(ncpus * nthreads * sizeof(u64));
89 if (evsel->id == NULL) {
90 xyarray__delete(evsel->sample_id);
91 evsel->sample_id = NULL;
92 return -ENOMEM;
93 }
94
95 return 0;
70db7533
ACM
96}
97
c52b12ed
ACM
98int perf_evsel__alloc_counts(struct perf_evsel *evsel, int ncpus)
99{
100 evsel->counts = zalloc((sizeof(*evsel->counts) +
101 (ncpus * sizeof(struct perf_counts_values))));
102 return evsel->counts != NULL ? 0 : -ENOMEM;
103}
104
69aad6f1
ACM
105void perf_evsel__free_fd(struct perf_evsel *evsel)
106{
107 xyarray__delete(evsel->fd);
108 evsel->fd = NULL;
109}
110
70db7533
ACM
111void perf_evsel__free_id(struct perf_evsel *evsel)
112{
a91e5431
ACM
113 xyarray__delete(evsel->sample_id);
114 evsel->sample_id = NULL;
115 free(evsel->id);
70db7533
ACM
116 evsel->id = NULL;
117}
118
c52b12ed
ACM
119void perf_evsel__close_fd(struct perf_evsel *evsel, int ncpus, int nthreads)
120{
121 int cpu, thread;
122
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;
127 }
128}
129
ef1d1af2 130void perf_evsel__exit(struct perf_evsel *evsel)
69aad6f1
ACM
131{
132 assert(list_empty(&evsel->node));
133 xyarray__delete(evsel->fd);
a91e5431
ACM
134 xyarray__delete(evsel->sample_id);
135 free(evsel->id);
ef1d1af2
ACM
136}
137
138void perf_evsel__delete(struct perf_evsel *evsel)
139{
140 perf_evsel__exit(evsel);
023695d9 141 close_cgroup(evsel->cgrp);
f0c55bcf 142 free(evsel->name);
69aad6f1
ACM
143 free(evsel);
144}
c52b12ed
ACM
145
146int __perf_evsel__read_on_cpu(struct perf_evsel *evsel,
147 int cpu, int thread, bool scale)
148{
149 struct perf_counts_values count;
150 size_t nv = scale ? 3 : 1;
151
152 if (FD(evsel, cpu, thread) < 0)
153 return -EINVAL;
154
4eed11d5
ACM
155 if (evsel->counts == NULL && perf_evsel__alloc_counts(evsel, cpu + 1) < 0)
156 return -ENOMEM;
157
c52b12ed
ACM
158 if (readn(FD(evsel, cpu, thread), &count, nv * sizeof(u64)) < 0)
159 return -errno;
160
161 if (scale) {
162 if (count.run == 0)
163 count.val = 0;
164 else if (count.run < count.ena)
165 count.val = (u64)((double)count.val * count.ena / count.run + 0.5);
166 } else
167 count.ena = count.run = 0;
168
169 evsel->counts->cpu[cpu] = count;
170 return 0;
171}
172
173int __perf_evsel__read(struct perf_evsel *evsel,
174 int ncpus, int nthreads, bool scale)
175{
176 size_t nv = scale ? 3 : 1;
177 int cpu, thread;
178 struct perf_counts_values *aggr = &evsel->counts->aggr, count;
179
52bcd994 180 aggr->val = aggr->ena = aggr->run = 0;
c52b12ed
ACM
181
182 for (cpu = 0; cpu < ncpus; cpu++) {
183 for (thread = 0; thread < nthreads; thread++) {
184 if (FD(evsel, cpu, thread) < 0)
185 continue;
186
187 if (readn(FD(evsel, cpu, thread),
188 &count, nv * sizeof(u64)) < 0)
189 return -errno;
190
191 aggr->val += count.val;
192 if (scale) {
193 aggr->ena += count.ena;
194 aggr->run += count.run;
195 }
196 }
197 }
198
199 evsel->counts->scaled = 0;
200 if (scale) {
201 if (aggr->run == 0) {
202 evsel->counts->scaled = -1;
203 aggr->val = 0;
204 return 0;
205 }
206
207 if (aggr->run < aggr->ena) {
208 evsel->counts->scaled = 1;
209 aggr->val = (u64)((double)aggr->val * aggr->ena / aggr->run + 0.5);
210 }
211 } else
212 aggr->ena = aggr->run = 0;
213
214 return 0;
215}
48290609 216
0252208e 217static int __perf_evsel__open(struct perf_evsel *evsel, struct cpu_map *cpus,
727ab04e
ACM
218 struct thread_map *threads, bool group,
219 struct xyarray *group_fds)
48290609 220{
0252208e 221 int cpu, thread;
023695d9 222 unsigned long flags = 0;
727ab04e 223 int pid = -1, err;
48290609 224
0252208e
ACM
225 if (evsel->fd == NULL &&
226 perf_evsel__alloc_fd(evsel, cpus->nr, threads->nr) < 0)
727ab04e 227 return -ENOMEM;
4eed11d5 228
023695d9
SE
229 if (evsel->cgrp) {
230 flags = PERF_FLAG_PID_CGROUP;
231 pid = evsel->cgrp->fd;
232 }
233
86bd5e86 234 for (cpu = 0; cpu < cpus->nr; cpu++) {
727ab04e 235 int group_fd = group_fds ? GROUP_FD(group_fds, cpu) : -1;
9d04f178 236
0252208e 237 for (thread = 0; thread < threads->nr; thread++) {
023695d9
SE
238
239 if (!evsel->cgrp)
240 pid = threads->map[thread];
241
0252208e 242 FD(evsel, cpu, thread) = sys_perf_event_open(&evsel->attr,
023695d9 243 pid,
f08199d3 244 cpus->map[cpu],
023695d9 245 group_fd, flags);
727ab04e
ACM
246 if (FD(evsel, cpu, thread) < 0) {
247 err = -errno;
0252208e 248 goto out_close;
727ab04e 249 }
f08199d3
ACM
250
251 if (group && group_fd == -1)
252 group_fd = FD(evsel, cpu, thread);
0252208e 253 }
48290609
ACM
254 }
255
256 return 0;
257
258out_close:
0252208e
ACM
259 do {
260 while (--thread >= 0) {
261 close(FD(evsel, cpu, thread));
262 FD(evsel, cpu, thread) = -1;
263 }
264 thread = threads->nr;
265 } while (--cpu >= 0);
727ab04e
ACM
266 return err;
267}
268
269void perf_evsel__close(struct perf_evsel *evsel, int ncpus, int nthreads)
270{
271 if (evsel->fd == NULL)
272 return;
273
274 perf_evsel__close_fd(evsel, ncpus, nthreads);
275 perf_evsel__free_fd(evsel);
276 evsel->fd = NULL;
48290609
ACM
277}
278
0252208e
ACM
279static struct {
280 struct cpu_map map;
281 int cpus[1];
282} empty_cpu_map = {
283 .map.nr = 1,
284 .cpus = { -1, },
285};
286
287static struct {
288 struct thread_map map;
289 int threads[1];
290} empty_thread_map = {
291 .map.nr = 1,
292 .threads = { -1, },
293};
294
f08199d3 295int perf_evsel__open(struct perf_evsel *evsel, struct cpu_map *cpus,
727ab04e
ACM
296 struct thread_map *threads, bool group,
297 struct xyarray *group_fd)
48290609 298{
0252208e
ACM
299 if (cpus == NULL) {
300 /* Work around old compiler warnings about strict aliasing */
301 cpus = &empty_cpu_map.map;
48290609
ACM
302 }
303
0252208e
ACM
304 if (threads == NULL)
305 threads = &empty_thread_map.map;
48290609 306
727ab04e 307 return __perf_evsel__open(evsel, cpus, threads, group, group_fd);
48290609
ACM
308}
309
f08199d3 310int perf_evsel__open_per_cpu(struct perf_evsel *evsel,
727ab04e
ACM
311 struct cpu_map *cpus, bool group,
312 struct xyarray *group_fd)
48290609 313{
727ab04e
ACM
314 return __perf_evsel__open(evsel, cpus, &empty_thread_map.map, group,
315 group_fd);
0252208e 316}
48290609 317
f08199d3 318int perf_evsel__open_per_thread(struct perf_evsel *evsel,
727ab04e
ACM
319 struct thread_map *threads, bool group,
320 struct xyarray *group_fd)
0252208e 321{
727ab04e
ACM
322 return __perf_evsel__open(evsel, &empty_cpu_map.map, threads, group,
323 group_fd);
48290609 324}
70082dd9 325
8115d60c
ACM
326static int perf_event__parse_id_sample(const union perf_event *event, u64 type,
327 struct perf_sample *sample)
d0dd74e8
ACM
328{
329 const u64 *array = event->sample.array;
330
331 array += ((event->header.size -
332 sizeof(event->header)) / sizeof(u64)) - 1;
333
334 if (type & PERF_SAMPLE_CPU) {
335 u32 *p = (u32 *)array;
336 sample->cpu = *p;
337 array--;
338 }
339
340 if (type & PERF_SAMPLE_STREAM_ID) {
341 sample->stream_id = *array;
342 array--;
343 }
344
345 if (type & PERF_SAMPLE_ID) {
346 sample->id = *array;
347 array--;
348 }
349
350 if (type & PERF_SAMPLE_TIME) {
351 sample->time = *array;
352 array--;
353 }
354
355 if (type & PERF_SAMPLE_TID) {
356 u32 *p = (u32 *)array;
357 sample->pid = p[0];
358 sample->tid = p[1];
359 }
360
361 return 0;
362}
363
98e1da90
FW
364static bool sample_overlap(const union perf_event *event,
365 const void *offset, u64 size)
366{
367 const void *base = event;
368
369 if (offset + size > base + event->header.size)
370 return true;
371
372 return false;
373}
374
8115d60c 375int perf_event__parse_sample(const union perf_event *event, u64 type,
a2854124 376 int sample_size, bool sample_id_all,
936be503 377 struct perf_sample *data, bool swapped)
d0dd74e8
ACM
378{
379 const u64 *array;
380
936be503
DA
381 /*
382 * used for cross-endian analysis. See git commit 65014ab3
383 * for why this goofiness is needed.
384 */
385 union {
386 u64 val64;
387 u32 val32[2];
388 } u;
389
390
d0dd74e8
ACM
391 data->cpu = data->pid = data->tid = -1;
392 data->stream_id = data->id = data->time = -1ULL;
393
394 if (event->header.type != PERF_RECORD_SAMPLE) {
395 if (!sample_id_all)
396 return 0;
8115d60c 397 return perf_event__parse_id_sample(event, type, data);
d0dd74e8
ACM
398 }
399
400 array = event->sample.array;
401
a2854124
FW
402 if (sample_size + sizeof(event->header) > event->header.size)
403 return -EFAULT;
404
d0dd74e8
ACM
405 if (type & PERF_SAMPLE_IP) {
406 data->ip = event->ip.ip;
407 array++;
408 }
409
410 if (type & PERF_SAMPLE_TID) {
936be503
DA
411 u.val64 = *array;
412 if (swapped) {
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]);
417 }
418
419 data->pid = u.val32[0];
420 data->tid = u.val32[1];
d0dd74e8
ACM
421 array++;
422 }
423
424 if (type & PERF_SAMPLE_TIME) {
425 data->time = *array;
426 array++;
427 }
428
7cec0922 429 data->addr = 0;
d0dd74e8
ACM
430 if (type & PERF_SAMPLE_ADDR) {
431 data->addr = *array;
432 array++;
433 }
434
435 data->id = -1ULL;
436 if (type & PERF_SAMPLE_ID) {
437 data->id = *array;
438 array++;
439 }
440
441 if (type & PERF_SAMPLE_STREAM_ID) {
442 data->stream_id = *array;
443 array++;
444 }
445
446 if (type & PERF_SAMPLE_CPU) {
936be503
DA
447
448 u.val64 = *array;
449 if (swapped) {
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]);
453 }
454
455 data->cpu = u.val32[0];
d0dd74e8
ACM
456 array++;
457 }
458
459 if (type & PERF_SAMPLE_PERIOD) {
460 data->period = *array;
461 array++;
462 }
463
464 if (type & PERF_SAMPLE_READ) {
465 fprintf(stderr, "PERF_SAMPLE_READ is unsuported for now\n");
466 return -1;
467 }
468
469 if (type & PERF_SAMPLE_CALLCHAIN) {
98e1da90
FW
470 if (sample_overlap(event, array, sizeof(data->callchain->nr)))
471 return -EFAULT;
472
d0dd74e8 473 data->callchain = (struct ip_callchain *)array;
98e1da90
FW
474
475 if (sample_overlap(event, array, data->callchain->nr))
476 return -EFAULT;
477
d0dd74e8
ACM
478 array += 1 + data->callchain->nr;
479 }
480
481 if (type & PERF_SAMPLE_RAW) {
8e303f20
JO
482 const u64 *pdata;
483
936be503
DA
484 u.val64 = *array;
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]);
491 }
98e1da90
FW
492
493 if (sample_overlap(event, array, sizeof(u32)))
494 return -EFAULT;
495
936be503 496 data->raw_size = u.val32[0];
8e303f20 497 pdata = (void *) array + sizeof(u32);
98e1da90 498
8e303f20 499 if (sample_overlap(event, pdata, data->raw_size))
98e1da90
FW
500 return -EFAULT;
501
8e303f20 502 data->raw_data = (void *) pdata;
d0dd74e8
ACM
503 }
504
505 return 0;
506}