Fix: update sched instrumentation for kernel 4.12
[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
53c4007f 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
1d2aa29e
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 */
2bdfbc55
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))
5b06448b
MD
59#define LTTNG_CLOCK_NMI_SAFE_BROKEN
60#endif
61
16200240
MD
62/*
63 * We need clock values to be monotonically increasing per-cpu, which is
64 * not strictly guaranteed by ktime_get_mono_fast_ns(). It is
65 * straightforward to do on architectures with a 64-bit cmpxchg(), but
66 * not so on architectures without 64-bit cmpxchg. For now, only enable
67 * this feature on 64-bit architectures.
68 */
69
1d2aa29e 70#if (LINUX_VERSION_CODE >= KERNEL_VERSION(3,17,0) \
16200240 71 && BITS_PER_LONG == 64 \
5b06448b 72 && !defined(LTTNG_CLOCK_NMI_SAFE_BROKEN))
16200240
MD
73#define LTTNG_USE_NMI_SAFE_CLOCK
74#endif
b0725207 75
16200240 76#ifdef LTTNG_USE_NMI_SAFE_CLOCK
b0725207 77
16200240 78DECLARE_PER_CPU(u64, lttng_last_tsc);
b0725207
MD
79
80/*
0aaa7220 81 * Sometimes called with preemption enabled. Can be interrupted.
b0725207
MD
82 */
83static inline u64 trace_clock_monotonic_wrapper(void)
84{
16200240
MD
85 u64 now, last, result;
86 u64 *last_tsc_ptr;
b0725207
MD
87
88 /* Use fast nmi-safe monotonic clock provided by the Linux kernel. */
0aaa7220 89 preempt_disable();
16200240
MD
90 last_tsc_ptr = lttng_this_cpu_ptr(&lttng_last_tsc);
91 last = *last_tsc_ptr;
b0725207
MD
92 /*
93 * Read "last" before "now". It is not strictly required, but it ensures
94 * that an interrupt coming in won't artificially trigger a case where
95 * "now" < "last". This kind of situation should only happen if the
96 * mono_fast time source goes slightly backwards.
97 */
98 barrier();
99 now = ktime_get_mono_fast_ns();
16200240
MD
100 if (U64_MAX / 2 < now - last)
101 now = last;
102 result = cmpxchg64_local(last_tsc_ptr, last, now);
0aaa7220 103 preempt_enable();
b0725207
MD
104 if (result == last) {
105 /* Update done. */
106 return now;
107 } else {
108 /*
109 * Update not done, due to concurrent update. We can use
110 * "result", since it has been sampled concurrently with our
111 * time read, so it should not be far from "now".
112 */
16200240 113 return result;
b0725207
MD
114 }
115}
116
16200240 117#else /* #ifdef LTTNG_USE_NMI_SAFE_CLOCK */
f6c19f6e
MD
118static inline u64 trace_clock_monotonic_wrapper(void)
119{
120 ktime_t ktime;
121
122 /*
123 * Refuse to trace from NMIs with this wrapper, because an NMI could
124 * nest over the xtime write seqlock and deadlock.
125 */
126 if (in_nmi())
97ca2c54 127 return (u64) -EIO;
f6c19f6e
MD
128
129 ktime = ktime_get();
cfaf9f3d 130 return ktime_to_ns(ktime);
f6c19f6e 131}
16200240 132#endif /* #else #ifdef LTTNG_USE_NMI_SAFE_CLOCK */
f6c19f6e 133
2754583e 134static inline u64 trace_clock_read64_monotonic(void)
f6c19f6e
MD
135{
136 return (u64) trace_clock_monotonic_wrapper();
137}
138
2754583e 139static inline u64 trace_clock_freq_monotonic(void)
f6c19f6e 140{
a3ccff4f 141 return (u64) NSEC_PER_SEC;
f6c19f6e
MD
142}
143
2754583e 144static inline int trace_clock_uuid_monotonic(char *uuid)
f6c19f6e 145{
a82c63f1 146 return wrapper_get_bootid(uuid);
f6c19f6e
MD
147}
148
2754583e
MD
149static inline const char *trace_clock_name_monotonic(void)
150{
151 return "monotonic";
152}
153
154static inline const char *trace_clock_description_monotonic(void)
155{
156 return "Monotonic Clock";
157}
158
16200240 159#ifdef LTTNG_USE_NMI_SAFE_CLOCK
f6c19f6e
MD
160static inline int get_trace_clock(void)
161{
2b0e9d96 162 printk_once(KERN_WARNING "LTTng: Using mainline kernel monotonic fast clock, which is NMI-safe.\n");
b0725207
MD
163 return 0;
164}
16200240 165#else /* #ifdef LTTNG_USE_NMI_SAFE_CLOCK */
b0725207
MD
166static inline int get_trace_clock(void)
167{
2b0e9d96 168 printk_once(KERN_WARNING "LTTng: Using mainline kernel monotonic clock. NMIs will not be traced.\n");
f6c19f6e
MD
169 return 0;
170}
16200240 171#endif /* #else #ifdef LTTNG_USE_NMI_SAFE_CLOCK */
f6c19f6e
MD
172
173static inline void put_trace_clock(void)
174{
175}
176
2754583e
MD
177static inline u64 trace_clock_read64(void)
178{
179 struct lttng_trace_clock *ltc = ACCESS_ONCE(lttng_trace_clock);
180
181 if (likely(!ltc)) {
182 return trace_clock_read64_monotonic();
183 } else {
184 read_barrier_depends(); /* load ltc before content */
185 return ltc->read64();
186 }
187}
188
189static inline u64 trace_clock_freq(void)
190{
191 struct lttng_trace_clock *ltc = ACCESS_ONCE(lttng_trace_clock);
192
193 if (!ltc) {
194 return trace_clock_freq_monotonic();
195 } else {
196 read_barrier_depends(); /* load ltc before content */
197 return ltc->freq();
198 }
199}
200
201static inline int trace_clock_uuid(char *uuid)
202{
203 struct lttng_trace_clock *ltc = ACCESS_ONCE(lttng_trace_clock);
204
205 read_barrier_depends(); /* load ltc before content */
206 /* Use default UUID cb when NULL */
207 if (!ltc || !ltc->uuid) {
208 return trace_clock_uuid_monotonic(uuid);
209 } else {
210 return ltc->uuid(uuid);
211 }
212}
213
214static inline const char *trace_clock_name(void)
215{
216 struct lttng_trace_clock *ltc = ACCESS_ONCE(lttng_trace_clock);
217
218 if (!ltc) {
219 return trace_clock_name_monotonic();
220 } else {
221 read_barrier_depends(); /* load ltc before content */
222 return ltc->name();
223 }
224}
225
226static inline const char *trace_clock_description(void)
227{
228 struct lttng_trace_clock *ltc = ACCESS_ONCE(lttng_trace_clock);
229
230 if (!ltc) {
231 return trace_clock_description_monotonic();
232 } else {
233 read_barrier_depends(); /* load ltc before content */
234 return ltc->description();
235 }
236}
237
f6c19f6e
MD
238#endif /* CONFIG_HAVE_TRACE_CLOCK */
239
a90917c3 240#endif /* _LTTNG_TRACE_CLOCK_H */
This page took 0.045109 seconds and 4 git commands to generate.