blob: 6022daaa7417b0efd9c0e0795d8565fb4ef82a63 [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 = {
Arnaldo Carvalho de Melo0b80f8b32012-09-26 12:28:26 -0300120 .type = PERF_TYPE_TRACEPOINT,
121 .sample_type = (PERF_SAMPLE_RAW | PERF_SAMPLE_TIME |
122 PERF_SAMPLE_CPU | PERF_SAMPLE_PERIOD),
Arnaldo Carvalho de Meloefd2b922012-09-18 11:21:50 -0300123 };
124
125 evsel->tp_format = event_format__new(sys, name);
126 if (evsel->tp_format == NULL)
127 goto out_free;
128
Arnaldo Carvalho de Melo0b80f8b32012-09-26 12:28:26 -0300129 event_attr_init(&attr);
Arnaldo Carvalho de Meloefd2b922012-09-18 11:21:50 -0300130 attr.config = evsel->tp_format->id;
Arnaldo Carvalho de Melo0b80f8b32012-09-26 12:28:26 -0300131 attr.sample_period = 1;
Arnaldo Carvalho de Meloefd2b922012-09-18 11:21:50 -0300132 perf_evsel__init(evsel, &attr, idx);
133 evsel->name = evsel->tp_format->name;
134 }
135
136 return evsel;
137
138out_free:
139 free(evsel);
140 return NULL;
141}
142
Arnaldo Carvalho de Melo8ad70132012-09-06 13:11:18 -0300143const char *perf_evsel__hw_names[PERF_COUNT_HW_MAX] = {
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300144 "cycles",
145 "instructions",
146 "cache-references",
147 "cache-misses",
148 "branches",
149 "branch-misses",
150 "bus-cycles",
151 "stalled-cycles-frontend",
152 "stalled-cycles-backend",
153 "ref-cycles",
154};
155
Arnaldo Carvalho de Melodd4f5222012-06-13 15:52:42 -0300156static const char *__perf_evsel__hw_name(u64 config)
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300157{
158 if (config < PERF_COUNT_HW_MAX && perf_evsel__hw_names[config])
159 return perf_evsel__hw_names[config];
160
161 return "unknown-hardware";
162}
163
Arnaldo Carvalho de Melo27f18612012-06-11 13:33:09 -0300164static int perf_evsel__add_modifiers(struct perf_evsel *evsel, char *bf, size_t size)
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300165{
Arnaldo Carvalho de Melo27f18612012-06-11 13:33:09 -0300166 int colon = 0, r = 0;
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300167 struct perf_event_attr *attr = &evsel->attr;
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300168 bool exclude_guest_default = false;
169
170#define MOD_PRINT(context, mod) do { \
171 if (!attr->exclude_##context) { \
Arnaldo Carvalho de Melo27f18612012-06-11 13:33:09 -0300172 if (!colon) colon = ++r; \
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300173 r += scnprintf(bf + r, size - r, "%c", mod); \
174 } } while(0)
175
176 if (attr->exclude_kernel || attr->exclude_user || attr->exclude_hv) {
177 MOD_PRINT(kernel, 'k');
178 MOD_PRINT(user, 'u');
179 MOD_PRINT(hv, 'h');
180 exclude_guest_default = true;
181 }
182
183 if (attr->precise_ip) {
184 if (!colon)
Arnaldo Carvalho de Melo27f18612012-06-11 13:33:09 -0300185 colon = ++r;
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300186 r += scnprintf(bf + r, size - r, "%.*s", attr->precise_ip, "ppp");
187 exclude_guest_default = true;
188 }
189
190 if (attr->exclude_host || attr->exclude_guest == exclude_guest_default) {
191 MOD_PRINT(host, 'H');
192 MOD_PRINT(guest, 'G');
193 }
194#undef MOD_PRINT
195 if (colon)
Arnaldo Carvalho de Melo27f18612012-06-11 13:33:09 -0300196 bf[colon - 1] = ':';
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300197 return r;
198}
199
Arnaldo Carvalho de Melo27f18612012-06-11 13:33:09 -0300200static int perf_evsel__hw_name(struct perf_evsel *evsel, char *bf, size_t size)
201{
202 int r = scnprintf(bf, size, "%s", __perf_evsel__hw_name(evsel->attr.config));
203 return r + perf_evsel__add_modifiers(evsel, bf + r, size - r);
204}
205
Arnaldo Carvalho de Melo8ad70132012-09-06 13:11:18 -0300206const char *perf_evsel__sw_names[PERF_COUNT_SW_MAX] = {
Arnaldo Carvalho de Melo335c2f52012-06-11 14:36:20 -0300207 "cpu-clock",
208 "task-clock",
209 "page-faults",
210 "context-switches",
Arnaldo Carvalho de Melo8ad70132012-09-06 13:11:18 -0300211 "cpu-migrations",
Arnaldo Carvalho de Melo335c2f52012-06-11 14:36:20 -0300212 "minor-faults",
213 "major-faults",
214 "alignment-faults",
215 "emulation-faults",
216};
217
Arnaldo Carvalho de Melodd4f5222012-06-13 15:52:42 -0300218static const char *__perf_evsel__sw_name(u64 config)
Arnaldo Carvalho de Melo335c2f52012-06-11 14:36:20 -0300219{
220 if (config < PERF_COUNT_SW_MAX && perf_evsel__sw_names[config])
221 return perf_evsel__sw_names[config];
222 return "unknown-software";
223}
224
225static int perf_evsel__sw_name(struct perf_evsel *evsel, char *bf, size_t size)
226{
227 int r = scnprintf(bf, size, "%s", __perf_evsel__sw_name(evsel->attr.config));
228 return r + perf_evsel__add_modifiers(evsel, bf + r, size - r);
229}
230
Jiri Olsa287e74a2012-06-28 23:18:49 +0200231static int __perf_evsel__bp_name(char *bf, size_t size, u64 addr, u64 type)
232{
233 int r;
234
235 r = scnprintf(bf, size, "mem:0x%" PRIx64 ":", addr);
236
237 if (type & HW_BREAKPOINT_R)
238 r += scnprintf(bf + r, size - r, "r");
239
240 if (type & HW_BREAKPOINT_W)
241 r += scnprintf(bf + r, size - r, "w");
242
243 if (type & HW_BREAKPOINT_X)
244 r += scnprintf(bf + r, size - r, "x");
245
246 return r;
247}
248
249static int perf_evsel__bp_name(struct perf_evsel *evsel, char *bf, size_t size)
250{
251 struct perf_event_attr *attr = &evsel->attr;
252 int r = __perf_evsel__bp_name(bf, size, attr->bp_addr, attr->bp_type);
253 return r + perf_evsel__add_modifiers(evsel, bf + r, size - r);
254}
255
Arnaldo Carvalho de Melo0b668bc2012-06-11 14:08:07 -0300256const char *perf_evsel__hw_cache[PERF_COUNT_HW_CACHE_MAX]
257 [PERF_EVSEL__MAX_ALIASES] = {
258 { "L1-dcache", "l1-d", "l1d", "L1-data", },
259 { "L1-icache", "l1-i", "l1i", "L1-instruction", },
260 { "LLC", "L2", },
261 { "dTLB", "d-tlb", "Data-TLB", },
262 { "iTLB", "i-tlb", "Instruction-TLB", },
263 { "branch", "branches", "bpu", "btb", "bpc", },
264 { "node", },
265};
266
267const char *perf_evsel__hw_cache_op[PERF_COUNT_HW_CACHE_OP_MAX]
268 [PERF_EVSEL__MAX_ALIASES] = {
269 { "load", "loads", "read", },
270 { "store", "stores", "write", },
271 { "prefetch", "prefetches", "speculative-read", "speculative-load", },
272};
273
274const char *perf_evsel__hw_cache_result[PERF_COUNT_HW_CACHE_RESULT_MAX]
275 [PERF_EVSEL__MAX_ALIASES] = {
276 { "refs", "Reference", "ops", "access", },
277 { "misses", "miss", },
278};
279
280#define C(x) PERF_COUNT_HW_CACHE_##x
281#define CACHE_READ (1 << C(OP_READ))
282#define CACHE_WRITE (1 << C(OP_WRITE))
283#define CACHE_PREFETCH (1 << C(OP_PREFETCH))
284#define COP(x) (1 << x)
285
286/*
287 * cache operartion stat
288 * L1I : Read and prefetch only
289 * ITLB and BPU : Read-only
290 */
291static unsigned long perf_evsel__hw_cache_stat[C(MAX)] = {
292 [C(L1D)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
293 [C(L1I)] = (CACHE_READ | CACHE_PREFETCH),
294 [C(LL)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
295 [C(DTLB)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
296 [C(ITLB)] = (CACHE_READ),
297 [C(BPU)] = (CACHE_READ),
298 [C(NODE)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
299};
300
301bool perf_evsel__is_cache_op_valid(u8 type, u8 op)
302{
303 if (perf_evsel__hw_cache_stat[type] & COP(op))
304 return true; /* valid */
305 else
306 return false; /* invalid */
307}
308
309int __perf_evsel__hw_cache_type_op_res_name(u8 type, u8 op, u8 result,
310 char *bf, size_t size)
311{
312 if (result) {
313 return scnprintf(bf, size, "%s-%s-%s", perf_evsel__hw_cache[type][0],
314 perf_evsel__hw_cache_op[op][0],
315 perf_evsel__hw_cache_result[result][0]);
316 }
317
318 return scnprintf(bf, size, "%s-%s", perf_evsel__hw_cache[type][0],
319 perf_evsel__hw_cache_op[op][1]);
320}
321
Arnaldo Carvalho de Melodd4f5222012-06-13 15:52:42 -0300322static int __perf_evsel__hw_cache_name(u64 config, char *bf, size_t size)
Arnaldo Carvalho de Melo0b668bc2012-06-11 14:08:07 -0300323{
324 u8 op, result, type = (config >> 0) & 0xff;
325 const char *err = "unknown-ext-hardware-cache-type";
326
327 if (type > PERF_COUNT_HW_CACHE_MAX)
328 goto out_err;
329
330 op = (config >> 8) & 0xff;
331 err = "unknown-ext-hardware-cache-op";
332 if (op > PERF_COUNT_HW_CACHE_OP_MAX)
333 goto out_err;
334
335 result = (config >> 16) & 0xff;
336 err = "unknown-ext-hardware-cache-result";
337 if (result > PERF_COUNT_HW_CACHE_RESULT_MAX)
338 goto out_err;
339
340 err = "invalid-cache";
341 if (!perf_evsel__is_cache_op_valid(type, op))
342 goto out_err;
343
344 return __perf_evsel__hw_cache_type_op_res_name(type, op, result, bf, size);
345out_err:
346 return scnprintf(bf, size, "%s", err);
347}
348
349static int perf_evsel__hw_cache_name(struct perf_evsel *evsel, char *bf, size_t size)
350{
351 int ret = __perf_evsel__hw_cache_name(evsel->attr.config, bf, size);
352 return ret + perf_evsel__add_modifiers(evsel, bf + ret, size - ret);
353}
354
Arnaldo Carvalho de Melo6eef3d92012-06-13 11:53:37 -0300355static int perf_evsel__raw_name(struct perf_evsel *evsel, char *bf, size_t size)
356{
357 int ret = scnprintf(bf, size, "raw 0x%" PRIx64, evsel->attr.config);
358 return ret + perf_evsel__add_modifiers(evsel, bf + ret, size - ret);
359}
360
Arnaldo Carvalho de Melo7289f832012-06-12 12:34:58 -0300361const char *perf_evsel__name(struct perf_evsel *evsel)
Arnaldo Carvalho de Meloa4460832012-06-12 10:29:12 -0300362{
Arnaldo Carvalho de Melo7289f832012-06-12 12:34:58 -0300363 char bf[128];
Arnaldo Carvalho de Meloa4460832012-06-12 10:29:12 -0300364
Arnaldo Carvalho de Melo7289f832012-06-12 12:34:58 -0300365 if (evsel->name)
366 return evsel->name;
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300367
368 switch (evsel->attr.type) {
369 case PERF_TYPE_RAW:
Arnaldo Carvalho de Melo6eef3d92012-06-13 11:53:37 -0300370 perf_evsel__raw_name(evsel, bf, sizeof(bf));
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300371 break;
372
373 case PERF_TYPE_HARDWARE:
Arnaldo Carvalho de Melo7289f832012-06-12 12:34:58 -0300374 perf_evsel__hw_name(evsel, bf, sizeof(bf));
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300375 break;
Arnaldo Carvalho de Melo0b668bc2012-06-11 14:08:07 -0300376
377 case PERF_TYPE_HW_CACHE:
Arnaldo Carvalho de Melo7289f832012-06-12 12:34:58 -0300378 perf_evsel__hw_cache_name(evsel, bf, sizeof(bf));
Arnaldo Carvalho de Melo0b668bc2012-06-11 14:08:07 -0300379 break;
380
Arnaldo Carvalho de Melo335c2f52012-06-11 14:36:20 -0300381 case PERF_TYPE_SOFTWARE:
Arnaldo Carvalho de Melo7289f832012-06-12 12:34:58 -0300382 perf_evsel__sw_name(evsel, bf, sizeof(bf));
Arnaldo Carvalho de Melo335c2f52012-06-11 14:36:20 -0300383 break;
384
Arnaldo Carvalho de Meloa4460832012-06-12 10:29:12 -0300385 case PERF_TYPE_TRACEPOINT:
Arnaldo Carvalho de Melo7289f832012-06-12 12:34:58 -0300386 scnprintf(bf, sizeof(bf), "%s", "unknown tracepoint");
Arnaldo Carvalho de Meloa4460832012-06-12 10:29:12 -0300387 break;
388
Jiri Olsa287e74a2012-06-28 23:18:49 +0200389 case PERF_TYPE_BREAKPOINT:
390 perf_evsel__bp_name(evsel, bf, sizeof(bf));
391 break;
392
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300393 default:
Robert Richterca1b1452012-08-16 21:10:18 +0200394 scnprintf(bf, sizeof(bf), "unknown attr type: %d",
395 evsel->attr.type);
Arnaldo Carvalho de Meloa4460832012-06-12 10:29:12 -0300396 break;
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300397 }
398
Arnaldo Carvalho de Melo7289f832012-06-12 12:34:58 -0300399 evsel->name = strdup(bf);
400
401 return evsel->name ?: "unknown";
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300402}
403
Namhyung Kim5090c6a2012-03-16 17:42:20 +0900404void perf_evsel__config(struct perf_evsel *evsel, struct perf_record_opts *opts,
405 struct perf_evsel *first)
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200406{
407 struct perf_event_attr *attr = &evsel->attr;
408 int track = !evsel->idx; /* only the first counter needs these */
409
David Ahern5e1c81d2012-05-13 22:01:28 -0600410 attr->disabled = 1;
Arnaldo Carvalho de Melo808e1222012-02-14 14:18:57 -0200411 attr->sample_id_all = opts->sample_id_all_missing ? 0 : 1;
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200412 attr->inherit = !opts->no_inherit;
413 attr->read_format = PERF_FORMAT_TOTAL_TIME_ENABLED |
414 PERF_FORMAT_TOTAL_TIME_RUNNING |
415 PERF_FORMAT_ID;
416
417 attr->sample_type |= PERF_SAMPLE_IP | PERF_SAMPLE_TID;
418
419 /*
420 * We default some events to a 1 default interval. But keep
421 * it a weak assumption overridable by the user.
422 */
423 if (!attr->sample_period || (opts->user_freq != UINT_MAX &&
424 opts->user_interval != ULLONG_MAX)) {
425 if (opts->freq) {
426 attr->sample_type |= PERF_SAMPLE_PERIOD;
427 attr->freq = 1;
428 attr->sample_freq = opts->freq;
429 } else {
430 attr->sample_period = opts->default_interval;
431 }
432 }
433
434 if (opts->no_samples)
435 attr->sample_freq = 0;
436
437 if (opts->inherit_stat)
438 attr->inherit_stat = 1;
439
440 if (opts->sample_address) {
441 attr->sample_type |= PERF_SAMPLE_ADDR;
442 attr->mmap_data = track;
443 }
444
Jiri Olsa26d33022012-08-07 15:20:47 +0200445 if (opts->call_graph) {
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200446 attr->sample_type |= PERF_SAMPLE_CALLCHAIN;
447
Jiri Olsa26d33022012-08-07 15:20:47 +0200448 if (opts->call_graph == CALLCHAIN_DWARF) {
449 attr->sample_type |= PERF_SAMPLE_REGS_USER |
450 PERF_SAMPLE_STACK_USER;
451 attr->sample_regs_user = PERF_REGS_MASK;
452 attr->sample_stack_user = opts->stack_dump_size;
453 attr->exclude_callchain_user = 1;
454 }
455 }
456
Namhyung Kime40ee742012-05-21 10:42:07 +0900457 if (perf_target__has_cpu(&opts->target))
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200458 attr->sample_type |= PERF_SAMPLE_CPU;
459
Andrew Vagin3e76ac72011-12-20 17:32:45 +0300460 if (opts->period)
461 attr->sample_type |= PERF_SAMPLE_PERIOD;
462
Arnaldo Carvalho de Melo808e1222012-02-14 14:18:57 -0200463 if (!opts->sample_id_all_missing &&
Namhyung Kimd67356e2012-05-07 14:09:03 +0900464 (opts->sample_time || !opts->no_inherit ||
Namhyung Kimaa22dd42012-05-16 18:45:47 +0900465 perf_target__has_cpu(&opts->target)))
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200466 attr->sample_type |= PERF_SAMPLE_TIME;
467
468 if (opts->raw_samples) {
469 attr->sample_type |= PERF_SAMPLE_TIME;
470 attr->sample_type |= PERF_SAMPLE_RAW;
471 attr->sample_type |= PERF_SAMPLE_CPU;
472 }
473
474 if (opts->no_delay) {
475 attr->watermark = 0;
476 attr->wakeup_events = 1;
477 }
Roberto Agostino Vitillobdfebd82012-02-09 23:21:02 +0100478 if (opts->branch_stack) {
479 attr->sample_type |= PERF_SAMPLE_BRANCH_STACK;
480 attr->branch_sample_type = opts->branch_stack;
481 }
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200482
483 attr->mmap = track;
484 attr->comm = track;
485
Namhyung Kimd67356e2012-05-07 14:09:03 +0900486 if (perf_target__none(&opts->target) &&
487 (!opts->group || evsel == first)) {
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200488 attr->enable_on_exec = 1;
489 }
490}
491
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200492int perf_evsel__alloc_fd(struct perf_evsel *evsel, int ncpus, int nthreads)
493{
David Ahern4af4c952011-05-27 09:58:34 -0600494 int cpu, thread;
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200495 evsel->fd = xyarray__new(ncpus, nthreads, sizeof(int));
David Ahern4af4c952011-05-27 09:58:34 -0600496
497 if (evsel->fd) {
498 for (cpu = 0; cpu < ncpus; cpu++) {
499 for (thread = 0; thread < nthreads; thread++) {
500 FD(evsel, cpu, thread) = -1;
501 }
502 }
503 }
504
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200505 return evsel->fd != NULL ? 0 : -ENOMEM;
506}
507
Arnaldo Carvalho de Melo745cefc2012-09-26 15:07:39 -0300508int perf_evsel__set_filter(struct perf_evsel *evsel, int ncpus, int nthreads,
509 const char *filter)
510{
511 int cpu, thread;
512
513 for (cpu = 0; cpu < ncpus; cpu++) {
514 for (thread = 0; thread < nthreads; thread++) {
515 int fd = FD(evsel, cpu, thread),
516 err = ioctl(fd, PERF_EVENT_IOC_SET_FILTER, filter);
517
518 if (err)
519 return err;
520 }
521 }
522
523 return 0;
524}
525
Arnaldo Carvalho de Melo70db7532011-01-12 22:39:13 -0200526int perf_evsel__alloc_id(struct perf_evsel *evsel, int ncpus, int nthreads)
527{
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -0300528 evsel->sample_id = xyarray__new(ncpus, nthreads, sizeof(struct perf_sample_id));
529 if (evsel->sample_id == NULL)
530 return -ENOMEM;
531
532 evsel->id = zalloc(ncpus * nthreads * sizeof(u64));
533 if (evsel->id == NULL) {
534 xyarray__delete(evsel->sample_id);
535 evsel->sample_id = NULL;
536 return -ENOMEM;
537 }
538
539 return 0;
Arnaldo Carvalho de Melo70db7532011-01-12 22:39:13 -0200540}
541
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200542int perf_evsel__alloc_counts(struct perf_evsel *evsel, int ncpus)
543{
544 evsel->counts = zalloc((sizeof(*evsel->counts) +
545 (ncpus * sizeof(struct perf_counts_values))));
546 return evsel->counts != NULL ? 0 : -ENOMEM;
547}
548
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200549void perf_evsel__free_fd(struct perf_evsel *evsel)
550{
551 xyarray__delete(evsel->fd);
552 evsel->fd = NULL;
553}
554
Arnaldo Carvalho de Melo70db7532011-01-12 22:39:13 -0200555void perf_evsel__free_id(struct perf_evsel *evsel)
556{
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -0300557 xyarray__delete(evsel->sample_id);
558 evsel->sample_id = NULL;
559 free(evsel->id);
Arnaldo Carvalho de Melo70db7532011-01-12 22:39:13 -0200560 evsel->id = NULL;
561}
562
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200563void perf_evsel__close_fd(struct perf_evsel *evsel, int ncpus, int nthreads)
564{
565 int cpu, thread;
566
567 for (cpu = 0; cpu < ncpus; cpu++)
568 for (thread = 0; thread < nthreads; ++thread) {
569 close(FD(evsel, cpu, thread));
570 FD(evsel, cpu, thread) = -1;
571 }
572}
573
Arnaldo Carvalho de Meloef1d1af2011-01-18 21:41:45 -0200574void perf_evsel__exit(struct perf_evsel *evsel)
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200575{
576 assert(list_empty(&evsel->node));
577 xyarray__delete(evsel->fd);
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -0300578 xyarray__delete(evsel->sample_id);
579 free(evsel->id);
Arnaldo Carvalho de Meloef1d1af2011-01-18 21:41:45 -0200580}
581
582void perf_evsel__delete(struct perf_evsel *evsel)
583{
584 perf_evsel__exit(evsel);
Stephane Eranian023695d2011-02-14 11:20:01 +0200585 close_cgroup(evsel->cgrp);
Jiri Olsa6a4bb042012-08-08 12:22:36 +0200586 free(evsel->group_name);
Arnaldo Carvalho de Meloefd2b922012-09-18 11:21:50 -0300587 if (evsel->tp_format && evsel->name == evsel->tp_format->name) {
588 evsel->name = NULL;
589 pevent_free_format(evsel->tp_format);
590 }
Stephane Eranianf0c55bc2011-02-16 15:10:01 +0200591 free(evsel->name);
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200592 free(evsel);
593}
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200594
595int __perf_evsel__read_on_cpu(struct perf_evsel *evsel,
596 int cpu, int thread, bool scale)
597{
598 struct perf_counts_values count;
599 size_t nv = scale ? 3 : 1;
600
601 if (FD(evsel, cpu, thread) < 0)
602 return -EINVAL;
603
Arnaldo Carvalho de Melo4eed11d2011-01-04 00:13:17 -0200604 if (evsel->counts == NULL && perf_evsel__alloc_counts(evsel, cpu + 1) < 0)
605 return -ENOMEM;
606
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200607 if (readn(FD(evsel, cpu, thread), &count, nv * sizeof(u64)) < 0)
608 return -errno;
609
610 if (scale) {
611 if (count.run == 0)
612 count.val = 0;
613 else if (count.run < count.ena)
614 count.val = (u64)((double)count.val * count.ena / count.run + 0.5);
615 } else
616 count.ena = count.run = 0;
617
618 evsel->counts->cpu[cpu] = count;
619 return 0;
620}
621
622int __perf_evsel__read(struct perf_evsel *evsel,
623 int ncpus, int nthreads, bool scale)
624{
625 size_t nv = scale ? 3 : 1;
626 int cpu, thread;
627 struct perf_counts_values *aggr = &evsel->counts->aggr, count;
628
Arnaldo Carvalho de Melo52bcd9942011-02-03 17:26:06 -0200629 aggr->val = aggr->ena = aggr->run = 0;
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200630
631 for (cpu = 0; cpu < ncpus; cpu++) {
632 for (thread = 0; thread < nthreads; thread++) {
633 if (FD(evsel, cpu, thread) < 0)
634 continue;
635
636 if (readn(FD(evsel, cpu, thread),
637 &count, nv * sizeof(u64)) < 0)
638 return -errno;
639
640 aggr->val += count.val;
641 if (scale) {
642 aggr->ena += count.ena;
643 aggr->run += count.run;
644 }
645 }
646 }
647
648 evsel->counts->scaled = 0;
649 if (scale) {
650 if (aggr->run == 0) {
651 evsel->counts->scaled = -1;
652 aggr->val = 0;
653 return 0;
654 }
655
656 if (aggr->run < aggr->ena) {
657 evsel->counts->scaled = 1;
658 aggr->val = (u64)((double)aggr->val * aggr->ena / aggr->run + 0.5);
659 }
660 } else
661 aggr->ena = aggr->run = 0;
662
663 return 0;
664}
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -0200665
Jiri Olsa6a4bb042012-08-08 12:22:36 +0200666static int get_group_fd(struct perf_evsel *evsel, int cpu, int thread)
667{
668 struct perf_evsel *leader = evsel->leader;
669 int fd;
670
671 if (!leader)
672 return -1;
673
674 /*
675 * Leader must be already processed/open,
676 * if not it's a bug.
677 */
678 BUG_ON(!leader->fd);
679
680 fd = FD(leader, cpu, thread);
681 BUG_ON(fd == -1);
682
683 return fd;
684}
685
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -0200686static int __perf_evsel__open(struct perf_evsel *evsel, struct cpu_map *cpus,
Jiri Olsa6a4bb042012-08-08 12:22:36 +0200687 struct thread_map *threads)
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -0200688{
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -0200689 int cpu, thread;
Stephane Eranian023695d2011-02-14 11:20:01 +0200690 unsigned long flags = 0;
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -0200691 int pid = -1, err;
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -0200692
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -0200693 if (evsel->fd == NULL &&
694 perf_evsel__alloc_fd(evsel, cpus->nr, threads->nr) < 0)
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -0200695 return -ENOMEM;
Arnaldo Carvalho de Melo4eed11d2011-01-04 00:13:17 -0200696
Stephane Eranian023695d2011-02-14 11:20:01 +0200697 if (evsel->cgrp) {
698 flags = PERF_FLAG_PID_CGROUP;
699 pid = evsel->cgrp->fd;
700 }
701
Arnaldo Carvalho de Melo86bd5e82011-01-03 23:09:46 -0200702 for (cpu = 0; cpu < cpus->nr; cpu++) {
Arnaldo Carvalho de Melo9d04f172011-01-12 00:08:18 -0200703
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -0200704 for (thread = 0; thread < threads->nr; thread++) {
Jiri Olsa6a4bb042012-08-08 12:22:36 +0200705 int group_fd;
Stephane Eranian023695d2011-02-14 11:20:01 +0200706
707 if (!evsel->cgrp)
708 pid = threads->map[thread];
709
Jiri Olsa6a4bb042012-08-08 12:22:36 +0200710 group_fd = get_group_fd(evsel, cpu, thread);
711
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -0200712 FD(evsel, cpu, thread) = sys_perf_event_open(&evsel->attr,
Stephane Eranian023695d2011-02-14 11:20:01 +0200713 pid,
Arnaldo Carvalho de Melof08199d2011-01-11 23:42:19 -0200714 cpus->map[cpu],
Stephane Eranian023695d2011-02-14 11:20:01 +0200715 group_fd, flags);
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -0200716 if (FD(evsel, cpu, thread) < 0) {
717 err = -errno;
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -0200718 goto out_close;
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -0200719 }
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -0200720 }
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -0200721 }
722
723 return 0;
724
725out_close:
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -0200726 do {
727 while (--thread >= 0) {
728 close(FD(evsel, cpu, thread));
729 FD(evsel, cpu, thread) = -1;
730 }
731 thread = threads->nr;
732 } while (--cpu >= 0);
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -0200733 return err;
734}
735
736void perf_evsel__close(struct perf_evsel *evsel, int ncpus, int nthreads)
737{
738 if (evsel->fd == NULL)
739 return;
740
741 perf_evsel__close_fd(evsel, ncpus, nthreads);
742 perf_evsel__free_fd(evsel);
743 evsel->fd = NULL;
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -0200744}
745
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -0200746static struct {
747 struct cpu_map map;
748 int cpus[1];
749} empty_cpu_map = {
750 .map.nr = 1,
751 .cpus = { -1, },
752};
753
754static struct {
755 struct thread_map map;
756 int threads[1];
757} empty_thread_map = {
758 .map.nr = 1,
759 .threads = { -1, },
760};
761
Arnaldo Carvalho de Melof08199d2011-01-11 23:42:19 -0200762int perf_evsel__open(struct perf_evsel *evsel, struct cpu_map *cpus,
Jiri Olsa6a4bb042012-08-08 12:22:36 +0200763 struct thread_map *threads)
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -0200764{
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -0200765 if (cpus == NULL) {
766 /* Work around old compiler warnings about strict aliasing */
767 cpus = &empty_cpu_map.map;
768 }
769
770 if (threads == NULL)
771 threads = &empty_thread_map.map;
772
Jiri Olsa6a4bb042012-08-08 12:22:36 +0200773 return __perf_evsel__open(evsel, cpus, threads);
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -0200774}
775
Arnaldo Carvalho de Melof08199d2011-01-11 23:42:19 -0200776int perf_evsel__open_per_cpu(struct perf_evsel *evsel,
Jiri Olsa6a4bb042012-08-08 12:22:36 +0200777 struct cpu_map *cpus)
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -0200778{
Jiri Olsa6a4bb042012-08-08 12:22:36 +0200779 return __perf_evsel__open(evsel, cpus, &empty_thread_map.map);
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -0200780}
781
Arnaldo Carvalho de Melof08199d2011-01-11 23:42:19 -0200782int perf_evsel__open_per_thread(struct perf_evsel *evsel,
Jiri Olsa6a4bb042012-08-08 12:22:36 +0200783 struct thread_map *threads)
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -0200784{
Jiri Olsa6a4bb042012-08-08 12:22:36 +0200785 return __perf_evsel__open(evsel, &empty_cpu_map.map, threads);
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -0200786}
Arnaldo Carvalho de Melo70082dd2011-01-12 17:03:24 -0200787
Arnaldo Carvalho de Melo0807d2d2012-09-26 12:48:18 -0300788static int perf_evsel__parse_id_sample(const struct perf_evsel *evsel,
789 const union perf_event *event,
790 struct perf_sample *sample)
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -0200791{
Arnaldo Carvalho de Melo0807d2d2012-09-26 12:48:18 -0300792 u64 type = evsel->attr.sample_type;
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -0200793 const u64 *array = event->sample.array;
Arnaldo Carvalho de Melo0807d2d2012-09-26 12:48:18 -0300794 bool swapped = evsel->needs_swap;
Jiri Olsa37073f92012-05-30 14:23:44 +0200795 union u64_swap u;
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -0200796
797 array += ((event->header.size -
798 sizeof(event->header)) / sizeof(u64)) - 1;
799
800 if (type & PERF_SAMPLE_CPU) {
Jiri Olsa37073f92012-05-30 14:23:44 +0200801 u.val64 = *array;
802 if (swapped) {
803 /* undo swap of u64, then swap on individual u32s */
804 u.val64 = bswap_64(u.val64);
805 u.val32[0] = bswap_32(u.val32[0]);
806 }
807
808 sample->cpu = u.val32[0];
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -0200809 array--;
810 }
811
812 if (type & PERF_SAMPLE_STREAM_ID) {
813 sample->stream_id = *array;
814 array--;
815 }
816
817 if (type & PERF_SAMPLE_ID) {
818 sample->id = *array;
819 array--;
820 }
821
822 if (type & PERF_SAMPLE_TIME) {
823 sample->time = *array;
824 array--;
825 }
826
827 if (type & PERF_SAMPLE_TID) {
Jiri Olsa37073f92012-05-30 14:23:44 +0200828 u.val64 = *array;
829 if (swapped) {
830 /* undo swap of u64, then swap on individual u32s */
831 u.val64 = bswap_64(u.val64);
832 u.val32[0] = bswap_32(u.val32[0]);
833 u.val32[1] = bswap_32(u.val32[1]);
834 }
835
836 sample->pid = u.val32[0];
837 sample->tid = u.val32[1];
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -0200838 }
839
840 return 0;
841}
842
Frederic Weisbecker98e1da92011-05-21 20:08:15 +0200843static bool sample_overlap(const union perf_event *event,
844 const void *offset, u64 size)
845{
846 const void *base = event;
847
848 if (offset + size > base + event->header.size)
849 return true;
850
851 return false;
852}
853
Arnaldo Carvalho de Meloa3f698f2012-08-02 12:23:46 -0300854int perf_evsel__parse_sample(struct perf_evsel *evsel, union perf_event *event,
Arnaldo Carvalho de Melo0807d2d2012-09-26 12:48:18 -0300855 struct perf_sample *data)
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -0200856{
Arnaldo Carvalho de Meloa3f698f2012-08-02 12:23:46 -0300857 u64 type = evsel->attr.sample_type;
Jiri Olsa0f6a3012012-08-07 15:20:45 +0200858 u64 regs_user = evsel->attr.sample_regs_user;
Arnaldo Carvalho de Melo0807d2d2012-09-26 12:48:18 -0300859 bool swapped = evsel->needs_swap;
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -0200860 const u64 *array;
861
David Ahern936be502011-09-06 09:12:26 -0600862 /*
863 * used for cross-endian analysis. See git commit 65014ab3
864 * for why this goofiness is needed.
865 */
Jiri Olsa6a11f922012-05-16 08:59:04 +0200866 union u64_swap u;
David Ahern936be502011-09-06 09:12:26 -0600867
Robert Richterf3bda2c2011-12-15 17:32:39 +0100868 memset(data, 0, sizeof(*data));
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -0200869 data->cpu = data->pid = data->tid = -1;
870 data->stream_id = data->id = data->time = -1ULL;
Naveen N. Raoa4a03fc2012-02-03 22:31:13 +0530871 data->period = 1;
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -0200872
873 if (event->header.type != PERF_RECORD_SAMPLE) {
Arnaldo Carvalho de Meloa3f698f2012-08-02 12:23:46 -0300874 if (!evsel->attr.sample_id_all)
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -0200875 return 0;
Arnaldo Carvalho de Melo0807d2d2012-09-26 12:48:18 -0300876 return perf_evsel__parse_id_sample(evsel, event, data);
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -0200877 }
878
879 array = event->sample.array;
880
Arnaldo Carvalho de Meloa3f698f2012-08-02 12:23:46 -0300881 if (evsel->sample_size + sizeof(event->header) > event->header.size)
Frederic Weisbeckera2854122011-05-21 19:33:04 +0200882 return -EFAULT;
883
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -0200884 if (type & PERF_SAMPLE_IP) {
885 data->ip = event->ip.ip;
886 array++;
887 }
888
889 if (type & PERF_SAMPLE_TID) {
David Ahern936be502011-09-06 09:12:26 -0600890 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 data->pid = u.val32[0];
899 data->tid = u.val32[1];
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -0200900 array++;
901 }
902
903 if (type & PERF_SAMPLE_TIME) {
904 data->time = *array;
905 array++;
906 }
907
David Ahern7cec0922011-05-30 13:08:23 -0600908 data->addr = 0;
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -0200909 if (type & PERF_SAMPLE_ADDR) {
910 data->addr = *array;
911 array++;
912 }
913
914 data->id = -1ULL;
915 if (type & PERF_SAMPLE_ID) {
916 data->id = *array;
917 array++;
918 }
919
920 if (type & PERF_SAMPLE_STREAM_ID) {
921 data->stream_id = *array;
922 array++;
923 }
924
925 if (type & PERF_SAMPLE_CPU) {
David Ahern936be502011-09-06 09:12:26 -0600926
927 u.val64 = *array;
928 if (swapped) {
929 /* undo swap of u64, then swap on individual u32s */
930 u.val64 = bswap_64(u.val64);
931 u.val32[0] = bswap_32(u.val32[0]);
932 }
933
934 data->cpu = u.val32[0];
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -0200935 array++;
936 }
937
938 if (type & PERF_SAMPLE_PERIOD) {
939 data->period = *array;
940 array++;
941 }
942
943 if (type & PERF_SAMPLE_READ) {
Masanari Iidaf9d36992012-01-25 15:20:40 +0100944 fprintf(stderr, "PERF_SAMPLE_READ is unsupported for now\n");
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -0200945 return -1;
946 }
947
948 if (type & PERF_SAMPLE_CALLCHAIN) {
Frederic Weisbecker98e1da92011-05-21 20:08:15 +0200949 if (sample_overlap(event, array, sizeof(data->callchain->nr)))
950 return -EFAULT;
951
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -0200952 data->callchain = (struct ip_callchain *)array;
Frederic Weisbecker98e1da92011-05-21 20:08:15 +0200953
954 if (sample_overlap(event, array, data->callchain->nr))
955 return -EFAULT;
956
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -0200957 array += 1 + data->callchain->nr;
958 }
959
960 if (type & PERF_SAMPLE_RAW) {
Jiri Olsa8e303f22011-09-29 17:05:08 +0200961 const u64 *pdata;
962
David Ahern936be502011-09-06 09:12:26 -0600963 u.val64 = *array;
964 if (WARN_ONCE(swapped,
965 "Endianness of raw data not corrected!\n")) {
966 /* undo swap of u64, then swap on individual u32s */
967 u.val64 = bswap_64(u.val64);
968 u.val32[0] = bswap_32(u.val32[0]);
969 u.val32[1] = bswap_32(u.val32[1]);
970 }
Frederic Weisbecker98e1da92011-05-21 20:08:15 +0200971
972 if (sample_overlap(event, array, sizeof(u32)))
973 return -EFAULT;
974
David Ahern936be502011-09-06 09:12:26 -0600975 data->raw_size = u.val32[0];
Jiri Olsa8e303f22011-09-29 17:05:08 +0200976 pdata = (void *) array + sizeof(u32);
Frederic Weisbecker98e1da92011-05-21 20:08:15 +0200977
Jiri Olsa8e303f22011-09-29 17:05:08 +0200978 if (sample_overlap(event, pdata, data->raw_size))
Frederic Weisbecker98e1da92011-05-21 20:08:15 +0200979 return -EFAULT;
980
Jiri Olsa8e303f22011-09-29 17:05:08 +0200981 data->raw_data = (void *) pdata;
Stephane Eranianfa30c962012-03-17 23:23:18 +0100982
983 array = (void *)array + data->raw_size + sizeof(u32);
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -0200984 }
985
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100986 if (type & PERF_SAMPLE_BRANCH_STACK) {
987 u64 sz;
988
989 data->branch_stack = (struct branch_stack *)array;
990 array++; /* nr */
991
992 sz = data->branch_stack->nr * sizeof(struct branch_entry);
993 sz /= sizeof(u64);
994 array += sz;
995 }
Jiri Olsa0f6a3012012-08-07 15:20:45 +0200996
997 if (type & PERF_SAMPLE_REGS_USER) {
998 /* First u64 tells us if we have any regs in sample. */
999 u64 avail = *array++;
1000
1001 if (avail) {
1002 data->user_regs.regs = (u64 *)array;
1003 array += hweight_long(regs_user);
1004 }
1005 }
1006
1007 if (type & PERF_SAMPLE_STACK_USER) {
1008 u64 size = *array++;
1009
1010 data->user_stack.offset = ((char *)(array - 1)
1011 - (char *) event);
1012
1013 if (!size) {
1014 data->user_stack.size = 0;
1015 } else {
1016 data->user_stack.data = (char *)array;
1017 array += size / sizeof(*array);
1018 data->user_stack.size = *array;
1019 }
1020 }
1021
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -02001022 return 0;
1023}
Andrew Vagin74eec262011-11-28 12:03:31 +03001024
1025int perf_event__synthesize_sample(union perf_event *event, u64 type,
1026 const struct perf_sample *sample,
1027 bool swapped)
1028{
1029 u64 *array;
1030
1031 /*
1032 * used for cross-endian analysis. See git commit 65014ab3
1033 * for why this goofiness is needed.
1034 */
Jiri Olsa6a11f922012-05-16 08:59:04 +02001035 union u64_swap u;
Andrew Vagin74eec262011-11-28 12:03:31 +03001036
1037 array = event->sample.array;
1038
1039 if (type & PERF_SAMPLE_IP) {
1040 event->ip.ip = sample->ip;
1041 array++;
1042 }
1043
1044 if (type & PERF_SAMPLE_TID) {
1045 u.val32[0] = sample->pid;
1046 u.val32[1] = sample->tid;
1047 if (swapped) {
1048 /*
Arnaldo Carvalho de Meloa3f698f2012-08-02 12:23:46 -03001049 * Inverse of what is done in perf_evsel__parse_sample
Andrew Vagin74eec262011-11-28 12:03:31 +03001050 */
1051 u.val32[0] = bswap_32(u.val32[0]);
1052 u.val32[1] = bswap_32(u.val32[1]);
1053 u.val64 = bswap_64(u.val64);
1054 }
1055
1056 *array = u.val64;
1057 array++;
1058 }
1059
1060 if (type & PERF_SAMPLE_TIME) {
1061 *array = sample->time;
1062 array++;
1063 }
1064
1065 if (type & PERF_SAMPLE_ADDR) {
1066 *array = sample->addr;
1067 array++;
1068 }
1069
1070 if (type & PERF_SAMPLE_ID) {
1071 *array = sample->id;
1072 array++;
1073 }
1074
1075 if (type & PERF_SAMPLE_STREAM_ID) {
1076 *array = sample->stream_id;
1077 array++;
1078 }
1079
1080 if (type & PERF_SAMPLE_CPU) {
1081 u.val32[0] = sample->cpu;
1082 if (swapped) {
1083 /*
Arnaldo Carvalho de Meloa3f698f2012-08-02 12:23:46 -03001084 * Inverse of what is done in perf_evsel__parse_sample
Andrew Vagin74eec262011-11-28 12:03:31 +03001085 */
1086 u.val32[0] = bswap_32(u.val32[0]);
1087 u.val64 = bswap_64(u.val64);
1088 }
1089 *array = u.val64;
1090 array++;
1091 }
1092
1093 if (type & PERF_SAMPLE_PERIOD) {
1094 *array = sample->period;
1095 array++;
1096 }
1097
1098 return 0;
1099}
Arnaldo Carvalho de Melo5555ded2012-09-11 19:24:23 -03001100
Arnaldo Carvalho de Meloefd2b922012-09-18 11:21:50 -03001101struct format_field *perf_evsel__field(struct perf_evsel *evsel, const char *name)
1102{
1103 return pevent_find_field(evsel->tp_format, name);
1104}
1105
Arnaldo Carvalho de Melo5555ded2012-09-11 19:24:23 -03001106char *perf_evsel__strval(struct perf_evsel *evsel, struct perf_sample *sample,
1107 const char *name)
1108{
Arnaldo Carvalho de Meloefd2b922012-09-18 11:21:50 -03001109 struct format_field *field = perf_evsel__field(evsel, name);
Arnaldo Carvalho de Melo5555ded2012-09-11 19:24:23 -03001110 int offset;
1111
Arnaldo Carvalho de Meloefd2b922012-09-18 11:21:50 -03001112 if (!field)
1113 return NULL;
Arnaldo Carvalho de Melo5555ded2012-09-11 19:24:23 -03001114
1115 offset = field->offset;
1116
1117 if (field->flags & FIELD_IS_DYNAMIC) {
1118 offset = *(int *)(sample->raw_data + field->offset);
1119 offset &= 0xffff;
1120 }
1121
1122 return sample->raw_data + offset;
1123}
1124
1125u64 perf_evsel__intval(struct perf_evsel *evsel, struct perf_sample *sample,
1126 const char *name)
1127{
Arnaldo Carvalho de Meloefd2b922012-09-18 11:21:50 -03001128 struct format_field *field = perf_evsel__field(evsel, name);
Arnaldo Carvalho de Meloe6b6f672012-09-26 13:13:04 -03001129 void *ptr;
1130 u64 value;
Arnaldo Carvalho de Melo5555ded2012-09-11 19:24:23 -03001131
Arnaldo Carvalho de Meloefd2b922012-09-18 11:21:50 -03001132 if (!field)
1133 return 0;
Arnaldo Carvalho de Melo5555ded2012-09-11 19:24:23 -03001134
Arnaldo Carvalho de Meloe6b6f672012-09-26 13:13:04 -03001135 ptr = sample->raw_data + field->offset;
Arnaldo Carvalho de Melo5555ded2012-09-11 19:24:23 -03001136
Arnaldo Carvalho de Meloe6b6f672012-09-26 13:13:04 -03001137 switch (field->size) {
1138 case 1:
1139 return *(u8 *)ptr;
1140 case 2:
1141 value = *(u16 *)ptr;
1142 break;
1143 case 4:
1144 value = *(u32 *)ptr;
1145 break;
1146 case 8:
1147 value = *(u64 *)ptr;
1148 break;
1149 default:
1150 return 0;
1151 }
1152
1153 if (!evsel->needs_swap)
1154 return value;
1155
1156 switch (field->size) {
1157 case 2:
1158 return bswap_16(value);
1159 case 4:
1160 return bswap_32(value);
1161 case 8:
1162 return bswap_64(value);
1163 default:
1164 return 0;
1165 }
1166
1167 return 0;
Arnaldo Carvalho de Melo5555ded2012-09-11 19:24:23 -03001168}