blob: ef2a31f6dd066453babd13abb8123c1c0e28365f [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"
Andi Kleenac12f672016-10-12 14:02:06 -070031#include "util/parse-branch-options.h"
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -020032
Arnaldo Carvalho de Melo594ac612012-12-13 13:13:07 -030033static struct {
34 bool sample_id_all;
35 bool exclude_guest;
Stephane Eranian5c5e8542013-08-21 12:10:25 +020036 bool mmap2;
Yann Droneaud57480d22014-06-30 22:28:47 +020037 bool cloexec;
Peter Zijlstra814c8c32015-03-31 00:19:31 +020038 bool clockid;
39 bool clockid_wrong;
Andi Kleenbd0f8892015-12-11 16:12:24 -080040 bool lbr_flags;
Wang Nanb90dc172016-05-20 16:38:23 +000041 bool write_backward;
Arnaldo Carvalho de Melo594ac612012-12-13 13:13:07 -030042} perf_missing_features;
43
Peter Zijlstra814c8c32015-03-31 00:19:31 +020044static clockid_t clockid;
45
Arnaldo Carvalho de Meloce8ccff2014-10-09 15:29:51 -030046static int perf_evsel__no_extra_init(struct perf_evsel *evsel __maybe_unused)
47{
48 return 0;
49}
50
51static void perf_evsel__no_extra_fini(struct perf_evsel *evsel __maybe_unused)
52{
53}
54
55static struct {
56 size_t size;
57 int (*init)(struct perf_evsel *evsel);
58 void (*fini)(struct perf_evsel *evsel);
59} perf_evsel__object = {
60 .size = sizeof(struct perf_evsel),
61 .init = perf_evsel__no_extra_init,
62 .fini = perf_evsel__no_extra_fini,
63};
64
65int perf_evsel__object_config(size_t object_size,
66 int (*init)(struct perf_evsel *evsel),
67 void (*fini)(struct perf_evsel *evsel))
68{
69
70 if (object_size == 0)
71 goto set_methods;
72
73 if (perf_evsel__object.size > object_size)
74 return -EINVAL;
75
76 perf_evsel__object.size = object_size;
77
78set_methods:
79 if (init != NULL)
80 perf_evsel__object.init = init;
81
82 if (fini != NULL)
83 perf_evsel__object.fini = fini;
84
85 return 0;
86}
87
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -020088#define FD(e, x, y) (*(int *)xyarray__entry(e->fd, x, y))
89
Adrian Hunter75562572013-08-27 11:23:09 +030090int __perf_evsel__sample_size(u64 sample_type)
Arnaldo Carvalho de Meloc2a70652011-06-02 11:04:54 -030091{
92 u64 mask = sample_type & PERF_SAMPLE_MASK;
93 int size = 0;
94 int i;
95
96 for (i = 0; i < 64; i++) {
97 if (mask & (1ULL << i))
98 size++;
99 }
100
101 size *= sizeof(u64);
102
103 return size;
104}
105
Adrian Hunter75562572013-08-27 11:23:09 +0300106/**
107 * __perf_evsel__calc_id_pos - calculate id_pos.
108 * @sample_type: sample type
109 *
110 * This function returns the position of the event id (PERF_SAMPLE_ID or
111 * PERF_SAMPLE_IDENTIFIER) in a sample event i.e. in the array of struct
112 * sample_event.
113 */
114static int __perf_evsel__calc_id_pos(u64 sample_type)
115{
116 int idx = 0;
117
118 if (sample_type & PERF_SAMPLE_IDENTIFIER)
119 return 0;
120
121 if (!(sample_type & PERF_SAMPLE_ID))
122 return -1;
123
124 if (sample_type & PERF_SAMPLE_IP)
125 idx += 1;
126
127 if (sample_type & PERF_SAMPLE_TID)
128 idx += 1;
129
130 if (sample_type & PERF_SAMPLE_TIME)
131 idx += 1;
132
133 if (sample_type & PERF_SAMPLE_ADDR)
134 idx += 1;
135
136 return idx;
137}
138
139/**
140 * __perf_evsel__calc_is_pos - calculate is_pos.
141 * @sample_type: sample type
142 *
143 * This function returns the position (counting backwards) of the event id
144 * (PERF_SAMPLE_ID or PERF_SAMPLE_IDENTIFIER) in a non-sample event i.e. if
145 * sample_id_all is used there is an id sample appended to non-sample events.
146 */
147static int __perf_evsel__calc_is_pos(u64 sample_type)
148{
149 int idx = 1;
150
151 if (sample_type & PERF_SAMPLE_IDENTIFIER)
152 return 1;
153
154 if (!(sample_type & PERF_SAMPLE_ID))
155 return -1;
156
157 if (sample_type & PERF_SAMPLE_CPU)
158 idx += 1;
159
160 if (sample_type & PERF_SAMPLE_STREAM_ID)
161 idx += 1;
162
163 return idx;
164}
165
166void perf_evsel__calc_id_pos(struct perf_evsel *evsel)
167{
168 evsel->id_pos = __perf_evsel__calc_id_pos(evsel->attr.sample_type);
169 evsel->is_pos = __perf_evsel__calc_is_pos(evsel->attr.sample_type);
170}
171
Arnaldo Carvalho de Melo7be5ebe2012-12-10 14:53:43 -0300172void __perf_evsel__set_sample_bit(struct perf_evsel *evsel,
173 enum perf_event_sample_format bit)
174{
175 if (!(evsel->attr.sample_type & bit)) {
176 evsel->attr.sample_type |= bit;
177 evsel->sample_size += sizeof(u64);
Adrian Hunter75562572013-08-27 11:23:09 +0300178 perf_evsel__calc_id_pos(evsel);
Arnaldo Carvalho de Melo7be5ebe2012-12-10 14:53:43 -0300179 }
180}
181
182void __perf_evsel__reset_sample_bit(struct perf_evsel *evsel,
183 enum perf_event_sample_format bit)
184{
185 if (evsel->attr.sample_type & bit) {
186 evsel->attr.sample_type &= ~bit;
187 evsel->sample_size -= sizeof(u64);
Adrian Hunter75562572013-08-27 11:23:09 +0300188 perf_evsel__calc_id_pos(evsel);
Arnaldo Carvalho de Melo7be5ebe2012-12-10 14:53:43 -0300189 }
190}
191
Adrian Hunter75562572013-08-27 11:23:09 +0300192void perf_evsel__set_sample_id(struct perf_evsel *evsel,
193 bool can_sample_identifier)
Arnaldo Carvalho de Melo7a5a5ca2012-12-10 15:21:30 -0300194{
Adrian Hunter75562572013-08-27 11:23:09 +0300195 if (can_sample_identifier) {
196 perf_evsel__reset_sample_bit(evsel, ID);
197 perf_evsel__set_sample_bit(evsel, IDENTIFIER);
198 } else {
199 perf_evsel__set_sample_bit(evsel, ID);
200 }
Arnaldo Carvalho de Melo7a5a5ca2012-12-10 15:21:30 -0300201 evsel->attr.read_format |= PERF_FORMAT_ID;
202}
203
Arnaldo Carvalho de Melo5496bc02016-07-07 11:51:47 -0300204/**
205 * perf_evsel__is_function_event - Return whether given evsel is a function
206 * trace event
207 *
208 * @evsel - evsel selector to be tested
209 *
210 * Return %true if event is function trace event
211 */
212bool perf_evsel__is_function_event(struct perf_evsel *evsel)
213{
214#define FUNCTION_EVENT "ftrace:function"
215
216 return evsel->name &&
217 !strncmp(FUNCTION_EVENT, evsel->name, sizeof(FUNCTION_EVENT));
218
219#undef FUNCTION_EVENT
220}
221
Arnaldo Carvalho de Meloef1d1af2011-01-18 21:41:45 -0200222void perf_evsel__init(struct perf_evsel *evsel,
223 struct perf_event_attr *attr, int idx)
224{
225 evsel->idx = idx;
Adrian Hunter60b08962014-07-31 09:00:52 +0300226 evsel->tracking = !idx;
Arnaldo Carvalho de Meloef1d1af2011-01-18 21:41:45 -0200227 evsel->attr = *attr;
Namhyung Kim2cfda562012-11-29 15:38:29 +0900228 evsel->leader = evsel;
Stephane Eranian410136f2013-11-12 17:58:49 +0100229 evsel->unit = "";
230 evsel->scale = 1.0;
Arnaldo Carvalho de Melod49e4692015-08-27 08:07:40 -0400231 evsel->evlist = NULL;
Wang Nan1f45b1d2015-10-14 12:41:18 +0000232 evsel->bpf_fd = -1;
Arnaldo Carvalho de Meloef1d1af2011-01-18 21:41:45 -0200233 INIT_LIST_HEAD(&evsel->node);
Jiri Olsa930a2e22015-07-29 05:42:10 -0400234 INIT_LIST_HEAD(&evsel->config_terms);
Arnaldo Carvalho de Meloce8ccff2014-10-09 15:29:51 -0300235 perf_evsel__object.init(evsel);
Arnaldo Carvalho de Melobde09462012-08-01 18:53:11 -0300236 evsel->sample_size = __perf_evsel__sample_size(attr->sample_type);
Adrian Hunter75562572013-08-27 11:23:09 +0300237 perf_evsel__calc_id_pos(evsel);
Wang Nan15bfd2c2015-07-10 07:36:09 +0000238 evsel->cmdline_group_boundary = false;
Andi Kleen37932c12017-03-20 13:17:08 -0700239 evsel->metric_expr = NULL;
240 evsel->metric_events = NULL;
241 evsel->collect_stat = false;
Arnaldo Carvalho de Meloef1d1af2011-01-18 21:41:45 -0200242}
243
Arnaldo Carvalho de Meloef503832013-11-07 16:41:19 -0300244struct perf_evsel *perf_evsel__new_idx(struct perf_event_attr *attr, int idx)
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200245{
Arnaldo Carvalho de Meloce8ccff2014-10-09 15:29:51 -0300246 struct perf_evsel *evsel = zalloc(perf_evsel__object.size);
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200247
Arnaldo Carvalho de Meloef1d1af2011-01-18 21:41:45 -0200248 if (evsel != NULL)
249 perf_evsel__init(evsel, attr, idx);
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200250
Wang Nan03e0a7d2016-02-22 09:10:37 +0000251 if (perf_evsel__is_bpf_output(evsel)) {
Wang Nand37ba882016-04-01 13:26:42 +0000252 evsel->attr.sample_type |= (PERF_SAMPLE_RAW | PERF_SAMPLE_TIME |
253 PERF_SAMPLE_CPU | PERF_SAMPLE_PERIOD),
Wang Nan03e0a7d2016-02-22 09:10:37 +0000254 evsel->attr.sample_period = 1;
255 }
256
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200257 return evsel;
258}
259
Arnaldo Carvalho de Melo7c48dcf2016-07-28 18:33:20 -0300260struct perf_evsel *perf_evsel__new_cycles(void)
261{
262 struct perf_event_attr attr = {
263 .type = PERF_TYPE_HARDWARE,
264 .config = PERF_COUNT_HW_CPU_CYCLES,
265 };
266 struct perf_evsel *evsel;
267
268 event_attr_init(&attr);
269
270 perf_event_attr__set_max_precise_ip(&attr);
271
272 evsel = perf_evsel__new(&attr);
273 if (evsel == NULL)
274 goto out;
275
276 /* use asprintf() because free(evsel) assumes name is allocated */
277 if (asprintf(&evsel->name, "cycles%.*s",
278 attr.precise_ip ? attr.precise_ip + 1 : 0, ":ppp") < 0)
279 goto error_free;
280out:
281 return evsel;
282error_free:
283 perf_evsel__delete(evsel);
284 evsel = NULL;
285 goto out;
286}
287
Jiri Olsa8dd2a132015-09-07 10:38:06 +0200288/*
289 * Returns pointer with encoded error via <linux/err.h> interface.
290 */
Arnaldo Carvalho de Meloef503832013-11-07 16:41:19 -0300291struct perf_evsel *perf_evsel__newtp_idx(const char *sys, const char *name, int idx)
Arnaldo Carvalho de Meloefd2b922012-09-18 11:21:50 -0300292{
Arnaldo Carvalho de Meloce8ccff2014-10-09 15:29:51 -0300293 struct perf_evsel *evsel = zalloc(perf_evsel__object.size);
Jiri Olsa8dd2a132015-09-07 10:38:06 +0200294 int err = -ENOMEM;
Arnaldo Carvalho de Meloefd2b922012-09-18 11:21:50 -0300295
Jiri Olsa8dd2a132015-09-07 10:38:06 +0200296 if (evsel == NULL) {
297 goto out_err;
298 } else {
Arnaldo Carvalho de Meloefd2b922012-09-18 11:21:50 -0300299 struct perf_event_attr attr = {
Arnaldo Carvalho de Melo0b80f8b32012-09-26 12:28:26 -0300300 .type = PERF_TYPE_TRACEPOINT,
301 .sample_type = (PERF_SAMPLE_RAW | PERF_SAMPLE_TIME |
302 PERF_SAMPLE_CPU | PERF_SAMPLE_PERIOD),
Arnaldo Carvalho de Meloefd2b922012-09-18 11:21:50 -0300303 };
304
Arnaldo Carvalho de Meloe48ffe22012-09-26 17:11:38 -0300305 if (asprintf(&evsel->name, "%s:%s", sys, name) < 0)
306 goto out_free;
307
Jiri Olsa97978b32013-12-03 14:09:24 +0100308 evsel->tp_format = trace_event__tp_format(sys, name);
Jiri Olsa8dd2a132015-09-07 10:38:06 +0200309 if (IS_ERR(evsel->tp_format)) {
310 err = PTR_ERR(evsel->tp_format);
Arnaldo Carvalho de Meloefd2b922012-09-18 11:21:50 -0300311 goto out_free;
Jiri Olsa8dd2a132015-09-07 10:38:06 +0200312 }
Arnaldo Carvalho de Meloefd2b922012-09-18 11:21:50 -0300313
Arnaldo Carvalho de Melo0b80f8b32012-09-26 12:28:26 -0300314 event_attr_init(&attr);
Arnaldo Carvalho de Meloefd2b922012-09-18 11:21:50 -0300315 attr.config = evsel->tp_format->id;
Arnaldo Carvalho de Melo0b80f8b32012-09-26 12:28:26 -0300316 attr.sample_period = 1;
Arnaldo Carvalho de Meloefd2b922012-09-18 11:21:50 -0300317 perf_evsel__init(evsel, &attr, idx);
Arnaldo Carvalho de Meloefd2b922012-09-18 11:21:50 -0300318 }
319
320 return evsel;
321
322out_free:
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -0300323 zfree(&evsel->name);
Arnaldo Carvalho de Meloefd2b922012-09-18 11:21:50 -0300324 free(evsel);
Jiri Olsa8dd2a132015-09-07 10:38:06 +0200325out_err:
326 return ERR_PTR(err);
Arnaldo Carvalho de Meloefd2b922012-09-18 11:21:50 -0300327}
328
Arnaldo Carvalho de Melo8ad70132012-09-06 13:11:18 -0300329const char *perf_evsel__hw_names[PERF_COUNT_HW_MAX] = {
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300330 "cycles",
331 "instructions",
332 "cache-references",
333 "cache-misses",
334 "branches",
335 "branch-misses",
336 "bus-cycles",
337 "stalled-cycles-frontend",
338 "stalled-cycles-backend",
339 "ref-cycles",
340};
341
Arnaldo Carvalho de Melodd4f5222012-06-13 15:52:42 -0300342static const char *__perf_evsel__hw_name(u64 config)
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300343{
344 if (config < PERF_COUNT_HW_MAX && perf_evsel__hw_names[config])
345 return perf_evsel__hw_names[config];
346
347 return "unknown-hardware";
348}
349
Arnaldo Carvalho de Melo27f18612012-06-11 13:33:09 -0300350static int perf_evsel__add_modifiers(struct perf_evsel *evsel, char *bf, size_t size)
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300351{
Arnaldo Carvalho de Melo27f18612012-06-11 13:33:09 -0300352 int colon = 0, r = 0;
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300353 struct perf_event_attr *attr = &evsel->attr;
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300354 bool exclude_guest_default = false;
355
356#define MOD_PRINT(context, mod) do { \
357 if (!attr->exclude_##context) { \
Arnaldo Carvalho de Melo27f18612012-06-11 13:33:09 -0300358 if (!colon) colon = ++r; \
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300359 r += scnprintf(bf + r, size - r, "%c", mod); \
360 } } while(0)
361
362 if (attr->exclude_kernel || attr->exclude_user || attr->exclude_hv) {
363 MOD_PRINT(kernel, 'k');
364 MOD_PRINT(user, 'u');
365 MOD_PRINT(hv, 'h');
366 exclude_guest_default = true;
367 }
368
369 if (attr->precise_ip) {
370 if (!colon)
Arnaldo Carvalho de Melo27f18612012-06-11 13:33:09 -0300371 colon = ++r;
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300372 r += scnprintf(bf + r, size - r, "%.*s", attr->precise_ip, "ppp");
373 exclude_guest_default = true;
374 }
375
376 if (attr->exclude_host || attr->exclude_guest == exclude_guest_default) {
377 MOD_PRINT(host, 'H');
378 MOD_PRINT(guest, 'G');
379 }
380#undef MOD_PRINT
381 if (colon)
Arnaldo Carvalho de Melo27f18612012-06-11 13:33:09 -0300382 bf[colon - 1] = ':';
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300383 return r;
384}
385
Arnaldo Carvalho de Melo27f18612012-06-11 13:33:09 -0300386static int perf_evsel__hw_name(struct perf_evsel *evsel, char *bf, size_t size)
387{
388 int r = scnprintf(bf, size, "%s", __perf_evsel__hw_name(evsel->attr.config));
389 return r + perf_evsel__add_modifiers(evsel, bf + r, size - r);
390}
391
Arnaldo Carvalho de Melo8ad70132012-09-06 13:11:18 -0300392const char *perf_evsel__sw_names[PERF_COUNT_SW_MAX] = {
Arnaldo Carvalho de Melo335c2f52012-06-11 14:36:20 -0300393 "cpu-clock",
394 "task-clock",
395 "page-faults",
396 "context-switches",
Arnaldo Carvalho de Melo8ad70132012-09-06 13:11:18 -0300397 "cpu-migrations",
Arnaldo Carvalho de Melo335c2f52012-06-11 14:36:20 -0300398 "minor-faults",
399 "major-faults",
400 "alignment-faults",
401 "emulation-faults",
Adrian Hunterd22d1a22013-08-31 21:50:52 +0300402 "dummy",
Arnaldo Carvalho de Melo335c2f52012-06-11 14:36:20 -0300403};
404
Arnaldo Carvalho de Melodd4f5222012-06-13 15:52:42 -0300405static const char *__perf_evsel__sw_name(u64 config)
Arnaldo Carvalho de Melo335c2f52012-06-11 14:36:20 -0300406{
407 if (config < PERF_COUNT_SW_MAX && perf_evsel__sw_names[config])
408 return perf_evsel__sw_names[config];
409 return "unknown-software";
410}
411
412static int perf_evsel__sw_name(struct perf_evsel *evsel, char *bf, size_t size)
413{
414 int r = scnprintf(bf, size, "%s", __perf_evsel__sw_name(evsel->attr.config));
415 return r + perf_evsel__add_modifiers(evsel, bf + r, size - r);
416}
417
Jiri Olsa287e74a2012-06-28 23:18:49 +0200418static int __perf_evsel__bp_name(char *bf, size_t size, u64 addr, u64 type)
419{
420 int r;
421
422 r = scnprintf(bf, size, "mem:0x%" PRIx64 ":", addr);
423
424 if (type & HW_BREAKPOINT_R)
425 r += scnprintf(bf + r, size - r, "r");
426
427 if (type & HW_BREAKPOINT_W)
428 r += scnprintf(bf + r, size - r, "w");
429
430 if (type & HW_BREAKPOINT_X)
431 r += scnprintf(bf + r, size - r, "x");
432
433 return r;
434}
435
436static int perf_evsel__bp_name(struct perf_evsel *evsel, char *bf, size_t size)
437{
438 struct perf_event_attr *attr = &evsel->attr;
439 int r = __perf_evsel__bp_name(bf, size, attr->bp_addr, attr->bp_type);
440 return r + perf_evsel__add_modifiers(evsel, bf + r, size - r);
441}
442
Arnaldo Carvalho de Melo0b668bc2012-06-11 14:08:07 -0300443const char *perf_evsel__hw_cache[PERF_COUNT_HW_CACHE_MAX]
444 [PERF_EVSEL__MAX_ALIASES] = {
445 { "L1-dcache", "l1-d", "l1d", "L1-data", },
446 { "L1-icache", "l1-i", "l1i", "L1-instruction", },
447 { "LLC", "L2", },
448 { "dTLB", "d-tlb", "Data-TLB", },
449 { "iTLB", "i-tlb", "Instruction-TLB", },
450 { "branch", "branches", "bpu", "btb", "bpc", },
451 { "node", },
452};
453
454const char *perf_evsel__hw_cache_op[PERF_COUNT_HW_CACHE_OP_MAX]
455 [PERF_EVSEL__MAX_ALIASES] = {
456 { "load", "loads", "read", },
457 { "store", "stores", "write", },
458 { "prefetch", "prefetches", "speculative-read", "speculative-load", },
459};
460
461const char *perf_evsel__hw_cache_result[PERF_COUNT_HW_CACHE_RESULT_MAX]
462 [PERF_EVSEL__MAX_ALIASES] = {
463 { "refs", "Reference", "ops", "access", },
464 { "misses", "miss", },
465};
466
467#define C(x) PERF_COUNT_HW_CACHE_##x
468#define CACHE_READ (1 << C(OP_READ))
469#define CACHE_WRITE (1 << C(OP_WRITE))
470#define CACHE_PREFETCH (1 << C(OP_PREFETCH))
471#define COP(x) (1 << x)
472
473/*
474 * cache operartion stat
475 * L1I : Read and prefetch only
476 * ITLB and BPU : Read-only
477 */
478static unsigned long perf_evsel__hw_cache_stat[C(MAX)] = {
479 [C(L1D)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
480 [C(L1I)] = (CACHE_READ | CACHE_PREFETCH),
481 [C(LL)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
482 [C(DTLB)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
483 [C(ITLB)] = (CACHE_READ),
484 [C(BPU)] = (CACHE_READ),
485 [C(NODE)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
486};
487
488bool perf_evsel__is_cache_op_valid(u8 type, u8 op)
489{
490 if (perf_evsel__hw_cache_stat[type] & COP(op))
491 return true; /* valid */
492 else
493 return false; /* invalid */
494}
495
496int __perf_evsel__hw_cache_type_op_res_name(u8 type, u8 op, u8 result,
497 char *bf, size_t size)
498{
499 if (result) {
500 return scnprintf(bf, size, "%s-%s-%s", perf_evsel__hw_cache[type][0],
501 perf_evsel__hw_cache_op[op][0],
502 perf_evsel__hw_cache_result[result][0]);
503 }
504
505 return scnprintf(bf, size, "%s-%s", perf_evsel__hw_cache[type][0],
506 perf_evsel__hw_cache_op[op][1]);
507}
508
Arnaldo Carvalho de Melodd4f5222012-06-13 15:52:42 -0300509static int __perf_evsel__hw_cache_name(u64 config, char *bf, size_t size)
Arnaldo Carvalho de Melo0b668bc2012-06-11 14:08:07 -0300510{
511 u8 op, result, type = (config >> 0) & 0xff;
512 const char *err = "unknown-ext-hardware-cache-type";
513
Arnaldo Carvalho de Meloc53412e2016-08-18 16:30:28 -0300514 if (type >= PERF_COUNT_HW_CACHE_MAX)
Arnaldo Carvalho de Melo0b668bc2012-06-11 14:08:07 -0300515 goto out_err;
516
517 op = (config >> 8) & 0xff;
518 err = "unknown-ext-hardware-cache-op";
Arnaldo Carvalho de Meloc53412e2016-08-18 16:30:28 -0300519 if (op >= PERF_COUNT_HW_CACHE_OP_MAX)
Arnaldo Carvalho de Melo0b668bc2012-06-11 14:08:07 -0300520 goto out_err;
521
522 result = (config >> 16) & 0xff;
523 err = "unknown-ext-hardware-cache-result";
Arnaldo Carvalho de Meloc53412e2016-08-18 16:30:28 -0300524 if (result >= PERF_COUNT_HW_CACHE_RESULT_MAX)
Arnaldo Carvalho de Melo0b668bc2012-06-11 14:08:07 -0300525 goto out_err;
526
527 err = "invalid-cache";
528 if (!perf_evsel__is_cache_op_valid(type, op))
529 goto out_err;
530
531 return __perf_evsel__hw_cache_type_op_res_name(type, op, result, bf, size);
532out_err:
533 return scnprintf(bf, size, "%s", err);
534}
535
536static int perf_evsel__hw_cache_name(struct perf_evsel *evsel, char *bf, size_t size)
537{
538 int ret = __perf_evsel__hw_cache_name(evsel->attr.config, bf, size);
539 return ret + perf_evsel__add_modifiers(evsel, bf + ret, size - ret);
540}
541
Arnaldo Carvalho de Melo6eef3d92012-06-13 11:53:37 -0300542static int perf_evsel__raw_name(struct perf_evsel *evsel, char *bf, size_t size)
543{
544 int ret = scnprintf(bf, size, "raw 0x%" PRIx64, evsel->attr.config);
545 return ret + perf_evsel__add_modifiers(evsel, bf + ret, size - ret);
546}
547
Arnaldo Carvalho de Melo7289f832012-06-12 12:34:58 -0300548const char *perf_evsel__name(struct perf_evsel *evsel)
Arnaldo Carvalho de Meloa4460832012-06-12 10:29:12 -0300549{
Arnaldo Carvalho de Melo7289f832012-06-12 12:34:58 -0300550 char bf[128];
Arnaldo Carvalho de Meloa4460832012-06-12 10:29:12 -0300551
Arnaldo Carvalho de Melo7289f832012-06-12 12:34:58 -0300552 if (evsel->name)
553 return evsel->name;
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300554
555 switch (evsel->attr.type) {
556 case PERF_TYPE_RAW:
Arnaldo Carvalho de Melo6eef3d92012-06-13 11:53:37 -0300557 perf_evsel__raw_name(evsel, bf, sizeof(bf));
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300558 break;
559
560 case PERF_TYPE_HARDWARE:
Arnaldo Carvalho de Melo7289f832012-06-12 12:34:58 -0300561 perf_evsel__hw_name(evsel, bf, sizeof(bf));
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300562 break;
Arnaldo Carvalho de Melo0b668bc2012-06-11 14:08:07 -0300563
564 case PERF_TYPE_HW_CACHE:
Arnaldo Carvalho de Melo7289f832012-06-12 12:34:58 -0300565 perf_evsel__hw_cache_name(evsel, bf, sizeof(bf));
Arnaldo Carvalho de Melo0b668bc2012-06-11 14:08:07 -0300566 break;
567
Arnaldo Carvalho de Melo335c2f52012-06-11 14:36:20 -0300568 case PERF_TYPE_SOFTWARE:
Arnaldo Carvalho de Melo7289f832012-06-12 12:34:58 -0300569 perf_evsel__sw_name(evsel, bf, sizeof(bf));
Arnaldo Carvalho de Melo335c2f52012-06-11 14:36:20 -0300570 break;
571
Arnaldo Carvalho de Meloa4460832012-06-12 10:29:12 -0300572 case PERF_TYPE_TRACEPOINT:
Arnaldo Carvalho de Melo7289f832012-06-12 12:34:58 -0300573 scnprintf(bf, sizeof(bf), "%s", "unknown tracepoint");
Arnaldo Carvalho de Meloa4460832012-06-12 10:29:12 -0300574 break;
575
Jiri Olsa287e74a2012-06-28 23:18:49 +0200576 case PERF_TYPE_BREAKPOINT:
577 perf_evsel__bp_name(evsel, bf, sizeof(bf));
578 break;
579
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300580 default:
Robert Richterca1b1452012-08-16 21:10:18 +0200581 scnprintf(bf, sizeof(bf), "unknown attr type: %d",
582 evsel->attr.type);
Arnaldo Carvalho de Meloa4460832012-06-12 10:29:12 -0300583 break;
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300584 }
585
Arnaldo Carvalho de Melo7289f832012-06-12 12:34:58 -0300586 evsel->name = strdup(bf);
587
588 return evsel->name ?: "unknown";
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300589}
590
Namhyung Kim717e2632013-01-22 18:09:44 +0900591const char *perf_evsel__group_name(struct perf_evsel *evsel)
592{
593 return evsel->group_name ?: "anon group";
594}
595
596int perf_evsel__group_desc(struct perf_evsel *evsel, char *buf, size_t size)
597{
598 int ret;
599 struct perf_evsel *pos;
600 const char *group_name = perf_evsel__group_name(evsel);
601
602 ret = scnprintf(buf, size, "%s", group_name);
603
604 ret += scnprintf(buf + ret, size - ret, " { %s",
605 perf_evsel__name(evsel));
606
607 for_each_group_member(pos, evsel)
608 ret += scnprintf(buf + ret, size - ret, ", %s",
609 perf_evsel__name(pos));
610
611 ret += scnprintf(buf + ret, size - ret, " }");
612
613 return ret;
614}
615
Arnaldo Carvalho de Melo01e0d502016-04-11 18:39:37 -0300616void perf_evsel__config_callchain(struct perf_evsel *evsel,
617 struct record_opts *opts,
618 struct callchain_param *param)
Jiri Olsa6bedfab2014-03-02 16:56:40 +0100619{
620 bool function = perf_evsel__is_function_event(evsel);
621 struct perf_event_attr *attr = &evsel->attr;
622
623 perf_evsel__set_sample_bit(evsel, CALLCHAIN);
624
Arnaldo Carvalho de Melo792d48b2016-04-28 19:03:42 -0300625 attr->sample_max_stack = param->max_stack;
626
Kan Liangc3a6a8c2015-08-04 04:30:20 -0400627 if (param->record_mode == CALLCHAIN_LBR) {
Kan Liangaad2b212015-01-05 13:23:04 -0500628 if (!opts->branch_stack) {
629 if (attr->exclude_user) {
630 pr_warning("LBR callstack option is only available "
631 "to get user callchain information. "
632 "Falling back to framepointers.\n");
633 } else {
634 perf_evsel__set_sample_bit(evsel, BRANCH_STACK);
635 attr->branch_sample_type = PERF_SAMPLE_BRANCH_USER |
Andi Kleenbd0f8892015-12-11 16:12:24 -0800636 PERF_SAMPLE_BRANCH_CALL_STACK |
637 PERF_SAMPLE_BRANCH_NO_CYCLES |
638 PERF_SAMPLE_BRANCH_NO_FLAGS;
Kan Liangaad2b212015-01-05 13:23:04 -0500639 }
640 } else
641 pr_warning("Cannot use LBR callstack with branch stack. "
642 "Falling back to framepointers.\n");
643 }
644
Kan Liangc3a6a8c2015-08-04 04:30:20 -0400645 if (param->record_mode == CALLCHAIN_DWARF) {
Jiri Olsa6bedfab2014-03-02 16:56:40 +0100646 if (!function) {
647 perf_evsel__set_sample_bit(evsel, REGS_USER);
648 perf_evsel__set_sample_bit(evsel, STACK_USER);
649 attr->sample_regs_user = PERF_REGS_MASK;
Kan Liangc3a6a8c2015-08-04 04:30:20 -0400650 attr->sample_stack_user = param->dump_size;
Jiri Olsa6bedfab2014-03-02 16:56:40 +0100651 attr->exclude_callchain_user = 1;
652 } else {
653 pr_info("Cannot use DWARF unwind for function trace event,"
654 " falling back to framepointers.\n");
655 }
656 }
657
658 if (function) {
659 pr_info("Disabling user space callchains for function trace event.\n");
660 attr->exclude_callchain_user = 1;
661 }
662}
663
Kan Liangd457c962015-08-11 06:30:47 -0400664static void
665perf_evsel__reset_callgraph(struct perf_evsel *evsel,
666 struct callchain_param *param)
667{
668 struct perf_event_attr *attr = &evsel->attr;
669
670 perf_evsel__reset_sample_bit(evsel, CALLCHAIN);
671 if (param->record_mode == CALLCHAIN_LBR) {
672 perf_evsel__reset_sample_bit(evsel, BRANCH_STACK);
673 attr->branch_sample_type &= ~(PERF_SAMPLE_BRANCH_USER |
674 PERF_SAMPLE_BRANCH_CALL_STACK);
675 }
676 if (param->record_mode == CALLCHAIN_DWARF) {
677 perf_evsel__reset_sample_bit(evsel, REGS_USER);
678 perf_evsel__reset_sample_bit(evsel, STACK_USER);
679 }
680}
681
682static void apply_config_terms(struct perf_evsel *evsel,
683 struct record_opts *opts)
Jiri Olsa930a2e22015-07-29 05:42:10 -0400684{
685 struct perf_evsel_config_term *term;
Kan Liang32067712015-08-04 04:30:19 -0400686 struct list_head *config_terms = &evsel->config_terms;
687 struct perf_event_attr *attr = &evsel->attr;
Kan Liangd457c962015-08-11 06:30:47 -0400688 struct callchain_param param;
689 u32 dump_size = 0;
Arnaldo Carvalho de Melo792d48b2016-04-28 19:03:42 -0300690 int max_stack = 0;
691 const char *callgraph_buf = NULL;
Kan Liangd457c962015-08-11 06:30:47 -0400692
693 /* callgraph default */
694 param.record_mode = callchain_param.record_mode;
Jiri Olsa930a2e22015-07-29 05:42:10 -0400695
696 list_for_each_entry(term, config_terms, list) {
697 switch (term->type) {
Jiri Olsaee4c7582015-07-29 05:42:11 -0400698 case PERF_EVSEL__CONFIG_TERM_PERIOD:
699 attr->sample_period = term->val.period;
Jiri Olsaab35a7d2015-08-08 19:12:10 +0200700 attr->freq = 0;
Kan Liang32067712015-08-04 04:30:19 -0400701 break;
Namhyung Kim09af2a52015-08-09 15:45:23 +0900702 case PERF_EVSEL__CONFIG_TERM_FREQ:
703 attr->sample_freq = term->val.freq;
704 attr->freq = 1;
705 break;
Kan Liang32067712015-08-04 04:30:19 -0400706 case PERF_EVSEL__CONFIG_TERM_TIME:
707 if (term->val.time)
708 perf_evsel__set_sample_bit(evsel, TIME);
709 else
710 perf_evsel__reset_sample_bit(evsel, TIME);
711 break;
Kan Liangd457c962015-08-11 06:30:47 -0400712 case PERF_EVSEL__CONFIG_TERM_CALLGRAPH:
713 callgraph_buf = term->val.callgraph;
714 break;
Andi Kleenac12f672016-10-12 14:02:06 -0700715 case PERF_EVSEL__CONFIG_TERM_BRANCH:
716 if (term->val.branch && strcmp(term->val.branch, "no")) {
717 perf_evsel__set_sample_bit(evsel, BRANCH_STACK);
718 parse_branch_str(term->val.branch,
719 &attr->branch_sample_type);
720 } else
721 perf_evsel__reset_sample_bit(evsel, BRANCH_STACK);
722 break;
Kan Liangd457c962015-08-11 06:30:47 -0400723 case PERF_EVSEL__CONFIG_TERM_STACK_USER:
724 dump_size = term->val.stack_user;
725 break;
Arnaldo Carvalho de Melo792d48b2016-04-28 19:03:42 -0300726 case PERF_EVSEL__CONFIG_TERM_MAX_STACK:
727 max_stack = term->val.max_stack;
728 break;
Wang Nan374ce932015-10-28 10:55:02 +0000729 case PERF_EVSEL__CONFIG_TERM_INHERIT:
730 /*
731 * attr->inherit should has already been set by
732 * perf_evsel__config. If user explicitly set
733 * inherit using config terms, override global
734 * opt->no_inherit setting.
735 */
736 attr->inherit = term->val.inherit ? 1 : 0;
737 break;
Wang Nan626a6b72016-07-14 08:34:45 +0000738 case PERF_EVSEL__CONFIG_TERM_OVERWRITE:
739 attr->write_backward = term->val.overwrite ? 1 : 0;
740 break;
Jiri Olsa930a2e22015-07-29 05:42:10 -0400741 default:
742 break;
743 }
744 }
Kan Liangd457c962015-08-11 06:30:47 -0400745
746 /* User explicitly set per-event callgraph, clear the old setting and reset. */
Arnaldo Carvalho de Melo792d48b2016-04-28 19:03:42 -0300747 if ((callgraph_buf != NULL) || (dump_size > 0) || max_stack) {
748 if (max_stack) {
749 param.max_stack = max_stack;
750 if (callgraph_buf == NULL)
751 callgraph_buf = "fp";
752 }
Kan Liangd457c962015-08-11 06:30:47 -0400753
754 /* parse callgraph parameters */
755 if (callgraph_buf != NULL) {
Kan Liangf9db0d02015-08-11 06:30:48 -0400756 if (!strcmp(callgraph_buf, "no")) {
757 param.enabled = false;
758 param.record_mode = CALLCHAIN_NONE;
759 } else {
760 param.enabled = true;
761 if (parse_callchain_record(callgraph_buf, &param)) {
762 pr_err("per-event callgraph setting for %s failed. "
763 "Apply callgraph global setting for it\n",
764 evsel->name);
765 return;
766 }
Kan Liangd457c962015-08-11 06:30:47 -0400767 }
768 }
769 if (dump_size > 0) {
770 dump_size = round_up(dump_size, sizeof(u64));
771 param.dump_size = dump_size;
772 }
773
774 /* If global callgraph set, clear it */
775 if (callchain_param.enabled)
776 perf_evsel__reset_callgraph(evsel, &callchain_param);
777
778 /* set perf-event callgraph */
779 if (param.enabled)
Arnaldo Carvalho de Melo01e0d502016-04-11 18:39:37 -0300780 perf_evsel__config_callchain(evsel, opts, &param);
Kan Liangd457c962015-08-11 06:30:47 -0400781 }
Jiri Olsa930a2e22015-07-29 05:42:10 -0400782}
783
Jiri Olsa774cb492012-11-12 18:34:01 +0100784/*
785 * The enable_on_exec/disabled value strategy:
786 *
787 * 1) For any type of traced program:
788 * - all independent events and group leaders are disabled
789 * - all group members are enabled
790 *
791 * Group members are ruled by group leaders. They need to
792 * be enabled, because the group scheduling relies on that.
793 *
794 * 2) For traced programs executed by perf:
795 * - all independent events and group leaders have
796 * enable_on_exec set
797 * - we don't specifically enable or disable any event during
798 * the record command
799 *
800 * Independent events and group leaders are initially disabled
801 * and get enabled by exec. Group members are ruled by group
802 * leaders as stated in 1).
803 *
804 * 3) For traced programs attached by perf (pid/tid):
805 * - we specifically enable or disable all events during
806 * the record command
807 *
808 * When attaching events to already running traced we
809 * enable/disable events specifically, as there's no
810 * initial traced exec call.
811 */
Arnaldo Carvalho de Meloe68ae9c2016-04-11 18:15:29 -0300812void perf_evsel__config(struct perf_evsel *evsel, struct record_opts *opts,
813 struct callchain_param *callchain)
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200814{
Jiri Olsa3c176312012-10-10 17:39:03 +0200815 struct perf_evsel *leader = evsel->leader;
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200816 struct perf_event_attr *attr = &evsel->attr;
Adrian Hunter60b08962014-07-31 09:00:52 +0300817 int track = evsel->tracking;
Adrian Hunter3aa59392013-11-15 15:52:29 +0200818 bool per_cpu = opts->target.default_per_cpu && !opts->target.per_thread;
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200819
Arnaldo Carvalho de Melo594ac612012-12-13 13:13:07 -0300820 attr->sample_id_all = perf_missing_features.sample_id_all ? 0 : 1;
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200821 attr->inherit = !opts->no_inherit;
Wang Nan626a6b72016-07-14 08:34:45 +0000822 attr->write_backward = opts->overwrite ? 1 : 0;
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200823
Arnaldo Carvalho de Melo7be5ebe2012-12-10 14:53:43 -0300824 perf_evsel__set_sample_bit(evsel, IP);
825 perf_evsel__set_sample_bit(evsel, TID);
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200826
Jiri Olsa3c176312012-10-10 17:39:03 +0200827 if (evsel->sample_read) {
828 perf_evsel__set_sample_bit(evsel, READ);
829
830 /*
831 * We need ID even in case of single event, because
832 * PERF_SAMPLE_READ process ID specific data.
833 */
Adrian Hunter75562572013-08-27 11:23:09 +0300834 perf_evsel__set_sample_id(evsel, false);
Jiri Olsa3c176312012-10-10 17:39:03 +0200835
836 /*
837 * Apply group format only if we belong to group
838 * with more than one members.
839 */
840 if (leader->nr_members > 1) {
841 attr->read_format |= PERF_FORMAT_GROUP;
842 attr->inherit = 0;
843 }
844 }
845
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200846 /*
Namhyung Kim17314e22014-06-09 14:43:37 +0900847 * We default some events to have a default interval. But keep
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200848 * it a weak assumption overridable by the user.
849 */
Namhyung Kim17314e22014-06-09 14:43:37 +0900850 if (!attr->sample_period || (opts->user_freq != UINT_MAX ||
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200851 opts->user_interval != ULLONG_MAX)) {
852 if (opts->freq) {
Arnaldo Carvalho de Melo7be5ebe2012-12-10 14:53:43 -0300853 perf_evsel__set_sample_bit(evsel, PERIOD);
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200854 attr->freq = 1;
855 attr->sample_freq = opts->freq;
856 } else {
857 attr->sample_period = opts->default_interval;
858 }
859 }
860
Jiri Olsa3c176312012-10-10 17:39:03 +0200861 /*
862 * Disable sampling for all group members other
863 * than leader in case leader 'leads' the sampling.
864 */
865 if ((leader != evsel) && leader->sample_read) {
866 attr->sample_freq = 0;
867 attr->sample_period = 0;
868 }
869
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200870 if (opts->no_samples)
871 attr->sample_freq = 0;
872
873 if (opts->inherit_stat)
874 attr->inherit_stat = 1;
875
876 if (opts->sample_address) {
Arnaldo Carvalho de Melo7be5ebe2012-12-10 14:53:43 -0300877 perf_evsel__set_sample_bit(evsel, ADDR);
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200878 attr->mmap_data = track;
879 }
880
Jiri Olsaf1403732014-11-13 18:21:03 +0100881 /*
882 * We don't allow user space callchains for function trace
883 * event, due to issues with page faults while tracing page
884 * fault handler and its overall trickiness nature.
885 */
886 if (perf_evsel__is_function_event(evsel))
887 evsel->attr.exclude_callchain_user = 1;
888
Arnaldo Carvalho de Meloe68ae9c2016-04-11 18:15:29 -0300889 if (callchain && callchain->enabled && !evsel->no_aux_samples)
Arnaldo Carvalho de Melo01e0d502016-04-11 18:39:37 -0300890 perf_evsel__config_callchain(evsel, opts, callchain);
Jiri Olsa26d33022012-08-07 15:20:47 +0200891
Stephane Eranian6a21c0b2014-09-24 13:48:39 +0200892 if (opts->sample_intr_regs) {
Stephane Eranianbcc84ec2015-08-31 18:41:12 +0200893 attr->sample_regs_intr = opts->sample_intr_regs;
Stephane Eranian6a21c0b2014-09-24 13:48:39 +0200894 perf_evsel__set_sample_bit(evsel, REGS_INTR);
895 }
896
Jiri Olsab6f35ed2016-08-01 20:02:35 +0200897 if (target__has_cpu(&opts->target) || opts->sample_cpu)
Arnaldo Carvalho de Melo7be5ebe2012-12-10 14:53:43 -0300898 perf_evsel__set_sample_bit(evsel, CPU);
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200899
Andrew Vagin3e76ac72011-12-20 17:32:45 +0300900 if (opts->period)
Arnaldo Carvalho de Melo7be5ebe2012-12-10 14:53:43 -0300901 perf_evsel__set_sample_bit(evsel, PERIOD);
Andrew Vagin3e76ac72011-12-20 17:32:45 +0300902
Andi Kleen8affc2b2014-07-31 14:45:04 +0800903 /*
Adam Buchbinderbd1a0be52016-02-24 10:02:25 -0800904 * When the user explicitly disabled time don't force it here.
Andi Kleen8affc2b2014-07-31 14:45:04 +0800905 */
906 if (opts->sample_time &&
907 (!perf_missing_features.sample_id_all &&
Adrian Hunter3abebc52015-07-06 14:51:01 +0300908 (!opts->no_inherit || target__has_cpu(&opts->target) || per_cpu ||
909 opts->sample_time_set)))
Arnaldo Carvalho de Melo7be5ebe2012-12-10 14:53:43 -0300910 perf_evsel__set_sample_bit(evsel, TIME);
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200911
Adrian Hunter6ff1ce72014-07-14 13:02:56 +0300912 if (opts->raw_samples && !evsel->no_aux_samples) {
Arnaldo Carvalho de Melo7be5ebe2012-12-10 14:53:43 -0300913 perf_evsel__set_sample_bit(evsel, TIME);
914 perf_evsel__set_sample_bit(evsel, RAW);
915 perf_evsel__set_sample_bit(evsel, CPU);
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200916 }
917
Stephane Eranianccf49bf2013-01-24 16:10:37 +0100918 if (opts->sample_address)
Adrian Hunter1e7ed5e2013-11-01 15:51:35 +0200919 perf_evsel__set_sample_bit(evsel, DATA_SRC);
Stephane Eranianccf49bf2013-01-24 16:10:37 +0100920
Arnaldo Carvalho de Melo509051e2014-01-14 17:52:14 -0300921 if (opts->no_buffering) {
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200922 attr->watermark = 0;
923 attr->wakeup_events = 1;
924 }
Adrian Hunter6ff1ce72014-07-14 13:02:56 +0300925 if (opts->branch_stack && !evsel->no_aux_samples) {
Arnaldo Carvalho de Melo7be5ebe2012-12-10 14:53:43 -0300926 perf_evsel__set_sample_bit(evsel, BRANCH_STACK);
Roberto Agostino Vitillobdfebd82012-02-09 23:21:02 +0100927 attr->branch_sample_type = opts->branch_stack;
928 }
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200929
Andi Kleen05484292013-01-24 16:10:29 +0100930 if (opts->sample_weight)
Adrian Hunter1e7ed5e2013-11-01 15:51:35 +0200931 perf_evsel__set_sample_bit(evsel, WEIGHT);
Andi Kleen05484292013-01-24 16:10:29 +0100932
Namhyung Kim62e503b2015-01-29 17:06:46 +0900933 attr->task = track;
Stephane Eranian5c5e8542013-08-21 12:10:25 +0200934 attr->mmap = track;
Don Zickusa5a5ba72014-05-30 10:49:42 -0400935 attr->mmap2 = track && !perf_missing_features.mmap2;
Stephane Eranian5c5e8542013-08-21 12:10:25 +0200936 attr->comm = track;
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200937
Hari Bathinif3b36142017-03-08 02:11:43 +0530938 if (opts->record_namespaces)
939 attr->namespaces = track;
940
Adrian Hunterb757bb02015-07-21 12:44:04 +0300941 if (opts->record_switch_events)
942 attr->context_switch = track;
943
Andi Kleen475eeab2013-09-20 07:40:43 -0700944 if (opts->sample_transaction)
Adrian Hunter1e7ed5e2013-11-01 15:51:35 +0200945 perf_evsel__set_sample_bit(evsel, TRANSACTION);
Andi Kleen475eeab2013-09-20 07:40:43 -0700946
Andi Kleen85c273d2015-02-24 15:13:40 -0800947 if (opts->running_time) {
948 evsel->attr.read_format |=
949 PERF_FORMAT_TOTAL_TIME_ENABLED |
950 PERF_FORMAT_TOTAL_TIME_RUNNING;
951 }
952
Jiri Olsa774cb492012-11-12 18:34:01 +0100953 /*
954 * XXX see the function comment above
955 *
956 * Disabling only independent events or group leaders,
957 * keeping group members enabled.
958 */
Namhyung Kim823254e2012-11-29 15:38:30 +0900959 if (perf_evsel__is_group_leader(evsel))
Jiri Olsa774cb492012-11-12 18:34:01 +0100960 attr->disabled = 1;
961
962 /*
963 * Setting enable_on_exec for independent events and
964 * group leaders for traced executed by perf.
965 */
Andi Kleen6619a532014-01-11 13:38:27 -0800966 if (target__none(&opts->target) && perf_evsel__is_group_leader(evsel) &&
967 !opts->initial_delay)
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200968 attr->enable_on_exec = 1;
Adrian Hunter2afd2bc2014-07-14 13:02:57 +0300969
970 if (evsel->immediate) {
971 attr->disabled = 0;
972 attr->enable_on_exec = 0;
973 }
Peter Zijlstra814c8c32015-03-31 00:19:31 +0200974
975 clockid = opts->clockid;
976 if (opts->use_clockid) {
977 attr->use_clockid = 1;
978 attr->clockid = opts->clockid;
979 }
Jiri Olsa930a2e22015-07-29 05:42:10 -0400980
Jiri Olsa7f94af72015-10-05 20:06:05 +0200981 if (evsel->precise_max)
982 perf_event_attr__set_max_precise_ip(attr);
983
Jiri Olsa85723882016-02-15 09:34:31 +0100984 if (opts->all_user) {
985 attr->exclude_kernel = 1;
986 attr->exclude_user = 0;
987 }
988
989 if (opts->all_kernel) {
990 attr->exclude_kernel = 0;
991 attr->exclude_user = 1;
992 }
993
Jiri Olsa930a2e22015-07-29 05:42:10 -0400994 /*
995 * Apply event specific term settings,
996 * it overloads any global configuration.
997 */
Kan Liangd457c962015-08-11 06:30:47 -0400998 apply_config_terms(evsel, opts);
Jiri Olsaa359c172016-12-13 08:46:22 +0100999
1000 evsel->ignore_missing_thread = opts->ignore_missing_thread;
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -02001001}
1002
Arnaldo Carvalho de Melo88858462014-10-13 13:30:27 -03001003static int perf_evsel__alloc_fd(struct perf_evsel *evsel, int ncpus, int nthreads)
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -02001004{
Adrian Hunterbf8e8f42014-07-31 09:00:51 +03001005 if (evsel->system_wide)
1006 nthreads = 1;
1007
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -02001008 evsel->fd = xyarray__new(ncpus, nthreads, sizeof(int));
David Ahern4af4c952011-05-27 09:58:34 -06001009
1010 if (evsel->fd) {
Arnaldo Carvalho de Melo18ef15c2016-10-03 11:07:24 -03001011 int cpu, thread;
David Ahern4af4c952011-05-27 09:58:34 -06001012 for (cpu = 0; cpu < ncpus; cpu++) {
1013 for (thread = 0; thread < nthreads; thread++) {
1014 FD(evsel, cpu, thread) = -1;
1015 }
1016 }
1017 }
1018
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -02001019 return evsel->fd != NULL ? 0 : -ENOMEM;
1020}
1021
Andi Kleene2407be2013-08-02 17:41:10 -07001022static int perf_evsel__run_ioctl(struct perf_evsel *evsel, int ncpus, int nthreads,
1023 int ioc, void *arg)
Arnaldo Carvalho de Melo745cefc2012-09-26 15:07:39 -03001024{
1025 int cpu, thread;
1026
Adrian Hunterbf8e8f42014-07-31 09:00:51 +03001027 if (evsel->system_wide)
1028 nthreads = 1;
1029
Arnaldo Carvalho de Melo745cefc2012-09-26 15:07:39 -03001030 for (cpu = 0; cpu < ncpus; cpu++) {
1031 for (thread = 0; thread < nthreads; thread++) {
1032 int fd = FD(evsel, cpu, thread),
Andi Kleene2407be2013-08-02 17:41:10 -07001033 err = ioctl(fd, ioc, arg);
Arnaldo Carvalho de Melo745cefc2012-09-26 15:07:39 -03001034
1035 if (err)
1036 return err;
1037 }
1038 }
1039
1040 return 0;
1041}
1042
Arnaldo Carvalho de Melof47805a2015-07-03 15:53:49 -03001043int perf_evsel__apply_filter(struct perf_evsel *evsel, int ncpus, int nthreads,
1044 const char *filter)
Andi Kleene2407be2013-08-02 17:41:10 -07001045{
1046 return perf_evsel__run_ioctl(evsel, ncpus, nthreads,
1047 PERF_EVENT_IOC_SET_FILTER,
1048 (void *)filter);
1049}
1050
Arnaldo Carvalho de Melo12467ae2015-07-03 17:05:50 -03001051int perf_evsel__set_filter(struct perf_evsel *evsel, const char *filter)
1052{
1053 char *new_filter = strdup(filter);
1054
1055 if (new_filter != NULL) {
1056 free(evsel->filter);
1057 evsel->filter = new_filter;
1058 return 0;
1059 }
1060
1061 return -1;
1062}
1063
Mathieu Poirier3541c032016-09-16 08:44:04 -06001064static int perf_evsel__append_filter(struct perf_evsel *evsel,
1065 const char *fmt, const char *filter)
Arnaldo Carvalho de Melo64ec84f2015-07-04 12:19:13 -03001066{
1067 char *new_filter;
1068
1069 if (evsel->filter == NULL)
1070 return perf_evsel__set_filter(evsel, filter);
1071
Mathieu Poirierb15d0a42016-09-16 08:44:03 -06001072 if (asprintf(&new_filter, fmt, evsel->filter, filter) > 0) {
Arnaldo Carvalho de Melo64ec84f2015-07-04 12:19:13 -03001073 free(evsel->filter);
1074 evsel->filter = new_filter;
1075 return 0;
1076 }
1077
1078 return -1;
1079}
1080
Mathieu Poirier3541c032016-09-16 08:44:04 -06001081int perf_evsel__append_tp_filter(struct perf_evsel *evsel, const char *filter)
1082{
1083 return perf_evsel__append_filter(evsel, "(%s) && (%s)", filter);
1084}
1085
Mathieu Poirier1e857482016-09-16 08:44:05 -06001086int perf_evsel__append_addr_filter(struct perf_evsel *evsel, const char *filter)
1087{
1088 return perf_evsel__append_filter(evsel, "%s,%s", filter);
1089}
1090
Jiri Olsa5cd95fc2015-12-03 10:06:40 +01001091int perf_evsel__enable(struct perf_evsel *evsel)
Andi Kleene2407be2013-08-02 17:41:10 -07001092{
Jiri Olsa5cd95fc2015-12-03 10:06:40 +01001093 int nthreads = thread_map__nr(evsel->threads);
1094 int ncpus = cpu_map__nr(evsel->cpus);
1095
Andi Kleene2407be2013-08-02 17:41:10 -07001096 return perf_evsel__run_ioctl(evsel, ncpus, nthreads,
1097 PERF_EVENT_IOC_ENABLE,
1098 0);
1099}
1100
Jiri Olsae98a4cb2015-12-03 10:06:41 +01001101int perf_evsel__disable(struct perf_evsel *evsel)
1102{
1103 int nthreads = thread_map__nr(evsel->threads);
1104 int ncpus = cpu_map__nr(evsel->cpus);
1105
1106 return perf_evsel__run_ioctl(evsel, ncpus, nthreads,
1107 PERF_EVENT_IOC_DISABLE,
1108 0);
1109}
1110
Arnaldo Carvalho de Melo70db7532011-01-12 22:39:13 -02001111int perf_evsel__alloc_id(struct perf_evsel *evsel, int ncpus, int nthreads)
1112{
Vineet Gupta8d9cbd82015-01-13 19:13:23 +05301113 if (ncpus == 0 || nthreads == 0)
1114 return 0;
1115
Adrian Hunterbf8e8f42014-07-31 09:00:51 +03001116 if (evsel->system_wide)
1117 nthreads = 1;
1118
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03001119 evsel->sample_id = xyarray__new(ncpus, nthreads, sizeof(struct perf_sample_id));
1120 if (evsel->sample_id == NULL)
1121 return -ENOMEM;
1122
1123 evsel->id = zalloc(ncpus * nthreads * sizeof(u64));
1124 if (evsel->id == NULL) {
1125 xyarray__delete(evsel->sample_id);
1126 evsel->sample_id = NULL;
1127 return -ENOMEM;
1128 }
1129
1130 return 0;
Arnaldo Carvalho de Melo70db7532011-01-12 22:39:13 -02001131}
1132
Arnaldo Carvalho de Melo88858462014-10-13 13:30:27 -03001133static void perf_evsel__free_fd(struct perf_evsel *evsel)
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -02001134{
1135 xyarray__delete(evsel->fd);
1136 evsel->fd = NULL;
1137}
1138
Arnaldo Carvalho de Melo88858462014-10-13 13:30:27 -03001139static void perf_evsel__free_id(struct perf_evsel *evsel)
Arnaldo Carvalho de Melo70db7532011-01-12 22:39:13 -02001140{
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03001141 xyarray__delete(evsel->sample_id);
1142 evsel->sample_id = NULL;
Arnaldo Carvalho de Melo04662522013-12-26 17:41:15 -03001143 zfree(&evsel->id);
Arnaldo Carvalho de Melo70db7532011-01-12 22:39:13 -02001144}
1145
Jiri Olsa930a2e22015-07-29 05:42:10 -04001146static void perf_evsel__free_config_terms(struct perf_evsel *evsel)
1147{
1148 struct perf_evsel_config_term *term, *h;
1149
1150 list_for_each_entry_safe(term, h, &evsel->config_terms, list) {
1151 list_del(&term->list);
1152 free(term);
1153 }
1154}
1155
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -02001156void perf_evsel__close_fd(struct perf_evsel *evsel, int ncpus, int nthreads)
1157{
1158 int cpu, thread;
1159
Adrian Hunterbf8e8f42014-07-31 09:00:51 +03001160 if (evsel->system_wide)
1161 nthreads = 1;
1162
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -02001163 for (cpu = 0; cpu < ncpus; cpu++)
1164 for (thread = 0; thread < nthreads; ++thread) {
1165 close(FD(evsel, cpu, thread));
1166 FD(evsel, cpu, thread) = -1;
1167 }
1168}
1169
Arnaldo Carvalho de Meloef1d1af2011-01-18 21:41:45 -02001170void perf_evsel__exit(struct perf_evsel *evsel)
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -02001171{
1172 assert(list_empty(&evsel->node));
Arnaldo Carvalho de Melod49e4692015-08-27 08:07:40 -04001173 assert(evsel->evlist == NULL);
Namhyung Kim736b05a2013-03-15 14:48:49 +09001174 perf_evsel__free_fd(evsel);
1175 perf_evsel__free_id(evsel);
Jiri Olsa930a2e22015-07-29 05:42:10 -04001176 perf_evsel__free_config_terms(evsel);
Arnaldo Carvalho de Melo597e48c2014-10-16 13:25:01 -03001177 close_cgroup(evsel->cgrp);
Jiri Olsaf30a79b2015-06-23 00:36:04 +02001178 cpu_map__put(evsel->cpus);
Adrian Hunterfce4d292015-09-08 10:58:55 +03001179 cpu_map__put(evsel->own_cpus);
Jiri Olsa578e91e2015-06-23 00:36:07 +02001180 thread_map__put(evsel->threads);
Arnaldo Carvalho de Melo597e48c2014-10-16 13:25:01 -03001181 zfree(&evsel->group_name);
Arnaldo Carvalho de Melo597e48c2014-10-16 13:25:01 -03001182 zfree(&evsel->name);
Arnaldo Carvalho de Meloce8ccff2014-10-09 15:29:51 -03001183 perf_evsel__object.fini(evsel);
Arnaldo Carvalho de Meloef1d1af2011-01-18 21:41:45 -02001184}
1185
1186void perf_evsel__delete(struct perf_evsel *evsel)
1187{
1188 perf_evsel__exit(evsel);
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -02001189 free(evsel);
1190}
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -02001191
Jiri Olsaa6fa0032015-06-26 11:29:11 +02001192void perf_evsel__compute_deltas(struct perf_evsel *evsel, int cpu, int thread,
Jiri Olsa857a94a2014-11-21 10:31:05 +01001193 struct perf_counts_values *count)
Stephane Eranianc7a79c42013-01-29 12:47:43 +01001194{
1195 struct perf_counts_values tmp;
1196
1197 if (!evsel->prev_raw_counts)
1198 return;
1199
1200 if (cpu == -1) {
1201 tmp = evsel->prev_raw_counts->aggr;
1202 evsel->prev_raw_counts->aggr = *count;
1203 } else {
Jiri Olsaa6fa0032015-06-26 11:29:11 +02001204 tmp = *perf_counts(evsel->prev_raw_counts, cpu, thread);
1205 *perf_counts(evsel->prev_raw_counts, cpu, thread) = *count;
Stephane Eranianc7a79c42013-01-29 12:47:43 +01001206 }
1207
1208 count->val = count->val - tmp.val;
1209 count->ena = count->ena - tmp.ena;
1210 count->run = count->run - tmp.run;
1211}
1212
Jiri Olsa13112bb2014-11-21 10:31:06 +01001213void perf_counts_values__scale(struct perf_counts_values *count,
1214 bool scale, s8 *pscaled)
1215{
1216 s8 scaled = 0;
1217
1218 if (scale) {
1219 if (count->run == 0) {
1220 scaled = -1;
1221 count->val = 0;
1222 } else if (count->run < count->ena) {
1223 scaled = 1;
1224 count->val = (u64)((double) count->val * count->ena / count->run + 0.5);
1225 }
1226 } else
1227 count->ena = count->run = 0;
1228
1229 if (pscaled)
1230 *pscaled = scaled;
1231}
1232
Jiri Olsaf99f47192015-06-26 11:29:18 +02001233int perf_evsel__read(struct perf_evsel *evsel, int cpu, int thread,
1234 struct perf_counts_values *count)
1235{
1236 memset(count, 0, sizeof(*count));
1237
1238 if (FD(evsel, cpu, thread) < 0)
1239 return -EINVAL;
1240
1241 if (readn(FD(evsel, cpu, thread), count, sizeof(*count)) < 0)
1242 return -errno;
1243
1244 return 0;
1245}
1246
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -02001247int __perf_evsel__read_on_cpu(struct perf_evsel *evsel,
1248 int cpu, int thread, bool scale)
1249{
1250 struct perf_counts_values count;
1251 size_t nv = scale ? 3 : 1;
1252
1253 if (FD(evsel, cpu, thread) < 0)
1254 return -EINVAL;
1255
Jiri Olsaa6fa0032015-06-26 11:29:11 +02001256 if (evsel->counts == NULL && perf_evsel__alloc_counts(evsel, cpu + 1, thread + 1) < 0)
Arnaldo Carvalho de Melo4eed11d2011-01-04 00:13:17 -02001257 return -ENOMEM;
1258
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -02001259 if (readn(FD(evsel, cpu, thread), &count, nv * sizeof(u64)) < 0)
1260 return -errno;
1261
Jiri Olsaa6fa0032015-06-26 11:29:11 +02001262 perf_evsel__compute_deltas(evsel, cpu, thread, &count);
Jiri Olsa13112bb2014-11-21 10:31:06 +01001263 perf_counts_values__scale(&count, scale, NULL);
Jiri Olsaa6fa0032015-06-26 11:29:11 +02001264 *perf_counts(evsel->counts, cpu, thread) = count;
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -02001265 return 0;
1266}
1267
Jiri Olsa6a4bb042012-08-08 12:22:36 +02001268static int get_group_fd(struct perf_evsel *evsel, int cpu, int thread)
1269{
1270 struct perf_evsel *leader = evsel->leader;
1271 int fd;
1272
Namhyung Kim823254e2012-11-29 15:38:30 +09001273 if (perf_evsel__is_group_leader(evsel))
Jiri Olsa6a4bb042012-08-08 12:22:36 +02001274 return -1;
1275
1276 /*
1277 * Leader must be already processed/open,
1278 * if not it's a bug.
1279 */
1280 BUG_ON(!leader->fd);
1281
1282 fd = FD(leader, cpu, thread);
1283 BUG_ON(fd == -1);
1284
1285 return fd;
1286}
1287
Peter Zijlstra2c5e8c52015-04-07 11:09:54 +02001288struct bit_names {
1289 int bit;
1290 const char *name;
1291};
Adrian Huntere3e1a542013-08-14 15:48:24 +03001292
Peter Zijlstra2c5e8c52015-04-07 11:09:54 +02001293static void __p_bits(char *buf, size_t size, u64 value, struct bit_names *bits)
Adrian Huntere3e1a542013-08-14 15:48:24 +03001294{
Peter Zijlstra2c5e8c52015-04-07 11:09:54 +02001295 bool first_bit = true;
1296 int i = 0;
Adrian Huntere3e1a542013-08-14 15:48:24 +03001297
Peter Zijlstra2c5e8c52015-04-07 11:09:54 +02001298 do {
1299 if (value & bits[i].bit) {
1300 buf += scnprintf(buf, size, "%s%s", first_bit ? "" : "|", bits[i].name);
1301 first_bit = false;
1302 }
1303 } while (bits[++i].name != NULL);
1304}
Adrian Huntere3e1a542013-08-14 15:48:24 +03001305
Peter Zijlstra2c5e8c52015-04-07 11:09:54 +02001306static void __p_sample_type(char *buf, size_t size, u64 value)
1307{
1308#define bit_name(n) { PERF_SAMPLE_##n, #n }
1309 struct bit_names bits[] = {
1310 bit_name(IP), bit_name(TID), bit_name(TIME), bit_name(ADDR),
1311 bit_name(READ), bit_name(CALLCHAIN), bit_name(ID), bit_name(CPU),
1312 bit_name(PERIOD), bit_name(STREAM_ID), bit_name(RAW),
1313 bit_name(BRANCH_STACK), bit_name(REGS_USER), bit_name(STACK_USER),
Jiri Olsa844225922015-10-05 20:06:02 +02001314 bit_name(IDENTIFIER), bit_name(REGS_INTR), bit_name(DATA_SRC),
Jiri Olsadcdd1842015-11-25 16:36:55 +01001315 bit_name(WEIGHT),
Peter Zijlstra2c5e8c52015-04-07 11:09:54 +02001316 { .name = NULL, }
1317 };
1318#undef bit_name
1319 __p_bits(buf, size, value, bits);
1320}
Adrian Huntere3e1a542013-08-14 15:48:24 +03001321
Arnaldo Carvalho de Meloa213b922016-04-25 16:45:29 -03001322static void __p_branch_sample_type(char *buf, size_t size, u64 value)
1323{
1324#define bit_name(n) { PERF_SAMPLE_BRANCH_##n, #n }
1325 struct bit_names bits[] = {
1326 bit_name(USER), bit_name(KERNEL), bit_name(HV), bit_name(ANY),
1327 bit_name(ANY_CALL), bit_name(ANY_RETURN), bit_name(IND_CALL),
1328 bit_name(ABORT_TX), bit_name(IN_TX), bit_name(NO_TX),
1329 bit_name(COND), bit_name(CALL_STACK), bit_name(IND_JUMP),
1330 bit_name(CALL), bit_name(NO_FLAGS), bit_name(NO_CYCLES),
1331 { .name = NULL, }
1332 };
1333#undef bit_name
1334 __p_bits(buf, size, value, bits);
1335}
1336
Peter Zijlstra2c5e8c52015-04-07 11:09:54 +02001337static void __p_read_format(char *buf, size_t size, u64 value)
1338{
1339#define bit_name(n) { PERF_FORMAT_##n, #n }
1340 struct bit_names bits[] = {
1341 bit_name(TOTAL_TIME_ENABLED), bit_name(TOTAL_TIME_RUNNING),
1342 bit_name(ID), bit_name(GROUP),
1343 { .name = NULL, }
1344 };
1345#undef bit_name
1346 __p_bits(buf, size, value, bits);
1347}
Peter Zijlstra814c8c32015-03-31 00:19:31 +02001348
Peter Zijlstra2c5e8c52015-04-07 11:09:54 +02001349#define BUF_SIZE 1024
Adrian Huntere3e1a542013-08-14 15:48:24 +03001350
Adrian Hunter7310aed2015-06-11 15:51:04 +03001351#define p_hex(val) snprintf(buf, BUF_SIZE, "%#"PRIx64, (uint64_t)(val))
Peter Zijlstra2c5e8c52015-04-07 11:09:54 +02001352#define p_unsigned(val) snprintf(buf, BUF_SIZE, "%"PRIu64, (uint64_t)(val))
1353#define p_signed(val) snprintf(buf, BUF_SIZE, "%"PRId64, (int64_t)(val))
1354#define p_sample_type(val) __p_sample_type(buf, BUF_SIZE, val)
Arnaldo Carvalho de Meloa213b922016-04-25 16:45:29 -03001355#define p_branch_sample_type(val) __p_branch_sample_type(buf, BUF_SIZE, val)
Peter Zijlstra2c5e8c52015-04-07 11:09:54 +02001356#define p_read_format(val) __p_read_format(buf, BUF_SIZE, val)
Adrian Huntere3e1a542013-08-14 15:48:24 +03001357
Peter Zijlstra2c5e8c52015-04-07 11:09:54 +02001358#define PRINT_ATTRn(_n, _f, _p) \
1359do { \
1360 if (attr->_f) { \
1361 _p(attr->_f); \
1362 ret += attr__fprintf(fp, _n, buf, priv);\
1363 } \
1364} while (0)
1365
1366#define PRINT_ATTRf(_f, _p) PRINT_ATTRn(#_f, _f, _p)
1367
1368int perf_event_attr__fprintf(FILE *fp, struct perf_event_attr *attr,
1369 attr__fprintf_f attr__fprintf, void *priv)
1370{
1371 char buf[BUF_SIZE];
1372 int ret = 0;
1373
1374 PRINT_ATTRf(type, p_unsigned);
1375 PRINT_ATTRf(size, p_unsigned);
1376 PRINT_ATTRf(config, p_hex);
1377 PRINT_ATTRn("{ sample_period, sample_freq }", sample_period, p_unsigned);
1378 PRINT_ATTRf(sample_type, p_sample_type);
1379 PRINT_ATTRf(read_format, p_read_format);
1380
1381 PRINT_ATTRf(disabled, p_unsigned);
1382 PRINT_ATTRf(inherit, p_unsigned);
1383 PRINT_ATTRf(pinned, p_unsigned);
1384 PRINT_ATTRf(exclusive, p_unsigned);
1385 PRINT_ATTRf(exclude_user, p_unsigned);
1386 PRINT_ATTRf(exclude_kernel, p_unsigned);
1387 PRINT_ATTRf(exclude_hv, p_unsigned);
1388 PRINT_ATTRf(exclude_idle, p_unsigned);
1389 PRINT_ATTRf(mmap, p_unsigned);
1390 PRINT_ATTRf(comm, p_unsigned);
1391 PRINT_ATTRf(freq, p_unsigned);
1392 PRINT_ATTRf(inherit_stat, p_unsigned);
1393 PRINT_ATTRf(enable_on_exec, p_unsigned);
1394 PRINT_ATTRf(task, p_unsigned);
1395 PRINT_ATTRf(watermark, p_unsigned);
1396 PRINT_ATTRf(precise_ip, p_unsigned);
1397 PRINT_ATTRf(mmap_data, p_unsigned);
1398 PRINT_ATTRf(sample_id_all, p_unsigned);
1399 PRINT_ATTRf(exclude_host, p_unsigned);
1400 PRINT_ATTRf(exclude_guest, p_unsigned);
1401 PRINT_ATTRf(exclude_callchain_kernel, p_unsigned);
1402 PRINT_ATTRf(exclude_callchain_user, p_unsigned);
1403 PRINT_ATTRf(mmap2, p_unsigned);
1404 PRINT_ATTRf(comm_exec, p_unsigned);
1405 PRINT_ATTRf(use_clockid, p_unsigned);
Adrian Hunter02860392015-07-21 12:44:03 +03001406 PRINT_ATTRf(context_switch, p_unsigned);
Arnaldo Carvalho de Melo0a241ef2016-05-09 18:08:33 -03001407 PRINT_ATTRf(write_backward, p_unsigned);
Peter Zijlstra2c5e8c52015-04-07 11:09:54 +02001408
1409 PRINT_ATTRn("{ wakeup_events, wakeup_watermark }", wakeup_events, p_unsigned);
1410 PRINT_ATTRf(bp_type, p_unsigned);
1411 PRINT_ATTRn("{ bp_addr, config1 }", bp_addr, p_hex);
1412 PRINT_ATTRn("{ bp_len, config2 }", bp_len, p_hex);
Arnaldo Carvalho de Meloa213b922016-04-25 16:45:29 -03001413 PRINT_ATTRf(branch_sample_type, p_branch_sample_type);
Peter Zijlstra2c5e8c52015-04-07 11:09:54 +02001414 PRINT_ATTRf(sample_regs_user, p_hex);
1415 PRINT_ATTRf(sample_stack_user, p_unsigned);
1416 PRINT_ATTRf(clockid, p_signed);
1417 PRINT_ATTRf(sample_regs_intr, p_hex);
Adrian Hunter70d73de2015-04-09 18:54:06 +03001418 PRINT_ATTRf(aux_watermark, p_unsigned);
Arnaldo Carvalho de Melo792d48b2016-04-28 19:03:42 -03001419 PRINT_ATTRf(sample_max_stack, p_unsigned);
Adrian Huntere3e1a542013-08-14 15:48:24 +03001420
1421 return ret;
1422}
1423
Peter Zijlstra2c5e8c52015-04-07 11:09:54 +02001424static int __open_attr__fprintf(FILE *fp, const char *name, const char *val,
1425 void *priv __attribute__((unused)))
1426{
1427 return fprintf(fp, " %-32s %s\n", name, val);
1428}
1429
Jiri Olsaa359c172016-12-13 08:46:22 +01001430static bool ignore_missing_thread(struct perf_evsel *evsel,
1431 struct thread_map *threads,
1432 int thread, int err)
1433{
1434 if (!evsel->ignore_missing_thread)
1435 return false;
1436
1437 /* The system wide setup does not work with threads. */
1438 if (evsel->system_wide)
1439 return false;
1440
1441 /* The -ESRCH is perf event syscall errno for pid's not found. */
1442 if (err != -ESRCH)
1443 return false;
1444
1445 /* If there's only one thread, let it fail. */
1446 if (threads->nr == 1)
1447 return false;
1448
1449 if (thread_map__remove(threads, thread))
1450 return false;
1451
1452 pr_warning("WARNING: Ignored open failure for pid %d\n",
1453 thread_map__pid(threads, thread));
1454 return true;
1455}
1456
Arnaldo Carvalho de Meloc24ae6d2017-02-14 10:59:04 -03001457int perf_evsel__open(struct perf_evsel *evsel, struct cpu_map *cpus,
1458 struct thread_map *threads)
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -02001459{
Adrian Hunterbf8e8f42014-07-31 09:00:51 +03001460 int cpu, thread, nthreads;
Yann Droneaud57480d22014-06-30 22:28:47 +02001461 unsigned long flags = PERF_FLAG_FD_CLOEXEC;
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -02001462 int pid = -1, err;
Andi Kleenbec19672013-08-04 19:41:26 -07001463 enum { NO_CHANGE, SET_TO_MAX, INCREASED_MAX } set_rlimit = NO_CHANGE;
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -02001464
Arnaldo Carvalho de Melo32a951b2016-07-14 08:34:33 +00001465 if (perf_missing_features.write_backward && evsel->attr.write_backward)
1466 return -EINVAL;
1467
Arnaldo Carvalho de Meloc24ae6d2017-02-14 10:59:04 -03001468 if (cpus == NULL) {
1469 static struct cpu_map *empty_cpu_map;
1470
1471 if (empty_cpu_map == NULL) {
1472 empty_cpu_map = cpu_map__dummy_new();
1473 if (empty_cpu_map == NULL)
1474 return -ENOMEM;
1475 }
1476
1477 cpus = empty_cpu_map;
1478 }
1479
1480 if (threads == NULL) {
1481 static struct thread_map *empty_thread_map;
1482
1483 if (empty_thread_map == NULL) {
1484 empty_thread_map = thread_map__new_by_tid(-1);
1485 if (empty_thread_map == NULL)
1486 return -ENOMEM;
1487 }
1488
1489 threads = empty_thread_map;
1490 }
1491
Adrian Hunterbf8e8f42014-07-31 09:00:51 +03001492 if (evsel->system_wide)
1493 nthreads = 1;
1494 else
1495 nthreads = threads->nr;
1496
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -02001497 if (evsel->fd == NULL &&
Adrian Hunterbf8e8f42014-07-31 09:00:51 +03001498 perf_evsel__alloc_fd(evsel, cpus->nr, nthreads) < 0)
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -02001499 return -ENOMEM;
Arnaldo Carvalho de Melo4eed11d2011-01-04 00:13:17 -02001500
Stephane Eranian023695d2011-02-14 11:20:01 +02001501 if (evsel->cgrp) {
Yann Droneaud57480d22014-06-30 22:28:47 +02001502 flags |= PERF_FLAG_PID_CGROUP;
Stephane Eranian023695d2011-02-14 11:20:01 +02001503 pid = evsel->cgrp->fd;
1504 }
1505
Arnaldo Carvalho de Melo594ac612012-12-13 13:13:07 -03001506fallback_missing_features:
Peter Zijlstra814c8c32015-03-31 00:19:31 +02001507 if (perf_missing_features.clockid_wrong)
1508 evsel->attr.clockid = CLOCK_MONOTONIC; /* should always work */
1509 if (perf_missing_features.clockid) {
1510 evsel->attr.use_clockid = 0;
1511 evsel->attr.clockid = 0;
1512 }
Yann Droneaud57480d22014-06-30 22:28:47 +02001513 if (perf_missing_features.cloexec)
1514 flags &= ~(unsigned long)PERF_FLAG_FD_CLOEXEC;
Stephane Eranian5c5e8542013-08-21 12:10:25 +02001515 if (perf_missing_features.mmap2)
1516 evsel->attr.mmap2 = 0;
Arnaldo Carvalho de Melo594ac612012-12-13 13:13:07 -03001517 if (perf_missing_features.exclude_guest)
1518 evsel->attr.exclude_guest = evsel->attr.exclude_host = 0;
Andi Kleenbd0f8892015-12-11 16:12:24 -08001519 if (perf_missing_features.lbr_flags)
1520 evsel->attr.branch_sample_type &= ~(PERF_SAMPLE_BRANCH_NO_FLAGS |
1521 PERF_SAMPLE_BRANCH_NO_CYCLES);
Arnaldo Carvalho de Melo594ac612012-12-13 13:13:07 -03001522retry_sample_id:
1523 if (perf_missing_features.sample_id_all)
1524 evsel->attr.sample_id_all = 0;
1525
Peter Zijlstra2c5e8c52015-04-07 11:09:54 +02001526 if (verbose >= 2) {
1527 fprintf(stderr, "%.60s\n", graph_dotted_line);
1528 fprintf(stderr, "perf_event_attr:\n");
1529 perf_event_attr__fprintf(stderr, &evsel->attr, __open_attr__fprintf, NULL);
1530 fprintf(stderr, "%.60s\n", graph_dotted_line);
1531 }
Adrian Huntere3e1a542013-08-14 15:48:24 +03001532
Arnaldo Carvalho de Melo86bd5e82011-01-03 23:09:46 -02001533 for (cpu = 0; cpu < cpus->nr; cpu++) {
Arnaldo Carvalho de Melo9d04f172011-01-12 00:08:18 -02001534
Adrian Hunterbf8e8f42014-07-31 09:00:51 +03001535 for (thread = 0; thread < nthreads; thread++) {
Jiri Olsa83c2e4f32016-12-12 11:35:40 +01001536 int fd, group_fd;
Stephane Eranian023695d2011-02-14 11:20:01 +02001537
Adrian Hunterbf8e8f42014-07-31 09:00:51 +03001538 if (!evsel->cgrp && !evsel->system_wide)
Jiri Olsae13798c2015-06-23 00:36:02 +02001539 pid = thread_map__pid(threads, thread);
Stephane Eranian023695d2011-02-14 11:20:01 +02001540
Jiri Olsa6a4bb042012-08-08 12:22:36 +02001541 group_fd = get_group_fd(evsel, cpu, thread);
Andi Kleenbec19672013-08-04 19:41:26 -07001542retry_open:
Jiri Olsa7b4b82bc2016-11-21 22:33:26 +01001543 pr_debug2("sys_perf_event_open: pid %d cpu %d group_fd %d flags %#lx",
Adrian Huntere3e1a542013-08-14 15:48:24 +03001544 pid, cpus->map[cpu], group_fd, flags);
1545
Jiri Olsa83c2e4f32016-12-12 11:35:40 +01001546 fd = sys_perf_event_open(&evsel->attr, pid, cpus->map[cpu],
1547 group_fd, flags);
1548
1549 FD(evsel, cpu, thread) = fd;
1550
1551 if (fd < 0) {
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -02001552 err = -errno;
Jiri Olsaa359c172016-12-13 08:46:22 +01001553
1554 if (ignore_missing_thread(evsel, threads, thread, err)) {
1555 /*
1556 * We just removed 1 thread, so take a step
1557 * back on thread index and lower the upper
1558 * nthreads limit.
1559 */
1560 nthreads--;
1561 thread--;
1562
1563 /* ... and pretend like nothing have happened. */
1564 err = 0;
1565 continue;
1566 }
1567
Jiri Olsa7b4b82bc2016-11-21 22:33:26 +01001568 pr_debug2("\nsys_perf_event_open failed, error %d\n",
Adrian Hunterf852fd62013-11-01 15:51:29 +02001569 err);
Arnaldo Carvalho de Melo594ac612012-12-13 13:13:07 -03001570 goto try_fallback;
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -02001571 }
Wang Nan1f45b1d2015-10-14 12:41:18 +00001572
Jiri Olsa83c2e4f32016-12-12 11:35:40 +01001573 pr_debug2(" = %d\n", fd);
Jiri Olsa7b4b82bc2016-11-21 22:33:26 +01001574
Wang Nan1f45b1d2015-10-14 12:41:18 +00001575 if (evsel->bpf_fd >= 0) {
Jiri Olsa83c2e4f32016-12-12 11:35:40 +01001576 int evt_fd = fd;
Wang Nan1f45b1d2015-10-14 12:41:18 +00001577 int bpf_fd = evsel->bpf_fd;
1578
1579 err = ioctl(evt_fd,
1580 PERF_EVENT_IOC_SET_BPF,
1581 bpf_fd);
1582 if (err && errno != EEXIST) {
1583 pr_err("failed to attach bpf fd %d: %s\n",
1584 bpf_fd, strerror(errno));
1585 err = -EINVAL;
1586 goto out_close;
1587 }
1588 }
1589
Andi Kleenbec19672013-08-04 19:41:26 -07001590 set_rlimit = NO_CHANGE;
Peter Zijlstra814c8c32015-03-31 00:19:31 +02001591
1592 /*
1593 * If we succeeded but had to kill clockid, fail and
1594 * have perf_evsel__open_strerror() print us a nice
1595 * error.
1596 */
1597 if (perf_missing_features.clockid ||
1598 perf_missing_features.clockid_wrong) {
1599 err = -EINVAL;
1600 goto out_close;
1601 }
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -02001602 }
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -02001603 }
1604
1605 return 0;
1606
Arnaldo Carvalho de Melo594ac612012-12-13 13:13:07 -03001607try_fallback:
Andi Kleenbec19672013-08-04 19:41:26 -07001608 /*
1609 * perf stat needs between 5 and 22 fds per CPU. When we run out
1610 * of them try to increase the limits.
1611 */
1612 if (err == -EMFILE && set_rlimit < INCREASED_MAX) {
1613 struct rlimit l;
1614 int old_errno = errno;
1615
1616 if (getrlimit(RLIMIT_NOFILE, &l) == 0) {
1617 if (set_rlimit == NO_CHANGE)
1618 l.rlim_cur = l.rlim_max;
1619 else {
1620 l.rlim_cur = l.rlim_max + 1000;
1621 l.rlim_max = l.rlim_cur;
1622 }
1623 if (setrlimit(RLIMIT_NOFILE, &l) == 0) {
1624 set_rlimit++;
1625 errno = old_errno;
1626 goto retry_open;
1627 }
1628 }
1629 errno = old_errno;
1630 }
1631
Arnaldo Carvalho de Melo594ac612012-12-13 13:13:07 -03001632 if (err != -EINVAL || cpu > 0 || thread > 0)
1633 goto out_close;
1634
Peter Zijlstra814c8c32015-03-31 00:19:31 +02001635 /*
1636 * Must probe features in the order they were added to the
1637 * perf_event_attr interface.
1638 */
Arnaldo Carvalho de Melo7da36e92016-06-20 10:47:18 +00001639 if (!perf_missing_features.write_backward && evsel->attr.write_backward) {
1640 perf_missing_features.write_backward = true;
Arnaldo Carvalho de Melo32a951b2016-07-14 08:34:33 +00001641 goto out_close;
Arnaldo Carvalho de Melo7da36e92016-06-20 10:47:18 +00001642 } else if (!perf_missing_features.clockid_wrong && evsel->attr.use_clockid) {
Peter Zijlstra814c8c32015-03-31 00:19:31 +02001643 perf_missing_features.clockid_wrong = true;
1644 goto fallback_missing_features;
1645 } else if (!perf_missing_features.clockid && evsel->attr.use_clockid) {
1646 perf_missing_features.clockid = true;
1647 goto fallback_missing_features;
1648 } else if (!perf_missing_features.cloexec && (flags & PERF_FLAG_FD_CLOEXEC)) {
Yann Droneaud57480d22014-06-30 22:28:47 +02001649 perf_missing_features.cloexec = true;
1650 goto fallback_missing_features;
1651 } else if (!perf_missing_features.mmap2 && evsel->attr.mmap2) {
Stephane Eranian5c5e8542013-08-21 12:10:25 +02001652 perf_missing_features.mmap2 = true;
1653 goto fallback_missing_features;
1654 } else if (!perf_missing_features.exclude_guest &&
1655 (evsel->attr.exclude_guest || evsel->attr.exclude_host)) {
Arnaldo Carvalho de Melo594ac612012-12-13 13:13:07 -03001656 perf_missing_features.exclude_guest = true;
1657 goto fallback_missing_features;
1658 } else if (!perf_missing_features.sample_id_all) {
1659 perf_missing_features.sample_id_all = true;
1660 goto retry_sample_id;
Andi Kleenbd0f8892015-12-11 16:12:24 -08001661 } else if (!perf_missing_features.lbr_flags &&
1662 (evsel->attr.branch_sample_type &
1663 (PERF_SAMPLE_BRANCH_NO_CYCLES |
1664 PERF_SAMPLE_BRANCH_NO_FLAGS))) {
1665 perf_missing_features.lbr_flags = true;
1666 goto fallback_missing_features;
Arnaldo Carvalho de Melo594ac612012-12-13 13:13:07 -03001667 }
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -02001668out_close:
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -02001669 do {
1670 while (--thread >= 0) {
1671 close(FD(evsel, cpu, thread));
1672 FD(evsel, cpu, thread) = -1;
1673 }
Adrian Hunterbf8e8f42014-07-31 09:00:51 +03001674 thread = nthreads;
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -02001675 } while (--cpu >= 0);
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -02001676 return err;
1677}
1678
1679void perf_evsel__close(struct perf_evsel *evsel, int ncpus, int nthreads)
1680{
1681 if (evsel->fd == NULL)
1682 return;
1683
1684 perf_evsel__close_fd(evsel, ncpus, nthreads);
1685 perf_evsel__free_fd(evsel);
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -02001686}
1687
Arnaldo Carvalho de Melof08199d2011-01-11 23:42:19 -02001688int perf_evsel__open_per_cpu(struct perf_evsel *evsel,
Jiri Olsa6a4bb042012-08-08 12:22:36 +02001689 struct cpu_map *cpus)
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -02001690{
Arnaldo Carvalho de Meloc24ae6d2017-02-14 10:59:04 -03001691 return perf_evsel__open(evsel, cpus, NULL);
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -02001692}
1693
Arnaldo Carvalho de Melof08199d2011-01-11 23:42:19 -02001694int perf_evsel__open_per_thread(struct perf_evsel *evsel,
Jiri Olsa6a4bb042012-08-08 12:22:36 +02001695 struct thread_map *threads)
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -02001696{
Arnaldo Carvalho de Meloc24ae6d2017-02-14 10:59:04 -03001697 return perf_evsel__open(evsel, NULL, threads);
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -02001698}
Arnaldo Carvalho de Melo70082dd2011-01-12 17:03:24 -02001699
Arnaldo Carvalho de Melo0807d2d2012-09-26 12:48:18 -03001700static int perf_evsel__parse_id_sample(const struct perf_evsel *evsel,
1701 const union perf_event *event,
1702 struct perf_sample *sample)
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001703{
Arnaldo Carvalho de Melo0807d2d2012-09-26 12:48:18 -03001704 u64 type = evsel->attr.sample_type;
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001705 const u64 *array = event->sample.array;
Arnaldo Carvalho de Melo0807d2d2012-09-26 12:48:18 -03001706 bool swapped = evsel->needs_swap;
Jiri Olsa37073f92012-05-30 14:23:44 +02001707 union u64_swap u;
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001708
1709 array += ((event->header.size -
1710 sizeof(event->header)) / sizeof(u64)) - 1;
1711
Adrian Hunter75562572013-08-27 11:23:09 +03001712 if (type & PERF_SAMPLE_IDENTIFIER) {
1713 sample->id = *array;
1714 array--;
1715 }
1716
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001717 if (type & PERF_SAMPLE_CPU) {
Jiri Olsa37073f92012-05-30 14:23:44 +02001718 u.val64 = *array;
1719 if (swapped) {
1720 /* undo swap of u64, then swap on individual u32s */
1721 u.val64 = bswap_64(u.val64);
1722 u.val32[0] = bswap_32(u.val32[0]);
1723 }
1724
1725 sample->cpu = u.val32[0];
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001726 array--;
1727 }
1728
1729 if (type & PERF_SAMPLE_STREAM_ID) {
1730 sample->stream_id = *array;
1731 array--;
1732 }
1733
1734 if (type & PERF_SAMPLE_ID) {
1735 sample->id = *array;
1736 array--;
1737 }
1738
1739 if (type & PERF_SAMPLE_TIME) {
1740 sample->time = *array;
1741 array--;
1742 }
1743
1744 if (type & PERF_SAMPLE_TID) {
Jiri Olsa37073f92012-05-30 14:23:44 +02001745 u.val64 = *array;
1746 if (swapped) {
1747 /* undo swap of u64, then swap on individual u32s */
1748 u.val64 = bswap_64(u.val64);
1749 u.val32[0] = bswap_32(u.val32[0]);
1750 u.val32[1] = bswap_32(u.val32[1]);
1751 }
1752
1753 sample->pid = u.val32[0];
1754 sample->tid = u.val32[1];
Adrian Hunterdd44bc62013-10-18 15:29:01 +03001755 array--;
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001756 }
1757
1758 return 0;
1759}
1760
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001761static inline bool overflow(const void *endp, u16 max_size, const void *offset,
1762 u64 size)
Frederic Weisbecker98e1da92011-05-21 20:08:15 +02001763{
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001764 return size > max_size || offset + size > endp;
Frederic Weisbecker98e1da92011-05-21 20:08:15 +02001765}
1766
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001767#define OVERFLOW_CHECK(offset, size, max_size) \
1768 do { \
1769 if (overflow(endp, (max_size), (offset), (size))) \
1770 return -EFAULT; \
1771 } while (0)
1772
1773#define OVERFLOW_CHECK_u64(offset) \
1774 OVERFLOW_CHECK(offset, sizeof(u64), sizeof(u64))
1775
Arnaldo Carvalho de Meloa3f698f2012-08-02 12:23:46 -03001776int perf_evsel__parse_sample(struct perf_evsel *evsel, union perf_event *event,
Arnaldo Carvalho de Melo0807d2d2012-09-26 12:48:18 -03001777 struct perf_sample *data)
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001778{
Arnaldo Carvalho de Meloa3f698f2012-08-02 12:23:46 -03001779 u64 type = evsel->attr.sample_type;
Arnaldo Carvalho de Melo0807d2d2012-09-26 12:48:18 -03001780 bool swapped = evsel->needs_swap;
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001781 const u64 *array;
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001782 u16 max_size = event->header.size;
1783 const void *endp = (void *)event + max_size;
1784 u64 sz;
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001785
David Ahern936be502011-09-06 09:12:26 -06001786 /*
1787 * used for cross-endian analysis. See git commit 65014ab3
1788 * for why this goofiness is needed.
1789 */
Jiri Olsa6a11f922012-05-16 08:59:04 +02001790 union u64_swap u;
David Ahern936be502011-09-06 09:12:26 -06001791
Robert Richterf3bda2c2011-12-15 17:32:39 +01001792 memset(data, 0, sizeof(*data));
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001793 data->cpu = data->pid = data->tid = -1;
1794 data->stream_id = data->id = data->time = -1ULL;
Jiri Olsabc529082014-02-03 12:44:41 +01001795 data->period = evsel->attr.sample_period;
Arnaldo Carvalho de Melo473398a2016-03-22 18:23:43 -03001796 data->cpumode = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001797
1798 if (event->header.type != PERF_RECORD_SAMPLE) {
Arnaldo Carvalho de Meloa3f698f2012-08-02 12:23:46 -03001799 if (!evsel->attr.sample_id_all)
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001800 return 0;
Arnaldo Carvalho de Melo0807d2d2012-09-26 12:48:18 -03001801 return perf_evsel__parse_id_sample(evsel, event, data);
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001802 }
1803
1804 array = event->sample.array;
1805
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001806 /*
1807 * The evsel's sample_size is based on PERF_SAMPLE_MASK which includes
1808 * up to PERF_SAMPLE_PERIOD. After that overflow() must be used to
1809 * check the format does not go past the end of the event.
1810 */
Arnaldo Carvalho de Meloa3f698f2012-08-02 12:23:46 -03001811 if (evsel->sample_size + sizeof(event->header) > event->header.size)
Frederic Weisbeckera2854122011-05-21 19:33:04 +02001812 return -EFAULT;
1813
Adrian Hunter75562572013-08-27 11:23:09 +03001814 data->id = -1ULL;
1815 if (type & PERF_SAMPLE_IDENTIFIER) {
1816 data->id = *array;
1817 array++;
1818 }
1819
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001820 if (type & PERF_SAMPLE_IP) {
Adrian Hunteref893252013-08-27 11:23:06 +03001821 data->ip = *array;
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001822 array++;
1823 }
1824
1825 if (type & PERF_SAMPLE_TID) {
David Ahern936be502011-09-06 09:12:26 -06001826 u.val64 = *array;
1827 if (swapped) {
1828 /* undo swap of u64, then swap on individual u32s */
1829 u.val64 = bswap_64(u.val64);
1830 u.val32[0] = bswap_32(u.val32[0]);
1831 u.val32[1] = bswap_32(u.val32[1]);
1832 }
1833
1834 data->pid = u.val32[0];
1835 data->tid = u.val32[1];
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001836 array++;
1837 }
1838
1839 if (type & PERF_SAMPLE_TIME) {
1840 data->time = *array;
1841 array++;
1842 }
1843
David Ahern7cec0922011-05-30 13:08:23 -06001844 data->addr = 0;
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001845 if (type & PERF_SAMPLE_ADDR) {
1846 data->addr = *array;
1847 array++;
1848 }
1849
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001850 if (type & PERF_SAMPLE_ID) {
1851 data->id = *array;
1852 array++;
1853 }
1854
1855 if (type & PERF_SAMPLE_STREAM_ID) {
1856 data->stream_id = *array;
1857 array++;
1858 }
1859
1860 if (type & PERF_SAMPLE_CPU) {
David Ahern936be502011-09-06 09:12:26 -06001861
1862 u.val64 = *array;
1863 if (swapped) {
1864 /* undo swap of u64, then swap on individual u32s */
1865 u.val64 = bswap_64(u.val64);
1866 u.val32[0] = bswap_32(u.val32[0]);
1867 }
1868
1869 data->cpu = u.val32[0];
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001870 array++;
1871 }
1872
1873 if (type & PERF_SAMPLE_PERIOD) {
1874 data->period = *array;
1875 array++;
1876 }
1877
1878 if (type & PERF_SAMPLE_READ) {
Jiri Olsa9ede4732012-10-10 17:38:13 +02001879 u64 read_format = evsel->attr.read_format;
1880
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001881 OVERFLOW_CHECK_u64(array);
Jiri Olsa9ede4732012-10-10 17:38:13 +02001882 if (read_format & PERF_FORMAT_GROUP)
1883 data->read.group.nr = *array;
1884 else
1885 data->read.one.value = *array;
1886
1887 array++;
1888
1889 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) {
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001890 OVERFLOW_CHECK_u64(array);
Jiri Olsa9ede4732012-10-10 17:38:13 +02001891 data->read.time_enabled = *array;
1892 array++;
1893 }
1894
1895 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) {
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001896 OVERFLOW_CHECK_u64(array);
Jiri Olsa9ede4732012-10-10 17:38:13 +02001897 data->read.time_running = *array;
1898 array++;
1899 }
1900
1901 /* PERF_FORMAT_ID is forced for PERF_SAMPLE_READ */
1902 if (read_format & PERF_FORMAT_GROUP) {
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001903 const u64 max_group_nr = UINT64_MAX /
1904 sizeof(struct sample_read_value);
1905
1906 if (data->read.group.nr > max_group_nr)
1907 return -EFAULT;
1908 sz = data->read.group.nr *
1909 sizeof(struct sample_read_value);
1910 OVERFLOW_CHECK(array, sz, max_size);
1911 data->read.group.values =
1912 (struct sample_read_value *)array;
1913 array = (void *)array + sz;
Jiri Olsa9ede4732012-10-10 17:38:13 +02001914 } else {
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001915 OVERFLOW_CHECK_u64(array);
Jiri Olsa9ede4732012-10-10 17:38:13 +02001916 data->read.one.id = *array;
1917 array++;
1918 }
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001919 }
1920
1921 if (type & PERF_SAMPLE_CALLCHAIN) {
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001922 const u64 max_callchain_nr = UINT64_MAX / sizeof(u64);
1923
1924 OVERFLOW_CHECK_u64(array);
1925 data->callchain = (struct ip_callchain *)array++;
1926 if (data->callchain->nr > max_callchain_nr)
Frederic Weisbecker98e1da92011-05-21 20:08:15 +02001927 return -EFAULT;
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001928 sz = data->callchain->nr * sizeof(u64);
1929 OVERFLOW_CHECK(array, sz, max_size);
1930 array = (void *)array + sz;
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001931 }
1932
1933 if (type & PERF_SAMPLE_RAW) {
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001934 OVERFLOW_CHECK_u64(array);
David Ahern936be502011-09-06 09:12:26 -06001935 u.val64 = *array;
1936 if (WARN_ONCE(swapped,
1937 "Endianness of raw data not corrected!\n")) {
1938 /* undo swap of u64, then swap on individual u32s */
1939 u.val64 = bswap_64(u.val64);
1940 u.val32[0] = bswap_32(u.val32[0]);
1941 u.val32[1] = bswap_32(u.val32[1]);
1942 }
David Ahern936be502011-09-06 09:12:26 -06001943 data->raw_size = u.val32[0];
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001944 array = (void *)array + sizeof(u32);
Frederic Weisbecker98e1da92011-05-21 20:08:15 +02001945
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001946 OVERFLOW_CHECK(array, data->raw_size, max_size);
1947 data->raw_data = (void *)array;
1948 array = (void *)array + data->raw_size;
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001949 }
1950
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +01001951 if (type & PERF_SAMPLE_BRANCH_STACK) {
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001952 const u64 max_branch_nr = UINT64_MAX /
1953 sizeof(struct branch_entry);
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +01001954
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001955 OVERFLOW_CHECK_u64(array);
1956 data->branch_stack = (struct branch_stack *)array++;
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +01001957
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001958 if (data->branch_stack->nr > max_branch_nr)
1959 return -EFAULT;
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +01001960 sz = data->branch_stack->nr * sizeof(struct branch_entry);
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001961 OVERFLOW_CHECK(array, sz, max_size);
1962 array = (void *)array + sz;
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +01001963 }
Jiri Olsa0f6a3012012-08-07 15:20:45 +02001964
1965 if (type & PERF_SAMPLE_REGS_USER) {
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001966 OVERFLOW_CHECK_u64(array);
Adrian Hunter5b95a4a32013-08-27 11:23:10 +03001967 data->user_regs.abi = *array;
1968 array++;
Jiri Olsa0f6a3012012-08-07 15:20:45 +02001969
Adrian Hunter5b95a4a32013-08-27 11:23:10 +03001970 if (data->user_regs.abi) {
Jiri Olsa352ea452014-01-07 13:47:25 +01001971 u64 mask = evsel->attr.sample_regs_user;
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001972
Jiri Olsa352ea452014-01-07 13:47:25 +01001973 sz = hweight_long(mask) * sizeof(u64);
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001974 OVERFLOW_CHECK(array, sz, max_size);
Jiri Olsa352ea452014-01-07 13:47:25 +01001975 data->user_regs.mask = mask;
Jiri Olsa0f6a3012012-08-07 15:20:45 +02001976 data->user_regs.regs = (u64 *)array;
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001977 array = (void *)array + sz;
Jiri Olsa0f6a3012012-08-07 15:20:45 +02001978 }
1979 }
1980
1981 if (type & PERF_SAMPLE_STACK_USER) {
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001982 OVERFLOW_CHECK_u64(array);
1983 sz = *array++;
Jiri Olsa0f6a3012012-08-07 15:20:45 +02001984
1985 data->user_stack.offset = ((char *)(array - 1)
1986 - (char *) event);
1987
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001988 if (!sz) {
Jiri Olsa0f6a3012012-08-07 15:20:45 +02001989 data->user_stack.size = 0;
1990 } else {
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001991 OVERFLOW_CHECK(array, sz, max_size);
Jiri Olsa0f6a3012012-08-07 15:20:45 +02001992 data->user_stack.data = (char *)array;
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001993 array = (void *)array + sz;
1994 OVERFLOW_CHECK_u64(array);
Adrian Hunter54bd2692013-07-04 16:20:34 +03001995 data->user_stack.size = *array++;
Jiri Olsaa65cb4b2013-10-02 15:46:39 +02001996 if (WARN_ONCE(data->user_stack.size > sz,
1997 "user stack dump failure\n"))
1998 return -EFAULT;
Jiri Olsa0f6a3012012-08-07 15:20:45 +02001999 }
2000 }
2001
Andi Kleen05484292013-01-24 16:10:29 +01002002 if (type & PERF_SAMPLE_WEIGHT) {
Adrian Hunter03b6ea92013-08-27 11:23:04 +03002003 OVERFLOW_CHECK_u64(array);
Andi Kleen05484292013-01-24 16:10:29 +01002004 data->weight = *array;
2005 array++;
2006 }
2007
Stephane Eranian98a3b322013-01-24 16:10:35 +01002008 data->data_src = PERF_MEM_DATA_SRC_NONE;
2009 if (type & PERF_SAMPLE_DATA_SRC) {
Adrian Hunter03b6ea92013-08-27 11:23:04 +03002010 OVERFLOW_CHECK_u64(array);
Stephane Eranian98a3b322013-01-24 16:10:35 +01002011 data->data_src = *array;
2012 array++;
2013 }
2014
Andi Kleen475eeab2013-09-20 07:40:43 -07002015 data->transaction = 0;
2016 if (type & PERF_SAMPLE_TRANSACTION) {
Adrian Hunter87b95522013-11-01 15:51:36 +02002017 OVERFLOW_CHECK_u64(array);
Andi Kleen475eeab2013-09-20 07:40:43 -07002018 data->transaction = *array;
2019 array++;
2020 }
2021
Stephane Eranian6a21c0b2014-09-24 13:48:39 +02002022 data->intr_regs.abi = PERF_SAMPLE_REGS_ABI_NONE;
2023 if (type & PERF_SAMPLE_REGS_INTR) {
2024 OVERFLOW_CHECK_u64(array);
2025 data->intr_regs.abi = *array;
2026 array++;
2027
2028 if (data->intr_regs.abi != PERF_SAMPLE_REGS_ABI_NONE) {
2029 u64 mask = evsel->attr.sample_regs_intr;
2030
2031 sz = hweight_long(mask) * sizeof(u64);
2032 OVERFLOW_CHECK(array, sz, max_size);
2033 data->intr_regs.mask = mask;
2034 data->intr_regs.regs = (u64 *)array;
2035 array = (void *)array + sz;
2036 }
2037 }
2038
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02002039 return 0;
2040}
Andrew Vagin74eec262011-11-28 12:03:31 +03002041
Adrian Hunterb1cf6f62013-08-27 11:23:12 +03002042size_t perf_event__sample_event_size(const struct perf_sample *sample, u64 type,
Jiri Olsa352ea452014-01-07 13:47:25 +01002043 u64 read_format)
Adrian Hunterb1cf6f62013-08-27 11:23:12 +03002044{
2045 size_t sz, result = sizeof(struct sample_event);
2046
2047 if (type & PERF_SAMPLE_IDENTIFIER)
2048 result += sizeof(u64);
2049
2050 if (type & PERF_SAMPLE_IP)
2051 result += sizeof(u64);
2052
2053 if (type & PERF_SAMPLE_TID)
2054 result += sizeof(u64);
2055
2056 if (type & PERF_SAMPLE_TIME)
2057 result += sizeof(u64);
2058
2059 if (type & PERF_SAMPLE_ADDR)
2060 result += sizeof(u64);
2061
2062 if (type & PERF_SAMPLE_ID)
2063 result += sizeof(u64);
2064
2065 if (type & PERF_SAMPLE_STREAM_ID)
2066 result += sizeof(u64);
2067
2068 if (type & PERF_SAMPLE_CPU)
2069 result += sizeof(u64);
2070
2071 if (type & PERF_SAMPLE_PERIOD)
2072 result += sizeof(u64);
2073
2074 if (type & PERF_SAMPLE_READ) {
2075 result += sizeof(u64);
2076 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
2077 result += sizeof(u64);
2078 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
2079 result += sizeof(u64);
2080 /* PERF_FORMAT_ID is forced for PERF_SAMPLE_READ */
2081 if (read_format & PERF_FORMAT_GROUP) {
2082 sz = sample->read.group.nr *
2083 sizeof(struct sample_read_value);
2084 result += sz;
2085 } else {
2086 result += sizeof(u64);
2087 }
2088 }
2089
2090 if (type & PERF_SAMPLE_CALLCHAIN) {
2091 sz = (sample->callchain->nr + 1) * sizeof(u64);
2092 result += sz;
2093 }
2094
2095 if (type & PERF_SAMPLE_RAW) {
2096 result += sizeof(u32);
2097 result += sample->raw_size;
2098 }
2099
2100 if (type & PERF_SAMPLE_BRANCH_STACK) {
2101 sz = sample->branch_stack->nr * sizeof(struct branch_entry);
2102 sz += sizeof(u64);
2103 result += sz;
2104 }
2105
2106 if (type & PERF_SAMPLE_REGS_USER) {
2107 if (sample->user_regs.abi) {
2108 result += sizeof(u64);
Jiri Olsa352ea452014-01-07 13:47:25 +01002109 sz = hweight_long(sample->user_regs.mask) * sizeof(u64);
Adrian Hunterb1cf6f62013-08-27 11:23:12 +03002110 result += sz;
2111 } else {
2112 result += sizeof(u64);
2113 }
2114 }
2115
2116 if (type & PERF_SAMPLE_STACK_USER) {
2117 sz = sample->user_stack.size;
2118 result += sizeof(u64);
2119 if (sz) {
2120 result += sz;
2121 result += sizeof(u64);
2122 }
2123 }
2124
2125 if (type & PERF_SAMPLE_WEIGHT)
2126 result += sizeof(u64);
2127
2128 if (type & PERF_SAMPLE_DATA_SRC)
2129 result += sizeof(u64);
2130
Adrian Hunter42d88912013-11-01 15:51:38 +02002131 if (type & PERF_SAMPLE_TRANSACTION)
2132 result += sizeof(u64);
2133
Stephane Eranian6a21c0b2014-09-24 13:48:39 +02002134 if (type & PERF_SAMPLE_REGS_INTR) {
2135 if (sample->intr_regs.abi) {
2136 result += sizeof(u64);
2137 sz = hweight_long(sample->intr_regs.mask) * sizeof(u64);
2138 result += sz;
2139 } else {
2140 result += sizeof(u64);
2141 }
2142 }
2143
Adrian Hunterb1cf6f62013-08-27 11:23:12 +03002144 return result;
2145}
2146
Andrew Vagin74eec262011-11-28 12:03:31 +03002147int perf_event__synthesize_sample(union perf_event *event, u64 type,
Jiri Olsa352ea452014-01-07 13:47:25 +01002148 u64 read_format,
Andrew Vagin74eec262011-11-28 12:03:31 +03002149 const struct perf_sample *sample,
2150 bool swapped)
2151{
2152 u64 *array;
Adrian Hunterd03f2172013-08-27 11:23:11 +03002153 size_t sz;
Andrew Vagin74eec262011-11-28 12:03:31 +03002154 /*
2155 * used for cross-endian analysis. See git commit 65014ab3
2156 * for why this goofiness is needed.
2157 */
Jiri Olsa6a11f922012-05-16 08:59:04 +02002158 union u64_swap u;
Andrew Vagin74eec262011-11-28 12:03:31 +03002159
2160 array = event->sample.array;
2161
Adrian Hunter75562572013-08-27 11:23:09 +03002162 if (type & PERF_SAMPLE_IDENTIFIER) {
2163 *array = sample->id;
2164 array++;
2165 }
2166
Andrew Vagin74eec262011-11-28 12:03:31 +03002167 if (type & PERF_SAMPLE_IP) {
Adrian Hunteref893252013-08-27 11:23:06 +03002168 *array = sample->ip;
Andrew Vagin74eec262011-11-28 12:03:31 +03002169 array++;
2170 }
2171
2172 if (type & PERF_SAMPLE_TID) {
2173 u.val32[0] = sample->pid;
2174 u.val32[1] = sample->tid;
2175 if (swapped) {
2176 /*
Arnaldo Carvalho de Meloa3f698f2012-08-02 12:23:46 -03002177 * Inverse of what is done in perf_evsel__parse_sample
Andrew Vagin74eec262011-11-28 12:03:31 +03002178 */
2179 u.val32[0] = bswap_32(u.val32[0]);
2180 u.val32[1] = bswap_32(u.val32[1]);
2181 u.val64 = bswap_64(u.val64);
2182 }
2183
2184 *array = u.val64;
2185 array++;
2186 }
2187
2188 if (type & PERF_SAMPLE_TIME) {
2189 *array = sample->time;
2190 array++;
2191 }
2192
2193 if (type & PERF_SAMPLE_ADDR) {
2194 *array = sample->addr;
2195 array++;
2196 }
2197
2198 if (type & PERF_SAMPLE_ID) {
2199 *array = sample->id;
2200 array++;
2201 }
2202
2203 if (type & PERF_SAMPLE_STREAM_ID) {
2204 *array = sample->stream_id;
2205 array++;
2206 }
2207
2208 if (type & PERF_SAMPLE_CPU) {
2209 u.val32[0] = sample->cpu;
2210 if (swapped) {
2211 /*
Arnaldo Carvalho de Meloa3f698f2012-08-02 12:23:46 -03002212 * Inverse of what is done in perf_evsel__parse_sample
Andrew Vagin74eec262011-11-28 12:03:31 +03002213 */
2214 u.val32[0] = bswap_32(u.val32[0]);
2215 u.val64 = bswap_64(u.val64);
2216 }
2217 *array = u.val64;
2218 array++;
2219 }
2220
2221 if (type & PERF_SAMPLE_PERIOD) {
2222 *array = sample->period;
2223 array++;
2224 }
2225
Adrian Hunterd03f2172013-08-27 11:23:11 +03002226 if (type & PERF_SAMPLE_READ) {
2227 if (read_format & PERF_FORMAT_GROUP)
2228 *array = sample->read.group.nr;
2229 else
2230 *array = sample->read.one.value;
2231 array++;
2232
2233 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) {
2234 *array = sample->read.time_enabled;
2235 array++;
2236 }
2237
2238 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) {
2239 *array = sample->read.time_running;
2240 array++;
2241 }
2242
2243 /* PERF_FORMAT_ID is forced for PERF_SAMPLE_READ */
2244 if (read_format & PERF_FORMAT_GROUP) {
2245 sz = sample->read.group.nr *
2246 sizeof(struct sample_read_value);
2247 memcpy(array, sample->read.group.values, sz);
2248 array = (void *)array + sz;
2249 } else {
2250 *array = sample->read.one.id;
2251 array++;
2252 }
2253 }
2254
2255 if (type & PERF_SAMPLE_CALLCHAIN) {
2256 sz = (sample->callchain->nr + 1) * sizeof(u64);
2257 memcpy(array, sample->callchain, sz);
2258 array = (void *)array + sz;
2259 }
2260
2261 if (type & PERF_SAMPLE_RAW) {
2262 u.val32[0] = sample->raw_size;
2263 if (WARN_ONCE(swapped,
2264 "Endianness of raw data not corrected!\n")) {
2265 /*
2266 * Inverse of what is done in perf_evsel__parse_sample
2267 */
2268 u.val32[0] = bswap_32(u.val32[0]);
2269 u.val32[1] = bswap_32(u.val32[1]);
2270 u.val64 = bswap_64(u.val64);
2271 }
2272 *array = u.val64;
2273 array = (void *)array + sizeof(u32);
2274
2275 memcpy(array, sample->raw_data, sample->raw_size);
2276 array = (void *)array + sample->raw_size;
2277 }
2278
2279 if (type & PERF_SAMPLE_BRANCH_STACK) {
2280 sz = sample->branch_stack->nr * sizeof(struct branch_entry);
2281 sz += sizeof(u64);
2282 memcpy(array, sample->branch_stack, sz);
2283 array = (void *)array + sz;
2284 }
2285
2286 if (type & PERF_SAMPLE_REGS_USER) {
2287 if (sample->user_regs.abi) {
2288 *array++ = sample->user_regs.abi;
Jiri Olsa352ea452014-01-07 13:47:25 +01002289 sz = hweight_long(sample->user_regs.mask) * sizeof(u64);
Adrian Hunterd03f2172013-08-27 11:23:11 +03002290 memcpy(array, sample->user_regs.regs, sz);
2291 array = (void *)array + sz;
2292 } else {
2293 *array++ = 0;
2294 }
2295 }
2296
2297 if (type & PERF_SAMPLE_STACK_USER) {
2298 sz = sample->user_stack.size;
2299 *array++ = sz;
2300 if (sz) {
2301 memcpy(array, sample->user_stack.data, sz);
2302 array = (void *)array + sz;
2303 *array++ = sz;
2304 }
2305 }
2306
2307 if (type & PERF_SAMPLE_WEIGHT) {
2308 *array = sample->weight;
2309 array++;
2310 }
2311
2312 if (type & PERF_SAMPLE_DATA_SRC) {
2313 *array = sample->data_src;
2314 array++;
2315 }
2316
Adrian Hunter42d88912013-11-01 15:51:38 +02002317 if (type & PERF_SAMPLE_TRANSACTION) {
2318 *array = sample->transaction;
2319 array++;
2320 }
2321
Stephane Eranian6a21c0b2014-09-24 13:48:39 +02002322 if (type & PERF_SAMPLE_REGS_INTR) {
2323 if (sample->intr_regs.abi) {
2324 *array++ = sample->intr_regs.abi;
2325 sz = hweight_long(sample->intr_regs.mask) * sizeof(u64);
2326 memcpy(array, sample->intr_regs.regs, sz);
2327 array = (void *)array + sz;
2328 } else {
2329 *array++ = 0;
2330 }
2331 }
2332
Andrew Vagin74eec262011-11-28 12:03:31 +03002333 return 0;
2334}
Arnaldo Carvalho de Melo5555ded2012-09-11 19:24:23 -03002335
Arnaldo Carvalho de Meloefd2b922012-09-18 11:21:50 -03002336struct format_field *perf_evsel__field(struct perf_evsel *evsel, const char *name)
2337{
2338 return pevent_find_field(evsel->tp_format, name);
2339}
2340
Arnaldo Carvalho de Melo5d2074e2012-09-26 20:22:00 -03002341void *perf_evsel__rawptr(struct perf_evsel *evsel, struct perf_sample *sample,
Arnaldo Carvalho de Melo5555ded2012-09-11 19:24:23 -03002342 const char *name)
2343{
Arnaldo Carvalho de Meloefd2b922012-09-18 11:21:50 -03002344 struct format_field *field = perf_evsel__field(evsel, name);
Arnaldo Carvalho de Melo5555ded2012-09-11 19:24:23 -03002345 int offset;
2346
Arnaldo Carvalho de Meloefd2b922012-09-18 11:21:50 -03002347 if (!field)
2348 return NULL;
Arnaldo Carvalho de Melo5555ded2012-09-11 19:24:23 -03002349
2350 offset = field->offset;
2351
2352 if (field->flags & FIELD_IS_DYNAMIC) {
2353 offset = *(int *)(sample->raw_data + field->offset);
2354 offset &= 0xffff;
2355 }
2356
2357 return sample->raw_data + offset;
2358}
2359
Arnaldo Carvalho de Melo90525172016-05-31 12:47:46 -03002360u64 format_field__intval(struct format_field *field, struct perf_sample *sample,
2361 bool needs_swap)
Arnaldo Carvalho de Melo5555ded2012-09-11 19:24:23 -03002362{
Arnaldo Carvalho de Meloe6b6f672012-09-26 13:13:04 -03002363 u64 value;
Arnaldo Carvalho de Melo90525172016-05-31 12:47:46 -03002364 void *ptr = sample->raw_data + field->offset;
Arnaldo Carvalho de Melo5555ded2012-09-11 19:24:23 -03002365
Arnaldo Carvalho de Meloe6b6f672012-09-26 13:13:04 -03002366 switch (field->size) {
2367 case 1:
2368 return *(u8 *)ptr;
2369 case 2:
2370 value = *(u16 *)ptr;
2371 break;
2372 case 4:
2373 value = *(u32 *)ptr;
2374 break;
2375 case 8:
David Aherne94eeda2015-03-24 16:14:09 -04002376 memcpy(&value, ptr, sizeof(u64));
Arnaldo Carvalho de Meloe6b6f672012-09-26 13:13:04 -03002377 break;
2378 default:
2379 return 0;
2380 }
2381
Arnaldo Carvalho de Melo90525172016-05-31 12:47:46 -03002382 if (!needs_swap)
Arnaldo Carvalho de Meloe6b6f672012-09-26 13:13:04 -03002383 return value;
2384
2385 switch (field->size) {
2386 case 2:
2387 return bswap_16(value);
2388 case 4:
2389 return bswap_32(value);
2390 case 8:
2391 return bswap_64(value);
2392 default:
2393 return 0;
2394 }
2395
2396 return 0;
Arnaldo Carvalho de Melo5555ded2012-09-11 19:24:23 -03002397}
Arnaldo Carvalho de Melo0698aed2012-12-10 18:17:08 -03002398
Arnaldo Carvalho de Melo90525172016-05-31 12:47:46 -03002399u64 perf_evsel__intval(struct perf_evsel *evsel, struct perf_sample *sample,
2400 const char *name)
2401{
2402 struct format_field *field = perf_evsel__field(evsel, name);
2403
2404 if (!field)
2405 return 0;
2406
2407 return field ? format_field__intval(field, sample, evsel->needs_swap) : 0;
2408}
2409
Arnaldo Carvalho de Meloc0a54342012-12-13 14:16:30 -03002410bool perf_evsel__fallback(struct perf_evsel *evsel, int err,
2411 char *msg, size_t msgsize)
2412{
Arnaldo Carvalho de Melo08094822016-05-12 16:07:47 -03002413 int paranoid;
2414
David Ahern2b821cc2013-07-18 17:27:59 -06002415 if ((err == ENOENT || err == ENXIO || err == ENODEV) &&
Arnaldo Carvalho de Meloc0a54342012-12-13 14:16:30 -03002416 evsel->attr.type == PERF_TYPE_HARDWARE &&
2417 evsel->attr.config == PERF_COUNT_HW_CPU_CYCLES) {
2418 /*
2419 * If it's cycles then fall back to hrtimer based
2420 * cpu-clock-tick sw counter, which is always available even if
2421 * no PMU support.
2422 *
2423 * PPC returns ENXIO until 2.6.37 (behavior changed with commit
2424 * b0a873e).
2425 */
2426 scnprintf(msg, msgsize, "%s",
2427"The cycles event is not supported, trying to fall back to cpu-clock-ticks");
2428
2429 evsel->attr.type = PERF_TYPE_SOFTWARE;
2430 evsel->attr.config = PERF_COUNT_SW_CPU_CLOCK;
2431
Arnaldo Carvalho de Melo04662522013-12-26 17:41:15 -03002432 zfree(&evsel->name);
Arnaldo Carvalho de Meloc0a54342012-12-13 14:16:30 -03002433 return true;
Arnaldo Carvalho de Melo08094822016-05-12 16:07:47 -03002434 } else if (err == EACCES && !evsel->attr.exclude_kernel &&
2435 (paranoid = perf_event_paranoid()) > 1) {
2436 const char *name = perf_evsel__name(evsel);
2437 char *new_name;
2438
2439 if (asprintf(&new_name, "%s%su", name, strchr(name, ':') ? "" : ":") < 0)
2440 return false;
2441
2442 if (evsel->name)
2443 free(evsel->name);
2444 evsel->name = new_name;
2445 scnprintf(msg, msgsize,
2446"kernel.perf_event_paranoid=%d, trying to fall back to excluding kernel samples", paranoid);
2447 evsel->attr.exclude_kernel = 1;
2448
2449 return true;
Arnaldo Carvalho de Meloc0a54342012-12-13 14:16:30 -03002450 }
2451
2452 return false;
2453}
Arnaldo Carvalho de Melo56e52e82012-12-13 15:10:58 -03002454
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03002455int perf_evsel__open_strerror(struct perf_evsel *evsel, struct target *target,
Arnaldo Carvalho de Melo56e52e82012-12-13 15:10:58 -03002456 int err, char *msg, size_t size)
2457{
Masami Hiramatsu6e81c742014-08-14 02:22:36 +00002458 char sbuf[STRERR_BUFSIZE];
2459
Arnaldo Carvalho de Melo56e52e82012-12-13 15:10:58 -03002460 switch (err) {
2461 case EPERM:
2462 case EACCES:
David Ahernb69e63a2013-05-25 17:54:00 -06002463 return scnprintf(msg, size,
Ben Hutchings3379e0c2016-01-19 21:35:15 +00002464 "You may not have permission to collect %sstats.\n\n"
2465 "Consider tweaking /proc/sys/kernel/perf_event_paranoid,\n"
2466 "which controls use of the performance events system by\n"
2467 "unprivileged users (without CAP_SYS_ADMIN).\n\n"
Arnaldo Carvalho de Melo7d173912016-05-12 15:44:55 -03002468 "The current value is %d:\n\n"
Ben Hutchings3379e0c2016-01-19 21:35:15 +00002469 " -1: Allow use of (almost) all events by all users\n"
2470 ">= 0: Disallow raw tracepoint access by users without CAP_IOC_LOCK\n"
2471 ">= 1: Disallow CPU event access by users without CAP_SYS_ADMIN\n"
Arnaldo Carvalho de Melod6195a62017-02-13 16:45:24 -03002472 ">= 2: Disallow kernel profiling by users without CAP_SYS_ADMIN\n\n"
2473 "To make this setting permanent, edit /etc/sysctl.conf too, e.g.:\n\n"
2474 " kernel.perf_event_paranoid = -1\n" ,
Arnaldo Carvalho de Melo7d173912016-05-12 15:44:55 -03002475 target->system_wide ? "system-wide " : "",
2476 perf_event_paranoid());
Arnaldo Carvalho de Melo56e52e82012-12-13 15:10:58 -03002477 case ENOENT:
2478 return scnprintf(msg, size, "The %s event is not supported.",
2479 perf_evsel__name(evsel));
2480 case EMFILE:
2481 return scnprintf(msg, size, "%s",
2482 "Too many events are opened.\n"
Jiri Olsa18ffdfe2015-05-25 22:51:54 +02002483 "Probably the maximum number of open file descriptors has been reached.\n"
2484 "Hint: Try again after reducing the number of events.\n"
2485 "Hint: Try increasing the limit with 'ulimit -n <limit>'");
Arnaldo Carvalho de Melode46d522016-04-27 17:51:45 -03002486 case ENOMEM:
2487 if ((evsel->attr.sample_type & PERF_SAMPLE_CALLCHAIN) != 0 &&
2488 access("/proc/sys/kernel/perf_event_max_stack", F_OK) == 0)
2489 return scnprintf(msg, size,
2490 "Not enough memory to setup event with callchain.\n"
2491 "Hint: Try tweaking /proc/sys/kernel/perf_event_max_stack\n"
2492 "Hint: Current value: %d", sysctl_perf_event_max_stack);
2493 break;
Arnaldo Carvalho de Melo56e52e82012-12-13 15:10:58 -03002494 case ENODEV:
2495 if (target->cpu_list)
2496 return scnprintf(msg, size, "%s",
Arnaldo Carvalho de Melo81d64f42016-04-27 17:56:53 -03002497 "No such device - did you specify an out-of-range profile CPU?");
Arnaldo Carvalho de Melo56e52e82012-12-13 15:10:58 -03002498 break;
2499 case EOPNOTSUPP:
Vineet Guptadc89e752016-05-09 15:07:39 +05302500 if (evsel->attr.sample_period != 0)
2501 return scnprintf(msg, size, "%s",
2502 "PMU Hardware doesn't support sampling/overflow-interrupts.");
Arnaldo Carvalho de Melo56e52e82012-12-13 15:10:58 -03002503 if (evsel->attr.precise_ip)
2504 return scnprintf(msg, size, "%s",
2505 "\'precise\' request may not be supported. Try removing 'p' modifier.");
2506#if defined(__i386__) || defined(__x86_64__)
2507 if (evsel->attr.type == PERF_TYPE_HARDWARE)
2508 return scnprintf(msg, size, "%s",
2509 "No hardware sampling interrupt available.\n"
2510 "No APIC? If so then you can boot the kernel with the \"lapic\" boot parameter to force-enable it.");
2511#endif
2512 break;
Jiri Olsa63914ac2014-08-01 17:46:54 +02002513 case EBUSY:
2514 if (find_process("oprofiled"))
2515 return scnprintf(msg, size,
2516 "The PMU counters are busy/taken by another profiler.\n"
2517 "We found oprofile daemon running, please stop it and try again.");
2518 break;
Peter Zijlstra814c8c32015-03-31 00:19:31 +02002519 case EINVAL:
Arnaldo Carvalho de Melo32a951b2016-07-14 08:34:33 +00002520 if (evsel->attr.write_backward && perf_missing_features.write_backward)
Arnaldo Carvalho de Melo7da36e92016-06-20 10:47:18 +00002521 return scnprintf(msg, size, "Reading from overwrite event is not supported by this kernel.");
Peter Zijlstra814c8c32015-03-31 00:19:31 +02002522 if (perf_missing_features.clockid)
2523 return scnprintf(msg, size, "clockid feature not supported.");
2524 if (perf_missing_features.clockid_wrong)
2525 return scnprintf(msg, size, "wrong clockid (%d).", clockid);
2526 break;
Arnaldo Carvalho de Melo56e52e82012-12-13 15:10:58 -03002527 default:
2528 break;
2529 }
2530
2531 return scnprintf(msg, size,
Masami Hiramatsu6e81c742014-08-14 02:22:36 +00002532 "The sys_perf_event_open() syscall returned with %d (%s) for event (%s).\n"
Arnaldo Carvalho de Melo56e52e82012-12-13 15:10:58 -03002533 "/bin/dmesg may provide additional information.\n"
Arnaldo Carvalho de Melo81d64f42016-04-27 17:56:53 -03002534 "No CONFIG_PERF_EVENTS=y kernel support configured?",
Arnaldo Carvalho de Meloc8b5f2c2016-07-06 11:56:20 -03002535 err, str_error_r(err, sbuf, sizeof(sbuf)),
Masami Hiramatsu6e81c742014-08-14 02:22:36 +00002536 perf_evsel__name(evsel));
Arnaldo Carvalho de Melo56e52e82012-12-13 15:10:58 -03002537}
Ravi Bangoriaf4e47f92016-06-30 11:44:19 +05302538
2539char *perf_evsel__env_arch(struct perf_evsel *evsel)
2540{
2541 if (evsel && evsel->evlist && evsel->evlist->env)
2542 return evsel->evlist->env->arch;
2543 return NULL;
2544}