blob: 34344ffa79cacd659dc286070869b537f6e9fd76 [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
Namhyung Kim72a128a2014-09-23 10:01:41 +0900661 if (callchain_param.enabled && !evsel->no_aux_samples)
662 perf_evsel__config_callgraph(evsel);
Jiri Olsa26d33022012-08-07 15:20:47 +0200663
Stephane Eranian6a21c0b2014-09-24 13:48:39 +0200664 if (opts->sample_intr_regs) {
665 attr->sample_regs_intr = PERF_REGS_MASK;
666 perf_evsel__set_sample_bit(evsel, REGS_INTR);
667 }
668
Adrian Hunter3aa59392013-11-15 15:52:29 +0200669 if (target__has_cpu(&opts->target))
Arnaldo Carvalho de Melo7be5ebe2012-12-10 14:53:43 -0300670 perf_evsel__set_sample_bit(evsel, CPU);
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200671
Andrew Vagin3e76ac72011-12-20 17:32:45 +0300672 if (opts->period)
Arnaldo Carvalho de Melo7be5ebe2012-12-10 14:53:43 -0300673 perf_evsel__set_sample_bit(evsel, PERIOD);
Andrew Vagin3e76ac72011-12-20 17:32:45 +0300674
Andi Kleen8affc2b2014-07-31 14:45:04 +0800675 /*
676 * When the user explicitely disabled time don't force it here.
677 */
678 if (opts->sample_time &&
679 (!perf_missing_features.sample_id_all &&
680 (!opts->no_inherit || target__has_cpu(&opts->target) || per_cpu)))
Arnaldo Carvalho de Melo7be5ebe2012-12-10 14:53:43 -0300681 perf_evsel__set_sample_bit(evsel, TIME);
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200682
Adrian Hunter6ff1ce72014-07-14 13:02:56 +0300683 if (opts->raw_samples && !evsel->no_aux_samples) {
Arnaldo Carvalho de Melo7be5ebe2012-12-10 14:53:43 -0300684 perf_evsel__set_sample_bit(evsel, TIME);
685 perf_evsel__set_sample_bit(evsel, RAW);
686 perf_evsel__set_sample_bit(evsel, CPU);
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200687 }
688
Stephane Eranianccf49bf2013-01-24 16:10:37 +0100689 if (opts->sample_address)
Adrian Hunter1e7ed5e2013-11-01 15:51:35 +0200690 perf_evsel__set_sample_bit(evsel, DATA_SRC);
Stephane Eranianccf49bf2013-01-24 16:10:37 +0100691
Arnaldo Carvalho de Melo509051e2014-01-14 17:52:14 -0300692 if (opts->no_buffering) {
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200693 attr->watermark = 0;
694 attr->wakeup_events = 1;
695 }
Adrian Hunter6ff1ce72014-07-14 13:02:56 +0300696 if (opts->branch_stack && !evsel->no_aux_samples) {
Arnaldo Carvalho de Melo7be5ebe2012-12-10 14:53:43 -0300697 perf_evsel__set_sample_bit(evsel, BRANCH_STACK);
Roberto Agostino Vitillobdfebd82012-02-09 23:21:02 +0100698 attr->branch_sample_type = opts->branch_stack;
699 }
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200700
Andi Kleen05484292013-01-24 16:10:29 +0100701 if (opts->sample_weight)
Adrian Hunter1e7ed5e2013-11-01 15:51:35 +0200702 perf_evsel__set_sample_bit(evsel, WEIGHT);
Andi Kleen05484292013-01-24 16:10:29 +0100703
Stephane Eranian5c5e8542013-08-21 12:10:25 +0200704 attr->mmap = track;
Don Zickusa5a5ba72014-05-30 10:49:42 -0400705 attr->mmap2 = track && !perf_missing_features.mmap2;
Stephane Eranian5c5e8542013-08-21 12:10:25 +0200706 attr->comm = track;
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200707
Andi Kleen475eeab2013-09-20 07:40:43 -0700708 if (opts->sample_transaction)
Adrian Hunter1e7ed5e2013-11-01 15:51:35 +0200709 perf_evsel__set_sample_bit(evsel, TRANSACTION);
Andi Kleen475eeab2013-09-20 07:40:43 -0700710
Jiri Olsa774cb492012-11-12 18:34:01 +0100711 /*
712 * XXX see the function comment above
713 *
714 * Disabling only independent events or group leaders,
715 * keeping group members enabled.
716 */
Namhyung Kim823254e2012-11-29 15:38:30 +0900717 if (perf_evsel__is_group_leader(evsel))
Jiri Olsa774cb492012-11-12 18:34:01 +0100718 attr->disabled = 1;
719
720 /*
721 * Setting enable_on_exec for independent events and
722 * group leaders for traced executed by perf.
723 */
Andi Kleen6619a532014-01-11 13:38:27 -0800724 if (target__none(&opts->target) && perf_evsel__is_group_leader(evsel) &&
725 !opts->initial_delay)
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200726 attr->enable_on_exec = 1;
Adrian Hunter2afd2bc2014-07-14 13:02:57 +0300727
728 if (evsel->immediate) {
729 attr->disabled = 0;
730 attr->enable_on_exec = 0;
731 }
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200732}
733
Arnaldo Carvalho de Melo88858462014-10-13 13:30:27 -0300734static int perf_evsel__alloc_fd(struct perf_evsel *evsel, int ncpus, int nthreads)
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200735{
David Ahern4af4c952011-05-27 09:58:34 -0600736 int cpu, thread;
Adrian Hunterbf8e8f42014-07-31 09:00:51 +0300737
738 if (evsel->system_wide)
739 nthreads = 1;
740
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200741 evsel->fd = xyarray__new(ncpus, nthreads, sizeof(int));
David Ahern4af4c952011-05-27 09:58:34 -0600742
743 if (evsel->fd) {
744 for (cpu = 0; cpu < ncpus; cpu++) {
745 for (thread = 0; thread < nthreads; thread++) {
746 FD(evsel, cpu, thread) = -1;
747 }
748 }
749 }
750
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200751 return evsel->fd != NULL ? 0 : -ENOMEM;
752}
753
Andi Kleene2407be2013-08-02 17:41:10 -0700754static int perf_evsel__run_ioctl(struct perf_evsel *evsel, int ncpus, int nthreads,
755 int ioc, void *arg)
Arnaldo Carvalho de Melo745cefc2012-09-26 15:07:39 -0300756{
757 int cpu, thread;
758
Adrian Hunterbf8e8f42014-07-31 09:00:51 +0300759 if (evsel->system_wide)
760 nthreads = 1;
761
Arnaldo Carvalho de Melo745cefc2012-09-26 15:07:39 -0300762 for (cpu = 0; cpu < ncpus; cpu++) {
763 for (thread = 0; thread < nthreads; thread++) {
764 int fd = FD(evsel, cpu, thread),
Andi Kleene2407be2013-08-02 17:41:10 -0700765 err = ioctl(fd, ioc, arg);
Arnaldo Carvalho de Melo745cefc2012-09-26 15:07:39 -0300766
767 if (err)
768 return err;
769 }
770 }
771
772 return 0;
773}
774
Andi Kleene2407be2013-08-02 17:41:10 -0700775int perf_evsel__set_filter(struct perf_evsel *evsel, int ncpus, int nthreads,
776 const char *filter)
777{
778 return perf_evsel__run_ioctl(evsel, ncpus, nthreads,
779 PERF_EVENT_IOC_SET_FILTER,
780 (void *)filter);
781}
782
783int perf_evsel__enable(struct perf_evsel *evsel, int ncpus, int nthreads)
784{
785 return perf_evsel__run_ioctl(evsel, ncpus, nthreads,
786 PERF_EVENT_IOC_ENABLE,
787 0);
788}
789
Arnaldo Carvalho de Melo70db7532011-01-12 22:39:13 -0200790int perf_evsel__alloc_id(struct perf_evsel *evsel, int ncpus, int nthreads)
791{
Adrian Hunterbf8e8f42014-07-31 09:00:51 +0300792 if (evsel->system_wide)
793 nthreads = 1;
794
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -0300795 evsel->sample_id = xyarray__new(ncpus, nthreads, sizeof(struct perf_sample_id));
796 if (evsel->sample_id == NULL)
797 return -ENOMEM;
798
799 evsel->id = zalloc(ncpus * nthreads * sizeof(u64));
800 if (evsel->id == NULL) {
801 xyarray__delete(evsel->sample_id);
802 evsel->sample_id = NULL;
803 return -ENOMEM;
804 }
805
806 return 0;
Arnaldo Carvalho de Melo70db7532011-01-12 22:39:13 -0200807}
808
Frederik Deweerdta7e191c2013-03-01 13:02:27 -0500809void perf_evsel__reset_counts(struct perf_evsel *evsel, int ncpus)
810{
811 memset(evsel->counts, 0, (sizeof(*evsel->counts) +
812 (ncpus * sizeof(struct perf_counts_values))));
813}
814
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200815int perf_evsel__alloc_counts(struct perf_evsel *evsel, int ncpus)
816{
817 evsel->counts = zalloc((sizeof(*evsel->counts) +
818 (ncpus * sizeof(struct perf_counts_values))));
819 return evsel->counts != NULL ? 0 : -ENOMEM;
820}
821
Arnaldo Carvalho de Melo88858462014-10-13 13:30:27 -0300822static void perf_evsel__free_fd(struct perf_evsel *evsel)
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200823{
824 xyarray__delete(evsel->fd);
825 evsel->fd = NULL;
826}
827
Arnaldo Carvalho de Melo88858462014-10-13 13:30:27 -0300828static void perf_evsel__free_id(struct perf_evsel *evsel)
Arnaldo Carvalho de Melo70db7532011-01-12 22:39:13 -0200829{
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -0300830 xyarray__delete(evsel->sample_id);
831 evsel->sample_id = NULL;
Arnaldo Carvalho de Melo04662522013-12-26 17:41:15 -0300832 zfree(&evsel->id);
Arnaldo Carvalho de Melo70db7532011-01-12 22:39:13 -0200833}
834
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200835void perf_evsel__close_fd(struct perf_evsel *evsel, int ncpus, int nthreads)
836{
837 int cpu, thread;
838
Adrian Hunterbf8e8f42014-07-31 09:00:51 +0300839 if (evsel->system_wide)
840 nthreads = 1;
841
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200842 for (cpu = 0; cpu < ncpus; cpu++)
843 for (thread = 0; thread < nthreads; ++thread) {
844 close(FD(evsel, cpu, thread));
845 FD(evsel, cpu, thread) = -1;
846 }
847}
848
Namhyung Kim43f8e762013-01-25 10:44:44 +0900849void perf_evsel__free_counts(struct perf_evsel *evsel)
850{
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -0300851 zfree(&evsel->counts);
Namhyung Kim43f8e762013-01-25 10:44:44 +0900852}
853
Arnaldo Carvalho de Meloef1d1af2011-01-18 21:41:45 -0200854void perf_evsel__exit(struct perf_evsel *evsel)
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200855{
856 assert(list_empty(&evsel->node));
Namhyung Kim736b05a2013-03-15 14:48:49 +0900857 perf_evsel__free_fd(evsel);
858 perf_evsel__free_id(evsel);
Arnaldo Carvalho de Melo597e48c2014-10-16 13:25:01 -0300859 close_cgroup(evsel->cgrp);
860 zfree(&evsel->group_name);
Arnaldo Carvalho de Melo597e48c2014-10-16 13:25:01 -0300861 zfree(&evsel->name);
Arnaldo Carvalho de Meloce8ccff2014-10-09 15:29:51 -0300862 perf_evsel__object.fini(evsel);
Arnaldo Carvalho de Meloef1d1af2011-01-18 21:41:45 -0200863}
864
865void perf_evsel__delete(struct perf_evsel *evsel)
866{
867 perf_evsel__exit(evsel);
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200868 free(evsel);
869}
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200870
Stephane Eranianc7a79c42013-01-29 12:47:43 +0100871static inline void compute_deltas(struct perf_evsel *evsel,
872 int cpu,
873 struct perf_counts_values *count)
874{
875 struct perf_counts_values tmp;
876
877 if (!evsel->prev_raw_counts)
878 return;
879
880 if (cpu == -1) {
881 tmp = evsel->prev_raw_counts->aggr;
882 evsel->prev_raw_counts->aggr = *count;
883 } else {
884 tmp = evsel->prev_raw_counts->cpu[cpu];
885 evsel->prev_raw_counts->cpu[cpu] = *count;
886 }
887
888 count->val = count->val - tmp.val;
889 count->ena = count->ena - tmp.ena;
890 count->run = count->run - tmp.run;
891}
892
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200893int __perf_evsel__read_on_cpu(struct perf_evsel *evsel,
894 int cpu, int thread, bool scale)
895{
896 struct perf_counts_values count;
897 size_t nv = scale ? 3 : 1;
898
899 if (FD(evsel, cpu, thread) < 0)
900 return -EINVAL;
901
Arnaldo Carvalho de Melo4eed11d2011-01-04 00:13:17 -0200902 if (evsel->counts == NULL && perf_evsel__alloc_counts(evsel, cpu + 1) < 0)
903 return -ENOMEM;
904
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200905 if (readn(FD(evsel, cpu, thread), &count, nv * sizeof(u64)) < 0)
906 return -errno;
907
Stephane Eranianc7a79c42013-01-29 12:47:43 +0100908 compute_deltas(evsel, cpu, &count);
909
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200910 if (scale) {
911 if (count.run == 0)
912 count.val = 0;
913 else if (count.run < count.ena)
914 count.val = (u64)((double)count.val * count.ena / count.run + 0.5);
915 } else
916 count.ena = count.run = 0;
917
918 evsel->counts->cpu[cpu] = count;
919 return 0;
920}
921
922int __perf_evsel__read(struct perf_evsel *evsel,
923 int ncpus, int nthreads, bool scale)
924{
925 size_t nv = scale ? 3 : 1;
926 int cpu, thread;
927 struct perf_counts_values *aggr = &evsel->counts->aggr, count;
928
Adrian Hunterbf8e8f42014-07-31 09:00:51 +0300929 if (evsel->system_wide)
930 nthreads = 1;
931
Arnaldo Carvalho de Melo52bcd9942011-02-03 17:26:06 -0200932 aggr->val = aggr->ena = aggr->run = 0;
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200933
934 for (cpu = 0; cpu < ncpus; cpu++) {
935 for (thread = 0; thread < nthreads; thread++) {
936 if (FD(evsel, cpu, thread) < 0)
937 continue;
938
939 if (readn(FD(evsel, cpu, thread),
940 &count, nv * sizeof(u64)) < 0)
941 return -errno;
942
943 aggr->val += count.val;
944 if (scale) {
945 aggr->ena += count.ena;
946 aggr->run += count.run;
947 }
948 }
949 }
950
Stephane Eranianc7a79c42013-01-29 12:47:43 +0100951 compute_deltas(evsel, -1, aggr);
952
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200953 evsel->counts->scaled = 0;
954 if (scale) {
955 if (aggr->run == 0) {
956 evsel->counts->scaled = -1;
957 aggr->val = 0;
958 return 0;
959 }
960
961 if (aggr->run < aggr->ena) {
962 evsel->counts->scaled = 1;
963 aggr->val = (u64)((double)aggr->val * aggr->ena / aggr->run + 0.5);
964 }
965 } else
966 aggr->ena = aggr->run = 0;
967
968 return 0;
969}
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -0200970
Jiri Olsa6a4bb042012-08-08 12:22:36 +0200971static int get_group_fd(struct perf_evsel *evsel, int cpu, int thread)
972{
973 struct perf_evsel *leader = evsel->leader;
974 int fd;
975
Namhyung Kim823254e2012-11-29 15:38:30 +0900976 if (perf_evsel__is_group_leader(evsel))
Jiri Olsa6a4bb042012-08-08 12:22:36 +0200977 return -1;
978
979 /*
980 * Leader must be already processed/open,
981 * if not it's a bug.
982 */
983 BUG_ON(!leader->fd);
984
985 fd = FD(leader, cpu, thread);
986 BUG_ON(fd == -1);
987
988 return fd;
989}
990
Adrian Huntere3e1a542013-08-14 15:48:24 +0300991#define __PRINT_ATTR(fmt, cast, field) \
992 fprintf(fp, " %-19s "fmt"\n", #field, cast attr->field)
993
994#define PRINT_ATTR_U32(field) __PRINT_ATTR("%u" , , field)
995#define PRINT_ATTR_X32(field) __PRINT_ATTR("%#x", , field)
996#define PRINT_ATTR_U64(field) __PRINT_ATTR("%" PRIu64, (uint64_t), field)
997#define PRINT_ATTR_X64(field) __PRINT_ATTR("%#"PRIx64, (uint64_t), field)
998
999#define PRINT_ATTR2N(name1, field1, name2, field2) \
1000 fprintf(fp, " %-19s %u %-19s %u\n", \
1001 name1, attr->field1, name2, attr->field2)
1002
1003#define PRINT_ATTR2(field1, field2) \
1004 PRINT_ATTR2N(#field1, field1, #field2, field2)
1005
1006static size_t perf_event_attr__fprintf(struct perf_event_attr *attr, FILE *fp)
1007{
1008 size_t ret = 0;
1009
1010 ret += fprintf(fp, "%.60s\n", graph_dotted_line);
1011 ret += fprintf(fp, "perf_event_attr:\n");
1012
1013 ret += PRINT_ATTR_U32(type);
1014 ret += PRINT_ATTR_U32(size);
1015 ret += PRINT_ATTR_X64(config);
1016 ret += PRINT_ATTR_U64(sample_period);
1017 ret += PRINT_ATTR_U64(sample_freq);
1018 ret += PRINT_ATTR_X64(sample_type);
1019 ret += PRINT_ATTR_X64(read_format);
1020
1021 ret += PRINT_ATTR2(disabled, inherit);
1022 ret += PRINT_ATTR2(pinned, exclusive);
1023 ret += PRINT_ATTR2(exclude_user, exclude_kernel);
1024 ret += PRINT_ATTR2(exclude_hv, exclude_idle);
1025 ret += PRINT_ATTR2(mmap, comm);
Adrian Hunter022c50d2014-07-14 13:02:27 +03001026 ret += PRINT_ATTR2(mmap2, comm_exec);
Adrian Huntere3e1a542013-08-14 15:48:24 +03001027 ret += PRINT_ATTR2(freq, inherit_stat);
1028 ret += PRINT_ATTR2(enable_on_exec, task);
1029 ret += PRINT_ATTR2(watermark, precise_ip);
1030 ret += PRINT_ATTR2(mmap_data, sample_id_all);
1031 ret += PRINT_ATTR2(exclude_host, exclude_guest);
1032 ret += PRINT_ATTR2N("excl.callchain_kern", exclude_callchain_kernel,
1033 "excl.callchain_user", exclude_callchain_user);
1034
1035 ret += PRINT_ATTR_U32(wakeup_events);
1036 ret += PRINT_ATTR_U32(wakeup_watermark);
1037 ret += PRINT_ATTR_X32(bp_type);
1038 ret += PRINT_ATTR_X64(bp_addr);
1039 ret += PRINT_ATTR_X64(config1);
1040 ret += PRINT_ATTR_U64(bp_len);
1041 ret += PRINT_ATTR_X64(config2);
1042 ret += PRINT_ATTR_X64(branch_sample_type);
1043 ret += PRINT_ATTR_X64(sample_regs_user);
1044 ret += PRINT_ATTR_U32(sample_stack_user);
Stephane Eranian6a21c0b2014-09-24 13:48:39 +02001045 ret += PRINT_ATTR_X64(sample_regs_intr);
Adrian Huntere3e1a542013-08-14 15:48:24 +03001046
1047 ret += fprintf(fp, "%.60s\n", graph_dotted_line);
1048
1049 return ret;
1050}
1051
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -02001052static int __perf_evsel__open(struct perf_evsel *evsel, struct cpu_map *cpus,
Jiri Olsa6a4bb042012-08-08 12:22:36 +02001053 struct thread_map *threads)
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -02001054{
Adrian Hunterbf8e8f42014-07-31 09:00:51 +03001055 int cpu, thread, nthreads;
Yann Droneaud57480d22014-06-30 22:28:47 +02001056 unsigned long flags = PERF_FLAG_FD_CLOEXEC;
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -02001057 int pid = -1, err;
Andi Kleenbec19672013-08-04 19:41:26 -07001058 enum { NO_CHANGE, SET_TO_MAX, INCREASED_MAX } set_rlimit = NO_CHANGE;
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -02001059
Adrian Hunterbf8e8f42014-07-31 09:00:51 +03001060 if (evsel->system_wide)
1061 nthreads = 1;
1062 else
1063 nthreads = threads->nr;
1064
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -02001065 if (evsel->fd == NULL &&
Adrian Hunterbf8e8f42014-07-31 09:00:51 +03001066 perf_evsel__alloc_fd(evsel, cpus->nr, nthreads) < 0)
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -02001067 return -ENOMEM;
Arnaldo Carvalho de Melo4eed11d2011-01-04 00:13:17 -02001068
Stephane Eranian023695d2011-02-14 11:20:01 +02001069 if (evsel->cgrp) {
Yann Droneaud57480d22014-06-30 22:28:47 +02001070 flags |= PERF_FLAG_PID_CGROUP;
Stephane Eranian023695d2011-02-14 11:20:01 +02001071 pid = evsel->cgrp->fd;
1072 }
1073
Arnaldo Carvalho de Melo594ac612012-12-13 13:13:07 -03001074fallback_missing_features:
Yann Droneaud57480d22014-06-30 22:28:47 +02001075 if (perf_missing_features.cloexec)
1076 flags &= ~(unsigned long)PERF_FLAG_FD_CLOEXEC;
Stephane Eranian5c5e8542013-08-21 12:10:25 +02001077 if (perf_missing_features.mmap2)
1078 evsel->attr.mmap2 = 0;
Arnaldo Carvalho de Melo594ac612012-12-13 13:13:07 -03001079 if (perf_missing_features.exclude_guest)
1080 evsel->attr.exclude_guest = evsel->attr.exclude_host = 0;
1081retry_sample_id:
1082 if (perf_missing_features.sample_id_all)
1083 evsel->attr.sample_id_all = 0;
1084
Adrian Huntere3e1a542013-08-14 15:48:24 +03001085 if (verbose >= 2)
1086 perf_event_attr__fprintf(&evsel->attr, stderr);
1087
Arnaldo Carvalho de Melo86bd5e82011-01-03 23:09:46 -02001088 for (cpu = 0; cpu < cpus->nr; cpu++) {
Arnaldo Carvalho de Melo9d04f172011-01-12 00:08:18 -02001089
Adrian Hunterbf8e8f42014-07-31 09:00:51 +03001090 for (thread = 0; thread < nthreads; thread++) {
Jiri Olsa6a4bb042012-08-08 12:22:36 +02001091 int group_fd;
Stephane Eranian023695d2011-02-14 11:20:01 +02001092
Adrian Hunterbf8e8f42014-07-31 09:00:51 +03001093 if (!evsel->cgrp && !evsel->system_wide)
Stephane Eranian023695d2011-02-14 11:20:01 +02001094 pid = threads->map[thread];
1095
Jiri Olsa6a4bb042012-08-08 12:22:36 +02001096 group_fd = get_group_fd(evsel, cpu, thread);
Andi Kleenbec19672013-08-04 19:41:26 -07001097retry_open:
Ramkumar Ramachandraa33f6ef2014-03-18 15:10:42 -04001098 pr_debug2("sys_perf_event_open: pid %d cpu %d group_fd %d flags %#lx\n",
Adrian Huntere3e1a542013-08-14 15:48:24 +03001099 pid, cpus->map[cpu], group_fd, flags);
1100
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -02001101 FD(evsel, cpu, thread) = sys_perf_event_open(&evsel->attr,
Stephane Eranian023695d2011-02-14 11:20:01 +02001102 pid,
Arnaldo Carvalho de Melof08199d2011-01-11 23:42:19 -02001103 cpus->map[cpu],
Stephane Eranian023695d2011-02-14 11:20:01 +02001104 group_fd, flags);
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -02001105 if (FD(evsel, cpu, thread) < 0) {
1106 err = -errno;
Ramkumar Ramachandraa33f6ef2014-03-18 15:10:42 -04001107 pr_debug2("sys_perf_event_open failed, error %d\n",
Adrian Hunterf852fd62013-11-01 15:51:29 +02001108 err);
Arnaldo Carvalho de Melo594ac612012-12-13 13:13:07 -03001109 goto try_fallback;
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -02001110 }
Andi Kleenbec19672013-08-04 19:41:26 -07001111 set_rlimit = NO_CHANGE;
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -02001112 }
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -02001113 }
1114
1115 return 0;
1116
Arnaldo Carvalho de Melo594ac612012-12-13 13:13:07 -03001117try_fallback:
Andi Kleenbec19672013-08-04 19:41:26 -07001118 /*
1119 * perf stat needs between 5 and 22 fds per CPU. When we run out
1120 * of them try to increase the limits.
1121 */
1122 if (err == -EMFILE && set_rlimit < INCREASED_MAX) {
1123 struct rlimit l;
1124 int old_errno = errno;
1125
1126 if (getrlimit(RLIMIT_NOFILE, &l) == 0) {
1127 if (set_rlimit == NO_CHANGE)
1128 l.rlim_cur = l.rlim_max;
1129 else {
1130 l.rlim_cur = l.rlim_max + 1000;
1131 l.rlim_max = l.rlim_cur;
1132 }
1133 if (setrlimit(RLIMIT_NOFILE, &l) == 0) {
1134 set_rlimit++;
1135 errno = old_errno;
1136 goto retry_open;
1137 }
1138 }
1139 errno = old_errno;
1140 }
1141
Arnaldo Carvalho de Melo594ac612012-12-13 13:13:07 -03001142 if (err != -EINVAL || cpu > 0 || thread > 0)
1143 goto out_close;
1144
Yann Droneaud57480d22014-06-30 22:28:47 +02001145 if (!perf_missing_features.cloexec && (flags & PERF_FLAG_FD_CLOEXEC)) {
1146 perf_missing_features.cloexec = true;
1147 goto fallback_missing_features;
1148 } else if (!perf_missing_features.mmap2 && evsel->attr.mmap2) {
Stephane Eranian5c5e8542013-08-21 12:10:25 +02001149 perf_missing_features.mmap2 = true;
1150 goto fallback_missing_features;
1151 } else if (!perf_missing_features.exclude_guest &&
1152 (evsel->attr.exclude_guest || evsel->attr.exclude_host)) {
Arnaldo Carvalho de Melo594ac612012-12-13 13:13:07 -03001153 perf_missing_features.exclude_guest = true;
1154 goto fallback_missing_features;
1155 } else if (!perf_missing_features.sample_id_all) {
1156 perf_missing_features.sample_id_all = true;
1157 goto retry_sample_id;
1158 }
1159
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -02001160out_close:
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -02001161 do {
1162 while (--thread >= 0) {
1163 close(FD(evsel, cpu, thread));
1164 FD(evsel, cpu, thread) = -1;
1165 }
Adrian Hunterbf8e8f42014-07-31 09:00:51 +03001166 thread = nthreads;
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -02001167 } while (--cpu >= 0);
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -02001168 return err;
1169}
1170
1171void perf_evsel__close(struct perf_evsel *evsel, int ncpus, int nthreads)
1172{
1173 if (evsel->fd == NULL)
1174 return;
1175
1176 perf_evsel__close_fd(evsel, ncpus, nthreads);
1177 perf_evsel__free_fd(evsel);
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -02001178}
1179
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -02001180static struct {
1181 struct cpu_map map;
1182 int cpus[1];
1183} empty_cpu_map = {
1184 .map.nr = 1,
1185 .cpus = { -1, },
1186};
1187
1188static struct {
1189 struct thread_map map;
1190 int threads[1];
1191} empty_thread_map = {
1192 .map.nr = 1,
1193 .threads = { -1, },
1194};
1195
Arnaldo Carvalho de Melof08199d2011-01-11 23:42:19 -02001196int perf_evsel__open(struct perf_evsel *evsel, struct cpu_map *cpus,
Jiri Olsa6a4bb042012-08-08 12:22:36 +02001197 struct thread_map *threads)
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -02001198{
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -02001199 if (cpus == NULL) {
1200 /* Work around old compiler warnings about strict aliasing */
1201 cpus = &empty_cpu_map.map;
1202 }
1203
1204 if (threads == NULL)
1205 threads = &empty_thread_map.map;
1206
Jiri Olsa6a4bb042012-08-08 12:22:36 +02001207 return __perf_evsel__open(evsel, cpus, threads);
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -02001208}
1209
Arnaldo Carvalho de Melof08199d2011-01-11 23:42:19 -02001210int perf_evsel__open_per_cpu(struct perf_evsel *evsel,
Jiri Olsa6a4bb042012-08-08 12:22:36 +02001211 struct cpu_map *cpus)
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -02001212{
Jiri Olsa6a4bb042012-08-08 12:22:36 +02001213 return __perf_evsel__open(evsel, cpus, &empty_thread_map.map);
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -02001214}
1215
Arnaldo Carvalho de Melof08199d2011-01-11 23:42:19 -02001216int perf_evsel__open_per_thread(struct perf_evsel *evsel,
Jiri Olsa6a4bb042012-08-08 12:22:36 +02001217 struct thread_map *threads)
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -02001218{
Jiri Olsa6a4bb042012-08-08 12:22:36 +02001219 return __perf_evsel__open(evsel, &empty_cpu_map.map, threads);
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -02001220}
Arnaldo Carvalho de Melo70082dd2011-01-12 17:03:24 -02001221
Arnaldo Carvalho de Melo0807d2d2012-09-26 12:48:18 -03001222static int perf_evsel__parse_id_sample(const struct perf_evsel *evsel,
1223 const union perf_event *event,
1224 struct perf_sample *sample)
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001225{
Arnaldo Carvalho de Melo0807d2d2012-09-26 12:48:18 -03001226 u64 type = evsel->attr.sample_type;
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001227 const u64 *array = event->sample.array;
Arnaldo Carvalho de Melo0807d2d2012-09-26 12:48:18 -03001228 bool swapped = evsel->needs_swap;
Jiri Olsa37073f92012-05-30 14:23:44 +02001229 union u64_swap u;
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001230
1231 array += ((event->header.size -
1232 sizeof(event->header)) / sizeof(u64)) - 1;
1233
Adrian Hunter75562572013-08-27 11:23:09 +03001234 if (type & PERF_SAMPLE_IDENTIFIER) {
1235 sample->id = *array;
1236 array--;
1237 }
1238
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001239 if (type & PERF_SAMPLE_CPU) {
Jiri Olsa37073f92012-05-30 14:23:44 +02001240 u.val64 = *array;
1241 if (swapped) {
1242 /* undo swap of u64, then swap on individual u32s */
1243 u.val64 = bswap_64(u.val64);
1244 u.val32[0] = bswap_32(u.val32[0]);
1245 }
1246
1247 sample->cpu = u.val32[0];
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001248 array--;
1249 }
1250
1251 if (type & PERF_SAMPLE_STREAM_ID) {
1252 sample->stream_id = *array;
1253 array--;
1254 }
1255
1256 if (type & PERF_SAMPLE_ID) {
1257 sample->id = *array;
1258 array--;
1259 }
1260
1261 if (type & PERF_SAMPLE_TIME) {
1262 sample->time = *array;
1263 array--;
1264 }
1265
1266 if (type & PERF_SAMPLE_TID) {
Jiri Olsa37073f92012-05-30 14:23:44 +02001267 u.val64 = *array;
1268 if (swapped) {
1269 /* undo swap of u64, then swap on individual u32s */
1270 u.val64 = bswap_64(u.val64);
1271 u.val32[0] = bswap_32(u.val32[0]);
1272 u.val32[1] = bswap_32(u.val32[1]);
1273 }
1274
1275 sample->pid = u.val32[0];
1276 sample->tid = u.val32[1];
Adrian Hunterdd44bc62013-10-18 15:29:01 +03001277 array--;
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001278 }
1279
1280 return 0;
1281}
1282
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001283static inline bool overflow(const void *endp, u16 max_size, const void *offset,
1284 u64 size)
Frederic Weisbecker98e1da92011-05-21 20:08:15 +02001285{
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001286 return size > max_size || offset + size > endp;
Frederic Weisbecker98e1da92011-05-21 20:08:15 +02001287}
1288
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001289#define OVERFLOW_CHECK(offset, size, max_size) \
1290 do { \
1291 if (overflow(endp, (max_size), (offset), (size))) \
1292 return -EFAULT; \
1293 } while (0)
1294
1295#define OVERFLOW_CHECK_u64(offset) \
1296 OVERFLOW_CHECK(offset, sizeof(u64), sizeof(u64))
1297
Arnaldo Carvalho de Meloa3f698f2012-08-02 12:23:46 -03001298int perf_evsel__parse_sample(struct perf_evsel *evsel, union perf_event *event,
Arnaldo Carvalho de Melo0807d2d2012-09-26 12:48:18 -03001299 struct perf_sample *data)
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001300{
Arnaldo Carvalho de Meloa3f698f2012-08-02 12:23:46 -03001301 u64 type = evsel->attr.sample_type;
Arnaldo Carvalho de Melo0807d2d2012-09-26 12:48:18 -03001302 bool swapped = evsel->needs_swap;
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001303 const u64 *array;
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001304 u16 max_size = event->header.size;
1305 const void *endp = (void *)event + max_size;
1306 u64 sz;
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001307
David Ahern936be502011-09-06 09:12:26 -06001308 /*
1309 * used for cross-endian analysis. See git commit 65014ab3
1310 * for why this goofiness is needed.
1311 */
Jiri Olsa6a11f922012-05-16 08:59:04 +02001312 union u64_swap u;
David Ahern936be502011-09-06 09:12:26 -06001313
Robert Richterf3bda2c2011-12-15 17:32:39 +01001314 memset(data, 0, sizeof(*data));
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001315 data->cpu = data->pid = data->tid = -1;
1316 data->stream_id = data->id = data->time = -1ULL;
Jiri Olsabc529082014-02-03 12:44:41 +01001317 data->period = evsel->attr.sample_period;
Andi Kleen05484292013-01-24 16:10:29 +01001318 data->weight = 0;
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001319
1320 if (event->header.type != PERF_RECORD_SAMPLE) {
Arnaldo Carvalho de Meloa3f698f2012-08-02 12:23:46 -03001321 if (!evsel->attr.sample_id_all)
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001322 return 0;
Arnaldo Carvalho de Melo0807d2d2012-09-26 12:48:18 -03001323 return perf_evsel__parse_id_sample(evsel, event, data);
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001324 }
1325
1326 array = event->sample.array;
1327
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001328 /*
1329 * The evsel's sample_size is based on PERF_SAMPLE_MASK which includes
1330 * up to PERF_SAMPLE_PERIOD. After that overflow() must be used to
1331 * check the format does not go past the end of the event.
1332 */
Arnaldo Carvalho de Meloa3f698f2012-08-02 12:23:46 -03001333 if (evsel->sample_size + sizeof(event->header) > event->header.size)
Frederic Weisbeckera2854122011-05-21 19:33:04 +02001334 return -EFAULT;
1335
Adrian Hunter75562572013-08-27 11:23:09 +03001336 data->id = -1ULL;
1337 if (type & PERF_SAMPLE_IDENTIFIER) {
1338 data->id = *array;
1339 array++;
1340 }
1341
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001342 if (type & PERF_SAMPLE_IP) {
Adrian Hunteref893252013-08-27 11:23:06 +03001343 data->ip = *array;
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001344 array++;
1345 }
1346
1347 if (type & PERF_SAMPLE_TID) {
David Ahern936be502011-09-06 09:12:26 -06001348 u.val64 = *array;
1349 if (swapped) {
1350 /* undo swap of u64, then swap on individual u32s */
1351 u.val64 = bswap_64(u.val64);
1352 u.val32[0] = bswap_32(u.val32[0]);
1353 u.val32[1] = bswap_32(u.val32[1]);
1354 }
1355
1356 data->pid = u.val32[0];
1357 data->tid = u.val32[1];
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001358 array++;
1359 }
1360
1361 if (type & PERF_SAMPLE_TIME) {
1362 data->time = *array;
1363 array++;
1364 }
1365
David Ahern7cec0922011-05-30 13:08:23 -06001366 data->addr = 0;
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001367 if (type & PERF_SAMPLE_ADDR) {
1368 data->addr = *array;
1369 array++;
1370 }
1371
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001372 if (type & PERF_SAMPLE_ID) {
1373 data->id = *array;
1374 array++;
1375 }
1376
1377 if (type & PERF_SAMPLE_STREAM_ID) {
1378 data->stream_id = *array;
1379 array++;
1380 }
1381
1382 if (type & PERF_SAMPLE_CPU) {
David Ahern936be502011-09-06 09:12:26 -06001383
1384 u.val64 = *array;
1385 if (swapped) {
1386 /* undo swap of u64, then swap on individual u32s */
1387 u.val64 = bswap_64(u.val64);
1388 u.val32[0] = bswap_32(u.val32[0]);
1389 }
1390
1391 data->cpu = u.val32[0];
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001392 array++;
1393 }
1394
1395 if (type & PERF_SAMPLE_PERIOD) {
1396 data->period = *array;
1397 array++;
1398 }
1399
1400 if (type & PERF_SAMPLE_READ) {
Jiri Olsa9ede4732012-10-10 17:38:13 +02001401 u64 read_format = evsel->attr.read_format;
1402
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001403 OVERFLOW_CHECK_u64(array);
Jiri Olsa9ede4732012-10-10 17:38:13 +02001404 if (read_format & PERF_FORMAT_GROUP)
1405 data->read.group.nr = *array;
1406 else
1407 data->read.one.value = *array;
1408
1409 array++;
1410
1411 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) {
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001412 OVERFLOW_CHECK_u64(array);
Jiri Olsa9ede4732012-10-10 17:38:13 +02001413 data->read.time_enabled = *array;
1414 array++;
1415 }
1416
1417 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) {
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001418 OVERFLOW_CHECK_u64(array);
Jiri Olsa9ede4732012-10-10 17:38:13 +02001419 data->read.time_running = *array;
1420 array++;
1421 }
1422
1423 /* PERF_FORMAT_ID is forced for PERF_SAMPLE_READ */
1424 if (read_format & PERF_FORMAT_GROUP) {
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001425 const u64 max_group_nr = UINT64_MAX /
1426 sizeof(struct sample_read_value);
1427
1428 if (data->read.group.nr > max_group_nr)
1429 return -EFAULT;
1430 sz = data->read.group.nr *
1431 sizeof(struct sample_read_value);
1432 OVERFLOW_CHECK(array, sz, max_size);
1433 data->read.group.values =
1434 (struct sample_read_value *)array;
1435 array = (void *)array + sz;
Jiri Olsa9ede4732012-10-10 17:38:13 +02001436 } else {
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001437 OVERFLOW_CHECK_u64(array);
Jiri Olsa9ede4732012-10-10 17:38:13 +02001438 data->read.one.id = *array;
1439 array++;
1440 }
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001441 }
1442
1443 if (type & PERF_SAMPLE_CALLCHAIN) {
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001444 const u64 max_callchain_nr = UINT64_MAX / sizeof(u64);
1445
1446 OVERFLOW_CHECK_u64(array);
1447 data->callchain = (struct ip_callchain *)array++;
1448 if (data->callchain->nr > max_callchain_nr)
Frederic Weisbecker98e1da92011-05-21 20:08:15 +02001449 return -EFAULT;
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001450 sz = data->callchain->nr * sizeof(u64);
1451 OVERFLOW_CHECK(array, sz, max_size);
1452 array = (void *)array + sz;
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001453 }
1454
1455 if (type & PERF_SAMPLE_RAW) {
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001456 OVERFLOW_CHECK_u64(array);
David Ahern936be502011-09-06 09:12:26 -06001457 u.val64 = *array;
1458 if (WARN_ONCE(swapped,
1459 "Endianness of raw data not corrected!\n")) {
1460 /* undo swap of u64, then swap on individual u32s */
1461 u.val64 = bswap_64(u.val64);
1462 u.val32[0] = bswap_32(u.val32[0]);
1463 u.val32[1] = bswap_32(u.val32[1]);
1464 }
David Ahern936be502011-09-06 09:12:26 -06001465 data->raw_size = u.val32[0];
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001466 array = (void *)array + sizeof(u32);
Frederic Weisbecker98e1da92011-05-21 20:08:15 +02001467
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001468 OVERFLOW_CHECK(array, data->raw_size, max_size);
1469 data->raw_data = (void *)array;
1470 array = (void *)array + data->raw_size;
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001471 }
1472
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +01001473 if (type & PERF_SAMPLE_BRANCH_STACK) {
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001474 const u64 max_branch_nr = UINT64_MAX /
1475 sizeof(struct branch_entry);
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +01001476
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001477 OVERFLOW_CHECK_u64(array);
1478 data->branch_stack = (struct branch_stack *)array++;
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +01001479
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001480 if (data->branch_stack->nr > max_branch_nr)
1481 return -EFAULT;
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +01001482 sz = data->branch_stack->nr * sizeof(struct branch_entry);
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001483 OVERFLOW_CHECK(array, sz, max_size);
1484 array = (void *)array + sz;
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +01001485 }
Jiri Olsa0f6a3012012-08-07 15:20:45 +02001486
1487 if (type & PERF_SAMPLE_REGS_USER) {
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001488 OVERFLOW_CHECK_u64(array);
Adrian Hunter5b95a4a32013-08-27 11:23:10 +03001489 data->user_regs.abi = *array;
1490 array++;
Jiri Olsa0f6a3012012-08-07 15:20:45 +02001491
Adrian Hunter5b95a4a32013-08-27 11:23:10 +03001492 if (data->user_regs.abi) {
Jiri Olsa352ea452014-01-07 13:47:25 +01001493 u64 mask = evsel->attr.sample_regs_user;
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001494
Jiri Olsa352ea452014-01-07 13:47:25 +01001495 sz = hweight_long(mask) * sizeof(u64);
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001496 OVERFLOW_CHECK(array, sz, max_size);
Jiri Olsa352ea452014-01-07 13:47:25 +01001497 data->user_regs.mask = mask;
Jiri Olsa0f6a3012012-08-07 15:20:45 +02001498 data->user_regs.regs = (u64 *)array;
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001499 array = (void *)array + sz;
Jiri Olsa0f6a3012012-08-07 15:20:45 +02001500 }
1501 }
1502
1503 if (type & PERF_SAMPLE_STACK_USER) {
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001504 OVERFLOW_CHECK_u64(array);
1505 sz = *array++;
Jiri Olsa0f6a3012012-08-07 15:20:45 +02001506
1507 data->user_stack.offset = ((char *)(array - 1)
1508 - (char *) event);
1509
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001510 if (!sz) {
Jiri Olsa0f6a3012012-08-07 15:20:45 +02001511 data->user_stack.size = 0;
1512 } else {
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001513 OVERFLOW_CHECK(array, sz, max_size);
Jiri Olsa0f6a3012012-08-07 15:20:45 +02001514 data->user_stack.data = (char *)array;
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001515 array = (void *)array + sz;
1516 OVERFLOW_CHECK_u64(array);
Adrian Hunter54bd2692013-07-04 16:20:34 +03001517 data->user_stack.size = *array++;
Jiri Olsaa65cb4b2013-10-02 15:46:39 +02001518 if (WARN_ONCE(data->user_stack.size > sz,
1519 "user stack dump failure\n"))
1520 return -EFAULT;
Jiri Olsa0f6a3012012-08-07 15:20:45 +02001521 }
1522 }
1523
Andi Kleen05484292013-01-24 16:10:29 +01001524 data->weight = 0;
1525 if (type & PERF_SAMPLE_WEIGHT) {
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001526 OVERFLOW_CHECK_u64(array);
Andi Kleen05484292013-01-24 16:10:29 +01001527 data->weight = *array;
1528 array++;
1529 }
1530
Stephane Eranian98a3b322013-01-24 16:10:35 +01001531 data->data_src = PERF_MEM_DATA_SRC_NONE;
1532 if (type & PERF_SAMPLE_DATA_SRC) {
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001533 OVERFLOW_CHECK_u64(array);
Stephane Eranian98a3b322013-01-24 16:10:35 +01001534 data->data_src = *array;
1535 array++;
1536 }
1537
Andi Kleen475eeab2013-09-20 07:40:43 -07001538 data->transaction = 0;
1539 if (type & PERF_SAMPLE_TRANSACTION) {
Adrian Hunter87b95522013-11-01 15:51:36 +02001540 OVERFLOW_CHECK_u64(array);
Andi Kleen475eeab2013-09-20 07:40:43 -07001541 data->transaction = *array;
1542 array++;
1543 }
1544
Stephane Eranian6a21c0b2014-09-24 13:48:39 +02001545 data->intr_regs.abi = PERF_SAMPLE_REGS_ABI_NONE;
1546 if (type & PERF_SAMPLE_REGS_INTR) {
1547 OVERFLOW_CHECK_u64(array);
1548 data->intr_regs.abi = *array;
1549 array++;
1550
1551 if (data->intr_regs.abi != PERF_SAMPLE_REGS_ABI_NONE) {
1552 u64 mask = evsel->attr.sample_regs_intr;
1553
1554 sz = hweight_long(mask) * sizeof(u64);
1555 OVERFLOW_CHECK(array, sz, max_size);
1556 data->intr_regs.mask = mask;
1557 data->intr_regs.regs = (u64 *)array;
1558 array = (void *)array + sz;
1559 }
1560 }
1561
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001562 return 0;
1563}
Andrew Vagin74eec262011-11-28 12:03:31 +03001564
Adrian Hunterb1cf6f62013-08-27 11:23:12 +03001565size_t perf_event__sample_event_size(const struct perf_sample *sample, u64 type,
Jiri Olsa352ea452014-01-07 13:47:25 +01001566 u64 read_format)
Adrian Hunterb1cf6f62013-08-27 11:23:12 +03001567{
1568 size_t sz, result = sizeof(struct sample_event);
1569
1570 if (type & PERF_SAMPLE_IDENTIFIER)
1571 result += sizeof(u64);
1572
1573 if (type & PERF_SAMPLE_IP)
1574 result += sizeof(u64);
1575
1576 if (type & PERF_SAMPLE_TID)
1577 result += sizeof(u64);
1578
1579 if (type & PERF_SAMPLE_TIME)
1580 result += sizeof(u64);
1581
1582 if (type & PERF_SAMPLE_ADDR)
1583 result += sizeof(u64);
1584
1585 if (type & PERF_SAMPLE_ID)
1586 result += sizeof(u64);
1587
1588 if (type & PERF_SAMPLE_STREAM_ID)
1589 result += sizeof(u64);
1590
1591 if (type & PERF_SAMPLE_CPU)
1592 result += sizeof(u64);
1593
1594 if (type & PERF_SAMPLE_PERIOD)
1595 result += sizeof(u64);
1596
1597 if (type & PERF_SAMPLE_READ) {
1598 result += sizeof(u64);
1599 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
1600 result += sizeof(u64);
1601 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
1602 result += sizeof(u64);
1603 /* PERF_FORMAT_ID is forced for PERF_SAMPLE_READ */
1604 if (read_format & PERF_FORMAT_GROUP) {
1605 sz = sample->read.group.nr *
1606 sizeof(struct sample_read_value);
1607 result += sz;
1608 } else {
1609 result += sizeof(u64);
1610 }
1611 }
1612
1613 if (type & PERF_SAMPLE_CALLCHAIN) {
1614 sz = (sample->callchain->nr + 1) * sizeof(u64);
1615 result += sz;
1616 }
1617
1618 if (type & PERF_SAMPLE_RAW) {
1619 result += sizeof(u32);
1620 result += sample->raw_size;
1621 }
1622
1623 if (type & PERF_SAMPLE_BRANCH_STACK) {
1624 sz = sample->branch_stack->nr * sizeof(struct branch_entry);
1625 sz += sizeof(u64);
1626 result += sz;
1627 }
1628
1629 if (type & PERF_SAMPLE_REGS_USER) {
1630 if (sample->user_regs.abi) {
1631 result += sizeof(u64);
Jiri Olsa352ea452014-01-07 13:47:25 +01001632 sz = hweight_long(sample->user_regs.mask) * sizeof(u64);
Adrian Hunterb1cf6f62013-08-27 11:23:12 +03001633 result += sz;
1634 } else {
1635 result += sizeof(u64);
1636 }
1637 }
1638
1639 if (type & PERF_SAMPLE_STACK_USER) {
1640 sz = sample->user_stack.size;
1641 result += sizeof(u64);
1642 if (sz) {
1643 result += sz;
1644 result += sizeof(u64);
1645 }
1646 }
1647
1648 if (type & PERF_SAMPLE_WEIGHT)
1649 result += sizeof(u64);
1650
1651 if (type & PERF_SAMPLE_DATA_SRC)
1652 result += sizeof(u64);
1653
Adrian Hunter42d88912013-11-01 15:51:38 +02001654 if (type & PERF_SAMPLE_TRANSACTION)
1655 result += sizeof(u64);
1656
Stephane Eranian6a21c0b2014-09-24 13:48:39 +02001657 if (type & PERF_SAMPLE_REGS_INTR) {
1658 if (sample->intr_regs.abi) {
1659 result += sizeof(u64);
1660 sz = hweight_long(sample->intr_regs.mask) * sizeof(u64);
1661 result += sz;
1662 } else {
1663 result += sizeof(u64);
1664 }
1665 }
1666
Adrian Hunterb1cf6f62013-08-27 11:23:12 +03001667 return result;
1668}
1669
Andrew Vagin74eec262011-11-28 12:03:31 +03001670int perf_event__synthesize_sample(union perf_event *event, u64 type,
Jiri Olsa352ea452014-01-07 13:47:25 +01001671 u64 read_format,
Andrew Vagin74eec262011-11-28 12:03:31 +03001672 const struct perf_sample *sample,
1673 bool swapped)
1674{
1675 u64 *array;
Adrian Hunterd03f2172013-08-27 11:23:11 +03001676 size_t sz;
Andrew Vagin74eec262011-11-28 12:03:31 +03001677 /*
1678 * used for cross-endian analysis. See git commit 65014ab3
1679 * for why this goofiness is needed.
1680 */
Jiri Olsa6a11f922012-05-16 08:59:04 +02001681 union u64_swap u;
Andrew Vagin74eec262011-11-28 12:03:31 +03001682
1683 array = event->sample.array;
1684
Adrian Hunter75562572013-08-27 11:23:09 +03001685 if (type & PERF_SAMPLE_IDENTIFIER) {
1686 *array = sample->id;
1687 array++;
1688 }
1689
Andrew Vagin74eec262011-11-28 12:03:31 +03001690 if (type & PERF_SAMPLE_IP) {
Adrian Hunteref893252013-08-27 11:23:06 +03001691 *array = sample->ip;
Andrew Vagin74eec262011-11-28 12:03:31 +03001692 array++;
1693 }
1694
1695 if (type & PERF_SAMPLE_TID) {
1696 u.val32[0] = sample->pid;
1697 u.val32[1] = sample->tid;
1698 if (swapped) {
1699 /*
Arnaldo Carvalho de Meloa3f698f2012-08-02 12:23:46 -03001700 * Inverse of what is done in perf_evsel__parse_sample
Andrew Vagin74eec262011-11-28 12:03:31 +03001701 */
1702 u.val32[0] = bswap_32(u.val32[0]);
1703 u.val32[1] = bswap_32(u.val32[1]);
1704 u.val64 = bswap_64(u.val64);
1705 }
1706
1707 *array = u.val64;
1708 array++;
1709 }
1710
1711 if (type & PERF_SAMPLE_TIME) {
1712 *array = sample->time;
1713 array++;
1714 }
1715
1716 if (type & PERF_SAMPLE_ADDR) {
1717 *array = sample->addr;
1718 array++;
1719 }
1720
1721 if (type & PERF_SAMPLE_ID) {
1722 *array = sample->id;
1723 array++;
1724 }
1725
1726 if (type & PERF_SAMPLE_STREAM_ID) {
1727 *array = sample->stream_id;
1728 array++;
1729 }
1730
1731 if (type & PERF_SAMPLE_CPU) {
1732 u.val32[0] = sample->cpu;
1733 if (swapped) {
1734 /*
Arnaldo Carvalho de Meloa3f698f2012-08-02 12:23:46 -03001735 * Inverse of what is done in perf_evsel__parse_sample
Andrew Vagin74eec262011-11-28 12:03:31 +03001736 */
1737 u.val32[0] = bswap_32(u.val32[0]);
1738 u.val64 = bswap_64(u.val64);
1739 }
1740 *array = u.val64;
1741 array++;
1742 }
1743
1744 if (type & PERF_SAMPLE_PERIOD) {
1745 *array = sample->period;
1746 array++;
1747 }
1748
Adrian Hunterd03f2172013-08-27 11:23:11 +03001749 if (type & PERF_SAMPLE_READ) {
1750 if (read_format & PERF_FORMAT_GROUP)
1751 *array = sample->read.group.nr;
1752 else
1753 *array = sample->read.one.value;
1754 array++;
1755
1756 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) {
1757 *array = sample->read.time_enabled;
1758 array++;
1759 }
1760
1761 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) {
1762 *array = sample->read.time_running;
1763 array++;
1764 }
1765
1766 /* PERF_FORMAT_ID is forced for PERF_SAMPLE_READ */
1767 if (read_format & PERF_FORMAT_GROUP) {
1768 sz = sample->read.group.nr *
1769 sizeof(struct sample_read_value);
1770 memcpy(array, sample->read.group.values, sz);
1771 array = (void *)array + sz;
1772 } else {
1773 *array = sample->read.one.id;
1774 array++;
1775 }
1776 }
1777
1778 if (type & PERF_SAMPLE_CALLCHAIN) {
1779 sz = (sample->callchain->nr + 1) * sizeof(u64);
1780 memcpy(array, sample->callchain, sz);
1781 array = (void *)array + sz;
1782 }
1783
1784 if (type & PERF_SAMPLE_RAW) {
1785 u.val32[0] = sample->raw_size;
1786 if (WARN_ONCE(swapped,
1787 "Endianness of raw data not corrected!\n")) {
1788 /*
1789 * Inverse of what is done in perf_evsel__parse_sample
1790 */
1791 u.val32[0] = bswap_32(u.val32[0]);
1792 u.val32[1] = bswap_32(u.val32[1]);
1793 u.val64 = bswap_64(u.val64);
1794 }
1795 *array = u.val64;
1796 array = (void *)array + sizeof(u32);
1797
1798 memcpy(array, sample->raw_data, sample->raw_size);
1799 array = (void *)array + sample->raw_size;
1800 }
1801
1802 if (type & PERF_SAMPLE_BRANCH_STACK) {
1803 sz = sample->branch_stack->nr * sizeof(struct branch_entry);
1804 sz += sizeof(u64);
1805 memcpy(array, sample->branch_stack, sz);
1806 array = (void *)array + sz;
1807 }
1808
1809 if (type & PERF_SAMPLE_REGS_USER) {
1810 if (sample->user_regs.abi) {
1811 *array++ = sample->user_regs.abi;
Jiri Olsa352ea452014-01-07 13:47:25 +01001812 sz = hweight_long(sample->user_regs.mask) * sizeof(u64);
Adrian Hunterd03f2172013-08-27 11:23:11 +03001813 memcpy(array, sample->user_regs.regs, sz);
1814 array = (void *)array + sz;
1815 } else {
1816 *array++ = 0;
1817 }
1818 }
1819
1820 if (type & PERF_SAMPLE_STACK_USER) {
1821 sz = sample->user_stack.size;
1822 *array++ = sz;
1823 if (sz) {
1824 memcpy(array, sample->user_stack.data, sz);
1825 array = (void *)array + sz;
1826 *array++ = sz;
1827 }
1828 }
1829
1830 if (type & PERF_SAMPLE_WEIGHT) {
1831 *array = sample->weight;
1832 array++;
1833 }
1834
1835 if (type & PERF_SAMPLE_DATA_SRC) {
1836 *array = sample->data_src;
1837 array++;
1838 }
1839
Adrian Hunter42d88912013-11-01 15:51:38 +02001840 if (type & PERF_SAMPLE_TRANSACTION) {
1841 *array = sample->transaction;
1842 array++;
1843 }
1844
Stephane Eranian6a21c0b2014-09-24 13:48:39 +02001845 if (type & PERF_SAMPLE_REGS_INTR) {
1846 if (sample->intr_regs.abi) {
1847 *array++ = sample->intr_regs.abi;
1848 sz = hweight_long(sample->intr_regs.mask) * sizeof(u64);
1849 memcpy(array, sample->intr_regs.regs, sz);
1850 array = (void *)array + sz;
1851 } else {
1852 *array++ = 0;
1853 }
1854 }
1855
Andrew Vagin74eec262011-11-28 12:03:31 +03001856 return 0;
1857}
Arnaldo Carvalho de Melo5555ded2012-09-11 19:24:23 -03001858
Arnaldo Carvalho de Meloefd2b922012-09-18 11:21:50 -03001859struct format_field *perf_evsel__field(struct perf_evsel *evsel, const char *name)
1860{
1861 return pevent_find_field(evsel->tp_format, name);
1862}
1863
Arnaldo Carvalho de Melo5d2074e2012-09-26 20:22:00 -03001864void *perf_evsel__rawptr(struct perf_evsel *evsel, struct perf_sample *sample,
Arnaldo Carvalho de Melo5555ded2012-09-11 19:24:23 -03001865 const char *name)
1866{
Arnaldo Carvalho de Meloefd2b922012-09-18 11:21:50 -03001867 struct format_field *field = perf_evsel__field(evsel, name);
Arnaldo Carvalho de Melo5555ded2012-09-11 19:24:23 -03001868 int offset;
1869
Arnaldo Carvalho de Meloefd2b922012-09-18 11:21:50 -03001870 if (!field)
1871 return NULL;
Arnaldo Carvalho de Melo5555ded2012-09-11 19:24:23 -03001872
1873 offset = field->offset;
1874
1875 if (field->flags & FIELD_IS_DYNAMIC) {
1876 offset = *(int *)(sample->raw_data + field->offset);
1877 offset &= 0xffff;
1878 }
1879
1880 return sample->raw_data + offset;
1881}
1882
1883u64 perf_evsel__intval(struct perf_evsel *evsel, struct perf_sample *sample,
1884 const char *name)
1885{
Arnaldo Carvalho de Meloefd2b922012-09-18 11:21:50 -03001886 struct format_field *field = perf_evsel__field(evsel, name);
Arnaldo Carvalho de Meloe6b6f672012-09-26 13:13:04 -03001887 void *ptr;
1888 u64 value;
Arnaldo Carvalho de Melo5555ded2012-09-11 19:24:23 -03001889
Arnaldo Carvalho de Meloefd2b922012-09-18 11:21:50 -03001890 if (!field)
1891 return 0;
Arnaldo Carvalho de Melo5555ded2012-09-11 19:24:23 -03001892
Arnaldo Carvalho de Meloe6b6f672012-09-26 13:13:04 -03001893 ptr = sample->raw_data + field->offset;
Arnaldo Carvalho de Melo5555ded2012-09-11 19:24:23 -03001894
Arnaldo Carvalho de Meloe6b6f672012-09-26 13:13:04 -03001895 switch (field->size) {
1896 case 1:
1897 return *(u8 *)ptr;
1898 case 2:
1899 value = *(u16 *)ptr;
1900 break;
1901 case 4:
1902 value = *(u32 *)ptr;
1903 break;
1904 case 8:
1905 value = *(u64 *)ptr;
1906 break;
1907 default:
1908 return 0;
1909 }
1910
1911 if (!evsel->needs_swap)
1912 return value;
1913
1914 switch (field->size) {
1915 case 2:
1916 return bswap_16(value);
1917 case 4:
1918 return bswap_32(value);
1919 case 8:
1920 return bswap_64(value);
1921 default:
1922 return 0;
1923 }
1924
1925 return 0;
Arnaldo Carvalho de Melo5555ded2012-09-11 19:24:23 -03001926}
Arnaldo Carvalho de Melo0698aed2012-12-10 18:17:08 -03001927
1928static int comma_fprintf(FILE *fp, bool *first, const char *fmt, ...)
1929{
1930 va_list args;
1931 int ret = 0;
1932
1933 if (!*first) {
1934 ret += fprintf(fp, ",");
1935 } else {
1936 ret += fprintf(fp, ":");
1937 *first = false;
1938 }
1939
1940 va_start(args, fmt);
1941 ret += vfprintf(fp, fmt, args);
1942 va_end(args);
1943 return ret;
1944}
1945
1946static int __if_fprintf(FILE *fp, bool *first, const char *field, u64 value)
1947{
1948 if (value == 0)
1949 return 0;
1950
1951 return comma_fprintf(fp, first, " %s: %" PRIu64, field, value);
1952}
1953
1954#define if_print(field) printed += __if_fprintf(fp, &first, #field, evsel->attr.field)
1955
Arnaldo Carvalho de Meloc79a4392012-12-11 10:54:12 -03001956struct bit_names {
1957 int bit;
1958 const char *name;
1959};
1960
1961static int bits__fprintf(FILE *fp, const char *field, u64 value,
1962 struct bit_names *bits, bool *first)
1963{
1964 int i = 0, printed = comma_fprintf(fp, first, " %s: ", field);
1965 bool first_bit = true;
1966
1967 do {
1968 if (value & bits[i].bit) {
1969 printed += fprintf(fp, "%s%s", first_bit ? "" : "|", bits[i].name);
1970 first_bit = false;
1971 }
1972 } while (bits[++i].name != NULL);
1973
1974 return printed;
1975}
1976
1977static int sample_type__fprintf(FILE *fp, bool *first, u64 value)
1978{
1979#define bit_name(n) { PERF_SAMPLE_##n, #n }
1980 struct bit_names bits[] = {
1981 bit_name(IP), bit_name(TID), bit_name(TIME), bit_name(ADDR),
1982 bit_name(READ), bit_name(CALLCHAIN), bit_name(ID), bit_name(CPU),
1983 bit_name(PERIOD), bit_name(STREAM_ID), bit_name(RAW),
1984 bit_name(BRANCH_STACK), bit_name(REGS_USER), bit_name(STACK_USER),
Stephane Eranian6a21c0b2014-09-24 13:48:39 +02001985 bit_name(IDENTIFIER), bit_name(REGS_INTR),
Arnaldo Carvalho de Meloc79a4392012-12-11 10:54:12 -03001986 { .name = NULL, }
1987 };
1988#undef bit_name
1989 return bits__fprintf(fp, "sample_type", value, bits, first);
1990}
1991
1992static int read_format__fprintf(FILE *fp, bool *first, u64 value)
1993{
1994#define bit_name(n) { PERF_FORMAT_##n, #n }
1995 struct bit_names bits[] = {
1996 bit_name(TOTAL_TIME_ENABLED), bit_name(TOTAL_TIME_RUNNING),
1997 bit_name(ID), bit_name(GROUP),
1998 { .name = NULL, }
1999 };
2000#undef bit_name
2001 return bits__fprintf(fp, "read_format", value, bits, first);
2002}
2003
Arnaldo Carvalho de Melo0698aed2012-12-10 18:17:08 -03002004int perf_evsel__fprintf(struct perf_evsel *evsel,
2005 struct perf_attr_details *details, FILE *fp)
2006{
2007 bool first = true;
Namhyung Kime6ab07d2013-01-22 18:09:47 +09002008 int printed = 0;
2009
Arnaldo Carvalho de Meloe35ef3552013-02-06 17:20:02 -03002010 if (details->event_group) {
Namhyung Kime6ab07d2013-01-22 18:09:47 +09002011 struct perf_evsel *pos;
2012
2013 if (!perf_evsel__is_group_leader(evsel))
2014 return 0;
2015
2016 if (evsel->nr_members > 1)
2017 printed += fprintf(fp, "%s{", evsel->group_name ?: "");
2018
2019 printed += fprintf(fp, "%s", perf_evsel__name(evsel));
2020 for_each_group_member(pos, evsel)
2021 printed += fprintf(fp, ",%s", perf_evsel__name(pos));
2022
2023 if (evsel->nr_members > 1)
2024 printed += fprintf(fp, "}");
2025 goto out;
2026 }
2027
2028 printed += fprintf(fp, "%s", perf_evsel__name(evsel));
Arnaldo Carvalho de Melo0698aed2012-12-10 18:17:08 -03002029
2030 if (details->verbose || details->freq) {
2031 printed += comma_fprintf(fp, &first, " sample_freq=%" PRIu64,
2032 (u64)evsel->attr.sample_freq);
2033 }
2034
2035 if (details->verbose) {
2036 if_print(type);
2037 if_print(config);
2038 if_print(config1);
2039 if_print(config2);
2040 if_print(size);
Arnaldo Carvalho de Meloc79a4392012-12-11 10:54:12 -03002041 printed += sample_type__fprintf(fp, &first, evsel->attr.sample_type);
2042 if (evsel->attr.read_format)
2043 printed += read_format__fprintf(fp, &first, evsel->attr.read_format);
Arnaldo Carvalho de Melo0698aed2012-12-10 18:17:08 -03002044 if_print(disabled);
2045 if_print(inherit);
2046 if_print(pinned);
2047 if_print(exclusive);
2048 if_print(exclude_user);
2049 if_print(exclude_kernel);
2050 if_print(exclude_hv);
2051 if_print(exclude_idle);
2052 if_print(mmap);
Stephane Eranian5c5e8542013-08-21 12:10:25 +02002053 if_print(mmap2);
Arnaldo Carvalho de Melo0698aed2012-12-10 18:17:08 -03002054 if_print(comm);
Adrian Hunter022c50d2014-07-14 13:02:27 +03002055 if_print(comm_exec);
Arnaldo Carvalho de Melo0698aed2012-12-10 18:17:08 -03002056 if_print(freq);
2057 if_print(inherit_stat);
2058 if_print(enable_on_exec);
2059 if_print(task);
2060 if_print(watermark);
2061 if_print(precise_ip);
2062 if_print(mmap_data);
2063 if_print(sample_id_all);
2064 if_print(exclude_host);
2065 if_print(exclude_guest);
2066 if_print(__reserved_1);
2067 if_print(wakeup_events);
2068 if_print(bp_type);
2069 if_print(branch_sample_type);
2070 }
Namhyung Kime6ab07d2013-01-22 18:09:47 +09002071out:
Arnaldo Carvalho de Melo0698aed2012-12-10 18:17:08 -03002072 fputc('\n', fp);
2073 return ++printed;
2074}
Arnaldo Carvalho de Meloc0a54342012-12-13 14:16:30 -03002075
2076bool perf_evsel__fallback(struct perf_evsel *evsel, int err,
2077 char *msg, size_t msgsize)
2078{
David Ahern2b821cc2013-07-18 17:27:59 -06002079 if ((err == ENOENT || err == ENXIO || err == ENODEV) &&
Arnaldo Carvalho de Meloc0a54342012-12-13 14:16:30 -03002080 evsel->attr.type == PERF_TYPE_HARDWARE &&
2081 evsel->attr.config == PERF_COUNT_HW_CPU_CYCLES) {
2082 /*
2083 * If it's cycles then fall back to hrtimer based
2084 * cpu-clock-tick sw counter, which is always available even if
2085 * no PMU support.
2086 *
2087 * PPC returns ENXIO until 2.6.37 (behavior changed with commit
2088 * b0a873e).
2089 */
2090 scnprintf(msg, msgsize, "%s",
2091"The cycles event is not supported, trying to fall back to cpu-clock-ticks");
2092
2093 evsel->attr.type = PERF_TYPE_SOFTWARE;
2094 evsel->attr.config = PERF_COUNT_SW_CPU_CLOCK;
2095
Arnaldo Carvalho de Melo04662522013-12-26 17:41:15 -03002096 zfree(&evsel->name);
Arnaldo Carvalho de Meloc0a54342012-12-13 14:16:30 -03002097 return true;
2098 }
2099
2100 return false;
2101}
Arnaldo Carvalho de Melo56e52e82012-12-13 15:10:58 -03002102
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03002103int perf_evsel__open_strerror(struct perf_evsel *evsel, struct target *target,
Arnaldo Carvalho de Melo56e52e82012-12-13 15:10:58 -03002104 int err, char *msg, size_t size)
2105{
Masami Hiramatsu6e81c742014-08-14 02:22:36 +00002106 char sbuf[STRERR_BUFSIZE];
2107
Arnaldo Carvalho de Melo56e52e82012-12-13 15:10:58 -03002108 switch (err) {
2109 case EPERM:
2110 case EACCES:
David Ahernb69e63a2013-05-25 17:54:00 -06002111 return scnprintf(msg, size,
Arnaldo Carvalho de Melo56e52e82012-12-13 15:10:58 -03002112 "You may not have permission to collect %sstats.\n"
2113 "Consider tweaking /proc/sys/kernel/perf_event_paranoid:\n"
2114 " -1 - Not paranoid at all\n"
2115 " 0 - Disallow raw tracepoint access for unpriv\n"
2116 " 1 - Disallow cpu events for unpriv\n"
2117 " 2 - Disallow kernel profiling for unpriv",
2118 target->system_wide ? "system-wide " : "");
2119 case ENOENT:
2120 return scnprintf(msg, size, "The %s event is not supported.",
2121 perf_evsel__name(evsel));
2122 case EMFILE:
2123 return scnprintf(msg, size, "%s",
2124 "Too many events are opened.\n"
2125 "Try again after reducing the number of events.");
2126 case ENODEV:
2127 if (target->cpu_list)
2128 return scnprintf(msg, size, "%s",
2129 "No such device - did you specify an out-of-range profile CPU?\n");
2130 break;
2131 case EOPNOTSUPP:
2132 if (evsel->attr.precise_ip)
2133 return scnprintf(msg, size, "%s",
2134 "\'precise\' request may not be supported. Try removing 'p' modifier.");
2135#if defined(__i386__) || defined(__x86_64__)
2136 if (evsel->attr.type == PERF_TYPE_HARDWARE)
2137 return scnprintf(msg, size, "%s",
2138 "No hardware sampling interrupt available.\n"
2139 "No APIC? If so then you can boot the kernel with the \"lapic\" boot parameter to force-enable it.");
2140#endif
2141 break;
Jiri Olsa63914ac2014-08-01 17:46:54 +02002142 case EBUSY:
2143 if (find_process("oprofiled"))
2144 return scnprintf(msg, size,
2145 "The PMU counters are busy/taken by another profiler.\n"
2146 "We found oprofile daemon running, please stop it and try again.");
2147 break;
Arnaldo Carvalho de Melo56e52e82012-12-13 15:10:58 -03002148 default:
2149 break;
2150 }
2151
2152 return scnprintf(msg, size,
Masami Hiramatsu6e81c742014-08-14 02:22:36 +00002153 "The sys_perf_event_open() syscall returned with %d (%s) for event (%s).\n"
Arnaldo Carvalho de Melo56e52e82012-12-13 15:10:58 -03002154 "/bin/dmesg may provide additional information.\n"
2155 "No CONFIG_PERF_EVENTS=y kernel support configured?\n",
Masami Hiramatsu6e81c742014-08-14 02:22:36 +00002156 err, strerror_r(err, sbuf, sizeof(sbuf)),
2157 perf_evsel__name(evsel));
Arnaldo Carvalho de Melo56e52e82012-12-13 15:10:58 -03002158}