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