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