Commit | Line | Data |
---|---|---|
886d51a3 MD |
1 | #ifndef _LTTNG_TRACE_CLOCK_H |
2 | #define _LTTNG_TRACE_CLOCK_H | |
3 | ||
f6c19f6e | 4 | /* |
886d51a3 | 5 | * wrapper/trace-clock.h |
f6c19f6e MD |
6 | * |
7 | * Contains LTTng trace clock mapping to LTTng trace clock or mainline monotonic | |
8 | * clock. This wrapper depends on CONFIG_HIGH_RES_TIMERS=y. | |
9 | * | |
886d51a3 MD |
10 | * Copyright (C) 2011-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
11 | * | |
12 | * This library is free software; you can redistribute it and/or | |
13 | * modify it under the terms of the GNU Lesser General Public | |
14 | * License as published by the Free Software Foundation; only | |
15 | * version 2.1 of the License. | |
16 | * | |
17 | * This library is distributed in the hope that it will be useful, | |
18 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
20 | * Lesser General Public License for more details. | |
21 | * | |
22 | * You should have received a copy of the GNU Lesser General Public | |
23 | * License along with this library; if not, write to the Free Software | |
24 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | |
f6c19f6e MD |
25 | */ |
26 | ||
f6c19f6e MD |
27 | #ifdef CONFIG_HAVE_TRACE_CLOCK |
28 | #include <linux/trace-clock.h> | |
29 | #else /* CONFIG_HAVE_TRACE_CLOCK */ | |
30 | ||
31 | #include <linux/hardirq.h> | |
32 | #include <linux/ktime.h> | |
33 | #include <linux/time.h> | |
34 | #include <linux/hrtimer.h> | |
b0725207 | 35 | #include <linux/percpu.h> |
fc8216ae | 36 | #include <linux/version.h> |
b0725207 | 37 | #include <asm/local.h> |
5a2f5e92 MD |
38 | #include <lttng-kernel-version.h> |
39 | #include <lttng-clock.h> | |
40 | #include <wrapper/percpu-defs.h> | |
41 | #include <wrapper/random.h> | |
f6c19f6e | 42 | |
c94ac1ac | 43 | #if ((LTTNG_KERNEL_RANGE(3,10,0, 3,10,14) && !LTTNG_RHEL_KERNEL_RANGE(3,10,0,123,0,0, 3,10,14,0,0,0)) \ |
f30ae671 | 44 | || LTTNG_KERNEL_RANGE(3,11,0, 3,11,3)) |
9998f521 | 45 | #error "Linux kernels 3.10 and 3.11 introduce a deadlock in the timekeeping subsystem. Fixed by commit 7bd36014460f793c19e7d6c94dab67b0afcfcb7f \"timekeeping: Fix HRTICK related deadlock from ntp lock changes\" in Linux." |
fc8216ae MD |
46 | #endif |
47 | ||
2754583e MD |
48 | extern struct lttng_trace_clock *lttng_trace_clock; |
49 | ||
b0725207 MD |
50 | #if (LINUX_VERSION_CODE >= KERNEL_VERSION(3,17,0)) |
51 | ||
52 | DECLARE_PER_CPU(local_t, lttng_last_tsc); | |
53 | ||
54 | #if (BITS_PER_LONG == 32) | |
55 | /* | |
56 | * Fixup "src_now" using the 32 LSB from "last". We need to handle overflow and | |
57 | * underflow of the 32nd bit. "last" can be above, below or equal to the 32 LSB | |
58 | * of "src_now". | |
59 | */ | |
60 | static inline u64 trace_clock_fixup(u64 src_now, u32 last) | |
61 | { | |
62 | u64 now; | |
63 | ||
64 | now = src_now & 0xFFFFFFFF00000000ULL; | |
65 | now |= (u64) last; | |
66 | /* Detect overflow or underflow between now and last. */ | |
67 | if ((src_now & 0x80000000U) && !(last & 0x80000000U)) { | |
68 | /* | |
69 | * If 32nd bit transitions from 1 to 0, and we move forward in | |
70 | * time from "now" to "last", then we have an overflow. | |
71 | */ | |
72 | if (((s32) now - (s32) last) < 0) | |
73 | now += 0x0000000100000000ULL; | |
74 | } else if (!(src_now & 0x80000000U) && (last & 0x80000000U)) { | |
75 | /* | |
76 | * If 32nd bit transitions from 0 to 1, and we move backward in | |
77 | * time from "now" to "last", then we have an underflow. | |
78 | */ | |
79 | if (((s32) now - (s32) last) > 0) | |
80 | now -= 0x0000000100000000ULL; | |
81 | } | |
82 | return now; | |
83 | } | |
84 | #else /* #if (BITS_PER_LONG == 32) */ | |
85 | /* | |
86 | * The fixup is pretty easy on 64-bit architectures: "last" is a 64-bit | |
87 | * value, so we can use last directly as current time. | |
88 | */ | |
89 | static inline u64 trace_clock_fixup(u64 src_now, u64 last) | |
90 | { | |
91 | return last; | |
92 | } | |
93 | #endif /* #else #if (BITS_PER_LONG == 32) */ | |
94 | ||
95 | /* | |
0aaa7220 | 96 | * Sometimes called with preemption enabled. Can be interrupted. |
b0725207 MD |
97 | */ |
98 | static inline u64 trace_clock_monotonic_wrapper(void) | |
99 | { | |
100 | u64 now; | |
101 | unsigned long last, result; | |
102 | local_t *last_tsc; | |
103 | ||
104 | /* Use fast nmi-safe monotonic clock provided by the Linux kernel. */ | |
0aaa7220 | 105 | preempt_disable(); |
e6b06d7d | 106 | last_tsc = lttng_this_cpu_ptr(<tng_last_tsc); |
b0725207 MD |
107 | last = local_read(last_tsc); |
108 | /* | |
109 | * Read "last" before "now". It is not strictly required, but it ensures | |
110 | * that an interrupt coming in won't artificially trigger a case where | |
111 | * "now" < "last". This kind of situation should only happen if the | |
112 | * mono_fast time source goes slightly backwards. | |
113 | */ | |
114 | barrier(); | |
115 | now = ktime_get_mono_fast_ns(); | |
116 | if (((long) now - (long) last) < 0) | |
117 | now = trace_clock_fixup(now, last); | |
118 | result = local_cmpxchg(last_tsc, last, (unsigned long) now); | |
0aaa7220 | 119 | preempt_enable(); |
b0725207 MD |
120 | if (result == last) { |
121 | /* Update done. */ | |
122 | return now; | |
123 | } else { | |
124 | /* | |
125 | * Update not done, due to concurrent update. We can use | |
126 | * "result", since it has been sampled concurrently with our | |
127 | * time read, so it should not be far from "now". | |
128 | */ | |
129 | return trace_clock_fixup(now, result); | |
130 | } | |
131 | } | |
132 | ||
133 | #else /* #if (LINUX_VERSION_CODE >= KERNEL_VERSION(3,17,0)) */ | |
f6c19f6e MD |
134 | static inline u64 trace_clock_monotonic_wrapper(void) |
135 | { | |
136 | ktime_t ktime; | |
137 | ||
138 | /* | |
139 | * Refuse to trace from NMIs with this wrapper, because an NMI could | |
140 | * nest over the xtime write seqlock and deadlock. | |
141 | */ | |
142 | if (in_nmi()) | |
97ca2c54 | 143 | return (u64) -EIO; |
f6c19f6e MD |
144 | |
145 | ktime = ktime_get(); | |
cfaf9f3d | 146 | return ktime_to_ns(ktime); |
f6c19f6e | 147 | } |
b0725207 | 148 | #endif /* #else #if (LINUX_VERSION_CODE >= KERNEL_VERSION(3,17,0)) */ |
f6c19f6e | 149 | |
2754583e | 150 | static inline u64 trace_clock_read64_monotonic(void) |
f6c19f6e MD |
151 | { |
152 | return (u64) trace_clock_monotonic_wrapper(); | |
153 | } | |
154 | ||
2754583e | 155 | static inline u64 trace_clock_freq_monotonic(void) |
f6c19f6e | 156 | { |
a3ccff4f | 157 | return (u64) NSEC_PER_SEC; |
f6c19f6e MD |
158 | } |
159 | ||
2754583e | 160 | static inline int trace_clock_uuid_monotonic(char *uuid) |
f6c19f6e | 161 | { |
a82c63f1 | 162 | return wrapper_get_bootid(uuid); |
f6c19f6e MD |
163 | } |
164 | ||
2754583e MD |
165 | static inline const char *trace_clock_name_monotonic(void) |
166 | { | |
167 | return "monotonic"; | |
168 | } | |
169 | ||
170 | static inline const char *trace_clock_description_monotonic(void) | |
171 | { | |
172 | return "Monotonic Clock"; | |
173 | } | |
174 | ||
b0725207 | 175 | #if (LINUX_VERSION_CODE >= KERNEL_VERSION(3,17,0)) |
f6c19f6e MD |
176 | static inline int get_trace_clock(void) |
177 | { | |
b0725207 MD |
178 | printk(KERN_WARNING "LTTng: Using mainline kernel monotonic fast clock, which is NMI-safe.\n"); |
179 | return 0; | |
180 | } | |
181 | #else /* #if (LINUX_VERSION_CODE >= KERNEL_VERSION(3,17,0)) */ | |
182 | static inline int get_trace_clock(void) | |
183 | { | |
184 | printk(KERN_WARNING "LTTng: Using mainline kernel monotonic clock. NMIs will not be traced.\n"); | |
f6c19f6e MD |
185 | return 0; |
186 | } | |
b0725207 | 187 | #endif /* #else #if (LINUX_VERSION_CODE >= KERNEL_VERSION(3,17,0)) */ |
f6c19f6e MD |
188 | |
189 | static inline void put_trace_clock(void) | |
190 | { | |
191 | } | |
192 | ||
2754583e MD |
193 | static inline u64 trace_clock_read64(void) |
194 | { | |
195 | struct lttng_trace_clock *ltc = ACCESS_ONCE(lttng_trace_clock); | |
196 | ||
197 | if (likely(!ltc)) { | |
198 | return trace_clock_read64_monotonic(); | |
199 | } else { | |
200 | read_barrier_depends(); /* load ltc before content */ | |
201 | return ltc->read64(); | |
202 | } | |
203 | } | |
204 | ||
205 | static inline u64 trace_clock_freq(void) | |
206 | { | |
207 | struct lttng_trace_clock *ltc = ACCESS_ONCE(lttng_trace_clock); | |
208 | ||
209 | if (!ltc) { | |
210 | return trace_clock_freq_monotonic(); | |
211 | } else { | |
212 | read_barrier_depends(); /* load ltc before content */ | |
213 | return ltc->freq(); | |
214 | } | |
215 | } | |
216 | ||
217 | static inline int trace_clock_uuid(char *uuid) | |
218 | { | |
219 | struct lttng_trace_clock *ltc = ACCESS_ONCE(lttng_trace_clock); | |
220 | ||
221 | read_barrier_depends(); /* load ltc before content */ | |
222 | /* Use default UUID cb when NULL */ | |
223 | if (!ltc || !ltc->uuid) { | |
224 | return trace_clock_uuid_monotonic(uuid); | |
225 | } else { | |
226 | return ltc->uuid(uuid); | |
227 | } | |
228 | } | |
229 | ||
230 | static inline const char *trace_clock_name(void) | |
231 | { | |
232 | struct lttng_trace_clock *ltc = ACCESS_ONCE(lttng_trace_clock); | |
233 | ||
234 | if (!ltc) { | |
235 | return trace_clock_name_monotonic(); | |
236 | } else { | |
237 | read_barrier_depends(); /* load ltc before content */ | |
238 | return ltc->name(); | |
239 | } | |
240 | } | |
241 | ||
242 | static inline const char *trace_clock_description(void) | |
243 | { | |
244 | struct lttng_trace_clock *ltc = ACCESS_ONCE(lttng_trace_clock); | |
245 | ||
246 | if (!ltc) { | |
247 | return trace_clock_description_monotonic(); | |
248 | } else { | |
249 | read_barrier_depends(); /* load ltc before content */ | |
250 | return ltc->description(); | |
251 | } | |
252 | } | |
253 | ||
f6c19f6e MD |
254 | #endif /* CONFIG_HAVE_TRACE_CLOCK */ |
255 | ||
a90917c3 | 256 | #endif /* _LTTNG_TRACE_CLOCK_H */ |