blob: 11841881cef971312d9b5fce4418e66845934875 [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",
326};
327
Arnaldo Carvalho de Melodd4f5222012-06-13 15:52:42 -0300328static const char *__perf_evsel__sw_name(u64 config)
Arnaldo Carvalho de Melo335c2f52012-06-11 14:36:20 -0300329{
330 if (config < PERF_COUNT_SW_MAX && perf_evsel__sw_names[config])
331 return perf_evsel__sw_names[config];
332 return "unknown-software";
333}
334
335static int perf_evsel__sw_name(struct perf_evsel *evsel, char *bf, size_t size)
336{
337 int r = scnprintf(bf, size, "%s", __perf_evsel__sw_name(evsel->attr.config));
338 return r + perf_evsel__add_modifiers(evsel, bf + r, size - r);
339}
340
Jiri Olsa287e74a2012-06-28 23:18:49 +0200341static int __perf_evsel__bp_name(char *bf, size_t size, u64 addr, u64 type)
342{
343 int r;
344
345 r = scnprintf(bf, size, "mem:0x%" PRIx64 ":", addr);
346
347 if (type & HW_BREAKPOINT_R)
348 r += scnprintf(bf + r, size - r, "r");
349
350 if (type & HW_BREAKPOINT_W)
351 r += scnprintf(bf + r, size - r, "w");
352
353 if (type & HW_BREAKPOINT_X)
354 r += scnprintf(bf + r, size - r, "x");
355
356 return r;
357}
358
359static int perf_evsel__bp_name(struct perf_evsel *evsel, char *bf, size_t size)
360{
361 struct perf_event_attr *attr = &evsel->attr;
362 int r = __perf_evsel__bp_name(bf, size, attr->bp_addr, attr->bp_type);
363 return r + perf_evsel__add_modifiers(evsel, bf + r, size - r);
364}
365
Arnaldo Carvalho de Melo0b668bc2012-06-11 14:08:07 -0300366const char *perf_evsel__hw_cache[PERF_COUNT_HW_CACHE_MAX]
367 [PERF_EVSEL__MAX_ALIASES] = {
368 { "L1-dcache", "l1-d", "l1d", "L1-data", },
369 { "L1-icache", "l1-i", "l1i", "L1-instruction", },
370 { "LLC", "L2", },
371 { "dTLB", "d-tlb", "Data-TLB", },
372 { "iTLB", "i-tlb", "Instruction-TLB", },
373 { "branch", "branches", "bpu", "btb", "bpc", },
374 { "node", },
375};
376
377const char *perf_evsel__hw_cache_op[PERF_COUNT_HW_CACHE_OP_MAX]
378 [PERF_EVSEL__MAX_ALIASES] = {
379 { "load", "loads", "read", },
380 { "store", "stores", "write", },
381 { "prefetch", "prefetches", "speculative-read", "speculative-load", },
382};
383
384const char *perf_evsel__hw_cache_result[PERF_COUNT_HW_CACHE_RESULT_MAX]
385 [PERF_EVSEL__MAX_ALIASES] = {
386 { "refs", "Reference", "ops", "access", },
387 { "misses", "miss", },
388};
389
390#define C(x) PERF_COUNT_HW_CACHE_##x
391#define CACHE_READ (1 << C(OP_READ))
392#define CACHE_WRITE (1 << C(OP_WRITE))
393#define CACHE_PREFETCH (1 << C(OP_PREFETCH))
394#define COP(x) (1 << x)
395
396/*
397 * cache operartion stat
398 * L1I : Read and prefetch only
399 * ITLB and BPU : Read-only
400 */
401static unsigned long perf_evsel__hw_cache_stat[C(MAX)] = {
402 [C(L1D)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
403 [C(L1I)] = (CACHE_READ | CACHE_PREFETCH),
404 [C(LL)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
405 [C(DTLB)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
406 [C(ITLB)] = (CACHE_READ),
407 [C(BPU)] = (CACHE_READ),
408 [C(NODE)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
409};
410
411bool perf_evsel__is_cache_op_valid(u8 type, u8 op)
412{
413 if (perf_evsel__hw_cache_stat[type] & COP(op))
414 return true; /* valid */
415 else
416 return false; /* invalid */
417}
418
419int __perf_evsel__hw_cache_type_op_res_name(u8 type, u8 op, u8 result,
420 char *bf, size_t size)
421{
422 if (result) {
423 return scnprintf(bf, size, "%s-%s-%s", perf_evsel__hw_cache[type][0],
424 perf_evsel__hw_cache_op[op][0],
425 perf_evsel__hw_cache_result[result][0]);
426 }
427
428 return scnprintf(bf, size, "%s-%s", perf_evsel__hw_cache[type][0],
429 perf_evsel__hw_cache_op[op][1]);
430}
431
Arnaldo Carvalho de Melodd4f5222012-06-13 15:52:42 -0300432static int __perf_evsel__hw_cache_name(u64 config, char *bf, size_t size)
Arnaldo Carvalho de Melo0b668bc2012-06-11 14:08:07 -0300433{
434 u8 op, result, type = (config >> 0) & 0xff;
435 const char *err = "unknown-ext-hardware-cache-type";
436
437 if (type > PERF_COUNT_HW_CACHE_MAX)
438 goto out_err;
439
440 op = (config >> 8) & 0xff;
441 err = "unknown-ext-hardware-cache-op";
442 if (op > PERF_COUNT_HW_CACHE_OP_MAX)
443 goto out_err;
444
445 result = (config >> 16) & 0xff;
446 err = "unknown-ext-hardware-cache-result";
447 if (result > PERF_COUNT_HW_CACHE_RESULT_MAX)
448 goto out_err;
449
450 err = "invalid-cache";
451 if (!perf_evsel__is_cache_op_valid(type, op))
452 goto out_err;
453
454 return __perf_evsel__hw_cache_type_op_res_name(type, op, result, bf, size);
455out_err:
456 return scnprintf(bf, size, "%s", err);
457}
458
459static int perf_evsel__hw_cache_name(struct perf_evsel *evsel, char *bf, size_t size)
460{
461 int ret = __perf_evsel__hw_cache_name(evsel->attr.config, bf, size);
462 return ret + perf_evsel__add_modifiers(evsel, bf + ret, size - ret);
463}
464
Arnaldo Carvalho de Melo6eef3d92012-06-13 11:53:37 -0300465static int perf_evsel__raw_name(struct perf_evsel *evsel, char *bf, size_t size)
466{
467 int ret = scnprintf(bf, size, "raw 0x%" PRIx64, evsel->attr.config);
468 return ret + perf_evsel__add_modifiers(evsel, bf + ret, size - ret);
469}
470
Arnaldo Carvalho de Melo7289f832012-06-12 12:34:58 -0300471const char *perf_evsel__name(struct perf_evsel *evsel)
Arnaldo Carvalho de Meloa4460832012-06-12 10:29:12 -0300472{
Arnaldo Carvalho de Melo7289f832012-06-12 12:34:58 -0300473 char bf[128];
Arnaldo Carvalho de Meloa4460832012-06-12 10:29:12 -0300474
Arnaldo Carvalho de Melo7289f832012-06-12 12:34:58 -0300475 if (evsel->name)
476 return evsel->name;
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300477
478 switch (evsel->attr.type) {
479 case PERF_TYPE_RAW:
Arnaldo Carvalho de Melo6eef3d92012-06-13 11:53:37 -0300480 perf_evsel__raw_name(evsel, bf, sizeof(bf));
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300481 break;
482
483 case PERF_TYPE_HARDWARE:
Arnaldo Carvalho de Melo7289f832012-06-12 12:34:58 -0300484 perf_evsel__hw_name(evsel, bf, sizeof(bf));
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300485 break;
Arnaldo Carvalho de Melo0b668bc2012-06-11 14:08:07 -0300486
487 case PERF_TYPE_HW_CACHE:
Arnaldo Carvalho de Melo7289f832012-06-12 12:34:58 -0300488 perf_evsel__hw_cache_name(evsel, bf, sizeof(bf));
Arnaldo Carvalho de Melo0b668bc2012-06-11 14:08:07 -0300489 break;
490
Arnaldo Carvalho de Melo335c2f52012-06-11 14:36:20 -0300491 case PERF_TYPE_SOFTWARE:
Arnaldo Carvalho de Melo7289f832012-06-12 12:34:58 -0300492 perf_evsel__sw_name(evsel, bf, sizeof(bf));
Arnaldo Carvalho de Melo335c2f52012-06-11 14:36:20 -0300493 break;
494
Arnaldo Carvalho de Meloa4460832012-06-12 10:29:12 -0300495 case PERF_TYPE_TRACEPOINT:
Arnaldo Carvalho de Melo7289f832012-06-12 12:34:58 -0300496 scnprintf(bf, sizeof(bf), "%s", "unknown tracepoint");
Arnaldo Carvalho de Meloa4460832012-06-12 10:29:12 -0300497 break;
498
Jiri Olsa287e74a2012-06-28 23:18:49 +0200499 case PERF_TYPE_BREAKPOINT:
500 perf_evsel__bp_name(evsel, bf, sizeof(bf));
501 break;
502
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300503 default:
Robert Richterca1b1452012-08-16 21:10:18 +0200504 scnprintf(bf, sizeof(bf), "unknown attr type: %d",
505 evsel->attr.type);
Arnaldo Carvalho de Meloa4460832012-06-12 10:29:12 -0300506 break;
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300507 }
508
Arnaldo Carvalho de Melo7289f832012-06-12 12:34:58 -0300509 evsel->name = strdup(bf);
510
511 return evsel->name ?: "unknown";
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300512}
513
Namhyung Kim717e2632013-01-22 18:09:44 +0900514const char *perf_evsel__group_name(struct perf_evsel *evsel)
515{
516 return evsel->group_name ?: "anon group";
517}
518
519int perf_evsel__group_desc(struct perf_evsel *evsel, char *buf, size_t size)
520{
521 int ret;
522 struct perf_evsel *pos;
523 const char *group_name = perf_evsel__group_name(evsel);
524
525 ret = scnprintf(buf, size, "%s", group_name);
526
527 ret += scnprintf(buf + ret, size - ret, " { %s",
528 perf_evsel__name(evsel));
529
530 for_each_group_member(pos, evsel)
531 ret += scnprintf(buf + ret, size - ret, ", %s",
532 perf_evsel__name(pos));
533
534 ret += scnprintf(buf + ret, size - ret, " }");
535
536 return ret;
537}
538
Jiri Olsa774cb492012-11-12 18:34:01 +0100539/*
540 * The enable_on_exec/disabled value strategy:
541 *
542 * 1) For any type of traced program:
543 * - all independent events and group leaders are disabled
544 * - all group members are enabled
545 *
546 * Group members are ruled by group leaders. They need to
547 * be enabled, because the group scheduling relies on that.
548 *
549 * 2) For traced programs executed by perf:
550 * - all independent events and group leaders have
551 * enable_on_exec set
552 * - we don't specifically enable or disable any event during
553 * the record command
554 *
555 * Independent events and group leaders are initially disabled
556 * and get enabled by exec. Group members are ruled by group
557 * leaders as stated in 1).
558 *
559 * 3) For traced programs attached by perf (pid/tid):
560 * - we specifically enable or disable all events during
561 * the record command
562 *
563 * When attaching events to already running traced we
564 * enable/disable events specifically, as there's no
565 * initial traced exec call.
566 */
Jiri Olsacac21422012-11-12 18:34:00 +0100567void perf_evsel__config(struct perf_evsel *evsel,
568 struct perf_record_opts *opts)
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200569{
Jiri Olsa3c176312012-10-10 17:39:03 +0200570 struct perf_evsel *leader = evsel->leader;
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200571 struct perf_event_attr *attr = &evsel->attr;
572 int track = !evsel->idx; /* only the first counter needs these */
573
Arnaldo Carvalho de Melo594ac612012-12-13 13:13:07 -0300574 attr->sample_id_all = perf_missing_features.sample_id_all ? 0 : 1;
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200575 attr->inherit = !opts->no_inherit;
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200576
Arnaldo Carvalho de Melo7be5ebe2012-12-10 14:53:43 -0300577 perf_evsel__set_sample_bit(evsel, IP);
578 perf_evsel__set_sample_bit(evsel, TID);
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200579
Jiri Olsa3c176312012-10-10 17:39:03 +0200580 if (evsel->sample_read) {
581 perf_evsel__set_sample_bit(evsel, READ);
582
583 /*
584 * We need ID even in case of single event, because
585 * PERF_SAMPLE_READ process ID specific data.
586 */
Adrian Hunter75562572013-08-27 11:23:09 +0300587 perf_evsel__set_sample_id(evsel, false);
Jiri Olsa3c176312012-10-10 17:39:03 +0200588
589 /*
590 * Apply group format only if we belong to group
591 * with more than one members.
592 */
593 if (leader->nr_members > 1) {
594 attr->read_format |= PERF_FORMAT_GROUP;
595 attr->inherit = 0;
596 }
597 }
598
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200599 /*
600 * We default some events to a 1 default interval. But keep
601 * it a weak assumption overridable by the user.
602 */
603 if (!attr->sample_period || (opts->user_freq != UINT_MAX &&
604 opts->user_interval != ULLONG_MAX)) {
605 if (opts->freq) {
Arnaldo Carvalho de Melo7be5ebe2012-12-10 14:53:43 -0300606 perf_evsel__set_sample_bit(evsel, PERIOD);
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200607 attr->freq = 1;
608 attr->sample_freq = opts->freq;
609 } else {
610 attr->sample_period = opts->default_interval;
611 }
612 }
613
Jiri Olsa3c176312012-10-10 17:39:03 +0200614 /*
615 * Disable sampling for all group members other
616 * than leader in case leader 'leads' the sampling.
617 */
618 if ((leader != evsel) && leader->sample_read) {
619 attr->sample_freq = 0;
620 attr->sample_period = 0;
621 }
622
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200623 if (opts->no_samples)
624 attr->sample_freq = 0;
625
626 if (opts->inherit_stat)
627 attr->inherit_stat = 1;
628
629 if (opts->sample_address) {
Arnaldo Carvalho de Melo7be5ebe2012-12-10 14:53:43 -0300630 perf_evsel__set_sample_bit(evsel, ADDR);
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200631 attr->mmap_data = track;
632 }
633
Jiri Olsa26d33022012-08-07 15:20:47 +0200634 if (opts->call_graph) {
Arnaldo Carvalho de Melo7be5ebe2012-12-10 14:53:43 -0300635 perf_evsel__set_sample_bit(evsel, CALLCHAIN);
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200636
Jiri Olsa26d33022012-08-07 15:20:47 +0200637 if (opts->call_graph == CALLCHAIN_DWARF) {
Arnaldo Carvalho de Melo7be5ebe2012-12-10 14:53:43 -0300638 perf_evsel__set_sample_bit(evsel, REGS_USER);
639 perf_evsel__set_sample_bit(evsel, STACK_USER);
Jiri Olsa26d33022012-08-07 15:20:47 +0200640 attr->sample_regs_user = PERF_REGS_MASK;
641 attr->sample_stack_user = opts->stack_dump_size;
642 attr->exclude_callchain_user = 1;
643 }
644 }
645
Namhyung Kime40ee742012-05-21 10:42:07 +0900646 if (perf_target__has_cpu(&opts->target))
Arnaldo Carvalho de Melo7be5ebe2012-12-10 14:53:43 -0300647 perf_evsel__set_sample_bit(evsel, CPU);
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200648
Andrew Vagin3e76ac72011-12-20 17:32:45 +0300649 if (opts->period)
Arnaldo Carvalho de Melo7be5ebe2012-12-10 14:53:43 -0300650 perf_evsel__set_sample_bit(evsel, PERIOD);
Andrew Vagin3e76ac72011-12-20 17:32:45 +0300651
Arnaldo Carvalho de Melo594ac612012-12-13 13:13:07 -0300652 if (!perf_missing_features.sample_id_all &&
Namhyung Kimd67356e2012-05-07 14:09:03 +0900653 (opts->sample_time || !opts->no_inherit ||
Namhyung Kimaa22dd42012-05-16 18:45:47 +0900654 perf_target__has_cpu(&opts->target)))
Arnaldo Carvalho de Melo7be5ebe2012-12-10 14:53:43 -0300655 perf_evsel__set_sample_bit(evsel, TIME);
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200656
657 if (opts->raw_samples) {
Arnaldo Carvalho de Melo7be5ebe2012-12-10 14:53:43 -0300658 perf_evsel__set_sample_bit(evsel, TIME);
659 perf_evsel__set_sample_bit(evsel, RAW);
660 perf_evsel__set_sample_bit(evsel, CPU);
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200661 }
662
Stephane Eranianccf49bf2013-01-24 16:10:37 +0100663 if (opts->sample_address)
664 attr->sample_type |= PERF_SAMPLE_DATA_SRC;
665
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200666 if (opts->no_delay) {
667 attr->watermark = 0;
668 attr->wakeup_events = 1;
669 }
Roberto Agostino Vitillobdfebd82012-02-09 23:21:02 +0100670 if (opts->branch_stack) {
Arnaldo Carvalho de Melo7be5ebe2012-12-10 14:53:43 -0300671 perf_evsel__set_sample_bit(evsel, BRANCH_STACK);
Roberto Agostino Vitillobdfebd82012-02-09 23:21:02 +0100672 attr->branch_sample_type = opts->branch_stack;
673 }
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200674
Andi Kleen05484292013-01-24 16:10:29 +0100675 if (opts->sample_weight)
676 attr->sample_type |= PERF_SAMPLE_WEIGHT;
677
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200678 attr->mmap = track;
679 attr->comm = track;
680
Jiri Olsa774cb492012-11-12 18:34:01 +0100681 /*
682 * XXX see the function comment above
683 *
684 * Disabling only independent events or group leaders,
685 * keeping group members enabled.
686 */
Namhyung Kim823254e2012-11-29 15:38:30 +0900687 if (perf_evsel__is_group_leader(evsel))
Jiri Olsa774cb492012-11-12 18:34:01 +0100688 attr->disabled = 1;
689
690 /*
691 * Setting enable_on_exec for independent events and
692 * group leaders for traced executed by perf.
693 */
Namhyung Kim823254e2012-11-29 15:38:30 +0900694 if (perf_target__none(&opts->target) && perf_evsel__is_group_leader(evsel))
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200695 attr->enable_on_exec = 1;
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200696}
697
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200698int perf_evsel__alloc_fd(struct perf_evsel *evsel, int ncpus, int nthreads)
699{
David Ahern4af4c952011-05-27 09:58:34 -0600700 int cpu, thread;
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200701 evsel->fd = xyarray__new(ncpus, nthreads, sizeof(int));
David Ahern4af4c952011-05-27 09:58:34 -0600702
703 if (evsel->fd) {
704 for (cpu = 0; cpu < ncpus; cpu++) {
705 for (thread = 0; thread < nthreads; thread++) {
706 FD(evsel, cpu, thread) = -1;
707 }
708 }
709 }
710
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200711 return evsel->fd != NULL ? 0 : -ENOMEM;
712}
713
Andi Kleene2407be2013-08-02 17:41:10 -0700714static int perf_evsel__run_ioctl(struct perf_evsel *evsel, int ncpus, int nthreads,
715 int ioc, void *arg)
Arnaldo Carvalho de Melo745cefc2012-09-26 15:07:39 -0300716{
717 int cpu, thread;
718
719 for (cpu = 0; cpu < ncpus; cpu++) {
720 for (thread = 0; thread < nthreads; thread++) {
721 int fd = FD(evsel, cpu, thread),
Andi Kleene2407be2013-08-02 17:41:10 -0700722 err = ioctl(fd, ioc, arg);
Arnaldo Carvalho de Melo745cefc2012-09-26 15:07:39 -0300723
724 if (err)
725 return err;
726 }
727 }
728
729 return 0;
730}
731
Andi Kleene2407be2013-08-02 17:41:10 -0700732int perf_evsel__set_filter(struct perf_evsel *evsel, int ncpus, int nthreads,
733 const char *filter)
734{
735 return perf_evsel__run_ioctl(evsel, ncpus, nthreads,
736 PERF_EVENT_IOC_SET_FILTER,
737 (void *)filter);
738}
739
740int perf_evsel__enable(struct perf_evsel *evsel, int ncpus, int nthreads)
741{
742 return perf_evsel__run_ioctl(evsel, ncpus, nthreads,
743 PERF_EVENT_IOC_ENABLE,
744 0);
745}
746
Arnaldo Carvalho de Melo70db7532011-01-12 22:39:13 -0200747int perf_evsel__alloc_id(struct perf_evsel *evsel, int ncpus, int nthreads)
748{
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -0300749 evsel->sample_id = xyarray__new(ncpus, nthreads, sizeof(struct perf_sample_id));
750 if (evsel->sample_id == NULL)
751 return -ENOMEM;
752
753 evsel->id = zalloc(ncpus * nthreads * sizeof(u64));
754 if (evsel->id == NULL) {
755 xyarray__delete(evsel->sample_id);
756 evsel->sample_id = NULL;
757 return -ENOMEM;
758 }
759
760 return 0;
Arnaldo Carvalho de Melo70db7532011-01-12 22:39:13 -0200761}
762
Frederik Deweerdta7e191c2013-03-01 13:02:27 -0500763void perf_evsel__reset_counts(struct perf_evsel *evsel, int ncpus)
764{
765 memset(evsel->counts, 0, (sizeof(*evsel->counts) +
766 (ncpus * sizeof(struct perf_counts_values))));
767}
768
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200769int perf_evsel__alloc_counts(struct perf_evsel *evsel, int ncpus)
770{
771 evsel->counts = zalloc((sizeof(*evsel->counts) +
772 (ncpus * sizeof(struct perf_counts_values))));
773 return evsel->counts != NULL ? 0 : -ENOMEM;
774}
775
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200776void perf_evsel__free_fd(struct perf_evsel *evsel)
777{
778 xyarray__delete(evsel->fd);
779 evsel->fd = NULL;
780}
781
Arnaldo Carvalho de Melo70db7532011-01-12 22:39:13 -0200782void perf_evsel__free_id(struct perf_evsel *evsel)
783{
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -0300784 xyarray__delete(evsel->sample_id);
785 evsel->sample_id = NULL;
786 free(evsel->id);
Arnaldo Carvalho de Melo70db7532011-01-12 22:39:13 -0200787 evsel->id = NULL;
788}
789
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200790void perf_evsel__close_fd(struct perf_evsel *evsel, int ncpus, int nthreads)
791{
792 int cpu, thread;
793
794 for (cpu = 0; cpu < ncpus; cpu++)
795 for (thread = 0; thread < nthreads; ++thread) {
796 close(FD(evsel, cpu, thread));
797 FD(evsel, cpu, thread) = -1;
798 }
799}
800
Namhyung Kim43f8e762013-01-25 10:44:44 +0900801void perf_evsel__free_counts(struct perf_evsel *evsel)
802{
803 free(evsel->counts);
804}
805
Arnaldo Carvalho de Meloef1d1af2011-01-18 21:41:45 -0200806void perf_evsel__exit(struct perf_evsel *evsel)
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200807{
808 assert(list_empty(&evsel->node));
Namhyung Kim736b05a2013-03-15 14:48:49 +0900809 perf_evsel__free_fd(evsel);
810 perf_evsel__free_id(evsel);
Arnaldo Carvalho de Meloef1d1af2011-01-18 21:41:45 -0200811}
812
813void perf_evsel__delete(struct perf_evsel *evsel)
814{
815 perf_evsel__exit(evsel);
Stephane Eranian023695d2011-02-14 11:20:01 +0200816 close_cgroup(evsel->cgrp);
Jiri Olsa6a4bb042012-08-08 12:22:36 +0200817 free(evsel->group_name);
Arnaldo Carvalho de Meloe48ffe22012-09-26 17:11:38 -0300818 if (evsel->tp_format)
Arnaldo Carvalho de Meloefd2b922012-09-18 11:21:50 -0300819 pevent_free_format(evsel->tp_format);
Stephane Eranianf0c55bc2011-02-16 15:10:01 +0200820 free(evsel->name);
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200821 free(evsel);
822}
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200823
Stephane Eranianc7a79c42013-01-29 12:47:43 +0100824static inline void compute_deltas(struct perf_evsel *evsel,
825 int cpu,
826 struct perf_counts_values *count)
827{
828 struct perf_counts_values tmp;
829
830 if (!evsel->prev_raw_counts)
831 return;
832
833 if (cpu == -1) {
834 tmp = evsel->prev_raw_counts->aggr;
835 evsel->prev_raw_counts->aggr = *count;
836 } else {
837 tmp = evsel->prev_raw_counts->cpu[cpu];
838 evsel->prev_raw_counts->cpu[cpu] = *count;
839 }
840
841 count->val = count->val - tmp.val;
842 count->ena = count->ena - tmp.ena;
843 count->run = count->run - tmp.run;
844}
845
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200846int __perf_evsel__read_on_cpu(struct perf_evsel *evsel,
847 int cpu, int thread, bool scale)
848{
849 struct perf_counts_values count;
850 size_t nv = scale ? 3 : 1;
851
852 if (FD(evsel, cpu, thread) < 0)
853 return -EINVAL;
854
Arnaldo Carvalho de Melo4eed11d2011-01-04 00:13:17 -0200855 if (evsel->counts == NULL && perf_evsel__alloc_counts(evsel, cpu + 1) < 0)
856 return -ENOMEM;
857
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200858 if (readn(FD(evsel, cpu, thread), &count, nv * sizeof(u64)) < 0)
859 return -errno;
860
Stephane Eranianc7a79c42013-01-29 12:47:43 +0100861 compute_deltas(evsel, cpu, &count);
862
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200863 if (scale) {
864 if (count.run == 0)
865 count.val = 0;
866 else if (count.run < count.ena)
867 count.val = (u64)((double)count.val * count.ena / count.run + 0.5);
868 } else
869 count.ena = count.run = 0;
870
871 evsel->counts->cpu[cpu] = count;
872 return 0;
873}
874
875int __perf_evsel__read(struct perf_evsel *evsel,
876 int ncpus, int nthreads, bool scale)
877{
878 size_t nv = scale ? 3 : 1;
879 int cpu, thread;
880 struct perf_counts_values *aggr = &evsel->counts->aggr, count;
881
Arnaldo Carvalho de Melo52bcd9942011-02-03 17:26:06 -0200882 aggr->val = aggr->ena = aggr->run = 0;
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200883
884 for (cpu = 0; cpu < ncpus; cpu++) {
885 for (thread = 0; thread < nthreads; thread++) {
886 if (FD(evsel, cpu, thread) < 0)
887 continue;
888
889 if (readn(FD(evsel, cpu, thread),
890 &count, nv * sizeof(u64)) < 0)
891 return -errno;
892
893 aggr->val += count.val;
894 if (scale) {
895 aggr->ena += count.ena;
896 aggr->run += count.run;
897 }
898 }
899 }
900
Stephane Eranianc7a79c42013-01-29 12:47:43 +0100901 compute_deltas(evsel, -1, aggr);
902
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200903 evsel->counts->scaled = 0;
904 if (scale) {
905 if (aggr->run == 0) {
906 evsel->counts->scaled = -1;
907 aggr->val = 0;
908 return 0;
909 }
910
911 if (aggr->run < aggr->ena) {
912 evsel->counts->scaled = 1;
913 aggr->val = (u64)((double)aggr->val * aggr->ena / aggr->run + 0.5);
914 }
915 } else
916 aggr->ena = aggr->run = 0;
917
918 return 0;
919}
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -0200920
Jiri Olsa6a4bb042012-08-08 12:22:36 +0200921static int get_group_fd(struct perf_evsel *evsel, int cpu, int thread)
922{
923 struct perf_evsel *leader = evsel->leader;
924 int fd;
925
Namhyung Kim823254e2012-11-29 15:38:30 +0900926 if (perf_evsel__is_group_leader(evsel))
Jiri Olsa6a4bb042012-08-08 12:22:36 +0200927 return -1;
928
929 /*
930 * Leader must be already processed/open,
931 * if not it's a bug.
932 */
933 BUG_ON(!leader->fd);
934
935 fd = FD(leader, cpu, thread);
936 BUG_ON(fd == -1);
937
938 return fd;
939}
940
Adrian Huntere3e1a542013-08-14 15:48:24 +0300941#define __PRINT_ATTR(fmt, cast, field) \
942 fprintf(fp, " %-19s "fmt"\n", #field, cast attr->field)
943
944#define PRINT_ATTR_U32(field) __PRINT_ATTR("%u" , , field)
945#define PRINT_ATTR_X32(field) __PRINT_ATTR("%#x", , field)
946#define PRINT_ATTR_U64(field) __PRINT_ATTR("%" PRIu64, (uint64_t), field)
947#define PRINT_ATTR_X64(field) __PRINT_ATTR("%#"PRIx64, (uint64_t), field)
948
949#define PRINT_ATTR2N(name1, field1, name2, field2) \
950 fprintf(fp, " %-19s %u %-19s %u\n", \
951 name1, attr->field1, name2, attr->field2)
952
953#define PRINT_ATTR2(field1, field2) \
954 PRINT_ATTR2N(#field1, field1, #field2, field2)
955
956static size_t perf_event_attr__fprintf(struct perf_event_attr *attr, FILE *fp)
957{
958 size_t ret = 0;
959
960 ret += fprintf(fp, "%.60s\n", graph_dotted_line);
961 ret += fprintf(fp, "perf_event_attr:\n");
962
963 ret += PRINT_ATTR_U32(type);
964 ret += PRINT_ATTR_U32(size);
965 ret += PRINT_ATTR_X64(config);
966 ret += PRINT_ATTR_U64(sample_period);
967 ret += PRINT_ATTR_U64(sample_freq);
968 ret += PRINT_ATTR_X64(sample_type);
969 ret += PRINT_ATTR_X64(read_format);
970
971 ret += PRINT_ATTR2(disabled, inherit);
972 ret += PRINT_ATTR2(pinned, exclusive);
973 ret += PRINT_ATTR2(exclude_user, exclude_kernel);
974 ret += PRINT_ATTR2(exclude_hv, exclude_idle);
975 ret += PRINT_ATTR2(mmap, comm);
976 ret += PRINT_ATTR2(freq, inherit_stat);
977 ret += PRINT_ATTR2(enable_on_exec, task);
978 ret += PRINT_ATTR2(watermark, precise_ip);
979 ret += PRINT_ATTR2(mmap_data, sample_id_all);
980 ret += PRINT_ATTR2(exclude_host, exclude_guest);
981 ret += PRINT_ATTR2N("excl.callchain_kern", exclude_callchain_kernel,
982 "excl.callchain_user", exclude_callchain_user);
983
984 ret += PRINT_ATTR_U32(wakeup_events);
985 ret += PRINT_ATTR_U32(wakeup_watermark);
986 ret += PRINT_ATTR_X32(bp_type);
987 ret += PRINT_ATTR_X64(bp_addr);
988 ret += PRINT_ATTR_X64(config1);
989 ret += PRINT_ATTR_U64(bp_len);
990 ret += PRINT_ATTR_X64(config2);
991 ret += PRINT_ATTR_X64(branch_sample_type);
992 ret += PRINT_ATTR_X64(sample_regs_user);
993 ret += PRINT_ATTR_U32(sample_stack_user);
994
995 ret += fprintf(fp, "%.60s\n", graph_dotted_line);
996
997 return ret;
998}
999
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -02001000static int __perf_evsel__open(struct perf_evsel *evsel, struct cpu_map *cpus,
Jiri Olsa6a4bb042012-08-08 12:22:36 +02001001 struct thread_map *threads)
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -02001002{
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -02001003 int cpu, thread;
Stephane Eranian023695d2011-02-14 11:20:01 +02001004 unsigned long flags = 0;
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -02001005 int pid = -1, err;
Andi Kleenbec19672013-08-04 19:41:26 -07001006 enum { NO_CHANGE, SET_TO_MAX, INCREASED_MAX } set_rlimit = NO_CHANGE;
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -02001007
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -02001008 if (evsel->fd == NULL &&
1009 perf_evsel__alloc_fd(evsel, cpus->nr, threads->nr) < 0)
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -02001010 return -ENOMEM;
Arnaldo Carvalho de Melo4eed11d2011-01-04 00:13:17 -02001011
Stephane Eranian023695d2011-02-14 11:20:01 +02001012 if (evsel->cgrp) {
1013 flags = PERF_FLAG_PID_CGROUP;
1014 pid = evsel->cgrp->fd;
1015 }
1016
Arnaldo Carvalho de Melo594ac612012-12-13 13:13:07 -03001017fallback_missing_features:
1018 if (perf_missing_features.exclude_guest)
1019 evsel->attr.exclude_guest = evsel->attr.exclude_host = 0;
1020retry_sample_id:
1021 if (perf_missing_features.sample_id_all)
1022 evsel->attr.sample_id_all = 0;
1023
Adrian Huntere3e1a542013-08-14 15:48:24 +03001024 if (verbose >= 2)
1025 perf_event_attr__fprintf(&evsel->attr, stderr);
1026
Arnaldo Carvalho de Melo86bd5e82011-01-03 23:09:46 -02001027 for (cpu = 0; cpu < cpus->nr; cpu++) {
Arnaldo Carvalho de Melo9d04f172011-01-12 00:08:18 -02001028
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -02001029 for (thread = 0; thread < threads->nr; thread++) {
Jiri Olsa6a4bb042012-08-08 12:22:36 +02001030 int group_fd;
Stephane Eranian023695d2011-02-14 11:20:01 +02001031
1032 if (!evsel->cgrp)
1033 pid = threads->map[thread];
1034
Jiri Olsa6a4bb042012-08-08 12:22:36 +02001035 group_fd = get_group_fd(evsel, cpu, thread);
Andi Kleenbec19672013-08-04 19:41:26 -07001036retry_open:
Adrian Huntere3e1a542013-08-14 15:48:24 +03001037 pr_debug2("perf_event_open: pid %d cpu %d group_fd %d flags %#lx\n",
1038 pid, cpus->map[cpu], group_fd, flags);
1039
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -02001040 FD(evsel, cpu, thread) = sys_perf_event_open(&evsel->attr,
Stephane Eranian023695d2011-02-14 11:20:01 +02001041 pid,
Arnaldo Carvalho de Melof08199d2011-01-11 23:42:19 -02001042 cpus->map[cpu],
Stephane Eranian023695d2011-02-14 11:20:01 +02001043 group_fd, flags);
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -02001044 if (FD(evsel, cpu, thread) < 0) {
1045 err = -errno;
Arnaldo Carvalho de Melo594ac612012-12-13 13:13:07 -03001046 goto try_fallback;
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -02001047 }
Andi Kleenbec19672013-08-04 19:41:26 -07001048 set_rlimit = NO_CHANGE;
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -02001049 }
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -02001050 }
1051
1052 return 0;
1053
Arnaldo Carvalho de Melo594ac612012-12-13 13:13:07 -03001054try_fallback:
Andi Kleenbec19672013-08-04 19:41:26 -07001055 /*
1056 * perf stat needs between 5 and 22 fds per CPU. When we run out
1057 * of them try to increase the limits.
1058 */
1059 if (err == -EMFILE && set_rlimit < INCREASED_MAX) {
1060 struct rlimit l;
1061 int old_errno = errno;
1062
1063 if (getrlimit(RLIMIT_NOFILE, &l) == 0) {
1064 if (set_rlimit == NO_CHANGE)
1065 l.rlim_cur = l.rlim_max;
1066 else {
1067 l.rlim_cur = l.rlim_max + 1000;
1068 l.rlim_max = l.rlim_cur;
1069 }
1070 if (setrlimit(RLIMIT_NOFILE, &l) == 0) {
1071 set_rlimit++;
1072 errno = old_errno;
1073 goto retry_open;
1074 }
1075 }
1076 errno = old_errno;
1077 }
1078
Arnaldo Carvalho de Melo594ac612012-12-13 13:13:07 -03001079 if (err != -EINVAL || cpu > 0 || thread > 0)
1080 goto out_close;
1081
1082 if (!perf_missing_features.exclude_guest &&
1083 (evsel->attr.exclude_guest || evsel->attr.exclude_host)) {
1084 perf_missing_features.exclude_guest = true;
1085 goto fallback_missing_features;
1086 } else if (!perf_missing_features.sample_id_all) {
1087 perf_missing_features.sample_id_all = true;
1088 goto retry_sample_id;
1089 }
1090
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -02001091out_close:
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -02001092 do {
1093 while (--thread >= 0) {
1094 close(FD(evsel, cpu, thread));
1095 FD(evsel, cpu, thread) = -1;
1096 }
1097 thread = threads->nr;
1098 } while (--cpu >= 0);
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -02001099 return err;
1100}
1101
1102void perf_evsel__close(struct perf_evsel *evsel, int ncpus, int nthreads)
1103{
1104 if (evsel->fd == NULL)
1105 return;
1106
1107 perf_evsel__close_fd(evsel, ncpus, nthreads);
1108 perf_evsel__free_fd(evsel);
1109 evsel->fd = NULL;
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -02001110}
1111
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -02001112static struct {
1113 struct cpu_map map;
1114 int cpus[1];
1115} empty_cpu_map = {
1116 .map.nr = 1,
1117 .cpus = { -1, },
1118};
1119
1120static struct {
1121 struct thread_map map;
1122 int threads[1];
1123} empty_thread_map = {
1124 .map.nr = 1,
1125 .threads = { -1, },
1126};
1127
Arnaldo Carvalho de Melof08199d2011-01-11 23:42:19 -02001128int perf_evsel__open(struct perf_evsel *evsel, struct cpu_map *cpus,
Jiri Olsa6a4bb042012-08-08 12:22:36 +02001129 struct thread_map *threads)
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -02001130{
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -02001131 if (cpus == NULL) {
1132 /* Work around old compiler warnings about strict aliasing */
1133 cpus = &empty_cpu_map.map;
1134 }
1135
1136 if (threads == NULL)
1137 threads = &empty_thread_map.map;
1138
Jiri Olsa6a4bb042012-08-08 12:22:36 +02001139 return __perf_evsel__open(evsel, cpus, threads);
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -02001140}
1141
Arnaldo Carvalho de Melof08199d2011-01-11 23:42:19 -02001142int perf_evsel__open_per_cpu(struct perf_evsel *evsel,
Jiri Olsa6a4bb042012-08-08 12:22:36 +02001143 struct cpu_map *cpus)
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -02001144{
Jiri Olsa6a4bb042012-08-08 12:22:36 +02001145 return __perf_evsel__open(evsel, cpus, &empty_thread_map.map);
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -02001146}
1147
Arnaldo Carvalho de Melof08199d2011-01-11 23:42:19 -02001148int perf_evsel__open_per_thread(struct perf_evsel *evsel,
Jiri Olsa6a4bb042012-08-08 12:22:36 +02001149 struct thread_map *threads)
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -02001150{
Jiri Olsa6a4bb042012-08-08 12:22:36 +02001151 return __perf_evsel__open(evsel, &empty_cpu_map.map, threads);
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -02001152}
Arnaldo Carvalho de Melo70082dd2011-01-12 17:03:24 -02001153
Arnaldo Carvalho de Melo0807d2d2012-09-26 12:48:18 -03001154static int perf_evsel__parse_id_sample(const struct perf_evsel *evsel,
1155 const union perf_event *event,
1156 struct perf_sample *sample)
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001157{
Arnaldo Carvalho de Melo0807d2d2012-09-26 12:48:18 -03001158 u64 type = evsel->attr.sample_type;
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001159 const u64 *array = event->sample.array;
Arnaldo Carvalho de Melo0807d2d2012-09-26 12:48:18 -03001160 bool swapped = evsel->needs_swap;
Jiri Olsa37073f92012-05-30 14:23:44 +02001161 union u64_swap u;
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001162
1163 array += ((event->header.size -
1164 sizeof(event->header)) / sizeof(u64)) - 1;
1165
Adrian Hunter75562572013-08-27 11:23:09 +03001166 if (type & PERF_SAMPLE_IDENTIFIER) {
1167 sample->id = *array;
1168 array--;
1169 }
1170
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001171 if (type & PERF_SAMPLE_CPU) {
Jiri Olsa37073f92012-05-30 14:23:44 +02001172 u.val64 = *array;
1173 if (swapped) {
1174 /* undo swap of u64, then swap on individual u32s */
1175 u.val64 = bswap_64(u.val64);
1176 u.val32[0] = bswap_32(u.val32[0]);
1177 }
1178
1179 sample->cpu = u.val32[0];
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001180 array--;
1181 }
1182
1183 if (type & PERF_SAMPLE_STREAM_ID) {
1184 sample->stream_id = *array;
1185 array--;
1186 }
1187
1188 if (type & PERF_SAMPLE_ID) {
1189 sample->id = *array;
1190 array--;
1191 }
1192
1193 if (type & PERF_SAMPLE_TIME) {
1194 sample->time = *array;
1195 array--;
1196 }
1197
1198 if (type & PERF_SAMPLE_TID) {
Jiri Olsa37073f92012-05-30 14:23:44 +02001199 u.val64 = *array;
1200 if (swapped) {
1201 /* undo swap of u64, then swap on individual u32s */
1202 u.val64 = bswap_64(u.val64);
1203 u.val32[0] = bswap_32(u.val32[0]);
1204 u.val32[1] = bswap_32(u.val32[1]);
1205 }
1206
1207 sample->pid = u.val32[0];
1208 sample->tid = u.val32[1];
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001209 }
1210
1211 return 0;
1212}
1213
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001214static inline bool overflow(const void *endp, u16 max_size, const void *offset,
1215 u64 size)
Frederic Weisbecker98e1da92011-05-21 20:08:15 +02001216{
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001217 return size > max_size || offset + size > endp;
Frederic Weisbecker98e1da92011-05-21 20:08:15 +02001218}
1219
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001220#define OVERFLOW_CHECK(offset, size, max_size) \
1221 do { \
1222 if (overflow(endp, (max_size), (offset), (size))) \
1223 return -EFAULT; \
1224 } while (0)
1225
1226#define OVERFLOW_CHECK_u64(offset) \
1227 OVERFLOW_CHECK(offset, sizeof(u64), sizeof(u64))
1228
Arnaldo Carvalho de Meloa3f698f2012-08-02 12:23:46 -03001229int perf_evsel__parse_sample(struct perf_evsel *evsel, union perf_event *event,
Arnaldo Carvalho de Melo0807d2d2012-09-26 12:48:18 -03001230 struct perf_sample *data)
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001231{
Arnaldo Carvalho de Meloa3f698f2012-08-02 12:23:46 -03001232 u64 type = evsel->attr.sample_type;
Arnaldo Carvalho de Melo0807d2d2012-09-26 12:48:18 -03001233 bool swapped = evsel->needs_swap;
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001234 const u64 *array;
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001235 u16 max_size = event->header.size;
1236 const void *endp = (void *)event + max_size;
1237 u64 sz;
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001238
David Ahern936be502011-09-06 09:12:26 -06001239 /*
1240 * used for cross-endian analysis. See git commit 65014ab3
1241 * for why this goofiness is needed.
1242 */
Jiri Olsa6a11f922012-05-16 08:59:04 +02001243 union u64_swap u;
David Ahern936be502011-09-06 09:12:26 -06001244
Robert Richterf3bda2c2011-12-15 17:32:39 +01001245 memset(data, 0, sizeof(*data));
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001246 data->cpu = data->pid = data->tid = -1;
1247 data->stream_id = data->id = data->time = -1ULL;
Naveen N. Raoa4a03fc2012-02-03 22:31:13 +05301248 data->period = 1;
Andi Kleen05484292013-01-24 16:10:29 +01001249 data->weight = 0;
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001250
1251 if (event->header.type != PERF_RECORD_SAMPLE) {
Arnaldo Carvalho de Meloa3f698f2012-08-02 12:23:46 -03001252 if (!evsel->attr.sample_id_all)
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001253 return 0;
Arnaldo Carvalho de Melo0807d2d2012-09-26 12:48:18 -03001254 return perf_evsel__parse_id_sample(evsel, event, data);
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001255 }
1256
1257 array = event->sample.array;
1258
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001259 /*
1260 * The evsel's sample_size is based on PERF_SAMPLE_MASK which includes
1261 * up to PERF_SAMPLE_PERIOD. After that overflow() must be used to
1262 * check the format does not go past the end of the event.
1263 */
Arnaldo Carvalho de Meloa3f698f2012-08-02 12:23:46 -03001264 if (evsel->sample_size + sizeof(event->header) > event->header.size)
Frederic Weisbeckera2854122011-05-21 19:33:04 +02001265 return -EFAULT;
1266
Adrian Hunter75562572013-08-27 11:23:09 +03001267 data->id = -1ULL;
1268 if (type & PERF_SAMPLE_IDENTIFIER) {
1269 data->id = *array;
1270 array++;
1271 }
1272
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001273 if (type & PERF_SAMPLE_IP) {
Adrian Hunteref893252013-08-27 11:23:06 +03001274 data->ip = *array;
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001275 array++;
1276 }
1277
1278 if (type & PERF_SAMPLE_TID) {
David Ahern936be502011-09-06 09:12:26 -06001279 u.val64 = *array;
1280 if (swapped) {
1281 /* undo swap of u64, then swap on individual u32s */
1282 u.val64 = bswap_64(u.val64);
1283 u.val32[0] = bswap_32(u.val32[0]);
1284 u.val32[1] = bswap_32(u.val32[1]);
1285 }
1286
1287 data->pid = u.val32[0];
1288 data->tid = u.val32[1];
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001289 array++;
1290 }
1291
1292 if (type & PERF_SAMPLE_TIME) {
1293 data->time = *array;
1294 array++;
1295 }
1296
David Ahern7cec0922011-05-30 13:08:23 -06001297 data->addr = 0;
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001298 if (type & PERF_SAMPLE_ADDR) {
1299 data->addr = *array;
1300 array++;
1301 }
1302
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001303 if (type & PERF_SAMPLE_ID) {
1304 data->id = *array;
1305 array++;
1306 }
1307
1308 if (type & PERF_SAMPLE_STREAM_ID) {
1309 data->stream_id = *array;
1310 array++;
1311 }
1312
1313 if (type & PERF_SAMPLE_CPU) {
David Ahern936be502011-09-06 09:12:26 -06001314
1315 u.val64 = *array;
1316 if (swapped) {
1317 /* undo swap of u64, then swap on individual u32s */
1318 u.val64 = bswap_64(u.val64);
1319 u.val32[0] = bswap_32(u.val32[0]);
1320 }
1321
1322 data->cpu = u.val32[0];
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001323 array++;
1324 }
1325
1326 if (type & PERF_SAMPLE_PERIOD) {
1327 data->period = *array;
1328 array++;
1329 }
1330
1331 if (type & PERF_SAMPLE_READ) {
Jiri Olsa9ede4732012-10-10 17:38:13 +02001332 u64 read_format = evsel->attr.read_format;
1333
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001334 OVERFLOW_CHECK_u64(array);
Jiri Olsa9ede4732012-10-10 17:38:13 +02001335 if (read_format & PERF_FORMAT_GROUP)
1336 data->read.group.nr = *array;
1337 else
1338 data->read.one.value = *array;
1339
1340 array++;
1341
1342 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) {
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001343 OVERFLOW_CHECK_u64(array);
Jiri Olsa9ede4732012-10-10 17:38:13 +02001344 data->read.time_enabled = *array;
1345 array++;
1346 }
1347
1348 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) {
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001349 OVERFLOW_CHECK_u64(array);
Jiri Olsa9ede4732012-10-10 17:38:13 +02001350 data->read.time_running = *array;
1351 array++;
1352 }
1353
1354 /* PERF_FORMAT_ID is forced for PERF_SAMPLE_READ */
1355 if (read_format & PERF_FORMAT_GROUP) {
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001356 const u64 max_group_nr = UINT64_MAX /
1357 sizeof(struct sample_read_value);
1358
1359 if (data->read.group.nr > max_group_nr)
1360 return -EFAULT;
1361 sz = data->read.group.nr *
1362 sizeof(struct sample_read_value);
1363 OVERFLOW_CHECK(array, sz, max_size);
1364 data->read.group.values =
1365 (struct sample_read_value *)array;
1366 array = (void *)array + sz;
Jiri Olsa9ede4732012-10-10 17:38:13 +02001367 } else {
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001368 OVERFLOW_CHECK_u64(array);
Jiri Olsa9ede4732012-10-10 17:38:13 +02001369 data->read.one.id = *array;
1370 array++;
1371 }
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001372 }
1373
1374 if (type & PERF_SAMPLE_CALLCHAIN) {
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001375 const u64 max_callchain_nr = UINT64_MAX / sizeof(u64);
1376
1377 OVERFLOW_CHECK_u64(array);
1378 data->callchain = (struct ip_callchain *)array++;
1379 if (data->callchain->nr > max_callchain_nr)
Frederic Weisbecker98e1da92011-05-21 20:08:15 +02001380 return -EFAULT;
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001381 sz = data->callchain->nr * sizeof(u64);
1382 OVERFLOW_CHECK(array, sz, max_size);
1383 array = (void *)array + sz;
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001384 }
1385
1386 if (type & PERF_SAMPLE_RAW) {
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001387 OVERFLOW_CHECK_u64(array);
David Ahern936be502011-09-06 09:12:26 -06001388 u.val64 = *array;
1389 if (WARN_ONCE(swapped,
1390 "Endianness of raw data not corrected!\n")) {
1391 /* undo swap of u64, then swap on individual u32s */
1392 u.val64 = bswap_64(u.val64);
1393 u.val32[0] = bswap_32(u.val32[0]);
1394 u.val32[1] = bswap_32(u.val32[1]);
1395 }
David Ahern936be502011-09-06 09:12:26 -06001396 data->raw_size = u.val32[0];
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001397 array = (void *)array + sizeof(u32);
Frederic Weisbecker98e1da92011-05-21 20:08:15 +02001398
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001399 OVERFLOW_CHECK(array, data->raw_size, max_size);
1400 data->raw_data = (void *)array;
1401 array = (void *)array + data->raw_size;
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001402 }
1403
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +01001404 if (type & PERF_SAMPLE_BRANCH_STACK) {
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001405 const u64 max_branch_nr = UINT64_MAX /
1406 sizeof(struct branch_entry);
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +01001407
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001408 OVERFLOW_CHECK_u64(array);
1409 data->branch_stack = (struct branch_stack *)array++;
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +01001410
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001411 if (data->branch_stack->nr > max_branch_nr)
1412 return -EFAULT;
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +01001413 sz = data->branch_stack->nr * sizeof(struct branch_entry);
Adrian Hunter03b6ea92013-08-27 11:23:04 +03001414 OVERFLOW_CHECK(array, sz, max_size);
1415 array = (void *)array + sz;
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +01001416 }
Jiri Olsa0f6a3012012-08-07 15:20:45 +02001417
1418 if (type & PERF_SAMPLE_REGS_USER) {
1419 /* First u64 tells us if we have any regs in sample. */
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
1469int perf_event__synthesize_sample(union perf_event *event, u64 type,
1470 const struct perf_sample *sample,
1471 bool swapped)
1472{
1473 u64 *array;
1474
1475 /*
1476 * used for cross-endian analysis. See git commit 65014ab3
1477 * for why this goofiness is needed.
1478 */
Jiri Olsa6a11f922012-05-16 08:59:04 +02001479 union u64_swap u;
Andrew Vagin74eec262011-11-28 12:03:31 +03001480
1481 array = event->sample.array;
1482
Adrian Hunter75562572013-08-27 11:23:09 +03001483 if (type & PERF_SAMPLE_IDENTIFIER) {
1484 *array = sample->id;
1485 array++;
1486 }
1487
Andrew Vagin74eec262011-11-28 12:03:31 +03001488 if (type & PERF_SAMPLE_IP) {
Adrian Hunteref893252013-08-27 11:23:06 +03001489 *array = sample->ip;
Andrew Vagin74eec262011-11-28 12:03:31 +03001490 array++;
1491 }
1492
1493 if (type & PERF_SAMPLE_TID) {
1494 u.val32[0] = sample->pid;
1495 u.val32[1] = sample->tid;
1496 if (swapped) {
1497 /*
Arnaldo Carvalho de Meloa3f698f2012-08-02 12:23:46 -03001498 * Inverse of what is done in perf_evsel__parse_sample
Andrew Vagin74eec262011-11-28 12:03:31 +03001499 */
1500 u.val32[0] = bswap_32(u.val32[0]);
1501 u.val32[1] = bswap_32(u.val32[1]);
1502 u.val64 = bswap_64(u.val64);
1503 }
1504
1505 *array = u.val64;
1506 array++;
1507 }
1508
1509 if (type & PERF_SAMPLE_TIME) {
1510 *array = sample->time;
1511 array++;
1512 }
1513
1514 if (type & PERF_SAMPLE_ADDR) {
1515 *array = sample->addr;
1516 array++;
1517 }
1518
1519 if (type & PERF_SAMPLE_ID) {
1520 *array = sample->id;
1521 array++;
1522 }
1523
1524 if (type & PERF_SAMPLE_STREAM_ID) {
1525 *array = sample->stream_id;
1526 array++;
1527 }
1528
1529 if (type & PERF_SAMPLE_CPU) {
1530 u.val32[0] = sample->cpu;
1531 if (swapped) {
1532 /*
Arnaldo Carvalho de Meloa3f698f2012-08-02 12:23:46 -03001533 * Inverse of what is done in perf_evsel__parse_sample
Andrew Vagin74eec262011-11-28 12:03:31 +03001534 */
1535 u.val32[0] = bswap_32(u.val32[0]);
1536 u.val64 = bswap_64(u.val64);
1537 }
1538 *array = u.val64;
1539 array++;
1540 }
1541
1542 if (type & PERF_SAMPLE_PERIOD) {
1543 *array = sample->period;
1544 array++;
1545 }
1546
1547 return 0;
1548}
Arnaldo Carvalho de Melo5555ded2012-09-11 19:24:23 -03001549
Arnaldo Carvalho de Meloefd2b922012-09-18 11:21:50 -03001550struct format_field *perf_evsel__field(struct perf_evsel *evsel, const char *name)
1551{
1552 return pevent_find_field(evsel->tp_format, name);
1553}
1554
Arnaldo Carvalho de Melo5d2074e2012-09-26 20:22:00 -03001555void *perf_evsel__rawptr(struct perf_evsel *evsel, struct perf_sample *sample,
Arnaldo Carvalho de Melo5555ded2012-09-11 19:24:23 -03001556 const char *name)
1557{
Arnaldo Carvalho de Meloefd2b922012-09-18 11:21:50 -03001558 struct format_field *field = perf_evsel__field(evsel, name);
Arnaldo Carvalho de Melo5555ded2012-09-11 19:24:23 -03001559 int offset;
1560
Arnaldo Carvalho de Meloefd2b922012-09-18 11:21:50 -03001561 if (!field)
1562 return NULL;
Arnaldo Carvalho de Melo5555ded2012-09-11 19:24:23 -03001563
1564 offset = field->offset;
1565
1566 if (field->flags & FIELD_IS_DYNAMIC) {
1567 offset = *(int *)(sample->raw_data + field->offset);
1568 offset &= 0xffff;
1569 }
1570
1571 return sample->raw_data + offset;
1572}
1573
1574u64 perf_evsel__intval(struct perf_evsel *evsel, struct perf_sample *sample,
1575 const char *name)
1576{
Arnaldo Carvalho de Meloefd2b922012-09-18 11:21:50 -03001577 struct format_field *field = perf_evsel__field(evsel, name);
Arnaldo Carvalho de Meloe6b6f672012-09-26 13:13:04 -03001578 void *ptr;
1579 u64 value;
Arnaldo Carvalho de Melo5555ded2012-09-11 19:24:23 -03001580
Arnaldo Carvalho de Meloefd2b922012-09-18 11:21:50 -03001581 if (!field)
1582 return 0;
Arnaldo Carvalho de Melo5555ded2012-09-11 19:24:23 -03001583
Arnaldo Carvalho de Meloe6b6f672012-09-26 13:13:04 -03001584 ptr = sample->raw_data + field->offset;
Arnaldo Carvalho de Melo5555ded2012-09-11 19:24:23 -03001585
Arnaldo Carvalho de Meloe6b6f672012-09-26 13:13:04 -03001586 switch (field->size) {
1587 case 1:
1588 return *(u8 *)ptr;
1589 case 2:
1590 value = *(u16 *)ptr;
1591 break;
1592 case 4:
1593 value = *(u32 *)ptr;
1594 break;
1595 case 8:
1596 value = *(u64 *)ptr;
1597 break;
1598 default:
1599 return 0;
1600 }
1601
1602 if (!evsel->needs_swap)
1603 return value;
1604
1605 switch (field->size) {
1606 case 2:
1607 return bswap_16(value);
1608 case 4:
1609 return bswap_32(value);
1610 case 8:
1611 return bswap_64(value);
1612 default:
1613 return 0;
1614 }
1615
1616 return 0;
Arnaldo Carvalho de Melo5555ded2012-09-11 19:24:23 -03001617}
Arnaldo Carvalho de Melo0698aed2012-12-10 18:17:08 -03001618
1619static int comma_fprintf(FILE *fp, bool *first, const char *fmt, ...)
1620{
1621 va_list args;
1622 int ret = 0;
1623
1624 if (!*first) {
1625 ret += fprintf(fp, ",");
1626 } else {
1627 ret += fprintf(fp, ":");
1628 *first = false;
1629 }
1630
1631 va_start(args, fmt);
1632 ret += vfprintf(fp, fmt, args);
1633 va_end(args);
1634 return ret;
1635}
1636
1637static int __if_fprintf(FILE *fp, bool *first, const char *field, u64 value)
1638{
1639 if (value == 0)
1640 return 0;
1641
1642 return comma_fprintf(fp, first, " %s: %" PRIu64, field, value);
1643}
1644
1645#define if_print(field) printed += __if_fprintf(fp, &first, #field, evsel->attr.field)
1646
Arnaldo Carvalho de Meloc79a4392012-12-11 10:54:12 -03001647struct bit_names {
1648 int bit;
1649 const char *name;
1650};
1651
1652static int bits__fprintf(FILE *fp, const char *field, u64 value,
1653 struct bit_names *bits, bool *first)
1654{
1655 int i = 0, printed = comma_fprintf(fp, first, " %s: ", field);
1656 bool first_bit = true;
1657
1658 do {
1659 if (value & bits[i].bit) {
1660 printed += fprintf(fp, "%s%s", first_bit ? "" : "|", bits[i].name);
1661 first_bit = false;
1662 }
1663 } while (bits[++i].name != NULL);
1664
1665 return printed;
1666}
1667
1668static int sample_type__fprintf(FILE *fp, bool *first, u64 value)
1669{
1670#define bit_name(n) { PERF_SAMPLE_##n, #n }
1671 struct bit_names bits[] = {
1672 bit_name(IP), bit_name(TID), bit_name(TIME), bit_name(ADDR),
1673 bit_name(READ), bit_name(CALLCHAIN), bit_name(ID), bit_name(CPU),
1674 bit_name(PERIOD), bit_name(STREAM_ID), bit_name(RAW),
1675 bit_name(BRANCH_STACK), bit_name(REGS_USER), bit_name(STACK_USER),
Adrian Hunter75562572013-08-27 11:23:09 +03001676 bit_name(IDENTIFIER),
Arnaldo Carvalho de Meloc79a4392012-12-11 10:54:12 -03001677 { .name = NULL, }
1678 };
1679#undef bit_name
1680 return bits__fprintf(fp, "sample_type", value, bits, first);
1681}
1682
1683static int read_format__fprintf(FILE *fp, bool *first, u64 value)
1684{
1685#define bit_name(n) { PERF_FORMAT_##n, #n }
1686 struct bit_names bits[] = {
1687 bit_name(TOTAL_TIME_ENABLED), bit_name(TOTAL_TIME_RUNNING),
1688 bit_name(ID), bit_name(GROUP),
1689 { .name = NULL, }
1690 };
1691#undef bit_name
1692 return bits__fprintf(fp, "read_format", value, bits, first);
1693}
1694
Arnaldo Carvalho de Melo0698aed2012-12-10 18:17:08 -03001695int perf_evsel__fprintf(struct perf_evsel *evsel,
1696 struct perf_attr_details *details, FILE *fp)
1697{
1698 bool first = true;
Namhyung Kime6ab07d2013-01-22 18:09:47 +09001699 int printed = 0;
1700
Arnaldo Carvalho de Meloe35ef3552013-02-06 17:20:02 -03001701 if (details->event_group) {
Namhyung Kime6ab07d2013-01-22 18:09:47 +09001702 struct perf_evsel *pos;
1703
1704 if (!perf_evsel__is_group_leader(evsel))
1705 return 0;
1706
1707 if (evsel->nr_members > 1)
1708 printed += fprintf(fp, "%s{", evsel->group_name ?: "");
1709
1710 printed += fprintf(fp, "%s", perf_evsel__name(evsel));
1711 for_each_group_member(pos, evsel)
1712 printed += fprintf(fp, ",%s", perf_evsel__name(pos));
1713
1714 if (evsel->nr_members > 1)
1715 printed += fprintf(fp, "}");
1716 goto out;
1717 }
1718
1719 printed += fprintf(fp, "%s", perf_evsel__name(evsel));
Arnaldo Carvalho de Melo0698aed2012-12-10 18:17:08 -03001720
1721 if (details->verbose || details->freq) {
1722 printed += comma_fprintf(fp, &first, " sample_freq=%" PRIu64,
1723 (u64)evsel->attr.sample_freq);
1724 }
1725
1726 if (details->verbose) {
1727 if_print(type);
1728 if_print(config);
1729 if_print(config1);
1730 if_print(config2);
1731 if_print(size);
Arnaldo Carvalho de Meloc79a4392012-12-11 10:54:12 -03001732 printed += sample_type__fprintf(fp, &first, evsel->attr.sample_type);
1733 if (evsel->attr.read_format)
1734 printed += read_format__fprintf(fp, &first, evsel->attr.read_format);
Arnaldo Carvalho de Melo0698aed2012-12-10 18:17:08 -03001735 if_print(disabled);
1736 if_print(inherit);
1737 if_print(pinned);
1738 if_print(exclusive);
1739 if_print(exclude_user);
1740 if_print(exclude_kernel);
1741 if_print(exclude_hv);
1742 if_print(exclude_idle);
1743 if_print(mmap);
1744 if_print(comm);
1745 if_print(freq);
1746 if_print(inherit_stat);
1747 if_print(enable_on_exec);
1748 if_print(task);
1749 if_print(watermark);
1750 if_print(precise_ip);
1751 if_print(mmap_data);
1752 if_print(sample_id_all);
1753 if_print(exclude_host);
1754 if_print(exclude_guest);
1755 if_print(__reserved_1);
1756 if_print(wakeup_events);
1757 if_print(bp_type);
1758 if_print(branch_sample_type);
1759 }
Namhyung Kime6ab07d2013-01-22 18:09:47 +09001760out:
Arnaldo Carvalho de Melo0698aed2012-12-10 18:17:08 -03001761 fputc('\n', fp);
1762 return ++printed;
1763}
Arnaldo Carvalho de Meloc0a54342012-12-13 14:16:30 -03001764
1765bool perf_evsel__fallback(struct perf_evsel *evsel, int err,
1766 char *msg, size_t msgsize)
1767{
David Ahern2b821cc2013-07-18 17:27:59 -06001768 if ((err == ENOENT || err == ENXIO || err == ENODEV) &&
Arnaldo Carvalho de Meloc0a54342012-12-13 14:16:30 -03001769 evsel->attr.type == PERF_TYPE_HARDWARE &&
1770 evsel->attr.config == PERF_COUNT_HW_CPU_CYCLES) {
1771 /*
1772 * If it's cycles then fall back to hrtimer based
1773 * cpu-clock-tick sw counter, which is always available even if
1774 * no PMU support.
1775 *
1776 * PPC returns ENXIO until 2.6.37 (behavior changed with commit
1777 * b0a873e).
1778 */
1779 scnprintf(msg, msgsize, "%s",
1780"The cycles event is not supported, trying to fall back to cpu-clock-ticks");
1781
1782 evsel->attr.type = PERF_TYPE_SOFTWARE;
1783 evsel->attr.config = PERF_COUNT_SW_CPU_CLOCK;
1784
1785 free(evsel->name);
1786 evsel->name = NULL;
1787 return true;
1788 }
1789
1790 return false;
1791}
Arnaldo Carvalho de Melo56e52e82012-12-13 15:10:58 -03001792
1793int perf_evsel__open_strerror(struct perf_evsel *evsel,
1794 struct perf_target *target,
1795 int err, char *msg, size_t size)
1796{
1797 switch (err) {
1798 case EPERM:
1799 case EACCES:
David Ahernb69e63a2013-05-25 17:54:00 -06001800 return scnprintf(msg, size,
Arnaldo Carvalho de Melo56e52e82012-12-13 15:10:58 -03001801 "You may not have permission to collect %sstats.\n"
1802 "Consider tweaking /proc/sys/kernel/perf_event_paranoid:\n"
1803 " -1 - Not paranoid at all\n"
1804 " 0 - Disallow raw tracepoint access for unpriv\n"
1805 " 1 - Disallow cpu events for unpriv\n"
1806 " 2 - Disallow kernel profiling for unpriv",
1807 target->system_wide ? "system-wide " : "");
1808 case ENOENT:
1809 return scnprintf(msg, size, "The %s event is not supported.",
1810 perf_evsel__name(evsel));
1811 case EMFILE:
1812 return scnprintf(msg, size, "%s",
1813 "Too many events are opened.\n"
1814 "Try again after reducing the number of events.");
1815 case ENODEV:
1816 if (target->cpu_list)
1817 return scnprintf(msg, size, "%s",
1818 "No such device - did you specify an out-of-range profile CPU?\n");
1819 break;
1820 case EOPNOTSUPP:
1821 if (evsel->attr.precise_ip)
1822 return scnprintf(msg, size, "%s",
1823 "\'precise\' request may not be supported. Try removing 'p' modifier.");
1824#if defined(__i386__) || defined(__x86_64__)
1825 if (evsel->attr.type == PERF_TYPE_HARDWARE)
1826 return scnprintf(msg, size, "%s",
1827 "No hardware sampling interrupt available.\n"
1828 "No APIC? If so then you can boot the kernel with the \"lapic\" boot parameter to force-enable it.");
1829#endif
1830 break;
1831 default:
1832 break;
1833 }
1834
1835 return scnprintf(msg, size,
1836 "The sys_perf_event_open() syscall returned with %d (%s) for event (%s). \n"
1837 "/bin/dmesg may provide additional information.\n"
1838 "No CONFIG_PERF_EVENTS=y kernel support configured?\n",
1839 err, strerror(err), perf_evsel__name(evsel));
1840}