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: 10 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 = 10000000UL;
37 * SCHED_BATCH wake-up granularity.
38 * (default: 25 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 = 25000000UL;
47 * SCHED_OTHER wake-up granularity.
48 * (default: 1 msec, units: nanoseconds)
50 * This option delays the preemption effects of decoupled workloads
51 * and reduces their over-scheduling. Synchronous workloads will still
52 * have immediate wakeup/sleep latencies.
54 unsigned int sysctl_sched_wakeup_granularity __read_mostly = 1000000UL;
56 unsigned int sysctl_sched_stat_granularity __read_mostly;
59 * Initialized in sched_init_granularity() [to 5 times the base granularity]:
61 unsigned int sysctl_sched_runtime_limit __read_mostly;
64 * Debugging: various feature bits
67 SCHED_FEAT_FAIR_SLEEPERS = 1,
68 SCHED_FEAT_SLEEPER_AVG = 2,
69 SCHED_FEAT_SLEEPER_LOAD_AVG = 4,
70 SCHED_FEAT_PRECISE_CPU_LOAD = 8,
71 SCHED_FEAT_START_DEBIT = 16,
72 SCHED_FEAT_SKIP_INITIAL = 32,
75 unsigned int sysctl_sched_features __read_mostly =
76 SCHED_FEAT_FAIR_SLEEPERS *1 |
77 SCHED_FEAT_SLEEPER_AVG *0 |
78 SCHED_FEAT_SLEEPER_LOAD_AVG *1 |
79 SCHED_FEAT_PRECISE_CPU_LOAD *1 |
80 SCHED_FEAT_START_DEBIT *1 |
81 SCHED_FEAT_SKIP_INITIAL *0;
83 extern struct sched_class fair_sched_class;
85 /**************************************************************
86 * CFS operations on generic schedulable entities:
89 #ifdef CONFIG_FAIR_GROUP_SCHED
91 /* cpu runqueue to which this cfs_rq is attached */
92 static inline struct rq *rq_of(struct cfs_rq *cfs_rq)
97 /* currently running entity (if any) on this cfs_rq */
98 static inline struct sched_entity *cfs_rq_curr(struct cfs_rq *cfs_rq)
103 /* An entity is a task if it doesn't "own" a runqueue */
104 #define entity_is_task(se) (!se->my_q)
107 set_cfs_rq_curr(struct cfs_rq *cfs_rq, struct sched_entity *se)
112 #else /* CONFIG_FAIR_GROUP_SCHED */
114 static inline struct rq *rq_of(struct cfs_rq *cfs_rq)
116 return container_of(cfs_rq, struct rq, cfs);
119 static inline struct sched_entity *cfs_rq_curr(struct cfs_rq *cfs_rq)
121 struct rq *rq = rq_of(cfs_rq);
123 if (unlikely(rq->curr->sched_class != &fair_sched_class))
126 return &rq->curr->se;
129 #define entity_is_task(se) 1
132 set_cfs_rq_curr(struct cfs_rq *cfs_rq, struct sched_entity *se) { }
134 #endif /* CONFIG_FAIR_GROUP_SCHED */
136 static inline struct task_struct *task_of(struct sched_entity *se)
138 return container_of(se, struct task_struct, se);
142 /**************************************************************
143 * Scheduling class tree data structure manipulation methods:
147 * Enqueue an entity into the rb-tree:
150 __enqueue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se)
152 struct rb_node **link = &cfs_rq->tasks_timeline.rb_node;
153 struct rb_node *parent = NULL;
154 struct sched_entity *entry;
155 s64 key = se->fair_key;
159 * Find the right place in the rbtree:
163 entry = rb_entry(parent, struct sched_entity, run_node);
165 * We dont care about collisions. Nodes with
166 * the same key stay together.
168 if (key - entry->fair_key < 0) {
169 link = &parent->rb_left;
171 link = &parent->rb_right;
177 * Maintain a cache of leftmost tree entries (it is frequently
181 cfs_rq->rb_leftmost = &se->run_node;
183 rb_link_node(&se->run_node, parent, link);
184 rb_insert_color(&se->run_node, &cfs_rq->tasks_timeline);
185 update_load_add(&cfs_rq->load, se->load.weight);
186 cfs_rq->nr_running++;
191 __dequeue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se)
193 if (cfs_rq->rb_leftmost == &se->run_node)
194 cfs_rq->rb_leftmost = rb_next(&se->run_node);
195 rb_erase(&se->run_node, &cfs_rq->tasks_timeline);
196 update_load_sub(&cfs_rq->load, se->load.weight);
197 cfs_rq->nr_running--;
201 static inline struct rb_node *first_fair(struct cfs_rq *cfs_rq)
203 return cfs_rq->rb_leftmost;
206 static struct sched_entity *__pick_next_entity(struct cfs_rq *cfs_rq)
208 return rb_entry(first_fair(cfs_rq), struct sched_entity, run_node);
211 /**************************************************************
212 * Scheduling class statistics methods:
216 * We rescale the rescheduling granularity of tasks according to their
217 * nice level, but only linearly, not exponentially:
220 niced_granularity(struct sched_entity *curr, unsigned long granularity)
224 if (likely(curr->load.weight == NICE_0_LOAD))
227 * Positive nice levels get the same granularity as nice-0:
229 if (likely(curr->load.weight < NICE_0_LOAD)) {
230 tmp = curr->load.weight * (u64)granularity;
231 return (long) (tmp >> NICE_0_SHIFT);
234 * Negative nice level tasks get linearly finer
237 tmp = curr->load.inv_weight * (u64)granularity;
240 * It will always fit into 'long':
242 return (long) (tmp >> WMULT_SHIFT);
246 limit_wait_runtime(struct cfs_rq *cfs_rq, struct sched_entity *se)
248 long limit = sysctl_sched_runtime_limit;
251 * Niced tasks have the same history dynamic range as
254 if (unlikely(se->wait_runtime > limit)) {
255 se->wait_runtime = limit;
256 schedstat_inc(se, wait_runtime_overruns);
257 schedstat_inc(cfs_rq, wait_runtime_overruns);
259 if (unlikely(se->wait_runtime < -limit)) {
260 se->wait_runtime = -limit;
261 schedstat_inc(se, wait_runtime_underruns);
262 schedstat_inc(cfs_rq, wait_runtime_underruns);
267 __add_wait_runtime(struct cfs_rq *cfs_rq, struct sched_entity *se, long delta)
269 se->wait_runtime += delta;
270 schedstat_add(se, sum_wait_runtime, delta);
271 limit_wait_runtime(cfs_rq, se);
275 add_wait_runtime(struct cfs_rq *cfs_rq, struct sched_entity *se, long delta)
277 schedstat_add(cfs_rq, wait_runtime, -se->wait_runtime);
278 __add_wait_runtime(cfs_rq, se, delta);
279 schedstat_add(cfs_rq, wait_runtime, se->wait_runtime);
283 * Update the current task's runtime statistics. Skip current tasks that
284 * are not in our scheduling class.
287 __update_curr(struct cfs_rq *cfs_rq, struct sched_entity *curr)
289 unsigned long delta, delta_exec, delta_fair, delta_mine;
290 struct load_weight *lw = &cfs_rq->load;
291 unsigned long load = lw->weight;
293 delta_exec = curr->delta_exec;
294 schedstat_set(curr->exec_max, max((u64)delta_exec, curr->exec_max));
296 curr->sum_exec_runtime += delta_exec;
297 cfs_rq->exec_clock += delta_exec;
302 delta_fair = calc_delta_fair(delta_exec, lw);
303 delta_mine = calc_delta_mine(delta_exec, curr->load.weight, lw);
305 if (cfs_rq->sleeper_bonus > sysctl_sched_granularity) {
306 delta = min(cfs_rq->sleeper_bonus, (u64)delta_exec);
307 delta = calc_delta_mine(delta, curr->load.weight, lw);
308 delta = min((u64)delta, cfs_rq->sleeper_bonus);
309 cfs_rq->sleeper_bonus -= delta;
313 cfs_rq->fair_clock += delta_fair;
315 * We executed delta_exec amount of time on the CPU,
316 * but we were only entitled to delta_mine amount of
317 * time during that period (if nr_running == 1 then
318 * the two values are equal)
319 * [Note: delta_mine - delta_exec is negative]:
321 add_wait_runtime(cfs_rq, curr, delta_mine - delta_exec);
324 static void update_curr(struct cfs_rq *cfs_rq)
326 struct sched_entity *curr = cfs_rq_curr(cfs_rq);
327 unsigned long delta_exec;
333 * Get the amount of time the current task was running
334 * since the last time we changed load (this cannot
335 * overflow on 32 bits):
337 delta_exec = (unsigned long)(rq_of(cfs_rq)->clock - curr->exec_start);
339 curr->delta_exec += delta_exec;
341 if (unlikely(curr->delta_exec > sysctl_sched_stat_granularity)) {
342 __update_curr(cfs_rq, curr);
343 curr->delta_exec = 0;
345 curr->exec_start = rq_of(cfs_rq)->clock;
349 update_stats_wait_start(struct cfs_rq *cfs_rq, struct sched_entity *se)
351 se->wait_start_fair = cfs_rq->fair_clock;
352 schedstat_set(se->wait_start, rq_of(cfs_rq)->clock);
356 * We calculate fair deltas here, so protect against the random effects
357 * of a multiplication overflow by capping it to the runtime limit:
359 #if BITS_PER_LONG == 32
360 static inline unsigned long
361 calc_weighted(unsigned long delta, unsigned long weight, int shift)
363 u64 tmp = (u64)delta * weight >> shift;
365 if (unlikely(tmp > sysctl_sched_runtime_limit*2))
366 return sysctl_sched_runtime_limit*2;
370 static inline unsigned long
371 calc_weighted(unsigned long delta, unsigned long weight, int shift)
373 return delta * weight >> shift;
378 * Task is being enqueued - update stats:
380 static void update_stats_enqueue(struct cfs_rq *cfs_rq, struct sched_entity *se)
385 * Are we enqueueing a waiting task? (for current tasks
386 * a dequeue/enqueue event is a NOP)
388 if (se != cfs_rq_curr(cfs_rq))
389 update_stats_wait_start(cfs_rq, se);
393 key = cfs_rq->fair_clock;
396 * Optimize the common nice 0 case:
398 if (likely(se->load.weight == NICE_0_LOAD)) {
399 key -= se->wait_runtime;
403 if (se->wait_runtime < 0) {
404 tmp = -se->wait_runtime;
405 key += (tmp * se->load.inv_weight) >>
406 (WMULT_SHIFT - NICE_0_SHIFT);
408 tmp = se->wait_runtime;
409 key -= (tmp * se->load.inv_weight) >>
410 (WMULT_SHIFT - NICE_0_SHIFT);
418 * Note: must be called with a freshly updated rq->fair_clock.
421 __update_stats_wait_end(struct cfs_rq *cfs_rq, struct sched_entity *se)
423 unsigned long delta_fair = se->delta_fair_run;
425 schedstat_set(se->wait_max, max(se->wait_max,
426 rq_of(cfs_rq)->clock - se->wait_start));
428 if (unlikely(se->load.weight != NICE_0_LOAD))
429 delta_fair = calc_weighted(delta_fair, se->load.weight,
432 add_wait_runtime(cfs_rq, se, delta_fair);
436 update_stats_wait_end(struct cfs_rq *cfs_rq, struct sched_entity *se)
438 unsigned long delta_fair;
440 delta_fair = (unsigned long)min((u64)(2*sysctl_sched_runtime_limit),
441 (u64)(cfs_rq->fair_clock - se->wait_start_fair));
443 se->delta_fair_run += delta_fair;
444 if (unlikely(abs(se->delta_fair_run) >=
445 sysctl_sched_stat_granularity)) {
446 __update_stats_wait_end(cfs_rq, se);
447 se->delta_fair_run = 0;
450 se->wait_start_fair = 0;
451 schedstat_set(se->wait_start, 0);
455 update_stats_dequeue(struct cfs_rq *cfs_rq, struct sched_entity *se)
459 * Mark the end of the wait period if dequeueing a
462 if (se != cfs_rq_curr(cfs_rq))
463 update_stats_wait_end(cfs_rq, se);
467 * We are picking a new current task - update its stats:
470 update_stats_curr_start(struct cfs_rq *cfs_rq, struct sched_entity *se)
473 * We are starting a new run period:
475 se->exec_start = rq_of(cfs_rq)->clock;
479 * We are descheduling a task - update its stats:
482 update_stats_curr_end(struct cfs_rq *cfs_rq, struct sched_entity *se)
487 /**************************************************
488 * Scheduling class queueing methods:
491 static void __enqueue_sleeper(struct cfs_rq *cfs_rq, struct sched_entity *se)
493 unsigned long load = cfs_rq->load.weight, delta_fair;
496 if (sysctl_sched_features & SCHED_FEAT_SLEEPER_LOAD_AVG)
497 load = rq_of(cfs_rq)->cpu_load[2];
499 delta_fair = se->delta_fair_sleep;
502 * Fix up delta_fair with the effect of us running
503 * during the whole sleep period:
505 if (sysctl_sched_features & SCHED_FEAT_SLEEPER_AVG)
506 delta_fair = div64_likely32((u64)delta_fair * load,
507 load + se->load.weight);
509 if (unlikely(se->load.weight != NICE_0_LOAD))
510 delta_fair = calc_weighted(delta_fair, se->load.weight,
513 prev_runtime = se->wait_runtime;
514 __add_wait_runtime(cfs_rq, se, delta_fair);
515 delta_fair = se->wait_runtime - prev_runtime;
518 * Track the amount of bonus we've given to sleepers:
520 cfs_rq->sleeper_bonus += delta_fair;
521 if (unlikely(cfs_rq->sleeper_bonus > sysctl_sched_runtime_limit))
522 cfs_rq->sleeper_bonus = sysctl_sched_runtime_limit;
524 schedstat_add(cfs_rq, wait_runtime, se->wait_runtime);
527 static void enqueue_sleeper(struct cfs_rq *cfs_rq, struct sched_entity *se)
529 struct task_struct *tsk = task_of(se);
530 unsigned long delta_fair;
532 if ((entity_is_task(se) && tsk->policy == SCHED_BATCH) ||
533 !(sysctl_sched_features & SCHED_FEAT_FAIR_SLEEPERS))
536 delta_fair = (unsigned long)min((u64)(2*sysctl_sched_runtime_limit),
537 (u64)(cfs_rq->fair_clock - se->sleep_start_fair));
539 se->delta_fair_sleep += delta_fair;
540 if (unlikely(abs(se->delta_fair_sleep) >=
541 sysctl_sched_stat_granularity)) {
542 __enqueue_sleeper(cfs_rq, se);
543 se->delta_fair_sleep = 0;
546 se->sleep_start_fair = 0;
548 #ifdef CONFIG_SCHEDSTATS
549 if (se->sleep_start) {
550 u64 delta = rq_of(cfs_rq)->clock - se->sleep_start;
555 if (unlikely(delta > se->sleep_max))
556 se->sleep_max = delta;
559 se->sum_sleep_runtime += delta;
561 if (se->block_start) {
562 u64 delta = rq_of(cfs_rq)->clock - se->block_start;
567 if (unlikely(delta > se->block_max))
568 se->block_max = delta;
571 se->sum_sleep_runtime += delta;
577 enqueue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se, int wakeup)
580 * Update the fair clock.
585 enqueue_sleeper(cfs_rq, se);
587 update_stats_enqueue(cfs_rq, se);
588 __enqueue_entity(cfs_rq, se);
592 dequeue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se, int sleep)
594 update_stats_dequeue(cfs_rq, se);
596 se->sleep_start_fair = cfs_rq->fair_clock;
597 #ifdef CONFIG_SCHEDSTATS
598 if (entity_is_task(se)) {
599 struct task_struct *tsk = task_of(se);
601 if (tsk->state & TASK_INTERRUPTIBLE)
602 se->sleep_start = rq_of(cfs_rq)->clock;
603 if (tsk->state & TASK_UNINTERRUPTIBLE)
604 se->block_start = rq_of(cfs_rq)->clock;
606 cfs_rq->wait_runtime -= se->wait_runtime;
609 __dequeue_entity(cfs_rq, se);
613 * Preempt the current task with a newly woken task if needed:
616 __check_preempt_curr_fair(struct cfs_rq *cfs_rq, struct sched_entity *se,
617 struct sched_entity *curr, unsigned long granularity)
619 s64 __delta = curr->fair_key - se->fair_key;
622 * Take scheduling granularity into account - do not
623 * preempt the current task unless the best task has
624 * a larger than sched_granularity fairness advantage:
626 if (__delta > niced_granularity(curr, granularity))
627 resched_task(rq_of(cfs_rq)->curr);
631 set_next_entity(struct cfs_rq *cfs_rq, struct sched_entity *se)
634 * Any task has to be enqueued before it get to execute on
635 * a CPU. So account for the time it spent waiting on the
636 * runqueue. (note, here we rely on pick_next_task() having
637 * done a put_prev_task_fair() shortly before this, which
638 * updated rq->fair_clock - used by update_stats_wait_end())
640 update_stats_wait_end(cfs_rq, se);
641 update_stats_curr_start(cfs_rq, se);
642 set_cfs_rq_curr(cfs_rq, se);
645 static struct sched_entity *pick_next_entity(struct cfs_rq *cfs_rq)
647 struct sched_entity *se = __pick_next_entity(cfs_rq);
649 set_next_entity(cfs_rq, se);
654 static void put_prev_entity(struct cfs_rq *cfs_rq, struct sched_entity *prev)
657 * If still on the runqueue then deactivate_task()
658 * was not called and update_curr() has to be done:
663 update_stats_curr_end(cfs_rq, prev);
666 update_stats_wait_start(cfs_rq, prev);
667 set_cfs_rq_curr(cfs_rq, NULL);
670 static void entity_tick(struct cfs_rq *cfs_rq, struct sched_entity *curr)
672 struct sched_entity *next;
675 * Dequeue and enqueue the task to update its
676 * position within the tree:
678 dequeue_entity(cfs_rq, curr, 0);
679 enqueue_entity(cfs_rq, curr, 0);
682 * Reschedule if another task tops the current one.
684 next = __pick_next_entity(cfs_rq);
688 __check_preempt_curr_fair(cfs_rq, next, curr, sysctl_sched_granularity);
691 /**************************************************
692 * CFS operations on tasks:
695 #ifdef CONFIG_FAIR_GROUP_SCHED
697 /* Walk up scheduling entities hierarchy */
698 #define for_each_sched_entity(se) \
699 for (; se; se = se->parent)
701 static inline struct cfs_rq *task_cfs_rq(struct task_struct *p)
706 /* runqueue on which this entity is (to be) queued */
707 static inline struct cfs_rq *cfs_rq_of(struct sched_entity *se)
712 /* runqueue "owned" by this group */
713 static inline struct cfs_rq *group_cfs_rq(struct sched_entity *grp)
718 /* Given a group's cfs_rq on one cpu, return its corresponding cfs_rq on
719 * another cpu ('this_cpu')
721 static inline struct cfs_rq *cpu_cfs_rq(struct cfs_rq *cfs_rq, int this_cpu)
723 /* A later patch will take group into account */
724 return &cpu_rq(this_cpu)->cfs;
727 /* Iterate thr' all leaf cfs_rq's on a runqueue */
728 #define for_each_leaf_cfs_rq(rq, cfs_rq) \
729 list_for_each_entry(cfs_rq, &rq->leaf_cfs_rq_list, leaf_cfs_rq_list)
731 /* Do the two (enqueued) tasks belong to the same group ? */
732 static inline int is_same_group(struct task_struct *curr, struct task_struct *p)
734 if (curr->se.cfs_rq == p->se.cfs_rq)
740 #else /* CONFIG_FAIR_GROUP_SCHED */
742 #define for_each_sched_entity(se) \
743 for (; se; se = NULL)
745 static inline struct cfs_rq *task_cfs_rq(struct task_struct *p)
747 return &task_rq(p)->cfs;
750 static inline struct cfs_rq *cfs_rq_of(struct sched_entity *se)
752 struct task_struct *p = task_of(se);
753 struct rq *rq = task_rq(p);
758 /* runqueue "owned" by this group */
759 static inline struct cfs_rq *group_cfs_rq(struct sched_entity *grp)
764 static inline struct cfs_rq *cpu_cfs_rq(struct cfs_rq *cfs_rq, int this_cpu)
766 return &cpu_rq(this_cpu)->cfs;
769 #define for_each_leaf_cfs_rq(rq, cfs_rq) \
770 for (cfs_rq = &rq->cfs; cfs_rq; cfs_rq = NULL)
772 static inline int is_same_group(struct task_struct *curr, struct task_struct *p)
777 #endif /* CONFIG_FAIR_GROUP_SCHED */
780 * The enqueue_task method is called before nr_running is
781 * increased. Here we update the fair scheduling stats and
782 * then put the task into the rbtree:
784 static void enqueue_task_fair(struct rq *rq, struct task_struct *p, int wakeup)
786 struct cfs_rq *cfs_rq;
787 struct sched_entity *se = &p->se;
789 for_each_sched_entity(se) {
792 cfs_rq = cfs_rq_of(se);
793 enqueue_entity(cfs_rq, se, wakeup);
798 * The dequeue_task method is called before nr_running is
799 * decreased. We remove the task from the rbtree and
800 * update the fair scheduling stats:
802 static void dequeue_task_fair(struct rq *rq, struct task_struct *p, int sleep)
804 struct cfs_rq *cfs_rq;
805 struct sched_entity *se = &p->se;
807 for_each_sched_entity(se) {
808 cfs_rq = cfs_rq_of(se);
809 dequeue_entity(cfs_rq, se, sleep);
810 /* Don't dequeue parent if it has other entities besides us */
811 if (cfs_rq->load.weight)
817 * sched_yield() support is very simple - we dequeue and enqueue
819 static void yield_task_fair(struct rq *rq, struct task_struct *p)
821 struct cfs_rq *cfs_rq = task_cfs_rq(p);
823 __update_rq_clock(rq);
825 * Dequeue and enqueue the task to update its
826 * position within the tree:
828 dequeue_entity(cfs_rq, &p->se, 0);
829 enqueue_entity(cfs_rq, &p->se, 0);
833 * Preempt the current task with a newly woken task if needed:
835 static void check_preempt_curr_fair(struct rq *rq, struct task_struct *p)
837 struct task_struct *curr = rq->curr;
838 struct cfs_rq *cfs_rq = task_cfs_rq(curr);
841 if (unlikely(rt_prio(p->prio))) {
848 gran = sysctl_sched_wakeup_granularity;
850 * Batch tasks prefer throughput over latency:
852 if (unlikely(p->policy == SCHED_BATCH))
853 gran = sysctl_sched_batch_wakeup_granularity;
855 if (is_same_group(curr, p))
856 __check_preempt_curr_fair(cfs_rq, &p->se, &curr->se, gran);
859 static struct task_struct *pick_next_task_fair(struct rq *rq)
861 struct cfs_rq *cfs_rq = &rq->cfs;
862 struct sched_entity *se;
864 if (unlikely(!cfs_rq->nr_running))
868 se = pick_next_entity(cfs_rq);
869 cfs_rq = group_cfs_rq(se);
876 * Account for a descheduled task:
878 static void put_prev_task_fair(struct rq *rq, struct task_struct *prev)
880 struct sched_entity *se = &prev->se;
881 struct cfs_rq *cfs_rq;
883 for_each_sched_entity(se) {
884 cfs_rq = cfs_rq_of(se);
885 put_prev_entity(cfs_rq, se);
889 /**************************************************
890 * Fair scheduling class load-balancing methods:
894 * Load-balancing iterator. Note: while the runqueue stays locked
895 * during the whole iteration, the current task might be
896 * dequeued so the iterator has to be dequeue-safe. Here we
897 * achieve that by always pre-iterating before returning
900 static inline struct task_struct *
901 __load_balance_iterator(struct cfs_rq *cfs_rq, struct rb_node *curr)
903 struct task_struct *p;
908 p = rb_entry(curr, struct task_struct, se.run_node);
909 cfs_rq->rb_load_balance_curr = rb_next(curr);
914 static struct task_struct *load_balance_start_fair(void *arg)
916 struct cfs_rq *cfs_rq = arg;
918 return __load_balance_iterator(cfs_rq, first_fair(cfs_rq));
921 static struct task_struct *load_balance_next_fair(void *arg)
923 struct cfs_rq *cfs_rq = arg;
925 return __load_balance_iterator(cfs_rq, cfs_rq->rb_load_balance_curr);
928 #ifdef CONFIG_FAIR_GROUP_SCHED
929 static int cfs_rq_best_prio(struct cfs_rq *cfs_rq)
931 struct sched_entity *curr;
932 struct task_struct *p;
934 if (!cfs_rq->nr_running)
937 curr = __pick_next_entity(cfs_rq);
945 load_balance_fair(struct rq *this_rq, int this_cpu, struct rq *busiest,
946 unsigned long max_nr_move, unsigned long max_load_move,
947 struct sched_domain *sd, enum cpu_idle_type idle,
948 int *all_pinned, int *this_best_prio)
950 struct cfs_rq *busy_cfs_rq;
951 unsigned long load_moved, total_nr_moved = 0, nr_moved;
952 long rem_load_move = max_load_move;
953 struct rq_iterator cfs_rq_iterator;
955 cfs_rq_iterator.start = load_balance_start_fair;
956 cfs_rq_iterator.next = load_balance_next_fair;
958 for_each_leaf_cfs_rq(busiest, busy_cfs_rq) {
959 #ifdef CONFIG_FAIR_GROUP_SCHED
960 struct cfs_rq *this_cfs_rq;
962 unsigned long maxload;
964 this_cfs_rq = cpu_cfs_rq(busy_cfs_rq, this_cpu);
966 imbalance = busy_cfs_rq->load.weight - this_cfs_rq->load.weight;
967 /* Don't pull if this_cfs_rq has more load than busy_cfs_rq */
971 /* Don't pull more than imbalance/2 */
973 maxload = min(rem_load_move, imbalance);
975 *this_best_prio = cfs_rq_best_prio(this_cfs_rq);
977 # define maxload rem_load_move
979 /* pass busy_cfs_rq argument into
980 * load_balance_[start|next]_fair iterators
982 cfs_rq_iterator.arg = busy_cfs_rq;
983 nr_moved = balance_tasks(this_rq, this_cpu, busiest,
984 max_nr_move, maxload, sd, idle, all_pinned,
985 &load_moved, this_best_prio, &cfs_rq_iterator);
987 total_nr_moved += nr_moved;
988 max_nr_move -= nr_moved;
989 rem_load_move -= load_moved;
991 if (max_nr_move <= 0 || rem_load_move <= 0)
995 return max_load_move - rem_load_move;
999 * scheduler tick hitting a task of our scheduling class:
1001 static void task_tick_fair(struct rq *rq, struct task_struct *curr)
1003 struct cfs_rq *cfs_rq;
1004 struct sched_entity *se = &curr->se;
1006 for_each_sched_entity(se) {
1007 cfs_rq = cfs_rq_of(se);
1008 entity_tick(cfs_rq, se);
1013 * Share the fairness runtime between parent and child, thus the
1014 * total amount of pressure for CPU stays equal - new tasks
1015 * get a chance to run but frequent forkers are not allowed to
1016 * monopolize the CPU. Note: the parent runqueue is locked,
1017 * the child is not running yet.
1019 static void task_new_fair(struct rq *rq, struct task_struct *p)
1021 struct cfs_rq *cfs_rq = task_cfs_rq(p);
1022 struct sched_entity *se = &p->se;
1024 sched_info_queued(p);
1026 update_stats_enqueue(cfs_rq, se);
1028 * Child runs first: we let it run before the parent
1029 * until it reschedules once. We set up the key so that
1030 * it will preempt the parent:
1032 p->se.fair_key = current->se.fair_key -
1033 niced_granularity(&rq->curr->se, sysctl_sched_granularity) - 1;
1035 * The first wait is dominated by the child-runs-first logic,
1036 * so do not credit it with that waiting time yet:
1038 if (sysctl_sched_features & SCHED_FEAT_SKIP_INITIAL)
1039 p->se.wait_start_fair = 0;
1042 * The statistical average of wait_runtime is about
1043 * -granularity/2, so initialize the task with that:
1045 if (sysctl_sched_features & SCHED_FEAT_START_DEBIT)
1046 p->se.wait_runtime = -(sysctl_sched_granularity / 2);
1048 __enqueue_entity(cfs_rq, se);
1051 #ifdef CONFIG_FAIR_GROUP_SCHED
1052 /* Account for a task changing its policy or group.
1054 * This routine is mostly called to set cfs_rq->curr field when a task
1055 * migrates between groups/classes.
1057 static void set_curr_task_fair(struct rq *rq)
1059 struct sched_entity *se = &rq->curr->se;
1061 for_each_sched_entity(se)
1062 set_next_entity(cfs_rq_of(se), se);
1065 static void set_curr_task_fair(struct rq *rq)
1071 * All the scheduling class methods:
1073 struct sched_class fair_sched_class __read_mostly = {
1074 .enqueue_task = enqueue_task_fair,
1075 .dequeue_task = dequeue_task_fair,
1076 .yield_task = yield_task_fair,
1078 .check_preempt_curr = check_preempt_curr_fair,
1080 .pick_next_task = pick_next_task_fair,
1081 .put_prev_task = put_prev_task_fair,
1083 .load_balance = load_balance_fair,
1085 .set_curr_task = set_curr_task_fair,
1086 .task_tick = task_tick_fair,
1087 .task_new = task_new_fair,
1090 #ifdef CONFIG_SCHED_DEBUG
1091 static void print_cfs_stats(struct seq_file *m, int cpu)
1093 struct cfs_rq *cfs_rq;
1095 for_each_leaf_cfs_rq(cpu_rq(cpu), cfs_rq)
1096 print_cfs_rq(m, cpu, cfs_rq);