tick-sched.c 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036
  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. *
  12. * Distribute under GPLv2.
  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>
  22. #include <linux/module.h>
  23. #include <linux/rq_stats.h>
  24. #include <asm/irq_regs.h>
  25. #include "tick-internal.h"
  26. struct rq_data rq_info;
  27. struct workqueue_struct *rq_wq;
  28. spinlock_t rq_lock;
  29. /*
  30. * Per cpu nohz control structure
  31. */
  32. static DEFINE_PER_CPU(struct tick_sched, tick_cpu_sched);
  33. /*
  34. * The time, when the last jiffy update happened. Protected by xtime_lock.
  35. */
  36. static ktime_t last_jiffies_update;
  37. struct tick_sched *tick_get_tick_sched(int cpu)
  38. {
  39. return &per_cpu(tick_cpu_sched, cpu);
  40. }
  41. /*
  42. * Must be called with interrupts disabled !
  43. */
  44. static void tick_do_update_jiffies64(ktime_t now)
  45. {
  46. unsigned long ticks = 0;
  47. ktime_t delta;
  48. /*
  49. * Do a quick check without holding xtime_lock:
  50. */
  51. delta = ktime_sub(now, last_jiffies_update);
  52. if (delta.tv64 < tick_period.tv64)
  53. return;
  54. /* Reevalute with xtime_lock held */
  55. write_seqlock(&xtime_lock);
  56. delta = ktime_sub(now, last_jiffies_update);
  57. if (delta.tv64 >= tick_period.tv64) {
  58. delta = ktime_sub(delta, tick_period);
  59. last_jiffies_update = ktime_add(last_jiffies_update,
  60. tick_period);
  61. /* Slow path for long timeouts */
  62. if (unlikely(delta.tv64 >= tick_period.tv64)) {
  63. s64 incr = ktime_to_ns(tick_period);
  64. ticks = ktime_divns(delta, incr);
  65. last_jiffies_update = ktime_add_ns(last_jiffies_update,
  66. incr * ticks);
  67. }
  68. do_timer(++ticks);
  69. /* Keep the tick_next_period variable up to date */
  70. tick_next_period = ktime_add(last_jiffies_update, tick_period);
  71. }
  72. write_sequnlock(&xtime_lock);
  73. }
  74. /*
  75. * Initialize and return retrieve the jiffies update.
  76. */
  77. static ktime_t tick_init_jiffy_update(void)
  78. {
  79. ktime_t period;
  80. write_seqlock(&xtime_lock);
  81. /* Did we start the jiffies update yet ? */
  82. if (last_jiffies_update.tv64 == 0)
  83. last_jiffies_update = tick_next_period;
  84. period = last_jiffies_update;
  85. write_sequnlock(&xtime_lock);
  86. return period;
  87. }
  88. /*
  89. * NOHZ - aka dynamic tick functionality
  90. */
  91. #ifdef CONFIG_NO_HZ
  92. /*
  93. * NO HZ enabled ?
  94. */
  95. int tick_nohz_enabled __read_mostly = 1;
  96. /*
  97. * Enable / Disable tickless mode
  98. */
  99. static int __init setup_tick_nohz(char *str)
  100. {
  101. if (!strcmp(str, "off"))
  102. tick_nohz_enabled = 0;
  103. else if (!strcmp(str, "on"))
  104. tick_nohz_enabled = 1;
  105. else
  106. return 0;
  107. return 1;
  108. }
  109. __setup("nohz=", setup_tick_nohz);
  110. /**
  111. * tick_nohz_update_jiffies - update jiffies when idle was interrupted
  112. *
  113. * Called from interrupt entry when the CPU was idle
  114. *
  115. * In case the sched_tick was stopped on this CPU, we have to check if jiffies
  116. * must be updated. Otherwise an interrupt handler could use a stale jiffy
  117. * value. We do this unconditionally on any cpu, as we don't know whether the
  118. * cpu, which has the update task assigned is in a long sleep.
  119. */
  120. static void tick_nohz_update_jiffies(ktime_t now)
  121. {
  122. int cpu = smp_processor_id();
  123. struct tick_sched *ts = &per_cpu(tick_cpu_sched, cpu);
  124. unsigned long flags;
  125. ts->idle_waketime = now;
  126. local_irq_save(flags);
  127. tick_do_update_jiffies64(now);
  128. local_irq_restore(flags);
  129. touch_softlockup_watchdog();
  130. }
  131. /*
  132. * Updates the per cpu time idle statistics counters
  133. */
  134. static void
  135. update_ts_time_stats(int cpu, struct tick_sched *ts, ktime_t now, u64 *last_update_time)
  136. {
  137. ktime_t delta;
  138. if (ts->idle_active) {
  139. delta = ktime_sub(now, ts->idle_entrytime);
  140. if (nr_iowait_cpu(cpu) > 0)
  141. ts->iowait_sleeptime = ktime_add(ts->iowait_sleeptime, delta);
  142. else
  143. ts->idle_sleeptime = ktime_add(ts->idle_sleeptime, delta);
  144. ts->idle_entrytime = now;
  145. }
  146. if (last_update_time)
  147. *last_update_time = ktime_to_us(now);
  148. }
  149. static void tick_nohz_stop_idle(int cpu, ktime_t now)
  150. {
  151. struct tick_sched *ts = &per_cpu(tick_cpu_sched, cpu);
  152. update_ts_time_stats(cpu, ts, now, NULL);
  153. ts->idle_active = 0;
  154. sched_clock_idle_wakeup_event(0);
  155. }
  156. static ktime_t tick_nohz_start_idle(int cpu, struct tick_sched *ts)
  157. {
  158. ktime_t now = ktime_get();
  159. ts->idle_entrytime = now;
  160. ts->idle_active = 1;
  161. sched_clock_idle_sleep_event();
  162. return now;
  163. }
  164. /**
  165. * get_cpu_idle_time_us - get the total idle time of a cpu
  166. * @cpu: CPU number to query
  167. * @last_update_time: variable to store update time in. Do not update
  168. * counters if NULL.
  169. *
  170. * Return the cummulative idle time (since boot) for a given
  171. * CPU, in microseconds.
  172. *
  173. * This time is measured via accounting rather than sampling,
  174. * and is as accurate as ktime_get() is.
  175. *
  176. * This function returns -1 if NOHZ is not enabled.
  177. */
  178. u64 get_cpu_idle_time_us(int cpu, u64 *last_update_time)
  179. {
  180. struct tick_sched *ts = &per_cpu(tick_cpu_sched, cpu);
  181. ktime_t now, idle;
  182. if (!tick_nohz_enabled)
  183. return -1;
  184. now = ktime_get();
  185. if (last_update_time) {
  186. update_ts_time_stats(cpu, ts, now, last_update_time);
  187. idle = ts->idle_sleeptime;
  188. } else {
  189. if (ts->idle_active && !nr_iowait_cpu(cpu)) {
  190. ktime_t delta = ktime_sub(now, ts->idle_entrytime);
  191. idle = ktime_add(ts->idle_sleeptime, delta);
  192. } else {
  193. idle = ts->idle_sleeptime;
  194. }
  195. }
  196. return ktime_to_us(idle);
  197. }
  198. EXPORT_SYMBOL_GPL(get_cpu_idle_time_us);
  199. /**
  200. * get_cpu_iowait_time_us - get the total iowait time of a cpu
  201. * @cpu: CPU number to query
  202. * @last_update_time: variable to store update time in. Do not update
  203. * counters if NULL.
  204. *
  205. * Return the cummulative iowait time (since boot) for a given
  206. * CPU, in microseconds.
  207. *
  208. * This time is measured via accounting rather than sampling,
  209. * and is as accurate as ktime_get() is.
  210. *
  211. * This function returns -1 if NOHZ is not enabled.
  212. */
  213. u64 get_cpu_iowait_time_us(int cpu, u64 *last_update_time)
  214. {
  215. struct tick_sched *ts = &per_cpu(tick_cpu_sched, cpu);
  216. ktime_t now, iowait;
  217. if (!tick_nohz_enabled)
  218. return -1;
  219. now = ktime_get();
  220. if (last_update_time) {
  221. update_ts_time_stats(cpu, ts, now, last_update_time);
  222. iowait = ts->iowait_sleeptime;
  223. } else {
  224. if (ts->idle_active && nr_iowait_cpu(cpu) > 0) {
  225. ktime_t delta = ktime_sub(now, ts->idle_entrytime);
  226. iowait = ktime_add(ts->iowait_sleeptime, delta);
  227. } else {
  228. iowait = ts->iowait_sleeptime;
  229. }
  230. }
  231. return ktime_to_us(iowait);
  232. }
  233. EXPORT_SYMBOL_GPL(get_cpu_iowait_time_us);
  234. static ktime_t tick_nohz_stop_sched_tick(struct tick_sched *ts,
  235. ktime_t now, int cpu)
  236. {
  237. unsigned long seq, last_jiffies, next_jiffies, delta_jiffies;
  238. unsigned long rcu_delta_jiffies;
  239. ktime_t last_update, expires, ret = { .tv64 = 0 };
  240. struct clock_event_device *dev = __get_cpu_var(tick_cpu_device).evtdev;
  241. u64 time_delta;
  242. /* Read jiffies and the time when jiffies were updated last */
  243. do {
  244. seq = read_seqbegin(&xtime_lock);
  245. last_update = last_jiffies_update;
  246. last_jiffies = jiffies;
  247. time_delta = timekeeping_max_deferment();
  248. } while (read_seqretry(&xtime_lock, seq));
  249. if (rcu_needs_cpu(cpu, &rcu_delta_jiffies) || printk_needs_cpu(cpu) ||
  250. arch_needs_cpu(cpu)) {
  251. next_jiffies = last_jiffies + 1;
  252. delta_jiffies = 1;
  253. } else {
  254. /* Get the next timer wheel timer */
  255. next_jiffies = get_next_timer_interrupt(last_jiffies);
  256. delta_jiffies = next_jiffies - last_jiffies;
  257. if (rcu_delta_jiffies < delta_jiffies) {
  258. next_jiffies = last_jiffies + rcu_delta_jiffies;
  259. delta_jiffies = rcu_delta_jiffies;
  260. }
  261. }
  262. /*
  263. * Do not stop the tick, if we are only one off (or less)
  264. * or if the cpu is required for RCU:
  265. */
  266. if (!ts->tick_stopped && delta_jiffies <= 1)
  267. goto out;
  268. /* Schedule the tick, if we are at least one jiffie off */
  269. if ((long)delta_jiffies >= 1) {
  270. /*
  271. * If this cpu is the one which updates jiffies, then
  272. * give up the assignment and let it be taken by the
  273. * cpu which runs the tick timer next, which might be
  274. * this cpu as well. If we don't drop this here the
  275. * jiffies might be stale and do_timer() never
  276. * invoked. Keep track of the fact that it was the one
  277. * which had the do_timer() duty last. If this cpu is
  278. * the one which had the do_timer() duty last, we
  279. * limit the sleep time to the timekeeping
  280. * max_deferement value which we retrieved
  281. * above. Otherwise we can sleep as long as we want.
  282. */
  283. if (cpu == tick_do_timer_cpu) {
  284. tick_do_timer_cpu = TICK_DO_TIMER_NONE;
  285. ts->do_timer_last = 1;
  286. } else if (tick_do_timer_cpu != TICK_DO_TIMER_NONE) {
  287. time_delta = KTIME_MAX;
  288. ts->do_timer_last = 0;
  289. } else if (!ts->do_timer_last) {
  290. time_delta = KTIME_MAX;
  291. }
  292. /*
  293. * calculate the expiry time for the next timer wheel
  294. * timer. delta_jiffies >= NEXT_TIMER_MAX_DELTA signals
  295. * that there is no timer pending or at least extremely
  296. * far into the future (12 days for HZ=1000). In this
  297. * case we set the expiry to the end of time.
  298. */
  299. if (likely(delta_jiffies < NEXT_TIMER_MAX_DELTA)) {
  300. /*
  301. * Calculate the time delta for the next timer event.
  302. * If the time delta exceeds the maximum time delta
  303. * permitted by the current clocksource then adjust
  304. * the time delta accordingly to ensure the
  305. * clocksource does not wrap.
  306. */
  307. time_delta = min_t(u64, time_delta,
  308. tick_period.tv64 * delta_jiffies);
  309. }
  310. if (time_delta < KTIME_MAX)
  311. expires = ktime_add_ns(last_update, time_delta);
  312. else
  313. expires.tv64 = KTIME_MAX;
  314. /* Skip reprogram of event if its not changed */
  315. if (ts->tick_stopped && ktime_equal(expires, dev->next_event))
  316. goto out;
  317. ret = expires;
  318. /*
  319. * nohz_stop_sched_tick can be called several times before
  320. * the nohz_restart_sched_tick is called. This happens when
  321. * interrupts arrive which do not cause a reschedule. In the
  322. * first call we save the current tick time, so we can restart
  323. * the scheduler tick in nohz_restart_sched_tick.
  324. */
  325. if (!ts->tick_stopped) {
  326. nohz_balance_enter_idle(cpu);
  327. calc_load_enter_idle();
  328. ts->last_tick = hrtimer_get_expires(&ts->sched_timer);
  329. ts->tick_stopped = 1;
  330. }
  331. /*
  332. * If the expiration time == KTIME_MAX, then
  333. * in this case we simply stop the tick timer.
  334. */
  335. if (unlikely(expires.tv64 == KTIME_MAX)) {
  336. if (ts->nohz_mode == NOHZ_MODE_HIGHRES)
  337. hrtimer_cancel(&ts->sched_timer);
  338. goto out;
  339. }
  340. if (ts->nohz_mode == NOHZ_MODE_HIGHRES) {
  341. hrtimer_start(&ts->sched_timer, expires,
  342. HRTIMER_MODE_ABS_PINNED);
  343. /* Check, if the timer was already in the past */
  344. if (hrtimer_active(&ts->sched_timer))
  345. goto out;
  346. } else if (!tick_program_event(expires, 0))
  347. goto out;
  348. /*
  349. * We are past the event already. So we crossed a
  350. * jiffie boundary. Update jiffies and raise the
  351. * softirq.
  352. */
  353. tick_do_update_jiffies64(ktime_get());
  354. }
  355. raise_softirq_irqoff(TIMER_SOFTIRQ);
  356. out:
  357. ts->next_jiffies = next_jiffies;
  358. ts->last_jiffies = last_jiffies;
  359. return ret;
  360. }
  361. static bool can_stop_idle_tick(int cpu, struct tick_sched *ts)
  362. {
  363. /*
  364. * If this cpu is offline and it is the one which updates
  365. * jiffies, then give up the assignment and let it be taken by
  366. * the cpu which runs the tick timer next. If we don't drop
  367. * this here the jiffies might be stale and do_timer() never
  368. * invoked.
  369. */
  370. if (unlikely(!cpu_online(cpu))) {
  371. if (cpu == tick_do_timer_cpu)
  372. tick_do_timer_cpu = TICK_DO_TIMER_NONE;
  373. }
  374. if (unlikely(ts->nohz_mode == NOHZ_MODE_INACTIVE))
  375. return false;
  376. if (need_resched())
  377. return false;
  378. if (unlikely(local_softirq_pending() && cpu_online(cpu))) {
  379. static int ratelimit;
  380. if (ratelimit < 10) {
  381. printk(KERN_ERR "NOHZ: local_softirq_pending %02x\n",
  382. (unsigned int) local_softirq_pending());
  383. ratelimit++;
  384. }
  385. return false;
  386. }
  387. return true;
  388. }
  389. static void __tick_nohz_idle_enter(struct tick_sched *ts)
  390. {
  391. ktime_t now, expires;
  392. int cpu = smp_processor_id();
  393. now = tick_nohz_start_idle(cpu, ts);
  394. if (can_stop_idle_tick(cpu, ts)) {
  395. int was_stopped = ts->tick_stopped;
  396. ts->idle_calls++;
  397. expires = tick_nohz_stop_sched_tick(ts, now, cpu);
  398. if (expires.tv64 > 0LL) {
  399. ts->idle_sleeps++;
  400. ts->idle_expires = expires;
  401. }
  402. if (!was_stopped && ts->tick_stopped)
  403. ts->idle_jiffies = ts->last_jiffies;
  404. }
  405. }
  406. /**
  407. * tick_nohz_idle_enter - stop the idle tick from the idle task
  408. *
  409. * When the next event is more than a tick into the future, stop the idle tick
  410. * Called when we start the idle loop.
  411. *
  412. * The arch is responsible of calling:
  413. *
  414. * - rcu_idle_enter() after its last use of RCU before the CPU is put
  415. * to sleep.
  416. * - rcu_idle_exit() before the first use of RCU after the CPU is woken up.
  417. */
  418. void tick_nohz_idle_enter(void)
  419. {
  420. struct tick_sched *ts;
  421. WARN_ON_ONCE(irqs_disabled());
  422. /*
  423. * Update the idle state in the scheduler domain hierarchy
  424. * when tick_nohz_stop_sched_tick() is called from the idle loop.
  425. * State will be updated to busy during the first busy tick after
  426. * exiting idle.
  427. */
  428. set_cpu_sd_state_idle();
  429. local_irq_disable();
  430. ts = &__get_cpu_var(tick_cpu_sched);
  431. /*
  432. * set ts->inidle unconditionally. even if the system did not
  433. * switch to nohz mode the cpu frequency governers rely on the
  434. * update of the idle time accounting in tick_nohz_start_idle().
  435. */
  436. ts->inidle = 1;
  437. __tick_nohz_idle_enter(ts);
  438. local_irq_enable();
  439. }
  440. /**
  441. * tick_nohz_irq_exit - update next tick event from interrupt exit
  442. *
  443. * When an interrupt fires while we are idle and it doesn't cause
  444. * a reschedule, it may still add, modify or delete a timer, enqueue
  445. * an RCU callback, etc...
  446. * So we need to re-calculate and reprogram the next tick event.
  447. */
  448. void tick_nohz_irq_exit(void)
  449. {
  450. unsigned long flags;
  451. struct tick_sched *ts = &__get_cpu_var(tick_cpu_sched);
  452. if (!ts->inidle)
  453. return;
  454. local_irq_save(flags);
  455. /* Cancel the timer because CPU already waken up from the C-states*/
  456. menu_hrtimer_cancel();
  457. __tick_nohz_idle_enter(ts);
  458. local_irq_restore(flags);
  459. }
  460. /**
  461. * tick_nohz_get_sleep_length - return the length of the current sleep
  462. *
  463. * Called from power state control code with interrupts disabled
  464. */
  465. ktime_t tick_nohz_get_sleep_length(void)
  466. {
  467. struct tick_sched *ts = &__get_cpu_var(tick_cpu_sched);
  468. struct clock_event_device *dev = __get_cpu_var(tick_cpu_device).evtdev;
  469. return ktime_sub(dev->next_event, ts->idle_entrytime);
  470. }
  471. static void tick_nohz_restart(struct tick_sched *ts, ktime_t now)
  472. {
  473. hrtimer_cancel(&ts->sched_timer);
  474. hrtimer_set_expires(&ts->sched_timer, ts->last_tick);
  475. while (1) {
  476. /* Forward the time to expire in the future */
  477. hrtimer_forward(&ts->sched_timer, now, tick_period);
  478. if (ts->nohz_mode == NOHZ_MODE_HIGHRES) {
  479. hrtimer_start_expires(&ts->sched_timer,
  480. HRTIMER_MODE_ABS_PINNED);
  481. /* Check, if the timer was already in the past */
  482. if (hrtimer_active(&ts->sched_timer))
  483. break;
  484. } else {
  485. if (!tick_program_event(
  486. hrtimer_get_expires(&ts->sched_timer), 0))
  487. break;
  488. }
  489. /* Reread time and update jiffies */
  490. now = ktime_get();
  491. tick_do_update_jiffies64(now);
  492. }
  493. }
  494. static void tick_nohz_restart_sched_tick(struct tick_sched *ts, ktime_t now)
  495. {
  496. /* Update jiffies first */
  497. tick_do_update_jiffies64(now);
  498. update_cpu_load_nohz();
  499. calc_load_exit_idle();
  500. touch_softlockup_watchdog();
  501. /*
  502. * Cancel the scheduled timer and restore the tick
  503. */
  504. ts->tick_stopped = 0;
  505. ts->idle_exittime = now;
  506. tick_nohz_restart(ts, now);
  507. }
  508. static void tick_nohz_account_idle_ticks(struct tick_sched *ts)
  509. {
  510. #ifndef CONFIG_VIRT_CPU_ACCOUNTING
  511. unsigned long ticks;
  512. /*
  513. * We stopped the tick in idle. Update process times would miss the
  514. * time we slept as update_process_times does only a 1 tick
  515. * accounting. Enforce that this is accounted to idle !
  516. */
  517. ticks = jiffies - ts->idle_jiffies;
  518. /*
  519. * We might be one off. Do not randomly account a huge number of ticks!
  520. */
  521. if (ticks && ticks < LONG_MAX)
  522. account_idle_ticks(ticks);
  523. #endif
  524. }
  525. /**
  526. * tick_nohz_idle_exit - restart the idle tick from the idle task
  527. *
  528. * Restart the idle tick when the CPU is woken up from idle
  529. * This also exit the RCU extended quiescent state. The CPU
  530. * can use RCU again after this function is called.
  531. */
  532. void tick_nohz_idle_exit(void)
  533. {
  534. int cpu = smp_processor_id();
  535. struct tick_sched *ts = &per_cpu(tick_cpu_sched, cpu);
  536. ktime_t now;
  537. local_irq_disable();
  538. WARN_ON_ONCE(!ts->inidle);
  539. ts->inidle = 0;
  540. /* Cancel the timer because CPU already waken up from the C-states*/
  541. menu_hrtimer_cancel();
  542. if (ts->idle_active || ts->tick_stopped)
  543. now = ktime_get();
  544. if (ts->idle_active)
  545. tick_nohz_stop_idle(cpu, now);
  546. if (ts->tick_stopped) {
  547. tick_nohz_restart_sched_tick(ts, now);
  548. tick_nohz_account_idle_ticks(ts);
  549. }
  550. local_irq_enable();
  551. }
  552. static int tick_nohz_reprogram(struct tick_sched *ts, ktime_t now)
  553. {
  554. hrtimer_forward(&ts->sched_timer, now, tick_period);
  555. return tick_program_event(hrtimer_get_expires(&ts->sched_timer), 0);
  556. }
  557. /*
  558. * The nohz low res interrupt handler
  559. */
  560. static void tick_nohz_handler(struct clock_event_device *dev)
  561. {
  562. struct tick_sched *ts = &__get_cpu_var(tick_cpu_sched);
  563. struct pt_regs *regs = get_irq_regs();
  564. int cpu = smp_processor_id();
  565. ktime_t now = ktime_get();
  566. dev->next_event.tv64 = KTIME_MAX;
  567. /*
  568. * Check if the do_timer duty was dropped. We don't care about
  569. * concurrency: This happens only when the cpu in charge went
  570. * into a long sleep. If two cpus happen to assign themself to
  571. * this duty, then the jiffies update is still serialized by
  572. * xtime_lock.
  573. */
  574. if (unlikely(tick_do_timer_cpu == TICK_DO_TIMER_NONE))
  575. tick_do_timer_cpu = cpu;
  576. /* Check, if the jiffies need an update */
  577. if (tick_do_timer_cpu == cpu)
  578. tick_do_update_jiffies64(now);
  579. /*
  580. * When we are idle and the tick is stopped, we have to touch
  581. * the watchdog as we might not schedule for a really long
  582. * time. This happens on complete idle SMP systems while
  583. * waiting on the login prompt. We also increment the "start
  584. * of idle" jiffy stamp so the idle accounting adjustment we
  585. * do when we go busy again does not account too much ticks.
  586. */
  587. if (ts->tick_stopped) {
  588. touch_softlockup_watchdog();
  589. ts->idle_jiffies++;
  590. }
  591. update_process_times(user_mode(regs));
  592. profile_tick(CPU_PROFILING);
  593. while (tick_nohz_reprogram(ts, now)) {
  594. now = ktime_get();
  595. tick_do_update_jiffies64(now);
  596. }
  597. }
  598. /**
  599. * tick_nohz_switch_to_nohz - switch to nohz mode
  600. */
  601. static void tick_nohz_switch_to_nohz(void)
  602. {
  603. struct tick_sched *ts = &__get_cpu_var(tick_cpu_sched);
  604. ktime_t next;
  605. if (!tick_nohz_enabled)
  606. return;
  607. local_irq_disable();
  608. if (tick_switch_to_oneshot(tick_nohz_handler)) {
  609. local_irq_enable();
  610. return;
  611. }
  612. ts->nohz_mode = NOHZ_MODE_LOWRES;
  613. /*
  614. * Recycle the hrtimer in ts, so we can share the
  615. * hrtimer_forward with the highres code.
  616. */
  617. hrtimer_init(&ts->sched_timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
  618. /* Get the next period */
  619. next = tick_init_jiffy_update();
  620. for (;;) {
  621. hrtimer_set_expires(&ts->sched_timer, next);
  622. if (!tick_program_event(next, 0))
  623. break;
  624. next = ktime_add(next, tick_period);
  625. }
  626. local_irq_enable();
  627. }
  628. /*
  629. * When NOHZ is enabled and the tick is stopped, we need to kick the
  630. * tick timer from irq_enter() so that the jiffies update is kept
  631. * alive during long running softirqs. That's ugly as hell, but
  632. * correctness is key even if we need to fix the offending softirq in
  633. * the first place.
  634. *
  635. * Note, this is different to tick_nohz_restart. We just kick the
  636. * timer and do not touch the other magic bits which need to be done
  637. * when idle is left.
  638. */
  639. static void tick_nohz_kick_tick(int cpu, ktime_t now)
  640. {
  641. #if 0
  642. /* Switch back to 2.6.27 behaviour */
  643. struct tick_sched *ts = &per_cpu(tick_cpu_sched, cpu);
  644. ktime_t delta;
  645. /*
  646. * Do not touch the tick device, when the next expiry is either
  647. * already reached or less/equal than the tick period.
  648. */
  649. delta = ktime_sub(hrtimer_get_expires(&ts->sched_timer), now);
  650. if (delta.tv64 <= tick_period.tv64)
  651. return;
  652. tick_nohz_restart(ts, now);
  653. #endif
  654. }
  655. static inline void tick_check_nohz(int cpu)
  656. {
  657. struct tick_sched *ts = &per_cpu(tick_cpu_sched, cpu);
  658. ktime_t now;
  659. if (!ts->idle_active && !ts->tick_stopped)
  660. return;
  661. now = ktime_get();
  662. if (ts->idle_active)
  663. tick_nohz_stop_idle(cpu, now);
  664. if (ts->tick_stopped) {
  665. tick_nohz_update_jiffies(now);
  666. tick_nohz_kick_tick(cpu, now);
  667. }
  668. }
  669. #else
  670. static inline void tick_nohz_switch_to_nohz(void) { }
  671. static inline void tick_check_nohz(int cpu) { }
  672. #endif /* NO_HZ */
  673. /*
  674. * Called from irq_enter to notify about the possible interruption of idle()
  675. */
  676. void tick_check_idle(int cpu)
  677. {
  678. tick_check_oneshot_broadcast(cpu);
  679. tick_check_nohz(cpu);
  680. }
  681. /*
  682. * High resolution timer specific code
  683. */
  684. #ifdef CONFIG_HIGH_RES_TIMERS
  685. static void update_rq_stats(void)
  686. {
  687. unsigned long jiffy_gap = 0;
  688. unsigned int rq_avg = 0;
  689. unsigned long flags = 0;
  690. jiffy_gap = jiffies - rq_info.rq_poll_last_jiffy;
  691. if (jiffy_gap >= rq_info.rq_poll_jiffies) {
  692. spin_lock_irqsave(&rq_lock, flags);
  693. if (!rq_info.rq_avg)
  694. rq_info.rq_poll_total_jiffies = 0;
  695. rq_avg = nr_running() * 10;
  696. if (rq_info.rq_poll_total_jiffies) {
  697. rq_avg = (rq_avg * jiffy_gap) +
  698. (rq_info.rq_avg *
  699. rq_info.rq_poll_total_jiffies);
  700. do_div(rq_avg,
  701. rq_info.rq_poll_total_jiffies + jiffy_gap);
  702. }
  703. rq_info.rq_avg = rq_avg;
  704. rq_info.rq_poll_total_jiffies += jiffy_gap;
  705. rq_info.rq_poll_last_jiffy = jiffies;
  706. spin_unlock_irqrestore(&rq_lock, flags);
  707. }
  708. }
  709. static void wakeup_user(void)
  710. {
  711. unsigned long jiffy_gap;
  712. jiffy_gap = jiffies - rq_info.def_timer_last_jiffy;
  713. if (jiffy_gap >= rq_info.def_timer_jiffies) {
  714. rq_info.def_timer_last_jiffy = jiffies;
  715. queue_work(rq_wq, &rq_info.def_timer_work);
  716. }
  717. }
  718. /*
  719. * We rearm the timer until we get disabled by the idle code.
  720. * Called with interrupts disabled and timer->base->cpu_base->lock held.
  721. */
  722. static enum hrtimer_restart tick_sched_timer(struct hrtimer *timer)
  723. {
  724. struct tick_sched *ts =
  725. container_of(timer, struct tick_sched, sched_timer);
  726. struct pt_regs *regs = get_irq_regs();
  727. ktime_t now = ktime_get();
  728. int cpu = smp_processor_id();
  729. #ifdef CONFIG_NO_HZ
  730. /*
  731. * Check if the do_timer duty was dropped. We don't care about
  732. * concurrency: This happens only when the cpu in charge went
  733. * into a long sleep. If two cpus happen to assign themself to
  734. * this duty, then the jiffies update is still serialized by
  735. * xtime_lock.
  736. */
  737. if (unlikely(tick_do_timer_cpu == TICK_DO_TIMER_NONE))
  738. tick_do_timer_cpu = cpu;
  739. #endif
  740. /* Check, if the jiffies need an update */
  741. if (tick_do_timer_cpu == cpu)
  742. tick_do_update_jiffies64(now);
  743. /*
  744. * Do not call, when we are not in irq context and have
  745. * no valid regs pointer
  746. */
  747. if (regs) {
  748. /*
  749. * When we are idle and the tick is stopped, we have to touch
  750. * the watchdog as we might not schedule for a really long
  751. * time. This happens on complete idle SMP systems while
  752. * waiting on the login prompt. We also increment the "start of
  753. * idle" jiffy stamp so the idle accounting adjustment we do
  754. * when we go busy again does not account too much ticks.
  755. */
  756. if (ts->tick_stopped) {
  757. touch_softlockup_watchdog();
  758. if (is_idle_task(current))
  759. ts->idle_jiffies++;
  760. }
  761. update_process_times(user_mode(regs));
  762. profile_tick(CPU_PROFILING);
  763. if ((rq_info.init == 1) && (tick_do_timer_cpu == cpu)) {
  764. /*
  765. * update run queue statistics
  766. */
  767. update_rq_stats();
  768. /*
  769. * wakeup user if needed
  770. */
  771. wakeup_user();
  772. }
  773. }
  774. hrtimer_forward(timer, now, tick_period);
  775. return HRTIMER_RESTART;
  776. }
  777. static int sched_skew_tick;
  778. static int __init skew_tick(char *str)
  779. {
  780. get_option(&str, &sched_skew_tick);
  781. return 0;
  782. }
  783. early_param("skew_tick", skew_tick);
  784. /**
  785. * tick_setup_sched_timer - setup the tick emulation timer
  786. */
  787. void tick_setup_sched_timer(void)
  788. {
  789. struct tick_sched *ts = &__get_cpu_var(tick_cpu_sched);
  790. ktime_t now = ktime_get();
  791. /*
  792. * Emulate tick processing via per-CPU hrtimers:
  793. */
  794. hrtimer_init(&ts->sched_timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
  795. ts->sched_timer.function = tick_sched_timer;
  796. /* Get the next period (per cpu) */
  797. hrtimer_set_expires(&ts->sched_timer, tick_init_jiffy_update());
  798. /* Offset the tick to avert xtime_lock contention. */
  799. if (sched_skew_tick) {
  800. u64 offset = ktime_to_ns(tick_period) >> 1;
  801. do_div(offset, num_possible_cpus());
  802. offset *= smp_processor_id();
  803. hrtimer_add_expires_ns(&ts->sched_timer, offset);
  804. }
  805. for (;;) {
  806. hrtimer_forward(&ts->sched_timer, now, tick_period);
  807. hrtimer_start_expires(&ts->sched_timer,
  808. HRTIMER_MODE_ABS_PINNED);
  809. /* Check, if the timer was already in the past */
  810. if (hrtimer_active(&ts->sched_timer))
  811. break;
  812. now = ktime_get();
  813. }
  814. #ifdef CONFIG_NO_HZ
  815. if (tick_nohz_enabled)
  816. ts->nohz_mode = NOHZ_MODE_HIGHRES;
  817. #endif
  818. }
  819. #endif /* HIGH_RES_TIMERS */
  820. #if defined CONFIG_NO_HZ || defined CONFIG_HIGH_RES_TIMERS
  821. void tick_cancel_sched_timer(int cpu)
  822. {
  823. struct tick_sched *ts = &per_cpu(tick_cpu_sched, cpu);
  824. # ifdef CONFIG_HIGH_RES_TIMERS
  825. if (ts->sched_timer.base)
  826. hrtimer_cancel(&ts->sched_timer);
  827. # endif
  828. memset(ts, 0, sizeof(*ts));
  829. }
  830. #endif
  831. /**
  832. * Async notification about clocksource changes
  833. */
  834. void tick_clock_notify(void)
  835. {
  836. int cpu;
  837. for_each_possible_cpu(cpu)
  838. set_bit(0, &per_cpu(tick_cpu_sched, cpu).check_clocks);
  839. }
  840. /*
  841. * Async notification about clock event changes
  842. */
  843. void tick_oneshot_notify(void)
  844. {
  845. struct tick_sched *ts = &__get_cpu_var(tick_cpu_sched);
  846. set_bit(0, &ts->check_clocks);
  847. }
  848. /**
  849. * Check, if a change happened, which makes oneshot possible.
  850. *
  851. * Called cyclic from the hrtimer softirq (driven by the timer
  852. * softirq) allow_nohz signals, that we can switch into low-res nohz
  853. * mode, because high resolution timers are disabled (either compile
  854. * or runtime).
  855. */
  856. int tick_check_oneshot_change(int allow_nohz)
  857. {
  858. struct tick_sched *ts = &__get_cpu_var(tick_cpu_sched);
  859. if (!test_and_clear_bit(0, &ts->check_clocks))
  860. return 0;
  861. if (ts->nohz_mode != NOHZ_MODE_INACTIVE)
  862. return 0;
  863. if (!timekeeping_valid_for_hres() || !tick_is_oneshot_available())
  864. return 0;
  865. if (!allow_nohz)
  866. return 1;
  867. tick_nohz_switch_to_nohz();
  868. return 0;
  869. }