blob: c886b9f7a48d01b31c0eb541961faa4c02df3179 [file] [log] [blame]
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -02001/*
2 * Copyright (C) 2011, Red Hat Inc, Arnaldo Carvalho de Melo <acme@redhat.com>
3 *
4 * Parts came from builtin-{top,stat,record}.c, see those files for further
5 * copyright notes.
6 *
7 * Released under the GPL v2. (and only v2, not any later version)
8 */
9
David Ahern936be502011-09-06 09:12:26 -060010#include <byteswap.h>
Jiri Olsa0f6a3012012-08-07 15:20:45 +020011#include <linux/bitops.h>
Borislav Petkov553873e2013-12-09 17:14:23 +010012#include <api/fs/debugfs.h>
Robert Richter4e319022013-06-11 17:29:18 +020013#include <traceevent/event-parse.h>
14#include <linux/hw_breakpoint.h>
15#include <linux/perf_event.h>
Andi Kleenbec19672013-08-04 19:41:26 -070016#include <sys/resource.h>
Robert Richter4e319022013-06-11 17:29:18 +020017#include "asm/bug.h"
Arnaldo Carvalho de Melo8f651ea2014-10-09 16:12:24 -030018#include "callchain.h"
Arnaldo Carvalho de Melof14d5702014-10-17 12:17:40 -030019#include "cgroup.h"
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -020020#include "evsel.h"
Arnaldo Carvalho de Melo70082dd2011-01-12 17:03:24 -020021#include "evlist.h"
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -020022#include "util.h"
Arnaldo Carvalho de Melo86bd5e82011-01-03 23:09:46 -020023#include "cpumap.h"
Arnaldo Carvalho de Melofd782602011-01-18 15:15:24 -020024#include "thread_map.h"
Namhyung Kim12864b32012-04-26 14:15:22 +090025#include "target.h"
Jiri Olsa26d33022012-08-07 15:20:47 +020026#include "perf_regs.h"
Adrian Huntere3e1a542013-08-14 15:48:24 +030027#include "debug.h"
Jiri Olsa97978b32013-12-03 14:09:24 +010028#include "trace-event.h"
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -020029
Arnaldo Carvalho de Melo594ac612012-12-13 13:13:07 -030030static struct {
31 bool sample_id_all;
32 bool exclude_guest;
Stephane Eranian5c5e8542013-08-21 12:10:25 +020033 bool mmap2;
Yann Droneaud57480d22014-06-30 22:28:47 +020034 bool cloexec;
Peter Zijlstra814c8c32015-03-31 00:19:31 +020035 bool clockid;
36 bool clockid_wrong;
Arnaldo Carvalho de Melo594ac612012-12-13 13:13:07 -030037} perf_missing_features;
38
Peter Zijlstra814c8c32015-03-31 00:19:31 +020039static clockid_t clockid;
40
Arnaldo Carvalho de Meloce8ccff2014-10-09 15:29:51 -030041static int perf_evsel__no_extra_init(struct perf_evsel *evsel __maybe_unused)
42{
43 return 0;
44}
45
46static void perf_evsel__no_extra_fini(struct perf_evsel *evsel __maybe_unused)
47{
48}
49
50static struct {
51 size_t size;
52 int (*init)(struct perf_evsel *evsel);
53 void (*fini)(struct perf_evsel *evsel);
54} perf_evsel__object = {
55 .size = sizeof(struct perf_evsel),
56 .init = perf_evsel__no_extra_init,
57 .fini = perf_evsel__no_extra_fini,
58};
59
60int perf_evsel__object_config(size_t object_size,
61 int (*init)(struct perf_evsel *evsel),
62 void (*fini)(struct perf_evsel *evsel))
63{
64
65 if (object_size == 0)
66 goto set_methods;
67
68 if (perf_evsel__object.size > object_size)
69 return -EINVAL;
70
71 perf_evsel__object.size = object_size;
72
73set_methods:
74 if (init != NULL)
75 perf_evsel__object.init = init;
76
77 if (fini != NULL)
78 perf_evsel__object.fini = fini;
79
80 return 0;
81}
82
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -020083#define FD(e, x, y) (*(int *)xyarray__entry(e->fd, x, y))
84
Adrian Hunter75562572013-08-27 11:23:09 +030085int __perf_evsel__sample_size(u64 sample_type)
Arnaldo Carvalho de Meloc2a70652011-06-02 11:04:54 -030086{
87 u64 mask = sample_type & PERF_SAMPLE_MASK;
88 int size = 0;
89 int i;
90
91 for (i = 0; i < 64; i++) {
92 if (mask & (1ULL << i))
93 size++;
94 }
95
96 size *= sizeof(u64);
97
98 return size;
99}
100
Adrian Hunter75562572013-08-27 11:23:09 +0300101/**
102 * __perf_evsel__calc_id_pos - calculate id_pos.
103 * @sample_type: sample type
104 *
105 * This function returns the position of the event id (PERF_SAMPLE_ID or
106 * PERF_SAMPLE_IDENTIFIER) in a sample event i.e. in the array of struct
107 * sample_event.
108 */
109static int __perf_evsel__calc_id_pos(u64 sample_type)
110{
111 int idx = 0;
112
113 if (sample_type & PERF_SAMPLE_IDENTIFIER)
114 return 0;
115
116 if (!(sample_type & PERF_SAMPLE_ID))
117 return -1;
118
119 if (sample_type & PERF_SAMPLE_IP)
120 idx += 1;
121
122 if (sample_type & PERF_SAMPLE_TID)
123 idx += 1;
124
125 if (sample_type & PERF_SAMPLE_TIME)
126 idx += 1;
127
128 if (sample_type & PERF_SAMPLE_ADDR)
129 idx += 1;
130
131 return idx;
132}
133
134/**
135 * __perf_evsel__calc_is_pos - calculate is_pos.
136 * @sample_type: sample type
137 *
138 * This function returns the position (counting backwards) of the event id
139 * (PERF_SAMPLE_ID or PERF_SAMPLE_IDENTIFIER) in a non-sample event i.e. if
140 * sample_id_all is used there is an id sample appended to non-sample events.
141 */
142static int __perf_evsel__calc_is_pos(u64 sample_type)
143{
144 int idx = 1;
145
146 if (sample_type & PERF_SAMPLE_IDENTIFIER)
147 return 1;
148
149 if (!(sample_type & PERF_SAMPLE_ID))
150 return -1;
151
152 if (sample_type & PERF_SAMPLE_CPU)
153 idx += 1;
154
155 if (sample_type & PERF_SAMPLE_STREAM_ID)
156 idx += 1;
157
158 return idx;
159}
160
161void perf_evsel__calc_id_pos(struct perf_evsel *evsel)
162{
163 evsel->id_pos = __perf_evsel__calc_id_pos(evsel->attr.sample_type);
164 evsel->is_pos = __perf_evsel__calc_is_pos(evsel->attr.sample_type);
165}
166
Arnaldo Carvalho de Melo7be5ebe2012-12-10 14:53:43 -0300167void __perf_evsel__set_sample_bit(struct perf_evsel *evsel,
168 enum perf_event_sample_format bit)
169{
170 if (!(evsel->attr.sample_type & bit)) {
171 evsel->attr.sample_type |= bit;
172 evsel->sample_size += sizeof(u64);
Adrian Hunter75562572013-08-27 11:23:09 +0300173 perf_evsel__calc_id_pos(evsel);
Arnaldo Carvalho de Melo7be5ebe2012-12-10 14:53:43 -0300174 }
175}
176
177void __perf_evsel__reset_sample_bit(struct perf_evsel *evsel,
178 enum perf_event_sample_format bit)
179{
180 if (evsel->attr.sample_type & bit) {
181 evsel->attr.sample_type &= ~bit;
182 evsel->sample_size -= sizeof(u64);
Adrian Hunter75562572013-08-27 11:23:09 +0300183 perf_evsel__calc_id_pos(evsel);
Arnaldo Carvalho de Melo7be5ebe2012-12-10 14:53:43 -0300184 }
185}
186
Adrian Hunter75562572013-08-27 11:23:09 +0300187void perf_evsel__set_sample_id(struct perf_evsel *evsel,
188 bool can_sample_identifier)
Arnaldo Carvalho de Melo7a5a5ca2012-12-10 15:21:30 -0300189{
Adrian Hunter75562572013-08-27 11:23:09 +0300190 if (can_sample_identifier) {
191 perf_evsel__reset_sample_bit(evsel, ID);
192 perf_evsel__set_sample_bit(evsel, IDENTIFIER);
193 } else {
194 perf_evsel__set_sample_bit(evsel, ID);
195 }
Arnaldo Carvalho de Melo7a5a5ca2012-12-10 15:21:30 -0300196 evsel->attr.read_format |= PERF_FORMAT_ID;
197}
198
Arnaldo Carvalho de Meloef1d1af2011-01-18 21:41:45 -0200199void perf_evsel__init(struct perf_evsel *evsel,
200 struct perf_event_attr *attr, int idx)
201{
202 evsel->idx = idx;
Adrian Hunter60b08962014-07-31 09:00:52 +0300203 evsel->tracking = !idx;
Arnaldo Carvalho de Meloef1d1af2011-01-18 21:41:45 -0200204 evsel->attr = *attr;
Namhyung Kim2cfda562012-11-29 15:38:29 +0900205 evsel->leader = evsel;
Stephane Eranian410136f2013-11-12 17:58:49 +0100206 evsel->unit = "";
207 evsel->scale = 1.0;
Arnaldo Carvalho de Meloef1d1af2011-01-18 21:41:45 -0200208 INIT_LIST_HEAD(&evsel->node);
Arnaldo Carvalho de Meloce8ccff2014-10-09 15:29:51 -0300209 perf_evsel__object.init(evsel);
Arnaldo Carvalho de Melobde09462012-08-01 18:53:11 -0300210 evsel->sample_size = __perf_evsel__sample_size(attr->sample_type);
Adrian Hunter75562572013-08-27 11:23:09 +0300211 perf_evsel__calc_id_pos(evsel);
Arnaldo Carvalho de Meloef1d1af2011-01-18 21:41:45 -0200212}
213
Arnaldo Carvalho de Meloef503832013-11-07 16:41:19 -0300214struct perf_evsel *perf_evsel__new_idx(struct perf_event_attr *attr, int idx)
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200215{
Arnaldo Carvalho de Meloce8ccff2014-10-09 15:29:51 -0300216 struct perf_evsel *evsel = zalloc(perf_evsel__object.size);
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200217
Arnaldo Carvalho de Meloef1d1af2011-01-18 21:41:45 -0200218 if (evsel != NULL)
219 perf_evsel__init(evsel, attr, idx);
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200220
221 return evsel;
222}
223
Arnaldo Carvalho de Meloef503832013-11-07 16:41:19 -0300224struct perf_evsel *perf_evsel__newtp_idx(const char *sys, const char *name, int idx)
Arnaldo Carvalho de Meloefd2b922012-09-18 11:21:50 -0300225{
Arnaldo Carvalho de Meloce8ccff2014-10-09 15:29:51 -0300226 struct perf_evsel *evsel = zalloc(perf_evsel__object.size);
Arnaldo Carvalho de Meloefd2b922012-09-18 11:21:50 -0300227
228 if (evsel != NULL) {
229 struct perf_event_attr attr = {
Arnaldo Carvalho de Melo0b80f8b32012-09-26 12:28:26 -0300230 .type = PERF_TYPE_TRACEPOINT,
231 .sample_type = (PERF_SAMPLE_RAW | PERF_SAMPLE_TIME |
232 PERF_SAMPLE_CPU | PERF_SAMPLE_PERIOD),
Arnaldo Carvalho de Meloefd2b922012-09-18 11:21:50 -0300233 };
234
Arnaldo Carvalho de Meloe48ffe22012-09-26 17:11:38 -0300235 if (asprintf(&evsel->name, "%s:%s", sys, name) < 0)
236 goto out_free;
237
Jiri Olsa97978b32013-12-03 14:09:24 +0100238 evsel->tp_format = trace_event__tp_format(sys, name);
Arnaldo Carvalho de Meloefd2b922012-09-18 11:21:50 -0300239 if (evsel->tp_format == NULL)
240 goto out_free;
241
Arnaldo Carvalho de Melo0b80f8b32012-09-26 12:28:26 -0300242 event_attr_init(&attr);
Arnaldo Carvalho de Meloefd2b922012-09-18 11:21:50 -0300243 attr.config = evsel->tp_format->id;
Arnaldo Carvalho de Melo0b80f8b32012-09-26 12:28:26 -0300244 attr.sample_period = 1;
Arnaldo Carvalho de Meloefd2b922012-09-18 11:21:50 -0300245 perf_evsel__init(evsel, &attr, idx);
Arnaldo Carvalho de Meloefd2b922012-09-18 11:21:50 -0300246 }
247
248 return evsel;
249
250out_free:
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -0300251 zfree(&evsel->name);
Arnaldo Carvalho de Meloefd2b922012-09-18 11:21:50 -0300252 free(evsel);
253 return NULL;
254}
255
Arnaldo Carvalho de Melo8ad70132012-09-06 13:11:18 -0300256const char *perf_evsel__hw_names[PERF_COUNT_HW_MAX] = {
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300257 "cycles",
258 "instructions",
259 "cache-references",
260 "cache-misses",
261 "branches",
262 "branch-misses",
263 "bus-cycles",
264 "stalled-cycles-frontend",
265 "stalled-cycles-backend",
266 "ref-cycles",
267};
268
Arnaldo Carvalho de Melodd4f5222012-06-13 15:52:42 -0300269static const char *__perf_evsel__hw_name(u64 config)
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300270{
271 if (config < PERF_COUNT_HW_MAX && perf_evsel__hw_names[config])
272 return perf_evsel__hw_names[config];
273
274 return "unknown-hardware";
275}
276
Arnaldo Carvalho de Melo27f18612012-06-11 13:33:09 -0300277static int perf_evsel__add_modifiers(struct perf_evsel *evsel, char *bf, size_t size)
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300278{
Arnaldo Carvalho de Melo27f18612012-06-11 13:33:09 -0300279 int colon = 0, r = 0;
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300280 struct perf_event_attr *attr = &evsel->attr;
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300281 bool exclude_guest_default = false;
282
283#define MOD_PRINT(context, mod) do { \
284 if (!attr->exclude_##context) { \
Arnaldo Carvalho de Melo27f18612012-06-11 13:33:09 -0300285 if (!colon) colon = ++r; \
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300286 r += scnprintf(bf + r, size - r, "%c", mod); \
287 } } while(0)
288
289 if (attr->exclude_kernel || attr->exclude_user || attr->exclude_hv) {
290 MOD_PRINT(kernel, 'k');
291 MOD_PRINT(user, 'u');
292 MOD_PRINT(hv, 'h');
293 exclude_guest_default = true;
294 }
295
296 if (attr->precise_ip) {
297 if (!colon)
Arnaldo Carvalho de Melo27f18612012-06-11 13:33:09 -0300298 colon = ++r;
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300299 r += scnprintf(bf + r, size - r, "%.*s", attr->precise_ip, "ppp");
300 exclude_guest_default = true;
301 }
302
303 if (attr->exclude_host || attr->exclude_guest == exclude_guest_default) {
304 MOD_PRINT(host, 'H');
305 MOD_PRINT(guest, 'G');
306 }
307#undef MOD_PRINT
308 if (colon)
Arnaldo Carvalho de Melo27f18612012-06-11 13:33:09 -0300309 bf[colon - 1] = ':';
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300310 return r;
311}
312
Arnaldo Carvalho de Melo27f18612012-06-11 13:33:09 -0300313static int perf_evsel__hw_name(struct perf_evsel *evsel, char *bf, size_t size)
314{
315 int r = scnprintf(bf, size, "%s", __perf_evsel__hw_name(evsel->attr.config));
316 return r + perf_evsel__add_modifiers(evsel, bf + r, size - r);
317}
318
Arnaldo Carvalho de Melo8ad70132012-09-06 13:11:18 -0300319const char *perf_evsel__sw_names[PERF_COUNT_SW_MAX] = {
Arnaldo Carvalho de Melo335c2f52012-06-11 14:36:20 -0300320 "cpu-clock",
321 "task-clock",
322 "page-faults",
323 "context-switches",
Arnaldo Carvalho de Melo8ad70132012-09-06 13:11:18 -0300324 "cpu-migrations",
Arnaldo Carvalho de Melo335c2f52012-06-11 14:36:20 -0300325 "minor-faults",
326 "major-faults",
327 "alignment-faults",
328 "emulation-faults",
Adrian Hunterd22d1a22013-08-31 21:50:52 +0300329 "dummy",
Arnaldo Carvalho de Melo335c2f52012-06-11 14:36:20 -0300330};
331
Arnaldo Carvalho de Melodd4f5222012-06-13 15:52:42 -0300332static const char *__perf_evsel__sw_name(u64 config)
Arnaldo Carvalho de Melo335c2f52012-06-11 14:36:20 -0300333{
334 if (config < PERF_COUNT_SW_MAX && perf_evsel__sw_names[config])
335 return perf_evsel__sw_names[config];
336 return "unknown-software";
337}
338
339static int perf_evsel__sw_name(struct perf_evsel *evsel, char *bf, size_t size)
340{
341 int r = scnprintf(bf, size, "%s", __perf_evsel__sw_name(evsel->attr.config));
342 return r + perf_evsel__add_modifiers(evsel, bf + r, size - r);
343}
344
Jiri Olsa287e74a2012-06-28 23:18:49 +0200345static int __perf_evsel__bp_name(char *bf, size_t size, u64 addr, u64 type)
346{
347 int r;
348
349 r = scnprintf(bf, size, "mem:0x%" PRIx64 ":", addr);
350
351 if (type & HW_BREAKPOINT_R)
352 r += scnprintf(bf + r, size - r, "r");
353
354 if (type & HW_BREAKPOINT_W)
355 r += scnprintf(bf + r, size - r, "w");
356
357 if (type & HW_BREAKPOINT_X)
358 r += scnprintf(bf + r, size - r, "x");
359
360 return r;
361}
362
363static int perf_evsel__bp_name(struct perf_evsel *evsel, char *bf, size_t size)
364{
365 struct perf_event_attr *attr = &evsel->attr;
366 int r = __perf_evsel__bp_name(bf, size, attr->bp_addr, attr->bp_type);
367 return r + perf_evsel__add_modifiers(evsel, bf + r, size - r);
368}
369
Arnaldo Carvalho de Melo0b668bc2012-06-11 14:08:07 -0300370const char *perf_evsel__hw_cache[PERF_COUNT_HW_CACHE_MAX]
371 [PERF_EVSEL__MAX_ALIASES] = {
372 { "L1-dcache", "l1-d", "l1d", "L1-data", },
373 { "L1-icache", "l1-i", "l1i", "L1-instruction", },
374 { "LLC", "L2", },
375 { "dTLB", "d-tlb", "Data-TLB", },
376 { "iTLB", "i-tlb", "Instruction-TLB", },
377 { "branch", "branches", "bpu", "btb", "bpc", },
378 { "node", },
379};
380
381const char *perf_evsel__hw_cache_op[PERF_COUNT_HW_CACHE_OP_MAX]
382 [PERF_EVSEL__MAX_ALIASES] = {
383 { "load", "loads", "read", },
384 { "store", "stores", "write", },
385 { "prefetch", "prefetches", "speculative-read", "speculative-load", },
386};
387
388const char *perf_evsel__hw_cache_result[PERF_COUNT_HW_CACHE_RESULT_MAX]
389 [PERF_EVSEL__MAX_ALIASES] = {
390 { "refs", "Reference", "ops", "access", },
391 { "misses", "miss", },
392};
393
394#define C(x) PERF_COUNT_HW_CACHE_##x
395#define CACHE_READ (1 << C(OP_READ))
396#define CACHE_WRITE (1 << C(OP_WRITE))
397#define CACHE_PREFETCH (1 << C(OP_PREFETCH))
398#define COP(x) (1 << x)
399
400/*
401 * cache operartion stat
402 * L1I : Read and prefetch only
403 * ITLB and BPU : Read-only
404 */
405static unsigned long perf_evsel__hw_cache_stat[C(MAX)] = {
406 [C(L1D)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
407 [C(L1I)] = (CACHE_READ | CACHE_PREFETCH),
408 [C(LL)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
409 [C(DTLB)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
410 [C(ITLB)] = (CACHE_READ),
411 [C(BPU)] = (CACHE_READ),
412 [C(NODE)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
413};
414
415bool perf_evsel__is_cache_op_valid(u8 type, u8 op)
416{
417 if (perf_evsel__hw_cache_stat[type] & COP(op))
418 return true; /* valid */
419 else
420 return false; /* invalid */
421}
422
423int __perf_evsel__hw_cache_type_op_res_name(u8 type, u8 op, u8 result,
424 char *bf, size_t size)
425{
426 if (result) {
427 return scnprintf(bf, size, "%s-%s-%s", perf_evsel__hw_cache[type][0],
428 perf_evsel__hw_cache_op[op][0],
429 perf_evsel__hw_cache_result[result][0]);
430 }
431
432 return scnprintf(bf, size, "%s-%s", perf_evsel__hw_cache[type][0],
433 perf_evsel__hw_cache_op[op][1]);
434}
435
Arnaldo Carvalho de Melodd4f5222012-06-13 15:52:42 -0300436static int __perf_evsel__hw_cache_name(u64 config, char *bf, size_t size)
Arnaldo Carvalho de Melo0b668bc2012-06-11 14:08:07 -0300437{
438 u8 op, result, type = (config >> 0) & 0xff;
439 const char *err = "unknown-ext-hardware-cache-type";
440
441 if (type > PERF_COUNT_HW_CACHE_MAX)
442 goto out_err;
443
444 op = (config >> 8) & 0xff;
445 err = "unknown-ext-hardware-cache-op";
446 if (op > PERF_COUNT_HW_CACHE_OP_MAX)
447 goto out_err;
448
449 result = (config >> 16) & 0xff;
450 err = "unknown-ext-hardware-cache-result";
451 if (result > PERF_COUNT_HW_CACHE_RESULT_MAX)
452 goto out_err;
453
454 err = "invalid-cache";
455 if (!perf_evsel__is_cache_op_valid(type, op))
456 goto out_err;
457
458 return __perf_evsel__hw_cache_type_op_res_name(type, op, result, bf, size);
459out_err:
460 return scnprintf(bf, size, "%s", err);
461}
462
463static int perf_evsel__hw_cache_name(struct perf_evsel *evsel, char *bf, size_t size)
464{
465 int ret = __perf_evsel__hw_cache_name(evsel->attr.config, bf, size);
466 return ret + perf_evsel__add_modifiers(evsel, bf + ret, size - ret);
467}
468
Arnaldo Carvalho de Melo6eef3d92012-06-13 11:53:37 -0300469static int perf_evsel__raw_name(struct perf_evsel *evsel, char *bf, size_t size)
470{
471 int ret = scnprintf(bf, size, "raw 0x%" PRIx64, evsel->attr.config);
472 return ret + perf_evsel__add_modifiers(evsel, bf + ret, size - ret);
473}
474
Arnaldo Carvalho de Melo7289f832012-06-12 12:34:58 -0300475const char *perf_evsel__name(struct perf_evsel *evsel)
Arnaldo Carvalho de Meloa4460832012-06-12 10:29:12 -0300476{
Arnaldo Carvalho de Melo7289f832012-06-12 12:34:58 -0300477 char bf[128];
Arnaldo Carvalho de Meloa4460832012-06-12 10:29:12 -0300478
Arnaldo Carvalho de Melo7289f832012-06-12 12:34:58 -0300479 if (evsel->name)
480 return evsel->name;
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300481
482 switch (evsel->attr.type) {
483 case PERF_TYPE_RAW:
Arnaldo Carvalho de Melo6eef3d92012-06-13 11:53:37 -0300484 perf_evsel__raw_name(evsel, bf, sizeof(bf));
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300485 break;
486
487 case PERF_TYPE_HARDWARE:
Arnaldo Carvalho de Melo7289f832012-06-12 12:34:58 -0300488 perf_evsel__hw_name(evsel, bf, sizeof(bf));
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300489 break;
Arnaldo Carvalho de Melo0b668bc2012-06-11 14:08:07 -0300490
491 case PERF_TYPE_HW_CACHE:
Arnaldo Carvalho de Melo7289f832012-06-12 12:34:58 -0300492 perf_evsel__hw_cache_name(evsel, bf, sizeof(bf));
Arnaldo Carvalho de Melo0b668bc2012-06-11 14:08:07 -0300493 break;
494
Arnaldo Carvalho de Melo335c2f52012-06-11 14:36:20 -0300495 case PERF_TYPE_SOFTWARE:
Arnaldo Carvalho de Melo7289f832012-06-12 12:34:58 -0300496 perf_evsel__sw_name(evsel, bf, sizeof(bf));
Arnaldo Carvalho de Melo335c2f52012-06-11 14:36:20 -0300497 break;
498
Arnaldo Carvalho de Meloa4460832012-06-12 10:29:12 -0300499 case PERF_TYPE_TRACEPOINT:
Arnaldo Carvalho de Melo7289f832012-06-12 12:34:58 -0300500 scnprintf(bf, sizeof(bf), "%s", "unknown tracepoint");
Arnaldo Carvalho de Meloa4460832012-06-12 10:29:12 -0300501 break;
502
Jiri Olsa287e74a2012-06-28 23:18:49 +0200503 case PERF_TYPE_BREAKPOINT:
504 perf_evsel__bp_name(evsel, bf, sizeof(bf));
505 break;
506
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300507 default:
Robert Richterca1b1452012-08-16 21:10:18 +0200508 scnprintf(bf, sizeof(bf), "unknown attr type: %d",
509 evsel->attr.type);
Arnaldo Carvalho de Meloa4460832012-06-12 10:29:12 -0300510 break;
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300511 }
512
Arnaldo Carvalho de Melo7289f832012-06-12 12:34:58 -0300513 evsel->name = strdup(bf);
514
515 return evsel->name ?: "unknown";
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300516}
517
Namhyung Kim717e2632013-01-22 18:09:44 +0900518const char *perf_evsel__group_name(struct perf_evsel *evsel)
519{
520 return evsel->group_name ?: "anon group";
521}
522
523int perf_evsel__group_desc(struct perf_evsel *evsel, char *buf, size_t size)
524{
525 int ret;
526 struct perf_evsel *pos;
527 const char *group_name = perf_evsel__group_name(evsel);
528
529 ret = scnprintf(buf, size, "%s", group_name);
530
531 ret += scnprintf(buf + ret, size - ret, " { %s",
532 perf_evsel__name(evsel));
533
534 for_each_group_member(pos, evsel)
535 ret += scnprintf(buf + ret, size - ret, ", %s",
536 perf_evsel__name(pos));
537
538 ret += scnprintf(buf + ret, size - ret, " }");
539
540 return ret;
541}
542
Jiri Olsa6bedfab2014-03-02 16:56:40 +0100543static void
Kan Liangaad2b212015-01-05 13:23:04 -0500544perf_evsel__config_callgraph(struct perf_evsel *evsel,
545 struct record_opts *opts)
Jiri Olsa6bedfab2014-03-02 16:56:40 +0100546{
547 bool function = perf_evsel__is_function_event(evsel);
548 struct perf_event_attr *attr = &evsel->attr;
549
550 perf_evsel__set_sample_bit(evsel, CALLCHAIN);
551
Kan Liangaad2b212015-01-05 13:23:04 -0500552 if (callchain_param.record_mode == CALLCHAIN_LBR) {
553 if (!opts->branch_stack) {
554 if (attr->exclude_user) {
555 pr_warning("LBR callstack option is only available "
556 "to get user callchain information. "
557 "Falling back to framepointers.\n");
558 } else {
559 perf_evsel__set_sample_bit(evsel, BRANCH_STACK);
560 attr->branch_sample_type = PERF_SAMPLE_BRANCH_USER |
561 PERF_SAMPLE_BRANCH_CALL_STACK;
562 }
563 } else
564 pr_warning("Cannot use LBR callstack with branch stack. "
565 "Falling back to framepointers.\n");
566 }
567
Namhyung Kim72a128a2014-09-23 10:01:41 +0900568 if (callchain_param.record_mode == CALLCHAIN_DWARF) {
Jiri Olsa6bedfab2014-03-02 16:56:40 +0100569 if (!function) {
570 perf_evsel__set_sample_bit(evsel, REGS_USER);
571 perf_evsel__set_sample_bit(evsel, STACK_USER);
572 attr->sample_regs_user = PERF_REGS_MASK;
Namhyung Kim72a128a2014-09-23 10:01:41 +0900573 attr->sample_stack_user = callchain_param.dump_size;
Jiri Olsa6bedfab2014-03-02 16:56:40 +0100574 attr->exclude_callchain_user = 1;
575 } else {
576 pr_info("Cannot use DWARF unwind for function trace event,"
577 " falling back to framepointers.\n");
578 }
579 }
580
581 if (function) {
582 pr_info("Disabling user space callchains for function trace event.\n");
583 attr->exclude_callchain_user = 1;
584 }
585}
586
Jiri Olsa774cb492012-11-12 18:34:01 +0100587/*
588 * The enable_on_exec/disabled value strategy:
589 *
590 * 1) For any type of traced program:
591 * - all independent events and group leaders are disabled
592 * - all group members are enabled
593 *
594 * Group members are ruled by group leaders. They need to
595 * be enabled, because the group scheduling relies on that.
596 *
597 * 2) For traced programs executed by perf:
598 * - all independent events and group leaders have
599 * enable_on_exec set
600 * - we don't specifically enable or disable any event during
601 * the record command
602 *
603 * Independent events and group leaders are initially disabled
604 * and get enabled by exec. Group members are ruled by group
605 * leaders as stated in 1).
606 *
607 * 3) For traced programs attached by perf (pid/tid):
608 * - we specifically enable or disable all events during
609 * the record command
610 *
611 * When attaching events to already running traced we
612 * enable/disable events specifically, as there's no
613 * initial traced exec call.
614 */
Arnaldo Carvalho de Melob4006792013-12-19 14:43:45 -0300615void perf_evsel__config(struct perf_evsel *evsel, struct record_opts *opts)
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200616{
Jiri Olsa3c176312012-10-10 17:39:03 +0200617 struct perf_evsel *leader = evsel->leader;
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200618 struct perf_event_attr *attr = &evsel->attr;
Adrian Hunter60b08962014-07-31 09:00:52 +0300619 int track = evsel->tracking;
Adrian Hunter3aa59392013-11-15 15:52:29 +0200620 bool per_cpu = opts->target.default_per_cpu && !opts->target.per_thread;
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200621
Arnaldo Carvalho de Melo594ac612012-12-13 13:13:07 -0300622 attr->sample_id_all = perf_missing_features.sample_id_all ? 0 : 1;
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200623 attr->inherit = !opts->no_inherit;
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200624
Arnaldo Carvalho de Melo7be5ebe2012-12-10 14:53:43 -0300625 perf_evsel__set_sample_bit(evsel, IP);
626 perf_evsel__set_sample_bit(evsel, TID);
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200627
Jiri Olsa3c176312012-10-10 17:39:03 +0200628 if (evsel->sample_read) {
629 perf_evsel__set_sample_bit(evsel, READ);
630
631 /*
632 * We need ID even in case of single event, because
633 * PERF_SAMPLE_READ process ID specific data.
634 */
Adrian Hunter75562572013-08-27 11:23:09 +0300635 perf_evsel__set_sample_id(evsel, false);
Jiri Olsa3c176312012-10-10 17:39:03 +0200636
637 /*
638 * Apply group format only if we belong to group
639 * with more than one members.
640 */
641 if (leader->nr_members > 1) {
642 attr->read_format |= PERF_FORMAT_GROUP;
643 attr->inherit = 0;
644 }
645 }
646
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200647 /*
Namhyung Kim17314e22014-06-09 14:43:37 +0900648 * We default some events to have a default interval. But keep
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200649 * it a weak assumption overridable by the user.
650 */
Namhyung Kim17314e22014-06-09 14:43:37 +0900651 if (!attr->sample_period || (opts->user_freq != UINT_MAX ||
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200652 opts->user_interval != ULLONG_MAX)) {
653 if (opts->freq) {
Arnaldo Carvalho de Melo7be5ebe2012-12-10 14:53:43 -0300654 perf_evsel__set_sample_bit(evsel, PERIOD);
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200655 attr->freq = 1;
656 attr->sample_freq = opts->freq;
657 } else {
658 attr->sample_period = opts->default_interval;
659 }
660 }
661
Jiri Olsa3c176312012-10-10 17:39:03 +0200662 /*
663 * Disable sampling for all group members other
664 * than leader in case leader 'leads' the sampling.
665 */
666 if ((leader != evsel) && leader->sample_read) {
667 attr->sample_freq = 0;
668 attr->sample_period = 0;
669 }
670
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200671 if (opts->no_samples)
672 attr->sample_freq = 0;
673
674 if (opts->inherit_stat)
675 attr->inherit_stat = 1;
676
677 if (opts->sample_address) {
Arnaldo Carvalho de Melo7be5ebe2012-12-10 14:53:43 -0300678 perf_evsel__set_sample_bit(evsel, ADDR);
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200679 attr->mmap_data = track;
680 }
681
Jiri Olsaf1403732014-11-13 18:21:03 +0100682 /*
683 * We don't allow user space callchains for function trace
684 * event, due to issues with page faults while tracing page
685 * fault handler and its overall trickiness nature.
686 */
687 if (perf_evsel__is_function_event(evsel))
688 evsel->attr.exclude_callchain_user = 1;
689
Namhyung Kim72a128a2014-09-23 10:01:41 +0900690 if (callchain_param.enabled && !evsel->no_aux_samples)
Kan Liangaad2b212015-01-05 13:23:04 -0500691 perf_evsel__config_callgraph(evsel, opts);
Jiri Olsa26d33022012-08-07 15:20:47 +0200692
Stephane Eranian6a21c0b2014-09-24 13:48:39 +0200693 if (opts->sample_intr_regs) {
694 attr->sample_regs_intr = PERF_REGS_MASK;
695 perf_evsel__set_sample_bit(evsel, REGS_INTR);
696 }
697
Adrian Hunter3aa59392013-11-15 15:52:29 +0200698 if (target__has_cpu(&opts->target))
Arnaldo Carvalho de Melo7be5ebe2012-12-10 14:53:43 -0300699 perf_evsel__set_sample_bit(evsel, CPU);
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200700
Andrew Vagin3e76ac72011-12-20 17:32:45 +0300701 if (opts->period)
Arnaldo Carvalho de Melo7be5ebe2012-12-10 14:53:43 -0300702 perf_evsel__set_sample_bit(evsel, PERIOD);
Andrew Vagin3e76ac72011-12-20 17:32:45 +0300703
Andi Kleen8affc2b2014-07-31 14:45:04 +0800704 /*
705 * When the user explicitely disabled time don't force it here.
706 */
707 if (opts->sample_time &&
708 (!perf_missing_features.sample_id_all &&
709 (!opts->no_inherit || target__has_cpu(&opts->target) || per_cpu)))
Arnaldo Carvalho de Melo7be5ebe2012-12-10 14:53:43 -0300710 perf_evsel__set_sample_bit(evsel, TIME);
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200711
Adrian Hunter6ff1ce72014-07-14 13:02:56 +0300712 if (opts->raw_samples && !evsel->no_aux_samples) {
Arnaldo Carvalho de Melo7be5ebe2012-12-10 14:53:43 -0300713 perf_evsel__set_sample_bit(evsel, TIME);
714 perf_evsel__set_sample_bit(evsel, RAW);
715 perf_evsel__set_sample_bit(evsel, CPU);
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200716 }
717
Stephane Eranianccf49bf2013-01-24 16:10:37 +0100718 if (opts->sample_address)
Adrian Hunter1e7ed5e2013-11-01 15:51:35 +0200719 perf_evsel__set_sample_bit(evsel, DATA_SRC);
Stephane Eranianccf49bf2013-01-24 16:10:37 +0100720
Arnaldo Carvalho de Melo509051e2014-01-14 17:52:14 -0300721 if (opts->no_buffering) {
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200722 attr->watermark = 0;
723 attr->wakeup_events = 1;
724 }
Adrian Hunter6ff1ce72014-07-14 13:02:56 +0300725 if (opts->branch_stack && !evsel->no_aux_samples) {
Arnaldo Carvalho de Melo7be5ebe2012-12-10 14:53:43 -0300726 perf_evsel__set_sample_bit(evsel, BRANCH_STACK);
Roberto Agostino Vitillobdfebd82012-02-09 23:21:02 +0100727 attr->branch_sample_type = opts->branch_stack;
728 }
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200729
Andi Kleen05484292013-01-24 16:10:29 +0100730 if (opts->sample_weight)
Adrian Hunter1e7ed5e2013-11-01 15:51:35 +0200731 perf_evsel__set_sample_bit(evsel, WEIGHT);
Andi Kleen05484292013-01-24 16:10:29 +0100732
Namhyung Kim62e503b2015-01-29 17:06:46 +0900733 attr->task = track;
Stephane Eranian5c5e8542013-08-21 12:10:25 +0200734 attr->mmap = track;
Don Zickusa5a5ba72014-05-30 10:49:42 -0400735 attr->mmap2 = track && !perf_missing_features.mmap2;
Stephane Eranian5c5e8542013-08-21 12:10:25 +0200736 attr->comm = track;
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200737
Andi Kleen475eeab2013-09-20 07:40:43 -0700738 if (opts->sample_transaction)
Adrian Hunter1e7ed5e2013-11-01 15:51:35 +0200739 perf_evsel__set_sample_bit(evsel, TRANSACTION);
Andi Kleen475eeab2013-09-20 07:40:43 -0700740
Andi Kleen85c273d2015-02-24 15:13:40 -0800741 if (opts->running_time) {
742 evsel->attr.read_format |=
743 PERF_FORMAT_TOTAL_TIME_ENABLED |
744 PERF_FORMAT_TOTAL_TIME_RUNNING;
745 }
746
Jiri Olsa774cb492012-11-12 18:34:01 +0100747 /*
748 * XXX see the function comment above
749 *
750 * Disabling only independent events or group leaders,
751 * keeping group members enabled.
752 */
Namhyung Kim823254e2012-11-29 15:38:30 +0900753 if (perf_evsel__is_group_leader(evsel))
Jiri Olsa774cb492012-11-12 18:34:01 +0100754 attr->disabled = 1;
755
756 /*
757 * Setting enable_on_exec for independent events and
758 * group leaders for traced executed by perf.
759 */
Andi Kleen6619a532014-01-11 13:38:27 -0800760 if (target__none(&opts->target) && perf_evsel__is_group_leader(evsel) &&
761 !opts->initial_delay)
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200762 attr->enable_on_exec = 1;
Adrian Hunter2afd2bc2014-07-14 13:02:57 +0300763
764 if (evsel->immediate) {
765 attr->disabled = 0;
766 attr->enable_on_exec = 0;
767 }
Peter Zijlstra814c8c32015-03-31 00:19:31 +0200768
769 clockid = opts->clockid;
770 if (opts->use_clockid) {
771 attr->use_clockid = 1;
772 attr->clockid = opts->clockid;
773 }
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200774}
775
Arnaldo Carvalho de Melo88858462014-10-13 13:30:27 -0300776static int perf_evsel__alloc_fd(struct perf_evsel *evsel, int ncpus, int nthreads)
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200777{
David Ahern4af4c952011-05-27 09:58:34 -0600778 int cpu, thread;
Adrian Hunterbf8e8f42014-07-31 09:00:51 +0300779
780 if (evsel->system_wide)
781 nthreads = 1;
782
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200783 evsel->fd = xyarray__new(ncpus, nthreads, sizeof(int));
David Ahern4af4c952011-05-27 09:58:34 -0600784
785 if (evsel->fd) {
786 for (cpu = 0; cpu < ncpus; cpu++) {
787 for (thread = 0; thread < nthreads; thread++) {
788 FD(evsel, cpu, thread) = -1;
789 }
790 }
791 }
792
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200793 return evsel->fd != NULL ? 0 : -ENOMEM;
794}
795
Andi Kleene2407be2013-08-02 17:41:10 -0700796static int perf_evsel__run_ioctl(struct perf_evsel *evsel, int ncpus, int nthreads,
797 int ioc, void *arg)
Arnaldo Carvalho de Melo745cefc2012-09-26 15:07:39 -0300798{
799 int cpu, thread;
800
Adrian Hunterbf8e8f42014-07-31 09:00:51 +0300801 if (evsel->system_wide)
802 nthreads = 1;
803
Arnaldo Carvalho de Melo745cefc2012-09-26 15:07:39 -0300804 for (cpu = 0; cpu < ncpus; cpu++) {
805 for (thread = 0; thread < nthreads; thread++) {
806 int fd = FD(evsel, cpu, thread),
Andi Kleene2407be2013-08-02 17:41:10 -0700807 err = ioctl(fd, ioc, arg);
Arnaldo Carvalho de Melo745cefc2012-09-26 15:07:39 -0300808
809 if (err)
810 return err;
811 }
812 }
813
814 return 0;
815}
816
Andi Kleene2407be2013-08-02 17:41:10 -0700817int perf_evsel__set_filter(struct perf_evsel *evsel, int ncpus, int nthreads,
818 const char *filter)
819{
820 return perf_evsel__run_ioctl(evsel, ncpus, nthreads,
821 PERF_EVENT_IOC_SET_FILTER,
822 (void *)filter);
823}
824
825int perf_evsel__enable(struct perf_evsel *evsel, int ncpus, int nthreads)
826{
827 return perf_evsel__run_ioctl(evsel, ncpus, nthreads,
828 PERF_EVENT_IOC_ENABLE,
829 0);
830}
831
Arnaldo Carvalho de Melo70db7532011-01-12 22:39:13 -0200832int perf_evsel__alloc_id(struct perf_evsel *evsel, int ncpus, int nthreads)
833{
Vineet Gupta8d9cbd82015-01-13 19:13:23 +0530834 if (ncpus == 0 || nthreads == 0)
835 return 0;
836
Adrian Hunterbf8e8f42014-07-31 09:00:51 +0300837 if (evsel->system_wide)
838 nthreads = 1;
839
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -0300840 evsel->sample_id = xyarray__new(ncpus, nthreads, sizeof(struct perf_sample_id));
841 if (evsel->sample_id == NULL)
842 return -ENOMEM;
843
844 evsel->id = zalloc(ncpus * nthreads * sizeof(u64));
845 if (evsel->id == NULL) {
846 xyarray__delete(evsel->sample_id);
847 evsel->sample_id = NULL;
848 return -ENOMEM;
849 }
850
851 return 0;
Arnaldo Carvalho de Melo70db7532011-01-12 22:39:13 -0200852}
853
Frederik Deweerdta7e191c2013-03-01 13:02:27 -0500854void perf_evsel__reset_counts(struct perf_evsel *evsel, int ncpus)
855{
856 memset(evsel->counts, 0, (sizeof(*evsel->counts) +
857 (ncpus * sizeof(struct perf_counts_values))));
858}
859
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200860int perf_evsel__alloc_counts(struct perf_evsel *evsel, int ncpus)
861{
862 evsel->counts = zalloc((sizeof(*evsel->counts) +
863 (ncpus * sizeof(struct perf_counts_values))));
864 return evsel->counts != NULL ? 0 : -ENOMEM;
865}
866
Arnaldo Carvalho de Melo88858462014-10-13 13:30:27 -0300867static void perf_evsel__free_fd(struct perf_evsel *evsel)
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200868{
869 xyarray__delete(evsel->fd);
870 evsel->fd = NULL;
871}
872
Arnaldo Carvalho de Melo88858462014-10-13 13:30:27 -0300873static void perf_evsel__free_id(struct perf_evsel *evsel)
Arnaldo Carvalho de Melo70db7532011-01-12 22:39:13 -0200874{
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -0300875 xyarray__delete(evsel->sample_id);
876 evsel->sample_id = NULL;
Arnaldo Carvalho de Melo04662522013-12-26 17:41:15 -0300877 zfree(&evsel->id);
Arnaldo Carvalho de Melo70db7532011-01-12 22:39:13 -0200878}
879
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200880void perf_evsel__close_fd(struct perf_evsel *evsel, int ncpus, int nthreads)
881{
882 int cpu, thread;
883
Adrian Hunterbf8e8f42014-07-31 09:00:51 +0300884 if (evsel->system_wide)
885 nthreads = 1;
886
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200887 for (cpu = 0; cpu < ncpus; cpu++)
888 for (thread = 0; thread < nthreads; ++thread) {
889 close(FD(evsel, cpu, thread));
890 FD(evsel, cpu, thread) = -1;
891 }
892}
893
Namhyung Kim43f8e762013-01-25 10:44:44 +0900894void perf_evsel__free_counts(struct perf_evsel *evsel)
895{
Arnaldo Carvalho de Melo74cf2492013-12-27 16:55:14 -0300896 zfree(&evsel->counts);
Namhyung Kim43f8e762013-01-25 10:44:44 +0900897}
898
Arnaldo Carvalho de Meloef1d1af2011-01-18 21:41:45 -0200899void perf_evsel__exit(struct perf_evsel *evsel)
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200900{
901 assert(list_empty(&evsel->node));
Namhyung Kim736b05a2013-03-15 14:48:49 +0900902 perf_evsel__free_fd(evsel);
903 perf_evsel__free_id(evsel);
Arnaldo Carvalho de Melo597e48c2014-10-16 13:25:01 -0300904 close_cgroup(evsel->cgrp);
905 zfree(&evsel->group_name);
Arnaldo Carvalho de Melo597e48c2014-10-16 13:25:01 -0300906 zfree(&evsel->name);
Arnaldo Carvalho de Meloce8ccff2014-10-09 15:29:51 -0300907 perf_evsel__object.fini(evsel);
Arnaldo Carvalho de Meloef1d1af2011-01-18 21:41:45 -0200908}
909
910void perf_evsel__delete(struct perf_evsel *evsel)
911{
912 perf_evsel__exit(evsel);
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200913 free(evsel);
914}
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200915
Jiri Olsa857a94a2014-11-21 10:31:05 +0100916void perf_evsel__compute_deltas(struct perf_evsel *evsel, int cpu,
917 struct perf_counts_values *count)
Stephane Eranianc7a79c42013-01-29 12:47:43 +0100918{
919 struct perf_counts_values tmp;
920
921 if (!evsel->prev_raw_counts)
922 return;
923
924 if (cpu == -1) {
925 tmp = evsel->prev_raw_counts->aggr;
926 evsel->prev_raw_counts->aggr = *count;
927 } else {
928 tmp = evsel->prev_raw_counts->cpu[cpu];
929 evsel->prev_raw_counts->cpu[cpu] = *count;
930 }
931
932 count->val = count->val - tmp.val;
933 count->ena = count->ena - tmp.ena;
934 count->run = count->run - tmp.run;
935}
936
Jiri Olsa13112bb2014-11-21 10:31:06 +0100937void perf_counts_values__scale(struct perf_counts_values *count,
938 bool scale, s8 *pscaled)
939{
940 s8 scaled = 0;
941
942 if (scale) {
943 if (count->run == 0) {
944 scaled = -1;
945 count->val = 0;
946 } else if (count->run < count->ena) {
947 scaled = 1;
948 count->val = (u64)((double) count->val * count->ena / count->run + 0.5);
949 }
950 } else
951 count->ena = count->run = 0;
952
953 if (pscaled)
954 *pscaled = scaled;
955}
956
Jiri Olsa011dccb2014-11-21 10:31:07 +0100957int perf_evsel__read_cb(struct perf_evsel *evsel, int cpu, int thread,
958 perf_evsel__read_cb_t cb)
959{
960 struct perf_counts_values count;
961
962 memset(&count, 0, sizeof(count));
963
964 if (FD(evsel, cpu, thread) < 0)
965 return -EINVAL;
966
967 if (readn(FD(evsel, cpu, thread), &count, sizeof(count)) < 0)
968 return -errno;
969
970 return cb(evsel, cpu, thread, &count);
971}
972
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200973int __perf_evsel__read_on_cpu(struct perf_evsel *evsel,
974 int cpu, int thread, bool scale)
975{
976 struct perf_counts_values count;
977 size_t nv = scale ? 3 : 1;
978
979 if (FD(evsel, cpu, thread) < 0)
980 return -EINVAL;
981
Arnaldo Carvalho de Melo4eed11d2011-01-04 00:13:17 -0200982 if (evsel->counts == NULL && perf_evsel__alloc_counts(evsel, cpu + 1) < 0)
983 return -ENOMEM;
984
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200985 if (readn(FD(evsel, cpu, thread), &count, nv * sizeof(u64)) < 0)
986 return -errno;
987
Jiri Olsa857a94a2014-11-21 10:31:05 +0100988 perf_evsel__compute_deltas(evsel, cpu, &count);
Jiri Olsa13112bb2014-11-21 10:31:06 +0100989 perf_counts_values__scale(&count, scale, NULL);
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200990 evsel->counts->cpu[cpu] = count;
991 return 0;
992}
993
Jiri Olsa6a4bb042012-08-08 12:22:36 +0200994static int get_group_fd(struct perf_evsel *evsel, int cpu, int thread)
995{
996 struct perf_evsel *leader = evsel->leader;
997 int fd;
998
Namhyung Kim823254e2012-11-29 15:38:30 +0900999 if (perf_evsel__is_group_leader(evsel))
Jiri Olsa6a4bb042012-08-08 12:22:36 +02001000 return -1;
1001
1002 /*
1003 * Leader must be already processed/open,
1004 * if not it's a bug.
1005 */
1006 BUG_ON(!leader->fd);
1007
1008 fd = FD(leader, cpu, thread);
1009 BUG_ON(fd == -1);
1010
1011 return fd;
1012}
1013
Peter Zijlstra2c5e8c52015-04-07 11:09:54 +02001014struct bit_names {
1015 int bit;
1016 const char *name;
1017};
Adrian Huntere3e1a542013-08-14 15:48:24 +03001018
Peter Zijlstra2c5e8c52015-04-07 11:09:54 +02001019static void __p_bits(char *buf, size_t size, u64 value, struct bit_names *bits)
Adrian Huntere3e1a542013-08-14 15:48:24 +03001020{
Peter Zijlstra2c5e8c52015-04-07 11:09:54 +02001021 bool first_bit = true;
1022 int i = 0;
Adrian Huntere3e1a542013-08-14 15:48:24 +03001023
Peter Zijlstra2c5e8c52015-04-07 11:09:54 +02001024 do {
1025 if (value & bits[i].bit) {
1026 buf += scnprintf(buf, size, "%s%s", first_bit ? "" : "|", bits[i].name);
1027 first_bit = false;
1028 }
1029 } while (bits[++i].name != NULL);
1030}
Adrian Huntere3e1a542013-08-14 15:48:24 +03001031
Peter Zijlstra2c5e8c52015-04-07 11:09:54 +02001032static void __p_sample_type(char *buf, size_t size, u64 value)
1033{
1034#define bit_name(n) { PERF_SAMPLE_##n, #n }
1035 struct bit_names bits[] = {
1036 bit_name(IP), bit_name(TID), bit_name(TIME), bit_name(ADDR),
1037 bit_name(READ), bit_name(CALLCHAIN), bit_name(ID), bit_name(CPU),
1038 bit_name(PERIOD), bit_name(STREAM_ID), bit_name(RAW),
1039 bit_name(BRANCH_STACK), bit_name(REGS_USER), bit_name(STACK_USER),
1040 bit_name(IDENTIFIER), bit_name(REGS_INTR),
1041 { .name = NULL, }
1042 };
1043#undef bit_name
1044 __p_bits(buf, size, value, bits);
1045}
Adrian Huntere3e1a542013-08-14 15:48:24 +03001046
Peter Zijlstra2c5e8c52015-04-07 11:09:54 +02001047static void __p_read_format(char *buf, size_t size, u64 value)
1048{
1049#define bit_name(n) { PERF_FORMAT_##n, #n }
1050 struct bit_names bits[] = {
1051 bit_name(TOTAL_TIME_ENABLED), bit_name(TOTAL_TIME_RUNNING),
1052 bit_name(ID), bit_name(GROUP),
1053 { .name = NULL, }
1054 };
1055#undef bit_name
1056 __p_bits(buf, size, value, bits);
1057}
Peter Zijlstra814c8c32015-03-31 00:19:31 +02001058
Peter Zijlstra2c5e8c52015-04-07 11:09:54 +02001059#define BUF_SIZE 1024
Adrian Huntere3e1a542013-08-14 15:48:24 +03001060
Peter Zijlstra2c5e8c52015-04-07 11:09:54 +02001061#define p_hex(val) snprintf(buf, BUF_SIZE, "%"PRIx64, (uint64_t)(val))
1062#define p_unsigned(val) snprintf(buf, BUF_SIZE, "%"PRIu64, (uint64_t)(val))
1063#define p_signed(val) snprintf(buf, BUF_SIZE, "%"PRId64, (int64_t)(val))
1064#define p_sample_type(val) __p_sample_type(buf, BUF_SIZE, val)
1065#define p_read_format(val) __p_read_format(buf, BUF_SIZE, val)
Adrian Huntere3e1a542013-08-14 15:48:24 +03001066
Peter Zijlstra2c5e8c52015-04-07 11:09:54 +02001067#define PRINT_ATTRn(_n, _f, _p) \
1068do { \
1069 if (attr->_f) { \
1070 _p(attr->_f); \
1071 ret += attr__fprintf(fp, _n, buf, priv);\
1072 } \
1073} while (0)
1074
1075#define PRINT_ATTRf(_f, _p) PRINT_ATTRn(#_f, _f, _p)
1076
1077int perf_event_attr__fprintf(FILE *fp, struct perf_event_attr *attr,
1078 attr__fprintf_f attr__fprintf, void *priv)
1079{
1080 char buf[BUF_SIZE];
1081 int ret = 0;
1082
1083 PRINT_ATTRf(type, p_unsigned);
1084 PRINT_ATTRf(size, p_unsigned);
1085 PRINT_ATTRf(config, p_hex);
1086 PRINT_ATTRn("{ sample_period, sample_freq }", sample_period, p_unsigned);
1087 PRINT_ATTRf(sample_type, p_sample_type);
1088 PRINT_ATTRf(read_format, p_read_format);
1089
1090 PRINT_ATTRf(disabled, p_unsigned);
1091 PRINT_ATTRf(inherit, p_unsigned);
1092 PRINT_ATTRf(pinned, p_unsigned);
1093 PRINT_ATTRf(exclusive, p_unsigned);
1094 PRINT_ATTRf(exclude_user, p_unsigned);
1095 PRINT_ATTRf(exclude_kernel, p_unsigned);
1096 PRINT_ATTRf(exclude_hv, p_unsigned);
1097 PRINT_ATTRf(exclude_idle, p_unsigned);
1098 PRINT_ATTRf(mmap, p_unsigned);
1099 PRINT_ATTRf(comm, p_unsigned);
1100 PRINT_ATTRf(freq, p_unsigned);
1101 PRINT_ATTRf(inherit_stat, p_unsigned);
1102 PRINT_ATTRf(enable_on_exec, p_unsigned);
1103 PRINT_ATTRf(task, p_unsigned);
1104 PRINT_ATTRf(watermark, p_unsigned);
1105 PRINT_ATTRf(precise_ip, p_unsigned);
1106 PRINT_ATTRf(mmap_data, p_unsigned);
1107 PRINT_ATTRf(sample_id_all, p_unsigned);
1108 PRINT_ATTRf(exclude_host, p_unsigned);
1109 PRINT_ATTRf(exclude_guest, p_unsigned);
1110 PRINT_ATTRf(exclude_callchain_kernel, p_unsigned);
1111 PRINT_ATTRf(exclude_callchain_user, p_unsigned);
1112 PRINT_ATTRf(mmap2, p_unsigned);
1113 PRINT_ATTRf(comm_exec, p_unsigned);
1114 PRINT_ATTRf(use_clockid, p_unsigned);
1115
1116 PRINT_ATTRn("{ wakeup_events, wakeup_watermark }", wakeup_events, p_unsigned);
1117 PRINT_ATTRf(bp_type, p_unsigned);
1118 PRINT_ATTRn("{ bp_addr, config1 }", bp_addr, p_hex);
1119 PRINT_ATTRn("{ bp_len, config2 }", bp_len, p_hex);
1120 PRINT_ATTRf(sample_regs_user, p_hex);
1121 PRINT_ATTRf(sample_stack_user, p_unsigned);
1122 PRINT_ATTRf(clockid, p_signed);
1123 PRINT_ATTRf(sample_regs_intr, p_hex);
Adrian Hunter70d73de2015-04-09 18:54:06 +03001124 PRINT_ATTRf(aux_watermark, p_unsigned);
Adrian Huntere3e1a542013-08-14 15:48:24 +03001125
1126 return ret;
1127}
1128
Peter Zijlstra2c5e8c52015-04-07 11:09:54 +02001129static int __open_attr__fprintf(FILE *fp, const char *name, const char *val,
1130 void *priv __attribute__((unused)))
1131{
1132 return fprintf(fp, " %-32s %s\n", name, val);
1133}
1134
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -02001135static int __perf_evsel__open(struct perf_evsel *evsel, struct cpu_map *cpus,
Jiri Olsa6a4bb042012-08-08 12:22:36 +02001136 struct thread_map *threads)
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -02001137{
Adrian Hunterbf8e8f42014-07-31 09:00:51 +03001138 int cpu, thread, nthreads;
Yann Droneaud57480d22014-06-30 22:28:47 +02001139 unsigned long flags = PERF_FLAG_FD_CLOEXEC;
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -02001140 int pid = -1, err;
Andi Kleenbec19672013-08-04 19:41:26 -07001141 enum { NO_CHANGE, SET_TO_MAX, INCREASED_MAX } set_rlimit = NO_CHANGE;
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -02001142
Adrian Hunterbf8e8f42014-07-31 09:00:51 +03001143 if (evsel->system_wide)
1144 nthreads = 1;
1145 else
1146 nthreads = threads->nr;
1147
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -02001148 if (evsel->fd == NULL &&
Adrian Hunterbf8e8f42014-07-31 09:00:51 +03001149 perf_evsel__alloc_fd(evsel, cpus->nr, nthreads) < 0)
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -02001150 return -ENOMEM;
Arnaldo Carvalho de Melo4eed11d2011-01-04 00:13:17 -02001151
Stephane Eranian023695d2011-02-14 11:20:01 +02001152 if (evsel->cgrp) {
Yann Droneaud57480d22014-06-30 22:28:47 +02001153 flags |= PERF_FLAG_PID_CGROUP;
Stephane Eranian023695d2011-02-14 11:20:01 +02001154 pid = evsel->cgrp->fd;
1155 }
1156
Arnaldo Carvalho de Melo594ac612012-12-13 13:13:07 -03001157fallback_missing_features:
Peter Zijlstra814c8c32015-03-31 00:19:31 +02001158 if (perf_missing_features.clockid_wrong)
1159 evsel->attr.clockid = CLOCK_MONOTONIC; /* should always work */
1160 if (perf_missing_features.clockid) {
1161 evsel->attr.use_clockid = 0;
1162 evsel->attr.clockid = 0;
1163 }
Yann Droneaud57480d22014-06-30 22:28:47 +02001164 if (perf_missing_features.cloexec)
1165 flags &= ~(unsigned long)PERF_FLAG_FD_CLOEXEC;
Stephane Eranian5c5e8542013-08-21 12:10:25 +02001166 if (perf_missing_features.mmap2)
1167 evsel->attr.mmap2 = 0;
Arnaldo Carvalho de Melo594ac612012-12-13 13:13:07 -03001168 if (perf_missing_features.exclude_guest)
1169 evsel->attr.exclude_guest = evsel->attr.exclude_host = 0;
1170retry_sample_id:
1171 if (perf_missing_features.sample_id_all)
1172 evsel->attr.sample_id_all = 0;
1173
Peter Zijlstra2c5e8c52015-04-07 11:09:54 +02001174 if (verbose >= 2) {
1175 fprintf(stderr, "%.60s\n", graph_dotted_line);
1176 fprintf(stderr, "perf_event_attr:\n");
1177 perf_event_attr__fprintf(stderr, &evsel->attr, __open_attr__fprintf, NULL);
1178 fprintf(stderr, "%.60s\n", graph_dotted_line);
1179 }
Adrian Huntere3e1a542013-08-14 15:48:24 +03001180
Arnaldo Carvalho de Melo86bd5e82011-01-03 23:09:46 -02001181 for (cpu = 0; cpu < cpus->nr; cpu++) {
Arnaldo Carvalho de Melo9d04f172011-01-12 00:08:18 -02001182
Adrian Hunterbf8e8f42014-07-31 09:00:51 +03001183 for (thread = 0; thread < nthreads; thread++) {
Jiri Olsa6a4bb042012-08-08 12:22:36 +02001184 int group_fd;
Stephane Eranian023695d2011-02-14 11:20:01 +02001185
Adrian Hunterbf8e8f42014-07-31 09:00:51 +03001186 if (!evsel->cgrp && !evsel->system_wide)
Stephane Eranian023695d2011-02-14 11:20:01 +02001187 pid = threads->map[thread];
1188
Jiri Olsa6a4bb042012-08-08 12:22:36 +02001189 group_fd = get_group_fd(evsel, cpu, thread);
Andi Kleenbec19672013-08-04 19:41:26 -07001190retry_open:
Ramkumar Ramachandraa33f6ef2014-03-18 15:10:42 -04001191 pr_debug2("sys_perf_event_open: pid %d cpu %d group_fd %d flags %#lx\n",
Adrian Huntere3e1a542013-08-14 15:48:24 +03001192 pid, cpus->map[cpu], group_fd, flags);
1193
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -02001194 FD(evsel, cpu, thread) = sys_perf_event_open(&evsel->attr,
Stephane Eranian023695d2011-02-14 11:20:01 +02001195 pid,
Arnaldo Carvalho de Melof08199d2011-01-11 23:42:19 -02001196 cpus->map[cpu],
Stephane Eranian023695d2011-02-14 11:20:01 +02001197 group_fd, flags);
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -02001198 if (FD(evsel, cpu, thread) < 0) {
1199 err = -errno;
Ramkumar Ramachandraa33f6ef2014-03-18 15:10:42 -04001200 pr_debug2("sys_perf_event_open failed, error %d\n",
Adrian Hunterf852fd62013-11-01 15:51:29 +02001201 err);
Arnaldo Carvalho de Melo594ac612012-12-13 13:13:07 -03001202 goto try_fallback;
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -02001203 }
Andi Kleenbec19672013-08-04 19:41:26 -07001204 set_rlimit = NO_CHANGE;
Peter Zijlstra814c8c32015-03-31 00:19:31 +02001205
1206 /*
1207 * If we succeeded but had to kill clockid, fail and
1208 * have perf_evsel__open_strerror() print us a nice
1209 * error.
1210 */
1211 if (perf_missing_features.clockid ||
1212 perf_missing_features.clockid_wrong) {
1213 err = -EINVAL;
1214 goto out_close;
1215 }
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -02001216 }
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -02001217 }
1218
1219 return 0;
1220
Arnaldo Carvalho de Melo594ac612012-12-13 13:13:07 -03001221try_fallback:
Andi Kleenbec19672013-08-04 19:41:26 -07001222 /*
1223 * perf stat needs between 5 and 22 fds per CPU. When we run out
1224 * of them try to increase the limits.
1225 */
1226 if (err == -EMFILE && set_rlimit < INCREASED_MAX) {
1227 struct rlimit l;
1228 int old_errno = errno;
1229
1230 if (getrlimit(RLIMIT_NOFILE, &l) == 0) {
1231 if (set_rlimit == NO_CHANGE)
1232 l.rlim_cur = l.rlim_max;
1233 else {
1234 l.rlim_cur = l.rlim_max + 1000;
1235 l.rlim_max = l.rlim_cur;
1236 }
1237 if (setrlimit(RLIMIT_NOFILE, &l) == 0) {
1238 set_rlimit++;
1239 errno = old_errno;
1240 goto retry_open;
1241 }
1242 }
1243 errno = old_errno;
1244 }
1245
Arnaldo Carvalho de Melo594ac612012-12-13 13:13:07 -03001246 if (err != -EINVAL || cpu > 0 || thread > 0)
1247 goto out_close;
1248
Peter Zijlstra814c8c32015-03-31 00:19:31 +02001249 /*
1250 * Must probe features in the order they were added to the
1251 * perf_event_attr interface.
1252 */
1253 if (!perf_missing_features.clockid_wrong && evsel->attr.use_clockid) {
1254 perf_missing_features.clockid_wrong = true;
1255 goto fallback_missing_features;
1256 } else if (!perf_missing_features.clockid && evsel->attr.use_clockid) {
1257 perf_missing_features.clockid = true;
1258 goto fallback_missing_features;
1259 } else if (!perf_missing_features.cloexec && (flags & PERF_FLAG_FD_CLOEXEC)) {
Yann Droneaud57480d22014-06-30 22:28:47 +02001260 perf_missing_features.cloexec = true;
1261 goto fallback_missing_features;
1262 } else if (!perf_missing_features.mmap2 && evsel->attr.mmap2) {
Stephane Eranian5c5e8542013-08-21 12:10:25 +02001263 perf_missing_features.mmap2 = true;
1264 goto fallback_missing_features;
1265 } else if (!perf_missing_features.exclude_guest &&
1266 (evsel->attr.exclude_guest || evsel->attr.exclude_host)) {
Arnaldo Carvalho de Melo594ac612012-12-13 13:13:07 -03001267 perf_missing_features.exclude_guest = true;
1268 goto fallback_missing_features;
1269 } else if (!perf_missing_features.sample_id_all) {
1270 perf_missing_features.sample_id_all = true;
1271 goto retry_sample_id;
1272 }
1273
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -02001274out_close:
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -02001275 do {
1276 while (--thread >= 0) {
1277 close(FD(evsel, cpu, thread));
1278 FD(evsel, cpu, thread) = -1;
1279 }
Adrian Hunterbf8e8f42014-07-31 09:00:51 +03001280 thread = nthreads;
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -02001281 } while (--cpu >= 0);
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -02001282 return err;
1283}
1284
1285void perf_evsel__close(struct perf_evsel *evsel, int ncpus, int nthreads)
1286{
1287 if (evsel->fd == NULL)
1288 return;
1289
1290 perf_evsel__close_fd(evsel, ncpus, nthreads);
1291 perf_evsel__free_fd(evsel);
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -02001292}
1293
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -02001294static struct {
1295 struct cpu_map map;
1296 int cpus[1];
1297} empty_cpu_map = {
1298 .map.nr = 1,
1299 .cpus = { -1, },
1300};
1301
1302static struct {
1303 struct thread_map map;
1304 int threads[1];
1305} empty_thread_map = {
1306 .map.nr = 1,
1307 .threads = { -1, },
1308};
1309
Arnaldo Carvalho de Melof08199d2011-01-11 23:42:19 -02001310int perf_evsel__open(struct perf_evsel *evsel, struct cpu_map *cpus,
Jiri Olsa6a4bb042012-08-08 12:22:36 +02001311 struct thread_map *threads)
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -02001312{
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -02001313 if (cpus == NULL) {
1314 /* Work around old compiler warnings about strict aliasing */
1315 cpus = &empty_cpu_map.map;
1316 }
1317
1318 if (threads == NULL)
1319 threads = &empty_thread_map.map;
1320
Jiri Olsa6a4bb042012-08-08 12:22:36 +02001321 return __perf_evsel__open(evsel, cpus, threads);
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -02001322}
1323
Arnaldo Carvalho de Melof08199d2011-01-11 23:42:19 -02001324int perf_evsel__open_per_cpu(struct perf_evsel *evsel,
Jiri Olsa6a4bb042012-08-08 12:22:36 +02001325 struct cpu_map *cpus)
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -02001326{
Jiri Olsa6a4bb042012-08-08 12:22:36 +02001327 return __perf_evsel__open(evsel, cpus, &empty_thread_map.map);
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -02001328}
1329
Arnaldo Carvalho de Melof08199d2011-01-11 23:42:19 -02001330int perf_evsel__open_per_thread(struct perf_evsel *evsel,
Jiri Olsa6a4bb042012-08-08 12:22:36 +02001331 struct thread_map *threads)
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -02001332{
Jiri Olsa6a4bb042012-08-08 12:22:36 +02001333 return __perf_evsel__open(evsel, &empty_cpu_map.map, threads);
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -02001334}
Arnaldo Carvalho de Melo70082dd2011-01-12 17:03:24 -02001335
Arnaldo Carvalho de Melo0807d2d2012-09-26 12:48:18 -03001336static int perf_evsel__parse_id_sample(const struct perf_evsel *evsel,
1337 const union perf_event *event,
1338 struct perf_sample *sample)
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001339{
Arnaldo Carvalho de Melo0807d2d2012-09-26 12:48:18 -03001340 u64 type = evsel->attr.sample_type;
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001341 const u64 *array = event->sample.array;
Arnaldo Carvalho de Melo0807d2d2012-09-26 12:48:18 -03001342 bool swapped = evsel->needs_swap;
Jiri Olsa37073f92012-05-30 14:23:44 +02001343 union u64_swap u;
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001344
1345 array += ((event->header.size -
1346 sizeof(event->header)) / sizeof(u64)) - 1;
1347
Adrian Hunter75562572013-08-27 11:23:09 +03001348 if (type & PERF_SAMPLE_IDENTIFIER) {
1349 sample->id = *array;
1350 array--;
1351 }
1352
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001353 if (type & PERF_SAMPLE_CPU) {
Jiri Olsa37073f92012-05-30 14:23:44 +02001354 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 }
1360
1361 sample->cpu = u.val32[0];
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001362 array--;
1363 }
1364
1365 if (type & PERF_SAMPLE_STREAM_ID) {
1366 sample->stream_id = *array;
1367 array--;
1368 }
1369
1370 if (type & PERF_SAMPLE_ID) {
1371 sample->id = *array;
1372 array--;
1373 }
1374
1375 if (type & PERF_SAMPLE_TIME) {
1376 sample->time = *array;
1377 array--;
1378 }
1379
1380 if (type & PERF_SAMPLE_TID) {
Jiri Olsa37073f92012-05-30 14:23:44 +02001381 u.val64 = *array;
1382 if (swapped) {
1383 /* undo swap of u64, then swap on individual u32s */
1384 u.val64 = bswap_64(u.val64);
1385 u.val32[0] = bswap_32(u.val32[0]);
1386 u.val32[1] = bswap_32(u.val32[1]);
1387 }
1388
1389 sample->pid = u.val32[0];
1390 sample->tid = u.val32[1];
Adrian Hunterdd44bc62013-10-18 15:29:01 +03001391 array--;
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001392 }
1393
1394 return 0;
1395}
1396
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001397static inline bool overflow(const void *endp, u16 max_size, const void *offset,
1398 u64 size)
Frederic Weisbecker98e1da92011-05-21 20:08:15 +02001399{
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001400 return size > max_size || offset + size > endp;
Frederic Weisbecker98e1da92011-05-21 20:08:15 +02001401}
1402
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001403#define OVERFLOW_CHECK(offset, size, max_size) \
1404 do { \
1405 if (overflow(endp, (max_size), (offset), (size))) \
1406 return -EFAULT; \
1407 } while (0)
1408
1409#define OVERFLOW_CHECK_u64(offset) \
1410 OVERFLOW_CHECK(offset, sizeof(u64), sizeof(u64))
1411
Arnaldo Carvalho de Meloa3f698f2012-08-02 12:23:46 -03001412int perf_evsel__parse_sample(struct perf_evsel *evsel, union perf_event *event,
Arnaldo Carvalho de Melo0807d2d2012-09-26 12:48:18 -03001413 struct perf_sample *data)
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001414{
Arnaldo Carvalho de Meloa3f698f2012-08-02 12:23:46 -03001415 u64 type = evsel->attr.sample_type;
Arnaldo Carvalho de Melo0807d2d2012-09-26 12:48:18 -03001416 bool swapped = evsel->needs_swap;
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001417 const u64 *array;
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001418 u16 max_size = event->header.size;
1419 const void *endp = (void *)event + max_size;
1420 u64 sz;
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001421
David Ahern936be502011-09-06 09:12:26 -06001422 /*
1423 * used for cross-endian analysis. See git commit 65014ab3
1424 * for why this goofiness is needed.
1425 */
Jiri Olsa6a11f922012-05-16 08:59:04 +02001426 union u64_swap u;
David Ahern936be502011-09-06 09:12:26 -06001427
Robert Richterf3bda2c2011-12-15 17:32:39 +01001428 memset(data, 0, sizeof(*data));
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001429 data->cpu = data->pid = data->tid = -1;
1430 data->stream_id = data->id = data->time = -1ULL;
Jiri Olsabc529082014-02-03 12:44:41 +01001431 data->period = evsel->attr.sample_period;
Andi Kleen05484292013-01-24 16:10:29 +01001432 data->weight = 0;
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001433
1434 if (event->header.type != PERF_RECORD_SAMPLE) {
Arnaldo Carvalho de Meloa3f698f2012-08-02 12:23:46 -03001435 if (!evsel->attr.sample_id_all)
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001436 return 0;
Arnaldo Carvalho de Melo0807d2d2012-09-26 12:48:18 -03001437 return perf_evsel__parse_id_sample(evsel, event, data);
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001438 }
1439
1440 array = event->sample.array;
1441
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001442 /*
1443 * The evsel's sample_size is based on PERF_SAMPLE_MASK which includes
1444 * up to PERF_SAMPLE_PERIOD. After that overflow() must be used to
1445 * check the format does not go past the end of the event.
1446 */
Arnaldo Carvalho de Meloa3f698f2012-08-02 12:23:46 -03001447 if (evsel->sample_size + sizeof(event->header) > event->header.size)
Frederic Weisbeckera2854122011-05-21 19:33:04 +02001448 return -EFAULT;
1449
Adrian Hunter75562572013-08-27 11:23:09 +03001450 data->id = -1ULL;
1451 if (type & PERF_SAMPLE_IDENTIFIER) {
1452 data->id = *array;
1453 array++;
1454 }
1455
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001456 if (type & PERF_SAMPLE_IP) {
Adrian Hunteref893252013-08-27 11:23:06 +03001457 data->ip = *array;
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001458 array++;
1459 }
1460
1461 if (type & PERF_SAMPLE_TID) {
David Ahern936be502011-09-06 09:12:26 -06001462 u.val64 = *array;
1463 if (swapped) {
1464 /* undo swap of u64, then swap on individual u32s */
1465 u.val64 = bswap_64(u.val64);
1466 u.val32[0] = bswap_32(u.val32[0]);
1467 u.val32[1] = bswap_32(u.val32[1]);
1468 }
1469
1470 data->pid = u.val32[0];
1471 data->tid = u.val32[1];
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001472 array++;
1473 }
1474
1475 if (type & PERF_SAMPLE_TIME) {
1476 data->time = *array;
1477 array++;
1478 }
1479
David Ahern7cec0922011-05-30 13:08:23 -06001480 data->addr = 0;
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001481 if (type & PERF_SAMPLE_ADDR) {
1482 data->addr = *array;
1483 array++;
1484 }
1485
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001486 if (type & PERF_SAMPLE_ID) {
1487 data->id = *array;
1488 array++;
1489 }
1490
1491 if (type & PERF_SAMPLE_STREAM_ID) {
1492 data->stream_id = *array;
1493 array++;
1494 }
1495
1496 if (type & PERF_SAMPLE_CPU) {
David Ahern936be502011-09-06 09:12:26 -06001497
1498 u.val64 = *array;
1499 if (swapped) {
1500 /* undo swap of u64, then swap on individual u32s */
1501 u.val64 = bswap_64(u.val64);
1502 u.val32[0] = bswap_32(u.val32[0]);
1503 }
1504
1505 data->cpu = u.val32[0];
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001506 array++;
1507 }
1508
1509 if (type & PERF_SAMPLE_PERIOD) {
1510 data->period = *array;
1511 array++;
1512 }
1513
1514 if (type & PERF_SAMPLE_READ) {
Jiri Olsa9ede4732012-10-10 17:38:13 +02001515 u64 read_format = evsel->attr.read_format;
1516
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001517 OVERFLOW_CHECK_u64(array);
Jiri Olsa9ede4732012-10-10 17:38:13 +02001518 if (read_format & PERF_FORMAT_GROUP)
1519 data->read.group.nr = *array;
1520 else
1521 data->read.one.value = *array;
1522
1523 array++;
1524
1525 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) {
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001526 OVERFLOW_CHECK_u64(array);
Jiri Olsa9ede4732012-10-10 17:38:13 +02001527 data->read.time_enabled = *array;
1528 array++;
1529 }
1530
1531 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) {
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001532 OVERFLOW_CHECK_u64(array);
Jiri Olsa9ede4732012-10-10 17:38:13 +02001533 data->read.time_running = *array;
1534 array++;
1535 }
1536
1537 /* PERF_FORMAT_ID is forced for PERF_SAMPLE_READ */
1538 if (read_format & PERF_FORMAT_GROUP) {
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001539 const u64 max_group_nr = UINT64_MAX /
1540 sizeof(struct sample_read_value);
1541
1542 if (data->read.group.nr > max_group_nr)
1543 return -EFAULT;
1544 sz = data->read.group.nr *
1545 sizeof(struct sample_read_value);
1546 OVERFLOW_CHECK(array, sz, max_size);
1547 data->read.group.values =
1548 (struct sample_read_value *)array;
1549 array = (void *)array + sz;
Jiri Olsa9ede4732012-10-10 17:38:13 +02001550 } else {
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001551 OVERFLOW_CHECK_u64(array);
Jiri Olsa9ede4732012-10-10 17:38:13 +02001552 data->read.one.id = *array;
1553 array++;
1554 }
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001555 }
1556
1557 if (type & PERF_SAMPLE_CALLCHAIN) {
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001558 const u64 max_callchain_nr = UINT64_MAX / sizeof(u64);
1559
1560 OVERFLOW_CHECK_u64(array);
1561 data->callchain = (struct ip_callchain *)array++;
1562 if (data->callchain->nr > max_callchain_nr)
Frederic Weisbecker98e1da92011-05-21 20:08:15 +02001563 return -EFAULT;
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001564 sz = data->callchain->nr * sizeof(u64);
1565 OVERFLOW_CHECK(array, sz, max_size);
1566 array = (void *)array + sz;
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001567 }
1568
1569 if (type & PERF_SAMPLE_RAW) {
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001570 OVERFLOW_CHECK_u64(array);
David Ahern936be502011-09-06 09:12:26 -06001571 u.val64 = *array;
1572 if (WARN_ONCE(swapped,
1573 "Endianness of raw data not corrected!\n")) {
1574 /* undo swap of u64, then swap on individual u32s */
1575 u.val64 = bswap_64(u.val64);
1576 u.val32[0] = bswap_32(u.val32[0]);
1577 u.val32[1] = bswap_32(u.val32[1]);
1578 }
David Ahern936be502011-09-06 09:12:26 -06001579 data->raw_size = u.val32[0];
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001580 array = (void *)array + sizeof(u32);
Frederic Weisbecker98e1da92011-05-21 20:08:15 +02001581
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001582 OVERFLOW_CHECK(array, data->raw_size, max_size);
1583 data->raw_data = (void *)array;
1584 array = (void *)array + data->raw_size;
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001585 }
1586
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +01001587 if (type & PERF_SAMPLE_BRANCH_STACK) {
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001588 const u64 max_branch_nr = UINT64_MAX /
1589 sizeof(struct branch_entry);
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +01001590
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001591 OVERFLOW_CHECK_u64(array);
1592 data->branch_stack = (struct branch_stack *)array++;
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +01001593
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001594 if (data->branch_stack->nr > max_branch_nr)
1595 return -EFAULT;
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +01001596 sz = data->branch_stack->nr * sizeof(struct branch_entry);
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001597 OVERFLOW_CHECK(array, sz, max_size);
1598 array = (void *)array + sz;
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +01001599 }
Jiri Olsa0f6a3012012-08-07 15:20:45 +02001600
1601 if (type & PERF_SAMPLE_REGS_USER) {
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001602 OVERFLOW_CHECK_u64(array);
Adrian Hunter5b95a4a32013-08-27 11:23:10 +03001603 data->user_regs.abi = *array;
1604 array++;
Jiri Olsa0f6a3012012-08-07 15:20:45 +02001605
Adrian Hunter5b95a4a32013-08-27 11:23:10 +03001606 if (data->user_regs.abi) {
Jiri Olsa352ea452014-01-07 13:47:25 +01001607 u64 mask = evsel->attr.sample_regs_user;
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001608
Jiri Olsa352ea452014-01-07 13:47:25 +01001609 sz = hweight_long(mask) * sizeof(u64);
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001610 OVERFLOW_CHECK(array, sz, max_size);
Jiri Olsa352ea452014-01-07 13:47:25 +01001611 data->user_regs.mask = mask;
Jiri Olsa0f6a3012012-08-07 15:20:45 +02001612 data->user_regs.regs = (u64 *)array;
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001613 array = (void *)array + sz;
Jiri Olsa0f6a3012012-08-07 15:20:45 +02001614 }
1615 }
1616
1617 if (type & PERF_SAMPLE_STACK_USER) {
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001618 OVERFLOW_CHECK_u64(array);
1619 sz = *array++;
Jiri Olsa0f6a3012012-08-07 15:20:45 +02001620
1621 data->user_stack.offset = ((char *)(array - 1)
1622 - (char *) event);
1623
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001624 if (!sz) {
Jiri Olsa0f6a3012012-08-07 15:20:45 +02001625 data->user_stack.size = 0;
1626 } else {
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001627 OVERFLOW_CHECK(array, sz, max_size);
Jiri Olsa0f6a3012012-08-07 15:20:45 +02001628 data->user_stack.data = (char *)array;
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001629 array = (void *)array + sz;
1630 OVERFLOW_CHECK_u64(array);
Adrian Hunter54bd2692013-07-04 16:20:34 +03001631 data->user_stack.size = *array++;
Jiri Olsaa65cb4b2013-10-02 15:46:39 +02001632 if (WARN_ONCE(data->user_stack.size > sz,
1633 "user stack dump failure\n"))
1634 return -EFAULT;
Jiri Olsa0f6a3012012-08-07 15:20:45 +02001635 }
1636 }
1637
Andi Kleen05484292013-01-24 16:10:29 +01001638 data->weight = 0;
1639 if (type & PERF_SAMPLE_WEIGHT) {
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001640 OVERFLOW_CHECK_u64(array);
Andi Kleen05484292013-01-24 16:10:29 +01001641 data->weight = *array;
1642 array++;
1643 }
1644
Stephane Eranian98a3b322013-01-24 16:10:35 +01001645 data->data_src = PERF_MEM_DATA_SRC_NONE;
1646 if (type & PERF_SAMPLE_DATA_SRC) {
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001647 OVERFLOW_CHECK_u64(array);
Stephane Eranian98a3b322013-01-24 16:10:35 +01001648 data->data_src = *array;
1649 array++;
1650 }
1651
Andi Kleen475eeab2013-09-20 07:40:43 -07001652 data->transaction = 0;
1653 if (type & PERF_SAMPLE_TRANSACTION) {
Adrian Hunter87b95522013-11-01 15:51:36 +02001654 OVERFLOW_CHECK_u64(array);
Andi Kleen475eeab2013-09-20 07:40:43 -07001655 data->transaction = *array;
1656 array++;
1657 }
1658
Stephane Eranian6a21c0b2014-09-24 13:48:39 +02001659 data->intr_regs.abi = PERF_SAMPLE_REGS_ABI_NONE;
1660 if (type & PERF_SAMPLE_REGS_INTR) {
1661 OVERFLOW_CHECK_u64(array);
1662 data->intr_regs.abi = *array;
1663 array++;
1664
1665 if (data->intr_regs.abi != PERF_SAMPLE_REGS_ABI_NONE) {
1666 u64 mask = evsel->attr.sample_regs_intr;
1667
1668 sz = hweight_long(mask) * sizeof(u64);
1669 OVERFLOW_CHECK(array, sz, max_size);
1670 data->intr_regs.mask = mask;
1671 data->intr_regs.regs = (u64 *)array;
1672 array = (void *)array + sz;
1673 }
1674 }
1675
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001676 return 0;
1677}
Andrew Vagin74eec262011-11-28 12:03:31 +03001678
Adrian Hunterb1cf6f62013-08-27 11:23:12 +03001679size_t perf_event__sample_event_size(const struct perf_sample *sample, u64 type,
Jiri Olsa352ea452014-01-07 13:47:25 +01001680 u64 read_format)
Adrian Hunterb1cf6f62013-08-27 11:23:12 +03001681{
1682 size_t sz, result = sizeof(struct sample_event);
1683
1684 if (type & PERF_SAMPLE_IDENTIFIER)
1685 result += sizeof(u64);
1686
1687 if (type & PERF_SAMPLE_IP)
1688 result += sizeof(u64);
1689
1690 if (type & PERF_SAMPLE_TID)
1691 result += sizeof(u64);
1692
1693 if (type & PERF_SAMPLE_TIME)
1694 result += sizeof(u64);
1695
1696 if (type & PERF_SAMPLE_ADDR)
1697 result += sizeof(u64);
1698
1699 if (type & PERF_SAMPLE_ID)
1700 result += sizeof(u64);
1701
1702 if (type & PERF_SAMPLE_STREAM_ID)
1703 result += sizeof(u64);
1704
1705 if (type & PERF_SAMPLE_CPU)
1706 result += sizeof(u64);
1707
1708 if (type & PERF_SAMPLE_PERIOD)
1709 result += sizeof(u64);
1710
1711 if (type & PERF_SAMPLE_READ) {
1712 result += sizeof(u64);
1713 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
1714 result += sizeof(u64);
1715 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
1716 result += sizeof(u64);
1717 /* PERF_FORMAT_ID is forced for PERF_SAMPLE_READ */
1718 if (read_format & PERF_FORMAT_GROUP) {
1719 sz = sample->read.group.nr *
1720 sizeof(struct sample_read_value);
1721 result += sz;
1722 } else {
1723 result += sizeof(u64);
1724 }
1725 }
1726
1727 if (type & PERF_SAMPLE_CALLCHAIN) {
1728 sz = (sample->callchain->nr + 1) * sizeof(u64);
1729 result += sz;
1730 }
1731
1732 if (type & PERF_SAMPLE_RAW) {
1733 result += sizeof(u32);
1734 result += sample->raw_size;
1735 }
1736
1737 if (type & PERF_SAMPLE_BRANCH_STACK) {
1738 sz = sample->branch_stack->nr * sizeof(struct branch_entry);
1739 sz += sizeof(u64);
1740 result += sz;
1741 }
1742
1743 if (type & PERF_SAMPLE_REGS_USER) {
1744 if (sample->user_regs.abi) {
1745 result += sizeof(u64);
Jiri Olsa352ea452014-01-07 13:47:25 +01001746 sz = hweight_long(sample->user_regs.mask) * sizeof(u64);
Adrian Hunterb1cf6f62013-08-27 11:23:12 +03001747 result += sz;
1748 } else {
1749 result += sizeof(u64);
1750 }
1751 }
1752
1753 if (type & PERF_SAMPLE_STACK_USER) {
1754 sz = sample->user_stack.size;
1755 result += sizeof(u64);
1756 if (sz) {
1757 result += sz;
1758 result += sizeof(u64);
1759 }
1760 }
1761
1762 if (type & PERF_SAMPLE_WEIGHT)
1763 result += sizeof(u64);
1764
1765 if (type & PERF_SAMPLE_DATA_SRC)
1766 result += sizeof(u64);
1767
Adrian Hunter42d88912013-11-01 15:51:38 +02001768 if (type & PERF_SAMPLE_TRANSACTION)
1769 result += sizeof(u64);
1770
Stephane Eranian6a21c0b2014-09-24 13:48:39 +02001771 if (type & PERF_SAMPLE_REGS_INTR) {
1772 if (sample->intr_regs.abi) {
1773 result += sizeof(u64);
1774 sz = hweight_long(sample->intr_regs.mask) * sizeof(u64);
1775 result += sz;
1776 } else {
1777 result += sizeof(u64);
1778 }
1779 }
1780
Adrian Hunterb1cf6f62013-08-27 11:23:12 +03001781 return result;
1782}
1783
Andrew Vagin74eec262011-11-28 12:03:31 +03001784int perf_event__synthesize_sample(union perf_event *event, u64 type,
Jiri Olsa352ea452014-01-07 13:47:25 +01001785 u64 read_format,
Andrew Vagin74eec262011-11-28 12:03:31 +03001786 const struct perf_sample *sample,
1787 bool swapped)
1788{
1789 u64 *array;
Adrian Hunterd03f2172013-08-27 11:23:11 +03001790 size_t sz;
Andrew Vagin74eec262011-11-28 12:03:31 +03001791 /*
1792 * used for cross-endian analysis. See git commit 65014ab3
1793 * for why this goofiness is needed.
1794 */
Jiri Olsa6a11f922012-05-16 08:59:04 +02001795 union u64_swap u;
Andrew Vagin74eec262011-11-28 12:03:31 +03001796
1797 array = event->sample.array;
1798
Adrian Hunter75562572013-08-27 11:23:09 +03001799 if (type & PERF_SAMPLE_IDENTIFIER) {
1800 *array = sample->id;
1801 array++;
1802 }
1803
Andrew Vagin74eec262011-11-28 12:03:31 +03001804 if (type & PERF_SAMPLE_IP) {
Adrian Hunteref893252013-08-27 11:23:06 +03001805 *array = sample->ip;
Andrew Vagin74eec262011-11-28 12:03:31 +03001806 array++;
1807 }
1808
1809 if (type & PERF_SAMPLE_TID) {
1810 u.val32[0] = sample->pid;
1811 u.val32[1] = sample->tid;
1812 if (swapped) {
1813 /*
Arnaldo Carvalho de Meloa3f698f2012-08-02 12:23:46 -03001814 * Inverse of what is done in perf_evsel__parse_sample
Andrew Vagin74eec262011-11-28 12:03:31 +03001815 */
1816 u.val32[0] = bswap_32(u.val32[0]);
1817 u.val32[1] = bswap_32(u.val32[1]);
1818 u.val64 = bswap_64(u.val64);
1819 }
1820
1821 *array = u.val64;
1822 array++;
1823 }
1824
1825 if (type & PERF_SAMPLE_TIME) {
1826 *array = sample->time;
1827 array++;
1828 }
1829
1830 if (type & PERF_SAMPLE_ADDR) {
1831 *array = sample->addr;
1832 array++;
1833 }
1834
1835 if (type & PERF_SAMPLE_ID) {
1836 *array = sample->id;
1837 array++;
1838 }
1839
1840 if (type & PERF_SAMPLE_STREAM_ID) {
1841 *array = sample->stream_id;
1842 array++;
1843 }
1844
1845 if (type & PERF_SAMPLE_CPU) {
1846 u.val32[0] = sample->cpu;
1847 if (swapped) {
1848 /*
Arnaldo Carvalho de Meloa3f698f2012-08-02 12:23:46 -03001849 * Inverse of what is done in perf_evsel__parse_sample
Andrew Vagin74eec262011-11-28 12:03:31 +03001850 */
1851 u.val32[0] = bswap_32(u.val32[0]);
1852 u.val64 = bswap_64(u.val64);
1853 }
1854 *array = u.val64;
1855 array++;
1856 }
1857
1858 if (type & PERF_SAMPLE_PERIOD) {
1859 *array = sample->period;
1860 array++;
1861 }
1862
Adrian Hunterd03f2172013-08-27 11:23:11 +03001863 if (type & PERF_SAMPLE_READ) {
1864 if (read_format & PERF_FORMAT_GROUP)
1865 *array = sample->read.group.nr;
1866 else
1867 *array = sample->read.one.value;
1868 array++;
1869
1870 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) {
1871 *array = sample->read.time_enabled;
1872 array++;
1873 }
1874
1875 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) {
1876 *array = sample->read.time_running;
1877 array++;
1878 }
1879
1880 /* PERF_FORMAT_ID is forced for PERF_SAMPLE_READ */
1881 if (read_format & PERF_FORMAT_GROUP) {
1882 sz = sample->read.group.nr *
1883 sizeof(struct sample_read_value);
1884 memcpy(array, sample->read.group.values, sz);
1885 array = (void *)array + sz;
1886 } else {
1887 *array = sample->read.one.id;
1888 array++;
1889 }
1890 }
1891
1892 if (type & PERF_SAMPLE_CALLCHAIN) {
1893 sz = (sample->callchain->nr + 1) * sizeof(u64);
1894 memcpy(array, sample->callchain, sz);
1895 array = (void *)array + sz;
1896 }
1897
1898 if (type & PERF_SAMPLE_RAW) {
1899 u.val32[0] = sample->raw_size;
1900 if (WARN_ONCE(swapped,
1901 "Endianness of raw data not corrected!\n")) {
1902 /*
1903 * Inverse of what is done in perf_evsel__parse_sample
1904 */
1905 u.val32[0] = bswap_32(u.val32[0]);
1906 u.val32[1] = bswap_32(u.val32[1]);
1907 u.val64 = bswap_64(u.val64);
1908 }
1909 *array = u.val64;
1910 array = (void *)array + sizeof(u32);
1911
1912 memcpy(array, sample->raw_data, sample->raw_size);
1913 array = (void *)array + sample->raw_size;
1914 }
1915
1916 if (type & PERF_SAMPLE_BRANCH_STACK) {
1917 sz = sample->branch_stack->nr * sizeof(struct branch_entry);
1918 sz += sizeof(u64);
1919 memcpy(array, sample->branch_stack, sz);
1920 array = (void *)array + sz;
1921 }
1922
1923 if (type & PERF_SAMPLE_REGS_USER) {
1924 if (sample->user_regs.abi) {
1925 *array++ = sample->user_regs.abi;
Jiri Olsa352ea452014-01-07 13:47:25 +01001926 sz = hweight_long(sample->user_regs.mask) * sizeof(u64);
Adrian Hunterd03f2172013-08-27 11:23:11 +03001927 memcpy(array, sample->user_regs.regs, sz);
1928 array = (void *)array + sz;
1929 } else {
1930 *array++ = 0;
1931 }
1932 }
1933
1934 if (type & PERF_SAMPLE_STACK_USER) {
1935 sz = sample->user_stack.size;
1936 *array++ = sz;
1937 if (sz) {
1938 memcpy(array, sample->user_stack.data, sz);
1939 array = (void *)array + sz;
1940 *array++ = sz;
1941 }
1942 }
1943
1944 if (type & PERF_SAMPLE_WEIGHT) {
1945 *array = sample->weight;
1946 array++;
1947 }
1948
1949 if (type & PERF_SAMPLE_DATA_SRC) {
1950 *array = sample->data_src;
1951 array++;
1952 }
1953
Adrian Hunter42d88912013-11-01 15:51:38 +02001954 if (type & PERF_SAMPLE_TRANSACTION) {
1955 *array = sample->transaction;
1956 array++;
1957 }
1958
Stephane Eranian6a21c0b2014-09-24 13:48:39 +02001959 if (type & PERF_SAMPLE_REGS_INTR) {
1960 if (sample->intr_regs.abi) {
1961 *array++ = sample->intr_regs.abi;
1962 sz = hweight_long(sample->intr_regs.mask) * sizeof(u64);
1963 memcpy(array, sample->intr_regs.regs, sz);
1964 array = (void *)array + sz;
1965 } else {
1966 *array++ = 0;
1967 }
1968 }
1969
Andrew Vagin74eec262011-11-28 12:03:31 +03001970 return 0;
1971}
Arnaldo Carvalho de Melo5555ded2012-09-11 19:24:23 -03001972
Arnaldo Carvalho de Meloefd2b922012-09-18 11:21:50 -03001973struct format_field *perf_evsel__field(struct perf_evsel *evsel, const char *name)
1974{
1975 return pevent_find_field(evsel->tp_format, name);
1976}
1977
Arnaldo Carvalho de Melo5d2074e2012-09-26 20:22:00 -03001978void *perf_evsel__rawptr(struct perf_evsel *evsel, struct perf_sample *sample,
Arnaldo Carvalho de Melo5555ded2012-09-11 19:24:23 -03001979 const char *name)
1980{
Arnaldo Carvalho de Meloefd2b922012-09-18 11:21:50 -03001981 struct format_field *field = perf_evsel__field(evsel, name);
Arnaldo Carvalho de Melo5555ded2012-09-11 19:24:23 -03001982 int offset;
1983
Arnaldo Carvalho de Meloefd2b922012-09-18 11:21:50 -03001984 if (!field)
1985 return NULL;
Arnaldo Carvalho de Melo5555ded2012-09-11 19:24:23 -03001986
1987 offset = field->offset;
1988
1989 if (field->flags & FIELD_IS_DYNAMIC) {
1990 offset = *(int *)(sample->raw_data + field->offset);
1991 offset &= 0xffff;
1992 }
1993
1994 return sample->raw_data + offset;
1995}
1996
1997u64 perf_evsel__intval(struct perf_evsel *evsel, struct perf_sample *sample,
1998 const char *name)
1999{
Arnaldo Carvalho de Meloefd2b922012-09-18 11:21:50 -03002000 struct format_field *field = perf_evsel__field(evsel, name);
Arnaldo Carvalho de Meloe6b6f672012-09-26 13:13:04 -03002001 void *ptr;
2002 u64 value;
Arnaldo Carvalho de Melo5555ded2012-09-11 19:24:23 -03002003
Arnaldo Carvalho de Meloefd2b922012-09-18 11:21:50 -03002004 if (!field)
2005 return 0;
Arnaldo Carvalho de Melo5555ded2012-09-11 19:24:23 -03002006
Arnaldo Carvalho de Meloe6b6f672012-09-26 13:13:04 -03002007 ptr = sample->raw_data + field->offset;
Arnaldo Carvalho de Melo5555ded2012-09-11 19:24:23 -03002008
Arnaldo Carvalho de Meloe6b6f672012-09-26 13:13:04 -03002009 switch (field->size) {
2010 case 1:
2011 return *(u8 *)ptr;
2012 case 2:
2013 value = *(u16 *)ptr;
2014 break;
2015 case 4:
2016 value = *(u32 *)ptr;
2017 break;
2018 case 8:
David Aherne94eeda2015-03-24 16:14:09 -04002019 memcpy(&value, ptr, sizeof(u64));
Arnaldo Carvalho de Meloe6b6f672012-09-26 13:13:04 -03002020 break;
2021 default:
2022 return 0;
2023 }
2024
2025 if (!evsel->needs_swap)
2026 return value;
2027
2028 switch (field->size) {
2029 case 2:
2030 return bswap_16(value);
2031 case 4:
2032 return bswap_32(value);
2033 case 8:
2034 return bswap_64(value);
2035 default:
2036 return 0;
2037 }
2038
2039 return 0;
Arnaldo Carvalho de Melo5555ded2012-09-11 19:24:23 -03002040}
Arnaldo Carvalho de Melo0698aed2012-12-10 18:17:08 -03002041
2042static int comma_fprintf(FILE *fp, bool *first, const char *fmt, ...)
2043{
2044 va_list args;
2045 int ret = 0;
2046
2047 if (!*first) {
2048 ret += fprintf(fp, ",");
2049 } else {
2050 ret += fprintf(fp, ":");
2051 *first = false;
2052 }
2053
2054 va_start(args, fmt);
2055 ret += vfprintf(fp, fmt, args);
2056 va_end(args);
2057 return ret;
2058}
2059
Peter Zijlstra2c5e8c52015-04-07 11:09:54 +02002060static int __print_attr__fprintf(FILE *fp, const char *name, const char *val, void *priv)
Arnaldo Carvalho de Melo0698aed2012-12-10 18:17:08 -03002061{
Peter Zijlstra2c5e8c52015-04-07 11:09:54 +02002062 return comma_fprintf(fp, (bool *)priv, " %s: %s", name, val);
Arnaldo Carvalho de Meloc79a4392012-12-11 10:54:12 -03002063}
2064
Arnaldo Carvalho de Melo0698aed2012-12-10 18:17:08 -03002065int perf_evsel__fprintf(struct perf_evsel *evsel,
2066 struct perf_attr_details *details, FILE *fp)
2067{
2068 bool first = true;
Namhyung Kime6ab07d2013-01-22 18:09:47 +09002069 int printed = 0;
2070
Arnaldo Carvalho de Meloe35ef3552013-02-06 17:20:02 -03002071 if (details->event_group) {
Namhyung Kime6ab07d2013-01-22 18:09:47 +09002072 struct perf_evsel *pos;
2073
2074 if (!perf_evsel__is_group_leader(evsel))
2075 return 0;
2076
2077 if (evsel->nr_members > 1)
2078 printed += fprintf(fp, "%s{", evsel->group_name ?: "");
2079
2080 printed += fprintf(fp, "%s", perf_evsel__name(evsel));
2081 for_each_group_member(pos, evsel)
2082 printed += fprintf(fp, ",%s", perf_evsel__name(pos));
2083
2084 if (evsel->nr_members > 1)
2085 printed += fprintf(fp, "}");
2086 goto out;
2087 }
2088
2089 printed += fprintf(fp, "%s", perf_evsel__name(evsel));
Arnaldo Carvalho de Melo0698aed2012-12-10 18:17:08 -03002090
Peter Zijlstra2c5e8c52015-04-07 11:09:54 +02002091 if (details->verbose) {
2092 printed += perf_event_attr__fprintf(fp, &evsel->attr,
2093 __print_attr__fprintf, &first);
2094 } else if (details->freq) {
Arnaldo Carvalho de Melo0698aed2012-12-10 18:17:08 -03002095 printed += comma_fprintf(fp, &first, " sample_freq=%" PRIu64,
2096 (u64)evsel->attr.sample_freq);
2097 }
Namhyung Kime6ab07d2013-01-22 18:09:47 +09002098out:
Arnaldo Carvalho de Melo0698aed2012-12-10 18:17:08 -03002099 fputc('\n', fp);
2100 return ++printed;
2101}
Arnaldo Carvalho de Meloc0a54342012-12-13 14:16:30 -03002102
2103bool perf_evsel__fallback(struct perf_evsel *evsel, int err,
2104 char *msg, size_t msgsize)
2105{
David Ahern2b821cc2013-07-18 17:27:59 -06002106 if ((err == ENOENT || err == ENXIO || err == ENODEV) &&
Arnaldo Carvalho de Meloc0a54342012-12-13 14:16:30 -03002107 evsel->attr.type == PERF_TYPE_HARDWARE &&
2108 evsel->attr.config == PERF_COUNT_HW_CPU_CYCLES) {
2109 /*
2110 * If it's cycles then fall back to hrtimer based
2111 * cpu-clock-tick sw counter, which is always available even if
2112 * no PMU support.
2113 *
2114 * PPC returns ENXIO until 2.6.37 (behavior changed with commit
2115 * b0a873e).
2116 */
2117 scnprintf(msg, msgsize, "%s",
2118"The cycles event is not supported, trying to fall back to cpu-clock-ticks");
2119
2120 evsel->attr.type = PERF_TYPE_SOFTWARE;
2121 evsel->attr.config = PERF_COUNT_SW_CPU_CLOCK;
2122
Arnaldo Carvalho de Melo04662522013-12-26 17:41:15 -03002123 zfree(&evsel->name);
Arnaldo Carvalho de Meloc0a54342012-12-13 14:16:30 -03002124 return true;
2125 }
2126
2127 return false;
2128}
Arnaldo Carvalho de Melo56e52e82012-12-13 15:10:58 -03002129
Arnaldo Carvalho de Melo602ad872013-11-12 16:46:16 -03002130int perf_evsel__open_strerror(struct perf_evsel *evsel, struct target *target,
Arnaldo Carvalho de Melo56e52e82012-12-13 15:10:58 -03002131 int err, char *msg, size_t size)
2132{
Masami Hiramatsu6e81c742014-08-14 02:22:36 +00002133 char sbuf[STRERR_BUFSIZE];
2134
Arnaldo Carvalho de Melo56e52e82012-12-13 15:10:58 -03002135 switch (err) {
2136 case EPERM:
2137 case EACCES:
David Ahernb69e63a2013-05-25 17:54:00 -06002138 return scnprintf(msg, size,
Arnaldo Carvalho de Melo56e52e82012-12-13 15:10:58 -03002139 "You may not have permission to collect %sstats.\n"
2140 "Consider tweaking /proc/sys/kernel/perf_event_paranoid:\n"
2141 " -1 - Not paranoid at all\n"
2142 " 0 - Disallow raw tracepoint access for unpriv\n"
2143 " 1 - Disallow cpu events for unpriv\n"
2144 " 2 - Disallow kernel profiling for unpriv",
2145 target->system_wide ? "system-wide " : "");
2146 case ENOENT:
2147 return scnprintf(msg, size, "The %s event is not supported.",
2148 perf_evsel__name(evsel));
2149 case EMFILE:
2150 return scnprintf(msg, size, "%s",
2151 "Too many events are opened.\n"
2152 "Try again after reducing the number of events.");
2153 case ENODEV:
2154 if (target->cpu_list)
2155 return scnprintf(msg, size, "%s",
2156 "No such device - did you specify an out-of-range profile CPU?\n");
2157 break;
2158 case EOPNOTSUPP:
2159 if (evsel->attr.precise_ip)
2160 return scnprintf(msg, size, "%s",
2161 "\'precise\' request may not be supported. Try removing 'p' modifier.");
2162#if defined(__i386__) || defined(__x86_64__)
2163 if (evsel->attr.type == PERF_TYPE_HARDWARE)
2164 return scnprintf(msg, size, "%s",
2165 "No hardware sampling interrupt available.\n"
2166 "No APIC? If so then you can boot the kernel with the \"lapic\" boot parameter to force-enable it.");
2167#endif
2168 break;
Jiri Olsa63914ac2014-08-01 17:46:54 +02002169 case EBUSY:
2170 if (find_process("oprofiled"))
2171 return scnprintf(msg, size,
2172 "The PMU counters are busy/taken by another profiler.\n"
2173 "We found oprofile daemon running, please stop it and try again.");
2174 break;
Peter Zijlstra814c8c32015-03-31 00:19:31 +02002175 case EINVAL:
2176 if (perf_missing_features.clockid)
2177 return scnprintf(msg, size, "clockid feature not supported.");
2178 if (perf_missing_features.clockid_wrong)
2179 return scnprintf(msg, size, "wrong clockid (%d).", clockid);
2180 break;
Arnaldo Carvalho de Melo56e52e82012-12-13 15:10:58 -03002181 default:
2182 break;
2183 }
2184
2185 return scnprintf(msg, size,
Masami Hiramatsu6e81c742014-08-14 02:22:36 +00002186 "The sys_perf_event_open() syscall returned with %d (%s) for event (%s).\n"
Arnaldo Carvalho de Melo56e52e82012-12-13 15:10:58 -03002187 "/bin/dmesg may provide additional information.\n"
2188 "No CONFIG_PERF_EVENTS=y kernel support configured?\n",
Masami Hiramatsu6e81c742014-08-14 02:22:36 +00002189 err, strerror_r(err, sbuf, sizeof(sbuf)),
2190 perf_evsel__name(evsel));
Arnaldo Carvalho de Melo56e52e82012-12-13 15:10:58 -03002191}