blob: 33449decf7bd2c24d981fdf30c10042063a503ee [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>
Borislav Petkov553873e2013-12-09 17:14:23 +010012#include <api/fs/debugfs.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>
Andi Kleenbec19672013-08-04 19:41:26 -070016#include <sys/resource.h>
Robert Richter4e319022013-06-11 17:29:18 +020017#include "asm/bug.h"
Arnaldo Carvalho de Melo8f651ea2014-10-09 16:12:24 -030018#include "callchain.h"
Arnaldo Carvalho de Melof14d5702014-10-17 12:17:40 -030019#include "cgroup.h"
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -020020#include "evsel.h"
Arnaldo Carvalho de Melo70082dd2011-01-12 17:03:24 -020021#include "evlist.h"
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -020022#include "util.h"
Arnaldo Carvalho de Melo86bd5e82011-01-03 23:09:46 -020023#include "cpumap.h"
Arnaldo Carvalho de Melofd782602011-01-18 15:15:24 -020024#include "thread_map.h"
Namhyung Kim12864b32012-04-26 14:15:22 +090025#include "target.h"
Jiri Olsa26d33022012-08-07 15:20:47 +020026#include "perf_regs.h"
Adrian Huntere3e1a542013-08-14 15:48:24 +030027#include "debug.h"
Jiri Olsa97978b32013-12-03 14:09:24 +010028#include "trace-event.h"
Jiri Olsaa9a3a4d2015-06-14 10:19:26 +020029#include "stat.h"
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -020030
Arnaldo Carvalho de Melo594ac612012-12-13 13:13:07 -030031static struct {
32 bool sample_id_all;
33 bool exclude_guest;
Stephane Eranian5c5e8542013-08-21 12:10:25 +020034 bool mmap2;
Yann Droneaud57480d22014-06-30 22:28:47 +020035 bool cloexec;
Peter Zijlstra814c8c32015-03-31 00:19:31 +020036 bool clockid;
37 bool clockid_wrong;
Arnaldo Carvalho de Melo594ac612012-12-13 13:13:07 -030038} perf_missing_features;
39
Peter Zijlstra814c8c32015-03-31 00:19:31 +020040static clockid_t clockid;
41
Arnaldo Carvalho de Meloce8ccff2014-10-09 15:29:51 -030042static int perf_evsel__no_extra_init(struct perf_evsel *evsel __maybe_unused)
43{
44 return 0;
45}
46
47static void perf_evsel__no_extra_fini(struct perf_evsel *evsel __maybe_unused)
48{
49}
50
51static struct {
52 size_t size;
53 int (*init)(struct perf_evsel *evsel);
54 void (*fini)(struct perf_evsel *evsel);
55} perf_evsel__object = {
56 .size = sizeof(struct perf_evsel),
57 .init = perf_evsel__no_extra_init,
58 .fini = perf_evsel__no_extra_fini,
59};
60
61int perf_evsel__object_config(size_t object_size,
62 int (*init)(struct perf_evsel *evsel),
63 void (*fini)(struct perf_evsel *evsel))
64{
65
66 if (object_size == 0)
67 goto set_methods;
68
69 if (perf_evsel__object.size > object_size)
70 return -EINVAL;
71
72 perf_evsel__object.size = object_size;
73
74set_methods:
75 if (init != NULL)
76 perf_evsel__object.init = init;
77
78 if (fini != NULL)
79 perf_evsel__object.fini = fini;
80
81 return 0;
82}
83
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -020084#define FD(e, x, y) (*(int *)xyarray__entry(e->fd, x, y))
85
Adrian Hunter75562572013-08-27 11:23:09 +030086int __perf_evsel__sample_size(u64 sample_type)
Arnaldo Carvalho de Meloc2a70652011-06-02 11:04:54 -030087{
88 u64 mask = sample_type & PERF_SAMPLE_MASK;
89 int size = 0;
90 int i;
91
92 for (i = 0; i < 64; i++) {
93 if (mask & (1ULL << i))
94 size++;
95 }
96
97 size *= sizeof(u64);
98
99 return size;
100}
101
Adrian Hunter75562572013-08-27 11:23:09 +0300102/**
103 * __perf_evsel__calc_id_pos - calculate id_pos.
104 * @sample_type: sample type
105 *
106 * This function returns the position of the event id (PERF_SAMPLE_ID or
107 * PERF_SAMPLE_IDENTIFIER) in a sample event i.e. in the array of struct
108 * sample_event.
109 */
110static int __perf_evsel__calc_id_pos(u64 sample_type)
111{
112 int idx = 0;
113
114 if (sample_type & PERF_SAMPLE_IDENTIFIER)
115 return 0;
116
117 if (!(sample_type & PERF_SAMPLE_ID))
118 return -1;
119
120 if (sample_type & PERF_SAMPLE_IP)
121 idx += 1;
122
123 if (sample_type & PERF_SAMPLE_TID)
124 idx += 1;
125
126 if (sample_type & PERF_SAMPLE_TIME)
127 idx += 1;
128
129 if (sample_type & PERF_SAMPLE_ADDR)
130 idx += 1;
131
132 return idx;
133}
134
135/**
136 * __perf_evsel__calc_is_pos - calculate is_pos.
137 * @sample_type: sample type
138 *
139 * This function returns the position (counting backwards) of the event id
140 * (PERF_SAMPLE_ID or PERF_SAMPLE_IDENTIFIER) in a non-sample event i.e. if
141 * sample_id_all is used there is an id sample appended to non-sample events.
142 */
143static int __perf_evsel__calc_is_pos(u64 sample_type)
144{
145 int idx = 1;
146
147 if (sample_type & PERF_SAMPLE_IDENTIFIER)
148 return 1;
149
150 if (!(sample_type & PERF_SAMPLE_ID))
151 return -1;
152
153 if (sample_type & PERF_SAMPLE_CPU)
154 idx += 1;
155
156 if (sample_type & PERF_SAMPLE_STREAM_ID)
157 idx += 1;
158
159 return idx;
160}
161
162void perf_evsel__calc_id_pos(struct perf_evsel *evsel)
163{
164 evsel->id_pos = __perf_evsel__calc_id_pos(evsel->attr.sample_type);
165 evsel->is_pos = __perf_evsel__calc_is_pos(evsel->attr.sample_type);
166}
167
Arnaldo Carvalho de Melo7be5ebe2012-12-10 14:53:43 -0300168void __perf_evsel__set_sample_bit(struct perf_evsel *evsel,
169 enum perf_event_sample_format bit)
170{
171 if (!(evsel->attr.sample_type & bit)) {
172 evsel->attr.sample_type |= bit;
173 evsel->sample_size += sizeof(u64);
Adrian Hunter75562572013-08-27 11:23:09 +0300174 perf_evsel__calc_id_pos(evsel);
Arnaldo Carvalho de Melo7be5ebe2012-12-10 14:53:43 -0300175 }
176}
177
178void __perf_evsel__reset_sample_bit(struct perf_evsel *evsel,
179 enum perf_event_sample_format bit)
180{
181 if (evsel->attr.sample_type & bit) {
182 evsel->attr.sample_type &= ~bit;
183 evsel->sample_size -= sizeof(u64);
Adrian Hunter75562572013-08-27 11:23:09 +0300184 perf_evsel__calc_id_pos(evsel);
Arnaldo Carvalho de Melo7be5ebe2012-12-10 14:53:43 -0300185 }
186}
187
Adrian Hunter75562572013-08-27 11:23:09 +0300188void perf_evsel__set_sample_id(struct perf_evsel *evsel,
189 bool can_sample_identifier)
Arnaldo Carvalho de Melo7a5a5ca2012-12-10 15:21:30 -0300190{
Adrian Hunter75562572013-08-27 11:23:09 +0300191 if (can_sample_identifier) {
192 perf_evsel__reset_sample_bit(evsel, ID);
193 perf_evsel__set_sample_bit(evsel, IDENTIFIER);
194 } else {
195 perf_evsel__set_sample_bit(evsel, ID);
196 }
Arnaldo Carvalho de Melo7a5a5ca2012-12-10 15:21:30 -0300197 evsel->attr.read_format |= PERF_FORMAT_ID;
198}
199
Arnaldo Carvalho de Meloef1d1af2011-01-18 21:41:45 -0200200void perf_evsel__init(struct perf_evsel *evsel,
201 struct perf_event_attr *attr, int idx)
202{
203 evsel->idx = idx;
Adrian Hunter60b08962014-07-31 09:00:52 +0300204 evsel->tracking = !idx;
Arnaldo Carvalho de Meloef1d1af2011-01-18 21:41:45 -0200205 evsel->attr = *attr;
Namhyung Kim2cfda562012-11-29 15:38:29 +0900206 evsel->leader = evsel;
Stephane Eranian410136f2013-11-12 17:58:49 +0100207 evsel->unit = "";
208 evsel->scale = 1.0;
Arnaldo Carvalho de Meloef1d1af2011-01-18 21:41:45 -0200209 INIT_LIST_HEAD(&evsel->node);
Arnaldo Carvalho de Meloce8ccff2014-10-09 15:29:51 -0300210 perf_evsel__object.init(evsel);
Arnaldo Carvalho de Melobde09462012-08-01 18:53:11 -0300211 evsel->sample_size = __perf_evsel__sample_size(attr->sample_type);
Adrian Hunter75562572013-08-27 11:23:09 +0300212 perf_evsel__calc_id_pos(evsel);
Arnaldo Carvalho de Meloef1d1af2011-01-18 21:41:45 -0200213}
214
Arnaldo Carvalho de Meloef503832013-11-07 16:41:19 -0300215struct perf_evsel *perf_evsel__new_idx(struct perf_event_attr *attr, int idx)
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200216{
Arnaldo Carvalho de Meloce8ccff2014-10-09 15:29:51 -0300217 struct perf_evsel *evsel = zalloc(perf_evsel__object.size);
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200218
Arnaldo Carvalho de Meloef1d1af2011-01-18 21:41:45 -0200219 if (evsel != NULL)
220 perf_evsel__init(evsel, attr, idx);
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200221
222 return evsel;
223}
224
Arnaldo Carvalho de Meloef503832013-11-07 16:41:19 -0300225struct perf_evsel *perf_evsel__newtp_idx(const char *sys, const char *name, int idx)
Arnaldo Carvalho de Meloefd2b922012-09-18 11:21:50 -0300226{
Arnaldo Carvalho de Meloce8ccff2014-10-09 15:29:51 -0300227 struct perf_evsel *evsel = zalloc(perf_evsel__object.size);
Arnaldo Carvalho de Meloefd2b922012-09-18 11:21:50 -0300228
229 if (evsel != NULL) {
230 struct perf_event_attr attr = {
Arnaldo Carvalho de Melo0b80f8b32012-09-26 12:28:26 -0300231 .type = PERF_TYPE_TRACEPOINT,
232 .sample_type = (PERF_SAMPLE_RAW | PERF_SAMPLE_TIME |
233 PERF_SAMPLE_CPU | PERF_SAMPLE_PERIOD),
Arnaldo Carvalho de Meloefd2b922012-09-18 11:21:50 -0300234 };
235
Arnaldo Carvalho de Meloe48ffe22012-09-26 17:11:38 -0300236 if (asprintf(&evsel->name, "%s:%s", sys, name) < 0)
237 goto out_free;
238
Jiri Olsa97978b32013-12-03 14:09:24 +0100239 evsel->tp_format = trace_event__tp_format(sys, name);
Arnaldo Carvalho de Meloefd2b922012-09-18 11:21:50 -0300240 if (evsel->tp_format == NULL)
241 goto out_free;
242
Arnaldo Carvalho de Melo0b80f8b32012-09-26 12:28:26 -0300243 event_attr_init(&attr);
Arnaldo Carvalho de Meloefd2b922012-09-18 11:21:50 -0300244 attr.config = evsel->tp_format->id;
Arnaldo Carvalho de Melo0b80f8b32012-09-26 12:28:26 -0300245 attr.sample_period = 1;
Arnaldo Carvalho de Meloefd2b922012-09-18 11:21:50 -0300246 perf_evsel__init(evsel, &attr, idx);
Arnaldo Carvalho de Meloefd2b922012-09-18 11:21:50 -0300247 }
248
249 return evsel;
250
251out_free:
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -0300252 zfree(&evsel->name);
Arnaldo Carvalho de Meloefd2b922012-09-18 11:21:50 -0300253 free(evsel);
254 return NULL;
255}
256
Arnaldo Carvalho de Melo8ad70132012-09-06 13:11:18 -0300257const char *perf_evsel__hw_names[PERF_COUNT_HW_MAX] = {
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300258 "cycles",
259 "instructions",
260 "cache-references",
261 "cache-misses",
262 "branches",
263 "branch-misses",
264 "bus-cycles",
265 "stalled-cycles-frontend",
266 "stalled-cycles-backend",
267 "ref-cycles",
268};
269
Arnaldo Carvalho de Melodd4f5222012-06-13 15:52:42 -0300270static const char *__perf_evsel__hw_name(u64 config)
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300271{
272 if (config < PERF_COUNT_HW_MAX && perf_evsel__hw_names[config])
273 return perf_evsel__hw_names[config];
274
275 return "unknown-hardware";
276}
277
Arnaldo Carvalho de Melo27f18612012-06-11 13:33:09 -0300278static int perf_evsel__add_modifiers(struct perf_evsel *evsel, char *bf, size_t size)
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300279{
Arnaldo Carvalho de Melo27f18612012-06-11 13:33:09 -0300280 int colon = 0, r = 0;
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300281 struct perf_event_attr *attr = &evsel->attr;
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300282 bool exclude_guest_default = false;
283
284#define MOD_PRINT(context, mod) do { \
285 if (!attr->exclude_##context) { \
Arnaldo Carvalho de Melo27f18612012-06-11 13:33:09 -0300286 if (!colon) colon = ++r; \
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300287 r += scnprintf(bf + r, size - r, "%c", mod); \
288 } } while(0)
289
290 if (attr->exclude_kernel || attr->exclude_user || attr->exclude_hv) {
291 MOD_PRINT(kernel, 'k');
292 MOD_PRINT(user, 'u');
293 MOD_PRINT(hv, 'h');
294 exclude_guest_default = true;
295 }
296
297 if (attr->precise_ip) {
298 if (!colon)
Arnaldo Carvalho de Melo27f18612012-06-11 13:33:09 -0300299 colon = ++r;
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300300 r += scnprintf(bf + r, size - r, "%.*s", attr->precise_ip, "ppp");
301 exclude_guest_default = true;
302 }
303
304 if (attr->exclude_host || attr->exclude_guest == exclude_guest_default) {
305 MOD_PRINT(host, 'H');
306 MOD_PRINT(guest, 'G');
307 }
308#undef MOD_PRINT
309 if (colon)
Arnaldo Carvalho de Melo27f18612012-06-11 13:33:09 -0300310 bf[colon - 1] = ':';
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300311 return r;
312}
313
Arnaldo Carvalho de Melo27f18612012-06-11 13:33:09 -0300314static int perf_evsel__hw_name(struct perf_evsel *evsel, char *bf, size_t size)
315{
316 int r = scnprintf(bf, size, "%s", __perf_evsel__hw_name(evsel->attr.config));
317 return r + perf_evsel__add_modifiers(evsel, bf + r, size - r);
318}
319
Arnaldo Carvalho de Melo8ad70132012-09-06 13:11:18 -0300320const char *perf_evsel__sw_names[PERF_COUNT_SW_MAX] = {
Arnaldo Carvalho de Melo335c2f52012-06-11 14:36:20 -0300321 "cpu-clock",
322 "task-clock",
323 "page-faults",
324 "context-switches",
Arnaldo Carvalho de Melo8ad70132012-09-06 13:11:18 -0300325 "cpu-migrations",
Arnaldo Carvalho de Melo335c2f52012-06-11 14:36:20 -0300326 "minor-faults",
327 "major-faults",
328 "alignment-faults",
329 "emulation-faults",
Adrian Hunterd22d1a22013-08-31 21:50:52 +0300330 "dummy",
Arnaldo Carvalho de Melo335c2f52012-06-11 14:36:20 -0300331};
332
Arnaldo Carvalho de Melodd4f5222012-06-13 15:52:42 -0300333static const char *__perf_evsel__sw_name(u64 config)
Arnaldo Carvalho de Melo335c2f52012-06-11 14:36:20 -0300334{
335 if (config < PERF_COUNT_SW_MAX && perf_evsel__sw_names[config])
336 return perf_evsel__sw_names[config];
337 return "unknown-software";
338}
339
340static int perf_evsel__sw_name(struct perf_evsel *evsel, char *bf, size_t size)
341{
342 int r = scnprintf(bf, size, "%s", __perf_evsel__sw_name(evsel->attr.config));
343 return r + perf_evsel__add_modifiers(evsel, bf + r, size - r);
344}
345
Jiri Olsa287e74a2012-06-28 23:18:49 +0200346static int __perf_evsel__bp_name(char *bf, size_t size, u64 addr, u64 type)
347{
348 int r;
349
350 r = scnprintf(bf, size, "mem:0x%" PRIx64 ":", addr);
351
352 if (type & HW_BREAKPOINT_R)
353 r += scnprintf(bf + r, size - r, "r");
354
355 if (type & HW_BREAKPOINT_W)
356 r += scnprintf(bf + r, size - r, "w");
357
358 if (type & HW_BREAKPOINT_X)
359 r += scnprintf(bf + r, size - r, "x");
360
361 return r;
362}
363
364static int perf_evsel__bp_name(struct perf_evsel *evsel, char *bf, size_t size)
365{
366 struct perf_event_attr *attr = &evsel->attr;
367 int r = __perf_evsel__bp_name(bf, size, attr->bp_addr, attr->bp_type);
368 return r + perf_evsel__add_modifiers(evsel, bf + r, size - r);
369}
370
Arnaldo Carvalho de Melo0b668bc2012-06-11 14:08:07 -0300371const char *perf_evsel__hw_cache[PERF_COUNT_HW_CACHE_MAX]
372 [PERF_EVSEL__MAX_ALIASES] = {
373 { "L1-dcache", "l1-d", "l1d", "L1-data", },
374 { "L1-icache", "l1-i", "l1i", "L1-instruction", },
375 { "LLC", "L2", },
376 { "dTLB", "d-tlb", "Data-TLB", },
377 { "iTLB", "i-tlb", "Instruction-TLB", },
378 { "branch", "branches", "bpu", "btb", "bpc", },
379 { "node", },
380};
381
382const char *perf_evsel__hw_cache_op[PERF_COUNT_HW_CACHE_OP_MAX]
383 [PERF_EVSEL__MAX_ALIASES] = {
384 { "load", "loads", "read", },
385 { "store", "stores", "write", },
386 { "prefetch", "prefetches", "speculative-read", "speculative-load", },
387};
388
389const char *perf_evsel__hw_cache_result[PERF_COUNT_HW_CACHE_RESULT_MAX]
390 [PERF_EVSEL__MAX_ALIASES] = {
391 { "refs", "Reference", "ops", "access", },
392 { "misses", "miss", },
393};
394
395#define C(x) PERF_COUNT_HW_CACHE_##x
396#define CACHE_READ (1 << C(OP_READ))
397#define CACHE_WRITE (1 << C(OP_WRITE))
398#define CACHE_PREFETCH (1 << C(OP_PREFETCH))
399#define COP(x) (1 << x)
400
401/*
402 * cache operartion stat
403 * L1I : Read and prefetch only
404 * ITLB and BPU : Read-only
405 */
406static unsigned long perf_evsel__hw_cache_stat[C(MAX)] = {
407 [C(L1D)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
408 [C(L1I)] = (CACHE_READ | CACHE_PREFETCH),
409 [C(LL)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
410 [C(DTLB)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
411 [C(ITLB)] = (CACHE_READ),
412 [C(BPU)] = (CACHE_READ),
413 [C(NODE)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
414};
415
416bool perf_evsel__is_cache_op_valid(u8 type, u8 op)
417{
418 if (perf_evsel__hw_cache_stat[type] & COP(op))
419 return true; /* valid */
420 else
421 return false; /* invalid */
422}
423
424int __perf_evsel__hw_cache_type_op_res_name(u8 type, u8 op, u8 result,
425 char *bf, size_t size)
426{
427 if (result) {
428 return scnprintf(bf, size, "%s-%s-%s", perf_evsel__hw_cache[type][0],
429 perf_evsel__hw_cache_op[op][0],
430 perf_evsel__hw_cache_result[result][0]);
431 }
432
433 return scnprintf(bf, size, "%s-%s", perf_evsel__hw_cache[type][0],
434 perf_evsel__hw_cache_op[op][1]);
435}
436
Arnaldo Carvalho de Melodd4f5222012-06-13 15:52:42 -0300437static int __perf_evsel__hw_cache_name(u64 config, char *bf, size_t size)
Arnaldo Carvalho de Melo0b668bc2012-06-11 14:08:07 -0300438{
439 u8 op, result, type = (config >> 0) & 0xff;
440 const char *err = "unknown-ext-hardware-cache-type";
441
442 if (type > PERF_COUNT_HW_CACHE_MAX)
443 goto out_err;
444
445 op = (config >> 8) & 0xff;
446 err = "unknown-ext-hardware-cache-op";
447 if (op > PERF_COUNT_HW_CACHE_OP_MAX)
448 goto out_err;
449
450 result = (config >> 16) & 0xff;
451 err = "unknown-ext-hardware-cache-result";
452 if (result > PERF_COUNT_HW_CACHE_RESULT_MAX)
453 goto out_err;
454
455 err = "invalid-cache";
456 if (!perf_evsel__is_cache_op_valid(type, op))
457 goto out_err;
458
459 return __perf_evsel__hw_cache_type_op_res_name(type, op, result, bf, size);
460out_err:
461 return scnprintf(bf, size, "%s", err);
462}
463
464static int perf_evsel__hw_cache_name(struct perf_evsel *evsel, char *bf, size_t size)
465{
466 int ret = __perf_evsel__hw_cache_name(evsel->attr.config, bf, size);
467 return ret + perf_evsel__add_modifiers(evsel, bf + ret, size - ret);
468}
469
Arnaldo Carvalho de Melo6eef3d92012-06-13 11:53:37 -0300470static int perf_evsel__raw_name(struct perf_evsel *evsel, char *bf, size_t size)
471{
472 int ret = scnprintf(bf, size, "raw 0x%" PRIx64, evsel->attr.config);
473 return ret + perf_evsel__add_modifiers(evsel, bf + ret, size - ret);
474}
475
Arnaldo Carvalho de Melo7289f832012-06-12 12:34:58 -0300476const char *perf_evsel__name(struct perf_evsel *evsel)
Arnaldo Carvalho de Meloa4460832012-06-12 10:29:12 -0300477{
Arnaldo Carvalho de Melo7289f832012-06-12 12:34:58 -0300478 char bf[128];
Arnaldo Carvalho de Meloa4460832012-06-12 10:29:12 -0300479
Arnaldo Carvalho de Melo7289f832012-06-12 12:34:58 -0300480 if (evsel->name)
481 return evsel->name;
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300482
483 switch (evsel->attr.type) {
484 case PERF_TYPE_RAW:
Arnaldo Carvalho de Melo6eef3d92012-06-13 11:53:37 -0300485 perf_evsel__raw_name(evsel, bf, sizeof(bf));
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300486 break;
487
488 case PERF_TYPE_HARDWARE:
Arnaldo Carvalho de Melo7289f832012-06-12 12:34:58 -0300489 perf_evsel__hw_name(evsel, bf, sizeof(bf));
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300490 break;
Arnaldo Carvalho de Melo0b668bc2012-06-11 14:08:07 -0300491
492 case PERF_TYPE_HW_CACHE:
Arnaldo Carvalho de Melo7289f832012-06-12 12:34:58 -0300493 perf_evsel__hw_cache_name(evsel, bf, sizeof(bf));
Arnaldo Carvalho de Melo0b668bc2012-06-11 14:08:07 -0300494 break;
495
Arnaldo Carvalho de Melo335c2f52012-06-11 14:36:20 -0300496 case PERF_TYPE_SOFTWARE:
Arnaldo Carvalho de Melo7289f832012-06-12 12:34:58 -0300497 perf_evsel__sw_name(evsel, bf, sizeof(bf));
Arnaldo Carvalho de Melo335c2f52012-06-11 14:36:20 -0300498 break;
499
Arnaldo Carvalho de Meloa4460832012-06-12 10:29:12 -0300500 case PERF_TYPE_TRACEPOINT:
Arnaldo Carvalho de Melo7289f832012-06-12 12:34:58 -0300501 scnprintf(bf, sizeof(bf), "%s", "unknown tracepoint");
Arnaldo Carvalho de Meloa4460832012-06-12 10:29:12 -0300502 break;
503
Jiri Olsa287e74a2012-06-28 23:18:49 +0200504 case PERF_TYPE_BREAKPOINT:
505 perf_evsel__bp_name(evsel, bf, sizeof(bf));
506 break;
507
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300508 default:
Robert Richterca1b1452012-08-16 21:10:18 +0200509 scnprintf(bf, sizeof(bf), "unknown attr type: %d",
510 evsel->attr.type);
Arnaldo Carvalho de Meloa4460832012-06-12 10:29:12 -0300511 break;
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300512 }
513
Arnaldo Carvalho de Melo7289f832012-06-12 12:34:58 -0300514 evsel->name = strdup(bf);
515
516 return evsel->name ?: "unknown";
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300517}
518
Namhyung Kim717e2632013-01-22 18:09:44 +0900519const char *perf_evsel__group_name(struct perf_evsel *evsel)
520{
521 return evsel->group_name ?: "anon group";
522}
523
524int perf_evsel__group_desc(struct perf_evsel *evsel, char *buf, size_t size)
525{
526 int ret;
527 struct perf_evsel *pos;
528 const char *group_name = perf_evsel__group_name(evsel);
529
530 ret = scnprintf(buf, size, "%s", group_name);
531
532 ret += scnprintf(buf + ret, size - ret, " { %s",
533 perf_evsel__name(evsel));
534
535 for_each_group_member(pos, evsel)
536 ret += scnprintf(buf + ret, size - ret, ", %s",
537 perf_evsel__name(pos));
538
539 ret += scnprintf(buf + ret, size - ret, " }");
540
541 return ret;
542}
543
Jiri Olsa6bedfab2014-03-02 16:56:40 +0100544static void
Kan Liangaad2b212015-01-05 13:23:04 -0500545perf_evsel__config_callgraph(struct perf_evsel *evsel,
546 struct record_opts *opts)
Jiri Olsa6bedfab2014-03-02 16:56:40 +0100547{
548 bool function = perf_evsel__is_function_event(evsel);
549 struct perf_event_attr *attr = &evsel->attr;
550
551 perf_evsel__set_sample_bit(evsel, CALLCHAIN);
552
Kan Liangaad2b212015-01-05 13:23:04 -0500553 if (callchain_param.record_mode == CALLCHAIN_LBR) {
554 if (!opts->branch_stack) {
555 if (attr->exclude_user) {
556 pr_warning("LBR callstack option is only available "
557 "to get user callchain information. "
558 "Falling back to framepointers.\n");
559 } else {
560 perf_evsel__set_sample_bit(evsel, BRANCH_STACK);
561 attr->branch_sample_type = PERF_SAMPLE_BRANCH_USER |
562 PERF_SAMPLE_BRANCH_CALL_STACK;
563 }
564 } else
565 pr_warning("Cannot use LBR callstack with branch stack. "
566 "Falling back to framepointers.\n");
567 }
568
Namhyung Kim72a128a2014-09-23 10:01:41 +0900569 if (callchain_param.record_mode == CALLCHAIN_DWARF) {
Jiri Olsa6bedfab2014-03-02 16:56:40 +0100570 if (!function) {
571 perf_evsel__set_sample_bit(evsel, REGS_USER);
572 perf_evsel__set_sample_bit(evsel, STACK_USER);
573 attr->sample_regs_user = PERF_REGS_MASK;
Namhyung Kim72a128a2014-09-23 10:01:41 +0900574 attr->sample_stack_user = callchain_param.dump_size;
Jiri Olsa6bedfab2014-03-02 16:56:40 +0100575 attr->exclude_callchain_user = 1;
576 } else {
577 pr_info("Cannot use DWARF unwind for function trace event,"
578 " falling back to framepointers.\n");
579 }
580 }
581
582 if (function) {
583 pr_info("Disabling user space callchains for function trace event.\n");
584 attr->exclude_callchain_user = 1;
585 }
586}
587
Jiri Olsa774cb492012-11-12 18:34:01 +0100588/*
589 * The enable_on_exec/disabled value strategy:
590 *
591 * 1) For any type of traced program:
592 * - all independent events and group leaders are disabled
593 * - all group members are enabled
594 *
595 * Group members are ruled by group leaders. They need to
596 * be enabled, because the group scheduling relies on that.
597 *
598 * 2) For traced programs executed by perf:
599 * - all independent events and group leaders have
600 * enable_on_exec set
601 * - we don't specifically enable or disable any event during
602 * the record command
603 *
604 * Independent events and group leaders are initially disabled
605 * and get enabled by exec. Group members are ruled by group
606 * leaders as stated in 1).
607 *
608 * 3) For traced programs attached by perf (pid/tid):
609 * - we specifically enable or disable all events during
610 * the record command
611 *
612 * When attaching events to already running traced we
613 * enable/disable events specifically, as there's no
614 * initial traced exec call.
615 */
Arnaldo Carvalho de Melob4006792013-12-19 14:43:45 -0300616void perf_evsel__config(struct perf_evsel *evsel, struct record_opts *opts)
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200617{
Jiri Olsa3c176312012-10-10 17:39:03 +0200618 struct perf_evsel *leader = evsel->leader;
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200619 struct perf_event_attr *attr = &evsel->attr;
Adrian Hunter60b08962014-07-31 09:00:52 +0300620 int track = evsel->tracking;
Adrian Hunter3aa59392013-11-15 15:52:29 +0200621 bool per_cpu = opts->target.default_per_cpu && !opts->target.per_thread;
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200622
Arnaldo Carvalho de Melo594ac612012-12-13 13:13:07 -0300623 attr->sample_id_all = perf_missing_features.sample_id_all ? 0 : 1;
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200624 attr->inherit = !opts->no_inherit;
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200625
Arnaldo Carvalho de Melo7be5ebe2012-12-10 14:53:43 -0300626 perf_evsel__set_sample_bit(evsel, IP);
627 perf_evsel__set_sample_bit(evsel, TID);
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200628
Jiri Olsa3c176312012-10-10 17:39:03 +0200629 if (evsel->sample_read) {
630 perf_evsel__set_sample_bit(evsel, READ);
631
632 /*
633 * We need ID even in case of single event, because
634 * PERF_SAMPLE_READ process ID specific data.
635 */
Adrian Hunter75562572013-08-27 11:23:09 +0300636 perf_evsel__set_sample_id(evsel, false);
Jiri Olsa3c176312012-10-10 17:39:03 +0200637
638 /*
639 * Apply group format only if we belong to group
640 * with more than one members.
641 */
642 if (leader->nr_members > 1) {
643 attr->read_format |= PERF_FORMAT_GROUP;
644 attr->inherit = 0;
645 }
646 }
647
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200648 /*
Namhyung Kim17314e22014-06-09 14:43:37 +0900649 * We default some events to have a default interval. But keep
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200650 * it a weak assumption overridable by the user.
651 */
Namhyung Kim17314e22014-06-09 14:43:37 +0900652 if (!attr->sample_period || (opts->user_freq != UINT_MAX ||
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200653 opts->user_interval != ULLONG_MAX)) {
654 if (opts->freq) {
Arnaldo Carvalho de Melo7be5ebe2012-12-10 14:53:43 -0300655 perf_evsel__set_sample_bit(evsel, PERIOD);
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200656 attr->freq = 1;
657 attr->sample_freq = opts->freq;
658 } else {
659 attr->sample_period = opts->default_interval;
660 }
661 }
662
Jiri Olsa3c176312012-10-10 17:39:03 +0200663 /*
664 * Disable sampling for all group members other
665 * than leader in case leader 'leads' the sampling.
666 */
667 if ((leader != evsel) && leader->sample_read) {
668 attr->sample_freq = 0;
669 attr->sample_period = 0;
670 }
671
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200672 if (opts->no_samples)
673 attr->sample_freq = 0;
674
675 if (opts->inherit_stat)
676 attr->inherit_stat = 1;
677
678 if (opts->sample_address) {
Arnaldo Carvalho de Melo7be5ebe2012-12-10 14:53:43 -0300679 perf_evsel__set_sample_bit(evsel, ADDR);
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200680 attr->mmap_data = track;
681 }
682
Jiri Olsaf1403732014-11-13 18:21:03 +0100683 /*
684 * We don't allow user space callchains for function trace
685 * event, due to issues with page faults while tracing page
686 * fault handler and its overall trickiness nature.
687 */
688 if (perf_evsel__is_function_event(evsel))
689 evsel->attr.exclude_callchain_user = 1;
690
Namhyung Kim72a128a2014-09-23 10:01:41 +0900691 if (callchain_param.enabled && !evsel->no_aux_samples)
Kan Liangaad2b212015-01-05 13:23:04 -0500692 perf_evsel__config_callgraph(evsel, opts);
Jiri Olsa26d33022012-08-07 15:20:47 +0200693
Stephane Eranian6a21c0b2014-09-24 13:48:39 +0200694 if (opts->sample_intr_regs) {
695 attr->sample_regs_intr = PERF_REGS_MASK;
696 perf_evsel__set_sample_bit(evsel, REGS_INTR);
697 }
698
Adrian Hunter3aa59392013-11-15 15:52:29 +0200699 if (target__has_cpu(&opts->target))
Arnaldo Carvalho de Melo7be5ebe2012-12-10 14:53:43 -0300700 perf_evsel__set_sample_bit(evsel, CPU);
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200701
Andrew Vagin3e76ac72011-12-20 17:32:45 +0300702 if (opts->period)
Arnaldo Carvalho de Melo7be5ebe2012-12-10 14:53:43 -0300703 perf_evsel__set_sample_bit(evsel, PERIOD);
Andrew Vagin3e76ac72011-12-20 17:32:45 +0300704
Andi Kleen8affc2b2014-07-31 14:45:04 +0800705 /*
706 * When the user explicitely disabled time don't force it here.
707 */
708 if (opts->sample_time &&
709 (!perf_missing_features.sample_id_all &&
710 (!opts->no_inherit || target__has_cpu(&opts->target) || per_cpu)))
Arnaldo Carvalho de Melo7be5ebe2012-12-10 14:53:43 -0300711 perf_evsel__set_sample_bit(evsel, TIME);
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200712
Adrian Hunter6ff1ce72014-07-14 13:02:56 +0300713 if (opts->raw_samples && !evsel->no_aux_samples) {
Arnaldo Carvalho de Melo7be5ebe2012-12-10 14:53:43 -0300714 perf_evsel__set_sample_bit(evsel, TIME);
715 perf_evsel__set_sample_bit(evsel, RAW);
716 perf_evsel__set_sample_bit(evsel, CPU);
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200717 }
718
Stephane Eranianccf49bf2013-01-24 16:10:37 +0100719 if (opts->sample_address)
Adrian Hunter1e7ed5e2013-11-01 15:51:35 +0200720 perf_evsel__set_sample_bit(evsel, DATA_SRC);
Stephane Eranianccf49bf2013-01-24 16:10:37 +0100721
Arnaldo Carvalho de Melo509051e2014-01-14 17:52:14 -0300722 if (opts->no_buffering) {
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200723 attr->watermark = 0;
724 attr->wakeup_events = 1;
725 }
Adrian Hunter6ff1ce72014-07-14 13:02:56 +0300726 if (opts->branch_stack && !evsel->no_aux_samples) {
Arnaldo Carvalho de Melo7be5ebe2012-12-10 14:53:43 -0300727 perf_evsel__set_sample_bit(evsel, BRANCH_STACK);
Roberto Agostino Vitillobdfebd82012-02-09 23:21:02 +0100728 attr->branch_sample_type = opts->branch_stack;
729 }
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200730
Andi Kleen05484292013-01-24 16:10:29 +0100731 if (opts->sample_weight)
Adrian Hunter1e7ed5e2013-11-01 15:51:35 +0200732 perf_evsel__set_sample_bit(evsel, WEIGHT);
Andi Kleen05484292013-01-24 16:10:29 +0100733
Namhyung Kim62e503b2015-01-29 17:06:46 +0900734 attr->task = track;
Stephane Eranian5c5e8542013-08-21 12:10:25 +0200735 attr->mmap = track;
Don Zickusa5a5ba72014-05-30 10:49:42 -0400736 attr->mmap2 = track && !perf_missing_features.mmap2;
Stephane Eranian5c5e8542013-08-21 12:10:25 +0200737 attr->comm = track;
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200738
Andi Kleen475eeab2013-09-20 07:40:43 -0700739 if (opts->sample_transaction)
Adrian Hunter1e7ed5e2013-11-01 15:51:35 +0200740 perf_evsel__set_sample_bit(evsel, TRANSACTION);
Andi Kleen475eeab2013-09-20 07:40:43 -0700741
Andi Kleen85c273d2015-02-24 15:13:40 -0800742 if (opts->running_time) {
743 evsel->attr.read_format |=
744 PERF_FORMAT_TOTAL_TIME_ENABLED |
745 PERF_FORMAT_TOTAL_TIME_RUNNING;
746 }
747
Jiri Olsa774cb492012-11-12 18:34:01 +0100748 /*
749 * XXX see the function comment above
750 *
751 * Disabling only independent events or group leaders,
752 * keeping group members enabled.
753 */
Namhyung Kim823254e2012-11-29 15:38:30 +0900754 if (perf_evsel__is_group_leader(evsel))
Jiri Olsa774cb492012-11-12 18:34:01 +0100755 attr->disabled = 1;
756
757 /*
758 * Setting enable_on_exec for independent events and
759 * group leaders for traced executed by perf.
760 */
Andi Kleen6619a532014-01-11 13:38:27 -0800761 if (target__none(&opts->target) && perf_evsel__is_group_leader(evsel) &&
762 !opts->initial_delay)
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200763 attr->enable_on_exec = 1;
Adrian Hunter2afd2bc2014-07-14 13:02:57 +0300764
765 if (evsel->immediate) {
766 attr->disabled = 0;
767 attr->enable_on_exec = 0;
768 }
Peter Zijlstra814c8c32015-03-31 00:19:31 +0200769
770 clockid = opts->clockid;
771 if (opts->use_clockid) {
772 attr->use_clockid = 1;
773 attr->clockid = opts->clockid;
774 }
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200775}
776
Arnaldo Carvalho de Melo88858462014-10-13 13:30:27 -0300777static int perf_evsel__alloc_fd(struct perf_evsel *evsel, int ncpus, int nthreads)
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200778{
David Ahern4af4c952011-05-27 09:58:34 -0600779 int cpu, thread;
Adrian Hunterbf8e8f42014-07-31 09:00:51 +0300780
781 if (evsel->system_wide)
782 nthreads = 1;
783
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200784 evsel->fd = xyarray__new(ncpus, nthreads, sizeof(int));
David Ahern4af4c952011-05-27 09:58:34 -0600785
786 if (evsel->fd) {
787 for (cpu = 0; cpu < ncpus; cpu++) {
788 for (thread = 0; thread < nthreads; thread++) {
789 FD(evsel, cpu, thread) = -1;
790 }
791 }
792 }
793
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200794 return evsel->fd != NULL ? 0 : -ENOMEM;
795}
796
Andi Kleene2407be2013-08-02 17:41:10 -0700797static int perf_evsel__run_ioctl(struct perf_evsel *evsel, int ncpus, int nthreads,
798 int ioc, void *arg)
Arnaldo Carvalho de Melo745cefc2012-09-26 15:07:39 -0300799{
800 int cpu, thread;
801
Adrian Hunterbf8e8f42014-07-31 09:00:51 +0300802 if (evsel->system_wide)
803 nthreads = 1;
804
Arnaldo Carvalho de Melo745cefc2012-09-26 15:07:39 -0300805 for (cpu = 0; cpu < ncpus; cpu++) {
806 for (thread = 0; thread < nthreads; thread++) {
807 int fd = FD(evsel, cpu, thread),
Andi Kleene2407be2013-08-02 17:41:10 -0700808 err = ioctl(fd, ioc, arg);
Arnaldo Carvalho de Melo745cefc2012-09-26 15:07:39 -0300809
810 if (err)
811 return err;
812 }
813 }
814
815 return 0;
816}
817
Andi Kleene2407be2013-08-02 17:41:10 -0700818int perf_evsel__set_filter(struct perf_evsel *evsel, int ncpus, int nthreads,
819 const char *filter)
820{
821 return perf_evsel__run_ioctl(evsel, ncpus, nthreads,
822 PERF_EVENT_IOC_SET_FILTER,
823 (void *)filter);
824}
825
826int perf_evsel__enable(struct perf_evsel *evsel, int ncpus, int nthreads)
827{
828 return perf_evsel__run_ioctl(evsel, ncpus, nthreads,
829 PERF_EVENT_IOC_ENABLE,
830 0);
831}
832
Arnaldo Carvalho de Melo70db7532011-01-12 22:39:13 -0200833int perf_evsel__alloc_id(struct perf_evsel *evsel, int ncpus, int nthreads)
834{
Vineet Gupta8d9cbd82015-01-13 19:13:23 +0530835 if (ncpus == 0 || nthreads == 0)
836 return 0;
837
Adrian Hunterbf8e8f42014-07-31 09:00:51 +0300838 if (evsel->system_wide)
839 nthreads = 1;
840
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -0300841 evsel->sample_id = xyarray__new(ncpus, nthreads, sizeof(struct perf_sample_id));
842 if (evsel->sample_id == NULL)
843 return -ENOMEM;
844
845 evsel->id = zalloc(ncpus * nthreads * sizeof(u64));
846 if (evsel->id == NULL) {
847 xyarray__delete(evsel->sample_id);
848 evsel->sample_id = NULL;
849 return -ENOMEM;
850 }
851
852 return 0;
Arnaldo Carvalho de Melo70db7532011-01-12 22:39:13 -0200853}
854
Arnaldo Carvalho de Melo88858462014-10-13 13:30:27 -0300855static void perf_evsel__free_fd(struct perf_evsel *evsel)
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200856{
857 xyarray__delete(evsel->fd);
858 evsel->fd = NULL;
859}
860
Arnaldo Carvalho de Melo88858462014-10-13 13:30:27 -0300861static void perf_evsel__free_id(struct perf_evsel *evsel)
Arnaldo Carvalho de Melo70db7532011-01-12 22:39:13 -0200862{
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -0300863 xyarray__delete(evsel->sample_id);
864 evsel->sample_id = NULL;
Arnaldo Carvalho de Melo04662522013-12-26 17:41:15 -0300865 zfree(&evsel->id);
Arnaldo Carvalho de Melo70db7532011-01-12 22:39:13 -0200866}
867
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200868void perf_evsel__close_fd(struct perf_evsel *evsel, int ncpus, int nthreads)
869{
870 int cpu, thread;
871
Adrian Hunterbf8e8f42014-07-31 09:00:51 +0300872 if (evsel->system_wide)
873 nthreads = 1;
874
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200875 for (cpu = 0; cpu < ncpus; cpu++)
876 for (thread = 0; thread < nthreads; ++thread) {
877 close(FD(evsel, cpu, thread));
878 FD(evsel, cpu, thread) = -1;
879 }
880}
881
Arnaldo Carvalho de Meloef1d1af2011-01-18 21:41:45 -0200882void perf_evsel__exit(struct perf_evsel *evsel)
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200883{
884 assert(list_empty(&evsel->node));
Namhyung Kim736b05a2013-03-15 14:48:49 +0900885 perf_evsel__free_fd(evsel);
886 perf_evsel__free_id(evsel);
Arnaldo Carvalho de Melo597e48c2014-10-16 13:25:01 -0300887 close_cgroup(evsel->cgrp);
888 zfree(&evsel->group_name);
Arnaldo Carvalho de Melo597e48c2014-10-16 13:25:01 -0300889 zfree(&evsel->name);
Arnaldo Carvalho de Meloce8ccff2014-10-09 15:29:51 -0300890 perf_evsel__object.fini(evsel);
Arnaldo Carvalho de Meloef1d1af2011-01-18 21:41:45 -0200891}
892
893void perf_evsel__delete(struct perf_evsel *evsel)
894{
895 perf_evsel__exit(evsel);
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200896 free(evsel);
897}
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200898
Jiri Olsa857a94a2014-11-21 10:31:05 +0100899void perf_evsel__compute_deltas(struct perf_evsel *evsel, int cpu,
900 struct perf_counts_values *count)
Stephane Eranianc7a79c42013-01-29 12:47:43 +0100901{
902 struct perf_counts_values tmp;
903
904 if (!evsel->prev_raw_counts)
905 return;
906
907 if (cpu == -1) {
908 tmp = evsel->prev_raw_counts->aggr;
909 evsel->prev_raw_counts->aggr = *count;
910 } else {
911 tmp = evsel->prev_raw_counts->cpu[cpu];
912 evsel->prev_raw_counts->cpu[cpu] = *count;
913 }
914
915 count->val = count->val - tmp.val;
916 count->ena = count->ena - tmp.ena;
917 count->run = count->run - tmp.run;
918}
919
Jiri Olsa13112bb2014-11-21 10:31:06 +0100920void perf_counts_values__scale(struct perf_counts_values *count,
921 bool scale, s8 *pscaled)
922{
923 s8 scaled = 0;
924
925 if (scale) {
926 if (count->run == 0) {
927 scaled = -1;
928 count->val = 0;
929 } else if (count->run < count->ena) {
930 scaled = 1;
931 count->val = (u64)((double) count->val * count->ena / count->run + 0.5);
932 }
933 } else
934 count->ena = count->run = 0;
935
936 if (pscaled)
937 *pscaled = scaled;
938}
939
Jiri Olsa011dccb2014-11-21 10:31:07 +0100940int perf_evsel__read_cb(struct perf_evsel *evsel, int cpu, int thread,
941 perf_evsel__read_cb_t cb)
942{
943 struct perf_counts_values count;
944
945 memset(&count, 0, sizeof(count));
946
947 if (FD(evsel, cpu, thread) < 0)
948 return -EINVAL;
949
950 if (readn(FD(evsel, cpu, thread), &count, sizeof(count)) < 0)
951 return -errno;
952
953 return cb(evsel, cpu, thread, &count);
954}
955
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200956int __perf_evsel__read_on_cpu(struct perf_evsel *evsel,
957 int cpu, int thread, bool scale)
958{
959 struct perf_counts_values count;
960 size_t nv = scale ? 3 : 1;
961
962 if (FD(evsel, cpu, thread) < 0)
963 return -EINVAL;
964
Arnaldo Carvalho de Melo4eed11d2011-01-04 00:13:17 -0200965 if (evsel->counts == NULL && perf_evsel__alloc_counts(evsel, cpu + 1) < 0)
966 return -ENOMEM;
967
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200968 if (readn(FD(evsel, cpu, thread), &count, nv * sizeof(u64)) < 0)
969 return -errno;
970
Jiri Olsa857a94a2014-11-21 10:31:05 +0100971 perf_evsel__compute_deltas(evsel, cpu, &count);
Jiri Olsa13112bb2014-11-21 10:31:06 +0100972 perf_counts_values__scale(&count, scale, NULL);
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200973 evsel->counts->cpu[cpu] = count;
974 return 0;
975}
976
Jiri Olsa6a4bb042012-08-08 12:22:36 +0200977static int get_group_fd(struct perf_evsel *evsel, int cpu, int thread)
978{
979 struct perf_evsel *leader = evsel->leader;
980 int fd;
981
Namhyung Kim823254e2012-11-29 15:38:30 +0900982 if (perf_evsel__is_group_leader(evsel))
Jiri Olsa6a4bb042012-08-08 12:22:36 +0200983 return -1;
984
985 /*
986 * Leader must be already processed/open,
987 * if not it's a bug.
988 */
989 BUG_ON(!leader->fd);
990
991 fd = FD(leader, cpu, thread);
992 BUG_ON(fd == -1);
993
994 return fd;
995}
996
Peter Zijlstra2c5e8c52015-04-07 11:09:54 +0200997struct bit_names {
998 int bit;
999 const char *name;
1000};
Adrian Huntere3e1a542013-08-14 15:48:24 +03001001
Peter Zijlstra2c5e8c52015-04-07 11:09:54 +02001002static void __p_bits(char *buf, size_t size, u64 value, struct bit_names *bits)
Adrian Huntere3e1a542013-08-14 15:48:24 +03001003{
Peter Zijlstra2c5e8c52015-04-07 11:09:54 +02001004 bool first_bit = true;
1005 int i = 0;
Adrian Huntere3e1a542013-08-14 15:48:24 +03001006
Peter Zijlstra2c5e8c52015-04-07 11:09:54 +02001007 do {
1008 if (value & bits[i].bit) {
1009 buf += scnprintf(buf, size, "%s%s", first_bit ? "" : "|", bits[i].name);
1010 first_bit = false;
1011 }
1012 } while (bits[++i].name != NULL);
1013}
Adrian Huntere3e1a542013-08-14 15:48:24 +03001014
Peter Zijlstra2c5e8c52015-04-07 11:09:54 +02001015static void __p_sample_type(char *buf, size_t size, u64 value)
1016{
1017#define bit_name(n) { PERF_SAMPLE_##n, #n }
1018 struct bit_names bits[] = {
1019 bit_name(IP), bit_name(TID), bit_name(TIME), bit_name(ADDR),
1020 bit_name(READ), bit_name(CALLCHAIN), bit_name(ID), bit_name(CPU),
1021 bit_name(PERIOD), bit_name(STREAM_ID), bit_name(RAW),
1022 bit_name(BRANCH_STACK), bit_name(REGS_USER), bit_name(STACK_USER),
1023 bit_name(IDENTIFIER), bit_name(REGS_INTR),
1024 { .name = NULL, }
1025 };
1026#undef bit_name
1027 __p_bits(buf, size, value, bits);
1028}
Adrian Huntere3e1a542013-08-14 15:48:24 +03001029
Peter Zijlstra2c5e8c52015-04-07 11:09:54 +02001030static void __p_read_format(char *buf, size_t size, u64 value)
1031{
1032#define bit_name(n) { PERF_FORMAT_##n, #n }
1033 struct bit_names bits[] = {
1034 bit_name(TOTAL_TIME_ENABLED), bit_name(TOTAL_TIME_RUNNING),
1035 bit_name(ID), bit_name(GROUP),
1036 { .name = NULL, }
1037 };
1038#undef bit_name
1039 __p_bits(buf, size, value, bits);
1040}
Peter Zijlstra814c8c32015-03-31 00:19:31 +02001041
Peter Zijlstra2c5e8c52015-04-07 11:09:54 +02001042#define BUF_SIZE 1024
Adrian Huntere3e1a542013-08-14 15:48:24 +03001043
Adrian Hunter7310aed2015-06-11 15:51:04 +03001044#define p_hex(val) snprintf(buf, BUF_SIZE, "%#"PRIx64, (uint64_t)(val))
Peter Zijlstra2c5e8c52015-04-07 11:09:54 +02001045#define p_unsigned(val) snprintf(buf, BUF_SIZE, "%"PRIu64, (uint64_t)(val))
1046#define p_signed(val) snprintf(buf, BUF_SIZE, "%"PRId64, (int64_t)(val))
1047#define p_sample_type(val) __p_sample_type(buf, BUF_SIZE, val)
1048#define p_read_format(val) __p_read_format(buf, BUF_SIZE, val)
Adrian Huntere3e1a542013-08-14 15:48:24 +03001049
Peter Zijlstra2c5e8c52015-04-07 11:09:54 +02001050#define PRINT_ATTRn(_n, _f, _p) \
1051do { \
1052 if (attr->_f) { \
1053 _p(attr->_f); \
1054 ret += attr__fprintf(fp, _n, buf, priv);\
1055 } \
1056} while (0)
1057
1058#define PRINT_ATTRf(_f, _p) PRINT_ATTRn(#_f, _f, _p)
1059
1060int perf_event_attr__fprintf(FILE *fp, struct perf_event_attr *attr,
1061 attr__fprintf_f attr__fprintf, void *priv)
1062{
1063 char buf[BUF_SIZE];
1064 int ret = 0;
1065
1066 PRINT_ATTRf(type, p_unsigned);
1067 PRINT_ATTRf(size, p_unsigned);
1068 PRINT_ATTRf(config, p_hex);
1069 PRINT_ATTRn("{ sample_period, sample_freq }", sample_period, p_unsigned);
1070 PRINT_ATTRf(sample_type, p_sample_type);
1071 PRINT_ATTRf(read_format, p_read_format);
1072
1073 PRINT_ATTRf(disabled, p_unsigned);
1074 PRINT_ATTRf(inherit, p_unsigned);
1075 PRINT_ATTRf(pinned, p_unsigned);
1076 PRINT_ATTRf(exclusive, p_unsigned);
1077 PRINT_ATTRf(exclude_user, p_unsigned);
1078 PRINT_ATTRf(exclude_kernel, p_unsigned);
1079 PRINT_ATTRf(exclude_hv, p_unsigned);
1080 PRINT_ATTRf(exclude_idle, p_unsigned);
1081 PRINT_ATTRf(mmap, p_unsigned);
1082 PRINT_ATTRf(comm, p_unsigned);
1083 PRINT_ATTRf(freq, p_unsigned);
1084 PRINT_ATTRf(inherit_stat, p_unsigned);
1085 PRINT_ATTRf(enable_on_exec, p_unsigned);
1086 PRINT_ATTRf(task, p_unsigned);
1087 PRINT_ATTRf(watermark, p_unsigned);
1088 PRINT_ATTRf(precise_ip, p_unsigned);
1089 PRINT_ATTRf(mmap_data, p_unsigned);
1090 PRINT_ATTRf(sample_id_all, p_unsigned);
1091 PRINT_ATTRf(exclude_host, p_unsigned);
1092 PRINT_ATTRf(exclude_guest, p_unsigned);
1093 PRINT_ATTRf(exclude_callchain_kernel, p_unsigned);
1094 PRINT_ATTRf(exclude_callchain_user, p_unsigned);
1095 PRINT_ATTRf(mmap2, p_unsigned);
1096 PRINT_ATTRf(comm_exec, p_unsigned);
1097 PRINT_ATTRf(use_clockid, p_unsigned);
1098
1099 PRINT_ATTRn("{ wakeup_events, wakeup_watermark }", wakeup_events, p_unsigned);
1100 PRINT_ATTRf(bp_type, p_unsigned);
1101 PRINT_ATTRn("{ bp_addr, config1 }", bp_addr, p_hex);
1102 PRINT_ATTRn("{ bp_len, config2 }", bp_len, p_hex);
1103 PRINT_ATTRf(sample_regs_user, p_hex);
1104 PRINT_ATTRf(sample_stack_user, p_unsigned);
1105 PRINT_ATTRf(clockid, p_signed);
1106 PRINT_ATTRf(sample_regs_intr, p_hex);
Adrian Hunter70d73de2015-04-09 18:54:06 +03001107 PRINT_ATTRf(aux_watermark, p_unsigned);
Adrian Huntere3e1a542013-08-14 15:48:24 +03001108
1109 return ret;
1110}
1111
Peter Zijlstra2c5e8c52015-04-07 11:09:54 +02001112static int __open_attr__fprintf(FILE *fp, const char *name, const char *val,
1113 void *priv __attribute__((unused)))
1114{
1115 return fprintf(fp, " %-32s %s\n", name, val);
1116}
1117
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -02001118static int __perf_evsel__open(struct perf_evsel *evsel, struct cpu_map *cpus,
Jiri Olsa6a4bb042012-08-08 12:22:36 +02001119 struct thread_map *threads)
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -02001120{
Adrian Hunterbf8e8f42014-07-31 09:00:51 +03001121 int cpu, thread, nthreads;
Yann Droneaud57480d22014-06-30 22:28:47 +02001122 unsigned long flags = PERF_FLAG_FD_CLOEXEC;
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -02001123 int pid = -1, err;
Andi Kleenbec19672013-08-04 19:41:26 -07001124 enum { NO_CHANGE, SET_TO_MAX, INCREASED_MAX } set_rlimit = NO_CHANGE;
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -02001125
Adrian Hunterbf8e8f42014-07-31 09:00:51 +03001126 if (evsel->system_wide)
1127 nthreads = 1;
1128 else
1129 nthreads = threads->nr;
1130
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -02001131 if (evsel->fd == NULL &&
Adrian Hunterbf8e8f42014-07-31 09:00:51 +03001132 perf_evsel__alloc_fd(evsel, cpus->nr, nthreads) < 0)
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -02001133 return -ENOMEM;
Arnaldo Carvalho de Melo4eed11d2011-01-04 00:13:17 -02001134
Stephane Eranian023695d2011-02-14 11:20:01 +02001135 if (evsel->cgrp) {
Yann Droneaud57480d22014-06-30 22:28:47 +02001136 flags |= PERF_FLAG_PID_CGROUP;
Stephane Eranian023695d2011-02-14 11:20:01 +02001137 pid = evsel->cgrp->fd;
1138 }
1139
Arnaldo Carvalho de Melo594ac612012-12-13 13:13:07 -03001140fallback_missing_features:
Peter Zijlstra814c8c32015-03-31 00:19:31 +02001141 if (perf_missing_features.clockid_wrong)
1142 evsel->attr.clockid = CLOCK_MONOTONIC; /* should always work */
1143 if (perf_missing_features.clockid) {
1144 evsel->attr.use_clockid = 0;
1145 evsel->attr.clockid = 0;
1146 }
Yann Droneaud57480d22014-06-30 22:28:47 +02001147 if (perf_missing_features.cloexec)
1148 flags &= ~(unsigned long)PERF_FLAG_FD_CLOEXEC;
Stephane Eranian5c5e8542013-08-21 12:10:25 +02001149 if (perf_missing_features.mmap2)
1150 evsel->attr.mmap2 = 0;
Arnaldo Carvalho de Melo594ac612012-12-13 13:13:07 -03001151 if (perf_missing_features.exclude_guest)
1152 evsel->attr.exclude_guest = evsel->attr.exclude_host = 0;
1153retry_sample_id:
1154 if (perf_missing_features.sample_id_all)
1155 evsel->attr.sample_id_all = 0;
1156
Peter Zijlstra2c5e8c52015-04-07 11:09:54 +02001157 if (verbose >= 2) {
1158 fprintf(stderr, "%.60s\n", graph_dotted_line);
1159 fprintf(stderr, "perf_event_attr:\n");
1160 perf_event_attr__fprintf(stderr, &evsel->attr, __open_attr__fprintf, NULL);
1161 fprintf(stderr, "%.60s\n", graph_dotted_line);
1162 }
Adrian Huntere3e1a542013-08-14 15:48:24 +03001163
Arnaldo Carvalho de Melo86bd5e82011-01-03 23:09:46 -02001164 for (cpu = 0; cpu < cpus->nr; cpu++) {
Arnaldo Carvalho de Melo9d04f172011-01-12 00:08:18 -02001165
Adrian Hunterbf8e8f42014-07-31 09:00:51 +03001166 for (thread = 0; thread < nthreads; thread++) {
Jiri Olsa6a4bb042012-08-08 12:22:36 +02001167 int group_fd;
Stephane Eranian023695d2011-02-14 11:20:01 +02001168
Adrian Hunterbf8e8f42014-07-31 09:00:51 +03001169 if (!evsel->cgrp && !evsel->system_wide)
Stephane Eranian023695d2011-02-14 11:20:01 +02001170 pid = threads->map[thread];
1171
Jiri Olsa6a4bb042012-08-08 12:22:36 +02001172 group_fd = get_group_fd(evsel, cpu, thread);
Andi Kleenbec19672013-08-04 19:41:26 -07001173retry_open:
Ramkumar Ramachandraa33f6ef2014-03-18 15:10:42 -04001174 pr_debug2("sys_perf_event_open: pid %d cpu %d group_fd %d flags %#lx\n",
Adrian Huntere3e1a542013-08-14 15:48:24 +03001175 pid, cpus->map[cpu], group_fd, flags);
1176
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -02001177 FD(evsel, cpu, thread) = sys_perf_event_open(&evsel->attr,
Stephane Eranian023695d2011-02-14 11:20:01 +02001178 pid,
Arnaldo Carvalho de Melof08199d2011-01-11 23:42:19 -02001179 cpus->map[cpu],
Stephane Eranian023695d2011-02-14 11:20:01 +02001180 group_fd, flags);
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -02001181 if (FD(evsel, cpu, thread) < 0) {
1182 err = -errno;
Ramkumar Ramachandraa33f6ef2014-03-18 15:10:42 -04001183 pr_debug2("sys_perf_event_open failed, error %d\n",
Adrian Hunterf852fd62013-11-01 15:51:29 +02001184 err);
Arnaldo Carvalho de Melo594ac612012-12-13 13:13:07 -03001185 goto try_fallback;
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -02001186 }
Andi Kleenbec19672013-08-04 19:41:26 -07001187 set_rlimit = NO_CHANGE;
Peter Zijlstra814c8c32015-03-31 00:19:31 +02001188
1189 /*
1190 * If we succeeded but had to kill clockid, fail and
1191 * have perf_evsel__open_strerror() print us a nice
1192 * error.
1193 */
1194 if (perf_missing_features.clockid ||
1195 perf_missing_features.clockid_wrong) {
1196 err = -EINVAL;
1197 goto out_close;
1198 }
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -02001199 }
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -02001200 }
1201
1202 return 0;
1203
Arnaldo Carvalho de Melo594ac612012-12-13 13:13:07 -03001204try_fallback:
Andi Kleenbec19672013-08-04 19:41:26 -07001205 /*
1206 * perf stat needs between 5 and 22 fds per CPU. When we run out
1207 * of them try to increase the limits.
1208 */
1209 if (err == -EMFILE && set_rlimit < INCREASED_MAX) {
1210 struct rlimit l;
1211 int old_errno = errno;
1212
1213 if (getrlimit(RLIMIT_NOFILE, &l) == 0) {
1214 if (set_rlimit == NO_CHANGE)
1215 l.rlim_cur = l.rlim_max;
1216 else {
1217 l.rlim_cur = l.rlim_max + 1000;
1218 l.rlim_max = l.rlim_cur;
1219 }
1220 if (setrlimit(RLIMIT_NOFILE, &l) == 0) {
1221 set_rlimit++;
1222 errno = old_errno;
1223 goto retry_open;
1224 }
1225 }
1226 errno = old_errno;
1227 }
1228
Arnaldo Carvalho de Melo594ac612012-12-13 13:13:07 -03001229 if (err != -EINVAL || cpu > 0 || thread > 0)
1230 goto out_close;
1231
Peter Zijlstra814c8c32015-03-31 00:19:31 +02001232 /*
1233 * Must probe features in the order they were added to the
1234 * perf_event_attr interface.
1235 */
1236 if (!perf_missing_features.clockid_wrong && evsel->attr.use_clockid) {
1237 perf_missing_features.clockid_wrong = true;
1238 goto fallback_missing_features;
1239 } else if (!perf_missing_features.clockid && evsel->attr.use_clockid) {
1240 perf_missing_features.clockid = true;
1241 goto fallback_missing_features;
1242 } else if (!perf_missing_features.cloexec && (flags & PERF_FLAG_FD_CLOEXEC)) {
Yann Droneaud57480d22014-06-30 22:28:47 +02001243 perf_missing_features.cloexec = true;
1244 goto fallback_missing_features;
1245 } else if (!perf_missing_features.mmap2 && evsel->attr.mmap2) {
Stephane Eranian5c5e8542013-08-21 12:10:25 +02001246 perf_missing_features.mmap2 = true;
1247 goto fallback_missing_features;
1248 } else if (!perf_missing_features.exclude_guest &&
1249 (evsel->attr.exclude_guest || evsel->attr.exclude_host)) {
Arnaldo Carvalho de Melo594ac612012-12-13 13:13:07 -03001250 perf_missing_features.exclude_guest = true;
1251 goto fallback_missing_features;
1252 } else if (!perf_missing_features.sample_id_all) {
1253 perf_missing_features.sample_id_all = true;
1254 goto retry_sample_id;
1255 }
1256
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -02001257out_close:
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -02001258 do {
1259 while (--thread >= 0) {
1260 close(FD(evsel, cpu, thread));
1261 FD(evsel, cpu, thread) = -1;
1262 }
Adrian Hunterbf8e8f42014-07-31 09:00:51 +03001263 thread = nthreads;
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -02001264 } while (--cpu >= 0);
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -02001265 return err;
1266}
1267
1268void perf_evsel__close(struct perf_evsel *evsel, int ncpus, int nthreads)
1269{
1270 if (evsel->fd == NULL)
1271 return;
1272
1273 perf_evsel__close_fd(evsel, ncpus, nthreads);
1274 perf_evsel__free_fd(evsel);
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -02001275}
1276
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -02001277static struct {
1278 struct cpu_map map;
1279 int cpus[1];
1280} empty_cpu_map = {
1281 .map.nr = 1,
1282 .cpus = { -1, },
1283};
1284
1285static struct {
1286 struct thread_map map;
1287 int threads[1];
1288} empty_thread_map = {
1289 .map.nr = 1,
1290 .threads = { -1, },
1291};
1292
Arnaldo Carvalho de Melof08199d2011-01-11 23:42:19 -02001293int perf_evsel__open(struct perf_evsel *evsel, struct cpu_map *cpus,
Jiri Olsa6a4bb042012-08-08 12:22:36 +02001294 struct thread_map *threads)
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -02001295{
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -02001296 if (cpus == NULL) {
1297 /* Work around old compiler warnings about strict aliasing */
1298 cpus = &empty_cpu_map.map;
1299 }
1300
1301 if (threads == NULL)
1302 threads = &empty_thread_map.map;
1303
Jiri Olsa6a4bb042012-08-08 12:22:36 +02001304 return __perf_evsel__open(evsel, cpus, threads);
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -02001305}
1306
Arnaldo Carvalho de Melof08199d2011-01-11 23:42:19 -02001307int perf_evsel__open_per_cpu(struct perf_evsel *evsel,
Jiri Olsa6a4bb042012-08-08 12:22:36 +02001308 struct cpu_map *cpus)
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -02001309{
Jiri Olsa6a4bb042012-08-08 12:22:36 +02001310 return __perf_evsel__open(evsel, cpus, &empty_thread_map.map);
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -02001311}
1312
Arnaldo Carvalho de Melof08199d2011-01-11 23:42:19 -02001313int perf_evsel__open_per_thread(struct perf_evsel *evsel,
Jiri Olsa6a4bb042012-08-08 12:22:36 +02001314 struct thread_map *threads)
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -02001315{
Jiri Olsa6a4bb042012-08-08 12:22:36 +02001316 return __perf_evsel__open(evsel, &empty_cpu_map.map, threads);
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -02001317}
Arnaldo Carvalho de Melo70082dd2011-01-12 17:03:24 -02001318
Arnaldo Carvalho de Melo0807d2d2012-09-26 12:48:18 -03001319static int perf_evsel__parse_id_sample(const struct perf_evsel *evsel,
1320 const union perf_event *event,
1321 struct perf_sample *sample)
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001322{
Arnaldo Carvalho de Melo0807d2d2012-09-26 12:48:18 -03001323 u64 type = evsel->attr.sample_type;
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001324 const u64 *array = event->sample.array;
Arnaldo Carvalho de Melo0807d2d2012-09-26 12:48:18 -03001325 bool swapped = evsel->needs_swap;
Jiri Olsa37073f92012-05-30 14:23:44 +02001326 union u64_swap u;
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001327
1328 array += ((event->header.size -
1329 sizeof(event->header)) / sizeof(u64)) - 1;
1330
Adrian Hunter75562572013-08-27 11:23:09 +03001331 if (type & PERF_SAMPLE_IDENTIFIER) {
1332 sample->id = *array;
1333 array--;
1334 }
1335
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001336 if (type & PERF_SAMPLE_CPU) {
Jiri Olsa37073f92012-05-30 14:23:44 +02001337 u.val64 = *array;
1338 if (swapped) {
1339 /* undo swap of u64, then swap on individual u32s */
1340 u.val64 = bswap_64(u.val64);
1341 u.val32[0] = bswap_32(u.val32[0]);
1342 }
1343
1344 sample->cpu = u.val32[0];
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001345 array--;
1346 }
1347
1348 if (type & PERF_SAMPLE_STREAM_ID) {
1349 sample->stream_id = *array;
1350 array--;
1351 }
1352
1353 if (type & PERF_SAMPLE_ID) {
1354 sample->id = *array;
1355 array--;
1356 }
1357
1358 if (type & PERF_SAMPLE_TIME) {
1359 sample->time = *array;
1360 array--;
1361 }
1362
1363 if (type & PERF_SAMPLE_TID) {
Jiri Olsa37073f92012-05-30 14:23:44 +02001364 u.val64 = *array;
1365 if (swapped) {
1366 /* undo swap of u64, then swap on individual u32s */
1367 u.val64 = bswap_64(u.val64);
1368 u.val32[0] = bswap_32(u.val32[0]);
1369 u.val32[1] = bswap_32(u.val32[1]);
1370 }
1371
1372 sample->pid = u.val32[0];
1373 sample->tid = u.val32[1];
Adrian Hunterdd44bc62013-10-18 15:29:01 +03001374 array--;
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001375 }
1376
1377 return 0;
1378}
1379
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001380static inline bool overflow(const void *endp, u16 max_size, const void *offset,
1381 u64 size)
Frederic Weisbecker98e1da92011-05-21 20:08:15 +02001382{
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001383 return size > max_size || offset + size > endp;
Frederic Weisbecker98e1da92011-05-21 20:08:15 +02001384}
1385
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001386#define OVERFLOW_CHECK(offset, size, max_size) \
1387 do { \
1388 if (overflow(endp, (max_size), (offset), (size))) \
1389 return -EFAULT; \
1390 } while (0)
1391
1392#define OVERFLOW_CHECK_u64(offset) \
1393 OVERFLOW_CHECK(offset, sizeof(u64), sizeof(u64))
1394
Arnaldo Carvalho de Meloa3f698f2012-08-02 12:23:46 -03001395int perf_evsel__parse_sample(struct perf_evsel *evsel, union perf_event *event,
Arnaldo Carvalho de Melo0807d2d2012-09-26 12:48:18 -03001396 struct perf_sample *data)
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001397{
Arnaldo Carvalho de Meloa3f698f2012-08-02 12:23:46 -03001398 u64 type = evsel->attr.sample_type;
Arnaldo Carvalho de Melo0807d2d2012-09-26 12:48:18 -03001399 bool swapped = evsel->needs_swap;
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001400 const u64 *array;
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001401 u16 max_size = event->header.size;
1402 const void *endp = (void *)event + max_size;
1403 u64 sz;
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001404
David Ahern936be502011-09-06 09:12:26 -06001405 /*
1406 * used for cross-endian analysis. See git commit 65014ab3
1407 * for why this goofiness is needed.
1408 */
Jiri Olsa6a11f922012-05-16 08:59:04 +02001409 union u64_swap u;
David Ahern936be502011-09-06 09:12:26 -06001410
Robert Richterf3bda2c2011-12-15 17:32:39 +01001411 memset(data, 0, sizeof(*data));
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001412 data->cpu = data->pid = data->tid = -1;
1413 data->stream_id = data->id = data->time = -1ULL;
Jiri Olsabc529082014-02-03 12:44:41 +01001414 data->period = evsel->attr.sample_period;
Andi Kleen05484292013-01-24 16:10:29 +01001415 data->weight = 0;
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001416
1417 if (event->header.type != PERF_RECORD_SAMPLE) {
Arnaldo Carvalho de Meloa3f698f2012-08-02 12:23:46 -03001418 if (!evsel->attr.sample_id_all)
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001419 return 0;
Arnaldo Carvalho de Melo0807d2d2012-09-26 12:48:18 -03001420 return perf_evsel__parse_id_sample(evsel, event, data);
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001421 }
1422
1423 array = event->sample.array;
1424
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001425 /*
1426 * The evsel's sample_size is based on PERF_SAMPLE_MASK which includes
1427 * up to PERF_SAMPLE_PERIOD. After that overflow() must be used to
1428 * check the format does not go past the end of the event.
1429 */
Arnaldo Carvalho de Meloa3f698f2012-08-02 12:23:46 -03001430 if (evsel->sample_size + sizeof(event->header) > event->header.size)
Frederic Weisbeckera2854122011-05-21 19:33:04 +02001431 return -EFAULT;
1432
Adrian Hunter75562572013-08-27 11:23:09 +03001433 data->id = -1ULL;
1434 if (type & PERF_SAMPLE_IDENTIFIER) {
1435 data->id = *array;
1436 array++;
1437 }
1438
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001439 if (type & PERF_SAMPLE_IP) {
Adrian Hunteref893252013-08-27 11:23:06 +03001440 data->ip = *array;
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001441 array++;
1442 }
1443
1444 if (type & PERF_SAMPLE_TID) {
David Ahern936be502011-09-06 09:12:26 -06001445 u.val64 = *array;
1446 if (swapped) {
1447 /* undo swap of u64, then swap on individual u32s */
1448 u.val64 = bswap_64(u.val64);
1449 u.val32[0] = bswap_32(u.val32[0]);
1450 u.val32[1] = bswap_32(u.val32[1]);
1451 }
1452
1453 data->pid = u.val32[0];
1454 data->tid = u.val32[1];
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001455 array++;
1456 }
1457
1458 if (type & PERF_SAMPLE_TIME) {
1459 data->time = *array;
1460 array++;
1461 }
1462
David Ahern7cec0922011-05-30 13:08:23 -06001463 data->addr = 0;
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001464 if (type & PERF_SAMPLE_ADDR) {
1465 data->addr = *array;
1466 array++;
1467 }
1468
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001469 if (type & PERF_SAMPLE_ID) {
1470 data->id = *array;
1471 array++;
1472 }
1473
1474 if (type & PERF_SAMPLE_STREAM_ID) {
1475 data->stream_id = *array;
1476 array++;
1477 }
1478
1479 if (type & PERF_SAMPLE_CPU) {
David Ahern936be502011-09-06 09:12:26 -06001480
1481 u.val64 = *array;
1482 if (swapped) {
1483 /* undo swap of u64, then swap on individual u32s */
1484 u.val64 = bswap_64(u.val64);
1485 u.val32[0] = bswap_32(u.val32[0]);
1486 }
1487
1488 data->cpu = u.val32[0];
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001489 array++;
1490 }
1491
1492 if (type & PERF_SAMPLE_PERIOD) {
1493 data->period = *array;
1494 array++;
1495 }
1496
1497 if (type & PERF_SAMPLE_READ) {
Jiri Olsa9ede4732012-10-10 17:38:13 +02001498 u64 read_format = evsel->attr.read_format;
1499
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001500 OVERFLOW_CHECK_u64(array);
Jiri Olsa9ede4732012-10-10 17:38:13 +02001501 if (read_format & PERF_FORMAT_GROUP)
1502 data->read.group.nr = *array;
1503 else
1504 data->read.one.value = *array;
1505
1506 array++;
1507
1508 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) {
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001509 OVERFLOW_CHECK_u64(array);
Jiri Olsa9ede4732012-10-10 17:38:13 +02001510 data->read.time_enabled = *array;
1511 array++;
1512 }
1513
1514 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) {
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001515 OVERFLOW_CHECK_u64(array);
Jiri Olsa9ede4732012-10-10 17:38:13 +02001516 data->read.time_running = *array;
1517 array++;
1518 }
1519
1520 /* PERF_FORMAT_ID is forced for PERF_SAMPLE_READ */
1521 if (read_format & PERF_FORMAT_GROUP) {
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001522 const u64 max_group_nr = UINT64_MAX /
1523 sizeof(struct sample_read_value);
1524
1525 if (data->read.group.nr > max_group_nr)
1526 return -EFAULT;
1527 sz = data->read.group.nr *
1528 sizeof(struct sample_read_value);
1529 OVERFLOW_CHECK(array, sz, max_size);
1530 data->read.group.values =
1531 (struct sample_read_value *)array;
1532 array = (void *)array + sz;
Jiri Olsa9ede4732012-10-10 17:38:13 +02001533 } else {
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001534 OVERFLOW_CHECK_u64(array);
Jiri Olsa9ede4732012-10-10 17:38:13 +02001535 data->read.one.id = *array;
1536 array++;
1537 }
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001538 }
1539
1540 if (type & PERF_SAMPLE_CALLCHAIN) {
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001541 const u64 max_callchain_nr = UINT64_MAX / sizeof(u64);
1542
1543 OVERFLOW_CHECK_u64(array);
1544 data->callchain = (struct ip_callchain *)array++;
1545 if (data->callchain->nr > max_callchain_nr)
Frederic Weisbecker98e1da92011-05-21 20:08:15 +02001546 return -EFAULT;
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001547 sz = data->callchain->nr * sizeof(u64);
1548 OVERFLOW_CHECK(array, sz, max_size);
1549 array = (void *)array + sz;
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001550 }
1551
1552 if (type & PERF_SAMPLE_RAW) {
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001553 OVERFLOW_CHECK_u64(array);
David Ahern936be502011-09-06 09:12:26 -06001554 u.val64 = *array;
1555 if (WARN_ONCE(swapped,
1556 "Endianness of raw data not corrected!\n")) {
1557 /* undo swap of u64, then swap on individual u32s */
1558 u.val64 = bswap_64(u.val64);
1559 u.val32[0] = bswap_32(u.val32[0]);
1560 u.val32[1] = bswap_32(u.val32[1]);
1561 }
David Ahern936be502011-09-06 09:12:26 -06001562 data->raw_size = u.val32[0];
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001563 array = (void *)array + sizeof(u32);
Frederic Weisbecker98e1da92011-05-21 20:08:15 +02001564
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001565 OVERFLOW_CHECK(array, data->raw_size, max_size);
1566 data->raw_data = (void *)array;
1567 array = (void *)array + data->raw_size;
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001568 }
1569
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +01001570 if (type & PERF_SAMPLE_BRANCH_STACK) {
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001571 const u64 max_branch_nr = UINT64_MAX /
1572 sizeof(struct branch_entry);
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +01001573
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001574 OVERFLOW_CHECK_u64(array);
1575 data->branch_stack = (struct branch_stack *)array++;
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +01001576
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001577 if (data->branch_stack->nr > max_branch_nr)
1578 return -EFAULT;
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +01001579 sz = data->branch_stack->nr * sizeof(struct branch_entry);
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001580 OVERFLOW_CHECK(array, sz, max_size);
1581 array = (void *)array + sz;
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +01001582 }
Jiri Olsa0f6a3012012-08-07 15:20:45 +02001583
1584 if (type & PERF_SAMPLE_REGS_USER) {
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001585 OVERFLOW_CHECK_u64(array);
Adrian Hunter5b95a4a32013-08-27 11:23:10 +03001586 data->user_regs.abi = *array;
1587 array++;
Jiri Olsa0f6a3012012-08-07 15:20:45 +02001588
Adrian Hunter5b95a4a32013-08-27 11:23:10 +03001589 if (data->user_regs.abi) {
Jiri Olsa352ea452014-01-07 13:47:25 +01001590 u64 mask = evsel->attr.sample_regs_user;
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001591
Jiri Olsa352ea452014-01-07 13:47:25 +01001592 sz = hweight_long(mask) * sizeof(u64);
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001593 OVERFLOW_CHECK(array, sz, max_size);
Jiri Olsa352ea452014-01-07 13:47:25 +01001594 data->user_regs.mask = mask;
Jiri Olsa0f6a3012012-08-07 15:20:45 +02001595 data->user_regs.regs = (u64 *)array;
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001596 array = (void *)array + sz;
Jiri Olsa0f6a3012012-08-07 15:20:45 +02001597 }
1598 }
1599
1600 if (type & PERF_SAMPLE_STACK_USER) {
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001601 OVERFLOW_CHECK_u64(array);
1602 sz = *array++;
Jiri Olsa0f6a3012012-08-07 15:20:45 +02001603
1604 data->user_stack.offset = ((char *)(array - 1)
1605 - (char *) event);
1606
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001607 if (!sz) {
Jiri Olsa0f6a3012012-08-07 15:20:45 +02001608 data->user_stack.size = 0;
1609 } else {
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001610 OVERFLOW_CHECK(array, sz, max_size);
Jiri Olsa0f6a3012012-08-07 15:20:45 +02001611 data->user_stack.data = (char *)array;
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001612 array = (void *)array + sz;
1613 OVERFLOW_CHECK_u64(array);
Adrian Hunter54bd2692013-07-04 16:20:34 +03001614 data->user_stack.size = *array++;
Jiri Olsaa65cb4b2013-10-02 15:46:39 +02001615 if (WARN_ONCE(data->user_stack.size > sz,
1616 "user stack dump failure\n"))
1617 return -EFAULT;
Jiri Olsa0f6a3012012-08-07 15:20:45 +02001618 }
1619 }
1620
Andi Kleen05484292013-01-24 16:10:29 +01001621 data->weight = 0;
1622 if (type & PERF_SAMPLE_WEIGHT) {
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001623 OVERFLOW_CHECK_u64(array);
Andi Kleen05484292013-01-24 16:10:29 +01001624 data->weight = *array;
1625 array++;
1626 }
1627
Stephane Eranian98a3b322013-01-24 16:10:35 +01001628 data->data_src = PERF_MEM_DATA_SRC_NONE;
1629 if (type & PERF_SAMPLE_DATA_SRC) {
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001630 OVERFLOW_CHECK_u64(array);
Stephane Eranian98a3b322013-01-24 16:10:35 +01001631 data->data_src = *array;
1632 array++;
1633 }
1634
Andi Kleen475eeab2013-09-20 07:40:43 -07001635 data->transaction = 0;
1636 if (type & PERF_SAMPLE_TRANSACTION) {
Adrian Hunter87b95522013-11-01 15:51:36 +02001637 OVERFLOW_CHECK_u64(array);
Andi Kleen475eeab2013-09-20 07:40:43 -07001638 data->transaction = *array;
1639 array++;
1640 }
1641
Stephane Eranian6a21c0b2014-09-24 13:48:39 +02001642 data->intr_regs.abi = PERF_SAMPLE_REGS_ABI_NONE;
1643 if (type & PERF_SAMPLE_REGS_INTR) {
1644 OVERFLOW_CHECK_u64(array);
1645 data->intr_regs.abi = *array;
1646 array++;
1647
1648 if (data->intr_regs.abi != PERF_SAMPLE_REGS_ABI_NONE) {
1649 u64 mask = evsel->attr.sample_regs_intr;
1650
1651 sz = hweight_long(mask) * sizeof(u64);
1652 OVERFLOW_CHECK(array, sz, max_size);
1653 data->intr_regs.mask = mask;
1654 data->intr_regs.regs = (u64 *)array;
1655 array = (void *)array + sz;
1656 }
1657 }
1658
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001659 return 0;
1660}
Andrew Vagin74eec262011-11-28 12:03:31 +03001661
Adrian Hunterb1cf6f62013-08-27 11:23:12 +03001662size_t perf_event__sample_event_size(const struct perf_sample *sample, u64 type,
Jiri Olsa352ea452014-01-07 13:47:25 +01001663 u64 read_format)
Adrian Hunterb1cf6f62013-08-27 11:23:12 +03001664{
1665 size_t sz, result = sizeof(struct sample_event);
1666
1667 if (type & PERF_SAMPLE_IDENTIFIER)
1668 result += sizeof(u64);
1669
1670 if (type & PERF_SAMPLE_IP)
1671 result += sizeof(u64);
1672
1673 if (type & PERF_SAMPLE_TID)
1674 result += sizeof(u64);
1675
1676 if (type & PERF_SAMPLE_TIME)
1677 result += sizeof(u64);
1678
1679 if (type & PERF_SAMPLE_ADDR)
1680 result += sizeof(u64);
1681
1682 if (type & PERF_SAMPLE_ID)
1683 result += sizeof(u64);
1684
1685 if (type & PERF_SAMPLE_STREAM_ID)
1686 result += sizeof(u64);
1687
1688 if (type & PERF_SAMPLE_CPU)
1689 result += sizeof(u64);
1690
1691 if (type & PERF_SAMPLE_PERIOD)
1692 result += sizeof(u64);
1693
1694 if (type & PERF_SAMPLE_READ) {
1695 result += sizeof(u64);
1696 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
1697 result += sizeof(u64);
1698 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
1699 result += sizeof(u64);
1700 /* PERF_FORMAT_ID is forced for PERF_SAMPLE_READ */
1701 if (read_format & PERF_FORMAT_GROUP) {
1702 sz = sample->read.group.nr *
1703 sizeof(struct sample_read_value);
1704 result += sz;
1705 } else {
1706 result += sizeof(u64);
1707 }
1708 }
1709
1710 if (type & PERF_SAMPLE_CALLCHAIN) {
1711 sz = (sample->callchain->nr + 1) * sizeof(u64);
1712 result += sz;
1713 }
1714
1715 if (type & PERF_SAMPLE_RAW) {
1716 result += sizeof(u32);
1717 result += sample->raw_size;
1718 }
1719
1720 if (type & PERF_SAMPLE_BRANCH_STACK) {
1721 sz = sample->branch_stack->nr * sizeof(struct branch_entry);
1722 sz += sizeof(u64);
1723 result += sz;
1724 }
1725
1726 if (type & PERF_SAMPLE_REGS_USER) {
1727 if (sample->user_regs.abi) {
1728 result += sizeof(u64);
Jiri Olsa352ea452014-01-07 13:47:25 +01001729 sz = hweight_long(sample->user_regs.mask) * sizeof(u64);
Adrian Hunterb1cf6f62013-08-27 11:23:12 +03001730 result += sz;
1731 } else {
1732 result += sizeof(u64);
1733 }
1734 }
1735
1736 if (type & PERF_SAMPLE_STACK_USER) {
1737 sz = sample->user_stack.size;
1738 result += sizeof(u64);
1739 if (sz) {
1740 result += sz;
1741 result += sizeof(u64);
1742 }
1743 }
1744
1745 if (type & PERF_SAMPLE_WEIGHT)
1746 result += sizeof(u64);
1747
1748 if (type & PERF_SAMPLE_DATA_SRC)
1749 result += sizeof(u64);
1750
Adrian Hunter42d88912013-11-01 15:51:38 +02001751 if (type & PERF_SAMPLE_TRANSACTION)
1752 result += sizeof(u64);
1753
Stephane Eranian6a21c0b2014-09-24 13:48:39 +02001754 if (type & PERF_SAMPLE_REGS_INTR) {
1755 if (sample->intr_regs.abi) {
1756 result += sizeof(u64);
1757 sz = hweight_long(sample->intr_regs.mask) * sizeof(u64);
1758 result += sz;
1759 } else {
1760 result += sizeof(u64);
1761 }
1762 }
1763
Adrian Hunterb1cf6f62013-08-27 11:23:12 +03001764 return result;
1765}
1766
Andrew Vagin74eec262011-11-28 12:03:31 +03001767int perf_event__synthesize_sample(union perf_event *event, u64 type,
Jiri Olsa352ea452014-01-07 13:47:25 +01001768 u64 read_format,
Andrew Vagin74eec262011-11-28 12:03:31 +03001769 const struct perf_sample *sample,
1770 bool swapped)
1771{
1772 u64 *array;
Adrian Hunterd03f2172013-08-27 11:23:11 +03001773 size_t sz;
Andrew Vagin74eec262011-11-28 12:03:31 +03001774 /*
1775 * used for cross-endian analysis. See git commit 65014ab3
1776 * for why this goofiness is needed.
1777 */
Jiri Olsa6a11f922012-05-16 08:59:04 +02001778 union u64_swap u;
Andrew Vagin74eec262011-11-28 12:03:31 +03001779
1780 array = event->sample.array;
1781
Adrian Hunter75562572013-08-27 11:23:09 +03001782 if (type & PERF_SAMPLE_IDENTIFIER) {
1783 *array = sample->id;
1784 array++;
1785 }
1786
Andrew Vagin74eec262011-11-28 12:03:31 +03001787 if (type & PERF_SAMPLE_IP) {
Adrian Hunteref893252013-08-27 11:23:06 +03001788 *array = sample->ip;
Andrew Vagin74eec262011-11-28 12:03:31 +03001789 array++;
1790 }
1791
1792 if (type & PERF_SAMPLE_TID) {
1793 u.val32[0] = sample->pid;
1794 u.val32[1] = sample->tid;
1795 if (swapped) {
1796 /*
Arnaldo Carvalho de Meloa3f698f2012-08-02 12:23:46 -03001797 * Inverse of what is done in perf_evsel__parse_sample
Andrew Vagin74eec262011-11-28 12:03:31 +03001798 */
1799 u.val32[0] = bswap_32(u.val32[0]);
1800 u.val32[1] = bswap_32(u.val32[1]);
1801 u.val64 = bswap_64(u.val64);
1802 }
1803
1804 *array = u.val64;
1805 array++;
1806 }
1807
1808 if (type & PERF_SAMPLE_TIME) {
1809 *array = sample->time;
1810 array++;
1811 }
1812
1813 if (type & PERF_SAMPLE_ADDR) {
1814 *array = sample->addr;
1815 array++;
1816 }
1817
1818 if (type & PERF_SAMPLE_ID) {
1819 *array = sample->id;
1820 array++;
1821 }
1822
1823 if (type & PERF_SAMPLE_STREAM_ID) {
1824 *array = sample->stream_id;
1825 array++;
1826 }
1827
1828 if (type & PERF_SAMPLE_CPU) {
1829 u.val32[0] = sample->cpu;
1830 if (swapped) {
1831 /*
Arnaldo Carvalho de Meloa3f698f2012-08-02 12:23:46 -03001832 * Inverse of what is done in perf_evsel__parse_sample
Andrew Vagin74eec262011-11-28 12:03:31 +03001833 */
1834 u.val32[0] = bswap_32(u.val32[0]);
1835 u.val64 = bswap_64(u.val64);
1836 }
1837 *array = u.val64;
1838 array++;
1839 }
1840
1841 if (type & PERF_SAMPLE_PERIOD) {
1842 *array = sample->period;
1843 array++;
1844 }
1845
Adrian Hunterd03f2172013-08-27 11:23:11 +03001846 if (type & PERF_SAMPLE_READ) {
1847 if (read_format & PERF_FORMAT_GROUP)
1848 *array = sample->read.group.nr;
1849 else
1850 *array = sample->read.one.value;
1851 array++;
1852
1853 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) {
1854 *array = sample->read.time_enabled;
1855 array++;
1856 }
1857
1858 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) {
1859 *array = sample->read.time_running;
1860 array++;
1861 }
1862
1863 /* PERF_FORMAT_ID is forced for PERF_SAMPLE_READ */
1864 if (read_format & PERF_FORMAT_GROUP) {
1865 sz = sample->read.group.nr *
1866 sizeof(struct sample_read_value);
1867 memcpy(array, sample->read.group.values, sz);
1868 array = (void *)array + sz;
1869 } else {
1870 *array = sample->read.one.id;
1871 array++;
1872 }
1873 }
1874
1875 if (type & PERF_SAMPLE_CALLCHAIN) {
1876 sz = (sample->callchain->nr + 1) * sizeof(u64);
1877 memcpy(array, sample->callchain, sz);
1878 array = (void *)array + sz;
1879 }
1880
1881 if (type & PERF_SAMPLE_RAW) {
1882 u.val32[0] = sample->raw_size;
1883 if (WARN_ONCE(swapped,
1884 "Endianness of raw data not corrected!\n")) {
1885 /*
1886 * Inverse of what is done in perf_evsel__parse_sample
1887 */
1888 u.val32[0] = bswap_32(u.val32[0]);
1889 u.val32[1] = bswap_32(u.val32[1]);
1890 u.val64 = bswap_64(u.val64);
1891 }
1892 *array = u.val64;
1893 array = (void *)array + sizeof(u32);
1894
1895 memcpy(array, sample->raw_data, sample->raw_size);
1896 array = (void *)array + sample->raw_size;
1897 }
1898
1899 if (type & PERF_SAMPLE_BRANCH_STACK) {
1900 sz = sample->branch_stack->nr * sizeof(struct branch_entry);
1901 sz += sizeof(u64);
1902 memcpy(array, sample->branch_stack, sz);
1903 array = (void *)array + sz;
1904 }
1905
1906 if (type & PERF_SAMPLE_REGS_USER) {
1907 if (sample->user_regs.abi) {
1908 *array++ = sample->user_regs.abi;
Jiri Olsa352ea452014-01-07 13:47:25 +01001909 sz = hweight_long(sample->user_regs.mask) * sizeof(u64);
Adrian Hunterd03f2172013-08-27 11:23:11 +03001910 memcpy(array, sample->user_regs.regs, sz);
1911 array = (void *)array + sz;
1912 } else {
1913 *array++ = 0;
1914 }
1915 }
1916
1917 if (type & PERF_SAMPLE_STACK_USER) {
1918 sz = sample->user_stack.size;
1919 *array++ = sz;
1920 if (sz) {
1921 memcpy(array, sample->user_stack.data, sz);
1922 array = (void *)array + sz;
1923 *array++ = sz;
1924 }
1925 }
1926
1927 if (type & PERF_SAMPLE_WEIGHT) {
1928 *array = sample->weight;
1929 array++;
1930 }
1931
1932 if (type & PERF_SAMPLE_DATA_SRC) {
1933 *array = sample->data_src;
1934 array++;
1935 }
1936
Adrian Hunter42d88912013-11-01 15:51:38 +02001937 if (type & PERF_SAMPLE_TRANSACTION) {
1938 *array = sample->transaction;
1939 array++;
1940 }
1941
Stephane Eranian6a21c0b2014-09-24 13:48:39 +02001942 if (type & PERF_SAMPLE_REGS_INTR) {
1943 if (sample->intr_regs.abi) {
1944 *array++ = sample->intr_regs.abi;
1945 sz = hweight_long(sample->intr_regs.mask) * sizeof(u64);
1946 memcpy(array, sample->intr_regs.regs, sz);
1947 array = (void *)array + sz;
1948 } else {
1949 *array++ = 0;
1950 }
1951 }
1952
Andrew Vagin74eec262011-11-28 12:03:31 +03001953 return 0;
1954}
Arnaldo Carvalho de Melo5555ded2012-09-11 19:24:23 -03001955
Arnaldo Carvalho de Meloefd2b922012-09-18 11:21:50 -03001956struct format_field *perf_evsel__field(struct perf_evsel *evsel, const char *name)
1957{
1958 return pevent_find_field(evsel->tp_format, name);
1959}
1960
Arnaldo Carvalho de Melo5d2074e2012-09-26 20:22:00 -03001961void *perf_evsel__rawptr(struct perf_evsel *evsel, struct perf_sample *sample,
Arnaldo Carvalho de Melo5555ded2012-09-11 19:24:23 -03001962 const char *name)
1963{
Arnaldo Carvalho de Meloefd2b922012-09-18 11:21:50 -03001964 struct format_field *field = perf_evsel__field(evsel, name);
Arnaldo Carvalho de Melo5555ded2012-09-11 19:24:23 -03001965 int offset;
1966
Arnaldo Carvalho de Meloefd2b922012-09-18 11:21:50 -03001967 if (!field)
1968 return NULL;
Arnaldo Carvalho de Melo5555ded2012-09-11 19:24:23 -03001969
1970 offset = field->offset;
1971
1972 if (field->flags & FIELD_IS_DYNAMIC) {
1973 offset = *(int *)(sample->raw_data + field->offset);
1974 offset &= 0xffff;
1975 }
1976
1977 return sample->raw_data + offset;
1978}
1979
1980u64 perf_evsel__intval(struct perf_evsel *evsel, struct perf_sample *sample,
1981 const char *name)
1982{
Arnaldo Carvalho de Meloefd2b922012-09-18 11:21:50 -03001983 struct format_field *field = perf_evsel__field(evsel, name);
Arnaldo Carvalho de Meloe6b6f672012-09-26 13:13:04 -03001984 void *ptr;
1985 u64 value;
Arnaldo Carvalho de Melo5555ded2012-09-11 19:24:23 -03001986
Arnaldo Carvalho de Meloefd2b922012-09-18 11:21:50 -03001987 if (!field)
1988 return 0;
Arnaldo Carvalho de Melo5555ded2012-09-11 19:24:23 -03001989
Arnaldo Carvalho de Meloe6b6f672012-09-26 13:13:04 -03001990 ptr = sample->raw_data + field->offset;
Arnaldo Carvalho de Melo5555ded2012-09-11 19:24:23 -03001991
Arnaldo Carvalho de Meloe6b6f672012-09-26 13:13:04 -03001992 switch (field->size) {
1993 case 1:
1994 return *(u8 *)ptr;
1995 case 2:
1996 value = *(u16 *)ptr;
1997 break;
1998 case 4:
1999 value = *(u32 *)ptr;
2000 break;
2001 case 8:
David Aherne94eeda2015-03-24 16:14:09 -04002002 memcpy(&value, ptr, sizeof(u64));
Arnaldo Carvalho de Meloe6b6f672012-09-26 13:13:04 -03002003 break;
2004 default:
2005 return 0;
2006 }
2007
2008 if (!evsel->needs_swap)
2009 return value;
2010
2011 switch (field->size) {
2012 case 2:
2013 return bswap_16(value);
2014 case 4:
2015 return bswap_32(value);
2016 case 8:
2017 return bswap_64(value);
2018 default:
2019 return 0;
2020 }
2021
2022 return 0;
Arnaldo Carvalho de Melo5555ded2012-09-11 19:24:23 -03002023}
Arnaldo Carvalho de Melo0698aed2012-12-10 18:17:08 -03002024
2025static int comma_fprintf(FILE *fp, bool *first, const char *fmt, ...)
2026{
2027 va_list args;
2028 int ret = 0;
2029
2030 if (!*first) {
2031 ret += fprintf(fp, ",");
2032 } else {
2033 ret += fprintf(fp, ":");
2034 *first = false;
2035 }
2036
2037 va_start(args, fmt);
2038 ret += vfprintf(fp, fmt, args);
2039 va_end(args);
2040 return ret;
2041}
2042
Peter Zijlstra2c5e8c52015-04-07 11:09:54 +02002043static int __print_attr__fprintf(FILE *fp, const char *name, const char *val, void *priv)
Arnaldo Carvalho de Melo0698aed2012-12-10 18:17:08 -03002044{
Peter Zijlstra2c5e8c52015-04-07 11:09:54 +02002045 return comma_fprintf(fp, (bool *)priv, " %s: %s", name, val);
Arnaldo Carvalho de Meloc79a4392012-12-11 10:54:12 -03002046}
2047
Arnaldo Carvalho de Melo0698aed2012-12-10 18:17:08 -03002048int perf_evsel__fprintf(struct perf_evsel *evsel,
2049 struct perf_attr_details *details, FILE *fp)
2050{
2051 bool first = true;
Namhyung Kime6ab07d2013-01-22 18:09:47 +09002052 int printed = 0;
2053
Arnaldo Carvalho de Meloe35ef3552013-02-06 17:20:02 -03002054 if (details->event_group) {
Namhyung Kime6ab07d2013-01-22 18:09:47 +09002055 struct perf_evsel *pos;
2056
2057 if (!perf_evsel__is_group_leader(evsel))
2058 return 0;
2059
2060 if (evsel->nr_members > 1)
2061 printed += fprintf(fp, "%s{", evsel->group_name ?: "");
2062
2063 printed += fprintf(fp, "%s", perf_evsel__name(evsel));
2064 for_each_group_member(pos, evsel)
2065 printed += fprintf(fp, ",%s", perf_evsel__name(pos));
2066
2067 if (evsel->nr_members > 1)
2068 printed += fprintf(fp, "}");
2069 goto out;
2070 }
2071
2072 printed += fprintf(fp, "%s", perf_evsel__name(evsel));
Arnaldo Carvalho de Melo0698aed2012-12-10 18:17:08 -03002073
Peter Zijlstra2c5e8c52015-04-07 11:09:54 +02002074 if (details->verbose) {
2075 printed += perf_event_attr__fprintf(fp, &evsel->attr,
2076 __print_attr__fprintf, &first);
2077 } else if (details->freq) {
Arnaldo Carvalho de Melo0698aed2012-12-10 18:17:08 -03002078 printed += comma_fprintf(fp, &first, " sample_freq=%" PRIu64,
2079 (u64)evsel->attr.sample_freq);
2080 }
Namhyung Kime6ab07d2013-01-22 18:09:47 +09002081out:
Arnaldo Carvalho de Melo0698aed2012-12-10 18:17:08 -03002082 fputc('\n', fp);
2083 return ++printed;
2084}
Arnaldo Carvalho de Meloc0a54342012-12-13 14:16:30 -03002085
2086bool perf_evsel__fallback(struct perf_evsel *evsel, int err,
2087 char *msg, size_t msgsize)
2088{
David Ahern2b821cc2013-07-18 17:27:59 -06002089 if ((err == ENOENT || err == ENXIO || err == ENODEV) &&
Arnaldo Carvalho de Meloc0a54342012-12-13 14:16:30 -03002090 evsel->attr.type == PERF_TYPE_HARDWARE &&
2091 evsel->attr.config == PERF_COUNT_HW_CPU_CYCLES) {
2092 /*
2093 * If it's cycles then fall back to hrtimer based
2094 * cpu-clock-tick sw counter, which is always available even if
2095 * no PMU support.
2096 *
2097 * PPC returns ENXIO until 2.6.37 (behavior changed with commit
2098 * b0a873e).
2099 */
2100 scnprintf(msg, msgsize, "%s",
2101"The cycles event is not supported, trying to fall back to cpu-clock-ticks");
2102
2103 evsel->attr.type = PERF_TYPE_SOFTWARE;
2104 evsel->attr.config = PERF_COUNT_SW_CPU_CLOCK;
2105
Arnaldo Carvalho de Melo04662522013-12-26 17:41:15 -03002106 zfree(&evsel->name);
Arnaldo Carvalho de Meloc0a54342012-12-13 14:16:30 -03002107 return true;
2108 }
2109
2110 return false;
2111}
Arnaldo Carvalho de Melo56e52e82012-12-13 15:10:58 -03002112
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03002113int perf_evsel__open_strerror(struct perf_evsel *evsel, struct target *target,
Arnaldo Carvalho de Melo56e52e82012-12-13 15:10:58 -03002114 int err, char *msg, size_t size)
2115{
Masami Hiramatsu6e81c742014-08-14 02:22:36 +00002116 char sbuf[STRERR_BUFSIZE];
2117
Arnaldo Carvalho de Melo56e52e82012-12-13 15:10:58 -03002118 switch (err) {
2119 case EPERM:
2120 case EACCES:
David Ahernb69e63a2013-05-25 17:54:00 -06002121 return scnprintf(msg, size,
Arnaldo Carvalho de Melo56e52e82012-12-13 15:10:58 -03002122 "You may not have permission to collect %sstats.\n"
2123 "Consider tweaking /proc/sys/kernel/perf_event_paranoid:\n"
2124 " -1 - Not paranoid at all\n"
2125 " 0 - Disallow raw tracepoint access for unpriv\n"
2126 " 1 - Disallow cpu events for unpriv\n"
2127 " 2 - Disallow kernel profiling for unpriv",
2128 target->system_wide ? "system-wide " : "");
2129 case ENOENT:
2130 return scnprintf(msg, size, "The %s event is not supported.",
2131 perf_evsel__name(evsel));
2132 case EMFILE:
2133 return scnprintf(msg, size, "%s",
2134 "Too many events are opened.\n"
Jiri Olsa18ffdfe2015-05-25 22:51:54 +02002135 "Probably the maximum number of open file descriptors has been reached.\n"
2136 "Hint: Try again after reducing the number of events.\n"
2137 "Hint: Try increasing the limit with 'ulimit -n <limit>'");
Arnaldo Carvalho de Melo56e52e82012-12-13 15:10:58 -03002138 case ENODEV:
2139 if (target->cpu_list)
2140 return scnprintf(msg, size, "%s",
2141 "No such device - did you specify an out-of-range profile CPU?\n");
2142 break;
2143 case EOPNOTSUPP:
2144 if (evsel->attr.precise_ip)
2145 return scnprintf(msg, size, "%s",
2146 "\'precise\' request may not be supported. Try removing 'p' modifier.");
2147#if defined(__i386__) || defined(__x86_64__)
2148 if (evsel->attr.type == PERF_TYPE_HARDWARE)
2149 return scnprintf(msg, size, "%s",
2150 "No hardware sampling interrupt available.\n"
2151 "No APIC? If so then you can boot the kernel with the \"lapic\" boot parameter to force-enable it.");
2152#endif
2153 break;
Jiri Olsa63914ac2014-08-01 17:46:54 +02002154 case EBUSY:
2155 if (find_process("oprofiled"))
2156 return scnprintf(msg, size,
2157 "The PMU counters are busy/taken by another profiler.\n"
2158 "We found oprofile daemon running, please stop it and try again.");
2159 break;
Peter Zijlstra814c8c32015-03-31 00:19:31 +02002160 case EINVAL:
2161 if (perf_missing_features.clockid)
2162 return scnprintf(msg, size, "clockid feature not supported.");
2163 if (perf_missing_features.clockid_wrong)
2164 return scnprintf(msg, size, "wrong clockid (%d).", clockid);
2165 break;
Arnaldo Carvalho de Melo56e52e82012-12-13 15:10:58 -03002166 default:
2167 break;
2168 }
2169
2170 return scnprintf(msg, size,
Masami Hiramatsu6e81c742014-08-14 02:22:36 +00002171 "The sys_perf_event_open() syscall returned with %d (%s) for event (%s).\n"
Arnaldo Carvalho de Melo56e52e82012-12-13 15:10:58 -03002172 "/bin/dmesg may provide additional information.\n"
2173 "No CONFIG_PERF_EVENTS=y kernel support configured?\n",
Masami Hiramatsu6e81c742014-08-14 02:22:36 +00002174 err, strerror_r(err, sbuf, sizeof(sbuf)),
2175 perf_evsel__name(evsel));
Arnaldo Carvalho de Melo56e52e82012-12-13 15:10:58 -03002176}