.. | .. |
---|
1 | 1 | // SPDX-License-Identifier: GPL-2.0 |
---|
| 2 | +#include <errno.h> |
---|
| 3 | + |
---|
2 | 4 | #include <linux/compiler.h> |
---|
| 5 | +#include <linux/perf_event.h> |
---|
| 6 | +#include <linux/stddef.h> |
---|
3 | 7 | #include <linux/types.h> |
---|
4 | 8 | |
---|
| 9 | +#include <asm/barrier.h> |
---|
| 10 | + |
---|
| 11 | +#include "event.h" |
---|
| 12 | +#include "synthetic-events.h" |
---|
| 13 | +#include "debug.h" |
---|
5 | 14 | #include "tsc.h" |
---|
6 | 15 | |
---|
7 | 16 | u64 perf_time_to_tsc(u64 ns, struct perf_tsc_conversion *tc) |
---|
.. | .. |
---|
19 | 28 | { |
---|
20 | 29 | u64 quot, rem; |
---|
21 | 30 | |
---|
| 31 | + if (tc->cap_user_time_short) |
---|
| 32 | + cyc = tc->time_cycles + |
---|
| 33 | + ((cyc - tc->time_cycles) & tc->time_mask); |
---|
| 34 | + |
---|
22 | 35 | quot = cyc >> tc->time_shift; |
---|
23 | 36 | rem = cyc & (((u64)1 << tc->time_shift) - 1); |
---|
24 | 37 | return tc->time_zero + quot * tc->time_mult + |
---|
25 | 38 | ((rem * tc->time_mult) >> tc->time_shift); |
---|
26 | 39 | } |
---|
27 | 40 | |
---|
| 41 | +int perf_read_tsc_conversion(const struct perf_event_mmap_page *pc, |
---|
| 42 | + struct perf_tsc_conversion *tc) |
---|
| 43 | +{ |
---|
| 44 | + u32 seq; |
---|
| 45 | + int i = 0; |
---|
| 46 | + |
---|
| 47 | + while (1) { |
---|
| 48 | + seq = pc->lock; |
---|
| 49 | + rmb(); |
---|
| 50 | + tc->time_mult = pc->time_mult; |
---|
| 51 | + tc->time_shift = pc->time_shift; |
---|
| 52 | + tc->time_zero = pc->time_zero; |
---|
| 53 | + tc->time_cycles = pc->time_cycles; |
---|
| 54 | + tc->time_mask = pc->time_mask; |
---|
| 55 | + tc->cap_user_time_zero = pc->cap_user_time_zero; |
---|
| 56 | + tc->cap_user_time_short = pc->cap_user_time_short; |
---|
| 57 | + rmb(); |
---|
| 58 | + if (pc->lock == seq && !(seq & 1)) |
---|
| 59 | + break; |
---|
| 60 | + if (++i > 10000) { |
---|
| 61 | + pr_debug("failed to get perf_event_mmap_page lock\n"); |
---|
| 62 | + return -EINVAL; |
---|
| 63 | + } |
---|
| 64 | + } |
---|
| 65 | + |
---|
| 66 | + if (!tc->cap_user_time_zero) |
---|
| 67 | + return -EOPNOTSUPP; |
---|
| 68 | + |
---|
| 69 | + return 0; |
---|
| 70 | +} |
---|
| 71 | + |
---|
| 72 | +int perf_event__synth_time_conv(const struct perf_event_mmap_page *pc, |
---|
| 73 | + struct perf_tool *tool, |
---|
| 74 | + perf_event__handler_t process, |
---|
| 75 | + struct machine *machine) |
---|
| 76 | +{ |
---|
| 77 | + union perf_event event = { |
---|
| 78 | + .time_conv = { |
---|
| 79 | + .header = { |
---|
| 80 | + .type = PERF_RECORD_TIME_CONV, |
---|
| 81 | + .size = sizeof(struct perf_record_time_conv), |
---|
| 82 | + }, |
---|
| 83 | + }, |
---|
| 84 | + }; |
---|
| 85 | + struct perf_tsc_conversion tc; |
---|
| 86 | + int err; |
---|
| 87 | + |
---|
| 88 | + if (!pc) |
---|
| 89 | + return 0; |
---|
| 90 | + err = perf_read_tsc_conversion(pc, &tc); |
---|
| 91 | + if (err == -EOPNOTSUPP) |
---|
| 92 | + return 0; |
---|
| 93 | + if (err) |
---|
| 94 | + return err; |
---|
| 95 | + |
---|
| 96 | + pr_debug2("Synthesizing TSC conversion information\n"); |
---|
| 97 | + |
---|
| 98 | + event.time_conv.time_mult = tc.time_mult; |
---|
| 99 | + event.time_conv.time_shift = tc.time_shift; |
---|
| 100 | + event.time_conv.time_zero = tc.time_zero; |
---|
| 101 | + event.time_conv.time_cycles = tc.time_cycles; |
---|
| 102 | + event.time_conv.time_mask = tc.time_mask; |
---|
| 103 | + event.time_conv.cap_user_time_zero = tc.cap_user_time_zero; |
---|
| 104 | + event.time_conv.cap_user_time_short = tc.cap_user_time_short; |
---|
| 105 | + |
---|
| 106 | + return process(tool, &event, NULL, machine); |
---|
| 107 | +} |
---|
| 108 | + |
---|
28 | 109 | u64 __weak rdtsc(void) |
---|
29 | 110 | { |
---|
30 | 111 | return 0; |
---|