blob: 21a373ebea226a8208c3ccf2aaee9ad92ad4156d [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 Melo69aad6f2011-01-03 16:39:04 -020018#include "evsel.h"
Arnaldo Carvalho de Melo70082dd2011-01-12 17:03:24 -020019#include "evlist.h"
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -020020#include "util.h"
Arnaldo Carvalho de Melo86bd5e82011-01-03 23:09:46 -020021#include "cpumap.h"
Arnaldo Carvalho de Melofd782602011-01-18 15:15:24 -020022#include "thread_map.h"
Namhyung Kim12864b32012-04-26 14:15:22 +090023#include "target.h"
Jiri Olsa26d33022012-08-07 15:20:47 +020024#include "perf_regs.h"
Adrian Huntere3e1a542013-08-14 15:48:24 +030025#include "debug.h"
Jiri Olsa97978b32013-12-03 14:09:24 +010026#include "trace-event.h"
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -020027
Arnaldo Carvalho de Melo594ac612012-12-13 13:13:07 -030028static struct {
29 bool sample_id_all;
30 bool exclude_guest;
Stephane Eranian5c5e8542013-08-21 12:10:25 +020031 bool mmap2;
Yann Droneaud57480d22014-06-30 22:28:47 +020032 bool cloexec;
Arnaldo Carvalho de Melo594ac612012-12-13 13:13:07 -030033} perf_missing_features;
34
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -020035#define FD(e, x, y) (*(int *)xyarray__entry(e->fd, x, y))
36
Adrian Hunter75562572013-08-27 11:23:09 +030037int __perf_evsel__sample_size(u64 sample_type)
Arnaldo Carvalho de Meloc2a70652011-06-02 11:04:54 -030038{
39 u64 mask = sample_type & PERF_SAMPLE_MASK;
40 int size = 0;
41 int i;
42
43 for (i = 0; i < 64; i++) {
44 if (mask & (1ULL << i))
45 size++;
46 }
47
48 size *= sizeof(u64);
49
50 return size;
51}
52
Adrian Hunter75562572013-08-27 11:23:09 +030053/**
54 * __perf_evsel__calc_id_pos - calculate id_pos.
55 * @sample_type: sample type
56 *
57 * This function returns the position of the event id (PERF_SAMPLE_ID or
58 * PERF_SAMPLE_IDENTIFIER) in a sample event i.e. in the array of struct
59 * sample_event.
60 */
61static int __perf_evsel__calc_id_pos(u64 sample_type)
62{
63 int idx = 0;
64
65 if (sample_type & PERF_SAMPLE_IDENTIFIER)
66 return 0;
67
68 if (!(sample_type & PERF_SAMPLE_ID))
69 return -1;
70
71 if (sample_type & PERF_SAMPLE_IP)
72 idx += 1;
73
74 if (sample_type & PERF_SAMPLE_TID)
75 idx += 1;
76
77 if (sample_type & PERF_SAMPLE_TIME)
78 idx += 1;
79
80 if (sample_type & PERF_SAMPLE_ADDR)
81 idx += 1;
82
83 return idx;
84}
85
86/**
87 * __perf_evsel__calc_is_pos - calculate is_pos.
88 * @sample_type: sample type
89 *
90 * This function returns the position (counting backwards) of the event id
91 * (PERF_SAMPLE_ID or PERF_SAMPLE_IDENTIFIER) in a non-sample event i.e. if
92 * sample_id_all is used there is an id sample appended to non-sample events.
93 */
94static int __perf_evsel__calc_is_pos(u64 sample_type)
95{
96 int idx = 1;
97
98 if (sample_type & PERF_SAMPLE_IDENTIFIER)
99 return 1;
100
101 if (!(sample_type & PERF_SAMPLE_ID))
102 return -1;
103
104 if (sample_type & PERF_SAMPLE_CPU)
105 idx += 1;
106
107 if (sample_type & PERF_SAMPLE_STREAM_ID)
108 idx += 1;
109
110 return idx;
111}
112
113void perf_evsel__calc_id_pos(struct perf_evsel *evsel)
114{
115 evsel->id_pos = __perf_evsel__calc_id_pos(evsel->attr.sample_type);
116 evsel->is_pos = __perf_evsel__calc_is_pos(evsel->attr.sample_type);
117}
118
Jiri Olsa4bf9ce12012-03-22 14:37:26 +0100119void hists__init(struct hists *hists)
Arnaldo Carvalho de Melo0e2a5f12011-11-04 08:16:58 -0200120{
121 memset(hists, 0, sizeof(*hists));
122 hists->entries_in_array[0] = hists->entries_in_array[1] = RB_ROOT;
123 hists->entries_in = &hists->entries_in_array[0];
124 hists->entries_collapsed = RB_ROOT;
125 hists->entries = RB_ROOT;
126 pthread_mutex_init(&hists->lock, NULL);
127}
128
Arnaldo Carvalho de Melo7be5ebe2012-12-10 14:53:43 -0300129void __perf_evsel__set_sample_bit(struct perf_evsel *evsel,
130 enum perf_event_sample_format bit)
131{
132 if (!(evsel->attr.sample_type & bit)) {
133 evsel->attr.sample_type |= bit;
134 evsel->sample_size += sizeof(u64);
Adrian Hunter75562572013-08-27 11:23:09 +0300135 perf_evsel__calc_id_pos(evsel);
Arnaldo Carvalho de Melo7be5ebe2012-12-10 14:53:43 -0300136 }
137}
138
139void __perf_evsel__reset_sample_bit(struct perf_evsel *evsel,
140 enum perf_event_sample_format bit)
141{
142 if (evsel->attr.sample_type & bit) {
143 evsel->attr.sample_type &= ~bit;
144 evsel->sample_size -= sizeof(u64);
Adrian Hunter75562572013-08-27 11:23:09 +0300145 perf_evsel__calc_id_pos(evsel);
Arnaldo Carvalho de Melo7be5ebe2012-12-10 14:53:43 -0300146 }
147}
148
Adrian Hunter75562572013-08-27 11:23:09 +0300149void perf_evsel__set_sample_id(struct perf_evsel *evsel,
150 bool can_sample_identifier)
Arnaldo Carvalho de Melo7a5a5ca2012-12-10 15:21:30 -0300151{
Adrian Hunter75562572013-08-27 11:23:09 +0300152 if (can_sample_identifier) {
153 perf_evsel__reset_sample_bit(evsel, ID);
154 perf_evsel__set_sample_bit(evsel, IDENTIFIER);
155 } else {
156 perf_evsel__set_sample_bit(evsel, ID);
157 }
Arnaldo Carvalho de Melo7a5a5ca2012-12-10 15:21:30 -0300158 evsel->attr.read_format |= PERF_FORMAT_ID;
159}
160
Arnaldo Carvalho de Meloef1d1af2011-01-18 21:41:45 -0200161void perf_evsel__init(struct perf_evsel *evsel,
162 struct perf_event_attr *attr, int idx)
163{
164 evsel->idx = idx;
165 evsel->attr = *attr;
Namhyung Kim2cfda562012-11-29 15:38:29 +0900166 evsel->leader = evsel;
Stephane Eranian410136f2013-11-12 17:58:49 +0100167 evsel->unit = "";
168 evsel->scale = 1.0;
Arnaldo Carvalho de Meloef1d1af2011-01-18 21:41:45 -0200169 INIT_LIST_HEAD(&evsel->node);
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300170 hists__init(&evsel->hists);
Arnaldo Carvalho de Melobde09462012-08-01 18:53:11 -0300171 evsel->sample_size = __perf_evsel__sample_size(attr->sample_type);
Adrian Hunter75562572013-08-27 11:23:09 +0300172 perf_evsel__calc_id_pos(evsel);
Arnaldo Carvalho de Meloef1d1af2011-01-18 21:41:45 -0200173}
174
Arnaldo Carvalho de Meloef503832013-11-07 16:41:19 -0300175struct perf_evsel *perf_evsel__new_idx(struct perf_event_attr *attr, int idx)
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200176{
177 struct perf_evsel *evsel = zalloc(sizeof(*evsel));
178
Arnaldo Carvalho de Meloef1d1af2011-01-18 21:41:45 -0200179 if (evsel != NULL)
180 perf_evsel__init(evsel, attr, idx);
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200181
182 return evsel;
183}
184
Arnaldo Carvalho de Meloef503832013-11-07 16:41:19 -0300185struct perf_evsel *perf_evsel__newtp_idx(const char *sys, const char *name, int idx)
Arnaldo Carvalho de Meloefd2b922012-09-18 11:21:50 -0300186{
187 struct perf_evsel *evsel = zalloc(sizeof(*evsel));
188
189 if (evsel != NULL) {
190 struct perf_event_attr attr = {
Arnaldo Carvalho de Melo0b80f8b32012-09-26 12:28:26 -0300191 .type = PERF_TYPE_TRACEPOINT,
192 .sample_type = (PERF_SAMPLE_RAW | PERF_SAMPLE_TIME |
193 PERF_SAMPLE_CPU | PERF_SAMPLE_PERIOD),
Arnaldo Carvalho de Meloefd2b922012-09-18 11:21:50 -0300194 };
195
Arnaldo Carvalho de Meloe48ffe22012-09-26 17:11:38 -0300196 if (asprintf(&evsel->name, "%s:%s", sys, name) < 0)
197 goto out_free;
198
Jiri Olsa97978b32013-12-03 14:09:24 +0100199 evsel->tp_format = trace_event__tp_format(sys, name);
Arnaldo Carvalho de Meloefd2b922012-09-18 11:21:50 -0300200 if (evsel->tp_format == NULL)
201 goto out_free;
202
Arnaldo Carvalho de Melo0b80f8b32012-09-26 12:28:26 -0300203 event_attr_init(&attr);
Arnaldo Carvalho de Meloefd2b922012-09-18 11:21:50 -0300204 attr.config = evsel->tp_format->id;
Arnaldo Carvalho de Melo0b80f8b32012-09-26 12:28:26 -0300205 attr.sample_period = 1;
Arnaldo Carvalho de Meloefd2b922012-09-18 11:21:50 -0300206 perf_evsel__init(evsel, &attr, idx);
Arnaldo Carvalho de Meloefd2b922012-09-18 11:21:50 -0300207 }
208
209 return evsel;
210
211out_free:
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -0300212 zfree(&evsel->name);
Arnaldo Carvalho de Meloefd2b922012-09-18 11:21:50 -0300213 free(evsel);
214 return NULL;
215}
216
Arnaldo Carvalho de Melo8ad70132012-09-06 13:11:18 -0300217const char *perf_evsel__hw_names[PERF_COUNT_HW_MAX] = {
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300218 "cycles",
219 "instructions",
220 "cache-references",
221 "cache-misses",
222 "branches",
223 "branch-misses",
224 "bus-cycles",
225 "stalled-cycles-frontend",
226 "stalled-cycles-backend",
227 "ref-cycles",
228};
229
Arnaldo Carvalho de Melodd4f5222012-06-13 15:52:42 -0300230static const char *__perf_evsel__hw_name(u64 config)
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300231{
232 if (config < PERF_COUNT_HW_MAX && perf_evsel__hw_names[config])
233 return perf_evsel__hw_names[config];
234
235 return "unknown-hardware";
236}
237
Arnaldo Carvalho de Melo27f18612012-06-11 13:33:09 -0300238static int perf_evsel__add_modifiers(struct perf_evsel *evsel, char *bf, size_t size)
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300239{
Arnaldo Carvalho de Melo27f18612012-06-11 13:33:09 -0300240 int colon = 0, r = 0;
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300241 struct perf_event_attr *attr = &evsel->attr;
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300242 bool exclude_guest_default = false;
243
244#define MOD_PRINT(context, mod) do { \
245 if (!attr->exclude_##context) { \
Arnaldo Carvalho de Melo27f18612012-06-11 13:33:09 -0300246 if (!colon) colon = ++r; \
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300247 r += scnprintf(bf + r, size - r, "%c", mod); \
248 } } while(0)
249
250 if (attr->exclude_kernel || attr->exclude_user || attr->exclude_hv) {
251 MOD_PRINT(kernel, 'k');
252 MOD_PRINT(user, 'u');
253 MOD_PRINT(hv, 'h');
254 exclude_guest_default = true;
255 }
256
257 if (attr->precise_ip) {
258 if (!colon)
Arnaldo Carvalho de Melo27f18612012-06-11 13:33:09 -0300259 colon = ++r;
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300260 r += scnprintf(bf + r, size - r, "%.*s", attr->precise_ip, "ppp");
261 exclude_guest_default = true;
262 }
263
264 if (attr->exclude_host || attr->exclude_guest == exclude_guest_default) {
265 MOD_PRINT(host, 'H');
266 MOD_PRINT(guest, 'G');
267 }
268#undef MOD_PRINT
269 if (colon)
Arnaldo Carvalho de Melo27f18612012-06-11 13:33:09 -0300270 bf[colon - 1] = ':';
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300271 return r;
272}
273
Arnaldo Carvalho de Melo27f18612012-06-11 13:33:09 -0300274static int perf_evsel__hw_name(struct perf_evsel *evsel, char *bf, size_t size)
275{
276 int r = scnprintf(bf, size, "%s", __perf_evsel__hw_name(evsel->attr.config));
277 return r + perf_evsel__add_modifiers(evsel, bf + r, size - r);
278}
279
Arnaldo Carvalho de Melo8ad70132012-09-06 13:11:18 -0300280const char *perf_evsel__sw_names[PERF_COUNT_SW_MAX] = {
Arnaldo Carvalho de Melo335c2f52012-06-11 14:36:20 -0300281 "cpu-clock",
282 "task-clock",
283 "page-faults",
284 "context-switches",
Arnaldo Carvalho de Melo8ad70132012-09-06 13:11:18 -0300285 "cpu-migrations",
Arnaldo Carvalho de Melo335c2f52012-06-11 14:36:20 -0300286 "minor-faults",
287 "major-faults",
288 "alignment-faults",
289 "emulation-faults",
Adrian Hunterd22d1a22013-08-31 21:50:52 +0300290 "dummy",
Arnaldo Carvalho de Melo335c2f52012-06-11 14:36:20 -0300291};
292
Arnaldo Carvalho de Melodd4f5222012-06-13 15:52:42 -0300293static const char *__perf_evsel__sw_name(u64 config)
Arnaldo Carvalho de Melo335c2f52012-06-11 14:36:20 -0300294{
295 if (config < PERF_COUNT_SW_MAX && perf_evsel__sw_names[config])
296 return perf_evsel__sw_names[config];
297 return "unknown-software";
298}
299
300static int perf_evsel__sw_name(struct perf_evsel *evsel, char *bf, size_t size)
301{
302 int r = scnprintf(bf, size, "%s", __perf_evsel__sw_name(evsel->attr.config));
303 return r + perf_evsel__add_modifiers(evsel, bf + r, size - r);
304}
305
Jiri Olsa287e74a2012-06-28 23:18:49 +0200306static int __perf_evsel__bp_name(char *bf, size_t size, u64 addr, u64 type)
307{
308 int r;
309
310 r = scnprintf(bf, size, "mem:0x%" PRIx64 ":", addr);
311
312 if (type & HW_BREAKPOINT_R)
313 r += scnprintf(bf + r, size - r, "r");
314
315 if (type & HW_BREAKPOINT_W)
316 r += scnprintf(bf + r, size - r, "w");
317
318 if (type & HW_BREAKPOINT_X)
319 r += scnprintf(bf + r, size - r, "x");
320
321 return r;
322}
323
324static int perf_evsel__bp_name(struct perf_evsel *evsel, char *bf, size_t size)
325{
326 struct perf_event_attr *attr = &evsel->attr;
327 int r = __perf_evsel__bp_name(bf, size, attr->bp_addr, attr->bp_type);
328 return r + perf_evsel__add_modifiers(evsel, bf + r, size - r);
329}
330
Arnaldo Carvalho de Melo0b668bc2012-06-11 14:08:07 -0300331const char *perf_evsel__hw_cache[PERF_COUNT_HW_CACHE_MAX]
332 [PERF_EVSEL__MAX_ALIASES] = {
333 { "L1-dcache", "l1-d", "l1d", "L1-data", },
334 { "L1-icache", "l1-i", "l1i", "L1-instruction", },
335 { "LLC", "L2", },
336 { "dTLB", "d-tlb", "Data-TLB", },
337 { "iTLB", "i-tlb", "Instruction-TLB", },
338 { "branch", "branches", "bpu", "btb", "bpc", },
339 { "node", },
340};
341
342const char *perf_evsel__hw_cache_op[PERF_COUNT_HW_CACHE_OP_MAX]
343 [PERF_EVSEL__MAX_ALIASES] = {
344 { "load", "loads", "read", },
345 { "store", "stores", "write", },
346 { "prefetch", "prefetches", "speculative-read", "speculative-load", },
347};
348
349const char *perf_evsel__hw_cache_result[PERF_COUNT_HW_CACHE_RESULT_MAX]
350 [PERF_EVSEL__MAX_ALIASES] = {
351 { "refs", "Reference", "ops", "access", },
352 { "misses", "miss", },
353};
354
355#define C(x) PERF_COUNT_HW_CACHE_##x
356#define CACHE_READ (1 << C(OP_READ))
357#define CACHE_WRITE (1 << C(OP_WRITE))
358#define CACHE_PREFETCH (1 << C(OP_PREFETCH))
359#define COP(x) (1 << x)
360
361/*
362 * cache operartion stat
363 * L1I : Read and prefetch only
364 * ITLB and BPU : Read-only
365 */
366static unsigned long perf_evsel__hw_cache_stat[C(MAX)] = {
367 [C(L1D)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
368 [C(L1I)] = (CACHE_READ | CACHE_PREFETCH),
369 [C(LL)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
370 [C(DTLB)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
371 [C(ITLB)] = (CACHE_READ),
372 [C(BPU)] = (CACHE_READ),
373 [C(NODE)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
374};
375
376bool perf_evsel__is_cache_op_valid(u8 type, u8 op)
377{
378 if (perf_evsel__hw_cache_stat[type] & COP(op))
379 return true; /* valid */
380 else
381 return false; /* invalid */
382}
383
384int __perf_evsel__hw_cache_type_op_res_name(u8 type, u8 op, u8 result,
385 char *bf, size_t size)
386{
387 if (result) {
388 return scnprintf(bf, size, "%s-%s-%s", perf_evsel__hw_cache[type][0],
389 perf_evsel__hw_cache_op[op][0],
390 perf_evsel__hw_cache_result[result][0]);
391 }
392
393 return scnprintf(bf, size, "%s-%s", perf_evsel__hw_cache[type][0],
394 perf_evsel__hw_cache_op[op][1]);
395}
396
Arnaldo Carvalho de Melodd4f5222012-06-13 15:52:42 -0300397static int __perf_evsel__hw_cache_name(u64 config, char *bf, size_t size)
Arnaldo Carvalho de Melo0b668bc2012-06-11 14:08:07 -0300398{
399 u8 op, result, type = (config >> 0) & 0xff;
400 const char *err = "unknown-ext-hardware-cache-type";
401
402 if (type > PERF_COUNT_HW_CACHE_MAX)
403 goto out_err;
404
405 op = (config >> 8) & 0xff;
406 err = "unknown-ext-hardware-cache-op";
407 if (op > PERF_COUNT_HW_CACHE_OP_MAX)
408 goto out_err;
409
410 result = (config >> 16) & 0xff;
411 err = "unknown-ext-hardware-cache-result";
412 if (result > PERF_COUNT_HW_CACHE_RESULT_MAX)
413 goto out_err;
414
415 err = "invalid-cache";
416 if (!perf_evsel__is_cache_op_valid(type, op))
417 goto out_err;
418
419 return __perf_evsel__hw_cache_type_op_res_name(type, op, result, bf, size);
420out_err:
421 return scnprintf(bf, size, "%s", err);
422}
423
424static int perf_evsel__hw_cache_name(struct perf_evsel *evsel, char *bf, size_t size)
425{
426 int ret = __perf_evsel__hw_cache_name(evsel->attr.config, bf, size);
427 return ret + perf_evsel__add_modifiers(evsel, bf + ret, size - ret);
428}
429
Arnaldo Carvalho de Melo6eef3d92012-06-13 11:53:37 -0300430static int perf_evsel__raw_name(struct perf_evsel *evsel, char *bf, size_t size)
431{
432 int ret = scnprintf(bf, size, "raw 0x%" PRIx64, evsel->attr.config);
433 return ret + perf_evsel__add_modifiers(evsel, bf + ret, size - ret);
434}
435
Arnaldo Carvalho de Melo7289f832012-06-12 12:34:58 -0300436const char *perf_evsel__name(struct perf_evsel *evsel)
Arnaldo Carvalho de Meloa4460832012-06-12 10:29:12 -0300437{
Arnaldo Carvalho de Melo7289f832012-06-12 12:34:58 -0300438 char bf[128];
Arnaldo Carvalho de Meloa4460832012-06-12 10:29:12 -0300439
Arnaldo Carvalho de Melo7289f832012-06-12 12:34:58 -0300440 if (evsel->name)
441 return evsel->name;
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300442
443 switch (evsel->attr.type) {
444 case PERF_TYPE_RAW:
Arnaldo Carvalho de Melo6eef3d92012-06-13 11:53:37 -0300445 perf_evsel__raw_name(evsel, bf, sizeof(bf));
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300446 break;
447
448 case PERF_TYPE_HARDWARE:
Arnaldo Carvalho de Melo7289f832012-06-12 12:34:58 -0300449 perf_evsel__hw_name(evsel, bf, sizeof(bf));
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300450 break;
Arnaldo Carvalho de Melo0b668bc2012-06-11 14:08:07 -0300451
452 case PERF_TYPE_HW_CACHE:
Arnaldo Carvalho de Melo7289f832012-06-12 12:34:58 -0300453 perf_evsel__hw_cache_name(evsel, bf, sizeof(bf));
Arnaldo Carvalho de Melo0b668bc2012-06-11 14:08:07 -0300454 break;
455
Arnaldo Carvalho de Melo335c2f52012-06-11 14:36:20 -0300456 case PERF_TYPE_SOFTWARE:
Arnaldo Carvalho de Melo7289f832012-06-12 12:34:58 -0300457 perf_evsel__sw_name(evsel, bf, sizeof(bf));
Arnaldo Carvalho de Melo335c2f52012-06-11 14:36:20 -0300458 break;
459
Arnaldo Carvalho de Meloa4460832012-06-12 10:29:12 -0300460 case PERF_TYPE_TRACEPOINT:
Arnaldo Carvalho de Melo7289f832012-06-12 12:34:58 -0300461 scnprintf(bf, sizeof(bf), "%s", "unknown tracepoint");
Arnaldo Carvalho de Meloa4460832012-06-12 10:29:12 -0300462 break;
463
Jiri Olsa287e74a2012-06-28 23:18:49 +0200464 case PERF_TYPE_BREAKPOINT:
465 perf_evsel__bp_name(evsel, bf, sizeof(bf));
466 break;
467
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300468 default:
Robert Richterca1b1452012-08-16 21:10:18 +0200469 scnprintf(bf, sizeof(bf), "unknown attr type: %d",
470 evsel->attr.type);
Arnaldo Carvalho de Meloa4460832012-06-12 10:29:12 -0300471 break;
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300472 }
473
Arnaldo Carvalho de Melo7289f832012-06-12 12:34:58 -0300474 evsel->name = strdup(bf);
475
476 return evsel->name ?: "unknown";
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300477}
478
Namhyung Kim717e2632013-01-22 18:09:44 +0900479const char *perf_evsel__group_name(struct perf_evsel *evsel)
480{
481 return evsel->group_name ?: "anon group";
482}
483
484int perf_evsel__group_desc(struct perf_evsel *evsel, char *buf, size_t size)
485{
486 int ret;
487 struct perf_evsel *pos;
488 const char *group_name = perf_evsel__group_name(evsel);
489
490 ret = scnprintf(buf, size, "%s", group_name);
491
492 ret += scnprintf(buf + ret, size - ret, " { %s",
493 perf_evsel__name(evsel));
494
495 for_each_group_member(pos, evsel)
496 ret += scnprintf(buf + ret, size - ret, ", %s",
497 perf_evsel__name(pos));
498
499 ret += scnprintf(buf + ret, size - ret, " }");
500
501 return ret;
502}
503
Jiri Olsa6bedfab2014-03-02 16:56:40 +0100504static void
505perf_evsel__config_callgraph(struct perf_evsel *evsel,
506 struct record_opts *opts)
507{
508 bool function = perf_evsel__is_function_event(evsel);
509 struct perf_event_attr *attr = &evsel->attr;
510
511 perf_evsel__set_sample_bit(evsel, CALLCHAIN);
512
513 if (opts->call_graph == CALLCHAIN_DWARF) {
514 if (!function) {
515 perf_evsel__set_sample_bit(evsel, REGS_USER);
516 perf_evsel__set_sample_bit(evsel, STACK_USER);
517 attr->sample_regs_user = PERF_REGS_MASK;
518 attr->sample_stack_user = opts->stack_dump_size;
519 attr->exclude_callchain_user = 1;
520 } else {
521 pr_info("Cannot use DWARF unwind for function trace event,"
522 " falling back to framepointers.\n");
523 }
524 }
525
526 if (function) {
527 pr_info("Disabling user space callchains for function trace event.\n");
528 attr->exclude_callchain_user = 1;
529 }
530}
531
Jiri Olsa774cb492012-11-12 18:34:01 +0100532/*
533 * The enable_on_exec/disabled value strategy:
534 *
535 * 1) For any type of traced program:
536 * - all independent events and group leaders are disabled
537 * - all group members are enabled
538 *
539 * Group members are ruled by group leaders. They need to
540 * be enabled, because the group scheduling relies on that.
541 *
542 * 2) For traced programs executed by perf:
543 * - all independent events and group leaders have
544 * enable_on_exec set
545 * - we don't specifically enable or disable any event during
546 * the record command
547 *
548 * Independent events and group leaders are initially disabled
549 * and get enabled by exec. Group members are ruled by group
550 * leaders as stated in 1).
551 *
552 * 3) For traced programs attached by perf (pid/tid):
553 * - we specifically enable or disable all events during
554 * the record command
555 *
556 * When attaching events to already running traced we
557 * enable/disable events specifically, as there's no
558 * initial traced exec call.
559 */
Arnaldo Carvalho de Melob4006792013-12-19 14:43:45 -0300560void perf_evsel__config(struct perf_evsel *evsel, struct record_opts *opts)
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200561{
Jiri Olsa3c176312012-10-10 17:39:03 +0200562 struct perf_evsel *leader = evsel->leader;
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200563 struct perf_event_attr *attr = &evsel->attr;
564 int track = !evsel->idx; /* only the first counter needs these */
Adrian Hunter3aa59392013-11-15 15:52:29 +0200565 bool per_cpu = opts->target.default_per_cpu && !opts->target.per_thread;
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200566
Arnaldo Carvalho de Melo594ac612012-12-13 13:13:07 -0300567 attr->sample_id_all = perf_missing_features.sample_id_all ? 0 : 1;
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200568 attr->inherit = !opts->no_inherit;
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200569
Arnaldo Carvalho de Melo7be5ebe2012-12-10 14:53:43 -0300570 perf_evsel__set_sample_bit(evsel, IP);
571 perf_evsel__set_sample_bit(evsel, TID);
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200572
Jiri Olsa3c176312012-10-10 17:39:03 +0200573 if (evsel->sample_read) {
574 perf_evsel__set_sample_bit(evsel, READ);
575
576 /*
577 * We need ID even in case of single event, because
578 * PERF_SAMPLE_READ process ID specific data.
579 */
Adrian Hunter75562572013-08-27 11:23:09 +0300580 perf_evsel__set_sample_id(evsel, false);
Jiri Olsa3c176312012-10-10 17:39:03 +0200581
582 /*
583 * Apply group format only if we belong to group
584 * with more than one members.
585 */
586 if (leader->nr_members > 1) {
587 attr->read_format |= PERF_FORMAT_GROUP;
588 attr->inherit = 0;
589 }
590 }
591
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200592 /*
Namhyung Kim17314e22014-06-09 14:43:37 +0900593 * We default some events to have a default interval. But keep
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200594 * it a weak assumption overridable by the user.
595 */
Namhyung Kim17314e22014-06-09 14:43:37 +0900596 if (!attr->sample_period || (opts->user_freq != UINT_MAX ||
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200597 opts->user_interval != ULLONG_MAX)) {
598 if (opts->freq) {
Arnaldo Carvalho de Melo7be5ebe2012-12-10 14:53:43 -0300599 perf_evsel__set_sample_bit(evsel, PERIOD);
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200600 attr->freq = 1;
601 attr->sample_freq = opts->freq;
602 } else {
603 attr->sample_period = opts->default_interval;
604 }
605 }
606
Jiri Olsa3c176312012-10-10 17:39:03 +0200607 /*
608 * Disable sampling for all group members other
609 * than leader in case leader 'leads' the sampling.
610 */
611 if ((leader != evsel) && leader->sample_read) {
612 attr->sample_freq = 0;
613 attr->sample_period = 0;
614 }
615
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200616 if (opts->no_samples)
617 attr->sample_freq = 0;
618
619 if (opts->inherit_stat)
620 attr->inherit_stat = 1;
621
622 if (opts->sample_address) {
Arnaldo Carvalho de Melo7be5ebe2012-12-10 14:53:43 -0300623 perf_evsel__set_sample_bit(evsel, ADDR);
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200624 attr->mmap_data = track;
625 }
626
Adrian Hunter6ff1ce72014-07-14 13:02:56 +0300627 if (opts->call_graph_enabled && !evsel->no_aux_samples)
Jiri Olsa6bedfab2014-03-02 16:56:40 +0100628 perf_evsel__config_callgraph(evsel, opts);
Jiri Olsa26d33022012-08-07 15:20:47 +0200629
Adrian Hunter3aa59392013-11-15 15:52:29 +0200630 if (target__has_cpu(&opts->target))
Arnaldo Carvalho de Melo7be5ebe2012-12-10 14:53:43 -0300631 perf_evsel__set_sample_bit(evsel, CPU);
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200632
Andrew Vagin3e76ac72011-12-20 17:32:45 +0300633 if (opts->period)
Arnaldo Carvalho de Melo7be5ebe2012-12-10 14:53:43 -0300634 perf_evsel__set_sample_bit(evsel, PERIOD);
Andrew Vagin3e76ac72011-12-20 17:32:45 +0300635
Arnaldo Carvalho de Melo594ac612012-12-13 13:13:07 -0300636 if (!perf_missing_features.sample_id_all &&
Namhyung Kimd67356e2012-05-07 14:09:03 +0900637 (opts->sample_time || !opts->no_inherit ||
Adrian Hunter3aa59392013-11-15 15:52:29 +0200638 target__has_cpu(&opts->target) || per_cpu))
Arnaldo Carvalho de Melo7be5ebe2012-12-10 14:53:43 -0300639 perf_evsel__set_sample_bit(evsel, TIME);
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200640
Adrian Hunter6ff1ce72014-07-14 13:02:56 +0300641 if (opts->raw_samples && !evsel->no_aux_samples) {
Arnaldo Carvalho de Melo7be5ebe2012-12-10 14:53:43 -0300642 perf_evsel__set_sample_bit(evsel, TIME);
643 perf_evsel__set_sample_bit(evsel, RAW);
644 perf_evsel__set_sample_bit(evsel, CPU);
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200645 }
646
Stephane Eranianccf49bf2013-01-24 16:10:37 +0100647 if (opts->sample_address)
Adrian Hunter1e7ed5e2013-11-01 15:51:35 +0200648 perf_evsel__set_sample_bit(evsel, DATA_SRC);
Stephane Eranianccf49bf2013-01-24 16:10:37 +0100649
Arnaldo Carvalho de Melo509051e2014-01-14 17:52:14 -0300650 if (opts->no_buffering) {
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200651 attr->watermark = 0;
652 attr->wakeup_events = 1;
653 }
Adrian Hunter6ff1ce72014-07-14 13:02:56 +0300654 if (opts->branch_stack && !evsel->no_aux_samples) {
Arnaldo Carvalho de Melo7be5ebe2012-12-10 14:53:43 -0300655 perf_evsel__set_sample_bit(evsel, BRANCH_STACK);
Roberto Agostino Vitillobdfebd82012-02-09 23:21:02 +0100656 attr->branch_sample_type = opts->branch_stack;
657 }
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200658
Andi Kleen05484292013-01-24 16:10:29 +0100659 if (opts->sample_weight)
Adrian Hunter1e7ed5e2013-11-01 15:51:35 +0200660 perf_evsel__set_sample_bit(evsel, WEIGHT);
Andi Kleen05484292013-01-24 16:10:29 +0100661
Stephane Eranian5c5e8542013-08-21 12:10:25 +0200662 attr->mmap = track;
Don Zickusa5a5ba72014-05-30 10:49:42 -0400663 attr->mmap2 = track && !perf_missing_features.mmap2;
Stephane Eranian5c5e8542013-08-21 12:10:25 +0200664 attr->comm = track;
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200665
Andi Kleen475eeab2013-09-20 07:40:43 -0700666 if (opts->sample_transaction)
Adrian Hunter1e7ed5e2013-11-01 15:51:35 +0200667 perf_evsel__set_sample_bit(evsel, TRANSACTION);
Andi Kleen475eeab2013-09-20 07:40:43 -0700668
Jiri Olsa774cb492012-11-12 18:34:01 +0100669 /*
670 * XXX see the function comment above
671 *
672 * Disabling only independent events or group leaders,
673 * keeping group members enabled.
674 */
Namhyung Kim823254e2012-11-29 15:38:30 +0900675 if (perf_evsel__is_group_leader(evsel))
Jiri Olsa774cb492012-11-12 18:34:01 +0100676 attr->disabled = 1;
677
678 /*
679 * Setting enable_on_exec for independent events and
680 * group leaders for traced executed by perf.
681 */
Andi Kleen6619a532014-01-11 13:38:27 -0800682 if (target__none(&opts->target) && perf_evsel__is_group_leader(evsel) &&
683 !opts->initial_delay)
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200684 attr->enable_on_exec = 1;
Adrian Hunter2afd2bc2014-07-14 13:02:57 +0300685
686 if (evsel->immediate) {
687 attr->disabled = 0;
688 attr->enable_on_exec = 0;
689 }
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200690}
691
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200692int perf_evsel__alloc_fd(struct perf_evsel *evsel, int ncpus, int nthreads)
693{
David Ahern4af4c952011-05-27 09:58:34 -0600694 int cpu, thread;
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200695 evsel->fd = xyarray__new(ncpus, nthreads, sizeof(int));
David Ahern4af4c952011-05-27 09:58:34 -0600696
697 if (evsel->fd) {
698 for (cpu = 0; cpu < ncpus; cpu++) {
699 for (thread = 0; thread < nthreads; thread++) {
700 FD(evsel, cpu, thread) = -1;
701 }
702 }
703 }
704
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200705 return evsel->fd != NULL ? 0 : -ENOMEM;
706}
707
Andi Kleene2407be2013-08-02 17:41:10 -0700708static int perf_evsel__run_ioctl(struct perf_evsel *evsel, int ncpus, int nthreads,
709 int ioc, void *arg)
Arnaldo Carvalho de Melo745cefc2012-09-26 15:07:39 -0300710{
711 int cpu, thread;
712
713 for (cpu = 0; cpu < ncpus; cpu++) {
714 for (thread = 0; thread < nthreads; thread++) {
715 int fd = FD(evsel, cpu, thread),
Andi Kleene2407be2013-08-02 17:41:10 -0700716 err = ioctl(fd, ioc, arg);
Arnaldo Carvalho de Melo745cefc2012-09-26 15:07:39 -0300717
718 if (err)
719 return err;
720 }
721 }
722
723 return 0;
724}
725
Andi Kleene2407be2013-08-02 17:41:10 -0700726int perf_evsel__set_filter(struct perf_evsel *evsel, int ncpus, int nthreads,
727 const char *filter)
728{
729 return perf_evsel__run_ioctl(evsel, ncpus, nthreads,
730 PERF_EVENT_IOC_SET_FILTER,
731 (void *)filter);
732}
733
734int perf_evsel__enable(struct perf_evsel *evsel, int ncpus, int nthreads)
735{
736 return perf_evsel__run_ioctl(evsel, ncpus, nthreads,
737 PERF_EVENT_IOC_ENABLE,
738 0);
739}
740
Arnaldo Carvalho de Melo70db7532011-01-12 22:39:13 -0200741int perf_evsel__alloc_id(struct perf_evsel *evsel, int ncpus, int nthreads)
742{
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -0300743 evsel->sample_id = xyarray__new(ncpus, nthreads, sizeof(struct perf_sample_id));
744 if (evsel->sample_id == NULL)
745 return -ENOMEM;
746
747 evsel->id = zalloc(ncpus * nthreads * sizeof(u64));
748 if (evsel->id == NULL) {
749 xyarray__delete(evsel->sample_id);
750 evsel->sample_id = NULL;
751 return -ENOMEM;
752 }
753
754 return 0;
Arnaldo Carvalho de Melo70db7532011-01-12 22:39:13 -0200755}
756
Frederik Deweerdta7e191c2013-03-01 13:02:27 -0500757void perf_evsel__reset_counts(struct perf_evsel *evsel, int ncpus)
758{
759 memset(evsel->counts, 0, (sizeof(*evsel->counts) +
760 (ncpus * sizeof(struct perf_counts_values))));
761}
762
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200763int perf_evsel__alloc_counts(struct perf_evsel *evsel, int ncpus)
764{
765 evsel->counts = zalloc((sizeof(*evsel->counts) +
766 (ncpus * sizeof(struct perf_counts_values))));
767 return evsel->counts != NULL ? 0 : -ENOMEM;
768}
769
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200770void perf_evsel__free_fd(struct perf_evsel *evsel)
771{
772 xyarray__delete(evsel->fd);
773 evsel->fd = NULL;
774}
775
Arnaldo Carvalho de Melo70db7532011-01-12 22:39:13 -0200776void perf_evsel__free_id(struct perf_evsel *evsel)
777{
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -0300778 xyarray__delete(evsel->sample_id);
779 evsel->sample_id = NULL;
Arnaldo Carvalho de Melo04662522013-12-26 17:41:15 -0300780 zfree(&evsel->id);
Arnaldo Carvalho de Melo70db7532011-01-12 22:39:13 -0200781}
782
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200783void perf_evsel__close_fd(struct perf_evsel *evsel, int ncpus, int nthreads)
784{
785 int cpu, thread;
786
787 for (cpu = 0; cpu < ncpus; cpu++)
788 for (thread = 0; thread < nthreads; ++thread) {
789 close(FD(evsel, cpu, thread));
790 FD(evsel, cpu, thread) = -1;
791 }
792}
793
Namhyung Kim43f8e762013-01-25 10:44:44 +0900794void perf_evsel__free_counts(struct perf_evsel *evsel)
795{
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -0300796 zfree(&evsel->counts);
Namhyung Kim43f8e762013-01-25 10:44:44 +0900797}
798
Arnaldo Carvalho de Meloef1d1af2011-01-18 21:41:45 -0200799void perf_evsel__exit(struct perf_evsel *evsel)
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200800{
801 assert(list_empty(&evsel->node));
Namhyung Kim736b05a2013-03-15 14:48:49 +0900802 perf_evsel__free_fd(evsel);
803 perf_evsel__free_id(evsel);
Arnaldo Carvalho de Meloef1d1af2011-01-18 21:41:45 -0200804}
805
806void perf_evsel__delete(struct perf_evsel *evsel)
807{
808 perf_evsel__exit(evsel);
Stephane Eranian023695d2011-02-14 11:20:01 +0200809 close_cgroup(evsel->cgrp);
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -0300810 zfree(&evsel->group_name);
Arnaldo Carvalho de Meloe48ffe22012-09-26 17:11:38 -0300811 if (evsel->tp_format)
Arnaldo Carvalho de Meloefd2b922012-09-18 11:21:50 -0300812 pevent_free_format(evsel->tp_format);
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -0300813 zfree(&evsel->name);
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200814 free(evsel);
815}
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200816
Stephane Eranianc7a79c42013-01-29 12:47:43 +0100817static inline void compute_deltas(struct perf_evsel *evsel,
818 int cpu,
819 struct perf_counts_values *count)
820{
821 struct perf_counts_values tmp;
822
823 if (!evsel->prev_raw_counts)
824 return;
825
826 if (cpu == -1) {
827 tmp = evsel->prev_raw_counts->aggr;
828 evsel->prev_raw_counts->aggr = *count;
829 } else {
830 tmp = evsel->prev_raw_counts->cpu[cpu];
831 evsel->prev_raw_counts->cpu[cpu] = *count;
832 }
833
834 count->val = count->val - tmp.val;
835 count->ena = count->ena - tmp.ena;
836 count->run = count->run - tmp.run;
837}
838
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200839int __perf_evsel__read_on_cpu(struct perf_evsel *evsel,
840 int cpu, int thread, bool scale)
841{
842 struct perf_counts_values count;
843 size_t nv = scale ? 3 : 1;
844
845 if (FD(evsel, cpu, thread) < 0)
846 return -EINVAL;
847
Arnaldo Carvalho de Melo4eed11d2011-01-04 00:13:17 -0200848 if (evsel->counts == NULL && perf_evsel__alloc_counts(evsel, cpu + 1) < 0)
849 return -ENOMEM;
850
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200851 if (readn(FD(evsel, cpu, thread), &count, nv * sizeof(u64)) < 0)
852 return -errno;
853
Stephane Eranianc7a79c42013-01-29 12:47:43 +0100854 compute_deltas(evsel, cpu, &count);
855
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200856 if (scale) {
857 if (count.run == 0)
858 count.val = 0;
859 else if (count.run < count.ena)
860 count.val = (u64)((double)count.val * count.ena / count.run + 0.5);
861 } else
862 count.ena = count.run = 0;
863
864 evsel->counts->cpu[cpu] = count;
865 return 0;
866}
867
868int __perf_evsel__read(struct perf_evsel *evsel,
869 int ncpus, int nthreads, bool scale)
870{
871 size_t nv = scale ? 3 : 1;
872 int cpu, thread;
873 struct perf_counts_values *aggr = &evsel->counts->aggr, count;
874
Arnaldo Carvalho de Melo52bcd9942011-02-03 17:26:06 -0200875 aggr->val = aggr->ena = aggr->run = 0;
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200876
877 for (cpu = 0; cpu < ncpus; cpu++) {
878 for (thread = 0; thread < nthreads; thread++) {
879 if (FD(evsel, cpu, thread) < 0)
880 continue;
881
882 if (readn(FD(evsel, cpu, thread),
883 &count, nv * sizeof(u64)) < 0)
884 return -errno;
885
886 aggr->val += count.val;
887 if (scale) {
888 aggr->ena += count.ena;
889 aggr->run += count.run;
890 }
891 }
892 }
893
Stephane Eranianc7a79c42013-01-29 12:47:43 +0100894 compute_deltas(evsel, -1, aggr);
895
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200896 evsel->counts->scaled = 0;
897 if (scale) {
898 if (aggr->run == 0) {
899 evsel->counts->scaled = -1;
900 aggr->val = 0;
901 return 0;
902 }
903
904 if (aggr->run < aggr->ena) {
905 evsel->counts->scaled = 1;
906 aggr->val = (u64)((double)aggr->val * aggr->ena / aggr->run + 0.5);
907 }
908 } else
909 aggr->ena = aggr->run = 0;
910
911 return 0;
912}
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -0200913
Jiri Olsa6a4bb042012-08-08 12:22:36 +0200914static int get_group_fd(struct perf_evsel *evsel, int cpu, int thread)
915{
916 struct perf_evsel *leader = evsel->leader;
917 int fd;
918
Namhyung Kim823254e2012-11-29 15:38:30 +0900919 if (perf_evsel__is_group_leader(evsel))
Jiri Olsa6a4bb042012-08-08 12:22:36 +0200920 return -1;
921
922 /*
923 * Leader must be already processed/open,
924 * if not it's a bug.
925 */
926 BUG_ON(!leader->fd);
927
928 fd = FD(leader, cpu, thread);
929 BUG_ON(fd == -1);
930
931 return fd;
932}
933
Adrian Huntere3e1a542013-08-14 15:48:24 +0300934#define __PRINT_ATTR(fmt, cast, field) \
935 fprintf(fp, " %-19s "fmt"\n", #field, cast attr->field)
936
937#define PRINT_ATTR_U32(field) __PRINT_ATTR("%u" , , field)
938#define PRINT_ATTR_X32(field) __PRINT_ATTR("%#x", , field)
939#define PRINT_ATTR_U64(field) __PRINT_ATTR("%" PRIu64, (uint64_t), field)
940#define PRINT_ATTR_X64(field) __PRINT_ATTR("%#"PRIx64, (uint64_t), field)
941
942#define PRINT_ATTR2N(name1, field1, name2, field2) \
943 fprintf(fp, " %-19s %u %-19s %u\n", \
944 name1, attr->field1, name2, attr->field2)
945
946#define PRINT_ATTR2(field1, field2) \
947 PRINT_ATTR2N(#field1, field1, #field2, field2)
948
949static size_t perf_event_attr__fprintf(struct perf_event_attr *attr, FILE *fp)
950{
951 size_t ret = 0;
952
953 ret += fprintf(fp, "%.60s\n", graph_dotted_line);
954 ret += fprintf(fp, "perf_event_attr:\n");
955
956 ret += PRINT_ATTR_U32(type);
957 ret += PRINT_ATTR_U32(size);
958 ret += PRINT_ATTR_X64(config);
959 ret += PRINT_ATTR_U64(sample_period);
960 ret += PRINT_ATTR_U64(sample_freq);
961 ret += PRINT_ATTR_X64(sample_type);
962 ret += PRINT_ATTR_X64(read_format);
963
964 ret += PRINT_ATTR2(disabled, inherit);
965 ret += PRINT_ATTR2(pinned, exclusive);
966 ret += PRINT_ATTR2(exclude_user, exclude_kernel);
967 ret += PRINT_ATTR2(exclude_hv, exclude_idle);
968 ret += PRINT_ATTR2(mmap, comm);
Adrian Hunter022c50d2014-07-14 13:02:27 +0300969 ret += PRINT_ATTR2(mmap2, comm_exec);
Adrian Huntere3e1a542013-08-14 15:48:24 +0300970 ret += PRINT_ATTR2(freq, inherit_stat);
971 ret += PRINT_ATTR2(enable_on_exec, task);
972 ret += PRINT_ATTR2(watermark, precise_ip);
973 ret += PRINT_ATTR2(mmap_data, sample_id_all);
974 ret += PRINT_ATTR2(exclude_host, exclude_guest);
975 ret += PRINT_ATTR2N("excl.callchain_kern", exclude_callchain_kernel,
976 "excl.callchain_user", exclude_callchain_user);
977
978 ret += PRINT_ATTR_U32(wakeup_events);
979 ret += PRINT_ATTR_U32(wakeup_watermark);
980 ret += PRINT_ATTR_X32(bp_type);
981 ret += PRINT_ATTR_X64(bp_addr);
982 ret += PRINT_ATTR_X64(config1);
983 ret += PRINT_ATTR_U64(bp_len);
984 ret += PRINT_ATTR_X64(config2);
985 ret += PRINT_ATTR_X64(branch_sample_type);
986 ret += PRINT_ATTR_X64(sample_regs_user);
987 ret += PRINT_ATTR_U32(sample_stack_user);
988
989 ret += fprintf(fp, "%.60s\n", graph_dotted_line);
990
991 return ret;
992}
993
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -0200994static int __perf_evsel__open(struct perf_evsel *evsel, struct cpu_map *cpus,
Jiri Olsa6a4bb042012-08-08 12:22:36 +0200995 struct thread_map *threads)
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -0200996{
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -0200997 int cpu, thread;
Yann Droneaud57480d22014-06-30 22:28:47 +0200998 unsigned long flags = PERF_FLAG_FD_CLOEXEC;
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -0200999 int pid = -1, err;
Andi Kleenbec19672013-08-04 19:41:26 -07001000 enum { NO_CHANGE, SET_TO_MAX, INCREASED_MAX } set_rlimit = NO_CHANGE;
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -02001001
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -02001002 if (evsel->fd == NULL &&
1003 perf_evsel__alloc_fd(evsel, cpus->nr, threads->nr) < 0)
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -02001004 return -ENOMEM;
Arnaldo Carvalho de Melo4eed11d2011-01-04 00:13:17 -02001005
Stephane Eranian023695d2011-02-14 11:20:01 +02001006 if (evsel->cgrp) {
Yann Droneaud57480d22014-06-30 22:28:47 +02001007 flags |= PERF_FLAG_PID_CGROUP;
Stephane Eranian023695d2011-02-14 11:20:01 +02001008 pid = evsel->cgrp->fd;
1009 }
1010
Arnaldo Carvalho de Melo594ac612012-12-13 13:13:07 -03001011fallback_missing_features:
Yann Droneaud57480d22014-06-30 22:28:47 +02001012 if (perf_missing_features.cloexec)
1013 flags &= ~(unsigned long)PERF_FLAG_FD_CLOEXEC;
Stephane Eranian5c5e8542013-08-21 12:10:25 +02001014 if (perf_missing_features.mmap2)
1015 evsel->attr.mmap2 = 0;
Arnaldo Carvalho de Melo594ac612012-12-13 13:13:07 -03001016 if (perf_missing_features.exclude_guest)
1017 evsel->attr.exclude_guest = evsel->attr.exclude_host = 0;
1018retry_sample_id:
1019 if (perf_missing_features.sample_id_all)
1020 evsel->attr.sample_id_all = 0;
1021
Adrian Huntere3e1a542013-08-14 15:48:24 +03001022 if (verbose >= 2)
1023 perf_event_attr__fprintf(&evsel->attr, stderr);
1024
Arnaldo Carvalho de Melo86bd5e82011-01-03 23:09:46 -02001025 for (cpu = 0; cpu < cpus->nr; cpu++) {
Arnaldo Carvalho de Melo9d04f172011-01-12 00:08:18 -02001026
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -02001027 for (thread = 0; thread < threads->nr; thread++) {
Jiri Olsa6a4bb042012-08-08 12:22:36 +02001028 int group_fd;
Stephane Eranian023695d2011-02-14 11:20:01 +02001029
1030 if (!evsel->cgrp)
1031 pid = threads->map[thread];
1032
Jiri Olsa6a4bb042012-08-08 12:22:36 +02001033 group_fd = get_group_fd(evsel, cpu, thread);
Andi Kleenbec19672013-08-04 19:41:26 -07001034retry_open:
Ramkumar Ramachandraa33f6ef2014-03-18 15:10:42 -04001035 pr_debug2("sys_perf_event_open: pid %d cpu %d group_fd %d flags %#lx\n",
Adrian Huntere3e1a542013-08-14 15:48:24 +03001036 pid, cpus->map[cpu], group_fd, flags);
1037
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -02001038 FD(evsel, cpu, thread) = sys_perf_event_open(&evsel->attr,
Stephane Eranian023695d2011-02-14 11:20:01 +02001039 pid,
Arnaldo Carvalho de Melof08199d2011-01-11 23:42:19 -02001040 cpus->map[cpu],
Stephane Eranian023695d2011-02-14 11:20:01 +02001041 group_fd, flags);
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -02001042 if (FD(evsel, cpu, thread) < 0) {
1043 err = -errno;
Ramkumar Ramachandraa33f6ef2014-03-18 15:10:42 -04001044 pr_debug2("sys_perf_event_open failed, error %d\n",
Adrian Hunterf852fd62013-11-01 15:51:29 +02001045 err);
Arnaldo Carvalho de Melo594ac612012-12-13 13:13:07 -03001046 goto try_fallback;
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -02001047 }
Andi Kleenbec19672013-08-04 19:41:26 -07001048 set_rlimit = NO_CHANGE;
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -02001049 }
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -02001050 }
1051
1052 return 0;
1053
Arnaldo Carvalho de Melo594ac612012-12-13 13:13:07 -03001054try_fallback:
Andi Kleenbec19672013-08-04 19:41:26 -07001055 /*
1056 * perf stat needs between 5 and 22 fds per CPU. When we run out
1057 * of them try to increase the limits.
1058 */
1059 if (err == -EMFILE && set_rlimit < INCREASED_MAX) {
1060 struct rlimit l;
1061 int old_errno = errno;
1062
1063 if (getrlimit(RLIMIT_NOFILE, &l) == 0) {
1064 if (set_rlimit == NO_CHANGE)
1065 l.rlim_cur = l.rlim_max;
1066 else {
1067 l.rlim_cur = l.rlim_max + 1000;
1068 l.rlim_max = l.rlim_cur;
1069 }
1070 if (setrlimit(RLIMIT_NOFILE, &l) == 0) {
1071 set_rlimit++;
1072 errno = old_errno;
1073 goto retry_open;
1074 }
1075 }
1076 errno = old_errno;
1077 }
1078
Arnaldo Carvalho de Melo594ac612012-12-13 13:13:07 -03001079 if (err != -EINVAL || cpu > 0 || thread > 0)
1080 goto out_close;
1081
Yann Droneaud57480d22014-06-30 22:28:47 +02001082 if (!perf_missing_features.cloexec && (flags & PERF_FLAG_FD_CLOEXEC)) {
1083 perf_missing_features.cloexec = true;
1084 goto fallback_missing_features;
1085 } else if (!perf_missing_features.mmap2 && evsel->attr.mmap2) {
Stephane Eranian5c5e8542013-08-21 12:10:25 +02001086 perf_missing_features.mmap2 = true;
1087 goto fallback_missing_features;
1088 } else if (!perf_missing_features.exclude_guest &&
1089 (evsel->attr.exclude_guest || evsel->attr.exclude_host)) {
Arnaldo Carvalho de Melo594ac612012-12-13 13:13:07 -03001090 perf_missing_features.exclude_guest = true;
1091 goto fallback_missing_features;
1092 } else if (!perf_missing_features.sample_id_all) {
1093 perf_missing_features.sample_id_all = true;
1094 goto retry_sample_id;
1095 }
1096
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -02001097out_close:
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -02001098 do {
1099 while (--thread >= 0) {
1100 close(FD(evsel, cpu, thread));
1101 FD(evsel, cpu, thread) = -1;
1102 }
1103 thread = threads->nr;
1104 } while (--cpu >= 0);
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -02001105 return err;
1106}
1107
1108void perf_evsel__close(struct perf_evsel *evsel, int ncpus, int nthreads)
1109{
1110 if (evsel->fd == NULL)
1111 return;
1112
1113 perf_evsel__close_fd(evsel, ncpus, nthreads);
1114 perf_evsel__free_fd(evsel);
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -02001115}
1116
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -02001117static struct {
1118 struct cpu_map map;
1119 int cpus[1];
1120} empty_cpu_map = {
1121 .map.nr = 1,
1122 .cpus = { -1, },
1123};
1124
1125static struct {
1126 struct thread_map map;
1127 int threads[1];
1128} empty_thread_map = {
1129 .map.nr = 1,
1130 .threads = { -1, },
1131};
1132
Arnaldo Carvalho de Melof08199d2011-01-11 23:42:19 -02001133int perf_evsel__open(struct perf_evsel *evsel, struct cpu_map *cpus,
Jiri Olsa6a4bb042012-08-08 12:22:36 +02001134 struct thread_map *threads)
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -02001135{
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -02001136 if (cpus == NULL) {
1137 /* Work around old compiler warnings about strict aliasing */
1138 cpus = &empty_cpu_map.map;
1139 }
1140
1141 if (threads == NULL)
1142 threads = &empty_thread_map.map;
1143
Jiri Olsa6a4bb042012-08-08 12:22:36 +02001144 return __perf_evsel__open(evsel, cpus, threads);
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -02001145}
1146
Arnaldo Carvalho de Melof08199d2011-01-11 23:42:19 -02001147int perf_evsel__open_per_cpu(struct perf_evsel *evsel,
Jiri Olsa6a4bb042012-08-08 12:22:36 +02001148 struct cpu_map *cpus)
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -02001149{
Jiri Olsa6a4bb042012-08-08 12:22:36 +02001150 return __perf_evsel__open(evsel, cpus, &empty_thread_map.map);
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -02001151}
1152
Arnaldo Carvalho de Melof08199d2011-01-11 23:42:19 -02001153int perf_evsel__open_per_thread(struct perf_evsel *evsel,
Jiri Olsa6a4bb042012-08-08 12:22:36 +02001154 struct thread_map *threads)
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -02001155{
Jiri Olsa6a4bb042012-08-08 12:22:36 +02001156 return __perf_evsel__open(evsel, &empty_cpu_map.map, threads);
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -02001157}
Arnaldo Carvalho de Melo70082dd2011-01-12 17:03:24 -02001158
Arnaldo Carvalho de Melo0807d2d2012-09-26 12:48:18 -03001159static int perf_evsel__parse_id_sample(const struct perf_evsel *evsel,
1160 const union perf_event *event,
1161 struct perf_sample *sample)
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001162{
Arnaldo Carvalho de Melo0807d2d2012-09-26 12:48:18 -03001163 u64 type = evsel->attr.sample_type;
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001164 const u64 *array = event->sample.array;
Arnaldo Carvalho de Melo0807d2d2012-09-26 12:48:18 -03001165 bool swapped = evsel->needs_swap;
Jiri Olsa37073f92012-05-30 14:23:44 +02001166 union u64_swap u;
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001167
1168 array += ((event->header.size -
1169 sizeof(event->header)) / sizeof(u64)) - 1;
1170
Adrian Hunter75562572013-08-27 11:23:09 +03001171 if (type & PERF_SAMPLE_IDENTIFIER) {
1172 sample->id = *array;
1173 array--;
1174 }
1175
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001176 if (type & PERF_SAMPLE_CPU) {
Jiri Olsa37073f92012-05-30 14:23:44 +02001177 u.val64 = *array;
1178 if (swapped) {
1179 /* undo swap of u64, then swap on individual u32s */
1180 u.val64 = bswap_64(u.val64);
1181 u.val32[0] = bswap_32(u.val32[0]);
1182 }
1183
1184 sample->cpu = u.val32[0];
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001185 array--;
1186 }
1187
1188 if (type & PERF_SAMPLE_STREAM_ID) {
1189 sample->stream_id = *array;
1190 array--;
1191 }
1192
1193 if (type & PERF_SAMPLE_ID) {
1194 sample->id = *array;
1195 array--;
1196 }
1197
1198 if (type & PERF_SAMPLE_TIME) {
1199 sample->time = *array;
1200 array--;
1201 }
1202
1203 if (type & PERF_SAMPLE_TID) {
Jiri Olsa37073f92012-05-30 14:23:44 +02001204 u.val64 = *array;
1205 if (swapped) {
1206 /* undo swap of u64, then swap on individual u32s */
1207 u.val64 = bswap_64(u.val64);
1208 u.val32[0] = bswap_32(u.val32[0]);
1209 u.val32[1] = bswap_32(u.val32[1]);
1210 }
1211
1212 sample->pid = u.val32[0];
1213 sample->tid = u.val32[1];
Adrian Hunterdd44bc62013-10-18 15:29:01 +03001214 array--;
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001215 }
1216
1217 return 0;
1218}
1219
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001220static inline bool overflow(const void *endp, u16 max_size, const void *offset,
1221 u64 size)
Frederic Weisbecker98e1da92011-05-21 20:08:15 +02001222{
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001223 return size > max_size || offset + size > endp;
Frederic Weisbecker98e1da92011-05-21 20:08:15 +02001224}
1225
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001226#define OVERFLOW_CHECK(offset, size, max_size) \
1227 do { \
1228 if (overflow(endp, (max_size), (offset), (size))) \
1229 return -EFAULT; \
1230 } while (0)
1231
1232#define OVERFLOW_CHECK_u64(offset) \
1233 OVERFLOW_CHECK(offset, sizeof(u64), sizeof(u64))
1234
Arnaldo Carvalho de Meloa3f698f2012-08-02 12:23:46 -03001235int perf_evsel__parse_sample(struct perf_evsel *evsel, union perf_event *event,
Arnaldo Carvalho de Melo0807d2d2012-09-26 12:48:18 -03001236 struct perf_sample *data)
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001237{
Arnaldo Carvalho de Meloa3f698f2012-08-02 12:23:46 -03001238 u64 type = evsel->attr.sample_type;
Arnaldo Carvalho de Melo0807d2d2012-09-26 12:48:18 -03001239 bool swapped = evsel->needs_swap;
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001240 const u64 *array;
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001241 u16 max_size = event->header.size;
1242 const void *endp = (void *)event + max_size;
1243 u64 sz;
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001244
David Ahern936be502011-09-06 09:12:26 -06001245 /*
1246 * used for cross-endian analysis. See git commit 65014ab3
1247 * for why this goofiness is needed.
1248 */
Jiri Olsa6a11f922012-05-16 08:59:04 +02001249 union u64_swap u;
David Ahern936be502011-09-06 09:12:26 -06001250
Robert Richterf3bda2c2011-12-15 17:32:39 +01001251 memset(data, 0, sizeof(*data));
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001252 data->cpu = data->pid = data->tid = -1;
1253 data->stream_id = data->id = data->time = -1ULL;
Jiri Olsabc529082014-02-03 12:44:41 +01001254 data->period = evsel->attr.sample_period;
Andi Kleen05484292013-01-24 16:10:29 +01001255 data->weight = 0;
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001256
1257 if (event->header.type != PERF_RECORD_SAMPLE) {
Arnaldo Carvalho de Meloa3f698f2012-08-02 12:23:46 -03001258 if (!evsel->attr.sample_id_all)
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001259 return 0;
Arnaldo Carvalho de Melo0807d2d2012-09-26 12:48:18 -03001260 return perf_evsel__parse_id_sample(evsel, event, data);
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001261 }
1262
1263 array = event->sample.array;
1264
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001265 /*
1266 * The evsel's sample_size is based on PERF_SAMPLE_MASK which includes
1267 * up to PERF_SAMPLE_PERIOD. After that overflow() must be used to
1268 * check the format does not go past the end of the event.
1269 */
Arnaldo Carvalho de Meloa3f698f2012-08-02 12:23:46 -03001270 if (evsel->sample_size + sizeof(event->header) > event->header.size)
Frederic Weisbeckera2854122011-05-21 19:33:04 +02001271 return -EFAULT;
1272
Adrian Hunter75562572013-08-27 11:23:09 +03001273 data->id = -1ULL;
1274 if (type & PERF_SAMPLE_IDENTIFIER) {
1275 data->id = *array;
1276 array++;
1277 }
1278
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001279 if (type & PERF_SAMPLE_IP) {
Adrian Hunteref893252013-08-27 11:23:06 +03001280 data->ip = *array;
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001281 array++;
1282 }
1283
1284 if (type & PERF_SAMPLE_TID) {
David Ahern936be502011-09-06 09:12:26 -06001285 u.val64 = *array;
1286 if (swapped) {
1287 /* undo swap of u64, then swap on individual u32s */
1288 u.val64 = bswap_64(u.val64);
1289 u.val32[0] = bswap_32(u.val32[0]);
1290 u.val32[1] = bswap_32(u.val32[1]);
1291 }
1292
1293 data->pid = u.val32[0];
1294 data->tid = u.val32[1];
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001295 array++;
1296 }
1297
1298 if (type & PERF_SAMPLE_TIME) {
1299 data->time = *array;
1300 array++;
1301 }
1302
David Ahern7cec0922011-05-30 13:08:23 -06001303 data->addr = 0;
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001304 if (type & PERF_SAMPLE_ADDR) {
1305 data->addr = *array;
1306 array++;
1307 }
1308
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001309 if (type & PERF_SAMPLE_ID) {
1310 data->id = *array;
1311 array++;
1312 }
1313
1314 if (type & PERF_SAMPLE_STREAM_ID) {
1315 data->stream_id = *array;
1316 array++;
1317 }
1318
1319 if (type & PERF_SAMPLE_CPU) {
David Ahern936be502011-09-06 09:12:26 -06001320
1321 u.val64 = *array;
1322 if (swapped) {
1323 /* undo swap of u64, then swap on individual u32s */
1324 u.val64 = bswap_64(u.val64);
1325 u.val32[0] = bswap_32(u.val32[0]);
1326 }
1327
1328 data->cpu = u.val32[0];
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001329 array++;
1330 }
1331
1332 if (type & PERF_SAMPLE_PERIOD) {
1333 data->period = *array;
1334 array++;
1335 }
1336
1337 if (type & PERF_SAMPLE_READ) {
Jiri Olsa9ede4732012-10-10 17:38:13 +02001338 u64 read_format = evsel->attr.read_format;
1339
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001340 OVERFLOW_CHECK_u64(array);
Jiri Olsa9ede4732012-10-10 17:38:13 +02001341 if (read_format & PERF_FORMAT_GROUP)
1342 data->read.group.nr = *array;
1343 else
1344 data->read.one.value = *array;
1345
1346 array++;
1347
1348 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) {
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001349 OVERFLOW_CHECK_u64(array);
Jiri Olsa9ede4732012-10-10 17:38:13 +02001350 data->read.time_enabled = *array;
1351 array++;
1352 }
1353
1354 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) {
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001355 OVERFLOW_CHECK_u64(array);
Jiri Olsa9ede4732012-10-10 17:38:13 +02001356 data->read.time_running = *array;
1357 array++;
1358 }
1359
1360 /* PERF_FORMAT_ID is forced for PERF_SAMPLE_READ */
1361 if (read_format & PERF_FORMAT_GROUP) {
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001362 const u64 max_group_nr = UINT64_MAX /
1363 sizeof(struct sample_read_value);
1364
1365 if (data->read.group.nr > max_group_nr)
1366 return -EFAULT;
1367 sz = data->read.group.nr *
1368 sizeof(struct sample_read_value);
1369 OVERFLOW_CHECK(array, sz, max_size);
1370 data->read.group.values =
1371 (struct sample_read_value *)array;
1372 array = (void *)array + sz;
Jiri Olsa9ede4732012-10-10 17:38:13 +02001373 } else {
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001374 OVERFLOW_CHECK_u64(array);
Jiri Olsa9ede4732012-10-10 17:38:13 +02001375 data->read.one.id = *array;
1376 array++;
1377 }
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001378 }
1379
1380 if (type & PERF_SAMPLE_CALLCHAIN) {
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001381 const u64 max_callchain_nr = UINT64_MAX / sizeof(u64);
1382
1383 OVERFLOW_CHECK_u64(array);
1384 data->callchain = (struct ip_callchain *)array++;
1385 if (data->callchain->nr > max_callchain_nr)
Frederic Weisbecker98e1da92011-05-21 20:08:15 +02001386 return -EFAULT;
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001387 sz = data->callchain->nr * sizeof(u64);
1388 OVERFLOW_CHECK(array, sz, max_size);
1389 array = (void *)array + sz;
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001390 }
1391
1392 if (type & PERF_SAMPLE_RAW) {
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001393 OVERFLOW_CHECK_u64(array);
David Ahern936be502011-09-06 09:12:26 -06001394 u.val64 = *array;
1395 if (WARN_ONCE(swapped,
1396 "Endianness of raw data not corrected!\n")) {
1397 /* undo swap of u64, then swap on individual u32s */
1398 u.val64 = bswap_64(u.val64);
1399 u.val32[0] = bswap_32(u.val32[0]);
1400 u.val32[1] = bswap_32(u.val32[1]);
1401 }
David Ahern936be502011-09-06 09:12:26 -06001402 data->raw_size = u.val32[0];
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001403 array = (void *)array + sizeof(u32);
Frederic Weisbecker98e1da92011-05-21 20:08:15 +02001404
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001405 OVERFLOW_CHECK(array, data->raw_size, max_size);
1406 data->raw_data = (void *)array;
1407 array = (void *)array + data->raw_size;
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001408 }
1409
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +01001410 if (type & PERF_SAMPLE_BRANCH_STACK) {
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001411 const u64 max_branch_nr = UINT64_MAX /
1412 sizeof(struct branch_entry);
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +01001413
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001414 OVERFLOW_CHECK_u64(array);
1415 data->branch_stack = (struct branch_stack *)array++;
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +01001416
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001417 if (data->branch_stack->nr > max_branch_nr)
1418 return -EFAULT;
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +01001419 sz = data->branch_stack->nr * sizeof(struct branch_entry);
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001420 OVERFLOW_CHECK(array, sz, max_size);
1421 array = (void *)array + sz;
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +01001422 }
Jiri Olsa0f6a3012012-08-07 15:20:45 +02001423
1424 if (type & PERF_SAMPLE_REGS_USER) {
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001425 OVERFLOW_CHECK_u64(array);
Adrian Hunter5b95a4a32013-08-27 11:23:10 +03001426 data->user_regs.abi = *array;
1427 array++;
Jiri Olsa0f6a3012012-08-07 15:20:45 +02001428
Adrian Hunter5b95a4a32013-08-27 11:23:10 +03001429 if (data->user_regs.abi) {
Jiri Olsa352ea452014-01-07 13:47:25 +01001430 u64 mask = evsel->attr.sample_regs_user;
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001431
Jiri Olsa352ea452014-01-07 13:47:25 +01001432 sz = hweight_long(mask) * sizeof(u64);
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001433 OVERFLOW_CHECK(array, sz, max_size);
Jiri Olsa352ea452014-01-07 13:47:25 +01001434 data->user_regs.mask = mask;
Jiri Olsa0f6a3012012-08-07 15:20:45 +02001435 data->user_regs.regs = (u64 *)array;
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001436 array = (void *)array + sz;
Jiri Olsa0f6a3012012-08-07 15:20:45 +02001437 }
1438 }
1439
1440 if (type & PERF_SAMPLE_STACK_USER) {
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001441 OVERFLOW_CHECK_u64(array);
1442 sz = *array++;
Jiri Olsa0f6a3012012-08-07 15:20:45 +02001443
1444 data->user_stack.offset = ((char *)(array - 1)
1445 - (char *) event);
1446
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001447 if (!sz) {
Jiri Olsa0f6a3012012-08-07 15:20:45 +02001448 data->user_stack.size = 0;
1449 } else {
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001450 OVERFLOW_CHECK(array, sz, max_size);
Jiri Olsa0f6a3012012-08-07 15:20:45 +02001451 data->user_stack.data = (char *)array;
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001452 array = (void *)array + sz;
1453 OVERFLOW_CHECK_u64(array);
Adrian Hunter54bd2692013-07-04 16:20:34 +03001454 data->user_stack.size = *array++;
Jiri Olsaa65cb4b2013-10-02 15:46:39 +02001455 if (WARN_ONCE(data->user_stack.size > sz,
1456 "user stack dump failure\n"))
1457 return -EFAULT;
Jiri Olsa0f6a3012012-08-07 15:20:45 +02001458 }
1459 }
1460
Andi Kleen05484292013-01-24 16:10:29 +01001461 data->weight = 0;
1462 if (type & PERF_SAMPLE_WEIGHT) {
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001463 OVERFLOW_CHECK_u64(array);
Andi Kleen05484292013-01-24 16:10:29 +01001464 data->weight = *array;
1465 array++;
1466 }
1467
Stephane Eranian98a3b322013-01-24 16:10:35 +01001468 data->data_src = PERF_MEM_DATA_SRC_NONE;
1469 if (type & PERF_SAMPLE_DATA_SRC) {
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001470 OVERFLOW_CHECK_u64(array);
Stephane Eranian98a3b322013-01-24 16:10:35 +01001471 data->data_src = *array;
1472 array++;
1473 }
1474
Andi Kleen475eeab2013-09-20 07:40:43 -07001475 data->transaction = 0;
1476 if (type & PERF_SAMPLE_TRANSACTION) {
Adrian Hunter87b95522013-11-01 15:51:36 +02001477 OVERFLOW_CHECK_u64(array);
Andi Kleen475eeab2013-09-20 07:40:43 -07001478 data->transaction = *array;
1479 array++;
1480 }
1481
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001482 return 0;
1483}
Andrew Vagin74eec262011-11-28 12:03:31 +03001484
Adrian Hunterb1cf6f62013-08-27 11:23:12 +03001485size_t perf_event__sample_event_size(const struct perf_sample *sample, u64 type,
Jiri Olsa352ea452014-01-07 13:47:25 +01001486 u64 read_format)
Adrian Hunterb1cf6f62013-08-27 11:23:12 +03001487{
1488 size_t sz, result = sizeof(struct sample_event);
1489
1490 if (type & PERF_SAMPLE_IDENTIFIER)
1491 result += sizeof(u64);
1492
1493 if (type & PERF_SAMPLE_IP)
1494 result += sizeof(u64);
1495
1496 if (type & PERF_SAMPLE_TID)
1497 result += sizeof(u64);
1498
1499 if (type & PERF_SAMPLE_TIME)
1500 result += sizeof(u64);
1501
1502 if (type & PERF_SAMPLE_ADDR)
1503 result += sizeof(u64);
1504
1505 if (type & PERF_SAMPLE_ID)
1506 result += sizeof(u64);
1507
1508 if (type & PERF_SAMPLE_STREAM_ID)
1509 result += sizeof(u64);
1510
1511 if (type & PERF_SAMPLE_CPU)
1512 result += sizeof(u64);
1513
1514 if (type & PERF_SAMPLE_PERIOD)
1515 result += sizeof(u64);
1516
1517 if (type & PERF_SAMPLE_READ) {
1518 result += sizeof(u64);
1519 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
1520 result += sizeof(u64);
1521 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
1522 result += sizeof(u64);
1523 /* PERF_FORMAT_ID is forced for PERF_SAMPLE_READ */
1524 if (read_format & PERF_FORMAT_GROUP) {
1525 sz = sample->read.group.nr *
1526 sizeof(struct sample_read_value);
1527 result += sz;
1528 } else {
1529 result += sizeof(u64);
1530 }
1531 }
1532
1533 if (type & PERF_SAMPLE_CALLCHAIN) {
1534 sz = (sample->callchain->nr + 1) * sizeof(u64);
1535 result += sz;
1536 }
1537
1538 if (type & PERF_SAMPLE_RAW) {
1539 result += sizeof(u32);
1540 result += sample->raw_size;
1541 }
1542
1543 if (type & PERF_SAMPLE_BRANCH_STACK) {
1544 sz = sample->branch_stack->nr * sizeof(struct branch_entry);
1545 sz += sizeof(u64);
1546 result += sz;
1547 }
1548
1549 if (type & PERF_SAMPLE_REGS_USER) {
1550 if (sample->user_regs.abi) {
1551 result += sizeof(u64);
Jiri Olsa352ea452014-01-07 13:47:25 +01001552 sz = hweight_long(sample->user_regs.mask) * sizeof(u64);
Adrian Hunterb1cf6f62013-08-27 11:23:12 +03001553 result += sz;
1554 } else {
1555 result += sizeof(u64);
1556 }
1557 }
1558
1559 if (type & PERF_SAMPLE_STACK_USER) {
1560 sz = sample->user_stack.size;
1561 result += sizeof(u64);
1562 if (sz) {
1563 result += sz;
1564 result += sizeof(u64);
1565 }
1566 }
1567
1568 if (type & PERF_SAMPLE_WEIGHT)
1569 result += sizeof(u64);
1570
1571 if (type & PERF_SAMPLE_DATA_SRC)
1572 result += sizeof(u64);
1573
Adrian Hunter42d88912013-11-01 15:51:38 +02001574 if (type & PERF_SAMPLE_TRANSACTION)
1575 result += sizeof(u64);
1576
Adrian Hunterb1cf6f62013-08-27 11:23:12 +03001577 return result;
1578}
1579
Andrew Vagin74eec262011-11-28 12:03:31 +03001580int perf_event__synthesize_sample(union perf_event *event, u64 type,
Jiri Olsa352ea452014-01-07 13:47:25 +01001581 u64 read_format,
Andrew Vagin74eec262011-11-28 12:03:31 +03001582 const struct perf_sample *sample,
1583 bool swapped)
1584{
1585 u64 *array;
Adrian Hunterd03f2172013-08-27 11:23:11 +03001586 size_t sz;
Andrew Vagin74eec262011-11-28 12:03:31 +03001587 /*
1588 * used for cross-endian analysis. See git commit 65014ab3
1589 * for why this goofiness is needed.
1590 */
Jiri Olsa6a11f922012-05-16 08:59:04 +02001591 union u64_swap u;
Andrew Vagin74eec262011-11-28 12:03:31 +03001592
1593 array = event->sample.array;
1594
Adrian Hunter75562572013-08-27 11:23:09 +03001595 if (type & PERF_SAMPLE_IDENTIFIER) {
1596 *array = sample->id;
1597 array++;
1598 }
1599
Andrew Vagin74eec262011-11-28 12:03:31 +03001600 if (type & PERF_SAMPLE_IP) {
Adrian Hunteref893252013-08-27 11:23:06 +03001601 *array = sample->ip;
Andrew Vagin74eec262011-11-28 12:03:31 +03001602 array++;
1603 }
1604
1605 if (type & PERF_SAMPLE_TID) {
1606 u.val32[0] = sample->pid;
1607 u.val32[1] = sample->tid;
1608 if (swapped) {
1609 /*
Arnaldo Carvalho de Meloa3f698f2012-08-02 12:23:46 -03001610 * Inverse of what is done in perf_evsel__parse_sample
Andrew Vagin74eec262011-11-28 12:03:31 +03001611 */
1612 u.val32[0] = bswap_32(u.val32[0]);
1613 u.val32[1] = bswap_32(u.val32[1]);
1614 u.val64 = bswap_64(u.val64);
1615 }
1616
1617 *array = u.val64;
1618 array++;
1619 }
1620
1621 if (type & PERF_SAMPLE_TIME) {
1622 *array = sample->time;
1623 array++;
1624 }
1625
1626 if (type & PERF_SAMPLE_ADDR) {
1627 *array = sample->addr;
1628 array++;
1629 }
1630
1631 if (type & PERF_SAMPLE_ID) {
1632 *array = sample->id;
1633 array++;
1634 }
1635
1636 if (type & PERF_SAMPLE_STREAM_ID) {
1637 *array = sample->stream_id;
1638 array++;
1639 }
1640
1641 if (type & PERF_SAMPLE_CPU) {
1642 u.val32[0] = sample->cpu;
1643 if (swapped) {
1644 /*
Arnaldo Carvalho de Meloa3f698f2012-08-02 12:23:46 -03001645 * Inverse of what is done in perf_evsel__parse_sample
Andrew Vagin74eec262011-11-28 12:03:31 +03001646 */
1647 u.val32[0] = bswap_32(u.val32[0]);
1648 u.val64 = bswap_64(u.val64);
1649 }
1650 *array = u.val64;
1651 array++;
1652 }
1653
1654 if (type & PERF_SAMPLE_PERIOD) {
1655 *array = sample->period;
1656 array++;
1657 }
1658
Adrian Hunterd03f2172013-08-27 11:23:11 +03001659 if (type & PERF_SAMPLE_READ) {
1660 if (read_format & PERF_FORMAT_GROUP)
1661 *array = sample->read.group.nr;
1662 else
1663 *array = sample->read.one.value;
1664 array++;
1665
1666 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) {
1667 *array = sample->read.time_enabled;
1668 array++;
1669 }
1670
1671 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) {
1672 *array = sample->read.time_running;
1673 array++;
1674 }
1675
1676 /* PERF_FORMAT_ID is forced for PERF_SAMPLE_READ */
1677 if (read_format & PERF_FORMAT_GROUP) {
1678 sz = sample->read.group.nr *
1679 sizeof(struct sample_read_value);
1680 memcpy(array, sample->read.group.values, sz);
1681 array = (void *)array + sz;
1682 } else {
1683 *array = sample->read.one.id;
1684 array++;
1685 }
1686 }
1687
1688 if (type & PERF_SAMPLE_CALLCHAIN) {
1689 sz = (sample->callchain->nr + 1) * sizeof(u64);
1690 memcpy(array, sample->callchain, sz);
1691 array = (void *)array + sz;
1692 }
1693
1694 if (type & PERF_SAMPLE_RAW) {
1695 u.val32[0] = sample->raw_size;
1696 if (WARN_ONCE(swapped,
1697 "Endianness of raw data not corrected!\n")) {
1698 /*
1699 * Inverse of what is done in perf_evsel__parse_sample
1700 */
1701 u.val32[0] = bswap_32(u.val32[0]);
1702 u.val32[1] = bswap_32(u.val32[1]);
1703 u.val64 = bswap_64(u.val64);
1704 }
1705 *array = u.val64;
1706 array = (void *)array + sizeof(u32);
1707
1708 memcpy(array, sample->raw_data, sample->raw_size);
1709 array = (void *)array + sample->raw_size;
1710 }
1711
1712 if (type & PERF_SAMPLE_BRANCH_STACK) {
1713 sz = sample->branch_stack->nr * sizeof(struct branch_entry);
1714 sz += sizeof(u64);
1715 memcpy(array, sample->branch_stack, sz);
1716 array = (void *)array + sz;
1717 }
1718
1719 if (type & PERF_SAMPLE_REGS_USER) {
1720 if (sample->user_regs.abi) {
1721 *array++ = sample->user_regs.abi;
Jiri Olsa352ea452014-01-07 13:47:25 +01001722 sz = hweight_long(sample->user_regs.mask) * sizeof(u64);
Adrian Hunterd03f2172013-08-27 11:23:11 +03001723 memcpy(array, sample->user_regs.regs, sz);
1724 array = (void *)array + sz;
1725 } else {
1726 *array++ = 0;
1727 }
1728 }
1729
1730 if (type & PERF_SAMPLE_STACK_USER) {
1731 sz = sample->user_stack.size;
1732 *array++ = sz;
1733 if (sz) {
1734 memcpy(array, sample->user_stack.data, sz);
1735 array = (void *)array + sz;
1736 *array++ = sz;
1737 }
1738 }
1739
1740 if (type & PERF_SAMPLE_WEIGHT) {
1741 *array = sample->weight;
1742 array++;
1743 }
1744
1745 if (type & PERF_SAMPLE_DATA_SRC) {
1746 *array = sample->data_src;
1747 array++;
1748 }
1749
Adrian Hunter42d88912013-11-01 15:51:38 +02001750 if (type & PERF_SAMPLE_TRANSACTION) {
1751 *array = sample->transaction;
1752 array++;
1753 }
1754
Andrew Vagin74eec262011-11-28 12:03:31 +03001755 return 0;
1756}
Arnaldo Carvalho de Melo5555ded2012-09-11 19:24:23 -03001757
Arnaldo Carvalho de Meloefd2b922012-09-18 11:21:50 -03001758struct format_field *perf_evsel__field(struct perf_evsel *evsel, const char *name)
1759{
1760 return pevent_find_field(evsel->tp_format, name);
1761}
1762
Arnaldo Carvalho de Melo5d2074e2012-09-26 20:22:00 -03001763void *perf_evsel__rawptr(struct perf_evsel *evsel, struct perf_sample *sample,
Arnaldo Carvalho de Melo5555ded2012-09-11 19:24:23 -03001764 const char *name)
1765{
Arnaldo Carvalho de Meloefd2b922012-09-18 11:21:50 -03001766 struct format_field *field = perf_evsel__field(evsel, name);
Arnaldo Carvalho de Melo5555ded2012-09-11 19:24:23 -03001767 int offset;
1768
Arnaldo Carvalho de Meloefd2b922012-09-18 11:21:50 -03001769 if (!field)
1770 return NULL;
Arnaldo Carvalho de Melo5555ded2012-09-11 19:24:23 -03001771
1772 offset = field->offset;
1773
1774 if (field->flags & FIELD_IS_DYNAMIC) {
1775 offset = *(int *)(sample->raw_data + field->offset);
1776 offset &= 0xffff;
1777 }
1778
1779 return sample->raw_data + offset;
1780}
1781
1782u64 perf_evsel__intval(struct perf_evsel *evsel, struct perf_sample *sample,
1783 const char *name)
1784{
Arnaldo Carvalho de Meloefd2b922012-09-18 11:21:50 -03001785 struct format_field *field = perf_evsel__field(evsel, name);
Arnaldo Carvalho de Meloe6b6f672012-09-26 13:13:04 -03001786 void *ptr;
1787 u64 value;
Arnaldo Carvalho de Melo5555ded2012-09-11 19:24:23 -03001788
Arnaldo Carvalho de Meloefd2b922012-09-18 11:21:50 -03001789 if (!field)
1790 return 0;
Arnaldo Carvalho de Melo5555ded2012-09-11 19:24:23 -03001791
Arnaldo Carvalho de Meloe6b6f672012-09-26 13:13:04 -03001792 ptr = sample->raw_data + field->offset;
Arnaldo Carvalho de Melo5555ded2012-09-11 19:24:23 -03001793
Arnaldo Carvalho de Meloe6b6f672012-09-26 13:13:04 -03001794 switch (field->size) {
1795 case 1:
1796 return *(u8 *)ptr;
1797 case 2:
1798 value = *(u16 *)ptr;
1799 break;
1800 case 4:
1801 value = *(u32 *)ptr;
1802 break;
1803 case 8:
1804 value = *(u64 *)ptr;
1805 break;
1806 default:
1807 return 0;
1808 }
1809
1810 if (!evsel->needs_swap)
1811 return value;
1812
1813 switch (field->size) {
1814 case 2:
1815 return bswap_16(value);
1816 case 4:
1817 return bswap_32(value);
1818 case 8:
1819 return bswap_64(value);
1820 default:
1821 return 0;
1822 }
1823
1824 return 0;
Arnaldo Carvalho de Melo5555ded2012-09-11 19:24:23 -03001825}
Arnaldo Carvalho de Melo0698aed2012-12-10 18:17:08 -03001826
1827static int comma_fprintf(FILE *fp, bool *first, const char *fmt, ...)
1828{
1829 va_list args;
1830 int ret = 0;
1831
1832 if (!*first) {
1833 ret += fprintf(fp, ",");
1834 } else {
1835 ret += fprintf(fp, ":");
1836 *first = false;
1837 }
1838
1839 va_start(args, fmt);
1840 ret += vfprintf(fp, fmt, args);
1841 va_end(args);
1842 return ret;
1843}
1844
1845static int __if_fprintf(FILE *fp, bool *first, const char *field, u64 value)
1846{
1847 if (value == 0)
1848 return 0;
1849
1850 return comma_fprintf(fp, first, " %s: %" PRIu64, field, value);
1851}
1852
1853#define if_print(field) printed += __if_fprintf(fp, &first, #field, evsel->attr.field)
1854
Arnaldo Carvalho de Meloc79a4392012-12-11 10:54:12 -03001855struct bit_names {
1856 int bit;
1857 const char *name;
1858};
1859
1860static int bits__fprintf(FILE *fp, const char *field, u64 value,
1861 struct bit_names *bits, bool *first)
1862{
1863 int i = 0, printed = comma_fprintf(fp, first, " %s: ", field);
1864 bool first_bit = true;
1865
1866 do {
1867 if (value & bits[i].bit) {
1868 printed += fprintf(fp, "%s%s", first_bit ? "" : "|", bits[i].name);
1869 first_bit = false;
1870 }
1871 } while (bits[++i].name != NULL);
1872
1873 return printed;
1874}
1875
1876static int sample_type__fprintf(FILE *fp, bool *first, u64 value)
1877{
1878#define bit_name(n) { PERF_SAMPLE_##n, #n }
1879 struct bit_names bits[] = {
1880 bit_name(IP), bit_name(TID), bit_name(TIME), bit_name(ADDR),
1881 bit_name(READ), bit_name(CALLCHAIN), bit_name(ID), bit_name(CPU),
1882 bit_name(PERIOD), bit_name(STREAM_ID), bit_name(RAW),
1883 bit_name(BRANCH_STACK), bit_name(REGS_USER), bit_name(STACK_USER),
Adrian Hunter75562572013-08-27 11:23:09 +03001884 bit_name(IDENTIFIER),
Arnaldo Carvalho de Meloc79a4392012-12-11 10:54:12 -03001885 { .name = NULL, }
1886 };
1887#undef bit_name
1888 return bits__fprintf(fp, "sample_type", value, bits, first);
1889}
1890
1891static int read_format__fprintf(FILE *fp, bool *first, u64 value)
1892{
1893#define bit_name(n) { PERF_FORMAT_##n, #n }
1894 struct bit_names bits[] = {
1895 bit_name(TOTAL_TIME_ENABLED), bit_name(TOTAL_TIME_RUNNING),
1896 bit_name(ID), bit_name(GROUP),
1897 { .name = NULL, }
1898 };
1899#undef bit_name
1900 return bits__fprintf(fp, "read_format", value, bits, first);
1901}
1902
Arnaldo Carvalho de Melo0698aed2012-12-10 18:17:08 -03001903int perf_evsel__fprintf(struct perf_evsel *evsel,
1904 struct perf_attr_details *details, FILE *fp)
1905{
1906 bool first = true;
Namhyung Kime6ab07d2013-01-22 18:09:47 +09001907 int printed = 0;
1908
Arnaldo Carvalho de Meloe35ef3552013-02-06 17:20:02 -03001909 if (details->event_group) {
Namhyung Kime6ab07d2013-01-22 18:09:47 +09001910 struct perf_evsel *pos;
1911
1912 if (!perf_evsel__is_group_leader(evsel))
1913 return 0;
1914
1915 if (evsel->nr_members > 1)
1916 printed += fprintf(fp, "%s{", evsel->group_name ?: "");
1917
1918 printed += fprintf(fp, "%s", perf_evsel__name(evsel));
1919 for_each_group_member(pos, evsel)
1920 printed += fprintf(fp, ",%s", perf_evsel__name(pos));
1921
1922 if (evsel->nr_members > 1)
1923 printed += fprintf(fp, "}");
1924 goto out;
1925 }
1926
1927 printed += fprintf(fp, "%s", perf_evsel__name(evsel));
Arnaldo Carvalho de Melo0698aed2012-12-10 18:17:08 -03001928
1929 if (details->verbose || details->freq) {
1930 printed += comma_fprintf(fp, &first, " sample_freq=%" PRIu64,
1931 (u64)evsel->attr.sample_freq);
1932 }
1933
1934 if (details->verbose) {
1935 if_print(type);
1936 if_print(config);
1937 if_print(config1);
1938 if_print(config2);
1939 if_print(size);
Arnaldo Carvalho de Meloc79a4392012-12-11 10:54:12 -03001940 printed += sample_type__fprintf(fp, &first, evsel->attr.sample_type);
1941 if (evsel->attr.read_format)
1942 printed += read_format__fprintf(fp, &first, evsel->attr.read_format);
Arnaldo Carvalho de Melo0698aed2012-12-10 18:17:08 -03001943 if_print(disabled);
1944 if_print(inherit);
1945 if_print(pinned);
1946 if_print(exclusive);
1947 if_print(exclude_user);
1948 if_print(exclude_kernel);
1949 if_print(exclude_hv);
1950 if_print(exclude_idle);
1951 if_print(mmap);
Stephane Eranian5c5e8542013-08-21 12:10:25 +02001952 if_print(mmap2);
Arnaldo Carvalho de Melo0698aed2012-12-10 18:17:08 -03001953 if_print(comm);
Adrian Hunter022c50d2014-07-14 13:02:27 +03001954 if_print(comm_exec);
Arnaldo Carvalho de Melo0698aed2012-12-10 18:17:08 -03001955 if_print(freq);
1956 if_print(inherit_stat);
1957 if_print(enable_on_exec);
1958 if_print(task);
1959 if_print(watermark);
1960 if_print(precise_ip);
1961 if_print(mmap_data);
1962 if_print(sample_id_all);
1963 if_print(exclude_host);
1964 if_print(exclude_guest);
1965 if_print(__reserved_1);
1966 if_print(wakeup_events);
1967 if_print(bp_type);
1968 if_print(branch_sample_type);
1969 }
Namhyung Kime6ab07d2013-01-22 18:09:47 +09001970out:
Arnaldo Carvalho de Melo0698aed2012-12-10 18:17:08 -03001971 fputc('\n', fp);
1972 return ++printed;
1973}
Arnaldo Carvalho de Meloc0a54342012-12-13 14:16:30 -03001974
1975bool perf_evsel__fallback(struct perf_evsel *evsel, int err,
1976 char *msg, size_t msgsize)
1977{
David Ahern2b821cc2013-07-18 17:27:59 -06001978 if ((err == ENOENT || err == ENXIO || err == ENODEV) &&
Arnaldo Carvalho de Meloc0a54342012-12-13 14:16:30 -03001979 evsel->attr.type == PERF_TYPE_HARDWARE &&
1980 evsel->attr.config == PERF_COUNT_HW_CPU_CYCLES) {
1981 /*
1982 * If it's cycles then fall back to hrtimer based
1983 * cpu-clock-tick sw counter, which is always available even if
1984 * no PMU support.
1985 *
1986 * PPC returns ENXIO until 2.6.37 (behavior changed with commit
1987 * b0a873e).
1988 */
1989 scnprintf(msg, msgsize, "%s",
1990"The cycles event is not supported, trying to fall back to cpu-clock-ticks");
1991
1992 evsel->attr.type = PERF_TYPE_SOFTWARE;
1993 evsel->attr.config = PERF_COUNT_SW_CPU_CLOCK;
1994
Arnaldo Carvalho de Melo04662522013-12-26 17:41:15 -03001995 zfree(&evsel->name);
Arnaldo Carvalho de Meloc0a54342012-12-13 14:16:30 -03001996 return true;
1997 }
1998
1999 return false;
2000}
Arnaldo Carvalho de Melo56e52e82012-12-13 15:10:58 -03002001
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03002002int perf_evsel__open_strerror(struct perf_evsel *evsel, struct target *target,
Arnaldo Carvalho de Melo56e52e82012-12-13 15:10:58 -03002003 int err, char *msg, size_t size)
2004{
2005 switch (err) {
2006 case EPERM:
2007 case EACCES:
David Ahernb69e63a2013-05-25 17:54:00 -06002008 return scnprintf(msg, size,
Arnaldo Carvalho de Melo56e52e82012-12-13 15:10:58 -03002009 "You may not have permission to collect %sstats.\n"
2010 "Consider tweaking /proc/sys/kernel/perf_event_paranoid:\n"
2011 " -1 - Not paranoid at all\n"
2012 " 0 - Disallow raw tracepoint access for unpriv\n"
2013 " 1 - Disallow cpu events for unpriv\n"
2014 " 2 - Disallow kernel profiling for unpriv",
2015 target->system_wide ? "system-wide " : "");
2016 case ENOENT:
2017 return scnprintf(msg, size, "The %s event is not supported.",
2018 perf_evsel__name(evsel));
2019 case EMFILE:
2020 return scnprintf(msg, size, "%s",
2021 "Too many events are opened.\n"
2022 "Try again after reducing the number of events.");
2023 case ENODEV:
2024 if (target->cpu_list)
2025 return scnprintf(msg, size, "%s",
2026 "No such device - did you specify an out-of-range profile CPU?\n");
2027 break;
2028 case EOPNOTSUPP:
2029 if (evsel->attr.precise_ip)
2030 return scnprintf(msg, size, "%s",
2031 "\'precise\' request may not be supported. Try removing 'p' modifier.");
2032#if defined(__i386__) || defined(__x86_64__)
2033 if (evsel->attr.type == PERF_TYPE_HARDWARE)
2034 return scnprintf(msg, size, "%s",
2035 "No hardware sampling interrupt available.\n"
2036 "No APIC? If so then you can boot the kernel with the \"lapic\" boot parameter to force-enable it.");
2037#endif
2038 break;
2039 default:
2040 break;
2041 }
2042
2043 return scnprintf(msg, size,
2044 "The sys_perf_event_open() syscall returned with %d (%s) for event (%s). \n"
2045 "/bin/dmesg may provide additional information.\n"
2046 "No CONFIG_PERF_EVENTS=y kernel support configured?\n",
2047 err, strerror(err), perf_evsel__name(evsel));
2048}