1 /* SPDX-License-Identifier: (GPL-2.0-only or LGPL-2.1-only)
3 * wrapper/trace-clock.h
5 * Contains LTTng trace clock mapping to LTTng trace clock or mainline monotonic
6 * clock. This wrapper depends on CONFIG_HIGH_RES_TIMERS=y.
8 * Copyright (C) 2011-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
11 #ifndef _LTTNG_TRACE_CLOCK_H
12 #define _LTTNG_TRACE_CLOCK_H
14 #ifdef CONFIG_HAVE_TRACE_CLOCK
15 #include <linux/trace-clock.h>
16 #else /* CONFIG_HAVE_TRACE_CLOCK */
18 #include <linux/hardirq.h>
19 #include <linux/ktime.h>
20 #include <linux/time.h>
21 #include <linux/hrtimer.h>
22 #include <linux/percpu.h>
23 #include <linux/percpu-defs.h>
25 #include <lttng/kernel-version.h>
26 #include <asm/local.h>
27 #include <lttng/kernel-version.h>
28 #include <lttng/clock.h>
29 #include <wrapper/compiler.h>
30 #include <wrapper/random.h>
32 extern struct lttng_trace_clock
*lttng_trace_clock
;
35 * Upstream Linux commit 27727df240c7 ("Avoid taking lock in NMI path with
36 * CONFIG_DEBUG_TIMEKEEPING") introduces a buggy ktime_get_mono_fast_ns().
37 * This is fixed by patch "timekeeping: Fix __ktime_get_fast_ns() regression".
39 #if (LTTNG_KERNEL_RANGE(4,8,0, 4,8,2) \
40 || LTTNG_KERNEL_RANGE(4,7,4, 4,7,8) \
41 || LTTNG_KERNEL_RANGE(4,4,20, 4,4,25))
42 #define LTTNG_CLOCK_NMI_SAFE_BROKEN
46 * We need clock values to be monotonically increasing per-cpu, which is
47 * not strictly guaranteed by ktime_get_mono_fast_ns(). It is
48 * straightforward to do on architectures with a 64-bit cmpxchg(), but
49 * not so on architectures without 64-bit cmpxchg. For now, only enable
50 * this feature on 64-bit architectures.
53 #if (BITS_PER_LONG == 64 && !defined(LTTNG_CLOCK_NMI_SAFE_BROKEN))
54 #define LTTNG_USE_NMI_SAFE_CLOCK
57 #ifdef LTTNG_USE_NMI_SAFE_CLOCK
59 DECLARE_PER_CPU(u64
, lttng_last_tsc
);
62 * Sometimes called with preemption enabled. Can be interrupted.
64 static inline u64
trace_clock_monotonic_wrapper(void)
66 u64 now
, last
, result
;
69 /* Use fast nmi-safe monotonic clock provided by the Linux kernel. */
71 last_tsc_ptr
= this_cpu_ptr(<tng_last_tsc
);
74 * Read "last" before "now". It is not strictly required, but it ensures
75 * that an interrupt coming in won't artificially trigger a case where
76 * "now" < "last". This kind of situation should only happen if the
77 * mono_fast time source goes slightly backwards.
80 now
= ktime_get_mono_fast_ns();
81 if (U64_MAX
/ 2 < now
- last
)
83 result
= cmpxchg64_local(last_tsc_ptr
, last
, now
);
90 * Update not done, due to concurrent update. We can use
91 * "result", since it has been sampled concurrently with our
92 * time read, so it should not be far from "now".
98 #else /* #ifdef LTTNG_USE_NMI_SAFE_CLOCK */
99 static inline u64
trace_clock_monotonic_wrapper(void)
104 * Refuse to trace from NMIs with this wrapper, because an NMI could
105 * nest over the xtime write seqlock and deadlock.
111 return ktime_to_ns(ktime
);
113 #endif /* #else #ifdef LTTNG_USE_NMI_SAFE_CLOCK */
115 static inline u64
trace_clock_read64_monotonic(void)
117 return (u64
) trace_clock_monotonic_wrapper();
120 static inline u64
trace_clock_freq_monotonic(void)
122 return (u64
) NSEC_PER_SEC
;
125 static inline int trace_clock_uuid_monotonic(char *uuid
)
127 return wrapper_get_bootid(uuid
);
130 static inline const char *trace_clock_name_monotonic(void)
135 static inline const char *trace_clock_description_monotonic(void)
137 return "Monotonic Clock";
140 #ifdef LTTNG_USE_NMI_SAFE_CLOCK
141 static inline int get_trace_clock(void)
143 printk_once(KERN_WARNING
"LTTng: Using mainline kernel monotonic fast clock, which is NMI-safe.\n");
146 #else /* #ifdef LTTNG_USE_NMI_SAFE_CLOCK */
147 static inline int get_trace_clock(void)
149 printk_once(KERN_WARNING
"LTTng: Using mainline kernel monotonic clock. NMIs will not be traced.\n");
152 #endif /* #else #ifdef LTTNG_USE_NMI_SAFE_CLOCK */
154 static inline void put_trace_clock(void)
158 static inline u64
trace_clock_read64(void)
160 struct lttng_trace_clock
*ltc
= LTTNG_READ_ONCE(lttng_trace_clock
);
163 return trace_clock_read64_monotonic();
165 return ltc
->read64();
169 static inline u64
trace_clock_freq(void)
171 struct lttng_trace_clock
*ltc
= LTTNG_READ_ONCE(lttng_trace_clock
);
174 return trace_clock_freq_monotonic();
180 static inline int trace_clock_uuid(char *uuid
)
182 struct lttng_trace_clock
*ltc
= LTTNG_READ_ONCE(lttng_trace_clock
);
184 /* Use default UUID cb when NULL */
185 if (!ltc
|| !ltc
->uuid
) {
186 return trace_clock_uuid_monotonic(uuid
);
188 return ltc
->uuid(uuid
);
192 static inline const char *trace_clock_name(void)
194 struct lttng_trace_clock
*ltc
= LTTNG_READ_ONCE(lttng_trace_clock
);
197 return trace_clock_name_monotonic();
203 static inline const char *trace_clock_description(void)
205 struct lttng_trace_clock
*ltc
= LTTNG_READ_ONCE(lttng_trace_clock
);
208 return trace_clock_description_monotonic();
210 return ltc
->description();
214 #endif /* CONFIG_HAVE_TRACE_CLOCK */
216 #endif /* _LTTNG_TRACE_CLOCK_H */
This page took 0.033555 seconds and 4 git commands to generate.