blob: 3612183e2cc5831c08fffbb8fc23f880d4aed7b3 [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 Petkov85c66be2013-02-20 16:32:30 +010012#include <lk/debugfs.h>
Robert Richter4e319022013-06-11 17:29:18 +020013#include <traceevent/event-parse.h>
14#include <linux/hw_breakpoint.h>
15#include <linux/perf_event.h>
Andi Kleenbec19672013-08-04 19:41:26 -070016#include <sys/resource.h>
Robert Richter4e319022013-06-11 17:29:18 +020017#include "asm/bug.h"
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -020018#include "evsel.h"
Arnaldo Carvalho de Melo70082dd2011-01-12 17:03:24 -020019#include "evlist.h"
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -020020#include "util.h"
Arnaldo Carvalho de Melo86bd5e82011-01-03 23:09:46 -020021#include "cpumap.h"
Arnaldo Carvalho de Melofd782602011-01-18 15:15:24 -020022#include "thread_map.h"
Namhyung Kim12864b32012-04-26 14:15:22 +090023#include "target.h"
Jiri Olsa26d33022012-08-07 15:20:47 +020024#include "perf_regs.h"
Adrian Huntere3e1a542013-08-14 15:48:24 +030025#include "debug.h"
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -020026
Arnaldo Carvalho de Melo594ac612012-12-13 13:13:07 -030027static struct {
28 bool sample_id_all;
29 bool exclude_guest;
30} perf_missing_features;
31
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -020032#define FD(e, x, y) (*(int *)xyarray__entry(e->fd, x, y))
33
Adrian Hunter75562572013-08-27 11:23:09 +030034int __perf_evsel__sample_size(u64 sample_type)
Arnaldo Carvalho de Meloc2a70652011-06-02 11:04:54 -030035{
36 u64 mask = sample_type & PERF_SAMPLE_MASK;
37 int size = 0;
38 int i;
39
40 for (i = 0; i < 64; i++) {
41 if (mask & (1ULL << i))
42 size++;
43 }
44
45 size *= sizeof(u64);
46
47 return size;
48}
49
Adrian Hunter75562572013-08-27 11:23:09 +030050/**
51 * __perf_evsel__calc_id_pos - calculate id_pos.
52 * @sample_type: sample type
53 *
54 * This function returns the position of the event id (PERF_SAMPLE_ID or
55 * PERF_SAMPLE_IDENTIFIER) in a sample event i.e. in the array of struct
56 * sample_event.
57 */
58static int __perf_evsel__calc_id_pos(u64 sample_type)
59{
60 int idx = 0;
61
62 if (sample_type & PERF_SAMPLE_IDENTIFIER)
63 return 0;
64
65 if (!(sample_type & PERF_SAMPLE_ID))
66 return -1;
67
68 if (sample_type & PERF_SAMPLE_IP)
69 idx += 1;
70
71 if (sample_type & PERF_SAMPLE_TID)
72 idx += 1;
73
74 if (sample_type & PERF_SAMPLE_TIME)
75 idx += 1;
76
77 if (sample_type & PERF_SAMPLE_ADDR)
78 idx += 1;
79
80 return idx;
81}
82
83/**
84 * __perf_evsel__calc_is_pos - calculate is_pos.
85 * @sample_type: sample type
86 *
87 * This function returns the position (counting backwards) of the event id
88 * (PERF_SAMPLE_ID or PERF_SAMPLE_IDENTIFIER) in a non-sample event i.e. if
89 * sample_id_all is used there is an id sample appended to non-sample events.
90 */
91static int __perf_evsel__calc_is_pos(u64 sample_type)
92{
93 int idx = 1;
94
95 if (sample_type & PERF_SAMPLE_IDENTIFIER)
96 return 1;
97
98 if (!(sample_type & PERF_SAMPLE_ID))
99 return -1;
100
101 if (sample_type & PERF_SAMPLE_CPU)
102 idx += 1;
103
104 if (sample_type & PERF_SAMPLE_STREAM_ID)
105 idx += 1;
106
107 return idx;
108}
109
110void perf_evsel__calc_id_pos(struct perf_evsel *evsel)
111{
112 evsel->id_pos = __perf_evsel__calc_id_pos(evsel->attr.sample_type);
113 evsel->is_pos = __perf_evsel__calc_is_pos(evsel->attr.sample_type);
114}
115
Jiri Olsa4bf9ce12012-03-22 14:37:26 +0100116void hists__init(struct hists *hists)
Arnaldo Carvalho de Melo0e2a5f12011-11-04 08:16:58 -0200117{
118 memset(hists, 0, sizeof(*hists));
119 hists->entries_in_array[0] = hists->entries_in_array[1] = RB_ROOT;
120 hists->entries_in = &hists->entries_in_array[0];
121 hists->entries_collapsed = RB_ROOT;
122 hists->entries = RB_ROOT;
123 pthread_mutex_init(&hists->lock, NULL);
124}
125
Arnaldo Carvalho de Melo7be5ebe2012-12-10 14:53:43 -0300126void __perf_evsel__set_sample_bit(struct perf_evsel *evsel,
127 enum perf_event_sample_format bit)
128{
129 if (!(evsel->attr.sample_type & bit)) {
130 evsel->attr.sample_type |= bit;
131 evsel->sample_size += sizeof(u64);
Adrian Hunter75562572013-08-27 11:23:09 +0300132 perf_evsel__calc_id_pos(evsel);
Arnaldo Carvalho de Melo7be5ebe2012-12-10 14:53:43 -0300133 }
134}
135
136void __perf_evsel__reset_sample_bit(struct perf_evsel *evsel,
137 enum perf_event_sample_format bit)
138{
139 if (evsel->attr.sample_type & bit) {
140 evsel->attr.sample_type &= ~bit;
141 evsel->sample_size -= sizeof(u64);
Adrian Hunter75562572013-08-27 11:23:09 +0300142 perf_evsel__calc_id_pos(evsel);
Arnaldo Carvalho de Melo7be5ebe2012-12-10 14:53:43 -0300143 }
144}
145
Adrian Hunter75562572013-08-27 11:23:09 +0300146void perf_evsel__set_sample_id(struct perf_evsel *evsel,
147 bool can_sample_identifier)
Arnaldo Carvalho de Melo7a5a5ca2012-12-10 15:21:30 -0300148{
Adrian Hunter75562572013-08-27 11:23:09 +0300149 if (can_sample_identifier) {
150 perf_evsel__reset_sample_bit(evsel, ID);
151 perf_evsel__set_sample_bit(evsel, IDENTIFIER);
152 } else {
153 perf_evsel__set_sample_bit(evsel, ID);
154 }
Arnaldo Carvalho de Melo7a5a5ca2012-12-10 15:21:30 -0300155 evsel->attr.read_format |= PERF_FORMAT_ID;
156}
157
Arnaldo Carvalho de Meloef1d1af2011-01-18 21:41:45 -0200158void perf_evsel__init(struct perf_evsel *evsel,
159 struct perf_event_attr *attr, int idx)
160{
161 evsel->idx = idx;
162 evsel->attr = *attr;
Namhyung Kim2cfda562012-11-29 15:38:29 +0900163 evsel->leader = evsel;
Arnaldo Carvalho de Meloef1d1af2011-01-18 21:41:45 -0200164 INIT_LIST_HEAD(&evsel->node);
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300165 hists__init(&evsel->hists);
Arnaldo Carvalho de Melobde09462012-08-01 18:53:11 -0300166 evsel->sample_size = __perf_evsel__sample_size(attr->sample_type);
Adrian Hunter75562572013-08-27 11:23:09 +0300167 perf_evsel__calc_id_pos(evsel);
Arnaldo Carvalho de Meloef1d1af2011-01-18 21:41:45 -0200168}
169
Lin Ming23a2f3a2011-01-07 11:11:09 +0800170struct perf_evsel *perf_evsel__new(struct perf_event_attr *attr, int idx)
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200171{
172 struct perf_evsel *evsel = zalloc(sizeof(*evsel));
173
Arnaldo Carvalho de Meloef1d1af2011-01-18 21:41:45 -0200174 if (evsel != NULL)
175 perf_evsel__init(evsel, attr, idx);
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200176
177 return evsel;
178}
179
Arnaldo Carvalho de Melo201b7332012-09-26 20:24:19 -0300180struct event_format *event_format__new(const char *sys, const char *name)
Arnaldo Carvalho de Meloefd2b922012-09-18 11:21:50 -0300181{
182 int fd, n;
183 char *filename;
184 void *bf = NULL, *nbf;
185 size_t size = 0, alloc_size = 0;
186 struct event_format *format = NULL;
187
188 if (asprintf(&filename, "%s/%s/%s/format", tracing_events_path, sys, name) < 0)
189 goto out;
190
191 fd = open(filename, O_RDONLY);
192 if (fd < 0)
193 goto out_free_filename;
194
195 do {
196 if (size == alloc_size) {
197 alloc_size += BUFSIZ;
198 nbf = realloc(bf, alloc_size);
199 if (nbf == NULL)
200 goto out_free_bf;
201 bf = nbf;
202 }
203
David Ahern7cab84e2013-07-02 13:27:20 -0600204 n = read(fd, bf + size, alloc_size - size);
Arnaldo Carvalho de Meloefd2b922012-09-18 11:21:50 -0300205 if (n < 0)
206 goto out_free_bf;
207 size += n;
208 } while (n > 0);
209
210 pevent_parse_format(&format, bf, size, sys);
211
212out_free_bf:
213 free(bf);
214 close(fd);
215out_free_filename:
216 free(filename);
217out:
218 return format;
219}
220
221struct perf_evsel *perf_evsel__newtp(const char *sys, const char *name, int idx)
222{
223 struct perf_evsel *evsel = zalloc(sizeof(*evsel));
224
225 if (evsel != NULL) {
226 struct perf_event_attr attr = {
Arnaldo Carvalho de Melo0b80f8b32012-09-26 12:28:26 -0300227 .type = PERF_TYPE_TRACEPOINT,
228 .sample_type = (PERF_SAMPLE_RAW | PERF_SAMPLE_TIME |
229 PERF_SAMPLE_CPU | PERF_SAMPLE_PERIOD),
Arnaldo Carvalho de Meloefd2b922012-09-18 11:21:50 -0300230 };
231
Arnaldo Carvalho de Meloe48ffe22012-09-26 17:11:38 -0300232 if (asprintf(&evsel->name, "%s:%s", sys, name) < 0)
233 goto out_free;
234
Arnaldo Carvalho de Meloefd2b922012-09-18 11:21:50 -0300235 evsel->tp_format = event_format__new(sys, name);
236 if (evsel->tp_format == NULL)
237 goto out_free;
238
Arnaldo Carvalho de Melo0b80f8b32012-09-26 12:28:26 -0300239 event_attr_init(&attr);
Arnaldo Carvalho de Meloefd2b922012-09-18 11:21:50 -0300240 attr.config = evsel->tp_format->id;
Arnaldo Carvalho de Melo0b80f8b32012-09-26 12:28:26 -0300241 attr.sample_period = 1;
Arnaldo Carvalho de Meloefd2b922012-09-18 11:21:50 -0300242 perf_evsel__init(evsel, &attr, idx);
Arnaldo Carvalho de Meloefd2b922012-09-18 11:21:50 -0300243 }
244
245 return evsel;
246
247out_free:
Arnaldo Carvalho de Meloe48ffe22012-09-26 17:11:38 -0300248 free(evsel->name);
Arnaldo Carvalho de Meloefd2b922012-09-18 11:21:50 -0300249 free(evsel);
250 return NULL;
251}
252
Arnaldo Carvalho de Melo8ad70132012-09-06 13:11:18 -0300253const char *perf_evsel__hw_names[PERF_COUNT_HW_MAX] = {
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300254 "cycles",
255 "instructions",
256 "cache-references",
257 "cache-misses",
258 "branches",
259 "branch-misses",
260 "bus-cycles",
261 "stalled-cycles-frontend",
262 "stalled-cycles-backend",
263 "ref-cycles",
264};
265
Arnaldo Carvalho de Melodd4f5222012-06-13 15:52:42 -0300266static const char *__perf_evsel__hw_name(u64 config)
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300267{
268 if (config < PERF_COUNT_HW_MAX && perf_evsel__hw_names[config])
269 return perf_evsel__hw_names[config];
270
271 return "unknown-hardware";
272}
273
Arnaldo Carvalho de Melo27f18612012-06-11 13:33:09 -0300274static int perf_evsel__add_modifiers(struct perf_evsel *evsel, char *bf, size_t size)
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300275{
Arnaldo Carvalho de Melo27f18612012-06-11 13:33:09 -0300276 int colon = 0, r = 0;
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300277 struct perf_event_attr *attr = &evsel->attr;
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300278 bool exclude_guest_default = false;
279
280#define MOD_PRINT(context, mod) do { \
281 if (!attr->exclude_##context) { \
Arnaldo Carvalho de Melo27f18612012-06-11 13:33:09 -0300282 if (!colon) colon = ++r; \
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300283 r += scnprintf(bf + r, size - r, "%c", mod); \
284 } } while(0)
285
286 if (attr->exclude_kernel || attr->exclude_user || attr->exclude_hv) {
287 MOD_PRINT(kernel, 'k');
288 MOD_PRINT(user, 'u');
289 MOD_PRINT(hv, 'h');
290 exclude_guest_default = true;
291 }
292
293 if (attr->precise_ip) {
294 if (!colon)
Arnaldo Carvalho de Melo27f18612012-06-11 13:33:09 -0300295 colon = ++r;
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300296 r += scnprintf(bf + r, size - r, "%.*s", attr->precise_ip, "ppp");
297 exclude_guest_default = true;
298 }
299
300 if (attr->exclude_host || attr->exclude_guest == exclude_guest_default) {
301 MOD_PRINT(host, 'H');
302 MOD_PRINT(guest, 'G');
303 }
304#undef MOD_PRINT
305 if (colon)
Arnaldo Carvalho de Melo27f18612012-06-11 13:33:09 -0300306 bf[colon - 1] = ':';
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300307 return r;
308}
309
Arnaldo Carvalho de Melo27f18612012-06-11 13:33:09 -0300310static int perf_evsel__hw_name(struct perf_evsel *evsel, char *bf, size_t size)
311{
312 int r = scnprintf(bf, size, "%s", __perf_evsel__hw_name(evsel->attr.config));
313 return r + perf_evsel__add_modifiers(evsel, bf + r, size - r);
314}
315
Arnaldo Carvalho de Melo8ad70132012-09-06 13:11:18 -0300316const char *perf_evsel__sw_names[PERF_COUNT_SW_MAX] = {
Arnaldo Carvalho de Melo335c2f52012-06-11 14:36:20 -0300317 "cpu-clock",
318 "task-clock",
319 "page-faults",
320 "context-switches",
Arnaldo Carvalho de Melo8ad70132012-09-06 13:11:18 -0300321 "cpu-migrations",
Arnaldo Carvalho de Melo335c2f52012-06-11 14:36:20 -0300322 "minor-faults",
323 "major-faults",
324 "alignment-faults",
325 "emulation-faults",
Adrian Hunterd22d1a22013-08-31 21:50:52 +0300326 "dummy",
Arnaldo Carvalho de Melo335c2f52012-06-11 14:36:20 -0300327};
328
Arnaldo Carvalho de Melodd4f5222012-06-13 15:52:42 -0300329static const char *__perf_evsel__sw_name(u64 config)
Arnaldo Carvalho de Melo335c2f52012-06-11 14:36:20 -0300330{
331 if (config < PERF_COUNT_SW_MAX && perf_evsel__sw_names[config])
332 return perf_evsel__sw_names[config];
333 return "unknown-software";
334}
335
336static int perf_evsel__sw_name(struct perf_evsel *evsel, char *bf, size_t size)
337{
338 int r = scnprintf(bf, size, "%s", __perf_evsel__sw_name(evsel->attr.config));
339 return r + perf_evsel__add_modifiers(evsel, bf + r, size - r);
340}
341
Jiri Olsa287e74a2012-06-28 23:18:49 +0200342static int __perf_evsel__bp_name(char *bf, size_t size, u64 addr, u64 type)
343{
344 int r;
345
346 r = scnprintf(bf, size, "mem:0x%" PRIx64 ":", addr);
347
348 if (type & HW_BREAKPOINT_R)
349 r += scnprintf(bf + r, size - r, "r");
350
351 if (type & HW_BREAKPOINT_W)
352 r += scnprintf(bf + r, size - r, "w");
353
354 if (type & HW_BREAKPOINT_X)
355 r += scnprintf(bf + r, size - r, "x");
356
357 return r;
358}
359
360static int perf_evsel__bp_name(struct perf_evsel *evsel, char *bf, size_t size)
361{
362 struct perf_event_attr *attr = &evsel->attr;
363 int r = __perf_evsel__bp_name(bf, size, attr->bp_addr, attr->bp_type);
364 return r + perf_evsel__add_modifiers(evsel, bf + r, size - r);
365}
366
Arnaldo Carvalho de Melo0b668bc2012-06-11 14:08:07 -0300367const char *perf_evsel__hw_cache[PERF_COUNT_HW_CACHE_MAX]
368 [PERF_EVSEL__MAX_ALIASES] = {
369 { "L1-dcache", "l1-d", "l1d", "L1-data", },
370 { "L1-icache", "l1-i", "l1i", "L1-instruction", },
371 { "LLC", "L2", },
372 { "dTLB", "d-tlb", "Data-TLB", },
373 { "iTLB", "i-tlb", "Instruction-TLB", },
374 { "branch", "branches", "bpu", "btb", "bpc", },
375 { "node", },
376};
377
378const char *perf_evsel__hw_cache_op[PERF_COUNT_HW_CACHE_OP_MAX]
379 [PERF_EVSEL__MAX_ALIASES] = {
380 { "load", "loads", "read", },
381 { "store", "stores", "write", },
382 { "prefetch", "prefetches", "speculative-read", "speculative-load", },
383};
384
385const char *perf_evsel__hw_cache_result[PERF_COUNT_HW_CACHE_RESULT_MAX]
386 [PERF_EVSEL__MAX_ALIASES] = {
387 { "refs", "Reference", "ops", "access", },
388 { "misses", "miss", },
389};
390
391#define C(x) PERF_COUNT_HW_CACHE_##x
392#define CACHE_READ (1 << C(OP_READ))
393#define CACHE_WRITE (1 << C(OP_WRITE))
394#define CACHE_PREFETCH (1 << C(OP_PREFETCH))
395#define COP(x) (1 << x)
396
397/*
398 * cache operartion stat
399 * L1I : Read and prefetch only
400 * ITLB and BPU : Read-only
401 */
402static unsigned long perf_evsel__hw_cache_stat[C(MAX)] = {
403 [C(L1D)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
404 [C(L1I)] = (CACHE_READ | CACHE_PREFETCH),
405 [C(LL)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
406 [C(DTLB)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
407 [C(ITLB)] = (CACHE_READ),
408 [C(BPU)] = (CACHE_READ),
409 [C(NODE)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
410};
411
412bool perf_evsel__is_cache_op_valid(u8 type, u8 op)
413{
414 if (perf_evsel__hw_cache_stat[type] & COP(op))
415 return true; /* valid */
416 else
417 return false; /* invalid */
418}
419
420int __perf_evsel__hw_cache_type_op_res_name(u8 type, u8 op, u8 result,
421 char *bf, size_t size)
422{
423 if (result) {
424 return scnprintf(bf, size, "%s-%s-%s", perf_evsel__hw_cache[type][0],
425 perf_evsel__hw_cache_op[op][0],
426 perf_evsel__hw_cache_result[result][0]);
427 }
428
429 return scnprintf(bf, size, "%s-%s", perf_evsel__hw_cache[type][0],
430 perf_evsel__hw_cache_op[op][1]);
431}
432
Arnaldo Carvalho de Melodd4f5222012-06-13 15:52:42 -0300433static int __perf_evsel__hw_cache_name(u64 config, char *bf, size_t size)
Arnaldo Carvalho de Melo0b668bc2012-06-11 14:08:07 -0300434{
435 u8 op, result, type = (config >> 0) & 0xff;
436 const char *err = "unknown-ext-hardware-cache-type";
437
438 if (type > PERF_COUNT_HW_CACHE_MAX)
439 goto out_err;
440
441 op = (config >> 8) & 0xff;
442 err = "unknown-ext-hardware-cache-op";
443 if (op > PERF_COUNT_HW_CACHE_OP_MAX)
444 goto out_err;
445
446 result = (config >> 16) & 0xff;
447 err = "unknown-ext-hardware-cache-result";
448 if (result > PERF_COUNT_HW_CACHE_RESULT_MAX)
449 goto out_err;
450
451 err = "invalid-cache";
452 if (!perf_evsel__is_cache_op_valid(type, op))
453 goto out_err;
454
455 return __perf_evsel__hw_cache_type_op_res_name(type, op, result, bf, size);
456out_err:
457 return scnprintf(bf, size, "%s", err);
458}
459
460static int perf_evsel__hw_cache_name(struct perf_evsel *evsel, char *bf, size_t size)
461{
462 int ret = __perf_evsel__hw_cache_name(evsel->attr.config, bf, size);
463 return ret + perf_evsel__add_modifiers(evsel, bf + ret, size - ret);
464}
465
Arnaldo Carvalho de Melo6eef3d92012-06-13 11:53:37 -0300466static int perf_evsel__raw_name(struct perf_evsel *evsel, char *bf, size_t size)
467{
468 int ret = scnprintf(bf, size, "raw 0x%" PRIx64, evsel->attr.config);
469 return ret + perf_evsel__add_modifiers(evsel, bf + ret, size - ret);
470}
471
Arnaldo Carvalho de Melo7289f832012-06-12 12:34:58 -0300472const char *perf_evsel__name(struct perf_evsel *evsel)
Arnaldo Carvalho de Meloa4460832012-06-12 10:29:12 -0300473{
Arnaldo Carvalho de Melo7289f832012-06-12 12:34:58 -0300474 char bf[128];
Arnaldo Carvalho de Meloa4460832012-06-12 10:29:12 -0300475
Arnaldo Carvalho de Melo7289f832012-06-12 12:34:58 -0300476 if (evsel->name)
477 return evsel->name;
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300478
479 switch (evsel->attr.type) {
480 case PERF_TYPE_RAW:
Arnaldo Carvalho de Melo6eef3d92012-06-13 11:53:37 -0300481 perf_evsel__raw_name(evsel, bf, sizeof(bf));
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300482 break;
483
484 case PERF_TYPE_HARDWARE:
Arnaldo Carvalho de Melo7289f832012-06-12 12:34:58 -0300485 perf_evsel__hw_name(evsel, bf, sizeof(bf));
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300486 break;
Arnaldo Carvalho de Melo0b668bc2012-06-11 14:08:07 -0300487
488 case PERF_TYPE_HW_CACHE:
Arnaldo Carvalho de Melo7289f832012-06-12 12:34:58 -0300489 perf_evsel__hw_cache_name(evsel, bf, sizeof(bf));
Arnaldo Carvalho de Melo0b668bc2012-06-11 14:08:07 -0300490 break;
491
Arnaldo Carvalho de Melo335c2f52012-06-11 14:36:20 -0300492 case PERF_TYPE_SOFTWARE:
Arnaldo Carvalho de Melo7289f832012-06-12 12:34:58 -0300493 perf_evsel__sw_name(evsel, bf, sizeof(bf));
Arnaldo Carvalho de Melo335c2f52012-06-11 14:36:20 -0300494 break;
495
Arnaldo Carvalho de Meloa4460832012-06-12 10:29:12 -0300496 case PERF_TYPE_TRACEPOINT:
Arnaldo Carvalho de Melo7289f832012-06-12 12:34:58 -0300497 scnprintf(bf, sizeof(bf), "%s", "unknown tracepoint");
Arnaldo Carvalho de Meloa4460832012-06-12 10:29:12 -0300498 break;
499
Jiri Olsa287e74a2012-06-28 23:18:49 +0200500 case PERF_TYPE_BREAKPOINT:
501 perf_evsel__bp_name(evsel, bf, sizeof(bf));
502 break;
503
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300504 default:
Robert Richterca1b1452012-08-16 21:10:18 +0200505 scnprintf(bf, sizeof(bf), "unknown attr type: %d",
506 evsel->attr.type);
Arnaldo Carvalho de Meloa4460832012-06-12 10:29:12 -0300507 break;
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300508 }
509
Arnaldo Carvalho de Melo7289f832012-06-12 12:34:58 -0300510 evsel->name = strdup(bf);
511
512 return evsel->name ?: "unknown";
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300513}
514
Namhyung Kim717e2632013-01-22 18:09:44 +0900515const char *perf_evsel__group_name(struct perf_evsel *evsel)
516{
517 return evsel->group_name ?: "anon group";
518}
519
520int perf_evsel__group_desc(struct perf_evsel *evsel, char *buf, size_t size)
521{
522 int ret;
523 struct perf_evsel *pos;
524 const char *group_name = perf_evsel__group_name(evsel);
525
526 ret = scnprintf(buf, size, "%s", group_name);
527
528 ret += scnprintf(buf + ret, size - ret, " { %s",
529 perf_evsel__name(evsel));
530
531 for_each_group_member(pos, evsel)
532 ret += scnprintf(buf + ret, size - ret, ", %s",
533 perf_evsel__name(pos));
534
535 ret += scnprintf(buf + ret, size - ret, " }");
536
537 return ret;
538}
539
Jiri Olsa774cb492012-11-12 18:34:01 +0100540/*
541 * The enable_on_exec/disabled value strategy:
542 *
543 * 1) For any type of traced program:
544 * - all independent events and group leaders are disabled
545 * - all group members are enabled
546 *
547 * Group members are ruled by group leaders. They need to
548 * be enabled, because the group scheduling relies on that.
549 *
550 * 2) For traced programs executed by perf:
551 * - all independent events and group leaders have
552 * enable_on_exec set
553 * - we don't specifically enable or disable any event during
554 * the record command
555 *
556 * Independent events and group leaders are initially disabled
557 * and get enabled by exec. Group members are ruled by group
558 * leaders as stated in 1).
559 *
560 * 3) For traced programs attached by perf (pid/tid):
561 * - we specifically enable or disable all events during
562 * the record command
563 *
564 * When attaching events to already running traced we
565 * enable/disable events specifically, as there's no
566 * initial traced exec call.
567 */
Jiri Olsacac21422012-11-12 18:34:00 +0100568void perf_evsel__config(struct perf_evsel *evsel,
569 struct perf_record_opts *opts)
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200570{
Jiri Olsa3c176312012-10-10 17:39:03 +0200571 struct perf_evsel *leader = evsel->leader;
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200572 struct perf_event_attr *attr = &evsel->attr;
573 int track = !evsel->idx; /* only the first counter needs these */
574
Arnaldo Carvalho de Melo594ac612012-12-13 13:13:07 -0300575 attr->sample_id_all = perf_missing_features.sample_id_all ? 0 : 1;
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200576 attr->inherit = !opts->no_inherit;
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200577
Arnaldo Carvalho de Melo7be5ebe2012-12-10 14:53:43 -0300578 perf_evsel__set_sample_bit(evsel, IP);
579 perf_evsel__set_sample_bit(evsel, TID);
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200580
Jiri Olsa3c176312012-10-10 17:39:03 +0200581 if (evsel->sample_read) {
582 perf_evsel__set_sample_bit(evsel, READ);
583
584 /*
585 * We need ID even in case of single event, because
586 * PERF_SAMPLE_READ process ID specific data.
587 */
Adrian Hunter75562572013-08-27 11:23:09 +0300588 perf_evsel__set_sample_id(evsel, false);
Jiri Olsa3c176312012-10-10 17:39:03 +0200589
590 /*
591 * Apply group format only if we belong to group
592 * with more than one members.
593 */
594 if (leader->nr_members > 1) {
595 attr->read_format |= PERF_FORMAT_GROUP;
596 attr->inherit = 0;
597 }
598 }
599
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200600 /*
601 * We default some events to a 1 default interval. But keep
602 * it a weak assumption overridable by the user.
603 */
604 if (!attr->sample_period || (opts->user_freq != UINT_MAX &&
605 opts->user_interval != ULLONG_MAX)) {
606 if (opts->freq) {
Arnaldo Carvalho de Melo7be5ebe2012-12-10 14:53:43 -0300607 perf_evsel__set_sample_bit(evsel, PERIOD);
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200608 attr->freq = 1;
609 attr->sample_freq = opts->freq;
610 } else {
611 attr->sample_period = opts->default_interval;
612 }
613 }
614
Jiri Olsa3c176312012-10-10 17:39:03 +0200615 /*
616 * Disable sampling for all group members other
617 * than leader in case leader 'leads' the sampling.
618 */
619 if ((leader != evsel) && leader->sample_read) {
620 attr->sample_freq = 0;
621 attr->sample_period = 0;
622 }
623
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200624 if (opts->no_samples)
625 attr->sample_freq = 0;
626
627 if (opts->inherit_stat)
628 attr->inherit_stat = 1;
629
630 if (opts->sample_address) {
Arnaldo Carvalho de Melo7be5ebe2012-12-10 14:53:43 -0300631 perf_evsel__set_sample_bit(evsel, ADDR);
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200632 attr->mmap_data = track;
633 }
634
Jiri Olsa26d33022012-08-07 15:20:47 +0200635 if (opts->call_graph) {
Arnaldo Carvalho de Melo7be5ebe2012-12-10 14:53:43 -0300636 perf_evsel__set_sample_bit(evsel, CALLCHAIN);
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200637
Jiri Olsa26d33022012-08-07 15:20:47 +0200638 if (opts->call_graph == CALLCHAIN_DWARF) {
Arnaldo Carvalho de Melo7be5ebe2012-12-10 14:53:43 -0300639 perf_evsel__set_sample_bit(evsel, REGS_USER);
640 perf_evsel__set_sample_bit(evsel, STACK_USER);
Jiri Olsa26d33022012-08-07 15:20:47 +0200641 attr->sample_regs_user = PERF_REGS_MASK;
642 attr->sample_stack_user = opts->stack_dump_size;
643 attr->exclude_callchain_user = 1;
644 }
645 }
646
Namhyung Kime40ee742012-05-21 10:42:07 +0900647 if (perf_target__has_cpu(&opts->target))
Arnaldo Carvalho de Melo7be5ebe2012-12-10 14:53:43 -0300648 perf_evsel__set_sample_bit(evsel, CPU);
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200649
Andrew Vagin3e76ac72011-12-20 17:32:45 +0300650 if (opts->period)
Arnaldo Carvalho de Melo7be5ebe2012-12-10 14:53:43 -0300651 perf_evsel__set_sample_bit(evsel, PERIOD);
Andrew Vagin3e76ac72011-12-20 17:32:45 +0300652
Arnaldo Carvalho de Melo594ac612012-12-13 13:13:07 -0300653 if (!perf_missing_features.sample_id_all &&
Namhyung Kimd67356e2012-05-07 14:09:03 +0900654 (opts->sample_time || !opts->no_inherit ||
Namhyung Kimaa22dd42012-05-16 18:45:47 +0900655 perf_target__has_cpu(&opts->target)))
Arnaldo Carvalho de Melo7be5ebe2012-12-10 14:53:43 -0300656 perf_evsel__set_sample_bit(evsel, TIME);
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200657
658 if (opts->raw_samples) {
Arnaldo Carvalho de Melo7be5ebe2012-12-10 14:53:43 -0300659 perf_evsel__set_sample_bit(evsel, TIME);
660 perf_evsel__set_sample_bit(evsel, RAW);
661 perf_evsel__set_sample_bit(evsel, CPU);
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200662 }
663
Stephane Eranianccf49bf2013-01-24 16:10:37 +0100664 if (opts->sample_address)
665 attr->sample_type |= PERF_SAMPLE_DATA_SRC;
666
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200667 if (opts->no_delay) {
668 attr->watermark = 0;
669 attr->wakeup_events = 1;
670 }
Roberto Agostino Vitillobdfebd82012-02-09 23:21:02 +0100671 if (opts->branch_stack) {
Arnaldo Carvalho de Melo7be5ebe2012-12-10 14:53:43 -0300672 perf_evsel__set_sample_bit(evsel, BRANCH_STACK);
Roberto Agostino Vitillobdfebd82012-02-09 23:21:02 +0100673 attr->branch_sample_type = opts->branch_stack;
674 }
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200675
Andi Kleen05484292013-01-24 16:10:29 +0100676 if (opts->sample_weight)
677 attr->sample_type |= PERF_SAMPLE_WEIGHT;
678
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200679 attr->mmap = track;
680 attr->comm = track;
681
Jiri Olsa774cb492012-11-12 18:34:01 +0100682 /*
683 * XXX see the function comment above
684 *
685 * Disabling only independent events or group leaders,
686 * keeping group members enabled.
687 */
Namhyung Kim823254e2012-11-29 15:38:30 +0900688 if (perf_evsel__is_group_leader(evsel))
Jiri Olsa774cb492012-11-12 18:34:01 +0100689 attr->disabled = 1;
690
691 /*
692 * Setting enable_on_exec for independent events and
693 * group leaders for traced executed by perf.
694 */
Namhyung Kim823254e2012-11-29 15:38:30 +0900695 if (perf_target__none(&opts->target) && perf_evsel__is_group_leader(evsel))
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200696 attr->enable_on_exec = 1;
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200697}
698
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200699int perf_evsel__alloc_fd(struct perf_evsel *evsel, int ncpus, int nthreads)
700{
David Ahern4af4c952011-05-27 09:58:34 -0600701 int cpu, thread;
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200702 evsel->fd = xyarray__new(ncpus, nthreads, sizeof(int));
David Ahern4af4c952011-05-27 09:58:34 -0600703
704 if (evsel->fd) {
705 for (cpu = 0; cpu < ncpus; cpu++) {
706 for (thread = 0; thread < nthreads; thread++) {
707 FD(evsel, cpu, thread) = -1;
708 }
709 }
710 }
711
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200712 return evsel->fd != NULL ? 0 : -ENOMEM;
713}
714
Andi Kleene2407be2013-08-02 17:41:10 -0700715static int perf_evsel__run_ioctl(struct perf_evsel *evsel, int ncpus, int nthreads,
716 int ioc, void *arg)
Arnaldo Carvalho de Melo745cefc2012-09-26 15:07:39 -0300717{
718 int cpu, thread;
719
720 for (cpu = 0; cpu < ncpus; cpu++) {
721 for (thread = 0; thread < nthreads; thread++) {
722 int fd = FD(evsel, cpu, thread),
Andi Kleene2407be2013-08-02 17:41:10 -0700723 err = ioctl(fd, ioc, arg);
Arnaldo Carvalho de Melo745cefc2012-09-26 15:07:39 -0300724
725 if (err)
726 return err;
727 }
728 }
729
730 return 0;
731}
732
Andi Kleene2407be2013-08-02 17:41:10 -0700733int perf_evsel__set_filter(struct perf_evsel *evsel, int ncpus, int nthreads,
734 const char *filter)
735{
736 return perf_evsel__run_ioctl(evsel, ncpus, nthreads,
737 PERF_EVENT_IOC_SET_FILTER,
738 (void *)filter);
739}
740
741int perf_evsel__enable(struct perf_evsel *evsel, int ncpus, int nthreads)
742{
743 return perf_evsel__run_ioctl(evsel, ncpus, nthreads,
744 PERF_EVENT_IOC_ENABLE,
745 0);
746}
747
Arnaldo Carvalho de Melo70db7532011-01-12 22:39:13 -0200748int perf_evsel__alloc_id(struct perf_evsel *evsel, int ncpus, int nthreads)
749{
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -0300750 evsel->sample_id = xyarray__new(ncpus, nthreads, sizeof(struct perf_sample_id));
751 if (evsel->sample_id == NULL)
752 return -ENOMEM;
753
754 evsel->id = zalloc(ncpus * nthreads * sizeof(u64));
755 if (evsel->id == NULL) {
756 xyarray__delete(evsel->sample_id);
757 evsel->sample_id = NULL;
758 return -ENOMEM;
759 }
760
761 return 0;
Arnaldo Carvalho de Melo70db7532011-01-12 22:39:13 -0200762}
763
Frederik Deweerdta7e191c2013-03-01 13:02:27 -0500764void perf_evsel__reset_counts(struct perf_evsel *evsel, int ncpus)
765{
766 memset(evsel->counts, 0, (sizeof(*evsel->counts) +
767 (ncpus * sizeof(struct perf_counts_values))));
768}
769
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200770int perf_evsel__alloc_counts(struct perf_evsel *evsel, int ncpus)
771{
772 evsel->counts = zalloc((sizeof(*evsel->counts) +
773 (ncpus * sizeof(struct perf_counts_values))));
774 return evsel->counts != NULL ? 0 : -ENOMEM;
775}
776
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200777void perf_evsel__free_fd(struct perf_evsel *evsel)
778{
779 xyarray__delete(evsel->fd);
780 evsel->fd = NULL;
781}
782
Arnaldo Carvalho de Melo70db7532011-01-12 22:39:13 -0200783void perf_evsel__free_id(struct perf_evsel *evsel)
784{
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -0300785 xyarray__delete(evsel->sample_id);
786 evsel->sample_id = NULL;
787 free(evsel->id);
Arnaldo Carvalho de Melo70db7532011-01-12 22:39:13 -0200788 evsel->id = NULL;
789}
790
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200791void perf_evsel__close_fd(struct perf_evsel *evsel, int ncpus, int nthreads)
792{
793 int cpu, thread;
794
795 for (cpu = 0; cpu < ncpus; cpu++)
796 for (thread = 0; thread < nthreads; ++thread) {
797 close(FD(evsel, cpu, thread));
798 FD(evsel, cpu, thread) = -1;
799 }
800}
801
Namhyung Kim43f8e762013-01-25 10:44:44 +0900802void perf_evsel__free_counts(struct perf_evsel *evsel)
803{
804 free(evsel->counts);
805}
806
Arnaldo Carvalho de Meloef1d1af2011-01-18 21:41:45 -0200807void perf_evsel__exit(struct perf_evsel *evsel)
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200808{
809 assert(list_empty(&evsel->node));
Namhyung Kim736b05a2013-03-15 14:48:49 +0900810 perf_evsel__free_fd(evsel);
811 perf_evsel__free_id(evsel);
Arnaldo Carvalho de Meloef1d1af2011-01-18 21:41:45 -0200812}
813
814void perf_evsel__delete(struct perf_evsel *evsel)
815{
816 perf_evsel__exit(evsel);
Stephane Eranian023695d2011-02-14 11:20:01 +0200817 close_cgroup(evsel->cgrp);
Jiri Olsa6a4bb042012-08-08 12:22:36 +0200818 free(evsel->group_name);
Arnaldo Carvalho de Meloe48ffe22012-09-26 17:11:38 -0300819 if (evsel->tp_format)
Arnaldo Carvalho de Meloefd2b922012-09-18 11:21:50 -0300820 pevent_free_format(evsel->tp_format);
Stephane Eranianf0c55bc2011-02-16 15:10:01 +0200821 free(evsel->name);
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200822 free(evsel);
823}
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200824
Stephane Eranianc7a79c42013-01-29 12:47:43 +0100825static inline void compute_deltas(struct perf_evsel *evsel,
826 int cpu,
827 struct perf_counts_values *count)
828{
829 struct perf_counts_values tmp;
830
831 if (!evsel->prev_raw_counts)
832 return;
833
834 if (cpu == -1) {
835 tmp = evsel->prev_raw_counts->aggr;
836 evsel->prev_raw_counts->aggr = *count;
837 } else {
838 tmp = evsel->prev_raw_counts->cpu[cpu];
839 evsel->prev_raw_counts->cpu[cpu] = *count;
840 }
841
842 count->val = count->val - tmp.val;
843 count->ena = count->ena - tmp.ena;
844 count->run = count->run - tmp.run;
845}
846
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200847int __perf_evsel__read_on_cpu(struct perf_evsel *evsel,
848 int cpu, int thread, bool scale)
849{
850 struct perf_counts_values count;
851 size_t nv = scale ? 3 : 1;
852
853 if (FD(evsel, cpu, thread) < 0)
854 return -EINVAL;
855
Arnaldo Carvalho de Melo4eed11d2011-01-04 00:13:17 -0200856 if (evsel->counts == NULL && perf_evsel__alloc_counts(evsel, cpu + 1) < 0)
857 return -ENOMEM;
858
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200859 if (readn(FD(evsel, cpu, thread), &count, nv * sizeof(u64)) < 0)
860 return -errno;
861
Stephane Eranianc7a79c42013-01-29 12:47:43 +0100862 compute_deltas(evsel, cpu, &count);
863
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200864 if (scale) {
865 if (count.run == 0)
866 count.val = 0;
867 else if (count.run < count.ena)
868 count.val = (u64)((double)count.val * count.ena / count.run + 0.5);
869 } else
870 count.ena = count.run = 0;
871
872 evsel->counts->cpu[cpu] = count;
873 return 0;
874}
875
876int __perf_evsel__read(struct perf_evsel *evsel,
877 int ncpus, int nthreads, bool scale)
878{
879 size_t nv = scale ? 3 : 1;
880 int cpu, thread;
881 struct perf_counts_values *aggr = &evsel->counts->aggr, count;
882
Arnaldo Carvalho de Melo52bcd9942011-02-03 17:26:06 -0200883 aggr->val = aggr->ena = aggr->run = 0;
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200884
885 for (cpu = 0; cpu < ncpus; cpu++) {
886 for (thread = 0; thread < nthreads; thread++) {
887 if (FD(evsel, cpu, thread) < 0)
888 continue;
889
890 if (readn(FD(evsel, cpu, thread),
891 &count, nv * sizeof(u64)) < 0)
892 return -errno;
893
894 aggr->val += count.val;
895 if (scale) {
896 aggr->ena += count.ena;
897 aggr->run += count.run;
898 }
899 }
900 }
901
Stephane Eranianc7a79c42013-01-29 12:47:43 +0100902 compute_deltas(evsel, -1, aggr);
903
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200904 evsel->counts->scaled = 0;
905 if (scale) {
906 if (aggr->run == 0) {
907 evsel->counts->scaled = -1;
908 aggr->val = 0;
909 return 0;
910 }
911
912 if (aggr->run < aggr->ena) {
913 evsel->counts->scaled = 1;
914 aggr->val = (u64)((double)aggr->val * aggr->ena / aggr->run + 0.5);
915 }
916 } else
917 aggr->ena = aggr->run = 0;
918
919 return 0;
920}
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -0200921
Jiri Olsa6a4bb042012-08-08 12:22:36 +0200922static int get_group_fd(struct perf_evsel *evsel, int cpu, int thread)
923{
924 struct perf_evsel *leader = evsel->leader;
925 int fd;
926
Namhyung Kim823254e2012-11-29 15:38:30 +0900927 if (perf_evsel__is_group_leader(evsel))
Jiri Olsa6a4bb042012-08-08 12:22:36 +0200928 return -1;
929
930 /*
931 * Leader must be already processed/open,
932 * if not it's a bug.
933 */
934 BUG_ON(!leader->fd);
935
936 fd = FD(leader, cpu, thread);
937 BUG_ON(fd == -1);
938
939 return fd;
940}
941
Adrian Huntere3e1a542013-08-14 15:48:24 +0300942#define __PRINT_ATTR(fmt, cast, field) \
943 fprintf(fp, " %-19s "fmt"\n", #field, cast attr->field)
944
945#define PRINT_ATTR_U32(field) __PRINT_ATTR("%u" , , field)
946#define PRINT_ATTR_X32(field) __PRINT_ATTR("%#x", , field)
947#define PRINT_ATTR_U64(field) __PRINT_ATTR("%" PRIu64, (uint64_t), field)
948#define PRINT_ATTR_X64(field) __PRINT_ATTR("%#"PRIx64, (uint64_t), field)
949
950#define PRINT_ATTR2N(name1, field1, name2, field2) \
951 fprintf(fp, " %-19s %u %-19s %u\n", \
952 name1, attr->field1, name2, attr->field2)
953
954#define PRINT_ATTR2(field1, field2) \
955 PRINT_ATTR2N(#field1, field1, #field2, field2)
956
957static size_t perf_event_attr__fprintf(struct perf_event_attr *attr, FILE *fp)
958{
959 size_t ret = 0;
960
961 ret += fprintf(fp, "%.60s\n", graph_dotted_line);
962 ret += fprintf(fp, "perf_event_attr:\n");
963
964 ret += PRINT_ATTR_U32(type);
965 ret += PRINT_ATTR_U32(size);
966 ret += PRINT_ATTR_X64(config);
967 ret += PRINT_ATTR_U64(sample_period);
968 ret += PRINT_ATTR_U64(sample_freq);
969 ret += PRINT_ATTR_X64(sample_type);
970 ret += PRINT_ATTR_X64(read_format);
971
972 ret += PRINT_ATTR2(disabled, inherit);
973 ret += PRINT_ATTR2(pinned, exclusive);
974 ret += PRINT_ATTR2(exclude_user, exclude_kernel);
975 ret += PRINT_ATTR2(exclude_hv, exclude_idle);
976 ret += PRINT_ATTR2(mmap, comm);
977 ret += PRINT_ATTR2(freq, inherit_stat);
978 ret += PRINT_ATTR2(enable_on_exec, task);
979 ret += PRINT_ATTR2(watermark, precise_ip);
980 ret += PRINT_ATTR2(mmap_data, sample_id_all);
981 ret += PRINT_ATTR2(exclude_host, exclude_guest);
982 ret += PRINT_ATTR2N("excl.callchain_kern", exclude_callchain_kernel,
983 "excl.callchain_user", exclude_callchain_user);
984
985 ret += PRINT_ATTR_U32(wakeup_events);
986 ret += PRINT_ATTR_U32(wakeup_watermark);
987 ret += PRINT_ATTR_X32(bp_type);
988 ret += PRINT_ATTR_X64(bp_addr);
989 ret += PRINT_ATTR_X64(config1);
990 ret += PRINT_ATTR_U64(bp_len);
991 ret += PRINT_ATTR_X64(config2);
992 ret += PRINT_ATTR_X64(branch_sample_type);
993 ret += PRINT_ATTR_X64(sample_regs_user);
994 ret += PRINT_ATTR_U32(sample_stack_user);
995
996 ret += fprintf(fp, "%.60s\n", graph_dotted_line);
997
998 return ret;
999}
1000
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -02001001static int __perf_evsel__open(struct perf_evsel *evsel, struct cpu_map *cpus,
Jiri Olsa6a4bb042012-08-08 12:22:36 +02001002 struct thread_map *threads)
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -02001003{
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -02001004 int cpu, thread;
Stephane Eranian023695d2011-02-14 11:20:01 +02001005 unsigned long flags = 0;
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -02001006 int pid = -1, err;
Andi Kleenbec19672013-08-04 19:41:26 -07001007 enum { NO_CHANGE, SET_TO_MAX, INCREASED_MAX } set_rlimit = NO_CHANGE;
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -02001008
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -02001009 if (evsel->fd == NULL &&
1010 perf_evsel__alloc_fd(evsel, cpus->nr, threads->nr) < 0)
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -02001011 return -ENOMEM;
Arnaldo Carvalho de Melo4eed11d2011-01-04 00:13:17 -02001012
Stephane Eranian023695d2011-02-14 11:20:01 +02001013 if (evsel->cgrp) {
1014 flags = PERF_FLAG_PID_CGROUP;
1015 pid = evsel->cgrp->fd;
1016 }
1017
Arnaldo Carvalho de Melo594ac612012-12-13 13:13:07 -03001018fallback_missing_features:
1019 if (perf_missing_features.exclude_guest)
1020 evsel->attr.exclude_guest = evsel->attr.exclude_host = 0;
1021retry_sample_id:
1022 if (perf_missing_features.sample_id_all)
1023 evsel->attr.sample_id_all = 0;
1024
Adrian Huntere3e1a542013-08-14 15:48:24 +03001025 if (verbose >= 2)
1026 perf_event_attr__fprintf(&evsel->attr, stderr);
1027
Arnaldo Carvalho de Melo86bd5e82011-01-03 23:09:46 -02001028 for (cpu = 0; cpu < cpus->nr; cpu++) {
Arnaldo Carvalho de Melo9d04f172011-01-12 00:08:18 -02001029
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -02001030 for (thread = 0; thread < threads->nr; thread++) {
Jiri Olsa6a4bb042012-08-08 12:22:36 +02001031 int group_fd;
Stephane Eranian023695d2011-02-14 11:20:01 +02001032
1033 if (!evsel->cgrp)
1034 pid = threads->map[thread];
1035
Jiri Olsa6a4bb042012-08-08 12:22:36 +02001036 group_fd = get_group_fd(evsel, cpu, thread);
Andi Kleenbec19672013-08-04 19:41:26 -07001037retry_open:
Adrian Huntere3e1a542013-08-14 15:48:24 +03001038 pr_debug2("perf_event_open: pid %d cpu %d group_fd %d flags %#lx\n",
1039 pid, cpus->map[cpu], group_fd, flags);
1040
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -02001041 FD(evsel, cpu, thread) = sys_perf_event_open(&evsel->attr,
Stephane Eranian023695d2011-02-14 11:20:01 +02001042 pid,
Arnaldo Carvalho de Melof08199d2011-01-11 23:42:19 -02001043 cpus->map[cpu],
Stephane Eranian023695d2011-02-14 11:20:01 +02001044 group_fd, flags);
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -02001045 if (FD(evsel, cpu, thread) < 0) {
1046 err = -errno;
Arnaldo Carvalho de Melo594ac612012-12-13 13:13:07 -03001047 goto try_fallback;
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -02001048 }
Andi Kleenbec19672013-08-04 19:41:26 -07001049 set_rlimit = NO_CHANGE;
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -02001050 }
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -02001051 }
1052
1053 return 0;
1054
Arnaldo Carvalho de Melo594ac612012-12-13 13:13:07 -03001055try_fallback:
Andi Kleenbec19672013-08-04 19:41:26 -07001056 /*
1057 * perf stat needs between 5 and 22 fds per CPU. When we run out
1058 * of them try to increase the limits.
1059 */
1060 if (err == -EMFILE && set_rlimit < INCREASED_MAX) {
1061 struct rlimit l;
1062 int old_errno = errno;
1063
1064 if (getrlimit(RLIMIT_NOFILE, &l) == 0) {
1065 if (set_rlimit == NO_CHANGE)
1066 l.rlim_cur = l.rlim_max;
1067 else {
1068 l.rlim_cur = l.rlim_max + 1000;
1069 l.rlim_max = l.rlim_cur;
1070 }
1071 if (setrlimit(RLIMIT_NOFILE, &l) == 0) {
1072 set_rlimit++;
1073 errno = old_errno;
1074 goto retry_open;
1075 }
1076 }
1077 errno = old_errno;
1078 }
1079
Arnaldo Carvalho de Melo594ac612012-12-13 13:13:07 -03001080 if (err != -EINVAL || cpu > 0 || thread > 0)
1081 goto out_close;
1082
1083 if (!perf_missing_features.exclude_guest &&
1084 (evsel->attr.exclude_guest || evsel->attr.exclude_host)) {
1085 perf_missing_features.exclude_guest = true;
1086 goto fallback_missing_features;
1087 } else if (!perf_missing_features.sample_id_all) {
1088 perf_missing_features.sample_id_all = true;
1089 goto retry_sample_id;
1090 }
1091
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -02001092out_close:
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -02001093 do {
1094 while (--thread >= 0) {
1095 close(FD(evsel, cpu, thread));
1096 FD(evsel, cpu, thread) = -1;
1097 }
1098 thread = threads->nr;
1099 } while (--cpu >= 0);
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -02001100 return err;
1101}
1102
1103void perf_evsel__close(struct perf_evsel *evsel, int ncpus, int nthreads)
1104{
1105 if (evsel->fd == NULL)
1106 return;
1107
1108 perf_evsel__close_fd(evsel, ncpus, nthreads);
1109 perf_evsel__free_fd(evsel);
1110 evsel->fd = NULL;
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -02001111}
1112
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -02001113static struct {
1114 struct cpu_map map;
1115 int cpus[1];
1116} empty_cpu_map = {
1117 .map.nr = 1,
1118 .cpus = { -1, },
1119};
1120
1121static struct {
1122 struct thread_map map;
1123 int threads[1];
1124} empty_thread_map = {
1125 .map.nr = 1,
1126 .threads = { -1, },
1127};
1128
Arnaldo Carvalho de Melof08199d2011-01-11 23:42:19 -02001129int perf_evsel__open(struct perf_evsel *evsel, struct cpu_map *cpus,
Jiri Olsa6a4bb042012-08-08 12:22:36 +02001130 struct thread_map *threads)
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -02001131{
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -02001132 if (cpus == NULL) {
1133 /* Work around old compiler warnings about strict aliasing */
1134 cpus = &empty_cpu_map.map;
1135 }
1136
1137 if (threads == NULL)
1138 threads = &empty_thread_map.map;
1139
Jiri Olsa6a4bb042012-08-08 12:22:36 +02001140 return __perf_evsel__open(evsel, cpus, threads);
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -02001141}
1142
Arnaldo Carvalho de Melof08199d2011-01-11 23:42:19 -02001143int perf_evsel__open_per_cpu(struct perf_evsel *evsel,
Jiri Olsa6a4bb042012-08-08 12:22:36 +02001144 struct cpu_map *cpus)
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -02001145{
Jiri Olsa6a4bb042012-08-08 12:22:36 +02001146 return __perf_evsel__open(evsel, cpus, &empty_thread_map.map);
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -02001147}
1148
Arnaldo Carvalho de Melof08199d2011-01-11 23:42:19 -02001149int perf_evsel__open_per_thread(struct perf_evsel *evsel,
Jiri Olsa6a4bb042012-08-08 12:22:36 +02001150 struct thread_map *threads)
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -02001151{
Jiri Olsa6a4bb042012-08-08 12:22:36 +02001152 return __perf_evsel__open(evsel, &empty_cpu_map.map, threads);
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -02001153}
Arnaldo Carvalho de Melo70082dd2011-01-12 17:03:24 -02001154
Arnaldo Carvalho de Melo0807d2d2012-09-26 12:48:18 -03001155static int perf_evsel__parse_id_sample(const struct perf_evsel *evsel,
1156 const union perf_event *event,
1157 struct perf_sample *sample)
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001158{
Arnaldo Carvalho de Melo0807d2d2012-09-26 12:48:18 -03001159 u64 type = evsel->attr.sample_type;
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001160 const u64 *array = event->sample.array;
Arnaldo Carvalho de Melo0807d2d2012-09-26 12:48:18 -03001161 bool swapped = evsel->needs_swap;
Jiri Olsa37073f92012-05-30 14:23:44 +02001162 union u64_swap u;
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001163
1164 array += ((event->header.size -
1165 sizeof(event->header)) / sizeof(u64)) - 1;
1166
Adrian Hunter75562572013-08-27 11:23:09 +03001167 if (type & PERF_SAMPLE_IDENTIFIER) {
1168 sample->id = *array;
1169 array--;
1170 }
1171
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001172 if (type & PERF_SAMPLE_CPU) {
Jiri Olsa37073f92012-05-30 14:23:44 +02001173 u.val64 = *array;
1174 if (swapped) {
1175 /* undo swap of u64, then swap on individual u32s */
1176 u.val64 = bswap_64(u.val64);
1177 u.val32[0] = bswap_32(u.val32[0]);
1178 }
1179
1180 sample->cpu = u.val32[0];
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001181 array--;
1182 }
1183
1184 if (type & PERF_SAMPLE_STREAM_ID) {
1185 sample->stream_id = *array;
1186 array--;
1187 }
1188
1189 if (type & PERF_SAMPLE_ID) {
1190 sample->id = *array;
1191 array--;
1192 }
1193
1194 if (type & PERF_SAMPLE_TIME) {
1195 sample->time = *array;
1196 array--;
1197 }
1198
1199 if (type & PERF_SAMPLE_TID) {
Jiri Olsa37073f92012-05-30 14:23:44 +02001200 u.val64 = *array;
1201 if (swapped) {
1202 /* undo swap of u64, then swap on individual u32s */
1203 u.val64 = bswap_64(u.val64);
1204 u.val32[0] = bswap_32(u.val32[0]);
1205 u.val32[1] = bswap_32(u.val32[1]);
1206 }
1207
1208 sample->pid = u.val32[0];
1209 sample->tid = u.val32[1];
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001210 }
1211
1212 return 0;
1213}
1214
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001215static inline bool overflow(const void *endp, u16 max_size, const void *offset,
1216 u64 size)
Frederic Weisbecker98e1da92011-05-21 20:08:15 +02001217{
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001218 return size > max_size || offset + size > endp;
Frederic Weisbecker98e1da92011-05-21 20:08:15 +02001219}
1220
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001221#define OVERFLOW_CHECK(offset, size, max_size) \
1222 do { \
1223 if (overflow(endp, (max_size), (offset), (size))) \
1224 return -EFAULT; \
1225 } while (0)
1226
1227#define OVERFLOW_CHECK_u64(offset) \
1228 OVERFLOW_CHECK(offset, sizeof(u64), sizeof(u64))
1229
Arnaldo Carvalho de Meloa3f698f2012-08-02 12:23:46 -03001230int perf_evsel__parse_sample(struct perf_evsel *evsel, union perf_event *event,
Arnaldo Carvalho de Melo0807d2d2012-09-26 12:48:18 -03001231 struct perf_sample *data)
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001232{
Arnaldo Carvalho de Meloa3f698f2012-08-02 12:23:46 -03001233 u64 type = evsel->attr.sample_type;
Arnaldo Carvalho de Melo0807d2d2012-09-26 12:48:18 -03001234 bool swapped = evsel->needs_swap;
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001235 const u64 *array;
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001236 u16 max_size = event->header.size;
1237 const void *endp = (void *)event + max_size;
1238 u64 sz;
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001239
David Ahern936be502011-09-06 09:12:26 -06001240 /*
1241 * used for cross-endian analysis. See git commit 65014ab3
1242 * for why this goofiness is needed.
1243 */
Jiri Olsa6a11f922012-05-16 08:59:04 +02001244 union u64_swap u;
David Ahern936be502011-09-06 09:12:26 -06001245
Robert Richterf3bda2c2011-12-15 17:32:39 +01001246 memset(data, 0, sizeof(*data));
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001247 data->cpu = data->pid = data->tid = -1;
1248 data->stream_id = data->id = data->time = -1ULL;
Naveen N. Raoa4a03fc2012-02-03 22:31:13 +05301249 data->period = 1;
Andi Kleen05484292013-01-24 16:10:29 +01001250 data->weight = 0;
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001251
1252 if (event->header.type != PERF_RECORD_SAMPLE) {
Arnaldo Carvalho de Meloa3f698f2012-08-02 12:23:46 -03001253 if (!evsel->attr.sample_id_all)
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001254 return 0;
Arnaldo Carvalho de Melo0807d2d2012-09-26 12:48:18 -03001255 return perf_evsel__parse_id_sample(evsel, event, data);
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001256 }
1257
1258 array = event->sample.array;
1259
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001260 /*
1261 * The evsel's sample_size is based on PERF_SAMPLE_MASK which includes
1262 * up to PERF_SAMPLE_PERIOD. After that overflow() must be used to
1263 * check the format does not go past the end of the event.
1264 */
Arnaldo Carvalho de Meloa3f698f2012-08-02 12:23:46 -03001265 if (evsel->sample_size + sizeof(event->header) > event->header.size)
Frederic Weisbeckera2854122011-05-21 19:33:04 +02001266 return -EFAULT;
1267
Adrian Hunter75562572013-08-27 11:23:09 +03001268 data->id = -1ULL;
1269 if (type & PERF_SAMPLE_IDENTIFIER) {
1270 data->id = *array;
1271 array++;
1272 }
1273
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001274 if (type & PERF_SAMPLE_IP) {
Adrian Hunteref893252013-08-27 11:23:06 +03001275 data->ip = *array;
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001276 array++;
1277 }
1278
1279 if (type & PERF_SAMPLE_TID) {
David Ahern936be502011-09-06 09:12:26 -06001280 u.val64 = *array;
1281 if (swapped) {
1282 /* undo swap of u64, then swap on individual u32s */
1283 u.val64 = bswap_64(u.val64);
1284 u.val32[0] = bswap_32(u.val32[0]);
1285 u.val32[1] = bswap_32(u.val32[1]);
1286 }
1287
1288 data->pid = u.val32[0];
1289 data->tid = u.val32[1];
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001290 array++;
1291 }
1292
1293 if (type & PERF_SAMPLE_TIME) {
1294 data->time = *array;
1295 array++;
1296 }
1297
David Ahern7cec0922011-05-30 13:08:23 -06001298 data->addr = 0;
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001299 if (type & PERF_SAMPLE_ADDR) {
1300 data->addr = *array;
1301 array++;
1302 }
1303
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001304 if (type & PERF_SAMPLE_ID) {
1305 data->id = *array;
1306 array++;
1307 }
1308
1309 if (type & PERF_SAMPLE_STREAM_ID) {
1310 data->stream_id = *array;
1311 array++;
1312 }
1313
1314 if (type & PERF_SAMPLE_CPU) {
David Ahern936be502011-09-06 09:12:26 -06001315
1316 u.val64 = *array;
1317 if (swapped) {
1318 /* undo swap of u64, then swap on individual u32s */
1319 u.val64 = bswap_64(u.val64);
1320 u.val32[0] = bswap_32(u.val32[0]);
1321 }
1322
1323 data->cpu = u.val32[0];
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001324 array++;
1325 }
1326
1327 if (type & PERF_SAMPLE_PERIOD) {
1328 data->period = *array;
1329 array++;
1330 }
1331
1332 if (type & PERF_SAMPLE_READ) {
Jiri Olsa9ede4732012-10-10 17:38:13 +02001333 u64 read_format = evsel->attr.read_format;
1334
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001335 OVERFLOW_CHECK_u64(array);
Jiri Olsa9ede4732012-10-10 17:38:13 +02001336 if (read_format & PERF_FORMAT_GROUP)
1337 data->read.group.nr = *array;
1338 else
1339 data->read.one.value = *array;
1340
1341 array++;
1342
1343 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) {
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001344 OVERFLOW_CHECK_u64(array);
Jiri Olsa9ede4732012-10-10 17:38:13 +02001345 data->read.time_enabled = *array;
1346 array++;
1347 }
1348
1349 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) {
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001350 OVERFLOW_CHECK_u64(array);
Jiri Olsa9ede4732012-10-10 17:38:13 +02001351 data->read.time_running = *array;
1352 array++;
1353 }
1354
1355 /* PERF_FORMAT_ID is forced for PERF_SAMPLE_READ */
1356 if (read_format & PERF_FORMAT_GROUP) {
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001357 const u64 max_group_nr = UINT64_MAX /
1358 sizeof(struct sample_read_value);
1359
1360 if (data->read.group.nr > max_group_nr)
1361 return -EFAULT;
1362 sz = data->read.group.nr *
1363 sizeof(struct sample_read_value);
1364 OVERFLOW_CHECK(array, sz, max_size);
1365 data->read.group.values =
1366 (struct sample_read_value *)array;
1367 array = (void *)array + sz;
Jiri Olsa9ede4732012-10-10 17:38:13 +02001368 } else {
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001369 OVERFLOW_CHECK_u64(array);
Jiri Olsa9ede4732012-10-10 17:38:13 +02001370 data->read.one.id = *array;
1371 array++;
1372 }
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001373 }
1374
1375 if (type & PERF_SAMPLE_CALLCHAIN) {
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001376 const u64 max_callchain_nr = UINT64_MAX / sizeof(u64);
1377
1378 OVERFLOW_CHECK_u64(array);
1379 data->callchain = (struct ip_callchain *)array++;
1380 if (data->callchain->nr > max_callchain_nr)
Frederic Weisbecker98e1da92011-05-21 20:08:15 +02001381 return -EFAULT;
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001382 sz = data->callchain->nr * sizeof(u64);
1383 OVERFLOW_CHECK(array, sz, max_size);
1384 array = (void *)array + sz;
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001385 }
1386
1387 if (type & PERF_SAMPLE_RAW) {
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001388 OVERFLOW_CHECK_u64(array);
David Ahern936be502011-09-06 09:12:26 -06001389 u.val64 = *array;
1390 if (WARN_ONCE(swapped,
1391 "Endianness of raw data not corrected!\n")) {
1392 /* undo swap of u64, then swap on individual u32s */
1393 u.val64 = bswap_64(u.val64);
1394 u.val32[0] = bswap_32(u.val32[0]);
1395 u.val32[1] = bswap_32(u.val32[1]);
1396 }
David Ahern936be502011-09-06 09:12:26 -06001397 data->raw_size = u.val32[0];
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001398 array = (void *)array + sizeof(u32);
Frederic Weisbecker98e1da92011-05-21 20:08:15 +02001399
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001400 OVERFLOW_CHECK(array, data->raw_size, max_size);
1401 data->raw_data = (void *)array;
1402 array = (void *)array + data->raw_size;
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001403 }
1404
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +01001405 if (type & PERF_SAMPLE_BRANCH_STACK) {
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001406 const u64 max_branch_nr = UINT64_MAX /
1407 sizeof(struct branch_entry);
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +01001408
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001409 OVERFLOW_CHECK_u64(array);
1410 data->branch_stack = (struct branch_stack *)array++;
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +01001411
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001412 if (data->branch_stack->nr > max_branch_nr)
1413 return -EFAULT;
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +01001414 sz = data->branch_stack->nr * sizeof(struct branch_entry);
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001415 OVERFLOW_CHECK(array, sz, max_size);
1416 array = (void *)array + sz;
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +01001417 }
Jiri Olsa0f6a3012012-08-07 15:20:45 +02001418
1419 if (type & PERF_SAMPLE_REGS_USER) {
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001420 OVERFLOW_CHECK_u64(array);
Adrian Hunter5b95a4a32013-08-27 11:23:10 +03001421 data->user_regs.abi = *array;
1422 array++;
Jiri Olsa0f6a3012012-08-07 15:20:45 +02001423
Adrian Hunter5b95a4a32013-08-27 11:23:10 +03001424 if (data->user_regs.abi) {
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001425 u64 regs_user = evsel->attr.sample_regs_user;
1426
1427 sz = hweight_long(regs_user) * sizeof(u64);
1428 OVERFLOW_CHECK(array, sz, max_size);
Jiri Olsa0f6a3012012-08-07 15:20:45 +02001429 data->user_regs.regs = (u64 *)array;
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001430 array = (void *)array + sz;
Jiri Olsa0f6a3012012-08-07 15:20:45 +02001431 }
1432 }
1433
1434 if (type & PERF_SAMPLE_STACK_USER) {
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001435 OVERFLOW_CHECK_u64(array);
1436 sz = *array++;
Jiri Olsa0f6a3012012-08-07 15:20:45 +02001437
1438 data->user_stack.offset = ((char *)(array - 1)
1439 - (char *) event);
1440
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001441 if (!sz) {
Jiri Olsa0f6a3012012-08-07 15:20:45 +02001442 data->user_stack.size = 0;
1443 } else {
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001444 OVERFLOW_CHECK(array, sz, max_size);
Jiri Olsa0f6a3012012-08-07 15:20:45 +02001445 data->user_stack.data = (char *)array;
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001446 array = (void *)array + sz;
1447 OVERFLOW_CHECK_u64(array);
Adrian Hunter54bd2692013-07-04 16:20:34 +03001448 data->user_stack.size = *array++;
Jiri Olsa0f6a3012012-08-07 15:20:45 +02001449 }
1450 }
1451
Andi Kleen05484292013-01-24 16:10:29 +01001452 data->weight = 0;
1453 if (type & PERF_SAMPLE_WEIGHT) {
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001454 OVERFLOW_CHECK_u64(array);
Andi Kleen05484292013-01-24 16:10:29 +01001455 data->weight = *array;
1456 array++;
1457 }
1458
Stephane Eranian98a3b322013-01-24 16:10:35 +01001459 data->data_src = PERF_MEM_DATA_SRC_NONE;
1460 if (type & PERF_SAMPLE_DATA_SRC) {
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001461 OVERFLOW_CHECK_u64(array);
Stephane Eranian98a3b322013-01-24 16:10:35 +01001462 data->data_src = *array;
1463 array++;
1464 }
1465
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001466 return 0;
1467}
Andrew Vagin74eec262011-11-28 12:03:31 +03001468
Adrian Hunterb1cf6f62013-08-27 11:23:12 +03001469size_t perf_event__sample_event_size(const struct perf_sample *sample, u64 type,
1470 u64 sample_regs_user, u64 read_format)
1471{
1472 size_t sz, result = sizeof(struct sample_event);
1473
1474 if (type & PERF_SAMPLE_IDENTIFIER)
1475 result += sizeof(u64);
1476
1477 if (type & PERF_SAMPLE_IP)
1478 result += sizeof(u64);
1479
1480 if (type & PERF_SAMPLE_TID)
1481 result += sizeof(u64);
1482
1483 if (type & PERF_SAMPLE_TIME)
1484 result += sizeof(u64);
1485
1486 if (type & PERF_SAMPLE_ADDR)
1487 result += sizeof(u64);
1488
1489 if (type & PERF_SAMPLE_ID)
1490 result += sizeof(u64);
1491
1492 if (type & PERF_SAMPLE_STREAM_ID)
1493 result += sizeof(u64);
1494
1495 if (type & PERF_SAMPLE_CPU)
1496 result += sizeof(u64);
1497
1498 if (type & PERF_SAMPLE_PERIOD)
1499 result += sizeof(u64);
1500
1501 if (type & PERF_SAMPLE_READ) {
1502 result += sizeof(u64);
1503 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
1504 result += sizeof(u64);
1505 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
1506 result += sizeof(u64);
1507 /* PERF_FORMAT_ID is forced for PERF_SAMPLE_READ */
1508 if (read_format & PERF_FORMAT_GROUP) {
1509 sz = sample->read.group.nr *
1510 sizeof(struct sample_read_value);
1511 result += sz;
1512 } else {
1513 result += sizeof(u64);
1514 }
1515 }
1516
1517 if (type & PERF_SAMPLE_CALLCHAIN) {
1518 sz = (sample->callchain->nr + 1) * sizeof(u64);
1519 result += sz;
1520 }
1521
1522 if (type & PERF_SAMPLE_RAW) {
1523 result += sizeof(u32);
1524 result += sample->raw_size;
1525 }
1526
1527 if (type & PERF_SAMPLE_BRANCH_STACK) {
1528 sz = sample->branch_stack->nr * sizeof(struct branch_entry);
1529 sz += sizeof(u64);
1530 result += sz;
1531 }
1532
1533 if (type & PERF_SAMPLE_REGS_USER) {
1534 if (sample->user_regs.abi) {
1535 result += sizeof(u64);
1536 sz = hweight_long(sample_regs_user) * sizeof(u64);
1537 result += sz;
1538 } else {
1539 result += sizeof(u64);
1540 }
1541 }
1542
1543 if (type & PERF_SAMPLE_STACK_USER) {
1544 sz = sample->user_stack.size;
1545 result += sizeof(u64);
1546 if (sz) {
1547 result += sz;
1548 result += sizeof(u64);
1549 }
1550 }
1551
1552 if (type & PERF_SAMPLE_WEIGHT)
1553 result += sizeof(u64);
1554
1555 if (type & PERF_SAMPLE_DATA_SRC)
1556 result += sizeof(u64);
1557
1558 return result;
1559}
1560
Andrew Vagin74eec262011-11-28 12:03:31 +03001561int perf_event__synthesize_sample(union perf_event *event, u64 type,
Adrian Hunterd03f2172013-08-27 11:23:11 +03001562 u64 sample_regs_user, u64 read_format,
Andrew Vagin74eec262011-11-28 12:03:31 +03001563 const struct perf_sample *sample,
1564 bool swapped)
1565{
1566 u64 *array;
Adrian Hunterd03f2172013-08-27 11:23:11 +03001567 size_t sz;
Andrew Vagin74eec262011-11-28 12:03:31 +03001568 /*
1569 * used for cross-endian analysis. See git commit 65014ab3
1570 * for why this goofiness is needed.
1571 */
Jiri Olsa6a11f922012-05-16 08:59:04 +02001572 union u64_swap u;
Andrew Vagin74eec262011-11-28 12:03:31 +03001573
1574 array = event->sample.array;
1575
Adrian Hunter75562572013-08-27 11:23:09 +03001576 if (type & PERF_SAMPLE_IDENTIFIER) {
1577 *array = sample->id;
1578 array++;
1579 }
1580
Andrew Vagin74eec262011-11-28 12:03:31 +03001581 if (type & PERF_SAMPLE_IP) {
Adrian Hunteref893252013-08-27 11:23:06 +03001582 *array = sample->ip;
Andrew Vagin74eec262011-11-28 12:03:31 +03001583 array++;
1584 }
1585
1586 if (type & PERF_SAMPLE_TID) {
1587 u.val32[0] = sample->pid;
1588 u.val32[1] = sample->tid;
1589 if (swapped) {
1590 /*
Arnaldo Carvalho de Meloa3f698f2012-08-02 12:23:46 -03001591 * Inverse of what is done in perf_evsel__parse_sample
Andrew Vagin74eec262011-11-28 12:03:31 +03001592 */
1593 u.val32[0] = bswap_32(u.val32[0]);
1594 u.val32[1] = bswap_32(u.val32[1]);
1595 u.val64 = bswap_64(u.val64);
1596 }
1597
1598 *array = u.val64;
1599 array++;
1600 }
1601
1602 if (type & PERF_SAMPLE_TIME) {
1603 *array = sample->time;
1604 array++;
1605 }
1606
1607 if (type & PERF_SAMPLE_ADDR) {
1608 *array = sample->addr;
1609 array++;
1610 }
1611
1612 if (type & PERF_SAMPLE_ID) {
1613 *array = sample->id;
1614 array++;
1615 }
1616
1617 if (type & PERF_SAMPLE_STREAM_ID) {
1618 *array = sample->stream_id;
1619 array++;
1620 }
1621
1622 if (type & PERF_SAMPLE_CPU) {
1623 u.val32[0] = sample->cpu;
1624 if (swapped) {
1625 /*
Arnaldo Carvalho de Meloa3f698f2012-08-02 12:23:46 -03001626 * Inverse of what is done in perf_evsel__parse_sample
Andrew Vagin74eec262011-11-28 12:03:31 +03001627 */
1628 u.val32[0] = bswap_32(u.val32[0]);
1629 u.val64 = bswap_64(u.val64);
1630 }
1631 *array = u.val64;
1632 array++;
1633 }
1634
1635 if (type & PERF_SAMPLE_PERIOD) {
1636 *array = sample->period;
1637 array++;
1638 }
1639
Adrian Hunterd03f2172013-08-27 11:23:11 +03001640 if (type & PERF_SAMPLE_READ) {
1641 if (read_format & PERF_FORMAT_GROUP)
1642 *array = sample->read.group.nr;
1643 else
1644 *array = sample->read.one.value;
1645 array++;
1646
1647 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) {
1648 *array = sample->read.time_enabled;
1649 array++;
1650 }
1651
1652 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) {
1653 *array = sample->read.time_running;
1654 array++;
1655 }
1656
1657 /* PERF_FORMAT_ID is forced for PERF_SAMPLE_READ */
1658 if (read_format & PERF_FORMAT_GROUP) {
1659 sz = sample->read.group.nr *
1660 sizeof(struct sample_read_value);
1661 memcpy(array, sample->read.group.values, sz);
1662 array = (void *)array + sz;
1663 } else {
1664 *array = sample->read.one.id;
1665 array++;
1666 }
1667 }
1668
1669 if (type & PERF_SAMPLE_CALLCHAIN) {
1670 sz = (sample->callchain->nr + 1) * sizeof(u64);
1671 memcpy(array, sample->callchain, sz);
1672 array = (void *)array + sz;
1673 }
1674
1675 if (type & PERF_SAMPLE_RAW) {
1676 u.val32[0] = sample->raw_size;
1677 if (WARN_ONCE(swapped,
1678 "Endianness of raw data not corrected!\n")) {
1679 /*
1680 * Inverse of what is done in perf_evsel__parse_sample
1681 */
1682 u.val32[0] = bswap_32(u.val32[0]);
1683 u.val32[1] = bswap_32(u.val32[1]);
1684 u.val64 = bswap_64(u.val64);
1685 }
1686 *array = u.val64;
1687 array = (void *)array + sizeof(u32);
1688
1689 memcpy(array, sample->raw_data, sample->raw_size);
1690 array = (void *)array + sample->raw_size;
1691 }
1692
1693 if (type & PERF_SAMPLE_BRANCH_STACK) {
1694 sz = sample->branch_stack->nr * sizeof(struct branch_entry);
1695 sz += sizeof(u64);
1696 memcpy(array, sample->branch_stack, sz);
1697 array = (void *)array + sz;
1698 }
1699
1700 if (type & PERF_SAMPLE_REGS_USER) {
1701 if (sample->user_regs.abi) {
1702 *array++ = sample->user_regs.abi;
1703 sz = hweight_long(sample_regs_user) * sizeof(u64);
1704 memcpy(array, sample->user_regs.regs, sz);
1705 array = (void *)array + sz;
1706 } else {
1707 *array++ = 0;
1708 }
1709 }
1710
1711 if (type & PERF_SAMPLE_STACK_USER) {
1712 sz = sample->user_stack.size;
1713 *array++ = sz;
1714 if (sz) {
1715 memcpy(array, sample->user_stack.data, sz);
1716 array = (void *)array + sz;
1717 *array++ = sz;
1718 }
1719 }
1720
1721 if (type & PERF_SAMPLE_WEIGHT) {
1722 *array = sample->weight;
1723 array++;
1724 }
1725
1726 if (type & PERF_SAMPLE_DATA_SRC) {
1727 *array = sample->data_src;
1728 array++;
1729 }
1730
Andrew Vagin74eec262011-11-28 12:03:31 +03001731 return 0;
1732}
Arnaldo Carvalho de Melo5555ded2012-09-11 19:24:23 -03001733
Arnaldo Carvalho de Meloefd2b922012-09-18 11:21:50 -03001734struct format_field *perf_evsel__field(struct perf_evsel *evsel, const char *name)
1735{
1736 return pevent_find_field(evsel->tp_format, name);
1737}
1738
Arnaldo Carvalho de Melo5d2074e2012-09-26 20:22:00 -03001739void *perf_evsel__rawptr(struct perf_evsel *evsel, struct perf_sample *sample,
Arnaldo Carvalho de Melo5555ded2012-09-11 19:24:23 -03001740 const char *name)
1741{
Arnaldo Carvalho de Meloefd2b922012-09-18 11:21:50 -03001742 struct format_field *field = perf_evsel__field(evsel, name);
Arnaldo Carvalho de Melo5555ded2012-09-11 19:24:23 -03001743 int offset;
1744
Arnaldo Carvalho de Meloefd2b922012-09-18 11:21:50 -03001745 if (!field)
1746 return NULL;
Arnaldo Carvalho de Melo5555ded2012-09-11 19:24:23 -03001747
1748 offset = field->offset;
1749
1750 if (field->flags & FIELD_IS_DYNAMIC) {
1751 offset = *(int *)(sample->raw_data + field->offset);
1752 offset &= 0xffff;
1753 }
1754
1755 return sample->raw_data + offset;
1756}
1757
1758u64 perf_evsel__intval(struct perf_evsel *evsel, struct perf_sample *sample,
1759 const char *name)
1760{
Arnaldo Carvalho de Meloefd2b922012-09-18 11:21:50 -03001761 struct format_field *field = perf_evsel__field(evsel, name);
Arnaldo Carvalho de Meloe6b6f672012-09-26 13:13:04 -03001762 void *ptr;
1763 u64 value;
Arnaldo Carvalho de Melo5555ded2012-09-11 19:24:23 -03001764
Arnaldo Carvalho de Meloefd2b922012-09-18 11:21:50 -03001765 if (!field)
1766 return 0;
Arnaldo Carvalho de Melo5555ded2012-09-11 19:24:23 -03001767
Arnaldo Carvalho de Meloe6b6f672012-09-26 13:13:04 -03001768 ptr = sample->raw_data + field->offset;
Arnaldo Carvalho de Melo5555ded2012-09-11 19:24:23 -03001769
Arnaldo Carvalho de Meloe6b6f672012-09-26 13:13:04 -03001770 switch (field->size) {
1771 case 1:
1772 return *(u8 *)ptr;
1773 case 2:
1774 value = *(u16 *)ptr;
1775 break;
1776 case 4:
1777 value = *(u32 *)ptr;
1778 break;
1779 case 8:
1780 value = *(u64 *)ptr;
1781 break;
1782 default:
1783 return 0;
1784 }
1785
1786 if (!evsel->needs_swap)
1787 return value;
1788
1789 switch (field->size) {
1790 case 2:
1791 return bswap_16(value);
1792 case 4:
1793 return bswap_32(value);
1794 case 8:
1795 return bswap_64(value);
1796 default:
1797 return 0;
1798 }
1799
1800 return 0;
Arnaldo Carvalho de Melo5555ded2012-09-11 19:24:23 -03001801}
Arnaldo Carvalho de Melo0698aed2012-12-10 18:17:08 -03001802
1803static int comma_fprintf(FILE *fp, bool *first, const char *fmt, ...)
1804{
1805 va_list args;
1806 int ret = 0;
1807
1808 if (!*first) {
1809 ret += fprintf(fp, ",");
1810 } else {
1811 ret += fprintf(fp, ":");
1812 *first = false;
1813 }
1814
1815 va_start(args, fmt);
1816 ret += vfprintf(fp, fmt, args);
1817 va_end(args);
1818 return ret;
1819}
1820
1821static int __if_fprintf(FILE *fp, bool *first, const char *field, u64 value)
1822{
1823 if (value == 0)
1824 return 0;
1825
1826 return comma_fprintf(fp, first, " %s: %" PRIu64, field, value);
1827}
1828
1829#define if_print(field) printed += __if_fprintf(fp, &first, #field, evsel->attr.field)
1830
Arnaldo Carvalho de Meloc79a4392012-12-11 10:54:12 -03001831struct bit_names {
1832 int bit;
1833 const char *name;
1834};
1835
1836static int bits__fprintf(FILE *fp, const char *field, u64 value,
1837 struct bit_names *bits, bool *first)
1838{
1839 int i = 0, printed = comma_fprintf(fp, first, " %s: ", field);
1840 bool first_bit = true;
1841
1842 do {
1843 if (value & bits[i].bit) {
1844 printed += fprintf(fp, "%s%s", first_bit ? "" : "|", bits[i].name);
1845 first_bit = false;
1846 }
1847 } while (bits[++i].name != NULL);
1848
1849 return printed;
1850}
1851
1852static int sample_type__fprintf(FILE *fp, bool *first, u64 value)
1853{
1854#define bit_name(n) { PERF_SAMPLE_##n, #n }
1855 struct bit_names bits[] = {
1856 bit_name(IP), bit_name(TID), bit_name(TIME), bit_name(ADDR),
1857 bit_name(READ), bit_name(CALLCHAIN), bit_name(ID), bit_name(CPU),
1858 bit_name(PERIOD), bit_name(STREAM_ID), bit_name(RAW),
1859 bit_name(BRANCH_STACK), bit_name(REGS_USER), bit_name(STACK_USER),
Adrian Hunter75562572013-08-27 11:23:09 +03001860 bit_name(IDENTIFIER),
Arnaldo Carvalho de Meloc79a4392012-12-11 10:54:12 -03001861 { .name = NULL, }
1862 };
1863#undef bit_name
1864 return bits__fprintf(fp, "sample_type", value, bits, first);
1865}
1866
1867static int read_format__fprintf(FILE *fp, bool *first, u64 value)
1868{
1869#define bit_name(n) { PERF_FORMAT_##n, #n }
1870 struct bit_names bits[] = {
1871 bit_name(TOTAL_TIME_ENABLED), bit_name(TOTAL_TIME_RUNNING),
1872 bit_name(ID), bit_name(GROUP),
1873 { .name = NULL, }
1874 };
1875#undef bit_name
1876 return bits__fprintf(fp, "read_format", value, bits, first);
1877}
1878
Arnaldo Carvalho de Melo0698aed2012-12-10 18:17:08 -03001879int perf_evsel__fprintf(struct perf_evsel *evsel,
1880 struct perf_attr_details *details, FILE *fp)
1881{
1882 bool first = true;
Namhyung Kime6ab07d2013-01-22 18:09:47 +09001883 int printed = 0;
1884
Arnaldo Carvalho de Meloe35ef3552013-02-06 17:20:02 -03001885 if (details->event_group) {
Namhyung Kime6ab07d2013-01-22 18:09:47 +09001886 struct perf_evsel *pos;
1887
1888 if (!perf_evsel__is_group_leader(evsel))
1889 return 0;
1890
1891 if (evsel->nr_members > 1)
1892 printed += fprintf(fp, "%s{", evsel->group_name ?: "");
1893
1894 printed += fprintf(fp, "%s", perf_evsel__name(evsel));
1895 for_each_group_member(pos, evsel)
1896 printed += fprintf(fp, ",%s", perf_evsel__name(pos));
1897
1898 if (evsel->nr_members > 1)
1899 printed += fprintf(fp, "}");
1900 goto out;
1901 }
1902
1903 printed += fprintf(fp, "%s", perf_evsel__name(evsel));
Arnaldo Carvalho de Melo0698aed2012-12-10 18:17:08 -03001904
1905 if (details->verbose || details->freq) {
1906 printed += comma_fprintf(fp, &first, " sample_freq=%" PRIu64,
1907 (u64)evsel->attr.sample_freq);
1908 }
1909
1910 if (details->verbose) {
1911 if_print(type);
1912 if_print(config);
1913 if_print(config1);
1914 if_print(config2);
1915 if_print(size);
Arnaldo Carvalho de Meloc79a4392012-12-11 10:54:12 -03001916 printed += sample_type__fprintf(fp, &first, evsel->attr.sample_type);
1917 if (evsel->attr.read_format)
1918 printed += read_format__fprintf(fp, &first, evsel->attr.read_format);
Arnaldo Carvalho de Melo0698aed2012-12-10 18:17:08 -03001919 if_print(disabled);
1920 if_print(inherit);
1921 if_print(pinned);
1922 if_print(exclusive);
1923 if_print(exclude_user);
1924 if_print(exclude_kernel);
1925 if_print(exclude_hv);
1926 if_print(exclude_idle);
1927 if_print(mmap);
1928 if_print(comm);
1929 if_print(freq);
1930 if_print(inherit_stat);
1931 if_print(enable_on_exec);
1932 if_print(task);
1933 if_print(watermark);
1934 if_print(precise_ip);
1935 if_print(mmap_data);
1936 if_print(sample_id_all);
1937 if_print(exclude_host);
1938 if_print(exclude_guest);
1939 if_print(__reserved_1);
1940 if_print(wakeup_events);
1941 if_print(bp_type);
1942 if_print(branch_sample_type);
1943 }
Namhyung Kime6ab07d2013-01-22 18:09:47 +09001944out:
Arnaldo Carvalho de Melo0698aed2012-12-10 18:17:08 -03001945 fputc('\n', fp);
1946 return ++printed;
1947}
Arnaldo Carvalho de Meloc0a54342012-12-13 14:16:30 -03001948
1949bool perf_evsel__fallback(struct perf_evsel *evsel, int err,
1950 char *msg, size_t msgsize)
1951{
David Ahern2b821cc2013-07-18 17:27:59 -06001952 if ((err == ENOENT || err == ENXIO || err == ENODEV) &&
Arnaldo Carvalho de Meloc0a54342012-12-13 14:16:30 -03001953 evsel->attr.type == PERF_TYPE_HARDWARE &&
1954 evsel->attr.config == PERF_COUNT_HW_CPU_CYCLES) {
1955 /*
1956 * If it's cycles then fall back to hrtimer based
1957 * cpu-clock-tick sw counter, which is always available even if
1958 * no PMU support.
1959 *
1960 * PPC returns ENXIO until 2.6.37 (behavior changed with commit
1961 * b0a873e).
1962 */
1963 scnprintf(msg, msgsize, "%s",
1964"The cycles event is not supported, trying to fall back to cpu-clock-ticks");
1965
1966 evsel->attr.type = PERF_TYPE_SOFTWARE;
1967 evsel->attr.config = PERF_COUNT_SW_CPU_CLOCK;
1968
1969 free(evsel->name);
1970 evsel->name = NULL;
1971 return true;
1972 }
1973
1974 return false;
1975}
Arnaldo Carvalho de Melo56e52e82012-12-13 15:10:58 -03001976
1977int perf_evsel__open_strerror(struct perf_evsel *evsel,
1978 struct perf_target *target,
1979 int err, char *msg, size_t size)
1980{
1981 switch (err) {
1982 case EPERM:
1983 case EACCES:
David Ahernb69e63a2013-05-25 17:54:00 -06001984 return scnprintf(msg, size,
Arnaldo Carvalho de Melo56e52e82012-12-13 15:10:58 -03001985 "You may not have permission to collect %sstats.\n"
1986 "Consider tweaking /proc/sys/kernel/perf_event_paranoid:\n"
1987 " -1 - Not paranoid at all\n"
1988 " 0 - Disallow raw tracepoint access for unpriv\n"
1989 " 1 - Disallow cpu events for unpriv\n"
1990 " 2 - Disallow kernel profiling for unpriv",
1991 target->system_wide ? "system-wide " : "");
1992 case ENOENT:
1993 return scnprintf(msg, size, "The %s event is not supported.",
1994 perf_evsel__name(evsel));
1995 case EMFILE:
1996 return scnprintf(msg, size, "%s",
1997 "Too many events are opened.\n"
1998 "Try again after reducing the number of events.");
1999 case ENODEV:
2000 if (target->cpu_list)
2001 return scnprintf(msg, size, "%s",
2002 "No such device - did you specify an out-of-range profile CPU?\n");
2003 break;
2004 case EOPNOTSUPP:
2005 if (evsel->attr.precise_ip)
2006 return scnprintf(msg, size, "%s",
2007 "\'precise\' request may not be supported. Try removing 'p' modifier.");
2008#if defined(__i386__) || defined(__x86_64__)
2009 if (evsel->attr.type == PERF_TYPE_HARDWARE)
2010 return scnprintf(msg, size, "%s",
2011 "No hardware sampling interrupt available.\n"
2012 "No APIC? If so then you can boot the kernel with the \"lapic\" boot parameter to force-enable it.");
2013#endif
2014 break;
2015 default:
2016 break;
2017 }
2018
2019 return scnprintf(msg, size,
2020 "The sys_perf_event_open() syscall returned with %d (%s) for event (%s). \n"
2021 "/bin/dmesg may provide additional information.\n"
2022 "No CONFIG_PERF_EVENTS=y kernel support configured?\n",
2023 err, strerror(err), perf_evsel__name(evsel));
2024}