blob: edd861c94c1bcfd29924aba9c6b5924afeda4880 [file] [log] [blame]
Claudio Scordinof58e2c32008-08-20 15:18:45 +02001 =============
2 CFS Scheduler
3 =============
Ingo Molnar5e7eaad2007-07-09 18:52:00 +02004
Dhaval Giani5cb350b2007-10-15 17:00:14 +02005
Claudio Scordinof58e2c32008-08-20 15:18:45 +020061. OVERVIEW
Dhaval Giani5cb350b2007-10-15 17:00:14 +02007
Claudio Scordinof58e2c32008-08-20 15:18:45 +02008CFS stands for "Completely Fair Scheduler," and is the new "desktop" process
9scheduler implemented by Ingo Molnar and merged in Linux 2.6.23. It is the
10replacement for the previous vanilla scheduler's SCHED_OTHER interactivity
11code.
Dhaval Giani5cb350b2007-10-15 17:00:14 +020012
Claudio Scordinof58e2c32008-08-20 15:18:45 +02001380% of CFS's design can be summed up in a single sentence: CFS basically models
14an "ideal, precise multi-tasking CPU" on real hardware.
Dhaval Giani5cb350b2007-10-15 17:00:14 +020015
Claudio Scordinof58e2c32008-08-20 15:18:45 +020016"Ideal multi-tasking CPU" is a (non-existent :-)) CPU that has 100% physical
17power and which can run each task at precise equal speed, in parallel, each at
181/nr_running speed. For example: if there are 2 tasks running, then it runs
19each at 50% physical power --- i.e., actually in parallel.
20
21On real hardware, we can run only a single task at once, so we have to
22introduce the concept of "virtual runtime." The virtual runtime of a task
23specifies when its next timeslice would start execution on the ideal
24multi-tasking CPU described above. In practice, the virtual runtime of a task
25is its actual runtime normalized to the total number of running tasks.
26
27
28
292. FEW IMPLEMENTATION DETAILS
30
31In CFS the virtual runtime is expressed and tracked via the per-task
32p->se.vruntime (nanosec-unit) value. This way, it's possible to accurately
33timestamp and measure the "expected CPU time" a task should have gotten.
34
35[ small detail: on "ideal" hardware, at any time all tasks would have the same
36 p->se.vruntime value --- i.e., tasks would execute simultaneously and no task
37 would ever get "out of balance" from the "ideal" share of CPU time. ]
38
39CFS's task picking logic is based on this p->se.vruntime value and it is thus
40very simple: it always tries to run the task with the smallest p->se.vruntime
41value (i.e., the task which executed least so far). CFS always tries to split
42up CPU time between runnable tasks as close to "ideal multitasking hardware" as
43possible.
44
45Most of the rest of CFS's design just falls out of this really simple concept,
46with a few add-on embellishments like nice levels, multiprocessing and various
47algorithm variants to recognize sleepers.
48
49
50
513. THE RBTREE
52
53CFS's design is quite radical: it does not use the old data structures for the
54runqueues, but it uses a time-ordered rbtree to build a "timeline" of future
55task execution, and thus has no "array switch" artifacts (by which both the
56previous vanilla scheduler and RSDL/SD are affected).
57
58CFS also maintains the rq->cfs.min_vruntime value, which is a monotonic
59increasing value tracking the smallest vruntime among all tasks in the
60runqueue. The total amount of work done by the system is tracked using
61min_vruntime; that value is used to place newly activated entities on the left
62side of the tree as much as possible.
63
64The total number of running tasks in the runqueue is accounted through the
65rq->cfs.load value, which is the sum of the weights of the tasks queued on the
66runqueue.
67
68CFS maintains a time-ordered rbtree, where all runnable tasks are sorted by the
Li Bin3b524d62013-09-09 14:05:40 +080069p->se.vruntime key. CFS picks the "leftmost" task from this tree and sticks to it.
Claudio Scordinof58e2c32008-08-20 15:18:45 +020070As the system progresses forwards, the executed tasks are put into the tree
71more and more to the right --- slowly but surely giving a chance for every task
72to become the "leftmost task" and thus get on the CPU within a deterministic
73amount of time.
74
75Summing up, CFS works like this: it runs a task a bit, and when the task
76schedules (or a scheduler tick happens) the task's CPU usage is "accounted
77for": the (small) time it just spent using the physical CPU is added to
78p->se.vruntime. Once p->se.vruntime gets high enough so that another task
79becomes the "leftmost task" of the time-ordered rbtree it maintains (plus a
80small amount of "granularity" distance relative to the leftmost task so that we
81do not over-schedule tasks and trash the cache), then the new leftmost task is
82picked and the current task is preempted.
83
84
85
864. SOME FEATURES OF CFS
87
88CFS uses nanosecond granularity accounting and does not rely on any jiffies or
89other HZ detail. Thus the CFS scheduler has no notion of "timeslices" in the
90way the previous scheduler had, and has no heuristics whatsoever. There is
91only one central tunable (you have to switch on CONFIG_SCHED_DEBUG):
92
Jiri Kosina4078e352008-10-27 17:41:58 +010093 /proc/sys/kernel/sched_min_granularity_ns
Claudio Scordinof58e2c32008-08-20 15:18:45 +020094
95which can be used to tune the scheduler from "desktop" (i.e., low latencies) to
96"server" (i.e., good batching) workloads. It defaults to a setting suitable
97for desktop workloads. SCHED_BATCH is handled by the CFS scheduler module too.
98
99Due to its design, the CFS scheduler is not prone to any of the "attacks" that
100exist today against the heuristics of the stock scheduler: fiftyp.c, thud.c,
101chew.c, ring-test.c, massive_intr.c all work fine and do not impact
102interactivity and produce the expected behavior.
103
104The CFS scheduler has a much stronger handling of nice levels and SCHED_BATCH
105than the previous vanilla scheduler: both types of workloads are isolated much
106more aggressively.
107
108SMP load-balancing has been reworked/sanitized: the runqueue-walking
109assumptions are gone from the load-balancing code now, and iterators of the
110scheduling modules are used. The balancing code got quite a bit simpler as a
111result.
112
113
114
Martin Steigerwald1a73ef62008-09-23 13:48:44 +02001155. Scheduling policies
116
117CFS implements three scheduling policies:
118
119 - SCHED_NORMAL (traditionally called SCHED_OTHER): The scheduling
120 policy that is used for regular tasks.
121
122 - SCHED_BATCH: Does not preempt nearly as often as regular tasks
123 would, thereby allowing tasks to run longer and make better use of
124 caches but at the cost of interactivity. This is well suited for
125 batch jobs.
126
127 - SCHED_IDLE: This is even weaker than nice 19, but its not a true
128 idle timer scheduler in order to avoid to get into priority
129 inversion problems which would deadlock the machine.
130
Hiroshi Shimamoto489a71b2012-04-02 17:00:44 +0900131SCHED_FIFO/_RR are implemented in sched/rt.c and are as specified by
Martin Steigerwald1a73ef62008-09-23 13:48:44 +0200132POSIX.
133
134The command chrt from util-linux-ng 2.13.1.1 can set all of these except
135SCHED_IDLE.
136
137
138
1396. SCHEDULING CLASSES
Claudio Scordinof58e2c32008-08-20 15:18:45 +0200140
141The new CFS scheduler has been designed in such a way to introduce "Scheduling
142Classes," an extensible hierarchy of scheduler modules. These modules
143encapsulate scheduling policy details and are handled by the scheduler core
144without the core code assuming too much about them.
145
Hiroshi Shimamoto489a71b2012-04-02 17:00:44 +0900146sched/fair.c implements the CFS scheduler described above.
Claudio Scordinof58e2c32008-08-20 15:18:45 +0200147
Hiroshi Shimamoto489a71b2012-04-02 17:00:44 +0900148sched/rt.c implements SCHED_FIFO and SCHED_RR semantics, in a simpler way than
Claudio Scordinof58e2c32008-08-20 15:18:45 +0200149the previous vanilla scheduler did. It uses 100 runqueues (for all 100 RT
150priority levels, instead of 140 in the previous scheduler) and it needs no
151expired array.
152
153Scheduling classes are implemented through the sched_class structure, which
154contains hooks to functions that must be called whenever an interesting event
155occurs.
156
157This is the (partial) list of the hooks:
158
159 - enqueue_task(...)
160
161 Called when a task enters a runnable state.
162 It puts the scheduling entity (task) into the red-black tree and
163 increments the nr_running variable.
164
Borislav Petkov1232d612011-03-22 18:46:18 +0100165 - dequeue_task(...)
Claudio Scordinof58e2c32008-08-20 15:18:45 +0200166
167 When a task is no longer runnable, this function is called to keep the
168 corresponding scheduling entity out of the red-black tree. It decrements
169 the nr_running variable.
170
171 - yield_task(...)
172
173 This function is basically just a dequeue followed by an enqueue, unless the
174 compat_yield sysctl is turned on; in that case, it places the scheduling
175 entity at the right-most end of the red-black tree.
176
177 - check_preempt_curr(...)
178
179 This function checks if a task that entered the runnable state should
180 preempt the currently running task.
181
182 - pick_next_task(...)
183
184 This function chooses the most appropriate task eligible to run next.
185
186 - set_curr_task(...)
187
188 This function is called when a task changes its scheduling class or changes
189 its task group.
190
191 - task_tick(...)
192
193 This function is mostly called from time tick functions; it might lead to
194 process switch. This drives the running preemption.
195
Claudio Scordinof58e2c32008-08-20 15:18:45 +0200196
197
198
Martin Steigerwald1a73ef62008-09-23 13:48:44 +02001997. GROUP SCHEDULER EXTENSIONS TO CFS
Claudio Scordinof58e2c32008-08-20 15:18:45 +0200200
201Normally, the scheduler operates on individual tasks and strives to provide
202fair CPU time to each task. Sometimes, it may be desirable to group tasks and
203provide fair CPU time to each such task group. For example, it may be
204desirable to first provide fair CPU time to each user on the system and then to
205each task belonging to a user.
206
Li Zefan25c2d552010-03-24 13:17:50 +0800207CONFIG_CGROUP_SCHED strives to achieve exactly that. It lets tasks to be
Claudio Scordinof58e2c32008-08-20 15:18:45 +0200208grouped and divides CPU time fairly among such groups.
209
210CONFIG_RT_GROUP_SCHED permits to group real-time (i.e., SCHED_FIFO and
211SCHED_RR) tasks.
212
213CONFIG_FAIR_GROUP_SCHED permits to group CFS (i.e., SCHED_NORMAL and
214SCHED_BATCH) tasks.
215
Li Zefan25c2d552010-03-24 13:17:50 +0800216 These options need CONFIG_CGROUPS to be defined, and let the administrator
Claudio Scordinof58e2c32008-08-20 15:18:45 +0200217 create arbitrary groups of tasks, using the "cgroup" pseudo filesystem. See
seokhoon.yoon09c3bcc2016-08-02 23:23:57 +0900218 Documentation/cgroup-v1/cgroups.txt for more information about this filesystem.
Dhaval Giani5cb350b2007-10-15 17:00:14 +0200219
Li Zefan25c2d552010-03-24 13:17:50 +0800220When CONFIG_FAIR_GROUP_SCHED is defined, a "cpu.shares" file is created for each
Claudio Scordinof58e2c32008-08-20 15:18:45 +0200221group created using the pseudo filesystem. See example steps below to create
222task groups and modify their CPU share using the "cgroups" pseudo filesystem.
Dhaval Giani5cb350b2007-10-15 17:00:14 +0200223
Jörg Sommerf6e07d32011-06-15 12:59:45 -0700224 # mount -t tmpfs cgroup_root /sys/fs/cgroup
225 # mkdir /sys/fs/cgroup/cpu
226 # mount -t cgroup -ocpu none /sys/fs/cgroup/cpu
227 # cd /sys/fs/cgroup/cpu
Dhaval Giani5cb350b2007-10-15 17:00:14 +0200228
229 # mkdir multimedia # create "multimedia" group of tasks
230 # mkdir browser # create "browser" group of tasks
231
232 # #Configure the multimedia group to receive twice the CPU bandwidth
233 # #that of browser group
234
235 # echo 2048 > multimedia/cpu.shares
236 # echo 1024 > browser/cpu.shares
237
238 # firefox & # Launch firefox and move it to "browser" group
239 # echo <firefox_pid> > browser/tasks
240
241 # #Launch gmplayer (or your favourite movie player)
242 # echo <movie_player_pid> > multimedia/tasks