]>
Commit | Line | Data |
---|---|---|
3e51f33f PZ |
1 | /* |
2 | * sched_clock for unstable cpu clocks | |
3 | * | |
4 | * Copyright (C) 2008 Red Hat, Inc., Peter Zijlstra <pzijlstr@redhat.com> | |
5 | * | |
c300ba25 SR |
6 | * Updates and enhancements: |
7 | * Copyright (C) 2008 Red Hat, Inc. Steven Rostedt <srostedt@redhat.com> | |
8 | * | |
3e51f33f PZ |
9 | * Based on code by: |
10 | * Ingo Molnar <mingo@redhat.com> | |
11 | * Guillaume Chazarain <guichaz@gmail.com> | |
12 | * | |
c676329a PZ |
13 | * |
14 | * What: | |
15 | * | |
16 | * cpu_clock(i) provides a fast (execution time) high resolution | |
17 | * clock with bounded drift between CPUs. The value of cpu_clock(i) | |
18 | * is monotonic for constant i. The timestamp returned is in nanoseconds. | |
19 | * | |
20 | * ######################### BIG FAT WARNING ########################## | |
21 | * # when comparing cpu_clock(i) to cpu_clock(j) for i != j, time can # | |
22 | * # go backwards !! # | |
23 | * #################################################################### | |
24 | * | |
25 | * There is no strict promise about the base, although it tends to start | |
26 | * at 0 on boot (but people really shouldn't rely on that). | |
27 | * | |
28 | * cpu_clock(i) -- can be used from any context, including NMI. | |
c676329a PZ |
29 | * local_clock() -- is cpu_clock() on the current cpu. |
30 | * | |
ef08f0ff PZ |
31 | * sched_clock_cpu(i) |
32 | * | |
c676329a PZ |
33 | * How: |
34 | * | |
35 | * The implementation either uses sched_clock() when | |
36 | * !CONFIG_HAVE_UNSTABLE_SCHED_CLOCK, which means in that case the | |
37 | * sched_clock() is assumed to provide these properties (mostly it means | |
38 | * the architecture provides a globally synchronized highres time source). | |
39 | * | |
40 | * Otherwise it tries to create a semi stable clock from a mixture of other | |
41 | * clocks, including: | |
42 | * | |
43 | * - GTOD (clock monotomic) | |
3e51f33f PZ |
44 | * - sched_clock() |
45 | * - explicit idle events | |
46 | * | |
c676329a PZ |
47 | * We use GTOD as base and use sched_clock() deltas to improve resolution. The |
48 | * deltas are filtered to provide monotonicity and keeping it within an | |
49 | * expected window. | |
3e51f33f PZ |
50 | * |
51 | * Furthermore, explicit sleep and wakeup hooks allow us to account for time | |
52 | * that is otherwise invisible (TSC gets stopped). | |
53 | * | |
3e51f33f | 54 | */ |
3e51f33f | 55 | #include <linux/spinlock.h> |
6409c4da | 56 | #include <linux/hardirq.h> |
9984de1a | 57 | #include <linux/export.h> |
b342501c IM |
58 | #include <linux/percpu.h> |
59 | #include <linux/ktime.h> | |
60 | #include <linux/sched.h> | |
35af99e6 | 61 | #include <linux/static_key.h> |
3e51f33f | 62 | |
2c3d103b HD |
63 | /* |
64 | * Scheduler clock - returns current time in nanosec units. | |
65 | * This is default implementation. | |
66 | * Architectures and sub-architectures can override this. | |
67 | */ | |
68 | unsigned long long __attribute__((weak)) sched_clock(void) | |
69 | { | |
92d23f70 R |
70 | return (unsigned long long)(jiffies - INITIAL_JIFFIES) |
71 | * (NSEC_PER_SEC / HZ); | |
2c3d103b | 72 | } |
b6ac23af | 73 | EXPORT_SYMBOL_GPL(sched_clock); |
3e51f33f | 74 | |
5bb6b1ea | 75 | __read_mostly int sched_clock_running; |
c1955a3d | 76 | |
3e51f33f | 77 | #ifdef CONFIG_HAVE_UNSTABLE_SCHED_CLOCK |
35af99e6 PZ |
78 | static struct static_key __sched_clock_stable = STATIC_KEY_INIT; |
79 | ||
80 | int sched_clock_stable(void) | |
81 | { | |
82 | if (static_key_false(&__sched_clock_stable)) | |
83 | return false; | |
84 | return true; | |
85 | } | |
86 | ||
87 | void set_sched_clock_stable(void) | |
88 | { | |
89 | if (!sched_clock_stable()) | |
90 | static_key_slow_dec(&__sched_clock_stable); | |
91 | } | |
92 | ||
93 | void clear_sched_clock_stable(void) | |
94 | { | |
95 | /* XXX worry about clock continuity */ | |
96 | if (sched_clock_stable()) | |
97 | static_key_slow_inc(&__sched_clock_stable); | |
98 | } | |
3e51f33f PZ |
99 | |
100 | struct sched_clock_data { | |
3e51f33f PZ |
101 | u64 tick_raw; |
102 | u64 tick_gtod; | |
103 | u64 clock; | |
104 | }; | |
105 | ||
106 | static DEFINE_PER_CPU_SHARED_ALIGNED(struct sched_clock_data, sched_clock_data); | |
107 | ||
108 | static inline struct sched_clock_data *this_scd(void) | |
109 | { | |
110 | return &__get_cpu_var(sched_clock_data); | |
111 | } | |
112 | ||
113 | static inline struct sched_clock_data *cpu_sdc(int cpu) | |
114 | { | |
115 | return &per_cpu(sched_clock_data, cpu); | |
116 | } | |
117 | ||
118 | void sched_clock_init(void) | |
119 | { | |
120 | u64 ktime_now = ktime_to_ns(ktime_get()); | |
3e51f33f PZ |
121 | int cpu; |
122 | ||
123 | for_each_possible_cpu(cpu) { | |
124 | struct sched_clock_data *scd = cpu_sdc(cpu); | |
125 | ||
a381759d | 126 | scd->tick_raw = 0; |
3e51f33f PZ |
127 | scd->tick_gtod = ktime_now; |
128 | scd->clock = ktime_now; | |
129 | } | |
a381759d PZ |
130 | |
131 | sched_clock_running = 1; | |
3e51f33f PZ |
132 | } |
133 | ||
354879bb | 134 | /* |
b342501c | 135 | * min, max except they take wrapping into account |
354879bb PZ |
136 | */ |
137 | ||
138 | static inline u64 wrap_min(u64 x, u64 y) | |
139 | { | |
140 | return (s64)(x - y) < 0 ? x : y; | |
141 | } | |
142 | ||
143 | static inline u64 wrap_max(u64 x, u64 y) | |
144 | { | |
145 | return (s64)(x - y) > 0 ? x : y; | |
146 | } | |
147 | ||
3e51f33f PZ |
148 | /* |
149 | * update the percpu scd from the raw @now value | |
150 | * | |
151 | * - filter out backward motion | |
354879bb | 152 | * - use the GTOD tick value to create a window to filter crazy TSC values |
3e51f33f | 153 | */ |
def0a9b2 | 154 | static u64 sched_clock_local(struct sched_clock_data *scd) |
3e51f33f | 155 | { |
def0a9b2 PZ |
156 | u64 now, clock, old_clock, min_clock, max_clock; |
157 | s64 delta; | |
3e51f33f | 158 | |
def0a9b2 PZ |
159 | again: |
160 | now = sched_clock(); | |
161 | delta = now - scd->tick_raw; | |
354879bb PZ |
162 | if (unlikely(delta < 0)) |
163 | delta = 0; | |
3e51f33f | 164 | |
def0a9b2 PZ |
165 | old_clock = scd->clock; |
166 | ||
354879bb PZ |
167 | /* |
168 | * scd->clock = clamp(scd->tick_gtod + delta, | |
b342501c IM |
169 | * max(scd->tick_gtod, scd->clock), |
170 | * scd->tick_gtod + TICK_NSEC); | |
354879bb | 171 | */ |
3e51f33f | 172 | |
354879bb | 173 | clock = scd->tick_gtod + delta; |
def0a9b2 PZ |
174 | min_clock = wrap_max(scd->tick_gtod, old_clock); |
175 | max_clock = wrap_max(old_clock, scd->tick_gtod + TICK_NSEC); | |
3e51f33f | 176 | |
354879bb PZ |
177 | clock = wrap_max(clock, min_clock); |
178 | clock = wrap_min(clock, max_clock); | |
3e51f33f | 179 | |
152f9d07 | 180 | if (cmpxchg64(&scd->clock, old_clock, clock) != old_clock) |
def0a9b2 | 181 | goto again; |
56b90612 | 182 | |
def0a9b2 | 183 | return clock; |
3e51f33f PZ |
184 | } |
185 | ||
def0a9b2 | 186 | static u64 sched_clock_remote(struct sched_clock_data *scd) |
3e51f33f | 187 | { |
def0a9b2 PZ |
188 | struct sched_clock_data *my_scd = this_scd(); |
189 | u64 this_clock, remote_clock; | |
190 | u64 *ptr, old_val, val; | |
191 | ||
a1cbcaa9 TG |
192 | #if BITS_PER_LONG != 64 |
193 | again: | |
194 | /* | |
195 | * Careful here: The local and the remote clock values need to | |
196 | * be read out atomic as we need to compare the values and | |
197 | * then update either the local or the remote side. So the | |
198 | * cmpxchg64 below only protects one readout. | |
199 | * | |
200 | * We must reread via sched_clock_local() in the retry case on | |
201 | * 32bit as an NMI could use sched_clock_local() via the | |
202 | * tracer and hit between the readout of | |
203 | * the low32bit and the high 32bit portion. | |
204 | */ | |
205 | this_clock = sched_clock_local(my_scd); | |
206 | /* | |
207 | * We must enforce atomic readout on 32bit, otherwise the | |
208 | * update on the remote cpu can hit inbetween the readout of | |
209 | * the low32bit and the high 32bit portion. | |
210 | */ | |
211 | remote_clock = cmpxchg64(&scd->clock, 0, 0); | |
212 | #else | |
213 | /* | |
214 | * On 64bit the read of [my]scd->clock is atomic versus the | |
215 | * update, so we can avoid the above 32bit dance. | |
216 | */ | |
def0a9b2 PZ |
217 | sched_clock_local(my_scd); |
218 | again: | |
219 | this_clock = my_scd->clock; | |
220 | remote_clock = scd->clock; | |
a1cbcaa9 | 221 | #endif |
def0a9b2 PZ |
222 | |
223 | /* | |
224 | * Use the opportunity that we have both locks | |
225 | * taken to couple the two clocks: we take the | |
226 | * larger time as the latest time for both | |
227 | * runqueues. (this creates monotonic movement) | |
228 | */ | |
229 | if (likely((s64)(remote_clock - this_clock) < 0)) { | |
230 | ptr = &scd->clock; | |
231 | old_val = remote_clock; | |
232 | val = this_clock; | |
3e51f33f | 233 | } else { |
def0a9b2 PZ |
234 | /* |
235 | * Should be rare, but possible: | |
236 | */ | |
237 | ptr = &my_scd->clock; | |
238 | old_val = this_clock; | |
239 | val = remote_clock; | |
3e51f33f | 240 | } |
def0a9b2 | 241 | |
152f9d07 | 242 | if (cmpxchg64(ptr, old_val, val) != old_val) |
def0a9b2 PZ |
243 | goto again; |
244 | ||
245 | return val; | |
3e51f33f PZ |
246 | } |
247 | ||
c676329a PZ |
248 | /* |
249 | * Similar to cpu_clock(), but requires local IRQs to be disabled. | |
250 | * | |
251 | * See cpu_clock(). | |
252 | */ | |
3e51f33f PZ |
253 | u64 sched_clock_cpu(int cpu) |
254 | { | |
b342501c | 255 | struct sched_clock_data *scd; |
def0a9b2 PZ |
256 | u64 clock; |
257 | ||
35af99e6 | 258 | if (sched_clock_stable()) |
b342501c | 259 | return sched_clock(); |
a381759d | 260 | |
a381759d PZ |
261 | if (unlikely(!sched_clock_running)) |
262 | return 0ull; | |
263 | ||
ef08f0ff | 264 | preempt_disable(); |
def0a9b2 | 265 | scd = cpu_sdc(cpu); |
3e51f33f | 266 | |
def0a9b2 PZ |
267 | if (cpu != smp_processor_id()) |
268 | clock = sched_clock_remote(scd); | |
269 | else | |
270 | clock = sched_clock_local(scd); | |
ef08f0ff | 271 | preempt_enable(); |
e4e4e534 | 272 | |
3e51f33f PZ |
273 | return clock; |
274 | } | |
275 | ||
276 | void sched_clock_tick(void) | |
277 | { | |
8325d9c0 | 278 | struct sched_clock_data *scd; |
3e51f33f PZ |
279 | u64 now, now_gtod; |
280 | ||
35af99e6 | 281 | if (sched_clock_stable()) |
8325d9c0 PZ |
282 | return; |
283 | ||
a381759d PZ |
284 | if (unlikely(!sched_clock_running)) |
285 | return; | |
286 | ||
3e51f33f PZ |
287 | WARN_ON_ONCE(!irqs_disabled()); |
288 | ||
8325d9c0 | 289 | scd = this_scd(); |
3e51f33f | 290 | now_gtod = ktime_to_ns(ktime_get()); |
a83bc47c | 291 | now = sched_clock(); |
3e51f33f | 292 | |
3e51f33f PZ |
293 | scd->tick_raw = now; |
294 | scd->tick_gtod = now_gtod; | |
def0a9b2 | 295 | sched_clock_local(scd); |
3e51f33f PZ |
296 | } |
297 | ||
298 | /* | |
299 | * We are going deep-idle (irqs are disabled): | |
300 | */ | |
301 | void sched_clock_idle_sleep_event(void) | |
302 | { | |
303 | sched_clock_cpu(smp_processor_id()); | |
304 | } | |
305 | EXPORT_SYMBOL_GPL(sched_clock_idle_sleep_event); | |
306 | ||
307 | /* | |
308 | * We just idled delta nanoseconds (called with irqs disabled): | |
309 | */ | |
310 | void sched_clock_idle_wakeup_event(u64 delta_ns) | |
311 | { | |
1c5745aa TG |
312 | if (timekeeping_suspended) |
313 | return; | |
314 | ||
354879bb | 315 | sched_clock_tick(); |
3e51f33f PZ |
316 | touch_softlockup_watchdog(); |
317 | } | |
318 | EXPORT_SYMBOL_GPL(sched_clock_idle_wakeup_event); | |
319 | ||
c676329a PZ |
320 | /* |
321 | * As outlined at the top, provides a fast, high resolution, nanosecond | |
322 | * time source that is monotonic per cpu argument and has bounded drift | |
323 | * between cpus. | |
324 | * | |
325 | * ######################### BIG FAT WARNING ########################## | |
326 | * # when comparing cpu_clock(i) to cpu_clock(j) for i != j, time can # | |
327 | * # go backwards !! # | |
328 | * #################################################################### | |
329 | */ | |
330 | u64 cpu_clock(int cpu) | |
b9f8fcd5 | 331 | { |
35af99e6 PZ |
332 | if (static_key_false(&__sched_clock_stable)) |
333 | return sched_clock_cpu(cpu); | |
334 | ||
335 | return sched_clock(); | |
b9f8fcd5 DM |
336 | } |
337 | ||
c676329a PZ |
338 | /* |
339 | * Similar to cpu_clock() for the current cpu. Time will only be observed | |
340 | * to be monotonic if care is taken to only compare timestampt taken on the | |
341 | * same CPU. | |
342 | * | |
343 | * See cpu_clock(). | |
344 | */ | |
345 | u64 local_clock(void) | |
346 | { | |
35af99e6 PZ |
347 | if (static_key_false(&__sched_clock_stable)) |
348 | return sched_clock_cpu(raw_smp_processor_id()); | |
349 | ||
350 | return sched_clock(); | |
c676329a PZ |
351 | } |
352 | ||
8325d9c0 PZ |
353 | #else /* CONFIG_HAVE_UNSTABLE_SCHED_CLOCK */ |
354 | ||
355 | void sched_clock_init(void) | |
356 | { | |
357 | sched_clock_running = 1; | |
358 | } | |
359 | ||
360 | u64 sched_clock_cpu(int cpu) | |
361 | { | |
362 | if (unlikely(!sched_clock_running)) | |
363 | return 0; | |
364 | ||
365 | return sched_clock(); | |
366 | } | |
367 | ||
c676329a | 368 | u64 cpu_clock(int cpu) |
76a2a6ee | 369 | { |
35af99e6 | 370 | return sched_clock(); |
b9f8fcd5 | 371 | } |
76a2a6ee | 372 | |
c676329a PZ |
373 | u64 local_clock(void) |
374 | { | |
35af99e6 | 375 | return sched_clock(); |
c676329a PZ |
376 | } |
377 | ||
b9f8fcd5 | 378 | #endif /* CONFIG_HAVE_UNSTABLE_SCHED_CLOCK */ |
76a2a6ee | 379 | |
4c9fe8ad | 380 | EXPORT_SYMBOL_GPL(cpu_clock); |
c676329a | 381 | EXPORT_SYMBOL_GPL(local_clock); |