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