blob: f664a22b5fea8af7dc1a5a7f00a5ec4d3f3b3431 [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);
Jiri Olsa930a2e22015-07-29 05:42:10 -0400210 INIT_LIST_HEAD(&evsel->config_terms);
Arnaldo Carvalho de Meloce8ccff2014-10-09 15:29:51 -0300211 perf_evsel__object.init(evsel);
Arnaldo Carvalho de Melobde09462012-08-01 18:53:11 -0300212 evsel->sample_size = __perf_evsel__sample_size(attr->sample_type);
Adrian Hunter75562572013-08-27 11:23:09 +0300213 perf_evsel__calc_id_pos(evsel);
Wang Nan15bfd2c2015-07-10 07:36:09 +0000214 evsel->cmdline_group_boundary = false;
Arnaldo Carvalho de Meloef1d1af2011-01-18 21:41:45 -0200215}
216
Arnaldo Carvalho de Meloef503832013-11-07 16:41:19 -0300217struct perf_evsel *perf_evsel__new_idx(struct perf_event_attr *attr, int idx)
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200218{
Arnaldo Carvalho de Meloce8ccff2014-10-09 15:29:51 -0300219 struct perf_evsel *evsel = zalloc(perf_evsel__object.size);
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200220
Arnaldo Carvalho de Meloef1d1af2011-01-18 21:41:45 -0200221 if (evsel != NULL)
222 perf_evsel__init(evsel, attr, idx);
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200223
224 return evsel;
225}
226
Arnaldo Carvalho de Meloef503832013-11-07 16:41:19 -0300227struct perf_evsel *perf_evsel__newtp_idx(const char *sys, const char *name, int idx)
Arnaldo Carvalho de Meloefd2b922012-09-18 11:21:50 -0300228{
Arnaldo Carvalho de Meloce8ccff2014-10-09 15:29:51 -0300229 struct perf_evsel *evsel = zalloc(perf_evsel__object.size);
Arnaldo Carvalho de Meloefd2b922012-09-18 11:21:50 -0300230
231 if (evsel != NULL) {
232 struct perf_event_attr attr = {
Arnaldo Carvalho de Melo0b80f8b32012-09-26 12:28:26 -0300233 .type = PERF_TYPE_TRACEPOINT,
234 .sample_type = (PERF_SAMPLE_RAW | PERF_SAMPLE_TIME |
235 PERF_SAMPLE_CPU | PERF_SAMPLE_PERIOD),
Arnaldo Carvalho de Meloefd2b922012-09-18 11:21:50 -0300236 };
237
Arnaldo Carvalho de Meloe48ffe22012-09-26 17:11:38 -0300238 if (asprintf(&evsel->name, "%s:%s", sys, name) < 0)
239 goto out_free;
240
Jiri Olsa97978b32013-12-03 14:09:24 +0100241 evsel->tp_format = trace_event__tp_format(sys, name);
Arnaldo Carvalho de Meloefd2b922012-09-18 11:21:50 -0300242 if (evsel->tp_format == NULL)
243 goto out_free;
244
Arnaldo Carvalho de Melo0b80f8b32012-09-26 12:28:26 -0300245 event_attr_init(&attr);
Arnaldo Carvalho de Meloefd2b922012-09-18 11:21:50 -0300246 attr.config = evsel->tp_format->id;
Arnaldo Carvalho de Melo0b80f8b32012-09-26 12:28:26 -0300247 attr.sample_period = 1;
Arnaldo Carvalho de Meloefd2b922012-09-18 11:21:50 -0300248 perf_evsel__init(evsel, &attr, idx);
Arnaldo Carvalho de Meloefd2b922012-09-18 11:21:50 -0300249 }
250
251 return evsel;
252
253out_free:
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -0300254 zfree(&evsel->name);
Arnaldo Carvalho de Meloefd2b922012-09-18 11:21:50 -0300255 free(evsel);
256 return NULL;
257}
258
Arnaldo Carvalho de Melo8ad70132012-09-06 13:11:18 -0300259const char *perf_evsel__hw_names[PERF_COUNT_HW_MAX] = {
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300260 "cycles",
261 "instructions",
262 "cache-references",
263 "cache-misses",
264 "branches",
265 "branch-misses",
266 "bus-cycles",
267 "stalled-cycles-frontend",
268 "stalled-cycles-backend",
269 "ref-cycles",
270};
271
Arnaldo Carvalho de Melodd4f5222012-06-13 15:52:42 -0300272static const char *__perf_evsel__hw_name(u64 config)
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300273{
274 if (config < PERF_COUNT_HW_MAX && perf_evsel__hw_names[config])
275 return perf_evsel__hw_names[config];
276
277 return "unknown-hardware";
278}
279
Arnaldo Carvalho de Melo27f18612012-06-11 13:33:09 -0300280static int perf_evsel__add_modifiers(struct perf_evsel *evsel, char *bf, size_t size)
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300281{
Arnaldo Carvalho de Melo27f18612012-06-11 13:33:09 -0300282 int colon = 0, r = 0;
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300283 struct perf_event_attr *attr = &evsel->attr;
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300284 bool exclude_guest_default = false;
285
286#define MOD_PRINT(context, mod) do { \
287 if (!attr->exclude_##context) { \
Arnaldo Carvalho de Melo27f18612012-06-11 13:33:09 -0300288 if (!colon) colon = ++r; \
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300289 r += scnprintf(bf + r, size - r, "%c", mod); \
290 } } while(0)
291
292 if (attr->exclude_kernel || attr->exclude_user || attr->exclude_hv) {
293 MOD_PRINT(kernel, 'k');
294 MOD_PRINT(user, 'u');
295 MOD_PRINT(hv, 'h');
296 exclude_guest_default = true;
297 }
298
299 if (attr->precise_ip) {
300 if (!colon)
Arnaldo Carvalho de Melo27f18612012-06-11 13:33:09 -0300301 colon = ++r;
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300302 r += scnprintf(bf + r, size - r, "%.*s", attr->precise_ip, "ppp");
303 exclude_guest_default = true;
304 }
305
306 if (attr->exclude_host || attr->exclude_guest == exclude_guest_default) {
307 MOD_PRINT(host, 'H');
308 MOD_PRINT(guest, 'G');
309 }
310#undef MOD_PRINT
311 if (colon)
Arnaldo Carvalho de Melo27f18612012-06-11 13:33:09 -0300312 bf[colon - 1] = ':';
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300313 return r;
314}
315
Arnaldo Carvalho de Melo27f18612012-06-11 13:33:09 -0300316static int perf_evsel__hw_name(struct perf_evsel *evsel, char *bf, size_t size)
317{
318 int r = scnprintf(bf, size, "%s", __perf_evsel__hw_name(evsel->attr.config));
319 return r + perf_evsel__add_modifiers(evsel, bf + r, size - r);
320}
321
Arnaldo Carvalho de Melo8ad70132012-09-06 13:11:18 -0300322const char *perf_evsel__sw_names[PERF_COUNT_SW_MAX] = {
Arnaldo Carvalho de Melo335c2f52012-06-11 14:36:20 -0300323 "cpu-clock",
324 "task-clock",
325 "page-faults",
326 "context-switches",
Arnaldo Carvalho de Melo8ad70132012-09-06 13:11:18 -0300327 "cpu-migrations",
Arnaldo Carvalho de Melo335c2f52012-06-11 14:36:20 -0300328 "minor-faults",
329 "major-faults",
330 "alignment-faults",
331 "emulation-faults",
Adrian Hunterd22d1a22013-08-31 21:50:52 +0300332 "dummy",
Arnaldo Carvalho de Melo335c2f52012-06-11 14:36:20 -0300333};
334
Arnaldo Carvalho de Melodd4f5222012-06-13 15:52:42 -0300335static const char *__perf_evsel__sw_name(u64 config)
Arnaldo Carvalho de Melo335c2f52012-06-11 14:36:20 -0300336{
337 if (config < PERF_COUNT_SW_MAX && perf_evsel__sw_names[config])
338 return perf_evsel__sw_names[config];
339 return "unknown-software";
340}
341
342static int perf_evsel__sw_name(struct perf_evsel *evsel, char *bf, size_t size)
343{
344 int r = scnprintf(bf, size, "%s", __perf_evsel__sw_name(evsel->attr.config));
345 return r + perf_evsel__add_modifiers(evsel, bf + r, size - r);
346}
347
Jiri Olsa287e74a2012-06-28 23:18:49 +0200348static int __perf_evsel__bp_name(char *bf, size_t size, u64 addr, u64 type)
349{
350 int r;
351
352 r = scnprintf(bf, size, "mem:0x%" PRIx64 ":", addr);
353
354 if (type & HW_BREAKPOINT_R)
355 r += scnprintf(bf + r, size - r, "r");
356
357 if (type & HW_BREAKPOINT_W)
358 r += scnprintf(bf + r, size - r, "w");
359
360 if (type & HW_BREAKPOINT_X)
361 r += scnprintf(bf + r, size - r, "x");
362
363 return r;
364}
365
366static int perf_evsel__bp_name(struct perf_evsel *evsel, char *bf, size_t size)
367{
368 struct perf_event_attr *attr = &evsel->attr;
369 int r = __perf_evsel__bp_name(bf, size, attr->bp_addr, attr->bp_type);
370 return r + perf_evsel__add_modifiers(evsel, bf + r, size - r);
371}
372
Arnaldo Carvalho de Melo0b668bc2012-06-11 14:08:07 -0300373const char *perf_evsel__hw_cache[PERF_COUNT_HW_CACHE_MAX]
374 [PERF_EVSEL__MAX_ALIASES] = {
375 { "L1-dcache", "l1-d", "l1d", "L1-data", },
376 { "L1-icache", "l1-i", "l1i", "L1-instruction", },
377 { "LLC", "L2", },
378 { "dTLB", "d-tlb", "Data-TLB", },
379 { "iTLB", "i-tlb", "Instruction-TLB", },
380 { "branch", "branches", "bpu", "btb", "bpc", },
381 { "node", },
382};
383
384const char *perf_evsel__hw_cache_op[PERF_COUNT_HW_CACHE_OP_MAX]
385 [PERF_EVSEL__MAX_ALIASES] = {
386 { "load", "loads", "read", },
387 { "store", "stores", "write", },
388 { "prefetch", "prefetches", "speculative-read", "speculative-load", },
389};
390
391const char *perf_evsel__hw_cache_result[PERF_COUNT_HW_CACHE_RESULT_MAX]
392 [PERF_EVSEL__MAX_ALIASES] = {
393 { "refs", "Reference", "ops", "access", },
394 { "misses", "miss", },
395};
396
397#define C(x) PERF_COUNT_HW_CACHE_##x
398#define CACHE_READ (1 << C(OP_READ))
399#define CACHE_WRITE (1 << C(OP_WRITE))
400#define CACHE_PREFETCH (1 << C(OP_PREFETCH))
401#define COP(x) (1 << x)
402
403/*
404 * cache operartion stat
405 * L1I : Read and prefetch only
406 * ITLB and BPU : Read-only
407 */
408static unsigned long perf_evsel__hw_cache_stat[C(MAX)] = {
409 [C(L1D)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
410 [C(L1I)] = (CACHE_READ | CACHE_PREFETCH),
411 [C(LL)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
412 [C(DTLB)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
413 [C(ITLB)] = (CACHE_READ),
414 [C(BPU)] = (CACHE_READ),
415 [C(NODE)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
416};
417
418bool perf_evsel__is_cache_op_valid(u8 type, u8 op)
419{
420 if (perf_evsel__hw_cache_stat[type] & COP(op))
421 return true; /* valid */
422 else
423 return false; /* invalid */
424}
425
426int __perf_evsel__hw_cache_type_op_res_name(u8 type, u8 op, u8 result,
427 char *bf, size_t size)
428{
429 if (result) {
430 return scnprintf(bf, size, "%s-%s-%s", perf_evsel__hw_cache[type][0],
431 perf_evsel__hw_cache_op[op][0],
432 perf_evsel__hw_cache_result[result][0]);
433 }
434
435 return scnprintf(bf, size, "%s-%s", perf_evsel__hw_cache[type][0],
436 perf_evsel__hw_cache_op[op][1]);
437}
438
Arnaldo Carvalho de Melodd4f5222012-06-13 15:52:42 -0300439static int __perf_evsel__hw_cache_name(u64 config, char *bf, size_t size)
Arnaldo Carvalho de Melo0b668bc2012-06-11 14:08:07 -0300440{
441 u8 op, result, type = (config >> 0) & 0xff;
442 const char *err = "unknown-ext-hardware-cache-type";
443
444 if (type > PERF_COUNT_HW_CACHE_MAX)
445 goto out_err;
446
447 op = (config >> 8) & 0xff;
448 err = "unknown-ext-hardware-cache-op";
449 if (op > PERF_COUNT_HW_CACHE_OP_MAX)
450 goto out_err;
451
452 result = (config >> 16) & 0xff;
453 err = "unknown-ext-hardware-cache-result";
454 if (result > PERF_COUNT_HW_CACHE_RESULT_MAX)
455 goto out_err;
456
457 err = "invalid-cache";
458 if (!perf_evsel__is_cache_op_valid(type, op))
459 goto out_err;
460
461 return __perf_evsel__hw_cache_type_op_res_name(type, op, result, bf, size);
462out_err:
463 return scnprintf(bf, size, "%s", err);
464}
465
466static int perf_evsel__hw_cache_name(struct perf_evsel *evsel, char *bf, size_t size)
467{
468 int ret = __perf_evsel__hw_cache_name(evsel->attr.config, bf, size);
469 return ret + perf_evsel__add_modifiers(evsel, bf + ret, size - ret);
470}
471
Arnaldo Carvalho de Melo6eef3d92012-06-13 11:53:37 -0300472static int perf_evsel__raw_name(struct perf_evsel *evsel, char *bf, size_t size)
473{
474 int ret = scnprintf(bf, size, "raw 0x%" PRIx64, evsel->attr.config);
475 return ret + perf_evsel__add_modifiers(evsel, bf + ret, size - ret);
476}
477
Arnaldo Carvalho de Melo7289f832012-06-12 12:34:58 -0300478const char *perf_evsel__name(struct perf_evsel *evsel)
Arnaldo Carvalho de Meloa4460832012-06-12 10:29:12 -0300479{
Arnaldo Carvalho de Melo7289f832012-06-12 12:34:58 -0300480 char bf[128];
Arnaldo Carvalho de Meloa4460832012-06-12 10:29:12 -0300481
Arnaldo Carvalho de Melo7289f832012-06-12 12:34:58 -0300482 if (evsel->name)
483 return evsel->name;
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300484
485 switch (evsel->attr.type) {
486 case PERF_TYPE_RAW:
Arnaldo Carvalho de Melo6eef3d92012-06-13 11:53:37 -0300487 perf_evsel__raw_name(evsel, bf, sizeof(bf));
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300488 break;
489
490 case PERF_TYPE_HARDWARE:
Arnaldo Carvalho de Melo7289f832012-06-12 12:34:58 -0300491 perf_evsel__hw_name(evsel, bf, sizeof(bf));
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300492 break;
Arnaldo Carvalho de Melo0b668bc2012-06-11 14:08:07 -0300493
494 case PERF_TYPE_HW_CACHE:
Arnaldo Carvalho de Melo7289f832012-06-12 12:34:58 -0300495 perf_evsel__hw_cache_name(evsel, bf, sizeof(bf));
Arnaldo Carvalho de Melo0b668bc2012-06-11 14:08:07 -0300496 break;
497
Arnaldo Carvalho de Melo335c2f52012-06-11 14:36:20 -0300498 case PERF_TYPE_SOFTWARE:
Arnaldo Carvalho de Melo7289f832012-06-12 12:34:58 -0300499 perf_evsel__sw_name(evsel, bf, sizeof(bf));
Arnaldo Carvalho de Melo335c2f52012-06-11 14:36:20 -0300500 break;
501
Arnaldo Carvalho de Meloa4460832012-06-12 10:29:12 -0300502 case PERF_TYPE_TRACEPOINT:
Arnaldo Carvalho de Melo7289f832012-06-12 12:34:58 -0300503 scnprintf(bf, sizeof(bf), "%s", "unknown tracepoint");
Arnaldo Carvalho de Meloa4460832012-06-12 10:29:12 -0300504 break;
505
Jiri Olsa287e74a2012-06-28 23:18:49 +0200506 case PERF_TYPE_BREAKPOINT:
507 perf_evsel__bp_name(evsel, bf, sizeof(bf));
508 break;
509
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300510 default:
Robert Richterca1b1452012-08-16 21:10:18 +0200511 scnprintf(bf, sizeof(bf), "unknown attr type: %d",
512 evsel->attr.type);
Arnaldo Carvalho de Meloa4460832012-06-12 10:29:12 -0300513 break;
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300514 }
515
Arnaldo Carvalho de Melo7289f832012-06-12 12:34:58 -0300516 evsel->name = strdup(bf);
517
518 return evsel->name ?: "unknown";
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300519}
520
Namhyung Kim717e2632013-01-22 18:09:44 +0900521const char *perf_evsel__group_name(struct perf_evsel *evsel)
522{
523 return evsel->group_name ?: "anon group";
524}
525
526int perf_evsel__group_desc(struct perf_evsel *evsel, char *buf, size_t size)
527{
528 int ret;
529 struct perf_evsel *pos;
530 const char *group_name = perf_evsel__group_name(evsel);
531
532 ret = scnprintf(buf, size, "%s", group_name);
533
534 ret += scnprintf(buf + ret, size - ret, " { %s",
535 perf_evsel__name(evsel));
536
537 for_each_group_member(pos, evsel)
538 ret += scnprintf(buf + ret, size - ret, ", %s",
539 perf_evsel__name(pos));
540
541 ret += scnprintf(buf + ret, size - ret, " }");
542
543 return ret;
544}
545
Jiri Olsa6bedfab2014-03-02 16:56:40 +0100546static void
Kan Liangaad2b212015-01-05 13:23:04 -0500547perf_evsel__config_callgraph(struct perf_evsel *evsel,
Kan Liangc3a6a8c2015-08-04 04:30:20 -0400548 struct record_opts *opts,
549 struct callchain_param *param)
Jiri Olsa6bedfab2014-03-02 16:56:40 +0100550{
551 bool function = perf_evsel__is_function_event(evsel);
552 struct perf_event_attr *attr = &evsel->attr;
553
554 perf_evsel__set_sample_bit(evsel, CALLCHAIN);
555
Kan Liangc3a6a8c2015-08-04 04:30:20 -0400556 if (param->record_mode == CALLCHAIN_LBR) {
Kan Liangaad2b212015-01-05 13:23:04 -0500557 if (!opts->branch_stack) {
558 if (attr->exclude_user) {
559 pr_warning("LBR callstack option is only available "
560 "to get user callchain information. "
561 "Falling back to framepointers.\n");
562 } else {
563 perf_evsel__set_sample_bit(evsel, BRANCH_STACK);
564 attr->branch_sample_type = PERF_SAMPLE_BRANCH_USER |
565 PERF_SAMPLE_BRANCH_CALL_STACK;
566 }
567 } else
568 pr_warning("Cannot use LBR callstack with branch stack. "
569 "Falling back to framepointers.\n");
570 }
571
Kan Liangc3a6a8c2015-08-04 04:30:20 -0400572 if (param->record_mode == CALLCHAIN_DWARF) {
Jiri Olsa6bedfab2014-03-02 16:56:40 +0100573 if (!function) {
574 perf_evsel__set_sample_bit(evsel, REGS_USER);
575 perf_evsel__set_sample_bit(evsel, STACK_USER);
576 attr->sample_regs_user = PERF_REGS_MASK;
Kan Liangc3a6a8c2015-08-04 04:30:20 -0400577 attr->sample_stack_user = param->dump_size;
Jiri Olsa6bedfab2014-03-02 16:56:40 +0100578 attr->exclude_callchain_user = 1;
579 } else {
580 pr_info("Cannot use DWARF unwind for function trace event,"
581 " falling back to framepointers.\n");
582 }
583 }
584
585 if (function) {
586 pr_info("Disabling user space callchains for function trace event.\n");
587 attr->exclude_callchain_user = 1;
588 }
589}
590
Kan Liang32067712015-08-04 04:30:19 -0400591static void apply_config_terms(struct perf_evsel *evsel)
Jiri Olsa930a2e22015-07-29 05:42:10 -0400592{
593 struct perf_evsel_config_term *term;
Kan Liang32067712015-08-04 04:30:19 -0400594 struct list_head *config_terms = &evsel->config_terms;
595 struct perf_event_attr *attr = &evsel->attr;
Jiri Olsa930a2e22015-07-29 05:42:10 -0400596
597 list_for_each_entry(term, config_terms, list) {
598 switch (term->type) {
Jiri Olsaee4c7582015-07-29 05:42:11 -0400599 case PERF_EVSEL__CONFIG_TERM_PERIOD:
600 attr->sample_period = term->val.period;
Jiri Olsaab35a7d2015-08-08 19:12:10 +0200601 attr->freq = 0;
Kan Liang32067712015-08-04 04:30:19 -0400602 break;
Namhyung Kim09af2a52015-08-09 15:45:23 +0900603 case PERF_EVSEL__CONFIG_TERM_FREQ:
604 attr->sample_freq = term->val.freq;
605 attr->freq = 1;
606 break;
Kan Liang32067712015-08-04 04:30:19 -0400607 case PERF_EVSEL__CONFIG_TERM_TIME:
608 if (term->val.time)
609 perf_evsel__set_sample_bit(evsel, TIME);
610 else
611 perf_evsel__reset_sample_bit(evsel, TIME);
612 break;
Jiri Olsa930a2e22015-07-29 05:42:10 -0400613 default:
614 break;
615 }
616 }
617}
618
Jiri Olsa774cb492012-11-12 18:34:01 +0100619/*
620 * The enable_on_exec/disabled value strategy:
621 *
622 * 1) For any type of traced program:
623 * - all independent events and group leaders are disabled
624 * - all group members are enabled
625 *
626 * Group members are ruled by group leaders. They need to
627 * be enabled, because the group scheduling relies on that.
628 *
629 * 2) For traced programs executed by perf:
630 * - all independent events and group leaders have
631 * enable_on_exec set
632 * - we don't specifically enable or disable any event during
633 * the record command
634 *
635 * Independent events and group leaders are initially disabled
636 * and get enabled by exec. Group members are ruled by group
637 * leaders as stated in 1).
638 *
639 * 3) For traced programs attached by perf (pid/tid):
640 * - we specifically enable or disable all events during
641 * the record command
642 *
643 * When attaching events to already running traced we
644 * enable/disable events specifically, as there's no
645 * initial traced exec call.
646 */
Arnaldo Carvalho de Melob4006792013-12-19 14:43:45 -0300647void perf_evsel__config(struct perf_evsel *evsel, struct record_opts *opts)
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200648{
Jiri Olsa3c176312012-10-10 17:39:03 +0200649 struct perf_evsel *leader = evsel->leader;
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200650 struct perf_event_attr *attr = &evsel->attr;
Adrian Hunter60b08962014-07-31 09:00:52 +0300651 int track = evsel->tracking;
Adrian Hunter3aa59392013-11-15 15:52:29 +0200652 bool per_cpu = opts->target.default_per_cpu && !opts->target.per_thread;
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200653
Arnaldo Carvalho de Melo594ac612012-12-13 13:13:07 -0300654 attr->sample_id_all = perf_missing_features.sample_id_all ? 0 : 1;
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200655 attr->inherit = !opts->no_inherit;
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200656
Arnaldo Carvalho de Melo7be5ebe2012-12-10 14:53:43 -0300657 perf_evsel__set_sample_bit(evsel, IP);
658 perf_evsel__set_sample_bit(evsel, TID);
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200659
Jiri Olsa3c176312012-10-10 17:39:03 +0200660 if (evsel->sample_read) {
661 perf_evsel__set_sample_bit(evsel, READ);
662
663 /*
664 * We need ID even in case of single event, because
665 * PERF_SAMPLE_READ process ID specific data.
666 */
Adrian Hunter75562572013-08-27 11:23:09 +0300667 perf_evsel__set_sample_id(evsel, false);
Jiri Olsa3c176312012-10-10 17:39:03 +0200668
669 /*
670 * Apply group format only if we belong to group
671 * with more than one members.
672 */
673 if (leader->nr_members > 1) {
674 attr->read_format |= PERF_FORMAT_GROUP;
675 attr->inherit = 0;
676 }
677 }
678
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200679 /*
Namhyung Kim17314e22014-06-09 14:43:37 +0900680 * We default some events to have a default interval. But keep
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200681 * it a weak assumption overridable by the user.
682 */
Namhyung Kim17314e22014-06-09 14:43:37 +0900683 if (!attr->sample_period || (opts->user_freq != UINT_MAX ||
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200684 opts->user_interval != ULLONG_MAX)) {
685 if (opts->freq) {
Arnaldo Carvalho de Melo7be5ebe2012-12-10 14:53:43 -0300686 perf_evsel__set_sample_bit(evsel, PERIOD);
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200687 attr->freq = 1;
688 attr->sample_freq = opts->freq;
689 } else {
690 attr->sample_period = opts->default_interval;
691 }
692 }
693
Jiri Olsa3c176312012-10-10 17:39:03 +0200694 /*
695 * Disable sampling for all group members other
696 * than leader in case leader 'leads' the sampling.
697 */
698 if ((leader != evsel) && leader->sample_read) {
699 attr->sample_freq = 0;
700 attr->sample_period = 0;
701 }
702
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200703 if (opts->no_samples)
704 attr->sample_freq = 0;
705
706 if (opts->inherit_stat)
707 attr->inherit_stat = 1;
708
709 if (opts->sample_address) {
Arnaldo Carvalho de Melo7be5ebe2012-12-10 14:53:43 -0300710 perf_evsel__set_sample_bit(evsel, ADDR);
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200711 attr->mmap_data = track;
712 }
713
Jiri Olsaf1403732014-11-13 18:21:03 +0100714 /*
715 * We don't allow user space callchains for function trace
716 * event, due to issues with page faults while tracing page
717 * fault handler and its overall trickiness nature.
718 */
719 if (perf_evsel__is_function_event(evsel))
720 evsel->attr.exclude_callchain_user = 1;
721
Namhyung Kim72a128a2014-09-23 10:01:41 +0900722 if (callchain_param.enabled && !evsel->no_aux_samples)
Kan Liangc3a6a8c2015-08-04 04:30:20 -0400723 perf_evsel__config_callgraph(evsel, opts, &callchain_param);
Jiri Olsa26d33022012-08-07 15:20:47 +0200724
Stephane Eranian6a21c0b2014-09-24 13:48:39 +0200725 if (opts->sample_intr_regs) {
726 attr->sample_regs_intr = PERF_REGS_MASK;
727 perf_evsel__set_sample_bit(evsel, REGS_INTR);
728 }
729
Adrian Hunter3aa59392013-11-15 15:52:29 +0200730 if (target__has_cpu(&opts->target))
Arnaldo Carvalho de Melo7be5ebe2012-12-10 14:53:43 -0300731 perf_evsel__set_sample_bit(evsel, CPU);
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200732
Andrew Vagin3e76ac72011-12-20 17:32:45 +0300733 if (opts->period)
Arnaldo Carvalho de Melo7be5ebe2012-12-10 14:53:43 -0300734 perf_evsel__set_sample_bit(evsel, PERIOD);
Andrew Vagin3e76ac72011-12-20 17:32:45 +0300735
Andi Kleen8affc2b2014-07-31 14:45:04 +0800736 /*
737 * When the user explicitely disabled time don't force it here.
738 */
739 if (opts->sample_time &&
740 (!perf_missing_features.sample_id_all &&
Adrian Hunter3abebc52015-07-06 14:51:01 +0300741 (!opts->no_inherit || target__has_cpu(&opts->target) || per_cpu ||
742 opts->sample_time_set)))
Arnaldo Carvalho de Melo7be5ebe2012-12-10 14:53:43 -0300743 perf_evsel__set_sample_bit(evsel, TIME);
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200744
Adrian Hunter6ff1ce72014-07-14 13:02:56 +0300745 if (opts->raw_samples && !evsel->no_aux_samples) {
Arnaldo Carvalho de Melo7be5ebe2012-12-10 14:53:43 -0300746 perf_evsel__set_sample_bit(evsel, TIME);
747 perf_evsel__set_sample_bit(evsel, RAW);
748 perf_evsel__set_sample_bit(evsel, CPU);
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200749 }
750
Stephane Eranianccf49bf2013-01-24 16:10:37 +0100751 if (opts->sample_address)
Adrian Hunter1e7ed5e2013-11-01 15:51:35 +0200752 perf_evsel__set_sample_bit(evsel, DATA_SRC);
Stephane Eranianccf49bf2013-01-24 16:10:37 +0100753
Arnaldo Carvalho de Melo509051e2014-01-14 17:52:14 -0300754 if (opts->no_buffering) {
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200755 attr->watermark = 0;
756 attr->wakeup_events = 1;
757 }
Adrian Hunter6ff1ce72014-07-14 13:02:56 +0300758 if (opts->branch_stack && !evsel->no_aux_samples) {
Arnaldo Carvalho de Melo7be5ebe2012-12-10 14:53:43 -0300759 perf_evsel__set_sample_bit(evsel, BRANCH_STACK);
Roberto Agostino Vitillobdfebd82012-02-09 23:21:02 +0100760 attr->branch_sample_type = opts->branch_stack;
761 }
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200762
Andi Kleen05484292013-01-24 16:10:29 +0100763 if (opts->sample_weight)
Adrian Hunter1e7ed5e2013-11-01 15:51:35 +0200764 perf_evsel__set_sample_bit(evsel, WEIGHT);
Andi Kleen05484292013-01-24 16:10:29 +0100765
Namhyung Kim62e503b2015-01-29 17:06:46 +0900766 attr->task = track;
Stephane Eranian5c5e8542013-08-21 12:10:25 +0200767 attr->mmap = track;
Don Zickusa5a5ba72014-05-30 10:49:42 -0400768 attr->mmap2 = track && !perf_missing_features.mmap2;
Stephane Eranian5c5e8542013-08-21 12:10:25 +0200769 attr->comm = track;
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200770
Adrian Hunterb757bb02015-07-21 12:44:04 +0300771 if (opts->record_switch_events)
772 attr->context_switch = track;
773
Andi Kleen475eeab2013-09-20 07:40:43 -0700774 if (opts->sample_transaction)
Adrian Hunter1e7ed5e2013-11-01 15:51:35 +0200775 perf_evsel__set_sample_bit(evsel, TRANSACTION);
Andi Kleen475eeab2013-09-20 07:40:43 -0700776
Andi Kleen85c273d2015-02-24 15:13:40 -0800777 if (opts->running_time) {
778 evsel->attr.read_format |=
779 PERF_FORMAT_TOTAL_TIME_ENABLED |
780 PERF_FORMAT_TOTAL_TIME_RUNNING;
781 }
782
Jiri Olsa774cb492012-11-12 18:34:01 +0100783 /*
784 * XXX see the function comment above
785 *
786 * Disabling only independent events or group leaders,
787 * keeping group members enabled.
788 */
Namhyung Kim823254e2012-11-29 15:38:30 +0900789 if (perf_evsel__is_group_leader(evsel))
Jiri Olsa774cb492012-11-12 18:34:01 +0100790 attr->disabled = 1;
791
792 /*
793 * Setting enable_on_exec for independent events and
794 * group leaders for traced executed by perf.
795 */
Andi Kleen6619a532014-01-11 13:38:27 -0800796 if (target__none(&opts->target) && perf_evsel__is_group_leader(evsel) &&
797 !opts->initial_delay)
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200798 attr->enable_on_exec = 1;
Adrian Hunter2afd2bc2014-07-14 13:02:57 +0300799
800 if (evsel->immediate) {
801 attr->disabled = 0;
802 attr->enable_on_exec = 0;
803 }
Peter Zijlstra814c8c32015-03-31 00:19:31 +0200804
805 clockid = opts->clockid;
806 if (opts->use_clockid) {
807 attr->use_clockid = 1;
808 attr->clockid = opts->clockid;
809 }
Jiri Olsa930a2e22015-07-29 05:42:10 -0400810
811 /*
812 * Apply event specific term settings,
813 * it overloads any global configuration.
814 */
Kan Liang32067712015-08-04 04:30:19 -0400815 apply_config_terms(evsel);
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200816}
817
Arnaldo Carvalho de Melo88858462014-10-13 13:30:27 -0300818static int perf_evsel__alloc_fd(struct perf_evsel *evsel, int ncpus, int nthreads)
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200819{
David Ahern4af4c952011-05-27 09:58:34 -0600820 int cpu, thread;
Adrian Hunterbf8e8f42014-07-31 09:00:51 +0300821
822 if (evsel->system_wide)
823 nthreads = 1;
824
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200825 evsel->fd = xyarray__new(ncpus, nthreads, sizeof(int));
David Ahern4af4c952011-05-27 09:58:34 -0600826
827 if (evsel->fd) {
828 for (cpu = 0; cpu < ncpus; cpu++) {
829 for (thread = 0; thread < nthreads; thread++) {
830 FD(evsel, cpu, thread) = -1;
831 }
832 }
833 }
834
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200835 return evsel->fd != NULL ? 0 : -ENOMEM;
836}
837
Andi Kleene2407be2013-08-02 17:41:10 -0700838static int perf_evsel__run_ioctl(struct perf_evsel *evsel, int ncpus, int nthreads,
839 int ioc, void *arg)
Arnaldo Carvalho de Melo745cefc2012-09-26 15:07:39 -0300840{
841 int cpu, thread;
842
Adrian Hunterbf8e8f42014-07-31 09:00:51 +0300843 if (evsel->system_wide)
844 nthreads = 1;
845
Arnaldo Carvalho de Melo745cefc2012-09-26 15:07:39 -0300846 for (cpu = 0; cpu < ncpus; cpu++) {
847 for (thread = 0; thread < nthreads; thread++) {
848 int fd = FD(evsel, cpu, thread),
Andi Kleene2407be2013-08-02 17:41:10 -0700849 err = ioctl(fd, ioc, arg);
Arnaldo Carvalho de Melo745cefc2012-09-26 15:07:39 -0300850
851 if (err)
852 return err;
853 }
854 }
855
856 return 0;
857}
858
Arnaldo Carvalho de Melof47805a2015-07-03 15:53:49 -0300859int perf_evsel__apply_filter(struct perf_evsel *evsel, int ncpus, int nthreads,
860 const char *filter)
Andi Kleene2407be2013-08-02 17:41:10 -0700861{
862 return perf_evsel__run_ioctl(evsel, ncpus, nthreads,
863 PERF_EVENT_IOC_SET_FILTER,
864 (void *)filter);
865}
866
Arnaldo Carvalho de Melo12467ae2015-07-03 17:05:50 -0300867int perf_evsel__set_filter(struct perf_evsel *evsel, const char *filter)
868{
869 char *new_filter = strdup(filter);
870
871 if (new_filter != NULL) {
872 free(evsel->filter);
873 evsel->filter = new_filter;
874 return 0;
875 }
876
877 return -1;
878}
879
Arnaldo Carvalho de Melo64ec84f2015-07-04 12:19:13 -0300880int perf_evsel__append_filter(struct perf_evsel *evsel,
881 const char *op, const char *filter)
882{
883 char *new_filter;
884
885 if (evsel->filter == NULL)
886 return perf_evsel__set_filter(evsel, filter);
887
888 if (asprintf(&new_filter,"(%s) %s (%s)", evsel->filter, op, filter) > 0) {
889 free(evsel->filter);
890 evsel->filter = new_filter;
891 return 0;
892 }
893
894 return -1;
895}
896
Andi Kleene2407be2013-08-02 17:41:10 -0700897int perf_evsel__enable(struct perf_evsel *evsel, int ncpus, int nthreads)
898{
899 return perf_evsel__run_ioctl(evsel, ncpus, nthreads,
900 PERF_EVENT_IOC_ENABLE,
901 0);
902}
903
Arnaldo Carvalho de Melo70db7532011-01-12 22:39:13 -0200904int perf_evsel__alloc_id(struct perf_evsel *evsel, int ncpus, int nthreads)
905{
Vineet Gupta8d9cbd82015-01-13 19:13:23 +0530906 if (ncpus == 0 || nthreads == 0)
907 return 0;
908
Adrian Hunterbf8e8f42014-07-31 09:00:51 +0300909 if (evsel->system_wide)
910 nthreads = 1;
911
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -0300912 evsel->sample_id = xyarray__new(ncpus, nthreads, sizeof(struct perf_sample_id));
913 if (evsel->sample_id == NULL)
914 return -ENOMEM;
915
916 evsel->id = zalloc(ncpus * nthreads * sizeof(u64));
917 if (evsel->id == NULL) {
918 xyarray__delete(evsel->sample_id);
919 evsel->sample_id = NULL;
920 return -ENOMEM;
921 }
922
923 return 0;
Arnaldo Carvalho de Melo70db7532011-01-12 22:39:13 -0200924}
925
Arnaldo Carvalho de Melo88858462014-10-13 13:30:27 -0300926static void perf_evsel__free_fd(struct perf_evsel *evsel)
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200927{
928 xyarray__delete(evsel->fd);
929 evsel->fd = NULL;
930}
931
Arnaldo Carvalho de Melo88858462014-10-13 13:30:27 -0300932static void perf_evsel__free_id(struct perf_evsel *evsel)
Arnaldo Carvalho de Melo70db7532011-01-12 22:39:13 -0200933{
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -0300934 xyarray__delete(evsel->sample_id);
935 evsel->sample_id = NULL;
Arnaldo Carvalho de Melo04662522013-12-26 17:41:15 -0300936 zfree(&evsel->id);
Arnaldo Carvalho de Melo70db7532011-01-12 22:39:13 -0200937}
938
Jiri Olsa930a2e22015-07-29 05:42:10 -0400939static void perf_evsel__free_config_terms(struct perf_evsel *evsel)
940{
941 struct perf_evsel_config_term *term, *h;
942
943 list_for_each_entry_safe(term, h, &evsel->config_terms, list) {
944 list_del(&term->list);
945 free(term);
946 }
947}
948
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200949void perf_evsel__close_fd(struct perf_evsel *evsel, int ncpus, int nthreads)
950{
951 int cpu, thread;
952
Adrian Hunterbf8e8f42014-07-31 09:00:51 +0300953 if (evsel->system_wide)
954 nthreads = 1;
955
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200956 for (cpu = 0; cpu < ncpus; cpu++)
957 for (thread = 0; thread < nthreads; ++thread) {
958 close(FD(evsel, cpu, thread));
959 FD(evsel, cpu, thread) = -1;
960 }
961}
962
Arnaldo Carvalho de Meloef1d1af2011-01-18 21:41:45 -0200963void perf_evsel__exit(struct perf_evsel *evsel)
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200964{
965 assert(list_empty(&evsel->node));
Namhyung Kim736b05a2013-03-15 14:48:49 +0900966 perf_evsel__free_fd(evsel);
967 perf_evsel__free_id(evsel);
Jiri Olsa930a2e22015-07-29 05:42:10 -0400968 perf_evsel__free_config_terms(evsel);
Arnaldo Carvalho de Melo597e48c2014-10-16 13:25:01 -0300969 close_cgroup(evsel->cgrp);
Jiri Olsaf30a79b2015-06-23 00:36:04 +0200970 cpu_map__put(evsel->cpus);
Jiri Olsa578e91e2015-06-23 00:36:07 +0200971 thread_map__put(evsel->threads);
Arnaldo Carvalho de Melo597e48c2014-10-16 13:25:01 -0300972 zfree(&evsel->group_name);
Arnaldo Carvalho de Melo597e48c2014-10-16 13:25:01 -0300973 zfree(&evsel->name);
Arnaldo Carvalho de Meloce8ccff2014-10-09 15:29:51 -0300974 perf_evsel__object.fini(evsel);
Arnaldo Carvalho de Meloef1d1af2011-01-18 21:41:45 -0200975}
976
977void perf_evsel__delete(struct perf_evsel *evsel)
978{
979 perf_evsel__exit(evsel);
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200980 free(evsel);
981}
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200982
Jiri Olsaa6fa0032015-06-26 11:29:11 +0200983void perf_evsel__compute_deltas(struct perf_evsel *evsel, int cpu, int thread,
Jiri Olsa857a94a2014-11-21 10:31:05 +0100984 struct perf_counts_values *count)
Stephane Eranianc7a79c42013-01-29 12:47:43 +0100985{
986 struct perf_counts_values tmp;
987
988 if (!evsel->prev_raw_counts)
989 return;
990
991 if (cpu == -1) {
992 tmp = evsel->prev_raw_counts->aggr;
993 evsel->prev_raw_counts->aggr = *count;
994 } else {
Jiri Olsaa6fa0032015-06-26 11:29:11 +0200995 tmp = *perf_counts(evsel->prev_raw_counts, cpu, thread);
996 *perf_counts(evsel->prev_raw_counts, cpu, thread) = *count;
Stephane Eranianc7a79c42013-01-29 12:47:43 +0100997 }
998
999 count->val = count->val - tmp.val;
1000 count->ena = count->ena - tmp.ena;
1001 count->run = count->run - tmp.run;
1002}
1003
Jiri Olsa13112bb2014-11-21 10:31:06 +01001004void perf_counts_values__scale(struct perf_counts_values *count,
1005 bool scale, s8 *pscaled)
1006{
1007 s8 scaled = 0;
1008
1009 if (scale) {
1010 if (count->run == 0) {
1011 scaled = -1;
1012 count->val = 0;
1013 } else if (count->run < count->ena) {
1014 scaled = 1;
1015 count->val = (u64)((double) count->val * count->ena / count->run + 0.5);
1016 }
1017 } else
1018 count->ena = count->run = 0;
1019
1020 if (pscaled)
1021 *pscaled = scaled;
1022}
1023
Jiri Olsaf99f47192015-06-26 11:29:18 +02001024int perf_evsel__read(struct perf_evsel *evsel, int cpu, int thread,
1025 struct perf_counts_values *count)
1026{
1027 memset(count, 0, sizeof(*count));
1028
1029 if (FD(evsel, cpu, thread) < 0)
1030 return -EINVAL;
1031
1032 if (readn(FD(evsel, cpu, thread), count, sizeof(*count)) < 0)
1033 return -errno;
1034
1035 return 0;
1036}
1037
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -02001038int __perf_evsel__read_on_cpu(struct perf_evsel *evsel,
1039 int cpu, int thread, bool scale)
1040{
1041 struct perf_counts_values count;
1042 size_t nv = scale ? 3 : 1;
1043
1044 if (FD(evsel, cpu, thread) < 0)
1045 return -EINVAL;
1046
Jiri Olsaa6fa0032015-06-26 11:29:11 +02001047 if (evsel->counts == NULL && perf_evsel__alloc_counts(evsel, cpu + 1, thread + 1) < 0)
Arnaldo Carvalho de Melo4eed11d2011-01-04 00:13:17 -02001048 return -ENOMEM;
1049
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -02001050 if (readn(FD(evsel, cpu, thread), &count, nv * sizeof(u64)) < 0)
1051 return -errno;
1052
Jiri Olsaa6fa0032015-06-26 11:29:11 +02001053 perf_evsel__compute_deltas(evsel, cpu, thread, &count);
Jiri Olsa13112bb2014-11-21 10:31:06 +01001054 perf_counts_values__scale(&count, scale, NULL);
Jiri Olsaa6fa0032015-06-26 11:29:11 +02001055 *perf_counts(evsel->counts, cpu, thread) = count;
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -02001056 return 0;
1057}
1058
Jiri Olsa6a4bb042012-08-08 12:22:36 +02001059static int get_group_fd(struct perf_evsel *evsel, int cpu, int thread)
1060{
1061 struct perf_evsel *leader = evsel->leader;
1062 int fd;
1063
Namhyung Kim823254e2012-11-29 15:38:30 +09001064 if (perf_evsel__is_group_leader(evsel))
Jiri Olsa6a4bb042012-08-08 12:22:36 +02001065 return -1;
1066
1067 /*
1068 * Leader must be already processed/open,
1069 * if not it's a bug.
1070 */
1071 BUG_ON(!leader->fd);
1072
1073 fd = FD(leader, cpu, thread);
1074 BUG_ON(fd == -1);
1075
1076 return fd;
1077}
1078
Peter Zijlstra2c5e8c52015-04-07 11:09:54 +02001079struct bit_names {
1080 int bit;
1081 const char *name;
1082};
Adrian Huntere3e1a542013-08-14 15:48:24 +03001083
Peter Zijlstra2c5e8c52015-04-07 11:09:54 +02001084static void __p_bits(char *buf, size_t size, u64 value, struct bit_names *bits)
Adrian Huntere3e1a542013-08-14 15:48:24 +03001085{
Peter Zijlstra2c5e8c52015-04-07 11:09:54 +02001086 bool first_bit = true;
1087 int i = 0;
Adrian Huntere3e1a542013-08-14 15:48:24 +03001088
Peter Zijlstra2c5e8c52015-04-07 11:09:54 +02001089 do {
1090 if (value & bits[i].bit) {
1091 buf += scnprintf(buf, size, "%s%s", first_bit ? "" : "|", bits[i].name);
1092 first_bit = false;
1093 }
1094 } while (bits[++i].name != NULL);
1095}
Adrian Huntere3e1a542013-08-14 15:48:24 +03001096
Peter Zijlstra2c5e8c52015-04-07 11:09:54 +02001097static void __p_sample_type(char *buf, size_t size, u64 value)
1098{
1099#define bit_name(n) { PERF_SAMPLE_##n, #n }
1100 struct bit_names bits[] = {
1101 bit_name(IP), bit_name(TID), bit_name(TIME), bit_name(ADDR),
1102 bit_name(READ), bit_name(CALLCHAIN), bit_name(ID), bit_name(CPU),
1103 bit_name(PERIOD), bit_name(STREAM_ID), bit_name(RAW),
1104 bit_name(BRANCH_STACK), bit_name(REGS_USER), bit_name(STACK_USER),
1105 bit_name(IDENTIFIER), bit_name(REGS_INTR),
1106 { .name = NULL, }
1107 };
1108#undef bit_name
1109 __p_bits(buf, size, value, bits);
1110}
Adrian Huntere3e1a542013-08-14 15:48:24 +03001111
Peter Zijlstra2c5e8c52015-04-07 11:09:54 +02001112static void __p_read_format(char *buf, size_t size, u64 value)
1113{
1114#define bit_name(n) { PERF_FORMAT_##n, #n }
1115 struct bit_names bits[] = {
1116 bit_name(TOTAL_TIME_ENABLED), bit_name(TOTAL_TIME_RUNNING),
1117 bit_name(ID), bit_name(GROUP),
1118 { .name = NULL, }
1119 };
1120#undef bit_name
1121 __p_bits(buf, size, value, bits);
1122}
Peter Zijlstra814c8c32015-03-31 00:19:31 +02001123
Peter Zijlstra2c5e8c52015-04-07 11:09:54 +02001124#define BUF_SIZE 1024
Adrian Huntere3e1a542013-08-14 15:48:24 +03001125
Adrian Hunter7310aed2015-06-11 15:51:04 +03001126#define p_hex(val) snprintf(buf, BUF_SIZE, "%#"PRIx64, (uint64_t)(val))
Peter Zijlstra2c5e8c52015-04-07 11:09:54 +02001127#define p_unsigned(val) snprintf(buf, BUF_SIZE, "%"PRIu64, (uint64_t)(val))
1128#define p_signed(val) snprintf(buf, BUF_SIZE, "%"PRId64, (int64_t)(val))
1129#define p_sample_type(val) __p_sample_type(buf, BUF_SIZE, val)
1130#define p_read_format(val) __p_read_format(buf, BUF_SIZE, val)
Adrian Huntere3e1a542013-08-14 15:48:24 +03001131
Peter Zijlstra2c5e8c52015-04-07 11:09:54 +02001132#define PRINT_ATTRn(_n, _f, _p) \
1133do { \
1134 if (attr->_f) { \
1135 _p(attr->_f); \
1136 ret += attr__fprintf(fp, _n, buf, priv);\
1137 } \
1138} while (0)
1139
1140#define PRINT_ATTRf(_f, _p) PRINT_ATTRn(#_f, _f, _p)
1141
1142int perf_event_attr__fprintf(FILE *fp, struct perf_event_attr *attr,
1143 attr__fprintf_f attr__fprintf, void *priv)
1144{
1145 char buf[BUF_SIZE];
1146 int ret = 0;
1147
1148 PRINT_ATTRf(type, p_unsigned);
1149 PRINT_ATTRf(size, p_unsigned);
1150 PRINT_ATTRf(config, p_hex);
1151 PRINT_ATTRn("{ sample_period, sample_freq }", sample_period, p_unsigned);
1152 PRINT_ATTRf(sample_type, p_sample_type);
1153 PRINT_ATTRf(read_format, p_read_format);
1154
1155 PRINT_ATTRf(disabled, p_unsigned);
1156 PRINT_ATTRf(inherit, p_unsigned);
1157 PRINT_ATTRf(pinned, p_unsigned);
1158 PRINT_ATTRf(exclusive, p_unsigned);
1159 PRINT_ATTRf(exclude_user, p_unsigned);
1160 PRINT_ATTRf(exclude_kernel, p_unsigned);
1161 PRINT_ATTRf(exclude_hv, p_unsigned);
1162 PRINT_ATTRf(exclude_idle, p_unsigned);
1163 PRINT_ATTRf(mmap, p_unsigned);
1164 PRINT_ATTRf(comm, p_unsigned);
1165 PRINT_ATTRf(freq, p_unsigned);
1166 PRINT_ATTRf(inherit_stat, p_unsigned);
1167 PRINT_ATTRf(enable_on_exec, p_unsigned);
1168 PRINT_ATTRf(task, p_unsigned);
1169 PRINT_ATTRf(watermark, p_unsigned);
1170 PRINT_ATTRf(precise_ip, p_unsigned);
1171 PRINT_ATTRf(mmap_data, p_unsigned);
1172 PRINT_ATTRf(sample_id_all, p_unsigned);
1173 PRINT_ATTRf(exclude_host, p_unsigned);
1174 PRINT_ATTRf(exclude_guest, p_unsigned);
1175 PRINT_ATTRf(exclude_callchain_kernel, p_unsigned);
1176 PRINT_ATTRf(exclude_callchain_user, p_unsigned);
1177 PRINT_ATTRf(mmap2, p_unsigned);
1178 PRINT_ATTRf(comm_exec, p_unsigned);
1179 PRINT_ATTRf(use_clockid, p_unsigned);
Adrian Hunter02860392015-07-21 12:44:03 +03001180 PRINT_ATTRf(context_switch, p_unsigned);
Peter Zijlstra2c5e8c52015-04-07 11:09:54 +02001181
1182 PRINT_ATTRn("{ wakeup_events, wakeup_watermark }", wakeup_events, p_unsigned);
1183 PRINT_ATTRf(bp_type, p_unsigned);
1184 PRINT_ATTRn("{ bp_addr, config1 }", bp_addr, p_hex);
1185 PRINT_ATTRn("{ bp_len, config2 }", bp_len, p_hex);
1186 PRINT_ATTRf(sample_regs_user, p_hex);
1187 PRINT_ATTRf(sample_stack_user, p_unsigned);
1188 PRINT_ATTRf(clockid, p_signed);
1189 PRINT_ATTRf(sample_regs_intr, p_hex);
Adrian Hunter70d73de2015-04-09 18:54:06 +03001190 PRINT_ATTRf(aux_watermark, p_unsigned);
Adrian Huntere3e1a542013-08-14 15:48:24 +03001191
1192 return ret;
1193}
1194
Peter Zijlstra2c5e8c52015-04-07 11:09:54 +02001195static int __open_attr__fprintf(FILE *fp, const char *name, const char *val,
1196 void *priv __attribute__((unused)))
1197{
1198 return fprintf(fp, " %-32s %s\n", name, val);
1199}
1200
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -02001201static int __perf_evsel__open(struct perf_evsel *evsel, struct cpu_map *cpus,
Jiri Olsa6a4bb042012-08-08 12:22:36 +02001202 struct thread_map *threads)
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -02001203{
Adrian Hunterbf8e8f42014-07-31 09:00:51 +03001204 int cpu, thread, nthreads;
Yann Droneaud57480d22014-06-30 22:28:47 +02001205 unsigned long flags = PERF_FLAG_FD_CLOEXEC;
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -02001206 int pid = -1, err;
Andi Kleenbec19672013-08-04 19:41:26 -07001207 enum { NO_CHANGE, SET_TO_MAX, INCREASED_MAX } set_rlimit = NO_CHANGE;
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -02001208
Adrian Hunterbf8e8f42014-07-31 09:00:51 +03001209 if (evsel->system_wide)
1210 nthreads = 1;
1211 else
1212 nthreads = threads->nr;
1213
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -02001214 if (evsel->fd == NULL &&
Adrian Hunterbf8e8f42014-07-31 09:00:51 +03001215 perf_evsel__alloc_fd(evsel, cpus->nr, nthreads) < 0)
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -02001216 return -ENOMEM;
Arnaldo Carvalho de Melo4eed11d2011-01-04 00:13:17 -02001217
Stephane Eranian023695d2011-02-14 11:20:01 +02001218 if (evsel->cgrp) {
Yann Droneaud57480d22014-06-30 22:28:47 +02001219 flags |= PERF_FLAG_PID_CGROUP;
Stephane Eranian023695d2011-02-14 11:20:01 +02001220 pid = evsel->cgrp->fd;
1221 }
1222
Arnaldo Carvalho de Melo594ac612012-12-13 13:13:07 -03001223fallback_missing_features:
Peter Zijlstra814c8c32015-03-31 00:19:31 +02001224 if (perf_missing_features.clockid_wrong)
1225 evsel->attr.clockid = CLOCK_MONOTONIC; /* should always work */
1226 if (perf_missing_features.clockid) {
1227 evsel->attr.use_clockid = 0;
1228 evsel->attr.clockid = 0;
1229 }
Yann Droneaud57480d22014-06-30 22:28:47 +02001230 if (perf_missing_features.cloexec)
1231 flags &= ~(unsigned long)PERF_FLAG_FD_CLOEXEC;
Stephane Eranian5c5e8542013-08-21 12:10:25 +02001232 if (perf_missing_features.mmap2)
1233 evsel->attr.mmap2 = 0;
Arnaldo Carvalho de Melo594ac612012-12-13 13:13:07 -03001234 if (perf_missing_features.exclude_guest)
1235 evsel->attr.exclude_guest = evsel->attr.exclude_host = 0;
1236retry_sample_id:
1237 if (perf_missing_features.sample_id_all)
1238 evsel->attr.sample_id_all = 0;
1239
Peter Zijlstra2c5e8c52015-04-07 11:09:54 +02001240 if (verbose >= 2) {
1241 fprintf(stderr, "%.60s\n", graph_dotted_line);
1242 fprintf(stderr, "perf_event_attr:\n");
1243 perf_event_attr__fprintf(stderr, &evsel->attr, __open_attr__fprintf, NULL);
1244 fprintf(stderr, "%.60s\n", graph_dotted_line);
1245 }
Adrian Huntere3e1a542013-08-14 15:48:24 +03001246
Arnaldo Carvalho de Melo86bd5e82011-01-03 23:09:46 -02001247 for (cpu = 0; cpu < cpus->nr; cpu++) {
Arnaldo Carvalho de Melo9d04f172011-01-12 00:08:18 -02001248
Adrian Hunterbf8e8f42014-07-31 09:00:51 +03001249 for (thread = 0; thread < nthreads; thread++) {
Jiri Olsa6a4bb042012-08-08 12:22:36 +02001250 int group_fd;
Stephane Eranian023695d2011-02-14 11:20:01 +02001251
Adrian Hunterbf8e8f42014-07-31 09:00:51 +03001252 if (!evsel->cgrp && !evsel->system_wide)
Jiri Olsae13798c2015-06-23 00:36:02 +02001253 pid = thread_map__pid(threads, thread);
Stephane Eranian023695d2011-02-14 11:20:01 +02001254
Jiri Olsa6a4bb042012-08-08 12:22:36 +02001255 group_fd = get_group_fd(evsel, cpu, thread);
Andi Kleenbec19672013-08-04 19:41:26 -07001256retry_open:
Ramkumar Ramachandraa33f6ef2014-03-18 15:10:42 -04001257 pr_debug2("sys_perf_event_open: pid %d cpu %d group_fd %d flags %#lx\n",
Adrian Huntere3e1a542013-08-14 15:48:24 +03001258 pid, cpus->map[cpu], group_fd, flags);
1259
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -02001260 FD(evsel, cpu, thread) = sys_perf_event_open(&evsel->attr,
Stephane Eranian023695d2011-02-14 11:20:01 +02001261 pid,
Arnaldo Carvalho de Melof08199d2011-01-11 23:42:19 -02001262 cpus->map[cpu],
Stephane Eranian023695d2011-02-14 11:20:01 +02001263 group_fd, flags);
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -02001264 if (FD(evsel, cpu, thread) < 0) {
1265 err = -errno;
Ramkumar Ramachandraa33f6ef2014-03-18 15:10:42 -04001266 pr_debug2("sys_perf_event_open failed, error %d\n",
Adrian Hunterf852fd62013-11-01 15:51:29 +02001267 err);
Arnaldo Carvalho de Melo594ac612012-12-13 13:13:07 -03001268 goto try_fallback;
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -02001269 }
Andi Kleenbec19672013-08-04 19:41:26 -07001270 set_rlimit = NO_CHANGE;
Peter Zijlstra814c8c32015-03-31 00:19:31 +02001271
1272 /*
1273 * If we succeeded but had to kill clockid, fail and
1274 * have perf_evsel__open_strerror() print us a nice
1275 * error.
1276 */
1277 if (perf_missing_features.clockid ||
1278 perf_missing_features.clockid_wrong) {
1279 err = -EINVAL;
1280 goto out_close;
1281 }
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -02001282 }
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -02001283 }
1284
1285 return 0;
1286
Arnaldo Carvalho de Melo594ac612012-12-13 13:13:07 -03001287try_fallback:
Andi Kleenbec19672013-08-04 19:41:26 -07001288 /*
1289 * perf stat needs between 5 and 22 fds per CPU. When we run out
1290 * of them try to increase the limits.
1291 */
1292 if (err == -EMFILE && set_rlimit < INCREASED_MAX) {
1293 struct rlimit l;
1294 int old_errno = errno;
1295
1296 if (getrlimit(RLIMIT_NOFILE, &l) == 0) {
1297 if (set_rlimit == NO_CHANGE)
1298 l.rlim_cur = l.rlim_max;
1299 else {
1300 l.rlim_cur = l.rlim_max + 1000;
1301 l.rlim_max = l.rlim_cur;
1302 }
1303 if (setrlimit(RLIMIT_NOFILE, &l) == 0) {
1304 set_rlimit++;
1305 errno = old_errno;
1306 goto retry_open;
1307 }
1308 }
1309 errno = old_errno;
1310 }
1311
Arnaldo Carvalho de Melo594ac612012-12-13 13:13:07 -03001312 if (err != -EINVAL || cpu > 0 || thread > 0)
1313 goto out_close;
1314
Peter Zijlstra814c8c32015-03-31 00:19:31 +02001315 /*
1316 * Must probe features in the order they were added to the
1317 * perf_event_attr interface.
1318 */
1319 if (!perf_missing_features.clockid_wrong && evsel->attr.use_clockid) {
1320 perf_missing_features.clockid_wrong = true;
1321 goto fallback_missing_features;
1322 } else if (!perf_missing_features.clockid && evsel->attr.use_clockid) {
1323 perf_missing_features.clockid = true;
1324 goto fallback_missing_features;
1325 } else if (!perf_missing_features.cloexec && (flags & PERF_FLAG_FD_CLOEXEC)) {
Yann Droneaud57480d22014-06-30 22:28:47 +02001326 perf_missing_features.cloexec = true;
1327 goto fallback_missing_features;
1328 } else if (!perf_missing_features.mmap2 && evsel->attr.mmap2) {
Stephane Eranian5c5e8542013-08-21 12:10:25 +02001329 perf_missing_features.mmap2 = true;
1330 goto fallback_missing_features;
1331 } else if (!perf_missing_features.exclude_guest &&
1332 (evsel->attr.exclude_guest || evsel->attr.exclude_host)) {
Arnaldo Carvalho de Melo594ac612012-12-13 13:13:07 -03001333 perf_missing_features.exclude_guest = true;
1334 goto fallback_missing_features;
1335 } else if (!perf_missing_features.sample_id_all) {
1336 perf_missing_features.sample_id_all = true;
1337 goto retry_sample_id;
1338 }
1339
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -02001340out_close:
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -02001341 do {
1342 while (--thread >= 0) {
1343 close(FD(evsel, cpu, thread));
1344 FD(evsel, cpu, thread) = -1;
1345 }
Adrian Hunterbf8e8f42014-07-31 09:00:51 +03001346 thread = nthreads;
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -02001347 } while (--cpu >= 0);
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -02001348 return err;
1349}
1350
1351void perf_evsel__close(struct perf_evsel *evsel, int ncpus, int nthreads)
1352{
1353 if (evsel->fd == NULL)
1354 return;
1355
1356 perf_evsel__close_fd(evsel, ncpus, nthreads);
1357 perf_evsel__free_fd(evsel);
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -02001358}
1359
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -02001360static struct {
1361 struct cpu_map map;
1362 int cpus[1];
1363} empty_cpu_map = {
1364 .map.nr = 1,
1365 .cpus = { -1, },
1366};
1367
1368static struct {
1369 struct thread_map map;
1370 int threads[1];
1371} empty_thread_map = {
1372 .map.nr = 1,
1373 .threads = { -1, },
1374};
1375
Arnaldo Carvalho de Melof08199d2011-01-11 23:42:19 -02001376int perf_evsel__open(struct perf_evsel *evsel, struct cpu_map *cpus,
Jiri Olsa6a4bb042012-08-08 12:22:36 +02001377 struct thread_map *threads)
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -02001378{
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -02001379 if (cpus == NULL) {
1380 /* Work around old compiler warnings about strict aliasing */
1381 cpus = &empty_cpu_map.map;
1382 }
1383
1384 if (threads == NULL)
1385 threads = &empty_thread_map.map;
1386
Jiri Olsa6a4bb042012-08-08 12:22:36 +02001387 return __perf_evsel__open(evsel, cpus, threads);
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -02001388}
1389
Arnaldo Carvalho de Melof08199d2011-01-11 23:42:19 -02001390int perf_evsel__open_per_cpu(struct perf_evsel *evsel,
Jiri Olsa6a4bb042012-08-08 12:22:36 +02001391 struct cpu_map *cpus)
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -02001392{
Jiri Olsa6a4bb042012-08-08 12:22:36 +02001393 return __perf_evsel__open(evsel, cpus, &empty_thread_map.map);
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -02001394}
1395
Arnaldo Carvalho de Melof08199d2011-01-11 23:42:19 -02001396int perf_evsel__open_per_thread(struct perf_evsel *evsel,
Jiri Olsa6a4bb042012-08-08 12:22:36 +02001397 struct thread_map *threads)
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -02001398{
Jiri Olsa6a4bb042012-08-08 12:22:36 +02001399 return __perf_evsel__open(evsel, &empty_cpu_map.map, threads);
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -02001400}
Arnaldo Carvalho de Melo70082dd2011-01-12 17:03:24 -02001401
Arnaldo Carvalho de Melo0807d2d2012-09-26 12:48:18 -03001402static int perf_evsel__parse_id_sample(const struct perf_evsel *evsel,
1403 const union perf_event *event,
1404 struct perf_sample *sample)
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001405{
Arnaldo Carvalho de Melo0807d2d2012-09-26 12:48:18 -03001406 u64 type = evsel->attr.sample_type;
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001407 const u64 *array = event->sample.array;
Arnaldo Carvalho de Melo0807d2d2012-09-26 12:48:18 -03001408 bool swapped = evsel->needs_swap;
Jiri Olsa37073f92012-05-30 14:23:44 +02001409 union u64_swap u;
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001410
1411 array += ((event->header.size -
1412 sizeof(event->header)) / sizeof(u64)) - 1;
1413
Adrian Hunter75562572013-08-27 11:23:09 +03001414 if (type & PERF_SAMPLE_IDENTIFIER) {
1415 sample->id = *array;
1416 array--;
1417 }
1418
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001419 if (type & PERF_SAMPLE_CPU) {
Jiri Olsa37073f92012-05-30 14:23:44 +02001420 u.val64 = *array;
1421 if (swapped) {
1422 /* undo swap of u64, then swap on individual u32s */
1423 u.val64 = bswap_64(u.val64);
1424 u.val32[0] = bswap_32(u.val32[0]);
1425 }
1426
1427 sample->cpu = u.val32[0];
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001428 array--;
1429 }
1430
1431 if (type & PERF_SAMPLE_STREAM_ID) {
1432 sample->stream_id = *array;
1433 array--;
1434 }
1435
1436 if (type & PERF_SAMPLE_ID) {
1437 sample->id = *array;
1438 array--;
1439 }
1440
1441 if (type & PERF_SAMPLE_TIME) {
1442 sample->time = *array;
1443 array--;
1444 }
1445
1446 if (type & PERF_SAMPLE_TID) {
Jiri Olsa37073f92012-05-30 14:23:44 +02001447 u.val64 = *array;
1448 if (swapped) {
1449 /* undo swap of u64, then swap on individual u32s */
1450 u.val64 = bswap_64(u.val64);
1451 u.val32[0] = bswap_32(u.val32[0]);
1452 u.val32[1] = bswap_32(u.val32[1]);
1453 }
1454
1455 sample->pid = u.val32[0];
1456 sample->tid = u.val32[1];
Adrian Hunterdd44bc62013-10-18 15:29:01 +03001457 array--;
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001458 }
1459
1460 return 0;
1461}
1462
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001463static inline bool overflow(const void *endp, u16 max_size, const void *offset,
1464 u64 size)
Frederic Weisbecker98e1da92011-05-21 20:08:15 +02001465{
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001466 return size > max_size || offset + size > endp;
Frederic Weisbecker98e1da92011-05-21 20:08:15 +02001467}
1468
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001469#define OVERFLOW_CHECK(offset, size, max_size) \
1470 do { \
1471 if (overflow(endp, (max_size), (offset), (size))) \
1472 return -EFAULT; \
1473 } while (0)
1474
1475#define OVERFLOW_CHECK_u64(offset) \
1476 OVERFLOW_CHECK(offset, sizeof(u64), sizeof(u64))
1477
Arnaldo Carvalho de Meloa3f698f2012-08-02 12:23:46 -03001478int perf_evsel__parse_sample(struct perf_evsel *evsel, union perf_event *event,
Arnaldo Carvalho de Melo0807d2d2012-09-26 12:48:18 -03001479 struct perf_sample *data)
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001480{
Arnaldo Carvalho de Meloa3f698f2012-08-02 12:23:46 -03001481 u64 type = evsel->attr.sample_type;
Arnaldo Carvalho de Melo0807d2d2012-09-26 12:48:18 -03001482 bool swapped = evsel->needs_swap;
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001483 const u64 *array;
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001484 u16 max_size = event->header.size;
1485 const void *endp = (void *)event + max_size;
1486 u64 sz;
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001487
David Ahern936be502011-09-06 09:12:26 -06001488 /*
1489 * used for cross-endian analysis. See git commit 65014ab3
1490 * for why this goofiness is needed.
1491 */
Jiri Olsa6a11f922012-05-16 08:59:04 +02001492 union u64_swap u;
David Ahern936be502011-09-06 09:12:26 -06001493
Robert Richterf3bda2c2011-12-15 17:32:39 +01001494 memset(data, 0, sizeof(*data));
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001495 data->cpu = data->pid = data->tid = -1;
1496 data->stream_id = data->id = data->time = -1ULL;
Jiri Olsabc529082014-02-03 12:44:41 +01001497 data->period = evsel->attr.sample_period;
Andi Kleen05484292013-01-24 16:10:29 +01001498 data->weight = 0;
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001499
1500 if (event->header.type != PERF_RECORD_SAMPLE) {
Arnaldo Carvalho de Meloa3f698f2012-08-02 12:23:46 -03001501 if (!evsel->attr.sample_id_all)
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001502 return 0;
Arnaldo Carvalho de Melo0807d2d2012-09-26 12:48:18 -03001503 return perf_evsel__parse_id_sample(evsel, event, data);
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001504 }
1505
1506 array = event->sample.array;
1507
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001508 /*
1509 * The evsel's sample_size is based on PERF_SAMPLE_MASK which includes
1510 * up to PERF_SAMPLE_PERIOD. After that overflow() must be used to
1511 * check the format does not go past the end of the event.
1512 */
Arnaldo Carvalho de Meloa3f698f2012-08-02 12:23:46 -03001513 if (evsel->sample_size + sizeof(event->header) > event->header.size)
Frederic Weisbeckera2854122011-05-21 19:33:04 +02001514 return -EFAULT;
1515
Adrian Hunter75562572013-08-27 11:23:09 +03001516 data->id = -1ULL;
1517 if (type & PERF_SAMPLE_IDENTIFIER) {
1518 data->id = *array;
1519 array++;
1520 }
1521
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001522 if (type & PERF_SAMPLE_IP) {
Adrian Hunteref893252013-08-27 11:23:06 +03001523 data->ip = *array;
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001524 array++;
1525 }
1526
1527 if (type & PERF_SAMPLE_TID) {
David Ahern936be502011-09-06 09:12:26 -06001528 u.val64 = *array;
1529 if (swapped) {
1530 /* undo swap of u64, then swap on individual u32s */
1531 u.val64 = bswap_64(u.val64);
1532 u.val32[0] = bswap_32(u.val32[0]);
1533 u.val32[1] = bswap_32(u.val32[1]);
1534 }
1535
1536 data->pid = u.val32[0];
1537 data->tid = u.val32[1];
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001538 array++;
1539 }
1540
1541 if (type & PERF_SAMPLE_TIME) {
1542 data->time = *array;
1543 array++;
1544 }
1545
David Ahern7cec0922011-05-30 13:08:23 -06001546 data->addr = 0;
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001547 if (type & PERF_SAMPLE_ADDR) {
1548 data->addr = *array;
1549 array++;
1550 }
1551
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001552 if (type & PERF_SAMPLE_ID) {
1553 data->id = *array;
1554 array++;
1555 }
1556
1557 if (type & PERF_SAMPLE_STREAM_ID) {
1558 data->stream_id = *array;
1559 array++;
1560 }
1561
1562 if (type & PERF_SAMPLE_CPU) {
David Ahern936be502011-09-06 09:12:26 -06001563
1564 u.val64 = *array;
1565 if (swapped) {
1566 /* undo swap of u64, then swap on individual u32s */
1567 u.val64 = bswap_64(u.val64);
1568 u.val32[0] = bswap_32(u.val32[0]);
1569 }
1570
1571 data->cpu = u.val32[0];
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001572 array++;
1573 }
1574
1575 if (type & PERF_SAMPLE_PERIOD) {
1576 data->period = *array;
1577 array++;
1578 }
1579
1580 if (type & PERF_SAMPLE_READ) {
Jiri Olsa9ede4732012-10-10 17:38:13 +02001581 u64 read_format = evsel->attr.read_format;
1582
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001583 OVERFLOW_CHECK_u64(array);
Jiri Olsa9ede4732012-10-10 17:38:13 +02001584 if (read_format & PERF_FORMAT_GROUP)
1585 data->read.group.nr = *array;
1586 else
1587 data->read.one.value = *array;
1588
1589 array++;
1590
1591 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) {
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001592 OVERFLOW_CHECK_u64(array);
Jiri Olsa9ede4732012-10-10 17:38:13 +02001593 data->read.time_enabled = *array;
1594 array++;
1595 }
1596
1597 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) {
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001598 OVERFLOW_CHECK_u64(array);
Jiri Olsa9ede4732012-10-10 17:38:13 +02001599 data->read.time_running = *array;
1600 array++;
1601 }
1602
1603 /* PERF_FORMAT_ID is forced for PERF_SAMPLE_READ */
1604 if (read_format & PERF_FORMAT_GROUP) {
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001605 const u64 max_group_nr = UINT64_MAX /
1606 sizeof(struct sample_read_value);
1607
1608 if (data->read.group.nr > max_group_nr)
1609 return -EFAULT;
1610 sz = data->read.group.nr *
1611 sizeof(struct sample_read_value);
1612 OVERFLOW_CHECK(array, sz, max_size);
1613 data->read.group.values =
1614 (struct sample_read_value *)array;
1615 array = (void *)array + sz;
Jiri Olsa9ede4732012-10-10 17:38:13 +02001616 } else {
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001617 OVERFLOW_CHECK_u64(array);
Jiri Olsa9ede4732012-10-10 17:38:13 +02001618 data->read.one.id = *array;
1619 array++;
1620 }
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001621 }
1622
1623 if (type & PERF_SAMPLE_CALLCHAIN) {
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001624 const u64 max_callchain_nr = UINT64_MAX / sizeof(u64);
1625
1626 OVERFLOW_CHECK_u64(array);
1627 data->callchain = (struct ip_callchain *)array++;
1628 if (data->callchain->nr > max_callchain_nr)
Frederic Weisbecker98e1da92011-05-21 20:08:15 +02001629 return -EFAULT;
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001630 sz = data->callchain->nr * sizeof(u64);
1631 OVERFLOW_CHECK(array, sz, max_size);
1632 array = (void *)array + sz;
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001633 }
1634
1635 if (type & PERF_SAMPLE_RAW) {
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001636 OVERFLOW_CHECK_u64(array);
David Ahern936be502011-09-06 09:12:26 -06001637 u.val64 = *array;
1638 if (WARN_ONCE(swapped,
1639 "Endianness of raw data not corrected!\n")) {
1640 /* undo swap of u64, then swap on individual u32s */
1641 u.val64 = bswap_64(u.val64);
1642 u.val32[0] = bswap_32(u.val32[0]);
1643 u.val32[1] = bswap_32(u.val32[1]);
1644 }
David Ahern936be502011-09-06 09:12:26 -06001645 data->raw_size = u.val32[0];
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001646 array = (void *)array + sizeof(u32);
Frederic Weisbecker98e1da92011-05-21 20:08:15 +02001647
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001648 OVERFLOW_CHECK(array, data->raw_size, max_size);
1649 data->raw_data = (void *)array;
1650 array = (void *)array + data->raw_size;
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001651 }
1652
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +01001653 if (type & PERF_SAMPLE_BRANCH_STACK) {
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001654 const u64 max_branch_nr = UINT64_MAX /
1655 sizeof(struct branch_entry);
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +01001656
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001657 OVERFLOW_CHECK_u64(array);
1658 data->branch_stack = (struct branch_stack *)array++;
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +01001659
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001660 if (data->branch_stack->nr > max_branch_nr)
1661 return -EFAULT;
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +01001662 sz = data->branch_stack->nr * sizeof(struct branch_entry);
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001663 OVERFLOW_CHECK(array, sz, max_size);
1664 array = (void *)array + sz;
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +01001665 }
Jiri Olsa0f6a3012012-08-07 15:20:45 +02001666
1667 if (type & PERF_SAMPLE_REGS_USER) {
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001668 OVERFLOW_CHECK_u64(array);
Adrian Hunter5b95a4a32013-08-27 11:23:10 +03001669 data->user_regs.abi = *array;
1670 array++;
Jiri Olsa0f6a3012012-08-07 15:20:45 +02001671
Adrian Hunter5b95a4a32013-08-27 11:23:10 +03001672 if (data->user_regs.abi) {
Jiri Olsa352ea452014-01-07 13:47:25 +01001673 u64 mask = evsel->attr.sample_regs_user;
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001674
Jiri Olsa352ea452014-01-07 13:47:25 +01001675 sz = hweight_long(mask) * sizeof(u64);
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001676 OVERFLOW_CHECK(array, sz, max_size);
Jiri Olsa352ea452014-01-07 13:47:25 +01001677 data->user_regs.mask = mask;
Jiri Olsa0f6a3012012-08-07 15:20:45 +02001678 data->user_regs.regs = (u64 *)array;
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001679 array = (void *)array + sz;
Jiri Olsa0f6a3012012-08-07 15:20:45 +02001680 }
1681 }
1682
1683 if (type & PERF_SAMPLE_STACK_USER) {
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001684 OVERFLOW_CHECK_u64(array);
1685 sz = *array++;
Jiri Olsa0f6a3012012-08-07 15:20:45 +02001686
1687 data->user_stack.offset = ((char *)(array - 1)
1688 - (char *) event);
1689
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001690 if (!sz) {
Jiri Olsa0f6a3012012-08-07 15:20:45 +02001691 data->user_stack.size = 0;
1692 } else {
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001693 OVERFLOW_CHECK(array, sz, max_size);
Jiri Olsa0f6a3012012-08-07 15:20:45 +02001694 data->user_stack.data = (char *)array;
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001695 array = (void *)array + sz;
1696 OVERFLOW_CHECK_u64(array);
Adrian Hunter54bd2692013-07-04 16:20:34 +03001697 data->user_stack.size = *array++;
Jiri Olsaa65cb4b2013-10-02 15:46:39 +02001698 if (WARN_ONCE(data->user_stack.size > sz,
1699 "user stack dump failure\n"))
1700 return -EFAULT;
Jiri Olsa0f6a3012012-08-07 15:20:45 +02001701 }
1702 }
1703
Andi Kleen05484292013-01-24 16:10:29 +01001704 data->weight = 0;
1705 if (type & PERF_SAMPLE_WEIGHT) {
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001706 OVERFLOW_CHECK_u64(array);
Andi Kleen05484292013-01-24 16:10:29 +01001707 data->weight = *array;
1708 array++;
1709 }
1710
Stephane Eranian98a3b322013-01-24 16:10:35 +01001711 data->data_src = PERF_MEM_DATA_SRC_NONE;
1712 if (type & PERF_SAMPLE_DATA_SRC) {
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001713 OVERFLOW_CHECK_u64(array);
Stephane Eranian98a3b322013-01-24 16:10:35 +01001714 data->data_src = *array;
1715 array++;
1716 }
1717
Andi Kleen475eeab2013-09-20 07:40:43 -07001718 data->transaction = 0;
1719 if (type & PERF_SAMPLE_TRANSACTION) {
Adrian Hunter87b95522013-11-01 15:51:36 +02001720 OVERFLOW_CHECK_u64(array);
Andi Kleen475eeab2013-09-20 07:40:43 -07001721 data->transaction = *array;
1722 array++;
1723 }
1724
Stephane Eranian6a21c0b2014-09-24 13:48:39 +02001725 data->intr_regs.abi = PERF_SAMPLE_REGS_ABI_NONE;
1726 if (type & PERF_SAMPLE_REGS_INTR) {
1727 OVERFLOW_CHECK_u64(array);
1728 data->intr_regs.abi = *array;
1729 array++;
1730
1731 if (data->intr_regs.abi != PERF_SAMPLE_REGS_ABI_NONE) {
1732 u64 mask = evsel->attr.sample_regs_intr;
1733
1734 sz = hweight_long(mask) * sizeof(u64);
1735 OVERFLOW_CHECK(array, sz, max_size);
1736 data->intr_regs.mask = mask;
1737 data->intr_regs.regs = (u64 *)array;
1738 array = (void *)array + sz;
1739 }
1740 }
1741
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001742 return 0;
1743}
Andrew Vagin74eec262011-11-28 12:03:31 +03001744
Adrian Hunterb1cf6f62013-08-27 11:23:12 +03001745size_t perf_event__sample_event_size(const struct perf_sample *sample, u64 type,
Jiri Olsa352ea452014-01-07 13:47:25 +01001746 u64 read_format)
Adrian Hunterb1cf6f62013-08-27 11:23:12 +03001747{
1748 size_t sz, result = sizeof(struct sample_event);
1749
1750 if (type & PERF_SAMPLE_IDENTIFIER)
1751 result += sizeof(u64);
1752
1753 if (type & PERF_SAMPLE_IP)
1754 result += sizeof(u64);
1755
1756 if (type & PERF_SAMPLE_TID)
1757 result += sizeof(u64);
1758
1759 if (type & PERF_SAMPLE_TIME)
1760 result += sizeof(u64);
1761
1762 if (type & PERF_SAMPLE_ADDR)
1763 result += sizeof(u64);
1764
1765 if (type & PERF_SAMPLE_ID)
1766 result += sizeof(u64);
1767
1768 if (type & PERF_SAMPLE_STREAM_ID)
1769 result += sizeof(u64);
1770
1771 if (type & PERF_SAMPLE_CPU)
1772 result += sizeof(u64);
1773
1774 if (type & PERF_SAMPLE_PERIOD)
1775 result += sizeof(u64);
1776
1777 if (type & PERF_SAMPLE_READ) {
1778 result += sizeof(u64);
1779 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
1780 result += sizeof(u64);
1781 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
1782 result += sizeof(u64);
1783 /* PERF_FORMAT_ID is forced for PERF_SAMPLE_READ */
1784 if (read_format & PERF_FORMAT_GROUP) {
1785 sz = sample->read.group.nr *
1786 sizeof(struct sample_read_value);
1787 result += sz;
1788 } else {
1789 result += sizeof(u64);
1790 }
1791 }
1792
1793 if (type & PERF_SAMPLE_CALLCHAIN) {
1794 sz = (sample->callchain->nr + 1) * sizeof(u64);
1795 result += sz;
1796 }
1797
1798 if (type & PERF_SAMPLE_RAW) {
1799 result += sizeof(u32);
1800 result += sample->raw_size;
1801 }
1802
1803 if (type & PERF_SAMPLE_BRANCH_STACK) {
1804 sz = sample->branch_stack->nr * sizeof(struct branch_entry);
1805 sz += sizeof(u64);
1806 result += sz;
1807 }
1808
1809 if (type & PERF_SAMPLE_REGS_USER) {
1810 if (sample->user_regs.abi) {
1811 result += sizeof(u64);
Jiri Olsa352ea452014-01-07 13:47:25 +01001812 sz = hweight_long(sample->user_regs.mask) * sizeof(u64);
Adrian Hunterb1cf6f62013-08-27 11:23:12 +03001813 result += sz;
1814 } else {
1815 result += sizeof(u64);
1816 }
1817 }
1818
1819 if (type & PERF_SAMPLE_STACK_USER) {
1820 sz = sample->user_stack.size;
1821 result += sizeof(u64);
1822 if (sz) {
1823 result += sz;
1824 result += sizeof(u64);
1825 }
1826 }
1827
1828 if (type & PERF_SAMPLE_WEIGHT)
1829 result += sizeof(u64);
1830
1831 if (type & PERF_SAMPLE_DATA_SRC)
1832 result += sizeof(u64);
1833
Adrian Hunter42d88912013-11-01 15:51:38 +02001834 if (type & PERF_SAMPLE_TRANSACTION)
1835 result += sizeof(u64);
1836
Stephane Eranian6a21c0b2014-09-24 13:48:39 +02001837 if (type & PERF_SAMPLE_REGS_INTR) {
1838 if (sample->intr_regs.abi) {
1839 result += sizeof(u64);
1840 sz = hweight_long(sample->intr_regs.mask) * sizeof(u64);
1841 result += sz;
1842 } else {
1843 result += sizeof(u64);
1844 }
1845 }
1846
Adrian Hunterb1cf6f62013-08-27 11:23:12 +03001847 return result;
1848}
1849
Andrew Vagin74eec262011-11-28 12:03:31 +03001850int perf_event__synthesize_sample(union perf_event *event, u64 type,
Jiri Olsa352ea452014-01-07 13:47:25 +01001851 u64 read_format,
Andrew Vagin74eec262011-11-28 12:03:31 +03001852 const struct perf_sample *sample,
1853 bool swapped)
1854{
1855 u64 *array;
Adrian Hunterd03f2172013-08-27 11:23:11 +03001856 size_t sz;
Andrew Vagin74eec262011-11-28 12:03:31 +03001857 /*
1858 * used for cross-endian analysis. See git commit 65014ab3
1859 * for why this goofiness is needed.
1860 */
Jiri Olsa6a11f922012-05-16 08:59:04 +02001861 union u64_swap u;
Andrew Vagin74eec262011-11-28 12:03:31 +03001862
1863 array = event->sample.array;
1864
Adrian Hunter75562572013-08-27 11:23:09 +03001865 if (type & PERF_SAMPLE_IDENTIFIER) {
1866 *array = sample->id;
1867 array++;
1868 }
1869
Andrew Vagin74eec262011-11-28 12:03:31 +03001870 if (type & PERF_SAMPLE_IP) {
Adrian Hunteref893252013-08-27 11:23:06 +03001871 *array = sample->ip;
Andrew Vagin74eec262011-11-28 12:03:31 +03001872 array++;
1873 }
1874
1875 if (type & PERF_SAMPLE_TID) {
1876 u.val32[0] = sample->pid;
1877 u.val32[1] = sample->tid;
1878 if (swapped) {
1879 /*
Arnaldo Carvalho de Meloa3f698f2012-08-02 12:23:46 -03001880 * Inverse of what is done in perf_evsel__parse_sample
Andrew Vagin74eec262011-11-28 12:03:31 +03001881 */
1882 u.val32[0] = bswap_32(u.val32[0]);
1883 u.val32[1] = bswap_32(u.val32[1]);
1884 u.val64 = bswap_64(u.val64);
1885 }
1886
1887 *array = u.val64;
1888 array++;
1889 }
1890
1891 if (type & PERF_SAMPLE_TIME) {
1892 *array = sample->time;
1893 array++;
1894 }
1895
1896 if (type & PERF_SAMPLE_ADDR) {
1897 *array = sample->addr;
1898 array++;
1899 }
1900
1901 if (type & PERF_SAMPLE_ID) {
1902 *array = sample->id;
1903 array++;
1904 }
1905
1906 if (type & PERF_SAMPLE_STREAM_ID) {
1907 *array = sample->stream_id;
1908 array++;
1909 }
1910
1911 if (type & PERF_SAMPLE_CPU) {
1912 u.val32[0] = sample->cpu;
1913 if (swapped) {
1914 /*
Arnaldo Carvalho de Meloa3f698f2012-08-02 12:23:46 -03001915 * Inverse of what is done in perf_evsel__parse_sample
Andrew Vagin74eec262011-11-28 12:03:31 +03001916 */
1917 u.val32[0] = bswap_32(u.val32[0]);
1918 u.val64 = bswap_64(u.val64);
1919 }
1920 *array = u.val64;
1921 array++;
1922 }
1923
1924 if (type & PERF_SAMPLE_PERIOD) {
1925 *array = sample->period;
1926 array++;
1927 }
1928
Adrian Hunterd03f2172013-08-27 11:23:11 +03001929 if (type & PERF_SAMPLE_READ) {
1930 if (read_format & PERF_FORMAT_GROUP)
1931 *array = sample->read.group.nr;
1932 else
1933 *array = sample->read.one.value;
1934 array++;
1935
1936 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) {
1937 *array = sample->read.time_enabled;
1938 array++;
1939 }
1940
1941 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) {
1942 *array = sample->read.time_running;
1943 array++;
1944 }
1945
1946 /* PERF_FORMAT_ID is forced for PERF_SAMPLE_READ */
1947 if (read_format & PERF_FORMAT_GROUP) {
1948 sz = sample->read.group.nr *
1949 sizeof(struct sample_read_value);
1950 memcpy(array, sample->read.group.values, sz);
1951 array = (void *)array + sz;
1952 } else {
1953 *array = sample->read.one.id;
1954 array++;
1955 }
1956 }
1957
1958 if (type & PERF_SAMPLE_CALLCHAIN) {
1959 sz = (sample->callchain->nr + 1) * sizeof(u64);
1960 memcpy(array, sample->callchain, sz);
1961 array = (void *)array + sz;
1962 }
1963
1964 if (type & PERF_SAMPLE_RAW) {
1965 u.val32[0] = sample->raw_size;
1966 if (WARN_ONCE(swapped,
1967 "Endianness of raw data not corrected!\n")) {
1968 /*
1969 * Inverse of what is done in perf_evsel__parse_sample
1970 */
1971 u.val32[0] = bswap_32(u.val32[0]);
1972 u.val32[1] = bswap_32(u.val32[1]);
1973 u.val64 = bswap_64(u.val64);
1974 }
1975 *array = u.val64;
1976 array = (void *)array + sizeof(u32);
1977
1978 memcpy(array, sample->raw_data, sample->raw_size);
1979 array = (void *)array + sample->raw_size;
1980 }
1981
1982 if (type & PERF_SAMPLE_BRANCH_STACK) {
1983 sz = sample->branch_stack->nr * sizeof(struct branch_entry);
1984 sz += sizeof(u64);
1985 memcpy(array, sample->branch_stack, sz);
1986 array = (void *)array + sz;
1987 }
1988
1989 if (type & PERF_SAMPLE_REGS_USER) {
1990 if (sample->user_regs.abi) {
1991 *array++ = sample->user_regs.abi;
Jiri Olsa352ea452014-01-07 13:47:25 +01001992 sz = hweight_long(sample->user_regs.mask) * sizeof(u64);
Adrian Hunterd03f2172013-08-27 11:23:11 +03001993 memcpy(array, sample->user_regs.regs, sz);
1994 array = (void *)array + sz;
1995 } else {
1996 *array++ = 0;
1997 }
1998 }
1999
2000 if (type & PERF_SAMPLE_STACK_USER) {
2001 sz = sample->user_stack.size;
2002 *array++ = sz;
2003 if (sz) {
2004 memcpy(array, sample->user_stack.data, sz);
2005 array = (void *)array + sz;
2006 *array++ = sz;
2007 }
2008 }
2009
2010 if (type & PERF_SAMPLE_WEIGHT) {
2011 *array = sample->weight;
2012 array++;
2013 }
2014
2015 if (type & PERF_SAMPLE_DATA_SRC) {
2016 *array = sample->data_src;
2017 array++;
2018 }
2019
Adrian Hunter42d88912013-11-01 15:51:38 +02002020 if (type & PERF_SAMPLE_TRANSACTION) {
2021 *array = sample->transaction;
2022 array++;
2023 }
2024
Stephane Eranian6a21c0b2014-09-24 13:48:39 +02002025 if (type & PERF_SAMPLE_REGS_INTR) {
2026 if (sample->intr_regs.abi) {
2027 *array++ = sample->intr_regs.abi;
2028 sz = hweight_long(sample->intr_regs.mask) * sizeof(u64);
2029 memcpy(array, sample->intr_regs.regs, sz);
2030 array = (void *)array + sz;
2031 } else {
2032 *array++ = 0;
2033 }
2034 }
2035
Andrew Vagin74eec262011-11-28 12:03:31 +03002036 return 0;
2037}
Arnaldo Carvalho de Melo5555ded2012-09-11 19:24:23 -03002038
Arnaldo Carvalho de Meloefd2b922012-09-18 11:21:50 -03002039struct format_field *perf_evsel__field(struct perf_evsel *evsel, const char *name)
2040{
2041 return pevent_find_field(evsel->tp_format, name);
2042}
2043
Arnaldo Carvalho de Melo5d2074e2012-09-26 20:22:00 -03002044void *perf_evsel__rawptr(struct perf_evsel *evsel, struct perf_sample *sample,
Arnaldo Carvalho de Melo5555ded2012-09-11 19:24:23 -03002045 const char *name)
2046{
Arnaldo Carvalho de Meloefd2b922012-09-18 11:21:50 -03002047 struct format_field *field = perf_evsel__field(evsel, name);
Arnaldo Carvalho de Melo5555ded2012-09-11 19:24:23 -03002048 int offset;
2049
Arnaldo Carvalho de Meloefd2b922012-09-18 11:21:50 -03002050 if (!field)
2051 return NULL;
Arnaldo Carvalho de Melo5555ded2012-09-11 19:24:23 -03002052
2053 offset = field->offset;
2054
2055 if (field->flags & FIELD_IS_DYNAMIC) {
2056 offset = *(int *)(sample->raw_data + field->offset);
2057 offset &= 0xffff;
2058 }
2059
2060 return sample->raw_data + offset;
2061}
2062
2063u64 perf_evsel__intval(struct perf_evsel *evsel, struct perf_sample *sample,
2064 const char *name)
2065{
Arnaldo Carvalho de Meloefd2b922012-09-18 11:21:50 -03002066 struct format_field *field = perf_evsel__field(evsel, name);
Arnaldo Carvalho de Meloe6b6f672012-09-26 13:13:04 -03002067 void *ptr;
2068 u64 value;
Arnaldo Carvalho de Melo5555ded2012-09-11 19:24:23 -03002069
Arnaldo Carvalho de Meloefd2b922012-09-18 11:21:50 -03002070 if (!field)
2071 return 0;
Arnaldo Carvalho de Melo5555ded2012-09-11 19:24:23 -03002072
Arnaldo Carvalho de Meloe6b6f672012-09-26 13:13:04 -03002073 ptr = sample->raw_data + field->offset;
Arnaldo Carvalho de Melo5555ded2012-09-11 19:24:23 -03002074
Arnaldo Carvalho de Meloe6b6f672012-09-26 13:13:04 -03002075 switch (field->size) {
2076 case 1:
2077 return *(u8 *)ptr;
2078 case 2:
2079 value = *(u16 *)ptr;
2080 break;
2081 case 4:
2082 value = *(u32 *)ptr;
2083 break;
2084 case 8:
David Aherne94eeda2015-03-24 16:14:09 -04002085 memcpy(&value, ptr, sizeof(u64));
Arnaldo Carvalho de Meloe6b6f672012-09-26 13:13:04 -03002086 break;
2087 default:
2088 return 0;
2089 }
2090
2091 if (!evsel->needs_swap)
2092 return value;
2093
2094 switch (field->size) {
2095 case 2:
2096 return bswap_16(value);
2097 case 4:
2098 return bswap_32(value);
2099 case 8:
2100 return bswap_64(value);
2101 default:
2102 return 0;
2103 }
2104
2105 return 0;
Arnaldo Carvalho de Melo5555ded2012-09-11 19:24:23 -03002106}
Arnaldo Carvalho de Melo0698aed2012-12-10 18:17:08 -03002107
2108static int comma_fprintf(FILE *fp, bool *first, const char *fmt, ...)
2109{
2110 va_list args;
2111 int ret = 0;
2112
2113 if (!*first) {
2114 ret += fprintf(fp, ",");
2115 } else {
2116 ret += fprintf(fp, ":");
2117 *first = false;
2118 }
2119
2120 va_start(args, fmt);
2121 ret += vfprintf(fp, fmt, args);
2122 va_end(args);
2123 return ret;
2124}
2125
Peter Zijlstra2c5e8c52015-04-07 11:09:54 +02002126static int __print_attr__fprintf(FILE *fp, const char *name, const char *val, void *priv)
Arnaldo Carvalho de Melo0698aed2012-12-10 18:17:08 -03002127{
Peter Zijlstra2c5e8c52015-04-07 11:09:54 +02002128 return comma_fprintf(fp, (bool *)priv, " %s: %s", name, val);
Arnaldo Carvalho de Meloc79a4392012-12-11 10:54:12 -03002129}
2130
Arnaldo Carvalho de Melo0698aed2012-12-10 18:17:08 -03002131int perf_evsel__fprintf(struct perf_evsel *evsel,
2132 struct perf_attr_details *details, FILE *fp)
2133{
2134 bool first = true;
Namhyung Kime6ab07d2013-01-22 18:09:47 +09002135 int printed = 0;
2136
Arnaldo Carvalho de Meloe35ef3552013-02-06 17:20:02 -03002137 if (details->event_group) {
Namhyung Kime6ab07d2013-01-22 18:09:47 +09002138 struct perf_evsel *pos;
2139
2140 if (!perf_evsel__is_group_leader(evsel))
2141 return 0;
2142
2143 if (evsel->nr_members > 1)
2144 printed += fprintf(fp, "%s{", evsel->group_name ?: "");
2145
2146 printed += fprintf(fp, "%s", perf_evsel__name(evsel));
2147 for_each_group_member(pos, evsel)
2148 printed += fprintf(fp, ",%s", perf_evsel__name(pos));
2149
2150 if (evsel->nr_members > 1)
2151 printed += fprintf(fp, "}");
2152 goto out;
2153 }
2154
2155 printed += fprintf(fp, "%s", perf_evsel__name(evsel));
Arnaldo Carvalho de Melo0698aed2012-12-10 18:17:08 -03002156
Peter Zijlstra2c5e8c52015-04-07 11:09:54 +02002157 if (details->verbose) {
2158 printed += perf_event_attr__fprintf(fp, &evsel->attr,
2159 __print_attr__fprintf, &first);
2160 } else if (details->freq) {
Arnaldo Carvalho de Melo0698aed2012-12-10 18:17:08 -03002161 printed += comma_fprintf(fp, &first, " sample_freq=%" PRIu64,
2162 (u64)evsel->attr.sample_freq);
2163 }
Namhyung Kime6ab07d2013-01-22 18:09:47 +09002164out:
Arnaldo Carvalho de Melo0698aed2012-12-10 18:17:08 -03002165 fputc('\n', fp);
2166 return ++printed;
2167}
Arnaldo Carvalho de Meloc0a54342012-12-13 14:16:30 -03002168
2169bool perf_evsel__fallback(struct perf_evsel *evsel, int err,
2170 char *msg, size_t msgsize)
2171{
David Ahern2b821cc2013-07-18 17:27:59 -06002172 if ((err == ENOENT || err == ENXIO || err == ENODEV) &&
Arnaldo Carvalho de Meloc0a54342012-12-13 14:16:30 -03002173 evsel->attr.type == PERF_TYPE_HARDWARE &&
2174 evsel->attr.config == PERF_COUNT_HW_CPU_CYCLES) {
2175 /*
2176 * If it's cycles then fall back to hrtimer based
2177 * cpu-clock-tick sw counter, which is always available even if
2178 * no PMU support.
2179 *
2180 * PPC returns ENXIO until 2.6.37 (behavior changed with commit
2181 * b0a873e).
2182 */
2183 scnprintf(msg, msgsize, "%s",
2184"The cycles event is not supported, trying to fall back to cpu-clock-ticks");
2185
2186 evsel->attr.type = PERF_TYPE_SOFTWARE;
2187 evsel->attr.config = PERF_COUNT_SW_CPU_CLOCK;
2188
Arnaldo Carvalho de Melo04662522013-12-26 17:41:15 -03002189 zfree(&evsel->name);
Arnaldo Carvalho de Meloc0a54342012-12-13 14:16:30 -03002190 return true;
2191 }
2192
2193 return false;
2194}
Arnaldo Carvalho de Melo56e52e82012-12-13 15:10:58 -03002195
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03002196int perf_evsel__open_strerror(struct perf_evsel *evsel, struct target *target,
Arnaldo Carvalho de Melo56e52e82012-12-13 15:10:58 -03002197 int err, char *msg, size_t size)
2198{
Masami Hiramatsu6e81c742014-08-14 02:22:36 +00002199 char sbuf[STRERR_BUFSIZE];
2200
Arnaldo Carvalho de Melo56e52e82012-12-13 15:10:58 -03002201 switch (err) {
2202 case EPERM:
2203 case EACCES:
David Ahernb69e63a2013-05-25 17:54:00 -06002204 return scnprintf(msg, size,
Arnaldo Carvalho de Melo56e52e82012-12-13 15:10:58 -03002205 "You may not have permission to collect %sstats.\n"
2206 "Consider tweaking /proc/sys/kernel/perf_event_paranoid:\n"
2207 " -1 - Not paranoid at all\n"
2208 " 0 - Disallow raw tracepoint access for unpriv\n"
2209 " 1 - Disallow cpu events for unpriv\n"
2210 " 2 - Disallow kernel profiling for unpriv",
2211 target->system_wide ? "system-wide " : "");
2212 case ENOENT:
2213 return scnprintf(msg, size, "The %s event is not supported.",
2214 perf_evsel__name(evsel));
2215 case EMFILE:
2216 return scnprintf(msg, size, "%s",
2217 "Too many events are opened.\n"
Jiri Olsa18ffdfe2015-05-25 22:51:54 +02002218 "Probably the maximum number of open file descriptors has been reached.\n"
2219 "Hint: Try again after reducing the number of events.\n"
2220 "Hint: Try increasing the limit with 'ulimit -n <limit>'");
Arnaldo Carvalho de Melo56e52e82012-12-13 15:10:58 -03002221 case ENODEV:
2222 if (target->cpu_list)
2223 return scnprintf(msg, size, "%s",
2224 "No such device - did you specify an out-of-range profile CPU?\n");
2225 break;
2226 case EOPNOTSUPP:
2227 if (evsel->attr.precise_ip)
2228 return scnprintf(msg, size, "%s",
2229 "\'precise\' request may not be supported. Try removing 'p' modifier.");
2230#if defined(__i386__) || defined(__x86_64__)
2231 if (evsel->attr.type == PERF_TYPE_HARDWARE)
2232 return scnprintf(msg, size, "%s",
2233 "No hardware sampling interrupt available.\n"
2234 "No APIC? If so then you can boot the kernel with the \"lapic\" boot parameter to force-enable it.");
2235#endif
2236 break;
Jiri Olsa63914ac2014-08-01 17:46:54 +02002237 case EBUSY:
2238 if (find_process("oprofiled"))
2239 return scnprintf(msg, size,
2240 "The PMU counters are busy/taken by another profiler.\n"
2241 "We found oprofile daemon running, please stop it and try again.");
2242 break;
Peter Zijlstra814c8c32015-03-31 00:19:31 +02002243 case EINVAL:
2244 if (perf_missing_features.clockid)
2245 return scnprintf(msg, size, "clockid feature not supported.");
2246 if (perf_missing_features.clockid_wrong)
2247 return scnprintf(msg, size, "wrong clockid (%d).", clockid);
2248 break;
Arnaldo Carvalho de Melo56e52e82012-12-13 15:10:58 -03002249 default:
2250 break;
2251 }
2252
2253 return scnprintf(msg, size,
Masami Hiramatsu6e81c742014-08-14 02:22:36 +00002254 "The sys_perf_event_open() syscall returned with %d (%s) for event (%s).\n"
Arnaldo Carvalho de Melo56e52e82012-12-13 15:10:58 -03002255 "/bin/dmesg may provide additional information.\n"
2256 "No CONFIG_PERF_EVENTS=y kernel support configured?\n",
Masami Hiramatsu6e81c742014-08-14 02:22:36 +00002257 err, strerror_r(err, sbuf, sizeof(sbuf)),
2258 perf_evsel__name(evsel));
Arnaldo Carvalho de Melo56e52e82012-12-13 15:10:58 -03002259}