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>
21 * Preemption granularity:
22 * (default: 2 msec, units: nanoseconds)
24 * NOTE: this granularity value is not the same as the concept of
25 * 'timeslice length' - timeslices in CFS will typically be somewhat
26 * larger than this value. (to see the precise effective timeslice
27 * length of your workload, run vmstat and monitor the context-switches
30 * On SMP systems the value of this is multiplied by the log2 of the
31 * number of CPUs. (i.e. factor 2x on 2-way systems, 3x on 4-way
32 * systems, 4x on 8-way systems, 5x on 16-way systems, etc.)
34 unsigned int sysctl_sched_granularity __read_mostly
= 2000000000ULL/HZ
;
37 * SCHED_BATCH wake-up granularity.
38 * (default: 10 msec, units: nanoseconds)
40 * This option delays the preemption effects of decoupled workloads
41 * and reduces their over-scheduling. Synchronous workloads will still
42 * have immediate wakeup/sleep latencies.
44 unsigned int sysctl_sched_batch_wakeup_granularity __read_mostly
=
48 * SCHED_OTHER wake-up granularity.
49 * (default: 1 msec, units: nanoseconds)
51 * This option delays the preemption effects of decoupled workloads
52 * and reduces their over-scheduling. Synchronous workloads will still
53 * have immediate wakeup/sleep latencies.
55 unsigned int sysctl_sched_wakeup_granularity __read_mostly
= 1000000000ULL/HZ
;
57 unsigned int sysctl_sched_stat_granularity __read_mostly
;
60 * Initialized in sched_init_granularity():
62 unsigned int sysctl_sched_runtime_limit __read_mostly
;
65 * Debugging: various feature bits
68 SCHED_FEAT_FAIR_SLEEPERS
= 1,
69 SCHED_FEAT_SLEEPER_AVG
= 2,
70 SCHED_FEAT_SLEEPER_LOAD_AVG
= 4,
71 SCHED_FEAT_PRECISE_CPU_LOAD
= 8,
72 SCHED_FEAT_START_DEBIT
= 16,
73 SCHED_FEAT_SKIP_INITIAL
= 32,
76 unsigned int sysctl_sched_features __read_mostly
=
77 SCHED_FEAT_FAIR_SLEEPERS
*1 |
78 SCHED_FEAT_SLEEPER_AVG
*1 |
79 SCHED_FEAT_SLEEPER_LOAD_AVG
*1 |
80 SCHED_FEAT_PRECISE_CPU_LOAD
*1 |
81 SCHED_FEAT_START_DEBIT
*1 |
82 SCHED_FEAT_SKIP_INITIAL
*0;
84 extern struct sched_class fair_sched_class
;
86 /**************************************************************
87 * CFS operations on generic schedulable entities:
90 #ifdef CONFIG_FAIR_GROUP_SCHED
92 /* cpu runqueue to which this cfs_rq is attached */
93 static inline struct rq
*rq_of(struct cfs_rq
*cfs_rq
)
98 /* currently running entity (if any) on this cfs_rq */
99 static inline struct sched_entity
*cfs_rq_curr(struct cfs_rq
*cfs_rq
)
104 /* An entity is a task if it doesn't "own" a runqueue */
105 #define entity_is_task(se) (!se->my_q)
108 set_cfs_rq_curr(struct cfs_rq
*cfs_rq
, struct sched_entity
*se
)
113 #else /* CONFIG_FAIR_GROUP_SCHED */
115 static inline struct rq
*rq_of(struct cfs_rq
*cfs_rq
)
117 return container_of(cfs_rq
, struct rq
, cfs
);
120 static inline struct sched_entity
*cfs_rq_curr(struct cfs_rq
*cfs_rq
)
122 struct rq
*rq
= rq_of(cfs_rq
);
124 if (unlikely(rq
->curr
->sched_class
!= &fair_sched_class
))
127 return &rq
->curr
->se
;
130 #define entity_is_task(se) 1
133 set_cfs_rq_curr(struct cfs_rq
*cfs_rq
, struct sched_entity
*se
) { }
135 #endif /* CONFIG_FAIR_GROUP_SCHED */
137 static inline struct task_struct
*task_of(struct sched_entity
*se
)
139 return container_of(se
, struct task_struct
, se
);
143 /**************************************************************
144 * Scheduling class tree data structure manipulation methods:
148 * Enqueue an entity into the rb-tree:
151 __enqueue_entity(struct cfs_rq
*cfs_rq
, struct sched_entity
*se
)
153 struct rb_node
**link
= &cfs_rq
->tasks_timeline
.rb_node
;
154 struct rb_node
*parent
= NULL
;
155 struct sched_entity
*entry
;
156 s64 key
= se
->fair_key
;
160 * Find the right place in the rbtree:
164 entry
= rb_entry(parent
, struct sched_entity
, run_node
);
166 * We dont care about collisions. Nodes with
167 * the same key stay together.
169 if (key
- entry
->fair_key
< 0) {
170 link
= &parent
->rb_left
;
172 link
= &parent
->rb_right
;
178 * Maintain a cache of leftmost tree entries (it is frequently
182 cfs_rq
->rb_leftmost
= &se
->run_node
;
184 rb_link_node(&se
->run_node
, parent
, link
);
185 rb_insert_color(&se
->run_node
, &cfs_rq
->tasks_timeline
);
186 update_load_add(&cfs_rq
->load
, se
->load
.weight
);
187 cfs_rq
->nr_running
++;
192 __dequeue_entity(struct cfs_rq
*cfs_rq
, struct sched_entity
*se
)
194 if (cfs_rq
->rb_leftmost
== &se
->run_node
)
195 cfs_rq
->rb_leftmost
= rb_next(&se
->run_node
);
196 rb_erase(&se
->run_node
, &cfs_rq
->tasks_timeline
);
197 update_load_sub(&cfs_rq
->load
, se
->load
.weight
);
198 cfs_rq
->nr_running
--;
202 static inline struct rb_node
*first_fair(struct cfs_rq
*cfs_rq
)
204 return cfs_rq
->rb_leftmost
;
207 static struct sched_entity
*__pick_next_entity(struct cfs_rq
*cfs_rq
)
209 return rb_entry(first_fair(cfs_rq
), struct sched_entity
, run_node
);
212 /**************************************************************
213 * Scheduling class statistics methods:
217 * We rescale the rescheduling granularity of tasks according to their
218 * nice level, but only linearly, not exponentially:
221 niced_granularity(struct sched_entity
*curr
, unsigned long granularity
)
226 * Negative nice levels get the same granularity as nice-0:
228 if (likely(curr
->load
.weight
>= NICE_0_LOAD
))
231 * Positive nice level tasks get linearly finer
234 tmp
= curr
->load
.weight
* (u64
)granularity
;
237 * It will always fit into 'long':
239 return (long) (tmp
>> NICE_0_SHIFT
);
243 limit_wait_runtime(struct cfs_rq
*cfs_rq
, struct sched_entity
*se
)
245 long limit
= sysctl_sched_runtime_limit
;
248 * Niced tasks have the same history dynamic range as
251 if (unlikely(se
->wait_runtime
> limit
)) {
252 se
->wait_runtime
= limit
;
253 schedstat_inc(se
, wait_runtime_overruns
);
254 schedstat_inc(cfs_rq
, wait_runtime_overruns
);
256 if (unlikely(se
->wait_runtime
< -limit
)) {
257 se
->wait_runtime
= -limit
;
258 schedstat_inc(se
, wait_runtime_underruns
);
259 schedstat_inc(cfs_rq
, wait_runtime_underruns
);
264 __add_wait_runtime(struct cfs_rq
*cfs_rq
, struct sched_entity
*se
, long delta
)
266 se
->wait_runtime
+= delta
;
267 schedstat_add(se
, sum_wait_runtime
, delta
);
268 limit_wait_runtime(cfs_rq
, se
);
272 add_wait_runtime(struct cfs_rq
*cfs_rq
, struct sched_entity
*se
, long delta
)
274 schedstat_add(cfs_rq
, wait_runtime
, -se
->wait_runtime
);
275 __add_wait_runtime(cfs_rq
, se
, delta
);
276 schedstat_add(cfs_rq
, wait_runtime
, se
->wait_runtime
);
280 * Update the current task's runtime statistics. Skip current tasks that
281 * are not in our scheduling class.
284 __update_curr(struct cfs_rq
*cfs_rq
, struct sched_entity
*curr
)
286 unsigned long delta
, delta_exec
, delta_fair
, delta_mine
;
287 struct load_weight
*lw
= &cfs_rq
->load
;
288 unsigned long load
= lw
->weight
;
290 delta_exec
= curr
->delta_exec
;
291 schedstat_set(curr
->exec_max
, max((u64
)delta_exec
, curr
->exec_max
));
293 curr
->sum_exec_runtime
+= delta_exec
;
294 cfs_rq
->exec_clock
+= delta_exec
;
299 delta_fair
= calc_delta_fair(delta_exec
, lw
);
300 delta_mine
= calc_delta_mine(delta_exec
, curr
->load
.weight
, lw
);
302 if (cfs_rq
->sleeper_bonus
> sysctl_sched_granularity
) {
303 delta
= calc_delta_mine(cfs_rq
->sleeper_bonus
,
304 curr
->load
.weight
, lw
);
305 if (unlikely(delta
> cfs_rq
->sleeper_bonus
))
306 delta
= cfs_rq
->sleeper_bonus
;
308 cfs_rq
->sleeper_bonus
-= delta
;
312 cfs_rq
->fair_clock
+= delta_fair
;
314 * We executed delta_exec amount of time on the CPU,
315 * but we were only entitled to delta_mine amount of
316 * time during that period (if nr_running == 1 then
317 * the two values are equal)
318 * [Note: delta_mine - delta_exec is negative]:
320 add_wait_runtime(cfs_rq
, curr
, delta_mine
- delta_exec
);
323 static void update_curr(struct cfs_rq
*cfs_rq
)
325 struct sched_entity
*curr
= cfs_rq_curr(cfs_rq
);
326 unsigned long delta_exec
;
332 * Get the amount of time the current task was running
333 * since the last time we changed load (this cannot
334 * overflow on 32 bits):
336 delta_exec
= (unsigned long)(rq_of(cfs_rq
)->clock
- curr
->exec_start
);
338 curr
->delta_exec
+= delta_exec
;
340 if (unlikely(curr
->delta_exec
> sysctl_sched_stat_granularity
)) {
341 __update_curr(cfs_rq
, curr
);
342 curr
->delta_exec
= 0;
344 curr
->exec_start
= rq_of(cfs_rq
)->clock
;
348 update_stats_wait_start(struct cfs_rq
*cfs_rq
, struct sched_entity
*se
)
350 se
->wait_start_fair
= cfs_rq
->fair_clock
;
351 schedstat_set(se
->wait_start
, rq_of(cfs_rq
)->clock
);
355 * We calculate fair deltas here, so protect against the random effects
356 * of a multiplication overflow by capping it to the runtime limit:
358 #if BITS_PER_LONG == 32
359 static inline unsigned long
360 calc_weighted(unsigned long delta
, unsigned long weight
, int shift
)
362 u64 tmp
= (u64
)delta
* weight
>> shift
;
364 if (unlikely(tmp
> sysctl_sched_runtime_limit
*2))
365 return sysctl_sched_runtime_limit
*2;
369 static inline unsigned long
370 calc_weighted(unsigned long delta
, unsigned long weight
, int shift
)
372 return delta
* weight
>> shift
;
377 * Task is being enqueued - update stats:
379 static void update_stats_enqueue(struct cfs_rq
*cfs_rq
, struct sched_entity
*se
)
384 * Are we enqueueing a waiting task? (for current tasks
385 * a dequeue/enqueue event is a NOP)
387 if (se
!= cfs_rq_curr(cfs_rq
))
388 update_stats_wait_start(cfs_rq
, se
);
392 key
= cfs_rq
->fair_clock
;
395 * Optimize the common nice 0 case:
397 if (likely(se
->load
.weight
== NICE_0_LOAD
)) {
398 key
-= se
->wait_runtime
;
402 if (se
->wait_runtime
< 0) {
403 tmp
= -se
->wait_runtime
;
404 key
+= (tmp
* se
->load
.inv_weight
) >>
405 (WMULT_SHIFT
- NICE_0_SHIFT
);
407 tmp
= se
->wait_runtime
;
408 key
-= (tmp
* se
->load
.weight
) >> NICE_0_SHIFT
;
416 * Note: must be called with a freshly updated rq->fair_clock.
419 __update_stats_wait_end(struct cfs_rq
*cfs_rq
, struct sched_entity
*se
)
421 unsigned long delta_fair
= se
->delta_fair_run
;
423 schedstat_set(se
->wait_max
, max(se
->wait_max
,
424 rq_of(cfs_rq
)->clock
- se
->wait_start
));
426 if (unlikely(se
->load
.weight
!= NICE_0_LOAD
))
427 delta_fair
= calc_weighted(delta_fair
, se
->load
.weight
,
430 add_wait_runtime(cfs_rq
, se
, delta_fair
);
434 update_stats_wait_end(struct cfs_rq
*cfs_rq
, struct sched_entity
*se
)
436 unsigned long delta_fair
;
438 delta_fair
= (unsigned long)min((u64
)(2*sysctl_sched_runtime_limit
),
439 (u64
)(cfs_rq
->fair_clock
- se
->wait_start_fair
));
441 se
->delta_fair_run
+= delta_fair
;
442 if (unlikely(abs(se
->delta_fair_run
) >=
443 sysctl_sched_stat_granularity
)) {
444 __update_stats_wait_end(cfs_rq
, se
);
445 se
->delta_fair_run
= 0;
448 se
->wait_start_fair
= 0;
449 schedstat_set(se
->wait_start
, 0);
453 update_stats_dequeue(struct cfs_rq
*cfs_rq
, struct sched_entity
*se
)
457 * Mark the end of the wait period if dequeueing a
460 if (se
!= cfs_rq_curr(cfs_rq
))
461 update_stats_wait_end(cfs_rq
, se
);
465 * We are picking a new current task - update its stats:
468 update_stats_curr_start(struct cfs_rq
*cfs_rq
, struct sched_entity
*se
)
471 * We are starting a new run period:
473 se
->exec_start
= rq_of(cfs_rq
)->clock
;
477 * We are descheduling a task - update its stats:
480 update_stats_curr_end(struct cfs_rq
*cfs_rq
, struct sched_entity
*se
)
485 /**************************************************
486 * Scheduling class queueing methods:
489 static void __enqueue_sleeper(struct cfs_rq
*cfs_rq
, struct sched_entity
*se
)
491 unsigned long load
= cfs_rq
->load
.weight
, delta_fair
;
494 if (sysctl_sched_features
& SCHED_FEAT_SLEEPER_LOAD_AVG
)
495 load
= rq_of(cfs_rq
)->cpu_load
[2];
497 delta_fair
= se
->delta_fair_sleep
;
500 * Fix up delta_fair with the effect of us running
501 * during the whole sleep period:
503 if (sysctl_sched_features
& SCHED_FEAT_SLEEPER_AVG
)
504 delta_fair
= div64_likely32((u64
)delta_fair
* load
,
505 load
+ se
->load
.weight
);
507 if (unlikely(se
->load
.weight
!= NICE_0_LOAD
))
508 delta_fair
= calc_weighted(delta_fair
, se
->load
.weight
,
511 prev_runtime
= se
->wait_runtime
;
512 __add_wait_runtime(cfs_rq
, se
, delta_fair
);
513 delta_fair
= se
->wait_runtime
- prev_runtime
;
516 * Track the amount of bonus we've given to sleepers:
518 cfs_rq
->sleeper_bonus
+= delta_fair
;
520 schedstat_add(cfs_rq
, wait_runtime
, se
->wait_runtime
);
523 static void enqueue_sleeper(struct cfs_rq
*cfs_rq
, struct sched_entity
*se
)
525 struct task_struct
*tsk
= task_of(se
);
526 unsigned long delta_fair
;
528 if ((entity_is_task(se
) && tsk
->policy
== SCHED_BATCH
) ||
529 !(sysctl_sched_features
& SCHED_FEAT_FAIR_SLEEPERS
))
532 delta_fair
= (unsigned long)min((u64
)(2*sysctl_sched_runtime_limit
),
533 (u64
)(cfs_rq
->fair_clock
- se
->sleep_start_fair
));
535 se
->delta_fair_sleep
+= delta_fair
;
536 if (unlikely(abs(se
->delta_fair_sleep
) >=
537 sysctl_sched_stat_granularity
)) {
538 __enqueue_sleeper(cfs_rq
, se
);
539 se
->delta_fair_sleep
= 0;
542 se
->sleep_start_fair
= 0;
544 #ifdef CONFIG_SCHEDSTATS
545 if (se
->sleep_start
) {
546 u64 delta
= rq_of(cfs_rq
)->clock
- se
->sleep_start
;
551 if (unlikely(delta
> se
->sleep_max
))
552 se
->sleep_max
= delta
;
555 se
->sum_sleep_runtime
+= delta
;
557 if (se
->block_start
) {
558 u64 delta
= rq_of(cfs_rq
)->clock
- se
->block_start
;
563 if (unlikely(delta
> se
->block_max
))
564 se
->block_max
= delta
;
567 se
->sum_sleep_runtime
+= delta
;
573 enqueue_entity(struct cfs_rq
*cfs_rq
, struct sched_entity
*se
, int wakeup
)
576 * Update the fair clock.
581 enqueue_sleeper(cfs_rq
, se
);
583 update_stats_enqueue(cfs_rq
, se
);
584 __enqueue_entity(cfs_rq
, se
);
588 dequeue_entity(struct cfs_rq
*cfs_rq
, struct sched_entity
*se
, int sleep
)
590 update_stats_dequeue(cfs_rq
, se
);
592 se
->sleep_start_fair
= cfs_rq
->fair_clock
;
593 #ifdef CONFIG_SCHEDSTATS
594 if (entity_is_task(se
)) {
595 struct task_struct
*tsk
= task_of(se
);
597 if (tsk
->state
& TASK_INTERRUPTIBLE
)
598 se
->sleep_start
= rq_of(cfs_rq
)->clock
;
599 if (tsk
->state
& TASK_UNINTERRUPTIBLE
)
600 se
->block_start
= rq_of(cfs_rq
)->clock
;
602 cfs_rq
->wait_runtime
-= se
->wait_runtime
;
605 __dequeue_entity(cfs_rq
, se
);
609 * Preempt the current task with a newly woken task if needed:
612 __check_preempt_curr_fair(struct cfs_rq
*cfs_rq
, struct sched_entity
*se
,
613 struct sched_entity
*curr
, unsigned long granularity
)
615 s64 __delta
= curr
->fair_key
- se
->fair_key
;
618 * Take scheduling granularity into account - do not
619 * preempt the current task unless the best task has
620 * a larger than sched_granularity fairness advantage:
622 if (__delta
> niced_granularity(curr
, granularity
))
623 resched_task(rq_of(cfs_rq
)->curr
);
627 set_next_entity(struct cfs_rq
*cfs_rq
, struct sched_entity
*se
)
630 * Any task has to be enqueued before it get to execute on
631 * a CPU. So account for the time it spent waiting on the
632 * runqueue. (note, here we rely on pick_next_task() having
633 * done a put_prev_task_fair() shortly before this, which
634 * updated rq->fair_clock - used by update_stats_wait_end())
636 update_stats_wait_end(cfs_rq
, se
);
637 update_stats_curr_start(cfs_rq
, se
);
638 set_cfs_rq_curr(cfs_rq
, se
);
641 static struct sched_entity
*pick_next_entity(struct cfs_rq
*cfs_rq
)
643 struct sched_entity
*se
= __pick_next_entity(cfs_rq
);
645 set_next_entity(cfs_rq
, se
);
650 static void put_prev_entity(struct cfs_rq
*cfs_rq
, struct sched_entity
*prev
)
653 * If still on the runqueue then deactivate_task()
654 * was not called and update_curr() has to be done:
659 update_stats_curr_end(cfs_rq
, prev
);
662 update_stats_wait_start(cfs_rq
, prev
);
663 set_cfs_rq_curr(cfs_rq
, NULL
);
666 static void entity_tick(struct cfs_rq
*cfs_rq
, struct sched_entity
*curr
)
668 struct sched_entity
*next
;
671 * Dequeue and enqueue the task to update its
672 * position within the tree:
674 dequeue_entity(cfs_rq
, curr
, 0);
675 enqueue_entity(cfs_rq
, curr
, 0);
678 * Reschedule if another task tops the current one.
680 next
= __pick_next_entity(cfs_rq
);
684 __check_preempt_curr_fair(cfs_rq
, next
, curr
, sysctl_sched_granularity
);
687 /**************************************************
688 * CFS operations on tasks:
691 #ifdef CONFIG_FAIR_GROUP_SCHED
693 /* Walk up scheduling entities hierarchy */
694 #define for_each_sched_entity(se) \
695 for (; se; se = se->parent)
697 static inline struct cfs_rq
*task_cfs_rq(struct task_struct
*p
)
702 /* runqueue on which this entity is (to be) queued */
703 static inline struct cfs_rq
*cfs_rq_of(struct sched_entity
*se
)
708 /* runqueue "owned" by this group */
709 static inline struct cfs_rq
*group_cfs_rq(struct sched_entity
*grp
)
714 /* Given a group's cfs_rq on one cpu, return its corresponding cfs_rq on
715 * another cpu ('this_cpu')
717 static inline struct cfs_rq
*cpu_cfs_rq(struct cfs_rq
*cfs_rq
, int this_cpu
)
719 /* A later patch will take group into account */
720 return &cpu_rq(this_cpu
)->cfs
;
723 /* Iterate thr' all leaf cfs_rq's on a runqueue */
724 #define for_each_leaf_cfs_rq(rq, cfs_rq) \
725 list_for_each_entry(cfs_rq, &rq->leaf_cfs_rq_list, leaf_cfs_rq_list)
727 /* Do the two (enqueued) tasks belong to the same group ? */
728 static inline int is_same_group(struct task_struct
*curr
, struct task_struct
*p
)
730 if (curr
->se
.cfs_rq
== p
->se
.cfs_rq
)
736 #else /* CONFIG_FAIR_GROUP_SCHED */
738 #define for_each_sched_entity(se) \
739 for (; se; se = NULL)
741 static inline struct cfs_rq
*task_cfs_rq(struct task_struct
*p
)
743 return &task_rq(p
)->cfs
;
746 static inline struct cfs_rq
*cfs_rq_of(struct sched_entity
*se
)
748 struct task_struct
*p
= task_of(se
);
749 struct rq
*rq
= task_rq(p
);
754 /* runqueue "owned" by this group */
755 static inline struct cfs_rq
*group_cfs_rq(struct sched_entity
*grp
)
760 static inline struct cfs_rq
*cpu_cfs_rq(struct cfs_rq
*cfs_rq
, int this_cpu
)
762 return &cpu_rq(this_cpu
)->cfs
;
765 #define for_each_leaf_cfs_rq(rq, cfs_rq) \
766 for (cfs_rq = &rq->cfs; cfs_rq; cfs_rq = NULL)
768 static inline int is_same_group(struct task_struct
*curr
, struct task_struct
*p
)
773 #endif /* CONFIG_FAIR_GROUP_SCHED */
776 * The enqueue_task method is called before nr_running is
777 * increased. Here we update the fair scheduling stats and
778 * then put the task into the rbtree:
780 static void enqueue_task_fair(struct rq
*rq
, struct task_struct
*p
, int wakeup
)
782 struct cfs_rq
*cfs_rq
;
783 struct sched_entity
*se
= &p
->se
;
785 for_each_sched_entity(se
) {
788 cfs_rq
= cfs_rq_of(se
);
789 enqueue_entity(cfs_rq
, se
, wakeup
);
794 * The dequeue_task method is called before nr_running is
795 * decreased. We remove the task from the rbtree and
796 * update the fair scheduling stats:
798 static void dequeue_task_fair(struct rq
*rq
, struct task_struct
*p
, int sleep
)
800 struct cfs_rq
*cfs_rq
;
801 struct sched_entity
*se
= &p
->se
;
803 for_each_sched_entity(se
) {
804 cfs_rq
= cfs_rq_of(se
);
805 dequeue_entity(cfs_rq
, se
, sleep
);
806 /* Don't dequeue parent if it has other entities besides us */
807 if (cfs_rq
->load
.weight
)
813 * sched_yield() support is very simple - we dequeue and enqueue
815 static void yield_task_fair(struct rq
*rq
, struct task_struct
*p
)
817 struct cfs_rq
*cfs_rq
= task_cfs_rq(p
);
819 __update_rq_clock(rq
);
821 * Dequeue and enqueue the task to update its
822 * position within the tree:
824 dequeue_entity(cfs_rq
, &p
->se
, 0);
825 enqueue_entity(cfs_rq
, &p
->se
, 0);
829 * Preempt the current task with a newly woken task if needed:
831 static void check_preempt_curr_fair(struct rq
*rq
, struct task_struct
*p
)
833 struct task_struct
*curr
= rq
->curr
;
834 struct cfs_rq
*cfs_rq
= task_cfs_rq(curr
);
837 if (unlikely(rt_prio(p
->prio
))) {
844 gran
= sysctl_sched_wakeup_granularity
;
846 * Batch tasks prefer throughput over latency:
848 if (unlikely(p
->policy
== SCHED_BATCH
))
849 gran
= sysctl_sched_batch_wakeup_granularity
;
851 if (is_same_group(curr
, p
))
852 __check_preempt_curr_fair(cfs_rq
, &p
->se
, &curr
->se
, gran
);
855 static struct task_struct
*pick_next_task_fair(struct rq
*rq
)
857 struct cfs_rq
*cfs_rq
= &rq
->cfs
;
858 struct sched_entity
*se
;
860 if (unlikely(!cfs_rq
->nr_running
))
864 se
= pick_next_entity(cfs_rq
);
865 cfs_rq
= group_cfs_rq(se
);
872 * Account for a descheduled task:
874 static void put_prev_task_fair(struct rq
*rq
, struct task_struct
*prev
)
876 struct sched_entity
*se
= &prev
->se
;
877 struct cfs_rq
*cfs_rq
;
879 for_each_sched_entity(se
) {
880 cfs_rq
= cfs_rq_of(se
);
881 put_prev_entity(cfs_rq
, se
);
885 /**************************************************
886 * Fair scheduling class load-balancing methods:
890 * Load-balancing iterator. Note: while the runqueue stays locked
891 * during the whole iteration, the current task might be
892 * dequeued so the iterator has to be dequeue-safe. Here we
893 * achieve that by always pre-iterating before returning
896 static inline struct task_struct
*
897 __load_balance_iterator(struct cfs_rq
*cfs_rq
, struct rb_node
*curr
)
899 struct task_struct
*p
;
904 p
= rb_entry(curr
, struct task_struct
, se
.run_node
);
905 cfs_rq
->rb_load_balance_curr
= rb_next(curr
);
910 static struct task_struct
*load_balance_start_fair(void *arg
)
912 struct cfs_rq
*cfs_rq
= arg
;
914 return __load_balance_iterator(cfs_rq
, first_fair(cfs_rq
));
917 static struct task_struct
*load_balance_next_fair(void *arg
)
919 struct cfs_rq
*cfs_rq
= arg
;
921 return __load_balance_iterator(cfs_rq
, cfs_rq
->rb_load_balance_curr
);
924 #ifdef CONFIG_FAIR_GROUP_SCHED
925 static int cfs_rq_best_prio(struct cfs_rq
*cfs_rq
)
927 struct sched_entity
*curr
;
928 struct task_struct
*p
;
930 if (!cfs_rq
->nr_running
)
933 curr
= __pick_next_entity(cfs_rq
);
941 load_balance_fair(struct rq
*this_rq
, int this_cpu
, struct rq
*busiest
,
942 unsigned long max_nr_move
, unsigned long max_load_move
,
943 struct sched_domain
*sd
, enum cpu_idle_type idle
,
944 int *all_pinned
, int *this_best_prio
)
946 struct cfs_rq
*busy_cfs_rq
;
947 unsigned long load_moved
, total_nr_moved
= 0, nr_moved
;
948 long rem_load_move
= max_load_move
;
949 struct rq_iterator cfs_rq_iterator
;
951 cfs_rq_iterator
.start
= load_balance_start_fair
;
952 cfs_rq_iterator
.next
= load_balance_next_fair
;
954 for_each_leaf_cfs_rq(busiest
, busy_cfs_rq
) {
955 #ifdef CONFIG_FAIR_GROUP_SCHED
956 struct cfs_rq
*this_cfs_rq
;
958 unsigned long maxload
;
960 this_cfs_rq
= cpu_cfs_rq(busy_cfs_rq
, this_cpu
);
962 imbalance
= busy_cfs_rq
->load
.weight
-
963 this_cfs_rq
->load
.weight
;
964 /* Don't pull if this_cfs_rq has more load than busy_cfs_rq */
968 /* Don't pull more than imbalance/2 */
970 maxload
= min(rem_load_move
, imbalance
);
972 *this_best_prio
= cfs_rq_best_prio(this_cfs_rq
);
974 #define maxload rem_load_move
976 /* pass busy_cfs_rq argument into
977 * load_balance_[start|next]_fair iterators
979 cfs_rq_iterator
.arg
= busy_cfs_rq
;
980 nr_moved
= balance_tasks(this_rq
, this_cpu
, busiest
,
981 max_nr_move
, maxload
, sd
, idle
, all_pinned
,
982 &load_moved
, this_best_prio
, &cfs_rq_iterator
);
984 total_nr_moved
+= nr_moved
;
985 max_nr_move
-= nr_moved
;
986 rem_load_move
-= load_moved
;
988 if (max_nr_move
<= 0 || rem_load_move
<= 0)
992 return max_load_move
- rem_load_move
;
996 * scheduler tick hitting a task of our scheduling class:
998 static void task_tick_fair(struct rq
*rq
, struct task_struct
*curr
)
1000 struct cfs_rq
*cfs_rq
;
1001 struct sched_entity
*se
= &curr
->se
;
1003 for_each_sched_entity(se
) {
1004 cfs_rq
= cfs_rq_of(se
);
1005 entity_tick(cfs_rq
, se
);
1010 * Share the fairness runtime between parent and child, thus the
1011 * total amount of pressure for CPU stays equal - new tasks
1012 * get a chance to run but frequent forkers are not allowed to
1013 * monopolize the CPU. Note: the parent runqueue is locked,
1014 * the child is not running yet.
1016 static void task_new_fair(struct rq
*rq
, struct task_struct
*p
)
1018 struct cfs_rq
*cfs_rq
= task_cfs_rq(p
);
1019 struct sched_entity
*se
= &p
->se
;
1021 sched_info_queued(p
);
1023 update_stats_enqueue(cfs_rq
, se
);
1025 * Child runs first: we let it run before the parent
1026 * until it reschedules once. We set up the key so that
1027 * it will preempt the parent:
1029 p
->se
.fair_key
= current
->se
.fair_key
-
1030 niced_granularity(&rq
->curr
->se
, sysctl_sched_granularity
) - 1;
1032 * The first wait is dominated by the child-runs-first logic,
1033 * so do not credit it with that waiting time yet:
1035 if (sysctl_sched_features
& SCHED_FEAT_SKIP_INITIAL
)
1036 p
->se
.wait_start_fair
= 0;
1039 * The statistical average of wait_runtime is about
1040 * -granularity/2, so initialize the task with that:
1042 if (sysctl_sched_features
& SCHED_FEAT_START_DEBIT
)
1043 p
->se
.wait_runtime
= -(sysctl_sched_granularity
/ 2);
1045 __enqueue_entity(cfs_rq
, se
);
1048 #ifdef CONFIG_FAIR_GROUP_SCHED
1049 /* Account for a task changing its policy or group.
1051 * This routine is mostly called to set cfs_rq->curr field when a task
1052 * migrates between groups/classes.
1054 static void set_curr_task_fair(struct rq
*rq
)
1056 struct task_struct
*curr
= rq
->curr
;
1057 struct sched_entity
*se
= &curr
->se
;
1058 struct cfs_rq
*cfs_rq
;
1060 update_rq_clock(rq
);
1062 for_each_sched_entity(se
) {
1063 cfs_rq
= cfs_rq_of(se
);
1064 set_next_entity(cfs_rq
, se
);
1068 static void set_curr_task_fair(struct rq
*rq
)
1074 * All the scheduling class methods:
1076 struct sched_class fair_sched_class __read_mostly
= {
1077 .enqueue_task
= enqueue_task_fair
,
1078 .dequeue_task
= dequeue_task_fair
,
1079 .yield_task
= yield_task_fair
,
1081 .check_preempt_curr
= check_preempt_curr_fair
,
1083 .pick_next_task
= pick_next_task_fair
,
1084 .put_prev_task
= put_prev_task_fair
,
1086 .load_balance
= load_balance_fair
,
1088 .set_curr_task
= set_curr_task_fair
,
1089 .task_tick
= task_tick_fair
,
1090 .task_new
= task_new_fair
,
1093 #ifdef CONFIG_SCHED_DEBUG
1094 static void print_cfs_stats(struct seq_file
*m
, int cpu
)
1096 struct rq
*rq
= cpu_rq(cpu
);
1097 struct cfs_rq
*cfs_rq
;
1099 for_each_leaf_cfs_rq(rq
, cfs_rq
)
1100 print_cfs_rq(m
, cpu
, cfs_rq
);