blob: e54ba63188fb5d65e1060a82cbbfc49ade920f65 [file] [log] [blame]
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -08001/*
2 * linux/kernel/time/tick-sched.c
3 *
4 * Copyright(C) 2005-2006, Thomas Gleixner <tglx@linutronix.de>
5 * Copyright(C) 2005-2007, Red Hat, Inc., Ingo Molnar
6 * Copyright(C) 2006-2007 Timesys Corp., Thomas Gleixner
7 *
8 * No idle tick implementation for low and high resolution timers
9 *
10 * Started by: Thomas Gleixner and Ingo Molnar
11 *
Pavel Machekb10db7f2008-01-30 13:30:00 +010012 * Distribute under GPLv2.
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -080013 */
14#include <linux/cpu.h>
15#include <linux/err.h>
16#include <linux/hrtimer.h>
17#include <linux/interrupt.h>
18#include <linux/kernel_stat.h>
19#include <linux/percpu.h>
20#include <linux/profile.h>
21#include <linux/sched.h>
venkatesh.pallipadi@intel.com8083e4a2008-08-04 11:59:11 -070022#include <linux/module.h>
Amar Singhalf49d99b2011-08-22 19:02:04 -070023#include <linux/rq_stats.h>
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -080024
David S. Miller9e203bc2007-02-24 22:10:13 -080025#include <asm/irq_regs.h>
26
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -080027#include "tick-internal.h"
28
Amar Singhalf49d99b2011-08-22 19:02:04 -070029
30struct rq_data rq_info;
31struct workqueue_struct *rq_wq;
32spinlock_t rq_lock;
33
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -080034/*
35 * Per cpu nohz control structure
36 */
37static DEFINE_PER_CPU(struct tick_sched, tick_cpu_sched);
38
39/*
40 * The time, when the last jiffy update happened. Protected by xtime_lock.
41 */
42static ktime_t last_jiffies_update;
43
Ingo Molnar289f4802007-02-16 01:28:15 -080044struct tick_sched *tick_get_tick_sched(int cpu)
45{
46 return &per_cpu(tick_cpu_sched, cpu);
47}
48
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -080049/*
50 * Must be called with interrupts disabled !
51 */
52static void tick_do_update_jiffies64(ktime_t now)
53{
54 unsigned long ticks = 0;
55 ktime_t delta;
56
Ingo Molnar7a14ce12008-05-12 15:43:53 +020057 /*
58 * Do a quick check without holding xtime_lock:
59 */
60 delta = ktime_sub(now, last_jiffies_update);
61 if (delta.tv64 < tick_period.tv64)
62 return;
63
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -080064 /* Reevalute with xtime_lock held */
65 write_seqlock(&xtime_lock);
66
67 delta = ktime_sub(now, last_jiffies_update);
68 if (delta.tv64 >= tick_period.tv64) {
69
70 delta = ktime_sub(delta, tick_period);
71 last_jiffies_update = ktime_add(last_jiffies_update,
72 tick_period);
73
74 /* Slow path for long timeouts */
75 if (unlikely(delta.tv64 >= tick_period.tv64)) {
76 s64 incr = ktime_to_ns(tick_period);
77
78 ticks = ktime_divns(delta, incr);
79
80 last_jiffies_update = ktime_add_ns(last_jiffies_update,
81 incr * ticks);
82 }
83 do_timer(++ticks);
Thomas Gleixner49d670f2008-09-22 18:56:01 +020084
85 /* Keep the tick_next_period variable up to date */
86 tick_next_period = ktime_add(last_jiffies_update, tick_period);
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -080087 }
88 write_sequnlock(&xtime_lock);
89}
90
91/*
92 * Initialize and return retrieve the jiffies update.
93 */
94static ktime_t tick_init_jiffy_update(void)
95{
96 ktime_t period;
97
98 write_seqlock(&xtime_lock);
99 /* Did we start the jiffies update yet ? */
100 if (last_jiffies_update.tv64 == 0)
101 last_jiffies_update = tick_next_period;
102 period = last_jiffies_update;
103 write_sequnlock(&xtime_lock);
104 return period;
105}
106
107/*
108 * NOHZ - aka dynamic tick functionality
109 */
110#ifdef CONFIG_NO_HZ
111/*
112 * NO HZ enabled ?
113 */
114static int tick_nohz_enabled __read_mostly = 1;
115
116/*
117 * Enable / Disable tickless mode
118 */
119static int __init setup_tick_nohz(char *str)
120{
121 if (!strcmp(str, "off"))
122 tick_nohz_enabled = 0;
123 else if (!strcmp(str, "on"))
124 tick_nohz_enabled = 1;
125 else
126 return 0;
127 return 1;
128}
129
130__setup("nohz=", setup_tick_nohz);
131
132/**
133 * tick_nohz_update_jiffies - update jiffies when idle was interrupted
134 *
135 * Called from interrupt entry when the CPU was idle
136 *
137 * In case the sched_tick was stopped on this CPU, we have to check if jiffies
138 * must be updated. Otherwise an interrupt handler could use a stale jiffy
139 * value. We do this unconditionally on any cpu, as we don't know whether the
140 * cpu, which has the update task assigned is in a long sleep.
141 */
Martin Schwidefskyeed3b9c2009-09-29 14:25:15 +0200142static void tick_nohz_update_jiffies(ktime_t now)
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800143{
144 int cpu = smp_processor_id();
145 struct tick_sched *ts = &per_cpu(tick_cpu_sched, cpu);
146 unsigned long flags;
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800147
Rusty Russell6a7b3dc2008-11-25 02:35:04 +1030148 cpumask_clear_cpu(cpu, nohz_cpu_mask);
Thomas Gleixner5df7fa12008-02-01 17:45:14 +0100149 ts->idle_waketime = now;
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800150
151 local_irq_save(flags);
152 tick_do_update_jiffies64(now);
153 local_irq_restore(flags);
Ingo Molnar02ff3752008-05-12 15:43:53 +0200154
155 touch_softlockup_watchdog();
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800156}
157
Arjan van de Ven595aac42010-05-09 08:22:45 -0700158/*
159 * Updates the per cpu time idle statistics counters
160 */
Arjan van de Ven8d63bf92010-05-09 08:24:03 -0700161static void
Peter Zijlstra8c215bd2010-07-01 09:07:17 +0200162update_ts_time_stats(int cpu, struct tick_sched *ts, ktime_t now, u64 *last_update_time)
Arjan van de Ven595aac42010-05-09 08:22:45 -0700163{
164 ktime_t delta;
165
Arjan van de Ven595aac42010-05-09 08:22:45 -0700166 if (ts->idle_active) {
167 delta = ktime_sub(now, ts->idle_entrytime);
168 ts->idle_sleeptime = ktime_add(ts->idle_sleeptime, delta);
Peter Zijlstra8c215bd2010-07-01 09:07:17 +0200169 if (nr_iowait_cpu(cpu) > 0)
Arjan van de Ven0224cf42010-05-09 08:25:23 -0700170 ts->iowait_sleeptime = ktime_add(ts->iowait_sleeptime, delta);
Arjan van de Ven8c7b09f2010-05-09 08:23:23 -0700171 ts->idle_entrytime = now;
Arjan van de Ven595aac42010-05-09 08:22:45 -0700172 }
Arjan van de Ven8d63bf92010-05-09 08:24:03 -0700173
Arjan van de Vene0e37c22010-05-09 08:24:39 -0700174 if (last_update_time)
Arjan van de Ven8d63bf92010-05-09 08:24:03 -0700175 *last_update_time = ktime_to_us(now);
176
Arjan van de Ven595aac42010-05-09 08:22:45 -0700177}
178
Martin Schwidefskyeed3b9c2009-09-29 14:25:15 +0200179static void tick_nohz_stop_idle(int cpu, ktime_t now)
Venki Pallipadi6378ddb2008-01-30 13:30:04 +0100180{
181 struct tick_sched *ts = &per_cpu(tick_cpu_sched, cpu);
182
Peter Zijlstra8c215bd2010-07-01 09:07:17 +0200183 update_ts_time_stats(cpu, ts, now, NULL);
Martin Schwidefskyeed3b9c2009-09-29 14:25:15 +0200184 ts->idle_active = 0;
Peter Zijlstra56c74262008-09-01 16:44:23 +0200185
Martin Schwidefskyeed3b9c2009-09-29 14:25:15 +0200186 sched_clock_idle_wakeup_event(0);
Venki Pallipadi6378ddb2008-01-30 13:30:04 +0100187}
188
Peter Zijlstra8c215bd2010-07-01 09:07:17 +0200189static ktime_t tick_nohz_start_idle(int cpu, struct tick_sched *ts)
Venki Pallipadi6378ddb2008-01-30 13:30:04 +0100190{
Arjan van de Ven595aac42010-05-09 08:22:45 -0700191 ktime_t now;
Venki Pallipadi6378ddb2008-01-30 13:30:04 +0100192
193 now = ktime_get();
Arjan van de Ven595aac42010-05-09 08:22:45 -0700194
Peter Zijlstra8c215bd2010-07-01 09:07:17 +0200195 update_ts_time_stats(cpu, ts, now, NULL);
Arjan van de Ven595aac42010-05-09 08:22:45 -0700196
Venki Pallipadi6378ddb2008-01-30 13:30:04 +0100197 ts->idle_entrytime = now;
198 ts->idle_active = 1;
Peter Zijlstra56c74262008-09-01 16:44:23 +0200199 sched_clock_idle_sleep_event();
Venki Pallipadi6378ddb2008-01-30 13:30:04 +0100200 return now;
201}
202
Arjan van de Venb1f724c2010-05-09 08:22:08 -0700203/**
204 * get_cpu_idle_time_us - get the total idle time of a cpu
205 * @cpu: CPU number to query
206 * @last_update_time: variable to store update time in
207 *
208 * Return the cummulative idle time (since boot) for a given
209 * CPU, in microseconds. The idle time returned includes
210 * the iowait time (unlike what "top" and co report).
211 *
212 * This time is measured via accounting rather than sampling,
213 * and is as accurate as ktime_get() is.
214 *
215 * This function returns -1 if NOHZ is not enabled.
216 */
Venki Pallipadi6378ddb2008-01-30 13:30:04 +0100217u64 get_cpu_idle_time_us(int cpu, u64 *last_update_time)
218{
219 struct tick_sched *ts = &per_cpu(tick_cpu_sched, cpu);
220
venkatesh.pallipadi@intel.com8083e4a2008-08-04 11:59:11 -0700221 if (!tick_nohz_enabled)
222 return -1;
223
Peter Zijlstra8c215bd2010-07-01 09:07:17 +0200224 update_ts_time_stats(cpu, ts, ktime_get(), last_update_time);
venkatesh.pallipadi@intel.com8083e4a2008-08-04 11:59:11 -0700225
Venki Pallipadi6378ddb2008-01-30 13:30:04 +0100226 return ktime_to_us(ts->idle_sleeptime);
227}
venkatesh.pallipadi@intel.com8083e4a2008-08-04 11:59:11 -0700228EXPORT_SYMBOL_GPL(get_cpu_idle_time_us);
Venki Pallipadi6378ddb2008-01-30 13:30:04 +0100229
Arjan van de Ven0224cf42010-05-09 08:25:23 -0700230/*
231 * get_cpu_iowait_time_us - get the total iowait time of a cpu
232 * @cpu: CPU number to query
233 * @last_update_time: variable to store update time in
234 *
235 * Return the cummulative iowait time (since boot) for a given
236 * CPU, in microseconds.
237 *
238 * This time is measured via accounting rather than sampling,
239 * and is as accurate as ktime_get() is.
240 *
241 * This function returns -1 if NOHZ is not enabled.
242 */
243u64 get_cpu_iowait_time_us(int cpu, u64 *last_update_time)
244{
245 struct tick_sched *ts = &per_cpu(tick_cpu_sched, cpu);
246
247 if (!tick_nohz_enabled)
248 return -1;
249
Peter Zijlstra8c215bd2010-07-01 09:07:17 +0200250 update_ts_time_stats(cpu, ts, ktime_get(), last_update_time);
Arjan van de Ven0224cf42010-05-09 08:25:23 -0700251
252 return ktime_to_us(ts->iowait_sleeptime);
253}
254EXPORT_SYMBOL_GPL(get_cpu_iowait_time_us);
255
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800256/**
257 * tick_nohz_stop_sched_tick - stop the idle tick from the idle task
258 *
259 * When the next event is more than a tick into the future, stop the idle tick
260 * Called either from the idle loop or from irq_exit() when an idle period was
261 * just interrupted by an interrupt which did not cause a reschedule.
262 */
Thomas Gleixnerb8f8c3c2008-07-18 17:27:28 +0200263void tick_nohz_stop_sched_tick(int inidle)
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800264{
265 unsigned long seq, last_jiffies, next_jiffies, delta_jiffies, flags;
266 struct tick_sched *ts;
Venki Pallipadi6378ddb2008-01-30 13:30:04 +0100267 ktime_t last_update, expires, now;
Len Brown4f86d3a2007-10-03 18:58:00 -0400268 struct clock_event_device *dev = __get_cpu_var(tick_cpu_device).evtdev;
Jon Hunter98962462009-08-18 12:45:10 -0500269 u64 time_delta;
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800270 int cpu;
271
272 local_irq_save(flags);
273
274 cpu = smp_processor_id();
275 ts = &per_cpu(tick_cpu_sched, cpu);
Eero Nurkkalaf2e21c92009-05-25 09:57:37 +0300276
277 /*
278 * Call to tick_nohz_start_idle stops the last_update_time from being
279 * updated. Thus, it must not be called in the event we are called from
280 * irq_exit() with the prior state different than idle.
281 */
282 if (!inidle && !ts->inidle)
283 goto end;
284
Eero Nurkkalafdc6f192009-10-07 11:54:26 +0300285 /*
286 * Set ts->inidle unconditionally. Even if the system did not
287 * switch to NOHZ mode the cpu frequency governers rely on the
288 * update of the idle time accounting in tick_nohz_start_idle().
289 */
290 ts->inidle = 1;
291
Peter Zijlstra8c215bd2010-07-01 09:07:17 +0200292 now = tick_nohz_start_idle(cpu, ts);
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800293
Thomas Gleixner5e41d0d2007-09-16 15:36:43 +0200294 /*
295 * If this cpu is offline and it is the one which updates
296 * jiffies, then give up the assignment and let it be taken by
297 * the cpu which runs the tick timer next. If we don't drop
298 * this here the jiffies might be stale and do_timer() never
299 * invoked.
300 */
301 if (unlikely(!cpu_online(cpu))) {
302 if (cpu == tick_do_timer_cpu)
Thomas Gleixner64414022008-09-22 18:46:37 +0200303 tick_do_timer_cpu = TICK_DO_TIMER_NONE;
Thomas Gleixner5e41d0d2007-09-16 15:36:43 +0200304 }
305
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800306 if (unlikely(ts->nohz_mode == NOHZ_MODE_INACTIVE))
307 goto end;
308
309 if (need_resched())
310 goto end;
311
Heiko Carstensfa116ea2008-12-11 17:04:11 +0100312 if (unlikely(local_softirq_pending() && cpu_online(cpu))) {
Thomas Gleixner35282312007-05-23 13:57:37 -0700313 static int ratelimit;
314
315 if (ratelimit < 10) {
316 printk(KERN_ERR "NOHZ: local_softirq_pending %02x\n",
Thomas Gleixner529eacc2009-11-13 14:32:19 +0100317 (unsigned int) local_softirq_pending());
Thomas Gleixner35282312007-05-23 13:57:37 -0700318 ratelimit++;
319 }
Heiko Carstens857f3fd2008-07-11 11:09:22 +0200320 goto end;
Thomas Gleixner35282312007-05-23 13:57:37 -0700321 }
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800322
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800323 ts->idle_calls++;
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800324 /* Read jiffies and the time when jiffies were updated last */
325 do {
326 seq = read_seqbegin(&xtime_lock);
327 last_update = last_jiffies_update;
328 last_jiffies = jiffies;
Thomas Gleixner27185012009-11-12 22:12:06 +0100329 time_delta = timekeeping_max_deferment();
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800330 } while (read_seqretry(&xtime_lock, seq));
331
Martin Schwidefsky3c5d92a2009-09-29 14:25:16 +0200332 if (rcu_needs_cpu(cpu) || printk_needs_cpu(cpu) ||
Peter Zijlstra396e8942010-07-09 15:12:27 +0200333 arch_needs_cpu(cpu)) {
Martin Schwidefsky3c5d92a2009-09-29 14:25:16 +0200334 next_jiffies = last_jiffies + 1;
Ingo Molnar6ba9b342007-02-19 18:11:56 +0000335 delta_jiffies = 1;
Martin Schwidefsky3c5d92a2009-09-29 14:25:16 +0200336 } else {
337 /* Get the next timer wheel timer */
338 next_jiffies = get_next_timer_interrupt(last_jiffies);
339 delta_jiffies = next_jiffies - last_jiffies;
340 }
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800341 /*
342 * Do not stop the tick, if we are only one off
343 * or if the cpu is required for rcu
344 */
Ingo Molnar6ba9b342007-02-19 18:11:56 +0000345 if (!ts->tick_stopped && delta_jiffies == 1)
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800346 goto out;
347
348 /* Schedule the tick, if we are at least one jiffie off */
349 if ((long)delta_jiffies >= 1) {
350
Woodruff, Richard00147442008-12-01 14:18:11 -0800351 /*
Woodruff, Richard00147442008-12-01 14:18:11 -0800352 * If this cpu is the one which updates jiffies, then
353 * give up the assignment and let it be taken by the
354 * cpu which runs the tick timer next, which might be
355 * this cpu as well. If we don't drop this here the
356 * jiffies might be stale and do_timer() never
Thomas Gleixner27185012009-11-12 22:12:06 +0100357 * invoked. Keep track of the fact that it was the one
358 * which had the do_timer() duty last. If this cpu is
359 * the one which had the do_timer() duty last, we
360 * limit the sleep time to the timekeeping
361 * max_deferement value which we retrieved
362 * above. Otherwise we can sleep as long as we want.
Woodruff, Richard00147442008-12-01 14:18:11 -0800363 */
Thomas Gleixner27185012009-11-12 22:12:06 +0100364 if (cpu == tick_do_timer_cpu) {
Woodruff, Richard00147442008-12-01 14:18:11 -0800365 tick_do_timer_cpu = TICK_DO_TIMER_NONE;
Thomas Gleixner27185012009-11-12 22:12:06 +0100366 ts->do_timer_last = 1;
367 } else if (tick_do_timer_cpu != TICK_DO_TIMER_NONE) {
368 time_delta = KTIME_MAX;
369 ts->do_timer_last = 0;
370 } else if (!ts->do_timer_last) {
371 time_delta = KTIME_MAX;
372 }
373
374 /*
Jon Hunter98962462009-08-18 12:45:10 -0500375 * calculate the expiry time for the next timer wheel
376 * timer. delta_jiffies >= NEXT_TIMER_MAX_DELTA signals
377 * that there is no timer pending or at least extremely
378 * far into the future (12 days for HZ=1000). In this
379 * case we set the expiry to the end of time.
380 */
381 if (likely(delta_jiffies < NEXT_TIMER_MAX_DELTA)) {
382 /*
383 * Calculate the time delta for the next timer event.
384 * If the time delta exceeds the maximum time delta
385 * permitted by the current clocksource then adjust
386 * the time delta accordingly to ensure the
387 * clocksource does not wrap.
388 */
389 time_delta = min_t(u64, time_delta,
390 tick_period.tv64 * delta_jiffies);
Jon Hunter98962462009-08-18 12:45:10 -0500391 }
Woodruff, Richard00147442008-12-01 14:18:11 -0800392
Thomas Gleixner27185012009-11-12 22:12:06 +0100393 if (time_delta < KTIME_MAX)
394 expires = ktime_add_ns(last_update, time_delta);
395 else
396 expires.tv64 = KTIME_MAX;
Woodruff, Richard00147442008-12-01 14:18:11 -0800397
Ingo Molnar6ba9b342007-02-19 18:11:56 +0000398 if (delta_jiffies > 1)
Rusty Russell6a7b3dc2008-11-25 02:35:04 +1030399 cpumask_set_cpu(cpu, nohz_cpu_mask);
Woodruff, Richard00147442008-12-01 14:18:11 -0800400
401 /* Skip reprogram of event if its not changed */
402 if (ts->tick_stopped && ktime_equal(expires, dev->next_event))
403 goto out;
404
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800405 /*
406 * nohz_stop_sched_tick can be called several times before
407 * the nohz_restart_sched_tick is called. This happens when
408 * interrupts arrive which do not cause a reschedule. In the
409 * first call we save the current tick time, so we can restart
410 * the scheduler tick in nohz_restart_sched_tick.
411 */
412 if (!ts->tick_stopped) {
Venkatesh Pallipadi83cd4fe2010-05-21 17:09:41 -0700413 select_nohz_load_balancer(1);
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -0700414
Arjan van de Vencc584b22008-09-01 15:02:30 -0700415 ts->idle_tick = hrtimer_get_expires(&ts->sched_timer);
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800416 ts->tick_stopped = 1;
417 ts->idle_jiffies = last_jiffies;
Steven Rostedt2232c2d2008-02-29 18:46:50 +0100418 rcu_enter_nohz();
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800419 }
Thomas Gleixnerd3ed7822007-05-08 00:30:03 -0700420
Thomas Gleixnereaad0842007-05-29 23:47:39 +0200421 ts->idle_sleeps++;
422
Jon Hunter98962462009-08-18 12:45:10 -0500423 /* Mark expires */
424 ts->idle_expires = expires;
425
Thomas Gleixnereaad0842007-05-29 23:47:39 +0200426 /*
Jon Hunter98962462009-08-18 12:45:10 -0500427 * If the expiration time == KTIME_MAX, then
428 * in this case we simply stop the tick timer.
Thomas Gleixnereaad0842007-05-29 23:47:39 +0200429 */
Jon Hunter98962462009-08-18 12:45:10 -0500430 if (unlikely(expires.tv64 == KTIME_MAX)) {
Thomas Gleixnereaad0842007-05-29 23:47:39 +0200431 if (ts->nohz_mode == NOHZ_MODE_HIGHRES)
432 hrtimer_cancel(&ts->sched_timer);
433 goto out;
434 }
435
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800436 if (ts->nohz_mode == NOHZ_MODE_HIGHRES) {
437 hrtimer_start(&ts->sched_timer, expires,
Arun R Bharadwaj5c333862009-04-16 12:14:37 +0530438 HRTIMER_MODE_ABS_PINNED);
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800439 /* Check, if the timer was already in the past */
440 if (hrtimer_active(&ts->sched_timer))
441 goto out;
Pavel Machek4c9dc642008-01-30 13:30:00 +0100442 } else if (!tick_program_event(expires, 0))
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800443 goto out;
444 /*
445 * We are past the event already. So we crossed a
446 * jiffie boundary. Update jiffies and raise the
447 * softirq.
448 */
449 tick_do_update_jiffies64(ktime_get());
Rusty Russell6a7b3dc2008-11-25 02:35:04 +1030450 cpumask_clear_cpu(cpu, nohz_cpu_mask);
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800451 }
452 raise_softirq_irqoff(TIMER_SOFTIRQ);
453out:
454 ts->next_jiffies = next_jiffies;
455 ts->last_jiffies = last_jiffies;
Len Brown4f86d3a2007-10-03 18:58:00 -0400456 ts->sleep_length = ktime_sub(dev->next_event, now);
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800457end:
458 local_irq_restore(flags);
459}
460
461/**
Len Brown4f86d3a2007-10-03 18:58:00 -0400462 * tick_nohz_get_sleep_length - return the length of the current sleep
463 *
464 * Called from power state control code with interrupts disabled
465 */
466ktime_t tick_nohz_get_sleep_length(void)
467{
468 struct tick_sched *ts = &__get_cpu_var(tick_cpu_sched);
469
470 return ts->sleep_length;
471}
472
Thomas Gleixnerc34bec52008-10-17 10:04:34 +0200473static void tick_nohz_restart(struct tick_sched *ts, ktime_t now)
474{
475 hrtimer_cancel(&ts->sched_timer);
Thomas Gleixner268a3dc2008-10-22 09:48:06 +0200476 hrtimer_set_expires(&ts->sched_timer, ts->idle_tick);
Thomas Gleixnerc34bec52008-10-17 10:04:34 +0200477
478 while (1) {
479 /* Forward the time to expire in the future */
480 hrtimer_forward(&ts->sched_timer, now, tick_period);
481
482 if (ts->nohz_mode == NOHZ_MODE_HIGHRES) {
Thomas Gleixner268a3dc2008-10-22 09:48:06 +0200483 hrtimer_start_expires(&ts->sched_timer,
Arun R Bharadwaj5c333862009-04-16 12:14:37 +0530484 HRTIMER_MODE_ABS_PINNED);
Thomas Gleixnerc34bec52008-10-17 10:04:34 +0200485 /* Check, if the timer was already in the past */
486 if (hrtimer_active(&ts->sched_timer))
487 break;
488 } else {
Thomas Gleixner268a3dc2008-10-22 09:48:06 +0200489 if (!tick_program_event(
490 hrtimer_get_expires(&ts->sched_timer), 0))
Thomas Gleixnerc34bec52008-10-17 10:04:34 +0200491 break;
492 }
493 /* Update jiffies and reread time */
494 tick_do_update_jiffies64(now);
495 now = ktime_get();
496 }
497}
498
Len Brown4f86d3a2007-10-03 18:58:00 -0400499/**
Li Zefan8dce39c2007-11-05 14:51:10 -0800500 * tick_nohz_restart_sched_tick - restart the idle tick from the idle task
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800501 *
502 * Restart the idle tick when the CPU is woken up from idle
503 */
504void tick_nohz_restart_sched_tick(void)
505{
506 int cpu = smp_processor_id();
507 struct tick_sched *ts = &per_cpu(tick_cpu_sched, cpu);
Martin Schwidefsky79741dd2008-12-31 15:11:38 +0100508#ifndef CONFIG_VIRT_CPU_ACCOUNTING
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800509 unsigned long ticks;
Martin Schwidefsky79741dd2008-12-31 15:11:38 +0100510#endif
Venki Pallipadi6378ddb2008-01-30 13:30:04 +0100511 ktime_t now;
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800512
513 local_irq_disable();
Martin Schwidefskyeed3b9c2009-09-29 14:25:15 +0200514 if (ts->idle_active || (ts->inidle && ts->tick_stopped))
515 now = ktime_get();
516
517 if (ts->idle_active)
518 tick_nohz_stop_idle(cpu, now);
Venki Pallipadi6378ddb2008-01-30 13:30:04 +0100519
Thomas Gleixnerb8f8c3c2008-07-18 17:27:28 +0200520 if (!ts->inidle || !ts->tick_stopped) {
521 ts->inidle = 0;
Venki Pallipadi6378ddb2008-01-30 13:30:04 +0100522 local_irq_enable();
523 return;
524 }
525
Thomas Gleixnerb8f8c3c2008-07-18 17:27:28 +0200526 ts->inidle = 0;
527
Steven Rostedt2232c2d2008-02-29 18:46:50 +0100528 rcu_exit_nohz();
529
Venki Pallipadi6378ddb2008-01-30 13:30:04 +0100530 /* Update jiffies first */
Siddha, Suresh B46cb4b72007-05-08 00:32:51 -0700531 select_nohz_load_balancer(0);
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800532 tick_do_update_jiffies64(now);
Rusty Russell6a7b3dc2008-11-25 02:35:04 +1030533 cpumask_clear_cpu(cpu, nohz_cpu_mask);
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800534
Martin Schwidefsky79741dd2008-12-31 15:11:38 +0100535#ifndef CONFIG_VIRT_CPU_ACCOUNTING
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800536 /*
537 * We stopped the tick in idle. Update process times would miss the
538 * time we slept as update_process_times does only a 1 tick
539 * accounting. Enforce that this is accounted to idle !
540 */
541 ticks = jiffies - ts->idle_jiffies;
542 /*
543 * We might be one off. Do not randomly account a huge number of ticks!
544 */
Martin Schwidefsky79741dd2008-12-31 15:11:38 +0100545 if (ticks && ticks < LONG_MAX)
546 account_idle_ticks(ticks);
547#endif
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800548
Ingo Molnar126e01b2008-04-25 00:25:08 +0200549 touch_softlockup_watchdog();
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800550 /*
551 * Cancel the scheduled timer and restore the tick
552 */
553 ts->tick_stopped = 0;
Thomas Gleixner5df7fa12008-02-01 17:45:14 +0100554 ts->idle_exittime = now;
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800555
Thomas Gleixnerc34bec52008-10-17 10:04:34 +0200556 tick_nohz_restart(ts, now);
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800557
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800558 local_irq_enable();
559}
560
561static int tick_nohz_reprogram(struct tick_sched *ts, ktime_t now)
562{
563 hrtimer_forward(&ts->sched_timer, now, tick_period);
Arjan van de Vencc584b22008-09-01 15:02:30 -0700564 return tick_program_event(hrtimer_get_expires(&ts->sched_timer), 0);
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800565}
566
567/*
568 * The nohz low res interrupt handler
569 */
570static void tick_nohz_handler(struct clock_event_device *dev)
571{
572 struct tick_sched *ts = &__get_cpu_var(tick_cpu_sched);
573 struct pt_regs *regs = get_irq_regs();
Thomas Gleixnerd3ed7822007-05-08 00:30:03 -0700574 int cpu = smp_processor_id();
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800575 ktime_t now = ktime_get();
576
577 dev->next_event.tv64 = KTIME_MAX;
578
Thomas Gleixnerd3ed7822007-05-08 00:30:03 -0700579 /*
580 * Check if the do_timer duty was dropped. We don't care about
581 * concurrency: This happens only when the cpu in charge went
582 * into a long sleep. If two cpus happen to assign themself to
583 * this duty, then the jiffies update is still serialized by
584 * xtime_lock.
585 */
Thomas Gleixner64414022008-09-22 18:46:37 +0200586 if (unlikely(tick_do_timer_cpu == TICK_DO_TIMER_NONE))
Thomas Gleixnerd3ed7822007-05-08 00:30:03 -0700587 tick_do_timer_cpu = cpu;
588
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800589 /* Check, if the jiffies need an update */
Thomas Gleixnerd3ed7822007-05-08 00:30:03 -0700590 if (tick_do_timer_cpu == cpu)
591 tick_do_update_jiffies64(now);
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800592
593 /*
594 * When we are idle and the tick is stopped, we have to touch
595 * the watchdog as we might not schedule for a really long
596 * time. This happens on complete idle SMP systems while
597 * waiting on the login prompt. We also increment the "start
598 * of idle" jiffy stamp so the idle accounting adjustment we
599 * do when we go busy again does not account too much ticks.
600 */
601 if (ts->tick_stopped) {
602 touch_softlockup_watchdog();
603 ts->idle_jiffies++;
604 }
605
606 update_process_times(user_mode(regs));
607 profile_tick(CPU_PROFILING);
608
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800609 while (tick_nohz_reprogram(ts, now)) {
610 now = ktime_get();
611 tick_do_update_jiffies64(now);
612 }
613}
614
615/**
616 * tick_nohz_switch_to_nohz - switch to nohz mode
617 */
618static void tick_nohz_switch_to_nohz(void)
619{
620 struct tick_sched *ts = &__get_cpu_var(tick_cpu_sched);
621 ktime_t next;
622
623 if (!tick_nohz_enabled)
624 return;
625
626 local_irq_disable();
627 if (tick_switch_to_oneshot(tick_nohz_handler)) {
628 local_irq_enable();
629 return;
630 }
631
632 ts->nohz_mode = NOHZ_MODE_LOWRES;
633
634 /*
635 * Recycle the hrtimer in ts, so we can share the
636 * hrtimer_forward with the highres code.
637 */
638 hrtimer_init(&ts->sched_timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
639 /* Get the next period */
640 next = tick_init_jiffy_update();
641
642 for (;;) {
Arjan van de Vencc584b22008-09-01 15:02:30 -0700643 hrtimer_set_expires(&ts->sched_timer, next);
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800644 if (!tick_program_event(next, 0))
645 break;
646 next = ktime_add(next, tick_period);
647 }
648 local_irq_enable();
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800649}
650
Thomas Gleixnerfb02fbc2008-10-17 10:01:23 +0200651/*
652 * When NOHZ is enabled and the tick is stopped, we need to kick the
653 * tick timer from irq_enter() so that the jiffies update is kept
654 * alive during long running softirqs. That's ugly as hell, but
655 * correctness is key even if we need to fix the offending softirq in
656 * the first place.
657 *
658 * Note, this is different to tick_nohz_restart. We just kick the
659 * timer and do not touch the other magic bits which need to be done
660 * when idle is left.
661 */
Martin Schwidefskyeed3b9c2009-09-29 14:25:15 +0200662static void tick_nohz_kick_tick(int cpu, ktime_t now)
Thomas Gleixnerfb02fbc2008-10-17 10:01:23 +0200663{
Thomas Gleixnerae992862008-11-10 13:20:23 +0100664#if 0
665 /* Switch back to 2.6.27 behaviour */
666
Thomas Gleixnerfb02fbc2008-10-17 10:01:23 +0200667 struct tick_sched *ts = &per_cpu(tick_cpu_sched, cpu);
Martin Schwidefskyeed3b9c2009-09-29 14:25:15 +0200668 ktime_t delta;
Thomas Gleixnerfb02fbc2008-10-17 10:01:23 +0200669
Thomas Gleixnerc4bd8222008-10-21 20:17:35 +0200670 /*
671 * Do not touch the tick device, when the next expiry is either
672 * already reached or less/equal than the tick period.
673 */
Thomas Gleixner268a3dc2008-10-22 09:48:06 +0200674 delta = ktime_sub(hrtimer_get_expires(&ts->sched_timer), now);
Thomas Gleixnerc4bd8222008-10-21 20:17:35 +0200675 if (delta.tv64 <= tick_period.tv64)
676 return;
677
678 tick_nohz_restart(ts, now);
Thomas Gleixnerae992862008-11-10 13:20:23 +0100679#endif
Thomas Gleixnerfb02fbc2008-10-17 10:01:23 +0200680}
681
Martin Schwidefskyeed3b9c2009-09-29 14:25:15 +0200682static inline void tick_check_nohz(int cpu)
683{
684 struct tick_sched *ts = &per_cpu(tick_cpu_sched, cpu);
685 ktime_t now;
686
687 if (!ts->idle_active && !ts->tick_stopped)
688 return;
689 now = ktime_get();
690 if (ts->idle_active)
691 tick_nohz_stop_idle(cpu, now);
692 if (ts->tick_stopped) {
693 tick_nohz_update_jiffies(now);
694 tick_nohz_kick_tick(cpu, now);
695 }
696}
697
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800698#else
699
700static inline void tick_nohz_switch_to_nohz(void) { }
Martin Schwidefskyeed3b9c2009-09-29 14:25:15 +0200701static inline void tick_check_nohz(int cpu) { }
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800702
703#endif /* NO_HZ */
704
705/*
Thomas Gleixner719254f2008-10-17 09:59:47 +0200706 * Called from irq_enter to notify about the possible interruption of idle()
707 */
708void tick_check_idle(int cpu)
709{
Thomas Gleixnerfb02fbc2008-10-17 10:01:23 +0200710 tick_check_oneshot_broadcast(cpu);
Martin Schwidefskyeed3b9c2009-09-29 14:25:15 +0200711 tick_check_nohz(cpu);
Thomas Gleixner719254f2008-10-17 09:59:47 +0200712}
713
714/*
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800715 * High resolution timer specific code
716 */
717#ifdef CONFIG_HIGH_RES_TIMERS
Amar Singhalf49d99b2011-08-22 19:02:04 -0700718static void update_rq_stats(void)
719{
720 unsigned long jiffy_gap = 0;
721 unsigned int rq_avg = 0;
722 unsigned long flags = 0;
723
724 jiffy_gap = jiffies - rq_info.rq_poll_last_jiffy;
725
726 if (jiffy_gap >= rq_info.rq_poll_jiffies) {
727
728 spin_lock_irqsave(&rq_lock, flags);
729
730 if (!rq_info.rq_avg)
731 rq_info.rq_poll_total_jiffies = 0;
732
733 rq_avg = nr_running() * 10;
734
735 if (rq_info.rq_poll_total_jiffies) {
736 rq_avg = (rq_avg * jiffy_gap) +
737 (rq_info.rq_avg *
738 rq_info.rq_poll_total_jiffies);
739 do_div(rq_avg,
740 rq_info.rq_poll_total_jiffies + jiffy_gap);
741 }
742
743 rq_info.rq_avg = rq_avg;
744 rq_info.rq_poll_total_jiffies += jiffy_gap;
745 rq_info.rq_poll_last_jiffy = jiffies;
746
747 spin_unlock_irqrestore(&rq_lock, flags);
748 }
749}
750
751static void wakeup_user(void)
752{
753 unsigned long jiffy_gap;
754
755 jiffy_gap = jiffies - rq_info.def_timer_last_jiffy;
756
757 if (jiffy_gap >= rq_info.def_timer_jiffies) {
758 rq_info.def_timer_last_jiffy = jiffies;
759 queue_work(rq_wq, &rq_info.def_timer_work);
760 }
761}
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800762/*
Pavel Machek4c9dc642008-01-30 13:30:00 +0100763 * We rearm the timer until we get disabled by the idle code.
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800764 * Called with interrupts disabled and timer->base->cpu_base->lock held.
765 */
766static enum hrtimer_restart tick_sched_timer(struct hrtimer *timer)
767{
768 struct tick_sched *ts =
769 container_of(timer, struct tick_sched, sched_timer);
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800770 struct pt_regs *regs = get_irq_regs();
771 ktime_t now = ktime_get();
Thomas Gleixnerd3ed7822007-05-08 00:30:03 -0700772 int cpu = smp_processor_id();
773
774#ifdef CONFIG_NO_HZ
775 /*
776 * Check if the do_timer duty was dropped. We don't care about
777 * concurrency: This happens only when the cpu in charge went
778 * into a long sleep. If two cpus happen to assign themself to
779 * this duty, then the jiffies update is still serialized by
780 * xtime_lock.
781 */
Thomas Gleixner64414022008-09-22 18:46:37 +0200782 if (unlikely(tick_do_timer_cpu == TICK_DO_TIMER_NONE))
Thomas Gleixnerd3ed7822007-05-08 00:30:03 -0700783 tick_do_timer_cpu = cpu;
784#endif
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800785
786 /* Check, if the jiffies need an update */
Thomas Gleixnerd3ed7822007-05-08 00:30:03 -0700787 if (tick_do_timer_cpu == cpu)
788 tick_do_update_jiffies64(now);
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800789
790 /*
791 * Do not call, when we are not in irq context and have
792 * no valid regs pointer
793 */
794 if (regs) {
795 /*
796 * When we are idle and the tick is stopped, we have to touch
797 * the watchdog as we might not schedule for a really long
798 * time. This happens on complete idle SMP systems while
799 * waiting on the login prompt. We also increment the "start of
800 * idle" jiffy stamp so the idle accounting adjustment we do
801 * when we go busy again does not account too much ticks.
802 */
803 if (ts->tick_stopped) {
804 touch_softlockup_watchdog();
805 ts->idle_jiffies++;
806 }
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800807 update_process_times(user_mode(regs));
808 profile_tick(CPU_PROFILING);
Amar Singhalf49d99b2011-08-22 19:02:04 -0700809
810
811 if ((rq_info.init == 1) && (cpu == 0)) {
812
813 /*
814 * update run queue statistics
815 */
816 update_rq_stats();
817
818 /*
819 * wakeup user if needed
820 */
821 wakeup_user();
822 }
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800823 }
824
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800825 hrtimer_forward(timer, now, tick_period);
826
827 return HRTIMER_RESTART;
828}
829
830/**
831 * tick_setup_sched_timer - setup the tick emulation timer
832 */
833void tick_setup_sched_timer(void)
834{
835 struct tick_sched *ts = &__get_cpu_var(tick_cpu_sched);
836 ktime_t now = ktime_get();
837
838 /*
839 * Emulate tick processing via per-CPU hrtimers:
840 */
841 hrtimer_init(&ts->sched_timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
842 ts->sched_timer.function = tick_sched_timer;
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800843
john stultz37045402007-07-21 04:37:35 -0700844 /* Get the next period (per cpu) */
Arjan van de Vencc584b22008-09-01 15:02:30 -0700845 hrtimer_set_expires(&ts->sched_timer, tick_init_jiffy_update());
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800846
847 for (;;) {
848 hrtimer_forward(&ts->sched_timer, now, tick_period);
Arun R Bharadwaj5c333862009-04-16 12:14:37 +0530849 hrtimer_start_expires(&ts->sched_timer,
850 HRTIMER_MODE_ABS_PINNED);
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800851 /* Check, if the timer was already in the past */
852 if (hrtimer_active(&ts->sched_timer))
853 break;
854 now = ktime_get();
855 }
856
857#ifdef CONFIG_NO_HZ
Heiko Carstens45db69a2011-08-23 13:20:46 +0200858 if (tick_nohz_enabled)
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800859 ts->nohz_mode = NOHZ_MODE_HIGHRES;
860#endif
861}
Miao Xie3c4fbe52008-08-20 16:37:38 -0700862#endif /* HIGH_RES_TIMERS */
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800863
Miao Xie3c4fbe52008-08-20 16:37:38 -0700864#if defined CONFIG_NO_HZ || defined CONFIG_HIGH_RES_TIMERS
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800865void tick_cancel_sched_timer(int cpu)
866{
867 struct tick_sched *ts = &per_cpu(tick_cpu_sched, cpu);
868
Miao Xie3c4fbe52008-08-20 16:37:38 -0700869# ifdef CONFIG_HIGH_RES_TIMERS
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800870 if (ts->sched_timer.base)
871 hrtimer_cancel(&ts->sched_timer);
Miao Xie3c4fbe52008-08-20 16:37:38 -0700872# endif
Karsten Wiesea7901762008-03-04 14:59:55 -0800873
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800874 ts->nohz_mode = NOHZ_MODE_INACTIVE;
875}
Miao Xie3c4fbe52008-08-20 16:37:38 -0700876#endif
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800877
878/**
879 * Async notification about clocksource changes
880 */
881void tick_clock_notify(void)
882{
883 int cpu;
884
885 for_each_possible_cpu(cpu)
886 set_bit(0, &per_cpu(tick_cpu_sched, cpu).check_clocks);
887}
888
889/*
890 * Async notification about clock event changes
891 */
892void tick_oneshot_notify(void)
893{
894 struct tick_sched *ts = &__get_cpu_var(tick_cpu_sched);
895
896 set_bit(0, &ts->check_clocks);
897}
898
899/**
900 * Check, if a change happened, which makes oneshot possible.
901 *
902 * Called cyclic from the hrtimer softirq (driven by the timer
903 * softirq) allow_nohz signals, that we can switch into low-res nohz
904 * mode, because high resolution timers are disabled (either compile
905 * or runtime).
906 */
907int tick_check_oneshot_change(int allow_nohz)
908{
909 struct tick_sched *ts = &__get_cpu_var(tick_cpu_sched);
910
911 if (!test_and_clear_bit(0, &ts->check_clocks))
912 return 0;
913
914 if (ts->nohz_mode != NOHZ_MODE_INACTIVE)
915 return 0;
916
Li Zefancf4fc6c2008-02-08 04:19:24 -0800917 if (!timekeeping_valid_for_hres() || !tick_is_oneshot_available())
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800918 return 0;
919
920 if (!allow_nohz)
921 return 1;
922
923 tick_nohz_switch_to_nohz();
924 return 0;
925}