blob: a0837642744862b792a51165cee7f2fc6320cb1a [file] [log] [blame]
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -02001/*
2 * Copyright (C) 2011, Red Hat Inc, Arnaldo Carvalho de Melo <acme@redhat.com>
3 *
4 * Parts came from builtin-{top,stat,record}.c, see those files for further
5 * copyright notes.
6 *
7 * Released under the GPL v2. (and only v2, not any later version)
8 */
9
David Ahern936be502011-09-06 09:12:26 -060010#include <byteswap.h>
Jiri Olsa0f6a3012012-08-07 15:20:45 +020011#include <linux/bitops.h>
Borislav Petkov553873e2013-12-09 17:14:23 +010012#include <api/fs/debugfs.h>
Robert Richter4e319022013-06-11 17:29:18 +020013#include <traceevent/event-parse.h>
14#include <linux/hw_breakpoint.h>
15#include <linux/perf_event.h>
Andi Kleenbec19672013-08-04 19:41:26 -070016#include <sys/resource.h>
Robert Richter4e319022013-06-11 17:29:18 +020017#include "asm/bug.h"
Arnaldo Carvalho de Melo8f651ea2014-10-09 16:12:24 -030018#include "callchain.h"
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -020019#include "evsel.h"
Arnaldo Carvalho de Melo70082dd2011-01-12 17:03:24 -020020#include "evlist.h"
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -020021#include "util.h"
Arnaldo Carvalho de Melo86bd5e82011-01-03 23:09:46 -020022#include "cpumap.h"
Arnaldo Carvalho de Melofd782602011-01-18 15:15:24 -020023#include "thread_map.h"
Namhyung Kim12864b32012-04-26 14:15:22 +090024#include "target.h"
Jiri Olsa26d33022012-08-07 15:20:47 +020025#include "perf_regs.h"
Adrian Huntere3e1a542013-08-14 15:48:24 +030026#include "debug.h"
Jiri Olsa97978b32013-12-03 14:09:24 +010027#include "trace-event.h"
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -020028
Arnaldo Carvalho de Melo594ac612012-12-13 13:13:07 -030029static struct {
30 bool sample_id_all;
31 bool exclude_guest;
Stephane Eranian5c5e8542013-08-21 12:10:25 +020032 bool mmap2;
Yann Droneaud57480d22014-06-30 22:28:47 +020033 bool cloexec;
Arnaldo Carvalho de Melo594ac612012-12-13 13:13:07 -030034} perf_missing_features;
35
Arnaldo Carvalho de Meloce8ccff2014-10-09 15:29:51 -030036static int perf_evsel__no_extra_init(struct perf_evsel *evsel __maybe_unused)
37{
38 return 0;
39}
40
41static void perf_evsel__no_extra_fini(struct perf_evsel *evsel __maybe_unused)
42{
43}
44
45static struct {
46 size_t size;
47 int (*init)(struct perf_evsel *evsel);
48 void (*fini)(struct perf_evsel *evsel);
49} perf_evsel__object = {
50 .size = sizeof(struct perf_evsel),
51 .init = perf_evsel__no_extra_init,
52 .fini = perf_evsel__no_extra_fini,
53};
54
55int perf_evsel__object_config(size_t object_size,
56 int (*init)(struct perf_evsel *evsel),
57 void (*fini)(struct perf_evsel *evsel))
58{
59
60 if (object_size == 0)
61 goto set_methods;
62
63 if (perf_evsel__object.size > object_size)
64 return -EINVAL;
65
66 perf_evsel__object.size = object_size;
67
68set_methods:
69 if (init != NULL)
70 perf_evsel__object.init = init;
71
72 if (fini != NULL)
73 perf_evsel__object.fini = fini;
74
75 return 0;
76}
77
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -020078#define FD(e, x, y) (*(int *)xyarray__entry(e->fd, x, y))
79
Adrian Hunter75562572013-08-27 11:23:09 +030080int __perf_evsel__sample_size(u64 sample_type)
Arnaldo Carvalho de Meloc2a70652011-06-02 11:04:54 -030081{
82 u64 mask = sample_type & PERF_SAMPLE_MASK;
83 int size = 0;
84 int i;
85
86 for (i = 0; i < 64; i++) {
87 if (mask & (1ULL << i))
88 size++;
89 }
90
91 size *= sizeof(u64);
92
93 return size;
94}
95
Adrian Hunter75562572013-08-27 11:23:09 +030096/**
97 * __perf_evsel__calc_id_pos - calculate id_pos.
98 * @sample_type: sample type
99 *
100 * This function returns the position of the event id (PERF_SAMPLE_ID or
101 * PERF_SAMPLE_IDENTIFIER) in a sample event i.e. in the array of struct
102 * sample_event.
103 */
104static int __perf_evsel__calc_id_pos(u64 sample_type)
105{
106 int idx = 0;
107
108 if (sample_type & PERF_SAMPLE_IDENTIFIER)
109 return 0;
110
111 if (!(sample_type & PERF_SAMPLE_ID))
112 return -1;
113
114 if (sample_type & PERF_SAMPLE_IP)
115 idx += 1;
116
117 if (sample_type & PERF_SAMPLE_TID)
118 idx += 1;
119
120 if (sample_type & PERF_SAMPLE_TIME)
121 idx += 1;
122
123 if (sample_type & PERF_SAMPLE_ADDR)
124 idx += 1;
125
126 return idx;
127}
128
129/**
130 * __perf_evsel__calc_is_pos - calculate is_pos.
131 * @sample_type: sample type
132 *
133 * This function returns the position (counting backwards) of the event id
134 * (PERF_SAMPLE_ID or PERF_SAMPLE_IDENTIFIER) in a non-sample event i.e. if
135 * sample_id_all is used there is an id sample appended to non-sample events.
136 */
137static int __perf_evsel__calc_is_pos(u64 sample_type)
138{
139 int idx = 1;
140
141 if (sample_type & PERF_SAMPLE_IDENTIFIER)
142 return 1;
143
144 if (!(sample_type & PERF_SAMPLE_ID))
145 return -1;
146
147 if (sample_type & PERF_SAMPLE_CPU)
148 idx += 1;
149
150 if (sample_type & PERF_SAMPLE_STREAM_ID)
151 idx += 1;
152
153 return idx;
154}
155
156void perf_evsel__calc_id_pos(struct perf_evsel *evsel)
157{
158 evsel->id_pos = __perf_evsel__calc_id_pos(evsel->attr.sample_type);
159 evsel->is_pos = __perf_evsel__calc_is_pos(evsel->attr.sample_type);
160}
161
Jiri Olsa4bf9ce12012-03-22 14:37:26 +0100162void hists__init(struct hists *hists)
Arnaldo Carvalho de Melo0e2a5f12011-11-04 08:16:58 -0200163{
164 memset(hists, 0, sizeof(*hists));
165 hists->entries_in_array[0] = hists->entries_in_array[1] = RB_ROOT;
166 hists->entries_in = &hists->entries_in_array[0];
167 hists->entries_collapsed = RB_ROOT;
168 hists->entries = RB_ROOT;
169 pthread_mutex_init(&hists->lock, NULL);
170}
171
Arnaldo Carvalho de Melo7be5ebe2012-12-10 14:53:43 -0300172void __perf_evsel__set_sample_bit(struct perf_evsel *evsel,
173 enum perf_event_sample_format bit)
174{
175 if (!(evsel->attr.sample_type & bit)) {
176 evsel->attr.sample_type |= bit;
177 evsel->sample_size += sizeof(u64);
Adrian Hunter75562572013-08-27 11:23:09 +0300178 perf_evsel__calc_id_pos(evsel);
Arnaldo Carvalho de Melo7be5ebe2012-12-10 14:53:43 -0300179 }
180}
181
182void __perf_evsel__reset_sample_bit(struct perf_evsel *evsel,
183 enum perf_event_sample_format bit)
184{
185 if (evsel->attr.sample_type & bit) {
186 evsel->attr.sample_type &= ~bit;
187 evsel->sample_size -= sizeof(u64);
Adrian Hunter75562572013-08-27 11:23:09 +0300188 perf_evsel__calc_id_pos(evsel);
Arnaldo Carvalho de Melo7be5ebe2012-12-10 14:53:43 -0300189 }
190}
191
Adrian Hunter75562572013-08-27 11:23:09 +0300192void perf_evsel__set_sample_id(struct perf_evsel *evsel,
193 bool can_sample_identifier)
Arnaldo Carvalho de Melo7a5a5ca2012-12-10 15:21:30 -0300194{
Adrian Hunter75562572013-08-27 11:23:09 +0300195 if (can_sample_identifier) {
196 perf_evsel__reset_sample_bit(evsel, ID);
197 perf_evsel__set_sample_bit(evsel, IDENTIFIER);
198 } else {
199 perf_evsel__set_sample_bit(evsel, ID);
200 }
Arnaldo Carvalho de Melo7a5a5ca2012-12-10 15:21:30 -0300201 evsel->attr.read_format |= PERF_FORMAT_ID;
202}
203
Arnaldo Carvalho de Meloef1d1af2011-01-18 21:41:45 -0200204void perf_evsel__init(struct perf_evsel *evsel,
205 struct perf_event_attr *attr, int idx)
206{
207 evsel->idx = idx;
Adrian Hunter60b08962014-07-31 09:00:52 +0300208 evsel->tracking = !idx;
Arnaldo Carvalho de Meloef1d1af2011-01-18 21:41:45 -0200209 evsel->attr = *attr;
Namhyung Kim2cfda562012-11-29 15:38:29 +0900210 evsel->leader = evsel;
Stephane Eranian410136f2013-11-12 17:58:49 +0100211 evsel->unit = "";
212 evsel->scale = 1.0;
Arnaldo Carvalho de Meloef1d1af2011-01-18 21:41:45 -0200213 INIT_LIST_HEAD(&evsel->node);
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300214 hists__init(&evsel->hists);
Arnaldo Carvalho de Meloce8ccff2014-10-09 15:29:51 -0300215 perf_evsel__object.init(evsel);
Arnaldo Carvalho de Melobde09462012-08-01 18:53:11 -0300216 evsel->sample_size = __perf_evsel__sample_size(attr->sample_type);
Adrian Hunter75562572013-08-27 11:23:09 +0300217 perf_evsel__calc_id_pos(evsel);
Arnaldo Carvalho de Meloef1d1af2011-01-18 21:41:45 -0200218}
219
Arnaldo Carvalho de Meloef503832013-11-07 16:41:19 -0300220struct perf_evsel *perf_evsel__new_idx(struct perf_event_attr *attr, int idx)
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200221{
Arnaldo Carvalho de Meloce8ccff2014-10-09 15:29:51 -0300222 struct perf_evsel *evsel = zalloc(perf_evsel__object.size);
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200223
Arnaldo Carvalho de Meloef1d1af2011-01-18 21:41:45 -0200224 if (evsel != NULL)
225 perf_evsel__init(evsel, attr, idx);
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200226
227 return evsel;
228}
229
Arnaldo Carvalho de Meloef503832013-11-07 16:41:19 -0300230struct perf_evsel *perf_evsel__newtp_idx(const char *sys, const char *name, int idx)
Arnaldo Carvalho de Meloefd2b922012-09-18 11:21:50 -0300231{
Arnaldo Carvalho de Meloce8ccff2014-10-09 15:29:51 -0300232 struct perf_evsel *evsel = zalloc(perf_evsel__object.size);
Arnaldo Carvalho de Meloefd2b922012-09-18 11:21:50 -0300233
234 if (evsel != NULL) {
235 struct perf_event_attr attr = {
Arnaldo Carvalho de Melo0b80f8b32012-09-26 12:28:26 -0300236 .type = PERF_TYPE_TRACEPOINT,
237 .sample_type = (PERF_SAMPLE_RAW | PERF_SAMPLE_TIME |
238 PERF_SAMPLE_CPU | PERF_SAMPLE_PERIOD),
Arnaldo Carvalho de Meloefd2b922012-09-18 11:21:50 -0300239 };
240
Arnaldo Carvalho de Meloe48ffe22012-09-26 17:11:38 -0300241 if (asprintf(&evsel->name, "%s:%s", sys, name) < 0)
242 goto out_free;
243
Jiri Olsa97978b32013-12-03 14:09:24 +0100244 evsel->tp_format = trace_event__tp_format(sys, name);
Arnaldo Carvalho de Meloefd2b922012-09-18 11:21:50 -0300245 if (evsel->tp_format == NULL)
246 goto out_free;
247
Arnaldo Carvalho de Melo0b80f8b32012-09-26 12:28:26 -0300248 event_attr_init(&attr);
Arnaldo Carvalho de Meloefd2b922012-09-18 11:21:50 -0300249 attr.config = evsel->tp_format->id;
Arnaldo Carvalho de Melo0b80f8b32012-09-26 12:28:26 -0300250 attr.sample_period = 1;
Arnaldo Carvalho de Meloefd2b922012-09-18 11:21:50 -0300251 perf_evsel__init(evsel, &attr, idx);
Arnaldo Carvalho de Meloefd2b922012-09-18 11:21:50 -0300252 }
253
254 return evsel;
255
256out_free:
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -0300257 zfree(&evsel->name);
Arnaldo Carvalho de Meloefd2b922012-09-18 11:21:50 -0300258 free(evsel);
259 return NULL;
260}
261
Arnaldo Carvalho de Melo8ad70132012-09-06 13:11:18 -0300262const char *perf_evsel__hw_names[PERF_COUNT_HW_MAX] = {
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300263 "cycles",
264 "instructions",
265 "cache-references",
266 "cache-misses",
267 "branches",
268 "branch-misses",
269 "bus-cycles",
270 "stalled-cycles-frontend",
271 "stalled-cycles-backend",
272 "ref-cycles",
273};
274
Arnaldo Carvalho de Melodd4f5222012-06-13 15:52:42 -0300275static const char *__perf_evsel__hw_name(u64 config)
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300276{
277 if (config < PERF_COUNT_HW_MAX && perf_evsel__hw_names[config])
278 return perf_evsel__hw_names[config];
279
280 return "unknown-hardware";
281}
282
Arnaldo Carvalho de Melo27f18612012-06-11 13:33:09 -0300283static int perf_evsel__add_modifiers(struct perf_evsel *evsel, char *bf, size_t size)
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300284{
Arnaldo Carvalho de Melo27f18612012-06-11 13:33:09 -0300285 int colon = 0, r = 0;
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300286 struct perf_event_attr *attr = &evsel->attr;
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300287 bool exclude_guest_default = false;
288
289#define MOD_PRINT(context, mod) do { \
290 if (!attr->exclude_##context) { \
Arnaldo Carvalho de Melo27f18612012-06-11 13:33:09 -0300291 if (!colon) colon = ++r; \
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300292 r += scnprintf(bf + r, size - r, "%c", mod); \
293 } } while(0)
294
295 if (attr->exclude_kernel || attr->exclude_user || attr->exclude_hv) {
296 MOD_PRINT(kernel, 'k');
297 MOD_PRINT(user, 'u');
298 MOD_PRINT(hv, 'h');
299 exclude_guest_default = true;
300 }
301
302 if (attr->precise_ip) {
303 if (!colon)
Arnaldo Carvalho de Melo27f18612012-06-11 13:33:09 -0300304 colon = ++r;
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300305 r += scnprintf(bf + r, size - r, "%.*s", attr->precise_ip, "ppp");
306 exclude_guest_default = true;
307 }
308
309 if (attr->exclude_host || attr->exclude_guest == exclude_guest_default) {
310 MOD_PRINT(host, 'H');
311 MOD_PRINT(guest, 'G');
312 }
313#undef MOD_PRINT
314 if (colon)
Arnaldo Carvalho de Melo27f18612012-06-11 13:33:09 -0300315 bf[colon - 1] = ':';
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300316 return r;
317}
318
Arnaldo Carvalho de Melo27f18612012-06-11 13:33:09 -0300319static int perf_evsel__hw_name(struct perf_evsel *evsel, char *bf, size_t size)
320{
321 int r = scnprintf(bf, size, "%s", __perf_evsel__hw_name(evsel->attr.config));
322 return r + perf_evsel__add_modifiers(evsel, bf + r, size - r);
323}
324
Arnaldo Carvalho de Melo8ad70132012-09-06 13:11:18 -0300325const char *perf_evsel__sw_names[PERF_COUNT_SW_MAX] = {
Arnaldo Carvalho de Melo335c2f52012-06-11 14:36:20 -0300326 "cpu-clock",
327 "task-clock",
328 "page-faults",
329 "context-switches",
Arnaldo Carvalho de Melo8ad70132012-09-06 13:11:18 -0300330 "cpu-migrations",
Arnaldo Carvalho de Melo335c2f52012-06-11 14:36:20 -0300331 "minor-faults",
332 "major-faults",
333 "alignment-faults",
334 "emulation-faults",
Adrian Hunterd22d1a22013-08-31 21:50:52 +0300335 "dummy",
Arnaldo Carvalho de Melo335c2f52012-06-11 14:36:20 -0300336};
337
Arnaldo Carvalho de Melodd4f5222012-06-13 15:52:42 -0300338static const char *__perf_evsel__sw_name(u64 config)
Arnaldo Carvalho de Melo335c2f52012-06-11 14:36:20 -0300339{
340 if (config < PERF_COUNT_SW_MAX && perf_evsel__sw_names[config])
341 return perf_evsel__sw_names[config];
342 return "unknown-software";
343}
344
345static int perf_evsel__sw_name(struct perf_evsel *evsel, char *bf, size_t size)
346{
347 int r = scnprintf(bf, size, "%s", __perf_evsel__sw_name(evsel->attr.config));
348 return r + perf_evsel__add_modifiers(evsel, bf + r, size - r);
349}
350
Jiri Olsa287e74a2012-06-28 23:18:49 +0200351static int __perf_evsel__bp_name(char *bf, size_t size, u64 addr, u64 type)
352{
353 int r;
354
355 r = scnprintf(bf, size, "mem:0x%" PRIx64 ":", addr);
356
357 if (type & HW_BREAKPOINT_R)
358 r += scnprintf(bf + r, size - r, "r");
359
360 if (type & HW_BREAKPOINT_W)
361 r += scnprintf(bf + r, size - r, "w");
362
363 if (type & HW_BREAKPOINT_X)
364 r += scnprintf(bf + r, size - r, "x");
365
366 return r;
367}
368
369static int perf_evsel__bp_name(struct perf_evsel *evsel, char *bf, size_t size)
370{
371 struct perf_event_attr *attr = &evsel->attr;
372 int r = __perf_evsel__bp_name(bf, size, attr->bp_addr, attr->bp_type);
373 return r + perf_evsel__add_modifiers(evsel, bf + r, size - r);
374}
375
Arnaldo Carvalho de Melo0b668bc2012-06-11 14:08:07 -0300376const char *perf_evsel__hw_cache[PERF_COUNT_HW_CACHE_MAX]
377 [PERF_EVSEL__MAX_ALIASES] = {
378 { "L1-dcache", "l1-d", "l1d", "L1-data", },
379 { "L1-icache", "l1-i", "l1i", "L1-instruction", },
380 { "LLC", "L2", },
381 { "dTLB", "d-tlb", "Data-TLB", },
382 { "iTLB", "i-tlb", "Instruction-TLB", },
383 { "branch", "branches", "bpu", "btb", "bpc", },
384 { "node", },
385};
386
387const char *perf_evsel__hw_cache_op[PERF_COUNT_HW_CACHE_OP_MAX]
388 [PERF_EVSEL__MAX_ALIASES] = {
389 { "load", "loads", "read", },
390 { "store", "stores", "write", },
391 { "prefetch", "prefetches", "speculative-read", "speculative-load", },
392};
393
394const char *perf_evsel__hw_cache_result[PERF_COUNT_HW_CACHE_RESULT_MAX]
395 [PERF_EVSEL__MAX_ALIASES] = {
396 { "refs", "Reference", "ops", "access", },
397 { "misses", "miss", },
398};
399
400#define C(x) PERF_COUNT_HW_CACHE_##x
401#define CACHE_READ (1 << C(OP_READ))
402#define CACHE_WRITE (1 << C(OP_WRITE))
403#define CACHE_PREFETCH (1 << C(OP_PREFETCH))
404#define COP(x) (1 << x)
405
406/*
407 * cache operartion stat
408 * L1I : Read and prefetch only
409 * ITLB and BPU : Read-only
410 */
411static unsigned long perf_evsel__hw_cache_stat[C(MAX)] = {
412 [C(L1D)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
413 [C(L1I)] = (CACHE_READ | CACHE_PREFETCH),
414 [C(LL)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
415 [C(DTLB)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
416 [C(ITLB)] = (CACHE_READ),
417 [C(BPU)] = (CACHE_READ),
418 [C(NODE)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
419};
420
421bool perf_evsel__is_cache_op_valid(u8 type, u8 op)
422{
423 if (perf_evsel__hw_cache_stat[type] & COP(op))
424 return true; /* valid */
425 else
426 return false; /* invalid */
427}
428
429int __perf_evsel__hw_cache_type_op_res_name(u8 type, u8 op, u8 result,
430 char *bf, size_t size)
431{
432 if (result) {
433 return scnprintf(bf, size, "%s-%s-%s", perf_evsel__hw_cache[type][0],
434 perf_evsel__hw_cache_op[op][0],
435 perf_evsel__hw_cache_result[result][0]);
436 }
437
438 return scnprintf(bf, size, "%s-%s", perf_evsel__hw_cache[type][0],
439 perf_evsel__hw_cache_op[op][1]);
440}
441
Arnaldo Carvalho de Melodd4f5222012-06-13 15:52:42 -0300442static int __perf_evsel__hw_cache_name(u64 config, char *bf, size_t size)
Arnaldo Carvalho de Melo0b668bc2012-06-11 14:08:07 -0300443{
444 u8 op, result, type = (config >> 0) & 0xff;
445 const char *err = "unknown-ext-hardware-cache-type";
446
447 if (type > PERF_COUNT_HW_CACHE_MAX)
448 goto out_err;
449
450 op = (config >> 8) & 0xff;
451 err = "unknown-ext-hardware-cache-op";
452 if (op > PERF_COUNT_HW_CACHE_OP_MAX)
453 goto out_err;
454
455 result = (config >> 16) & 0xff;
456 err = "unknown-ext-hardware-cache-result";
457 if (result > PERF_COUNT_HW_CACHE_RESULT_MAX)
458 goto out_err;
459
460 err = "invalid-cache";
461 if (!perf_evsel__is_cache_op_valid(type, op))
462 goto out_err;
463
464 return __perf_evsel__hw_cache_type_op_res_name(type, op, result, bf, size);
465out_err:
466 return scnprintf(bf, size, "%s", err);
467}
468
469static int perf_evsel__hw_cache_name(struct perf_evsel *evsel, char *bf, size_t size)
470{
471 int ret = __perf_evsel__hw_cache_name(evsel->attr.config, bf, size);
472 return ret + perf_evsel__add_modifiers(evsel, bf + ret, size - ret);
473}
474
Arnaldo Carvalho de Melo6eef3d92012-06-13 11:53:37 -0300475static int perf_evsel__raw_name(struct perf_evsel *evsel, char *bf, size_t size)
476{
477 int ret = scnprintf(bf, size, "raw 0x%" PRIx64, evsel->attr.config);
478 return ret + perf_evsel__add_modifiers(evsel, bf + ret, size - ret);
479}
480
Arnaldo Carvalho de Melo7289f832012-06-12 12:34:58 -0300481const char *perf_evsel__name(struct perf_evsel *evsel)
Arnaldo Carvalho de Meloa4460832012-06-12 10:29:12 -0300482{
Arnaldo Carvalho de Melo7289f832012-06-12 12:34:58 -0300483 char bf[128];
Arnaldo Carvalho de Meloa4460832012-06-12 10:29:12 -0300484
Arnaldo Carvalho de Melo7289f832012-06-12 12:34:58 -0300485 if (evsel->name)
486 return evsel->name;
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300487
488 switch (evsel->attr.type) {
489 case PERF_TYPE_RAW:
Arnaldo Carvalho de Melo6eef3d92012-06-13 11:53:37 -0300490 perf_evsel__raw_name(evsel, bf, sizeof(bf));
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300491 break;
492
493 case PERF_TYPE_HARDWARE:
Arnaldo Carvalho de Melo7289f832012-06-12 12:34:58 -0300494 perf_evsel__hw_name(evsel, bf, sizeof(bf));
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300495 break;
Arnaldo Carvalho de Melo0b668bc2012-06-11 14:08:07 -0300496
497 case PERF_TYPE_HW_CACHE:
Arnaldo Carvalho de Melo7289f832012-06-12 12:34:58 -0300498 perf_evsel__hw_cache_name(evsel, bf, sizeof(bf));
Arnaldo Carvalho de Melo0b668bc2012-06-11 14:08:07 -0300499 break;
500
Arnaldo Carvalho de Melo335c2f52012-06-11 14:36:20 -0300501 case PERF_TYPE_SOFTWARE:
Arnaldo Carvalho de Melo7289f832012-06-12 12:34:58 -0300502 perf_evsel__sw_name(evsel, bf, sizeof(bf));
Arnaldo Carvalho de Melo335c2f52012-06-11 14:36:20 -0300503 break;
504
Arnaldo Carvalho de Meloa4460832012-06-12 10:29:12 -0300505 case PERF_TYPE_TRACEPOINT:
Arnaldo Carvalho de Melo7289f832012-06-12 12:34:58 -0300506 scnprintf(bf, sizeof(bf), "%s", "unknown tracepoint");
Arnaldo Carvalho de Meloa4460832012-06-12 10:29:12 -0300507 break;
508
Jiri Olsa287e74a2012-06-28 23:18:49 +0200509 case PERF_TYPE_BREAKPOINT:
510 perf_evsel__bp_name(evsel, bf, sizeof(bf));
511 break;
512
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300513 default:
Robert Richterca1b1452012-08-16 21:10:18 +0200514 scnprintf(bf, sizeof(bf), "unknown attr type: %d",
515 evsel->attr.type);
Arnaldo Carvalho de Meloa4460832012-06-12 10:29:12 -0300516 break;
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300517 }
518
Arnaldo Carvalho de Melo7289f832012-06-12 12:34:58 -0300519 evsel->name = strdup(bf);
520
521 return evsel->name ?: "unknown";
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300522}
523
Namhyung Kim717e2632013-01-22 18:09:44 +0900524const char *perf_evsel__group_name(struct perf_evsel *evsel)
525{
526 return evsel->group_name ?: "anon group";
527}
528
529int perf_evsel__group_desc(struct perf_evsel *evsel, char *buf, size_t size)
530{
531 int ret;
532 struct perf_evsel *pos;
533 const char *group_name = perf_evsel__group_name(evsel);
534
535 ret = scnprintf(buf, size, "%s", group_name);
536
537 ret += scnprintf(buf + ret, size - ret, " { %s",
538 perf_evsel__name(evsel));
539
540 for_each_group_member(pos, evsel)
541 ret += scnprintf(buf + ret, size - ret, ", %s",
542 perf_evsel__name(pos));
543
544 ret += scnprintf(buf + ret, size - ret, " }");
545
546 return ret;
547}
548
Jiri Olsa6bedfab2014-03-02 16:56:40 +0100549static void
Namhyung Kim72a128a2014-09-23 10:01:41 +0900550perf_evsel__config_callgraph(struct perf_evsel *evsel)
Jiri Olsa6bedfab2014-03-02 16:56:40 +0100551{
552 bool function = perf_evsel__is_function_event(evsel);
553 struct perf_event_attr *attr = &evsel->attr;
554
555 perf_evsel__set_sample_bit(evsel, CALLCHAIN);
556
Namhyung Kim72a128a2014-09-23 10:01:41 +0900557 if (callchain_param.record_mode == CALLCHAIN_DWARF) {
Jiri Olsa6bedfab2014-03-02 16:56:40 +0100558 if (!function) {
559 perf_evsel__set_sample_bit(evsel, REGS_USER);
560 perf_evsel__set_sample_bit(evsel, STACK_USER);
561 attr->sample_regs_user = PERF_REGS_MASK;
Namhyung Kim72a128a2014-09-23 10:01:41 +0900562 attr->sample_stack_user = callchain_param.dump_size;
Jiri Olsa6bedfab2014-03-02 16:56:40 +0100563 attr->exclude_callchain_user = 1;
564 } else {
565 pr_info("Cannot use DWARF unwind for function trace event,"
566 " falling back to framepointers.\n");
567 }
568 }
569
570 if (function) {
571 pr_info("Disabling user space callchains for function trace event.\n");
572 attr->exclude_callchain_user = 1;
573 }
574}
575
Jiri Olsa774cb492012-11-12 18:34:01 +0100576/*
577 * The enable_on_exec/disabled value strategy:
578 *
579 * 1) For any type of traced program:
580 * - all independent events and group leaders are disabled
581 * - all group members are enabled
582 *
583 * Group members are ruled by group leaders. They need to
584 * be enabled, because the group scheduling relies on that.
585 *
586 * 2) For traced programs executed by perf:
587 * - all independent events and group leaders have
588 * enable_on_exec set
589 * - we don't specifically enable or disable any event during
590 * the record command
591 *
592 * Independent events and group leaders are initially disabled
593 * and get enabled by exec. Group members are ruled by group
594 * leaders as stated in 1).
595 *
596 * 3) For traced programs attached by perf (pid/tid):
597 * - we specifically enable or disable all events during
598 * the record command
599 *
600 * When attaching events to already running traced we
601 * enable/disable events specifically, as there's no
602 * initial traced exec call.
603 */
Arnaldo Carvalho de Melob4006792013-12-19 14:43:45 -0300604void perf_evsel__config(struct perf_evsel *evsel, struct record_opts *opts)
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200605{
Jiri Olsa3c176312012-10-10 17:39:03 +0200606 struct perf_evsel *leader = evsel->leader;
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200607 struct perf_event_attr *attr = &evsel->attr;
Adrian Hunter60b08962014-07-31 09:00:52 +0300608 int track = evsel->tracking;
Adrian Hunter3aa59392013-11-15 15:52:29 +0200609 bool per_cpu = opts->target.default_per_cpu && !opts->target.per_thread;
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200610
Arnaldo Carvalho de Melo594ac612012-12-13 13:13:07 -0300611 attr->sample_id_all = perf_missing_features.sample_id_all ? 0 : 1;
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200612 attr->inherit = !opts->no_inherit;
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200613
Arnaldo Carvalho de Melo7be5ebe2012-12-10 14:53:43 -0300614 perf_evsel__set_sample_bit(evsel, IP);
615 perf_evsel__set_sample_bit(evsel, TID);
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200616
Jiri Olsa3c176312012-10-10 17:39:03 +0200617 if (evsel->sample_read) {
618 perf_evsel__set_sample_bit(evsel, READ);
619
620 /*
621 * We need ID even in case of single event, because
622 * PERF_SAMPLE_READ process ID specific data.
623 */
Adrian Hunter75562572013-08-27 11:23:09 +0300624 perf_evsel__set_sample_id(evsel, false);
Jiri Olsa3c176312012-10-10 17:39:03 +0200625
626 /*
627 * Apply group format only if we belong to group
628 * with more than one members.
629 */
630 if (leader->nr_members > 1) {
631 attr->read_format |= PERF_FORMAT_GROUP;
632 attr->inherit = 0;
633 }
634 }
635
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200636 /*
Namhyung Kim17314e22014-06-09 14:43:37 +0900637 * We default some events to have a default interval. But keep
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200638 * it a weak assumption overridable by the user.
639 */
Namhyung Kim17314e22014-06-09 14:43:37 +0900640 if (!attr->sample_period || (opts->user_freq != UINT_MAX ||
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200641 opts->user_interval != ULLONG_MAX)) {
642 if (opts->freq) {
Arnaldo Carvalho de Melo7be5ebe2012-12-10 14:53:43 -0300643 perf_evsel__set_sample_bit(evsel, PERIOD);
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200644 attr->freq = 1;
645 attr->sample_freq = opts->freq;
646 } else {
647 attr->sample_period = opts->default_interval;
648 }
649 }
650
Jiri Olsa3c176312012-10-10 17:39:03 +0200651 /*
652 * Disable sampling for all group members other
653 * than leader in case leader 'leads' the sampling.
654 */
655 if ((leader != evsel) && leader->sample_read) {
656 attr->sample_freq = 0;
657 attr->sample_period = 0;
658 }
659
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200660 if (opts->no_samples)
661 attr->sample_freq = 0;
662
663 if (opts->inherit_stat)
664 attr->inherit_stat = 1;
665
666 if (opts->sample_address) {
Arnaldo Carvalho de Melo7be5ebe2012-12-10 14:53:43 -0300667 perf_evsel__set_sample_bit(evsel, ADDR);
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200668 attr->mmap_data = track;
669 }
670
Namhyung Kim72a128a2014-09-23 10:01:41 +0900671 if (callchain_param.enabled && !evsel->no_aux_samples)
672 perf_evsel__config_callgraph(evsel);
Jiri Olsa26d33022012-08-07 15:20:47 +0200673
Adrian Hunter3aa59392013-11-15 15:52:29 +0200674 if (target__has_cpu(&opts->target))
Arnaldo Carvalho de Melo7be5ebe2012-12-10 14:53:43 -0300675 perf_evsel__set_sample_bit(evsel, CPU);
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200676
Andrew Vagin3e76ac72011-12-20 17:32:45 +0300677 if (opts->period)
Arnaldo Carvalho de Melo7be5ebe2012-12-10 14:53:43 -0300678 perf_evsel__set_sample_bit(evsel, PERIOD);
Andrew Vagin3e76ac72011-12-20 17:32:45 +0300679
Andi Kleen8affc2b2014-07-31 14:45:04 +0800680 /*
681 * When the user explicitely disabled time don't force it here.
682 */
683 if (opts->sample_time &&
684 (!perf_missing_features.sample_id_all &&
685 (!opts->no_inherit || target__has_cpu(&opts->target) || per_cpu)))
Arnaldo Carvalho de Melo7be5ebe2012-12-10 14:53:43 -0300686 perf_evsel__set_sample_bit(evsel, TIME);
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200687
Adrian Hunter6ff1ce72014-07-14 13:02:56 +0300688 if (opts->raw_samples && !evsel->no_aux_samples) {
Arnaldo Carvalho de Melo7be5ebe2012-12-10 14:53:43 -0300689 perf_evsel__set_sample_bit(evsel, TIME);
690 perf_evsel__set_sample_bit(evsel, RAW);
691 perf_evsel__set_sample_bit(evsel, CPU);
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200692 }
693
Stephane Eranianccf49bf2013-01-24 16:10:37 +0100694 if (opts->sample_address)
Adrian Hunter1e7ed5e2013-11-01 15:51:35 +0200695 perf_evsel__set_sample_bit(evsel, DATA_SRC);
Stephane Eranianccf49bf2013-01-24 16:10:37 +0100696
Arnaldo Carvalho de Melo509051e2014-01-14 17:52:14 -0300697 if (opts->no_buffering) {
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200698 attr->watermark = 0;
699 attr->wakeup_events = 1;
700 }
Adrian Hunter6ff1ce72014-07-14 13:02:56 +0300701 if (opts->branch_stack && !evsel->no_aux_samples) {
Arnaldo Carvalho de Melo7be5ebe2012-12-10 14:53:43 -0300702 perf_evsel__set_sample_bit(evsel, BRANCH_STACK);
Roberto Agostino Vitillobdfebd82012-02-09 23:21:02 +0100703 attr->branch_sample_type = opts->branch_stack;
704 }
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200705
Andi Kleen05484292013-01-24 16:10:29 +0100706 if (opts->sample_weight)
Adrian Hunter1e7ed5e2013-11-01 15:51:35 +0200707 perf_evsel__set_sample_bit(evsel, WEIGHT);
Andi Kleen05484292013-01-24 16:10:29 +0100708
Stephane Eranian5c5e8542013-08-21 12:10:25 +0200709 attr->mmap = track;
Don Zickusa5a5ba72014-05-30 10:49:42 -0400710 attr->mmap2 = track && !perf_missing_features.mmap2;
Stephane Eranian5c5e8542013-08-21 12:10:25 +0200711 attr->comm = track;
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200712
Andi Kleen475eeab2013-09-20 07:40:43 -0700713 if (opts->sample_transaction)
Adrian Hunter1e7ed5e2013-11-01 15:51:35 +0200714 perf_evsel__set_sample_bit(evsel, TRANSACTION);
Andi Kleen475eeab2013-09-20 07:40:43 -0700715
Jiri Olsa774cb492012-11-12 18:34:01 +0100716 /*
717 * XXX see the function comment above
718 *
719 * Disabling only independent events or group leaders,
720 * keeping group members enabled.
721 */
Namhyung Kim823254e2012-11-29 15:38:30 +0900722 if (perf_evsel__is_group_leader(evsel))
Jiri Olsa774cb492012-11-12 18:34:01 +0100723 attr->disabled = 1;
724
725 /*
726 * Setting enable_on_exec for independent events and
727 * group leaders for traced executed by perf.
728 */
Andi Kleen6619a532014-01-11 13:38:27 -0800729 if (target__none(&opts->target) && perf_evsel__is_group_leader(evsel) &&
730 !opts->initial_delay)
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200731 attr->enable_on_exec = 1;
Adrian Hunter2afd2bc2014-07-14 13:02:57 +0300732
733 if (evsel->immediate) {
734 attr->disabled = 0;
735 attr->enable_on_exec = 0;
736 }
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200737}
738
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200739int perf_evsel__alloc_fd(struct perf_evsel *evsel, int ncpus, int nthreads)
740{
David Ahern4af4c952011-05-27 09:58:34 -0600741 int cpu, thread;
Adrian Hunterbf8e8f42014-07-31 09:00:51 +0300742
743 if (evsel->system_wide)
744 nthreads = 1;
745
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200746 evsel->fd = xyarray__new(ncpus, nthreads, sizeof(int));
David Ahern4af4c952011-05-27 09:58:34 -0600747
748 if (evsel->fd) {
749 for (cpu = 0; cpu < ncpus; cpu++) {
750 for (thread = 0; thread < nthreads; thread++) {
751 FD(evsel, cpu, thread) = -1;
752 }
753 }
754 }
755
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200756 return evsel->fd != NULL ? 0 : -ENOMEM;
757}
758
Andi Kleene2407be2013-08-02 17:41:10 -0700759static int perf_evsel__run_ioctl(struct perf_evsel *evsel, int ncpus, int nthreads,
760 int ioc, void *arg)
Arnaldo Carvalho de Melo745cefc2012-09-26 15:07:39 -0300761{
762 int cpu, thread;
763
Adrian Hunterbf8e8f42014-07-31 09:00:51 +0300764 if (evsel->system_wide)
765 nthreads = 1;
766
Arnaldo Carvalho de Melo745cefc2012-09-26 15:07:39 -0300767 for (cpu = 0; cpu < ncpus; cpu++) {
768 for (thread = 0; thread < nthreads; thread++) {
769 int fd = FD(evsel, cpu, thread),
Andi Kleene2407be2013-08-02 17:41:10 -0700770 err = ioctl(fd, ioc, arg);
Arnaldo Carvalho de Melo745cefc2012-09-26 15:07:39 -0300771
772 if (err)
773 return err;
774 }
775 }
776
777 return 0;
778}
779
Andi Kleene2407be2013-08-02 17:41:10 -0700780int perf_evsel__set_filter(struct perf_evsel *evsel, int ncpus, int nthreads,
781 const char *filter)
782{
783 return perf_evsel__run_ioctl(evsel, ncpus, nthreads,
784 PERF_EVENT_IOC_SET_FILTER,
785 (void *)filter);
786}
787
788int perf_evsel__enable(struct perf_evsel *evsel, int ncpus, int nthreads)
789{
790 return perf_evsel__run_ioctl(evsel, ncpus, nthreads,
791 PERF_EVENT_IOC_ENABLE,
792 0);
793}
794
Arnaldo Carvalho de Melo70db7532011-01-12 22:39:13 -0200795int perf_evsel__alloc_id(struct perf_evsel *evsel, int ncpus, int nthreads)
796{
Adrian Hunterbf8e8f42014-07-31 09:00:51 +0300797 if (evsel->system_wide)
798 nthreads = 1;
799
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -0300800 evsel->sample_id = xyarray__new(ncpus, nthreads, sizeof(struct perf_sample_id));
801 if (evsel->sample_id == NULL)
802 return -ENOMEM;
803
804 evsel->id = zalloc(ncpus * nthreads * sizeof(u64));
805 if (evsel->id == NULL) {
806 xyarray__delete(evsel->sample_id);
807 evsel->sample_id = NULL;
808 return -ENOMEM;
809 }
810
811 return 0;
Arnaldo Carvalho de Melo70db7532011-01-12 22:39:13 -0200812}
813
Frederik Deweerdta7e191c2013-03-01 13:02:27 -0500814void perf_evsel__reset_counts(struct perf_evsel *evsel, int ncpus)
815{
816 memset(evsel->counts, 0, (sizeof(*evsel->counts) +
817 (ncpus * sizeof(struct perf_counts_values))));
818}
819
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200820int perf_evsel__alloc_counts(struct perf_evsel *evsel, int ncpus)
821{
822 evsel->counts = zalloc((sizeof(*evsel->counts) +
823 (ncpus * sizeof(struct perf_counts_values))));
824 return evsel->counts != NULL ? 0 : -ENOMEM;
825}
826
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200827void perf_evsel__free_fd(struct perf_evsel *evsel)
828{
829 xyarray__delete(evsel->fd);
830 evsel->fd = NULL;
831}
832
Arnaldo Carvalho de Melo70db7532011-01-12 22:39:13 -0200833void perf_evsel__free_id(struct perf_evsel *evsel)
834{
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -0300835 xyarray__delete(evsel->sample_id);
836 evsel->sample_id = NULL;
Arnaldo Carvalho de Melo04662522013-12-26 17:41:15 -0300837 zfree(&evsel->id);
Arnaldo Carvalho de Melo70db7532011-01-12 22:39:13 -0200838}
839
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200840void perf_evsel__close_fd(struct perf_evsel *evsel, int ncpus, int nthreads)
841{
842 int cpu, thread;
843
Adrian Hunterbf8e8f42014-07-31 09:00:51 +0300844 if (evsel->system_wide)
845 nthreads = 1;
846
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200847 for (cpu = 0; cpu < ncpus; cpu++)
848 for (thread = 0; thread < nthreads; ++thread) {
849 close(FD(evsel, cpu, thread));
850 FD(evsel, cpu, thread) = -1;
851 }
852}
853
Namhyung Kim43f8e762013-01-25 10:44:44 +0900854void perf_evsel__free_counts(struct perf_evsel *evsel)
855{
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -0300856 zfree(&evsel->counts);
Namhyung Kim43f8e762013-01-25 10:44:44 +0900857}
858
Arnaldo Carvalho de Meloef1d1af2011-01-18 21:41:45 -0200859void perf_evsel__exit(struct perf_evsel *evsel)
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200860{
861 assert(list_empty(&evsel->node));
Namhyung Kim736b05a2013-03-15 14:48:49 +0900862 perf_evsel__free_fd(evsel);
863 perf_evsel__free_id(evsel);
Arnaldo Carvalho de Meloce8ccff2014-10-09 15:29:51 -0300864 perf_evsel__object.fini(evsel);
Arnaldo Carvalho de Meloef1d1af2011-01-18 21:41:45 -0200865}
866
867void perf_evsel__delete(struct perf_evsel *evsel)
868{
869 perf_evsel__exit(evsel);
Stephane Eranian023695d2011-02-14 11:20:01 +0200870 close_cgroup(evsel->cgrp);
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -0300871 zfree(&evsel->group_name);
Arnaldo Carvalho de Meloe48ffe22012-09-26 17:11:38 -0300872 if (evsel->tp_format)
Arnaldo Carvalho de Meloefd2b922012-09-18 11:21:50 -0300873 pevent_free_format(evsel->tp_format);
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -0300874 zfree(&evsel->name);
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200875 free(evsel);
876}
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200877
Stephane Eranianc7a79c42013-01-29 12:47:43 +0100878static inline void compute_deltas(struct perf_evsel *evsel,
879 int cpu,
880 struct perf_counts_values *count)
881{
882 struct perf_counts_values tmp;
883
884 if (!evsel->prev_raw_counts)
885 return;
886
887 if (cpu == -1) {
888 tmp = evsel->prev_raw_counts->aggr;
889 evsel->prev_raw_counts->aggr = *count;
890 } else {
891 tmp = evsel->prev_raw_counts->cpu[cpu];
892 evsel->prev_raw_counts->cpu[cpu] = *count;
893 }
894
895 count->val = count->val - tmp.val;
896 count->ena = count->ena - tmp.ena;
897 count->run = count->run - tmp.run;
898}
899
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200900int __perf_evsel__read_on_cpu(struct perf_evsel *evsel,
901 int cpu, int thread, bool scale)
902{
903 struct perf_counts_values count;
904 size_t nv = scale ? 3 : 1;
905
906 if (FD(evsel, cpu, thread) < 0)
907 return -EINVAL;
908
Arnaldo Carvalho de Melo4eed11d2011-01-04 00:13:17 -0200909 if (evsel->counts == NULL && perf_evsel__alloc_counts(evsel, cpu + 1) < 0)
910 return -ENOMEM;
911
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200912 if (readn(FD(evsel, cpu, thread), &count, nv * sizeof(u64)) < 0)
913 return -errno;
914
Stephane Eranianc7a79c42013-01-29 12:47:43 +0100915 compute_deltas(evsel, cpu, &count);
916
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200917 if (scale) {
918 if (count.run == 0)
919 count.val = 0;
920 else if (count.run < count.ena)
921 count.val = (u64)((double)count.val * count.ena / count.run + 0.5);
922 } else
923 count.ena = count.run = 0;
924
925 evsel->counts->cpu[cpu] = count;
926 return 0;
927}
928
929int __perf_evsel__read(struct perf_evsel *evsel,
930 int ncpus, int nthreads, bool scale)
931{
932 size_t nv = scale ? 3 : 1;
933 int cpu, thread;
934 struct perf_counts_values *aggr = &evsel->counts->aggr, count;
935
Adrian Hunterbf8e8f42014-07-31 09:00:51 +0300936 if (evsel->system_wide)
937 nthreads = 1;
938
Arnaldo Carvalho de Melo52bcd9942011-02-03 17:26:06 -0200939 aggr->val = aggr->ena = aggr->run = 0;
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200940
941 for (cpu = 0; cpu < ncpus; cpu++) {
942 for (thread = 0; thread < nthreads; thread++) {
943 if (FD(evsel, cpu, thread) < 0)
944 continue;
945
946 if (readn(FD(evsel, cpu, thread),
947 &count, nv * sizeof(u64)) < 0)
948 return -errno;
949
950 aggr->val += count.val;
951 if (scale) {
952 aggr->ena += count.ena;
953 aggr->run += count.run;
954 }
955 }
956 }
957
Stephane Eranianc7a79c42013-01-29 12:47:43 +0100958 compute_deltas(evsel, -1, aggr);
959
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200960 evsel->counts->scaled = 0;
961 if (scale) {
962 if (aggr->run == 0) {
963 evsel->counts->scaled = -1;
964 aggr->val = 0;
965 return 0;
966 }
967
968 if (aggr->run < aggr->ena) {
969 evsel->counts->scaled = 1;
970 aggr->val = (u64)((double)aggr->val * aggr->ena / aggr->run + 0.5);
971 }
972 } else
973 aggr->ena = aggr->run = 0;
974
975 return 0;
976}
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -0200977
Jiri Olsa6a4bb042012-08-08 12:22:36 +0200978static int get_group_fd(struct perf_evsel *evsel, int cpu, int thread)
979{
980 struct perf_evsel *leader = evsel->leader;
981 int fd;
982
Namhyung Kim823254e2012-11-29 15:38:30 +0900983 if (perf_evsel__is_group_leader(evsel))
Jiri Olsa6a4bb042012-08-08 12:22:36 +0200984 return -1;
985
986 /*
987 * Leader must be already processed/open,
988 * if not it's a bug.
989 */
990 BUG_ON(!leader->fd);
991
992 fd = FD(leader, cpu, thread);
993 BUG_ON(fd == -1);
994
995 return fd;
996}
997
Adrian Huntere3e1a542013-08-14 15:48:24 +0300998#define __PRINT_ATTR(fmt, cast, field) \
999 fprintf(fp, " %-19s "fmt"\n", #field, cast attr->field)
1000
1001#define PRINT_ATTR_U32(field) __PRINT_ATTR("%u" , , field)
1002#define PRINT_ATTR_X32(field) __PRINT_ATTR("%#x", , field)
1003#define PRINT_ATTR_U64(field) __PRINT_ATTR("%" PRIu64, (uint64_t), field)
1004#define PRINT_ATTR_X64(field) __PRINT_ATTR("%#"PRIx64, (uint64_t), field)
1005
1006#define PRINT_ATTR2N(name1, field1, name2, field2) \
1007 fprintf(fp, " %-19s %u %-19s %u\n", \
1008 name1, attr->field1, name2, attr->field2)
1009
1010#define PRINT_ATTR2(field1, field2) \
1011 PRINT_ATTR2N(#field1, field1, #field2, field2)
1012
1013static size_t perf_event_attr__fprintf(struct perf_event_attr *attr, FILE *fp)
1014{
1015 size_t ret = 0;
1016
1017 ret += fprintf(fp, "%.60s\n", graph_dotted_line);
1018 ret += fprintf(fp, "perf_event_attr:\n");
1019
1020 ret += PRINT_ATTR_U32(type);
1021 ret += PRINT_ATTR_U32(size);
1022 ret += PRINT_ATTR_X64(config);
1023 ret += PRINT_ATTR_U64(sample_period);
1024 ret += PRINT_ATTR_U64(sample_freq);
1025 ret += PRINT_ATTR_X64(sample_type);
1026 ret += PRINT_ATTR_X64(read_format);
1027
1028 ret += PRINT_ATTR2(disabled, inherit);
1029 ret += PRINT_ATTR2(pinned, exclusive);
1030 ret += PRINT_ATTR2(exclude_user, exclude_kernel);
1031 ret += PRINT_ATTR2(exclude_hv, exclude_idle);
1032 ret += PRINT_ATTR2(mmap, comm);
Adrian Hunter022c50d2014-07-14 13:02:27 +03001033 ret += PRINT_ATTR2(mmap2, comm_exec);
Adrian Huntere3e1a542013-08-14 15:48:24 +03001034 ret += PRINT_ATTR2(freq, inherit_stat);
1035 ret += PRINT_ATTR2(enable_on_exec, task);
1036 ret += PRINT_ATTR2(watermark, precise_ip);
1037 ret += PRINT_ATTR2(mmap_data, sample_id_all);
1038 ret += PRINT_ATTR2(exclude_host, exclude_guest);
1039 ret += PRINT_ATTR2N("excl.callchain_kern", exclude_callchain_kernel,
1040 "excl.callchain_user", exclude_callchain_user);
1041
1042 ret += PRINT_ATTR_U32(wakeup_events);
1043 ret += PRINT_ATTR_U32(wakeup_watermark);
1044 ret += PRINT_ATTR_X32(bp_type);
1045 ret += PRINT_ATTR_X64(bp_addr);
1046 ret += PRINT_ATTR_X64(config1);
1047 ret += PRINT_ATTR_U64(bp_len);
1048 ret += PRINT_ATTR_X64(config2);
1049 ret += PRINT_ATTR_X64(branch_sample_type);
1050 ret += PRINT_ATTR_X64(sample_regs_user);
1051 ret += PRINT_ATTR_U32(sample_stack_user);
1052
1053 ret += fprintf(fp, "%.60s\n", graph_dotted_line);
1054
1055 return ret;
1056}
1057
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -02001058static int __perf_evsel__open(struct perf_evsel *evsel, struct cpu_map *cpus,
Jiri Olsa6a4bb042012-08-08 12:22:36 +02001059 struct thread_map *threads)
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -02001060{
Adrian Hunterbf8e8f42014-07-31 09:00:51 +03001061 int cpu, thread, nthreads;
Yann Droneaud57480d22014-06-30 22:28:47 +02001062 unsigned long flags = PERF_FLAG_FD_CLOEXEC;
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -02001063 int pid = -1, err;
Andi Kleenbec19672013-08-04 19:41:26 -07001064 enum { NO_CHANGE, SET_TO_MAX, INCREASED_MAX } set_rlimit = NO_CHANGE;
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -02001065
Adrian Hunterbf8e8f42014-07-31 09:00:51 +03001066 if (evsel->system_wide)
1067 nthreads = 1;
1068 else
1069 nthreads = threads->nr;
1070
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -02001071 if (evsel->fd == NULL &&
Adrian Hunterbf8e8f42014-07-31 09:00:51 +03001072 perf_evsel__alloc_fd(evsel, cpus->nr, nthreads) < 0)
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -02001073 return -ENOMEM;
Arnaldo Carvalho de Melo4eed11d2011-01-04 00:13:17 -02001074
Stephane Eranian023695d2011-02-14 11:20:01 +02001075 if (evsel->cgrp) {
Yann Droneaud57480d22014-06-30 22:28:47 +02001076 flags |= PERF_FLAG_PID_CGROUP;
Stephane Eranian023695d2011-02-14 11:20:01 +02001077 pid = evsel->cgrp->fd;
1078 }
1079
Arnaldo Carvalho de Melo594ac612012-12-13 13:13:07 -03001080fallback_missing_features:
Yann Droneaud57480d22014-06-30 22:28:47 +02001081 if (perf_missing_features.cloexec)
1082 flags &= ~(unsigned long)PERF_FLAG_FD_CLOEXEC;
Stephane Eranian5c5e8542013-08-21 12:10:25 +02001083 if (perf_missing_features.mmap2)
1084 evsel->attr.mmap2 = 0;
Arnaldo Carvalho de Melo594ac612012-12-13 13:13:07 -03001085 if (perf_missing_features.exclude_guest)
1086 evsel->attr.exclude_guest = evsel->attr.exclude_host = 0;
1087retry_sample_id:
1088 if (perf_missing_features.sample_id_all)
1089 evsel->attr.sample_id_all = 0;
1090
Adrian Huntere3e1a542013-08-14 15:48:24 +03001091 if (verbose >= 2)
1092 perf_event_attr__fprintf(&evsel->attr, stderr);
1093
Arnaldo Carvalho de Melo86bd5e82011-01-03 23:09:46 -02001094 for (cpu = 0; cpu < cpus->nr; cpu++) {
Arnaldo Carvalho de Melo9d04f172011-01-12 00:08:18 -02001095
Adrian Hunterbf8e8f42014-07-31 09:00:51 +03001096 for (thread = 0; thread < nthreads; thread++) {
Jiri Olsa6a4bb042012-08-08 12:22:36 +02001097 int group_fd;
Stephane Eranian023695d2011-02-14 11:20:01 +02001098
Adrian Hunterbf8e8f42014-07-31 09:00:51 +03001099 if (!evsel->cgrp && !evsel->system_wide)
Stephane Eranian023695d2011-02-14 11:20:01 +02001100 pid = threads->map[thread];
1101
Jiri Olsa6a4bb042012-08-08 12:22:36 +02001102 group_fd = get_group_fd(evsel, cpu, thread);
Andi Kleenbec19672013-08-04 19:41:26 -07001103retry_open:
Ramkumar Ramachandraa33f6ef2014-03-18 15:10:42 -04001104 pr_debug2("sys_perf_event_open: pid %d cpu %d group_fd %d flags %#lx\n",
Adrian Huntere3e1a542013-08-14 15:48:24 +03001105 pid, cpus->map[cpu], group_fd, flags);
1106
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -02001107 FD(evsel, cpu, thread) = sys_perf_event_open(&evsel->attr,
Stephane Eranian023695d2011-02-14 11:20:01 +02001108 pid,
Arnaldo Carvalho de Melof08199d2011-01-11 23:42:19 -02001109 cpus->map[cpu],
Stephane Eranian023695d2011-02-14 11:20:01 +02001110 group_fd, flags);
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -02001111 if (FD(evsel, cpu, thread) < 0) {
1112 err = -errno;
Ramkumar Ramachandraa33f6ef2014-03-18 15:10:42 -04001113 pr_debug2("sys_perf_event_open failed, error %d\n",
Adrian Hunterf852fd62013-11-01 15:51:29 +02001114 err);
Arnaldo Carvalho de Melo594ac612012-12-13 13:13:07 -03001115 goto try_fallback;
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -02001116 }
Andi Kleenbec19672013-08-04 19:41:26 -07001117 set_rlimit = NO_CHANGE;
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -02001118 }
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -02001119 }
1120
1121 return 0;
1122
Arnaldo Carvalho de Melo594ac612012-12-13 13:13:07 -03001123try_fallback:
Andi Kleenbec19672013-08-04 19:41:26 -07001124 /*
1125 * perf stat needs between 5 and 22 fds per CPU. When we run out
1126 * of them try to increase the limits.
1127 */
1128 if (err == -EMFILE && set_rlimit < INCREASED_MAX) {
1129 struct rlimit l;
1130 int old_errno = errno;
1131
1132 if (getrlimit(RLIMIT_NOFILE, &l) == 0) {
1133 if (set_rlimit == NO_CHANGE)
1134 l.rlim_cur = l.rlim_max;
1135 else {
1136 l.rlim_cur = l.rlim_max + 1000;
1137 l.rlim_max = l.rlim_cur;
1138 }
1139 if (setrlimit(RLIMIT_NOFILE, &l) == 0) {
1140 set_rlimit++;
1141 errno = old_errno;
1142 goto retry_open;
1143 }
1144 }
1145 errno = old_errno;
1146 }
1147
Arnaldo Carvalho de Melo594ac612012-12-13 13:13:07 -03001148 if (err != -EINVAL || cpu > 0 || thread > 0)
1149 goto out_close;
1150
Yann Droneaud57480d22014-06-30 22:28:47 +02001151 if (!perf_missing_features.cloexec && (flags & PERF_FLAG_FD_CLOEXEC)) {
1152 perf_missing_features.cloexec = true;
1153 goto fallback_missing_features;
1154 } else if (!perf_missing_features.mmap2 && evsel->attr.mmap2) {
Stephane Eranian5c5e8542013-08-21 12:10:25 +02001155 perf_missing_features.mmap2 = true;
1156 goto fallback_missing_features;
1157 } else if (!perf_missing_features.exclude_guest &&
1158 (evsel->attr.exclude_guest || evsel->attr.exclude_host)) {
Arnaldo Carvalho de Melo594ac612012-12-13 13:13:07 -03001159 perf_missing_features.exclude_guest = true;
1160 goto fallback_missing_features;
1161 } else if (!perf_missing_features.sample_id_all) {
1162 perf_missing_features.sample_id_all = true;
1163 goto retry_sample_id;
1164 }
1165
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -02001166out_close:
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -02001167 do {
1168 while (--thread >= 0) {
1169 close(FD(evsel, cpu, thread));
1170 FD(evsel, cpu, thread) = -1;
1171 }
Adrian Hunterbf8e8f42014-07-31 09:00:51 +03001172 thread = nthreads;
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -02001173 } while (--cpu >= 0);
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -02001174 return err;
1175}
1176
1177void perf_evsel__close(struct perf_evsel *evsel, int ncpus, int nthreads)
1178{
1179 if (evsel->fd == NULL)
1180 return;
1181
1182 perf_evsel__close_fd(evsel, ncpus, nthreads);
1183 perf_evsel__free_fd(evsel);
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -02001184}
1185
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -02001186static struct {
1187 struct cpu_map map;
1188 int cpus[1];
1189} empty_cpu_map = {
1190 .map.nr = 1,
1191 .cpus = { -1, },
1192};
1193
1194static struct {
1195 struct thread_map map;
1196 int threads[1];
1197} empty_thread_map = {
1198 .map.nr = 1,
1199 .threads = { -1, },
1200};
1201
Arnaldo Carvalho de Melof08199d2011-01-11 23:42:19 -02001202int perf_evsel__open(struct perf_evsel *evsel, struct cpu_map *cpus,
Jiri Olsa6a4bb042012-08-08 12:22:36 +02001203 struct thread_map *threads)
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -02001204{
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -02001205 if (cpus == NULL) {
1206 /* Work around old compiler warnings about strict aliasing */
1207 cpus = &empty_cpu_map.map;
1208 }
1209
1210 if (threads == NULL)
1211 threads = &empty_thread_map.map;
1212
Jiri Olsa6a4bb042012-08-08 12:22:36 +02001213 return __perf_evsel__open(evsel, cpus, threads);
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -02001214}
1215
Arnaldo Carvalho de Melof08199d2011-01-11 23:42:19 -02001216int perf_evsel__open_per_cpu(struct perf_evsel *evsel,
Jiri Olsa6a4bb042012-08-08 12:22:36 +02001217 struct cpu_map *cpus)
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -02001218{
Jiri Olsa6a4bb042012-08-08 12:22:36 +02001219 return __perf_evsel__open(evsel, cpus, &empty_thread_map.map);
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -02001220}
1221
Arnaldo Carvalho de Melof08199d2011-01-11 23:42:19 -02001222int perf_evsel__open_per_thread(struct perf_evsel *evsel,
Jiri Olsa6a4bb042012-08-08 12:22:36 +02001223 struct thread_map *threads)
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -02001224{
Jiri Olsa6a4bb042012-08-08 12:22:36 +02001225 return __perf_evsel__open(evsel, &empty_cpu_map.map, threads);
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -02001226}
Arnaldo Carvalho de Melo70082dd2011-01-12 17:03:24 -02001227
Arnaldo Carvalho de Melo0807d2d2012-09-26 12:48:18 -03001228static int perf_evsel__parse_id_sample(const struct perf_evsel *evsel,
1229 const union perf_event *event,
1230 struct perf_sample *sample)
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001231{
Arnaldo Carvalho de Melo0807d2d2012-09-26 12:48:18 -03001232 u64 type = evsel->attr.sample_type;
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001233 const u64 *array = event->sample.array;
Arnaldo Carvalho de Melo0807d2d2012-09-26 12:48:18 -03001234 bool swapped = evsel->needs_swap;
Jiri Olsa37073f92012-05-30 14:23:44 +02001235 union u64_swap u;
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001236
1237 array += ((event->header.size -
1238 sizeof(event->header)) / sizeof(u64)) - 1;
1239
Adrian Hunter75562572013-08-27 11:23:09 +03001240 if (type & PERF_SAMPLE_IDENTIFIER) {
1241 sample->id = *array;
1242 array--;
1243 }
1244
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001245 if (type & PERF_SAMPLE_CPU) {
Jiri Olsa37073f92012-05-30 14:23:44 +02001246 u.val64 = *array;
1247 if (swapped) {
1248 /* undo swap of u64, then swap on individual u32s */
1249 u.val64 = bswap_64(u.val64);
1250 u.val32[0] = bswap_32(u.val32[0]);
1251 }
1252
1253 sample->cpu = u.val32[0];
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001254 array--;
1255 }
1256
1257 if (type & PERF_SAMPLE_STREAM_ID) {
1258 sample->stream_id = *array;
1259 array--;
1260 }
1261
1262 if (type & PERF_SAMPLE_ID) {
1263 sample->id = *array;
1264 array--;
1265 }
1266
1267 if (type & PERF_SAMPLE_TIME) {
1268 sample->time = *array;
1269 array--;
1270 }
1271
1272 if (type & PERF_SAMPLE_TID) {
Jiri Olsa37073f92012-05-30 14:23:44 +02001273 u.val64 = *array;
1274 if (swapped) {
1275 /* undo swap of u64, then swap on individual u32s */
1276 u.val64 = bswap_64(u.val64);
1277 u.val32[0] = bswap_32(u.val32[0]);
1278 u.val32[1] = bswap_32(u.val32[1]);
1279 }
1280
1281 sample->pid = u.val32[0];
1282 sample->tid = u.val32[1];
Adrian Hunterdd44bc62013-10-18 15:29:01 +03001283 array--;
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001284 }
1285
1286 return 0;
1287}
1288
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001289static inline bool overflow(const void *endp, u16 max_size, const void *offset,
1290 u64 size)
Frederic Weisbecker98e1da92011-05-21 20:08:15 +02001291{
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001292 return size > max_size || offset + size > endp;
Frederic Weisbecker98e1da92011-05-21 20:08:15 +02001293}
1294
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001295#define OVERFLOW_CHECK(offset, size, max_size) \
1296 do { \
1297 if (overflow(endp, (max_size), (offset), (size))) \
1298 return -EFAULT; \
1299 } while (0)
1300
1301#define OVERFLOW_CHECK_u64(offset) \
1302 OVERFLOW_CHECK(offset, sizeof(u64), sizeof(u64))
1303
Arnaldo Carvalho de Meloa3f698f2012-08-02 12:23:46 -03001304int perf_evsel__parse_sample(struct perf_evsel *evsel, union perf_event *event,
Arnaldo Carvalho de Melo0807d2d2012-09-26 12:48:18 -03001305 struct perf_sample *data)
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001306{
Arnaldo Carvalho de Meloa3f698f2012-08-02 12:23:46 -03001307 u64 type = evsel->attr.sample_type;
Arnaldo Carvalho de Melo0807d2d2012-09-26 12:48:18 -03001308 bool swapped = evsel->needs_swap;
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001309 const u64 *array;
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001310 u16 max_size = event->header.size;
1311 const void *endp = (void *)event + max_size;
1312 u64 sz;
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001313
David Ahern936be502011-09-06 09:12:26 -06001314 /*
1315 * used for cross-endian analysis. See git commit 65014ab3
1316 * for why this goofiness is needed.
1317 */
Jiri Olsa6a11f922012-05-16 08:59:04 +02001318 union u64_swap u;
David Ahern936be502011-09-06 09:12:26 -06001319
Robert Richterf3bda2c2011-12-15 17:32:39 +01001320 memset(data, 0, sizeof(*data));
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001321 data->cpu = data->pid = data->tid = -1;
1322 data->stream_id = data->id = data->time = -1ULL;
Jiri Olsabc529082014-02-03 12:44:41 +01001323 data->period = evsel->attr.sample_period;
Andi Kleen05484292013-01-24 16:10:29 +01001324 data->weight = 0;
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001325
1326 if (event->header.type != PERF_RECORD_SAMPLE) {
Arnaldo Carvalho de Meloa3f698f2012-08-02 12:23:46 -03001327 if (!evsel->attr.sample_id_all)
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001328 return 0;
Arnaldo Carvalho de Melo0807d2d2012-09-26 12:48:18 -03001329 return perf_evsel__parse_id_sample(evsel, event, data);
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001330 }
1331
1332 array = event->sample.array;
1333
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001334 /*
1335 * The evsel's sample_size is based on PERF_SAMPLE_MASK which includes
1336 * up to PERF_SAMPLE_PERIOD. After that overflow() must be used to
1337 * check the format does not go past the end of the event.
1338 */
Arnaldo Carvalho de Meloa3f698f2012-08-02 12:23:46 -03001339 if (evsel->sample_size + sizeof(event->header) > event->header.size)
Frederic Weisbeckera2854122011-05-21 19:33:04 +02001340 return -EFAULT;
1341
Adrian Hunter75562572013-08-27 11:23:09 +03001342 data->id = -1ULL;
1343 if (type & PERF_SAMPLE_IDENTIFIER) {
1344 data->id = *array;
1345 array++;
1346 }
1347
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001348 if (type & PERF_SAMPLE_IP) {
Adrian Hunteref893252013-08-27 11:23:06 +03001349 data->ip = *array;
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001350 array++;
1351 }
1352
1353 if (type & PERF_SAMPLE_TID) {
David Ahern936be502011-09-06 09:12:26 -06001354 u.val64 = *array;
1355 if (swapped) {
1356 /* undo swap of u64, then swap on individual u32s */
1357 u.val64 = bswap_64(u.val64);
1358 u.val32[0] = bswap_32(u.val32[0]);
1359 u.val32[1] = bswap_32(u.val32[1]);
1360 }
1361
1362 data->pid = u.val32[0];
1363 data->tid = u.val32[1];
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001364 array++;
1365 }
1366
1367 if (type & PERF_SAMPLE_TIME) {
1368 data->time = *array;
1369 array++;
1370 }
1371
David Ahern7cec0922011-05-30 13:08:23 -06001372 data->addr = 0;
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001373 if (type & PERF_SAMPLE_ADDR) {
1374 data->addr = *array;
1375 array++;
1376 }
1377
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001378 if (type & PERF_SAMPLE_ID) {
1379 data->id = *array;
1380 array++;
1381 }
1382
1383 if (type & PERF_SAMPLE_STREAM_ID) {
1384 data->stream_id = *array;
1385 array++;
1386 }
1387
1388 if (type & PERF_SAMPLE_CPU) {
David Ahern936be502011-09-06 09:12:26 -06001389
1390 u.val64 = *array;
1391 if (swapped) {
1392 /* undo swap of u64, then swap on individual u32s */
1393 u.val64 = bswap_64(u.val64);
1394 u.val32[0] = bswap_32(u.val32[0]);
1395 }
1396
1397 data->cpu = u.val32[0];
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001398 array++;
1399 }
1400
1401 if (type & PERF_SAMPLE_PERIOD) {
1402 data->period = *array;
1403 array++;
1404 }
1405
1406 if (type & PERF_SAMPLE_READ) {
Jiri Olsa9ede4732012-10-10 17:38:13 +02001407 u64 read_format = evsel->attr.read_format;
1408
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001409 OVERFLOW_CHECK_u64(array);
Jiri Olsa9ede4732012-10-10 17:38:13 +02001410 if (read_format & PERF_FORMAT_GROUP)
1411 data->read.group.nr = *array;
1412 else
1413 data->read.one.value = *array;
1414
1415 array++;
1416
1417 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) {
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001418 OVERFLOW_CHECK_u64(array);
Jiri Olsa9ede4732012-10-10 17:38:13 +02001419 data->read.time_enabled = *array;
1420 array++;
1421 }
1422
1423 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) {
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001424 OVERFLOW_CHECK_u64(array);
Jiri Olsa9ede4732012-10-10 17:38:13 +02001425 data->read.time_running = *array;
1426 array++;
1427 }
1428
1429 /* PERF_FORMAT_ID is forced for PERF_SAMPLE_READ */
1430 if (read_format & PERF_FORMAT_GROUP) {
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001431 const u64 max_group_nr = UINT64_MAX /
1432 sizeof(struct sample_read_value);
1433
1434 if (data->read.group.nr > max_group_nr)
1435 return -EFAULT;
1436 sz = data->read.group.nr *
1437 sizeof(struct sample_read_value);
1438 OVERFLOW_CHECK(array, sz, max_size);
1439 data->read.group.values =
1440 (struct sample_read_value *)array;
1441 array = (void *)array + sz;
Jiri Olsa9ede4732012-10-10 17:38:13 +02001442 } else {
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001443 OVERFLOW_CHECK_u64(array);
Jiri Olsa9ede4732012-10-10 17:38:13 +02001444 data->read.one.id = *array;
1445 array++;
1446 }
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001447 }
1448
1449 if (type & PERF_SAMPLE_CALLCHAIN) {
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001450 const u64 max_callchain_nr = UINT64_MAX / sizeof(u64);
1451
1452 OVERFLOW_CHECK_u64(array);
1453 data->callchain = (struct ip_callchain *)array++;
1454 if (data->callchain->nr > max_callchain_nr)
Frederic Weisbecker98e1da92011-05-21 20:08:15 +02001455 return -EFAULT;
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001456 sz = data->callchain->nr * sizeof(u64);
1457 OVERFLOW_CHECK(array, sz, max_size);
1458 array = (void *)array + sz;
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001459 }
1460
1461 if (type & PERF_SAMPLE_RAW) {
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001462 OVERFLOW_CHECK_u64(array);
David Ahern936be502011-09-06 09:12:26 -06001463 u.val64 = *array;
1464 if (WARN_ONCE(swapped,
1465 "Endianness of raw data not corrected!\n")) {
1466 /* undo swap of u64, then swap on individual u32s */
1467 u.val64 = bswap_64(u.val64);
1468 u.val32[0] = bswap_32(u.val32[0]);
1469 u.val32[1] = bswap_32(u.val32[1]);
1470 }
David Ahern936be502011-09-06 09:12:26 -06001471 data->raw_size = u.val32[0];
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001472 array = (void *)array + sizeof(u32);
Frederic Weisbecker98e1da92011-05-21 20:08:15 +02001473
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001474 OVERFLOW_CHECK(array, data->raw_size, max_size);
1475 data->raw_data = (void *)array;
1476 array = (void *)array + data->raw_size;
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001477 }
1478
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +01001479 if (type & PERF_SAMPLE_BRANCH_STACK) {
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001480 const u64 max_branch_nr = UINT64_MAX /
1481 sizeof(struct branch_entry);
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +01001482
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001483 OVERFLOW_CHECK_u64(array);
1484 data->branch_stack = (struct branch_stack *)array++;
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +01001485
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001486 if (data->branch_stack->nr > max_branch_nr)
1487 return -EFAULT;
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +01001488 sz = data->branch_stack->nr * sizeof(struct branch_entry);
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001489 OVERFLOW_CHECK(array, sz, max_size);
1490 array = (void *)array + sz;
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +01001491 }
Jiri Olsa0f6a3012012-08-07 15:20:45 +02001492
1493 if (type & PERF_SAMPLE_REGS_USER) {
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001494 OVERFLOW_CHECK_u64(array);
Adrian Hunter5b95a4a32013-08-27 11:23:10 +03001495 data->user_regs.abi = *array;
1496 array++;
Jiri Olsa0f6a3012012-08-07 15:20:45 +02001497
Adrian Hunter5b95a4a32013-08-27 11:23:10 +03001498 if (data->user_regs.abi) {
Jiri Olsa352ea452014-01-07 13:47:25 +01001499 u64 mask = evsel->attr.sample_regs_user;
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001500
Jiri Olsa352ea452014-01-07 13:47:25 +01001501 sz = hweight_long(mask) * sizeof(u64);
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001502 OVERFLOW_CHECK(array, sz, max_size);
Jiri Olsa352ea452014-01-07 13:47:25 +01001503 data->user_regs.mask = mask;
Jiri Olsa0f6a3012012-08-07 15:20:45 +02001504 data->user_regs.regs = (u64 *)array;
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001505 array = (void *)array + sz;
Jiri Olsa0f6a3012012-08-07 15:20:45 +02001506 }
1507 }
1508
1509 if (type & PERF_SAMPLE_STACK_USER) {
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001510 OVERFLOW_CHECK_u64(array);
1511 sz = *array++;
Jiri Olsa0f6a3012012-08-07 15:20:45 +02001512
1513 data->user_stack.offset = ((char *)(array - 1)
1514 - (char *) event);
1515
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001516 if (!sz) {
Jiri Olsa0f6a3012012-08-07 15:20:45 +02001517 data->user_stack.size = 0;
1518 } else {
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001519 OVERFLOW_CHECK(array, sz, max_size);
Jiri Olsa0f6a3012012-08-07 15:20:45 +02001520 data->user_stack.data = (char *)array;
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001521 array = (void *)array + sz;
1522 OVERFLOW_CHECK_u64(array);
Adrian Hunter54bd2692013-07-04 16:20:34 +03001523 data->user_stack.size = *array++;
Jiri Olsaa65cb4b2013-10-02 15:46:39 +02001524 if (WARN_ONCE(data->user_stack.size > sz,
1525 "user stack dump failure\n"))
1526 return -EFAULT;
Jiri Olsa0f6a3012012-08-07 15:20:45 +02001527 }
1528 }
1529
Andi Kleen05484292013-01-24 16:10:29 +01001530 data->weight = 0;
1531 if (type & PERF_SAMPLE_WEIGHT) {
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001532 OVERFLOW_CHECK_u64(array);
Andi Kleen05484292013-01-24 16:10:29 +01001533 data->weight = *array;
1534 array++;
1535 }
1536
Stephane Eranian98a3b322013-01-24 16:10:35 +01001537 data->data_src = PERF_MEM_DATA_SRC_NONE;
1538 if (type & PERF_SAMPLE_DATA_SRC) {
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001539 OVERFLOW_CHECK_u64(array);
Stephane Eranian98a3b322013-01-24 16:10:35 +01001540 data->data_src = *array;
1541 array++;
1542 }
1543
Andi Kleen475eeab2013-09-20 07:40:43 -07001544 data->transaction = 0;
1545 if (type & PERF_SAMPLE_TRANSACTION) {
Adrian Hunter87b95522013-11-01 15:51:36 +02001546 OVERFLOW_CHECK_u64(array);
Andi Kleen475eeab2013-09-20 07:40:43 -07001547 data->transaction = *array;
1548 array++;
1549 }
1550
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001551 return 0;
1552}
Andrew Vagin74eec262011-11-28 12:03:31 +03001553
Adrian Hunterb1cf6f62013-08-27 11:23:12 +03001554size_t perf_event__sample_event_size(const struct perf_sample *sample, u64 type,
Jiri Olsa352ea452014-01-07 13:47:25 +01001555 u64 read_format)
Adrian Hunterb1cf6f62013-08-27 11:23:12 +03001556{
1557 size_t sz, result = sizeof(struct sample_event);
1558
1559 if (type & PERF_SAMPLE_IDENTIFIER)
1560 result += sizeof(u64);
1561
1562 if (type & PERF_SAMPLE_IP)
1563 result += sizeof(u64);
1564
1565 if (type & PERF_SAMPLE_TID)
1566 result += sizeof(u64);
1567
1568 if (type & PERF_SAMPLE_TIME)
1569 result += sizeof(u64);
1570
1571 if (type & PERF_SAMPLE_ADDR)
1572 result += sizeof(u64);
1573
1574 if (type & PERF_SAMPLE_ID)
1575 result += sizeof(u64);
1576
1577 if (type & PERF_SAMPLE_STREAM_ID)
1578 result += sizeof(u64);
1579
1580 if (type & PERF_SAMPLE_CPU)
1581 result += sizeof(u64);
1582
1583 if (type & PERF_SAMPLE_PERIOD)
1584 result += sizeof(u64);
1585
1586 if (type & PERF_SAMPLE_READ) {
1587 result += sizeof(u64);
1588 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
1589 result += sizeof(u64);
1590 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
1591 result += sizeof(u64);
1592 /* PERF_FORMAT_ID is forced for PERF_SAMPLE_READ */
1593 if (read_format & PERF_FORMAT_GROUP) {
1594 sz = sample->read.group.nr *
1595 sizeof(struct sample_read_value);
1596 result += sz;
1597 } else {
1598 result += sizeof(u64);
1599 }
1600 }
1601
1602 if (type & PERF_SAMPLE_CALLCHAIN) {
1603 sz = (sample->callchain->nr + 1) * sizeof(u64);
1604 result += sz;
1605 }
1606
1607 if (type & PERF_SAMPLE_RAW) {
1608 result += sizeof(u32);
1609 result += sample->raw_size;
1610 }
1611
1612 if (type & PERF_SAMPLE_BRANCH_STACK) {
1613 sz = sample->branch_stack->nr * sizeof(struct branch_entry);
1614 sz += sizeof(u64);
1615 result += sz;
1616 }
1617
1618 if (type & PERF_SAMPLE_REGS_USER) {
1619 if (sample->user_regs.abi) {
1620 result += sizeof(u64);
Jiri Olsa352ea452014-01-07 13:47:25 +01001621 sz = hweight_long(sample->user_regs.mask) * sizeof(u64);
Adrian Hunterb1cf6f62013-08-27 11:23:12 +03001622 result += sz;
1623 } else {
1624 result += sizeof(u64);
1625 }
1626 }
1627
1628 if (type & PERF_SAMPLE_STACK_USER) {
1629 sz = sample->user_stack.size;
1630 result += sizeof(u64);
1631 if (sz) {
1632 result += sz;
1633 result += sizeof(u64);
1634 }
1635 }
1636
1637 if (type & PERF_SAMPLE_WEIGHT)
1638 result += sizeof(u64);
1639
1640 if (type & PERF_SAMPLE_DATA_SRC)
1641 result += sizeof(u64);
1642
Adrian Hunter42d88912013-11-01 15:51:38 +02001643 if (type & PERF_SAMPLE_TRANSACTION)
1644 result += sizeof(u64);
1645
Adrian Hunterb1cf6f62013-08-27 11:23:12 +03001646 return result;
1647}
1648
Andrew Vagin74eec262011-11-28 12:03:31 +03001649int perf_event__synthesize_sample(union perf_event *event, u64 type,
Jiri Olsa352ea452014-01-07 13:47:25 +01001650 u64 read_format,
Andrew Vagin74eec262011-11-28 12:03:31 +03001651 const struct perf_sample *sample,
1652 bool swapped)
1653{
1654 u64 *array;
Adrian Hunterd03f2172013-08-27 11:23:11 +03001655 size_t sz;
Andrew Vagin74eec262011-11-28 12:03:31 +03001656 /*
1657 * used for cross-endian analysis. See git commit 65014ab3
1658 * for why this goofiness is needed.
1659 */
Jiri Olsa6a11f922012-05-16 08:59:04 +02001660 union u64_swap u;
Andrew Vagin74eec262011-11-28 12:03:31 +03001661
1662 array = event->sample.array;
1663
Adrian Hunter75562572013-08-27 11:23:09 +03001664 if (type & PERF_SAMPLE_IDENTIFIER) {
1665 *array = sample->id;
1666 array++;
1667 }
1668
Andrew Vagin74eec262011-11-28 12:03:31 +03001669 if (type & PERF_SAMPLE_IP) {
Adrian Hunteref893252013-08-27 11:23:06 +03001670 *array = sample->ip;
Andrew Vagin74eec262011-11-28 12:03:31 +03001671 array++;
1672 }
1673
1674 if (type & PERF_SAMPLE_TID) {
1675 u.val32[0] = sample->pid;
1676 u.val32[1] = sample->tid;
1677 if (swapped) {
1678 /*
Arnaldo Carvalho de Meloa3f698f2012-08-02 12:23:46 -03001679 * Inverse of what is done in perf_evsel__parse_sample
Andrew Vagin74eec262011-11-28 12:03:31 +03001680 */
1681 u.val32[0] = bswap_32(u.val32[0]);
1682 u.val32[1] = bswap_32(u.val32[1]);
1683 u.val64 = bswap_64(u.val64);
1684 }
1685
1686 *array = u.val64;
1687 array++;
1688 }
1689
1690 if (type & PERF_SAMPLE_TIME) {
1691 *array = sample->time;
1692 array++;
1693 }
1694
1695 if (type & PERF_SAMPLE_ADDR) {
1696 *array = sample->addr;
1697 array++;
1698 }
1699
1700 if (type & PERF_SAMPLE_ID) {
1701 *array = sample->id;
1702 array++;
1703 }
1704
1705 if (type & PERF_SAMPLE_STREAM_ID) {
1706 *array = sample->stream_id;
1707 array++;
1708 }
1709
1710 if (type & PERF_SAMPLE_CPU) {
1711 u.val32[0] = sample->cpu;
1712 if (swapped) {
1713 /*
Arnaldo Carvalho de Meloa3f698f2012-08-02 12:23:46 -03001714 * Inverse of what is done in perf_evsel__parse_sample
Andrew Vagin74eec262011-11-28 12:03:31 +03001715 */
1716 u.val32[0] = bswap_32(u.val32[0]);
1717 u.val64 = bswap_64(u.val64);
1718 }
1719 *array = u.val64;
1720 array++;
1721 }
1722
1723 if (type & PERF_SAMPLE_PERIOD) {
1724 *array = sample->period;
1725 array++;
1726 }
1727
Adrian Hunterd03f2172013-08-27 11:23:11 +03001728 if (type & PERF_SAMPLE_READ) {
1729 if (read_format & PERF_FORMAT_GROUP)
1730 *array = sample->read.group.nr;
1731 else
1732 *array = sample->read.one.value;
1733 array++;
1734
1735 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) {
1736 *array = sample->read.time_enabled;
1737 array++;
1738 }
1739
1740 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) {
1741 *array = sample->read.time_running;
1742 array++;
1743 }
1744
1745 /* PERF_FORMAT_ID is forced for PERF_SAMPLE_READ */
1746 if (read_format & PERF_FORMAT_GROUP) {
1747 sz = sample->read.group.nr *
1748 sizeof(struct sample_read_value);
1749 memcpy(array, sample->read.group.values, sz);
1750 array = (void *)array + sz;
1751 } else {
1752 *array = sample->read.one.id;
1753 array++;
1754 }
1755 }
1756
1757 if (type & PERF_SAMPLE_CALLCHAIN) {
1758 sz = (sample->callchain->nr + 1) * sizeof(u64);
1759 memcpy(array, sample->callchain, sz);
1760 array = (void *)array + sz;
1761 }
1762
1763 if (type & PERF_SAMPLE_RAW) {
1764 u.val32[0] = sample->raw_size;
1765 if (WARN_ONCE(swapped,
1766 "Endianness of raw data not corrected!\n")) {
1767 /*
1768 * Inverse of what is done in perf_evsel__parse_sample
1769 */
1770 u.val32[0] = bswap_32(u.val32[0]);
1771 u.val32[1] = bswap_32(u.val32[1]);
1772 u.val64 = bswap_64(u.val64);
1773 }
1774 *array = u.val64;
1775 array = (void *)array + sizeof(u32);
1776
1777 memcpy(array, sample->raw_data, sample->raw_size);
1778 array = (void *)array + sample->raw_size;
1779 }
1780
1781 if (type & PERF_SAMPLE_BRANCH_STACK) {
1782 sz = sample->branch_stack->nr * sizeof(struct branch_entry);
1783 sz += sizeof(u64);
1784 memcpy(array, sample->branch_stack, sz);
1785 array = (void *)array + sz;
1786 }
1787
1788 if (type & PERF_SAMPLE_REGS_USER) {
1789 if (sample->user_regs.abi) {
1790 *array++ = sample->user_regs.abi;
Jiri Olsa352ea452014-01-07 13:47:25 +01001791 sz = hweight_long(sample->user_regs.mask) * sizeof(u64);
Adrian Hunterd03f2172013-08-27 11:23:11 +03001792 memcpy(array, sample->user_regs.regs, sz);
1793 array = (void *)array + sz;
1794 } else {
1795 *array++ = 0;
1796 }
1797 }
1798
1799 if (type & PERF_SAMPLE_STACK_USER) {
1800 sz = sample->user_stack.size;
1801 *array++ = sz;
1802 if (sz) {
1803 memcpy(array, sample->user_stack.data, sz);
1804 array = (void *)array + sz;
1805 *array++ = sz;
1806 }
1807 }
1808
1809 if (type & PERF_SAMPLE_WEIGHT) {
1810 *array = sample->weight;
1811 array++;
1812 }
1813
1814 if (type & PERF_SAMPLE_DATA_SRC) {
1815 *array = sample->data_src;
1816 array++;
1817 }
1818
Adrian Hunter42d88912013-11-01 15:51:38 +02001819 if (type & PERF_SAMPLE_TRANSACTION) {
1820 *array = sample->transaction;
1821 array++;
1822 }
1823
Andrew Vagin74eec262011-11-28 12:03:31 +03001824 return 0;
1825}
Arnaldo Carvalho de Melo5555ded2012-09-11 19:24:23 -03001826
Arnaldo Carvalho de Meloefd2b922012-09-18 11:21:50 -03001827struct format_field *perf_evsel__field(struct perf_evsel *evsel, const char *name)
1828{
1829 return pevent_find_field(evsel->tp_format, name);
1830}
1831
Arnaldo Carvalho de Melo5d2074e2012-09-26 20:22:00 -03001832void *perf_evsel__rawptr(struct perf_evsel *evsel, struct perf_sample *sample,
Arnaldo Carvalho de Melo5555ded2012-09-11 19:24:23 -03001833 const char *name)
1834{
Arnaldo Carvalho de Meloefd2b922012-09-18 11:21:50 -03001835 struct format_field *field = perf_evsel__field(evsel, name);
Arnaldo Carvalho de Melo5555ded2012-09-11 19:24:23 -03001836 int offset;
1837
Arnaldo Carvalho de Meloefd2b922012-09-18 11:21:50 -03001838 if (!field)
1839 return NULL;
Arnaldo Carvalho de Melo5555ded2012-09-11 19:24:23 -03001840
1841 offset = field->offset;
1842
1843 if (field->flags & FIELD_IS_DYNAMIC) {
1844 offset = *(int *)(sample->raw_data + field->offset);
1845 offset &= 0xffff;
1846 }
1847
1848 return sample->raw_data + offset;
1849}
1850
1851u64 perf_evsel__intval(struct perf_evsel *evsel, struct perf_sample *sample,
1852 const char *name)
1853{
Arnaldo Carvalho de Meloefd2b922012-09-18 11:21:50 -03001854 struct format_field *field = perf_evsel__field(evsel, name);
Arnaldo Carvalho de Meloe6b6f672012-09-26 13:13:04 -03001855 void *ptr;
1856 u64 value;
Arnaldo Carvalho de Melo5555ded2012-09-11 19:24:23 -03001857
Arnaldo Carvalho de Meloefd2b922012-09-18 11:21:50 -03001858 if (!field)
1859 return 0;
Arnaldo Carvalho de Melo5555ded2012-09-11 19:24:23 -03001860
Arnaldo Carvalho de Meloe6b6f672012-09-26 13:13:04 -03001861 ptr = sample->raw_data + field->offset;
Arnaldo Carvalho de Melo5555ded2012-09-11 19:24:23 -03001862
Arnaldo Carvalho de Meloe6b6f672012-09-26 13:13:04 -03001863 switch (field->size) {
1864 case 1:
1865 return *(u8 *)ptr;
1866 case 2:
1867 value = *(u16 *)ptr;
1868 break;
1869 case 4:
1870 value = *(u32 *)ptr;
1871 break;
1872 case 8:
1873 value = *(u64 *)ptr;
1874 break;
1875 default:
1876 return 0;
1877 }
1878
1879 if (!evsel->needs_swap)
1880 return value;
1881
1882 switch (field->size) {
1883 case 2:
1884 return bswap_16(value);
1885 case 4:
1886 return bswap_32(value);
1887 case 8:
1888 return bswap_64(value);
1889 default:
1890 return 0;
1891 }
1892
1893 return 0;
Arnaldo Carvalho de Melo5555ded2012-09-11 19:24:23 -03001894}
Arnaldo Carvalho de Melo0698aed2012-12-10 18:17:08 -03001895
1896static int comma_fprintf(FILE *fp, bool *first, const char *fmt, ...)
1897{
1898 va_list args;
1899 int ret = 0;
1900
1901 if (!*first) {
1902 ret += fprintf(fp, ",");
1903 } else {
1904 ret += fprintf(fp, ":");
1905 *first = false;
1906 }
1907
1908 va_start(args, fmt);
1909 ret += vfprintf(fp, fmt, args);
1910 va_end(args);
1911 return ret;
1912}
1913
1914static int __if_fprintf(FILE *fp, bool *first, const char *field, u64 value)
1915{
1916 if (value == 0)
1917 return 0;
1918
1919 return comma_fprintf(fp, first, " %s: %" PRIu64, field, value);
1920}
1921
1922#define if_print(field) printed += __if_fprintf(fp, &first, #field, evsel->attr.field)
1923
Arnaldo Carvalho de Meloc79a4392012-12-11 10:54:12 -03001924struct bit_names {
1925 int bit;
1926 const char *name;
1927};
1928
1929static int bits__fprintf(FILE *fp, const char *field, u64 value,
1930 struct bit_names *bits, bool *first)
1931{
1932 int i = 0, printed = comma_fprintf(fp, first, " %s: ", field);
1933 bool first_bit = true;
1934
1935 do {
1936 if (value & bits[i].bit) {
1937 printed += fprintf(fp, "%s%s", first_bit ? "" : "|", bits[i].name);
1938 first_bit = false;
1939 }
1940 } while (bits[++i].name != NULL);
1941
1942 return printed;
1943}
1944
1945static int sample_type__fprintf(FILE *fp, bool *first, u64 value)
1946{
1947#define bit_name(n) { PERF_SAMPLE_##n, #n }
1948 struct bit_names bits[] = {
1949 bit_name(IP), bit_name(TID), bit_name(TIME), bit_name(ADDR),
1950 bit_name(READ), bit_name(CALLCHAIN), bit_name(ID), bit_name(CPU),
1951 bit_name(PERIOD), bit_name(STREAM_ID), bit_name(RAW),
1952 bit_name(BRANCH_STACK), bit_name(REGS_USER), bit_name(STACK_USER),
Adrian Hunter75562572013-08-27 11:23:09 +03001953 bit_name(IDENTIFIER),
Arnaldo Carvalho de Meloc79a4392012-12-11 10:54:12 -03001954 { .name = NULL, }
1955 };
1956#undef bit_name
1957 return bits__fprintf(fp, "sample_type", value, bits, first);
1958}
1959
1960static int read_format__fprintf(FILE *fp, bool *first, u64 value)
1961{
1962#define bit_name(n) { PERF_FORMAT_##n, #n }
1963 struct bit_names bits[] = {
1964 bit_name(TOTAL_TIME_ENABLED), bit_name(TOTAL_TIME_RUNNING),
1965 bit_name(ID), bit_name(GROUP),
1966 { .name = NULL, }
1967 };
1968#undef bit_name
1969 return bits__fprintf(fp, "read_format", value, bits, first);
1970}
1971
Arnaldo Carvalho de Melo0698aed2012-12-10 18:17:08 -03001972int perf_evsel__fprintf(struct perf_evsel *evsel,
1973 struct perf_attr_details *details, FILE *fp)
1974{
1975 bool first = true;
Namhyung Kime6ab07d2013-01-22 18:09:47 +09001976 int printed = 0;
1977
Arnaldo Carvalho de Meloe35ef3552013-02-06 17:20:02 -03001978 if (details->event_group) {
Namhyung Kime6ab07d2013-01-22 18:09:47 +09001979 struct perf_evsel *pos;
1980
1981 if (!perf_evsel__is_group_leader(evsel))
1982 return 0;
1983
1984 if (evsel->nr_members > 1)
1985 printed += fprintf(fp, "%s{", evsel->group_name ?: "");
1986
1987 printed += fprintf(fp, "%s", perf_evsel__name(evsel));
1988 for_each_group_member(pos, evsel)
1989 printed += fprintf(fp, ",%s", perf_evsel__name(pos));
1990
1991 if (evsel->nr_members > 1)
1992 printed += fprintf(fp, "}");
1993 goto out;
1994 }
1995
1996 printed += fprintf(fp, "%s", perf_evsel__name(evsel));
Arnaldo Carvalho de Melo0698aed2012-12-10 18:17:08 -03001997
1998 if (details->verbose || details->freq) {
1999 printed += comma_fprintf(fp, &first, " sample_freq=%" PRIu64,
2000 (u64)evsel->attr.sample_freq);
2001 }
2002
2003 if (details->verbose) {
2004 if_print(type);
2005 if_print(config);
2006 if_print(config1);
2007 if_print(config2);
2008 if_print(size);
Arnaldo Carvalho de Meloc79a4392012-12-11 10:54:12 -03002009 printed += sample_type__fprintf(fp, &first, evsel->attr.sample_type);
2010 if (evsel->attr.read_format)
2011 printed += read_format__fprintf(fp, &first, evsel->attr.read_format);
Arnaldo Carvalho de Melo0698aed2012-12-10 18:17:08 -03002012 if_print(disabled);
2013 if_print(inherit);
2014 if_print(pinned);
2015 if_print(exclusive);
2016 if_print(exclude_user);
2017 if_print(exclude_kernel);
2018 if_print(exclude_hv);
2019 if_print(exclude_idle);
2020 if_print(mmap);
Stephane Eranian5c5e8542013-08-21 12:10:25 +02002021 if_print(mmap2);
Arnaldo Carvalho de Melo0698aed2012-12-10 18:17:08 -03002022 if_print(comm);
Adrian Hunter022c50d2014-07-14 13:02:27 +03002023 if_print(comm_exec);
Arnaldo Carvalho de Melo0698aed2012-12-10 18:17:08 -03002024 if_print(freq);
2025 if_print(inherit_stat);
2026 if_print(enable_on_exec);
2027 if_print(task);
2028 if_print(watermark);
2029 if_print(precise_ip);
2030 if_print(mmap_data);
2031 if_print(sample_id_all);
2032 if_print(exclude_host);
2033 if_print(exclude_guest);
2034 if_print(__reserved_1);
2035 if_print(wakeup_events);
2036 if_print(bp_type);
2037 if_print(branch_sample_type);
2038 }
Namhyung Kime6ab07d2013-01-22 18:09:47 +09002039out:
Arnaldo Carvalho de Melo0698aed2012-12-10 18:17:08 -03002040 fputc('\n', fp);
2041 return ++printed;
2042}
Arnaldo Carvalho de Meloc0a54342012-12-13 14:16:30 -03002043
2044bool perf_evsel__fallback(struct perf_evsel *evsel, int err,
2045 char *msg, size_t msgsize)
2046{
David Ahern2b821cc2013-07-18 17:27:59 -06002047 if ((err == ENOENT || err == ENXIO || err == ENODEV) &&
Arnaldo Carvalho de Meloc0a54342012-12-13 14:16:30 -03002048 evsel->attr.type == PERF_TYPE_HARDWARE &&
2049 evsel->attr.config == PERF_COUNT_HW_CPU_CYCLES) {
2050 /*
2051 * If it's cycles then fall back to hrtimer based
2052 * cpu-clock-tick sw counter, which is always available even if
2053 * no PMU support.
2054 *
2055 * PPC returns ENXIO until 2.6.37 (behavior changed with commit
2056 * b0a873e).
2057 */
2058 scnprintf(msg, msgsize, "%s",
2059"The cycles event is not supported, trying to fall back to cpu-clock-ticks");
2060
2061 evsel->attr.type = PERF_TYPE_SOFTWARE;
2062 evsel->attr.config = PERF_COUNT_SW_CPU_CLOCK;
2063
Arnaldo Carvalho de Melo04662522013-12-26 17:41:15 -03002064 zfree(&evsel->name);
Arnaldo Carvalho de Meloc0a54342012-12-13 14:16:30 -03002065 return true;
2066 }
2067
2068 return false;
2069}
Arnaldo Carvalho de Melo56e52e82012-12-13 15:10:58 -03002070
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03002071int perf_evsel__open_strerror(struct perf_evsel *evsel, struct target *target,
Arnaldo Carvalho de Melo56e52e82012-12-13 15:10:58 -03002072 int err, char *msg, size_t size)
2073{
Masami Hiramatsu6e81c742014-08-14 02:22:36 +00002074 char sbuf[STRERR_BUFSIZE];
2075
Arnaldo Carvalho de Melo56e52e82012-12-13 15:10:58 -03002076 switch (err) {
2077 case EPERM:
2078 case EACCES:
David Ahernb69e63a2013-05-25 17:54:00 -06002079 return scnprintf(msg, size,
Arnaldo Carvalho de Melo56e52e82012-12-13 15:10:58 -03002080 "You may not have permission to collect %sstats.\n"
2081 "Consider tweaking /proc/sys/kernel/perf_event_paranoid:\n"
2082 " -1 - Not paranoid at all\n"
2083 " 0 - Disallow raw tracepoint access for unpriv\n"
2084 " 1 - Disallow cpu events for unpriv\n"
2085 " 2 - Disallow kernel profiling for unpriv",
2086 target->system_wide ? "system-wide " : "");
2087 case ENOENT:
2088 return scnprintf(msg, size, "The %s event is not supported.",
2089 perf_evsel__name(evsel));
2090 case EMFILE:
2091 return scnprintf(msg, size, "%s",
2092 "Too many events are opened.\n"
2093 "Try again after reducing the number of events.");
2094 case ENODEV:
2095 if (target->cpu_list)
2096 return scnprintf(msg, size, "%s",
2097 "No such device - did you specify an out-of-range profile CPU?\n");
2098 break;
2099 case EOPNOTSUPP:
2100 if (evsel->attr.precise_ip)
2101 return scnprintf(msg, size, "%s",
2102 "\'precise\' request may not be supported. Try removing 'p' modifier.");
2103#if defined(__i386__) || defined(__x86_64__)
2104 if (evsel->attr.type == PERF_TYPE_HARDWARE)
2105 return scnprintf(msg, size, "%s",
2106 "No hardware sampling interrupt available.\n"
2107 "No APIC? If so then you can boot the kernel with the \"lapic\" boot parameter to force-enable it.");
2108#endif
2109 break;
Jiri Olsa63914ac2014-08-01 17:46:54 +02002110 case EBUSY:
2111 if (find_process("oprofiled"))
2112 return scnprintf(msg, size,
2113 "The PMU counters are busy/taken by another profiler.\n"
2114 "We found oprofile daemon running, please stop it and try again.");
2115 break;
Arnaldo Carvalho de Melo56e52e82012-12-13 15:10:58 -03002116 default:
2117 break;
2118 }
2119
2120 return scnprintf(msg, size,
Masami Hiramatsu6e81c742014-08-14 02:22:36 +00002121 "The sys_perf_event_open() syscall returned with %d (%s) for event (%s).\n"
Arnaldo Carvalho de Melo56e52e82012-12-13 15:10:58 -03002122 "/bin/dmesg may provide additional information.\n"
2123 "No CONFIG_PERF_EVENTS=y kernel support configured?\n",
Masami Hiramatsu6e81c742014-08-14 02:22:36 +00002124 err, strerror_r(err, sbuf, sizeof(sbuf)),
2125 perf_evsel__name(evsel));
Arnaldo Carvalho de Melo56e52e82012-12-13 15:10:58 -03002126}