Update README.md for supported kernel
[lttng-modules.git] / wrapper / trace-clock.h
... / ...
CommitLineData
1/* SPDX-License-Identifier: (GPL-2.0-only or LGPL-2.1-only)
2 *
3 * wrapper/trace-clock.h
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 *
8 * Copyright (C) 2011-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
9 */
10
11#ifndef _LTTNG_TRACE_CLOCK_H
12#define _LTTNG_TRACE_CLOCK_H
13
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>
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 <linux/random.h>
29
30extern struct lttng_trace_clock *lttng_trace_clock;
31
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
40#if BITS_PER_LONG == 64
41#define LTTNG_USE_NMI_SAFE_CLOCK
42#endif
43
44#ifdef LTTNG_USE_NMI_SAFE_CLOCK
45
46DECLARE_PER_CPU(u64, lttng_last_tsc);
47
48/*
49 * Sometimes called with preemption enabled. Can be interrupted.
50 */
51static inline u64 trace_clock_monotonic_wrapper(void)
52{
53 u64 now, last, result;
54 u64 *last_tsc_ptr;
55
56 /* Use fast nmi-safe monotonic clock provided by the Linux kernel. */
57 preempt_disable();
58 last_tsc_ptr = this_cpu_ptr(&lttng_last_tsc);
59 last = *last_tsc_ptr;
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();
68 if (U64_MAX / 2 < now - last)
69 now = last;
70 result = cmpxchg64_local(last_tsc_ptr, last, now);
71 preempt_enable();
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 */
81 return result;
82 }
83}
84
85#else /* #ifdef LTTNG_USE_NMI_SAFE_CLOCK */
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())
95 return (u64) -EIO;
96
97 ktime = ktime_get();
98 return ktime_to_ns(ktime);
99}
100#endif /* #else #ifdef LTTNG_USE_NMI_SAFE_CLOCK */
101
102static inline u64 trace_clock_read64_monotonic(void)
103{
104 return (u64) trace_clock_monotonic_wrapper();
105}
106
107static inline u64 trace_clock_freq_monotonic(void)
108{
109 return (u64) NSEC_PER_SEC;
110}
111
112static inline int trace_clock_uuid_monotonic(char *uuid)
113{
114 unsigned char *boot_id;
115
116 boot_id = get_kernel_boot_id();
117 sprintf(uuid, "%pU", boot_id);
118 return 0;
119}
120
121static inline const char *trace_clock_name_monotonic(void)
122{
123 return "monotonic";
124}
125
126static inline const char *trace_clock_description_monotonic(void)
127{
128 return "Monotonic Clock";
129}
130
131#ifdef LTTNG_USE_NMI_SAFE_CLOCK
132static inline int get_trace_clock(void)
133{
134 printk_once(KERN_WARNING "LTTng: Using mainline kernel monotonic fast clock, which is NMI-safe.\n");
135 return 0;
136}
137#else /* #ifdef LTTNG_USE_NMI_SAFE_CLOCK */
138static inline int get_trace_clock(void)
139{
140 printk_once(KERN_WARNING "LTTng: Using mainline kernel monotonic clock. NMIs will not be traced.\n");
141 return 0;
142}
143#endif /* #else #ifdef LTTNG_USE_NMI_SAFE_CLOCK */
144
145static inline void put_trace_clock(void)
146{
147}
148
149static inline u64 trace_clock_read64(void)
150{
151 struct lttng_trace_clock *ltc = READ_ONCE(lttng_trace_clock);
152
153 if (likely(!ltc)) {
154 return trace_clock_read64_monotonic();
155 } else {
156 read_barrier_depends(); /* load ltc before content */
157 return ltc->read64();
158 }
159}
160
161static inline u64 trace_clock_freq(void)
162{
163 struct lttng_trace_clock *ltc = READ_ONCE(lttng_trace_clock);
164
165 if (!ltc) {
166 return trace_clock_freq_monotonic();
167 } else {
168 read_barrier_depends(); /* load ltc before content */
169 return ltc->freq();
170 }
171}
172
173static inline int trace_clock_uuid(char *uuid)
174{
175 struct lttng_trace_clock *ltc = READ_ONCE(lttng_trace_clock);
176
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);
181 } else {
182 return ltc->uuid(uuid);
183 }
184}
185
186static inline const char *trace_clock_name(void)
187{
188 struct lttng_trace_clock *ltc = READ_ONCE(lttng_trace_clock);
189
190 if (!ltc) {
191 return trace_clock_name_monotonic();
192 } else {
193 read_barrier_depends(); /* load ltc before content */
194 return ltc->name();
195 }
196}
197
198static inline const char *trace_clock_description(void)
199{
200 struct lttng_trace_clock *ltc = READ_ONCE(lttng_trace_clock);
201
202 if (!ltc) {
203 return trace_clock_description_monotonic();
204 } else {
205 read_barrier_depends(); /* load ltc before content */
206 return ltc->description();
207 }
208}
209
210#endif /* CONFIG_HAVE_TRACE_CLOCK */
211
212#endif /* _LTTNG_TRACE_CLOCK_H */
This page took 0.023147 seconds and 4 git commands to generate.