0001 =============
0002 CFS Scheduler
0003 =============
0004
0005
0006 1. OVERVIEW
0007 ============
0008
0009 CFS stands for "Completely Fair Scheduler," and is the new "desktop" process
0010 scheduler implemented by Ingo Molnar and merged in Linux 2.6.23. It is the
0011 replacement for the previous vanilla scheduler's SCHED_OTHER interactivity
0012 code.
0013
0014 80% of CFS's design can be summed up in a single sentence: CFS basically models
0015 an "ideal, precise multi-tasking CPU" on real hardware.
0016
0017 "Ideal multi-tasking CPU" is a (non-existent :-)) CPU that has 100% physical
0018 power and which can run each task at precise equal speed, in parallel, each at
0019 1/nr_running speed. For example: if there are 2 tasks running, then it runs
0020 each at 50% physical power --- i.e., actually in parallel.
0021
0022 On real hardware, we can run only a single task at once, so we have to
0023 introduce the concept of "virtual runtime." The virtual runtime of a task
0024 specifies when its next timeslice would start execution on the ideal
0025 multi-tasking CPU described above. In practice, the virtual runtime of a task
0026 is its actual runtime normalized to the total number of running tasks.
0027
0028
0029
0030 2. FEW IMPLEMENTATION DETAILS
0031 ==============================
0032
0033 In CFS the virtual runtime is expressed and tracked via the per-task
0034 p->se.vruntime (nanosec-unit) value. This way, it's possible to accurately
0035 timestamp and measure the "expected CPU time" a task should have gotten.
0036
0037 Small detail: on "ideal" hardware, at any time all tasks would have the same
0038 p->se.vruntime value --- i.e., tasks would execute simultaneously and no task
0039 would ever get "out of balance" from the "ideal" share of CPU time.
0040
0041 CFS's task picking logic is based on this p->se.vruntime value and it is thus
0042 very simple: it always tries to run the task with the smallest p->se.vruntime
0043 value (i.e., the task which executed least so far). CFS always tries to split
0044 up CPU time between runnable tasks as close to "ideal multitasking hardware" as
0045 possible.
0046
0047 Most of the rest of CFS's design just falls out of this really simple concept,
0048 with a few add-on embellishments like nice levels, multiprocessing and various
0049 algorithm variants to recognize sleepers.
0050
0051
0052
0053 3. THE RBTREE
0054 ==============
0055
0056 CFS's design is quite radical: it does not use the old data structures for the
0057 runqueues, but it uses a time-ordered rbtree to build a "timeline" of future
0058 task execution, and thus has no "array switch" artifacts (by which both the
0059 previous vanilla scheduler and RSDL/SD are affected).
0060
0061 CFS also maintains the rq->cfs.min_vruntime value, which is a monotonic
0062 increasing value tracking the smallest vruntime among all tasks in the
0063 runqueue. The total amount of work done by the system is tracked using
0064 min_vruntime; that value is used to place newly activated entities on the left
0065 side of the tree as much as possible.
0066
0067 The total number of running tasks in the runqueue is accounted through the
0068 rq->cfs.load value, which is the sum of the weights of the tasks queued on the
0069 runqueue.
0070
0071 CFS maintains a time-ordered rbtree, where all runnable tasks are sorted by the
0072 p->se.vruntime key. CFS picks the "leftmost" task from this tree and sticks to it.
0073 As the system progresses forwards, the executed tasks are put into the tree
0074 more and more to the right --- slowly but surely giving a chance for every task
0075 to become the "leftmost task" and thus get on the CPU within a deterministic
0076 amount of time.
0077
0078 Summing up, CFS works like this: it runs a task a bit, and when the task
0079 schedules (or a scheduler tick happens) the task's CPU usage is "accounted
0080 for": the (small) time it just spent using the physical CPU is added to
0081 p->se.vruntime. Once p->se.vruntime gets high enough so that another task
0082 becomes the "leftmost task" of the time-ordered rbtree it maintains (plus a
0083 small amount of "granularity" distance relative to the leftmost task so that we
0084 do not over-schedule tasks and trash the cache), then the new leftmost task is
0085 picked and the current task is preempted.
0086
0087
0088
0089 4. SOME FEATURES OF CFS
0090 ========================
0091
0092 CFS uses nanosecond granularity accounting and does not rely on any jiffies or
0093 other HZ detail. Thus the CFS scheduler has no notion of "timeslices" in the
0094 way the previous scheduler had, and has no heuristics whatsoever. There is
0095 only one central tunable (you have to switch on CONFIG_SCHED_DEBUG):
0096
0097 /proc/sys/kernel/sched_min_granularity_ns
0098
0099 which can be used to tune the scheduler from "desktop" (i.e., low latencies) to
0100 "server" (i.e., good batching) workloads. It defaults to a setting suitable
0101 for desktop workloads. SCHED_BATCH is handled by the CFS scheduler module too.
0102
0103 Due to its design, the CFS scheduler is not prone to any of the "attacks" that
0104 exist today against the heuristics of the stock scheduler: fiftyp.c, thud.c,
0105 chew.c, ring-test.c, massive_intr.c all work fine and do not impact
0106 interactivity and produce the expected behavior.
0107
0108 The CFS scheduler has a much stronger handling of nice levels and SCHED_BATCH
0109 than the previous vanilla scheduler: both types of workloads are isolated much
0110 more aggressively.
0111
0112 SMP load-balancing has been reworked/sanitized: the runqueue-walking
0113 assumptions are gone from the load-balancing code now, and iterators of the
0114 scheduling modules are used. The balancing code got quite a bit simpler as a
0115 result.
0116
0117
0118
0119 5. Scheduling policies
0120 ======================
0121
0122 CFS implements three scheduling policies:
0123
0124 - SCHED_NORMAL (traditionally called SCHED_OTHER): The scheduling
0125 policy that is used for regular tasks.
0126
0127 - SCHED_BATCH: Does not preempt nearly as often as regular tasks
0128 would, thereby allowing tasks to run longer and make better use of
0129 caches but at the cost of interactivity. This is well suited for
0130 batch jobs.
0131
0132 - SCHED_IDLE: This is even weaker than nice 19, but its not a true
0133 idle timer scheduler in order to avoid to get into priority
0134 inversion problems which would deadlock the machine.
0135
0136 SCHED_FIFO/_RR are implemented in sched/rt.c and are as specified by
0137 POSIX.
0138
0139 The command chrt from util-linux-ng 2.13.1.1 can set all of these except
0140 SCHED_IDLE.
0141
0142
0143
0144 6. SCHEDULING CLASSES
0145 ======================
0146
0147 The new CFS scheduler has been designed in such a way to introduce "Scheduling
0148 Classes," an extensible hierarchy of scheduler modules. These modules
0149 encapsulate scheduling policy details and are handled by the scheduler core
0150 without the core code assuming too much about them.
0151
0152 sched/fair.c implements the CFS scheduler described above.
0153
0154 sched/rt.c implements SCHED_FIFO and SCHED_RR semantics, in a simpler way than
0155 the previous vanilla scheduler did. It uses 100 runqueues (for all 100 RT
0156 priority levels, instead of 140 in the previous scheduler) and it needs no
0157 expired array.
0158
0159 Scheduling classes are implemented through the sched_class structure, which
0160 contains hooks to functions that must be called whenever an interesting event
0161 occurs.
0162
0163 This is the (partial) list of the hooks:
0164
0165 - enqueue_task(...)
0166
0167 Called when a task enters a runnable state.
0168 It puts the scheduling entity (task) into the red-black tree and
0169 increments the nr_running variable.
0170
0171 - dequeue_task(...)
0172
0173 When a task is no longer runnable, this function is called to keep the
0174 corresponding scheduling entity out of the red-black tree. It decrements
0175 the nr_running variable.
0176
0177 - yield_task(...)
0178
0179 This function is basically just a dequeue followed by an enqueue, unless the
0180 compat_yield sysctl is turned on; in that case, it places the scheduling
0181 entity at the right-most end of the red-black tree.
0182
0183 - check_preempt_curr(...)
0184
0185 This function checks if a task that entered the runnable state should
0186 preempt the currently running task.
0187
0188 - pick_next_task(...)
0189
0190 This function chooses the most appropriate task eligible to run next.
0191
0192 - set_curr_task(...)
0193
0194 This function is called when a task changes its scheduling class or changes
0195 its task group.
0196
0197 - task_tick(...)
0198
0199 This function is mostly called from time tick functions; it might lead to
0200 process switch. This drives the running preemption.
0201
0202
0203
0204
0205 7. GROUP SCHEDULER EXTENSIONS TO CFS
0206 =====================================
0207
0208 Normally, the scheduler operates on individual tasks and strives to provide
0209 fair CPU time to each task. Sometimes, it may be desirable to group tasks and
0210 provide fair CPU time to each such task group. For example, it may be
0211 desirable to first provide fair CPU time to each user on the system and then to
0212 each task belonging to a user.
0213
0214 CONFIG_CGROUP_SCHED strives to achieve exactly that. It lets tasks to be
0215 grouped and divides CPU time fairly among such groups.
0216
0217 CONFIG_RT_GROUP_SCHED permits to group real-time (i.e., SCHED_FIFO and
0218 SCHED_RR) tasks.
0219
0220 CONFIG_FAIR_GROUP_SCHED permits to group CFS (i.e., SCHED_NORMAL and
0221 SCHED_BATCH) tasks.
0222
0223 These options need CONFIG_CGROUPS to be defined, and let the administrator
0224 create arbitrary groups of tasks, using the "cgroup" pseudo filesystem. See
0225 Documentation/admin-guide/cgroup-v1/cgroups.rst for more information about this filesystem.
0226
0227 When CONFIG_FAIR_GROUP_SCHED is defined, a "cpu.shares" file is created for each
0228 group created using the pseudo filesystem. See example steps below to create
0229 task groups and modify their CPU share using the "cgroups" pseudo filesystem::
0230
0231 # mount -t tmpfs cgroup_root /sys/fs/cgroup
0232 # mkdir /sys/fs/cgroup/cpu
0233 # mount -t cgroup -ocpu none /sys/fs/cgroup/cpu
0234 # cd /sys/fs/cgroup/cpu
0235
0236 # mkdir multimedia # create "multimedia" group of tasks
0237 # mkdir browser # create "browser" group of tasks
0238
0239 # #Configure the multimedia group to receive twice the CPU bandwidth
0240 # #that of browser group
0241
0242 # echo 2048 > multimedia/cpu.shares
0243 # echo 1024 > browser/cpu.shares
0244
0245 # firefox & # Launch firefox and move it to "browser" group
0246 # echo <firefox_pid> > browser/tasks
0247
0248 # #Launch gmplayer (or your favourite movie player)
0249 # echo <movie_player_pid> > multimedia/tasks