blob: 4a8c8b02e9cc4961298477eb4dab10e35f0db16c [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>
11#include "asm/bug.h"
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -020012#include "evsel.h"
Arnaldo Carvalho de Melo70082dd2011-01-12 17:03:24 -020013#include "evlist.h"
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -020014#include "util.h"
Arnaldo Carvalho de Melo86bd5e82011-01-03 23:09:46 -020015#include "cpumap.h"
Arnaldo Carvalho de Melofd782602011-01-18 15:15:24 -020016#include "thread_map.h"
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -020017
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -020018#define FD(e, x, y) (*(int *)xyarray__entry(e->fd, x, y))
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -020019#define GROUP_FD(group_fd, cpu) (*(int *)xyarray__entry(group_fd, cpu, 0))
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -020020
Arnaldo Carvalho de Meloc2a70652011-06-02 11:04:54 -030021int __perf_evsel__sample_size(u64 sample_type)
22{
23 u64 mask = sample_type & PERF_SAMPLE_MASK;
24 int size = 0;
25 int i;
26
27 for (i = 0; i < 64; i++) {
28 if (mask & (1ULL << i))
29 size++;
30 }
31
32 size *= sizeof(u64);
33
34 return size;
35}
36
Arnaldo Carvalho de Melo0e2a5f12011-11-04 08:16:58 -020037static void hists__init(struct hists *hists)
38{
39 memset(hists, 0, sizeof(*hists));
40 hists->entries_in_array[0] = hists->entries_in_array[1] = RB_ROOT;
41 hists->entries_in = &hists->entries_in_array[0];
42 hists->entries_collapsed = RB_ROOT;
43 hists->entries = RB_ROOT;
44 pthread_mutex_init(&hists->lock, NULL);
45}
46
Arnaldo Carvalho de Meloef1d1af2011-01-18 21:41:45 -020047void perf_evsel__init(struct perf_evsel *evsel,
48 struct perf_event_attr *attr, int idx)
49{
50 evsel->idx = idx;
51 evsel->attr = *attr;
52 INIT_LIST_HEAD(&evsel->node);
Arnaldo Carvalho de Melo1980c2eb2011-10-05 17:50:23 -030053 hists__init(&evsel->hists);
Arnaldo Carvalho de Meloef1d1af2011-01-18 21:41:45 -020054}
55
Lin Ming23a2f3a2011-01-07 11:11:09 +080056struct perf_evsel *perf_evsel__new(struct perf_event_attr *attr, int idx)
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -020057{
58 struct perf_evsel *evsel = zalloc(sizeof(*evsel));
59
Arnaldo Carvalho de Meloef1d1af2011-01-18 21:41:45 -020060 if (evsel != NULL)
61 perf_evsel__init(evsel, attr, idx);
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -020062
63 return evsel;
64}
65
Arnaldo Carvalho de Melo0f82ebc2011-11-08 14:41:57 -020066void perf_evsel__config(struct perf_evsel *evsel, struct perf_record_opts *opts)
67{
68 struct perf_event_attr *attr = &evsel->attr;
69 int track = !evsel->idx; /* only the first counter needs these */
70
71 attr->sample_id_all = opts->sample_id_all_avail ? 1 : 0;
72 attr->inherit = !opts->no_inherit;
73 attr->read_format = PERF_FORMAT_TOTAL_TIME_ENABLED |
74 PERF_FORMAT_TOTAL_TIME_RUNNING |
75 PERF_FORMAT_ID;
76
77 attr->sample_type |= PERF_SAMPLE_IP | PERF_SAMPLE_TID;
78
79 /*
80 * We default some events to a 1 default interval. But keep
81 * it a weak assumption overridable by the user.
82 */
83 if (!attr->sample_period || (opts->user_freq != UINT_MAX &&
84 opts->user_interval != ULLONG_MAX)) {
85 if (opts->freq) {
86 attr->sample_type |= PERF_SAMPLE_PERIOD;
87 attr->freq = 1;
88 attr->sample_freq = opts->freq;
89 } else {
90 attr->sample_period = opts->default_interval;
91 }
92 }
93
94 if (opts->no_samples)
95 attr->sample_freq = 0;
96
97 if (opts->inherit_stat)
98 attr->inherit_stat = 1;
99
100 if (opts->sample_address) {
101 attr->sample_type |= PERF_SAMPLE_ADDR;
102 attr->mmap_data = track;
103 }
104
105 if (opts->call_graph)
106 attr->sample_type |= PERF_SAMPLE_CALLCHAIN;
107
108 if (opts->system_wide)
109 attr->sample_type |= PERF_SAMPLE_CPU;
110
111 if (opts->sample_id_all_avail &&
112 (opts->sample_time || opts->system_wide ||
113 !opts->no_inherit || opts->cpu_list))
114 attr->sample_type |= PERF_SAMPLE_TIME;
115
116 if (opts->raw_samples) {
117 attr->sample_type |= PERF_SAMPLE_TIME;
118 attr->sample_type |= PERF_SAMPLE_RAW;
119 attr->sample_type |= PERF_SAMPLE_CPU;
120 }
121
122 if (opts->no_delay) {
123 attr->watermark = 0;
124 attr->wakeup_events = 1;
125 }
126
127 attr->mmap = track;
128 attr->comm = track;
129
130 if (opts->target_pid == -1 && opts->target_tid == -1 && !opts->system_wide) {
131 attr->disabled = 1;
132 attr->enable_on_exec = 1;
133 }
134}
135
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200136int perf_evsel__alloc_fd(struct perf_evsel *evsel, int ncpus, int nthreads)
137{
David Ahern4af4c952011-05-27 09:58:34 -0600138 int cpu, thread;
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200139 evsel->fd = xyarray__new(ncpus, nthreads, sizeof(int));
David Ahern4af4c952011-05-27 09:58:34 -0600140
141 if (evsel->fd) {
142 for (cpu = 0; cpu < ncpus; cpu++) {
143 for (thread = 0; thread < nthreads; thread++) {
144 FD(evsel, cpu, thread) = -1;
145 }
146 }
147 }
148
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200149 return evsel->fd != NULL ? 0 : -ENOMEM;
150}
151
Arnaldo Carvalho de Melo70db7532011-01-12 22:39:13 -0200152int perf_evsel__alloc_id(struct perf_evsel *evsel, int ncpus, int nthreads)
153{
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -0300154 evsel->sample_id = xyarray__new(ncpus, nthreads, sizeof(struct perf_sample_id));
155 if (evsel->sample_id == NULL)
156 return -ENOMEM;
157
158 evsel->id = zalloc(ncpus * nthreads * sizeof(u64));
159 if (evsel->id == NULL) {
160 xyarray__delete(evsel->sample_id);
161 evsel->sample_id = NULL;
162 return -ENOMEM;
163 }
164
165 return 0;
Arnaldo Carvalho de Melo70db7532011-01-12 22:39:13 -0200166}
167
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200168int perf_evsel__alloc_counts(struct perf_evsel *evsel, int ncpus)
169{
170 evsel->counts = zalloc((sizeof(*evsel->counts) +
171 (ncpus * sizeof(struct perf_counts_values))));
172 return evsel->counts != NULL ? 0 : -ENOMEM;
173}
174
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200175void perf_evsel__free_fd(struct perf_evsel *evsel)
176{
177 xyarray__delete(evsel->fd);
178 evsel->fd = NULL;
179}
180
Arnaldo Carvalho de Melo70db7532011-01-12 22:39:13 -0200181void perf_evsel__free_id(struct perf_evsel *evsel)
182{
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -0300183 xyarray__delete(evsel->sample_id);
184 evsel->sample_id = NULL;
185 free(evsel->id);
Arnaldo Carvalho de Melo70db7532011-01-12 22:39:13 -0200186 evsel->id = NULL;
187}
188
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200189void perf_evsel__close_fd(struct perf_evsel *evsel, int ncpus, int nthreads)
190{
191 int cpu, thread;
192
193 for (cpu = 0; cpu < ncpus; cpu++)
194 for (thread = 0; thread < nthreads; ++thread) {
195 close(FD(evsel, cpu, thread));
196 FD(evsel, cpu, thread) = -1;
197 }
198}
199
Arnaldo Carvalho de Meloef1d1af2011-01-18 21:41:45 -0200200void perf_evsel__exit(struct perf_evsel *evsel)
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200201{
202 assert(list_empty(&evsel->node));
203 xyarray__delete(evsel->fd);
Arnaldo Carvalho de Meloa91e5432011-03-10 11:15:54 -0300204 xyarray__delete(evsel->sample_id);
205 free(evsel->id);
Arnaldo Carvalho de Meloef1d1af2011-01-18 21:41:45 -0200206}
207
208void perf_evsel__delete(struct perf_evsel *evsel)
209{
210 perf_evsel__exit(evsel);
Stephane Eranian023695d2011-02-14 11:20:01 +0200211 close_cgroup(evsel->cgrp);
Stephane Eranianf0c55bc2011-02-16 15:10:01 +0200212 free(evsel->name);
Arnaldo Carvalho de Melo69aad6f2011-01-03 16:39:04 -0200213 free(evsel);
214}
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200215
216int __perf_evsel__read_on_cpu(struct perf_evsel *evsel,
217 int cpu, int thread, bool scale)
218{
219 struct perf_counts_values count;
220 size_t nv = scale ? 3 : 1;
221
222 if (FD(evsel, cpu, thread) < 0)
223 return -EINVAL;
224
Arnaldo Carvalho de Melo4eed11d2011-01-04 00:13:17 -0200225 if (evsel->counts == NULL && perf_evsel__alloc_counts(evsel, cpu + 1) < 0)
226 return -ENOMEM;
227
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200228 if (readn(FD(evsel, cpu, thread), &count, nv * sizeof(u64)) < 0)
229 return -errno;
230
231 if (scale) {
232 if (count.run == 0)
233 count.val = 0;
234 else if (count.run < count.ena)
235 count.val = (u64)((double)count.val * count.ena / count.run + 0.5);
236 } else
237 count.ena = count.run = 0;
238
239 evsel->counts->cpu[cpu] = count;
240 return 0;
241}
242
243int __perf_evsel__read(struct perf_evsel *evsel,
244 int ncpus, int nthreads, bool scale)
245{
246 size_t nv = scale ? 3 : 1;
247 int cpu, thread;
248 struct perf_counts_values *aggr = &evsel->counts->aggr, count;
249
Arnaldo Carvalho de Melo52bcd9942011-02-03 17:26:06 -0200250 aggr->val = aggr->ena = aggr->run = 0;
Arnaldo Carvalho de Meloc52b12e2011-01-03 17:45:52 -0200251
252 for (cpu = 0; cpu < ncpus; cpu++) {
253 for (thread = 0; thread < nthreads; thread++) {
254 if (FD(evsel, cpu, thread) < 0)
255 continue;
256
257 if (readn(FD(evsel, cpu, thread),
258 &count, nv * sizeof(u64)) < 0)
259 return -errno;
260
261 aggr->val += count.val;
262 if (scale) {
263 aggr->ena += count.ena;
264 aggr->run += count.run;
265 }
266 }
267 }
268
269 evsel->counts->scaled = 0;
270 if (scale) {
271 if (aggr->run == 0) {
272 evsel->counts->scaled = -1;
273 aggr->val = 0;
274 return 0;
275 }
276
277 if (aggr->run < aggr->ena) {
278 evsel->counts->scaled = 1;
279 aggr->val = (u64)((double)aggr->val * aggr->ena / aggr->run + 0.5);
280 }
281 } else
282 aggr->ena = aggr->run = 0;
283
284 return 0;
285}
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -0200286
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -0200287static int __perf_evsel__open(struct perf_evsel *evsel, struct cpu_map *cpus,
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -0200288 struct thread_map *threads, bool group,
289 struct xyarray *group_fds)
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -0200290{
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -0200291 int cpu, thread;
Stephane Eranian023695d2011-02-14 11:20:01 +0200292 unsigned long flags = 0;
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -0200293 int pid = -1, err;
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -0200294
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -0200295 if (evsel->fd == NULL &&
296 perf_evsel__alloc_fd(evsel, cpus->nr, threads->nr) < 0)
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -0200297 return -ENOMEM;
Arnaldo Carvalho de Melo4eed11d2011-01-04 00:13:17 -0200298
Stephane Eranian023695d2011-02-14 11:20:01 +0200299 if (evsel->cgrp) {
300 flags = PERF_FLAG_PID_CGROUP;
301 pid = evsel->cgrp->fd;
302 }
303
Arnaldo Carvalho de Melo86bd5e82011-01-03 23:09:46 -0200304 for (cpu = 0; cpu < cpus->nr; cpu++) {
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -0200305 int group_fd = group_fds ? GROUP_FD(group_fds, cpu) : -1;
Arnaldo Carvalho de Melo9d04f172011-01-12 00:08:18 -0200306
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -0200307 for (thread = 0; thread < threads->nr; thread++) {
Stephane Eranian023695d2011-02-14 11:20:01 +0200308
309 if (!evsel->cgrp)
310 pid = threads->map[thread];
311
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -0200312 FD(evsel, cpu, thread) = sys_perf_event_open(&evsel->attr,
Stephane Eranian023695d2011-02-14 11:20:01 +0200313 pid,
Arnaldo Carvalho de Melof08199d2011-01-11 23:42:19 -0200314 cpus->map[cpu],
Stephane Eranian023695d2011-02-14 11:20:01 +0200315 group_fd, flags);
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -0200316 if (FD(evsel, cpu, thread) < 0) {
317 err = -errno;
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -0200318 goto out_close;
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -0200319 }
Arnaldo Carvalho de Melof08199d2011-01-11 23:42:19 -0200320
321 if (group && group_fd == -1)
322 group_fd = FD(evsel, cpu, thread);
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -0200323 }
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -0200324 }
325
326 return 0;
327
328out_close:
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -0200329 do {
330 while (--thread >= 0) {
331 close(FD(evsel, cpu, thread));
332 FD(evsel, cpu, thread) = -1;
333 }
334 thread = threads->nr;
335 } while (--cpu >= 0);
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -0200336 return err;
337}
338
339void perf_evsel__close(struct perf_evsel *evsel, int ncpus, int nthreads)
340{
341 if (evsel->fd == NULL)
342 return;
343
344 perf_evsel__close_fd(evsel, ncpus, nthreads);
345 perf_evsel__free_fd(evsel);
346 evsel->fd = NULL;
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -0200347}
348
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -0200349static struct {
350 struct cpu_map map;
351 int cpus[1];
352} empty_cpu_map = {
353 .map.nr = 1,
354 .cpus = { -1, },
355};
356
357static struct {
358 struct thread_map map;
359 int threads[1];
360} empty_thread_map = {
361 .map.nr = 1,
362 .threads = { -1, },
363};
364
Arnaldo Carvalho de Melof08199d2011-01-11 23:42:19 -0200365int perf_evsel__open(struct perf_evsel *evsel, struct cpu_map *cpus,
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -0200366 struct thread_map *threads, bool group,
367 struct xyarray *group_fd)
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -0200368{
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -0200369 if (cpus == NULL) {
370 /* Work around old compiler warnings about strict aliasing */
371 cpus = &empty_cpu_map.map;
372 }
373
374 if (threads == NULL)
375 threads = &empty_thread_map.map;
376
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -0200377 return __perf_evsel__open(evsel, cpus, threads, group, group_fd);
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -0200378}
379
Arnaldo Carvalho de Melof08199d2011-01-11 23:42:19 -0200380int perf_evsel__open_per_cpu(struct perf_evsel *evsel,
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -0200381 struct cpu_map *cpus, bool group,
382 struct xyarray *group_fd)
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -0200383{
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -0200384 return __perf_evsel__open(evsel, cpus, &empty_thread_map.map, group,
385 group_fd);
Arnaldo Carvalho de Melo02522082011-01-04 11:55:27 -0200386}
387
Arnaldo Carvalho de Melof08199d2011-01-11 23:42:19 -0200388int perf_evsel__open_per_thread(struct perf_evsel *evsel,
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -0200389 struct thread_map *threads, bool group,
390 struct xyarray *group_fd)
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -0200391{
Arnaldo Carvalho de Melo727ab042011-10-25 10:42:19 -0200392 return __perf_evsel__open(evsel, &empty_cpu_map.map, threads, group,
393 group_fd);
Arnaldo Carvalho de Melo48290602011-01-03 17:48:12 -0200394}
Arnaldo Carvalho de Melo70082dd2011-01-12 17:03:24 -0200395
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200396static int perf_event__parse_id_sample(const union perf_event *event, u64 type,
397 struct perf_sample *sample)
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -0200398{
399 const u64 *array = event->sample.array;
400
401 array += ((event->header.size -
402 sizeof(event->header)) / sizeof(u64)) - 1;
403
404 if (type & PERF_SAMPLE_CPU) {
405 u32 *p = (u32 *)array;
406 sample->cpu = *p;
407 array--;
408 }
409
410 if (type & PERF_SAMPLE_STREAM_ID) {
411 sample->stream_id = *array;
412 array--;
413 }
414
415 if (type & PERF_SAMPLE_ID) {
416 sample->id = *array;
417 array--;
418 }
419
420 if (type & PERF_SAMPLE_TIME) {
421 sample->time = *array;
422 array--;
423 }
424
425 if (type & PERF_SAMPLE_TID) {
426 u32 *p = (u32 *)array;
427 sample->pid = p[0];
428 sample->tid = p[1];
429 }
430
431 return 0;
432}
433
Frederic Weisbecker98e1da92011-05-21 20:08:15 +0200434static bool sample_overlap(const union perf_event *event,
435 const void *offset, u64 size)
436{
437 const void *base = event;
438
439 if (offset + size > base + event->header.size)
440 return true;
441
442 return false;
443}
444
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200445int perf_event__parse_sample(const union perf_event *event, u64 type,
Frederic Weisbeckera2854122011-05-21 19:33:04 +0200446 int sample_size, bool sample_id_all,
David Ahern936be502011-09-06 09:12:26 -0600447 struct perf_sample *data, bool swapped)
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -0200448{
449 const u64 *array;
450
David Ahern936be502011-09-06 09:12:26 -0600451 /*
452 * used for cross-endian analysis. See git commit 65014ab3
453 * for why this goofiness is needed.
454 */
455 union {
456 u64 val64;
457 u32 val32[2];
458 } u;
459
460
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -0200461 data->cpu = data->pid = data->tid = -1;
462 data->stream_id = data->id = data->time = -1ULL;
463
464 if (event->header.type != PERF_RECORD_SAMPLE) {
465 if (!sample_id_all)
466 return 0;
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200467 return perf_event__parse_id_sample(event, type, data);
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -0200468 }
469
470 array = event->sample.array;
471
Frederic Weisbeckera2854122011-05-21 19:33:04 +0200472 if (sample_size + sizeof(event->header) > event->header.size)
473 return -EFAULT;
474
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -0200475 if (type & PERF_SAMPLE_IP) {
476 data->ip = event->ip.ip;
477 array++;
478 }
479
480 if (type & PERF_SAMPLE_TID) {
David Ahern936be502011-09-06 09:12:26 -0600481 u.val64 = *array;
482 if (swapped) {
483 /* undo swap of u64, then swap on individual u32s */
484 u.val64 = bswap_64(u.val64);
485 u.val32[0] = bswap_32(u.val32[0]);
486 u.val32[1] = bswap_32(u.val32[1]);
487 }
488
489 data->pid = u.val32[0];
490 data->tid = u.val32[1];
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -0200491 array++;
492 }
493
494 if (type & PERF_SAMPLE_TIME) {
495 data->time = *array;
496 array++;
497 }
498
David Ahern7cec0922011-05-30 13:08:23 -0600499 data->addr = 0;
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -0200500 if (type & PERF_SAMPLE_ADDR) {
501 data->addr = *array;
502 array++;
503 }
504
505 data->id = -1ULL;
506 if (type & PERF_SAMPLE_ID) {
507 data->id = *array;
508 array++;
509 }
510
511 if (type & PERF_SAMPLE_STREAM_ID) {
512 data->stream_id = *array;
513 array++;
514 }
515
516 if (type & PERF_SAMPLE_CPU) {
David Ahern936be502011-09-06 09:12:26 -0600517
518 u.val64 = *array;
519 if (swapped) {
520 /* undo swap of u64, then swap on individual u32s */
521 u.val64 = bswap_64(u.val64);
522 u.val32[0] = bswap_32(u.val32[0]);
523 }
524
525 data->cpu = u.val32[0];
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -0200526 array++;
527 }
528
529 if (type & PERF_SAMPLE_PERIOD) {
530 data->period = *array;
531 array++;
532 }
533
534 if (type & PERF_SAMPLE_READ) {
535 fprintf(stderr, "PERF_SAMPLE_READ is unsuported for now\n");
536 return -1;
537 }
538
539 if (type & PERF_SAMPLE_CALLCHAIN) {
Frederic Weisbecker98e1da92011-05-21 20:08:15 +0200540 if (sample_overlap(event, array, sizeof(data->callchain->nr)))
541 return -EFAULT;
542
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -0200543 data->callchain = (struct ip_callchain *)array;
Frederic Weisbecker98e1da92011-05-21 20:08:15 +0200544
545 if (sample_overlap(event, array, data->callchain->nr))
546 return -EFAULT;
547
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -0200548 array += 1 + data->callchain->nr;
549 }
550
551 if (type & PERF_SAMPLE_RAW) {
Jiri Olsa8e303f22011-09-29 17:05:08 +0200552 const u64 *pdata;
553
David Ahern936be502011-09-06 09:12:26 -0600554 u.val64 = *array;
555 if (WARN_ONCE(swapped,
556 "Endianness of raw data not corrected!\n")) {
557 /* undo swap of u64, then swap on individual u32s */
558 u.val64 = bswap_64(u.val64);
559 u.val32[0] = bswap_32(u.val32[0]);
560 u.val32[1] = bswap_32(u.val32[1]);
561 }
Frederic Weisbecker98e1da92011-05-21 20:08:15 +0200562
563 if (sample_overlap(event, array, sizeof(u32)))
564 return -EFAULT;
565
David Ahern936be502011-09-06 09:12:26 -0600566 data->raw_size = u.val32[0];
Jiri Olsa8e303f22011-09-29 17:05:08 +0200567 pdata = (void *) array + sizeof(u32);
Frederic Weisbecker98e1da92011-05-21 20:08:15 +0200568
Jiri Olsa8e303f22011-09-29 17:05:08 +0200569 if (sample_overlap(event, pdata, data->raw_size))
Frederic Weisbecker98e1da92011-05-21 20:08:15 +0200570 return -EFAULT;
571
Jiri Olsa8e303f22011-09-29 17:05:08 +0200572 data->raw_data = (void *) pdata;
Arnaldo Carvalho de Melod0dd74e2011-01-21 13:46:41 -0200573 }
574
575 return 0;
576}
Andrew Vagin74eec262011-11-28 12:03:31 +0300577
578int perf_event__synthesize_sample(union perf_event *event, u64 type,
579 const struct perf_sample *sample,
580 bool swapped)
581{
582 u64 *array;
583
584 /*
585 * used for cross-endian analysis. See git commit 65014ab3
586 * for why this goofiness is needed.
587 */
588 union {
589 u64 val64;
590 u32 val32[2];
591 } u;
592
593 array = event->sample.array;
594
595 if (type & PERF_SAMPLE_IP) {
596 event->ip.ip = sample->ip;
597 array++;
598 }
599
600 if (type & PERF_SAMPLE_TID) {
601 u.val32[0] = sample->pid;
602 u.val32[1] = sample->tid;
603 if (swapped) {
604 /*
605 * Inverse of what is done in perf_event__parse_sample
606 */
607 u.val32[0] = bswap_32(u.val32[0]);
608 u.val32[1] = bswap_32(u.val32[1]);
609 u.val64 = bswap_64(u.val64);
610 }
611
612 *array = u.val64;
613 array++;
614 }
615
616 if (type & PERF_SAMPLE_TIME) {
617 *array = sample->time;
618 array++;
619 }
620
621 if (type & PERF_SAMPLE_ADDR) {
622 *array = sample->addr;
623 array++;
624 }
625
626 if (type & PERF_SAMPLE_ID) {
627 *array = sample->id;
628 array++;
629 }
630
631 if (type & PERF_SAMPLE_STREAM_ID) {
632 *array = sample->stream_id;
633 array++;
634 }
635
636 if (type & PERF_SAMPLE_CPU) {
637 u.val32[0] = sample->cpu;
638 if (swapped) {
639 /*
640 * Inverse of what is done in perf_event__parse_sample
641 */
642 u.val32[0] = bswap_32(u.val32[0]);
643 u.val64 = bswap_64(u.val64);
644 }
645 *array = u.val64;
646 array++;
647 }
648
649 if (type & PERF_SAMPLE_PERIOD) {
650 *array = sample->period;
651 array++;
652 }
653
654 return 0;
655}