aboutsummaryrefslogtreecommitdiffstats
path: root/kernel/sched_fair.c
diff options
context:
space:
mode:
authorPeter Zijlstra <a.p.zijlstra@chello.nl>2007-08-25 12:41:53 -0400
committerIngo Molnar <mingo@elte.hu>2007-08-25 12:41:53 -0400
commit218050855ece4e923106ab614ac65afa0f618df3 (patch)
treef7b1234ce9e8ad0bc5d5af949949251240ec6a2c /kernel/sched_fair.c
parent1fc84aaae3bae9646dd4c7798b8c0ff934338909 (diff)
sched: adaptive scheduler granularity
Instead of specifying the preemption granularity, specify the wanted latency. By fixing the granlarity to a constany the wakeup latency it a function of the number of running tasks on the rq. Invert this relation. sysctl_sched_granularity becomes a minimum for the dynamic granularity computed from the new sysctl_sched_latency. Then use this latency to do more intelligent granularity decisions: if there are fewer tasks running then we can schedule coarser. This helps performance while still always keeping the latency target. Signed-off-by: Peter Zijlstra <a.p.zijlstra@chello.nl> Signed-off-by: Ingo Molnar <mingo@elte.hu>
Diffstat (limited to 'kernel/sched_fair.c')
-rw-r--r--kernel/sched_fair.c77
1 files changed, 65 insertions, 12 deletions
diff --git a/kernel/sched_fair.c b/kernel/sched_fair.c
index 4d6b7e2df2aa..0ba1e60f08d0 100644
--- a/kernel/sched_fair.c
+++ b/kernel/sched_fair.c
@@ -15,23 +15,32 @@
15 * 15 *
16 * Scaled math optimizations by Thomas Gleixner 16 * Scaled math optimizations by Thomas Gleixner
17 * Copyright (C) 2007, Thomas Gleixner <tglx@linutronix.de> 17 * Copyright (C) 2007, Thomas Gleixner <tglx@linutronix.de>
18 *
19 * Adaptive scheduling granularity, math enhancements by Peter Zijlstra
20 * Copyright (C) 2007 Red Hat, Inc., Peter Zijlstra <pzijlstr@redhat.com>
18 */ 21 */
19 22
20/* 23/*
21 * Preemption granularity: 24 * Targeted preemption latency for CPU-bound tasks:
22 * (default: 10 msec, units: nanoseconds) 25 * (default: 20ms, units: nanoseconds)
23 * 26 *
24 * NOTE: this granularity value is not the same as the concept of 27 * NOTE: this latency value is not the same as the concept of
25 * 'timeslice length' - timeslices in CFS will typically be somewhat 28 * 'timeslice length' - timeslices in CFS are of variable length.
26 * larger than this value. (to see the precise effective timeslice 29 * (to see the precise effective timeslice length of your workload,
27 * length of your workload, run vmstat and monitor the context-switches 30 * run vmstat and monitor the context-switches field)
28 * field)
29 * 31 *
30 * On SMP systems the value of this is multiplied by the log2 of the 32 * On SMP systems the value of this is multiplied by the log2 of the
31 * number of CPUs. (i.e. factor 2x on 2-way systems, 3x on 4-way 33 * number of CPUs. (i.e. factor 2x on 2-way systems, 3x on 4-way
32 * systems, 4x on 8-way systems, 5x on 16-way systems, etc.) 34 * systems, 4x on 8-way systems, 5x on 16-way systems, etc.)
35 * Targeted preemption latency for CPU-bound tasks:
33 */ 36 */
34unsigned int sysctl_sched_granularity __read_mostly = 10000000UL; 37unsigned int sysctl_sched_latency __read_mostly = 20000000ULL;
38
39/*
40 * Minimal preemption granularity for CPU-bound tasks:
41 * (default: 2 msec, units: nanoseconds)
42 */
43unsigned int sysctl_sched_granularity __read_mostly = 2000000ULL;
35 44
36/* 45/*
37 * SCHED_BATCH wake-up granularity. 46 * SCHED_BATCH wake-up granularity.
@@ -213,6 +222,49 @@ static struct sched_entity *__pick_next_entity(struct cfs_rq *cfs_rq)
213 */ 222 */
214 223
215/* 224/*
225 * Calculate the preemption granularity needed to schedule every
226 * runnable task once per sysctl_sched_latency amount of time.
227 * (down to a sensible low limit on granularity)
228 *
229 * For example, if there are 2 tasks running and latency is 10 msecs,
230 * we switch tasks every 5 msecs. If we have 3 tasks running, we have
231 * to switch tasks every 3.33 msecs to get a 10 msecs observed latency
232 * for each task. We do finer and finer scheduling up to until we
233 * reach the minimum granularity value.
234 *
235 * To achieve this we use the following dynamic-granularity rule:
236 *
237 * gran = lat/nr - lat/nr/nr
238 *
239 * This comes out of the following equations:
240 *
241 * kA1 + gran = kB1
242 * kB2 + gran = kA2
243 * kA2 = kA1
244 * kB2 = kB1 - d + d/nr
245 * lat = d * nr
246 *
247 * Where 'k' is key, 'A' is task A (waiting), 'B' is task B (running),
248 * '1' is start of time, '2' is end of time, 'd' is delay between
249 * 1 and 2 (during which task B was running), 'nr' is number of tasks
250 * running, 'lat' is the the period of each task. ('lat' is the
251 * sched_latency that we aim for.)
252 */
253static long
254sched_granularity(struct cfs_rq *cfs_rq)
255{
256 unsigned int gran = sysctl_sched_latency;
257 unsigned int nr = cfs_rq->nr_running;
258
259 if (nr > 1) {
260 gran = gran/nr - gran/nr/nr;
261 gran = max(gran, sysctl_sched_granularity);
262 }
263
264 return gran;
265}
266
267/*
216 * We rescale the rescheduling granularity of tasks according to their 268 * We rescale the rescheduling granularity of tasks according to their
217 * nice level, but only linearly, not exponentially: 269 * nice level, but only linearly, not exponentially:
218 */ 270 */
@@ -302,7 +354,7 @@ __update_curr(struct cfs_rq *cfs_rq, struct sched_entity *curr)
302 delta_fair = calc_delta_fair(delta_exec, lw); 354 delta_fair = calc_delta_fair(delta_exec, lw);
303 delta_mine = calc_delta_mine(delta_exec, curr->load.weight, lw); 355 delta_mine = calc_delta_mine(delta_exec, curr->load.weight, lw);
304 356
305 if (cfs_rq->sleeper_bonus > sysctl_sched_granularity) { 357 if (cfs_rq->sleeper_bonus > sysctl_sched_latency) {
306 delta = min((u64)delta_mine, cfs_rq->sleeper_bonus); 358 delta = min((u64)delta_mine, cfs_rq->sleeper_bonus);
307 delta = min(delta, (unsigned long)( 359 delta = min(delta, (unsigned long)(
308 (long)sysctl_sched_runtime_limit - curr->wait_runtime)); 360 (long)sysctl_sched_runtime_limit - curr->wait_runtime));
@@ -689,7 +741,8 @@ static void entity_tick(struct cfs_rq *cfs_rq, struct sched_entity *curr)
689 if (next == curr) 741 if (next == curr)
690 return; 742 return;
691 743
692 __check_preempt_curr_fair(cfs_rq, next, curr, sysctl_sched_granularity); 744 __check_preempt_curr_fair(cfs_rq, next, curr,
745 sched_granularity(cfs_rq));
693} 746}
694 747
695/************************************************** 748/**************************************************
@@ -1034,7 +1087,7 @@ static void task_new_fair(struct rq *rq, struct task_struct *p)
1034 * it will preempt the parent: 1087 * it will preempt the parent:
1035 */ 1088 */
1036 p->se.fair_key = current->se.fair_key - 1089 p->se.fair_key = current->se.fair_key -
1037 niced_granularity(&rq->curr->se, sysctl_sched_granularity) - 1; 1090 niced_granularity(&rq->curr->se, sched_granularity(cfs_rq)) - 1;
1038 /* 1091 /*
1039 * The first wait is dominated by the child-runs-first logic, 1092 * The first wait is dominated by the child-runs-first logic,
1040 * so do not credit it with that waiting time yet: 1093 * so do not credit it with that waiting time yet:
@@ -1047,7 +1100,7 @@ static void task_new_fair(struct rq *rq, struct task_struct *p)
1047 * -granularity/2, so initialize the task with that: 1100 * -granularity/2, so initialize the task with that:
1048 */ 1101 */
1049 if (sysctl_sched_features & SCHED_FEAT_START_DEBIT) 1102 if (sysctl_sched_features & SCHED_FEAT_START_DEBIT)
1050 p->se.wait_runtime = -((long)sysctl_sched_granularity / 2); 1103 p->se.wait_runtime = -(sched_granularity(cfs_rq) / 2);
1051 1104
1052 __enqueue_entity(cfs_rq, se); 1105 __enqueue_entity(cfs_rq, se);
1053} 1106}