blob: 06f76441547a31785659be9f5619a6bbbb733a0a [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 Melo69aad6f2011-01-03 16:39:04 -020013#include "evsel.h"
Arnaldo Carvalho de Melo70082dd2011-01-12 17:03:24 -020014#include "evlist.h"
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -020015#include "util.h"
Arnaldo Carvalho de Melo86bd5e82011-01-03 23:09:46 -020016#include "cpumap.h"
Arnaldo Carvalho de Melofd782602011-01-18 15:15:24 -020017#include "thread_map.h"
Namhyung Kim12864b32012-04-26 14:15:22 +090018#include "target.h"
Jiri Olsa287e74a2012-06-28 23:18:49 +020019#include "../../../include/linux/hw_breakpoint.h"
Jiri Olsa26d33022012-08-07 15:20:47 +020020#include "../../include/linux/perf_event.h"
21#include "perf_regs.h"
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -020022
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -020023#define FD(e, x, y) (*(int *)xyarray__entry(e->fd, x, y))
24
Arnaldo Carvalho de Melobde09462012-08-01 18:53:11 -030025static int __perf_evsel__sample_size(u64 sample_type)
Arnaldo Carvalho de Meloc2a70652011-06-02 11:04:54 -030026{
27 u64 mask = sample_type & PERF_SAMPLE_MASK;
28 int size = 0;
29 int i;
30
31 for (i = 0; i < 64; i++) {
32 if (mask & (1ULL << i))
33 size++;
34 }
35
36 size *= sizeof(u64);
37
38 return size;
39}
40
Jiri Olsa4bf9ce12012-03-22 14:37:26 +010041void hists__init(struct hists *hists)
Arnaldo Carvalho de Melo0e2a5f12011-11-04 08:16:58 -020042{
43 memset(hists, 0, sizeof(*hists));
44 hists->entries_in_array[0] = hists->entries_in_array[1] = RB_ROOT;
45 hists->entries_in = &hists->entries_in_array[0];
46 hists->entries_collapsed = RB_ROOT;
47 hists->entries = RB_ROOT;
48 pthread_mutex_init(&hists->lock, NULL);
49}
50
Arnaldo Carvalho de Meloef1d1af2011-01-18 21:41:45 -020051void perf_evsel__init(struct perf_evsel *evsel,
52 struct perf_event_attr *attr, int idx)
53{
54 evsel->idx = idx;
55 evsel->attr = *attr;
56 INIT_LIST_HEAD(&evsel->node);
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -030057 hists__init(&evsel->hists);
Arnaldo Carvalho de Melobde09462012-08-01 18:53:11 -030058 evsel->sample_size = __perf_evsel__sample_size(attr->sample_type);
Arnaldo Carvalho de Meloef1d1af2011-01-18 21:41:45 -020059}
60
Lin Ming23a2f3a2011-01-07 11:11:09 +080061struct perf_evsel *perf_evsel__new(struct perf_event_attr *attr, int idx)
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -020062{
63 struct perf_evsel *evsel = zalloc(sizeof(*evsel));
64
Arnaldo Carvalho de Meloef1d1af2011-01-18 21:41:45 -020065 if (evsel != NULL)
66 perf_evsel__init(evsel, attr, idx);
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -020067
68 return evsel;
69}
70
Arnaldo Carvalho de Melo8ad70132012-09-06 13:11:18 -030071const char *perf_evsel__hw_names[PERF_COUNT_HW_MAX] = {
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -030072 "cycles",
73 "instructions",
74 "cache-references",
75 "cache-misses",
76 "branches",
77 "branch-misses",
78 "bus-cycles",
79 "stalled-cycles-frontend",
80 "stalled-cycles-backend",
81 "ref-cycles",
82};
83
Arnaldo Carvalho de Melodd4f5222012-06-13 15:52:42 -030084static const char *__perf_evsel__hw_name(u64 config)
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -030085{
86 if (config < PERF_COUNT_HW_MAX && perf_evsel__hw_names[config])
87 return perf_evsel__hw_names[config];
88
89 return "unknown-hardware";
90}
91
Arnaldo Carvalho de Melo27f18612012-06-11 13:33:09 -030092static int perf_evsel__add_modifiers(struct perf_evsel *evsel, char *bf, size_t size)
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -030093{
Arnaldo Carvalho de Melo27f18612012-06-11 13:33:09 -030094 int colon = 0, r = 0;
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -030095 struct perf_event_attr *attr = &evsel->attr;
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -030096 bool exclude_guest_default = false;
97
98#define MOD_PRINT(context, mod) do { \
99 if (!attr->exclude_##context) { \
Arnaldo Carvalho de Melo27f18612012-06-11 13:33:09 -0300100 if (!colon) colon = ++r; \
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300101 r += scnprintf(bf + r, size - r, "%c", mod); \
102 } } while(0)
103
104 if (attr->exclude_kernel || attr->exclude_user || attr->exclude_hv) {
105 MOD_PRINT(kernel, 'k');
106 MOD_PRINT(user, 'u');
107 MOD_PRINT(hv, 'h');
108 exclude_guest_default = true;
109 }
110
111 if (attr->precise_ip) {
112 if (!colon)
Arnaldo Carvalho de Melo27f18612012-06-11 13:33:09 -0300113 colon = ++r;
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300114 r += scnprintf(bf + r, size - r, "%.*s", attr->precise_ip, "ppp");
115 exclude_guest_default = true;
116 }
117
118 if (attr->exclude_host || attr->exclude_guest == exclude_guest_default) {
119 MOD_PRINT(host, 'H');
120 MOD_PRINT(guest, 'G');
121 }
122#undef MOD_PRINT
123 if (colon)
Arnaldo Carvalho de Melo27f18612012-06-11 13:33:09 -0300124 bf[colon - 1] = ':';
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300125 return r;
126}
127
Arnaldo Carvalho de Melo27f18612012-06-11 13:33:09 -0300128static int perf_evsel__hw_name(struct perf_evsel *evsel, char *bf, size_t size)
129{
130 int r = scnprintf(bf, size, "%s", __perf_evsel__hw_name(evsel->attr.config));
131 return r + perf_evsel__add_modifiers(evsel, bf + r, size - r);
132}
133
Arnaldo Carvalho de Melo8ad70132012-09-06 13:11:18 -0300134const char *perf_evsel__sw_names[PERF_COUNT_SW_MAX] = {
Arnaldo Carvalho de Melo335c2f52012-06-11 14:36:20 -0300135 "cpu-clock",
136 "task-clock",
137 "page-faults",
138 "context-switches",
Arnaldo Carvalho de Melo8ad70132012-09-06 13:11:18 -0300139 "cpu-migrations",
Arnaldo Carvalho de Melo335c2f52012-06-11 14:36:20 -0300140 "minor-faults",
141 "major-faults",
142 "alignment-faults",
143 "emulation-faults",
144};
145
Arnaldo Carvalho de Melodd4f5222012-06-13 15:52:42 -0300146static const char *__perf_evsel__sw_name(u64 config)
Arnaldo Carvalho de Melo335c2f52012-06-11 14:36:20 -0300147{
148 if (config < PERF_COUNT_SW_MAX && perf_evsel__sw_names[config])
149 return perf_evsel__sw_names[config];
150 return "unknown-software";
151}
152
153static int perf_evsel__sw_name(struct perf_evsel *evsel, char *bf, size_t size)
154{
155 int r = scnprintf(bf, size, "%s", __perf_evsel__sw_name(evsel->attr.config));
156 return r + perf_evsel__add_modifiers(evsel, bf + r, size - r);
157}
158
Jiri Olsa287e74a2012-06-28 23:18:49 +0200159static int __perf_evsel__bp_name(char *bf, size_t size, u64 addr, u64 type)
160{
161 int r;
162
163 r = scnprintf(bf, size, "mem:0x%" PRIx64 ":", addr);
164
165 if (type & HW_BREAKPOINT_R)
166 r += scnprintf(bf + r, size - r, "r");
167
168 if (type & HW_BREAKPOINT_W)
169 r += scnprintf(bf + r, size - r, "w");
170
171 if (type & HW_BREAKPOINT_X)
172 r += scnprintf(bf + r, size - r, "x");
173
174 return r;
175}
176
177static int perf_evsel__bp_name(struct perf_evsel *evsel, char *bf, size_t size)
178{
179 struct perf_event_attr *attr = &evsel->attr;
180 int r = __perf_evsel__bp_name(bf, size, attr->bp_addr, attr->bp_type);
181 return r + perf_evsel__add_modifiers(evsel, bf + r, size - r);
182}
183
Arnaldo Carvalho de Melo0b668bc2012-06-11 14:08:07 -0300184const char *perf_evsel__hw_cache[PERF_COUNT_HW_CACHE_MAX]
185 [PERF_EVSEL__MAX_ALIASES] = {
186 { "L1-dcache", "l1-d", "l1d", "L1-data", },
187 { "L1-icache", "l1-i", "l1i", "L1-instruction", },
188 { "LLC", "L2", },
189 { "dTLB", "d-tlb", "Data-TLB", },
190 { "iTLB", "i-tlb", "Instruction-TLB", },
191 { "branch", "branches", "bpu", "btb", "bpc", },
192 { "node", },
193};
194
195const char *perf_evsel__hw_cache_op[PERF_COUNT_HW_CACHE_OP_MAX]
196 [PERF_EVSEL__MAX_ALIASES] = {
197 { "load", "loads", "read", },
198 { "store", "stores", "write", },
199 { "prefetch", "prefetches", "speculative-read", "speculative-load", },
200};
201
202const char *perf_evsel__hw_cache_result[PERF_COUNT_HW_CACHE_RESULT_MAX]
203 [PERF_EVSEL__MAX_ALIASES] = {
204 { "refs", "Reference", "ops", "access", },
205 { "misses", "miss", },
206};
207
208#define C(x) PERF_COUNT_HW_CACHE_##x
209#define CACHE_READ (1 << C(OP_READ))
210#define CACHE_WRITE (1 << C(OP_WRITE))
211#define CACHE_PREFETCH (1 << C(OP_PREFETCH))
212#define COP(x) (1 << x)
213
214/*
215 * cache operartion stat
216 * L1I : Read and prefetch only
217 * ITLB and BPU : Read-only
218 */
219static unsigned long perf_evsel__hw_cache_stat[C(MAX)] = {
220 [C(L1D)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
221 [C(L1I)] = (CACHE_READ | CACHE_PREFETCH),
222 [C(LL)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
223 [C(DTLB)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
224 [C(ITLB)] = (CACHE_READ),
225 [C(BPU)] = (CACHE_READ),
226 [C(NODE)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
227};
228
229bool perf_evsel__is_cache_op_valid(u8 type, u8 op)
230{
231 if (perf_evsel__hw_cache_stat[type] & COP(op))
232 return true; /* valid */
233 else
234 return false; /* invalid */
235}
236
237int __perf_evsel__hw_cache_type_op_res_name(u8 type, u8 op, u8 result,
238 char *bf, size_t size)
239{
240 if (result) {
241 return scnprintf(bf, size, "%s-%s-%s", perf_evsel__hw_cache[type][0],
242 perf_evsel__hw_cache_op[op][0],
243 perf_evsel__hw_cache_result[result][0]);
244 }
245
246 return scnprintf(bf, size, "%s-%s", perf_evsel__hw_cache[type][0],
247 perf_evsel__hw_cache_op[op][1]);
248}
249
Arnaldo Carvalho de Melodd4f5222012-06-13 15:52:42 -0300250static int __perf_evsel__hw_cache_name(u64 config, char *bf, size_t size)
Arnaldo Carvalho de Melo0b668bc2012-06-11 14:08:07 -0300251{
252 u8 op, result, type = (config >> 0) & 0xff;
253 const char *err = "unknown-ext-hardware-cache-type";
254
255 if (type > PERF_COUNT_HW_CACHE_MAX)
256 goto out_err;
257
258 op = (config >> 8) & 0xff;
259 err = "unknown-ext-hardware-cache-op";
260 if (op > PERF_COUNT_HW_CACHE_OP_MAX)
261 goto out_err;
262
263 result = (config >> 16) & 0xff;
264 err = "unknown-ext-hardware-cache-result";
265 if (result > PERF_COUNT_HW_CACHE_RESULT_MAX)
266 goto out_err;
267
268 err = "invalid-cache";
269 if (!perf_evsel__is_cache_op_valid(type, op))
270 goto out_err;
271
272 return __perf_evsel__hw_cache_type_op_res_name(type, op, result, bf, size);
273out_err:
274 return scnprintf(bf, size, "%s", err);
275}
276
277static int perf_evsel__hw_cache_name(struct perf_evsel *evsel, char *bf, size_t size)
278{
279 int ret = __perf_evsel__hw_cache_name(evsel->attr.config, bf, size);
280 return ret + perf_evsel__add_modifiers(evsel, bf + ret, size - ret);
281}
282
Arnaldo Carvalho de Melo6eef3d92012-06-13 11:53:37 -0300283static int perf_evsel__raw_name(struct perf_evsel *evsel, char *bf, size_t size)
284{
285 int ret = scnprintf(bf, size, "raw 0x%" PRIx64, evsel->attr.config);
286 return ret + perf_evsel__add_modifiers(evsel, bf + ret, size - ret);
287}
288
Arnaldo Carvalho de Melo7289f832012-06-12 12:34:58 -0300289const char *perf_evsel__name(struct perf_evsel *evsel)
Arnaldo Carvalho de Meloa4460832012-06-12 10:29:12 -0300290{
Arnaldo Carvalho de Melo7289f832012-06-12 12:34:58 -0300291 char bf[128];
Arnaldo Carvalho de Meloa4460832012-06-12 10:29:12 -0300292
Arnaldo Carvalho de Melo7289f832012-06-12 12:34:58 -0300293 if (evsel->name)
294 return evsel->name;
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300295
296 switch (evsel->attr.type) {
297 case PERF_TYPE_RAW:
Arnaldo Carvalho de Melo6eef3d92012-06-13 11:53:37 -0300298 perf_evsel__raw_name(evsel, bf, sizeof(bf));
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300299 break;
300
301 case PERF_TYPE_HARDWARE:
Arnaldo Carvalho de Melo7289f832012-06-12 12:34:58 -0300302 perf_evsel__hw_name(evsel, bf, sizeof(bf));
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300303 break;
Arnaldo Carvalho de Melo0b668bc2012-06-11 14:08:07 -0300304
305 case PERF_TYPE_HW_CACHE:
Arnaldo Carvalho de Melo7289f832012-06-12 12:34:58 -0300306 perf_evsel__hw_cache_name(evsel, bf, sizeof(bf));
Arnaldo Carvalho de Melo0b668bc2012-06-11 14:08:07 -0300307 break;
308
Arnaldo Carvalho de Melo335c2f52012-06-11 14:36:20 -0300309 case PERF_TYPE_SOFTWARE:
Arnaldo Carvalho de Melo7289f832012-06-12 12:34:58 -0300310 perf_evsel__sw_name(evsel, bf, sizeof(bf));
Arnaldo Carvalho de Melo335c2f52012-06-11 14:36:20 -0300311 break;
312
Arnaldo Carvalho de Meloa4460832012-06-12 10:29:12 -0300313 case PERF_TYPE_TRACEPOINT:
Arnaldo Carvalho de Melo7289f832012-06-12 12:34:58 -0300314 scnprintf(bf, sizeof(bf), "%s", "unknown tracepoint");
Arnaldo Carvalho de Meloa4460832012-06-12 10:29:12 -0300315 break;
316
Jiri Olsa287e74a2012-06-28 23:18:49 +0200317 case PERF_TYPE_BREAKPOINT:
318 perf_evsel__bp_name(evsel, bf, sizeof(bf));
319 break;
320
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300321 default:
Robert Richterca1b1452012-08-16 21:10:18 +0200322 scnprintf(bf, sizeof(bf), "unknown attr type: %d",
323 evsel->attr.type);
Arnaldo Carvalho de Meloa4460832012-06-12 10:29:12 -0300324 break;
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300325 }
326
Arnaldo Carvalho de Melo7289f832012-06-12 12:34:58 -0300327 evsel->name = strdup(bf);
328
329 return evsel->name ?: "unknown";
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300330}
331
Namhyung Kim5090c6a2012-03-16 17:42:20 +0900332void perf_evsel__config(struct perf_evsel *evsel, struct perf_record_opts *opts,
333 struct perf_evsel *first)
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200334{
335 struct perf_event_attr *attr = &evsel->attr;
336 int track = !evsel->idx; /* only the first counter needs these */
337
David Ahern5e1c81d2012-05-13 22:01:28 -0600338 attr->disabled = 1;
Arnaldo Carvalho de Melo808e1222012-02-14 14:18:57 -0200339 attr->sample_id_all = opts->sample_id_all_missing ? 0 : 1;
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200340 attr->inherit = !opts->no_inherit;
341 attr->read_format = PERF_FORMAT_TOTAL_TIME_ENABLED |
342 PERF_FORMAT_TOTAL_TIME_RUNNING |
343 PERF_FORMAT_ID;
344
345 attr->sample_type |= PERF_SAMPLE_IP | PERF_SAMPLE_TID;
346
347 /*
348 * We default some events to a 1 default interval. But keep
349 * it a weak assumption overridable by the user.
350 */
351 if (!attr->sample_period || (opts->user_freq != UINT_MAX &&
352 opts->user_interval != ULLONG_MAX)) {
353 if (opts->freq) {
354 attr->sample_type |= PERF_SAMPLE_PERIOD;
355 attr->freq = 1;
356 attr->sample_freq = opts->freq;
357 } else {
358 attr->sample_period = opts->default_interval;
359 }
360 }
361
362 if (opts->no_samples)
363 attr->sample_freq = 0;
364
365 if (opts->inherit_stat)
366 attr->inherit_stat = 1;
367
368 if (opts->sample_address) {
369 attr->sample_type |= PERF_SAMPLE_ADDR;
370 attr->mmap_data = track;
371 }
372
Jiri Olsa26d33022012-08-07 15:20:47 +0200373 if (opts->call_graph) {
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200374 attr->sample_type |= PERF_SAMPLE_CALLCHAIN;
375
Jiri Olsa26d33022012-08-07 15:20:47 +0200376 if (opts->call_graph == CALLCHAIN_DWARF) {
377 attr->sample_type |= PERF_SAMPLE_REGS_USER |
378 PERF_SAMPLE_STACK_USER;
379 attr->sample_regs_user = PERF_REGS_MASK;
380 attr->sample_stack_user = opts->stack_dump_size;
381 attr->exclude_callchain_user = 1;
382 }
383 }
384
Namhyung Kime40ee742012-05-21 10:42:07 +0900385 if (perf_target__has_cpu(&opts->target))
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200386 attr->sample_type |= PERF_SAMPLE_CPU;
387
Andrew Vagin3e76ac72011-12-20 17:32:45 +0300388 if (opts->period)
389 attr->sample_type |= PERF_SAMPLE_PERIOD;
390
Arnaldo Carvalho de Melo808e1222012-02-14 14:18:57 -0200391 if (!opts->sample_id_all_missing &&
Namhyung Kimd67356e2012-05-07 14:09:03 +0900392 (opts->sample_time || !opts->no_inherit ||
Namhyung Kimaa22dd42012-05-16 18:45:47 +0900393 perf_target__has_cpu(&opts->target)))
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200394 attr->sample_type |= PERF_SAMPLE_TIME;
395
396 if (opts->raw_samples) {
397 attr->sample_type |= PERF_SAMPLE_TIME;
398 attr->sample_type |= PERF_SAMPLE_RAW;
399 attr->sample_type |= PERF_SAMPLE_CPU;
400 }
401
402 if (opts->no_delay) {
403 attr->watermark = 0;
404 attr->wakeup_events = 1;
405 }
Roberto Agostino Vitillobdfebd82012-02-09 23:21:02 +0100406 if (opts->branch_stack) {
407 attr->sample_type |= PERF_SAMPLE_BRANCH_STACK;
408 attr->branch_sample_type = opts->branch_stack;
409 }
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200410
411 attr->mmap = track;
412 attr->comm = track;
413
Namhyung Kimd67356e2012-05-07 14:09:03 +0900414 if (perf_target__none(&opts->target) &&
415 (!opts->group || evsel == first)) {
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200416 attr->enable_on_exec = 1;
417 }
418}
419
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200420int perf_evsel__alloc_fd(struct perf_evsel *evsel, int ncpus, int nthreads)
421{
David Ahern4af4c952011-05-27 09:58:34 -0600422 int cpu, thread;
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200423 evsel->fd = xyarray__new(ncpus, nthreads, sizeof(int));
David Ahern4af4c952011-05-27 09:58:34 -0600424
425 if (evsel->fd) {
426 for (cpu = 0; cpu < ncpus; cpu++) {
427 for (thread = 0; thread < nthreads; thread++) {
428 FD(evsel, cpu, thread) = -1;
429 }
430 }
431 }
432
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200433 return evsel->fd != NULL ? 0 : -ENOMEM;
434}
435
Arnaldo Carvalho de Melo70db7532011-01-12 22:39:13 -0200436int perf_evsel__alloc_id(struct perf_evsel *evsel, int ncpus, int nthreads)
437{
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -0300438 evsel->sample_id = xyarray__new(ncpus, nthreads, sizeof(struct perf_sample_id));
439 if (evsel->sample_id == NULL)
440 return -ENOMEM;
441
442 evsel->id = zalloc(ncpus * nthreads * sizeof(u64));
443 if (evsel->id == NULL) {
444 xyarray__delete(evsel->sample_id);
445 evsel->sample_id = NULL;
446 return -ENOMEM;
447 }
448
449 return 0;
Arnaldo Carvalho de Melo70db7532011-01-12 22:39:13 -0200450}
451
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200452int perf_evsel__alloc_counts(struct perf_evsel *evsel, int ncpus)
453{
454 evsel->counts = zalloc((sizeof(*evsel->counts) +
455 (ncpus * sizeof(struct perf_counts_values))));
456 return evsel->counts != NULL ? 0 : -ENOMEM;
457}
458
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200459void perf_evsel__free_fd(struct perf_evsel *evsel)
460{
461 xyarray__delete(evsel->fd);
462 evsel->fd = NULL;
463}
464
Arnaldo Carvalho de Melo70db7532011-01-12 22:39:13 -0200465void perf_evsel__free_id(struct perf_evsel *evsel)
466{
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -0300467 xyarray__delete(evsel->sample_id);
468 evsel->sample_id = NULL;
469 free(evsel->id);
Arnaldo Carvalho de Melo70db7532011-01-12 22:39:13 -0200470 evsel->id = NULL;
471}
472
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200473void perf_evsel__close_fd(struct perf_evsel *evsel, int ncpus, int nthreads)
474{
475 int cpu, thread;
476
477 for (cpu = 0; cpu < ncpus; cpu++)
478 for (thread = 0; thread < nthreads; ++thread) {
479 close(FD(evsel, cpu, thread));
480 FD(evsel, cpu, thread) = -1;
481 }
482}
483
Arnaldo Carvalho de Meloef1d1af2011-01-18 21:41:45 -0200484void perf_evsel__exit(struct perf_evsel *evsel)
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200485{
486 assert(list_empty(&evsel->node));
487 xyarray__delete(evsel->fd);
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -0300488 xyarray__delete(evsel->sample_id);
489 free(evsel->id);
Arnaldo Carvalho de Meloef1d1af2011-01-18 21:41:45 -0200490}
491
492void perf_evsel__delete(struct perf_evsel *evsel)
493{
494 perf_evsel__exit(evsel);
Stephane Eranian023695d2011-02-14 11:20:01 +0200495 close_cgroup(evsel->cgrp);
Jiri Olsa6a4bb042012-08-08 12:22:36 +0200496 free(evsel->group_name);
Stephane Eranianf0c55bc2011-02-16 15:10:01 +0200497 free(evsel->name);
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200498 free(evsel);
499}
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200500
501int __perf_evsel__read_on_cpu(struct perf_evsel *evsel,
502 int cpu, int thread, bool scale)
503{
504 struct perf_counts_values count;
505 size_t nv = scale ? 3 : 1;
506
507 if (FD(evsel, cpu, thread) < 0)
508 return -EINVAL;
509
Arnaldo Carvalho de Melo4eed11d2011-01-04 00:13:17 -0200510 if (evsel->counts == NULL && perf_evsel__alloc_counts(evsel, cpu + 1) < 0)
511 return -ENOMEM;
512
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200513 if (readn(FD(evsel, cpu, thread), &count, nv * sizeof(u64)) < 0)
514 return -errno;
515
516 if (scale) {
517 if (count.run == 0)
518 count.val = 0;
519 else if (count.run < count.ena)
520 count.val = (u64)((double)count.val * count.ena / count.run + 0.5);
521 } else
522 count.ena = count.run = 0;
523
524 evsel->counts->cpu[cpu] = count;
525 return 0;
526}
527
528int __perf_evsel__read(struct perf_evsel *evsel,
529 int ncpus, int nthreads, bool scale)
530{
531 size_t nv = scale ? 3 : 1;
532 int cpu, thread;
533 struct perf_counts_values *aggr = &evsel->counts->aggr, count;
534
Arnaldo Carvalho de Melo52bcd9942011-02-03 17:26:06 -0200535 aggr->val = aggr->ena = aggr->run = 0;
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200536
537 for (cpu = 0; cpu < ncpus; cpu++) {
538 for (thread = 0; thread < nthreads; thread++) {
539 if (FD(evsel, cpu, thread) < 0)
540 continue;
541
542 if (readn(FD(evsel, cpu, thread),
543 &count, nv * sizeof(u64)) < 0)
544 return -errno;
545
546 aggr->val += count.val;
547 if (scale) {
548 aggr->ena += count.ena;
549 aggr->run += count.run;
550 }
551 }
552 }
553
554 evsel->counts->scaled = 0;
555 if (scale) {
556 if (aggr->run == 0) {
557 evsel->counts->scaled = -1;
558 aggr->val = 0;
559 return 0;
560 }
561
562 if (aggr->run < aggr->ena) {
563 evsel->counts->scaled = 1;
564 aggr->val = (u64)((double)aggr->val * aggr->ena / aggr->run + 0.5);
565 }
566 } else
567 aggr->ena = aggr->run = 0;
568
569 return 0;
570}
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -0200571
Jiri Olsa6a4bb042012-08-08 12:22:36 +0200572static int get_group_fd(struct perf_evsel *evsel, int cpu, int thread)
573{
574 struct perf_evsel *leader = evsel->leader;
575 int fd;
576
577 if (!leader)
578 return -1;
579
580 /*
581 * Leader must be already processed/open,
582 * if not it's a bug.
583 */
584 BUG_ON(!leader->fd);
585
586 fd = FD(leader, cpu, thread);
587 BUG_ON(fd == -1);
588
589 return fd;
590}
591
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -0200592static int __perf_evsel__open(struct perf_evsel *evsel, struct cpu_map *cpus,
Jiri Olsa6a4bb042012-08-08 12:22:36 +0200593 struct thread_map *threads)
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -0200594{
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -0200595 int cpu, thread;
Stephane Eranian023695d2011-02-14 11:20:01 +0200596 unsigned long flags = 0;
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -0200597 int pid = -1, err;
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -0200598
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -0200599 if (evsel->fd == NULL &&
600 perf_evsel__alloc_fd(evsel, cpus->nr, threads->nr) < 0)
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -0200601 return -ENOMEM;
Arnaldo Carvalho de Melo4eed11d2011-01-04 00:13:17 -0200602
Stephane Eranian023695d2011-02-14 11:20:01 +0200603 if (evsel->cgrp) {
604 flags = PERF_FLAG_PID_CGROUP;
605 pid = evsel->cgrp->fd;
606 }
607
Arnaldo Carvalho de Melo86bd5e82011-01-03 23:09:46 -0200608 for (cpu = 0; cpu < cpus->nr; cpu++) {
Arnaldo Carvalho de Melo9d04f172011-01-12 00:08:18 -0200609
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -0200610 for (thread = 0; thread < threads->nr; thread++) {
Jiri Olsa6a4bb042012-08-08 12:22:36 +0200611 int group_fd;
Stephane Eranian023695d2011-02-14 11:20:01 +0200612
613 if (!evsel->cgrp)
614 pid = threads->map[thread];
615
Jiri Olsa6a4bb042012-08-08 12:22:36 +0200616 group_fd = get_group_fd(evsel, cpu, thread);
617
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -0200618 FD(evsel, cpu, thread) = sys_perf_event_open(&evsel->attr,
Stephane Eranian023695d2011-02-14 11:20:01 +0200619 pid,
Arnaldo Carvalho de Melof08199d2011-01-11 23:42:19 -0200620 cpus->map[cpu],
Stephane Eranian023695d2011-02-14 11:20:01 +0200621 group_fd, flags);
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -0200622 if (FD(evsel, cpu, thread) < 0) {
623 err = -errno;
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -0200624 goto out_close;
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -0200625 }
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -0200626 }
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -0200627 }
628
629 return 0;
630
631out_close:
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -0200632 do {
633 while (--thread >= 0) {
634 close(FD(evsel, cpu, thread));
635 FD(evsel, cpu, thread) = -1;
636 }
637 thread = threads->nr;
638 } while (--cpu >= 0);
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -0200639 return err;
640}
641
642void perf_evsel__close(struct perf_evsel *evsel, int ncpus, int nthreads)
643{
644 if (evsel->fd == NULL)
645 return;
646
647 perf_evsel__close_fd(evsel, ncpus, nthreads);
648 perf_evsel__free_fd(evsel);
649 evsel->fd = NULL;
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -0200650}
651
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -0200652static struct {
653 struct cpu_map map;
654 int cpus[1];
655} empty_cpu_map = {
656 .map.nr = 1,
657 .cpus = { -1, },
658};
659
660static struct {
661 struct thread_map map;
662 int threads[1];
663} empty_thread_map = {
664 .map.nr = 1,
665 .threads = { -1, },
666};
667
Arnaldo Carvalho de Melof08199d2011-01-11 23:42:19 -0200668int perf_evsel__open(struct perf_evsel *evsel, struct cpu_map *cpus,
Jiri Olsa6a4bb042012-08-08 12:22:36 +0200669 struct thread_map *threads)
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -0200670{
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -0200671 if (cpus == NULL) {
672 /* Work around old compiler warnings about strict aliasing */
673 cpus = &empty_cpu_map.map;
674 }
675
676 if (threads == NULL)
677 threads = &empty_thread_map.map;
678
Jiri Olsa6a4bb042012-08-08 12:22:36 +0200679 return __perf_evsel__open(evsel, cpus, threads);
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -0200680}
681
Arnaldo Carvalho de Melof08199d2011-01-11 23:42:19 -0200682int perf_evsel__open_per_cpu(struct perf_evsel *evsel,
Jiri Olsa6a4bb042012-08-08 12:22:36 +0200683 struct cpu_map *cpus)
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -0200684{
Jiri Olsa6a4bb042012-08-08 12:22:36 +0200685 return __perf_evsel__open(evsel, cpus, &empty_thread_map.map);
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -0200686}
687
Arnaldo Carvalho de Melof08199d2011-01-11 23:42:19 -0200688int perf_evsel__open_per_thread(struct perf_evsel *evsel,
Jiri Olsa6a4bb042012-08-08 12:22:36 +0200689 struct thread_map *threads)
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -0200690{
Jiri Olsa6a4bb042012-08-08 12:22:36 +0200691 return __perf_evsel__open(evsel, &empty_cpu_map.map, threads);
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -0200692}
Arnaldo Carvalho de Melo70082dd2011-01-12 17:03:24 -0200693
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200694static int perf_event__parse_id_sample(const union perf_event *event, u64 type,
Jiri Olsa37073f92012-05-30 14:23:44 +0200695 struct perf_sample *sample,
696 bool swapped)
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -0200697{
698 const u64 *array = event->sample.array;
Jiri Olsa37073f92012-05-30 14:23:44 +0200699 union u64_swap u;
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -0200700
701 array += ((event->header.size -
702 sizeof(event->header)) / sizeof(u64)) - 1;
703
704 if (type & PERF_SAMPLE_CPU) {
Jiri Olsa37073f92012-05-30 14:23:44 +0200705 u.val64 = *array;
706 if (swapped) {
707 /* undo swap of u64, then swap on individual u32s */
708 u.val64 = bswap_64(u.val64);
709 u.val32[0] = bswap_32(u.val32[0]);
710 }
711
712 sample->cpu = u.val32[0];
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -0200713 array--;
714 }
715
716 if (type & PERF_SAMPLE_STREAM_ID) {
717 sample->stream_id = *array;
718 array--;
719 }
720
721 if (type & PERF_SAMPLE_ID) {
722 sample->id = *array;
723 array--;
724 }
725
726 if (type & PERF_SAMPLE_TIME) {
727 sample->time = *array;
728 array--;
729 }
730
731 if (type & PERF_SAMPLE_TID) {
Jiri Olsa37073f92012-05-30 14:23:44 +0200732 u.val64 = *array;
733 if (swapped) {
734 /* undo swap of u64, then swap on individual u32s */
735 u.val64 = bswap_64(u.val64);
736 u.val32[0] = bswap_32(u.val32[0]);
737 u.val32[1] = bswap_32(u.val32[1]);
738 }
739
740 sample->pid = u.val32[0];
741 sample->tid = u.val32[1];
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -0200742 }
743
744 return 0;
745}
746
Frederic Weisbecker98e1da92011-05-21 20:08:15 +0200747static bool sample_overlap(const union perf_event *event,
748 const void *offset, u64 size)
749{
750 const void *base = event;
751
752 if (offset + size > base + event->header.size)
753 return true;
754
755 return false;
756}
757
Arnaldo Carvalho de Meloa3f698f2012-08-02 12:23:46 -0300758int perf_evsel__parse_sample(struct perf_evsel *evsel, union perf_event *event,
David Ahern936be502011-09-06 09:12:26 -0600759 struct perf_sample *data, bool swapped)
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -0200760{
Arnaldo Carvalho de Meloa3f698f2012-08-02 12:23:46 -0300761 u64 type = evsel->attr.sample_type;
Jiri Olsa0f6a3012012-08-07 15:20:45 +0200762 u64 regs_user = evsel->attr.sample_regs_user;
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -0200763 const u64 *array;
764
David Ahern936be502011-09-06 09:12:26 -0600765 /*
766 * used for cross-endian analysis. See git commit 65014ab3
767 * for why this goofiness is needed.
768 */
Jiri Olsa6a11f922012-05-16 08:59:04 +0200769 union u64_swap u;
David Ahern936be502011-09-06 09:12:26 -0600770
Robert Richterf3bda2c2011-12-15 17:32:39 +0100771 memset(data, 0, sizeof(*data));
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -0200772 data->cpu = data->pid = data->tid = -1;
773 data->stream_id = data->id = data->time = -1ULL;
Naveen N. Raoa4a03fc2012-02-03 22:31:13 +0530774 data->period = 1;
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -0200775
776 if (event->header.type != PERF_RECORD_SAMPLE) {
Arnaldo Carvalho de Meloa3f698f2012-08-02 12:23:46 -0300777 if (!evsel->attr.sample_id_all)
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -0200778 return 0;
Jiri Olsa37073f92012-05-30 14:23:44 +0200779 return perf_event__parse_id_sample(event, type, data, swapped);
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -0200780 }
781
782 array = event->sample.array;
783
Arnaldo Carvalho de Meloa3f698f2012-08-02 12:23:46 -0300784 if (evsel->sample_size + sizeof(event->header) > event->header.size)
Frederic Weisbeckera2854122011-05-21 19:33:04 +0200785 return -EFAULT;
786
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -0200787 if (type & PERF_SAMPLE_IP) {
788 data->ip = event->ip.ip;
789 array++;
790 }
791
792 if (type & PERF_SAMPLE_TID) {
David Ahern936be502011-09-06 09:12:26 -0600793 u.val64 = *array;
794 if (swapped) {
795 /* undo swap of u64, then swap on individual u32s */
796 u.val64 = bswap_64(u.val64);
797 u.val32[0] = bswap_32(u.val32[0]);
798 u.val32[1] = bswap_32(u.val32[1]);
799 }
800
801 data->pid = u.val32[0];
802 data->tid = u.val32[1];
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -0200803 array++;
804 }
805
806 if (type & PERF_SAMPLE_TIME) {
807 data->time = *array;
808 array++;
809 }
810
David Ahern7cec0922011-05-30 13:08:23 -0600811 data->addr = 0;
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -0200812 if (type & PERF_SAMPLE_ADDR) {
813 data->addr = *array;
814 array++;
815 }
816
817 data->id = -1ULL;
818 if (type & PERF_SAMPLE_ID) {
819 data->id = *array;
820 array++;
821 }
822
823 if (type & PERF_SAMPLE_STREAM_ID) {
824 data->stream_id = *array;
825 array++;
826 }
827
828 if (type & PERF_SAMPLE_CPU) {
David Ahern936be502011-09-06 09:12:26 -0600829
830 u.val64 = *array;
831 if (swapped) {
832 /* undo swap of u64, then swap on individual u32s */
833 u.val64 = bswap_64(u.val64);
834 u.val32[0] = bswap_32(u.val32[0]);
835 }
836
837 data->cpu = u.val32[0];
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -0200838 array++;
839 }
840
841 if (type & PERF_SAMPLE_PERIOD) {
842 data->period = *array;
843 array++;
844 }
845
846 if (type & PERF_SAMPLE_READ) {
Masanari Iidaf9d36992012-01-25 15:20:40 +0100847 fprintf(stderr, "PERF_SAMPLE_READ is unsupported for now\n");
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -0200848 return -1;
849 }
850
851 if (type & PERF_SAMPLE_CALLCHAIN) {
Frederic Weisbecker98e1da92011-05-21 20:08:15 +0200852 if (sample_overlap(event, array, sizeof(data->callchain->nr)))
853 return -EFAULT;
854
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -0200855 data->callchain = (struct ip_callchain *)array;
Frederic Weisbecker98e1da92011-05-21 20:08:15 +0200856
857 if (sample_overlap(event, array, data->callchain->nr))
858 return -EFAULT;
859
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -0200860 array += 1 + data->callchain->nr;
861 }
862
863 if (type & PERF_SAMPLE_RAW) {
Jiri Olsa8e303f22011-09-29 17:05:08 +0200864 const u64 *pdata;
865
David Ahern936be502011-09-06 09:12:26 -0600866 u.val64 = *array;
867 if (WARN_ONCE(swapped,
868 "Endianness of raw data not corrected!\n")) {
869 /* undo swap of u64, then swap on individual u32s */
870 u.val64 = bswap_64(u.val64);
871 u.val32[0] = bswap_32(u.val32[0]);
872 u.val32[1] = bswap_32(u.val32[1]);
873 }
Frederic Weisbecker98e1da92011-05-21 20:08:15 +0200874
875 if (sample_overlap(event, array, sizeof(u32)))
876 return -EFAULT;
877
David Ahern936be502011-09-06 09:12:26 -0600878 data->raw_size = u.val32[0];
Jiri Olsa8e303f22011-09-29 17:05:08 +0200879 pdata = (void *) array + sizeof(u32);
Frederic Weisbecker98e1da92011-05-21 20:08:15 +0200880
Jiri Olsa8e303f22011-09-29 17:05:08 +0200881 if (sample_overlap(event, pdata, data->raw_size))
Frederic Weisbecker98e1da92011-05-21 20:08:15 +0200882 return -EFAULT;
883
Jiri Olsa8e303f22011-09-29 17:05:08 +0200884 data->raw_data = (void *) pdata;
Stephane Eranianfa30c962012-03-17 23:23:18 +0100885
886 array = (void *)array + data->raw_size + sizeof(u32);
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -0200887 }
888
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100889 if (type & PERF_SAMPLE_BRANCH_STACK) {
890 u64 sz;
891
892 data->branch_stack = (struct branch_stack *)array;
893 array++; /* nr */
894
895 sz = data->branch_stack->nr * sizeof(struct branch_entry);
896 sz /= sizeof(u64);
897 array += sz;
898 }
Jiri Olsa0f6a3012012-08-07 15:20:45 +0200899
900 if (type & PERF_SAMPLE_REGS_USER) {
901 /* First u64 tells us if we have any regs in sample. */
902 u64 avail = *array++;
903
904 if (avail) {
905 data->user_regs.regs = (u64 *)array;
906 array += hweight_long(regs_user);
907 }
908 }
909
910 if (type & PERF_SAMPLE_STACK_USER) {
911 u64 size = *array++;
912
913 data->user_stack.offset = ((char *)(array - 1)
914 - (char *) event);
915
916 if (!size) {
917 data->user_stack.size = 0;
918 } else {
919 data->user_stack.data = (char *)array;
920 array += size / sizeof(*array);
921 data->user_stack.size = *array;
922 }
923 }
924
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -0200925 return 0;
926}
Andrew Vagin74eec262011-11-28 12:03:31 +0300927
928int perf_event__synthesize_sample(union perf_event *event, u64 type,
929 const struct perf_sample *sample,
930 bool swapped)
931{
932 u64 *array;
933
934 /*
935 * used for cross-endian analysis. See git commit 65014ab3
936 * for why this goofiness is needed.
937 */
Jiri Olsa6a11f922012-05-16 08:59:04 +0200938 union u64_swap u;
Andrew Vagin74eec262011-11-28 12:03:31 +0300939
940 array = event->sample.array;
941
942 if (type & PERF_SAMPLE_IP) {
943 event->ip.ip = sample->ip;
944 array++;
945 }
946
947 if (type & PERF_SAMPLE_TID) {
948 u.val32[0] = sample->pid;
949 u.val32[1] = sample->tid;
950 if (swapped) {
951 /*
Arnaldo Carvalho de Meloa3f698f2012-08-02 12:23:46 -0300952 * Inverse of what is done in perf_evsel__parse_sample
Andrew Vagin74eec262011-11-28 12:03:31 +0300953 */
954 u.val32[0] = bswap_32(u.val32[0]);
955 u.val32[1] = bswap_32(u.val32[1]);
956 u.val64 = bswap_64(u.val64);
957 }
958
959 *array = u.val64;
960 array++;
961 }
962
963 if (type & PERF_SAMPLE_TIME) {
964 *array = sample->time;
965 array++;
966 }
967
968 if (type & PERF_SAMPLE_ADDR) {
969 *array = sample->addr;
970 array++;
971 }
972
973 if (type & PERF_SAMPLE_ID) {
974 *array = sample->id;
975 array++;
976 }
977
978 if (type & PERF_SAMPLE_STREAM_ID) {
979 *array = sample->stream_id;
980 array++;
981 }
982
983 if (type & PERF_SAMPLE_CPU) {
984 u.val32[0] = sample->cpu;
985 if (swapped) {
986 /*
Arnaldo Carvalho de Meloa3f698f2012-08-02 12:23:46 -0300987 * Inverse of what is done in perf_evsel__parse_sample
Andrew Vagin74eec262011-11-28 12:03:31 +0300988 */
989 u.val32[0] = bswap_32(u.val32[0]);
990 u.val64 = bswap_64(u.val64);
991 }
992 *array = u.val64;
993 array++;
994 }
995
996 if (type & PERF_SAMPLE_PERIOD) {
997 *array = sample->period;
998 array++;
999 }
1000
1001 return 0;
1002}