kernel-per-CPU-kthreads.txt 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. REDUCING OS JITTER DUE TO PER-CPU KTHREADS
  2. This document lists per-CPU kthreads in the Linux kernel and presents
  3. options to control their OS jitter. Note that non-per-CPU kthreads are
  4. not listed here. To reduce OS jitter from non-per-CPU kthreads, bind
  5. them to a "housekeeping" CPU dedicated to such work.
  6. REFERENCES
  7. o Documentation/IRQ-affinity.txt: Binding interrupts to sets of CPUs.
  8. o Documentation/cgroups: Using cgroups to bind tasks to sets of CPUs.
  9. o man taskset: Using the taskset command to bind tasks to sets
  10. of CPUs.
  11. o man sched_setaffinity: Using the sched_setaffinity() system
  12. call to bind tasks to sets of CPUs.
  13. o /sys/devices/system/cpu/cpuN/online: Control CPU N's hotplug state,
  14. writing "0" to offline and "1" to online.
  15. o In order to locate kernel-generated OS jitter on CPU N:
  16. cd /sys/kernel/debug/tracing
  17. echo 1 > max_graph_depth # Increase the "1" for more detail
  18. echo function_graph > current_tracer
  19. # run workload
  20. cat per_cpu/cpuN/trace
  21. KTHREADS
  22. Name: ehca_comp/%u
  23. Purpose: Periodically process Infiniband-related work.
  24. To reduce its OS jitter, do any of the following:
  25. 1. Don't use eHCA Infiniband hardware, instead choosing hardware
  26. that does not require per-CPU kthreads. This will prevent these
  27. kthreads from being created in the first place. (This will
  28. work for most people, as this hardware, though important, is
  29. relatively old and is produced in relatively low unit volumes.)
  30. 2. Do all eHCA-Infiniband-related work on other CPUs, including
  31. interrupts.
  32. 3. Rework the eHCA driver so that its per-CPU kthreads are
  33. provisioned only on selected CPUs.
  34. Name: irq/%d-%s
  35. Purpose: Handle threaded interrupts.
  36. To reduce its OS jitter, do the following:
  37. 1. Use irq affinity to force the irq threads to execute on
  38. some other CPU.
  39. Name: kcmtpd_ctr_%d
  40. Purpose: Handle Bluetooth work.
  41. To reduce its OS jitter, do one of the following:
  42. 1. Don't use Bluetooth, in which case these kthreads won't be
  43. created in the first place.
  44. 2. Use irq affinity to force Bluetooth-related interrupts to
  45. occur on some other CPU and furthermore initiate all
  46. Bluetooth activity on some other CPU.
  47. Name: ksoftirqd/%u
  48. Purpose: Execute softirq handlers when threaded or when under heavy load.
  49. To reduce its OS jitter, each softirq vector must be handled
  50. separately as follows:
  51. TIMER_SOFTIRQ: Do all of the following:
  52. 1. To the extent possible, keep the CPU out of the kernel when it
  53. is non-idle, for example, by avoiding system calls and by forcing
  54. both kernel threads and interrupts to execute elsewhere.
  55. 2. Build with CONFIG_HOTPLUG_CPU=y. After boot completes, force
  56. the CPU offline, then bring it back online. This forces
  57. recurring timers to migrate elsewhere. If you are concerned
  58. with multiple CPUs, force them all offline before bringing the
  59. first one back online. Once you have onlined the CPUs in question,
  60. do not offline any other CPUs, because doing so could force the
  61. timer back onto one of the CPUs in question.
  62. NET_TX_SOFTIRQ and NET_RX_SOFTIRQ: Do all of the following:
  63. 1. Force networking interrupts onto other CPUs.
  64. 2. Initiate any network I/O on other CPUs.
  65. 3. Once your application has started, prevent CPU-hotplug operations
  66. from being initiated from tasks that might run on the CPU to
  67. be de-jittered. (It is OK to force this CPU offline and then
  68. bring it back online before you start your application.)
  69. BLOCK_SOFTIRQ: Do all of the following:
  70. 1. Force block-device interrupts onto some other CPU.
  71. 2. Initiate any block I/O on other CPUs.
  72. 3. Once your application has started, prevent CPU-hotplug operations
  73. from being initiated from tasks that might run on the CPU to
  74. be de-jittered. (It is OK to force this CPU offline and then
  75. bring it back online before you start your application.)
  76. BLOCK_IOPOLL_SOFTIRQ: Do all of the following:
  77. 1. Force block-device interrupts onto some other CPU.
  78. 2. Initiate any block I/O and block-I/O polling on other CPUs.
  79. 3. Once your application has started, prevent CPU-hotplug operations
  80. from being initiated from tasks that might run on the CPU to
  81. be de-jittered. (It is OK to force this CPU offline and then
  82. bring it back online before you start your application.)
  83. TASKLET_SOFTIRQ: Do one or more of the following:
  84. 1. Avoid use of drivers that use tasklets. (Such drivers will contain
  85. calls to things like tasklet_schedule().)
  86. 2. Convert all drivers that you must use from tasklets to workqueues.
  87. 3. Force interrupts for drivers using tasklets onto other CPUs,
  88. and also do I/O involving these drivers on other CPUs.
  89. SCHED_SOFTIRQ: Do all of the following:
  90. 1. Avoid sending scheduler IPIs to the CPU to be de-jittered,
  91. for example, ensure that at most one runnable kthread is present
  92. on that CPU. If a thread that expects to run on the de-jittered
  93. CPU awakens, the scheduler will send an IPI that can result in
  94. a subsequent SCHED_SOFTIRQ.
  95. 2. Build with CONFIG_RCU_NOCB_CPU=y, CONFIG_RCU_NOCB_CPU_ALL=y,
  96. CONFIG_NO_HZ_FULL=y, and, in addition, ensure that the CPU
  97. to be de-jittered is marked as an adaptive-ticks CPU using the
  98. "nohz_full=" boot parameter. This reduces the number of
  99. scheduler-clock interrupts that the de-jittered CPU receives,
  100. minimizing its chances of being selected to do the load balancing
  101. work that runs in SCHED_SOFTIRQ context.
  102. 3. To the extent possible, keep the CPU out of the kernel when it
  103. is non-idle, for example, by avoiding system calls and by
  104. forcing both kernel threads and interrupts to execute elsewhere.
  105. This further reduces the number of scheduler-clock interrupts
  106. received by the de-jittered CPU.
  107. HRTIMER_SOFTIRQ: Do all of the following:
  108. 1. To the extent possible, keep the CPU out of the kernel when it
  109. is non-idle. For example, avoid system calls and force both
  110. kernel threads and interrupts to execute elsewhere.
  111. 2. Build with CONFIG_HOTPLUG_CPU=y. Once boot completes, force the
  112. CPU offline, then bring it back online. This forces recurring
  113. timers to migrate elsewhere. If you are concerned with multiple
  114. CPUs, force them all offline before bringing the first one
  115. back online. Once you have onlined the CPUs in question, do not
  116. offline any other CPUs, because doing so could force the timer
  117. back onto one of the CPUs in question.
  118. RCU_SOFTIRQ: Do at least one of the following:
  119. 1. Offload callbacks and keep the CPU in either dyntick-idle or
  120. adaptive-ticks state by doing all of the following:
  121. a. Build with CONFIG_RCU_NOCB_CPU=y, CONFIG_RCU_NOCB_CPU_ALL=y,
  122. CONFIG_NO_HZ_FULL=y, and, in addition ensure that the CPU
  123. to be de-jittered is marked as an adaptive-ticks CPU using
  124. the "nohz_full=" boot parameter. Bind the rcuo kthreads
  125. to housekeeping CPUs, which can tolerate OS jitter.
  126. b. To the extent possible, keep the CPU out of the kernel
  127. when it is non-idle, for example, by avoiding system
  128. calls and by forcing both kernel threads and interrupts
  129. to execute elsewhere.
  130. 2. Enable RCU to do its processing remotely via dyntick-idle by
  131. doing all of the following:
  132. a. Build with CONFIG_NO_HZ=y and CONFIG_RCU_FAST_NO_HZ=y.
  133. b. Ensure that the CPU goes idle frequently, allowing other
  134. CPUs to detect that it has passed through an RCU quiescent
  135. state. If the kernel is built with CONFIG_NO_HZ_FULL=y,
  136. userspace execution also allows other CPUs to detect that
  137. the CPU in question has passed through a quiescent state.
  138. c. To the extent possible, keep the CPU out of the kernel
  139. when it is non-idle, for example, by avoiding system
  140. calls and by forcing both kernel threads and interrupts
  141. to execute elsewhere.
  142. Name: kworker/%u:%d%s (cpu, id, priority)
  143. Purpose: Execute workqueue requests
  144. To reduce its OS jitter, do any of the following:
  145. 1. Run your workload at a real-time priority, which will allow
  146. preempting the kworker daemons.
  147. 2. A given workqueue can be made visible in the sysfs filesystem
  148. by passing the WQ_SYSFS to that workqueue's alloc_workqueue().
  149. Such a workqueue can be confined to a given subset of the
  150. CPUs using the /sys/devices/virtual/workqueue/*/cpumask sysfs
  151. files. The set of WQ_SYSFS workqueues can be displayed using
  152. "ls sys/devices/virtual/workqueue". That said, the workqueues
  153. maintainer would like to caution people against indiscriminately
  154. sprinkling WQ_SYSFS across all the workqueues. The reason for
  155. caution is that it is easy to add WQ_SYSFS, but because sysfs is
  156. part of the formal user/kernel API, it can be nearly impossible
  157. to remove it, even if its addition was a mistake.
  158. 3. Do any of the following needed to avoid jitter that your
  159. application cannot tolerate:
  160. a. Build your kernel with CONFIG_SLUB=y rather than
  161. CONFIG_SLAB=y, thus avoiding the slab allocator's periodic
  162. use of each CPU's workqueues to run its cache_reap()
  163. function.
  164. b. Avoid using oprofile, thus avoiding OS jitter from
  165. wq_sync_buffer().
  166. c. Limit your CPU frequency so that a CPU-frequency
  167. governor is not required, possibly enlisting the aid of
  168. special heatsinks or other cooling technologies. If done
  169. correctly, and if you CPU architecture permits, you should
  170. be able to build your kernel with CONFIG_CPU_FREQ=n to
  171. avoid the CPU-frequency governor periodically running
  172. on each CPU, including cs_dbs_timer() and od_dbs_timer().
  173. WARNING: Please check your CPU specifications to
  174. make sure that this is safe on your particular system.
  175. d. As of v3.18, Christoph Lameter's on-demand vmstat workers
  176. commit prevents OS jitter due to vmstat_update() on
  177. CONFIG_SMP=y systems. Before v3.18, is not possible
  178. to entirely get rid of the OS jitter, but you can
  179. decrease its frequency by writing a large value to
  180. /proc/sys/vm/stat_interval. The default value is HZ,
  181. for an interval of one second. Of course, larger values
  182. will make your virtual-memory statistics update more
  183. slowly. Of course, you can also run your workload at
  184. a real-time priority, thus preempting vmstat_update(),
  185. but if your workload is CPU-bound, this is a bad idea.
  186. However, there is an RFC patch from Christoph Lameter
  187. (based on an earlier one from Gilad Ben-Yossef) that
  188. reduces or even eliminates vmstat overhead for some
  189. workloads at https://lkml.org/lkml/2013/9/4/379.
  190. e. Boot with "elevator=noop" to avoid workqueue use by
  191. the block layer.
  192. f. If running on high-end powerpc servers, build with
  193. CONFIG_PPC_RTAS_DAEMON=n. This prevents the RTAS
  194. daemon from running on each CPU every second or so.
  195. (This will require editing Kconfig files and will defeat
  196. this platform's RAS functionality.) This avoids jitter
  197. due to the rtas_event_scan() function.
  198. WARNING: Please check your CPU specifications to
  199. make sure that this is safe on your particular system.
  200. g. If running on Cell Processor, build your kernel with
  201. CBE_CPUFREQ_SPU_GOVERNOR=n to avoid OS jitter from
  202. spu_gov_work().
  203. WARNING: Please check your CPU specifications to
  204. make sure that this is safe on your particular system.
  205. h. If running on PowerMAC, build your kernel with
  206. CONFIG_PMAC_RACKMETER=n to disable the CPU-meter,
  207. avoiding OS jitter from rackmeter_do_timer().
  208. Name: rcuc/%u
  209. Purpose: Execute RCU callbacks in CONFIG_RCU_BOOST=y kernels.
  210. To reduce its OS jitter, do at least one of the following:
  211. 1. Build the kernel with CONFIG_PREEMPT=n. This prevents these
  212. kthreads from being created in the first place, and also obviates
  213. the need for RCU priority boosting. This approach is feasible
  214. for workloads that do not require high degrees of responsiveness.
  215. 2. Build the kernel with CONFIG_RCU_BOOST=n. This prevents these
  216. kthreads from being created in the first place. This approach
  217. is feasible only if your workload never requires RCU priority
  218. boosting, for example, if you ensure frequent idle time on all
  219. CPUs that might execute within the kernel.
  220. 3. Build with CONFIG_RCU_NOCB_CPU=y and CONFIG_RCU_NOCB_CPU_ALL=y,
  221. which offloads all RCU callbacks to kthreads that can be moved
  222. off of CPUs susceptible to OS jitter. This approach prevents the
  223. rcuc/%u kthreads from having any work to do, so that they are
  224. never awakened.
  225. 4. Ensure that the CPU never enters the kernel, and, in particular,
  226. avoid initiating any CPU hotplug operations on this CPU. This is
  227. another way of preventing any callbacks from being queued on the
  228. CPU, again preventing the rcuc/%u kthreads from having any work
  229. to do.
  230. Name: rcuob/%d, rcuop/%d, and rcuos/%d
  231. Purpose: Offload RCU callbacks from the corresponding CPU.
  232. To reduce its OS jitter, do at least one of the following:
  233. 1. Use affinity, cgroups, or other mechanism to force these kthreads
  234. to execute on some other CPU.
  235. 2. Build with CONFIG_RCU_NOCB_CPU=n, which will prevent these
  236. kthreads from being created in the first place. However, please
  237. note that this will not eliminate OS jitter, but will instead
  238. shift it to RCU_SOFTIRQ.
  239. Name: watchdog/%u
  240. Purpose: Detect software lockups on each CPU.
  241. To reduce its OS jitter, do at least one of the following:
  242. 1. Build with CONFIG_LOCKUP_DETECTOR=n, which will prevent these
  243. kthreads from being created in the first place.
  244. 2. Boot with "nosoftlockup=0", which will also prevent these kthreads
  245. from being created. Other related watchdog and softlockup boot
  246. parameters may be found in Documentation/kernel-parameters.txt
  247. and Documentation/watchdog/watchdog-parameters.txt.
  248. 3. Echo a zero to /proc/sys/kernel/watchdog to disable the
  249. watchdog timer.
  250. 4. Echo a large number of /proc/sys/kernel/watchdog_thresh in
  251. order to reduce the frequency of OS jitter due to the watchdog
  252. timer down to a level that is acceptable for your workload.