blob: d9b80ef881cde43a1e134b89c6a752f2ae06d108 [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 Melo5496bc02016-07-07 11:51:47 -0300203/**
204 * perf_evsel__is_function_event - Return whether given evsel is a function
205 * trace event
206 *
207 * @evsel - evsel selector to be tested
208 *
209 * Return %true if event is function trace event
210 */
211bool perf_evsel__is_function_event(struct perf_evsel *evsel)
212{
213#define FUNCTION_EVENT "ftrace:function"
214
215 return evsel->name &&
216 !strncmp(FUNCTION_EVENT, evsel->name, sizeof(FUNCTION_EVENT));
217
218#undef FUNCTION_EVENT
219}
220
Arnaldo Carvalho de Meloef1d1af2011-01-18 21:41:45 -0200221void perf_evsel__init(struct perf_evsel *evsel,
222 struct perf_event_attr *attr, int idx)
223{
224 evsel->idx = idx;
Adrian Hunter60b08962014-07-31 09:00:52 +0300225 evsel->tracking = !idx;
Arnaldo Carvalho de Meloef1d1af2011-01-18 21:41:45 -0200226 evsel->attr = *attr;
Namhyung Kim2cfda562012-11-29 15:38:29 +0900227 evsel->leader = evsel;
Stephane Eranian410136f2013-11-12 17:58:49 +0100228 evsel->unit = "";
229 evsel->scale = 1.0;
Arnaldo Carvalho de Melod49e4692015-08-27 08:07:40 -0400230 evsel->evlist = NULL;
Wang Nan1f45b1d2015-10-14 12:41:18 +0000231 evsel->bpf_fd = -1;
Arnaldo Carvalho de Meloef1d1af2011-01-18 21:41:45 -0200232 INIT_LIST_HEAD(&evsel->node);
Jiri Olsa930a2e22015-07-29 05:42:10 -0400233 INIT_LIST_HEAD(&evsel->config_terms);
Arnaldo Carvalho de Meloce8ccff2014-10-09 15:29:51 -0300234 perf_evsel__object.init(evsel);
Arnaldo Carvalho de Melobde09462012-08-01 18:53:11 -0300235 evsel->sample_size = __perf_evsel__sample_size(attr->sample_type);
Adrian Hunter75562572013-08-27 11:23:09 +0300236 perf_evsel__calc_id_pos(evsel);
Wang Nan15bfd2c2015-07-10 07:36:09 +0000237 evsel->cmdline_group_boundary = false;
Arnaldo Carvalho de Meloef1d1af2011-01-18 21:41:45 -0200238}
239
Arnaldo Carvalho de Meloef503832013-11-07 16:41:19 -0300240struct perf_evsel *perf_evsel__new_idx(struct perf_event_attr *attr, int idx)
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200241{
Arnaldo Carvalho de Meloce8ccff2014-10-09 15:29:51 -0300242 struct perf_evsel *evsel = zalloc(perf_evsel__object.size);
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200243
Arnaldo Carvalho de Meloef1d1af2011-01-18 21:41:45 -0200244 if (evsel != NULL)
245 perf_evsel__init(evsel, attr, idx);
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200246
Wang Nan03e0a7d2016-02-22 09:10:37 +0000247 if (perf_evsel__is_bpf_output(evsel)) {
Wang Nand37ba882016-04-01 13:26:42 +0000248 evsel->attr.sample_type |= (PERF_SAMPLE_RAW | PERF_SAMPLE_TIME |
249 PERF_SAMPLE_CPU | PERF_SAMPLE_PERIOD),
Wang Nan03e0a7d2016-02-22 09:10:37 +0000250 evsel->attr.sample_period = 1;
251 }
252
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200253 return evsel;
254}
255
Arnaldo Carvalho de Melo7c48dcf2016-07-28 18:33:20 -0300256struct perf_evsel *perf_evsel__new_cycles(void)
257{
258 struct perf_event_attr attr = {
259 .type = PERF_TYPE_HARDWARE,
260 .config = PERF_COUNT_HW_CPU_CYCLES,
261 };
262 struct perf_evsel *evsel;
263
264 event_attr_init(&attr);
265
266 perf_event_attr__set_max_precise_ip(&attr);
267
268 evsel = perf_evsel__new(&attr);
269 if (evsel == NULL)
270 goto out;
271
272 /* use asprintf() because free(evsel) assumes name is allocated */
273 if (asprintf(&evsel->name, "cycles%.*s",
274 attr.precise_ip ? attr.precise_ip + 1 : 0, ":ppp") < 0)
275 goto error_free;
276out:
277 return evsel;
278error_free:
279 perf_evsel__delete(evsel);
280 evsel = NULL;
281 goto out;
282}
283
Jiri Olsa8dd2a132015-09-07 10:38:06 +0200284/*
285 * Returns pointer with encoded error via <linux/err.h> interface.
286 */
Arnaldo Carvalho de Meloef503832013-11-07 16:41:19 -0300287struct perf_evsel *perf_evsel__newtp_idx(const char *sys, const char *name, int idx)
Arnaldo Carvalho de Meloefd2b922012-09-18 11:21:50 -0300288{
Arnaldo Carvalho de Meloce8ccff2014-10-09 15:29:51 -0300289 struct perf_evsel *evsel = zalloc(perf_evsel__object.size);
Jiri Olsa8dd2a132015-09-07 10:38:06 +0200290 int err = -ENOMEM;
Arnaldo Carvalho de Meloefd2b922012-09-18 11:21:50 -0300291
Jiri Olsa8dd2a132015-09-07 10:38:06 +0200292 if (evsel == NULL) {
293 goto out_err;
294 } else {
Arnaldo Carvalho de Meloefd2b922012-09-18 11:21:50 -0300295 struct perf_event_attr attr = {
Arnaldo Carvalho de Melo0b80f8b32012-09-26 12:28:26 -0300296 .type = PERF_TYPE_TRACEPOINT,
297 .sample_type = (PERF_SAMPLE_RAW | PERF_SAMPLE_TIME |
298 PERF_SAMPLE_CPU | PERF_SAMPLE_PERIOD),
Arnaldo Carvalho de Meloefd2b922012-09-18 11:21:50 -0300299 };
300
Arnaldo Carvalho de Meloe48ffe22012-09-26 17:11:38 -0300301 if (asprintf(&evsel->name, "%s:%s", sys, name) < 0)
302 goto out_free;
303
Jiri Olsa97978b32013-12-03 14:09:24 +0100304 evsel->tp_format = trace_event__tp_format(sys, name);
Jiri Olsa8dd2a132015-09-07 10:38:06 +0200305 if (IS_ERR(evsel->tp_format)) {
306 err = PTR_ERR(evsel->tp_format);
Arnaldo Carvalho de Meloefd2b922012-09-18 11:21:50 -0300307 goto out_free;
Jiri Olsa8dd2a132015-09-07 10:38:06 +0200308 }
Arnaldo Carvalho de Meloefd2b922012-09-18 11:21:50 -0300309
Arnaldo Carvalho de Melo0b80f8b32012-09-26 12:28:26 -0300310 event_attr_init(&attr);
Arnaldo Carvalho de Meloefd2b922012-09-18 11:21:50 -0300311 attr.config = evsel->tp_format->id;
Arnaldo Carvalho de Melo0b80f8b32012-09-26 12:28:26 -0300312 attr.sample_period = 1;
Arnaldo Carvalho de Meloefd2b922012-09-18 11:21:50 -0300313 perf_evsel__init(evsel, &attr, idx);
Arnaldo Carvalho de Meloefd2b922012-09-18 11:21:50 -0300314 }
315
316 return evsel;
317
318out_free:
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -0300319 zfree(&evsel->name);
Arnaldo Carvalho de Meloefd2b922012-09-18 11:21:50 -0300320 free(evsel);
Jiri Olsa8dd2a132015-09-07 10:38:06 +0200321out_err:
322 return ERR_PTR(err);
Arnaldo Carvalho de Meloefd2b922012-09-18 11:21:50 -0300323}
324
Arnaldo Carvalho de Melo8ad70132012-09-06 13:11:18 -0300325const char *perf_evsel__hw_names[PERF_COUNT_HW_MAX] = {
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300326 "cycles",
327 "instructions",
328 "cache-references",
329 "cache-misses",
330 "branches",
331 "branch-misses",
332 "bus-cycles",
333 "stalled-cycles-frontend",
334 "stalled-cycles-backend",
335 "ref-cycles",
336};
337
Arnaldo Carvalho de Melodd4f5222012-06-13 15:52:42 -0300338static const char *__perf_evsel__hw_name(u64 config)
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300339{
340 if (config < PERF_COUNT_HW_MAX && perf_evsel__hw_names[config])
341 return perf_evsel__hw_names[config];
342
343 return "unknown-hardware";
344}
345
Arnaldo Carvalho de Melo27f18612012-06-11 13:33:09 -0300346static int perf_evsel__add_modifiers(struct perf_evsel *evsel, char *bf, size_t size)
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300347{
Arnaldo Carvalho de Melo27f18612012-06-11 13:33:09 -0300348 int colon = 0, r = 0;
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300349 struct perf_event_attr *attr = &evsel->attr;
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300350 bool exclude_guest_default = false;
351
352#define MOD_PRINT(context, mod) do { \
353 if (!attr->exclude_##context) { \
Arnaldo Carvalho de Melo27f18612012-06-11 13:33:09 -0300354 if (!colon) colon = ++r; \
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300355 r += scnprintf(bf + r, size - r, "%c", mod); \
356 } } while(0)
357
358 if (attr->exclude_kernel || attr->exclude_user || attr->exclude_hv) {
359 MOD_PRINT(kernel, 'k');
360 MOD_PRINT(user, 'u');
361 MOD_PRINT(hv, 'h');
362 exclude_guest_default = true;
363 }
364
365 if (attr->precise_ip) {
366 if (!colon)
Arnaldo Carvalho de Melo27f18612012-06-11 13:33:09 -0300367 colon = ++r;
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300368 r += scnprintf(bf + r, size - r, "%.*s", attr->precise_ip, "ppp");
369 exclude_guest_default = true;
370 }
371
372 if (attr->exclude_host || attr->exclude_guest == exclude_guest_default) {
373 MOD_PRINT(host, 'H');
374 MOD_PRINT(guest, 'G');
375 }
376#undef MOD_PRINT
377 if (colon)
Arnaldo Carvalho de Melo27f18612012-06-11 13:33:09 -0300378 bf[colon - 1] = ':';
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300379 return r;
380}
381
Arnaldo Carvalho de Melo27f18612012-06-11 13:33:09 -0300382static int perf_evsel__hw_name(struct perf_evsel *evsel, char *bf, size_t size)
383{
384 int r = scnprintf(bf, size, "%s", __perf_evsel__hw_name(evsel->attr.config));
385 return r + perf_evsel__add_modifiers(evsel, bf + r, size - r);
386}
387
Arnaldo Carvalho de Melo8ad70132012-09-06 13:11:18 -0300388const char *perf_evsel__sw_names[PERF_COUNT_SW_MAX] = {
Arnaldo Carvalho de Melo335c2f52012-06-11 14:36:20 -0300389 "cpu-clock",
390 "task-clock",
391 "page-faults",
392 "context-switches",
Arnaldo Carvalho de Melo8ad70132012-09-06 13:11:18 -0300393 "cpu-migrations",
Arnaldo Carvalho de Melo335c2f52012-06-11 14:36:20 -0300394 "minor-faults",
395 "major-faults",
396 "alignment-faults",
397 "emulation-faults",
Adrian Hunterd22d1a22013-08-31 21:50:52 +0300398 "dummy",
Arnaldo Carvalho de Melo335c2f52012-06-11 14:36:20 -0300399};
400
Arnaldo Carvalho de Melodd4f5222012-06-13 15:52:42 -0300401static const char *__perf_evsel__sw_name(u64 config)
Arnaldo Carvalho de Melo335c2f52012-06-11 14:36:20 -0300402{
403 if (config < PERF_COUNT_SW_MAX && perf_evsel__sw_names[config])
404 return perf_evsel__sw_names[config];
405 return "unknown-software";
406}
407
408static int perf_evsel__sw_name(struct perf_evsel *evsel, char *bf, size_t size)
409{
410 int r = scnprintf(bf, size, "%s", __perf_evsel__sw_name(evsel->attr.config));
411 return r + perf_evsel__add_modifiers(evsel, bf + r, size - r);
412}
413
Jiri Olsa287e74a2012-06-28 23:18:49 +0200414static int __perf_evsel__bp_name(char *bf, size_t size, u64 addr, u64 type)
415{
416 int r;
417
418 r = scnprintf(bf, size, "mem:0x%" PRIx64 ":", addr);
419
420 if (type & HW_BREAKPOINT_R)
421 r += scnprintf(bf + r, size - r, "r");
422
423 if (type & HW_BREAKPOINT_W)
424 r += scnprintf(bf + r, size - r, "w");
425
426 if (type & HW_BREAKPOINT_X)
427 r += scnprintf(bf + r, size - r, "x");
428
429 return r;
430}
431
432static int perf_evsel__bp_name(struct perf_evsel *evsel, char *bf, size_t size)
433{
434 struct perf_event_attr *attr = &evsel->attr;
435 int r = __perf_evsel__bp_name(bf, size, attr->bp_addr, attr->bp_type);
436 return r + perf_evsel__add_modifiers(evsel, bf + r, size - r);
437}
438
Arnaldo Carvalho de Melo0b668bc2012-06-11 14:08:07 -0300439const char *perf_evsel__hw_cache[PERF_COUNT_HW_CACHE_MAX]
440 [PERF_EVSEL__MAX_ALIASES] = {
441 { "L1-dcache", "l1-d", "l1d", "L1-data", },
442 { "L1-icache", "l1-i", "l1i", "L1-instruction", },
443 { "LLC", "L2", },
444 { "dTLB", "d-tlb", "Data-TLB", },
445 { "iTLB", "i-tlb", "Instruction-TLB", },
446 { "branch", "branches", "bpu", "btb", "bpc", },
447 { "node", },
448};
449
450const char *perf_evsel__hw_cache_op[PERF_COUNT_HW_CACHE_OP_MAX]
451 [PERF_EVSEL__MAX_ALIASES] = {
452 { "load", "loads", "read", },
453 { "store", "stores", "write", },
454 { "prefetch", "prefetches", "speculative-read", "speculative-load", },
455};
456
457const char *perf_evsel__hw_cache_result[PERF_COUNT_HW_CACHE_RESULT_MAX]
458 [PERF_EVSEL__MAX_ALIASES] = {
459 { "refs", "Reference", "ops", "access", },
460 { "misses", "miss", },
461};
462
463#define C(x) PERF_COUNT_HW_CACHE_##x
464#define CACHE_READ (1 << C(OP_READ))
465#define CACHE_WRITE (1 << C(OP_WRITE))
466#define CACHE_PREFETCH (1 << C(OP_PREFETCH))
467#define COP(x) (1 << x)
468
469/*
470 * cache operartion stat
471 * L1I : Read and prefetch only
472 * ITLB and BPU : Read-only
473 */
474static unsigned long perf_evsel__hw_cache_stat[C(MAX)] = {
475 [C(L1D)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
476 [C(L1I)] = (CACHE_READ | CACHE_PREFETCH),
477 [C(LL)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
478 [C(DTLB)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
479 [C(ITLB)] = (CACHE_READ),
480 [C(BPU)] = (CACHE_READ),
481 [C(NODE)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
482};
483
484bool perf_evsel__is_cache_op_valid(u8 type, u8 op)
485{
486 if (perf_evsel__hw_cache_stat[type] & COP(op))
487 return true; /* valid */
488 else
489 return false; /* invalid */
490}
491
492int __perf_evsel__hw_cache_type_op_res_name(u8 type, u8 op, u8 result,
493 char *bf, size_t size)
494{
495 if (result) {
496 return scnprintf(bf, size, "%s-%s-%s", perf_evsel__hw_cache[type][0],
497 perf_evsel__hw_cache_op[op][0],
498 perf_evsel__hw_cache_result[result][0]);
499 }
500
501 return scnprintf(bf, size, "%s-%s", perf_evsel__hw_cache[type][0],
502 perf_evsel__hw_cache_op[op][1]);
503}
504
Arnaldo Carvalho de Melodd4f5222012-06-13 15:52:42 -0300505static int __perf_evsel__hw_cache_name(u64 config, char *bf, size_t size)
Arnaldo Carvalho de Melo0b668bc2012-06-11 14:08:07 -0300506{
507 u8 op, result, type = (config >> 0) & 0xff;
508 const char *err = "unknown-ext-hardware-cache-type";
509
510 if (type > PERF_COUNT_HW_CACHE_MAX)
511 goto out_err;
512
513 op = (config >> 8) & 0xff;
514 err = "unknown-ext-hardware-cache-op";
515 if (op > PERF_COUNT_HW_CACHE_OP_MAX)
516 goto out_err;
517
518 result = (config >> 16) & 0xff;
519 err = "unknown-ext-hardware-cache-result";
520 if (result > PERF_COUNT_HW_CACHE_RESULT_MAX)
521 goto out_err;
522
523 err = "invalid-cache";
524 if (!perf_evsel__is_cache_op_valid(type, op))
525 goto out_err;
526
527 return __perf_evsel__hw_cache_type_op_res_name(type, op, result, bf, size);
528out_err:
529 return scnprintf(bf, size, "%s", err);
530}
531
532static int perf_evsel__hw_cache_name(struct perf_evsel *evsel, char *bf, size_t size)
533{
534 int ret = __perf_evsel__hw_cache_name(evsel->attr.config, bf, size);
535 return ret + perf_evsel__add_modifiers(evsel, bf + ret, size - ret);
536}
537
Arnaldo Carvalho de Melo6eef3d92012-06-13 11:53:37 -0300538static int perf_evsel__raw_name(struct perf_evsel *evsel, char *bf, size_t size)
539{
540 int ret = scnprintf(bf, size, "raw 0x%" PRIx64, evsel->attr.config);
541 return ret + perf_evsel__add_modifiers(evsel, bf + ret, size - ret);
542}
543
Arnaldo Carvalho de Melo7289f832012-06-12 12:34:58 -0300544const char *perf_evsel__name(struct perf_evsel *evsel)
Arnaldo Carvalho de Meloa4460832012-06-12 10:29:12 -0300545{
Arnaldo Carvalho de Melo7289f832012-06-12 12:34:58 -0300546 char bf[128];
Arnaldo Carvalho de Meloa4460832012-06-12 10:29:12 -0300547
Arnaldo Carvalho de Melo7289f832012-06-12 12:34:58 -0300548 if (evsel->name)
549 return evsel->name;
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300550
551 switch (evsel->attr.type) {
552 case PERF_TYPE_RAW:
Arnaldo Carvalho de Melo6eef3d92012-06-13 11:53:37 -0300553 perf_evsel__raw_name(evsel, bf, sizeof(bf));
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300554 break;
555
556 case PERF_TYPE_HARDWARE:
Arnaldo Carvalho de Melo7289f832012-06-12 12:34:58 -0300557 perf_evsel__hw_name(evsel, bf, sizeof(bf));
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300558 break;
Arnaldo Carvalho de Melo0b668bc2012-06-11 14:08:07 -0300559
560 case PERF_TYPE_HW_CACHE:
Arnaldo Carvalho de Melo7289f832012-06-12 12:34:58 -0300561 perf_evsel__hw_cache_name(evsel, bf, sizeof(bf));
Arnaldo Carvalho de Melo0b668bc2012-06-11 14:08:07 -0300562 break;
563
Arnaldo Carvalho de Melo335c2f52012-06-11 14:36:20 -0300564 case PERF_TYPE_SOFTWARE:
Arnaldo Carvalho de Melo7289f832012-06-12 12:34:58 -0300565 perf_evsel__sw_name(evsel, bf, sizeof(bf));
Arnaldo Carvalho de Melo335c2f52012-06-11 14:36:20 -0300566 break;
567
Arnaldo Carvalho de Meloa4460832012-06-12 10:29:12 -0300568 case PERF_TYPE_TRACEPOINT:
Arnaldo Carvalho de Melo7289f832012-06-12 12:34:58 -0300569 scnprintf(bf, sizeof(bf), "%s", "unknown tracepoint");
Arnaldo Carvalho de Meloa4460832012-06-12 10:29:12 -0300570 break;
571
Jiri Olsa287e74a2012-06-28 23:18:49 +0200572 case PERF_TYPE_BREAKPOINT:
573 perf_evsel__bp_name(evsel, bf, sizeof(bf));
574 break;
575
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300576 default:
Robert Richterca1b1452012-08-16 21:10:18 +0200577 scnprintf(bf, sizeof(bf), "unknown attr type: %d",
578 evsel->attr.type);
Arnaldo Carvalho de Meloa4460832012-06-12 10:29:12 -0300579 break;
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300580 }
581
Arnaldo Carvalho de Melo7289f832012-06-12 12:34:58 -0300582 evsel->name = strdup(bf);
583
584 return evsel->name ?: "unknown";
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300585}
586
Namhyung Kim717e2632013-01-22 18:09:44 +0900587const char *perf_evsel__group_name(struct perf_evsel *evsel)
588{
589 return evsel->group_name ?: "anon group";
590}
591
592int perf_evsel__group_desc(struct perf_evsel *evsel, char *buf, size_t size)
593{
594 int ret;
595 struct perf_evsel *pos;
596 const char *group_name = perf_evsel__group_name(evsel);
597
598 ret = scnprintf(buf, size, "%s", group_name);
599
600 ret += scnprintf(buf + ret, size - ret, " { %s",
601 perf_evsel__name(evsel));
602
603 for_each_group_member(pos, evsel)
604 ret += scnprintf(buf + ret, size - ret, ", %s",
605 perf_evsel__name(pos));
606
607 ret += scnprintf(buf + ret, size - ret, " }");
608
609 return ret;
610}
611
Arnaldo Carvalho de Melo01e0d502016-04-11 18:39:37 -0300612void perf_evsel__config_callchain(struct perf_evsel *evsel,
613 struct record_opts *opts,
614 struct callchain_param *param)
Jiri Olsa6bedfab2014-03-02 16:56:40 +0100615{
616 bool function = perf_evsel__is_function_event(evsel);
617 struct perf_event_attr *attr = &evsel->attr;
618
619 perf_evsel__set_sample_bit(evsel, CALLCHAIN);
620
Arnaldo Carvalho de Melo792d48b2016-04-28 19:03:42 -0300621 attr->sample_max_stack = param->max_stack;
622
Kan Liangc3a6a8c2015-08-04 04:30:20 -0400623 if (param->record_mode == CALLCHAIN_LBR) {
Kan Liangaad2b212015-01-05 13:23:04 -0500624 if (!opts->branch_stack) {
625 if (attr->exclude_user) {
626 pr_warning("LBR callstack option is only available "
627 "to get user callchain information. "
628 "Falling back to framepointers.\n");
629 } else {
630 perf_evsel__set_sample_bit(evsel, BRANCH_STACK);
631 attr->branch_sample_type = PERF_SAMPLE_BRANCH_USER |
Andi Kleenbd0f8892015-12-11 16:12:24 -0800632 PERF_SAMPLE_BRANCH_CALL_STACK |
633 PERF_SAMPLE_BRANCH_NO_CYCLES |
634 PERF_SAMPLE_BRANCH_NO_FLAGS;
Kan Liangaad2b212015-01-05 13:23:04 -0500635 }
636 } else
637 pr_warning("Cannot use LBR callstack with branch stack. "
638 "Falling back to framepointers.\n");
639 }
640
Kan Liangc3a6a8c2015-08-04 04:30:20 -0400641 if (param->record_mode == CALLCHAIN_DWARF) {
Jiri Olsa6bedfab2014-03-02 16:56:40 +0100642 if (!function) {
643 perf_evsel__set_sample_bit(evsel, REGS_USER);
644 perf_evsel__set_sample_bit(evsel, STACK_USER);
645 attr->sample_regs_user = PERF_REGS_MASK;
Kan Liangc3a6a8c2015-08-04 04:30:20 -0400646 attr->sample_stack_user = param->dump_size;
Jiri Olsa6bedfab2014-03-02 16:56:40 +0100647 attr->exclude_callchain_user = 1;
648 } else {
649 pr_info("Cannot use DWARF unwind for function trace event,"
650 " falling back to framepointers.\n");
651 }
652 }
653
654 if (function) {
655 pr_info("Disabling user space callchains for function trace event.\n");
656 attr->exclude_callchain_user = 1;
657 }
658}
659
Kan Liangd457c962015-08-11 06:30:47 -0400660static void
661perf_evsel__reset_callgraph(struct perf_evsel *evsel,
662 struct callchain_param *param)
663{
664 struct perf_event_attr *attr = &evsel->attr;
665
666 perf_evsel__reset_sample_bit(evsel, CALLCHAIN);
667 if (param->record_mode == CALLCHAIN_LBR) {
668 perf_evsel__reset_sample_bit(evsel, BRANCH_STACK);
669 attr->branch_sample_type &= ~(PERF_SAMPLE_BRANCH_USER |
670 PERF_SAMPLE_BRANCH_CALL_STACK);
671 }
672 if (param->record_mode == CALLCHAIN_DWARF) {
673 perf_evsel__reset_sample_bit(evsel, REGS_USER);
674 perf_evsel__reset_sample_bit(evsel, STACK_USER);
675 }
676}
677
678static void apply_config_terms(struct perf_evsel *evsel,
679 struct record_opts *opts)
Jiri Olsa930a2e22015-07-29 05:42:10 -0400680{
681 struct perf_evsel_config_term *term;
Kan Liang32067712015-08-04 04:30:19 -0400682 struct list_head *config_terms = &evsel->config_terms;
683 struct perf_event_attr *attr = &evsel->attr;
Kan Liangd457c962015-08-11 06:30:47 -0400684 struct callchain_param param;
685 u32 dump_size = 0;
Arnaldo Carvalho de Melo792d48b2016-04-28 19:03:42 -0300686 int max_stack = 0;
687 const char *callgraph_buf = NULL;
Kan Liangd457c962015-08-11 06:30:47 -0400688
689 /* callgraph default */
690 param.record_mode = callchain_param.record_mode;
Jiri Olsa930a2e22015-07-29 05:42:10 -0400691
692 list_for_each_entry(term, config_terms, list) {
693 switch (term->type) {
Jiri Olsaee4c7582015-07-29 05:42:11 -0400694 case PERF_EVSEL__CONFIG_TERM_PERIOD:
695 attr->sample_period = term->val.period;
Jiri Olsaab35a7d2015-08-08 19:12:10 +0200696 attr->freq = 0;
Kan Liang32067712015-08-04 04:30:19 -0400697 break;
Namhyung Kim09af2a52015-08-09 15:45:23 +0900698 case PERF_EVSEL__CONFIG_TERM_FREQ:
699 attr->sample_freq = term->val.freq;
700 attr->freq = 1;
701 break;
Kan Liang32067712015-08-04 04:30:19 -0400702 case PERF_EVSEL__CONFIG_TERM_TIME:
703 if (term->val.time)
704 perf_evsel__set_sample_bit(evsel, TIME);
705 else
706 perf_evsel__reset_sample_bit(evsel, TIME);
707 break;
Kan Liangd457c962015-08-11 06:30:47 -0400708 case PERF_EVSEL__CONFIG_TERM_CALLGRAPH:
709 callgraph_buf = term->val.callgraph;
710 break;
711 case PERF_EVSEL__CONFIG_TERM_STACK_USER:
712 dump_size = term->val.stack_user;
713 break;
Arnaldo Carvalho de Melo792d48b2016-04-28 19:03:42 -0300714 case PERF_EVSEL__CONFIG_TERM_MAX_STACK:
715 max_stack = term->val.max_stack;
716 break;
Wang Nan374ce932015-10-28 10:55:02 +0000717 case PERF_EVSEL__CONFIG_TERM_INHERIT:
718 /*
719 * attr->inherit should has already been set by
720 * perf_evsel__config. If user explicitly set
721 * inherit using config terms, override global
722 * opt->no_inherit setting.
723 */
724 attr->inherit = term->val.inherit ? 1 : 0;
725 break;
Wang Nan626a6b72016-07-14 08:34:45 +0000726 case PERF_EVSEL__CONFIG_TERM_OVERWRITE:
727 attr->write_backward = term->val.overwrite ? 1 : 0;
728 break;
Jiri Olsa930a2e22015-07-29 05:42:10 -0400729 default:
730 break;
731 }
732 }
Kan Liangd457c962015-08-11 06:30:47 -0400733
734 /* User explicitly set per-event callgraph, clear the old setting and reset. */
Arnaldo Carvalho de Melo792d48b2016-04-28 19:03:42 -0300735 if ((callgraph_buf != NULL) || (dump_size > 0) || max_stack) {
736 if (max_stack) {
737 param.max_stack = max_stack;
738 if (callgraph_buf == NULL)
739 callgraph_buf = "fp";
740 }
Kan Liangd457c962015-08-11 06:30:47 -0400741
742 /* parse callgraph parameters */
743 if (callgraph_buf != NULL) {
Kan Liangf9db0d02015-08-11 06:30:48 -0400744 if (!strcmp(callgraph_buf, "no")) {
745 param.enabled = false;
746 param.record_mode = CALLCHAIN_NONE;
747 } else {
748 param.enabled = true;
749 if (parse_callchain_record(callgraph_buf, &param)) {
750 pr_err("per-event callgraph setting for %s failed. "
751 "Apply callgraph global setting for it\n",
752 evsel->name);
753 return;
754 }
Kan Liangd457c962015-08-11 06:30:47 -0400755 }
756 }
757 if (dump_size > 0) {
758 dump_size = round_up(dump_size, sizeof(u64));
759 param.dump_size = dump_size;
760 }
761
762 /* If global callgraph set, clear it */
763 if (callchain_param.enabled)
764 perf_evsel__reset_callgraph(evsel, &callchain_param);
765
766 /* set perf-event callgraph */
767 if (param.enabled)
Arnaldo Carvalho de Melo01e0d502016-04-11 18:39:37 -0300768 perf_evsel__config_callchain(evsel, opts, &param);
Kan Liangd457c962015-08-11 06:30:47 -0400769 }
Jiri Olsa930a2e22015-07-29 05:42:10 -0400770}
771
Jiri Olsa774cb492012-11-12 18:34:01 +0100772/*
773 * The enable_on_exec/disabled value strategy:
774 *
775 * 1) For any type of traced program:
776 * - all independent events and group leaders are disabled
777 * - all group members are enabled
778 *
779 * Group members are ruled by group leaders. They need to
780 * be enabled, because the group scheduling relies on that.
781 *
782 * 2) For traced programs executed by perf:
783 * - all independent events and group leaders have
784 * enable_on_exec set
785 * - we don't specifically enable or disable any event during
786 * the record command
787 *
788 * Independent events and group leaders are initially disabled
789 * and get enabled by exec. Group members are ruled by group
790 * leaders as stated in 1).
791 *
792 * 3) For traced programs attached by perf (pid/tid):
793 * - we specifically enable or disable all events during
794 * the record command
795 *
796 * When attaching events to already running traced we
797 * enable/disable events specifically, as there's no
798 * initial traced exec call.
799 */
Arnaldo Carvalho de Meloe68ae9c2016-04-11 18:15:29 -0300800void perf_evsel__config(struct perf_evsel *evsel, struct record_opts *opts,
801 struct callchain_param *callchain)
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200802{
Jiri Olsa3c176312012-10-10 17:39:03 +0200803 struct perf_evsel *leader = evsel->leader;
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200804 struct perf_event_attr *attr = &evsel->attr;
Adrian Hunter60b08962014-07-31 09:00:52 +0300805 int track = evsel->tracking;
Adrian Hunter3aa59392013-11-15 15:52:29 +0200806 bool per_cpu = opts->target.default_per_cpu && !opts->target.per_thread;
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200807
Arnaldo Carvalho de Melo594ac612012-12-13 13:13:07 -0300808 attr->sample_id_all = perf_missing_features.sample_id_all ? 0 : 1;
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200809 attr->inherit = !opts->no_inherit;
Wang Nan626a6b72016-07-14 08:34:45 +0000810 attr->write_backward = opts->overwrite ? 1 : 0;
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200811
Arnaldo Carvalho de Melo7be5ebe2012-12-10 14:53:43 -0300812 perf_evsel__set_sample_bit(evsel, IP);
813 perf_evsel__set_sample_bit(evsel, TID);
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200814
Jiri Olsa3c176312012-10-10 17:39:03 +0200815 if (evsel->sample_read) {
816 perf_evsel__set_sample_bit(evsel, READ);
817
818 /*
819 * We need ID even in case of single event, because
820 * PERF_SAMPLE_READ process ID specific data.
821 */
Adrian Hunter75562572013-08-27 11:23:09 +0300822 perf_evsel__set_sample_id(evsel, false);
Jiri Olsa3c176312012-10-10 17:39:03 +0200823
824 /*
825 * Apply group format only if we belong to group
826 * with more than one members.
827 */
828 if (leader->nr_members > 1) {
829 attr->read_format |= PERF_FORMAT_GROUP;
830 attr->inherit = 0;
831 }
832 }
833
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200834 /*
Namhyung Kim17314e22014-06-09 14:43:37 +0900835 * We default some events to have a default interval. But keep
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200836 * it a weak assumption overridable by the user.
837 */
Namhyung Kim17314e22014-06-09 14:43:37 +0900838 if (!attr->sample_period || (opts->user_freq != UINT_MAX ||
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200839 opts->user_interval != ULLONG_MAX)) {
840 if (opts->freq) {
Arnaldo Carvalho de Melo7be5ebe2012-12-10 14:53:43 -0300841 perf_evsel__set_sample_bit(evsel, PERIOD);
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200842 attr->freq = 1;
843 attr->sample_freq = opts->freq;
844 } else {
845 attr->sample_period = opts->default_interval;
846 }
847 }
848
Jiri Olsa3c176312012-10-10 17:39:03 +0200849 /*
850 * Disable sampling for all group members other
851 * than leader in case leader 'leads' the sampling.
852 */
853 if ((leader != evsel) && leader->sample_read) {
854 attr->sample_freq = 0;
855 attr->sample_period = 0;
856 }
857
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200858 if (opts->no_samples)
859 attr->sample_freq = 0;
860
861 if (opts->inherit_stat)
862 attr->inherit_stat = 1;
863
864 if (opts->sample_address) {
Arnaldo Carvalho de Melo7be5ebe2012-12-10 14:53:43 -0300865 perf_evsel__set_sample_bit(evsel, ADDR);
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200866 attr->mmap_data = track;
867 }
868
Jiri Olsaf1403732014-11-13 18:21:03 +0100869 /*
870 * We don't allow user space callchains for function trace
871 * event, due to issues with page faults while tracing page
872 * fault handler and its overall trickiness nature.
873 */
874 if (perf_evsel__is_function_event(evsel))
875 evsel->attr.exclude_callchain_user = 1;
876
Arnaldo Carvalho de Meloe68ae9c2016-04-11 18:15:29 -0300877 if (callchain && callchain->enabled && !evsel->no_aux_samples)
Arnaldo Carvalho de Melo01e0d502016-04-11 18:39:37 -0300878 perf_evsel__config_callchain(evsel, opts, callchain);
Jiri Olsa26d33022012-08-07 15:20:47 +0200879
Stephane Eranian6a21c0b2014-09-24 13:48:39 +0200880 if (opts->sample_intr_regs) {
Stephane Eranianbcc84ec2015-08-31 18:41:12 +0200881 attr->sample_regs_intr = opts->sample_intr_regs;
Stephane Eranian6a21c0b2014-09-24 13:48:39 +0200882 perf_evsel__set_sample_bit(evsel, REGS_INTR);
883 }
884
Jiri Olsab6f35ed2016-08-01 20:02:35 +0200885 if (target__has_cpu(&opts->target) || opts->sample_cpu)
Arnaldo Carvalho de Melo7be5ebe2012-12-10 14:53:43 -0300886 perf_evsel__set_sample_bit(evsel, CPU);
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200887
Andrew Vagin3e76ac72011-12-20 17:32:45 +0300888 if (opts->period)
Arnaldo Carvalho de Melo7be5ebe2012-12-10 14:53:43 -0300889 perf_evsel__set_sample_bit(evsel, PERIOD);
Andrew Vagin3e76ac72011-12-20 17:32:45 +0300890
Andi Kleen8affc2b2014-07-31 14:45:04 +0800891 /*
Adam Buchbinderbd1a0be2016-02-24 10:02:25 -0800892 * When the user explicitly disabled time don't force it here.
Andi Kleen8affc2b2014-07-31 14:45:04 +0800893 */
894 if (opts->sample_time &&
895 (!perf_missing_features.sample_id_all &&
Adrian Hunter3abebc52015-07-06 14:51:01 +0300896 (!opts->no_inherit || target__has_cpu(&opts->target) || per_cpu ||
897 opts->sample_time_set)))
Arnaldo Carvalho de Melo7be5ebe2012-12-10 14:53:43 -0300898 perf_evsel__set_sample_bit(evsel, TIME);
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200899
Adrian Hunter6ff1ce72014-07-14 13:02:56 +0300900 if (opts->raw_samples && !evsel->no_aux_samples) {
Arnaldo Carvalho de Melo7be5ebe2012-12-10 14:53:43 -0300901 perf_evsel__set_sample_bit(evsel, TIME);
902 perf_evsel__set_sample_bit(evsel, RAW);
903 perf_evsel__set_sample_bit(evsel, CPU);
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200904 }
905
Stephane Eranianccf49bf2013-01-24 16:10:37 +0100906 if (opts->sample_address)
Adrian Hunter1e7ed5e2013-11-01 15:51:35 +0200907 perf_evsel__set_sample_bit(evsel, DATA_SRC);
Stephane Eranianccf49bf2013-01-24 16:10:37 +0100908
Arnaldo Carvalho de Melo509051e2014-01-14 17:52:14 -0300909 if (opts->no_buffering) {
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200910 attr->watermark = 0;
911 attr->wakeup_events = 1;
912 }
Adrian Hunter6ff1ce72014-07-14 13:02:56 +0300913 if (opts->branch_stack && !evsel->no_aux_samples) {
Arnaldo Carvalho de Melo7be5ebe2012-12-10 14:53:43 -0300914 perf_evsel__set_sample_bit(evsel, BRANCH_STACK);
Roberto Agostino Vitillobdfebd82012-02-09 23:21:02 +0100915 attr->branch_sample_type = opts->branch_stack;
916 }
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200917
Andi Kleen05484292013-01-24 16:10:29 +0100918 if (opts->sample_weight)
Adrian Hunter1e7ed5e2013-11-01 15:51:35 +0200919 perf_evsel__set_sample_bit(evsel, WEIGHT);
Andi Kleen05484292013-01-24 16:10:29 +0100920
Namhyung Kim62e503b2015-01-29 17:06:46 +0900921 attr->task = track;
Stephane Eranian5c5e8542013-08-21 12:10:25 +0200922 attr->mmap = track;
Don Zickusa5a5ba72014-05-30 10:49:42 -0400923 attr->mmap2 = track && !perf_missing_features.mmap2;
Stephane Eranian5c5e8542013-08-21 12:10:25 +0200924 attr->comm = track;
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200925
Adrian Hunterb757bb02015-07-21 12:44:04 +0300926 if (opts->record_switch_events)
927 attr->context_switch = track;
928
Andi Kleen475eeab2013-09-20 07:40:43 -0700929 if (opts->sample_transaction)
Adrian Hunter1e7ed5e2013-11-01 15:51:35 +0200930 perf_evsel__set_sample_bit(evsel, TRANSACTION);
Andi Kleen475eeab2013-09-20 07:40:43 -0700931
Andi Kleen85c273d2015-02-24 15:13:40 -0800932 if (opts->running_time) {
933 evsel->attr.read_format |=
934 PERF_FORMAT_TOTAL_TIME_ENABLED |
935 PERF_FORMAT_TOTAL_TIME_RUNNING;
936 }
937
Jiri Olsa774cb492012-11-12 18:34:01 +0100938 /*
939 * XXX see the function comment above
940 *
941 * Disabling only independent events or group leaders,
942 * keeping group members enabled.
943 */
Namhyung Kim823254e2012-11-29 15:38:30 +0900944 if (perf_evsel__is_group_leader(evsel))
Jiri Olsa774cb492012-11-12 18:34:01 +0100945 attr->disabled = 1;
946
947 /*
948 * Setting enable_on_exec for independent events and
949 * group leaders for traced executed by perf.
950 */
Andi Kleen6619a532014-01-11 13:38:27 -0800951 if (target__none(&opts->target) && perf_evsel__is_group_leader(evsel) &&
952 !opts->initial_delay)
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200953 attr->enable_on_exec = 1;
Adrian Hunter2afd2bc2014-07-14 13:02:57 +0300954
955 if (evsel->immediate) {
956 attr->disabled = 0;
957 attr->enable_on_exec = 0;
958 }
Peter Zijlstra814c8c32015-03-31 00:19:31 +0200959
960 clockid = opts->clockid;
961 if (opts->use_clockid) {
962 attr->use_clockid = 1;
963 attr->clockid = opts->clockid;
964 }
Jiri Olsa930a2e22015-07-29 05:42:10 -0400965
Jiri Olsa7f94af72015-10-05 20:06:05 +0200966 if (evsel->precise_max)
967 perf_event_attr__set_max_precise_ip(attr);
968
Jiri Olsa85723882016-02-15 09:34:31 +0100969 if (opts->all_user) {
970 attr->exclude_kernel = 1;
971 attr->exclude_user = 0;
972 }
973
974 if (opts->all_kernel) {
975 attr->exclude_kernel = 0;
976 attr->exclude_user = 1;
977 }
978
Jiri Olsa930a2e22015-07-29 05:42:10 -0400979 /*
980 * Apply event specific term settings,
981 * it overloads any global configuration.
982 */
Kan Liangd457c962015-08-11 06:30:47 -0400983 apply_config_terms(evsel, opts);
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200984}
985
Arnaldo Carvalho de Melo88858462014-10-13 13:30:27 -0300986static int perf_evsel__alloc_fd(struct perf_evsel *evsel, int ncpus, int nthreads)
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200987{
David Ahern4af4c952011-05-27 09:58:34 -0600988 int cpu, thread;
Adrian Hunterbf8e8f42014-07-31 09:00:51 +0300989
990 if (evsel->system_wide)
991 nthreads = 1;
992
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200993 evsel->fd = xyarray__new(ncpus, nthreads, sizeof(int));
David Ahern4af4c952011-05-27 09:58:34 -0600994
995 if (evsel->fd) {
996 for (cpu = 0; cpu < ncpus; cpu++) {
997 for (thread = 0; thread < nthreads; thread++) {
998 FD(evsel, cpu, thread) = -1;
999 }
1000 }
1001 }
1002
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -02001003 return evsel->fd != NULL ? 0 : -ENOMEM;
1004}
1005
Andi Kleene2407be2013-08-02 17:41:10 -07001006static int perf_evsel__run_ioctl(struct perf_evsel *evsel, int ncpus, int nthreads,
1007 int ioc, void *arg)
Arnaldo Carvalho de Melo745cefc2012-09-26 15:07:39 -03001008{
1009 int cpu, thread;
1010
Adrian Hunterbf8e8f42014-07-31 09:00:51 +03001011 if (evsel->system_wide)
1012 nthreads = 1;
1013
Arnaldo Carvalho de Melo745cefc2012-09-26 15:07:39 -03001014 for (cpu = 0; cpu < ncpus; cpu++) {
1015 for (thread = 0; thread < nthreads; thread++) {
1016 int fd = FD(evsel, cpu, thread),
Andi Kleene2407be2013-08-02 17:41:10 -07001017 err = ioctl(fd, ioc, arg);
Arnaldo Carvalho de Melo745cefc2012-09-26 15:07:39 -03001018
1019 if (err)
1020 return err;
1021 }
1022 }
1023
1024 return 0;
1025}
1026
Arnaldo Carvalho de Melof47805a2015-07-03 15:53:49 -03001027int perf_evsel__apply_filter(struct perf_evsel *evsel, int ncpus, int nthreads,
1028 const char *filter)
Andi Kleene2407be2013-08-02 17:41:10 -07001029{
1030 return perf_evsel__run_ioctl(evsel, ncpus, nthreads,
1031 PERF_EVENT_IOC_SET_FILTER,
1032 (void *)filter);
1033}
1034
Arnaldo Carvalho de Melo12467ae2015-07-03 17:05:50 -03001035int perf_evsel__set_filter(struct perf_evsel *evsel, const char *filter)
1036{
1037 char *new_filter = strdup(filter);
1038
1039 if (new_filter != NULL) {
1040 free(evsel->filter);
1041 evsel->filter = new_filter;
1042 return 0;
1043 }
1044
1045 return -1;
1046}
1047
Arnaldo Carvalho de Melo64ec84f2015-07-04 12:19:13 -03001048int perf_evsel__append_filter(struct perf_evsel *evsel,
1049 const char *op, const char *filter)
1050{
1051 char *new_filter;
1052
1053 if (evsel->filter == NULL)
1054 return perf_evsel__set_filter(evsel, filter);
1055
1056 if (asprintf(&new_filter,"(%s) %s (%s)", evsel->filter, op, filter) > 0) {
1057 free(evsel->filter);
1058 evsel->filter = new_filter;
1059 return 0;
1060 }
1061
1062 return -1;
1063}
1064
Jiri Olsa5cd95fc2015-12-03 10:06:40 +01001065int perf_evsel__enable(struct perf_evsel *evsel)
Andi Kleene2407be2013-08-02 17:41:10 -07001066{
Jiri Olsa5cd95fc2015-12-03 10:06:40 +01001067 int nthreads = thread_map__nr(evsel->threads);
1068 int ncpus = cpu_map__nr(evsel->cpus);
1069
Andi Kleene2407be2013-08-02 17:41:10 -07001070 return perf_evsel__run_ioctl(evsel, ncpus, nthreads,
1071 PERF_EVENT_IOC_ENABLE,
1072 0);
1073}
1074
Jiri Olsae98a4cb2015-12-03 10:06:41 +01001075int perf_evsel__disable(struct perf_evsel *evsel)
1076{
1077 int nthreads = thread_map__nr(evsel->threads);
1078 int ncpus = cpu_map__nr(evsel->cpus);
1079
1080 return perf_evsel__run_ioctl(evsel, ncpus, nthreads,
1081 PERF_EVENT_IOC_DISABLE,
1082 0);
1083}
1084
Arnaldo Carvalho de Melo70db7532011-01-12 22:39:13 -02001085int perf_evsel__alloc_id(struct perf_evsel *evsel, int ncpus, int nthreads)
1086{
Vineet Gupta8d9cbd82015-01-13 19:13:23 +05301087 if (ncpus == 0 || nthreads == 0)
1088 return 0;
1089
Adrian Hunterbf8e8f42014-07-31 09:00:51 +03001090 if (evsel->system_wide)
1091 nthreads = 1;
1092
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03001093 evsel->sample_id = xyarray__new(ncpus, nthreads, sizeof(struct perf_sample_id));
1094 if (evsel->sample_id == NULL)
1095 return -ENOMEM;
1096
1097 evsel->id = zalloc(ncpus * nthreads * sizeof(u64));
1098 if (evsel->id == NULL) {
1099 xyarray__delete(evsel->sample_id);
1100 evsel->sample_id = NULL;
1101 return -ENOMEM;
1102 }
1103
1104 return 0;
Arnaldo Carvalho de Melo70db7532011-01-12 22:39:13 -02001105}
1106
Arnaldo Carvalho de Melo88858462014-10-13 13:30:27 -03001107static void perf_evsel__free_fd(struct perf_evsel *evsel)
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -02001108{
1109 xyarray__delete(evsel->fd);
1110 evsel->fd = NULL;
1111}
1112
Arnaldo Carvalho de Melo88858462014-10-13 13:30:27 -03001113static void perf_evsel__free_id(struct perf_evsel *evsel)
Arnaldo Carvalho de Melo70db7532011-01-12 22:39:13 -02001114{
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03001115 xyarray__delete(evsel->sample_id);
1116 evsel->sample_id = NULL;
Arnaldo Carvalho de Melo04662522013-12-26 17:41:15 -03001117 zfree(&evsel->id);
Arnaldo Carvalho de Melo70db7532011-01-12 22:39:13 -02001118}
1119
Jiri Olsa930a2e22015-07-29 05:42:10 -04001120static void perf_evsel__free_config_terms(struct perf_evsel *evsel)
1121{
1122 struct perf_evsel_config_term *term, *h;
1123
1124 list_for_each_entry_safe(term, h, &evsel->config_terms, list) {
1125 list_del(&term->list);
1126 free(term);
1127 }
1128}
1129
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -02001130void perf_evsel__close_fd(struct perf_evsel *evsel, int ncpus, int nthreads)
1131{
1132 int cpu, thread;
1133
Adrian Hunterbf8e8f42014-07-31 09:00:51 +03001134 if (evsel->system_wide)
1135 nthreads = 1;
1136
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -02001137 for (cpu = 0; cpu < ncpus; cpu++)
1138 for (thread = 0; thread < nthreads; ++thread) {
1139 close(FD(evsel, cpu, thread));
1140 FD(evsel, cpu, thread) = -1;
1141 }
1142}
1143
Arnaldo Carvalho de Meloef1d1af2011-01-18 21:41:45 -02001144void perf_evsel__exit(struct perf_evsel *evsel)
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -02001145{
1146 assert(list_empty(&evsel->node));
Arnaldo Carvalho de Melod49e4692015-08-27 08:07:40 -04001147 assert(evsel->evlist == NULL);
Namhyung Kim736b05a2013-03-15 14:48:49 +09001148 perf_evsel__free_fd(evsel);
1149 perf_evsel__free_id(evsel);
Jiri Olsa930a2e22015-07-29 05:42:10 -04001150 perf_evsel__free_config_terms(evsel);
Arnaldo Carvalho de Melo597e48c2014-10-16 13:25:01 -03001151 close_cgroup(evsel->cgrp);
Jiri Olsaf30a79b2015-06-23 00:36:04 +02001152 cpu_map__put(evsel->cpus);
Adrian Hunterfce4d292015-09-08 10:58:55 +03001153 cpu_map__put(evsel->own_cpus);
Jiri Olsa578e91e2015-06-23 00:36:07 +02001154 thread_map__put(evsel->threads);
Arnaldo Carvalho de Melo597e48c2014-10-16 13:25:01 -03001155 zfree(&evsel->group_name);
Arnaldo Carvalho de Melo597e48c2014-10-16 13:25:01 -03001156 zfree(&evsel->name);
Arnaldo Carvalho de Meloce8ccff2014-10-09 15:29:51 -03001157 perf_evsel__object.fini(evsel);
Arnaldo Carvalho de Meloef1d1af2011-01-18 21:41:45 -02001158}
1159
1160void perf_evsel__delete(struct perf_evsel *evsel)
1161{
1162 perf_evsel__exit(evsel);
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -02001163 free(evsel);
1164}
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -02001165
Jiri Olsaa6fa0032015-06-26 11:29:11 +02001166void perf_evsel__compute_deltas(struct perf_evsel *evsel, int cpu, int thread,
Jiri Olsa857a94a2014-11-21 10:31:05 +01001167 struct perf_counts_values *count)
Stephane Eranianc7a79c42013-01-29 12:47:43 +01001168{
1169 struct perf_counts_values tmp;
1170
1171 if (!evsel->prev_raw_counts)
1172 return;
1173
1174 if (cpu == -1) {
1175 tmp = evsel->prev_raw_counts->aggr;
1176 evsel->prev_raw_counts->aggr = *count;
1177 } else {
Jiri Olsaa6fa0032015-06-26 11:29:11 +02001178 tmp = *perf_counts(evsel->prev_raw_counts, cpu, thread);
1179 *perf_counts(evsel->prev_raw_counts, cpu, thread) = *count;
Stephane Eranianc7a79c42013-01-29 12:47:43 +01001180 }
1181
1182 count->val = count->val - tmp.val;
1183 count->ena = count->ena - tmp.ena;
1184 count->run = count->run - tmp.run;
1185}
1186
Jiri Olsa13112bb2014-11-21 10:31:06 +01001187void perf_counts_values__scale(struct perf_counts_values *count,
1188 bool scale, s8 *pscaled)
1189{
1190 s8 scaled = 0;
1191
1192 if (scale) {
1193 if (count->run == 0) {
1194 scaled = -1;
1195 count->val = 0;
1196 } else if (count->run < count->ena) {
1197 scaled = 1;
1198 count->val = (u64)((double) count->val * count->ena / count->run + 0.5);
1199 }
1200 } else
1201 count->ena = count->run = 0;
1202
1203 if (pscaled)
1204 *pscaled = scaled;
1205}
1206
Jiri Olsaf99f47192015-06-26 11:29:18 +02001207int perf_evsel__read(struct perf_evsel *evsel, int cpu, int thread,
1208 struct perf_counts_values *count)
1209{
1210 memset(count, 0, sizeof(*count));
1211
1212 if (FD(evsel, cpu, thread) < 0)
1213 return -EINVAL;
1214
1215 if (readn(FD(evsel, cpu, thread), count, sizeof(*count)) < 0)
1216 return -errno;
1217
1218 return 0;
1219}
1220
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -02001221int __perf_evsel__read_on_cpu(struct perf_evsel *evsel,
1222 int cpu, int thread, bool scale)
1223{
1224 struct perf_counts_values count;
1225 size_t nv = scale ? 3 : 1;
1226
1227 if (FD(evsel, cpu, thread) < 0)
1228 return -EINVAL;
1229
Jiri Olsaa6fa0032015-06-26 11:29:11 +02001230 if (evsel->counts == NULL && perf_evsel__alloc_counts(evsel, cpu + 1, thread + 1) < 0)
Arnaldo Carvalho de Melo4eed11d2011-01-04 00:13:17 -02001231 return -ENOMEM;
1232
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -02001233 if (readn(FD(evsel, cpu, thread), &count, nv * sizeof(u64)) < 0)
1234 return -errno;
1235
Jiri Olsaa6fa0032015-06-26 11:29:11 +02001236 perf_evsel__compute_deltas(evsel, cpu, thread, &count);
Jiri Olsa13112bb2014-11-21 10:31:06 +01001237 perf_counts_values__scale(&count, scale, NULL);
Jiri Olsaa6fa0032015-06-26 11:29:11 +02001238 *perf_counts(evsel->counts, cpu, thread) = count;
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -02001239 return 0;
1240}
1241
Jiri Olsa6a4bb042012-08-08 12:22:36 +02001242static int get_group_fd(struct perf_evsel *evsel, int cpu, int thread)
1243{
1244 struct perf_evsel *leader = evsel->leader;
1245 int fd;
1246
Namhyung Kim823254e2012-11-29 15:38:30 +09001247 if (perf_evsel__is_group_leader(evsel))
Jiri Olsa6a4bb042012-08-08 12:22:36 +02001248 return -1;
1249
1250 /*
1251 * Leader must be already processed/open,
1252 * if not it's a bug.
1253 */
1254 BUG_ON(!leader->fd);
1255
1256 fd = FD(leader, cpu, thread);
1257 BUG_ON(fd == -1);
1258
1259 return fd;
1260}
1261
Peter Zijlstra2c5e8c52015-04-07 11:09:54 +02001262struct bit_names {
1263 int bit;
1264 const char *name;
1265};
Adrian Huntere3e1a542013-08-14 15:48:24 +03001266
Peter Zijlstra2c5e8c52015-04-07 11:09:54 +02001267static void __p_bits(char *buf, size_t size, u64 value, struct bit_names *bits)
Adrian Huntere3e1a542013-08-14 15:48:24 +03001268{
Peter Zijlstra2c5e8c52015-04-07 11:09:54 +02001269 bool first_bit = true;
1270 int i = 0;
Adrian Huntere3e1a542013-08-14 15:48:24 +03001271
Peter Zijlstra2c5e8c52015-04-07 11:09:54 +02001272 do {
1273 if (value & bits[i].bit) {
1274 buf += scnprintf(buf, size, "%s%s", first_bit ? "" : "|", bits[i].name);
1275 first_bit = false;
1276 }
1277 } while (bits[++i].name != NULL);
1278}
Adrian Huntere3e1a542013-08-14 15:48:24 +03001279
Peter Zijlstra2c5e8c52015-04-07 11:09:54 +02001280static void __p_sample_type(char *buf, size_t size, u64 value)
1281{
1282#define bit_name(n) { PERF_SAMPLE_##n, #n }
1283 struct bit_names bits[] = {
1284 bit_name(IP), bit_name(TID), bit_name(TIME), bit_name(ADDR),
1285 bit_name(READ), bit_name(CALLCHAIN), bit_name(ID), bit_name(CPU),
1286 bit_name(PERIOD), bit_name(STREAM_ID), bit_name(RAW),
1287 bit_name(BRANCH_STACK), bit_name(REGS_USER), bit_name(STACK_USER),
Jiri Olsa84422592015-10-05 20:06:02 +02001288 bit_name(IDENTIFIER), bit_name(REGS_INTR), bit_name(DATA_SRC),
Jiri Olsadcdd1842015-11-25 16:36:55 +01001289 bit_name(WEIGHT),
Peter Zijlstra2c5e8c52015-04-07 11:09:54 +02001290 { .name = NULL, }
1291 };
1292#undef bit_name
1293 __p_bits(buf, size, value, bits);
1294}
Adrian Huntere3e1a542013-08-14 15:48:24 +03001295
Arnaldo Carvalho de Meloa213b922016-04-25 16:45:29 -03001296static void __p_branch_sample_type(char *buf, size_t size, u64 value)
1297{
1298#define bit_name(n) { PERF_SAMPLE_BRANCH_##n, #n }
1299 struct bit_names bits[] = {
1300 bit_name(USER), bit_name(KERNEL), bit_name(HV), bit_name(ANY),
1301 bit_name(ANY_CALL), bit_name(ANY_RETURN), bit_name(IND_CALL),
1302 bit_name(ABORT_TX), bit_name(IN_TX), bit_name(NO_TX),
1303 bit_name(COND), bit_name(CALL_STACK), bit_name(IND_JUMP),
1304 bit_name(CALL), bit_name(NO_FLAGS), bit_name(NO_CYCLES),
1305 { .name = NULL, }
1306 };
1307#undef bit_name
1308 __p_bits(buf, size, value, bits);
1309}
1310
Peter Zijlstra2c5e8c52015-04-07 11:09:54 +02001311static void __p_read_format(char *buf, size_t size, u64 value)
1312{
1313#define bit_name(n) { PERF_FORMAT_##n, #n }
1314 struct bit_names bits[] = {
1315 bit_name(TOTAL_TIME_ENABLED), bit_name(TOTAL_TIME_RUNNING),
1316 bit_name(ID), bit_name(GROUP),
1317 { .name = NULL, }
1318 };
1319#undef bit_name
1320 __p_bits(buf, size, value, bits);
1321}
Peter Zijlstra814c8c32015-03-31 00:19:31 +02001322
Peter Zijlstra2c5e8c52015-04-07 11:09:54 +02001323#define BUF_SIZE 1024
Adrian Huntere3e1a542013-08-14 15:48:24 +03001324
Adrian Hunter7310aed2015-06-11 15:51:04 +03001325#define p_hex(val) snprintf(buf, BUF_SIZE, "%#"PRIx64, (uint64_t)(val))
Peter Zijlstra2c5e8c52015-04-07 11:09:54 +02001326#define p_unsigned(val) snprintf(buf, BUF_SIZE, "%"PRIu64, (uint64_t)(val))
1327#define p_signed(val) snprintf(buf, BUF_SIZE, "%"PRId64, (int64_t)(val))
1328#define p_sample_type(val) __p_sample_type(buf, BUF_SIZE, val)
Arnaldo Carvalho de Meloa213b922016-04-25 16:45:29 -03001329#define p_branch_sample_type(val) __p_branch_sample_type(buf, BUF_SIZE, val)
Peter Zijlstra2c5e8c52015-04-07 11:09:54 +02001330#define p_read_format(val) __p_read_format(buf, BUF_SIZE, val)
Adrian Huntere3e1a542013-08-14 15:48:24 +03001331
Peter Zijlstra2c5e8c52015-04-07 11:09:54 +02001332#define PRINT_ATTRn(_n, _f, _p) \
1333do { \
1334 if (attr->_f) { \
1335 _p(attr->_f); \
1336 ret += attr__fprintf(fp, _n, buf, priv);\
1337 } \
1338} while (0)
1339
1340#define PRINT_ATTRf(_f, _p) PRINT_ATTRn(#_f, _f, _p)
1341
1342int perf_event_attr__fprintf(FILE *fp, struct perf_event_attr *attr,
1343 attr__fprintf_f attr__fprintf, void *priv)
1344{
1345 char buf[BUF_SIZE];
1346 int ret = 0;
1347
1348 PRINT_ATTRf(type, p_unsigned);
1349 PRINT_ATTRf(size, p_unsigned);
1350 PRINT_ATTRf(config, p_hex);
1351 PRINT_ATTRn("{ sample_period, sample_freq }", sample_period, p_unsigned);
1352 PRINT_ATTRf(sample_type, p_sample_type);
1353 PRINT_ATTRf(read_format, p_read_format);
1354
1355 PRINT_ATTRf(disabled, p_unsigned);
1356 PRINT_ATTRf(inherit, p_unsigned);
1357 PRINT_ATTRf(pinned, p_unsigned);
1358 PRINT_ATTRf(exclusive, p_unsigned);
1359 PRINT_ATTRf(exclude_user, p_unsigned);
1360 PRINT_ATTRf(exclude_kernel, p_unsigned);
1361 PRINT_ATTRf(exclude_hv, p_unsigned);
1362 PRINT_ATTRf(exclude_idle, p_unsigned);
1363 PRINT_ATTRf(mmap, p_unsigned);
1364 PRINT_ATTRf(comm, p_unsigned);
1365 PRINT_ATTRf(freq, p_unsigned);
1366 PRINT_ATTRf(inherit_stat, p_unsigned);
1367 PRINT_ATTRf(enable_on_exec, p_unsigned);
1368 PRINT_ATTRf(task, p_unsigned);
1369 PRINT_ATTRf(watermark, p_unsigned);
1370 PRINT_ATTRf(precise_ip, p_unsigned);
1371 PRINT_ATTRf(mmap_data, p_unsigned);
1372 PRINT_ATTRf(sample_id_all, p_unsigned);
1373 PRINT_ATTRf(exclude_host, p_unsigned);
1374 PRINT_ATTRf(exclude_guest, p_unsigned);
1375 PRINT_ATTRf(exclude_callchain_kernel, p_unsigned);
1376 PRINT_ATTRf(exclude_callchain_user, p_unsigned);
1377 PRINT_ATTRf(mmap2, p_unsigned);
1378 PRINT_ATTRf(comm_exec, p_unsigned);
1379 PRINT_ATTRf(use_clockid, p_unsigned);
Adrian Hunter02860392015-07-21 12:44:03 +03001380 PRINT_ATTRf(context_switch, p_unsigned);
Arnaldo Carvalho de Melo0a241ef2016-05-09 18:08:33 -03001381 PRINT_ATTRf(write_backward, p_unsigned);
Peter Zijlstra2c5e8c52015-04-07 11:09:54 +02001382
1383 PRINT_ATTRn("{ wakeup_events, wakeup_watermark }", wakeup_events, p_unsigned);
1384 PRINT_ATTRf(bp_type, p_unsigned);
1385 PRINT_ATTRn("{ bp_addr, config1 }", bp_addr, p_hex);
1386 PRINT_ATTRn("{ bp_len, config2 }", bp_len, p_hex);
Arnaldo Carvalho de Meloa213b922016-04-25 16:45:29 -03001387 PRINT_ATTRf(branch_sample_type, p_branch_sample_type);
Peter Zijlstra2c5e8c52015-04-07 11:09:54 +02001388 PRINT_ATTRf(sample_regs_user, p_hex);
1389 PRINT_ATTRf(sample_stack_user, p_unsigned);
1390 PRINT_ATTRf(clockid, p_signed);
1391 PRINT_ATTRf(sample_regs_intr, p_hex);
Adrian Hunter70d73de2015-04-09 18:54:06 +03001392 PRINT_ATTRf(aux_watermark, p_unsigned);
Arnaldo Carvalho de Melo792d48b2016-04-28 19:03:42 -03001393 PRINT_ATTRf(sample_max_stack, p_unsigned);
Adrian Huntere3e1a542013-08-14 15:48:24 +03001394
1395 return ret;
1396}
1397
Peter Zijlstra2c5e8c52015-04-07 11:09:54 +02001398static int __open_attr__fprintf(FILE *fp, const char *name, const char *val,
1399 void *priv __attribute__((unused)))
1400{
1401 return fprintf(fp, " %-32s %s\n", name, val);
1402}
1403
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -02001404static int __perf_evsel__open(struct perf_evsel *evsel, struct cpu_map *cpus,
Jiri Olsa6a4bb042012-08-08 12:22:36 +02001405 struct thread_map *threads)
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -02001406{
Adrian Hunterbf8e8f42014-07-31 09:00:51 +03001407 int cpu, thread, nthreads;
Yann Droneaud57480d22014-06-30 22:28:47 +02001408 unsigned long flags = PERF_FLAG_FD_CLOEXEC;
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -02001409 int pid = -1, err;
Andi Kleenbec19672013-08-04 19:41:26 -07001410 enum { NO_CHANGE, SET_TO_MAX, INCREASED_MAX } set_rlimit = NO_CHANGE;
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -02001411
Arnaldo Carvalho de Melo32a951b2016-07-14 08:34:33 +00001412 if (perf_missing_features.write_backward && evsel->attr.write_backward)
1413 return -EINVAL;
1414
Adrian Hunterbf8e8f42014-07-31 09:00:51 +03001415 if (evsel->system_wide)
1416 nthreads = 1;
1417 else
1418 nthreads = threads->nr;
1419
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -02001420 if (evsel->fd == NULL &&
Adrian Hunterbf8e8f42014-07-31 09:00:51 +03001421 perf_evsel__alloc_fd(evsel, cpus->nr, nthreads) < 0)
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -02001422 return -ENOMEM;
Arnaldo Carvalho de Melo4eed11d2011-01-04 00:13:17 -02001423
Stephane Eranian023695d2011-02-14 11:20:01 +02001424 if (evsel->cgrp) {
Yann Droneaud57480d22014-06-30 22:28:47 +02001425 flags |= PERF_FLAG_PID_CGROUP;
Stephane Eranian023695d2011-02-14 11:20:01 +02001426 pid = evsel->cgrp->fd;
1427 }
1428
Arnaldo Carvalho de Melo594ac612012-12-13 13:13:07 -03001429fallback_missing_features:
Peter Zijlstra814c8c32015-03-31 00:19:31 +02001430 if (perf_missing_features.clockid_wrong)
1431 evsel->attr.clockid = CLOCK_MONOTONIC; /* should always work */
1432 if (perf_missing_features.clockid) {
1433 evsel->attr.use_clockid = 0;
1434 evsel->attr.clockid = 0;
1435 }
Yann Droneaud57480d22014-06-30 22:28:47 +02001436 if (perf_missing_features.cloexec)
1437 flags &= ~(unsigned long)PERF_FLAG_FD_CLOEXEC;
Stephane Eranian5c5e8542013-08-21 12:10:25 +02001438 if (perf_missing_features.mmap2)
1439 evsel->attr.mmap2 = 0;
Arnaldo Carvalho de Melo594ac612012-12-13 13:13:07 -03001440 if (perf_missing_features.exclude_guest)
1441 evsel->attr.exclude_guest = evsel->attr.exclude_host = 0;
Andi Kleenbd0f8892015-12-11 16:12:24 -08001442 if (perf_missing_features.lbr_flags)
1443 evsel->attr.branch_sample_type &= ~(PERF_SAMPLE_BRANCH_NO_FLAGS |
1444 PERF_SAMPLE_BRANCH_NO_CYCLES);
Arnaldo Carvalho de Melo594ac612012-12-13 13:13:07 -03001445retry_sample_id:
1446 if (perf_missing_features.sample_id_all)
1447 evsel->attr.sample_id_all = 0;
1448
Peter Zijlstra2c5e8c52015-04-07 11:09:54 +02001449 if (verbose >= 2) {
1450 fprintf(stderr, "%.60s\n", graph_dotted_line);
1451 fprintf(stderr, "perf_event_attr:\n");
1452 perf_event_attr__fprintf(stderr, &evsel->attr, __open_attr__fprintf, NULL);
1453 fprintf(stderr, "%.60s\n", graph_dotted_line);
1454 }
Adrian Huntere3e1a542013-08-14 15:48:24 +03001455
Arnaldo Carvalho de Melo86bd5e82011-01-03 23:09:46 -02001456 for (cpu = 0; cpu < cpus->nr; cpu++) {
Arnaldo Carvalho de Melo9d04f172011-01-12 00:08:18 -02001457
Adrian Hunterbf8e8f42014-07-31 09:00:51 +03001458 for (thread = 0; thread < nthreads; thread++) {
Jiri Olsa6a4bb042012-08-08 12:22:36 +02001459 int group_fd;
Stephane Eranian023695d2011-02-14 11:20:01 +02001460
Adrian Hunterbf8e8f42014-07-31 09:00:51 +03001461 if (!evsel->cgrp && !evsel->system_wide)
Jiri Olsae13798c2015-06-23 00:36:02 +02001462 pid = thread_map__pid(threads, thread);
Stephane Eranian023695d2011-02-14 11:20:01 +02001463
Jiri Olsa6a4bb042012-08-08 12:22:36 +02001464 group_fd = get_group_fd(evsel, cpu, thread);
Andi Kleenbec19672013-08-04 19:41:26 -07001465retry_open:
Ramkumar Ramachandraa33f6ef2014-03-18 15:10:42 -04001466 pr_debug2("sys_perf_event_open: pid %d cpu %d group_fd %d flags %#lx\n",
Adrian Huntere3e1a542013-08-14 15:48:24 +03001467 pid, cpus->map[cpu], group_fd, flags);
1468
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -02001469 FD(evsel, cpu, thread) = sys_perf_event_open(&evsel->attr,
Stephane Eranian023695d2011-02-14 11:20:01 +02001470 pid,
Arnaldo Carvalho de Melof08199d2011-01-11 23:42:19 -02001471 cpus->map[cpu],
Stephane Eranian023695d2011-02-14 11:20:01 +02001472 group_fd, flags);
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -02001473 if (FD(evsel, cpu, thread) < 0) {
1474 err = -errno;
Ramkumar Ramachandraa33f6ef2014-03-18 15:10:42 -04001475 pr_debug2("sys_perf_event_open failed, error %d\n",
Adrian Hunterf852fd62013-11-01 15:51:29 +02001476 err);
Arnaldo Carvalho de Melo594ac612012-12-13 13:13:07 -03001477 goto try_fallback;
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -02001478 }
Wang Nan1f45b1d2015-10-14 12:41:18 +00001479
1480 if (evsel->bpf_fd >= 0) {
1481 int evt_fd = FD(evsel, cpu, thread);
1482 int bpf_fd = evsel->bpf_fd;
1483
1484 err = ioctl(evt_fd,
1485 PERF_EVENT_IOC_SET_BPF,
1486 bpf_fd);
1487 if (err && errno != EEXIST) {
1488 pr_err("failed to attach bpf fd %d: %s\n",
1489 bpf_fd, strerror(errno));
1490 err = -EINVAL;
1491 goto out_close;
1492 }
1493 }
1494
Andi Kleenbec19672013-08-04 19:41:26 -07001495 set_rlimit = NO_CHANGE;
Peter Zijlstra814c8c32015-03-31 00:19:31 +02001496
1497 /*
1498 * If we succeeded but had to kill clockid, fail and
1499 * have perf_evsel__open_strerror() print us a nice
1500 * error.
1501 */
1502 if (perf_missing_features.clockid ||
1503 perf_missing_features.clockid_wrong) {
1504 err = -EINVAL;
1505 goto out_close;
1506 }
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -02001507 }
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -02001508 }
1509
1510 return 0;
1511
Arnaldo Carvalho de Melo594ac612012-12-13 13:13:07 -03001512try_fallback:
Andi Kleenbec19672013-08-04 19:41:26 -07001513 /*
1514 * perf stat needs between 5 and 22 fds per CPU. When we run out
1515 * of them try to increase the limits.
1516 */
1517 if (err == -EMFILE && set_rlimit < INCREASED_MAX) {
1518 struct rlimit l;
1519 int old_errno = errno;
1520
1521 if (getrlimit(RLIMIT_NOFILE, &l) == 0) {
1522 if (set_rlimit == NO_CHANGE)
1523 l.rlim_cur = l.rlim_max;
1524 else {
1525 l.rlim_cur = l.rlim_max + 1000;
1526 l.rlim_max = l.rlim_cur;
1527 }
1528 if (setrlimit(RLIMIT_NOFILE, &l) == 0) {
1529 set_rlimit++;
1530 errno = old_errno;
1531 goto retry_open;
1532 }
1533 }
1534 errno = old_errno;
1535 }
1536
Arnaldo Carvalho de Melo594ac612012-12-13 13:13:07 -03001537 if (err != -EINVAL || cpu > 0 || thread > 0)
1538 goto out_close;
1539
Peter Zijlstra814c8c32015-03-31 00:19:31 +02001540 /*
1541 * Must probe features in the order they were added to the
1542 * perf_event_attr interface.
1543 */
Arnaldo Carvalho de Melo7da36e92016-06-20 10:47:18 +00001544 if (!perf_missing_features.write_backward && evsel->attr.write_backward) {
1545 perf_missing_features.write_backward = true;
Arnaldo Carvalho de Melo32a951b2016-07-14 08:34:33 +00001546 goto out_close;
Arnaldo Carvalho de Melo7da36e92016-06-20 10:47:18 +00001547 } else if (!perf_missing_features.clockid_wrong && evsel->attr.use_clockid) {
Peter Zijlstra814c8c32015-03-31 00:19:31 +02001548 perf_missing_features.clockid_wrong = true;
1549 goto fallback_missing_features;
1550 } else if (!perf_missing_features.clockid && evsel->attr.use_clockid) {
1551 perf_missing_features.clockid = true;
1552 goto fallback_missing_features;
1553 } else if (!perf_missing_features.cloexec && (flags & PERF_FLAG_FD_CLOEXEC)) {
Yann Droneaud57480d22014-06-30 22:28:47 +02001554 perf_missing_features.cloexec = true;
1555 goto fallback_missing_features;
1556 } else if (!perf_missing_features.mmap2 && evsel->attr.mmap2) {
Stephane Eranian5c5e8542013-08-21 12:10:25 +02001557 perf_missing_features.mmap2 = true;
1558 goto fallback_missing_features;
1559 } else if (!perf_missing_features.exclude_guest &&
1560 (evsel->attr.exclude_guest || evsel->attr.exclude_host)) {
Arnaldo Carvalho de Melo594ac612012-12-13 13:13:07 -03001561 perf_missing_features.exclude_guest = true;
1562 goto fallback_missing_features;
1563 } else if (!perf_missing_features.sample_id_all) {
1564 perf_missing_features.sample_id_all = true;
1565 goto retry_sample_id;
Andi Kleenbd0f8892015-12-11 16:12:24 -08001566 } else if (!perf_missing_features.lbr_flags &&
1567 (evsel->attr.branch_sample_type &
1568 (PERF_SAMPLE_BRANCH_NO_CYCLES |
1569 PERF_SAMPLE_BRANCH_NO_FLAGS))) {
1570 perf_missing_features.lbr_flags = true;
1571 goto fallback_missing_features;
Arnaldo Carvalho de Melo594ac612012-12-13 13:13:07 -03001572 }
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -02001573out_close:
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -02001574 do {
1575 while (--thread >= 0) {
1576 close(FD(evsel, cpu, thread));
1577 FD(evsel, cpu, thread) = -1;
1578 }
Adrian Hunterbf8e8f42014-07-31 09:00:51 +03001579 thread = nthreads;
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -02001580 } while (--cpu >= 0);
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -02001581 return err;
1582}
1583
1584void perf_evsel__close(struct perf_evsel *evsel, int ncpus, int nthreads)
1585{
1586 if (evsel->fd == NULL)
1587 return;
1588
1589 perf_evsel__close_fd(evsel, ncpus, nthreads);
1590 perf_evsel__free_fd(evsel);
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -02001591}
1592
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -02001593static struct {
1594 struct cpu_map map;
1595 int cpus[1];
1596} empty_cpu_map = {
1597 .map.nr = 1,
1598 .cpus = { -1, },
1599};
1600
1601static struct {
1602 struct thread_map map;
1603 int threads[1];
1604} empty_thread_map = {
1605 .map.nr = 1,
1606 .threads = { -1, },
1607};
1608
Arnaldo Carvalho de Melof08199d2011-01-11 23:42:19 -02001609int perf_evsel__open(struct perf_evsel *evsel, struct cpu_map *cpus,
Jiri Olsa6a4bb042012-08-08 12:22:36 +02001610 struct thread_map *threads)
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -02001611{
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -02001612 if (cpus == NULL) {
1613 /* Work around old compiler warnings about strict aliasing */
1614 cpus = &empty_cpu_map.map;
1615 }
1616
1617 if (threads == NULL)
1618 threads = &empty_thread_map.map;
1619
Jiri Olsa6a4bb042012-08-08 12:22:36 +02001620 return __perf_evsel__open(evsel, cpus, threads);
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -02001621}
1622
Arnaldo Carvalho de Melof08199d2011-01-11 23:42:19 -02001623int perf_evsel__open_per_cpu(struct perf_evsel *evsel,
Jiri Olsa6a4bb042012-08-08 12:22:36 +02001624 struct cpu_map *cpus)
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -02001625{
Jiri Olsa6a4bb042012-08-08 12:22:36 +02001626 return __perf_evsel__open(evsel, cpus, &empty_thread_map.map);
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -02001627}
1628
Arnaldo Carvalho de Melof08199d2011-01-11 23:42:19 -02001629int perf_evsel__open_per_thread(struct perf_evsel *evsel,
Jiri Olsa6a4bb042012-08-08 12:22:36 +02001630 struct thread_map *threads)
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -02001631{
Jiri Olsa6a4bb042012-08-08 12:22:36 +02001632 return __perf_evsel__open(evsel, &empty_cpu_map.map, threads);
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -02001633}
Arnaldo Carvalho de Melo70082dd2011-01-12 17:03:24 -02001634
Arnaldo Carvalho de Melo0807d2d2012-09-26 12:48:18 -03001635static int perf_evsel__parse_id_sample(const struct perf_evsel *evsel,
1636 const union perf_event *event,
1637 struct perf_sample *sample)
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001638{
Arnaldo Carvalho de Melo0807d2d2012-09-26 12:48:18 -03001639 u64 type = evsel->attr.sample_type;
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001640 const u64 *array = event->sample.array;
Arnaldo Carvalho de Melo0807d2d2012-09-26 12:48:18 -03001641 bool swapped = evsel->needs_swap;
Jiri Olsa37073f92012-05-30 14:23:44 +02001642 union u64_swap u;
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001643
1644 array += ((event->header.size -
1645 sizeof(event->header)) / sizeof(u64)) - 1;
1646
Adrian Hunter75562572013-08-27 11:23:09 +03001647 if (type & PERF_SAMPLE_IDENTIFIER) {
1648 sample->id = *array;
1649 array--;
1650 }
1651
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001652 if (type & PERF_SAMPLE_CPU) {
Jiri Olsa37073f92012-05-30 14:23:44 +02001653 u.val64 = *array;
1654 if (swapped) {
1655 /* undo swap of u64, then swap on individual u32s */
1656 u.val64 = bswap_64(u.val64);
1657 u.val32[0] = bswap_32(u.val32[0]);
1658 }
1659
1660 sample->cpu = u.val32[0];
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001661 array--;
1662 }
1663
1664 if (type & PERF_SAMPLE_STREAM_ID) {
1665 sample->stream_id = *array;
1666 array--;
1667 }
1668
1669 if (type & PERF_SAMPLE_ID) {
1670 sample->id = *array;
1671 array--;
1672 }
1673
1674 if (type & PERF_SAMPLE_TIME) {
1675 sample->time = *array;
1676 array--;
1677 }
1678
1679 if (type & PERF_SAMPLE_TID) {
Jiri Olsa37073f92012-05-30 14:23:44 +02001680 u.val64 = *array;
1681 if (swapped) {
1682 /* undo swap of u64, then swap on individual u32s */
1683 u.val64 = bswap_64(u.val64);
1684 u.val32[0] = bswap_32(u.val32[0]);
1685 u.val32[1] = bswap_32(u.val32[1]);
1686 }
1687
1688 sample->pid = u.val32[0];
1689 sample->tid = u.val32[1];
Adrian Hunterdd44bc62013-10-18 15:29:01 +03001690 array--;
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001691 }
1692
1693 return 0;
1694}
1695
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001696static inline bool overflow(const void *endp, u16 max_size, const void *offset,
1697 u64 size)
Frederic Weisbecker98e1da92011-05-21 20:08:15 +02001698{
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001699 return size > max_size || offset + size > endp;
Frederic Weisbecker98e1da92011-05-21 20:08:15 +02001700}
1701
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001702#define OVERFLOW_CHECK(offset, size, max_size) \
1703 do { \
1704 if (overflow(endp, (max_size), (offset), (size))) \
1705 return -EFAULT; \
1706 } while (0)
1707
1708#define OVERFLOW_CHECK_u64(offset) \
1709 OVERFLOW_CHECK(offset, sizeof(u64), sizeof(u64))
1710
Arnaldo Carvalho de Meloa3f698f2012-08-02 12:23:46 -03001711int perf_evsel__parse_sample(struct perf_evsel *evsel, union perf_event *event,
Arnaldo Carvalho de Melo0807d2d2012-09-26 12:48:18 -03001712 struct perf_sample *data)
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001713{
Arnaldo Carvalho de Meloa3f698f2012-08-02 12:23:46 -03001714 u64 type = evsel->attr.sample_type;
Arnaldo Carvalho de Melo0807d2d2012-09-26 12:48:18 -03001715 bool swapped = evsel->needs_swap;
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001716 const u64 *array;
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001717 u16 max_size = event->header.size;
1718 const void *endp = (void *)event + max_size;
1719 u64 sz;
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001720
David Ahern936be502011-09-06 09:12:26 -06001721 /*
1722 * used for cross-endian analysis. See git commit 65014ab3
1723 * for why this goofiness is needed.
1724 */
Jiri Olsa6a11f922012-05-16 08:59:04 +02001725 union u64_swap u;
David Ahern936be502011-09-06 09:12:26 -06001726
Robert Richterf3bda2c2011-12-15 17:32:39 +01001727 memset(data, 0, sizeof(*data));
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001728 data->cpu = data->pid = data->tid = -1;
1729 data->stream_id = data->id = data->time = -1ULL;
Jiri Olsabc529082014-02-03 12:44:41 +01001730 data->period = evsel->attr.sample_period;
Andi Kleen05484292013-01-24 16:10:29 +01001731 data->weight = 0;
Arnaldo Carvalho de Melo473398a2016-03-22 18:23:43 -03001732 data->cpumode = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001733
1734 if (event->header.type != PERF_RECORD_SAMPLE) {
Arnaldo Carvalho de Meloa3f698f2012-08-02 12:23:46 -03001735 if (!evsel->attr.sample_id_all)
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001736 return 0;
Arnaldo Carvalho de Melo0807d2d2012-09-26 12:48:18 -03001737 return perf_evsel__parse_id_sample(evsel, event, data);
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001738 }
1739
1740 array = event->sample.array;
1741
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001742 /*
1743 * The evsel's sample_size is based on PERF_SAMPLE_MASK which includes
1744 * up to PERF_SAMPLE_PERIOD. After that overflow() must be used to
1745 * check the format does not go past the end of the event.
1746 */
Arnaldo Carvalho de Meloa3f698f2012-08-02 12:23:46 -03001747 if (evsel->sample_size + sizeof(event->header) > event->header.size)
Frederic Weisbeckera2854122011-05-21 19:33:04 +02001748 return -EFAULT;
1749
Adrian Hunter75562572013-08-27 11:23:09 +03001750 data->id = -1ULL;
1751 if (type & PERF_SAMPLE_IDENTIFIER) {
1752 data->id = *array;
1753 array++;
1754 }
1755
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001756 if (type & PERF_SAMPLE_IP) {
Adrian Hunteref893252013-08-27 11:23:06 +03001757 data->ip = *array;
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001758 array++;
1759 }
1760
1761 if (type & PERF_SAMPLE_TID) {
David Ahern936be502011-09-06 09:12:26 -06001762 u.val64 = *array;
1763 if (swapped) {
1764 /* undo swap of u64, then swap on individual u32s */
1765 u.val64 = bswap_64(u.val64);
1766 u.val32[0] = bswap_32(u.val32[0]);
1767 u.val32[1] = bswap_32(u.val32[1]);
1768 }
1769
1770 data->pid = u.val32[0];
1771 data->tid = u.val32[1];
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001772 array++;
1773 }
1774
1775 if (type & PERF_SAMPLE_TIME) {
1776 data->time = *array;
1777 array++;
1778 }
1779
David Ahern7cec0922011-05-30 13:08:23 -06001780 data->addr = 0;
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001781 if (type & PERF_SAMPLE_ADDR) {
1782 data->addr = *array;
1783 array++;
1784 }
1785
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001786 if (type & PERF_SAMPLE_ID) {
1787 data->id = *array;
1788 array++;
1789 }
1790
1791 if (type & PERF_SAMPLE_STREAM_ID) {
1792 data->stream_id = *array;
1793 array++;
1794 }
1795
1796 if (type & PERF_SAMPLE_CPU) {
David Ahern936be502011-09-06 09:12:26 -06001797
1798 u.val64 = *array;
1799 if (swapped) {
1800 /* undo swap of u64, then swap on individual u32s */
1801 u.val64 = bswap_64(u.val64);
1802 u.val32[0] = bswap_32(u.val32[0]);
1803 }
1804
1805 data->cpu = u.val32[0];
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001806 array++;
1807 }
1808
1809 if (type & PERF_SAMPLE_PERIOD) {
1810 data->period = *array;
1811 array++;
1812 }
1813
1814 if (type & PERF_SAMPLE_READ) {
Jiri Olsa9ede4732012-10-10 17:38:13 +02001815 u64 read_format = evsel->attr.read_format;
1816
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001817 OVERFLOW_CHECK_u64(array);
Jiri Olsa9ede4732012-10-10 17:38:13 +02001818 if (read_format & PERF_FORMAT_GROUP)
1819 data->read.group.nr = *array;
1820 else
1821 data->read.one.value = *array;
1822
1823 array++;
1824
1825 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) {
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001826 OVERFLOW_CHECK_u64(array);
Jiri Olsa9ede4732012-10-10 17:38:13 +02001827 data->read.time_enabled = *array;
1828 array++;
1829 }
1830
1831 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) {
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001832 OVERFLOW_CHECK_u64(array);
Jiri Olsa9ede4732012-10-10 17:38:13 +02001833 data->read.time_running = *array;
1834 array++;
1835 }
1836
1837 /* PERF_FORMAT_ID is forced for PERF_SAMPLE_READ */
1838 if (read_format & PERF_FORMAT_GROUP) {
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001839 const u64 max_group_nr = UINT64_MAX /
1840 sizeof(struct sample_read_value);
1841
1842 if (data->read.group.nr > max_group_nr)
1843 return -EFAULT;
1844 sz = data->read.group.nr *
1845 sizeof(struct sample_read_value);
1846 OVERFLOW_CHECK(array, sz, max_size);
1847 data->read.group.values =
1848 (struct sample_read_value *)array;
1849 array = (void *)array + sz;
Jiri Olsa9ede4732012-10-10 17:38:13 +02001850 } else {
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001851 OVERFLOW_CHECK_u64(array);
Jiri Olsa9ede4732012-10-10 17:38:13 +02001852 data->read.one.id = *array;
1853 array++;
1854 }
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001855 }
1856
1857 if (type & PERF_SAMPLE_CALLCHAIN) {
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001858 const u64 max_callchain_nr = UINT64_MAX / sizeof(u64);
1859
1860 OVERFLOW_CHECK_u64(array);
1861 data->callchain = (struct ip_callchain *)array++;
1862 if (data->callchain->nr > max_callchain_nr)
Frederic Weisbecker98e1da92011-05-21 20:08:15 +02001863 return -EFAULT;
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001864 sz = data->callchain->nr * sizeof(u64);
1865 OVERFLOW_CHECK(array, sz, max_size);
1866 array = (void *)array + sz;
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001867 }
1868
1869 if (type & PERF_SAMPLE_RAW) {
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001870 OVERFLOW_CHECK_u64(array);
David Ahern936be502011-09-06 09:12:26 -06001871 u.val64 = *array;
1872 if (WARN_ONCE(swapped,
1873 "Endianness of raw data not corrected!\n")) {
1874 /* undo swap of u64, then swap on individual u32s */
1875 u.val64 = bswap_64(u.val64);
1876 u.val32[0] = bswap_32(u.val32[0]);
1877 u.val32[1] = bswap_32(u.val32[1]);
1878 }
David Ahern936be502011-09-06 09:12:26 -06001879 data->raw_size = u.val32[0];
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001880 array = (void *)array + sizeof(u32);
Frederic Weisbecker98e1da92011-05-21 20:08:15 +02001881
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001882 OVERFLOW_CHECK(array, data->raw_size, max_size);
1883 data->raw_data = (void *)array;
1884 array = (void *)array + data->raw_size;
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001885 }
1886
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +01001887 if (type & PERF_SAMPLE_BRANCH_STACK) {
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001888 const u64 max_branch_nr = UINT64_MAX /
1889 sizeof(struct branch_entry);
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +01001890
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001891 OVERFLOW_CHECK_u64(array);
1892 data->branch_stack = (struct branch_stack *)array++;
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +01001893
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001894 if (data->branch_stack->nr > max_branch_nr)
1895 return -EFAULT;
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +01001896 sz = data->branch_stack->nr * sizeof(struct branch_entry);
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001897 OVERFLOW_CHECK(array, sz, max_size);
1898 array = (void *)array + sz;
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +01001899 }
Jiri Olsa0f6a3012012-08-07 15:20:45 +02001900
1901 if (type & PERF_SAMPLE_REGS_USER) {
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001902 OVERFLOW_CHECK_u64(array);
Adrian Hunter5b95a4a32013-08-27 11:23:10 +03001903 data->user_regs.abi = *array;
1904 array++;
Jiri Olsa0f6a3012012-08-07 15:20:45 +02001905
Adrian Hunter5b95a4a32013-08-27 11:23:10 +03001906 if (data->user_regs.abi) {
Jiri Olsa352ea452014-01-07 13:47:25 +01001907 u64 mask = evsel->attr.sample_regs_user;
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001908
Jiri Olsa352ea452014-01-07 13:47:25 +01001909 sz = hweight_long(mask) * sizeof(u64);
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001910 OVERFLOW_CHECK(array, sz, max_size);
Jiri Olsa352ea452014-01-07 13:47:25 +01001911 data->user_regs.mask = mask;
Jiri Olsa0f6a3012012-08-07 15:20:45 +02001912 data->user_regs.regs = (u64 *)array;
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001913 array = (void *)array + sz;
Jiri Olsa0f6a3012012-08-07 15:20:45 +02001914 }
1915 }
1916
1917 if (type & PERF_SAMPLE_STACK_USER) {
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001918 OVERFLOW_CHECK_u64(array);
1919 sz = *array++;
Jiri Olsa0f6a3012012-08-07 15:20:45 +02001920
1921 data->user_stack.offset = ((char *)(array - 1)
1922 - (char *) event);
1923
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001924 if (!sz) {
Jiri Olsa0f6a3012012-08-07 15:20:45 +02001925 data->user_stack.size = 0;
1926 } else {
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001927 OVERFLOW_CHECK(array, sz, max_size);
Jiri Olsa0f6a3012012-08-07 15:20:45 +02001928 data->user_stack.data = (char *)array;
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001929 array = (void *)array + sz;
1930 OVERFLOW_CHECK_u64(array);
Adrian Hunter54bd2692013-07-04 16:20:34 +03001931 data->user_stack.size = *array++;
Jiri Olsaa65cb4b2013-10-02 15:46:39 +02001932 if (WARN_ONCE(data->user_stack.size > sz,
1933 "user stack dump failure\n"))
1934 return -EFAULT;
Jiri Olsa0f6a3012012-08-07 15:20:45 +02001935 }
1936 }
1937
Andi Kleen05484292013-01-24 16:10:29 +01001938 data->weight = 0;
1939 if (type & PERF_SAMPLE_WEIGHT) {
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001940 OVERFLOW_CHECK_u64(array);
Andi Kleen05484292013-01-24 16:10:29 +01001941 data->weight = *array;
1942 array++;
1943 }
1944
Stephane Eranian98a3b322013-01-24 16:10:35 +01001945 data->data_src = PERF_MEM_DATA_SRC_NONE;
1946 if (type & PERF_SAMPLE_DATA_SRC) {
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001947 OVERFLOW_CHECK_u64(array);
Stephane Eranian98a3b322013-01-24 16:10:35 +01001948 data->data_src = *array;
1949 array++;
1950 }
1951
Andi Kleen475eeab2013-09-20 07:40:43 -07001952 data->transaction = 0;
1953 if (type & PERF_SAMPLE_TRANSACTION) {
Adrian Hunter87b95522013-11-01 15:51:36 +02001954 OVERFLOW_CHECK_u64(array);
Andi Kleen475eeab2013-09-20 07:40:43 -07001955 data->transaction = *array;
1956 array++;
1957 }
1958
Stephane Eranian6a21c0b2014-09-24 13:48:39 +02001959 data->intr_regs.abi = PERF_SAMPLE_REGS_ABI_NONE;
1960 if (type & PERF_SAMPLE_REGS_INTR) {
1961 OVERFLOW_CHECK_u64(array);
1962 data->intr_regs.abi = *array;
1963 array++;
1964
1965 if (data->intr_regs.abi != PERF_SAMPLE_REGS_ABI_NONE) {
1966 u64 mask = evsel->attr.sample_regs_intr;
1967
1968 sz = hweight_long(mask) * sizeof(u64);
1969 OVERFLOW_CHECK(array, sz, max_size);
1970 data->intr_regs.mask = mask;
1971 data->intr_regs.regs = (u64 *)array;
1972 array = (void *)array + sz;
1973 }
1974 }
1975
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001976 return 0;
1977}
Andrew Vagin74eec262011-11-28 12:03:31 +03001978
Adrian Hunterb1cf6f62013-08-27 11:23:12 +03001979size_t perf_event__sample_event_size(const struct perf_sample *sample, u64 type,
Jiri Olsa352ea452014-01-07 13:47:25 +01001980 u64 read_format)
Adrian Hunterb1cf6f62013-08-27 11:23:12 +03001981{
1982 size_t sz, result = sizeof(struct sample_event);
1983
1984 if (type & PERF_SAMPLE_IDENTIFIER)
1985 result += sizeof(u64);
1986
1987 if (type & PERF_SAMPLE_IP)
1988 result += sizeof(u64);
1989
1990 if (type & PERF_SAMPLE_TID)
1991 result += sizeof(u64);
1992
1993 if (type & PERF_SAMPLE_TIME)
1994 result += sizeof(u64);
1995
1996 if (type & PERF_SAMPLE_ADDR)
1997 result += sizeof(u64);
1998
1999 if (type & PERF_SAMPLE_ID)
2000 result += sizeof(u64);
2001
2002 if (type & PERF_SAMPLE_STREAM_ID)
2003 result += sizeof(u64);
2004
2005 if (type & PERF_SAMPLE_CPU)
2006 result += sizeof(u64);
2007
2008 if (type & PERF_SAMPLE_PERIOD)
2009 result += sizeof(u64);
2010
2011 if (type & PERF_SAMPLE_READ) {
2012 result += sizeof(u64);
2013 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
2014 result += sizeof(u64);
2015 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
2016 result += sizeof(u64);
2017 /* PERF_FORMAT_ID is forced for PERF_SAMPLE_READ */
2018 if (read_format & PERF_FORMAT_GROUP) {
2019 sz = sample->read.group.nr *
2020 sizeof(struct sample_read_value);
2021 result += sz;
2022 } else {
2023 result += sizeof(u64);
2024 }
2025 }
2026
2027 if (type & PERF_SAMPLE_CALLCHAIN) {
2028 sz = (sample->callchain->nr + 1) * sizeof(u64);
2029 result += sz;
2030 }
2031
2032 if (type & PERF_SAMPLE_RAW) {
2033 result += sizeof(u32);
2034 result += sample->raw_size;
2035 }
2036
2037 if (type & PERF_SAMPLE_BRANCH_STACK) {
2038 sz = sample->branch_stack->nr * sizeof(struct branch_entry);
2039 sz += sizeof(u64);
2040 result += sz;
2041 }
2042
2043 if (type & PERF_SAMPLE_REGS_USER) {
2044 if (sample->user_regs.abi) {
2045 result += sizeof(u64);
Jiri Olsa352ea452014-01-07 13:47:25 +01002046 sz = hweight_long(sample->user_regs.mask) * sizeof(u64);
Adrian Hunterb1cf6f62013-08-27 11:23:12 +03002047 result += sz;
2048 } else {
2049 result += sizeof(u64);
2050 }
2051 }
2052
2053 if (type & PERF_SAMPLE_STACK_USER) {
2054 sz = sample->user_stack.size;
2055 result += sizeof(u64);
2056 if (sz) {
2057 result += sz;
2058 result += sizeof(u64);
2059 }
2060 }
2061
2062 if (type & PERF_SAMPLE_WEIGHT)
2063 result += sizeof(u64);
2064
2065 if (type & PERF_SAMPLE_DATA_SRC)
2066 result += sizeof(u64);
2067
Adrian Hunter42d88912013-11-01 15:51:38 +02002068 if (type & PERF_SAMPLE_TRANSACTION)
2069 result += sizeof(u64);
2070
Stephane Eranian6a21c0b2014-09-24 13:48:39 +02002071 if (type & PERF_SAMPLE_REGS_INTR) {
2072 if (sample->intr_regs.abi) {
2073 result += sizeof(u64);
2074 sz = hweight_long(sample->intr_regs.mask) * sizeof(u64);
2075 result += sz;
2076 } else {
2077 result += sizeof(u64);
2078 }
2079 }
2080
Adrian Hunterb1cf6f62013-08-27 11:23:12 +03002081 return result;
2082}
2083
Andrew Vagin74eec262011-11-28 12:03:31 +03002084int perf_event__synthesize_sample(union perf_event *event, u64 type,
Jiri Olsa352ea452014-01-07 13:47:25 +01002085 u64 read_format,
Andrew Vagin74eec262011-11-28 12:03:31 +03002086 const struct perf_sample *sample,
2087 bool swapped)
2088{
2089 u64 *array;
Adrian Hunterd03f2172013-08-27 11:23:11 +03002090 size_t sz;
Andrew Vagin74eec262011-11-28 12:03:31 +03002091 /*
2092 * used for cross-endian analysis. See git commit 65014ab3
2093 * for why this goofiness is needed.
2094 */
Jiri Olsa6a11f922012-05-16 08:59:04 +02002095 union u64_swap u;
Andrew Vagin74eec262011-11-28 12:03:31 +03002096
2097 array = event->sample.array;
2098
Adrian Hunter75562572013-08-27 11:23:09 +03002099 if (type & PERF_SAMPLE_IDENTIFIER) {
2100 *array = sample->id;
2101 array++;
2102 }
2103
Andrew Vagin74eec262011-11-28 12:03:31 +03002104 if (type & PERF_SAMPLE_IP) {
Adrian Hunteref893252013-08-27 11:23:06 +03002105 *array = sample->ip;
Andrew Vagin74eec262011-11-28 12:03:31 +03002106 array++;
2107 }
2108
2109 if (type & PERF_SAMPLE_TID) {
2110 u.val32[0] = sample->pid;
2111 u.val32[1] = sample->tid;
2112 if (swapped) {
2113 /*
Arnaldo Carvalho de Meloa3f698f2012-08-02 12:23:46 -03002114 * Inverse of what is done in perf_evsel__parse_sample
Andrew Vagin74eec262011-11-28 12:03:31 +03002115 */
2116 u.val32[0] = bswap_32(u.val32[0]);
2117 u.val32[1] = bswap_32(u.val32[1]);
2118 u.val64 = bswap_64(u.val64);
2119 }
2120
2121 *array = u.val64;
2122 array++;
2123 }
2124
2125 if (type & PERF_SAMPLE_TIME) {
2126 *array = sample->time;
2127 array++;
2128 }
2129
2130 if (type & PERF_SAMPLE_ADDR) {
2131 *array = sample->addr;
2132 array++;
2133 }
2134
2135 if (type & PERF_SAMPLE_ID) {
2136 *array = sample->id;
2137 array++;
2138 }
2139
2140 if (type & PERF_SAMPLE_STREAM_ID) {
2141 *array = sample->stream_id;
2142 array++;
2143 }
2144
2145 if (type & PERF_SAMPLE_CPU) {
2146 u.val32[0] = sample->cpu;
2147 if (swapped) {
2148 /*
Arnaldo Carvalho de Meloa3f698f2012-08-02 12:23:46 -03002149 * Inverse of what is done in perf_evsel__parse_sample
Andrew Vagin74eec262011-11-28 12:03:31 +03002150 */
2151 u.val32[0] = bswap_32(u.val32[0]);
2152 u.val64 = bswap_64(u.val64);
2153 }
2154 *array = u.val64;
2155 array++;
2156 }
2157
2158 if (type & PERF_SAMPLE_PERIOD) {
2159 *array = sample->period;
2160 array++;
2161 }
2162
Adrian Hunterd03f2172013-08-27 11:23:11 +03002163 if (type & PERF_SAMPLE_READ) {
2164 if (read_format & PERF_FORMAT_GROUP)
2165 *array = sample->read.group.nr;
2166 else
2167 *array = sample->read.one.value;
2168 array++;
2169
2170 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) {
2171 *array = sample->read.time_enabled;
2172 array++;
2173 }
2174
2175 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) {
2176 *array = sample->read.time_running;
2177 array++;
2178 }
2179
2180 /* PERF_FORMAT_ID is forced for PERF_SAMPLE_READ */
2181 if (read_format & PERF_FORMAT_GROUP) {
2182 sz = sample->read.group.nr *
2183 sizeof(struct sample_read_value);
2184 memcpy(array, sample->read.group.values, sz);
2185 array = (void *)array + sz;
2186 } else {
2187 *array = sample->read.one.id;
2188 array++;
2189 }
2190 }
2191
2192 if (type & PERF_SAMPLE_CALLCHAIN) {
2193 sz = (sample->callchain->nr + 1) * sizeof(u64);
2194 memcpy(array, sample->callchain, sz);
2195 array = (void *)array + sz;
2196 }
2197
2198 if (type & PERF_SAMPLE_RAW) {
2199 u.val32[0] = sample->raw_size;
2200 if (WARN_ONCE(swapped,
2201 "Endianness of raw data not corrected!\n")) {
2202 /*
2203 * Inverse of what is done in perf_evsel__parse_sample
2204 */
2205 u.val32[0] = bswap_32(u.val32[0]);
2206 u.val32[1] = bswap_32(u.val32[1]);
2207 u.val64 = bswap_64(u.val64);
2208 }
2209 *array = u.val64;
2210 array = (void *)array + sizeof(u32);
2211
2212 memcpy(array, sample->raw_data, sample->raw_size);
2213 array = (void *)array + sample->raw_size;
2214 }
2215
2216 if (type & PERF_SAMPLE_BRANCH_STACK) {
2217 sz = sample->branch_stack->nr * sizeof(struct branch_entry);
2218 sz += sizeof(u64);
2219 memcpy(array, sample->branch_stack, sz);
2220 array = (void *)array + sz;
2221 }
2222
2223 if (type & PERF_SAMPLE_REGS_USER) {
2224 if (sample->user_regs.abi) {
2225 *array++ = sample->user_regs.abi;
Jiri Olsa352ea452014-01-07 13:47:25 +01002226 sz = hweight_long(sample->user_regs.mask) * sizeof(u64);
Adrian Hunterd03f2172013-08-27 11:23:11 +03002227 memcpy(array, sample->user_regs.regs, sz);
2228 array = (void *)array + sz;
2229 } else {
2230 *array++ = 0;
2231 }
2232 }
2233
2234 if (type & PERF_SAMPLE_STACK_USER) {
2235 sz = sample->user_stack.size;
2236 *array++ = sz;
2237 if (sz) {
2238 memcpy(array, sample->user_stack.data, sz);
2239 array = (void *)array + sz;
2240 *array++ = sz;
2241 }
2242 }
2243
2244 if (type & PERF_SAMPLE_WEIGHT) {
2245 *array = sample->weight;
2246 array++;
2247 }
2248
2249 if (type & PERF_SAMPLE_DATA_SRC) {
2250 *array = sample->data_src;
2251 array++;
2252 }
2253
Adrian Hunter42d88912013-11-01 15:51:38 +02002254 if (type & PERF_SAMPLE_TRANSACTION) {
2255 *array = sample->transaction;
2256 array++;
2257 }
2258
Stephane Eranian6a21c0b2014-09-24 13:48:39 +02002259 if (type & PERF_SAMPLE_REGS_INTR) {
2260 if (sample->intr_regs.abi) {
2261 *array++ = sample->intr_regs.abi;
2262 sz = hweight_long(sample->intr_regs.mask) * sizeof(u64);
2263 memcpy(array, sample->intr_regs.regs, sz);
2264 array = (void *)array + sz;
2265 } else {
2266 *array++ = 0;
2267 }
2268 }
2269
Andrew Vagin74eec262011-11-28 12:03:31 +03002270 return 0;
2271}
Arnaldo Carvalho de Melo5555ded2012-09-11 19:24:23 -03002272
Arnaldo Carvalho de Meloefd2b922012-09-18 11:21:50 -03002273struct format_field *perf_evsel__field(struct perf_evsel *evsel, const char *name)
2274{
2275 return pevent_find_field(evsel->tp_format, name);
2276}
2277
Arnaldo Carvalho de Melo5d2074e2012-09-26 20:22:00 -03002278void *perf_evsel__rawptr(struct perf_evsel *evsel, struct perf_sample *sample,
Arnaldo Carvalho de Melo5555ded2012-09-11 19:24:23 -03002279 const char *name)
2280{
Arnaldo Carvalho de Meloefd2b922012-09-18 11:21:50 -03002281 struct format_field *field = perf_evsel__field(evsel, name);
Arnaldo Carvalho de Melo5555ded2012-09-11 19:24:23 -03002282 int offset;
2283
Arnaldo Carvalho de Meloefd2b922012-09-18 11:21:50 -03002284 if (!field)
2285 return NULL;
Arnaldo Carvalho de Melo5555ded2012-09-11 19:24:23 -03002286
2287 offset = field->offset;
2288
2289 if (field->flags & FIELD_IS_DYNAMIC) {
2290 offset = *(int *)(sample->raw_data + field->offset);
2291 offset &= 0xffff;
2292 }
2293
2294 return sample->raw_data + offset;
2295}
2296
Arnaldo Carvalho de Melo90525172016-05-31 12:47:46 -03002297u64 format_field__intval(struct format_field *field, struct perf_sample *sample,
2298 bool needs_swap)
Arnaldo Carvalho de Melo5555ded2012-09-11 19:24:23 -03002299{
Arnaldo Carvalho de Meloe6b6f672012-09-26 13:13:04 -03002300 u64 value;
Arnaldo Carvalho de Melo90525172016-05-31 12:47:46 -03002301 void *ptr = sample->raw_data + field->offset;
Arnaldo Carvalho de Melo5555ded2012-09-11 19:24:23 -03002302
Arnaldo Carvalho de Meloe6b6f672012-09-26 13:13:04 -03002303 switch (field->size) {
2304 case 1:
2305 return *(u8 *)ptr;
2306 case 2:
2307 value = *(u16 *)ptr;
2308 break;
2309 case 4:
2310 value = *(u32 *)ptr;
2311 break;
2312 case 8:
David Aherne94eeda2015-03-24 16:14:09 -04002313 memcpy(&value, ptr, sizeof(u64));
Arnaldo Carvalho de Meloe6b6f672012-09-26 13:13:04 -03002314 break;
2315 default:
2316 return 0;
2317 }
2318
Arnaldo Carvalho de Melo90525172016-05-31 12:47:46 -03002319 if (!needs_swap)
Arnaldo Carvalho de Meloe6b6f672012-09-26 13:13:04 -03002320 return value;
2321
2322 switch (field->size) {
2323 case 2:
2324 return bswap_16(value);
2325 case 4:
2326 return bswap_32(value);
2327 case 8:
2328 return bswap_64(value);
2329 default:
2330 return 0;
2331 }
2332
2333 return 0;
Arnaldo Carvalho de Melo5555ded2012-09-11 19:24:23 -03002334}
Arnaldo Carvalho de Melo0698aed2012-12-10 18:17:08 -03002335
Arnaldo Carvalho de Melo90525172016-05-31 12:47:46 -03002336u64 perf_evsel__intval(struct perf_evsel *evsel, struct perf_sample *sample,
2337 const char *name)
2338{
2339 struct format_field *field = perf_evsel__field(evsel, name);
2340
2341 if (!field)
2342 return 0;
2343
2344 return field ? format_field__intval(field, sample, evsel->needs_swap) : 0;
2345}
2346
Arnaldo Carvalho de Meloc0a54342012-12-13 14:16:30 -03002347bool perf_evsel__fallback(struct perf_evsel *evsel, int err,
2348 char *msg, size_t msgsize)
2349{
Arnaldo Carvalho de Melo08094822016-05-12 16:07:47 -03002350 int paranoid;
2351
David Ahern2b821cc2013-07-18 17:27:59 -06002352 if ((err == ENOENT || err == ENXIO || err == ENODEV) &&
Arnaldo Carvalho de Meloc0a54342012-12-13 14:16:30 -03002353 evsel->attr.type == PERF_TYPE_HARDWARE &&
2354 evsel->attr.config == PERF_COUNT_HW_CPU_CYCLES) {
2355 /*
2356 * If it's cycles then fall back to hrtimer based
2357 * cpu-clock-tick sw counter, which is always available even if
2358 * no PMU support.
2359 *
2360 * PPC returns ENXIO until 2.6.37 (behavior changed with commit
2361 * b0a873e).
2362 */
2363 scnprintf(msg, msgsize, "%s",
2364"The cycles event is not supported, trying to fall back to cpu-clock-ticks");
2365
2366 evsel->attr.type = PERF_TYPE_SOFTWARE;
2367 evsel->attr.config = PERF_COUNT_SW_CPU_CLOCK;
2368
Arnaldo Carvalho de Melo04662522013-12-26 17:41:15 -03002369 zfree(&evsel->name);
Arnaldo Carvalho de Meloc0a54342012-12-13 14:16:30 -03002370 return true;
Arnaldo Carvalho de Melo08094822016-05-12 16:07:47 -03002371 } else if (err == EACCES && !evsel->attr.exclude_kernel &&
2372 (paranoid = perf_event_paranoid()) > 1) {
2373 const char *name = perf_evsel__name(evsel);
2374 char *new_name;
2375
2376 if (asprintf(&new_name, "%s%su", name, strchr(name, ':') ? "" : ":") < 0)
2377 return false;
2378
2379 if (evsel->name)
2380 free(evsel->name);
2381 evsel->name = new_name;
2382 scnprintf(msg, msgsize,
2383"kernel.perf_event_paranoid=%d, trying to fall back to excluding kernel samples", paranoid);
2384 evsel->attr.exclude_kernel = 1;
2385
2386 return true;
Arnaldo Carvalho de Meloc0a54342012-12-13 14:16:30 -03002387 }
2388
2389 return false;
2390}
Arnaldo Carvalho de Melo56e52e82012-12-13 15:10:58 -03002391
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03002392int perf_evsel__open_strerror(struct perf_evsel *evsel, struct target *target,
Arnaldo Carvalho de Melo56e52e82012-12-13 15:10:58 -03002393 int err, char *msg, size_t size)
2394{
Masami Hiramatsu6e81c742014-08-14 02:22:36 +00002395 char sbuf[STRERR_BUFSIZE];
2396
Arnaldo Carvalho de Melo56e52e82012-12-13 15:10:58 -03002397 switch (err) {
2398 case EPERM:
2399 case EACCES:
David Ahernb69e63a2013-05-25 17:54:00 -06002400 return scnprintf(msg, size,
Ben Hutchings3379e0c2016-01-19 21:35:15 +00002401 "You may not have permission to collect %sstats.\n\n"
2402 "Consider tweaking /proc/sys/kernel/perf_event_paranoid,\n"
2403 "which controls use of the performance events system by\n"
2404 "unprivileged users (without CAP_SYS_ADMIN).\n\n"
Arnaldo Carvalho de Melo7d173912016-05-12 15:44:55 -03002405 "The current value is %d:\n\n"
Ben Hutchings3379e0c2016-01-19 21:35:15 +00002406 " -1: Allow use of (almost) all events by all users\n"
2407 ">= 0: Disallow raw tracepoint access by users without CAP_IOC_LOCK\n"
2408 ">= 1: Disallow CPU event access by users without CAP_SYS_ADMIN\n"
2409 ">= 2: Disallow kernel profiling by users without CAP_SYS_ADMIN",
Arnaldo Carvalho de Melo7d173912016-05-12 15:44:55 -03002410 target->system_wide ? "system-wide " : "",
2411 perf_event_paranoid());
Arnaldo Carvalho de Melo56e52e82012-12-13 15:10:58 -03002412 case ENOENT:
2413 return scnprintf(msg, size, "The %s event is not supported.",
2414 perf_evsel__name(evsel));
2415 case EMFILE:
2416 return scnprintf(msg, size, "%s",
2417 "Too many events are opened.\n"
Jiri Olsa18ffdfe2015-05-25 22:51:54 +02002418 "Probably the maximum number of open file descriptors has been reached.\n"
2419 "Hint: Try again after reducing the number of events.\n"
2420 "Hint: Try increasing the limit with 'ulimit -n <limit>'");
Arnaldo Carvalho de Melode46d522016-04-27 17:51:45 -03002421 case ENOMEM:
2422 if ((evsel->attr.sample_type & PERF_SAMPLE_CALLCHAIN) != 0 &&
2423 access("/proc/sys/kernel/perf_event_max_stack", F_OK) == 0)
2424 return scnprintf(msg, size,
2425 "Not enough memory to setup event with callchain.\n"
2426 "Hint: Try tweaking /proc/sys/kernel/perf_event_max_stack\n"
2427 "Hint: Current value: %d", sysctl_perf_event_max_stack);
2428 break;
Arnaldo Carvalho de Melo56e52e82012-12-13 15:10:58 -03002429 case ENODEV:
2430 if (target->cpu_list)
2431 return scnprintf(msg, size, "%s",
Arnaldo Carvalho de Melo81d64f42016-04-27 17:56:53 -03002432 "No such device - did you specify an out-of-range profile CPU?");
Arnaldo Carvalho de Melo56e52e82012-12-13 15:10:58 -03002433 break;
2434 case EOPNOTSUPP:
Vineet Guptadc89e752016-05-09 15:07:39 +05302435 if (evsel->attr.sample_period != 0)
2436 return scnprintf(msg, size, "%s",
2437 "PMU Hardware doesn't support sampling/overflow-interrupts.");
Arnaldo Carvalho de Melo56e52e82012-12-13 15:10:58 -03002438 if (evsel->attr.precise_ip)
2439 return scnprintf(msg, size, "%s",
2440 "\'precise\' request may not be supported. Try removing 'p' modifier.");
2441#if defined(__i386__) || defined(__x86_64__)
2442 if (evsel->attr.type == PERF_TYPE_HARDWARE)
2443 return scnprintf(msg, size, "%s",
2444 "No hardware sampling interrupt available.\n"
2445 "No APIC? If so then you can boot the kernel with the \"lapic\" boot parameter to force-enable it.");
2446#endif
2447 break;
Jiri Olsa63914ac2014-08-01 17:46:54 +02002448 case EBUSY:
2449 if (find_process("oprofiled"))
2450 return scnprintf(msg, size,
2451 "The PMU counters are busy/taken by another profiler.\n"
2452 "We found oprofile daemon running, please stop it and try again.");
2453 break;
Peter Zijlstra814c8c32015-03-31 00:19:31 +02002454 case EINVAL:
Arnaldo Carvalho de Melo32a951b2016-07-14 08:34:33 +00002455 if (evsel->attr.write_backward && perf_missing_features.write_backward)
Arnaldo Carvalho de Melo7da36e92016-06-20 10:47:18 +00002456 return scnprintf(msg, size, "Reading from overwrite event is not supported by this kernel.");
Peter Zijlstra814c8c32015-03-31 00:19:31 +02002457 if (perf_missing_features.clockid)
2458 return scnprintf(msg, size, "clockid feature not supported.");
2459 if (perf_missing_features.clockid_wrong)
2460 return scnprintf(msg, size, "wrong clockid (%d).", clockid);
2461 break;
Arnaldo Carvalho de Melo56e52e82012-12-13 15:10:58 -03002462 default:
2463 break;
2464 }
2465
2466 return scnprintf(msg, size,
Masami Hiramatsu6e81c742014-08-14 02:22:36 +00002467 "The sys_perf_event_open() syscall returned with %d (%s) for event (%s).\n"
Arnaldo Carvalho de Melo56e52e82012-12-13 15:10:58 -03002468 "/bin/dmesg may provide additional information.\n"
Arnaldo Carvalho de Melo81d64f42016-04-27 17:56:53 -03002469 "No CONFIG_PERF_EVENTS=y kernel support configured?",
Arnaldo Carvalho de Meloc8b5f2c2016-07-06 11:56:20 -03002470 err, str_error_r(err, sbuf, sizeof(sbuf)),
Masami Hiramatsu6e81c742014-08-14 02:22:36 +00002471 perf_evsel__name(evsel));
Arnaldo Carvalho de Melo56e52e82012-12-13 15:10:58 -03002472}
Ravi Bangoriaf4e47f92016-06-30 11:44:19 +05302473
2474char *perf_evsel__env_arch(struct perf_evsel *evsel)
2475{
2476 if (evsel && evsel->evlist && evsel->evlist->env)
2477 return evsel->evlist->env->arch;
2478 return NULL;
2479}