blob: 0b1c289da36d2ed510fe653e83e364a24182e40d [file] [log] [blame]
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -02001/*
2 * Copyright (C) 2011, Red Hat Inc, Arnaldo Carvalho de Melo <acme@redhat.com>
3 *
4 * Parts came from builtin-{top,stat,record}.c, see those files for further
5 * copyright notes.
6 *
7 * Released under the GPL v2. (and only v2, not any later version)
8 */
9
David Ahern936be502011-09-06 09:12:26 -060010#include <byteswap.h>
Jiri Olsa0f6a3012012-08-07 15:20:45 +020011#include <linux/bitops.h>
Jiri Olsa4605eab2015-09-02 09:56:43 +020012#include <api/fs/tracing_path.h>
Robert Richter4e319022013-06-11 17:29:18 +020013#include <traceevent/event-parse.h>
14#include <linux/hw_breakpoint.h>
15#include <linux/perf_event.h>
Jiri Olsa8dd2a132015-09-07 10:38:06 +020016#include <linux/err.h>
Andi Kleenbec19672013-08-04 19:41:26 -070017#include <sys/resource.h>
Robert Richter4e319022013-06-11 17:29:18 +020018#include "asm/bug.h"
Arnaldo Carvalho de Melo8f651ea2014-10-09 16:12:24 -030019#include "callchain.h"
Arnaldo Carvalho de Melof14d5702014-10-17 12:17:40 -030020#include "cgroup.h"
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -020021#include "evsel.h"
Arnaldo Carvalho de Melo70082dd2011-01-12 17:03:24 -020022#include "evlist.h"
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -020023#include "util.h"
Arnaldo Carvalho de Melo86bd5e82011-01-03 23:09:46 -020024#include "cpumap.h"
Arnaldo Carvalho de Melofd782602011-01-18 15:15:24 -020025#include "thread_map.h"
Namhyung Kim12864b32012-04-26 14:15:22 +090026#include "target.h"
Jiri Olsa26d33022012-08-07 15:20:47 +020027#include "perf_regs.h"
Adrian Huntere3e1a542013-08-14 15:48:24 +030028#include "debug.h"
Jiri Olsa97978b32013-12-03 14:09:24 +010029#include "trace-event.h"
Jiri Olsaa9a3a4d2015-06-14 10:19:26 +020030#include "stat.h"
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -020031
Arnaldo Carvalho de Melo594ac612012-12-13 13:13:07 -030032static struct {
33 bool sample_id_all;
34 bool exclude_guest;
Stephane Eranian5c5e8542013-08-21 12:10:25 +020035 bool mmap2;
Yann Droneaud57480d22014-06-30 22:28:47 +020036 bool cloexec;
Peter Zijlstra814c8c32015-03-31 00:19:31 +020037 bool clockid;
38 bool clockid_wrong;
Arnaldo Carvalho de Melo594ac612012-12-13 13:13:07 -030039} perf_missing_features;
40
Peter Zijlstra814c8c32015-03-31 00:19:31 +020041static clockid_t clockid;
42
Arnaldo Carvalho de Meloce8ccff2014-10-09 15:29:51 -030043static int perf_evsel__no_extra_init(struct perf_evsel *evsel __maybe_unused)
44{
45 return 0;
46}
47
48static void perf_evsel__no_extra_fini(struct perf_evsel *evsel __maybe_unused)
49{
50}
51
52static struct {
53 size_t size;
54 int (*init)(struct perf_evsel *evsel);
55 void (*fini)(struct perf_evsel *evsel);
56} perf_evsel__object = {
57 .size = sizeof(struct perf_evsel),
58 .init = perf_evsel__no_extra_init,
59 .fini = perf_evsel__no_extra_fini,
60};
61
62int perf_evsel__object_config(size_t object_size,
63 int (*init)(struct perf_evsel *evsel),
64 void (*fini)(struct perf_evsel *evsel))
65{
66
67 if (object_size == 0)
68 goto set_methods;
69
70 if (perf_evsel__object.size > object_size)
71 return -EINVAL;
72
73 perf_evsel__object.size = object_size;
74
75set_methods:
76 if (init != NULL)
77 perf_evsel__object.init = init;
78
79 if (fini != NULL)
80 perf_evsel__object.fini = fini;
81
82 return 0;
83}
84
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -020085#define FD(e, x, y) (*(int *)xyarray__entry(e->fd, x, y))
86
Adrian Hunter75562572013-08-27 11:23:09 +030087int __perf_evsel__sample_size(u64 sample_type)
Arnaldo Carvalho de Meloc2a70652011-06-02 11:04:54 -030088{
89 u64 mask = sample_type & PERF_SAMPLE_MASK;
90 int size = 0;
91 int i;
92
93 for (i = 0; i < 64; i++) {
94 if (mask & (1ULL << i))
95 size++;
96 }
97
98 size *= sizeof(u64);
99
100 return size;
101}
102
Adrian Hunter75562572013-08-27 11:23:09 +0300103/**
104 * __perf_evsel__calc_id_pos - calculate id_pos.
105 * @sample_type: sample type
106 *
107 * This function returns the position of the event id (PERF_SAMPLE_ID or
108 * PERF_SAMPLE_IDENTIFIER) in a sample event i.e. in the array of struct
109 * sample_event.
110 */
111static int __perf_evsel__calc_id_pos(u64 sample_type)
112{
113 int idx = 0;
114
115 if (sample_type & PERF_SAMPLE_IDENTIFIER)
116 return 0;
117
118 if (!(sample_type & PERF_SAMPLE_ID))
119 return -1;
120
121 if (sample_type & PERF_SAMPLE_IP)
122 idx += 1;
123
124 if (sample_type & PERF_SAMPLE_TID)
125 idx += 1;
126
127 if (sample_type & PERF_SAMPLE_TIME)
128 idx += 1;
129
130 if (sample_type & PERF_SAMPLE_ADDR)
131 idx += 1;
132
133 return idx;
134}
135
136/**
137 * __perf_evsel__calc_is_pos - calculate is_pos.
138 * @sample_type: sample type
139 *
140 * This function returns the position (counting backwards) of the event id
141 * (PERF_SAMPLE_ID or PERF_SAMPLE_IDENTIFIER) in a non-sample event i.e. if
142 * sample_id_all is used there is an id sample appended to non-sample events.
143 */
144static int __perf_evsel__calc_is_pos(u64 sample_type)
145{
146 int idx = 1;
147
148 if (sample_type & PERF_SAMPLE_IDENTIFIER)
149 return 1;
150
151 if (!(sample_type & PERF_SAMPLE_ID))
152 return -1;
153
154 if (sample_type & PERF_SAMPLE_CPU)
155 idx += 1;
156
157 if (sample_type & PERF_SAMPLE_STREAM_ID)
158 idx += 1;
159
160 return idx;
161}
162
163void perf_evsel__calc_id_pos(struct perf_evsel *evsel)
164{
165 evsel->id_pos = __perf_evsel__calc_id_pos(evsel->attr.sample_type);
166 evsel->is_pos = __perf_evsel__calc_is_pos(evsel->attr.sample_type);
167}
168
Arnaldo Carvalho de Melo7be5ebe2012-12-10 14:53:43 -0300169void __perf_evsel__set_sample_bit(struct perf_evsel *evsel,
170 enum perf_event_sample_format bit)
171{
172 if (!(evsel->attr.sample_type & bit)) {
173 evsel->attr.sample_type |= bit;
174 evsel->sample_size += sizeof(u64);
Adrian Hunter75562572013-08-27 11:23:09 +0300175 perf_evsel__calc_id_pos(evsel);
Arnaldo Carvalho de Melo7be5ebe2012-12-10 14:53:43 -0300176 }
177}
178
179void __perf_evsel__reset_sample_bit(struct perf_evsel *evsel,
180 enum perf_event_sample_format bit)
181{
182 if (evsel->attr.sample_type & bit) {
183 evsel->attr.sample_type &= ~bit;
184 evsel->sample_size -= sizeof(u64);
Adrian Hunter75562572013-08-27 11:23:09 +0300185 perf_evsel__calc_id_pos(evsel);
Arnaldo Carvalho de Melo7be5ebe2012-12-10 14:53:43 -0300186 }
187}
188
Adrian Hunter75562572013-08-27 11:23:09 +0300189void perf_evsel__set_sample_id(struct perf_evsel *evsel,
190 bool can_sample_identifier)
Arnaldo Carvalho de Melo7a5a5ca2012-12-10 15:21:30 -0300191{
Adrian Hunter75562572013-08-27 11:23:09 +0300192 if (can_sample_identifier) {
193 perf_evsel__reset_sample_bit(evsel, ID);
194 perf_evsel__set_sample_bit(evsel, IDENTIFIER);
195 } else {
196 perf_evsel__set_sample_bit(evsel, ID);
197 }
Arnaldo Carvalho de Melo7a5a5ca2012-12-10 15:21:30 -0300198 evsel->attr.read_format |= PERF_FORMAT_ID;
199}
200
Arnaldo Carvalho de Meloef1d1af2011-01-18 21:41:45 -0200201void perf_evsel__init(struct perf_evsel *evsel,
202 struct perf_event_attr *attr, int idx)
203{
204 evsel->idx = idx;
Adrian Hunter60b08962014-07-31 09:00:52 +0300205 evsel->tracking = !idx;
Arnaldo Carvalho de Meloef1d1af2011-01-18 21:41:45 -0200206 evsel->attr = *attr;
Namhyung Kim2cfda562012-11-29 15:38:29 +0900207 evsel->leader = evsel;
Stephane Eranian410136f2013-11-12 17:58:49 +0100208 evsel->unit = "";
209 evsel->scale = 1.0;
Arnaldo Carvalho de Melod49e4692015-08-27 08:07:40 -0400210 evsel->evlist = NULL;
Arnaldo Carvalho de Meloef1d1af2011-01-18 21:41:45 -0200211 INIT_LIST_HEAD(&evsel->node);
Jiri Olsa930a2e22015-07-29 05:42:10 -0400212 INIT_LIST_HEAD(&evsel->config_terms);
Arnaldo Carvalho de Meloce8ccff2014-10-09 15:29:51 -0300213 perf_evsel__object.init(evsel);
Arnaldo Carvalho de Melobde09462012-08-01 18:53:11 -0300214 evsel->sample_size = __perf_evsel__sample_size(attr->sample_type);
Adrian Hunter75562572013-08-27 11:23:09 +0300215 perf_evsel__calc_id_pos(evsel);
Wang Nan15bfd2c2015-07-10 07:36:09 +0000216 evsel->cmdline_group_boundary = false;
Arnaldo Carvalho de Meloef1d1af2011-01-18 21:41:45 -0200217}
218
Arnaldo Carvalho de Meloef503832013-11-07 16:41:19 -0300219struct perf_evsel *perf_evsel__new_idx(struct perf_event_attr *attr, int idx)
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200220{
Arnaldo Carvalho de Meloce8ccff2014-10-09 15:29:51 -0300221 struct perf_evsel *evsel = zalloc(perf_evsel__object.size);
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200222
Arnaldo Carvalho de Meloef1d1af2011-01-18 21:41:45 -0200223 if (evsel != NULL)
224 perf_evsel__init(evsel, attr, idx);
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200225
226 return evsel;
227}
228
Jiri Olsa8dd2a132015-09-07 10:38:06 +0200229/*
230 * Returns pointer with encoded error via <linux/err.h> interface.
231 */
Arnaldo Carvalho de Meloef503832013-11-07 16:41:19 -0300232struct perf_evsel *perf_evsel__newtp_idx(const char *sys, const char *name, int idx)
Arnaldo Carvalho de Meloefd2b922012-09-18 11:21:50 -0300233{
Arnaldo Carvalho de Meloce8ccff2014-10-09 15:29:51 -0300234 struct perf_evsel *evsel = zalloc(perf_evsel__object.size);
Jiri Olsa8dd2a132015-09-07 10:38:06 +0200235 int err = -ENOMEM;
Arnaldo Carvalho de Meloefd2b922012-09-18 11:21:50 -0300236
Jiri Olsa8dd2a132015-09-07 10:38:06 +0200237 if (evsel == NULL) {
238 goto out_err;
239 } else {
Arnaldo Carvalho de Meloefd2b922012-09-18 11:21:50 -0300240 struct perf_event_attr attr = {
Arnaldo Carvalho de Melo0b80f8b32012-09-26 12:28:26 -0300241 .type = PERF_TYPE_TRACEPOINT,
242 .sample_type = (PERF_SAMPLE_RAW | PERF_SAMPLE_TIME |
243 PERF_SAMPLE_CPU | PERF_SAMPLE_PERIOD),
Arnaldo Carvalho de Meloefd2b922012-09-18 11:21:50 -0300244 };
245
Arnaldo Carvalho de Meloe48ffe22012-09-26 17:11:38 -0300246 if (asprintf(&evsel->name, "%s:%s", sys, name) < 0)
247 goto out_free;
248
Jiri Olsa97978b32013-12-03 14:09:24 +0100249 evsel->tp_format = trace_event__tp_format(sys, name);
Jiri Olsa8dd2a132015-09-07 10:38:06 +0200250 if (IS_ERR(evsel->tp_format)) {
251 err = PTR_ERR(evsel->tp_format);
Arnaldo Carvalho de Meloefd2b922012-09-18 11:21:50 -0300252 goto out_free;
Jiri Olsa8dd2a132015-09-07 10:38:06 +0200253 }
Arnaldo Carvalho de Meloefd2b922012-09-18 11:21:50 -0300254
Arnaldo Carvalho de Melo0b80f8b32012-09-26 12:28:26 -0300255 event_attr_init(&attr);
Arnaldo Carvalho de Meloefd2b922012-09-18 11:21:50 -0300256 attr.config = evsel->tp_format->id;
Arnaldo Carvalho de Melo0b80f8b32012-09-26 12:28:26 -0300257 attr.sample_period = 1;
Arnaldo Carvalho de Meloefd2b922012-09-18 11:21:50 -0300258 perf_evsel__init(evsel, &attr, idx);
Arnaldo Carvalho de Meloefd2b922012-09-18 11:21:50 -0300259 }
260
261 return evsel;
262
263out_free:
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -0300264 zfree(&evsel->name);
Arnaldo Carvalho de Meloefd2b922012-09-18 11:21:50 -0300265 free(evsel);
Jiri Olsa8dd2a132015-09-07 10:38:06 +0200266out_err:
267 return ERR_PTR(err);
Arnaldo Carvalho de Meloefd2b922012-09-18 11:21:50 -0300268}
269
Arnaldo Carvalho de Melo8ad70132012-09-06 13:11:18 -0300270const char *perf_evsel__hw_names[PERF_COUNT_HW_MAX] = {
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300271 "cycles",
272 "instructions",
273 "cache-references",
274 "cache-misses",
275 "branches",
276 "branch-misses",
277 "bus-cycles",
278 "stalled-cycles-frontend",
279 "stalled-cycles-backend",
280 "ref-cycles",
281};
282
Arnaldo Carvalho de Melodd4f5222012-06-13 15:52:42 -0300283static const char *__perf_evsel__hw_name(u64 config)
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300284{
285 if (config < PERF_COUNT_HW_MAX && perf_evsel__hw_names[config])
286 return perf_evsel__hw_names[config];
287
288 return "unknown-hardware";
289}
290
Arnaldo Carvalho de Melo27f18612012-06-11 13:33:09 -0300291static int perf_evsel__add_modifiers(struct perf_evsel *evsel, char *bf, size_t size)
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300292{
Arnaldo Carvalho de Melo27f18612012-06-11 13:33:09 -0300293 int colon = 0, r = 0;
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300294 struct perf_event_attr *attr = &evsel->attr;
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300295 bool exclude_guest_default = false;
296
297#define MOD_PRINT(context, mod) do { \
298 if (!attr->exclude_##context) { \
Arnaldo Carvalho de Melo27f18612012-06-11 13:33:09 -0300299 if (!colon) colon = ++r; \
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300300 r += scnprintf(bf + r, size - r, "%c", mod); \
301 } } while(0)
302
303 if (attr->exclude_kernel || attr->exclude_user || attr->exclude_hv) {
304 MOD_PRINT(kernel, 'k');
305 MOD_PRINT(user, 'u');
306 MOD_PRINT(hv, 'h');
307 exclude_guest_default = true;
308 }
309
310 if (attr->precise_ip) {
311 if (!colon)
Arnaldo Carvalho de Melo27f18612012-06-11 13:33:09 -0300312 colon = ++r;
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300313 r += scnprintf(bf + r, size - r, "%.*s", attr->precise_ip, "ppp");
314 exclude_guest_default = true;
315 }
316
317 if (attr->exclude_host || attr->exclude_guest == exclude_guest_default) {
318 MOD_PRINT(host, 'H');
319 MOD_PRINT(guest, 'G');
320 }
321#undef MOD_PRINT
322 if (colon)
Arnaldo Carvalho de Melo27f18612012-06-11 13:33:09 -0300323 bf[colon - 1] = ':';
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300324 return r;
325}
326
Arnaldo Carvalho de Melo27f18612012-06-11 13:33:09 -0300327static int perf_evsel__hw_name(struct perf_evsel *evsel, char *bf, size_t size)
328{
329 int r = scnprintf(bf, size, "%s", __perf_evsel__hw_name(evsel->attr.config));
330 return r + perf_evsel__add_modifiers(evsel, bf + r, size - r);
331}
332
Arnaldo Carvalho de Melo8ad70132012-09-06 13:11:18 -0300333const char *perf_evsel__sw_names[PERF_COUNT_SW_MAX] = {
Arnaldo Carvalho de Melo335c2f52012-06-11 14:36:20 -0300334 "cpu-clock",
335 "task-clock",
336 "page-faults",
337 "context-switches",
Arnaldo Carvalho de Melo8ad70132012-09-06 13:11:18 -0300338 "cpu-migrations",
Arnaldo Carvalho de Melo335c2f52012-06-11 14:36:20 -0300339 "minor-faults",
340 "major-faults",
341 "alignment-faults",
342 "emulation-faults",
Adrian Hunterd22d1a22013-08-31 21:50:52 +0300343 "dummy",
Arnaldo Carvalho de Melo335c2f52012-06-11 14:36:20 -0300344};
345
Arnaldo Carvalho de Melodd4f5222012-06-13 15:52:42 -0300346static const char *__perf_evsel__sw_name(u64 config)
Arnaldo Carvalho de Melo335c2f52012-06-11 14:36:20 -0300347{
348 if (config < PERF_COUNT_SW_MAX && perf_evsel__sw_names[config])
349 return perf_evsel__sw_names[config];
350 return "unknown-software";
351}
352
353static int perf_evsel__sw_name(struct perf_evsel *evsel, char *bf, size_t size)
354{
355 int r = scnprintf(bf, size, "%s", __perf_evsel__sw_name(evsel->attr.config));
356 return r + perf_evsel__add_modifiers(evsel, bf + r, size - r);
357}
358
Jiri Olsa287e74a2012-06-28 23:18:49 +0200359static int __perf_evsel__bp_name(char *bf, size_t size, u64 addr, u64 type)
360{
361 int r;
362
363 r = scnprintf(bf, size, "mem:0x%" PRIx64 ":", addr);
364
365 if (type & HW_BREAKPOINT_R)
366 r += scnprintf(bf + r, size - r, "r");
367
368 if (type & HW_BREAKPOINT_W)
369 r += scnprintf(bf + r, size - r, "w");
370
371 if (type & HW_BREAKPOINT_X)
372 r += scnprintf(bf + r, size - r, "x");
373
374 return r;
375}
376
377static int perf_evsel__bp_name(struct perf_evsel *evsel, char *bf, size_t size)
378{
379 struct perf_event_attr *attr = &evsel->attr;
380 int r = __perf_evsel__bp_name(bf, size, attr->bp_addr, attr->bp_type);
381 return r + perf_evsel__add_modifiers(evsel, bf + r, size - r);
382}
383
Arnaldo Carvalho de Melo0b668bc2012-06-11 14:08:07 -0300384const char *perf_evsel__hw_cache[PERF_COUNT_HW_CACHE_MAX]
385 [PERF_EVSEL__MAX_ALIASES] = {
386 { "L1-dcache", "l1-d", "l1d", "L1-data", },
387 { "L1-icache", "l1-i", "l1i", "L1-instruction", },
388 { "LLC", "L2", },
389 { "dTLB", "d-tlb", "Data-TLB", },
390 { "iTLB", "i-tlb", "Instruction-TLB", },
391 { "branch", "branches", "bpu", "btb", "bpc", },
392 { "node", },
393};
394
395const char *perf_evsel__hw_cache_op[PERF_COUNT_HW_CACHE_OP_MAX]
396 [PERF_EVSEL__MAX_ALIASES] = {
397 { "load", "loads", "read", },
398 { "store", "stores", "write", },
399 { "prefetch", "prefetches", "speculative-read", "speculative-load", },
400};
401
402const char *perf_evsel__hw_cache_result[PERF_COUNT_HW_CACHE_RESULT_MAX]
403 [PERF_EVSEL__MAX_ALIASES] = {
404 { "refs", "Reference", "ops", "access", },
405 { "misses", "miss", },
406};
407
408#define C(x) PERF_COUNT_HW_CACHE_##x
409#define CACHE_READ (1 << C(OP_READ))
410#define CACHE_WRITE (1 << C(OP_WRITE))
411#define CACHE_PREFETCH (1 << C(OP_PREFETCH))
412#define COP(x) (1 << x)
413
414/*
415 * cache operartion stat
416 * L1I : Read and prefetch only
417 * ITLB and BPU : Read-only
418 */
419static unsigned long perf_evsel__hw_cache_stat[C(MAX)] = {
420 [C(L1D)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
421 [C(L1I)] = (CACHE_READ | CACHE_PREFETCH),
422 [C(LL)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
423 [C(DTLB)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
424 [C(ITLB)] = (CACHE_READ),
425 [C(BPU)] = (CACHE_READ),
426 [C(NODE)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
427};
428
429bool perf_evsel__is_cache_op_valid(u8 type, u8 op)
430{
431 if (perf_evsel__hw_cache_stat[type] & COP(op))
432 return true; /* valid */
433 else
434 return false; /* invalid */
435}
436
437int __perf_evsel__hw_cache_type_op_res_name(u8 type, u8 op, u8 result,
438 char *bf, size_t size)
439{
440 if (result) {
441 return scnprintf(bf, size, "%s-%s-%s", perf_evsel__hw_cache[type][0],
442 perf_evsel__hw_cache_op[op][0],
443 perf_evsel__hw_cache_result[result][0]);
444 }
445
446 return scnprintf(bf, size, "%s-%s", perf_evsel__hw_cache[type][0],
447 perf_evsel__hw_cache_op[op][1]);
448}
449
Arnaldo Carvalho de Melodd4f5222012-06-13 15:52:42 -0300450static int __perf_evsel__hw_cache_name(u64 config, char *bf, size_t size)
Arnaldo Carvalho de Melo0b668bc2012-06-11 14:08:07 -0300451{
452 u8 op, result, type = (config >> 0) & 0xff;
453 const char *err = "unknown-ext-hardware-cache-type";
454
455 if (type > PERF_COUNT_HW_CACHE_MAX)
456 goto out_err;
457
458 op = (config >> 8) & 0xff;
459 err = "unknown-ext-hardware-cache-op";
460 if (op > PERF_COUNT_HW_CACHE_OP_MAX)
461 goto out_err;
462
463 result = (config >> 16) & 0xff;
464 err = "unknown-ext-hardware-cache-result";
465 if (result > PERF_COUNT_HW_CACHE_RESULT_MAX)
466 goto out_err;
467
468 err = "invalid-cache";
469 if (!perf_evsel__is_cache_op_valid(type, op))
470 goto out_err;
471
472 return __perf_evsel__hw_cache_type_op_res_name(type, op, result, bf, size);
473out_err:
474 return scnprintf(bf, size, "%s", err);
475}
476
477static int perf_evsel__hw_cache_name(struct perf_evsel *evsel, char *bf, size_t size)
478{
479 int ret = __perf_evsel__hw_cache_name(evsel->attr.config, bf, size);
480 return ret + perf_evsel__add_modifiers(evsel, bf + ret, size - ret);
481}
482
Arnaldo Carvalho de Melo6eef3d92012-06-13 11:53:37 -0300483static int perf_evsel__raw_name(struct perf_evsel *evsel, char *bf, size_t size)
484{
485 int ret = scnprintf(bf, size, "raw 0x%" PRIx64, evsel->attr.config);
486 return ret + perf_evsel__add_modifiers(evsel, bf + ret, size - ret);
487}
488
Arnaldo Carvalho de Melo7289f832012-06-12 12:34:58 -0300489const char *perf_evsel__name(struct perf_evsel *evsel)
Arnaldo Carvalho de Meloa4460832012-06-12 10:29:12 -0300490{
Arnaldo Carvalho de Melo7289f832012-06-12 12:34:58 -0300491 char bf[128];
Arnaldo Carvalho de Meloa4460832012-06-12 10:29:12 -0300492
Arnaldo Carvalho de Melo7289f832012-06-12 12:34:58 -0300493 if (evsel->name)
494 return evsel->name;
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300495
496 switch (evsel->attr.type) {
497 case PERF_TYPE_RAW:
Arnaldo Carvalho de Melo6eef3d92012-06-13 11:53:37 -0300498 perf_evsel__raw_name(evsel, bf, sizeof(bf));
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300499 break;
500
501 case PERF_TYPE_HARDWARE:
Arnaldo Carvalho de Melo7289f832012-06-12 12:34:58 -0300502 perf_evsel__hw_name(evsel, bf, sizeof(bf));
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300503 break;
Arnaldo Carvalho de Melo0b668bc2012-06-11 14:08:07 -0300504
505 case PERF_TYPE_HW_CACHE:
Arnaldo Carvalho de Melo7289f832012-06-12 12:34:58 -0300506 perf_evsel__hw_cache_name(evsel, bf, sizeof(bf));
Arnaldo Carvalho de Melo0b668bc2012-06-11 14:08:07 -0300507 break;
508
Arnaldo Carvalho de Melo335c2f52012-06-11 14:36:20 -0300509 case PERF_TYPE_SOFTWARE:
Arnaldo Carvalho de Melo7289f832012-06-12 12:34:58 -0300510 perf_evsel__sw_name(evsel, bf, sizeof(bf));
Arnaldo Carvalho de Melo335c2f52012-06-11 14:36:20 -0300511 break;
512
Arnaldo Carvalho de Meloa4460832012-06-12 10:29:12 -0300513 case PERF_TYPE_TRACEPOINT:
Arnaldo Carvalho de Melo7289f832012-06-12 12:34:58 -0300514 scnprintf(bf, sizeof(bf), "%s", "unknown tracepoint");
Arnaldo Carvalho de Meloa4460832012-06-12 10:29:12 -0300515 break;
516
Jiri Olsa287e74a2012-06-28 23:18:49 +0200517 case PERF_TYPE_BREAKPOINT:
518 perf_evsel__bp_name(evsel, bf, sizeof(bf));
519 break;
520
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300521 default:
Robert Richterca1b1452012-08-16 21:10:18 +0200522 scnprintf(bf, sizeof(bf), "unknown attr type: %d",
523 evsel->attr.type);
Arnaldo Carvalho de Meloa4460832012-06-12 10:29:12 -0300524 break;
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300525 }
526
Arnaldo Carvalho de Melo7289f832012-06-12 12:34:58 -0300527 evsel->name = strdup(bf);
528
529 return evsel->name ?: "unknown";
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300530}
531
Namhyung Kim717e2632013-01-22 18:09:44 +0900532const char *perf_evsel__group_name(struct perf_evsel *evsel)
533{
534 return evsel->group_name ?: "anon group";
535}
536
537int perf_evsel__group_desc(struct perf_evsel *evsel, char *buf, size_t size)
538{
539 int ret;
540 struct perf_evsel *pos;
541 const char *group_name = perf_evsel__group_name(evsel);
542
543 ret = scnprintf(buf, size, "%s", group_name);
544
545 ret += scnprintf(buf + ret, size - ret, " { %s",
546 perf_evsel__name(evsel));
547
548 for_each_group_member(pos, evsel)
549 ret += scnprintf(buf + ret, size - ret, ", %s",
550 perf_evsel__name(pos));
551
552 ret += scnprintf(buf + ret, size - ret, " }");
553
554 return ret;
555}
556
Jiri Olsa6bedfab2014-03-02 16:56:40 +0100557static void
Kan Liangaad2b212015-01-05 13:23:04 -0500558perf_evsel__config_callgraph(struct perf_evsel *evsel,
Kan Liangc3a6a8c2015-08-04 04:30:20 -0400559 struct record_opts *opts,
560 struct callchain_param *param)
Jiri Olsa6bedfab2014-03-02 16:56:40 +0100561{
562 bool function = perf_evsel__is_function_event(evsel);
563 struct perf_event_attr *attr = &evsel->attr;
564
565 perf_evsel__set_sample_bit(evsel, CALLCHAIN);
566
Kan Liangc3a6a8c2015-08-04 04:30:20 -0400567 if (param->record_mode == CALLCHAIN_LBR) {
Kan Liangaad2b212015-01-05 13:23:04 -0500568 if (!opts->branch_stack) {
569 if (attr->exclude_user) {
570 pr_warning("LBR callstack option is only available "
571 "to get user callchain information. "
572 "Falling back to framepointers.\n");
573 } else {
574 perf_evsel__set_sample_bit(evsel, BRANCH_STACK);
575 attr->branch_sample_type = PERF_SAMPLE_BRANCH_USER |
576 PERF_SAMPLE_BRANCH_CALL_STACK;
577 }
578 } else
579 pr_warning("Cannot use LBR callstack with branch stack. "
580 "Falling back to framepointers.\n");
581 }
582
Kan Liangc3a6a8c2015-08-04 04:30:20 -0400583 if (param->record_mode == CALLCHAIN_DWARF) {
Jiri Olsa6bedfab2014-03-02 16:56:40 +0100584 if (!function) {
585 perf_evsel__set_sample_bit(evsel, REGS_USER);
586 perf_evsel__set_sample_bit(evsel, STACK_USER);
587 attr->sample_regs_user = PERF_REGS_MASK;
Kan Liangc3a6a8c2015-08-04 04:30:20 -0400588 attr->sample_stack_user = param->dump_size;
Jiri Olsa6bedfab2014-03-02 16:56:40 +0100589 attr->exclude_callchain_user = 1;
590 } else {
591 pr_info("Cannot use DWARF unwind for function trace event,"
592 " falling back to framepointers.\n");
593 }
594 }
595
596 if (function) {
597 pr_info("Disabling user space callchains for function trace event.\n");
598 attr->exclude_callchain_user = 1;
599 }
600}
601
Kan Liangd457c962015-08-11 06:30:47 -0400602static void
603perf_evsel__reset_callgraph(struct perf_evsel *evsel,
604 struct callchain_param *param)
605{
606 struct perf_event_attr *attr = &evsel->attr;
607
608 perf_evsel__reset_sample_bit(evsel, CALLCHAIN);
609 if (param->record_mode == CALLCHAIN_LBR) {
610 perf_evsel__reset_sample_bit(evsel, BRANCH_STACK);
611 attr->branch_sample_type &= ~(PERF_SAMPLE_BRANCH_USER |
612 PERF_SAMPLE_BRANCH_CALL_STACK);
613 }
614 if (param->record_mode == CALLCHAIN_DWARF) {
615 perf_evsel__reset_sample_bit(evsel, REGS_USER);
616 perf_evsel__reset_sample_bit(evsel, STACK_USER);
617 }
618}
619
620static void apply_config_terms(struct perf_evsel *evsel,
621 struct record_opts *opts)
Jiri Olsa930a2e22015-07-29 05:42:10 -0400622{
623 struct perf_evsel_config_term *term;
Kan Liang32067712015-08-04 04:30:19 -0400624 struct list_head *config_terms = &evsel->config_terms;
625 struct perf_event_attr *attr = &evsel->attr;
Kan Liangd457c962015-08-11 06:30:47 -0400626 struct callchain_param param;
627 u32 dump_size = 0;
628 char *callgraph_buf = NULL;
629
630 /* callgraph default */
631 param.record_mode = callchain_param.record_mode;
Jiri Olsa930a2e22015-07-29 05:42:10 -0400632
633 list_for_each_entry(term, config_terms, list) {
634 switch (term->type) {
Jiri Olsaee4c7582015-07-29 05:42:11 -0400635 case PERF_EVSEL__CONFIG_TERM_PERIOD:
636 attr->sample_period = term->val.period;
Jiri Olsaab35a7d2015-08-08 19:12:10 +0200637 attr->freq = 0;
Kan Liang32067712015-08-04 04:30:19 -0400638 break;
Namhyung Kim09af2a52015-08-09 15:45:23 +0900639 case PERF_EVSEL__CONFIG_TERM_FREQ:
640 attr->sample_freq = term->val.freq;
641 attr->freq = 1;
642 break;
Kan Liang32067712015-08-04 04:30:19 -0400643 case PERF_EVSEL__CONFIG_TERM_TIME:
644 if (term->val.time)
645 perf_evsel__set_sample_bit(evsel, TIME);
646 else
647 perf_evsel__reset_sample_bit(evsel, TIME);
648 break;
Kan Liangd457c962015-08-11 06:30:47 -0400649 case PERF_EVSEL__CONFIG_TERM_CALLGRAPH:
650 callgraph_buf = term->val.callgraph;
651 break;
652 case PERF_EVSEL__CONFIG_TERM_STACK_USER:
653 dump_size = term->val.stack_user;
654 break;
Jiri Olsa930a2e22015-07-29 05:42:10 -0400655 default:
656 break;
657 }
658 }
Kan Liangd457c962015-08-11 06:30:47 -0400659
660 /* User explicitly set per-event callgraph, clear the old setting and reset. */
661 if ((callgraph_buf != NULL) || (dump_size > 0)) {
662
663 /* parse callgraph parameters */
664 if (callgraph_buf != NULL) {
Kan Liangf9db0d02015-08-11 06:30:48 -0400665 if (!strcmp(callgraph_buf, "no")) {
666 param.enabled = false;
667 param.record_mode = CALLCHAIN_NONE;
668 } else {
669 param.enabled = true;
670 if (parse_callchain_record(callgraph_buf, &param)) {
671 pr_err("per-event callgraph setting for %s failed. "
672 "Apply callgraph global setting for it\n",
673 evsel->name);
674 return;
675 }
Kan Liangd457c962015-08-11 06:30:47 -0400676 }
677 }
678 if (dump_size > 0) {
679 dump_size = round_up(dump_size, sizeof(u64));
680 param.dump_size = dump_size;
681 }
682
683 /* If global callgraph set, clear it */
684 if (callchain_param.enabled)
685 perf_evsel__reset_callgraph(evsel, &callchain_param);
686
687 /* set perf-event callgraph */
688 if (param.enabled)
689 perf_evsel__config_callgraph(evsel, opts, &param);
690 }
Jiri Olsa930a2e22015-07-29 05:42:10 -0400691}
692
Jiri Olsa774cb492012-11-12 18:34:01 +0100693/*
694 * The enable_on_exec/disabled value strategy:
695 *
696 * 1) For any type of traced program:
697 * - all independent events and group leaders are disabled
698 * - all group members are enabled
699 *
700 * Group members are ruled by group leaders. They need to
701 * be enabled, because the group scheduling relies on that.
702 *
703 * 2) For traced programs executed by perf:
704 * - all independent events and group leaders have
705 * enable_on_exec set
706 * - we don't specifically enable or disable any event during
707 * the record command
708 *
709 * Independent events and group leaders are initially disabled
710 * and get enabled by exec. Group members are ruled by group
711 * leaders as stated in 1).
712 *
713 * 3) For traced programs attached by perf (pid/tid):
714 * - we specifically enable or disable all events during
715 * the record command
716 *
717 * When attaching events to already running traced we
718 * enable/disable events specifically, as there's no
719 * initial traced exec call.
720 */
Arnaldo Carvalho de Melob4006792013-12-19 14:43:45 -0300721void perf_evsel__config(struct perf_evsel *evsel, struct record_opts *opts)
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200722{
Jiri Olsa3c176312012-10-10 17:39:03 +0200723 struct perf_evsel *leader = evsel->leader;
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200724 struct perf_event_attr *attr = &evsel->attr;
Adrian Hunter60b08962014-07-31 09:00:52 +0300725 int track = evsel->tracking;
Adrian Hunter3aa59392013-11-15 15:52:29 +0200726 bool per_cpu = opts->target.default_per_cpu && !opts->target.per_thread;
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200727
Arnaldo Carvalho de Melo594ac612012-12-13 13:13:07 -0300728 attr->sample_id_all = perf_missing_features.sample_id_all ? 0 : 1;
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200729 attr->inherit = !opts->no_inherit;
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200730
Arnaldo Carvalho de Melo7be5ebe2012-12-10 14:53:43 -0300731 perf_evsel__set_sample_bit(evsel, IP);
732 perf_evsel__set_sample_bit(evsel, TID);
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200733
Jiri Olsa3c176312012-10-10 17:39:03 +0200734 if (evsel->sample_read) {
735 perf_evsel__set_sample_bit(evsel, READ);
736
737 /*
738 * We need ID even in case of single event, because
739 * PERF_SAMPLE_READ process ID specific data.
740 */
Adrian Hunter75562572013-08-27 11:23:09 +0300741 perf_evsel__set_sample_id(evsel, false);
Jiri Olsa3c176312012-10-10 17:39:03 +0200742
743 /*
744 * Apply group format only if we belong to group
745 * with more than one members.
746 */
747 if (leader->nr_members > 1) {
748 attr->read_format |= PERF_FORMAT_GROUP;
749 attr->inherit = 0;
750 }
751 }
752
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200753 /*
Namhyung Kim17314e22014-06-09 14:43:37 +0900754 * We default some events to have a default interval. But keep
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200755 * it a weak assumption overridable by the user.
756 */
Namhyung Kim17314e22014-06-09 14:43:37 +0900757 if (!attr->sample_period || (opts->user_freq != UINT_MAX ||
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200758 opts->user_interval != ULLONG_MAX)) {
759 if (opts->freq) {
Arnaldo Carvalho de Melo7be5ebe2012-12-10 14:53:43 -0300760 perf_evsel__set_sample_bit(evsel, PERIOD);
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200761 attr->freq = 1;
762 attr->sample_freq = opts->freq;
763 } else {
764 attr->sample_period = opts->default_interval;
765 }
766 }
767
Jiri Olsa3c176312012-10-10 17:39:03 +0200768 /*
769 * Disable sampling for all group members other
770 * than leader in case leader 'leads' the sampling.
771 */
772 if ((leader != evsel) && leader->sample_read) {
773 attr->sample_freq = 0;
774 attr->sample_period = 0;
775 }
776
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200777 if (opts->no_samples)
778 attr->sample_freq = 0;
779
780 if (opts->inherit_stat)
781 attr->inherit_stat = 1;
782
783 if (opts->sample_address) {
Arnaldo Carvalho de Melo7be5ebe2012-12-10 14:53:43 -0300784 perf_evsel__set_sample_bit(evsel, ADDR);
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200785 attr->mmap_data = track;
786 }
787
Jiri Olsaf1403732014-11-13 18:21:03 +0100788 /*
789 * We don't allow user space callchains for function trace
790 * event, due to issues with page faults while tracing page
791 * fault handler and its overall trickiness nature.
792 */
793 if (perf_evsel__is_function_event(evsel))
794 evsel->attr.exclude_callchain_user = 1;
795
Namhyung Kim72a128a2014-09-23 10:01:41 +0900796 if (callchain_param.enabled && !evsel->no_aux_samples)
Kan Liangc3a6a8c2015-08-04 04:30:20 -0400797 perf_evsel__config_callgraph(evsel, opts, &callchain_param);
Jiri Olsa26d33022012-08-07 15:20:47 +0200798
Stephane Eranian6a21c0b2014-09-24 13:48:39 +0200799 if (opts->sample_intr_regs) {
Stephane Eranianbcc84ec2015-08-31 18:41:12 +0200800 attr->sample_regs_intr = opts->sample_intr_regs;
Stephane Eranian6a21c0b2014-09-24 13:48:39 +0200801 perf_evsel__set_sample_bit(evsel, REGS_INTR);
802 }
803
Adrian Hunter3aa59392013-11-15 15:52:29 +0200804 if (target__has_cpu(&opts->target))
Arnaldo Carvalho de Melo7be5ebe2012-12-10 14:53:43 -0300805 perf_evsel__set_sample_bit(evsel, CPU);
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200806
Andrew Vagin3e76ac72011-12-20 17:32:45 +0300807 if (opts->period)
Arnaldo Carvalho de Melo7be5ebe2012-12-10 14:53:43 -0300808 perf_evsel__set_sample_bit(evsel, PERIOD);
Andrew Vagin3e76ac72011-12-20 17:32:45 +0300809
Andi Kleen8affc2b2014-07-31 14:45:04 +0800810 /*
811 * When the user explicitely disabled time don't force it here.
812 */
813 if (opts->sample_time &&
814 (!perf_missing_features.sample_id_all &&
Adrian Hunter3abebc52015-07-06 14:51:01 +0300815 (!opts->no_inherit || target__has_cpu(&opts->target) || per_cpu ||
816 opts->sample_time_set)))
Arnaldo Carvalho de Melo7be5ebe2012-12-10 14:53:43 -0300817 perf_evsel__set_sample_bit(evsel, TIME);
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200818
Adrian Hunter6ff1ce72014-07-14 13:02:56 +0300819 if (opts->raw_samples && !evsel->no_aux_samples) {
Arnaldo Carvalho de Melo7be5ebe2012-12-10 14:53:43 -0300820 perf_evsel__set_sample_bit(evsel, TIME);
821 perf_evsel__set_sample_bit(evsel, RAW);
822 perf_evsel__set_sample_bit(evsel, CPU);
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200823 }
824
Stephane Eranianccf49bf2013-01-24 16:10:37 +0100825 if (opts->sample_address)
Adrian Hunter1e7ed5e2013-11-01 15:51:35 +0200826 perf_evsel__set_sample_bit(evsel, DATA_SRC);
Stephane Eranianccf49bf2013-01-24 16:10:37 +0100827
Arnaldo Carvalho de Melo509051e2014-01-14 17:52:14 -0300828 if (opts->no_buffering) {
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200829 attr->watermark = 0;
830 attr->wakeup_events = 1;
831 }
Adrian Hunter6ff1ce72014-07-14 13:02:56 +0300832 if (opts->branch_stack && !evsel->no_aux_samples) {
Arnaldo Carvalho de Melo7be5ebe2012-12-10 14:53:43 -0300833 perf_evsel__set_sample_bit(evsel, BRANCH_STACK);
Roberto Agostino Vitillobdfebd82012-02-09 23:21:02 +0100834 attr->branch_sample_type = opts->branch_stack;
835 }
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200836
Andi Kleen05484292013-01-24 16:10:29 +0100837 if (opts->sample_weight)
Adrian Hunter1e7ed5e2013-11-01 15:51:35 +0200838 perf_evsel__set_sample_bit(evsel, WEIGHT);
Andi Kleen05484292013-01-24 16:10:29 +0100839
Namhyung Kim62e503b2015-01-29 17:06:46 +0900840 attr->task = track;
Stephane Eranian5c5e8542013-08-21 12:10:25 +0200841 attr->mmap = track;
Don Zickusa5a5ba72014-05-30 10:49:42 -0400842 attr->mmap2 = track && !perf_missing_features.mmap2;
Stephane Eranian5c5e8542013-08-21 12:10:25 +0200843 attr->comm = track;
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200844
Adrian Hunterb757bb02015-07-21 12:44:04 +0300845 if (opts->record_switch_events)
846 attr->context_switch = track;
847
Andi Kleen475eeab2013-09-20 07:40:43 -0700848 if (opts->sample_transaction)
Adrian Hunter1e7ed5e2013-11-01 15:51:35 +0200849 perf_evsel__set_sample_bit(evsel, TRANSACTION);
Andi Kleen475eeab2013-09-20 07:40:43 -0700850
Andi Kleen85c273d2015-02-24 15:13:40 -0800851 if (opts->running_time) {
852 evsel->attr.read_format |=
853 PERF_FORMAT_TOTAL_TIME_ENABLED |
854 PERF_FORMAT_TOTAL_TIME_RUNNING;
855 }
856
Jiri Olsa774cb492012-11-12 18:34:01 +0100857 /*
858 * XXX see the function comment above
859 *
860 * Disabling only independent events or group leaders,
861 * keeping group members enabled.
862 */
Namhyung Kim823254e2012-11-29 15:38:30 +0900863 if (perf_evsel__is_group_leader(evsel))
Jiri Olsa774cb492012-11-12 18:34:01 +0100864 attr->disabled = 1;
865
866 /*
867 * Setting enable_on_exec for independent events and
868 * group leaders for traced executed by perf.
869 */
Andi Kleen6619a532014-01-11 13:38:27 -0800870 if (target__none(&opts->target) && perf_evsel__is_group_leader(evsel) &&
871 !opts->initial_delay)
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200872 attr->enable_on_exec = 1;
Adrian Hunter2afd2bc2014-07-14 13:02:57 +0300873
874 if (evsel->immediate) {
875 attr->disabled = 0;
876 attr->enable_on_exec = 0;
877 }
Peter Zijlstra814c8c32015-03-31 00:19:31 +0200878
879 clockid = opts->clockid;
880 if (opts->use_clockid) {
881 attr->use_clockid = 1;
882 attr->clockid = opts->clockid;
883 }
Jiri Olsa930a2e22015-07-29 05:42:10 -0400884
885 /*
886 * Apply event specific term settings,
887 * it overloads any global configuration.
888 */
Kan Liangd457c962015-08-11 06:30:47 -0400889 apply_config_terms(evsel, opts);
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200890}
891
Arnaldo Carvalho de Melo88858462014-10-13 13:30:27 -0300892static int perf_evsel__alloc_fd(struct perf_evsel *evsel, int ncpus, int nthreads)
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200893{
David Ahern4af4c952011-05-27 09:58:34 -0600894 int cpu, thread;
Adrian Hunterbf8e8f42014-07-31 09:00:51 +0300895
896 if (evsel->system_wide)
897 nthreads = 1;
898
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200899 evsel->fd = xyarray__new(ncpus, nthreads, sizeof(int));
David Ahern4af4c952011-05-27 09:58:34 -0600900
901 if (evsel->fd) {
902 for (cpu = 0; cpu < ncpus; cpu++) {
903 for (thread = 0; thread < nthreads; thread++) {
904 FD(evsel, cpu, thread) = -1;
905 }
906 }
907 }
908
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200909 return evsel->fd != NULL ? 0 : -ENOMEM;
910}
911
Andi Kleene2407be2013-08-02 17:41:10 -0700912static int perf_evsel__run_ioctl(struct perf_evsel *evsel, int ncpus, int nthreads,
913 int ioc, void *arg)
Arnaldo Carvalho de Melo745cefc2012-09-26 15:07:39 -0300914{
915 int cpu, thread;
916
Adrian Hunterbf8e8f42014-07-31 09:00:51 +0300917 if (evsel->system_wide)
918 nthreads = 1;
919
Arnaldo Carvalho de Melo745cefc2012-09-26 15:07:39 -0300920 for (cpu = 0; cpu < ncpus; cpu++) {
921 for (thread = 0; thread < nthreads; thread++) {
922 int fd = FD(evsel, cpu, thread),
Andi Kleene2407be2013-08-02 17:41:10 -0700923 err = ioctl(fd, ioc, arg);
Arnaldo Carvalho de Melo745cefc2012-09-26 15:07:39 -0300924
925 if (err)
926 return err;
927 }
928 }
929
930 return 0;
931}
932
Arnaldo Carvalho de Melof47805a2015-07-03 15:53:49 -0300933int perf_evsel__apply_filter(struct perf_evsel *evsel, int ncpus, int nthreads,
934 const char *filter)
Andi Kleene2407be2013-08-02 17:41:10 -0700935{
936 return perf_evsel__run_ioctl(evsel, ncpus, nthreads,
937 PERF_EVENT_IOC_SET_FILTER,
938 (void *)filter);
939}
940
Arnaldo Carvalho de Melo12467ae2015-07-03 17:05:50 -0300941int perf_evsel__set_filter(struct perf_evsel *evsel, const char *filter)
942{
943 char *new_filter = strdup(filter);
944
945 if (new_filter != NULL) {
946 free(evsel->filter);
947 evsel->filter = new_filter;
948 return 0;
949 }
950
951 return -1;
952}
953
Arnaldo Carvalho de Melo64ec84f2015-07-04 12:19:13 -0300954int perf_evsel__append_filter(struct perf_evsel *evsel,
955 const char *op, const char *filter)
956{
957 char *new_filter;
958
959 if (evsel->filter == NULL)
960 return perf_evsel__set_filter(evsel, filter);
961
962 if (asprintf(&new_filter,"(%s) %s (%s)", evsel->filter, op, filter) > 0) {
963 free(evsel->filter);
964 evsel->filter = new_filter;
965 return 0;
966 }
967
968 return -1;
969}
970
Andi Kleene2407be2013-08-02 17:41:10 -0700971int perf_evsel__enable(struct perf_evsel *evsel, int ncpus, int nthreads)
972{
973 return perf_evsel__run_ioctl(evsel, ncpus, nthreads,
974 PERF_EVENT_IOC_ENABLE,
975 0);
976}
977
Arnaldo Carvalho de Melo70db7532011-01-12 22:39:13 -0200978int perf_evsel__alloc_id(struct perf_evsel *evsel, int ncpus, int nthreads)
979{
Vineet Gupta8d9cbd82015-01-13 19:13:23 +0530980 if (ncpus == 0 || nthreads == 0)
981 return 0;
982
Adrian Hunterbf8e8f42014-07-31 09:00:51 +0300983 if (evsel->system_wide)
984 nthreads = 1;
985
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -0300986 evsel->sample_id = xyarray__new(ncpus, nthreads, sizeof(struct perf_sample_id));
987 if (evsel->sample_id == NULL)
988 return -ENOMEM;
989
990 evsel->id = zalloc(ncpus * nthreads * sizeof(u64));
991 if (evsel->id == NULL) {
992 xyarray__delete(evsel->sample_id);
993 evsel->sample_id = NULL;
994 return -ENOMEM;
995 }
996
997 return 0;
Arnaldo Carvalho de Melo70db7532011-01-12 22:39:13 -0200998}
999
Arnaldo Carvalho de Melo88858462014-10-13 13:30:27 -03001000static void perf_evsel__free_fd(struct perf_evsel *evsel)
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -02001001{
1002 xyarray__delete(evsel->fd);
1003 evsel->fd = NULL;
1004}
1005
Arnaldo Carvalho de Melo88858462014-10-13 13:30:27 -03001006static void perf_evsel__free_id(struct perf_evsel *evsel)
Arnaldo Carvalho de Melo70db7532011-01-12 22:39:13 -02001007{
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -03001008 xyarray__delete(evsel->sample_id);
1009 evsel->sample_id = NULL;
Arnaldo Carvalho de Melo04662522013-12-26 17:41:15 -03001010 zfree(&evsel->id);
Arnaldo Carvalho de Melo70db7532011-01-12 22:39:13 -02001011}
1012
Jiri Olsa930a2e22015-07-29 05:42:10 -04001013static void perf_evsel__free_config_terms(struct perf_evsel *evsel)
1014{
1015 struct perf_evsel_config_term *term, *h;
1016
1017 list_for_each_entry_safe(term, h, &evsel->config_terms, list) {
1018 list_del(&term->list);
1019 free(term);
1020 }
1021}
1022
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -02001023void perf_evsel__close_fd(struct perf_evsel *evsel, int ncpus, int nthreads)
1024{
1025 int cpu, thread;
1026
Adrian Hunterbf8e8f42014-07-31 09:00:51 +03001027 if (evsel->system_wide)
1028 nthreads = 1;
1029
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -02001030 for (cpu = 0; cpu < ncpus; cpu++)
1031 for (thread = 0; thread < nthreads; ++thread) {
1032 close(FD(evsel, cpu, thread));
1033 FD(evsel, cpu, thread) = -1;
1034 }
1035}
1036
Arnaldo Carvalho de Meloef1d1af2011-01-18 21:41:45 -02001037void perf_evsel__exit(struct perf_evsel *evsel)
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -02001038{
1039 assert(list_empty(&evsel->node));
Arnaldo Carvalho de Melod49e4692015-08-27 08:07:40 -04001040 assert(evsel->evlist == NULL);
Namhyung Kim736b05a2013-03-15 14:48:49 +09001041 perf_evsel__free_fd(evsel);
1042 perf_evsel__free_id(evsel);
Jiri Olsa930a2e22015-07-29 05:42:10 -04001043 perf_evsel__free_config_terms(evsel);
Arnaldo Carvalho de Melo597e48c2014-10-16 13:25:01 -03001044 close_cgroup(evsel->cgrp);
Jiri Olsaf30a79b2015-06-23 00:36:04 +02001045 cpu_map__put(evsel->cpus);
Adrian Hunterfce4d292015-09-08 10:58:55 +03001046 cpu_map__put(evsel->own_cpus);
Jiri Olsa578e91e2015-06-23 00:36:07 +02001047 thread_map__put(evsel->threads);
Arnaldo Carvalho de Melo597e48c2014-10-16 13:25:01 -03001048 zfree(&evsel->group_name);
Arnaldo Carvalho de Melo597e48c2014-10-16 13:25:01 -03001049 zfree(&evsel->name);
Arnaldo Carvalho de Meloce8ccff2014-10-09 15:29:51 -03001050 perf_evsel__object.fini(evsel);
Arnaldo Carvalho de Meloef1d1af2011-01-18 21:41:45 -02001051}
1052
1053void perf_evsel__delete(struct perf_evsel *evsel)
1054{
1055 perf_evsel__exit(evsel);
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -02001056 free(evsel);
1057}
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -02001058
Jiri Olsaa6fa0032015-06-26 11:29:11 +02001059void perf_evsel__compute_deltas(struct perf_evsel *evsel, int cpu, int thread,
Jiri Olsa857a94a2014-11-21 10:31:05 +01001060 struct perf_counts_values *count)
Stephane Eranianc7a79c42013-01-29 12:47:43 +01001061{
1062 struct perf_counts_values tmp;
1063
1064 if (!evsel->prev_raw_counts)
1065 return;
1066
1067 if (cpu == -1) {
1068 tmp = evsel->prev_raw_counts->aggr;
1069 evsel->prev_raw_counts->aggr = *count;
1070 } else {
Jiri Olsaa6fa0032015-06-26 11:29:11 +02001071 tmp = *perf_counts(evsel->prev_raw_counts, cpu, thread);
1072 *perf_counts(evsel->prev_raw_counts, cpu, thread) = *count;
Stephane Eranianc7a79c42013-01-29 12:47:43 +01001073 }
1074
1075 count->val = count->val - tmp.val;
1076 count->ena = count->ena - tmp.ena;
1077 count->run = count->run - tmp.run;
1078}
1079
Jiri Olsa13112bb2014-11-21 10:31:06 +01001080void perf_counts_values__scale(struct perf_counts_values *count,
1081 bool scale, s8 *pscaled)
1082{
1083 s8 scaled = 0;
1084
1085 if (scale) {
1086 if (count->run == 0) {
1087 scaled = -1;
1088 count->val = 0;
1089 } else if (count->run < count->ena) {
1090 scaled = 1;
1091 count->val = (u64)((double) count->val * count->ena / count->run + 0.5);
1092 }
1093 } else
1094 count->ena = count->run = 0;
1095
1096 if (pscaled)
1097 *pscaled = scaled;
1098}
1099
Jiri Olsaf99f47192015-06-26 11:29:18 +02001100int perf_evsel__read(struct perf_evsel *evsel, int cpu, int thread,
1101 struct perf_counts_values *count)
1102{
1103 memset(count, 0, sizeof(*count));
1104
1105 if (FD(evsel, cpu, thread) < 0)
1106 return -EINVAL;
1107
1108 if (readn(FD(evsel, cpu, thread), count, sizeof(*count)) < 0)
1109 return -errno;
1110
1111 return 0;
1112}
1113
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -02001114int __perf_evsel__read_on_cpu(struct perf_evsel *evsel,
1115 int cpu, int thread, bool scale)
1116{
1117 struct perf_counts_values count;
1118 size_t nv = scale ? 3 : 1;
1119
1120 if (FD(evsel, cpu, thread) < 0)
1121 return -EINVAL;
1122
Jiri Olsaa6fa0032015-06-26 11:29:11 +02001123 if (evsel->counts == NULL && perf_evsel__alloc_counts(evsel, cpu + 1, thread + 1) < 0)
Arnaldo Carvalho de Melo4eed11d2011-01-04 00:13:17 -02001124 return -ENOMEM;
1125
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -02001126 if (readn(FD(evsel, cpu, thread), &count, nv * sizeof(u64)) < 0)
1127 return -errno;
1128
Jiri Olsaa6fa0032015-06-26 11:29:11 +02001129 perf_evsel__compute_deltas(evsel, cpu, thread, &count);
Jiri Olsa13112bb2014-11-21 10:31:06 +01001130 perf_counts_values__scale(&count, scale, NULL);
Jiri Olsaa6fa0032015-06-26 11:29:11 +02001131 *perf_counts(evsel->counts, cpu, thread) = count;
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -02001132 return 0;
1133}
1134
Jiri Olsa6a4bb042012-08-08 12:22:36 +02001135static int get_group_fd(struct perf_evsel *evsel, int cpu, int thread)
1136{
1137 struct perf_evsel *leader = evsel->leader;
1138 int fd;
1139
Namhyung Kim823254e2012-11-29 15:38:30 +09001140 if (perf_evsel__is_group_leader(evsel))
Jiri Olsa6a4bb042012-08-08 12:22:36 +02001141 return -1;
1142
1143 /*
1144 * Leader must be already processed/open,
1145 * if not it's a bug.
1146 */
1147 BUG_ON(!leader->fd);
1148
1149 fd = FD(leader, cpu, thread);
1150 BUG_ON(fd == -1);
1151
1152 return fd;
1153}
1154
Peter Zijlstra2c5e8c52015-04-07 11:09:54 +02001155struct bit_names {
1156 int bit;
1157 const char *name;
1158};
Adrian Huntere3e1a542013-08-14 15:48:24 +03001159
Peter Zijlstra2c5e8c52015-04-07 11:09:54 +02001160static void __p_bits(char *buf, size_t size, u64 value, struct bit_names *bits)
Adrian Huntere3e1a542013-08-14 15:48:24 +03001161{
Peter Zijlstra2c5e8c52015-04-07 11:09:54 +02001162 bool first_bit = true;
1163 int i = 0;
Adrian Huntere3e1a542013-08-14 15:48:24 +03001164
Peter Zijlstra2c5e8c52015-04-07 11:09:54 +02001165 do {
1166 if (value & bits[i].bit) {
1167 buf += scnprintf(buf, size, "%s%s", first_bit ? "" : "|", bits[i].name);
1168 first_bit = false;
1169 }
1170 } while (bits[++i].name != NULL);
1171}
Adrian Huntere3e1a542013-08-14 15:48:24 +03001172
Peter Zijlstra2c5e8c52015-04-07 11:09:54 +02001173static void __p_sample_type(char *buf, size_t size, u64 value)
1174{
1175#define bit_name(n) { PERF_SAMPLE_##n, #n }
1176 struct bit_names bits[] = {
1177 bit_name(IP), bit_name(TID), bit_name(TIME), bit_name(ADDR),
1178 bit_name(READ), bit_name(CALLCHAIN), bit_name(ID), bit_name(CPU),
1179 bit_name(PERIOD), bit_name(STREAM_ID), bit_name(RAW),
1180 bit_name(BRANCH_STACK), bit_name(REGS_USER), bit_name(STACK_USER),
Jiri Olsa844225922015-10-05 20:06:02 +02001181 bit_name(IDENTIFIER), bit_name(REGS_INTR), bit_name(DATA_SRC),
Peter Zijlstra2c5e8c52015-04-07 11:09:54 +02001182 { .name = NULL, }
1183 };
1184#undef bit_name
1185 __p_bits(buf, size, value, bits);
1186}
Adrian Huntere3e1a542013-08-14 15:48:24 +03001187
Peter Zijlstra2c5e8c52015-04-07 11:09:54 +02001188static void __p_read_format(char *buf, size_t size, u64 value)
1189{
1190#define bit_name(n) { PERF_FORMAT_##n, #n }
1191 struct bit_names bits[] = {
1192 bit_name(TOTAL_TIME_ENABLED), bit_name(TOTAL_TIME_RUNNING),
1193 bit_name(ID), bit_name(GROUP),
1194 { .name = NULL, }
1195 };
1196#undef bit_name
1197 __p_bits(buf, size, value, bits);
1198}
Peter Zijlstra814c8c32015-03-31 00:19:31 +02001199
Peter Zijlstra2c5e8c52015-04-07 11:09:54 +02001200#define BUF_SIZE 1024
Adrian Huntere3e1a542013-08-14 15:48:24 +03001201
Adrian Hunter7310aed2015-06-11 15:51:04 +03001202#define p_hex(val) snprintf(buf, BUF_SIZE, "%#"PRIx64, (uint64_t)(val))
Peter Zijlstra2c5e8c52015-04-07 11:09:54 +02001203#define p_unsigned(val) snprintf(buf, BUF_SIZE, "%"PRIu64, (uint64_t)(val))
1204#define p_signed(val) snprintf(buf, BUF_SIZE, "%"PRId64, (int64_t)(val))
1205#define p_sample_type(val) __p_sample_type(buf, BUF_SIZE, val)
1206#define p_read_format(val) __p_read_format(buf, BUF_SIZE, val)
Adrian Huntere3e1a542013-08-14 15:48:24 +03001207
Peter Zijlstra2c5e8c52015-04-07 11:09:54 +02001208#define PRINT_ATTRn(_n, _f, _p) \
1209do { \
1210 if (attr->_f) { \
1211 _p(attr->_f); \
1212 ret += attr__fprintf(fp, _n, buf, priv);\
1213 } \
1214} while (0)
1215
1216#define PRINT_ATTRf(_f, _p) PRINT_ATTRn(#_f, _f, _p)
1217
1218int perf_event_attr__fprintf(FILE *fp, struct perf_event_attr *attr,
1219 attr__fprintf_f attr__fprintf, void *priv)
1220{
1221 char buf[BUF_SIZE];
1222 int ret = 0;
1223
1224 PRINT_ATTRf(type, p_unsigned);
1225 PRINT_ATTRf(size, p_unsigned);
1226 PRINT_ATTRf(config, p_hex);
1227 PRINT_ATTRn("{ sample_period, sample_freq }", sample_period, p_unsigned);
1228 PRINT_ATTRf(sample_type, p_sample_type);
1229 PRINT_ATTRf(read_format, p_read_format);
1230
1231 PRINT_ATTRf(disabled, p_unsigned);
1232 PRINT_ATTRf(inherit, p_unsigned);
1233 PRINT_ATTRf(pinned, p_unsigned);
1234 PRINT_ATTRf(exclusive, p_unsigned);
1235 PRINT_ATTRf(exclude_user, p_unsigned);
1236 PRINT_ATTRf(exclude_kernel, p_unsigned);
1237 PRINT_ATTRf(exclude_hv, p_unsigned);
1238 PRINT_ATTRf(exclude_idle, p_unsigned);
1239 PRINT_ATTRf(mmap, p_unsigned);
1240 PRINT_ATTRf(comm, p_unsigned);
1241 PRINT_ATTRf(freq, p_unsigned);
1242 PRINT_ATTRf(inherit_stat, p_unsigned);
1243 PRINT_ATTRf(enable_on_exec, p_unsigned);
1244 PRINT_ATTRf(task, p_unsigned);
1245 PRINT_ATTRf(watermark, p_unsigned);
1246 PRINT_ATTRf(precise_ip, p_unsigned);
1247 PRINT_ATTRf(mmap_data, p_unsigned);
1248 PRINT_ATTRf(sample_id_all, p_unsigned);
1249 PRINT_ATTRf(exclude_host, p_unsigned);
1250 PRINT_ATTRf(exclude_guest, p_unsigned);
1251 PRINT_ATTRf(exclude_callchain_kernel, p_unsigned);
1252 PRINT_ATTRf(exclude_callchain_user, p_unsigned);
1253 PRINT_ATTRf(mmap2, p_unsigned);
1254 PRINT_ATTRf(comm_exec, p_unsigned);
1255 PRINT_ATTRf(use_clockid, p_unsigned);
Adrian Hunter02860392015-07-21 12:44:03 +03001256 PRINT_ATTRf(context_switch, p_unsigned);
Peter Zijlstra2c5e8c52015-04-07 11:09:54 +02001257
1258 PRINT_ATTRn("{ wakeup_events, wakeup_watermark }", wakeup_events, p_unsigned);
1259 PRINT_ATTRf(bp_type, p_unsigned);
1260 PRINT_ATTRn("{ bp_addr, config1 }", bp_addr, p_hex);
1261 PRINT_ATTRn("{ bp_len, config2 }", bp_len, p_hex);
1262 PRINT_ATTRf(sample_regs_user, p_hex);
1263 PRINT_ATTRf(sample_stack_user, p_unsigned);
1264 PRINT_ATTRf(clockid, p_signed);
1265 PRINT_ATTRf(sample_regs_intr, p_hex);
Adrian Hunter70d73de2015-04-09 18:54:06 +03001266 PRINT_ATTRf(aux_watermark, p_unsigned);
Adrian Huntere3e1a542013-08-14 15:48:24 +03001267
1268 return ret;
1269}
1270
Peter Zijlstra2c5e8c52015-04-07 11:09:54 +02001271static int __open_attr__fprintf(FILE *fp, const char *name, const char *val,
1272 void *priv __attribute__((unused)))
1273{
1274 return fprintf(fp, " %-32s %s\n", name, val);
1275}
1276
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -02001277static int __perf_evsel__open(struct perf_evsel *evsel, struct cpu_map *cpus,
Jiri Olsa6a4bb042012-08-08 12:22:36 +02001278 struct thread_map *threads)
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -02001279{
Adrian Hunterbf8e8f42014-07-31 09:00:51 +03001280 int cpu, thread, nthreads;
Yann Droneaud57480d22014-06-30 22:28:47 +02001281 unsigned long flags = PERF_FLAG_FD_CLOEXEC;
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -02001282 int pid = -1, err;
Andi Kleenbec19672013-08-04 19:41:26 -07001283 enum { NO_CHANGE, SET_TO_MAX, INCREASED_MAX } set_rlimit = NO_CHANGE;
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -02001284
Adrian Hunterbf8e8f42014-07-31 09:00:51 +03001285 if (evsel->system_wide)
1286 nthreads = 1;
1287 else
1288 nthreads = threads->nr;
1289
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -02001290 if (evsel->fd == NULL &&
Adrian Hunterbf8e8f42014-07-31 09:00:51 +03001291 perf_evsel__alloc_fd(evsel, cpus->nr, nthreads) < 0)
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -02001292 return -ENOMEM;
Arnaldo Carvalho de Melo4eed11d2011-01-04 00:13:17 -02001293
Stephane Eranian023695d2011-02-14 11:20:01 +02001294 if (evsel->cgrp) {
Yann Droneaud57480d22014-06-30 22:28:47 +02001295 flags |= PERF_FLAG_PID_CGROUP;
Stephane Eranian023695d2011-02-14 11:20:01 +02001296 pid = evsel->cgrp->fd;
1297 }
1298
Arnaldo Carvalho de Melo594ac612012-12-13 13:13:07 -03001299fallback_missing_features:
Peter Zijlstra814c8c32015-03-31 00:19:31 +02001300 if (perf_missing_features.clockid_wrong)
1301 evsel->attr.clockid = CLOCK_MONOTONIC; /* should always work */
1302 if (perf_missing_features.clockid) {
1303 evsel->attr.use_clockid = 0;
1304 evsel->attr.clockid = 0;
1305 }
Yann Droneaud57480d22014-06-30 22:28:47 +02001306 if (perf_missing_features.cloexec)
1307 flags &= ~(unsigned long)PERF_FLAG_FD_CLOEXEC;
Stephane Eranian5c5e8542013-08-21 12:10:25 +02001308 if (perf_missing_features.mmap2)
1309 evsel->attr.mmap2 = 0;
Arnaldo Carvalho de Melo594ac612012-12-13 13:13:07 -03001310 if (perf_missing_features.exclude_guest)
1311 evsel->attr.exclude_guest = evsel->attr.exclude_host = 0;
1312retry_sample_id:
1313 if (perf_missing_features.sample_id_all)
1314 evsel->attr.sample_id_all = 0;
1315
Peter Zijlstra2c5e8c52015-04-07 11:09:54 +02001316 if (verbose >= 2) {
1317 fprintf(stderr, "%.60s\n", graph_dotted_line);
1318 fprintf(stderr, "perf_event_attr:\n");
1319 perf_event_attr__fprintf(stderr, &evsel->attr, __open_attr__fprintf, NULL);
1320 fprintf(stderr, "%.60s\n", graph_dotted_line);
1321 }
Adrian Huntere3e1a542013-08-14 15:48:24 +03001322
Arnaldo Carvalho de Melo86bd5e82011-01-03 23:09:46 -02001323 for (cpu = 0; cpu < cpus->nr; cpu++) {
Arnaldo Carvalho de Melo9d04f172011-01-12 00:08:18 -02001324
Adrian Hunterbf8e8f42014-07-31 09:00:51 +03001325 for (thread = 0; thread < nthreads; thread++) {
Jiri Olsa6a4bb042012-08-08 12:22:36 +02001326 int group_fd;
Stephane Eranian023695d2011-02-14 11:20:01 +02001327
Adrian Hunterbf8e8f42014-07-31 09:00:51 +03001328 if (!evsel->cgrp && !evsel->system_wide)
Jiri Olsae13798c2015-06-23 00:36:02 +02001329 pid = thread_map__pid(threads, thread);
Stephane Eranian023695d2011-02-14 11:20:01 +02001330
Jiri Olsa6a4bb042012-08-08 12:22:36 +02001331 group_fd = get_group_fd(evsel, cpu, thread);
Andi Kleenbec19672013-08-04 19:41:26 -07001332retry_open:
Ramkumar Ramachandraa33f6ef2014-03-18 15:10:42 -04001333 pr_debug2("sys_perf_event_open: pid %d cpu %d group_fd %d flags %#lx\n",
Adrian Huntere3e1a542013-08-14 15:48:24 +03001334 pid, cpus->map[cpu], group_fd, flags);
1335
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -02001336 FD(evsel, cpu, thread) = sys_perf_event_open(&evsel->attr,
Stephane Eranian023695d2011-02-14 11:20:01 +02001337 pid,
Arnaldo Carvalho de Melof08199d2011-01-11 23:42:19 -02001338 cpus->map[cpu],
Stephane Eranian023695d2011-02-14 11:20:01 +02001339 group_fd, flags);
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -02001340 if (FD(evsel, cpu, thread) < 0) {
1341 err = -errno;
Ramkumar Ramachandraa33f6ef2014-03-18 15:10:42 -04001342 pr_debug2("sys_perf_event_open failed, error %d\n",
Adrian Hunterf852fd62013-11-01 15:51:29 +02001343 err);
Arnaldo Carvalho de Melo594ac612012-12-13 13:13:07 -03001344 goto try_fallback;
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -02001345 }
Andi Kleenbec19672013-08-04 19:41:26 -07001346 set_rlimit = NO_CHANGE;
Peter Zijlstra814c8c32015-03-31 00:19:31 +02001347
1348 /*
1349 * If we succeeded but had to kill clockid, fail and
1350 * have perf_evsel__open_strerror() print us a nice
1351 * error.
1352 */
1353 if (perf_missing_features.clockid ||
1354 perf_missing_features.clockid_wrong) {
1355 err = -EINVAL;
1356 goto out_close;
1357 }
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -02001358 }
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -02001359 }
1360
1361 return 0;
1362
Arnaldo Carvalho de Melo594ac612012-12-13 13:13:07 -03001363try_fallback:
Andi Kleenbec19672013-08-04 19:41:26 -07001364 /*
1365 * perf stat needs between 5 and 22 fds per CPU. When we run out
1366 * of them try to increase the limits.
1367 */
1368 if (err == -EMFILE && set_rlimit < INCREASED_MAX) {
1369 struct rlimit l;
1370 int old_errno = errno;
1371
1372 if (getrlimit(RLIMIT_NOFILE, &l) == 0) {
1373 if (set_rlimit == NO_CHANGE)
1374 l.rlim_cur = l.rlim_max;
1375 else {
1376 l.rlim_cur = l.rlim_max + 1000;
1377 l.rlim_max = l.rlim_cur;
1378 }
1379 if (setrlimit(RLIMIT_NOFILE, &l) == 0) {
1380 set_rlimit++;
1381 errno = old_errno;
1382 goto retry_open;
1383 }
1384 }
1385 errno = old_errno;
1386 }
1387
Arnaldo Carvalho de Melo594ac612012-12-13 13:13:07 -03001388 if (err != -EINVAL || cpu > 0 || thread > 0)
1389 goto out_close;
1390
Peter Zijlstra814c8c32015-03-31 00:19:31 +02001391 /*
1392 * Must probe features in the order they were added to the
1393 * perf_event_attr interface.
1394 */
1395 if (!perf_missing_features.clockid_wrong && evsel->attr.use_clockid) {
1396 perf_missing_features.clockid_wrong = true;
1397 goto fallback_missing_features;
1398 } else if (!perf_missing_features.clockid && evsel->attr.use_clockid) {
1399 perf_missing_features.clockid = true;
1400 goto fallback_missing_features;
1401 } else if (!perf_missing_features.cloexec && (flags & PERF_FLAG_FD_CLOEXEC)) {
Yann Droneaud57480d22014-06-30 22:28:47 +02001402 perf_missing_features.cloexec = true;
1403 goto fallback_missing_features;
1404 } else if (!perf_missing_features.mmap2 && evsel->attr.mmap2) {
Stephane Eranian5c5e8542013-08-21 12:10:25 +02001405 perf_missing_features.mmap2 = true;
1406 goto fallback_missing_features;
1407 } else if (!perf_missing_features.exclude_guest &&
1408 (evsel->attr.exclude_guest || evsel->attr.exclude_host)) {
Arnaldo Carvalho de Melo594ac612012-12-13 13:13:07 -03001409 perf_missing_features.exclude_guest = true;
1410 goto fallback_missing_features;
1411 } else if (!perf_missing_features.sample_id_all) {
1412 perf_missing_features.sample_id_all = true;
1413 goto retry_sample_id;
1414 }
1415
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -02001416out_close:
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -02001417 do {
1418 while (--thread >= 0) {
1419 close(FD(evsel, cpu, thread));
1420 FD(evsel, cpu, thread) = -1;
1421 }
Adrian Hunterbf8e8f42014-07-31 09:00:51 +03001422 thread = nthreads;
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -02001423 } while (--cpu >= 0);
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -02001424 return err;
1425}
1426
1427void perf_evsel__close(struct perf_evsel *evsel, int ncpus, int nthreads)
1428{
1429 if (evsel->fd == NULL)
1430 return;
1431
1432 perf_evsel__close_fd(evsel, ncpus, nthreads);
1433 perf_evsel__free_fd(evsel);
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -02001434}
1435
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -02001436static struct {
1437 struct cpu_map map;
1438 int cpus[1];
1439} empty_cpu_map = {
1440 .map.nr = 1,
1441 .cpus = { -1, },
1442};
1443
1444static struct {
1445 struct thread_map map;
1446 int threads[1];
1447} empty_thread_map = {
1448 .map.nr = 1,
1449 .threads = { -1, },
1450};
1451
Arnaldo Carvalho de Melof08199d2011-01-11 23:42:19 -02001452int perf_evsel__open(struct perf_evsel *evsel, struct cpu_map *cpus,
Jiri Olsa6a4bb042012-08-08 12:22:36 +02001453 struct thread_map *threads)
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -02001454{
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -02001455 if (cpus == NULL) {
1456 /* Work around old compiler warnings about strict aliasing */
1457 cpus = &empty_cpu_map.map;
1458 }
1459
1460 if (threads == NULL)
1461 threads = &empty_thread_map.map;
1462
Jiri Olsa6a4bb042012-08-08 12:22:36 +02001463 return __perf_evsel__open(evsel, cpus, threads);
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -02001464}
1465
Arnaldo Carvalho de Melof08199d2011-01-11 23:42:19 -02001466int perf_evsel__open_per_cpu(struct perf_evsel *evsel,
Jiri Olsa6a4bb042012-08-08 12:22:36 +02001467 struct cpu_map *cpus)
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -02001468{
Jiri Olsa6a4bb042012-08-08 12:22:36 +02001469 return __perf_evsel__open(evsel, cpus, &empty_thread_map.map);
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -02001470}
1471
Arnaldo Carvalho de Melof08199d2011-01-11 23:42:19 -02001472int perf_evsel__open_per_thread(struct perf_evsel *evsel,
Jiri Olsa6a4bb042012-08-08 12:22:36 +02001473 struct thread_map *threads)
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -02001474{
Jiri Olsa6a4bb042012-08-08 12:22:36 +02001475 return __perf_evsel__open(evsel, &empty_cpu_map.map, threads);
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -02001476}
Arnaldo Carvalho de Melo70082dd2011-01-12 17:03:24 -02001477
Arnaldo Carvalho de Melo0807d2d2012-09-26 12:48:18 -03001478static int perf_evsel__parse_id_sample(const struct perf_evsel *evsel,
1479 const union perf_event *event,
1480 struct perf_sample *sample)
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001481{
Arnaldo Carvalho de Melo0807d2d2012-09-26 12:48:18 -03001482 u64 type = evsel->attr.sample_type;
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001483 const u64 *array = event->sample.array;
Arnaldo Carvalho de Melo0807d2d2012-09-26 12:48:18 -03001484 bool swapped = evsel->needs_swap;
Jiri Olsa37073f92012-05-30 14:23:44 +02001485 union u64_swap u;
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001486
1487 array += ((event->header.size -
1488 sizeof(event->header)) / sizeof(u64)) - 1;
1489
Adrian Hunter75562572013-08-27 11:23:09 +03001490 if (type & PERF_SAMPLE_IDENTIFIER) {
1491 sample->id = *array;
1492 array--;
1493 }
1494
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001495 if (type & PERF_SAMPLE_CPU) {
Jiri Olsa37073f92012-05-30 14:23:44 +02001496 u.val64 = *array;
1497 if (swapped) {
1498 /* undo swap of u64, then swap on individual u32s */
1499 u.val64 = bswap_64(u.val64);
1500 u.val32[0] = bswap_32(u.val32[0]);
1501 }
1502
1503 sample->cpu = u.val32[0];
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001504 array--;
1505 }
1506
1507 if (type & PERF_SAMPLE_STREAM_ID) {
1508 sample->stream_id = *array;
1509 array--;
1510 }
1511
1512 if (type & PERF_SAMPLE_ID) {
1513 sample->id = *array;
1514 array--;
1515 }
1516
1517 if (type & PERF_SAMPLE_TIME) {
1518 sample->time = *array;
1519 array--;
1520 }
1521
1522 if (type & PERF_SAMPLE_TID) {
Jiri Olsa37073f92012-05-30 14:23:44 +02001523 u.val64 = *array;
1524 if (swapped) {
1525 /* undo swap of u64, then swap on individual u32s */
1526 u.val64 = bswap_64(u.val64);
1527 u.val32[0] = bswap_32(u.val32[0]);
1528 u.val32[1] = bswap_32(u.val32[1]);
1529 }
1530
1531 sample->pid = u.val32[0];
1532 sample->tid = u.val32[1];
Adrian Hunterdd44bc62013-10-18 15:29:01 +03001533 array--;
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001534 }
1535
1536 return 0;
1537}
1538
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001539static inline bool overflow(const void *endp, u16 max_size, const void *offset,
1540 u64 size)
Frederic Weisbecker98e1da92011-05-21 20:08:15 +02001541{
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001542 return size > max_size || offset + size > endp;
Frederic Weisbecker98e1da92011-05-21 20:08:15 +02001543}
1544
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001545#define OVERFLOW_CHECK(offset, size, max_size) \
1546 do { \
1547 if (overflow(endp, (max_size), (offset), (size))) \
1548 return -EFAULT; \
1549 } while (0)
1550
1551#define OVERFLOW_CHECK_u64(offset) \
1552 OVERFLOW_CHECK(offset, sizeof(u64), sizeof(u64))
1553
Arnaldo Carvalho de Meloa3f698f2012-08-02 12:23:46 -03001554int perf_evsel__parse_sample(struct perf_evsel *evsel, union perf_event *event,
Arnaldo Carvalho de Melo0807d2d2012-09-26 12:48:18 -03001555 struct perf_sample *data)
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001556{
Arnaldo Carvalho de Meloa3f698f2012-08-02 12:23:46 -03001557 u64 type = evsel->attr.sample_type;
Arnaldo Carvalho de Melo0807d2d2012-09-26 12:48:18 -03001558 bool swapped = evsel->needs_swap;
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001559 const u64 *array;
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001560 u16 max_size = event->header.size;
1561 const void *endp = (void *)event + max_size;
1562 u64 sz;
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001563
David Ahern936be502011-09-06 09:12:26 -06001564 /*
1565 * used for cross-endian analysis. See git commit 65014ab3
1566 * for why this goofiness is needed.
1567 */
Jiri Olsa6a11f922012-05-16 08:59:04 +02001568 union u64_swap u;
David Ahern936be502011-09-06 09:12:26 -06001569
Robert Richterf3bda2c2011-12-15 17:32:39 +01001570 memset(data, 0, sizeof(*data));
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001571 data->cpu = data->pid = data->tid = -1;
1572 data->stream_id = data->id = data->time = -1ULL;
Jiri Olsabc529082014-02-03 12:44:41 +01001573 data->period = evsel->attr.sample_period;
Andi Kleen05484292013-01-24 16:10:29 +01001574 data->weight = 0;
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001575
1576 if (event->header.type != PERF_RECORD_SAMPLE) {
Arnaldo Carvalho de Meloa3f698f2012-08-02 12:23:46 -03001577 if (!evsel->attr.sample_id_all)
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001578 return 0;
Arnaldo Carvalho de Melo0807d2d2012-09-26 12:48:18 -03001579 return perf_evsel__parse_id_sample(evsel, event, data);
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001580 }
1581
1582 array = event->sample.array;
1583
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001584 /*
1585 * The evsel's sample_size is based on PERF_SAMPLE_MASK which includes
1586 * up to PERF_SAMPLE_PERIOD. After that overflow() must be used to
1587 * check the format does not go past the end of the event.
1588 */
Arnaldo Carvalho de Meloa3f698f2012-08-02 12:23:46 -03001589 if (evsel->sample_size + sizeof(event->header) > event->header.size)
Frederic Weisbeckera2854122011-05-21 19:33:04 +02001590 return -EFAULT;
1591
Adrian Hunter75562572013-08-27 11:23:09 +03001592 data->id = -1ULL;
1593 if (type & PERF_SAMPLE_IDENTIFIER) {
1594 data->id = *array;
1595 array++;
1596 }
1597
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001598 if (type & PERF_SAMPLE_IP) {
Adrian Hunteref893252013-08-27 11:23:06 +03001599 data->ip = *array;
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001600 array++;
1601 }
1602
1603 if (type & PERF_SAMPLE_TID) {
David Ahern936be502011-09-06 09:12:26 -06001604 u.val64 = *array;
1605 if (swapped) {
1606 /* undo swap of u64, then swap on individual u32s */
1607 u.val64 = bswap_64(u.val64);
1608 u.val32[0] = bswap_32(u.val32[0]);
1609 u.val32[1] = bswap_32(u.val32[1]);
1610 }
1611
1612 data->pid = u.val32[0];
1613 data->tid = u.val32[1];
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001614 array++;
1615 }
1616
1617 if (type & PERF_SAMPLE_TIME) {
1618 data->time = *array;
1619 array++;
1620 }
1621
David Ahern7cec0922011-05-30 13:08:23 -06001622 data->addr = 0;
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001623 if (type & PERF_SAMPLE_ADDR) {
1624 data->addr = *array;
1625 array++;
1626 }
1627
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001628 if (type & PERF_SAMPLE_ID) {
1629 data->id = *array;
1630 array++;
1631 }
1632
1633 if (type & PERF_SAMPLE_STREAM_ID) {
1634 data->stream_id = *array;
1635 array++;
1636 }
1637
1638 if (type & PERF_SAMPLE_CPU) {
David Ahern936be502011-09-06 09:12:26 -06001639
1640 u.val64 = *array;
1641 if (swapped) {
1642 /* undo swap of u64, then swap on individual u32s */
1643 u.val64 = bswap_64(u.val64);
1644 u.val32[0] = bswap_32(u.val32[0]);
1645 }
1646
1647 data->cpu = u.val32[0];
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001648 array++;
1649 }
1650
1651 if (type & PERF_SAMPLE_PERIOD) {
1652 data->period = *array;
1653 array++;
1654 }
1655
1656 if (type & PERF_SAMPLE_READ) {
Jiri Olsa9ede4732012-10-10 17:38:13 +02001657 u64 read_format = evsel->attr.read_format;
1658
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001659 OVERFLOW_CHECK_u64(array);
Jiri Olsa9ede4732012-10-10 17:38:13 +02001660 if (read_format & PERF_FORMAT_GROUP)
1661 data->read.group.nr = *array;
1662 else
1663 data->read.one.value = *array;
1664
1665 array++;
1666
1667 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) {
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001668 OVERFLOW_CHECK_u64(array);
Jiri Olsa9ede4732012-10-10 17:38:13 +02001669 data->read.time_enabled = *array;
1670 array++;
1671 }
1672
1673 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) {
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001674 OVERFLOW_CHECK_u64(array);
Jiri Olsa9ede4732012-10-10 17:38:13 +02001675 data->read.time_running = *array;
1676 array++;
1677 }
1678
1679 /* PERF_FORMAT_ID is forced for PERF_SAMPLE_READ */
1680 if (read_format & PERF_FORMAT_GROUP) {
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001681 const u64 max_group_nr = UINT64_MAX /
1682 sizeof(struct sample_read_value);
1683
1684 if (data->read.group.nr > max_group_nr)
1685 return -EFAULT;
1686 sz = data->read.group.nr *
1687 sizeof(struct sample_read_value);
1688 OVERFLOW_CHECK(array, sz, max_size);
1689 data->read.group.values =
1690 (struct sample_read_value *)array;
1691 array = (void *)array + sz;
Jiri Olsa9ede4732012-10-10 17:38:13 +02001692 } else {
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001693 OVERFLOW_CHECK_u64(array);
Jiri Olsa9ede4732012-10-10 17:38:13 +02001694 data->read.one.id = *array;
1695 array++;
1696 }
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001697 }
1698
1699 if (type & PERF_SAMPLE_CALLCHAIN) {
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001700 const u64 max_callchain_nr = UINT64_MAX / sizeof(u64);
1701
1702 OVERFLOW_CHECK_u64(array);
1703 data->callchain = (struct ip_callchain *)array++;
1704 if (data->callchain->nr > max_callchain_nr)
Frederic Weisbecker98e1da92011-05-21 20:08:15 +02001705 return -EFAULT;
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001706 sz = data->callchain->nr * sizeof(u64);
1707 OVERFLOW_CHECK(array, sz, max_size);
1708 array = (void *)array + sz;
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001709 }
1710
1711 if (type & PERF_SAMPLE_RAW) {
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001712 OVERFLOW_CHECK_u64(array);
David Ahern936be502011-09-06 09:12:26 -06001713 u.val64 = *array;
1714 if (WARN_ONCE(swapped,
1715 "Endianness of raw data not corrected!\n")) {
1716 /* undo swap of u64, then swap on individual u32s */
1717 u.val64 = bswap_64(u.val64);
1718 u.val32[0] = bswap_32(u.val32[0]);
1719 u.val32[1] = bswap_32(u.val32[1]);
1720 }
David Ahern936be502011-09-06 09:12:26 -06001721 data->raw_size = u.val32[0];
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001722 array = (void *)array + sizeof(u32);
Frederic Weisbecker98e1da92011-05-21 20:08:15 +02001723
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001724 OVERFLOW_CHECK(array, data->raw_size, max_size);
1725 data->raw_data = (void *)array;
1726 array = (void *)array + data->raw_size;
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001727 }
1728
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +01001729 if (type & PERF_SAMPLE_BRANCH_STACK) {
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001730 const u64 max_branch_nr = UINT64_MAX /
1731 sizeof(struct branch_entry);
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +01001732
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001733 OVERFLOW_CHECK_u64(array);
1734 data->branch_stack = (struct branch_stack *)array++;
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +01001735
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001736 if (data->branch_stack->nr > max_branch_nr)
1737 return -EFAULT;
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +01001738 sz = data->branch_stack->nr * sizeof(struct branch_entry);
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001739 OVERFLOW_CHECK(array, sz, max_size);
1740 array = (void *)array + sz;
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +01001741 }
Jiri Olsa0f6a3012012-08-07 15:20:45 +02001742
1743 if (type & PERF_SAMPLE_REGS_USER) {
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001744 OVERFLOW_CHECK_u64(array);
Adrian Hunter5b95a4a32013-08-27 11:23:10 +03001745 data->user_regs.abi = *array;
1746 array++;
Jiri Olsa0f6a3012012-08-07 15:20:45 +02001747
Adrian Hunter5b95a4a32013-08-27 11:23:10 +03001748 if (data->user_regs.abi) {
Jiri Olsa352ea452014-01-07 13:47:25 +01001749 u64 mask = evsel->attr.sample_regs_user;
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001750
Jiri Olsa352ea452014-01-07 13:47:25 +01001751 sz = hweight_long(mask) * sizeof(u64);
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001752 OVERFLOW_CHECK(array, sz, max_size);
Jiri Olsa352ea452014-01-07 13:47:25 +01001753 data->user_regs.mask = mask;
Jiri Olsa0f6a3012012-08-07 15:20:45 +02001754 data->user_regs.regs = (u64 *)array;
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001755 array = (void *)array + sz;
Jiri Olsa0f6a3012012-08-07 15:20:45 +02001756 }
1757 }
1758
1759 if (type & PERF_SAMPLE_STACK_USER) {
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001760 OVERFLOW_CHECK_u64(array);
1761 sz = *array++;
Jiri Olsa0f6a3012012-08-07 15:20:45 +02001762
1763 data->user_stack.offset = ((char *)(array - 1)
1764 - (char *) event);
1765
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001766 if (!sz) {
Jiri Olsa0f6a3012012-08-07 15:20:45 +02001767 data->user_stack.size = 0;
1768 } else {
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001769 OVERFLOW_CHECK(array, sz, max_size);
Jiri Olsa0f6a3012012-08-07 15:20:45 +02001770 data->user_stack.data = (char *)array;
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001771 array = (void *)array + sz;
1772 OVERFLOW_CHECK_u64(array);
Adrian Hunter54bd2692013-07-04 16:20:34 +03001773 data->user_stack.size = *array++;
Jiri Olsaa65cb4b2013-10-02 15:46:39 +02001774 if (WARN_ONCE(data->user_stack.size > sz,
1775 "user stack dump failure\n"))
1776 return -EFAULT;
Jiri Olsa0f6a3012012-08-07 15:20:45 +02001777 }
1778 }
1779
Andi Kleen05484292013-01-24 16:10:29 +01001780 data->weight = 0;
1781 if (type & PERF_SAMPLE_WEIGHT) {
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001782 OVERFLOW_CHECK_u64(array);
Andi Kleen05484292013-01-24 16:10:29 +01001783 data->weight = *array;
1784 array++;
1785 }
1786
Stephane Eranian98a3b322013-01-24 16:10:35 +01001787 data->data_src = PERF_MEM_DATA_SRC_NONE;
1788 if (type & PERF_SAMPLE_DATA_SRC) {
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001789 OVERFLOW_CHECK_u64(array);
Stephane Eranian98a3b322013-01-24 16:10:35 +01001790 data->data_src = *array;
1791 array++;
1792 }
1793
Andi Kleen475eeab2013-09-20 07:40:43 -07001794 data->transaction = 0;
1795 if (type & PERF_SAMPLE_TRANSACTION) {
Adrian Hunter87b95522013-11-01 15:51:36 +02001796 OVERFLOW_CHECK_u64(array);
Andi Kleen475eeab2013-09-20 07:40:43 -07001797 data->transaction = *array;
1798 array++;
1799 }
1800
Stephane Eranian6a21c0b2014-09-24 13:48:39 +02001801 data->intr_regs.abi = PERF_SAMPLE_REGS_ABI_NONE;
1802 if (type & PERF_SAMPLE_REGS_INTR) {
1803 OVERFLOW_CHECK_u64(array);
1804 data->intr_regs.abi = *array;
1805 array++;
1806
1807 if (data->intr_regs.abi != PERF_SAMPLE_REGS_ABI_NONE) {
1808 u64 mask = evsel->attr.sample_regs_intr;
1809
1810 sz = hweight_long(mask) * sizeof(u64);
1811 OVERFLOW_CHECK(array, sz, max_size);
1812 data->intr_regs.mask = mask;
1813 data->intr_regs.regs = (u64 *)array;
1814 array = (void *)array + sz;
1815 }
1816 }
1817
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001818 return 0;
1819}
Andrew Vagin74eec262011-11-28 12:03:31 +03001820
Adrian Hunterb1cf6f62013-08-27 11:23:12 +03001821size_t perf_event__sample_event_size(const struct perf_sample *sample, u64 type,
Jiri Olsa352ea452014-01-07 13:47:25 +01001822 u64 read_format)
Adrian Hunterb1cf6f62013-08-27 11:23:12 +03001823{
1824 size_t sz, result = sizeof(struct sample_event);
1825
1826 if (type & PERF_SAMPLE_IDENTIFIER)
1827 result += sizeof(u64);
1828
1829 if (type & PERF_SAMPLE_IP)
1830 result += sizeof(u64);
1831
1832 if (type & PERF_SAMPLE_TID)
1833 result += sizeof(u64);
1834
1835 if (type & PERF_SAMPLE_TIME)
1836 result += sizeof(u64);
1837
1838 if (type & PERF_SAMPLE_ADDR)
1839 result += sizeof(u64);
1840
1841 if (type & PERF_SAMPLE_ID)
1842 result += sizeof(u64);
1843
1844 if (type & PERF_SAMPLE_STREAM_ID)
1845 result += sizeof(u64);
1846
1847 if (type & PERF_SAMPLE_CPU)
1848 result += sizeof(u64);
1849
1850 if (type & PERF_SAMPLE_PERIOD)
1851 result += sizeof(u64);
1852
1853 if (type & PERF_SAMPLE_READ) {
1854 result += sizeof(u64);
1855 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
1856 result += sizeof(u64);
1857 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
1858 result += sizeof(u64);
1859 /* PERF_FORMAT_ID is forced for PERF_SAMPLE_READ */
1860 if (read_format & PERF_FORMAT_GROUP) {
1861 sz = sample->read.group.nr *
1862 sizeof(struct sample_read_value);
1863 result += sz;
1864 } else {
1865 result += sizeof(u64);
1866 }
1867 }
1868
1869 if (type & PERF_SAMPLE_CALLCHAIN) {
1870 sz = (sample->callchain->nr + 1) * sizeof(u64);
1871 result += sz;
1872 }
1873
1874 if (type & PERF_SAMPLE_RAW) {
1875 result += sizeof(u32);
1876 result += sample->raw_size;
1877 }
1878
1879 if (type & PERF_SAMPLE_BRANCH_STACK) {
1880 sz = sample->branch_stack->nr * sizeof(struct branch_entry);
1881 sz += sizeof(u64);
1882 result += sz;
1883 }
1884
1885 if (type & PERF_SAMPLE_REGS_USER) {
1886 if (sample->user_regs.abi) {
1887 result += sizeof(u64);
Jiri Olsa352ea452014-01-07 13:47:25 +01001888 sz = hweight_long(sample->user_regs.mask) * sizeof(u64);
Adrian Hunterb1cf6f62013-08-27 11:23:12 +03001889 result += sz;
1890 } else {
1891 result += sizeof(u64);
1892 }
1893 }
1894
1895 if (type & PERF_SAMPLE_STACK_USER) {
1896 sz = sample->user_stack.size;
1897 result += sizeof(u64);
1898 if (sz) {
1899 result += sz;
1900 result += sizeof(u64);
1901 }
1902 }
1903
1904 if (type & PERF_SAMPLE_WEIGHT)
1905 result += sizeof(u64);
1906
1907 if (type & PERF_SAMPLE_DATA_SRC)
1908 result += sizeof(u64);
1909
Adrian Hunter42d88912013-11-01 15:51:38 +02001910 if (type & PERF_SAMPLE_TRANSACTION)
1911 result += sizeof(u64);
1912
Stephane Eranian6a21c0b2014-09-24 13:48:39 +02001913 if (type & PERF_SAMPLE_REGS_INTR) {
1914 if (sample->intr_regs.abi) {
1915 result += sizeof(u64);
1916 sz = hweight_long(sample->intr_regs.mask) * sizeof(u64);
1917 result += sz;
1918 } else {
1919 result += sizeof(u64);
1920 }
1921 }
1922
Adrian Hunterb1cf6f62013-08-27 11:23:12 +03001923 return result;
1924}
1925
Andrew Vagin74eec262011-11-28 12:03:31 +03001926int perf_event__synthesize_sample(union perf_event *event, u64 type,
Jiri Olsa352ea452014-01-07 13:47:25 +01001927 u64 read_format,
Andrew Vagin74eec262011-11-28 12:03:31 +03001928 const struct perf_sample *sample,
1929 bool swapped)
1930{
1931 u64 *array;
Adrian Hunterd03f2172013-08-27 11:23:11 +03001932 size_t sz;
Andrew Vagin74eec262011-11-28 12:03:31 +03001933 /*
1934 * used for cross-endian analysis. See git commit 65014ab3
1935 * for why this goofiness is needed.
1936 */
Jiri Olsa6a11f922012-05-16 08:59:04 +02001937 union u64_swap u;
Andrew Vagin74eec262011-11-28 12:03:31 +03001938
1939 array = event->sample.array;
1940
Adrian Hunter75562572013-08-27 11:23:09 +03001941 if (type & PERF_SAMPLE_IDENTIFIER) {
1942 *array = sample->id;
1943 array++;
1944 }
1945
Andrew Vagin74eec262011-11-28 12:03:31 +03001946 if (type & PERF_SAMPLE_IP) {
Adrian Hunteref893252013-08-27 11:23:06 +03001947 *array = sample->ip;
Andrew Vagin74eec262011-11-28 12:03:31 +03001948 array++;
1949 }
1950
1951 if (type & PERF_SAMPLE_TID) {
1952 u.val32[0] = sample->pid;
1953 u.val32[1] = sample->tid;
1954 if (swapped) {
1955 /*
Arnaldo Carvalho de Meloa3f698f2012-08-02 12:23:46 -03001956 * Inverse of what is done in perf_evsel__parse_sample
Andrew Vagin74eec262011-11-28 12:03:31 +03001957 */
1958 u.val32[0] = bswap_32(u.val32[0]);
1959 u.val32[1] = bswap_32(u.val32[1]);
1960 u.val64 = bswap_64(u.val64);
1961 }
1962
1963 *array = u.val64;
1964 array++;
1965 }
1966
1967 if (type & PERF_SAMPLE_TIME) {
1968 *array = sample->time;
1969 array++;
1970 }
1971
1972 if (type & PERF_SAMPLE_ADDR) {
1973 *array = sample->addr;
1974 array++;
1975 }
1976
1977 if (type & PERF_SAMPLE_ID) {
1978 *array = sample->id;
1979 array++;
1980 }
1981
1982 if (type & PERF_SAMPLE_STREAM_ID) {
1983 *array = sample->stream_id;
1984 array++;
1985 }
1986
1987 if (type & PERF_SAMPLE_CPU) {
1988 u.val32[0] = sample->cpu;
1989 if (swapped) {
1990 /*
Arnaldo Carvalho de Meloa3f698f2012-08-02 12:23:46 -03001991 * Inverse of what is done in perf_evsel__parse_sample
Andrew Vagin74eec262011-11-28 12:03:31 +03001992 */
1993 u.val32[0] = bswap_32(u.val32[0]);
1994 u.val64 = bswap_64(u.val64);
1995 }
1996 *array = u.val64;
1997 array++;
1998 }
1999
2000 if (type & PERF_SAMPLE_PERIOD) {
2001 *array = sample->period;
2002 array++;
2003 }
2004
Adrian Hunterd03f2172013-08-27 11:23:11 +03002005 if (type & PERF_SAMPLE_READ) {
2006 if (read_format & PERF_FORMAT_GROUP)
2007 *array = sample->read.group.nr;
2008 else
2009 *array = sample->read.one.value;
2010 array++;
2011
2012 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) {
2013 *array = sample->read.time_enabled;
2014 array++;
2015 }
2016
2017 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) {
2018 *array = sample->read.time_running;
2019 array++;
2020 }
2021
2022 /* PERF_FORMAT_ID is forced for PERF_SAMPLE_READ */
2023 if (read_format & PERF_FORMAT_GROUP) {
2024 sz = sample->read.group.nr *
2025 sizeof(struct sample_read_value);
2026 memcpy(array, sample->read.group.values, sz);
2027 array = (void *)array + sz;
2028 } else {
2029 *array = sample->read.one.id;
2030 array++;
2031 }
2032 }
2033
2034 if (type & PERF_SAMPLE_CALLCHAIN) {
2035 sz = (sample->callchain->nr + 1) * sizeof(u64);
2036 memcpy(array, sample->callchain, sz);
2037 array = (void *)array + sz;
2038 }
2039
2040 if (type & PERF_SAMPLE_RAW) {
2041 u.val32[0] = sample->raw_size;
2042 if (WARN_ONCE(swapped,
2043 "Endianness of raw data not corrected!\n")) {
2044 /*
2045 * Inverse of what is done in perf_evsel__parse_sample
2046 */
2047 u.val32[0] = bswap_32(u.val32[0]);
2048 u.val32[1] = bswap_32(u.val32[1]);
2049 u.val64 = bswap_64(u.val64);
2050 }
2051 *array = u.val64;
2052 array = (void *)array + sizeof(u32);
2053
2054 memcpy(array, sample->raw_data, sample->raw_size);
2055 array = (void *)array + sample->raw_size;
2056 }
2057
2058 if (type & PERF_SAMPLE_BRANCH_STACK) {
2059 sz = sample->branch_stack->nr * sizeof(struct branch_entry);
2060 sz += sizeof(u64);
2061 memcpy(array, sample->branch_stack, sz);
2062 array = (void *)array + sz;
2063 }
2064
2065 if (type & PERF_SAMPLE_REGS_USER) {
2066 if (sample->user_regs.abi) {
2067 *array++ = sample->user_regs.abi;
Jiri Olsa352ea452014-01-07 13:47:25 +01002068 sz = hweight_long(sample->user_regs.mask) * sizeof(u64);
Adrian Hunterd03f2172013-08-27 11:23:11 +03002069 memcpy(array, sample->user_regs.regs, sz);
2070 array = (void *)array + sz;
2071 } else {
2072 *array++ = 0;
2073 }
2074 }
2075
2076 if (type & PERF_SAMPLE_STACK_USER) {
2077 sz = sample->user_stack.size;
2078 *array++ = sz;
2079 if (sz) {
2080 memcpy(array, sample->user_stack.data, sz);
2081 array = (void *)array + sz;
2082 *array++ = sz;
2083 }
2084 }
2085
2086 if (type & PERF_SAMPLE_WEIGHT) {
2087 *array = sample->weight;
2088 array++;
2089 }
2090
2091 if (type & PERF_SAMPLE_DATA_SRC) {
2092 *array = sample->data_src;
2093 array++;
2094 }
2095
Adrian Hunter42d88912013-11-01 15:51:38 +02002096 if (type & PERF_SAMPLE_TRANSACTION) {
2097 *array = sample->transaction;
2098 array++;
2099 }
2100
Stephane Eranian6a21c0b2014-09-24 13:48:39 +02002101 if (type & PERF_SAMPLE_REGS_INTR) {
2102 if (sample->intr_regs.abi) {
2103 *array++ = sample->intr_regs.abi;
2104 sz = hweight_long(sample->intr_regs.mask) * sizeof(u64);
2105 memcpy(array, sample->intr_regs.regs, sz);
2106 array = (void *)array + sz;
2107 } else {
2108 *array++ = 0;
2109 }
2110 }
2111
Andrew Vagin74eec262011-11-28 12:03:31 +03002112 return 0;
2113}
Arnaldo Carvalho de Melo5555ded2012-09-11 19:24:23 -03002114
Arnaldo Carvalho de Meloefd2b922012-09-18 11:21:50 -03002115struct format_field *perf_evsel__field(struct perf_evsel *evsel, const char *name)
2116{
2117 return pevent_find_field(evsel->tp_format, name);
2118}
2119
Arnaldo Carvalho de Melo5d2074e2012-09-26 20:22:00 -03002120void *perf_evsel__rawptr(struct perf_evsel *evsel, struct perf_sample *sample,
Arnaldo Carvalho de Melo5555ded2012-09-11 19:24:23 -03002121 const char *name)
2122{
Arnaldo Carvalho de Meloefd2b922012-09-18 11:21:50 -03002123 struct format_field *field = perf_evsel__field(evsel, name);
Arnaldo Carvalho de Melo5555ded2012-09-11 19:24:23 -03002124 int offset;
2125
Arnaldo Carvalho de Meloefd2b922012-09-18 11:21:50 -03002126 if (!field)
2127 return NULL;
Arnaldo Carvalho de Melo5555ded2012-09-11 19:24:23 -03002128
2129 offset = field->offset;
2130
2131 if (field->flags & FIELD_IS_DYNAMIC) {
2132 offset = *(int *)(sample->raw_data + field->offset);
2133 offset &= 0xffff;
2134 }
2135
2136 return sample->raw_data + offset;
2137}
2138
2139u64 perf_evsel__intval(struct perf_evsel *evsel, struct perf_sample *sample,
2140 const char *name)
2141{
Arnaldo Carvalho de Meloefd2b922012-09-18 11:21:50 -03002142 struct format_field *field = perf_evsel__field(evsel, name);
Arnaldo Carvalho de Meloe6b6f672012-09-26 13:13:04 -03002143 void *ptr;
2144 u64 value;
Arnaldo Carvalho de Melo5555ded2012-09-11 19:24:23 -03002145
Arnaldo Carvalho de Meloefd2b922012-09-18 11:21:50 -03002146 if (!field)
2147 return 0;
Arnaldo Carvalho de Melo5555ded2012-09-11 19:24:23 -03002148
Arnaldo Carvalho de Meloe6b6f672012-09-26 13:13:04 -03002149 ptr = sample->raw_data + field->offset;
Arnaldo Carvalho de Melo5555ded2012-09-11 19:24:23 -03002150
Arnaldo Carvalho de Meloe6b6f672012-09-26 13:13:04 -03002151 switch (field->size) {
2152 case 1:
2153 return *(u8 *)ptr;
2154 case 2:
2155 value = *(u16 *)ptr;
2156 break;
2157 case 4:
2158 value = *(u32 *)ptr;
2159 break;
2160 case 8:
David Aherne94eeda2015-03-24 16:14:09 -04002161 memcpy(&value, ptr, sizeof(u64));
Arnaldo Carvalho de Meloe6b6f672012-09-26 13:13:04 -03002162 break;
2163 default:
2164 return 0;
2165 }
2166
2167 if (!evsel->needs_swap)
2168 return value;
2169
2170 switch (field->size) {
2171 case 2:
2172 return bswap_16(value);
2173 case 4:
2174 return bswap_32(value);
2175 case 8:
2176 return bswap_64(value);
2177 default:
2178 return 0;
2179 }
2180
2181 return 0;
Arnaldo Carvalho de Melo5555ded2012-09-11 19:24:23 -03002182}
Arnaldo Carvalho de Melo0698aed2012-12-10 18:17:08 -03002183
2184static int comma_fprintf(FILE *fp, bool *first, const char *fmt, ...)
2185{
2186 va_list args;
2187 int ret = 0;
2188
2189 if (!*first) {
2190 ret += fprintf(fp, ",");
2191 } else {
2192 ret += fprintf(fp, ":");
2193 *first = false;
2194 }
2195
2196 va_start(args, fmt);
2197 ret += vfprintf(fp, fmt, args);
2198 va_end(args);
2199 return ret;
2200}
2201
Peter Zijlstra2c5e8c52015-04-07 11:09:54 +02002202static int __print_attr__fprintf(FILE *fp, const char *name, const char *val, void *priv)
Arnaldo Carvalho de Melo0698aed2012-12-10 18:17:08 -03002203{
Peter Zijlstra2c5e8c52015-04-07 11:09:54 +02002204 return comma_fprintf(fp, (bool *)priv, " %s: %s", name, val);
Arnaldo Carvalho de Meloc79a4392012-12-11 10:54:12 -03002205}
2206
Arnaldo Carvalho de Melo0698aed2012-12-10 18:17:08 -03002207int perf_evsel__fprintf(struct perf_evsel *evsel,
2208 struct perf_attr_details *details, FILE *fp)
2209{
2210 bool first = true;
Namhyung Kime6ab07d2013-01-22 18:09:47 +09002211 int printed = 0;
2212
Arnaldo Carvalho de Meloe35ef3552013-02-06 17:20:02 -03002213 if (details->event_group) {
Namhyung Kime6ab07d2013-01-22 18:09:47 +09002214 struct perf_evsel *pos;
2215
2216 if (!perf_evsel__is_group_leader(evsel))
2217 return 0;
2218
2219 if (evsel->nr_members > 1)
2220 printed += fprintf(fp, "%s{", evsel->group_name ?: "");
2221
2222 printed += fprintf(fp, "%s", perf_evsel__name(evsel));
2223 for_each_group_member(pos, evsel)
2224 printed += fprintf(fp, ",%s", perf_evsel__name(pos));
2225
2226 if (evsel->nr_members > 1)
2227 printed += fprintf(fp, "}");
2228 goto out;
2229 }
2230
2231 printed += fprintf(fp, "%s", perf_evsel__name(evsel));
Arnaldo Carvalho de Melo0698aed2012-12-10 18:17:08 -03002232
Peter Zijlstra2c5e8c52015-04-07 11:09:54 +02002233 if (details->verbose) {
2234 printed += perf_event_attr__fprintf(fp, &evsel->attr,
2235 __print_attr__fprintf, &first);
2236 } else if (details->freq) {
Namhyung Kim4605bb52015-08-09 15:45:24 +09002237 const char *term = "sample_freq";
2238
2239 if (!evsel->attr.freq)
2240 term = "sample_period";
2241
2242 printed += comma_fprintf(fp, &first, " %s=%" PRIu64,
2243 term, (u64)evsel->attr.sample_freq);
Arnaldo Carvalho de Melo0698aed2012-12-10 18:17:08 -03002244 }
Namhyung Kime6ab07d2013-01-22 18:09:47 +09002245out:
Arnaldo Carvalho de Melo0698aed2012-12-10 18:17:08 -03002246 fputc('\n', fp);
2247 return ++printed;
2248}
Arnaldo Carvalho de Meloc0a54342012-12-13 14:16:30 -03002249
2250bool perf_evsel__fallback(struct perf_evsel *evsel, int err,
2251 char *msg, size_t msgsize)
2252{
David Ahern2b821cc2013-07-18 17:27:59 -06002253 if ((err == ENOENT || err == ENXIO || err == ENODEV) &&
Arnaldo Carvalho de Meloc0a54342012-12-13 14:16:30 -03002254 evsel->attr.type == PERF_TYPE_HARDWARE &&
2255 evsel->attr.config == PERF_COUNT_HW_CPU_CYCLES) {
2256 /*
2257 * If it's cycles then fall back to hrtimer based
2258 * cpu-clock-tick sw counter, which is always available even if
2259 * no PMU support.
2260 *
2261 * PPC returns ENXIO until 2.6.37 (behavior changed with commit
2262 * b0a873e).
2263 */
2264 scnprintf(msg, msgsize, "%s",
2265"The cycles event is not supported, trying to fall back to cpu-clock-ticks");
2266
2267 evsel->attr.type = PERF_TYPE_SOFTWARE;
2268 evsel->attr.config = PERF_COUNT_SW_CPU_CLOCK;
2269
Arnaldo Carvalho de Melo04662522013-12-26 17:41:15 -03002270 zfree(&evsel->name);
Arnaldo Carvalho de Meloc0a54342012-12-13 14:16:30 -03002271 return true;
2272 }
2273
2274 return false;
2275}
Arnaldo Carvalho de Melo56e52e82012-12-13 15:10:58 -03002276
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03002277int perf_evsel__open_strerror(struct perf_evsel *evsel, struct target *target,
Arnaldo Carvalho de Melo56e52e82012-12-13 15:10:58 -03002278 int err, char *msg, size_t size)
2279{
Masami Hiramatsu6e81c742014-08-14 02:22:36 +00002280 char sbuf[STRERR_BUFSIZE];
2281
Arnaldo Carvalho de Melo56e52e82012-12-13 15:10:58 -03002282 switch (err) {
2283 case EPERM:
2284 case EACCES:
David Ahernb69e63a2013-05-25 17:54:00 -06002285 return scnprintf(msg, size,
Arnaldo Carvalho de Melo56e52e82012-12-13 15:10:58 -03002286 "You may not have permission to collect %sstats.\n"
2287 "Consider tweaking /proc/sys/kernel/perf_event_paranoid:\n"
2288 " -1 - Not paranoid at all\n"
2289 " 0 - Disallow raw tracepoint access for unpriv\n"
2290 " 1 - Disallow cpu events for unpriv\n"
2291 " 2 - Disallow kernel profiling for unpriv",
2292 target->system_wide ? "system-wide " : "");
2293 case ENOENT:
2294 return scnprintf(msg, size, "The %s event is not supported.",
2295 perf_evsel__name(evsel));
2296 case EMFILE:
2297 return scnprintf(msg, size, "%s",
2298 "Too many events are opened.\n"
Jiri Olsa18ffdfe2015-05-25 22:51:54 +02002299 "Probably the maximum number of open file descriptors has been reached.\n"
2300 "Hint: Try again after reducing the number of events.\n"
2301 "Hint: Try increasing the limit with 'ulimit -n <limit>'");
Arnaldo Carvalho de Melo56e52e82012-12-13 15:10:58 -03002302 case ENODEV:
2303 if (target->cpu_list)
2304 return scnprintf(msg, size, "%s",
2305 "No such device - did you specify an out-of-range profile CPU?\n");
2306 break;
2307 case EOPNOTSUPP:
2308 if (evsel->attr.precise_ip)
2309 return scnprintf(msg, size, "%s",
2310 "\'precise\' request may not be supported. Try removing 'p' modifier.");
2311#if defined(__i386__) || defined(__x86_64__)
2312 if (evsel->attr.type == PERF_TYPE_HARDWARE)
2313 return scnprintf(msg, size, "%s",
2314 "No hardware sampling interrupt available.\n"
2315 "No APIC? If so then you can boot the kernel with the \"lapic\" boot parameter to force-enable it.");
2316#endif
2317 break;
Jiri Olsa63914ac2014-08-01 17:46:54 +02002318 case EBUSY:
2319 if (find_process("oprofiled"))
2320 return scnprintf(msg, size,
2321 "The PMU counters are busy/taken by another profiler.\n"
2322 "We found oprofile daemon running, please stop it and try again.");
2323 break;
Peter Zijlstra814c8c32015-03-31 00:19:31 +02002324 case EINVAL:
2325 if (perf_missing_features.clockid)
2326 return scnprintf(msg, size, "clockid feature not supported.");
2327 if (perf_missing_features.clockid_wrong)
2328 return scnprintf(msg, size, "wrong clockid (%d).", clockid);
2329 break;
Arnaldo Carvalho de Melo56e52e82012-12-13 15:10:58 -03002330 default:
2331 break;
2332 }
2333
2334 return scnprintf(msg, size,
Masami Hiramatsu6e81c742014-08-14 02:22:36 +00002335 "The sys_perf_event_open() syscall returned with %d (%s) for event (%s).\n"
Arnaldo Carvalho de Melo56e52e82012-12-13 15:10:58 -03002336 "/bin/dmesg may provide additional information.\n"
2337 "No CONFIG_PERF_EVENTS=y kernel support configured?\n",
Masami Hiramatsu6e81c742014-08-14 02:22:36 +00002338 err, strerror_r(err, sbuf, sizeof(sbuf)),
2339 perf_evsel__name(evsel));
Arnaldo Carvalho de Melo56e52e82012-12-13 15:10:58 -03002340}