blob: 643df4b06b84e52eac8591edfc5a1d4f167ea094 [file] [log] [blame]
Arnaldo Carvalho de Melof8a95302011-01-30 10:46:46 -02001/*
2 * Copyright (C) 2011, Red Hat Inc, Arnaldo Carvalho de Melo <acme@redhat.com>
3 *
4 * Parts came from builtin-{top,stat,record}.c, see those files for further
5 * copyright notes.
6 *
7 * Released under the GPL v2. (and only v2, not any later version)
8 */
9
David Ahern936be502011-09-06 09:12:26 -060010#include <byteswap.h>
Jiri Olsa0f6a3012012-08-07 15:20:45 +020011#include <linux/bitops.h>
David Ahern936be502011-09-06 09:12:26 -060012#include "asm/bug.h"
Arnaldo Carvalho de Meloefd2b922012-09-18 11:21:50 -030013#include "debugfs.h"
Arnaldo Carvalho de Melo5555ded2012-09-11 19:24:23 -030014#include "event-parse.h"
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -020015#include "evsel.h"
Arnaldo Carvalho de Melo70082dd2011-01-12 17:03:24 -020016#include "evlist.h"
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -020017#include "util.h"
Arnaldo Carvalho de Melo86bd5e82011-01-03 23:09:46 -020018#include "cpumap.h"
Arnaldo Carvalho de Melofd782602011-01-18 15:15:24 -020019#include "thread_map.h"
Namhyung Kim12864b32012-04-26 14:15:22 +090020#include "target.h"
David Howellsd2709c72012-11-19 22:21:03 +000021#include <linux/hw_breakpoint.h>
22#include <linux/perf_event.h>
Jiri Olsa26d33022012-08-07 15:20:47 +020023#include "perf_regs.h"
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -020024
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -020025#define FD(e, x, y) (*(int *)xyarray__entry(e->fd, x, y))
26
Arnaldo Carvalho de Melobde09462012-08-01 18:53:11 -030027static int __perf_evsel__sample_size(u64 sample_type)
Arnaldo Carvalho de Meloc2a70652011-06-02 11:04:54 -030028{
29 u64 mask = sample_type & PERF_SAMPLE_MASK;
30 int size = 0;
31 int i;
32
33 for (i = 0; i < 64; i++) {
34 if (mask & (1ULL << i))
35 size++;
36 }
37
38 size *= sizeof(u64);
39
40 return size;
41}
42
Jiri Olsa4bf9ce12012-03-22 14:37:26 +010043void hists__init(struct hists *hists)
Arnaldo Carvalho de Melo0e2a5f12011-11-04 08:16:58 -020044{
45 memset(hists, 0, sizeof(*hists));
46 hists->entries_in_array[0] = hists->entries_in_array[1] = RB_ROOT;
47 hists->entries_in = &hists->entries_in_array[0];
48 hists->entries_collapsed = RB_ROOT;
49 hists->entries = RB_ROOT;
50 pthread_mutex_init(&hists->lock, NULL);
51}
52
Arnaldo Carvalho de Melo7be5ebe2012-12-10 14:53:43 -030053void __perf_evsel__set_sample_bit(struct perf_evsel *evsel,
54 enum perf_event_sample_format bit)
55{
56 if (!(evsel->attr.sample_type & bit)) {
57 evsel->attr.sample_type |= bit;
58 evsel->sample_size += sizeof(u64);
59 }
60}
61
62void __perf_evsel__reset_sample_bit(struct perf_evsel *evsel,
63 enum perf_event_sample_format bit)
64{
65 if (evsel->attr.sample_type & bit) {
66 evsel->attr.sample_type &= ~bit;
67 evsel->sample_size -= sizeof(u64);
68 }
69}
70
Arnaldo Carvalho de Melo7a5a5ca2012-12-10 15:21:30 -030071void perf_evsel__set_sample_id(struct perf_evsel *evsel)
72{
73 perf_evsel__set_sample_bit(evsel, ID);
74 evsel->attr.read_format |= PERF_FORMAT_ID;
75}
76
Arnaldo Carvalho de Meloef1d1af2011-01-18 21:41:45 -020077void perf_evsel__init(struct perf_evsel *evsel,
78 struct perf_event_attr *attr, int idx)
79{
80 evsel->idx = idx;
81 evsel->attr = *attr;
Namhyung Kim2cfda562012-11-29 15:38:29 +090082 evsel->leader = evsel;
Arnaldo Carvalho de Meloef1d1af2011-01-18 21:41:45 -020083 INIT_LIST_HEAD(&evsel->node);
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -030084 hists__init(&evsel->hists);
Arnaldo Carvalho de Melobde09462012-08-01 18:53:11 -030085 evsel->sample_size = __perf_evsel__sample_size(attr->sample_type);
Arnaldo Carvalho de Meloef1d1af2011-01-18 21:41:45 -020086}
87
Lin Ming23a2f3a2011-01-07 11:11:09 +080088struct perf_evsel *perf_evsel__new(struct perf_event_attr *attr, int idx)
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -020089{
90 struct perf_evsel *evsel = zalloc(sizeof(*evsel));
91
Arnaldo Carvalho de Meloef1d1af2011-01-18 21:41:45 -020092 if (evsel != NULL)
93 perf_evsel__init(evsel, attr, idx);
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -020094
95 return evsel;
96}
97
Arnaldo Carvalho de Melo201b7332012-09-26 20:24:19 -030098struct event_format *event_format__new(const char *sys, const char *name)
Arnaldo Carvalho de Meloefd2b922012-09-18 11:21:50 -030099{
100 int fd, n;
101 char *filename;
102 void *bf = NULL, *nbf;
103 size_t size = 0, alloc_size = 0;
104 struct event_format *format = NULL;
105
106 if (asprintf(&filename, "%s/%s/%s/format", tracing_events_path, sys, name) < 0)
107 goto out;
108
109 fd = open(filename, O_RDONLY);
110 if (fd < 0)
111 goto out_free_filename;
112
113 do {
114 if (size == alloc_size) {
115 alloc_size += BUFSIZ;
116 nbf = realloc(bf, alloc_size);
117 if (nbf == NULL)
118 goto out_free_bf;
119 bf = nbf;
120 }
121
122 n = read(fd, bf + size, BUFSIZ);
123 if (n < 0)
124 goto out_free_bf;
125 size += n;
126 } while (n > 0);
127
128 pevent_parse_format(&format, bf, size, sys);
129
130out_free_bf:
131 free(bf);
132 close(fd);
133out_free_filename:
134 free(filename);
135out:
136 return format;
137}
138
139struct perf_evsel *perf_evsel__newtp(const char *sys, const char *name, int idx)
140{
141 struct perf_evsel *evsel = zalloc(sizeof(*evsel));
142
143 if (evsel != NULL) {
144 struct perf_event_attr attr = {
Arnaldo Carvalho de Melo0b80f8b32012-09-26 12:28:26 -0300145 .type = PERF_TYPE_TRACEPOINT,
146 .sample_type = (PERF_SAMPLE_RAW | PERF_SAMPLE_TIME |
147 PERF_SAMPLE_CPU | PERF_SAMPLE_PERIOD),
Arnaldo Carvalho de Meloefd2b922012-09-18 11:21:50 -0300148 };
149
Arnaldo Carvalho de Meloe48ffe22012-09-26 17:11:38 -0300150 if (asprintf(&evsel->name, "%s:%s", sys, name) < 0)
151 goto out_free;
152
Arnaldo Carvalho de Meloefd2b922012-09-18 11:21:50 -0300153 evsel->tp_format = event_format__new(sys, name);
154 if (evsel->tp_format == NULL)
155 goto out_free;
156
Arnaldo Carvalho de Melo0b80f8b32012-09-26 12:28:26 -0300157 event_attr_init(&attr);
Arnaldo Carvalho de Meloefd2b922012-09-18 11:21:50 -0300158 attr.config = evsel->tp_format->id;
Arnaldo Carvalho de Melo0b80f8b32012-09-26 12:28:26 -0300159 attr.sample_period = 1;
Arnaldo Carvalho de Meloefd2b922012-09-18 11:21:50 -0300160 perf_evsel__init(evsel, &attr, idx);
Arnaldo Carvalho de Meloefd2b922012-09-18 11:21:50 -0300161 }
162
163 return evsel;
164
165out_free:
Arnaldo Carvalho de Meloe48ffe22012-09-26 17:11:38 -0300166 free(evsel->name);
Arnaldo Carvalho de Meloefd2b922012-09-18 11:21:50 -0300167 free(evsel);
168 return NULL;
169}
170
Arnaldo Carvalho de Melo8ad70132012-09-06 13:11:18 -0300171const char *perf_evsel__hw_names[PERF_COUNT_HW_MAX] = {
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300172 "cycles",
173 "instructions",
174 "cache-references",
175 "cache-misses",
176 "branches",
177 "branch-misses",
178 "bus-cycles",
179 "stalled-cycles-frontend",
180 "stalled-cycles-backend",
181 "ref-cycles",
182};
183
Arnaldo Carvalho de Melodd4f5222012-06-13 15:52:42 -0300184static const char *__perf_evsel__hw_name(u64 config)
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300185{
186 if (config < PERF_COUNT_HW_MAX && perf_evsel__hw_names[config])
187 return perf_evsel__hw_names[config];
188
189 return "unknown-hardware";
190}
191
Arnaldo Carvalho de Melo27f18612012-06-11 13:33:09 -0300192static int perf_evsel__add_modifiers(struct perf_evsel *evsel, char *bf, size_t size)
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300193{
Arnaldo Carvalho de Melo27f18612012-06-11 13:33:09 -0300194 int colon = 0, r = 0;
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300195 struct perf_event_attr *attr = &evsel->attr;
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300196 bool exclude_guest_default = false;
197
198#define MOD_PRINT(context, mod) do { \
199 if (!attr->exclude_##context) { \
Arnaldo Carvalho de Melo27f18612012-06-11 13:33:09 -0300200 if (!colon) colon = ++r; \
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300201 r += scnprintf(bf + r, size - r, "%c", mod); \
202 } } while(0)
203
204 if (attr->exclude_kernel || attr->exclude_user || attr->exclude_hv) {
205 MOD_PRINT(kernel, 'k');
206 MOD_PRINT(user, 'u');
207 MOD_PRINT(hv, 'h');
208 exclude_guest_default = true;
209 }
210
211 if (attr->precise_ip) {
212 if (!colon)
Arnaldo Carvalho de Melo27f18612012-06-11 13:33:09 -0300213 colon = ++r;
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300214 r += scnprintf(bf + r, size - r, "%.*s", attr->precise_ip, "ppp");
215 exclude_guest_default = true;
216 }
217
218 if (attr->exclude_host || attr->exclude_guest == exclude_guest_default) {
219 MOD_PRINT(host, 'H');
220 MOD_PRINT(guest, 'G');
221 }
222#undef MOD_PRINT
223 if (colon)
Arnaldo Carvalho de Melo27f18612012-06-11 13:33:09 -0300224 bf[colon - 1] = ':';
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300225 return r;
226}
227
Arnaldo Carvalho de Melo27f18612012-06-11 13:33:09 -0300228static int perf_evsel__hw_name(struct perf_evsel *evsel, char *bf, size_t size)
229{
230 int r = scnprintf(bf, size, "%s", __perf_evsel__hw_name(evsel->attr.config));
231 return r + perf_evsel__add_modifiers(evsel, bf + r, size - r);
232}
233
Arnaldo Carvalho de Melo8ad70132012-09-06 13:11:18 -0300234const char *perf_evsel__sw_names[PERF_COUNT_SW_MAX] = {
Arnaldo Carvalho de Melo335c2f52012-06-11 14:36:20 -0300235 "cpu-clock",
236 "task-clock",
237 "page-faults",
238 "context-switches",
Arnaldo Carvalho de Melo8ad70132012-09-06 13:11:18 -0300239 "cpu-migrations",
Arnaldo Carvalho de Melo335c2f52012-06-11 14:36:20 -0300240 "minor-faults",
241 "major-faults",
242 "alignment-faults",
243 "emulation-faults",
244};
245
Arnaldo Carvalho de Melodd4f5222012-06-13 15:52:42 -0300246static const char *__perf_evsel__sw_name(u64 config)
Arnaldo Carvalho de Melo335c2f52012-06-11 14:36:20 -0300247{
248 if (config < PERF_COUNT_SW_MAX && perf_evsel__sw_names[config])
249 return perf_evsel__sw_names[config];
250 return "unknown-software";
251}
252
253static int perf_evsel__sw_name(struct perf_evsel *evsel, char *bf, size_t size)
254{
255 int r = scnprintf(bf, size, "%s", __perf_evsel__sw_name(evsel->attr.config));
256 return r + perf_evsel__add_modifiers(evsel, bf + r, size - r);
257}
258
Jiri Olsa287e74a2012-06-28 23:18:49 +0200259static int __perf_evsel__bp_name(char *bf, size_t size, u64 addr, u64 type)
260{
261 int r;
262
263 r = scnprintf(bf, size, "mem:0x%" PRIx64 ":", addr);
264
265 if (type & HW_BREAKPOINT_R)
266 r += scnprintf(bf + r, size - r, "r");
267
268 if (type & HW_BREAKPOINT_W)
269 r += scnprintf(bf + r, size - r, "w");
270
271 if (type & HW_BREAKPOINT_X)
272 r += scnprintf(bf + r, size - r, "x");
273
274 return r;
275}
276
277static int perf_evsel__bp_name(struct perf_evsel *evsel, char *bf, size_t size)
278{
279 struct perf_event_attr *attr = &evsel->attr;
280 int r = __perf_evsel__bp_name(bf, size, attr->bp_addr, attr->bp_type);
281 return r + perf_evsel__add_modifiers(evsel, bf + r, size - r);
282}
283
Arnaldo Carvalho de Melo0b668bc2012-06-11 14:08:07 -0300284const char *perf_evsel__hw_cache[PERF_COUNT_HW_CACHE_MAX]
285 [PERF_EVSEL__MAX_ALIASES] = {
286 { "L1-dcache", "l1-d", "l1d", "L1-data", },
287 { "L1-icache", "l1-i", "l1i", "L1-instruction", },
288 { "LLC", "L2", },
289 { "dTLB", "d-tlb", "Data-TLB", },
290 { "iTLB", "i-tlb", "Instruction-TLB", },
291 { "branch", "branches", "bpu", "btb", "bpc", },
292 { "node", },
293};
294
295const char *perf_evsel__hw_cache_op[PERF_COUNT_HW_CACHE_OP_MAX]
296 [PERF_EVSEL__MAX_ALIASES] = {
297 { "load", "loads", "read", },
298 { "store", "stores", "write", },
299 { "prefetch", "prefetches", "speculative-read", "speculative-load", },
300};
301
302const char *perf_evsel__hw_cache_result[PERF_COUNT_HW_CACHE_RESULT_MAX]
303 [PERF_EVSEL__MAX_ALIASES] = {
304 { "refs", "Reference", "ops", "access", },
305 { "misses", "miss", },
306};
307
308#define C(x) PERF_COUNT_HW_CACHE_##x
309#define CACHE_READ (1 << C(OP_READ))
310#define CACHE_WRITE (1 << C(OP_WRITE))
311#define CACHE_PREFETCH (1 << C(OP_PREFETCH))
312#define COP(x) (1 << x)
313
314/*
315 * cache operartion stat
316 * L1I : Read and prefetch only
317 * ITLB and BPU : Read-only
318 */
319static unsigned long perf_evsel__hw_cache_stat[C(MAX)] = {
320 [C(L1D)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
321 [C(L1I)] = (CACHE_READ | CACHE_PREFETCH),
322 [C(LL)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
323 [C(DTLB)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
324 [C(ITLB)] = (CACHE_READ),
325 [C(BPU)] = (CACHE_READ),
326 [C(NODE)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
327};
328
329bool perf_evsel__is_cache_op_valid(u8 type, u8 op)
330{
331 if (perf_evsel__hw_cache_stat[type] & COP(op))
332 return true; /* valid */
333 else
334 return false; /* invalid */
335}
336
337int __perf_evsel__hw_cache_type_op_res_name(u8 type, u8 op, u8 result,
338 char *bf, size_t size)
339{
340 if (result) {
341 return scnprintf(bf, size, "%s-%s-%s", perf_evsel__hw_cache[type][0],
342 perf_evsel__hw_cache_op[op][0],
343 perf_evsel__hw_cache_result[result][0]);
344 }
345
346 return scnprintf(bf, size, "%s-%s", perf_evsel__hw_cache[type][0],
347 perf_evsel__hw_cache_op[op][1]);
348}
349
Arnaldo Carvalho de Melodd4f5222012-06-13 15:52:42 -0300350static int __perf_evsel__hw_cache_name(u64 config, char *bf, size_t size)
Arnaldo Carvalho de Melo0b668bc2012-06-11 14:08:07 -0300351{
352 u8 op, result, type = (config >> 0) & 0xff;
353 const char *err = "unknown-ext-hardware-cache-type";
354
355 if (type > PERF_COUNT_HW_CACHE_MAX)
356 goto out_err;
357
358 op = (config >> 8) & 0xff;
359 err = "unknown-ext-hardware-cache-op";
360 if (op > PERF_COUNT_HW_CACHE_OP_MAX)
361 goto out_err;
362
363 result = (config >> 16) & 0xff;
364 err = "unknown-ext-hardware-cache-result";
365 if (result > PERF_COUNT_HW_CACHE_RESULT_MAX)
366 goto out_err;
367
368 err = "invalid-cache";
369 if (!perf_evsel__is_cache_op_valid(type, op))
370 goto out_err;
371
372 return __perf_evsel__hw_cache_type_op_res_name(type, op, result, bf, size);
373out_err:
374 return scnprintf(bf, size, "%s", err);
375}
376
377static int perf_evsel__hw_cache_name(struct perf_evsel *evsel, char *bf, size_t size)
378{
379 int ret = __perf_evsel__hw_cache_name(evsel->attr.config, bf, size);
380 return ret + perf_evsel__add_modifiers(evsel, bf + ret, size - ret);
381}
382
Arnaldo Carvalho de Melo6eef3d92012-06-13 11:53:37 -0300383static int perf_evsel__raw_name(struct perf_evsel *evsel, char *bf, size_t size)
384{
385 int ret = scnprintf(bf, size, "raw 0x%" PRIx64, evsel->attr.config);
386 return ret + perf_evsel__add_modifiers(evsel, bf + ret, size - ret);
387}
388
Arnaldo Carvalho de Melo7289f832012-06-12 12:34:58 -0300389const char *perf_evsel__name(struct perf_evsel *evsel)
Arnaldo Carvalho de Meloa4460832012-06-12 10:29:12 -0300390{
Arnaldo Carvalho de Melo7289f832012-06-12 12:34:58 -0300391 char bf[128];
Arnaldo Carvalho de Meloa4460832012-06-12 10:29:12 -0300392
Arnaldo Carvalho de Melo7289f832012-06-12 12:34:58 -0300393 if (evsel->name)
394 return evsel->name;
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300395
396 switch (evsel->attr.type) {
397 case PERF_TYPE_RAW:
Arnaldo Carvalho de Melo6eef3d92012-06-13 11:53:37 -0300398 perf_evsel__raw_name(evsel, bf, sizeof(bf));
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300399 break;
400
401 case PERF_TYPE_HARDWARE:
Arnaldo Carvalho de Melo7289f832012-06-12 12:34:58 -0300402 perf_evsel__hw_name(evsel, bf, sizeof(bf));
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300403 break;
Arnaldo Carvalho de Melo0b668bc2012-06-11 14:08:07 -0300404
405 case PERF_TYPE_HW_CACHE:
Arnaldo Carvalho de Melo7289f832012-06-12 12:34:58 -0300406 perf_evsel__hw_cache_name(evsel, bf, sizeof(bf));
Arnaldo Carvalho de Melo0b668bc2012-06-11 14:08:07 -0300407 break;
408
Arnaldo Carvalho de Melo335c2f52012-06-11 14:36:20 -0300409 case PERF_TYPE_SOFTWARE:
Arnaldo Carvalho de Melo7289f832012-06-12 12:34:58 -0300410 perf_evsel__sw_name(evsel, bf, sizeof(bf));
Arnaldo Carvalho de Melo335c2f52012-06-11 14:36:20 -0300411 break;
412
Arnaldo Carvalho de Meloa4460832012-06-12 10:29:12 -0300413 case PERF_TYPE_TRACEPOINT:
Arnaldo Carvalho de Melo7289f832012-06-12 12:34:58 -0300414 scnprintf(bf, sizeof(bf), "%s", "unknown tracepoint");
Arnaldo Carvalho de Meloa4460832012-06-12 10:29:12 -0300415 break;
416
Jiri Olsa287e74a2012-06-28 23:18:49 +0200417 case PERF_TYPE_BREAKPOINT:
418 perf_evsel__bp_name(evsel, bf, sizeof(bf));
419 break;
420
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300421 default:
Robert Richterca1b1452012-08-16 21:10:18 +0200422 scnprintf(bf, sizeof(bf), "unknown attr type: %d",
423 evsel->attr.type);
Arnaldo Carvalho de Meloa4460832012-06-12 10:29:12 -0300424 break;
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300425 }
426
Arnaldo Carvalho de Melo7289f832012-06-12 12:34:58 -0300427 evsel->name = strdup(bf);
428
429 return evsel->name ?: "unknown";
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300430}
431
Jiri Olsa774cb492012-11-12 18:34:01 +0100432/*
433 * The enable_on_exec/disabled value strategy:
434 *
435 * 1) For any type of traced program:
436 * - all independent events and group leaders are disabled
437 * - all group members are enabled
438 *
439 * Group members are ruled by group leaders. They need to
440 * be enabled, because the group scheduling relies on that.
441 *
442 * 2) For traced programs executed by perf:
443 * - all independent events and group leaders have
444 * enable_on_exec set
445 * - we don't specifically enable or disable any event during
446 * the record command
447 *
448 * Independent events and group leaders are initially disabled
449 * and get enabled by exec. Group members are ruled by group
450 * leaders as stated in 1).
451 *
452 * 3) For traced programs attached by perf (pid/tid):
453 * - we specifically enable or disable all events during
454 * the record command
455 *
456 * When attaching events to already running traced we
457 * enable/disable events specifically, as there's no
458 * initial traced exec call.
459 */
Jiri Olsacac21422012-11-12 18:34:00 +0100460void perf_evsel__config(struct perf_evsel *evsel,
461 struct perf_record_opts *opts)
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200462{
463 struct perf_event_attr *attr = &evsel->attr;
464 int track = !evsel->idx; /* only the first counter needs these */
465
Arnaldo Carvalho de Melo808e1222012-02-14 14:18:57 -0200466 attr->sample_id_all = opts->sample_id_all_missing ? 0 : 1;
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200467 attr->inherit = !opts->no_inherit;
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200468
Arnaldo Carvalho de Melo7be5ebe2012-12-10 14:53:43 -0300469 perf_evsel__set_sample_bit(evsel, IP);
470 perf_evsel__set_sample_bit(evsel, TID);
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200471
472 /*
473 * We default some events to a 1 default interval. But keep
474 * it a weak assumption overridable by the user.
475 */
476 if (!attr->sample_period || (opts->user_freq != UINT_MAX &&
477 opts->user_interval != ULLONG_MAX)) {
478 if (opts->freq) {
Arnaldo Carvalho de Melo7be5ebe2012-12-10 14:53:43 -0300479 perf_evsel__set_sample_bit(evsel, PERIOD);
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200480 attr->freq = 1;
481 attr->sample_freq = opts->freq;
482 } else {
483 attr->sample_period = opts->default_interval;
484 }
485 }
486
487 if (opts->no_samples)
488 attr->sample_freq = 0;
489
490 if (opts->inherit_stat)
491 attr->inherit_stat = 1;
492
493 if (opts->sample_address) {
Arnaldo Carvalho de Melo7be5ebe2012-12-10 14:53:43 -0300494 perf_evsel__set_sample_bit(evsel, ADDR);
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200495 attr->mmap_data = track;
496 }
497
Jiri Olsa26d33022012-08-07 15:20:47 +0200498 if (opts->call_graph) {
Arnaldo Carvalho de Melo7be5ebe2012-12-10 14:53:43 -0300499 perf_evsel__set_sample_bit(evsel, CALLCHAIN);
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200500
Jiri Olsa26d33022012-08-07 15:20:47 +0200501 if (opts->call_graph == CALLCHAIN_DWARF) {
Arnaldo Carvalho de Melo7be5ebe2012-12-10 14:53:43 -0300502 perf_evsel__set_sample_bit(evsel, REGS_USER);
503 perf_evsel__set_sample_bit(evsel, STACK_USER);
Jiri Olsa26d33022012-08-07 15:20:47 +0200504 attr->sample_regs_user = PERF_REGS_MASK;
505 attr->sample_stack_user = opts->stack_dump_size;
506 attr->exclude_callchain_user = 1;
507 }
508 }
509
Namhyung Kime40ee742012-05-21 10:42:07 +0900510 if (perf_target__has_cpu(&opts->target))
Arnaldo Carvalho de Melo7be5ebe2012-12-10 14:53:43 -0300511 perf_evsel__set_sample_bit(evsel, CPU);
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200512
Andrew Vagin3e76ac72011-12-20 17:32:45 +0300513 if (opts->period)
Arnaldo Carvalho de Melo7be5ebe2012-12-10 14:53:43 -0300514 perf_evsel__set_sample_bit(evsel, PERIOD);
Andrew Vagin3e76ac72011-12-20 17:32:45 +0300515
Arnaldo Carvalho de Melo808e1222012-02-14 14:18:57 -0200516 if (!opts->sample_id_all_missing &&
Namhyung Kimd67356e2012-05-07 14:09:03 +0900517 (opts->sample_time || !opts->no_inherit ||
Namhyung Kimaa22dd42012-05-16 18:45:47 +0900518 perf_target__has_cpu(&opts->target)))
Arnaldo Carvalho de Melo7be5ebe2012-12-10 14:53:43 -0300519 perf_evsel__set_sample_bit(evsel, TIME);
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200520
521 if (opts->raw_samples) {
Arnaldo Carvalho de Melo7be5ebe2012-12-10 14:53:43 -0300522 perf_evsel__set_sample_bit(evsel, TIME);
523 perf_evsel__set_sample_bit(evsel, RAW);
524 perf_evsel__set_sample_bit(evsel, CPU);
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200525 }
526
527 if (opts->no_delay) {
528 attr->watermark = 0;
529 attr->wakeup_events = 1;
530 }
Roberto Agostino Vitillobdfebd82012-02-09 23:21:02 +0100531 if (opts->branch_stack) {
Arnaldo Carvalho de Melo7be5ebe2012-12-10 14:53:43 -0300532 perf_evsel__set_sample_bit(evsel, BRANCH_STACK);
Roberto Agostino Vitillobdfebd82012-02-09 23:21:02 +0100533 attr->branch_sample_type = opts->branch_stack;
534 }
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200535
536 attr->mmap = track;
537 attr->comm = track;
538
Jiri Olsa774cb492012-11-12 18:34:01 +0100539 /*
540 * XXX see the function comment above
541 *
542 * Disabling only independent events or group leaders,
543 * keeping group members enabled.
544 */
Namhyung Kim823254e2012-11-29 15:38:30 +0900545 if (perf_evsel__is_group_leader(evsel))
Jiri Olsa774cb492012-11-12 18:34:01 +0100546 attr->disabled = 1;
547
548 /*
549 * Setting enable_on_exec for independent events and
550 * group leaders for traced executed by perf.
551 */
Namhyung Kim823254e2012-11-29 15:38:30 +0900552 if (perf_target__none(&opts->target) && perf_evsel__is_group_leader(evsel))
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200553 attr->enable_on_exec = 1;
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200554}
555
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200556int perf_evsel__alloc_fd(struct perf_evsel *evsel, int ncpus, int nthreads)
557{
David Ahern4af4c952011-05-27 09:58:34 -0600558 int cpu, thread;
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200559 evsel->fd = xyarray__new(ncpus, nthreads, sizeof(int));
David Ahern4af4c952011-05-27 09:58:34 -0600560
561 if (evsel->fd) {
562 for (cpu = 0; cpu < ncpus; cpu++) {
563 for (thread = 0; thread < nthreads; thread++) {
564 FD(evsel, cpu, thread) = -1;
565 }
566 }
567 }
568
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200569 return evsel->fd != NULL ? 0 : -ENOMEM;
570}
571
Arnaldo Carvalho de Melo745cefc2012-09-26 15:07:39 -0300572int perf_evsel__set_filter(struct perf_evsel *evsel, int ncpus, int nthreads,
573 const char *filter)
574{
575 int cpu, thread;
576
577 for (cpu = 0; cpu < ncpus; cpu++) {
578 for (thread = 0; thread < nthreads; thread++) {
579 int fd = FD(evsel, cpu, thread),
580 err = ioctl(fd, PERF_EVENT_IOC_SET_FILTER, filter);
581
582 if (err)
583 return err;
584 }
585 }
586
587 return 0;
588}
589
Arnaldo Carvalho de Melo70db7532011-01-12 22:39:13 -0200590int perf_evsel__alloc_id(struct perf_evsel *evsel, int ncpus, int nthreads)
591{
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -0300592 evsel->sample_id = xyarray__new(ncpus, nthreads, sizeof(struct perf_sample_id));
593 if (evsel->sample_id == NULL)
594 return -ENOMEM;
595
596 evsel->id = zalloc(ncpus * nthreads * sizeof(u64));
597 if (evsel->id == NULL) {
598 xyarray__delete(evsel->sample_id);
599 evsel->sample_id = NULL;
600 return -ENOMEM;
601 }
602
603 return 0;
Arnaldo Carvalho de Melo70db7532011-01-12 22:39:13 -0200604}
605
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200606int perf_evsel__alloc_counts(struct perf_evsel *evsel, int ncpus)
607{
608 evsel->counts = zalloc((sizeof(*evsel->counts) +
609 (ncpus * sizeof(struct perf_counts_values))));
610 return evsel->counts != NULL ? 0 : -ENOMEM;
611}
612
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200613void perf_evsel__free_fd(struct perf_evsel *evsel)
614{
615 xyarray__delete(evsel->fd);
616 evsel->fd = NULL;
617}
618
Arnaldo Carvalho de Melo70db7532011-01-12 22:39:13 -0200619void perf_evsel__free_id(struct perf_evsel *evsel)
620{
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -0300621 xyarray__delete(evsel->sample_id);
622 evsel->sample_id = NULL;
623 free(evsel->id);
Arnaldo Carvalho de Melo70db7532011-01-12 22:39:13 -0200624 evsel->id = NULL;
625}
626
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200627void perf_evsel__close_fd(struct perf_evsel *evsel, int ncpus, int nthreads)
628{
629 int cpu, thread;
630
631 for (cpu = 0; cpu < ncpus; cpu++)
632 for (thread = 0; thread < nthreads; ++thread) {
633 close(FD(evsel, cpu, thread));
634 FD(evsel, cpu, thread) = -1;
635 }
636}
637
Arnaldo Carvalho de Meloef1d1af2011-01-18 21:41:45 -0200638void perf_evsel__exit(struct perf_evsel *evsel)
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200639{
640 assert(list_empty(&evsel->node));
641 xyarray__delete(evsel->fd);
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -0300642 xyarray__delete(evsel->sample_id);
643 free(evsel->id);
Arnaldo Carvalho de Meloef1d1af2011-01-18 21:41:45 -0200644}
645
646void perf_evsel__delete(struct perf_evsel *evsel)
647{
648 perf_evsel__exit(evsel);
Stephane Eranian023695d2011-02-14 11:20:01 +0200649 close_cgroup(evsel->cgrp);
Jiri Olsa6a4bb042012-08-08 12:22:36 +0200650 free(evsel->group_name);
Arnaldo Carvalho de Meloe48ffe22012-09-26 17:11:38 -0300651 if (evsel->tp_format)
Arnaldo Carvalho de Meloefd2b922012-09-18 11:21:50 -0300652 pevent_free_format(evsel->tp_format);
Stephane Eranianf0c55bc2011-02-16 15:10:01 +0200653 free(evsel->name);
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200654 free(evsel);
655}
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200656
657int __perf_evsel__read_on_cpu(struct perf_evsel *evsel,
658 int cpu, int thread, bool scale)
659{
660 struct perf_counts_values count;
661 size_t nv = scale ? 3 : 1;
662
663 if (FD(evsel, cpu, thread) < 0)
664 return -EINVAL;
665
Arnaldo Carvalho de Melo4eed11d2011-01-04 00:13:17 -0200666 if (evsel->counts == NULL && perf_evsel__alloc_counts(evsel, cpu + 1) < 0)
667 return -ENOMEM;
668
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200669 if (readn(FD(evsel, cpu, thread), &count, nv * sizeof(u64)) < 0)
670 return -errno;
671
672 if (scale) {
673 if (count.run == 0)
674 count.val = 0;
675 else if (count.run < count.ena)
676 count.val = (u64)((double)count.val * count.ena / count.run + 0.5);
677 } else
678 count.ena = count.run = 0;
679
680 evsel->counts->cpu[cpu] = count;
681 return 0;
682}
683
684int __perf_evsel__read(struct perf_evsel *evsel,
685 int ncpus, int nthreads, bool scale)
686{
687 size_t nv = scale ? 3 : 1;
688 int cpu, thread;
689 struct perf_counts_values *aggr = &evsel->counts->aggr, count;
690
Arnaldo Carvalho de Melo52bcd9942011-02-03 17:26:06 -0200691 aggr->val = aggr->ena = aggr->run = 0;
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200692
693 for (cpu = 0; cpu < ncpus; cpu++) {
694 for (thread = 0; thread < nthreads; thread++) {
695 if (FD(evsel, cpu, thread) < 0)
696 continue;
697
698 if (readn(FD(evsel, cpu, thread),
699 &count, nv * sizeof(u64)) < 0)
700 return -errno;
701
702 aggr->val += count.val;
703 if (scale) {
704 aggr->ena += count.ena;
705 aggr->run += count.run;
706 }
707 }
708 }
709
710 evsel->counts->scaled = 0;
711 if (scale) {
712 if (aggr->run == 0) {
713 evsel->counts->scaled = -1;
714 aggr->val = 0;
715 return 0;
716 }
717
718 if (aggr->run < aggr->ena) {
719 evsel->counts->scaled = 1;
720 aggr->val = (u64)((double)aggr->val * aggr->ena / aggr->run + 0.5);
721 }
722 } else
723 aggr->ena = aggr->run = 0;
724
725 return 0;
726}
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -0200727
Jiri Olsa6a4bb042012-08-08 12:22:36 +0200728static int get_group_fd(struct perf_evsel *evsel, int cpu, int thread)
729{
730 struct perf_evsel *leader = evsel->leader;
731 int fd;
732
Namhyung Kim823254e2012-11-29 15:38:30 +0900733 if (perf_evsel__is_group_leader(evsel))
Jiri Olsa6a4bb042012-08-08 12:22:36 +0200734 return -1;
735
736 /*
737 * Leader must be already processed/open,
738 * if not it's a bug.
739 */
740 BUG_ON(!leader->fd);
741
742 fd = FD(leader, cpu, thread);
743 BUG_ON(fd == -1);
744
745 return fd;
746}
747
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -0200748static int __perf_evsel__open(struct perf_evsel *evsel, struct cpu_map *cpus,
Jiri Olsa6a4bb042012-08-08 12:22:36 +0200749 struct thread_map *threads)
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -0200750{
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -0200751 int cpu, thread;
Stephane Eranian023695d2011-02-14 11:20:01 +0200752 unsigned long flags = 0;
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -0200753 int pid = -1, err;
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -0200754
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -0200755 if (evsel->fd == NULL &&
756 perf_evsel__alloc_fd(evsel, cpus->nr, threads->nr) < 0)
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -0200757 return -ENOMEM;
Arnaldo Carvalho de Melo4eed11d2011-01-04 00:13:17 -0200758
Stephane Eranian023695d2011-02-14 11:20:01 +0200759 if (evsel->cgrp) {
760 flags = PERF_FLAG_PID_CGROUP;
761 pid = evsel->cgrp->fd;
762 }
763
Arnaldo Carvalho de Melo86bd5e82011-01-03 23:09:46 -0200764 for (cpu = 0; cpu < cpus->nr; cpu++) {
Arnaldo Carvalho de Melo9d04f172011-01-12 00:08:18 -0200765
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -0200766 for (thread = 0; thread < threads->nr; thread++) {
Jiri Olsa6a4bb042012-08-08 12:22:36 +0200767 int group_fd;
Stephane Eranian023695d2011-02-14 11:20:01 +0200768
769 if (!evsel->cgrp)
770 pid = threads->map[thread];
771
Jiri Olsa6a4bb042012-08-08 12:22:36 +0200772 group_fd = get_group_fd(evsel, cpu, thread);
773
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -0200774 FD(evsel, cpu, thread) = sys_perf_event_open(&evsel->attr,
Stephane Eranian023695d2011-02-14 11:20:01 +0200775 pid,
Arnaldo Carvalho de Melof08199d2011-01-11 23:42:19 -0200776 cpus->map[cpu],
Stephane Eranian023695d2011-02-14 11:20:01 +0200777 group_fd, flags);
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -0200778 if (FD(evsel, cpu, thread) < 0) {
779 err = -errno;
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -0200780 goto out_close;
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -0200781 }
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -0200782 }
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -0200783 }
784
785 return 0;
786
787out_close:
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -0200788 do {
789 while (--thread >= 0) {
790 close(FD(evsel, cpu, thread));
791 FD(evsel, cpu, thread) = -1;
792 }
793 thread = threads->nr;
794 } while (--cpu >= 0);
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -0200795 return err;
796}
797
798void perf_evsel__close(struct perf_evsel *evsel, int ncpus, int nthreads)
799{
800 if (evsel->fd == NULL)
801 return;
802
803 perf_evsel__close_fd(evsel, ncpus, nthreads);
804 perf_evsel__free_fd(evsel);
805 evsel->fd = NULL;
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -0200806}
807
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -0200808static struct {
809 struct cpu_map map;
810 int cpus[1];
811} empty_cpu_map = {
812 .map.nr = 1,
813 .cpus = { -1, },
814};
815
816static struct {
817 struct thread_map map;
818 int threads[1];
819} empty_thread_map = {
820 .map.nr = 1,
821 .threads = { -1, },
822};
823
Arnaldo Carvalho de Melof08199d2011-01-11 23:42:19 -0200824int perf_evsel__open(struct perf_evsel *evsel, struct cpu_map *cpus,
Jiri Olsa6a4bb042012-08-08 12:22:36 +0200825 struct thread_map *threads)
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -0200826{
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -0200827 if (cpus == NULL) {
828 /* Work around old compiler warnings about strict aliasing */
829 cpus = &empty_cpu_map.map;
830 }
831
832 if (threads == NULL)
833 threads = &empty_thread_map.map;
834
Jiri Olsa6a4bb042012-08-08 12:22:36 +0200835 return __perf_evsel__open(evsel, cpus, threads);
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -0200836}
837
Arnaldo Carvalho de Melof08199d2011-01-11 23:42:19 -0200838int perf_evsel__open_per_cpu(struct perf_evsel *evsel,
Jiri Olsa6a4bb042012-08-08 12:22:36 +0200839 struct cpu_map *cpus)
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -0200840{
Jiri Olsa6a4bb042012-08-08 12:22:36 +0200841 return __perf_evsel__open(evsel, cpus, &empty_thread_map.map);
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -0200842}
843
Arnaldo Carvalho de Melof08199d2011-01-11 23:42:19 -0200844int perf_evsel__open_per_thread(struct perf_evsel *evsel,
Jiri Olsa6a4bb042012-08-08 12:22:36 +0200845 struct thread_map *threads)
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -0200846{
Jiri Olsa6a4bb042012-08-08 12:22:36 +0200847 return __perf_evsel__open(evsel, &empty_cpu_map.map, threads);
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -0200848}
Arnaldo Carvalho de Melo70082dd2011-01-12 17:03:24 -0200849
Arnaldo Carvalho de Melo0807d2d2012-09-26 12:48:18 -0300850static int perf_evsel__parse_id_sample(const struct perf_evsel *evsel,
851 const union perf_event *event,
852 struct perf_sample *sample)
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -0200853{
Arnaldo Carvalho de Melo0807d2d2012-09-26 12:48:18 -0300854 u64 type = evsel->attr.sample_type;
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -0200855 const u64 *array = event->sample.array;
Arnaldo Carvalho de Melo0807d2d2012-09-26 12:48:18 -0300856 bool swapped = evsel->needs_swap;
Jiri Olsa37073f92012-05-30 14:23:44 +0200857 union u64_swap u;
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -0200858
859 array += ((event->header.size -
860 sizeof(event->header)) / sizeof(u64)) - 1;
861
862 if (type & PERF_SAMPLE_CPU) {
Jiri Olsa37073f92012-05-30 14:23:44 +0200863 u.val64 = *array;
864 if (swapped) {
865 /* undo swap of u64, then swap on individual u32s */
866 u.val64 = bswap_64(u.val64);
867 u.val32[0] = bswap_32(u.val32[0]);
868 }
869
870 sample->cpu = u.val32[0];
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -0200871 array--;
872 }
873
874 if (type & PERF_SAMPLE_STREAM_ID) {
875 sample->stream_id = *array;
876 array--;
877 }
878
879 if (type & PERF_SAMPLE_ID) {
880 sample->id = *array;
881 array--;
882 }
883
884 if (type & PERF_SAMPLE_TIME) {
885 sample->time = *array;
886 array--;
887 }
888
889 if (type & PERF_SAMPLE_TID) {
Jiri Olsa37073f92012-05-30 14:23:44 +0200890 u.val64 = *array;
891 if (swapped) {
892 /* undo swap of u64, then swap on individual u32s */
893 u.val64 = bswap_64(u.val64);
894 u.val32[0] = bswap_32(u.val32[0]);
895 u.val32[1] = bswap_32(u.val32[1]);
896 }
897
898 sample->pid = u.val32[0];
899 sample->tid = u.val32[1];
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -0200900 }
901
902 return 0;
903}
904
Frederic Weisbecker98e1da92011-05-21 20:08:15 +0200905static bool sample_overlap(const union perf_event *event,
906 const void *offset, u64 size)
907{
908 const void *base = event;
909
910 if (offset + size > base + event->header.size)
911 return true;
912
913 return false;
914}
915
Arnaldo Carvalho de Meloa3f698f2012-08-02 12:23:46 -0300916int perf_evsel__parse_sample(struct perf_evsel *evsel, union perf_event *event,
Arnaldo Carvalho de Melo0807d2d2012-09-26 12:48:18 -0300917 struct perf_sample *data)
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -0200918{
Arnaldo Carvalho de Meloa3f698f2012-08-02 12:23:46 -0300919 u64 type = evsel->attr.sample_type;
Jiri Olsa0f6a3012012-08-07 15:20:45 +0200920 u64 regs_user = evsel->attr.sample_regs_user;
Arnaldo Carvalho de Melo0807d2d2012-09-26 12:48:18 -0300921 bool swapped = evsel->needs_swap;
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -0200922 const u64 *array;
923
David Ahern936be502011-09-06 09:12:26 -0600924 /*
925 * used for cross-endian analysis. See git commit 65014ab3
926 * for why this goofiness is needed.
927 */
Jiri Olsa6a11f922012-05-16 08:59:04 +0200928 union u64_swap u;
David Ahern936be502011-09-06 09:12:26 -0600929
Robert Richterf3bda2c2011-12-15 17:32:39 +0100930 memset(data, 0, sizeof(*data));
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -0200931 data->cpu = data->pid = data->tid = -1;
932 data->stream_id = data->id = data->time = -1ULL;
Naveen N. Raoa4a03fc2012-02-03 22:31:13 +0530933 data->period = 1;
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -0200934
935 if (event->header.type != PERF_RECORD_SAMPLE) {
Arnaldo Carvalho de Meloa3f698f2012-08-02 12:23:46 -0300936 if (!evsel->attr.sample_id_all)
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -0200937 return 0;
Arnaldo Carvalho de Melo0807d2d2012-09-26 12:48:18 -0300938 return perf_evsel__parse_id_sample(evsel, event, data);
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -0200939 }
940
941 array = event->sample.array;
942
Arnaldo Carvalho de Meloa3f698f2012-08-02 12:23:46 -0300943 if (evsel->sample_size + sizeof(event->header) > event->header.size)
Frederic Weisbeckera2854122011-05-21 19:33:04 +0200944 return -EFAULT;
945
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -0200946 if (type & PERF_SAMPLE_IP) {
947 data->ip = event->ip.ip;
948 array++;
949 }
950
951 if (type & PERF_SAMPLE_TID) {
David Ahern936be502011-09-06 09:12:26 -0600952 u.val64 = *array;
953 if (swapped) {
954 /* undo swap of u64, then swap on individual u32s */
955 u.val64 = bswap_64(u.val64);
956 u.val32[0] = bswap_32(u.val32[0]);
957 u.val32[1] = bswap_32(u.val32[1]);
958 }
959
960 data->pid = u.val32[0];
961 data->tid = u.val32[1];
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -0200962 array++;
963 }
964
965 if (type & PERF_SAMPLE_TIME) {
966 data->time = *array;
967 array++;
968 }
969
David Ahern7cec0922011-05-30 13:08:23 -0600970 data->addr = 0;
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -0200971 if (type & PERF_SAMPLE_ADDR) {
972 data->addr = *array;
973 array++;
974 }
975
976 data->id = -1ULL;
977 if (type & PERF_SAMPLE_ID) {
978 data->id = *array;
979 array++;
980 }
981
982 if (type & PERF_SAMPLE_STREAM_ID) {
983 data->stream_id = *array;
984 array++;
985 }
986
987 if (type & PERF_SAMPLE_CPU) {
David Ahern936be502011-09-06 09:12:26 -0600988
989 u.val64 = *array;
990 if (swapped) {
991 /* undo swap of u64, then swap on individual u32s */
992 u.val64 = bswap_64(u.val64);
993 u.val32[0] = bswap_32(u.val32[0]);
994 }
995
996 data->cpu = u.val32[0];
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -0200997 array++;
998 }
999
1000 if (type & PERF_SAMPLE_PERIOD) {
1001 data->period = *array;
1002 array++;
1003 }
1004
1005 if (type & PERF_SAMPLE_READ) {
Masanari Iidaf9d36992012-01-25 15:20:40 +01001006 fprintf(stderr, "PERF_SAMPLE_READ is unsupported for now\n");
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001007 return -1;
1008 }
1009
1010 if (type & PERF_SAMPLE_CALLCHAIN) {
Frederic Weisbecker98e1da92011-05-21 20:08:15 +02001011 if (sample_overlap(event, array, sizeof(data->callchain->nr)))
1012 return -EFAULT;
1013
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001014 data->callchain = (struct ip_callchain *)array;
Frederic Weisbecker98e1da92011-05-21 20:08:15 +02001015
1016 if (sample_overlap(event, array, data->callchain->nr))
1017 return -EFAULT;
1018
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001019 array += 1 + data->callchain->nr;
1020 }
1021
1022 if (type & PERF_SAMPLE_RAW) {
Jiri Olsa8e303f22011-09-29 17:05:08 +02001023 const u64 *pdata;
1024
David Ahern936be502011-09-06 09:12:26 -06001025 u.val64 = *array;
1026 if (WARN_ONCE(swapped,
1027 "Endianness of raw data not corrected!\n")) {
1028 /* undo swap of u64, then swap on individual u32s */
1029 u.val64 = bswap_64(u.val64);
1030 u.val32[0] = bswap_32(u.val32[0]);
1031 u.val32[1] = bswap_32(u.val32[1]);
1032 }
Frederic Weisbecker98e1da92011-05-21 20:08:15 +02001033
1034 if (sample_overlap(event, array, sizeof(u32)))
1035 return -EFAULT;
1036
David Ahern936be502011-09-06 09:12:26 -06001037 data->raw_size = u.val32[0];
Jiri Olsa8e303f22011-09-29 17:05:08 +02001038 pdata = (void *) array + sizeof(u32);
Frederic Weisbecker98e1da92011-05-21 20:08:15 +02001039
Jiri Olsa8e303f22011-09-29 17:05:08 +02001040 if (sample_overlap(event, pdata, data->raw_size))
Frederic Weisbecker98e1da92011-05-21 20:08:15 +02001041 return -EFAULT;
1042
Jiri Olsa8e303f22011-09-29 17:05:08 +02001043 data->raw_data = (void *) pdata;
Stephane Eranianfa30c962012-03-17 23:23:18 +01001044
1045 array = (void *)array + data->raw_size + sizeof(u32);
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001046 }
1047
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +01001048 if (type & PERF_SAMPLE_BRANCH_STACK) {
1049 u64 sz;
1050
1051 data->branch_stack = (struct branch_stack *)array;
1052 array++; /* nr */
1053
1054 sz = data->branch_stack->nr * sizeof(struct branch_entry);
1055 sz /= sizeof(u64);
1056 array += sz;
1057 }
Jiri Olsa0f6a3012012-08-07 15:20:45 +02001058
1059 if (type & PERF_SAMPLE_REGS_USER) {
1060 /* First u64 tells us if we have any regs in sample. */
1061 u64 avail = *array++;
1062
1063 if (avail) {
1064 data->user_regs.regs = (u64 *)array;
1065 array += hweight_long(regs_user);
1066 }
1067 }
1068
1069 if (type & PERF_SAMPLE_STACK_USER) {
1070 u64 size = *array++;
1071
1072 data->user_stack.offset = ((char *)(array - 1)
1073 - (char *) event);
1074
1075 if (!size) {
1076 data->user_stack.size = 0;
1077 } else {
1078 data->user_stack.data = (char *)array;
1079 array += size / sizeof(*array);
1080 data->user_stack.size = *array;
1081 }
1082 }
1083
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001084 return 0;
1085}
Andrew Vagin74eec262011-11-28 12:03:31 +03001086
1087int perf_event__synthesize_sample(union perf_event *event, u64 type,
1088 const struct perf_sample *sample,
1089 bool swapped)
1090{
1091 u64 *array;
1092
1093 /*
1094 * used for cross-endian analysis. See git commit 65014ab3
1095 * for why this goofiness is needed.
1096 */
Jiri Olsa6a11f922012-05-16 08:59:04 +02001097 union u64_swap u;
Andrew Vagin74eec262011-11-28 12:03:31 +03001098
1099 array = event->sample.array;
1100
1101 if (type & PERF_SAMPLE_IP) {
1102 event->ip.ip = sample->ip;
1103 array++;
1104 }
1105
1106 if (type & PERF_SAMPLE_TID) {
1107 u.val32[0] = sample->pid;
1108 u.val32[1] = sample->tid;
1109 if (swapped) {
1110 /*
Arnaldo Carvalho de Meloa3f698f2012-08-02 12:23:46 -03001111 * Inverse of what is done in perf_evsel__parse_sample
Andrew Vagin74eec262011-11-28 12:03:31 +03001112 */
1113 u.val32[0] = bswap_32(u.val32[0]);
1114 u.val32[1] = bswap_32(u.val32[1]);
1115 u.val64 = bswap_64(u.val64);
1116 }
1117
1118 *array = u.val64;
1119 array++;
1120 }
1121
1122 if (type & PERF_SAMPLE_TIME) {
1123 *array = sample->time;
1124 array++;
1125 }
1126
1127 if (type & PERF_SAMPLE_ADDR) {
1128 *array = sample->addr;
1129 array++;
1130 }
1131
1132 if (type & PERF_SAMPLE_ID) {
1133 *array = sample->id;
1134 array++;
1135 }
1136
1137 if (type & PERF_SAMPLE_STREAM_ID) {
1138 *array = sample->stream_id;
1139 array++;
1140 }
1141
1142 if (type & PERF_SAMPLE_CPU) {
1143 u.val32[0] = sample->cpu;
1144 if (swapped) {
1145 /*
Arnaldo Carvalho de Meloa3f698f2012-08-02 12:23:46 -03001146 * Inverse of what is done in perf_evsel__parse_sample
Andrew Vagin74eec262011-11-28 12:03:31 +03001147 */
1148 u.val32[0] = bswap_32(u.val32[0]);
1149 u.val64 = bswap_64(u.val64);
1150 }
1151 *array = u.val64;
1152 array++;
1153 }
1154
1155 if (type & PERF_SAMPLE_PERIOD) {
1156 *array = sample->period;
1157 array++;
1158 }
1159
1160 return 0;
1161}
Arnaldo Carvalho de Melo5555ded2012-09-11 19:24:23 -03001162
Arnaldo Carvalho de Meloefd2b922012-09-18 11:21:50 -03001163struct format_field *perf_evsel__field(struct perf_evsel *evsel, const char *name)
1164{
1165 return pevent_find_field(evsel->tp_format, name);
1166}
1167
Arnaldo Carvalho de Melo5d2074e2012-09-26 20:22:00 -03001168void *perf_evsel__rawptr(struct perf_evsel *evsel, struct perf_sample *sample,
Arnaldo Carvalho de Melo5555ded2012-09-11 19:24:23 -03001169 const char *name)
1170{
Arnaldo Carvalho de Meloefd2b922012-09-18 11:21:50 -03001171 struct format_field *field = perf_evsel__field(evsel, name);
Arnaldo Carvalho de Melo5555ded2012-09-11 19:24:23 -03001172 int offset;
1173
Arnaldo Carvalho de Meloefd2b922012-09-18 11:21:50 -03001174 if (!field)
1175 return NULL;
Arnaldo Carvalho de Melo5555ded2012-09-11 19:24:23 -03001176
1177 offset = field->offset;
1178
1179 if (field->flags & FIELD_IS_DYNAMIC) {
1180 offset = *(int *)(sample->raw_data + field->offset);
1181 offset &= 0xffff;
1182 }
1183
1184 return sample->raw_data + offset;
1185}
1186
1187u64 perf_evsel__intval(struct perf_evsel *evsel, struct perf_sample *sample,
1188 const char *name)
1189{
Arnaldo Carvalho de Meloefd2b922012-09-18 11:21:50 -03001190 struct format_field *field = perf_evsel__field(evsel, name);
Arnaldo Carvalho de Meloe6b6f672012-09-26 13:13:04 -03001191 void *ptr;
1192 u64 value;
Arnaldo Carvalho de Melo5555ded2012-09-11 19:24:23 -03001193
Arnaldo Carvalho de Meloefd2b922012-09-18 11:21:50 -03001194 if (!field)
1195 return 0;
Arnaldo Carvalho de Melo5555ded2012-09-11 19:24:23 -03001196
Arnaldo Carvalho de Meloe6b6f672012-09-26 13:13:04 -03001197 ptr = sample->raw_data + field->offset;
Arnaldo Carvalho de Melo5555ded2012-09-11 19:24:23 -03001198
Arnaldo Carvalho de Meloe6b6f672012-09-26 13:13:04 -03001199 switch (field->size) {
1200 case 1:
1201 return *(u8 *)ptr;
1202 case 2:
1203 value = *(u16 *)ptr;
1204 break;
1205 case 4:
1206 value = *(u32 *)ptr;
1207 break;
1208 case 8:
1209 value = *(u64 *)ptr;
1210 break;
1211 default:
1212 return 0;
1213 }
1214
1215 if (!evsel->needs_swap)
1216 return value;
1217
1218 switch (field->size) {
1219 case 2:
1220 return bswap_16(value);
1221 case 4:
1222 return bswap_32(value);
1223 case 8:
1224 return bswap_64(value);
1225 default:
1226 return 0;
1227 }
1228
1229 return 0;
Arnaldo Carvalho de Melo5555ded2012-09-11 19:24:23 -03001230}
Arnaldo Carvalho de Melo0698aed2012-12-10 18:17:08 -03001231
1232static int comma_fprintf(FILE *fp, bool *first, const char *fmt, ...)
1233{
1234 va_list args;
1235 int ret = 0;
1236
1237 if (!*first) {
1238 ret += fprintf(fp, ",");
1239 } else {
1240 ret += fprintf(fp, ":");
1241 *first = false;
1242 }
1243
1244 va_start(args, fmt);
1245 ret += vfprintf(fp, fmt, args);
1246 va_end(args);
1247 return ret;
1248}
1249
1250static int __if_fprintf(FILE *fp, bool *first, const char *field, u64 value)
1251{
1252 if (value == 0)
1253 return 0;
1254
1255 return comma_fprintf(fp, first, " %s: %" PRIu64, field, value);
1256}
1257
1258#define if_print(field) printed += __if_fprintf(fp, &first, #field, evsel->attr.field)
1259
1260int perf_evsel__fprintf(struct perf_evsel *evsel,
1261 struct perf_attr_details *details, FILE *fp)
1262{
1263 bool first = true;
1264 int printed = fprintf(fp, "%s", perf_evsel__name(evsel));
1265
1266 if (details->verbose || details->freq) {
1267 printed += comma_fprintf(fp, &first, " sample_freq=%" PRIu64,
1268 (u64)evsel->attr.sample_freq);
1269 }
1270
1271 if (details->verbose) {
1272 if_print(type);
1273 if_print(config);
1274 if_print(config1);
1275 if_print(config2);
1276 if_print(size);
1277 if_print(sample_type);
1278 if_print(read_format);
1279 if_print(disabled);
1280 if_print(inherit);
1281 if_print(pinned);
1282 if_print(exclusive);
1283 if_print(exclude_user);
1284 if_print(exclude_kernel);
1285 if_print(exclude_hv);
1286 if_print(exclude_idle);
1287 if_print(mmap);
1288 if_print(comm);
1289 if_print(freq);
1290 if_print(inherit_stat);
1291 if_print(enable_on_exec);
1292 if_print(task);
1293 if_print(watermark);
1294 if_print(precise_ip);
1295 if_print(mmap_data);
1296 if_print(sample_id_all);
1297 if_print(exclude_host);
1298 if_print(exclude_guest);
1299 if_print(__reserved_1);
1300 if_print(wakeup_events);
1301 if_print(bp_type);
1302 if_print(branch_sample_type);
1303 }
1304
1305 fputc('\n', fp);
1306 return ++printed;
1307}