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