diff options
author | Peter Zijlstra <a.p.zijlstra@chello.nl> | 2008-08-25 11:15:34 -0400 |
---|---|---|
committer | Ingo Molnar <mingo@elte.hu> | 2008-08-25 11:39:57 -0400 |
commit | 354879bb977e06695993435745f06a0f6d39ce2b (patch) | |
tree | b811b4ec423fac8bdb671dafbf422aa4245bdcb3 /kernel/sched_clock.c | |
parent | efc2dead2c82cae31943828f6d977c483942b0eb (diff) |
sched_clock: fix cpu_clock()
This patch fixes 3 issues:
a) it removes the dependency on jiffies, because jiffies are incremented
by a single CPU, and the tick is not synchronized between CPUs. Therefore
relying on it to calculate a window to clip whacky TSC values doesn't work
as it can drift around.
So instead use [GTOD, GTOD+TICK_NSEC) as the window.
b) __update_sched_clock() did (roughly speaking):
delta = sched_clock() - scd->tick_raw;
clock += delta;
Which gives exponential growth, instead of linear.
c) allows the sched_clock_cpu() value to warp the u64 without breaking.
the results are more reliable sched_clock() deltas:
before after sched_clock
cpu_clock: 15750 51312 51488
cpu_clock: 59719 51052 50947
cpu_clock: 15879 51249 51061
cpu_clock: 1 50933 51198
cpu_clock: 1 50931 51039
cpu_clock: 1 51093 50981
cpu_clock: 1 51043 51040
cpu_clock: 1 50959 50938
cpu_clock: 1 50981 51011
cpu_clock: 1 51364 51212
cpu_clock: 1 51219 51273
cpu_clock: 1 51389 51048
cpu_clock: 1 51285 51611
cpu_clock: 1 50964 51137
cpu_clock: 1 50973 50968
cpu_clock: 1 50967 50972
cpu_clock: 1 58910 58485
cpu_clock: 1 51082 51025
cpu_clock: 1 50957 50958
cpu_clock: 1 50958 50957
cpu_clock: 1006128 51128 50971
cpu_clock: 1 51107 51155
cpu_clock: 1 51371 51081
cpu_clock: 1 51104 51365
cpu_clock: 1 51363 51309
cpu_clock: 1 51107 51160
cpu_clock: 1 51139 51100
cpu_clock: 1 51216 51136
cpu_clock: 1 51207 51215
cpu_clock: 1 51087 51263
cpu_clock: 1 51249 51177
cpu_clock: 1 51519 51412
cpu_clock: 1 51416 51255
cpu_clock: 1 51591 51594
cpu_clock: 1 50966 51374
cpu_clock: 1 50966 50966
cpu_clock: 1 51291 50948
cpu_clock: 1 50973 50867
cpu_clock: 1 50970 50970
cpu_clock: 998306 50970 50971
cpu_clock: 1 50971 50970
cpu_clock: 1 50970 50970
cpu_clock: 1 50971 50971
cpu_clock: 1 50970 50970
cpu_clock: 1 51351 50970
cpu_clock: 1 50970 51352
cpu_clock: 1 50971 50970
cpu_clock: 1 50970 50970
cpu_clock: 1 51321 50971
cpu_clock: 1 50974 51324
Signed-off-by: Peter Zijlstra <a.p.zijlstra@chello.nl>
Signed-off-by: Ingo Molnar <mingo@elte.hu>
Diffstat (limited to 'kernel/sched_clock.c')
-rw-r--r-- | kernel/sched_clock.c | 84 |
1 files changed, 34 insertions, 50 deletions
diff --git a/kernel/sched_clock.c b/kernel/sched_clock.c index 204991a0bfa7..e8ab096ddfe3 100644 --- a/kernel/sched_clock.c +++ b/kernel/sched_clock.c | |||
@@ -12,19 +12,17 @@ | |||
12 | * | 12 | * |
13 | * Create a semi stable clock from a mixture of other events, including: | 13 | * Create a semi stable clock from a mixture of other events, including: |
14 | * - gtod | 14 | * - gtod |
15 | * - jiffies | ||
16 | * - sched_clock() | 15 | * - sched_clock() |
17 | * - explicit idle events | 16 | * - explicit idle events |
18 | * | 17 | * |
19 | * We use gtod as base and the unstable clock deltas. The deltas are filtered, | 18 | * We use gtod as base and the unstable clock deltas. The deltas are filtered, |
20 | * making it monotonic and keeping it within an expected window. This window | 19 | * making it monotonic and keeping it within an expected window. |
21 | * is set up using jiffies. | ||
22 | * | 20 | * |
23 | * Furthermore, explicit sleep and wakeup hooks allow us to account for time | 21 | * Furthermore, explicit sleep and wakeup hooks allow us to account for time |
24 | * that is otherwise invisible (TSC gets stopped). | 22 | * that is otherwise invisible (TSC gets stopped). |
25 | * | 23 | * |
26 | * The clock: sched_clock_cpu() is monotonic per cpu, and should be somewhat | 24 | * The clock: sched_clock_cpu() is monotonic per cpu, and should be somewhat |
27 | * consistent between cpus (never more than 1 jiffies difference). | 25 | * consistent between cpus (never more than 2 jiffies difference). |
28 | */ | 26 | */ |
29 | #include <linux/sched.h> | 27 | #include <linux/sched.h> |
30 | #include <linux/percpu.h> | 28 | #include <linux/percpu.h> |
@@ -54,7 +52,6 @@ struct sched_clock_data { | |||
54 | */ | 52 | */ |
55 | raw_spinlock_t lock; | 53 | raw_spinlock_t lock; |
56 | 54 | ||
57 | unsigned long tick_jiffies; | ||
58 | u64 tick_raw; | 55 | u64 tick_raw; |
59 | u64 tick_gtod; | 56 | u64 tick_gtod; |
60 | u64 clock; | 57 | u64 clock; |
@@ -75,14 +72,12 @@ static inline struct sched_clock_data *cpu_sdc(int cpu) | |||
75 | void sched_clock_init(void) | 72 | void sched_clock_init(void) |
76 | { | 73 | { |
77 | u64 ktime_now = ktime_to_ns(ktime_get()); | 74 | u64 ktime_now = ktime_to_ns(ktime_get()); |
78 | unsigned long now_jiffies = jiffies; | ||
79 | int cpu; | 75 | int cpu; |
80 | 76 | ||
81 | for_each_possible_cpu(cpu) { | 77 | for_each_possible_cpu(cpu) { |
82 | struct sched_clock_data *scd = cpu_sdc(cpu); | 78 | struct sched_clock_data *scd = cpu_sdc(cpu); |
83 | 79 | ||
84 | scd->lock = (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED; | 80 | scd->lock = (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED; |
85 | scd->tick_jiffies = now_jiffies; | ||
86 | scd->tick_raw = 0; | 81 | scd->tick_raw = 0; |
87 | scd->tick_gtod = ktime_now; | 82 | scd->tick_gtod = ktime_now; |
88 | scd->clock = ktime_now; | 83 | scd->clock = ktime_now; |
@@ -92,46 +87,51 @@ void sched_clock_init(void) | |||
92 | } | 87 | } |
93 | 88 | ||
94 | /* | 89 | /* |
90 | * min,max except they take wrapping into account | ||
91 | */ | ||
92 | |||
93 | static inline u64 wrap_min(u64 x, u64 y) | ||
94 | { | ||
95 | return (s64)(x - y) < 0 ? x : y; | ||
96 | } | ||
97 | |||
98 | static inline u64 wrap_max(u64 x, u64 y) | ||
99 | { | ||
100 | return (s64)(x - y) > 0 ? x : y; | ||
101 | } | ||
102 | |||
103 | /* | ||
95 | * update the percpu scd from the raw @now value | 104 | * update the percpu scd from the raw @now value |
96 | * | 105 | * |
97 | * - filter out backward motion | 106 | * - filter out backward motion |
98 | * - use jiffies to generate a min,max window to clip the raw values | 107 | * - use the GTOD tick value to create a window to filter crazy TSC values |
99 | */ | 108 | */ |
100 | static u64 __update_sched_clock(struct sched_clock_data *scd, u64 now) | 109 | static u64 __update_sched_clock(struct sched_clock_data *scd, u64 now) |
101 | { | 110 | { |
102 | unsigned long now_jiffies = jiffies; | ||
103 | long delta_jiffies = now_jiffies - scd->tick_jiffies; | ||
104 | u64 clock = scd->clock; | ||
105 | u64 min_clock, max_clock; | ||
106 | s64 delta = now - scd->tick_raw; | 111 | s64 delta = now - scd->tick_raw; |
112 | u64 clock, min_clock, max_clock; | ||
107 | 113 | ||
108 | WARN_ON_ONCE(!irqs_disabled()); | 114 | WARN_ON_ONCE(!irqs_disabled()); |
109 | min_clock = scd->tick_gtod + delta_jiffies * TICK_NSEC; | ||
110 | 115 | ||
111 | if (unlikely(delta < 0)) { | 116 | if (unlikely(delta < 0)) |
112 | clock++; | 117 | delta = 0; |
113 | goto out; | ||
114 | } | ||
115 | 118 | ||
116 | max_clock = min_clock + TICK_NSEC; | 119 | /* |
120 | * scd->clock = clamp(scd->tick_gtod + delta, | ||
121 | * max(scd->tick_gtod, scd->clock), | ||
122 | * scd->tick_gtod + TICK_NSEC); | ||
123 | */ | ||
117 | 124 | ||
118 | if (unlikely(clock + delta > max_clock)) { | 125 | clock = scd->tick_gtod + delta; |
119 | if (clock < max_clock) | 126 | min_clock = wrap_max(scd->tick_gtod, scd->clock); |
120 | clock = max_clock; | 127 | max_clock = scd->tick_gtod + TICK_NSEC; |
121 | else | ||
122 | clock++; | ||
123 | } else { | ||
124 | clock += delta; | ||
125 | } | ||
126 | 128 | ||
127 | out: | 129 | clock = wrap_max(clock, min_clock); |
128 | if (unlikely(clock < min_clock)) | 130 | clock = wrap_min(clock, max_clock); |
129 | clock = min_clock; | ||
130 | 131 | ||
131 | scd->tick_jiffies = now_jiffies; | ||
132 | scd->clock = clock; | 132 | scd->clock = clock; |
133 | 133 | ||
134 | return clock; | 134 | return scd->clock; |
135 | } | 135 | } |
136 | 136 | ||
137 | static void lock_double_clock(struct sched_clock_data *data1, | 137 | static void lock_double_clock(struct sched_clock_data *data1, |
@@ -171,7 +171,7 @@ u64 sched_clock_cpu(int cpu) | |||
171 | * larger time as the latest time for both | 171 | * larger time as the latest time for both |
172 | * runqueues. (this creates monotonic movement) | 172 | * runqueues. (this creates monotonic movement) |
173 | */ | 173 | */ |
174 | if (likely(remote_clock < this_clock)) { | 174 | if (likely((s64)(remote_clock - this_clock) < 0)) { |
175 | clock = this_clock; | 175 | clock = this_clock; |
176 | scd->clock = clock; | 176 | scd->clock = clock; |
177 | } else { | 177 | } else { |
@@ -207,14 +207,9 @@ void sched_clock_tick(void) | |||
207 | now = sched_clock(); | 207 | now = sched_clock(); |
208 | 208 | ||
209 | __raw_spin_lock(&scd->lock); | 209 | __raw_spin_lock(&scd->lock); |
210 | __update_sched_clock(scd, now); | ||
211 | /* | ||
212 | * update tick_gtod after __update_sched_clock() because that will | ||
213 | * already observe 1 new jiffy; adding a new tick_gtod to that would | ||
214 | * increase the clock 2 jiffies. | ||
215 | */ | ||
216 | scd->tick_raw = now; | 210 | scd->tick_raw = now; |
217 | scd->tick_gtod = now_gtod; | 211 | scd->tick_gtod = now_gtod; |
212 | __update_sched_clock(scd, now); | ||
218 | __raw_spin_unlock(&scd->lock); | 213 | __raw_spin_unlock(&scd->lock); |
219 | } | 214 | } |
220 | 215 | ||
@@ -232,18 +227,7 @@ EXPORT_SYMBOL_GPL(sched_clock_idle_sleep_event); | |||
232 | */ | 227 | */ |
233 | void sched_clock_idle_wakeup_event(u64 delta_ns) | 228 | void sched_clock_idle_wakeup_event(u64 delta_ns) |
234 | { | 229 | { |
235 | struct sched_clock_data *scd = this_scd(); | 230 | sched_clock_tick(); |
236 | |||
237 | /* | ||
238 | * Override the previous timestamp and ignore all | ||
239 | * sched_clock() deltas that occured while we idled, | ||
240 | * and use the PM-provided delta_ns to advance the | ||
241 | * rq clock: | ||
242 | */ | ||
243 | __raw_spin_lock(&scd->lock); | ||
244 | scd->clock += delta_ns; | ||
245 | __raw_spin_unlock(&scd->lock); | ||
246 | |||
247 | touch_softlockup_watchdog(); | 231 | touch_softlockup_watchdog(); |
248 | } | 232 | } |
249 | EXPORT_SYMBOL_GPL(sched_clock_idle_wakeup_event); | 233 | EXPORT_SYMBOL_GPL(sched_clock_idle_wakeup_event); |