blob: 2da04733117350a2fad52e2dab501c811c8819d6 [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 Melo335c2f52012-06-11 14:36:20 -0300131static const char *perf_evsel__sw_names[PERF_COUNT_SW_MAX] = {
132 "cpu-clock",
133 "task-clock",
134 "page-faults",
135 "context-switches",
136 "CPU-migrations",
137 "minor-faults",
138 "major-faults",
139 "alignment-faults",
140 "emulation-faults",
141};
142
143const char *__perf_evsel__sw_name(u64 config)
144{
145 if (config < PERF_COUNT_SW_MAX && perf_evsel__sw_names[config])
146 return perf_evsel__sw_names[config];
147 return "unknown-software";
148}
149
150static int perf_evsel__sw_name(struct perf_evsel *evsel, char *bf, size_t size)
151{
152 int r = scnprintf(bf, size, "%s", __perf_evsel__sw_name(evsel->attr.config));
153 return r + perf_evsel__add_modifiers(evsel, bf + r, size - r);
154}
155
Arnaldo Carvalho de Melo0b668bc2012-06-11 14:08:07 -0300156const char *perf_evsel__hw_cache[PERF_COUNT_HW_CACHE_MAX]
157 [PERF_EVSEL__MAX_ALIASES] = {
158 { "L1-dcache", "l1-d", "l1d", "L1-data", },
159 { "L1-icache", "l1-i", "l1i", "L1-instruction", },
160 { "LLC", "L2", },
161 { "dTLB", "d-tlb", "Data-TLB", },
162 { "iTLB", "i-tlb", "Instruction-TLB", },
163 { "branch", "branches", "bpu", "btb", "bpc", },
164 { "node", },
165};
166
167const char *perf_evsel__hw_cache_op[PERF_COUNT_HW_CACHE_OP_MAX]
168 [PERF_EVSEL__MAX_ALIASES] = {
169 { "load", "loads", "read", },
170 { "store", "stores", "write", },
171 { "prefetch", "prefetches", "speculative-read", "speculative-load", },
172};
173
174const char *perf_evsel__hw_cache_result[PERF_COUNT_HW_CACHE_RESULT_MAX]
175 [PERF_EVSEL__MAX_ALIASES] = {
176 { "refs", "Reference", "ops", "access", },
177 { "misses", "miss", },
178};
179
180#define C(x) PERF_COUNT_HW_CACHE_##x
181#define CACHE_READ (1 << C(OP_READ))
182#define CACHE_WRITE (1 << C(OP_WRITE))
183#define CACHE_PREFETCH (1 << C(OP_PREFETCH))
184#define COP(x) (1 << x)
185
186/*
187 * cache operartion stat
188 * L1I : Read and prefetch only
189 * ITLB and BPU : Read-only
190 */
191static unsigned long perf_evsel__hw_cache_stat[C(MAX)] = {
192 [C(L1D)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
193 [C(L1I)] = (CACHE_READ | CACHE_PREFETCH),
194 [C(LL)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
195 [C(DTLB)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
196 [C(ITLB)] = (CACHE_READ),
197 [C(BPU)] = (CACHE_READ),
198 [C(NODE)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
199};
200
201bool perf_evsel__is_cache_op_valid(u8 type, u8 op)
202{
203 if (perf_evsel__hw_cache_stat[type] & COP(op))
204 return true; /* valid */
205 else
206 return false; /* invalid */
207}
208
209int __perf_evsel__hw_cache_type_op_res_name(u8 type, u8 op, u8 result,
210 char *bf, size_t size)
211{
212 if (result) {
213 return scnprintf(bf, size, "%s-%s-%s", perf_evsel__hw_cache[type][0],
214 perf_evsel__hw_cache_op[op][0],
215 perf_evsel__hw_cache_result[result][0]);
216 }
217
218 return scnprintf(bf, size, "%s-%s", perf_evsel__hw_cache[type][0],
219 perf_evsel__hw_cache_op[op][1]);
220}
221
222int __perf_evsel__hw_cache_name(u64 config, char *bf, size_t size)
223{
224 u8 op, result, type = (config >> 0) & 0xff;
225 const char *err = "unknown-ext-hardware-cache-type";
226
227 if (type > PERF_COUNT_HW_CACHE_MAX)
228 goto out_err;
229
230 op = (config >> 8) & 0xff;
231 err = "unknown-ext-hardware-cache-op";
232 if (op > PERF_COUNT_HW_CACHE_OP_MAX)
233 goto out_err;
234
235 result = (config >> 16) & 0xff;
236 err = "unknown-ext-hardware-cache-result";
237 if (result > PERF_COUNT_HW_CACHE_RESULT_MAX)
238 goto out_err;
239
240 err = "invalid-cache";
241 if (!perf_evsel__is_cache_op_valid(type, op))
242 goto out_err;
243
244 return __perf_evsel__hw_cache_type_op_res_name(type, op, result, bf, size);
245out_err:
246 return scnprintf(bf, size, "%s", err);
247}
248
249static int perf_evsel__hw_cache_name(struct perf_evsel *evsel, char *bf, size_t size)
250{
251 int ret = __perf_evsel__hw_cache_name(evsel->attr.config, bf, size);
252 return ret + perf_evsel__add_modifiers(evsel, bf + ret, size - ret);
253}
254
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300255int perf_evsel__name(struct perf_evsel *evsel, char *bf, size_t size)
256{
257 int ret;
258
259 switch (evsel->attr.type) {
260 case PERF_TYPE_RAW:
261 ret = scnprintf(bf, size, "raw 0x%" PRIx64, evsel->attr.config);
262 break;
263
264 case PERF_TYPE_HARDWARE:
265 ret = perf_evsel__hw_name(evsel, bf, size);
266 break;
Arnaldo Carvalho de Melo0b668bc2012-06-11 14:08:07 -0300267
268 case PERF_TYPE_HW_CACHE:
269 ret = perf_evsel__hw_cache_name(evsel, bf, size);
270 break;
271
Arnaldo Carvalho de Melo335c2f52012-06-11 14:36:20 -0300272 case PERF_TYPE_SOFTWARE:
273 ret = perf_evsel__sw_name(evsel, bf, size);
274 break;
275
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300276 default:
277 /*
278 * FIXME
279 *
280 * This is the minimal perf_evsel__name so that we can
281 * reconstruct event names taking into account event modifiers.
282 *
283 * The old event_name uses it now for raw anr hw events, so that
284 * we don't drag all the parsing stuff into the python binding.
285 *
286 * On the next devel cycle the rest of the event naming will be
287 * brought here.
288 */
289 return 0;
290 }
291
292 return ret;
293}
294
Namhyung Kim5090c6a2012-03-16 17:42:20 +0900295void perf_evsel__config(struct perf_evsel *evsel, struct perf_record_opts *opts,
296 struct perf_evsel *first)
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200297{
298 struct perf_event_attr *attr = &evsel->attr;
299 int track = !evsel->idx; /* only the first counter needs these */
300
David Ahern5e1c81d2012-05-13 22:01:28 -0600301 attr->disabled = 1;
Arnaldo Carvalho de Melo808e1222012-02-14 14:18:57 -0200302 attr->sample_id_all = opts->sample_id_all_missing ? 0 : 1;
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200303 attr->inherit = !opts->no_inherit;
304 attr->read_format = PERF_FORMAT_TOTAL_TIME_ENABLED |
305 PERF_FORMAT_TOTAL_TIME_RUNNING |
306 PERF_FORMAT_ID;
307
308 attr->sample_type |= PERF_SAMPLE_IP | PERF_SAMPLE_TID;
309
310 /*
311 * We default some events to a 1 default interval. But keep
312 * it a weak assumption overridable by the user.
313 */
314 if (!attr->sample_period || (opts->user_freq != UINT_MAX &&
315 opts->user_interval != ULLONG_MAX)) {
316 if (opts->freq) {
317 attr->sample_type |= PERF_SAMPLE_PERIOD;
318 attr->freq = 1;
319 attr->sample_freq = opts->freq;
320 } else {
321 attr->sample_period = opts->default_interval;
322 }
323 }
324
325 if (opts->no_samples)
326 attr->sample_freq = 0;
327
328 if (opts->inherit_stat)
329 attr->inherit_stat = 1;
330
331 if (opts->sample_address) {
332 attr->sample_type |= PERF_SAMPLE_ADDR;
333 attr->mmap_data = track;
334 }
335
336 if (opts->call_graph)
337 attr->sample_type |= PERF_SAMPLE_CALLCHAIN;
338
Namhyung Kime40ee742012-05-21 10:42:07 +0900339 if (perf_target__has_cpu(&opts->target))
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200340 attr->sample_type |= PERF_SAMPLE_CPU;
341
Andrew Vagin3e76ac72011-12-20 17:32:45 +0300342 if (opts->period)
343 attr->sample_type |= PERF_SAMPLE_PERIOD;
344
Arnaldo Carvalho de Melo808e1222012-02-14 14:18:57 -0200345 if (!opts->sample_id_all_missing &&
Namhyung Kimd67356e2012-05-07 14:09:03 +0900346 (opts->sample_time || !opts->no_inherit ||
Namhyung Kimaa22dd42012-05-16 18:45:47 +0900347 perf_target__has_cpu(&opts->target)))
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200348 attr->sample_type |= PERF_SAMPLE_TIME;
349
350 if (opts->raw_samples) {
351 attr->sample_type |= PERF_SAMPLE_TIME;
352 attr->sample_type |= PERF_SAMPLE_RAW;
353 attr->sample_type |= PERF_SAMPLE_CPU;
354 }
355
356 if (opts->no_delay) {
357 attr->watermark = 0;
358 attr->wakeup_events = 1;
359 }
Roberto Agostino Vitillobdfebd82012-02-09 23:21:02 +0100360 if (opts->branch_stack) {
361 attr->sample_type |= PERF_SAMPLE_BRANCH_STACK;
362 attr->branch_sample_type = opts->branch_stack;
363 }
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200364
365 attr->mmap = track;
366 attr->comm = track;
367
Namhyung Kimd67356e2012-05-07 14:09:03 +0900368 if (perf_target__none(&opts->target) &&
369 (!opts->group || evsel == first)) {
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200370 attr->enable_on_exec = 1;
371 }
372}
373
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200374int perf_evsel__alloc_fd(struct perf_evsel *evsel, int ncpus, int nthreads)
375{
David Ahern4af4c952011-05-27 09:58:34 -0600376 int cpu, thread;
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200377 evsel->fd = xyarray__new(ncpus, nthreads, sizeof(int));
David Ahern4af4c952011-05-27 09:58:34 -0600378
379 if (evsel->fd) {
380 for (cpu = 0; cpu < ncpus; cpu++) {
381 for (thread = 0; thread < nthreads; thread++) {
382 FD(evsel, cpu, thread) = -1;
383 }
384 }
385 }
386
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200387 return evsel->fd != NULL ? 0 : -ENOMEM;
388}
389
Arnaldo Carvalho de Melo70db7532011-01-12 22:39:13 -0200390int perf_evsel__alloc_id(struct perf_evsel *evsel, int ncpus, int nthreads)
391{
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -0300392 evsel->sample_id = xyarray__new(ncpus, nthreads, sizeof(struct perf_sample_id));
393 if (evsel->sample_id == NULL)
394 return -ENOMEM;
395
396 evsel->id = zalloc(ncpus * nthreads * sizeof(u64));
397 if (evsel->id == NULL) {
398 xyarray__delete(evsel->sample_id);
399 evsel->sample_id = NULL;
400 return -ENOMEM;
401 }
402
403 return 0;
Arnaldo Carvalho de Melo70db7532011-01-12 22:39:13 -0200404}
405
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200406int perf_evsel__alloc_counts(struct perf_evsel *evsel, int ncpus)
407{
408 evsel->counts = zalloc((sizeof(*evsel->counts) +
409 (ncpus * sizeof(struct perf_counts_values))));
410 return evsel->counts != NULL ? 0 : -ENOMEM;
411}
412
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200413void perf_evsel__free_fd(struct perf_evsel *evsel)
414{
415 xyarray__delete(evsel->fd);
416 evsel->fd = NULL;
417}
418
Arnaldo Carvalho de Melo70db7532011-01-12 22:39:13 -0200419void perf_evsel__free_id(struct perf_evsel *evsel)
420{
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -0300421 xyarray__delete(evsel->sample_id);
422 evsel->sample_id = NULL;
423 free(evsel->id);
Arnaldo Carvalho de Melo70db7532011-01-12 22:39:13 -0200424 evsel->id = NULL;
425}
426
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200427void perf_evsel__close_fd(struct perf_evsel *evsel, int ncpus, int nthreads)
428{
429 int cpu, thread;
430
431 for (cpu = 0; cpu < ncpus; cpu++)
432 for (thread = 0; thread < nthreads; ++thread) {
433 close(FD(evsel, cpu, thread));
434 FD(evsel, cpu, thread) = -1;
435 }
436}
437
Arnaldo Carvalho de Meloef1d1af2011-01-18 21:41:45 -0200438void perf_evsel__exit(struct perf_evsel *evsel)
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200439{
440 assert(list_empty(&evsel->node));
441 xyarray__delete(evsel->fd);
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -0300442 xyarray__delete(evsel->sample_id);
443 free(evsel->id);
Arnaldo Carvalho de Meloef1d1af2011-01-18 21:41:45 -0200444}
445
446void perf_evsel__delete(struct perf_evsel *evsel)
447{
448 perf_evsel__exit(evsel);
Stephane Eranian023695d2011-02-14 11:20:01 +0200449 close_cgroup(evsel->cgrp);
Stephane Eranianf0c55bc2011-02-16 15:10:01 +0200450 free(evsel->name);
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200451 free(evsel);
452}
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200453
454int __perf_evsel__read_on_cpu(struct perf_evsel *evsel,
455 int cpu, int thread, bool scale)
456{
457 struct perf_counts_values count;
458 size_t nv = scale ? 3 : 1;
459
460 if (FD(evsel, cpu, thread) < 0)
461 return -EINVAL;
462
Arnaldo Carvalho de Melo4eed11d2011-01-04 00:13:17 -0200463 if (evsel->counts == NULL && perf_evsel__alloc_counts(evsel, cpu + 1) < 0)
464 return -ENOMEM;
465
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200466 if (readn(FD(evsel, cpu, thread), &count, nv * sizeof(u64)) < 0)
467 return -errno;
468
469 if (scale) {
470 if (count.run == 0)
471 count.val = 0;
472 else if (count.run < count.ena)
473 count.val = (u64)((double)count.val * count.ena / count.run + 0.5);
474 } else
475 count.ena = count.run = 0;
476
477 evsel->counts->cpu[cpu] = count;
478 return 0;
479}
480
481int __perf_evsel__read(struct perf_evsel *evsel,
482 int ncpus, int nthreads, bool scale)
483{
484 size_t nv = scale ? 3 : 1;
485 int cpu, thread;
486 struct perf_counts_values *aggr = &evsel->counts->aggr, count;
487
Arnaldo Carvalho de Melo52bcd9942011-02-03 17:26:06 -0200488 aggr->val = aggr->ena = aggr->run = 0;
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200489
490 for (cpu = 0; cpu < ncpus; cpu++) {
491 for (thread = 0; thread < nthreads; thread++) {
492 if (FD(evsel, cpu, thread) < 0)
493 continue;
494
495 if (readn(FD(evsel, cpu, thread),
496 &count, nv * sizeof(u64)) < 0)
497 return -errno;
498
499 aggr->val += count.val;
500 if (scale) {
501 aggr->ena += count.ena;
502 aggr->run += count.run;
503 }
504 }
505 }
506
507 evsel->counts->scaled = 0;
508 if (scale) {
509 if (aggr->run == 0) {
510 evsel->counts->scaled = -1;
511 aggr->val = 0;
512 return 0;
513 }
514
515 if (aggr->run < aggr->ena) {
516 evsel->counts->scaled = 1;
517 aggr->val = (u64)((double)aggr->val * aggr->ena / aggr->run + 0.5);
518 }
519 } else
520 aggr->ena = aggr->run = 0;
521
522 return 0;
523}
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -0200524
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -0200525static int __perf_evsel__open(struct perf_evsel *evsel, struct cpu_map *cpus,
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -0200526 struct thread_map *threads, bool group,
527 struct xyarray *group_fds)
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -0200528{
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -0200529 int cpu, thread;
Stephane Eranian023695d2011-02-14 11:20:01 +0200530 unsigned long flags = 0;
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -0200531 int pid = -1, err;
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -0200532
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -0200533 if (evsel->fd == NULL &&
534 perf_evsel__alloc_fd(evsel, cpus->nr, threads->nr) < 0)
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -0200535 return -ENOMEM;
Arnaldo Carvalho de Melo4eed11d2011-01-04 00:13:17 -0200536
Stephane Eranian023695d2011-02-14 11:20:01 +0200537 if (evsel->cgrp) {
538 flags = PERF_FLAG_PID_CGROUP;
539 pid = evsel->cgrp->fd;
540 }
541
Arnaldo Carvalho de Melo86bd5e82011-01-03 23:09:46 -0200542 for (cpu = 0; cpu < cpus->nr; cpu++) {
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -0200543 int group_fd = group_fds ? GROUP_FD(group_fds, cpu) : -1;
Arnaldo Carvalho de Melo9d04f172011-01-12 00:08:18 -0200544
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -0200545 for (thread = 0; thread < threads->nr; thread++) {
Stephane Eranian023695d2011-02-14 11:20:01 +0200546
547 if (!evsel->cgrp)
548 pid = threads->map[thread];
549
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -0200550 FD(evsel, cpu, thread) = sys_perf_event_open(&evsel->attr,
Stephane Eranian023695d2011-02-14 11:20:01 +0200551 pid,
Arnaldo Carvalho de Melof08199d2011-01-11 23:42:19 -0200552 cpus->map[cpu],
Stephane Eranian023695d2011-02-14 11:20:01 +0200553 group_fd, flags);
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -0200554 if (FD(evsel, cpu, thread) < 0) {
555 err = -errno;
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -0200556 goto out_close;
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -0200557 }
Arnaldo Carvalho de Melof08199d2011-01-11 23:42:19 -0200558
559 if (group && group_fd == -1)
560 group_fd = FD(evsel, cpu, thread);
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -0200561 }
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -0200562 }
563
564 return 0;
565
566out_close:
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -0200567 do {
568 while (--thread >= 0) {
569 close(FD(evsel, cpu, thread));
570 FD(evsel, cpu, thread) = -1;
571 }
572 thread = threads->nr;
573 } while (--cpu >= 0);
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -0200574 return err;
575}
576
577void perf_evsel__close(struct perf_evsel *evsel, int ncpus, int nthreads)
578{
579 if (evsel->fd == NULL)
580 return;
581
582 perf_evsel__close_fd(evsel, ncpus, nthreads);
583 perf_evsel__free_fd(evsel);
584 evsel->fd = NULL;
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -0200585}
586
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -0200587static struct {
588 struct cpu_map map;
589 int cpus[1];
590} empty_cpu_map = {
591 .map.nr = 1,
592 .cpus = { -1, },
593};
594
595static struct {
596 struct thread_map map;
597 int threads[1];
598} empty_thread_map = {
599 .map.nr = 1,
600 .threads = { -1, },
601};
602
Arnaldo Carvalho de Melof08199d2011-01-11 23:42:19 -0200603int perf_evsel__open(struct perf_evsel *evsel, struct cpu_map *cpus,
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -0200604 struct thread_map *threads, bool group,
605 struct xyarray *group_fd)
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -0200606{
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -0200607 if (cpus == NULL) {
608 /* Work around old compiler warnings about strict aliasing */
609 cpus = &empty_cpu_map.map;
610 }
611
612 if (threads == NULL)
613 threads = &empty_thread_map.map;
614
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -0200615 return __perf_evsel__open(evsel, cpus, threads, group, group_fd);
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -0200616}
617
Arnaldo Carvalho de Melof08199d2011-01-11 23:42:19 -0200618int perf_evsel__open_per_cpu(struct perf_evsel *evsel,
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -0200619 struct cpu_map *cpus, bool group,
620 struct xyarray *group_fd)
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -0200621{
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -0200622 return __perf_evsel__open(evsel, cpus, &empty_thread_map.map, group,
623 group_fd);
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -0200624}
625
Arnaldo Carvalho de Melof08199d2011-01-11 23:42:19 -0200626int perf_evsel__open_per_thread(struct perf_evsel *evsel,
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -0200627 struct thread_map *threads, bool group,
628 struct xyarray *group_fd)
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -0200629{
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -0200630 return __perf_evsel__open(evsel, &empty_cpu_map.map, threads, group,
631 group_fd);
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -0200632}
Arnaldo Carvalho de Melo70082dd2011-01-12 17:03:24 -0200633
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200634static int perf_event__parse_id_sample(const union perf_event *event, u64 type,
Jiri Olsa37073f92012-05-30 14:23:44 +0200635 struct perf_sample *sample,
636 bool swapped)
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -0200637{
638 const u64 *array = event->sample.array;
Jiri Olsa37073f92012-05-30 14:23:44 +0200639 union u64_swap u;
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -0200640
641 array += ((event->header.size -
642 sizeof(event->header)) / sizeof(u64)) - 1;
643
644 if (type & PERF_SAMPLE_CPU) {
Jiri Olsa37073f92012-05-30 14:23:44 +0200645 u.val64 = *array;
646 if (swapped) {
647 /* undo swap of u64, then swap on individual u32s */
648 u.val64 = bswap_64(u.val64);
649 u.val32[0] = bswap_32(u.val32[0]);
650 }
651
652 sample->cpu = u.val32[0];
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -0200653 array--;
654 }
655
656 if (type & PERF_SAMPLE_STREAM_ID) {
657 sample->stream_id = *array;
658 array--;
659 }
660
661 if (type & PERF_SAMPLE_ID) {
662 sample->id = *array;
663 array--;
664 }
665
666 if (type & PERF_SAMPLE_TIME) {
667 sample->time = *array;
668 array--;
669 }
670
671 if (type & PERF_SAMPLE_TID) {
Jiri Olsa37073f92012-05-30 14:23:44 +0200672 u.val64 = *array;
673 if (swapped) {
674 /* undo swap of u64, then swap on individual u32s */
675 u.val64 = bswap_64(u.val64);
676 u.val32[0] = bswap_32(u.val32[0]);
677 u.val32[1] = bswap_32(u.val32[1]);
678 }
679
680 sample->pid = u.val32[0];
681 sample->tid = u.val32[1];
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -0200682 }
683
684 return 0;
685}
686
Frederic Weisbecker98e1da92011-05-21 20:08:15 +0200687static bool sample_overlap(const union perf_event *event,
688 const void *offset, u64 size)
689{
690 const void *base = event;
691
692 if (offset + size > base + event->header.size)
693 return true;
694
695 return false;
696}
697
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200698int perf_event__parse_sample(const union perf_event *event, u64 type,
Frederic Weisbeckera2854122011-05-21 19:33:04 +0200699 int sample_size, bool sample_id_all,
David Ahern936be502011-09-06 09:12:26 -0600700 struct perf_sample *data, bool swapped)
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -0200701{
702 const u64 *array;
703
David Ahern936be502011-09-06 09:12:26 -0600704 /*
705 * used for cross-endian analysis. See git commit 65014ab3
706 * for why this goofiness is needed.
707 */
Jiri Olsa6a11f922012-05-16 08:59:04 +0200708 union u64_swap u;
David Ahern936be502011-09-06 09:12:26 -0600709
Robert Richterf3bda2c2011-12-15 17:32:39 +0100710 memset(data, 0, sizeof(*data));
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -0200711 data->cpu = data->pid = data->tid = -1;
712 data->stream_id = data->id = data->time = -1ULL;
Naveen N. Raoa4a03fc2012-02-03 22:31:13 +0530713 data->period = 1;
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -0200714
715 if (event->header.type != PERF_RECORD_SAMPLE) {
716 if (!sample_id_all)
717 return 0;
Jiri Olsa37073f92012-05-30 14:23:44 +0200718 return perf_event__parse_id_sample(event, type, data, swapped);
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -0200719 }
720
721 array = event->sample.array;
722
Frederic Weisbeckera2854122011-05-21 19:33:04 +0200723 if (sample_size + sizeof(event->header) > event->header.size)
724 return -EFAULT;
725
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -0200726 if (type & PERF_SAMPLE_IP) {
727 data->ip = event->ip.ip;
728 array++;
729 }
730
731 if (type & PERF_SAMPLE_TID) {
David Ahern936be502011-09-06 09:12:26 -0600732 u.val64 = *array;
733 if (swapped) {
734 /* undo swap of u64, then swap on individual u32s */
735 u.val64 = bswap_64(u.val64);
736 u.val32[0] = bswap_32(u.val32[0]);
737 u.val32[1] = bswap_32(u.val32[1]);
738 }
739
740 data->pid = u.val32[0];
741 data->tid = u.val32[1];
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -0200742 array++;
743 }
744
745 if (type & PERF_SAMPLE_TIME) {
746 data->time = *array;
747 array++;
748 }
749
David Ahern7cec0922011-05-30 13:08:23 -0600750 data->addr = 0;
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -0200751 if (type & PERF_SAMPLE_ADDR) {
752 data->addr = *array;
753 array++;
754 }
755
756 data->id = -1ULL;
757 if (type & PERF_SAMPLE_ID) {
758 data->id = *array;
759 array++;
760 }
761
762 if (type & PERF_SAMPLE_STREAM_ID) {
763 data->stream_id = *array;
764 array++;
765 }
766
767 if (type & PERF_SAMPLE_CPU) {
David Ahern936be502011-09-06 09:12:26 -0600768
769 u.val64 = *array;
770 if (swapped) {
771 /* undo swap of u64, then swap on individual u32s */
772 u.val64 = bswap_64(u.val64);
773 u.val32[0] = bswap_32(u.val32[0]);
774 }
775
776 data->cpu = u.val32[0];
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -0200777 array++;
778 }
779
780 if (type & PERF_SAMPLE_PERIOD) {
781 data->period = *array;
782 array++;
783 }
784
785 if (type & PERF_SAMPLE_READ) {
Masanari Iidaf9d36992012-01-25 15:20:40 +0100786 fprintf(stderr, "PERF_SAMPLE_READ is unsupported for now\n");
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -0200787 return -1;
788 }
789
790 if (type & PERF_SAMPLE_CALLCHAIN) {
Frederic Weisbecker98e1da92011-05-21 20:08:15 +0200791 if (sample_overlap(event, array, sizeof(data->callchain->nr)))
792 return -EFAULT;
793
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -0200794 data->callchain = (struct ip_callchain *)array;
Frederic Weisbecker98e1da92011-05-21 20:08:15 +0200795
796 if (sample_overlap(event, array, data->callchain->nr))
797 return -EFAULT;
798
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -0200799 array += 1 + data->callchain->nr;
800 }
801
802 if (type & PERF_SAMPLE_RAW) {
Jiri Olsa8e303f22011-09-29 17:05:08 +0200803 const u64 *pdata;
804
David Ahern936be502011-09-06 09:12:26 -0600805 u.val64 = *array;
806 if (WARN_ONCE(swapped,
807 "Endianness of raw data not corrected!\n")) {
808 /* undo swap of u64, then swap on individual u32s */
809 u.val64 = bswap_64(u.val64);
810 u.val32[0] = bswap_32(u.val32[0]);
811 u.val32[1] = bswap_32(u.val32[1]);
812 }
Frederic Weisbecker98e1da92011-05-21 20:08:15 +0200813
814 if (sample_overlap(event, array, sizeof(u32)))
815 return -EFAULT;
816
David Ahern936be502011-09-06 09:12:26 -0600817 data->raw_size = u.val32[0];
Jiri Olsa8e303f22011-09-29 17:05:08 +0200818 pdata = (void *) array + sizeof(u32);
Frederic Weisbecker98e1da92011-05-21 20:08:15 +0200819
Jiri Olsa8e303f22011-09-29 17:05:08 +0200820 if (sample_overlap(event, pdata, data->raw_size))
Frederic Weisbecker98e1da92011-05-21 20:08:15 +0200821 return -EFAULT;
822
Jiri Olsa8e303f22011-09-29 17:05:08 +0200823 data->raw_data = (void *) pdata;
Stephane Eranianfa30c962012-03-17 23:23:18 +0100824
825 array = (void *)array + data->raw_size + sizeof(u32);
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -0200826 }
827
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100828 if (type & PERF_SAMPLE_BRANCH_STACK) {
829 u64 sz;
830
831 data->branch_stack = (struct branch_stack *)array;
832 array++; /* nr */
833
834 sz = data->branch_stack->nr * sizeof(struct branch_entry);
835 sz /= sizeof(u64);
836 array += sz;
837 }
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -0200838 return 0;
839}
Andrew Vagin74eec262011-11-28 12:03:31 +0300840
841int perf_event__synthesize_sample(union perf_event *event, u64 type,
842 const struct perf_sample *sample,
843 bool swapped)
844{
845 u64 *array;
846
847 /*
848 * used for cross-endian analysis. See git commit 65014ab3
849 * for why this goofiness is needed.
850 */
Jiri Olsa6a11f922012-05-16 08:59:04 +0200851 union u64_swap u;
Andrew Vagin74eec262011-11-28 12:03:31 +0300852
853 array = event->sample.array;
854
855 if (type & PERF_SAMPLE_IP) {
856 event->ip.ip = sample->ip;
857 array++;
858 }
859
860 if (type & PERF_SAMPLE_TID) {
861 u.val32[0] = sample->pid;
862 u.val32[1] = sample->tid;
863 if (swapped) {
864 /*
865 * Inverse of what is done in perf_event__parse_sample
866 */
867 u.val32[0] = bswap_32(u.val32[0]);
868 u.val32[1] = bswap_32(u.val32[1]);
869 u.val64 = bswap_64(u.val64);
870 }
871
872 *array = u.val64;
873 array++;
874 }
875
876 if (type & PERF_SAMPLE_TIME) {
877 *array = sample->time;
878 array++;
879 }
880
881 if (type & PERF_SAMPLE_ADDR) {
882 *array = sample->addr;
883 array++;
884 }
885
886 if (type & PERF_SAMPLE_ID) {
887 *array = sample->id;
888 array++;
889 }
890
891 if (type & PERF_SAMPLE_STREAM_ID) {
892 *array = sample->stream_id;
893 array++;
894 }
895
896 if (type & PERF_SAMPLE_CPU) {
897 u.val32[0] = sample->cpu;
898 if (swapped) {
899 /*
900 * Inverse of what is done in perf_event__parse_sample
901 */
902 u.val32[0] = bswap_32(u.val32[0]);
903 u.val64 = bswap_64(u.val64);
904 }
905 *array = u.val64;
906 array++;
907 }
908
909 if (type & PERF_SAMPLE_PERIOD) {
910 *array = sample->period;
911 array++;
912 }
913
914 return 0;
915}