sched-design.txt 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. Goals, Design and Implementation of the
  2. new ultra-scalable O(1) scheduler
  3. This is an edited version of an email Ingo Molnar sent to
  4. lkml on 4 Jan 2002. It describes the goals, design, and
  5. implementation of Ingo's new ultra-scalable O(1) scheduler.
  6. Last Updated: 18 April 2002.
  7. Goal
  8. ====
  9. The main goal of the new scheduler is to keep all the good things we know
  10. and love about the current Linux scheduler:
  11. - good interactive performance even during high load: if the user
  12. types or clicks then the system must react instantly and must execute
  13. the user tasks smoothly, even during considerable background load.
  14. - good scheduling/wakeup performance with 1-2 runnable processes.
  15. - fairness: no process should stay without any timeslice for any
  16. unreasonable amount of time. No process should get an unjustly high
  17. amount of CPU time.
  18. - priorities: less important tasks can be started with lower priority,
  19. more important tasks with higher priority.
  20. - SMP efficiency: no CPU should stay idle if there is work to do.
  21. - SMP affinity: processes which run on one CPU should stay affine to
  22. that CPU. Processes should not bounce between CPUs too frequently.
  23. - plus additional scheduler features: RT scheduling, CPU binding.
  24. and the goal is also to add a few new things:
  25. - fully O(1) scheduling. Are you tired of the recalculation loop
  26. blowing the L1 cache away every now and then? Do you think the goodness
  27. loop is taking a bit too long to finish if there are lots of runnable
  28. processes? This new scheduler takes no prisoners: wakeup(), schedule(),
  29. the timer interrupt are all O(1) algorithms. There is no recalculation
  30. loop. There is no goodness loop either.
  31. - 'perfect' SMP scalability. With the new scheduler there is no 'big'
  32. runqueue_lock anymore - it's all per-CPU runqueues and locks - two
  33. tasks on two separate CPUs can wake up, schedule and context-switch
  34. completely in parallel, without any interlocking. All
  35. scheduling-relevant data is structured for maximum scalability.
  36. - better SMP affinity. The old scheduler has a particular weakness that
  37. causes the random bouncing of tasks between CPUs if/when higher
  38. priority/interactive tasks, this was observed and reported by many
  39. people. The reason is that the timeslice recalculation loop first needs
  40. every currently running task to consume its timeslice. But when this
  41. happens on eg. an 8-way system, then this property starves an
  42. increasing number of CPUs from executing any process. Once the last
  43. task that has a timeslice left has finished using up that timeslice,
  44. the recalculation loop is triggered and other CPUs can start executing
  45. tasks again - after having idled around for a number of timer ticks.
  46. The more CPUs, the worse this effect.
  47. Furthermore, this same effect causes the bouncing effect as well:
  48. whenever there is such a 'timeslice squeeze' of the global runqueue,
  49. idle processors start executing tasks which are not affine to that CPU.
  50. (because the affine tasks have finished off their timeslices already.)
  51. The new scheduler solves this problem by distributing timeslices on a
  52. per-CPU basis, without having any global synchronization or
  53. recalculation.
  54. - batch scheduling. A significant proportion of computing-intensive tasks
  55. benefit from batch-scheduling, where timeslices are long and processes
  56. are roundrobin scheduled. The new scheduler does such batch-scheduling
  57. of the lowest priority tasks - so nice +19 jobs will get
  58. 'batch-scheduled' automatically. With this scheduler, nice +19 jobs are
  59. in essence SCHED_IDLE, from an interactiveness point of view.
  60. - handle extreme loads more smoothly, without breakdown and scheduling
  61. storms.
  62. - O(1) RT scheduling. For those RT folks who are paranoid about the
  63. O(nr_running) property of the goodness loop and the recalculation loop.
  64. - run fork()ed children before the parent. Andrea has pointed out the
  65. advantages of this a few months ago, but patches for this feature
  66. do not work with the old scheduler as well as they should,
  67. because idle processes often steal the new child before the fork()ing
  68. CPU gets to execute it.
  69. Design
  70. ======
  71. The core of the new scheduler contains the following mechanisms:
  72. - *two* priority-ordered 'priority arrays' per CPU. There is an 'active'
  73. array and an 'expired' array. The active array contains all tasks that
  74. are affine to this CPU and have timeslices left. The expired array
  75. contains all tasks which have used up their timeslices - but this array
  76. is kept sorted as well. The active and expired array is not accessed
  77. directly, it's accessed through two pointers in the per-CPU runqueue
  78. structure. If all active tasks are used up then we 'switch' the two
  79. pointers and from now on the ready-to-go (former-) expired array is the
  80. active array - and the empty active array serves as the new collector
  81. for expired tasks.
  82. - there is a 64-bit bitmap cache for array indices. Finding the highest
  83. priority task is thus a matter of two x86 BSFL bit-search instructions.
  84. the split-array solution enables us to have an arbitrary number of active
  85. and expired tasks, and the recalculation of timeslices can be done
  86. immediately when the timeslice expires. Because the arrays are always
  87. access through the pointers in the runqueue, switching the two arrays can
  88. be done very quickly.
  89. this is a hybride priority-list approach coupled with roundrobin
  90. scheduling and the array-switch method of distributing timeslices.
  91. - there is a per-task 'load estimator'.
  92. one of the toughest things to get right is good interactive feel during
  93. heavy system load. While playing with various scheduler variants i found
  94. that the best interactive feel is achieved not by 'boosting' interactive
  95. tasks, but by 'punishing' tasks that want to use more CPU time than there
  96. is available. This method is also much easier to do in an O(1) fashion.
  97. to establish the actual 'load' the task contributes to the system, a
  98. complex-looking but pretty accurate method is used: there is a 4-entry
  99. 'history' ringbuffer of the task's activities during the last 4 seconds.
  100. This ringbuffer is operated without much overhead. The entries tell the
  101. scheduler a pretty accurate load-history of the task: has it used up more
  102. CPU time or less during the past N seconds. [the size '4' and the interval
  103. of 4x 1 seconds was found by lots of experimentation - this part is
  104. flexible and can be changed in both directions.]
  105. the penalty a task gets for generating more load than the CPU can handle
  106. is a priority decrease - there is a maximum amount to this penalty
  107. relative to their static priority, so even fully CPU-bound tasks will
  108. observe each other's priorities, and will share the CPU accordingly.
  109. the SMP load-balancer can be extended/switched with additional parallel
  110. computing and cache hierarchy concepts: NUMA scheduling, multi-core CPUs
  111. can be supported easily by changing the load-balancer. Right now it's
  112. tuned for my SMP systems.
  113. i skipped the prev->mm == next->mm advantage - no workload i know of shows
  114. any sensitivity to this. It can be added back by sacrificing O(1)
  115. schedule() [the current and one-lower priority list can be searched for a
  116. that->mm == current->mm condition], but costs a fair number of cycles
  117. during a number of important workloads, so i wanted to avoid this as much
  118. as possible.
  119. - the SMP idle-task startup code was still racy and the new scheduler
  120. triggered this. So i streamlined the idle-setup code a bit. We do not call
  121. into schedule() before all processors have started up fully and all idle
  122. threads are in place.
  123. - the patch also cleans up a number of aspects of sched.c - moves code
  124. into other areas of the kernel where it's appropriate, and simplifies
  125. certain code paths and data constructs. As a result, the new scheduler's
  126. code is smaller than the old one.
  127. Ingo