sched-nice-design.rst 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. =====================
  2. Scheduler Nice Design
  3. =====================
  4. This document explains the thinking about the revamped and streamlined
  5. nice-levels implementation in the new Linux scheduler.
  6. Nice levels were always pretty weak under Linux and people continuously
  7. pestered us to make nice +19 tasks use up much less CPU time.
  8. Unfortunately that was not that easy to implement under the old
  9. scheduler, (otherwise we'd have done it long ago) because nice level
  10. support was historically coupled to timeslice length, and timeslice
  11. units were driven by the HZ tick, so the smallest timeslice was 1/HZ.
  12. In the O(1) scheduler (in 2003) we changed negative nice levels to be
  13. much stronger than they were before in 2.4 (and people were happy about
  14. that change), and we also intentionally calibrated the linear timeslice
  15. rule so that nice +19 level would be _exactly_ 1 jiffy. To better
  16. understand it, the timeslice graph went like this (cheesy ASCII art
  17. alert!)::
  18. A
  19. \ | [timeslice length]
  20. \ |
  21. \ |
  22. \ |
  23. \ |
  24. \|___100msecs
  25. |^ . _
  26. | ^ . _
  27. | ^ . _
  28. -*----------------------------------*-----> [nice level]
  29. -20 | +19
  30. |
  31. |
  32. So that if someone wanted to really renice tasks, +19 would give a much
  33. bigger hit than the normal linear rule would do. (The solution of
  34. changing the ABI to extend priorities was discarded early on.)
  35. This approach worked to some degree for some time, but later on with
  36. HZ=1000 it caused 1 jiffy to be 1 msec, which meant 0.1% CPU usage which
  37. we felt to be a bit excessive. Excessive _not_ because it's too small of
  38. a CPU utilization, but because it causes too frequent (once per
  39. millisec) rescheduling. (and would thus trash the cache, etc. Remember,
  40. this was long ago when hardware was weaker and caches were smaller, and
  41. people were running number crunching apps at nice +19.)
  42. So for HZ=1000 we changed nice +19 to 5msecs, because that felt like the
  43. right minimal granularity - and this translates to 5% CPU utilization.
  44. But the fundamental HZ-sensitive property for nice+19 still remained,
  45. and we never got a single complaint about nice +19 being too _weak_ in
  46. terms of CPU utilization, we only got complaints about it (still) being
  47. too _strong_ :-)
  48. To sum it up: we always wanted to make nice levels more consistent, but
  49. within the constraints of HZ and jiffies and their nasty design level
  50. coupling to timeslices and granularity it was not really viable.
  51. The second (less frequent but still periodically occurring) complaint
  52. about Linux's nice level support was its assymetry around the origo
  53. (which you can see demonstrated in the picture above), or more
  54. accurately: the fact that nice level behavior depended on the _absolute_
  55. nice level as well, while the nice API itself is fundamentally
  56. "relative":
  57. int nice(int inc);
  58. asmlinkage long sys_nice(int increment)
  59. (the first one is the glibc API, the second one is the syscall API.)
  60. Note that the 'inc' is relative to the current nice level. Tools like
  61. bash's "nice" command mirror this relative API.
  62. With the old scheduler, if you for example started a niced task with +1
  63. and another task with +2, the CPU split between the two tasks would
  64. depend on the nice level of the parent shell - if it was at nice -10 the
  65. CPU split was different than if it was at +5 or +10.
  66. A third complaint against Linux's nice level support was that negative
  67. nice levels were not 'punchy enough', so lots of people had to resort to
  68. run audio (and other multimedia) apps under RT priorities such as
  69. SCHED_FIFO. But this caused other problems: SCHED_FIFO is not starvation
  70. proof, and a buggy SCHED_FIFO app can also lock up the system for good.
  71. The new scheduler in v2.6.23 addresses all three types of complaints:
  72. To address the first complaint (of nice levels being not "punchy"
  73. enough), the scheduler was decoupled from 'time slice' and HZ concepts
  74. (and granularity was made a separate concept from nice levels) and thus
  75. it was possible to implement better and more consistent nice +19
  76. support: with the new scheduler nice +19 tasks get a HZ-independent
  77. 1.5%, instead of the variable 3%-5%-9% range they got in the old
  78. scheduler.
  79. To address the second complaint (of nice levels not being consistent),
  80. the new scheduler makes nice(1) have the same CPU utilization effect on
  81. tasks, regardless of their absolute nice levels. So on the new
  82. scheduler, running a nice +10 and a nice 11 task has the same CPU
  83. utilization "split" between them as running a nice -5 and a nice -4
  84. task. (one will get 55% of the CPU, the other 45%.) That is why nice
  85. levels were changed to be "multiplicative" (or exponential) - that way
  86. it does not matter which nice level you start out from, the 'relative
  87. result' will always be the same.
  88. The third complaint (of negative nice levels not being "punchy" enough
  89. and forcing audio apps to run under the more dangerous SCHED_FIFO
  90. scheduling policy) is addressed by the new scheduler almost
  91. automatically: stronger negative nice levels are an automatic
  92. side-effect of the recalibrated dynamic range of nice levels.