blob: 7fde9fb7996696bd97c875661dfb54d20fd6d706 [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>
David Ahern936be502011-09-06 09:12:26 -060012#include "asm/bug.h"
Borislav Petkov85c66be2013-02-20 16:32:30 +010013#include <lk/debugfs.h>
Arnaldo Carvalho de Melo5555ded2012-09-11 19:24:23 -030014#include "event-parse.h"
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -020015#include "evsel.h"
Arnaldo Carvalho de Melo70082dd2011-01-12 17:03:24 -020016#include "evlist.h"
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -020017#include "util.h"
Arnaldo Carvalho de Melo86bd5e82011-01-03 23:09:46 -020018#include "cpumap.h"
Arnaldo Carvalho de Melofd782602011-01-18 15:15:24 -020019#include "thread_map.h"
Namhyung Kim12864b32012-04-26 14:15:22 +090020#include "target.h"
David Howellsd2709c72012-11-19 22:21:03 +000021#include <linux/hw_breakpoint.h>
22#include <linux/perf_event.h>
Jiri Olsa26d33022012-08-07 15:20:47 +020023#include "perf_regs.h"
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -020024
Arnaldo Carvalho de Melo594ac612012-12-13 13:13:07 -030025static struct {
26 bool sample_id_all;
27 bool exclude_guest;
28} perf_missing_features;
29
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -020030#define FD(e, x, y) (*(int *)xyarray__entry(e->fd, x, y))
31
Arnaldo Carvalho de Melobde09462012-08-01 18:53:11 -030032static int __perf_evsel__sample_size(u64 sample_type)
Arnaldo Carvalho de Meloc2a70652011-06-02 11:04:54 -030033{
34 u64 mask = sample_type & PERF_SAMPLE_MASK;
35 int size = 0;
36 int i;
37
38 for (i = 0; i < 64; i++) {
39 if (mask & (1ULL << i))
40 size++;
41 }
42
43 size *= sizeof(u64);
44
45 return size;
46}
47
Jiri Olsa4bf9ce12012-03-22 14:37:26 +010048void hists__init(struct hists *hists)
Arnaldo Carvalho de Melo0e2a5f12011-11-04 08:16:58 -020049{
50 memset(hists, 0, sizeof(*hists));
51 hists->entries_in_array[0] = hists->entries_in_array[1] = RB_ROOT;
52 hists->entries_in = &hists->entries_in_array[0];
53 hists->entries_collapsed = RB_ROOT;
54 hists->entries = RB_ROOT;
55 pthread_mutex_init(&hists->lock, NULL);
56}
57
Arnaldo Carvalho de Melo7be5ebe2012-12-10 14:53:43 -030058void __perf_evsel__set_sample_bit(struct perf_evsel *evsel,
59 enum perf_event_sample_format bit)
60{
61 if (!(evsel->attr.sample_type & bit)) {
62 evsel->attr.sample_type |= bit;
63 evsel->sample_size += sizeof(u64);
64 }
65}
66
67void __perf_evsel__reset_sample_bit(struct perf_evsel *evsel,
68 enum perf_event_sample_format bit)
69{
70 if (evsel->attr.sample_type & bit) {
71 evsel->attr.sample_type &= ~bit;
72 evsel->sample_size -= sizeof(u64);
73 }
74}
75
Arnaldo Carvalho de Melo7a5a5ca2012-12-10 15:21:30 -030076void perf_evsel__set_sample_id(struct perf_evsel *evsel)
77{
78 perf_evsel__set_sample_bit(evsel, ID);
79 evsel->attr.read_format |= PERF_FORMAT_ID;
80}
81
Arnaldo Carvalho de Meloef1d1af2011-01-18 21:41:45 -020082void perf_evsel__init(struct perf_evsel *evsel,
83 struct perf_event_attr *attr, int idx)
84{
85 evsel->idx = idx;
86 evsel->attr = *attr;
Namhyung Kim2cfda562012-11-29 15:38:29 +090087 evsel->leader = evsel;
Arnaldo Carvalho de Meloef1d1af2011-01-18 21:41:45 -020088 INIT_LIST_HEAD(&evsel->node);
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -030089 hists__init(&evsel->hists);
Arnaldo Carvalho de Melobde09462012-08-01 18:53:11 -030090 evsel->sample_size = __perf_evsel__sample_size(attr->sample_type);
Arnaldo Carvalho de Meloef1d1af2011-01-18 21:41:45 -020091}
92
Lin Ming23a2f3a2011-01-07 11:11:09 +080093struct perf_evsel *perf_evsel__new(struct perf_event_attr *attr, int idx)
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -020094{
95 struct perf_evsel *evsel = zalloc(sizeof(*evsel));
96
Arnaldo Carvalho de Meloef1d1af2011-01-18 21:41:45 -020097 if (evsel != NULL)
98 perf_evsel__init(evsel, attr, idx);
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -020099
100 return evsel;
101}
102
Arnaldo Carvalho de Melo201b7332012-09-26 20:24:19 -0300103struct event_format *event_format__new(const char *sys, const char *name)
Arnaldo Carvalho de Meloefd2b922012-09-18 11:21:50 -0300104{
105 int fd, n;
106 char *filename;
107 void *bf = NULL, *nbf;
108 size_t size = 0, alloc_size = 0;
109 struct event_format *format = NULL;
110
111 if (asprintf(&filename, "%s/%s/%s/format", tracing_events_path, sys, name) < 0)
112 goto out;
113
114 fd = open(filename, O_RDONLY);
115 if (fd < 0)
116 goto out_free_filename;
117
118 do {
119 if (size == alloc_size) {
120 alloc_size += BUFSIZ;
121 nbf = realloc(bf, alloc_size);
122 if (nbf == NULL)
123 goto out_free_bf;
124 bf = nbf;
125 }
126
127 n = read(fd, bf + size, BUFSIZ);
128 if (n < 0)
129 goto out_free_bf;
130 size += n;
131 } while (n > 0);
132
133 pevent_parse_format(&format, bf, size, sys);
134
135out_free_bf:
136 free(bf);
137 close(fd);
138out_free_filename:
139 free(filename);
140out:
141 return format;
142}
143
144struct perf_evsel *perf_evsel__newtp(const char *sys, const char *name, int idx)
145{
146 struct perf_evsel *evsel = zalloc(sizeof(*evsel));
147
148 if (evsel != NULL) {
149 struct perf_event_attr attr = {
Arnaldo Carvalho de Melo0b80f8b32012-09-26 12:28:26 -0300150 .type = PERF_TYPE_TRACEPOINT,
151 .sample_type = (PERF_SAMPLE_RAW | PERF_SAMPLE_TIME |
152 PERF_SAMPLE_CPU | PERF_SAMPLE_PERIOD),
Arnaldo Carvalho de Meloefd2b922012-09-18 11:21:50 -0300153 };
154
Arnaldo Carvalho de Meloe48ffe22012-09-26 17:11:38 -0300155 if (asprintf(&evsel->name, "%s:%s", sys, name) < 0)
156 goto out_free;
157
Arnaldo Carvalho de Meloefd2b922012-09-18 11:21:50 -0300158 evsel->tp_format = event_format__new(sys, name);
159 if (evsel->tp_format == NULL)
160 goto out_free;
161
Arnaldo Carvalho de Melo0b80f8b32012-09-26 12:28:26 -0300162 event_attr_init(&attr);
Arnaldo Carvalho de Meloefd2b922012-09-18 11:21:50 -0300163 attr.config = evsel->tp_format->id;
Arnaldo Carvalho de Melo0b80f8b32012-09-26 12:28:26 -0300164 attr.sample_period = 1;
Arnaldo Carvalho de Meloefd2b922012-09-18 11:21:50 -0300165 perf_evsel__init(evsel, &attr, idx);
Arnaldo Carvalho de Meloefd2b922012-09-18 11:21:50 -0300166 }
167
168 return evsel;
169
170out_free:
Arnaldo Carvalho de Meloe48ffe22012-09-26 17:11:38 -0300171 free(evsel->name);
Arnaldo Carvalho de Meloefd2b922012-09-18 11:21:50 -0300172 free(evsel);
173 return NULL;
174}
175
Arnaldo Carvalho de Melo8ad70132012-09-06 13:11:18 -0300176const char *perf_evsel__hw_names[PERF_COUNT_HW_MAX] = {
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300177 "cycles",
178 "instructions",
179 "cache-references",
180 "cache-misses",
181 "branches",
182 "branch-misses",
183 "bus-cycles",
184 "stalled-cycles-frontend",
185 "stalled-cycles-backend",
186 "ref-cycles",
187};
188
Arnaldo Carvalho de Melodd4f5222012-06-13 15:52:42 -0300189static const char *__perf_evsel__hw_name(u64 config)
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300190{
191 if (config < PERF_COUNT_HW_MAX && perf_evsel__hw_names[config])
192 return perf_evsel__hw_names[config];
193
194 return "unknown-hardware";
195}
196
Arnaldo Carvalho de Melo27f18612012-06-11 13:33:09 -0300197static int perf_evsel__add_modifiers(struct perf_evsel *evsel, char *bf, size_t size)
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300198{
Arnaldo Carvalho de Melo27f18612012-06-11 13:33:09 -0300199 int colon = 0, r = 0;
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300200 struct perf_event_attr *attr = &evsel->attr;
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300201 bool exclude_guest_default = false;
202
203#define MOD_PRINT(context, mod) do { \
204 if (!attr->exclude_##context) { \
Arnaldo Carvalho de Melo27f18612012-06-11 13:33:09 -0300205 if (!colon) colon = ++r; \
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300206 r += scnprintf(bf + r, size - r, "%c", mod); \
207 } } while(0)
208
209 if (attr->exclude_kernel || attr->exclude_user || attr->exclude_hv) {
210 MOD_PRINT(kernel, 'k');
211 MOD_PRINT(user, 'u');
212 MOD_PRINT(hv, 'h');
213 exclude_guest_default = true;
214 }
215
216 if (attr->precise_ip) {
217 if (!colon)
Arnaldo Carvalho de Melo27f18612012-06-11 13:33:09 -0300218 colon = ++r;
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300219 r += scnprintf(bf + r, size - r, "%.*s", attr->precise_ip, "ppp");
220 exclude_guest_default = true;
221 }
222
223 if (attr->exclude_host || attr->exclude_guest == exclude_guest_default) {
224 MOD_PRINT(host, 'H');
225 MOD_PRINT(guest, 'G');
226 }
227#undef MOD_PRINT
228 if (colon)
Arnaldo Carvalho de Melo27f18612012-06-11 13:33:09 -0300229 bf[colon - 1] = ':';
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300230 return r;
231}
232
Arnaldo Carvalho de Melo27f18612012-06-11 13:33:09 -0300233static int perf_evsel__hw_name(struct perf_evsel *evsel, char *bf, size_t size)
234{
235 int r = scnprintf(bf, size, "%s", __perf_evsel__hw_name(evsel->attr.config));
236 return r + perf_evsel__add_modifiers(evsel, bf + r, size - r);
237}
238
Arnaldo Carvalho de Melo8ad70132012-09-06 13:11:18 -0300239const char *perf_evsel__sw_names[PERF_COUNT_SW_MAX] = {
Arnaldo Carvalho de Melo335c2f52012-06-11 14:36:20 -0300240 "cpu-clock",
241 "task-clock",
242 "page-faults",
243 "context-switches",
Arnaldo Carvalho de Melo8ad70132012-09-06 13:11:18 -0300244 "cpu-migrations",
Arnaldo Carvalho de Melo335c2f52012-06-11 14:36:20 -0300245 "minor-faults",
246 "major-faults",
247 "alignment-faults",
248 "emulation-faults",
249};
250
Arnaldo Carvalho de Melodd4f5222012-06-13 15:52:42 -0300251static const char *__perf_evsel__sw_name(u64 config)
Arnaldo Carvalho de Melo335c2f52012-06-11 14:36:20 -0300252{
253 if (config < PERF_COUNT_SW_MAX && perf_evsel__sw_names[config])
254 return perf_evsel__sw_names[config];
255 return "unknown-software";
256}
257
258static int perf_evsel__sw_name(struct perf_evsel *evsel, char *bf, size_t size)
259{
260 int r = scnprintf(bf, size, "%s", __perf_evsel__sw_name(evsel->attr.config));
261 return r + perf_evsel__add_modifiers(evsel, bf + r, size - r);
262}
263
Jiri Olsa287e74a2012-06-28 23:18:49 +0200264static int __perf_evsel__bp_name(char *bf, size_t size, u64 addr, u64 type)
265{
266 int r;
267
268 r = scnprintf(bf, size, "mem:0x%" PRIx64 ":", addr);
269
270 if (type & HW_BREAKPOINT_R)
271 r += scnprintf(bf + r, size - r, "r");
272
273 if (type & HW_BREAKPOINT_W)
274 r += scnprintf(bf + r, size - r, "w");
275
276 if (type & HW_BREAKPOINT_X)
277 r += scnprintf(bf + r, size - r, "x");
278
279 return r;
280}
281
282static int perf_evsel__bp_name(struct perf_evsel *evsel, char *bf, size_t size)
283{
284 struct perf_event_attr *attr = &evsel->attr;
285 int r = __perf_evsel__bp_name(bf, size, attr->bp_addr, attr->bp_type);
286 return r + perf_evsel__add_modifiers(evsel, bf + r, size - r);
287}
288
Arnaldo Carvalho de Melo0b668bc2012-06-11 14:08:07 -0300289const char *perf_evsel__hw_cache[PERF_COUNT_HW_CACHE_MAX]
290 [PERF_EVSEL__MAX_ALIASES] = {
291 { "L1-dcache", "l1-d", "l1d", "L1-data", },
292 { "L1-icache", "l1-i", "l1i", "L1-instruction", },
293 { "LLC", "L2", },
294 { "dTLB", "d-tlb", "Data-TLB", },
295 { "iTLB", "i-tlb", "Instruction-TLB", },
296 { "branch", "branches", "bpu", "btb", "bpc", },
297 { "node", },
298};
299
300const char *perf_evsel__hw_cache_op[PERF_COUNT_HW_CACHE_OP_MAX]
301 [PERF_EVSEL__MAX_ALIASES] = {
302 { "load", "loads", "read", },
303 { "store", "stores", "write", },
304 { "prefetch", "prefetches", "speculative-read", "speculative-load", },
305};
306
307const char *perf_evsel__hw_cache_result[PERF_COUNT_HW_CACHE_RESULT_MAX]
308 [PERF_EVSEL__MAX_ALIASES] = {
309 { "refs", "Reference", "ops", "access", },
310 { "misses", "miss", },
311};
312
313#define C(x) PERF_COUNT_HW_CACHE_##x
314#define CACHE_READ (1 << C(OP_READ))
315#define CACHE_WRITE (1 << C(OP_WRITE))
316#define CACHE_PREFETCH (1 << C(OP_PREFETCH))
317#define COP(x) (1 << x)
318
319/*
320 * cache operartion stat
321 * L1I : Read and prefetch only
322 * ITLB and BPU : Read-only
323 */
324static unsigned long perf_evsel__hw_cache_stat[C(MAX)] = {
325 [C(L1D)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
326 [C(L1I)] = (CACHE_READ | CACHE_PREFETCH),
327 [C(LL)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
328 [C(DTLB)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
329 [C(ITLB)] = (CACHE_READ),
330 [C(BPU)] = (CACHE_READ),
331 [C(NODE)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
332};
333
334bool perf_evsel__is_cache_op_valid(u8 type, u8 op)
335{
336 if (perf_evsel__hw_cache_stat[type] & COP(op))
337 return true; /* valid */
338 else
339 return false; /* invalid */
340}
341
342int __perf_evsel__hw_cache_type_op_res_name(u8 type, u8 op, u8 result,
343 char *bf, size_t size)
344{
345 if (result) {
346 return scnprintf(bf, size, "%s-%s-%s", perf_evsel__hw_cache[type][0],
347 perf_evsel__hw_cache_op[op][0],
348 perf_evsel__hw_cache_result[result][0]);
349 }
350
351 return scnprintf(bf, size, "%s-%s", perf_evsel__hw_cache[type][0],
352 perf_evsel__hw_cache_op[op][1]);
353}
354
Arnaldo Carvalho de Melodd4f5222012-06-13 15:52:42 -0300355static int __perf_evsel__hw_cache_name(u64 config, char *bf, size_t size)
Arnaldo Carvalho de Melo0b668bc2012-06-11 14:08:07 -0300356{
357 u8 op, result, type = (config >> 0) & 0xff;
358 const char *err = "unknown-ext-hardware-cache-type";
359
360 if (type > PERF_COUNT_HW_CACHE_MAX)
361 goto out_err;
362
363 op = (config >> 8) & 0xff;
364 err = "unknown-ext-hardware-cache-op";
365 if (op > PERF_COUNT_HW_CACHE_OP_MAX)
366 goto out_err;
367
368 result = (config >> 16) & 0xff;
369 err = "unknown-ext-hardware-cache-result";
370 if (result > PERF_COUNT_HW_CACHE_RESULT_MAX)
371 goto out_err;
372
373 err = "invalid-cache";
374 if (!perf_evsel__is_cache_op_valid(type, op))
375 goto out_err;
376
377 return __perf_evsel__hw_cache_type_op_res_name(type, op, result, bf, size);
378out_err:
379 return scnprintf(bf, size, "%s", err);
380}
381
382static int perf_evsel__hw_cache_name(struct perf_evsel *evsel, char *bf, size_t size)
383{
384 int ret = __perf_evsel__hw_cache_name(evsel->attr.config, bf, size);
385 return ret + perf_evsel__add_modifiers(evsel, bf + ret, size - ret);
386}
387
Arnaldo Carvalho de Melo6eef3d92012-06-13 11:53:37 -0300388static int perf_evsel__raw_name(struct perf_evsel *evsel, char *bf, size_t size)
389{
390 int ret = scnprintf(bf, size, "raw 0x%" PRIx64, evsel->attr.config);
391 return ret + perf_evsel__add_modifiers(evsel, bf + ret, size - ret);
392}
393
Arnaldo Carvalho de Melo7289f832012-06-12 12:34:58 -0300394const char *perf_evsel__name(struct perf_evsel *evsel)
Arnaldo Carvalho de Meloa4460832012-06-12 10:29:12 -0300395{
Arnaldo Carvalho de Melo7289f832012-06-12 12:34:58 -0300396 char bf[128];
Arnaldo Carvalho de Meloa4460832012-06-12 10:29:12 -0300397
Arnaldo Carvalho de Melo7289f832012-06-12 12:34:58 -0300398 if (evsel->name)
399 return evsel->name;
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300400
401 switch (evsel->attr.type) {
402 case PERF_TYPE_RAW:
Arnaldo Carvalho de Melo6eef3d92012-06-13 11:53:37 -0300403 perf_evsel__raw_name(evsel, bf, sizeof(bf));
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300404 break;
405
406 case PERF_TYPE_HARDWARE:
Arnaldo Carvalho de Melo7289f832012-06-12 12:34:58 -0300407 perf_evsel__hw_name(evsel, bf, sizeof(bf));
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300408 break;
Arnaldo Carvalho de Melo0b668bc2012-06-11 14:08:07 -0300409
410 case PERF_TYPE_HW_CACHE:
Arnaldo Carvalho de Melo7289f832012-06-12 12:34:58 -0300411 perf_evsel__hw_cache_name(evsel, bf, sizeof(bf));
Arnaldo Carvalho de Melo0b668bc2012-06-11 14:08:07 -0300412 break;
413
Arnaldo Carvalho de Melo335c2f52012-06-11 14:36:20 -0300414 case PERF_TYPE_SOFTWARE:
Arnaldo Carvalho de Melo7289f832012-06-12 12:34:58 -0300415 perf_evsel__sw_name(evsel, bf, sizeof(bf));
Arnaldo Carvalho de Melo335c2f52012-06-11 14:36:20 -0300416 break;
417
Arnaldo Carvalho de Meloa4460832012-06-12 10:29:12 -0300418 case PERF_TYPE_TRACEPOINT:
Arnaldo Carvalho de Melo7289f832012-06-12 12:34:58 -0300419 scnprintf(bf, sizeof(bf), "%s", "unknown tracepoint");
Arnaldo Carvalho de Meloa4460832012-06-12 10:29:12 -0300420 break;
421
Jiri Olsa287e74a2012-06-28 23:18:49 +0200422 case PERF_TYPE_BREAKPOINT:
423 perf_evsel__bp_name(evsel, bf, sizeof(bf));
424 break;
425
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300426 default:
Robert Richterca1b1452012-08-16 21:10:18 +0200427 scnprintf(bf, sizeof(bf), "unknown attr type: %d",
428 evsel->attr.type);
Arnaldo Carvalho de Meloa4460832012-06-12 10:29:12 -0300429 break;
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300430 }
431
Arnaldo Carvalho de Melo7289f832012-06-12 12:34:58 -0300432 evsel->name = strdup(bf);
433
434 return evsel->name ?: "unknown";
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300435}
436
Namhyung Kim717e2632013-01-22 18:09:44 +0900437const char *perf_evsel__group_name(struct perf_evsel *evsel)
438{
439 return evsel->group_name ?: "anon group";
440}
441
442int perf_evsel__group_desc(struct perf_evsel *evsel, char *buf, size_t size)
443{
444 int ret;
445 struct perf_evsel *pos;
446 const char *group_name = perf_evsel__group_name(evsel);
447
448 ret = scnprintf(buf, size, "%s", group_name);
449
450 ret += scnprintf(buf + ret, size - ret, " { %s",
451 perf_evsel__name(evsel));
452
453 for_each_group_member(pos, evsel)
454 ret += scnprintf(buf + ret, size - ret, ", %s",
455 perf_evsel__name(pos));
456
457 ret += scnprintf(buf + ret, size - ret, " }");
458
459 return ret;
460}
461
Jiri Olsa774cb492012-11-12 18:34:01 +0100462/*
463 * The enable_on_exec/disabled value strategy:
464 *
465 * 1) For any type of traced program:
466 * - all independent events and group leaders are disabled
467 * - all group members are enabled
468 *
469 * Group members are ruled by group leaders. They need to
470 * be enabled, because the group scheduling relies on that.
471 *
472 * 2) For traced programs executed by perf:
473 * - all independent events and group leaders have
474 * enable_on_exec set
475 * - we don't specifically enable or disable any event during
476 * the record command
477 *
478 * Independent events and group leaders are initially disabled
479 * and get enabled by exec. Group members are ruled by group
480 * leaders as stated in 1).
481 *
482 * 3) For traced programs attached by perf (pid/tid):
483 * - we specifically enable or disable all events during
484 * the record command
485 *
486 * When attaching events to already running traced we
487 * enable/disable events specifically, as there's no
488 * initial traced exec call.
489 */
Jiri Olsacac21422012-11-12 18:34:00 +0100490void perf_evsel__config(struct perf_evsel *evsel,
491 struct perf_record_opts *opts)
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200492{
493 struct perf_event_attr *attr = &evsel->attr;
494 int track = !evsel->idx; /* only the first counter needs these */
495
Arnaldo Carvalho de Melo594ac612012-12-13 13:13:07 -0300496 attr->sample_id_all = perf_missing_features.sample_id_all ? 0 : 1;
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200497 attr->inherit = !opts->no_inherit;
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200498
Arnaldo Carvalho de Melo7be5ebe2012-12-10 14:53:43 -0300499 perf_evsel__set_sample_bit(evsel, IP);
500 perf_evsel__set_sample_bit(evsel, TID);
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200501
502 /*
503 * We default some events to a 1 default interval. But keep
504 * it a weak assumption overridable by the user.
505 */
506 if (!attr->sample_period || (opts->user_freq != UINT_MAX &&
507 opts->user_interval != ULLONG_MAX)) {
508 if (opts->freq) {
Arnaldo Carvalho de Melo7be5ebe2012-12-10 14:53:43 -0300509 perf_evsel__set_sample_bit(evsel, PERIOD);
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200510 attr->freq = 1;
511 attr->sample_freq = opts->freq;
512 } else {
513 attr->sample_period = opts->default_interval;
514 }
515 }
516
517 if (opts->no_samples)
518 attr->sample_freq = 0;
519
520 if (opts->inherit_stat)
521 attr->inherit_stat = 1;
522
523 if (opts->sample_address) {
Arnaldo Carvalho de Melo7be5ebe2012-12-10 14:53:43 -0300524 perf_evsel__set_sample_bit(evsel, ADDR);
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200525 attr->mmap_data = track;
526 }
527
Jiri Olsa26d33022012-08-07 15:20:47 +0200528 if (opts->call_graph) {
Arnaldo Carvalho de Melo7be5ebe2012-12-10 14:53:43 -0300529 perf_evsel__set_sample_bit(evsel, CALLCHAIN);
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200530
Jiri Olsa26d33022012-08-07 15:20:47 +0200531 if (opts->call_graph == CALLCHAIN_DWARF) {
Arnaldo Carvalho de Melo7be5ebe2012-12-10 14:53:43 -0300532 perf_evsel__set_sample_bit(evsel, REGS_USER);
533 perf_evsel__set_sample_bit(evsel, STACK_USER);
Jiri Olsa26d33022012-08-07 15:20:47 +0200534 attr->sample_regs_user = PERF_REGS_MASK;
535 attr->sample_stack_user = opts->stack_dump_size;
536 attr->exclude_callchain_user = 1;
537 }
538 }
539
Namhyung Kime40ee742012-05-21 10:42:07 +0900540 if (perf_target__has_cpu(&opts->target))
Arnaldo Carvalho de Melo7be5ebe2012-12-10 14:53:43 -0300541 perf_evsel__set_sample_bit(evsel, CPU);
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200542
Andrew Vagin3e76ac72011-12-20 17:32:45 +0300543 if (opts->period)
Arnaldo Carvalho de Melo7be5ebe2012-12-10 14:53:43 -0300544 perf_evsel__set_sample_bit(evsel, PERIOD);
Andrew Vagin3e76ac72011-12-20 17:32:45 +0300545
Arnaldo Carvalho de Melo594ac612012-12-13 13:13:07 -0300546 if (!perf_missing_features.sample_id_all &&
Namhyung Kimd67356e2012-05-07 14:09:03 +0900547 (opts->sample_time || !opts->no_inherit ||
Namhyung Kimaa22dd42012-05-16 18:45:47 +0900548 perf_target__has_cpu(&opts->target)))
Arnaldo Carvalho de Melo7be5ebe2012-12-10 14:53:43 -0300549 perf_evsel__set_sample_bit(evsel, TIME);
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200550
551 if (opts->raw_samples) {
Arnaldo Carvalho de Melo7be5ebe2012-12-10 14:53:43 -0300552 perf_evsel__set_sample_bit(evsel, TIME);
553 perf_evsel__set_sample_bit(evsel, RAW);
554 perf_evsel__set_sample_bit(evsel, CPU);
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200555 }
556
557 if (opts->no_delay) {
558 attr->watermark = 0;
559 attr->wakeup_events = 1;
560 }
Roberto Agostino Vitillobdfebd82012-02-09 23:21:02 +0100561 if (opts->branch_stack) {
Arnaldo Carvalho de Melo7be5ebe2012-12-10 14:53:43 -0300562 perf_evsel__set_sample_bit(evsel, BRANCH_STACK);
Roberto Agostino Vitillobdfebd82012-02-09 23:21:02 +0100563 attr->branch_sample_type = opts->branch_stack;
564 }
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200565
566 attr->mmap = track;
567 attr->comm = track;
568
Jiri Olsa774cb492012-11-12 18:34:01 +0100569 /*
570 * XXX see the function comment above
571 *
572 * Disabling only independent events or group leaders,
573 * keeping group members enabled.
574 */
Namhyung Kim823254e2012-11-29 15:38:30 +0900575 if (perf_evsel__is_group_leader(evsel))
Jiri Olsa774cb492012-11-12 18:34:01 +0100576 attr->disabled = 1;
577
578 /*
579 * Setting enable_on_exec for independent events and
580 * group leaders for traced executed by perf.
581 */
Namhyung Kim823254e2012-11-29 15:38:30 +0900582 if (perf_target__none(&opts->target) && perf_evsel__is_group_leader(evsel))
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200583 attr->enable_on_exec = 1;
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200584}
585
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200586int perf_evsel__alloc_fd(struct perf_evsel *evsel, int ncpus, int nthreads)
587{
David Ahern4af4c952011-05-27 09:58:34 -0600588 int cpu, thread;
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200589 evsel->fd = xyarray__new(ncpus, nthreads, sizeof(int));
David Ahern4af4c952011-05-27 09:58:34 -0600590
591 if (evsel->fd) {
592 for (cpu = 0; cpu < ncpus; cpu++) {
593 for (thread = 0; thread < nthreads; thread++) {
594 FD(evsel, cpu, thread) = -1;
595 }
596 }
597 }
598
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200599 return evsel->fd != NULL ? 0 : -ENOMEM;
600}
601
Arnaldo Carvalho de Melo745cefc2012-09-26 15:07:39 -0300602int perf_evsel__set_filter(struct perf_evsel *evsel, int ncpus, int nthreads,
603 const char *filter)
604{
605 int cpu, thread;
606
607 for (cpu = 0; cpu < ncpus; cpu++) {
608 for (thread = 0; thread < nthreads; thread++) {
609 int fd = FD(evsel, cpu, thread),
610 err = ioctl(fd, PERF_EVENT_IOC_SET_FILTER, filter);
611
612 if (err)
613 return err;
614 }
615 }
616
617 return 0;
618}
619
Arnaldo Carvalho de Melo70db7532011-01-12 22:39:13 -0200620int perf_evsel__alloc_id(struct perf_evsel *evsel, int ncpus, int nthreads)
621{
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -0300622 evsel->sample_id = xyarray__new(ncpus, nthreads, sizeof(struct perf_sample_id));
623 if (evsel->sample_id == NULL)
624 return -ENOMEM;
625
626 evsel->id = zalloc(ncpus * nthreads * sizeof(u64));
627 if (evsel->id == NULL) {
628 xyarray__delete(evsel->sample_id);
629 evsel->sample_id = NULL;
630 return -ENOMEM;
631 }
632
633 return 0;
Arnaldo Carvalho de Melo70db7532011-01-12 22:39:13 -0200634}
635
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200636int perf_evsel__alloc_counts(struct perf_evsel *evsel, int ncpus)
637{
638 evsel->counts = zalloc((sizeof(*evsel->counts) +
639 (ncpus * sizeof(struct perf_counts_values))));
640 return evsel->counts != NULL ? 0 : -ENOMEM;
641}
642
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200643void perf_evsel__free_fd(struct perf_evsel *evsel)
644{
645 xyarray__delete(evsel->fd);
646 evsel->fd = NULL;
647}
648
Arnaldo Carvalho de Melo70db7532011-01-12 22:39:13 -0200649void perf_evsel__free_id(struct perf_evsel *evsel)
650{
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -0300651 xyarray__delete(evsel->sample_id);
652 evsel->sample_id = NULL;
653 free(evsel->id);
Arnaldo Carvalho de Melo70db7532011-01-12 22:39:13 -0200654 evsel->id = NULL;
655}
656
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200657void perf_evsel__close_fd(struct perf_evsel *evsel, int ncpus, int nthreads)
658{
659 int cpu, thread;
660
661 for (cpu = 0; cpu < ncpus; cpu++)
662 for (thread = 0; thread < nthreads; ++thread) {
663 close(FD(evsel, cpu, thread));
664 FD(evsel, cpu, thread) = -1;
665 }
666}
667
Namhyung Kim43f8e762013-01-25 10:44:44 +0900668void perf_evsel__free_counts(struct perf_evsel *evsel)
669{
670 free(evsel->counts);
671}
672
Arnaldo Carvalho de Meloef1d1af2011-01-18 21:41:45 -0200673void perf_evsel__exit(struct perf_evsel *evsel)
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200674{
675 assert(list_empty(&evsel->node));
Namhyung Kim736b05a2013-03-15 14:48:49 +0900676 perf_evsel__free_fd(evsel);
677 perf_evsel__free_id(evsel);
Arnaldo Carvalho de Meloef1d1af2011-01-18 21:41:45 -0200678}
679
680void perf_evsel__delete(struct perf_evsel *evsel)
681{
682 perf_evsel__exit(evsel);
Stephane Eranian023695d2011-02-14 11:20:01 +0200683 close_cgroup(evsel->cgrp);
Jiri Olsa6a4bb042012-08-08 12:22:36 +0200684 free(evsel->group_name);
Arnaldo Carvalho de Meloe48ffe22012-09-26 17:11:38 -0300685 if (evsel->tp_format)
Arnaldo Carvalho de Meloefd2b922012-09-18 11:21:50 -0300686 pevent_free_format(evsel->tp_format);
Stephane Eranianf0c55bc2011-02-16 15:10:01 +0200687 free(evsel->name);
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200688 free(evsel);
689}
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200690
Stephane Eranianc7a79c42013-01-29 12:47:43 +0100691static inline void compute_deltas(struct perf_evsel *evsel,
692 int cpu,
693 struct perf_counts_values *count)
694{
695 struct perf_counts_values tmp;
696
697 if (!evsel->prev_raw_counts)
698 return;
699
700 if (cpu == -1) {
701 tmp = evsel->prev_raw_counts->aggr;
702 evsel->prev_raw_counts->aggr = *count;
703 } else {
704 tmp = evsel->prev_raw_counts->cpu[cpu];
705 evsel->prev_raw_counts->cpu[cpu] = *count;
706 }
707
708 count->val = count->val - tmp.val;
709 count->ena = count->ena - tmp.ena;
710 count->run = count->run - tmp.run;
711}
712
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200713int __perf_evsel__read_on_cpu(struct perf_evsel *evsel,
714 int cpu, int thread, bool scale)
715{
716 struct perf_counts_values count;
717 size_t nv = scale ? 3 : 1;
718
719 if (FD(evsel, cpu, thread) < 0)
720 return -EINVAL;
721
Arnaldo Carvalho de Melo4eed11d2011-01-04 00:13:17 -0200722 if (evsel->counts == NULL && perf_evsel__alloc_counts(evsel, cpu + 1) < 0)
723 return -ENOMEM;
724
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200725 if (readn(FD(evsel, cpu, thread), &count, nv * sizeof(u64)) < 0)
726 return -errno;
727
Stephane Eranianc7a79c42013-01-29 12:47:43 +0100728 compute_deltas(evsel, cpu, &count);
729
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200730 if (scale) {
731 if (count.run == 0)
732 count.val = 0;
733 else if (count.run < count.ena)
734 count.val = (u64)((double)count.val * count.ena / count.run + 0.5);
735 } else
736 count.ena = count.run = 0;
737
738 evsel->counts->cpu[cpu] = count;
739 return 0;
740}
741
742int __perf_evsel__read(struct perf_evsel *evsel,
743 int ncpus, int nthreads, bool scale)
744{
745 size_t nv = scale ? 3 : 1;
746 int cpu, thread;
747 struct perf_counts_values *aggr = &evsel->counts->aggr, count;
748
Arnaldo Carvalho de Melo52bcd9942011-02-03 17:26:06 -0200749 aggr->val = aggr->ena = aggr->run = 0;
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200750
751 for (cpu = 0; cpu < ncpus; cpu++) {
752 for (thread = 0; thread < nthreads; thread++) {
753 if (FD(evsel, cpu, thread) < 0)
754 continue;
755
756 if (readn(FD(evsel, cpu, thread),
757 &count, nv * sizeof(u64)) < 0)
758 return -errno;
759
760 aggr->val += count.val;
761 if (scale) {
762 aggr->ena += count.ena;
763 aggr->run += count.run;
764 }
765 }
766 }
767
Stephane Eranianc7a79c42013-01-29 12:47:43 +0100768 compute_deltas(evsel, -1, aggr);
769
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200770 evsel->counts->scaled = 0;
771 if (scale) {
772 if (aggr->run == 0) {
773 evsel->counts->scaled = -1;
774 aggr->val = 0;
775 return 0;
776 }
777
778 if (aggr->run < aggr->ena) {
779 evsel->counts->scaled = 1;
780 aggr->val = (u64)((double)aggr->val * aggr->ena / aggr->run + 0.5);
781 }
782 } else
783 aggr->ena = aggr->run = 0;
784
785 return 0;
786}
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -0200787
Jiri Olsa6a4bb042012-08-08 12:22:36 +0200788static int get_group_fd(struct perf_evsel *evsel, int cpu, int thread)
789{
790 struct perf_evsel *leader = evsel->leader;
791 int fd;
792
Namhyung Kim823254e2012-11-29 15:38:30 +0900793 if (perf_evsel__is_group_leader(evsel))
Jiri Olsa6a4bb042012-08-08 12:22:36 +0200794 return -1;
795
796 /*
797 * Leader must be already processed/open,
798 * if not it's a bug.
799 */
800 BUG_ON(!leader->fd);
801
802 fd = FD(leader, cpu, thread);
803 BUG_ON(fd == -1);
804
805 return fd;
806}
807
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -0200808static int __perf_evsel__open(struct perf_evsel *evsel, struct cpu_map *cpus,
Jiri Olsa6a4bb042012-08-08 12:22:36 +0200809 struct thread_map *threads)
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -0200810{
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -0200811 int cpu, thread;
Stephane Eranian023695d2011-02-14 11:20:01 +0200812 unsigned long flags = 0;
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -0200813 int pid = -1, err;
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -0200814
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -0200815 if (evsel->fd == NULL &&
816 perf_evsel__alloc_fd(evsel, cpus->nr, threads->nr) < 0)
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -0200817 return -ENOMEM;
Arnaldo Carvalho de Melo4eed11d2011-01-04 00:13:17 -0200818
Stephane Eranian023695d2011-02-14 11:20:01 +0200819 if (evsel->cgrp) {
820 flags = PERF_FLAG_PID_CGROUP;
821 pid = evsel->cgrp->fd;
822 }
823
Arnaldo Carvalho de Melo594ac612012-12-13 13:13:07 -0300824fallback_missing_features:
825 if (perf_missing_features.exclude_guest)
826 evsel->attr.exclude_guest = evsel->attr.exclude_host = 0;
827retry_sample_id:
828 if (perf_missing_features.sample_id_all)
829 evsel->attr.sample_id_all = 0;
830
Arnaldo Carvalho de Melo86bd5e82011-01-03 23:09:46 -0200831 for (cpu = 0; cpu < cpus->nr; cpu++) {
Arnaldo Carvalho de Melo9d04f172011-01-12 00:08:18 -0200832
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -0200833 for (thread = 0; thread < threads->nr; thread++) {
Jiri Olsa6a4bb042012-08-08 12:22:36 +0200834 int group_fd;
Stephane Eranian023695d2011-02-14 11:20:01 +0200835
836 if (!evsel->cgrp)
837 pid = threads->map[thread];
838
Jiri Olsa6a4bb042012-08-08 12:22:36 +0200839 group_fd = get_group_fd(evsel, cpu, thread);
840
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -0200841 FD(evsel, cpu, thread) = sys_perf_event_open(&evsel->attr,
Stephane Eranian023695d2011-02-14 11:20:01 +0200842 pid,
Arnaldo Carvalho de Melof08199d2011-01-11 23:42:19 -0200843 cpus->map[cpu],
Stephane Eranian023695d2011-02-14 11:20:01 +0200844 group_fd, flags);
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -0200845 if (FD(evsel, cpu, thread) < 0) {
846 err = -errno;
Arnaldo Carvalho de Melo594ac612012-12-13 13:13:07 -0300847 goto try_fallback;
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -0200848 }
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -0200849 }
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -0200850 }
851
852 return 0;
853
Arnaldo Carvalho de Melo594ac612012-12-13 13:13:07 -0300854try_fallback:
855 if (err != -EINVAL || cpu > 0 || thread > 0)
856 goto out_close;
857
858 if (!perf_missing_features.exclude_guest &&
859 (evsel->attr.exclude_guest || evsel->attr.exclude_host)) {
860 perf_missing_features.exclude_guest = true;
861 goto fallback_missing_features;
862 } else if (!perf_missing_features.sample_id_all) {
863 perf_missing_features.sample_id_all = true;
864 goto retry_sample_id;
865 }
866
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -0200867out_close:
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -0200868 do {
869 while (--thread >= 0) {
870 close(FD(evsel, cpu, thread));
871 FD(evsel, cpu, thread) = -1;
872 }
873 thread = threads->nr;
874 } while (--cpu >= 0);
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -0200875 return err;
876}
877
878void perf_evsel__close(struct perf_evsel *evsel, int ncpus, int nthreads)
879{
880 if (evsel->fd == NULL)
881 return;
882
883 perf_evsel__close_fd(evsel, ncpus, nthreads);
884 perf_evsel__free_fd(evsel);
885 evsel->fd = NULL;
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -0200886}
887
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -0200888static struct {
889 struct cpu_map map;
890 int cpus[1];
891} empty_cpu_map = {
892 .map.nr = 1,
893 .cpus = { -1, },
894};
895
896static struct {
897 struct thread_map map;
898 int threads[1];
899} empty_thread_map = {
900 .map.nr = 1,
901 .threads = { -1, },
902};
903
Arnaldo Carvalho de Melof08199d2011-01-11 23:42:19 -0200904int perf_evsel__open(struct perf_evsel *evsel, struct cpu_map *cpus,
Jiri Olsa6a4bb042012-08-08 12:22:36 +0200905 struct thread_map *threads)
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -0200906{
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -0200907 if (cpus == NULL) {
908 /* Work around old compiler warnings about strict aliasing */
909 cpus = &empty_cpu_map.map;
910 }
911
912 if (threads == NULL)
913 threads = &empty_thread_map.map;
914
Jiri Olsa6a4bb042012-08-08 12:22:36 +0200915 return __perf_evsel__open(evsel, cpus, threads);
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -0200916}
917
Arnaldo Carvalho de Melof08199d2011-01-11 23:42:19 -0200918int perf_evsel__open_per_cpu(struct perf_evsel *evsel,
Jiri Olsa6a4bb042012-08-08 12:22:36 +0200919 struct cpu_map *cpus)
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -0200920{
Jiri Olsa6a4bb042012-08-08 12:22:36 +0200921 return __perf_evsel__open(evsel, cpus, &empty_thread_map.map);
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -0200922}
923
Arnaldo Carvalho de Melof08199d2011-01-11 23:42:19 -0200924int perf_evsel__open_per_thread(struct perf_evsel *evsel,
Jiri Olsa6a4bb042012-08-08 12:22:36 +0200925 struct thread_map *threads)
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -0200926{
Jiri Olsa6a4bb042012-08-08 12:22:36 +0200927 return __perf_evsel__open(evsel, &empty_cpu_map.map, threads);
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -0200928}
Arnaldo Carvalho de Melo70082dd2011-01-12 17:03:24 -0200929
Arnaldo Carvalho de Melo0807d2d2012-09-26 12:48:18 -0300930static int perf_evsel__parse_id_sample(const struct perf_evsel *evsel,
931 const union perf_event *event,
932 struct perf_sample *sample)
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -0200933{
Arnaldo Carvalho de Melo0807d2d2012-09-26 12:48:18 -0300934 u64 type = evsel->attr.sample_type;
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -0200935 const u64 *array = event->sample.array;
Arnaldo Carvalho de Melo0807d2d2012-09-26 12:48:18 -0300936 bool swapped = evsel->needs_swap;
Jiri Olsa37073f92012-05-30 14:23:44 +0200937 union u64_swap u;
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -0200938
939 array += ((event->header.size -
940 sizeof(event->header)) / sizeof(u64)) - 1;
941
942 if (type & PERF_SAMPLE_CPU) {
Jiri Olsa37073f92012-05-30 14:23:44 +0200943 u.val64 = *array;
944 if (swapped) {
945 /* undo swap of u64, then swap on individual u32s */
946 u.val64 = bswap_64(u.val64);
947 u.val32[0] = bswap_32(u.val32[0]);
948 }
949
950 sample->cpu = u.val32[0];
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -0200951 array--;
952 }
953
954 if (type & PERF_SAMPLE_STREAM_ID) {
955 sample->stream_id = *array;
956 array--;
957 }
958
959 if (type & PERF_SAMPLE_ID) {
960 sample->id = *array;
961 array--;
962 }
963
964 if (type & PERF_SAMPLE_TIME) {
965 sample->time = *array;
966 array--;
967 }
968
969 if (type & PERF_SAMPLE_TID) {
Jiri Olsa37073f92012-05-30 14:23:44 +0200970 u.val64 = *array;
971 if (swapped) {
972 /* undo swap of u64, then swap on individual u32s */
973 u.val64 = bswap_64(u.val64);
974 u.val32[0] = bswap_32(u.val32[0]);
975 u.val32[1] = bswap_32(u.val32[1]);
976 }
977
978 sample->pid = u.val32[0];
979 sample->tid = u.val32[1];
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -0200980 }
981
982 return 0;
983}
984
Frederic Weisbecker98e1da92011-05-21 20:08:15 +0200985static bool sample_overlap(const union perf_event *event,
986 const void *offset, u64 size)
987{
988 const void *base = event;
989
990 if (offset + size > base + event->header.size)
991 return true;
992
993 return false;
994}
995
Arnaldo Carvalho de Meloa3f698f2012-08-02 12:23:46 -0300996int perf_evsel__parse_sample(struct perf_evsel *evsel, union perf_event *event,
Arnaldo Carvalho de Melo0807d2d2012-09-26 12:48:18 -0300997 struct perf_sample *data)
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -0200998{
Arnaldo Carvalho de Meloa3f698f2012-08-02 12:23:46 -0300999 u64 type = evsel->attr.sample_type;
Jiri Olsa0f6a3012012-08-07 15:20:45 +02001000 u64 regs_user = evsel->attr.sample_regs_user;
Arnaldo Carvalho de Melo0807d2d2012-09-26 12:48:18 -03001001 bool swapped = evsel->needs_swap;
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001002 const u64 *array;
1003
David Ahern936be502011-09-06 09:12:26 -06001004 /*
1005 * used for cross-endian analysis. See git commit 65014ab3
1006 * for why this goofiness is needed.
1007 */
Jiri Olsa6a11f922012-05-16 08:59:04 +02001008 union u64_swap u;
David Ahern936be502011-09-06 09:12:26 -06001009
Robert Richterf3bda2c2011-12-15 17:32:39 +01001010 memset(data, 0, sizeof(*data));
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001011 data->cpu = data->pid = data->tid = -1;
1012 data->stream_id = data->id = data->time = -1ULL;
Naveen N. Raoa4a03fc2012-02-03 22:31:13 +05301013 data->period = 1;
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001014
1015 if (event->header.type != PERF_RECORD_SAMPLE) {
Arnaldo Carvalho de Meloa3f698f2012-08-02 12:23:46 -03001016 if (!evsel->attr.sample_id_all)
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001017 return 0;
Arnaldo Carvalho de Melo0807d2d2012-09-26 12:48:18 -03001018 return perf_evsel__parse_id_sample(evsel, event, data);
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001019 }
1020
1021 array = event->sample.array;
1022
Arnaldo Carvalho de Meloa3f698f2012-08-02 12:23:46 -03001023 if (evsel->sample_size + sizeof(event->header) > event->header.size)
Frederic Weisbeckera2854122011-05-21 19:33:04 +02001024 return -EFAULT;
1025
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001026 if (type & PERF_SAMPLE_IP) {
1027 data->ip = event->ip.ip;
1028 array++;
1029 }
1030
1031 if (type & PERF_SAMPLE_TID) {
David Ahern936be502011-09-06 09:12:26 -06001032 u.val64 = *array;
1033 if (swapped) {
1034 /* undo swap of u64, then swap on individual u32s */
1035 u.val64 = bswap_64(u.val64);
1036 u.val32[0] = bswap_32(u.val32[0]);
1037 u.val32[1] = bswap_32(u.val32[1]);
1038 }
1039
1040 data->pid = u.val32[0];
1041 data->tid = u.val32[1];
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001042 array++;
1043 }
1044
1045 if (type & PERF_SAMPLE_TIME) {
1046 data->time = *array;
1047 array++;
1048 }
1049
David Ahern7cec0922011-05-30 13:08:23 -06001050 data->addr = 0;
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001051 if (type & PERF_SAMPLE_ADDR) {
1052 data->addr = *array;
1053 array++;
1054 }
1055
1056 data->id = -1ULL;
1057 if (type & PERF_SAMPLE_ID) {
1058 data->id = *array;
1059 array++;
1060 }
1061
1062 if (type & PERF_SAMPLE_STREAM_ID) {
1063 data->stream_id = *array;
1064 array++;
1065 }
1066
1067 if (type & PERF_SAMPLE_CPU) {
David Ahern936be502011-09-06 09:12:26 -06001068
1069 u.val64 = *array;
1070 if (swapped) {
1071 /* undo swap of u64, then swap on individual u32s */
1072 u.val64 = bswap_64(u.val64);
1073 u.val32[0] = bswap_32(u.val32[0]);
1074 }
1075
1076 data->cpu = u.val32[0];
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001077 array++;
1078 }
1079
1080 if (type & PERF_SAMPLE_PERIOD) {
1081 data->period = *array;
1082 array++;
1083 }
1084
1085 if (type & PERF_SAMPLE_READ) {
Masanari Iidaf9d36992012-01-25 15:20:40 +01001086 fprintf(stderr, "PERF_SAMPLE_READ is unsupported for now\n");
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001087 return -1;
1088 }
1089
1090 if (type & PERF_SAMPLE_CALLCHAIN) {
Frederic Weisbecker98e1da92011-05-21 20:08:15 +02001091 if (sample_overlap(event, array, sizeof(data->callchain->nr)))
1092 return -EFAULT;
1093
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001094 data->callchain = (struct ip_callchain *)array;
Frederic Weisbecker98e1da92011-05-21 20:08:15 +02001095
1096 if (sample_overlap(event, array, data->callchain->nr))
1097 return -EFAULT;
1098
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001099 array += 1 + data->callchain->nr;
1100 }
1101
1102 if (type & PERF_SAMPLE_RAW) {
Jiri Olsa8e303f22011-09-29 17:05:08 +02001103 const u64 *pdata;
1104
David Ahern936be502011-09-06 09:12:26 -06001105 u.val64 = *array;
1106 if (WARN_ONCE(swapped,
1107 "Endianness of raw data not corrected!\n")) {
1108 /* undo swap of u64, then swap on individual u32s */
1109 u.val64 = bswap_64(u.val64);
1110 u.val32[0] = bswap_32(u.val32[0]);
1111 u.val32[1] = bswap_32(u.val32[1]);
1112 }
Frederic Weisbecker98e1da92011-05-21 20:08:15 +02001113
1114 if (sample_overlap(event, array, sizeof(u32)))
1115 return -EFAULT;
1116
David Ahern936be502011-09-06 09:12:26 -06001117 data->raw_size = u.val32[0];
Jiri Olsa8e303f22011-09-29 17:05:08 +02001118 pdata = (void *) array + sizeof(u32);
Frederic Weisbecker98e1da92011-05-21 20:08:15 +02001119
Jiri Olsa8e303f22011-09-29 17:05:08 +02001120 if (sample_overlap(event, pdata, data->raw_size))
Frederic Weisbecker98e1da92011-05-21 20:08:15 +02001121 return -EFAULT;
1122
Jiri Olsa8e303f22011-09-29 17:05:08 +02001123 data->raw_data = (void *) pdata;
Stephane Eranianfa30c962012-03-17 23:23:18 +01001124
1125 array = (void *)array + data->raw_size + sizeof(u32);
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001126 }
1127
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +01001128 if (type & PERF_SAMPLE_BRANCH_STACK) {
1129 u64 sz;
1130
1131 data->branch_stack = (struct branch_stack *)array;
1132 array++; /* nr */
1133
1134 sz = data->branch_stack->nr * sizeof(struct branch_entry);
1135 sz /= sizeof(u64);
1136 array += sz;
1137 }
Jiri Olsa0f6a3012012-08-07 15:20:45 +02001138
1139 if (type & PERF_SAMPLE_REGS_USER) {
1140 /* First u64 tells us if we have any regs in sample. */
1141 u64 avail = *array++;
1142
1143 if (avail) {
1144 data->user_regs.regs = (u64 *)array;
1145 array += hweight_long(regs_user);
1146 }
1147 }
1148
1149 if (type & PERF_SAMPLE_STACK_USER) {
1150 u64 size = *array++;
1151
1152 data->user_stack.offset = ((char *)(array - 1)
1153 - (char *) event);
1154
1155 if (!size) {
1156 data->user_stack.size = 0;
1157 } else {
1158 data->user_stack.data = (char *)array;
1159 array += size / sizeof(*array);
1160 data->user_stack.size = *array;
1161 }
1162 }
1163
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001164 return 0;
1165}
Andrew Vagin74eec262011-11-28 12:03:31 +03001166
1167int perf_event__synthesize_sample(union perf_event *event, u64 type,
1168 const struct perf_sample *sample,
1169 bool swapped)
1170{
1171 u64 *array;
1172
1173 /*
1174 * used for cross-endian analysis. See git commit 65014ab3
1175 * for why this goofiness is needed.
1176 */
Jiri Olsa6a11f922012-05-16 08:59:04 +02001177 union u64_swap u;
Andrew Vagin74eec262011-11-28 12:03:31 +03001178
1179 array = event->sample.array;
1180
1181 if (type & PERF_SAMPLE_IP) {
1182 event->ip.ip = sample->ip;
1183 array++;
1184 }
1185
1186 if (type & PERF_SAMPLE_TID) {
1187 u.val32[0] = sample->pid;
1188 u.val32[1] = sample->tid;
1189 if (swapped) {
1190 /*
Arnaldo Carvalho de Meloa3f698f2012-08-02 12:23:46 -03001191 * Inverse of what is done in perf_evsel__parse_sample
Andrew Vagin74eec262011-11-28 12:03:31 +03001192 */
1193 u.val32[0] = bswap_32(u.val32[0]);
1194 u.val32[1] = bswap_32(u.val32[1]);
1195 u.val64 = bswap_64(u.val64);
1196 }
1197
1198 *array = u.val64;
1199 array++;
1200 }
1201
1202 if (type & PERF_SAMPLE_TIME) {
1203 *array = sample->time;
1204 array++;
1205 }
1206
1207 if (type & PERF_SAMPLE_ADDR) {
1208 *array = sample->addr;
1209 array++;
1210 }
1211
1212 if (type & PERF_SAMPLE_ID) {
1213 *array = sample->id;
1214 array++;
1215 }
1216
1217 if (type & PERF_SAMPLE_STREAM_ID) {
1218 *array = sample->stream_id;
1219 array++;
1220 }
1221
1222 if (type & PERF_SAMPLE_CPU) {
1223 u.val32[0] = sample->cpu;
1224 if (swapped) {
1225 /*
Arnaldo Carvalho de Meloa3f698f2012-08-02 12:23:46 -03001226 * Inverse of what is done in perf_evsel__parse_sample
Andrew Vagin74eec262011-11-28 12:03:31 +03001227 */
1228 u.val32[0] = bswap_32(u.val32[0]);
1229 u.val64 = bswap_64(u.val64);
1230 }
1231 *array = u.val64;
1232 array++;
1233 }
1234
1235 if (type & PERF_SAMPLE_PERIOD) {
1236 *array = sample->period;
1237 array++;
1238 }
1239
1240 return 0;
1241}
Arnaldo Carvalho de Melo5555ded2012-09-11 19:24:23 -03001242
Arnaldo Carvalho de Meloefd2b922012-09-18 11:21:50 -03001243struct format_field *perf_evsel__field(struct perf_evsel *evsel, const char *name)
1244{
1245 return pevent_find_field(evsel->tp_format, name);
1246}
1247
Arnaldo Carvalho de Melo5d2074e2012-09-26 20:22:00 -03001248void *perf_evsel__rawptr(struct perf_evsel *evsel, struct perf_sample *sample,
Arnaldo Carvalho de Melo5555ded2012-09-11 19:24:23 -03001249 const char *name)
1250{
Arnaldo Carvalho de Meloefd2b922012-09-18 11:21:50 -03001251 struct format_field *field = perf_evsel__field(evsel, name);
Arnaldo Carvalho de Melo5555ded2012-09-11 19:24:23 -03001252 int offset;
1253
Arnaldo Carvalho de Meloefd2b922012-09-18 11:21:50 -03001254 if (!field)
1255 return NULL;
Arnaldo Carvalho de Melo5555ded2012-09-11 19:24:23 -03001256
1257 offset = field->offset;
1258
1259 if (field->flags & FIELD_IS_DYNAMIC) {
1260 offset = *(int *)(sample->raw_data + field->offset);
1261 offset &= 0xffff;
1262 }
1263
1264 return sample->raw_data + offset;
1265}
1266
1267u64 perf_evsel__intval(struct perf_evsel *evsel, struct perf_sample *sample,
1268 const char *name)
1269{
Arnaldo Carvalho de Meloefd2b922012-09-18 11:21:50 -03001270 struct format_field *field = perf_evsel__field(evsel, name);
Arnaldo Carvalho de Meloe6b6f672012-09-26 13:13:04 -03001271 void *ptr;
1272 u64 value;
Arnaldo Carvalho de Melo5555ded2012-09-11 19:24:23 -03001273
Arnaldo Carvalho de Meloefd2b922012-09-18 11:21:50 -03001274 if (!field)
1275 return 0;
Arnaldo Carvalho de Melo5555ded2012-09-11 19:24:23 -03001276
Arnaldo Carvalho de Meloe6b6f672012-09-26 13:13:04 -03001277 ptr = sample->raw_data + field->offset;
Arnaldo Carvalho de Melo5555ded2012-09-11 19:24:23 -03001278
Arnaldo Carvalho de Meloe6b6f672012-09-26 13:13:04 -03001279 switch (field->size) {
1280 case 1:
1281 return *(u8 *)ptr;
1282 case 2:
1283 value = *(u16 *)ptr;
1284 break;
1285 case 4:
1286 value = *(u32 *)ptr;
1287 break;
1288 case 8:
1289 value = *(u64 *)ptr;
1290 break;
1291 default:
1292 return 0;
1293 }
1294
1295 if (!evsel->needs_swap)
1296 return value;
1297
1298 switch (field->size) {
1299 case 2:
1300 return bswap_16(value);
1301 case 4:
1302 return bswap_32(value);
1303 case 8:
1304 return bswap_64(value);
1305 default:
1306 return 0;
1307 }
1308
1309 return 0;
Arnaldo Carvalho de Melo5555ded2012-09-11 19:24:23 -03001310}
Arnaldo Carvalho de Melo0698aed2012-12-10 18:17:08 -03001311
1312static int comma_fprintf(FILE *fp, bool *first, const char *fmt, ...)
1313{
1314 va_list args;
1315 int ret = 0;
1316
1317 if (!*first) {
1318 ret += fprintf(fp, ",");
1319 } else {
1320 ret += fprintf(fp, ":");
1321 *first = false;
1322 }
1323
1324 va_start(args, fmt);
1325 ret += vfprintf(fp, fmt, args);
1326 va_end(args);
1327 return ret;
1328}
1329
1330static int __if_fprintf(FILE *fp, bool *first, const char *field, u64 value)
1331{
1332 if (value == 0)
1333 return 0;
1334
1335 return comma_fprintf(fp, first, " %s: %" PRIu64, field, value);
1336}
1337
1338#define if_print(field) printed += __if_fprintf(fp, &first, #field, evsel->attr.field)
1339
Arnaldo Carvalho de Meloc79a4392012-12-11 10:54:12 -03001340struct bit_names {
1341 int bit;
1342 const char *name;
1343};
1344
1345static int bits__fprintf(FILE *fp, const char *field, u64 value,
1346 struct bit_names *bits, bool *first)
1347{
1348 int i = 0, printed = comma_fprintf(fp, first, " %s: ", field);
1349 bool first_bit = true;
1350
1351 do {
1352 if (value & bits[i].bit) {
1353 printed += fprintf(fp, "%s%s", first_bit ? "" : "|", bits[i].name);
1354 first_bit = false;
1355 }
1356 } while (bits[++i].name != NULL);
1357
1358 return printed;
1359}
1360
1361static int sample_type__fprintf(FILE *fp, bool *first, u64 value)
1362{
1363#define bit_name(n) { PERF_SAMPLE_##n, #n }
1364 struct bit_names bits[] = {
1365 bit_name(IP), bit_name(TID), bit_name(TIME), bit_name(ADDR),
1366 bit_name(READ), bit_name(CALLCHAIN), bit_name(ID), bit_name(CPU),
1367 bit_name(PERIOD), bit_name(STREAM_ID), bit_name(RAW),
1368 bit_name(BRANCH_STACK), bit_name(REGS_USER), bit_name(STACK_USER),
1369 { .name = NULL, }
1370 };
1371#undef bit_name
1372 return bits__fprintf(fp, "sample_type", value, bits, first);
1373}
1374
1375static int read_format__fprintf(FILE *fp, bool *first, u64 value)
1376{
1377#define bit_name(n) { PERF_FORMAT_##n, #n }
1378 struct bit_names bits[] = {
1379 bit_name(TOTAL_TIME_ENABLED), bit_name(TOTAL_TIME_RUNNING),
1380 bit_name(ID), bit_name(GROUP),
1381 { .name = NULL, }
1382 };
1383#undef bit_name
1384 return bits__fprintf(fp, "read_format", value, bits, first);
1385}
1386
Arnaldo Carvalho de Melo0698aed2012-12-10 18:17:08 -03001387int perf_evsel__fprintf(struct perf_evsel *evsel,
1388 struct perf_attr_details *details, FILE *fp)
1389{
1390 bool first = true;
Namhyung Kime6ab07d2013-01-22 18:09:47 +09001391 int printed = 0;
1392
Arnaldo Carvalho de Meloe35ef352013-02-06 17:20:02 -03001393 if (details->event_group) {
Namhyung Kime6ab07d2013-01-22 18:09:47 +09001394 struct perf_evsel *pos;
1395
1396 if (!perf_evsel__is_group_leader(evsel))
1397 return 0;
1398
1399 if (evsel->nr_members > 1)
1400 printed += fprintf(fp, "%s{", evsel->group_name ?: "");
1401
1402 printed += fprintf(fp, "%s", perf_evsel__name(evsel));
1403 for_each_group_member(pos, evsel)
1404 printed += fprintf(fp, ",%s", perf_evsel__name(pos));
1405
1406 if (evsel->nr_members > 1)
1407 printed += fprintf(fp, "}");
1408 goto out;
1409 }
1410
1411 printed += fprintf(fp, "%s", perf_evsel__name(evsel));
Arnaldo Carvalho de Melo0698aed2012-12-10 18:17:08 -03001412
1413 if (details->verbose || details->freq) {
1414 printed += comma_fprintf(fp, &first, " sample_freq=%" PRIu64,
1415 (u64)evsel->attr.sample_freq);
1416 }
1417
1418 if (details->verbose) {
1419 if_print(type);
1420 if_print(config);
1421 if_print(config1);
1422 if_print(config2);
1423 if_print(size);
Arnaldo Carvalho de Meloc79a4392012-12-11 10:54:12 -03001424 printed += sample_type__fprintf(fp, &first, evsel->attr.sample_type);
1425 if (evsel->attr.read_format)
1426 printed += read_format__fprintf(fp, &first, evsel->attr.read_format);
Arnaldo Carvalho de Melo0698aed2012-12-10 18:17:08 -03001427 if_print(disabled);
1428 if_print(inherit);
1429 if_print(pinned);
1430 if_print(exclusive);
1431 if_print(exclude_user);
1432 if_print(exclude_kernel);
1433 if_print(exclude_hv);
1434 if_print(exclude_idle);
1435 if_print(mmap);
1436 if_print(comm);
1437 if_print(freq);
1438 if_print(inherit_stat);
1439 if_print(enable_on_exec);
1440 if_print(task);
1441 if_print(watermark);
1442 if_print(precise_ip);
1443 if_print(mmap_data);
1444 if_print(sample_id_all);
1445 if_print(exclude_host);
1446 if_print(exclude_guest);
1447 if_print(__reserved_1);
1448 if_print(wakeup_events);
1449 if_print(bp_type);
1450 if_print(branch_sample_type);
1451 }
Namhyung Kime6ab07d2013-01-22 18:09:47 +09001452out:
Arnaldo Carvalho de Melo0698aed2012-12-10 18:17:08 -03001453 fputc('\n', fp);
1454 return ++printed;
1455}
Arnaldo Carvalho de Meloc0a54342012-12-13 14:16:30 -03001456
1457bool perf_evsel__fallback(struct perf_evsel *evsel, int err,
1458 char *msg, size_t msgsize)
1459{
1460 if ((err == ENOENT || err == ENXIO) &&
1461 evsel->attr.type == PERF_TYPE_HARDWARE &&
1462 evsel->attr.config == PERF_COUNT_HW_CPU_CYCLES) {
1463 /*
1464 * If it's cycles then fall back to hrtimer based
1465 * cpu-clock-tick sw counter, which is always available even if
1466 * no PMU support.
1467 *
1468 * PPC returns ENXIO until 2.6.37 (behavior changed with commit
1469 * b0a873e).
1470 */
1471 scnprintf(msg, msgsize, "%s",
1472"The cycles event is not supported, trying to fall back to cpu-clock-ticks");
1473
1474 evsel->attr.type = PERF_TYPE_SOFTWARE;
1475 evsel->attr.config = PERF_COUNT_SW_CPU_CLOCK;
1476
1477 free(evsel->name);
1478 evsel->name = NULL;
1479 return true;
1480 }
1481
1482 return false;
1483}
Arnaldo Carvalho de Melo56e52e82012-12-13 15:10:58 -03001484
1485int perf_evsel__open_strerror(struct perf_evsel *evsel,
1486 struct perf_target *target,
1487 int err, char *msg, size_t size)
1488{
1489 switch (err) {
1490 case EPERM:
1491 case EACCES:
1492 return scnprintf(msg, size, "%s",
1493 "You may not have permission to collect %sstats.\n"
1494 "Consider tweaking /proc/sys/kernel/perf_event_paranoid:\n"
1495 " -1 - Not paranoid at all\n"
1496 " 0 - Disallow raw tracepoint access for unpriv\n"
1497 " 1 - Disallow cpu events for unpriv\n"
1498 " 2 - Disallow kernel profiling for unpriv",
1499 target->system_wide ? "system-wide " : "");
1500 case ENOENT:
1501 return scnprintf(msg, size, "The %s event is not supported.",
1502 perf_evsel__name(evsel));
1503 case EMFILE:
1504 return scnprintf(msg, size, "%s",
1505 "Too many events are opened.\n"
1506 "Try again after reducing the number of events.");
1507 case ENODEV:
1508 if (target->cpu_list)
1509 return scnprintf(msg, size, "%s",
1510 "No such device - did you specify an out-of-range profile CPU?\n");
1511 break;
1512 case EOPNOTSUPP:
1513 if (evsel->attr.precise_ip)
1514 return scnprintf(msg, size, "%s",
1515 "\'precise\' request may not be supported. Try removing 'p' modifier.");
1516#if defined(__i386__) || defined(__x86_64__)
1517 if (evsel->attr.type == PERF_TYPE_HARDWARE)
1518 return scnprintf(msg, size, "%s",
1519 "No hardware sampling interrupt available.\n"
1520 "No APIC? If so then you can boot the kernel with the \"lapic\" boot parameter to force-enable it.");
1521#endif
1522 break;
1523 default:
1524 break;
1525 }
1526
1527 return scnprintf(msg, size,
1528 "The sys_perf_event_open() syscall returned with %d (%s) for event (%s). \n"
1529 "/bin/dmesg may provide additional information.\n"
1530 "No CONFIG_PERF_EVENTS=y kernel support configured?\n",
1531 err, strerror(err), perf_evsel__name(evsel));
1532}