blob: f5b68e73d75174ca7ab40165266b5dedf7ab18b2 [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 Meloc4104312012-05-25 16:38:11 -030071static const char *perf_evsel__hw_names[PERF_COUNT_HW_MAX] = {
72 "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 Melo335c2f52012-06-11 14:36:20 -0300134static const char *perf_evsel__sw_names[PERF_COUNT_SW_MAX] = {
135 "cpu-clock",
136 "task-clock",
137 "page-faults",
138 "context-switches",
139 "CPU-migrations",
140 "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:
Arnaldo Carvalho de Melo7289f832012-06-12 12:34:58 -0300322 scnprintf(bf, sizeof(bf), "%s", "unknown attr type");
Arnaldo Carvalho de Meloa4460832012-06-12 10:29:12 -0300323 break;
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300324 }
325
Arnaldo Carvalho de Melo7289f832012-06-12 12:34:58 -0300326 evsel->name = strdup(bf);
327
328 return evsel->name ?: "unknown";
Arnaldo Carvalho de Meloc4104312012-05-25 16:38:11 -0300329}
330
Namhyung Kim5090c6a2012-03-16 17:42:20 +0900331void perf_evsel__config(struct perf_evsel *evsel, struct perf_record_opts *opts,
332 struct perf_evsel *first)
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200333{
334 struct perf_event_attr *attr = &evsel->attr;
335 int track = !evsel->idx; /* only the first counter needs these */
336
David Ahern5e1c81d2012-05-13 22:01:28 -0600337 attr->disabled = 1;
Arnaldo Carvalho de Melo808e1222012-02-14 14:18:57 -0200338 attr->sample_id_all = opts->sample_id_all_missing ? 0 : 1;
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200339 attr->inherit = !opts->no_inherit;
340 attr->read_format = PERF_FORMAT_TOTAL_TIME_ENABLED |
341 PERF_FORMAT_TOTAL_TIME_RUNNING |
342 PERF_FORMAT_ID;
343
344 attr->sample_type |= PERF_SAMPLE_IP | PERF_SAMPLE_TID;
345
346 /*
347 * We default some events to a 1 default interval. But keep
348 * it a weak assumption overridable by the user.
349 */
350 if (!attr->sample_period || (opts->user_freq != UINT_MAX &&
351 opts->user_interval != ULLONG_MAX)) {
352 if (opts->freq) {
353 attr->sample_type |= PERF_SAMPLE_PERIOD;
354 attr->freq = 1;
355 attr->sample_freq = opts->freq;
356 } else {
357 attr->sample_period = opts->default_interval;
358 }
359 }
360
361 if (opts->no_samples)
362 attr->sample_freq = 0;
363
364 if (opts->inherit_stat)
365 attr->inherit_stat = 1;
366
367 if (opts->sample_address) {
368 attr->sample_type |= PERF_SAMPLE_ADDR;
369 attr->mmap_data = track;
370 }
371
Jiri Olsa26d33022012-08-07 15:20:47 +0200372 if (opts->call_graph) {
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200373 attr->sample_type |= PERF_SAMPLE_CALLCHAIN;
374
Jiri Olsa26d33022012-08-07 15:20:47 +0200375 if (opts->call_graph == CALLCHAIN_DWARF) {
376 attr->sample_type |= PERF_SAMPLE_REGS_USER |
377 PERF_SAMPLE_STACK_USER;
378 attr->sample_regs_user = PERF_REGS_MASK;
379 attr->sample_stack_user = opts->stack_dump_size;
380 attr->exclude_callchain_user = 1;
381 }
382 }
383
Namhyung Kime40ee742012-05-21 10:42:07 +0900384 if (perf_target__has_cpu(&opts->target))
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200385 attr->sample_type |= PERF_SAMPLE_CPU;
386
Andrew Vagin3e76ac72011-12-20 17:32:45 +0300387 if (opts->period)
388 attr->sample_type |= PERF_SAMPLE_PERIOD;
389
Arnaldo Carvalho de Melo808e1222012-02-14 14:18:57 -0200390 if (!opts->sample_id_all_missing &&
Namhyung Kimd67356e2012-05-07 14:09:03 +0900391 (opts->sample_time || !opts->no_inherit ||
Namhyung Kimaa22dd42012-05-16 18:45:47 +0900392 perf_target__has_cpu(&opts->target)))
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200393 attr->sample_type |= PERF_SAMPLE_TIME;
394
395 if (opts->raw_samples) {
396 attr->sample_type |= PERF_SAMPLE_TIME;
397 attr->sample_type |= PERF_SAMPLE_RAW;
398 attr->sample_type |= PERF_SAMPLE_CPU;
399 }
400
401 if (opts->no_delay) {
402 attr->watermark = 0;
403 attr->wakeup_events = 1;
404 }
Roberto Agostino Vitillobdfebd82012-02-09 23:21:02 +0100405 if (opts->branch_stack) {
406 attr->sample_type |= PERF_SAMPLE_BRANCH_STACK;
407 attr->branch_sample_type = opts->branch_stack;
408 }
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200409
410 attr->mmap = track;
411 attr->comm = track;
412
Namhyung Kimd67356e2012-05-07 14:09:03 +0900413 if (perf_target__none(&opts->target) &&
414 (!opts->group || evsel == first)) {
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -0200415 attr->enable_on_exec = 1;
416 }
417}
418
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200419int perf_evsel__alloc_fd(struct perf_evsel *evsel, int ncpus, int nthreads)
420{
David Ahern4af4c952011-05-27 09:58:34 -0600421 int cpu, thread;
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200422 evsel->fd = xyarray__new(ncpus, nthreads, sizeof(int));
David Ahern4af4c952011-05-27 09:58:34 -0600423
424 if (evsel->fd) {
425 for (cpu = 0; cpu < ncpus; cpu++) {
426 for (thread = 0; thread < nthreads; thread++) {
427 FD(evsel, cpu, thread) = -1;
428 }
429 }
430 }
431
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200432 return evsel->fd != NULL ? 0 : -ENOMEM;
433}
434
Arnaldo Carvalho de Melo70db7532011-01-12 22:39:13 -0200435int perf_evsel__alloc_id(struct perf_evsel *evsel, int ncpus, int nthreads)
436{
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -0300437 evsel->sample_id = xyarray__new(ncpus, nthreads, sizeof(struct perf_sample_id));
438 if (evsel->sample_id == NULL)
439 return -ENOMEM;
440
441 evsel->id = zalloc(ncpus * nthreads * sizeof(u64));
442 if (evsel->id == NULL) {
443 xyarray__delete(evsel->sample_id);
444 evsel->sample_id = NULL;
445 return -ENOMEM;
446 }
447
448 return 0;
Arnaldo Carvalho de Melo70db7532011-01-12 22:39:13 -0200449}
450
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200451int perf_evsel__alloc_counts(struct perf_evsel *evsel, int ncpus)
452{
453 evsel->counts = zalloc((sizeof(*evsel->counts) +
454 (ncpus * sizeof(struct perf_counts_values))));
455 return evsel->counts != NULL ? 0 : -ENOMEM;
456}
457
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200458void perf_evsel__free_fd(struct perf_evsel *evsel)
459{
460 xyarray__delete(evsel->fd);
461 evsel->fd = NULL;
462}
463
Arnaldo Carvalho de Melo70db7532011-01-12 22:39:13 -0200464void perf_evsel__free_id(struct perf_evsel *evsel)
465{
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -0300466 xyarray__delete(evsel->sample_id);
467 evsel->sample_id = NULL;
468 free(evsel->id);
Arnaldo Carvalho de Melo70db7532011-01-12 22:39:13 -0200469 evsel->id = NULL;
470}
471
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200472void perf_evsel__close_fd(struct perf_evsel *evsel, int ncpus, int nthreads)
473{
474 int cpu, thread;
475
476 for (cpu = 0; cpu < ncpus; cpu++)
477 for (thread = 0; thread < nthreads; ++thread) {
478 close(FD(evsel, cpu, thread));
479 FD(evsel, cpu, thread) = -1;
480 }
481}
482
Arnaldo Carvalho de Meloef1d1af2011-01-18 21:41:45 -0200483void perf_evsel__exit(struct perf_evsel *evsel)
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200484{
485 assert(list_empty(&evsel->node));
486 xyarray__delete(evsel->fd);
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -0300487 xyarray__delete(evsel->sample_id);
488 free(evsel->id);
Arnaldo Carvalho de Meloef1d1af2011-01-18 21:41:45 -0200489}
490
491void perf_evsel__delete(struct perf_evsel *evsel)
492{
493 perf_evsel__exit(evsel);
Stephane Eranian023695d2011-02-14 11:20:01 +0200494 close_cgroup(evsel->cgrp);
Jiri Olsa6a4bb042012-08-08 12:22:36 +0200495 free(evsel->group_name);
Stephane Eranianf0c55bc2011-02-16 15:10:01 +0200496 free(evsel->name);
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200497 free(evsel);
498}
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200499
500int __perf_evsel__read_on_cpu(struct perf_evsel *evsel,
501 int cpu, int thread, bool scale)
502{
503 struct perf_counts_values count;
504 size_t nv = scale ? 3 : 1;
505
506 if (FD(evsel, cpu, thread) < 0)
507 return -EINVAL;
508
Arnaldo Carvalho de Melo4eed11d2011-01-04 00:13:17 -0200509 if (evsel->counts == NULL && perf_evsel__alloc_counts(evsel, cpu + 1) < 0)
510 return -ENOMEM;
511
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200512 if (readn(FD(evsel, cpu, thread), &count, nv * sizeof(u64)) < 0)
513 return -errno;
514
515 if (scale) {
516 if (count.run == 0)
517 count.val = 0;
518 else if (count.run < count.ena)
519 count.val = (u64)((double)count.val * count.ena / count.run + 0.5);
520 } else
521 count.ena = count.run = 0;
522
523 evsel->counts->cpu[cpu] = count;
524 return 0;
525}
526
527int __perf_evsel__read(struct perf_evsel *evsel,
528 int ncpus, int nthreads, bool scale)
529{
530 size_t nv = scale ? 3 : 1;
531 int cpu, thread;
532 struct perf_counts_values *aggr = &evsel->counts->aggr, count;
533
Arnaldo Carvalho de Melo52bcd9942011-02-03 17:26:06 -0200534 aggr->val = aggr->ena = aggr->run = 0;
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200535
536 for (cpu = 0; cpu < ncpus; cpu++) {
537 for (thread = 0; thread < nthreads; thread++) {
538 if (FD(evsel, cpu, thread) < 0)
539 continue;
540
541 if (readn(FD(evsel, cpu, thread),
542 &count, nv * sizeof(u64)) < 0)
543 return -errno;
544
545 aggr->val += count.val;
546 if (scale) {
547 aggr->ena += count.ena;
548 aggr->run += count.run;
549 }
550 }
551 }
552
553 evsel->counts->scaled = 0;
554 if (scale) {
555 if (aggr->run == 0) {
556 evsel->counts->scaled = -1;
557 aggr->val = 0;
558 return 0;
559 }
560
561 if (aggr->run < aggr->ena) {
562 evsel->counts->scaled = 1;
563 aggr->val = (u64)((double)aggr->val * aggr->ena / aggr->run + 0.5);
564 }
565 } else
566 aggr->ena = aggr->run = 0;
567
568 return 0;
569}
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -0200570
Jiri Olsa6a4bb042012-08-08 12:22:36 +0200571static int get_group_fd(struct perf_evsel *evsel, int cpu, int thread)
572{
573 struct perf_evsel *leader = evsel->leader;
574 int fd;
575
576 if (!leader)
577 return -1;
578
579 /*
580 * Leader must be already processed/open,
581 * if not it's a bug.
582 */
583 BUG_ON(!leader->fd);
584
585 fd = FD(leader, cpu, thread);
586 BUG_ON(fd == -1);
587
588 return fd;
589}
590
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -0200591static int __perf_evsel__open(struct perf_evsel *evsel, struct cpu_map *cpus,
Jiri Olsa6a4bb042012-08-08 12:22:36 +0200592 struct thread_map *threads)
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -0200593{
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -0200594 int cpu, thread;
Stephane Eranian023695d2011-02-14 11:20:01 +0200595 unsigned long flags = 0;
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -0200596 int pid = -1, err;
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -0200597
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -0200598 if (evsel->fd == NULL &&
599 perf_evsel__alloc_fd(evsel, cpus->nr, threads->nr) < 0)
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -0200600 return -ENOMEM;
Arnaldo Carvalho de Melo4eed11d2011-01-04 00:13:17 -0200601
Stephane Eranian023695d2011-02-14 11:20:01 +0200602 if (evsel->cgrp) {
603 flags = PERF_FLAG_PID_CGROUP;
604 pid = evsel->cgrp->fd;
605 }
606
Arnaldo Carvalho de Melo86bd5e82011-01-03 23:09:46 -0200607 for (cpu = 0; cpu < cpus->nr; cpu++) {
Arnaldo Carvalho de Melo9d04f172011-01-12 00:08:18 -0200608
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -0200609 for (thread = 0; thread < threads->nr; thread++) {
Jiri Olsa6a4bb042012-08-08 12:22:36 +0200610 int group_fd;
Stephane Eranian023695d2011-02-14 11:20:01 +0200611
612 if (!evsel->cgrp)
613 pid = threads->map[thread];
614
Jiri Olsa6a4bb042012-08-08 12:22:36 +0200615 group_fd = get_group_fd(evsel, cpu, thread);
616
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -0200617 FD(evsel, cpu, thread) = sys_perf_event_open(&evsel->attr,
Stephane Eranian023695d2011-02-14 11:20:01 +0200618 pid,
Arnaldo Carvalho de Melof08199d2011-01-11 23:42:19 -0200619 cpus->map[cpu],
Stephane Eranian023695d2011-02-14 11:20:01 +0200620 group_fd, flags);
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -0200621 if (FD(evsel, cpu, thread) < 0) {
622 err = -errno;
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -0200623 goto out_close;
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -0200624 }
Arnaldo Carvalho de Melof08199d2011-01-11 23:42:19 -0200625
Jiri Olsa6a4bb042012-08-08 12:22:36 +0200626 pr_debug("event cpu %d, thread %d, fd %d, group %d\n",
627 cpu, pid, FD(evsel, cpu, thread),
628 group_fd);
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -0200629 }
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -0200630 }
631
632 return 0;
633
634out_close:
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -0200635 do {
636 while (--thread >= 0) {
637 close(FD(evsel, cpu, thread));
638 FD(evsel, cpu, thread) = -1;
639 }
640 thread = threads->nr;
641 } while (--cpu >= 0);
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -0200642 return err;
643}
644
645void perf_evsel__close(struct perf_evsel *evsel, int ncpus, int nthreads)
646{
647 if (evsel->fd == NULL)
648 return;
649
650 perf_evsel__close_fd(evsel, ncpus, nthreads);
651 perf_evsel__free_fd(evsel);
652 evsel->fd = NULL;
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -0200653}
654
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -0200655static struct {
656 struct cpu_map map;
657 int cpus[1];
658} empty_cpu_map = {
659 .map.nr = 1,
660 .cpus = { -1, },
661};
662
663static struct {
664 struct thread_map map;
665 int threads[1];
666} empty_thread_map = {
667 .map.nr = 1,
668 .threads = { -1, },
669};
670
Arnaldo Carvalho de Melof08199d2011-01-11 23:42:19 -0200671int perf_evsel__open(struct perf_evsel *evsel, struct cpu_map *cpus,
Jiri Olsa6a4bb042012-08-08 12:22:36 +0200672 struct thread_map *threads)
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -0200673{
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -0200674 if (cpus == NULL) {
675 /* Work around old compiler warnings about strict aliasing */
676 cpus = &empty_cpu_map.map;
677 }
678
679 if (threads == NULL)
680 threads = &empty_thread_map.map;
681
Jiri Olsa6a4bb042012-08-08 12:22:36 +0200682 return __perf_evsel__open(evsel, cpus, threads);
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -0200683}
684
Arnaldo Carvalho de Melof08199d2011-01-11 23:42:19 -0200685int perf_evsel__open_per_cpu(struct perf_evsel *evsel,
Jiri Olsa6a4bb042012-08-08 12:22:36 +0200686 struct cpu_map *cpus)
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -0200687{
Jiri Olsa6a4bb042012-08-08 12:22:36 +0200688 return __perf_evsel__open(evsel, cpus, &empty_thread_map.map);
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -0200689}
690
Arnaldo Carvalho de Melof08199d2011-01-11 23:42:19 -0200691int perf_evsel__open_per_thread(struct perf_evsel *evsel,
Jiri Olsa6a4bb042012-08-08 12:22:36 +0200692 struct thread_map *threads)
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -0200693{
Jiri Olsa6a4bb042012-08-08 12:22:36 +0200694 return __perf_evsel__open(evsel, &empty_cpu_map.map, threads);
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -0200695}
Arnaldo Carvalho de Melo70082dd2011-01-12 17:03:24 -0200696
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200697static int perf_event__parse_id_sample(const union perf_event *event, u64 type,
Jiri Olsa37073f92012-05-30 14:23:44 +0200698 struct perf_sample *sample,
699 bool swapped)
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -0200700{
701 const u64 *array = event->sample.array;
Jiri Olsa37073f92012-05-30 14:23:44 +0200702 union u64_swap u;
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -0200703
704 array += ((event->header.size -
705 sizeof(event->header)) / sizeof(u64)) - 1;
706
707 if (type & PERF_SAMPLE_CPU) {
Jiri Olsa37073f92012-05-30 14:23:44 +0200708 u.val64 = *array;
709 if (swapped) {
710 /* undo swap of u64, then swap on individual u32s */
711 u.val64 = bswap_64(u.val64);
712 u.val32[0] = bswap_32(u.val32[0]);
713 }
714
715 sample->cpu = u.val32[0];
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -0200716 array--;
717 }
718
719 if (type & PERF_SAMPLE_STREAM_ID) {
720 sample->stream_id = *array;
721 array--;
722 }
723
724 if (type & PERF_SAMPLE_ID) {
725 sample->id = *array;
726 array--;
727 }
728
729 if (type & PERF_SAMPLE_TIME) {
730 sample->time = *array;
731 array--;
732 }
733
734 if (type & PERF_SAMPLE_TID) {
Jiri Olsa37073f92012-05-30 14:23:44 +0200735 u.val64 = *array;
736 if (swapped) {
737 /* undo swap of u64, then swap on individual u32s */
738 u.val64 = bswap_64(u.val64);
739 u.val32[0] = bswap_32(u.val32[0]);
740 u.val32[1] = bswap_32(u.val32[1]);
741 }
742
743 sample->pid = u.val32[0];
744 sample->tid = u.val32[1];
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -0200745 }
746
747 return 0;
748}
749
Frederic Weisbecker98e1da92011-05-21 20:08:15 +0200750static bool sample_overlap(const union perf_event *event,
751 const void *offset, u64 size)
752{
753 const void *base = event;
754
755 if (offset + size > base + event->header.size)
756 return true;
757
758 return false;
759}
760
Arnaldo Carvalho de Meloa3f698f2012-08-02 12:23:46 -0300761int perf_evsel__parse_sample(struct perf_evsel *evsel, union perf_event *event,
David Ahern936be502011-09-06 09:12:26 -0600762 struct perf_sample *data, bool swapped)
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -0200763{
Arnaldo Carvalho de Meloa3f698f2012-08-02 12:23:46 -0300764 u64 type = evsel->attr.sample_type;
Jiri Olsa0f6a3012012-08-07 15:20:45 +0200765 u64 regs_user = evsel->attr.sample_regs_user;
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -0200766 const u64 *array;
767
David Ahern936be502011-09-06 09:12:26 -0600768 /*
769 * used for cross-endian analysis. See git commit 65014ab3
770 * for why this goofiness is needed.
771 */
Jiri Olsa6a11f922012-05-16 08:59:04 +0200772 union u64_swap u;
David Ahern936be502011-09-06 09:12:26 -0600773
Robert Richterf3bda2c2011-12-15 17:32:39 +0100774 memset(data, 0, sizeof(*data));
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -0200775 data->cpu = data->pid = data->tid = -1;
776 data->stream_id = data->id = data->time = -1ULL;
Naveen N. Raoa4a03fc2012-02-03 22:31:13 +0530777 data->period = 1;
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -0200778
779 if (event->header.type != PERF_RECORD_SAMPLE) {
Arnaldo Carvalho de Meloa3f698f2012-08-02 12:23:46 -0300780 if (!evsel->attr.sample_id_all)
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -0200781 return 0;
Jiri Olsa37073f92012-05-30 14:23:44 +0200782 return perf_event__parse_id_sample(event, type, data, swapped);
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -0200783 }
784
785 array = event->sample.array;
786
Arnaldo Carvalho de Meloa3f698f2012-08-02 12:23:46 -0300787 if (evsel->sample_size + sizeof(event->header) > event->header.size)
Frederic Weisbeckera2854122011-05-21 19:33:04 +0200788 return -EFAULT;
789
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -0200790 if (type & PERF_SAMPLE_IP) {
791 data->ip = event->ip.ip;
792 array++;
793 }
794
795 if (type & PERF_SAMPLE_TID) {
David Ahern936be502011-09-06 09:12:26 -0600796 u.val64 = *array;
797 if (swapped) {
798 /* undo swap of u64, then swap on individual u32s */
799 u.val64 = bswap_64(u.val64);
800 u.val32[0] = bswap_32(u.val32[0]);
801 u.val32[1] = bswap_32(u.val32[1]);
802 }
803
804 data->pid = u.val32[0];
805 data->tid = u.val32[1];
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -0200806 array++;
807 }
808
809 if (type & PERF_SAMPLE_TIME) {
810 data->time = *array;
811 array++;
812 }
813
David Ahern7cec0922011-05-30 13:08:23 -0600814 data->addr = 0;
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -0200815 if (type & PERF_SAMPLE_ADDR) {
816 data->addr = *array;
817 array++;
818 }
819
820 data->id = -1ULL;
821 if (type & PERF_SAMPLE_ID) {
822 data->id = *array;
823 array++;
824 }
825
826 if (type & PERF_SAMPLE_STREAM_ID) {
827 data->stream_id = *array;
828 array++;
829 }
830
831 if (type & PERF_SAMPLE_CPU) {
David Ahern936be502011-09-06 09:12:26 -0600832
833 u.val64 = *array;
834 if (swapped) {
835 /* undo swap of u64, then swap on individual u32s */
836 u.val64 = bswap_64(u.val64);
837 u.val32[0] = bswap_32(u.val32[0]);
838 }
839
840 data->cpu = u.val32[0];
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -0200841 array++;
842 }
843
844 if (type & PERF_SAMPLE_PERIOD) {
845 data->period = *array;
846 array++;
847 }
848
849 if (type & PERF_SAMPLE_READ) {
Masanari Iidaf9d36992012-01-25 15:20:40 +0100850 fprintf(stderr, "PERF_SAMPLE_READ is unsupported for now\n");
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -0200851 return -1;
852 }
853
854 if (type & PERF_SAMPLE_CALLCHAIN) {
Frederic Weisbecker98e1da92011-05-21 20:08:15 +0200855 if (sample_overlap(event, array, sizeof(data->callchain->nr)))
856 return -EFAULT;
857
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -0200858 data->callchain = (struct ip_callchain *)array;
Frederic Weisbecker98e1da92011-05-21 20:08:15 +0200859
860 if (sample_overlap(event, array, data->callchain->nr))
861 return -EFAULT;
862
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -0200863 array += 1 + data->callchain->nr;
864 }
865
866 if (type & PERF_SAMPLE_RAW) {
Jiri Olsa8e303f22011-09-29 17:05:08 +0200867 const u64 *pdata;
868
David Ahern936be502011-09-06 09:12:26 -0600869 u.val64 = *array;
870 if (WARN_ONCE(swapped,
871 "Endianness of raw data not corrected!\n")) {
872 /* undo swap of u64, then swap on individual u32s */
873 u.val64 = bswap_64(u.val64);
874 u.val32[0] = bswap_32(u.val32[0]);
875 u.val32[1] = bswap_32(u.val32[1]);
876 }
Frederic Weisbecker98e1da92011-05-21 20:08:15 +0200877
878 if (sample_overlap(event, array, sizeof(u32)))
879 return -EFAULT;
880
David Ahern936be502011-09-06 09:12:26 -0600881 data->raw_size = u.val32[0];
Jiri Olsa8e303f22011-09-29 17:05:08 +0200882 pdata = (void *) array + sizeof(u32);
Frederic Weisbecker98e1da92011-05-21 20:08:15 +0200883
Jiri Olsa8e303f22011-09-29 17:05:08 +0200884 if (sample_overlap(event, pdata, data->raw_size))
Frederic Weisbecker98e1da92011-05-21 20:08:15 +0200885 return -EFAULT;
886
Jiri Olsa8e303f22011-09-29 17:05:08 +0200887 data->raw_data = (void *) pdata;
Stephane Eranianfa30c962012-03-17 23:23:18 +0100888
889 array = (void *)array + data->raw_size + sizeof(u32);
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -0200890 }
891
Roberto Agostino Vitillob5387522012-02-09 23:21:01 +0100892 if (type & PERF_SAMPLE_BRANCH_STACK) {
893 u64 sz;
894
895 data->branch_stack = (struct branch_stack *)array;
896 array++; /* nr */
897
898 sz = data->branch_stack->nr * sizeof(struct branch_entry);
899 sz /= sizeof(u64);
900 array += sz;
901 }
Jiri Olsa0f6a3012012-08-07 15:20:45 +0200902
903 if (type & PERF_SAMPLE_REGS_USER) {
904 /* First u64 tells us if we have any regs in sample. */
905 u64 avail = *array++;
906
907 if (avail) {
908 data->user_regs.regs = (u64 *)array;
909 array += hweight_long(regs_user);
910 }
911 }
912
913 if (type & PERF_SAMPLE_STACK_USER) {
914 u64 size = *array++;
915
916 data->user_stack.offset = ((char *)(array - 1)
917 - (char *) event);
918
919 if (!size) {
920 data->user_stack.size = 0;
921 } else {
922 data->user_stack.data = (char *)array;
923 array += size / sizeof(*array);
924 data->user_stack.size = *array;
925 }
926 }
927
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -0200928 return 0;
929}
Andrew Vagin74eec262011-11-28 12:03:31 +0300930
931int perf_event__synthesize_sample(union perf_event *event, u64 type,
932 const struct perf_sample *sample,
933 bool swapped)
934{
935 u64 *array;
936
937 /*
938 * used for cross-endian analysis. See git commit 65014ab3
939 * for why this goofiness is needed.
940 */
Jiri Olsa6a11f922012-05-16 08:59:04 +0200941 union u64_swap u;
Andrew Vagin74eec262011-11-28 12:03:31 +0300942
943 array = event->sample.array;
944
945 if (type & PERF_SAMPLE_IP) {
946 event->ip.ip = sample->ip;
947 array++;
948 }
949
950 if (type & PERF_SAMPLE_TID) {
951 u.val32[0] = sample->pid;
952 u.val32[1] = sample->tid;
953 if (swapped) {
954 /*
Arnaldo Carvalho de Meloa3f698f2012-08-02 12:23:46 -0300955 * Inverse of what is done in perf_evsel__parse_sample
Andrew Vagin74eec262011-11-28 12:03:31 +0300956 */
957 u.val32[0] = bswap_32(u.val32[0]);
958 u.val32[1] = bswap_32(u.val32[1]);
959 u.val64 = bswap_64(u.val64);
960 }
961
962 *array = u.val64;
963 array++;
964 }
965
966 if (type & PERF_SAMPLE_TIME) {
967 *array = sample->time;
968 array++;
969 }
970
971 if (type & PERF_SAMPLE_ADDR) {
972 *array = sample->addr;
973 array++;
974 }
975
976 if (type & PERF_SAMPLE_ID) {
977 *array = sample->id;
978 array++;
979 }
980
981 if (type & PERF_SAMPLE_STREAM_ID) {
982 *array = sample->stream_id;
983 array++;
984 }
985
986 if (type & PERF_SAMPLE_CPU) {
987 u.val32[0] = sample->cpu;
988 if (swapped) {
989 /*
Arnaldo Carvalho de Meloa3f698f2012-08-02 12:23:46 -0300990 * Inverse of what is done in perf_evsel__parse_sample
Andrew Vagin74eec262011-11-28 12:03:31 +0300991 */
992 u.val32[0] = bswap_32(u.val32[0]);
993 u.val64 = bswap_64(u.val64);
994 }
995 *array = u.val64;
996 array++;
997 }
998
999 if (type & PERF_SAMPLE_PERIOD) {
1000 *array = sample->period;
1001 array++;
1002 }
1003
1004 return 0;
1005}