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