NO_HZ.txt 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. NO_HZ: Reducing Scheduling-Clock Ticks
  2. This document describes Kconfig options and boot parameters that can
  3. reduce the number of scheduling-clock interrupts, thereby improving energy
  4. efficiency and reducing OS jitter. Reducing OS jitter is important for
  5. some types of computationally intensive high-performance computing (HPC)
  6. applications and for real-time applications.
  7. There are two main contexts in which the number of scheduling-clock
  8. interrupts can be reduced compared to the old-school approach of sending
  9. a scheduling-clock interrupt to all CPUs every jiffy whether they need
  10. it or not (CONFIG_HZ_PERIODIC=y or CONFIG_NO_HZ=n for older kernels):
  11. 1. Idle CPUs (CONFIG_NO_HZ_IDLE=y or CONFIG_NO_HZ=y for older kernels).
  12. 2. CPUs having only one runnable task (CONFIG_NO_HZ_FULL=y).
  13. These two cases are described in the following two sections, followed
  14. by a third section on RCU-specific considerations and a fourth and final
  15. section listing known issues.
  16. IDLE CPUs
  17. If a CPU is idle, there is little point in sending it a scheduling-clock
  18. interrupt. After all, the primary purpose of a scheduling-clock interrupt
  19. is to force a busy CPU to shift its attention among multiple duties,
  20. and an idle CPU has no duties to shift its attention among.
  21. The CONFIG_NO_HZ_IDLE=y Kconfig option causes the kernel to avoid sending
  22. scheduling-clock interrupts to idle CPUs, which is critically important
  23. both to battery-powered devices and to highly virtualized mainframes.
  24. A battery-powered device running a CONFIG_HZ_PERIODIC=y kernel would
  25. drain its battery very quickly, easily 2-3 times as fast as would the
  26. same device running a CONFIG_NO_HZ_IDLE=y kernel. A mainframe running
  27. 1,500 OS instances might find that half of its CPU time was consumed by
  28. unnecessary scheduling-clock interrupts. In these situations, there
  29. is strong motivation to avoid sending scheduling-clock interrupts to
  30. idle CPUs. That said, dyntick-idle mode is not free:
  31. 1. It increases the number of instructions executed on the path
  32. to and from the idle loop.
  33. 2. On many architectures, dyntick-idle mode also increases the
  34. number of expensive clock-reprogramming operations.
  35. Therefore, systems with aggressive real-time response constraints often
  36. run CONFIG_HZ_PERIODIC=y kernels (or CONFIG_NO_HZ=n for older kernels)
  37. in order to avoid degrading from-idle transition latencies.
  38. An idle CPU that is not receiving scheduling-clock interrupts is said to
  39. be "dyntick-idle", "in dyntick-idle mode", "in nohz mode", or "running
  40. tickless". The remainder of this document will use "dyntick-idle mode".
  41. There is also a boot parameter "nohz=" that can be used to disable
  42. dyntick-idle mode in CONFIG_NO_HZ_IDLE=y kernels by specifying "nohz=off".
  43. By default, CONFIG_NO_HZ_IDLE=y kernels boot with "nohz=on", enabling
  44. dyntick-idle mode.
  45. CPUs WITH ONLY ONE RUNNABLE TASK
  46. If a CPU has only one runnable task, there is little point in sending it
  47. a scheduling-clock interrupt because there is no other task to switch to.
  48. The CONFIG_NO_HZ_FULL=y Kconfig option causes the kernel to avoid
  49. sending scheduling-clock interrupts to CPUs with a single runnable task,
  50. and such CPUs are said to be "adaptive-ticks CPUs". This is important
  51. for applications with aggressive real-time response constraints because
  52. it allows them to improve their worst-case response times by the maximum
  53. duration of a scheduling-clock interrupt. It is also important for
  54. computationally intensive short-iteration workloads: If any CPU is
  55. delayed during a given iteration, all the other CPUs will be forced to
  56. wait idle while the delayed CPU finishes. Thus, the delay is multiplied
  57. by one less than the number of CPUs. In these situations, there is
  58. again strong motivation to avoid sending scheduling-clock interrupts.
  59. By default, no CPU will be an adaptive-ticks CPU. The "nohz_full="
  60. boot parameter specifies the adaptive-ticks CPUs. For example,
  61. "nohz_full=1,6-8" says that CPUs 1, 6, 7, and 8 are to be adaptive-ticks
  62. CPUs. Note that you are prohibited from marking all of the CPUs as
  63. adaptive-tick CPUs: At least one non-adaptive-tick CPU must remain
  64. online to handle timekeeping tasks in order to ensure that system calls
  65. like gettimeofday() returns accurate values on adaptive-tick CPUs.
  66. (This is not an issue for CONFIG_NO_HZ_IDLE=y because there are no
  67. running user processes to observe slight drifts in clock rate.)
  68. Therefore, the boot CPU is prohibited from entering adaptive-ticks
  69. mode. Specifying a "nohz_full=" mask that includes the boot CPU will
  70. result in a boot-time error message, and the boot CPU will be removed
  71. from the mask.
  72. Alternatively, the CONFIG_NO_HZ_FULL_ALL=y Kconfig parameter specifies
  73. that all CPUs other than the boot CPU are adaptive-ticks CPUs. This
  74. Kconfig parameter will be overridden by the "nohz_full=" boot parameter,
  75. so that if both the CONFIG_NO_HZ_FULL_ALL=y Kconfig parameter and
  76. the "nohz_full=1" boot parameter is specified, the boot parameter will
  77. prevail so that only CPU 1 will be an adaptive-ticks CPU.
  78. Finally, adaptive-ticks CPUs must have their RCU callbacks offloaded.
  79. This is covered in the "RCU IMPLICATIONS" section below.
  80. Normally, a CPU remains in adaptive-ticks mode as long as possible.
  81. In particular, transitioning to kernel mode does not automatically change
  82. the mode. Instead, the CPU will exit adaptive-ticks mode only if needed,
  83. for example, if that CPU enqueues an RCU callback.
  84. Just as with dyntick-idle mode, the benefits of adaptive-tick mode do
  85. not come for free:
  86. 1. CONFIG_NO_HZ_FULL selects CONFIG_NO_HZ_COMMON, so you cannot run
  87. adaptive ticks without also running dyntick idle. This dependency
  88. extends down into the implementation, so that all of the costs
  89. of CONFIG_NO_HZ_IDLE are also incurred by CONFIG_NO_HZ_FULL.
  90. 2. The user/kernel transitions are slightly more expensive due
  91. to the need to inform kernel subsystems (such as RCU) about
  92. the change in mode.
  93. 3. POSIX CPU timers on adaptive-tick CPUs may miss their deadlines
  94. (perhaps indefinitely) because they currently rely on
  95. scheduling-tick interrupts. This will likely be fixed in
  96. one of two ways: (1) Prevent CPUs with POSIX CPU timers from
  97. entering adaptive-tick mode, or (2) Use hrtimers or other
  98. adaptive-ticks-immune mechanism to cause the POSIX CPU timer to
  99. fire properly.
  100. 4. If there are more perf events pending than the hardware can
  101. accommodate, they are normally round-robined so as to collect
  102. all of them over time. Adaptive-tick mode may prevent this
  103. round-robining from happening. This will likely be fixed by
  104. preventing CPUs with large numbers of perf events pending from
  105. entering adaptive-tick mode.
  106. 5. Scheduler statistics for adaptive-tick CPUs may be computed
  107. slightly differently than those for non-adaptive-tick CPUs.
  108. This might in turn perturb load-balancing of real-time tasks.
  109. 6. The LB_BIAS scheduler feature is disabled by adaptive ticks.
  110. Although improvements are expected over time, adaptive ticks is quite
  111. useful for many types of real-time and compute-intensive applications.
  112. However, the drawbacks listed above mean that adaptive ticks should not
  113. (yet) be enabled by default.
  114. RCU IMPLICATIONS
  115. There are situations in which idle CPUs cannot be permitted to
  116. enter either dyntick-idle mode or adaptive-tick mode, the most
  117. common being when that CPU has RCU callbacks pending.
  118. The CONFIG_RCU_FAST_NO_HZ=y Kconfig option may be used to cause such CPUs
  119. to enter dyntick-idle mode or adaptive-tick mode anyway. In this case,
  120. a timer will awaken these CPUs every four jiffies in order to ensure
  121. that the RCU callbacks are processed in a timely fashion.
  122. Another approach is to offload RCU callback processing to "rcuo" kthreads
  123. using the CONFIG_RCU_NOCB_CPU=y Kconfig option. The specific CPUs to
  124. offload may be selected via several methods:
  125. 1. One of three mutually exclusive Kconfig options specify a
  126. build-time default for the CPUs to offload:
  127. a. The CONFIG_RCU_NOCB_CPU_NONE=y Kconfig option results in
  128. no CPUs being offloaded.
  129. b. The CONFIG_RCU_NOCB_CPU_ZERO=y Kconfig option causes
  130. CPU 0 to be offloaded.
  131. c. The CONFIG_RCU_NOCB_CPU_ALL=y Kconfig option causes all
  132. CPUs to be offloaded. Note that the callbacks will be
  133. offloaded to "rcuo" kthreads, and that those kthreads
  134. will in fact run on some CPU. However, this approach
  135. gives fine-grained control on exactly which CPUs the
  136. callbacks run on, along with their scheduling priority
  137. (including the default of SCHED_OTHER), and it further
  138. allows this control to be varied dynamically at runtime.
  139. 2. The "rcu_nocbs=" kernel boot parameter, which takes a comma-separated
  140. list of CPUs and CPU ranges, for example, "1,3-5" selects CPUs 1,
  141. 3, 4, and 5. The specified CPUs will be offloaded in addition to
  142. any CPUs specified as offloaded by CONFIG_RCU_NOCB_CPU_ZERO=y or
  143. CONFIG_RCU_NOCB_CPU_ALL=y. This means that the "rcu_nocbs=" boot
  144. parameter has no effect for kernels built with RCU_NOCB_CPU_ALL=y.
  145. The offloaded CPUs will never queue RCU callbacks, and therefore RCU
  146. never prevents offloaded CPUs from entering either dyntick-idle mode
  147. or adaptive-tick mode. That said, note that it is up to userspace to
  148. pin the "rcuo" kthreads to specific CPUs if desired. Otherwise, the
  149. scheduler will decide where to run them, which might or might not be
  150. where you want them to run.
  151. KNOWN ISSUES
  152. o Dyntick-idle slows transitions to and from idle slightly.
  153. In practice, this has not been a problem except for the most
  154. aggressive real-time workloads, which have the option of disabling
  155. dyntick-idle mode, an option that most of them take. However,
  156. some workloads will no doubt want to use adaptive ticks to
  157. eliminate scheduling-clock interrupt latencies. Here are some
  158. options for these workloads:
  159. a. Use PMQOS from userspace to inform the kernel of your
  160. latency requirements (preferred).
  161. b. On x86 systems, use the "idle=mwait" boot parameter.
  162. c. On x86 systems, use the "intel_idle.max_cstate=" to limit
  163. ` the maximum C-state depth.
  164. d. On x86 systems, use the "idle=poll" boot parameter.
  165. However, please note that use of this parameter can cause
  166. your CPU to overheat, which may cause thermal throttling
  167. to degrade your latencies -- and that this degradation can
  168. be even worse than that of dyntick-idle. Furthermore,
  169. this parameter effectively disables Turbo Mode on Intel
  170. CPUs, which can significantly reduce maximum performance.
  171. o Adaptive-ticks slows user/kernel transitions slightly.
  172. This is not expected to be a problem for computationally intensive
  173. workloads, which have few such transitions. Careful benchmarking
  174. will be required to determine whether or not other workloads
  175. are significantly affected by this effect.
  176. o Adaptive-ticks does not do anything unless there is only one
  177. runnable task for a given CPU, even though there are a number
  178. of other situations where the scheduling-clock tick is not
  179. needed. To give but one example, consider a CPU that has one
  180. runnable high-priority SCHED_FIFO task and an arbitrary number
  181. of low-priority SCHED_OTHER tasks. In this case, the CPU is
  182. required to run the SCHED_FIFO task until it either blocks or
  183. some other higher-priority task awakens on (or is assigned to)
  184. this CPU, so there is no point in sending a scheduling-clock
  185. interrupt to this CPU. However, the current implementation
  186. nevertheless sends scheduling-clock interrupts to CPUs having a
  187. single runnable SCHED_FIFO task and multiple runnable SCHED_OTHER
  188. tasks, even though these interrupts are unnecessary.
  189. Better handling of these sorts of situations is future work.
  190. o A reboot is required to reconfigure both adaptive idle and RCU
  191. callback offloading. Runtime reconfiguration could be provided
  192. if needed, however, due to the complexity of reconfiguring RCU at
  193. runtime, there would need to be an earthshakingly good reason.
  194. Especially given that you have the straightforward option of
  195. simply offloading RCU callbacks from all CPUs and pinning them
  196. where you want them whenever you want them pinned.
  197. o Additional configuration is required to deal with other sources
  198. of OS jitter, including interrupts and system-utility tasks
  199. and processes. This configuration normally involves binding
  200. interrupts and tasks to particular CPUs.
  201. o Some sources of OS jitter can currently be eliminated only by
  202. constraining the workload. For example, the only way to eliminate
  203. OS jitter due to global TLB shootdowns is to avoid the unmapping
  204. operations (such as kernel module unload operations) that
  205. result in these shootdowns. For another example, page faults
  206. and TLB misses can be reduced (and in some cases eliminated) by
  207. using huge pages and by constraining the amount of memory used
  208. by the application. Pre-faulting the working set can also be
  209. helpful, especially when combined with the mlock() and mlockall()
  210. system calls.
  211. o Unless all CPUs are idle, at least one CPU must keep the
  212. scheduling-clock interrupt going in order to support accurate
  213. timekeeping.
  214. o If there are adaptive-ticks CPUs, there will be at least one
  215. CPU keeping the scheduling-clock interrupt going, even if all
  216. CPUs are otherwise idle.