0001 REDUCING OS JITTER DUE TO PER-CPU KTHREADS
0003 This document lists per-CPU kthreads in the Linux kernel and presents
0004 options to control their OS jitter. Note that non-per-CPU kthreads are
0005 not listed here. To reduce OS jitter from non-per-CPU kthreads, bind
0006 them to a "housekeeping" CPU dedicated to such work.
0011 o Documentation/IRQ-affinity.txt: Binding interrupts to sets of CPUs.
0013 o Documentation/cgroup-v1: Using cgroups to bind tasks to sets of CPUs.
0015 o man taskset: Using the taskset command to bind tasks to sets
0016 of CPUs.
0018 o man sched_setaffinity: Using the sched_setaffinity() system
0019 call to bind tasks to sets of CPUs.
0021 o /sys/devices/system/cpu/cpuN/online: Control CPU N's hotplug state,
0022 writing "0" to offline and "1" to online.
0024 o In order to locate kernel-generated OS jitter on CPU N:
0026 cd /sys/kernel/debug/tracing
0027 echo 1 > max_graph_depth # Increase the "1" for more detail
0028 echo function_graph > current_tracer
0029 # run workload
0030 cat per_cpu/cpuN/trace
0035 Name: ehca_comp/%u
0036 Purpose: Periodically process Infiniband-related work.
0037 To reduce its OS jitter, do any of the following:
0038 1. Don't use eHCA Infiniband hardware, instead choosing hardware
0039 that does not require per-CPU kthreads. This will prevent these
0040 kthreads from being created in the first place. (This will
0041 work for most people, as this hardware, though important, is
0042 relatively old and is produced in relatively low unit volumes.)
0043 2. Do all eHCA-Infiniband-related work on other CPUs, including
0045 3. Rework the eHCA driver so that its per-CPU kthreads are
0046 provisioned only on selected CPUs.
0049 Name: irq/%d-%s
0050 Purpose: Handle threaded interrupts.
0051 To reduce its OS jitter, do the following:
0052 1. Use irq affinity to force the irq threads to execute on
0053 some other CPU.
0055 Name: kcmtpd_ctr_%d
0056 Purpose: Handle Bluetooth work.
0057 To reduce its OS jitter, do one of the following:
0058 1. Don't use Bluetooth, in which case these kthreads won't be
0059 created in the first place.
0060 2. Use irq affinity to force Bluetooth-related interrupts to
0061 occur on some other CPU and furthermore initiate all
0062 Bluetooth activity on some other CPU.
0064 Name: ksoftirqd/%u
0065 Purpose: Execute softirq handlers when threaded or when under heavy load.
0066 To reduce its OS jitter, each softirq vector must be handled
0067 separately as follows:
0068 TIMER_SOFTIRQ: Do all of the following:
0069 1. To the extent possible, keep the CPU out of the kernel when it
0070 is non-idle, for example, by avoiding system calls and by forcing
0071 both kernel threads and interrupts to execute elsewhere.
0072 2. Build with CONFIG_HOTPLUG_CPU=y. After boot completes, force
0073 the CPU offline, then bring it back online. This forces
0074 recurring timers to migrate elsewhere. If you are concerned
0075 with multiple CPUs, force them all offline before bringing the
0076 first one back online. Once you have onlined the CPUs in question,
0077 do not offline any other CPUs, because doing so could force the
0078 timer back onto one of the CPUs in question.
0079 NET_TX_SOFTIRQ and NET_RX_SOFTIRQ: Do all of the following:
0080 1. Force networking interrupts onto other CPUs.
0081 2. Initiate any network I/O on other CPUs.
0082 3. Once your application has started, prevent CPU-hotplug operations
0083 from being initiated from tasks that might run on the CPU to
0084 be de-jittered. (It is OK to force this CPU offline and then
0085 bring it back online before you start your application.)
0086 BLOCK_SOFTIRQ: Do all of the following:
0087 1. Force block-device interrupts onto some other CPU.
0088 2. Initiate any block I/O on other CPUs.
0089 3. Once your application has started, prevent CPU-hotplug operations
0090 from being initiated from tasks that might run on the CPU to
0091 be de-jittered. (It is OK to force this CPU offline and then
0092 bring it back online before you start your application.)
0093 IRQ_POLL_SOFTIRQ: Do all of the following:
0094 1. Force block-device interrupts onto some other CPU.
0095 2. Initiate any block I/O and block-I/O polling on other CPUs.
0096 3. Once your application has started, prevent CPU-hotplug operations
0097 from being initiated from tasks that might run on the CPU to
0098 be de-jittered. (It is OK to force this CPU offline and then
0099 bring it back online before you start your application.)
0100 TASKLET_SOFTIRQ: Do one or more of the following:
0101 1. Avoid use of drivers that use tasklets. (Such drivers will contain
0102 calls to things like tasklet_schedule().)
0103 2. Convert all drivers that you must use from tasklets to workqueues.
0104 3. Force interrupts for drivers using tasklets onto other CPUs,
0105 and also do I/O involving these drivers on other CPUs.
0106 SCHED_SOFTIRQ: Do all of the following:
0107 1. Avoid sending scheduler IPIs to the CPU to be de-jittered,
0108 for example, ensure that at most one runnable kthread is present
0109 on that CPU. If a thread that expects to run on the de-jittered
0110 CPU awakens, the scheduler will send an IPI that can result in
0111 a subsequent SCHED_SOFTIRQ.
0112 2. Build with CONFIG_RCU_NOCB_CPU=y, CONFIG_RCU_NOCB_CPU_ALL=y,
0113 CONFIG_NO_HZ_FULL=y, and, in addition, ensure that the CPU
0114 to be de-jittered is marked as an adaptive-ticks CPU using the
0115 "nohz_full=" boot parameter. This reduces the number of
0116 scheduler-clock interrupts that the de-jittered CPU receives,
0117 minimizing its chances of being selected to do the load balancing
0118 work that runs in SCHED_SOFTIRQ context.
0119 3. To the extent possible, keep the CPU out of the kernel when it
0120 is non-idle, for example, by avoiding system calls and by
0121 forcing both kernel threads and interrupts to execute elsewhere.
0122 This further reduces the number of scheduler-clock interrupts
0123 received by the de-jittered CPU.
0124 HRTIMER_SOFTIRQ: Do all of the following:
0125 1. To the extent possible, keep the CPU out of the kernel when it
0126 is non-idle. For example, avoid system calls and force both
0127 kernel threads and interrupts to execute elsewhere.
0128 2. Build with CONFIG_HOTPLUG_CPU=y. Once boot completes, force the
0129 CPU offline, then bring it back online. This forces recurring
0130 timers to migrate elsewhere. If you are concerned with multiple
0131 CPUs, force them all offline before bringing the first one
0132 back online. Once you have onlined the CPUs in question, do not
0133 offline any other CPUs, because doing so could force the timer
0134 back onto one of the CPUs in question.
0135 RCU_SOFTIRQ: Do at least one of the following:
0136 1. Offload callbacks and keep the CPU in either dyntick-idle or
0137 adaptive-ticks state by doing all of the following:
0138 a. Build with CONFIG_RCU_NOCB_CPU=y, CONFIG_RCU_NOCB_CPU_ALL=y,
0139 CONFIG_NO_HZ_FULL=y, and, in addition ensure that the CPU
0140 to be de-jittered is marked as an adaptive-ticks CPU using
0141 the "nohz_full=" boot parameter. Bind the rcuo kthreads
0142 to housekeeping CPUs, which can tolerate OS jitter.
0143 b. To the extent possible, keep the CPU out of the kernel
0144 when it is non-idle, for example, by avoiding system
0145 calls and by forcing both kernel threads and interrupts
0146 to execute elsewhere.
0147 2. Enable RCU to do its processing remotely via dyntick-idle by
0148 doing all of the following:
0149 a. Build with CONFIG_NO_HZ=y and CONFIG_RCU_FAST_NO_HZ=y.
0150 b. Ensure that the CPU goes idle frequently, allowing other
0151 CPUs to detect that it has passed through an RCU quiescent
0152 state. If the kernel is built with CONFIG_NO_HZ_FULL=y,
0153 userspace execution also allows other CPUs to detect that
0154 the CPU in question has passed through a quiescent state.
0155 c. To the extent possible, keep the CPU out of the kernel
0156 when it is non-idle, for example, by avoiding system
0157 calls and by forcing both kernel threads and interrupts
0158 to execute elsewhere.
0160 Name: kworker/%u:%d%s (cpu, id, priority)
0161 Purpose: Execute workqueue requests
0162 To reduce its OS jitter, do any of the following:
0163 1. Run your workload at a real-time priority, which will allow
0164 preempting the kworker daemons.
0165 2. A given workqueue can be made visible in the sysfs filesystem
0166 by passing the WQ_SYSFS to that workqueue's alloc_workqueue().
0167 Such a workqueue can be confined to a given subset of the
0168 CPUs using the /sys/devices/virtual/workqueue/*/cpumask sysfs
0169 files. The set of WQ_SYSFS workqueues can be displayed using
0170 "ls sys/devices/virtual/workqueue". That said, the workqueues
0171 maintainer would like to caution people against indiscriminately
0172 sprinkling WQ_SYSFS across all the workqueues. The reason for
0173 caution is that it is easy to add WQ_SYSFS, but because sysfs is
0174 part of the formal user/kernel API, it can be nearly impossible
0175 to remove it, even if its addition was a mistake.
0176 3. Do any of the following needed to avoid jitter that your
0177 application cannot tolerate:
0178 a. Build your kernel with CONFIG_SLUB=y rather than
0179 CONFIG_SLAB=y, thus avoiding the slab allocator's periodic
0180 use of each CPU's workqueues to run its cache_reap()
0182 b. Avoid using oprofile, thus avoiding OS jitter from
0184 c. Limit your CPU frequency so that a CPU-frequency
0185 governor is not required, possibly enlisting the aid of
0186 special heatsinks or other cooling technologies. If done
0187 correctly, and if you CPU architecture permits, you should
0188 be able to build your kernel with CONFIG_CPU_FREQ=n to
0189 avoid the CPU-frequency governor periodically running
0190 on each CPU, including cs_dbs_timer() and od_dbs_timer().
0191 WARNING: Please check your CPU specifications to
0192 make sure that this is safe on your particular system.
0193 d. As of v3.18, Christoph Lameter's on-demand vmstat workers
0194 commit prevents OS jitter due to vmstat_update() on
0195 CONFIG_SMP=y systems. Before v3.18, is not possible
0196 to entirely get rid of the OS jitter, but you can
0197 decrease its frequency by writing a large value to
0198 /proc/sys/vm/stat_interval. The default value is HZ,
0199 for an interval of one second. Of course, larger values
0200 will make your virtual-memory statistics update more
0201 slowly. Of course, you can also run your workload at
0202 a real-time priority, thus preempting vmstat_update(),
0203 but if your workload is CPU-bound, this is a bad idea.
0204 However, there is an RFC patch from Christoph Lameter
0205 (based on an earlier one from Gilad Ben-Yossef) that
0206 reduces or even eliminates vmstat overhead for some
0207 workloads at https://lkml.org/lkml/2013/9/4/379.
0208 e. Boot with "elevator=noop" to avoid workqueue use by
0209 the block layer.
0210 f. If running on high-end powerpc servers, build with
0211 CONFIG_PPC_RTAS_DAEMON=n. This prevents the RTAS
0212 daemon from running on each CPU every second or so.
0213 (This will require editing Kconfig files and will defeat
0214 this platform's RAS functionality.) This avoids jitter
0215 due to the rtas_event_scan() function.
0216 WARNING: Please check your CPU specifications to
0217 make sure that this is safe on your particular system.
0218 g. If running on Cell Processor, build your kernel with
0219 CBE_CPUFREQ_SPU_GOVERNOR=n to avoid OS jitter from
0221 WARNING: Please check your CPU specifications to
0222 make sure that this is safe on your particular system.
0223 h. If running on PowerMAC, build your kernel with
0224 CONFIG_PMAC_RACKMETER=n to disable the CPU-meter,
0225 avoiding OS jitter from rackmeter_do_timer().
0227 Name: rcuc/%u
0228 Purpose: Execute RCU callbacks in CONFIG_RCU_BOOST=y kernels.
0229 To reduce its OS jitter, do at least one of the following:
0230 1. Build the kernel with CONFIG_PREEMPT=n. This prevents these
0231 kthreads from being created in the first place, and also obviates
0232 the need for RCU priority boosting. This approach is feasible
0233 for workloads that do not require high degrees of responsiveness.
0234 2. Build the kernel with CONFIG_RCU_BOOST=n. This prevents these
0235 kthreads from being created in the first place. This approach
0236 is feasible only if your workload never requires RCU priority
0237 boosting, for example, if you ensure frequent idle time on all
0238 CPUs that might execute within the kernel.
0239 3. Build with CONFIG_RCU_NOCB_CPU=y and CONFIG_RCU_NOCB_CPU_ALL=y,
0240 which offloads all RCU callbacks to kthreads that can be moved
0241 off of CPUs susceptible to OS jitter. This approach prevents the
0242 rcuc/%u kthreads from having any work to do, so that they are
0243 never awakened.
0244 4. Ensure that the CPU never enters the kernel, and, in particular,
0245 avoid initiating any CPU hotplug operations on this CPU. This is
0246 another way of preventing any callbacks from being queued on the
0247 CPU, again preventing the rcuc/%u kthreads from having any work
0248 to do.
0250 Name: rcuob/%d, rcuop/%d, and rcuos/%d
0251 Purpose: Offload RCU callbacks from the corresponding CPU.
0252 To reduce its OS jitter, do at least one of the following:
0253 1. Use affinity, cgroups, or other mechanism to force these kthreads
0254 to execute on some other CPU.
0255 2. Build with CONFIG_RCU_NOCB_CPU=n, which will prevent these
0256 kthreads from being created in the first place. However, please
0257 note that this will not eliminate OS jitter, but will instead
0258 shift it to RCU_SOFTIRQ.
0260 Name: watchdog/%u
0261 Purpose: Detect software lockups on each CPU.
0262 To reduce its OS jitter, do at least one of the following:
0263 1. Build with CONFIG_LOCKUP_DETECTOR=n, which will prevent these
0264 kthreads from being created in the first place.
0265 2. Boot with "nosoftlockup=0", which will also prevent these kthreads
0266 from being created. Other related watchdog and softlockup boot
0267 parameters may be found in Documentation/admin-guide/kernel-parameters.rst
0268 and Documentation/watchdog/watchdog-parameters.txt.
0269 3. Echo a zero to /proc/sys/kernel/watchdog to disable the
0270 watchdog timer.
0271 4. Echo a large number of /proc/sys/kernel/watchdog_thresh in
0272 order to reduce the frequency of OS jitter due to the watchdog
0273 timer down to a level that is acceptable for your workload.