Fix: show warning for broken clock work-around
[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>
9998f521 38#include "../lttng-kernel-version.h"
e6b06d7d 39#include "percpu-defs.h"
a82c63f1 40#include "random.h"
f6c19f6e 41
8d2c7a7c 42#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 43 || LTTNG_KERNEL_RANGE(3,11,0, 3,11,3))
9998f521 44#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
45#endif
46
95678343
MD
47/*
48 * Upstream Linux commit 27727df240c7 ("Avoid taking lock in NMI path with
49 * CONFIG_DEBUG_TIMEKEEPING") introduces a buggy ktime_get_mono_fast_ns().
50 * This is fixed by patch "timekeeping: Fix __ktime_get_fast_ns() regression".
51 */
2c888988
MD
52#if (LTTNG_KERNEL_RANGE(4,8,0, 4,8,1) \
53 || LTTNG_KERNEL_RANGE(4,7,4, 4,7,7) \
54 || LTTNG_KERNEL_RANGE(4,4,20, 4,4,24) \
55 || LTTNG_KERNEL_RANGE(4,1,32, 4,1,34))
56#define LTTNG_CLOCK_NMI_SAFE_BROKEN
57#endif
58
95678343 59#if (LINUX_VERSION_CODE >= KERNEL_VERSION(3,17,0) \
2c888988 60 && !defined(LTTNG_CLOCK_NMI_SAFE_BROKEN))
b0725207
MD
61
62DECLARE_PER_CPU(local_t, lttng_last_tsc);
63
64#if (BITS_PER_LONG == 32)
65/*
66 * Fixup "src_now" using the 32 LSB from "last". We need to handle overflow and
67 * underflow of the 32nd bit. "last" can be above, below or equal to the 32 LSB
68 * of "src_now".
69 */
70static inline u64 trace_clock_fixup(u64 src_now, u32 last)
71{
72 u64 now;
73
74 now = src_now & 0xFFFFFFFF00000000ULL;
75 now |= (u64) last;
76 /* Detect overflow or underflow between now and last. */
77 if ((src_now & 0x80000000U) && !(last & 0x80000000U)) {
78 /*
79 * If 32nd bit transitions from 1 to 0, and we move forward in
80 * time from "now" to "last", then we have an overflow.
81 */
82 if (((s32) now - (s32) last) < 0)
83 now += 0x0000000100000000ULL;
84 } else if (!(src_now & 0x80000000U) && (last & 0x80000000U)) {
85 /*
86 * If 32nd bit transitions from 0 to 1, and we move backward in
87 * time from "now" to "last", then we have an underflow.
88 */
89 if (((s32) now - (s32) last) > 0)
90 now -= 0x0000000100000000ULL;
91 }
92 return now;
93}
94#else /* #if (BITS_PER_LONG == 32) */
95/*
96 * The fixup is pretty easy on 64-bit architectures: "last" is a 64-bit
97 * value, so we can use last directly as current time.
98 */
99static inline u64 trace_clock_fixup(u64 src_now, u64 last)
100{
101 return last;
102}
103#endif /* #else #if (BITS_PER_LONG == 32) */
104
105/*
fe2ebb3d 106 * Sometimes called with preemption enabled. Can be interrupted.
b0725207
MD
107 */
108static inline u64 trace_clock_monotonic_wrapper(void)
109{
110 u64 now;
111 unsigned long last, result;
112 local_t *last_tsc;
113
114 /* Use fast nmi-safe monotonic clock provided by the Linux kernel. */
fe2ebb3d 115 preempt_disable();
e6b06d7d 116 last_tsc = lttng_this_cpu_ptr(&lttng_last_tsc);
b0725207
MD
117 last = local_read(last_tsc);
118 /*
119 * Read "last" before "now". It is not strictly required, but it ensures
120 * that an interrupt coming in won't artificially trigger a case where
121 * "now" < "last". This kind of situation should only happen if the
122 * mono_fast time source goes slightly backwards.
123 */
124 barrier();
125 now = ktime_get_mono_fast_ns();
126 if (((long) now - (long) last) < 0)
127 now = trace_clock_fixup(now, last);
128 result = local_cmpxchg(last_tsc, last, (unsigned long) now);
fe2ebb3d 129 preempt_enable();
b0725207
MD
130 if (result == last) {
131 /* Update done. */
132 return now;
133 } else {
134 /*
135 * Update not done, due to concurrent update. We can use
136 * "result", since it has been sampled concurrently with our
137 * time read, so it should not be far from "now".
138 */
139 return trace_clock_fixup(now, result);
140 }
141}
142
143#else /* #if (LINUX_VERSION_CODE >= KERNEL_VERSION(3,17,0)) */
f6c19f6e
MD
144static inline u64 trace_clock_monotonic_wrapper(void)
145{
146 ktime_t ktime;
147
148 /*
149 * Refuse to trace from NMIs with this wrapper, because an NMI could
150 * nest over the xtime write seqlock and deadlock.
151 */
152 if (in_nmi())
97ca2c54 153 return (u64) -EIO;
f6c19f6e
MD
154
155 ktime = ktime_get();
cfaf9f3d 156 return ktime_to_ns(ktime);
f6c19f6e 157}
b0725207 158#endif /* #else #if (LINUX_VERSION_CODE >= KERNEL_VERSION(3,17,0)) */
f6c19f6e 159
f6c19f6e
MD
160static inline u64 trace_clock_read64(void)
161{
162 return (u64) trace_clock_monotonic_wrapper();
163}
164
a3ccff4f 165static inline u64 trace_clock_freq(void)
f6c19f6e 166{
a3ccff4f 167 return (u64) NSEC_PER_SEC;
f6c19f6e
MD
168}
169
a82c63f1 170static inline int trace_clock_uuid(char *uuid)
f6c19f6e 171{
a82c63f1 172 return wrapper_get_bootid(uuid);
f6c19f6e
MD
173}
174
b0725207 175#if (LINUX_VERSION_CODE >= KERNEL_VERSION(3,17,0))
f6c19f6e
MD
176static inline int get_trace_clock(void)
177{
b0725207
MD
178 printk(KERN_WARNING "LTTng: Using mainline kernel monotonic fast clock, which is NMI-safe.\n");
179 return 0;
180}
181#else /* #if (LINUX_VERSION_CODE >= KERNEL_VERSION(3,17,0)) */
182static inline int get_trace_clock(void)
183{
184 printk(KERN_WARNING "LTTng: Using mainline kernel monotonic clock. NMIs will not be traced.\n");
f6c19f6e
MD
185 return 0;
186}
b0725207 187#endif /* #else #if (LINUX_VERSION_CODE >= KERNEL_VERSION(3,17,0)) */
f6c19f6e
MD
188
189static inline void put_trace_clock(void)
190{
191}
192
193#endif /* CONFIG_HAVE_TRACE_CLOCK */
194
a90917c3 195#endif /* _LTTNG_TRACE_CLOCK_H */
This page took 0.038876 seconds and 4 git commands to generate.