Commit | Line | Data |
---|---|---|
886d51a3 MD |
1 | #ifndef _LTTNG_TRACE_CLOCK_H |
2 | #define _LTTNG_TRACE_CLOCK_H | |
3 | ||
f6c19f6e | 4 | /* |
886d51a3 | 5 | * wrapper/trace-clock.h |
f6c19f6e MD |
6 | * |
7 | * Contains LTTng trace clock mapping to LTTng trace clock or mainline monotonic | |
8 | * clock. This wrapper depends on CONFIG_HIGH_RES_TIMERS=y. | |
9 | * | |
886d51a3 MD |
10 | * Copyright (C) 2011-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
11 | * | |
12 | * This library is free software; you can redistribute it and/or | |
13 | * modify it under the terms of the GNU Lesser General Public | |
14 | * License as published by the Free Software Foundation; only | |
15 | * version 2.1 of the License. | |
16 | * | |
17 | * This library is distributed in the hope that it will be useful, | |
18 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
20 | * Lesser General Public License for more details. | |
21 | * | |
22 | * You should have received a copy of the GNU Lesser General Public | |
23 | * License along with this library; if not, write to the Free Software | |
24 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | |
f6c19f6e MD |
25 | */ |
26 | ||
f6c19f6e MD |
27 | #ifdef CONFIG_HAVE_TRACE_CLOCK |
28 | #include <linux/trace-clock.h> | |
29 | #else /* CONFIG_HAVE_TRACE_CLOCK */ | |
30 | ||
31 | #include <linux/hardirq.h> | |
32 | #include <linux/ktime.h> | |
33 | #include <linux/time.h> | |
34 | #include <linux/hrtimer.h> | |
b0725207 | 35 | #include <linux/percpu.h> |
fc8216ae | 36 | #include <linux/version.h> |
b0725207 | 37 | #include <asm/local.h> |
9998f521 | 38 | #include "../lttng-kernel-version.h" |
18166235 | 39 | #include "percpu-defs.h" |
a82c63f1 | 40 | #include "random.h" |
f6c19f6e | 41 | |
e14bf964 | 42 | #if LTTNG_KERNEL_RANGE(3,10,0, 3,10,14) || LTTNG_KERNEL_RANGE(3,11,0, 3,11,3) |
9998f521 | 43 | #error "Linux kernels 3.10 and 3.11 introduce a deadlock in the timekeeping subsystem. Fixed by commit 7bd36014460f793c19e7d6c94dab67b0afcfcb7f \"timekeeping: Fix HRTICK related deadlock from ntp lock changes\" in Linux." |
fc8216ae MD |
44 | #endif |
45 | ||
b0725207 MD |
46 | #if (LINUX_VERSION_CODE >= KERNEL_VERSION(3,17,0)) |
47 | ||
48 | DECLARE_PER_CPU(local_t, lttng_last_tsc); | |
49 | ||
50 | #if (BITS_PER_LONG == 32) | |
51 | /* | |
52 | * Fixup "src_now" using the 32 LSB from "last". We need to handle overflow and | |
53 | * underflow of the 32nd bit. "last" can be above, below or equal to the 32 LSB | |
54 | * of "src_now". | |
55 | */ | |
56 | static inline u64 trace_clock_fixup(u64 src_now, u32 last) | |
57 | { | |
58 | u64 now; | |
59 | ||
60 | now = src_now & 0xFFFFFFFF00000000ULL; | |
61 | now |= (u64) last; | |
62 | /* Detect overflow or underflow between now and last. */ | |
63 | if ((src_now & 0x80000000U) && !(last & 0x80000000U)) { | |
64 | /* | |
65 | * If 32nd bit transitions from 1 to 0, and we move forward in | |
66 | * time from "now" to "last", then we have an overflow. | |
67 | */ | |
68 | if (((s32) now - (s32) last) < 0) | |
69 | now += 0x0000000100000000ULL; | |
70 | } else if (!(src_now & 0x80000000U) && (last & 0x80000000U)) { | |
71 | /* | |
72 | * If 32nd bit transitions from 0 to 1, and we move backward in | |
73 | * time from "now" to "last", then we have an underflow. | |
74 | */ | |
75 | if (((s32) now - (s32) last) > 0) | |
76 | now -= 0x0000000100000000ULL; | |
77 | } | |
78 | return now; | |
79 | } | |
80 | #else /* #if (BITS_PER_LONG == 32) */ | |
81 | /* | |
82 | * The fixup is pretty easy on 64-bit architectures: "last" is a 64-bit | |
83 | * value, so we can use last directly as current time. | |
84 | */ | |
85 | static inline u64 trace_clock_fixup(u64 src_now, u64 last) | |
86 | { | |
87 | return last; | |
88 | } | |
89 | #endif /* #else #if (BITS_PER_LONG == 32) */ | |
90 | ||
91 | /* | |
92 | * Always called with preemption disabled. Can be interrupted. | |
93 | */ | |
94 | static inline u64 trace_clock_monotonic_wrapper(void) | |
95 | { | |
96 | u64 now; | |
97 | unsigned long last, result; | |
98 | local_t *last_tsc; | |
99 | ||
100 | /* Use fast nmi-safe monotonic clock provided by the Linux kernel. */ | |
18166235 | 101 | last_tsc = lttng_this_cpu_ptr(<tng_last_tsc); |
b0725207 MD |
102 | last = local_read(last_tsc); |
103 | /* | |
104 | * Read "last" before "now". It is not strictly required, but it ensures | |
105 | * that an interrupt coming in won't artificially trigger a case where | |
106 | * "now" < "last". This kind of situation should only happen if the | |
107 | * mono_fast time source goes slightly backwards. | |
108 | */ | |
109 | barrier(); | |
110 | now = ktime_get_mono_fast_ns(); | |
111 | if (((long) now - (long) last) < 0) | |
112 | now = trace_clock_fixup(now, last); | |
113 | result = local_cmpxchg(last_tsc, last, (unsigned long) now); | |
114 | if (result == last) { | |
115 | /* Update done. */ | |
116 | return now; | |
117 | } else { | |
118 | /* | |
119 | * Update not done, due to concurrent update. We can use | |
120 | * "result", since it has been sampled concurrently with our | |
121 | * time read, so it should not be far from "now". | |
122 | */ | |
123 | return trace_clock_fixup(now, result); | |
124 | } | |
125 | } | |
126 | ||
127 | #else /* #if (LINUX_VERSION_CODE >= KERNEL_VERSION(3,17,0)) */ | |
f6c19f6e MD |
128 | static inline u64 trace_clock_monotonic_wrapper(void) |
129 | { | |
130 | ktime_t ktime; | |
131 | ||
132 | /* | |
133 | * Refuse to trace from NMIs with this wrapper, because an NMI could | |
134 | * nest over the xtime write seqlock and deadlock. | |
135 | */ | |
136 | if (in_nmi()) | |
97ca2c54 | 137 | return (u64) -EIO; |
f6c19f6e MD |
138 | |
139 | ktime = ktime_get(); | |
cfaf9f3d | 140 | return ktime_to_ns(ktime); |
f6c19f6e | 141 | } |
b0725207 | 142 | #endif /* #else #if (LINUX_VERSION_CODE >= KERNEL_VERSION(3,17,0)) */ |
f6c19f6e | 143 | |
f6c19f6e MD |
144 | static inline u64 trace_clock_read64(void) |
145 | { | |
146 | return (u64) trace_clock_monotonic_wrapper(); | |
147 | } | |
148 | ||
a3ccff4f | 149 | static inline u64 trace_clock_freq(void) |
f6c19f6e | 150 | { |
a3ccff4f | 151 | return (u64) NSEC_PER_SEC; |
f6c19f6e MD |
152 | } |
153 | ||
a82c63f1 | 154 | static inline int trace_clock_uuid(char *uuid) |
f6c19f6e | 155 | { |
a82c63f1 | 156 | return wrapper_get_bootid(uuid); |
f6c19f6e MD |
157 | } |
158 | ||
b0725207 | 159 | #if (LINUX_VERSION_CODE >= KERNEL_VERSION(3,17,0)) |
f6c19f6e MD |
160 | static inline int get_trace_clock(void) |
161 | { | |
b0725207 MD |
162 | printk(KERN_WARNING "LTTng: Using mainline kernel monotonic fast clock, which is NMI-safe.\n"); |
163 | return 0; | |
164 | } | |
165 | #else /* #if (LINUX_VERSION_CODE >= KERNEL_VERSION(3,17,0)) */ | |
166 | static inline int get_trace_clock(void) | |
167 | { | |
168 | printk(KERN_WARNING "LTTng: Using mainline kernel monotonic clock. NMIs will not be traced.\n"); | |
f6c19f6e MD |
169 | return 0; |
170 | } | |
b0725207 | 171 | #endif /* #else #if (LINUX_VERSION_CODE >= KERNEL_VERSION(3,17,0)) */ |
f6c19f6e MD |
172 | |
173 | static inline void put_trace_clock(void) | |
174 | { | |
175 | } | |
176 | ||
177 | #endif /* CONFIG_HAVE_TRACE_CLOCK */ | |
178 | ||
a90917c3 | 179 | #endif /* _LTTNG_TRACE_CLOCK_H */ |