1 /* SPDX-License-Identifier: (GPL-2.0-only or LGPL-2.1-only)
3 * wrapper/trace-clock.h
5 * Contains LTTng trace clock mapping to LTTng trace clock or mainline monotonic
6 * clock. This wrapper depends on CONFIG_HIGH_RES_TIMERS=y.
8 * Copyright (C) 2011-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
11 #ifndef _LTTNG_TRACE_CLOCK_H
12 #define _LTTNG_TRACE_CLOCK_H
14 #ifdef CONFIG_HAVE_TRACE_CLOCK
15 #include <linux/trace-clock.h>
16 #else /* CONFIG_HAVE_TRACE_CLOCK */
18 #include <linux/hardirq.h>
19 #include <linux/ktime.h>
20 #include <linux/time.h>
21 #include <linux/hrtimer.h>
22 #include <linux/percpu.h>
23 #include <linux/version.h>
24 #include <linux/percpu-defs.h>
25 #include <asm/local.h>
26 #include <lttng-kernel-version.h>
27 #include <lttng-clock.h>
28 #include <wrapper/random.h>
30 extern struct lttng_trace_clock
*lttng_trace_clock
;
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.
40 #if BITS_PER_LONG == 64
41 #define LTTNG_USE_NMI_SAFE_CLOCK
44 #ifdef LTTNG_USE_NMI_SAFE_CLOCK
46 DECLARE_PER_CPU(u64
, lttng_last_tsc
);
49 * Sometimes called with preemption enabled. Can be interrupted.
51 static inline u64
trace_clock_monotonic_wrapper(void)
53 u64 now
, last
, result
;
56 /* Use fast nmi-safe monotonic clock provided by the Linux kernel. */
58 last_tsc_ptr
= this_cpu_ptr(<tng_last_tsc
);
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.
67 now
= ktime_get_mono_fast_ns();
68 if (U64_MAX
/ 2 < now
- last
)
70 result
= cmpxchg64_local(last_tsc_ptr
, last
, now
);
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".
85 #else /* #ifdef LTTNG_USE_NMI_SAFE_CLOCK */
86 static inline u64
trace_clock_monotonic_wrapper(void)
91 * Refuse to trace from NMIs with this wrapper, because an NMI could
92 * nest over the xtime write seqlock and deadlock.
98 return ktime_to_ns(ktime
);
100 #endif /* #else #ifdef LTTNG_USE_NMI_SAFE_CLOCK */
102 static inline u64
trace_clock_read64_monotonic(void)
104 return (u64
) trace_clock_monotonic_wrapper();
107 static inline u64
trace_clock_freq_monotonic(void)
109 return (u64
) NSEC_PER_SEC
;
112 static inline int trace_clock_uuid_monotonic(char *uuid
)
114 return wrapper_get_bootid(uuid
);
117 static inline const char *trace_clock_name_monotonic(void)
122 static inline const char *trace_clock_description_monotonic(void)
124 return "Monotonic Clock";
127 #ifdef LTTNG_USE_NMI_SAFE_CLOCK
128 static inline int get_trace_clock(void)
130 printk_once(KERN_WARNING
"LTTng: Using mainline kernel monotonic fast clock, which is NMI-safe.\n");
133 #else /* #ifdef LTTNG_USE_NMI_SAFE_CLOCK */
134 static inline int get_trace_clock(void)
136 printk_once(KERN_WARNING
"LTTng: Using mainline kernel monotonic clock. NMIs will not be traced.\n");
139 #endif /* #else #ifdef LTTNG_USE_NMI_SAFE_CLOCK */
141 static inline void put_trace_clock(void)
145 static inline u64
trace_clock_read64(void)
147 struct lttng_trace_clock
*ltc
= READ_ONCE(lttng_trace_clock
);
150 return trace_clock_read64_monotonic();
152 read_barrier_depends(); /* load ltc before content */
153 return ltc
->read64();
157 static inline u64
trace_clock_freq(void)
159 struct lttng_trace_clock
*ltc
= READ_ONCE(lttng_trace_clock
);
162 return trace_clock_freq_monotonic();
164 read_barrier_depends(); /* load ltc before content */
169 static inline int trace_clock_uuid(char *uuid
)
171 struct lttng_trace_clock
*ltc
= READ_ONCE(lttng_trace_clock
);
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
);
178 return ltc
->uuid(uuid
);
182 static inline const char *trace_clock_name(void)
184 struct lttng_trace_clock
*ltc
= READ_ONCE(lttng_trace_clock
);
187 return trace_clock_name_monotonic();
189 read_barrier_depends(); /* load ltc before content */
194 static inline const char *trace_clock_description(void)
196 struct lttng_trace_clock
*ltc
= READ_ONCE(lttng_trace_clock
);
199 return trace_clock_description_monotonic();
201 read_barrier_depends(); /* load ltc before content */
202 return ltc
->description();
206 #endif /* CONFIG_HAVE_TRACE_CLOCK */
208 #endif /* _LTTNG_TRACE_CLOCK_H */
This page took 0.033346 seconds and 4 git commands to generate.