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++;
198 schedstat_add(cfs_rq, wait_runtime, se->wait_runtime);
202 __dequeue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se)
204 if (cfs_rq->rb_leftmost == &se->run_node)
205 cfs_rq->rb_leftmost = rb_next(&se->run_node);
206 rb_erase(&se->run_node, &cfs_rq->tasks_timeline);
207 update_load_sub(&cfs_rq->load, se->load.weight);
208 cfs_rq->nr_running--;
211 schedstat_add(cfs_rq, wait_runtime, -se->wait_runtime);
214 static inline struct rb_node *first_fair(struct cfs_rq *cfs_rq)
216 return cfs_rq->rb_leftmost;
219 static struct sched_entity *__pick_next_entity(struct cfs_rq *cfs_rq)
221 return rb_entry(first_fair(cfs_rq), struct sched_entity, run_node);
224 /**************************************************************
225 * Scheduling class statistics methods:
229 * Calculate the preemption granularity needed to schedule every
230 * runnable task once per sysctl_sched_latency amount of time.
231 * (down to a sensible low limit on granularity)
233 * For example, if there are 2 tasks running and latency is 10 msecs,
234 * we switch tasks every 5 msecs. If we have 3 tasks running, we have
235 * to switch tasks every 3.33 msecs to get a 10 msecs observed latency
236 * for each task. We do finer and finer scheduling up to until we
237 * reach the minimum granularity value.
239 * To achieve this we use the following dynamic-granularity rule:
241 * gran = lat/nr - lat/nr/nr
243 * This comes out of the following equations:
248 * kB2 = kB1 - d + d/nr
251 * Where 'k' is key, 'A' is task A (waiting), 'B' is task B (running),
252 * '1' is start of time, '2' is end of time, 'd' is delay between
253 * 1 and 2 (during which task B was running), 'nr' is number of tasks
254 * running, 'lat' is the the period of each task. ('lat' is the
255 * sched_latency that we aim for.)
258 sched_granularity(struct cfs_rq *cfs_rq)
260 unsigned int gran = sysctl_sched_latency;
261 unsigned int nr = cfs_rq->nr_running;
264 gran = gran/nr - gran/nr/nr;
265 gran = max(gran, sysctl_sched_min_granularity);
272 * We rescale the rescheduling granularity of tasks according to their
273 * nice level, but only linearly, not exponentially:
276 niced_granularity(struct sched_entity *curr, unsigned long granularity)
280 if (likely(curr->load.weight == NICE_0_LOAD))
283 * Positive nice levels get the same granularity as nice-0:
285 if (likely(curr->load.weight < NICE_0_LOAD)) {
286 tmp = curr->load.weight * (u64)granularity;
287 return (long) (tmp >> NICE_0_SHIFT);
290 * Negative nice level tasks get linearly finer
293 tmp = curr->load.inv_weight * (u64)granularity;
296 * It will always fit into 'long':
298 return (long) (tmp >> (WMULT_SHIFT-NICE_0_SHIFT));
302 limit_wait_runtime(struct cfs_rq *cfs_rq, struct sched_entity *se)
304 long limit = sysctl_sched_runtime_limit;
307 * Niced tasks have the same history dynamic range as
310 if (unlikely(se->wait_runtime > limit)) {
311 se->wait_runtime = limit;
312 schedstat_inc(se, wait_runtime_overruns);
313 schedstat_inc(cfs_rq, wait_runtime_overruns);
315 if (unlikely(se->wait_runtime < -limit)) {
316 se->wait_runtime = -limit;
317 schedstat_inc(se, wait_runtime_underruns);
318 schedstat_inc(cfs_rq, wait_runtime_underruns);
323 __add_wait_runtime(struct cfs_rq *cfs_rq, struct sched_entity *se, long delta)
325 se->wait_runtime += delta;
326 schedstat_add(se, sum_wait_runtime, delta);
327 limit_wait_runtime(cfs_rq, se);
331 add_wait_runtime(struct cfs_rq *cfs_rq, struct sched_entity *se, long delta)
333 schedstat_add(cfs_rq, wait_runtime, -se->wait_runtime);
334 __add_wait_runtime(cfs_rq, se, delta);
335 schedstat_add(cfs_rq, wait_runtime, se->wait_runtime);
339 * Update the current task's runtime statistics. Skip current tasks that
340 * are not in our scheduling class.
343 __update_curr(struct cfs_rq *cfs_rq, struct sched_entity *curr)
345 unsigned long delta, delta_exec, delta_fair, delta_mine;
346 struct load_weight *lw = &cfs_rq->load;
347 unsigned long load = lw->weight;
349 delta_exec = curr->delta_exec;
350 schedstat_set(curr->exec_max, max((u64)delta_exec, curr->exec_max));
352 curr->sum_exec_runtime += delta_exec;
353 cfs_rq->exec_clock += delta_exec;
358 delta_fair = calc_delta_fair(delta_exec, lw);
359 delta_mine = calc_delta_mine(delta_exec, curr->load.weight, lw);
361 if (cfs_rq->sleeper_bonus > sysctl_sched_min_granularity) {
362 delta = min((u64)delta_mine, cfs_rq->sleeper_bonus);
363 delta = min(delta, (unsigned long)(
364 (long)sysctl_sched_runtime_limit - curr->wait_runtime));
365 cfs_rq->sleeper_bonus -= delta;
369 cfs_rq->fair_clock += delta_fair;
371 * We executed delta_exec amount of time on the CPU,
372 * but we were only entitled to delta_mine amount of
373 * time during that period (if nr_running == 1 then
374 * the two values are equal)
375 * [Note: delta_mine - delta_exec is negative]:
377 add_wait_runtime(cfs_rq, curr, delta_mine - delta_exec);
380 static void update_curr(struct cfs_rq *cfs_rq)
382 struct sched_entity *curr = cfs_rq_curr(cfs_rq);
383 unsigned long delta_exec;
389 * Get the amount of time the current task was running
390 * since the last time we changed load (this cannot
391 * overflow on 32 bits):
393 delta_exec = (unsigned long)(rq_of(cfs_rq)->clock - curr->exec_start);
395 curr->delta_exec += delta_exec;
397 if (unlikely(curr->delta_exec > sysctl_sched_stat_granularity)) {
398 __update_curr(cfs_rq, curr);
399 curr->delta_exec = 0;
401 curr->exec_start = rq_of(cfs_rq)->clock;
405 update_stats_wait_start(struct cfs_rq *cfs_rq, struct sched_entity *se)
407 se->wait_start_fair = cfs_rq->fair_clock;
408 schedstat_set(se->wait_start, rq_of(cfs_rq)->clock);
412 * We calculate fair deltas here, so protect against the random effects
413 * of a multiplication overflow by capping it to the runtime limit:
415 #if BITS_PER_LONG == 32
416 static inline unsigned long
417 calc_weighted(unsigned long delta, unsigned long weight, int shift)
419 u64 tmp = (u64)delta * weight >> shift;
421 if (unlikely(tmp > sysctl_sched_runtime_limit*2))
422 return sysctl_sched_runtime_limit*2;
426 static inline unsigned long
427 calc_weighted(unsigned long delta, unsigned long weight, int shift)
429 return delta * weight >> shift;
434 * Task is being enqueued - update stats:
436 static void update_stats_enqueue(struct cfs_rq *cfs_rq, struct sched_entity *se)
441 * Are we enqueueing a waiting task? (for current tasks
442 * a dequeue/enqueue event is a NOP)
444 if (se != cfs_rq_curr(cfs_rq))
445 update_stats_wait_start(cfs_rq, se);
449 key = cfs_rq->fair_clock;
452 * Optimize the common nice 0 case:
454 if (likely(se->load.weight == NICE_0_LOAD)) {
455 key -= se->wait_runtime;
459 if (se->wait_runtime < 0) {
460 tmp = -se->wait_runtime;
461 key += (tmp * se->load.inv_weight) >>
462 (WMULT_SHIFT - NICE_0_SHIFT);
464 tmp = se->wait_runtime;
465 key -= (tmp * se->load.inv_weight) >>
466 (WMULT_SHIFT - NICE_0_SHIFT);
474 * Note: must be called with a freshly updated rq->fair_clock.
477 __update_stats_wait_end(struct cfs_rq *cfs_rq, struct sched_entity *se)
479 unsigned long delta_fair = se->delta_fair_run;
481 schedstat_set(se->wait_max, max(se->wait_max,
482 rq_of(cfs_rq)->clock - se->wait_start));
484 if (unlikely(se->load.weight != NICE_0_LOAD))
485 delta_fair = calc_weighted(delta_fair, se->load.weight,
488 add_wait_runtime(cfs_rq, se, delta_fair);
492 update_stats_wait_end(struct cfs_rq *cfs_rq, struct sched_entity *se)
494 unsigned long delta_fair;
496 if (unlikely(!se->wait_start_fair))
499 delta_fair = (unsigned long)min((u64)(2*sysctl_sched_runtime_limit),
500 (u64)(cfs_rq->fair_clock - se->wait_start_fair));
502 se->delta_fair_run += delta_fair;
503 if (unlikely(abs(se->delta_fair_run) >=
504 sysctl_sched_stat_granularity)) {
505 __update_stats_wait_end(cfs_rq, se);
506 se->delta_fair_run = 0;
509 se->wait_start_fair = 0;
510 schedstat_set(se->wait_start, 0);
514 update_stats_dequeue(struct cfs_rq *cfs_rq, struct sched_entity *se)
518 * Mark the end of the wait period if dequeueing a
521 if (se != cfs_rq_curr(cfs_rq))
522 update_stats_wait_end(cfs_rq, se);
526 * We are picking a new current task - update its stats:
529 update_stats_curr_start(struct cfs_rq *cfs_rq, struct sched_entity *se)
532 * We are starting a new run period:
534 se->exec_start = rq_of(cfs_rq)->clock;
538 * We are descheduling a task - update its stats:
541 update_stats_curr_end(struct cfs_rq *cfs_rq, struct sched_entity *se)
546 /**************************************************
547 * Scheduling class queueing methods:
550 static void __enqueue_sleeper(struct cfs_rq *cfs_rq, struct sched_entity *se)
552 unsigned long load = cfs_rq->load.weight, delta_fair;
556 * Do not boost sleepers if there's too much bonus 'in flight'
559 if (unlikely(cfs_rq->sleeper_bonus > sysctl_sched_runtime_limit))
562 if (sysctl_sched_features & SCHED_FEAT_SLEEPER_LOAD_AVG)
563 load = rq_of(cfs_rq)->cpu_load[2];
565 delta_fair = se->delta_fair_sleep;
568 * Fix up delta_fair with the effect of us running
569 * during the whole sleep period:
571 if (sysctl_sched_features & SCHED_FEAT_SLEEPER_AVG)
572 delta_fair = div64_likely32((u64)delta_fair * load,
573 load + se->load.weight);
575 if (unlikely(se->load.weight != NICE_0_LOAD))
576 delta_fair = calc_weighted(delta_fair, se->load.weight,
579 prev_runtime = se->wait_runtime;
580 __add_wait_runtime(cfs_rq, se, delta_fair);
581 delta_fair = se->wait_runtime - prev_runtime;
584 * Track the amount of bonus we've given to sleepers:
586 cfs_rq->sleeper_bonus += delta_fair;
589 static void enqueue_sleeper(struct cfs_rq *cfs_rq, struct sched_entity *se)
591 struct task_struct *tsk = task_of(se);
592 unsigned long delta_fair;
594 if ((entity_is_task(se) && tsk->policy == SCHED_BATCH) ||
595 !(sysctl_sched_features & SCHED_FEAT_FAIR_SLEEPERS))
598 delta_fair = (unsigned long)min((u64)(2*sysctl_sched_runtime_limit),
599 (u64)(cfs_rq->fair_clock - se->sleep_start_fair));
601 se->delta_fair_sleep += delta_fair;
602 if (unlikely(abs(se->delta_fair_sleep) >=
603 sysctl_sched_stat_granularity)) {
604 __enqueue_sleeper(cfs_rq, se);
605 se->delta_fair_sleep = 0;
608 se->sleep_start_fair = 0;
610 #ifdef CONFIG_SCHEDSTATS
611 if (se->sleep_start) {
612 u64 delta = rq_of(cfs_rq)->clock - se->sleep_start;
617 if (unlikely(delta > se->sleep_max))
618 se->sleep_max = delta;
621 se->sum_sleep_runtime += delta;
623 if (se->block_start) {
624 u64 delta = rq_of(cfs_rq)->clock - se->block_start;
629 if (unlikely(delta > se->block_max))
630 se->block_max = delta;
633 se->sum_sleep_runtime += delta;
639 enqueue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se, int wakeup)
642 * Update the fair clock.
647 enqueue_sleeper(cfs_rq, se);
649 update_stats_enqueue(cfs_rq, se);
650 __enqueue_entity(cfs_rq, se);
654 dequeue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se, int sleep)
656 update_stats_dequeue(cfs_rq, se);
658 se->sleep_start_fair = cfs_rq->fair_clock;
659 #ifdef CONFIG_SCHEDSTATS
660 if (entity_is_task(se)) {
661 struct task_struct *tsk = task_of(se);
663 if (tsk->state & TASK_INTERRUPTIBLE)
664 se->sleep_start = rq_of(cfs_rq)->clock;
665 if (tsk->state & TASK_UNINTERRUPTIBLE)
666 se->block_start = rq_of(cfs_rq)->clock;
670 __dequeue_entity(cfs_rq, se);
674 * Preempt the current task with a newly woken task if needed:
677 __check_preempt_curr_fair(struct cfs_rq *cfs_rq, struct sched_entity *se,
678 struct sched_entity *curr, unsigned long granularity)
680 s64 __delta = curr->fair_key - se->fair_key;
683 * Take scheduling granularity into account - do not
684 * preempt the current task unless the best task has
685 * a larger than sched_granularity fairness advantage:
687 if (__delta > niced_granularity(curr, granularity)) {
688 resched_task(rq_of(cfs_rq)->curr);
689 curr->prev_sum_exec_runtime = curr->sum_exec_runtime;
694 set_next_entity(struct cfs_rq *cfs_rq, struct sched_entity *se)
697 * Any task has to be enqueued before it get to execute on
698 * a CPU. So account for the time it spent waiting on the
699 * runqueue. (note, here we rely on pick_next_task() having
700 * done a put_prev_task_fair() shortly before this, which
701 * updated rq->fair_clock - used by update_stats_wait_end())
703 update_stats_wait_end(cfs_rq, se);
704 update_stats_curr_start(cfs_rq, se);
705 set_cfs_rq_curr(cfs_rq, se);
708 static struct sched_entity *pick_next_entity(struct cfs_rq *cfs_rq)
710 struct sched_entity *se = __pick_next_entity(cfs_rq);
712 set_next_entity(cfs_rq, se);
717 static void put_prev_entity(struct cfs_rq *cfs_rq, struct sched_entity *prev)
720 * If still on the runqueue then deactivate_task()
721 * was not called and update_curr() has to be done:
726 update_stats_curr_end(cfs_rq, prev);
729 update_stats_wait_start(cfs_rq, prev);
730 set_cfs_rq_curr(cfs_rq, NULL);
733 static void entity_tick(struct cfs_rq *cfs_rq, struct sched_entity *curr)
735 unsigned long gran, ideal_runtime, delta_exec;
736 struct sched_entity *next;
739 * Dequeue and enqueue the task to update its
740 * position within the tree:
742 dequeue_entity(cfs_rq, curr, 0);
743 enqueue_entity(cfs_rq, curr, 0);
746 * Reschedule if another task tops the current one.
748 next = __pick_next_entity(cfs_rq);
752 gran = sched_granularity(cfs_rq);
753 ideal_runtime = niced_granularity(curr,
754 max(sysctl_sched_latency / cfs_rq->nr_running,
755 (unsigned long)sysctl_sched_min_granularity));
757 * If we executed more than what the latency constraint suggests,
758 * reduce the rescheduling granularity. This way the total latency
759 * of how much a task is not scheduled converges to
760 * sysctl_sched_latency:
762 delta_exec = curr->sum_exec_runtime - curr->prev_sum_exec_runtime;
763 if (delta_exec > ideal_runtime)
766 __check_preempt_curr_fair(cfs_rq, next, curr, gran);
769 /**************************************************
770 * CFS operations on tasks:
773 #ifdef CONFIG_FAIR_GROUP_SCHED
775 /* Walk up scheduling entities hierarchy */
776 #define for_each_sched_entity(se) \
777 for (; se; se = se->parent)
779 static inline struct cfs_rq *task_cfs_rq(struct task_struct *p)
784 /* runqueue on which this entity is (to be) queued */
785 static inline struct cfs_rq *cfs_rq_of(struct sched_entity *se)
790 /* runqueue "owned" by this group */
791 static inline struct cfs_rq *group_cfs_rq(struct sched_entity *grp)
796 /* Given a group's cfs_rq on one cpu, return its corresponding cfs_rq on
797 * another cpu ('this_cpu')
799 static inline struct cfs_rq *cpu_cfs_rq(struct cfs_rq *cfs_rq, int this_cpu)
801 /* A later patch will take group into account */
802 return &cpu_rq(this_cpu)->cfs;
805 /* Iterate thr' all leaf cfs_rq's on a runqueue */
806 #define for_each_leaf_cfs_rq(rq, cfs_rq) \
807 list_for_each_entry(cfs_rq, &rq->leaf_cfs_rq_list, leaf_cfs_rq_list)
809 /* Do the two (enqueued) tasks belong to the same group ? */
810 static inline int is_same_group(struct task_struct *curr, struct task_struct *p)
812 if (curr->se.cfs_rq == p->se.cfs_rq)
818 #else /* CONFIG_FAIR_GROUP_SCHED */
820 #define for_each_sched_entity(se) \
821 for (; se; se = NULL)
823 static inline struct cfs_rq *task_cfs_rq(struct task_struct *p)
825 return &task_rq(p)->cfs;
828 static inline struct cfs_rq *cfs_rq_of(struct sched_entity *se)
830 struct task_struct *p = task_of(se);
831 struct rq *rq = task_rq(p);
836 /* runqueue "owned" by this group */
837 static inline struct cfs_rq *group_cfs_rq(struct sched_entity *grp)
842 static inline struct cfs_rq *cpu_cfs_rq(struct cfs_rq *cfs_rq, int this_cpu)
844 return &cpu_rq(this_cpu)->cfs;
847 #define for_each_leaf_cfs_rq(rq, cfs_rq) \
848 for (cfs_rq = &rq->cfs; cfs_rq; cfs_rq = NULL)
850 static inline int is_same_group(struct task_struct *curr, struct task_struct *p)
855 #endif /* CONFIG_FAIR_GROUP_SCHED */
858 * The enqueue_task method is called before nr_running is
859 * increased. Here we update the fair scheduling stats and
860 * then put the task into the rbtree:
862 static void enqueue_task_fair(struct rq *rq, struct task_struct *p, int wakeup)
864 struct cfs_rq *cfs_rq;
865 struct sched_entity *se = &p->se;
867 for_each_sched_entity(se) {
870 cfs_rq = cfs_rq_of(se);
871 enqueue_entity(cfs_rq, se, wakeup);
876 * The dequeue_task method is called before nr_running is
877 * decreased. We remove the task from the rbtree and
878 * update the fair scheduling stats:
880 static void dequeue_task_fair(struct rq *rq, struct task_struct *p, int sleep)
882 struct cfs_rq *cfs_rq;
883 struct sched_entity *se = &p->se;
885 for_each_sched_entity(se) {
886 cfs_rq = cfs_rq_of(se);
887 dequeue_entity(cfs_rq, se, sleep);
888 /* Don't dequeue parent if it has other entities besides us */
889 if (cfs_rq->load.weight)
895 * sched_yield() support is very simple - we dequeue and enqueue
897 static void yield_task_fair(struct rq *rq, struct task_struct *p)
899 struct cfs_rq *cfs_rq = task_cfs_rq(p);
901 __update_rq_clock(rq);
903 * Dequeue and enqueue the task to update its
904 * position within the tree:
906 dequeue_entity(cfs_rq, &p->se, 0);
907 enqueue_entity(cfs_rq, &p->se, 0);
911 * Preempt the current task with a newly woken task if needed:
913 static void check_preempt_curr_fair(struct rq *rq, struct task_struct *p)
915 struct task_struct *curr = rq->curr;
916 struct cfs_rq *cfs_rq = task_cfs_rq(curr);
919 if (unlikely(rt_prio(p->prio))) {
926 gran = sysctl_sched_wakeup_granularity;
928 * Batch tasks prefer throughput over latency:
930 if (unlikely(p->policy == SCHED_BATCH))
931 gran = sysctl_sched_batch_wakeup_granularity;
933 if (is_same_group(curr, p))
934 __check_preempt_curr_fair(cfs_rq, &p->se, &curr->se, gran);
937 static struct task_struct *pick_next_task_fair(struct rq *rq)
939 struct cfs_rq *cfs_rq = &rq->cfs;
940 struct sched_entity *se;
942 if (unlikely(!cfs_rq->nr_running))
946 se = pick_next_entity(cfs_rq);
947 cfs_rq = group_cfs_rq(se);
954 * Account for a descheduled task:
956 static void put_prev_task_fair(struct rq *rq, struct task_struct *prev)
958 struct sched_entity *se = &prev->se;
959 struct cfs_rq *cfs_rq;
961 for_each_sched_entity(se) {
962 cfs_rq = cfs_rq_of(se);
963 put_prev_entity(cfs_rq, se);
967 /**************************************************
968 * Fair scheduling class load-balancing methods:
972 * Load-balancing iterator. Note: while the runqueue stays locked
973 * during the whole iteration, the current task might be
974 * dequeued so the iterator has to be dequeue-safe. Here we
975 * achieve that by always pre-iterating before returning
978 static inline struct task_struct *
979 __load_balance_iterator(struct cfs_rq *cfs_rq, struct rb_node *curr)
981 struct task_struct *p;
986 p = rb_entry(curr, struct task_struct, se.run_node);
987 cfs_rq->rb_load_balance_curr = rb_next(curr);
992 static struct task_struct *load_balance_start_fair(void *arg)
994 struct cfs_rq *cfs_rq = arg;
996 return __load_balance_iterator(cfs_rq, first_fair(cfs_rq));
999 static struct task_struct *load_balance_next_fair(void *arg)
1001 struct cfs_rq *cfs_rq = arg;
1003 return __load_balance_iterator(cfs_rq, cfs_rq->rb_load_balance_curr);
1006 #ifdef CONFIG_FAIR_GROUP_SCHED
1007 static int cfs_rq_best_prio(struct cfs_rq *cfs_rq)
1009 struct sched_entity *curr;
1010 struct task_struct *p;
1012 if (!cfs_rq->nr_running)
1015 curr = __pick_next_entity(cfs_rq);
1022 static unsigned long
1023 load_balance_fair(struct rq *this_rq, int this_cpu, struct rq *busiest,
1024 unsigned long max_nr_move, unsigned long max_load_move,
1025 struct sched_domain *sd, enum cpu_idle_type idle,
1026 int *all_pinned, int *this_best_prio)
1028 struct cfs_rq *busy_cfs_rq;
1029 unsigned long load_moved, total_nr_moved = 0, nr_moved;
1030 long rem_load_move = max_load_move;
1031 struct rq_iterator cfs_rq_iterator;
1033 cfs_rq_iterator.start = load_balance_start_fair;
1034 cfs_rq_iterator.next = load_balance_next_fair;
1036 for_each_leaf_cfs_rq(busiest, busy_cfs_rq) {
1037 #ifdef CONFIG_FAIR_GROUP_SCHED
1038 struct cfs_rq *this_cfs_rq;
1040 unsigned long maxload;
1042 this_cfs_rq = cpu_cfs_rq(busy_cfs_rq, this_cpu);
1044 imbalance = busy_cfs_rq->load.weight - this_cfs_rq->load.weight;
1045 /* Don't pull if this_cfs_rq has more load than busy_cfs_rq */
1049 /* Don't pull more than imbalance/2 */
1051 maxload = min(rem_load_move, imbalance);
1053 *this_best_prio = cfs_rq_best_prio(this_cfs_rq);
1055 # define maxload rem_load_move
1057 /* pass busy_cfs_rq argument into
1058 * load_balance_[start|next]_fair iterators
1060 cfs_rq_iterator.arg = busy_cfs_rq;
1061 nr_moved = balance_tasks(this_rq, this_cpu, busiest,
1062 max_nr_move, maxload, sd, idle, all_pinned,
1063 &load_moved, this_best_prio, &cfs_rq_iterator);
1065 total_nr_moved += nr_moved;
1066 max_nr_move -= nr_moved;
1067 rem_load_move -= load_moved;
1069 if (max_nr_move <= 0 || rem_load_move <= 0)
1073 return max_load_move - rem_load_move;
1077 * scheduler tick hitting a task of our scheduling class:
1079 static void task_tick_fair(struct rq *rq, struct task_struct *curr)
1081 struct cfs_rq *cfs_rq;
1082 struct sched_entity *se = &curr->se;
1084 for_each_sched_entity(se) {
1085 cfs_rq = cfs_rq_of(se);
1086 entity_tick(cfs_rq, se);
1091 * Share the fairness runtime between parent and child, thus the
1092 * total amount of pressure for CPU stays equal - new tasks
1093 * get a chance to run but frequent forkers are not allowed to
1094 * monopolize the CPU. Note: the parent runqueue is locked,
1095 * the child is not running yet.
1097 static void task_new_fair(struct rq *rq, struct task_struct *p)
1099 struct cfs_rq *cfs_rq = task_cfs_rq(p);
1100 struct sched_entity *se = &p->se, *curr = cfs_rq_curr(cfs_rq);
1102 sched_info_queued(p);
1104 update_curr(cfs_rq);
1105 update_stats_enqueue(cfs_rq, se);
1107 * Child runs first: we let it run before the parent
1108 * until it reschedules once. We set up the key so that
1109 * it will preempt the parent:
1111 se->fair_key = curr->fair_key -
1112 niced_granularity(curr, sched_granularity(cfs_rq)) - 1;
1114 * The first wait is dominated by the child-runs-first logic,
1115 * so do not credit it with that waiting time yet:
1117 if (sysctl_sched_features & SCHED_FEAT_SKIP_INITIAL)
1118 se->wait_start_fair = 0;
1121 * The statistical average of wait_runtime is about
1122 * -granularity/2, so initialize the task with that:
1124 if (sysctl_sched_features & SCHED_FEAT_START_DEBIT)
1125 se->wait_runtime = -(sched_granularity(cfs_rq) / 2);
1127 __enqueue_entity(cfs_rq, se);
1130 #ifdef CONFIG_FAIR_GROUP_SCHED
1131 /* Account for a task changing its policy or group.
1133 * This routine is mostly called to set cfs_rq->curr field when a task
1134 * migrates between groups/classes.
1136 static void set_curr_task_fair(struct rq *rq)
1138 struct sched_entity *se = &rq->curr->se;
1140 for_each_sched_entity(se)
1141 set_next_entity(cfs_rq_of(se), se);
1144 static void set_curr_task_fair(struct rq *rq)
1150 * All the scheduling class methods:
1152 struct sched_class fair_sched_class __read_mostly = {
1153 .enqueue_task = enqueue_task_fair,
1154 .dequeue_task = dequeue_task_fair,
1155 .yield_task = yield_task_fair,
1157 .check_preempt_curr = check_preempt_curr_fair,
1159 .pick_next_task = pick_next_task_fair,
1160 .put_prev_task = put_prev_task_fair,
1162 .load_balance = load_balance_fair,
1164 .set_curr_task = set_curr_task_fair,
1165 .task_tick = task_tick_fair,
1166 .task_new = task_new_fair,
1169 #ifdef CONFIG_SCHED_DEBUG
1170 static void print_cfs_stats(struct seq_file *m, int cpu)
1172 struct cfs_rq *cfs_rq;
1174 for_each_leaf_cfs_rq(cpu_rq(cpu), cfs_rq)
1175 print_cfs_rq(m, cpu, cfs_rq);