perf tools: Reconstruct hw cache event with modifiers from perf_event_attr
[firefly-linux-kernel-4.4.55.git] / tools / perf / util / evsel.c
1 /*
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
10 #include <byteswap.h>
11 #include "asm/bug.h"
12 #include "evsel.h"
13 #include "evlist.h"
14 #include "util.h"
15 #include "cpumap.h"
16 #include "thread_map.h"
17 #include "target.h"
18 #include "../../include/linux/perf_event.h"
19
20 #define FD(e, x, y) (*(int *)xyarray__entry(e->fd, x, y))
21 #define GROUP_FD(group_fd, cpu) (*(int *)xyarray__entry(group_fd, cpu, 0))
22
23 int __perf_evsel__sample_size(u64 sample_type)
24 {
25         u64 mask = sample_type & PERF_SAMPLE_MASK;
26         int size = 0;
27         int i;
28
29         for (i = 0; i < 64; i++) {
30                 if (mask & (1ULL << i))
31                         size++;
32         }
33
34         size *= sizeof(u64);
35
36         return size;
37 }
38
39 void hists__init(struct hists *hists)
40 {
41         memset(hists, 0, sizeof(*hists));
42         hists->entries_in_array[0] = hists->entries_in_array[1] = RB_ROOT;
43         hists->entries_in = &hists->entries_in_array[0];
44         hists->entries_collapsed = RB_ROOT;
45         hists->entries = RB_ROOT;
46         pthread_mutex_init(&hists->lock, NULL);
47 }
48
49 void perf_evsel__init(struct perf_evsel *evsel,
50                       struct perf_event_attr *attr, int idx)
51 {
52         evsel->idx         = idx;
53         evsel->attr        = *attr;
54         INIT_LIST_HEAD(&evsel->node);
55         hists__init(&evsel->hists);
56 }
57
58 struct perf_evsel *perf_evsel__new(struct perf_event_attr *attr, int idx)
59 {
60         struct perf_evsel *evsel = zalloc(sizeof(*evsel));
61
62         if (evsel != NULL)
63                 perf_evsel__init(evsel, attr, idx);
64
65         return evsel;
66 }
67
68 static const char *perf_evsel__hw_names[PERF_COUNT_HW_MAX] = {
69         "cycles",
70         "instructions",
71         "cache-references",
72         "cache-misses",
73         "branches",
74         "branch-misses",
75         "bus-cycles",
76         "stalled-cycles-frontend",
77         "stalled-cycles-backend",
78         "ref-cycles",
79 };
80
81 const char *__perf_evsel__hw_name(u64 config)
82 {
83         if (config < PERF_COUNT_HW_MAX && perf_evsel__hw_names[config])
84                 return perf_evsel__hw_names[config];
85
86         return "unknown-hardware";
87 }
88
89 static int perf_evsel__add_modifiers(struct perf_evsel *evsel, char *bf, size_t size)
90 {
91         int colon = 0, r = 0;
92         struct perf_event_attr *attr = &evsel->attr;
93         bool exclude_guest_default = false;
94
95 #define MOD_PRINT(context, mod) do {                                    \
96                 if (!attr->exclude_##context) {                         \
97                         if (!colon) colon = ++r;                        \
98                         r += scnprintf(bf + r, size - r, "%c", mod);    \
99                 } } while(0)
100
101         if (attr->exclude_kernel || attr->exclude_user || attr->exclude_hv) {
102                 MOD_PRINT(kernel, 'k');
103                 MOD_PRINT(user, 'u');
104                 MOD_PRINT(hv, 'h');
105                 exclude_guest_default = true;
106         }
107
108         if (attr->precise_ip) {
109                 if (!colon)
110                         colon = ++r;
111                 r += scnprintf(bf + r, size - r, "%.*s", attr->precise_ip, "ppp");
112                 exclude_guest_default = true;
113         }
114
115         if (attr->exclude_host || attr->exclude_guest == exclude_guest_default) {
116                 MOD_PRINT(host, 'H');
117                 MOD_PRINT(guest, 'G');
118         }
119 #undef MOD_PRINT
120         if (colon)
121                 bf[colon - 1] = ':';
122         return r;
123 }
124
125 static int perf_evsel__hw_name(struct perf_evsel *evsel, char *bf, size_t size)
126 {
127         int r = scnprintf(bf, size, "%s", __perf_evsel__hw_name(evsel->attr.config));
128         return r + perf_evsel__add_modifiers(evsel, bf + r, size - r);
129 }
130
131 const char *perf_evsel__hw_cache[PERF_COUNT_HW_CACHE_MAX]
132                                 [PERF_EVSEL__MAX_ALIASES] = {
133  { "L1-dcache", "l1-d",         "l1d",          "L1-data",              },
134  { "L1-icache", "l1-i",         "l1i",          "L1-instruction",       },
135  { "LLC",       "L2",                                                   },
136  { "dTLB",      "d-tlb",        "Data-TLB",                             },
137  { "iTLB",      "i-tlb",        "Instruction-TLB",                      },
138  { "branch",    "branches",     "bpu",          "btb",          "bpc",  },
139  { "node",                                                              },
140 };
141
142 const char *perf_evsel__hw_cache_op[PERF_COUNT_HW_CACHE_OP_MAX]
143                                    [PERF_EVSEL__MAX_ALIASES] = {
144  { "load",      "loads",        "read",                                 },
145  { "store",     "stores",       "write",                                },
146  { "prefetch",  "prefetches",   "speculative-read", "speculative-load", },
147 };
148
149 const char *perf_evsel__hw_cache_result[PERF_COUNT_HW_CACHE_RESULT_MAX]
150                                        [PERF_EVSEL__MAX_ALIASES] = {
151  { "refs",      "Reference",    "ops",          "access",               },
152  { "misses",    "miss",                                                 },
153 };
154
155 #define C(x)            PERF_COUNT_HW_CACHE_##x
156 #define CACHE_READ      (1 << C(OP_READ))
157 #define CACHE_WRITE     (1 << C(OP_WRITE))
158 #define CACHE_PREFETCH  (1 << C(OP_PREFETCH))
159 #define COP(x)          (1 << x)
160
161 /*
162  * cache operartion stat
163  * L1I : Read and prefetch only
164  * ITLB and BPU : Read-only
165  */
166 static unsigned long perf_evsel__hw_cache_stat[C(MAX)] = {
167  [C(L1D)]       = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
168  [C(L1I)]       = (CACHE_READ | CACHE_PREFETCH),
169  [C(LL)]        = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
170  [C(DTLB)]      = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
171  [C(ITLB)]      = (CACHE_READ),
172  [C(BPU)]       = (CACHE_READ),
173  [C(NODE)]      = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
174 };
175
176 bool perf_evsel__is_cache_op_valid(u8 type, u8 op)
177 {
178         if (perf_evsel__hw_cache_stat[type] & COP(op))
179                 return true;    /* valid */
180         else
181                 return false;   /* invalid */
182 }
183
184 int __perf_evsel__hw_cache_type_op_res_name(u8 type, u8 op, u8 result,
185                                             char *bf, size_t size)
186 {
187         if (result) {
188                 return scnprintf(bf, size, "%s-%s-%s", perf_evsel__hw_cache[type][0],
189                                  perf_evsel__hw_cache_op[op][0],
190                                  perf_evsel__hw_cache_result[result][0]);
191         }
192
193         return scnprintf(bf, size, "%s-%s", perf_evsel__hw_cache[type][0],
194                          perf_evsel__hw_cache_op[op][1]);
195 }
196
197 int __perf_evsel__hw_cache_name(u64 config, char *bf, size_t size)
198 {
199         u8 op, result, type = (config >>  0) & 0xff;
200         const char *err = "unknown-ext-hardware-cache-type";
201
202         if (type > PERF_COUNT_HW_CACHE_MAX)
203                 goto out_err;
204
205         op = (config >>  8) & 0xff;
206         err = "unknown-ext-hardware-cache-op";
207         if (op > PERF_COUNT_HW_CACHE_OP_MAX)
208                 goto out_err;
209
210         result = (config >> 16) & 0xff;
211         err = "unknown-ext-hardware-cache-result";
212         if (result > PERF_COUNT_HW_CACHE_RESULT_MAX)
213                 goto out_err;
214
215         err = "invalid-cache";
216         if (!perf_evsel__is_cache_op_valid(type, op))
217                 goto out_err;
218
219         return __perf_evsel__hw_cache_type_op_res_name(type, op, result, bf, size);
220 out_err:
221         return scnprintf(bf, size, "%s", err);
222 }
223
224 static int perf_evsel__hw_cache_name(struct perf_evsel *evsel, char *bf, size_t size)
225 {
226         int ret = __perf_evsel__hw_cache_name(evsel->attr.config, bf, size);
227         return ret + perf_evsel__add_modifiers(evsel, bf + ret, size - ret);
228 }
229
230 int perf_evsel__name(struct perf_evsel *evsel, char *bf, size_t size)
231 {
232         int ret;
233
234         switch (evsel->attr.type) {
235         case PERF_TYPE_RAW:
236                 ret = scnprintf(bf, size, "raw 0x%" PRIx64, evsel->attr.config);
237                 break;
238
239         case PERF_TYPE_HARDWARE:
240                 ret = perf_evsel__hw_name(evsel, bf, size);
241                 break;
242
243         case PERF_TYPE_HW_CACHE:
244                 ret = perf_evsel__hw_cache_name(evsel, bf, size);
245                 break;
246
247         default:
248                 /*
249                  * FIXME
250                  *
251                  * This is the minimal perf_evsel__name so that we can
252                  * reconstruct event names taking into account event modifiers.
253                  *
254                  * The old event_name uses it now for raw anr hw events, so that
255                  * we don't drag all the parsing stuff into the python binding.
256                  *
257                  * On the next devel cycle the rest of the event naming will be
258                  * brought here.
259                  */
260                 return 0;
261         }
262
263         return ret;
264 }
265
266 void perf_evsel__config(struct perf_evsel *evsel, struct perf_record_opts *opts,
267                         struct perf_evsel *first)
268 {
269         struct perf_event_attr *attr = &evsel->attr;
270         int track = !evsel->idx; /* only the first counter needs these */
271
272         attr->disabled = 1;
273         attr->sample_id_all = opts->sample_id_all_missing ? 0 : 1;
274         attr->inherit       = !opts->no_inherit;
275         attr->read_format   = PERF_FORMAT_TOTAL_TIME_ENABLED |
276                               PERF_FORMAT_TOTAL_TIME_RUNNING |
277                               PERF_FORMAT_ID;
278
279         attr->sample_type  |= PERF_SAMPLE_IP | PERF_SAMPLE_TID;
280
281         /*
282          * We default some events to a 1 default interval. But keep
283          * it a weak assumption overridable by the user.
284          */
285         if (!attr->sample_period || (opts->user_freq != UINT_MAX &&
286                                      opts->user_interval != ULLONG_MAX)) {
287                 if (opts->freq) {
288                         attr->sample_type       |= PERF_SAMPLE_PERIOD;
289                         attr->freq              = 1;
290                         attr->sample_freq       = opts->freq;
291                 } else {
292                         attr->sample_period = opts->default_interval;
293                 }
294         }
295
296         if (opts->no_samples)
297                 attr->sample_freq = 0;
298
299         if (opts->inherit_stat)
300                 attr->inherit_stat = 1;
301
302         if (opts->sample_address) {
303                 attr->sample_type       |= PERF_SAMPLE_ADDR;
304                 attr->mmap_data = track;
305         }
306
307         if (opts->call_graph)
308                 attr->sample_type       |= PERF_SAMPLE_CALLCHAIN;
309
310         if (perf_target__has_cpu(&opts->target))
311                 attr->sample_type       |= PERF_SAMPLE_CPU;
312
313         if (opts->period)
314                 attr->sample_type       |= PERF_SAMPLE_PERIOD;
315
316         if (!opts->sample_id_all_missing &&
317             (opts->sample_time || !opts->no_inherit ||
318              perf_target__has_cpu(&opts->target)))
319                 attr->sample_type       |= PERF_SAMPLE_TIME;
320
321         if (opts->raw_samples) {
322                 attr->sample_type       |= PERF_SAMPLE_TIME;
323                 attr->sample_type       |= PERF_SAMPLE_RAW;
324                 attr->sample_type       |= PERF_SAMPLE_CPU;
325         }
326
327         if (opts->no_delay) {
328                 attr->watermark = 0;
329                 attr->wakeup_events = 1;
330         }
331         if (opts->branch_stack) {
332                 attr->sample_type       |= PERF_SAMPLE_BRANCH_STACK;
333                 attr->branch_sample_type = opts->branch_stack;
334         }
335
336         attr->mmap = track;
337         attr->comm = track;
338
339         if (perf_target__none(&opts->target) &&
340             (!opts->group || evsel == first)) {
341                 attr->enable_on_exec = 1;
342         }
343 }
344
345 int perf_evsel__alloc_fd(struct perf_evsel *evsel, int ncpus, int nthreads)
346 {
347         int cpu, thread;
348         evsel->fd = xyarray__new(ncpus, nthreads, sizeof(int));
349
350         if (evsel->fd) {
351                 for (cpu = 0; cpu < ncpus; cpu++) {
352                         for (thread = 0; thread < nthreads; thread++) {
353                                 FD(evsel, cpu, thread) = -1;
354                         }
355                 }
356         }
357
358         return evsel->fd != NULL ? 0 : -ENOMEM;
359 }
360
361 int perf_evsel__alloc_id(struct perf_evsel *evsel, int ncpus, int nthreads)
362 {
363         evsel->sample_id = xyarray__new(ncpus, nthreads, sizeof(struct perf_sample_id));
364         if (evsel->sample_id == NULL)
365                 return -ENOMEM;
366
367         evsel->id = zalloc(ncpus * nthreads * sizeof(u64));
368         if (evsel->id == NULL) {
369                 xyarray__delete(evsel->sample_id);
370                 evsel->sample_id = NULL;
371                 return -ENOMEM;
372         }
373
374         return 0;
375 }
376
377 int perf_evsel__alloc_counts(struct perf_evsel *evsel, int ncpus)
378 {
379         evsel->counts = zalloc((sizeof(*evsel->counts) +
380                                 (ncpus * sizeof(struct perf_counts_values))));
381         return evsel->counts != NULL ? 0 : -ENOMEM;
382 }
383
384 void perf_evsel__free_fd(struct perf_evsel *evsel)
385 {
386         xyarray__delete(evsel->fd);
387         evsel->fd = NULL;
388 }
389
390 void perf_evsel__free_id(struct perf_evsel *evsel)
391 {
392         xyarray__delete(evsel->sample_id);
393         evsel->sample_id = NULL;
394         free(evsel->id);
395         evsel->id = NULL;
396 }
397
398 void perf_evsel__close_fd(struct perf_evsel *evsel, int ncpus, int nthreads)
399 {
400         int cpu, thread;
401
402         for (cpu = 0; cpu < ncpus; cpu++)
403                 for (thread = 0; thread < nthreads; ++thread) {
404                         close(FD(evsel, cpu, thread));
405                         FD(evsel, cpu, thread) = -1;
406                 }
407 }
408
409 void perf_evsel__exit(struct perf_evsel *evsel)
410 {
411         assert(list_empty(&evsel->node));
412         xyarray__delete(evsel->fd);
413         xyarray__delete(evsel->sample_id);
414         free(evsel->id);
415 }
416
417 void perf_evsel__delete(struct perf_evsel *evsel)
418 {
419         perf_evsel__exit(evsel);
420         close_cgroup(evsel->cgrp);
421         free(evsel->name);
422         free(evsel);
423 }
424
425 int __perf_evsel__read_on_cpu(struct perf_evsel *evsel,
426                               int cpu, int thread, bool scale)
427 {
428         struct perf_counts_values count;
429         size_t nv = scale ? 3 : 1;
430
431         if (FD(evsel, cpu, thread) < 0)
432                 return -EINVAL;
433
434         if (evsel->counts == NULL && perf_evsel__alloc_counts(evsel, cpu + 1) < 0)
435                 return -ENOMEM;
436
437         if (readn(FD(evsel, cpu, thread), &count, nv * sizeof(u64)) < 0)
438                 return -errno;
439
440         if (scale) {
441                 if (count.run == 0)
442                         count.val = 0;
443                 else if (count.run < count.ena)
444                         count.val = (u64)((double)count.val * count.ena / count.run + 0.5);
445         } else
446                 count.ena = count.run = 0;
447
448         evsel->counts->cpu[cpu] = count;
449         return 0;
450 }
451
452 int __perf_evsel__read(struct perf_evsel *evsel,
453                        int ncpus, int nthreads, bool scale)
454 {
455         size_t nv = scale ? 3 : 1;
456         int cpu, thread;
457         struct perf_counts_values *aggr = &evsel->counts->aggr, count;
458
459         aggr->val = aggr->ena = aggr->run = 0;
460
461         for (cpu = 0; cpu < ncpus; cpu++) {
462                 for (thread = 0; thread < nthreads; thread++) {
463                         if (FD(evsel, cpu, thread) < 0)
464                                 continue;
465
466                         if (readn(FD(evsel, cpu, thread),
467                                   &count, nv * sizeof(u64)) < 0)
468                                 return -errno;
469
470                         aggr->val += count.val;
471                         if (scale) {
472                                 aggr->ena += count.ena;
473                                 aggr->run += count.run;
474                         }
475                 }
476         }
477
478         evsel->counts->scaled = 0;
479         if (scale) {
480                 if (aggr->run == 0) {
481                         evsel->counts->scaled = -1;
482                         aggr->val = 0;
483                         return 0;
484                 }
485
486                 if (aggr->run < aggr->ena) {
487                         evsel->counts->scaled = 1;
488                         aggr->val = (u64)((double)aggr->val * aggr->ena / aggr->run + 0.5);
489                 }
490         } else
491                 aggr->ena = aggr->run = 0;
492
493         return 0;
494 }
495
496 static int __perf_evsel__open(struct perf_evsel *evsel, struct cpu_map *cpus,
497                               struct thread_map *threads, bool group,
498                               struct xyarray *group_fds)
499 {
500         int cpu, thread;
501         unsigned long flags = 0;
502         int pid = -1, err;
503
504         if (evsel->fd == NULL &&
505             perf_evsel__alloc_fd(evsel, cpus->nr, threads->nr) < 0)
506                 return -ENOMEM;
507
508         if (evsel->cgrp) {
509                 flags = PERF_FLAG_PID_CGROUP;
510                 pid = evsel->cgrp->fd;
511         }
512
513         for (cpu = 0; cpu < cpus->nr; cpu++) {
514                 int group_fd = group_fds ? GROUP_FD(group_fds, cpu) : -1;
515
516                 for (thread = 0; thread < threads->nr; thread++) {
517
518                         if (!evsel->cgrp)
519                                 pid = threads->map[thread];
520
521                         FD(evsel, cpu, thread) = sys_perf_event_open(&evsel->attr,
522                                                                      pid,
523                                                                      cpus->map[cpu],
524                                                                      group_fd, flags);
525                         if (FD(evsel, cpu, thread) < 0) {
526                                 err = -errno;
527                                 goto out_close;
528                         }
529
530                         if (group && group_fd == -1)
531                                 group_fd = FD(evsel, cpu, thread);
532                 }
533         }
534
535         return 0;
536
537 out_close:
538         do {
539                 while (--thread >= 0) {
540                         close(FD(evsel, cpu, thread));
541                         FD(evsel, cpu, thread) = -1;
542                 }
543                 thread = threads->nr;
544         } while (--cpu >= 0);
545         return err;
546 }
547
548 void perf_evsel__close(struct perf_evsel *evsel, int ncpus, int nthreads)
549 {
550         if (evsel->fd == NULL)
551                 return;
552
553         perf_evsel__close_fd(evsel, ncpus, nthreads);
554         perf_evsel__free_fd(evsel);
555         evsel->fd = NULL;
556 }
557
558 static struct {
559         struct cpu_map map;
560         int cpus[1];
561 } empty_cpu_map = {
562         .map.nr = 1,
563         .cpus   = { -1, },
564 };
565
566 static struct {
567         struct thread_map map;
568         int threads[1];
569 } empty_thread_map = {
570         .map.nr  = 1,
571         .threads = { -1, },
572 };
573
574 int perf_evsel__open(struct perf_evsel *evsel, struct cpu_map *cpus,
575                      struct thread_map *threads, bool group,
576                      struct xyarray *group_fd)
577 {
578         if (cpus == NULL) {
579                 /* Work around old compiler warnings about strict aliasing */
580                 cpus = &empty_cpu_map.map;
581         }
582
583         if (threads == NULL)
584                 threads = &empty_thread_map.map;
585
586         return __perf_evsel__open(evsel, cpus, threads, group, group_fd);
587 }
588
589 int perf_evsel__open_per_cpu(struct perf_evsel *evsel,
590                              struct cpu_map *cpus, bool group,
591                              struct xyarray *group_fd)
592 {
593         return __perf_evsel__open(evsel, cpus, &empty_thread_map.map, group,
594                                   group_fd);
595 }
596
597 int perf_evsel__open_per_thread(struct perf_evsel *evsel,
598                                 struct thread_map *threads, bool group,
599                                 struct xyarray *group_fd)
600 {
601         return __perf_evsel__open(evsel, &empty_cpu_map.map, threads, group,
602                                   group_fd);
603 }
604
605 static int perf_event__parse_id_sample(const union perf_event *event, u64 type,
606                                        struct perf_sample *sample,
607                                        bool swapped)
608 {
609         const u64 *array = event->sample.array;
610         union u64_swap u;
611
612         array += ((event->header.size -
613                    sizeof(event->header)) / sizeof(u64)) - 1;
614
615         if (type & PERF_SAMPLE_CPU) {
616                 u.val64 = *array;
617                 if (swapped) {
618                         /* undo swap of u64, then swap on individual u32s */
619                         u.val64 = bswap_64(u.val64);
620                         u.val32[0] = bswap_32(u.val32[0]);
621                 }
622
623                 sample->cpu = u.val32[0];
624                 array--;
625         }
626
627         if (type & PERF_SAMPLE_STREAM_ID) {
628                 sample->stream_id = *array;
629                 array--;
630         }
631
632         if (type & PERF_SAMPLE_ID) {
633                 sample->id = *array;
634                 array--;
635         }
636
637         if (type & PERF_SAMPLE_TIME) {
638                 sample->time = *array;
639                 array--;
640         }
641
642         if (type & PERF_SAMPLE_TID) {
643                 u.val64 = *array;
644                 if (swapped) {
645                         /* undo swap of u64, then swap on individual u32s */
646                         u.val64 = bswap_64(u.val64);
647                         u.val32[0] = bswap_32(u.val32[0]);
648                         u.val32[1] = bswap_32(u.val32[1]);
649                 }
650
651                 sample->pid = u.val32[0];
652                 sample->tid = u.val32[1];
653         }
654
655         return 0;
656 }
657
658 static bool sample_overlap(const union perf_event *event,
659                            const void *offset, u64 size)
660 {
661         const void *base = event;
662
663         if (offset + size > base + event->header.size)
664                 return true;
665
666         return false;
667 }
668
669 int perf_event__parse_sample(const union perf_event *event, u64 type,
670                              int sample_size, bool sample_id_all,
671                              struct perf_sample *data, bool swapped)
672 {
673         const u64 *array;
674
675         /*
676          * used for cross-endian analysis. See git commit 65014ab3
677          * for why this goofiness is needed.
678          */
679         union u64_swap u;
680
681         memset(data, 0, sizeof(*data));
682         data->cpu = data->pid = data->tid = -1;
683         data->stream_id = data->id = data->time = -1ULL;
684         data->period = 1;
685
686         if (event->header.type != PERF_RECORD_SAMPLE) {
687                 if (!sample_id_all)
688                         return 0;
689                 return perf_event__parse_id_sample(event, type, data, swapped);
690         }
691
692         array = event->sample.array;
693
694         if (sample_size + sizeof(event->header) > event->header.size)
695                 return -EFAULT;
696
697         if (type & PERF_SAMPLE_IP) {
698                 data->ip = event->ip.ip;
699                 array++;
700         }
701
702         if (type & PERF_SAMPLE_TID) {
703                 u.val64 = *array;
704                 if (swapped) {
705                         /* undo swap of u64, then swap on individual u32s */
706                         u.val64 = bswap_64(u.val64);
707                         u.val32[0] = bswap_32(u.val32[0]);
708                         u.val32[1] = bswap_32(u.val32[1]);
709                 }
710
711                 data->pid = u.val32[0];
712                 data->tid = u.val32[1];
713                 array++;
714         }
715
716         if (type & PERF_SAMPLE_TIME) {
717                 data->time = *array;
718                 array++;
719         }
720
721         data->addr = 0;
722         if (type & PERF_SAMPLE_ADDR) {
723                 data->addr = *array;
724                 array++;
725         }
726
727         data->id = -1ULL;
728         if (type & PERF_SAMPLE_ID) {
729                 data->id = *array;
730                 array++;
731         }
732
733         if (type & PERF_SAMPLE_STREAM_ID) {
734                 data->stream_id = *array;
735                 array++;
736         }
737
738         if (type & PERF_SAMPLE_CPU) {
739
740                 u.val64 = *array;
741                 if (swapped) {
742                         /* undo swap of u64, then swap on individual u32s */
743                         u.val64 = bswap_64(u.val64);
744                         u.val32[0] = bswap_32(u.val32[0]);
745                 }
746
747                 data->cpu = u.val32[0];
748                 array++;
749         }
750
751         if (type & PERF_SAMPLE_PERIOD) {
752                 data->period = *array;
753                 array++;
754         }
755
756         if (type & PERF_SAMPLE_READ) {
757                 fprintf(stderr, "PERF_SAMPLE_READ is unsupported for now\n");
758                 return -1;
759         }
760
761         if (type & PERF_SAMPLE_CALLCHAIN) {
762                 if (sample_overlap(event, array, sizeof(data->callchain->nr)))
763                         return -EFAULT;
764
765                 data->callchain = (struct ip_callchain *)array;
766
767                 if (sample_overlap(event, array, data->callchain->nr))
768                         return -EFAULT;
769
770                 array += 1 + data->callchain->nr;
771         }
772
773         if (type & PERF_SAMPLE_RAW) {
774                 const u64 *pdata;
775
776                 u.val64 = *array;
777                 if (WARN_ONCE(swapped,
778                               "Endianness of raw data not corrected!\n")) {
779                         /* undo swap of u64, then swap on individual u32s */
780                         u.val64 = bswap_64(u.val64);
781                         u.val32[0] = bswap_32(u.val32[0]);
782                         u.val32[1] = bswap_32(u.val32[1]);
783                 }
784
785                 if (sample_overlap(event, array, sizeof(u32)))
786                         return -EFAULT;
787
788                 data->raw_size = u.val32[0];
789                 pdata = (void *) array + sizeof(u32);
790
791                 if (sample_overlap(event, pdata, data->raw_size))
792                         return -EFAULT;
793
794                 data->raw_data = (void *) pdata;
795
796                 array = (void *)array + data->raw_size + sizeof(u32);
797         }
798
799         if (type & PERF_SAMPLE_BRANCH_STACK) {
800                 u64 sz;
801
802                 data->branch_stack = (struct branch_stack *)array;
803                 array++; /* nr */
804
805                 sz = data->branch_stack->nr * sizeof(struct branch_entry);
806                 sz /= sizeof(u64);
807                 array += sz;
808         }
809         return 0;
810 }
811
812 int perf_event__synthesize_sample(union perf_event *event, u64 type,
813                                   const struct perf_sample *sample,
814                                   bool swapped)
815 {
816         u64 *array;
817
818         /*
819          * used for cross-endian analysis. See git commit 65014ab3
820          * for why this goofiness is needed.
821          */
822         union u64_swap u;
823
824         array = event->sample.array;
825
826         if (type & PERF_SAMPLE_IP) {
827                 event->ip.ip = sample->ip;
828                 array++;
829         }
830
831         if (type & PERF_SAMPLE_TID) {
832                 u.val32[0] = sample->pid;
833                 u.val32[1] = sample->tid;
834                 if (swapped) {
835                         /*
836                          * Inverse of what is done in perf_event__parse_sample
837                          */
838                         u.val32[0] = bswap_32(u.val32[0]);
839                         u.val32[1] = bswap_32(u.val32[1]);
840                         u.val64 = bswap_64(u.val64);
841                 }
842
843                 *array = u.val64;
844                 array++;
845         }
846
847         if (type & PERF_SAMPLE_TIME) {
848                 *array = sample->time;
849                 array++;
850         }
851
852         if (type & PERF_SAMPLE_ADDR) {
853                 *array = sample->addr;
854                 array++;
855         }
856
857         if (type & PERF_SAMPLE_ID) {
858                 *array = sample->id;
859                 array++;
860         }
861
862         if (type & PERF_SAMPLE_STREAM_ID) {
863                 *array = sample->stream_id;
864                 array++;
865         }
866
867         if (type & PERF_SAMPLE_CPU) {
868                 u.val32[0] = sample->cpu;
869                 if (swapped) {
870                         /*
871                          * Inverse of what is done in perf_event__parse_sample
872                          */
873                         u.val32[0] = bswap_32(u.val32[0]);
874                         u.val64 = bswap_64(u.val64);
875                 }
876                 *array = u.val64;
877                 array++;
878         }
879
880         if (type & PERF_SAMPLE_PERIOD) {
881                 *array = sample->period;
882                 array++;
883         }
884
885         return 0;
886 }