~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/Documentation/scheduler/sched-design-CFS.rst

Version: ~ [ linux-6.12-rc7 ] ~ [ linux-6.11.7 ] ~ [ linux-6.10.14 ] ~ [ linux-6.9.12 ] ~ [ linux-6.8.12 ] ~ [ linux-6.7.12 ] ~ [ linux-6.6.60 ] ~ [ linux-6.5.13 ] ~ [ linux-6.4.16 ] ~ [ linux-6.3.13 ] ~ [ linux-6.2.16 ] ~ [ linux-6.1.116 ] ~ [ linux-6.0.19 ] ~ [ linux-5.19.17 ] ~ [ linux-5.18.19 ] ~ [ linux-5.17.15 ] ~ [ linux-5.16.20 ] ~ [ linux-5.15.171 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.229 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.285 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.323 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.336 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.337 ] ~ [ linux-4.4.302 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.12 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

Diff markup

Differences between /Documentation/scheduler/sched-design-CFS.rst (Version linux-6.12-rc7) and /Documentation/scheduler/sched-design-CFS.rst (Version linux-6.11.7)


  1 .. _sched_design_CFS:                               1 .. _sched_design_CFS:
  2                                                     2 
  3 =============                                       3 =============
  4 CFS Scheduler                                       4 CFS Scheduler
  5 =============                                       5 =============
  6                                                     6 
  7                                                     7 
  8 1.  OVERVIEW                                        8 1.  OVERVIEW
  9 ============                                        9 ============
 10                                                    10 
 11 CFS stands for "Completely Fair Scheduler," an !!  11 CFS stands for "Completely Fair Scheduler," and is the new "desktop" process
 12 scheduler implemented by Ingo Molnar and merge !!  12 scheduler implemented by Ingo Molnar and merged in Linux 2.6.23.  It is the
 13 originally merged, it was the replacement for  !!  13 replacement for the previous vanilla scheduler's SCHED_OTHER interactivity
 14 scheduler's SCHED_OTHER interactivity code. No !!  14 code.
 15 for EEVDF, for which documentation can be foun << 
 16 Documentation/scheduler/sched-eevdf.rst.       << 
 17                                                    15 
 18 80% of CFS's design can be summed up in a sing     16 80% of CFS's design can be summed up in a single sentence: CFS basically models
 19 an "ideal, precise multi-tasking CPU" on real      17 an "ideal, precise multi-tasking CPU" on real hardware.
 20                                                    18 
 21 "Ideal multi-tasking CPU" is a (non-existent       19 "Ideal multi-tasking CPU" is a (non-existent  :-)) CPU that has 100% physical
 22 power and which can run each task at precise e     20 power and which can run each task at precise equal speed, in parallel, each at
 23 1/nr_running speed.  For example: if there are     21 1/nr_running speed.  For example: if there are 2 tasks running, then it runs
 24 each at 50% physical power --- i.e., actually      22 each at 50% physical power --- i.e., actually in parallel.
 25                                                    23 
 26 On real hardware, we can run only a single tas     24 On real hardware, we can run only a single task at once, so we have to
 27 introduce the concept of "virtual runtime."  T     25 introduce the concept of "virtual runtime."  The virtual runtime of a task
 28 specifies when its next timeslice would start      26 specifies when its next timeslice would start execution on the ideal
 29 multi-tasking CPU described above.  In practic     27 multi-tasking CPU described above.  In practice, the virtual runtime of a task
 30 is its actual runtime normalized to the total      28 is its actual runtime normalized to the total number of running tasks.
 31                                                    29 
 32                                                    30 
 33                                                    31 
 34 2.  FEW IMPLEMENTATION DETAILS                     32 2.  FEW IMPLEMENTATION DETAILS
 35 ==============================                     33 ==============================
 36                                                    34 
 37 In CFS the virtual runtime is expressed and tr     35 In CFS the virtual runtime is expressed and tracked via the per-task
 38 p->se.vruntime (nanosec-unit) value.  This way     36 p->se.vruntime (nanosec-unit) value.  This way, it's possible to accurately
 39 timestamp and measure the "expected CPU time"      37 timestamp and measure the "expected CPU time" a task should have gotten.
 40                                                    38 
 41    Small detail: on "ideal" hardware, at any t     39    Small detail: on "ideal" hardware, at any time all tasks would have the same
 42    p->se.vruntime value --- i.e., tasks would      40    p->se.vruntime value --- i.e., tasks would execute simultaneously and no task
 43    would ever get "out of balance" from the "i     41    would ever get "out of balance" from the "ideal" share of CPU time.
 44                                                    42 
 45 CFS's task picking logic is based on this p->s     43 CFS's task picking logic is based on this p->se.vruntime value and it is thus
 46 very simple: it always tries to run the task w     44 very simple: it always tries to run the task with the smallest p->se.vruntime
 47 value (i.e., the task which executed least so      45 value (i.e., the task which executed least so far).  CFS always tries to split
 48 up CPU time between runnable tasks as close to     46 up CPU time between runnable tasks as close to "ideal multitasking hardware" as
 49 possible.                                          47 possible.
 50                                                    48 
 51 Most of the rest of CFS's design just falls ou     49 Most of the rest of CFS's design just falls out of this really simple concept,
 52 with a few add-on embellishments like nice lev     50 with a few add-on embellishments like nice levels, multiprocessing and various
 53 algorithm variants to recognize sleepers.          51 algorithm variants to recognize sleepers.
 54                                                    52 
 55                                                    53 
 56                                                    54 
 57 3.  THE RBTREE                                     55 3.  THE RBTREE
 58 ==============                                     56 ==============
 59                                                    57 
 60 CFS's design is quite radical: it does not use     58 CFS's design is quite radical: it does not use the old data structures for the
 61 runqueues, but it uses a time-ordered rbtree t     59 runqueues, but it uses a time-ordered rbtree to build a "timeline" of future
 62 task execution, and thus has no "array switch"     60 task execution, and thus has no "array switch" artifacts (by which both the
 63 previous vanilla scheduler and RSDL/SD are aff     61 previous vanilla scheduler and RSDL/SD are affected).
 64                                                    62 
 65 CFS also maintains the rq->cfs.min_vruntime va     63 CFS also maintains the rq->cfs.min_vruntime value, which is a monotonic
 66 increasing value tracking the smallest vruntim     64 increasing value tracking the smallest vruntime among all tasks in the
 67 runqueue.  The total amount of work done by th     65 runqueue.  The total amount of work done by the system is tracked using
 68 min_vruntime; that value is used to place newl     66 min_vruntime; that value is used to place newly activated entities on the left
 69 side of the tree as much as possible.              67 side of the tree as much as possible.
 70                                                    68 
 71 The total number of running tasks in the runqu     69 The total number of running tasks in the runqueue is accounted through the
 72 rq->cfs.load value, which is the sum of the we     70 rq->cfs.load value, which is the sum of the weights of the tasks queued on the
 73 runqueue.                                          71 runqueue.
 74                                                    72 
 75 CFS maintains a time-ordered rbtree, where all     73 CFS maintains a time-ordered rbtree, where all runnable tasks are sorted by the
 76 p->se.vruntime key. CFS picks the "leftmost" t     74 p->se.vruntime key. CFS picks the "leftmost" task from this tree and sticks to it.
 77 As the system progresses forwards, the execute     75 As the system progresses forwards, the executed tasks are put into the tree
 78 more and more to the right --- slowly but sure     76 more and more to the right --- slowly but surely giving a chance for every task
 79 to become the "leftmost task" and thus get on      77 to become the "leftmost task" and thus get on the CPU within a deterministic
 80 amount of time.                                    78 amount of time.
 81                                                    79 
 82 Summing up, CFS works like this: it runs a tas     80 Summing up, CFS works like this: it runs a task a bit, and when the task
 83 schedules (or a scheduler tick happens) the ta     81 schedules (or a scheduler tick happens) the task's CPU usage is "accounted
 84 for": the (small) time it just spent using the     82 for": the (small) time it just spent using the physical CPU is added to
 85 p->se.vruntime.  Once p->se.vruntime gets high     83 p->se.vruntime.  Once p->se.vruntime gets high enough so that another task
 86 becomes the "leftmost task" of the time-ordere     84 becomes the "leftmost task" of the time-ordered rbtree it maintains (plus a
 87 small amount of "granularity" distance relativ     85 small amount of "granularity" distance relative to the leftmost task so that we
 88 do not over-schedule tasks and trash the cache     86 do not over-schedule tasks and trash the cache), then the new leftmost task is
 89 picked and the current task is preempted.          87 picked and the current task is preempted.
 90                                                    88 
 91                                                    89 
 92                                                    90 
 93 4.  SOME FEATURES OF CFS                           91 4.  SOME FEATURES OF CFS
 94 ========================                           92 ========================
 95                                                    93 
 96 CFS uses nanosecond granularity accounting and     94 CFS uses nanosecond granularity accounting and does not rely on any jiffies or
 97 other HZ detail.  Thus the CFS scheduler has n     95 other HZ detail.  Thus the CFS scheduler has no notion of "timeslices" in the
 98 way the previous scheduler had, and has no heu     96 way the previous scheduler had, and has no heuristics whatsoever.  There is
 99 only one central tunable (you have to switch o     97 only one central tunable (you have to switch on CONFIG_SCHED_DEBUG):
100                                                    98 
101    /sys/kernel/debug/sched/base_slice_ns           99    /sys/kernel/debug/sched/base_slice_ns
102                                                   100 
103 which can be used to tune the scheduler from "    101 which can be used to tune the scheduler from "desktop" (i.e., low latencies) to
104 "server" (i.e., good batching) workloads.  It     102 "server" (i.e., good batching) workloads.  It defaults to a setting suitable
105 for desktop workloads.  SCHED_BATCH is handled    103 for desktop workloads.  SCHED_BATCH is handled by the CFS scheduler module too.
106                                                   104 
107 In case CONFIG_HZ results in base_slice_ns < T    105 In case CONFIG_HZ results in base_slice_ns < TICK_NSEC, the value of
108 base_slice_ns will have little to no impact on    106 base_slice_ns will have little to no impact on the workloads.
109                                                   107 
110 Due to its design, the CFS scheduler is not pr    108 Due to its design, the CFS scheduler is not prone to any of the "attacks" that
111 exist today against the heuristics of the stoc    109 exist today against the heuristics of the stock scheduler: fiftyp.c, thud.c,
112 chew.c, ring-test.c, massive_intr.c all work f    110 chew.c, ring-test.c, massive_intr.c all work fine and do not impact
113 interactivity and produce the expected behavio    111 interactivity and produce the expected behavior.
114                                                   112 
115 The CFS scheduler has a much stronger handling    113 The CFS scheduler has a much stronger handling of nice levels and SCHED_BATCH
116 than the previous vanilla scheduler: both type    114 than the previous vanilla scheduler: both types of workloads are isolated much
117 more aggressively.                                115 more aggressively.
118                                                   116 
119 SMP load-balancing has been reworked/sanitized    117 SMP load-balancing has been reworked/sanitized: the runqueue-walking
120 assumptions are gone from the load-balancing c    118 assumptions are gone from the load-balancing code now, and iterators of the
121 scheduling modules are used.  The balancing co    119 scheduling modules are used.  The balancing code got quite a bit simpler as a
122 result.                                           120 result.
123                                                   121 
124                                                   122 
125                                                   123 
126 5. Scheduling policies                            124 5. Scheduling policies
127 ======================                            125 ======================
128                                                   126 
129 CFS implements three scheduling policies:         127 CFS implements three scheduling policies:
130                                                   128 
131   - SCHED_NORMAL (traditionally called SCHED_O    129   - SCHED_NORMAL (traditionally called SCHED_OTHER): The scheduling
132     policy that is used for regular tasks.        130     policy that is used for regular tasks.
133                                                   131 
134   - SCHED_BATCH: Does not preempt nearly as of    132   - SCHED_BATCH: Does not preempt nearly as often as regular tasks
135     would, thereby allowing tasks to run longe    133     would, thereby allowing tasks to run longer and make better use of
136     caches but at the cost of interactivity. T    134     caches but at the cost of interactivity. This is well suited for
137     batch jobs.                                   135     batch jobs.
138                                                   136 
139   - SCHED_IDLE: This is even weaker than nice     137   - SCHED_IDLE: This is even weaker than nice 19, but its not a true
140     idle timer scheduler in order to avoid to     138     idle timer scheduler in order to avoid to get into priority
141     inversion problems which would deadlock th    139     inversion problems which would deadlock the machine.
142                                                   140 
143 SCHED_FIFO/_RR are implemented in sched/rt.c a    141 SCHED_FIFO/_RR are implemented in sched/rt.c and are as specified by
144 POSIX.                                            142 POSIX.
145                                                   143 
146 The command chrt from util-linux-ng 2.13.1.1 c    144 The command chrt from util-linux-ng 2.13.1.1 can set all of these except
147 SCHED_IDLE.                                       145 SCHED_IDLE.
148                                                   146 
149                                                   147 
150                                                   148 
151 6.  SCHEDULING CLASSES                            149 6.  SCHEDULING CLASSES
152 ======================                            150 ======================
153                                                   151 
154 The new CFS scheduler has been designed in suc    152 The new CFS scheduler has been designed in such a way to introduce "Scheduling
155 Classes," an extensible hierarchy of scheduler    153 Classes," an extensible hierarchy of scheduler modules.  These modules
156 encapsulate scheduling policy details and are     154 encapsulate scheduling policy details and are handled by the scheduler core
157 without the core code assuming too much about     155 without the core code assuming too much about them.
158                                                   156 
159 sched/fair.c implements the CFS scheduler desc    157 sched/fair.c implements the CFS scheduler described above.
160                                                   158 
161 sched/rt.c implements SCHED_FIFO and SCHED_RR     159 sched/rt.c implements SCHED_FIFO and SCHED_RR semantics, in a simpler way than
162 the previous vanilla scheduler did.  It uses 1    160 the previous vanilla scheduler did.  It uses 100 runqueues (for all 100 RT
163 priority levels, instead of 140 in the previou    161 priority levels, instead of 140 in the previous scheduler) and it needs no
164 expired array.                                    162 expired array.
165                                                   163 
166 Scheduling classes are implemented through the    164 Scheduling classes are implemented through the sched_class structure, which
167 contains hooks to functions that must be calle    165 contains hooks to functions that must be called whenever an interesting event
168 occurs.                                           166 occurs.
169                                                   167 
170 This is the (partial) list of the hooks:          168 This is the (partial) list of the hooks:
171                                                   169 
172  - enqueue_task(...)                              170  - enqueue_task(...)
173                                                   171 
174    Called when a task enters a runnable state.    172    Called when a task enters a runnable state.
175    It puts the scheduling entity (task) into t    173    It puts the scheduling entity (task) into the red-black tree and
176    increments the nr_running variable.            174    increments the nr_running variable.
177                                                   175 
178  - dequeue_task(...)                              176  - dequeue_task(...)
179                                                   177 
180    When a task is no longer runnable, this fun    178    When a task is no longer runnable, this function is called to keep the
181    corresponding scheduling entity out of the     179    corresponding scheduling entity out of the red-black tree.  It decrements
182    the nr_running variable.                       180    the nr_running variable.
183                                                   181 
184  - yield_task(...)                                182  - yield_task(...)
185                                                   183 
186    This function is basically just a dequeue f    184    This function is basically just a dequeue followed by an enqueue, unless the
187    compat_yield sysctl is turned on; in that c    185    compat_yield sysctl is turned on; in that case, it places the scheduling
188    entity at the right-most end of the red-bla    186    entity at the right-most end of the red-black tree.
189                                                   187 
190  - wakeup_preempt(...)                            188  - wakeup_preempt(...)
191                                                   189 
192    This function checks if a task that entered    190    This function checks if a task that entered the runnable state should
193    preempt the currently running task.            191    preempt the currently running task.
194                                                   192 
195  - pick_next_task(...)                            193  - pick_next_task(...)
196                                                   194 
197    This function chooses the most appropriate     195    This function chooses the most appropriate task eligible to run next.
198                                                   196 
199  - set_next_task(...)                             197  - set_next_task(...)
200                                                   198 
201    This function is called when a task changes    199    This function is called when a task changes its scheduling class, changes
202    its task group or is scheduled.                200    its task group or is scheduled.
203                                                   201 
204  - task_tick(...)                                 202  - task_tick(...)
205                                                   203 
206    This function is mostly called from time ti    204    This function is mostly called from time tick functions; it might lead to
207    process switch.  This drives the running pr    205    process switch.  This drives the running preemption.
208                                                   206 
209                                                   207 
210                                                   208 
211                                                   209 
212 7.  GROUP SCHEDULER EXTENSIONS TO CFS             210 7.  GROUP SCHEDULER EXTENSIONS TO CFS
213 =====================================             211 =====================================
214                                                   212 
215 Normally, the scheduler operates on individual    213 Normally, the scheduler operates on individual tasks and strives to provide
216 fair CPU time to each task.  Sometimes, it may    214 fair CPU time to each task.  Sometimes, it may be desirable to group tasks and
217 provide fair CPU time to each such task group.    215 provide fair CPU time to each such task group.  For example, it may be
218 desirable to first provide fair CPU time to ea    216 desirable to first provide fair CPU time to each user on the system and then to
219 each task belonging to a user.                    217 each task belonging to a user.
220                                                   218 
221 CONFIG_CGROUP_SCHED strives to achieve exactly    219 CONFIG_CGROUP_SCHED strives to achieve exactly that.  It lets tasks to be
222 grouped and divides CPU time fairly among such    220 grouped and divides CPU time fairly among such groups.
223                                                   221 
224 CONFIG_RT_GROUP_SCHED permits to group real-ti    222 CONFIG_RT_GROUP_SCHED permits to group real-time (i.e., SCHED_FIFO and
225 SCHED_RR) tasks.                                  223 SCHED_RR) tasks.
226                                                   224 
227 CONFIG_FAIR_GROUP_SCHED permits to group CFS (    225 CONFIG_FAIR_GROUP_SCHED permits to group CFS (i.e., SCHED_NORMAL and
228 SCHED_BATCH) tasks.                               226 SCHED_BATCH) tasks.
229                                                   227 
230    These options need CONFIG_CGROUPS to be def    228    These options need CONFIG_CGROUPS to be defined, and let the administrator
231    create arbitrary groups of tasks, using the    229    create arbitrary groups of tasks, using the "cgroup" pseudo filesystem.  See
232    Documentation/admin-guide/cgroup-v1/cgroups    230    Documentation/admin-guide/cgroup-v1/cgroups.rst for more information about this filesystem.
233                                                   231 
234 When CONFIG_FAIR_GROUP_SCHED is defined, a "cp    232 When CONFIG_FAIR_GROUP_SCHED is defined, a "cpu.shares" file is created for each
235 group created using the pseudo filesystem.  Se    233 group created using the pseudo filesystem.  See example steps below to create
236 task groups and modify their CPU share using t    234 task groups and modify their CPU share using the "cgroups" pseudo filesystem::
237                                                   235 
238         # mount -t tmpfs cgroup_root /sys/fs/c    236         # mount -t tmpfs cgroup_root /sys/fs/cgroup
239         # mkdir /sys/fs/cgroup/cpu                237         # mkdir /sys/fs/cgroup/cpu
240         # mount -t cgroup -ocpu none /sys/fs/c    238         # mount -t cgroup -ocpu none /sys/fs/cgroup/cpu
241         # cd /sys/fs/cgroup/cpu                   239         # cd /sys/fs/cgroup/cpu
242                                                   240 
243         # mkdir multimedia      # create "mult    241         # mkdir multimedia      # create "multimedia" group of tasks
244         # mkdir browser         # create "brow    242         # mkdir browser         # create "browser" group of tasks
245                                                   243 
246         # #Configure the multimedia group to r    244         # #Configure the multimedia group to receive twice the CPU bandwidth
247         # #that of browser group                  245         # #that of browser group
248                                                   246 
249         # echo 2048 > multimedia/cpu.shares       247         # echo 2048 > multimedia/cpu.shares
250         # echo 1024 > browser/cpu.shares          248         # echo 1024 > browser/cpu.shares
251                                                   249 
252         # firefox &     # Launch firefox and m    250         # firefox &     # Launch firefox and move it to "browser" group
253         # echo <firefox_pid> > browser/tasks      251         # echo <firefox_pid> > browser/tasks
254                                                   252 
255         # #Launch gmplayer (or your favourite     253         # #Launch gmplayer (or your favourite movie player)
256         # echo <movie_player_pid> > multimedia    254         # echo <movie_player_pid> > multimedia/tasks
                                                      

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

sflogo.php