2 * Completely Fair Scheduling (CFS) Class (SCHED_NORMAL/SCHED_BATCH)
4 * Copyright (C) 2007 Red Hat, Inc., Ingo Molnar <mingo@redhat.com>
6 * Interactivity improvements by Mike Galbraith
7 * (C) 2007 Mike Galbraith <efault@gmx.de>
9 * Various enhancements by Dmitry Adamushko.
10 * (C) 2007 Dmitry Adamushko <dmitry.adamushko@gmail.com>
12 * Group scheduling enhancements by Srivatsa Vaddagiri
13 * Copyright IBM Corporation, 2007
14 * Author: Srivatsa Vaddagiri <vatsa@linux.vnet.ibm.com>
16 * Scaled math optimizations by Thomas Gleixner
17 * Copyright (C) 2007, Thomas Gleixner <tglx@linutronix.de>
19 * Adaptive scheduling granularity, math enhancements by Peter Zijlstra
20 * Copyright (C) 2007 Red Hat, Inc., Peter Zijlstra <pzijlstr@redhat.com>
24 * Targeted preemption latency for CPU-bound tasks:
25 * (default: 20ms, units: nanoseconds)
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)
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.)
35 * Targeted preemption latency for CPU-bound tasks:
37 unsigned int sysctl_sched_latency __read_mostly
= 20000000ULL;
40 * Minimal preemption granularity for CPU-bound tasks:
41 * (default: 2 msec, units: nanoseconds)
43 unsigned int sysctl_sched_min_granularity __read_mostly
= 2000000ULL;
46 * SCHED_BATCH wake-up granularity.
47 * (default: 25 msec, units: nanoseconds)
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.
53 unsigned int sysctl_sched_batch_wakeup_granularity __read_mostly
= 25000000UL;
56 * SCHED_OTHER wake-up granularity.
57 * (default: 1 msec, units: nanoseconds)
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.
63 unsigned int sysctl_sched_wakeup_granularity __read_mostly
= 1000000UL;
65 unsigned int sysctl_sched_stat_granularity __read_mostly
;
68 * Initialized in sched_init_granularity() [to 5 times the base granularity]:
70 unsigned int sysctl_sched_runtime_limit __read_mostly
;
73 * Debugging: various feature bits
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,
84 unsigned int sysctl_sched_features __read_mostly
=
85 SCHED_FEAT_FAIR_SLEEPERS
*1 |
86 SCHED_FEAT_SLEEPER_AVG
*0 |
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;
92 extern struct sched_class fair_sched_class
;
94 /**************************************************************
95 * CFS operations on generic schedulable entities:
98 #ifdef CONFIG_FAIR_GROUP_SCHED
100 /* cpu runqueue to which this cfs_rq is attached */
101 static inline struct rq
*rq_of(struct cfs_rq
*cfs_rq
)
106 /* currently running entity (if any) on this cfs_rq */
107 static inline struct sched_entity
*cfs_rq_curr(struct cfs_rq
*cfs_rq
)
112 /* An entity is a task if it doesn't "own" a runqueue */
113 #define entity_is_task(se) (!se->my_q)
116 set_cfs_rq_curr(struct cfs_rq
*cfs_rq
, struct sched_entity
*se
)
121 #else /* CONFIG_FAIR_GROUP_SCHED */
123 static inline struct rq
*rq_of(struct cfs_rq
*cfs_rq
)
125 return container_of(cfs_rq
, struct rq
, cfs
);
128 static inline struct sched_entity
*cfs_rq_curr(struct cfs_rq
*cfs_rq
)
130 struct rq
*rq
= rq_of(cfs_rq
);
132 if (unlikely(rq
->curr
->sched_class
!= &fair_sched_class
))
135 return &rq
->curr
->se
;
138 #define entity_is_task(se) 1
141 set_cfs_rq_curr(struct cfs_rq
*cfs_rq
, struct sched_entity
*se
) { }
143 #endif /* CONFIG_FAIR_GROUP_SCHED */
145 static inline struct task_struct
*task_of(struct sched_entity
*se
)
147 return container_of(se
, struct task_struct
, se
);
151 /**************************************************************
152 * Scheduling class tree data structure manipulation methods:
156 * Enqueue an entity into the rb-tree:
159 __enqueue_entity(struct cfs_rq
*cfs_rq
, struct sched_entity
*se
)
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
;
168 * Find the right place in the rbtree:
172 entry
= rb_entry(parent
, struct sched_entity
, run_node
);
174 * We dont care about collisions. Nodes with
175 * the same key stay together.
177 if (key
- entry
->fair_key
< 0) {
178 link
= &parent
->rb_left
;
180 link
= &parent
->rb_right
;
186 * Maintain a cache of leftmost tree entries (it is frequently
190 cfs_rq
->rb_leftmost
= &se
->run_node
;
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
++;
200 __dequeue_entity(struct cfs_rq
*cfs_rq
, struct sched_entity
*se
)
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
--;
210 static inline struct rb_node
*first_fair(struct cfs_rq
*cfs_rq
)
212 return cfs_rq
->rb_leftmost
;
215 static struct sched_entity
*__pick_next_entity(struct cfs_rq
*cfs_rq
)
217 return rb_entry(first_fair(cfs_rq
), struct sched_entity
, run_node
);
220 /**************************************************************
221 * Scheduling class statistics methods:
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)
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.
235 * To achieve this we use the following dynamic-granularity rule:
237 * gran = lat/nr - lat/nr/nr
239 * This comes out of the following equations:
244 * kB2 = kB1 - d + d/nr
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.)
254 sched_granularity(struct cfs_rq
*cfs_rq
)
256 unsigned int gran
= sysctl_sched_latency
;
257 unsigned int nr
= cfs_rq
->nr_running
;
260 gran
= gran
/nr
- gran
/nr
/nr
;
261 gran
= max(gran
, sysctl_sched_min_granularity
);
268 * We rescale the rescheduling granularity of tasks according to their
269 * nice level, but only linearly, not exponentially:
272 niced_granularity(struct sched_entity
*curr
, unsigned long granularity
)
276 if (likely(curr
->load
.weight
== NICE_0_LOAD
))
279 * Positive nice levels get the same granularity as nice-0:
281 if (likely(curr
->load
.weight
< NICE_0_LOAD
)) {
282 tmp
= curr
->load
.weight
* (u64
)granularity
;
283 return (long) (tmp
>> NICE_0_SHIFT
);
286 * Negative nice level tasks get linearly finer
289 tmp
= curr
->load
.inv_weight
* (u64
)granularity
;
292 * It will always fit into 'long':
294 return (long) (tmp
>> WMULT_SHIFT
);
298 limit_wait_runtime(struct cfs_rq
*cfs_rq
, struct sched_entity
*se
)
300 long limit
= sysctl_sched_runtime_limit
;
303 * Niced tasks have the same history dynamic range as
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
);
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
);
319 __add_wait_runtime(struct cfs_rq
*cfs_rq
, struct sched_entity
*se
, long delta
)
321 se
->wait_runtime
+= delta
;
322 schedstat_add(se
, sum_wait_runtime
, delta
);
323 limit_wait_runtime(cfs_rq
, se
);
327 add_wait_runtime(struct cfs_rq
*cfs_rq
, struct sched_entity
*se
, long delta
)
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
);
335 * Update the current task's runtime statistics. Skip current tasks that
336 * are not in our scheduling class.
339 __update_curr(struct cfs_rq
*cfs_rq
, struct sched_entity
*curr
)
341 unsigned long delta
, delta_exec
, delta_fair
, delta_mine
;
342 struct load_weight
*lw
= &cfs_rq
->load
;
343 unsigned long load
= lw
->weight
;
345 delta_exec
= curr
->delta_exec
;
346 schedstat_set(curr
->exec_max
, max((u64
)delta_exec
, curr
->exec_max
));
348 curr
->sum_exec_runtime
+= delta_exec
;
349 cfs_rq
->exec_clock
+= delta_exec
;
354 delta_fair
= calc_delta_fair(delta_exec
, lw
);
355 delta_mine
= calc_delta_mine(delta_exec
, curr
->load
.weight
, lw
);
357 if (cfs_rq
->sleeper_bonus
> sysctl_sched_min_granularity
) {
358 delta
= min((u64
)delta_mine
, cfs_rq
->sleeper_bonus
);
359 delta
= min(delta
, (unsigned long)(
360 (long)sysctl_sched_runtime_limit
- curr
->wait_runtime
));
361 cfs_rq
->sleeper_bonus
-= delta
;
365 cfs_rq
->fair_clock
+= delta_fair
;
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]:
373 add_wait_runtime(cfs_rq
, curr
, delta_mine
- delta_exec
);
376 static void update_curr(struct cfs_rq
*cfs_rq
)
378 struct sched_entity
*curr
= cfs_rq_curr(cfs_rq
);
379 unsigned long delta_exec
;
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):
389 delta_exec
= (unsigned long)(rq_of(cfs_rq
)->clock
- curr
->exec_start
);
391 curr
->delta_exec
+= delta_exec
;
393 if (unlikely(curr
->delta_exec
> sysctl_sched_stat_granularity
)) {
394 __update_curr(cfs_rq
, curr
);
395 curr
->delta_exec
= 0;
397 curr
->exec_start
= rq_of(cfs_rq
)->clock
;
401 update_stats_wait_start(struct cfs_rq
*cfs_rq
, struct sched_entity
*se
)
403 se
->wait_start_fair
= cfs_rq
->fair_clock
;
404 schedstat_set(se
->wait_start
, rq_of(cfs_rq
)->clock
);
408 * We calculate fair deltas here, so protect against the random effects
409 * of a multiplication overflow by capping it to the runtime limit:
411 #if BITS_PER_LONG == 32
412 static inline unsigned long
413 calc_weighted(unsigned long delta
, unsigned long weight
, int shift
)
415 u64 tmp
= (u64
)delta
* weight
>> shift
;
417 if (unlikely(tmp
> sysctl_sched_runtime_limit
*2))
418 return sysctl_sched_runtime_limit
*2;
422 static inline unsigned long
423 calc_weighted(unsigned long delta
, unsigned long weight
, int shift
)
425 return delta
* weight
>> shift
;
430 * Task is being enqueued - update stats:
432 static void update_stats_enqueue(struct cfs_rq
*cfs_rq
, struct sched_entity
*se
)
437 * Are we enqueueing a waiting task? (for current tasks
438 * a dequeue/enqueue event is a NOP)
440 if (se
!= cfs_rq_curr(cfs_rq
))
441 update_stats_wait_start(cfs_rq
, se
);
445 key
= cfs_rq
->fair_clock
;
448 * Optimize the common nice 0 case:
450 if (likely(se
->load
.weight
== NICE_0_LOAD
)) {
451 key
-= se
->wait_runtime
;
455 if (se
->wait_runtime
< 0) {
456 tmp
= -se
->wait_runtime
;
457 key
+= (tmp
* se
->load
.inv_weight
) >>
458 (WMULT_SHIFT
- NICE_0_SHIFT
);
460 tmp
= se
->wait_runtime
;
461 key
-= (tmp
* se
->load
.inv_weight
) >>
462 (WMULT_SHIFT
- NICE_0_SHIFT
);
470 * Note: must be called with a freshly updated rq->fair_clock.
473 __update_stats_wait_end(struct cfs_rq
*cfs_rq
, struct sched_entity
*se
)
475 unsigned long delta_fair
= se
->delta_fair_run
;
477 schedstat_set(se
->wait_max
, max(se
->wait_max
,
478 rq_of(cfs_rq
)->clock
- se
->wait_start
));
480 if (unlikely(se
->load
.weight
!= NICE_0_LOAD
))
481 delta_fair
= calc_weighted(delta_fair
, se
->load
.weight
,
484 add_wait_runtime(cfs_rq
, se
, delta_fair
);
488 update_stats_wait_end(struct cfs_rq
*cfs_rq
, struct sched_entity
*se
)
490 unsigned long delta_fair
;
492 delta_fair
= (unsigned long)min((u64
)(2*sysctl_sched_runtime_limit
),
493 (u64
)(cfs_rq
->fair_clock
- se
->wait_start_fair
));
495 se
->delta_fair_run
+= delta_fair
;
496 if (unlikely(abs(se
->delta_fair_run
) >=
497 sysctl_sched_stat_granularity
)) {
498 __update_stats_wait_end(cfs_rq
, se
);
499 se
->delta_fair_run
= 0;
502 se
->wait_start_fair
= 0;
503 schedstat_set(se
->wait_start
, 0);
507 update_stats_dequeue(struct cfs_rq
*cfs_rq
, struct sched_entity
*se
)
511 * Mark the end of the wait period if dequeueing a
514 if (se
!= cfs_rq_curr(cfs_rq
))
515 update_stats_wait_end(cfs_rq
, se
);
519 * We are picking a new current task - update its stats:
522 update_stats_curr_start(struct cfs_rq
*cfs_rq
, struct sched_entity
*se
)
525 * We are starting a new run period:
527 se
->exec_start
= rq_of(cfs_rq
)->clock
;
531 * We are descheduling a task - update its stats:
534 update_stats_curr_end(struct cfs_rq
*cfs_rq
, struct sched_entity
*se
)
539 /**************************************************
540 * Scheduling class queueing methods:
543 static void __enqueue_sleeper(struct cfs_rq
*cfs_rq
, struct sched_entity
*se
)
545 unsigned long load
= cfs_rq
->load
.weight
, delta_fair
;
549 * Do not boost sleepers if there's too much bonus 'in flight'
552 if (unlikely(cfs_rq
->sleeper_bonus
> sysctl_sched_runtime_limit
))
555 if (sysctl_sched_features
& SCHED_FEAT_SLEEPER_LOAD_AVG
)
556 load
= rq_of(cfs_rq
)->cpu_load
[2];
558 delta_fair
= se
->delta_fair_sleep
;
561 * Fix up delta_fair with the effect of us running
562 * during the whole sleep period:
564 if (sysctl_sched_features
& SCHED_FEAT_SLEEPER_AVG
)
565 delta_fair
= div64_likely32((u64
)delta_fair
* load
,
566 load
+ se
->load
.weight
);
568 if (unlikely(se
->load
.weight
!= NICE_0_LOAD
))
569 delta_fair
= calc_weighted(delta_fair
, se
->load
.weight
,
572 prev_runtime
= se
->wait_runtime
;
573 __add_wait_runtime(cfs_rq
, se
, delta_fair
);
574 schedstat_add(cfs_rq
, wait_runtime
, se
->wait_runtime
);
575 delta_fair
= se
->wait_runtime
- prev_runtime
;
578 * Track the amount of bonus we've given to sleepers:
580 cfs_rq
->sleeper_bonus
+= delta_fair
;
583 static void enqueue_sleeper(struct cfs_rq
*cfs_rq
, struct sched_entity
*se
)
585 struct task_struct
*tsk
= task_of(se
);
586 unsigned long delta_fair
;
588 if ((entity_is_task(se
) && tsk
->policy
== SCHED_BATCH
) ||
589 !(sysctl_sched_features
& SCHED_FEAT_FAIR_SLEEPERS
))
592 delta_fair
= (unsigned long)min((u64
)(2*sysctl_sched_runtime_limit
),
593 (u64
)(cfs_rq
->fair_clock
- se
->sleep_start_fair
));
595 se
->delta_fair_sleep
+= delta_fair
;
596 if (unlikely(abs(se
->delta_fair_sleep
) >=
597 sysctl_sched_stat_granularity
)) {
598 __enqueue_sleeper(cfs_rq
, se
);
599 se
->delta_fair_sleep
= 0;
602 se
->sleep_start_fair
= 0;
604 #ifdef CONFIG_SCHEDSTATS
605 if (se
->sleep_start
) {
606 u64 delta
= rq_of(cfs_rq
)->clock
- se
->sleep_start
;
611 if (unlikely(delta
> se
->sleep_max
))
612 se
->sleep_max
= delta
;
615 se
->sum_sleep_runtime
+= delta
;
617 if (se
->block_start
) {
618 u64 delta
= rq_of(cfs_rq
)->clock
- se
->block_start
;
623 if (unlikely(delta
> se
->block_max
))
624 se
->block_max
= delta
;
627 se
->sum_sleep_runtime
+= delta
;
633 enqueue_entity(struct cfs_rq
*cfs_rq
, struct sched_entity
*se
, int wakeup
)
636 * Update the fair clock.
641 enqueue_sleeper(cfs_rq
, se
);
643 update_stats_enqueue(cfs_rq
, se
);
644 __enqueue_entity(cfs_rq
, se
);
648 dequeue_entity(struct cfs_rq
*cfs_rq
, struct sched_entity
*se
, int sleep
)
650 update_stats_dequeue(cfs_rq
, se
);
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
);
657 if (tsk
->state
& TASK_INTERRUPTIBLE
)
658 se
->sleep_start
= rq_of(cfs_rq
)->clock
;
659 if (tsk
->state
& TASK_UNINTERRUPTIBLE
)
660 se
->block_start
= rq_of(cfs_rq
)->clock
;
662 cfs_rq
->wait_runtime
-= se
->wait_runtime
;
665 __dequeue_entity(cfs_rq
, se
);
669 * Preempt the current task with a newly woken task if needed:
672 __check_preempt_curr_fair(struct cfs_rq
*cfs_rq
, struct sched_entity
*se
,
673 struct sched_entity
*curr
, unsigned long granularity
)
675 s64 __delta
= curr
->fair_key
- se
->fair_key
;
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:
682 if (__delta
> niced_granularity(curr
, granularity
))
683 resched_task(rq_of(cfs_rq
)->curr
);
687 set_next_entity(struct cfs_rq
*cfs_rq
, struct sched_entity
*se
)
690 * Any task has to be enqueued before it get to execute on
691 * a CPU. So account for the time it spent waiting on the
692 * runqueue. (note, here we rely on pick_next_task() having
693 * done a put_prev_task_fair() shortly before this, which
694 * updated rq->fair_clock - used by update_stats_wait_end())
696 update_stats_wait_end(cfs_rq
, se
);
697 update_stats_curr_start(cfs_rq
, se
);
698 set_cfs_rq_curr(cfs_rq
, se
);
701 static struct sched_entity
*pick_next_entity(struct cfs_rq
*cfs_rq
)
703 struct sched_entity
*se
= __pick_next_entity(cfs_rq
);
705 set_next_entity(cfs_rq
, se
);
710 static void put_prev_entity(struct cfs_rq
*cfs_rq
, struct sched_entity
*prev
)
713 * If still on the runqueue then deactivate_task()
714 * was not called and update_curr() has to be done:
719 update_stats_curr_end(cfs_rq
, prev
);
722 update_stats_wait_start(cfs_rq
, prev
);
723 set_cfs_rq_curr(cfs_rq
, NULL
);
726 static void entity_tick(struct cfs_rq
*cfs_rq
, struct sched_entity
*curr
)
728 struct sched_entity
*next
;
731 * Dequeue and enqueue the task to update its
732 * position within the tree:
734 dequeue_entity(cfs_rq
, curr
, 0);
735 enqueue_entity(cfs_rq
, curr
, 0);
738 * Reschedule if another task tops the current one.
740 next
= __pick_next_entity(cfs_rq
);
744 __check_preempt_curr_fair(cfs_rq
, next
, curr
,
745 sched_granularity(cfs_rq
));
748 /**************************************************
749 * CFS operations on tasks:
752 #ifdef CONFIG_FAIR_GROUP_SCHED
754 /* Walk up scheduling entities hierarchy */
755 #define for_each_sched_entity(se) \
756 for (; se; se = se->parent)
758 static inline struct cfs_rq
*task_cfs_rq(struct task_struct
*p
)
763 /* runqueue on which this entity is (to be) queued */
764 static inline struct cfs_rq
*cfs_rq_of(struct sched_entity
*se
)
769 /* runqueue "owned" by this group */
770 static inline struct cfs_rq
*group_cfs_rq(struct sched_entity
*grp
)
775 /* Given a group's cfs_rq on one cpu, return its corresponding cfs_rq on
776 * another cpu ('this_cpu')
778 static inline struct cfs_rq
*cpu_cfs_rq(struct cfs_rq
*cfs_rq
, int this_cpu
)
780 /* A later patch will take group into account */
781 return &cpu_rq(this_cpu
)->cfs
;
784 /* Iterate thr' all leaf cfs_rq's on a runqueue */
785 #define for_each_leaf_cfs_rq(rq, cfs_rq) \
786 list_for_each_entry(cfs_rq, &rq->leaf_cfs_rq_list, leaf_cfs_rq_list)
788 /* Do the two (enqueued) tasks belong to the same group ? */
789 static inline int is_same_group(struct task_struct
*curr
, struct task_struct
*p
)
791 if (curr
->se
.cfs_rq
== p
->se
.cfs_rq
)
797 #else /* CONFIG_FAIR_GROUP_SCHED */
799 #define for_each_sched_entity(se) \
800 for (; se; se = NULL)
802 static inline struct cfs_rq
*task_cfs_rq(struct task_struct
*p
)
804 return &task_rq(p
)->cfs
;
807 static inline struct cfs_rq
*cfs_rq_of(struct sched_entity
*se
)
809 struct task_struct
*p
= task_of(se
);
810 struct rq
*rq
= task_rq(p
);
815 /* runqueue "owned" by this group */
816 static inline struct cfs_rq
*group_cfs_rq(struct sched_entity
*grp
)
821 static inline struct cfs_rq
*cpu_cfs_rq(struct cfs_rq
*cfs_rq
, int this_cpu
)
823 return &cpu_rq(this_cpu
)->cfs
;
826 #define for_each_leaf_cfs_rq(rq, cfs_rq) \
827 for (cfs_rq = &rq->cfs; cfs_rq; cfs_rq = NULL)
829 static inline int is_same_group(struct task_struct
*curr
, struct task_struct
*p
)
834 #endif /* CONFIG_FAIR_GROUP_SCHED */
837 * The enqueue_task method is called before nr_running is
838 * increased. Here we update the fair scheduling stats and
839 * then put the task into the rbtree:
841 static void enqueue_task_fair(struct rq
*rq
, struct task_struct
*p
, int wakeup
)
843 struct cfs_rq
*cfs_rq
;
844 struct sched_entity
*se
= &p
->se
;
846 for_each_sched_entity(se
) {
849 cfs_rq
= cfs_rq_of(se
);
850 enqueue_entity(cfs_rq
, se
, wakeup
);
855 * The dequeue_task method is called before nr_running is
856 * decreased. We remove the task from the rbtree and
857 * update the fair scheduling stats:
859 static void dequeue_task_fair(struct rq
*rq
, struct task_struct
*p
, int sleep
)
861 struct cfs_rq
*cfs_rq
;
862 struct sched_entity
*se
= &p
->se
;
864 for_each_sched_entity(se
) {
865 cfs_rq
= cfs_rq_of(se
);
866 dequeue_entity(cfs_rq
, se
, sleep
);
867 /* Don't dequeue parent if it has other entities besides us */
868 if (cfs_rq
->load
.weight
)
874 * sched_yield() support is very simple - we dequeue and enqueue
876 static void yield_task_fair(struct rq
*rq
, struct task_struct
*p
)
878 struct cfs_rq
*cfs_rq
= task_cfs_rq(p
);
880 __update_rq_clock(rq
);
882 * Dequeue and enqueue the task to update its
883 * position within the tree:
885 dequeue_entity(cfs_rq
, &p
->se
, 0);
886 enqueue_entity(cfs_rq
, &p
->se
, 0);
890 * Preempt the current task with a newly woken task if needed:
892 static void check_preempt_curr_fair(struct rq
*rq
, struct task_struct
*p
)
894 struct task_struct
*curr
= rq
->curr
;
895 struct cfs_rq
*cfs_rq
= task_cfs_rq(curr
);
898 if (unlikely(rt_prio(p
->prio
))) {
905 gran
= sysctl_sched_wakeup_granularity
;
907 * Batch tasks prefer throughput over latency:
909 if (unlikely(p
->policy
== SCHED_BATCH
))
910 gran
= sysctl_sched_batch_wakeup_granularity
;
912 if (is_same_group(curr
, p
))
913 __check_preempt_curr_fair(cfs_rq
, &p
->se
, &curr
->se
, gran
);
916 static struct task_struct
*pick_next_task_fair(struct rq
*rq
)
918 struct cfs_rq
*cfs_rq
= &rq
->cfs
;
919 struct sched_entity
*se
;
921 if (unlikely(!cfs_rq
->nr_running
))
925 se
= pick_next_entity(cfs_rq
);
926 cfs_rq
= group_cfs_rq(se
);
933 * Account for a descheduled task:
935 static void put_prev_task_fair(struct rq
*rq
, struct task_struct
*prev
)
937 struct sched_entity
*se
= &prev
->se
;
938 struct cfs_rq
*cfs_rq
;
940 for_each_sched_entity(se
) {
941 cfs_rq
= cfs_rq_of(se
);
942 put_prev_entity(cfs_rq
, se
);
946 /**************************************************
947 * Fair scheduling class load-balancing methods:
951 * Load-balancing iterator. Note: while the runqueue stays locked
952 * during the whole iteration, the current task might be
953 * dequeued so the iterator has to be dequeue-safe. Here we
954 * achieve that by always pre-iterating before returning
957 static inline struct task_struct
*
958 __load_balance_iterator(struct cfs_rq
*cfs_rq
, struct rb_node
*curr
)
960 struct task_struct
*p
;
965 p
= rb_entry(curr
, struct task_struct
, se
.run_node
);
966 cfs_rq
->rb_load_balance_curr
= rb_next(curr
);
971 static struct task_struct
*load_balance_start_fair(void *arg
)
973 struct cfs_rq
*cfs_rq
= arg
;
975 return __load_balance_iterator(cfs_rq
, first_fair(cfs_rq
));
978 static struct task_struct
*load_balance_next_fair(void *arg
)
980 struct cfs_rq
*cfs_rq
= arg
;
982 return __load_balance_iterator(cfs_rq
, cfs_rq
->rb_load_balance_curr
);
985 #ifdef CONFIG_FAIR_GROUP_SCHED
986 static int cfs_rq_best_prio(struct cfs_rq
*cfs_rq
)
988 struct sched_entity
*curr
;
989 struct task_struct
*p
;
991 if (!cfs_rq
->nr_running
)
994 curr
= __pick_next_entity(cfs_rq
);
1001 static unsigned long
1002 load_balance_fair(struct rq
*this_rq
, int this_cpu
, struct rq
*busiest
,
1003 unsigned long max_nr_move
, unsigned long max_load_move
,
1004 struct sched_domain
*sd
, enum cpu_idle_type idle
,
1005 int *all_pinned
, int *this_best_prio
)
1007 struct cfs_rq
*busy_cfs_rq
;
1008 unsigned long load_moved
, total_nr_moved
= 0, nr_moved
;
1009 long rem_load_move
= max_load_move
;
1010 struct rq_iterator cfs_rq_iterator
;
1012 cfs_rq_iterator
.start
= load_balance_start_fair
;
1013 cfs_rq_iterator
.next
= load_balance_next_fair
;
1015 for_each_leaf_cfs_rq(busiest
, busy_cfs_rq
) {
1016 #ifdef CONFIG_FAIR_GROUP_SCHED
1017 struct cfs_rq
*this_cfs_rq
;
1019 unsigned long maxload
;
1021 this_cfs_rq
= cpu_cfs_rq(busy_cfs_rq
, this_cpu
);
1023 imbalance
= busy_cfs_rq
->load
.weight
- this_cfs_rq
->load
.weight
;
1024 /* Don't pull if this_cfs_rq has more load than busy_cfs_rq */
1028 /* Don't pull more than imbalance/2 */
1030 maxload
= min(rem_load_move
, imbalance
);
1032 *this_best_prio
= cfs_rq_best_prio(this_cfs_rq
);
1034 # define maxload rem_load_move
1036 /* pass busy_cfs_rq argument into
1037 * load_balance_[start|next]_fair iterators
1039 cfs_rq_iterator
.arg
= busy_cfs_rq
;
1040 nr_moved
= balance_tasks(this_rq
, this_cpu
, busiest
,
1041 max_nr_move
, maxload
, sd
, idle
, all_pinned
,
1042 &load_moved
, this_best_prio
, &cfs_rq_iterator
);
1044 total_nr_moved
+= nr_moved
;
1045 max_nr_move
-= nr_moved
;
1046 rem_load_move
-= load_moved
;
1048 if (max_nr_move
<= 0 || rem_load_move
<= 0)
1052 return max_load_move
- rem_load_move
;
1056 * scheduler tick hitting a task of our scheduling class:
1058 static void task_tick_fair(struct rq
*rq
, struct task_struct
*curr
)
1060 struct cfs_rq
*cfs_rq
;
1061 struct sched_entity
*se
= &curr
->se
;
1063 for_each_sched_entity(se
) {
1064 cfs_rq
= cfs_rq_of(se
);
1065 entity_tick(cfs_rq
, se
);
1070 * Share the fairness runtime between parent and child, thus the
1071 * total amount of pressure for CPU stays equal - new tasks
1072 * get a chance to run but frequent forkers are not allowed to
1073 * monopolize the CPU. Note: the parent runqueue is locked,
1074 * the child is not running yet.
1076 static void task_new_fair(struct rq
*rq
, struct task_struct
*p
)
1078 struct cfs_rq
*cfs_rq
= task_cfs_rq(p
);
1079 struct sched_entity
*se
= &p
->se
;
1081 sched_info_queued(p
);
1083 update_stats_enqueue(cfs_rq
, se
);
1085 * Child runs first: we let it run before the parent
1086 * until it reschedules once. We set up the key so that
1087 * it will preempt the parent:
1089 p
->se
.fair_key
= current
->se
.fair_key
-
1090 niced_granularity(&rq
->curr
->se
, sched_granularity(cfs_rq
)) - 1;
1092 * The first wait is dominated by the child-runs-first logic,
1093 * so do not credit it with that waiting time yet:
1095 if (sysctl_sched_features
& SCHED_FEAT_SKIP_INITIAL
)
1096 p
->se
.wait_start_fair
= 0;
1099 * The statistical average of wait_runtime is about
1100 * -granularity/2, so initialize the task with that:
1102 if (sysctl_sched_features
& SCHED_FEAT_START_DEBIT
)
1103 p
->se
.wait_runtime
= -(sched_granularity(cfs_rq
) / 2);
1105 __enqueue_entity(cfs_rq
, se
);
1108 #ifdef CONFIG_FAIR_GROUP_SCHED
1109 /* Account for a task changing its policy or group.
1111 * This routine is mostly called to set cfs_rq->curr field when a task
1112 * migrates between groups/classes.
1114 static void set_curr_task_fair(struct rq
*rq
)
1116 struct sched_entity
*se
= &rq
->curr
->se
;
1118 for_each_sched_entity(se
)
1119 set_next_entity(cfs_rq_of(se
), se
);
1122 static void set_curr_task_fair(struct rq
*rq
)
1128 * All the scheduling class methods:
1130 struct sched_class fair_sched_class __read_mostly
= {
1131 .enqueue_task
= enqueue_task_fair
,
1132 .dequeue_task
= dequeue_task_fair
,
1133 .yield_task
= yield_task_fair
,
1135 .check_preempt_curr
= check_preempt_curr_fair
,
1137 .pick_next_task
= pick_next_task_fair
,
1138 .put_prev_task
= put_prev_task_fair
,
1140 .load_balance
= load_balance_fair
,
1142 .set_curr_task
= set_curr_task_fair
,
1143 .task_tick
= task_tick_fair
,
1144 .task_new
= task_new_fair
,
1147 #ifdef CONFIG_SCHED_DEBUG
1148 static void print_cfs_stats(struct seq_file
*m
, int cpu
)
1150 struct cfs_rq
*cfs_rq
;
1152 for_each_leaf_cfs_rq(cpu_rq(cpu
), cfs_rq
)
1153 print_cfs_rq(m
, cpu
, cfs_rq
);