diff options
Diffstat (limited to 'kernel/sched_clock.c')
-rw-r--r-- | kernel/sched_clock.c | 137 |
1 files changed, 118 insertions, 19 deletions
diff --git a/kernel/sched_clock.c b/kernel/sched_clock.c index ce05271219ab..22ed55d1167f 100644 --- a/kernel/sched_clock.c +++ b/kernel/sched_clock.c | |||
@@ -3,6 +3,9 @@ | |||
3 | * | 3 | * |
4 | * Copyright (C) 2008 Red Hat, Inc., Peter Zijlstra <pzijlstr@redhat.com> | 4 | * Copyright (C) 2008 Red Hat, Inc., Peter Zijlstra <pzijlstr@redhat.com> |
5 | * | 5 | * |
6 | * Updates and enhancements: | ||
7 | * Copyright (C) 2008 Red Hat, Inc. Steven Rostedt <srostedt@redhat.com> | ||
8 | * | ||
6 | * Based on code by: | 9 | * Based on code by: |
7 | * Ingo Molnar <mingo@redhat.com> | 10 | * Ingo Molnar <mingo@redhat.com> |
8 | * Guillaume Chazarain <guichaz@gmail.com> | 11 | * Guillaume Chazarain <guichaz@gmail.com> |
@@ -32,6 +35,11 @@ | |||
32 | 35 | ||
33 | #ifdef CONFIG_HAVE_UNSTABLE_SCHED_CLOCK | 36 | #ifdef CONFIG_HAVE_UNSTABLE_SCHED_CLOCK |
34 | 37 | ||
38 | #define MULTI_SHIFT 15 | ||
39 | /* Max is double, Min is 1/2 */ | ||
40 | #define MAX_MULTI (2LL << MULTI_SHIFT) | ||
41 | #define MIN_MULTI (1LL << (MULTI_SHIFT-1)) | ||
42 | |||
35 | struct sched_clock_data { | 43 | struct sched_clock_data { |
36 | /* | 44 | /* |
37 | * Raw spinlock - this is a special case: this might be called | 45 | * Raw spinlock - this is a special case: this might be called |
@@ -40,11 +48,15 @@ struct sched_clock_data { | |||
40 | */ | 48 | */ |
41 | raw_spinlock_t lock; | 49 | raw_spinlock_t lock; |
42 | 50 | ||
43 | unsigned long prev_jiffies; | 51 | unsigned long tick_jiffies; |
44 | u64 prev_raw; | 52 | u64 prev_raw; |
45 | u64 tick_raw; | 53 | u64 tick_raw; |
46 | u64 tick_gtod; | 54 | u64 tick_gtod; |
47 | u64 clock; | 55 | u64 clock; |
56 | s64 multi; | ||
57 | #ifdef CONFIG_NO_HZ | ||
58 | int check_max; | ||
59 | #endif | ||
48 | }; | 60 | }; |
49 | 61 | ||
50 | static DEFINE_PER_CPU_SHARED_ALIGNED(struct sched_clock_data, sched_clock_data); | 62 | static DEFINE_PER_CPU_SHARED_ALIGNED(struct sched_clock_data, sched_clock_data); |
@@ -71,41 +83,91 @@ void sched_clock_init(void) | |||
71 | struct sched_clock_data *scd = cpu_sdc(cpu); | 83 | struct sched_clock_data *scd = cpu_sdc(cpu); |
72 | 84 | ||
73 | scd->lock = (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED; | 85 | scd->lock = (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED; |
74 | scd->prev_jiffies = now_jiffies; | 86 | scd->tick_jiffies = now_jiffies; |
75 | scd->prev_raw = 0; | 87 | scd->prev_raw = 0; |
76 | scd->tick_raw = 0; | 88 | scd->tick_raw = 0; |
77 | scd->tick_gtod = ktime_now; | 89 | scd->tick_gtod = ktime_now; |
78 | scd->clock = ktime_now; | 90 | scd->clock = ktime_now; |
91 | scd->multi = 1 << MULTI_SHIFT; | ||
92 | #ifdef CONFIG_NO_HZ | ||
93 | scd->check_max = 1; | ||
94 | #endif | ||
79 | } | 95 | } |
80 | 96 | ||
81 | sched_clock_running = 1; | 97 | sched_clock_running = 1; |
82 | } | 98 | } |
83 | 99 | ||
100 | #ifdef CONFIG_NO_HZ | ||
101 | /* | ||
102 | * The dynamic ticks makes the delta jiffies inaccurate. This | ||
103 | * prevents us from checking the maximum time update. | ||
104 | * Disable the maximum check during stopped ticks. | ||
105 | */ | ||
106 | void sched_clock_tick_stop(int cpu) | ||
107 | { | ||
108 | struct sched_clock_data *scd = cpu_sdc(cpu); | ||
109 | |||
110 | scd->check_max = 0; | ||
111 | } | ||
112 | |||
113 | void sched_clock_tick_start(int cpu) | ||
114 | { | ||
115 | struct sched_clock_data *scd = cpu_sdc(cpu); | ||
116 | |||
117 | scd->check_max = 1; | ||
118 | } | ||
119 | |||
120 | static int check_max(struct sched_clock_data *scd) | ||
121 | { | ||
122 | return scd->check_max; | ||
123 | } | ||
124 | #else | ||
125 | static int check_max(struct sched_clock_data *scd) | ||
126 | { | ||
127 | return 1; | ||
128 | } | ||
129 | #endif /* CONFIG_NO_HZ */ | ||
130 | |||
84 | /* | 131 | /* |
85 | * update the percpu scd from the raw @now value | 132 | * update the percpu scd from the raw @now value |
86 | * | 133 | * |
87 | * - filter out backward motion | 134 | * - filter out backward motion |
88 | * - use jiffies to generate a min,max window to clip the raw values | 135 | * - use jiffies to generate a min,max window to clip the raw values |
89 | */ | 136 | */ |
90 | static void __update_sched_clock(struct sched_clock_data *scd, u64 now) | 137 | static void __update_sched_clock(struct sched_clock_data *scd, u64 now, u64 *time) |
91 | { | 138 | { |
92 | unsigned long now_jiffies = jiffies; | 139 | unsigned long now_jiffies = jiffies; |
93 | long delta_jiffies = now_jiffies - scd->prev_jiffies; | 140 | long delta_jiffies = now_jiffies - scd->tick_jiffies; |
94 | u64 clock = scd->clock; | 141 | u64 clock = scd->clock; |
95 | u64 min_clock, max_clock; | 142 | u64 min_clock, max_clock; |
96 | s64 delta = now - scd->prev_raw; | 143 | s64 delta = now - scd->prev_raw; |
97 | 144 | ||
98 | WARN_ON_ONCE(!irqs_disabled()); | 145 | WARN_ON_ONCE(!irqs_disabled()); |
99 | min_clock = scd->tick_gtod + delta_jiffies * TICK_NSEC; | 146 | |
147 | /* | ||
148 | * At schedule tick the clock can be just under the gtod. We don't | ||
149 | * want to push it too prematurely. | ||
150 | */ | ||
151 | min_clock = scd->tick_gtod + (delta_jiffies * TICK_NSEC); | ||
152 | if (min_clock > TICK_NSEC) | ||
153 | min_clock -= TICK_NSEC / 2; | ||
100 | 154 | ||
101 | if (unlikely(delta < 0)) { | 155 | if (unlikely(delta < 0)) { |
102 | clock++; | 156 | clock++; |
103 | goto out; | 157 | goto out; |
104 | } | 158 | } |
105 | 159 | ||
106 | max_clock = min_clock + TICK_NSEC; | 160 | /* |
161 | * The clock must stay within a jiffie of the gtod. | ||
162 | * But since we may be at the start of a jiffy or the end of one | ||
163 | * we add another jiffy buffer. | ||
164 | */ | ||
165 | max_clock = scd->tick_gtod + (2 + delta_jiffies) * TICK_NSEC; | ||
166 | |||
167 | delta *= scd->multi; | ||
168 | delta >>= MULTI_SHIFT; | ||
107 | 169 | ||
108 | if (unlikely(clock + delta > max_clock)) { | 170 | if (unlikely(clock + delta > max_clock) && check_max(scd)) { |
109 | if (clock < max_clock) | 171 | if (clock < max_clock) |
110 | clock = max_clock; | 172 | clock = max_clock; |
111 | else | 173 | else |
@@ -118,9 +180,12 @@ static void __update_sched_clock(struct sched_clock_data *scd, u64 now) | |||
118 | if (unlikely(clock < min_clock)) | 180 | if (unlikely(clock < min_clock)) |
119 | clock = min_clock; | 181 | clock = min_clock; |
120 | 182 | ||
121 | scd->prev_raw = now; | 183 | if (time) |
122 | scd->prev_jiffies = now_jiffies; | 184 | *time = clock; |
123 | scd->clock = clock; | 185 | else { |
186 | scd->prev_raw = now; | ||
187 | scd->clock = clock; | ||
188 | } | ||
124 | } | 189 | } |
125 | 190 | ||
126 | static void lock_double_clock(struct sched_clock_data *data1, | 191 | static void lock_double_clock(struct sched_clock_data *data1, |
@@ -160,25 +225,30 @@ u64 sched_clock_cpu(int cpu) | |||
160 | now -= my_scd->tick_raw; | 225 | now -= my_scd->tick_raw; |
161 | now += scd->tick_raw; | 226 | now += scd->tick_raw; |
162 | 227 | ||
163 | now -= my_scd->tick_gtod; | 228 | now += my_scd->tick_gtod; |
164 | now += scd->tick_gtod; | 229 | now -= scd->tick_gtod; |
165 | 230 | ||
166 | __raw_spin_unlock(&my_scd->lock); | 231 | __raw_spin_unlock(&my_scd->lock); |
232 | |||
233 | __update_sched_clock(scd, now, &clock); | ||
234 | |||
235 | __raw_spin_unlock(&scd->lock); | ||
236 | |||
167 | } else { | 237 | } else { |
168 | __raw_spin_lock(&scd->lock); | 238 | __raw_spin_lock(&scd->lock); |
239 | __update_sched_clock(scd, now, NULL); | ||
240 | clock = scd->clock; | ||
241 | __raw_spin_unlock(&scd->lock); | ||
169 | } | 242 | } |
170 | 243 | ||
171 | __update_sched_clock(scd, now); | ||
172 | clock = scd->clock; | ||
173 | |||
174 | __raw_spin_unlock(&scd->lock); | ||
175 | |||
176 | return clock; | 244 | return clock; |
177 | } | 245 | } |
178 | 246 | ||
179 | void sched_clock_tick(void) | 247 | void sched_clock_tick(void) |
180 | { | 248 | { |
181 | struct sched_clock_data *scd = this_scd(); | 249 | struct sched_clock_data *scd = this_scd(); |
250 | unsigned long now_jiffies = jiffies; | ||
251 | s64 mult, delta_gtod, delta_raw; | ||
182 | u64 now, now_gtod; | 252 | u64 now, now_gtod; |
183 | 253 | ||
184 | if (unlikely(!sched_clock_running)) | 254 | if (unlikely(!sched_clock_running)) |
@@ -186,18 +256,33 @@ void sched_clock_tick(void) | |||
186 | 256 | ||
187 | WARN_ON_ONCE(!irqs_disabled()); | 257 | WARN_ON_ONCE(!irqs_disabled()); |
188 | 258 | ||
189 | now = sched_clock(); | ||
190 | now_gtod = ktime_to_ns(ktime_get()); | 259 | now_gtod = ktime_to_ns(ktime_get()); |
260 | now = sched_clock(); | ||
191 | 261 | ||
192 | __raw_spin_lock(&scd->lock); | 262 | __raw_spin_lock(&scd->lock); |
193 | __update_sched_clock(scd, now); | 263 | __update_sched_clock(scd, now, NULL); |
194 | /* | 264 | /* |
195 | * update tick_gtod after __update_sched_clock() because that will | 265 | * update tick_gtod after __update_sched_clock() because that will |
196 | * already observe 1 new jiffy; adding a new tick_gtod to that would | 266 | * already observe 1 new jiffy; adding a new tick_gtod to that would |
197 | * increase the clock 2 jiffies. | 267 | * increase the clock 2 jiffies. |
198 | */ | 268 | */ |
269 | delta_gtod = now_gtod - scd->tick_gtod; | ||
270 | delta_raw = now - scd->tick_raw; | ||
271 | |||
272 | if ((long)delta_raw > 0) { | ||
273 | mult = delta_gtod << MULTI_SHIFT; | ||
274 | do_div(mult, delta_raw); | ||
275 | scd->multi = mult; | ||
276 | if (scd->multi > MAX_MULTI) | ||
277 | scd->multi = MAX_MULTI; | ||
278 | else if (scd->multi < MIN_MULTI) | ||
279 | scd->multi = MIN_MULTI; | ||
280 | } else | ||
281 | scd->multi = 1 << MULTI_SHIFT; | ||
282 | |||
199 | scd->tick_raw = now; | 283 | scd->tick_raw = now; |
200 | scd->tick_gtod = now_gtod; | 284 | scd->tick_gtod = now_gtod; |
285 | scd->tick_jiffies = now_jiffies; | ||
201 | __raw_spin_unlock(&scd->lock); | 286 | __raw_spin_unlock(&scd->lock); |
202 | } | 287 | } |
203 | 288 | ||
@@ -227,6 +312,7 @@ void sched_clock_idle_wakeup_event(u64 delta_ns) | |||
227 | __raw_spin_lock(&scd->lock); | 312 | __raw_spin_lock(&scd->lock); |
228 | scd->prev_raw = now; | 313 | scd->prev_raw = now; |
229 | scd->clock += delta_ns; | 314 | scd->clock += delta_ns; |
315 | scd->multi = 1 << MULTI_SHIFT; | ||
230 | __raw_spin_unlock(&scd->lock); | 316 | __raw_spin_unlock(&scd->lock); |
231 | 317 | ||
232 | touch_softlockup_watchdog(); | 318 | touch_softlockup_watchdog(); |
@@ -244,3 +330,16 @@ unsigned long long __attribute__((weak)) sched_clock(void) | |||
244 | { | 330 | { |
245 | return (unsigned long long)jiffies * (NSEC_PER_SEC / HZ); | 331 | return (unsigned long long)jiffies * (NSEC_PER_SEC / HZ); |
246 | } | 332 | } |
333 | |||
334 | unsigned long long cpu_clock(int cpu) | ||
335 | { | ||
336 | unsigned long long clock; | ||
337 | unsigned long flags; | ||
338 | |||
339 | local_irq_save(flags); | ||
340 | clock = sched_clock_cpu(cpu); | ||
341 | local_irq_restore(flags); | ||
342 | |||
343 | return clock; | ||
344 | } | ||
345 | EXPORT_SYMBOL_GPL(cpu_clock); | ||