blob: 455106d318a8ae5c87e7a56c19c8e8c05ced83cd [file] [log] [blame]
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001/*
2 * Completely Fair Scheduling (CFS) Class (SCHED_NORMAL/SCHED_BATCH)
3 *
4 * Copyright (C) 2007 Red Hat, Inc., Ingo Molnar <mingo@redhat.com>
5 *
6 * Interactivity improvements by Mike Galbraith
7 * (C) 2007 Mike Galbraith <efault@gmx.de>
8 *
9 * Various enhancements by Dmitry Adamushko.
10 * (C) 2007 Dmitry Adamushko <dmitry.adamushko@gmail.com>
11 *
12 * Group scheduling enhancements by Srivatsa Vaddagiri
13 * Copyright IBM Corporation, 2007
14 * Author: Srivatsa Vaddagiri <vatsa@linux.vnet.ibm.com>
15 *
16 * Scaled math optimizations by Thomas Gleixner
17 * Copyright (C) 2007, Thomas Gleixner <tglx@linutronix.de>
Peter Zijlstra21805082007-08-25 18:41:53 +020018 *
19 * Adaptive scheduling granularity, math enhancements by Peter Zijlstra
20 * Copyright (C) 2007 Red Hat, Inc., Peter Zijlstra <pzijlstr@redhat.com>
Ingo Molnarbf0f6f22007-07-09 18:51:58 +020021 */
22
Arjan van de Ven97455122008-01-25 21:08:34 +010023#include <linux/latencytop.h>
Christian Ehrhardt1983a922009-11-30 12:16:47 +010024#include <linux/sched.h>
Arjan van de Ven97455122008-01-25 21:08:34 +010025
Ingo Molnarbf0f6f22007-07-09 18:51:58 +020026/*
Peter Zijlstra21805082007-08-25 18:41:53 +020027 * Targeted preemption latency for CPU-bound tasks:
Mike Galbraith172e0822009-09-09 15:41:37 +020028 * (default: 5ms * (1 + ilog(ncpus)), units: nanoseconds)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +020029 *
Peter Zijlstra21805082007-08-25 18:41:53 +020030 * NOTE: this latency value is not the same as the concept of
Ingo Molnard274a4c2007-10-15 17:00:14 +020031 * 'timeslice length' - timeslices in CFS are of variable length
32 * and have no persistent notion like in traditional, time-slice
33 * based scheduling concepts.
Ingo Molnarbf0f6f22007-07-09 18:51:58 +020034 *
Ingo Molnard274a4c2007-10-15 17:00:14 +020035 * (to see the precise effective timeslice length of your workload,
36 * run vmstat and monitor the context-switches (cs) field)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +020037 */
Mike Galbraith172e0822009-09-09 15:41:37 +020038unsigned int sysctl_sched_latency = 5000000ULL;
Christian Ehrhardt0bcdcf22009-11-30 12:16:46 +010039unsigned int normalized_sysctl_sched_latency = 5000000ULL;
Ingo Molnar2bd8e6d2007-10-15 17:00:02 +020040
41/*
Christian Ehrhardt1983a922009-11-30 12:16:47 +010042 * The initial- and re-scaling of tunables is configurable
43 * (default SCHED_TUNABLESCALING_LOG = *(1+ilog(ncpus))
44 *
45 * Options are:
46 * SCHED_TUNABLESCALING_NONE - unscaled, always *1
47 * SCHED_TUNABLESCALING_LOG - scaled logarithmical, *1+ilog(ncpus)
48 * SCHED_TUNABLESCALING_LINEAR - scaled linear, *ncpus
49 */
50enum sched_tunable_scaling sysctl_sched_tunable_scaling
51 = SCHED_TUNABLESCALING_LOG;
52
53/*
Peter Zijlstrab2be5e92007-11-09 22:39:37 +010054 * Minimal preemption granularity for CPU-bound tasks:
Mike Galbraith172e0822009-09-09 15:41:37 +020055 * (default: 1 msec * (1 + ilog(ncpus)), units: nanoseconds)
Peter Zijlstrab2be5e92007-11-09 22:39:37 +010056 */
Mike Galbraith172e0822009-09-09 15:41:37 +020057unsigned int sysctl_sched_min_granularity = 1000000ULL;
Christian Ehrhardt0bcdcf22009-11-30 12:16:46 +010058unsigned int normalized_sysctl_sched_min_granularity = 1000000ULL;
Peter Zijlstrab2be5e92007-11-09 22:39:37 +010059
60/*
61 * is kept at sysctl_sched_latency / sysctl_sched_min_granularity
62 */
Zou Nan hai722aab02007-11-26 21:21:49 +010063static unsigned int sched_nr_latency = 5;
Peter Zijlstrab2be5e92007-11-09 22:39:37 +010064
65/*
Mike Galbraith2bba22c2009-09-09 15:41:37 +020066 * After fork, child runs first. If set to 0 (default) then
Ingo Molnar2bd8e6d2007-10-15 17:00:02 +020067 * parent will (try to) run first.
68 */
Mike Galbraith2bba22c2009-09-09 15:41:37 +020069unsigned int sysctl_sched_child_runs_first __read_mostly;
Peter Zijlstra21805082007-08-25 18:41:53 +020070
71/*
Ingo Molnar1799e352007-09-19 23:34:46 +020072 * sys_sched_yield() compat mode
73 *
74 * This option switches the agressive yield implementation of the
75 * old scheduler back on.
76 */
77unsigned int __read_mostly sysctl_sched_compat_yield;
78
79/*
Ingo Molnarbf0f6f22007-07-09 18:51:58 +020080 * SCHED_OTHER wake-up granularity.
Mike Galbraith172e0822009-09-09 15:41:37 +020081 * (default: 1 msec * (1 + ilog(ncpus)), units: nanoseconds)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +020082 *
83 * This option delays the preemption effects of decoupled workloads
84 * and reduces their over-scheduling. Synchronous workloads will still
85 * have immediate wakeup/sleep latencies.
86 */
Mike Galbraith172e0822009-09-09 15:41:37 +020087unsigned int sysctl_sched_wakeup_granularity = 1000000UL;
Christian Ehrhardt0bcdcf22009-11-30 12:16:46 +010088unsigned int normalized_sysctl_sched_wakeup_granularity = 1000000UL;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +020089
Ingo Molnarda84d962007-10-15 17:00:18 +020090const_debug unsigned int sysctl_sched_migration_cost = 500000UL;
91
Peter Zijlstraa4c2f002008-10-17 19:27:03 +020092static const struct sched_class fair_sched_class;
93
Ingo Molnarbf0f6f22007-07-09 18:51:58 +020094/**************************************************************
95 * CFS operations on generic schedulable entities:
96 */
97
98#ifdef CONFIG_FAIR_GROUP_SCHED
99
100/* cpu runqueue to which this cfs_rq is attached */
101static inline struct rq *rq_of(struct cfs_rq *cfs_rq)
102{
103 return cfs_rq->rq;
104}
105
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200106/* An entity is a task if it doesn't "own" a runqueue */
107#define entity_is_task(se) (!se->my_q)
108
Peter Zijlstra8f488942009-07-24 12:25:30 +0200109static inline struct task_struct *task_of(struct sched_entity *se)
110{
111#ifdef CONFIG_SCHED_DEBUG
112 WARN_ON_ONCE(!entity_is_task(se));
113#endif
114 return container_of(se, struct task_struct, se);
115}
116
Peter Zijlstrab7581492008-04-19 19:45:00 +0200117/* Walk up scheduling entities hierarchy */
118#define for_each_sched_entity(se) \
119 for (; se; se = se->parent)
120
121static inline struct cfs_rq *task_cfs_rq(struct task_struct *p)
122{
123 return p->se.cfs_rq;
124}
125
126/* runqueue on which this entity is (to be) queued */
127static inline struct cfs_rq *cfs_rq_of(struct sched_entity *se)
128{
129 return se->cfs_rq;
130}
131
132/* runqueue "owned" by this group */
133static inline struct cfs_rq *group_cfs_rq(struct sched_entity *grp)
134{
135 return grp->my_q;
136}
137
138/* Given a group's cfs_rq on one cpu, return its corresponding cfs_rq on
139 * another cpu ('this_cpu')
140 */
141static inline struct cfs_rq *cpu_cfs_rq(struct cfs_rq *cfs_rq, int this_cpu)
142{
143 return cfs_rq->tg->cfs_rq[this_cpu];
144}
145
146/* Iterate thr' all leaf cfs_rq's on a runqueue */
147#define for_each_leaf_cfs_rq(rq, cfs_rq) \
148 list_for_each_entry_rcu(cfs_rq, &rq->leaf_cfs_rq_list, leaf_cfs_rq_list)
149
150/* Do the two (enqueued) entities belong to the same group ? */
151static inline int
152is_same_group(struct sched_entity *se, struct sched_entity *pse)
153{
154 if (se->cfs_rq == pse->cfs_rq)
155 return 1;
156
157 return 0;
158}
159
160static inline struct sched_entity *parent_entity(struct sched_entity *se)
161{
162 return se->parent;
163}
164
Peter Zijlstra464b7522008-10-24 11:06:15 +0200165/* return depth at which a sched entity is present in the hierarchy */
166static inline int depth_se(struct sched_entity *se)
167{
168 int depth = 0;
169
170 for_each_sched_entity(se)
171 depth++;
172
173 return depth;
174}
175
176static void
177find_matching_se(struct sched_entity **se, struct sched_entity **pse)
178{
179 int se_depth, pse_depth;
180
181 /*
182 * preemption test can be made between sibling entities who are in the
183 * same cfs_rq i.e who have a common parent. Walk up the hierarchy of
184 * both tasks until we find their ancestors who are siblings of common
185 * parent.
186 */
187
188 /* First walk up until both entities are at same depth */
189 se_depth = depth_se(*se);
190 pse_depth = depth_se(*pse);
191
192 while (se_depth > pse_depth) {
193 se_depth--;
194 *se = parent_entity(*se);
195 }
196
197 while (pse_depth > se_depth) {
198 pse_depth--;
199 *pse = parent_entity(*pse);
200 }
201
202 while (!is_same_group(*se, *pse)) {
203 *se = parent_entity(*se);
204 *pse = parent_entity(*pse);
205 }
206}
207
Peter Zijlstra8f488942009-07-24 12:25:30 +0200208#else /* !CONFIG_FAIR_GROUP_SCHED */
209
210static inline struct task_struct *task_of(struct sched_entity *se)
211{
212 return container_of(se, struct task_struct, se);
213}
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200214
215static inline struct rq *rq_of(struct cfs_rq *cfs_rq)
216{
217 return container_of(cfs_rq, struct rq, cfs);
218}
219
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200220#define entity_is_task(se) 1
221
Peter Zijlstrab7581492008-04-19 19:45:00 +0200222#define for_each_sched_entity(se) \
223 for (; se; se = NULL)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200224
Peter Zijlstrab7581492008-04-19 19:45:00 +0200225static inline struct cfs_rq *task_cfs_rq(struct task_struct *p)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200226{
Peter Zijlstrab7581492008-04-19 19:45:00 +0200227 return &task_rq(p)->cfs;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200228}
229
Peter Zijlstrab7581492008-04-19 19:45:00 +0200230static inline struct cfs_rq *cfs_rq_of(struct sched_entity *se)
231{
232 struct task_struct *p = task_of(se);
233 struct rq *rq = task_rq(p);
234
235 return &rq->cfs;
236}
237
238/* runqueue "owned" by this group */
239static inline struct cfs_rq *group_cfs_rq(struct sched_entity *grp)
240{
241 return NULL;
242}
243
244static inline struct cfs_rq *cpu_cfs_rq(struct cfs_rq *cfs_rq, int this_cpu)
245{
246 return &cpu_rq(this_cpu)->cfs;
247}
248
249#define for_each_leaf_cfs_rq(rq, cfs_rq) \
250 for (cfs_rq = &rq->cfs; cfs_rq; cfs_rq = NULL)
251
252static inline int
253is_same_group(struct sched_entity *se, struct sched_entity *pse)
254{
255 return 1;
256}
257
258static inline struct sched_entity *parent_entity(struct sched_entity *se)
259{
260 return NULL;
261}
262
Peter Zijlstra464b7522008-10-24 11:06:15 +0200263static inline void
264find_matching_se(struct sched_entity **se, struct sched_entity **pse)
265{
266}
267
Peter Zijlstrab7581492008-04-19 19:45:00 +0200268#endif /* CONFIG_FAIR_GROUP_SCHED */
269
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200270
271/**************************************************************
272 * Scheduling class tree data structure manipulation methods:
273 */
274
Ingo Molnar0702e3e2007-10-15 17:00:14 +0200275static inline u64 max_vruntime(u64 min_vruntime, u64 vruntime)
Peter Zijlstra02e04312007-10-15 17:00:07 +0200276{
Peter Zijlstra368059a2007-10-15 17:00:11 +0200277 s64 delta = (s64)(vruntime - min_vruntime);
278 if (delta > 0)
Peter Zijlstra02e04312007-10-15 17:00:07 +0200279 min_vruntime = vruntime;
280
281 return min_vruntime;
282}
283
Ingo Molnar0702e3e2007-10-15 17:00:14 +0200284static inline u64 min_vruntime(u64 min_vruntime, u64 vruntime)
Peter Zijlstrab0ffd242007-10-15 17:00:12 +0200285{
286 s64 delta = (s64)(vruntime - min_vruntime);
287 if (delta < 0)
288 min_vruntime = vruntime;
289
290 return min_vruntime;
291}
292
Fabio Checconi54fdc582009-07-16 12:32:27 +0200293static inline int entity_before(struct sched_entity *a,
294 struct sched_entity *b)
295{
296 return (s64)(a->vruntime - b->vruntime) < 0;
297}
298
Ingo Molnar0702e3e2007-10-15 17:00:14 +0200299static inline s64 entity_key(struct cfs_rq *cfs_rq, struct sched_entity *se)
Peter Zijlstra9014623c2007-10-15 17:00:05 +0200300{
Dmitry Adamushko30cfdcf2007-10-15 17:00:07 +0200301 return se->vruntime - cfs_rq->min_vruntime;
Peter Zijlstra9014623c2007-10-15 17:00:05 +0200302}
303
Peter Zijlstra1af5f732008-10-24 11:06:13 +0200304static void update_min_vruntime(struct cfs_rq *cfs_rq)
305{
306 u64 vruntime = cfs_rq->min_vruntime;
307
308 if (cfs_rq->curr)
309 vruntime = cfs_rq->curr->vruntime;
310
311 if (cfs_rq->rb_leftmost) {
312 struct sched_entity *se = rb_entry(cfs_rq->rb_leftmost,
313 struct sched_entity,
314 run_node);
315
Peter Zijlstrae17036d2009-01-15 14:53:39 +0100316 if (!cfs_rq->curr)
Peter Zijlstra1af5f732008-10-24 11:06:13 +0200317 vruntime = se->vruntime;
318 else
319 vruntime = min_vruntime(vruntime, se->vruntime);
320 }
321
322 cfs_rq->min_vruntime = max_vruntime(cfs_rq->min_vruntime, vruntime);
323}
324
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200325/*
326 * Enqueue an entity into the rb-tree:
327 */
Ingo Molnar0702e3e2007-10-15 17:00:14 +0200328static void __enqueue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200329{
330 struct rb_node **link = &cfs_rq->tasks_timeline.rb_node;
331 struct rb_node *parent = NULL;
332 struct sched_entity *entry;
Peter Zijlstra9014623c2007-10-15 17:00:05 +0200333 s64 key = entity_key(cfs_rq, se);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200334 int leftmost = 1;
335
336 /*
337 * Find the right place in the rbtree:
338 */
339 while (*link) {
340 parent = *link;
341 entry = rb_entry(parent, struct sched_entity, run_node);
342 /*
343 * We dont care about collisions. Nodes with
344 * the same key stay together.
345 */
Peter Zijlstra9014623c2007-10-15 17:00:05 +0200346 if (key < entity_key(cfs_rq, entry)) {
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200347 link = &parent->rb_left;
348 } else {
349 link = &parent->rb_right;
350 leftmost = 0;
351 }
352 }
353
354 /*
355 * Maintain a cache of leftmost tree entries (it is frequently
356 * used):
357 */
Peter Zijlstra1af5f732008-10-24 11:06:13 +0200358 if (leftmost)
Ingo Molnar57cb4992007-10-15 17:00:11 +0200359 cfs_rq->rb_leftmost = &se->run_node;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200360
361 rb_link_node(&se->run_node, parent, link);
362 rb_insert_color(&se->run_node, &cfs_rq->tasks_timeline);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200363}
364
Ingo Molnar0702e3e2007-10-15 17:00:14 +0200365static void __dequeue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200366{
Peter Zijlstra3fe69742008-03-14 20:55:51 +0100367 if (cfs_rq->rb_leftmost == &se->run_node) {
368 struct rb_node *next_node;
Peter Zijlstra3fe69742008-03-14 20:55:51 +0100369
370 next_node = rb_next(&se->run_node);
371 cfs_rq->rb_leftmost = next_node;
Peter Zijlstra3fe69742008-03-14 20:55:51 +0100372 }
Ingo Molnare9acbff2007-10-15 17:00:04 +0200373
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200374 rb_erase(&se->run_node, &cfs_rq->tasks_timeline);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200375}
376
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200377static struct sched_entity *__pick_next_entity(struct cfs_rq *cfs_rq)
378{
Peter Zijlstraf4b67552008-11-04 21:25:07 +0100379 struct rb_node *left = cfs_rq->rb_leftmost;
380
381 if (!left)
382 return NULL;
383
384 return rb_entry(left, struct sched_entity, run_node);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200385}
386
Peter Zijlstraf4b67552008-11-04 21:25:07 +0100387static struct sched_entity *__pick_last_entity(struct cfs_rq *cfs_rq)
Peter Zijlstraaeb73b02007-10-15 17:00:05 +0200388{
Ingo Molnar7eee3e62008-02-22 10:32:21 +0100389 struct rb_node *last = rb_last(&cfs_rq->tasks_timeline);
Peter Zijlstraaeb73b02007-10-15 17:00:05 +0200390
Balbir Singh70eee742008-02-22 13:25:53 +0530391 if (!last)
392 return NULL;
Ingo Molnar7eee3e62008-02-22 10:32:21 +0100393
394 return rb_entry(last, struct sched_entity, run_node);
Peter Zijlstraaeb73b02007-10-15 17:00:05 +0200395}
396
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200397/**************************************************************
398 * Scheduling class statistics methods:
399 */
400
Peter Zijlstrab2be5e92007-11-09 22:39:37 +0100401#ifdef CONFIG_SCHED_DEBUG
402int sched_nr_latency_handler(struct ctl_table *table, int write,
Alexey Dobriyan8d65af72009-09-23 15:57:19 -0700403 void __user *buffer, size_t *lenp,
Peter Zijlstrab2be5e92007-11-09 22:39:37 +0100404 loff_t *ppos)
405{
Alexey Dobriyan8d65af72009-09-23 15:57:19 -0700406 int ret = proc_dointvec_minmax(table, write, buffer, lenp, ppos);
Peter Zijlstrab2be5e92007-11-09 22:39:37 +0100407
408 if (ret || !write)
409 return ret;
410
411 sched_nr_latency = DIV_ROUND_UP(sysctl_sched_latency,
412 sysctl_sched_min_granularity);
413
414 return 0;
415}
416#endif
Ingo Molnar647e7ca2007-10-15 17:00:13 +0200417
418/*
Peter Zijlstraf9c0b092008-10-17 19:27:04 +0200419 * delta /= w
Peter Zijlstraa7be37a2008-06-27 13:41:11 +0200420 */
421static inline unsigned long
422calc_delta_fair(unsigned long delta, struct sched_entity *se)
423{
Peter Zijlstraf9c0b092008-10-17 19:27:04 +0200424 if (unlikely(se->load.weight != NICE_0_LOAD))
425 delta = calc_delta_mine(delta, NICE_0_LOAD, &se->load);
Peter Zijlstraa7be37a2008-06-27 13:41:11 +0200426
427 return delta;
428}
429
430/*
Ingo Molnar647e7ca2007-10-15 17:00:13 +0200431 * The idea is to set a period in which each task runs once.
432 *
433 * When there are too many tasks (sysctl_sched_nr_latency) we have to stretch
434 * this period because otherwise the slices get too small.
435 *
436 * p = (nr <= nl) ? l : l*nr/nl
437 */
Peter Zijlstra4d78e7b2007-10-15 17:00:04 +0200438static u64 __sched_period(unsigned long nr_running)
439{
440 u64 period = sysctl_sched_latency;
Peter Zijlstrab2be5e92007-11-09 22:39:37 +0100441 unsigned long nr_latency = sched_nr_latency;
Peter Zijlstra4d78e7b2007-10-15 17:00:04 +0200442
443 if (unlikely(nr_running > nr_latency)) {
Peter Zijlstra4bf0b772008-01-25 21:08:21 +0100444 period = sysctl_sched_min_granularity;
Peter Zijlstra4d78e7b2007-10-15 17:00:04 +0200445 period *= nr_running;
Peter Zijlstra4d78e7b2007-10-15 17:00:04 +0200446 }
447
448 return period;
449}
450
Ingo Molnar647e7ca2007-10-15 17:00:13 +0200451/*
452 * We calculate the wall-time slice from the period by taking a part
453 * proportional to the weight.
454 *
Peter Zijlstraf9c0b092008-10-17 19:27:04 +0200455 * s = p*P[w/rw]
Ingo Molnar647e7ca2007-10-15 17:00:13 +0200456 */
Peter Zijlstra6d0f0eb2007-10-15 17:00:05 +0200457static u64 sched_slice(struct cfs_rq *cfs_rq, struct sched_entity *se)
Peter Zijlstra21805082007-08-25 18:41:53 +0200458{
Mike Galbraith0a582442009-01-02 12:16:42 +0100459 u64 slice = __sched_period(cfs_rq->nr_running + !se->on_rq);
Peter Zijlstraf9c0b092008-10-17 19:27:04 +0200460
Mike Galbraith0a582442009-01-02 12:16:42 +0100461 for_each_sched_entity(se) {
Lin Ming6272d682009-01-15 17:17:15 +0100462 struct load_weight *load;
Christian Engelmayer3104bf02009-06-16 10:35:12 +0200463 struct load_weight lw;
Lin Ming6272d682009-01-15 17:17:15 +0100464
465 cfs_rq = cfs_rq_of(se);
466 load = &cfs_rq->load;
Peter Zijlstraf9c0b092008-10-17 19:27:04 +0200467
Mike Galbraith0a582442009-01-02 12:16:42 +0100468 if (unlikely(!se->on_rq)) {
Christian Engelmayer3104bf02009-06-16 10:35:12 +0200469 lw = cfs_rq->load;
Mike Galbraith0a582442009-01-02 12:16:42 +0100470
471 update_load_add(&lw, se->load.weight);
472 load = &lw;
473 }
474 slice = calc_delta_mine(slice, se->load.weight, load);
475 }
476 return slice;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200477}
478
Ingo Molnar647e7ca2007-10-15 17:00:13 +0200479/*
Peter Zijlstraac884de2008-04-19 19:45:00 +0200480 * We calculate the vruntime slice of a to be inserted task
Ingo Molnar647e7ca2007-10-15 17:00:13 +0200481 *
Peter Zijlstraf9c0b092008-10-17 19:27:04 +0200482 * vs = s/w
Ingo Molnar647e7ca2007-10-15 17:00:13 +0200483 */
Peter Zijlstraf9c0b092008-10-17 19:27:04 +0200484static u64 sched_vslice(struct cfs_rq *cfs_rq, struct sched_entity *se)
Ingo Molnar647e7ca2007-10-15 17:00:13 +0200485{
Peter Zijlstraf9c0b092008-10-17 19:27:04 +0200486 return calc_delta_fair(sched_slice(cfs_rq, se), se);
Peter Zijlstraa7be37a2008-06-27 13:41:11 +0200487}
488
489/*
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200490 * Update the current task's runtime statistics. Skip current tasks that
491 * are not in our scheduling class.
492 */
493static inline void
Ingo Molnar8ebc91d2007-10-15 17:00:03 +0200494__update_curr(struct cfs_rq *cfs_rq, struct sched_entity *curr,
495 unsigned long delta_exec)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200496{
Ingo Molnarbbdba7c2007-10-15 17:00:06 +0200497 unsigned long delta_exec_weighted;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200498
Ingo Molnar8179ca232007-08-02 17:41:40 +0200499 schedstat_set(curr->exec_max, max((u64)delta_exec, curr->exec_max));
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200500
501 curr->sum_exec_runtime += delta_exec;
Ingo Molnar7a62eab2007-10-15 17:00:06 +0200502 schedstat_add(cfs_rq, exec_clock, delta_exec);
Peter Zijlstraa7be37a2008-06-27 13:41:11 +0200503 delta_exec_weighted = calc_delta_fair(delta_exec, curr);
Ingo Molnare9acbff2007-10-15 17:00:04 +0200504 curr->vruntime += delta_exec_weighted;
Peter Zijlstra1af5f732008-10-24 11:06:13 +0200505 update_min_vruntime(cfs_rq);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200506}
507
Ingo Molnarb7cc0892007-08-09 11:16:47 +0200508static void update_curr(struct cfs_rq *cfs_rq)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200509{
Ingo Molnar429d43b2007-10-15 17:00:03 +0200510 struct sched_entity *curr = cfs_rq->curr;
Ingo Molnar8ebc91d2007-10-15 17:00:03 +0200511 u64 now = rq_of(cfs_rq)->clock;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200512 unsigned long delta_exec;
513
514 if (unlikely(!curr))
515 return;
516
517 /*
518 * Get the amount of time the current task was running
519 * since the last time we changed load (this cannot
520 * overflow on 32 bits):
521 */
Ingo Molnar8ebc91d2007-10-15 17:00:03 +0200522 delta_exec = (unsigned long)(now - curr->exec_start);
Peter Zijlstra34f28ec2008-12-16 08:45:31 +0100523 if (!delta_exec)
524 return;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200525
Ingo Molnar8ebc91d2007-10-15 17:00:03 +0200526 __update_curr(cfs_rq, curr, delta_exec);
527 curr->exec_start = now;
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +0100528
529 if (entity_is_task(curr)) {
530 struct task_struct *curtask = task_of(curr);
531
Ingo Molnarf977bb42009-09-13 18:15:54 +0200532 trace_sched_stat_runtime(curtask, delta_exec, curr->vruntime);
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +0100533 cpuacct_charge(curtask, delta_exec);
Frank Mayharf06febc2008-09-12 09:54:39 -0700534 account_group_exec_runtime(curtask, delta_exec);
Srivatsa Vaddagirid842de82007-12-02 20:04:49 +0100535 }
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200536}
537
538static inline void
Ingo Molnar5870db52007-08-09 11:16:47 +0200539update_stats_wait_start(struct cfs_rq *cfs_rq, struct sched_entity *se)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200540{
Ingo Molnard2819182007-08-09 11:16:47 +0200541 schedstat_set(se->wait_start, rq_of(cfs_rq)->clock);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200542}
543
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200544/*
545 * Task is being enqueued - update stats:
546 */
Ingo Molnard2417e52007-08-09 11:16:47 +0200547static void update_stats_enqueue(struct cfs_rq *cfs_rq, struct sched_entity *se)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200548{
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200549 /*
550 * Are we enqueueing a waiting task? (for current tasks
551 * a dequeue/enqueue event is a NOP)
552 */
Ingo Molnar429d43b2007-10-15 17:00:03 +0200553 if (se != cfs_rq->curr)
Ingo Molnar5870db52007-08-09 11:16:47 +0200554 update_stats_wait_start(cfs_rq, se);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200555}
556
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200557static void
Ingo Molnar9ef0a962007-08-09 11:16:47 +0200558update_stats_wait_end(struct cfs_rq *cfs_rq, struct sched_entity *se)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200559{
Ingo Molnarbbdba7c2007-10-15 17:00:06 +0200560 schedstat_set(se->wait_max, max(se->wait_max,
561 rq_of(cfs_rq)->clock - se->wait_start));
Arjan van de Ven6d082592008-01-25 21:08:35 +0100562 schedstat_set(se->wait_count, se->wait_count + 1);
563 schedstat_set(se->wait_sum, se->wait_sum +
564 rq_of(cfs_rq)->clock - se->wait_start);
Peter Zijlstra768d0c22009-07-23 20:13:26 +0200565#ifdef CONFIG_SCHEDSTATS
566 if (entity_is_task(se)) {
567 trace_sched_stat_wait(task_of(se),
568 rq_of(cfs_rq)->clock - se->wait_start);
569 }
570#endif
Ingo Molnare1f84502009-09-10 20:52:09 +0200571 schedstat_set(se->wait_start, 0);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200572}
573
574static inline void
Ingo Molnar19b6a2e2007-08-09 11:16:48 +0200575update_stats_dequeue(struct cfs_rq *cfs_rq, struct sched_entity *se)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200576{
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200577 /*
578 * Mark the end of the wait period if dequeueing a
579 * waiting task:
580 */
Ingo Molnar429d43b2007-10-15 17:00:03 +0200581 if (se != cfs_rq->curr)
Ingo Molnar9ef0a962007-08-09 11:16:47 +0200582 update_stats_wait_end(cfs_rq, se);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200583}
584
585/*
586 * We are picking a new current task - update its stats:
587 */
588static inline void
Ingo Molnar79303e92007-08-09 11:16:47 +0200589update_stats_curr_start(struct cfs_rq *cfs_rq, struct sched_entity *se)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200590{
591 /*
592 * We are starting a new run period:
593 */
Ingo Molnard2819182007-08-09 11:16:47 +0200594 se->exec_start = rq_of(cfs_rq)->clock;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200595}
596
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200597/**************************************************
598 * Scheduling class queueing methods:
599 */
600
Peter Zijlstrac09595f2008-06-27 13:41:14 +0200601#if defined CONFIG_SMP && defined CONFIG_FAIR_GROUP_SCHED
602static void
603add_cfs_task_weight(struct cfs_rq *cfs_rq, unsigned long weight)
604{
605 cfs_rq->task_weight += weight;
606}
607#else
608static inline void
609add_cfs_task_weight(struct cfs_rq *cfs_rq, unsigned long weight)
610{
611}
612#endif
613
Dmitry Adamushko30cfdcf2007-10-15 17:00:07 +0200614static void
615account_entity_enqueue(struct cfs_rq *cfs_rq, struct sched_entity *se)
616{
617 update_load_add(&cfs_rq->load, se->load.weight);
Peter Zijlstrac09595f2008-06-27 13:41:14 +0200618 if (!parent_entity(se))
619 inc_cpu_load(rq_of(cfs_rq), se->load.weight);
Bharata B Raob87f1722008-09-25 09:53:54 +0530620 if (entity_is_task(se)) {
Peter Zijlstrac09595f2008-06-27 13:41:14 +0200621 add_cfs_task_weight(cfs_rq, se->load.weight);
Bharata B Raob87f1722008-09-25 09:53:54 +0530622 list_add(&se->group_node, &cfs_rq->tasks);
623 }
Dmitry Adamushko30cfdcf2007-10-15 17:00:07 +0200624 cfs_rq->nr_running++;
625 se->on_rq = 1;
626}
627
628static void
629account_entity_dequeue(struct cfs_rq *cfs_rq, struct sched_entity *se)
630{
631 update_load_sub(&cfs_rq->load, se->load.weight);
Peter Zijlstrac09595f2008-06-27 13:41:14 +0200632 if (!parent_entity(se))
633 dec_cpu_load(rq_of(cfs_rq), se->load.weight);
Bharata B Raob87f1722008-09-25 09:53:54 +0530634 if (entity_is_task(se)) {
Peter Zijlstrac09595f2008-06-27 13:41:14 +0200635 add_cfs_task_weight(cfs_rq, -se->load.weight);
Bharata B Raob87f1722008-09-25 09:53:54 +0530636 list_del_init(&se->group_node);
637 }
Dmitry Adamushko30cfdcf2007-10-15 17:00:07 +0200638 cfs_rq->nr_running--;
639 se->on_rq = 0;
640}
641
Ingo Molnar2396af62007-08-09 11:16:48 +0200642static void enqueue_sleeper(struct cfs_rq *cfs_rq, struct sched_entity *se)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200643{
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200644#ifdef CONFIG_SCHEDSTATS
Peter Zijlstrae4143142009-07-23 20:13:26 +0200645 struct task_struct *tsk = NULL;
646
647 if (entity_is_task(se))
648 tsk = task_of(se);
649
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200650 if (se->sleep_start) {
Ingo Molnard2819182007-08-09 11:16:47 +0200651 u64 delta = rq_of(cfs_rq)->clock - se->sleep_start;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200652
653 if ((s64)delta < 0)
654 delta = 0;
655
656 if (unlikely(delta > se->sleep_max))
657 se->sleep_max = delta;
658
659 se->sleep_start = 0;
660 se->sum_sleep_runtime += delta;
Arjan van de Ven97455122008-01-25 21:08:34 +0100661
Peter Zijlstra768d0c22009-07-23 20:13:26 +0200662 if (tsk) {
Peter Zijlstrae4143142009-07-23 20:13:26 +0200663 account_scheduler_latency(tsk, delta >> 10, 1);
Peter Zijlstra768d0c22009-07-23 20:13:26 +0200664 trace_sched_stat_sleep(tsk, delta);
665 }
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200666 }
667 if (se->block_start) {
Ingo Molnard2819182007-08-09 11:16:47 +0200668 u64 delta = rq_of(cfs_rq)->clock - se->block_start;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200669
670 if ((s64)delta < 0)
671 delta = 0;
672
673 if (unlikely(delta > se->block_max))
674 se->block_max = delta;
675
676 se->block_start = 0;
677 se->sum_sleep_runtime += delta;
Ingo Molnar30084fb2007-10-02 14:13:08 +0200678
Peter Zijlstrae4143142009-07-23 20:13:26 +0200679 if (tsk) {
Arjan van de Ven8f0dfc32009-07-20 11:26:58 -0700680 if (tsk->in_iowait) {
681 se->iowait_sum += delta;
682 se->iowait_count++;
Peter Zijlstra768d0c22009-07-23 20:13:26 +0200683 trace_sched_stat_iowait(tsk, delta);
Arjan van de Ven8f0dfc32009-07-20 11:26:58 -0700684 }
685
Peter Zijlstrae4143142009-07-23 20:13:26 +0200686 /*
687 * Blocking time is in units of nanosecs, so shift by
688 * 20 to get a milliseconds-range estimation of the
689 * amount of time that the task spent sleeping:
690 */
691 if (unlikely(prof_on == SLEEP_PROFILING)) {
692 profile_hits(SLEEP_PROFILING,
693 (void *)get_wchan(tsk),
694 delta >> 20);
695 }
696 account_scheduler_latency(tsk, delta >> 10, 0);
Ingo Molnar30084fb2007-10-02 14:13:08 +0200697 }
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200698 }
699#endif
700}
701
Peter Zijlstraddc97292007-10-15 17:00:10 +0200702static void check_spread(struct cfs_rq *cfs_rq, struct sched_entity *se)
703{
704#ifdef CONFIG_SCHED_DEBUG
705 s64 d = se->vruntime - cfs_rq->min_vruntime;
706
707 if (d < 0)
708 d = -d;
709
710 if (d > 3*sysctl_sched_latency)
711 schedstat_inc(cfs_rq, nr_spread_over);
712#endif
713}
714
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200715static void
Peter Zijlstraaeb73b02007-10-15 17:00:05 +0200716place_entity(struct cfs_rq *cfs_rq, struct sched_entity *se, int initial)
717{
Peter Zijlstra1af5f732008-10-24 11:06:13 +0200718 u64 vruntime = cfs_rq->min_vruntime;
Peter Zijlstra94dfb5e2007-10-15 17:00:05 +0200719
Peter Zijlstra2cb86002007-11-09 22:39:37 +0100720 /*
721 * The 'current' period is already promised to the current tasks,
722 * however the extra weight of the new task will slow them down a
723 * little, place the new task so that it fits in the slot that
724 * stays open at the end.
725 */
Peter Zijlstra94dfb5e2007-10-15 17:00:05 +0200726 if (initial && sched_feat(START_DEBIT))
Peter Zijlstraf9c0b092008-10-17 19:27:04 +0200727 vruntime += sched_vslice(cfs_rq, se);
Peter Zijlstraaeb73b02007-10-15 17:00:05 +0200728
Mike Galbraitha2e7a7e2009-09-18 09:19:25 +0200729 /* sleeps up to a single latency don't count. */
730 if (!initial && sched_feat(FAIR_SLEEPERS)) {
731 unsigned long thresh = sysctl_sched_latency;
Peter Zijlstraa7be37a2008-06-27 13:41:11 +0200732
Mike Galbraitha2e7a7e2009-09-18 09:19:25 +0200733 /*
734 * Convert the sleeper threshold into virtual time.
735 * SCHED_IDLE is a special sub-class. We care about
736 * fairness only relative to other SCHED_IDLE tasks,
737 * all of which have the same weight.
738 */
739 if (sched_feat(NORMALIZED_SLEEPER) && (!entity_is_task(se) ||
740 task_of(se)->policy != SCHED_IDLE))
741 thresh = calc_delta_fair(thresh, se);
Peter Zijlstraa7be37a2008-06-27 13:41:11 +0200742
Mike Galbraitha2e7a7e2009-09-18 09:19:25 +0200743 /*
744 * Halve their sleep time's effect, to allow
745 * for a gentler effect of sleepers:
746 */
747 if (sched_feat(GENTLE_FAIR_SLEEPERS))
748 thresh >>= 1;
Ingo Molnar51e03042009-09-16 08:54:45 +0200749
Mike Galbraitha2e7a7e2009-09-18 09:19:25 +0200750 vruntime -= thresh;
Peter Zijlstraaeb73b02007-10-15 17:00:05 +0200751 }
752
Mike Galbraithb5d9d732009-09-08 11:12:28 +0200753 /* ensure we never gain time by being placed backwards. */
754 vruntime = max_vruntime(se->vruntime, vruntime);
755
Peter Zijlstra67e9fb22007-10-15 17:00:10 +0200756 se->vruntime = vruntime;
Peter Zijlstraaeb73b02007-10-15 17:00:05 +0200757}
758
759static void
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +0200760enqueue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se, int wakeup)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200761{
762 /*
Dmitry Adamushkoa2a2d682007-10-15 17:00:13 +0200763 * Update run-time statistics of the 'current'.
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200764 */
Ingo Molnarb7cc0892007-08-09 11:16:47 +0200765 update_curr(cfs_rq);
Peter Zijlstraa9922412008-05-05 23:56:17 +0200766 account_entity_enqueue(cfs_rq, se);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200767
Ingo Molnare9acbff2007-10-15 17:00:04 +0200768 if (wakeup) {
Peter Zijlstraaeb73b02007-10-15 17:00:05 +0200769 place_entity(cfs_rq, se, 0);
Ingo Molnar2396af62007-08-09 11:16:48 +0200770 enqueue_sleeper(cfs_rq, se);
Ingo Molnare9acbff2007-10-15 17:00:04 +0200771 }
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200772
Ingo Molnard2417e52007-08-09 11:16:47 +0200773 update_stats_enqueue(cfs_rq, se);
Peter Zijlstraddc97292007-10-15 17:00:10 +0200774 check_spread(cfs_rq, se);
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +0200775 if (se != cfs_rq->curr)
776 __enqueue_entity(cfs_rq, se);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200777}
778
Peter Zijlstraa571bbe2009-01-28 14:51:40 +0100779static void __clear_buddies(struct cfs_rq *cfs_rq, struct sched_entity *se)
Peter Zijlstra2002c692008-11-11 11:52:33 +0100780{
Peter Zijlstrade69a802009-09-17 09:01:20 +0200781 if (!se || cfs_rq->last == se)
Peter Zijlstra2002c692008-11-11 11:52:33 +0100782 cfs_rq->last = NULL;
783
Peter Zijlstrade69a802009-09-17 09:01:20 +0200784 if (!se || cfs_rq->next == se)
Peter Zijlstra2002c692008-11-11 11:52:33 +0100785 cfs_rq->next = NULL;
786}
787
Peter Zijlstraa571bbe2009-01-28 14:51:40 +0100788static void clear_buddies(struct cfs_rq *cfs_rq, struct sched_entity *se)
789{
790 for_each_sched_entity(se)
791 __clear_buddies(cfs_rq_of(se), se);
792}
793
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200794static void
Ingo Molnar525c2712007-08-09 11:16:48 +0200795dequeue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se, int sleep)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200796{
Dmitry Adamushkoa2a2d682007-10-15 17:00:13 +0200797 /*
798 * Update run-time statistics of the 'current'.
799 */
800 update_curr(cfs_rq);
801
Ingo Molnar19b6a2e2007-08-09 11:16:48 +0200802 update_stats_dequeue(cfs_rq, se);
Dmitry Adamushkodb36cc72007-10-15 17:00:06 +0200803 if (sleep) {
Peter Zijlstra67e9fb22007-10-15 17:00:10 +0200804#ifdef CONFIG_SCHEDSTATS
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200805 if (entity_is_task(se)) {
806 struct task_struct *tsk = task_of(se);
807
808 if (tsk->state & TASK_INTERRUPTIBLE)
Ingo Molnard2819182007-08-09 11:16:47 +0200809 se->sleep_start = rq_of(cfs_rq)->clock;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200810 if (tsk->state & TASK_UNINTERRUPTIBLE)
Ingo Molnard2819182007-08-09 11:16:47 +0200811 se->block_start = rq_of(cfs_rq)->clock;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200812 }
Dmitry Adamushkodb36cc72007-10-15 17:00:06 +0200813#endif
Peter Zijlstra67e9fb22007-10-15 17:00:10 +0200814 }
815
Peter Zijlstra2002c692008-11-11 11:52:33 +0100816 clear_buddies(cfs_rq, se);
Peter Zijlstra47932412008-11-04 21:25:09 +0100817
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +0200818 if (se != cfs_rq->curr)
Dmitry Adamushko30cfdcf2007-10-15 17:00:07 +0200819 __dequeue_entity(cfs_rq, se);
820 account_entity_dequeue(cfs_rq, se);
Peter Zijlstra1af5f732008-10-24 11:06:13 +0200821 update_min_vruntime(cfs_rq);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200822}
823
824/*
825 * Preempt the current task with a newly woken task if needed:
826 */
Peter Zijlstra7c92e542007-09-05 14:32:49 +0200827static void
Ingo Molnar2e09bf52007-10-15 17:00:05 +0200828check_preempt_tick(struct cfs_rq *cfs_rq, struct sched_entity *curr)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200829{
Peter Zijlstra11697832007-09-05 14:32:49 +0200830 unsigned long ideal_runtime, delta_exec;
831
Peter Zijlstra6d0f0eb2007-10-15 17:00:05 +0200832 ideal_runtime = sched_slice(cfs_rq, curr);
Peter Zijlstra11697832007-09-05 14:32:49 +0200833 delta_exec = curr->sum_exec_runtime - curr->prev_sum_exec_runtime;
Mike Galbraitha9f3e2b2009-01-28 14:51:39 +0100834 if (delta_exec > ideal_runtime) {
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200835 resched_task(rq_of(cfs_rq)->curr);
Mike Galbraitha9f3e2b2009-01-28 14:51:39 +0100836 /*
837 * The current task ran long enough, ensure it doesn't get
838 * re-elected due to buddy favours.
839 */
840 clear_buddies(cfs_rq, curr);
Mike Galbraithf685cea2009-10-23 23:09:22 +0200841 return;
842 }
843
844 /*
845 * Ensure that a task that missed wakeup preemption by a
846 * narrow margin doesn't have to wait for a full slice.
847 * This also mitigates buddy induced latencies under load.
848 */
849 if (!sched_feat(WAKEUP_PREEMPT))
850 return;
851
852 if (delta_exec < sysctl_sched_min_granularity)
853 return;
854
855 if (cfs_rq->nr_running > 1) {
856 struct sched_entity *se = __pick_next_entity(cfs_rq);
857 s64 delta = curr->vruntime - se->vruntime;
858
859 if (delta > ideal_runtime)
860 resched_task(rq_of(cfs_rq)->curr);
Mike Galbraitha9f3e2b2009-01-28 14:51:39 +0100861 }
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200862}
863
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +0200864static void
Ingo Molnar8494f412007-08-09 11:16:48 +0200865set_next_entity(struct cfs_rq *cfs_rq, struct sched_entity *se)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200866{
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +0200867 /* 'current' is not kept within the tree. */
868 if (se->on_rq) {
869 /*
870 * Any task has to be enqueued before it get to execute on
871 * a CPU. So account for the time it spent waiting on the
872 * runqueue.
873 */
874 update_stats_wait_end(cfs_rq, se);
875 __dequeue_entity(cfs_rq, se);
876 }
877
Ingo Molnar79303e92007-08-09 11:16:47 +0200878 update_stats_curr_start(cfs_rq, se);
Ingo Molnar429d43b2007-10-15 17:00:03 +0200879 cfs_rq->curr = se;
Ingo Molnareba1ed42007-10-15 17:00:02 +0200880#ifdef CONFIG_SCHEDSTATS
881 /*
882 * Track our maximum slice length, if the CPU's load is at
883 * least twice that of our own weight (i.e. dont track it
884 * when there are only lesser-weight tasks around):
885 */
Dmitry Adamushko495eca42007-10-15 17:00:06 +0200886 if (rq_of(cfs_rq)->load.weight >= 2*se->load.weight) {
Ingo Molnareba1ed42007-10-15 17:00:02 +0200887 se->slice_max = max(se->slice_max,
888 se->sum_exec_runtime - se->prev_sum_exec_runtime);
889 }
890#endif
Peter Zijlstra4a55b452007-09-05 14:32:49 +0200891 se->prev_sum_exec_runtime = se->sum_exec_runtime;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200892}
893
Peter Zijlstra3f3a4902008-10-24 11:06:16 +0200894static int
895wakeup_preempt_entity(struct sched_entity *curr, struct sched_entity *se);
896
Peter Zijlstraf4b67552008-11-04 21:25:07 +0100897static struct sched_entity *pick_next_entity(struct cfs_rq *cfs_rq)
Peter Zijlstraaa2ac252008-03-14 21:12:12 +0100898{
Peter Zijlstraf4b67552008-11-04 21:25:07 +0100899 struct sched_entity *se = __pick_next_entity(cfs_rq);
Mike Galbraithf685cea2009-10-23 23:09:22 +0200900 struct sched_entity *left = se;
Peter Zijlstraf4b67552008-11-04 21:25:07 +0100901
Mike Galbraithf685cea2009-10-23 23:09:22 +0200902 if (cfs_rq->next && wakeup_preempt_entity(cfs_rq->next, left) < 1)
903 se = cfs_rq->next;
Peter Zijlstraaa2ac252008-03-14 21:12:12 +0100904
Mike Galbraithf685cea2009-10-23 23:09:22 +0200905 /*
906 * Prefer last buddy, try to return the CPU to a preempted task.
907 */
908 if (cfs_rq->last && wakeup_preempt_entity(cfs_rq->last, left) < 1)
909 se = cfs_rq->last;
910
911 clear_buddies(cfs_rq, se);
Peter Zijlstra47932412008-11-04 21:25:09 +0100912
913 return se;
Peter Zijlstraaa2ac252008-03-14 21:12:12 +0100914}
915
Ingo Molnarab6cde22007-08-09 11:16:48 +0200916static void put_prev_entity(struct cfs_rq *cfs_rq, struct sched_entity *prev)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200917{
918 /*
919 * If still on the runqueue then deactivate_task()
920 * was not called and update_curr() has to be done:
921 */
922 if (prev->on_rq)
Ingo Molnarb7cc0892007-08-09 11:16:47 +0200923 update_curr(cfs_rq);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200924
Peter Zijlstraddc97292007-10-15 17:00:10 +0200925 check_spread(cfs_rq, prev);
Dmitry Adamushko30cfdcf2007-10-15 17:00:07 +0200926 if (prev->on_rq) {
Ingo Molnar5870db52007-08-09 11:16:47 +0200927 update_stats_wait_start(cfs_rq, prev);
Dmitry Adamushko30cfdcf2007-10-15 17:00:07 +0200928 /* Put 'current' back into the tree. */
929 __enqueue_entity(cfs_rq, prev);
930 }
Ingo Molnar429d43b2007-10-15 17:00:03 +0200931 cfs_rq->curr = NULL;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200932}
933
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +0100934static void
935entity_tick(struct cfs_rq *cfs_rq, struct sched_entity *curr, int queued)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200936{
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200937 /*
Dmitry Adamushko30cfdcf2007-10-15 17:00:07 +0200938 * Update run-time statistics of the 'current'.
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200939 */
Dmitry Adamushko30cfdcf2007-10-15 17:00:07 +0200940 update_curr(cfs_rq);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200941
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +0100942#ifdef CONFIG_SCHED_HRTICK
943 /*
944 * queued ticks are scheduled to match the slice, so don't bother
945 * validating it and just reschedule.
946 */
Harvey Harrison983ed7a2008-04-24 18:17:55 -0700947 if (queued) {
948 resched_task(rq_of(cfs_rq)->curr);
949 return;
950 }
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +0100951 /*
952 * don't let the period tick interfere with the hrtick preemption
953 */
954 if (!sched_feat(DOUBLE_TICK) &&
955 hrtimer_active(&rq_of(cfs_rq)->hrtick_timer))
956 return;
957#endif
958
Peter Zijlstrace6c1312007-10-15 17:00:14 +0200959 if (cfs_rq->nr_running > 1 || !sched_feat(WAKEUP_PREEMPT))
Ingo Molnar2e09bf52007-10-15 17:00:05 +0200960 check_preempt_tick(cfs_rq, curr);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +0200961}
962
963/**************************************************
964 * CFS operations on tasks:
965 */
966
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +0100967#ifdef CONFIG_SCHED_HRTICK
968static void hrtick_start_fair(struct rq *rq, struct task_struct *p)
969{
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +0100970 struct sched_entity *se = &p->se;
971 struct cfs_rq *cfs_rq = cfs_rq_of(se);
972
973 WARN_ON(task_rq(p) != rq);
974
975 if (hrtick_enabled(rq) && cfs_rq->nr_running > 1) {
976 u64 slice = sched_slice(cfs_rq, se);
977 u64 ran = se->sum_exec_runtime - se->prev_sum_exec_runtime;
978 s64 delta = slice - ran;
979
980 if (delta < 0) {
981 if (rq->curr == p)
982 resched_task(p);
983 return;
984 }
985
986 /*
987 * Don't schedule slices shorter than 10000ns, that just
988 * doesn't make sense. Rely on vruntime for fairness.
989 */
Peter Zijlstra31656512008-07-18 18:01:23 +0200990 if (rq->curr != p)
Peter Zijlstra157124c2008-07-28 11:53:11 +0200991 delta = max_t(s64, 10000LL, delta);
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +0100992
Peter Zijlstra31656512008-07-18 18:01:23 +0200993 hrtick_start(rq, delta);
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +0100994 }
995}
Peter Zijlstraa4c2f002008-10-17 19:27:03 +0200996
997/*
998 * called from enqueue/dequeue and updates the hrtick when the
999 * current task is from our class and nr_running is low enough
1000 * to matter.
1001 */
1002static void hrtick_update(struct rq *rq)
1003{
1004 struct task_struct *curr = rq->curr;
1005
1006 if (curr->sched_class != &fair_sched_class)
1007 return;
1008
1009 if (cfs_rq_of(&curr->se)->nr_running < sched_nr_latency)
1010 hrtick_start_fair(rq, curr);
1011}
Dhaval Giani55e12e52008-06-24 23:39:43 +05301012#else /* !CONFIG_SCHED_HRTICK */
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001013static inline void
1014hrtick_start_fair(struct rq *rq, struct task_struct *p)
1015{
1016}
Peter Zijlstraa4c2f002008-10-17 19:27:03 +02001017
1018static inline void hrtick_update(struct rq *rq)
1019{
1020}
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001021#endif
1022
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001023/*
1024 * The enqueue_task method is called before nr_running is
1025 * increased. Here we update the fair scheduling stats and
1026 * then put the task into the rbtree:
1027 */
Ingo Molnarfd390f62007-08-09 11:16:48 +02001028static void enqueue_task_fair(struct rq *rq, struct task_struct *p, int wakeup)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001029{
1030 struct cfs_rq *cfs_rq;
Peter Zijlstra62fb1852008-02-25 17:34:02 +01001031 struct sched_entity *se = &p->se;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001032
1033 for_each_sched_entity(se) {
Peter Zijlstra62fb1852008-02-25 17:34:02 +01001034 if (se->on_rq)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001035 break;
1036 cfs_rq = cfs_rq_of(se);
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +02001037 enqueue_entity(cfs_rq, se, wakeup);
Srivatsa Vaddagirib9fa3df2007-10-15 17:00:12 +02001038 wakeup = 1;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001039 }
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001040
Peter Zijlstraa4c2f002008-10-17 19:27:03 +02001041 hrtick_update(rq);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001042}
1043
1044/*
1045 * The dequeue_task method is called before nr_running is
1046 * decreased. We remove the task from the rbtree and
1047 * update the fair scheduling stats:
1048 */
Ingo Molnarf02231e2007-08-09 11:16:48 +02001049static void dequeue_task_fair(struct rq *rq, struct task_struct *p, int sleep)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001050{
1051 struct cfs_rq *cfs_rq;
Peter Zijlstra62fb1852008-02-25 17:34:02 +01001052 struct sched_entity *se = &p->se;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001053
1054 for_each_sched_entity(se) {
1055 cfs_rq = cfs_rq_of(se);
Ingo Molnar525c2712007-08-09 11:16:48 +02001056 dequeue_entity(cfs_rq, se, sleep);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001057 /* Don't dequeue parent if it has other entities besides us */
Peter Zijlstra62fb1852008-02-25 17:34:02 +01001058 if (cfs_rq->load.weight)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001059 break;
Srivatsa Vaddagirib9fa3df2007-10-15 17:00:12 +02001060 sleep = 1;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001061 }
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001062
Peter Zijlstraa4c2f002008-10-17 19:27:03 +02001063 hrtick_update(rq);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001064}
1065
1066/*
Ingo Molnar1799e352007-09-19 23:34:46 +02001067 * sched_yield() support is very simple - we dequeue and enqueue.
1068 *
1069 * If compat_yield is turned on then we requeue to the end of the tree.
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001070 */
Dmitry Adamushko4530d7a2007-10-15 17:00:08 +02001071static void yield_task_fair(struct rq *rq)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001072{
Ingo Molnardb292ca2007-12-04 17:04:39 +01001073 struct task_struct *curr = rq->curr;
1074 struct cfs_rq *cfs_rq = task_cfs_rq(curr);
1075 struct sched_entity *rightmost, *se = &curr->se;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001076
1077 /*
Ingo Molnar1799e352007-09-19 23:34:46 +02001078 * Are we the only task in the tree?
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001079 */
Ingo Molnar1799e352007-09-19 23:34:46 +02001080 if (unlikely(cfs_rq->nr_running == 1))
1081 return;
1082
Peter Zijlstra2002c692008-11-11 11:52:33 +01001083 clear_buddies(cfs_rq, se);
1084
Ingo Molnardb292ca2007-12-04 17:04:39 +01001085 if (likely(!sysctl_sched_compat_yield) && curr->policy != SCHED_BATCH) {
Peter Zijlstra3e51f332008-05-03 18:29:28 +02001086 update_rq_clock(rq);
Ingo Molnar1799e352007-09-19 23:34:46 +02001087 /*
Dmitry Adamushkoa2a2d682007-10-15 17:00:13 +02001088 * Update run-time statistics of the 'current'.
Ingo Molnar1799e352007-09-19 23:34:46 +02001089 */
Dmitry Adamushko2b1e3152007-10-15 17:00:12 +02001090 update_curr(cfs_rq);
Ingo Molnar1799e352007-09-19 23:34:46 +02001091
1092 return;
1093 }
1094 /*
1095 * Find the rightmost entry in the rbtree:
1096 */
Dmitry Adamushko2b1e3152007-10-15 17:00:12 +02001097 rightmost = __pick_last_entity(cfs_rq);
Ingo Molnar1799e352007-09-19 23:34:46 +02001098 /*
1099 * Already in the rightmost position?
1100 */
Fabio Checconi54fdc582009-07-16 12:32:27 +02001101 if (unlikely(!rightmost || entity_before(rightmost, se)))
Ingo Molnar1799e352007-09-19 23:34:46 +02001102 return;
1103
1104 /*
1105 * Minimally necessary key value to be last in the tree:
Dmitry Adamushko2b1e3152007-10-15 17:00:12 +02001106 * Upon rescheduling, sched_class::put_prev_task() will place
1107 * 'current' within the tree based on its new key value.
Ingo Molnar1799e352007-09-19 23:34:46 +02001108 */
Dmitry Adamushko30cfdcf2007-10-15 17:00:07 +02001109 se->vruntime = rightmost->vruntime + 1;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001110}
1111
Gregory Haskinse7693a32008-01-25 21:08:09 +01001112#ifdef CONFIG_SMP
Ingo Molnar098fb9d2008-03-16 20:36:10 +01001113
Peter Zijlstrabb3469a2008-06-27 13:41:27 +02001114#ifdef CONFIG_FAIR_GROUP_SCHED
Peter Zijlstraf5bfb7d2008-06-27 13:41:39 +02001115/*
1116 * effective_load() calculates the load change as seen from the root_task_group
1117 *
1118 * Adding load to a group doesn't make a group heavier, but can cause movement
1119 * of group shares between cpus. Assuming the shares were perfectly aligned one
1120 * can calculate the shift in shares.
1121 *
1122 * The problem is that perfectly aligning the shares is rather expensive, hence
1123 * we try to avoid doing that too often - see update_shares(), which ratelimits
1124 * this change.
1125 *
1126 * We compensate this by not only taking the current delta into account, but
1127 * also considering the delta between when the shares were last adjusted and
1128 * now.
1129 *
1130 * We still saw a performance dip, some tracing learned us that between
1131 * cgroup:/ and cgroup:/foo balancing the number of affine wakeups increased
1132 * significantly. Therefore try to bias the error in direction of failing
1133 * the affine wakeup.
1134 *
1135 */
Peter Zijlstraf1d239f2008-06-27 13:41:38 +02001136static long effective_load(struct task_group *tg, int cpu,
1137 long wl, long wg)
Peter Zijlstrabb3469a2008-06-27 13:41:27 +02001138{
Peter Zijlstra4be9daa2008-06-27 13:41:30 +02001139 struct sched_entity *se = tg->se[cpu];
Peter Zijlstraf1d239f2008-06-27 13:41:38 +02001140
1141 if (!tg->parent)
1142 return wl;
1143
1144 /*
Peter Zijlstraf5bfb7d2008-06-27 13:41:39 +02001145 * By not taking the decrease of shares on the other cpu into
1146 * account our error leans towards reducing the affine wakeups.
1147 */
1148 if (!wl && sched_feat(ASYM_EFF_LOAD))
1149 return wl;
1150
Peter Zijlstra4be9daa2008-06-27 13:41:30 +02001151 for_each_sched_entity(se) {
Peter Zijlstracb5ef422008-06-27 13:41:32 +02001152 long S, rw, s, a, b;
Peter Zijlstra940959e2008-09-23 15:33:42 +02001153 long more_w;
1154
1155 /*
1156 * Instead of using this increment, also add the difference
1157 * between when the shares were last updated and now.
1158 */
1159 more_w = se->my_q->load.weight - se->my_q->rq_weight;
1160 wl += more_w;
1161 wg += more_w;
Peter Zijlstrabb3469a2008-06-27 13:41:27 +02001162
Peter Zijlstra4be9daa2008-06-27 13:41:30 +02001163 S = se->my_q->tg->shares;
1164 s = se->my_q->shares;
Peter Zijlstraf1d239f2008-06-27 13:41:38 +02001165 rw = se->my_q->rq_weight;
Peter Zijlstra4be9daa2008-06-27 13:41:30 +02001166
Peter Zijlstracb5ef422008-06-27 13:41:32 +02001167 a = S*(rw + wl);
1168 b = S*rw + s*wg;
Peter Zijlstra4be9daa2008-06-27 13:41:30 +02001169
Peter Zijlstra940959e2008-09-23 15:33:42 +02001170 wl = s*(a-b);
1171
1172 if (likely(b))
1173 wl /= b;
1174
Peter Zijlstra83378262008-06-27 13:41:37 +02001175 /*
1176 * Assume the group is already running and will
1177 * thus already be accounted for in the weight.
1178 *
1179 * That is, moving shares between CPUs, does not
1180 * alter the group weight.
1181 */
Peter Zijlstra4be9daa2008-06-27 13:41:30 +02001182 wg = 0;
Peter Zijlstra4be9daa2008-06-27 13:41:30 +02001183 }
1184
1185 return wl;
Peter Zijlstrabb3469a2008-06-27 13:41:27 +02001186}
Peter Zijlstra4be9daa2008-06-27 13:41:30 +02001187
Peter Zijlstrabb3469a2008-06-27 13:41:27 +02001188#else
Peter Zijlstra4be9daa2008-06-27 13:41:30 +02001189
Peter Zijlstra83378262008-06-27 13:41:37 +02001190static inline unsigned long effective_load(struct task_group *tg, int cpu,
1191 unsigned long wl, unsigned long wg)
Peter Zijlstra4be9daa2008-06-27 13:41:30 +02001192{
Peter Zijlstra83378262008-06-27 13:41:37 +02001193 return wl;
Peter Zijlstrabb3469a2008-06-27 13:41:27 +02001194}
Peter Zijlstra4be9daa2008-06-27 13:41:30 +02001195
Peter Zijlstrabb3469a2008-06-27 13:41:27 +02001196#endif
1197
Peter Zijlstrac88d5912009-09-10 13:50:02 +02001198static int wake_affine(struct sched_domain *sd, struct task_struct *p, int sync)
Ingo Molnar098fb9d2008-03-16 20:36:10 +01001199{
Peter Zijlstrac88d5912009-09-10 13:50:02 +02001200 struct task_struct *curr = current;
1201 unsigned long this_load, load;
1202 int idx, this_cpu, prev_cpu;
Ingo Molnar098fb9d2008-03-16 20:36:10 +01001203 unsigned long tl_per_task;
Peter Zijlstrac88d5912009-09-10 13:50:02 +02001204 unsigned int imbalance;
1205 struct task_group *tg;
Peter Zijlstra83378262008-06-27 13:41:37 +02001206 unsigned long weight;
Mike Galbraithb3137bc2008-05-29 11:11:41 +02001207 int balanced;
Ingo Molnar098fb9d2008-03-16 20:36:10 +01001208
Peter Zijlstrac88d5912009-09-10 13:50:02 +02001209 idx = sd->wake_idx;
1210 this_cpu = smp_processor_id();
1211 prev_cpu = task_cpu(p);
1212 load = source_load(prev_cpu, idx);
1213 this_load = target_load(this_cpu, idx);
Ingo Molnar098fb9d2008-03-16 20:36:10 +01001214
Peter Zijlstrae69b0f12009-09-15 19:38:52 +02001215 if (sync) {
1216 if (sched_feat(SYNC_LESS) &&
1217 (curr->se.avg_overlap > sysctl_sched_migration_cost ||
1218 p->se.avg_overlap > sysctl_sched_migration_cost))
1219 sync = 0;
1220 } else {
1221 if (sched_feat(SYNC_MORE) &&
1222 (curr->se.avg_overlap < sysctl_sched_migration_cost &&
1223 p->se.avg_overlap < sysctl_sched_migration_cost))
1224 sync = 1;
1225 }
Peter Zijlstrafc631c82009-02-11 14:27:17 +01001226
Ingo Molnar098fb9d2008-03-16 20:36:10 +01001227 /*
Ingo Molnar098fb9d2008-03-16 20:36:10 +01001228 * If sync wakeup then subtract the (maximum possible)
1229 * effect of the currently running task from the load
1230 * of the current CPU:
1231 */
Peter Zijlstra83378262008-06-27 13:41:37 +02001232 if (sync) {
1233 tg = task_group(current);
1234 weight = current->se.load.weight;
Ingo Molnar098fb9d2008-03-16 20:36:10 +01001235
Peter Zijlstrac88d5912009-09-10 13:50:02 +02001236 this_load += effective_load(tg, this_cpu, -weight, -weight);
Peter Zijlstra83378262008-06-27 13:41:37 +02001237 load += effective_load(tg, prev_cpu, 0, -weight);
1238 }
1239
1240 tg = task_group(p);
1241 weight = p->se.load.weight;
1242
Peter Zijlstrac88d5912009-09-10 13:50:02 +02001243 imbalance = 100 + (sd->imbalance_pct - 100) / 2;
1244
Peter Zijlstra71a29aa2009-09-07 18:28:05 +02001245 /*
1246 * In low-load situations, where prev_cpu is idle and this_cpu is idle
Peter Zijlstrac88d5912009-09-10 13:50:02 +02001247 * due to the sync cause above having dropped this_load to 0, we'll
1248 * always have an imbalance, but there's really nothing you can do
1249 * about that, so that's good too.
Peter Zijlstra71a29aa2009-09-07 18:28:05 +02001250 *
1251 * Otherwise check if either cpus are near enough in load to allow this
1252 * task to be woken on this_cpu.
1253 */
Peter Zijlstrac88d5912009-09-10 13:50:02 +02001254 balanced = !this_load ||
1255 100*(this_load + effective_load(tg, this_cpu, weight, weight)) <=
Peter Zijlstra83378262008-06-27 13:41:37 +02001256 imbalance*(load + effective_load(tg, prev_cpu, 0, weight));
Mike Galbraithb3137bc2008-05-29 11:11:41 +02001257
1258 /*
1259 * If the currently running task will sleep within
1260 * a reasonable amount of time then attract this newly
1261 * woken task:
1262 */
Peter Zijlstra2fb76352008-10-08 09:16:04 +02001263 if (sync && balanced)
1264 return 1;
Mike Galbraithb3137bc2008-05-29 11:11:41 +02001265
1266 schedstat_inc(p, se.nr_wakeups_affine_attempts);
1267 tl_per_task = cpu_avg_load_per_task(this_cpu);
1268
Peter Zijlstrac88d5912009-09-10 13:50:02 +02001269 if (balanced ||
1270 (this_load <= load &&
1271 this_load + target_load(prev_cpu, idx) <= tl_per_task)) {
Ingo Molnar098fb9d2008-03-16 20:36:10 +01001272 /*
1273 * This domain has SD_WAKE_AFFINE and
1274 * p is cache cold in this domain, and
1275 * there is no bad imbalance.
1276 */
Peter Zijlstrac88d5912009-09-10 13:50:02 +02001277 schedstat_inc(sd, ttwu_move_affine);
Ingo Molnar098fb9d2008-03-16 20:36:10 +01001278 schedstat_inc(p, se.nr_wakeups_affine);
1279
1280 return 1;
1281 }
1282 return 0;
1283}
1284
Peter Zijlstraaaee1202009-09-10 13:36:25 +02001285/*
1286 * find_idlest_group finds and returns the least busy CPU group within the
1287 * domain.
1288 */
1289static struct sched_group *
Peter Zijlstra78e7ed52009-09-03 13:16:51 +02001290find_idlest_group(struct sched_domain *sd, struct task_struct *p,
Peter Zijlstra5158f4e2009-09-16 13:46:59 +02001291 int this_cpu, int load_idx)
Gregory Haskinse7693a32008-01-25 21:08:09 +01001292{
Peter Zijlstraaaee1202009-09-10 13:36:25 +02001293 struct sched_group *idlest = NULL, *this = NULL, *group = sd->groups;
1294 unsigned long min_load = ULONG_MAX, this_load = 0;
Peter Zijlstraaaee1202009-09-10 13:36:25 +02001295 int imbalance = 100 + (sd->imbalance_pct-100)/2;
Gregory Haskinse7693a32008-01-25 21:08:09 +01001296
Peter Zijlstraaaee1202009-09-10 13:36:25 +02001297 do {
1298 unsigned long load, avg_load;
1299 int local_group;
1300 int i;
Gregory Haskinse7693a32008-01-25 21:08:09 +01001301
Peter Zijlstraaaee1202009-09-10 13:36:25 +02001302 /* Skip over this group if it has no CPUs allowed */
1303 if (!cpumask_intersects(sched_group_cpus(group),
1304 &p->cpus_allowed))
1305 continue;
1306
1307 local_group = cpumask_test_cpu(this_cpu,
1308 sched_group_cpus(group));
1309
1310 /* Tally up the load of all CPUs in the group */
1311 avg_load = 0;
1312
1313 for_each_cpu(i, sched_group_cpus(group)) {
1314 /* Bias balancing toward cpus of our domain */
1315 if (local_group)
1316 load = source_load(i, load_idx);
1317 else
1318 load = target_load(i, load_idx);
1319
1320 avg_load += load;
1321 }
1322
1323 /* Adjust by relative CPU power of the group */
1324 avg_load = (avg_load * SCHED_LOAD_SCALE) / group->cpu_power;
1325
1326 if (local_group) {
1327 this_load = avg_load;
1328 this = group;
1329 } else if (avg_load < min_load) {
1330 min_load = avg_load;
1331 idlest = group;
1332 }
1333 } while (group = group->next, group != sd->groups);
1334
1335 if (!idlest || 100*this_load < imbalance*min_load)
1336 return NULL;
1337 return idlest;
1338}
1339
1340/*
1341 * find_idlest_cpu - find the idlest cpu among the cpus in group.
1342 */
1343static int
1344find_idlest_cpu(struct sched_group *group, struct task_struct *p, int this_cpu)
1345{
1346 unsigned long load, min_load = ULONG_MAX;
1347 int idlest = -1;
1348 int i;
1349
1350 /* Traverse only the allowed CPUs */
1351 for_each_cpu_and(i, sched_group_cpus(group), &p->cpus_allowed) {
1352 load = weighted_cpuload(i);
1353
1354 if (load < min_load || (load == min_load && i == this_cpu)) {
1355 min_load = load;
1356 idlest = i;
Gregory Haskinse7693a32008-01-25 21:08:09 +01001357 }
1358 }
1359
Peter Zijlstraaaee1202009-09-10 13:36:25 +02001360 return idlest;
1361}
Gregory Haskinse7693a32008-01-25 21:08:09 +01001362
Peter Zijlstraaaee1202009-09-10 13:36:25 +02001363/*
Peter Zijlstraa50bde52009-11-12 15:55:28 +01001364 * Try and locate an idle CPU in the sched_domain.
1365 */
1366static int
1367select_idle_sibling(struct task_struct *p, struct sched_domain *sd, int target)
1368{
1369 int cpu = smp_processor_id();
1370 int prev_cpu = task_cpu(p);
1371 int i;
1372
1373 /*
1374 * If this domain spans both cpu and prev_cpu (see the SD_WAKE_AFFINE
1375 * test in select_task_rq_fair) and the prev_cpu is idle then that's
1376 * always a better target than the current cpu.
1377 */
Peter Zijlstrafe3bcfe2009-11-12 15:55:29 +01001378 if (target == cpu && !cpu_rq(prev_cpu)->cfs.nr_running)
1379 return prev_cpu;
Peter Zijlstraa50bde52009-11-12 15:55:28 +01001380
1381 /*
1382 * Otherwise, iterate the domain and find an elegible idle cpu.
1383 */
Peter Zijlstrafe3bcfe2009-11-12 15:55:29 +01001384 for_each_cpu_and(i, sched_domain_span(sd), &p->cpus_allowed) {
1385 if (!cpu_rq(i)->cfs.nr_running) {
1386 target = i;
1387 break;
Peter Zijlstraa50bde52009-11-12 15:55:28 +01001388 }
1389 }
1390
1391 return target;
1392}
1393
1394/*
Peter Zijlstraaaee1202009-09-10 13:36:25 +02001395 * sched_balance_self: balance the current task (running on cpu) in domains
1396 * that have the 'flag' flag set. In practice, this is SD_BALANCE_FORK and
1397 * SD_BALANCE_EXEC.
1398 *
1399 * Balance, ie. select the least loaded group.
1400 *
1401 * Returns the target CPU number, or the same CPU if no balancing is needed.
1402 *
1403 * preempt must be disabled.
1404 */
Peter Zijlstra5158f4e2009-09-16 13:46:59 +02001405static int select_task_rq_fair(struct task_struct *p, int sd_flag, int wake_flags)
Peter Zijlstraaaee1202009-09-10 13:36:25 +02001406{
Peter Zijlstra29cd8ba2009-09-17 09:01:14 +02001407 struct sched_domain *tmp, *affine_sd = NULL, *sd = NULL;
Peter Zijlstrac88d5912009-09-10 13:50:02 +02001408 int cpu = smp_processor_id();
1409 int prev_cpu = task_cpu(p);
1410 int new_cpu = cpu;
1411 int want_affine = 0;
Peter Zijlstra29cd8ba2009-09-17 09:01:14 +02001412 int want_sd = 1;
Peter Zijlstra5158f4e2009-09-16 13:46:59 +02001413 int sync = wake_flags & WF_SYNC;
Gregory Haskinse7693a32008-01-25 21:08:09 +01001414
Peter Zijlstra0763a662009-09-14 19:37:39 +02001415 if (sd_flag & SD_BALANCE_WAKE) {
Mike Galbraith3f04e8c2009-09-19 16:52:35 +02001416 if (sched_feat(AFFINE_WAKEUPS) &&
1417 cpumask_test_cpu(cpu, &p->cpus_allowed))
Peter Zijlstrac88d5912009-09-10 13:50:02 +02001418 want_affine = 1;
1419 new_cpu = prev_cpu;
1420 }
Gregory Haskinse7693a32008-01-25 21:08:09 +01001421
Peter Zijlstraaaee1202009-09-10 13:36:25 +02001422 for_each_domain(cpu, tmp) {
1423 /*
Peter Zijlstraae154be2009-09-10 14:40:57 +02001424 * If power savings logic is enabled for a domain, see if we
1425 * are not overloaded, if so, don't balance wider.
Peter Zijlstraaaee1202009-09-10 13:36:25 +02001426 */
Peter Zijlstra59abf022009-09-16 08:28:30 +02001427 if (tmp->flags & (SD_POWERSAVINGS_BALANCE|SD_PREFER_LOCAL)) {
Peter Zijlstraae154be2009-09-10 14:40:57 +02001428 unsigned long power = 0;
1429 unsigned long nr_running = 0;
1430 unsigned long capacity;
1431 int i;
Gregory Haskinse7693a32008-01-25 21:08:09 +01001432
Peter Zijlstraae154be2009-09-10 14:40:57 +02001433 for_each_cpu(i, sched_domain_span(tmp)) {
1434 power += power_of(i);
1435 nr_running += cpu_rq(i)->cfs.nr_running;
1436 }
Gregory Haskinse7693a32008-01-25 21:08:09 +01001437
Peter Zijlstraae154be2009-09-10 14:40:57 +02001438 capacity = DIV_ROUND_CLOSEST(power, SCHED_LOAD_SCALE);
Ingo Molnar4ae7d5c2008-03-19 01:42:00 +01001439
Peter Zijlstra59abf022009-09-16 08:28:30 +02001440 if (tmp->flags & SD_POWERSAVINGS_BALANCE)
1441 nr_running /= 2;
1442
1443 if (nr_running < capacity)
Peter Zijlstra29cd8ba2009-09-17 09:01:14 +02001444 want_sd = 0;
Gregory Haskinse7693a32008-01-25 21:08:09 +01001445 }
Peter Zijlstraaaee1202009-09-10 13:36:25 +02001446
Peter Zijlstrafe3bcfe2009-11-12 15:55:29 +01001447 /*
1448 * While iterating the domains looking for a spanning
1449 * WAKE_AFFINE domain, adjust the affine target to any idle cpu
1450 * in cache sharing domains along the way.
1451 */
1452 if (want_affine) {
Peter Zijlstraa50bde52009-11-12 15:55:28 +01001453 int target = -1;
Mike Galbraitha1f84a32009-10-27 15:35:38 +01001454
1455 /*
Peter Zijlstraa50bde52009-11-12 15:55:28 +01001456 * If both cpu and prev_cpu are part of this domain,
1457 * cpu is a valid SD_WAKE_AFFINE target.
Mike Galbraitha1f84a32009-10-27 15:35:38 +01001458 */
Peter Zijlstraa50bde52009-11-12 15:55:28 +01001459 if (cpumask_test_cpu(prev_cpu, sched_domain_span(tmp)))
1460 target = cpu;
Mike Galbraitha1f84a32009-10-27 15:35:38 +01001461
Peter Zijlstraa50bde52009-11-12 15:55:28 +01001462 /*
1463 * If there's an idle sibling in this domain, make that
1464 * the wake_affine target instead of the current cpu.
Peter Zijlstraa50bde52009-11-12 15:55:28 +01001465 */
1466 if (tmp->flags & SD_PREFER_SIBLING)
1467 target = select_idle_sibling(p, tmp, target);
Mike Galbraitha1f84a32009-10-27 15:35:38 +01001468
Peter Zijlstraa50bde52009-11-12 15:55:28 +01001469 if (target >= 0) {
Peter Zijlstrafe3bcfe2009-11-12 15:55:29 +01001470 if (tmp->flags & SD_WAKE_AFFINE) {
1471 affine_sd = tmp;
1472 want_affine = 0;
1473 }
Peter Zijlstraa50bde52009-11-12 15:55:28 +01001474 cpu = target;
Mike Galbraitha1f84a32009-10-27 15:35:38 +01001475 }
Peter Zijlstrac88d5912009-09-10 13:50:02 +02001476 }
1477
Peter Zijlstra29cd8ba2009-09-17 09:01:14 +02001478 if (!want_sd && !want_affine)
1479 break;
1480
Peter Zijlstra0763a662009-09-14 19:37:39 +02001481 if (!(tmp->flags & sd_flag))
Peter Zijlstrac88d5912009-09-10 13:50:02 +02001482 continue;
1483
Peter Zijlstra29cd8ba2009-09-17 09:01:14 +02001484 if (want_sd)
1485 sd = tmp;
Peter Zijlstrac88d5912009-09-10 13:50:02 +02001486 }
Peter Zijlstraaaee1202009-09-10 13:36:25 +02001487
Peter Zijlstra29cd8ba2009-09-17 09:01:14 +02001488 if (sched_feat(LB_SHARES_UPDATE)) {
1489 /*
1490 * Pick the largest domain to update shares over
1491 */
1492 tmp = sd;
1493 if (affine_sd && (!tmp ||
1494 cpumask_weight(sched_domain_span(affine_sd)) >
1495 cpumask_weight(sched_domain_span(sd))))
1496 tmp = affine_sd;
1497
1498 if (tmp)
1499 update_shares(tmp);
1500 }
1501
Peter Zijlstrafb58bac2009-12-01 12:21:47 +01001502 if (affine_sd && wake_affine(affine_sd, p, sync))
1503 return cpu;
Peter Zijlstra3b640892009-09-16 13:44:33 +02001504
Peter Zijlstraaaee1202009-09-10 13:36:25 +02001505 while (sd) {
Peter Zijlstra5158f4e2009-09-16 13:46:59 +02001506 int load_idx = sd->forkexec_idx;
Peter Zijlstraaaee1202009-09-10 13:36:25 +02001507 struct sched_group *group;
Peter Zijlstrac88d5912009-09-10 13:50:02 +02001508 int weight;
Peter Zijlstraaaee1202009-09-10 13:36:25 +02001509
Peter Zijlstra0763a662009-09-14 19:37:39 +02001510 if (!(sd->flags & sd_flag)) {
Peter Zijlstraaaee1202009-09-10 13:36:25 +02001511 sd = sd->child;
1512 continue;
1513 }
1514
Peter Zijlstra5158f4e2009-09-16 13:46:59 +02001515 if (sd_flag & SD_BALANCE_WAKE)
1516 load_idx = sd->wake_idx;
1517
1518 group = find_idlest_group(sd, p, cpu, load_idx);
Peter Zijlstraaaee1202009-09-10 13:36:25 +02001519 if (!group) {
1520 sd = sd->child;
1521 continue;
1522 }
1523
Peter Zijlstrad7c33c42009-09-11 12:45:38 +02001524 new_cpu = find_idlest_cpu(group, p, cpu);
Peter Zijlstraaaee1202009-09-10 13:36:25 +02001525 if (new_cpu == -1 || new_cpu == cpu) {
1526 /* Now try balancing at a lower domain level of cpu */
1527 sd = sd->child;
1528 continue;
1529 }
1530
1531 /* Now try balancing at a lower domain level of new_cpu */
1532 cpu = new_cpu;
1533 weight = cpumask_weight(sched_domain_span(sd));
1534 sd = NULL;
1535 for_each_domain(cpu, tmp) {
1536 if (weight <= cpumask_weight(sched_domain_span(tmp)))
1537 break;
Peter Zijlstra0763a662009-09-14 19:37:39 +02001538 if (tmp->flags & sd_flag)
Peter Zijlstraaaee1202009-09-10 13:36:25 +02001539 sd = tmp;
1540 }
1541 /* while loop will break here if sd == NULL */
Gregory Haskinse7693a32008-01-25 21:08:09 +01001542 }
1543
Peter Zijlstrac88d5912009-09-10 13:50:02 +02001544 return new_cpu;
Gregory Haskinse7693a32008-01-25 21:08:09 +01001545}
1546#endif /* CONFIG_SMP */
1547
Peter Zijlstrae52fb7c2009-01-14 12:39:19 +01001548/*
1549 * Adaptive granularity
1550 *
1551 * se->avg_wakeup gives the average time a task runs until it does a wakeup,
1552 * with the limit of wakeup_gran -- when it never does a wakeup.
1553 *
1554 * So the smaller avg_wakeup is the faster we want this task to preempt,
1555 * but we don't want to treat the preemptee unfairly and therefore allow it
1556 * to run for at least the amount of time we'd like to run.
1557 *
1558 * NOTE: we use 2*avg_wakeup to increase the probability of actually doing one
1559 *
1560 * NOTE: we use *nr_running to scale with load, this nicely matches the
1561 * degrading latency on load.
1562 */
1563static unsigned long
1564adaptive_gran(struct sched_entity *curr, struct sched_entity *se)
1565{
1566 u64 this_run = curr->sum_exec_runtime - curr->prev_sum_exec_runtime;
1567 u64 expected_wakeup = 2*se->avg_wakeup * cfs_rq_of(se)->nr_running;
1568 u64 gran = 0;
1569
1570 if (this_run < expected_wakeup)
1571 gran = expected_wakeup - this_run;
1572
1573 return min_t(s64, gran, sysctl_sched_wakeup_granularity);
1574}
1575
1576static unsigned long
1577wakeup_gran(struct sched_entity *curr, struct sched_entity *se)
Peter Zijlstra0bbd3332008-04-19 19:44:57 +02001578{
1579 unsigned long gran = sysctl_sched_wakeup_granularity;
1580
Peter Zijlstrae52fb7c2009-01-14 12:39:19 +01001581 if (cfs_rq_of(curr)->curr && sched_feat(ADAPTIVE_GRAN))
1582 gran = adaptive_gran(curr, se);
1583
Peter Zijlstra0bbd3332008-04-19 19:44:57 +02001584 /*
Peter Zijlstrae52fb7c2009-01-14 12:39:19 +01001585 * Since its curr running now, convert the gran from real-time
1586 * to virtual-time in his units.
Peter Zijlstra0bbd3332008-04-19 19:44:57 +02001587 */
Peter Zijlstrae52fb7c2009-01-14 12:39:19 +01001588 if (sched_feat(ASYM_GRAN)) {
1589 /*
1590 * By using 'se' instead of 'curr' we penalize light tasks, so
1591 * they get preempted easier. That is, if 'se' < 'curr' then
1592 * the resulting gran will be larger, therefore penalizing the
1593 * lighter, if otoh 'se' > 'curr' then the resulting gran will
1594 * be smaller, again penalizing the lighter task.
1595 *
1596 * This is especially important for buddies when the leftmost
1597 * task is higher priority than the buddy.
1598 */
1599 if (unlikely(se->load.weight != NICE_0_LOAD))
1600 gran = calc_delta_fair(gran, se);
1601 } else {
1602 if (unlikely(curr->load.weight != NICE_0_LOAD))
1603 gran = calc_delta_fair(gran, curr);
1604 }
Peter Zijlstra0bbd3332008-04-19 19:44:57 +02001605
1606 return gran;
1607}
1608
1609/*
Peter Zijlstra464b7522008-10-24 11:06:15 +02001610 * Should 'se' preempt 'curr'.
1611 *
1612 * |s1
1613 * |s2
1614 * |s3
1615 * g
1616 * |<--->|c
1617 *
1618 * w(c, s1) = -1
1619 * w(c, s2) = 0
1620 * w(c, s3) = 1
1621 *
1622 */
1623static int
1624wakeup_preempt_entity(struct sched_entity *curr, struct sched_entity *se)
1625{
1626 s64 gran, vdiff = curr->vruntime - se->vruntime;
1627
1628 if (vdiff <= 0)
1629 return -1;
1630
Peter Zijlstrae52fb7c2009-01-14 12:39:19 +01001631 gran = wakeup_gran(curr, se);
Peter Zijlstra464b7522008-10-24 11:06:15 +02001632 if (vdiff > gran)
1633 return 1;
1634
1635 return 0;
1636}
1637
Peter Zijlstra02479092008-11-04 21:25:10 +01001638static void set_last_buddy(struct sched_entity *se)
1639{
Peter Zijlstra6bc912b2009-01-15 14:53:38 +01001640 if (likely(task_of(se)->policy != SCHED_IDLE)) {
1641 for_each_sched_entity(se)
1642 cfs_rq_of(se)->last = se;
1643 }
Peter Zijlstra02479092008-11-04 21:25:10 +01001644}
1645
1646static void set_next_buddy(struct sched_entity *se)
1647{
Peter Zijlstra6bc912b2009-01-15 14:53:38 +01001648 if (likely(task_of(se)->policy != SCHED_IDLE)) {
1649 for_each_sched_entity(se)
1650 cfs_rq_of(se)->next = se;
1651 }
Peter Zijlstra02479092008-11-04 21:25:10 +01001652}
1653
Peter Zijlstra464b7522008-10-24 11:06:15 +02001654/*
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001655 * Preempt the current task with a newly woken task if needed:
1656 */
Peter Zijlstra5a9b86f2009-09-16 13:47:58 +02001657static void check_preempt_wakeup(struct rq *rq, struct task_struct *p, int wake_flags)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001658{
1659 struct task_struct *curr = rq->curr;
Srivatsa Vaddagiri8651a862007-10-15 17:00:12 +02001660 struct sched_entity *se = &curr->se, *pse = &p->se;
Mike Galbraith03e89e42008-12-16 08:45:30 +01001661 struct cfs_rq *cfs_rq = task_cfs_rq(curr);
Peter Zijlstra5a9b86f2009-09-16 13:47:58 +02001662 int sync = wake_flags & WF_SYNC;
Mike Galbraithf685cea2009-10-23 23:09:22 +02001663 int scale = cfs_rq->nr_running >= sched_nr_latency;
Mike Galbraith03e89e42008-12-16 08:45:30 +01001664
Peter Zijlstra3a7e73a2009-11-28 18:51:02 +01001665 if (unlikely(rt_prio(p->prio)))
1666 goto preempt;
Peter Zijlstraaa2ac252008-03-14 21:12:12 +01001667
Peter Zijlstrad95f98d2008-11-04 21:25:08 +01001668 if (unlikely(p->sched_class != &fair_sched_class))
1669 return;
1670
Ingo Molnar4ae7d5c2008-03-19 01:42:00 +01001671 if (unlikely(se == pse))
1672 return;
1673
Mike Galbraithf685cea2009-10-23 23:09:22 +02001674 if (sched_feat(NEXT_BUDDY) && scale && !(wake_flags & WF_FORK))
Mike Galbraith3cb63d52009-09-11 12:01:17 +02001675 set_next_buddy(pse);
Peter Zijlstra57fdc262008-09-23 15:33:45 +02001676
Bharata B Raoaec0a512008-08-28 14:42:49 +05301677 /*
1678 * We can come here with TIF_NEED_RESCHED already set from new task
1679 * wake up path.
1680 */
1681 if (test_tsk_need_resched(curr))
1682 return;
1683
Ingo Molnar91c234b2007-10-15 17:00:18 +02001684 /*
Peter Zijlstra6bc912b2009-01-15 14:53:38 +01001685 * Batch and idle tasks do not preempt (their preemption is driven by
Ingo Molnar91c234b2007-10-15 17:00:18 +02001686 * the tick):
1687 */
Peter Zijlstra6bc912b2009-01-15 14:53:38 +01001688 if (unlikely(p->policy != SCHED_NORMAL))
Ingo Molnar91c234b2007-10-15 17:00:18 +02001689 return;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001690
Peter Zijlstra6bc912b2009-01-15 14:53:38 +01001691 /* Idle tasks are by definition preempted by everybody. */
Peter Zijlstra3a7e73a2009-11-28 18:51:02 +01001692 if (unlikely(curr->policy == SCHED_IDLE))
1693 goto preempt;
Peter Zijlstra6bc912b2009-01-15 14:53:38 +01001694
Peter Zijlstra3a7e73a2009-11-28 18:51:02 +01001695 if (sched_feat(WAKEUP_SYNC) && sync)
1696 goto preempt;
Peter Zijlstra15afe092008-09-20 23:38:02 +02001697
Peter Zijlstra3a7e73a2009-11-28 18:51:02 +01001698 if (sched_feat(WAKEUP_OVERLAP) &&
1699 se->avg_overlap < sysctl_sched_migration_cost &&
1700 pse->avg_overlap < sysctl_sched_migration_cost)
1701 goto preempt;
1702
Peter Zijlstraad4b78b2009-09-16 12:31:31 +02001703 if (!sched_feat(WAKEUP_PREEMPT))
1704 return;
1705
Jupyung Leea65ac742009-11-17 18:51:40 +09001706 update_curr(cfs_rq);
Peter Zijlstra3a7e73a2009-11-28 18:51:02 +01001707 find_matching_se(&se, &pse);
1708 BUG_ON(!pse);
1709 if (wakeup_preempt_entity(se, pse) == 1)
1710 goto preempt;
Jupyung Leea65ac742009-11-17 18:51:40 +09001711
Peter Zijlstra3a7e73a2009-11-28 18:51:02 +01001712 return;
1713
1714preempt:
1715 resched_task(curr);
1716 /*
1717 * Only set the backward buddy when the current task is still
1718 * on the rq. This can happen when a wakeup gets interleaved
1719 * with schedule on the ->pre_schedule() or idle_balance()
1720 * point, either of which can * drop the rq lock.
1721 *
1722 * Also, during early boot the idle thread is in the fair class,
1723 * for obvious reasons its a bad idea to schedule back to it.
1724 */
1725 if (unlikely(!se->on_rq || curr == rq->idle))
1726 return;
1727
1728 if (sched_feat(LAST_BUDDY) && scale && entity_is_task(se))
1729 set_last_buddy(se);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001730}
1731
Ingo Molnarfb8d4722007-08-09 11:16:48 +02001732static struct task_struct *pick_next_task_fair(struct rq *rq)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001733{
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001734 struct task_struct *p;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001735 struct cfs_rq *cfs_rq = &rq->cfs;
1736 struct sched_entity *se;
1737
Tim Blechmann36ace272009-11-24 11:55:45 +01001738 if (!cfs_rq->nr_running)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001739 return NULL;
1740
1741 do {
Ingo Molnar9948f4b2007-08-09 11:16:48 +02001742 se = pick_next_entity(cfs_rq);
Peter Zijlstraf4b67552008-11-04 21:25:07 +01001743 set_next_entity(cfs_rq, se);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001744 cfs_rq = group_cfs_rq(se);
1745 } while (cfs_rq);
1746
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001747 p = task_of(se);
1748 hrtick_start_fair(rq, p);
1749
1750 return p;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001751}
1752
1753/*
1754 * Account for a descheduled task:
1755 */
Ingo Molnar31ee5292007-08-09 11:16:49 +02001756static void put_prev_task_fair(struct rq *rq, struct task_struct *prev)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001757{
1758 struct sched_entity *se = &prev->se;
1759 struct cfs_rq *cfs_rq;
1760
1761 for_each_sched_entity(se) {
1762 cfs_rq = cfs_rq_of(se);
Ingo Molnarab6cde22007-08-09 11:16:48 +02001763 put_prev_entity(cfs_rq, se);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001764 }
1765}
1766
Peter Williams681f3e62007-10-24 18:23:51 +02001767#ifdef CONFIG_SMP
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001768/**************************************************
1769 * Fair scheduling class load-balancing methods:
1770 */
1771
1772/*
1773 * Load-balancing iterator. Note: while the runqueue stays locked
1774 * during the whole iteration, the current task might be
1775 * dequeued so the iterator has to be dequeue-safe. Here we
1776 * achieve that by always pre-iterating before returning
1777 * the current task:
1778 */
Alexey Dobriyana9957442007-10-15 17:00:13 +02001779static struct task_struct *
Peter Zijlstra4a55bd52008-04-19 19:45:00 +02001780__load_balance_iterator(struct cfs_rq *cfs_rq, struct list_head *next)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001781{
Dhaval Giani354d60c2008-04-19 19:44:59 +02001782 struct task_struct *p = NULL;
1783 struct sched_entity *se;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001784
Mike Galbraith77ae6512008-08-11 13:32:02 +02001785 if (next == &cfs_rq->tasks)
1786 return NULL;
1787
Bharata B Raob87f1722008-09-25 09:53:54 +05301788 se = list_entry(next, struct sched_entity, group_node);
1789 p = task_of(se);
1790 cfs_rq->balance_iterator = next->next;
Mike Galbraith77ae6512008-08-11 13:32:02 +02001791
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001792 return p;
1793}
1794
1795static struct task_struct *load_balance_start_fair(void *arg)
1796{
1797 struct cfs_rq *cfs_rq = arg;
1798
Peter Zijlstra4a55bd52008-04-19 19:45:00 +02001799 return __load_balance_iterator(cfs_rq, cfs_rq->tasks.next);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001800}
1801
1802static struct task_struct *load_balance_next_fair(void *arg)
1803{
1804 struct cfs_rq *cfs_rq = arg;
1805
Peter Zijlstra4a55bd52008-04-19 19:45:00 +02001806 return __load_balance_iterator(cfs_rq, cfs_rq->balance_iterator);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001807}
1808
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001809static unsigned long
1810__load_balance_fair(struct rq *this_rq, int this_cpu, struct rq *busiest,
1811 unsigned long max_load_move, struct sched_domain *sd,
1812 enum cpu_idle_type idle, int *all_pinned, int *this_best_prio,
1813 struct cfs_rq *cfs_rq)
Peter Zijlstra62fb1852008-02-25 17:34:02 +01001814{
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001815 struct rq_iterator cfs_rq_iterator;
Ingo Molnar6363ca52008-05-29 11:28:57 +02001816
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001817 cfs_rq_iterator.start = load_balance_start_fair;
1818 cfs_rq_iterator.next = load_balance_next_fair;
1819 cfs_rq_iterator.arg = cfs_rq;
Ingo Molnar6363ca52008-05-29 11:28:57 +02001820
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001821 return balance_tasks(this_rq, this_cpu, busiest,
1822 max_load_move, sd, idle, all_pinned,
1823 this_best_prio, &cfs_rq_iterator);
Ingo Molnar6363ca52008-05-29 11:28:57 +02001824}
Ingo Molnar6363ca52008-05-29 11:28:57 +02001825
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001826#ifdef CONFIG_FAIR_GROUP_SCHED
Ingo Molnar6363ca52008-05-29 11:28:57 +02001827static unsigned long
1828load_balance_fair(struct rq *this_rq, int this_cpu, struct rq *busiest,
1829 unsigned long max_load_move,
1830 struct sched_domain *sd, enum cpu_idle_type idle,
1831 int *all_pinned, int *this_best_prio)
1832{
Ingo Molnar6363ca52008-05-29 11:28:57 +02001833 long rem_load_move = max_load_move;
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001834 int busiest_cpu = cpu_of(busiest);
1835 struct task_group *tg;
Peter Zijlstra62fb1852008-02-25 17:34:02 +01001836
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001837 rcu_read_lock();
Peter Zijlstrac8cba852008-06-27 13:41:23 +02001838 update_h_load(busiest_cpu);
Peter Zijlstra62fb1852008-02-25 17:34:02 +01001839
Chris Friesencaea8a02008-09-22 11:06:09 -06001840 list_for_each_entry_rcu(tg, &task_groups, list) {
Peter Zijlstrac8cba852008-06-27 13:41:23 +02001841 struct cfs_rq *busiest_cfs_rq = tg->cfs_rq[busiest_cpu];
Peter Zijlstra42a3ac72008-06-27 13:41:29 +02001842 unsigned long busiest_h_load = busiest_cfs_rq->h_load;
1843 unsigned long busiest_weight = busiest_cfs_rq->load.weight;
Srivatsa Vaddagiri243e0e72008-06-27 13:41:36 +02001844 u64 rem_load, moved_load;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001845
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001846 /*
1847 * empty group
1848 */
Peter Zijlstrac8cba852008-06-27 13:41:23 +02001849 if (!busiest_cfs_rq->task_weight)
Ingo Molnar6363ca52008-05-29 11:28:57 +02001850 continue;
1851
Srivatsa Vaddagiri243e0e72008-06-27 13:41:36 +02001852 rem_load = (u64)rem_load_move * busiest_weight;
1853 rem_load = div_u64(rem_load, busiest_h_load + 1);
Ingo Molnar6363ca52008-05-29 11:28:57 +02001854
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001855 moved_load = __load_balance_fair(this_rq, this_cpu, busiest,
Srivatsa Vaddagiri53fecd82008-06-27 13:41:20 +02001856 rem_load, sd, idle, all_pinned, this_best_prio,
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001857 tg->cfs_rq[busiest_cpu]);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001858
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001859 if (!moved_load)
1860 continue;
1861
Peter Zijlstra42a3ac72008-06-27 13:41:29 +02001862 moved_load *= busiest_h_load;
Srivatsa Vaddagiri243e0e72008-06-27 13:41:36 +02001863 moved_load = div_u64(moved_load, busiest_weight + 1);
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001864
1865 rem_load_move -= moved_load;
1866 if (rem_load_move < 0)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001867 break;
1868 }
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001869 rcu_read_unlock();
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001870
Peter Williams43010652007-08-09 11:16:46 +02001871 return max_load_move - rem_load_move;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001872}
Peter Zijlstrac09595f2008-06-27 13:41:14 +02001873#else
1874static unsigned long
1875load_balance_fair(struct rq *this_rq, int this_cpu, struct rq *busiest,
1876 unsigned long max_load_move,
1877 struct sched_domain *sd, enum cpu_idle_type idle,
1878 int *all_pinned, int *this_best_prio)
1879{
1880 return __load_balance_fair(this_rq, this_cpu, busiest,
1881 max_load_move, sd, idle, all_pinned,
1882 this_best_prio, &busiest->cfs);
1883}
1884#endif
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001885
Peter Williamse1d14842007-10-24 18:23:51 +02001886static int
1887move_one_task_fair(struct rq *this_rq, int this_cpu, struct rq *busiest,
1888 struct sched_domain *sd, enum cpu_idle_type idle)
1889{
1890 struct cfs_rq *busy_cfs_rq;
1891 struct rq_iterator cfs_rq_iterator;
1892
1893 cfs_rq_iterator.start = load_balance_start_fair;
1894 cfs_rq_iterator.next = load_balance_next_fair;
1895
1896 for_each_leaf_cfs_rq(busiest, busy_cfs_rq) {
1897 /*
1898 * pass busy_cfs_rq argument into
1899 * load_balance_[start|next]_fair iterators
1900 */
1901 cfs_rq_iterator.arg = busy_cfs_rq;
1902 if (iter_move_one_task(this_rq, this_cpu, busiest, sd, idle,
1903 &cfs_rq_iterator))
1904 return 1;
1905 }
1906
1907 return 0;
1908}
Christian Ehrhardt0bcdcf22009-11-30 12:16:46 +01001909
1910static void rq_online_fair(struct rq *rq)
1911{
1912 update_sysctl();
1913}
1914
1915static void rq_offline_fair(struct rq *rq)
1916{
1917 update_sysctl();
1918}
1919
Dhaval Giani55e12e52008-06-24 23:39:43 +05301920#endif /* CONFIG_SMP */
Peter Williamse1d14842007-10-24 18:23:51 +02001921
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001922/*
1923 * scheduler tick hitting a task of our scheduling class:
1924 */
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001925static void task_tick_fair(struct rq *rq, struct task_struct *curr, int queued)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001926{
1927 struct cfs_rq *cfs_rq;
1928 struct sched_entity *se = &curr->se;
1929
1930 for_each_sched_entity(se) {
1931 cfs_rq = cfs_rq_of(se);
Peter Zijlstra8f4d37e2008-01-25 21:08:29 +01001932 entity_tick(cfs_rq, se, queued);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001933 }
1934}
1935
1936/*
Peter Zijlstracd29fe62009-11-27 17:32:46 +01001937 * called on fork with the child task as argument from the parent's context
1938 * - child not yet on the tasklist
1939 * - preemption disabled
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001940 */
Peter Zijlstracd29fe62009-11-27 17:32:46 +01001941static void task_fork_fair(struct task_struct *p)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001942{
Peter Zijlstracd29fe62009-11-27 17:32:46 +01001943 struct cfs_rq *cfs_rq = task_cfs_rq(current);
Ingo Molnar429d43b2007-10-15 17:00:03 +02001944 struct sched_entity *se = &p->se, *curr = cfs_rq->curr;
Ingo Molnar00bf7bf2007-10-15 17:00:14 +02001945 int this_cpu = smp_processor_id();
Peter Zijlstracd29fe62009-11-27 17:32:46 +01001946 struct rq *rq = this_rq();
1947 unsigned long flags;
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001948
Peter Zijlstracd29fe62009-11-27 17:32:46 +01001949 spin_lock_irqsave(&rq->lock, flags);
1950
1951 if (unlikely(task_cpu(p) != this_cpu))
1952 __set_task_cpu(p, this_cpu);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001953
Ting Yang7109c4422007-08-28 12:53:24 +02001954 update_curr(cfs_rq);
Peter Zijlstracd29fe62009-11-27 17:32:46 +01001955
Mike Galbraithb5d9d732009-09-08 11:12:28 +02001956 if (curr)
1957 se->vruntime = curr->vruntime;
Peter Zijlstraaeb73b02007-10-15 17:00:05 +02001958 place_entity(cfs_rq, se, 1);
Peter Zijlstra4d78e7b2007-10-15 17:00:04 +02001959
Peter Zijlstracd29fe62009-11-27 17:32:46 +01001960 if (sysctl_sched_child_runs_first && curr && entity_before(curr, se)) {
Dmitry Adamushko87fefa32007-10-15 17:00:08 +02001961 /*
Ingo Molnaredcb60a2007-10-15 17:00:08 +02001962 * Upon rescheduling, sched_class::put_prev_task() will place
1963 * 'current' within the tree based on its new key value.
1964 */
Peter Zijlstra4d78e7b2007-10-15 17:00:04 +02001965 swap(curr->vruntime, se->vruntime);
Bharata B Raoaec0a512008-08-28 14:42:49 +05301966 resched_task(rq->curr);
Peter Zijlstra4d78e7b2007-10-15 17:00:04 +02001967 }
1968
Peter Zijlstracd29fe62009-11-27 17:32:46 +01001969 spin_unlock_irqrestore(&rq->lock, flags);
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02001970}
1971
Steven Rostedtcb469842008-01-25 21:08:22 +01001972/*
1973 * Priority of the task has changed. Check to see if we preempt
1974 * the current task.
1975 */
1976static void prio_changed_fair(struct rq *rq, struct task_struct *p,
1977 int oldprio, int running)
1978{
1979 /*
1980 * Reschedule if we are currently running on this runqueue and
1981 * our priority decreased, or if we are not currently running on
1982 * this runqueue and our priority is higher than the current's
1983 */
1984 if (running) {
1985 if (p->prio > oldprio)
1986 resched_task(rq->curr);
1987 } else
Peter Zijlstra15afe092008-09-20 23:38:02 +02001988 check_preempt_curr(rq, p, 0);
Steven Rostedtcb469842008-01-25 21:08:22 +01001989}
1990
1991/*
1992 * We switched to the sched_fair class.
1993 */
1994static void switched_to_fair(struct rq *rq, struct task_struct *p,
1995 int running)
1996{
1997 /*
1998 * We were most likely switched from sched_rt, so
1999 * kick off the schedule if running, otherwise just see
2000 * if we can still preempt the current task.
2001 */
2002 if (running)
2003 resched_task(rq->curr);
2004 else
Peter Zijlstra15afe092008-09-20 23:38:02 +02002005 check_preempt_curr(rq, p, 0);
Steven Rostedtcb469842008-01-25 21:08:22 +01002006}
2007
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +02002008/* Account for a task changing its policy or group.
2009 *
2010 * This routine is mostly called to set cfs_rq->curr field when a task
2011 * migrates between groups/classes.
2012 */
2013static void set_curr_task_fair(struct rq *rq)
2014{
2015 struct sched_entity *se = &rq->curr->se;
2016
2017 for_each_sched_entity(se)
2018 set_next_entity(cfs_rq_of(se), se);
2019}
2020
Peter Zijlstra810b3812008-02-29 15:21:01 -05002021#ifdef CONFIG_FAIR_GROUP_SCHED
2022static void moved_group_fair(struct task_struct *p)
2023{
2024 struct cfs_rq *cfs_rq = task_cfs_rq(p);
2025
2026 update_curr(cfs_rq);
2027 place_entity(cfs_rq, &p->se, 1);
2028}
2029#endif
2030
Thomas Gleixnerdba091b2009-12-09 09:32:03 +01002031unsigned int get_rr_interval_fair(struct rq *rq, struct task_struct *task)
Peter Williams0d721ce2009-09-21 01:31:53 +00002032{
2033 struct sched_entity *se = &task->se;
Peter Williams0d721ce2009-09-21 01:31:53 +00002034 unsigned int rr_interval = 0;
2035
2036 /*
2037 * Time slice is 0 for SCHED_OTHER tasks that are on an otherwise
2038 * idle runqueue:
2039 */
Peter Williams0d721ce2009-09-21 01:31:53 +00002040 if (rq->cfs.load.weight)
2041 rr_interval = NS_TO_JIFFIES(sched_slice(&rq->cfs, se));
Peter Williams0d721ce2009-09-21 01:31:53 +00002042
2043 return rr_interval;
2044}
2045
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002046/*
2047 * All the scheduling class methods:
2048 */
Ingo Molnar5522d5d2007-10-15 17:00:12 +02002049static const struct sched_class fair_sched_class = {
2050 .next = &idle_sched_class,
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002051 .enqueue_task = enqueue_task_fair,
2052 .dequeue_task = dequeue_task_fair,
2053 .yield_task = yield_task_fair,
2054
Ingo Molnar2e09bf52007-10-15 17:00:05 +02002055 .check_preempt_curr = check_preempt_wakeup,
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002056
2057 .pick_next_task = pick_next_task_fair,
2058 .put_prev_task = put_prev_task_fair,
2059
Peter Williams681f3e62007-10-24 18:23:51 +02002060#ifdef CONFIG_SMP
Li Zefan4ce72a22008-10-22 15:25:26 +08002061 .select_task_rq = select_task_rq_fair,
2062
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002063 .load_balance = load_balance_fair,
Peter Williamse1d14842007-10-24 18:23:51 +02002064 .move_one_task = move_one_task_fair,
Christian Ehrhardt0bcdcf22009-11-30 12:16:46 +01002065 .rq_online = rq_online_fair,
2066 .rq_offline = rq_offline_fair,
Peter Williams681f3e62007-10-24 18:23:51 +02002067#endif
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002068
Srivatsa Vaddagiri83b699e2007-10-15 17:00:08 +02002069 .set_curr_task = set_curr_task_fair,
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002070 .task_tick = task_tick_fair,
Peter Zijlstracd29fe62009-11-27 17:32:46 +01002071 .task_fork = task_fork_fair,
Steven Rostedtcb469842008-01-25 21:08:22 +01002072
2073 .prio_changed = prio_changed_fair,
2074 .switched_to = switched_to_fair,
Peter Zijlstra810b3812008-02-29 15:21:01 -05002075
Peter Williams0d721ce2009-09-21 01:31:53 +00002076 .get_rr_interval = get_rr_interval_fair,
2077
Peter Zijlstra810b3812008-02-29 15:21:01 -05002078#ifdef CONFIG_FAIR_GROUP_SCHED
2079 .moved_group = moved_group_fair,
2080#endif
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002081};
2082
2083#ifdef CONFIG_SCHED_DEBUG
Ingo Molnar5cef9ec2007-08-09 11:16:47 +02002084static void print_cfs_stats(struct seq_file *m, int cpu)
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002085{
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002086 struct cfs_rq *cfs_rq;
2087
Peter Zijlstra5973e5b2008-01-25 21:08:34 +01002088 rcu_read_lock();
Ingo Molnarc3b64f12007-08-09 11:16:51 +02002089 for_each_leaf_cfs_rq(cpu_rq(cpu), cfs_rq)
Ingo Molnar5cef9ec2007-08-09 11:16:47 +02002090 print_cfs_rq(m, cpu, cfs_rq);
Peter Zijlstra5973e5b2008-01-25 21:08:34 +01002091 rcu_read_unlock();
Ingo Molnarbf0f6f22007-07-09 18:51:58 +02002092}
2093#endif