blob: 00936ad29ff2db0b722d437f5f8767b4d525cc02 [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"
Jiri Olsa287e74a2012-06-28 23:18:49 +020021#include "../../../include/linux/hw_breakpoint.h"
Jiri Olsa26d33022012-08-07 15:20:47 +020022#include "../../include/linux/perf_event.h"
23#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 Meloef1d1af2011-01-18 21:41:45 -020053void perf_evsel__init(struct perf_evsel *evsel,
54 struct perf_event_attr *attr, int idx)
55{
56 evsel->idx = idx;
57 evsel->attr = *attr;
58 INIT_LIST_HEAD(&evsel->node);
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -030059 hists__init(&evsel->hists);
Arnaldo Carvalho de Melobde09462012-08-01 18:53:11 -030060 evsel->sample_size = __perf_evsel__sample_size(attr->sample_type);
Arnaldo Carvalho de Meloef1d1af2011-01-18 21:41:45 -020061}
62
Lin Ming23a2f3a2011-01-07 11:11:09 +080063struct perf_evsel *perf_evsel__new(struct perf_event_attr *attr, int idx)
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -020064{
65 struct perf_evsel *evsel = zalloc(sizeof(*evsel));
66
Arnaldo Carvalho de Meloef1d1af2011-01-18 21:41:45 -020067 if (evsel != NULL)
68 perf_evsel__init(evsel, attr, idx);
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -020069
70 return evsel;
71}
72
Arnaldo Carvalho de Meloefd2b922012-09-18 11:21:50 -030073static struct event_format *event_format__new(const char *sys, const char *name)
74{
75 int fd, n;
76 char *filename;
77 void *bf = NULL, *nbf;
78 size_t size = 0, alloc_size = 0;
79 struct event_format *format = NULL;
80
81 if (asprintf(&filename, "%s/%s/%s/format", tracing_events_path, sys, name) < 0)
82 goto out;
83
84 fd = open(filename, O_RDONLY);
85 if (fd < 0)
86 goto out_free_filename;
87
88 do {
89 if (size == alloc_size) {
90 alloc_size += BUFSIZ;
91 nbf = realloc(bf, alloc_size);
92 if (nbf == NULL)
93 goto out_free_bf;
94 bf = nbf;
95 }
96
97 n = read(fd, bf + size, BUFSIZ);
98 if (n < 0)
99 goto out_free_bf;
100 size += n;
101 } while (n > 0);
102
103 pevent_parse_format(&format, bf, size, sys);
104
105out_free_bf:
106 free(bf);
107 close(fd);
108out_free_filename:
109 free(filename);
110out:
111 return format;
112}
113
114struct perf_evsel *perf_evsel__newtp(const char *sys, const char *name, int idx)
115{
116 struct perf_evsel *evsel = zalloc(sizeof(*evsel));
117
118 if (evsel != NULL) {
119 struct perf_event_attr attr = {
120 .type = PERF_TYPE_TRACEPOINT,
121 };
122
123 evsel->tp_format = event_format__new(sys, name);
124 if (evsel->tp_format == NULL)
125 goto out_free;
126
127 attr.config = evsel->tp_format->id;
128 perf_evsel__init(evsel, &attr, idx);
129 evsel->name = evsel->tp_format->name;
130 }
131
132 return evsel;
133
134out_free:
135 free(evsel);
136 return NULL;
137}
138
Arnaldo Carvalho de Melo8ad70132012-09-06 13:11:18 -0300139const char *perf_evsel__hw_names[PERF_COUNT_HW_MAX] = {
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300140 "cycles",
141 "instructions",
142 "cache-references",
143 "cache-misses",
144 "branches",
145 "branch-misses",
146 "bus-cycles",
147 "stalled-cycles-frontend",
148 "stalled-cycles-backend",
149 "ref-cycles",
150};
151
Arnaldo Carvalho de Melodd4f5222012-06-13 15:52:42 -0300152static const char *__perf_evsel__hw_name(u64 config)
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300153{
154 if (config < PERF_COUNT_HW_MAX && perf_evsel__hw_names[config])
155 return perf_evsel__hw_names[config];
156
157 return "unknown-hardware";
158}
159
Arnaldo Carvalho de Melo27f18612012-06-11 13:33:09 -0300160static int perf_evsel__add_modifiers(struct perf_evsel *evsel, char *bf, size_t size)
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300161{
Arnaldo Carvalho de Melo27f18612012-06-11 13:33:09 -0300162 int colon = 0, r = 0;
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300163 struct perf_event_attr *attr = &evsel->attr;
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300164 bool exclude_guest_default = false;
165
166#define MOD_PRINT(context, mod) do { \
167 if (!attr->exclude_##context) { \
Arnaldo Carvalho de Melo27f18612012-06-11 13:33:09 -0300168 if (!colon) colon = ++r; \
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300169 r += scnprintf(bf + r, size - r, "%c", mod); \
170 } } while(0)
171
172 if (attr->exclude_kernel || attr->exclude_user || attr->exclude_hv) {
173 MOD_PRINT(kernel, 'k');
174 MOD_PRINT(user, 'u');
175 MOD_PRINT(hv, 'h');
176 exclude_guest_default = true;
177 }
178
179 if (attr->precise_ip) {
180 if (!colon)
Arnaldo Carvalho de Melo27f18612012-06-11 13:33:09 -0300181 colon = ++r;
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300182 r += scnprintf(bf + r, size - r, "%.*s", attr->precise_ip, "ppp");
183 exclude_guest_default = true;
184 }
185
186 if (attr->exclude_host || attr->exclude_guest == exclude_guest_default) {
187 MOD_PRINT(host, 'H');
188 MOD_PRINT(guest, 'G');
189 }
190#undef MOD_PRINT
191 if (colon)
Arnaldo Carvalho de Melo27f18612012-06-11 13:33:09 -0300192 bf[colon - 1] = ':';
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300193 return r;
194}
195
Arnaldo Carvalho de Melo27f18612012-06-11 13:33:09 -0300196static int perf_evsel__hw_name(struct perf_evsel *evsel, char *bf, size_t size)
197{
198 int r = scnprintf(bf, size, "%s", __perf_evsel__hw_name(evsel->attr.config));
199 return r + perf_evsel__add_modifiers(evsel, bf + r, size - r);
200}
201
Arnaldo Carvalho de Melo8ad70132012-09-06 13:11:18 -0300202const char *perf_evsel__sw_names[PERF_COUNT_SW_MAX] = {
Arnaldo Carvalho de Melo335c2f52012-06-11 14:36:20 -0300203 "cpu-clock",
204 "task-clock",
205 "page-faults",
206 "context-switches",
Arnaldo Carvalho de Melo8ad70132012-09-06 13:11:18 -0300207 "cpu-migrations",
Arnaldo Carvalho de Melo335c2f52012-06-11 14:36:20 -0300208 "minor-faults",
209 "major-faults",
210 "alignment-faults",
211 "emulation-faults",
212};
213
Arnaldo Carvalho de Melodd4f5222012-06-13 15:52:42 -0300214static const char *__perf_evsel__sw_name(u64 config)
Arnaldo Carvalho de Melo335c2f52012-06-11 14:36:20 -0300215{
216 if (config < PERF_COUNT_SW_MAX && perf_evsel__sw_names[config])
217 return perf_evsel__sw_names[config];
218 return "unknown-software";
219}
220
221static int perf_evsel__sw_name(struct perf_evsel *evsel, char *bf, size_t size)
222{
223 int r = scnprintf(bf, size, "%s", __perf_evsel__sw_name(evsel->attr.config));
224 return r + perf_evsel__add_modifiers(evsel, bf + r, size - r);
225}
226
Jiri Olsa287e74a2012-06-28 23:18:49 +0200227static int __perf_evsel__bp_name(char *bf, size_t size, u64 addr, u64 type)
228{
229 int r;
230
231 r = scnprintf(bf, size, "mem:0x%" PRIx64 ":", addr);
232
233 if (type & HW_BREAKPOINT_R)
234 r += scnprintf(bf + r, size - r, "r");
235
236 if (type & HW_BREAKPOINT_W)
237 r += scnprintf(bf + r, size - r, "w");
238
239 if (type & HW_BREAKPOINT_X)
240 r += scnprintf(bf + r, size - r, "x");
241
242 return r;
243}
244
245static int perf_evsel__bp_name(struct perf_evsel *evsel, char *bf, size_t size)
246{
247 struct perf_event_attr *attr = &evsel->attr;
248 int r = __perf_evsel__bp_name(bf, size, attr->bp_addr, attr->bp_type);
249 return r + perf_evsel__add_modifiers(evsel, bf + r, size - r);
250}
251
Arnaldo Carvalho de Melo0b668bc2012-06-11 14:08:07 -0300252const char *perf_evsel__hw_cache[PERF_COUNT_HW_CACHE_MAX]
253 [PERF_EVSEL__MAX_ALIASES] = {
254 { "L1-dcache", "l1-d", "l1d", "L1-data", },
255 { "L1-icache", "l1-i", "l1i", "L1-instruction", },
256 { "LLC", "L2", },
257 { "dTLB", "d-tlb", "Data-TLB", },
258 { "iTLB", "i-tlb", "Instruction-TLB", },
259 { "branch", "branches", "bpu", "btb", "bpc", },
260 { "node", },
261};
262
263const char *perf_evsel__hw_cache_op[PERF_COUNT_HW_CACHE_OP_MAX]
264 [PERF_EVSEL__MAX_ALIASES] = {
265 { "load", "loads", "read", },
266 { "store", "stores", "write", },
267 { "prefetch", "prefetches", "speculative-read", "speculative-load", },
268};
269
270const char *perf_evsel__hw_cache_result[PERF_COUNT_HW_CACHE_RESULT_MAX]
271 [PERF_EVSEL__MAX_ALIASES] = {
272 { "refs", "Reference", "ops", "access", },
273 { "misses", "miss", },
274};
275
276#define C(x) PERF_COUNT_HW_CACHE_##x
277#define CACHE_READ (1 << C(OP_READ))
278#define CACHE_WRITE (1 << C(OP_WRITE))
279#define CACHE_PREFETCH (1 << C(OP_PREFETCH))
280#define COP(x) (1 << x)
281
282/*
283 * cache operartion stat
284 * L1I : Read and prefetch only
285 * ITLB and BPU : Read-only
286 */
287static unsigned long perf_evsel__hw_cache_stat[C(MAX)] = {
288 [C(L1D)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
289 [C(L1I)] = (CACHE_READ | CACHE_PREFETCH),
290 [C(LL)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
291 [C(DTLB)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
292 [C(ITLB)] = (CACHE_READ),
293 [C(BPU)] = (CACHE_READ),
294 [C(NODE)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
295};
296
297bool perf_evsel__is_cache_op_valid(u8 type, u8 op)
298{
299 if (perf_evsel__hw_cache_stat[type] & COP(op))
300 return true; /* valid */
301 else
302 return false; /* invalid */
303}
304
305int __perf_evsel__hw_cache_type_op_res_name(u8 type, u8 op, u8 result,
306 char *bf, size_t size)
307{
308 if (result) {
309 return scnprintf(bf, size, "%s-%s-%s", perf_evsel__hw_cache[type][0],
310 perf_evsel__hw_cache_op[op][0],
311 perf_evsel__hw_cache_result[result][0]);
312 }
313
314 return scnprintf(bf, size, "%s-%s", perf_evsel__hw_cache[type][0],
315 perf_evsel__hw_cache_op[op][1]);
316}
317
Arnaldo Carvalho de Melodd4f5222012-06-13 15:52:42 -0300318static int __perf_evsel__hw_cache_name(u64 config, char *bf, size_t size)
Arnaldo Carvalho de Melo0b668bc2012-06-11 14:08:07 -0300319{
320 u8 op, result, type = (config >> 0) & 0xff;
321 const char *err = "unknown-ext-hardware-cache-type";
322
323 if (type > PERF_COUNT_HW_CACHE_MAX)
324 goto out_err;
325
326 op = (config >> 8) & 0xff;
327 err = "unknown-ext-hardware-cache-op";
328 if (op > PERF_COUNT_HW_CACHE_OP_MAX)
329 goto out_err;
330
331 result = (config >> 16) & 0xff;
332 err = "unknown-ext-hardware-cache-result";
333 if (result > PERF_COUNT_HW_CACHE_RESULT_MAX)
334 goto out_err;
335
336 err = "invalid-cache";
337 if (!perf_evsel__is_cache_op_valid(type, op))
338 goto out_err;
339
340 return __perf_evsel__hw_cache_type_op_res_name(type, op, result, bf, size);
341out_err:
342 return scnprintf(bf, size, "%s", err);
343}
344
345static int perf_evsel__hw_cache_name(struct perf_evsel *evsel, char *bf, size_t size)
346{
347 int ret = __perf_evsel__hw_cache_name(evsel->attr.config, bf, size);
348 return ret + perf_evsel__add_modifiers(evsel, bf + ret, size - ret);
349}
350
Arnaldo Carvalho de Melo6eef3d92012-06-13 11:53:37 -0300351static int perf_evsel__raw_name(struct perf_evsel *evsel, char *bf, size_t size)
352{
353 int ret = scnprintf(bf, size, "raw 0x%" PRIx64, evsel->attr.config);
354 return ret + perf_evsel__add_modifiers(evsel, bf + ret, size - ret);
355}
356
Arnaldo Carvalho de Melo7289f832012-06-12 12:34:58 -0300357const char *perf_evsel__name(struct perf_evsel *evsel)
Arnaldo Carvalho de Meloa4460832012-06-12 10:29:12 -0300358{
Arnaldo Carvalho de Melo7289f832012-06-12 12:34:58 -0300359 char bf[128];
Arnaldo Carvalho de Meloa4460832012-06-12 10:29:12 -0300360
Arnaldo Carvalho de Melo7289f832012-06-12 12:34:58 -0300361 if (evsel->name)
362 return evsel->name;
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300363
364 switch (evsel->attr.type) {
365 case PERF_TYPE_RAW:
Arnaldo Carvalho de Melo6eef3d92012-06-13 11:53:37 -0300366 perf_evsel__raw_name(evsel, bf, sizeof(bf));
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300367 break;
368
369 case PERF_TYPE_HARDWARE:
Arnaldo Carvalho de Melo7289f832012-06-12 12:34:58 -0300370 perf_evsel__hw_name(evsel, bf, sizeof(bf));
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300371 break;
Arnaldo Carvalho de Melo0b668bc2012-06-11 14:08:07 -0300372
373 case PERF_TYPE_HW_CACHE:
Arnaldo Carvalho de Melo7289f832012-06-12 12:34:58 -0300374 perf_evsel__hw_cache_name(evsel, bf, sizeof(bf));
Arnaldo Carvalho de Melo0b668bc2012-06-11 14:08:07 -0300375 break;
376
Arnaldo Carvalho de Melo335c2f52012-06-11 14:36:20 -0300377 case PERF_TYPE_SOFTWARE:
Arnaldo Carvalho de Melo7289f832012-06-12 12:34:58 -0300378 perf_evsel__sw_name(evsel, bf, sizeof(bf));
Arnaldo Carvalho de Melo335c2f52012-06-11 14:36:20 -0300379 break;
380
Arnaldo Carvalho de Meloa4460832012-06-12 10:29:12 -0300381 case PERF_TYPE_TRACEPOINT:
Arnaldo Carvalho de Melo7289f832012-06-12 12:34:58 -0300382 scnprintf(bf, sizeof(bf), "%s", "unknown tracepoint");
Arnaldo Carvalho de Meloa4460832012-06-12 10:29:12 -0300383 break;
384
Jiri Olsa287e74a2012-06-28 23:18:49 +0200385 case PERF_TYPE_BREAKPOINT:
386 perf_evsel__bp_name(evsel, bf, sizeof(bf));
387 break;
388
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300389 default:
Robert Richterca1b1452012-08-16 21:10:18 +0200390 scnprintf(bf, sizeof(bf), "unknown attr type: %d",
391 evsel->attr.type);
Arnaldo Carvalho de Meloa4460832012-06-12 10:29:12 -0300392 break;
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300393 }
394
Arnaldo Carvalho de Melo7289f832012-06-12 12:34:58 -0300395 evsel->name = strdup(bf);
396
397 return evsel->name ?: "unknown";
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300398}
399
Namhyung Kim5090c6a2012-03-16 17:42:20 +0900400void perf_evsel__config(struct perf_evsel *evsel, struct perf_record_opts *opts,
401 struct perf_evsel *first)
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200402{
403 struct perf_event_attr *attr = &evsel->attr;
404 int track = !evsel->idx; /* only the first counter needs these */
405
David Ahern5e1c81d2012-05-13 22:01:28 -0600406 attr->disabled = 1;
Arnaldo Carvalho de Melo808e1222012-02-14 14:18:57 -0200407 attr->sample_id_all = opts->sample_id_all_missing ? 0 : 1;
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200408 attr->inherit = !opts->no_inherit;
409 attr->read_format = PERF_FORMAT_TOTAL_TIME_ENABLED |
410 PERF_FORMAT_TOTAL_TIME_RUNNING |
411 PERF_FORMAT_ID;
412
413 attr->sample_type |= PERF_SAMPLE_IP | PERF_SAMPLE_TID;
414
415 /*
416 * We default some events to a 1 default interval. But keep
417 * it a weak assumption overridable by the user.
418 */
419 if (!attr->sample_period || (opts->user_freq != UINT_MAX &&
420 opts->user_interval != ULLONG_MAX)) {
421 if (opts->freq) {
422 attr->sample_type |= PERF_SAMPLE_PERIOD;
423 attr->freq = 1;
424 attr->sample_freq = opts->freq;
425 } else {
426 attr->sample_period = opts->default_interval;
427 }
428 }
429
430 if (opts->no_samples)
431 attr->sample_freq = 0;
432
433 if (opts->inherit_stat)
434 attr->inherit_stat = 1;
435
436 if (opts->sample_address) {
437 attr->sample_type |= PERF_SAMPLE_ADDR;
438 attr->mmap_data = track;
439 }
440
Jiri Olsa26d33022012-08-07 15:20:47 +0200441 if (opts->call_graph) {
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200442 attr->sample_type |= PERF_SAMPLE_CALLCHAIN;
443
Jiri Olsa26d33022012-08-07 15:20:47 +0200444 if (opts->call_graph == CALLCHAIN_DWARF) {
445 attr->sample_type |= PERF_SAMPLE_REGS_USER |
446 PERF_SAMPLE_STACK_USER;
447 attr->sample_regs_user = PERF_REGS_MASK;
448 attr->sample_stack_user = opts->stack_dump_size;
449 attr->exclude_callchain_user = 1;
450 }
451 }
452
Namhyung Kime40ee742012-05-21 10:42:07 +0900453 if (perf_target__has_cpu(&opts->target))
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200454 attr->sample_type |= PERF_SAMPLE_CPU;
455
Andrew Vagin3e76ac72011-12-20 17:32:45 +0300456 if (opts->period)
457 attr->sample_type |= PERF_SAMPLE_PERIOD;
458
Arnaldo Carvalho de Melo808e1222012-02-14 14:18:57 -0200459 if (!opts->sample_id_all_missing &&
Namhyung Kimd67356e2012-05-07 14:09:03 +0900460 (opts->sample_time || !opts->no_inherit ||
Namhyung Kimaa22dd42012-05-16 18:45:47 +0900461 perf_target__has_cpu(&opts->target)))
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200462 attr->sample_type |= PERF_SAMPLE_TIME;
463
464 if (opts->raw_samples) {
465 attr->sample_type |= PERF_SAMPLE_TIME;
466 attr->sample_type |= PERF_SAMPLE_RAW;
467 attr->sample_type |= PERF_SAMPLE_CPU;
468 }
469
470 if (opts->no_delay) {
471 attr->watermark = 0;
472 attr->wakeup_events = 1;
473 }
Roberto Agostino Vitillobdfebd82012-02-09 23:21:02 +0100474 if (opts->branch_stack) {
475 attr->sample_type |= PERF_SAMPLE_BRANCH_STACK;
476 attr->branch_sample_type = opts->branch_stack;
477 }
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200478
479 attr->mmap = track;
480 attr->comm = track;
481
Namhyung Kimd67356e2012-05-07 14:09:03 +0900482 if (perf_target__none(&opts->target) &&
483 (!opts->group || evsel == first)) {
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200484 attr->enable_on_exec = 1;
485 }
486}
487
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200488int perf_evsel__alloc_fd(struct perf_evsel *evsel, int ncpus, int nthreads)
489{
David Ahern4af4c952011-05-27 09:58:34 -0600490 int cpu, thread;
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200491 evsel->fd = xyarray__new(ncpus, nthreads, sizeof(int));
David Ahern4af4c952011-05-27 09:58:34 -0600492
493 if (evsel->fd) {
494 for (cpu = 0; cpu < ncpus; cpu++) {
495 for (thread = 0; thread < nthreads; thread++) {
496 FD(evsel, cpu, thread) = -1;
497 }
498 }
499 }
500
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200501 return evsel->fd != NULL ? 0 : -ENOMEM;
502}
503
Arnaldo Carvalho de Melo70db7532011-01-12 22:39:13 -0200504int perf_evsel__alloc_id(struct perf_evsel *evsel, int ncpus, int nthreads)
505{
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -0300506 evsel->sample_id = xyarray__new(ncpus, nthreads, sizeof(struct perf_sample_id));
507 if (evsel->sample_id == NULL)
508 return -ENOMEM;
509
510 evsel->id = zalloc(ncpus * nthreads * sizeof(u64));
511 if (evsel->id == NULL) {
512 xyarray__delete(evsel->sample_id);
513 evsel->sample_id = NULL;
514 return -ENOMEM;
515 }
516
517 return 0;
Arnaldo Carvalho de Melo70db7532011-01-12 22:39:13 -0200518}
519
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200520int perf_evsel__alloc_counts(struct perf_evsel *evsel, int ncpus)
521{
522 evsel->counts = zalloc((sizeof(*evsel->counts) +
523 (ncpus * sizeof(struct perf_counts_values))));
524 return evsel->counts != NULL ? 0 : -ENOMEM;
525}
526
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200527void perf_evsel__free_fd(struct perf_evsel *evsel)
528{
529 xyarray__delete(evsel->fd);
530 evsel->fd = NULL;
531}
532
Arnaldo Carvalho de Melo70db7532011-01-12 22:39:13 -0200533void perf_evsel__free_id(struct perf_evsel *evsel)
534{
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -0300535 xyarray__delete(evsel->sample_id);
536 evsel->sample_id = NULL;
537 free(evsel->id);
Arnaldo Carvalho de Melo70db7532011-01-12 22:39:13 -0200538 evsel->id = NULL;
539}
540
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200541void perf_evsel__close_fd(struct perf_evsel *evsel, int ncpus, int nthreads)
542{
543 int cpu, thread;
544
545 for (cpu = 0; cpu < ncpus; cpu++)
546 for (thread = 0; thread < nthreads; ++thread) {
547 close(FD(evsel, cpu, thread));
548 FD(evsel, cpu, thread) = -1;
549 }
550}
551
Arnaldo Carvalho de Meloef1d1af2011-01-18 21:41:45 -0200552void perf_evsel__exit(struct perf_evsel *evsel)
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200553{
554 assert(list_empty(&evsel->node));
555 xyarray__delete(evsel->fd);
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -0300556 xyarray__delete(evsel->sample_id);
557 free(evsel->id);
Arnaldo Carvalho de Meloef1d1af2011-01-18 21:41:45 -0200558}
559
560void perf_evsel__delete(struct perf_evsel *evsel)
561{
562 perf_evsel__exit(evsel);
Stephane Eranian023695d2011-02-14 11:20:01 +0200563 close_cgroup(evsel->cgrp);
Jiri Olsa6a4bb042012-08-08 12:22:36 +0200564 free(evsel->group_name);
Arnaldo Carvalho de Meloefd2b922012-09-18 11:21:50 -0300565 if (evsel->tp_format && evsel->name == evsel->tp_format->name) {
566 evsel->name = NULL;
567 pevent_free_format(evsel->tp_format);
568 }
Stephane Eranianf0c55bc2011-02-16 15:10:01 +0200569 free(evsel->name);
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200570 free(evsel);
571}
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200572
573int __perf_evsel__read_on_cpu(struct perf_evsel *evsel,
574 int cpu, int thread, bool scale)
575{
576 struct perf_counts_values count;
577 size_t nv = scale ? 3 : 1;
578
579 if (FD(evsel, cpu, thread) < 0)
580 return -EINVAL;
581
Arnaldo Carvalho de Melo4eed11d2011-01-04 00:13:17 -0200582 if (evsel->counts == NULL && perf_evsel__alloc_counts(evsel, cpu + 1) < 0)
583 return -ENOMEM;
584
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200585 if (readn(FD(evsel, cpu, thread), &count, nv * sizeof(u64)) < 0)
586 return -errno;
587
588 if (scale) {
589 if (count.run == 0)
590 count.val = 0;
591 else if (count.run < count.ena)
592 count.val = (u64)((double)count.val * count.ena / count.run + 0.5);
593 } else
594 count.ena = count.run = 0;
595
596 evsel->counts->cpu[cpu] = count;
597 return 0;
598}
599
600int __perf_evsel__read(struct perf_evsel *evsel,
601 int ncpus, int nthreads, bool scale)
602{
603 size_t nv = scale ? 3 : 1;
604 int cpu, thread;
605 struct perf_counts_values *aggr = &evsel->counts->aggr, count;
606
Arnaldo Carvalho de Melo52bcd9942011-02-03 17:26:06 -0200607 aggr->val = aggr->ena = aggr->run = 0;
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200608
609 for (cpu = 0; cpu < ncpus; cpu++) {
610 for (thread = 0; thread < nthreads; thread++) {
611 if (FD(evsel, cpu, thread) < 0)
612 continue;
613
614 if (readn(FD(evsel, cpu, thread),
615 &count, nv * sizeof(u64)) < 0)
616 return -errno;
617
618 aggr->val += count.val;
619 if (scale) {
620 aggr->ena += count.ena;
621 aggr->run += count.run;
622 }
623 }
624 }
625
626 evsel->counts->scaled = 0;
627 if (scale) {
628 if (aggr->run == 0) {
629 evsel->counts->scaled = -1;
630 aggr->val = 0;
631 return 0;
632 }
633
634 if (aggr->run < aggr->ena) {
635 evsel->counts->scaled = 1;
636 aggr->val = (u64)((double)aggr->val * aggr->ena / aggr->run + 0.5);
637 }
638 } else
639 aggr->ena = aggr->run = 0;
640
641 return 0;
642}
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -0200643
Jiri Olsa6a4bb042012-08-08 12:22:36 +0200644static int get_group_fd(struct perf_evsel *evsel, int cpu, int thread)
645{
646 struct perf_evsel *leader = evsel->leader;
647 int fd;
648
649 if (!leader)
650 return -1;
651
652 /*
653 * Leader must be already processed/open,
654 * if not it's a bug.
655 */
656 BUG_ON(!leader->fd);
657
658 fd = FD(leader, cpu, thread);
659 BUG_ON(fd == -1);
660
661 return fd;
662}
663
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -0200664static int __perf_evsel__open(struct perf_evsel *evsel, struct cpu_map *cpus,
Jiri Olsa6a4bb042012-08-08 12:22:36 +0200665 struct thread_map *threads)
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -0200666{
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -0200667 int cpu, thread;
Stephane Eranian023695d2011-02-14 11:20:01 +0200668 unsigned long flags = 0;
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -0200669 int pid = -1, err;
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -0200670
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -0200671 if (evsel->fd == NULL &&
672 perf_evsel__alloc_fd(evsel, cpus->nr, threads->nr) < 0)
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -0200673 return -ENOMEM;
Arnaldo Carvalho de Melo4eed11d2011-01-04 00:13:17 -0200674
Stephane Eranian023695d2011-02-14 11:20:01 +0200675 if (evsel->cgrp) {
676 flags = PERF_FLAG_PID_CGROUP;
677 pid = evsel->cgrp->fd;
678 }
679
Arnaldo Carvalho de Melo86bd5e82011-01-03 23:09:46 -0200680 for (cpu = 0; cpu < cpus->nr; cpu++) {
Arnaldo Carvalho de Melo9d04f172011-01-12 00:08:18 -0200681
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -0200682 for (thread = 0; thread < threads->nr; thread++) {
Jiri Olsa6a4bb042012-08-08 12:22:36 +0200683 int group_fd;
Stephane Eranian023695d2011-02-14 11:20:01 +0200684
685 if (!evsel->cgrp)
686 pid = threads->map[thread];
687
Jiri Olsa6a4bb042012-08-08 12:22:36 +0200688 group_fd = get_group_fd(evsel, cpu, thread);
689
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -0200690 FD(evsel, cpu, thread) = sys_perf_event_open(&evsel->attr,
Stephane Eranian023695d2011-02-14 11:20:01 +0200691 pid,
Arnaldo Carvalho de Melof08199d2011-01-11 23:42:19 -0200692 cpus->map[cpu],
Stephane Eranian023695d2011-02-14 11:20:01 +0200693 group_fd, flags);
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -0200694 if (FD(evsel, cpu, thread) < 0) {
695 err = -errno;
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -0200696 goto out_close;
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -0200697 }
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -0200698 }
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -0200699 }
700
701 return 0;
702
703out_close:
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -0200704 do {
705 while (--thread >= 0) {
706 close(FD(evsel, cpu, thread));
707 FD(evsel, cpu, thread) = -1;
708 }
709 thread = threads->nr;
710 } while (--cpu >= 0);
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -0200711 return err;
712}
713
714void perf_evsel__close(struct perf_evsel *evsel, int ncpus, int nthreads)
715{
716 if (evsel->fd == NULL)
717 return;
718
719 perf_evsel__close_fd(evsel, ncpus, nthreads);
720 perf_evsel__free_fd(evsel);
721 evsel->fd = NULL;
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -0200722}
723
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -0200724static struct {
725 struct cpu_map map;
726 int cpus[1];
727} empty_cpu_map = {
728 .map.nr = 1,
729 .cpus = { -1, },
730};
731
732static struct {
733 struct thread_map map;
734 int threads[1];
735} empty_thread_map = {
736 .map.nr = 1,
737 .threads = { -1, },
738};
739
Arnaldo Carvalho de Melof08199d2011-01-11 23:42:19 -0200740int perf_evsel__open(struct perf_evsel *evsel, struct cpu_map *cpus,
Jiri Olsa6a4bb042012-08-08 12:22:36 +0200741 struct thread_map *threads)
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -0200742{
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -0200743 if (cpus == NULL) {
744 /* Work around old compiler warnings about strict aliasing */
745 cpus = &empty_cpu_map.map;
746 }
747
748 if (threads == NULL)
749 threads = &empty_thread_map.map;
750
Jiri Olsa6a4bb042012-08-08 12:22:36 +0200751 return __perf_evsel__open(evsel, cpus, threads);
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -0200752}
753
Arnaldo Carvalho de Melof08199d2011-01-11 23:42:19 -0200754int perf_evsel__open_per_cpu(struct perf_evsel *evsel,
Jiri Olsa6a4bb042012-08-08 12:22:36 +0200755 struct cpu_map *cpus)
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -0200756{
Jiri Olsa6a4bb042012-08-08 12:22:36 +0200757 return __perf_evsel__open(evsel, cpus, &empty_thread_map.map);
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -0200758}
759
Arnaldo Carvalho de Melof08199d2011-01-11 23:42:19 -0200760int perf_evsel__open_per_thread(struct perf_evsel *evsel,
Jiri Olsa6a4bb042012-08-08 12:22:36 +0200761 struct thread_map *threads)
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -0200762{
Jiri Olsa6a4bb042012-08-08 12:22:36 +0200763 return __perf_evsel__open(evsel, &empty_cpu_map.map, threads);
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -0200764}
Arnaldo Carvalho de Melo70082dd2011-01-12 17:03:24 -0200765
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200766static int perf_event__parse_id_sample(const union perf_event *event, u64 type,
Jiri Olsa37073f92012-05-30 14:23:44 +0200767 struct perf_sample *sample,
768 bool swapped)
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -0200769{
770 const u64 *array = event->sample.array;
Jiri Olsa37073f92012-05-30 14:23:44 +0200771 union u64_swap u;
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -0200772
773 array += ((event->header.size -
774 sizeof(event->header)) / sizeof(u64)) - 1;
775
776 if (type & PERF_SAMPLE_CPU) {
Jiri Olsa37073f92012-05-30 14:23:44 +0200777 u.val64 = *array;
778 if (swapped) {
779 /* undo swap of u64, then swap on individual u32s */
780 u.val64 = bswap_64(u.val64);
781 u.val32[0] = bswap_32(u.val32[0]);
782 }
783
784 sample->cpu = u.val32[0];
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -0200785 array--;
786 }
787
788 if (type & PERF_SAMPLE_STREAM_ID) {
789 sample->stream_id = *array;
790 array--;
791 }
792
793 if (type & PERF_SAMPLE_ID) {
794 sample->id = *array;
795 array--;
796 }
797
798 if (type & PERF_SAMPLE_TIME) {
799 sample->time = *array;
800 array--;
801 }
802
803 if (type & PERF_SAMPLE_TID) {
Jiri Olsa37073f92012-05-30 14:23:44 +0200804 u.val64 = *array;
805 if (swapped) {
806 /* undo swap of u64, then swap on individual u32s */
807 u.val64 = bswap_64(u.val64);
808 u.val32[0] = bswap_32(u.val32[0]);
809 u.val32[1] = bswap_32(u.val32[1]);
810 }
811
812 sample->pid = u.val32[0];
813 sample->tid = u.val32[1];
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -0200814 }
815
816 return 0;
817}
818
Frederic Weisbecker98e1da92011-05-21 20:08:15 +0200819static bool sample_overlap(const union perf_event *event,
820 const void *offset, u64 size)
821{
822 const void *base = event;
823
824 if (offset + size > base + event->header.size)
825 return true;
826
827 return false;
828}
829
Arnaldo Carvalho de Meloa3f698f2012-08-02 12:23:46 -0300830int perf_evsel__parse_sample(struct perf_evsel *evsel, union perf_event *event,
David Ahern936be502011-09-06 09:12:26 -0600831 struct perf_sample *data, bool swapped)
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -0200832{
Arnaldo Carvalho de Meloa3f698f2012-08-02 12:23:46 -0300833 u64 type = evsel->attr.sample_type;
Jiri Olsa0f6a3012012-08-07 15:20:45 +0200834 u64 regs_user = evsel->attr.sample_regs_user;
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -0200835 const u64 *array;
836
David Ahern936be502011-09-06 09:12:26 -0600837 /*
838 * used for cross-endian analysis. See git commit 65014ab3
839 * for why this goofiness is needed.
840 */
Jiri Olsa6a11f922012-05-16 08:59:04 +0200841 union u64_swap u;
David Ahern936be502011-09-06 09:12:26 -0600842
Robert Richterf3bda2c2011-12-15 17:32:39 +0100843 memset(data, 0, sizeof(*data));
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -0200844 data->cpu = data->pid = data->tid = -1;
845 data->stream_id = data->id = data->time = -1ULL;
Naveen N. Raoa4a03fc2012-02-03 22:31:13 +0530846 data->period = 1;
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -0200847
848 if (event->header.type != PERF_RECORD_SAMPLE) {
Arnaldo Carvalho de Meloa3f698f2012-08-02 12:23:46 -0300849 if (!evsel->attr.sample_id_all)
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -0200850 return 0;
Jiri Olsa37073f92012-05-30 14:23:44 +0200851 return perf_event__parse_id_sample(event, type, data, swapped);
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -0200852 }
853
854 array = event->sample.array;
855
Arnaldo Carvalho de Meloa3f698f2012-08-02 12:23:46 -0300856 if (evsel->sample_size + sizeof(event->header) > event->header.size)
Frederic Weisbeckera2854122011-05-21 19:33:04 +0200857 return -EFAULT;
858
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -0200859 if (type & PERF_SAMPLE_IP) {
860 data->ip = event->ip.ip;
861 array++;
862 }
863
864 if (type & PERF_SAMPLE_TID) {
David Ahern936be502011-09-06 09:12:26 -0600865 u.val64 = *array;
866 if (swapped) {
867 /* undo swap of u64, then swap on individual u32s */
868 u.val64 = bswap_64(u.val64);
869 u.val32[0] = bswap_32(u.val32[0]);
870 u.val32[1] = bswap_32(u.val32[1]);
871 }
872
873 data->pid = u.val32[0];
874 data->tid = u.val32[1];
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -0200875 array++;
876 }
877
878 if (type & PERF_SAMPLE_TIME) {
879 data->time = *array;
880 array++;
881 }
882
David Ahern7cec0922011-05-30 13:08:23 -0600883 data->addr = 0;
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -0200884 if (type & PERF_SAMPLE_ADDR) {
885 data->addr = *array;
886 array++;
887 }
888
889 data->id = -1ULL;
890 if (type & PERF_SAMPLE_ID) {
891 data->id = *array;
892 array++;
893 }
894
895 if (type & PERF_SAMPLE_STREAM_ID) {
896 data->stream_id = *array;
897 array++;
898 }
899
900 if (type & PERF_SAMPLE_CPU) {
David Ahern936be502011-09-06 09:12:26 -0600901
902 u.val64 = *array;
903 if (swapped) {
904 /* undo swap of u64, then swap on individual u32s */
905 u.val64 = bswap_64(u.val64);
906 u.val32[0] = bswap_32(u.val32[0]);
907 }
908
909 data->cpu = u.val32[0];
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -0200910 array++;
911 }
912
913 if (type & PERF_SAMPLE_PERIOD) {
914 data->period = *array;
915 array++;
916 }
917
918 if (type & PERF_SAMPLE_READ) {
Masanari Iidaf9d36992012-01-25 15:20:40 +0100919 fprintf(stderr, "PERF_SAMPLE_READ is unsupported for now\n");
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -0200920 return -1;
921 }
922
923 if (type & PERF_SAMPLE_CALLCHAIN) {
Frederic Weisbecker98e1da92011-05-21 20:08:15 +0200924 if (sample_overlap(event, array, sizeof(data->callchain->nr)))
925 return -EFAULT;
926
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -0200927 data->callchain = (struct ip_callchain *)array;
Frederic Weisbecker98e1da92011-05-21 20:08:15 +0200928
929 if (sample_overlap(event, array, data->callchain->nr))
930 return -EFAULT;
931
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -0200932 array += 1 + data->callchain->nr;
933 }
934
935 if (type & PERF_SAMPLE_RAW) {
Jiri Olsa8e303f22011-09-29 17:05:08 +0200936 const u64 *pdata;
937
David Ahern936be502011-09-06 09:12:26 -0600938 u.val64 = *array;
939 if (WARN_ONCE(swapped,
940 "Endianness of raw data not corrected!\n")) {
941 /* undo swap of u64, then swap on individual u32s */
942 u.val64 = bswap_64(u.val64);
943 u.val32[0] = bswap_32(u.val32[0]);
944 u.val32[1] = bswap_32(u.val32[1]);
945 }
Frederic Weisbecker98e1da92011-05-21 20:08:15 +0200946
947 if (sample_overlap(event, array, sizeof(u32)))
948 return -EFAULT;
949
David Ahern936be502011-09-06 09:12:26 -0600950 data->raw_size = u.val32[0];
Jiri Olsa8e303f22011-09-29 17:05:08 +0200951 pdata = (void *) array + sizeof(u32);
Frederic Weisbecker98e1da92011-05-21 20:08:15 +0200952
Jiri Olsa8e303f22011-09-29 17:05:08 +0200953 if (sample_overlap(event, pdata, data->raw_size))
Frederic Weisbecker98e1da92011-05-21 20:08:15 +0200954 return -EFAULT;
955
Jiri Olsa8e303f22011-09-29 17:05:08 +0200956 data->raw_data = (void *) pdata;
Stephane Eranianfa30c962012-03-17 23:23:18 +0100957
958 array = (void *)array + data->raw_size + sizeof(u32);
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -0200959 }
960
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100961 if (type & PERF_SAMPLE_BRANCH_STACK) {
962 u64 sz;
963
964 data->branch_stack = (struct branch_stack *)array;
965 array++; /* nr */
966
967 sz = data->branch_stack->nr * sizeof(struct branch_entry);
968 sz /= sizeof(u64);
969 array += sz;
970 }
Jiri Olsa0f6a3012012-08-07 15:20:45 +0200971
972 if (type & PERF_SAMPLE_REGS_USER) {
973 /* First u64 tells us if we have any regs in sample. */
974 u64 avail = *array++;
975
976 if (avail) {
977 data->user_regs.regs = (u64 *)array;
978 array += hweight_long(regs_user);
979 }
980 }
981
982 if (type & PERF_SAMPLE_STACK_USER) {
983 u64 size = *array++;
984
985 data->user_stack.offset = ((char *)(array - 1)
986 - (char *) event);
987
988 if (!size) {
989 data->user_stack.size = 0;
990 } else {
991 data->user_stack.data = (char *)array;
992 array += size / sizeof(*array);
993 data->user_stack.size = *array;
994 }
995 }
996
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -0200997 return 0;
998}
Andrew Vagin74eec262011-11-28 12:03:31 +0300999
1000int perf_event__synthesize_sample(union perf_event *event, u64 type,
1001 const struct perf_sample *sample,
1002 bool swapped)
1003{
1004 u64 *array;
1005
1006 /*
1007 * used for cross-endian analysis. See git commit 65014ab3
1008 * for why this goofiness is needed.
1009 */
Jiri Olsa6a11f922012-05-16 08:59:04 +02001010 union u64_swap u;
Andrew Vagin74eec262011-11-28 12:03:31 +03001011
1012 array = event->sample.array;
1013
1014 if (type & PERF_SAMPLE_IP) {
1015 event->ip.ip = sample->ip;
1016 array++;
1017 }
1018
1019 if (type & PERF_SAMPLE_TID) {
1020 u.val32[0] = sample->pid;
1021 u.val32[1] = sample->tid;
1022 if (swapped) {
1023 /*
Arnaldo Carvalho de Meloa3f698f2012-08-02 12:23:46 -03001024 * Inverse of what is done in perf_evsel__parse_sample
Andrew Vagin74eec262011-11-28 12:03:31 +03001025 */
1026 u.val32[0] = bswap_32(u.val32[0]);
1027 u.val32[1] = bswap_32(u.val32[1]);
1028 u.val64 = bswap_64(u.val64);
1029 }
1030
1031 *array = u.val64;
1032 array++;
1033 }
1034
1035 if (type & PERF_SAMPLE_TIME) {
1036 *array = sample->time;
1037 array++;
1038 }
1039
1040 if (type & PERF_SAMPLE_ADDR) {
1041 *array = sample->addr;
1042 array++;
1043 }
1044
1045 if (type & PERF_SAMPLE_ID) {
1046 *array = sample->id;
1047 array++;
1048 }
1049
1050 if (type & PERF_SAMPLE_STREAM_ID) {
1051 *array = sample->stream_id;
1052 array++;
1053 }
1054
1055 if (type & PERF_SAMPLE_CPU) {
1056 u.val32[0] = sample->cpu;
1057 if (swapped) {
1058 /*
Arnaldo Carvalho de Meloa3f698f2012-08-02 12:23:46 -03001059 * Inverse of what is done in perf_evsel__parse_sample
Andrew Vagin74eec262011-11-28 12:03:31 +03001060 */
1061 u.val32[0] = bswap_32(u.val32[0]);
1062 u.val64 = bswap_64(u.val64);
1063 }
1064 *array = u.val64;
1065 array++;
1066 }
1067
1068 if (type & PERF_SAMPLE_PERIOD) {
1069 *array = sample->period;
1070 array++;
1071 }
1072
1073 return 0;
1074}
Arnaldo Carvalho de Melo5555ded2012-09-11 19:24:23 -03001075
Arnaldo Carvalho de Meloefd2b922012-09-18 11:21:50 -03001076struct format_field *perf_evsel__field(struct perf_evsel *evsel, const char *name)
1077{
1078 return pevent_find_field(evsel->tp_format, name);
1079}
1080
Arnaldo Carvalho de Melo5555ded2012-09-11 19:24:23 -03001081char *perf_evsel__strval(struct perf_evsel *evsel, struct perf_sample *sample,
1082 const char *name)
1083{
Arnaldo Carvalho de Meloefd2b922012-09-18 11:21:50 -03001084 struct format_field *field = perf_evsel__field(evsel, name);
Arnaldo Carvalho de Melo5555ded2012-09-11 19:24:23 -03001085 int offset;
1086
Arnaldo Carvalho de Meloefd2b922012-09-18 11:21:50 -03001087 if (!field)
1088 return NULL;
Arnaldo Carvalho de Melo5555ded2012-09-11 19:24:23 -03001089
1090 offset = field->offset;
1091
1092 if (field->flags & FIELD_IS_DYNAMIC) {
1093 offset = *(int *)(sample->raw_data + field->offset);
1094 offset &= 0xffff;
1095 }
1096
1097 return sample->raw_data + offset;
1098}
1099
1100u64 perf_evsel__intval(struct perf_evsel *evsel, struct perf_sample *sample,
1101 const char *name)
1102{
Arnaldo Carvalho de Meloefd2b922012-09-18 11:21:50 -03001103 struct format_field *field = perf_evsel__field(evsel, name);
Arnaldo Carvalho de Melo5555ded2012-09-11 19:24:23 -03001104 u64 val;
1105
Arnaldo Carvalho de Meloefd2b922012-09-18 11:21:50 -03001106 if (!field)
1107 return 0;
Arnaldo Carvalho de Melo5555ded2012-09-11 19:24:23 -03001108
1109 val = pevent_read_number(evsel->tp_format->pevent,
1110 sample->raw_data + field->offset, field->size);
1111 return val;
1112
1113}