blob: f2a1876d268eaa6ac00ab169f7aa923af8d49a0e [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>
Arnaldo Carvalho de Meloa43783a2017-04-18 10:46:11 -030011#include <errno.h>
Arnaldo Carvalho de Melofd20e812017-04-17 15:23:08 -030012#include <inttypes.h>
Jiri Olsa0f6a3012012-08-07 15:20:45 +020013#include <linux/bitops.h>
Arnaldo Carvalho de Melo2157f6e2017-06-20 12:05:38 -030014#include <api/fs/fs.h>
Jiri Olsa4605eab2015-09-02 09:56:43 +020015#include <api/fs/tracing_path.h>
Robert Richter4e319022013-06-11 17:29:18 +020016#include <traceevent/event-parse.h>
17#include <linux/hw_breakpoint.h>
18#include <linux/perf_event.h>
Arnaldo Carvalho de Melo03536312017-06-16 12:18:27 -030019#include <linux/compiler.h>
Jiri Olsa8dd2a132015-09-07 10:38:06 +020020#include <linux/err.h>
Arnaldo Carvalho de Melo86a5e0c2017-04-19 19:03:14 -030021#include <sys/ioctl.h>
Andi Kleenbec19672013-08-04 19:41:26 -070022#include <sys/resource.h>
Arnaldo Carvalho de Melo2157f6e2017-06-20 12:05:38 -030023#include <sys/types.h>
24#include <dirent.h>
Robert Richter4e319022013-06-11 17:29:18 +020025#include "asm/bug.h"
Arnaldo Carvalho de Melo8f651ea2014-10-09 16:12:24 -030026#include "callchain.h"
Arnaldo Carvalho de Melof14d5702014-10-17 12:17:40 -030027#include "cgroup.h"
Arnaldo Carvalho de Melo5ab8c682017-04-25 15:30:47 -030028#include "event.h"
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -020029#include "evsel.h"
Arnaldo Carvalho de Melo70082dd2011-01-12 17:03:24 -020030#include "evlist.h"
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -020031#include "util.h"
Arnaldo Carvalho de Melo86bd5e82011-01-03 23:09:46 -020032#include "cpumap.h"
Arnaldo Carvalho de Melofd782602011-01-18 15:15:24 -020033#include "thread_map.h"
Namhyung Kim12864b32012-04-26 14:15:22 +090034#include "target.h"
Jiri Olsa26d33022012-08-07 15:20:47 +020035#include "perf_regs.h"
Adrian Huntere3e1a542013-08-14 15:48:24 +030036#include "debug.h"
Jiri Olsa97978b32013-12-03 14:09:24 +010037#include "trace-event.h"
Jiri Olsaa9a3a4d2015-06-14 10:19:26 +020038#include "stat.h"
Andi Kleenac12f672016-10-12 14:02:06 -070039#include "util/parse-branch-options.h"
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -020040
Arnaldo Carvalho de Melo3d689ed2017-04-17 16:10:49 -030041#include "sane_ctype.h"
42
Arnaldo Carvalho de Melo594ac612012-12-13 13:13:07 -030043static struct {
44 bool sample_id_all;
45 bool exclude_guest;
Stephane Eranian5c5e8542013-08-21 12:10:25 +020046 bool mmap2;
Yann Droneaud57480d22014-06-30 22:28:47 +020047 bool cloexec;
Peter Zijlstra814c8c32015-03-31 00:19:31 +020048 bool clockid;
49 bool clockid_wrong;
Andi Kleenbd0f8892015-12-11 16:12:24 -080050 bool lbr_flags;
Wang Nanb90dc172016-05-20 16:38:23 +000051 bool write_backward;
Arnaldo Carvalho de Melo594ac612012-12-13 13:13:07 -030052} perf_missing_features;
53
Peter Zijlstra814c8c32015-03-31 00:19:31 +020054static clockid_t clockid;
55
Arnaldo Carvalho de Meloce8ccff2014-10-09 15:29:51 -030056static int perf_evsel__no_extra_init(struct perf_evsel *evsel __maybe_unused)
57{
58 return 0;
59}
60
61static void perf_evsel__no_extra_fini(struct perf_evsel *evsel __maybe_unused)
62{
63}
64
65static struct {
66 size_t size;
67 int (*init)(struct perf_evsel *evsel);
68 void (*fini)(struct perf_evsel *evsel);
69} perf_evsel__object = {
70 .size = sizeof(struct perf_evsel),
71 .init = perf_evsel__no_extra_init,
72 .fini = perf_evsel__no_extra_fini,
73};
74
75int perf_evsel__object_config(size_t object_size,
76 int (*init)(struct perf_evsel *evsel),
77 void (*fini)(struct perf_evsel *evsel))
78{
79
80 if (object_size == 0)
81 goto set_methods;
82
83 if (perf_evsel__object.size > object_size)
84 return -EINVAL;
85
86 perf_evsel__object.size = object_size;
87
88set_methods:
89 if (init != NULL)
90 perf_evsel__object.init = init;
91
92 if (fini != NULL)
93 perf_evsel__object.fini = fini;
94
95 return 0;
96}
97
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -020098#define FD(e, x, y) (*(int *)xyarray__entry(e->fd, x, y))
99
Adrian Hunter75562572013-08-27 11:23:09 +0300100int __perf_evsel__sample_size(u64 sample_type)
Arnaldo Carvalho de Meloc2a70652011-06-02 11:04:54 -0300101{
102 u64 mask = sample_type & PERF_SAMPLE_MASK;
103 int size = 0;
104 int i;
105
106 for (i = 0; i < 64; i++) {
107 if (mask & (1ULL << i))
108 size++;
109 }
110
111 size *= sizeof(u64);
112
113 return size;
114}
115
Adrian Hunter75562572013-08-27 11:23:09 +0300116/**
117 * __perf_evsel__calc_id_pos - calculate id_pos.
118 * @sample_type: sample type
119 *
120 * This function returns the position of the event id (PERF_SAMPLE_ID or
121 * PERF_SAMPLE_IDENTIFIER) in a sample event i.e. in the array of struct
122 * sample_event.
123 */
124static int __perf_evsel__calc_id_pos(u64 sample_type)
125{
126 int idx = 0;
127
128 if (sample_type & PERF_SAMPLE_IDENTIFIER)
129 return 0;
130
131 if (!(sample_type & PERF_SAMPLE_ID))
132 return -1;
133
134 if (sample_type & PERF_SAMPLE_IP)
135 idx += 1;
136
137 if (sample_type & PERF_SAMPLE_TID)
138 idx += 1;
139
140 if (sample_type & PERF_SAMPLE_TIME)
141 idx += 1;
142
143 if (sample_type & PERF_SAMPLE_ADDR)
144 idx += 1;
145
146 return idx;
147}
148
149/**
150 * __perf_evsel__calc_is_pos - calculate is_pos.
151 * @sample_type: sample type
152 *
153 * This function returns the position (counting backwards) of the event id
154 * (PERF_SAMPLE_ID or PERF_SAMPLE_IDENTIFIER) in a non-sample event i.e. if
155 * sample_id_all is used there is an id sample appended to non-sample events.
156 */
157static int __perf_evsel__calc_is_pos(u64 sample_type)
158{
159 int idx = 1;
160
161 if (sample_type & PERF_SAMPLE_IDENTIFIER)
162 return 1;
163
164 if (!(sample_type & PERF_SAMPLE_ID))
165 return -1;
166
167 if (sample_type & PERF_SAMPLE_CPU)
168 idx += 1;
169
170 if (sample_type & PERF_SAMPLE_STREAM_ID)
171 idx += 1;
172
173 return idx;
174}
175
176void perf_evsel__calc_id_pos(struct perf_evsel *evsel)
177{
178 evsel->id_pos = __perf_evsel__calc_id_pos(evsel->attr.sample_type);
179 evsel->is_pos = __perf_evsel__calc_is_pos(evsel->attr.sample_type);
180}
181
Arnaldo Carvalho de Melo7be5ebe2012-12-10 14:53:43 -0300182void __perf_evsel__set_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
192void __perf_evsel__reset_sample_bit(struct perf_evsel *evsel,
193 enum perf_event_sample_format bit)
194{
195 if (evsel->attr.sample_type & bit) {
196 evsel->attr.sample_type &= ~bit;
197 evsel->sample_size -= sizeof(u64);
Adrian Hunter75562572013-08-27 11:23:09 +0300198 perf_evsel__calc_id_pos(evsel);
Arnaldo Carvalho de Melo7be5ebe2012-12-10 14:53:43 -0300199 }
200}
201
Adrian Hunter75562572013-08-27 11:23:09 +0300202void perf_evsel__set_sample_id(struct perf_evsel *evsel,
203 bool can_sample_identifier)
Arnaldo Carvalho de Melo7a5a5ca2012-12-10 15:21:30 -0300204{
Adrian Hunter75562572013-08-27 11:23:09 +0300205 if (can_sample_identifier) {
206 perf_evsel__reset_sample_bit(evsel, ID);
207 perf_evsel__set_sample_bit(evsel, IDENTIFIER);
208 } else {
209 perf_evsel__set_sample_bit(evsel, ID);
210 }
Arnaldo Carvalho de Melo7a5a5ca2012-12-10 15:21:30 -0300211 evsel->attr.read_format |= PERF_FORMAT_ID;
212}
213
Arnaldo Carvalho de Melo5496bc02016-07-07 11:51:47 -0300214/**
215 * perf_evsel__is_function_event - Return whether given evsel is a function
216 * trace event
217 *
218 * @evsel - evsel selector to be tested
219 *
220 * Return %true if event is function trace event
221 */
222bool perf_evsel__is_function_event(struct perf_evsel *evsel)
223{
224#define FUNCTION_EVENT "ftrace:function"
225
226 return evsel->name &&
227 !strncmp(FUNCTION_EVENT, evsel->name, sizeof(FUNCTION_EVENT));
228
229#undef FUNCTION_EVENT
230}
231
Arnaldo Carvalho de Meloef1d1af2011-01-18 21:41:45 -0200232void perf_evsel__init(struct perf_evsel *evsel,
233 struct perf_event_attr *attr, int idx)
234{
235 evsel->idx = idx;
Adrian Hunter60b08962014-07-31 09:00:52 +0300236 evsel->tracking = !idx;
Arnaldo Carvalho de Meloef1d1af2011-01-18 21:41:45 -0200237 evsel->attr = *attr;
Namhyung Kim2cfda562012-11-29 15:38:29 +0900238 evsel->leader = evsel;
Stephane Eranian410136f2013-11-12 17:58:49 +0100239 evsel->unit = "";
240 evsel->scale = 1.0;
Arnaldo Carvalho de Melod49e4692015-08-27 08:07:40 -0400241 evsel->evlist = NULL;
Wang Nan1f45b1d2015-10-14 12:41:18 +0000242 evsel->bpf_fd = -1;
Arnaldo Carvalho de Meloef1d1af2011-01-18 21:41:45 -0200243 INIT_LIST_HEAD(&evsel->node);
Jiri Olsa930a2e22015-07-29 05:42:10 -0400244 INIT_LIST_HEAD(&evsel->config_terms);
Arnaldo Carvalho de Meloce8ccff2014-10-09 15:29:51 -0300245 perf_evsel__object.init(evsel);
Arnaldo Carvalho de Melobde09462012-08-01 18:53:11 -0300246 evsel->sample_size = __perf_evsel__sample_size(attr->sample_type);
Adrian Hunter75562572013-08-27 11:23:09 +0300247 perf_evsel__calc_id_pos(evsel);
Wang Nan15bfd2c2015-07-10 07:36:09 +0000248 evsel->cmdline_group_boundary = false;
Andi Kleen37932c12017-03-20 13:17:08 -0700249 evsel->metric_expr = NULL;
Andi Kleen96284812017-03-20 13:17:10 -0700250 evsel->metric_name = NULL;
Andi Kleen37932c12017-03-20 13:17:08 -0700251 evsel->metric_events = NULL;
252 evsel->collect_stat = false;
Arnaldo Carvalho de Meloef1d1af2011-01-18 21:41:45 -0200253}
254
Arnaldo Carvalho de Meloef503832013-11-07 16:41:19 -0300255struct perf_evsel *perf_evsel__new_idx(struct perf_event_attr *attr, int idx)
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200256{
Arnaldo Carvalho de Meloce8ccff2014-10-09 15:29:51 -0300257 struct perf_evsel *evsel = zalloc(perf_evsel__object.size);
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200258
Arnaldo Carvalho de Meloef1d1af2011-01-18 21:41:45 -0200259 if (evsel != NULL)
260 perf_evsel__init(evsel, attr, idx);
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200261
Wang Nan03e0a7d2016-02-22 09:10:37 +0000262 if (perf_evsel__is_bpf_output(evsel)) {
Wang Nand37ba882016-04-01 13:26:42 +0000263 evsel->attr.sample_type |= (PERF_SAMPLE_RAW | PERF_SAMPLE_TIME |
264 PERF_SAMPLE_CPU | PERF_SAMPLE_PERIOD),
Wang Nan03e0a7d2016-02-22 09:10:37 +0000265 evsel->attr.sample_period = 1;
266 }
267
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200268 return evsel;
269}
270
Arnaldo Carvalho de Melo7c48dcf2016-07-28 18:33:20 -0300271struct perf_evsel *perf_evsel__new_cycles(void)
272{
273 struct perf_event_attr attr = {
274 .type = PERF_TYPE_HARDWARE,
275 .config = PERF_COUNT_HW_CPU_CYCLES,
Arnaldo Carvalho de Melod37a3692017-07-10 16:08:07 -0300276 .exclude_kernel = geteuid() != 0,
Arnaldo Carvalho de Melo7c48dcf2016-07-28 18:33:20 -0300277 };
278 struct perf_evsel *evsel;
279
280 event_attr_init(&attr);
Arnaldo Carvalho de Melo7a1ac112017-06-09 16:54:28 -0300281 /*
282 * Unnamed union member, not supported as struct member named
283 * initializer in older compilers such as gcc 4.4.7
284 *
285 * Just for probing the precise_ip:
286 */
287 attr.sample_period = 1;
Arnaldo Carvalho de Melo7c48dcf2016-07-28 18:33:20 -0300288
289 perf_event_attr__set_max_precise_ip(&attr);
Arnaldo Carvalho de Melo7a1ac112017-06-09 16:54:28 -0300290 /*
291 * Now let the usual logic to set up the perf_event_attr defaults
292 * to kick in when we return and before perf_evsel__open() is called.
293 */
294 attr.sample_period = 0;
Arnaldo Carvalho de Melo7c48dcf2016-07-28 18:33:20 -0300295
296 evsel = perf_evsel__new(&attr);
297 if (evsel == NULL)
298 goto out;
299
300 /* use asprintf() because free(evsel) assumes name is allocated */
301 if (asprintf(&evsel->name, "cycles%.*s",
302 attr.precise_ip ? attr.precise_ip + 1 : 0, ":ppp") < 0)
303 goto error_free;
304out:
305 return evsel;
306error_free:
307 perf_evsel__delete(evsel);
308 evsel = NULL;
309 goto out;
310}
311
Jiri Olsa8dd2a132015-09-07 10:38:06 +0200312/*
313 * Returns pointer with encoded error via <linux/err.h> interface.
314 */
Arnaldo Carvalho de Meloef503832013-11-07 16:41:19 -0300315struct perf_evsel *perf_evsel__newtp_idx(const char *sys, const char *name, int idx)
Arnaldo Carvalho de Meloefd2b922012-09-18 11:21:50 -0300316{
Arnaldo Carvalho de Meloce8ccff2014-10-09 15:29:51 -0300317 struct perf_evsel *evsel = zalloc(perf_evsel__object.size);
Jiri Olsa8dd2a132015-09-07 10:38:06 +0200318 int err = -ENOMEM;
Arnaldo Carvalho de Meloefd2b922012-09-18 11:21:50 -0300319
Jiri Olsa8dd2a132015-09-07 10:38:06 +0200320 if (evsel == NULL) {
321 goto out_err;
322 } else {
Arnaldo Carvalho de Meloefd2b922012-09-18 11:21:50 -0300323 struct perf_event_attr attr = {
Arnaldo Carvalho de Melo0b80f8b32012-09-26 12:28:26 -0300324 .type = PERF_TYPE_TRACEPOINT,
325 .sample_type = (PERF_SAMPLE_RAW | PERF_SAMPLE_TIME |
326 PERF_SAMPLE_CPU | PERF_SAMPLE_PERIOD),
Arnaldo Carvalho de Meloefd2b922012-09-18 11:21:50 -0300327 };
328
Arnaldo Carvalho de Meloe48ffe22012-09-26 17:11:38 -0300329 if (asprintf(&evsel->name, "%s:%s", sys, name) < 0)
330 goto out_free;
331
Jiri Olsa97978b32013-12-03 14:09:24 +0100332 evsel->tp_format = trace_event__tp_format(sys, name);
Jiri Olsa8dd2a132015-09-07 10:38:06 +0200333 if (IS_ERR(evsel->tp_format)) {
334 err = PTR_ERR(evsel->tp_format);
Arnaldo Carvalho de Meloefd2b922012-09-18 11:21:50 -0300335 goto out_free;
Jiri Olsa8dd2a132015-09-07 10:38:06 +0200336 }
Arnaldo Carvalho de Meloefd2b922012-09-18 11:21:50 -0300337
Arnaldo Carvalho de Melo0b80f8b32012-09-26 12:28:26 -0300338 event_attr_init(&attr);
Arnaldo Carvalho de Meloefd2b922012-09-18 11:21:50 -0300339 attr.config = evsel->tp_format->id;
Arnaldo Carvalho de Melo0b80f8b32012-09-26 12:28:26 -0300340 attr.sample_period = 1;
Arnaldo Carvalho de Meloefd2b922012-09-18 11:21:50 -0300341 perf_evsel__init(evsel, &attr, idx);
Arnaldo Carvalho de Meloefd2b922012-09-18 11:21:50 -0300342 }
343
344 return evsel;
345
346out_free:
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -0300347 zfree(&evsel->name);
Arnaldo Carvalho de Meloefd2b922012-09-18 11:21:50 -0300348 free(evsel);
Jiri Olsa8dd2a132015-09-07 10:38:06 +0200349out_err:
350 return ERR_PTR(err);
Arnaldo Carvalho de Meloefd2b922012-09-18 11:21:50 -0300351}
352
Arnaldo Carvalho de Melo8ad70132012-09-06 13:11:18 -0300353const char *perf_evsel__hw_names[PERF_COUNT_HW_MAX] = {
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300354 "cycles",
355 "instructions",
356 "cache-references",
357 "cache-misses",
358 "branches",
359 "branch-misses",
360 "bus-cycles",
361 "stalled-cycles-frontend",
362 "stalled-cycles-backend",
363 "ref-cycles",
364};
365
Arnaldo Carvalho de Melodd4f5222012-06-13 15:52:42 -0300366static const char *__perf_evsel__hw_name(u64 config)
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300367{
368 if (config < PERF_COUNT_HW_MAX && perf_evsel__hw_names[config])
369 return perf_evsel__hw_names[config];
370
371 return "unknown-hardware";
372}
373
Arnaldo Carvalho de Melo27f18612012-06-11 13:33:09 -0300374static int perf_evsel__add_modifiers(struct perf_evsel *evsel, char *bf, size_t size)
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300375{
Arnaldo Carvalho de Melo27f18612012-06-11 13:33:09 -0300376 int colon = 0, r = 0;
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300377 struct perf_event_attr *attr = &evsel->attr;
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300378 bool exclude_guest_default = false;
379
380#define MOD_PRINT(context, mod) do { \
381 if (!attr->exclude_##context) { \
Arnaldo Carvalho de Melo27f18612012-06-11 13:33:09 -0300382 if (!colon) colon = ++r; \
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300383 r += scnprintf(bf + r, size - r, "%c", mod); \
384 } } while(0)
385
386 if (attr->exclude_kernel || attr->exclude_user || attr->exclude_hv) {
387 MOD_PRINT(kernel, 'k');
388 MOD_PRINT(user, 'u');
389 MOD_PRINT(hv, 'h');
390 exclude_guest_default = true;
391 }
392
393 if (attr->precise_ip) {
394 if (!colon)
Arnaldo Carvalho de Melo27f18612012-06-11 13:33:09 -0300395 colon = ++r;
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300396 r += scnprintf(bf + r, size - r, "%.*s", attr->precise_ip, "ppp");
397 exclude_guest_default = true;
398 }
399
400 if (attr->exclude_host || attr->exclude_guest == exclude_guest_default) {
401 MOD_PRINT(host, 'H');
402 MOD_PRINT(guest, 'G');
403 }
404#undef MOD_PRINT
405 if (colon)
Arnaldo Carvalho de Melo27f18612012-06-11 13:33:09 -0300406 bf[colon - 1] = ':';
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300407 return r;
408}
409
Arnaldo Carvalho de Melo27f18612012-06-11 13:33:09 -0300410static int perf_evsel__hw_name(struct perf_evsel *evsel, char *bf, size_t size)
411{
412 int r = scnprintf(bf, size, "%s", __perf_evsel__hw_name(evsel->attr.config));
413 return r + perf_evsel__add_modifiers(evsel, bf + r, size - r);
414}
415
Arnaldo Carvalho de Melo8ad70132012-09-06 13:11:18 -0300416const char *perf_evsel__sw_names[PERF_COUNT_SW_MAX] = {
Arnaldo Carvalho de Melo335c2f52012-06-11 14:36:20 -0300417 "cpu-clock",
418 "task-clock",
419 "page-faults",
420 "context-switches",
Arnaldo Carvalho de Melo8ad70132012-09-06 13:11:18 -0300421 "cpu-migrations",
Arnaldo Carvalho de Melo335c2f52012-06-11 14:36:20 -0300422 "minor-faults",
423 "major-faults",
424 "alignment-faults",
425 "emulation-faults",
Adrian Hunterd22d1a22013-08-31 21:50:52 +0300426 "dummy",
Arnaldo Carvalho de Melo335c2f52012-06-11 14:36:20 -0300427};
428
Arnaldo Carvalho de Melodd4f5222012-06-13 15:52:42 -0300429static const char *__perf_evsel__sw_name(u64 config)
Arnaldo Carvalho de Melo335c2f52012-06-11 14:36:20 -0300430{
431 if (config < PERF_COUNT_SW_MAX && perf_evsel__sw_names[config])
432 return perf_evsel__sw_names[config];
433 return "unknown-software";
434}
435
436static int perf_evsel__sw_name(struct perf_evsel *evsel, char *bf, size_t size)
437{
438 int r = scnprintf(bf, size, "%s", __perf_evsel__sw_name(evsel->attr.config));
439 return r + perf_evsel__add_modifiers(evsel, bf + r, size - r);
440}
441
Jiri Olsa287e74a2012-06-28 23:18:49 +0200442static int __perf_evsel__bp_name(char *bf, size_t size, u64 addr, u64 type)
443{
444 int r;
445
446 r = scnprintf(bf, size, "mem:0x%" PRIx64 ":", addr);
447
448 if (type & HW_BREAKPOINT_R)
449 r += scnprintf(bf + r, size - r, "r");
450
451 if (type & HW_BREAKPOINT_W)
452 r += scnprintf(bf + r, size - r, "w");
453
454 if (type & HW_BREAKPOINT_X)
455 r += scnprintf(bf + r, size - r, "x");
456
457 return r;
458}
459
460static int perf_evsel__bp_name(struct perf_evsel *evsel, char *bf, size_t size)
461{
462 struct perf_event_attr *attr = &evsel->attr;
463 int r = __perf_evsel__bp_name(bf, size, attr->bp_addr, attr->bp_type);
464 return r + perf_evsel__add_modifiers(evsel, bf + r, size - r);
465}
466
Arnaldo Carvalho de Melo0b668bc2012-06-11 14:08:07 -0300467const char *perf_evsel__hw_cache[PERF_COUNT_HW_CACHE_MAX]
468 [PERF_EVSEL__MAX_ALIASES] = {
469 { "L1-dcache", "l1-d", "l1d", "L1-data", },
470 { "L1-icache", "l1-i", "l1i", "L1-instruction", },
471 { "LLC", "L2", },
472 { "dTLB", "d-tlb", "Data-TLB", },
473 { "iTLB", "i-tlb", "Instruction-TLB", },
474 { "branch", "branches", "bpu", "btb", "bpc", },
475 { "node", },
476};
477
478const char *perf_evsel__hw_cache_op[PERF_COUNT_HW_CACHE_OP_MAX]
479 [PERF_EVSEL__MAX_ALIASES] = {
480 { "load", "loads", "read", },
481 { "store", "stores", "write", },
482 { "prefetch", "prefetches", "speculative-read", "speculative-load", },
483};
484
485const char *perf_evsel__hw_cache_result[PERF_COUNT_HW_CACHE_RESULT_MAX]
486 [PERF_EVSEL__MAX_ALIASES] = {
487 { "refs", "Reference", "ops", "access", },
488 { "misses", "miss", },
489};
490
491#define C(x) PERF_COUNT_HW_CACHE_##x
492#define CACHE_READ (1 << C(OP_READ))
493#define CACHE_WRITE (1 << C(OP_WRITE))
494#define CACHE_PREFETCH (1 << C(OP_PREFETCH))
495#define COP(x) (1 << x)
496
497/*
498 * cache operartion stat
499 * L1I : Read and prefetch only
500 * ITLB and BPU : Read-only
501 */
502static unsigned long perf_evsel__hw_cache_stat[C(MAX)] = {
503 [C(L1D)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
504 [C(L1I)] = (CACHE_READ | CACHE_PREFETCH),
505 [C(LL)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
506 [C(DTLB)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
507 [C(ITLB)] = (CACHE_READ),
508 [C(BPU)] = (CACHE_READ),
509 [C(NODE)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
510};
511
512bool perf_evsel__is_cache_op_valid(u8 type, u8 op)
513{
514 if (perf_evsel__hw_cache_stat[type] & COP(op))
515 return true; /* valid */
516 else
517 return false; /* invalid */
518}
519
520int __perf_evsel__hw_cache_type_op_res_name(u8 type, u8 op, u8 result,
521 char *bf, size_t size)
522{
523 if (result) {
524 return scnprintf(bf, size, "%s-%s-%s", perf_evsel__hw_cache[type][0],
525 perf_evsel__hw_cache_op[op][0],
526 perf_evsel__hw_cache_result[result][0]);
527 }
528
529 return scnprintf(bf, size, "%s-%s", perf_evsel__hw_cache[type][0],
530 perf_evsel__hw_cache_op[op][1]);
531}
532
Arnaldo Carvalho de Melodd4f5222012-06-13 15:52:42 -0300533static int __perf_evsel__hw_cache_name(u64 config, char *bf, size_t size)
Arnaldo Carvalho de Melo0b668bc2012-06-11 14:08:07 -0300534{
535 u8 op, result, type = (config >> 0) & 0xff;
536 const char *err = "unknown-ext-hardware-cache-type";
537
Arnaldo Carvalho de Meloc53412e2016-08-18 16:30:28 -0300538 if (type >= PERF_COUNT_HW_CACHE_MAX)
Arnaldo Carvalho de Melo0b668bc2012-06-11 14:08:07 -0300539 goto out_err;
540
541 op = (config >> 8) & 0xff;
542 err = "unknown-ext-hardware-cache-op";
Arnaldo Carvalho de Meloc53412e2016-08-18 16:30:28 -0300543 if (op >= PERF_COUNT_HW_CACHE_OP_MAX)
Arnaldo Carvalho de Melo0b668bc2012-06-11 14:08:07 -0300544 goto out_err;
545
546 result = (config >> 16) & 0xff;
547 err = "unknown-ext-hardware-cache-result";
Arnaldo Carvalho de Meloc53412e2016-08-18 16:30:28 -0300548 if (result >= PERF_COUNT_HW_CACHE_RESULT_MAX)
Arnaldo Carvalho de Melo0b668bc2012-06-11 14:08:07 -0300549 goto out_err;
550
551 err = "invalid-cache";
552 if (!perf_evsel__is_cache_op_valid(type, op))
553 goto out_err;
554
555 return __perf_evsel__hw_cache_type_op_res_name(type, op, result, bf, size);
556out_err:
557 return scnprintf(bf, size, "%s", err);
558}
559
560static int perf_evsel__hw_cache_name(struct perf_evsel *evsel, char *bf, size_t size)
561{
562 int ret = __perf_evsel__hw_cache_name(evsel->attr.config, bf, size);
563 return ret + perf_evsel__add_modifiers(evsel, bf + ret, size - ret);
564}
565
Arnaldo Carvalho de Melo6eef3d92012-06-13 11:53:37 -0300566static int perf_evsel__raw_name(struct perf_evsel *evsel, char *bf, size_t size)
567{
568 int ret = scnprintf(bf, size, "raw 0x%" PRIx64, evsel->attr.config);
569 return ret + perf_evsel__add_modifiers(evsel, bf + ret, size - ret);
570}
571
Arnaldo Carvalho de Melo7289f832012-06-12 12:34:58 -0300572const char *perf_evsel__name(struct perf_evsel *evsel)
Arnaldo Carvalho de Meloa4460832012-06-12 10:29:12 -0300573{
Arnaldo Carvalho de Melo7289f832012-06-12 12:34:58 -0300574 char bf[128];
Arnaldo Carvalho de Meloa4460832012-06-12 10:29:12 -0300575
Arnaldo Carvalho de Melo7289f832012-06-12 12:34:58 -0300576 if (evsel->name)
577 return evsel->name;
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300578
579 switch (evsel->attr.type) {
580 case PERF_TYPE_RAW:
Arnaldo Carvalho de Melo6eef3d92012-06-13 11:53:37 -0300581 perf_evsel__raw_name(evsel, bf, sizeof(bf));
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300582 break;
583
584 case PERF_TYPE_HARDWARE:
Arnaldo Carvalho de Melo7289f832012-06-12 12:34:58 -0300585 perf_evsel__hw_name(evsel, bf, sizeof(bf));
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300586 break;
Arnaldo Carvalho de Melo0b668bc2012-06-11 14:08:07 -0300587
588 case PERF_TYPE_HW_CACHE:
Arnaldo Carvalho de Melo7289f832012-06-12 12:34:58 -0300589 perf_evsel__hw_cache_name(evsel, bf, sizeof(bf));
Arnaldo Carvalho de Melo0b668bc2012-06-11 14:08:07 -0300590 break;
591
Arnaldo Carvalho de Melo335c2f52012-06-11 14:36:20 -0300592 case PERF_TYPE_SOFTWARE:
Arnaldo Carvalho de Melo7289f832012-06-12 12:34:58 -0300593 perf_evsel__sw_name(evsel, bf, sizeof(bf));
Arnaldo Carvalho de Melo335c2f52012-06-11 14:36:20 -0300594 break;
595
Arnaldo Carvalho de Meloa4460832012-06-12 10:29:12 -0300596 case PERF_TYPE_TRACEPOINT:
Arnaldo Carvalho de Melo7289f832012-06-12 12:34:58 -0300597 scnprintf(bf, sizeof(bf), "%s", "unknown tracepoint");
Arnaldo Carvalho de Meloa4460832012-06-12 10:29:12 -0300598 break;
599
Jiri Olsa287e74a2012-06-28 23:18:49 +0200600 case PERF_TYPE_BREAKPOINT:
601 perf_evsel__bp_name(evsel, bf, sizeof(bf));
602 break;
603
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300604 default:
Robert Richterca1b1452012-08-16 21:10:18 +0200605 scnprintf(bf, sizeof(bf), "unknown attr type: %d",
606 evsel->attr.type);
Arnaldo Carvalho de Meloa4460832012-06-12 10:29:12 -0300607 break;
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300608 }
609
Arnaldo Carvalho de Melo7289f832012-06-12 12:34:58 -0300610 evsel->name = strdup(bf);
611
612 return evsel->name ?: "unknown";
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300613}
614
Namhyung Kim717e2632013-01-22 18:09:44 +0900615const char *perf_evsel__group_name(struct perf_evsel *evsel)
616{
617 return evsel->group_name ?: "anon group";
618}
619
620int perf_evsel__group_desc(struct perf_evsel *evsel, char *buf, size_t size)
621{
622 int ret;
623 struct perf_evsel *pos;
624 const char *group_name = perf_evsel__group_name(evsel);
625
626 ret = scnprintf(buf, size, "%s", group_name);
627
628 ret += scnprintf(buf + ret, size - ret, " { %s",
629 perf_evsel__name(evsel));
630
631 for_each_group_member(pos, evsel)
632 ret += scnprintf(buf + ret, size - ret, ", %s",
633 perf_evsel__name(pos));
634
635 ret += scnprintf(buf + ret, size - ret, " }");
636
637 return ret;
638}
639
Arnaldo Carvalho de Melo01e0d502016-04-11 18:39:37 -0300640void perf_evsel__config_callchain(struct perf_evsel *evsel,
641 struct record_opts *opts,
642 struct callchain_param *param)
Jiri Olsa6bedfab2014-03-02 16:56:40 +0100643{
644 bool function = perf_evsel__is_function_event(evsel);
645 struct perf_event_attr *attr = &evsel->attr;
646
647 perf_evsel__set_sample_bit(evsel, CALLCHAIN);
648
Arnaldo Carvalho de Melo792d48b2016-04-28 19:03:42 -0300649 attr->sample_max_stack = param->max_stack;
650
Kan Liangc3a6a8c2015-08-04 04:30:20 -0400651 if (param->record_mode == CALLCHAIN_LBR) {
Kan Liangaad2b212015-01-05 13:23:04 -0500652 if (!opts->branch_stack) {
653 if (attr->exclude_user) {
654 pr_warning("LBR callstack option is only available "
655 "to get user callchain information. "
656 "Falling back to framepointers.\n");
657 } else {
658 perf_evsel__set_sample_bit(evsel, BRANCH_STACK);
659 attr->branch_sample_type = PERF_SAMPLE_BRANCH_USER |
Andi Kleenbd0f8892015-12-11 16:12:24 -0800660 PERF_SAMPLE_BRANCH_CALL_STACK |
661 PERF_SAMPLE_BRANCH_NO_CYCLES |
662 PERF_SAMPLE_BRANCH_NO_FLAGS;
Kan Liangaad2b212015-01-05 13:23:04 -0500663 }
664 } else
665 pr_warning("Cannot use LBR callstack with branch stack. "
666 "Falling back to framepointers.\n");
667 }
668
Kan Liangc3a6a8c2015-08-04 04:30:20 -0400669 if (param->record_mode == CALLCHAIN_DWARF) {
Jiri Olsa6bedfab2014-03-02 16:56:40 +0100670 if (!function) {
671 perf_evsel__set_sample_bit(evsel, REGS_USER);
672 perf_evsel__set_sample_bit(evsel, STACK_USER);
673 attr->sample_regs_user = PERF_REGS_MASK;
Kan Liangc3a6a8c2015-08-04 04:30:20 -0400674 attr->sample_stack_user = param->dump_size;
Jiri Olsa6bedfab2014-03-02 16:56:40 +0100675 attr->exclude_callchain_user = 1;
676 } else {
677 pr_info("Cannot use DWARF unwind for function trace event,"
678 " falling back to framepointers.\n");
679 }
680 }
681
682 if (function) {
683 pr_info("Disabling user space callchains for function trace event.\n");
684 attr->exclude_callchain_user = 1;
685 }
686}
687
Kan Liangd457c962015-08-11 06:30:47 -0400688static void
689perf_evsel__reset_callgraph(struct perf_evsel *evsel,
690 struct callchain_param *param)
691{
692 struct perf_event_attr *attr = &evsel->attr;
693
694 perf_evsel__reset_sample_bit(evsel, CALLCHAIN);
695 if (param->record_mode == CALLCHAIN_LBR) {
696 perf_evsel__reset_sample_bit(evsel, BRANCH_STACK);
697 attr->branch_sample_type &= ~(PERF_SAMPLE_BRANCH_USER |
698 PERF_SAMPLE_BRANCH_CALL_STACK);
699 }
700 if (param->record_mode == CALLCHAIN_DWARF) {
701 perf_evsel__reset_sample_bit(evsel, REGS_USER);
702 perf_evsel__reset_sample_bit(evsel, STACK_USER);
703 }
704}
705
706static void apply_config_terms(struct perf_evsel *evsel,
707 struct record_opts *opts)
Jiri Olsa930a2e22015-07-29 05:42:10 -0400708{
709 struct perf_evsel_config_term *term;
Kan Liang32067712015-08-04 04:30:19 -0400710 struct list_head *config_terms = &evsel->config_terms;
711 struct perf_event_attr *attr = &evsel->attr;
Kan Liangd457c962015-08-11 06:30:47 -0400712 struct callchain_param param;
713 u32 dump_size = 0;
Arnaldo Carvalho de Melo792d48b2016-04-28 19:03:42 -0300714 int max_stack = 0;
715 const char *callgraph_buf = NULL;
Kan Liangd457c962015-08-11 06:30:47 -0400716
717 /* callgraph default */
718 param.record_mode = callchain_param.record_mode;
Jiri Olsa930a2e22015-07-29 05:42:10 -0400719
720 list_for_each_entry(term, config_terms, list) {
721 switch (term->type) {
Jiri Olsaee4c7582015-07-29 05:42:11 -0400722 case PERF_EVSEL__CONFIG_TERM_PERIOD:
723 attr->sample_period = term->val.period;
Jiri Olsaab35a7d2015-08-08 19:12:10 +0200724 attr->freq = 0;
Kan Liang32067712015-08-04 04:30:19 -0400725 break;
Namhyung Kim09af2a52015-08-09 15:45:23 +0900726 case PERF_EVSEL__CONFIG_TERM_FREQ:
727 attr->sample_freq = term->val.freq;
728 attr->freq = 1;
729 break;
Kan Liang32067712015-08-04 04:30:19 -0400730 case PERF_EVSEL__CONFIG_TERM_TIME:
731 if (term->val.time)
732 perf_evsel__set_sample_bit(evsel, TIME);
733 else
734 perf_evsel__reset_sample_bit(evsel, TIME);
735 break;
Kan Liangd457c962015-08-11 06:30:47 -0400736 case PERF_EVSEL__CONFIG_TERM_CALLGRAPH:
737 callgraph_buf = term->val.callgraph;
738 break;
Andi Kleenac12f672016-10-12 14:02:06 -0700739 case PERF_EVSEL__CONFIG_TERM_BRANCH:
740 if (term->val.branch && strcmp(term->val.branch, "no")) {
741 perf_evsel__set_sample_bit(evsel, BRANCH_STACK);
742 parse_branch_str(term->val.branch,
743 &attr->branch_sample_type);
744 } else
745 perf_evsel__reset_sample_bit(evsel, BRANCH_STACK);
746 break;
Kan Liangd457c962015-08-11 06:30:47 -0400747 case PERF_EVSEL__CONFIG_TERM_STACK_USER:
748 dump_size = term->val.stack_user;
749 break;
Arnaldo Carvalho de Melo792d48b2016-04-28 19:03:42 -0300750 case PERF_EVSEL__CONFIG_TERM_MAX_STACK:
751 max_stack = term->val.max_stack;
752 break;
Wang Nan374ce932015-10-28 10:55:02 +0000753 case PERF_EVSEL__CONFIG_TERM_INHERIT:
754 /*
755 * attr->inherit should has already been set by
756 * perf_evsel__config. If user explicitly set
757 * inherit using config terms, override global
758 * opt->no_inherit setting.
759 */
760 attr->inherit = term->val.inherit ? 1 : 0;
761 break;
Wang Nan626a6b72016-07-14 08:34:45 +0000762 case PERF_EVSEL__CONFIG_TERM_OVERWRITE:
763 attr->write_backward = term->val.overwrite ? 1 : 0;
764 break;
Jiri Olsa930a2e22015-07-29 05:42:10 -0400765 default:
766 break;
767 }
768 }
Kan Liangd457c962015-08-11 06:30:47 -0400769
770 /* User explicitly set per-event callgraph, clear the old setting and reset. */
Arnaldo Carvalho de Melo792d48b2016-04-28 19:03:42 -0300771 if ((callgraph_buf != NULL) || (dump_size > 0) || max_stack) {
772 if (max_stack) {
773 param.max_stack = max_stack;
774 if (callgraph_buf == NULL)
775 callgraph_buf = "fp";
776 }
Kan Liangd457c962015-08-11 06:30:47 -0400777
778 /* parse callgraph parameters */
779 if (callgraph_buf != NULL) {
Kan Liangf9db0d02015-08-11 06:30:48 -0400780 if (!strcmp(callgraph_buf, "no")) {
781 param.enabled = false;
782 param.record_mode = CALLCHAIN_NONE;
783 } else {
784 param.enabled = true;
785 if (parse_callchain_record(callgraph_buf, &param)) {
786 pr_err("per-event callgraph setting for %s failed. "
787 "Apply callgraph global setting for it\n",
788 evsel->name);
789 return;
790 }
Kan Liangd457c962015-08-11 06:30:47 -0400791 }
792 }
793 if (dump_size > 0) {
794 dump_size = round_up(dump_size, sizeof(u64));
795 param.dump_size = dump_size;
796 }
797
798 /* If global callgraph set, clear it */
799 if (callchain_param.enabled)
800 perf_evsel__reset_callgraph(evsel, &callchain_param);
801
802 /* set perf-event callgraph */
803 if (param.enabled)
Arnaldo Carvalho de Melo01e0d502016-04-11 18:39:37 -0300804 perf_evsel__config_callchain(evsel, opts, &param);
Kan Liangd457c962015-08-11 06:30:47 -0400805 }
Jiri Olsa930a2e22015-07-29 05:42:10 -0400806}
807
Jiri Olsa774cb492012-11-12 18:34:01 +0100808/*
809 * The enable_on_exec/disabled value strategy:
810 *
811 * 1) For any type of traced program:
812 * - all independent events and group leaders are disabled
813 * - all group members are enabled
814 *
815 * Group members are ruled by group leaders. They need to
816 * be enabled, because the group scheduling relies on that.
817 *
818 * 2) For traced programs executed by perf:
819 * - all independent events and group leaders have
820 * enable_on_exec set
821 * - we don't specifically enable or disable any event during
822 * the record command
823 *
824 * Independent events and group leaders are initially disabled
825 * and get enabled by exec. Group members are ruled by group
826 * leaders as stated in 1).
827 *
828 * 3) For traced programs attached by perf (pid/tid):
829 * - we specifically enable or disable all events during
830 * the record command
831 *
832 * When attaching events to already running traced we
833 * enable/disable events specifically, as there's no
834 * initial traced exec call.
835 */
Arnaldo Carvalho de Meloe68ae9c2016-04-11 18:15:29 -0300836void perf_evsel__config(struct perf_evsel *evsel, struct record_opts *opts,
837 struct callchain_param *callchain)
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200838{
Jiri Olsa3c176312012-10-10 17:39:03 +0200839 struct perf_evsel *leader = evsel->leader;
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200840 struct perf_event_attr *attr = &evsel->attr;
Adrian Hunter60b08962014-07-31 09:00:52 +0300841 int track = evsel->tracking;
Adrian Hunter3aa59392013-11-15 15:52:29 +0200842 bool per_cpu = opts->target.default_per_cpu && !opts->target.per_thread;
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200843
Arnaldo Carvalho de Melo594ac612012-12-13 13:13:07 -0300844 attr->sample_id_all = perf_missing_features.sample_id_all ? 0 : 1;
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200845 attr->inherit = !opts->no_inherit;
Wang Nan626a6b72016-07-14 08:34:45 +0000846 attr->write_backward = opts->overwrite ? 1 : 0;
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200847
Arnaldo Carvalho de Melo7be5ebe2012-12-10 14:53:43 -0300848 perf_evsel__set_sample_bit(evsel, IP);
849 perf_evsel__set_sample_bit(evsel, TID);
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200850
Jiri Olsa3c176312012-10-10 17:39:03 +0200851 if (evsel->sample_read) {
852 perf_evsel__set_sample_bit(evsel, READ);
853
854 /*
855 * We need ID even in case of single event, because
856 * PERF_SAMPLE_READ process ID specific data.
857 */
Adrian Hunter75562572013-08-27 11:23:09 +0300858 perf_evsel__set_sample_id(evsel, false);
Jiri Olsa3c176312012-10-10 17:39:03 +0200859
860 /*
861 * Apply group format only if we belong to group
862 * with more than one members.
863 */
864 if (leader->nr_members > 1) {
865 attr->read_format |= PERF_FORMAT_GROUP;
866 attr->inherit = 0;
867 }
868 }
869
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200870 /*
Namhyung Kim17314e22014-06-09 14:43:37 +0900871 * We default some events to have a default interval. But keep
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200872 * it a weak assumption overridable by the user.
873 */
Namhyung Kim17314e22014-06-09 14:43:37 +0900874 if (!attr->sample_period || (opts->user_freq != UINT_MAX ||
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200875 opts->user_interval != ULLONG_MAX)) {
876 if (opts->freq) {
Arnaldo Carvalho de Melo7be5ebe2012-12-10 14:53:43 -0300877 perf_evsel__set_sample_bit(evsel, PERIOD);
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200878 attr->freq = 1;
879 attr->sample_freq = opts->freq;
880 } else {
881 attr->sample_period = opts->default_interval;
882 }
883 }
884
Jiri Olsa3c176312012-10-10 17:39:03 +0200885 /*
886 * Disable sampling for all group members other
887 * than leader in case leader 'leads' the sampling.
888 */
889 if ((leader != evsel) && leader->sample_read) {
890 attr->sample_freq = 0;
891 attr->sample_period = 0;
892 }
893
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200894 if (opts->no_samples)
895 attr->sample_freq = 0;
896
897 if (opts->inherit_stat)
898 attr->inherit_stat = 1;
899
900 if (opts->sample_address) {
Arnaldo Carvalho de Melo7be5ebe2012-12-10 14:53:43 -0300901 perf_evsel__set_sample_bit(evsel, ADDR);
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200902 attr->mmap_data = track;
903 }
904
Jiri Olsaf1403732014-11-13 18:21:03 +0100905 /*
906 * We don't allow user space callchains for function trace
907 * event, due to issues with page faults while tracing page
908 * fault handler and its overall trickiness nature.
909 */
910 if (perf_evsel__is_function_event(evsel))
911 evsel->attr.exclude_callchain_user = 1;
912
Arnaldo Carvalho de Meloe68ae9c2016-04-11 18:15:29 -0300913 if (callchain && callchain->enabled && !evsel->no_aux_samples)
Arnaldo Carvalho de Melo01e0d502016-04-11 18:39:37 -0300914 perf_evsel__config_callchain(evsel, opts, callchain);
Jiri Olsa26d33022012-08-07 15:20:47 +0200915
Stephane Eranian6a21c0b2014-09-24 13:48:39 +0200916 if (opts->sample_intr_regs) {
Stephane Eranianbcc84ec2015-08-31 18:41:12 +0200917 attr->sample_regs_intr = opts->sample_intr_regs;
Stephane Eranian6a21c0b2014-09-24 13:48:39 +0200918 perf_evsel__set_sample_bit(evsel, REGS_INTR);
919 }
920
Jiri Olsab6f35ed2016-08-01 20:02:35 +0200921 if (target__has_cpu(&opts->target) || opts->sample_cpu)
Arnaldo Carvalho de Melo7be5ebe2012-12-10 14:53:43 -0300922 perf_evsel__set_sample_bit(evsel, CPU);
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200923
Andrew Vagin3e76ac72011-12-20 17:32:45 +0300924 if (opts->period)
Arnaldo Carvalho de Melo7be5ebe2012-12-10 14:53:43 -0300925 perf_evsel__set_sample_bit(evsel, PERIOD);
Andrew Vagin3e76ac72011-12-20 17:32:45 +0300926
Andi Kleen8affc2b2014-07-31 14:45:04 +0800927 /*
Adam Buchbinderbd1a0be2016-02-24 10:02:25 -0800928 * When the user explicitly disabled time don't force it here.
Andi Kleen8affc2b2014-07-31 14:45:04 +0800929 */
930 if (opts->sample_time &&
931 (!perf_missing_features.sample_id_all &&
Adrian Hunter3abebc52015-07-06 14:51:01 +0300932 (!opts->no_inherit || target__has_cpu(&opts->target) || per_cpu ||
933 opts->sample_time_set)))
Arnaldo Carvalho de Melo7be5ebe2012-12-10 14:53:43 -0300934 perf_evsel__set_sample_bit(evsel, TIME);
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200935
Adrian Hunter6ff1ce72014-07-14 13:02:56 +0300936 if (opts->raw_samples && !evsel->no_aux_samples) {
Arnaldo Carvalho de Melo7be5ebe2012-12-10 14:53:43 -0300937 perf_evsel__set_sample_bit(evsel, TIME);
938 perf_evsel__set_sample_bit(evsel, RAW);
939 perf_evsel__set_sample_bit(evsel, CPU);
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200940 }
941
Stephane Eranianccf49bf2013-01-24 16:10:37 +0100942 if (opts->sample_address)
Adrian Hunter1e7ed5e2013-11-01 15:51:35 +0200943 perf_evsel__set_sample_bit(evsel, DATA_SRC);
Stephane Eranianccf49bf2013-01-24 16:10:37 +0100944
Arnaldo Carvalho de Melo509051e2014-01-14 17:52:14 -0300945 if (opts->no_buffering) {
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200946 attr->watermark = 0;
947 attr->wakeup_events = 1;
948 }
Adrian Hunter6ff1ce72014-07-14 13:02:56 +0300949 if (opts->branch_stack && !evsel->no_aux_samples) {
Arnaldo Carvalho de Melo7be5ebe2012-12-10 14:53:43 -0300950 perf_evsel__set_sample_bit(evsel, BRANCH_STACK);
Roberto Agostino Vitillobdfebd82012-02-09 23:21:02 +0100951 attr->branch_sample_type = opts->branch_stack;
952 }
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200953
Andi Kleen05484292013-01-24 16:10:29 +0100954 if (opts->sample_weight)
Adrian Hunter1e7ed5e2013-11-01 15:51:35 +0200955 perf_evsel__set_sample_bit(evsel, WEIGHT);
Andi Kleen05484292013-01-24 16:10:29 +0100956
Namhyung Kim62e503b2015-01-29 17:06:46 +0900957 attr->task = track;
Stephane Eranian5c5e8542013-08-21 12:10:25 +0200958 attr->mmap = track;
Don Zickusa5a5ba72014-05-30 10:49:42 -0400959 attr->mmap2 = track && !perf_missing_features.mmap2;
Stephane Eranian5c5e8542013-08-21 12:10:25 +0200960 attr->comm = track;
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200961
Hari Bathinif3b36142017-03-08 02:11:43 +0530962 if (opts->record_namespaces)
963 attr->namespaces = track;
964
Adrian Hunterb757bb02015-07-21 12:44:04 +0300965 if (opts->record_switch_events)
966 attr->context_switch = track;
967
Andi Kleen475eeab2013-09-20 07:40:43 -0700968 if (opts->sample_transaction)
Adrian Hunter1e7ed5e2013-11-01 15:51:35 +0200969 perf_evsel__set_sample_bit(evsel, TRANSACTION);
Andi Kleen475eeab2013-09-20 07:40:43 -0700970
Andi Kleen85c273d2015-02-24 15:13:40 -0800971 if (opts->running_time) {
972 evsel->attr.read_format |=
973 PERF_FORMAT_TOTAL_TIME_ENABLED |
974 PERF_FORMAT_TOTAL_TIME_RUNNING;
975 }
976
Jiri Olsa774cb492012-11-12 18:34:01 +0100977 /*
978 * XXX see the function comment above
979 *
980 * Disabling only independent events or group leaders,
981 * keeping group members enabled.
982 */
Namhyung Kim823254e2012-11-29 15:38:30 +0900983 if (perf_evsel__is_group_leader(evsel))
Jiri Olsa774cb492012-11-12 18:34:01 +0100984 attr->disabled = 1;
985
986 /*
987 * Setting enable_on_exec for independent events and
988 * group leaders for traced executed by perf.
989 */
Andi Kleen6619a532014-01-11 13:38:27 -0800990 if (target__none(&opts->target) && perf_evsel__is_group_leader(evsel) &&
991 !opts->initial_delay)
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200992 attr->enable_on_exec = 1;
Adrian Hunter2afd2bc2014-07-14 13:02:57 +0300993
994 if (evsel->immediate) {
995 attr->disabled = 0;
996 attr->enable_on_exec = 0;
997 }
Peter Zijlstra814c8c32015-03-31 00:19:31 +0200998
999 clockid = opts->clockid;
1000 if (opts->use_clockid) {
1001 attr->use_clockid = 1;
1002 attr->clockid = opts->clockid;
1003 }
Jiri Olsa930a2e22015-07-29 05:42:10 -04001004
Jiri Olsa7f94af72015-10-05 20:06:05 +02001005 if (evsel->precise_max)
1006 perf_event_attr__set_max_precise_ip(attr);
1007
Jiri Olsa85723882016-02-15 09:34:31 +01001008 if (opts->all_user) {
1009 attr->exclude_kernel = 1;
1010 attr->exclude_user = 0;
1011 }
1012
1013 if (opts->all_kernel) {
1014 attr->exclude_kernel = 0;
1015 attr->exclude_user = 1;
1016 }
1017
Jiri Olsa930a2e22015-07-29 05:42:10 -04001018 /*
1019 * Apply event specific term settings,
1020 * it overloads any global configuration.
1021 */
Kan Liangd457c962015-08-11 06:30:47 -04001022 apply_config_terms(evsel, opts);
Jiri Olsaa359c172016-12-13 08:46:22 +01001023
1024 evsel->ignore_missing_thread = opts->ignore_missing_thread;
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -02001025}
1026
Arnaldo Carvalho de Melo88858462014-10-13 13:30:27 -03001027static int perf_evsel__alloc_fd(struct perf_evsel *evsel, int ncpus, int nthreads)
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -02001028{
Adrian Hunterbf8e8f42014-07-31 09:00:51 +03001029 if (evsel->system_wide)
1030 nthreads = 1;
1031
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -02001032 evsel->fd = xyarray__new(ncpus, nthreads, sizeof(int));
David Ahern4af4c952011-05-27 09:58:34 -06001033
1034 if (evsel->fd) {
Arnaldo Carvalho de Melo18ef15c2016-10-03 11:07:24 -03001035 int cpu, thread;
David Ahern4af4c952011-05-27 09:58:34 -06001036 for (cpu = 0; cpu < ncpus; cpu++) {
1037 for (thread = 0; thread < nthreads; thread++) {
1038 FD(evsel, cpu, thread) = -1;
1039 }
1040 }
1041 }
1042
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -02001043 return evsel->fd != NULL ? 0 : -ENOMEM;
1044}
1045
Andi Kleene2407be2013-08-02 17:41:10 -07001046static int perf_evsel__run_ioctl(struct perf_evsel *evsel, int ncpus, int nthreads,
1047 int ioc, void *arg)
Arnaldo Carvalho de Melo745cefc2012-09-26 15:07:39 -03001048{
1049 int cpu, thread;
1050
Adrian Hunterbf8e8f42014-07-31 09:00:51 +03001051 if (evsel->system_wide)
1052 nthreads = 1;
1053
Arnaldo Carvalho de Melo745cefc2012-09-26 15:07:39 -03001054 for (cpu = 0; cpu < ncpus; cpu++) {
1055 for (thread = 0; thread < nthreads; thread++) {
1056 int fd = FD(evsel, cpu, thread),
Andi Kleene2407be2013-08-02 17:41:10 -07001057 err = ioctl(fd, ioc, arg);
Arnaldo Carvalho de Melo745cefc2012-09-26 15:07:39 -03001058
1059 if (err)
1060 return err;
1061 }
1062 }
1063
1064 return 0;
1065}
1066
Arnaldo Carvalho de Melof47805a2015-07-03 15:53:49 -03001067int perf_evsel__apply_filter(struct perf_evsel *evsel, int ncpus, int nthreads,
1068 const char *filter)
Andi Kleene2407be2013-08-02 17:41:10 -07001069{
1070 return perf_evsel__run_ioctl(evsel, ncpus, nthreads,
1071 PERF_EVENT_IOC_SET_FILTER,
1072 (void *)filter);
1073}
1074
Arnaldo Carvalho de Melo12467ae2015-07-03 17:05:50 -03001075int perf_evsel__set_filter(struct perf_evsel *evsel, const char *filter)
1076{
1077 char *new_filter = strdup(filter);
1078
1079 if (new_filter != NULL) {
1080 free(evsel->filter);
1081 evsel->filter = new_filter;
1082 return 0;
1083 }
1084
1085 return -1;
1086}
1087
Mathieu Poirier3541c032016-09-16 08:44:04 -06001088static int perf_evsel__append_filter(struct perf_evsel *evsel,
1089 const char *fmt, const char *filter)
Arnaldo Carvalho de Melo64ec84f2015-07-04 12:19:13 -03001090{
1091 char *new_filter;
1092
1093 if (evsel->filter == NULL)
1094 return perf_evsel__set_filter(evsel, filter);
1095
Mathieu Poirierb15d0a42016-09-16 08:44:03 -06001096 if (asprintf(&new_filter, fmt, evsel->filter, filter) > 0) {
Arnaldo Carvalho de Melo64ec84f2015-07-04 12:19:13 -03001097 free(evsel->filter);
1098 evsel->filter = new_filter;
1099 return 0;
1100 }
1101
1102 return -1;
1103}
1104
Mathieu Poirier3541c032016-09-16 08:44:04 -06001105int perf_evsel__append_tp_filter(struct perf_evsel *evsel, const char *filter)
1106{
1107 return perf_evsel__append_filter(evsel, "(%s) && (%s)", filter);
1108}
1109
Mathieu Poirier1e857482016-09-16 08:44:05 -06001110int perf_evsel__append_addr_filter(struct perf_evsel *evsel, const char *filter)
1111{
1112 return perf_evsel__append_filter(evsel, "%s,%s", filter);
1113}
1114
Jiri Olsa5cd95fc2015-12-03 10:06:40 +01001115int perf_evsel__enable(struct perf_evsel *evsel)
Andi Kleene2407be2013-08-02 17:41:10 -07001116{
Jiri Olsa5cd95fc2015-12-03 10:06:40 +01001117 int nthreads = thread_map__nr(evsel->threads);
1118 int ncpus = cpu_map__nr(evsel->cpus);
1119
Andi Kleene2407be2013-08-02 17:41:10 -07001120 return perf_evsel__run_ioctl(evsel, ncpus, nthreads,
1121 PERF_EVENT_IOC_ENABLE,
1122 0);
1123}
1124
Jiri Olsae98a4cb2015-12-03 10:06:41 +01001125int perf_evsel__disable(struct perf_evsel *evsel)
1126{
1127 int nthreads = thread_map__nr(evsel->threads);
1128 int ncpus = cpu_map__nr(evsel->cpus);
1129
1130 return perf_evsel__run_ioctl(evsel, ncpus, nthreads,
1131 PERF_EVENT_IOC_DISABLE,
1132 0);
1133}
1134
Arnaldo Carvalho de Melo70db7532011-01-12 22:39:13 -02001135int perf_evsel__alloc_id(struct perf_evsel *evsel, int ncpus, int nthreads)
1136{
Vineet Gupta8d9cbd82015-01-13 19:13:23 +05301137 if (ncpus == 0 || nthreads == 0)
1138 return 0;
1139
Adrian Hunterbf8e8f42014-07-31 09:00:51 +03001140 if (evsel->system_wide)
1141 nthreads = 1;
1142
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03001143 evsel->sample_id = xyarray__new(ncpus, nthreads, sizeof(struct perf_sample_id));
1144 if (evsel->sample_id == NULL)
1145 return -ENOMEM;
1146
1147 evsel->id = zalloc(ncpus * nthreads * sizeof(u64));
1148 if (evsel->id == NULL) {
1149 xyarray__delete(evsel->sample_id);
1150 evsel->sample_id = NULL;
1151 return -ENOMEM;
1152 }
1153
1154 return 0;
Arnaldo Carvalho de Melo70db7532011-01-12 22:39:13 -02001155}
1156
Arnaldo Carvalho de Melo88858462014-10-13 13:30:27 -03001157static void perf_evsel__free_fd(struct perf_evsel *evsel)
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -02001158{
1159 xyarray__delete(evsel->fd);
1160 evsel->fd = NULL;
1161}
1162
Arnaldo Carvalho de Melo88858462014-10-13 13:30:27 -03001163static void perf_evsel__free_id(struct perf_evsel *evsel)
Arnaldo Carvalho de Melo70db7532011-01-12 22:39:13 -02001164{
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03001165 xyarray__delete(evsel->sample_id);
1166 evsel->sample_id = NULL;
Arnaldo Carvalho de Melo04662522013-12-26 17:41:15 -03001167 zfree(&evsel->id);
Arnaldo Carvalho de Melo70db7532011-01-12 22:39:13 -02001168}
1169
Jiri Olsa930a2e22015-07-29 05:42:10 -04001170static void perf_evsel__free_config_terms(struct perf_evsel *evsel)
1171{
1172 struct perf_evsel_config_term *term, *h;
1173
1174 list_for_each_entry_safe(term, h, &evsel->config_terms, list) {
1175 list_del(&term->list);
1176 free(term);
1177 }
1178}
1179
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -02001180void perf_evsel__close_fd(struct perf_evsel *evsel, int ncpus, int nthreads)
1181{
1182 int cpu, thread;
1183
Adrian Hunterbf8e8f42014-07-31 09:00:51 +03001184 if (evsel->system_wide)
1185 nthreads = 1;
1186
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -02001187 for (cpu = 0; cpu < ncpus; cpu++)
1188 for (thread = 0; thread < nthreads; ++thread) {
1189 close(FD(evsel, cpu, thread));
1190 FD(evsel, cpu, thread) = -1;
1191 }
1192}
1193
Arnaldo Carvalho de Meloef1d1af2011-01-18 21:41:45 -02001194void perf_evsel__exit(struct perf_evsel *evsel)
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -02001195{
1196 assert(list_empty(&evsel->node));
Arnaldo Carvalho de Melod49e4692015-08-27 08:07:40 -04001197 assert(evsel->evlist == NULL);
Namhyung Kim736b05a2013-03-15 14:48:49 +09001198 perf_evsel__free_fd(evsel);
1199 perf_evsel__free_id(evsel);
Jiri Olsa930a2e22015-07-29 05:42:10 -04001200 perf_evsel__free_config_terms(evsel);
Arnaldo Carvalho de Melo597e48c2014-10-16 13:25:01 -03001201 close_cgroup(evsel->cgrp);
Jiri Olsaf30a79b2015-06-23 00:36:04 +02001202 cpu_map__put(evsel->cpus);
Adrian Hunterfce4d292015-09-08 10:58:55 +03001203 cpu_map__put(evsel->own_cpus);
Jiri Olsa578e91e2015-06-23 00:36:07 +02001204 thread_map__put(evsel->threads);
Arnaldo Carvalho de Melo597e48c2014-10-16 13:25:01 -03001205 zfree(&evsel->group_name);
Arnaldo Carvalho de Melo597e48c2014-10-16 13:25:01 -03001206 zfree(&evsel->name);
Arnaldo Carvalho de Meloce8ccff2014-10-09 15:29:51 -03001207 perf_evsel__object.fini(evsel);
Arnaldo Carvalho de Meloef1d1af2011-01-18 21:41:45 -02001208}
1209
1210void perf_evsel__delete(struct perf_evsel *evsel)
1211{
1212 perf_evsel__exit(evsel);
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -02001213 free(evsel);
1214}
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -02001215
Jiri Olsaa6fa0032015-06-26 11:29:11 +02001216void perf_evsel__compute_deltas(struct perf_evsel *evsel, int cpu, int thread,
Jiri Olsa857a94a2014-11-21 10:31:05 +01001217 struct perf_counts_values *count)
Stephane Eranianc7a79c42013-01-29 12:47:43 +01001218{
1219 struct perf_counts_values tmp;
1220
1221 if (!evsel->prev_raw_counts)
1222 return;
1223
1224 if (cpu == -1) {
1225 tmp = evsel->prev_raw_counts->aggr;
1226 evsel->prev_raw_counts->aggr = *count;
1227 } else {
Jiri Olsaa6fa0032015-06-26 11:29:11 +02001228 tmp = *perf_counts(evsel->prev_raw_counts, cpu, thread);
1229 *perf_counts(evsel->prev_raw_counts, cpu, thread) = *count;
Stephane Eranianc7a79c42013-01-29 12:47:43 +01001230 }
1231
1232 count->val = count->val - tmp.val;
1233 count->ena = count->ena - tmp.ena;
1234 count->run = count->run - tmp.run;
1235}
1236
Jiri Olsa13112bb2014-11-21 10:31:06 +01001237void perf_counts_values__scale(struct perf_counts_values *count,
1238 bool scale, s8 *pscaled)
1239{
1240 s8 scaled = 0;
1241
1242 if (scale) {
1243 if (count->run == 0) {
1244 scaled = -1;
1245 count->val = 0;
1246 } else if (count->run < count->ena) {
1247 scaled = 1;
1248 count->val = (u64)((double) count->val * count->ena / count->run + 0.5);
1249 }
1250 } else
1251 count->ena = count->run = 0;
1252
1253 if (pscaled)
1254 *pscaled = scaled;
1255}
1256
Jiri Olsaf99f47192015-06-26 11:29:18 +02001257int perf_evsel__read(struct perf_evsel *evsel, int cpu, int thread,
1258 struct perf_counts_values *count)
1259{
1260 memset(count, 0, sizeof(*count));
1261
1262 if (FD(evsel, cpu, thread) < 0)
1263 return -EINVAL;
1264
Stephane Eraniandb49a712017-04-12 11:23:01 -07001265 if (readn(FD(evsel, cpu, thread), count, sizeof(*count)) <= 0)
Jiri Olsaf99f47192015-06-26 11:29:18 +02001266 return -errno;
1267
1268 return 0;
1269}
1270
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -02001271int __perf_evsel__read_on_cpu(struct perf_evsel *evsel,
1272 int cpu, int thread, bool scale)
1273{
1274 struct perf_counts_values count;
1275 size_t nv = scale ? 3 : 1;
1276
1277 if (FD(evsel, cpu, thread) < 0)
1278 return -EINVAL;
1279
Jiri Olsaa6fa0032015-06-26 11:29:11 +02001280 if (evsel->counts == NULL && perf_evsel__alloc_counts(evsel, cpu + 1, thread + 1) < 0)
Arnaldo Carvalho de Melo4eed11d2011-01-04 00:13:17 -02001281 return -ENOMEM;
1282
Stephane Eraniandb49a712017-04-12 11:23:01 -07001283 if (readn(FD(evsel, cpu, thread), &count, nv * sizeof(u64)) <= 0)
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -02001284 return -errno;
1285
Jiri Olsaa6fa0032015-06-26 11:29:11 +02001286 perf_evsel__compute_deltas(evsel, cpu, thread, &count);
Jiri Olsa13112bb2014-11-21 10:31:06 +01001287 perf_counts_values__scale(&count, scale, NULL);
Jiri Olsaa6fa0032015-06-26 11:29:11 +02001288 *perf_counts(evsel->counts, cpu, thread) = count;
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -02001289 return 0;
1290}
1291
Jiri Olsa6a4bb042012-08-08 12:22:36 +02001292static int get_group_fd(struct perf_evsel *evsel, int cpu, int thread)
1293{
1294 struct perf_evsel *leader = evsel->leader;
1295 int fd;
1296
Namhyung Kim823254e2012-11-29 15:38:30 +09001297 if (perf_evsel__is_group_leader(evsel))
Jiri Olsa6a4bb042012-08-08 12:22:36 +02001298 return -1;
1299
1300 /*
1301 * Leader must be already processed/open,
1302 * if not it's a bug.
1303 */
1304 BUG_ON(!leader->fd);
1305
1306 fd = FD(leader, cpu, thread);
1307 BUG_ON(fd == -1);
1308
1309 return fd;
1310}
1311
Peter Zijlstra2c5e8c52015-04-07 11:09:54 +02001312struct bit_names {
1313 int bit;
1314 const char *name;
1315};
Adrian Huntere3e1a542013-08-14 15:48:24 +03001316
Peter Zijlstra2c5e8c52015-04-07 11:09:54 +02001317static void __p_bits(char *buf, size_t size, u64 value, struct bit_names *bits)
Adrian Huntere3e1a542013-08-14 15:48:24 +03001318{
Peter Zijlstra2c5e8c52015-04-07 11:09:54 +02001319 bool first_bit = true;
1320 int i = 0;
Adrian Huntere3e1a542013-08-14 15:48:24 +03001321
Peter Zijlstra2c5e8c52015-04-07 11:09:54 +02001322 do {
1323 if (value & bits[i].bit) {
1324 buf += scnprintf(buf, size, "%s%s", first_bit ? "" : "|", bits[i].name);
1325 first_bit = false;
1326 }
1327 } while (bits[++i].name != NULL);
1328}
Adrian Huntere3e1a542013-08-14 15:48:24 +03001329
Peter Zijlstra2c5e8c52015-04-07 11:09:54 +02001330static void __p_sample_type(char *buf, size_t size, u64 value)
1331{
1332#define bit_name(n) { PERF_SAMPLE_##n, #n }
1333 struct bit_names bits[] = {
1334 bit_name(IP), bit_name(TID), bit_name(TIME), bit_name(ADDR),
1335 bit_name(READ), bit_name(CALLCHAIN), bit_name(ID), bit_name(CPU),
1336 bit_name(PERIOD), bit_name(STREAM_ID), bit_name(RAW),
1337 bit_name(BRANCH_STACK), bit_name(REGS_USER), bit_name(STACK_USER),
Jiri Olsa84422592015-10-05 20:06:02 +02001338 bit_name(IDENTIFIER), bit_name(REGS_INTR), bit_name(DATA_SRC),
Jiri Olsadcdd1842015-11-25 16:36:55 +01001339 bit_name(WEIGHT),
Peter Zijlstra2c5e8c52015-04-07 11:09:54 +02001340 { .name = NULL, }
1341 };
1342#undef bit_name
1343 __p_bits(buf, size, value, bits);
1344}
Adrian Huntere3e1a542013-08-14 15:48:24 +03001345
Arnaldo Carvalho de Meloa213b922016-04-25 16:45:29 -03001346static void __p_branch_sample_type(char *buf, size_t size, u64 value)
1347{
1348#define bit_name(n) { PERF_SAMPLE_BRANCH_##n, #n }
1349 struct bit_names bits[] = {
1350 bit_name(USER), bit_name(KERNEL), bit_name(HV), bit_name(ANY),
1351 bit_name(ANY_CALL), bit_name(ANY_RETURN), bit_name(IND_CALL),
1352 bit_name(ABORT_TX), bit_name(IN_TX), bit_name(NO_TX),
1353 bit_name(COND), bit_name(CALL_STACK), bit_name(IND_JUMP),
1354 bit_name(CALL), bit_name(NO_FLAGS), bit_name(NO_CYCLES),
1355 { .name = NULL, }
1356 };
1357#undef bit_name
1358 __p_bits(buf, size, value, bits);
1359}
1360
Peter Zijlstra2c5e8c52015-04-07 11:09:54 +02001361static void __p_read_format(char *buf, size_t size, u64 value)
1362{
1363#define bit_name(n) { PERF_FORMAT_##n, #n }
1364 struct bit_names bits[] = {
1365 bit_name(TOTAL_TIME_ENABLED), bit_name(TOTAL_TIME_RUNNING),
1366 bit_name(ID), bit_name(GROUP),
1367 { .name = NULL, }
1368 };
1369#undef bit_name
1370 __p_bits(buf, size, value, bits);
1371}
Peter Zijlstra814c8c32015-03-31 00:19:31 +02001372
Peter Zijlstra2c5e8c52015-04-07 11:09:54 +02001373#define BUF_SIZE 1024
Adrian Huntere3e1a542013-08-14 15:48:24 +03001374
Adrian Hunter7310aed2015-06-11 15:51:04 +03001375#define p_hex(val) snprintf(buf, BUF_SIZE, "%#"PRIx64, (uint64_t)(val))
Peter Zijlstra2c5e8c52015-04-07 11:09:54 +02001376#define p_unsigned(val) snprintf(buf, BUF_SIZE, "%"PRIu64, (uint64_t)(val))
1377#define p_signed(val) snprintf(buf, BUF_SIZE, "%"PRId64, (int64_t)(val))
1378#define p_sample_type(val) __p_sample_type(buf, BUF_SIZE, val)
Arnaldo Carvalho de Meloa213b922016-04-25 16:45:29 -03001379#define p_branch_sample_type(val) __p_branch_sample_type(buf, BUF_SIZE, val)
Peter Zijlstra2c5e8c52015-04-07 11:09:54 +02001380#define p_read_format(val) __p_read_format(buf, BUF_SIZE, val)
Adrian Huntere3e1a542013-08-14 15:48:24 +03001381
Peter Zijlstra2c5e8c52015-04-07 11:09:54 +02001382#define PRINT_ATTRn(_n, _f, _p) \
1383do { \
1384 if (attr->_f) { \
1385 _p(attr->_f); \
1386 ret += attr__fprintf(fp, _n, buf, priv);\
1387 } \
1388} while (0)
1389
1390#define PRINT_ATTRf(_f, _p) PRINT_ATTRn(#_f, _f, _p)
1391
1392int perf_event_attr__fprintf(FILE *fp, struct perf_event_attr *attr,
1393 attr__fprintf_f attr__fprintf, void *priv)
1394{
1395 char buf[BUF_SIZE];
1396 int ret = 0;
1397
1398 PRINT_ATTRf(type, p_unsigned);
1399 PRINT_ATTRf(size, p_unsigned);
1400 PRINT_ATTRf(config, p_hex);
1401 PRINT_ATTRn("{ sample_period, sample_freq }", sample_period, p_unsigned);
1402 PRINT_ATTRf(sample_type, p_sample_type);
1403 PRINT_ATTRf(read_format, p_read_format);
1404
1405 PRINT_ATTRf(disabled, p_unsigned);
1406 PRINT_ATTRf(inherit, p_unsigned);
1407 PRINT_ATTRf(pinned, p_unsigned);
1408 PRINT_ATTRf(exclusive, p_unsigned);
1409 PRINT_ATTRf(exclude_user, p_unsigned);
1410 PRINT_ATTRf(exclude_kernel, p_unsigned);
1411 PRINT_ATTRf(exclude_hv, p_unsigned);
1412 PRINT_ATTRf(exclude_idle, p_unsigned);
1413 PRINT_ATTRf(mmap, p_unsigned);
1414 PRINT_ATTRf(comm, p_unsigned);
1415 PRINT_ATTRf(freq, p_unsigned);
1416 PRINT_ATTRf(inherit_stat, p_unsigned);
1417 PRINT_ATTRf(enable_on_exec, p_unsigned);
1418 PRINT_ATTRf(task, p_unsigned);
1419 PRINT_ATTRf(watermark, p_unsigned);
1420 PRINT_ATTRf(precise_ip, p_unsigned);
1421 PRINT_ATTRf(mmap_data, p_unsigned);
1422 PRINT_ATTRf(sample_id_all, p_unsigned);
1423 PRINT_ATTRf(exclude_host, p_unsigned);
1424 PRINT_ATTRf(exclude_guest, p_unsigned);
1425 PRINT_ATTRf(exclude_callchain_kernel, p_unsigned);
1426 PRINT_ATTRf(exclude_callchain_user, p_unsigned);
1427 PRINT_ATTRf(mmap2, p_unsigned);
1428 PRINT_ATTRf(comm_exec, p_unsigned);
1429 PRINT_ATTRf(use_clockid, p_unsigned);
Adrian Hunter02860392015-07-21 12:44:03 +03001430 PRINT_ATTRf(context_switch, p_unsigned);
Arnaldo Carvalho de Melo0a241ef2016-05-09 18:08:33 -03001431 PRINT_ATTRf(write_backward, p_unsigned);
Peter Zijlstra2c5e8c52015-04-07 11:09:54 +02001432
1433 PRINT_ATTRn("{ wakeup_events, wakeup_watermark }", wakeup_events, p_unsigned);
1434 PRINT_ATTRf(bp_type, p_unsigned);
1435 PRINT_ATTRn("{ bp_addr, config1 }", bp_addr, p_hex);
1436 PRINT_ATTRn("{ bp_len, config2 }", bp_len, p_hex);
Arnaldo Carvalho de Meloa213b922016-04-25 16:45:29 -03001437 PRINT_ATTRf(branch_sample_type, p_branch_sample_type);
Peter Zijlstra2c5e8c52015-04-07 11:09:54 +02001438 PRINT_ATTRf(sample_regs_user, p_hex);
1439 PRINT_ATTRf(sample_stack_user, p_unsigned);
1440 PRINT_ATTRf(clockid, p_signed);
1441 PRINT_ATTRf(sample_regs_intr, p_hex);
Adrian Hunter70d73de2015-04-09 18:54:06 +03001442 PRINT_ATTRf(aux_watermark, p_unsigned);
Arnaldo Carvalho de Melo792d48b2016-04-28 19:03:42 -03001443 PRINT_ATTRf(sample_max_stack, p_unsigned);
Adrian Huntere3e1a542013-08-14 15:48:24 +03001444
1445 return ret;
1446}
1447
Peter Zijlstra2c5e8c52015-04-07 11:09:54 +02001448static int __open_attr__fprintf(FILE *fp, const char *name, const char *val,
Arnaldo Carvalho de Melo03536312017-06-16 12:18:27 -03001449 void *priv __maybe_unused)
Peter Zijlstra2c5e8c52015-04-07 11:09:54 +02001450{
1451 return fprintf(fp, " %-32s %s\n", name, val);
1452}
1453
Jiri Olsaa359c172016-12-13 08:46:22 +01001454static bool ignore_missing_thread(struct perf_evsel *evsel,
1455 struct thread_map *threads,
1456 int thread, int err)
1457{
1458 if (!evsel->ignore_missing_thread)
1459 return false;
1460
1461 /* The system wide setup does not work with threads. */
1462 if (evsel->system_wide)
1463 return false;
1464
1465 /* The -ESRCH is perf event syscall errno for pid's not found. */
1466 if (err != -ESRCH)
1467 return false;
1468
1469 /* If there's only one thread, let it fail. */
1470 if (threads->nr == 1)
1471 return false;
1472
1473 if (thread_map__remove(threads, thread))
1474 return false;
1475
1476 pr_warning("WARNING: Ignored open failure for pid %d\n",
1477 thread_map__pid(threads, thread));
1478 return true;
1479}
1480
Arnaldo Carvalho de Meloc24ae6d2017-02-14 10:59:04 -03001481int perf_evsel__open(struct perf_evsel *evsel, struct cpu_map *cpus,
1482 struct thread_map *threads)
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -02001483{
Adrian Hunterbf8e8f42014-07-31 09:00:51 +03001484 int cpu, thread, nthreads;
Yann Droneaud57480d22014-06-30 22:28:47 +02001485 unsigned long flags = PERF_FLAG_FD_CLOEXEC;
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -02001486 int pid = -1, err;
Andi Kleenbec19672013-08-04 19:41:26 -07001487 enum { NO_CHANGE, SET_TO_MAX, INCREASED_MAX } set_rlimit = NO_CHANGE;
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -02001488
Arnaldo Carvalho de Melo32a951b2016-07-14 08:34:33 +00001489 if (perf_missing_features.write_backward && evsel->attr.write_backward)
1490 return -EINVAL;
1491
Arnaldo Carvalho de Meloc24ae6d2017-02-14 10:59:04 -03001492 if (cpus == NULL) {
1493 static struct cpu_map *empty_cpu_map;
1494
1495 if (empty_cpu_map == NULL) {
1496 empty_cpu_map = cpu_map__dummy_new();
1497 if (empty_cpu_map == NULL)
1498 return -ENOMEM;
1499 }
1500
1501 cpus = empty_cpu_map;
1502 }
1503
1504 if (threads == NULL) {
1505 static struct thread_map *empty_thread_map;
1506
1507 if (empty_thread_map == NULL) {
1508 empty_thread_map = thread_map__new_by_tid(-1);
1509 if (empty_thread_map == NULL)
1510 return -ENOMEM;
1511 }
1512
1513 threads = empty_thread_map;
1514 }
1515
Adrian Hunterbf8e8f42014-07-31 09:00:51 +03001516 if (evsel->system_wide)
1517 nthreads = 1;
1518 else
1519 nthreads = threads->nr;
1520
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -02001521 if (evsel->fd == NULL &&
Adrian Hunterbf8e8f42014-07-31 09:00:51 +03001522 perf_evsel__alloc_fd(evsel, cpus->nr, nthreads) < 0)
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -02001523 return -ENOMEM;
Arnaldo Carvalho de Melo4eed11d2011-01-04 00:13:17 -02001524
Stephane Eranian023695d2011-02-14 11:20:01 +02001525 if (evsel->cgrp) {
Yann Droneaud57480d22014-06-30 22:28:47 +02001526 flags |= PERF_FLAG_PID_CGROUP;
Stephane Eranian023695d2011-02-14 11:20:01 +02001527 pid = evsel->cgrp->fd;
1528 }
1529
Arnaldo Carvalho de Melo594ac612012-12-13 13:13:07 -03001530fallback_missing_features:
Peter Zijlstra814c8c32015-03-31 00:19:31 +02001531 if (perf_missing_features.clockid_wrong)
1532 evsel->attr.clockid = CLOCK_MONOTONIC; /* should always work */
1533 if (perf_missing_features.clockid) {
1534 evsel->attr.use_clockid = 0;
1535 evsel->attr.clockid = 0;
1536 }
Yann Droneaud57480d22014-06-30 22:28:47 +02001537 if (perf_missing_features.cloexec)
1538 flags &= ~(unsigned long)PERF_FLAG_FD_CLOEXEC;
Stephane Eranian5c5e8542013-08-21 12:10:25 +02001539 if (perf_missing_features.mmap2)
1540 evsel->attr.mmap2 = 0;
Arnaldo Carvalho de Melo594ac612012-12-13 13:13:07 -03001541 if (perf_missing_features.exclude_guest)
1542 evsel->attr.exclude_guest = evsel->attr.exclude_host = 0;
Andi Kleenbd0f8892015-12-11 16:12:24 -08001543 if (perf_missing_features.lbr_flags)
1544 evsel->attr.branch_sample_type &= ~(PERF_SAMPLE_BRANCH_NO_FLAGS |
1545 PERF_SAMPLE_BRANCH_NO_CYCLES);
Arnaldo Carvalho de Melo594ac612012-12-13 13:13:07 -03001546retry_sample_id:
1547 if (perf_missing_features.sample_id_all)
1548 evsel->attr.sample_id_all = 0;
1549
Peter Zijlstra2c5e8c52015-04-07 11:09:54 +02001550 if (verbose >= 2) {
1551 fprintf(stderr, "%.60s\n", graph_dotted_line);
1552 fprintf(stderr, "perf_event_attr:\n");
1553 perf_event_attr__fprintf(stderr, &evsel->attr, __open_attr__fprintf, NULL);
1554 fprintf(stderr, "%.60s\n", graph_dotted_line);
1555 }
Adrian Huntere3e1a542013-08-14 15:48:24 +03001556
Arnaldo Carvalho de Melo86bd5e82011-01-03 23:09:46 -02001557 for (cpu = 0; cpu < cpus->nr; cpu++) {
Arnaldo Carvalho de Melo9d04f172011-01-12 00:08:18 -02001558
Adrian Hunterbf8e8f42014-07-31 09:00:51 +03001559 for (thread = 0; thread < nthreads; thread++) {
Jiri Olsa83c2e4f32016-12-12 11:35:40 +01001560 int fd, group_fd;
Stephane Eranian023695d2011-02-14 11:20:01 +02001561
Adrian Hunterbf8e8f42014-07-31 09:00:51 +03001562 if (!evsel->cgrp && !evsel->system_wide)
Jiri Olsae13798c2015-06-23 00:36:02 +02001563 pid = thread_map__pid(threads, thread);
Stephane Eranian023695d2011-02-14 11:20:01 +02001564
Jiri Olsa6a4bb042012-08-08 12:22:36 +02001565 group_fd = get_group_fd(evsel, cpu, thread);
Andi Kleenbec19672013-08-04 19:41:26 -07001566retry_open:
Jiri Olsa7b4b82bc2016-11-21 22:33:26 +01001567 pr_debug2("sys_perf_event_open: pid %d cpu %d group_fd %d flags %#lx",
Adrian Huntere3e1a542013-08-14 15:48:24 +03001568 pid, cpus->map[cpu], group_fd, flags);
1569
Jiri Olsa83c2e4f32016-12-12 11:35:40 +01001570 fd = sys_perf_event_open(&evsel->attr, pid, cpus->map[cpu],
1571 group_fd, flags);
1572
1573 FD(evsel, cpu, thread) = fd;
1574
1575 if (fd < 0) {
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -02001576 err = -errno;
Jiri Olsaa359c172016-12-13 08:46:22 +01001577
1578 if (ignore_missing_thread(evsel, threads, thread, err)) {
1579 /*
1580 * We just removed 1 thread, so take a step
1581 * back on thread index and lower the upper
1582 * nthreads limit.
1583 */
1584 nthreads--;
1585 thread--;
1586
1587 /* ... and pretend like nothing have happened. */
1588 err = 0;
1589 continue;
1590 }
1591
Jiri Olsa7b4b82bc2016-11-21 22:33:26 +01001592 pr_debug2("\nsys_perf_event_open failed, error %d\n",
Adrian Hunterf852fd62013-11-01 15:51:29 +02001593 err);
Arnaldo Carvalho de Melo594ac612012-12-13 13:13:07 -03001594 goto try_fallback;
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -02001595 }
Wang Nan1f45b1d2015-10-14 12:41:18 +00001596
Jiri Olsa83c2e4f32016-12-12 11:35:40 +01001597 pr_debug2(" = %d\n", fd);
Jiri Olsa7b4b82bc2016-11-21 22:33:26 +01001598
Wang Nan1f45b1d2015-10-14 12:41:18 +00001599 if (evsel->bpf_fd >= 0) {
Jiri Olsa83c2e4f32016-12-12 11:35:40 +01001600 int evt_fd = fd;
Wang Nan1f45b1d2015-10-14 12:41:18 +00001601 int bpf_fd = evsel->bpf_fd;
1602
1603 err = ioctl(evt_fd,
1604 PERF_EVENT_IOC_SET_BPF,
1605 bpf_fd);
1606 if (err && errno != EEXIST) {
1607 pr_err("failed to attach bpf fd %d: %s\n",
1608 bpf_fd, strerror(errno));
1609 err = -EINVAL;
1610 goto out_close;
1611 }
1612 }
1613
Andi Kleenbec19672013-08-04 19:41:26 -07001614 set_rlimit = NO_CHANGE;
Peter Zijlstra814c8c32015-03-31 00:19:31 +02001615
1616 /*
1617 * If we succeeded but had to kill clockid, fail and
1618 * have perf_evsel__open_strerror() print us a nice
1619 * error.
1620 */
1621 if (perf_missing_features.clockid ||
1622 perf_missing_features.clockid_wrong) {
1623 err = -EINVAL;
1624 goto out_close;
1625 }
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -02001626 }
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -02001627 }
1628
1629 return 0;
1630
Arnaldo Carvalho de Melo594ac612012-12-13 13:13:07 -03001631try_fallback:
Andi Kleenbec19672013-08-04 19:41:26 -07001632 /*
1633 * perf stat needs between 5 and 22 fds per CPU. When we run out
1634 * of them try to increase the limits.
1635 */
1636 if (err == -EMFILE && set_rlimit < INCREASED_MAX) {
1637 struct rlimit l;
1638 int old_errno = errno;
1639
1640 if (getrlimit(RLIMIT_NOFILE, &l) == 0) {
1641 if (set_rlimit == NO_CHANGE)
1642 l.rlim_cur = l.rlim_max;
1643 else {
1644 l.rlim_cur = l.rlim_max + 1000;
1645 l.rlim_max = l.rlim_cur;
1646 }
1647 if (setrlimit(RLIMIT_NOFILE, &l) == 0) {
1648 set_rlimit++;
1649 errno = old_errno;
1650 goto retry_open;
1651 }
1652 }
1653 errno = old_errno;
1654 }
1655
Arnaldo Carvalho de Melo594ac612012-12-13 13:13:07 -03001656 if (err != -EINVAL || cpu > 0 || thread > 0)
1657 goto out_close;
1658
Peter Zijlstra814c8c32015-03-31 00:19:31 +02001659 /*
1660 * Must probe features in the order they were added to the
1661 * perf_event_attr interface.
1662 */
Arnaldo Carvalho de Melo7da36e92016-06-20 10:47:18 +00001663 if (!perf_missing_features.write_backward && evsel->attr.write_backward) {
1664 perf_missing_features.write_backward = true;
Arnaldo Carvalho de Melo32a951b2016-07-14 08:34:33 +00001665 goto out_close;
Arnaldo Carvalho de Melo7da36e92016-06-20 10:47:18 +00001666 } else if (!perf_missing_features.clockid_wrong && evsel->attr.use_clockid) {
Peter Zijlstra814c8c32015-03-31 00:19:31 +02001667 perf_missing_features.clockid_wrong = true;
1668 goto fallback_missing_features;
1669 } else if (!perf_missing_features.clockid && evsel->attr.use_clockid) {
1670 perf_missing_features.clockid = true;
1671 goto fallback_missing_features;
1672 } else if (!perf_missing_features.cloexec && (flags & PERF_FLAG_FD_CLOEXEC)) {
Yann Droneaud57480d22014-06-30 22:28:47 +02001673 perf_missing_features.cloexec = true;
1674 goto fallback_missing_features;
1675 } else if (!perf_missing_features.mmap2 && evsel->attr.mmap2) {
Stephane Eranian5c5e8542013-08-21 12:10:25 +02001676 perf_missing_features.mmap2 = true;
1677 goto fallback_missing_features;
1678 } else if (!perf_missing_features.exclude_guest &&
1679 (evsel->attr.exclude_guest || evsel->attr.exclude_host)) {
Arnaldo Carvalho de Melo594ac612012-12-13 13:13:07 -03001680 perf_missing_features.exclude_guest = true;
1681 goto fallback_missing_features;
1682 } else if (!perf_missing_features.sample_id_all) {
1683 perf_missing_features.sample_id_all = true;
1684 goto retry_sample_id;
Andi Kleenbd0f8892015-12-11 16:12:24 -08001685 } else if (!perf_missing_features.lbr_flags &&
1686 (evsel->attr.branch_sample_type &
1687 (PERF_SAMPLE_BRANCH_NO_CYCLES |
1688 PERF_SAMPLE_BRANCH_NO_FLAGS))) {
1689 perf_missing_features.lbr_flags = true;
1690 goto fallback_missing_features;
Arnaldo Carvalho de Melo594ac612012-12-13 13:13:07 -03001691 }
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -02001692out_close:
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -02001693 do {
1694 while (--thread >= 0) {
1695 close(FD(evsel, cpu, thread));
1696 FD(evsel, cpu, thread) = -1;
1697 }
Adrian Hunterbf8e8f42014-07-31 09:00:51 +03001698 thread = nthreads;
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -02001699 } while (--cpu >= 0);
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -02001700 return err;
1701}
1702
1703void perf_evsel__close(struct perf_evsel *evsel, int ncpus, int nthreads)
1704{
1705 if (evsel->fd == NULL)
1706 return;
1707
1708 perf_evsel__close_fd(evsel, ncpus, nthreads);
1709 perf_evsel__free_fd(evsel);
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -02001710}
1711
Arnaldo Carvalho de Melof08199d2011-01-11 23:42:19 -02001712int perf_evsel__open_per_cpu(struct perf_evsel *evsel,
Jiri Olsa6a4bb042012-08-08 12:22:36 +02001713 struct cpu_map *cpus)
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -02001714{
Arnaldo Carvalho de Meloc24ae6d2017-02-14 10:59:04 -03001715 return perf_evsel__open(evsel, cpus, NULL);
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -02001716}
1717
Arnaldo Carvalho de Melof08199d2011-01-11 23:42:19 -02001718int perf_evsel__open_per_thread(struct perf_evsel *evsel,
Jiri Olsa6a4bb042012-08-08 12:22:36 +02001719 struct thread_map *threads)
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -02001720{
Arnaldo Carvalho de Meloc24ae6d2017-02-14 10:59:04 -03001721 return perf_evsel__open(evsel, NULL, threads);
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -02001722}
Arnaldo Carvalho de Melo70082dd2011-01-12 17:03:24 -02001723
Arnaldo Carvalho de Melo0807d2d2012-09-26 12:48:18 -03001724static int perf_evsel__parse_id_sample(const struct perf_evsel *evsel,
1725 const union perf_event *event,
1726 struct perf_sample *sample)
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001727{
Arnaldo Carvalho de Melo0807d2d2012-09-26 12:48:18 -03001728 u64 type = evsel->attr.sample_type;
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001729 const u64 *array = event->sample.array;
Arnaldo Carvalho de Melo0807d2d2012-09-26 12:48:18 -03001730 bool swapped = evsel->needs_swap;
Jiri Olsa37073f92012-05-30 14:23:44 +02001731 union u64_swap u;
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001732
1733 array += ((event->header.size -
1734 sizeof(event->header)) / sizeof(u64)) - 1;
1735
Adrian Hunter75562572013-08-27 11:23:09 +03001736 if (type & PERF_SAMPLE_IDENTIFIER) {
1737 sample->id = *array;
1738 array--;
1739 }
1740
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001741 if (type & PERF_SAMPLE_CPU) {
Jiri Olsa37073f92012-05-30 14:23:44 +02001742 u.val64 = *array;
1743 if (swapped) {
1744 /* undo swap of u64, then swap on individual u32s */
1745 u.val64 = bswap_64(u.val64);
1746 u.val32[0] = bswap_32(u.val32[0]);
1747 }
1748
1749 sample->cpu = u.val32[0];
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001750 array--;
1751 }
1752
1753 if (type & PERF_SAMPLE_STREAM_ID) {
1754 sample->stream_id = *array;
1755 array--;
1756 }
1757
1758 if (type & PERF_SAMPLE_ID) {
1759 sample->id = *array;
1760 array--;
1761 }
1762
1763 if (type & PERF_SAMPLE_TIME) {
1764 sample->time = *array;
1765 array--;
1766 }
1767
1768 if (type & PERF_SAMPLE_TID) {
Jiri Olsa37073f92012-05-30 14:23:44 +02001769 u.val64 = *array;
1770 if (swapped) {
1771 /* undo swap of u64, then swap on individual u32s */
1772 u.val64 = bswap_64(u.val64);
1773 u.val32[0] = bswap_32(u.val32[0]);
1774 u.val32[1] = bswap_32(u.val32[1]);
1775 }
1776
1777 sample->pid = u.val32[0];
1778 sample->tid = u.val32[1];
Adrian Hunterdd44bc62013-10-18 15:29:01 +03001779 array--;
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001780 }
1781
1782 return 0;
1783}
1784
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001785static inline bool overflow(const void *endp, u16 max_size, const void *offset,
1786 u64 size)
Frederic Weisbecker98e1da92011-05-21 20:08:15 +02001787{
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001788 return size > max_size || offset + size > endp;
Frederic Weisbecker98e1da92011-05-21 20:08:15 +02001789}
1790
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001791#define OVERFLOW_CHECK(offset, size, max_size) \
1792 do { \
1793 if (overflow(endp, (max_size), (offset), (size))) \
1794 return -EFAULT; \
1795 } while (0)
1796
1797#define OVERFLOW_CHECK_u64(offset) \
1798 OVERFLOW_CHECK(offset, sizeof(u64), sizeof(u64))
1799
Arnaldo Carvalho de Meloa3f698f2012-08-02 12:23:46 -03001800int perf_evsel__parse_sample(struct perf_evsel *evsel, union perf_event *event,
Arnaldo Carvalho de Melo0807d2d2012-09-26 12:48:18 -03001801 struct perf_sample *data)
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001802{
Arnaldo Carvalho de Meloa3f698f2012-08-02 12:23:46 -03001803 u64 type = evsel->attr.sample_type;
Arnaldo Carvalho de Melo0807d2d2012-09-26 12:48:18 -03001804 bool swapped = evsel->needs_swap;
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001805 const u64 *array;
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001806 u16 max_size = event->header.size;
1807 const void *endp = (void *)event + max_size;
1808 u64 sz;
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001809
David Ahern936be502011-09-06 09:12:26 -06001810 /*
1811 * used for cross-endian analysis. See git commit 65014ab3
1812 * for why this goofiness is needed.
1813 */
Jiri Olsa6a11f922012-05-16 08:59:04 +02001814 union u64_swap u;
David Ahern936be502011-09-06 09:12:26 -06001815
Robert Richterf3bda2c2011-12-15 17:32:39 +01001816 memset(data, 0, sizeof(*data));
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001817 data->cpu = data->pid = data->tid = -1;
1818 data->stream_id = data->id = data->time = -1ULL;
Jiri Olsabc529082014-02-03 12:44:41 +01001819 data->period = evsel->attr.sample_period;
Arnaldo Carvalho de Melo473398a2016-03-22 18:23:43 -03001820 data->cpumode = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001821
1822 if (event->header.type != PERF_RECORD_SAMPLE) {
Arnaldo Carvalho de Meloa3f698f2012-08-02 12:23:46 -03001823 if (!evsel->attr.sample_id_all)
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001824 return 0;
Arnaldo Carvalho de Melo0807d2d2012-09-26 12:48:18 -03001825 return perf_evsel__parse_id_sample(evsel, event, data);
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001826 }
1827
1828 array = event->sample.array;
1829
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001830 /*
1831 * The evsel's sample_size is based on PERF_SAMPLE_MASK which includes
1832 * up to PERF_SAMPLE_PERIOD. After that overflow() must be used to
1833 * check the format does not go past the end of the event.
1834 */
Arnaldo Carvalho de Meloa3f698f2012-08-02 12:23:46 -03001835 if (evsel->sample_size + sizeof(event->header) > event->header.size)
Frederic Weisbeckera2854122011-05-21 19:33:04 +02001836 return -EFAULT;
1837
Adrian Hunter75562572013-08-27 11:23:09 +03001838 data->id = -1ULL;
1839 if (type & PERF_SAMPLE_IDENTIFIER) {
1840 data->id = *array;
1841 array++;
1842 }
1843
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001844 if (type & PERF_SAMPLE_IP) {
Adrian Hunteref893252013-08-27 11:23:06 +03001845 data->ip = *array;
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001846 array++;
1847 }
1848
1849 if (type & PERF_SAMPLE_TID) {
David Ahern936be502011-09-06 09:12:26 -06001850 u.val64 = *array;
1851 if (swapped) {
1852 /* undo swap of u64, then swap on individual u32s */
1853 u.val64 = bswap_64(u.val64);
1854 u.val32[0] = bswap_32(u.val32[0]);
1855 u.val32[1] = bswap_32(u.val32[1]);
1856 }
1857
1858 data->pid = u.val32[0];
1859 data->tid = u.val32[1];
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001860 array++;
1861 }
1862
1863 if (type & PERF_SAMPLE_TIME) {
1864 data->time = *array;
1865 array++;
1866 }
1867
David Ahern7cec0922011-05-30 13:08:23 -06001868 data->addr = 0;
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001869 if (type & PERF_SAMPLE_ADDR) {
1870 data->addr = *array;
1871 array++;
1872 }
1873
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001874 if (type & PERF_SAMPLE_ID) {
1875 data->id = *array;
1876 array++;
1877 }
1878
1879 if (type & PERF_SAMPLE_STREAM_ID) {
1880 data->stream_id = *array;
1881 array++;
1882 }
1883
1884 if (type & PERF_SAMPLE_CPU) {
David Ahern936be502011-09-06 09:12:26 -06001885
1886 u.val64 = *array;
1887 if (swapped) {
1888 /* undo swap of u64, then swap on individual u32s */
1889 u.val64 = bswap_64(u.val64);
1890 u.val32[0] = bswap_32(u.val32[0]);
1891 }
1892
1893 data->cpu = u.val32[0];
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001894 array++;
1895 }
1896
1897 if (type & PERF_SAMPLE_PERIOD) {
1898 data->period = *array;
1899 array++;
1900 }
1901
1902 if (type & PERF_SAMPLE_READ) {
Jiri Olsa9ede4732012-10-10 17:38:13 +02001903 u64 read_format = evsel->attr.read_format;
1904
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001905 OVERFLOW_CHECK_u64(array);
Jiri Olsa9ede4732012-10-10 17:38:13 +02001906 if (read_format & PERF_FORMAT_GROUP)
1907 data->read.group.nr = *array;
1908 else
1909 data->read.one.value = *array;
1910
1911 array++;
1912
1913 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) {
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001914 OVERFLOW_CHECK_u64(array);
Jiri Olsa9ede4732012-10-10 17:38:13 +02001915 data->read.time_enabled = *array;
1916 array++;
1917 }
1918
1919 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) {
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001920 OVERFLOW_CHECK_u64(array);
Jiri Olsa9ede4732012-10-10 17:38:13 +02001921 data->read.time_running = *array;
1922 array++;
1923 }
1924
1925 /* PERF_FORMAT_ID is forced for PERF_SAMPLE_READ */
1926 if (read_format & PERF_FORMAT_GROUP) {
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001927 const u64 max_group_nr = UINT64_MAX /
1928 sizeof(struct sample_read_value);
1929
1930 if (data->read.group.nr > max_group_nr)
1931 return -EFAULT;
1932 sz = data->read.group.nr *
1933 sizeof(struct sample_read_value);
1934 OVERFLOW_CHECK(array, sz, max_size);
1935 data->read.group.values =
1936 (struct sample_read_value *)array;
1937 array = (void *)array + sz;
Jiri Olsa9ede4732012-10-10 17:38:13 +02001938 } else {
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001939 OVERFLOW_CHECK_u64(array);
Jiri Olsa9ede4732012-10-10 17:38:13 +02001940 data->read.one.id = *array;
1941 array++;
1942 }
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001943 }
1944
1945 if (type & PERF_SAMPLE_CALLCHAIN) {
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001946 const u64 max_callchain_nr = UINT64_MAX / sizeof(u64);
1947
1948 OVERFLOW_CHECK_u64(array);
1949 data->callchain = (struct ip_callchain *)array++;
1950 if (data->callchain->nr > max_callchain_nr)
Frederic Weisbecker98e1da92011-05-21 20:08:15 +02001951 return -EFAULT;
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001952 sz = data->callchain->nr * sizeof(u64);
1953 OVERFLOW_CHECK(array, sz, max_size);
1954 array = (void *)array + sz;
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001955 }
1956
1957 if (type & PERF_SAMPLE_RAW) {
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001958 OVERFLOW_CHECK_u64(array);
David Ahern936be502011-09-06 09:12:26 -06001959 u.val64 = *array;
1960 if (WARN_ONCE(swapped,
1961 "Endianness of raw data not corrected!\n")) {
1962 /* undo swap of u64, then swap on individual u32s */
1963 u.val64 = bswap_64(u.val64);
1964 u.val32[0] = bswap_32(u.val32[0]);
1965 u.val32[1] = bswap_32(u.val32[1]);
1966 }
David Ahern936be502011-09-06 09:12:26 -06001967 data->raw_size = u.val32[0];
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001968 array = (void *)array + sizeof(u32);
Frederic Weisbecker98e1da92011-05-21 20:08:15 +02001969
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001970 OVERFLOW_CHECK(array, data->raw_size, max_size);
1971 data->raw_data = (void *)array;
1972 array = (void *)array + data->raw_size;
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001973 }
1974
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +01001975 if (type & PERF_SAMPLE_BRANCH_STACK) {
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001976 const u64 max_branch_nr = UINT64_MAX /
1977 sizeof(struct branch_entry);
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +01001978
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001979 OVERFLOW_CHECK_u64(array);
1980 data->branch_stack = (struct branch_stack *)array++;
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +01001981
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001982 if (data->branch_stack->nr > max_branch_nr)
1983 return -EFAULT;
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +01001984 sz = data->branch_stack->nr * sizeof(struct branch_entry);
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001985 OVERFLOW_CHECK(array, sz, max_size);
1986 array = (void *)array + sz;
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +01001987 }
Jiri Olsa0f6a3012012-08-07 15:20:45 +02001988
1989 if (type & PERF_SAMPLE_REGS_USER) {
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001990 OVERFLOW_CHECK_u64(array);
Adrian Hunter5b95a4a32013-08-27 11:23:10 +03001991 data->user_regs.abi = *array;
1992 array++;
Jiri Olsa0f6a3012012-08-07 15:20:45 +02001993
Adrian Hunter5b95a4a32013-08-27 11:23:10 +03001994 if (data->user_regs.abi) {
Jiri Olsa352ea452014-01-07 13:47:25 +01001995 u64 mask = evsel->attr.sample_regs_user;
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001996
Jiri Olsa352ea452014-01-07 13:47:25 +01001997 sz = hweight_long(mask) * sizeof(u64);
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001998 OVERFLOW_CHECK(array, sz, max_size);
Jiri Olsa352ea452014-01-07 13:47:25 +01001999 data->user_regs.mask = mask;
Jiri Olsa0f6a3012012-08-07 15:20:45 +02002000 data->user_regs.regs = (u64 *)array;
Adrian Hunter03b6ea92013-08-27 11:23:04 +03002001 array = (void *)array + sz;
Jiri Olsa0f6a3012012-08-07 15:20:45 +02002002 }
2003 }
2004
2005 if (type & PERF_SAMPLE_STACK_USER) {
Adrian Hunter03b6ea92013-08-27 11:23:04 +03002006 OVERFLOW_CHECK_u64(array);
2007 sz = *array++;
Jiri Olsa0f6a3012012-08-07 15:20:45 +02002008
2009 data->user_stack.offset = ((char *)(array - 1)
2010 - (char *) event);
2011
Adrian Hunter03b6ea92013-08-27 11:23:04 +03002012 if (!sz) {
Jiri Olsa0f6a3012012-08-07 15:20:45 +02002013 data->user_stack.size = 0;
2014 } else {
Adrian Hunter03b6ea92013-08-27 11:23:04 +03002015 OVERFLOW_CHECK(array, sz, max_size);
Jiri Olsa0f6a3012012-08-07 15:20:45 +02002016 data->user_stack.data = (char *)array;
Adrian Hunter03b6ea92013-08-27 11:23:04 +03002017 array = (void *)array + sz;
2018 OVERFLOW_CHECK_u64(array);
Adrian Hunter54bd2692013-07-04 16:20:34 +03002019 data->user_stack.size = *array++;
Jiri Olsaa65cb4b2013-10-02 15:46:39 +02002020 if (WARN_ONCE(data->user_stack.size > sz,
2021 "user stack dump failure\n"))
2022 return -EFAULT;
Jiri Olsa0f6a3012012-08-07 15:20:45 +02002023 }
2024 }
2025
Andi Kleen05484292013-01-24 16:10:29 +01002026 if (type & PERF_SAMPLE_WEIGHT) {
Adrian Hunter03b6ea92013-08-27 11:23:04 +03002027 OVERFLOW_CHECK_u64(array);
Andi Kleen05484292013-01-24 16:10:29 +01002028 data->weight = *array;
2029 array++;
2030 }
2031
Stephane Eranian98a3b322013-01-24 16:10:35 +01002032 data->data_src = PERF_MEM_DATA_SRC_NONE;
2033 if (type & PERF_SAMPLE_DATA_SRC) {
Adrian Hunter03b6ea92013-08-27 11:23:04 +03002034 OVERFLOW_CHECK_u64(array);
Stephane Eranian98a3b322013-01-24 16:10:35 +01002035 data->data_src = *array;
2036 array++;
2037 }
2038
Andi Kleen475eeab2013-09-20 07:40:43 -07002039 data->transaction = 0;
2040 if (type & PERF_SAMPLE_TRANSACTION) {
Adrian Hunter87b95522013-11-01 15:51:36 +02002041 OVERFLOW_CHECK_u64(array);
Andi Kleen475eeab2013-09-20 07:40:43 -07002042 data->transaction = *array;
2043 array++;
2044 }
2045
Stephane Eranian6a21c0b2014-09-24 13:48:39 +02002046 data->intr_regs.abi = PERF_SAMPLE_REGS_ABI_NONE;
2047 if (type & PERF_SAMPLE_REGS_INTR) {
2048 OVERFLOW_CHECK_u64(array);
2049 data->intr_regs.abi = *array;
2050 array++;
2051
2052 if (data->intr_regs.abi != PERF_SAMPLE_REGS_ABI_NONE) {
2053 u64 mask = evsel->attr.sample_regs_intr;
2054
2055 sz = hweight_long(mask) * sizeof(u64);
2056 OVERFLOW_CHECK(array, sz, max_size);
2057 data->intr_regs.mask = mask;
2058 data->intr_regs.regs = (u64 *)array;
2059 array = (void *)array + sz;
2060 }
2061 }
2062
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02002063 return 0;
2064}
Andrew Vagin74eec262011-11-28 12:03:31 +03002065
Adrian Hunterb1cf6f62013-08-27 11:23:12 +03002066size_t perf_event__sample_event_size(const struct perf_sample *sample, u64 type,
Jiri Olsa352ea452014-01-07 13:47:25 +01002067 u64 read_format)
Adrian Hunterb1cf6f62013-08-27 11:23:12 +03002068{
2069 size_t sz, result = sizeof(struct sample_event);
2070
2071 if (type & PERF_SAMPLE_IDENTIFIER)
2072 result += sizeof(u64);
2073
2074 if (type & PERF_SAMPLE_IP)
2075 result += sizeof(u64);
2076
2077 if (type & PERF_SAMPLE_TID)
2078 result += sizeof(u64);
2079
2080 if (type & PERF_SAMPLE_TIME)
2081 result += sizeof(u64);
2082
2083 if (type & PERF_SAMPLE_ADDR)
2084 result += sizeof(u64);
2085
2086 if (type & PERF_SAMPLE_ID)
2087 result += sizeof(u64);
2088
2089 if (type & PERF_SAMPLE_STREAM_ID)
2090 result += sizeof(u64);
2091
2092 if (type & PERF_SAMPLE_CPU)
2093 result += sizeof(u64);
2094
2095 if (type & PERF_SAMPLE_PERIOD)
2096 result += sizeof(u64);
2097
2098 if (type & PERF_SAMPLE_READ) {
2099 result += sizeof(u64);
2100 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
2101 result += sizeof(u64);
2102 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
2103 result += sizeof(u64);
2104 /* PERF_FORMAT_ID is forced for PERF_SAMPLE_READ */
2105 if (read_format & PERF_FORMAT_GROUP) {
2106 sz = sample->read.group.nr *
2107 sizeof(struct sample_read_value);
2108 result += sz;
2109 } else {
2110 result += sizeof(u64);
2111 }
2112 }
2113
2114 if (type & PERF_SAMPLE_CALLCHAIN) {
2115 sz = (sample->callchain->nr + 1) * sizeof(u64);
2116 result += sz;
2117 }
2118
2119 if (type & PERF_SAMPLE_RAW) {
2120 result += sizeof(u32);
2121 result += sample->raw_size;
2122 }
2123
2124 if (type & PERF_SAMPLE_BRANCH_STACK) {
2125 sz = sample->branch_stack->nr * sizeof(struct branch_entry);
2126 sz += sizeof(u64);
2127 result += sz;
2128 }
2129
2130 if (type & PERF_SAMPLE_REGS_USER) {
2131 if (sample->user_regs.abi) {
2132 result += sizeof(u64);
Jiri Olsa352ea452014-01-07 13:47:25 +01002133 sz = hweight_long(sample->user_regs.mask) * sizeof(u64);
Adrian Hunterb1cf6f62013-08-27 11:23:12 +03002134 result += sz;
2135 } else {
2136 result += sizeof(u64);
2137 }
2138 }
2139
2140 if (type & PERF_SAMPLE_STACK_USER) {
2141 sz = sample->user_stack.size;
2142 result += sizeof(u64);
2143 if (sz) {
2144 result += sz;
2145 result += sizeof(u64);
2146 }
2147 }
2148
2149 if (type & PERF_SAMPLE_WEIGHT)
2150 result += sizeof(u64);
2151
2152 if (type & PERF_SAMPLE_DATA_SRC)
2153 result += sizeof(u64);
2154
Adrian Hunter42d88912013-11-01 15:51:38 +02002155 if (type & PERF_SAMPLE_TRANSACTION)
2156 result += sizeof(u64);
2157
Stephane Eranian6a21c0b2014-09-24 13:48:39 +02002158 if (type & PERF_SAMPLE_REGS_INTR) {
2159 if (sample->intr_regs.abi) {
2160 result += sizeof(u64);
2161 sz = hweight_long(sample->intr_regs.mask) * sizeof(u64);
2162 result += sz;
2163 } else {
2164 result += sizeof(u64);
2165 }
2166 }
2167
Adrian Hunterb1cf6f62013-08-27 11:23:12 +03002168 return result;
2169}
2170
Andrew Vagin74eec262011-11-28 12:03:31 +03002171int perf_event__synthesize_sample(union perf_event *event, u64 type,
Jiri Olsa352ea452014-01-07 13:47:25 +01002172 u64 read_format,
Andrew Vagin74eec262011-11-28 12:03:31 +03002173 const struct perf_sample *sample,
2174 bool swapped)
2175{
2176 u64 *array;
Adrian Hunterd03f2172013-08-27 11:23:11 +03002177 size_t sz;
Andrew Vagin74eec262011-11-28 12:03:31 +03002178 /*
2179 * used for cross-endian analysis. See git commit 65014ab3
2180 * for why this goofiness is needed.
2181 */
Jiri Olsa6a11f922012-05-16 08:59:04 +02002182 union u64_swap u;
Andrew Vagin74eec262011-11-28 12:03:31 +03002183
2184 array = event->sample.array;
2185
Adrian Hunter75562572013-08-27 11:23:09 +03002186 if (type & PERF_SAMPLE_IDENTIFIER) {
2187 *array = sample->id;
2188 array++;
2189 }
2190
Andrew Vagin74eec262011-11-28 12:03:31 +03002191 if (type & PERF_SAMPLE_IP) {
Adrian Hunteref893252013-08-27 11:23:06 +03002192 *array = sample->ip;
Andrew Vagin74eec262011-11-28 12:03:31 +03002193 array++;
2194 }
2195
2196 if (type & PERF_SAMPLE_TID) {
2197 u.val32[0] = sample->pid;
2198 u.val32[1] = sample->tid;
2199 if (swapped) {
2200 /*
Arnaldo Carvalho de Meloa3f698f2012-08-02 12:23:46 -03002201 * Inverse of what is done in perf_evsel__parse_sample
Andrew Vagin74eec262011-11-28 12:03:31 +03002202 */
2203 u.val32[0] = bswap_32(u.val32[0]);
2204 u.val32[1] = bswap_32(u.val32[1]);
2205 u.val64 = bswap_64(u.val64);
2206 }
2207
2208 *array = u.val64;
2209 array++;
2210 }
2211
2212 if (type & PERF_SAMPLE_TIME) {
2213 *array = sample->time;
2214 array++;
2215 }
2216
2217 if (type & PERF_SAMPLE_ADDR) {
2218 *array = sample->addr;
2219 array++;
2220 }
2221
2222 if (type & PERF_SAMPLE_ID) {
2223 *array = sample->id;
2224 array++;
2225 }
2226
2227 if (type & PERF_SAMPLE_STREAM_ID) {
2228 *array = sample->stream_id;
2229 array++;
2230 }
2231
2232 if (type & PERF_SAMPLE_CPU) {
2233 u.val32[0] = sample->cpu;
2234 if (swapped) {
2235 /*
Arnaldo Carvalho de Meloa3f698f2012-08-02 12:23:46 -03002236 * Inverse of what is done in perf_evsel__parse_sample
Andrew Vagin74eec262011-11-28 12:03:31 +03002237 */
2238 u.val32[0] = bswap_32(u.val32[0]);
2239 u.val64 = bswap_64(u.val64);
2240 }
2241 *array = u.val64;
2242 array++;
2243 }
2244
2245 if (type & PERF_SAMPLE_PERIOD) {
2246 *array = sample->period;
2247 array++;
2248 }
2249
Adrian Hunterd03f2172013-08-27 11:23:11 +03002250 if (type & PERF_SAMPLE_READ) {
2251 if (read_format & PERF_FORMAT_GROUP)
2252 *array = sample->read.group.nr;
2253 else
2254 *array = sample->read.one.value;
2255 array++;
2256
2257 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) {
2258 *array = sample->read.time_enabled;
2259 array++;
2260 }
2261
2262 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) {
2263 *array = sample->read.time_running;
2264 array++;
2265 }
2266
2267 /* PERF_FORMAT_ID is forced for PERF_SAMPLE_READ */
2268 if (read_format & PERF_FORMAT_GROUP) {
2269 sz = sample->read.group.nr *
2270 sizeof(struct sample_read_value);
2271 memcpy(array, sample->read.group.values, sz);
2272 array = (void *)array + sz;
2273 } else {
2274 *array = sample->read.one.id;
2275 array++;
2276 }
2277 }
2278
2279 if (type & PERF_SAMPLE_CALLCHAIN) {
2280 sz = (sample->callchain->nr + 1) * sizeof(u64);
2281 memcpy(array, sample->callchain, sz);
2282 array = (void *)array + sz;
2283 }
2284
2285 if (type & PERF_SAMPLE_RAW) {
2286 u.val32[0] = sample->raw_size;
2287 if (WARN_ONCE(swapped,
2288 "Endianness of raw data not corrected!\n")) {
2289 /*
2290 * Inverse of what is done in perf_evsel__parse_sample
2291 */
2292 u.val32[0] = bswap_32(u.val32[0]);
2293 u.val32[1] = bswap_32(u.val32[1]);
2294 u.val64 = bswap_64(u.val64);
2295 }
2296 *array = u.val64;
2297 array = (void *)array + sizeof(u32);
2298
2299 memcpy(array, sample->raw_data, sample->raw_size);
2300 array = (void *)array + sample->raw_size;
2301 }
2302
2303 if (type & PERF_SAMPLE_BRANCH_STACK) {
2304 sz = sample->branch_stack->nr * sizeof(struct branch_entry);
2305 sz += sizeof(u64);
2306 memcpy(array, sample->branch_stack, sz);
2307 array = (void *)array + sz;
2308 }
2309
2310 if (type & PERF_SAMPLE_REGS_USER) {
2311 if (sample->user_regs.abi) {
2312 *array++ = sample->user_regs.abi;
Jiri Olsa352ea452014-01-07 13:47:25 +01002313 sz = hweight_long(sample->user_regs.mask) * sizeof(u64);
Adrian Hunterd03f2172013-08-27 11:23:11 +03002314 memcpy(array, sample->user_regs.regs, sz);
2315 array = (void *)array + sz;
2316 } else {
2317 *array++ = 0;
2318 }
2319 }
2320
2321 if (type & PERF_SAMPLE_STACK_USER) {
2322 sz = sample->user_stack.size;
2323 *array++ = sz;
2324 if (sz) {
2325 memcpy(array, sample->user_stack.data, sz);
2326 array = (void *)array + sz;
2327 *array++ = sz;
2328 }
2329 }
2330
2331 if (type & PERF_SAMPLE_WEIGHT) {
2332 *array = sample->weight;
2333 array++;
2334 }
2335
2336 if (type & PERF_SAMPLE_DATA_SRC) {
2337 *array = sample->data_src;
2338 array++;
2339 }
2340
Adrian Hunter42d88912013-11-01 15:51:38 +02002341 if (type & PERF_SAMPLE_TRANSACTION) {
2342 *array = sample->transaction;
2343 array++;
2344 }
2345
Stephane Eranian6a21c0b2014-09-24 13:48:39 +02002346 if (type & PERF_SAMPLE_REGS_INTR) {
2347 if (sample->intr_regs.abi) {
2348 *array++ = sample->intr_regs.abi;
2349 sz = hweight_long(sample->intr_regs.mask) * sizeof(u64);
2350 memcpy(array, sample->intr_regs.regs, sz);
2351 array = (void *)array + sz;
2352 } else {
2353 *array++ = 0;
2354 }
2355 }
2356
Andrew Vagin74eec262011-11-28 12:03:31 +03002357 return 0;
2358}
Arnaldo Carvalho de Melo5555ded2012-09-11 19:24:23 -03002359
Arnaldo Carvalho de Meloefd2b922012-09-18 11:21:50 -03002360struct format_field *perf_evsel__field(struct perf_evsel *evsel, const char *name)
2361{
2362 return pevent_find_field(evsel->tp_format, name);
2363}
2364
Arnaldo Carvalho de Melo5d2074e2012-09-26 20:22:00 -03002365void *perf_evsel__rawptr(struct perf_evsel *evsel, struct perf_sample *sample,
Arnaldo Carvalho de Melo5555ded2012-09-11 19:24:23 -03002366 const char *name)
2367{
Arnaldo Carvalho de Meloefd2b922012-09-18 11:21:50 -03002368 struct format_field *field = perf_evsel__field(evsel, name);
Arnaldo Carvalho de Melo5555ded2012-09-11 19:24:23 -03002369 int offset;
2370
Arnaldo Carvalho de Meloefd2b922012-09-18 11:21:50 -03002371 if (!field)
2372 return NULL;
Arnaldo Carvalho de Melo5555ded2012-09-11 19:24:23 -03002373
2374 offset = field->offset;
2375
2376 if (field->flags & FIELD_IS_DYNAMIC) {
2377 offset = *(int *)(sample->raw_data + field->offset);
2378 offset &= 0xffff;
2379 }
2380
2381 return sample->raw_data + offset;
2382}
2383
Arnaldo Carvalho de Melo90525172016-05-31 12:47:46 -03002384u64 format_field__intval(struct format_field *field, struct perf_sample *sample,
2385 bool needs_swap)
Arnaldo Carvalho de Melo5555ded2012-09-11 19:24:23 -03002386{
Arnaldo Carvalho de Meloe6b6f672012-09-26 13:13:04 -03002387 u64 value;
Arnaldo Carvalho de Melo90525172016-05-31 12:47:46 -03002388 void *ptr = sample->raw_data + field->offset;
Arnaldo Carvalho de Melo5555ded2012-09-11 19:24:23 -03002389
Arnaldo Carvalho de Meloe6b6f672012-09-26 13:13:04 -03002390 switch (field->size) {
2391 case 1:
2392 return *(u8 *)ptr;
2393 case 2:
2394 value = *(u16 *)ptr;
2395 break;
2396 case 4:
2397 value = *(u32 *)ptr;
2398 break;
2399 case 8:
David Aherne94eeda2015-03-24 16:14:09 -04002400 memcpy(&value, ptr, sizeof(u64));
Arnaldo Carvalho de Meloe6b6f672012-09-26 13:13:04 -03002401 break;
2402 default:
2403 return 0;
2404 }
2405
Arnaldo Carvalho de Melo90525172016-05-31 12:47:46 -03002406 if (!needs_swap)
Arnaldo Carvalho de Meloe6b6f672012-09-26 13:13:04 -03002407 return value;
2408
2409 switch (field->size) {
2410 case 2:
2411 return bswap_16(value);
2412 case 4:
2413 return bswap_32(value);
2414 case 8:
2415 return bswap_64(value);
2416 default:
2417 return 0;
2418 }
2419
2420 return 0;
Arnaldo Carvalho de Melo5555ded2012-09-11 19:24:23 -03002421}
Arnaldo Carvalho de Melo0698aed2012-12-10 18:17:08 -03002422
Arnaldo Carvalho de Melo90525172016-05-31 12:47:46 -03002423u64 perf_evsel__intval(struct perf_evsel *evsel, struct perf_sample *sample,
2424 const char *name)
2425{
2426 struct format_field *field = perf_evsel__field(evsel, name);
2427
2428 if (!field)
2429 return 0;
2430
2431 return field ? format_field__intval(field, sample, evsel->needs_swap) : 0;
2432}
2433
Arnaldo Carvalho de Meloc0a54342012-12-13 14:16:30 -03002434bool perf_evsel__fallback(struct perf_evsel *evsel, int err,
2435 char *msg, size_t msgsize)
2436{
Arnaldo Carvalho de Melo08094822016-05-12 16:07:47 -03002437 int paranoid;
2438
David Ahern2b821cc2013-07-18 17:27:59 -06002439 if ((err == ENOENT || err == ENXIO || err == ENODEV) &&
Arnaldo Carvalho de Meloc0a54342012-12-13 14:16:30 -03002440 evsel->attr.type == PERF_TYPE_HARDWARE &&
2441 evsel->attr.config == PERF_COUNT_HW_CPU_CYCLES) {
2442 /*
2443 * If it's cycles then fall back to hrtimer based
2444 * cpu-clock-tick sw counter, which is always available even if
2445 * no PMU support.
2446 *
2447 * PPC returns ENXIO until 2.6.37 (behavior changed with commit
2448 * b0a873e).
2449 */
2450 scnprintf(msg, msgsize, "%s",
2451"The cycles event is not supported, trying to fall back to cpu-clock-ticks");
2452
2453 evsel->attr.type = PERF_TYPE_SOFTWARE;
2454 evsel->attr.config = PERF_COUNT_SW_CPU_CLOCK;
2455
Arnaldo Carvalho de Melo04662522013-12-26 17:41:15 -03002456 zfree(&evsel->name);
Arnaldo Carvalho de Meloc0a54342012-12-13 14:16:30 -03002457 return true;
Arnaldo Carvalho de Melo08094822016-05-12 16:07:47 -03002458 } else if (err == EACCES && !evsel->attr.exclude_kernel &&
2459 (paranoid = perf_event_paranoid()) > 1) {
2460 const char *name = perf_evsel__name(evsel);
2461 char *new_name;
2462
2463 if (asprintf(&new_name, "%s%su", name, strchr(name, ':') ? "" : ":") < 0)
2464 return false;
2465
2466 if (evsel->name)
2467 free(evsel->name);
2468 evsel->name = new_name;
2469 scnprintf(msg, msgsize,
2470"kernel.perf_event_paranoid=%d, trying to fall back to excluding kernel samples", paranoid);
2471 evsel->attr.exclude_kernel = 1;
2472
2473 return true;
Arnaldo Carvalho de Meloc0a54342012-12-13 14:16:30 -03002474 }
2475
2476 return false;
2477}
Arnaldo Carvalho de Melo56e52e82012-12-13 15:10:58 -03002478
Arnaldo Carvalho de Melo2157f6e2017-06-20 12:05:38 -03002479static bool find_process(const char *name)
2480{
2481 size_t len = strlen(name);
2482 DIR *dir;
2483 struct dirent *d;
2484 int ret = -1;
2485
2486 dir = opendir(procfs__mountpoint());
2487 if (!dir)
2488 return false;
2489
2490 /* Walk through the directory. */
2491 while (ret && (d = readdir(dir)) != NULL) {
2492 char path[PATH_MAX];
2493 char *data;
2494 size_t size;
2495
2496 if ((d->d_type != DT_DIR) ||
2497 !strcmp(".", d->d_name) ||
2498 !strcmp("..", d->d_name))
2499 continue;
2500
2501 scnprintf(path, sizeof(path), "%s/%s/comm",
2502 procfs__mountpoint(), d->d_name);
2503
2504 if (filename__read_str(path, &data, &size))
2505 continue;
2506
2507 ret = strncmp(name, data, len);
2508 free(data);
2509 }
2510
2511 closedir(dir);
2512 return ret ? false : true;
2513}
2514
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03002515int perf_evsel__open_strerror(struct perf_evsel *evsel, struct target *target,
Arnaldo Carvalho de Melo56e52e82012-12-13 15:10:58 -03002516 int err, char *msg, size_t size)
2517{
Masami Hiramatsu6e81c742014-08-14 02:22:36 +00002518 char sbuf[STRERR_BUFSIZE];
Jin Yao32ccb132017-04-07 20:08:52 +08002519 int printed = 0;
Masami Hiramatsu6e81c742014-08-14 02:22:36 +00002520
Arnaldo Carvalho de Melo56e52e82012-12-13 15:10:58 -03002521 switch (err) {
2522 case EPERM:
2523 case EACCES:
Jin Yao32ccb132017-04-07 20:08:52 +08002524 if (err == EPERM)
2525 printed = scnprintf(msg, size,
2526 "No permission to enable %s event.\n\n",
2527 perf_evsel__name(evsel));
2528
2529 return scnprintf(msg + printed, size - printed,
Ben Hutchings3379e0c2016-01-19 21:35:15 +00002530 "You may not have permission to collect %sstats.\n\n"
2531 "Consider tweaking /proc/sys/kernel/perf_event_paranoid,\n"
2532 "which controls use of the performance events system by\n"
2533 "unprivileged users (without CAP_SYS_ADMIN).\n\n"
Arnaldo Carvalho de Melo7d173912016-05-12 15:44:55 -03002534 "The current value is %d:\n\n"
Ben Hutchings3379e0c2016-01-19 21:35:15 +00002535 " -1: Allow use of (almost) all events by all users\n"
2536 ">= 0: Disallow raw tracepoint access by users without CAP_IOC_LOCK\n"
2537 ">= 1: Disallow CPU event access by users without CAP_SYS_ADMIN\n"
Arnaldo Carvalho de Melod6195a62017-02-13 16:45:24 -03002538 ">= 2: Disallow kernel profiling by users without CAP_SYS_ADMIN\n\n"
2539 "To make this setting permanent, edit /etc/sysctl.conf too, e.g.:\n\n"
2540 " kernel.perf_event_paranoid = -1\n" ,
Arnaldo Carvalho de Melo7d173912016-05-12 15:44:55 -03002541 target->system_wide ? "system-wide " : "",
2542 perf_event_paranoid());
Arnaldo Carvalho de Melo56e52e82012-12-13 15:10:58 -03002543 case ENOENT:
2544 return scnprintf(msg, size, "The %s event is not supported.",
2545 perf_evsel__name(evsel));
2546 case EMFILE:
2547 return scnprintf(msg, size, "%s",
2548 "Too many events are opened.\n"
Jiri Olsa18ffdfe2015-05-25 22:51:54 +02002549 "Probably the maximum number of open file descriptors has been reached.\n"
2550 "Hint: Try again after reducing the number of events.\n"
2551 "Hint: Try increasing the limit with 'ulimit -n <limit>'");
Arnaldo Carvalho de Melode46d522016-04-27 17:51:45 -03002552 case ENOMEM:
2553 if ((evsel->attr.sample_type & PERF_SAMPLE_CALLCHAIN) != 0 &&
2554 access("/proc/sys/kernel/perf_event_max_stack", F_OK) == 0)
2555 return scnprintf(msg, size,
2556 "Not enough memory to setup event with callchain.\n"
2557 "Hint: Try tweaking /proc/sys/kernel/perf_event_max_stack\n"
2558 "Hint: Current value: %d", sysctl_perf_event_max_stack);
2559 break;
Arnaldo Carvalho de Melo56e52e82012-12-13 15:10:58 -03002560 case ENODEV:
2561 if (target->cpu_list)
2562 return scnprintf(msg, size, "%s",
Arnaldo Carvalho de Melo81d64f42016-04-27 17:56:53 -03002563 "No such device - did you specify an out-of-range profile CPU?");
Arnaldo Carvalho de Melo56e52e82012-12-13 15:10:58 -03002564 break;
2565 case EOPNOTSUPP:
Vineet Guptadc89e752016-05-09 15:07:39 +05302566 if (evsel->attr.sample_period != 0)
2567 return scnprintf(msg, size, "%s",
2568 "PMU Hardware doesn't support sampling/overflow-interrupts.");
Arnaldo Carvalho de Melo56e52e82012-12-13 15:10:58 -03002569 if (evsel->attr.precise_ip)
2570 return scnprintf(msg, size, "%s",
2571 "\'precise\' request may not be supported. Try removing 'p' modifier.");
2572#if defined(__i386__) || defined(__x86_64__)
2573 if (evsel->attr.type == PERF_TYPE_HARDWARE)
2574 return scnprintf(msg, size, "%s",
2575 "No hardware sampling interrupt available.\n"
2576 "No APIC? If so then you can boot the kernel with the \"lapic\" boot parameter to force-enable it.");
2577#endif
2578 break;
Jiri Olsa63914ac2014-08-01 17:46:54 +02002579 case EBUSY:
2580 if (find_process("oprofiled"))
2581 return scnprintf(msg, size,
2582 "The PMU counters are busy/taken by another profiler.\n"
2583 "We found oprofile daemon running, please stop it and try again.");
2584 break;
Peter Zijlstra814c8c32015-03-31 00:19:31 +02002585 case EINVAL:
Arnaldo Carvalho de Melo32a951b2016-07-14 08:34:33 +00002586 if (evsel->attr.write_backward && perf_missing_features.write_backward)
Arnaldo Carvalho de Melo7da36e92016-06-20 10:47:18 +00002587 return scnprintf(msg, size, "Reading from overwrite event is not supported by this kernel.");
Peter Zijlstra814c8c32015-03-31 00:19:31 +02002588 if (perf_missing_features.clockid)
2589 return scnprintf(msg, size, "clockid feature not supported.");
2590 if (perf_missing_features.clockid_wrong)
2591 return scnprintf(msg, size, "wrong clockid (%d).", clockid);
2592 break;
Arnaldo Carvalho de Melo56e52e82012-12-13 15:10:58 -03002593 default:
2594 break;
2595 }
2596
2597 return scnprintf(msg, size,
Masami Hiramatsu6e81c742014-08-14 02:22:36 +00002598 "The sys_perf_event_open() syscall returned with %d (%s) for event (%s).\n"
Arnaldo Carvalho de Melo56e52e82012-12-13 15:10:58 -03002599 "/bin/dmesg may provide additional information.\n"
Arnaldo Carvalho de Melo81d64f42016-04-27 17:56:53 -03002600 "No CONFIG_PERF_EVENTS=y kernel support configured?",
Arnaldo Carvalho de Meloc8b5f2c2016-07-06 11:56:20 -03002601 err, str_error_r(err, sbuf, sizeof(sbuf)),
Masami Hiramatsu6e81c742014-08-14 02:22:36 +00002602 perf_evsel__name(evsel));
Arnaldo Carvalho de Melo56e52e82012-12-13 15:10:58 -03002603}
Ravi Bangoriaf4e47f92016-06-30 11:44:19 +05302604
2605char *perf_evsel__env_arch(struct perf_evsel *evsel)
2606{
2607 if (evsel && evsel->evlist && evsel->evlist->env)
2608 return evsel->evlist->env->arch;
2609 return NULL;
2610}