blob: 47f1fe2feab89495d06283ec270b079c3e1f2181 [file] [log] [blame]
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -02001/*
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
David Ahern936be502011-09-06 09:12:26 -060010#include <byteswap.h>
11#include "asm/bug.h"
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -020012#include "evsel.h"
Arnaldo Carvalho de Melo70082dd2011-01-12 17:03:24 -020013#include "evlist.h"
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -020014#include "util.h"
Arnaldo Carvalho de Melo86bd5e82011-01-03 23:09:46 -020015#include "cpumap.h"
Arnaldo Carvalho de Melofd782602011-01-18 15:15:24 -020016#include "thread_map.h"
Namhyung Kim12864b32012-04-26 14:15:22 +090017#include "target.h"
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -030018#include "../../include/linux/perf_event.h"
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -020019
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -020020#define FD(e, x, y) (*(int *)xyarray__entry(e->fd, x, y))
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -020021#define GROUP_FD(group_fd, cpu) (*(int *)xyarray__entry(group_fd, cpu, 0))
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -020022
Arnaldo Carvalho de Meloc2a70652011-06-02 11:04:54 -030023int __perf_evsel__sample_size(u64 sample_type)
24{
25 u64 mask = sample_type & PERF_SAMPLE_MASK;
26 int size = 0;
27 int i;
28
29 for (i = 0; i < 64; i++) {
30 if (mask & (1ULL << i))
31 size++;
32 }
33
34 size *= sizeof(u64);
35
36 return size;
37}
38
Jiri Olsa4bf9ce12012-03-22 14:37:26 +010039void hists__init(struct hists *hists)
Arnaldo Carvalho de Melo0e2a5f12011-11-04 08:16:58 -020040{
41 memset(hists, 0, sizeof(*hists));
42 hists->entries_in_array[0] = hists->entries_in_array[1] = RB_ROOT;
43 hists->entries_in = &hists->entries_in_array[0];
44 hists->entries_collapsed = RB_ROOT;
45 hists->entries = RB_ROOT;
46 pthread_mutex_init(&hists->lock, NULL);
47}
48
Arnaldo Carvalho de Meloef1d1af2011-01-18 21:41:45 -020049void perf_evsel__init(struct perf_evsel *evsel,
50 struct perf_event_attr *attr, int idx)
51{
52 evsel->idx = idx;
53 evsel->attr = *attr;
54 INIT_LIST_HEAD(&evsel->node);
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -030055 hists__init(&evsel->hists);
Arnaldo Carvalho de Meloef1d1af2011-01-18 21:41:45 -020056}
57
Lin Ming23a2f3a2011-01-07 11:11:09 +080058struct perf_evsel *perf_evsel__new(struct perf_event_attr *attr, int idx)
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -020059{
60 struct perf_evsel *evsel = zalloc(sizeof(*evsel));
61
Arnaldo Carvalho de Meloef1d1af2011-01-18 21:41:45 -020062 if (evsel != NULL)
63 perf_evsel__init(evsel, attr, idx);
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -020064
65 return evsel;
66}
67
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -030068static const char *perf_evsel__hw_names[PERF_COUNT_HW_MAX] = {
69 "cycles",
70 "instructions",
71 "cache-references",
72 "cache-misses",
73 "branches",
74 "branch-misses",
75 "bus-cycles",
76 "stalled-cycles-frontend",
77 "stalled-cycles-backend",
78 "ref-cycles",
79};
80
81const char *__perf_evsel__hw_name(u64 config)
82{
83 if (config < PERF_COUNT_HW_MAX && perf_evsel__hw_names[config])
84 return perf_evsel__hw_names[config];
85
86 return "unknown-hardware";
87}
88
Arnaldo Carvalho de Melo27f18612012-06-11 13:33:09 -030089static int perf_evsel__add_modifiers(struct perf_evsel *evsel, char *bf, size_t size)
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -030090{
Arnaldo Carvalho de Melo27f18612012-06-11 13:33:09 -030091 int colon = 0, r = 0;
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -030092 struct perf_event_attr *attr = &evsel->attr;
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -030093 bool exclude_guest_default = false;
94
95#define MOD_PRINT(context, mod) do { \
96 if (!attr->exclude_##context) { \
Arnaldo Carvalho de Melo27f18612012-06-11 13:33:09 -030097 if (!colon) colon = ++r; \
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -030098 r += scnprintf(bf + r, size - r, "%c", mod); \
99 } } while(0)
100
101 if (attr->exclude_kernel || attr->exclude_user || attr->exclude_hv) {
102 MOD_PRINT(kernel, 'k');
103 MOD_PRINT(user, 'u');
104 MOD_PRINT(hv, 'h');
105 exclude_guest_default = true;
106 }
107
108 if (attr->precise_ip) {
109 if (!colon)
Arnaldo Carvalho de Melo27f18612012-06-11 13:33:09 -0300110 colon = ++r;
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300111 r += scnprintf(bf + r, size - r, "%.*s", attr->precise_ip, "ppp");
112 exclude_guest_default = true;
113 }
114
115 if (attr->exclude_host || attr->exclude_guest == exclude_guest_default) {
116 MOD_PRINT(host, 'H');
117 MOD_PRINT(guest, 'G');
118 }
119#undef MOD_PRINT
120 if (colon)
Arnaldo Carvalho de Melo27f18612012-06-11 13:33:09 -0300121 bf[colon - 1] = ':';
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300122 return r;
123}
124
Arnaldo Carvalho de Melo27f18612012-06-11 13:33:09 -0300125static int perf_evsel__hw_name(struct perf_evsel *evsel, char *bf, size_t size)
126{
127 int r = scnprintf(bf, size, "%s", __perf_evsel__hw_name(evsel->attr.config));
128 return r + perf_evsel__add_modifiers(evsel, bf + r, size - r);
129}
130
Arnaldo Carvalho de Melo0b668bc2012-06-11 14:08:07 -0300131const char *perf_evsel__hw_cache[PERF_COUNT_HW_CACHE_MAX]
132 [PERF_EVSEL__MAX_ALIASES] = {
133 { "L1-dcache", "l1-d", "l1d", "L1-data", },
134 { "L1-icache", "l1-i", "l1i", "L1-instruction", },
135 { "LLC", "L2", },
136 { "dTLB", "d-tlb", "Data-TLB", },
137 { "iTLB", "i-tlb", "Instruction-TLB", },
138 { "branch", "branches", "bpu", "btb", "bpc", },
139 { "node", },
140};
141
142const char *perf_evsel__hw_cache_op[PERF_COUNT_HW_CACHE_OP_MAX]
143 [PERF_EVSEL__MAX_ALIASES] = {
144 { "load", "loads", "read", },
145 { "store", "stores", "write", },
146 { "prefetch", "prefetches", "speculative-read", "speculative-load", },
147};
148
149const char *perf_evsel__hw_cache_result[PERF_COUNT_HW_CACHE_RESULT_MAX]
150 [PERF_EVSEL__MAX_ALIASES] = {
151 { "refs", "Reference", "ops", "access", },
152 { "misses", "miss", },
153};
154
155#define C(x) PERF_COUNT_HW_CACHE_##x
156#define CACHE_READ (1 << C(OP_READ))
157#define CACHE_WRITE (1 << C(OP_WRITE))
158#define CACHE_PREFETCH (1 << C(OP_PREFETCH))
159#define COP(x) (1 << x)
160
161/*
162 * cache operartion stat
163 * L1I : Read and prefetch only
164 * ITLB and BPU : Read-only
165 */
166static unsigned long perf_evsel__hw_cache_stat[C(MAX)] = {
167 [C(L1D)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
168 [C(L1I)] = (CACHE_READ | CACHE_PREFETCH),
169 [C(LL)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
170 [C(DTLB)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
171 [C(ITLB)] = (CACHE_READ),
172 [C(BPU)] = (CACHE_READ),
173 [C(NODE)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
174};
175
176bool perf_evsel__is_cache_op_valid(u8 type, u8 op)
177{
178 if (perf_evsel__hw_cache_stat[type] & COP(op))
179 return true; /* valid */
180 else
181 return false; /* invalid */
182}
183
184int __perf_evsel__hw_cache_type_op_res_name(u8 type, u8 op, u8 result,
185 char *bf, size_t size)
186{
187 if (result) {
188 return scnprintf(bf, size, "%s-%s-%s", perf_evsel__hw_cache[type][0],
189 perf_evsel__hw_cache_op[op][0],
190 perf_evsel__hw_cache_result[result][0]);
191 }
192
193 return scnprintf(bf, size, "%s-%s", perf_evsel__hw_cache[type][0],
194 perf_evsel__hw_cache_op[op][1]);
195}
196
197int __perf_evsel__hw_cache_name(u64 config, char *bf, size_t size)
198{
199 u8 op, result, type = (config >> 0) & 0xff;
200 const char *err = "unknown-ext-hardware-cache-type";
201
202 if (type > PERF_COUNT_HW_CACHE_MAX)
203 goto out_err;
204
205 op = (config >> 8) & 0xff;
206 err = "unknown-ext-hardware-cache-op";
207 if (op > PERF_COUNT_HW_CACHE_OP_MAX)
208 goto out_err;
209
210 result = (config >> 16) & 0xff;
211 err = "unknown-ext-hardware-cache-result";
212 if (result > PERF_COUNT_HW_CACHE_RESULT_MAX)
213 goto out_err;
214
215 err = "invalid-cache";
216 if (!perf_evsel__is_cache_op_valid(type, op))
217 goto out_err;
218
219 return __perf_evsel__hw_cache_type_op_res_name(type, op, result, bf, size);
220out_err:
221 return scnprintf(bf, size, "%s", err);
222}
223
224static int perf_evsel__hw_cache_name(struct perf_evsel *evsel, char *bf, size_t size)
225{
226 int ret = __perf_evsel__hw_cache_name(evsel->attr.config, bf, size);
227 return ret + perf_evsel__add_modifiers(evsel, bf + ret, size - ret);
228}
229
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300230int perf_evsel__name(struct perf_evsel *evsel, char *bf, size_t size)
231{
232 int ret;
233
234 switch (evsel->attr.type) {
235 case PERF_TYPE_RAW:
236 ret = scnprintf(bf, size, "raw 0x%" PRIx64, evsel->attr.config);
237 break;
238
239 case PERF_TYPE_HARDWARE:
240 ret = perf_evsel__hw_name(evsel, bf, size);
241 break;
Arnaldo Carvalho de Melo0b668bc2012-06-11 14:08:07 -0300242
243 case PERF_TYPE_HW_CACHE:
244 ret = perf_evsel__hw_cache_name(evsel, bf, size);
245 break;
246
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300247 default:
248 /*
249 * FIXME
250 *
251 * This is the minimal perf_evsel__name so that we can
252 * reconstruct event names taking into account event modifiers.
253 *
254 * The old event_name uses it now for raw anr hw events, so that
255 * we don't drag all the parsing stuff into the python binding.
256 *
257 * On the next devel cycle the rest of the event naming will be
258 * brought here.
259 */
260 return 0;
261 }
262
263 return ret;
264}
265
Namhyung Kim5090c6a2012-03-16 17:42:20 +0900266void perf_evsel__config(struct perf_evsel *evsel, struct perf_record_opts *opts,
267 struct perf_evsel *first)
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200268{
269 struct perf_event_attr *attr = &evsel->attr;
270 int track = !evsel->idx; /* only the first counter needs these */
271
David Ahern5e1c81d2012-05-13 22:01:28 -0600272 attr->disabled = 1;
Arnaldo Carvalho de Melo808e1222012-02-14 14:18:57 -0200273 attr->sample_id_all = opts->sample_id_all_missing ? 0 : 1;
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200274 attr->inherit = !opts->no_inherit;
275 attr->read_format = PERF_FORMAT_TOTAL_TIME_ENABLED |
276 PERF_FORMAT_TOTAL_TIME_RUNNING |
277 PERF_FORMAT_ID;
278
279 attr->sample_type |= PERF_SAMPLE_IP | PERF_SAMPLE_TID;
280
281 /*
282 * We default some events to a 1 default interval. But keep
283 * it a weak assumption overridable by the user.
284 */
285 if (!attr->sample_period || (opts->user_freq != UINT_MAX &&
286 opts->user_interval != ULLONG_MAX)) {
287 if (opts->freq) {
288 attr->sample_type |= PERF_SAMPLE_PERIOD;
289 attr->freq = 1;
290 attr->sample_freq = opts->freq;
291 } else {
292 attr->sample_period = opts->default_interval;
293 }
294 }
295
296 if (opts->no_samples)
297 attr->sample_freq = 0;
298
299 if (opts->inherit_stat)
300 attr->inherit_stat = 1;
301
302 if (opts->sample_address) {
303 attr->sample_type |= PERF_SAMPLE_ADDR;
304 attr->mmap_data = track;
305 }
306
307 if (opts->call_graph)
308 attr->sample_type |= PERF_SAMPLE_CALLCHAIN;
309
Namhyung Kime40ee742012-05-21 10:42:07 +0900310 if (perf_target__has_cpu(&opts->target))
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200311 attr->sample_type |= PERF_SAMPLE_CPU;
312
Andrew Vagin3e76ac72011-12-20 17:32:45 +0300313 if (opts->period)
314 attr->sample_type |= PERF_SAMPLE_PERIOD;
315
Arnaldo Carvalho de Melo808e1222012-02-14 14:18:57 -0200316 if (!opts->sample_id_all_missing &&
Namhyung Kimd67356e2012-05-07 14:09:03 +0900317 (opts->sample_time || !opts->no_inherit ||
Namhyung Kimaa22dd42012-05-16 18:45:47 +0900318 perf_target__has_cpu(&opts->target)))
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200319 attr->sample_type |= PERF_SAMPLE_TIME;
320
321 if (opts->raw_samples) {
322 attr->sample_type |= PERF_SAMPLE_TIME;
323 attr->sample_type |= PERF_SAMPLE_RAW;
324 attr->sample_type |= PERF_SAMPLE_CPU;
325 }
326
327 if (opts->no_delay) {
328 attr->watermark = 0;
329 attr->wakeup_events = 1;
330 }
Roberto Agostino Vitillobdfebd82012-02-09 23:21:02 +0100331 if (opts->branch_stack) {
332 attr->sample_type |= PERF_SAMPLE_BRANCH_STACK;
333 attr->branch_sample_type = opts->branch_stack;
334 }
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200335
336 attr->mmap = track;
337 attr->comm = track;
338
Namhyung Kimd67356e2012-05-07 14:09:03 +0900339 if (perf_target__none(&opts->target) &&
340 (!opts->group || evsel == first)) {
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200341 attr->enable_on_exec = 1;
342 }
343}
344
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200345int perf_evsel__alloc_fd(struct perf_evsel *evsel, int ncpus, int nthreads)
346{
David Ahern4af4c952011-05-27 09:58:34 -0600347 int cpu, thread;
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200348 evsel->fd = xyarray__new(ncpus, nthreads, sizeof(int));
David Ahern4af4c952011-05-27 09:58:34 -0600349
350 if (evsel->fd) {
351 for (cpu = 0; cpu < ncpus; cpu++) {
352 for (thread = 0; thread < nthreads; thread++) {
353 FD(evsel, cpu, thread) = -1;
354 }
355 }
356 }
357
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200358 return evsel->fd != NULL ? 0 : -ENOMEM;
359}
360
Arnaldo Carvalho de Melo70db7532011-01-12 22:39:13 -0200361int perf_evsel__alloc_id(struct perf_evsel *evsel, int ncpus, int nthreads)
362{
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -0300363 evsel->sample_id = xyarray__new(ncpus, nthreads, sizeof(struct perf_sample_id));
364 if (evsel->sample_id == NULL)
365 return -ENOMEM;
366
367 evsel->id = zalloc(ncpus * nthreads * sizeof(u64));
368 if (evsel->id == NULL) {
369 xyarray__delete(evsel->sample_id);
370 evsel->sample_id = NULL;
371 return -ENOMEM;
372 }
373
374 return 0;
Arnaldo Carvalho de Melo70db7532011-01-12 22:39:13 -0200375}
376
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200377int perf_evsel__alloc_counts(struct perf_evsel *evsel, int ncpus)
378{
379 evsel->counts = zalloc((sizeof(*evsel->counts) +
380 (ncpus * sizeof(struct perf_counts_values))));
381 return evsel->counts != NULL ? 0 : -ENOMEM;
382}
383
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200384void perf_evsel__free_fd(struct perf_evsel *evsel)
385{
386 xyarray__delete(evsel->fd);
387 evsel->fd = NULL;
388}
389
Arnaldo Carvalho de Melo70db7532011-01-12 22:39:13 -0200390void perf_evsel__free_id(struct perf_evsel *evsel)
391{
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -0300392 xyarray__delete(evsel->sample_id);
393 evsel->sample_id = NULL;
394 free(evsel->id);
Arnaldo Carvalho de Melo70db7532011-01-12 22:39:13 -0200395 evsel->id = NULL;
396}
397
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200398void perf_evsel__close_fd(struct perf_evsel *evsel, int ncpus, int nthreads)
399{
400 int cpu, thread;
401
402 for (cpu = 0; cpu < ncpus; cpu++)
403 for (thread = 0; thread < nthreads; ++thread) {
404 close(FD(evsel, cpu, thread));
405 FD(evsel, cpu, thread) = -1;
406 }
407}
408
Arnaldo Carvalho de Meloef1d1af2011-01-18 21:41:45 -0200409void perf_evsel__exit(struct perf_evsel *evsel)
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200410{
411 assert(list_empty(&evsel->node));
412 xyarray__delete(evsel->fd);
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -0300413 xyarray__delete(evsel->sample_id);
414 free(evsel->id);
Arnaldo Carvalho de Meloef1d1af2011-01-18 21:41:45 -0200415}
416
417void perf_evsel__delete(struct perf_evsel *evsel)
418{
419 perf_evsel__exit(evsel);
Stephane Eranian023695d2011-02-14 11:20:01 +0200420 close_cgroup(evsel->cgrp);
Stephane Eranianf0c55bc2011-02-16 15:10:01 +0200421 free(evsel->name);
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200422 free(evsel);
423}
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200424
425int __perf_evsel__read_on_cpu(struct perf_evsel *evsel,
426 int cpu, int thread, bool scale)
427{
428 struct perf_counts_values count;
429 size_t nv = scale ? 3 : 1;
430
431 if (FD(evsel, cpu, thread) < 0)
432 return -EINVAL;
433
Arnaldo Carvalho de Melo4eed11d2011-01-04 00:13:17 -0200434 if (evsel->counts == NULL && perf_evsel__alloc_counts(evsel, cpu + 1) < 0)
435 return -ENOMEM;
436
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200437 if (readn(FD(evsel, cpu, thread), &count, nv * sizeof(u64)) < 0)
438 return -errno;
439
440 if (scale) {
441 if (count.run == 0)
442 count.val = 0;
443 else if (count.run < count.ena)
444 count.val = (u64)((double)count.val * count.ena / count.run + 0.5);
445 } else
446 count.ena = count.run = 0;
447
448 evsel->counts->cpu[cpu] = count;
449 return 0;
450}
451
452int __perf_evsel__read(struct perf_evsel *evsel,
453 int ncpus, int nthreads, bool scale)
454{
455 size_t nv = scale ? 3 : 1;
456 int cpu, thread;
457 struct perf_counts_values *aggr = &evsel->counts->aggr, count;
458
Arnaldo Carvalho de Melo52bcd9942011-02-03 17:26:06 -0200459 aggr->val = aggr->ena = aggr->run = 0;
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200460
461 for (cpu = 0; cpu < ncpus; cpu++) {
462 for (thread = 0; thread < nthreads; thread++) {
463 if (FD(evsel, cpu, thread) < 0)
464 continue;
465
466 if (readn(FD(evsel, cpu, thread),
467 &count, nv * sizeof(u64)) < 0)
468 return -errno;
469
470 aggr->val += count.val;
471 if (scale) {
472 aggr->ena += count.ena;
473 aggr->run += count.run;
474 }
475 }
476 }
477
478 evsel->counts->scaled = 0;
479 if (scale) {
480 if (aggr->run == 0) {
481 evsel->counts->scaled = -1;
482 aggr->val = 0;
483 return 0;
484 }
485
486 if (aggr->run < aggr->ena) {
487 evsel->counts->scaled = 1;
488 aggr->val = (u64)((double)aggr->val * aggr->ena / aggr->run + 0.5);
489 }
490 } else
491 aggr->ena = aggr->run = 0;
492
493 return 0;
494}
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -0200495
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -0200496static int __perf_evsel__open(struct perf_evsel *evsel, struct cpu_map *cpus,
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -0200497 struct thread_map *threads, bool group,
498 struct xyarray *group_fds)
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -0200499{
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -0200500 int cpu, thread;
Stephane Eranian023695d2011-02-14 11:20:01 +0200501 unsigned long flags = 0;
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -0200502 int pid = -1, err;
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -0200503
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -0200504 if (evsel->fd == NULL &&
505 perf_evsel__alloc_fd(evsel, cpus->nr, threads->nr) < 0)
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -0200506 return -ENOMEM;
Arnaldo Carvalho de Melo4eed11d2011-01-04 00:13:17 -0200507
Stephane Eranian023695d2011-02-14 11:20:01 +0200508 if (evsel->cgrp) {
509 flags = PERF_FLAG_PID_CGROUP;
510 pid = evsel->cgrp->fd;
511 }
512
Arnaldo Carvalho de Melo86bd5e82011-01-03 23:09:46 -0200513 for (cpu = 0; cpu < cpus->nr; cpu++) {
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -0200514 int group_fd = group_fds ? GROUP_FD(group_fds, cpu) : -1;
Arnaldo Carvalho de Melo9d04f172011-01-12 00:08:18 -0200515
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -0200516 for (thread = 0; thread < threads->nr; thread++) {
Stephane Eranian023695d2011-02-14 11:20:01 +0200517
518 if (!evsel->cgrp)
519 pid = threads->map[thread];
520
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -0200521 FD(evsel, cpu, thread) = sys_perf_event_open(&evsel->attr,
Stephane Eranian023695d2011-02-14 11:20:01 +0200522 pid,
Arnaldo Carvalho de Melof08199d2011-01-11 23:42:19 -0200523 cpus->map[cpu],
Stephane Eranian023695d2011-02-14 11:20:01 +0200524 group_fd, flags);
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -0200525 if (FD(evsel, cpu, thread) < 0) {
526 err = -errno;
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -0200527 goto out_close;
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -0200528 }
Arnaldo Carvalho de Melof08199d2011-01-11 23:42:19 -0200529
530 if (group && group_fd == -1)
531 group_fd = FD(evsel, cpu, thread);
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -0200532 }
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -0200533 }
534
535 return 0;
536
537out_close:
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -0200538 do {
539 while (--thread >= 0) {
540 close(FD(evsel, cpu, thread));
541 FD(evsel, cpu, thread) = -1;
542 }
543 thread = threads->nr;
544 } while (--cpu >= 0);
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -0200545 return err;
546}
547
548void perf_evsel__close(struct perf_evsel *evsel, int ncpus, int nthreads)
549{
550 if (evsel->fd == NULL)
551 return;
552
553 perf_evsel__close_fd(evsel, ncpus, nthreads);
554 perf_evsel__free_fd(evsel);
555 evsel->fd = NULL;
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -0200556}
557
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -0200558static struct {
559 struct cpu_map map;
560 int cpus[1];
561} empty_cpu_map = {
562 .map.nr = 1,
563 .cpus = { -1, },
564};
565
566static struct {
567 struct thread_map map;
568 int threads[1];
569} empty_thread_map = {
570 .map.nr = 1,
571 .threads = { -1, },
572};
573
Arnaldo Carvalho de Melof08199d2011-01-11 23:42:19 -0200574int perf_evsel__open(struct perf_evsel *evsel, struct cpu_map *cpus,
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -0200575 struct thread_map *threads, bool group,
576 struct xyarray *group_fd)
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -0200577{
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -0200578 if (cpus == NULL) {
579 /* Work around old compiler warnings about strict aliasing */
580 cpus = &empty_cpu_map.map;
581 }
582
583 if (threads == NULL)
584 threads = &empty_thread_map.map;
585
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -0200586 return __perf_evsel__open(evsel, cpus, threads, group, group_fd);
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -0200587}
588
Arnaldo Carvalho de Melof08199d2011-01-11 23:42:19 -0200589int perf_evsel__open_per_cpu(struct perf_evsel *evsel,
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -0200590 struct cpu_map *cpus, bool group,
591 struct xyarray *group_fd)
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -0200592{
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -0200593 return __perf_evsel__open(evsel, cpus, &empty_thread_map.map, group,
594 group_fd);
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -0200595}
596
Arnaldo Carvalho de Melof08199d2011-01-11 23:42:19 -0200597int perf_evsel__open_per_thread(struct perf_evsel *evsel,
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -0200598 struct thread_map *threads, bool group,
599 struct xyarray *group_fd)
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -0200600{
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -0200601 return __perf_evsel__open(evsel, &empty_cpu_map.map, threads, group,
602 group_fd);
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -0200603}
Arnaldo Carvalho de Melo70082dd2011-01-12 17:03:24 -0200604
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200605static int perf_event__parse_id_sample(const union perf_event *event, u64 type,
Jiri Olsa37073f92012-05-30 14:23:44 +0200606 struct perf_sample *sample,
607 bool swapped)
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -0200608{
609 const u64 *array = event->sample.array;
Jiri Olsa37073f92012-05-30 14:23:44 +0200610 union u64_swap u;
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -0200611
612 array += ((event->header.size -
613 sizeof(event->header)) / sizeof(u64)) - 1;
614
615 if (type & PERF_SAMPLE_CPU) {
Jiri Olsa37073f92012-05-30 14:23:44 +0200616 u.val64 = *array;
617 if (swapped) {
618 /* undo swap of u64, then swap on individual u32s */
619 u.val64 = bswap_64(u.val64);
620 u.val32[0] = bswap_32(u.val32[0]);
621 }
622
623 sample->cpu = u.val32[0];
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -0200624 array--;
625 }
626
627 if (type & PERF_SAMPLE_STREAM_ID) {
628 sample->stream_id = *array;
629 array--;
630 }
631
632 if (type & PERF_SAMPLE_ID) {
633 sample->id = *array;
634 array--;
635 }
636
637 if (type & PERF_SAMPLE_TIME) {
638 sample->time = *array;
639 array--;
640 }
641
642 if (type & PERF_SAMPLE_TID) {
Jiri Olsa37073f92012-05-30 14:23:44 +0200643 u.val64 = *array;
644 if (swapped) {
645 /* undo swap of u64, then swap on individual u32s */
646 u.val64 = bswap_64(u.val64);
647 u.val32[0] = bswap_32(u.val32[0]);
648 u.val32[1] = bswap_32(u.val32[1]);
649 }
650
651 sample->pid = u.val32[0];
652 sample->tid = u.val32[1];
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -0200653 }
654
655 return 0;
656}
657
Frederic Weisbecker98e1da92011-05-21 20:08:15 +0200658static bool sample_overlap(const union perf_event *event,
659 const void *offset, u64 size)
660{
661 const void *base = event;
662
663 if (offset + size > base + event->header.size)
664 return true;
665
666 return false;
667}
668
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200669int perf_event__parse_sample(const union perf_event *event, u64 type,
Frederic Weisbeckera2854122011-05-21 19:33:04 +0200670 int sample_size, bool sample_id_all,
David Ahern936be502011-09-06 09:12:26 -0600671 struct perf_sample *data, bool swapped)
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -0200672{
673 const u64 *array;
674
David Ahern936be502011-09-06 09:12:26 -0600675 /*
676 * used for cross-endian analysis. See git commit 65014ab3
677 * for why this goofiness is needed.
678 */
Jiri Olsa6a11f922012-05-16 08:59:04 +0200679 union u64_swap u;
David Ahern936be502011-09-06 09:12:26 -0600680
Robert Richterf3bda2c2011-12-15 17:32:39 +0100681 memset(data, 0, sizeof(*data));
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -0200682 data->cpu = data->pid = data->tid = -1;
683 data->stream_id = data->id = data->time = -1ULL;
Naveen N. Raoa4a03fc2012-02-03 22:31:13 +0530684 data->period = 1;
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -0200685
686 if (event->header.type != PERF_RECORD_SAMPLE) {
687 if (!sample_id_all)
688 return 0;
Jiri Olsa37073f92012-05-30 14:23:44 +0200689 return perf_event__parse_id_sample(event, type, data, swapped);
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -0200690 }
691
692 array = event->sample.array;
693
Frederic Weisbeckera2854122011-05-21 19:33:04 +0200694 if (sample_size + sizeof(event->header) > event->header.size)
695 return -EFAULT;
696
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -0200697 if (type & PERF_SAMPLE_IP) {
698 data->ip = event->ip.ip;
699 array++;
700 }
701
702 if (type & PERF_SAMPLE_TID) {
David Ahern936be502011-09-06 09:12:26 -0600703 u.val64 = *array;
704 if (swapped) {
705 /* undo swap of u64, then swap on individual u32s */
706 u.val64 = bswap_64(u.val64);
707 u.val32[0] = bswap_32(u.val32[0]);
708 u.val32[1] = bswap_32(u.val32[1]);
709 }
710
711 data->pid = u.val32[0];
712 data->tid = u.val32[1];
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -0200713 array++;
714 }
715
716 if (type & PERF_SAMPLE_TIME) {
717 data->time = *array;
718 array++;
719 }
720
David Ahern7cec0922011-05-30 13:08:23 -0600721 data->addr = 0;
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -0200722 if (type & PERF_SAMPLE_ADDR) {
723 data->addr = *array;
724 array++;
725 }
726
727 data->id = -1ULL;
728 if (type & PERF_SAMPLE_ID) {
729 data->id = *array;
730 array++;
731 }
732
733 if (type & PERF_SAMPLE_STREAM_ID) {
734 data->stream_id = *array;
735 array++;
736 }
737
738 if (type & PERF_SAMPLE_CPU) {
David Ahern936be502011-09-06 09:12:26 -0600739
740 u.val64 = *array;
741 if (swapped) {
742 /* undo swap of u64, then swap on individual u32s */
743 u.val64 = bswap_64(u.val64);
744 u.val32[0] = bswap_32(u.val32[0]);
745 }
746
747 data->cpu = u.val32[0];
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -0200748 array++;
749 }
750
751 if (type & PERF_SAMPLE_PERIOD) {
752 data->period = *array;
753 array++;
754 }
755
756 if (type & PERF_SAMPLE_READ) {
Masanari Iidaf9d36992012-01-25 15:20:40 +0100757 fprintf(stderr, "PERF_SAMPLE_READ is unsupported for now\n");
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -0200758 return -1;
759 }
760
761 if (type & PERF_SAMPLE_CALLCHAIN) {
Frederic Weisbecker98e1da92011-05-21 20:08:15 +0200762 if (sample_overlap(event, array, sizeof(data->callchain->nr)))
763 return -EFAULT;
764
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -0200765 data->callchain = (struct ip_callchain *)array;
Frederic Weisbecker98e1da92011-05-21 20:08:15 +0200766
767 if (sample_overlap(event, array, data->callchain->nr))
768 return -EFAULT;
769
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -0200770 array += 1 + data->callchain->nr;
771 }
772
773 if (type & PERF_SAMPLE_RAW) {
Jiri Olsa8e303f22011-09-29 17:05:08 +0200774 const u64 *pdata;
775
David Ahern936be502011-09-06 09:12:26 -0600776 u.val64 = *array;
777 if (WARN_ONCE(swapped,
778 "Endianness of raw data not corrected!\n")) {
779 /* undo swap of u64, then swap on individual u32s */
780 u.val64 = bswap_64(u.val64);
781 u.val32[0] = bswap_32(u.val32[0]);
782 u.val32[1] = bswap_32(u.val32[1]);
783 }
Frederic Weisbecker98e1da92011-05-21 20:08:15 +0200784
785 if (sample_overlap(event, array, sizeof(u32)))
786 return -EFAULT;
787
David Ahern936be502011-09-06 09:12:26 -0600788 data->raw_size = u.val32[0];
Jiri Olsa8e303f22011-09-29 17:05:08 +0200789 pdata = (void *) array + sizeof(u32);
Frederic Weisbecker98e1da92011-05-21 20:08:15 +0200790
Jiri Olsa8e303f22011-09-29 17:05:08 +0200791 if (sample_overlap(event, pdata, data->raw_size))
Frederic Weisbecker98e1da92011-05-21 20:08:15 +0200792 return -EFAULT;
793
Jiri Olsa8e303f22011-09-29 17:05:08 +0200794 data->raw_data = (void *) pdata;
Stephane Eranianfa30c962012-03-17 23:23:18 +0100795
796 array = (void *)array + data->raw_size + sizeof(u32);
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -0200797 }
798
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100799 if (type & PERF_SAMPLE_BRANCH_STACK) {
800 u64 sz;
801
802 data->branch_stack = (struct branch_stack *)array;
803 array++; /* nr */
804
805 sz = data->branch_stack->nr * sizeof(struct branch_entry);
806 sz /= sizeof(u64);
807 array += sz;
808 }
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -0200809 return 0;
810}
Andrew Vagin74eec262011-11-28 12:03:31 +0300811
812int perf_event__synthesize_sample(union perf_event *event, u64 type,
813 const struct perf_sample *sample,
814 bool swapped)
815{
816 u64 *array;
817
818 /*
819 * used for cross-endian analysis. See git commit 65014ab3
820 * for why this goofiness is needed.
821 */
Jiri Olsa6a11f922012-05-16 08:59:04 +0200822 union u64_swap u;
Andrew Vagin74eec262011-11-28 12:03:31 +0300823
824 array = event->sample.array;
825
826 if (type & PERF_SAMPLE_IP) {
827 event->ip.ip = sample->ip;
828 array++;
829 }
830
831 if (type & PERF_SAMPLE_TID) {
832 u.val32[0] = sample->pid;
833 u.val32[1] = sample->tid;
834 if (swapped) {
835 /*
836 * Inverse of what is done in perf_event__parse_sample
837 */
838 u.val32[0] = bswap_32(u.val32[0]);
839 u.val32[1] = bswap_32(u.val32[1]);
840 u.val64 = bswap_64(u.val64);
841 }
842
843 *array = u.val64;
844 array++;
845 }
846
847 if (type & PERF_SAMPLE_TIME) {
848 *array = sample->time;
849 array++;
850 }
851
852 if (type & PERF_SAMPLE_ADDR) {
853 *array = sample->addr;
854 array++;
855 }
856
857 if (type & PERF_SAMPLE_ID) {
858 *array = sample->id;
859 array++;
860 }
861
862 if (type & PERF_SAMPLE_STREAM_ID) {
863 *array = sample->stream_id;
864 array++;
865 }
866
867 if (type & PERF_SAMPLE_CPU) {
868 u.val32[0] = sample->cpu;
869 if (swapped) {
870 /*
871 * Inverse of what is done in perf_event__parse_sample
872 */
873 u.val32[0] = bswap_32(u.val32[0]);
874 u.val64 = bswap_64(u.val64);
875 }
876 *array = u.val64;
877 array++;
878 }
879
880 if (type & PERF_SAMPLE_PERIOD) {
881 *array = sample->period;
882 array++;
883 }
884
885 return 0;
886}