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/lttng-kernel-version.h>
27 #include <lttng/lttng-clock.h>
28 #include <linux/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 unsigned char *boot_id
;
116 boot_id
= get_kernel_boot_id();
117 sprintf(uuid
, "%pU", boot_id
);
121 static inline const char *trace_clock_name_monotonic(void)
126 static inline const char *trace_clock_description_monotonic(void)
128 return "Monotonic Clock";
131 #ifdef LTTNG_USE_NMI_SAFE_CLOCK
132 static inline int get_trace_clock(void)
134 printk_once(KERN_WARNING
"LTTng: Using mainline kernel monotonic fast clock, which is NMI-safe.\n");
137 #else /* #ifdef LTTNG_USE_NMI_SAFE_CLOCK */
138 static inline int get_trace_clock(void)
140 printk_once(KERN_WARNING
"LTTng: Using mainline kernel monotonic clock. NMIs will not be traced.\n");
143 #endif /* #else #ifdef LTTNG_USE_NMI_SAFE_CLOCK */
145 static inline void put_trace_clock(void)
149 static inline u64
trace_clock_read64(void)
151 struct lttng_trace_clock
*ltc
= READ_ONCE(lttng_trace_clock
);
154 return trace_clock_read64_monotonic();
156 read_barrier_depends(); /* load ltc before content */
157 return ltc
->read64();
161 static inline u64
trace_clock_freq(void)
163 struct lttng_trace_clock
*ltc
= READ_ONCE(lttng_trace_clock
);
166 return trace_clock_freq_monotonic();
168 read_barrier_depends(); /* load ltc before content */
173 static inline int trace_clock_uuid(char *uuid
)
175 struct lttng_trace_clock
*ltc
= READ_ONCE(lttng_trace_clock
);
177 read_barrier_depends(); /* load ltc before content */
178 /* Use default UUID cb when NULL */
179 if (!ltc
|| !ltc
->uuid
) {
180 return trace_clock_uuid_monotonic(uuid
);
182 return ltc
->uuid(uuid
);
186 static inline const char *trace_clock_name(void)
188 struct lttng_trace_clock
*ltc
= READ_ONCE(lttng_trace_clock
);
191 return trace_clock_name_monotonic();
193 read_barrier_depends(); /* load ltc before content */
198 static inline const char *trace_clock_description(void)
200 struct lttng_trace_clock
*ltc
= READ_ONCE(lttng_trace_clock
);
203 return trace_clock_description_monotonic();
205 read_barrier_depends(); /* load ltc before content */
206 return ltc
->description();
210 #endif /* CONFIG_HAVE_TRACE_CLOCK */
212 #endif /* _LTTNG_TRACE_CLOCK_H */
This page took 0.033946 seconds and 4 git commands to generate.