blob: 5d7037ef7d3b7d43f0b38822e4de50131ab6b4d2 [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>
Jiri Olsa4605eab2015-09-02 09:56:43 +020012#include <api/fs/tracing_path.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>
Jiri Olsa8dd2a132015-09-07 10:38:06 +020016#include <linux/err.h>
Andi Kleenbec19672013-08-04 19:41:26 -070017#include <sys/resource.h>
Robert Richter4e319022013-06-11 17:29:18 +020018#include "asm/bug.h"
Arnaldo Carvalho de Melo8f651ea2014-10-09 16:12:24 -030019#include "callchain.h"
Arnaldo Carvalho de Melof14d5702014-10-17 12:17:40 -030020#include "cgroup.h"
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -020021#include "evsel.h"
Arnaldo Carvalho de Melo70082dd2011-01-12 17:03:24 -020022#include "evlist.h"
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -020023#include "util.h"
Arnaldo Carvalho de Melo86bd5e82011-01-03 23:09:46 -020024#include "cpumap.h"
Arnaldo Carvalho de Melofd782602011-01-18 15:15:24 -020025#include "thread_map.h"
Namhyung Kim12864b32012-04-26 14:15:22 +090026#include "target.h"
Jiri Olsa26d33022012-08-07 15:20:47 +020027#include "perf_regs.h"
Adrian Huntere3e1a542013-08-14 15:48:24 +030028#include "debug.h"
Jiri Olsa97978b32013-12-03 14:09:24 +010029#include "trace-event.h"
Jiri Olsaa9a3a4d2015-06-14 10:19:26 +020030#include "stat.h"
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -020031
Arnaldo Carvalho de Melo594ac612012-12-13 13:13:07 -030032static struct {
33 bool sample_id_all;
34 bool exclude_guest;
Stephane Eranian5c5e8542013-08-21 12:10:25 +020035 bool mmap2;
Yann Droneaud57480d22014-06-30 22:28:47 +020036 bool cloexec;
Peter Zijlstra814c8c32015-03-31 00:19:31 +020037 bool clockid;
38 bool clockid_wrong;
Andi Kleenbd0f8892015-12-11 16:12:24 -080039 bool lbr_flags;
Wang Nanb90dc172016-05-20 16:38:23 +000040 bool write_backward;
Arnaldo Carvalho de Melo594ac612012-12-13 13:13:07 -030041} perf_missing_features;
42
Peter Zijlstra814c8c32015-03-31 00:19:31 +020043static clockid_t clockid;
44
Arnaldo Carvalho de Meloce8ccff2014-10-09 15:29:51 -030045static int perf_evsel__no_extra_init(struct perf_evsel *evsel __maybe_unused)
46{
47 return 0;
48}
49
50static void perf_evsel__no_extra_fini(struct perf_evsel *evsel __maybe_unused)
51{
52}
53
54static struct {
55 size_t size;
56 int (*init)(struct perf_evsel *evsel);
57 void (*fini)(struct perf_evsel *evsel);
58} perf_evsel__object = {
59 .size = sizeof(struct perf_evsel),
60 .init = perf_evsel__no_extra_init,
61 .fini = perf_evsel__no_extra_fini,
62};
63
64int perf_evsel__object_config(size_t object_size,
65 int (*init)(struct perf_evsel *evsel),
66 void (*fini)(struct perf_evsel *evsel))
67{
68
69 if (object_size == 0)
70 goto set_methods;
71
72 if (perf_evsel__object.size > object_size)
73 return -EINVAL;
74
75 perf_evsel__object.size = object_size;
76
77set_methods:
78 if (init != NULL)
79 perf_evsel__object.init = init;
80
81 if (fini != NULL)
82 perf_evsel__object.fini = fini;
83
84 return 0;
85}
86
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -020087#define FD(e, x, y) (*(int *)xyarray__entry(e->fd, x, y))
88
Adrian Hunter75562572013-08-27 11:23:09 +030089int __perf_evsel__sample_size(u64 sample_type)
Arnaldo Carvalho de Meloc2a70652011-06-02 11:04:54 -030090{
91 u64 mask = sample_type & PERF_SAMPLE_MASK;
92 int size = 0;
93 int i;
94
95 for (i = 0; i < 64; i++) {
96 if (mask & (1ULL << i))
97 size++;
98 }
99
100 size *= sizeof(u64);
101
102 return size;
103}
104
Adrian Hunter75562572013-08-27 11:23:09 +0300105/**
106 * __perf_evsel__calc_id_pos - calculate id_pos.
107 * @sample_type: sample type
108 *
109 * This function returns the position of the event id (PERF_SAMPLE_ID or
110 * PERF_SAMPLE_IDENTIFIER) in a sample event i.e. in the array of struct
111 * sample_event.
112 */
113static int __perf_evsel__calc_id_pos(u64 sample_type)
114{
115 int idx = 0;
116
117 if (sample_type & PERF_SAMPLE_IDENTIFIER)
118 return 0;
119
120 if (!(sample_type & PERF_SAMPLE_ID))
121 return -1;
122
123 if (sample_type & PERF_SAMPLE_IP)
124 idx += 1;
125
126 if (sample_type & PERF_SAMPLE_TID)
127 idx += 1;
128
129 if (sample_type & PERF_SAMPLE_TIME)
130 idx += 1;
131
132 if (sample_type & PERF_SAMPLE_ADDR)
133 idx += 1;
134
135 return idx;
136}
137
138/**
139 * __perf_evsel__calc_is_pos - calculate is_pos.
140 * @sample_type: sample type
141 *
142 * This function returns the position (counting backwards) of the event id
143 * (PERF_SAMPLE_ID or PERF_SAMPLE_IDENTIFIER) in a non-sample event i.e. if
144 * sample_id_all is used there is an id sample appended to non-sample events.
145 */
146static int __perf_evsel__calc_is_pos(u64 sample_type)
147{
148 int idx = 1;
149
150 if (sample_type & PERF_SAMPLE_IDENTIFIER)
151 return 1;
152
153 if (!(sample_type & PERF_SAMPLE_ID))
154 return -1;
155
156 if (sample_type & PERF_SAMPLE_CPU)
157 idx += 1;
158
159 if (sample_type & PERF_SAMPLE_STREAM_ID)
160 idx += 1;
161
162 return idx;
163}
164
165void perf_evsel__calc_id_pos(struct perf_evsel *evsel)
166{
167 evsel->id_pos = __perf_evsel__calc_id_pos(evsel->attr.sample_type);
168 evsel->is_pos = __perf_evsel__calc_is_pos(evsel->attr.sample_type);
169}
170
Arnaldo Carvalho de Melo7be5ebe2012-12-10 14:53:43 -0300171void __perf_evsel__set_sample_bit(struct perf_evsel *evsel,
172 enum perf_event_sample_format bit)
173{
174 if (!(evsel->attr.sample_type & bit)) {
175 evsel->attr.sample_type |= bit;
176 evsel->sample_size += sizeof(u64);
Adrian Hunter75562572013-08-27 11:23:09 +0300177 perf_evsel__calc_id_pos(evsel);
Arnaldo Carvalho de Melo7be5ebe2012-12-10 14:53:43 -0300178 }
179}
180
181void __perf_evsel__reset_sample_bit(struct perf_evsel *evsel,
182 enum perf_event_sample_format bit)
183{
184 if (evsel->attr.sample_type & bit) {
185 evsel->attr.sample_type &= ~bit;
186 evsel->sample_size -= sizeof(u64);
Adrian Hunter75562572013-08-27 11:23:09 +0300187 perf_evsel__calc_id_pos(evsel);
Arnaldo Carvalho de Melo7be5ebe2012-12-10 14:53:43 -0300188 }
189}
190
Adrian Hunter75562572013-08-27 11:23:09 +0300191void perf_evsel__set_sample_id(struct perf_evsel *evsel,
192 bool can_sample_identifier)
Arnaldo Carvalho de Melo7a5a5ca2012-12-10 15:21:30 -0300193{
Adrian Hunter75562572013-08-27 11:23:09 +0300194 if (can_sample_identifier) {
195 perf_evsel__reset_sample_bit(evsel, ID);
196 perf_evsel__set_sample_bit(evsel, IDENTIFIER);
197 } else {
198 perf_evsel__set_sample_bit(evsel, ID);
199 }
Arnaldo Carvalho de Melo7a5a5ca2012-12-10 15:21:30 -0300200 evsel->attr.read_format |= PERF_FORMAT_ID;
201}
202
Arnaldo Carvalho de Meloef1d1af2011-01-18 21:41:45 -0200203void perf_evsel__init(struct perf_evsel *evsel,
204 struct perf_event_attr *attr, int idx)
205{
206 evsel->idx = idx;
Adrian Hunter60b08962014-07-31 09:00:52 +0300207 evsel->tracking = !idx;
Arnaldo Carvalho de Meloef1d1af2011-01-18 21:41:45 -0200208 evsel->attr = *attr;
Namhyung Kim2cfda562012-11-29 15:38:29 +0900209 evsel->leader = evsel;
Stephane Eranian410136f2013-11-12 17:58:49 +0100210 evsel->unit = "";
211 evsel->scale = 1.0;
Arnaldo Carvalho de Melod49e4692015-08-27 08:07:40 -0400212 evsel->evlist = NULL;
Wang Nan1f45b1d2015-10-14 12:41:18 +0000213 evsel->bpf_fd = -1;
Arnaldo Carvalho de Meloef1d1af2011-01-18 21:41:45 -0200214 INIT_LIST_HEAD(&evsel->node);
Jiri Olsa930a2e22015-07-29 05:42:10 -0400215 INIT_LIST_HEAD(&evsel->config_terms);
Arnaldo Carvalho de Meloce8ccff2014-10-09 15:29:51 -0300216 perf_evsel__object.init(evsel);
Arnaldo Carvalho de Melobde09462012-08-01 18:53:11 -0300217 evsel->sample_size = __perf_evsel__sample_size(attr->sample_type);
Adrian Hunter75562572013-08-27 11:23:09 +0300218 perf_evsel__calc_id_pos(evsel);
Wang Nan15bfd2c2015-07-10 07:36:09 +0000219 evsel->cmdline_group_boundary = false;
Arnaldo Carvalho de Meloef1d1af2011-01-18 21:41:45 -0200220}
221
Arnaldo Carvalho de Meloef503832013-11-07 16:41:19 -0300222struct perf_evsel *perf_evsel__new_idx(struct perf_event_attr *attr, int idx)
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200223{
Arnaldo Carvalho de Meloce8ccff2014-10-09 15:29:51 -0300224 struct perf_evsel *evsel = zalloc(perf_evsel__object.size);
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200225
Arnaldo Carvalho de Meloef1d1af2011-01-18 21:41:45 -0200226 if (evsel != NULL)
227 perf_evsel__init(evsel, attr, idx);
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200228
Wang Nan03e0a7d2016-02-22 09:10:37 +0000229 if (perf_evsel__is_bpf_output(evsel)) {
Wang Nand37ba882016-04-01 13:26:42 +0000230 evsel->attr.sample_type |= (PERF_SAMPLE_RAW | PERF_SAMPLE_TIME |
231 PERF_SAMPLE_CPU | PERF_SAMPLE_PERIOD),
Wang Nan03e0a7d2016-02-22 09:10:37 +0000232 evsel->attr.sample_period = 1;
233 }
234
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200235 return evsel;
236}
237
Jiri Olsa8dd2a132015-09-07 10:38:06 +0200238/*
239 * Returns pointer with encoded error via <linux/err.h> interface.
240 */
Arnaldo Carvalho de Meloef503832013-11-07 16:41:19 -0300241struct perf_evsel *perf_evsel__newtp_idx(const char *sys, const char *name, int idx)
Arnaldo Carvalho de Meloefd2b922012-09-18 11:21:50 -0300242{
Arnaldo Carvalho de Meloce8ccff2014-10-09 15:29:51 -0300243 struct perf_evsel *evsel = zalloc(perf_evsel__object.size);
Jiri Olsa8dd2a132015-09-07 10:38:06 +0200244 int err = -ENOMEM;
Arnaldo Carvalho de Meloefd2b922012-09-18 11:21:50 -0300245
Jiri Olsa8dd2a132015-09-07 10:38:06 +0200246 if (evsel == NULL) {
247 goto out_err;
248 } else {
Arnaldo Carvalho de Meloefd2b922012-09-18 11:21:50 -0300249 struct perf_event_attr attr = {
Arnaldo Carvalho de Melo0b80f8b32012-09-26 12:28:26 -0300250 .type = PERF_TYPE_TRACEPOINT,
251 .sample_type = (PERF_SAMPLE_RAW | PERF_SAMPLE_TIME |
252 PERF_SAMPLE_CPU | PERF_SAMPLE_PERIOD),
Arnaldo Carvalho de Meloefd2b922012-09-18 11:21:50 -0300253 };
254
Arnaldo Carvalho de Meloe48ffe22012-09-26 17:11:38 -0300255 if (asprintf(&evsel->name, "%s:%s", sys, name) < 0)
256 goto out_free;
257
Jiri Olsa97978b32013-12-03 14:09:24 +0100258 evsel->tp_format = trace_event__tp_format(sys, name);
Jiri Olsa8dd2a132015-09-07 10:38:06 +0200259 if (IS_ERR(evsel->tp_format)) {
260 err = PTR_ERR(evsel->tp_format);
Arnaldo Carvalho de Meloefd2b922012-09-18 11:21:50 -0300261 goto out_free;
Jiri Olsa8dd2a132015-09-07 10:38:06 +0200262 }
Arnaldo Carvalho de Meloefd2b922012-09-18 11:21:50 -0300263
Arnaldo Carvalho de Melo0b80f8b32012-09-26 12:28:26 -0300264 event_attr_init(&attr);
Arnaldo Carvalho de Meloefd2b922012-09-18 11:21:50 -0300265 attr.config = evsel->tp_format->id;
Arnaldo Carvalho de Melo0b80f8b32012-09-26 12:28:26 -0300266 attr.sample_period = 1;
Arnaldo Carvalho de Meloefd2b922012-09-18 11:21:50 -0300267 perf_evsel__init(evsel, &attr, idx);
Arnaldo Carvalho de Meloefd2b922012-09-18 11:21:50 -0300268 }
269
270 return evsel;
271
272out_free:
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -0300273 zfree(&evsel->name);
Arnaldo Carvalho de Meloefd2b922012-09-18 11:21:50 -0300274 free(evsel);
Jiri Olsa8dd2a132015-09-07 10:38:06 +0200275out_err:
276 return ERR_PTR(err);
Arnaldo Carvalho de Meloefd2b922012-09-18 11:21:50 -0300277}
278
Arnaldo Carvalho de Melo8ad70132012-09-06 13:11:18 -0300279const char *perf_evsel__hw_names[PERF_COUNT_HW_MAX] = {
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300280 "cycles",
281 "instructions",
282 "cache-references",
283 "cache-misses",
284 "branches",
285 "branch-misses",
286 "bus-cycles",
287 "stalled-cycles-frontend",
288 "stalled-cycles-backend",
289 "ref-cycles",
290};
291
Arnaldo Carvalho de Melodd4f5222012-06-13 15:52:42 -0300292static const char *__perf_evsel__hw_name(u64 config)
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300293{
294 if (config < PERF_COUNT_HW_MAX && perf_evsel__hw_names[config])
295 return perf_evsel__hw_names[config];
296
297 return "unknown-hardware";
298}
299
Arnaldo Carvalho de Melo27f18612012-06-11 13:33:09 -0300300static int perf_evsel__add_modifiers(struct perf_evsel *evsel, char *bf, size_t size)
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300301{
Arnaldo Carvalho de Melo27f18612012-06-11 13:33:09 -0300302 int colon = 0, r = 0;
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300303 struct perf_event_attr *attr = &evsel->attr;
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300304 bool exclude_guest_default = false;
305
306#define MOD_PRINT(context, mod) do { \
307 if (!attr->exclude_##context) { \
Arnaldo Carvalho de Melo27f18612012-06-11 13:33:09 -0300308 if (!colon) colon = ++r; \
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300309 r += scnprintf(bf + r, size - r, "%c", mod); \
310 } } while(0)
311
312 if (attr->exclude_kernel || attr->exclude_user || attr->exclude_hv) {
313 MOD_PRINT(kernel, 'k');
314 MOD_PRINT(user, 'u');
315 MOD_PRINT(hv, 'h');
316 exclude_guest_default = true;
317 }
318
319 if (attr->precise_ip) {
320 if (!colon)
Arnaldo Carvalho de Melo27f18612012-06-11 13:33:09 -0300321 colon = ++r;
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300322 r += scnprintf(bf + r, size - r, "%.*s", attr->precise_ip, "ppp");
323 exclude_guest_default = true;
324 }
325
326 if (attr->exclude_host || attr->exclude_guest == exclude_guest_default) {
327 MOD_PRINT(host, 'H');
328 MOD_PRINT(guest, 'G');
329 }
330#undef MOD_PRINT
331 if (colon)
Arnaldo Carvalho de Melo27f18612012-06-11 13:33:09 -0300332 bf[colon - 1] = ':';
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300333 return r;
334}
335
Arnaldo Carvalho de Melo27f18612012-06-11 13:33:09 -0300336static int perf_evsel__hw_name(struct perf_evsel *evsel, char *bf, size_t size)
337{
338 int r = scnprintf(bf, size, "%s", __perf_evsel__hw_name(evsel->attr.config));
339 return r + perf_evsel__add_modifiers(evsel, bf + r, size - r);
340}
341
Arnaldo Carvalho de Melo8ad70132012-09-06 13:11:18 -0300342const char *perf_evsel__sw_names[PERF_COUNT_SW_MAX] = {
Arnaldo Carvalho de Melo335c2f52012-06-11 14:36:20 -0300343 "cpu-clock",
344 "task-clock",
345 "page-faults",
346 "context-switches",
Arnaldo Carvalho de Melo8ad70132012-09-06 13:11:18 -0300347 "cpu-migrations",
Arnaldo Carvalho de Melo335c2f52012-06-11 14:36:20 -0300348 "minor-faults",
349 "major-faults",
350 "alignment-faults",
351 "emulation-faults",
Adrian Hunterd22d1a22013-08-31 21:50:52 +0300352 "dummy",
Arnaldo Carvalho de Melo335c2f52012-06-11 14:36:20 -0300353};
354
Arnaldo Carvalho de Melodd4f5222012-06-13 15:52:42 -0300355static const char *__perf_evsel__sw_name(u64 config)
Arnaldo Carvalho de Melo335c2f52012-06-11 14:36:20 -0300356{
357 if (config < PERF_COUNT_SW_MAX && perf_evsel__sw_names[config])
358 return perf_evsel__sw_names[config];
359 return "unknown-software";
360}
361
362static int perf_evsel__sw_name(struct perf_evsel *evsel, char *bf, size_t size)
363{
364 int r = scnprintf(bf, size, "%s", __perf_evsel__sw_name(evsel->attr.config));
365 return r + perf_evsel__add_modifiers(evsel, bf + r, size - r);
366}
367
Jiri Olsa287e74a2012-06-28 23:18:49 +0200368static int __perf_evsel__bp_name(char *bf, size_t size, u64 addr, u64 type)
369{
370 int r;
371
372 r = scnprintf(bf, size, "mem:0x%" PRIx64 ":", addr);
373
374 if (type & HW_BREAKPOINT_R)
375 r += scnprintf(bf + r, size - r, "r");
376
377 if (type & HW_BREAKPOINT_W)
378 r += scnprintf(bf + r, size - r, "w");
379
380 if (type & HW_BREAKPOINT_X)
381 r += scnprintf(bf + r, size - r, "x");
382
383 return r;
384}
385
386static int perf_evsel__bp_name(struct perf_evsel *evsel, char *bf, size_t size)
387{
388 struct perf_event_attr *attr = &evsel->attr;
389 int r = __perf_evsel__bp_name(bf, size, attr->bp_addr, attr->bp_type);
390 return r + perf_evsel__add_modifiers(evsel, bf + r, size - r);
391}
392
Arnaldo Carvalho de Melo0b668bc2012-06-11 14:08:07 -0300393const char *perf_evsel__hw_cache[PERF_COUNT_HW_CACHE_MAX]
394 [PERF_EVSEL__MAX_ALIASES] = {
395 { "L1-dcache", "l1-d", "l1d", "L1-data", },
396 { "L1-icache", "l1-i", "l1i", "L1-instruction", },
397 { "LLC", "L2", },
398 { "dTLB", "d-tlb", "Data-TLB", },
399 { "iTLB", "i-tlb", "Instruction-TLB", },
400 { "branch", "branches", "bpu", "btb", "bpc", },
401 { "node", },
402};
403
404const char *perf_evsel__hw_cache_op[PERF_COUNT_HW_CACHE_OP_MAX]
405 [PERF_EVSEL__MAX_ALIASES] = {
406 { "load", "loads", "read", },
407 { "store", "stores", "write", },
408 { "prefetch", "prefetches", "speculative-read", "speculative-load", },
409};
410
411const char *perf_evsel__hw_cache_result[PERF_COUNT_HW_CACHE_RESULT_MAX]
412 [PERF_EVSEL__MAX_ALIASES] = {
413 { "refs", "Reference", "ops", "access", },
414 { "misses", "miss", },
415};
416
417#define C(x) PERF_COUNT_HW_CACHE_##x
418#define CACHE_READ (1 << C(OP_READ))
419#define CACHE_WRITE (1 << C(OP_WRITE))
420#define CACHE_PREFETCH (1 << C(OP_PREFETCH))
421#define COP(x) (1 << x)
422
423/*
424 * cache operartion stat
425 * L1I : Read and prefetch only
426 * ITLB and BPU : Read-only
427 */
428static unsigned long perf_evsel__hw_cache_stat[C(MAX)] = {
429 [C(L1D)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
430 [C(L1I)] = (CACHE_READ | CACHE_PREFETCH),
431 [C(LL)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
432 [C(DTLB)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
433 [C(ITLB)] = (CACHE_READ),
434 [C(BPU)] = (CACHE_READ),
435 [C(NODE)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
436};
437
438bool perf_evsel__is_cache_op_valid(u8 type, u8 op)
439{
440 if (perf_evsel__hw_cache_stat[type] & COP(op))
441 return true; /* valid */
442 else
443 return false; /* invalid */
444}
445
446int __perf_evsel__hw_cache_type_op_res_name(u8 type, u8 op, u8 result,
447 char *bf, size_t size)
448{
449 if (result) {
450 return scnprintf(bf, size, "%s-%s-%s", perf_evsel__hw_cache[type][0],
451 perf_evsel__hw_cache_op[op][0],
452 perf_evsel__hw_cache_result[result][0]);
453 }
454
455 return scnprintf(bf, size, "%s-%s", perf_evsel__hw_cache[type][0],
456 perf_evsel__hw_cache_op[op][1]);
457}
458
Arnaldo Carvalho de Melodd4f5222012-06-13 15:52:42 -0300459static int __perf_evsel__hw_cache_name(u64 config, char *bf, size_t size)
Arnaldo Carvalho de Melo0b668bc2012-06-11 14:08:07 -0300460{
461 u8 op, result, type = (config >> 0) & 0xff;
462 const char *err = "unknown-ext-hardware-cache-type";
463
464 if (type > PERF_COUNT_HW_CACHE_MAX)
465 goto out_err;
466
467 op = (config >> 8) & 0xff;
468 err = "unknown-ext-hardware-cache-op";
469 if (op > PERF_COUNT_HW_CACHE_OP_MAX)
470 goto out_err;
471
472 result = (config >> 16) & 0xff;
473 err = "unknown-ext-hardware-cache-result";
474 if (result > PERF_COUNT_HW_CACHE_RESULT_MAX)
475 goto out_err;
476
477 err = "invalid-cache";
478 if (!perf_evsel__is_cache_op_valid(type, op))
479 goto out_err;
480
481 return __perf_evsel__hw_cache_type_op_res_name(type, op, result, bf, size);
482out_err:
483 return scnprintf(bf, size, "%s", err);
484}
485
486static int perf_evsel__hw_cache_name(struct perf_evsel *evsel, char *bf, size_t size)
487{
488 int ret = __perf_evsel__hw_cache_name(evsel->attr.config, bf, size);
489 return ret + perf_evsel__add_modifiers(evsel, bf + ret, size - ret);
490}
491
Arnaldo Carvalho de Melo6eef3d92012-06-13 11:53:37 -0300492static int perf_evsel__raw_name(struct perf_evsel *evsel, char *bf, size_t size)
493{
494 int ret = scnprintf(bf, size, "raw 0x%" PRIx64, evsel->attr.config);
495 return ret + perf_evsel__add_modifiers(evsel, bf + ret, size - ret);
496}
497
Arnaldo Carvalho de Melo7289f832012-06-12 12:34:58 -0300498const char *perf_evsel__name(struct perf_evsel *evsel)
Arnaldo Carvalho de Meloa4460832012-06-12 10:29:12 -0300499{
Arnaldo Carvalho de Melo7289f832012-06-12 12:34:58 -0300500 char bf[128];
Arnaldo Carvalho de Meloa4460832012-06-12 10:29:12 -0300501
Arnaldo Carvalho de Melo7289f832012-06-12 12:34:58 -0300502 if (evsel->name)
503 return evsel->name;
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300504
505 switch (evsel->attr.type) {
506 case PERF_TYPE_RAW:
Arnaldo Carvalho de Melo6eef3d92012-06-13 11:53:37 -0300507 perf_evsel__raw_name(evsel, bf, sizeof(bf));
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300508 break;
509
510 case PERF_TYPE_HARDWARE:
Arnaldo Carvalho de Melo7289f832012-06-12 12:34:58 -0300511 perf_evsel__hw_name(evsel, bf, sizeof(bf));
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300512 break;
Arnaldo Carvalho de Melo0b668bc2012-06-11 14:08:07 -0300513
514 case PERF_TYPE_HW_CACHE:
Arnaldo Carvalho de Melo7289f832012-06-12 12:34:58 -0300515 perf_evsel__hw_cache_name(evsel, bf, sizeof(bf));
Arnaldo Carvalho de Melo0b668bc2012-06-11 14:08:07 -0300516 break;
517
Arnaldo Carvalho de Melo335c2f52012-06-11 14:36:20 -0300518 case PERF_TYPE_SOFTWARE:
Arnaldo Carvalho de Melo7289f832012-06-12 12:34:58 -0300519 perf_evsel__sw_name(evsel, bf, sizeof(bf));
Arnaldo Carvalho de Melo335c2f52012-06-11 14:36:20 -0300520 break;
521
Arnaldo Carvalho de Meloa4460832012-06-12 10:29:12 -0300522 case PERF_TYPE_TRACEPOINT:
Arnaldo Carvalho de Melo7289f832012-06-12 12:34:58 -0300523 scnprintf(bf, sizeof(bf), "%s", "unknown tracepoint");
Arnaldo Carvalho de Meloa4460832012-06-12 10:29:12 -0300524 break;
525
Jiri Olsa287e74a2012-06-28 23:18:49 +0200526 case PERF_TYPE_BREAKPOINT:
527 perf_evsel__bp_name(evsel, bf, sizeof(bf));
528 break;
529
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300530 default:
Robert Richterca1b1452012-08-16 21:10:18 +0200531 scnprintf(bf, sizeof(bf), "unknown attr type: %d",
532 evsel->attr.type);
Arnaldo Carvalho de Meloa4460832012-06-12 10:29:12 -0300533 break;
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300534 }
535
Arnaldo Carvalho de Melo7289f832012-06-12 12:34:58 -0300536 evsel->name = strdup(bf);
537
538 return evsel->name ?: "unknown";
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300539}
540
Namhyung Kim717e2632013-01-22 18:09:44 +0900541const char *perf_evsel__group_name(struct perf_evsel *evsel)
542{
543 return evsel->group_name ?: "anon group";
544}
545
546int perf_evsel__group_desc(struct perf_evsel *evsel, char *buf, size_t size)
547{
548 int ret;
549 struct perf_evsel *pos;
550 const char *group_name = perf_evsel__group_name(evsel);
551
552 ret = scnprintf(buf, size, "%s", group_name);
553
554 ret += scnprintf(buf + ret, size - ret, " { %s",
555 perf_evsel__name(evsel));
556
557 for_each_group_member(pos, evsel)
558 ret += scnprintf(buf + ret, size - ret, ", %s",
559 perf_evsel__name(pos));
560
561 ret += scnprintf(buf + ret, size - ret, " }");
562
563 return ret;
564}
565
Arnaldo Carvalho de Melo01e0d502016-04-11 18:39:37 -0300566void perf_evsel__config_callchain(struct perf_evsel *evsel,
567 struct record_opts *opts,
568 struct callchain_param *param)
Jiri Olsa6bedfab2014-03-02 16:56:40 +0100569{
570 bool function = perf_evsel__is_function_event(evsel);
571 struct perf_event_attr *attr = &evsel->attr;
572
573 perf_evsel__set_sample_bit(evsel, CALLCHAIN);
574
Kan Liangc3a6a8c2015-08-04 04:30:20 -0400575 if (param->record_mode == CALLCHAIN_LBR) {
Kan Liangaad2b212015-01-05 13:23:04 -0500576 if (!opts->branch_stack) {
577 if (attr->exclude_user) {
578 pr_warning("LBR callstack option is only available "
579 "to get user callchain information. "
580 "Falling back to framepointers.\n");
581 } else {
582 perf_evsel__set_sample_bit(evsel, BRANCH_STACK);
583 attr->branch_sample_type = PERF_SAMPLE_BRANCH_USER |
Andi Kleenbd0f8892015-12-11 16:12:24 -0800584 PERF_SAMPLE_BRANCH_CALL_STACK |
585 PERF_SAMPLE_BRANCH_NO_CYCLES |
586 PERF_SAMPLE_BRANCH_NO_FLAGS;
Kan Liangaad2b212015-01-05 13:23:04 -0500587 }
588 } else
589 pr_warning("Cannot use LBR callstack with branch stack. "
590 "Falling back to framepointers.\n");
591 }
592
Kan Liangc3a6a8c2015-08-04 04:30:20 -0400593 if (param->record_mode == CALLCHAIN_DWARF) {
Jiri Olsa6bedfab2014-03-02 16:56:40 +0100594 if (!function) {
595 perf_evsel__set_sample_bit(evsel, REGS_USER);
596 perf_evsel__set_sample_bit(evsel, STACK_USER);
597 attr->sample_regs_user = PERF_REGS_MASK;
Kan Liangc3a6a8c2015-08-04 04:30:20 -0400598 attr->sample_stack_user = param->dump_size;
Jiri Olsa6bedfab2014-03-02 16:56:40 +0100599 attr->exclude_callchain_user = 1;
600 } else {
601 pr_info("Cannot use DWARF unwind for function trace event,"
602 " falling back to framepointers.\n");
603 }
604 }
605
606 if (function) {
607 pr_info("Disabling user space callchains for function trace event.\n");
608 attr->exclude_callchain_user = 1;
609 }
610}
611
Kan Liangd457c962015-08-11 06:30:47 -0400612static void
613perf_evsel__reset_callgraph(struct perf_evsel *evsel,
614 struct callchain_param *param)
615{
616 struct perf_event_attr *attr = &evsel->attr;
617
618 perf_evsel__reset_sample_bit(evsel, CALLCHAIN);
619 if (param->record_mode == CALLCHAIN_LBR) {
620 perf_evsel__reset_sample_bit(evsel, BRANCH_STACK);
621 attr->branch_sample_type &= ~(PERF_SAMPLE_BRANCH_USER |
622 PERF_SAMPLE_BRANCH_CALL_STACK);
623 }
624 if (param->record_mode == CALLCHAIN_DWARF) {
625 perf_evsel__reset_sample_bit(evsel, REGS_USER);
626 perf_evsel__reset_sample_bit(evsel, STACK_USER);
627 }
628}
629
630static void apply_config_terms(struct perf_evsel *evsel,
631 struct record_opts *opts)
Jiri Olsa930a2e22015-07-29 05:42:10 -0400632{
633 struct perf_evsel_config_term *term;
Kan Liang32067712015-08-04 04:30:19 -0400634 struct list_head *config_terms = &evsel->config_terms;
635 struct perf_event_attr *attr = &evsel->attr;
Kan Liangd457c962015-08-11 06:30:47 -0400636 struct callchain_param param;
637 u32 dump_size = 0;
638 char *callgraph_buf = NULL;
639
640 /* callgraph default */
641 param.record_mode = callchain_param.record_mode;
Jiri Olsa930a2e22015-07-29 05:42:10 -0400642
643 list_for_each_entry(term, config_terms, list) {
644 switch (term->type) {
Jiri Olsaee4c7582015-07-29 05:42:11 -0400645 case PERF_EVSEL__CONFIG_TERM_PERIOD:
646 attr->sample_period = term->val.period;
Jiri Olsaab35a7d2015-08-08 19:12:10 +0200647 attr->freq = 0;
Kan Liang32067712015-08-04 04:30:19 -0400648 break;
Namhyung Kim09af2a52015-08-09 15:45:23 +0900649 case PERF_EVSEL__CONFIG_TERM_FREQ:
650 attr->sample_freq = term->val.freq;
651 attr->freq = 1;
652 break;
Kan Liang32067712015-08-04 04:30:19 -0400653 case PERF_EVSEL__CONFIG_TERM_TIME:
654 if (term->val.time)
655 perf_evsel__set_sample_bit(evsel, TIME);
656 else
657 perf_evsel__reset_sample_bit(evsel, TIME);
658 break;
Kan Liangd457c962015-08-11 06:30:47 -0400659 case PERF_EVSEL__CONFIG_TERM_CALLGRAPH:
660 callgraph_buf = term->val.callgraph;
661 break;
662 case PERF_EVSEL__CONFIG_TERM_STACK_USER:
663 dump_size = term->val.stack_user;
664 break;
Wang Nan374ce932015-10-28 10:55:02 +0000665 case PERF_EVSEL__CONFIG_TERM_INHERIT:
666 /*
667 * attr->inherit should has already been set by
668 * perf_evsel__config. If user explicitly set
669 * inherit using config terms, override global
670 * opt->no_inherit setting.
671 */
672 attr->inherit = term->val.inherit ? 1 : 0;
673 break;
Jiri Olsa930a2e22015-07-29 05:42:10 -0400674 default:
675 break;
676 }
677 }
Kan Liangd457c962015-08-11 06:30:47 -0400678
679 /* User explicitly set per-event callgraph, clear the old setting and reset. */
680 if ((callgraph_buf != NULL) || (dump_size > 0)) {
681
682 /* parse callgraph parameters */
683 if (callgraph_buf != NULL) {
Kan Liangf9db0d02015-08-11 06:30:48 -0400684 if (!strcmp(callgraph_buf, "no")) {
685 param.enabled = false;
686 param.record_mode = CALLCHAIN_NONE;
687 } else {
688 param.enabled = true;
689 if (parse_callchain_record(callgraph_buf, &param)) {
690 pr_err("per-event callgraph setting for %s failed. "
691 "Apply callgraph global setting for it\n",
692 evsel->name);
693 return;
694 }
Kan Liangd457c962015-08-11 06:30:47 -0400695 }
696 }
697 if (dump_size > 0) {
698 dump_size = round_up(dump_size, sizeof(u64));
699 param.dump_size = dump_size;
700 }
701
702 /* If global callgraph set, clear it */
703 if (callchain_param.enabled)
704 perf_evsel__reset_callgraph(evsel, &callchain_param);
705
706 /* set perf-event callgraph */
707 if (param.enabled)
Arnaldo Carvalho de Melo01e0d502016-04-11 18:39:37 -0300708 perf_evsel__config_callchain(evsel, opts, &param);
Kan Liangd457c962015-08-11 06:30:47 -0400709 }
Jiri Olsa930a2e22015-07-29 05:42:10 -0400710}
711
Jiri Olsa774cb492012-11-12 18:34:01 +0100712/*
713 * The enable_on_exec/disabled value strategy:
714 *
715 * 1) For any type of traced program:
716 * - all independent events and group leaders are disabled
717 * - all group members are enabled
718 *
719 * Group members are ruled by group leaders. They need to
720 * be enabled, because the group scheduling relies on that.
721 *
722 * 2) For traced programs executed by perf:
723 * - all independent events and group leaders have
724 * enable_on_exec set
725 * - we don't specifically enable or disable any event during
726 * the record command
727 *
728 * Independent events and group leaders are initially disabled
729 * and get enabled by exec. Group members are ruled by group
730 * leaders as stated in 1).
731 *
732 * 3) For traced programs attached by perf (pid/tid):
733 * - we specifically enable or disable all events during
734 * the record command
735 *
736 * When attaching events to already running traced we
737 * enable/disable events specifically, as there's no
738 * initial traced exec call.
739 */
Arnaldo Carvalho de Meloe68ae9c2016-04-11 18:15:29 -0300740void perf_evsel__config(struct perf_evsel *evsel, struct record_opts *opts,
741 struct callchain_param *callchain)
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200742{
Jiri Olsa3c176312012-10-10 17:39:03 +0200743 struct perf_evsel *leader = evsel->leader;
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200744 struct perf_event_attr *attr = &evsel->attr;
Adrian Hunter60b08962014-07-31 09:00:52 +0300745 int track = evsel->tracking;
Adrian Hunter3aa59392013-11-15 15:52:29 +0200746 bool per_cpu = opts->target.default_per_cpu && !opts->target.per_thread;
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200747
Arnaldo Carvalho de Melo594ac612012-12-13 13:13:07 -0300748 attr->sample_id_all = perf_missing_features.sample_id_all ? 0 : 1;
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200749 attr->inherit = !opts->no_inherit;
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200750
Arnaldo Carvalho de Melo7be5ebe2012-12-10 14:53:43 -0300751 perf_evsel__set_sample_bit(evsel, IP);
752 perf_evsel__set_sample_bit(evsel, TID);
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200753
Jiri Olsa3c176312012-10-10 17:39:03 +0200754 if (evsel->sample_read) {
755 perf_evsel__set_sample_bit(evsel, READ);
756
757 /*
758 * We need ID even in case of single event, because
759 * PERF_SAMPLE_READ process ID specific data.
760 */
Adrian Hunter75562572013-08-27 11:23:09 +0300761 perf_evsel__set_sample_id(evsel, false);
Jiri Olsa3c176312012-10-10 17:39:03 +0200762
763 /*
764 * Apply group format only if we belong to group
765 * with more than one members.
766 */
767 if (leader->nr_members > 1) {
768 attr->read_format |= PERF_FORMAT_GROUP;
769 attr->inherit = 0;
770 }
771 }
772
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200773 /*
Namhyung Kim17314e22014-06-09 14:43:37 +0900774 * We default some events to have a default interval. But keep
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200775 * it a weak assumption overridable by the user.
776 */
Namhyung Kim17314e22014-06-09 14:43:37 +0900777 if (!attr->sample_period || (opts->user_freq != UINT_MAX ||
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200778 opts->user_interval != ULLONG_MAX)) {
779 if (opts->freq) {
Arnaldo Carvalho de Melo7be5ebe2012-12-10 14:53:43 -0300780 perf_evsel__set_sample_bit(evsel, PERIOD);
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200781 attr->freq = 1;
782 attr->sample_freq = opts->freq;
783 } else {
784 attr->sample_period = opts->default_interval;
785 }
786 }
787
Jiri Olsa3c176312012-10-10 17:39:03 +0200788 /*
789 * Disable sampling for all group members other
790 * than leader in case leader 'leads' the sampling.
791 */
792 if ((leader != evsel) && leader->sample_read) {
793 attr->sample_freq = 0;
794 attr->sample_period = 0;
795 }
796
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200797 if (opts->no_samples)
798 attr->sample_freq = 0;
799
800 if (opts->inherit_stat)
801 attr->inherit_stat = 1;
802
803 if (opts->sample_address) {
Arnaldo Carvalho de Melo7be5ebe2012-12-10 14:53:43 -0300804 perf_evsel__set_sample_bit(evsel, ADDR);
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200805 attr->mmap_data = track;
806 }
807
Jiri Olsaf1403732014-11-13 18:21:03 +0100808 /*
809 * We don't allow user space callchains for function trace
810 * event, due to issues with page faults while tracing page
811 * fault handler and its overall trickiness nature.
812 */
813 if (perf_evsel__is_function_event(evsel))
814 evsel->attr.exclude_callchain_user = 1;
815
Arnaldo Carvalho de Meloe68ae9c2016-04-11 18:15:29 -0300816 if (callchain && callchain->enabled && !evsel->no_aux_samples)
Arnaldo Carvalho de Melo01e0d502016-04-11 18:39:37 -0300817 perf_evsel__config_callchain(evsel, opts, callchain);
Jiri Olsa26d33022012-08-07 15:20:47 +0200818
Stephane Eranian6a21c0b2014-09-24 13:48:39 +0200819 if (opts->sample_intr_regs) {
Stephane Eranianbcc84ec2015-08-31 18:41:12 +0200820 attr->sample_regs_intr = opts->sample_intr_regs;
Stephane Eranian6a21c0b2014-09-24 13:48:39 +0200821 perf_evsel__set_sample_bit(evsel, REGS_INTR);
822 }
823
Adrian Hunter3aa59392013-11-15 15:52:29 +0200824 if (target__has_cpu(&opts->target))
Arnaldo Carvalho de Melo7be5ebe2012-12-10 14:53:43 -0300825 perf_evsel__set_sample_bit(evsel, CPU);
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200826
Andrew Vagin3e76ac72011-12-20 17:32:45 +0300827 if (opts->period)
Arnaldo Carvalho de Melo7be5ebe2012-12-10 14:53:43 -0300828 perf_evsel__set_sample_bit(evsel, PERIOD);
Andrew Vagin3e76ac72011-12-20 17:32:45 +0300829
Andi Kleen8affc2b2014-07-31 14:45:04 +0800830 /*
Adam Buchbinderbd1a0be2016-02-24 10:02:25 -0800831 * When the user explicitly disabled time don't force it here.
Andi Kleen8affc2b2014-07-31 14:45:04 +0800832 */
833 if (opts->sample_time &&
834 (!perf_missing_features.sample_id_all &&
Adrian Hunter3abebc52015-07-06 14:51:01 +0300835 (!opts->no_inherit || target__has_cpu(&opts->target) || per_cpu ||
836 opts->sample_time_set)))
Arnaldo Carvalho de Melo7be5ebe2012-12-10 14:53:43 -0300837 perf_evsel__set_sample_bit(evsel, TIME);
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200838
Adrian Hunter6ff1ce72014-07-14 13:02:56 +0300839 if (opts->raw_samples && !evsel->no_aux_samples) {
Arnaldo Carvalho de Melo7be5ebe2012-12-10 14:53:43 -0300840 perf_evsel__set_sample_bit(evsel, TIME);
841 perf_evsel__set_sample_bit(evsel, RAW);
842 perf_evsel__set_sample_bit(evsel, CPU);
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200843 }
844
Stephane Eranianccf49bf2013-01-24 16:10:37 +0100845 if (opts->sample_address)
Adrian Hunter1e7ed5e2013-11-01 15:51:35 +0200846 perf_evsel__set_sample_bit(evsel, DATA_SRC);
Stephane Eranianccf49bf2013-01-24 16:10:37 +0100847
Arnaldo Carvalho de Melo509051e2014-01-14 17:52:14 -0300848 if (opts->no_buffering) {
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200849 attr->watermark = 0;
850 attr->wakeup_events = 1;
851 }
Adrian Hunter6ff1ce72014-07-14 13:02:56 +0300852 if (opts->branch_stack && !evsel->no_aux_samples) {
Arnaldo Carvalho de Melo7be5ebe2012-12-10 14:53:43 -0300853 perf_evsel__set_sample_bit(evsel, BRANCH_STACK);
Roberto Agostino Vitillobdfebd82012-02-09 23:21:02 +0100854 attr->branch_sample_type = opts->branch_stack;
855 }
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200856
Andi Kleen05484292013-01-24 16:10:29 +0100857 if (opts->sample_weight)
Adrian Hunter1e7ed5e2013-11-01 15:51:35 +0200858 perf_evsel__set_sample_bit(evsel, WEIGHT);
Andi Kleen05484292013-01-24 16:10:29 +0100859
Namhyung Kim62e503b2015-01-29 17:06:46 +0900860 attr->task = track;
Stephane Eranian5c5e8542013-08-21 12:10:25 +0200861 attr->mmap = track;
Don Zickusa5a5ba72014-05-30 10:49:42 -0400862 attr->mmap2 = track && !perf_missing_features.mmap2;
Stephane Eranian5c5e8542013-08-21 12:10:25 +0200863 attr->comm = track;
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200864
Adrian Hunterb757bb02015-07-21 12:44:04 +0300865 if (opts->record_switch_events)
866 attr->context_switch = track;
867
Andi Kleen475eeab2013-09-20 07:40:43 -0700868 if (opts->sample_transaction)
Adrian Hunter1e7ed5e2013-11-01 15:51:35 +0200869 perf_evsel__set_sample_bit(evsel, TRANSACTION);
Andi Kleen475eeab2013-09-20 07:40:43 -0700870
Andi Kleen85c273d2015-02-24 15:13:40 -0800871 if (opts->running_time) {
872 evsel->attr.read_format |=
873 PERF_FORMAT_TOTAL_TIME_ENABLED |
874 PERF_FORMAT_TOTAL_TIME_RUNNING;
875 }
876
Jiri Olsa774cb492012-11-12 18:34:01 +0100877 /*
878 * XXX see the function comment above
879 *
880 * Disabling only independent events or group leaders,
881 * keeping group members enabled.
882 */
Namhyung Kim823254e2012-11-29 15:38:30 +0900883 if (perf_evsel__is_group_leader(evsel))
Jiri Olsa774cb492012-11-12 18:34:01 +0100884 attr->disabled = 1;
885
886 /*
887 * Setting enable_on_exec for independent events and
888 * group leaders for traced executed by perf.
889 */
Andi Kleen6619a532014-01-11 13:38:27 -0800890 if (target__none(&opts->target) && perf_evsel__is_group_leader(evsel) &&
891 !opts->initial_delay)
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200892 attr->enable_on_exec = 1;
Adrian Hunter2afd2bc2014-07-14 13:02:57 +0300893
894 if (evsel->immediate) {
895 attr->disabled = 0;
896 attr->enable_on_exec = 0;
897 }
Peter Zijlstra814c8c32015-03-31 00:19:31 +0200898
899 clockid = opts->clockid;
900 if (opts->use_clockid) {
901 attr->use_clockid = 1;
902 attr->clockid = opts->clockid;
903 }
Jiri Olsa930a2e22015-07-29 05:42:10 -0400904
Jiri Olsa7f94af72015-10-05 20:06:05 +0200905 if (evsel->precise_max)
906 perf_event_attr__set_max_precise_ip(attr);
907
Jiri Olsa85723882016-02-15 09:34:31 +0100908 if (opts->all_user) {
909 attr->exclude_kernel = 1;
910 attr->exclude_user = 0;
911 }
912
913 if (opts->all_kernel) {
914 attr->exclude_kernel = 0;
915 attr->exclude_user = 1;
916 }
917
Jiri Olsa930a2e22015-07-29 05:42:10 -0400918 /*
919 * Apply event specific term settings,
920 * it overloads any global configuration.
921 */
Kan Liangd457c962015-08-11 06:30:47 -0400922 apply_config_terms(evsel, opts);
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200923}
924
Arnaldo Carvalho de Melo88858462014-10-13 13:30:27 -0300925static int perf_evsel__alloc_fd(struct perf_evsel *evsel, int ncpus, int nthreads)
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200926{
David Ahern4af4c952011-05-27 09:58:34 -0600927 int cpu, thread;
Adrian Hunterbf8e8f42014-07-31 09:00:51 +0300928
929 if (evsel->system_wide)
930 nthreads = 1;
931
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200932 evsel->fd = xyarray__new(ncpus, nthreads, sizeof(int));
David Ahern4af4c952011-05-27 09:58:34 -0600933
934 if (evsel->fd) {
935 for (cpu = 0; cpu < ncpus; cpu++) {
936 for (thread = 0; thread < nthreads; thread++) {
937 FD(evsel, cpu, thread) = -1;
938 }
939 }
940 }
941
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200942 return evsel->fd != NULL ? 0 : -ENOMEM;
943}
944
Andi Kleene2407be2013-08-02 17:41:10 -0700945static int perf_evsel__run_ioctl(struct perf_evsel *evsel, int ncpus, int nthreads,
946 int ioc, void *arg)
Arnaldo Carvalho de Melo745cefc2012-09-26 15:07:39 -0300947{
948 int cpu, thread;
949
Adrian Hunterbf8e8f42014-07-31 09:00:51 +0300950 if (evsel->system_wide)
951 nthreads = 1;
952
Arnaldo Carvalho de Melo745cefc2012-09-26 15:07:39 -0300953 for (cpu = 0; cpu < ncpus; cpu++) {
954 for (thread = 0; thread < nthreads; thread++) {
955 int fd = FD(evsel, cpu, thread),
Andi Kleene2407be2013-08-02 17:41:10 -0700956 err = ioctl(fd, ioc, arg);
Arnaldo Carvalho de Melo745cefc2012-09-26 15:07:39 -0300957
958 if (err)
959 return err;
960 }
961 }
962
963 return 0;
964}
965
Arnaldo Carvalho de Melof47805a2015-07-03 15:53:49 -0300966int perf_evsel__apply_filter(struct perf_evsel *evsel, int ncpus, int nthreads,
967 const char *filter)
Andi Kleene2407be2013-08-02 17:41:10 -0700968{
969 return perf_evsel__run_ioctl(evsel, ncpus, nthreads,
970 PERF_EVENT_IOC_SET_FILTER,
971 (void *)filter);
972}
973
Arnaldo Carvalho de Melo12467ae2015-07-03 17:05:50 -0300974int perf_evsel__set_filter(struct perf_evsel *evsel, const char *filter)
975{
976 char *new_filter = strdup(filter);
977
978 if (new_filter != NULL) {
979 free(evsel->filter);
980 evsel->filter = new_filter;
981 return 0;
982 }
983
984 return -1;
985}
986
Arnaldo Carvalho de Melo64ec84f2015-07-04 12:19:13 -0300987int perf_evsel__append_filter(struct perf_evsel *evsel,
988 const char *op, const char *filter)
989{
990 char *new_filter;
991
992 if (evsel->filter == NULL)
993 return perf_evsel__set_filter(evsel, filter);
994
995 if (asprintf(&new_filter,"(%s) %s (%s)", evsel->filter, op, filter) > 0) {
996 free(evsel->filter);
997 evsel->filter = new_filter;
998 return 0;
999 }
1000
1001 return -1;
1002}
1003
Jiri Olsa5cd95fc2015-12-03 10:06:40 +01001004int perf_evsel__enable(struct perf_evsel *evsel)
Andi Kleene2407be2013-08-02 17:41:10 -07001005{
Jiri Olsa5cd95fc2015-12-03 10:06:40 +01001006 int nthreads = thread_map__nr(evsel->threads);
1007 int ncpus = cpu_map__nr(evsel->cpus);
1008
Andi Kleene2407be2013-08-02 17:41:10 -07001009 return perf_evsel__run_ioctl(evsel, ncpus, nthreads,
1010 PERF_EVENT_IOC_ENABLE,
1011 0);
1012}
1013
Jiri Olsae98a4cb2015-12-03 10:06:41 +01001014int perf_evsel__disable(struct perf_evsel *evsel)
1015{
1016 int nthreads = thread_map__nr(evsel->threads);
1017 int ncpus = cpu_map__nr(evsel->cpus);
1018
1019 return perf_evsel__run_ioctl(evsel, ncpus, nthreads,
1020 PERF_EVENT_IOC_DISABLE,
1021 0);
1022}
1023
Arnaldo Carvalho de Melo70db7532011-01-12 22:39:13 -02001024int perf_evsel__alloc_id(struct perf_evsel *evsel, int ncpus, int nthreads)
1025{
Vineet Gupta8d9cbd82015-01-13 19:13:23 +05301026 if (ncpus == 0 || nthreads == 0)
1027 return 0;
1028
Adrian Hunterbf8e8f42014-07-31 09:00:51 +03001029 if (evsel->system_wide)
1030 nthreads = 1;
1031
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03001032 evsel->sample_id = xyarray__new(ncpus, nthreads, sizeof(struct perf_sample_id));
1033 if (evsel->sample_id == NULL)
1034 return -ENOMEM;
1035
1036 evsel->id = zalloc(ncpus * nthreads * sizeof(u64));
1037 if (evsel->id == NULL) {
1038 xyarray__delete(evsel->sample_id);
1039 evsel->sample_id = NULL;
1040 return -ENOMEM;
1041 }
1042
1043 return 0;
Arnaldo Carvalho de Melo70db7532011-01-12 22:39:13 -02001044}
1045
Arnaldo Carvalho de Melo88858462014-10-13 13:30:27 -03001046static void perf_evsel__free_fd(struct perf_evsel *evsel)
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -02001047{
1048 xyarray__delete(evsel->fd);
1049 evsel->fd = NULL;
1050}
1051
Arnaldo Carvalho de Melo88858462014-10-13 13:30:27 -03001052static void perf_evsel__free_id(struct perf_evsel *evsel)
Arnaldo Carvalho de Melo70db7532011-01-12 22:39:13 -02001053{
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03001054 xyarray__delete(evsel->sample_id);
1055 evsel->sample_id = NULL;
Arnaldo Carvalho de Melo04662522013-12-26 17:41:15 -03001056 zfree(&evsel->id);
Arnaldo Carvalho de Melo70db7532011-01-12 22:39:13 -02001057}
1058
Jiri Olsa930a2e22015-07-29 05:42:10 -04001059static void perf_evsel__free_config_terms(struct perf_evsel *evsel)
1060{
1061 struct perf_evsel_config_term *term, *h;
1062
1063 list_for_each_entry_safe(term, h, &evsel->config_terms, list) {
1064 list_del(&term->list);
1065 free(term);
1066 }
1067}
1068
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -02001069void perf_evsel__close_fd(struct perf_evsel *evsel, int ncpus, int nthreads)
1070{
1071 int cpu, thread;
1072
Adrian Hunterbf8e8f42014-07-31 09:00:51 +03001073 if (evsel->system_wide)
1074 nthreads = 1;
1075
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -02001076 for (cpu = 0; cpu < ncpus; cpu++)
1077 for (thread = 0; thread < nthreads; ++thread) {
1078 close(FD(evsel, cpu, thread));
1079 FD(evsel, cpu, thread) = -1;
1080 }
1081}
1082
Arnaldo Carvalho de Meloef1d1af2011-01-18 21:41:45 -02001083void perf_evsel__exit(struct perf_evsel *evsel)
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -02001084{
1085 assert(list_empty(&evsel->node));
Arnaldo Carvalho de Melod49e4692015-08-27 08:07:40 -04001086 assert(evsel->evlist == NULL);
Namhyung Kim736b05a2013-03-15 14:48:49 +09001087 perf_evsel__free_fd(evsel);
1088 perf_evsel__free_id(evsel);
Jiri Olsa930a2e22015-07-29 05:42:10 -04001089 perf_evsel__free_config_terms(evsel);
Arnaldo Carvalho de Melo597e48c2014-10-16 13:25:01 -03001090 close_cgroup(evsel->cgrp);
Jiri Olsaf30a79b2015-06-23 00:36:04 +02001091 cpu_map__put(evsel->cpus);
Adrian Hunterfce4d292015-09-08 10:58:55 +03001092 cpu_map__put(evsel->own_cpus);
Jiri Olsa578e91e2015-06-23 00:36:07 +02001093 thread_map__put(evsel->threads);
Arnaldo Carvalho de Melo597e48c2014-10-16 13:25:01 -03001094 zfree(&evsel->group_name);
Arnaldo Carvalho de Melo597e48c2014-10-16 13:25:01 -03001095 zfree(&evsel->name);
Arnaldo Carvalho de Meloce8ccff2014-10-09 15:29:51 -03001096 perf_evsel__object.fini(evsel);
Arnaldo Carvalho de Meloef1d1af2011-01-18 21:41:45 -02001097}
1098
1099void perf_evsel__delete(struct perf_evsel *evsel)
1100{
1101 perf_evsel__exit(evsel);
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -02001102 free(evsel);
1103}
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -02001104
Jiri Olsaa6fa0032015-06-26 11:29:11 +02001105void perf_evsel__compute_deltas(struct perf_evsel *evsel, int cpu, int thread,
Jiri Olsa857a94a2014-11-21 10:31:05 +01001106 struct perf_counts_values *count)
Stephane Eranianc7a79c42013-01-29 12:47:43 +01001107{
1108 struct perf_counts_values tmp;
1109
1110 if (!evsel->prev_raw_counts)
1111 return;
1112
1113 if (cpu == -1) {
1114 tmp = evsel->prev_raw_counts->aggr;
1115 evsel->prev_raw_counts->aggr = *count;
1116 } else {
Jiri Olsaa6fa0032015-06-26 11:29:11 +02001117 tmp = *perf_counts(evsel->prev_raw_counts, cpu, thread);
1118 *perf_counts(evsel->prev_raw_counts, cpu, thread) = *count;
Stephane Eranianc7a79c42013-01-29 12:47:43 +01001119 }
1120
1121 count->val = count->val - tmp.val;
1122 count->ena = count->ena - tmp.ena;
1123 count->run = count->run - tmp.run;
1124}
1125
Jiri Olsa13112bb2014-11-21 10:31:06 +01001126void perf_counts_values__scale(struct perf_counts_values *count,
1127 bool scale, s8 *pscaled)
1128{
1129 s8 scaled = 0;
1130
1131 if (scale) {
1132 if (count->run == 0) {
1133 scaled = -1;
1134 count->val = 0;
1135 } else if (count->run < count->ena) {
1136 scaled = 1;
1137 count->val = (u64)((double) count->val * count->ena / count->run + 0.5);
1138 }
1139 } else
1140 count->ena = count->run = 0;
1141
1142 if (pscaled)
1143 *pscaled = scaled;
1144}
1145
Jiri Olsaf99f47192015-06-26 11:29:18 +02001146int perf_evsel__read(struct perf_evsel *evsel, int cpu, int thread,
1147 struct perf_counts_values *count)
1148{
1149 memset(count, 0, sizeof(*count));
1150
1151 if (FD(evsel, cpu, thread) < 0)
1152 return -EINVAL;
1153
1154 if (readn(FD(evsel, cpu, thread), count, sizeof(*count)) < 0)
1155 return -errno;
1156
1157 return 0;
1158}
1159
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -02001160int __perf_evsel__read_on_cpu(struct perf_evsel *evsel,
1161 int cpu, int thread, bool scale)
1162{
1163 struct perf_counts_values count;
1164 size_t nv = scale ? 3 : 1;
1165
1166 if (FD(evsel, cpu, thread) < 0)
1167 return -EINVAL;
1168
Jiri Olsaa6fa0032015-06-26 11:29:11 +02001169 if (evsel->counts == NULL && perf_evsel__alloc_counts(evsel, cpu + 1, thread + 1) < 0)
Arnaldo Carvalho de Melo4eed11d2011-01-04 00:13:17 -02001170 return -ENOMEM;
1171
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -02001172 if (readn(FD(evsel, cpu, thread), &count, nv * sizeof(u64)) < 0)
1173 return -errno;
1174
Jiri Olsaa6fa0032015-06-26 11:29:11 +02001175 perf_evsel__compute_deltas(evsel, cpu, thread, &count);
Jiri Olsa13112bb2014-11-21 10:31:06 +01001176 perf_counts_values__scale(&count, scale, NULL);
Jiri Olsaa6fa0032015-06-26 11:29:11 +02001177 *perf_counts(evsel->counts, cpu, thread) = count;
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -02001178 return 0;
1179}
1180
Jiri Olsa6a4bb042012-08-08 12:22:36 +02001181static int get_group_fd(struct perf_evsel *evsel, int cpu, int thread)
1182{
1183 struct perf_evsel *leader = evsel->leader;
1184 int fd;
1185
Namhyung Kim823254e2012-11-29 15:38:30 +09001186 if (perf_evsel__is_group_leader(evsel))
Jiri Olsa6a4bb042012-08-08 12:22:36 +02001187 return -1;
1188
1189 /*
1190 * Leader must be already processed/open,
1191 * if not it's a bug.
1192 */
1193 BUG_ON(!leader->fd);
1194
1195 fd = FD(leader, cpu, thread);
1196 BUG_ON(fd == -1);
1197
1198 return fd;
1199}
1200
Peter Zijlstra2c5e8c52015-04-07 11:09:54 +02001201struct bit_names {
1202 int bit;
1203 const char *name;
1204};
Adrian Huntere3e1a542013-08-14 15:48:24 +03001205
Peter Zijlstra2c5e8c52015-04-07 11:09:54 +02001206static void __p_bits(char *buf, size_t size, u64 value, struct bit_names *bits)
Adrian Huntere3e1a542013-08-14 15:48:24 +03001207{
Peter Zijlstra2c5e8c52015-04-07 11:09:54 +02001208 bool first_bit = true;
1209 int i = 0;
Adrian Huntere3e1a542013-08-14 15:48:24 +03001210
Peter Zijlstra2c5e8c52015-04-07 11:09:54 +02001211 do {
1212 if (value & bits[i].bit) {
1213 buf += scnprintf(buf, size, "%s%s", first_bit ? "" : "|", bits[i].name);
1214 first_bit = false;
1215 }
1216 } while (bits[++i].name != NULL);
1217}
Adrian Huntere3e1a542013-08-14 15:48:24 +03001218
Peter Zijlstra2c5e8c52015-04-07 11:09:54 +02001219static void __p_sample_type(char *buf, size_t size, u64 value)
1220{
1221#define bit_name(n) { PERF_SAMPLE_##n, #n }
1222 struct bit_names bits[] = {
1223 bit_name(IP), bit_name(TID), bit_name(TIME), bit_name(ADDR),
1224 bit_name(READ), bit_name(CALLCHAIN), bit_name(ID), bit_name(CPU),
1225 bit_name(PERIOD), bit_name(STREAM_ID), bit_name(RAW),
1226 bit_name(BRANCH_STACK), bit_name(REGS_USER), bit_name(STACK_USER),
Jiri Olsa84422592015-10-05 20:06:02 +02001227 bit_name(IDENTIFIER), bit_name(REGS_INTR), bit_name(DATA_SRC),
Jiri Olsadcdd1842015-11-25 16:36:55 +01001228 bit_name(WEIGHT),
Peter Zijlstra2c5e8c52015-04-07 11:09:54 +02001229 { .name = NULL, }
1230 };
1231#undef bit_name
1232 __p_bits(buf, size, value, bits);
1233}
Adrian Huntere3e1a542013-08-14 15:48:24 +03001234
Arnaldo Carvalho de Meloa213b922016-04-25 16:45:29 -03001235static void __p_branch_sample_type(char *buf, size_t size, u64 value)
1236{
1237#define bit_name(n) { PERF_SAMPLE_BRANCH_##n, #n }
1238 struct bit_names bits[] = {
1239 bit_name(USER), bit_name(KERNEL), bit_name(HV), bit_name(ANY),
1240 bit_name(ANY_CALL), bit_name(ANY_RETURN), bit_name(IND_CALL),
1241 bit_name(ABORT_TX), bit_name(IN_TX), bit_name(NO_TX),
1242 bit_name(COND), bit_name(CALL_STACK), bit_name(IND_JUMP),
1243 bit_name(CALL), bit_name(NO_FLAGS), bit_name(NO_CYCLES),
1244 { .name = NULL, }
1245 };
1246#undef bit_name
1247 __p_bits(buf, size, value, bits);
1248}
1249
Peter Zijlstra2c5e8c52015-04-07 11:09:54 +02001250static void __p_read_format(char *buf, size_t size, u64 value)
1251{
1252#define bit_name(n) { PERF_FORMAT_##n, #n }
1253 struct bit_names bits[] = {
1254 bit_name(TOTAL_TIME_ENABLED), bit_name(TOTAL_TIME_RUNNING),
1255 bit_name(ID), bit_name(GROUP),
1256 { .name = NULL, }
1257 };
1258#undef bit_name
1259 __p_bits(buf, size, value, bits);
1260}
Peter Zijlstra814c8c32015-03-31 00:19:31 +02001261
Peter Zijlstra2c5e8c52015-04-07 11:09:54 +02001262#define BUF_SIZE 1024
Adrian Huntere3e1a542013-08-14 15:48:24 +03001263
Adrian Hunter7310aed2015-06-11 15:51:04 +03001264#define p_hex(val) snprintf(buf, BUF_SIZE, "%#"PRIx64, (uint64_t)(val))
Peter Zijlstra2c5e8c52015-04-07 11:09:54 +02001265#define p_unsigned(val) snprintf(buf, BUF_SIZE, "%"PRIu64, (uint64_t)(val))
1266#define p_signed(val) snprintf(buf, BUF_SIZE, "%"PRId64, (int64_t)(val))
1267#define p_sample_type(val) __p_sample_type(buf, BUF_SIZE, val)
Arnaldo Carvalho de Meloa213b922016-04-25 16:45:29 -03001268#define p_branch_sample_type(val) __p_branch_sample_type(buf, BUF_SIZE, val)
Peter Zijlstra2c5e8c52015-04-07 11:09:54 +02001269#define p_read_format(val) __p_read_format(buf, BUF_SIZE, val)
Adrian Huntere3e1a542013-08-14 15:48:24 +03001270
Peter Zijlstra2c5e8c52015-04-07 11:09:54 +02001271#define PRINT_ATTRn(_n, _f, _p) \
1272do { \
1273 if (attr->_f) { \
1274 _p(attr->_f); \
1275 ret += attr__fprintf(fp, _n, buf, priv);\
1276 } \
1277} while (0)
1278
1279#define PRINT_ATTRf(_f, _p) PRINT_ATTRn(#_f, _f, _p)
1280
1281int perf_event_attr__fprintf(FILE *fp, struct perf_event_attr *attr,
1282 attr__fprintf_f attr__fprintf, void *priv)
1283{
1284 char buf[BUF_SIZE];
1285 int ret = 0;
1286
1287 PRINT_ATTRf(type, p_unsigned);
1288 PRINT_ATTRf(size, p_unsigned);
1289 PRINT_ATTRf(config, p_hex);
1290 PRINT_ATTRn("{ sample_period, sample_freq }", sample_period, p_unsigned);
1291 PRINT_ATTRf(sample_type, p_sample_type);
1292 PRINT_ATTRf(read_format, p_read_format);
1293
1294 PRINT_ATTRf(disabled, p_unsigned);
1295 PRINT_ATTRf(inherit, p_unsigned);
1296 PRINT_ATTRf(pinned, p_unsigned);
1297 PRINT_ATTRf(exclusive, p_unsigned);
1298 PRINT_ATTRf(exclude_user, p_unsigned);
1299 PRINT_ATTRf(exclude_kernel, p_unsigned);
1300 PRINT_ATTRf(exclude_hv, p_unsigned);
1301 PRINT_ATTRf(exclude_idle, p_unsigned);
1302 PRINT_ATTRf(mmap, p_unsigned);
1303 PRINT_ATTRf(comm, p_unsigned);
1304 PRINT_ATTRf(freq, p_unsigned);
1305 PRINT_ATTRf(inherit_stat, p_unsigned);
1306 PRINT_ATTRf(enable_on_exec, p_unsigned);
1307 PRINT_ATTRf(task, p_unsigned);
1308 PRINT_ATTRf(watermark, p_unsigned);
1309 PRINT_ATTRf(precise_ip, p_unsigned);
1310 PRINT_ATTRf(mmap_data, p_unsigned);
1311 PRINT_ATTRf(sample_id_all, p_unsigned);
1312 PRINT_ATTRf(exclude_host, p_unsigned);
1313 PRINT_ATTRf(exclude_guest, p_unsigned);
1314 PRINT_ATTRf(exclude_callchain_kernel, p_unsigned);
1315 PRINT_ATTRf(exclude_callchain_user, p_unsigned);
1316 PRINT_ATTRf(mmap2, p_unsigned);
1317 PRINT_ATTRf(comm_exec, p_unsigned);
1318 PRINT_ATTRf(use_clockid, p_unsigned);
Adrian Hunter02860392015-07-21 12:44:03 +03001319 PRINT_ATTRf(context_switch, p_unsigned);
Arnaldo Carvalho de Melo0a241ef2016-05-09 18:08:33 -03001320 PRINT_ATTRf(write_backward, p_unsigned);
Peter Zijlstra2c5e8c52015-04-07 11:09:54 +02001321
1322 PRINT_ATTRn("{ wakeup_events, wakeup_watermark }", wakeup_events, p_unsigned);
1323 PRINT_ATTRf(bp_type, p_unsigned);
1324 PRINT_ATTRn("{ bp_addr, config1 }", bp_addr, p_hex);
1325 PRINT_ATTRn("{ bp_len, config2 }", bp_len, p_hex);
Arnaldo Carvalho de Meloa213b922016-04-25 16:45:29 -03001326 PRINT_ATTRf(branch_sample_type, p_branch_sample_type);
Peter Zijlstra2c5e8c52015-04-07 11:09:54 +02001327 PRINT_ATTRf(sample_regs_user, p_hex);
1328 PRINT_ATTRf(sample_stack_user, p_unsigned);
1329 PRINT_ATTRf(clockid, p_signed);
1330 PRINT_ATTRf(sample_regs_intr, p_hex);
Adrian Hunter70d73de2015-04-09 18:54:06 +03001331 PRINT_ATTRf(aux_watermark, p_unsigned);
Adrian Huntere3e1a542013-08-14 15:48:24 +03001332
1333 return ret;
1334}
1335
Peter Zijlstra2c5e8c52015-04-07 11:09:54 +02001336static int __open_attr__fprintf(FILE *fp, const char *name, const char *val,
1337 void *priv __attribute__((unused)))
1338{
1339 return fprintf(fp, " %-32s %s\n", name, val);
1340}
1341
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -02001342static int __perf_evsel__open(struct perf_evsel *evsel, struct cpu_map *cpus,
Jiri Olsa6a4bb042012-08-08 12:22:36 +02001343 struct thread_map *threads)
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -02001344{
Adrian Hunterbf8e8f42014-07-31 09:00:51 +03001345 int cpu, thread, nthreads;
Yann Droneaud57480d22014-06-30 22:28:47 +02001346 unsigned long flags = PERF_FLAG_FD_CLOEXEC;
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -02001347 int pid = -1, err;
Andi Kleenbec19672013-08-04 19:41:26 -07001348 enum { NO_CHANGE, SET_TO_MAX, INCREASED_MAX } set_rlimit = NO_CHANGE;
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -02001349
Adrian Hunterbf8e8f42014-07-31 09:00:51 +03001350 if (evsel->system_wide)
1351 nthreads = 1;
1352 else
1353 nthreads = threads->nr;
1354
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -02001355 if (evsel->fd == NULL &&
Adrian Hunterbf8e8f42014-07-31 09:00:51 +03001356 perf_evsel__alloc_fd(evsel, cpus->nr, nthreads) < 0)
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -02001357 return -ENOMEM;
Arnaldo Carvalho de Melo4eed11d2011-01-04 00:13:17 -02001358
Stephane Eranian023695d2011-02-14 11:20:01 +02001359 if (evsel->cgrp) {
Yann Droneaud57480d22014-06-30 22:28:47 +02001360 flags |= PERF_FLAG_PID_CGROUP;
Stephane Eranian023695d2011-02-14 11:20:01 +02001361 pid = evsel->cgrp->fd;
1362 }
1363
Arnaldo Carvalho de Melo594ac612012-12-13 13:13:07 -03001364fallback_missing_features:
Peter Zijlstra814c8c32015-03-31 00:19:31 +02001365 if (perf_missing_features.clockid_wrong)
1366 evsel->attr.clockid = CLOCK_MONOTONIC; /* should always work */
1367 if (perf_missing_features.clockid) {
1368 evsel->attr.use_clockid = 0;
1369 evsel->attr.clockid = 0;
1370 }
Yann Droneaud57480d22014-06-30 22:28:47 +02001371 if (perf_missing_features.cloexec)
1372 flags &= ~(unsigned long)PERF_FLAG_FD_CLOEXEC;
Stephane Eranian5c5e8542013-08-21 12:10:25 +02001373 if (perf_missing_features.mmap2)
1374 evsel->attr.mmap2 = 0;
Arnaldo Carvalho de Melo594ac612012-12-13 13:13:07 -03001375 if (perf_missing_features.exclude_guest)
1376 evsel->attr.exclude_guest = evsel->attr.exclude_host = 0;
Andi Kleenbd0f8892015-12-11 16:12:24 -08001377 if (perf_missing_features.lbr_flags)
1378 evsel->attr.branch_sample_type &= ~(PERF_SAMPLE_BRANCH_NO_FLAGS |
1379 PERF_SAMPLE_BRANCH_NO_CYCLES);
Wang Nanb90dc172016-05-20 16:38:23 +00001380 if (perf_missing_features.write_backward)
1381 evsel->attr.write_backward = false;
Arnaldo Carvalho de Melo594ac612012-12-13 13:13:07 -03001382retry_sample_id:
1383 if (perf_missing_features.sample_id_all)
1384 evsel->attr.sample_id_all = 0;
1385
Peter Zijlstra2c5e8c52015-04-07 11:09:54 +02001386 if (verbose >= 2) {
1387 fprintf(stderr, "%.60s\n", graph_dotted_line);
1388 fprintf(stderr, "perf_event_attr:\n");
1389 perf_event_attr__fprintf(stderr, &evsel->attr, __open_attr__fprintf, NULL);
1390 fprintf(stderr, "%.60s\n", graph_dotted_line);
1391 }
Adrian Huntere3e1a542013-08-14 15:48:24 +03001392
Arnaldo Carvalho de Melo86bd5e82011-01-03 23:09:46 -02001393 for (cpu = 0; cpu < cpus->nr; cpu++) {
Arnaldo Carvalho de Melo9d04f172011-01-12 00:08:18 -02001394
Adrian Hunterbf8e8f42014-07-31 09:00:51 +03001395 for (thread = 0; thread < nthreads; thread++) {
Jiri Olsa6a4bb042012-08-08 12:22:36 +02001396 int group_fd;
Stephane Eranian023695d2011-02-14 11:20:01 +02001397
Adrian Hunterbf8e8f42014-07-31 09:00:51 +03001398 if (!evsel->cgrp && !evsel->system_wide)
Jiri Olsae13798c2015-06-23 00:36:02 +02001399 pid = thread_map__pid(threads, thread);
Stephane Eranian023695d2011-02-14 11:20:01 +02001400
Jiri Olsa6a4bb042012-08-08 12:22:36 +02001401 group_fd = get_group_fd(evsel, cpu, thread);
Andi Kleenbec19672013-08-04 19:41:26 -07001402retry_open:
Ramkumar Ramachandraa33f6ef2014-03-18 15:10:42 -04001403 pr_debug2("sys_perf_event_open: pid %d cpu %d group_fd %d flags %#lx\n",
Adrian Huntere3e1a542013-08-14 15:48:24 +03001404 pid, cpus->map[cpu], group_fd, flags);
1405
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -02001406 FD(evsel, cpu, thread) = sys_perf_event_open(&evsel->attr,
Stephane Eranian023695d2011-02-14 11:20:01 +02001407 pid,
Arnaldo Carvalho de Melof08199d2011-01-11 23:42:19 -02001408 cpus->map[cpu],
Stephane Eranian023695d2011-02-14 11:20:01 +02001409 group_fd, flags);
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -02001410 if (FD(evsel, cpu, thread) < 0) {
1411 err = -errno;
Ramkumar Ramachandraa33f6ef2014-03-18 15:10:42 -04001412 pr_debug2("sys_perf_event_open failed, error %d\n",
Adrian Hunterf852fd62013-11-01 15:51:29 +02001413 err);
Arnaldo Carvalho de Melo594ac612012-12-13 13:13:07 -03001414 goto try_fallback;
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -02001415 }
Wang Nan1f45b1d2015-10-14 12:41:18 +00001416
1417 if (evsel->bpf_fd >= 0) {
1418 int evt_fd = FD(evsel, cpu, thread);
1419 int bpf_fd = evsel->bpf_fd;
1420
1421 err = ioctl(evt_fd,
1422 PERF_EVENT_IOC_SET_BPF,
1423 bpf_fd);
1424 if (err && errno != EEXIST) {
1425 pr_err("failed to attach bpf fd %d: %s\n",
1426 bpf_fd, strerror(errno));
1427 err = -EINVAL;
1428 goto out_close;
1429 }
1430 }
1431
Andi Kleenbec19672013-08-04 19:41:26 -07001432 set_rlimit = NO_CHANGE;
Peter Zijlstra814c8c32015-03-31 00:19:31 +02001433
1434 /*
1435 * If we succeeded but had to kill clockid, fail and
1436 * have perf_evsel__open_strerror() print us a nice
1437 * error.
1438 */
1439 if (perf_missing_features.clockid ||
1440 perf_missing_features.clockid_wrong) {
1441 err = -EINVAL;
1442 goto out_close;
1443 }
Wang Nanb90dc172016-05-20 16:38:23 +00001444
1445 if (evsel->overwrite &&
1446 perf_missing_features.write_backward) {
1447 err = -EINVAL;
1448 goto out_close;
1449 }
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -02001450 }
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -02001451 }
1452
1453 return 0;
1454
Arnaldo Carvalho de Melo594ac612012-12-13 13:13:07 -03001455try_fallback:
Andi Kleenbec19672013-08-04 19:41:26 -07001456 /*
1457 * perf stat needs between 5 and 22 fds per CPU. When we run out
1458 * of them try to increase the limits.
1459 */
1460 if (err == -EMFILE && set_rlimit < INCREASED_MAX) {
1461 struct rlimit l;
1462 int old_errno = errno;
1463
1464 if (getrlimit(RLIMIT_NOFILE, &l) == 0) {
1465 if (set_rlimit == NO_CHANGE)
1466 l.rlim_cur = l.rlim_max;
1467 else {
1468 l.rlim_cur = l.rlim_max + 1000;
1469 l.rlim_max = l.rlim_cur;
1470 }
1471 if (setrlimit(RLIMIT_NOFILE, &l) == 0) {
1472 set_rlimit++;
1473 errno = old_errno;
1474 goto retry_open;
1475 }
1476 }
1477 errno = old_errno;
1478 }
1479
Arnaldo Carvalho de Melo594ac612012-12-13 13:13:07 -03001480 if (err != -EINVAL || cpu > 0 || thread > 0)
1481 goto out_close;
1482
Peter Zijlstra814c8c32015-03-31 00:19:31 +02001483 /*
1484 * Must probe features in the order they were added to the
1485 * perf_event_attr interface.
1486 */
1487 if (!perf_missing_features.clockid_wrong && evsel->attr.use_clockid) {
1488 perf_missing_features.clockid_wrong = true;
1489 goto fallback_missing_features;
1490 } else if (!perf_missing_features.clockid && evsel->attr.use_clockid) {
1491 perf_missing_features.clockid = true;
1492 goto fallback_missing_features;
1493 } else if (!perf_missing_features.cloexec && (flags & PERF_FLAG_FD_CLOEXEC)) {
Yann Droneaud57480d22014-06-30 22:28:47 +02001494 perf_missing_features.cloexec = true;
1495 goto fallback_missing_features;
1496 } else if (!perf_missing_features.mmap2 && evsel->attr.mmap2) {
Stephane Eranian5c5e8542013-08-21 12:10:25 +02001497 perf_missing_features.mmap2 = true;
1498 goto fallback_missing_features;
1499 } else if (!perf_missing_features.exclude_guest &&
1500 (evsel->attr.exclude_guest || evsel->attr.exclude_host)) {
Arnaldo Carvalho de Melo594ac612012-12-13 13:13:07 -03001501 perf_missing_features.exclude_guest = true;
1502 goto fallback_missing_features;
1503 } else if (!perf_missing_features.sample_id_all) {
1504 perf_missing_features.sample_id_all = true;
1505 goto retry_sample_id;
Andi Kleenbd0f8892015-12-11 16:12:24 -08001506 } else if (!perf_missing_features.lbr_flags &&
1507 (evsel->attr.branch_sample_type &
1508 (PERF_SAMPLE_BRANCH_NO_CYCLES |
1509 PERF_SAMPLE_BRANCH_NO_FLAGS))) {
1510 perf_missing_features.lbr_flags = true;
1511 goto fallback_missing_features;
Wang Nanb90dc172016-05-20 16:38:23 +00001512 } else if (!perf_missing_features.write_backward &&
1513 evsel->attr.write_backward) {
1514 perf_missing_features.write_backward = true;
1515 goto fallback_missing_features;
Arnaldo Carvalho de Melo594ac612012-12-13 13:13:07 -03001516 }
1517
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -02001518out_close:
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -02001519 do {
1520 while (--thread >= 0) {
1521 close(FD(evsel, cpu, thread));
1522 FD(evsel, cpu, thread) = -1;
1523 }
Adrian Hunterbf8e8f42014-07-31 09:00:51 +03001524 thread = nthreads;
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -02001525 } while (--cpu >= 0);
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -02001526 return err;
1527}
1528
1529void perf_evsel__close(struct perf_evsel *evsel, int ncpus, int nthreads)
1530{
1531 if (evsel->fd == NULL)
1532 return;
1533
1534 perf_evsel__close_fd(evsel, ncpus, nthreads);
1535 perf_evsel__free_fd(evsel);
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -02001536}
1537
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -02001538static struct {
1539 struct cpu_map map;
1540 int cpus[1];
1541} empty_cpu_map = {
1542 .map.nr = 1,
1543 .cpus = { -1, },
1544};
1545
1546static struct {
1547 struct thread_map map;
1548 int threads[1];
1549} empty_thread_map = {
1550 .map.nr = 1,
1551 .threads = { -1, },
1552};
1553
Arnaldo Carvalho de Melof08199d2011-01-11 23:42:19 -02001554int perf_evsel__open(struct perf_evsel *evsel, struct cpu_map *cpus,
Jiri Olsa6a4bb042012-08-08 12:22:36 +02001555 struct thread_map *threads)
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -02001556{
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -02001557 if (cpus == NULL) {
1558 /* Work around old compiler warnings about strict aliasing */
1559 cpus = &empty_cpu_map.map;
1560 }
1561
1562 if (threads == NULL)
1563 threads = &empty_thread_map.map;
1564
Jiri Olsa6a4bb042012-08-08 12:22:36 +02001565 return __perf_evsel__open(evsel, cpus, threads);
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -02001566}
1567
Arnaldo Carvalho de Melof08199d2011-01-11 23:42:19 -02001568int perf_evsel__open_per_cpu(struct perf_evsel *evsel,
Jiri Olsa6a4bb042012-08-08 12:22:36 +02001569 struct cpu_map *cpus)
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -02001570{
Jiri Olsa6a4bb042012-08-08 12:22:36 +02001571 return __perf_evsel__open(evsel, cpus, &empty_thread_map.map);
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -02001572}
1573
Arnaldo Carvalho de Melof08199d2011-01-11 23:42:19 -02001574int perf_evsel__open_per_thread(struct perf_evsel *evsel,
Jiri Olsa6a4bb042012-08-08 12:22:36 +02001575 struct thread_map *threads)
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -02001576{
Jiri Olsa6a4bb042012-08-08 12:22:36 +02001577 return __perf_evsel__open(evsel, &empty_cpu_map.map, threads);
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -02001578}
Arnaldo Carvalho de Melo70082dd2011-01-12 17:03:24 -02001579
Arnaldo Carvalho de Melo0807d2d2012-09-26 12:48:18 -03001580static int perf_evsel__parse_id_sample(const struct perf_evsel *evsel,
1581 const union perf_event *event,
1582 struct perf_sample *sample)
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001583{
Arnaldo Carvalho de Melo0807d2d2012-09-26 12:48:18 -03001584 u64 type = evsel->attr.sample_type;
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001585 const u64 *array = event->sample.array;
Arnaldo Carvalho de Melo0807d2d2012-09-26 12:48:18 -03001586 bool swapped = evsel->needs_swap;
Jiri Olsa37073f92012-05-30 14:23:44 +02001587 union u64_swap u;
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001588
1589 array += ((event->header.size -
1590 sizeof(event->header)) / sizeof(u64)) - 1;
1591
Adrian Hunter75562572013-08-27 11:23:09 +03001592 if (type & PERF_SAMPLE_IDENTIFIER) {
1593 sample->id = *array;
1594 array--;
1595 }
1596
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001597 if (type & PERF_SAMPLE_CPU) {
Jiri Olsa37073f92012-05-30 14:23:44 +02001598 u.val64 = *array;
1599 if (swapped) {
1600 /* undo swap of u64, then swap on individual u32s */
1601 u.val64 = bswap_64(u.val64);
1602 u.val32[0] = bswap_32(u.val32[0]);
1603 }
1604
1605 sample->cpu = u.val32[0];
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001606 array--;
1607 }
1608
1609 if (type & PERF_SAMPLE_STREAM_ID) {
1610 sample->stream_id = *array;
1611 array--;
1612 }
1613
1614 if (type & PERF_SAMPLE_ID) {
1615 sample->id = *array;
1616 array--;
1617 }
1618
1619 if (type & PERF_SAMPLE_TIME) {
1620 sample->time = *array;
1621 array--;
1622 }
1623
1624 if (type & PERF_SAMPLE_TID) {
Jiri Olsa37073f92012-05-30 14:23:44 +02001625 u.val64 = *array;
1626 if (swapped) {
1627 /* undo swap of u64, then swap on individual u32s */
1628 u.val64 = bswap_64(u.val64);
1629 u.val32[0] = bswap_32(u.val32[0]);
1630 u.val32[1] = bswap_32(u.val32[1]);
1631 }
1632
1633 sample->pid = u.val32[0];
1634 sample->tid = u.val32[1];
Adrian Hunterdd44bc62013-10-18 15:29:01 +03001635 array--;
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001636 }
1637
1638 return 0;
1639}
1640
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001641static inline bool overflow(const void *endp, u16 max_size, const void *offset,
1642 u64 size)
Frederic Weisbecker98e1da92011-05-21 20:08:15 +02001643{
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001644 return size > max_size || offset + size > endp;
Frederic Weisbecker98e1da92011-05-21 20:08:15 +02001645}
1646
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001647#define OVERFLOW_CHECK(offset, size, max_size) \
1648 do { \
1649 if (overflow(endp, (max_size), (offset), (size))) \
1650 return -EFAULT; \
1651 } while (0)
1652
1653#define OVERFLOW_CHECK_u64(offset) \
1654 OVERFLOW_CHECK(offset, sizeof(u64), sizeof(u64))
1655
Arnaldo Carvalho de Meloa3f698f2012-08-02 12:23:46 -03001656int perf_evsel__parse_sample(struct perf_evsel *evsel, union perf_event *event,
Arnaldo Carvalho de Melo0807d2d2012-09-26 12:48:18 -03001657 struct perf_sample *data)
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001658{
Arnaldo Carvalho de Meloa3f698f2012-08-02 12:23:46 -03001659 u64 type = evsel->attr.sample_type;
Arnaldo Carvalho de Melo0807d2d2012-09-26 12:48:18 -03001660 bool swapped = evsel->needs_swap;
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001661 const u64 *array;
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001662 u16 max_size = event->header.size;
1663 const void *endp = (void *)event + max_size;
1664 u64 sz;
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001665
David Ahern936be502011-09-06 09:12:26 -06001666 /*
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;
David Ahern936be502011-09-06 09:12:26 -06001671
Robert Richterf3bda2c2011-12-15 17:32:39 +01001672 memset(data, 0, sizeof(*data));
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001673 data->cpu = data->pid = data->tid = -1;
1674 data->stream_id = data->id = data->time = -1ULL;
Jiri Olsabc529082014-02-03 12:44:41 +01001675 data->period = evsel->attr.sample_period;
Andi Kleen05484292013-01-24 16:10:29 +01001676 data->weight = 0;
Arnaldo Carvalho de Melo473398a2016-03-22 18:23:43 -03001677 data->cpumode = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001678
1679 if (event->header.type != PERF_RECORD_SAMPLE) {
Arnaldo Carvalho de Meloa3f698f2012-08-02 12:23:46 -03001680 if (!evsel->attr.sample_id_all)
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001681 return 0;
Arnaldo Carvalho de Melo0807d2d2012-09-26 12:48:18 -03001682 return perf_evsel__parse_id_sample(evsel, event, data);
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001683 }
1684
1685 array = event->sample.array;
1686
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001687 /*
1688 * The evsel's sample_size is based on PERF_SAMPLE_MASK which includes
1689 * up to PERF_SAMPLE_PERIOD. After that overflow() must be used to
1690 * check the format does not go past the end of the event.
1691 */
Arnaldo Carvalho de Meloa3f698f2012-08-02 12:23:46 -03001692 if (evsel->sample_size + sizeof(event->header) > event->header.size)
Frederic Weisbeckera2854122011-05-21 19:33:04 +02001693 return -EFAULT;
1694
Adrian Hunter75562572013-08-27 11:23:09 +03001695 data->id = -1ULL;
1696 if (type & PERF_SAMPLE_IDENTIFIER) {
1697 data->id = *array;
1698 array++;
1699 }
1700
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001701 if (type & PERF_SAMPLE_IP) {
Adrian Hunteref893252013-08-27 11:23:06 +03001702 data->ip = *array;
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001703 array++;
1704 }
1705
1706 if (type & PERF_SAMPLE_TID) {
David Ahern936be502011-09-06 09:12:26 -06001707 u.val64 = *array;
1708 if (swapped) {
1709 /* undo swap of u64, then swap on individual u32s */
1710 u.val64 = bswap_64(u.val64);
1711 u.val32[0] = bswap_32(u.val32[0]);
1712 u.val32[1] = bswap_32(u.val32[1]);
1713 }
1714
1715 data->pid = u.val32[0];
1716 data->tid = u.val32[1];
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001717 array++;
1718 }
1719
1720 if (type & PERF_SAMPLE_TIME) {
1721 data->time = *array;
1722 array++;
1723 }
1724
David Ahern7cec0922011-05-30 13:08:23 -06001725 data->addr = 0;
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001726 if (type & PERF_SAMPLE_ADDR) {
1727 data->addr = *array;
1728 array++;
1729 }
1730
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001731 if (type & PERF_SAMPLE_ID) {
1732 data->id = *array;
1733 array++;
1734 }
1735
1736 if (type & PERF_SAMPLE_STREAM_ID) {
1737 data->stream_id = *array;
1738 array++;
1739 }
1740
1741 if (type & PERF_SAMPLE_CPU) {
David Ahern936be502011-09-06 09:12:26 -06001742
1743 u.val64 = *array;
1744 if (swapped) {
1745 /* undo swap of u64, then swap on individual u32s */
1746 u.val64 = bswap_64(u.val64);
1747 u.val32[0] = bswap_32(u.val32[0]);
1748 }
1749
1750 data->cpu = u.val32[0];
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001751 array++;
1752 }
1753
1754 if (type & PERF_SAMPLE_PERIOD) {
1755 data->period = *array;
1756 array++;
1757 }
1758
1759 if (type & PERF_SAMPLE_READ) {
Jiri Olsa9ede4732012-10-10 17:38:13 +02001760 u64 read_format = evsel->attr.read_format;
1761
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001762 OVERFLOW_CHECK_u64(array);
Jiri Olsa9ede4732012-10-10 17:38:13 +02001763 if (read_format & PERF_FORMAT_GROUP)
1764 data->read.group.nr = *array;
1765 else
1766 data->read.one.value = *array;
1767
1768 array++;
1769
1770 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) {
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001771 OVERFLOW_CHECK_u64(array);
Jiri Olsa9ede4732012-10-10 17:38:13 +02001772 data->read.time_enabled = *array;
1773 array++;
1774 }
1775
1776 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) {
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001777 OVERFLOW_CHECK_u64(array);
Jiri Olsa9ede4732012-10-10 17:38:13 +02001778 data->read.time_running = *array;
1779 array++;
1780 }
1781
1782 /* PERF_FORMAT_ID is forced for PERF_SAMPLE_READ */
1783 if (read_format & PERF_FORMAT_GROUP) {
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001784 const u64 max_group_nr = UINT64_MAX /
1785 sizeof(struct sample_read_value);
1786
1787 if (data->read.group.nr > max_group_nr)
1788 return -EFAULT;
1789 sz = data->read.group.nr *
1790 sizeof(struct sample_read_value);
1791 OVERFLOW_CHECK(array, sz, max_size);
1792 data->read.group.values =
1793 (struct sample_read_value *)array;
1794 array = (void *)array + sz;
Jiri Olsa9ede4732012-10-10 17:38:13 +02001795 } else {
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001796 OVERFLOW_CHECK_u64(array);
Jiri Olsa9ede4732012-10-10 17:38:13 +02001797 data->read.one.id = *array;
1798 array++;
1799 }
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001800 }
1801
1802 if (type & PERF_SAMPLE_CALLCHAIN) {
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001803 const u64 max_callchain_nr = UINT64_MAX / sizeof(u64);
1804
1805 OVERFLOW_CHECK_u64(array);
1806 data->callchain = (struct ip_callchain *)array++;
1807 if (data->callchain->nr > max_callchain_nr)
Frederic Weisbecker98e1da92011-05-21 20:08:15 +02001808 return -EFAULT;
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001809 sz = data->callchain->nr * sizeof(u64);
1810 OVERFLOW_CHECK(array, sz, max_size);
1811 array = (void *)array + sz;
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001812 }
1813
1814 if (type & PERF_SAMPLE_RAW) {
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001815 OVERFLOW_CHECK_u64(array);
David Ahern936be502011-09-06 09:12:26 -06001816 u.val64 = *array;
1817 if (WARN_ONCE(swapped,
1818 "Endianness of raw data not corrected!\n")) {
1819 /* undo swap of u64, then swap on individual u32s */
1820 u.val64 = bswap_64(u.val64);
1821 u.val32[0] = bswap_32(u.val32[0]);
1822 u.val32[1] = bswap_32(u.val32[1]);
1823 }
David Ahern936be502011-09-06 09:12:26 -06001824 data->raw_size = u.val32[0];
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001825 array = (void *)array + sizeof(u32);
Frederic Weisbecker98e1da92011-05-21 20:08:15 +02001826
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001827 OVERFLOW_CHECK(array, data->raw_size, max_size);
1828 data->raw_data = (void *)array;
1829 array = (void *)array + data->raw_size;
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001830 }
1831
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +01001832 if (type & PERF_SAMPLE_BRANCH_STACK) {
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001833 const u64 max_branch_nr = UINT64_MAX /
1834 sizeof(struct branch_entry);
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +01001835
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001836 OVERFLOW_CHECK_u64(array);
1837 data->branch_stack = (struct branch_stack *)array++;
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +01001838
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001839 if (data->branch_stack->nr > max_branch_nr)
1840 return -EFAULT;
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +01001841 sz = data->branch_stack->nr * sizeof(struct branch_entry);
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001842 OVERFLOW_CHECK(array, sz, max_size);
1843 array = (void *)array + sz;
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +01001844 }
Jiri Olsa0f6a3012012-08-07 15:20:45 +02001845
1846 if (type & PERF_SAMPLE_REGS_USER) {
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001847 OVERFLOW_CHECK_u64(array);
Adrian Hunter5b95a4a32013-08-27 11:23:10 +03001848 data->user_regs.abi = *array;
1849 array++;
Jiri Olsa0f6a3012012-08-07 15:20:45 +02001850
Adrian Hunter5b95a4a32013-08-27 11:23:10 +03001851 if (data->user_regs.abi) {
Jiri Olsa352ea452014-01-07 13:47:25 +01001852 u64 mask = evsel->attr.sample_regs_user;
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001853
Jiri Olsa352ea452014-01-07 13:47:25 +01001854 sz = hweight_long(mask) * sizeof(u64);
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001855 OVERFLOW_CHECK(array, sz, max_size);
Jiri Olsa352ea452014-01-07 13:47:25 +01001856 data->user_regs.mask = mask;
Jiri Olsa0f6a3012012-08-07 15:20:45 +02001857 data->user_regs.regs = (u64 *)array;
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001858 array = (void *)array + sz;
Jiri Olsa0f6a3012012-08-07 15:20:45 +02001859 }
1860 }
1861
1862 if (type & PERF_SAMPLE_STACK_USER) {
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001863 OVERFLOW_CHECK_u64(array);
1864 sz = *array++;
Jiri Olsa0f6a3012012-08-07 15:20:45 +02001865
1866 data->user_stack.offset = ((char *)(array - 1)
1867 - (char *) event);
1868
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001869 if (!sz) {
Jiri Olsa0f6a3012012-08-07 15:20:45 +02001870 data->user_stack.size = 0;
1871 } else {
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001872 OVERFLOW_CHECK(array, sz, max_size);
Jiri Olsa0f6a3012012-08-07 15:20:45 +02001873 data->user_stack.data = (char *)array;
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001874 array = (void *)array + sz;
1875 OVERFLOW_CHECK_u64(array);
Adrian Hunter54bd2692013-07-04 16:20:34 +03001876 data->user_stack.size = *array++;
Jiri Olsaa65cb4b2013-10-02 15:46:39 +02001877 if (WARN_ONCE(data->user_stack.size > sz,
1878 "user stack dump failure\n"))
1879 return -EFAULT;
Jiri Olsa0f6a3012012-08-07 15:20:45 +02001880 }
1881 }
1882
Andi Kleen05484292013-01-24 16:10:29 +01001883 data->weight = 0;
1884 if (type & PERF_SAMPLE_WEIGHT) {
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001885 OVERFLOW_CHECK_u64(array);
Andi Kleen05484292013-01-24 16:10:29 +01001886 data->weight = *array;
1887 array++;
1888 }
1889
Stephane Eranian98a3b322013-01-24 16:10:35 +01001890 data->data_src = PERF_MEM_DATA_SRC_NONE;
1891 if (type & PERF_SAMPLE_DATA_SRC) {
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001892 OVERFLOW_CHECK_u64(array);
Stephane Eranian98a3b322013-01-24 16:10:35 +01001893 data->data_src = *array;
1894 array++;
1895 }
1896
Andi Kleen475eeab2013-09-20 07:40:43 -07001897 data->transaction = 0;
1898 if (type & PERF_SAMPLE_TRANSACTION) {
Adrian Hunter87b95522013-11-01 15:51:36 +02001899 OVERFLOW_CHECK_u64(array);
Andi Kleen475eeab2013-09-20 07:40:43 -07001900 data->transaction = *array;
1901 array++;
1902 }
1903
Stephane Eranian6a21c0b2014-09-24 13:48:39 +02001904 data->intr_regs.abi = PERF_SAMPLE_REGS_ABI_NONE;
1905 if (type & PERF_SAMPLE_REGS_INTR) {
1906 OVERFLOW_CHECK_u64(array);
1907 data->intr_regs.abi = *array;
1908 array++;
1909
1910 if (data->intr_regs.abi != PERF_SAMPLE_REGS_ABI_NONE) {
1911 u64 mask = evsel->attr.sample_regs_intr;
1912
1913 sz = hweight_long(mask) * sizeof(u64);
1914 OVERFLOW_CHECK(array, sz, max_size);
1915 data->intr_regs.mask = mask;
1916 data->intr_regs.regs = (u64 *)array;
1917 array = (void *)array + sz;
1918 }
1919 }
1920
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001921 return 0;
1922}
Andrew Vagin74eec262011-11-28 12:03:31 +03001923
Adrian Hunterb1cf6f62013-08-27 11:23:12 +03001924size_t perf_event__sample_event_size(const struct perf_sample *sample, u64 type,
Jiri Olsa352ea452014-01-07 13:47:25 +01001925 u64 read_format)
Adrian Hunterb1cf6f62013-08-27 11:23:12 +03001926{
1927 size_t sz, result = sizeof(struct sample_event);
1928
1929 if (type & PERF_SAMPLE_IDENTIFIER)
1930 result += sizeof(u64);
1931
1932 if (type & PERF_SAMPLE_IP)
1933 result += sizeof(u64);
1934
1935 if (type & PERF_SAMPLE_TID)
1936 result += sizeof(u64);
1937
1938 if (type & PERF_SAMPLE_TIME)
1939 result += sizeof(u64);
1940
1941 if (type & PERF_SAMPLE_ADDR)
1942 result += sizeof(u64);
1943
1944 if (type & PERF_SAMPLE_ID)
1945 result += sizeof(u64);
1946
1947 if (type & PERF_SAMPLE_STREAM_ID)
1948 result += sizeof(u64);
1949
1950 if (type & PERF_SAMPLE_CPU)
1951 result += sizeof(u64);
1952
1953 if (type & PERF_SAMPLE_PERIOD)
1954 result += sizeof(u64);
1955
1956 if (type & PERF_SAMPLE_READ) {
1957 result += sizeof(u64);
1958 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
1959 result += sizeof(u64);
1960 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
1961 result += sizeof(u64);
1962 /* PERF_FORMAT_ID is forced for PERF_SAMPLE_READ */
1963 if (read_format & PERF_FORMAT_GROUP) {
1964 sz = sample->read.group.nr *
1965 sizeof(struct sample_read_value);
1966 result += sz;
1967 } else {
1968 result += sizeof(u64);
1969 }
1970 }
1971
1972 if (type & PERF_SAMPLE_CALLCHAIN) {
1973 sz = (sample->callchain->nr + 1) * sizeof(u64);
1974 result += sz;
1975 }
1976
1977 if (type & PERF_SAMPLE_RAW) {
1978 result += sizeof(u32);
1979 result += sample->raw_size;
1980 }
1981
1982 if (type & PERF_SAMPLE_BRANCH_STACK) {
1983 sz = sample->branch_stack->nr * sizeof(struct branch_entry);
1984 sz += sizeof(u64);
1985 result += sz;
1986 }
1987
1988 if (type & PERF_SAMPLE_REGS_USER) {
1989 if (sample->user_regs.abi) {
1990 result += sizeof(u64);
Jiri Olsa352ea452014-01-07 13:47:25 +01001991 sz = hweight_long(sample->user_regs.mask) * sizeof(u64);
Adrian Hunterb1cf6f62013-08-27 11:23:12 +03001992 result += sz;
1993 } else {
1994 result += sizeof(u64);
1995 }
1996 }
1997
1998 if (type & PERF_SAMPLE_STACK_USER) {
1999 sz = sample->user_stack.size;
2000 result += sizeof(u64);
2001 if (sz) {
2002 result += sz;
2003 result += sizeof(u64);
2004 }
2005 }
2006
2007 if (type & PERF_SAMPLE_WEIGHT)
2008 result += sizeof(u64);
2009
2010 if (type & PERF_SAMPLE_DATA_SRC)
2011 result += sizeof(u64);
2012
Adrian Hunter42d88912013-11-01 15:51:38 +02002013 if (type & PERF_SAMPLE_TRANSACTION)
2014 result += sizeof(u64);
2015
Stephane Eranian6a21c0b2014-09-24 13:48:39 +02002016 if (type & PERF_SAMPLE_REGS_INTR) {
2017 if (sample->intr_regs.abi) {
2018 result += sizeof(u64);
2019 sz = hweight_long(sample->intr_regs.mask) * sizeof(u64);
2020 result += sz;
2021 } else {
2022 result += sizeof(u64);
2023 }
2024 }
2025
Adrian Hunterb1cf6f62013-08-27 11:23:12 +03002026 return result;
2027}
2028
Andrew Vagin74eec262011-11-28 12:03:31 +03002029int perf_event__synthesize_sample(union perf_event *event, u64 type,
Jiri Olsa352ea452014-01-07 13:47:25 +01002030 u64 read_format,
Andrew Vagin74eec262011-11-28 12:03:31 +03002031 const struct perf_sample *sample,
2032 bool swapped)
2033{
2034 u64 *array;
Adrian Hunterd03f2172013-08-27 11:23:11 +03002035 size_t sz;
Andrew Vagin74eec262011-11-28 12:03:31 +03002036 /*
2037 * used for cross-endian analysis. See git commit 65014ab3
2038 * for why this goofiness is needed.
2039 */
Jiri Olsa6a11f922012-05-16 08:59:04 +02002040 union u64_swap u;
Andrew Vagin74eec262011-11-28 12:03:31 +03002041
2042 array = event->sample.array;
2043
Adrian Hunter75562572013-08-27 11:23:09 +03002044 if (type & PERF_SAMPLE_IDENTIFIER) {
2045 *array = sample->id;
2046 array++;
2047 }
2048
Andrew Vagin74eec262011-11-28 12:03:31 +03002049 if (type & PERF_SAMPLE_IP) {
Adrian Hunteref893252013-08-27 11:23:06 +03002050 *array = sample->ip;
Andrew Vagin74eec262011-11-28 12:03:31 +03002051 array++;
2052 }
2053
2054 if (type & PERF_SAMPLE_TID) {
2055 u.val32[0] = sample->pid;
2056 u.val32[1] = sample->tid;
2057 if (swapped) {
2058 /*
Arnaldo Carvalho de Meloa3f698f2012-08-02 12:23:46 -03002059 * Inverse of what is done in perf_evsel__parse_sample
Andrew Vagin74eec262011-11-28 12:03:31 +03002060 */
2061 u.val32[0] = bswap_32(u.val32[0]);
2062 u.val32[1] = bswap_32(u.val32[1]);
2063 u.val64 = bswap_64(u.val64);
2064 }
2065
2066 *array = u.val64;
2067 array++;
2068 }
2069
2070 if (type & PERF_SAMPLE_TIME) {
2071 *array = sample->time;
2072 array++;
2073 }
2074
2075 if (type & PERF_SAMPLE_ADDR) {
2076 *array = sample->addr;
2077 array++;
2078 }
2079
2080 if (type & PERF_SAMPLE_ID) {
2081 *array = sample->id;
2082 array++;
2083 }
2084
2085 if (type & PERF_SAMPLE_STREAM_ID) {
2086 *array = sample->stream_id;
2087 array++;
2088 }
2089
2090 if (type & PERF_SAMPLE_CPU) {
2091 u.val32[0] = sample->cpu;
2092 if (swapped) {
2093 /*
Arnaldo Carvalho de Meloa3f698f2012-08-02 12:23:46 -03002094 * Inverse of what is done in perf_evsel__parse_sample
Andrew Vagin74eec262011-11-28 12:03:31 +03002095 */
2096 u.val32[0] = bswap_32(u.val32[0]);
2097 u.val64 = bswap_64(u.val64);
2098 }
2099 *array = u.val64;
2100 array++;
2101 }
2102
2103 if (type & PERF_SAMPLE_PERIOD) {
2104 *array = sample->period;
2105 array++;
2106 }
2107
Adrian Hunterd03f2172013-08-27 11:23:11 +03002108 if (type & PERF_SAMPLE_READ) {
2109 if (read_format & PERF_FORMAT_GROUP)
2110 *array = sample->read.group.nr;
2111 else
2112 *array = sample->read.one.value;
2113 array++;
2114
2115 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) {
2116 *array = sample->read.time_enabled;
2117 array++;
2118 }
2119
2120 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) {
2121 *array = sample->read.time_running;
2122 array++;
2123 }
2124
2125 /* PERF_FORMAT_ID is forced for PERF_SAMPLE_READ */
2126 if (read_format & PERF_FORMAT_GROUP) {
2127 sz = sample->read.group.nr *
2128 sizeof(struct sample_read_value);
2129 memcpy(array, sample->read.group.values, sz);
2130 array = (void *)array + sz;
2131 } else {
2132 *array = sample->read.one.id;
2133 array++;
2134 }
2135 }
2136
2137 if (type & PERF_SAMPLE_CALLCHAIN) {
2138 sz = (sample->callchain->nr + 1) * sizeof(u64);
2139 memcpy(array, sample->callchain, sz);
2140 array = (void *)array + sz;
2141 }
2142
2143 if (type & PERF_SAMPLE_RAW) {
2144 u.val32[0] = sample->raw_size;
2145 if (WARN_ONCE(swapped,
2146 "Endianness of raw data not corrected!\n")) {
2147 /*
2148 * Inverse of what is done in perf_evsel__parse_sample
2149 */
2150 u.val32[0] = bswap_32(u.val32[0]);
2151 u.val32[1] = bswap_32(u.val32[1]);
2152 u.val64 = bswap_64(u.val64);
2153 }
2154 *array = u.val64;
2155 array = (void *)array + sizeof(u32);
2156
2157 memcpy(array, sample->raw_data, sample->raw_size);
2158 array = (void *)array + sample->raw_size;
2159 }
2160
2161 if (type & PERF_SAMPLE_BRANCH_STACK) {
2162 sz = sample->branch_stack->nr * sizeof(struct branch_entry);
2163 sz += sizeof(u64);
2164 memcpy(array, sample->branch_stack, sz);
2165 array = (void *)array + sz;
2166 }
2167
2168 if (type & PERF_SAMPLE_REGS_USER) {
2169 if (sample->user_regs.abi) {
2170 *array++ = sample->user_regs.abi;
Jiri Olsa352ea452014-01-07 13:47:25 +01002171 sz = hweight_long(sample->user_regs.mask) * sizeof(u64);
Adrian Hunterd03f2172013-08-27 11:23:11 +03002172 memcpy(array, sample->user_regs.regs, sz);
2173 array = (void *)array + sz;
2174 } else {
2175 *array++ = 0;
2176 }
2177 }
2178
2179 if (type & PERF_SAMPLE_STACK_USER) {
2180 sz = sample->user_stack.size;
2181 *array++ = sz;
2182 if (sz) {
2183 memcpy(array, sample->user_stack.data, sz);
2184 array = (void *)array + sz;
2185 *array++ = sz;
2186 }
2187 }
2188
2189 if (type & PERF_SAMPLE_WEIGHT) {
2190 *array = sample->weight;
2191 array++;
2192 }
2193
2194 if (type & PERF_SAMPLE_DATA_SRC) {
2195 *array = sample->data_src;
2196 array++;
2197 }
2198
Adrian Hunter42d88912013-11-01 15:51:38 +02002199 if (type & PERF_SAMPLE_TRANSACTION) {
2200 *array = sample->transaction;
2201 array++;
2202 }
2203
Stephane Eranian6a21c0b2014-09-24 13:48:39 +02002204 if (type & PERF_SAMPLE_REGS_INTR) {
2205 if (sample->intr_regs.abi) {
2206 *array++ = sample->intr_regs.abi;
2207 sz = hweight_long(sample->intr_regs.mask) * sizeof(u64);
2208 memcpy(array, sample->intr_regs.regs, sz);
2209 array = (void *)array + sz;
2210 } else {
2211 *array++ = 0;
2212 }
2213 }
2214
Andrew Vagin74eec262011-11-28 12:03:31 +03002215 return 0;
2216}
Arnaldo Carvalho de Melo5555ded2012-09-11 19:24:23 -03002217
Arnaldo Carvalho de Meloefd2b922012-09-18 11:21:50 -03002218struct format_field *perf_evsel__field(struct perf_evsel *evsel, const char *name)
2219{
2220 return pevent_find_field(evsel->tp_format, name);
2221}
2222
Arnaldo Carvalho de Melo5d2074e2012-09-26 20:22:00 -03002223void *perf_evsel__rawptr(struct perf_evsel *evsel, struct perf_sample *sample,
Arnaldo Carvalho de Melo5555ded2012-09-11 19:24:23 -03002224 const char *name)
2225{
Arnaldo Carvalho de Meloefd2b922012-09-18 11:21:50 -03002226 struct format_field *field = perf_evsel__field(evsel, name);
Arnaldo Carvalho de Melo5555ded2012-09-11 19:24:23 -03002227 int offset;
2228
Arnaldo Carvalho de Meloefd2b922012-09-18 11:21:50 -03002229 if (!field)
2230 return NULL;
Arnaldo Carvalho de Melo5555ded2012-09-11 19:24:23 -03002231
2232 offset = field->offset;
2233
2234 if (field->flags & FIELD_IS_DYNAMIC) {
2235 offset = *(int *)(sample->raw_data + field->offset);
2236 offset &= 0xffff;
2237 }
2238
2239 return sample->raw_data + offset;
2240}
2241
2242u64 perf_evsel__intval(struct perf_evsel *evsel, struct perf_sample *sample,
2243 const char *name)
2244{
Arnaldo Carvalho de Meloefd2b922012-09-18 11:21:50 -03002245 struct format_field *field = perf_evsel__field(evsel, name);
Arnaldo Carvalho de Meloe6b6f672012-09-26 13:13:04 -03002246 void *ptr;
2247 u64 value;
Arnaldo Carvalho de Melo5555ded2012-09-11 19:24:23 -03002248
Arnaldo Carvalho de Meloefd2b922012-09-18 11:21:50 -03002249 if (!field)
2250 return 0;
Arnaldo Carvalho de Melo5555ded2012-09-11 19:24:23 -03002251
Arnaldo Carvalho de Meloe6b6f672012-09-26 13:13:04 -03002252 ptr = sample->raw_data + field->offset;
Arnaldo Carvalho de Melo5555ded2012-09-11 19:24:23 -03002253
Arnaldo Carvalho de Meloe6b6f672012-09-26 13:13:04 -03002254 switch (field->size) {
2255 case 1:
2256 return *(u8 *)ptr;
2257 case 2:
2258 value = *(u16 *)ptr;
2259 break;
2260 case 4:
2261 value = *(u32 *)ptr;
2262 break;
2263 case 8:
David Aherne94eeda2015-03-24 16:14:09 -04002264 memcpy(&value, ptr, sizeof(u64));
Arnaldo Carvalho de Meloe6b6f672012-09-26 13:13:04 -03002265 break;
2266 default:
2267 return 0;
2268 }
2269
2270 if (!evsel->needs_swap)
2271 return value;
2272
2273 switch (field->size) {
2274 case 2:
2275 return bswap_16(value);
2276 case 4:
2277 return bswap_32(value);
2278 case 8:
2279 return bswap_64(value);
2280 default:
2281 return 0;
2282 }
2283
2284 return 0;
Arnaldo Carvalho de Melo5555ded2012-09-11 19:24:23 -03002285}
Arnaldo Carvalho de Melo0698aed2012-12-10 18:17:08 -03002286
Arnaldo Carvalho de Meloc0a54342012-12-13 14:16:30 -03002287bool perf_evsel__fallback(struct perf_evsel *evsel, int err,
2288 char *msg, size_t msgsize)
2289{
Arnaldo Carvalho de Melo08094822016-05-12 16:07:47 -03002290 int paranoid;
2291
David Ahern2b821cc2013-07-18 17:27:59 -06002292 if ((err == ENOENT || err == ENXIO || err == ENODEV) &&
Arnaldo Carvalho de Meloc0a54342012-12-13 14:16:30 -03002293 evsel->attr.type == PERF_TYPE_HARDWARE &&
2294 evsel->attr.config == PERF_COUNT_HW_CPU_CYCLES) {
2295 /*
2296 * If it's cycles then fall back to hrtimer based
2297 * cpu-clock-tick sw counter, which is always available even if
2298 * no PMU support.
2299 *
2300 * PPC returns ENXIO until 2.6.37 (behavior changed with commit
2301 * b0a873e).
2302 */
2303 scnprintf(msg, msgsize, "%s",
2304"The cycles event is not supported, trying to fall back to cpu-clock-ticks");
2305
2306 evsel->attr.type = PERF_TYPE_SOFTWARE;
2307 evsel->attr.config = PERF_COUNT_SW_CPU_CLOCK;
2308
Arnaldo Carvalho de Melo04662522013-12-26 17:41:15 -03002309 zfree(&evsel->name);
Arnaldo Carvalho de Meloc0a54342012-12-13 14:16:30 -03002310 return true;
Arnaldo Carvalho de Melo08094822016-05-12 16:07:47 -03002311 } else if (err == EACCES && !evsel->attr.exclude_kernel &&
2312 (paranoid = perf_event_paranoid()) > 1) {
2313 const char *name = perf_evsel__name(evsel);
2314 char *new_name;
2315
2316 if (asprintf(&new_name, "%s%su", name, strchr(name, ':') ? "" : ":") < 0)
2317 return false;
2318
2319 if (evsel->name)
2320 free(evsel->name);
2321 evsel->name = new_name;
2322 scnprintf(msg, msgsize,
2323"kernel.perf_event_paranoid=%d, trying to fall back to excluding kernel samples", paranoid);
2324 evsel->attr.exclude_kernel = 1;
2325
2326 return true;
Arnaldo Carvalho de Meloc0a54342012-12-13 14:16:30 -03002327 }
2328
2329 return false;
2330}
Arnaldo Carvalho de Melo56e52e82012-12-13 15:10:58 -03002331
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03002332int perf_evsel__open_strerror(struct perf_evsel *evsel, struct target *target,
Arnaldo Carvalho de Melo56e52e82012-12-13 15:10:58 -03002333 int err, char *msg, size_t size)
2334{
Masami Hiramatsu6e81c742014-08-14 02:22:36 +00002335 char sbuf[STRERR_BUFSIZE];
2336
Arnaldo Carvalho de Melo56e52e82012-12-13 15:10:58 -03002337 switch (err) {
2338 case EPERM:
2339 case EACCES:
David Ahernb69e63a2013-05-25 17:54:00 -06002340 return scnprintf(msg, size,
Ben Hutchings3379e0c2016-01-19 21:35:15 +00002341 "You may not have permission to collect %sstats.\n\n"
2342 "Consider tweaking /proc/sys/kernel/perf_event_paranoid,\n"
2343 "which controls use of the performance events system by\n"
2344 "unprivileged users (without CAP_SYS_ADMIN).\n\n"
Arnaldo Carvalho de Melo7d173912016-05-12 15:44:55 -03002345 "The current value is %d:\n\n"
Ben Hutchings3379e0c2016-01-19 21:35:15 +00002346 " -1: Allow use of (almost) all events by all users\n"
2347 ">= 0: Disallow raw tracepoint access by users without CAP_IOC_LOCK\n"
2348 ">= 1: Disallow CPU event access by users without CAP_SYS_ADMIN\n"
2349 ">= 2: Disallow kernel profiling by users without CAP_SYS_ADMIN",
Arnaldo Carvalho de Melo7d173912016-05-12 15:44:55 -03002350 target->system_wide ? "system-wide " : "",
2351 perf_event_paranoid());
Arnaldo Carvalho de Melo56e52e82012-12-13 15:10:58 -03002352 case ENOENT:
2353 return scnprintf(msg, size, "The %s event is not supported.",
2354 perf_evsel__name(evsel));
2355 case EMFILE:
2356 return scnprintf(msg, size, "%s",
2357 "Too many events are opened.\n"
Jiri Olsa18ffdfe2015-05-25 22:51:54 +02002358 "Probably the maximum number of open file descriptors has been reached.\n"
2359 "Hint: Try again after reducing the number of events.\n"
2360 "Hint: Try increasing the limit with 'ulimit -n <limit>'");
Arnaldo Carvalho de Melode46d522016-04-27 17:51:45 -03002361 case ENOMEM:
2362 if ((evsel->attr.sample_type & PERF_SAMPLE_CALLCHAIN) != 0 &&
2363 access("/proc/sys/kernel/perf_event_max_stack", F_OK) == 0)
2364 return scnprintf(msg, size,
2365 "Not enough memory to setup event with callchain.\n"
2366 "Hint: Try tweaking /proc/sys/kernel/perf_event_max_stack\n"
2367 "Hint: Current value: %d", sysctl_perf_event_max_stack);
2368 break;
Arnaldo Carvalho de Melo56e52e82012-12-13 15:10:58 -03002369 case ENODEV:
2370 if (target->cpu_list)
2371 return scnprintf(msg, size, "%s",
Arnaldo Carvalho de Melo81d64f42016-04-27 17:56:53 -03002372 "No such device - did you specify an out-of-range profile CPU?");
Arnaldo Carvalho de Melo56e52e82012-12-13 15:10:58 -03002373 break;
2374 case EOPNOTSUPP:
2375 if (evsel->attr.precise_ip)
2376 return scnprintf(msg, size, "%s",
2377 "\'precise\' request may not be supported. Try removing 'p' modifier.");
2378#if defined(__i386__) || defined(__x86_64__)
2379 if (evsel->attr.type == PERF_TYPE_HARDWARE)
2380 return scnprintf(msg, size, "%s",
2381 "No hardware sampling interrupt available.\n"
2382 "No APIC? If so then you can boot the kernel with the \"lapic\" boot parameter to force-enable it.");
2383#endif
2384 break;
Jiri Olsa63914ac2014-08-01 17:46:54 +02002385 case EBUSY:
2386 if (find_process("oprofiled"))
2387 return scnprintf(msg, size,
2388 "The PMU counters are busy/taken by another profiler.\n"
2389 "We found oprofile daemon running, please stop it and try again.");
2390 break;
Peter Zijlstra814c8c32015-03-31 00:19:31 +02002391 case EINVAL:
2392 if (perf_missing_features.clockid)
2393 return scnprintf(msg, size, "clockid feature not supported.");
2394 if (perf_missing_features.clockid_wrong)
2395 return scnprintf(msg, size, "wrong clockid (%d).", clockid);
2396 break;
Arnaldo Carvalho de Melo56e52e82012-12-13 15:10:58 -03002397 default:
2398 break;
2399 }
2400
2401 return scnprintf(msg, size,
Masami Hiramatsu6e81c742014-08-14 02:22:36 +00002402 "The sys_perf_event_open() syscall returned with %d (%s) for event (%s).\n"
Arnaldo Carvalho de Melo56e52e82012-12-13 15:10:58 -03002403 "/bin/dmesg may provide additional information.\n"
Arnaldo Carvalho de Melo81d64f42016-04-27 17:56:53 -03002404 "No CONFIG_PERF_EVENTS=y kernel support configured?",
Masami Hiramatsu6e81c742014-08-14 02:22:36 +00002405 err, strerror_r(err, sbuf, sizeof(sbuf)),
2406 perf_evsel__name(evsel));
Arnaldo Carvalho de Melo56e52e82012-12-13 15:10:58 -03002407}