blob: 0580b13df2e6f53777db4ffe1e1fe2b303ca1dfa [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 Meloce8ccff2014-10-09 15:29:51 -030035static int perf_evsel__no_extra_init(struct perf_evsel *evsel __maybe_unused)
36{
37 return 0;
38}
39
40static void perf_evsel__no_extra_fini(struct perf_evsel *evsel __maybe_unused)
41{
42}
43
44static struct {
45 size_t size;
46 int (*init)(struct perf_evsel *evsel);
47 void (*fini)(struct perf_evsel *evsel);
48} perf_evsel__object = {
49 .size = sizeof(struct perf_evsel),
50 .init = perf_evsel__no_extra_init,
51 .fini = perf_evsel__no_extra_fini,
52};
53
54int perf_evsel__object_config(size_t object_size,
55 int (*init)(struct perf_evsel *evsel),
56 void (*fini)(struct perf_evsel *evsel))
57{
58
59 if (object_size == 0)
60 goto set_methods;
61
62 if (perf_evsel__object.size > object_size)
63 return -EINVAL;
64
65 perf_evsel__object.size = object_size;
66
67set_methods:
68 if (init != NULL)
69 perf_evsel__object.init = init;
70
71 if (fini != NULL)
72 perf_evsel__object.fini = fini;
73
74 return 0;
75}
76
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -020077#define FD(e, x, y) (*(int *)xyarray__entry(e->fd, x, y))
78
Adrian Hunter75562572013-08-27 11:23:09 +030079int __perf_evsel__sample_size(u64 sample_type)
Arnaldo Carvalho de Meloc2a70652011-06-02 11:04:54 -030080{
81 u64 mask = sample_type & PERF_SAMPLE_MASK;
82 int size = 0;
83 int i;
84
85 for (i = 0; i < 64; i++) {
86 if (mask & (1ULL << i))
87 size++;
88 }
89
90 size *= sizeof(u64);
91
92 return size;
93}
94
Adrian Hunter75562572013-08-27 11:23:09 +030095/**
96 * __perf_evsel__calc_id_pos - calculate id_pos.
97 * @sample_type: sample type
98 *
99 * This function returns the position of the event id (PERF_SAMPLE_ID or
100 * PERF_SAMPLE_IDENTIFIER) in a sample event i.e. in the array of struct
101 * sample_event.
102 */
103static int __perf_evsel__calc_id_pos(u64 sample_type)
104{
105 int idx = 0;
106
107 if (sample_type & PERF_SAMPLE_IDENTIFIER)
108 return 0;
109
110 if (!(sample_type & PERF_SAMPLE_ID))
111 return -1;
112
113 if (sample_type & PERF_SAMPLE_IP)
114 idx += 1;
115
116 if (sample_type & PERF_SAMPLE_TID)
117 idx += 1;
118
119 if (sample_type & PERF_SAMPLE_TIME)
120 idx += 1;
121
122 if (sample_type & PERF_SAMPLE_ADDR)
123 idx += 1;
124
125 return idx;
126}
127
128/**
129 * __perf_evsel__calc_is_pos - calculate is_pos.
130 * @sample_type: sample type
131 *
132 * This function returns the position (counting backwards) of the event id
133 * (PERF_SAMPLE_ID or PERF_SAMPLE_IDENTIFIER) in a non-sample event i.e. if
134 * sample_id_all is used there is an id sample appended to non-sample events.
135 */
136static int __perf_evsel__calc_is_pos(u64 sample_type)
137{
138 int idx = 1;
139
140 if (sample_type & PERF_SAMPLE_IDENTIFIER)
141 return 1;
142
143 if (!(sample_type & PERF_SAMPLE_ID))
144 return -1;
145
146 if (sample_type & PERF_SAMPLE_CPU)
147 idx += 1;
148
149 if (sample_type & PERF_SAMPLE_STREAM_ID)
150 idx += 1;
151
152 return idx;
153}
154
155void perf_evsel__calc_id_pos(struct perf_evsel *evsel)
156{
157 evsel->id_pos = __perf_evsel__calc_id_pos(evsel->attr.sample_type);
158 evsel->is_pos = __perf_evsel__calc_is_pos(evsel->attr.sample_type);
159}
160
Jiri Olsa4bf9ce12012-03-22 14:37:26 +0100161void hists__init(struct hists *hists)
Arnaldo Carvalho de Melo0e2a5f12011-11-04 08:16:58 -0200162{
163 memset(hists, 0, sizeof(*hists));
164 hists->entries_in_array[0] = hists->entries_in_array[1] = RB_ROOT;
165 hists->entries_in = &hists->entries_in_array[0];
166 hists->entries_collapsed = RB_ROOT;
167 hists->entries = RB_ROOT;
168 pthread_mutex_init(&hists->lock, NULL);
169}
170
Arnaldo Carvalho de Melo7be5ebe2012-12-10 14:53:43 -0300171void __perf_evsel__set_sample_bit(struct perf_evsel *evsel,
172 enum perf_event_sample_format bit)
173{
174 if (!(evsel->attr.sample_type & bit)) {
175 evsel->attr.sample_type |= bit;
176 evsel->sample_size += sizeof(u64);
Adrian Hunter75562572013-08-27 11:23:09 +0300177 perf_evsel__calc_id_pos(evsel);
Arnaldo Carvalho de Melo7be5ebe2012-12-10 14:53:43 -0300178 }
179}
180
181void __perf_evsel__reset_sample_bit(struct perf_evsel *evsel,
182 enum perf_event_sample_format bit)
183{
184 if (evsel->attr.sample_type & bit) {
185 evsel->attr.sample_type &= ~bit;
186 evsel->sample_size -= sizeof(u64);
Adrian Hunter75562572013-08-27 11:23:09 +0300187 perf_evsel__calc_id_pos(evsel);
Arnaldo Carvalho de Melo7be5ebe2012-12-10 14:53:43 -0300188 }
189}
190
Adrian Hunter75562572013-08-27 11:23:09 +0300191void perf_evsel__set_sample_id(struct perf_evsel *evsel,
192 bool can_sample_identifier)
Arnaldo Carvalho de Melo7a5a5ca2012-12-10 15:21:30 -0300193{
Adrian Hunter75562572013-08-27 11:23:09 +0300194 if (can_sample_identifier) {
195 perf_evsel__reset_sample_bit(evsel, ID);
196 perf_evsel__set_sample_bit(evsel, IDENTIFIER);
197 } else {
198 perf_evsel__set_sample_bit(evsel, ID);
199 }
Arnaldo Carvalho de Melo7a5a5ca2012-12-10 15:21:30 -0300200 evsel->attr.read_format |= PERF_FORMAT_ID;
201}
202
Arnaldo Carvalho de Meloef1d1af2011-01-18 21:41:45 -0200203void perf_evsel__init(struct perf_evsel *evsel,
204 struct perf_event_attr *attr, int idx)
205{
206 evsel->idx = idx;
Adrian Hunter60b08962014-07-31 09:00:52 +0300207 evsel->tracking = !idx;
Arnaldo Carvalho de Meloef1d1af2011-01-18 21:41:45 -0200208 evsel->attr = *attr;
Namhyung Kim2cfda562012-11-29 15:38:29 +0900209 evsel->leader = evsel;
Stephane Eranian410136f2013-11-12 17:58:49 +0100210 evsel->unit = "";
211 evsel->scale = 1.0;
Arnaldo Carvalho de Meloef1d1af2011-01-18 21:41:45 -0200212 INIT_LIST_HEAD(&evsel->node);
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300213 hists__init(&evsel->hists);
Arnaldo Carvalho de Meloce8ccff2014-10-09 15:29:51 -0300214 perf_evsel__object.init(evsel);
Arnaldo Carvalho de Melobde09462012-08-01 18:53:11 -0300215 evsel->sample_size = __perf_evsel__sample_size(attr->sample_type);
Adrian Hunter75562572013-08-27 11:23:09 +0300216 perf_evsel__calc_id_pos(evsel);
Arnaldo Carvalho de Meloef1d1af2011-01-18 21:41:45 -0200217}
218
Arnaldo Carvalho de Meloef503832013-11-07 16:41:19 -0300219struct perf_evsel *perf_evsel__new_idx(struct perf_event_attr *attr, int idx)
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200220{
Arnaldo Carvalho de Meloce8ccff2014-10-09 15:29:51 -0300221 struct perf_evsel *evsel = zalloc(perf_evsel__object.size);
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200222
Arnaldo Carvalho de Meloef1d1af2011-01-18 21:41:45 -0200223 if (evsel != NULL)
224 perf_evsel__init(evsel, attr, idx);
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200225
226 return evsel;
227}
228
Arnaldo Carvalho de Meloef503832013-11-07 16:41:19 -0300229struct perf_evsel *perf_evsel__newtp_idx(const char *sys, const char *name, int idx)
Arnaldo Carvalho de Meloefd2b922012-09-18 11:21:50 -0300230{
Arnaldo Carvalho de Meloce8ccff2014-10-09 15:29:51 -0300231 struct perf_evsel *evsel = zalloc(perf_evsel__object.size);
Arnaldo Carvalho de Meloefd2b922012-09-18 11:21:50 -0300232
233 if (evsel != NULL) {
234 struct perf_event_attr attr = {
Arnaldo Carvalho de Melo0b80f8b32012-09-26 12:28:26 -0300235 .type = PERF_TYPE_TRACEPOINT,
236 .sample_type = (PERF_SAMPLE_RAW | PERF_SAMPLE_TIME |
237 PERF_SAMPLE_CPU | PERF_SAMPLE_PERIOD),
Arnaldo Carvalho de Meloefd2b922012-09-18 11:21:50 -0300238 };
239
Arnaldo Carvalho de Meloe48ffe22012-09-26 17:11:38 -0300240 if (asprintf(&evsel->name, "%s:%s", sys, name) < 0)
241 goto out_free;
242
Jiri Olsa97978b32013-12-03 14:09:24 +0100243 evsel->tp_format = trace_event__tp_format(sys, name);
Arnaldo Carvalho de Meloefd2b922012-09-18 11:21:50 -0300244 if (evsel->tp_format == NULL)
245 goto out_free;
246
Arnaldo Carvalho de Melo0b80f8b32012-09-26 12:28:26 -0300247 event_attr_init(&attr);
Arnaldo Carvalho de Meloefd2b922012-09-18 11:21:50 -0300248 attr.config = evsel->tp_format->id;
Arnaldo Carvalho de Melo0b80f8b32012-09-26 12:28:26 -0300249 attr.sample_period = 1;
Arnaldo Carvalho de Meloefd2b922012-09-18 11:21:50 -0300250 perf_evsel__init(evsel, &attr, idx);
Arnaldo Carvalho de Meloefd2b922012-09-18 11:21:50 -0300251 }
252
253 return evsel;
254
255out_free:
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -0300256 zfree(&evsel->name);
Arnaldo Carvalho de Meloefd2b922012-09-18 11:21:50 -0300257 free(evsel);
258 return NULL;
259}
260
Arnaldo Carvalho de Melo8ad70132012-09-06 13:11:18 -0300261const char *perf_evsel__hw_names[PERF_COUNT_HW_MAX] = {
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300262 "cycles",
263 "instructions",
264 "cache-references",
265 "cache-misses",
266 "branches",
267 "branch-misses",
268 "bus-cycles",
269 "stalled-cycles-frontend",
270 "stalled-cycles-backend",
271 "ref-cycles",
272};
273
Arnaldo Carvalho de Melodd4f5222012-06-13 15:52:42 -0300274static const char *__perf_evsel__hw_name(u64 config)
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300275{
276 if (config < PERF_COUNT_HW_MAX && perf_evsel__hw_names[config])
277 return perf_evsel__hw_names[config];
278
279 return "unknown-hardware";
280}
281
Arnaldo Carvalho de Melo27f18612012-06-11 13:33:09 -0300282static int perf_evsel__add_modifiers(struct perf_evsel *evsel, char *bf, size_t size)
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300283{
Arnaldo Carvalho de Melo27f18612012-06-11 13:33:09 -0300284 int colon = 0, r = 0;
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300285 struct perf_event_attr *attr = &evsel->attr;
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300286 bool exclude_guest_default = false;
287
288#define MOD_PRINT(context, mod) do { \
289 if (!attr->exclude_##context) { \
Arnaldo Carvalho de Melo27f18612012-06-11 13:33:09 -0300290 if (!colon) colon = ++r; \
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300291 r += scnprintf(bf + r, size - r, "%c", mod); \
292 } } while(0)
293
294 if (attr->exclude_kernel || attr->exclude_user || attr->exclude_hv) {
295 MOD_PRINT(kernel, 'k');
296 MOD_PRINT(user, 'u');
297 MOD_PRINT(hv, 'h');
298 exclude_guest_default = true;
299 }
300
301 if (attr->precise_ip) {
302 if (!colon)
Arnaldo Carvalho de Melo27f18612012-06-11 13:33:09 -0300303 colon = ++r;
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300304 r += scnprintf(bf + r, size - r, "%.*s", attr->precise_ip, "ppp");
305 exclude_guest_default = true;
306 }
307
308 if (attr->exclude_host || attr->exclude_guest == exclude_guest_default) {
309 MOD_PRINT(host, 'H');
310 MOD_PRINT(guest, 'G');
311 }
312#undef MOD_PRINT
313 if (colon)
Arnaldo Carvalho de Melo27f18612012-06-11 13:33:09 -0300314 bf[colon - 1] = ':';
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300315 return r;
316}
317
Arnaldo Carvalho de Melo27f18612012-06-11 13:33:09 -0300318static int perf_evsel__hw_name(struct perf_evsel *evsel, char *bf, size_t size)
319{
320 int r = scnprintf(bf, size, "%s", __perf_evsel__hw_name(evsel->attr.config));
321 return r + perf_evsel__add_modifiers(evsel, bf + r, size - r);
322}
323
Arnaldo Carvalho de Melo8ad70132012-09-06 13:11:18 -0300324const char *perf_evsel__sw_names[PERF_COUNT_SW_MAX] = {
Arnaldo Carvalho de Melo335c2f52012-06-11 14:36:20 -0300325 "cpu-clock",
326 "task-clock",
327 "page-faults",
328 "context-switches",
Arnaldo Carvalho de Melo8ad70132012-09-06 13:11:18 -0300329 "cpu-migrations",
Arnaldo Carvalho de Melo335c2f52012-06-11 14:36:20 -0300330 "minor-faults",
331 "major-faults",
332 "alignment-faults",
333 "emulation-faults",
Adrian Hunterd22d1a22013-08-31 21:50:52 +0300334 "dummy",
Arnaldo Carvalho de Melo335c2f52012-06-11 14:36:20 -0300335};
336
Arnaldo Carvalho de Melodd4f5222012-06-13 15:52:42 -0300337static const char *__perf_evsel__sw_name(u64 config)
Arnaldo Carvalho de Melo335c2f52012-06-11 14:36:20 -0300338{
339 if (config < PERF_COUNT_SW_MAX && perf_evsel__sw_names[config])
340 return perf_evsel__sw_names[config];
341 return "unknown-software";
342}
343
344static int perf_evsel__sw_name(struct perf_evsel *evsel, char *bf, size_t size)
345{
346 int r = scnprintf(bf, size, "%s", __perf_evsel__sw_name(evsel->attr.config));
347 return r + perf_evsel__add_modifiers(evsel, bf + r, size - r);
348}
349
Jiri Olsa287e74a2012-06-28 23:18:49 +0200350static int __perf_evsel__bp_name(char *bf, size_t size, u64 addr, u64 type)
351{
352 int r;
353
354 r = scnprintf(bf, size, "mem:0x%" PRIx64 ":", addr);
355
356 if (type & HW_BREAKPOINT_R)
357 r += scnprintf(bf + r, size - r, "r");
358
359 if (type & HW_BREAKPOINT_W)
360 r += scnprintf(bf + r, size - r, "w");
361
362 if (type & HW_BREAKPOINT_X)
363 r += scnprintf(bf + r, size - r, "x");
364
365 return r;
366}
367
368static int perf_evsel__bp_name(struct perf_evsel *evsel, char *bf, size_t size)
369{
370 struct perf_event_attr *attr = &evsel->attr;
371 int r = __perf_evsel__bp_name(bf, size, attr->bp_addr, attr->bp_type);
372 return r + perf_evsel__add_modifiers(evsel, bf + r, size - r);
373}
374
Arnaldo Carvalho de Melo0b668bc2012-06-11 14:08:07 -0300375const char *perf_evsel__hw_cache[PERF_COUNT_HW_CACHE_MAX]
376 [PERF_EVSEL__MAX_ALIASES] = {
377 { "L1-dcache", "l1-d", "l1d", "L1-data", },
378 { "L1-icache", "l1-i", "l1i", "L1-instruction", },
379 { "LLC", "L2", },
380 { "dTLB", "d-tlb", "Data-TLB", },
381 { "iTLB", "i-tlb", "Instruction-TLB", },
382 { "branch", "branches", "bpu", "btb", "bpc", },
383 { "node", },
384};
385
386const char *perf_evsel__hw_cache_op[PERF_COUNT_HW_CACHE_OP_MAX]
387 [PERF_EVSEL__MAX_ALIASES] = {
388 { "load", "loads", "read", },
389 { "store", "stores", "write", },
390 { "prefetch", "prefetches", "speculative-read", "speculative-load", },
391};
392
393const char *perf_evsel__hw_cache_result[PERF_COUNT_HW_CACHE_RESULT_MAX]
394 [PERF_EVSEL__MAX_ALIASES] = {
395 { "refs", "Reference", "ops", "access", },
396 { "misses", "miss", },
397};
398
399#define C(x) PERF_COUNT_HW_CACHE_##x
400#define CACHE_READ (1 << C(OP_READ))
401#define CACHE_WRITE (1 << C(OP_WRITE))
402#define CACHE_PREFETCH (1 << C(OP_PREFETCH))
403#define COP(x) (1 << x)
404
405/*
406 * cache operartion stat
407 * L1I : Read and prefetch only
408 * ITLB and BPU : Read-only
409 */
410static unsigned long perf_evsel__hw_cache_stat[C(MAX)] = {
411 [C(L1D)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
412 [C(L1I)] = (CACHE_READ | CACHE_PREFETCH),
413 [C(LL)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
414 [C(DTLB)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
415 [C(ITLB)] = (CACHE_READ),
416 [C(BPU)] = (CACHE_READ),
417 [C(NODE)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
418};
419
420bool perf_evsel__is_cache_op_valid(u8 type, u8 op)
421{
422 if (perf_evsel__hw_cache_stat[type] & COP(op))
423 return true; /* valid */
424 else
425 return false; /* invalid */
426}
427
428int __perf_evsel__hw_cache_type_op_res_name(u8 type, u8 op, u8 result,
429 char *bf, size_t size)
430{
431 if (result) {
432 return scnprintf(bf, size, "%s-%s-%s", perf_evsel__hw_cache[type][0],
433 perf_evsel__hw_cache_op[op][0],
434 perf_evsel__hw_cache_result[result][0]);
435 }
436
437 return scnprintf(bf, size, "%s-%s", perf_evsel__hw_cache[type][0],
438 perf_evsel__hw_cache_op[op][1]);
439}
440
Arnaldo Carvalho de Melodd4f5222012-06-13 15:52:42 -0300441static int __perf_evsel__hw_cache_name(u64 config, char *bf, size_t size)
Arnaldo Carvalho de Melo0b668bc2012-06-11 14:08:07 -0300442{
443 u8 op, result, type = (config >> 0) & 0xff;
444 const char *err = "unknown-ext-hardware-cache-type";
445
446 if (type > PERF_COUNT_HW_CACHE_MAX)
447 goto out_err;
448
449 op = (config >> 8) & 0xff;
450 err = "unknown-ext-hardware-cache-op";
451 if (op > PERF_COUNT_HW_CACHE_OP_MAX)
452 goto out_err;
453
454 result = (config >> 16) & 0xff;
455 err = "unknown-ext-hardware-cache-result";
456 if (result > PERF_COUNT_HW_CACHE_RESULT_MAX)
457 goto out_err;
458
459 err = "invalid-cache";
460 if (!perf_evsel__is_cache_op_valid(type, op))
461 goto out_err;
462
463 return __perf_evsel__hw_cache_type_op_res_name(type, op, result, bf, size);
464out_err:
465 return scnprintf(bf, size, "%s", err);
466}
467
468static int perf_evsel__hw_cache_name(struct perf_evsel *evsel, char *bf, size_t size)
469{
470 int ret = __perf_evsel__hw_cache_name(evsel->attr.config, bf, size);
471 return ret + perf_evsel__add_modifiers(evsel, bf + ret, size - ret);
472}
473
Arnaldo Carvalho de Melo6eef3d92012-06-13 11:53:37 -0300474static int perf_evsel__raw_name(struct perf_evsel *evsel, char *bf, size_t size)
475{
476 int ret = scnprintf(bf, size, "raw 0x%" PRIx64, evsel->attr.config);
477 return ret + perf_evsel__add_modifiers(evsel, bf + ret, size - ret);
478}
479
Arnaldo Carvalho de Melo7289f832012-06-12 12:34:58 -0300480const char *perf_evsel__name(struct perf_evsel *evsel)
Arnaldo Carvalho de Meloa4460832012-06-12 10:29:12 -0300481{
Arnaldo Carvalho de Melo7289f832012-06-12 12:34:58 -0300482 char bf[128];
Arnaldo Carvalho de Meloa4460832012-06-12 10:29:12 -0300483
Arnaldo Carvalho de Melo7289f832012-06-12 12:34:58 -0300484 if (evsel->name)
485 return evsel->name;
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300486
487 switch (evsel->attr.type) {
488 case PERF_TYPE_RAW:
Arnaldo Carvalho de Melo6eef3d92012-06-13 11:53:37 -0300489 perf_evsel__raw_name(evsel, bf, sizeof(bf));
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300490 break;
491
492 case PERF_TYPE_HARDWARE:
Arnaldo Carvalho de Melo7289f832012-06-12 12:34:58 -0300493 perf_evsel__hw_name(evsel, bf, sizeof(bf));
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300494 break;
Arnaldo Carvalho de Melo0b668bc2012-06-11 14:08:07 -0300495
496 case PERF_TYPE_HW_CACHE:
Arnaldo Carvalho de Melo7289f832012-06-12 12:34:58 -0300497 perf_evsel__hw_cache_name(evsel, bf, sizeof(bf));
Arnaldo Carvalho de Melo0b668bc2012-06-11 14:08:07 -0300498 break;
499
Arnaldo Carvalho de Melo335c2f52012-06-11 14:36:20 -0300500 case PERF_TYPE_SOFTWARE:
Arnaldo Carvalho de Melo7289f832012-06-12 12:34:58 -0300501 perf_evsel__sw_name(evsel, bf, sizeof(bf));
Arnaldo Carvalho de Melo335c2f52012-06-11 14:36:20 -0300502 break;
503
Arnaldo Carvalho de Meloa4460832012-06-12 10:29:12 -0300504 case PERF_TYPE_TRACEPOINT:
Arnaldo Carvalho de Melo7289f832012-06-12 12:34:58 -0300505 scnprintf(bf, sizeof(bf), "%s", "unknown tracepoint");
Arnaldo Carvalho de Meloa4460832012-06-12 10:29:12 -0300506 break;
507
Jiri Olsa287e74a2012-06-28 23:18:49 +0200508 case PERF_TYPE_BREAKPOINT:
509 perf_evsel__bp_name(evsel, bf, sizeof(bf));
510 break;
511
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300512 default:
Robert Richterca1b1452012-08-16 21:10:18 +0200513 scnprintf(bf, sizeof(bf), "unknown attr type: %d",
514 evsel->attr.type);
Arnaldo Carvalho de Meloa4460832012-06-12 10:29:12 -0300515 break;
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300516 }
517
Arnaldo Carvalho de Melo7289f832012-06-12 12:34:58 -0300518 evsel->name = strdup(bf);
519
520 return evsel->name ?: "unknown";
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300521}
522
Namhyung Kim717e2632013-01-22 18:09:44 +0900523const char *perf_evsel__group_name(struct perf_evsel *evsel)
524{
525 return evsel->group_name ?: "anon group";
526}
527
528int perf_evsel__group_desc(struct perf_evsel *evsel, char *buf, size_t size)
529{
530 int ret;
531 struct perf_evsel *pos;
532 const char *group_name = perf_evsel__group_name(evsel);
533
534 ret = scnprintf(buf, size, "%s", group_name);
535
536 ret += scnprintf(buf + ret, size - ret, " { %s",
537 perf_evsel__name(evsel));
538
539 for_each_group_member(pos, evsel)
540 ret += scnprintf(buf + ret, size - ret, ", %s",
541 perf_evsel__name(pos));
542
543 ret += scnprintf(buf + ret, size - ret, " }");
544
545 return ret;
546}
547
Jiri Olsa6bedfab2014-03-02 16:56:40 +0100548static void
Namhyung Kim72a128a2014-09-23 10:01:41 +0900549perf_evsel__config_callgraph(struct perf_evsel *evsel)
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
Namhyung Kim72a128a2014-09-23 10:01:41 +0900556 if (callchain_param.record_mode == CALLCHAIN_DWARF) {
Jiri Olsa6bedfab2014-03-02 16:56:40 +0100557 if (!function) {
558 perf_evsel__set_sample_bit(evsel, REGS_USER);
559 perf_evsel__set_sample_bit(evsel, STACK_USER);
560 attr->sample_regs_user = PERF_REGS_MASK;
Namhyung Kim72a128a2014-09-23 10:01:41 +0900561 attr->sample_stack_user = callchain_param.dump_size;
Jiri Olsa6bedfab2014-03-02 16:56:40 +0100562 attr->exclude_callchain_user = 1;
563 } else {
564 pr_info("Cannot use DWARF unwind for function trace event,"
565 " falling back to framepointers.\n");
566 }
567 }
568
569 if (function) {
570 pr_info("Disabling user space callchains for function trace event.\n");
571 attr->exclude_callchain_user = 1;
572 }
573}
574
Jiri Olsa774cb492012-11-12 18:34:01 +0100575/*
576 * The enable_on_exec/disabled value strategy:
577 *
578 * 1) For any type of traced program:
579 * - all independent events and group leaders are disabled
580 * - all group members are enabled
581 *
582 * Group members are ruled by group leaders. They need to
583 * be enabled, because the group scheduling relies on that.
584 *
585 * 2) For traced programs executed by perf:
586 * - all independent events and group leaders have
587 * enable_on_exec set
588 * - we don't specifically enable or disable any event during
589 * the record command
590 *
591 * Independent events and group leaders are initially disabled
592 * and get enabled by exec. Group members are ruled by group
593 * leaders as stated in 1).
594 *
595 * 3) For traced programs attached by perf (pid/tid):
596 * - we specifically enable or disable all events during
597 * the record command
598 *
599 * When attaching events to already running traced we
600 * enable/disable events specifically, as there's no
601 * initial traced exec call.
602 */
Arnaldo Carvalho de Melob4006792013-12-19 14:43:45 -0300603void perf_evsel__config(struct perf_evsel *evsel, struct record_opts *opts)
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200604{
Jiri Olsa3c176312012-10-10 17:39:03 +0200605 struct perf_evsel *leader = evsel->leader;
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200606 struct perf_event_attr *attr = &evsel->attr;
Adrian Hunter60b08962014-07-31 09:00:52 +0300607 int track = evsel->tracking;
Adrian Hunter3aa59392013-11-15 15:52:29 +0200608 bool per_cpu = opts->target.default_per_cpu && !opts->target.per_thread;
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200609
Arnaldo Carvalho de Melo594ac612012-12-13 13:13:07 -0300610 attr->sample_id_all = perf_missing_features.sample_id_all ? 0 : 1;
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200611 attr->inherit = !opts->no_inherit;
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200612
Arnaldo Carvalho de Melo7be5ebe2012-12-10 14:53:43 -0300613 perf_evsel__set_sample_bit(evsel, IP);
614 perf_evsel__set_sample_bit(evsel, TID);
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200615
Jiri Olsa3c176312012-10-10 17:39:03 +0200616 if (evsel->sample_read) {
617 perf_evsel__set_sample_bit(evsel, READ);
618
619 /*
620 * We need ID even in case of single event, because
621 * PERF_SAMPLE_READ process ID specific data.
622 */
Adrian Hunter75562572013-08-27 11:23:09 +0300623 perf_evsel__set_sample_id(evsel, false);
Jiri Olsa3c176312012-10-10 17:39:03 +0200624
625 /*
626 * Apply group format only if we belong to group
627 * with more than one members.
628 */
629 if (leader->nr_members > 1) {
630 attr->read_format |= PERF_FORMAT_GROUP;
631 attr->inherit = 0;
632 }
633 }
634
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200635 /*
Namhyung Kim17314e22014-06-09 14:43:37 +0900636 * We default some events to have a default interval. But keep
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200637 * it a weak assumption overridable by the user.
638 */
Namhyung Kim17314e22014-06-09 14:43:37 +0900639 if (!attr->sample_period || (opts->user_freq != UINT_MAX ||
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200640 opts->user_interval != ULLONG_MAX)) {
641 if (opts->freq) {
Arnaldo Carvalho de Melo7be5ebe2012-12-10 14:53:43 -0300642 perf_evsel__set_sample_bit(evsel, PERIOD);
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200643 attr->freq = 1;
644 attr->sample_freq = opts->freq;
645 } else {
646 attr->sample_period = opts->default_interval;
647 }
648 }
649
Jiri Olsa3c176312012-10-10 17:39:03 +0200650 /*
651 * Disable sampling for all group members other
652 * than leader in case leader 'leads' the sampling.
653 */
654 if ((leader != evsel) && leader->sample_read) {
655 attr->sample_freq = 0;
656 attr->sample_period = 0;
657 }
658
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200659 if (opts->no_samples)
660 attr->sample_freq = 0;
661
662 if (opts->inherit_stat)
663 attr->inherit_stat = 1;
664
665 if (opts->sample_address) {
Arnaldo Carvalho de Melo7be5ebe2012-12-10 14:53:43 -0300666 perf_evsel__set_sample_bit(evsel, ADDR);
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200667 attr->mmap_data = track;
668 }
669
Namhyung Kim72a128a2014-09-23 10:01:41 +0900670 if (callchain_param.enabled && !evsel->no_aux_samples)
671 perf_evsel__config_callgraph(evsel);
Jiri Olsa26d33022012-08-07 15:20:47 +0200672
Adrian Hunter3aa59392013-11-15 15:52:29 +0200673 if (target__has_cpu(&opts->target))
Arnaldo Carvalho de Melo7be5ebe2012-12-10 14:53:43 -0300674 perf_evsel__set_sample_bit(evsel, CPU);
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200675
Andrew Vagin3e76ac72011-12-20 17:32:45 +0300676 if (opts->period)
Arnaldo Carvalho de Melo7be5ebe2012-12-10 14:53:43 -0300677 perf_evsel__set_sample_bit(evsel, PERIOD);
Andrew Vagin3e76ac72011-12-20 17:32:45 +0300678
Andi Kleen8affc2b2014-07-31 14:45:04 +0800679 /*
680 * When the user explicitely disabled time don't force it here.
681 */
682 if (opts->sample_time &&
683 (!perf_missing_features.sample_id_all &&
684 (!opts->no_inherit || target__has_cpu(&opts->target) || per_cpu)))
Arnaldo Carvalho de Melo7be5ebe2012-12-10 14:53:43 -0300685 perf_evsel__set_sample_bit(evsel, TIME);
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200686
Adrian Hunter6ff1ce72014-07-14 13:02:56 +0300687 if (opts->raw_samples && !evsel->no_aux_samples) {
Arnaldo Carvalho de Melo7be5ebe2012-12-10 14:53:43 -0300688 perf_evsel__set_sample_bit(evsel, TIME);
689 perf_evsel__set_sample_bit(evsel, RAW);
690 perf_evsel__set_sample_bit(evsel, CPU);
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200691 }
692
Stephane Eranianccf49bf2013-01-24 16:10:37 +0100693 if (opts->sample_address)
Adrian Hunter1e7ed5e2013-11-01 15:51:35 +0200694 perf_evsel__set_sample_bit(evsel, DATA_SRC);
Stephane Eranianccf49bf2013-01-24 16:10:37 +0100695
Arnaldo Carvalho de Melo509051e2014-01-14 17:52:14 -0300696 if (opts->no_buffering) {
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200697 attr->watermark = 0;
698 attr->wakeup_events = 1;
699 }
Adrian Hunter6ff1ce72014-07-14 13:02:56 +0300700 if (opts->branch_stack && !evsel->no_aux_samples) {
Arnaldo Carvalho de Melo7be5ebe2012-12-10 14:53:43 -0300701 perf_evsel__set_sample_bit(evsel, BRANCH_STACK);
Roberto Agostino Vitillobdfebd82012-02-09 23:21:02 +0100702 attr->branch_sample_type = opts->branch_stack;
703 }
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200704
Andi Kleen05484292013-01-24 16:10:29 +0100705 if (opts->sample_weight)
Adrian Hunter1e7ed5e2013-11-01 15:51:35 +0200706 perf_evsel__set_sample_bit(evsel, WEIGHT);
Andi Kleen05484292013-01-24 16:10:29 +0100707
Stephane Eranian5c5e8542013-08-21 12:10:25 +0200708 attr->mmap = track;
Don Zickusa5a5ba72014-05-30 10:49:42 -0400709 attr->mmap2 = track && !perf_missing_features.mmap2;
Stephane Eranian5c5e8542013-08-21 12:10:25 +0200710 attr->comm = track;
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200711
Andi Kleen475eeab2013-09-20 07:40:43 -0700712 if (opts->sample_transaction)
Adrian Hunter1e7ed5e2013-11-01 15:51:35 +0200713 perf_evsel__set_sample_bit(evsel, TRANSACTION);
Andi Kleen475eeab2013-09-20 07:40:43 -0700714
Jiri Olsa774cb492012-11-12 18:34:01 +0100715 /*
716 * XXX see the function comment above
717 *
718 * Disabling only independent events or group leaders,
719 * keeping group members enabled.
720 */
Namhyung Kim823254e2012-11-29 15:38:30 +0900721 if (perf_evsel__is_group_leader(evsel))
Jiri Olsa774cb492012-11-12 18:34:01 +0100722 attr->disabled = 1;
723
724 /*
725 * Setting enable_on_exec for independent events and
726 * group leaders for traced executed by perf.
727 */
Andi Kleen6619a532014-01-11 13:38:27 -0800728 if (target__none(&opts->target) && perf_evsel__is_group_leader(evsel) &&
729 !opts->initial_delay)
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200730 attr->enable_on_exec = 1;
Adrian Hunter2afd2bc2014-07-14 13:02:57 +0300731
732 if (evsel->immediate) {
733 attr->disabled = 0;
734 attr->enable_on_exec = 0;
735 }
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200736}
737
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200738int perf_evsel__alloc_fd(struct perf_evsel *evsel, int ncpus, int nthreads)
739{
David Ahern4af4c952011-05-27 09:58:34 -0600740 int cpu, thread;
Adrian Hunterbf8e8f42014-07-31 09:00:51 +0300741
742 if (evsel->system_wide)
743 nthreads = 1;
744
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200745 evsel->fd = xyarray__new(ncpus, nthreads, sizeof(int));
David Ahern4af4c952011-05-27 09:58:34 -0600746
747 if (evsel->fd) {
748 for (cpu = 0; cpu < ncpus; cpu++) {
749 for (thread = 0; thread < nthreads; thread++) {
750 FD(evsel, cpu, thread) = -1;
751 }
752 }
753 }
754
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200755 return evsel->fd != NULL ? 0 : -ENOMEM;
756}
757
Andi Kleene2407be2013-08-02 17:41:10 -0700758static int perf_evsel__run_ioctl(struct perf_evsel *evsel, int ncpus, int nthreads,
759 int ioc, void *arg)
Arnaldo Carvalho de Melo745cefc2012-09-26 15:07:39 -0300760{
761 int cpu, thread;
762
Adrian Hunterbf8e8f42014-07-31 09:00:51 +0300763 if (evsel->system_wide)
764 nthreads = 1;
765
Arnaldo Carvalho de Melo745cefc2012-09-26 15:07:39 -0300766 for (cpu = 0; cpu < ncpus; cpu++) {
767 for (thread = 0; thread < nthreads; thread++) {
768 int fd = FD(evsel, cpu, thread),
Andi Kleene2407be2013-08-02 17:41:10 -0700769 err = ioctl(fd, ioc, arg);
Arnaldo Carvalho de Melo745cefc2012-09-26 15:07:39 -0300770
771 if (err)
772 return err;
773 }
774 }
775
776 return 0;
777}
778
Andi Kleene2407be2013-08-02 17:41:10 -0700779int perf_evsel__set_filter(struct perf_evsel *evsel, int ncpus, int nthreads,
780 const char *filter)
781{
782 return perf_evsel__run_ioctl(evsel, ncpus, nthreads,
783 PERF_EVENT_IOC_SET_FILTER,
784 (void *)filter);
785}
786
787int perf_evsel__enable(struct perf_evsel *evsel, int ncpus, int nthreads)
788{
789 return perf_evsel__run_ioctl(evsel, ncpus, nthreads,
790 PERF_EVENT_IOC_ENABLE,
791 0);
792}
793
Arnaldo Carvalho de Melo70db7532011-01-12 22:39:13 -0200794int perf_evsel__alloc_id(struct perf_evsel *evsel, int ncpus, int nthreads)
795{
Adrian Hunterbf8e8f42014-07-31 09:00:51 +0300796 if (evsel->system_wide)
797 nthreads = 1;
798
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -0300799 evsel->sample_id = xyarray__new(ncpus, nthreads, sizeof(struct perf_sample_id));
800 if (evsel->sample_id == NULL)
801 return -ENOMEM;
802
803 evsel->id = zalloc(ncpus * nthreads * sizeof(u64));
804 if (evsel->id == NULL) {
805 xyarray__delete(evsel->sample_id);
806 evsel->sample_id = NULL;
807 return -ENOMEM;
808 }
809
810 return 0;
Arnaldo Carvalho de Melo70db7532011-01-12 22:39:13 -0200811}
812
Frederik Deweerdta7e191c2013-03-01 13:02:27 -0500813void perf_evsel__reset_counts(struct perf_evsel *evsel, int ncpus)
814{
815 memset(evsel->counts, 0, (sizeof(*evsel->counts) +
816 (ncpus * sizeof(struct perf_counts_values))));
817}
818
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200819int perf_evsel__alloc_counts(struct perf_evsel *evsel, int ncpus)
820{
821 evsel->counts = zalloc((sizeof(*evsel->counts) +
822 (ncpus * sizeof(struct perf_counts_values))));
823 return evsel->counts != NULL ? 0 : -ENOMEM;
824}
825
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200826void perf_evsel__free_fd(struct perf_evsel *evsel)
827{
828 xyarray__delete(evsel->fd);
829 evsel->fd = NULL;
830}
831
Arnaldo Carvalho de Melo70db7532011-01-12 22:39:13 -0200832void perf_evsel__free_id(struct perf_evsel *evsel)
833{
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -0300834 xyarray__delete(evsel->sample_id);
835 evsel->sample_id = NULL;
Arnaldo Carvalho de Melo04662522013-12-26 17:41:15 -0300836 zfree(&evsel->id);
Arnaldo Carvalho de Melo70db7532011-01-12 22:39:13 -0200837}
838
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200839void perf_evsel__close_fd(struct perf_evsel *evsel, int ncpus, int nthreads)
840{
841 int cpu, thread;
842
Adrian Hunterbf8e8f42014-07-31 09:00:51 +0300843 if (evsel->system_wide)
844 nthreads = 1;
845
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200846 for (cpu = 0; cpu < ncpus; cpu++)
847 for (thread = 0; thread < nthreads; ++thread) {
848 close(FD(evsel, cpu, thread));
849 FD(evsel, cpu, thread) = -1;
850 }
851}
852
Namhyung Kim43f8e762013-01-25 10:44:44 +0900853void perf_evsel__free_counts(struct perf_evsel *evsel)
854{
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -0300855 zfree(&evsel->counts);
Namhyung Kim43f8e762013-01-25 10:44:44 +0900856}
857
Arnaldo Carvalho de Meloef1d1af2011-01-18 21:41:45 -0200858void perf_evsel__exit(struct perf_evsel *evsel)
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200859{
860 assert(list_empty(&evsel->node));
Namhyung Kim736b05a2013-03-15 14:48:49 +0900861 perf_evsel__free_fd(evsel);
862 perf_evsel__free_id(evsel);
Arnaldo Carvalho de Meloce8ccff2014-10-09 15:29:51 -0300863 perf_evsel__object.fini(evsel);
Arnaldo Carvalho de Meloef1d1af2011-01-18 21:41:45 -0200864}
865
866void perf_evsel__delete(struct perf_evsel *evsel)
867{
868 perf_evsel__exit(evsel);
Stephane Eranian023695d2011-02-14 11:20:01 +0200869 close_cgroup(evsel->cgrp);
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -0300870 zfree(&evsel->group_name);
Arnaldo Carvalho de Meloe48ffe22012-09-26 17:11:38 -0300871 if (evsel->tp_format)
Arnaldo Carvalho de Meloefd2b922012-09-18 11:21:50 -0300872 pevent_free_format(evsel->tp_format);
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -0300873 zfree(&evsel->name);
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200874 free(evsel);
875}
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200876
Stephane Eranianc7a79c42013-01-29 12:47:43 +0100877static inline void compute_deltas(struct perf_evsel *evsel,
878 int cpu,
879 struct perf_counts_values *count)
880{
881 struct perf_counts_values tmp;
882
883 if (!evsel->prev_raw_counts)
884 return;
885
886 if (cpu == -1) {
887 tmp = evsel->prev_raw_counts->aggr;
888 evsel->prev_raw_counts->aggr = *count;
889 } else {
890 tmp = evsel->prev_raw_counts->cpu[cpu];
891 evsel->prev_raw_counts->cpu[cpu] = *count;
892 }
893
894 count->val = count->val - tmp.val;
895 count->ena = count->ena - tmp.ena;
896 count->run = count->run - tmp.run;
897}
898
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200899int __perf_evsel__read_on_cpu(struct perf_evsel *evsel,
900 int cpu, int thread, bool scale)
901{
902 struct perf_counts_values count;
903 size_t nv = scale ? 3 : 1;
904
905 if (FD(evsel, cpu, thread) < 0)
906 return -EINVAL;
907
Arnaldo Carvalho de Melo4eed11d2011-01-04 00:13:17 -0200908 if (evsel->counts == NULL && perf_evsel__alloc_counts(evsel, cpu + 1) < 0)
909 return -ENOMEM;
910
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200911 if (readn(FD(evsel, cpu, thread), &count, nv * sizeof(u64)) < 0)
912 return -errno;
913
Stephane Eranianc7a79c42013-01-29 12:47:43 +0100914 compute_deltas(evsel, cpu, &count);
915
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200916 if (scale) {
917 if (count.run == 0)
918 count.val = 0;
919 else if (count.run < count.ena)
920 count.val = (u64)((double)count.val * count.ena / count.run + 0.5);
921 } else
922 count.ena = count.run = 0;
923
924 evsel->counts->cpu[cpu] = count;
925 return 0;
926}
927
928int __perf_evsel__read(struct perf_evsel *evsel,
929 int ncpus, int nthreads, bool scale)
930{
931 size_t nv = scale ? 3 : 1;
932 int cpu, thread;
933 struct perf_counts_values *aggr = &evsel->counts->aggr, count;
934
Adrian Hunterbf8e8f42014-07-31 09:00:51 +0300935 if (evsel->system_wide)
936 nthreads = 1;
937
Arnaldo Carvalho de Melo52bcd9942011-02-03 17:26:06 -0200938 aggr->val = aggr->ena = aggr->run = 0;
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200939
940 for (cpu = 0; cpu < ncpus; cpu++) {
941 for (thread = 0; thread < nthreads; thread++) {
942 if (FD(evsel, cpu, thread) < 0)
943 continue;
944
945 if (readn(FD(evsel, cpu, thread),
946 &count, nv * sizeof(u64)) < 0)
947 return -errno;
948
949 aggr->val += count.val;
950 if (scale) {
951 aggr->ena += count.ena;
952 aggr->run += count.run;
953 }
954 }
955 }
956
Stephane Eranianc7a79c42013-01-29 12:47:43 +0100957 compute_deltas(evsel, -1, aggr);
958
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200959 evsel->counts->scaled = 0;
960 if (scale) {
961 if (aggr->run == 0) {
962 evsel->counts->scaled = -1;
963 aggr->val = 0;
964 return 0;
965 }
966
967 if (aggr->run < aggr->ena) {
968 evsel->counts->scaled = 1;
969 aggr->val = (u64)((double)aggr->val * aggr->ena / aggr->run + 0.5);
970 }
971 } else
972 aggr->ena = aggr->run = 0;
973
974 return 0;
975}
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -0200976
Jiri Olsa6a4bb042012-08-08 12:22:36 +0200977static int get_group_fd(struct perf_evsel *evsel, int cpu, int thread)
978{
979 struct perf_evsel *leader = evsel->leader;
980 int fd;
981
Namhyung Kim823254e2012-11-29 15:38:30 +0900982 if (perf_evsel__is_group_leader(evsel))
Jiri Olsa6a4bb042012-08-08 12:22:36 +0200983 return -1;
984
985 /*
986 * Leader must be already processed/open,
987 * if not it's a bug.
988 */
989 BUG_ON(!leader->fd);
990
991 fd = FD(leader, cpu, thread);
992 BUG_ON(fd == -1);
993
994 return fd;
995}
996
Adrian Huntere3e1a542013-08-14 15:48:24 +0300997#define __PRINT_ATTR(fmt, cast, field) \
998 fprintf(fp, " %-19s "fmt"\n", #field, cast attr->field)
999
1000#define PRINT_ATTR_U32(field) __PRINT_ATTR("%u" , , field)
1001#define PRINT_ATTR_X32(field) __PRINT_ATTR("%#x", , field)
1002#define PRINT_ATTR_U64(field) __PRINT_ATTR("%" PRIu64, (uint64_t), field)
1003#define PRINT_ATTR_X64(field) __PRINT_ATTR("%#"PRIx64, (uint64_t), field)
1004
1005#define PRINT_ATTR2N(name1, field1, name2, field2) \
1006 fprintf(fp, " %-19s %u %-19s %u\n", \
1007 name1, attr->field1, name2, attr->field2)
1008
1009#define PRINT_ATTR2(field1, field2) \
1010 PRINT_ATTR2N(#field1, field1, #field2, field2)
1011
1012static size_t perf_event_attr__fprintf(struct perf_event_attr *attr, FILE *fp)
1013{
1014 size_t ret = 0;
1015
1016 ret += fprintf(fp, "%.60s\n", graph_dotted_line);
1017 ret += fprintf(fp, "perf_event_attr:\n");
1018
1019 ret += PRINT_ATTR_U32(type);
1020 ret += PRINT_ATTR_U32(size);
1021 ret += PRINT_ATTR_X64(config);
1022 ret += PRINT_ATTR_U64(sample_period);
1023 ret += PRINT_ATTR_U64(sample_freq);
1024 ret += PRINT_ATTR_X64(sample_type);
1025 ret += PRINT_ATTR_X64(read_format);
1026
1027 ret += PRINT_ATTR2(disabled, inherit);
1028 ret += PRINT_ATTR2(pinned, exclusive);
1029 ret += PRINT_ATTR2(exclude_user, exclude_kernel);
1030 ret += PRINT_ATTR2(exclude_hv, exclude_idle);
1031 ret += PRINT_ATTR2(mmap, comm);
Adrian Hunter022c50d2014-07-14 13:02:27 +03001032 ret += PRINT_ATTR2(mmap2, comm_exec);
Adrian Huntere3e1a542013-08-14 15:48:24 +03001033 ret += PRINT_ATTR2(freq, inherit_stat);
1034 ret += PRINT_ATTR2(enable_on_exec, task);
1035 ret += PRINT_ATTR2(watermark, precise_ip);
1036 ret += PRINT_ATTR2(mmap_data, sample_id_all);
1037 ret += PRINT_ATTR2(exclude_host, exclude_guest);
1038 ret += PRINT_ATTR2N("excl.callchain_kern", exclude_callchain_kernel,
1039 "excl.callchain_user", exclude_callchain_user);
1040
1041 ret += PRINT_ATTR_U32(wakeup_events);
1042 ret += PRINT_ATTR_U32(wakeup_watermark);
1043 ret += PRINT_ATTR_X32(bp_type);
1044 ret += PRINT_ATTR_X64(bp_addr);
1045 ret += PRINT_ATTR_X64(config1);
1046 ret += PRINT_ATTR_U64(bp_len);
1047 ret += PRINT_ATTR_X64(config2);
1048 ret += PRINT_ATTR_X64(branch_sample_type);
1049 ret += PRINT_ATTR_X64(sample_regs_user);
1050 ret += PRINT_ATTR_U32(sample_stack_user);
1051
1052 ret += fprintf(fp, "%.60s\n", graph_dotted_line);
1053
1054 return ret;
1055}
1056
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -02001057static int __perf_evsel__open(struct perf_evsel *evsel, struct cpu_map *cpus,
Jiri Olsa6a4bb042012-08-08 12:22:36 +02001058 struct thread_map *threads)
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -02001059{
Adrian Hunterbf8e8f42014-07-31 09:00:51 +03001060 int cpu, thread, nthreads;
Yann Droneaud57480d22014-06-30 22:28:47 +02001061 unsigned long flags = PERF_FLAG_FD_CLOEXEC;
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -02001062 int pid = -1, err;
Andi Kleenbec19672013-08-04 19:41:26 -07001063 enum { NO_CHANGE, SET_TO_MAX, INCREASED_MAX } set_rlimit = NO_CHANGE;
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -02001064
Adrian Hunterbf8e8f42014-07-31 09:00:51 +03001065 if (evsel->system_wide)
1066 nthreads = 1;
1067 else
1068 nthreads = threads->nr;
1069
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -02001070 if (evsel->fd == NULL &&
Adrian Hunterbf8e8f42014-07-31 09:00:51 +03001071 perf_evsel__alloc_fd(evsel, cpus->nr, nthreads) < 0)
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -02001072 return -ENOMEM;
Arnaldo Carvalho de Melo4eed11d2011-01-04 00:13:17 -02001073
Stephane Eranian023695d2011-02-14 11:20:01 +02001074 if (evsel->cgrp) {
Yann Droneaud57480d22014-06-30 22:28:47 +02001075 flags |= PERF_FLAG_PID_CGROUP;
Stephane Eranian023695d2011-02-14 11:20:01 +02001076 pid = evsel->cgrp->fd;
1077 }
1078
Arnaldo Carvalho de Melo594ac612012-12-13 13:13:07 -03001079fallback_missing_features:
Yann Droneaud57480d22014-06-30 22:28:47 +02001080 if (perf_missing_features.cloexec)
1081 flags &= ~(unsigned long)PERF_FLAG_FD_CLOEXEC;
Stephane Eranian5c5e8542013-08-21 12:10:25 +02001082 if (perf_missing_features.mmap2)
1083 evsel->attr.mmap2 = 0;
Arnaldo Carvalho de Melo594ac612012-12-13 13:13:07 -03001084 if (perf_missing_features.exclude_guest)
1085 evsel->attr.exclude_guest = evsel->attr.exclude_host = 0;
1086retry_sample_id:
1087 if (perf_missing_features.sample_id_all)
1088 evsel->attr.sample_id_all = 0;
1089
Adrian Huntere3e1a542013-08-14 15:48:24 +03001090 if (verbose >= 2)
1091 perf_event_attr__fprintf(&evsel->attr, stderr);
1092
Arnaldo Carvalho de Melo86bd5e82011-01-03 23:09:46 -02001093 for (cpu = 0; cpu < cpus->nr; cpu++) {
Arnaldo Carvalho de Melo9d04f172011-01-12 00:08:18 -02001094
Adrian Hunterbf8e8f42014-07-31 09:00:51 +03001095 for (thread = 0; thread < nthreads; thread++) {
Jiri Olsa6a4bb042012-08-08 12:22:36 +02001096 int group_fd;
Stephane Eranian023695d2011-02-14 11:20:01 +02001097
Adrian Hunterbf8e8f42014-07-31 09:00:51 +03001098 if (!evsel->cgrp && !evsel->system_wide)
Stephane Eranian023695d2011-02-14 11:20:01 +02001099 pid = threads->map[thread];
1100
Jiri Olsa6a4bb042012-08-08 12:22:36 +02001101 group_fd = get_group_fd(evsel, cpu, thread);
Andi Kleenbec19672013-08-04 19:41:26 -07001102retry_open:
Ramkumar Ramachandraa33f6ef2014-03-18 15:10:42 -04001103 pr_debug2("sys_perf_event_open: pid %d cpu %d group_fd %d flags %#lx\n",
Adrian Huntere3e1a542013-08-14 15:48:24 +03001104 pid, cpus->map[cpu], group_fd, flags);
1105
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -02001106 FD(evsel, cpu, thread) = sys_perf_event_open(&evsel->attr,
Stephane Eranian023695d2011-02-14 11:20:01 +02001107 pid,
Arnaldo Carvalho de Melof08199d2011-01-11 23:42:19 -02001108 cpus->map[cpu],
Stephane Eranian023695d2011-02-14 11:20:01 +02001109 group_fd, flags);
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -02001110 if (FD(evsel, cpu, thread) < 0) {
1111 err = -errno;
Ramkumar Ramachandraa33f6ef2014-03-18 15:10:42 -04001112 pr_debug2("sys_perf_event_open failed, error %d\n",
Adrian Hunterf852fd62013-11-01 15:51:29 +02001113 err);
Arnaldo Carvalho de Melo594ac612012-12-13 13:13:07 -03001114 goto try_fallback;
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -02001115 }
Andi Kleenbec19672013-08-04 19:41:26 -07001116 set_rlimit = NO_CHANGE;
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -02001117 }
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -02001118 }
1119
1120 return 0;
1121
Arnaldo Carvalho de Melo594ac612012-12-13 13:13:07 -03001122try_fallback:
Andi Kleenbec19672013-08-04 19:41:26 -07001123 /*
1124 * perf stat needs between 5 and 22 fds per CPU. When we run out
1125 * of them try to increase the limits.
1126 */
1127 if (err == -EMFILE && set_rlimit < INCREASED_MAX) {
1128 struct rlimit l;
1129 int old_errno = errno;
1130
1131 if (getrlimit(RLIMIT_NOFILE, &l) == 0) {
1132 if (set_rlimit == NO_CHANGE)
1133 l.rlim_cur = l.rlim_max;
1134 else {
1135 l.rlim_cur = l.rlim_max + 1000;
1136 l.rlim_max = l.rlim_cur;
1137 }
1138 if (setrlimit(RLIMIT_NOFILE, &l) == 0) {
1139 set_rlimit++;
1140 errno = old_errno;
1141 goto retry_open;
1142 }
1143 }
1144 errno = old_errno;
1145 }
1146
Arnaldo Carvalho de Melo594ac612012-12-13 13:13:07 -03001147 if (err != -EINVAL || cpu > 0 || thread > 0)
1148 goto out_close;
1149
Yann Droneaud57480d22014-06-30 22:28:47 +02001150 if (!perf_missing_features.cloexec && (flags & PERF_FLAG_FD_CLOEXEC)) {
1151 perf_missing_features.cloexec = true;
1152 goto fallback_missing_features;
1153 } else if (!perf_missing_features.mmap2 && evsel->attr.mmap2) {
Stephane Eranian5c5e8542013-08-21 12:10:25 +02001154 perf_missing_features.mmap2 = true;
1155 goto fallback_missing_features;
1156 } else if (!perf_missing_features.exclude_guest &&
1157 (evsel->attr.exclude_guest || evsel->attr.exclude_host)) {
Arnaldo Carvalho de Melo594ac612012-12-13 13:13:07 -03001158 perf_missing_features.exclude_guest = true;
1159 goto fallback_missing_features;
1160 } else if (!perf_missing_features.sample_id_all) {
1161 perf_missing_features.sample_id_all = true;
1162 goto retry_sample_id;
1163 }
1164
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -02001165out_close:
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -02001166 do {
1167 while (--thread >= 0) {
1168 close(FD(evsel, cpu, thread));
1169 FD(evsel, cpu, thread) = -1;
1170 }
Adrian Hunterbf8e8f42014-07-31 09:00:51 +03001171 thread = nthreads;
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -02001172 } while (--cpu >= 0);
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -02001173 return err;
1174}
1175
1176void perf_evsel__close(struct perf_evsel *evsel, int ncpus, int nthreads)
1177{
1178 if (evsel->fd == NULL)
1179 return;
1180
1181 perf_evsel__close_fd(evsel, ncpus, nthreads);
1182 perf_evsel__free_fd(evsel);
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -02001183}
1184
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -02001185static struct {
1186 struct cpu_map map;
1187 int cpus[1];
1188} empty_cpu_map = {
1189 .map.nr = 1,
1190 .cpus = { -1, },
1191};
1192
1193static struct {
1194 struct thread_map map;
1195 int threads[1];
1196} empty_thread_map = {
1197 .map.nr = 1,
1198 .threads = { -1, },
1199};
1200
Arnaldo Carvalho de Melof08199d2011-01-11 23:42:19 -02001201int 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 Melo02522082011-01-04 11:55:27 -02001203{
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -02001204 if (cpus == NULL) {
1205 /* Work around old compiler warnings about strict aliasing */
1206 cpus = &empty_cpu_map.map;
1207 }
1208
1209 if (threads == NULL)
1210 threads = &empty_thread_map.map;
1211
Jiri Olsa6a4bb042012-08-08 12:22:36 +02001212 return __perf_evsel__open(evsel, cpus, threads);
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -02001213}
1214
Arnaldo Carvalho de Melof08199d2011-01-11 23:42:19 -02001215int perf_evsel__open_per_cpu(struct perf_evsel *evsel,
Jiri Olsa6a4bb042012-08-08 12:22:36 +02001216 struct cpu_map *cpus)
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -02001217{
Jiri Olsa6a4bb042012-08-08 12:22:36 +02001218 return __perf_evsel__open(evsel, cpus, &empty_thread_map.map);
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -02001219}
1220
Arnaldo Carvalho de Melof08199d2011-01-11 23:42:19 -02001221int perf_evsel__open_per_thread(struct perf_evsel *evsel,
Jiri Olsa6a4bb042012-08-08 12:22:36 +02001222 struct thread_map *threads)
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -02001223{
Jiri Olsa6a4bb042012-08-08 12:22:36 +02001224 return __perf_evsel__open(evsel, &empty_cpu_map.map, threads);
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -02001225}
Arnaldo Carvalho de Melo70082dd2011-01-12 17:03:24 -02001226
Arnaldo Carvalho de Melo0807d2d2012-09-26 12:48:18 -03001227static int perf_evsel__parse_id_sample(const struct perf_evsel *evsel,
1228 const union perf_event *event,
1229 struct perf_sample *sample)
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001230{
Arnaldo Carvalho de Melo0807d2d2012-09-26 12:48:18 -03001231 u64 type = evsel->attr.sample_type;
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001232 const u64 *array = event->sample.array;
Arnaldo Carvalho de Melo0807d2d2012-09-26 12:48:18 -03001233 bool swapped = evsel->needs_swap;
Jiri Olsa37073f92012-05-30 14:23:44 +02001234 union u64_swap u;
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001235
1236 array += ((event->header.size -
1237 sizeof(event->header)) / sizeof(u64)) - 1;
1238
Adrian Hunter75562572013-08-27 11:23:09 +03001239 if (type & PERF_SAMPLE_IDENTIFIER) {
1240 sample->id = *array;
1241 array--;
1242 }
1243
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001244 if (type & PERF_SAMPLE_CPU) {
Jiri Olsa37073f92012-05-30 14:23:44 +02001245 u.val64 = *array;
1246 if (swapped) {
1247 /* undo swap of u64, then swap on individual u32s */
1248 u.val64 = bswap_64(u.val64);
1249 u.val32[0] = bswap_32(u.val32[0]);
1250 }
1251
1252 sample->cpu = u.val32[0];
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001253 array--;
1254 }
1255
1256 if (type & PERF_SAMPLE_STREAM_ID) {
1257 sample->stream_id = *array;
1258 array--;
1259 }
1260
1261 if (type & PERF_SAMPLE_ID) {
1262 sample->id = *array;
1263 array--;
1264 }
1265
1266 if (type & PERF_SAMPLE_TIME) {
1267 sample->time = *array;
1268 array--;
1269 }
1270
1271 if (type & PERF_SAMPLE_TID) {
Jiri Olsa37073f92012-05-30 14:23:44 +02001272 u.val64 = *array;
1273 if (swapped) {
1274 /* undo swap of u64, then swap on individual u32s */
1275 u.val64 = bswap_64(u.val64);
1276 u.val32[0] = bswap_32(u.val32[0]);
1277 u.val32[1] = bswap_32(u.val32[1]);
1278 }
1279
1280 sample->pid = u.val32[0];
1281 sample->tid = u.val32[1];
Adrian Hunterdd44bc62013-10-18 15:29:01 +03001282 array--;
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001283 }
1284
1285 return 0;
1286}
1287
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001288static inline bool overflow(const void *endp, u16 max_size, const void *offset,
1289 u64 size)
Frederic Weisbecker98e1da92011-05-21 20:08:15 +02001290{
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001291 return size > max_size || offset + size > endp;
Frederic Weisbecker98e1da92011-05-21 20:08:15 +02001292}
1293
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001294#define OVERFLOW_CHECK(offset, size, max_size) \
1295 do { \
1296 if (overflow(endp, (max_size), (offset), (size))) \
1297 return -EFAULT; \
1298 } while (0)
1299
1300#define OVERFLOW_CHECK_u64(offset) \
1301 OVERFLOW_CHECK(offset, sizeof(u64), sizeof(u64))
1302
Arnaldo Carvalho de Meloa3f698f2012-08-02 12:23:46 -03001303int perf_evsel__parse_sample(struct perf_evsel *evsel, union perf_event *event,
Arnaldo Carvalho de Melo0807d2d2012-09-26 12:48:18 -03001304 struct perf_sample *data)
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001305{
Arnaldo Carvalho de Meloa3f698f2012-08-02 12:23:46 -03001306 u64 type = evsel->attr.sample_type;
Arnaldo Carvalho de Melo0807d2d2012-09-26 12:48:18 -03001307 bool swapped = evsel->needs_swap;
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001308 const u64 *array;
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001309 u16 max_size = event->header.size;
1310 const void *endp = (void *)event + max_size;
1311 u64 sz;
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001312
David Ahern936be502011-09-06 09:12:26 -06001313 /*
1314 * used for cross-endian analysis. See git commit 65014ab3
1315 * for why this goofiness is needed.
1316 */
Jiri Olsa6a11f922012-05-16 08:59:04 +02001317 union u64_swap u;
David Ahern936be502011-09-06 09:12:26 -06001318
Robert Richterf3bda2c2011-12-15 17:32:39 +01001319 memset(data, 0, sizeof(*data));
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001320 data->cpu = data->pid = data->tid = -1;
1321 data->stream_id = data->id = data->time = -1ULL;
Jiri Olsabc529082014-02-03 12:44:41 +01001322 data->period = evsel->attr.sample_period;
Andi Kleen05484292013-01-24 16:10:29 +01001323 data->weight = 0;
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001324
1325 if (event->header.type != PERF_RECORD_SAMPLE) {
Arnaldo Carvalho de Meloa3f698f2012-08-02 12:23:46 -03001326 if (!evsel->attr.sample_id_all)
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001327 return 0;
Arnaldo Carvalho de Melo0807d2d2012-09-26 12:48:18 -03001328 return perf_evsel__parse_id_sample(evsel, event, data);
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001329 }
1330
1331 array = event->sample.array;
1332
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001333 /*
1334 * The evsel's sample_size is based on PERF_SAMPLE_MASK which includes
1335 * up to PERF_SAMPLE_PERIOD. After that overflow() must be used to
1336 * check the format does not go past the end of the event.
1337 */
Arnaldo Carvalho de Meloa3f698f2012-08-02 12:23:46 -03001338 if (evsel->sample_size + sizeof(event->header) > event->header.size)
Frederic Weisbeckera2854122011-05-21 19:33:04 +02001339 return -EFAULT;
1340
Adrian Hunter75562572013-08-27 11:23:09 +03001341 data->id = -1ULL;
1342 if (type & PERF_SAMPLE_IDENTIFIER) {
1343 data->id = *array;
1344 array++;
1345 }
1346
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001347 if (type & PERF_SAMPLE_IP) {
Adrian Hunteref893252013-08-27 11:23:06 +03001348 data->ip = *array;
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001349 array++;
1350 }
1351
1352 if (type & PERF_SAMPLE_TID) {
David Ahern936be502011-09-06 09:12:26 -06001353 u.val64 = *array;
1354 if (swapped) {
1355 /* undo swap of u64, then swap on individual u32s */
1356 u.val64 = bswap_64(u.val64);
1357 u.val32[0] = bswap_32(u.val32[0]);
1358 u.val32[1] = bswap_32(u.val32[1]);
1359 }
1360
1361 data->pid = u.val32[0];
1362 data->tid = u.val32[1];
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001363 array++;
1364 }
1365
1366 if (type & PERF_SAMPLE_TIME) {
1367 data->time = *array;
1368 array++;
1369 }
1370
David Ahern7cec0922011-05-30 13:08:23 -06001371 data->addr = 0;
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001372 if (type & PERF_SAMPLE_ADDR) {
1373 data->addr = *array;
1374 array++;
1375 }
1376
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001377 if (type & PERF_SAMPLE_ID) {
1378 data->id = *array;
1379 array++;
1380 }
1381
1382 if (type & PERF_SAMPLE_STREAM_ID) {
1383 data->stream_id = *array;
1384 array++;
1385 }
1386
1387 if (type & PERF_SAMPLE_CPU) {
David Ahern936be502011-09-06 09:12:26 -06001388
1389 u.val64 = *array;
1390 if (swapped) {
1391 /* undo swap of u64, then swap on individual u32s */
1392 u.val64 = bswap_64(u.val64);
1393 u.val32[0] = bswap_32(u.val32[0]);
1394 }
1395
1396 data->cpu = u.val32[0];
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001397 array++;
1398 }
1399
1400 if (type & PERF_SAMPLE_PERIOD) {
1401 data->period = *array;
1402 array++;
1403 }
1404
1405 if (type & PERF_SAMPLE_READ) {
Jiri Olsa9ede4732012-10-10 17:38:13 +02001406 u64 read_format = evsel->attr.read_format;
1407
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001408 OVERFLOW_CHECK_u64(array);
Jiri Olsa9ede4732012-10-10 17:38:13 +02001409 if (read_format & PERF_FORMAT_GROUP)
1410 data->read.group.nr = *array;
1411 else
1412 data->read.one.value = *array;
1413
1414 array++;
1415
1416 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) {
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001417 OVERFLOW_CHECK_u64(array);
Jiri Olsa9ede4732012-10-10 17:38:13 +02001418 data->read.time_enabled = *array;
1419 array++;
1420 }
1421
1422 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) {
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001423 OVERFLOW_CHECK_u64(array);
Jiri Olsa9ede4732012-10-10 17:38:13 +02001424 data->read.time_running = *array;
1425 array++;
1426 }
1427
1428 /* PERF_FORMAT_ID is forced for PERF_SAMPLE_READ */
1429 if (read_format & PERF_FORMAT_GROUP) {
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001430 const u64 max_group_nr = UINT64_MAX /
1431 sizeof(struct sample_read_value);
1432
1433 if (data->read.group.nr > max_group_nr)
1434 return -EFAULT;
1435 sz = data->read.group.nr *
1436 sizeof(struct sample_read_value);
1437 OVERFLOW_CHECK(array, sz, max_size);
1438 data->read.group.values =
1439 (struct sample_read_value *)array;
1440 array = (void *)array + sz;
Jiri Olsa9ede4732012-10-10 17:38:13 +02001441 } else {
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001442 OVERFLOW_CHECK_u64(array);
Jiri Olsa9ede4732012-10-10 17:38:13 +02001443 data->read.one.id = *array;
1444 array++;
1445 }
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001446 }
1447
1448 if (type & PERF_SAMPLE_CALLCHAIN) {
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001449 const u64 max_callchain_nr = UINT64_MAX / sizeof(u64);
1450
1451 OVERFLOW_CHECK_u64(array);
1452 data->callchain = (struct ip_callchain *)array++;
1453 if (data->callchain->nr > max_callchain_nr)
Frederic Weisbecker98e1da92011-05-21 20:08:15 +02001454 return -EFAULT;
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001455 sz = data->callchain->nr * sizeof(u64);
1456 OVERFLOW_CHECK(array, sz, max_size);
1457 array = (void *)array + sz;
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001458 }
1459
1460 if (type & PERF_SAMPLE_RAW) {
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001461 OVERFLOW_CHECK_u64(array);
David Ahern936be502011-09-06 09:12:26 -06001462 u.val64 = *array;
1463 if (WARN_ONCE(swapped,
1464 "Endianness of raw data not corrected!\n")) {
1465 /* undo swap of u64, then swap on individual u32s */
1466 u.val64 = bswap_64(u.val64);
1467 u.val32[0] = bswap_32(u.val32[0]);
1468 u.val32[1] = bswap_32(u.val32[1]);
1469 }
David Ahern936be502011-09-06 09:12:26 -06001470 data->raw_size = u.val32[0];
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001471 array = (void *)array + sizeof(u32);
Frederic Weisbecker98e1da92011-05-21 20:08:15 +02001472
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001473 OVERFLOW_CHECK(array, data->raw_size, max_size);
1474 data->raw_data = (void *)array;
1475 array = (void *)array + data->raw_size;
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001476 }
1477
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +01001478 if (type & PERF_SAMPLE_BRANCH_STACK) {
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001479 const u64 max_branch_nr = UINT64_MAX /
1480 sizeof(struct branch_entry);
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +01001481
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001482 OVERFLOW_CHECK_u64(array);
1483 data->branch_stack = (struct branch_stack *)array++;
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +01001484
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001485 if (data->branch_stack->nr > max_branch_nr)
1486 return -EFAULT;
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +01001487 sz = data->branch_stack->nr * sizeof(struct branch_entry);
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001488 OVERFLOW_CHECK(array, sz, max_size);
1489 array = (void *)array + sz;
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +01001490 }
Jiri Olsa0f6a3012012-08-07 15:20:45 +02001491
1492 if (type & PERF_SAMPLE_REGS_USER) {
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001493 OVERFLOW_CHECK_u64(array);
Adrian Hunter5b95a4a32013-08-27 11:23:10 +03001494 data->user_regs.abi = *array;
1495 array++;
Jiri Olsa0f6a3012012-08-07 15:20:45 +02001496
Adrian Hunter5b95a4a32013-08-27 11:23:10 +03001497 if (data->user_regs.abi) {
Jiri Olsa352ea452014-01-07 13:47:25 +01001498 u64 mask = evsel->attr.sample_regs_user;
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001499
Jiri Olsa352ea452014-01-07 13:47:25 +01001500 sz = hweight_long(mask) * sizeof(u64);
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001501 OVERFLOW_CHECK(array, sz, max_size);
Jiri Olsa352ea452014-01-07 13:47:25 +01001502 data->user_regs.mask = mask;
Jiri Olsa0f6a3012012-08-07 15:20:45 +02001503 data->user_regs.regs = (u64 *)array;
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001504 array = (void *)array + sz;
Jiri Olsa0f6a3012012-08-07 15:20:45 +02001505 }
1506 }
1507
1508 if (type & PERF_SAMPLE_STACK_USER) {
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001509 OVERFLOW_CHECK_u64(array);
1510 sz = *array++;
Jiri Olsa0f6a3012012-08-07 15:20:45 +02001511
1512 data->user_stack.offset = ((char *)(array - 1)
1513 - (char *) event);
1514
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001515 if (!sz) {
Jiri Olsa0f6a3012012-08-07 15:20:45 +02001516 data->user_stack.size = 0;
1517 } else {
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001518 OVERFLOW_CHECK(array, sz, max_size);
Jiri Olsa0f6a3012012-08-07 15:20:45 +02001519 data->user_stack.data = (char *)array;
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001520 array = (void *)array + sz;
1521 OVERFLOW_CHECK_u64(array);
Adrian Hunter54bd2692013-07-04 16:20:34 +03001522 data->user_stack.size = *array++;
Jiri Olsaa65cb4b2013-10-02 15:46:39 +02001523 if (WARN_ONCE(data->user_stack.size > sz,
1524 "user stack dump failure\n"))
1525 return -EFAULT;
Jiri Olsa0f6a3012012-08-07 15:20:45 +02001526 }
1527 }
1528
Andi Kleen05484292013-01-24 16:10:29 +01001529 data->weight = 0;
1530 if (type & PERF_SAMPLE_WEIGHT) {
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001531 OVERFLOW_CHECK_u64(array);
Andi Kleen05484292013-01-24 16:10:29 +01001532 data->weight = *array;
1533 array++;
1534 }
1535
Stephane Eranian98a3b322013-01-24 16:10:35 +01001536 data->data_src = PERF_MEM_DATA_SRC_NONE;
1537 if (type & PERF_SAMPLE_DATA_SRC) {
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001538 OVERFLOW_CHECK_u64(array);
Stephane Eranian98a3b322013-01-24 16:10:35 +01001539 data->data_src = *array;
1540 array++;
1541 }
1542
Andi Kleen475eeab2013-09-20 07:40:43 -07001543 data->transaction = 0;
1544 if (type & PERF_SAMPLE_TRANSACTION) {
Adrian Hunter87b95522013-11-01 15:51:36 +02001545 OVERFLOW_CHECK_u64(array);
Andi Kleen475eeab2013-09-20 07:40:43 -07001546 data->transaction = *array;
1547 array++;
1548 }
1549
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001550 return 0;
1551}
Andrew Vagin74eec262011-11-28 12:03:31 +03001552
Adrian Hunterb1cf6f62013-08-27 11:23:12 +03001553size_t perf_event__sample_event_size(const struct perf_sample *sample, u64 type,
Jiri Olsa352ea452014-01-07 13:47:25 +01001554 u64 read_format)
Adrian Hunterb1cf6f62013-08-27 11:23:12 +03001555{
1556 size_t sz, result = sizeof(struct sample_event);
1557
1558 if (type & PERF_SAMPLE_IDENTIFIER)
1559 result += sizeof(u64);
1560
1561 if (type & PERF_SAMPLE_IP)
1562 result += sizeof(u64);
1563
1564 if (type & PERF_SAMPLE_TID)
1565 result += sizeof(u64);
1566
1567 if (type & PERF_SAMPLE_TIME)
1568 result += sizeof(u64);
1569
1570 if (type & PERF_SAMPLE_ADDR)
1571 result += sizeof(u64);
1572
1573 if (type & PERF_SAMPLE_ID)
1574 result += sizeof(u64);
1575
1576 if (type & PERF_SAMPLE_STREAM_ID)
1577 result += sizeof(u64);
1578
1579 if (type & PERF_SAMPLE_CPU)
1580 result += sizeof(u64);
1581
1582 if (type & PERF_SAMPLE_PERIOD)
1583 result += sizeof(u64);
1584
1585 if (type & PERF_SAMPLE_READ) {
1586 result += sizeof(u64);
1587 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
1588 result += sizeof(u64);
1589 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
1590 result += sizeof(u64);
1591 /* PERF_FORMAT_ID is forced for PERF_SAMPLE_READ */
1592 if (read_format & PERF_FORMAT_GROUP) {
1593 sz = sample->read.group.nr *
1594 sizeof(struct sample_read_value);
1595 result += sz;
1596 } else {
1597 result += sizeof(u64);
1598 }
1599 }
1600
1601 if (type & PERF_SAMPLE_CALLCHAIN) {
1602 sz = (sample->callchain->nr + 1) * sizeof(u64);
1603 result += sz;
1604 }
1605
1606 if (type & PERF_SAMPLE_RAW) {
1607 result += sizeof(u32);
1608 result += sample->raw_size;
1609 }
1610
1611 if (type & PERF_SAMPLE_BRANCH_STACK) {
1612 sz = sample->branch_stack->nr * sizeof(struct branch_entry);
1613 sz += sizeof(u64);
1614 result += sz;
1615 }
1616
1617 if (type & PERF_SAMPLE_REGS_USER) {
1618 if (sample->user_regs.abi) {
1619 result += sizeof(u64);
Jiri Olsa352ea452014-01-07 13:47:25 +01001620 sz = hweight_long(sample->user_regs.mask) * sizeof(u64);
Adrian Hunterb1cf6f62013-08-27 11:23:12 +03001621 result += sz;
1622 } else {
1623 result += sizeof(u64);
1624 }
1625 }
1626
1627 if (type & PERF_SAMPLE_STACK_USER) {
1628 sz = sample->user_stack.size;
1629 result += sizeof(u64);
1630 if (sz) {
1631 result += sz;
1632 result += sizeof(u64);
1633 }
1634 }
1635
1636 if (type & PERF_SAMPLE_WEIGHT)
1637 result += sizeof(u64);
1638
1639 if (type & PERF_SAMPLE_DATA_SRC)
1640 result += sizeof(u64);
1641
Adrian Hunter42d88912013-11-01 15:51:38 +02001642 if (type & PERF_SAMPLE_TRANSACTION)
1643 result += sizeof(u64);
1644
Adrian Hunterb1cf6f62013-08-27 11:23:12 +03001645 return result;
1646}
1647
Andrew Vagin74eec262011-11-28 12:03:31 +03001648int perf_event__synthesize_sample(union perf_event *event, u64 type,
Jiri Olsa352ea452014-01-07 13:47:25 +01001649 u64 read_format,
Andrew Vagin74eec262011-11-28 12:03:31 +03001650 const struct perf_sample *sample,
1651 bool swapped)
1652{
1653 u64 *array;
Adrian Hunterd03f2172013-08-27 11:23:11 +03001654 size_t sz;
Andrew Vagin74eec262011-11-28 12:03:31 +03001655 /*
1656 * used for cross-endian analysis. See git commit 65014ab3
1657 * for why this goofiness is needed.
1658 */
Jiri Olsa6a11f922012-05-16 08:59:04 +02001659 union u64_swap u;
Andrew Vagin74eec262011-11-28 12:03:31 +03001660
1661 array = event->sample.array;
1662
Adrian Hunter75562572013-08-27 11:23:09 +03001663 if (type & PERF_SAMPLE_IDENTIFIER) {
1664 *array = sample->id;
1665 array++;
1666 }
1667
Andrew Vagin74eec262011-11-28 12:03:31 +03001668 if (type & PERF_SAMPLE_IP) {
Adrian Hunteref893252013-08-27 11:23:06 +03001669 *array = sample->ip;
Andrew Vagin74eec262011-11-28 12:03:31 +03001670 array++;
1671 }
1672
1673 if (type & PERF_SAMPLE_TID) {
1674 u.val32[0] = sample->pid;
1675 u.val32[1] = sample->tid;
1676 if (swapped) {
1677 /*
Arnaldo Carvalho de Meloa3f698f2012-08-02 12:23:46 -03001678 * Inverse of what is done in perf_evsel__parse_sample
Andrew Vagin74eec262011-11-28 12:03:31 +03001679 */
1680 u.val32[0] = bswap_32(u.val32[0]);
1681 u.val32[1] = bswap_32(u.val32[1]);
1682 u.val64 = bswap_64(u.val64);
1683 }
1684
1685 *array = u.val64;
1686 array++;
1687 }
1688
1689 if (type & PERF_SAMPLE_TIME) {
1690 *array = sample->time;
1691 array++;
1692 }
1693
1694 if (type & PERF_SAMPLE_ADDR) {
1695 *array = sample->addr;
1696 array++;
1697 }
1698
1699 if (type & PERF_SAMPLE_ID) {
1700 *array = sample->id;
1701 array++;
1702 }
1703
1704 if (type & PERF_SAMPLE_STREAM_ID) {
1705 *array = sample->stream_id;
1706 array++;
1707 }
1708
1709 if (type & PERF_SAMPLE_CPU) {
1710 u.val32[0] = sample->cpu;
1711 if (swapped) {
1712 /*
Arnaldo Carvalho de Meloa3f698f2012-08-02 12:23:46 -03001713 * Inverse of what is done in perf_evsel__parse_sample
Andrew Vagin74eec262011-11-28 12:03:31 +03001714 */
1715 u.val32[0] = bswap_32(u.val32[0]);
1716 u.val64 = bswap_64(u.val64);
1717 }
1718 *array = u.val64;
1719 array++;
1720 }
1721
1722 if (type & PERF_SAMPLE_PERIOD) {
1723 *array = sample->period;
1724 array++;
1725 }
1726
Adrian Hunterd03f2172013-08-27 11:23:11 +03001727 if (type & PERF_SAMPLE_READ) {
1728 if (read_format & PERF_FORMAT_GROUP)
1729 *array = sample->read.group.nr;
1730 else
1731 *array = sample->read.one.value;
1732 array++;
1733
1734 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) {
1735 *array = sample->read.time_enabled;
1736 array++;
1737 }
1738
1739 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) {
1740 *array = sample->read.time_running;
1741 array++;
1742 }
1743
1744 /* PERF_FORMAT_ID is forced for PERF_SAMPLE_READ */
1745 if (read_format & PERF_FORMAT_GROUP) {
1746 sz = sample->read.group.nr *
1747 sizeof(struct sample_read_value);
1748 memcpy(array, sample->read.group.values, sz);
1749 array = (void *)array + sz;
1750 } else {
1751 *array = sample->read.one.id;
1752 array++;
1753 }
1754 }
1755
1756 if (type & PERF_SAMPLE_CALLCHAIN) {
1757 sz = (sample->callchain->nr + 1) * sizeof(u64);
1758 memcpy(array, sample->callchain, sz);
1759 array = (void *)array + sz;
1760 }
1761
1762 if (type & PERF_SAMPLE_RAW) {
1763 u.val32[0] = sample->raw_size;
1764 if (WARN_ONCE(swapped,
1765 "Endianness of raw data not corrected!\n")) {
1766 /*
1767 * Inverse of what is done in perf_evsel__parse_sample
1768 */
1769 u.val32[0] = bswap_32(u.val32[0]);
1770 u.val32[1] = bswap_32(u.val32[1]);
1771 u.val64 = bswap_64(u.val64);
1772 }
1773 *array = u.val64;
1774 array = (void *)array + sizeof(u32);
1775
1776 memcpy(array, sample->raw_data, sample->raw_size);
1777 array = (void *)array + sample->raw_size;
1778 }
1779
1780 if (type & PERF_SAMPLE_BRANCH_STACK) {
1781 sz = sample->branch_stack->nr * sizeof(struct branch_entry);
1782 sz += sizeof(u64);
1783 memcpy(array, sample->branch_stack, sz);
1784 array = (void *)array + sz;
1785 }
1786
1787 if (type & PERF_SAMPLE_REGS_USER) {
1788 if (sample->user_regs.abi) {
1789 *array++ = sample->user_regs.abi;
Jiri Olsa352ea452014-01-07 13:47:25 +01001790 sz = hweight_long(sample->user_regs.mask) * sizeof(u64);
Adrian Hunterd03f2172013-08-27 11:23:11 +03001791 memcpy(array, sample->user_regs.regs, sz);
1792 array = (void *)array + sz;
1793 } else {
1794 *array++ = 0;
1795 }
1796 }
1797
1798 if (type & PERF_SAMPLE_STACK_USER) {
1799 sz = sample->user_stack.size;
1800 *array++ = sz;
1801 if (sz) {
1802 memcpy(array, sample->user_stack.data, sz);
1803 array = (void *)array + sz;
1804 *array++ = sz;
1805 }
1806 }
1807
1808 if (type & PERF_SAMPLE_WEIGHT) {
1809 *array = sample->weight;
1810 array++;
1811 }
1812
1813 if (type & PERF_SAMPLE_DATA_SRC) {
1814 *array = sample->data_src;
1815 array++;
1816 }
1817
Adrian Hunter42d88912013-11-01 15:51:38 +02001818 if (type & PERF_SAMPLE_TRANSACTION) {
1819 *array = sample->transaction;
1820 array++;
1821 }
1822
Andrew Vagin74eec262011-11-28 12:03:31 +03001823 return 0;
1824}
Arnaldo Carvalho de Melo5555ded2012-09-11 19:24:23 -03001825
Arnaldo Carvalho de Meloefd2b922012-09-18 11:21:50 -03001826struct format_field *perf_evsel__field(struct perf_evsel *evsel, const char *name)
1827{
1828 return pevent_find_field(evsel->tp_format, name);
1829}
1830
Arnaldo Carvalho de Melo5d2074e2012-09-26 20:22:00 -03001831void *perf_evsel__rawptr(struct perf_evsel *evsel, struct perf_sample *sample,
Arnaldo Carvalho de Melo5555ded2012-09-11 19:24:23 -03001832 const char *name)
1833{
Arnaldo Carvalho de Meloefd2b922012-09-18 11:21:50 -03001834 struct format_field *field = perf_evsel__field(evsel, name);
Arnaldo Carvalho de Melo5555ded2012-09-11 19:24:23 -03001835 int offset;
1836
Arnaldo Carvalho de Meloefd2b922012-09-18 11:21:50 -03001837 if (!field)
1838 return NULL;
Arnaldo Carvalho de Melo5555ded2012-09-11 19:24:23 -03001839
1840 offset = field->offset;
1841
1842 if (field->flags & FIELD_IS_DYNAMIC) {
1843 offset = *(int *)(sample->raw_data + field->offset);
1844 offset &= 0xffff;
1845 }
1846
1847 return sample->raw_data + offset;
1848}
1849
1850u64 perf_evsel__intval(struct perf_evsel *evsel, struct perf_sample *sample,
1851 const char *name)
1852{
Arnaldo Carvalho de Meloefd2b922012-09-18 11:21:50 -03001853 struct format_field *field = perf_evsel__field(evsel, name);
Arnaldo Carvalho de Meloe6b6f672012-09-26 13:13:04 -03001854 void *ptr;
1855 u64 value;
Arnaldo Carvalho de Melo5555ded2012-09-11 19:24:23 -03001856
Arnaldo Carvalho de Meloefd2b922012-09-18 11:21:50 -03001857 if (!field)
1858 return 0;
Arnaldo Carvalho de Melo5555ded2012-09-11 19:24:23 -03001859
Arnaldo Carvalho de Meloe6b6f672012-09-26 13:13:04 -03001860 ptr = sample->raw_data + field->offset;
Arnaldo Carvalho de Melo5555ded2012-09-11 19:24:23 -03001861
Arnaldo Carvalho de Meloe6b6f672012-09-26 13:13:04 -03001862 switch (field->size) {
1863 case 1:
1864 return *(u8 *)ptr;
1865 case 2:
1866 value = *(u16 *)ptr;
1867 break;
1868 case 4:
1869 value = *(u32 *)ptr;
1870 break;
1871 case 8:
1872 value = *(u64 *)ptr;
1873 break;
1874 default:
1875 return 0;
1876 }
1877
1878 if (!evsel->needs_swap)
1879 return value;
1880
1881 switch (field->size) {
1882 case 2:
1883 return bswap_16(value);
1884 case 4:
1885 return bswap_32(value);
1886 case 8:
1887 return bswap_64(value);
1888 default:
1889 return 0;
1890 }
1891
1892 return 0;
Arnaldo Carvalho de Melo5555ded2012-09-11 19:24:23 -03001893}
Arnaldo Carvalho de Melo0698aed2012-12-10 18:17:08 -03001894
1895static int comma_fprintf(FILE *fp, bool *first, const char *fmt, ...)
1896{
1897 va_list args;
1898 int ret = 0;
1899
1900 if (!*first) {
1901 ret += fprintf(fp, ",");
1902 } else {
1903 ret += fprintf(fp, ":");
1904 *first = false;
1905 }
1906
1907 va_start(args, fmt);
1908 ret += vfprintf(fp, fmt, args);
1909 va_end(args);
1910 return ret;
1911}
1912
1913static int __if_fprintf(FILE *fp, bool *first, const char *field, u64 value)
1914{
1915 if (value == 0)
1916 return 0;
1917
1918 return comma_fprintf(fp, first, " %s: %" PRIu64, field, value);
1919}
1920
1921#define if_print(field) printed += __if_fprintf(fp, &first, #field, evsel->attr.field)
1922
Arnaldo Carvalho de Meloc79a4392012-12-11 10:54:12 -03001923struct bit_names {
1924 int bit;
1925 const char *name;
1926};
1927
1928static int bits__fprintf(FILE *fp, const char *field, u64 value,
1929 struct bit_names *bits, bool *first)
1930{
1931 int i = 0, printed = comma_fprintf(fp, first, " %s: ", field);
1932 bool first_bit = true;
1933
1934 do {
1935 if (value & bits[i].bit) {
1936 printed += fprintf(fp, "%s%s", first_bit ? "" : "|", bits[i].name);
1937 first_bit = false;
1938 }
1939 } while (bits[++i].name != NULL);
1940
1941 return printed;
1942}
1943
1944static int sample_type__fprintf(FILE *fp, bool *first, u64 value)
1945{
1946#define bit_name(n) { PERF_SAMPLE_##n, #n }
1947 struct bit_names bits[] = {
1948 bit_name(IP), bit_name(TID), bit_name(TIME), bit_name(ADDR),
1949 bit_name(READ), bit_name(CALLCHAIN), bit_name(ID), bit_name(CPU),
1950 bit_name(PERIOD), bit_name(STREAM_ID), bit_name(RAW),
1951 bit_name(BRANCH_STACK), bit_name(REGS_USER), bit_name(STACK_USER),
Adrian Hunter75562572013-08-27 11:23:09 +03001952 bit_name(IDENTIFIER),
Arnaldo Carvalho de Meloc79a4392012-12-11 10:54:12 -03001953 { .name = NULL, }
1954 };
1955#undef bit_name
1956 return bits__fprintf(fp, "sample_type", value, bits, first);
1957}
1958
1959static int read_format__fprintf(FILE *fp, bool *first, u64 value)
1960{
1961#define bit_name(n) { PERF_FORMAT_##n, #n }
1962 struct bit_names bits[] = {
1963 bit_name(TOTAL_TIME_ENABLED), bit_name(TOTAL_TIME_RUNNING),
1964 bit_name(ID), bit_name(GROUP),
1965 { .name = NULL, }
1966 };
1967#undef bit_name
1968 return bits__fprintf(fp, "read_format", value, bits, first);
1969}
1970
Arnaldo Carvalho de Melo0698aed2012-12-10 18:17:08 -03001971int perf_evsel__fprintf(struct perf_evsel *evsel,
1972 struct perf_attr_details *details, FILE *fp)
1973{
1974 bool first = true;
Namhyung Kime6ab07d2013-01-22 18:09:47 +09001975 int printed = 0;
1976
Arnaldo Carvalho de Meloe35ef3552013-02-06 17:20:02 -03001977 if (details->event_group) {
Namhyung Kime6ab07d2013-01-22 18:09:47 +09001978 struct perf_evsel *pos;
1979
1980 if (!perf_evsel__is_group_leader(evsel))
1981 return 0;
1982
1983 if (evsel->nr_members > 1)
1984 printed += fprintf(fp, "%s{", evsel->group_name ?: "");
1985
1986 printed += fprintf(fp, "%s", perf_evsel__name(evsel));
1987 for_each_group_member(pos, evsel)
1988 printed += fprintf(fp, ",%s", perf_evsel__name(pos));
1989
1990 if (evsel->nr_members > 1)
1991 printed += fprintf(fp, "}");
1992 goto out;
1993 }
1994
1995 printed += fprintf(fp, "%s", perf_evsel__name(evsel));
Arnaldo Carvalho de Melo0698aed2012-12-10 18:17:08 -03001996
1997 if (details->verbose || details->freq) {
1998 printed += comma_fprintf(fp, &first, " sample_freq=%" PRIu64,
1999 (u64)evsel->attr.sample_freq);
2000 }
2001
2002 if (details->verbose) {
2003 if_print(type);
2004 if_print(config);
2005 if_print(config1);
2006 if_print(config2);
2007 if_print(size);
Arnaldo Carvalho de Meloc79a4392012-12-11 10:54:12 -03002008 printed += sample_type__fprintf(fp, &first, evsel->attr.sample_type);
2009 if (evsel->attr.read_format)
2010 printed += read_format__fprintf(fp, &first, evsel->attr.read_format);
Arnaldo Carvalho de Melo0698aed2012-12-10 18:17:08 -03002011 if_print(disabled);
2012 if_print(inherit);
2013 if_print(pinned);
2014 if_print(exclusive);
2015 if_print(exclude_user);
2016 if_print(exclude_kernel);
2017 if_print(exclude_hv);
2018 if_print(exclude_idle);
2019 if_print(mmap);
Stephane Eranian5c5e8542013-08-21 12:10:25 +02002020 if_print(mmap2);
Arnaldo Carvalho de Melo0698aed2012-12-10 18:17:08 -03002021 if_print(comm);
Adrian Hunter022c50d2014-07-14 13:02:27 +03002022 if_print(comm_exec);
Arnaldo Carvalho de Melo0698aed2012-12-10 18:17:08 -03002023 if_print(freq);
2024 if_print(inherit_stat);
2025 if_print(enable_on_exec);
2026 if_print(task);
2027 if_print(watermark);
2028 if_print(precise_ip);
2029 if_print(mmap_data);
2030 if_print(sample_id_all);
2031 if_print(exclude_host);
2032 if_print(exclude_guest);
2033 if_print(__reserved_1);
2034 if_print(wakeup_events);
2035 if_print(bp_type);
2036 if_print(branch_sample_type);
2037 }
Namhyung Kime6ab07d2013-01-22 18:09:47 +09002038out:
Arnaldo Carvalho de Melo0698aed2012-12-10 18:17:08 -03002039 fputc('\n', fp);
2040 return ++printed;
2041}
Arnaldo Carvalho de Meloc0a54342012-12-13 14:16:30 -03002042
2043bool perf_evsel__fallback(struct perf_evsel *evsel, int err,
2044 char *msg, size_t msgsize)
2045{
David Ahern2b821cc2013-07-18 17:27:59 -06002046 if ((err == ENOENT || err == ENXIO || err == ENODEV) &&
Arnaldo Carvalho de Meloc0a54342012-12-13 14:16:30 -03002047 evsel->attr.type == PERF_TYPE_HARDWARE &&
2048 evsel->attr.config == PERF_COUNT_HW_CPU_CYCLES) {
2049 /*
2050 * If it's cycles then fall back to hrtimer based
2051 * cpu-clock-tick sw counter, which is always available even if
2052 * no PMU support.
2053 *
2054 * PPC returns ENXIO until 2.6.37 (behavior changed with commit
2055 * b0a873e).
2056 */
2057 scnprintf(msg, msgsize, "%s",
2058"The cycles event is not supported, trying to fall back to cpu-clock-ticks");
2059
2060 evsel->attr.type = PERF_TYPE_SOFTWARE;
2061 evsel->attr.config = PERF_COUNT_SW_CPU_CLOCK;
2062
Arnaldo Carvalho de Melo04662522013-12-26 17:41:15 -03002063 zfree(&evsel->name);
Arnaldo Carvalho de Meloc0a54342012-12-13 14:16:30 -03002064 return true;
2065 }
2066
2067 return false;
2068}
Arnaldo Carvalho de Melo56e52e82012-12-13 15:10:58 -03002069
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03002070int perf_evsel__open_strerror(struct perf_evsel *evsel, struct target *target,
Arnaldo Carvalho de Melo56e52e82012-12-13 15:10:58 -03002071 int err, char *msg, size_t size)
2072{
Masami Hiramatsu6e81c742014-08-14 02:22:36 +00002073 char sbuf[STRERR_BUFSIZE];
2074
Arnaldo Carvalho de Melo56e52e82012-12-13 15:10:58 -03002075 switch (err) {
2076 case EPERM:
2077 case EACCES:
David Ahernb69e63a2013-05-25 17:54:00 -06002078 return scnprintf(msg, size,
Arnaldo Carvalho de Melo56e52e82012-12-13 15:10:58 -03002079 "You may not have permission to collect %sstats.\n"
2080 "Consider tweaking /proc/sys/kernel/perf_event_paranoid:\n"
2081 " -1 - Not paranoid at all\n"
2082 " 0 - Disallow raw tracepoint access for unpriv\n"
2083 " 1 - Disallow cpu events for unpriv\n"
2084 " 2 - Disallow kernel profiling for unpriv",
2085 target->system_wide ? "system-wide " : "");
2086 case ENOENT:
2087 return scnprintf(msg, size, "The %s event is not supported.",
2088 perf_evsel__name(evsel));
2089 case EMFILE:
2090 return scnprintf(msg, size, "%s",
2091 "Too many events are opened.\n"
2092 "Try again after reducing the number of events.");
2093 case ENODEV:
2094 if (target->cpu_list)
2095 return scnprintf(msg, size, "%s",
2096 "No such device - did you specify an out-of-range profile CPU?\n");
2097 break;
2098 case EOPNOTSUPP:
2099 if (evsel->attr.precise_ip)
2100 return scnprintf(msg, size, "%s",
2101 "\'precise\' request may not be supported. Try removing 'p' modifier.");
2102#if defined(__i386__) || defined(__x86_64__)
2103 if (evsel->attr.type == PERF_TYPE_HARDWARE)
2104 return scnprintf(msg, size, "%s",
2105 "No hardware sampling interrupt available.\n"
2106 "No APIC? If so then you can boot the kernel with the \"lapic\" boot parameter to force-enable it.");
2107#endif
2108 break;
Jiri Olsa63914ac2014-08-01 17:46:54 +02002109 case EBUSY:
2110 if (find_process("oprofiled"))
2111 return scnprintf(msg, size,
2112 "The PMU counters are busy/taken by another profiler.\n"
2113 "We found oprofile daemon running, please stop it and try again.");
2114 break;
Arnaldo Carvalho de Melo56e52e82012-12-13 15:10:58 -03002115 default:
2116 break;
2117 }
2118
2119 return scnprintf(msg, size,
Masami Hiramatsu6e81c742014-08-14 02:22:36 +00002120 "The sys_perf_event_open() syscall returned with %d (%s) for event (%s).\n"
Arnaldo Carvalho de Melo56e52e82012-12-13 15:10:58 -03002121 "/bin/dmesg may provide additional information.\n"
2122 "No CONFIG_PERF_EVENTS=y kernel support configured?\n",
Masami Hiramatsu6e81c742014-08-14 02:22:36 +00002123 err, strerror_r(err, sbuf, sizeof(sbuf)),
2124 perf_evsel__name(evsel));
Arnaldo Carvalho de Melo56e52e82012-12-13 15:10:58 -03002125}