649c93f358d62c9bcede2cd33bd3f890ffa412ab
[lttng-modules.git] / wrapper / trace-clock.h
1 #ifndef _LTTNG_TRACE_CLOCK_H
2 #define _LTTNG_TRACE_CLOCK_H
3
4 /*
5 * wrapper/trace-clock.h
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 *
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
25 */
26
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>
35 #include <linux/percpu.h>
36 #include <linux/version.h>
37 #include <asm/local.h>
38 #include <lttng-kernel-version.h>
39 #include <lttng-clock.h>
40 #include <wrapper/percpu-defs.h>
41 #include <wrapper/random.h>
42
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)) \
44 || LTTNG_KERNEL_RANGE(3,11,0, 3,11,3))
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."
46 #endif
47
48 extern struct lttng_trace_clock *lttng_trace_clock;
49
50 /*
51 * Upstream Linux commit 27727df240c7 ("Avoid taking lock in NMI path with
52 * CONFIG_DEBUG_TIMEKEEPING") introduces a buggy ktime_get_mono_fast_ns().
53 * This is fixed by patch "timekeeping: Fix __ktime_get_fast_ns() regression".
54 */
55 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(3,17,0) \
56 && !LTTNG_KERNEL_RANGE(4,8,0, 4,8,1) \
57 && !LTTNG_KERNEL_RANGE(4,7,4, 4,7,7) \
58 && !LTTNG_KERNEL_RANGE(4,4,20, 4,4,24) \
59 && !LTTNG_KERNEL_RANGE(4,1,32, 4,1,34))
60
61 DECLARE_PER_CPU(local_t, lttng_last_tsc);
62
63 #if (BITS_PER_LONG == 32)
64 /*
65 * Fixup "src_now" using the 32 LSB from "last". We need to handle overflow and
66 * underflow of the 32nd bit. "last" can be above, below or equal to the 32 LSB
67 * of "src_now".
68 */
69 static inline u64 trace_clock_fixup(u64 src_now, u32 last)
70 {
71 u64 now;
72
73 now = src_now & 0xFFFFFFFF00000000ULL;
74 now |= (u64) last;
75 /* Detect overflow or underflow between now and last. */
76 if ((src_now & 0x80000000U) && !(last & 0x80000000U)) {
77 /*
78 * If 32nd bit transitions from 1 to 0, and we move forward in
79 * time from "now" to "last", then we have an overflow.
80 */
81 if (((s32) now - (s32) last) < 0)
82 now += 0x0000000100000000ULL;
83 } else if (!(src_now & 0x80000000U) && (last & 0x80000000U)) {
84 /*
85 * If 32nd bit transitions from 0 to 1, and we move backward in
86 * time from "now" to "last", then we have an underflow.
87 */
88 if (((s32) now - (s32) last) > 0)
89 now -= 0x0000000100000000ULL;
90 }
91 return now;
92 }
93 #else /* #if (BITS_PER_LONG == 32) */
94 /*
95 * The fixup is pretty easy on 64-bit architectures: "last" is a 64-bit
96 * value, so we can use last directly as current time.
97 */
98 static inline u64 trace_clock_fixup(u64 src_now, u64 last)
99 {
100 return last;
101 }
102 #endif /* #else #if (BITS_PER_LONG == 32) */
103
104 /*
105 * Sometimes called with preemption enabled. Can be interrupted.
106 */
107 static inline u64 trace_clock_monotonic_wrapper(void)
108 {
109 u64 now;
110 unsigned long last, result;
111 local_t *last_tsc;
112
113 /* Use fast nmi-safe monotonic clock provided by the Linux kernel. */
114 preempt_disable();
115 last_tsc = lttng_this_cpu_ptr(&lttng_last_tsc);
116 last = local_read(last_tsc);
117 /*
118 * Read "last" before "now". It is not strictly required, but it ensures
119 * that an interrupt coming in won't artificially trigger a case where
120 * "now" < "last". This kind of situation should only happen if the
121 * mono_fast time source goes slightly backwards.
122 */
123 barrier();
124 now = ktime_get_mono_fast_ns();
125 if (((long) now - (long) last) < 0)
126 now = trace_clock_fixup(now, last);
127 result = local_cmpxchg(last_tsc, last, (unsigned long) now);
128 preempt_enable();
129 if (result == last) {
130 /* Update done. */
131 return now;
132 } else {
133 /*
134 * Update not done, due to concurrent update. We can use
135 * "result", since it has been sampled concurrently with our
136 * time read, so it should not be far from "now".
137 */
138 return trace_clock_fixup(now, result);
139 }
140 }
141
142 #else /* #if (LINUX_VERSION_CODE >= KERNEL_VERSION(3,17,0)) */
143 static inline u64 trace_clock_monotonic_wrapper(void)
144 {
145 ktime_t ktime;
146
147 /*
148 * Refuse to trace from NMIs with this wrapper, because an NMI could
149 * nest over the xtime write seqlock and deadlock.
150 */
151 if (in_nmi())
152 return (u64) -EIO;
153
154 ktime = ktime_get();
155 return ktime_to_ns(ktime);
156 }
157 #endif /* #else #if (LINUX_VERSION_CODE >= KERNEL_VERSION(3,17,0)) */
158
159 static inline u64 trace_clock_read64_monotonic(void)
160 {
161 return (u64) trace_clock_monotonic_wrapper();
162 }
163
164 static inline u64 trace_clock_freq_monotonic(void)
165 {
166 return (u64) NSEC_PER_SEC;
167 }
168
169 static inline int trace_clock_uuid_monotonic(char *uuid)
170 {
171 return wrapper_get_bootid(uuid);
172 }
173
174 static inline const char *trace_clock_name_monotonic(void)
175 {
176 return "monotonic";
177 }
178
179 static inline const char *trace_clock_description_monotonic(void)
180 {
181 return "Monotonic Clock";
182 }
183
184 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(3,17,0))
185 static inline int get_trace_clock(void)
186 {
187 printk_once(KERN_WARNING "LTTng: Using mainline kernel monotonic fast clock, which is NMI-safe.\n");
188 return 0;
189 }
190 #else /* #if (LINUX_VERSION_CODE >= KERNEL_VERSION(3,17,0)) */
191 static inline int get_trace_clock(void)
192 {
193 printk_once(KERN_WARNING "LTTng: Using mainline kernel monotonic clock. NMIs will not be traced.\n");
194 return 0;
195 }
196 #endif /* #else #if (LINUX_VERSION_CODE >= KERNEL_VERSION(3,17,0)) */
197
198 static inline void put_trace_clock(void)
199 {
200 }
201
202 static inline u64 trace_clock_read64(void)
203 {
204 struct lttng_trace_clock *ltc = ACCESS_ONCE(lttng_trace_clock);
205
206 if (likely(!ltc)) {
207 return trace_clock_read64_monotonic();
208 } else {
209 read_barrier_depends(); /* load ltc before content */
210 return ltc->read64();
211 }
212 }
213
214 static inline u64 trace_clock_freq(void)
215 {
216 struct lttng_trace_clock *ltc = ACCESS_ONCE(lttng_trace_clock);
217
218 if (!ltc) {
219 return trace_clock_freq_monotonic();
220 } else {
221 read_barrier_depends(); /* load ltc before content */
222 return ltc->freq();
223 }
224 }
225
226 static inline int trace_clock_uuid(char *uuid)
227 {
228 struct lttng_trace_clock *ltc = ACCESS_ONCE(lttng_trace_clock);
229
230 read_barrier_depends(); /* load ltc before content */
231 /* Use default UUID cb when NULL */
232 if (!ltc || !ltc->uuid) {
233 return trace_clock_uuid_monotonic(uuid);
234 } else {
235 return ltc->uuid(uuid);
236 }
237 }
238
239 static inline const char *trace_clock_name(void)
240 {
241 struct lttng_trace_clock *ltc = ACCESS_ONCE(lttng_trace_clock);
242
243 if (!ltc) {
244 return trace_clock_name_monotonic();
245 } else {
246 read_barrier_depends(); /* load ltc before content */
247 return ltc->name();
248 }
249 }
250
251 static inline const char *trace_clock_description(void)
252 {
253 struct lttng_trace_clock *ltc = ACCESS_ONCE(lttng_trace_clock);
254
255 if (!ltc) {
256 return trace_clock_description_monotonic();
257 } else {
258 read_barrier_depends(); /* load ltc before content */
259 return ltc->description();
260 }
261 }
262
263 #endif /* CONFIG_HAVE_TRACE_CLOCK */
264
265 #endif /* _LTTNG_TRACE_CLOCK_H */
This page took 0.03367 seconds and 3 git commands to generate.