NO_HZ.txt 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  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 three main ways of managing scheduling-clock interrupts
  8. (also known as "scheduling-clock ticks" or simply "ticks"):
  9. 1. Never omit scheduling-clock ticks (CONFIG_HZ_PERIODIC=y or
  10. CONFIG_NO_HZ=n for older kernels). You normally will -not-
  11. want to choose this option.
  12. 2. Omit scheduling-clock ticks on idle CPUs (CONFIG_NO_HZ_IDLE=y or
  13. CONFIG_NO_HZ=y for older kernels). This is the most common
  14. approach, and should be the default.
  15. 3. Omit scheduling-clock ticks on CPUs that are either idle or that
  16. have only one runnable task (CONFIG_NO_HZ_FULL=y). Unless you
  17. are running realtime applications or certain types of HPC
  18. workloads, you will normally -not- want this option.
  19. These three cases are described in the following three sections, followed
  20. by a third section on RCU-specific considerations, a fourth section
  21. discussing testing, and a fifth and final section listing known issues.
  22. NEVER OMIT SCHEDULING-CLOCK TICKS
  23. Very old versions of Linux from the 1990s and the very early 2000s
  24. are incapable of omitting scheduling-clock ticks. It turns out that
  25. there are some situations where this old-school approach is still the
  26. right approach, for example, in heavy workloads with lots of tasks
  27. that use short bursts of CPU, where there are very frequent idle
  28. periods, but where these idle periods are also quite short (tens or
  29. hundreds of microseconds). For these types of workloads, scheduling
  30. clock interrupts will normally be delivered any way because there
  31. will frequently be multiple runnable tasks per CPU. In these cases,
  32. attempting to turn off the scheduling clock interrupt will have no effect
  33. other than increasing the overhead of switching to and from idle and
  34. transitioning between user and kernel execution.
  35. This mode of operation can be selected using CONFIG_HZ_PERIODIC=y (or
  36. CONFIG_NO_HZ=n for older kernels).
  37. However, if you are instead running a light workload with long idle
  38. periods, failing to omit scheduling-clock interrupts will result in
  39. excessive power consumption. This is especially bad on battery-powered
  40. devices, where it results in extremely short battery lifetimes. If you
  41. are running light workloads, you should therefore read the following
  42. section.
  43. In addition, if you are running either a real-time workload or an HPC
  44. workload with short iterations, the scheduling-clock interrupts can
  45. degrade your applications performance. If this describes your workload,
  46. you should read the following two sections.
  47. OMIT SCHEDULING-CLOCK TICKS FOR IDLE CPUs
  48. If a CPU is idle, there is little point in sending it a scheduling-clock
  49. interrupt. After all, the primary purpose of a scheduling-clock interrupt
  50. is to force a busy CPU to shift its attention among multiple duties,
  51. and an idle CPU has no duties to shift its attention among.
  52. The CONFIG_NO_HZ_IDLE=y Kconfig option causes the kernel to avoid sending
  53. scheduling-clock interrupts to idle CPUs, which is critically important
  54. both to battery-powered devices and to highly virtualized mainframes.
  55. A battery-powered device running a CONFIG_HZ_PERIODIC=y kernel would
  56. drain its battery very quickly, easily 2-3 times as fast as would the
  57. same device running a CONFIG_NO_HZ_IDLE=y kernel. A mainframe running
  58. 1,500 OS instances might find that half of its CPU time was consumed by
  59. unnecessary scheduling-clock interrupts. In these situations, there
  60. is strong motivation to avoid sending scheduling-clock interrupts to
  61. idle CPUs. That said, dyntick-idle mode is not free:
  62. 1. It increases the number of instructions executed on the path
  63. to and from the idle loop.
  64. 2. On many architectures, dyntick-idle mode also increases the
  65. number of expensive clock-reprogramming operations.
  66. Therefore, systems with aggressive real-time response constraints often
  67. run CONFIG_HZ_PERIODIC=y kernels (or CONFIG_NO_HZ=n for older kernels)
  68. in order to avoid degrading from-idle transition latencies.
  69. An idle CPU that is not receiving scheduling-clock interrupts is said to
  70. be "dyntick-idle", "in dyntick-idle mode", "in nohz mode", or "running
  71. tickless". The remainder of this document will use "dyntick-idle mode".
  72. There is also a boot parameter "nohz=" that can be used to disable
  73. dyntick-idle mode in CONFIG_NO_HZ_IDLE=y kernels by specifying "nohz=off".
  74. By default, CONFIG_NO_HZ_IDLE=y kernels boot with "nohz=on", enabling
  75. dyntick-idle mode.
  76. OMIT SCHEDULING-CLOCK TICKS FOR CPUs WITH ONLY ONE RUNNABLE TASK
  77. If a CPU has only one runnable task, there is little point in sending it
  78. a scheduling-clock interrupt because there is no other task to switch to.
  79. Note that omitting scheduling-clock ticks for CPUs with only one runnable
  80. task implies also omitting them for idle CPUs.
  81. The CONFIG_NO_HZ_FULL=y Kconfig option causes the kernel to avoid
  82. sending scheduling-clock interrupts to CPUs with a single runnable task,
  83. and such CPUs are said to be "adaptive-ticks CPUs". This is important
  84. for applications with aggressive real-time response constraints because
  85. it allows them to improve their worst-case response times by the maximum
  86. duration of a scheduling-clock interrupt. It is also important for
  87. computationally intensive short-iteration workloads: If any CPU is
  88. delayed during a given iteration, all the other CPUs will be forced to
  89. wait idle while the delayed CPU finishes. Thus, the delay is multiplied
  90. by one less than the number of CPUs. In these situations, there is
  91. again strong motivation to avoid sending scheduling-clock interrupts.
  92. By default, no CPU will be an adaptive-ticks CPU. The "nohz_full="
  93. boot parameter specifies the adaptive-ticks CPUs. For example,
  94. "nohz_full=1,6-8" says that CPUs 1, 6, 7, and 8 are to be adaptive-ticks
  95. CPUs. Note that you are prohibited from marking all of the CPUs as
  96. adaptive-tick CPUs: At least one non-adaptive-tick CPU must remain
  97. online to handle timekeeping tasks in order to ensure that system
  98. calls like gettimeofday() returns accurate values on adaptive-tick CPUs.
  99. (This is not an issue for CONFIG_NO_HZ_IDLE=y because there are no running
  100. user processes to observe slight drifts in clock rate.) Therefore, the
  101. boot CPU is prohibited from entering adaptive-ticks mode. Specifying a
  102. "nohz_full=" mask that includes the boot CPU will result in a boot-time
  103. error message, and the boot CPU will be removed from the mask. Note that
  104. this means that your system must have at least two CPUs in order for
  105. CONFIG_NO_HZ_FULL=y to do anything for you.
  106. Alternatively, the CONFIG_NO_HZ_FULL_ALL=y Kconfig parameter specifies
  107. that all CPUs other than the boot CPU are adaptive-ticks CPUs. This
  108. Kconfig parameter will be overridden by the "nohz_full=" boot parameter,
  109. so that if both the CONFIG_NO_HZ_FULL_ALL=y Kconfig parameter and
  110. the "nohz_full=1" boot parameter is specified, the boot parameter will
  111. prevail so that only CPU 1 will be an adaptive-ticks CPU.
  112. Finally, adaptive-ticks CPUs must have their RCU callbacks offloaded.
  113. This is covered in the "RCU IMPLICATIONS" section below.
  114. Normally, a CPU remains in adaptive-ticks mode as long as possible.
  115. In particular, transitioning to kernel mode does not automatically change
  116. the mode. Instead, the CPU will exit adaptive-ticks mode only if needed,
  117. for example, if that CPU enqueues an RCU callback.
  118. Just as with dyntick-idle mode, the benefits of adaptive-tick mode do
  119. not come for free:
  120. 1. CONFIG_NO_HZ_FULL selects CONFIG_NO_HZ_COMMON, so you cannot run
  121. adaptive ticks without also running dyntick idle. This dependency
  122. extends down into the implementation, so that all of the costs
  123. of CONFIG_NO_HZ_IDLE are also incurred by CONFIG_NO_HZ_FULL.
  124. 2. The user/kernel transitions are slightly more expensive due
  125. to the need to inform kernel subsystems (such as RCU) about
  126. the change in mode.
  127. 3. POSIX CPU timers prevent CPUs from entering adaptive-tick mode.
  128. Real-time applications needing to take actions based on CPU time
  129. consumption need to use other means of doing so.
  130. 4. If there are more perf events pending than the hardware can
  131. accommodate, they are normally round-robined so as to collect
  132. all of them over time. Adaptive-tick mode may prevent this
  133. round-robining from happening. This will likely be fixed by
  134. preventing CPUs with large numbers of perf events pending from
  135. entering adaptive-tick mode.
  136. 5. Scheduler statistics for adaptive-tick CPUs may be computed
  137. slightly differently than those for non-adaptive-tick CPUs.
  138. This might in turn perturb load-balancing of real-time tasks.
  139. 6. The LB_BIAS scheduler feature is disabled by adaptive ticks.
  140. Although improvements are expected over time, adaptive ticks is quite
  141. useful for many types of real-time and compute-intensive applications.
  142. However, the drawbacks listed above mean that adaptive ticks should not
  143. (yet) be enabled by default.
  144. RCU IMPLICATIONS
  145. There are situations in which idle CPUs cannot be permitted to
  146. enter either dyntick-idle mode or adaptive-tick mode, the most
  147. common being when that CPU has RCU callbacks pending.
  148. The CONFIG_RCU_FAST_NO_HZ=y Kconfig option may be used to cause such CPUs
  149. to enter dyntick-idle mode or adaptive-tick mode anyway. In this case,
  150. a timer will awaken these CPUs every four jiffies in order to ensure
  151. that the RCU callbacks are processed in a timely fashion.
  152. Another approach is to offload RCU callback processing to "rcuo" kthreads
  153. using the CONFIG_RCU_NOCB_CPU=y Kconfig option. The specific CPUs to
  154. offload may be selected via several methods:
  155. 1. One of three mutually exclusive Kconfig options specify a
  156. build-time default for the CPUs to offload:
  157. a. The CONFIG_RCU_NOCB_CPU_NONE=y Kconfig option results in
  158. no CPUs being offloaded.
  159. b. The CONFIG_RCU_NOCB_CPU_ZERO=y Kconfig option causes
  160. CPU 0 to be offloaded.
  161. c. The CONFIG_RCU_NOCB_CPU_ALL=y Kconfig option causes all
  162. CPUs to be offloaded. Note that the callbacks will be
  163. offloaded to "rcuo" kthreads, and that those kthreads
  164. will in fact run on some CPU. However, this approach
  165. gives fine-grained control on exactly which CPUs the
  166. callbacks run on, along with their scheduling priority
  167. (including the default of SCHED_OTHER), and it further
  168. allows this control to be varied dynamically at runtime.
  169. 2. The "rcu_nocbs=" kernel boot parameter, which takes a comma-separated
  170. list of CPUs and CPU ranges, for example, "1,3-5" selects CPUs 1,
  171. 3, 4, and 5. The specified CPUs will be offloaded in addition to
  172. any CPUs specified as offloaded by CONFIG_RCU_NOCB_CPU_ZERO=y or
  173. CONFIG_RCU_NOCB_CPU_ALL=y. This means that the "rcu_nocbs=" boot
  174. parameter has no effect for kernels built with RCU_NOCB_CPU_ALL=y.
  175. The offloaded CPUs will never queue RCU callbacks, and therefore RCU
  176. never prevents offloaded CPUs from entering either dyntick-idle mode
  177. or adaptive-tick mode. That said, note that it is up to userspace to
  178. pin the "rcuo" kthreads to specific CPUs if desired. Otherwise, the
  179. scheduler will decide where to run them, which might or might not be
  180. where you want them to run.
  181. TESTING
  182. So you enable all the OS-jitter features described in this document,
  183. but do not see any change in your workload's behavior. Is this because
  184. your workload isn't affected that much by OS jitter, or is it because
  185. something else is in the way? This section helps answer this question
  186. by providing a simple OS-jitter test suite, which is available on branch
  187. master of the following git archive:
  188. git://git.kernel.org/pub/scm/linux/kernel/git/frederic/dynticks-testing.git
  189. Clone this archive and follow the instructions in the README file.
  190. This test procedure will produce a trace that will allow you to evaluate
  191. whether or not you have succeeded in removing OS jitter from your system.
  192. If this trace shows that you have removed OS jitter as much as is
  193. possible, then you can conclude that your workload is not all that
  194. sensitive to OS jitter.
  195. Note: this test requires that your system have at least two CPUs.
  196. We do not currently have a good way to remove OS jitter from single-CPU
  197. systems.
  198. KNOWN ISSUES
  199. o Dyntick-idle slows transitions to and from idle slightly.
  200. In practice, this has not been a problem except for the most
  201. aggressive real-time workloads, which have the option of disabling
  202. dyntick-idle mode, an option that most of them take. However,
  203. some workloads will no doubt want to use adaptive ticks to
  204. eliminate scheduling-clock interrupt latencies. Here are some
  205. options for these workloads:
  206. a. Use PMQOS from userspace to inform the kernel of your
  207. latency requirements (preferred).
  208. b. On x86 systems, use the "idle=mwait" boot parameter.
  209. c. On x86 systems, use the "intel_idle.max_cstate=" to limit
  210. ` the maximum C-state depth.
  211. d. On x86 systems, use the "idle=poll" boot parameter.
  212. However, please note that use of this parameter can cause
  213. your CPU to overheat, which may cause thermal throttling
  214. to degrade your latencies -- and that this degradation can
  215. be even worse than that of dyntick-idle. Furthermore,
  216. this parameter effectively disables Turbo Mode on Intel
  217. CPUs, which can significantly reduce maximum performance.
  218. o Adaptive-ticks slows user/kernel transitions slightly.
  219. This is not expected to be a problem for computationally intensive
  220. workloads, which have few such transitions. Careful benchmarking
  221. will be required to determine whether or not other workloads
  222. are significantly affected by this effect.
  223. o Adaptive-ticks does not do anything unless there is only one
  224. runnable task for a given CPU, even though there are a number
  225. of other situations where the scheduling-clock tick is not
  226. needed. To give but one example, consider a CPU that has one
  227. runnable high-priority SCHED_FIFO task and an arbitrary number
  228. of low-priority SCHED_OTHER tasks. In this case, the CPU is
  229. required to run the SCHED_FIFO task until it either blocks or
  230. some other higher-priority task awakens on (or is assigned to)
  231. this CPU, so there is no point in sending a scheduling-clock
  232. interrupt to this CPU. However, the current implementation
  233. nevertheless sends scheduling-clock interrupts to CPUs having a
  234. single runnable SCHED_FIFO task and multiple runnable SCHED_OTHER
  235. tasks, even though these interrupts are unnecessary.
  236. And even when there are multiple runnable tasks on a given CPU,
  237. there is little point in interrupting that CPU until the current
  238. running task's timeslice expires, which is almost always way
  239. longer than the time of the next scheduling-clock interrupt.
  240. Better handling of these sorts of situations is future work.
  241. o A reboot is required to reconfigure both adaptive idle and RCU
  242. callback offloading. Runtime reconfiguration could be provided
  243. if needed, however, due to the complexity of reconfiguring RCU at
  244. runtime, there would need to be an earthshakingly good reason.
  245. Especially given that you have the straightforward option of
  246. simply offloading RCU callbacks from all CPUs and pinning them
  247. where you want them whenever you want them pinned.
  248. o Additional configuration is required to deal with other sources
  249. of OS jitter, including interrupts and system-utility tasks
  250. and processes. This configuration normally involves binding
  251. interrupts and tasks to particular CPUs.
  252. o Some sources of OS jitter can currently be eliminated only by
  253. constraining the workload. For example, the only way to eliminate
  254. OS jitter due to global TLB shootdowns is to avoid the unmapping
  255. operations (such as kernel module unload operations) that
  256. result in these shootdowns. For another example, page faults
  257. and TLB misses can be reduced (and in some cases eliminated) by
  258. using huge pages and by constraining the amount of memory used
  259. by the application. Pre-faulting the working set can also be
  260. helpful, especially when combined with the mlock() and mlockall()
  261. system calls.
  262. o Unless all CPUs are idle, at least one CPU must keep the
  263. scheduling-clock interrupt going in order to support accurate
  264. timekeeping.
  265. o If there might potentially be some adaptive-ticks CPUs, there
  266. will be at least one CPU keeping the scheduling-clock interrupt
  267. going, even if all CPUs are otherwise idle.
  268. Better handling of this situation is ongoing work.
  269. o Some process-handling operations still require the occasional
  270. scheduling-clock tick. These operations include calculating CPU
  271. load, maintaining sched average, computing CFS entity vruntime,
  272. computing avenrun, and carrying out load balancing. They are
  273. currently accommodated by scheduling-clock tick every second
  274. or so. On-going work will eliminate the need even for these
  275. infrequent scheduling-clock ticks.