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