hc
2023-12-09 b22da3d8526a935aa31e086e63f60ff3246cb61c
kernel/tools/perf/util/tsc.c
....@@ -1,7 +1,16 @@
11 // SPDX-License-Identifier: GPL-2.0
2
+#include <errno.h>
3
+
24 #include <linux/compiler.h>
5
+#include <linux/perf_event.h>
6
+#include <linux/stddef.h>
37 #include <linux/types.h>
48
9
+#include <asm/barrier.h>
10
+
11
+#include "event.h"
12
+#include "synthetic-events.h"
13
+#include "debug.h"
514 #include "tsc.h"
615
716 u64 perf_time_to_tsc(u64 ns, struct perf_tsc_conversion *tc)
....@@ -19,12 +28,84 @@
1928 {
2029 u64 quot, rem;
2130
31
+ if (tc->cap_user_time_short)
32
+ cyc = tc->time_cycles +
33
+ ((cyc - tc->time_cycles) & tc->time_mask);
34
+
2235 quot = cyc >> tc->time_shift;
2336 rem = cyc & (((u64)1 << tc->time_shift) - 1);
2437 return tc->time_zero + quot * tc->time_mult +
2538 ((rem * tc->time_mult) >> tc->time_shift);
2639 }
2740
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
+
28109 u64 __weak rdtsc(void)
29110 {
30111 return 0;