sched: call update_curr() in task_tick_fair()
[deliverable/linux.git] / kernel / sched_fair.c
CommitLineData
bf0f6f24
IM
1/*
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>
21805085
PZ
18 *
19 * Adaptive scheduling granularity, math enhancements by Peter Zijlstra
20 * Copyright (C) 2007 Red Hat, Inc., Peter Zijlstra <pzijlstr@redhat.com>
bf0f6f24
IM
21 */
22
23/*
21805085
PZ
24 * Targeted preemption latency for CPU-bound tasks:
25 * (default: 20ms, units: nanoseconds)
bf0f6f24 26 *
21805085
PZ
27 * 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)
bf0f6f24
IM
31 *
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.)
21805085 35 * Targeted preemption latency for CPU-bound tasks:
bf0f6f24 36 */
21805085
PZ
37unsigned int sysctl_sched_latency __read_mostly = 20000000ULL;
38
39/*
40 * Minimal preemption granularity for CPU-bound tasks:
41 * (default: 2 msec, units: nanoseconds)
42 */
172ac3db 43unsigned int sysctl_sched_min_granularity __read_mostly = 2000000ULL;
bf0f6f24
IM
44
45/*
46 * SCHED_BATCH wake-up granularity.
71fd3714 47 * (default: 25 msec, units: nanoseconds)
bf0f6f24
IM
48 *
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 */
71fd3714 53unsigned int sysctl_sched_batch_wakeup_granularity __read_mostly = 25000000UL;
bf0f6f24
IM
54
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 */
71fd3714 63unsigned int sysctl_sched_wakeup_granularity __read_mostly = 1000000UL;
bf0f6f24
IM
64
65unsigned int sysctl_sched_stat_granularity __read_mostly;
66
67/*
71fd3714 68 * Initialized in sched_init_granularity() [to 5 times the base granularity]:
bf0f6f24
IM
69 */
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 |
5d2b3d36 86 SCHED_FEAT_SLEEPER_AVG *0 |
bf0f6f24
IM
87 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;
197}
198
199static inline void
200__dequeue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se)
201{
202 if (cfs_rq->rb_leftmost == &se->run_node)
203 cfs_rq->rb_leftmost = rb_next(&se->run_node);
204 rb_erase(&se->run_node, &cfs_rq->tasks_timeline);
205 update_load_sub(&cfs_rq->load, se->load.weight);
206 cfs_rq->nr_running--;
207 se->on_rq = 0;
208}
209
210static inline struct rb_node *first_fair(struct cfs_rq *cfs_rq)
211{
212 return cfs_rq->rb_leftmost;
213}
214
215static struct sched_entity *__pick_next_entity(struct cfs_rq *cfs_rq)
216{
217 return rb_entry(first_fair(cfs_rq), struct sched_entity, run_node);
218}
219
220/**************************************************************
221 * Scheduling class statistics methods:
222 */
223
21805085
PZ
224/*
225 * Calculate the preemption granularity needed to schedule every
226 * runnable task once per sysctl_sched_latency amount of time.
227 * (down to a sensible low limit on granularity)
228 *
229 * For example, if there are 2 tasks running and latency is 10 msecs,
230 * we switch tasks every 5 msecs. If we have 3 tasks running, we have
231 * to switch tasks every 3.33 msecs to get a 10 msecs observed latency
232 * for each task. We do finer and finer scheduling up to until we
233 * reach the minimum granularity value.
234 *
235 * To achieve this we use the following dynamic-granularity rule:
236 *
237 * gran = lat/nr - lat/nr/nr
238 *
239 * This comes out of the following equations:
240 *
241 * kA1 + gran = kB1
242 * kB2 + gran = kA2
243 * kA2 = kA1
244 * kB2 = kB1 - d + d/nr
245 * lat = d * nr
246 *
247 * Where 'k' is key, 'A' is task A (waiting), 'B' is task B (running),
248 * '1' is start of time, '2' is end of time, 'd' is delay between
249 * 1 and 2 (during which task B was running), 'nr' is number of tasks
250 * running, 'lat' is the the period of each task. ('lat' is the
251 * sched_latency that we aim for.)
252 */
253static long
254sched_granularity(struct cfs_rq *cfs_rq)
255{
256 unsigned int gran = sysctl_sched_latency;
257 unsigned int nr = cfs_rq->nr_running;
258
259 if (nr > 1) {
260 gran = gran/nr - gran/nr/nr;
172ac3db 261 gran = max(gran, sysctl_sched_min_granularity);
21805085
PZ
262 }
263
264 return gran;
265}
266
bf0f6f24
IM
267/*
268 * We rescale the rescheduling granularity of tasks according to their
269 * nice level, but only linearly, not exponentially:
270 */
271static long
272niced_granularity(struct sched_entity *curr, unsigned long granularity)
273{
274 u64 tmp;
275
7cff8cf6
IM
276 if (likely(curr->load.weight == NICE_0_LOAD))
277 return granularity;
bf0f6f24 278 /*
7cff8cf6 279 * Positive nice levels get the same granularity as nice-0:
bf0f6f24 280 */
7cff8cf6
IM
281 if (likely(curr->load.weight < NICE_0_LOAD)) {
282 tmp = curr->load.weight * (u64)granularity;
283 return (long) (tmp >> NICE_0_SHIFT);
284 }
bf0f6f24 285 /*
7cff8cf6 286 * Negative nice level tasks get linearly finer
bf0f6f24
IM
287 * granularity:
288 */
7cff8cf6 289 tmp = curr->load.inv_weight * (u64)granularity;
bf0f6f24
IM
290
291 /*
292 * It will always fit into 'long':
293 */
7cff8cf6 294 return (long) (tmp >> WMULT_SHIFT);
bf0f6f24
IM
295}
296
297static inline void
298limit_wait_runtime(struct cfs_rq *cfs_rq, struct sched_entity *se)
299{
300 long limit = sysctl_sched_runtime_limit;
301
302 /*
303 * Niced tasks have the same history dynamic range as
304 * non-niced tasks:
305 */
306 if (unlikely(se->wait_runtime > limit)) {
307 se->wait_runtime = limit;
308 schedstat_inc(se, wait_runtime_overruns);
309 schedstat_inc(cfs_rq, wait_runtime_overruns);
310 }
311 if (unlikely(se->wait_runtime < -limit)) {
312 se->wait_runtime = -limit;
313 schedstat_inc(se, wait_runtime_underruns);
314 schedstat_inc(cfs_rq, wait_runtime_underruns);
315 }
316}
317
318static inline void
319__add_wait_runtime(struct cfs_rq *cfs_rq, struct sched_entity *se, long delta)
320{
321 se->wait_runtime += delta;
322 schedstat_add(se, sum_wait_runtime, delta);
323 limit_wait_runtime(cfs_rq, se);
324}
325
326static void
327add_wait_runtime(struct cfs_rq *cfs_rq, struct sched_entity *se, long delta)
328{
329 schedstat_add(cfs_rq, wait_runtime, -se->wait_runtime);
330 __add_wait_runtime(cfs_rq, se, delta);
331 schedstat_add(cfs_rq, wait_runtime, se->wait_runtime);
332}
333
334/*
335 * Update the current task's runtime statistics. Skip current tasks that
336 * are not in our scheduling class.
337 */
338static inline void
b7cc0896 339__update_curr(struct cfs_rq *cfs_rq, struct sched_entity *curr)
bf0f6f24 340{
c5dcfe72 341 unsigned long delta, delta_exec, delta_fair, delta_mine;
bf0f6f24
IM
342 struct load_weight *lw = &cfs_rq->load;
343 unsigned long load = lw->weight;
344
bf0f6f24 345 delta_exec = curr->delta_exec;
8179ca23 346 schedstat_set(curr->exec_max, max((u64)delta_exec, curr->exec_max));
bf0f6f24
IM
347
348 curr->sum_exec_runtime += delta_exec;
349 cfs_rq->exec_clock += delta_exec;
350
fd8bb43e
IM
351 if (unlikely(!load))
352 return;
353
bf0f6f24
IM
354 delta_fair = calc_delta_fair(delta_exec, lw);
355 delta_mine = calc_delta_mine(delta_exec, curr->load.weight, lw);
356
5f01d519 357 if (cfs_rq->sleeper_bonus > sysctl_sched_min_granularity) {
ea0aa3b2 358 delta = min((u64)delta_mine, cfs_rq->sleeper_bonus);
b2133c8b
IM
359 delta = min(delta, (unsigned long)(
360 (long)sysctl_sched_runtime_limit - curr->wait_runtime));
bf0f6f24
IM
361 cfs_rq->sleeper_bonus -= delta;
362 delta_mine -= delta;
363 }
364
365 cfs_rq->fair_clock += delta_fair;
366 /*
367 * We executed delta_exec amount of time on the CPU,
368 * but we were only entitled to delta_mine amount of
369 * time during that period (if nr_running == 1 then
370 * the two values are equal)
371 * [Note: delta_mine - delta_exec is negative]:
372 */
373 add_wait_runtime(cfs_rq, curr, delta_mine - delta_exec);
374}
375
b7cc0896 376static void update_curr(struct cfs_rq *cfs_rq)
bf0f6f24
IM
377{
378 struct sched_entity *curr = cfs_rq_curr(cfs_rq);
379 unsigned long delta_exec;
380
381 if (unlikely(!curr))
382 return;
383
384 /*
385 * Get the amount of time the current task was running
386 * since the last time we changed load (this cannot
387 * overflow on 32 bits):
388 */
d281918d 389 delta_exec = (unsigned long)(rq_of(cfs_rq)->clock - curr->exec_start);
bf0f6f24
IM
390
391 curr->delta_exec += delta_exec;
392
393 if (unlikely(curr->delta_exec > sysctl_sched_stat_granularity)) {
b7cc0896 394 __update_curr(cfs_rq, curr);
bf0f6f24
IM
395 curr->delta_exec = 0;
396 }
d281918d 397 curr->exec_start = rq_of(cfs_rq)->clock;
bf0f6f24
IM
398}
399
400static inline void
5870db5b 401update_stats_wait_start(struct cfs_rq *cfs_rq, struct sched_entity *se)
bf0f6f24
IM
402{
403 se->wait_start_fair = cfs_rq->fair_clock;
d281918d 404 schedstat_set(se->wait_start, rq_of(cfs_rq)->clock);
bf0f6f24
IM
405}
406
407/*
408 * We calculate fair deltas here, so protect against the random effects
409 * of a multiplication overflow by capping it to the runtime limit:
410 */
411#if BITS_PER_LONG == 32
412static inline unsigned long
413calc_weighted(unsigned long delta, unsigned long weight, int shift)
414{
415 u64 tmp = (u64)delta * weight >> shift;
416
417 if (unlikely(tmp > sysctl_sched_runtime_limit*2))
418 return sysctl_sched_runtime_limit*2;
419 return tmp;
420}
421#else
422static inline unsigned long
423calc_weighted(unsigned long delta, unsigned long weight, int shift)
424{
425 return delta * weight >> shift;
426}
427#endif
428
429/*
430 * Task is being enqueued - update stats:
431 */
d2417e5a 432static void update_stats_enqueue(struct cfs_rq *cfs_rq, struct sched_entity *se)
bf0f6f24
IM
433{
434 s64 key;
435
436 /*
437 * Are we enqueueing a waiting task? (for current tasks
438 * a dequeue/enqueue event is a NOP)
439 */
440 if (se != cfs_rq_curr(cfs_rq))
5870db5b 441 update_stats_wait_start(cfs_rq, se);
bf0f6f24
IM
442 /*
443 * Update the key:
444 */
445 key = cfs_rq->fair_clock;
446
447 /*
448 * Optimize the common nice 0 case:
449 */
450 if (likely(se->load.weight == NICE_0_LOAD)) {
451 key -= se->wait_runtime;
452 } else {
453 u64 tmp;
454
455 if (se->wait_runtime < 0) {
456 tmp = -se->wait_runtime;
457 key += (tmp * se->load.inv_weight) >>
458 (WMULT_SHIFT - NICE_0_SHIFT);
459 } else {
460 tmp = se->wait_runtime;
a69edb55
IM
461 key -= (tmp * se->load.inv_weight) >>
462 (WMULT_SHIFT - NICE_0_SHIFT);
bf0f6f24
IM
463 }
464 }
465
466 se->fair_key = key;
467}
468
469/*
470 * Note: must be called with a freshly updated rq->fair_clock.
471 */
472static inline void
eac55ea3 473__update_stats_wait_end(struct cfs_rq *cfs_rq, struct sched_entity *se)
bf0f6f24
IM
474{
475 unsigned long delta_fair = se->delta_fair_run;
476
d281918d
IM
477 schedstat_set(se->wait_max, max(se->wait_max,
478 rq_of(cfs_rq)->clock - se->wait_start));
bf0f6f24
IM
479
480 if (unlikely(se->load.weight != NICE_0_LOAD))
481 delta_fair = calc_weighted(delta_fair, se->load.weight,
482 NICE_0_SHIFT);
483
484 add_wait_runtime(cfs_rq, se, delta_fair);
485}
486
487static void
9ef0a961 488update_stats_wait_end(struct cfs_rq *cfs_rq, struct sched_entity *se)
bf0f6f24
IM
489{
490 unsigned long delta_fair;
491
492 delta_fair = (unsigned long)min((u64)(2*sysctl_sched_runtime_limit),
493 (u64)(cfs_rq->fair_clock - se->wait_start_fair));
494
495 se->delta_fair_run += delta_fair;
496 if (unlikely(abs(se->delta_fair_run) >=
497 sysctl_sched_stat_granularity)) {
eac55ea3 498 __update_stats_wait_end(cfs_rq, se);
bf0f6f24
IM
499 se->delta_fair_run = 0;
500 }
501
502 se->wait_start_fair = 0;
6cfb0d5d 503 schedstat_set(se->wait_start, 0);
bf0f6f24
IM
504}
505
506static inline void
19b6a2e3 507update_stats_dequeue(struct cfs_rq *cfs_rq, struct sched_entity *se)
bf0f6f24 508{
b7cc0896 509 update_curr(cfs_rq);
bf0f6f24
IM
510 /*
511 * Mark the end of the wait period if dequeueing a
512 * waiting task:
513 */
514 if (se != cfs_rq_curr(cfs_rq))
9ef0a961 515 update_stats_wait_end(cfs_rq, se);
bf0f6f24
IM
516}
517
518/*
519 * We are picking a new current task - update its stats:
520 */
521static inline void
79303e9e 522update_stats_curr_start(struct cfs_rq *cfs_rq, struct sched_entity *se)
bf0f6f24
IM
523{
524 /*
525 * We are starting a new run period:
526 */
d281918d 527 se->exec_start = rq_of(cfs_rq)->clock;
bf0f6f24
IM
528}
529
530/*
531 * We are descheduling a task - update its stats:
532 */
533static inline void
c7e9b5b2 534update_stats_curr_end(struct cfs_rq *cfs_rq, struct sched_entity *se)
bf0f6f24
IM
535{
536 se->exec_start = 0;
537}
538
539/**************************************************
540 * Scheduling class queueing methods:
541 */
542
dfdc119e 543static void __enqueue_sleeper(struct cfs_rq *cfs_rq, struct sched_entity *se)
bf0f6f24
IM
544{
545 unsigned long load = cfs_rq->load.weight, delta_fair;
546 long prev_runtime;
547
b2133c8b
IM
548 /*
549 * Do not boost sleepers if there's too much bonus 'in flight'
550 * already:
551 */
552 if (unlikely(cfs_rq->sleeper_bonus > sysctl_sched_runtime_limit))
553 return;
554
bf0f6f24
IM
555 if (sysctl_sched_features & SCHED_FEAT_SLEEPER_LOAD_AVG)
556 load = rq_of(cfs_rq)->cpu_load[2];
557
558 delta_fair = se->delta_fair_sleep;
559
560 /*
561 * Fix up delta_fair with the effect of us running
562 * during the whole sleep period:
563 */
564 if (sysctl_sched_features & SCHED_FEAT_SLEEPER_AVG)
565 delta_fair = div64_likely32((u64)delta_fair * load,
566 load + se->load.weight);
567
568 if (unlikely(se->load.weight != NICE_0_LOAD))
569 delta_fair = calc_weighted(delta_fair, se->load.weight,
570 NICE_0_SHIFT);
571
572 prev_runtime = se->wait_runtime;
573 __add_wait_runtime(cfs_rq, se, delta_fair);
b2133c8b 574 schedstat_add(cfs_rq, wait_runtime, se->wait_runtime);
bf0f6f24
IM
575 delta_fair = se->wait_runtime - prev_runtime;
576
577 /*
578 * Track the amount of bonus we've given to sleepers:
579 */
580 cfs_rq->sleeper_bonus += delta_fair;
bf0f6f24
IM
581}
582
2396af69 583static void enqueue_sleeper(struct cfs_rq *cfs_rq, struct sched_entity *se)
bf0f6f24
IM
584{
585 struct task_struct *tsk = task_of(se);
586 unsigned long delta_fair;
587
588 if ((entity_is_task(se) && tsk->policy == SCHED_BATCH) ||
589 !(sysctl_sched_features & SCHED_FEAT_FAIR_SLEEPERS))
590 return;
591
592 delta_fair = (unsigned long)min((u64)(2*sysctl_sched_runtime_limit),
593 (u64)(cfs_rq->fair_clock - se->sleep_start_fair));
594
595 se->delta_fair_sleep += delta_fair;
596 if (unlikely(abs(se->delta_fair_sleep) >=
597 sysctl_sched_stat_granularity)) {
dfdc119e 598 __enqueue_sleeper(cfs_rq, se);
bf0f6f24
IM
599 se->delta_fair_sleep = 0;
600 }
601
602 se->sleep_start_fair = 0;
603
604#ifdef CONFIG_SCHEDSTATS
605 if (se->sleep_start) {
d281918d 606 u64 delta = rq_of(cfs_rq)->clock - se->sleep_start;
bf0f6f24
IM
607
608 if ((s64)delta < 0)
609 delta = 0;
610
611 if (unlikely(delta > se->sleep_max))
612 se->sleep_max = delta;
613
614 se->sleep_start = 0;
615 se->sum_sleep_runtime += delta;
616 }
617 if (se->block_start) {
d281918d 618 u64 delta = rq_of(cfs_rq)->clock - se->block_start;
bf0f6f24
IM
619
620 if ((s64)delta < 0)
621 delta = 0;
622
623 if (unlikely(delta > se->block_max))
624 se->block_max = delta;
625
626 se->block_start = 0;
627 se->sum_sleep_runtime += delta;
628 }
629#endif
630}
631
632static void
668031ca 633enqueue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se, int wakeup)
bf0f6f24
IM
634{
635 /*
636 * Update the fair clock.
637 */
b7cc0896 638 update_curr(cfs_rq);
bf0f6f24
IM
639
640 if (wakeup)
2396af69 641 enqueue_sleeper(cfs_rq, se);
bf0f6f24 642
d2417e5a 643 update_stats_enqueue(cfs_rq, se);
bf0f6f24
IM
644 __enqueue_entity(cfs_rq, se);
645}
646
647static void
525c2716 648dequeue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se, int sleep)
bf0f6f24 649{
19b6a2e3 650 update_stats_dequeue(cfs_rq, se);
bf0f6f24
IM
651 if (sleep) {
652 se->sleep_start_fair = cfs_rq->fair_clock;
653#ifdef CONFIG_SCHEDSTATS
654 if (entity_is_task(se)) {
655 struct task_struct *tsk = task_of(se);
656
657 if (tsk->state & TASK_INTERRUPTIBLE)
d281918d 658 se->sleep_start = rq_of(cfs_rq)->clock;
bf0f6f24 659 if (tsk->state & TASK_UNINTERRUPTIBLE)
d281918d 660 se->block_start = rq_of(cfs_rq)->clock;
bf0f6f24
IM
661 }
662 cfs_rq->wait_runtime -= se->wait_runtime;
663#endif
664 }
665 __dequeue_entity(cfs_rq, se);
666}
667
668/*
669 * Preempt the current task with a newly woken task if needed:
670 */
f6cf891c 671static int
bf0f6f24
IM
672__check_preempt_curr_fair(struct cfs_rq *cfs_rq, struct sched_entity *se,
673 struct sched_entity *curr, unsigned long granularity)
674{
675 s64 __delta = curr->fair_key - se->fair_key;
676
677 /*
678 * Take scheduling granularity into account - do not
679 * preempt the current task unless the best task has
680 * a larger than sched_granularity fairness advantage:
681 */
f6cf891c 682 if (__delta > niced_granularity(curr, granularity)) {
bf0f6f24 683 resched_task(rq_of(cfs_rq)->curr);
f6cf891c
IM
684 return 1;
685 }
686 return 0;
bf0f6f24
IM
687}
688
689static inline void
8494f412 690set_next_entity(struct cfs_rq *cfs_rq, struct sched_entity *se)
bf0f6f24
IM
691{
692 /*
693 * Any task has to be enqueued before it get to execute on
694 * a CPU. So account for the time it spent waiting on the
695 * runqueue. (note, here we rely on pick_next_task() having
696 * done a put_prev_task_fair() shortly before this, which
697 * updated rq->fair_clock - used by update_stats_wait_end())
698 */
9ef0a961 699 update_stats_wait_end(cfs_rq, se);
79303e9e 700 update_stats_curr_start(cfs_rq, se);
bf0f6f24
IM
701 set_cfs_rq_curr(cfs_rq, se);
702}
703
9948f4b2 704static struct sched_entity *pick_next_entity(struct cfs_rq *cfs_rq)
bf0f6f24
IM
705{
706 struct sched_entity *se = __pick_next_entity(cfs_rq);
707
8494f412 708 set_next_entity(cfs_rq, se);
bf0f6f24
IM
709
710 return se;
711}
712
ab6cde26 713static void put_prev_entity(struct cfs_rq *cfs_rq, struct sched_entity *prev)
bf0f6f24
IM
714{
715 /*
716 * If still on the runqueue then deactivate_task()
717 * was not called and update_curr() has to be done:
718 */
719 if (prev->on_rq)
b7cc0896 720 update_curr(cfs_rq);
bf0f6f24 721
c7e9b5b2 722 update_stats_curr_end(cfs_rq, prev);
bf0f6f24
IM
723
724 if (prev->on_rq)
5870db5b 725 update_stats_wait_start(cfs_rq, prev);
bf0f6f24
IM
726 set_cfs_rq_curr(cfs_rq, NULL);
727}
728
729static void entity_tick(struct cfs_rq *cfs_rq, struct sched_entity *curr)
730{
f6cf891c 731 unsigned long gran, ideal_runtime, delta_exec;
bf0f6f24 732 struct sched_entity *next;
c1b3da3e 733
bf0f6f24
IM
734 /*
735 * Dequeue and enqueue the task to update its
736 * position within the tree:
737 */
525c2716 738 dequeue_entity(cfs_rq, curr, 0);
668031ca 739 enqueue_entity(cfs_rq, curr, 0);
bf0f6f24
IM
740
741 /*
742 * Reschedule if another task tops the current one.
743 */
744 next = __pick_next_entity(cfs_rq);
745 if (next == curr)
746 return;
747
f6cf891c
IM
748 gran = sched_granularity(cfs_rq);
749 ideal_runtime = niced_granularity(curr,
750 max(sysctl_sched_latency / cfs_rq->nr_running,
751 (unsigned long)sysctl_sched_min_granularity));
752 /*
753 * If we executed more than what the latency constraint suggests,
754 * reduce the rescheduling granularity. This way the total latency
755 * of how much a task is not scheduled converges to
756 * sysctl_sched_latency:
757 */
758 delta_exec = curr->sum_exec_runtime - curr->prev_sum_exec_runtime;
759 if (delta_exec > ideal_runtime)
760 gran = 0;
761
762 if (__check_preempt_curr_fair(cfs_rq, next, curr, gran))
763 curr->prev_sum_exec_runtime = curr->sum_exec_runtime;
bf0f6f24
IM
764}
765
766/**************************************************
767 * CFS operations on tasks:
768 */
769
770#ifdef CONFIG_FAIR_GROUP_SCHED
771
772/* Walk up scheduling entities hierarchy */
773#define for_each_sched_entity(se) \
774 for (; se; se = se->parent)
775
776static inline struct cfs_rq *task_cfs_rq(struct task_struct *p)
777{
778 return p->se.cfs_rq;
779}
780
781/* runqueue on which this entity is (to be) queued */
782static inline struct cfs_rq *cfs_rq_of(struct sched_entity *se)
783{
784 return se->cfs_rq;
785}
786
787/* runqueue "owned" by this group */
788static inline struct cfs_rq *group_cfs_rq(struct sched_entity *grp)
789{
790 return grp->my_q;
791}
792
793/* Given a group's cfs_rq on one cpu, return its corresponding cfs_rq on
794 * another cpu ('this_cpu')
795 */
796static inline struct cfs_rq *cpu_cfs_rq(struct cfs_rq *cfs_rq, int this_cpu)
797{
798 /* A later patch will take group into account */
799 return &cpu_rq(this_cpu)->cfs;
800}
801
802/* Iterate thr' all leaf cfs_rq's on a runqueue */
803#define for_each_leaf_cfs_rq(rq, cfs_rq) \
804 list_for_each_entry(cfs_rq, &rq->leaf_cfs_rq_list, leaf_cfs_rq_list)
805
806/* Do the two (enqueued) tasks belong to the same group ? */
807static inline int is_same_group(struct task_struct *curr, struct task_struct *p)
808{
809 if (curr->se.cfs_rq == p->se.cfs_rq)
810 return 1;
811
812 return 0;
813}
814
815#else /* CONFIG_FAIR_GROUP_SCHED */
816
817#define for_each_sched_entity(se) \
818 for (; se; se = NULL)
819
820static inline struct cfs_rq *task_cfs_rq(struct task_struct *p)
821{
822 return &task_rq(p)->cfs;
823}
824
825static inline struct cfs_rq *cfs_rq_of(struct sched_entity *se)
826{
827 struct task_struct *p = task_of(se);
828 struct rq *rq = task_rq(p);
829
830 return &rq->cfs;
831}
832
833/* runqueue "owned" by this group */
834static inline struct cfs_rq *group_cfs_rq(struct sched_entity *grp)
835{
836 return NULL;
837}
838
839static inline struct cfs_rq *cpu_cfs_rq(struct cfs_rq *cfs_rq, int this_cpu)
840{
841 return &cpu_rq(this_cpu)->cfs;
842}
843
844#define for_each_leaf_cfs_rq(rq, cfs_rq) \
845 for (cfs_rq = &rq->cfs; cfs_rq; cfs_rq = NULL)
846
847static inline int is_same_group(struct task_struct *curr, struct task_struct *p)
848{
849 return 1;
850}
851
852#endif /* CONFIG_FAIR_GROUP_SCHED */
853
854/*
855 * The enqueue_task method is called before nr_running is
856 * increased. Here we update the fair scheduling stats and
857 * then put the task into the rbtree:
858 */
fd390f6a 859static void enqueue_task_fair(struct rq *rq, struct task_struct *p, int wakeup)
bf0f6f24
IM
860{
861 struct cfs_rq *cfs_rq;
862 struct sched_entity *se = &p->se;
863
864 for_each_sched_entity(se) {
865 if (se->on_rq)
866 break;
867 cfs_rq = cfs_rq_of(se);
668031ca 868 enqueue_entity(cfs_rq, se, wakeup);
bf0f6f24
IM
869 }
870}
871
872/*
873 * The dequeue_task method is called before nr_running is
874 * decreased. We remove the task from the rbtree and
875 * update the fair scheduling stats:
876 */
f02231e5 877static void dequeue_task_fair(struct rq *rq, struct task_struct *p, int sleep)
bf0f6f24
IM
878{
879 struct cfs_rq *cfs_rq;
880 struct sched_entity *se = &p->se;
881
882 for_each_sched_entity(se) {
883 cfs_rq = cfs_rq_of(se);
525c2716 884 dequeue_entity(cfs_rq, se, sleep);
bf0f6f24
IM
885 /* Don't dequeue parent if it has other entities besides us */
886 if (cfs_rq->load.weight)
887 break;
888 }
889}
890
891/*
892 * sched_yield() support is very simple - we dequeue and enqueue
893 */
894static void yield_task_fair(struct rq *rq, struct task_struct *p)
895{
896 struct cfs_rq *cfs_rq = task_cfs_rq(p);
bf0f6f24 897
c1b3da3e 898 __update_rq_clock(rq);
bf0f6f24
IM
899 /*
900 * Dequeue and enqueue the task to update its
901 * position within the tree:
902 */
525c2716 903 dequeue_entity(cfs_rq, &p->se, 0);
668031ca 904 enqueue_entity(cfs_rq, &p->se, 0);
bf0f6f24
IM
905}
906
907/*
908 * Preempt the current task with a newly woken task if needed:
909 */
910static void check_preempt_curr_fair(struct rq *rq, struct task_struct *p)
911{
912 struct task_struct *curr = rq->curr;
913 struct cfs_rq *cfs_rq = task_cfs_rq(curr);
914 unsigned long gran;
915
916 if (unlikely(rt_prio(p->prio))) {
a8e504d2 917 update_rq_clock(rq);
b7cc0896 918 update_curr(cfs_rq);
bf0f6f24
IM
919 resched_task(curr);
920 return;
921 }
922
923 gran = sysctl_sched_wakeup_granularity;
924 /*
925 * Batch tasks prefer throughput over latency:
926 */
927 if (unlikely(p->policy == SCHED_BATCH))
928 gran = sysctl_sched_batch_wakeup_granularity;
929
930 if (is_same_group(curr, p))
931 __check_preempt_curr_fair(cfs_rq, &p->se, &curr->se, gran);
932}
933
fb8d4724 934static struct task_struct *pick_next_task_fair(struct rq *rq)
bf0f6f24
IM
935{
936 struct cfs_rq *cfs_rq = &rq->cfs;
937 struct sched_entity *se;
938
939 if (unlikely(!cfs_rq->nr_running))
940 return NULL;
941
942 do {
9948f4b2 943 se = pick_next_entity(cfs_rq);
bf0f6f24
IM
944 cfs_rq = group_cfs_rq(se);
945 } while (cfs_rq);
946
947 return task_of(se);
948}
949
950/*
951 * Account for a descheduled task:
952 */
31ee529c 953static void put_prev_task_fair(struct rq *rq, struct task_struct *prev)
bf0f6f24
IM
954{
955 struct sched_entity *se = &prev->se;
956 struct cfs_rq *cfs_rq;
957
958 for_each_sched_entity(se) {
959 cfs_rq = cfs_rq_of(se);
ab6cde26 960 put_prev_entity(cfs_rq, se);
bf0f6f24
IM
961 }
962}
963
964/**************************************************
965 * Fair scheduling class load-balancing methods:
966 */
967
968/*
969 * Load-balancing iterator. Note: while the runqueue stays locked
970 * during the whole iteration, the current task might be
971 * dequeued so the iterator has to be dequeue-safe. Here we
972 * achieve that by always pre-iterating before returning
973 * the current task:
974 */
975static inline struct task_struct *
976__load_balance_iterator(struct cfs_rq *cfs_rq, struct rb_node *curr)
977{
978 struct task_struct *p;
979
980 if (!curr)
981 return NULL;
982
983 p = rb_entry(curr, struct task_struct, se.run_node);
984 cfs_rq->rb_load_balance_curr = rb_next(curr);
985
986 return p;
987}
988
989static struct task_struct *load_balance_start_fair(void *arg)
990{
991 struct cfs_rq *cfs_rq = arg;
992
993 return __load_balance_iterator(cfs_rq, first_fair(cfs_rq));
994}
995
996static struct task_struct *load_balance_next_fair(void *arg)
997{
998 struct cfs_rq *cfs_rq = arg;
999
1000 return __load_balance_iterator(cfs_rq, cfs_rq->rb_load_balance_curr);
1001}
1002
a4ac01c3 1003#ifdef CONFIG_FAIR_GROUP_SCHED
bf0f6f24
IM
1004static int cfs_rq_best_prio(struct cfs_rq *cfs_rq)
1005{
1006 struct sched_entity *curr;
1007 struct task_struct *p;
1008
1009 if (!cfs_rq->nr_running)
1010 return MAX_PRIO;
1011
1012 curr = __pick_next_entity(cfs_rq);
1013 p = task_of(curr);
1014
1015 return p->prio;
1016}
a4ac01c3 1017#endif
bf0f6f24 1018
43010659 1019static unsigned long
bf0f6f24 1020load_balance_fair(struct rq *this_rq, int this_cpu, struct rq *busiest,
a4ac01c3
PW
1021 unsigned long max_nr_move, unsigned long max_load_move,
1022 struct sched_domain *sd, enum cpu_idle_type idle,
1023 int *all_pinned, int *this_best_prio)
bf0f6f24
IM
1024{
1025 struct cfs_rq *busy_cfs_rq;
1026 unsigned long load_moved, total_nr_moved = 0, nr_moved;
1027 long rem_load_move = max_load_move;
1028 struct rq_iterator cfs_rq_iterator;
1029
1030 cfs_rq_iterator.start = load_balance_start_fair;
1031 cfs_rq_iterator.next = load_balance_next_fair;
1032
1033 for_each_leaf_cfs_rq(busiest, busy_cfs_rq) {
a4ac01c3 1034#ifdef CONFIG_FAIR_GROUP_SCHED
bf0f6f24 1035 struct cfs_rq *this_cfs_rq;
e56f31aa 1036 long imbalance;
bf0f6f24 1037 unsigned long maxload;
bf0f6f24
IM
1038
1039 this_cfs_rq = cpu_cfs_rq(busy_cfs_rq, this_cpu);
1040
e56f31aa 1041 imbalance = busy_cfs_rq->load.weight - this_cfs_rq->load.weight;
bf0f6f24
IM
1042 /* Don't pull if this_cfs_rq has more load than busy_cfs_rq */
1043 if (imbalance <= 0)
1044 continue;
1045
1046 /* Don't pull more than imbalance/2 */
1047 imbalance /= 2;
1048 maxload = min(rem_load_move, imbalance);
1049
a4ac01c3
PW
1050 *this_best_prio = cfs_rq_best_prio(this_cfs_rq);
1051#else
e56f31aa 1052# define maxload rem_load_move
a4ac01c3 1053#endif
bf0f6f24
IM
1054 /* pass busy_cfs_rq argument into
1055 * load_balance_[start|next]_fair iterators
1056 */
1057 cfs_rq_iterator.arg = busy_cfs_rq;
1058 nr_moved = balance_tasks(this_rq, this_cpu, busiest,
1059 max_nr_move, maxload, sd, idle, all_pinned,
a4ac01c3 1060 &load_moved, this_best_prio, &cfs_rq_iterator);
bf0f6f24
IM
1061
1062 total_nr_moved += nr_moved;
1063 max_nr_move -= nr_moved;
1064 rem_load_move -= load_moved;
1065
1066 if (max_nr_move <= 0 || rem_load_move <= 0)
1067 break;
1068 }
1069
43010659 1070 return max_load_move - rem_load_move;
bf0f6f24
IM
1071}
1072
1073/*
1074 * scheduler tick hitting a task of our scheduling class:
1075 */
1076static void task_tick_fair(struct rq *rq, struct task_struct *curr)
1077{
1078 struct cfs_rq *cfs_rq;
1079 struct sched_entity *se = &curr->se;
1080
1081 for_each_sched_entity(se) {
1082 cfs_rq = cfs_rq_of(se);
1083 entity_tick(cfs_rq, se);
1084 }
1085}
1086
1087/*
1088 * Share the fairness runtime between parent and child, thus the
1089 * total amount of pressure for CPU stays equal - new tasks
1090 * get a chance to run but frequent forkers are not allowed to
1091 * monopolize the CPU. Note: the parent runqueue is locked,
1092 * the child is not running yet.
1093 */
ee0827d8 1094static void task_new_fair(struct rq *rq, struct task_struct *p)
bf0f6f24
IM
1095{
1096 struct cfs_rq *cfs_rq = task_cfs_rq(p);
7109c442 1097 struct sched_entity *se = &p->se, *curr = cfs_rq_curr(cfs_rq);
bf0f6f24
IM
1098
1099 sched_info_queued(p);
1100
7109c442 1101 update_curr(cfs_rq);
d2417e5a 1102 update_stats_enqueue(cfs_rq, se);
bf0f6f24
IM
1103 /*
1104 * Child runs first: we let it run before the parent
1105 * until it reschedules once. We set up the key so that
1106 * it will preempt the parent:
1107 */
1108 p->se.fair_key = current->se.fair_key -
7109c442 1109 niced_granularity(curr, sched_granularity(cfs_rq)) - 1;
bf0f6f24
IM
1110 /*
1111 * The first wait is dominated by the child-runs-first logic,
1112 * so do not credit it with that waiting time yet:
1113 */
1114 if (sysctl_sched_features & SCHED_FEAT_SKIP_INITIAL)
1115 p->se.wait_start_fair = 0;
1116
1117 /*
1118 * The statistical average of wait_runtime is about
1119 * -granularity/2, so initialize the task with that:
1120 */
1121 if (sysctl_sched_features & SCHED_FEAT_START_DEBIT)
21805085 1122 p->se.wait_runtime = -(sched_granularity(cfs_rq) / 2);
bf0f6f24
IM
1123
1124 __enqueue_entity(cfs_rq, se);
bf0f6f24
IM
1125}
1126
1127#ifdef CONFIG_FAIR_GROUP_SCHED
1128/* Account for a task changing its policy or group.
1129 *
1130 * This routine is mostly called to set cfs_rq->curr field when a task
1131 * migrates between groups/classes.
1132 */
1133static void set_curr_task_fair(struct rq *rq)
1134{
7c6c16f3 1135 struct sched_entity *se = &rq->curr->se;
a8e504d2 1136
c3b64f1e
IM
1137 for_each_sched_entity(se)
1138 set_next_entity(cfs_rq_of(se), se);
bf0f6f24
IM
1139}
1140#else
1141static void set_curr_task_fair(struct rq *rq)
1142{
1143}
1144#endif
1145
1146/*
1147 * All the scheduling class methods:
1148 */
1149struct sched_class fair_sched_class __read_mostly = {
1150 .enqueue_task = enqueue_task_fair,
1151 .dequeue_task = dequeue_task_fair,
1152 .yield_task = yield_task_fair,
1153
1154 .check_preempt_curr = check_preempt_curr_fair,
1155
1156 .pick_next_task = pick_next_task_fair,
1157 .put_prev_task = put_prev_task_fair,
1158
1159 .load_balance = load_balance_fair,
1160
1161 .set_curr_task = set_curr_task_fair,
1162 .task_tick = task_tick_fair,
1163 .task_new = task_new_fair,
1164};
1165
1166#ifdef CONFIG_SCHED_DEBUG
5cef9eca 1167static void print_cfs_stats(struct seq_file *m, int cpu)
bf0f6f24 1168{
bf0f6f24
IM
1169 struct cfs_rq *cfs_rq;
1170
c3b64f1e 1171 for_each_leaf_cfs_rq(cpu_rq(cpu), cfs_rq)
5cef9eca 1172 print_cfs_rq(m, cpu, cfs_rq);
bf0f6f24
IM
1173}
1174#endif
This page took 0.102298 seconds and 5 git commands to generate.