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