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