1 #ifndef _LTTNG_TRACE_CLOCK_H
2 #define _LTTNG_TRACE_CLOCK_H
5 * wrapper/trace-clock.h
7 * Contains LTTng trace clock mapping to LTTng trace clock or mainline monotonic
8 * clock. This wrapper depends on CONFIG_HIGH_RES_TIMERS=y.
10 * Copyright (C) 2011-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
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.
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.
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
27 #ifdef CONFIG_HAVE_TRACE_CLOCK
28 #include <linux/trace-clock.h>
29 #else /* CONFIG_HAVE_TRACE_CLOCK */
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/compiler.h>
41 #include <wrapper/percpu-defs.h>
42 #include <wrapper/random.h>
44 #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)) \
45 || LTTNG_KERNEL_RANGE(3,11,0, 3,11,3))
46 #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."
49 extern struct lttng_trace_clock
*lttng_trace_clock
;
52 * Upstream Linux commit 27727df240c7 ("Avoid taking lock in NMI path with
53 * CONFIG_DEBUG_TIMEKEEPING") introduces a buggy ktime_get_mono_fast_ns().
54 * This is fixed by patch "timekeeping: Fix __ktime_get_fast_ns() regression".
56 #if (LTTNG_KERNEL_RANGE(4,8,0, 4,8,2) \
57 || LTTNG_KERNEL_RANGE(4,7,4, 4,7,8) \
58 || LTTNG_KERNEL_RANGE(4,4,20, 4,4,25) \
59 || LTTNG_KERNEL_RANGE(4,1,32, 4,1,35))
60 #define LTTNG_CLOCK_NMI_SAFE_BROKEN
64 * We need clock values to be monotonically increasing per-cpu, which is
65 * not strictly guaranteed by ktime_get_mono_fast_ns(). It is
66 * straightforward to do on architectures with a 64-bit cmpxchg(), but
67 * not so on architectures without 64-bit cmpxchg. For now, only enable
68 * this feature on 64-bit architectures.
71 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(3,17,0) \
72 && BITS_PER_LONG == 64 \
73 && !defined(LTTNG_CLOCK_NMI_SAFE_BROKEN))
74 #define LTTNG_USE_NMI_SAFE_CLOCK
77 #ifdef LTTNG_USE_NMI_SAFE_CLOCK
79 DECLARE_PER_CPU(u64
, lttng_last_tsc
);
82 * Sometimes called with preemption enabled. Can be interrupted.
84 static inline u64
trace_clock_monotonic_wrapper(void)
86 u64 now
, last
, result
;
89 /* Use fast nmi-safe monotonic clock provided by the Linux kernel. */
91 last_tsc_ptr
= lttng_this_cpu_ptr(<tng_last_tsc
);
94 * Read "last" before "now". It is not strictly required, but it ensures
95 * that an interrupt coming in won't artificially trigger a case where
96 * "now" < "last". This kind of situation should only happen if the
97 * mono_fast time source goes slightly backwards.
100 now
= ktime_get_mono_fast_ns();
101 if (U64_MAX
/ 2 < now
- last
)
103 result
= cmpxchg64_local(last_tsc_ptr
, last
, now
);
105 if (result
== last
) {
110 * Update not done, due to concurrent update. We can use
111 * "result", since it has been sampled concurrently with our
112 * time read, so it should not be far from "now".
118 #else /* #ifdef LTTNG_USE_NMI_SAFE_CLOCK */
119 static inline u64
trace_clock_monotonic_wrapper(void)
124 * Refuse to trace from NMIs with this wrapper, because an NMI could
125 * nest over the xtime write seqlock and deadlock.
131 return ktime_to_ns(ktime
);
133 #endif /* #else #ifdef LTTNG_USE_NMI_SAFE_CLOCK */
135 static inline u64
trace_clock_read64_monotonic(void)
137 return (u64
) trace_clock_monotonic_wrapper();
140 static inline u64
trace_clock_freq_monotonic(void)
142 return (u64
) NSEC_PER_SEC
;
145 static inline int trace_clock_uuid_monotonic(char *uuid
)
147 return wrapper_get_bootid(uuid
);
150 static inline const char *trace_clock_name_monotonic(void)
155 static inline const char *trace_clock_description_monotonic(void)
157 return "Monotonic Clock";
160 #ifdef LTTNG_USE_NMI_SAFE_CLOCK
161 static inline int get_trace_clock(void)
163 printk_once(KERN_WARNING
"LTTng: Using mainline kernel monotonic fast clock, which is NMI-safe.\n");
166 #else /* #ifdef LTTNG_USE_NMI_SAFE_CLOCK */
167 static inline int get_trace_clock(void)
169 printk_once(KERN_WARNING
"LTTng: Using mainline kernel monotonic clock. NMIs will not be traced.\n");
172 #endif /* #else #ifdef LTTNG_USE_NMI_SAFE_CLOCK */
174 static inline void put_trace_clock(void)
178 static inline u64
trace_clock_read64(void)
180 struct lttng_trace_clock
*ltc
= READ_ONCE(lttng_trace_clock
);
183 return trace_clock_read64_monotonic();
185 read_barrier_depends(); /* load ltc before content */
186 return ltc
->read64();
190 static inline u64
trace_clock_freq(void)
192 struct lttng_trace_clock
*ltc
= READ_ONCE(lttng_trace_clock
);
195 return trace_clock_freq_monotonic();
197 read_barrier_depends(); /* load ltc before content */
202 static inline int trace_clock_uuid(char *uuid
)
204 struct lttng_trace_clock
*ltc
= READ_ONCE(lttng_trace_clock
);
206 read_barrier_depends(); /* load ltc before content */
207 /* Use default UUID cb when NULL */
208 if (!ltc
|| !ltc
->uuid
) {
209 return trace_clock_uuid_monotonic(uuid
);
211 return ltc
->uuid(uuid
);
215 static inline const char *trace_clock_name(void)
217 struct lttng_trace_clock
*ltc
= READ_ONCE(lttng_trace_clock
);
220 return trace_clock_name_monotonic();
222 read_barrier_depends(); /* load ltc before content */
227 static inline const char *trace_clock_description(void)
229 struct lttng_trace_clock
*ltc
= READ_ONCE(lttng_trace_clock
);
232 return trace_clock_description_monotonic();
234 read_barrier_depends(); /* load ltc before content */
235 return ltc
->description();
239 #endif /* CONFIG_HAVE_TRACE_CLOCK */
241 #endif /* _LTTNG_TRACE_CLOCK_H */
This page took 0.03775 seconds and 4 git commands to generate.