0001 ======================================
0002 NO_HZ: Reducing Scheduling-Clock Ticks
0003 ======================================
0004
0005
0006 This document describes Kconfig options and boot parameters that can
0007 reduce the number of scheduling-clock interrupts, thereby improving energy
0008 efficiency and reducing OS jitter. Reducing OS jitter is important for
0009 some types of computationally intensive high-performance computing (HPC)
0010 applications and for real-time applications.
0011
0012 There are three main ways of managing scheduling-clock interrupts
0013 (also known as "scheduling-clock ticks" or simply "ticks"):
0014
0015 1. Never omit scheduling-clock ticks (CONFIG_HZ_PERIODIC=y or
0016 CONFIG_NO_HZ=n for older kernels). You normally will -not-
0017 want to choose this option.
0018
0019 2. Omit scheduling-clock ticks on idle CPUs (CONFIG_NO_HZ_IDLE=y or
0020 CONFIG_NO_HZ=y for older kernels). This is the most common
0021 approach, and should be the default.
0022
0023 3. Omit scheduling-clock ticks on CPUs that are either idle or that
0024 have only one runnable task (CONFIG_NO_HZ_FULL=y). Unless you
0025 are running realtime applications or certain types of HPC
0026 workloads, you will normally -not- want this option.
0027
0028 These three cases are described in the following three sections, followed
0029 by a third section on RCU-specific considerations, a fourth section
0030 discussing testing, and a fifth and final section listing known issues.
0031
0032
0033 Never Omit Scheduling-Clock Ticks
0034 =================================
0035
0036 Very old versions of Linux from the 1990s and the very early 2000s
0037 are incapable of omitting scheduling-clock ticks. It turns out that
0038 there are some situations where this old-school approach is still the
0039 right approach, for example, in heavy workloads with lots of tasks
0040 that use short bursts of CPU, where there are very frequent idle
0041 periods, but where these idle periods are also quite short (tens or
0042 hundreds of microseconds). For these types of workloads, scheduling
0043 clock interrupts will normally be delivered any way because there
0044 will frequently be multiple runnable tasks per CPU. In these cases,
0045 attempting to turn off the scheduling clock interrupt will have no effect
0046 other than increasing the overhead of switching to and from idle and
0047 transitioning between user and kernel execution.
0048
0049 This mode of operation can be selected using CONFIG_HZ_PERIODIC=y (or
0050 CONFIG_NO_HZ=n for older kernels).
0051
0052 However, if you are instead running a light workload with long idle
0053 periods, failing to omit scheduling-clock interrupts will result in
0054 excessive power consumption. This is especially bad on battery-powered
0055 devices, where it results in extremely short battery lifetimes. If you
0056 are running light workloads, you should therefore read the following
0057 section.
0058
0059 In addition, if you are running either a real-time workload or an HPC
0060 workload with short iterations, the scheduling-clock interrupts can
0061 degrade your applications performance. If this describes your workload,
0062 you should read the following two sections.
0063
0064
0065 Omit Scheduling-Clock Ticks For Idle CPUs
0066 =========================================
0067
0068 If a CPU is idle, there is little point in sending it a scheduling-clock
0069 interrupt. After all, the primary purpose of a scheduling-clock interrupt
0070 is to force a busy CPU to shift its attention among multiple duties,
0071 and an idle CPU has no duties to shift its attention among.
0072
0073 An idle CPU that is not receiving scheduling-clock interrupts is said to
0074 be "dyntick-idle", "in dyntick-idle mode", "in nohz mode", or "running
0075 tickless". The remainder of this document will use "dyntick-idle mode".
0076
0077 The CONFIG_NO_HZ_IDLE=y Kconfig option causes the kernel to avoid sending
0078 scheduling-clock interrupts to idle CPUs, which is critically important
0079 both to battery-powered devices and to highly virtualized mainframes.
0080 A battery-powered device running a CONFIG_HZ_PERIODIC=y kernel would
0081 drain its battery very quickly, easily 2-3 times as fast as would the
0082 same device running a CONFIG_NO_HZ_IDLE=y kernel. A mainframe running
0083 1,500 OS instances might find that half of its CPU time was consumed by
0084 unnecessary scheduling-clock interrupts. In these situations, there
0085 is strong motivation to avoid sending scheduling-clock interrupts to
0086 idle CPUs. That said, dyntick-idle mode is not free:
0087
0088 1. It increases the number of instructions executed on the path
0089 to and from the idle loop.
0090
0091 2. On many architectures, dyntick-idle mode also increases the
0092 number of expensive clock-reprogramming operations.
0093
0094 Therefore, systems with aggressive real-time response constraints often
0095 run CONFIG_HZ_PERIODIC=y kernels (or CONFIG_NO_HZ=n for older kernels)
0096 in order to avoid degrading from-idle transition latencies.
0097
0098 There is also a boot parameter "nohz=" that can be used to disable
0099 dyntick-idle mode in CONFIG_NO_HZ_IDLE=y kernels by specifying "nohz=off".
0100 By default, CONFIG_NO_HZ_IDLE=y kernels boot with "nohz=on", enabling
0101 dyntick-idle mode.
0102
0103
0104 Omit Scheduling-Clock Ticks For CPUs With Only One Runnable Task
0105 ================================================================
0106
0107 If a CPU has only one runnable task, there is little point in sending it
0108 a scheduling-clock interrupt because there is no other task to switch to.
0109 Note that omitting scheduling-clock ticks for CPUs with only one runnable
0110 task implies also omitting them for idle CPUs.
0111
0112 The CONFIG_NO_HZ_FULL=y Kconfig option causes the kernel to avoid
0113 sending scheduling-clock interrupts to CPUs with a single runnable task,
0114 and such CPUs are said to be "adaptive-ticks CPUs". This is important
0115 for applications with aggressive real-time response constraints because
0116 it allows them to improve their worst-case response times by the maximum
0117 duration of a scheduling-clock interrupt. It is also important for
0118 computationally intensive short-iteration workloads: If any CPU is
0119 delayed during a given iteration, all the other CPUs will be forced to
0120 wait idle while the delayed CPU finishes. Thus, the delay is multiplied
0121 by one less than the number of CPUs. In these situations, there is
0122 again strong motivation to avoid sending scheduling-clock interrupts.
0123
0124 By default, no CPU will be an adaptive-ticks CPU. The "nohz_full="
0125 boot parameter specifies the adaptive-ticks CPUs. For example,
0126 "nohz_full=1,6-8" says that CPUs 1, 6, 7, and 8 are to be adaptive-ticks
0127 CPUs. Note that you are prohibited from marking all of the CPUs as
0128 adaptive-tick CPUs: At least one non-adaptive-tick CPU must remain
0129 online to handle timekeeping tasks in order to ensure that system
0130 calls like gettimeofday() returns accurate values on adaptive-tick CPUs.
0131 (This is not an issue for CONFIG_NO_HZ_IDLE=y because there are no running
0132 user processes to observe slight drifts in clock rate.) Therefore, the
0133 boot CPU is prohibited from entering adaptive-ticks mode. Specifying a
0134 "nohz_full=" mask that includes the boot CPU will result in a boot-time
0135 error message, and the boot CPU will be removed from the mask. Note that
0136 this means that your system must have at least two CPUs in order for
0137 CONFIG_NO_HZ_FULL=y to do anything for you.
0138
0139 Finally, adaptive-ticks CPUs must have their RCU callbacks offloaded.
0140 This is covered in the "RCU IMPLICATIONS" section below.
0141
0142 Normally, a CPU remains in adaptive-ticks mode as long as possible.
0143 In particular, transitioning to kernel mode does not automatically change
0144 the mode. Instead, the CPU will exit adaptive-ticks mode only if needed,
0145 for example, if that CPU enqueues an RCU callback.
0146
0147 Just as with dyntick-idle mode, the benefits of adaptive-tick mode do
0148 not come for free:
0149
0150 1. CONFIG_NO_HZ_FULL selects CONFIG_NO_HZ_COMMON, so you cannot run
0151 adaptive ticks without also running dyntick idle. This dependency
0152 extends down into the implementation, so that all of the costs
0153 of CONFIG_NO_HZ_IDLE are also incurred by CONFIG_NO_HZ_FULL.
0154
0155 2. The user/kernel transitions are slightly more expensive due
0156 to the need to inform kernel subsystems (such as RCU) about
0157 the change in mode.
0158
0159 3. POSIX CPU timers prevent CPUs from entering adaptive-tick mode.
0160 Real-time applications needing to take actions based on CPU time
0161 consumption need to use other means of doing so.
0162
0163 4. If there are more perf events pending than the hardware can
0164 accommodate, they are normally round-robined so as to collect
0165 all of them over time. Adaptive-tick mode may prevent this
0166 round-robining from happening. This will likely be fixed by
0167 preventing CPUs with large numbers of perf events pending from
0168 entering adaptive-tick mode.
0169
0170 5. Scheduler statistics for adaptive-tick CPUs may be computed
0171 slightly differently than those for non-adaptive-tick CPUs.
0172 This might in turn perturb load-balancing of real-time tasks.
0173
0174 Although improvements are expected over time, adaptive ticks is quite
0175 useful for many types of real-time and compute-intensive applications.
0176 However, the drawbacks listed above mean that adaptive ticks should not
0177 (yet) be enabled by default.
0178
0179
0180 RCU Implications
0181 ================
0182
0183 There are situations in which idle CPUs cannot be permitted to
0184 enter either dyntick-idle mode or adaptive-tick mode, the most
0185 common being when that CPU has RCU callbacks pending.
0186
0187 Avoid this by offloading RCU callback processing to "rcuo" kthreads
0188 using the CONFIG_RCU_NOCB_CPU=y Kconfig option. The specific CPUs to
0189 offload may be selected using The "rcu_nocbs=" kernel boot parameter,
0190 which takes a comma-separated list of CPUs and CPU ranges, for example,
0191 "1,3-5" selects CPUs 1, 3, 4, and 5. Note that CPUs specified by
0192 the "nohz_full" kernel boot parameter are also offloaded.
0193
0194 The offloaded CPUs will never queue RCU callbacks, and therefore RCU
0195 never prevents offloaded CPUs from entering either dyntick-idle mode
0196 or adaptive-tick mode. That said, note that it is up to userspace to
0197 pin the "rcuo" kthreads to specific CPUs if desired. Otherwise, the
0198 scheduler will decide where to run them, which might or might not be
0199 where you want them to run.
0200
0201
0202 Testing
0203 =======
0204
0205 So you enable all the OS-jitter features described in this document,
0206 but do not see any change in your workload's behavior. Is this because
0207 your workload isn't affected that much by OS jitter, or is it because
0208 something else is in the way? This section helps answer this question
0209 by providing a simple OS-jitter test suite, which is available on branch
0210 master of the following git archive:
0211
0212 git://git.kernel.org/pub/scm/linux/kernel/git/frederic/dynticks-testing.git
0213
0214 Clone this archive and follow the instructions in the README file.
0215 This test procedure will produce a trace that will allow you to evaluate
0216 whether or not you have succeeded in removing OS jitter from your system.
0217 If this trace shows that you have removed OS jitter as much as is
0218 possible, then you can conclude that your workload is not all that
0219 sensitive to OS jitter.
0220
0221 Note: this test requires that your system have at least two CPUs.
0222 We do not currently have a good way to remove OS jitter from single-CPU
0223 systems.
0224
0225
0226 Known Issues
0227 ============
0228
0229 * Dyntick-idle slows transitions to and from idle slightly.
0230 In practice, this has not been a problem except for the most
0231 aggressive real-time workloads, which have the option of disabling
0232 dyntick-idle mode, an option that most of them take. However,
0233 some workloads will no doubt want to use adaptive ticks to
0234 eliminate scheduling-clock interrupt latencies. Here are some
0235 options for these workloads:
0236
0237 a. Use PMQOS from userspace to inform the kernel of your
0238 latency requirements (preferred).
0239
0240 b. On x86 systems, use the "idle=mwait" boot parameter.
0241
0242 c. On x86 systems, use the "intel_idle.max_cstate=" to limit
0243 ` the maximum C-state depth.
0244
0245 d. On x86 systems, use the "idle=poll" boot parameter.
0246 However, please note that use of this parameter can cause
0247 your CPU to overheat, which may cause thermal throttling
0248 to degrade your latencies -- and that this degradation can
0249 be even worse than that of dyntick-idle. Furthermore,
0250 this parameter effectively disables Turbo Mode on Intel
0251 CPUs, which can significantly reduce maximum performance.
0252
0253 * Adaptive-ticks slows user/kernel transitions slightly.
0254 This is not expected to be a problem for computationally intensive
0255 workloads, which have few such transitions. Careful benchmarking
0256 will be required to determine whether or not other workloads
0257 are significantly affected by this effect.
0258
0259 * Adaptive-ticks does not do anything unless there is only one
0260 runnable task for a given CPU, even though there are a number
0261 of other situations where the scheduling-clock tick is not
0262 needed. To give but one example, consider a CPU that has one
0263 runnable high-priority SCHED_FIFO task and an arbitrary number
0264 of low-priority SCHED_OTHER tasks. In this case, the CPU is
0265 required to run the SCHED_FIFO task until it either blocks or
0266 some other higher-priority task awakens on (or is assigned to)
0267 this CPU, so there is no point in sending a scheduling-clock
0268 interrupt to this CPU. However, the current implementation
0269 nevertheless sends scheduling-clock interrupts to CPUs having a
0270 single runnable SCHED_FIFO task and multiple runnable SCHED_OTHER
0271 tasks, even though these interrupts are unnecessary.
0272
0273 And even when there are multiple runnable tasks on a given CPU,
0274 there is little point in interrupting that CPU until the current
0275 running task's timeslice expires, which is almost always way
0276 longer than the time of the next scheduling-clock interrupt.
0277
0278 Better handling of these sorts of situations is future work.
0279
0280 * A reboot is required to reconfigure both adaptive idle and RCU
0281 callback offloading. Runtime reconfiguration could be provided
0282 if needed, however, due to the complexity of reconfiguring RCU at
0283 runtime, there would need to be an earthshakingly good reason.
0284 Especially given that you have the straightforward option of
0285 simply offloading RCU callbacks from all CPUs and pinning them
0286 where you want them whenever you want them pinned.
0287
0288 * Additional configuration is required to deal with other sources
0289 of OS jitter, including interrupts and system-utility tasks
0290 and processes. This configuration normally involves binding
0291 interrupts and tasks to particular CPUs.
0292
0293 * Some sources of OS jitter can currently be eliminated only by
0294 constraining the workload. For example, the only way to eliminate
0295 OS jitter due to global TLB shootdowns is to avoid the unmapping
0296 operations (such as kernel module unload operations) that
0297 result in these shootdowns. For another example, page faults
0298 and TLB misses can be reduced (and in some cases eliminated) by
0299 using huge pages and by constraining the amount of memory used
0300 by the application. Pre-faulting the working set can also be
0301 helpful, especially when combined with the mlock() and mlockall()
0302 system calls.
0303
0304 * Unless all CPUs are idle, at least one CPU must keep the
0305 scheduling-clock interrupt going in order to support accurate
0306 timekeeping.
0307
0308 * If there might potentially be some adaptive-ticks CPUs, there
0309 will be at least one CPU keeping the scheduling-clock interrupt
0310 going, even if all CPUs are otherwise idle.
0311
0312 Better handling of this situation is ongoing work.
0313
0314 * Some process-handling operations still require the occasional
0315 scheduling-clock tick. These operations include calculating CPU
0316 load, maintaining sched average, computing CFS entity vruntime,
0317 computing avenrun, and carrying out load balancing. They are
0318 currently accommodated by scheduling-clock tick every second
0319 or so. On-going work will eliminate the need even for these
0320 infrequent scheduling-clock ticks.