schedutil.rst 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. =========
  2. Schedutil
  3. =========
  4. .. note::
  5. All this assumes a linear relation between frequency and work capacity,
  6. we know this is flawed, but it is the best workable approximation.
  7. PELT (Per Entity Load Tracking)
  8. ===============================
  9. With PELT we track some metrics across the various scheduler entities, from
  10. individual tasks to task-group slices to CPU runqueues. As the basis for this
  11. we use an Exponentially Weighted Moving Average (EWMA), each period (1024us)
  12. is decayed such that y^32 = 0.5. That is, the most recent 32ms contribute
  13. half, while the rest of history contribute the other half.
  14. Specifically:
  15. ewma_sum(u) := u_0 + u_1*y + u_2*y^2 + ...
  16. ewma(u) = ewma_sum(u) / ewma_sum(1)
  17. Since this is essentially a progression of an infinite geometric series, the
  18. results are composable, that is ewma(A) + ewma(B) = ewma(A+B). This property
  19. is key, since it gives the ability to recompose the averages when tasks move
  20. around.
  21. Note that blocked tasks still contribute to the aggregates (task-group slices
  22. and CPU runqueues), which reflects their expected contribution when they
  23. resume running.
  24. Using this we track 2 key metrics: 'running' and 'runnable'. 'Running'
  25. reflects the time an entity spends on the CPU, while 'runnable' reflects the
  26. time an entity spends on the runqueue. When there is only a single task these
  27. two metrics are the same, but once there is contention for the CPU 'running'
  28. will decrease to reflect the fraction of time each task spends on the CPU
  29. while 'runnable' will increase to reflect the amount of contention.
  30. For more detail see: kernel/sched/pelt.c
  31. Frequency / CPU Invariance
  32. ==========================
  33. Because consuming the CPU for 50% at 1GHz is not the same as consuming the CPU
  34. for 50% at 2GHz, nor is running 50% on a LITTLE CPU the same as running 50% on
  35. a big CPU, we allow architectures to scale the time delta with two ratios, one
  36. Dynamic Voltage and Frequency Scaling (DVFS) ratio and one microarch ratio.
  37. For simple DVFS architectures (where software is in full control) we trivially
  38. compute the ratio as::
  39. f_cur
  40. r_dvfs := -----
  41. f_max
  42. For more dynamic systems where the hardware is in control of DVFS we use
  43. hardware counters (Intel APERF/MPERF, ARMv8.4-AMU) to provide us this ratio.
  44. For Intel specifically, we use::
  45. APERF
  46. f_cur := ----- * P0
  47. MPERF
  48. 4C-turbo; if available and turbo enabled
  49. f_max := { 1C-turbo; if turbo enabled
  50. P0; otherwise
  51. f_cur
  52. r_dvfs := min( 1, ----- )
  53. f_max
  54. We pick 4C turbo over 1C turbo to make it slightly more sustainable.
  55. r_cpu is determined as the ratio of highest performance level of the current
  56. CPU vs the highest performance level of any other CPU in the system.
  57. r_tot = r_dvfs * r_cpu
  58. The result is that the above 'running' and 'runnable' metrics become invariant
  59. of DVFS and CPU type. IOW. we can transfer and compare them between CPUs.
  60. For more detail see:
  61. - kernel/sched/pelt.h:update_rq_clock_pelt()
  62. - arch/x86/kernel/smpboot.c:"APERF/MPERF frequency ratio computation."
  63. - Documentation/scheduler/sched-capacity.rst:"1. CPU Capacity + 2. Task utilization"
  64. UTIL_EST / UTIL_EST_FASTUP
  65. ==========================
  66. Because periodic tasks have their averages decayed while they sleep, even
  67. though when running their expected utilization will be the same, they suffer a
  68. (DVFS) ramp-up after they are running again.
  69. To alleviate this (a default enabled option) UTIL_EST drives an Infinite
  70. Impulse Response (IIR) EWMA with the 'running' value on dequeue -- when it is
  71. highest. A further default enabled option UTIL_EST_FASTUP modifies the IIR
  72. filter to instantly increase and only decay on decrease.
  73. A further runqueue wide sum (of runnable tasks) is maintained of:
  74. util_est := \Sum_t max( t_running, t_util_est_ewma )
  75. For more detail see: kernel/sched/fair.c:util_est_dequeue()
  76. UCLAMP
  77. ======
  78. It is possible to set effective u_min and u_max clamps on each CFS or RT task;
  79. the runqueue keeps an max aggregate of these clamps for all running tasks.
  80. For more detail see: include/uapi/linux/sched/types.h
  81. Schedutil / DVFS
  82. ================
  83. Every time the scheduler load tracking is updated (task wakeup, task
  84. migration, time progression) we call out to schedutil to update the hardware
  85. DVFS state.
  86. The basis is the CPU runqueue's 'running' metric, which per the above it is
  87. the frequency invariant utilization estimate of the CPU. From this we compute
  88. a desired frequency like::
  89. max( running, util_est ); if UTIL_EST
  90. u_cfs := { running; otherwise
  91. clamp( u_cfs + u_rt , u_min, u_max ); if UCLAMP_TASK
  92. u_clamp := { u_cfs + u_rt; otherwise
  93. u := u_clamp + u_irq + u_dl; [approx. see source for more detail]
  94. f_des := min( f_max, 1.25 u * f_max )
  95. XXX IO-wait: when the update is due to a task wakeup from IO-completion we
  96. boost 'u' above.
  97. This frequency is then used to select a P-state/OPP or directly munged into a
  98. CPPC style request to the hardware.
  99. XXX: deadline tasks (Sporadic Task Model) allows us to calculate a hard f_min
  100. required to satisfy the workload.
  101. Because these callbacks are directly from the scheduler, the DVFS hardware
  102. interaction should be 'fast' and non-blocking. Schedutil supports
  103. rate-limiting DVFS requests for when hardware interaction is slow and
  104. expensive, this reduces effectiveness.
  105. For more information see: kernel/sched/cpufreq_schedutil.c
  106. NOTES
  107. =====
  108. - On low-load scenarios, where DVFS is most relevant, the 'running' numbers
  109. will closely reflect utilization.
  110. - In saturated scenarios task movement will cause some transient dips,
  111. suppose we have a CPU saturated with 4 tasks, then when we migrate a task
  112. to an idle CPU, the old CPU will have a 'running' value of 0.75 while the
  113. new CPU will gain 0.25. This is inevitable and time progression will
  114. correct this. XXX do we still guarantee f_max due to no idle-time?
  115. - Much of the above is about avoiding DVFS dips, and independent DVFS domains
  116. having to re-learn / ramp-up when load shifts.