Cleanup: move to kernel style SPDX license identifiers
[lttng-modules.git] / wrapper / trace-clock.h
CommitLineData
9f36eaed
MJ
1/* SPDX-License-Identifier: (GPL-2.0 or LGPL-2.1)
2 *
886d51a3 3 * wrapper/trace-clock.h
f6c19f6e
MD
4 *
5 * Contains LTTng trace clock mapping to LTTng trace clock or mainline monotonic
6 * clock. This wrapper depends on CONFIG_HIGH_RES_TIMERS=y.
7 *
886d51a3 8 * Copyright (C) 2011-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
f6c19f6e
MD
9 */
10
9f36eaed
MJ
11#ifndef _LTTNG_TRACE_CLOCK_H
12#define _LTTNG_TRACE_CLOCK_H
13
f6c19f6e
MD
14#ifdef CONFIG_HAVE_TRACE_CLOCK
15#include <linux/trace-clock.h>
16#else /* CONFIG_HAVE_TRACE_CLOCK */
17
18#include <linux/hardirq.h>
19#include <linux/ktime.h>
20#include <linux/time.h>
21#include <linux/hrtimer.h>
b0725207 22#include <linux/percpu.h>
fc8216ae 23#include <linux/version.h>
b0725207 24#include <asm/local.h>
5a2f5e92
MD
25#include <lttng-kernel-version.h>
26#include <lttng-clock.h>
a8f2d0c7 27#include <wrapper/compiler.h>
5a2f5e92
MD
28#include <wrapper/percpu-defs.h>
29#include <wrapper/random.h>
f6c19f6e 30
c94ac1ac 31#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 32 || LTTNG_KERNEL_RANGE(3,11,0, 3,11,3))
9998f521 33#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
34#endif
35
2754583e
MD
36extern struct lttng_trace_clock *lttng_trace_clock;
37
a9df1445
MD
38/*
39 * Upstream Linux commit 27727df240c7 ("Avoid taking lock in NMI path with
40 * CONFIG_DEBUG_TIMEKEEPING") introduces a buggy ktime_get_mono_fast_ns().
41 * This is fixed by patch "timekeeping: Fix __ktime_get_fast_ns() regression".
42 */
7d99572f
MD
43#if (LTTNG_KERNEL_RANGE(4,8,0, 4,8,2) \
44 || LTTNG_KERNEL_RANGE(4,7,4, 4,7,8) \
45 || LTTNG_KERNEL_RANGE(4,4,20, 4,4,25) \
46 || LTTNG_KERNEL_RANGE(4,1,32, 4,1,35))
254adeb0
MD
47#define LTTNG_CLOCK_NMI_SAFE_BROKEN
48#endif
49
60e1cd07
MD
50/*
51 * We need clock values to be monotonically increasing per-cpu, which is
52 * not strictly guaranteed by ktime_get_mono_fast_ns(). It is
53 * straightforward to do on architectures with a 64-bit cmpxchg(), but
54 * not so on architectures without 64-bit cmpxchg. For now, only enable
55 * this feature on 64-bit architectures.
56 */
57
a9df1445 58#if (LINUX_VERSION_CODE >= KERNEL_VERSION(3,17,0) \
60e1cd07 59 && BITS_PER_LONG == 64 \
254adeb0 60 && !defined(LTTNG_CLOCK_NMI_SAFE_BROKEN))
60e1cd07
MD
61#define LTTNG_USE_NMI_SAFE_CLOCK
62#endif
b0725207 63
60e1cd07 64#ifdef LTTNG_USE_NMI_SAFE_CLOCK
b0725207 65
60e1cd07 66DECLARE_PER_CPU(u64, lttng_last_tsc);
b0725207
MD
67
68/*
0aaa7220 69 * Sometimes called with preemption enabled. Can be interrupted.
b0725207
MD
70 */
71static inline u64 trace_clock_monotonic_wrapper(void)
72{
60e1cd07
MD
73 u64 now, last, result;
74 u64 *last_tsc_ptr;
b0725207
MD
75
76 /* Use fast nmi-safe monotonic clock provided by the Linux kernel. */
0aaa7220 77 preempt_disable();
60e1cd07
MD
78 last_tsc_ptr = lttng_this_cpu_ptr(&lttng_last_tsc);
79 last = *last_tsc_ptr;
b0725207
MD
80 /*
81 * Read "last" before "now". It is not strictly required, but it ensures
82 * that an interrupt coming in won't artificially trigger a case where
83 * "now" < "last". This kind of situation should only happen if the
84 * mono_fast time source goes slightly backwards.
85 */
86 barrier();
87 now = ktime_get_mono_fast_ns();
60e1cd07
MD
88 if (U64_MAX / 2 < now - last)
89 now = last;
90 result = cmpxchg64_local(last_tsc_ptr, last, now);
0aaa7220 91 preempt_enable();
b0725207
MD
92 if (result == last) {
93 /* Update done. */
94 return now;
95 } else {
96 /*
97 * Update not done, due to concurrent update. We can use
98 * "result", since it has been sampled concurrently with our
99 * time read, so it should not be far from "now".
100 */
60e1cd07 101 return result;
b0725207
MD
102 }
103}
104
60e1cd07 105#else /* #ifdef LTTNG_USE_NMI_SAFE_CLOCK */
f6c19f6e
MD
106static inline u64 trace_clock_monotonic_wrapper(void)
107{
108 ktime_t ktime;
109
110 /*
111 * Refuse to trace from NMIs with this wrapper, because an NMI could
112 * nest over the xtime write seqlock and deadlock.
113 */
114 if (in_nmi())
97ca2c54 115 return (u64) -EIO;
f6c19f6e
MD
116
117 ktime = ktime_get();
cfaf9f3d 118 return ktime_to_ns(ktime);
f6c19f6e 119}
60e1cd07 120#endif /* #else #ifdef LTTNG_USE_NMI_SAFE_CLOCK */
f6c19f6e 121
2754583e 122static inline u64 trace_clock_read64_monotonic(void)
f6c19f6e
MD
123{
124 return (u64) trace_clock_monotonic_wrapper();
125}
126
2754583e 127static inline u64 trace_clock_freq_monotonic(void)
f6c19f6e 128{
a3ccff4f 129 return (u64) NSEC_PER_SEC;
f6c19f6e
MD
130}
131
2754583e 132static inline int trace_clock_uuid_monotonic(char *uuid)
f6c19f6e 133{
a82c63f1 134 return wrapper_get_bootid(uuid);
f6c19f6e
MD
135}
136
2754583e
MD
137static inline const char *trace_clock_name_monotonic(void)
138{
139 return "monotonic";
140}
141
142static inline const char *trace_clock_description_monotonic(void)
143{
144 return "Monotonic Clock";
145}
146
60e1cd07 147#ifdef LTTNG_USE_NMI_SAFE_CLOCK
f6c19f6e
MD
148static inline int get_trace_clock(void)
149{
e36de50d 150 printk_once(KERN_WARNING "LTTng: Using mainline kernel monotonic fast clock, which is NMI-safe.\n");
b0725207
MD
151 return 0;
152}
60e1cd07 153#else /* #ifdef LTTNG_USE_NMI_SAFE_CLOCK */
b0725207
MD
154static inline int get_trace_clock(void)
155{
e36de50d 156 printk_once(KERN_WARNING "LTTng: Using mainline kernel monotonic clock. NMIs will not be traced.\n");
f6c19f6e
MD
157 return 0;
158}
60e1cd07 159#endif /* #else #ifdef LTTNG_USE_NMI_SAFE_CLOCK */
f6c19f6e
MD
160
161static inline void put_trace_clock(void)
162{
163}
164
2754583e
MD
165static inline u64 trace_clock_read64(void)
166{
a8f2d0c7 167 struct lttng_trace_clock *ltc = READ_ONCE(lttng_trace_clock);
2754583e
MD
168
169 if (likely(!ltc)) {
170 return trace_clock_read64_monotonic();
171 } else {
172 read_barrier_depends(); /* load ltc before content */
173 return ltc->read64();
174 }
175}
176
177static inline u64 trace_clock_freq(void)
178{
a8f2d0c7 179 struct lttng_trace_clock *ltc = READ_ONCE(lttng_trace_clock);
2754583e
MD
180
181 if (!ltc) {
182 return trace_clock_freq_monotonic();
183 } else {
184 read_barrier_depends(); /* load ltc before content */
185 return ltc->freq();
186 }
187}
188
189static inline int trace_clock_uuid(char *uuid)
190{
a8f2d0c7 191 struct lttng_trace_clock *ltc = READ_ONCE(lttng_trace_clock);
2754583e
MD
192
193 read_barrier_depends(); /* load ltc before content */
194 /* Use default UUID cb when NULL */
195 if (!ltc || !ltc->uuid) {
196 return trace_clock_uuid_monotonic(uuid);
197 } else {
198 return ltc->uuid(uuid);
199 }
200}
201
202static inline const char *trace_clock_name(void)
203{
a8f2d0c7 204 struct lttng_trace_clock *ltc = READ_ONCE(lttng_trace_clock);
2754583e
MD
205
206 if (!ltc) {
207 return trace_clock_name_monotonic();
208 } else {
209 read_barrier_depends(); /* load ltc before content */
210 return ltc->name();
211 }
212}
213
214static inline const char *trace_clock_description(void)
215{
a8f2d0c7 216 struct lttng_trace_clock *ltc = READ_ONCE(lttng_trace_clock);
2754583e
MD
217
218 if (!ltc) {
219 return trace_clock_description_monotonic();
220 } else {
221 read_barrier_depends(); /* load ltc before content */
222 return ltc->description();
223 }
224}
225
f6c19f6e
MD
226#endif /* CONFIG_HAVE_TRACE_CLOCK */
227
a90917c3 228#endif /* _LTTNG_TRACE_CLOCK_H */
This page took 0.043584 seconds and 4 git commands to generate.