blob: 2d01bbc2d04a2a70d429cce65cdce2f5940ae1ca [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/*
Peter Zijlstra21805082007-08-25 18:41:53 +020024 * Targeted preemption latency for CPU-bound tasks:
25 * (default: 20ms, units: nanoseconds)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +020026 *
Peter Zijlstra21805082007-08-25 18:41:53 +020027 * NOTE: this latency value is not the same as the concept of
28 * 'timeslice length' - timeslices in CFS are of variable length.
29 * (to see the precise effective timeslice length of your workload,
30 * run vmstat and monitor the context-switches field)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +020031 *
32 * On SMP systems the value of this is multiplied by the log2 of the
33 * number of CPUs. (i.e. factor 2x on 2-way systems, 3x on 4-way
34 * systems, 4x on 8-way systems, 5x on 16-way systems, etc.)
Peter Zijlstra21805082007-08-25 18:41:53 +020035 * Targeted preemption latency for CPU-bound tasks:
Ingo Molnarbf0f6f22007-07-09 18:51:58 +020036 */
Peter Zijlstra21805082007-08-25 18:41:53 +020037unsigned int sysctl_sched_latency __read_mostly = 20000000ULL;
38
39/*
40 * Minimal preemption granularity for CPU-bound tasks:
41 * (default: 2 msec, units: nanoseconds)
42 */
Ingo Molnar172ac3d2007-08-25 18:41:53 +020043unsigned int sysctl_sched_min_granularity __read_mostly = 2000000ULL;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +020044
45/*
46 * SCHED_BATCH wake-up granularity.
Ingo Molnar71fd3712007-08-24 20:39:10 +020047 * (default: 25 msec, units: nanoseconds)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +020048 *
49 * This option delays the preemption effects of decoupled workloads
50 * and reduces their over-scheduling. Synchronous workloads will still
51 * have immediate wakeup/sleep latencies.
52 */
Ingo Molnar71fd3712007-08-24 20:39:10 +020053unsigned int sysctl_sched_batch_wakeup_granularity __read_mostly = 25000000UL;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +020054
55/*
56 * SCHED_OTHER wake-up granularity.
57 * (default: 1 msec, units: nanoseconds)
58 *
59 * This option delays the preemption effects of decoupled workloads
60 * and reduces their over-scheduling. Synchronous workloads will still
61 * have immediate wakeup/sleep latencies.
62 */
Ingo Molnar71fd3712007-08-24 20:39:10 +020063unsigned int sysctl_sched_wakeup_granularity __read_mostly = 1000000UL;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +020064
65unsigned int sysctl_sched_stat_granularity __read_mostly;
66
67/*
Ingo Molnar71fd3712007-08-24 20:39:10 +020068 * Initialized in sched_init_granularity() [to 5 times the base granularity]:
Ingo Molnarbf0f6f22007-07-09 18:51:58 +020069 */
70unsigned int sysctl_sched_runtime_limit __read_mostly;
71
72/*
73 * Debugging: various feature bits
74 */
75enum {
76 SCHED_FEAT_FAIR_SLEEPERS = 1,
77 SCHED_FEAT_SLEEPER_AVG = 2,
78 SCHED_FEAT_SLEEPER_LOAD_AVG = 4,
79 SCHED_FEAT_PRECISE_CPU_LOAD = 8,
80 SCHED_FEAT_START_DEBIT = 16,
81 SCHED_FEAT_SKIP_INITIAL = 32,
82};
83
84unsigned int sysctl_sched_features __read_mostly =
85 SCHED_FEAT_FAIR_SLEEPERS *1 |
Ingo Molnar5d2b3d32007-08-12 18:08:19 +020086 SCHED_FEAT_SLEEPER_AVG *0 |
Ingo Molnarbf0f6f22007-07-09 18:51:58 +020087 SCHED_FEAT_SLEEPER_LOAD_AVG *1 |
88 SCHED_FEAT_PRECISE_CPU_LOAD *1 |
89 SCHED_FEAT_START_DEBIT *1 |
90 SCHED_FEAT_SKIP_INITIAL *0;
91
92extern struct sched_class fair_sched_class;
93
94/**************************************************************
95 * CFS operations on generic schedulable entities:
96 */
97
98#ifdef CONFIG_FAIR_GROUP_SCHED
99
100/* cpu runqueue to which this cfs_rq is attached */
101static inline struct rq *rq_of(struct cfs_rq *cfs_rq)
102{
103 return cfs_rq->rq;
104}
105
106/* currently running entity (if any) on this cfs_rq */
107static inline struct sched_entity *cfs_rq_curr(struct cfs_rq *cfs_rq)
108{
109 return cfs_rq->curr;
110}
111
112/* An entity is a task if it doesn't "own" a runqueue */
113#define entity_is_task(se) (!se->my_q)
114
115static inline void
116set_cfs_rq_curr(struct cfs_rq *cfs_rq, struct sched_entity *se)
117{
118 cfs_rq->curr = se;
119}
120
121#else /* CONFIG_FAIR_GROUP_SCHED */
122
123static inline struct rq *rq_of(struct cfs_rq *cfs_rq)
124{
125 return container_of(cfs_rq, struct rq, cfs);
126}
127
128static inline struct sched_entity *cfs_rq_curr(struct cfs_rq *cfs_rq)
129{
130 struct rq *rq = rq_of(cfs_rq);
131
132 if (unlikely(rq->curr->sched_class != &fair_sched_class))
133 return NULL;
134
135 return &rq->curr->se;
136}
137
138#define entity_is_task(se) 1
139
140static inline void
141set_cfs_rq_curr(struct cfs_rq *cfs_rq, struct sched_entity *se) { }
142
143#endif /* CONFIG_FAIR_GROUP_SCHED */
144
145static inline struct task_struct *task_of(struct sched_entity *se)
146{
147 return container_of(se, struct task_struct, se);
148}
149
150
151/**************************************************************
152 * Scheduling class tree data structure manipulation methods:
153 */
154
155/*
156 * Enqueue an entity into the rb-tree:
157 */
158static inline void
159__enqueue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se)
160{
161 struct rb_node **link = &cfs_rq->tasks_timeline.rb_node;
162 struct rb_node *parent = NULL;
163 struct sched_entity *entry;
164 s64 key = se->fair_key;
165 int leftmost = 1;
166
167 /*
168 * Find the right place in the rbtree:
169 */
170 while (*link) {
171 parent = *link;
172 entry = rb_entry(parent, struct sched_entity, run_node);
173 /*
174 * We dont care about collisions. Nodes with
175 * the same key stay together.
176 */
177 if (key - entry->fair_key < 0) {
178 link = &parent->rb_left;
179 } else {
180 link = &parent->rb_right;
181 leftmost = 0;
182 }
183 }
184
185 /*
186 * Maintain a cache of leftmost tree entries (it is frequently
187 * used):
188 */
189 if (leftmost)
190 cfs_rq->rb_leftmost = &se->run_node;
191
192 rb_link_node(&se->run_node, parent, link);
193 rb_insert_color(&se->run_node, &cfs_rq->tasks_timeline);
194 update_load_add(&cfs_rq->load, se->load.weight);
195 cfs_rq->nr_running++;
196 se->on_rq = 1;
Ingo Molnara206c072007-09-05 14:32:49 +0200197
198 schedstat_add(cfs_rq, wait_runtime, se->wait_runtime);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200199}
200
201static inline void
202__dequeue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se)
203{
204 if (cfs_rq->rb_leftmost == &se->run_node)
205 cfs_rq->rb_leftmost = rb_next(&se->run_node);
206 rb_erase(&se->run_node, &cfs_rq->tasks_timeline);
207 update_load_sub(&cfs_rq->load, se->load.weight);
208 cfs_rq->nr_running--;
209 se->on_rq = 0;
Ingo Molnara206c072007-09-05 14:32:49 +0200210
211 schedstat_add(cfs_rq, wait_runtime, -se->wait_runtime);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200212}
213
214static inline struct rb_node *first_fair(struct cfs_rq *cfs_rq)
215{
216 return cfs_rq->rb_leftmost;
217}
218
219static struct sched_entity *__pick_next_entity(struct cfs_rq *cfs_rq)
220{
221 return rb_entry(first_fair(cfs_rq), struct sched_entity, run_node);
222}
223
224/**************************************************************
225 * Scheduling class statistics methods:
226 */
227
228/*
Peter Zijlstra21805082007-08-25 18:41:53 +0200229 * Calculate the preemption granularity needed to schedule every
230 * runnable task once per sysctl_sched_latency amount of time.
231 * (down to a sensible low limit on granularity)
232 *
233 * For example, if there are 2 tasks running and latency is 10 msecs,
234 * we switch tasks every 5 msecs. If we have 3 tasks running, we have
235 * to switch tasks every 3.33 msecs to get a 10 msecs observed latency
236 * for each task. We do finer and finer scheduling up to until we
237 * reach the minimum granularity value.
238 *
239 * To achieve this we use the following dynamic-granularity rule:
240 *
241 * gran = lat/nr - lat/nr/nr
242 *
243 * This comes out of the following equations:
244 *
245 * kA1 + gran = kB1
246 * kB2 + gran = kA2
247 * kA2 = kA1
248 * kB2 = kB1 - d + d/nr
249 * lat = d * nr
250 *
251 * Where 'k' is key, 'A' is task A (waiting), 'B' is task B (running),
252 * '1' is start of time, '2' is end of time, 'd' is delay between
253 * 1 and 2 (during which task B was running), 'nr' is number of tasks
254 * running, 'lat' is the the period of each task. ('lat' is the
255 * sched_latency that we aim for.)
256 */
257static long
258sched_granularity(struct cfs_rq *cfs_rq)
259{
260 unsigned int gran = sysctl_sched_latency;
261 unsigned int nr = cfs_rq->nr_running;
262
263 if (nr > 1) {
264 gran = gran/nr - gran/nr/nr;
Ingo Molnar172ac3d2007-08-25 18:41:53 +0200265 gran = max(gran, sysctl_sched_min_granularity);
Peter Zijlstra21805082007-08-25 18:41:53 +0200266 }
267
268 return gran;
269}
270
271/*
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200272 * We rescale the rescheduling granularity of tasks according to their
273 * nice level, but only linearly, not exponentially:
274 */
275static long
276niced_granularity(struct sched_entity *curr, unsigned long granularity)
277{
278 u64 tmp;
279
Ingo Molnar7cff8cf2007-08-09 11:16:52 +0200280 if (likely(curr->load.weight == NICE_0_LOAD))
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200281 return granularity;
282 /*
Ingo Molnar7cff8cf2007-08-09 11:16:52 +0200283 * Positive nice levels get the same granularity as nice-0:
284 */
285 if (likely(curr->load.weight < NICE_0_LOAD)) {
286 tmp = curr->load.weight * (u64)granularity;
287 return (long) (tmp >> NICE_0_SHIFT);
288 }
289 /*
290 * Negative nice level tasks get linearly finer
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200291 * granularity:
292 */
Ingo Molnar7cff8cf2007-08-09 11:16:52 +0200293 tmp = curr->load.inv_weight * (u64)granularity;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200294
295 /*
296 * It will always fit into 'long':
297 */
Ingo Molnara0dc7262007-09-05 14:32:49 +0200298 return (long) (tmp >> (WMULT_SHIFT-NICE_0_SHIFT));
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200299}
300
301static inline void
302limit_wait_runtime(struct cfs_rq *cfs_rq, struct sched_entity *se)
303{
304 long limit = sysctl_sched_runtime_limit;
305
306 /*
307 * Niced tasks have the same history dynamic range as
308 * non-niced tasks:
309 */
310 if (unlikely(se->wait_runtime > limit)) {
311 se->wait_runtime = limit;
312 schedstat_inc(se, wait_runtime_overruns);
313 schedstat_inc(cfs_rq, wait_runtime_overruns);
314 }
315 if (unlikely(se->wait_runtime < -limit)) {
316 se->wait_runtime = -limit;
317 schedstat_inc(se, wait_runtime_underruns);
318 schedstat_inc(cfs_rq, wait_runtime_underruns);
319 }
320}
321
322static inline void
323__add_wait_runtime(struct cfs_rq *cfs_rq, struct sched_entity *se, long delta)
324{
325 se->wait_runtime += delta;
326 schedstat_add(se, sum_wait_runtime, delta);
327 limit_wait_runtime(cfs_rq, se);
328}
329
330static void
331add_wait_runtime(struct cfs_rq *cfs_rq, struct sched_entity *se, long delta)
332{
333 schedstat_add(cfs_rq, wait_runtime, -se->wait_runtime);
334 __add_wait_runtime(cfs_rq, se, delta);
335 schedstat_add(cfs_rq, wait_runtime, se->wait_runtime);
336}
337
338/*
339 * Update the current task's runtime statistics. Skip current tasks that
340 * are not in our scheduling class.
341 */
342static inline void
Ingo Molnarb7cc0892007-08-09 11:16:47 +0200343__update_curr(struct cfs_rq *cfs_rq, struct sched_entity *curr)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200344{
Ingo Molnarc5dcfe72007-08-09 11:16:46 +0200345 unsigned long delta, delta_exec, delta_fair, delta_mine;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200346 struct load_weight *lw = &cfs_rq->load;
347 unsigned long load = lw->weight;
348
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200349 delta_exec = curr->delta_exec;
Ingo Molnar8179ca232007-08-02 17:41:40 +0200350 schedstat_set(curr->exec_max, max((u64)delta_exec, curr->exec_max));
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200351
352 curr->sum_exec_runtime += delta_exec;
353 cfs_rq->exec_clock += delta_exec;
354
Ingo Molnarfd8bb432007-08-09 11:16:46 +0200355 if (unlikely(!load))
356 return;
357
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200358 delta_fair = calc_delta_fair(delta_exec, lw);
359 delta_mine = calc_delta_mine(delta_exec, curr->load.weight, lw);
360
Mike Galbraith5f01d512007-08-28 12:53:24 +0200361 if (cfs_rq->sleeper_bonus > sysctl_sched_min_granularity) {
Peter Zijlstraea0aa3b2007-08-24 20:39:10 +0200362 delta = min((u64)delta_mine, cfs_rq->sleeper_bonus);
Ingo Molnarb2133c82007-08-24 20:39:10 +0200363 delta = min(delta, (unsigned long)(
364 (long)sysctl_sched_runtime_limit - curr->wait_runtime));
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200365 cfs_rq->sleeper_bonus -= delta;
366 delta_mine -= delta;
367 }
368
369 cfs_rq->fair_clock += delta_fair;
370 /*
371 * We executed delta_exec amount of time on the CPU,
372 * but we were only entitled to delta_mine amount of
373 * time during that period (if nr_running == 1 then
374 * the two values are equal)
375 * [Note: delta_mine - delta_exec is negative]:
376 */
377 add_wait_runtime(cfs_rq, curr, delta_mine - delta_exec);
378}
379
Ingo Molnarb7cc0892007-08-09 11:16:47 +0200380static void update_curr(struct cfs_rq *cfs_rq)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200381{
382 struct sched_entity *curr = cfs_rq_curr(cfs_rq);
383 unsigned long delta_exec;
384
385 if (unlikely(!curr))
386 return;
387
388 /*
389 * Get the amount of time the current task was running
390 * since the last time we changed load (this cannot
391 * overflow on 32 bits):
392 */
Ingo Molnard2819182007-08-09 11:16:47 +0200393 delta_exec = (unsigned long)(rq_of(cfs_rq)->clock - curr->exec_start);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200394
395 curr->delta_exec += delta_exec;
396
397 if (unlikely(curr->delta_exec > sysctl_sched_stat_granularity)) {
Ingo Molnarb7cc0892007-08-09 11:16:47 +0200398 __update_curr(cfs_rq, curr);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200399 curr->delta_exec = 0;
400 }
Ingo Molnard2819182007-08-09 11:16:47 +0200401 curr->exec_start = rq_of(cfs_rq)->clock;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200402}
403
404static inline void
Ingo Molnar5870db52007-08-09 11:16:47 +0200405update_stats_wait_start(struct cfs_rq *cfs_rq, struct sched_entity *se)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200406{
407 se->wait_start_fair = cfs_rq->fair_clock;
Ingo Molnard2819182007-08-09 11:16:47 +0200408 schedstat_set(se->wait_start, rq_of(cfs_rq)->clock);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200409}
410
411/*
412 * We calculate fair deltas here, so protect against the random effects
413 * of a multiplication overflow by capping it to the runtime limit:
414 */
415#if BITS_PER_LONG == 32
416static inline unsigned long
417calc_weighted(unsigned long delta, unsigned long weight, int shift)
418{
419 u64 tmp = (u64)delta * weight >> shift;
420
421 if (unlikely(tmp > sysctl_sched_runtime_limit*2))
422 return sysctl_sched_runtime_limit*2;
423 return tmp;
424}
425#else
426static inline unsigned long
427calc_weighted(unsigned long delta, unsigned long weight, int shift)
428{
429 return delta * weight >> shift;
430}
431#endif
432
433/*
434 * Task is being enqueued - update stats:
435 */
Ingo Molnard2417e52007-08-09 11:16:47 +0200436static void update_stats_enqueue(struct cfs_rq *cfs_rq, struct sched_entity *se)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200437{
438 s64 key;
439
440 /*
441 * Are we enqueueing a waiting task? (for current tasks
442 * a dequeue/enqueue event is a NOP)
443 */
444 if (se != cfs_rq_curr(cfs_rq))
Ingo Molnar5870db52007-08-09 11:16:47 +0200445 update_stats_wait_start(cfs_rq, se);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200446 /*
447 * Update the key:
448 */
449 key = cfs_rq->fair_clock;
450
451 /*
452 * Optimize the common nice 0 case:
453 */
454 if (likely(se->load.weight == NICE_0_LOAD)) {
455 key -= se->wait_runtime;
456 } else {
457 u64 tmp;
458
459 if (se->wait_runtime < 0) {
460 tmp = -se->wait_runtime;
461 key += (tmp * se->load.inv_weight) >>
462 (WMULT_SHIFT - NICE_0_SHIFT);
463 } else {
464 tmp = se->wait_runtime;
Ingo Molnara69edb52007-08-09 11:16:52 +0200465 key -= (tmp * se->load.inv_weight) >>
466 (WMULT_SHIFT - NICE_0_SHIFT);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200467 }
468 }
469
470 se->fair_key = key;
471}
472
473/*
474 * Note: must be called with a freshly updated rq->fair_clock.
475 */
476static inline void
Ingo Molnareac55ea2007-08-09 11:16:47 +0200477__update_stats_wait_end(struct cfs_rq *cfs_rq, struct sched_entity *se)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200478{
479 unsigned long delta_fair = se->delta_fair_run;
480
Ingo Molnard2819182007-08-09 11:16:47 +0200481 schedstat_set(se->wait_max, max(se->wait_max,
482 rq_of(cfs_rq)->clock - se->wait_start));
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200483
484 if (unlikely(se->load.weight != NICE_0_LOAD))
485 delta_fair = calc_weighted(delta_fair, se->load.weight,
486 NICE_0_SHIFT);
487
488 add_wait_runtime(cfs_rq, se, delta_fair);
489}
490
491static void
Ingo Molnar9ef0a962007-08-09 11:16:47 +0200492update_stats_wait_end(struct cfs_rq *cfs_rq, struct sched_entity *se)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200493{
494 unsigned long delta_fair;
495
Ingo Molnarb77d69d2007-08-28 12:53:24 +0200496 if (unlikely(!se->wait_start_fair))
497 return;
498
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200499 delta_fair = (unsigned long)min((u64)(2*sysctl_sched_runtime_limit),
500 (u64)(cfs_rq->fair_clock - se->wait_start_fair));
501
502 se->delta_fair_run += delta_fair;
503 if (unlikely(abs(se->delta_fair_run) >=
504 sysctl_sched_stat_granularity)) {
Ingo Molnareac55ea2007-08-09 11:16:47 +0200505 __update_stats_wait_end(cfs_rq, se);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200506 se->delta_fair_run = 0;
507 }
508
509 se->wait_start_fair = 0;
Ingo Molnar6cfb0d52007-08-02 17:41:40 +0200510 schedstat_set(se->wait_start, 0);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200511}
512
513static inline void
Ingo Molnar19b6a2e2007-08-09 11:16:48 +0200514update_stats_dequeue(struct cfs_rq *cfs_rq, struct sched_entity *se)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200515{
Ingo Molnarb7cc0892007-08-09 11:16:47 +0200516 update_curr(cfs_rq);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200517 /*
518 * Mark the end of the wait period if dequeueing a
519 * waiting task:
520 */
521 if (se != cfs_rq_curr(cfs_rq))
Ingo Molnar9ef0a962007-08-09 11:16:47 +0200522 update_stats_wait_end(cfs_rq, se);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200523}
524
525/*
526 * We are picking a new current task - update its stats:
527 */
528static inline void
Ingo Molnar79303e92007-08-09 11:16:47 +0200529update_stats_curr_start(struct cfs_rq *cfs_rq, struct sched_entity *se)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200530{
531 /*
532 * We are starting a new run period:
533 */
Ingo Molnard2819182007-08-09 11:16:47 +0200534 se->exec_start = rq_of(cfs_rq)->clock;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200535}
536
537/*
538 * We are descheduling a task - update its stats:
539 */
540static inline void
Ingo Molnarc7e9b5b2007-08-09 11:16:48 +0200541update_stats_curr_end(struct cfs_rq *cfs_rq, struct sched_entity *se)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200542{
543 se->exec_start = 0;
544}
545
546/**************************************************
547 * Scheduling class queueing methods:
548 */
549
Ingo Molnardfdc1192007-08-09 11:16:48 +0200550static void __enqueue_sleeper(struct cfs_rq *cfs_rq, struct sched_entity *se)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200551{
552 unsigned long load = cfs_rq->load.weight, delta_fair;
553 long prev_runtime;
554
Ingo Molnarb2133c82007-08-24 20:39:10 +0200555 /*
556 * Do not boost sleepers if there's too much bonus 'in flight'
557 * already:
558 */
559 if (unlikely(cfs_rq->sleeper_bonus > sysctl_sched_runtime_limit))
560 return;
561
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200562 if (sysctl_sched_features & SCHED_FEAT_SLEEPER_LOAD_AVG)
563 load = rq_of(cfs_rq)->cpu_load[2];
564
565 delta_fair = se->delta_fair_sleep;
566
567 /*
568 * Fix up delta_fair with the effect of us running
569 * during the whole sleep period:
570 */
571 if (sysctl_sched_features & SCHED_FEAT_SLEEPER_AVG)
572 delta_fair = div64_likely32((u64)delta_fair * load,
573 load + se->load.weight);
574
575 if (unlikely(se->load.weight != NICE_0_LOAD))
576 delta_fair = calc_weighted(delta_fair, se->load.weight,
577 NICE_0_SHIFT);
578
579 prev_runtime = se->wait_runtime;
580 __add_wait_runtime(cfs_rq, se, delta_fair);
581 delta_fair = se->wait_runtime - prev_runtime;
582
583 /*
584 * Track the amount of bonus we've given to sleepers:
585 */
586 cfs_rq->sleeper_bonus += delta_fair;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200587}
588
Ingo Molnar2396af62007-08-09 11:16:48 +0200589static void enqueue_sleeper(struct cfs_rq *cfs_rq, struct sched_entity *se)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200590{
591 struct task_struct *tsk = task_of(se);
592 unsigned long delta_fair;
593
594 if ((entity_is_task(se) && tsk->policy == SCHED_BATCH) ||
595 !(sysctl_sched_features & SCHED_FEAT_FAIR_SLEEPERS))
596 return;
597
598 delta_fair = (unsigned long)min((u64)(2*sysctl_sched_runtime_limit),
599 (u64)(cfs_rq->fair_clock - se->sleep_start_fair));
600
601 se->delta_fair_sleep += delta_fair;
602 if (unlikely(abs(se->delta_fair_sleep) >=
603 sysctl_sched_stat_granularity)) {
Ingo Molnardfdc1192007-08-09 11:16:48 +0200604 __enqueue_sleeper(cfs_rq, se);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200605 se->delta_fair_sleep = 0;
606 }
607
608 se->sleep_start_fair = 0;
609
610#ifdef CONFIG_SCHEDSTATS
611 if (se->sleep_start) {
Ingo Molnard2819182007-08-09 11:16:47 +0200612 u64 delta = rq_of(cfs_rq)->clock - se->sleep_start;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200613
614 if ((s64)delta < 0)
615 delta = 0;
616
617 if (unlikely(delta > se->sleep_max))
618 se->sleep_max = delta;
619
620 se->sleep_start = 0;
621 se->sum_sleep_runtime += delta;
622 }
623 if (se->block_start) {
Ingo Molnard2819182007-08-09 11:16:47 +0200624 u64 delta = rq_of(cfs_rq)->clock - se->block_start;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200625
626 if ((s64)delta < 0)
627 delta = 0;
628
629 if (unlikely(delta > se->block_max))
630 se->block_max = delta;
631
632 se->block_start = 0;
633 se->sum_sleep_runtime += delta;
634 }
635#endif
636}
637
638static void
Ingo Molnar668031c2007-08-09 11:16:48 +0200639enqueue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se, int wakeup)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200640{
641 /*
642 * Update the fair clock.
643 */
Ingo Molnarb7cc0892007-08-09 11:16:47 +0200644 update_curr(cfs_rq);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200645
646 if (wakeup)
Ingo Molnar2396af62007-08-09 11:16:48 +0200647 enqueue_sleeper(cfs_rq, se);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200648
Ingo Molnard2417e52007-08-09 11:16:47 +0200649 update_stats_enqueue(cfs_rq, se);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200650 __enqueue_entity(cfs_rq, se);
651}
652
653static void
Ingo Molnar525c2712007-08-09 11:16:48 +0200654dequeue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se, int sleep)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200655{
Ingo Molnar19b6a2e2007-08-09 11:16:48 +0200656 update_stats_dequeue(cfs_rq, se);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200657 if (sleep) {
658 se->sleep_start_fair = cfs_rq->fair_clock;
659#ifdef CONFIG_SCHEDSTATS
660 if (entity_is_task(se)) {
661 struct task_struct *tsk = task_of(se);
662
663 if (tsk->state & TASK_INTERRUPTIBLE)
Ingo Molnard2819182007-08-09 11:16:47 +0200664 se->sleep_start = rq_of(cfs_rq)->clock;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200665 if (tsk->state & TASK_UNINTERRUPTIBLE)
Ingo Molnard2819182007-08-09 11:16:47 +0200666 se->block_start = rq_of(cfs_rq)->clock;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200667 }
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200668#endif
669 }
670 __dequeue_entity(cfs_rq, se);
671}
672
673/*
674 * Preempt the current task with a newly woken task if needed:
675 */
Peter Zijlstra7c92e542007-09-05 14:32:49 +0200676static void
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200677__check_preempt_curr_fair(struct cfs_rq *cfs_rq, struct sched_entity *se,
678 struct sched_entity *curr, unsigned long granularity)
679{
680 s64 __delta = curr->fair_key - se->fair_key;
681
682 /*
683 * Take scheduling granularity into account - do not
684 * preempt the current task unless the best task has
685 * a larger than sched_granularity fairness advantage:
686 */
Peter Zijlstra4a55b452007-09-05 14:32:49 +0200687 if (__delta > niced_granularity(curr, granularity))
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200688 resched_task(rq_of(cfs_rq)->curr);
689}
690
691static inline void
Ingo Molnar8494f412007-08-09 11:16:48 +0200692set_next_entity(struct cfs_rq *cfs_rq, struct sched_entity *se)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200693{
694 /*
695 * Any task has to be enqueued before it get to execute on
696 * a CPU. So account for the time it spent waiting on the
697 * runqueue. (note, here we rely on pick_next_task() having
698 * done a put_prev_task_fair() shortly before this, which
699 * updated rq->fair_clock - used by update_stats_wait_end())
700 */
Ingo Molnar9ef0a962007-08-09 11:16:47 +0200701 update_stats_wait_end(cfs_rq, se);
Ingo Molnar79303e92007-08-09 11:16:47 +0200702 update_stats_curr_start(cfs_rq, se);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200703 set_cfs_rq_curr(cfs_rq, se);
Peter Zijlstra4a55b452007-09-05 14:32:49 +0200704 se->prev_sum_exec_runtime = se->sum_exec_runtime;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200705}
706
Ingo Molnar9948f4b2007-08-09 11:16:48 +0200707static struct sched_entity *pick_next_entity(struct cfs_rq *cfs_rq)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200708{
709 struct sched_entity *se = __pick_next_entity(cfs_rq);
710
Ingo Molnar8494f412007-08-09 11:16:48 +0200711 set_next_entity(cfs_rq, se);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200712
713 return se;
714}
715
Ingo Molnarab6cde22007-08-09 11:16:48 +0200716static void put_prev_entity(struct cfs_rq *cfs_rq, struct sched_entity *prev)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200717{
718 /*
719 * If still on the runqueue then deactivate_task()
720 * was not called and update_curr() has to be done:
721 */
722 if (prev->on_rq)
Ingo Molnarb7cc0892007-08-09 11:16:47 +0200723 update_curr(cfs_rq);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200724
Ingo Molnarc7e9b5b2007-08-09 11:16:48 +0200725 update_stats_curr_end(cfs_rq, prev);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200726
727 if (prev->on_rq)
Ingo Molnar5870db52007-08-09 11:16:47 +0200728 update_stats_wait_start(cfs_rq, prev);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200729 set_cfs_rq_curr(cfs_rq, NULL);
730}
731
732static void entity_tick(struct cfs_rq *cfs_rq, struct sched_entity *curr)
733{
Ingo Molnarf6cf8912007-08-28 12:53:24 +0200734 unsigned long gran, ideal_runtime, delta_exec;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200735 struct sched_entity *next;
Ingo Molnarc1b3da32007-08-09 11:16:47 +0200736
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200737 /*
738 * Dequeue and enqueue the task to update its
739 * position within the tree:
740 */
Ingo Molnar525c2712007-08-09 11:16:48 +0200741 dequeue_entity(cfs_rq, curr, 0);
Ingo Molnar668031c2007-08-09 11:16:48 +0200742 enqueue_entity(cfs_rq, curr, 0);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200743
744 /*
745 * Reschedule if another task tops the current one.
746 */
747 next = __pick_next_entity(cfs_rq);
748 if (next == curr)
749 return;
750
Ingo Molnarf6cf8912007-08-28 12:53:24 +0200751 gran = sched_granularity(cfs_rq);
752 ideal_runtime = niced_granularity(curr,
753 max(sysctl_sched_latency / cfs_rq->nr_running,
754 (unsigned long)sysctl_sched_min_granularity));
755 /*
756 * If we executed more than what the latency constraint suggests,
757 * reduce the rescheduling granularity. This way the total latency
758 * of how much a task is not scheduled converges to
759 * sysctl_sched_latency:
760 */
761 delta_exec = curr->sum_exec_runtime - curr->prev_sum_exec_runtime;
762 if (delta_exec > ideal_runtime)
763 gran = 0;
764
Peter Zijlstra7c92e542007-09-05 14:32:49 +0200765 __check_preempt_curr_fair(cfs_rq, next, curr, gran);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200766}
767
768/**************************************************
769 * CFS operations on tasks:
770 */
771
772#ifdef CONFIG_FAIR_GROUP_SCHED
773
774/* Walk up scheduling entities hierarchy */
775#define for_each_sched_entity(se) \
776 for (; se; se = se->parent)
777
778static inline struct cfs_rq *task_cfs_rq(struct task_struct *p)
779{
780 return p->se.cfs_rq;
781}
782
783/* runqueue on which this entity is (to be) queued */
784static inline struct cfs_rq *cfs_rq_of(struct sched_entity *se)
785{
786 return se->cfs_rq;
787}
788
789/* runqueue "owned" by this group */
790static inline struct cfs_rq *group_cfs_rq(struct sched_entity *grp)
791{
792 return grp->my_q;
793}
794
795/* Given a group's cfs_rq on one cpu, return its corresponding cfs_rq on
796 * another cpu ('this_cpu')
797 */
798static inline struct cfs_rq *cpu_cfs_rq(struct cfs_rq *cfs_rq, int this_cpu)
799{
800 /* A later patch will take group into account */
801 return &cpu_rq(this_cpu)->cfs;
802}
803
804/* Iterate thr' all leaf cfs_rq's on a runqueue */
805#define for_each_leaf_cfs_rq(rq, cfs_rq) \
806 list_for_each_entry(cfs_rq, &rq->leaf_cfs_rq_list, leaf_cfs_rq_list)
807
808/* Do the two (enqueued) tasks belong to the same group ? */
809static inline int is_same_group(struct task_struct *curr, struct task_struct *p)
810{
811 if (curr->se.cfs_rq == p->se.cfs_rq)
812 return 1;
813
814 return 0;
815}
816
817#else /* CONFIG_FAIR_GROUP_SCHED */
818
819#define for_each_sched_entity(se) \
820 for (; se; se = NULL)
821
822static inline struct cfs_rq *task_cfs_rq(struct task_struct *p)
823{
824 return &task_rq(p)->cfs;
825}
826
827static inline struct cfs_rq *cfs_rq_of(struct sched_entity *se)
828{
829 struct task_struct *p = task_of(se);
830 struct rq *rq = task_rq(p);
831
832 return &rq->cfs;
833}
834
835/* runqueue "owned" by this group */
836static inline struct cfs_rq *group_cfs_rq(struct sched_entity *grp)
837{
838 return NULL;
839}
840
841static inline struct cfs_rq *cpu_cfs_rq(struct cfs_rq *cfs_rq, int this_cpu)
842{
843 return &cpu_rq(this_cpu)->cfs;
844}
845
846#define for_each_leaf_cfs_rq(rq, cfs_rq) \
847 for (cfs_rq = &rq->cfs; cfs_rq; cfs_rq = NULL)
848
849static inline int is_same_group(struct task_struct *curr, struct task_struct *p)
850{
851 return 1;
852}
853
854#endif /* CONFIG_FAIR_GROUP_SCHED */
855
856/*
857 * The enqueue_task method is called before nr_running is
858 * increased. Here we update the fair scheduling stats and
859 * then put the task into the rbtree:
860 */
Ingo Molnarfd390f62007-08-09 11:16:48 +0200861static void enqueue_task_fair(struct rq *rq, struct task_struct *p, int wakeup)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200862{
863 struct cfs_rq *cfs_rq;
864 struct sched_entity *se = &p->se;
865
866 for_each_sched_entity(se) {
867 if (se->on_rq)
868 break;
869 cfs_rq = cfs_rq_of(se);
Ingo Molnar668031c2007-08-09 11:16:48 +0200870 enqueue_entity(cfs_rq, se, wakeup);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200871 }
872}
873
874/*
875 * The dequeue_task method is called before nr_running is
876 * decreased. We remove the task from the rbtree and
877 * update the fair scheduling stats:
878 */
Ingo Molnarf02231e2007-08-09 11:16:48 +0200879static void dequeue_task_fair(struct rq *rq, struct task_struct *p, int sleep)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200880{
881 struct cfs_rq *cfs_rq;
882 struct sched_entity *se = &p->se;
883
884 for_each_sched_entity(se) {
885 cfs_rq = cfs_rq_of(se);
Ingo Molnar525c2712007-08-09 11:16:48 +0200886 dequeue_entity(cfs_rq, se, sleep);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200887 /* Don't dequeue parent if it has other entities besides us */
888 if (cfs_rq->load.weight)
889 break;
890 }
891}
892
893/*
894 * sched_yield() support is very simple - we dequeue and enqueue
895 */
896static void yield_task_fair(struct rq *rq, struct task_struct *p)
897{
898 struct cfs_rq *cfs_rq = task_cfs_rq(p);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200899
Ingo Molnarc1b3da32007-08-09 11:16:47 +0200900 __update_rq_clock(rq);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200901 /*
902 * Dequeue and enqueue the task to update its
903 * position within the tree:
904 */
Ingo Molnar525c2712007-08-09 11:16:48 +0200905 dequeue_entity(cfs_rq, &p->se, 0);
Ingo Molnar668031c2007-08-09 11:16:48 +0200906 enqueue_entity(cfs_rq, &p->se, 0);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200907}
908
909/*
910 * Preempt the current task with a newly woken task if needed:
911 */
912static void check_preempt_curr_fair(struct rq *rq, struct task_struct *p)
913{
914 struct task_struct *curr = rq->curr;
915 struct cfs_rq *cfs_rq = task_cfs_rq(curr);
916 unsigned long gran;
917
918 if (unlikely(rt_prio(p->prio))) {
Ingo Molnara8e504d2007-08-09 11:16:47 +0200919 update_rq_clock(rq);
Ingo Molnarb7cc0892007-08-09 11:16:47 +0200920 update_curr(cfs_rq);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200921 resched_task(curr);
922 return;
923 }
924
925 gran = sysctl_sched_wakeup_granularity;
926 /*
927 * Batch tasks prefer throughput over latency:
928 */
929 if (unlikely(p->policy == SCHED_BATCH))
930 gran = sysctl_sched_batch_wakeup_granularity;
931
932 if (is_same_group(curr, p))
933 __check_preempt_curr_fair(cfs_rq, &p->se, &curr->se, gran);
934}
935
Ingo Molnarfb8d4722007-08-09 11:16:48 +0200936static struct task_struct *pick_next_task_fair(struct rq *rq)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200937{
938 struct cfs_rq *cfs_rq = &rq->cfs;
939 struct sched_entity *se;
940
941 if (unlikely(!cfs_rq->nr_running))
942 return NULL;
943
944 do {
Ingo Molnar9948f4b2007-08-09 11:16:48 +0200945 se = pick_next_entity(cfs_rq);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200946 cfs_rq = group_cfs_rq(se);
947 } while (cfs_rq);
948
949 return task_of(se);
950}
951
952/*
953 * Account for a descheduled task:
954 */
Ingo Molnar31ee5292007-08-09 11:16:49 +0200955static void put_prev_task_fair(struct rq *rq, struct task_struct *prev)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200956{
957 struct sched_entity *se = &prev->se;
958 struct cfs_rq *cfs_rq;
959
960 for_each_sched_entity(se) {
961 cfs_rq = cfs_rq_of(se);
Ingo Molnarab6cde22007-08-09 11:16:48 +0200962 put_prev_entity(cfs_rq, se);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200963 }
964}
965
966/**************************************************
967 * Fair scheduling class load-balancing methods:
968 */
969
970/*
971 * Load-balancing iterator. Note: while the runqueue stays locked
972 * during the whole iteration, the current task might be
973 * dequeued so the iterator has to be dequeue-safe. Here we
974 * achieve that by always pre-iterating before returning
975 * the current task:
976 */
977static inline struct task_struct *
978__load_balance_iterator(struct cfs_rq *cfs_rq, struct rb_node *curr)
979{
980 struct task_struct *p;
981
982 if (!curr)
983 return NULL;
984
985 p = rb_entry(curr, struct task_struct, se.run_node);
986 cfs_rq->rb_load_balance_curr = rb_next(curr);
987
988 return p;
989}
990
991static struct task_struct *load_balance_start_fair(void *arg)
992{
993 struct cfs_rq *cfs_rq = arg;
994
995 return __load_balance_iterator(cfs_rq, first_fair(cfs_rq));
996}
997
998static struct task_struct *load_balance_next_fair(void *arg)
999{
1000 struct cfs_rq *cfs_rq = arg;
1001
1002 return __load_balance_iterator(cfs_rq, cfs_rq->rb_load_balance_curr);
1003}
1004
Peter Williamsa4ac01c2007-08-09 11:16:46 +02001005#ifdef CONFIG_FAIR_GROUP_SCHED
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001006static int cfs_rq_best_prio(struct cfs_rq *cfs_rq)
1007{
1008 struct sched_entity *curr;
1009 struct task_struct *p;
1010
1011 if (!cfs_rq->nr_running)
1012 return MAX_PRIO;
1013
1014 curr = __pick_next_entity(cfs_rq);
1015 p = task_of(curr);
1016
1017 return p->prio;
1018}
Peter Williamsa4ac01c2007-08-09 11:16:46 +02001019#endif
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001020
Peter Williams43010652007-08-09 11:16:46 +02001021static unsigned long
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001022load_balance_fair(struct rq *this_rq, int this_cpu, struct rq *busiest,
Peter Williamsa4ac01c2007-08-09 11:16:46 +02001023 unsigned long max_nr_move, unsigned long max_load_move,
1024 struct sched_domain *sd, enum cpu_idle_type idle,
1025 int *all_pinned, int *this_best_prio)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001026{
1027 struct cfs_rq *busy_cfs_rq;
1028 unsigned long load_moved, total_nr_moved = 0, nr_moved;
1029 long rem_load_move = max_load_move;
1030 struct rq_iterator cfs_rq_iterator;
1031
1032 cfs_rq_iterator.start = load_balance_start_fair;
1033 cfs_rq_iterator.next = load_balance_next_fair;
1034
1035 for_each_leaf_cfs_rq(busiest, busy_cfs_rq) {
Peter Williamsa4ac01c2007-08-09 11:16:46 +02001036#ifdef CONFIG_FAIR_GROUP_SCHED
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001037 struct cfs_rq *this_cfs_rq;
Ingo Molnare56f31a2007-08-10 23:05:11 +02001038 long imbalance;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001039 unsigned long maxload;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001040
1041 this_cfs_rq = cpu_cfs_rq(busy_cfs_rq, this_cpu);
1042
Ingo Molnare56f31a2007-08-10 23:05:11 +02001043 imbalance = busy_cfs_rq->load.weight - this_cfs_rq->load.weight;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001044 /* Don't pull if this_cfs_rq has more load than busy_cfs_rq */
1045 if (imbalance <= 0)
1046 continue;
1047
1048 /* Don't pull more than imbalance/2 */
1049 imbalance /= 2;
1050 maxload = min(rem_load_move, imbalance);
1051
Peter Williamsa4ac01c2007-08-09 11:16:46 +02001052 *this_best_prio = cfs_rq_best_prio(this_cfs_rq);
1053#else
Ingo Molnare56f31a2007-08-10 23:05:11 +02001054# define maxload rem_load_move
Peter Williamsa4ac01c2007-08-09 11:16:46 +02001055#endif
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001056 /* pass busy_cfs_rq argument into
1057 * load_balance_[start|next]_fair iterators
1058 */
1059 cfs_rq_iterator.arg = busy_cfs_rq;
1060 nr_moved = balance_tasks(this_rq, this_cpu, busiest,
1061 max_nr_move, maxload, sd, idle, all_pinned,
Peter Williamsa4ac01c2007-08-09 11:16:46 +02001062 &load_moved, this_best_prio, &cfs_rq_iterator);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001063
1064 total_nr_moved += nr_moved;
1065 max_nr_move -= nr_moved;
1066 rem_load_move -= load_moved;
1067
1068 if (max_nr_move <= 0 || rem_load_move <= 0)
1069 break;
1070 }
1071
Peter Williams43010652007-08-09 11:16:46 +02001072 return max_load_move - rem_load_move;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001073}
1074
1075/*
1076 * scheduler tick hitting a task of our scheduling class:
1077 */
1078static void task_tick_fair(struct rq *rq, struct task_struct *curr)
1079{
1080 struct cfs_rq *cfs_rq;
1081 struct sched_entity *se = &curr->se;
1082
1083 for_each_sched_entity(se) {
1084 cfs_rq = cfs_rq_of(se);
1085 entity_tick(cfs_rq, se);
1086 }
1087}
1088
1089/*
1090 * Share the fairness runtime between parent and child, thus the
1091 * total amount of pressure for CPU stays equal - new tasks
1092 * get a chance to run but frequent forkers are not allowed to
1093 * monopolize the CPU. Note: the parent runqueue is locked,
1094 * the child is not running yet.
1095 */
Ingo Molnaree0827d2007-08-09 11:16:49 +02001096static void task_new_fair(struct rq *rq, struct task_struct *p)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001097{
1098 struct cfs_rq *cfs_rq = task_cfs_rq(p);
Ting Yang7109c442007-08-28 12:53:24 +02001099 struct sched_entity *se = &p->se, *curr = cfs_rq_curr(cfs_rq);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001100
1101 sched_info_queued(p);
1102
Ting Yang7109c442007-08-28 12:53:24 +02001103 update_curr(cfs_rq);
Ingo Molnard2417e52007-08-09 11:16:47 +02001104 update_stats_enqueue(cfs_rq, se);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001105 /*
1106 * Child runs first: we let it run before the parent
1107 * until it reschedules once. We set up the key so that
1108 * it will preempt the parent:
1109 */
Ingo Molnar9f508f82007-08-28 12:53:24 +02001110 se->fair_key = curr->fair_key -
Ting Yang7109c442007-08-28 12:53:24 +02001111 niced_granularity(curr, sched_granularity(cfs_rq)) - 1;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001112 /*
1113 * The first wait is dominated by the child-runs-first logic,
1114 * so do not credit it with that waiting time yet:
1115 */
1116 if (sysctl_sched_features & SCHED_FEAT_SKIP_INITIAL)
Ingo Molnar9f508f82007-08-28 12:53:24 +02001117 se->wait_start_fair = 0;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001118
1119 /*
1120 * The statistical average of wait_runtime is about
1121 * -granularity/2, so initialize the task with that:
1122 */
Ingo Molnara206c072007-09-05 14:32:49 +02001123 if (sysctl_sched_features & SCHED_FEAT_START_DEBIT)
Ingo Molnar9f508f82007-08-28 12:53:24 +02001124 se->wait_runtime = -(sched_granularity(cfs_rq) / 2);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001125
1126 __enqueue_entity(cfs_rq, se);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001127}
1128
1129#ifdef CONFIG_FAIR_GROUP_SCHED
1130/* Account for a task changing its policy or group.
1131 *
1132 * This routine is mostly called to set cfs_rq->curr field when a task
1133 * migrates between groups/classes.
1134 */
1135static void set_curr_task_fair(struct rq *rq)
1136{
Bruce Ashfield7c6c16f2007-08-24 20:39:10 +02001137 struct sched_entity *se = &rq->curr->se;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001138
Ingo Molnarc3b64f12007-08-09 11:16:51 +02001139 for_each_sched_entity(se)
1140 set_next_entity(cfs_rq_of(se), se);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001141}
1142#else
1143static void set_curr_task_fair(struct rq *rq)
1144{
1145}
1146#endif
1147
1148/*
1149 * All the scheduling class methods:
1150 */
1151struct sched_class fair_sched_class __read_mostly = {
1152 .enqueue_task = enqueue_task_fair,
1153 .dequeue_task = dequeue_task_fair,
1154 .yield_task = yield_task_fair,
1155
1156 .check_preempt_curr = check_preempt_curr_fair,
1157
1158 .pick_next_task = pick_next_task_fair,
1159 .put_prev_task = put_prev_task_fair,
1160
1161 .load_balance = load_balance_fair,
1162
1163 .set_curr_task = set_curr_task_fair,
1164 .task_tick = task_tick_fair,
1165 .task_new = task_new_fair,
1166};
1167
1168#ifdef CONFIG_SCHED_DEBUG
Ingo Molnar5cef9ec2007-08-09 11:16:47 +02001169static void print_cfs_stats(struct seq_file *m, int cpu)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001170{
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001171 struct cfs_rq *cfs_rq;
1172
Ingo Molnarc3b64f12007-08-09 11:16:51 +02001173 for_each_leaf_cfs_rq(cpu_rq(cpu), cfs_rq)
Ingo Molnar5cef9ec2007-08-09 11:16:47 +02001174 print_cfs_rq(m, cpu, cfs_rq);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001175}
1176#endif