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