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