blob: bfebc1ea3c51a3fd78ad84f139a11fa32c87683e [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;
Stephane Eranian5c5e8542013-08-21 12:10:25 +020030 bool mmap2;
Arnaldo Carvalho de Melo594ac612012-12-13 13:13:07 -030031} perf_missing_features;
32
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -020033#define FD(e, x, y) (*(int *)xyarray__entry(e->fd, x, y))
34
Adrian Hunter75562572013-08-27 11:23:09 +030035int __perf_evsel__sample_size(u64 sample_type)
Arnaldo Carvalho de Meloc2a70652011-06-02 11:04:54 -030036{
37 u64 mask = sample_type & PERF_SAMPLE_MASK;
38 int size = 0;
39 int i;
40
41 for (i = 0; i < 64; i++) {
42 if (mask & (1ULL << i))
43 size++;
44 }
45
46 size *= sizeof(u64);
47
48 return size;
49}
50
Adrian Hunter75562572013-08-27 11:23:09 +030051/**
52 * __perf_evsel__calc_id_pos - calculate id_pos.
53 * @sample_type: sample type
54 *
55 * This function returns the position of the event id (PERF_SAMPLE_ID or
56 * PERF_SAMPLE_IDENTIFIER) in a sample event i.e. in the array of struct
57 * sample_event.
58 */
59static int __perf_evsel__calc_id_pos(u64 sample_type)
60{
61 int idx = 0;
62
63 if (sample_type & PERF_SAMPLE_IDENTIFIER)
64 return 0;
65
66 if (!(sample_type & PERF_SAMPLE_ID))
67 return -1;
68
69 if (sample_type & PERF_SAMPLE_IP)
70 idx += 1;
71
72 if (sample_type & PERF_SAMPLE_TID)
73 idx += 1;
74
75 if (sample_type & PERF_SAMPLE_TIME)
76 idx += 1;
77
78 if (sample_type & PERF_SAMPLE_ADDR)
79 idx += 1;
80
81 return idx;
82}
83
84/**
85 * __perf_evsel__calc_is_pos - calculate is_pos.
86 * @sample_type: sample type
87 *
88 * This function returns the position (counting backwards) of the event id
89 * (PERF_SAMPLE_ID or PERF_SAMPLE_IDENTIFIER) in a non-sample event i.e. if
90 * sample_id_all is used there is an id sample appended to non-sample events.
91 */
92static int __perf_evsel__calc_is_pos(u64 sample_type)
93{
94 int idx = 1;
95
96 if (sample_type & PERF_SAMPLE_IDENTIFIER)
97 return 1;
98
99 if (!(sample_type & PERF_SAMPLE_ID))
100 return -1;
101
102 if (sample_type & PERF_SAMPLE_CPU)
103 idx += 1;
104
105 if (sample_type & PERF_SAMPLE_STREAM_ID)
106 idx += 1;
107
108 return idx;
109}
110
111void perf_evsel__calc_id_pos(struct perf_evsel *evsel)
112{
113 evsel->id_pos = __perf_evsel__calc_id_pos(evsel->attr.sample_type);
114 evsel->is_pos = __perf_evsel__calc_is_pos(evsel->attr.sample_type);
115}
116
Jiri Olsa4bf9ce12012-03-22 14:37:26 +0100117void hists__init(struct hists *hists)
Arnaldo Carvalho de Melo0e2a5f12011-11-04 08:16:58 -0200118{
119 memset(hists, 0, sizeof(*hists));
120 hists->entries_in_array[0] = hists->entries_in_array[1] = RB_ROOT;
121 hists->entries_in = &hists->entries_in_array[0];
122 hists->entries_collapsed = RB_ROOT;
123 hists->entries = RB_ROOT;
124 pthread_mutex_init(&hists->lock, NULL);
125}
126
Arnaldo Carvalho de Melo7be5ebe2012-12-10 14:53:43 -0300127void __perf_evsel__set_sample_bit(struct perf_evsel *evsel,
128 enum perf_event_sample_format bit)
129{
130 if (!(evsel->attr.sample_type & bit)) {
131 evsel->attr.sample_type |= bit;
132 evsel->sample_size += sizeof(u64);
Adrian Hunter75562572013-08-27 11:23:09 +0300133 perf_evsel__calc_id_pos(evsel);
Arnaldo Carvalho de Melo7be5ebe2012-12-10 14:53:43 -0300134 }
135}
136
137void __perf_evsel__reset_sample_bit(struct perf_evsel *evsel,
138 enum perf_event_sample_format bit)
139{
140 if (evsel->attr.sample_type & bit) {
141 evsel->attr.sample_type &= ~bit;
142 evsel->sample_size -= sizeof(u64);
Adrian Hunter75562572013-08-27 11:23:09 +0300143 perf_evsel__calc_id_pos(evsel);
Arnaldo Carvalho de Melo7be5ebe2012-12-10 14:53:43 -0300144 }
145}
146
Adrian Hunter75562572013-08-27 11:23:09 +0300147void perf_evsel__set_sample_id(struct perf_evsel *evsel,
148 bool can_sample_identifier)
Arnaldo Carvalho de Melo7a5a5ca2012-12-10 15:21:30 -0300149{
Adrian Hunter75562572013-08-27 11:23:09 +0300150 if (can_sample_identifier) {
151 perf_evsel__reset_sample_bit(evsel, ID);
152 perf_evsel__set_sample_bit(evsel, IDENTIFIER);
153 } else {
154 perf_evsel__set_sample_bit(evsel, ID);
155 }
Arnaldo Carvalho de Melo7a5a5ca2012-12-10 15:21:30 -0300156 evsel->attr.read_format |= PERF_FORMAT_ID;
157}
158
Arnaldo Carvalho de Meloef1d1af2011-01-18 21:41:45 -0200159void perf_evsel__init(struct perf_evsel *evsel,
160 struct perf_event_attr *attr, int idx)
161{
162 evsel->idx = idx;
163 evsel->attr = *attr;
Namhyung Kim2cfda562012-11-29 15:38:29 +0900164 evsel->leader = evsel;
Arnaldo Carvalho de Meloef1d1af2011-01-18 21:41:45 -0200165 INIT_LIST_HEAD(&evsel->node);
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -0300166 hists__init(&evsel->hists);
Arnaldo Carvalho de Melobde09462012-08-01 18:53:11 -0300167 evsel->sample_size = __perf_evsel__sample_size(attr->sample_type);
Adrian Hunter75562572013-08-27 11:23:09 +0300168 perf_evsel__calc_id_pos(evsel);
Arnaldo Carvalho de Meloef1d1af2011-01-18 21:41:45 -0200169}
170
Lin Ming23a2f3a2011-01-07 11:11:09 +0800171struct perf_evsel *perf_evsel__new(struct perf_event_attr *attr, int idx)
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200172{
173 struct perf_evsel *evsel = zalloc(sizeof(*evsel));
174
Arnaldo Carvalho de Meloef1d1af2011-01-18 21:41:45 -0200175 if (evsel != NULL)
176 perf_evsel__init(evsel, attr, idx);
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200177
178 return evsel;
179}
180
Arnaldo Carvalho de Melo201b7332012-09-26 20:24:19 -0300181struct event_format *event_format__new(const char *sys, const char *name)
Arnaldo Carvalho de Meloefd2b922012-09-18 11:21:50 -0300182{
183 int fd, n;
184 char *filename;
185 void *bf = NULL, *nbf;
186 size_t size = 0, alloc_size = 0;
187 struct event_format *format = NULL;
188
189 if (asprintf(&filename, "%s/%s/%s/format", tracing_events_path, sys, name) < 0)
190 goto out;
191
192 fd = open(filename, O_RDONLY);
193 if (fd < 0)
194 goto out_free_filename;
195
196 do {
197 if (size == alloc_size) {
198 alloc_size += BUFSIZ;
199 nbf = realloc(bf, alloc_size);
200 if (nbf == NULL)
201 goto out_free_bf;
202 bf = nbf;
203 }
204
David Ahern7cab84e2013-07-02 13:27:20 -0600205 n = read(fd, bf + size, alloc_size - size);
Arnaldo Carvalho de Meloefd2b922012-09-18 11:21:50 -0300206 if (n < 0)
207 goto out_free_bf;
208 size += n;
209 } while (n > 0);
210
211 pevent_parse_format(&format, bf, size, sys);
212
213out_free_bf:
214 free(bf);
215 close(fd);
216out_free_filename:
217 free(filename);
218out:
219 return format;
220}
221
222struct perf_evsel *perf_evsel__newtp(const char *sys, const char *name, int idx)
223{
224 struct perf_evsel *evsel = zalloc(sizeof(*evsel));
225
226 if (evsel != NULL) {
227 struct perf_event_attr attr = {
Arnaldo Carvalho de Melo0b80f8b32012-09-26 12:28:26 -0300228 .type = PERF_TYPE_TRACEPOINT,
229 .sample_type = (PERF_SAMPLE_RAW | PERF_SAMPLE_TIME |
230 PERF_SAMPLE_CPU | PERF_SAMPLE_PERIOD),
Arnaldo Carvalho de Meloefd2b922012-09-18 11:21:50 -0300231 };
232
Arnaldo Carvalho de Meloe48ffe22012-09-26 17:11:38 -0300233 if (asprintf(&evsel->name, "%s:%s", sys, name) < 0)
234 goto out_free;
235
Arnaldo Carvalho de Meloefd2b922012-09-18 11:21:50 -0300236 evsel->tp_format = event_format__new(sys, name);
237 if (evsel->tp_format == NULL)
238 goto out_free;
239
Arnaldo Carvalho de Melo0b80f8b32012-09-26 12:28:26 -0300240 event_attr_init(&attr);
Arnaldo Carvalho de Meloefd2b922012-09-18 11:21:50 -0300241 attr.config = evsel->tp_format->id;
Arnaldo Carvalho de Melo0b80f8b32012-09-26 12:28:26 -0300242 attr.sample_period = 1;
Arnaldo Carvalho de Meloefd2b922012-09-18 11:21:50 -0300243 perf_evsel__init(evsel, &attr, idx);
Arnaldo Carvalho de Meloefd2b922012-09-18 11:21:50 -0300244 }
245
246 return evsel;
247
248out_free:
Arnaldo Carvalho de Meloe48ffe22012-09-26 17:11:38 -0300249 free(evsel->name);
Arnaldo Carvalho de Meloefd2b922012-09-18 11:21:50 -0300250 free(evsel);
251 return NULL;
252}
253
Arnaldo Carvalho de Melo8ad70132012-09-06 13:11:18 -0300254const char *perf_evsel__hw_names[PERF_COUNT_HW_MAX] = {
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300255 "cycles",
256 "instructions",
257 "cache-references",
258 "cache-misses",
259 "branches",
260 "branch-misses",
261 "bus-cycles",
262 "stalled-cycles-frontend",
263 "stalled-cycles-backend",
264 "ref-cycles",
265};
266
Arnaldo Carvalho de Melodd4f5222012-06-13 15:52:42 -0300267static const char *__perf_evsel__hw_name(u64 config)
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300268{
269 if (config < PERF_COUNT_HW_MAX && perf_evsel__hw_names[config])
270 return perf_evsel__hw_names[config];
271
272 return "unknown-hardware";
273}
274
Arnaldo Carvalho de Melo27f18612012-06-11 13:33:09 -0300275static int perf_evsel__add_modifiers(struct perf_evsel *evsel, char *bf, size_t size)
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300276{
Arnaldo Carvalho de Melo27f18612012-06-11 13:33:09 -0300277 int colon = 0, r = 0;
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300278 struct perf_event_attr *attr = &evsel->attr;
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300279 bool exclude_guest_default = false;
280
281#define MOD_PRINT(context, mod) do { \
282 if (!attr->exclude_##context) { \
Arnaldo Carvalho de Melo27f18612012-06-11 13:33:09 -0300283 if (!colon) colon = ++r; \
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300284 r += scnprintf(bf + r, size - r, "%c", mod); \
285 } } while(0)
286
287 if (attr->exclude_kernel || attr->exclude_user || attr->exclude_hv) {
288 MOD_PRINT(kernel, 'k');
289 MOD_PRINT(user, 'u');
290 MOD_PRINT(hv, 'h');
291 exclude_guest_default = true;
292 }
293
294 if (attr->precise_ip) {
295 if (!colon)
Arnaldo Carvalho de Melo27f18612012-06-11 13:33:09 -0300296 colon = ++r;
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300297 r += scnprintf(bf + r, size - r, "%.*s", attr->precise_ip, "ppp");
298 exclude_guest_default = true;
299 }
300
301 if (attr->exclude_host || attr->exclude_guest == exclude_guest_default) {
302 MOD_PRINT(host, 'H');
303 MOD_PRINT(guest, 'G');
304 }
305#undef MOD_PRINT
306 if (colon)
Arnaldo Carvalho de Melo27f18612012-06-11 13:33:09 -0300307 bf[colon - 1] = ':';
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300308 return r;
309}
310
Arnaldo Carvalho de Melo27f18612012-06-11 13:33:09 -0300311static int perf_evsel__hw_name(struct perf_evsel *evsel, char *bf, size_t size)
312{
313 int r = scnprintf(bf, size, "%s", __perf_evsel__hw_name(evsel->attr.config));
314 return r + perf_evsel__add_modifiers(evsel, bf + r, size - r);
315}
316
Arnaldo Carvalho de Melo8ad70132012-09-06 13:11:18 -0300317const char *perf_evsel__sw_names[PERF_COUNT_SW_MAX] = {
Arnaldo Carvalho de Melo335c2f52012-06-11 14:36:20 -0300318 "cpu-clock",
319 "task-clock",
320 "page-faults",
321 "context-switches",
Arnaldo Carvalho de Melo8ad70132012-09-06 13:11:18 -0300322 "cpu-migrations",
Arnaldo Carvalho de Melo335c2f52012-06-11 14:36:20 -0300323 "minor-faults",
324 "major-faults",
325 "alignment-faults",
326 "emulation-faults",
Adrian Hunterd22d1a22013-08-31 21:50:52 +0300327 "dummy",
Arnaldo Carvalho de Melo335c2f52012-06-11 14:36:20 -0300328};
329
Arnaldo Carvalho de Melodd4f5222012-06-13 15:52:42 -0300330static const char *__perf_evsel__sw_name(u64 config)
Arnaldo Carvalho de Melo335c2f52012-06-11 14:36:20 -0300331{
332 if (config < PERF_COUNT_SW_MAX && perf_evsel__sw_names[config])
333 return perf_evsel__sw_names[config];
334 return "unknown-software";
335}
336
337static int perf_evsel__sw_name(struct perf_evsel *evsel, char *bf, size_t size)
338{
339 int r = scnprintf(bf, size, "%s", __perf_evsel__sw_name(evsel->attr.config));
340 return r + perf_evsel__add_modifiers(evsel, bf + r, size - r);
341}
342
Jiri Olsa287e74a2012-06-28 23:18:49 +0200343static int __perf_evsel__bp_name(char *bf, size_t size, u64 addr, u64 type)
344{
345 int r;
346
347 r = scnprintf(bf, size, "mem:0x%" PRIx64 ":", addr);
348
349 if (type & HW_BREAKPOINT_R)
350 r += scnprintf(bf + r, size - r, "r");
351
352 if (type & HW_BREAKPOINT_W)
353 r += scnprintf(bf + r, size - r, "w");
354
355 if (type & HW_BREAKPOINT_X)
356 r += scnprintf(bf + r, size - r, "x");
357
358 return r;
359}
360
361static int perf_evsel__bp_name(struct perf_evsel *evsel, char *bf, size_t size)
362{
363 struct perf_event_attr *attr = &evsel->attr;
364 int r = __perf_evsel__bp_name(bf, size, attr->bp_addr, attr->bp_type);
365 return r + perf_evsel__add_modifiers(evsel, bf + r, size - r);
366}
367
Arnaldo Carvalho de Melo0b668bc2012-06-11 14:08:07 -0300368const char *perf_evsel__hw_cache[PERF_COUNT_HW_CACHE_MAX]
369 [PERF_EVSEL__MAX_ALIASES] = {
370 { "L1-dcache", "l1-d", "l1d", "L1-data", },
371 { "L1-icache", "l1-i", "l1i", "L1-instruction", },
372 { "LLC", "L2", },
373 { "dTLB", "d-tlb", "Data-TLB", },
374 { "iTLB", "i-tlb", "Instruction-TLB", },
375 { "branch", "branches", "bpu", "btb", "bpc", },
376 { "node", },
377};
378
379const char *perf_evsel__hw_cache_op[PERF_COUNT_HW_CACHE_OP_MAX]
380 [PERF_EVSEL__MAX_ALIASES] = {
381 { "load", "loads", "read", },
382 { "store", "stores", "write", },
383 { "prefetch", "prefetches", "speculative-read", "speculative-load", },
384};
385
386const char *perf_evsel__hw_cache_result[PERF_COUNT_HW_CACHE_RESULT_MAX]
387 [PERF_EVSEL__MAX_ALIASES] = {
388 { "refs", "Reference", "ops", "access", },
389 { "misses", "miss", },
390};
391
392#define C(x) PERF_COUNT_HW_CACHE_##x
393#define CACHE_READ (1 << C(OP_READ))
394#define CACHE_WRITE (1 << C(OP_WRITE))
395#define CACHE_PREFETCH (1 << C(OP_PREFETCH))
396#define COP(x) (1 << x)
397
398/*
399 * cache operartion stat
400 * L1I : Read and prefetch only
401 * ITLB and BPU : Read-only
402 */
403static unsigned long perf_evsel__hw_cache_stat[C(MAX)] = {
404 [C(L1D)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
405 [C(L1I)] = (CACHE_READ | CACHE_PREFETCH),
406 [C(LL)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
407 [C(DTLB)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
408 [C(ITLB)] = (CACHE_READ),
409 [C(BPU)] = (CACHE_READ),
410 [C(NODE)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
411};
412
413bool perf_evsel__is_cache_op_valid(u8 type, u8 op)
414{
415 if (perf_evsel__hw_cache_stat[type] & COP(op))
416 return true; /* valid */
417 else
418 return false; /* invalid */
419}
420
421int __perf_evsel__hw_cache_type_op_res_name(u8 type, u8 op, u8 result,
422 char *bf, size_t size)
423{
424 if (result) {
425 return scnprintf(bf, size, "%s-%s-%s", perf_evsel__hw_cache[type][0],
426 perf_evsel__hw_cache_op[op][0],
427 perf_evsel__hw_cache_result[result][0]);
428 }
429
430 return scnprintf(bf, size, "%s-%s", perf_evsel__hw_cache[type][0],
431 perf_evsel__hw_cache_op[op][1]);
432}
433
Arnaldo Carvalho de Melodd4f5222012-06-13 15:52:42 -0300434static int __perf_evsel__hw_cache_name(u64 config, char *bf, size_t size)
Arnaldo Carvalho de Melo0b668bc2012-06-11 14:08:07 -0300435{
436 u8 op, result, type = (config >> 0) & 0xff;
437 const char *err = "unknown-ext-hardware-cache-type";
438
439 if (type > PERF_COUNT_HW_CACHE_MAX)
440 goto out_err;
441
442 op = (config >> 8) & 0xff;
443 err = "unknown-ext-hardware-cache-op";
444 if (op > PERF_COUNT_HW_CACHE_OP_MAX)
445 goto out_err;
446
447 result = (config >> 16) & 0xff;
448 err = "unknown-ext-hardware-cache-result";
449 if (result > PERF_COUNT_HW_CACHE_RESULT_MAX)
450 goto out_err;
451
452 err = "invalid-cache";
453 if (!perf_evsel__is_cache_op_valid(type, op))
454 goto out_err;
455
456 return __perf_evsel__hw_cache_type_op_res_name(type, op, result, bf, size);
457out_err:
458 return scnprintf(bf, size, "%s", err);
459}
460
461static int perf_evsel__hw_cache_name(struct perf_evsel *evsel, char *bf, size_t size)
462{
463 int ret = __perf_evsel__hw_cache_name(evsel->attr.config, bf, size);
464 return ret + perf_evsel__add_modifiers(evsel, bf + ret, size - ret);
465}
466
Arnaldo Carvalho de Melo6eef3d92012-06-13 11:53:37 -0300467static int perf_evsel__raw_name(struct perf_evsel *evsel, char *bf, size_t size)
468{
469 int ret = scnprintf(bf, size, "raw 0x%" PRIx64, evsel->attr.config);
470 return ret + perf_evsel__add_modifiers(evsel, bf + ret, size - ret);
471}
472
Arnaldo Carvalho de Melo7289f832012-06-12 12:34:58 -0300473const char *perf_evsel__name(struct perf_evsel *evsel)
Arnaldo Carvalho de Meloa4460832012-06-12 10:29:12 -0300474{
Arnaldo Carvalho de Melo7289f832012-06-12 12:34:58 -0300475 char bf[128];
Arnaldo Carvalho de Meloa4460832012-06-12 10:29:12 -0300476
Arnaldo Carvalho de Melo7289f832012-06-12 12:34:58 -0300477 if (evsel->name)
478 return evsel->name;
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300479
480 switch (evsel->attr.type) {
481 case PERF_TYPE_RAW:
Arnaldo Carvalho de Melo6eef3d92012-06-13 11:53:37 -0300482 perf_evsel__raw_name(evsel, bf, sizeof(bf));
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300483 break;
484
485 case PERF_TYPE_HARDWARE:
Arnaldo Carvalho de Melo7289f832012-06-12 12:34:58 -0300486 perf_evsel__hw_name(evsel, bf, sizeof(bf));
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300487 break;
Arnaldo Carvalho de Melo0b668bc2012-06-11 14:08:07 -0300488
489 case PERF_TYPE_HW_CACHE:
Arnaldo Carvalho de Melo7289f832012-06-12 12:34:58 -0300490 perf_evsel__hw_cache_name(evsel, bf, sizeof(bf));
Arnaldo Carvalho de Melo0b668bc2012-06-11 14:08:07 -0300491 break;
492
Arnaldo Carvalho de Melo335c2f52012-06-11 14:36:20 -0300493 case PERF_TYPE_SOFTWARE:
Arnaldo Carvalho de Melo7289f832012-06-12 12:34:58 -0300494 perf_evsel__sw_name(evsel, bf, sizeof(bf));
Arnaldo Carvalho de Melo335c2f52012-06-11 14:36:20 -0300495 break;
496
Arnaldo Carvalho de Meloa4460832012-06-12 10:29:12 -0300497 case PERF_TYPE_TRACEPOINT:
Arnaldo Carvalho de Melo7289f832012-06-12 12:34:58 -0300498 scnprintf(bf, sizeof(bf), "%s", "unknown tracepoint");
Arnaldo Carvalho de Meloa4460832012-06-12 10:29:12 -0300499 break;
500
Jiri Olsa287e74a2012-06-28 23:18:49 +0200501 case PERF_TYPE_BREAKPOINT:
502 perf_evsel__bp_name(evsel, bf, sizeof(bf));
503 break;
504
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300505 default:
Robert Richterca1b1452012-08-16 21:10:18 +0200506 scnprintf(bf, sizeof(bf), "unknown attr type: %d",
507 evsel->attr.type);
Arnaldo Carvalho de Meloa4460832012-06-12 10:29:12 -0300508 break;
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300509 }
510
Arnaldo Carvalho de Melo7289f832012-06-12 12:34:58 -0300511 evsel->name = strdup(bf);
512
513 return evsel->name ?: "unknown";
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300514}
515
Namhyung Kim717e2632013-01-22 18:09:44 +0900516const char *perf_evsel__group_name(struct perf_evsel *evsel)
517{
518 return evsel->group_name ?: "anon group";
519}
520
521int perf_evsel__group_desc(struct perf_evsel *evsel, char *buf, size_t size)
522{
523 int ret;
524 struct perf_evsel *pos;
525 const char *group_name = perf_evsel__group_name(evsel);
526
527 ret = scnprintf(buf, size, "%s", group_name);
528
529 ret += scnprintf(buf + ret, size - ret, " { %s",
530 perf_evsel__name(evsel));
531
532 for_each_group_member(pos, evsel)
533 ret += scnprintf(buf + ret, size - ret, ", %s",
534 perf_evsel__name(pos));
535
536 ret += scnprintf(buf + ret, size - ret, " }");
537
538 return ret;
539}
540
Jiri Olsa774cb492012-11-12 18:34:01 +0100541/*
542 * The enable_on_exec/disabled value strategy:
543 *
544 * 1) For any type of traced program:
545 * - all independent events and group leaders are disabled
546 * - all group members are enabled
547 *
548 * Group members are ruled by group leaders. They need to
549 * be enabled, because the group scheduling relies on that.
550 *
551 * 2) For traced programs executed by perf:
552 * - all independent events and group leaders have
553 * enable_on_exec set
554 * - we don't specifically enable or disable any event during
555 * the record command
556 *
557 * Independent events and group leaders are initially disabled
558 * and get enabled by exec. Group members are ruled by group
559 * leaders as stated in 1).
560 *
561 * 3) For traced programs attached by perf (pid/tid):
562 * - we specifically enable or disable all events during
563 * the record command
564 *
565 * When attaching events to already running traced we
566 * enable/disable events specifically, as there's no
567 * initial traced exec call.
568 */
Jiri Olsacac21422012-11-12 18:34:00 +0100569void perf_evsel__config(struct perf_evsel *evsel,
570 struct perf_record_opts *opts)
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200571{
Jiri Olsa3c176312012-10-10 17:39:03 +0200572 struct perf_evsel *leader = evsel->leader;
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200573 struct perf_event_attr *attr = &evsel->attr;
574 int track = !evsel->idx; /* only the first counter needs these */
575
Arnaldo Carvalho de Melo594ac612012-12-13 13:13:07 -0300576 attr->sample_id_all = perf_missing_features.sample_id_all ? 0 : 1;
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200577 attr->inherit = !opts->no_inherit;
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200578
Arnaldo Carvalho de Melo7be5ebe2012-12-10 14:53:43 -0300579 perf_evsel__set_sample_bit(evsel, IP);
580 perf_evsel__set_sample_bit(evsel, TID);
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200581
Jiri Olsa3c176312012-10-10 17:39:03 +0200582 if (evsel->sample_read) {
583 perf_evsel__set_sample_bit(evsel, READ);
584
585 /*
586 * We need ID even in case of single event, because
587 * PERF_SAMPLE_READ process ID specific data.
588 */
Adrian Hunter75562572013-08-27 11:23:09 +0300589 perf_evsel__set_sample_id(evsel, false);
Jiri Olsa3c176312012-10-10 17:39:03 +0200590
591 /*
592 * Apply group format only if we belong to group
593 * with more than one members.
594 */
595 if (leader->nr_members > 1) {
596 attr->read_format |= PERF_FORMAT_GROUP;
597 attr->inherit = 0;
598 }
599 }
600
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200601 /*
602 * We default some events to a 1 default interval. But keep
603 * it a weak assumption overridable by the user.
604 */
605 if (!attr->sample_period || (opts->user_freq != UINT_MAX &&
606 opts->user_interval != ULLONG_MAX)) {
607 if (opts->freq) {
Arnaldo Carvalho de Melo7be5ebe2012-12-10 14:53:43 -0300608 perf_evsel__set_sample_bit(evsel, PERIOD);
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200609 attr->freq = 1;
610 attr->sample_freq = opts->freq;
611 } else {
612 attr->sample_period = opts->default_interval;
613 }
614 }
615
Jiri Olsa3c176312012-10-10 17:39:03 +0200616 /*
617 * Disable sampling for all group members other
618 * than leader in case leader 'leads' the sampling.
619 */
620 if ((leader != evsel) && leader->sample_read) {
621 attr->sample_freq = 0;
622 attr->sample_period = 0;
623 }
624
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200625 if (opts->no_samples)
626 attr->sample_freq = 0;
627
628 if (opts->inherit_stat)
629 attr->inherit_stat = 1;
630
631 if (opts->sample_address) {
Arnaldo Carvalho de Melo7be5ebe2012-12-10 14:53:43 -0300632 perf_evsel__set_sample_bit(evsel, ADDR);
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200633 attr->mmap_data = track;
634 }
635
Jiri Olsa26d33022012-08-07 15:20:47 +0200636 if (opts->call_graph) {
Arnaldo Carvalho de Melo7be5ebe2012-12-10 14:53:43 -0300637 perf_evsel__set_sample_bit(evsel, CALLCHAIN);
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200638
Jiri Olsa26d33022012-08-07 15:20:47 +0200639 if (opts->call_graph == CALLCHAIN_DWARF) {
Arnaldo Carvalho de Melo7be5ebe2012-12-10 14:53:43 -0300640 perf_evsel__set_sample_bit(evsel, REGS_USER);
641 perf_evsel__set_sample_bit(evsel, STACK_USER);
Jiri Olsa26d33022012-08-07 15:20:47 +0200642 attr->sample_regs_user = PERF_REGS_MASK;
643 attr->sample_stack_user = opts->stack_dump_size;
644 attr->exclude_callchain_user = 1;
645 }
646 }
647
Namhyung Kime40ee742012-05-21 10:42:07 +0900648 if (perf_target__has_cpu(&opts->target))
Arnaldo Carvalho de Melo7be5ebe2012-12-10 14:53:43 -0300649 perf_evsel__set_sample_bit(evsel, CPU);
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200650
Andrew Vagin3e76ac72011-12-20 17:32:45 +0300651 if (opts->period)
Arnaldo Carvalho de Melo7be5ebe2012-12-10 14:53:43 -0300652 perf_evsel__set_sample_bit(evsel, PERIOD);
Andrew Vagin3e76ac72011-12-20 17:32:45 +0300653
Arnaldo Carvalho de Melo594ac612012-12-13 13:13:07 -0300654 if (!perf_missing_features.sample_id_all &&
Namhyung Kimd67356e2012-05-07 14:09:03 +0900655 (opts->sample_time || !opts->no_inherit ||
Namhyung Kimaa22dd42012-05-16 18:45:47 +0900656 perf_target__has_cpu(&opts->target)))
Arnaldo Carvalho de Melo7be5ebe2012-12-10 14:53:43 -0300657 perf_evsel__set_sample_bit(evsel, TIME);
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200658
659 if (opts->raw_samples) {
Arnaldo Carvalho de Melo7be5ebe2012-12-10 14:53:43 -0300660 perf_evsel__set_sample_bit(evsel, TIME);
661 perf_evsel__set_sample_bit(evsel, RAW);
662 perf_evsel__set_sample_bit(evsel, CPU);
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200663 }
664
Stephane Eranianccf49bf2013-01-24 16:10:37 +0100665 if (opts->sample_address)
666 attr->sample_type |= PERF_SAMPLE_DATA_SRC;
667
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200668 if (opts->no_delay) {
669 attr->watermark = 0;
670 attr->wakeup_events = 1;
671 }
Roberto Agostino Vitillobdfebd82012-02-09 23:21:02 +0100672 if (opts->branch_stack) {
Arnaldo Carvalho de Melo7be5ebe2012-12-10 14:53:43 -0300673 perf_evsel__set_sample_bit(evsel, BRANCH_STACK);
Roberto Agostino Vitillobdfebd82012-02-09 23:21:02 +0100674 attr->branch_sample_type = opts->branch_stack;
675 }
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200676
Andi Kleen05484292013-01-24 16:10:29 +0100677 if (opts->sample_weight)
678 attr->sample_type |= PERF_SAMPLE_WEIGHT;
679
Stephane Eranian5c5e8542013-08-21 12:10:25 +0200680 attr->mmap = track;
681 attr->mmap2 = track && !perf_missing_features.mmap2;
682 attr->comm = track;
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200683
Andi Kleen475eeab2013-09-20 07:40:43 -0700684 if (opts->sample_transaction)
685 attr->sample_type |= PERF_SAMPLE_TRANSACTION;
686
Jiri Olsa774cb492012-11-12 18:34:01 +0100687 /*
688 * XXX see the function comment above
689 *
690 * Disabling only independent events or group leaders,
691 * keeping group members enabled.
692 */
Namhyung Kim823254e2012-11-29 15:38:30 +0900693 if (perf_evsel__is_group_leader(evsel))
Jiri Olsa774cb492012-11-12 18:34:01 +0100694 attr->disabled = 1;
695
696 /*
697 * Setting enable_on_exec for independent events and
698 * group leaders for traced executed by perf.
699 */
Namhyung Kim823254e2012-11-29 15:38:30 +0900700 if (perf_target__none(&opts->target) && perf_evsel__is_group_leader(evsel))
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200701 attr->enable_on_exec = 1;
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200702}
703
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200704int perf_evsel__alloc_fd(struct perf_evsel *evsel, int ncpus, int nthreads)
705{
David Ahern4af4c952011-05-27 09:58:34 -0600706 int cpu, thread;
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200707 evsel->fd = xyarray__new(ncpus, nthreads, sizeof(int));
David Ahern4af4c952011-05-27 09:58:34 -0600708
709 if (evsel->fd) {
710 for (cpu = 0; cpu < ncpus; cpu++) {
711 for (thread = 0; thread < nthreads; thread++) {
712 FD(evsel, cpu, thread) = -1;
713 }
714 }
715 }
716
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200717 return evsel->fd != NULL ? 0 : -ENOMEM;
718}
719
Andi Kleene2407be2013-08-02 17:41:10 -0700720static int perf_evsel__run_ioctl(struct perf_evsel *evsel, int ncpus, int nthreads,
721 int ioc, void *arg)
Arnaldo Carvalho de Melo745cefc2012-09-26 15:07:39 -0300722{
723 int cpu, thread;
724
725 for (cpu = 0; cpu < ncpus; cpu++) {
726 for (thread = 0; thread < nthreads; thread++) {
727 int fd = FD(evsel, cpu, thread),
Andi Kleene2407be2013-08-02 17:41:10 -0700728 err = ioctl(fd, ioc, arg);
Arnaldo Carvalho de Melo745cefc2012-09-26 15:07:39 -0300729
730 if (err)
731 return err;
732 }
733 }
734
735 return 0;
736}
737
Andi Kleene2407be2013-08-02 17:41:10 -0700738int perf_evsel__set_filter(struct perf_evsel *evsel, int ncpus, int nthreads,
739 const char *filter)
740{
741 return perf_evsel__run_ioctl(evsel, ncpus, nthreads,
742 PERF_EVENT_IOC_SET_FILTER,
743 (void *)filter);
744}
745
746int perf_evsel__enable(struct perf_evsel *evsel, int ncpus, int nthreads)
747{
748 return perf_evsel__run_ioctl(evsel, ncpus, nthreads,
749 PERF_EVENT_IOC_ENABLE,
750 0);
751}
752
Arnaldo Carvalho de Melo70db7532011-01-12 22:39:13 -0200753int perf_evsel__alloc_id(struct perf_evsel *evsel, int ncpus, int nthreads)
754{
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -0300755 evsel->sample_id = xyarray__new(ncpus, nthreads, sizeof(struct perf_sample_id));
756 if (evsel->sample_id == NULL)
757 return -ENOMEM;
758
759 evsel->id = zalloc(ncpus * nthreads * sizeof(u64));
760 if (evsel->id == NULL) {
761 xyarray__delete(evsel->sample_id);
762 evsel->sample_id = NULL;
763 return -ENOMEM;
764 }
765
766 return 0;
Arnaldo Carvalho de Melo70db7532011-01-12 22:39:13 -0200767}
768
Frederik Deweerdta7e191c2013-03-01 13:02:27 -0500769void perf_evsel__reset_counts(struct perf_evsel *evsel, int ncpus)
770{
771 memset(evsel->counts, 0, (sizeof(*evsel->counts) +
772 (ncpus * sizeof(struct perf_counts_values))));
773}
774
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200775int perf_evsel__alloc_counts(struct perf_evsel *evsel, int ncpus)
776{
777 evsel->counts = zalloc((sizeof(*evsel->counts) +
778 (ncpus * sizeof(struct perf_counts_values))));
779 return evsel->counts != NULL ? 0 : -ENOMEM;
780}
781
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200782void perf_evsel__free_fd(struct perf_evsel *evsel)
783{
784 xyarray__delete(evsel->fd);
785 evsel->fd = NULL;
786}
787
Arnaldo Carvalho de Melo70db7532011-01-12 22:39:13 -0200788void perf_evsel__free_id(struct perf_evsel *evsel)
789{
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -0300790 xyarray__delete(evsel->sample_id);
791 evsel->sample_id = NULL;
792 free(evsel->id);
Arnaldo Carvalho de Melo70db7532011-01-12 22:39:13 -0200793 evsel->id = NULL;
794}
795
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200796void perf_evsel__close_fd(struct perf_evsel *evsel, int ncpus, int nthreads)
797{
798 int cpu, thread;
799
800 for (cpu = 0; cpu < ncpus; cpu++)
801 for (thread = 0; thread < nthreads; ++thread) {
802 close(FD(evsel, cpu, thread));
803 FD(evsel, cpu, thread) = -1;
804 }
805}
806
Namhyung Kim43f8e762013-01-25 10:44:44 +0900807void perf_evsel__free_counts(struct perf_evsel *evsel)
808{
809 free(evsel->counts);
810}
811
Arnaldo Carvalho de Meloef1d1af2011-01-18 21:41:45 -0200812void perf_evsel__exit(struct perf_evsel *evsel)
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200813{
814 assert(list_empty(&evsel->node));
Namhyung Kim736b05a2013-03-15 14:48:49 +0900815 perf_evsel__free_fd(evsel);
816 perf_evsel__free_id(evsel);
Arnaldo Carvalho de Meloef1d1af2011-01-18 21:41:45 -0200817}
818
819void perf_evsel__delete(struct perf_evsel *evsel)
820{
821 perf_evsel__exit(evsel);
Stephane Eranian023695d2011-02-14 11:20:01 +0200822 close_cgroup(evsel->cgrp);
Jiri Olsa6a4bb042012-08-08 12:22:36 +0200823 free(evsel->group_name);
Arnaldo Carvalho de Meloe48ffe22012-09-26 17:11:38 -0300824 if (evsel->tp_format)
Arnaldo Carvalho de Meloefd2b922012-09-18 11:21:50 -0300825 pevent_free_format(evsel->tp_format);
Stephane Eranianf0c55bc2011-02-16 15:10:01 +0200826 free(evsel->name);
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200827 free(evsel);
828}
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200829
Stephane Eranianc7a79c42013-01-29 12:47:43 +0100830static inline void compute_deltas(struct perf_evsel *evsel,
831 int cpu,
832 struct perf_counts_values *count)
833{
834 struct perf_counts_values tmp;
835
836 if (!evsel->prev_raw_counts)
837 return;
838
839 if (cpu == -1) {
840 tmp = evsel->prev_raw_counts->aggr;
841 evsel->prev_raw_counts->aggr = *count;
842 } else {
843 tmp = evsel->prev_raw_counts->cpu[cpu];
844 evsel->prev_raw_counts->cpu[cpu] = *count;
845 }
846
847 count->val = count->val - tmp.val;
848 count->ena = count->ena - tmp.ena;
849 count->run = count->run - tmp.run;
850}
851
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200852int __perf_evsel__read_on_cpu(struct perf_evsel *evsel,
853 int cpu, int thread, bool scale)
854{
855 struct perf_counts_values count;
856 size_t nv = scale ? 3 : 1;
857
858 if (FD(evsel, cpu, thread) < 0)
859 return -EINVAL;
860
Arnaldo Carvalho de Melo4eed11d2011-01-04 00:13:17 -0200861 if (evsel->counts == NULL && perf_evsel__alloc_counts(evsel, cpu + 1) < 0)
862 return -ENOMEM;
863
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200864 if (readn(FD(evsel, cpu, thread), &count, nv * sizeof(u64)) < 0)
865 return -errno;
866
Stephane Eranianc7a79c42013-01-29 12:47:43 +0100867 compute_deltas(evsel, cpu, &count);
868
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200869 if (scale) {
870 if (count.run == 0)
871 count.val = 0;
872 else if (count.run < count.ena)
873 count.val = (u64)((double)count.val * count.ena / count.run + 0.5);
874 } else
875 count.ena = count.run = 0;
876
877 evsel->counts->cpu[cpu] = count;
878 return 0;
879}
880
881int __perf_evsel__read(struct perf_evsel *evsel,
882 int ncpus, int nthreads, bool scale)
883{
884 size_t nv = scale ? 3 : 1;
885 int cpu, thread;
886 struct perf_counts_values *aggr = &evsel->counts->aggr, count;
887
Arnaldo Carvalho de Melo52bcd9942011-02-03 17:26:06 -0200888 aggr->val = aggr->ena = aggr->run = 0;
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200889
890 for (cpu = 0; cpu < ncpus; cpu++) {
891 for (thread = 0; thread < nthreads; thread++) {
892 if (FD(evsel, cpu, thread) < 0)
893 continue;
894
895 if (readn(FD(evsel, cpu, thread),
896 &count, nv * sizeof(u64)) < 0)
897 return -errno;
898
899 aggr->val += count.val;
900 if (scale) {
901 aggr->ena += count.ena;
902 aggr->run += count.run;
903 }
904 }
905 }
906
Stephane Eranianc7a79c42013-01-29 12:47:43 +0100907 compute_deltas(evsel, -1, aggr);
908
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200909 evsel->counts->scaled = 0;
910 if (scale) {
911 if (aggr->run == 0) {
912 evsel->counts->scaled = -1;
913 aggr->val = 0;
914 return 0;
915 }
916
917 if (aggr->run < aggr->ena) {
918 evsel->counts->scaled = 1;
919 aggr->val = (u64)((double)aggr->val * aggr->ena / aggr->run + 0.5);
920 }
921 } else
922 aggr->ena = aggr->run = 0;
923
924 return 0;
925}
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -0200926
Jiri Olsa6a4bb042012-08-08 12:22:36 +0200927static int get_group_fd(struct perf_evsel *evsel, int cpu, int thread)
928{
929 struct perf_evsel *leader = evsel->leader;
930 int fd;
931
Namhyung Kim823254e2012-11-29 15:38:30 +0900932 if (perf_evsel__is_group_leader(evsel))
Jiri Olsa6a4bb042012-08-08 12:22:36 +0200933 return -1;
934
935 /*
936 * Leader must be already processed/open,
937 * if not it's a bug.
938 */
939 BUG_ON(!leader->fd);
940
941 fd = FD(leader, cpu, thread);
942 BUG_ON(fd == -1);
943
944 return fd;
945}
946
Adrian Huntere3e1a542013-08-14 15:48:24 +0300947#define __PRINT_ATTR(fmt, cast, field) \
948 fprintf(fp, " %-19s "fmt"\n", #field, cast attr->field)
949
950#define PRINT_ATTR_U32(field) __PRINT_ATTR("%u" , , field)
951#define PRINT_ATTR_X32(field) __PRINT_ATTR("%#x", , field)
952#define PRINT_ATTR_U64(field) __PRINT_ATTR("%" PRIu64, (uint64_t), field)
953#define PRINT_ATTR_X64(field) __PRINT_ATTR("%#"PRIx64, (uint64_t), field)
954
955#define PRINT_ATTR2N(name1, field1, name2, field2) \
956 fprintf(fp, " %-19s %u %-19s %u\n", \
957 name1, attr->field1, name2, attr->field2)
958
959#define PRINT_ATTR2(field1, field2) \
960 PRINT_ATTR2N(#field1, field1, #field2, field2)
961
962static size_t perf_event_attr__fprintf(struct perf_event_attr *attr, FILE *fp)
963{
964 size_t ret = 0;
965
966 ret += fprintf(fp, "%.60s\n", graph_dotted_line);
967 ret += fprintf(fp, "perf_event_attr:\n");
968
969 ret += PRINT_ATTR_U32(type);
970 ret += PRINT_ATTR_U32(size);
971 ret += PRINT_ATTR_X64(config);
972 ret += PRINT_ATTR_U64(sample_period);
973 ret += PRINT_ATTR_U64(sample_freq);
974 ret += PRINT_ATTR_X64(sample_type);
975 ret += PRINT_ATTR_X64(read_format);
976
977 ret += PRINT_ATTR2(disabled, inherit);
978 ret += PRINT_ATTR2(pinned, exclusive);
979 ret += PRINT_ATTR2(exclude_user, exclude_kernel);
980 ret += PRINT_ATTR2(exclude_hv, exclude_idle);
981 ret += PRINT_ATTR2(mmap, comm);
982 ret += PRINT_ATTR2(freq, inherit_stat);
983 ret += PRINT_ATTR2(enable_on_exec, task);
984 ret += PRINT_ATTR2(watermark, precise_ip);
985 ret += PRINT_ATTR2(mmap_data, sample_id_all);
986 ret += PRINT_ATTR2(exclude_host, exclude_guest);
987 ret += PRINT_ATTR2N("excl.callchain_kern", exclude_callchain_kernel,
988 "excl.callchain_user", exclude_callchain_user);
989
990 ret += PRINT_ATTR_U32(wakeup_events);
991 ret += PRINT_ATTR_U32(wakeup_watermark);
992 ret += PRINT_ATTR_X32(bp_type);
993 ret += PRINT_ATTR_X64(bp_addr);
994 ret += PRINT_ATTR_X64(config1);
995 ret += PRINT_ATTR_U64(bp_len);
996 ret += PRINT_ATTR_X64(config2);
997 ret += PRINT_ATTR_X64(branch_sample_type);
998 ret += PRINT_ATTR_X64(sample_regs_user);
999 ret += PRINT_ATTR_U32(sample_stack_user);
1000
1001 ret += fprintf(fp, "%.60s\n", graph_dotted_line);
1002
1003 return ret;
1004}
1005
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -02001006static int __perf_evsel__open(struct perf_evsel *evsel, struct cpu_map *cpus,
Jiri Olsa6a4bb042012-08-08 12:22:36 +02001007 struct thread_map *threads)
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -02001008{
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -02001009 int cpu, thread;
Stephane Eranian023695d2011-02-14 11:20:01 +02001010 unsigned long flags = 0;
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -02001011 int pid = -1, err;
Andi Kleenbec19672013-08-04 19:41:26 -07001012 enum { NO_CHANGE, SET_TO_MAX, INCREASED_MAX } set_rlimit = NO_CHANGE;
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -02001013
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -02001014 if (evsel->fd == NULL &&
1015 perf_evsel__alloc_fd(evsel, cpus->nr, threads->nr) < 0)
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -02001016 return -ENOMEM;
Arnaldo Carvalho de Melo4eed11d2011-01-04 00:13:17 -02001017
Stephane Eranian023695d2011-02-14 11:20:01 +02001018 if (evsel->cgrp) {
1019 flags = PERF_FLAG_PID_CGROUP;
1020 pid = evsel->cgrp->fd;
1021 }
1022
Arnaldo Carvalho de Melo594ac612012-12-13 13:13:07 -03001023fallback_missing_features:
Stephane Eranian5c5e8542013-08-21 12:10:25 +02001024 if (perf_missing_features.mmap2)
1025 evsel->attr.mmap2 = 0;
Arnaldo Carvalho de Melo594ac612012-12-13 13:13:07 -03001026 if (perf_missing_features.exclude_guest)
1027 evsel->attr.exclude_guest = evsel->attr.exclude_host = 0;
1028retry_sample_id:
1029 if (perf_missing_features.sample_id_all)
1030 evsel->attr.sample_id_all = 0;
1031
Adrian Huntere3e1a542013-08-14 15:48:24 +03001032 if (verbose >= 2)
1033 perf_event_attr__fprintf(&evsel->attr, stderr);
1034
Arnaldo Carvalho de Melo86bd5e82011-01-03 23:09:46 -02001035 for (cpu = 0; cpu < cpus->nr; cpu++) {
Arnaldo Carvalho de Melo9d04f172011-01-12 00:08:18 -02001036
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -02001037 for (thread = 0; thread < threads->nr; thread++) {
Jiri Olsa6a4bb042012-08-08 12:22:36 +02001038 int group_fd;
Stephane Eranian023695d2011-02-14 11:20:01 +02001039
1040 if (!evsel->cgrp)
1041 pid = threads->map[thread];
1042
Jiri Olsa6a4bb042012-08-08 12:22:36 +02001043 group_fd = get_group_fd(evsel, cpu, thread);
Andi Kleenbec19672013-08-04 19:41:26 -07001044retry_open:
Adrian Huntere3e1a542013-08-14 15:48:24 +03001045 pr_debug2("perf_event_open: pid %d cpu %d group_fd %d flags %#lx\n",
1046 pid, cpus->map[cpu], group_fd, flags);
1047
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -02001048 FD(evsel, cpu, thread) = sys_perf_event_open(&evsel->attr,
Stephane Eranian023695d2011-02-14 11:20:01 +02001049 pid,
Arnaldo Carvalho de Melof08199d2011-01-11 23:42:19 -02001050 cpus->map[cpu],
Stephane Eranian023695d2011-02-14 11:20:01 +02001051 group_fd, flags);
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -02001052 if (FD(evsel, cpu, thread) < 0) {
1053 err = -errno;
Arnaldo Carvalho de Melo594ac612012-12-13 13:13:07 -03001054 goto try_fallback;
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -02001055 }
Andi Kleenbec19672013-08-04 19:41:26 -07001056 set_rlimit = NO_CHANGE;
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -02001057 }
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -02001058 }
1059
1060 return 0;
1061
Arnaldo Carvalho de Melo594ac612012-12-13 13:13:07 -03001062try_fallback:
Andi Kleenbec19672013-08-04 19:41:26 -07001063 /*
1064 * perf stat needs between 5 and 22 fds per CPU. When we run out
1065 * of them try to increase the limits.
1066 */
1067 if (err == -EMFILE && set_rlimit < INCREASED_MAX) {
1068 struct rlimit l;
1069 int old_errno = errno;
1070
1071 if (getrlimit(RLIMIT_NOFILE, &l) == 0) {
1072 if (set_rlimit == NO_CHANGE)
1073 l.rlim_cur = l.rlim_max;
1074 else {
1075 l.rlim_cur = l.rlim_max + 1000;
1076 l.rlim_max = l.rlim_cur;
1077 }
1078 if (setrlimit(RLIMIT_NOFILE, &l) == 0) {
1079 set_rlimit++;
1080 errno = old_errno;
1081 goto retry_open;
1082 }
1083 }
1084 errno = old_errno;
1085 }
1086
Arnaldo Carvalho de Melo594ac612012-12-13 13:13:07 -03001087 if (err != -EINVAL || cpu > 0 || thread > 0)
1088 goto out_close;
1089
Stephane Eranian5c5e8542013-08-21 12:10:25 +02001090 if (!perf_missing_features.mmap2 && evsel->attr.mmap2) {
1091 perf_missing_features.mmap2 = true;
1092 goto fallback_missing_features;
1093 } else if (!perf_missing_features.exclude_guest &&
1094 (evsel->attr.exclude_guest || evsel->attr.exclude_host)) {
Arnaldo Carvalho de Melo594ac612012-12-13 13:13:07 -03001095 perf_missing_features.exclude_guest = true;
1096 goto fallback_missing_features;
1097 } else if (!perf_missing_features.sample_id_all) {
1098 perf_missing_features.sample_id_all = true;
1099 goto retry_sample_id;
1100 }
1101
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -02001102out_close:
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -02001103 do {
1104 while (--thread >= 0) {
1105 close(FD(evsel, cpu, thread));
1106 FD(evsel, cpu, thread) = -1;
1107 }
1108 thread = threads->nr;
1109 } while (--cpu >= 0);
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -02001110 return err;
1111}
1112
1113void perf_evsel__close(struct perf_evsel *evsel, int ncpus, int nthreads)
1114{
1115 if (evsel->fd == NULL)
1116 return;
1117
1118 perf_evsel__close_fd(evsel, ncpus, nthreads);
1119 perf_evsel__free_fd(evsel);
1120 evsel->fd = NULL;
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -02001121}
1122
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -02001123static struct {
1124 struct cpu_map map;
1125 int cpus[1];
1126} empty_cpu_map = {
1127 .map.nr = 1,
1128 .cpus = { -1, },
1129};
1130
1131static struct {
1132 struct thread_map map;
1133 int threads[1];
1134} empty_thread_map = {
1135 .map.nr = 1,
1136 .threads = { -1, },
1137};
1138
Arnaldo Carvalho de Melof08199d2011-01-11 23:42:19 -02001139int perf_evsel__open(struct perf_evsel *evsel, struct cpu_map *cpus,
Jiri Olsa6a4bb042012-08-08 12:22:36 +02001140 struct thread_map *threads)
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -02001141{
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -02001142 if (cpus == NULL) {
1143 /* Work around old compiler warnings about strict aliasing */
1144 cpus = &empty_cpu_map.map;
1145 }
1146
1147 if (threads == NULL)
1148 threads = &empty_thread_map.map;
1149
Jiri Olsa6a4bb042012-08-08 12:22:36 +02001150 return __perf_evsel__open(evsel, cpus, threads);
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -02001151}
1152
Arnaldo Carvalho de Melof08199d2011-01-11 23:42:19 -02001153int perf_evsel__open_per_cpu(struct perf_evsel *evsel,
Jiri Olsa6a4bb042012-08-08 12:22:36 +02001154 struct cpu_map *cpus)
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -02001155{
Jiri Olsa6a4bb042012-08-08 12:22:36 +02001156 return __perf_evsel__open(evsel, cpus, &empty_thread_map.map);
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -02001157}
1158
Arnaldo Carvalho de Melof08199d2011-01-11 23:42:19 -02001159int perf_evsel__open_per_thread(struct perf_evsel *evsel,
Jiri Olsa6a4bb042012-08-08 12:22:36 +02001160 struct thread_map *threads)
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -02001161{
Jiri Olsa6a4bb042012-08-08 12:22:36 +02001162 return __perf_evsel__open(evsel, &empty_cpu_map.map, threads);
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -02001163}
Arnaldo Carvalho de Melo70082dd2011-01-12 17:03:24 -02001164
Arnaldo Carvalho de Melo0807d2d2012-09-26 12:48:18 -03001165static int perf_evsel__parse_id_sample(const struct perf_evsel *evsel,
1166 const union perf_event *event,
1167 struct perf_sample *sample)
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001168{
Arnaldo Carvalho de Melo0807d2d2012-09-26 12:48:18 -03001169 u64 type = evsel->attr.sample_type;
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001170 const u64 *array = event->sample.array;
Arnaldo Carvalho de Melo0807d2d2012-09-26 12:48:18 -03001171 bool swapped = evsel->needs_swap;
Jiri Olsa37073f92012-05-30 14:23:44 +02001172 union u64_swap u;
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001173
1174 array += ((event->header.size -
1175 sizeof(event->header)) / sizeof(u64)) - 1;
1176
Adrian Hunter75562572013-08-27 11:23:09 +03001177 if (type & PERF_SAMPLE_IDENTIFIER) {
1178 sample->id = *array;
1179 array--;
1180 }
1181
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001182 if (type & PERF_SAMPLE_CPU) {
Jiri Olsa37073f92012-05-30 14:23:44 +02001183 u.val64 = *array;
1184 if (swapped) {
1185 /* undo swap of u64, then swap on individual u32s */
1186 u.val64 = bswap_64(u.val64);
1187 u.val32[0] = bswap_32(u.val32[0]);
1188 }
1189
1190 sample->cpu = u.val32[0];
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001191 array--;
1192 }
1193
1194 if (type & PERF_SAMPLE_STREAM_ID) {
1195 sample->stream_id = *array;
1196 array--;
1197 }
1198
1199 if (type & PERF_SAMPLE_ID) {
1200 sample->id = *array;
1201 array--;
1202 }
1203
1204 if (type & PERF_SAMPLE_TIME) {
1205 sample->time = *array;
1206 array--;
1207 }
1208
1209 if (type & PERF_SAMPLE_TID) {
Jiri Olsa37073f92012-05-30 14:23:44 +02001210 u.val64 = *array;
1211 if (swapped) {
1212 /* undo swap of u64, then swap on individual u32s */
1213 u.val64 = bswap_64(u.val64);
1214 u.val32[0] = bswap_32(u.val32[0]);
1215 u.val32[1] = bswap_32(u.val32[1]);
1216 }
1217
1218 sample->pid = u.val32[0];
1219 sample->tid = u.val32[1];
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001220 }
1221
1222 return 0;
1223}
1224
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001225static inline bool overflow(const void *endp, u16 max_size, const void *offset,
1226 u64 size)
Frederic Weisbecker98e1da92011-05-21 20:08:15 +02001227{
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001228 return size > max_size || offset + size > endp;
Frederic Weisbecker98e1da92011-05-21 20:08:15 +02001229}
1230
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001231#define OVERFLOW_CHECK(offset, size, max_size) \
1232 do { \
1233 if (overflow(endp, (max_size), (offset), (size))) \
1234 return -EFAULT; \
1235 } while (0)
1236
1237#define OVERFLOW_CHECK_u64(offset) \
1238 OVERFLOW_CHECK(offset, sizeof(u64), sizeof(u64))
1239
Arnaldo Carvalho de Meloa3f698f2012-08-02 12:23:46 -03001240int perf_evsel__parse_sample(struct perf_evsel *evsel, union perf_event *event,
Arnaldo Carvalho de Melo0807d2d2012-09-26 12:48:18 -03001241 struct perf_sample *data)
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001242{
Arnaldo Carvalho de Meloa3f698f2012-08-02 12:23:46 -03001243 u64 type = evsel->attr.sample_type;
Arnaldo Carvalho de Melo0807d2d2012-09-26 12:48:18 -03001244 bool swapped = evsel->needs_swap;
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001245 const u64 *array;
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001246 u16 max_size = event->header.size;
1247 const void *endp = (void *)event + max_size;
1248 u64 sz;
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001249
David Ahern936be502011-09-06 09:12:26 -06001250 /*
1251 * used for cross-endian analysis. See git commit 65014ab3
1252 * for why this goofiness is needed.
1253 */
Jiri Olsa6a11f922012-05-16 08:59:04 +02001254 union u64_swap u;
David Ahern936be502011-09-06 09:12:26 -06001255
Robert Richterf3bda2c2011-12-15 17:32:39 +01001256 memset(data, 0, sizeof(*data));
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001257 data->cpu = data->pid = data->tid = -1;
1258 data->stream_id = data->id = data->time = -1ULL;
Naveen N. Raoa4a03fc2012-02-03 22:31:13 +05301259 data->period = 1;
Andi Kleen05484292013-01-24 16:10:29 +01001260 data->weight = 0;
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001261
1262 if (event->header.type != PERF_RECORD_SAMPLE) {
Arnaldo Carvalho de Meloa3f698f2012-08-02 12:23:46 -03001263 if (!evsel->attr.sample_id_all)
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001264 return 0;
Arnaldo Carvalho de Melo0807d2d2012-09-26 12:48:18 -03001265 return perf_evsel__parse_id_sample(evsel, event, data);
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001266 }
1267
1268 array = event->sample.array;
1269
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001270 /*
1271 * The evsel's sample_size is based on PERF_SAMPLE_MASK which includes
1272 * up to PERF_SAMPLE_PERIOD. After that overflow() must be used to
1273 * check the format does not go past the end of the event.
1274 */
Arnaldo Carvalho de Meloa3f698f2012-08-02 12:23:46 -03001275 if (evsel->sample_size + sizeof(event->header) > event->header.size)
Frederic Weisbeckera2854122011-05-21 19:33:04 +02001276 return -EFAULT;
1277
Adrian Hunter75562572013-08-27 11:23:09 +03001278 data->id = -1ULL;
1279 if (type & PERF_SAMPLE_IDENTIFIER) {
1280 data->id = *array;
1281 array++;
1282 }
1283
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001284 if (type & PERF_SAMPLE_IP) {
Adrian Hunteref893252013-08-27 11:23:06 +03001285 data->ip = *array;
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001286 array++;
1287 }
1288
1289 if (type & PERF_SAMPLE_TID) {
David Ahern936be502011-09-06 09:12:26 -06001290 u.val64 = *array;
1291 if (swapped) {
1292 /* undo swap of u64, then swap on individual u32s */
1293 u.val64 = bswap_64(u.val64);
1294 u.val32[0] = bswap_32(u.val32[0]);
1295 u.val32[1] = bswap_32(u.val32[1]);
1296 }
1297
1298 data->pid = u.val32[0];
1299 data->tid = u.val32[1];
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001300 array++;
1301 }
1302
1303 if (type & PERF_SAMPLE_TIME) {
1304 data->time = *array;
1305 array++;
1306 }
1307
David Ahern7cec0922011-05-30 13:08:23 -06001308 data->addr = 0;
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001309 if (type & PERF_SAMPLE_ADDR) {
1310 data->addr = *array;
1311 array++;
1312 }
1313
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001314 if (type & PERF_SAMPLE_ID) {
1315 data->id = *array;
1316 array++;
1317 }
1318
1319 if (type & PERF_SAMPLE_STREAM_ID) {
1320 data->stream_id = *array;
1321 array++;
1322 }
1323
1324 if (type & PERF_SAMPLE_CPU) {
David Ahern936be502011-09-06 09:12:26 -06001325
1326 u.val64 = *array;
1327 if (swapped) {
1328 /* undo swap of u64, then swap on individual u32s */
1329 u.val64 = bswap_64(u.val64);
1330 u.val32[0] = bswap_32(u.val32[0]);
1331 }
1332
1333 data->cpu = u.val32[0];
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001334 array++;
1335 }
1336
1337 if (type & PERF_SAMPLE_PERIOD) {
1338 data->period = *array;
1339 array++;
1340 }
1341
1342 if (type & PERF_SAMPLE_READ) {
Jiri Olsa9ede4732012-10-10 17:38:13 +02001343 u64 read_format = evsel->attr.read_format;
1344
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001345 OVERFLOW_CHECK_u64(array);
Jiri Olsa9ede4732012-10-10 17:38:13 +02001346 if (read_format & PERF_FORMAT_GROUP)
1347 data->read.group.nr = *array;
1348 else
1349 data->read.one.value = *array;
1350
1351 array++;
1352
1353 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) {
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001354 OVERFLOW_CHECK_u64(array);
Jiri Olsa9ede4732012-10-10 17:38:13 +02001355 data->read.time_enabled = *array;
1356 array++;
1357 }
1358
1359 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) {
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001360 OVERFLOW_CHECK_u64(array);
Jiri Olsa9ede4732012-10-10 17:38:13 +02001361 data->read.time_running = *array;
1362 array++;
1363 }
1364
1365 /* PERF_FORMAT_ID is forced for PERF_SAMPLE_READ */
1366 if (read_format & PERF_FORMAT_GROUP) {
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001367 const u64 max_group_nr = UINT64_MAX /
1368 sizeof(struct sample_read_value);
1369
1370 if (data->read.group.nr > max_group_nr)
1371 return -EFAULT;
1372 sz = data->read.group.nr *
1373 sizeof(struct sample_read_value);
1374 OVERFLOW_CHECK(array, sz, max_size);
1375 data->read.group.values =
1376 (struct sample_read_value *)array;
1377 array = (void *)array + sz;
Jiri Olsa9ede4732012-10-10 17:38:13 +02001378 } else {
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001379 OVERFLOW_CHECK_u64(array);
Jiri Olsa9ede4732012-10-10 17:38:13 +02001380 data->read.one.id = *array;
1381 array++;
1382 }
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001383 }
1384
1385 if (type & PERF_SAMPLE_CALLCHAIN) {
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001386 const u64 max_callchain_nr = UINT64_MAX / sizeof(u64);
1387
1388 OVERFLOW_CHECK_u64(array);
1389 data->callchain = (struct ip_callchain *)array++;
1390 if (data->callchain->nr > max_callchain_nr)
Frederic Weisbecker98e1da92011-05-21 20:08:15 +02001391 return -EFAULT;
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001392 sz = data->callchain->nr * sizeof(u64);
1393 OVERFLOW_CHECK(array, sz, max_size);
1394 array = (void *)array + sz;
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001395 }
1396
1397 if (type & PERF_SAMPLE_RAW) {
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001398 OVERFLOW_CHECK_u64(array);
David Ahern936be502011-09-06 09:12:26 -06001399 u.val64 = *array;
1400 if (WARN_ONCE(swapped,
1401 "Endianness of raw data not corrected!\n")) {
1402 /* undo swap of u64, then swap on individual u32s */
1403 u.val64 = bswap_64(u.val64);
1404 u.val32[0] = bswap_32(u.val32[0]);
1405 u.val32[1] = bswap_32(u.val32[1]);
1406 }
David Ahern936be502011-09-06 09:12:26 -06001407 data->raw_size = u.val32[0];
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001408 array = (void *)array + sizeof(u32);
Frederic Weisbecker98e1da92011-05-21 20:08:15 +02001409
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001410 OVERFLOW_CHECK(array, data->raw_size, max_size);
1411 data->raw_data = (void *)array;
1412 array = (void *)array + data->raw_size;
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001413 }
1414
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +01001415 if (type & PERF_SAMPLE_BRANCH_STACK) {
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001416 const u64 max_branch_nr = UINT64_MAX /
1417 sizeof(struct branch_entry);
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +01001418
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001419 OVERFLOW_CHECK_u64(array);
1420 data->branch_stack = (struct branch_stack *)array++;
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +01001421
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001422 if (data->branch_stack->nr > max_branch_nr)
1423 return -EFAULT;
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +01001424 sz = data->branch_stack->nr * sizeof(struct branch_entry);
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001425 OVERFLOW_CHECK(array, sz, max_size);
1426 array = (void *)array + sz;
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +01001427 }
Jiri Olsa0f6a3012012-08-07 15:20:45 +02001428
1429 if (type & PERF_SAMPLE_REGS_USER) {
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001430 OVERFLOW_CHECK_u64(array);
Adrian Hunter5b95a4a32013-08-27 11:23:10 +03001431 data->user_regs.abi = *array;
1432 array++;
Jiri Olsa0f6a3012012-08-07 15:20:45 +02001433
Adrian Hunter5b95a4a32013-08-27 11:23:10 +03001434 if (data->user_regs.abi) {
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001435 u64 regs_user = evsel->attr.sample_regs_user;
1436
1437 sz = hweight_long(regs_user) * sizeof(u64);
1438 OVERFLOW_CHECK(array, sz, max_size);
Jiri Olsa0f6a3012012-08-07 15:20:45 +02001439 data->user_regs.regs = (u64 *)array;
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001440 array = (void *)array + sz;
Jiri Olsa0f6a3012012-08-07 15:20:45 +02001441 }
1442 }
1443
1444 if (type & PERF_SAMPLE_STACK_USER) {
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001445 OVERFLOW_CHECK_u64(array);
1446 sz = *array++;
Jiri Olsa0f6a3012012-08-07 15:20:45 +02001447
1448 data->user_stack.offset = ((char *)(array - 1)
1449 - (char *) event);
1450
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001451 if (!sz) {
Jiri Olsa0f6a3012012-08-07 15:20:45 +02001452 data->user_stack.size = 0;
1453 } else {
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001454 OVERFLOW_CHECK(array, sz, max_size);
Jiri Olsa0f6a3012012-08-07 15:20:45 +02001455 data->user_stack.data = (char *)array;
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001456 array = (void *)array + sz;
1457 OVERFLOW_CHECK_u64(array);
Adrian Hunter54bd2692013-07-04 16:20:34 +03001458 data->user_stack.size = *array++;
Jiri Olsaa65cb4b2013-10-02 15:46:39 +02001459 if (WARN_ONCE(data->user_stack.size > sz,
1460 "user stack dump failure\n"))
1461 return -EFAULT;
Jiri Olsa0f6a3012012-08-07 15:20:45 +02001462 }
1463 }
1464
Andi Kleen05484292013-01-24 16:10:29 +01001465 data->weight = 0;
1466 if (type & PERF_SAMPLE_WEIGHT) {
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001467 OVERFLOW_CHECK_u64(array);
Andi Kleen05484292013-01-24 16:10:29 +01001468 data->weight = *array;
1469 array++;
1470 }
1471
Stephane Eranian98a3b322013-01-24 16:10:35 +01001472 data->data_src = PERF_MEM_DATA_SRC_NONE;
1473 if (type & PERF_SAMPLE_DATA_SRC) {
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001474 OVERFLOW_CHECK_u64(array);
Stephane Eranian98a3b322013-01-24 16:10:35 +01001475 data->data_src = *array;
1476 array++;
1477 }
1478
Andi Kleen475eeab2013-09-20 07:40:43 -07001479 data->transaction = 0;
1480 if (type & PERF_SAMPLE_TRANSACTION) {
1481 data->transaction = *array;
1482 array++;
1483 }
1484
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001485 return 0;
1486}
Andrew Vagin74eec262011-11-28 12:03:31 +03001487
Adrian Hunterb1cf6f62013-08-27 11:23:12 +03001488size_t perf_event__sample_event_size(const struct perf_sample *sample, u64 type,
1489 u64 sample_regs_user, u64 read_format)
1490{
1491 size_t sz, result = sizeof(struct sample_event);
1492
1493 if (type & PERF_SAMPLE_IDENTIFIER)
1494 result += sizeof(u64);
1495
1496 if (type & PERF_SAMPLE_IP)
1497 result += sizeof(u64);
1498
1499 if (type & PERF_SAMPLE_TID)
1500 result += sizeof(u64);
1501
1502 if (type & PERF_SAMPLE_TIME)
1503 result += sizeof(u64);
1504
1505 if (type & PERF_SAMPLE_ADDR)
1506 result += sizeof(u64);
1507
1508 if (type & PERF_SAMPLE_ID)
1509 result += sizeof(u64);
1510
1511 if (type & PERF_SAMPLE_STREAM_ID)
1512 result += sizeof(u64);
1513
1514 if (type & PERF_SAMPLE_CPU)
1515 result += sizeof(u64);
1516
1517 if (type & PERF_SAMPLE_PERIOD)
1518 result += sizeof(u64);
1519
1520 if (type & PERF_SAMPLE_READ) {
1521 result += sizeof(u64);
1522 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
1523 result += sizeof(u64);
1524 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
1525 result += sizeof(u64);
1526 /* PERF_FORMAT_ID is forced for PERF_SAMPLE_READ */
1527 if (read_format & PERF_FORMAT_GROUP) {
1528 sz = sample->read.group.nr *
1529 sizeof(struct sample_read_value);
1530 result += sz;
1531 } else {
1532 result += sizeof(u64);
1533 }
1534 }
1535
1536 if (type & PERF_SAMPLE_CALLCHAIN) {
1537 sz = (sample->callchain->nr + 1) * sizeof(u64);
1538 result += sz;
1539 }
1540
1541 if (type & PERF_SAMPLE_RAW) {
1542 result += sizeof(u32);
1543 result += sample->raw_size;
1544 }
1545
1546 if (type & PERF_SAMPLE_BRANCH_STACK) {
1547 sz = sample->branch_stack->nr * sizeof(struct branch_entry);
1548 sz += sizeof(u64);
1549 result += sz;
1550 }
1551
1552 if (type & PERF_SAMPLE_REGS_USER) {
1553 if (sample->user_regs.abi) {
1554 result += sizeof(u64);
1555 sz = hweight_long(sample_regs_user) * sizeof(u64);
1556 result += sz;
1557 } else {
1558 result += sizeof(u64);
1559 }
1560 }
1561
1562 if (type & PERF_SAMPLE_STACK_USER) {
1563 sz = sample->user_stack.size;
1564 result += sizeof(u64);
1565 if (sz) {
1566 result += sz;
1567 result += sizeof(u64);
1568 }
1569 }
1570
1571 if (type & PERF_SAMPLE_WEIGHT)
1572 result += sizeof(u64);
1573
1574 if (type & PERF_SAMPLE_DATA_SRC)
1575 result += sizeof(u64);
1576
1577 return result;
1578}
1579
Andrew Vagin74eec262011-11-28 12:03:31 +03001580int perf_event__synthesize_sample(union perf_event *event, u64 type,
Adrian Hunterd03f2172013-08-27 11:23:11 +03001581 u64 sample_regs_user, u64 read_format,
Andrew Vagin74eec262011-11-28 12:03:31 +03001582 const struct perf_sample *sample,
1583 bool swapped)
1584{
1585 u64 *array;
Adrian Hunterd03f2172013-08-27 11:23:11 +03001586 size_t sz;
Andrew Vagin74eec262011-11-28 12:03:31 +03001587 /*
1588 * used for cross-endian analysis. See git commit 65014ab3
1589 * for why this goofiness is needed.
1590 */
Jiri Olsa6a11f922012-05-16 08:59:04 +02001591 union u64_swap u;
Andrew Vagin74eec262011-11-28 12:03:31 +03001592
1593 array = event->sample.array;
1594
Adrian Hunter75562572013-08-27 11:23:09 +03001595 if (type & PERF_SAMPLE_IDENTIFIER) {
1596 *array = sample->id;
1597 array++;
1598 }
1599
Andrew Vagin74eec262011-11-28 12:03:31 +03001600 if (type & PERF_SAMPLE_IP) {
Adrian Hunteref893252013-08-27 11:23:06 +03001601 *array = sample->ip;
Andrew Vagin74eec262011-11-28 12:03:31 +03001602 array++;
1603 }
1604
1605 if (type & PERF_SAMPLE_TID) {
1606 u.val32[0] = sample->pid;
1607 u.val32[1] = sample->tid;
1608 if (swapped) {
1609 /*
Arnaldo Carvalho de Meloa3f698f2012-08-02 12:23:46 -03001610 * Inverse of what is done in perf_evsel__parse_sample
Andrew Vagin74eec262011-11-28 12:03:31 +03001611 */
1612 u.val32[0] = bswap_32(u.val32[0]);
1613 u.val32[1] = bswap_32(u.val32[1]);
1614 u.val64 = bswap_64(u.val64);
1615 }
1616
1617 *array = u.val64;
1618 array++;
1619 }
1620
1621 if (type & PERF_SAMPLE_TIME) {
1622 *array = sample->time;
1623 array++;
1624 }
1625
1626 if (type & PERF_SAMPLE_ADDR) {
1627 *array = sample->addr;
1628 array++;
1629 }
1630
1631 if (type & PERF_SAMPLE_ID) {
1632 *array = sample->id;
1633 array++;
1634 }
1635
1636 if (type & PERF_SAMPLE_STREAM_ID) {
1637 *array = sample->stream_id;
1638 array++;
1639 }
1640
1641 if (type & PERF_SAMPLE_CPU) {
1642 u.val32[0] = sample->cpu;
1643 if (swapped) {
1644 /*
Arnaldo Carvalho de Meloa3f698f2012-08-02 12:23:46 -03001645 * Inverse of what is done in perf_evsel__parse_sample
Andrew Vagin74eec262011-11-28 12:03:31 +03001646 */
1647 u.val32[0] = bswap_32(u.val32[0]);
1648 u.val64 = bswap_64(u.val64);
1649 }
1650 *array = u.val64;
1651 array++;
1652 }
1653
1654 if (type & PERF_SAMPLE_PERIOD) {
1655 *array = sample->period;
1656 array++;
1657 }
1658
Adrian Hunterd03f2172013-08-27 11:23:11 +03001659 if (type & PERF_SAMPLE_READ) {
1660 if (read_format & PERF_FORMAT_GROUP)
1661 *array = sample->read.group.nr;
1662 else
1663 *array = sample->read.one.value;
1664 array++;
1665
1666 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) {
1667 *array = sample->read.time_enabled;
1668 array++;
1669 }
1670
1671 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) {
1672 *array = sample->read.time_running;
1673 array++;
1674 }
1675
1676 /* PERF_FORMAT_ID is forced for PERF_SAMPLE_READ */
1677 if (read_format & PERF_FORMAT_GROUP) {
1678 sz = sample->read.group.nr *
1679 sizeof(struct sample_read_value);
1680 memcpy(array, sample->read.group.values, sz);
1681 array = (void *)array + sz;
1682 } else {
1683 *array = sample->read.one.id;
1684 array++;
1685 }
1686 }
1687
1688 if (type & PERF_SAMPLE_CALLCHAIN) {
1689 sz = (sample->callchain->nr + 1) * sizeof(u64);
1690 memcpy(array, sample->callchain, sz);
1691 array = (void *)array + sz;
1692 }
1693
1694 if (type & PERF_SAMPLE_RAW) {
1695 u.val32[0] = sample->raw_size;
1696 if (WARN_ONCE(swapped,
1697 "Endianness of raw data not corrected!\n")) {
1698 /*
1699 * Inverse of what is done in perf_evsel__parse_sample
1700 */
1701 u.val32[0] = bswap_32(u.val32[0]);
1702 u.val32[1] = bswap_32(u.val32[1]);
1703 u.val64 = bswap_64(u.val64);
1704 }
1705 *array = u.val64;
1706 array = (void *)array + sizeof(u32);
1707
1708 memcpy(array, sample->raw_data, sample->raw_size);
1709 array = (void *)array + sample->raw_size;
1710 }
1711
1712 if (type & PERF_SAMPLE_BRANCH_STACK) {
1713 sz = sample->branch_stack->nr * sizeof(struct branch_entry);
1714 sz += sizeof(u64);
1715 memcpy(array, sample->branch_stack, sz);
1716 array = (void *)array + sz;
1717 }
1718
1719 if (type & PERF_SAMPLE_REGS_USER) {
1720 if (sample->user_regs.abi) {
1721 *array++ = sample->user_regs.abi;
1722 sz = hweight_long(sample_regs_user) * sizeof(u64);
1723 memcpy(array, sample->user_regs.regs, sz);
1724 array = (void *)array + sz;
1725 } else {
1726 *array++ = 0;
1727 }
1728 }
1729
1730 if (type & PERF_SAMPLE_STACK_USER) {
1731 sz = sample->user_stack.size;
1732 *array++ = sz;
1733 if (sz) {
1734 memcpy(array, sample->user_stack.data, sz);
1735 array = (void *)array + sz;
1736 *array++ = sz;
1737 }
1738 }
1739
1740 if (type & PERF_SAMPLE_WEIGHT) {
1741 *array = sample->weight;
1742 array++;
1743 }
1744
1745 if (type & PERF_SAMPLE_DATA_SRC) {
1746 *array = sample->data_src;
1747 array++;
1748 }
1749
Andrew Vagin74eec262011-11-28 12:03:31 +03001750 return 0;
1751}
Arnaldo Carvalho de Melo5555ded2012-09-11 19:24:23 -03001752
Arnaldo Carvalho de Meloefd2b922012-09-18 11:21:50 -03001753struct format_field *perf_evsel__field(struct perf_evsel *evsel, const char *name)
1754{
1755 return pevent_find_field(evsel->tp_format, name);
1756}
1757
Arnaldo Carvalho de Melo5d2074e2012-09-26 20:22:00 -03001758void *perf_evsel__rawptr(struct perf_evsel *evsel, struct perf_sample *sample,
Arnaldo Carvalho de Melo5555ded2012-09-11 19:24:23 -03001759 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 Melo5555ded2012-09-11 19:24:23 -03001762 int offset;
1763
Arnaldo Carvalho de Meloefd2b922012-09-18 11:21:50 -03001764 if (!field)
1765 return NULL;
Arnaldo Carvalho de Melo5555ded2012-09-11 19:24:23 -03001766
1767 offset = field->offset;
1768
1769 if (field->flags & FIELD_IS_DYNAMIC) {
1770 offset = *(int *)(sample->raw_data + field->offset);
1771 offset &= 0xffff;
1772 }
1773
1774 return sample->raw_data + offset;
1775}
1776
1777u64 perf_evsel__intval(struct perf_evsel *evsel, struct perf_sample *sample,
1778 const char *name)
1779{
Arnaldo Carvalho de Meloefd2b922012-09-18 11:21:50 -03001780 struct format_field *field = perf_evsel__field(evsel, name);
Arnaldo Carvalho de Meloe6b6f672012-09-26 13:13:04 -03001781 void *ptr;
1782 u64 value;
Arnaldo Carvalho de Melo5555ded2012-09-11 19:24:23 -03001783
Arnaldo Carvalho de Meloefd2b922012-09-18 11:21:50 -03001784 if (!field)
1785 return 0;
Arnaldo Carvalho de Melo5555ded2012-09-11 19:24:23 -03001786
Arnaldo Carvalho de Meloe6b6f672012-09-26 13:13:04 -03001787 ptr = sample->raw_data + field->offset;
Arnaldo Carvalho de Melo5555ded2012-09-11 19:24:23 -03001788
Arnaldo Carvalho de Meloe6b6f672012-09-26 13:13:04 -03001789 switch (field->size) {
1790 case 1:
1791 return *(u8 *)ptr;
1792 case 2:
1793 value = *(u16 *)ptr;
1794 break;
1795 case 4:
1796 value = *(u32 *)ptr;
1797 break;
1798 case 8:
1799 value = *(u64 *)ptr;
1800 break;
1801 default:
1802 return 0;
1803 }
1804
1805 if (!evsel->needs_swap)
1806 return value;
1807
1808 switch (field->size) {
1809 case 2:
1810 return bswap_16(value);
1811 case 4:
1812 return bswap_32(value);
1813 case 8:
1814 return bswap_64(value);
1815 default:
1816 return 0;
1817 }
1818
1819 return 0;
Arnaldo Carvalho de Melo5555ded2012-09-11 19:24:23 -03001820}
Arnaldo Carvalho de Melo0698aed2012-12-10 18:17:08 -03001821
1822static int comma_fprintf(FILE *fp, bool *first, const char *fmt, ...)
1823{
1824 va_list args;
1825 int ret = 0;
1826
1827 if (!*first) {
1828 ret += fprintf(fp, ",");
1829 } else {
1830 ret += fprintf(fp, ":");
1831 *first = false;
1832 }
1833
1834 va_start(args, fmt);
1835 ret += vfprintf(fp, fmt, args);
1836 va_end(args);
1837 return ret;
1838}
1839
1840static int __if_fprintf(FILE *fp, bool *first, const char *field, u64 value)
1841{
1842 if (value == 0)
1843 return 0;
1844
1845 return comma_fprintf(fp, first, " %s: %" PRIu64, field, value);
1846}
1847
1848#define if_print(field) printed += __if_fprintf(fp, &first, #field, evsel->attr.field)
1849
Arnaldo Carvalho de Meloc79a4392012-12-11 10:54:12 -03001850struct bit_names {
1851 int bit;
1852 const char *name;
1853};
1854
1855static int bits__fprintf(FILE *fp, const char *field, u64 value,
1856 struct bit_names *bits, bool *first)
1857{
1858 int i = 0, printed = comma_fprintf(fp, first, " %s: ", field);
1859 bool first_bit = true;
1860
1861 do {
1862 if (value & bits[i].bit) {
1863 printed += fprintf(fp, "%s%s", first_bit ? "" : "|", bits[i].name);
1864 first_bit = false;
1865 }
1866 } while (bits[++i].name != NULL);
1867
1868 return printed;
1869}
1870
1871static int sample_type__fprintf(FILE *fp, bool *first, u64 value)
1872{
1873#define bit_name(n) { PERF_SAMPLE_##n, #n }
1874 struct bit_names bits[] = {
1875 bit_name(IP), bit_name(TID), bit_name(TIME), bit_name(ADDR),
1876 bit_name(READ), bit_name(CALLCHAIN), bit_name(ID), bit_name(CPU),
1877 bit_name(PERIOD), bit_name(STREAM_ID), bit_name(RAW),
1878 bit_name(BRANCH_STACK), bit_name(REGS_USER), bit_name(STACK_USER),
Adrian Hunter75562572013-08-27 11:23:09 +03001879 bit_name(IDENTIFIER),
Arnaldo Carvalho de Meloc79a4392012-12-11 10:54:12 -03001880 { .name = NULL, }
1881 };
1882#undef bit_name
1883 return bits__fprintf(fp, "sample_type", value, bits, first);
1884}
1885
1886static int read_format__fprintf(FILE *fp, bool *first, u64 value)
1887{
1888#define bit_name(n) { PERF_FORMAT_##n, #n }
1889 struct bit_names bits[] = {
1890 bit_name(TOTAL_TIME_ENABLED), bit_name(TOTAL_TIME_RUNNING),
1891 bit_name(ID), bit_name(GROUP),
1892 { .name = NULL, }
1893 };
1894#undef bit_name
1895 return bits__fprintf(fp, "read_format", value, bits, first);
1896}
1897
Arnaldo Carvalho de Melo0698aed2012-12-10 18:17:08 -03001898int perf_evsel__fprintf(struct perf_evsel *evsel,
1899 struct perf_attr_details *details, FILE *fp)
1900{
1901 bool first = true;
Namhyung Kime6ab07d2013-01-22 18:09:47 +09001902 int printed = 0;
1903
Arnaldo Carvalho de Meloe35ef3552013-02-06 17:20:02 -03001904 if (details->event_group) {
Namhyung Kime6ab07d2013-01-22 18:09:47 +09001905 struct perf_evsel *pos;
1906
1907 if (!perf_evsel__is_group_leader(evsel))
1908 return 0;
1909
1910 if (evsel->nr_members > 1)
1911 printed += fprintf(fp, "%s{", evsel->group_name ?: "");
1912
1913 printed += fprintf(fp, "%s", perf_evsel__name(evsel));
1914 for_each_group_member(pos, evsel)
1915 printed += fprintf(fp, ",%s", perf_evsel__name(pos));
1916
1917 if (evsel->nr_members > 1)
1918 printed += fprintf(fp, "}");
1919 goto out;
1920 }
1921
1922 printed += fprintf(fp, "%s", perf_evsel__name(evsel));
Arnaldo Carvalho de Melo0698aed2012-12-10 18:17:08 -03001923
1924 if (details->verbose || details->freq) {
1925 printed += comma_fprintf(fp, &first, " sample_freq=%" PRIu64,
1926 (u64)evsel->attr.sample_freq);
1927 }
1928
1929 if (details->verbose) {
1930 if_print(type);
1931 if_print(config);
1932 if_print(config1);
1933 if_print(config2);
1934 if_print(size);
Arnaldo Carvalho de Meloc79a4392012-12-11 10:54:12 -03001935 printed += sample_type__fprintf(fp, &first, evsel->attr.sample_type);
1936 if (evsel->attr.read_format)
1937 printed += read_format__fprintf(fp, &first, evsel->attr.read_format);
Arnaldo Carvalho de Melo0698aed2012-12-10 18:17:08 -03001938 if_print(disabled);
1939 if_print(inherit);
1940 if_print(pinned);
1941 if_print(exclusive);
1942 if_print(exclude_user);
1943 if_print(exclude_kernel);
1944 if_print(exclude_hv);
1945 if_print(exclude_idle);
1946 if_print(mmap);
Stephane Eranian5c5e8542013-08-21 12:10:25 +02001947 if_print(mmap2);
Arnaldo Carvalho de Melo0698aed2012-12-10 18:17:08 -03001948 if_print(comm);
1949 if_print(freq);
1950 if_print(inherit_stat);
1951 if_print(enable_on_exec);
1952 if_print(task);
1953 if_print(watermark);
1954 if_print(precise_ip);
1955 if_print(mmap_data);
1956 if_print(sample_id_all);
1957 if_print(exclude_host);
1958 if_print(exclude_guest);
1959 if_print(__reserved_1);
1960 if_print(wakeup_events);
1961 if_print(bp_type);
1962 if_print(branch_sample_type);
1963 }
Namhyung Kime6ab07d2013-01-22 18:09:47 +09001964out:
Arnaldo Carvalho de Melo0698aed2012-12-10 18:17:08 -03001965 fputc('\n', fp);
1966 return ++printed;
1967}
Arnaldo Carvalho de Meloc0a54342012-12-13 14:16:30 -03001968
1969bool perf_evsel__fallback(struct perf_evsel *evsel, int err,
1970 char *msg, size_t msgsize)
1971{
David Ahern2b821cc2013-07-18 17:27:59 -06001972 if ((err == ENOENT || err == ENXIO || err == ENODEV) &&
Arnaldo Carvalho de Meloc0a54342012-12-13 14:16:30 -03001973 evsel->attr.type == PERF_TYPE_HARDWARE &&
1974 evsel->attr.config == PERF_COUNT_HW_CPU_CYCLES) {
1975 /*
1976 * If it's cycles then fall back to hrtimer based
1977 * cpu-clock-tick sw counter, which is always available even if
1978 * no PMU support.
1979 *
1980 * PPC returns ENXIO until 2.6.37 (behavior changed with commit
1981 * b0a873e).
1982 */
1983 scnprintf(msg, msgsize, "%s",
1984"The cycles event is not supported, trying to fall back to cpu-clock-ticks");
1985
1986 evsel->attr.type = PERF_TYPE_SOFTWARE;
1987 evsel->attr.config = PERF_COUNT_SW_CPU_CLOCK;
1988
1989 free(evsel->name);
1990 evsel->name = NULL;
1991 return true;
1992 }
1993
1994 return false;
1995}
Arnaldo Carvalho de Melo56e52e82012-12-13 15:10:58 -03001996
1997int perf_evsel__open_strerror(struct perf_evsel *evsel,
1998 struct perf_target *target,
1999 int err, char *msg, size_t size)
2000{
2001 switch (err) {
2002 case EPERM:
2003 case EACCES:
David Ahernb69e63a2013-05-25 17:54:00 -06002004 return scnprintf(msg, size,
Arnaldo Carvalho de Melo56e52e82012-12-13 15:10:58 -03002005 "You may not have permission to collect %sstats.\n"
2006 "Consider tweaking /proc/sys/kernel/perf_event_paranoid:\n"
2007 " -1 - Not paranoid at all\n"
2008 " 0 - Disallow raw tracepoint access for unpriv\n"
2009 " 1 - Disallow cpu events for unpriv\n"
2010 " 2 - Disallow kernel profiling for unpriv",
2011 target->system_wide ? "system-wide " : "");
2012 case ENOENT:
2013 return scnprintf(msg, size, "The %s event is not supported.",
2014 perf_evsel__name(evsel));
2015 case EMFILE:
2016 return scnprintf(msg, size, "%s",
2017 "Too many events are opened.\n"
2018 "Try again after reducing the number of events.");
2019 case ENODEV:
2020 if (target->cpu_list)
2021 return scnprintf(msg, size, "%s",
2022 "No such device - did you specify an out-of-range profile CPU?\n");
2023 break;
2024 case EOPNOTSUPP:
2025 if (evsel->attr.precise_ip)
2026 return scnprintf(msg, size, "%s",
2027 "\'precise\' request may not be supported. Try removing 'p' modifier.");
2028#if defined(__i386__) || defined(__x86_64__)
2029 if (evsel->attr.type == PERF_TYPE_HARDWARE)
2030 return scnprintf(msg, size, "%s",
2031 "No hardware sampling interrupt available.\n"
2032 "No APIC? If so then you can boot the kernel with the \"lapic\" boot parameter to force-enable it.");
2033#endif
2034 break;
2035 default:
2036 break;
2037 }
2038
2039 return scnprintf(msg, size,
2040 "The sys_perf_event_open() syscall returned with %d (%s) for event (%s). \n"
2041 "/bin/dmesg may provide additional information.\n"
2042 "No CONFIG_PERF_EVENTS=y kernel support configured?\n",
2043 err, strerror(err), perf_evsel__name(evsel));
2044}