blob: 74d47e65b9ea6887f6afa9cb7ba78ab532a7f26b [file] [log] [blame]
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001/*
2 * Completely Fair Scheduling (CFS) Class (SCHED_NORMAL/SCHED_BATCH)
3 *
4 * Copyright (C) 2007 Red Hat, Inc., Ingo Molnar <mingo@redhat.com>
5 *
6 * Interactivity improvements by Mike Galbraith
7 * (C) 2007 Mike Galbraith <efault@gmx.de>
8 *
9 * Various enhancements by Dmitry Adamushko.
10 * (C) 2007 Dmitry Adamushko <dmitry.adamushko@gmail.com>
11 *
12 * Group scheduling enhancements by Srivatsa Vaddagiri
13 * Copyright IBM Corporation, 2007
14 * Author: Srivatsa Vaddagiri <vatsa@linux.vnet.ibm.com>
15 *
16 * Scaled math optimizations by Thomas Gleixner
17 * Copyright (C) 2007, Thomas Gleixner <tglx@linutronix.de>
Peter Zijlstra21805082007-08-25 18:41:53 +020018 *
19 * Adaptive scheduling granularity, math enhancements by Peter Zijlstra
20 * Copyright (C) 2007 Red Hat, Inc., Peter Zijlstra <pzijlstr@redhat.com>
Ingo Molnarbf0f6f22007-07-09 18:51:58 +020021 */
22
23/*
Ingo Molnar2bd8e6d2007-10-15 17:00:02 +020024 * Tunables that become constants when CONFIG_SCHED_DEBUG is off:
25 */
26#ifdef CONFIG_SCHED_DEBUG
27# define const_debug __read_mostly
28#else
29# define const_debug static const
30#endif
31
32/*
Peter Zijlstra21805082007-08-25 18:41:53 +020033 * Targeted preemption latency for CPU-bound tasks:
34 * (default: 20ms, units: nanoseconds)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +020035 *
Peter Zijlstra21805082007-08-25 18:41:53 +020036 * NOTE: this latency value is not the same as the concept of
37 * 'timeslice length' - timeslices in CFS are of variable length.
38 * (to see the precise effective timeslice length of your workload,
39 * run vmstat and monitor the context-switches field)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +020040 *
41 * On SMP systems the value of this is multiplied by the log2 of the
42 * number of CPUs. (i.e. factor 2x on 2-way systems, 3x on 4-way
43 * systems, 4x on 8-way systems, 5x on 16-way systems, etc.)
Peter Zijlstra21805082007-08-25 18:41:53 +020044 * Targeted preemption latency for CPU-bound tasks:
Ingo Molnarbf0f6f22007-07-09 18:51:58 +020045 */
Ingo Molnar2bd8e6d2007-10-15 17:00:02 +020046const_debug unsigned int sysctl_sched_latency = 20000000ULL;
47
48/*
49 * After fork, child runs first. (default) If set to 0 then
50 * parent will (try to) run first.
51 */
52const_debug unsigned int sysctl_sched_child_runs_first = 1;
Peter Zijlstra21805082007-08-25 18:41:53 +020053
54/*
55 * Minimal preemption granularity for CPU-bound tasks:
56 * (default: 2 msec, units: nanoseconds)
57 */
Ingo Molnar172ac3d2007-08-25 18:41:53 +020058unsigned int sysctl_sched_min_granularity __read_mostly = 2000000ULL;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +020059
60/*
Ingo Molnar1799e352007-09-19 23:34:46 +020061 * sys_sched_yield() compat mode
62 *
63 * This option switches the agressive yield implementation of the
64 * old scheduler back on.
65 */
66unsigned int __read_mostly sysctl_sched_compat_yield;
67
68/*
Ingo Molnarbf0f6f22007-07-09 18:51:58 +020069 * SCHED_BATCH wake-up granularity.
Ingo Molnar71fd3712007-08-24 20:39:10 +020070 * (default: 25 msec, units: nanoseconds)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +020071 *
72 * This option delays the preemption effects of decoupled workloads
73 * and reduces their over-scheduling. Synchronous workloads will still
74 * have immediate wakeup/sleep latencies.
75 */
Ingo Molnar2bd8e6d2007-10-15 17:00:02 +020076const_debug unsigned int sysctl_sched_batch_wakeup_granularity = 25000000UL;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +020077
78/*
79 * SCHED_OTHER wake-up granularity.
80 * (default: 1 msec, units: nanoseconds)
81 *
82 * This option delays the preemption effects of decoupled workloads
83 * and reduces their over-scheduling. Synchronous workloads will still
84 * have immediate wakeup/sleep latencies.
85 */
Ingo Molnar2bd8e6d2007-10-15 17:00:02 +020086const_debug unsigned int sysctl_sched_wakeup_granularity = 1000000UL;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +020087
Ingo Molnarbf0f6f22007-07-09 18:51:58 +020088unsigned int sysctl_sched_runtime_limit __read_mostly;
89
90/*
91 * Debugging: various feature bits
92 */
93enum {
94 SCHED_FEAT_FAIR_SLEEPERS = 1,
95 SCHED_FEAT_SLEEPER_AVG = 2,
96 SCHED_FEAT_SLEEPER_LOAD_AVG = 4,
Ingo Molnara25707f2007-10-15 17:00:03 +020097 SCHED_FEAT_START_DEBIT = 8,
98 SCHED_FEAT_SKIP_INITIAL = 16,
Ingo Molnarbf0f6f22007-07-09 18:51:58 +020099};
100
Ingo Molnar2bd8e6d2007-10-15 17:00:02 +0200101const_debug unsigned int sysctl_sched_features =
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200102 SCHED_FEAT_FAIR_SLEEPERS *1 |
Ingo Molnar5d2b3d32007-08-12 18:08:19 +0200103 SCHED_FEAT_SLEEPER_AVG *0 |
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200104 SCHED_FEAT_SLEEPER_LOAD_AVG *1 |
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200105 SCHED_FEAT_START_DEBIT *1 |
106 SCHED_FEAT_SKIP_INITIAL *0;
107
108extern struct sched_class fair_sched_class;
109
110/**************************************************************
111 * CFS operations on generic schedulable entities:
112 */
113
114#ifdef CONFIG_FAIR_GROUP_SCHED
115
116/* cpu runqueue to which this cfs_rq is attached */
117static inline struct rq *rq_of(struct cfs_rq *cfs_rq)
118{
119 return cfs_rq->rq;
120}
121
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200122/* An entity is a task if it doesn't "own" a runqueue */
123#define entity_is_task(se) (!se->my_q)
124
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200125#else /* CONFIG_FAIR_GROUP_SCHED */
126
127static inline struct rq *rq_of(struct cfs_rq *cfs_rq)
128{
129 return container_of(cfs_rq, struct rq, cfs);
130}
131
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200132#define entity_is_task(se) 1
133
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200134#endif /* CONFIG_FAIR_GROUP_SCHED */
135
136static inline struct task_struct *task_of(struct sched_entity *se)
137{
138 return container_of(se, struct task_struct, se);
139}
140
141
142/**************************************************************
143 * Scheduling class tree data structure manipulation methods:
144 */
145
146/*
147 * Enqueue an entity into the rb-tree:
148 */
149static inline void
150__enqueue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se)
151{
152 struct rb_node **link = &cfs_rq->tasks_timeline.rb_node;
153 struct rb_node *parent = NULL;
154 struct sched_entity *entry;
155 s64 key = se->fair_key;
156 int leftmost = 1;
157
158 /*
159 * Find the right place in the rbtree:
160 */
161 while (*link) {
162 parent = *link;
163 entry = rb_entry(parent, struct sched_entity, run_node);
164 /*
165 * We dont care about collisions. Nodes with
166 * the same key stay together.
167 */
168 if (key - entry->fair_key < 0) {
169 link = &parent->rb_left;
170 } else {
171 link = &parent->rb_right;
172 leftmost = 0;
173 }
174 }
175
176 /*
177 * Maintain a cache of leftmost tree entries (it is frequently
178 * used):
179 */
180 if (leftmost)
181 cfs_rq->rb_leftmost = &se->run_node;
182
183 rb_link_node(&se->run_node, parent, link);
184 rb_insert_color(&se->run_node, &cfs_rq->tasks_timeline);
185 update_load_add(&cfs_rq->load, se->load.weight);
186 cfs_rq->nr_running++;
187 se->on_rq = 1;
Ingo Molnara206c072007-09-05 14:32:49 +0200188
189 schedstat_add(cfs_rq, wait_runtime, se->wait_runtime);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200190}
191
192static inline void
193__dequeue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se)
194{
195 if (cfs_rq->rb_leftmost == &se->run_node)
196 cfs_rq->rb_leftmost = rb_next(&se->run_node);
197 rb_erase(&se->run_node, &cfs_rq->tasks_timeline);
198 update_load_sub(&cfs_rq->load, se->load.weight);
199 cfs_rq->nr_running--;
200 se->on_rq = 0;
Ingo Molnara206c072007-09-05 14:32:49 +0200201
202 schedstat_add(cfs_rq, wait_runtime, -se->wait_runtime);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200203}
204
205static inline struct rb_node *first_fair(struct cfs_rq *cfs_rq)
206{
207 return cfs_rq->rb_leftmost;
208}
209
210static struct sched_entity *__pick_next_entity(struct cfs_rq *cfs_rq)
211{
212 return rb_entry(first_fair(cfs_rq), struct sched_entity, run_node);
213}
214
215/**************************************************************
216 * Scheduling class statistics methods:
217 */
218
219/*
Peter Zijlstra21805082007-08-25 18:41:53 +0200220 * Calculate the preemption granularity needed to schedule every
221 * runnable task once per sysctl_sched_latency amount of time.
222 * (down to a sensible low limit on granularity)
223 *
224 * For example, if there are 2 tasks running and latency is 10 msecs,
225 * we switch tasks every 5 msecs. If we have 3 tasks running, we have
226 * to switch tasks every 3.33 msecs to get a 10 msecs observed latency
227 * for each task. We do finer and finer scheduling up to until we
228 * reach the minimum granularity value.
229 *
230 * To achieve this we use the following dynamic-granularity rule:
231 *
232 * gran = lat/nr - lat/nr/nr
233 *
234 * This comes out of the following equations:
235 *
236 * kA1 + gran = kB1
237 * kB2 + gran = kA2
238 * kA2 = kA1
239 * kB2 = kB1 - d + d/nr
240 * lat = d * nr
241 *
242 * Where 'k' is key, 'A' is task A (waiting), 'B' is task B (running),
243 * '1' is start of time, '2' is end of time, 'd' is delay between
244 * 1 and 2 (during which task B was running), 'nr' is number of tasks
245 * running, 'lat' is the the period of each task. ('lat' is the
246 * sched_latency that we aim for.)
247 */
248static long
249sched_granularity(struct cfs_rq *cfs_rq)
250{
251 unsigned int gran = sysctl_sched_latency;
252 unsigned int nr = cfs_rq->nr_running;
253
254 if (nr > 1) {
255 gran = gran/nr - gran/nr/nr;
Ingo Molnar172ac3d2007-08-25 18:41:53 +0200256 gran = max(gran, sysctl_sched_min_granularity);
Peter Zijlstra21805082007-08-25 18:41:53 +0200257 }
258
259 return gran;
260}
261
262/*
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200263 * We rescale the rescheduling granularity of tasks according to their
264 * nice level, but only linearly, not exponentially:
265 */
266static long
267niced_granularity(struct sched_entity *curr, unsigned long granularity)
268{
269 u64 tmp;
270
Ingo Molnar7cff8cf2007-08-09 11:16:52 +0200271 if (likely(curr->load.weight == NICE_0_LOAD))
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200272 return granularity;
273 /*
Ingo Molnar7cff8cf2007-08-09 11:16:52 +0200274 * Positive nice levels get the same granularity as nice-0:
275 */
276 if (likely(curr->load.weight < NICE_0_LOAD)) {
277 tmp = curr->load.weight * (u64)granularity;
278 return (long) (tmp >> NICE_0_SHIFT);
279 }
280 /*
281 * Negative nice level tasks get linearly finer
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200282 * granularity:
283 */
Ingo Molnar7cff8cf2007-08-09 11:16:52 +0200284 tmp = curr->load.inv_weight * (u64)granularity;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200285
286 /*
287 * It will always fit into 'long':
288 */
Ingo Molnara0dc7262007-09-05 14:32:49 +0200289 return (long) (tmp >> (WMULT_SHIFT-NICE_0_SHIFT));
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200290}
291
292static inline void
293limit_wait_runtime(struct cfs_rq *cfs_rq, struct sched_entity *se)
294{
295 long limit = sysctl_sched_runtime_limit;
296
297 /*
298 * Niced tasks have the same history dynamic range as
299 * non-niced tasks:
300 */
301 if (unlikely(se->wait_runtime > limit)) {
302 se->wait_runtime = limit;
303 schedstat_inc(se, wait_runtime_overruns);
304 schedstat_inc(cfs_rq, wait_runtime_overruns);
305 }
306 if (unlikely(se->wait_runtime < -limit)) {
307 se->wait_runtime = -limit;
308 schedstat_inc(se, wait_runtime_underruns);
309 schedstat_inc(cfs_rq, wait_runtime_underruns);
310 }
311}
312
313static inline void
314__add_wait_runtime(struct cfs_rq *cfs_rq, struct sched_entity *se, long delta)
315{
316 se->wait_runtime += delta;
317 schedstat_add(se, sum_wait_runtime, delta);
318 limit_wait_runtime(cfs_rq, se);
319}
320
321static void
322add_wait_runtime(struct cfs_rq *cfs_rq, struct sched_entity *se, long delta)
323{
324 schedstat_add(cfs_rq, wait_runtime, -se->wait_runtime);
325 __add_wait_runtime(cfs_rq, se, delta);
326 schedstat_add(cfs_rq, wait_runtime, se->wait_runtime);
327}
328
329/*
330 * Update the current task's runtime statistics. Skip current tasks that
331 * are not in our scheduling class.
332 */
333static inline void
Ingo Molnar8ebc91d2007-10-15 17:00:03 +0200334__update_curr(struct cfs_rq *cfs_rq, struct sched_entity *curr,
335 unsigned long delta_exec)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200336{
Ingo Molnar8ebc91d2007-10-15 17:00:03 +0200337 unsigned long delta, delta_fair, delta_mine;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200338 struct load_weight *lw = &cfs_rq->load;
339 unsigned long load = lw->weight;
340
Ingo Molnar8179ca232007-08-02 17:41:40 +0200341 schedstat_set(curr->exec_max, max((u64)delta_exec, curr->exec_max));
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200342
343 curr->sum_exec_runtime += delta_exec;
344 cfs_rq->exec_clock += delta_exec;
345
Ingo Molnarfd8bb432007-08-09 11:16:46 +0200346 if (unlikely(!load))
347 return;
348
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200349 delta_fair = calc_delta_fair(delta_exec, lw);
350 delta_mine = calc_delta_mine(delta_exec, curr->load.weight, lw);
351
Mike Galbraith5f01d512007-08-28 12:53:24 +0200352 if (cfs_rq->sleeper_bonus > sysctl_sched_min_granularity) {
Peter Zijlstraea0aa3b2007-08-24 20:39:10 +0200353 delta = min((u64)delta_mine, cfs_rq->sleeper_bonus);
Ingo Molnarb2133c82007-08-24 20:39:10 +0200354 delta = min(delta, (unsigned long)(
355 (long)sysctl_sched_runtime_limit - curr->wait_runtime));
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200356 cfs_rq->sleeper_bonus -= delta;
357 delta_mine -= delta;
358 }
359
360 cfs_rq->fair_clock += delta_fair;
361 /*
362 * We executed delta_exec amount of time on the CPU,
363 * but we were only entitled to delta_mine amount of
364 * time during that period (if nr_running == 1 then
365 * the two values are equal)
366 * [Note: delta_mine - delta_exec is negative]:
367 */
368 add_wait_runtime(cfs_rq, curr, delta_mine - delta_exec);
369}
370
Ingo Molnarb7cc0892007-08-09 11:16:47 +0200371static void update_curr(struct cfs_rq *cfs_rq)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200372{
Ingo Molnar429d43b2007-10-15 17:00:03 +0200373 struct sched_entity *curr = cfs_rq->curr;
Ingo Molnar8ebc91d2007-10-15 17:00:03 +0200374 u64 now = rq_of(cfs_rq)->clock;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200375 unsigned long delta_exec;
376
377 if (unlikely(!curr))
378 return;
379
380 /*
381 * Get the amount of time the current task was running
382 * since the last time we changed load (this cannot
383 * overflow on 32 bits):
384 */
Ingo Molnar8ebc91d2007-10-15 17:00:03 +0200385 delta_exec = (unsigned long)(now - curr->exec_start);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200386
Ingo Molnar8ebc91d2007-10-15 17:00:03 +0200387 __update_curr(cfs_rq, curr, delta_exec);
388 curr->exec_start = now;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200389}
390
391static inline void
Ingo Molnar5870db52007-08-09 11:16:47 +0200392update_stats_wait_start(struct cfs_rq *cfs_rq, struct sched_entity *se)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200393{
394 se->wait_start_fair = cfs_rq->fair_clock;
Ingo Molnard2819182007-08-09 11:16:47 +0200395 schedstat_set(se->wait_start, rq_of(cfs_rq)->clock);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200396}
397
398/*
399 * We calculate fair deltas here, so protect against the random effects
400 * of a multiplication overflow by capping it to the runtime limit:
401 */
402#if BITS_PER_LONG == 32
403static inline unsigned long
404calc_weighted(unsigned long delta, unsigned long weight, int shift)
405{
406 u64 tmp = (u64)delta * weight >> shift;
407
408 if (unlikely(tmp > sysctl_sched_runtime_limit*2))
409 return sysctl_sched_runtime_limit*2;
410 return tmp;
411}
412#else
413static inline unsigned long
414calc_weighted(unsigned long delta, unsigned long weight, int shift)
415{
416 return delta * weight >> shift;
417}
418#endif
419
420/*
421 * Task is being enqueued - update stats:
422 */
Ingo Molnard2417e52007-08-09 11:16:47 +0200423static void update_stats_enqueue(struct cfs_rq *cfs_rq, struct sched_entity *se)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200424{
425 s64 key;
426
427 /*
428 * Are we enqueueing a waiting task? (for current tasks
429 * a dequeue/enqueue event is a NOP)
430 */
Ingo Molnar429d43b2007-10-15 17:00:03 +0200431 if (se != cfs_rq->curr)
Ingo Molnar5870db52007-08-09 11:16:47 +0200432 update_stats_wait_start(cfs_rq, se);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200433 /*
434 * Update the key:
435 */
436 key = cfs_rq->fair_clock;
437
438 /*
439 * Optimize the common nice 0 case:
440 */
441 if (likely(se->load.weight == NICE_0_LOAD)) {
442 key -= se->wait_runtime;
443 } else {
444 u64 tmp;
445
446 if (se->wait_runtime < 0) {
447 tmp = -se->wait_runtime;
448 key += (tmp * se->load.inv_weight) >>
449 (WMULT_SHIFT - NICE_0_SHIFT);
450 } else {
451 tmp = se->wait_runtime;
Ingo Molnara69edb52007-08-09 11:16:52 +0200452 key -= (tmp * se->load.inv_weight) >>
453 (WMULT_SHIFT - NICE_0_SHIFT);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200454 }
455 }
456
457 se->fair_key = key;
458}
459
460/*
461 * Note: must be called with a freshly updated rq->fair_clock.
462 */
463static inline void
Ingo Molnar8ebc91d2007-10-15 17:00:03 +0200464__update_stats_wait_end(struct cfs_rq *cfs_rq, struct sched_entity *se,
465 unsigned long delta_fair)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200466{
Ingo Molnard2819182007-08-09 11:16:47 +0200467 schedstat_set(se->wait_max, max(se->wait_max,
468 rq_of(cfs_rq)->clock - se->wait_start));
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200469
470 if (unlikely(se->load.weight != NICE_0_LOAD))
471 delta_fair = calc_weighted(delta_fair, se->load.weight,
472 NICE_0_SHIFT);
473
474 add_wait_runtime(cfs_rq, se, delta_fair);
475}
476
477static void
Ingo Molnar9ef0a962007-08-09 11:16:47 +0200478update_stats_wait_end(struct cfs_rq *cfs_rq, struct sched_entity *se)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200479{
480 unsigned long delta_fair;
481
Ingo Molnarb77d69d2007-08-28 12:53:24 +0200482 if (unlikely(!se->wait_start_fair))
483 return;
484
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200485 delta_fair = (unsigned long)min((u64)(2*sysctl_sched_runtime_limit),
486 (u64)(cfs_rq->fair_clock - se->wait_start_fair));
487
Ingo Molnar8ebc91d2007-10-15 17:00:03 +0200488 __update_stats_wait_end(cfs_rq, se, delta_fair);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200489
490 se->wait_start_fair = 0;
Ingo Molnar6cfb0d52007-08-02 17:41:40 +0200491 schedstat_set(se->wait_start, 0);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200492}
493
494static inline void
Ingo Molnar19b6a2e2007-08-09 11:16:48 +0200495update_stats_dequeue(struct cfs_rq *cfs_rq, struct sched_entity *se)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200496{
Ingo Molnarb7cc0892007-08-09 11:16:47 +0200497 update_curr(cfs_rq);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200498 /*
499 * Mark the end of the wait period if dequeueing a
500 * waiting task:
501 */
Ingo Molnar429d43b2007-10-15 17:00:03 +0200502 if (se != cfs_rq->curr)
Ingo Molnar9ef0a962007-08-09 11:16:47 +0200503 update_stats_wait_end(cfs_rq, se);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200504}
505
506/*
507 * We are picking a new current task - update its stats:
508 */
509static inline void
Ingo Molnar79303e92007-08-09 11:16:47 +0200510update_stats_curr_start(struct cfs_rq *cfs_rq, struct sched_entity *se)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200511{
512 /*
513 * We are starting a new run period:
514 */
Ingo Molnard2819182007-08-09 11:16:47 +0200515 se->exec_start = rq_of(cfs_rq)->clock;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200516}
517
518/*
519 * We are descheduling a task - update its stats:
520 */
521static inline void
Ingo Molnarc7e9b5b2007-08-09 11:16:48 +0200522update_stats_curr_end(struct cfs_rq *cfs_rq, struct sched_entity *se)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200523{
524 se->exec_start = 0;
525}
526
527/**************************************************
528 * Scheduling class queueing methods:
529 */
530
Ingo Molnar8ebc91d2007-10-15 17:00:03 +0200531static void __enqueue_sleeper(struct cfs_rq *cfs_rq, struct sched_entity *se,
532 unsigned long delta_fair)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200533{
Ingo Molnar8ebc91d2007-10-15 17:00:03 +0200534 unsigned long load = cfs_rq->load.weight;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200535 long prev_runtime;
536
Ingo Molnarb2133c82007-08-24 20:39:10 +0200537 /*
538 * Do not boost sleepers if there's too much bonus 'in flight'
539 * already:
540 */
541 if (unlikely(cfs_rq->sleeper_bonus > sysctl_sched_runtime_limit))
542 return;
543
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200544 if (sysctl_sched_features & SCHED_FEAT_SLEEPER_LOAD_AVG)
545 load = rq_of(cfs_rq)->cpu_load[2];
546
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200547 /*
548 * Fix up delta_fair with the effect of us running
549 * during the whole sleep period:
550 */
551 if (sysctl_sched_features & SCHED_FEAT_SLEEPER_AVG)
552 delta_fair = div64_likely32((u64)delta_fair * load,
553 load + se->load.weight);
554
555 if (unlikely(se->load.weight != NICE_0_LOAD))
556 delta_fair = calc_weighted(delta_fair, se->load.weight,
557 NICE_0_SHIFT);
558
559 prev_runtime = se->wait_runtime;
560 __add_wait_runtime(cfs_rq, se, delta_fair);
561 delta_fair = se->wait_runtime - prev_runtime;
562
563 /*
564 * Track the amount of bonus we've given to sleepers:
565 */
566 cfs_rq->sleeper_bonus += delta_fair;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200567}
568
Ingo Molnar2396af62007-08-09 11:16:48 +0200569static void enqueue_sleeper(struct cfs_rq *cfs_rq, struct sched_entity *se)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200570{
571 struct task_struct *tsk = task_of(se);
572 unsigned long delta_fair;
573
574 if ((entity_is_task(se) && tsk->policy == SCHED_BATCH) ||
575 !(sysctl_sched_features & SCHED_FEAT_FAIR_SLEEPERS))
576 return;
577
578 delta_fair = (unsigned long)min((u64)(2*sysctl_sched_runtime_limit),
579 (u64)(cfs_rq->fair_clock - se->sleep_start_fair));
580
Ingo Molnar8ebc91d2007-10-15 17:00:03 +0200581 __enqueue_sleeper(cfs_rq, se, delta_fair);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200582
583 se->sleep_start_fair = 0;
584
585#ifdef CONFIG_SCHEDSTATS
586 if (se->sleep_start) {
Ingo Molnard2819182007-08-09 11:16:47 +0200587 u64 delta = rq_of(cfs_rq)->clock - se->sleep_start;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200588
589 if ((s64)delta < 0)
590 delta = 0;
591
592 if (unlikely(delta > se->sleep_max))
593 se->sleep_max = delta;
594
595 se->sleep_start = 0;
596 se->sum_sleep_runtime += delta;
597 }
598 if (se->block_start) {
Ingo Molnard2819182007-08-09 11:16:47 +0200599 u64 delta = rq_of(cfs_rq)->clock - se->block_start;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200600
601 if ((s64)delta < 0)
602 delta = 0;
603
604 if (unlikely(delta > se->block_max))
605 se->block_max = delta;
606
607 se->block_start = 0;
608 se->sum_sleep_runtime += delta;
Ingo Molnar30084fb2007-10-02 14:13:08 +0200609
610 /*
611 * Blocking time is in units of nanosecs, so shift by 20 to
612 * get a milliseconds-range estimation of the amount of
613 * time that the task spent sleeping:
614 */
615 if (unlikely(prof_on == SLEEP_PROFILING)) {
616 profile_hits(SLEEP_PROFILING, (void *)get_wchan(tsk),
617 delta >> 20);
618 }
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200619 }
620#endif
621}
622
623static void
Ingo Molnar668031c2007-08-09 11:16:48 +0200624enqueue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se, int wakeup)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200625{
626 /*
627 * Update the fair clock.
628 */
Ingo Molnarb7cc0892007-08-09 11:16:47 +0200629 update_curr(cfs_rq);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200630
631 if (wakeup)
Ingo Molnar2396af62007-08-09 11:16:48 +0200632 enqueue_sleeper(cfs_rq, se);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200633
Ingo Molnard2417e52007-08-09 11:16:47 +0200634 update_stats_enqueue(cfs_rq, se);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200635 __enqueue_entity(cfs_rq, se);
636}
637
638static void
Ingo Molnar525c2712007-08-09 11:16:48 +0200639dequeue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se, int sleep)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200640{
Ingo Molnar19b6a2e2007-08-09 11:16:48 +0200641 update_stats_dequeue(cfs_rq, se);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200642 if (sleep) {
643 se->sleep_start_fair = cfs_rq->fair_clock;
644#ifdef CONFIG_SCHEDSTATS
645 if (entity_is_task(se)) {
646 struct task_struct *tsk = task_of(se);
647
648 if (tsk->state & TASK_INTERRUPTIBLE)
Ingo Molnard2819182007-08-09 11:16:47 +0200649 se->sleep_start = rq_of(cfs_rq)->clock;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200650 if (tsk->state & TASK_UNINTERRUPTIBLE)
Ingo Molnard2819182007-08-09 11:16:47 +0200651 se->block_start = rq_of(cfs_rq)->clock;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200652 }
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200653#endif
654 }
655 __dequeue_entity(cfs_rq, se);
656}
657
658/*
659 * Preempt the current task with a newly woken task if needed:
660 */
Peter Zijlstra7c92e542007-09-05 14:32:49 +0200661static void
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200662__check_preempt_curr_fair(struct cfs_rq *cfs_rq, struct sched_entity *se,
663 struct sched_entity *curr, unsigned long granularity)
664{
665 s64 __delta = curr->fair_key - se->fair_key;
Peter Zijlstra11697832007-09-05 14:32:49 +0200666 unsigned long ideal_runtime, delta_exec;
667
668 /*
669 * ideal_runtime is compared against sum_exec_runtime, which is
670 * walltime, hence do not scale.
671 */
672 ideal_runtime = max(sysctl_sched_latency / cfs_rq->nr_running,
673 (unsigned long)sysctl_sched_min_granularity);
674
675 /*
676 * If we executed more than what the latency constraint suggests,
677 * reduce the rescheduling granularity. This way the total latency
678 * of how much a task is not scheduled converges to
679 * sysctl_sched_latency:
680 */
681 delta_exec = curr->sum_exec_runtime - curr->prev_sum_exec_runtime;
682 if (delta_exec > ideal_runtime)
683 granularity = 0;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200684
685 /*
686 * Take scheduling granularity into account - do not
687 * preempt the current task unless the best task has
688 * a larger than sched_granularity fairness advantage:
Peter Zijlstra11697832007-09-05 14:32:49 +0200689 *
690 * scale granularity as key space is in fair_clock.
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200691 */
Peter Zijlstra4a55b452007-09-05 14:32:49 +0200692 if (__delta > niced_granularity(curr, granularity))
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200693 resched_task(rq_of(cfs_rq)->curr);
694}
695
696static inline void
Ingo Molnar8494f412007-08-09 11:16:48 +0200697set_next_entity(struct cfs_rq *cfs_rq, struct sched_entity *se)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200698{
699 /*
700 * Any task has to be enqueued before it get to execute on
701 * a CPU. So account for the time it spent waiting on the
702 * runqueue. (note, here we rely on pick_next_task() having
703 * done a put_prev_task_fair() shortly before this, which
704 * updated rq->fair_clock - used by update_stats_wait_end())
705 */
Ingo Molnar9ef0a962007-08-09 11:16:47 +0200706 update_stats_wait_end(cfs_rq, se);
Ingo Molnar79303e92007-08-09 11:16:47 +0200707 update_stats_curr_start(cfs_rq, se);
Ingo Molnar429d43b2007-10-15 17:00:03 +0200708 cfs_rq->curr = se;
Ingo Molnareba1ed42007-10-15 17:00:02 +0200709#ifdef CONFIG_SCHEDSTATS
710 /*
711 * Track our maximum slice length, if the CPU's load is at
712 * least twice that of our own weight (i.e. dont track it
713 * when there are only lesser-weight tasks around):
714 */
715 if (rq_of(cfs_rq)->ls.load.weight >= 2*se->load.weight) {
716 se->slice_max = max(se->slice_max,
717 se->sum_exec_runtime - se->prev_sum_exec_runtime);
718 }
719#endif
Peter Zijlstra4a55b452007-09-05 14:32:49 +0200720 se->prev_sum_exec_runtime = se->sum_exec_runtime;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200721}
722
Ingo Molnar9948f4b2007-08-09 11:16:48 +0200723static struct sched_entity *pick_next_entity(struct cfs_rq *cfs_rq)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200724{
725 struct sched_entity *se = __pick_next_entity(cfs_rq);
726
Ingo Molnar8494f412007-08-09 11:16:48 +0200727 set_next_entity(cfs_rq, se);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200728
729 return se;
730}
731
Ingo Molnarab6cde22007-08-09 11:16:48 +0200732static void put_prev_entity(struct cfs_rq *cfs_rq, struct sched_entity *prev)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200733{
734 /*
735 * If still on the runqueue then deactivate_task()
736 * was not called and update_curr() has to be done:
737 */
738 if (prev->on_rq)
Ingo Molnarb7cc0892007-08-09 11:16:47 +0200739 update_curr(cfs_rq);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200740
Ingo Molnarc7e9b5b2007-08-09 11:16:48 +0200741 update_stats_curr_end(cfs_rq, prev);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200742
743 if (prev->on_rq)
Ingo Molnar5870db52007-08-09 11:16:47 +0200744 update_stats_wait_start(cfs_rq, prev);
Ingo Molnar429d43b2007-10-15 17:00:03 +0200745 cfs_rq->curr = NULL;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200746}
747
748static void entity_tick(struct cfs_rq *cfs_rq, struct sched_entity *curr)
749{
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200750 struct sched_entity *next;
Ingo Molnarc1b3da32007-08-09 11:16:47 +0200751
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200752 /*
753 * Dequeue and enqueue the task to update its
754 * position within the tree:
755 */
Ingo Molnar525c2712007-08-09 11:16:48 +0200756 dequeue_entity(cfs_rq, curr, 0);
Ingo Molnar668031c2007-08-09 11:16:48 +0200757 enqueue_entity(cfs_rq, curr, 0);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200758
759 /*
760 * Reschedule if another task tops the current one.
761 */
762 next = __pick_next_entity(cfs_rq);
763 if (next == curr)
764 return;
765
Peter Zijlstra11697832007-09-05 14:32:49 +0200766 __check_preempt_curr_fair(cfs_rq, next, curr,
767 sched_granularity(cfs_rq));
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200768}
769
770/**************************************************
771 * CFS operations on tasks:
772 */
773
774#ifdef CONFIG_FAIR_GROUP_SCHED
775
776/* Walk up scheduling entities hierarchy */
777#define for_each_sched_entity(se) \
778 for (; se; se = se->parent)
779
780static inline struct cfs_rq *task_cfs_rq(struct task_struct *p)
781{
782 return p->se.cfs_rq;
783}
784
785/* runqueue on which this entity is (to be) queued */
786static inline struct cfs_rq *cfs_rq_of(struct sched_entity *se)
787{
788 return se->cfs_rq;
789}
790
791/* runqueue "owned" by this group */
792static inline struct cfs_rq *group_cfs_rq(struct sched_entity *grp)
793{
794 return grp->my_q;
795}
796
797/* Given a group's cfs_rq on one cpu, return its corresponding cfs_rq on
798 * another cpu ('this_cpu')
799 */
800static inline struct cfs_rq *cpu_cfs_rq(struct cfs_rq *cfs_rq, int this_cpu)
801{
802 /* A later patch will take group into account */
803 return &cpu_rq(this_cpu)->cfs;
804}
805
806/* Iterate thr' all leaf cfs_rq's on a runqueue */
807#define for_each_leaf_cfs_rq(rq, cfs_rq) \
808 list_for_each_entry(cfs_rq, &rq->leaf_cfs_rq_list, leaf_cfs_rq_list)
809
810/* Do the two (enqueued) tasks belong to the same group ? */
811static inline int is_same_group(struct task_struct *curr, struct task_struct *p)
812{
813 if (curr->se.cfs_rq == p->se.cfs_rq)
814 return 1;
815
816 return 0;
817}
818
819#else /* CONFIG_FAIR_GROUP_SCHED */
820
821#define for_each_sched_entity(se) \
822 for (; se; se = NULL)
823
824static inline struct cfs_rq *task_cfs_rq(struct task_struct *p)
825{
826 return &task_rq(p)->cfs;
827}
828
829static inline struct cfs_rq *cfs_rq_of(struct sched_entity *se)
830{
831 struct task_struct *p = task_of(se);
832 struct rq *rq = task_rq(p);
833
834 return &rq->cfs;
835}
836
837/* runqueue "owned" by this group */
838static inline struct cfs_rq *group_cfs_rq(struct sched_entity *grp)
839{
840 return NULL;
841}
842
843static inline struct cfs_rq *cpu_cfs_rq(struct cfs_rq *cfs_rq, int this_cpu)
844{
845 return &cpu_rq(this_cpu)->cfs;
846}
847
848#define for_each_leaf_cfs_rq(rq, cfs_rq) \
849 for (cfs_rq = &rq->cfs; cfs_rq; cfs_rq = NULL)
850
851static inline int is_same_group(struct task_struct *curr, struct task_struct *p)
852{
853 return 1;
854}
855
856#endif /* CONFIG_FAIR_GROUP_SCHED */
857
858/*
859 * The enqueue_task method is called before nr_running is
860 * increased. Here we update the fair scheduling stats and
861 * then put the task into the rbtree:
862 */
Ingo Molnarfd390f62007-08-09 11:16:48 +0200863static void enqueue_task_fair(struct rq *rq, struct task_struct *p, int wakeup)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200864{
865 struct cfs_rq *cfs_rq;
866 struct sched_entity *se = &p->se;
867
868 for_each_sched_entity(se) {
869 if (se->on_rq)
870 break;
871 cfs_rq = cfs_rq_of(se);
Ingo Molnar668031c2007-08-09 11:16:48 +0200872 enqueue_entity(cfs_rq, se, wakeup);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200873 }
874}
875
876/*
877 * The dequeue_task method is called before nr_running is
878 * decreased. We remove the task from the rbtree and
879 * update the fair scheduling stats:
880 */
Ingo Molnarf02231e2007-08-09 11:16:48 +0200881static void dequeue_task_fair(struct rq *rq, struct task_struct *p, int sleep)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200882{
883 struct cfs_rq *cfs_rq;
884 struct sched_entity *se = &p->se;
885
886 for_each_sched_entity(se) {
887 cfs_rq = cfs_rq_of(se);
Ingo Molnar525c2712007-08-09 11:16:48 +0200888 dequeue_entity(cfs_rq, se, sleep);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200889 /* Don't dequeue parent if it has other entities besides us */
890 if (cfs_rq->load.weight)
891 break;
892 }
893}
894
895/*
Ingo Molnar1799e352007-09-19 23:34:46 +0200896 * sched_yield() support is very simple - we dequeue and enqueue.
897 *
898 * If compat_yield is turned on then we requeue to the end of the tree.
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200899 */
900static void yield_task_fair(struct rq *rq, struct task_struct *p)
901{
902 struct cfs_rq *cfs_rq = task_cfs_rq(p);
Ingo Molnar1799e352007-09-19 23:34:46 +0200903 struct rb_node **link = &cfs_rq->tasks_timeline.rb_node;
904 struct sched_entity *rightmost, *se = &p->se;
905 struct rb_node *parent;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200906
907 /*
Ingo Molnar1799e352007-09-19 23:34:46 +0200908 * Are we the only task in the tree?
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200909 */
Ingo Molnar1799e352007-09-19 23:34:46 +0200910 if (unlikely(cfs_rq->nr_running == 1))
911 return;
912
913 if (likely(!sysctl_sched_compat_yield)) {
914 __update_rq_clock(rq);
915 /*
916 * Dequeue and enqueue the task to update its
917 * position within the tree:
918 */
919 dequeue_entity(cfs_rq, &p->se, 0);
920 enqueue_entity(cfs_rq, &p->se, 0);
921
922 return;
923 }
924 /*
925 * Find the rightmost entry in the rbtree:
926 */
927 do {
928 parent = *link;
929 link = &parent->rb_right;
930 } while (*link);
931
932 rightmost = rb_entry(parent, struct sched_entity, run_node);
933 /*
934 * Already in the rightmost position?
935 */
936 if (unlikely(rightmost == se))
937 return;
938
939 /*
940 * Minimally necessary key value to be last in the tree:
941 */
942 se->fair_key = rightmost->fair_key + 1;
943
944 if (cfs_rq->rb_leftmost == &se->run_node)
945 cfs_rq->rb_leftmost = rb_next(&se->run_node);
946 /*
947 * Relink the task to the rightmost position:
948 */
949 rb_erase(&se->run_node, &cfs_rq->tasks_timeline);
950 rb_link_node(&se->run_node, parent, link);
951 rb_insert_color(&se->run_node, &cfs_rq->tasks_timeline);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200952}
953
954/*
955 * Preempt the current task with a newly woken task if needed:
956 */
957static void check_preempt_curr_fair(struct rq *rq, struct task_struct *p)
958{
959 struct task_struct *curr = rq->curr;
960 struct cfs_rq *cfs_rq = task_cfs_rq(curr);
961 unsigned long gran;
962
963 if (unlikely(rt_prio(p->prio))) {
Ingo Molnara8e504d2007-08-09 11:16:47 +0200964 update_rq_clock(rq);
Ingo Molnarb7cc0892007-08-09 11:16:47 +0200965 update_curr(cfs_rq);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200966 resched_task(curr);
967 return;
968 }
969
970 gran = sysctl_sched_wakeup_granularity;
971 /*
972 * Batch tasks prefer throughput over latency:
973 */
974 if (unlikely(p->policy == SCHED_BATCH))
975 gran = sysctl_sched_batch_wakeup_granularity;
976
977 if (is_same_group(curr, p))
978 __check_preempt_curr_fair(cfs_rq, &p->se, &curr->se, gran);
979}
980
Ingo Molnarfb8d4722007-08-09 11:16:48 +0200981static struct task_struct *pick_next_task_fair(struct rq *rq)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200982{
983 struct cfs_rq *cfs_rq = &rq->cfs;
984 struct sched_entity *se;
985
986 if (unlikely(!cfs_rq->nr_running))
987 return NULL;
988
989 do {
Ingo Molnar9948f4b2007-08-09 11:16:48 +0200990 se = pick_next_entity(cfs_rq);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200991 cfs_rq = group_cfs_rq(se);
992 } while (cfs_rq);
993
994 return task_of(se);
995}
996
997/*
998 * Account for a descheduled task:
999 */
Ingo Molnar31ee5292007-08-09 11:16:49 +02001000static void put_prev_task_fair(struct rq *rq, struct task_struct *prev)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001001{
1002 struct sched_entity *se = &prev->se;
1003 struct cfs_rq *cfs_rq;
1004
1005 for_each_sched_entity(se) {
1006 cfs_rq = cfs_rq_of(se);
Ingo Molnarab6cde22007-08-09 11:16:48 +02001007 put_prev_entity(cfs_rq, se);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001008 }
1009}
1010
1011/**************************************************
1012 * Fair scheduling class load-balancing methods:
1013 */
1014
1015/*
1016 * Load-balancing iterator. Note: while the runqueue stays locked
1017 * during the whole iteration, the current task might be
1018 * dequeued so the iterator has to be dequeue-safe. Here we
1019 * achieve that by always pre-iterating before returning
1020 * the current task:
1021 */
1022static inline struct task_struct *
1023__load_balance_iterator(struct cfs_rq *cfs_rq, struct rb_node *curr)
1024{
1025 struct task_struct *p;
1026
1027 if (!curr)
1028 return NULL;
1029
1030 p = rb_entry(curr, struct task_struct, se.run_node);
1031 cfs_rq->rb_load_balance_curr = rb_next(curr);
1032
1033 return p;
1034}
1035
1036static struct task_struct *load_balance_start_fair(void *arg)
1037{
1038 struct cfs_rq *cfs_rq = arg;
1039
1040 return __load_balance_iterator(cfs_rq, first_fair(cfs_rq));
1041}
1042
1043static struct task_struct *load_balance_next_fair(void *arg)
1044{
1045 struct cfs_rq *cfs_rq = arg;
1046
1047 return __load_balance_iterator(cfs_rq, cfs_rq->rb_load_balance_curr);
1048}
1049
Peter Williamsa4ac01c2007-08-09 11:16:46 +02001050#ifdef CONFIG_FAIR_GROUP_SCHED
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001051static int cfs_rq_best_prio(struct cfs_rq *cfs_rq)
1052{
1053 struct sched_entity *curr;
1054 struct task_struct *p;
1055
1056 if (!cfs_rq->nr_running)
1057 return MAX_PRIO;
1058
1059 curr = __pick_next_entity(cfs_rq);
1060 p = task_of(curr);
1061
1062 return p->prio;
1063}
Peter Williamsa4ac01c2007-08-09 11:16:46 +02001064#endif
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001065
Peter Williams43010652007-08-09 11:16:46 +02001066static unsigned long
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001067load_balance_fair(struct rq *this_rq, int this_cpu, struct rq *busiest,
Peter Williamsa4ac01c2007-08-09 11:16:46 +02001068 unsigned long max_nr_move, unsigned long max_load_move,
1069 struct sched_domain *sd, enum cpu_idle_type idle,
1070 int *all_pinned, int *this_best_prio)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001071{
1072 struct cfs_rq *busy_cfs_rq;
1073 unsigned long load_moved, total_nr_moved = 0, nr_moved;
1074 long rem_load_move = max_load_move;
1075 struct rq_iterator cfs_rq_iterator;
1076
1077 cfs_rq_iterator.start = load_balance_start_fair;
1078 cfs_rq_iterator.next = load_balance_next_fair;
1079
1080 for_each_leaf_cfs_rq(busiest, busy_cfs_rq) {
Peter Williamsa4ac01c2007-08-09 11:16:46 +02001081#ifdef CONFIG_FAIR_GROUP_SCHED
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001082 struct cfs_rq *this_cfs_rq;
Ingo Molnare56f31a2007-08-10 23:05:11 +02001083 long imbalance;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001084 unsigned long maxload;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001085
1086 this_cfs_rq = cpu_cfs_rq(busy_cfs_rq, this_cpu);
1087
Ingo Molnare56f31a2007-08-10 23:05:11 +02001088 imbalance = busy_cfs_rq->load.weight - this_cfs_rq->load.weight;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001089 /* Don't pull if this_cfs_rq has more load than busy_cfs_rq */
1090 if (imbalance <= 0)
1091 continue;
1092
1093 /* Don't pull more than imbalance/2 */
1094 imbalance /= 2;
1095 maxload = min(rem_load_move, imbalance);
1096
Peter Williamsa4ac01c2007-08-09 11:16:46 +02001097 *this_best_prio = cfs_rq_best_prio(this_cfs_rq);
1098#else
Ingo Molnare56f31a2007-08-10 23:05:11 +02001099# define maxload rem_load_move
Peter Williamsa4ac01c2007-08-09 11:16:46 +02001100#endif
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001101 /* pass busy_cfs_rq argument into
1102 * load_balance_[start|next]_fair iterators
1103 */
1104 cfs_rq_iterator.arg = busy_cfs_rq;
1105 nr_moved = balance_tasks(this_rq, this_cpu, busiest,
1106 max_nr_move, maxload, sd, idle, all_pinned,
Peter Williamsa4ac01c2007-08-09 11:16:46 +02001107 &load_moved, this_best_prio, &cfs_rq_iterator);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001108
1109 total_nr_moved += nr_moved;
1110 max_nr_move -= nr_moved;
1111 rem_load_move -= load_moved;
1112
1113 if (max_nr_move <= 0 || rem_load_move <= 0)
1114 break;
1115 }
1116
Peter Williams43010652007-08-09 11:16:46 +02001117 return max_load_move - rem_load_move;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001118}
1119
1120/*
1121 * scheduler tick hitting a task of our scheduling class:
1122 */
1123static void task_tick_fair(struct rq *rq, struct task_struct *curr)
1124{
1125 struct cfs_rq *cfs_rq;
1126 struct sched_entity *se = &curr->se;
1127
1128 for_each_sched_entity(se) {
1129 cfs_rq = cfs_rq_of(se);
1130 entity_tick(cfs_rq, se);
1131 }
1132}
1133
1134/*
1135 * Share the fairness runtime between parent and child, thus the
1136 * total amount of pressure for CPU stays equal - new tasks
1137 * get a chance to run but frequent forkers are not allowed to
1138 * monopolize the CPU. Note: the parent runqueue is locked,
1139 * the child is not running yet.
1140 */
Ingo Molnaree0827d2007-08-09 11:16:49 +02001141static void task_new_fair(struct rq *rq, struct task_struct *p)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001142{
1143 struct cfs_rq *cfs_rq = task_cfs_rq(p);
Ingo Molnar429d43b2007-10-15 17:00:03 +02001144 struct sched_entity *se = &p->se, *curr = cfs_rq->curr;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001145
1146 sched_info_queued(p);
1147
Ting Yang7109c442007-08-28 12:53:24 +02001148 update_curr(cfs_rq);
Ingo Molnard2417e52007-08-09 11:16:47 +02001149 update_stats_enqueue(cfs_rq, se);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001150 /*
1151 * Child runs first: we let it run before the parent
1152 * until it reschedules once. We set up the key so that
1153 * it will preempt the parent:
1154 */
Ingo Molnar9f508f82007-08-28 12:53:24 +02001155 se->fair_key = curr->fair_key -
Ting Yang7109c442007-08-28 12:53:24 +02001156 niced_granularity(curr, sched_granularity(cfs_rq)) - 1;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001157 /*
1158 * The first wait is dominated by the child-runs-first logic,
1159 * so do not credit it with that waiting time yet:
1160 */
1161 if (sysctl_sched_features & SCHED_FEAT_SKIP_INITIAL)
Ingo Molnar9f508f82007-08-28 12:53:24 +02001162 se->wait_start_fair = 0;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001163
1164 /*
1165 * The statistical average of wait_runtime is about
1166 * -granularity/2, so initialize the task with that:
1167 */
Ingo Molnara206c072007-09-05 14:32:49 +02001168 if (sysctl_sched_features & SCHED_FEAT_START_DEBIT)
Ingo Molnar9f508f82007-08-28 12:53:24 +02001169 se->wait_runtime = -(sched_granularity(cfs_rq) / 2);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001170
1171 __enqueue_entity(cfs_rq, se);
Ingo Molnarbb61c212007-10-15 17:00:02 +02001172 resched_task(rq->curr);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001173}
1174
1175#ifdef CONFIG_FAIR_GROUP_SCHED
1176/* Account for a task changing its policy or group.
1177 *
1178 * This routine is mostly called to set cfs_rq->curr field when a task
1179 * migrates between groups/classes.
1180 */
1181static void set_curr_task_fair(struct rq *rq)
1182{
Bruce Ashfield7c6c16f2007-08-24 20:39:10 +02001183 struct sched_entity *se = &rq->curr->se;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001184
Ingo Molnarc3b64f12007-08-09 11:16:51 +02001185 for_each_sched_entity(se)
1186 set_next_entity(cfs_rq_of(se), se);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001187}
1188#else
1189static void set_curr_task_fair(struct rq *rq)
1190{
1191}
1192#endif
1193
1194/*
1195 * All the scheduling class methods:
1196 */
1197struct sched_class fair_sched_class __read_mostly = {
1198 .enqueue_task = enqueue_task_fair,
1199 .dequeue_task = dequeue_task_fair,
1200 .yield_task = yield_task_fair,
1201
1202 .check_preempt_curr = check_preempt_curr_fair,
1203
1204 .pick_next_task = pick_next_task_fair,
1205 .put_prev_task = put_prev_task_fair,
1206
1207 .load_balance = load_balance_fair,
1208
1209 .set_curr_task = set_curr_task_fair,
1210 .task_tick = task_tick_fair,
1211 .task_new = task_new_fair,
1212};
1213
1214#ifdef CONFIG_SCHED_DEBUG
Ingo Molnar5cef9ec2007-08-09 11:16:47 +02001215static void print_cfs_stats(struct seq_file *m, int cpu)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001216{
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001217 struct cfs_rq *cfs_rq;
1218
Ingo Molnarc3b64f12007-08-09 11:16:51 +02001219 for_each_leaf_cfs_rq(cpu_rq(cpu), cfs_rq)
Ingo Molnar5cef9ec2007-08-09 11:16:47 +02001220 print_cfs_rq(m, cpu, cfs_rq);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001221}
1222#endif