Thomas Gleixner | 79bf2bb | 2007-02-16 01:28:03 -0800 | [diff] [blame] | 1 | /* |
| 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 Machek | b10db7f | 2008-01-30 13:30:00 +0100 | [diff] [blame] | 12 | * Distribute under GPLv2. |
Thomas Gleixner | 79bf2bb | 2007-02-16 01:28:03 -0800 | [diff] [blame] | 13 | */ |
| 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.com | 8083e4a | 2008-08-04 11:59:11 -0700 | [diff] [blame] | 22 | #include <linux/module.h> |
Amar Singhal | f49d99b | 2011-08-22 19:02:04 -0700 | [diff] [blame] | 23 | #include <linux/rq_stats.h> |
Thomas Gleixner | 79bf2bb | 2007-02-16 01:28:03 -0800 | [diff] [blame] | 24 | |
David S. Miller | 9e203bc | 2007-02-24 22:10:13 -0800 | [diff] [blame] | 25 | #include <asm/irq_regs.h> |
| 26 | |
Thomas Gleixner | 79bf2bb | 2007-02-16 01:28:03 -0800 | [diff] [blame] | 27 | #include "tick-internal.h" |
| 28 | |
Amar Singhal | f49d99b | 2011-08-22 19:02:04 -0700 | [diff] [blame] | 29 | |
| 30 | struct rq_data rq_info; |
| 31 | struct workqueue_struct *rq_wq; |
| 32 | spinlock_t rq_lock; |
| 33 | |
Thomas Gleixner | 79bf2bb | 2007-02-16 01:28:03 -0800 | [diff] [blame] | 34 | /* |
| 35 | * Per cpu nohz control structure |
| 36 | */ |
| 37 | static 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 | */ |
| 42 | static ktime_t last_jiffies_update; |
| 43 | |
Ingo Molnar | 289f480 | 2007-02-16 01:28:15 -0800 | [diff] [blame] | 44 | struct tick_sched *tick_get_tick_sched(int cpu) |
| 45 | { |
| 46 | return &per_cpu(tick_cpu_sched, cpu); |
| 47 | } |
| 48 | |
Thomas Gleixner | 79bf2bb | 2007-02-16 01:28:03 -0800 | [diff] [blame] | 49 | /* |
| 50 | * Must be called with interrupts disabled ! |
| 51 | */ |
| 52 | static void tick_do_update_jiffies64(ktime_t now) |
| 53 | { |
| 54 | unsigned long ticks = 0; |
| 55 | ktime_t delta; |
| 56 | |
Ingo Molnar | 7a14ce1 | 2008-05-12 15:43:53 +0200 | [diff] [blame] | 57 | /* |
| 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 Gleixner | 79bf2bb | 2007-02-16 01:28:03 -0800 | [diff] [blame] | 64 | /* 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 Gleixner | 49d670f | 2008-09-22 18:56:01 +0200 | [diff] [blame] | 84 | |
| 85 | /* Keep the tick_next_period variable up to date */ |
| 86 | tick_next_period = ktime_add(last_jiffies_update, tick_period); |
Thomas Gleixner | 79bf2bb | 2007-02-16 01:28:03 -0800 | [diff] [blame] | 87 | } |
| 88 | write_sequnlock(&xtime_lock); |
| 89 | } |
| 90 | |
| 91 | /* |
| 92 | * Initialize and return retrieve the jiffies update. |
| 93 | */ |
| 94 | static 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 | */ |
| 114 | static int tick_nohz_enabled __read_mostly = 1; |
| 115 | |
| 116 | /* |
| 117 | * Enable / Disable tickless mode |
| 118 | */ |
| 119 | static 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 Schwidefsky | eed3b9c | 2009-09-29 14:25:15 +0200 | [diff] [blame] | 142 | static void tick_nohz_update_jiffies(ktime_t now) |
Thomas Gleixner | 79bf2bb | 2007-02-16 01:28:03 -0800 | [diff] [blame] | 143 | { |
| 144 | int cpu = smp_processor_id(); |
| 145 | struct tick_sched *ts = &per_cpu(tick_cpu_sched, cpu); |
| 146 | unsigned long flags; |
Thomas Gleixner | 79bf2bb | 2007-02-16 01:28:03 -0800 | [diff] [blame] | 147 | |
Rusty Russell | 6a7b3dc | 2008-11-25 02:35:04 +1030 | [diff] [blame] | 148 | cpumask_clear_cpu(cpu, nohz_cpu_mask); |
Thomas Gleixner | 5df7fa1 | 2008-02-01 17:45:14 +0100 | [diff] [blame] | 149 | ts->idle_waketime = now; |
Thomas Gleixner | 79bf2bb | 2007-02-16 01:28:03 -0800 | [diff] [blame] | 150 | |
| 151 | local_irq_save(flags); |
| 152 | tick_do_update_jiffies64(now); |
| 153 | local_irq_restore(flags); |
Ingo Molnar | 02ff375 | 2008-05-12 15:43:53 +0200 | [diff] [blame] | 154 | |
| 155 | touch_softlockup_watchdog(); |
Thomas Gleixner | 79bf2bb | 2007-02-16 01:28:03 -0800 | [diff] [blame] | 156 | } |
| 157 | |
Arjan van de Ven | 595aac4 | 2010-05-09 08:22:45 -0700 | [diff] [blame] | 158 | /* |
| 159 | * Updates the per cpu time idle statistics counters |
| 160 | */ |
Arjan van de Ven | 8d63bf9 | 2010-05-09 08:24:03 -0700 | [diff] [blame] | 161 | static void |
Peter Zijlstra | 8c215bd | 2010-07-01 09:07:17 +0200 | [diff] [blame] | 162 | update_ts_time_stats(int cpu, struct tick_sched *ts, ktime_t now, u64 *last_update_time) |
Arjan van de Ven | 595aac4 | 2010-05-09 08:22:45 -0700 | [diff] [blame] | 163 | { |
| 164 | ktime_t delta; |
| 165 | |
Arjan van de Ven | 595aac4 | 2010-05-09 08:22:45 -0700 | [diff] [blame] | 166 | if (ts->idle_active) { |
| 167 | delta = ktime_sub(now, ts->idle_entrytime); |
| 168 | ts->idle_sleeptime = ktime_add(ts->idle_sleeptime, delta); |
Peter Zijlstra | 8c215bd | 2010-07-01 09:07:17 +0200 | [diff] [blame] | 169 | if (nr_iowait_cpu(cpu) > 0) |
Arjan van de Ven | 0224cf4 | 2010-05-09 08:25:23 -0700 | [diff] [blame] | 170 | ts->iowait_sleeptime = ktime_add(ts->iowait_sleeptime, delta); |
Arjan van de Ven | 8c7b09f | 2010-05-09 08:23:23 -0700 | [diff] [blame] | 171 | ts->idle_entrytime = now; |
Arjan van de Ven | 595aac4 | 2010-05-09 08:22:45 -0700 | [diff] [blame] | 172 | } |
Arjan van de Ven | 8d63bf9 | 2010-05-09 08:24:03 -0700 | [diff] [blame] | 173 | |
Arjan van de Ven | e0e37c2 | 2010-05-09 08:24:39 -0700 | [diff] [blame] | 174 | if (last_update_time) |
Arjan van de Ven | 8d63bf9 | 2010-05-09 08:24:03 -0700 | [diff] [blame] | 175 | *last_update_time = ktime_to_us(now); |
| 176 | |
Arjan van de Ven | 595aac4 | 2010-05-09 08:22:45 -0700 | [diff] [blame] | 177 | } |
| 178 | |
Martin Schwidefsky | eed3b9c | 2009-09-29 14:25:15 +0200 | [diff] [blame] | 179 | static void tick_nohz_stop_idle(int cpu, ktime_t now) |
Venki Pallipadi | 6378ddb | 2008-01-30 13:30:04 +0100 | [diff] [blame] | 180 | { |
| 181 | struct tick_sched *ts = &per_cpu(tick_cpu_sched, cpu); |
| 182 | |
Peter Zijlstra | 8c215bd | 2010-07-01 09:07:17 +0200 | [diff] [blame] | 183 | update_ts_time_stats(cpu, ts, now, NULL); |
Martin Schwidefsky | eed3b9c | 2009-09-29 14:25:15 +0200 | [diff] [blame] | 184 | ts->idle_active = 0; |
Peter Zijlstra | 56c7426 | 2008-09-01 16:44:23 +0200 | [diff] [blame] | 185 | |
Martin Schwidefsky | eed3b9c | 2009-09-29 14:25:15 +0200 | [diff] [blame] | 186 | sched_clock_idle_wakeup_event(0); |
Venki Pallipadi | 6378ddb | 2008-01-30 13:30:04 +0100 | [diff] [blame] | 187 | } |
| 188 | |
Peter Zijlstra | 8c215bd | 2010-07-01 09:07:17 +0200 | [diff] [blame] | 189 | static ktime_t tick_nohz_start_idle(int cpu, struct tick_sched *ts) |
Venki Pallipadi | 6378ddb | 2008-01-30 13:30:04 +0100 | [diff] [blame] | 190 | { |
Arjan van de Ven | 595aac4 | 2010-05-09 08:22:45 -0700 | [diff] [blame] | 191 | ktime_t now; |
Venki Pallipadi | 6378ddb | 2008-01-30 13:30:04 +0100 | [diff] [blame] | 192 | |
| 193 | now = ktime_get(); |
Arjan van de Ven | 595aac4 | 2010-05-09 08:22:45 -0700 | [diff] [blame] | 194 | |
Peter Zijlstra | 8c215bd | 2010-07-01 09:07:17 +0200 | [diff] [blame] | 195 | update_ts_time_stats(cpu, ts, now, NULL); |
Arjan van de Ven | 595aac4 | 2010-05-09 08:22:45 -0700 | [diff] [blame] | 196 | |
Venki Pallipadi | 6378ddb | 2008-01-30 13:30:04 +0100 | [diff] [blame] | 197 | ts->idle_entrytime = now; |
| 198 | ts->idle_active = 1; |
Peter Zijlstra | 56c7426 | 2008-09-01 16:44:23 +0200 | [diff] [blame] | 199 | sched_clock_idle_sleep_event(); |
Venki Pallipadi | 6378ddb | 2008-01-30 13:30:04 +0100 | [diff] [blame] | 200 | return now; |
| 201 | } |
| 202 | |
Arjan van de Ven | b1f724c | 2010-05-09 08:22:08 -0700 | [diff] [blame] | 203 | /** |
| 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 Pallipadi | 6378ddb | 2008-01-30 13:30:04 +0100 | [diff] [blame] | 217 | u64 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.com | 8083e4a | 2008-08-04 11:59:11 -0700 | [diff] [blame] | 221 | if (!tick_nohz_enabled) |
| 222 | return -1; |
| 223 | |
Peter Zijlstra | 8c215bd | 2010-07-01 09:07:17 +0200 | [diff] [blame] | 224 | update_ts_time_stats(cpu, ts, ktime_get(), last_update_time); |
venkatesh.pallipadi@intel.com | 8083e4a | 2008-08-04 11:59:11 -0700 | [diff] [blame] | 225 | |
Venki Pallipadi | 6378ddb | 2008-01-30 13:30:04 +0100 | [diff] [blame] | 226 | return ktime_to_us(ts->idle_sleeptime); |
| 227 | } |
venkatesh.pallipadi@intel.com | 8083e4a | 2008-08-04 11:59:11 -0700 | [diff] [blame] | 228 | EXPORT_SYMBOL_GPL(get_cpu_idle_time_us); |
Venki Pallipadi | 6378ddb | 2008-01-30 13:30:04 +0100 | [diff] [blame] | 229 | |
Arjan van de Ven | 0224cf4 | 2010-05-09 08:25:23 -0700 | [diff] [blame] | 230 | /* |
| 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 | */ |
| 243 | u64 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 Zijlstra | 8c215bd | 2010-07-01 09:07:17 +0200 | [diff] [blame] | 250 | update_ts_time_stats(cpu, ts, ktime_get(), last_update_time); |
Arjan van de Ven | 0224cf4 | 2010-05-09 08:25:23 -0700 | [diff] [blame] | 251 | |
| 252 | return ktime_to_us(ts->iowait_sleeptime); |
| 253 | } |
| 254 | EXPORT_SYMBOL_GPL(get_cpu_iowait_time_us); |
| 255 | |
Thomas Gleixner | 79bf2bb | 2007-02-16 01:28:03 -0800 | [diff] [blame] | 256 | /** |
| 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 Gleixner | b8f8c3c | 2008-07-18 17:27:28 +0200 | [diff] [blame] | 263 | void tick_nohz_stop_sched_tick(int inidle) |
Thomas Gleixner | 79bf2bb | 2007-02-16 01:28:03 -0800 | [diff] [blame] | 264 | { |
| 265 | unsigned long seq, last_jiffies, next_jiffies, delta_jiffies, flags; |
| 266 | struct tick_sched *ts; |
Venki Pallipadi | 6378ddb | 2008-01-30 13:30:04 +0100 | [diff] [blame] | 267 | ktime_t last_update, expires, now; |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 268 | struct clock_event_device *dev = __get_cpu_var(tick_cpu_device).evtdev; |
Jon Hunter | 9896246 | 2009-08-18 12:45:10 -0500 | [diff] [blame] | 269 | u64 time_delta; |
Thomas Gleixner | 79bf2bb | 2007-02-16 01:28:03 -0800 | [diff] [blame] | 270 | int cpu; |
| 271 | |
| 272 | local_irq_save(flags); |
| 273 | |
| 274 | cpu = smp_processor_id(); |
| 275 | ts = &per_cpu(tick_cpu_sched, cpu); |
Eero Nurkkala | f2e21c9 | 2009-05-25 09:57:37 +0300 | [diff] [blame] | 276 | |
| 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 Nurkkala | fdc6f19 | 2009-10-07 11:54:26 +0300 | [diff] [blame] | 285 | /* |
| 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 Zijlstra | 8c215bd | 2010-07-01 09:07:17 +0200 | [diff] [blame] | 292 | now = tick_nohz_start_idle(cpu, ts); |
Thomas Gleixner | 79bf2bb | 2007-02-16 01:28:03 -0800 | [diff] [blame] | 293 | |
Thomas Gleixner | 5e41d0d | 2007-09-16 15:36:43 +0200 | [diff] [blame] | 294 | /* |
| 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 Gleixner | 6441402 | 2008-09-22 18:46:37 +0200 | [diff] [blame] | 303 | tick_do_timer_cpu = TICK_DO_TIMER_NONE; |
Thomas Gleixner | 5e41d0d | 2007-09-16 15:36:43 +0200 | [diff] [blame] | 304 | } |
| 305 | |
Thomas Gleixner | 79bf2bb | 2007-02-16 01:28:03 -0800 | [diff] [blame] | 306 | if (unlikely(ts->nohz_mode == NOHZ_MODE_INACTIVE)) |
| 307 | goto end; |
| 308 | |
| 309 | if (need_resched()) |
| 310 | goto end; |
| 311 | |
Heiko Carstens | fa116ea | 2008-12-11 17:04:11 +0100 | [diff] [blame] | 312 | if (unlikely(local_softirq_pending() && cpu_online(cpu))) { |
Thomas Gleixner | 3528231 | 2007-05-23 13:57:37 -0700 | [diff] [blame] | 313 | static int ratelimit; |
| 314 | |
| 315 | if (ratelimit < 10) { |
| 316 | printk(KERN_ERR "NOHZ: local_softirq_pending %02x\n", |
Thomas Gleixner | 529eacc | 2009-11-13 14:32:19 +0100 | [diff] [blame] | 317 | (unsigned int) local_softirq_pending()); |
Thomas Gleixner | 3528231 | 2007-05-23 13:57:37 -0700 | [diff] [blame] | 318 | ratelimit++; |
| 319 | } |
Heiko Carstens | 857f3fd | 2008-07-11 11:09:22 +0200 | [diff] [blame] | 320 | goto end; |
Thomas Gleixner | 3528231 | 2007-05-23 13:57:37 -0700 | [diff] [blame] | 321 | } |
Thomas Gleixner | 79bf2bb | 2007-02-16 01:28:03 -0800 | [diff] [blame] | 322 | |
Thomas Gleixner | 79bf2bb | 2007-02-16 01:28:03 -0800 | [diff] [blame] | 323 | ts->idle_calls++; |
Thomas Gleixner | 79bf2bb | 2007-02-16 01:28:03 -0800 | [diff] [blame] | 324 | /* 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 Gleixner | 2718501 | 2009-11-12 22:12:06 +0100 | [diff] [blame] | 329 | time_delta = timekeeping_max_deferment(); |
Thomas Gleixner | 79bf2bb | 2007-02-16 01:28:03 -0800 | [diff] [blame] | 330 | } while (read_seqretry(&xtime_lock, seq)); |
| 331 | |
Martin Schwidefsky | 3c5d92a | 2009-09-29 14:25:16 +0200 | [diff] [blame] | 332 | if (rcu_needs_cpu(cpu) || printk_needs_cpu(cpu) || |
Peter Zijlstra | 396e894 | 2010-07-09 15:12:27 +0200 | [diff] [blame] | 333 | arch_needs_cpu(cpu)) { |
Martin Schwidefsky | 3c5d92a | 2009-09-29 14:25:16 +0200 | [diff] [blame] | 334 | next_jiffies = last_jiffies + 1; |
Ingo Molnar | 6ba9b34 | 2007-02-19 18:11:56 +0000 | [diff] [blame] | 335 | delta_jiffies = 1; |
Martin Schwidefsky | 3c5d92a | 2009-09-29 14:25:16 +0200 | [diff] [blame] | 336 | } 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 Gleixner | 79bf2bb | 2007-02-16 01:28:03 -0800 | [diff] [blame] | 341 | /* |
| 342 | * Do not stop the tick, if we are only one off |
| 343 | * or if the cpu is required for rcu |
| 344 | */ |
Ingo Molnar | 6ba9b34 | 2007-02-19 18:11:56 +0000 | [diff] [blame] | 345 | if (!ts->tick_stopped && delta_jiffies == 1) |
Thomas Gleixner | 79bf2bb | 2007-02-16 01:28:03 -0800 | [diff] [blame] | 346 | goto out; |
| 347 | |
| 348 | /* Schedule the tick, if we are at least one jiffie off */ |
| 349 | if ((long)delta_jiffies >= 1) { |
| 350 | |
Woodruff, Richard | 0014744 | 2008-12-01 14:18:11 -0800 | [diff] [blame] | 351 | /* |
Woodruff, Richard | 0014744 | 2008-12-01 14:18:11 -0800 | [diff] [blame] | 352 | * 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 Gleixner | 2718501 | 2009-11-12 22:12:06 +0100 | [diff] [blame] | 357 | * 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, Richard | 0014744 | 2008-12-01 14:18:11 -0800 | [diff] [blame] | 363 | */ |
Thomas Gleixner | 2718501 | 2009-11-12 22:12:06 +0100 | [diff] [blame] | 364 | if (cpu == tick_do_timer_cpu) { |
Woodruff, Richard | 0014744 | 2008-12-01 14:18:11 -0800 | [diff] [blame] | 365 | tick_do_timer_cpu = TICK_DO_TIMER_NONE; |
Thomas Gleixner | 2718501 | 2009-11-12 22:12:06 +0100 | [diff] [blame] | 366 | 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 Hunter | 9896246 | 2009-08-18 12:45:10 -0500 | [diff] [blame] | 375 | * 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 Hunter | 9896246 | 2009-08-18 12:45:10 -0500 | [diff] [blame] | 391 | } |
Woodruff, Richard | 0014744 | 2008-12-01 14:18:11 -0800 | [diff] [blame] | 392 | |
Thomas Gleixner | 2718501 | 2009-11-12 22:12:06 +0100 | [diff] [blame] | 393 | if (time_delta < KTIME_MAX) |
| 394 | expires = ktime_add_ns(last_update, time_delta); |
| 395 | else |
| 396 | expires.tv64 = KTIME_MAX; |
Woodruff, Richard | 0014744 | 2008-12-01 14:18:11 -0800 | [diff] [blame] | 397 | |
Ingo Molnar | 6ba9b34 | 2007-02-19 18:11:56 +0000 | [diff] [blame] | 398 | if (delta_jiffies > 1) |
Rusty Russell | 6a7b3dc | 2008-11-25 02:35:04 +1030 | [diff] [blame] | 399 | cpumask_set_cpu(cpu, nohz_cpu_mask); |
Woodruff, Richard | 0014744 | 2008-12-01 14:18:11 -0800 | [diff] [blame] | 400 | |
| 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 Gleixner | 79bf2bb | 2007-02-16 01:28:03 -0800 | [diff] [blame] | 405 | /* |
| 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 Pallipadi | 83cd4fe | 2010-05-21 17:09:41 -0700 | [diff] [blame] | 413 | select_nohz_load_balancer(1); |
Siddha, Suresh B | 46cb4b7 | 2007-05-08 00:32:51 -0700 | [diff] [blame] | 414 | |
Arjan van de Ven | cc584b2 | 2008-09-01 15:02:30 -0700 | [diff] [blame] | 415 | ts->idle_tick = hrtimer_get_expires(&ts->sched_timer); |
Thomas Gleixner | 79bf2bb | 2007-02-16 01:28:03 -0800 | [diff] [blame] | 416 | ts->tick_stopped = 1; |
| 417 | ts->idle_jiffies = last_jiffies; |
Steven Rostedt | 2232c2d | 2008-02-29 18:46:50 +0100 | [diff] [blame] | 418 | rcu_enter_nohz(); |
Thomas Gleixner | 79bf2bb | 2007-02-16 01:28:03 -0800 | [diff] [blame] | 419 | } |
Thomas Gleixner | d3ed782 | 2007-05-08 00:30:03 -0700 | [diff] [blame] | 420 | |
Thomas Gleixner | eaad084 | 2007-05-29 23:47:39 +0200 | [diff] [blame] | 421 | ts->idle_sleeps++; |
| 422 | |
Jon Hunter | 9896246 | 2009-08-18 12:45:10 -0500 | [diff] [blame] | 423 | /* Mark expires */ |
| 424 | ts->idle_expires = expires; |
| 425 | |
Thomas Gleixner | eaad084 | 2007-05-29 23:47:39 +0200 | [diff] [blame] | 426 | /* |
Jon Hunter | 9896246 | 2009-08-18 12:45:10 -0500 | [diff] [blame] | 427 | * If the expiration time == KTIME_MAX, then |
| 428 | * in this case we simply stop the tick timer. |
Thomas Gleixner | eaad084 | 2007-05-29 23:47:39 +0200 | [diff] [blame] | 429 | */ |
Jon Hunter | 9896246 | 2009-08-18 12:45:10 -0500 | [diff] [blame] | 430 | if (unlikely(expires.tv64 == KTIME_MAX)) { |
Thomas Gleixner | eaad084 | 2007-05-29 23:47:39 +0200 | [diff] [blame] | 431 | if (ts->nohz_mode == NOHZ_MODE_HIGHRES) |
| 432 | hrtimer_cancel(&ts->sched_timer); |
| 433 | goto out; |
| 434 | } |
| 435 | |
Thomas Gleixner | 79bf2bb | 2007-02-16 01:28:03 -0800 | [diff] [blame] | 436 | if (ts->nohz_mode == NOHZ_MODE_HIGHRES) { |
| 437 | hrtimer_start(&ts->sched_timer, expires, |
Arun R Bharadwaj | 5c33386 | 2009-04-16 12:14:37 +0530 | [diff] [blame] | 438 | HRTIMER_MODE_ABS_PINNED); |
Thomas Gleixner | 79bf2bb | 2007-02-16 01:28:03 -0800 | [diff] [blame] | 439 | /* Check, if the timer was already in the past */ |
| 440 | if (hrtimer_active(&ts->sched_timer)) |
| 441 | goto out; |
Pavel Machek | 4c9dc64 | 2008-01-30 13:30:00 +0100 | [diff] [blame] | 442 | } else if (!tick_program_event(expires, 0)) |
Thomas Gleixner | 79bf2bb | 2007-02-16 01:28:03 -0800 | [diff] [blame] | 443 | 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 Russell | 6a7b3dc | 2008-11-25 02:35:04 +1030 | [diff] [blame] | 450 | cpumask_clear_cpu(cpu, nohz_cpu_mask); |
Thomas Gleixner | 79bf2bb | 2007-02-16 01:28:03 -0800 | [diff] [blame] | 451 | } |
| 452 | raise_softirq_irqoff(TIMER_SOFTIRQ); |
| 453 | out: |
| 454 | ts->next_jiffies = next_jiffies; |
| 455 | ts->last_jiffies = last_jiffies; |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 456 | ts->sleep_length = ktime_sub(dev->next_event, now); |
Thomas Gleixner | 79bf2bb | 2007-02-16 01:28:03 -0800 | [diff] [blame] | 457 | end: |
| 458 | local_irq_restore(flags); |
| 459 | } |
| 460 | |
| 461 | /** |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 462 | * tick_nohz_get_sleep_length - return the length of the current sleep |
| 463 | * |
| 464 | * Called from power state control code with interrupts disabled |
| 465 | */ |
| 466 | ktime_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 Gleixner | c34bec5 | 2008-10-17 10:04:34 +0200 | [diff] [blame] | 473 | static void tick_nohz_restart(struct tick_sched *ts, ktime_t now) |
| 474 | { |
| 475 | hrtimer_cancel(&ts->sched_timer); |
Thomas Gleixner | 268a3dc | 2008-10-22 09:48:06 +0200 | [diff] [blame] | 476 | hrtimer_set_expires(&ts->sched_timer, ts->idle_tick); |
Thomas Gleixner | c34bec5 | 2008-10-17 10:04:34 +0200 | [diff] [blame] | 477 | |
| 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 Gleixner | 268a3dc | 2008-10-22 09:48:06 +0200 | [diff] [blame] | 483 | hrtimer_start_expires(&ts->sched_timer, |
Arun R Bharadwaj | 5c33386 | 2009-04-16 12:14:37 +0530 | [diff] [blame] | 484 | HRTIMER_MODE_ABS_PINNED); |
Thomas Gleixner | c34bec5 | 2008-10-17 10:04:34 +0200 | [diff] [blame] | 485 | /* Check, if the timer was already in the past */ |
| 486 | if (hrtimer_active(&ts->sched_timer)) |
| 487 | break; |
| 488 | } else { |
Thomas Gleixner | 268a3dc | 2008-10-22 09:48:06 +0200 | [diff] [blame] | 489 | if (!tick_program_event( |
| 490 | hrtimer_get_expires(&ts->sched_timer), 0)) |
Thomas Gleixner | c34bec5 | 2008-10-17 10:04:34 +0200 | [diff] [blame] | 491 | break; |
| 492 | } |
| 493 | /* Update jiffies and reread time */ |
| 494 | tick_do_update_jiffies64(now); |
| 495 | now = ktime_get(); |
| 496 | } |
| 497 | } |
| 498 | |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 499 | /** |
Li Zefan | 8dce39c | 2007-11-05 14:51:10 -0800 | [diff] [blame] | 500 | * tick_nohz_restart_sched_tick - restart the idle tick from the idle task |
Thomas Gleixner | 79bf2bb | 2007-02-16 01:28:03 -0800 | [diff] [blame] | 501 | * |
| 502 | * Restart the idle tick when the CPU is woken up from idle |
| 503 | */ |
| 504 | void 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 Schwidefsky | 79741dd | 2008-12-31 15:11:38 +0100 | [diff] [blame] | 508 | #ifndef CONFIG_VIRT_CPU_ACCOUNTING |
Thomas Gleixner | 79bf2bb | 2007-02-16 01:28:03 -0800 | [diff] [blame] | 509 | unsigned long ticks; |
Martin Schwidefsky | 79741dd | 2008-12-31 15:11:38 +0100 | [diff] [blame] | 510 | #endif |
Venki Pallipadi | 6378ddb | 2008-01-30 13:30:04 +0100 | [diff] [blame] | 511 | ktime_t now; |
Thomas Gleixner | 79bf2bb | 2007-02-16 01:28:03 -0800 | [diff] [blame] | 512 | |
| 513 | local_irq_disable(); |
Martin Schwidefsky | eed3b9c | 2009-09-29 14:25:15 +0200 | [diff] [blame] | 514 | 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 Pallipadi | 6378ddb | 2008-01-30 13:30:04 +0100 | [diff] [blame] | 519 | |
Thomas Gleixner | b8f8c3c | 2008-07-18 17:27:28 +0200 | [diff] [blame] | 520 | if (!ts->inidle || !ts->tick_stopped) { |
| 521 | ts->inidle = 0; |
Venki Pallipadi | 6378ddb | 2008-01-30 13:30:04 +0100 | [diff] [blame] | 522 | local_irq_enable(); |
| 523 | return; |
| 524 | } |
| 525 | |
Thomas Gleixner | b8f8c3c | 2008-07-18 17:27:28 +0200 | [diff] [blame] | 526 | ts->inidle = 0; |
| 527 | |
Steven Rostedt | 2232c2d | 2008-02-29 18:46:50 +0100 | [diff] [blame] | 528 | rcu_exit_nohz(); |
| 529 | |
Venki Pallipadi | 6378ddb | 2008-01-30 13:30:04 +0100 | [diff] [blame] | 530 | /* Update jiffies first */ |
Siddha, Suresh B | 46cb4b7 | 2007-05-08 00:32:51 -0700 | [diff] [blame] | 531 | select_nohz_load_balancer(0); |
Thomas Gleixner | 79bf2bb | 2007-02-16 01:28:03 -0800 | [diff] [blame] | 532 | tick_do_update_jiffies64(now); |
Rusty Russell | 6a7b3dc | 2008-11-25 02:35:04 +1030 | [diff] [blame] | 533 | cpumask_clear_cpu(cpu, nohz_cpu_mask); |
Thomas Gleixner | 79bf2bb | 2007-02-16 01:28:03 -0800 | [diff] [blame] | 534 | |
Martin Schwidefsky | 79741dd | 2008-12-31 15:11:38 +0100 | [diff] [blame] | 535 | #ifndef CONFIG_VIRT_CPU_ACCOUNTING |
Thomas Gleixner | 79bf2bb | 2007-02-16 01:28:03 -0800 | [diff] [blame] | 536 | /* |
| 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 Schwidefsky | 79741dd | 2008-12-31 15:11:38 +0100 | [diff] [blame] | 545 | if (ticks && ticks < LONG_MAX) |
| 546 | account_idle_ticks(ticks); |
| 547 | #endif |
Thomas Gleixner | 79bf2bb | 2007-02-16 01:28:03 -0800 | [diff] [blame] | 548 | |
Ingo Molnar | 126e01b | 2008-04-25 00:25:08 +0200 | [diff] [blame] | 549 | touch_softlockup_watchdog(); |
Thomas Gleixner | 79bf2bb | 2007-02-16 01:28:03 -0800 | [diff] [blame] | 550 | /* |
| 551 | * Cancel the scheduled timer and restore the tick |
| 552 | */ |
| 553 | ts->tick_stopped = 0; |
Thomas Gleixner | 5df7fa1 | 2008-02-01 17:45:14 +0100 | [diff] [blame] | 554 | ts->idle_exittime = now; |
Thomas Gleixner | 79bf2bb | 2007-02-16 01:28:03 -0800 | [diff] [blame] | 555 | |
Thomas Gleixner | c34bec5 | 2008-10-17 10:04:34 +0200 | [diff] [blame] | 556 | tick_nohz_restart(ts, now); |
Thomas Gleixner | 79bf2bb | 2007-02-16 01:28:03 -0800 | [diff] [blame] | 557 | |
Thomas Gleixner | 79bf2bb | 2007-02-16 01:28:03 -0800 | [diff] [blame] | 558 | local_irq_enable(); |
| 559 | } |
| 560 | |
| 561 | static int tick_nohz_reprogram(struct tick_sched *ts, ktime_t now) |
| 562 | { |
| 563 | hrtimer_forward(&ts->sched_timer, now, tick_period); |
Arjan van de Ven | cc584b2 | 2008-09-01 15:02:30 -0700 | [diff] [blame] | 564 | return tick_program_event(hrtimer_get_expires(&ts->sched_timer), 0); |
Thomas Gleixner | 79bf2bb | 2007-02-16 01:28:03 -0800 | [diff] [blame] | 565 | } |
| 566 | |
| 567 | /* |
| 568 | * The nohz low res interrupt handler |
| 569 | */ |
| 570 | static 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 Gleixner | d3ed782 | 2007-05-08 00:30:03 -0700 | [diff] [blame] | 574 | int cpu = smp_processor_id(); |
Thomas Gleixner | 79bf2bb | 2007-02-16 01:28:03 -0800 | [diff] [blame] | 575 | ktime_t now = ktime_get(); |
| 576 | |
| 577 | dev->next_event.tv64 = KTIME_MAX; |
| 578 | |
Thomas Gleixner | d3ed782 | 2007-05-08 00:30:03 -0700 | [diff] [blame] | 579 | /* |
| 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 Gleixner | 6441402 | 2008-09-22 18:46:37 +0200 | [diff] [blame] | 586 | if (unlikely(tick_do_timer_cpu == TICK_DO_TIMER_NONE)) |
Thomas Gleixner | d3ed782 | 2007-05-08 00:30:03 -0700 | [diff] [blame] | 587 | tick_do_timer_cpu = cpu; |
| 588 | |
Thomas Gleixner | 79bf2bb | 2007-02-16 01:28:03 -0800 | [diff] [blame] | 589 | /* Check, if the jiffies need an update */ |
Thomas Gleixner | d3ed782 | 2007-05-08 00:30:03 -0700 | [diff] [blame] | 590 | if (tick_do_timer_cpu == cpu) |
| 591 | tick_do_update_jiffies64(now); |
Thomas Gleixner | 79bf2bb | 2007-02-16 01:28:03 -0800 | [diff] [blame] | 592 | |
| 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 Gleixner | 79bf2bb | 2007-02-16 01:28:03 -0800 | [diff] [blame] | 609 | 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 | */ |
| 618 | static 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 Ven | cc584b2 | 2008-09-01 15:02:30 -0700 | [diff] [blame] | 643 | hrtimer_set_expires(&ts->sched_timer, next); |
Thomas Gleixner | 79bf2bb | 2007-02-16 01:28:03 -0800 | [diff] [blame] | 644 | if (!tick_program_event(next, 0)) |
| 645 | break; |
| 646 | next = ktime_add(next, tick_period); |
| 647 | } |
| 648 | local_irq_enable(); |
Thomas Gleixner | 79bf2bb | 2007-02-16 01:28:03 -0800 | [diff] [blame] | 649 | } |
| 650 | |
Thomas Gleixner | fb02fbc | 2008-10-17 10:01:23 +0200 | [diff] [blame] | 651 | /* |
| 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 Schwidefsky | eed3b9c | 2009-09-29 14:25:15 +0200 | [diff] [blame] | 662 | static void tick_nohz_kick_tick(int cpu, ktime_t now) |
Thomas Gleixner | fb02fbc | 2008-10-17 10:01:23 +0200 | [diff] [blame] | 663 | { |
Thomas Gleixner | ae99286 | 2008-11-10 13:20:23 +0100 | [diff] [blame] | 664 | #if 0 |
| 665 | /* Switch back to 2.6.27 behaviour */ |
| 666 | |
Thomas Gleixner | fb02fbc | 2008-10-17 10:01:23 +0200 | [diff] [blame] | 667 | struct tick_sched *ts = &per_cpu(tick_cpu_sched, cpu); |
Martin Schwidefsky | eed3b9c | 2009-09-29 14:25:15 +0200 | [diff] [blame] | 668 | ktime_t delta; |
Thomas Gleixner | fb02fbc | 2008-10-17 10:01:23 +0200 | [diff] [blame] | 669 | |
Thomas Gleixner | c4bd822 | 2008-10-21 20:17:35 +0200 | [diff] [blame] | 670 | /* |
| 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 Gleixner | 268a3dc | 2008-10-22 09:48:06 +0200 | [diff] [blame] | 674 | delta = ktime_sub(hrtimer_get_expires(&ts->sched_timer), now); |
Thomas Gleixner | c4bd822 | 2008-10-21 20:17:35 +0200 | [diff] [blame] | 675 | if (delta.tv64 <= tick_period.tv64) |
| 676 | return; |
| 677 | |
| 678 | tick_nohz_restart(ts, now); |
Thomas Gleixner | ae99286 | 2008-11-10 13:20:23 +0100 | [diff] [blame] | 679 | #endif |
Thomas Gleixner | fb02fbc | 2008-10-17 10:01:23 +0200 | [diff] [blame] | 680 | } |
| 681 | |
Martin Schwidefsky | eed3b9c | 2009-09-29 14:25:15 +0200 | [diff] [blame] | 682 | static 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 Gleixner | 79bf2bb | 2007-02-16 01:28:03 -0800 | [diff] [blame] | 698 | #else |
| 699 | |
| 700 | static inline void tick_nohz_switch_to_nohz(void) { } |
Martin Schwidefsky | eed3b9c | 2009-09-29 14:25:15 +0200 | [diff] [blame] | 701 | static inline void tick_check_nohz(int cpu) { } |
Thomas Gleixner | 79bf2bb | 2007-02-16 01:28:03 -0800 | [diff] [blame] | 702 | |
| 703 | #endif /* NO_HZ */ |
| 704 | |
| 705 | /* |
Thomas Gleixner | 719254f | 2008-10-17 09:59:47 +0200 | [diff] [blame] | 706 | * Called from irq_enter to notify about the possible interruption of idle() |
| 707 | */ |
| 708 | void tick_check_idle(int cpu) |
| 709 | { |
Thomas Gleixner | fb02fbc | 2008-10-17 10:01:23 +0200 | [diff] [blame] | 710 | tick_check_oneshot_broadcast(cpu); |
Martin Schwidefsky | eed3b9c | 2009-09-29 14:25:15 +0200 | [diff] [blame] | 711 | tick_check_nohz(cpu); |
Thomas Gleixner | 719254f | 2008-10-17 09:59:47 +0200 | [diff] [blame] | 712 | } |
| 713 | |
| 714 | /* |
Thomas Gleixner | 79bf2bb | 2007-02-16 01:28:03 -0800 | [diff] [blame] | 715 | * High resolution timer specific code |
| 716 | */ |
| 717 | #ifdef CONFIG_HIGH_RES_TIMERS |
Amar Singhal | f49d99b | 2011-08-22 19:02:04 -0700 | [diff] [blame] | 718 | static 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 | |
| 751 | static 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 Gleixner | 79bf2bb | 2007-02-16 01:28:03 -0800 | [diff] [blame] | 762 | /* |
Pavel Machek | 4c9dc64 | 2008-01-30 13:30:00 +0100 | [diff] [blame] | 763 | * We rearm the timer until we get disabled by the idle code. |
Thomas Gleixner | 79bf2bb | 2007-02-16 01:28:03 -0800 | [diff] [blame] | 764 | * Called with interrupts disabled and timer->base->cpu_base->lock held. |
| 765 | */ |
| 766 | static 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 Gleixner | 79bf2bb | 2007-02-16 01:28:03 -0800 | [diff] [blame] | 770 | struct pt_regs *regs = get_irq_regs(); |
| 771 | ktime_t now = ktime_get(); |
Thomas Gleixner | d3ed782 | 2007-05-08 00:30:03 -0700 | [diff] [blame] | 772 | 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 Gleixner | 6441402 | 2008-09-22 18:46:37 +0200 | [diff] [blame] | 782 | if (unlikely(tick_do_timer_cpu == TICK_DO_TIMER_NONE)) |
Thomas Gleixner | d3ed782 | 2007-05-08 00:30:03 -0700 | [diff] [blame] | 783 | tick_do_timer_cpu = cpu; |
| 784 | #endif |
Thomas Gleixner | 79bf2bb | 2007-02-16 01:28:03 -0800 | [diff] [blame] | 785 | |
| 786 | /* Check, if the jiffies need an update */ |
Thomas Gleixner | d3ed782 | 2007-05-08 00:30:03 -0700 | [diff] [blame] | 787 | if (tick_do_timer_cpu == cpu) |
| 788 | tick_do_update_jiffies64(now); |
Thomas Gleixner | 79bf2bb | 2007-02-16 01:28:03 -0800 | [diff] [blame] | 789 | |
| 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 Gleixner | 79bf2bb | 2007-02-16 01:28:03 -0800 | [diff] [blame] | 807 | update_process_times(user_mode(regs)); |
| 808 | profile_tick(CPU_PROFILING); |
Amar Singhal | f49d99b | 2011-08-22 19:02:04 -0700 | [diff] [blame] | 809 | |
| 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 Gleixner | 79bf2bb | 2007-02-16 01:28:03 -0800 | [diff] [blame] | 823 | } |
| 824 | |
Thomas Gleixner | 79bf2bb | 2007-02-16 01:28:03 -0800 | [diff] [blame] | 825 | 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 | */ |
| 833 | void 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 Gleixner | 79bf2bb | 2007-02-16 01:28:03 -0800 | [diff] [blame] | 843 | |
john stultz | 3704540 | 2007-07-21 04:37:35 -0700 | [diff] [blame] | 844 | /* Get the next period (per cpu) */ |
Arjan van de Ven | cc584b2 | 2008-09-01 15:02:30 -0700 | [diff] [blame] | 845 | hrtimer_set_expires(&ts->sched_timer, tick_init_jiffy_update()); |
Thomas Gleixner | 79bf2bb | 2007-02-16 01:28:03 -0800 | [diff] [blame] | 846 | |
| 847 | for (;;) { |
| 848 | hrtimer_forward(&ts->sched_timer, now, tick_period); |
Arun R Bharadwaj | 5c33386 | 2009-04-16 12:14:37 +0530 | [diff] [blame] | 849 | hrtimer_start_expires(&ts->sched_timer, |
| 850 | HRTIMER_MODE_ABS_PINNED); |
Thomas Gleixner | 79bf2bb | 2007-02-16 01:28:03 -0800 | [diff] [blame] | 851 | /* 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 Carstens | 45db69a | 2011-08-23 13:20:46 +0200 | [diff] [blame] | 858 | if (tick_nohz_enabled) |
Thomas Gleixner | 79bf2bb | 2007-02-16 01:28:03 -0800 | [diff] [blame] | 859 | ts->nohz_mode = NOHZ_MODE_HIGHRES; |
| 860 | #endif |
| 861 | } |
Miao Xie | 3c4fbe5 | 2008-08-20 16:37:38 -0700 | [diff] [blame] | 862 | #endif /* HIGH_RES_TIMERS */ |
Thomas Gleixner | 79bf2bb | 2007-02-16 01:28:03 -0800 | [diff] [blame] | 863 | |
Miao Xie | 3c4fbe5 | 2008-08-20 16:37:38 -0700 | [diff] [blame] | 864 | #if defined CONFIG_NO_HZ || defined CONFIG_HIGH_RES_TIMERS |
Thomas Gleixner | 79bf2bb | 2007-02-16 01:28:03 -0800 | [diff] [blame] | 865 | void tick_cancel_sched_timer(int cpu) |
| 866 | { |
| 867 | struct tick_sched *ts = &per_cpu(tick_cpu_sched, cpu); |
| 868 | |
Miao Xie | 3c4fbe5 | 2008-08-20 16:37:38 -0700 | [diff] [blame] | 869 | # ifdef CONFIG_HIGH_RES_TIMERS |
Thomas Gleixner | 79bf2bb | 2007-02-16 01:28:03 -0800 | [diff] [blame] | 870 | if (ts->sched_timer.base) |
| 871 | hrtimer_cancel(&ts->sched_timer); |
Miao Xie | 3c4fbe5 | 2008-08-20 16:37:38 -0700 | [diff] [blame] | 872 | # endif |
Karsten Wiese | a790176 | 2008-03-04 14:59:55 -0800 | [diff] [blame] | 873 | |
Thomas Gleixner | 79bf2bb | 2007-02-16 01:28:03 -0800 | [diff] [blame] | 874 | ts->nohz_mode = NOHZ_MODE_INACTIVE; |
| 875 | } |
Miao Xie | 3c4fbe5 | 2008-08-20 16:37:38 -0700 | [diff] [blame] | 876 | #endif |
Thomas Gleixner | 79bf2bb | 2007-02-16 01:28:03 -0800 | [diff] [blame] | 877 | |
| 878 | /** |
| 879 | * Async notification about clocksource changes |
| 880 | */ |
| 881 | void 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 | */ |
| 892 | void 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 | */ |
| 907 | int 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 Zefan | cf4fc6c | 2008-02-08 04:19:24 -0800 | [diff] [blame] | 917 | if (!timekeeping_valid_for_hres() || !tick_is_oneshot_available()) |
Thomas Gleixner | 79bf2bb | 2007-02-16 01:28:03 -0800 | [diff] [blame] | 918 | return 0; |
| 919 | |
| 920 | if (!allow_nohz) |
| 921 | return 1; |
| 922 | |
| 923 | tick_nohz_switch_to_nohz(); |
| 924 | return 0; |
| 925 | } |