69dcc8003790a5ca9bb1486955237e20971cab35
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>
31 #include <blacklist/timekeeping.h>
33 extern struct lttng_trace_clock
*lttng_trace_clock
;
36 * Upstream Linux commit 27727df240c7 ("Avoid taking lock in NMI path with
37 * CONFIG_DEBUG_TIMEKEEPING") introduces a buggy ktime_get_mono_fast_ns().
38 * This is fixed by patch "timekeeping: Fix __ktime_get_fast_ns() regression".
40 #if (LTTNG_KERNEL_RANGE(4,8,0, 4,8,2) \
41 || LTTNG_KERNEL_RANGE(4,7,4, 4,7,8) \
42 || LTTNG_KERNEL_RANGE(4,4,20, 4,4,25) \
43 || LTTNG_KERNEL_RANGE(4,1,32, 4,1,35))
44 #define LTTNG_CLOCK_NMI_SAFE_BROKEN
48 * We need clock values to be monotonically increasing per-cpu, which is
49 * not strictly guaranteed by ktime_get_mono_fast_ns(). It is
50 * straightforward to do on architectures with a 64-bit cmpxchg(), but
51 * not so on architectures without 64-bit cmpxchg. For now, only enable
52 * this feature on 64-bit architectures.
55 #if (LTTNG_LINUX_VERSION_CODE >= LTTNG_KERNEL_VERSION(3,17,0) \
56 && BITS_PER_LONG == 64 \
57 && !defined(LTTNG_CLOCK_NMI_SAFE_BROKEN))
58 #define LTTNG_USE_NMI_SAFE_CLOCK
61 #ifdef LTTNG_USE_NMI_SAFE_CLOCK
63 DECLARE_PER_CPU(u64
, lttng_last_tsc
);
66 * Sometimes called with preemption enabled. Can be interrupted.
68 static inline u64
trace_clock_monotonic_wrapper(void)
70 u64 now
, last
, result
;
73 /* Use fast nmi-safe monotonic clock provided by the Linux kernel. */
75 last_tsc_ptr
= this_cpu_ptr(<tng_last_tsc
);
78 * Read "last" before "now". It is not strictly required, but it ensures
79 * that an interrupt coming in won't artificially trigger a case where
80 * "now" < "last". This kind of situation should only happen if the
81 * mono_fast time source goes slightly backwards.
84 now
= ktime_get_mono_fast_ns();
85 if (U64_MAX
/ 2 < now
- last
)
87 result
= cmpxchg64_local(last_tsc_ptr
, last
, now
);
94 * Update not done, due to concurrent update. We can use
95 * "result", since it has been sampled concurrently with our
96 * time read, so it should not be far from "now".
102 #else /* #ifdef LTTNG_USE_NMI_SAFE_CLOCK */
103 static inline u64
trace_clock_monotonic_wrapper(void)
108 * Refuse to trace from NMIs with this wrapper, because an NMI could
109 * nest over the xtime write seqlock and deadlock.
115 return ktime_to_ns(ktime
);
117 #endif /* #else #ifdef LTTNG_USE_NMI_SAFE_CLOCK */
119 static inline u64
trace_clock_read64_monotonic(void)
121 return (u64
) trace_clock_monotonic_wrapper();
124 static inline u64
trace_clock_freq_monotonic(void)
126 return (u64
) NSEC_PER_SEC
;
129 static inline int trace_clock_uuid_monotonic(char *uuid
)
131 return wrapper_get_bootid(uuid
);
134 static inline const char *trace_clock_name_monotonic(void)
139 static inline const char *trace_clock_description_monotonic(void)
141 return "Monotonic Clock";
144 #ifdef LTTNG_USE_NMI_SAFE_CLOCK
145 static inline int get_trace_clock(void)
147 printk_once(KERN_WARNING
"LTTng: Using mainline kernel monotonic fast clock, which is NMI-safe.\n");
150 #else /* #ifdef LTTNG_USE_NMI_SAFE_CLOCK */
151 static inline int get_trace_clock(void)
153 printk_once(KERN_WARNING
"LTTng: Using mainline kernel monotonic clock. NMIs will not be traced.\n");
156 #endif /* #else #ifdef LTTNG_USE_NMI_SAFE_CLOCK */
158 static inline void put_trace_clock(void)
162 static inline u64
trace_clock_read64(void)
164 struct lttng_trace_clock
*ltc
= LTTNG_READ_ONCE(lttng_trace_clock
);
167 return trace_clock_read64_monotonic();
169 return ltc
->read64();
173 static inline u64
trace_clock_freq(void)
175 struct lttng_trace_clock
*ltc
= LTTNG_READ_ONCE(lttng_trace_clock
);
178 return trace_clock_freq_monotonic();
184 static inline int trace_clock_uuid(char *uuid
)
186 struct lttng_trace_clock
*ltc
= LTTNG_READ_ONCE(lttng_trace_clock
);
188 /* Use default UUID cb when NULL */
189 if (!ltc
|| !ltc
->uuid
) {
190 return trace_clock_uuid_monotonic(uuid
);
192 return ltc
->uuid(uuid
);
196 static inline const char *trace_clock_name(void)
198 struct lttng_trace_clock
*ltc
= LTTNG_READ_ONCE(lttng_trace_clock
);
201 return trace_clock_name_monotonic();
207 static inline const char *trace_clock_description(void)
209 struct lttng_trace_clock
*ltc
= LTTNG_READ_ONCE(lttng_trace_clock
);
212 return trace_clock_description_monotonic();
214 return ltc
->description();
218 #endif /* CONFIG_HAVE_TRACE_CLOCK */
220 #endif /* _LTTNG_TRACE_CLOCK_H */
This page took 0.038036 seconds and 4 git commands to generate.