Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 1 | /* |
| 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 Zijlstra | 2180508 | 2007-08-25 18:41:53 +0200 | [diff] [blame] | 18 | * |
| 19 | * Adaptive scheduling granularity, math enhancements by Peter Zijlstra |
| 20 | * Copyright (C) 2007 Red Hat, Inc., Peter Zijlstra <pzijlstr@redhat.com> |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 21 | */ |
| 22 | |
| 23 | /* |
Ingo Molnar | 2bd8e6d | 2007-10-15 17:00:02 +0200 | [diff] [blame] | 24 | * Tunables that become constants when CONFIG_SCHED_DEBUG is off: |
| 25 | */ |
| 26 | #ifdef CONFIG_SCHED_DEBUG |
| 27 | # define const_debug __read_mostly |
| 28 | #else |
| 29 | # define const_debug static const |
| 30 | #endif |
| 31 | |
| 32 | /* |
Peter Zijlstra | 2180508 | 2007-08-25 18:41:53 +0200 | [diff] [blame] | 33 | * Targeted preemption latency for CPU-bound tasks: |
| 34 | * (default: 20ms, units: nanoseconds) |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 35 | * |
Peter Zijlstra | 2180508 | 2007-08-25 18:41:53 +0200 | [diff] [blame] | 36 | * NOTE: this latency value is not the same as the concept of |
| 37 | * 'timeslice length' - timeslices in CFS are of variable length. |
| 38 | * (to see the precise effective timeslice length of your workload, |
| 39 | * run vmstat and monitor the context-switches field) |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 40 | * |
| 41 | * On SMP systems the value of this is multiplied by the log2 of the |
| 42 | * number of CPUs. (i.e. factor 2x on 2-way systems, 3x on 4-way |
| 43 | * systems, 4x on 8-way systems, 5x on 16-way systems, etc.) |
Peter Zijlstra | 2180508 | 2007-08-25 18:41:53 +0200 | [diff] [blame] | 44 | * Targeted preemption latency for CPU-bound tasks: |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 45 | */ |
Ingo Molnar | 2bd8e6d | 2007-10-15 17:00:02 +0200 | [diff] [blame] | 46 | const_debug unsigned int sysctl_sched_latency = 20000000ULL; |
| 47 | |
| 48 | /* |
| 49 | * After fork, child runs first. (default) If set to 0 then |
| 50 | * parent will (try to) run first. |
| 51 | */ |
| 52 | const_debug unsigned int sysctl_sched_child_runs_first = 1; |
Peter Zijlstra | 2180508 | 2007-08-25 18:41:53 +0200 | [diff] [blame] | 53 | |
| 54 | /* |
| 55 | * Minimal preemption granularity for CPU-bound tasks: |
| 56 | * (default: 2 msec, units: nanoseconds) |
| 57 | */ |
Ingo Molnar | 172ac3d | 2007-08-25 18:41:53 +0200 | [diff] [blame] | 58 | unsigned int sysctl_sched_min_granularity __read_mostly = 2000000ULL; |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 59 | |
| 60 | /* |
Ingo Molnar | 1799e35 | 2007-09-19 23:34:46 +0200 | [diff] [blame] | 61 | * sys_sched_yield() compat mode |
| 62 | * |
| 63 | * This option switches the agressive yield implementation of the |
| 64 | * old scheduler back on. |
| 65 | */ |
| 66 | unsigned int __read_mostly sysctl_sched_compat_yield; |
| 67 | |
| 68 | /* |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 69 | * SCHED_BATCH wake-up granularity. |
Ingo Molnar | 71fd371 | 2007-08-24 20:39:10 +0200 | [diff] [blame] | 70 | * (default: 25 msec, units: nanoseconds) |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 71 | * |
| 72 | * This option delays the preemption effects of decoupled workloads |
| 73 | * and reduces their over-scheduling. Synchronous workloads will still |
| 74 | * have immediate wakeup/sleep latencies. |
| 75 | */ |
Ingo Molnar | 2bd8e6d | 2007-10-15 17:00:02 +0200 | [diff] [blame] | 76 | const_debug unsigned int sysctl_sched_batch_wakeup_granularity = 25000000UL; |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 77 | |
| 78 | /* |
| 79 | * SCHED_OTHER wake-up granularity. |
| 80 | * (default: 1 msec, units: nanoseconds) |
| 81 | * |
| 82 | * This option delays the preemption effects of decoupled workloads |
| 83 | * and reduces their over-scheduling. Synchronous workloads will still |
| 84 | * have immediate wakeup/sleep latencies. |
| 85 | */ |
Ingo Molnar | 2bd8e6d | 2007-10-15 17:00:02 +0200 | [diff] [blame] | 86 | const_debug unsigned int sysctl_sched_wakeup_granularity = 1000000UL; |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 87 | |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 88 | unsigned int sysctl_sched_runtime_limit __read_mostly; |
| 89 | |
| 90 | /* |
| 91 | * Debugging: various feature bits |
| 92 | */ |
| 93 | enum { |
| 94 | SCHED_FEAT_FAIR_SLEEPERS = 1, |
Ingo Molnar | e9acbff | 2007-10-15 17:00:04 +0200 | [diff] [blame^] | 95 | SCHED_FEAT_NEW_FAIR_SLEEPERS = 2, |
| 96 | SCHED_FEAT_SLEEPER_AVG = 4, |
| 97 | SCHED_FEAT_SLEEPER_LOAD_AVG = 8, |
| 98 | SCHED_FEAT_START_DEBIT = 16, |
| 99 | SCHED_FEAT_SKIP_INITIAL = 32, |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 100 | }; |
| 101 | |
Ingo Molnar | 2bd8e6d | 2007-10-15 17:00:02 +0200 | [diff] [blame] | 102 | const_debug unsigned int sysctl_sched_features = |
Ingo Molnar | e9acbff | 2007-10-15 17:00:04 +0200 | [diff] [blame^] | 103 | SCHED_FEAT_FAIR_SLEEPERS *0 | |
| 104 | SCHED_FEAT_NEW_FAIR_SLEEPERS *1 | |
Ingo Molnar | 5d2b3d3 | 2007-08-12 18:08:19 +0200 | [diff] [blame] | 105 | SCHED_FEAT_SLEEPER_AVG *0 | |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 106 | SCHED_FEAT_SLEEPER_LOAD_AVG *1 | |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 107 | SCHED_FEAT_START_DEBIT *1 | |
| 108 | SCHED_FEAT_SKIP_INITIAL *0; |
| 109 | |
Peter Zijlstra | e59c80c | 2007-10-15 17:00:03 +0200 | [diff] [blame] | 110 | #define sched_feat(x) (sysctl_sched_features & SCHED_FEAT_##x) |
| 111 | |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 112 | extern struct sched_class fair_sched_class; |
| 113 | |
| 114 | /************************************************************** |
| 115 | * CFS operations on generic schedulable entities: |
| 116 | */ |
| 117 | |
| 118 | #ifdef CONFIG_FAIR_GROUP_SCHED |
| 119 | |
| 120 | /* cpu runqueue to which this cfs_rq is attached */ |
| 121 | static inline struct rq *rq_of(struct cfs_rq *cfs_rq) |
| 122 | { |
| 123 | return cfs_rq->rq; |
| 124 | } |
| 125 | |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 126 | /* An entity is a task if it doesn't "own" a runqueue */ |
| 127 | #define entity_is_task(se) (!se->my_q) |
| 128 | |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 129 | #else /* CONFIG_FAIR_GROUP_SCHED */ |
| 130 | |
| 131 | static inline struct rq *rq_of(struct cfs_rq *cfs_rq) |
| 132 | { |
| 133 | return container_of(cfs_rq, struct rq, cfs); |
| 134 | } |
| 135 | |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 136 | #define entity_is_task(se) 1 |
| 137 | |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 138 | #endif /* CONFIG_FAIR_GROUP_SCHED */ |
| 139 | |
| 140 | static inline struct task_struct *task_of(struct sched_entity *se) |
| 141 | { |
| 142 | return container_of(se, struct task_struct, se); |
| 143 | } |
| 144 | |
| 145 | |
| 146 | /************************************************************** |
| 147 | * Scheduling class tree data structure manipulation methods: |
| 148 | */ |
| 149 | |
Ingo Molnar | e9acbff | 2007-10-15 17:00:04 +0200 | [diff] [blame^] | 150 | static inline void |
| 151 | set_leftmost(struct cfs_rq *cfs_rq, struct rb_node *leftmost) |
| 152 | { |
| 153 | struct sched_entity *se; |
| 154 | |
| 155 | cfs_rq->rb_leftmost = leftmost; |
| 156 | if (leftmost) { |
| 157 | se = rb_entry(leftmost, struct sched_entity, run_node); |
| 158 | cfs_rq->min_vruntime = max(se->vruntime, |
| 159 | cfs_rq->min_vruntime); |
| 160 | } |
| 161 | } |
| 162 | |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 163 | /* |
| 164 | * Enqueue an entity into the rb-tree: |
| 165 | */ |
Ingo Molnar | 19ccd97 | 2007-10-15 17:00:04 +0200 | [diff] [blame] | 166 | static void |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 167 | __enqueue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se) |
| 168 | { |
| 169 | struct rb_node **link = &cfs_rq->tasks_timeline.rb_node; |
| 170 | struct rb_node *parent = NULL; |
| 171 | struct sched_entity *entry; |
| 172 | s64 key = se->fair_key; |
| 173 | int leftmost = 1; |
| 174 | |
| 175 | /* |
| 176 | * Find the right place in the rbtree: |
| 177 | */ |
| 178 | while (*link) { |
| 179 | parent = *link; |
| 180 | entry = rb_entry(parent, struct sched_entity, run_node); |
| 181 | /* |
| 182 | * We dont care about collisions. Nodes with |
| 183 | * the same key stay together. |
| 184 | */ |
| 185 | if (key - entry->fair_key < 0) { |
| 186 | link = &parent->rb_left; |
| 187 | } else { |
| 188 | link = &parent->rb_right; |
| 189 | leftmost = 0; |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | /* |
| 194 | * Maintain a cache of leftmost tree entries (it is frequently |
| 195 | * used): |
| 196 | */ |
| 197 | if (leftmost) |
Ingo Molnar | e9acbff | 2007-10-15 17:00:04 +0200 | [diff] [blame^] | 198 | set_leftmost(cfs_rq, &se->run_node); |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 199 | |
| 200 | rb_link_node(&se->run_node, parent, link); |
| 201 | rb_insert_color(&se->run_node, &cfs_rq->tasks_timeline); |
| 202 | update_load_add(&cfs_rq->load, se->load.weight); |
| 203 | cfs_rq->nr_running++; |
| 204 | se->on_rq = 1; |
Ingo Molnar | a206c07 | 2007-09-05 14:32:49 +0200 | [diff] [blame] | 205 | |
| 206 | schedstat_add(cfs_rq, wait_runtime, se->wait_runtime); |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 207 | } |
| 208 | |
Ingo Molnar | 19ccd97 | 2007-10-15 17:00:04 +0200 | [diff] [blame] | 209 | static void |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 210 | __dequeue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se) |
| 211 | { |
| 212 | if (cfs_rq->rb_leftmost == &se->run_node) |
Ingo Molnar | e9acbff | 2007-10-15 17:00:04 +0200 | [diff] [blame^] | 213 | set_leftmost(cfs_rq, rb_next(&se->run_node)); |
| 214 | |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 215 | rb_erase(&se->run_node, &cfs_rq->tasks_timeline); |
| 216 | update_load_sub(&cfs_rq->load, se->load.weight); |
| 217 | cfs_rq->nr_running--; |
| 218 | se->on_rq = 0; |
Ingo Molnar | a206c07 | 2007-09-05 14:32:49 +0200 | [diff] [blame] | 219 | |
| 220 | schedstat_add(cfs_rq, wait_runtime, -se->wait_runtime); |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 221 | } |
| 222 | |
| 223 | static inline struct rb_node *first_fair(struct cfs_rq *cfs_rq) |
| 224 | { |
| 225 | return cfs_rq->rb_leftmost; |
| 226 | } |
| 227 | |
| 228 | static struct sched_entity *__pick_next_entity(struct cfs_rq *cfs_rq) |
| 229 | { |
| 230 | return rb_entry(first_fair(cfs_rq), struct sched_entity, run_node); |
| 231 | } |
| 232 | |
| 233 | /************************************************************** |
| 234 | * Scheduling class statistics methods: |
| 235 | */ |
| 236 | |
| 237 | /* |
Peter Zijlstra | 2180508 | 2007-08-25 18:41:53 +0200 | [diff] [blame] | 238 | * Calculate the preemption granularity needed to schedule every |
| 239 | * runnable task once per sysctl_sched_latency amount of time. |
| 240 | * (down to a sensible low limit on granularity) |
| 241 | * |
| 242 | * For example, if there are 2 tasks running and latency is 10 msecs, |
| 243 | * we switch tasks every 5 msecs. If we have 3 tasks running, we have |
| 244 | * to switch tasks every 3.33 msecs to get a 10 msecs observed latency |
| 245 | * for each task. We do finer and finer scheduling up to until we |
| 246 | * reach the minimum granularity value. |
| 247 | * |
| 248 | * To achieve this we use the following dynamic-granularity rule: |
| 249 | * |
| 250 | * gran = lat/nr - lat/nr/nr |
| 251 | * |
| 252 | * This comes out of the following equations: |
| 253 | * |
| 254 | * kA1 + gran = kB1 |
| 255 | * kB2 + gran = kA2 |
| 256 | * kA2 = kA1 |
| 257 | * kB2 = kB1 - d + d/nr |
| 258 | * lat = d * nr |
| 259 | * |
| 260 | * Where 'k' is key, 'A' is task A (waiting), 'B' is task B (running), |
| 261 | * '1' is start of time, '2' is end of time, 'd' is delay between |
| 262 | * 1 and 2 (during which task B was running), 'nr' is number of tasks |
| 263 | * running, 'lat' is the the period of each task. ('lat' is the |
| 264 | * sched_latency that we aim for.) |
| 265 | */ |
| 266 | static long |
| 267 | sched_granularity(struct cfs_rq *cfs_rq) |
| 268 | { |
| 269 | unsigned int gran = sysctl_sched_latency; |
| 270 | unsigned int nr = cfs_rq->nr_running; |
| 271 | |
| 272 | if (nr > 1) { |
| 273 | gran = gran/nr - gran/nr/nr; |
Ingo Molnar | 172ac3d | 2007-08-25 18:41:53 +0200 | [diff] [blame] | 274 | gran = max(gran, sysctl_sched_min_granularity); |
Peter Zijlstra | 2180508 | 2007-08-25 18:41:53 +0200 | [diff] [blame] | 275 | } |
| 276 | |
| 277 | return gran; |
| 278 | } |
| 279 | |
| 280 | /* |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 281 | * We rescale the rescheduling granularity of tasks according to their |
| 282 | * nice level, but only linearly, not exponentially: |
| 283 | */ |
| 284 | static long |
| 285 | niced_granularity(struct sched_entity *curr, unsigned long granularity) |
| 286 | { |
| 287 | u64 tmp; |
| 288 | |
Ingo Molnar | 7cff8cf | 2007-08-09 11:16:52 +0200 | [diff] [blame] | 289 | if (likely(curr->load.weight == NICE_0_LOAD)) |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 290 | return granularity; |
| 291 | /* |
Ingo Molnar | 7cff8cf | 2007-08-09 11:16:52 +0200 | [diff] [blame] | 292 | * Positive nice levels get the same granularity as nice-0: |
| 293 | */ |
| 294 | if (likely(curr->load.weight < NICE_0_LOAD)) { |
| 295 | tmp = curr->load.weight * (u64)granularity; |
| 296 | return (long) (tmp >> NICE_0_SHIFT); |
| 297 | } |
| 298 | /* |
| 299 | * Negative nice level tasks get linearly finer |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 300 | * granularity: |
| 301 | */ |
Ingo Molnar | 7cff8cf | 2007-08-09 11:16:52 +0200 | [diff] [blame] | 302 | tmp = curr->load.inv_weight * (u64)granularity; |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 303 | |
| 304 | /* |
| 305 | * It will always fit into 'long': |
| 306 | */ |
Ingo Molnar | a0dc726 | 2007-09-05 14:32:49 +0200 | [diff] [blame] | 307 | return (long) (tmp >> (WMULT_SHIFT-NICE_0_SHIFT)); |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 308 | } |
| 309 | |
| 310 | static inline void |
| 311 | limit_wait_runtime(struct cfs_rq *cfs_rq, struct sched_entity *se) |
| 312 | { |
| 313 | long limit = sysctl_sched_runtime_limit; |
| 314 | |
| 315 | /* |
| 316 | * Niced tasks have the same history dynamic range as |
| 317 | * non-niced tasks: |
| 318 | */ |
| 319 | if (unlikely(se->wait_runtime > limit)) { |
| 320 | se->wait_runtime = limit; |
| 321 | schedstat_inc(se, wait_runtime_overruns); |
| 322 | schedstat_inc(cfs_rq, wait_runtime_overruns); |
| 323 | } |
| 324 | if (unlikely(se->wait_runtime < -limit)) { |
| 325 | se->wait_runtime = -limit; |
| 326 | schedstat_inc(se, wait_runtime_underruns); |
| 327 | schedstat_inc(cfs_rq, wait_runtime_underruns); |
| 328 | } |
| 329 | } |
| 330 | |
| 331 | static inline void |
| 332 | __add_wait_runtime(struct cfs_rq *cfs_rq, struct sched_entity *se, long delta) |
| 333 | { |
| 334 | se->wait_runtime += delta; |
| 335 | schedstat_add(se, sum_wait_runtime, delta); |
| 336 | limit_wait_runtime(cfs_rq, se); |
| 337 | } |
| 338 | |
| 339 | static void |
| 340 | add_wait_runtime(struct cfs_rq *cfs_rq, struct sched_entity *se, long delta) |
| 341 | { |
| 342 | schedstat_add(cfs_rq, wait_runtime, -se->wait_runtime); |
| 343 | __add_wait_runtime(cfs_rq, se, delta); |
| 344 | schedstat_add(cfs_rq, wait_runtime, se->wait_runtime); |
| 345 | } |
| 346 | |
| 347 | /* |
| 348 | * Update the current task's runtime statistics. Skip current tasks that |
| 349 | * are not in our scheduling class. |
| 350 | */ |
| 351 | static inline void |
Ingo Molnar | 8ebc91d | 2007-10-15 17:00:03 +0200 | [diff] [blame] | 352 | __update_curr(struct cfs_rq *cfs_rq, struct sched_entity *curr, |
| 353 | unsigned long delta_exec) |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 354 | { |
Ingo Molnar | e9acbff | 2007-10-15 17:00:04 +0200 | [diff] [blame^] | 355 | unsigned long delta, delta_fair, delta_mine, delta_exec_weighted; |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 356 | struct load_weight *lw = &cfs_rq->load; |
| 357 | unsigned long load = lw->weight; |
| 358 | |
Ingo Molnar | 8179ca23 | 2007-08-02 17:41:40 +0200 | [diff] [blame] | 359 | schedstat_set(curr->exec_max, max((u64)delta_exec, curr->exec_max)); |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 360 | |
| 361 | curr->sum_exec_runtime += delta_exec; |
| 362 | cfs_rq->exec_clock += delta_exec; |
Ingo Molnar | e9acbff | 2007-10-15 17:00:04 +0200 | [diff] [blame^] | 363 | delta_exec_weighted = delta_exec; |
| 364 | if (unlikely(curr->load.weight != NICE_0_LOAD)) { |
| 365 | delta_exec_weighted = calc_delta_fair(delta_exec_weighted, |
| 366 | &curr->load); |
| 367 | } |
| 368 | curr->vruntime += delta_exec_weighted; |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 369 | |
Ingo Molnar | fd8bb43 | 2007-08-09 11:16:46 +0200 | [diff] [blame] | 370 | if (unlikely(!load)) |
| 371 | return; |
| 372 | |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 373 | delta_fair = calc_delta_fair(delta_exec, lw); |
| 374 | delta_mine = calc_delta_mine(delta_exec, curr->load.weight, lw); |
| 375 | |
Mike Galbraith | 5f01d51 | 2007-08-28 12:53:24 +0200 | [diff] [blame] | 376 | if (cfs_rq->sleeper_bonus > sysctl_sched_min_granularity) { |
Peter Zijlstra | ea0aa3b | 2007-08-24 20:39:10 +0200 | [diff] [blame] | 377 | delta = min((u64)delta_mine, cfs_rq->sleeper_bonus); |
Ingo Molnar | b2133c8 | 2007-08-24 20:39:10 +0200 | [diff] [blame] | 378 | delta = min(delta, (unsigned long)( |
| 379 | (long)sysctl_sched_runtime_limit - curr->wait_runtime)); |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 380 | cfs_rq->sleeper_bonus -= delta; |
| 381 | delta_mine -= delta; |
| 382 | } |
| 383 | |
| 384 | cfs_rq->fair_clock += delta_fair; |
| 385 | /* |
| 386 | * We executed delta_exec amount of time on the CPU, |
| 387 | * but we were only entitled to delta_mine amount of |
| 388 | * time during that period (if nr_running == 1 then |
| 389 | * the two values are equal) |
| 390 | * [Note: delta_mine - delta_exec is negative]: |
| 391 | */ |
| 392 | add_wait_runtime(cfs_rq, curr, delta_mine - delta_exec); |
| 393 | } |
| 394 | |
Ingo Molnar | b7cc089 | 2007-08-09 11:16:47 +0200 | [diff] [blame] | 395 | static void update_curr(struct cfs_rq *cfs_rq) |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 396 | { |
Ingo Molnar | 429d43b | 2007-10-15 17:00:03 +0200 | [diff] [blame] | 397 | struct sched_entity *curr = cfs_rq->curr; |
Ingo Molnar | 8ebc91d | 2007-10-15 17:00:03 +0200 | [diff] [blame] | 398 | u64 now = rq_of(cfs_rq)->clock; |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 399 | unsigned long delta_exec; |
| 400 | |
| 401 | if (unlikely(!curr)) |
| 402 | return; |
| 403 | |
| 404 | /* |
| 405 | * Get the amount of time the current task was running |
| 406 | * since the last time we changed load (this cannot |
| 407 | * overflow on 32 bits): |
| 408 | */ |
Ingo Molnar | 8ebc91d | 2007-10-15 17:00:03 +0200 | [diff] [blame] | 409 | delta_exec = (unsigned long)(now - curr->exec_start); |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 410 | |
Ingo Molnar | 8ebc91d | 2007-10-15 17:00:03 +0200 | [diff] [blame] | 411 | __update_curr(cfs_rq, curr, delta_exec); |
| 412 | curr->exec_start = now; |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 413 | } |
| 414 | |
| 415 | static inline void |
Ingo Molnar | 5870db5 | 2007-08-09 11:16:47 +0200 | [diff] [blame] | 416 | update_stats_wait_start(struct cfs_rq *cfs_rq, struct sched_entity *se) |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 417 | { |
| 418 | se->wait_start_fair = cfs_rq->fair_clock; |
Ingo Molnar | d281918 | 2007-08-09 11:16:47 +0200 | [diff] [blame] | 419 | schedstat_set(se->wait_start, rq_of(cfs_rq)->clock); |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 420 | } |
| 421 | |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 422 | static inline unsigned long |
Ingo Molnar | 08e2388 | 2007-10-15 17:00:04 +0200 | [diff] [blame] | 423 | calc_weighted(unsigned long delta, struct sched_entity *se) |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 424 | { |
Ingo Molnar | 08e2388 | 2007-10-15 17:00:04 +0200 | [diff] [blame] | 425 | unsigned long weight = se->load.weight; |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 426 | |
Ingo Molnar | 08e2388 | 2007-10-15 17:00:04 +0200 | [diff] [blame] | 427 | if (unlikely(weight != NICE_0_LOAD)) |
| 428 | return (u64)delta * se->load.weight >> NICE_0_SHIFT; |
| 429 | else |
| 430 | return delta; |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 431 | } |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 432 | |
| 433 | /* |
| 434 | * Task is being enqueued - update stats: |
| 435 | */ |
Ingo Molnar | d2417e5 | 2007-08-09 11:16:47 +0200 | [diff] [blame] | 436 | static void update_stats_enqueue(struct cfs_rq *cfs_rq, struct sched_entity *se) |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 437 | { |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 438 | /* |
| 439 | * Are we enqueueing a waiting task? (for current tasks |
| 440 | * a dequeue/enqueue event is a NOP) |
| 441 | */ |
Ingo Molnar | 429d43b | 2007-10-15 17:00:03 +0200 | [diff] [blame] | 442 | if (se != cfs_rq->curr) |
Ingo Molnar | 5870db5 | 2007-08-09 11:16:47 +0200 | [diff] [blame] | 443 | update_stats_wait_start(cfs_rq, se); |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 444 | /* |
| 445 | * Update the key: |
| 446 | */ |
Ingo Molnar | e9acbff | 2007-10-15 17:00:04 +0200 | [diff] [blame^] | 447 | se->fair_key = se->vruntime; |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 448 | } |
| 449 | |
| 450 | /* |
| 451 | * Note: must be called with a freshly updated rq->fair_clock. |
| 452 | */ |
| 453 | static inline void |
Ingo Molnar | 8ebc91d | 2007-10-15 17:00:03 +0200 | [diff] [blame] | 454 | __update_stats_wait_end(struct cfs_rq *cfs_rq, struct sched_entity *se, |
| 455 | unsigned long delta_fair) |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 456 | { |
Ingo Molnar | d281918 | 2007-08-09 11:16:47 +0200 | [diff] [blame] | 457 | schedstat_set(se->wait_max, max(se->wait_max, |
| 458 | rq_of(cfs_rq)->clock - se->wait_start)); |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 459 | |
Ingo Molnar | 08e2388 | 2007-10-15 17:00:04 +0200 | [diff] [blame] | 460 | delta_fair = calc_weighted(delta_fair, se); |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 461 | |
| 462 | add_wait_runtime(cfs_rq, se, delta_fair); |
| 463 | } |
| 464 | |
| 465 | static void |
Ingo Molnar | 9ef0a96 | 2007-08-09 11:16:47 +0200 | [diff] [blame] | 466 | update_stats_wait_end(struct cfs_rq *cfs_rq, struct sched_entity *se) |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 467 | { |
| 468 | unsigned long delta_fair; |
| 469 | |
Ingo Molnar | b77d69d | 2007-08-28 12:53:24 +0200 | [diff] [blame] | 470 | if (unlikely(!se->wait_start_fair)) |
| 471 | return; |
| 472 | |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 473 | delta_fair = (unsigned long)min((u64)(2*sysctl_sched_runtime_limit), |
| 474 | (u64)(cfs_rq->fair_clock - se->wait_start_fair)); |
| 475 | |
Ingo Molnar | 8ebc91d | 2007-10-15 17:00:03 +0200 | [diff] [blame] | 476 | __update_stats_wait_end(cfs_rq, se, delta_fair); |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 477 | |
| 478 | se->wait_start_fair = 0; |
Ingo Molnar | 6cfb0d5 | 2007-08-02 17:41:40 +0200 | [diff] [blame] | 479 | schedstat_set(se->wait_start, 0); |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 480 | } |
| 481 | |
| 482 | static inline void |
Ingo Molnar | 19b6a2e | 2007-08-09 11:16:48 +0200 | [diff] [blame] | 483 | update_stats_dequeue(struct cfs_rq *cfs_rq, struct sched_entity *se) |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 484 | { |
Ingo Molnar | b7cc089 | 2007-08-09 11:16:47 +0200 | [diff] [blame] | 485 | update_curr(cfs_rq); |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 486 | /* |
| 487 | * Mark the end of the wait period if dequeueing a |
| 488 | * waiting task: |
| 489 | */ |
Ingo Molnar | 429d43b | 2007-10-15 17:00:03 +0200 | [diff] [blame] | 490 | if (se != cfs_rq->curr) |
Ingo Molnar | 9ef0a96 | 2007-08-09 11:16:47 +0200 | [diff] [blame] | 491 | update_stats_wait_end(cfs_rq, se); |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 492 | } |
| 493 | |
| 494 | /* |
| 495 | * We are picking a new current task - update its stats: |
| 496 | */ |
| 497 | static inline void |
Ingo Molnar | 79303e9 | 2007-08-09 11:16:47 +0200 | [diff] [blame] | 498 | update_stats_curr_start(struct cfs_rq *cfs_rq, struct sched_entity *se) |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 499 | { |
| 500 | /* |
| 501 | * We are starting a new run period: |
| 502 | */ |
Ingo Molnar | d281918 | 2007-08-09 11:16:47 +0200 | [diff] [blame] | 503 | se->exec_start = rq_of(cfs_rq)->clock; |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 504 | } |
| 505 | |
| 506 | /* |
| 507 | * We are descheduling a task - update its stats: |
| 508 | */ |
| 509 | static inline void |
Ingo Molnar | c7e9b5b | 2007-08-09 11:16:48 +0200 | [diff] [blame] | 510 | update_stats_curr_end(struct cfs_rq *cfs_rq, struct sched_entity *se) |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 511 | { |
| 512 | se->exec_start = 0; |
| 513 | } |
| 514 | |
| 515 | /************************************************** |
| 516 | * Scheduling class queueing methods: |
| 517 | */ |
| 518 | |
Ingo Molnar | 8ebc91d | 2007-10-15 17:00:03 +0200 | [diff] [blame] | 519 | static void __enqueue_sleeper(struct cfs_rq *cfs_rq, struct sched_entity *se, |
| 520 | unsigned long delta_fair) |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 521 | { |
Ingo Molnar | 8ebc91d | 2007-10-15 17:00:03 +0200 | [diff] [blame] | 522 | unsigned long load = cfs_rq->load.weight; |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 523 | long prev_runtime; |
| 524 | |
Ingo Molnar | b2133c8 | 2007-08-24 20:39:10 +0200 | [diff] [blame] | 525 | /* |
| 526 | * Do not boost sleepers if there's too much bonus 'in flight' |
| 527 | * already: |
| 528 | */ |
| 529 | if (unlikely(cfs_rq->sleeper_bonus > sysctl_sched_runtime_limit)) |
| 530 | return; |
| 531 | |
Peter Zijlstra | e59c80c | 2007-10-15 17:00:03 +0200 | [diff] [blame] | 532 | if (sched_feat(SLEEPER_LOAD_AVG)) |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 533 | load = rq_of(cfs_rq)->cpu_load[2]; |
| 534 | |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 535 | /* |
| 536 | * Fix up delta_fair with the effect of us running |
| 537 | * during the whole sleep period: |
| 538 | */ |
Peter Zijlstra | e59c80c | 2007-10-15 17:00:03 +0200 | [diff] [blame] | 539 | if (sched_feat(SLEEPER_AVG)) |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 540 | delta_fair = div64_likely32((u64)delta_fair * load, |
| 541 | load + se->load.weight); |
| 542 | |
Ingo Molnar | 08e2388 | 2007-10-15 17:00:04 +0200 | [diff] [blame] | 543 | delta_fair = calc_weighted(delta_fair, se); |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 544 | |
| 545 | prev_runtime = se->wait_runtime; |
| 546 | __add_wait_runtime(cfs_rq, se, delta_fair); |
| 547 | delta_fair = se->wait_runtime - prev_runtime; |
| 548 | |
| 549 | /* |
| 550 | * Track the amount of bonus we've given to sleepers: |
| 551 | */ |
| 552 | cfs_rq->sleeper_bonus += delta_fair; |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 553 | } |
| 554 | |
Ingo Molnar | 2396af6 | 2007-08-09 11:16:48 +0200 | [diff] [blame] | 555 | static void enqueue_sleeper(struct cfs_rq *cfs_rq, struct sched_entity *se) |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 556 | { |
| 557 | struct task_struct *tsk = task_of(se); |
| 558 | unsigned long delta_fair; |
| 559 | |
| 560 | if ((entity_is_task(se) && tsk->policy == SCHED_BATCH) || |
Peter Zijlstra | e59c80c | 2007-10-15 17:00:03 +0200 | [diff] [blame] | 561 | !sched_feat(FAIR_SLEEPERS)) |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 562 | return; |
| 563 | |
| 564 | delta_fair = (unsigned long)min((u64)(2*sysctl_sched_runtime_limit), |
| 565 | (u64)(cfs_rq->fair_clock - se->sleep_start_fair)); |
| 566 | |
Ingo Molnar | 8ebc91d | 2007-10-15 17:00:03 +0200 | [diff] [blame] | 567 | __enqueue_sleeper(cfs_rq, se, delta_fair); |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 568 | |
| 569 | se->sleep_start_fair = 0; |
| 570 | |
| 571 | #ifdef CONFIG_SCHEDSTATS |
| 572 | if (se->sleep_start) { |
Ingo Molnar | d281918 | 2007-08-09 11:16:47 +0200 | [diff] [blame] | 573 | u64 delta = rq_of(cfs_rq)->clock - se->sleep_start; |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 574 | |
| 575 | if ((s64)delta < 0) |
| 576 | delta = 0; |
| 577 | |
| 578 | if (unlikely(delta > se->sleep_max)) |
| 579 | se->sleep_max = delta; |
| 580 | |
| 581 | se->sleep_start = 0; |
| 582 | se->sum_sleep_runtime += delta; |
| 583 | } |
| 584 | if (se->block_start) { |
Ingo Molnar | d281918 | 2007-08-09 11:16:47 +0200 | [diff] [blame] | 585 | u64 delta = rq_of(cfs_rq)->clock - se->block_start; |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 586 | |
| 587 | if ((s64)delta < 0) |
| 588 | delta = 0; |
| 589 | |
| 590 | if (unlikely(delta > se->block_max)) |
| 591 | se->block_max = delta; |
| 592 | |
| 593 | se->block_start = 0; |
| 594 | se->sum_sleep_runtime += delta; |
Ingo Molnar | 30084fb | 2007-10-02 14:13:08 +0200 | [diff] [blame] | 595 | |
| 596 | /* |
| 597 | * Blocking time is in units of nanosecs, so shift by 20 to |
| 598 | * get a milliseconds-range estimation of the amount of |
| 599 | * time that the task spent sleeping: |
| 600 | */ |
| 601 | if (unlikely(prof_on == SLEEP_PROFILING)) { |
| 602 | profile_hits(SLEEP_PROFILING, (void *)get_wchan(tsk), |
| 603 | delta >> 20); |
| 604 | } |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 605 | } |
| 606 | #endif |
| 607 | } |
| 608 | |
| 609 | static void |
Ingo Molnar | 668031c | 2007-08-09 11:16:48 +0200 | [diff] [blame] | 610 | enqueue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se, int wakeup) |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 611 | { |
| 612 | /* |
| 613 | * Update the fair clock. |
| 614 | */ |
Ingo Molnar | b7cc089 | 2007-08-09 11:16:47 +0200 | [diff] [blame] | 615 | update_curr(cfs_rq); |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 616 | |
Ingo Molnar | e9acbff | 2007-10-15 17:00:04 +0200 | [diff] [blame^] | 617 | if (wakeup) { |
| 618 | u64 min_runtime, latency; |
| 619 | |
| 620 | min_runtime = cfs_rq->min_vruntime; |
| 621 | min_runtime += sysctl_sched_latency/2; |
| 622 | |
| 623 | if (sched_feat(NEW_FAIR_SLEEPERS)) { |
| 624 | latency = calc_weighted(sysctl_sched_latency, se); |
| 625 | if (min_runtime > latency) |
| 626 | min_runtime -= latency; |
| 627 | } |
| 628 | |
| 629 | se->vruntime = max(se->vruntime, min_runtime); |
| 630 | |
Ingo Molnar | 2396af6 | 2007-08-09 11:16:48 +0200 | [diff] [blame] | 631 | enqueue_sleeper(cfs_rq, se); |
Ingo Molnar | e9acbff | 2007-10-15 17:00:04 +0200 | [diff] [blame^] | 632 | } |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 633 | |
Ingo Molnar | d2417e5 | 2007-08-09 11:16:47 +0200 | [diff] [blame] | 634 | update_stats_enqueue(cfs_rq, se); |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 635 | __enqueue_entity(cfs_rq, se); |
| 636 | } |
| 637 | |
| 638 | static void |
Ingo Molnar | 525c271 | 2007-08-09 11:16:48 +0200 | [diff] [blame] | 639 | dequeue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se, int sleep) |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 640 | { |
Ingo Molnar | 19b6a2e | 2007-08-09 11:16:48 +0200 | [diff] [blame] | 641 | update_stats_dequeue(cfs_rq, se); |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 642 | if (sleep) { |
| 643 | se->sleep_start_fair = cfs_rq->fair_clock; |
| 644 | #ifdef CONFIG_SCHEDSTATS |
| 645 | if (entity_is_task(se)) { |
| 646 | struct task_struct *tsk = task_of(se); |
| 647 | |
| 648 | if (tsk->state & TASK_INTERRUPTIBLE) |
Ingo Molnar | d281918 | 2007-08-09 11:16:47 +0200 | [diff] [blame] | 649 | se->sleep_start = rq_of(cfs_rq)->clock; |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 650 | if (tsk->state & TASK_UNINTERRUPTIBLE) |
Ingo Molnar | d281918 | 2007-08-09 11:16:47 +0200 | [diff] [blame] | 651 | se->block_start = rq_of(cfs_rq)->clock; |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 652 | } |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 653 | #endif |
| 654 | } |
| 655 | __dequeue_entity(cfs_rq, se); |
| 656 | } |
| 657 | |
| 658 | /* |
| 659 | * Preempt the current task with a newly woken task if needed: |
| 660 | */ |
Peter Zijlstra | 7c92e54 | 2007-09-05 14:32:49 +0200 | [diff] [blame] | 661 | static void |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 662 | __check_preempt_curr_fair(struct cfs_rq *cfs_rq, struct sched_entity *se, |
| 663 | struct sched_entity *curr, unsigned long granularity) |
| 664 | { |
| 665 | s64 __delta = curr->fair_key - se->fair_key; |
Peter Zijlstra | 1169783 | 2007-09-05 14:32:49 +0200 | [diff] [blame] | 666 | unsigned long ideal_runtime, delta_exec; |
| 667 | |
| 668 | /* |
| 669 | * ideal_runtime is compared against sum_exec_runtime, which is |
| 670 | * walltime, hence do not scale. |
| 671 | */ |
| 672 | ideal_runtime = max(sysctl_sched_latency / cfs_rq->nr_running, |
| 673 | (unsigned long)sysctl_sched_min_granularity); |
| 674 | |
| 675 | /* |
| 676 | * If we executed more than what the latency constraint suggests, |
| 677 | * reduce the rescheduling granularity. This way the total latency |
| 678 | * of how much a task is not scheduled converges to |
| 679 | * sysctl_sched_latency: |
| 680 | */ |
| 681 | delta_exec = curr->sum_exec_runtime - curr->prev_sum_exec_runtime; |
| 682 | if (delta_exec > ideal_runtime) |
| 683 | granularity = 0; |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 684 | |
| 685 | /* |
| 686 | * Take scheduling granularity into account - do not |
| 687 | * preempt the current task unless the best task has |
| 688 | * a larger than sched_granularity fairness advantage: |
Peter Zijlstra | 1169783 | 2007-09-05 14:32:49 +0200 | [diff] [blame] | 689 | * |
| 690 | * scale granularity as key space is in fair_clock. |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 691 | */ |
Peter Zijlstra | 4a55b45 | 2007-09-05 14:32:49 +0200 | [diff] [blame] | 692 | if (__delta > niced_granularity(curr, granularity)) |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 693 | resched_task(rq_of(cfs_rq)->curr); |
| 694 | } |
| 695 | |
| 696 | static inline void |
Ingo Molnar | 8494f41 | 2007-08-09 11:16:48 +0200 | [diff] [blame] | 697 | set_next_entity(struct cfs_rq *cfs_rq, struct sched_entity *se) |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 698 | { |
| 699 | /* |
| 700 | * Any task has to be enqueued before it get to execute on |
| 701 | * a CPU. So account for the time it spent waiting on the |
| 702 | * runqueue. (note, here we rely on pick_next_task() having |
| 703 | * done a put_prev_task_fair() shortly before this, which |
| 704 | * updated rq->fair_clock - used by update_stats_wait_end()) |
| 705 | */ |
Ingo Molnar | 9ef0a96 | 2007-08-09 11:16:47 +0200 | [diff] [blame] | 706 | update_stats_wait_end(cfs_rq, se); |
Ingo Molnar | 79303e9 | 2007-08-09 11:16:47 +0200 | [diff] [blame] | 707 | update_stats_curr_start(cfs_rq, se); |
Ingo Molnar | 429d43b | 2007-10-15 17:00:03 +0200 | [diff] [blame] | 708 | cfs_rq->curr = se; |
Ingo Molnar | eba1ed4 | 2007-10-15 17:00:02 +0200 | [diff] [blame] | 709 | #ifdef CONFIG_SCHEDSTATS |
| 710 | /* |
| 711 | * Track our maximum slice length, if the CPU's load is at |
| 712 | * least twice that of our own weight (i.e. dont track it |
| 713 | * when there are only lesser-weight tasks around): |
| 714 | */ |
| 715 | if (rq_of(cfs_rq)->ls.load.weight >= 2*se->load.weight) { |
| 716 | se->slice_max = max(se->slice_max, |
| 717 | se->sum_exec_runtime - se->prev_sum_exec_runtime); |
| 718 | } |
| 719 | #endif |
Peter Zijlstra | 4a55b45 | 2007-09-05 14:32:49 +0200 | [diff] [blame] | 720 | se->prev_sum_exec_runtime = se->sum_exec_runtime; |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 721 | } |
| 722 | |
Ingo Molnar | 9948f4b | 2007-08-09 11:16:48 +0200 | [diff] [blame] | 723 | static struct sched_entity *pick_next_entity(struct cfs_rq *cfs_rq) |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 724 | { |
| 725 | struct sched_entity *se = __pick_next_entity(cfs_rq); |
| 726 | |
Ingo Molnar | 8494f41 | 2007-08-09 11:16:48 +0200 | [diff] [blame] | 727 | set_next_entity(cfs_rq, se); |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 728 | |
| 729 | return se; |
| 730 | } |
| 731 | |
Ingo Molnar | ab6cde2 | 2007-08-09 11:16:48 +0200 | [diff] [blame] | 732 | static void put_prev_entity(struct cfs_rq *cfs_rq, struct sched_entity *prev) |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 733 | { |
| 734 | /* |
| 735 | * If still on the runqueue then deactivate_task() |
| 736 | * was not called and update_curr() has to be done: |
| 737 | */ |
| 738 | if (prev->on_rq) |
Ingo Molnar | b7cc089 | 2007-08-09 11:16:47 +0200 | [diff] [blame] | 739 | update_curr(cfs_rq); |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 740 | |
Ingo Molnar | c7e9b5b | 2007-08-09 11:16:48 +0200 | [diff] [blame] | 741 | update_stats_curr_end(cfs_rq, prev); |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 742 | |
| 743 | if (prev->on_rq) |
Ingo Molnar | 5870db5 | 2007-08-09 11:16:47 +0200 | [diff] [blame] | 744 | update_stats_wait_start(cfs_rq, prev); |
Ingo Molnar | 429d43b | 2007-10-15 17:00:03 +0200 | [diff] [blame] | 745 | cfs_rq->curr = NULL; |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 746 | } |
| 747 | |
| 748 | static void entity_tick(struct cfs_rq *cfs_rq, struct sched_entity *curr) |
| 749 | { |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 750 | struct sched_entity *next; |
Ingo Molnar | c1b3da3 | 2007-08-09 11:16:47 +0200 | [diff] [blame] | 751 | |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 752 | /* |
| 753 | * Dequeue and enqueue the task to update its |
| 754 | * position within the tree: |
| 755 | */ |
Ingo Molnar | 525c271 | 2007-08-09 11:16:48 +0200 | [diff] [blame] | 756 | dequeue_entity(cfs_rq, curr, 0); |
Ingo Molnar | 668031c | 2007-08-09 11:16:48 +0200 | [diff] [blame] | 757 | enqueue_entity(cfs_rq, curr, 0); |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 758 | |
| 759 | /* |
| 760 | * Reschedule if another task tops the current one. |
| 761 | */ |
| 762 | next = __pick_next_entity(cfs_rq); |
| 763 | if (next == curr) |
| 764 | return; |
| 765 | |
Peter Zijlstra | 1169783 | 2007-09-05 14:32:49 +0200 | [diff] [blame] | 766 | __check_preempt_curr_fair(cfs_rq, next, curr, |
| 767 | sched_granularity(cfs_rq)); |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 768 | } |
| 769 | |
| 770 | /************************************************** |
| 771 | * CFS operations on tasks: |
| 772 | */ |
| 773 | |
| 774 | #ifdef CONFIG_FAIR_GROUP_SCHED |
| 775 | |
| 776 | /* Walk up scheduling entities hierarchy */ |
| 777 | #define for_each_sched_entity(se) \ |
| 778 | for (; se; se = se->parent) |
| 779 | |
| 780 | static inline struct cfs_rq *task_cfs_rq(struct task_struct *p) |
| 781 | { |
| 782 | return p->se.cfs_rq; |
| 783 | } |
| 784 | |
| 785 | /* runqueue on which this entity is (to be) queued */ |
| 786 | static inline struct cfs_rq *cfs_rq_of(struct sched_entity *se) |
| 787 | { |
| 788 | return se->cfs_rq; |
| 789 | } |
| 790 | |
| 791 | /* runqueue "owned" by this group */ |
| 792 | static inline struct cfs_rq *group_cfs_rq(struct sched_entity *grp) |
| 793 | { |
| 794 | return grp->my_q; |
| 795 | } |
| 796 | |
| 797 | /* Given a group's cfs_rq on one cpu, return its corresponding cfs_rq on |
| 798 | * another cpu ('this_cpu') |
| 799 | */ |
| 800 | static inline struct cfs_rq *cpu_cfs_rq(struct cfs_rq *cfs_rq, int this_cpu) |
| 801 | { |
| 802 | /* A later patch will take group into account */ |
| 803 | return &cpu_rq(this_cpu)->cfs; |
| 804 | } |
| 805 | |
| 806 | /* Iterate thr' all leaf cfs_rq's on a runqueue */ |
| 807 | #define for_each_leaf_cfs_rq(rq, cfs_rq) \ |
| 808 | list_for_each_entry(cfs_rq, &rq->leaf_cfs_rq_list, leaf_cfs_rq_list) |
| 809 | |
| 810 | /* Do the two (enqueued) tasks belong to the same group ? */ |
| 811 | static inline int is_same_group(struct task_struct *curr, struct task_struct *p) |
| 812 | { |
| 813 | if (curr->se.cfs_rq == p->se.cfs_rq) |
| 814 | return 1; |
| 815 | |
| 816 | return 0; |
| 817 | } |
| 818 | |
| 819 | #else /* CONFIG_FAIR_GROUP_SCHED */ |
| 820 | |
| 821 | #define for_each_sched_entity(se) \ |
| 822 | for (; se; se = NULL) |
| 823 | |
| 824 | static inline struct cfs_rq *task_cfs_rq(struct task_struct *p) |
| 825 | { |
| 826 | return &task_rq(p)->cfs; |
| 827 | } |
| 828 | |
| 829 | static inline struct cfs_rq *cfs_rq_of(struct sched_entity *se) |
| 830 | { |
| 831 | struct task_struct *p = task_of(se); |
| 832 | struct rq *rq = task_rq(p); |
| 833 | |
| 834 | return &rq->cfs; |
| 835 | } |
| 836 | |
| 837 | /* runqueue "owned" by this group */ |
| 838 | static inline struct cfs_rq *group_cfs_rq(struct sched_entity *grp) |
| 839 | { |
| 840 | return NULL; |
| 841 | } |
| 842 | |
| 843 | static inline struct cfs_rq *cpu_cfs_rq(struct cfs_rq *cfs_rq, int this_cpu) |
| 844 | { |
| 845 | return &cpu_rq(this_cpu)->cfs; |
| 846 | } |
| 847 | |
| 848 | #define for_each_leaf_cfs_rq(rq, cfs_rq) \ |
| 849 | for (cfs_rq = &rq->cfs; cfs_rq; cfs_rq = NULL) |
| 850 | |
| 851 | static inline int is_same_group(struct task_struct *curr, struct task_struct *p) |
| 852 | { |
| 853 | return 1; |
| 854 | } |
| 855 | |
| 856 | #endif /* CONFIG_FAIR_GROUP_SCHED */ |
| 857 | |
| 858 | /* |
| 859 | * The enqueue_task method is called before nr_running is |
| 860 | * increased. Here we update the fair scheduling stats and |
| 861 | * then put the task into the rbtree: |
| 862 | */ |
Ingo Molnar | fd390f6 | 2007-08-09 11:16:48 +0200 | [diff] [blame] | 863 | static void enqueue_task_fair(struct rq *rq, struct task_struct *p, int wakeup) |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 864 | { |
| 865 | struct cfs_rq *cfs_rq; |
| 866 | struct sched_entity *se = &p->se; |
| 867 | |
| 868 | for_each_sched_entity(se) { |
| 869 | if (se->on_rq) |
| 870 | break; |
| 871 | cfs_rq = cfs_rq_of(se); |
Ingo Molnar | 668031c | 2007-08-09 11:16:48 +0200 | [diff] [blame] | 872 | enqueue_entity(cfs_rq, se, wakeup); |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 873 | } |
| 874 | } |
| 875 | |
| 876 | /* |
| 877 | * The dequeue_task method is called before nr_running is |
| 878 | * decreased. We remove the task from the rbtree and |
| 879 | * update the fair scheduling stats: |
| 880 | */ |
Ingo Molnar | f02231e | 2007-08-09 11:16:48 +0200 | [diff] [blame] | 881 | static void dequeue_task_fair(struct rq *rq, struct task_struct *p, int sleep) |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 882 | { |
| 883 | struct cfs_rq *cfs_rq; |
| 884 | struct sched_entity *se = &p->se; |
| 885 | |
| 886 | for_each_sched_entity(se) { |
| 887 | cfs_rq = cfs_rq_of(se); |
Ingo Molnar | 525c271 | 2007-08-09 11:16:48 +0200 | [diff] [blame] | 888 | dequeue_entity(cfs_rq, se, sleep); |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 889 | /* Don't dequeue parent if it has other entities besides us */ |
| 890 | if (cfs_rq->load.weight) |
| 891 | break; |
| 892 | } |
| 893 | } |
| 894 | |
| 895 | /* |
Ingo Molnar | 1799e35 | 2007-09-19 23:34:46 +0200 | [diff] [blame] | 896 | * sched_yield() support is very simple - we dequeue and enqueue. |
| 897 | * |
| 898 | * If compat_yield is turned on then we requeue to the end of the tree. |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 899 | */ |
| 900 | static void yield_task_fair(struct rq *rq, struct task_struct *p) |
| 901 | { |
| 902 | struct cfs_rq *cfs_rq = task_cfs_rq(p); |
Ingo Molnar | 1799e35 | 2007-09-19 23:34:46 +0200 | [diff] [blame] | 903 | struct rb_node **link = &cfs_rq->tasks_timeline.rb_node; |
| 904 | struct sched_entity *rightmost, *se = &p->se; |
| 905 | struct rb_node *parent; |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 906 | |
| 907 | /* |
Ingo Molnar | 1799e35 | 2007-09-19 23:34:46 +0200 | [diff] [blame] | 908 | * Are we the only task in the tree? |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 909 | */ |
Ingo Molnar | 1799e35 | 2007-09-19 23:34:46 +0200 | [diff] [blame] | 910 | if (unlikely(cfs_rq->nr_running == 1)) |
| 911 | return; |
| 912 | |
| 913 | if (likely(!sysctl_sched_compat_yield)) { |
| 914 | __update_rq_clock(rq); |
| 915 | /* |
| 916 | * Dequeue and enqueue the task to update its |
| 917 | * position within the tree: |
| 918 | */ |
| 919 | dequeue_entity(cfs_rq, &p->se, 0); |
| 920 | enqueue_entity(cfs_rq, &p->se, 0); |
| 921 | |
| 922 | return; |
| 923 | } |
| 924 | /* |
| 925 | * Find the rightmost entry in the rbtree: |
| 926 | */ |
| 927 | do { |
| 928 | parent = *link; |
| 929 | link = &parent->rb_right; |
| 930 | } while (*link); |
| 931 | |
| 932 | rightmost = rb_entry(parent, struct sched_entity, run_node); |
| 933 | /* |
| 934 | * Already in the rightmost position? |
| 935 | */ |
| 936 | if (unlikely(rightmost == se)) |
| 937 | return; |
| 938 | |
| 939 | /* |
| 940 | * Minimally necessary key value to be last in the tree: |
| 941 | */ |
| 942 | se->fair_key = rightmost->fair_key + 1; |
| 943 | |
| 944 | if (cfs_rq->rb_leftmost == &se->run_node) |
| 945 | cfs_rq->rb_leftmost = rb_next(&se->run_node); |
| 946 | /* |
| 947 | * Relink the task to the rightmost position: |
| 948 | */ |
| 949 | rb_erase(&se->run_node, &cfs_rq->tasks_timeline); |
| 950 | rb_link_node(&se->run_node, parent, link); |
| 951 | rb_insert_color(&se->run_node, &cfs_rq->tasks_timeline); |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 952 | } |
| 953 | |
| 954 | /* |
| 955 | * Preempt the current task with a newly woken task if needed: |
| 956 | */ |
| 957 | static void check_preempt_curr_fair(struct rq *rq, struct task_struct *p) |
| 958 | { |
| 959 | struct task_struct *curr = rq->curr; |
| 960 | struct cfs_rq *cfs_rq = task_cfs_rq(curr); |
| 961 | unsigned long gran; |
| 962 | |
| 963 | if (unlikely(rt_prio(p->prio))) { |
Ingo Molnar | a8e504d | 2007-08-09 11:16:47 +0200 | [diff] [blame] | 964 | update_rq_clock(rq); |
Ingo Molnar | b7cc089 | 2007-08-09 11:16:47 +0200 | [diff] [blame] | 965 | update_curr(cfs_rq); |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 966 | resched_task(curr); |
| 967 | return; |
| 968 | } |
| 969 | |
| 970 | gran = sysctl_sched_wakeup_granularity; |
| 971 | /* |
| 972 | * Batch tasks prefer throughput over latency: |
| 973 | */ |
| 974 | if (unlikely(p->policy == SCHED_BATCH)) |
| 975 | gran = sysctl_sched_batch_wakeup_granularity; |
| 976 | |
| 977 | if (is_same_group(curr, p)) |
| 978 | __check_preempt_curr_fair(cfs_rq, &p->se, &curr->se, gran); |
| 979 | } |
| 980 | |
Ingo Molnar | fb8d472 | 2007-08-09 11:16:48 +0200 | [diff] [blame] | 981 | static struct task_struct *pick_next_task_fair(struct rq *rq) |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 982 | { |
| 983 | struct cfs_rq *cfs_rq = &rq->cfs; |
| 984 | struct sched_entity *se; |
| 985 | |
| 986 | if (unlikely(!cfs_rq->nr_running)) |
| 987 | return NULL; |
| 988 | |
| 989 | do { |
Ingo Molnar | 9948f4b | 2007-08-09 11:16:48 +0200 | [diff] [blame] | 990 | se = pick_next_entity(cfs_rq); |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 991 | cfs_rq = group_cfs_rq(se); |
| 992 | } while (cfs_rq); |
| 993 | |
| 994 | return task_of(se); |
| 995 | } |
| 996 | |
| 997 | /* |
| 998 | * Account for a descheduled task: |
| 999 | */ |
Ingo Molnar | 31ee529 | 2007-08-09 11:16:49 +0200 | [diff] [blame] | 1000 | static void put_prev_task_fair(struct rq *rq, struct task_struct *prev) |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 1001 | { |
| 1002 | struct sched_entity *se = &prev->se; |
| 1003 | struct cfs_rq *cfs_rq; |
| 1004 | |
| 1005 | for_each_sched_entity(se) { |
| 1006 | cfs_rq = cfs_rq_of(se); |
Ingo Molnar | ab6cde2 | 2007-08-09 11:16:48 +0200 | [diff] [blame] | 1007 | put_prev_entity(cfs_rq, se); |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 1008 | } |
| 1009 | } |
| 1010 | |
| 1011 | /************************************************** |
| 1012 | * Fair scheduling class load-balancing methods: |
| 1013 | */ |
| 1014 | |
| 1015 | /* |
| 1016 | * Load-balancing iterator. Note: while the runqueue stays locked |
| 1017 | * during the whole iteration, the current task might be |
| 1018 | * dequeued so the iterator has to be dequeue-safe. Here we |
| 1019 | * achieve that by always pre-iterating before returning |
| 1020 | * the current task: |
| 1021 | */ |
| 1022 | static inline struct task_struct * |
| 1023 | __load_balance_iterator(struct cfs_rq *cfs_rq, struct rb_node *curr) |
| 1024 | { |
| 1025 | struct task_struct *p; |
| 1026 | |
| 1027 | if (!curr) |
| 1028 | return NULL; |
| 1029 | |
| 1030 | p = rb_entry(curr, struct task_struct, se.run_node); |
| 1031 | cfs_rq->rb_load_balance_curr = rb_next(curr); |
| 1032 | |
| 1033 | return p; |
| 1034 | } |
| 1035 | |
| 1036 | static struct task_struct *load_balance_start_fair(void *arg) |
| 1037 | { |
| 1038 | struct cfs_rq *cfs_rq = arg; |
| 1039 | |
| 1040 | return __load_balance_iterator(cfs_rq, first_fair(cfs_rq)); |
| 1041 | } |
| 1042 | |
| 1043 | static struct task_struct *load_balance_next_fair(void *arg) |
| 1044 | { |
| 1045 | struct cfs_rq *cfs_rq = arg; |
| 1046 | |
| 1047 | return __load_balance_iterator(cfs_rq, cfs_rq->rb_load_balance_curr); |
| 1048 | } |
| 1049 | |
Peter Williams | a4ac01c | 2007-08-09 11:16:46 +0200 | [diff] [blame] | 1050 | #ifdef CONFIG_FAIR_GROUP_SCHED |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 1051 | static int cfs_rq_best_prio(struct cfs_rq *cfs_rq) |
| 1052 | { |
| 1053 | struct sched_entity *curr; |
| 1054 | struct task_struct *p; |
| 1055 | |
| 1056 | if (!cfs_rq->nr_running) |
| 1057 | return MAX_PRIO; |
| 1058 | |
| 1059 | curr = __pick_next_entity(cfs_rq); |
| 1060 | p = task_of(curr); |
| 1061 | |
| 1062 | return p->prio; |
| 1063 | } |
Peter Williams | a4ac01c | 2007-08-09 11:16:46 +0200 | [diff] [blame] | 1064 | #endif |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 1065 | |
Peter Williams | 4301065 | 2007-08-09 11:16:46 +0200 | [diff] [blame] | 1066 | static unsigned long |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 1067 | load_balance_fair(struct rq *this_rq, int this_cpu, struct rq *busiest, |
Peter Williams | a4ac01c | 2007-08-09 11:16:46 +0200 | [diff] [blame] | 1068 | unsigned long max_nr_move, unsigned long max_load_move, |
| 1069 | struct sched_domain *sd, enum cpu_idle_type idle, |
| 1070 | int *all_pinned, int *this_best_prio) |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 1071 | { |
| 1072 | struct cfs_rq *busy_cfs_rq; |
| 1073 | unsigned long load_moved, total_nr_moved = 0, nr_moved; |
| 1074 | long rem_load_move = max_load_move; |
| 1075 | struct rq_iterator cfs_rq_iterator; |
| 1076 | |
| 1077 | cfs_rq_iterator.start = load_balance_start_fair; |
| 1078 | cfs_rq_iterator.next = load_balance_next_fair; |
| 1079 | |
| 1080 | for_each_leaf_cfs_rq(busiest, busy_cfs_rq) { |
Peter Williams | a4ac01c | 2007-08-09 11:16:46 +0200 | [diff] [blame] | 1081 | #ifdef CONFIG_FAIR_GROUP_SCHED |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 1082 | struct cfs_rq *this_cfs_rq; |
Ingo Molnar | e56f31a | 2007-08-10 23:05:11 +0200 | [diff] [blame] | 1083 | long imbalance; |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 1084 | unsigned long maxload; |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 1085 | |
| 1086 | this_cfs_rq = cpu_cfs_rq(busy_cfs_rq, this_cpu); |
| 1087 | |
Ingo Molnar | e56f31a | 2007-08-10 23:05:11 +0200 | [diff] [blame] | 1088 | imbalance = busy_cfs_rq->load.weight - this_cfs_rq->load.weight; |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 1089 | /* Don't pull if this_cfs_rq has more load than busy_cfs_rq */ |
| 1090 | if (imbalance <= 0) |
| 1091 | continue; |
| 1092 | |
| 1093 | /* Don't pull more than imbalance/2 */ |
| 1094 | imbalance /= 2; |
| 1095 | maxload = min(rem_load_move, imbalance); |
| 1096 | |
Peter Williams | a4ac01c | 2007-08-09 11:16:46 +0200 | [diff] [blame] | 1097 | *this_best_prio = cfs_rq_best_prio(this_cfs_rq); |
| 1098 | #else |
Ingo Molnar | e56f31a | 2007-08-10 23:05:11 +0200 | [diff] [blame] | 1099 | # define maxload rem_load_move |
Peter Williams | a4ac01c | 2007-08-09 11:16:46 +0200 | [diff] [blame] | 1100 | #endif |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 1101 | /* pass busy_cfs_rq argument into |
| 1102 | * load_balance_[start|next]_fair iterators |
| 1103 | */ |
| 1104 | cfs_rq_iterator.arg = busy_cfs_rq; |
| 1105 | nr_moved = balance_tasks(this_rq, this_cpu, busiest, |
| 1106 | max_nr_move, maxload, sd, idle, all_pinned, |
Peter Williams | a4ac01c | 2007-08-09 11:16:46 +0200 | [diff] [blame] | 1107 | &load_moved, this_best_prio, &cfs_rq_iterator); |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 1108 | |
| 1109 | total_nr_moved += nr_moved; |
| 1110 | max_nr_move -= nr_moved; |
| 1111 | rem_load_move -= load_moved; |
| 1112 | |
| 1113 | if (max_nr_move <= 0 || rem_load_move <= 0) |
| 1114 | break; |
| 1115 | } |
| 1116 | |
Peter Williams | 4301065 | 2007-08-09 11:16:46 +0200 | [diff] [blame] | 1117 | return max_load_move - rem_load_move; |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 1118 | } |
| 1119 | |
| 1120 | /* |
| 1121 | * scheduler tick hitting a task of our scheduling class: |
| 1122 | */ |
| 1123 | static void task_tick_fair(struct rq *rq, struct task_struct *curr) |
| 1124 | { |
| 1125 | struct cfs_rq *cfs_rq; |
| 1126 | struct sched_entity *se = &curr->se; |
| 1127 | |
| 1128 | for_each_sched_entity(se) { |
| 1129 | cfs_rq = cfs_rq_of(se); |
| 1130 | entity_tick(cfs_rq, se); |
| 1131 | } |
| 1132 | } |
| 1133 | |
| 1134 | /* |
| 1135 | * Share the fairness runtime between parent and child, thus the |
| 1136 | * total amount of pressure for CPU stays equal - new tasks |
| 1137 | * get a chance to run but frequent forkers are not allowed to |
| 1138 | * monopolize the CPU. Note: the parent runqueue is locked, |
| 1139 | * the child is not running yet. |
| 1140 | */ |
Ingo Molnar | ee0827d | 2007-08-09 11:16:49 +0200 | [diff] [blame] | 1141 | static void task_new_fair(struct rq *rq, struct task_struct *p) |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 1142 | { |
| 1143 | struct cfs_rq *cfs_rq = task_cfs_rq(p); |
Ingo Molnar | 429d43b | 2007-10-15 17:00:03 +0200 | [diff] [blame] | 1144 | struct sched_entity *se = &p->se, *curr = cfs_rq->curr; |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 1145 | |
| 1146 | sched_info_queued(p); |
| 1147 | |
Ting Yang | 7109c442 | 2007-08-28 12:53:24 +0200 | [diff] [blame] | 1148 | update_curr(cfs_rq); |
Ingo Molnar | d2417e5 | 2007-08-09 11:16:47 +0200 | [diff] [blame] | 1149 | update_stats_enqueue(cfs_rq, se); |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 1150 | /* |
| 1151 | * Child runs first: we let it run before the parent |
| 1152 | * until it reschedules once. We set up the key so that |
| 1153 | * it will preempt the parent: |
| 1154 | */ |
Ingo Molnar | 9f508f8 | 2007-08-28 12:53:24 +0200 | [diff] [blame] | 1155 | se->fair_key = curr->fair_key - |
Ting Yang | 7109c442 | 2007-08-28 12:53:24 +0200 | [diff] [blame] | 1156 | niced_granularity(curr, sched_granularity(cfs_rq)) - 1; |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 1157 | /* |
| 1158 | * The first wait is dominated by the child-runs-first logic, |
| 1159 | * so do not credit it with that waiting time yet: |
| 1160 | */ |
Peter Zijlstra | e59c80c | 2007-10-15 17:00:03 +0200 | [diff] [blame] | 1161 | if (sched_feat(SKIP_INITIAL)) |
Ingo Molnar | 9f508f8 | 2007-08-28 12:53:24 +0200 | [diff] [blame] | 1162 | se->wait_start_fair = 0; |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 1163 | |
| 1164 | /* |
| 1165 | * The statistical average of wait_runtime is about |
| 1166 | * -granularity/2, so initialize the task with that: |
| 1167 | */ |
Peter Zijlstra | e59c80c | 2007-10-15 17:00:03 +0200 | [diff] [blame] | 1168 | if (sched_feat(START_DEBIT)) |
Ingo Molnar | 9f508f8 | 2007-08-28 12:53:24 +0200 | [diff] [blame] | 1169 | se->wait_runtime = -(sched_granularity(cfs_rq) / 2); |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 1170 | |
Ingo Molnar | e9acbff | 2007-10-15 17:00:04 +0200 | [diff] [blame^] | 1171 | se->vruntime = cfs_rq->min_vruntime; |
| 1172 | update_stats_enqueue(cfs_rq, se); |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 1173 | __enqueue_entity(cfs_rq, se); |
Ingo Molnar | bb61c21 | 2007-10-15 17:00:02 +0200 | [diff] [blame] | 1174 | resched_task(rq->curr); |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 1175 | } |
| 1176 | |
| 1177 | #ifdef CONFIG_FAIR_GROUP_SCHED |
| 1178 | /* Account for a task changing its policy or group. |
| 1179 | * |
| 1180 | * This routine is mostly called to set cfs_rq->curr field when a task |
| 1181 | * migrates between groups/classes. |
| 1182 | */ |
| 1183 | static void set_curr_task_fair(struct rq *rq) |
| 1184 | { |
Bruce Ashfield | 7c6c16f | 2007-08-24 20:39:10 +0200 | [diff] [blame] | 1185 | struct sched_entity *se = &rq->curr->se; |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 1186 | |
Ingo Molnar | c3b64f1 | 2007-08-09 11:16:51 +0200 | [diff] [blame] | 1187 | for_each_sched_entity(se) |
| 1188 | set_next_entity(cfs_rq_of(se), se); |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 1189 | } |
| 1190 | #else |
| 1191 | static void set_curr_task_fair(struct rq *rq) |
| 1192 | { |
| 1193 | } |
| 1194 | #endif |
| 1195 | |
| 1196 | /* |
| 1197 | * All the scheduling class methods: |
| 1198 | */ |
| 1199 | struct sched_class fair_sched_class __read_mostly = { |
| 1200 | .enqueue_task = enqueue_task_fair, |
| 1201 | .dequeue_task = dequeue_task_fair, |
| 1202 | .yield_task = yield_task_fair, |
| 1203 | |
| 1204 | .check_preempt_curr = check_preempt_curr_fair, |
| 1205 | |
| 1206 | .pick_next_task = pick_next_task_fair, |
| 1207 | .put_prev_task = put_prev_task_fair, |
| 1208 | |
| 1209 | .load_balance = load_balance_fair, |
| 1210 | |
| 1211 | .set_curr_task = set_curr_task_fair, |
| 1212 | .task_tick = task_tick_fair, |
| 1213 | .task_new = task_new_fair, |
| 1214 | }; |
| 1215 | |
| 1216 | #ifdef CONFIG_SCHED_DEBUG |
Ingo Molnar | 5cef9ec | 2007-08-09 11:16:47 +0200 | [diff] [blame] | 1217 | static void print_cfs_stats(struct seq_file *m, int cpu) |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 1218 | { |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 1219 | struct cfs_rq *cfs_rq; |
| 1220 | |
Ingo Molnar | c3b64f1 | 2007-08-09 11:16:51 +0200 | [diff] [blame] | 1221 | for_each_leaf_cfs_rq(cpu_rq(cpu), cfs_rq) |
Ingo Molnar | 5cef9ec | 2007-08-09 11:16:47 +0200 | [diff] [blame] | 1222 | print_cfs_rq(m, cpu, cfs_rq); |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 1223 | } |
| 1224 | #endif |