blob: 1d826d63bc20c9f33d66655d3d6639b6a42d5d7e [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>
Jiri Olsa0f6a3012012-08-07 15:20:45 +020011#include <linux/bitops.h>
Borislav Petkov553873e2013-12-09 17:14:23 +010012#include <api/fs/debugfs.h>
Robert Richter4e319022013-06-11 17:29:18 +020013#include <traceevent/event-parse.h>
14#include <linux/hw_breakpoint.h>
15#include <linux/perf_event.h>
Andi Kleenbec19672013-08-04 19:41:26 -070016#include <sys/resource.h>
Robert Richter4e319022013-06-11 17:29:18 +020017#include "asm/bug.h"
Arnaldo Carvalho de Melo8f651ea2014-10-09 16:12:24 -030018#include "callchain.h"
Arnaldo Carvalho de Melof14d5702014-10-17 12:17:40 -030019#include "cgroup.h"
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -020020#include "evsel.h"
Arnaldo Carvalho de Melo70082dd2011-01-12 17:03:24 -020021#include "evlist.h"
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -020022#include "util.h"
Arnaldo Carvalho de Melo86bd5e82011-01-03 23:09:46 -020023#include "cpumap.h"
Arnaldo Carvalho de Melofd782602011-01-18 15:15:24 -020024#include "thread_map.h"
Namhyung Kim12864b32012-04-26 14:15:22 +090025#include "target.h"
Jiri Olsa26d33022012-08-07 15:20:47 +020026#include "perf_regs.h"
Adrian Huntere3e1a542013-08-14 15:48:24 +030027#include "debug.h"
Jiri Olsa97978b32013-12-03 14:09:24 +010028#include "trace-event.h"
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -020029
Arnaldo Carvalho de Melo594ac612012-12-13 13:13:07 -030030static struct {
31 bool sample_id_all;
32 bool exclude_guest;
Stephane Eranian5c5e8542013-08-21 12:10:25 +020033 bool mmap2;
Yann Droneaud57480d22014-06-30 22:28:47 +020034 bool cloexec;
Arnaldo Carvalho de Melo594ac612012-12-13 13:13:07 -030035} perf_missing_features;
36
Arnaldo Carvalho de Meloce8ccff2014-10-09 15:29:51 -030037static int perf_evsel__no_extra_init(struct perf_evsel *evsel __maybe_unused)
38{
39 return 0;
40}
41
42static void perf_evsel__no_extra_fini(struct perf_evsel *evsel __maybe_unused)
43{
44}
45
46static struct {
47 size_t size;
48 int (*init)(struct perf_evsel *evsel);
49 void (*fini)(struct perf_evsel *evsel);
50} perf_evsel__object = {
51 .size = sizeof(struct perf_evsel),
52 .init = perf_evsel__no_extra_init,
53 .fini = perf_evsel__no_extra_fini,
54};
55
56int perf_evsel__object_config(size_t object_size,
57 int (*init)(struct perf_evsel *evsel),
58 void (*fini)(struct perf_evsel *evsel))
59{
60
61 if (object_size == 0)
62 goto set_methods;
63
64 if (perf_evsel__object.size > object_size)
65 return -EINVAL;
66
67 perf_evsel__object.size = object_size;
68
69set_methods:
70 if (init != NULL)
71 perf_evsel__object.init = init;
72
73 if (fini != NULL)
74 perf_evsel__object.fini = fini;
75
76 return 0;
77}
78
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -020079#define FD(e, x, y) (*(int *)xyarray__entry(e->fd, x, y))
80
Adrian Hunter75562572013-08-27 11:23:09 +030081int __perf_evsel__sample_size(u64 sample_type)
Arnaldo Carvalho de Meloc2a70652011-06-02 11:04:54 -030082{
83 u64 mask = sample_type & PERF_SAMPLE_MASK;
84 int size = 0;
85 int i;
86
87 for (i = 0; i < 64; i++) {
88 if (mask & (1ULL << i))
89 size++;
90 }
91
92 size *= sizeof(u64);
93
94 return size;
95}
96
Adrian Hunter75562572013-08-27 11:23:09 +030097/**
98 * __perf_evsel__calc_id_pos - calculate id_pos.
99 * @sample_type: sample type
100 *
101 * This function returns the position of the event id (PERF_SAMPLE_ID or
102 * PERF_SAMPLE_IDENTIFIER) in a sample event i.e. in the array of struct
103 * sample_event.
104 */
105static int __perf_evsel__calc_id_pos(u64 sample_type)
106{
107 int idx = 0;
108
109 if (sample_type & PERF_SAMPLE_IDENTIFIER)
110 return 0;
111
112 if (!(sample_type & PERF_SAMPLE_ID))
113 return -1;
114
115 if (sample_type & PERF_SAMPLE_IP)
116 idx += 1;
117
118 if (sample_type & PERF_SAMPLE_TID)
119 idx += 1;
120
121 if (sample_type & PERF_SAMPLE_TIME)
122 idx += 1;
123
124 if (sample_type & PERF_SAMPLE_ADDR)
125 idx += 1;
126
127 return idx;
128}
129
130/**
131 * __perf_evsel__calc_is_pos - calculate is_pos.
132 * @sample_type: sample type
133 *
134 * This function returns the position (counting backwards) of the event id
135 * (PERF_SAMPLE_ID or PERF_SAMPLE_IDENTIFIER) in a non-sample event i.e. if
136 * sample_id_all is used there is an id sample appended to non-sample events.
137 */
138static int __perf_evsel__calc_is_pos(u64 sample_type)
139{
140 int idx = 1;
141
142 if (sample_type & PERF_SAMPLE_IDENTIFIER)
143 return 1;
144
145 if (!(sample_type & PERF_SAMPLE_ID))
146 return -1;
147
148 if (sample_type & PERF_SAMPLE_CPU)
149 idx += 1;
150
151 if (sample_type & PERF_SAMPLE_STREAM_ID)
152 idx += 1;
153
154 return idx;
155}
156
157void perf_evsel__calc_id_pos(struct perf_evsel *evsel)
158{
159 evsel->id_pos = __perf_evsel__calc_id_pos(evsel->attr.sample_type);
160 evsel->is_pos = __perf_evsel__calc_is_pos(evsel->attr.sample_type);
161}
162
Arnaldo Carvalho de Melo7be5ebe2012-12-10 14:53:43 -0300163void __perf_evsel__set_sample_bit(struct perf_evsel *evsel,
164 enum perf_event_sample_format bit)
165{
166 if (!(evsel->attr.sample_type & bit)) {
167 evsel->attr.sample_type |= bit;
168 evsel->sample_size += sizeof(u64);
Adrian Hunter75562572013-08-27 11:23:09 +0300169 perf_evsel__calc_id_pos(evsel);
Arnaldo Carvalho de Melo7be5ebe2012-12-10 14:53:43 -0300170 }
171}
172
173void __perf_evsel__reset_sample_bit(struct perf_evsel *evsel,
174 enum perf_event_sample_format bit)
175{
176 if (evsel->attr.sample_type & bit) {
177 evsel->attr.sample_type &= ~bit;
178 evsel->sample_size -= sizeof(u64);
Adrian Hunter75562572013-08-27 11:23:09 +0300179 perf_evsel__calc_id_pos(evsel);
Arnaldo Carvalho de Melo7be5ebe2012-12-10 14:53:43 -0300180 }
181}
182
Adrian Hunter75562572013-08-27 11:23:09 +0300183void perf_evsel__set_sample_id(struct perf_evsel *evsel,
184 bool can_sample_identifier)
Arnaldo Carvalho de Melo7a5a5ca2012-12-10 15:21:30 -0300185{
Adrian Hunter75562572013-08-27 11:23:09 +0300186 if (can_sample_identifier) {
187 perf_evsel__reset_sample_bit(evsel, ID);
188 perf_evsel__set_sample_bit(evsel, IDENTIFIER);
189 } else {
190 perf_evsel__set_sample_bit(evsel, ID);
191 }
Arnaldo Carvalho de Melo7a5a5ca2012-12-10 15:21:30 -0300192 evsel->attr.read_format |= PERF_FORMAT_ID;
193}
194
Arnaldo Carvalho de Meloef1d1af2011-01-18 21:41:45 -0200195void perf_evsel__init(struct perf_evsel *evsel,
196 struct perf_event_attr *attr, int idx)
197{
198 evsel->idx = idx;
Adrian Hunter60b08962014-07-31 09:00:52 +0300199 evsel->tracking = !idx;
Arnaldo Carvalho de Meloef1d1af2011-01-18 21:41:45 -0200200 evsel->attr = *attr;
Namhyung Kim2cfda562012-11-29 15:38:29 +0900201 evsel->leader = evsel;
Stephane Eranian410136f2013-11-12 17:58:49 +0100202 evsel->unit = "";
203 evsel->scale = 1.0;
Arnaldo Carvalho de Meloef1d1af2011-01-18 21:41:45 -0200204 INIT_LIST_HEAD(&evsel->node);
Arnaldo Carvalho de Meloce8ccff2014-10-09 15:29:51 -0300205 perf_evsel__object.init(evsel);
Arnaldo Carvalho de Melobde09462012-08-01 18:53:11 -0300206 evsel->sample_size = __perf_evsel__sample_size(attr->sample_type);
Adrian Hunter75562572013-08-27 11:23:09 +0300207 perf_evsel__calc_id_pos(evsel);
Arnaldo Carvalho de Meloef1d1af2011-01-18 21:41:45 -0200208}
209
Arnaldo Carvalho de Meloef503832013-11-07 16:41:19 -0300210struct perf_evsel *perf_evsel__new_idx(struct perf_event_attr *attr, int idx)
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200211{
Arnaldo Carvalho de Meloce8ccff2014-10-09 15:29:51 -0300212 struct perf_evsel *evsel = zalloc(perf_evsel__object.size);
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200213
Arnaldo Carvalho de Meloef1d1af2011-01-18 21:41:45 -0200214 if (evsel != NULL)
215 perf_evsel__init(evsel, attr, idx);
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200216
217 return evsel;
218}
219
Arnaldo Carvalho de Meloef503832013-11-07 16:41:19 -0300220struct perf_evsel *perf_evsel__newtp_idx(const char *sys, const char *name, int idx)
Arnaldo Carvalho de Meloefd2b922012-09-18 11:21:50 -0300221{
Arnaldo Carvalho de Meloce8ccff2014-10-09 15:29:51 -0300222 struct perf_evsel *evsel = zalloc(perf_evsel__object.size);
Arnaldo Carvalho de Meloefd2b922012-09-18 11:21:50 -0300223
224 if (evsel != NULL) {
225 struct perf_event_attr attr = {
Arnaldo Carvalho de Melo0b80f8b32012-09-26 12:28:26 -0300226 .type = PERF_TYPE_TRACEPOINT,
227 .sample_type = (PERF_SAMPLE_RAW | PERF_SAMPLE_TIME |
228 PERF_SAMPLE_CPU | PERF_SAMPLE_PERIOD),
Arnaldo Carvalho de Meloefd2b922012-09-18 11:21:50 -0300229 };
230
Arnaldo Carvalho de Meloe48ffe22012-09-26 17:11:38 -0300231 if (asprintf(&evsel->name, "%s:%s", sys, name) < 0)
232 goto out_free;
233
Jiri Olsa97978b32013-12-03 14:09:24 +0100234 evsel->tp_format = trace_event__tp_format(sys, name);
Arnaldo Carvalho de Meloefd2b922012-09-18 11:21:50 -0300235 if (evsel->tp_format == NULL)
236 goto out_free;
237
Arnaldo Carvalho de Melo0b80f8b32012-09-26 12:28:26 -0300238 event_attr_init(&attr);
Arnaldo Carvalho de Meloefd2b922012-09-18 11:21:50 -0300239 attr.config = evsel->tp_format->id;
Arnaldo Carvalho de Melo0b80f8b32012-09-26 12:28:26 -0300240 attr.sample_period = 1;
Arnaldo Carvalho de Meloefd2b922012-09-18 11:21:50 -0300241 perf_evsel__init(evsel, &attr, idx);
Arnaldo Carvalho de Meloefd2b922012-09-18 11:21:50 -0300242 }
243
244 return evsel;
245
246out_free:
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -0300247 zfree(&evsel->name);
Arnaldo Carvalho de Meloefd2b922012-09-18 11:21:50 -0300248 free(evsel);
249 return NULL;
250}
251
Arnaldo Carvalho de Melo8ad70132012-09-06 13:11:18 -0300252const char *perf_evsel__hw_names[PERF_COUNT_HW_MAX] = {
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300253 "cycles",
254 "instructions",
255 "cache-references",
256 "cache-misses",
257 "branches",
258 "branch-misses",
259 "bus-cycles",
260 "stalled-cycles-frontend",
261 "stalled-cycles-backend",
262 "ref-cycles",
263};
264
Arnaldo Carvalho de Melodd4f5222012-06-13 15:52:42 -0300265static const char *__perf_evsel__hw_name(u64 config)
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300266{
267 if (config < PERF_COUNT_HW_MAX && perf_evsel__hw_names[config])
268 return perf_evsel__hw_names[config];
269
270 return "unknown-hardware";
271}
272
Arnaldo Carvalho de Melo27f18612012-06-11 13:33:09 -0300273static int perf_evsel__add_modifiers(struct perf_evsel *evsel, char *bf, size_t size)
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300274{
Arnaldo Carvalho de Melo27f18612012-06-11 13:33:09 -0300275 int colon = 0, r = 0;
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300276 struct perf_event_attr *attr = &evsel->attr;
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300277 bool exclude_guest_default = false;
278
279#define MOD_PRINT(context, mod) do { \
280 if (!attr->exclude_##context) { \
Arnaldo Carvalho de Melo27f18612012-06-11 13:33:09 -0300281 if (!colon) colon = ++r; \
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300282 r += scnprintf(bf + r, size - r, "%c", mod); \
283 } } while(0)
284
285 if (attr->exclude_kernel || attr->exclude_user || attr->exclude_hv) {
286 MOD_PRINT(kernel, 'k');
287 MOD_PRINT(user, 'u');
288 MOD_PRINT(hv, 'h');
289 exclude_guest_default = true;
290 }
291
292 if (attr->precise_ip) {
293 if (!colon)
Arnaldo Carvalho de Melo27f18612012-06-11 13:33:09 -0300294 colon = ++r;
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300295 r += scnprintf(bf + r, size - r, "%.*s", attr->precise_ip, "ppp");
296 exclude_guest_default = true;
297 }
298
299 if (attr->exclude_host || attr->exclude_guest == exclude_guest_default) {
300 MOD_PRINT(host, 'H');
301 MOD_PRINT(guest, 'G');
302 }
303#undef MOD_PRINT
304 if (colon)
Arnaldo Carvalho de Melo27f18612012-06-11 13:33:09 -0300305 bf[colon - 1] = ':';
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300306 return r;
307}
308
Arnaldo Carvalho de Melo27f18612012-06-11 13:33:09 -0300309static int perf_evsel__hw_name(struct perf_evsel *evsel, char *bf, size_t size)
310{
311 int r = scnprintf(bf, size, "%s", __perf_evsel__hw_name(evsel->attr.config));
312 return r + perf_evsel__add_modifiers(evsel, bf + r, size - r);
313}
314
Arnaldo Carvalho de Melo8ad70132012-09-06 13:11:18 -0300315const char *perf_evsel__sw_names[PERF_COUNT_SW_MAX] = {
Arnaldo Carvalho de Melo335c2f52012-06-11 14:36:20 -0300316 "cpu-clock",
317 "task-clock",
318 "page-faults",
319 "context-switches",
Arnaldo Carvalho de Melo8ad70132012-09-06 13:11:18 -0300320 "cpu-migrations",
Arnaldo Carvalho de Melo335c2f52012-06-11 14:36:20 -0300321 "minor-faults",
322 "major-faults",
323 "alignment-faults",
324 "emulation-faults",
Adrian Hunterd22d1a22013-08-31 21:50:52 +0300325 "dummy",
Arnaldo Carvalho de Melo335c2f52012-06-11 14:36:20 -0300326};
327
Arnaldo Carvalho de Melodd4f5222012-06-13 15:52:42 -0300328static const char *__perf_evsel__sw_name(u64 config)
Arnaldo Carvalho de Melo335c2f52012-06-11 14:36:20 -0300329{
330 if (config < PERF_COUNT_SW_MAX && perf_evsel__sw_names[config])
331 return perf_evsel__sw_names[config];
332 return "unknown-software";
333}
334
335static int perf_evsel__sw_name(struct perf_evsel *evsel, char *bf, size_t size)
336{
337 int r = scnprintf(bf, size, "%s", __perf_evsel__sw_name(evsel->attr.config));
338 return r + perf_evsel__add_modifiers(evsel, bf + r, size - r);
339}
340
Jiri Olsa287e74a2012-06-28 23:18:49 +0200341static int __perf_evsel__bp_name(char *bf, size_t size, u64 addr, u64 type)
342{
343 int r;
344
345 r = scnprintf(bf, size, "mem:0x%" PRIx64 ":", addr);
346
347 if (type & HW_BREAKPOINT_R)
348 r += scnprintf(bf + r, size - r, "r");
349
350 if (type & HW_BREAKPOINT_W)
351 r += scnprintf(bf + r, size - r, "w");
352
353 if (type & HW_BREAKPOINT_X)
354 r += scnprintf(bf + r, size - r, "x");
355
356 return r;
357}
358
359static int perf_evsel__bp_name(struct perf_evsel *evsel, char *bf, size_t size)
360{
361 struct perf_event_attr *attr = &evsel->attr;
362 int r = __perf_evsel__bp_name(bf, size, attr->bp_addr, attr->bp_type);
363 return r + perf_evsel__add_modifiers(evsel, bf + r, size - r);
364}
365
Arnaldo Carvalho de Melo0b668bc2012-06-11 14:08:07 -0300366const char *perf_evsel__hw_cache[PERF_COUNT_HW_CACHE_MAX]
367 [PERF_EVSEL__MAX_ALIASES] = {
368 { "L1-dcache", "l1-d", "l1d", "L1-data", },
369 { "L1-icache", "l1-i", "l1i", "L1-instruction", },
370 { "LLC", "L2", },
371 { "dTLB", "d-tlb", "Data-TLB", },
372 { "iTLB", "i-tlb", "Instruction-TLB", },
373 { "branch", "branches", "bpu", "btb", "bpc", },
374 { "node", },
375};
376
377const char *perf_evsel__hw_cache_op[PERF_COUNT_HW_CACHE_OP_MAX]
378 [PERF_EVSEL__MAX_ALIASES] = {
379 { "load", "loads", "read", },
380 { "store", "stores", "write", },
381 { "prefetch", "prefetches", "speculative-read", "speculative-load", },
382};
383
384const char *perf_evsel__hw_cache_result[PERF_COUNT_HW_CACHE_RESULT_MAX]
385 [PERF_EVSEL__MAX_ALIASES] = {
386 { "refs", "Reference", "ops", "access", },
387 { "misses", "miss", },
388};
389
390#define C(x) PERF_COUNT_HW_CACHE_##x
391#define CACHE_READ (1 << C(OP_READ))
392#define CACHE_WRITE (1 << C(OP_WRITE))
393#define CACHE_PREFETCH (1 << C(OP_PREFETCH))
394#define COP(x) (1 << x)
395
396/*
397 * cache operartion stat
398 * L1I : Read and prefetch only
399 * ITLB and BPU : Read-only
400 */
401static unsigned long perf_evsel__hw_cache_stat[C(MAX)] = {
402 [C(L1D)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
403 [C(L1I)] = (CACHE_READ | CACHE_PREFETCH),
404 [C(LL)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
405 [C(DTLB)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
406 [C(ITLB)] = (CACHE_READ),
407 [C(BPU)] = (CACHE_READ),
408 [C(NODE)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
409};
410
411bool perf_evsel__is_cache_op_valid(u8 type, u8 op)
412{
413 if (perf_evsel__hw_cache_stat[type] & COP(op))
414 return true; /* valid */
415 else
416 return false; /* invalid */
417}
418
419int __perf_evsel__hw_cache_type_op_res_name(u8 type, u8 op, u8 result,
420 char *bf, size_t size)
421{
422 if (result) {
423 return scnprintf(bf, size, "%s-%s-%s", perf_evsel__hw_cache[type][0],
424 perf_evsel__hw_cache_op[op][0],
425 perf_evsel__hw_cache_result[result][0]);
426 }
427
428 return scnprintf(bf, size, "%s-%s", perf_evsel__hw_cache[type][0],
429 perf_evsel__hw_cache_op[op][1]);
430}
431
Arnaldo Carvalho de Melodd4f5222012-06-13 15:52:42 -0300432static int __perf_evsel__hw_cache_name(u64 config, char *bf, size_t size)
Arnaldo Carvalho de Melo0b668bc2012-06-11 14:08:07 -0300433{
434 u8 op, result, type = (config >> 0) & 0xff;
435 const char *err = "unknown-ext-hardware-cache-type";
436
437 if (type > PERF_COUNT_HW_CACHE_MAX)
438 goto out_err;
439
440 op = (config >> 8) & 0xff;
441 err = "unknown-ext-hardware-cache-op";
442 if (op > PERF_COUNT_HW_CACHE_OP_MAX)
443 goto out_err;
444
445 result = (config >> 16) & 0xff;
446 err = "unknown-ext-hardware-cache-result";
447 if (result > PERF_COUNT_HW_CACHE_RESULT_MAX)
448 goto out_err;
449
450 err = "invalid-cache";
451 if (!perf_evsel__is_cache_op_valid(type, op))
452 goto out_err;
453
454 return __perf_evsel__hw_cache_type_op_res_name(type, op, result, bf, size);
455out_err:
456 return scnprintf(bf, size, "%s", err);
457}
458
459static int perf_evsel__hw_cache_name(struct perf_evsel *evsel, char *bf, size_t size)
460{
461 int ret = __perf_evsel__hw_cache_name(evsel->attr.config, bf, size);
462 return ret + perf_evsel__add_modifiers(evsel, bf + ret, size - ret);
463}
464
Arnaldo Carvalho de Melo6eef3d92012-06-13 11:53:37 -0300465static int perf_evsel__raw_name(struct perf_evsel *evsel, char *bf, size_t size)
466{
467 int ret = scnprintf(bf, size, "raw 0x%" PRIx64, evsel->attr.config);
468 return ret + perf_evsel__add_modifiers(evsel, bf + ret, size - ret);
469}
470
Arnaldo Carvalho de Melo7289f832012-06-12 12:34:58 -0300471const char *perf_evsel__name(struct perf_evsel *evsel)
Arnaldo Carvalho de Meloa4460832012-06-12 10:29:12 -0300472{
Arnaldo Carvalho de Melo7289f832012-06-12 12:34:58 -0300473 char bf[128];
Arnaldo Carvalho de Meloa4460832012-06-12 10:29:12 -0300474
Arnaldo Carvalho de Melo7289f832012-06-12 12:34:58 -0300475 if (evsel->name)
476 return evsel->name;
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300477
478 switch (evsel->attr.type) {
479 case PERF_TYPE_RAW:
Arnaldo Carvalho de Melo6eef3d92012-06-13 11:53:37 -0300480 perf_evsel__raw_name(evsel, bf, sizeof(bf));
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300481 break;
482
483 case PERF_TYPE_HARDWARE:
Arnaldo Carvalho de Melo7289f832012-06-12 12:34:58 -0300484 perf_evsel__hw_name(evsel, bf, sizeof(bf));
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300485 break;
Arnaldo Carvalho de Melo0b668bc2012-06-11 14:08:07 -0300486
487 case PERF_TYPE_HW_CACHE:
Arnaldo Carvalho de Melo7289f832012-06-12 12:34:58 -0300488 perf_evsel__hw_cache_name(evsel, bf, sizeof(bf));
Arnaldo Carvalho de Melo0b668bc2012-06-11 14:08:07 -0300489 break;
490
Arnaldo Carvalho de Melo335c2f52012-06-11 14:36:20 -0300491 case PERF_TYPE_SOFTWARE:
Arnaldo Carvalho de Melo7289f832012-06-12 12:34:58 -0300492 perf_evsel__sw_name(evsel, bf, sizeof(bf));
Arnaldo Carvalho de Melo335c2f52012-06-11 14:36:20 -0300493 break;
494
Arnaldo Carvalho de Meloa4460832012-06-12 10:29:12 -0300495 case PERF_TYPE_TRACEPOINT:
Arnaldo Carvalho de Melo7289f832012-06-12 12:34:58 -0300496 scnprintf(bf, sizeof(bf), "%s", "unknown tracepoint");
Arnaldo Carvalho de Meloa4460832012-06-12 10:29:12 -0300497 break;
498
Jiri Olsa287e74a2012-06-28 23:18:49 +0200499 case PERF_TYPE_BREAKPOINT:
500 perf_evsel__bp_name(evsel, bf, sizeof(bf));
501 break;
502
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300503 default:
Robert Richterca1b1452012-08-16 21:10:18 +0200504 scnprintf(bf, sizeof(bf), "unknown attr type: %d",
505 evsel->attr.type);
Arnaldo Carvalho de Meloa4460832012-06-12 10:29:12 -0300506 break;
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300507 }
508
Arnaldo Carvalho de Melo7289f832012-06-12 12:34:58 -0300509 evsel->name = strdup(bf);
510
511 return evsel->name ?: "unknown";
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300512}
513
Namhyung Kim717e2632013-01-22 18:09:44 +0900514const char *perf_evsel__group_name(struct perf_evsel *evsel)
515{
516 return evsel->group_name ?: "anon group";
517}
518
519int perf_evsel__group_desc(struct perf_evsel *evsel, char *buf, size_t size)
520{
521 int ret;
522 struct perf_evsel *pos;
523 const char *group_name = perf_evsel__group_name(evsel);
524
525 ret = scnprintf(buf, size, "%s", group_name);
526
527 ret += scnprintf(buf + ret, size - ret, " { %s",
528 perf_evsel__name(evsel));
529
530 for_each_group_member(pos, evsel)
531 ret += scnprintf(buf + ret, size - ret, ", %s",
532 perf_evsel__name(pos));
533
534 ret += scnprintf(buf + ret, size - ret, " }");
535
536 return ret;
537}
538
Jiri Olsa6bedfab2014-03-02 16:56:40 +0100539static void
Namhyung Kim72a128a2014-09-23 10:01:41 +0900540perf_evsel__config_callgraph(struct perf_evsel *evsel)
Jiri Olsa6bedfab2014-03-02 16:56:40 +0100541{
542 bool function = perf_evsel__is_function_event(evsel);
543 struct perf_event_attr *attr = &evsel->attr;
544
545 perf_evsel__set_sample_bit(evsel, CALLCHAIN);
546
Namhyung Kim72a128a2014-09-23 10:01:41 +0900547 if (callchain_param.record_mode == CALLCHAIN_DWARF) {
Jiri Olsa6bedfab2014-03-02 16:56:40 +0100548 if (!function) {
549 perf_evsel__set_sample_bit(evsel, REGS_USER);
550 perf_evsel__set_sample_bit(evsel, STACK_USER);
551 attr->sample_regs_user = PERF_REGS_MASK;
Namhyung Kim72a128a2014-09-23 10:01:41 +0900552 attr->sample_stack_user = callchain_param.dump_size;
Jiri Olsa6bedfab2014-03-02 16:56:40 +0100553 attr->exclude_callchain_user = 1;
554 } else {
555 pr_info("Cannot use DWARF unwind for function trace event,"
556 " falling back to framepointers.\n");
557 }
558 }
559
560 if (function) {
561 pr_info("Disabling user space callchains for function trace event.\n");
562 attr->exclude_callchain_user = 1;
563 }
564}
565
Jiri Olsa774cb492012-11-12 18:34:01 +0100566/*
567 * The enable_on_exec/disabled value strategy:
568 *
569 * 1) For any type of traced program:
570 * - all independent events and group leaders are disabled
571 * - all group members are enabled
572 *
573 * Group members are ruled by group leaders. They need to
574 * be enabled, because the group scheduling relies on that.
575 *
576 * 2) For traced programs executed by perf:
577 * - all independent events and group leaders have
578 * enable_on_exec set
579 * - we don't specifically enable or disable any event during
580 * the record command
581 *
582 * Independent events and group leaders are initially disabled
583 * and get enabled by exec. Group members are ruled by group
584 * leaders as stated in 1).
585 *
586 * 3) For traced programs attached by perf (pid/tid):
587 * - we specifically enable or disable all events during
588 * the record command
589 *
590 * When attaching events to already running traced we
591 * enable/disable events specifically, as there's no
592 * initial traced exec call.
593 */
Arnaldo Carvalho de Melob4006792013-12-19 14:43:45 -0300594void perf_evsel__config(struct perf_evsel *evsel, struct record_opts *opts)
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200595{
Jiri Olsa3c176312012-10-10 17:39:03 +0200596 struct perf_evsel *leader = evsel->leader;
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200597 struct perf_event_attr *attr = &evsel->attr;
Adrian Hunter60b08962014-07-31 09:00:52 +0300598 int track = evsel->tracking;
Adrian Hunter3aa59392013-11-15 15:52:29 +0200599 bool per_cpu = opts->target.default_per_cpu && !opts->target.per_thread;
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200600
Arnaldo Carvalho de Melo594ac612012-12-13 13:13:07 -0300601 attr->sample_id_all = perf_missing_features.sample_id_all ? 0 : 1;
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200602 attr->inherit = !opts->no_inherit;
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200603
Arnaldo Carvalho de Melo7be5ebe2012-12-10 14:53:43 -0300604 perf_evsel__set_sample_bit(evsel, IP);
605 perf_evsel__set_sample_bit(evsel, TID);
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200606
Jiri Olsa3c176312012-10-10 17:39:03 +0200607 if (evsel->sample_read) {
608 perf_evsel__set_sample_bit(evsel, READ);
609
610 /*
611 * We need ID even in case of single event, because
612 * PERF_SAMPLE_READ process ID specific data.
613 */
Adrian Hunter75562572013-08-27 11:23:09 +0300614 perf_evsel__set_sample_id(evsel, false);
Jiri Olsa3c176312012-10-10 17:39:03 +0200615
616 /*
617 * Apply group format only if we belong to group
618 * with more than one members.
619 */
620 if (leader->nr_members > 1) {
621 attr->read_format |= PERF_FORMAT_GROUP;
622 attr->inherit = 0;
623 }
624 }
625
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200626 /*
Namhyung Kim17314e22014-06-09 14:43:37 +0900627 * We default some events to have a default interval. But keep
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200628 * it a weak assumption overridable by the user.
629 */
Namhyung Kim17314e22014-06-09 14:43:37 +0900630 if (!attr->sample_period || (opts->user_freq != UINT_MAX ||
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200631 opts->user_interval != ULLONG_MAX)) {
632 if (opts->freq) {
Arnaldo Carvalho de Melo7be5ebe2012-12-10 14:53:43 -0300633 perf_evsel__set_sample_bit(evsel, PERIOD);
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200634 attr->freq = 1;
635 attr->sample_freq = opts->freq;
636 } else {
637 attr->sample_period = opts->default_interval;
638 }
639 }
640
Jiri Olsa3c176312012-10-10 17:39:03 +0200641 /*
642 * Disable sampling for all group members other
643 * than leader in case leader 'leads' the sampling.
644 */
645 if ((leader != evsel) && leader->sample_read) {
646 attr->sample_freq = 0;
647 attr->sample_period = 0;
648 }
649
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200650 if (opts->no_samples)
651 attr->sample_freq = 0;
652
653 if (opts->inherit_stat)
654 attr->inherit_stat = 1;
655
656 if (opts->sample_address) {
Arnaldo Carvalho de Melo7be5ebe2012-12-10 14:53:43 -0300657 perf_evsel__set_sample_bit(evsel, ADDR);
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200658 attr->mmap_data = track;
659 }
660
Jiri Olsaf1403732014-11-13 18:21:03 +0100661 /*
662 * We don't allow user space callchains for function trace
663 * event, due to issues with page faults while tracing page
664 * fault handler and its overall trickiness nature.
665 */
666 if (perf_evsel__is_function_event(evsel))
667 evsel->attr.exclude_callchain_user = 1;
668
Namhyung Kim72a128a2014-09-23 10:01:41 +0900669 if (callchain_param.enabled && !evsel->no_aux_samples)
670 perf_evsel__config_callgraph(evsel);
Jiri Olsa26d33022012-08-07 15:20:47 +0200671
Stephane Eranian6a21c0b2014-09-24 13:48:39 +0200672 if (opts->sample_intr_regs) {
673 attr->sample_regs_intr = PERF_REGS_MASK;
674 perf_evsel__set_sample_bit(evsel, REGS_INTR);
675 }
676
Adrian Hunter3aa59392013-11-15 15:52:29 +0200677 if (target__has_cpu(&opts->target))
Arnaldo Carvalho de Melo7be5ebe2012-12-10 14:53:43 -0300678 perf_evsel__set_sample_bit(evsel, CPU);
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200679
Andrew Vagin3e76ac72011-12-20 17:32:45 +0300680 if (opts->period)
Arnaldo Carvalho de Melo7be5ebe2012-12-10 14:53:43 -0300681 perf_evsel__set_sample_bit(evsel, PERIOD);
Andrew Vagin3e76ac72011-12-20 17:32:45 +0300682
Andi Kleen8affc2b2014-07-31 14:45:04 +0800683 /*
684 * When the user explicitely disabled time don't force it here.
685 */
686 if (opts->sample_time &&
687 (!perf_missing_features.sample_id_all &&
688 (!opts->no_inherit || target__has_cpu(&opts->target) || per_cpu)))
Arnaldo Carvalho de Melo7be5ebe2012-12-10 14:53:43 -0300689 perf_evsel__set_sample_bit(evsel, TIME);
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200690
Adrian Hunter6ff1ce72014-07-14 13:02:56 +0300691 if (opts->raw_samples && !evsel->no_aux_samples) {
Arnaldo Carvalho de Melo7be5ebe2012-12-10 14:53:43 -0300692 perf_evsel__set_sample_bit(evsel, TIME);
693 perf_evsel__set_sample_bit(evsel, RAW);
694 perf_evsel__set_sample_bit(evsel, CPU);
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200695 }
696
Stephane Eranianccf49bf2013-01-24 16:10:37 +0100697 if (opts->sample_address)
Adrian Hunter1e7ed5e2013-11-01 15:51:35 +0200698 perf_evsel__set_sample_bit(evsel, DATA_SRC);
Stephane Eranianccf49bf2013-01-24 16:10:37 +0100699
Arnaldo Carvalho de Melo509051e2014-01-14 17:52:14 -0300700 if (opts->no_buffering) {
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200701 attr->watermark = 0;
702 attr->wakeup_events = 1;
703 }
Adrian Hunter6ff1ce72014-07-14 13:02:56 +0300704 if (opts->branch_stack && !evsel->no_aux_samples) {
Arnaldo Carvalho de Melo7be5ebe2012-12-10 14:53:43 -0300705 perf_evsel__set_sample_bit(evsel, BRANCH_STACK);
Roberto Agostino Vitillobdfebd82012-02-09 23:21:02 +0100706 attr->branch_sample_type = opts->branch_stack;
707 }
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200708
Andi Kleen05484292013-01-24 16:10:29 +0100709 if (opts->sample_weight)
Adrian Hunter1e7ed5e2013-11-01 15:51:35 +0200710 perf_evsel__set_sample_bit(evsel, WEIGHT);
Andi Kleen05484292013-01-24 16:10:29 +0100711
Stephane Eranian5c5e8542013-08-21 12:10:25 +0200712 attr->mmap = track;
Don Zickusa5a5ba72014-05-30 10:49:42 -0400713 attr->mmap2 = track && !perf_missing_features.mmap2;
Stephane Eranian5c5e8542013-08-21 12:10:25 +0200714 attr->comm = track;
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200715
Andi Kleen475eeab2013-09-20 07:40:43 -0700716 if (opts->sample_transaction)
Adrian Hunter1e7ed5e2013-11-01 15:51:35 +0200717 perf_evsel__set_sample_bit(evsel, TRANSACTION);
Andi Kleen475eeab2013-09-20 07:40:43 -0700718
Jiri Olsa774cb492012-11-12 18:34:01 +0100719 /*
720 * XXX see the function comment above
721 *
722 * Disabling only independent events or group leaders,
723 * keeping group members enabled.
724 */
Namhyung Kim823254e2012-11-29 15:38:30 +0900725 if (perf_evsel__is_group_leader(evsel))
Jiri Olsa774cb492012-11-12 18:34:01 +0100726 attr->disabled = 1;
727
728 /*
729 * Setting enable_on_exec for independent events and
730 * group leaders for traced executed by perf.
731 */
Andi Kleen6619a532014-01-11 13:38:27 -0800732 if (target__none(&opts->target) && perf_evsel__is_group_leader(evsel) &&
733 !opts->initial_delay)
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200734 attr->enable_on_exec = 1;
Adrian Hunter2afd2bc2014-07-14 13:02:57 +0300735
736 if (evsel->immediate) {
737 attr->disabled = 0;
738 attr->enable_on_exec = 0;
739 }
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200740}
741
Arnaldo Carvalho de Melo88858462014-10-13 13:30:27 -0300742static int perf_evsel__alloc_fd(struct perf_evsel *evsel, int ncpus, int nthreads)
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200743{
David Ahern4af4c952011-05-27 09:58:34 -0600744 int cpu, thread;
Adrian Hunterbf8e8f42014-07-31 09:00:51 +0300745
746 if (evsel->system_wide)
747 nthreads = 1;
748
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200749 evsel->fd = xyarray__new(ncpus, nthreads, sizeof(int));
David Ahern4af4c952011-05-27 09:58:34 -0600750
751 if (evsel->fd) {
752 for (cpu = 0; cpu < ncpus; cpu++) {
753 for (thread = 0; thread < nthreads; thread++) {
754 FD(evsel, cpu, thread) = -1;
755 }
756 }
757 }
758
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200759 return evsel->fd != NULL ? 0 : -ENOMEM;
760}
761
Andi Kleene2407be2013-08-02 17:41:10 -0700762static int perf_evsel__run_ioctl(struct perf_evsel *evsel, int ncpus, int nthreads,
763 int ioc, void *arg)
Arnaldo Carvalho de Melo745cefc2012-09-26 15:07:39 -0300764{
765 int cpu, thread;
766
Adrian Hunterbf8e8f42014-07-31 09:00:51 +0300767 if (evsel->system_wide)
768 nthreads = 1;
769
Arnaldo Carvalho de Melo745cefc2012-09-26 15:07:39 -0300770 for (cpu = 0; cpu < ncpus; cpu++) {
771 for (thread = 0; thread < nthreads; thread++) {
772 int fd = FD(evsel, cpu, thread),
Andi Kleene2407be2013-08-02 17:41:10 -0700773 err = ioctl(fd, ioc, arg);
Arnaldo Carvalho de Melo745cefc2012-09-26 15:07:39 -0300774
775 if (err)
776 return err;
777 }
778 }
779
780 return 0;
781}
782
Andi Kleene2407be2013-08-02 17:41:10 -0700783int perf_evsel__set_filter(struct perf_evsel *evsel, int ncpus, int nthreads,
784 const char *filter)
785{
786 return perf_evsel__run_ioctl(evsel, ncpus, nthreads,
787 PERF_EVENT_IOC_SET_FILTER,
788 (void *)filter);
789}
790
791int perf_evsel__enable(struct perf_evsel *evsel, int ncpus, int nthreads)
792{
793 return perf_evsel__run_ioctl(evsel, ncpus, nthreads,
794 PERF_EVENT_IOC_ENABLE,
795 0);
796}
797
Arnaldo Carvalho de Melo70db7532011-01-12 22:39:13 -0200798int perf_evsel__alloc_id(struct perf_evsel *evsel, int ncpus, int nthreads)
799{
Vineet Gupta8d9cbd82015-01-13 19:13:23 +0530800 if (ncpus == 0 || nthreads == 0)
801 return 0;
802
Adrian Hunterbf8e8f42014-07-31 09:00:51 +0300803 if (evsel->system_wide)
804 nthreads = 1;
805
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -0300806 evsel->sample_id = xyarray__new(ncpus, nthreads, sizeof(struct perf_sample_id));
807 if (evsel->sample_id == NULL)
808 return -ENOMEM;
809
810 evsel->id = zalloc(ncpus * nthreads * sizeof(u64));
811 if (evsel->id == NULL) {
812 xyarray__delete(evsel->sample_id);
813 evsel->sample_id = NULL;
814 return -ENOMEM;
815 }
816
817 return 0;
Arnaldo Carvalho de Melo70db7532011-01-12 22:39:13 -0200818}
819
Frederik Deweerdta7e191c2013-03-01 13:02:27 -0500820void perf_evsel__reset_counts(struct perf_evsel *evsel, int ncpus)
821{
822 memset(evsel->counts, 0, (sizeof(*evsel->counts) +
823 (ncpus * sizeof(struct perf_counts_values))));
824}
825
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200826int perf_evsel__alloc_counts(struct perf_evsel *evsel, int ncpus)
827{
828 evsel->counts = zalloc((sizeof(*evsel->counts) +
829 (ncpus * sizeof(struct perf_counts_values))));
830 return evsel->counts != NULL ? 0 : -ENOMEM;
831}
832
Arnaldo Carvalho de Melo88858462014-10-13 13:30:27 -0300833static void perf_evsel__free_fd(struct perf_evsel *evsel)
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200834{
835 xyarray__delete(evsel->fd);
836 evsel->fd = NULL;
837}
838
Arnaldo Carvalho de Melo88858462014-10-13 13:30:27 -0300839static void perf_evsel__free_id(struct perf_evsel *evsel)
Arnaldo Carvalho de Melo70db7532011-01-12 22:39:13 -0200840{
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -0300841 xyarray__delete(evsel->sample_id);
842 evsel->sample_id = NULL;
Arnaldo Carvalho de Melo04662522013-12-26 17:41:15 -0300843 zfree(&evsel->id);
Arnaldo Carvalho de Melo70db7532011-01-12 22:39:13 -0200844}
845
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200846void perf_evsel__close_fd(struct perf_evsel *evsel, int ncpus, int nthreads)
847{
848 int cpu, thread;
849
Adrian Hunterbf8e8f42014-07-31 09:00:51 +0300850 if (evsel->system_wide)
851 nthreads = 1;
852
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200853 for (cpu = 0; cpu < ncpus; cpu++)
854 for (thread = 0; thread < nthreads; ++thread) {
855 close(FD(evsel, cpu, thread));
856 FD(evsel, cpu, thread) = -1;
857 }
858}
859
Namhyung Kim43f8e762013-01-25 10:44:44 +0900860void perf_evsel__free_counts(struct perf_evsel *evsel)
861{
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -0300862 zfree(&evsel->counts);
Namhyung Kim43f8e762013-01-25 10:44:44 +0900863}
864
Arnaldo Carvalho de Meloef1d1af2011-01-18 21:41:45 -0200865void perf_evsel__exit(struct perf_evsel *evsel)
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200866{
867 assert(list_empty(&evsel->node));
Namhyung Kim736b05a2013-03-15 14:48:49 +0900868 perf_evsel__free_fd(evsel);
869 perf_evsel__free_id(evsel);
Arnaldo Carvalho de Melo597e48c2014-10-16 13:25:01 -0300870 close_cgroup(evsel->cgrp);
871 zfree(&evsel->group_name);
Arnaldo Carvalho de Melo597e48c2014-10-16 13:25:01 -0300872 zfree(&evsel->name);
Arnaldo Carvalho de Meloce8ccff2014-10-09 15:29:51 -0300873 perf_evsel__object.fini(evsel);
Arnaldo Carvalho de Meloef1d1af2011-01-18 21:41:45 -0200874}
875
876void perf_evsel__delete(struct perf_evsel *evsel)
877{
878 perf_evsel__exit(evsel);
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200879 free(evsel);
880}
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200881
Jiri Olsa857a94a2014-11-21 10:31:05 +0100882void perf_evsel__compute_deltas(struct perf_evsel *evsel, int cpu,
883 struct perf_counts_values *count)
Stephane Eranianc7a79c42013-01-29 12:47:43 +0100884{
885 struct perf_counts_values tmp;
886
887 if (!evsel->prev_raw_counts)
888 return;
889
890 if (cpu == -1) {
891 tmp = evsel->prev_raw_counts->aggr;
892 evsel->prev_raw_counts->aggr = *count;
893 } else {
894 tmp = evsel->prev_raw_counts->cpu[cpu];
895 evsel->prev_raw_counts->cpu[cpu] = *count;
896 }
897
898 count->val = count->val - tmp.val;
899 count->ena = count->ena - tmp.ena;
900 count->run = count->run - tmp.run;
901}
902
Jiri Olsa13112bb2014-11-21 10:31:06 +0100903void perf_counts_values__scale(struct perf_counts_values *count,
904 bool scale, s8 *pscaled)
905{
906 s8 scaled = 0;
907
908 if (scale) {
909 if (count->run == 0) {
910 scaled = -1;
911 count->val = 0;
912 } else if (count->run < count->ena) {
913 scaled = 1;
914 count->val = (u64)((double) count->val * count->ena / count->run + 0.5);
915 }
916 } else
917 count->ena = count->run = 0;
918
919 if (pscaled)
920 *pscaled = scaled;
921}
922
Jiri Olsa011dccb2014-11-21 10:31:07 +0100923int perf_evsel__read_cb(struct perf_evsel *evsel, int cpu, int thread,
924 perf_evsel__read_cb_t cb)
925{
926 struct perf_counts_values count;
927
928 memset(&count, 0, sizeof(count));
929
930 if (FD(evsel, cpu, thread) < 0)
931 return -EINVAL;
932
933 if (readn(FD(evsel, cpu, thread), &count, sizeof(count)) < 0)
934 return -errno;
935
936 return cb(evsel, cpu, thread, &count);
937}
938
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200939int __perf_evsel__read_on_cpu(struct perf_evsel *evsel,
940 int cpu, int thread, bool scale)
941{
942 struct perf_counts_values count;
943 size_t nv = scale ? 3 : 1;
944
945 if (FD(evsel, cpu, thread) < 0)
946 return -EINVAL;
947
Arnaldo Carvalho de Melo4eed11d2011-01-04 00:13:17 -0200948 if (evsel->counts == NULL && perf_evsel__alloc_counts(evsel, cpu + 1) < 0)
949 return -ENOMEM;
950
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200951 if (readn(FD(evsel, cpu, thread), &count, nv * sizeof(u64)) < 0)
952 return -errno;
953
Jiri Olsa857a94a2014-11-21 10:31:05 +0100954 perf_evsel__compute_deltas(evsel, cpu, &count);
Jiri Olsa13112bb2014-11-21 10:31:06 +0100955 perf_counts_values__scale(&count, scale, NULL);
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200956 evsel->counts->cpu[cpu] = count;
957 return 0;
958}
959
Jiri Olsa6a4bb042012-08-08 12:22:36 +0200960static int get_group_fd(struct perf_evsel *evsel, int cpu, int thread)
961{
962 struct perf_evsel *leader = evsel->leader;
963 int fd;
964
Namhyung Kim823254e2012-11-29 15:38:30 +0900965 if (perf_evsel__is_group_leader(evsel))
Jiri Olsa6a4bb042012-08-08 12:22:36 +0200966 return -1;
967
968 /*
969 * Leader must be already processed/open,
970 * if not it's a bug.
971 */
972 BUG_ON(!leader->fd);
973
974 fd = FD(leader, cpu, thread);
975 BUG_ON(fd == -1);
976
977 return fd;
978}
979
Adrian Huntere3e1a542013-08-14 15:48:24 +0300980#define __PRINT_ATTR(fmt, cast, field) \
981 fprintf(fp, " %-19s "fmt"\n", #field, cast attr->field)
982
983#define PRINT_ATTR_U32(field) __PRINT_ATTR("%u" , , field)
984#define PRINT_ATTR_X32(field) __PRINT_ATTR("%#x", , field)
985#define PRINT_ATTR_U64(field) __PRINT_ATTR("%" PRIu64, (uint64_t), field)
986#define PRINT_ATTR_X64(field) __PRINT_ATTR("%#"PRIx64, (uint64_t), field)
987
988#define PRINT_ATTR2N(name1, field1, name2, field2) \
989 fprintf(fp, " %-19s %u %-19s %u\n", \
990 name1, attr->field1, name2, attr->field2)
991
992#define PRINT_ATTR2(field1, field2) \
993 PRINT_ATTR2N(#field1, field1, #field2, field2)
994
995static size_t perf_event_attr__fprintf(struct perf_event_attr *attr, FILE *fp)
996{
997 size_t ret = 0;
998
999 ret += fprintf(fp, "%.60s\n", graph_dotted_line);
1000 ret += fprintf(fp, "perf_event_attr:\n");
1001
1002 ret += PRINT_ATTR_U32(type);
1003 ret += PRINT_ATTR_U32(size);
1004 ret += PRINT_ATTR_X64(config);
1005 ret += PRINT_ATTR_U64(sample_period);
1006 ret += PRINT_ATTR_U64(sample_freq);
1007 ret += PRINT_ATTR_X64(sample_type);
1008 ret += PRINT_ATTR_X64(read_format);
1009
1010 ret += PRINT_ATTR2(disabled, inherit);
1011 ret += PRINT_ATTR2(pinned, exclusive);
1012 ret += PRINT_ATTR2(exclude_user, exclude_kernel);
1013 ret += PRINT_ATTR2(exclude_hv, exclude_idle);
1014 ret += PRINT_ATTR2(mmap, comm);
Adrian Hunter022c50d2014-07-14 13:02:27 +03001015 ret += PRINT_ATTR2(mmap2, comm_exec);
Adrian Huntere3e1a542013-08-14 15:48:24 +03001016 ret += PRINT_ATTR2(freq, inherit_stat);
1017 ret += PRINT_ATTR2(enable_on_exec, task);
1018 ret += PRINT_ATTR2(watermark, precise_ip);
1019 ret += PRINT_ATTR2(mmap_data, sample_id_all);
1020 ret += PRINT_ATTR2(exclude_host, exclude_guest);
1021 ret += PRINT_ATTR2N("excl.callchain_kern", exclude_callchain_kernel,
1022 "excl.callchain_user", exclude_callchain_user);
1023
1024 ret += PRINT_ATTR_U32(wakeup_events);
1025 ret += PRINT_ATTR_U32(wakeup_watermark);
1026 ret += PRINT_ATTR_X32(bp_type);
1027 ret += PRINT_ATTR_X64(bp_addr);
1028 ret += PRINT_ATTR_X64(config1);
1029 ret += PRINT_ATTR_U64(bp_len);
1030 ret += PRINT_ATTR_X64(config2);
1031 ret += PRINT_ATTR_X64(branch_sample_type);
1032 ret += PRINT_ATTR_X64(sample_regs_user);
1033 ret += PRINT_ATTR_U32(sample_stack_user);
Stephane Eranian6a21c0b2014-09-24 13:48:39 +02001034 ret += PRINT_ATTR_X64(sample_regs_intr);
Adrian Huntere3e1a542013-08-14 15:48:24 +03001035
1036 ret += fprintf(fp, "%.60s\n", graph_dotted_line);
1037
1038 return ret;
1039}
1040
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -02001041static int __perf_evsel__open(struct perf_evsel *evsel, struct cpu_map *cpus,
Jiri Olsa6a4bb042012-08-08 12:22:36 +02001042 struct thread_map *threads)
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -02001043{
Adrian Hunterbf8e8f42014-07-31 09:00:51 +03001044 int cpu, thread, nthreads;
Yann Droneaud57480d22014-06-30 22:28:47 +02001045 unsigned long flags = PERF_FLAG_FD_CLOEXEC;
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -02001046 int pid = -1, err;
Andi Kleenbec19672013-08-04 19:41:26 -07001047 enum { NO_CHANGE, SET_TO_MAX, INCREASED_MAX } set_rlimit = NO_CHANGE;
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -02001048
Adrian Hunterbf8e8f42014-07-31 09:00:51 +03001049 if (evsel->system_wide)
1050 nthreads = 1;
1051 else
1052 nthreads = threads->nr;
1053
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -02001054 if (evsel->fd == NULL &&
Adrian Hunterbf8e8f42014-07-31 09:00:51 +03001055 perf_evsel__alloc_fd(evsel, cpus->nr, nthreads) < 0)
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -02001056 return -ENOMEM;
Arnaldo Carvalho de Melo4eed11d2011-01-04 00:13:17 -02001057
Stephane Eranian023695d2011-02-14 11:20:01 +02001058 if (evsel->cgrp) {
Yann Droneaud57480d22014-06-30 22:28:47 +02001059 flags |= PERF_FLAG_PID_CGROUP;
Stephane Eranian023695d2011-02-14 11:20:01 +02001060 pid = evsel->cgrp->fd;
1061 }
1062
Arnaldo Carvalho de Melo594ac612012-12-13 13:13:07 -03001063fallback_missing_features:
Yann Droneaud57480d22014-06-30 22:28:47 +02001064 if (perf_missing_features.cloexec)
1065 flags &= ~(unsigned long)PERF_FLAG_FD_CLOEXEC;
Stephane Eranian5c5e8542013-08-21 12:10:25 +02001066 if (perf_missing_features.mmap2)
1067 evsel->attr.mmap2 = 0;
Arnaldo Carvalho de Melo594ac612012-12-13 13:13:07 -03001068 if (perf_missing_features.exclude_guest)
1069 evsel->attr.exclude_guest = evsel->attr.exclude_host = 0;
1070retry_sample_id:
1071 if (perf_missing_features.sample_id_all)
1072 evsel->attr.sample_id_all = 0;
1073
Adrian Huntere3e1a542013-08-14 15:48:24 +03001074 if (verbose >= 2)
1075 perf_event_attr__fprintf(&evsel->attr, stderr);
1076
Arnaldo Carvalho de Melo86bd5e82011-01-03 23:09:46 -02001077 for (cpu = 0; cpu < cpus->nr; cpu++) {
Arnaldo Carvalho de Melo9d04f172011-01-12 00:08:18 -02001078
Adrian Hunterbf8e8f42014-07-31 09:00:51 +03001079 for (thread = 0; thread < nthreads; thread++) {
Jiri Olsa6a4bb042012-08-08 12:22:36 +02001080 int group_fd;
Stephane Eranian023695d2011-02-14 11:20:01 +02001081
Adrian Hunterbf8e8f42014-07-31 09:00:51 +03001082 if (!evsel->cgrp && !evsel->system_wide)
Stephane Eranian023695d2011-02-14 11:20:01 +02001083 pid = threads->map[thread];
1084
Jiri Olsa6a4bb042012-08-08 12:22:36 +02001085 group_fd = get_group_fd(evsel, cpu, thread);
Andi Kleenbec19672013-08-04 19:41:26 -07001086retry_open:
Ramkumar Ramachandraa33f6ef2014-03-18 15:10:42 -04001087 pr_debug2("sys_perf_event_open: pid %d cpu %d group_fd %d flags %#lx\n",
Adrian Huntere3e1a542013-08-14 15:48:24 +03001088 pid, cpus->map[cpu], group_fd, flags);
1089
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -02001090 FD(evsel, cpu, thread) = sys_perf_event_open(&evsel->attr,
Stephane Eranian023695d2011-02-14 11:20:01 +02001091 pid,
Arnaldo Carvalho de Melof08199d2011-01-11 23:42:19 -02001092 cpus->map[cpu],
Stephane Eranian023695d2011-02-14 11:20:01 +02001093 group_fd, flags);
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -02001094 if (FD(evsel, cpu, thread) < 0) {
1095 err = -errno;
Ramkumar Ramachandraa33f6ef2014-03-18 15:10:42 -04001096 pr_debug2("sys_perf_event_open failed, error %d\n",
Adrian Hunterf852fd62013-11-01 15:51:29 +02001097 err);
Arnaldo Carvalho de Melo594ac612012-12-13 13:13:07 -03001098 goto try_fallback;
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -02001099 }
Andi Kleenbec19672013-08-04 19:41:26 -07001100 set_rlimit = NO_CHANGE;
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -02001101 }
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -02001102 }
1103
1104 return 0;
1105
Arnaldo Carvalho de Melo594ac612012-12-13 13:13:07 -03001106try_fallback:
Andi Kleenbec19672013-08-04 19:41:26 -07001107 /*
1108 * perf stat needs between 5 and 22 fds per CPU. When we run out
1109 * of them try to increase the limits.
1110 */
1111 if (err == -EMFILE && set_rlimit < INCREASED_MAX) {
1112 struct rlimit l;
1113 int old_errno = errno;
1114
1115 if (getrlimit(RLIMIT_NOFILE, &l) == 0) {
1116 if (set_rlimit == NO_CHANGE)
1117 l.rlim_cur = l.rlim_max;
1118 else {
1119 l.rlim_cur = l.rlim_max + 1000;
1120 l.rlim_max = l.rlim_cur;
1121 }
1122 if (setrlimit(RLIMIT_NOFILE, &l) == 0) {
1123 set_rlimit++;
1124 errno = old_errno;
1125 goto retry_open;
1126 }
1127 }
1128 errno = old_errno;
1129 }
1130
Arnaldo Carvalho de Melo594ac612012-12-13 13:13:07 -03001131 if (err != -EINVAL || cpu > 0 || thread > 0)
1132 goto out_close;
1133
Yann Droneaud57480d22014-06-30 22:28:47 +02001134 if (!perf_missing_features.cloexec && (flags & PERF_FLAG_FD_CLOEXEC)) {
1135 perf_missing_features.cloexec = true;
1136 goto fallback_missing_features;
1137 } else if (!perf_missing_features.mmap2 && evsel->attr.mmap2) {
Stephane Eranian5c5e8542013-08-21 12:10:25 +02001138 perf_missing_features.mmap2 = true;
1139 goto fallback_missing_features;
1140 } else if (!perf_missing_features.exclude_guest &&
1141 (evsel->attr.exclude_guest || evsel->attr.exclude_host)) {
Arnaldo Carvalho de Melo594ac612012-12-13 13:13:07 -03001142 perf_missing_features.exclude_guest = true;
1143 goto fallback_missing_features;
1144 } else if (!perf_missing_features.sample_id_all) {
1145 perf_missing_features.sample_id_all = true;
1146 goto retry_sample_id;
1147 }
1148
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -02001149out_close:
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -02001150 do {
1151 while (--thread >= 0) {
1152 close(FD(evsel, cpu, thread));
1153 FD(evsel, cpu, thread) = -1;
1154 }
Adrian Hunterbf8e8f42014-07-31 09:00:51 +03001155 thread = nthreads;
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -02001156 } while (--cpu >= 0);
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -02001157 return err;
1158}
1159
1160void perf_evsel__close(struct perf_evsel *evsel, int ncpus, int nthreads)
1161{
1162 if (evsel->fd == NULL)
1163 return;
1164
1165 perf_evsel__close_fd(evsel, ncpus, nthreads);
1166 perf_evsel__free_fd(evsel);
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -02001167}
1168
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -02001169static struct {
1170 struct cpu_map map;
1171 int cpus[1];
1172} empty_cpu_map = {
1173 .map.nr = 1,
1174 .cpus = { -1, },
1175};
1176
1177static struct {
1178 struct thread_map map;
1179 int threads[1];
1180} empty_thread_map = {
1181 .map.nr = 1,
1182 .threads = { -1, },
1183};
1184
Arnaldo Carvalho de Melof08199d2011-01-11 23:42:19 -02001185int perf_evsel__open(struct perf_evsel *evsel, struct cpu_map *cpus,
Jiri Olsa6a4bb042012-08-08 12:22:36 +02001186 struct thread_map *threads)
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -02001187{
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -02001188 if (cpus == NULL) {
1189 /* Work around old compiler warnings about strict aliasing */
1190 cpus = &empty_cpu_map.map;
1191 }
1192
1193 if (threads == NULL)
1194 threads = &empty_thread_map.map;
1195
Jiri Olsa6a4bb042012-08-08 12:22:36 +02001196 return __perf_evsel__open(evsel, cpus, threads);
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -02001197}
1198
Arnaldo Carvalho de Melof08199d2011-01-11 23:42:19 -02001199int perf_evsel__open_per_cpu(struct perf_evsel *evsel,
Jiri Olsa6a4bb042012-08-08 12:22:36 +02001200 struct cpu_map *cpus)
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -02001201{
Jiri Olsa6a4bb042012-08-08 12:22:36 +02001202 return __perf_evsel__open(evsel, cpus, &empty_thread_map.map);
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -02001203}
1204
Arnaldo Carvalho de Melof08199d2011-01-11 23:42:19 -02001205int perf_evsel__open_per_thread(struct perf_evsel *evsel,
Jiri Olsa6a4bb042012-08-08 12:22:36 +02001206 struct thread_map *threads)
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -02001207{
Jiri Olsa6a4bb042012-08-08 12:22:36 +02001208 return __perf_evsel__open(evsel, &empty_cpu_map.map, threads);
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -02001209}
Arnaldo Carvalho de Melo70082dd2011-01-12 17:03:24 -02001210
Arnaldo Carvalho de Melo0807d2d2012-09-26 12:48:18 -03001211static int perf_evsel__parse_id_sample(const struct perf_evsel *evsel,
1212 const union perf_event *event,
1213 struct perf_sample *sample)
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001214{
Arnaldo Carvalho de Melo0807d2d2012-09-26 12:48:18 -03001215 u64 type = evsel->attr.sample_type;
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001216 const u64 *array = event->sample.array;
Arnaldo Carvalho de Melo0807d2d2012-09-26 12:48:18 -03001217 bool swapped = evsel->needs_swap;
Jiri Olsa37073f92012-05-30 14:23:44 +02001218 union u64_swap u;
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001219
1220 array += ((event->header.size -
1221 sizeof(event->header)) / sizeof(u64)) - 1;
1222
Adrian Hunter75562572013-08-27 11:23:09 +03001223 if (type & PERF_SAMPLE_IDENTIFIER) {
1224 sample->id = *array;
1225 array--;
1226 }
1227
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001228 if (type & PERF_SAMPLE_CPU) {
Jiri Olsa37073f92012-05-30 14:23:44 +02001229 u.val64 = *array;
1230 if (swapped) {
1231 /* undo swap of u64, then swap on individual u32s */
1232 u.val64 = bswap_64(u.val64);
1233 u.val32[0] = bswap_32(u.val32[0]);
1234 }
1235
1236 sample->cpu = u.val32[0];
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001237 array--;
1238 }
1239
1240 if (type & PERF_SAMPLE_STREAM_ID) {
1241 sample->stream_id = *array;
1242 array--;
1243 }
1244
1245 if (type & PERF_SAMPLE_ID) {
1246 sample->id = *array;
1247 array--;
1248 }
1249
1250 if (type & PERF_SAMPLE_TIME) {
1251 sample->time = *array;
1252 array--;
1253 }
1254
1255 if (type & PERF_SAMPLE_TID) {
Jiri Olsa37073f92012-05-30 14:23:44 +02001256 u.val64 = *array;
1257 if (swapped) {
1258 /* undo swap of u64, then swap on individual u32s */
1259 u.val64 = bswap_64(u.val64);
1260 u.val32[0] = bswap_32(u.val32[0]);
1261 u.val32[1] = bswap_32(u.val32[1]);
1262 }
1263
1264 sample->pid = u.val32[0];
1265 sample->tid = u.val32[1];
Adrian Hunterdd44bc62013-10-18 15:29:01 +03001266 array--;
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001267 }
1268
1269 return 0;
1270}
1271
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001272static inline bool overflow(const void *endp, u16 max_size, const void *offset,
1273 u64 size)
Frederic Weisbecker98e1da92011-05-21 20:08:15 +02001274{
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001275 return size > max_size || offset + size > endp;
Frederic Weisbecker98e1da92011-05-21 20:08:15 +02001276}
1277
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001278#define OVERFLOW_CHECK(offset, size, max_size) \
1279 do { \
1280 if (overflow(endp, (max_size), (offset), (size))) \
1281 return -EFAULT; \
1282 } while (0)
1283
1284#define OVERFLOW_CHECK_u64(offset) \
1285 OVERFLOW_CHECK(offset, sizeof(u64), sizeof(u64))
1286
Arnaldo Carvalho de Meloa3f698f2012-08-02 12:23:46 -03001287int perf_evsel__parse_sample(struct perf_evsel *evsel, union perf_event *event,
Arnaldo Carvalho de Melo0807d2d2012-09-26 12:48:18 -03001288 struct perf_sample *data)
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001289{
Arnaldo Carvalho de Meloa3f698f2012-08-02 12:23:46 -03001290 u64 type = evsel->attr.sample_type;
Arnaldo Carvalho de Melo0807d2d2012-09-26 12:48:18 -03001291 bool swapped = evsel->needs_swap;
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001292 const u64 *array;
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001293 u16 max_size = event->header.size;
1294 const void *endp = (void *)event + max_size;
1295 u64 sz;
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001296
David Ahern936be502011-09-06 09:12:26 -06001297 /*
1298 * used for cross-endian analysis. See git commit 65014ab3
1299 * for why this goofiness is needed.
1300 */
Jiri Olsa6a11f922012-05-16 08:59:04 +02001301 union u64_swap u;
David Ahern936be502011-09-06 09:12:26 -06001302
Robert Richterf3bda2c2011-12-15 17:32:39 +01001303 memset(data, 0, sizeof(*data));
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001304 data->cpu = data->pid = data->tid = -1;
1305 data->stream_id = data->id = data->time = -1ULL;
Jiri Olsabc529082014-02-03 12:44:41 +01001306 data->period = evsel->attr.sample_period;
Andi Kleen05484292013-01-24 16:10:29 +01001307 data->weight = 0;
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001308
1309 if (event->header.type != PERF_RECORD_SAMPLE) {
Arnaldo Carvalho de Meloa3f698f2012-08-02 12:23:46 -03001310 if (!evsel->attr.sample_id_all)
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001311 return 0;
Arnaldo Carvalho de Melo0807d2d2012-09-26 12:48:18 -03001312 return perf_evsel__parse_id_sample(evsel, event, data);
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001313 }
1314
1315 array = event->sample.array;
1316
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001317 /*
1318 * The evsel's sample_size is based on PERF_SAMPLE_MASK which includes
1319 * up to PERF_SAMPLE_PERIOD. After that overflow() must be used to
1320 * check the format does not go past the end of the event.
1321 */
Arnaldo Carvalho de Meloa3f698f2012-08-02 12:23:46 -03001322 if (evsel->sample_size + sizeof(event->header) > event->header.size)
Frederic Weisbeckera2854122011-05-21 19:33:04 +02001323 return -EFAULT;
1324
Adrian Hunter75562572013-08-27 11:23:09 +03001325 data->id = -1ULL;
1326 if (type & PERF_SAMPLE_IDENTIFIER) {
1327 data->id = *array;
1328 array++;
1329 }
1330
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001331 if (type & PERF_SAMPLE_IP) {
Adrian Hunteref893252013-08-27 11:23:06 +03001332 data->ip = *array;
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001333 array++;
1334 }
1335
1336 if (type & PERF_SAMPLE_TID) {
David Ahern936be502011-09-06 09:12:26 -06001337 u.val64 = *array;
1338 if (swapped) {
1339 /* undo swap of u64, then swap on individual u32s */
1340 u.val64 = bswap_64(u.val64);
1341 u.val32[0] = bswap_32(u.val32[0]);
1342 u.val32[1] = bswap_32(u.val32[1]);
1343 }
1344
1345 data->pid = u.val32[0];
1346 data->tid = u.val32[1];
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001347 array++;
1348 }
1349
1350 if (type & PERF_SAMPLE_TIME) {
1351 data->time = *array;
1352 array++;
1353 }
1354
David Ahern7cec0922011-05-30 13:08:23 -06001355 data->addr = 0;
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001356 if (type & PERF_SAMPLE_ADDR) {
1357 data->addr = *array;
1358 array++;
1359 }
1360
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001361 if (type & PERF_SAMPLE_ID) {
1362 data->id = *array;
1363 array++;
1364 }
1365
1366 if (type & PERF_SAMPLE_STREAM_ID) {
1367 data->stream_id = *array;
1368 array++;
1369 }
1370
1371 if (type & PERF_SAMPLE_CPU) {
David Ahern936be502011-09-06 09:12:26 -06001372
1373 u.val64 = *array;
1374 if (swapped) {
1375 /* undo swap of u64, then swap on individual u32s */
1376 u.val64 = bswap_64(u.val64);
1377 u.val32[0] = bswap_32(u.val32[0]);
1378 }
1379
1380 data->cpu = u.val32[0];
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001381 array++;
1382 }
1383
1384 if (type & PERF_SAMPLE_PERIOD) {
1385 data->period = *array;
1386 array++;
1387 }
1388
1389 if (type & PERF_SAMPLE_READ) {
Jiri Olsa9ede4732012-10-10 17:38:13 +02001390 u64 read_format = evsel->attr.read_format;
1391
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001392 OVERFLOW_CHECK_u64(array);
Jiri Olsa9ede4732012-10-10 17:38:13 +02001393 if (read_format & PERF_FORMAT_GROUP)
1394 data->read.group.nr = *array;
1395 else
1396 data->read.one.value = *array;
1397
1398 array++;
1399
1400 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) {
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001401 OVERFLOW_CHECK_u64(array);
Jiri Olsa9ede4732012-10-10 17:38:13 +02001402 data->read.time_enabled = *array;
1403 array++;
1404 }
1405
1406 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) {
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001407 OVERFLOW_CHECK_u64(array);
Jiri Olsa9ede4732012-10-10 17:38:13 +02001408 data->read.time_running = *array;
1409 array++;
1410 }
1411
1412 /* PERF_FORMAT_ID is forced for PERF_SAMPLE_READ */
1413 if (read_format & PERF_FORMAT_GROUP) {
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001414 const u64 max_group_nr = UINT64_MAX /
1415 sizeof(struct sample_read_value);
1416
1417 if (data->read.group.nr > max_group_nr)
1418 return -EFAULT;
1419 sz = data->read.group.nr *
1420 sizeof(struct sample_read_value);
1421 OVERFLOW_CHECK(array, sz, max_size);
1422 data->read.group.values =
1423 (struct sample_read_value *)array;
1424 array = (void *)array + sz;
Jiri Olsa9ede4732012-10-10 17:38:13 +02001425 } else {
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001426 OVERFLOW_CHECK_u64(array);
Jiri Olsa9ede4732012-10-10 17:38:13 +02001427 data->read.one.id = *array;
1428 array++;
1429 }
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001430 }
1431
1432 if (type & PERF_SAMPLE_CALLCHAIN) {
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001433 const u64 max_callchain_nr = UINT64_MAX / sizeof(u64);
1434
1435 OVERFLOW_CHECK_u64(array);
1436 data->callchain = (struct ip_callchain *)array++;
1437 if (data->callchain->nr > max_callchain_nr)
Frederic Weisbecker98e1da92011-05-21 20:08:15 +02001438 return -EFAULT;
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001439 sz = data->callchain->nr * sizeof(u64);
1440 OVERFLOW_CHECK(array, sz, max_size);
1441 array = (void *)array + sz;
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001442 }
1443
1444 if (type & PERF_SAMPLE_RAW) {
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001445 OVERFLOW_CHECK_u64(array);
David Ahern936be502011-09-06 09:12:26 -06001446 u.val64 = *array;
1447 if (WARN_ONCE(swapped,
1448 "Endianness of raw data not corrected!\n")) {
1449 /* undo swap of u64, then swap on individual u32s */
1450 u.val64 = bswap_64(u.val64);
1451 u.val32[0] = bswap_32(u.val32[0]);
1452 u.val32[1] = bswap_32(u.val32[1]);
1453 }
David Ahern936be502011-09-06 09:12:26 -06001454 data->raw_size = u.val32[0];
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001455 array = (void *)array + sizeof(u32);
Frederic Weisbecker98e1da92011-05-21 20:08:15 +02001456
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001457 OVERFLOW_CHECK(array, data->raw_size, max_size);
1458 data->raw_data = (void *)array;
1459 array = (void *)array + data->raw_size;
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001460 }
1461
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +01001462 if (type & PERF_SAMPLE_BRANCH_STACK) {
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001463 const u64 max_branch_nr = UINT64_MAX /
1464 sizeof(struct branch_entry);
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +01001465
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001466 OVERFLOW_CHECK_u64(array);
1467 data->branch_stack = (struct branch_stack *)array++;
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +01001468
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001469 if (data->branch_stack->nr > max_branch_nr)
1470 return -EFAULT;
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +01001471 sz = data->branch_stack->nr * sizeof(struct branch_entry);
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001472 OVERFLOW_CHECK(array, sz, max_size);
1473 array = (void *)array + sz;
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +01001474 }
Jiri Olsa0f6a3012012-08-07 15:20:45 +02001475
1476 if (type & PERF_SAMPLE_REGS_USER) {
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001477 OVERFLOW_CHECK_u64(array);
Adrian Hunter5b95a4a32013-08-27 11:23:10 +03001478 data->user_regs.abi = *array;
1479 array++;
Jiri Olsa0f6a3012012-08-07 15:20:45 +02001480
Adrian Hunter5b95a4a32013-08-27 11:23:10 +03001481 if (data->user_regs.abi) {
Jiri Olsa352ea452014-01-07 13:47:25 +01001482 u64 mask = evsel->attr.sample_regs_user;
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001483
Jiri Olsa352ea452014-01-07 13:47:25 +01001484 sz = hweight_long(mask) * sizeof(u64);
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001485 OVERFLOW_CHECK(array, sz, max_size);
Jiri Olsa352ea452014-01-07 13:47:25 +01001486 data->user_regs.mask = mask;
Jiri Olsa0f6a3012012-08-07 15:20:45 +02001487 data->user_regs.regs = (u64 *)array;
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001488 array = (void *)array + sz;
Jiri Olsa0f6a3012012-08-07 15:20:45 +02001489 }
1490 }
1491
1492 if (type & PERF_SAMPLE_STACK_USER) {
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001493 OVERFLOW_CHECK_u64(array);
1494 sz = *array++;
Jiri Olsa0f6a3012012-08-07 15:20:45 +02001495
1496 data->user_stack.offset = ((char *)(array - 1)
1497 - (char *) event);
1498
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001499 if (!sz) {
Jiri Olsa0f6a3012012-08-07 15:20:45 +02001500 data->user_stack.size = 0;
1501 } else {
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001502 OVERFLOW_CHECK(array, sz, max_size);
Jiri Olsa0f6a3012012-08-07 15:20:45 +02001503 data->user_stack.data = (char *)array;
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001504 array = (void *)array + sz;
1505 OVERFLOW_CHECK_u64(array);
Adrian Hunter54bd2692013-07-04 16:20:34 +03001506 data->user_stack.size = *array++;
Jiri Olsaa65cb4b2013-10-02 15:46:39 +02001507 if (WARN_ONCE(data->user_stack.size > sz,
1508 "user stack dump failure\n"))
1509 return -EFAULT;
Jiri Olsa0f6a3012012-08-07 15:20:45 +02001510 }
1511 }
1512
Andi Kleen05484292013-01-24 16:10:29 +01001513 data->weight = 0;
1514 if (type & PERF_SAMPLE_WEIGHT) {
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001515 OVERFLOW_CHECK_u64(array);
Andi Kleen05484292013-01-24 16:10:29 +01001516 data->weight = *array;
1517 array++;
1518 }
1519
Stephane Eranian98a3b322013-01-24 16:10:35 +01001520 data->data_src = PERF_MEM_DATA_SRC_NONE;
1521 if (type & PERF_SAMPLE_DATA_SRC) {
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001522 OVERFLOW_CHECK_u64(array);
Stephane Eranian98a3b322013-01-24 16:10:35 +01001523 data->data_src = *array;
1524 array++;
1525 }
1526
Andi Kleen475eeab2013-09-20 07:40:43 -07001527 data->transaction = 0;
1528 if (type & PERF_SAMPLE_TRANSACTION) {
Adrian Hunter87b95522013-11-01 15:51:36 +02001529 OVERFLOW_CHECK_u64(array);
Andi Kleen475eeab2013-09-20 07:40:43 -07001530 data->transaction = *array;
1531 array++;
1532 }
1533
Stephane Eranian6a21c0b2014-09-24 13:48:39 +02001534 data->intr_regs.abi = PERF_SAMPLE_REGS_ABI_NONE;
1535 if (type & PERF_SAMPLE_REGS_INTR) {
1536 OVERFLOW_CHECK_u64(array);
1537 data->intr_regs.abi = *array;
1538 array++;
1539
1540 if (data->intr_regs.abi != PERF_SAMPLE_REGS_ABI_NONE) {
1541 u64 mask = evsel->attr.sample_regs_intr;
1542
1543 sz = hweight_long(mask) * sizeof(u64);
1544 OVERFLOW_CHECK(array, sz, max_size);
1545 data->intr_regs.mask = mask;
1546 data->intr_regs.regs = (u64 *)array;
1547 array = (void *)array + sz;
1548 }
1549 }
1550
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001551 return 0;
1552}
Andrew Vagin74eec262011-11-28 12:03:31 +03001553
Adrian Hunterb1cf6f62013-08-27 11:23:12 +03001554size_t perf_event__sample_event_size(const struct perf_sample *sample, u64 type,
Jiri Olsa352ea452014-01-07 13:47:25 +01001555 u64 read_format)
Adrian Hunterb1cf6f62013-08-27 11:23:12 +03001556{
1557 size_t sz, result = sizeof(struct sample_event);
1558
1559 if (type & PERF_SAMPLE_IDENTIFIER)
1560 result += sizeof(u64);
1561
1562 if (type & PERF_SAMPLE_IP)
1563 result += sizeof(u64);
1564
1565 if (type & PERF_SAMPLE_TID)
1566 result += sizeof(u64);
1567
1568 if (type & PERF_SAMPLE_TIME)
1569 result += sizeof(u64);
1570
1571 if (type & PERF_SAMPLE_ADDR)
1572 result += sizeof(u64);
1573
1574 if (type & PERF_SAMPLE_ID)
1575 result += sizeof(u64);
1576
1577 if (type & PERF_SAMPLE_STREAM_ID)
1578 result += sizeof(u64);
1579
1580 if (type & PERF_SAMPLE_CPU)
1581 result += sizeof(u64);
1582
1583 if (type & PERF_SAMPLE_PERIOD)
1584 result += sizeof(u64);
1585
1586 if (type & PERF_SAMPLE_READ) {
1587 result += sizeof(u64);
1588 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
1589 result += sizeof(u64);
1590 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
1591 result += sizeof(u64);
1592 /* PERF_FORMAT_ID is forced for PERF_SAMPLE_READ */
1593 if (read_format & PERF_FORMAT_GROUP) {
1594 sz = sample->read.group.nr *
1595 sizeof(struct sample_read_value);
1596 result += sz;
1597 } else {
1598 result += sizeof(u64);
1599 }
1600 }
1601
1602 if (type & PERF_SAMPLE_CALLCHAIN) {
1603 sz = (sample->callchain->nr + 1) * sizeof(u64);
1604 result += sz;
1605 }
1606
1607 if (type & PERF_SAMPLE_RAW) {
1608 result += sizeof(u32);
1609 result += sample->raw_size;
1610 }
1611
1612 if (type & PERF_SAMPLE_BRANCH_STACK) {
1613 sz = sample->branch_stack->nr * sizeof(struct branch_entry);
1614 sz += sizeof(u64);
1615 result += sz;
1616 }
1617
1618 if (type & PERF_SAMPLE_REGS_USER) {
1619 if (sample->user_regs.abi) {
1620 result += sizeof(u64);
Jiri Olsa352ea452014-01-07 13:47:25 +01001621 sz = hweight_long(sample->user_regs.mask) * sizeof(u64);
Adrian Hunterb1cf6f62013-08-27 11:23:12 +03001622 result += sz;
1623 } else {
1624 result += sizeof(u64);
1625 }
1626 }
1627
1628 if (type & PERF_SAMPLE_STACK_USER) {
1629 sz = sample->user_stack.size;
1630 result += sizeof(u64);
1631 if (sz) {
1632 result += sz;
1633 result += sizeof(u64);
1634 }
1635 }
1636
1637 if (type & PERF_SAMPLE_WEIGHT)
1638 result += sizeof(u64);
1639
1640 if (type & PERF_SAMPLE_DATA_SRC)
1641 result += sizeof(u64);
1642
Adrian Hunter42d88912013-11-01 15:51:38 +02001643 if (type & PERF_SAMPLE_TRANSACTION)
1644 result += sizeof(u64);
1645
Stephane Eranian6a21c0b2014-09-24 13:48:39 +02001646 if (type & PERF_SAMPLE_REGS_INTR) {
1647 if (sample->intr_regs.abi) {
1648 result += sizeof(u64);
1649 sz = hweight_long(sample->intr_regs.mask) * sizeof(u64);
1650 result += sz;
1651 } else {
1652 result += sizeof(u64);
1653 }
1654 }
1655
Adrian Hunterb1cf6f62013-08-27 11:23:12 +03001656 return result;
1657}
1658
Andrew Vagin74eec262011-11-28 12:03:31 +03001659int perf_event__synthesize_sample(union perf_event *event, u64 type,
Jiri Olsa352ea452014-01-07 13:47:25 +01001660 u64 read_format,
Andrew Vagin74eec262011-11-28 12:03:31 +03001661 const struct perf_sample *sample,
1662 bool swapped)
1663{
1664 u64 *array;
Adrian Hunterd03f2172013-08-27 11:23:11 +03001665 size_t sz;
Andrew Vagin74eec262011-11-28 12:03:31 +03001666 /*
1667 * used for cross-endian analysis. See git commit 65014ab3
1668 * for why this goofiness is needed.
1669 */
Jiri Olsa6a11f922012-05-16 08:59:04 +02001670 union u64_swap u;
Andrew Vagin74eec262011-11-28 12:03:31 +03001671
1672 array = event->sample.array;
1673
Adrian Hunter75562572013-08-27 11:23:09 +03001674 if (type & PERF_SAMPLE_IDENTIFIER) {
1675 *array = sample->id;
1676 array++;
1677 }
1678
Andrew Vagin74eec262011-11-28 12:03:31 +03001679 if (type & PERF_SAMPLE_IP) {
Adrian Hunteref893252013-08-27 11:23:06 +03001680 *array = sample->ip;
Andrew Vagin74eec262011-11-28 12:03:31 +03001681 array++;
1682 }
1683
1684 if (type & PERF_SAMPLE_TID) {
1685 u.val32[0] = sample->pid;
1686 u.val32[1] = sample->tid;
1687 if (swapped) {
1688 /*
Arnaldo Carvalho de Meloa3f698f2012-08-02 12:23:46 -03001689 * Inverse of what is done in perf_evsel__parse_sample
Andrew Vagin74eec262011-11-28 12:03:31 +03001690 */
1691 u.val32[0] = bswap_32(u.val32[0]);
1692 u.val32[1] = bswap_32(u.val32[1]);
1693 u.val64 = bswap_64(u.val64);
1694 }
1695
1696 *array = u.val64;
1697 array++;
1698 }
1699
1700 if (type & PERF_SAMPLE_TIME) {
1701 *array = sample->time;
1702 array++;
1703 }
1704
1705 if (type & PERF_SAMPLE_ADDR) {
1706 *array = sample->addr;
1707 array++;
1708 }
1709
1710 if (type & PERF_SAMPLE_ID) {
1711 *array = sample->id;
1712 array++;
1713 }
1714
1715 if (type & PERF_SAMPLE_STREAM_ID) {
1716 *array = sample->stream_id;
1717 array++;
1718 }
1719
1720 if (type & PERF_SAMPLE_CPU) {
1721 u.val32[0] = sample->cpu;
1722 if (swapped) {
1723 /*
Arnaldo Carvalho de Meloa3f698f2012-08-02 12:23:46 -03001724 * Inverse of what is done in perf_evsel__parse_sample
Andrew Vagin74eec262011-11-28 12:03:31 +03001725 */
1726 u.val32[0] = bswap_32(u.val32[0]);
1727 u.val64 = bswap_64(u.val64);
1728 }
1729 *array = u.val64;
1730 array++;
1731 }
1732
1733 if (type & PERF_SAMPLE_PERIOD) {
1734 *array = sample->period;
1735 array++;
1736 }
1737
Adrian Hunterd03f2172013-08-27 11:23:11 +03001738 if (type & PERF_SAMPLE_READ) {
1739 if (read_format & PERF_FORMAT_GROUP)
1740 *array = sample->read.group.nr;
1741 else
1742 *array = sample->read.one.value;
1743 array++;
1744
1745 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) {
1746 *array = sample->read.time_enabled;
1747 array++;
1748 }
1749
1750 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) {
1751 *array = sample->read.time_running;
1752 array++;
1753 }
1754
1755 /* PERF_FORMAT_ID is forced for PERF_SAMPLE_READ */
1756 if (read_format & PERF_FORMAT_GROUP) {
1757 sz = sample->read.group.nr *
1758 sizeof(struct sample_read_value);
1759 memcpy(array, sample->read.group.values, sz);
1760 array = (void *)array + sz;
1761 } else {
1762 *array = sample->read.one.id;
1763 array++;
1764 }
1765 }
1766
1767 if (type & PERF_SAMPLE_CALLCHAIN) {
1768 sz = (sample->callchain->nr + 1) * sizeof(u64);
1769 memcpy(array, sample->callchain, sz);
1770 array = (void *)array + sz;
1771 }
1772
1773 if (type & PERF_SAMPLE_RAW) {
1774 u.val32[0] = sample->raw_size;
1775 if (WARN_ONCE(swapped,
1776 "Endianness of raw data not corrected!\n")) {
1777 /*
1778 * Inverse of what is done in perf_evsel__parse_sample
1779 */
1780 u.val32[0] = bswap_32(u.val32[0]);
1781 u.val32[1] = bswap_32(u.val32[1]);
1782 u.val64 = bswap_64(u.val64);
1783 }
1784 *array = u.val64;
1785 array = (void *)array + sizeof(u32);
1786
1787 memcpy(array, sample->raw_data, sample->raw_size);
1788 array = (void *)array + sample->raw_size;
1789 }
1790
1791 if (type & PERF_SAMPLE_BRANCH_STACK) {
1792 sz = sample->branch_stack->nr * sizeof(struct branch_entry);
1793 sz += sizeof(u64);
1794 memcpy(array, sample->branch_stack, sz);
1795 array = (void *)array + sz;
1796 }
1797
1798 if (type & PERF_SAMPLE_REGS_USER) {
1799 if (sample->user_regs.abi) {
1800 *array++ = sample->user_regs.abi;
Jiri Olsa352ea452014-01-07 13:47:25 +01001801 sz = hweight_long(sample->user_regs.mask) * sizeof(u64);
Adrian Hunterd03f2172013-08-27 11:23:11 +03001802 memcpy(array, sample->user_regs.regs, sz);
1803 array = (void *)array + sz;
1804 } else {
1805 *array++ = 0;
1806 }
1807 }
1808
1809 if (type & PERF_SAMPLE_STACK_USER) {
1810 sz = sample->user_stack.size;
1811 *array++ = sz;
1812 if (sz) {
1813 memcpy(array, sample->user_stack.data, sz);
1814 array = (void *)array + sz;
1815 *array++ = sz;
1816 }
1817 }
1818
1819 if (type & PERF_SAMPLE_WEIGHT) {
1820 *array = sample->weight;
1821 array++;
1822 }
1823
1824 if (type & PERF_SAMPLE_DATA_SRC) {
1825 *array = sample->data_src;
1826 array++;
1827 }
1828
Adrian Hunter42d88912013-11-01 15:51:38 +02001829 if (type & PERF_SAMPLE_TRANSACTION) {
1830 *array = sample->transaction;
1831 array++;
1832 }
1833
Stephane Eranian6a21c0b2014-09-24 13:48:39 +02001834 if (type & PERF_SAMPLE_REGS_INTR) {
1835 if (sample->intr_regs.abi) {
1836 *array++ = sample->intr_regs.abi;
1837 sz = hweight_long(sample->intr_regs.mask) * sizeof(u64);
1838 memcpy(array, sample->intr_regs.regs, sz);
1839 array = (void *)array + sz;
1840 } else {
1841 *array++ = 0;
1842 }
1843 }
1844
Andrew Vagin74eec262011-11-28 12:03:31 +03001845 return 0;
1846}
Arnaldo Carvalho de Melo5555ded2012-09-11 19:24:23 -03001847
Arnaldo Carvalho de Meloefd2b922012-09-18 11:21:50 -03001848struct format_field *perf_evsel__field(struct perf_evsel *evsel, const char *name)
1849{
1850 return pevent_find_field(evsel->tp_format, name);
1851}
1852
Arnaldo Carvalho de Melo5d2074e2012-09-26 20:22:00 -03001853void *perf_evsel__rawptr(struct perf_evsel *evsel, struct perf_sample *sample,
Arnaldo Carvalho de Melo5555ded2012-09-11 19:24:23 -03001854 const char *name)
1855{
Arnaldo Carvalho de Meloefd2b922012-09-18 11:21:50 -03001856 struct format_field *field = perf_evsel__field(evsel, name);
Arnaldo Carvalho de Melo5555ded2012-09-11 19:24:23 -03001857 int offset;
1858
Arnaldo Carvalho de Meloefd2b922012-09-18 11:21:50 -03001859 if (!field)
1860 return NULL;
Arnaldo Carvalho de Melo5555ded2012-09-11 19:24:23 -03001861
1862 offset = field->offset;
1863
1864 if (field->flags & FIELD_IS_DYNAMIC) {
1865 offset = *(int *)(sample->raw_data + field->offset);
1866 offset &= 0xffff;
1867 }
1868
1869 return sample->raw_data + offset;
1870}
1871
1872u64 perf_evsel__intval(struct perf_evsel *evsel, struct perf_sample *sample,
1873 const char *name)
1874{
Arnaldo Carvalho de Meloefd2b922012-09-18 11:21:50 -03001875 struct format_field *field = perf_evsel__field(evsel, name);
Arnaldo Carvalho de Meloe6b6f672012-09-26 13:13:04 -03001876 void *ptr;
1877 u64 value;
Arnaldo Carvalho de Melo5555ded2012-09-11 19:24:23 -03001878
Arnaldo Carvalho de Meloefd2b922012-09-18 11:21:50 -03001879 if (!field)
1880 return 0;
Arnaldo Carvalho de Melo5555ded2012-09-11 19:24:23 -03001881
Arnaldo Carvalho de Meloe6b6f672012-09-26 13:13:04 -03001882 ptr = sample->raw_data + field->offset;
Arnaldo Carvalho de Melo5555ded2012-09-11 19:24:23 -03001883
Arnaldo Carvalho de Meloe6b6f672012-09-26 13:13:04 -03001884 switch (field->size) {
1885 case 1:
1886 return *(u8 *)ptr;
1887 case 2:
1888 value = *(u16 *)ptr;
1889 break;
1890 case 4:
1891 value = *(u32 *)ptr;
1892 break;
1893 case 8:
1894 value = *(u64 *)ptr;
1895 break;
1896 default:
1897 return 0;
1898 }
1899
1900 if (!evsel->needs_swap)
1901 return value;
1902
1903 switch (field->size) {
1904 case 2:
1905 return bswap_16(value);
1906 case 4:
1907 return bswap_32(value);
1908 case 8:
1909 return bswap_64(value);
1910 default:
1911 return 0;
1912 }
1913
1914 return 0;
Arnaldo Carvalho de Melo5555ded2012-09-11 19:24:23 -03001915}
Arnaldo Carvalho de Melo0698aed2012-12-10 18:17:08 -03001916
1917static int comma_fprintf(FILE *fp, bool *first, const char *fmt, ...)
1918{
1919 va_list args;
1920 int ret = 0;
1921
1922 if (!*first) {
1923 ret += fprintf(fp, ",");
1924 } else {
1925 ret += fprintf(fp, ":");
1926 *first = false;
1927 }
1928
1929 va_start(args, fmt);
1930 ret += vfprintf(fp, fmt, args);
1931 va_end(args);
1932 return ret;
1933}
1934
1935static int __if_fprintf(FILE *fp, bool *first, const char *field, u64 value)
1936{
1937 if (value == 0)
1938 return 0;
1939
1940 return comma_fprintf(fp, first, " %s: %" PRIu64, field, value);
1941}
1942
1943#define if_print(field) printed += __if_fprintf(fp, &first, #field, evsel->attr.field)
1944
Arnaldo Carvalho de Meloc79a4392012-12-11 10:54:12 -03001945struct bit_names {
1946 int bit;
1947 const char *name;
1948};
1949
1950static int bits__fprintf(FILE *fp, const char *field, u64 value,
1951 struct bit_names *bits, bool *first)
1952{
1953 int i = 0, printed = comma_fprintf(fp, first, " %s: ", field);
1954 bool first_bit = true;
1955
1956 do {
1957 if (value & bits[i].bit) {
1958 printed += fprintf(fp, "%s%s", first_bit ? "" : "|", bits[i].name);
1959 first_bit = false;
1960 }
1961 } while (bits[++i].name != NULL);
1962
1963 return printed;
1964}
1965
1966static int sample_type__fprintf(FILE *fp, bool *first, u64 value)
1967{
1968#define bit_name(n) { PERF_SAMPLE_##n, #n }
1969 struct bit_names bits[] = {
1970 bit_name(IP), bit_name(TID), bit_name(TIME), bit_name(ADDR),
1971 bit_name(READ), bit_name(CALLCHAIN), bit_name(ID), bit_name(CPU),
1972 bit_name(PERIOD), bit_name(STREAM_ID), bit_name(RAW),
1973 bit_name(BRANCH_STACK), bit_name(REGS_USER), bit_name(STACK_USER),
Stephane Eranian6a21c0b2014-09-24 13:48:39 +02001974 bit_name(IDENTIFIER), bit_name(REGS_INTR),
Arnaldo Carvalho de Meloc79a4392012-12-11 10:54:12 -03001975 { .name = NULL, }
1976 };
1977#undef bit_name
1978 return bits__fprintf(fp, "sample_type", value, bits, first);
1979}
1980
1981static int read_format__fprintf(FILE *fp, bool *first, u64 value)
1982{
1983#define bit_name(n) { PERF_FORMAT_##n, #n }
1984 struct bit_names bits[] = {
1985 bit_name(TOTAL_TIME_ENABLED), bit_name(TOTAL_TIME_RUNNING),
1986 bit_name(ID), bit_name(GROUP),
1987 { .name = NULL, }
1988 };
1989#undef bit_name
1990 return bits__fprintf(fp, "read_format", value, bits, first);
1991}
1992
Arnaldo Carvalho de Melo0698aed2012-12-10 18:17:08 -03001993int perf_evsel__fprintf(struct perf_evsel *evsel,
1994 struct perf_attr_details *details, FILE *fp)
1995{
1996 bool first = true;
Namhyung Kime6ab07d2013-01-22 18:09:47 +09001997 int printed = 0;
1998
Arnaldo Carvalho de Meloe35ef3552013-02-06 17:20:02 -03001999 if (details->event_group) {
Namhyung Kime6ab07d2013-01-22 18:09:47 +09002000 struct perf_evsel *pos;
2001
2002 if (!perf_evsel__is_group_leader(evsel))
2003 return 0;
2004
2005 if (evsel->nr_members > 1)
2006 printed += fprintf(fp, "%s{", evsel->group_name ?: "");
2007
2008 printed += fprintf(fp, "%s", perf_evsel__name(evsel));
2009 for_each_group_member(pos, evsel)
2010 printed += fprintf(fp, ",%s", perf_evsel__name(pos));
2011
2012 if (evsel->nr_members > 1)
2013 printed += fprintf(fp, "}");
2014 goto out;
2015 }
2016
2017 printed += fprintf(fp, "%s", perf_evsel__name(evsel));
Arnaldo Carvalho de Melo0698aed2012-12-10 18:17:08 -03002018
2019 if (details->verbose || details->freq) {
2020 printed += comma_fprintf(fp, &first, " sample_freq=%" PRIu64,
2021 (u64)evsel->attr.sample_freq);
2022 }
2023
2024 if (details->verbose) {
2025 if_print(type);
2026 if_print(config);
2027 if_print(config1);
2028 if_print(config2);
2029 if_print(size);
Arnaldo Carvalho de Meloc79a4392012-12-11 10:54:12 -03002030 printed += sample_type__fprintf(fp, &first, evsel->attr.sample_type);
2031 if (evsel->attr.read_format)
2032 printed += read_format__fprintf(fp, &first, evsel->attr.read_format);
Arnaldo Carvalho de Melo0698aed2012-12-10 18:17:08 -03002033 if_print(disabled);
2034 if_print(inherit);
2035 if_print(pinned);
2036 if_print(exclusive);
2037 if_print(exclude_user);
2038 if_print(exclude_kernel);
2039 if_print(exclude_hv);
2040 if_print(exclude_idle);
2041 if_print(mmap);
Stephane Eranian5c5e8542013-08-21 12:10:25 +02002042 if_print(mmap2);
Arnaldo Carvalho de Melo0698aed2012-12-10 18:17:08 -03002043 if_print(comm);
Adrian Hunter022c50d2014-07-14 13:02:27 +03002044 if_print(comm_exec);
Arnaldo Carvalho de Melo0698aed2012-12-10 18:17:08 -03002045 if_print(freq);
2046 if_print(inherit_stat);
2047 if_print(enable_on_exec);
2048 if_print(task);
2049 if_print(watermark);
2050 if_print(precise_ip);
2051 if_print(mmap_data);
2052 if_print(sample_id_all);
2053 if_print(exclude_host);
2054 if_print(exclude_guest);
2055 if_print(__reserved_1);
2056 if_print(wakeup_events);
2057 if_print(bp_type);
2058 if_print(branch_sample_type);
2059 }
Namhyung Kime6ab07d2013-01-22 18:09:47 +09002060out:
Arnaldo Carvalho de Melo0698aed2012-12-10 18:17:08 -03002061 fputc('\n', fp);
2062 return ++printed;
2063}
Arnaldo Carvalho de Meloc0a54342012-12-13 14:16:30 -03002064
2065bool perf_evsel__fallback(struct perf_evsel *evsel, int err,
2066 char *msg, size_t msgsize)
2067{
David Ahern2b821cc2013-07-18 17:27:59 -06002068 if ((err == ENOENT || err == ENXIO || err == ENODEV) &&
Arnaldo Carvalho de Meloc0a54342012-12-13 14:16:30 -03002069 evsel->attr.type == PERF_TYPE_HARDWARE &&
2070 evsel->attr.config == PERF_COUNT_HW_CPU_CYCLES) {
2071 /*
2072 * If it's cycles then fall back to hrtimer based
2073 * cpu-clock-tick sw counter, which is always available even if
2074 * no PMU support.
2075 *
2076 * PPC returns ENXIO until 2.6.37 (behavior changed with commit
2077 * b0a873e).
2078 */
2079 scnprintf(msg, msgsize, "%s",
2080"The cycles event is not supported, trying to fall back to cpu-clock-ticks");
2081
2082 evsel->attr.type = PERF_TYPE_SOFTWARE;
2083 evsel->attr.config = PERF_COUNT_SW_CPU_CLOCK;
2084
Arnaldo Carvalho de Melo04662522013-12-26 17:41:15 -03002085 zfree(&evsel->name);
Arnaldo Carvalho de Meloc0a54342012-12-13 14:16:30 -03002086 return true;
2087 }
2088
2089 return false;
2090}
Arnaldo Carvalho de Melo56e52e82012-12-13 15:10:58 -03002091
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03002092int perf_evsel__open_strerror(struct perf_evsel *evsel, struct target *target,
Arnaldo Carvalho de Melo56e52e82012-12-13 15:10:58 -03002093 int err, char *msg, size_t size)
2094{
Masami Hiramatsu6e81c742014-08-14 02:22:36 +00002095 char sbuf[STRERR_BUFSIZE];
2096
Arnaldo Carvalho de Melo56e52e82012-12-13 15:10:58 -03002097 switch (err) {
2098 case EPERM:
2099 case EACCES:
David Ahernb69e63a2013-05-25 17:54:00 -06002100 return scnprintf(msg, size,
Arnaldo Carvalho de Melo56e52e82012-12-13 15:10:58 -03002101 "You may not have permission to collect %sstats.\n"
2102 "Consider tweaking /proc/sys/kernel/perf_event_paranoid:\n"
2103 " -1 - Not paranoid at all\n"
2104 " 0 - Disallow raw tracepoint access for unpriv\n"
2105 " 1 - Disallow cpu events for unpriv\n"
2106 " 2 - Disallow kernel profiling for unpriv",
2107 target->system_wide ? "system-wide " : "");
2108 case ENOENT:
2109 return scnprintf(msg, size, "The %s event is not supported.",
2110 perf_evsel__name(evsel));
2111 case EMFILE:
2112 return scnprintf(msg, size, "%s",
2113 "Too many events are opened.\n"
2114 "Try again after reducing the number of events.");
2115 case ENODEV:
2116 if (target->cpu_list)
2117 return scnprintf(msg, size, "%s",
2118 "No such device - did you specify an out-of-range profile CPU?\n");
2119 break;
2120 case EOPNOTSUPP:
2121 if (evsel->attr.precise_ip)
2122 return scnprintf(msg, size, "%s",
2123 "\'precise\' request may not be supported. Try removing 'p' modifier.");
2124#if defined(__i386__) || defined(__x86_64__)
2125 if (evsel->attr.type == PERF_TYPE_HARDWARE)
2126 return scnprintf(msg, size, "%s",
2127 "No hardware sampling interrupt available.\n"
2128 "No APIC? If so then you can boot the kernel with the \"lapic\" boot parameter to force-enable it.");
2129#endif
2130 break;
Jiri Olsa63914ac2014-08-01 17:46:54 +02002131 case EBUSY:
2132 if (find_process("oprofiled"))
2133 return scnprintf(msg, size,
2134 "The PMU counters are busy/taken by another profiler.\n"
2135 "We found oprofile daemon running, please stop it and try again.");
2136 break;
Arnaldo Carvalho de Melo56e52e82012-12-13 15:10:58 -03002137 default:
2138 break;
2139 }
2140
2141 return scnprintf(msg, size,
Masami Hiramatsu6e81c742014-08-14 02:22:36 +00002142 "The sys_perf_event_open() syscall returned with %d (%s) for event (%s).\n"
Arnaldo Carvalho de Melo56e52e82012-12-13 15:10:58 -03002143 "/bin/dmesg may provide additional information.\n"
2144 "No CONFIG_PERF_EVENTS=y kernel support configured?\n",
Masami Hiramatsu6e81c742014-08-14 02:22:36 +00002145 err, strerror_r(err, sbuf, sizeof(sbuf)),
2146 perf_evsel__name(evsel));
Arnaldo Carvalho de Melo56e52e82012-12-13 15:10:58 -03002147}