kernel-per-CPU-kthreads.txt 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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: rcuc/%u
  143. Purpose: Execute RCU callbacks in CONFIG_RCU_BOOST=y kernels.
  144. To reduce its OS jitter, do at least one of the following:
  145. 1. Build the kernel with CONFIG_PREEMPT=n. This prevents these
  146. kthreads from being created in the first place, and also obviates
  147. the need for RCU priority boosting. This approach is feasible
  148. for workloads that do not require high degrees of responsiveness.
  149. 2. Build the kernel with CONFIG_RCU_BOOST=n. This prevents these
  150. kthreads from being created in the first place. This approach
  151. is feasible only if your workload never requires RCU priority
  152. boosting, for example, if you ensure frequent idle time on all
  153. CPUs that might execute within the kernel.
  154. 3. Build with CONFIG_RCU_NOCB_CPU=y and CONFIG_RCU_NOCB_CPU_ALL=y,
  155. which offloads all RCU callbacks to kthreads that can be moved
  156. off of CPUs susceptible to OS jitter. This approach prevents the
  157. rcuc/%u kthreads from having any work to do, so that they are
  158. never awakened.
  159. 4. Ensure that the CPU never enters the kernel, and, in particular,
  160. avoid initiating any CPU hotplug operations on this CPU. This is
  161. another way of preventing any callbacks from being queued on the
  162. CPU, again preventing the rcuc/%u kthreads from having any work
  163. to do.
  164. Name: rcuob/%d, rcuop/%d, and rcuos/%d
  165. Purpose: Offload RCU callbacks from the corresponding CPU.
  166. To reduce its OS jitter, do at least one of the following:
  167. 1. Use affinity, cgroups, or other mechanism to force these kthreads
  168. to execute on some other CPU.
  169. 2. Build with CONFIG_RCU_NOCB_CPUS=n, which will prevent these
  170. kthreads from being created in the first place. However, please
  171. note that this will not eliminate OS jitter, but will instead
  172. shift it to RCU_SOFTIRQ.
  173. Name: watchdog/%u
  174. Purpose: Detect software lockups on each CPU.
  175. To reduce its OS jitter, do at least one of the following:
  176. 1. Build with CONFIG_LOCKUP_DETECTOR=n, which will prevent these
  177. kthreads from being created in the first place.
  178. 2. Echo a zero to /proc/sys/kernel/watchdog to disable the
  179. watchdog timer.
  180. 3. Echo a large number of /proc/sys/kernel/watchdog_thresh in
  181. order to reduce the frequency of OS jitter due to the watchdog
  182. timer down to a level that is acceptable for your workload.