nohz: Make idle/iowait counter update conditional
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / kernel / time / tick-sched.c
blob664c4a365439ac79ba6d39bf4b67c6c476a4525a
1 /*
2 * linux/kernel/time/tick-sched.c
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
8 * No idle tick implementation for low and high resolution timers
10 * Started by: Thomas Gleixner and Ingo Molnar
12 * Distribute under GPLv2.
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>
22 #include <linux/module.h>
24 #include <asm/irq_regs.h>
26 #include "tick-internal.h"
29 * Per cpu nohz control structure
31 static DEFINE_PER_CPU(struct tick_sched, tick_cpu_sched);
34 * The time, when the last jiffy update happened. Protected by xtime_lock.
36 static ktime_t last_jiffies_update;
38 struct tick_sched *tick_get_tick_sched(int cpu)
40 return &per_cpu(tick_cpu_sched, cpu);
44 * Must be called with interrupts disabled !
46 static void tick_do_update_jiffies64(ktime_t now)
48 unsigned long ticks = 0;
49 ktime_t delta;
52 * Do a quick check without holding xtime_lock:
54 delta = ktime_sub(now, last_jiffies_update);
55 if (delta.tv64 < tick_period.tv64)
56 return;
58 /* Reevalute with xtime_lock held */
59 write_seqlock(&xtime_lock);
61 delta = ktime_sub(now, last_jiffies_update);
62 if (delta.tv64 >= tick_period.tv64) {
64 delta = ktime_sub(delta, tick_period);
65 last_jiffies_update = ktime_add(last_jiffies_update,
66 tick_period);
68 /* Slow path for long timeouts */
69 if (unlikely(delta.tv64 >= tick_period.tv64)) {
70 s64 incr = ktime_to_ns(tick_period);
72 ticks = ktime_divns(delta, incr);
74 last_jiffies_update = ktime_add_ns(last_jiffies_update,
75 incr * ticks);
77 do_timer(++ticks);
79 /* Keep the tick_next_period variable up to date */
80 tick_next_period = ktime_add(last_jiffies_update, tick_period);
82 write_sequnlock(&xtime_lock);
86 * Initialize and return retrieve the jiffies update.
88 static ktime_t tick_init_jiffy_update(void)
90 ktime_t period;
92 write_seqlock(&xtime_lock);
93 /* Did we start the jiffies update yet ? */
94 if (last_jiffies_update.tv64 == 0)
95 last_jiffies_update = tick_next_period;
96 period = last_jiffies_update;
97 write_sequnlock(&xtime_lock);
98 return period;
102 * NOHZ - aka dynamic tick functionality
104 #ifdef CONFIG_NO_HZ
106 * NO HZ enabled ?
108 static int tick_nohz_enabled __read_mostly = 1;
111 * Enable / Disable tickless mode
113 static int __init setup_tick_nohz(char *str)
115 if (!strcmp(str, "off"))
116 tick_nohz_enabled = 0;
117 else if (!strcmp(str, "on"))
118 tick_nohz_enabled = 1;
119 else
120 return 0;
121 return 1;
124 __setup("nohz=", setup_tick_nohz);
127 * tick_nohz_update_jiffies - update jiffies when idle was interrupted
129 * Called from interrupt entry when the CPU was idle
131 * In case the sched_tick was stopped on this CPU, we have to check if jiffies
132 * must be updated. Otherwise an interrupt handler could use a stale jiffy
133 * value. We do this unconditionally on any cpu, as we don't know whether the
134 * cpu, which has the update task assigned is in a long sleep.
136 static void tick_nohz_update_jiffies(ktime_t now)
138 int cpu = smp_processor_id();
139 struct tick_sched *ts = &per_cpu(tick_cpu_sched, cpu);
140 unsigned long flags;
142 cpumask_clear_cpu(cpu, nohz_cpu_mask);
143 ts->idle_waketime = now;
145 local_irq_save(flags);
146 tick_do_update_jiffies64(now);
147 local_irq_restore(flags);
149 touch_softlockup_watchdog();
153 * Updates the per cpu time idle statistics counters
155 static void
156 update_ts_time_stats(int cpu, struct tick_sched *ts, ktime_t now, u64 *last_update_time)
158 ktime_t delta;
160 if (ts->idle_active) {
161 delta = ktime_sub(now, ts->idle_entrytime);
162 if (nr_iowait_cpu(cpu) > 0)
163 ts->iowait_sleeptime = ktime_add(ts->iowait_sleeptime, delta);
164 else
165 ts->idle_sleeptime = ktime_add(ts->idle_sleeptime, delta);
166 ts->idle_entrytime = now;
169 if (last_update_time)
170 *last_update_time = ktime_to_us(now);
174 static void tick_nohz_stop_idle(int cpu, ktime_t now)
176 struct tick_sched *ts = &per_cpu(tick_cpu_sched, cpu);
178 update_ts_time_stats(cpu, ts, now, NULL);
179 ts->idle_active = 0;
181 sched_clock_idle_wakeup_event(0);
184 static ktime_t tick_nohz_start_idle(int cpu, struct tick_sched *ts)
186 ktime_t now;
188 now = ktime_get();
190 update_ts_time_stats(cpu, ts, now, NULL);
192 ts->idle_entrytime = now;
193 ts->idle_active = 1;
194 sched_clock_idle_sleep_event();
195 return now;
199 * get_cpu_idle_time_us - get the total idle time of a cpu
200 * @cpu: CPU number to query
201 * @last_update_time: variable to store update time in. Do not update
202 * counters if NULL.
204 * Return the cummulative idle time (since boot) for a given
205 * CPU, in microseconds.
207 * This time is measured via accounting rather than sampling,
208 * and is as accurate as ktime_get() is.
210 * This function returns -1 if NOHZ is not enabled.
212 u64 get_cpu_idle_time_us(int cpu, u64 *last_update_time)
214 struct tick_sched *ts = &per_cpu(tick_cpu_sched, cpu);
215 ktime_t now, idle;
217 if (!tick_nohz_enabled)
218 return -1;
220 now = ktime_get();
221 if (last_update_time) {
222 update_ts_time_stats(cpu, ts, now, last_update_time);
223 idle = ts->idle_sleeptime;
224 } else {
225 if (ts->idle_active && !nr_iowait_cpu(cpu)) {
226 ktime_t delta = ktime_sub(now, ts->idle_entrytime);
228 idle = ktime_add(ts->idle_sleeptime, delta);
229 } else {
230 idle = ts->idle_sleeptime;
234 return ktime_to_us(idle);
237 EXPORT_SYMBOL_GPL(get_cpu_idle_time_us);
240 * get_cpu_iowait_time_us - get the total iowait time of a cpu
241 * @cpu: CPU number to query
242 * @last_update_time: variable to store update time in. Do not update
243 * counters if NULL.
245 * Return the cummulative iowait time (since boot) for a given
246 * CPU, in microseconds.
248 * This time is measured via accounting rather than sampling,
249 * and is as accurate as ktime_get() is.
251 * This function returns -1 if NOHZ is not enabled.
253 u64 get_cpu_iowait_time_us(int cpu, u64 *last_update_time)
255 struct tick_sched *ts = &per_cpu(tick_cpu_sched, cpu);
256 ktime_t now, iowait;
258 if (!tick_nohz_enabled)
259 return -1;
261 now = ktime_get();
262 if (last_update_time) {
263 update_ts_time_stats(cpu, ts, now, last_update_time);
264 iowait = ts->iowait_sleeptime;
265 } else {
266 if (ts->idle_active && nr_iowait_cpu(cpu) > 0) {
267 ktime_t delta = ktime_sub(now, ts->idle_entrytime);
269 iowait = ktime_add(ts->iowait_sleeptime, delta);
270 } else {
271 iowait = ts->iowait_sleeptime;
275 return ktime_to_us(iowait);
277 EXPORT_SYMBOL_GPL(get_cpu_iowait_time_us);
280 * tick_nohz_stop_sched_tick - stop the idle tick from the idle task
282 * When the next event is more than a tick into the future, stop the idle tick
283 * Called either from the idle loop or from irq_exit() when an idle period was
284 * just interrupted by an interrupt which did not cause a reschedule.
286 void tick_nohz_stop_sched_tick(int inidle)
288 unsigned long seq, last_jiffies, next_jiffies, delta_jiffies, flags;
289 struct tick_sched *ts;
290 ktime_t last_update, expires, now;
291 struct clock_event_device *dev = __get_cpu_var(tick_cpu_device).evtdev;
292 u64 time_delta;
293 int cpu;
295 local_irq_save(flags);
297 cpu = smp_processor_id();
298 ts = &per_cpu(tick_cpu_sched, cpu);
301 * Call to tick_nohz_start_idle stops the last_update_time from being
302 * updated. Thus, it must not be called in the event we are called from
303 * irq_exit() with the prior state different than idle.
305 if (!inidle && !ts->inidle)
306 goto end;
309 * Set ts->inidle unconditionally. Even if the system did not
310 * switch to NOHZ mode the cpu frequency governers rely on the
311 * update of the idle time accounting in tick_nohz_start_idle().
313 ts->inidle = 1;
315 now = tick_nohz_start_idle(cpu, ts);
318 * If this cpu is offline and it is the one which updates
319 * jiffies, then give up the assignment and let it be taken by
320 * the cpu which runs the tick timer next. If we don't drop
321 * this here the jiffies might be stale and do_timer() never
322 * invoked.
324 if (unlikely(!cpu_online(cpu))) {
325 if (cpu == tick_do_timer_cpu)
326 tick_do_timer_cpu = TICK_DO_TIMER_NONE;
329 if (unlikely(ts->nohz_mode == NOHZ_MODE_INACTIVE))
330 goto end;
332 if (need_resched())
333 goto end;
335 if (unlikely(local_softirq_pending() && cpu_online(cpu))) {
336 static int ratelimit;
338 if (ratelimit < 10) {
339 printk(KERN_ERR "NOHZ: local_softirq_pending %02x\n",
340 (unsigned int) local_softirq_pending());
341 ratelimit++;
343 goto end;
346 ts->idle_calls++;
347 /* Read jiffies and the time when jiffies were updated last */
348 do {
349 seq = read_seqbegin(&xtime_lock);
350 last_update = last_jiffies_update;
351 last_jiffies = jiffies;
352 time_delta = timekeeping_max_deferment();
353 } while (read_seqretry(&xtime_lock, seq));
355 if (rcu_needs_cpu(cpu) || printk_needs_cpu(cpu) ||
356 arch_needs_cpu(cpu)) {
357 next_jiffies = last_jiffies + 1;
358 delta_jiffies = 1;
359 } else {
360 /* Get the next timer wheel timer */
361 next_jiffies = get_next_timer_interrupt(last_jiffies);
362 delta_jiffies = next_jiffies - last_jiffies;
365 * Do not stop the tick, if we are only one off
366 * or if the cpu is required for rcu
368 if (!ts->tick_stopped && delta_jiffies == 1)
369 goto out;
371 /* Schedule the tick, if we are at least one jiffie off */
372 if ((long)delta_jiffies >= 1) {
375 * If this cpu is the one which updates jiffies, then
376 * give up the assignment and let it be taken by the
377 * cpu which runs the tick timer next, which might be
378 * this cpu as well. If we don't drop this here the
379 * jiffies might be stale and do_timer() never
380 * invoked. Keep track of the fact that it was the one
381 * which had the do_timer() duty last. If this cpu is
382 * the one which had the do_timer() duty last, we
383 * limit the sleep time to the timekeeping
384 * max_deferement value which we retrieved
385 * above. Otherwise we can sleep as long as we want.
387 if (cpu == tick_do_timer_cpu) {
388 tick_do_timer_cpu = TICK_DO_TIMER_NONE;
389 ts->do_timer_last = 1;
390 } else if (tick_do_timer_cpu != TICK_DO_TIMER_NONE) {
391 time_delta = KTIME_MAX;
392 ts->do_timer_last = 0;
393 } else if (!ts->do_timer_last) {
394 time_delta = KTIME_MAX;
398 * calculate the expiry time for the next timer wheel
399 * timer. delta_jiffies >= NEXT_TIMER_MAX_DELTA signals
400 * that there is no timer pending or at least extremely
401 * far into the future (12 days for HZ=1000). In this
402 * case we set the expiry to the end of time.
404 if (likely(delta_jiffies < NEXT_TIMER_MAX_DELTA)) {
406 * Calculate the time delta for the next timer event.
407 * If the time delta exceeds the maximum time delta
408 * permitted by the current clocksource then adjust
409 * the time delta accordingly to ensure the
410 * clocksource does not wrap.
412 time_delta = min_t(u64, time_delta,
413 tick_period.tv64 * delta_jiffies);
416 if (time_delta < KTIME_MAX)
417 expires = ktime_add_ns(last_update, time_delta);
418 else
419 expires.tv64 = KTIME_MAX;
421 if (delta_jiffies > 1)
422 cpumask_set_cpu(cpu, nohz_cpu_mask);
424 /* Skip reprogram of event if its not changed */
425 if (ts->tick_stopped && ktime_equal(expires, dev->next_event))
426 goto out;
429 * nohz_stop_sched_tick can be called several times before
430 * the nohz_restart_sched_tick is called. This happens when
431 * interrupts arrive which do not cause a reschedule. In the
432 * first call we save the current tick time, so we can restart
433 * the scheduler tick in nohz_restart_sched_tick.
435 if (!ts->tick_stopped) {
436 select_nohz_load_balancer(1);
438 ts->idle_tick = hrtimer_get_expires(&ts->sched_timer);
439 ts->tick_stopped = 1;
440 ts->idle_jiffies = last_jiffies;
441 rcu_enter_nohz();
444 ts->idle_sleeps++;
446 /* Mark expires */
447 ts->idle_expires = expires;
450 * If the expiration time == KTIME_MAX, then
451 * in this case we simply stop the tick timer.
453 if (unlikely(expires.tv64 == KTIME_MAX)) {
454 if (ts->nohz_mode == NOHZ_MODE_HIGHRES)
455 hrtimer_cancel(&ts->sched_timer);
456 goto out;
459 if (ts->nohz_mode == NOHZ_MODE_HIGHRES) {
460 hrtimer_start(&ts->sched_timer, expires,
461 HRTIMER_MODE_ABS_PINNED);
462 /* Check, if the timer was already in the past */
463 if (hrtimer_active(&ts->sched_timer))
464 goto out;
465 } else if (!tick_program_event(expires, 0))
466 goto out;
468 * We are past the event already. So we crossed a
469 * jiffie boundary. Update jiffies and raise the
470 * softirq.
472 tick_do_update_jiffies64(ktime_get());
473 cpumask_clear_cpu(cpu, nohz_cpu_mask);
475 raise_softirq_irqoff(TIMER_SOFTIRQ);
476 out:
477 ts->next_jiffies = next_jiffies;
478 ts->last_jiffies = last_jiffies;
479 ts->sleep_length = ktime_sub(dev->next_event, now);
480 end:
481 local_irq_restore(flags);
485 * tick_nohz_get_sleep_length - return the length of the current sleep
487 * Called from power state control code with interrupts disabled
489 ktime_t tick_nohz_get_sleep_length(void)
491 struct tick_sched *ts = &__get_cpu_var(tick_cpu_sched);
493 return ts->sleep_length;
496 static void tick_nohz_restart(struct tick_sched *ts, ktime_t now)
498 hrtimer_cancel(&ts->sched_timer);
499 hrtimer_set_expires(&ts->sched_timer, ts->idle_tick);
501 while (1) {
502 /* Forward the time to expire in the future */
503 hrtimer_forward(&ts->sched_timer, now, tick_period);
505 if (ts->nohz_mode == NOHZ_MODE_HIGHRES) {
506 hrtimer_start_expires(&ts->sched_timer,
507 HRTIMER_MODE_ABS_PINNED);
508 /* Check, if the timer was already in the past */
509 if (hrtimer_active(&ts->sched_timer))
510 break;
511 } else {
512 if (!tick_program_event(
513 hrtimer_get_expires(&ts->sched_timer), 0))
514 break;
516 /* Update jiffies and reread time */
517 tick_do_update_jiffies64(now);
518 now = ktime_get();
523 * tick_nohz_restart_sched_tick - restart the idle tick from the idle task
525 * Restart the idle tick when the CPU is woken up from idle
527 void tick_nohz_restart_sched_tick(void)
529 int cpu = smp_processor_id();
530 struct tick_sched *ts = &per_cpu(tick_cpu_sched, cpu);
531 #ifndef CONFIG_VIRT_CPU_ACCOUNTING
532 unsigned long ticks;
533 #endif
534 ktime_t now;
536 local_irq_disable();
537 if (ts->idle_active || (ts->inidle && ts->tick_stopped))
538 now = ktime_get();
540 if (ts->idle_active)
541 tick_nohz_stop_idle(cpu, now);
543 if (!ts->inidle || !ts->tick_stopped) {
544 ts->inidle = 0;
545 local_irq_enable();
546 return;
549 ts->inidle = 0;
551 rcu_exit_nohz();
553 /* Update jiffies first */
554 select_nohz_load_balancer(0);
555 tick_do_update_jiffies64(now);
556 cpumask_clear_cpu(cpu, nohz_cpu_mask);
558 #ifndef CONFIG_VIRT_CPU_ACCOUNTING
560 * We stopped the tick in idle. Update process times would miss the
561 * time we slept as update_process_times does only a 1 tick
562 * accounting. Enforce that this is accounted to idle !
564 ticks = jiffies - ts->idle_jiffies;
566 * We might be one off. Do not randomly account a huge number of ticks!
568 if (ticks && ticks < LONG_MAX)
569 account_idle_ticks(ticks);
570 #endif
572 touch_softlockup_watchdog();
574 * Cancel the scheduled timer and restore the tick
576 ts->tick_stopped = 0;
577 ts->idle_exittime = now;
579 tick_nohz_restart(ts, now);
581 local_irq_enable();
584 static int tick_nohz_reprogram(struct tick_sched *ts, ktime_t now)
586 hrtimer_forward(&ts->sched_timer, now, tick_period);
587 return tick_program_event(hrtimer_get_expires(&ts->sched_timer), 0);
591 * The nohz low res interrupt handler
593 static void tick_nohz_handler(struct clock_event_device *dev)
595 struct tick_sched *ts = &__get_cpu_var(tick_cpu_sched);
596 struct pt_regs *regs = get_irq_regs();
597 int cpu = smp_processor_id();
598 ktime_t now = ktime_get();
600 dev->next_event.tv64 = KTIME_MAX;
603 * Check if the do_timer duty was dropped. We don't care about
604 * concurrency: This happens only when the cpu in charge went
605 * into a long sleep. If two cpus happen to assign themself to
606 * this duty, then the jiffies update is still serialized by
607 * xtime_lock.
609 if (unlikely(tick_do_timer_cpu == TICK_DO_TIMER_NONE))
610 tick_do_timer_cpu = cpu;
612 /* Check, if the jiffies need an update */
613 if (tick_do_timer_cpu == cpu)
614 tick_do_update_jiffies64(now);
617 * When we are idle and the tick is stopped, we have to touch
618 * the watchdog as we might not schedule for a really long
619 * time. This happens on complete idle SMP systems while
620 * waiting on the login prompt. We also increment the "start
621 * of idle" jiffy stamp so the idle accounting adjustment we
622 * do when we go busy again does not account too much ticks.
624 if (ts->tick_stopped) {
625 touch_softlockup_watchdog();
626 ts->idle_jiffies++;
629 update_process_times(user_mode(regs));
630 profile_tick(CPU_PROFILING);
632 while (tick_nohz_reprogram(ts, now)) {
633 now = ktime_get();
634 tick_do_update_jiffies64(now);
639 * tick_nohz_switch_to_nohz - switch to nohz mode
641 static void tick_nohz_switch_to_nohz(void)
643 struct tick_sched *ts = &__get_cpu_var(tick_cpu_sched);
644 ktime_t next;
646 if (!tick_nohz_enabled)
647 return;
649 local_irq_disable();
650 if (tick_switch_to_oneshot(tick_nohz_handler)) {
651 local_irq_enable();
652 return;
655 ts->nohz_mode = NOHZ_MODE_LOWRES;
658 * Recycle the hrtimer in ts, so we can share the
659 * hrtimer_forward with the highres code.
661 hrtimer_init(&ts->sched_timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
662 /* Get the next period */
663 next = tick_init_jiffy_update();
665 for (;;) {
666 hrtimer_set_expires(&ts->sched_timer, next);
667 if (!tick_program_event(next, 0))
668 break;
669 next = ktime_add(next, tick_period);
671 local_irq_enable();
673 printk(KERN_INFO "Switched to NOHz mode on CPU #%d\n", smp_processor_id());
677 * When NOHZ is enabled and the tick is stopped, we need to kick the
678 * tick timer from irq_enter() so that the jiffies update is kept
679 * alive during long running softirqs. That's ugly as hell, but
680 * correctness is key even if we need to fix the offending softirq in
681 * the first place.
683 * Note, this is different to tick_nohz_restart. We just kick the
684 * timer and do not touch the other magic bits which need to be done
685 * when idle is left.
687 static void tick_nohz_kick_tick(int cpu, ktime_t now)
689 #if 0
690 /* Switch back to 2.6.27 behaviour */
692 struct tick_sched *ts = &per_cpu(tick_cpu_sched, cpu);
693 ktime_t delta;
696 * Do not touch the tick device, when the next expiry is either
697 * already reached or less/equal than the tick period.
699 delta = ktime_sub(hrtimer_get_expires(&ts->sched_timer), now);
700 if (delta.tv64 <= tick_period.tv64)
701 return;
703 tick_nohz_restart(ts, now);
704 #endif
707 static inline void tick_check_nohz(int cpu)
709 struct tick_sched *ts = &per_cpu(tick_cpu_sched, cpu);
710 ktime_t now;
712 if (!ts->idle_active && !ts->tick_stopped)
713 return;
714 now = ktime_get();
715 if (ts->idle_active)
716 tick_nohz_stop_idle(cpu, now);
717 if (ts->tick_stopped) {
718 tick_nohz_update_jiffies(now);
719 tick_nohz_kick_tick(cpu, now);
723 #else
725 static inline void tick_nohz_switch_to_nohz(void) { }
726 static inline void tick_check_nohz(int cpu) { }
728 #endif /* NO_HZ */
731 * Called from irq_enter to notify about the possible interruption of idle()
733 void tick_check_idle(int cpu)
735 tick_check_oneshot_broadcast(cpu);
736 tick_check_nohz(cpu);
740 * High resolution timer specific code
742 #ifdef CONFIG_HIGH_RES_TIMERS
744 * We rearm the timer until we get disabled by the idle code.
745 * Called with interrupts disabled and timer->base->cpu_base->lock held.
747 static enum hrtimer_restart tick_sched_timer(struct hrtimer *timer)
749 struct tick_sched *ts =
750 container_of(timer, struct tick_sched, sched_timer);
751 struct pt_regs *regs = get_irq_regs();
752 ktime_t now = ktime_get();
753 int cpu = smp_processor_id();
755 #ifdef CONFIG_NO_HZ
757 * Check if the do_timer duty was dropped. We don't care about
758 * concurrency: This happens only when the cpu in charge went
759 * into a long sleep. If two cpus happen to assign themself to
760 * this duty, then the jiffies update is still serialized by
761 * xtime_lock.
763 if (unlikely(tick_do_timer_cpu == TICK_DO_TIMER_NONE))
764 tick_do_timer_cpu = cpu;
765 #endif
767 /* Check, if the jiffies need an update */
768 if (tick_do_timer_cpu == cpu)
769 tick_do_update_jiffies64(now);
772 * Do not call, when we are not in irq context and have
773 * no valid regs pointer
775 if (regs) {
777 * When we are idle and the tick is stopped, we have to touch
778 * the watchdog as we might not schedule for a really long
779 * time. This happens on complete idle SMP systems while
780 * waiting on the login prompt. We also increment the "start of
781 * idle" jiffy stamp so the idle accounting adjustment we do
782 * when we go busy again does not account too much ticks.
784 if (ts->tick_stopped) {
785 touch_softlockup_watchdog();
786 ts->idle_jiffies++;
788 update_process_times(user_mode(regs));
789 profile_tick(CPU_PROFILING);
792 hrtimer_forward(timer, now, tick_period);
794 return HRTIMER_RESTART;
798 * tick_setup_sched_timer - setup the tick emulation timer
800 void tick_setup_sched_timer(void)
802 struct tick_sched *ts = &__get_cpu_var(tick_cpu_sched);
803 ktime_t now = ktime_get();
806 * Emulate tick processing via per-CPU hrtimers:
808 hrtimer_init(&ts->sched_timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
809 ts->sched_timer.function = tick_sched_timer;
811 /* Get the next period (per cpu) */
812 hrtimer_set_expires(&ts->sched_timer, tick_init_jiffy_update());
814 for (;;) {
815 hrtimer_forward(&ts->sched_timer, now, tick_period);
816 hrtimer_start_expires(&ts->sched_timer,
817 HRTIMER_MODE_ABS_PINNED);
818 /* Check, if the timer was already in the past */
819 if (hrtimer_active(&ts->sched_timer))
820 break;
821 now = ktime_get();
824 #ifdef CONFIG_NO_HZ
825 if (tick_nohz_enabled) {
826 ts->nohz_mode = NOHZ_MODE_HIGHRES;
827 printk(KERN_INFO "Switched to NOHz mode on CPU #%d\n", smp_processor_id());
829 #endif
831 #endif /* HIGH_RES_TIMERS */
833 #if defined CONFIG_NO_HZ || defined CONFIG_HIGH_RES_TIMERS
834 void tick_cancel_sched_timer(int cpu)
836 struct tick_sched *ts = &per_cpu(tick_cpu_sched, cpu);
838 # ifdef CONFIG_HIGH_RES_TIMERS
839 if (ts->sched_timer.base)
840 hrtimer_cancel(&ts->sched_timer);
841 # endif
843 ts->nohz_mode = NOHZ_MODE_INACTIVE;
845 #endif
848 * Async notification about clocksource changes
850 void tick_clock_notify(void)
852 int cpu;
854 for_each_possible_cpu(cpu)
855 set_bit(0, &per_cpu(tick_cpu_sched, cpu).check_clocks);
859 * Async notification about clock event changes
861 void tick_oneshot_notify(void)
863 struct tick_sched *ts = &__get_cpu_var(tick_cpu_sched);
865 set_bit(0, &ts->check_clocks);
869 * Check, if a change happened, which makes oneshot possible.
871 * Called cyclic from the hrtimer softirq (driven by the timer
872 * softirq) allow_nohz signals, that we can switch into low-res nohz
873 * mode, because high resolution timers are disabled (either compile
874 * or runtime).
876 int tick_check_oneshot_change(int allow_nohz)
878 struct tick_sched *ts = &__get_cpu_var(tick_cpu_sched);
880 if (!test_and_clear_bit(0, &ts->check_clocks))
881 return 0;
883 if (ts->nohz_mode != NOHZ_MODE_INACTIVE)
884 return 0;
886 if (!timekeeping_valid_for_hres() || !tick_is_oneshot_available())
887 return 0;
889 if (!allow_nohz)
890 return 1;
892 tick_nohz_switch_to_nohz();
893 return 0;