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>
23 #ifdef CONFIG_MTK_SCHED_RQAVG_US
24 #include <linux/rq_stats.h>
26 #include <linux/irq_work.h>
27 #include <linux/posix-timers.h>
28 #include <linux/perf_event.h>
30 #include <asm/irq_regs.h>
32 #include "tick-internal.h"
34 #include <trace/events/timer.h>
36 #ifdef CONFIG_MTK_SCHED_RQAVG_US
37 struct rq_data rq_info
;
38 #ifdef CONFIG_MTK_SCHED_RQAVG_US_ENABLE_WQ
39 struct workqueue_struct
*rq_wq
;
44 #ifdef CONFIG_MT_LOAD_BALANCE_PROFILER
45 #include <mtlbprof/mtlbprof.h>
49 * Per cpu nohz control structure
51 DEFINE_PER_CPU(struct tick_sched
, tick_cpu_sched
);
54 * The time, when the last jiffy update happened. Protected by jiffies_lock.
56 static ktime_t last_jiffies_update
;
58 struct tick_sched
*tick_get_tick_sched(int cpu
)
60 return &per_cpu(tick_cpu_sched
, cpu
);
64 * Must be called with interrupts disabled !
66 static void tick_do_update_jiffies64(ktime_t now
)
68 unsigned long ticks
= 0;
72 * Do a quick check without holding jiffies_lock:
74 delta
= ktime_sub(now
, last_jiffies_update
);
75 if (delta
.tv64
< tick_period
.tv64
)
78 /* Reevalute with jiffies_lock held */
79 write_seqlock(&jiffies_lock
);
81 delta
= ktime_sub(now
, last_jiffies_update
);
82 if (delta
.tv64
>= tick_period
.tv64
) {
84 delta
= ktime_sub(delta
, tick_period
);
85 last_jiffies_update
= ktime_add(last_jiffies_update
,
88 /* Slow path for long timeouts */
89 if (unlikely(delta
.tv64
>= tick_period
.tv64
)) {
90 s64 incr
= ktime_to_ns(tick_period
);
92 ticks
= ktime_divns(delta
, incr
);
94 last_jiffies_update
= ktime_add_ns(last_jiffies_update
,
99 /* Keep the tick_next_period variable up to date */
100 tick_next_period
= ktime_add(last_jiffies_update
, tick_period
);
102 write_sequnlock(&jiffies_lock
);
106 * Initialize and return retrieve the jiffies update.
108 static ktime_t
tick_init_jiffy_update(void)
112 write_seqlock(&jiffies_lock
);
113 /* Did we start the jiffies update yet ? */
114 if (last_jiffies_update
.tv64
== 0)
115 last_jiffies_update
= tick_next_period
;
116 period
= last_jiffies_update
;
117 write_sequnlock(&jiffies_lock
);
122 static void tick_sched_do_timer(ktime_t now
)
124 int cpu
= smp_processor_id();
126 #ifdef CONFIG_NO_HZ_COMMON
128 * Check if the do_timer duty was dropped. We don't care about
129 * concurrency: This happens only when the cpu in charge went
130 * into a long sleep. If two cpus happen to assign themself to
131 * this duty, then the jiffies update is still serialized by
134 if (unlikely(tick_do_timer_cpu
== TICK_DO_TIMER_NONE
)
135 && !tick_nohz_full_cpu(cpu
))
136 tick_do_timer_cpu
= cpu
;
139 /* Check, if the jiffies need an update */
140 if (tick_do_timer_cpu
== cpu
)
141 tick_do_update_jiffies64(now
);
144 static void tick_sched_handle(struct tick_sched
*ts
, struct pt_regs
*regs
)
146 #ifdef CONFIG_NO_HZ_COMMON
148 * When we are idle and the tick is stopped, we have to touch
149 * the watchdog as we might not schedule for a really long
150 * time. This happens on complete idle SMP systems while
151 * waiting on the login prompt. We also increment the "start of
152 * idle" jiffy stamp so the idle accounting adjustment we do
153 * when we go busy again does not account too much ticks.
155 if (ts
->tick_stopped
) {
156 touch_softlockup_watchdog();
157 if (is_idle_task(current
))
161 update_process_times(user_mode(regs
));
162 profile_tick(CPU_PROFILING
);
165 #ifdef CONFIG_NO_HZ_FULL
166 static cpumask_var_t nohz_full_mask
;
167 bool have_nohz_full_mask
;
169 static bool can_stop_full_tick(void)
171 WARN_ON_ONCE(!irqs_disabled());
173 if (!sched_can_stop_tick()) {
174 trace_tick_stop(0, "more than 1 task in runqueue\n");
178 if (!posix_cpu_timers_can_stop_tick(current
)) {
179 trace_tick_stop(0, "posix timers running\n");
183 if (!perf_event_can_stop_tick()) {
184 trace_tick_stop(0, "perf events running\n");
188 /* sched_clock_tick() needs us? */
189 #ifdef CONFIG_HAVE_UNSTABLE_SCHED_CLOCK
191 * TODO: kick full dynticks CPUs when
192 * sched_clock_stable is set.
194 if (!sched_clock_stable
) {
195 trace_tick_stop(0, "unstable sched clock\n");
203 static void tick_nohz_restart_sched_tick(struct tick_sched
*ts
, ktime_t now
);
206 * Re-evaluate the need for the tick on the current CPU
207 * and restart it if necessary.
209 void tick_nohz_full_check(void)
211 struct tick_sched
*ts
= &__get_cpu_var(tick_cpu_sched
);
213 if (tick_nohz_full_cpu(smp_processor_id())) {
214 if (ts
->tick_stopped
&& !is_idle_task(current
)) {
215 if (!can_stop_full_tick())
216 tick_nohz_restart_sched_tick(ts
, ktime_get());
221 static void nohz_full_kick_work_func(struct irq_work
*work
)
223 tick_nohz_full_check();
226 static DEFINE_PER_CPU(struct irq_work
, nohz_full_kick_work
) = {
227 .func
= nohz_full_kick_work_func
,
231 * Kick the current CPU if it's full dynticks in order to force it to
232 * re-evaluate its dependency on the tick and restart it if necessary.
234 void tick_nohz_full_kick(void)
236 if (tick_nohz_full_cpu(smp_processor_id()))
237 irq_work_queue(&__get_cpu_var(nohz_full_kick_work
));
240 static void nohz_full_kick_ipi(void *info
)
242 tick_nohz_full_check();
246 * Kick all full dynticks CPUs in order to force these to re-evaluate
247 * their dependency on the tick and restart it if necessary.
249 void tick_nohz_full_kick_all(void)
251 if (!have_nohz_full_mask
)
255 smp_call_function_many(nohz_full_mask
,
256 nohz_full_kick_ipi
, NULL
, false);
261 * Re-evaluate the need for the tick as we switch the current task.
262 * It might need the tick due to per task/process properties:
263 * perf events, posix cpu timers, ...
265 void tick_nohz_task_switch(struct task_struct
*tsk
)
269 local_irq_save(flags
);
271 if (!tick_nohz_full_cpu(smp_processor_id()))
274 if (tick_nohz_tick_stopped() && !can_stop_full_tick())
275 tick_nohz_full_kick();
278 local_irq_restore(flags
);
281 int tick_nohz_full_cpu(int cpu
)
283 if (!have_nohz_full_mask
)
286 return cpumask_test_cpu(cpu
, nohz_full_mask
);
289 /* Parse the boot-time nohz CPU list from the kernel parameters. */
290 static int __init
tick_nohz_full_setup(char *str
)
294 alloc_bootmem_cpumask_var(&nohz_full_mask
);
295 if (cpulist_parse(str
, nohz_full_mask
) < 0) {
296 pr_warning("NOHZ: Incorrect nohz_full cpumask\n");
300 cpu
= smp_processor_id();
301 if (cpumask_test_cpu(cpu
, nohz_full_mask
)) {
302 pr_warning("NO_HZ: Clearing %d from nohz_full range for timekeeping\n", cpu
);
303 cpumask_clear_cpu(cpu
, nohz_full_mask
);
305 have_nohz_full_mask
= true;
309 __setup("nohz_full=", tick_nohz_full_setup
);
311 static int __cpuinit
tick_nohz_cpu_down_callback(struct notifier_block
*nfb
,
312 unsigned long action
,
315 unsigned int cpu
= (unsigned long)hcpu
;
317 switch (action
& ~CPU_TASKS_FROZEN
) {
318 case CPU_DOWN_PREPARE
:
320 * If we handle the timekeeping duty for full dynticks CPUs,
321 * we can't safely shutdown that CPU.
323 if (have_nohz_full_mask
&& tick_do_timer_cpu
== cpu
)
331 * Worst case string length in chunks of CPU range seems 2 steps
332 * separations: 0,2,4,6,...
333 * This is NR_CPUS + sizeof('\0')
335 static char __initdata nohz_full_buf
[NR_CPUS
+ 1];
337 static int tick_nohz_init_all(void)
341 #ifdef CONFIG_NO_HZ_FULL_ALL
342 if (!alloc_cpumask_var(&nohz_full_mask
, GFP_KERNEL
)) {
343 pr_err("NO_HZ: Can't allocate full dynticks cpumask\n");
347 cpumask_setall(nohz_full_mask
);
348 cpumask_clear_cpu(smp_processor_id(), nohz_full_mask
);
349 have_nohz_full_mask
= true;
354 void __init
tick_nohz_init(void)
358 if (!have_nohz_full_mask
) {
359 if (tick_nohz_init_all() < 0)
363 cpu_notifier(tick_nohz_cpu_down_callback
, 0);
365 /* Make sure full dynticks CPU are also RCU nocbs */
366 for_each_cpu(cpu
, nohz_full_mask
) {
367 if (!rcu_is_nocb_cpu(cpu
)) {
368 pr_warning("NO_HZ: CPU %d is not RCU nocb: "
369 "cleared from nohz_full range", cpu
);
370 cpumask_clear_cpu(cpu
, nohz_full_mask
);
374 cpulist_scnprintf(nohz_full_buf
, sizeof(nohz_full_buf
), nohz_full_mask
);
375 pr_info("NO_HZ: Full dynticks CPUs: %s.\n", nohz_full_buf
);
378 #define have_nohz_full_mask (0)
382 * NOHZ - aka dynamic tick functionality
384 #ifdef CONFIG_NO_HZ_COMMON
388 int tick_nohz_enabled __read_mostly
= 1;
391 * Enable / Disable tickless mode
393 static int __init
setup_tick_nohz(char *str
)
395 if (!strcmp(str
, "off"))
396 tick_nohz_enabled
= 0;
397 else if (!strcmp(str
, "on"))
398 tick_nohz_enabled
= 1;
404 __setup("nohz=", setup_tick_nohz
);
407 * tick_nohz_update_jiffies - update jiffies when idle was interrupted
409 * Called from interrupt entry when the CPU was idle
411 * In case the sched_tick was stopped on this CPU, we have to check if jiffies
412 * must be updated. Otherwise an interrupt handler could use a stale jiffy
413 * value. We do this unconditionally on any cpu, as we don't know whether the
414 * cpu, which has the update task assigned is in a long sleep.
416 static void tick_nohz_update_jiffies(ktime_t now
)
418 int cpu
= smp_processor_id();
419 struct tick_sched
*ts
= &per_cpu(tick_cpu_sched
, cpu
);
422 ts
->idle_waketime
= now
;
424 local_irq_save(flags
);
425 tick_do_update_jiffies64(now
);
426 local_irq_restore(flags
);
428 touch_softlockup_watchdog();
432 * Updates the per cpu time idle statistics counters
435 update_ts_time_stats(int cpu
, struct tick_sched
*ts
, ktime_t now
, u64
*last_update_time
)
439 if (ts
->idle_active
) {
440 delta
= ktime_sub(now
, ts
->idle_entrytime
);
441 if (nr_iowait_cpu(cpu
) > 0)
442 ts
->iowait_sleeptime
= ktime_add(ts
->iowait_sleeptime
, delta
);
444 ts
->idle_sleeptime
= ktime_add(ts
->idle_sleeptime
, delta
);
445 ts
->idle_entrytime
= now
;
448 if (last_update_time
)
449 *last_update_time
= ktime_to_us(now
);
453 static void tick_nohz_stop_idle(int cpu
, ktime_t now
)
455 struct tick_sched
*ts
= &per_cpu(tick_cpu_sched
, cpu
);
457 update_ts_time_stats(cpu
, ts
, now
, NULL
);
459 #ifdef CONFIG_MT_LOAD_BALANCE_PROFILER
460 mt_lbprof_update_state(cpu
, MT_LBPROF_NO_TASK_STATE
);
463 sched_clock_idle_wakeup_event(0);
466 static ktime_t
tick_nohz_start_idle(int cpu
, struct tick_sched
*ts
)
468 ktime_t now
= ktime_get();
470 ts
->idle_entrytime
= now
;
472 #ifdef CONFIG_MT_LOAD_BALANCE_PROFILER
473 mt_lbprof_update_state(cpu
, MT_LBPROF_NO_TASK_STATE
);
476 sched_clock_idle_sleep_event();
481 * get_cpu_idle_time_us - get the total idle time of a cpu
482 * @cpu: CPU number to query
483 * @last_update_time: variable to store update time in. Do not update
486 * Return the cummulative idle time (since boot) for a given
487 * CPU, in microseconds.
489 * This time is measured via accounting rather than sampling,
490 * and is as accurate as ktime_get() is.
492 * This function returns -1 if NOHZ is not enabled.
494 u64
get_cpu_idle_time_us(int cpu
, u64
*last_update_time
)
496 struct tick_sched
*ts
= &per_cpu(tick_cpu_sched
, cpu
);
499 if (!tick_nohz_enabled
)
503 if (last_update_time
) {
504 update_ts_time_stats(cpu
, ts
, now
, last_update_time
);
505 idle
= ts
->idle_sleeptime
;
507 if (ts
->idle_active
&& !nr_iowait_cpu(cpu
)) {
508 ktime_t delta
= ktime_sub(now
, ts
->idle_entrytime
);
510 idle
= ktime_add(ts
->idle_sleeptime
, delta
);
512 idle
= ts
->idle_sleeptime
;
516 return ktime_to_us(idle
);
519 EXPORT_SYMBOL_GPL(get_cpu_idle_time_us
);
522 * get_cpu_iowait_time_us - get the total iowait time of a cpu
523 * @cpu: CPU number to query
524 * @last_update_time: variable to store update time in. Do not update
527 * Return the cummulative iowait time (since boot) for a given
528 * CPU, in microseconds.
530 * This time is measured via accounting rather than sampling,
531 * and is as accurate as ktime_get() is.
533 * This function returns -1 if NOHZ is not enabled.
535 u64
get_cpu_iowait_time_us(int cpu
, u64
*last_update_time
)
537 struct tick_sched
*ts
= &per_cpu(tick_cpu_sched
, cpu
);
540 if (!tick_nohz_enabled
)
544 if (last_update_time
) {
545 update_ts_time_stats(cpu
, ts
, now
, last_update_time
);
546 iowait
= ts
->iowait_sleeptime
;
548 if (ts
->idle_active
&& nr_iowait_cpu(cpu
) > 0) {
549 ktime_t delta
= ktime_sub(now
, ts
->idle_entrytime
);
551 iowait
= ktime_add(ts
->iowait_sleeptime
, delta
);
553 iowait
= ts
->iowait_sleeptime
;
557 return ktime_to_us(iowait
);
559 EXPORT_SYMBOL_GPL(get_cpu_iowait_time_us
);
561 static ktime_t
tick_nohz_stop_sched_tick(struct tick_sched
*ts
,
562 ktime_t now
, int cpu
)
564 unsigned long seq
, last_jiffies
, next_jiffies
, delta_jiffies
;
565 ktime_t last_update
, expires
, ret
= { .tv64
= 0 };
566 unsigned long rcu_delta_jiffies
;
567 struct clock_event_device
*dev
= __get_cpu_var(tick_cpu_device
).evtdev
;
570 /* Read jiffies and the time when jiffies were updated last */
572 seq
= read_seqbegin(&jiffies_lock
);
573 last_update
= last_jiffies_update
;
574 last_jiffies
= jiffies
;
575 time_delta
= timekeeping_max_deferment();
576 } while (read_seqretry(&jiffies_lock
, seq
));
578 if (rcu_needs_cpu(cpu
, &rcu_delta_jiffies
) ||
579 arch_needs_cpu(cpu
) || irq_work_needs_cpu()) {
580 next_jiffies
= last_jiffies
+ 1;
583 /* Get the next timer wheel timer */
584 next_jiffies
= get_next_timer_interrupt(last_jiffies
);
585 delta_jiffies
= next_jiffies
- last_jiffies
;
586 if (rcu_delta_jiffies
< delta_jiffies
) {
587 next_jiffies
= last_jiffies
+ rcu_delta_jiffies
;
588 delta_jiffies
= rcu_delta_jiffies
;
593 * Do not stop the tick, if we are only one off (or less)
594 * or if the cpu is required for RCU:
596 if (!ts
->tick_stopped
&& delta_jiffies
<= 1)
599 /* Schedule the tick, if we are at least one jiffie off */
600 if ((long)delta_jiffies
>= 1) {
603 * If this cpu is the one which updates jiffies, then
604 * give up the assignment and let it be taken by the
605 * cpu which runs the tick timer next, which might be
606 * this cpu as well. If we don't drop this here the
607 * jiffies might be stale and do_timer() never
608 * invoked. Keep track of the fact that it was the one
609 * which had the do_timer() duty last. If this cpu is
610 * the one which had the do_timer() duty last, we
611 * limit the sleep time to the timekeeping
612 * max_deferement value which we retrieved
613 * above. Otherwise we can sleep as long as we want.
615 if (cpu
== tick_do_timer_cpu
) {
616 tick_do_timer_cpu
= TICK_DO_TIMER_NONE
;
617 ts
->do_timer_last
= 1;
618 } else if (tick_do_timer_cpu
!= TICK_DO_TIMER_NONE
) {
619 time_delta
= KTIME_MAX
;
620 ts
->do_timer_last
= 0;
621 } else if (!ts
->do_timer_last
) {
622 time_delta
= KTIME_MAX
;
625 #ifdef CONFIG_NO_HZ_FULL
627 time_delta
= min(time_delta
,
628 scheduler_tick_max_deferment());
633 * calculate the expiry time for the next timer wheel
634 * timer. delta_jiffies >= NEXT_TIMER_MAX_DELTA signals
635 * that there is no timer pending or at least extremely
636 * far into the future (12 days for HZ=1000). In this
637 * case we set the expiry to the end of time.
639 if (likely(delta_jiffies
< NEXT_TIMER_MAX_DELTA
)) {
641 * Calculate the time delta for the next timer event.
642 * If the time delta exceeds the maximum time delta
643 * permitted by the current clocksource then adjust
644 * the time delta accordingly to ensure the
645 * clocksource does not wrap.
647 time_delta
= min_t(u64
, time_delta
,
648 tick_period
.tv64
* delta_jiffies
);
651 if (time_delta
< KTIME_MAX
)
652 expires
= ktime_add_ns(last_update
, time_delta
);
654 expires
.tv64
= KTIME_MAX
;
656 /* Skip reprogram of event if its not changed */
657 if (ts
->tick_stopped
&& ktime_equal(expires
, dev
->next_event
))
663 * nohz_stop_sched_tick can be called several times before
664 * the nohz_restart_sched_tick is called. This happens when
665 * interrupts arrive which do not cause a reschedule. In the
666 * first call we save the current tick time, so we can restart
667 * the scheduler tick in nohz_restart_sched_tick.
669 if (!ts
->tick_stopped
) {
670 nohz_balance_enter_idle(cpu
);
671 calc_load_enter_idle();
673 ts
->last_tick
= hrtimer_get_expires(&ts
->sched_timer
);
674 ts
->tick_stopped
= 1;
675 trace_tick_stop(1, " ");
679 * If the expiration time == KTIME_MAX, then
680 * in this case we simply stop the tick timer.
682 if (unlikely(expires
.tv64
== KTIME_MAX
)) {
683 if (ts
->nohz_mode
== NOHZ_MODE_HIGHRES
)
684 hrtimer_cancel(&ts
->sched_timer
);
688 if (ts
->nohz_mode
== NOHZ_MODE_HIGHRES
) {
689 hrtimer_start(&ts
->sched_timer
, expires
,
690 HRTIMER_MODE_ABS_PINNED
);
691 /* Check, if the timer was already in the past */
692 if (hrtimer_active(&ts
->sched_timer
))
694 } else if (!tick_program_event(expires
, 0))
697 * We are past the event already. So we crossed a
698 * jiffie boundary. Update jiffies and raise the
701 tick_do_update_jiffies64(ktime_get());
703 raise_softirq_irqoff(TIMER_SOFTIRQ
);
705 ts
->next_jiffies
= next_jiffies
;
706 ts
->last_jiffies
= last_jiffies
;
707 ts
->sleep_length
= ktime_sub(dev
->next_event
, now
);
712 static void tick_nohz_full_stop_tick(struct tick_sched
*ts
)
714 #ifdef CONFIG_NO_HZ_FULL
715 int cpu
= smp_processor_id();
717 if (!tick_nohz_full_cpu(cpu
) || is_idle_task(current
))
720 if (!ts
->tick_stopped
&& ts
->nohz_mode
== NOHZ_MODE_INACTIVE
)
723 if (!can_stop_full_tick())
726 tick_nohz_stop_sched_tick(ts
, ktime_get(), cpu
);
730 static bool can_stop_idle_tick(int cpu
, struct tick_sched
*ts
)
733 * If this cpu is offline and it is the one which updates
734 * jiffies, then give up the assignment and let it be taken by
735 * the cpu which runs the tick timer next. If we don't drop
736 * this here the jiffies might be stale and do_timer() never
739 if (unlikely(!cpu_online(cpu
))) {
740 if (cpu
== tick_do_timer_cpu
)
741 tick_do_timer_cpu
= TICK_DO_TIMER_NONE
;
745 if (unlikely(ts
->nohz_mode
== NOHZ_MODE_INACTIVE
)) {
746 ts
->sleep_length
= (ktime_t
) { .tv64
= NSEC_PER_SEC
/HZ
};
753 if (unlikely(local_softirq_pending() && cpu_online(cpu
))) {
754 static int ratelimit
;
756 if (ratelimit
< 10 &&
757 (local_softirq_pending() & SOFTIRQ_STOP_IDLE_MASK
)) {
758 pr_warn("NOHZ: local_softirq_pending %02x\n",
759 (unsigned int) local_softirq_pending());
765 if (have_nohz_full_mask
) {
767 * Keep the tick alive to guarantee timekeeping progression
768 * if there are full dynticks CPUs around
770 if (tick_do_timer_cpu
== cpu
)
773 * Boot safety: make sure the timekeeping duty has been
774 * assigned before entering dyntick-idle mode,
776 if (tick_do_timer_cpu
== TICK_DO_TIMER_NONE
)
783 static void __tick_nohz_idle_enter(struct tick_sched
*ts
)
785 ktime_t now
, expires
;
786 int cpu
= smp_processor_id();
788 now
= tick_nohz_start_idle(cpu
, ts
);
790 if (can_stop_idle_tick(cpu
, ts
)) {
791 int was_stopped
= ts
->tick_stopped
;
795 expires
= tick_nohz_stop_sched_tick(ts
, now
, cpu
);
796 if (expires
.tv64
> 0LL) {
798 ts
->idle_expires
= expires
;
801 if (!was_stopped
&& ts
->tick_stopped
)
802 ts
->idle_jiffies
= ts
->last_jiffies
;
807 * tick_nohz_idle_enter - stop the idle tick from the idle task
809 * When the next event is more than a tick into the future, stop the idle tick
810 * Called when we start the idle loop.
812 * The arch is responsible of calling:
814 * - rcu_idle_enter() after its last use of RCU before the CPU is put
816 * - rcu_idle_exit() before the first use of RCU after the CPU is woken up.
818 void tick_nohz_idle_enter(void)
820 struct tick_sched
*ts
;
822 WARN_ON_ONCE(irqs_disabled());
825 * Update the idle state in the scheduler domain hierarchy
826 * when tick_nohz_stop_sched_tick() is called from the idle loop.
827 * State will be updated to busy during the first busy tick after
830 set_cpu_sd_state_idle();
834 ts
= &__get_cpu_var(tick_cpu_sched
);
836 * set ts->inidle unconditionally. even if the system did not
837 * switch to nohz mode the cpu frequency governers rely on the
838 * update of the idle time accounting in tick_nohz_start_idle().
841 __tick_nohz_idle_enter(ts
);
845 EXPORT_SYMBOL_GPL(tick_nohz_idle_enter
);
848 * tick_nohz_irq_exit - update next tick event from interrupt exit
850 * When an interrupt fires while we are idle and it doesn't cause
851 * a reschedule, it may still add, modify or delete a timer, enqueue
852 * an RCU callback, etc...
853 * So we need to re-calculate and reprogram the next tick event.
855 void tick_nohz_irq_exit(void)
857 struct tick_sched
*ts
= &__get_cpu_var(tick_cpu_sched
);
860 __tick_nohz_idle_enter(ts
);
862 tick_nohz_full_stop_tick(ts
);
866 * tick_nohz_get_sleep_length - return the length of the current sleep
868 * Called from power state control code with interrupts disabled
870 ktime_t
tick_nohz_get_sleep_length(void)
872 struct tick_sched
*ts
= &__get_cpu_var(tick_cpu_sched
);
874 return ts
->sleep_length
;
877 static void tick_nohz_restart(struct tick_sched
*ts
, ktime_t now
)
879 hrtimer_cancel(&ts
->sched_timer
);
880 hrtimer_set_expires(&ts
->sched_timer
, ts
->last_tick
);
883 /* Forward the time to expire in the future */
884 hrtimer_forward(&ts
->sched_timer
, now
, tick_period
);
886 if (ts
->nohz_mode
== NOHZ_MODE_HIGHRES
) {
887 hrtimer_start_expires(&ts
->sched_timer
,
888 HRTIMER_MODE_ABS_PINNED
);
889 /* Check, if the timer was already in the past */
890 if (hrtimer_active(&ts
->sched_timer
))
893 if (!tick_program_event(
894 hrtimer_get_expires(&ts
->sched_timer
), 0))
897 /* Reread time and update jiffies */
899 tick_do_update_jiffies64(now
);
903 static void tick_nohz_restart_sched_tick(struct tick_sched
*ts
, ktime_t now
)
905 /* Update jiffies first */
906 tick_do_update_jiffies64(now
);
907 update_cpu_load_nohz();
909 calc_load_exit_idle();
910 touch_softlockup_watchdog();
912 * Cancel the scheduled timer and restore the tick
914 ts
->tick_stopped
= 0;
915 ts
->idle_exittime
= now
;
917 tick_nohz_restart(ts
, now
);
920 static void tick_nohz_account_idle_ticks(struct tick_sched
*ts
)
922 #ifndef CONFIG_VIRT_CPU_ACCOUNTING_NATIVE
925 if (vtime_accounting_enabled())
928 * We stopped the tick in idle. Update process times would miss the
929 * time we slept as update_process_times does only a 1 tick
930 * accounting. Enforce that this is accounted to idle !
932 ticks
= jiffies
- ts
->idle_jiffies
;
934 * We might be one off. Do not randomly account a huge number of ticks!
936 if (ticks
&& ticks
< LONG_MAX
)
937 account_idle_ticks(ticks
);
942 * tick_nohz_idle_exit - restart the idle tick from the idle task
944 * Restart the idle tick when the CPU is woken up from idle
945 * This also exit the RCU extended quiescent state. The CPU
946 * can use RCU again after this function is called.
948 void tick_nohz_idle_exit(void)
950 int cpu
= smp_processor_id();
951 struct tick_sched
*ts
= &per_cpu(tick_cpu_sched
, cpu
);
956 WARN_ON_ONCE(!ts
->inidle
);
960 if (ts
->idle_active
|| ts
->tick_stopped
)
964 tick_nohz_stop_idle(cpu
, now
);
966 if (ts
->tick_stopped
) {
967 tick_nohz_restart_sched_tick(ts
, now
);
968 tick_nohz_account_idle_ticks(ts
);
973 EXPORT_SYMBOL_GPL(tick_nohz_idle_exit
);
975 static int tick_nohz_reprogram(struct tick_sched
*ts
, ktime_t now
)
977 hrtimer_forward(&ts
->sched_timer
, now
, tick_period
);
978 return tick_program_event(hrtimer_get_expires(&ts
->sched_timer
), 0);
982 * The nohz low res interrupt handler
984 static void tick_nohz_handler(struct clock_event_device
*dev
)
986 struct tick_sched
*ts
= &__get_cpu_var(tick_cpu_sched
);
987 struct pt_regs
*regs
= get_irq_regs();
988 ktime_t now
= ktime_get();
990 dev
->next_event
.tv64
= KTIME_MAX
;
992 tick_sched_do_timer(now
);
993 tick_sched_handle(ts
, regs
);
995 while (tick_nohz_reprogram(ts
, now
)) {
997 tick_do_update_jiffies64(now
);
1002 * tick_nohz_switch_to_nohz - switch to nohz mode
1004 static void tick_nohz_switch_to_nohz(void)
1006 struct tick_sched
*ts
= &__get_cpu_var(tick_cpu_sched
);
1009 if (!tick_nohz_enabled
)
1012 local_irq_disable();
1013 if (tick_switch_to_oneshot(tick_nohz_handler
)) {
1018 ts
->nohz_mode
= NOHZ_MODE_LOWRES
;
1021 * Recycle the hrtimer in ts, so we can share the
1022 * hrtimer_forward with the highres code.
1024 hrtimer_init(&ts
->sched_timer
, CLOCK_MONOTONIC
, HRTIMER_MODE_ABS
);
1025 /* Get the next period */
1026 next
= tick_init_jiffy_update();
1029 hrtimer_set_expires(&ts
->sched_timer
, next
);
1030 if (!tick_program_event(next
, 0))
1032 next
= ktime_add(next
, tick_period
);
1038 * When NOHZ is enabled and the tick is stopped, we need to kick the
1039 * tick timer from irq_enter() so that the jiffies update is kept
1040 * alive during long running softirqs. That's ugly as hell, but
1041 * correctness is key even if we need to fix the offending softirq in
1044 * Note, this is different to tick_nohz_restart. We just kick the
1045 * timer and do not touch the other magic bits which need to be done
1046 * when idle is left.
1048 static void tick_nohz_kick_tick(int cpu
, ktime_t now
)
1051 /* Switch back to 2.6.27 behaviour */
1053 struct tick_sched
*ts
= &per_cpu(tick_cpu_sched
, cpu
);
1057 * Do not touch the tick device, when the next expiry is either
1058 * already reached or less/equal than the tick period.
1060 delta
= ktime_sub(hrtimer_get_expires(&ts
->sched_timer
), now
);
1061 if (delta
.tv64
<= tick_period
.tv64
)
1064 tick_nohz_restart(ts
, now
);
1068 static inline void tick_check_nohz(int cpu
)
1070 struct tick_sched
*ts
= &per_cpu(tick_cpu_sched
, cpu
);
1073 if (!ts
->idle_active
&& !ts
->tick_stopped
)
1076 if (ts
->idle_active
)
1077 tick_nohz_stop_idle(cpu
, now
);
1078 if (ts
->tick_stopped
) {
1079 tick_nohz_update_jiffies(now
);
1080 tick_nohz_kick_tick(cpu
, now
);
1086 static inline void tick_nohz_switch_to_nohz(void) { }
1087 static inline void tick_check_nohz(int cpu
) { }
1089 #endif /* CONFIG_NO_HZ_COMMON */
1092 * Called from irq_enter to notify about the possible interruption of idle()
1094 void tick_check_idle(int cpu
)
1096 tick_check_oneshot_broadcast(cpu
);
1097 tick_check_nohz(cpu
);
1101 * High resolution timer specific code
1103 #ifdef CONFIG_HIGH_RES_TIMERS
1105 #ifdef CONFIG_MTK_SCHED_RQAVG_US
1106 static void update_rq_stats(void)
1108 unsigned long jiffy_gap
= 0;
1109 unsigned int rq_avg
= 0;
1110 unsigned long flags
= 0;
1112 spin_lock_irqsave(&rq_lock
, flags
);
1114 jiffy_gap
= jiffies
- rq_info
.rq_poll_last_jiffy
;
1115 if (jiffy_gap
>= rq_info
.rq_poll_jiffies
) {
1116 if (!rq_info
.rq_avg
)
1117 rq_info
.rq_poll_total_jiffies
= 0;
1119 rq_avg
= nr_running() * 10;
1121 if (rq_info
.rq_poll_total_jiffies
) {
1122 rq_avg
= (rq_avg
* jiffy_gap
) +
1124 rq_info
.rq_poll_total_jiffies
);
1126 rq_info
.rq_poll_total_jiffies
+ jiffy_gap
);
1129 rq_info
.rq_avg
= rq_avg
;
1130 rq_info
.rq_poll_total_jiffies
+= jiffy_gap
;
1131 rq_info
.rq_poll_last_jiffy
= jiffies
;
1134 spin_unlock_irqrestore(&rq_lock
, flags
);
1137 #ifdef CONFIG_MTK_SCHED_RQAVG_US_ENABLE_WQ
1138 static void wakeup_user(void)
1140 unsigned long jiffy_gap
;
1142 jiffy_gap
= jiffies
- rq_info
.def_timer_last_jiffy
;
1144 if (jiffy_gap
>= rq_info
.def_timer_jiffies
) {
1145 rq_info
.def_timer_last_jiffy
= jiffies
;
1146 queue_work(rq_wq
, &rq_info
.def_timer_work
);
1149 #endif /* CONFIG_MTK_SCHED_RQAVG_US_ENABLE_WQ */
1151 #endif /* CONFIG_MTK_SCHED_RQAVG_US */
1153 * We rearm the timer until we get disabled by the idle code.
1154 * Called with interrupts disabled.
1156 static enum hrtimer_restart
tick_sched_timer(struct hrtimer
*timer
)
1158 struct tick_sched
*ts
=
1159 container_of(timer
, struct tick_sched
, sched_timer
);
1160 struct pt_regs
*regs
= get_irq_regs();
1161 ktime_t now
= ktime_get();
1163 tick_sched_do_timer(now
);
1166 * Do not call, when we are not in irq context and have
1167 * no valid regs pointer
1170 tick_sched_handle(ts
, regs
);
1172 #ifdef CONFIG_MTK_SCHED_RQAVG_US
1173 if ((rq_info
.init
== 1) && (tick_do_timer_cpu
== smp_processor_id())) {
1176 * update run queue statistics
1180 #ifdef CONFIG_MTK_SCHED_RQAVG_US_ENABLE_WQ
1182 * wakeup user if needed
1185 #endif /* CONFIG_MTK_SCHED_RQAVG_US_ENABLE_WQ */
1187 #endif /* CONFIG_MTK_SCHED_RQAVG_US */
1189 hrtimer_forward(timer
, now
, tick_period
);
1191 return HRTIMER_RESTART
;
1194 static int sched_skew_tick
;
1196 static int __init
skew_tick(char *str
)
1198 get_option(&str
, &sched_skew_tick
);
1202 early_param("skew_tick", skew_tick
);
1205 * tick_setup_sched_timer - setup the tick emulation timer
1207 void tick_setup_sched_timer(void)
1209 struct tick_sched
*ts
= &__get_cpu_var(tick_cpu_sched
);
1210 ktime_t now
= ktime_get();
1213 * Emulate tick processing via per-CPU hrtimers:
1215 hrtimer_init(&ts
->sched_timer
, CLOCK_MONOTONIC
, HRTIMER_MODE_ABS
);
1216 ts
->sched_timer
.function
= tick_sched_timer
;
1218 /* Get the next period (per cpu) */
1219 hrtimer_set_expires(&ts
->sched_timer
, tick_init_jiffy_update());
1221 /* Offset the tick to avert jiffies_lock contention. */
1222 if (sched_skew_tick
) {
1223 u64 offset
= ktime_to_ns(tick_period
) >> 1;
1224 do_div(offset
, num_possible_cpus());
1225 offset
*= smp_processor_id();
1226 hrtimer_add_expires_ns(&ts
->sched_timer
, offset
);
1230 hrtimer_forward(&ts
->sched_timer
, now
, tick_period
);
1231 hrtimer_start_expires(&ts
->sched_timer
,
1232 HRTIMER_MODE_ABS_PINNED
);
1233 /* Check, if the timer was already in the past */
1234 if (hrtimer_active(&ts
->sched_timer
))
1239 #ifdef CONFIG_NO_HZ_COMMON
1240 if (tick_nohz_enabled
)
1241 ts
->nohz_mode
= NOHZ_MODE_HIGHRES
;
1244 #endif /* HIGH_RES_TIMERS */
1246 #if defined CONFIG_NO_HZ_COMMON || defined CONFIG_HIGH_RES_TIMERS
1247 void tick_cancel_sched_timer(int cpu
)
1249 struct tick_sched
*ts
= &per_cpu(tick_cpu_sched
, cpu
);
1251 # ifdef CONFIG_HIGH_RES_TIMERS
1252 if (ts
->sched_timer
.base
)
1253 hrtimer_cancel(&ts
->sched_timer
);
1256 //memset(ts, 0, sizeof(*ts)); /*to avoid idle time clear to 0 after CPU plug off*/
1257 ts
->nohz_mode
= NOHZ_MODE_INACTIVE
;
1262 * Async notification about clocksource changes
1264 void tick_clock_notify(void)
1268 for_each_possible_cpu(cpu
)
1269 set_bit(0, &per_cpu(tick_cpu_sched
, cpu
).check_clocks
);
1273 * Async notification about clock event changes
1275 void tick_oneshot_notify(void)
1277 struct tick_sched
*ts
= &__get_cpu_var(tick_cpu_sched
);
1279 set_bit(0, &ts
->check_clocks
);
1283 * Check, if a change happened, which makes oneshot possible.
1285 * Called cyclic from the hrtimer softirq (driven by the timer
1286 * softirq) allow_nohz signals, that we can switch into low-res nohz
1287 * mode, because high resolution timers are disabled (either compile
1290 int tick_check_oneshot_change(int allow_nohz
)
1292 struct tick_sched
*ts
= &__get_cpu_var(tick_cpu_sched
);
1294 if (!test_and_clear_bit(0, &ts
->check_clocks
))
1297 if (ts
->nohz_mode
!= NOHZ_MODE_INACTIVE
)
1300 if (!timekeeping_valid_for_hres() || !tick_is_oneshot_available())
1306 tick_nohz_switch_to_nohz();