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 | |
Arjan van de Ven | 9745512 | 2008-01-25 21:08:34 +0100 | [diff] [blame] | 23 | #include <linux/latencytop.h> |
Christian Ehrhardt | 1983a92 | 2009-11-30 12:16:47 +0100 | [diff] [blame] | 24 | #include <linux/sched.h> |
Arjan van de Ven | 9745512 | 2008-01-25 21:08:34 +0100 | [diff] [blame] | 25 | |
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 | * Targeted preemption latency for CPU-bound tasks: |
Takuya Yoshikawa | 864616e | 2010-10-14 16:09:13 +0900 | [diff] [blame] | 28 | * (default: 6ms * (1 + ilog(ncpus)), units: nanoseconds) |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 29 | * |
Peter Zijlstra | 2180508 | 2007-08-25 18:41:53 +0200 | [diff] [blame] | 30 | * NOTE: this latency value is not the same as the concept of |
Ingo Molnar | d274a4c | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 31 | * 'timeslice length' - timeslices in CFS are of variable length |
| 32 | * and have no persistent notion like in traditional, time-slice |
| 33 | * based scheduling concepts. |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 34 | * |
Ingo Molnar | d274a4c | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 35 | * (to see the precise effective timeslice length of your workload, |
| 36 | * run vmstat and monitor the context-switches (cs) field) |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 37 | */ |
Mike Galbraith | 2140692 | 2010-03-11 17:17:15 +0100 | [diff] [blame] | 38 | unsigned int sysctl_sched_latency = 6000000ULL; |
| 39 | unsigned int normalized_sysctl_sched_latency = 6000000ULL; |
Ingo Molnar | 2bd8e6d | 2007-10-15 17:00:02 +0200 | [diff] [blame] | 40 | |
| 41 | /* |
Christian Ehrhardt | 1983a92 | 2009-11-30 12:16:47 +0100 | [diff] [blame] | 42 | * The initial- and re-scaling of tunables is configurable |
| 43 | * (default SCHED_TUNABLESCALING_LOG = *(1+ilog(ncpus)) |
| 44 | * |
| 45 | * Options are: |
| 46 | * SCHED_TUNABLESCALING_NONE - unscaled, always *1 |
| 47 | * SCHED_TUNABLESCALING_LOG - scaled logarithmical, *1+ilog(ncpus) |
| 48 | * SCHED_TUNABLESCALING_LINEAR - scaled linear, *ncpus |
| 49 | */ |
| 50 | enum sched_tunable_scaling sysctl_sched_tunable_scaling |
| 51 | = SCHED_TUNABLESCALING_LOG; |
| 52 | |
| 53 | /* |
Peter Zijlstra | b2be5e9 | 2007-11-09 22:39:37 +0100 | [diff] [blame] | 54 | * Minimal preemption granularity for CPU-bound tasks: |
Takuya Yoshikawa | 864616e | 2010-10-14 16:09:13 +0900 | [diff] [blame] | 55 | * (default: 0.75 msec * (1 + ilog(ncpus)), units: nanoseconds) |
Peter Zijlstra | b2be5e9 | 2007-11-09 22:39:37 +0100 | [diff] [blame] | 56 | */ |
Ingo Molnar | 0bf377b | 2010-09-12 08:14:52 +0200 | [diff] [blame] | 57 | unsigned int sysctl_sched_min_granularity = 750000ULL; |
| 58 | unsigned int normalized_sysctl_sched_min_granularity = 750000ULL; |
Peter Zijlstra | b2be5e9 | 2007-11-09 22:39:37 +0100 | [diff] [blame] | 59 | |
| 60 | /* |
| 61 | * is kept at sysctl_sched_latency / sysctl_sched_min_granularity |
| 62 | */ |
Ingo Molnar | 0bf377b | 2010-09-12 08:14:52 +0200 | [diff] [blame] | 63 | static unsigned int sched_nr_latency = 8; |
Peter Zijlstra | b2be5e9 | 2007-11-09 22:39:37 +0100 | [diff] [blame] | 64 | |
| 65 | /* |
Mike Galbraith | 2bba22c | 2009-09-09 15:41:37 +0200 | [diff] [blame] | 66 | * After fork, child runs first. If set to 0 (default) then |
Ingo Molnar | 2bd8e6d | 2007-10-15 17:00:02 +0200 | [diff] [blame] | 67 | * parent will (try to) run first. |
| 68 | */ |
Mike Galbraith | 2bba22c | 2009-09-09 15:41:37 +0200 | [diff] [blame] | 69 | unsigned int sysctl_sched_child_runs_first __read_mostly; |
Peter Zijlstra | 2180508 | 2007-08-25 18:41:53 +0200 | [diff] [blame] | 70 | |
| 71 | /* |
Ingo Molnar | 1799e35 | 2007-09-19 23:34:46 +0200 | [diff] [blame] | 72 | * sys_sched_yield() compat mode |
| 73 | * |
| 74 | * This option switches the agressive yield implementation of the |
| 75 | * old scheduler back on. |
| 76 | */ |
| 77 | unsigned int __read_mostly sysctl_sched_compat_yield; |
| 78 | |
| 79 | /* |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 80 | * SCHED_OTHER wake-up granularity. |
Mike Galbraith | 172e082 | 2009-09-09 15:41:37 +0200 | [diff] [blame] | 81 | * (default: 1 msec * (1 + ilog(ncpus)), units: nanoseconds) |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 82 | * |
| 83 | * This option delays the preemption effects of decoupled workloads |
| 84 | * and reduces their over-scheduling. Synchronous workloads will still |
| 85 | * have immediate wakeup/sleep latencies. |
| 86 | */ |
Mike Galbraith | 172e082 | 2009-09-09 15:41:37 +0200 | [diff] [blame] | 87 | unsigned int sysctl_sched_wakeup_granularity = 1000000UL; |
Christian Ehrhardt | 0bcdcf2 | 2009-11-30 12:16:46 +0100 | [diff] [blame] | 88 | unsigned int normalized_sysctl_sched_wakeup_granularity = 1000000UL; |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 89 | |
Ingo Molnar | da84d96 | 2007-10-15 17:00:18 +0200 | [diff] [blame] | 90 | const_debug unsigned int sysctl_sched_migration_cost = 500000UL; |
| 91 | |
Peter Zijlstra | a4c2f00 | 2008-10-17 19:27:03 +0200 | [diff] [blame] | 92 | static const struct sched_class fair_sched_class; |
| 93 | |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 94 | /************************************************************** |
| 95 | * CFS operations on generic schedulable entities: |
| 96 | */ |
| 97 | |
| 98 | #ifdef CONFIG_FAIR_GROUP_SCHED |
| 99 | |
| 100 | /* cpu runqueue to which this cfs_rq is attached */ |
| 101 | static inline struct rq *rq_of(struct cfs_rq *cfs_rq) |
| 102 | { |
| 103 | return cfs_rq->rq; |
| 104 | } |
| 105 | |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 106 | /* An entity is a task if it doesn't "own" a runqueue */ |
| 107 | #define entity_is_task(se) (!se->my_q) |
| 108 | |
Peter Zijlstra | 8f48894 | 2009-07-24 12:25:30 +0200 | [diff] [blame] | 109 | static inline struct task_struct *task_of(struct sched_entity *se) |
| 110 | { |
| 111 | #ifdef CONFIG_SCHED_DEBUG |
| 112 | WARN_ON_ONCE(!entity_is_task(se)); |
| 113 | #endif |
| 114 | return container_of(se, struct task_struct, se); |
| 115 | } |
| 116 | |
Peter Zijlstra | b758149 | 2008-04-19 19:45:00 +0200 | [diff] [blame] | 117 | /* Walk up scheduling entities hierarchy */ |
| 118 | #define for_each_sched_entity(se) \ |
| 119 | for (; se; se = se->parent) |
| 120 | |
| 121 | static inline struct cfs_rq *task_cfs_rq(struct task_struct *p) |
| 122 | { |
| 123 | return p->se.cfs_rq; |
| 124 | } |
| 125 | |
| 126 | /* runqueue on which this entity is (to be) queued */ |
| 127 | static inline struct cfs_rq *cfs_rq_of(struct sched_entity *se) |
| 128 | { |
| 129 | return se->cfs_rq; |
| 130 | } |
| 131 | |
| 132 | /* runqueue "owned" by this group */ |
| 133 | static inline struct cfs_rq *group_cfs_rq(struct sched_entity *grp) |
| 134 | { |
| 135 | return grp->my_q; |
| 136 | } |
| 137 | |
| 138 | /* Given a group's cfs_rq on one cpu, return its corresponding cfs_rq on |
| 139 | * another cpu ('this_cpu') |
| 140 | */ |
| 141 | static inline struct cfs_rq *cpu_cfs_rq(struct cfs_rq *cfs_rq, int this_cpu) |
| 142 | { |
| 143 | return cfs_rq->tg->cfs_rq[this_cpu]; |
| 144 | } |
| 145 | |
Peter Zijlstra | 3d4b47b | 2010-11-15 15:47:01 -0800 | [diff] [blame^] | 146 | static inline void list_add_leaf_cfs_rq(struct cfs_rq *cfs_rq) |
| 147 | { |
| 148 | if (!cfs_rq->on_list) { |
| 149 | list_add_rcu(&cfs_rq->leaf_cfs_rq_list, |
| 150 | &rq_of(cfs_rq)->leaf_cfs_rq_list); |
| 151 | |
| 152 | cfs_rq->on_list = 1; |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | static inline void list_del_leaf_cfs_rq(struct cfs_rq *cfs_rq) |
| 157 | { |
| 158 | if (cfs_rq->on_list) { |
| 159 | list_del_rcu(&cfs_rq->leaf_cfs_rq_list); |
| 160 | cfs_rq->on_list = 0; |
| 161 | } |
| 162 | } |
| 163 | |
Peter Zijlstra | b758149 | 2008-04-19 19:45:00 +0200 | [diff] [blame] | 164 | /* Iterate thr' all leaf cfs_rq's on a runqueue */ |
| 165 | #define for_each_leaf_cfs_rq(rq, cfs_rq) \ |
| 166 | list_for_each_entry_rcu(cfs_rq, &rq->leaf_cfs_rq_list, leaf_cfs_rq_list) |
| 167 | |
| 168 | /* Do the two (enqueued) entities belong to the same group ? */ |
| 169 | static inline int |
| 170 | is_same_group(struct sched_entity *se, struct sched_entity *pse) |
| 171 | { |
| 172 | if (se->cfs_rq == pse->cfs_rq) |
| 173 | return 1; |
| 174 | |
| 175 | return 0; |
| 176 | } |
| 177 | |
| 178 | static inline struct sched_entity *parent_entity(struct sched_entity *se) |
| 179 | { |
| 180 | return se->parent; |
| 181 | } |
| 182 | |
Peter Zijlstra | 464b752 | 2008-10-24 11:06:15 +0200 | [diff] [blame] | 183 | /* return depth at which a sched entity is present in the hierarchy */ |
| 184 | static inline int depth_se(struct sched_entity *se) |
| 185 | { |
| 186 | int depth = 0; |
| 187 | |
| 188 | for_each_sched_entity(se) |
| 189 | depth++; |
| 190 | |
| 191 | return depth; |
| 192 | } |
| 193 | |
| 194 | static void |
| 195 | find_matching_se(struct sched_entity **se, struct sched_entity **pse) |
| 196 | { |
| 197 | int se_depth, pse_depth; |
| 198 | |
| 199 | /* |
| 200 | * preemption test can be made between sibling entities who are in the |
| 201 | * same cfs_rq i.e who have a common parent. Walk up the hierarchy of |
| 202 | * both tasks until we find their ancestors who are siblings of common |
| 203 | * parent. |
| 204 | */ |
| 205 | |
| 206 | /* First walk up until both entities are at same depth */ |
| 207 | se_depth = depth_se(*se); |
| 208 | pse_depth = depth_se(*pse); |
| 209 | |
| 210 | while (se_depth > pse_depth) { |
| 211 | se_depth--; |
| 212 | *se = parent_entity(*se); |
| 213 | } |
| 214 | |
| 215 | while (pse_depth > se_depth) { |
| 216 | pse_depth--; |
| 217 | *pse = parent_entity(*pse); |
| 218 | } |
| 219 | |
| 220 | while (!is_same_group(*se, *pse)) { |
| 221 | *se = parent_entity(*se); |
| 222 | *pse = parent_entity(*pse); |
| 223 | } |
| 224 | } |
| 225 | |
Peter Zijlstra | 8f48894 | 2009-07-24 12:25:30 +0200 | [diff] [blame] | 226 | #else /* !CONFIG_FAIR_GROUP_SCHED */ |
| 227 | |
| 228 | static inline struct task_struct *task_of(struct sched_entity *se) |
| 229 | { |
| 230 | return container_of(se, struct task_struct, se); |
| 231 | } |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 232 | |
| 233 | static inline struct rq *rq_of(struct cfs_rq *cfs_rq) |
| 234 | { |
| 235 | return container_of(cfs_rq, struct rq, cfs); |
| 236 | } |
| 237 | |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 238 | #define entity_is_task(se) 1 |
| 239 | |
Peter Zijlstra | b758149 | 2008-04-19 19:45:00 +0200 | [diff] [blame] | 240 | #define for_each_sched_entity(se) \ |
| 241 | for (; se; se = NULL) |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 242 | |
Peter Zijlstra | b758149 | 2008-04-19 19:45:00 +0200 | [diff] [blame] | 243 | static inline struct cfs_rq *task_cfs_rq(struct task_struct *p) |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 244 | { |
Peter Zijlstra | b758149 | 2008-04-19 19:45:00 +0200 | [diff] [blame] | 245 | return &task_rq(p)->cfs; |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 246 | } |
| 247 | |
Peter Zijlstra | b758149 | 2008-04-19 19:45:00 +0200 | [diff] [blame] | 248 | static inline struct cfs_rq *cfs_rq_of(struct sched_entity *se) |
| 249 | { |
| 250 | struct task_struct *p = task_of(se); |
| 251 | struct rq *rq = task_rq(p); |
| 252 | |
| 253 | return &rq->cfs; |
| 254 | } |
| 255 | |
| 256 | /* runqueue "owned" by this group */ |
| 257 | static inline struct cfs_rq *group_cfs_rq(struct sched_entity *grp) |
| 258 | { |
| 259 | return NULL; |
| 260 | } |
| 261 | |
| 262 | static inline struct cfs_rq *cpu_cfs_rq(struct cfs_rq *cfs_rq, int this_cpu) |
| 263 | { |
| 264 | return &cpu_rq(this_cpu)->cfs; |
| 265 | } |
| 266 | |
Peter Zijlstra | 3d4b47b | 2010-11-15 15:47:01 -0800 | [diff] [blame^] | 267 | static inline void list_add_leaf_cfs_rq(struct cfs_rq *cfs_rq) |
| 268 | { |
| 269 | } |
| 270 | |
| 271 | static inline void list_del_leaf_cfs_rq(struct cfs_rq *cfs_rq) |
| 272 | { |
| 273 | } |
| 274 | |
Peter Zijlstra | b758149 | 2008-04-19 19:45:00 +0200 | [diff] [blame] | 275 | #define for_each_leaf_cfs_rq(rq, cfs_rq) \ |
| 276 | for (cfs_rq = &rq->cfs; cfs_rq; cfs_rq = NULL) |
| 277 | |
| 278 | static inline int |
| 279 | is_same_group(struct sched_entity *se, struct sched_entity *pse) |
| 280 | { |
| 281 | return 1; |
| 282 | } |
| 283 | |
| 284 | static inline struct sched_entity *parent_entity(struct sched_entity *se) |
| 285 | { |
| 286 | return NULL; |
| 287 | } |
| 288 | |
Peter Zijlstra | 464b752 | 2008-10-24 11:06:15 +0200 | [diff] [blame] | 289 | static inline void |
| 290 | find_matching_se(struct sched_entity **se, struct sched_entity **pse) |
| 291 | { |
| 292 | } |
| 293 | |
Peter Zijlstra | b758149 | 2008-04-19 19:45:00 +0200 | [diff] [blame] | 294 | #endif /* CONFIG_FAIR_GROUP_SCHED */ |
| 295 | |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 296 | |
| 297 | /************************************************************** |
| 298 | * Scheduling class tree data structure manipulation methods: |
| 299 | */ |
| 300 | |
Ingo Molnar | 0702e3e | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 301 | static inline u64 max_vruntime(u64 min_vruntime, u64 vruntime) |
Peter Zijlstra | 02e0431 | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 302 | { |
Peter Zijlstra | 368059a | 2007-10-15 17:00:11 +0200 | [diff] [blame] | 303 | s64 delta = (s64)(vruntime - min_vruntime); |
| 304 | if (delta > 0) |
Peter Zijlstra | 02e0431 | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 305 | min_vruntime = vruntime; |
| 306 | |
| 307 | return min_vruntime; |
| 308 | } |
| 309 | |
Ingo Molnar | 0702e3e | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 310 | static inline u64 min_vruntime(u64 min_vruntime, u64 vruntime) |
Peter Zijlstra | b0ffd24 | 2007-10-15 17:00:12 +0200 | [diff] [blame] | 311 | { |
| 312 | s64 delta = (s64)(vruntime - min_vruntime); |
| 313 | if (delta < 0) |
| 314 | min_vruntime = vruntime; |
| 315 | |
| 316 | return min_vruntime; |
| 317 | } |
| 318 | |
Fabio Checconi | 54fdc58 | 2009-07-16 12:32:27 +0200 | [diff] [blame] | 319 | static inline int entity_before(struct sched_entity *a, |
| 320 | struct sched_entity *b) |
| 321 | { |
| 322 | return (s64)(a->vruntime - b->vruntime) < 0; |
| 323 | } |
| 324 | |
Ingo Molnar | 0702e3e | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 325 | static inline s64 entity_key(struct cfs_rq *cfs_rq, struct sched_entity *se) |
Peter Zijlstra | 9014623 | 2007-10-15 17:00:05 +0200 | [diff] [blame] | 326 | { |
Dmitry Adamushko | 30cfdcf | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 327 | return se->vruntime - cfs_rq->min_vruntime; |
Peter Zijlstra | 9014623 | 2007-10-15 17:00:05 +0200 | [diff] [blame] | 328 | } |
| 329 | |
Peter Zijlstra | 1af5f73 | 2008-10-24 11:06:13 +0200 | [diff] [blame] | 330 | static void update_min_vruntime(struct cfs_rq *cfs_rq) |
| 331 | { |
| 332 | u64 vruntime = cfs_rq->min_vruntime; |
| 333 | |
| 334 | if (cfs_rq->curr) |
| 335 | vruntime = cfs_rq->curr->vruntime; |
| 336 | |
| 337 | if (cfs_rq->rb_leftmost) { |
| 338 | struct sched_entity *se = rb_entry(cfs_rq->rb_leftmost, |
| 339 | struct sched_entity, |
| 340 | run_node); |
| 341 | |
Peter Zijlstra | e17036d | 2009-01-15 14:53:39 +0100 | [diff] [blame] | 342 | if (!cfs_rq->curr) |
Peter Zijlstra | 1af5f73 | 2008-10-24 11:06:13 +0200 | [diff] [blame] | 343 | vruntime = se->vruntime; |
| 344 | else |
| 345 | vruntime = min_vruntime(vruntime, se->vruntime); |
| 346 | } |
| 347 | |
| 348 | cfs_rq->min_vruntime = max_vruntime(cfs_rq->min_vruntime, vruntime); |
| 349 | } |
| 350 | |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 351 | /* |
| 352 | * Enqueue an entity into the rb-tree: |
| 353 | */ |
Ingo Molnar | 0702e3e | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 354 | static void __enqueue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se) |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 355 | { |
| 356 | struct rb_node **link = &cfs_rq->tasks_timeline.rb_node; |
| 357 | struct rb_node *parent = NULL; |
| 358 | struct sched_entity *entry; |
Peter Zijlstra | 9014623 | 2007-10-15 17:00:05 +0200 | [diff] [blame] | 359 | s64 key = entity_key(cfs_rq, se); |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 360 | int leftmost = 1; |
| 361 | |
| 362 | /* |
| 363 | * Find the right place in the rbtree: |
| 364 | */ |
| 365 | while (*link) { |
| 366 | parent = *link; |
| 367 | entry = rb_entry(parent, struct sched_entity, run_node); |
| 368 | /* |
| 369 | * We dont care about collisions. Nodes with |
| 370 | * the same key stay together. |
| 371 | */ |
Peter Zijlstra | 9014623 | 2007-10-15 17:00:05 +0200 | [diff] [blame] | 372 | if (key < entity_key(cfs_rq, entry)) { |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 373 | link = &parent->rb_left; |
| 374 | } else { |
| 375 | link = &parent->rb_right; |
| 376 | leftmost = 0; |
| 377 | } |
| 378 | } |
| 379 | |
| 380 | /* |
| 381 | * Maintain a cache of leftmost tree entries (it is frequently |
| 382 | * used): |
| 383 | */ |
Peter Zijlstra | 1af5f73 | 2008-10-24 11:06:13 +0200 | [diff] [blame] | 384 | if (leftmost) |
Ingo Molnar | 57cb499 | 2007-10-15 17:00:11 +0200 | [diff] [blame] | 385 | cfs_rq->rb_leftmost = &se->run_node; |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 386 | |
| 387 | rb_link_node(&se->run_node, parent, link); |
| 388 | rb_insert_color(&se->run_node, &cfs_rq->tasks_timeline); |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 389 | } |
| 390 | |
Ingo Molnar | 0702e3e | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 391 | static void __dequeue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se) |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 392 | { |
Peter Zijlstra | 3fe6974 | 2008-03-14 20:55:51 +0100 | [diff] [blame] | 393 | if (cfs_rq->rb_leftmost == &se->run_node) { |
| 394 | struct rb_node *next_node; |
Peter Zijlstra | 3fe6974 | 2008-03-14 20:55:51 +0100 | [diff] [blame] | 395 | |
| 396 | next_node = rb_next(&se->run_node); |
| 397 | cfs_rq->rb_leftmost = next_node; |
Peter Zijlstra | 3fe6974 | 2008-03-14 20:55:51 +0100 | [diff] [blame] | 398 | } |
Ingo Molnar | e9acbff | 2007-10-15 17:00:04 +0200 | [diff] [blame] | 399 | |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 400 | rb_erase(&se->run_node, &cfs_rq->tasks_timeline); |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 401 | } |
| 402 | |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 403 | static struct sched_entity *__pick_next_entity(struct cfs_rq *cfs_rq) |
| 404 | { |
Peter Zijlstra | f4b6755 | 2008-11-04 21:25:07 +0100 | [diff] [blame] | 405 | struct rb_node *left = cfs_rq->rb_leftmost; |
| 406 | |
| 407 | if (!left) |
| 408 | return NULL; |
| 409 | |
| 410 | return rb_entry(left, struct sched_entity, run_node); |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 411 | } |
| 412 | |
Peter Zijlstra | f4b6755 | 2008-11-04 21:25:07 +0100 | [diff] [blame] | 413 | static struct sched_entity *__pick_last_entity(struct cfs_rq *cfs_rq) |
Peter Zijlstra | aeb73b0 | 2007-10-15 17:00:05 +0200 | [diff] [blame] | 414 | { |
Ingo Molnar | 7eee3e6 | 2008-02-22 10:32:21 +0100 | [diff] [blame] | 415 | struct rb_node *last = rb_last(&cfs_rq->tasks_timeline); |
Peter Zijlstra | aeb73b0 | 2007-10-15 17:00:05 +0200 | [diff] [blame] | 416 | |
Balbir Singh | 70eee74 | 2008-02-22 13:25:53 +0530 | [diff] [blame] | 417 | if (!last) |
| 418 | return NULL; |
Ingo Molnar | 7eee3e6 | 2008-02-22 10:32:21 +0100 | [diff] [blame] | 419 | |
| 420 | return rb_entry(last, struct sched_entity, run_node); |
Peter Zijlstra | aeb73b0 | 2007-10-15 17:00:05 +0200 | [diff] [blame] | 421 | } |
| 422 | |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 423 | /************************************************************** |
| 424 | * Scheduling class statistics methods: |
| 425 | */ |
| 426 | |
Peter Zijlstra | b2be5e9 | 2007-11-09 22:39:37 +0100 | [diff] [blame] | 427 | #ifdef CONFIG_SCHED_DEBUG |
Christian Ehrhardt | acb4a84 | 2009-11-30 12:16:48 +0100 | [diff] [blame] | 428 | int sched_proc_update_handler(struct ctl_table *table, int write, |
Alexey Dobriyan | 8d65af7 | 2009-09-23 15:57:19 -0700 | [diff] [blame] | 429 | void __user *buffer, size_t *lenp, |
Peter Zijlstra | b2be5e9 | 2007-11-09 22:39:37 +0100 | [diff] [blame] | 430 | loff_t *ppos) |
| 431 | { |
Alexey Dobriyan | 8d65af7 | 2009-09-23 15:57:19 -0700 | [diff] [blame] | 432 | int ret = proc_dointvec_minmax(table, write, buffer, lenp, ppos); |
Christian Ehrhardt | acb4a84 | 2009-11-30 12:16:48 +0100 | [diff] [blame] | 433 | int factor = get_update_sysctl_factor(); |
Peter Zijlstra | b2be5e9 | 2007-11-09 22:39:37 +0100 | [diff] [blame] | 434 | |
| 435 | if (ret || !write) |
| 436 | return ret; |
| 437 | |
| 438 | sched_nr_latency = DIV_ROUND_UP(sysctl_sched_latency, |
| 439 | sysctl_sched_min_granularity); |
| 440 | |
Christian Ehrhardt | acb4a84 | 2009-11-30 12:16:48 +0100 | [diff] [blame] | 441 | #define WRT_SYSCTL(name) \ |
| 442 | (normalized_sysctl_##name = sysctl_##name / (factor)) |
| 443 | WRT_SYSCTL(sched_min_granularity); |
| 444 | WRT_SYSCTL(sched_latency); |
| 445 | WRT_SYSCTL(sched_wakeup_granularity); |
Christian Ehrhardt | acb4a84 | 2009-11-30 12:16:48 +0100 | [diff] [blame] | 446 | #undef WRT_SYSCTL |
| 447 | |
Peter Zijlstra | b2be5e9 | 2007-11-09 22:39:37 +0100 | [diff] [blame] | 448 | return 0; |
| 449 | } |
| 450 | #endif |
Ingo Molnar | 647e7ca | 2007-10-15 17:00:13 +0200 | [diff] [blame] | 451 | |
| 452 | /* |
Peter Zijlstra | f9c0b09 | 2008-10-17 19:27:04 +0200 | [diff] [blame] | 453 | * delta /= w |
Peter Zijlstra | a7be37a | 2008-06-27 13:41:11 +0200 | [diff] [blame] | 454 | */ |
| 455 | static inline unsigned long |
| 456 | calc_delta_fair(unsigned long delta, struct sched_entity *se) |
| 457 | { |
Peter Zijlstra | f9c0b09 | 2008-10-17 19:27:04 +0200 | [diff] [blame] | 458 | if (unlikely(se->load.weight != NICE_0_LOAD)) |
| 459 | delta = calc_delta_mine(delta, NICE_0_LOAD, &se->load); |
Peter Zijlstra | a7be37a | 2008-06-27 13:41:11 +0200 | [diff] [blame] | 460 | |
| 461 | return delta; |
| 462 | } |
| 463 | |
| 464 | /* |
Ingo Molnar | 647e7ca | 2007-10-15 17:00:13 +0200 | [diff] [blame] | 465 | * The idea is to set a period in which each task runs once. |
| 466 | * |
| 467 | * When there are too many tasks (sysctl_sched_nr_latency) we have to stretch |
| 468 | * this period because otherwise the slices get too small. |
| 469 | * |
| 470 | * p = (nr <= nl) ? l : l*nr/nl |
| 471 | */ |
Peter Zijlstra | 4d78e7b | 2007-10-15 17:00:04 +0200 | [diff] [blame] | 472 | static u64 __sched_period(unsigned long nr_running) |
| 473 | { |
| 474 | u64 period = sysctl_sched_latency; |
Peter Zijlstra | b2be5e9 | 2007-11-09 22:39:37 +0100 | [diff] [blame] | 475 | unsigned long nr_latency = sched_nr_latency; |
Peter Zijlstra | 4d78e7b | 2007-10-15 17:00:04 +0200 | [diff] [blame] | 476 | |
| 477 | if (unlikely(nr_running > nr_latency)) { |
Peter Zijlstra | 4bf0b77 | 2008-01-25 21:08:21 +0100 | [diff] [blame] | 478 | period = sysctl_sched_min_granularity; |
Peter Zijlstra | 4d78e7b | 2007-10-15 17:00:04 +0200 | [diff] [blame] | 479 | period *= nr_running; |
Peter Zijlstra | 4d78e7b | 2007-10-15 17:00:04 +0200 | [diff] [blame] | 480 | } |
| 481 | |
| 482 | return period; |
| 483 | } |
| 484 | |
Ingo Molnar | 647e7ca | 2007-10-15 17:00:13 +0200 | [diff] [blame] | 485 | /* |
| 486 | * We calculate the wall-time slice from the period by taking a part |
| 487 | * proportional to the weight. |
| 488 | * |
Peter Zijlstra | f9c0b09 | 2008-10-17 19:27:04 +0200 | [diff] [blame] | 489 | * s = p*P[w/rw] |
Ingo Molnar | 647e7ca | 2007-10-15 17:00:13 +0200 | [diff] [blame] | 490 | */ |
Peter Zijlstra | 6d0f0ebd | 2007-10-15 17:00:05 +0200 | [diff] [blame] | 491 | 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] | 492 | { |
Mike Galbraith | 0a58244 | 2009-01-02 12:16:42 +0100 | [diff] [blame] | 493 | u64 slice = __sched_period(cfs_rq->nr_running + !se->on_rq); |
Peter Zijlstra | f9c0b09 | 2008-10-17 19:27:04 +0200 | [diff] [blame] | 494 | |
Mike Galbraith | 0a58244 | 2009-01-02 12:16:42 +0100 | [diff] [blame] | 495 | for_each_sched_entity(se) { |
Lin Ming | 6272d68 | 2009-01-15 17:17:15 +0100 | [diff] [blame] | 496 | struct load_weight *load; |
Christian Engelmayer | 3104bf0 | 2009-06-16 10:35:12 +0200 | [diff] [blame] | 497 | struct load_weight lw; |
Lin Ming | 6272d68 | 2009-01-15 17:17:15 +0100 | [diff] [blame] | 498 | |
| 499 | cfs_rq = cfs_rq_of(se); |
| 500 | load = &cfs_rq->load; |
Peter Zijlstra | f9c0b09 | 2008-10-17 19:27:04 +0200 | [diff] [blame] | 501 | |
Mike Galbraith | 0a58244 | 2009-01-02 12:16:42 +0100 | [diff] [blame] | 502 | if (unlikely(!se->on_rq)) { |
Christian Engelmayer | 3104bf0 | 2009-06-16 10:35:12 +0200 | [diff] [blame] | 503 | lw = cfs_rq->load; |
Mike Galbraith | 0a58244 | 2009-01-02 12:16:42 +0100 | [diff] [blame] | 504 | |
| 505 | update_load_add(&lw, se->load.weight); |
| 506 | load = &lw; |
| 507 | } |
| 508 | slice = calc_delta_mine(slice, se->load.weight, load); |
| 509 | } |
| 510 | return slice; |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 511 | } |
| 512 | |
Ingo Molnar | 647e7ca | 2007-10-15 17:00:13 +0200 | [diff] [blame] | 513 | /* |
Peter Zijlstra | ac884de | 2008-04-19 19:45:00 +0200 | [diff] [blame] | 514 | * We calculate the vruntime slice of a to be inserted task |
Ingo Molnar | 647e7ca | 2007-10-15 17:00:13 +0200 | [diff] [blame] | 515 | * |
Peter Zijlstra | f9c0b09 | 2008-10-17 19:27:04 +0200 | [diff] [blame] | 516 | * vs = s/w |
Ingo Molnar | 647e7ca | 2007-10-15 17:00:13 +0200 | [diff] [blame] | 517 | */ |
Peter Zijlstra | f9c0b09 | 2008-10-17 19:27:04 +0200 | [diff] [blame] | 518 | static u64 sched_vslice(struct cfs_rq *cfs_rq, struct sched_entity *se) |
Ingo Molnar | 647e7ca | 2007-10-15 17:00:13 +0200 | [diff] [blame] | 519 | { |
Peter Zijlstra | f9c0b09 | 2008-10-17 19:27:04 +0200 | [diff] [blame] | 520 | return calc_delta_fair(sched_slice(cfs_rq, se), se); |
Peter Zijlstra | a7be37a | 2008-06-27 13:41:11 +0200 | [diff] [blame] | 521 | } |
| 522 | |
| 523 | /* |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 524 | * Update the current task's runtime statistics. Skip current tasks that |
| 525 | * are not in our scheduling class. |
| 526 | */ |
| 527 | static inline void |
Ingo Molnar | 8ebc91d | 2007-10-15 17:00:03 +0200 | [diff] [blame] | 528 | __update_curr(struct cfs_rq *cfs_rq, struct sched_entity *curr, |
| 529 | unsigned long delta_exec) |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 530 | { |
Ingo Molnar | bbdba7c | 2007-10-15 17:00:06 +0200 | [diff] [blame] | 531 | unsigned long delta_exec_weighted; |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 532 | |
Lucas De Marchi | 41acab8 | 2010-03-10 23:37:45 -0300 | [diff] [blame] | 533 | schedstat_set(curr->statistics.exec_max, |
| 534 | max((u64)delta_exec, curr->statistics.exec_max)); |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 535 | |
| 536 | curr->sum_exec_runtime += delta_exec; |
Ingo Molnar | 7a62eab | 2007-10-15 17:00:06 +0200 | [diff] [blame] | 537 | schedstat_add(cfs_rq, exec_clock, delta_exec); |
Peter Zijlstra | a7be37a | 2008-06-27 13:41:11 +0200 | [diff] [blame] | 538 | delta_exec_weighted = calc_delta_fair(delta_exec, curr); |
Peter Zijlstra | 88ec22d | 2009-12-16 18:04:41 +0100 | [diff] [blame] | 539 | |
Ingo Molnar | e9acbff | 2007-10-15 17:00:04 +0200 | [diff] [blame] | 540 | curr->vruntime += delta_exec_weighted; |
Peter Zijlstra | 1af5f73 | 2008-10-24 11:06:13 +0200 | [diff] [blame] | 541 | update_min_vruntime(cfs_rq); |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 542 | } |
| 543 | |
Ingo Molnar | b7cc089 | 2007-08-09 11:16:47 +0200 | [diff] [blame] | 544 | static void update_curr(struct cfs_rq *cfs_rq) |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 545 | { |
Ingo Molnar | 429d43b | 2007-10-15 17:00:03 +0200 | [diff] [blame] | 546 | struct sched_entity *curr = cfs_rq->curr; |
Venkatesh Pallipadi | 305e683 | 2010-10-04 17:03:21 -0700 | [diff] [blame] | 547 | u64 now = rq_of(cfs_rq)->clock_task; |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 548 | unsigned long delta_exec; |
| 549 | |
| 550 | if (unlikely(!curr)) |
| 551 | return; |
| 552 | |
| 553 | /* |
| 554 | * Get the amount of time the current task was running |
| 555 | * since the last time we changed load (this cannot |
| 556 | * overflow on 32 bits): |
| 557 | */ |
Ingo Molnar | 8ebc91d | 2007-10-15 17:00:03 +0200 | [diff] [blame] | 558 | delta_exec = (unsigned long)(now - curr->exec_start); |
Peter Zijlstra | 34f28ec | 2008-12-16 08:45:31 +0100 | [diff] [blame] | 559 | if (!delta_exec) |
| 560 | return; |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 561 | |
Ingo Molnar | 8ebc91d | 2007-10-15 17:00:03 +0200 | [diff] [blame] | 562 | __update_curr(cfs_rq, curr, delta_exec); |
| 563 | curr->exec_start = now; |
Srivatsa Vaddagiri | d842de8 | 2007-12-02 20:04:49 +0100 | [diff] [blame] | 564 | |
| 565 | if (entity_is_task(curr)) { |
| 566 | struct task_struct *curtask = task_of(curr); |
| 567 | |
Ingo Molnar | f977bb4 | 2009-09-13 18:15:54 +0200 | [diff] [blame] | 568 | trace_sched_stat_runtime(curtask, delta_exec, curr->vruntime); |
Srivatsa Vaddagiri | d842de8 | 2007-12-02 20:04:49 +0100 | [diff] [blame] | 569 | cpuacct_charge(curtask, delta_exec); |
Frank Mayhar | f06febc | 2008-09-12 09:54:39 -0700 | [diff] [blame] | 570 | account_group_exec_runtime(curtask, delta_exec); |
Srivatsa Vaddagiri | d842de8 | 2007-12-02 20:04:49 +0100 | [diff] [blame] | 571 | } |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 572 | } |
| 573 | |
| 574 | static inline void |
Ingo Molnar | 5870db5 | 2007-08-09 11:16:47 +0200 | [diff] [blame] | 575 | 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] | 576 | { |
Lucas De Marchi | 41acab8 | 2010-03-10 23:37:45 -0300 | [diff] [blame] | 577 | schedstat_set(se->statistics.wait_start, rq_of(cfs_rq)->clock); |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 578 | } |
| 579 | |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 580 | /* |
| 581 | * Task is being enqueued - update stats: |
| 582 | */ |
Ingo Molnar | d2417e5 | 2007-08-09 11:16:47 +0200 | [diff] [blame] | 583 | 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] | 584 | { |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 585 | /* |
| 586 | * Are we enqueueing a waiting task? (for current tasks |
| 587 | * a dequeue/enqueue event is a NOP) |
| 588 | */ |
Ingo Molnar | 429d43b | 2007-10-15 17:00:03 +0200 | [diff] [blame] | 589 | if (se != cfs_rq->curr) |
Ingo Molnar | 5870db5 | 2007-08-09 11:16:47 +0200 | [diff] [blame] | 590 | update_stats_wait_start(cfs_rq, se); |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 591 | } |
| 592 | |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 593 | static void |
Ingo Molnar | 9ef0a96 | 2007-08-09 11:16:47 +0200 | [diff] [blame] | 594 | 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] | 595 | { |
Lucas De Marchi | 41acab8 | 2010-03-10 23:37:45 -0300 | [diff] [blame] | 596 | schedstat_set(se->statistics.wait_max, max(se->statistics.wait_max, |
| 597 | rq_of(cfs_rq)->clock - se->statistics.wait_start)); |
| 598 | schedstat_set(se->statistics.wait_count, se->statistics.wait_count + 1); |
| 599 | schedstat_set(se->statistics.wait_sum, se->statistics.wait_sum + |
| 600 | rq_of(cfs_rq)->clock - se->statistics.wait_start); |
Peter Zijlstra | 768d0c2 | 2009-07-23 20:13:26 +0200 | [diff] [blame] | 601 | #ifdef CONFIG_SCHEDSTATS |
| 602 | if (entity_is_task(se)) { |
| 603 | trace_sched_stat_wait(task_of(se), |
Lucas De Marchi | 41acab8 | 2010-03-10 23:37:45 -0300 | [diff] [blame] | 604 | rq_of(cfs_rq)->clock - se->statistics.wait_start); |
Peter Zijlstra | 768d0c2 | 2009-07-23 20:13:26 +0200 | [diff] [blame] | 605 | } |
| 606 | #endif |
Lucas De Marchi | 41acab8 | 2010-03-10 23:37:45 -0300 | [diff] [blame] | 607 | schedstat_set(se->statistics.wait_start, 0); |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 608 | } |
| 609 | |
| 610 | static inline void |
Ingo Molnar | 19b6a2e | 2007-08-09 11:16:48 +0200 | [diff] [blame] | 611 | update_stats_dequeue(struct cfs_rq *cfs_rq, struct sched_entity *se) |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 612 | { |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 613 | /* |
| 614 | * Mark the end of the wait period if dequeueing a |
| 615 | * waiting task: |
| 616 | */ |
Ingo Molnar | 429d43b | 2007-10-15 17:00:03 +0200 | [diff] [blame] | 617 | if (se != cfs_rq->curr) |
Ingo Molnar | 9ef0a96 | 2007-08-09 11:16:47 +0200 | [diff] [blame] | 618 | update_stats_wait_end(cfs_rq, se); |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 619 | } |
| 620 | |
| 621 | /* |
| 622 | * We are picking a new current task - update its stats: |
| 623 | */ |
| 624 | static inline void |
Ingo Molnar | 79303e9 | 2007-08-09 11:16:47 +0200 | [diff] [blame] | 625 | 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] | 626 | { |
| 627 | /* |
| 628 | * We are starting a new run period: |
| 629 | */ |
Venkatesh Pallipadi | 305e683 | 2010-10-04 17:03:21 -0700 | [diff] [blame] | 630 | se->exec_start = rq_of(cfs_rq)->clock_task; |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 631 | } |
| 632 | |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 633 | /************************************************** |
| 634 | * Scheduling class queueing methods: |
| 635 | */ |
| 636 | |
Peter Zijlstra | c09595f | 2008-06-27 13:41:14 +0200 | [diff] [blame] | 637 | #if defined CONFIG_SMP && defined CONFIG_FAIR_GROUP_SCHED |
| 638 | static void |
| 639 | add_cfs_task_weight(struct cfs_rq *cfs_rq, unsigned long weight) |
| 640 | { |
| 641 | cfs_rq->task_weight += weight; |
| 642 | } |
| 643 | #else |
| 644 | static inline void |
| 645 | add_cfs_task_weight(struct cfs_rq *cfs_rq, unsigned long weight) |
| 646 | { |
| 647 | } |
| 648 | #endif |
| 649 | |
Dmitry Adamushko | 30cfdcf | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 650 | static void |
| 651 | account_entity_enqueue(struct cfs_rq *cfs_rq, struct sched_entity *se) |
| 652 | { |
| 653 | update_load_add(&cfs_rq->load, se->load.weight); |
Peter Zijlstra | c09595f | 2008-06-27 13:41:14 +0200 | [diff] [blame] | 654 | if (!parent_entity(se)) |
| 655 | inc_cpu_load(rq_of(cfs_rq), se->load.weight); |
Bharata B Rao | b87f172 | 2008-09-25 09:53:54 +0530 | [diff] [blame] | 656 | if (entity_is_task(se)) { |
Peter Zijlstra | c09595f | 2008-06-27 13:41:14 +0200 | [diff] [blame] | 657 | add_cfs_task_weight(cfs_rq, se->load.weight); |
Bharata B Rao | b87f172 | 2008-09-25 09:53:54 +0530 | [diff] [blame] | 658 | list_add(&se->group_node, &cfs_rq->tasks); |
| 659 | } |
Dmitry Adamushko | 30cfdcf | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 660 | cfs_rq->nr_running++; |
Dmitry Adamushko | 30cfdcf | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 661 | } |
| 662 | |
| 663 | static void |
| 664 | account_entity_dequeue(struct cfs_rq *cfs_rq, struct sched_entity *se) |
| 665 | { |
| 666 | update_load_sub(&cfs_rq->load, se->load.weight); |
Peter Zijlstra | c09595f | 2008-06-27 13:41:14 +0200 | [diff] [blame] | 667 | if (!parent_entity(se)) |
| 668 | dec_cpu_load(rq_of(cfs_rq), se->load.weight); |
Bharata B Rao | b87f172 | 2008-09-25 09:53:54 +0530 | [diff] [blame] | 669 | if (entity_is_task(se)) { |
Peter Zijlstra | c09595f | 2008-06-27 13:41:14 +0200 | [diff] [blame] | 670 | add_cfs_task_weight(cfs_rq, -se->load.weight); |
Bharata B Rao | b87f172 | 2008-09-25 09:53:54 +0530 | [diff] [blame] | 671 | list_del_init(&se->group_node); |
| 672 | } |
Dmitry Adamushko | 30cfdcf | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 673 | cfs_rq->nr_running--; |
Dmitry Adamushko | 30cfdcf | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 674 | } |
| 675 | |
Peter Zijlstra | 2069dd7 | 2010-11-15 15:47:00 -0800 | [diff] [blame] | 676 | #if defined CONFIG_SMP && defined CONFIG_FAIR_GROUP_SCHED |
Peter Zijlstra | 3d4b47b | 2010-11-15 15:47:01 -0800 | [diff] [blame^] | 677 | static void update_cfs_load(struct cfs_rq *cfs_rq, int lb) |
Peter Zijlstra | 2069dd7 | 2010-11-15 15:47:00 -0800 | [diff] [blame] | 678 | { |
| 679 | u64 period = sched_avg_period(); |
| 680 | u64 now, delta; |
| 681 | |
| 682 | if (!cfs_rq) |
| 683 | return; |
| 684 | |
| 685 | now = rq_of(cfs_rq)->clock; |
| 686 | delta = now - cfs_rq->load_stamp; |
| 687 | |
| 688 | cfs_rq->load_stamp = now; |
| 689 | cfs_rq->load_period += delta; |
| 690 | cfs_rq->load_avg += delta * cfs_rq->load.weight; |
| 691 | |
| 692 | while (cfs_rq->load_period > period) { |
| 693 | /* |
| 694 | * Inline assembly required to prevent the compiler |
| 695 | * optimising this loop into a divmod call. |
| 696 | * See __iter_div_u64_rem() for another example of this. |
| 697 | */ |
| 698 | asm("" : "+rm" (cfs_rq->load_period)); |
| 699 | cfs_rq->load_period /= 2; |
| 700 | cfs_rq->load_avg /= 2; |
| 701 | } |
Peter Zijlstra | 3d4b47b | 2010-11-15 15:47:01 -0800 | [diff] [blame^] | 702 | |
| 703 | if (lb && !cfs_rq->nr_running) { |
| 704 | if (cfs_rq->load_avg < (period / 8)) |
| 705 | list_del_leaf_cfs_rq(cfs_rq); |
| 706 | } |
Peter Zijlstra | 2069dd7 | 2010-11-15 15:47:00 -0800 | [diff] [blame] | 707 | } |
| 708 | |
| 709 | static void reweight_entity(struct cfs_rq *cfs_rq, struct sched_entity *se, |
| 710 | unsigned long weight) |
| 711 | { |
| 712 | if (se->on_rq) |
| 713 | account_entity_dequeue(cfs_rq, se); |
| 714 | |
| 715 | update_load_set(&se->load, weight); |
| 716 | |
| 717 | if (se->on_rq) |
| 718 | account_entity_enqueue(cfs_rq, se); |
| 719 | } |
| 720 | |
| 721 | static void update_cfs_shares(struct cfs_rq *cfs_rq) |
| 722 | { |
| 723 | struct task_group *tg; |
| 724 | struct sched_entity *se; |
| 725 | long load_weight, load, shares; |
| 726 | |
| 727 | if (!cfs_rq) |
| 728 | return; |
| 729 | |
| 730 | tg = cfs_rq->tg; |
| 731 | se = tg->se[cpu_of(rq_of(cfs_rq))]; |
| 732 | if (!se) |
| 733 | return; |
| 734 | |
| 735 | load = cfs_rq->load.weight; |
| 736 | |
| 737 | load_weight = atomic_read(&tg->load_weight); |
| 738 | load_weight -= cfs_rq->load_contribution; |
| 739 | load_weight += load; |
| 740 | |
| 741 | shares = (tg->shares * load); |
| 742 | if (load_weight) |
| 743 | shares /= load_weight; |
| 744 | |
| 745 | if (shares < MIN_SHARES) |
| 746 | shares = MIN_SHARES; |
| 747 | if (shares > tg->shares) |
| 748 | shares = tg->shares; |
| 749 | |
| 750 | reweight_entity(cfs_rq_of(se), se, shares); |
| 751 | } |
| 752 | #else /* CONFIG_FAIR_GROUP_SCHED */ |
Peter Zijlstra | 3d4b47b | 2010-11-15 15:47:01 -0800 | [diff] [blame^] | 753 | static inline void update_cfs_load(struct cfs_rq *cfs_rq, int lb) |
Peter Zijlstra | 2069dd7 | 2010-11-15 15:47:00 -0800 | [diff] [blame] | 754 | { |
| 755 | } |
| 756 | |
| 757 | static inline void update_cfs_shares(struct cfs_rq *cfs_rq) |
| 758 | { |
| 759 | } |
| 760 | #endif /* CONFIG_FAIR_GROUP_SCHED */ |
| 761 | |
Ingo Molnar | 2396af6 | 2007-08-09 11:16:48 +0200 | [diff] [blame] | 762 | 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] | 763 | { |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 764 | #ifdef CONFIG_SCHEDSTATS |
Peter Zijlstra | e414314 | 2009-07-23 20:13:26 +0200 | [diff] [blame] | 765 | struct task_struct *tsk = NULL; |
| 766 | |
| 767 | if (entity_is_task(se)) |
| 768 | tsk = task_of(se); |
| 769 | |
Lucas De Marchi | 41acab8 | 2010-03-10 23:37:45 -0300 | [diff] [blame] | 770 | if (se->statistics.sleep_start) { |
| 771 | u64 delta = rq_of(cfs_rq)->clock - se->statistics.sleep_start; |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 772 | |
| 773 | if ((s64)delta < 0) |
| 774 | delta = 0; |
| 775 | |
Lucas De Marchi | 41acab8 | 2010-03-10 23:37:45 -0300 | [diff] [blame] | 776 | if (unlikely(delta > se->statistics.sleep_max)) |
| 777 | se->statistics.sleep_max = delta; |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 778 | |
Lucas De Marchi | 41acab8 | 2010-03-10 23:37:45 -0300 | [diff] [blame] | 779 | se->statistics.sleep_start = 0; |
| 780 | se->statistics.sum_sleep_runtime += delta; |
Arjan van de Ven | 9745512 | 2008-01-25 21:08:34 +0100 | [diff] [blame] | 781 | |
Peter Zijlstra | 768d0c2 | 2009-07-23 20:13:26 +0200 | [diff] [blame] | 782 | if (tsk) { |
Peter Zijlstra | e414314 | 2009-07-23 20:13:26 +0200 | [diff] [blame] | 783 | account_scheduler_latency(tsk, delta >> 10, 1); |
Peter Zijlstra | 768d0c2 | 2009-07-23 20:13:26 +0200 | [diff] [blame] | 784 | trace_sched_stat_sleep(tsk, delta); |
| 785 | } |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 786 | } |
Lucas De Marchi | 41acab8 | 2010-03-10 23:37:45 -0300 | [diff] [blame] | 787 | if (se->statistics.block_start) { |
| 788 | u64 delta = rq_of(cfs_rq)->clock - se->statistics.block_start; |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 789 | |
| 790 | if ((s64)delta < 0) |
| 791 | delta = 0; |
| 792 | |
Lucas De Marchi | 41acab8 | 2010-03-10 23:37:45 -0300 | [diff] [blame] | 793 | if (unlikely(delta > se->statistics.block_max)) |
| 794 | se->statistics.block_max = delta; |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 795 | |
Lucas De Marchi | 41acab8 | 2010-03-10 23:37:45 -0300 | [diff] [blame] | 796 | se->statistics.block_start = 0; |
| 797 | se->statistics.sum_sleep_runtime += delta; |
Ingo Molnar | 30084fb | 2007-10-02 14:13:08 +0200 | [diff] [blame] | 798 | |
Peter Zijlstra | e414314 | 2009-07-23 20:13:26 +0200 | [diff] [blame] | 799 | if (tsk) { |
Arjan van de Ven | 8f0dfc3 | 2009-07-20 11:26:58 -0700 | [diff] [blame] | 800 | if (tsk->in_iowait) { |
Lucas De Marchi | 41acab8 | 2010-03-10 23:37:45 -0300 | [diff] [blame] | 801 | se->statistics.iowait_sum += delta; |
| 802 | se->statistics.iowait_count++; |
Peter Zijlstra | 768d0c2 | 2009-07-23 20:13:26 +0200 | [diff] [blame] | 803 | trace_sched_stat_iowait(tsk, delta); |
Arjan van de Ven | 8f0dfc3 | 2009-07-20 11:26:58 -0700 | [diff] [blame] | 804 | } |
| 805 | |
Peter Zijlstra | e414314 | 2009-07-23 20:13:26 +0200 | [diff] [blame] | 806 | /* |
| 807 | * Blocking time is in units of nanosecs, so shift by |
| 808 | * 20 to get a milliseconds-range estimation of the |
| 809 | * amount of time that the task spent sleeping: |
| 810 | */ |
| 811 | if (unlikely(prof_on == SLEEP_PROFILING)) { |
| 812 | profile_hits(SLEEP_PROFILING, |
| 813 | (void *)get_wchan(tsk), |
| 814 | delta >> 20); |
| 815 | } |
| 816 | account_scheduler_latency(tsk, delta >> 10, 0); |
Ingo Molnar | 30084fb | 2007-10-02 14:13:08 +0200 | [diff] [blame] | 817 | } |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 818 | } |
| 819 | #endif |
| 820 | } |
| 821 | |
Peter Zijlstra | ddc9729 | 2007-10-15 17:00:10 +0200 | [diff] [blame] | 822 | static void check_spread(struct cfs_rq *cfs_rq, struct sched_entity *se) |
| 823 | { |
| 824 | #ifdef CONFIG_SCHED_DEBUG |
| 825 | s64 d = se->vruntime - cfs_rq->min_vruntime; |
| 826 | |
| 827 | if (d < 0) |
| 828 | d = -d; |
| 829 | |
| 830 | if (d > 3*sysctl_sched_latency) |
| 831 | schedstat_inc(cfs_rq, nr_spread_over); |
| 832 | #endif |
| 833 | } |
| 834 | |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 835 | static void |
Peter Zijlstra | aeb73b0 | 2007-10-15 17:00:05 +0200 | [diff] [blame] | 836 | place_entity(struct cfs_rq *cfs_rq, struct sched_entity *se, int initial) |
| 837 | { |
Peter Zijlstra | 1af5f73 | 2008-10-24 11:06:13 +0200 | [diff] [blame] | 838 | u64 vruntime = cfs_rq->min_vruntime; |
Peter Zijlstra | 94dfb5e | 2007-10-15 17:00:05 +0200 | [diff] [blame] | 839 | |
Peter Zijlstra | 2cb8600 | 2007-11-09 22:39:37 +0100 | [diff] [blame] | 840 | /* |
| 841 | * The 'current' period is already promised to the current tasks, |
| 842 | * however the extra weight of the new task will slow them down a |
| 843 | * little, place the new task so that it fits in the slot that |
| 844 | * stays open at the end. |
| 845 | */ |
Peter Zijlstra | 94dfb5e | 2007-10-15 17:00:05 +0200 | [diff] [blame] | 846 | if (initial && sched_feat(START_DEBIT)) |
Peter Zijlstra | f9c0b09 | 2008-10-17 19:27:04 +0200 | [diff] [blame] | 847 | vruntime += sched_vslice(cfs_rq, se); |
Peter Zijlstra | aeb73b0 | 2007-10-15 17:00:05 +0200 | [diff] [blame] | 848 | |
Mike Galbraith | a2e7a7e | 2009-09-18 09:19:25 +0200 | [diff] [blame] | 849 | /* sleeps up to a single latency don't count. */ |
Mike Galbraith | 5ca9880 | 2010-03-11 17:17:17 +0100 | [diff] [blame] | 850 | if (!initial) { |
Mike Galbraith | a2e7a7e | 2009-09-18 09:19:25 +0200 | [diff] [blame] | 851 | unsigned long thresh = sysctl_sched_latency; |
Peter Zijlstra | a7be37a | 2008-06-27 13:41:11 +0200 | [diff] [blame] | 852 | |
Mike Galbraith | a2e7a7e | 2009-09-18 09:19:25 +0200 | [diff] [blame] | 853 | /* |
Mike Galbraith | a2e7a7e | 2009-09-18 09:19:25 +0200 | [diff] [blame] | 854 | * Halve their sleep time's effect, to allow |
| 855 | * for a gentler effect of sleepers: |
| 856 | */ |
| 857 | if (sched_feat(GENTLE_FAIR_SLEEPERS)) |
| 858 | thresh >>= 1; |
Ingo Molnar | 51e0304 | 2009-09-16 08:54:45 +0200 | [diff] [blame] | 859 | |
Mike Galbraith | a2e7a7e | 2009-09-18 09:19:25 +0200 | [diff] [blame] | 860 | vruntime -= thresh; |
Peter Zijlstra | aeb73b0 | 2007-10-15 17:00:05 +0200 | [diff] [blame] | 861 | } |
| 862 | |
Mike Galbraith | b5d9d73 | 2009-09-08 11:12:28 +0200 | [diff] [blame] | 863 | /* ensure we never gain time by being placed backwards. */ |
| 864 | vruntime = max_vruntime(se->vruntime, vruntime); |
| 865 | |
Peter Zijlstra | 67e9fb2 | 2007-10-15 17:00:10 +0200 | [diff] [blame] | 866 | se->vruntime = vruntime; |
Peter Zijlstra | aeb73b0 | 2007-10-15 17:00:05 +0200 | [diff] [blame] | 867 | } |
| 868 | |
| 869 | static void |
Peter Zijlstra | 88ec22d | 2009-12-16 18:04:41 +0100 | [diff] [blame] | 870 | enqueue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se, int flags) |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 871 | { |
| 872 | /* |
Peter Zijlstra | 88ec22d | 2009-12-16 18:04:41 +0100 | [diff] [blame] | 873 | * Update the normalized vruntime before updating min_vruntime |
| 874 | * through callig update_curr(). |
| 875 | */ |
Peter Zijlstra | 371fd7e | 2010-03-24 16:38:48 +0100 | [diff] [blame] | 876 | if (!(flags & ENQUEUE_WAKEUP) || (flags & ENQUEUE_WAKING)) |
Peter Zijlstra | 88ec22d | 2009-12-16 18:04:41 +0100 | [diff] [blame] | 877 | se->vruntime += cfs_rq->min_vruntime; |
| 878 | |
| 879 | /* |
Dmitry Adamushko | a2a2d68 | 2007-10-15 17:00:13 +0200 | [diff] [blame] | 880 | * Update run-time statistics of the 'current'. |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 881 | */ |
Ingo Molnar | b7cc089 | 2007-08-09 11:16:47 +0200 | [diff] [blame] | 882 | update_curr(cfs_rq); |
Peter Zijlstra | 3d4b47b | 2010-11-15 15:47:01 -0800 | [diff] [blame^] | 883 | update_cfs_load(cfs_rq, 0); |
Peter Zijlstra | a992241 | 2008-05-05 23:56:17 +0200 | [diff] [blame] | 884 | account_entity_enqueue(cfs_rq, se); |
Peter Zijlstra | 2069dd7 | 2010-11-15 15:47:00 -0800 | [diff] [blame] | 885 | update_cfs_shares(cfs_rq); |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 886 | |
Peter Zijlstra | 88ec22d | 2009-12-16 18:04:41 +0100 | [diff] [blame] | 887 | if (flags & ENQUEUE_WAKEUP) { |
Peter Zijlstra | aeb73b0 | 2007-10-15 17:00:05 +0200 | [diff] [blame] | 888 | place_entity(cfs_rq, se, 0); |
Ingo Molnar | 2396af6 | 2007-08-09 11:16:48 +0200 | [diff] [blame] | 889 | enqueue_sleeper(cfs_rq, se); |
Ingo Molnar | e9acbff | 2007-10-15 17:00:04 +0200 | [diff] [blame] | 890 | } |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 891 | |
Ingo Molnar | d2417e5 | 2007-08-09 11:16:47 +0200 | [diff] [blame] | 892 | update_stats_enqueue(cfs_rq, se); |
Peter Zijlstra | ddc9729 | 2007-10-15 17:00:10 +0200 | [diff] [blame] | 893 | check_spread(cfs_rq, se); |
Srivatsa Vaddagiri | 83b699e | 2007-10-15 17:00:08 +0200 | [diff] [blame] | 894 | if (se != cfs_rq->curr) |
| 895 | __enqueue_entity(cfs_rq, se); |
Peter Zijlstra | 2069dd7 | 2010-11-15 15:47:00 -0800 | [diff] [blame] | 896 | se->on_rq = 1; |
Peter Zijlstra | 3d4b47b | 2010-11-15 15:47:01 -0800 | [diff] [blame^] | 897 | |
| 898 | if (cfs_rq->nr_running == 1) |
| 899 | list_add_leaf_cfs_rq(cfs_rq); |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 900 | } |
| 901 | |
Peter Zijlstra | a571bbe | 2009-01-28 14:51:40 +0100 | [diff] [blame] | 902 | static void __clear_buddies(struct cfs_rq *cfs_rq, struct sched_entity *se) |
Peter Zijlstra | 2002c69 | 2008-11-11 11:52:33 +0100 | [diff] [blame] | 903 | { |
Peter Zijlstra | de69a80 | 2009-09-17 09:01:20 +0200 | [diff] [blame] | 904 | if (!se || cfs_rq->last == se) |
Peter Zijlstra | 2002c69 | 2008-11-11 11:52:33 +0100 | [diff] [blame] | 905 | cfs_rq->last = NULL; |
| 906 | |
Peter Zijlstra | de69a80 | 2009-09-17 09:01:20 +0200 | [diff] [blame] | 907 | if (!se || cfs_rq->next == se) |
Peter Zijlstra | 2002c69 | 2008-11-11 11:52:33 +0100 | [diff] [blame] | 908 | cfs_rq->next = NULL; |
| 909 | } |
| 910 | |
Peter Zijlstra | a571bbe | 2009-01-28 14:51:40 +0100 | [diff] [blame] | 911 | static void clear_buddies(struct cfs_rq *cfs_rq, struct sched_entity *se) |
| 912 | { |
| 913 | for_each_sched_entity(se) |
| 914 | __clear_buddies(cfs_rq_of(se), se); |
| 915 | } |
| 916 | |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 917 | static void |
Peter Zijlstra | 371fd7e | 2010-03-24 16:38:48 +0100 | [diff] [blame] | 918 | dequeue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se, int flags) |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 919 | { |
Dmitry Adamushko | a2a2d68 | 2007-10-15 17:00:13 +0200 | [diff] [blame] | 920 | /* |
| 921 | * Update run-time statistics of the 'current'. |
| 922 | */ |
| 923 | update_curr(cfs_rq); |
| 924 | |
Ingo Molnar | 19b6a2e | 2007-08-09 11:16:48 +0200 | [diff] [blame] | 925 | update_stats_dequeue(cfs_rq, se); |
Peter Zijlstra | 371fd7e | 2010-03-24 16:38:48 +0100 | [diff] [blame] | 926 | if (flags & DEQUEUE_SLEEP) { |
Peter Zijlstra | 67e9fb2 | 2007-10-15 17:00:10 +0200 | [diff] [blame] | 927 | #ifdef CONFIG_SCHEDSTATS |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 928 | if (entity_is_task(se)) { |
| 929 | struct task_struct *tsk = task_of(se); |
| 930 | |
| 931 | if (tsk->state & TASK_INTERRUPTIBLE) |
Lucas De Marchi | 41acab8 | 2010-03-10 23:37:45 -0300 | [diff] [blame] | 932 | se->statistics.sleep_start = rq_of(cfs_rq)->clock; |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 933 | if (tsk->state & TASK_UNINTERRUPTIBLE) |
Lucas De Marchi | 41acab8 | 2010-03-10 23:37:45 -0300 | [diff] [blame] | 934 | se->statistics.block_start = rq_of(cfs_rq)->clock; |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 935 | } |
Dmitry Adamushko | db36cc7 | 2007-10-15 17:00:06 +0200 | [diff] [blame] | 936 | #endif |
Peter Zijlstra | 67e9fb2 | 2007-10-15 17:00:10 +0200 | [diff] [blame] | 937 | } |
| 938 | |
Peter Zijlstra | 2002c69 | 2008-11-11 11:52:33 +0100 | [diff] [blame] | 939 | clear_buddies(cfs_rq, se); |
Peter Zijlstra | 4793241 | 2008-11-04 21:25:09 +0100 | [diff] [blame] | 940 | |
Srivatsa Vaddagiri | 83b699e | 2007-10-15 17:00:08 +0200 | [diff] [blame] | 941 | if (se != cfs_rq->curr) |
Dmitry Adamushko | 30cfdcf | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 942 | __dequeue_entity(cfs_rq, se); |
Peter Zijlstra | 2069dd7 | 2010-11-15 15:47:00 -0800 | [diff] [blame] | 943 | se->on_rq = 0; |
Peter Zijlstra | 3d4b47b | 2010-11-15 15:47:01 -0800 | [diff] [blame^] | 944 | update_cfs_load(cfs_rq, 0); |
Dmitry Adamushko | 30cfdcf | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 945 | account_entity_dequeue(cfs_rq, se); |
Peter Zijlstra | 1af5f73 | 2008-10-24 11:06:13 +0200 | [diff] [blame] | 946 | update_min_vruntime(cfs_rq); |
Peter Zijlstra | 2069dd7 | 2010-11-15 15:47:00 -0800 | [diff] [blame] | 947 | update_cfs_shares(cfs_rq); |
Peter Zijlstra | 88ec22d | 2009-12-16 18:04:41 +0100 | [diff] [blame] | 948 | |
| 949 | /* |
| 950 | * Normalize the entity after updating the min_vruntime because the |
| 951 | * update can refer to the ->curr item and we need to reflect this |
| 952 | * movement in our normalized position. |
| 953 | */ |
Peter Zijlstra | 371fd7e | 2010-03-24 16:38:48 +0100 | [diff] [blame] | 954 | if (!(flags & DEQUEUE_SLEEP)) |
Peter Zijlstra | 88ec22d | 2009-12-16 18:04:41 +0100 | [diff] [blame] | 955 | se->vruntime -= cfs_rq->min_vruntime; |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 956 | } |
| 957 | |
| 958 | /* |
| 959 | * Preempt the current task with a newly woken task if needed: |
| 960 | */ |
Peter Zijlstra | 7c92e54 | 2007-09-05 14:32:49 +0200 | [diff] [blame] | 961 | static void |
Ingo Molnar | 2e09bf5 | 2007-10-15 17:00:05 +0200 | [diff] [blame] | 962 | check_preempt_tick(struct cfs_rq *cfs_rq, struct sched_entity *curr) |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 963 | { |
Peter Zijlstra | 1169783 | 2007-09-05 14:32:49 +0200 | [diff] [blame] | 964 | unsigned long ideal_runtime, delta_exec; |
| 965 | |
Peter Zijlstra | 6d0f0ebd | 2007-10-15 17:00:05 +0200 | [diff] [blame] | 966 | ideal_runtime = sched_slice(cfs_rq, curr); |
Peter Zijlstra | 1169783 | 2007-09-05 14:32:49 +0200 | [diff] [blame] | 967 | delta_exec = curr->sum_exec_runtime - curr->prev_sum_exec_runtime; |
Mike Galbraith | a9f3e2b | 2009-01-28 14:51:39 +0100 | [diff] [blame] | 968 | if (delta_exec > ideal_runtime) { |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 969 | resched_task(rq_of(cfs_rq)->curr); |
Mike Galbraith | a9f3e2b | 2009-01-28 14:51:39 +0100 | [diff] [blame] | 970 | /* |
| 971 | * The current task ran long enough, ensure it doesn't get |
| 972 | * re-elected due to buddy favours. |
| 973 | */ |
| 974 | clear_buddies(cfs_rq, curr); |
Mike Galbraith | f685cea | 2009-10-23 23:09:22 +0200 | [diff] [blame] | 975 | return; |
| 976 | } |
| 977 | |
| 978 | /* |
| 979 | * Ensure that a task that missed wakeup preemption by a |
| 980 | * narrow margin doesn't have to wait for a full slice. |
| 981 | * This also mitigates buddy induced latencies under load. |
| 982 | */ |
| 983 | if (!sched_feat(WAKEUP_PREEMPT)) |
| 984 | return; |
| 985 | |
| 986 | if (delta_exec < sysctl_sched_min_granularity) |
| 987 | return; |
| 988 | |
| 989 | if (cfs_rq->nr_running > 1) { |
| 990 | struct sched_entity *se = __pick_next_entity(cfs_rq); |
| 991 | s64 delta = curr->vruntime - se->vruntime; |
| 992 | |
| 993 | if (delta > ideal_runtime) |
| 994 | resched_task(rq_of(cfs_rq)->curr); |
Mike Galbraith | a9f3e2b | 2009-01-28 14:51:39 +0100 | [diff] [blame] | 995 | } |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 996 | } |
| 997 | |
Srivatsa Vaddagiri | 83b699e | 2007-10-15 17:00:08 +0200 | [diff] [blame] | 998 | static void |
Ingo Molnar | 8494f41 | 2007-08-09 11:16:48 +0200 | [diff] [blame] | 999 | set_next_entity(struct cfs_rq *cfs_rq, struct sched_entity *se) |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 1000 | { |
Srivatsa Vaddagiri | 83b699e | 2007-10-15 17:00:08 +0200 | [diff] [blame] | 1001 | /* 'current' is not kept within the tree. */ |
| 1002 | if (se->on_rq) { |
| 1003 | /* |
| 1004 | * Any task has to be enqueued before it get to execute on |
| 1005 | * a CPU. So account for the time it spent waiting on the |
| 1006 | * runqueue. |
| 1007 | */ |
| 1008 | update_stats_wait_end(cfs_rq, se); |
| 1009 | __dequeue_entity(cfs_rq, se); |
| 1010 | } |
| 1011 | |
Ingo Molnar | 79303e9 | 2007-08-09 11:16:47 +0200 | [diff] [blame] | 1012 | update_stats_curr_start(cfs_rq, se); |
Ingo Molnar | 429d43b | 2007-10-15 17:00:03 +0200 | [diff] [blame] | 1013 | cfs_rq->curr = se; |
Ingo Molnar | eba1ed4 | 2007-10-15 17:00:02 +0200 | [diff] [blame] | 1014 | #ifdef CONFIG_SCHEDSTATS |
| 1015 | /* |
| 1016 | * Track our maximum slice length, if the CPU's load is at |
| 1017 | * least twice that of our own weight (i.e. dont track it |
| 1018 | * when there are only lesser-weight tasks around): |
| 1019 | */ |
Dmitry Adamushko | 495eca4 | 2007-10-15 17:00:06 +0200 | [diff] [blame] | 1020 | if (rq_of(cfs_rq)->load.weight >= 2*se->load.weight) { |
Lucas De Marchi | 41acab8 | 2010-03-10 23:37:45 -0300 | [diff] [blame] | 1021 | se->statistics.slice_max = max(se->statistics.slice_max, |
Ingo Molnar | eba1ed4 | 2007-10-15 17:00:02 +0200 | [diff] [blame] | 1022 | se->sum_exec_runtime - se->prev_sum_exec_runtime); |
| 1023 | } |
| 1024 | #endif |
Peter Zijlstra | 4a55b45 | 2007-09-05 14:32:49 +0200 | [diff] [blame] | 1025 | se->prev_sum_exec_runtime = se->sum_exec_runtime; |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 1026 | } |
| 1027 | |
Peter Zijlstra | 3f3a490 | 2008-10-24 11:06:16 +0200 | [diff] [blame] | 1028 | static int |
| 1029 | wakeup_preempt_entity(struct sched_entity *curr, struct sched_entity *se); |
| 1030 | |
Peter Zijlstra | f4b6755 | 2008-11-04 21:25:07 +0100 | [diff] [blame] | 1031 | static struct sched_entity *pick_next_entity(struct cfs_rq *cfs_rq) |
Peter Zijlstra | aa2ac25 | 2008-03-14 21:12:12 +0100 | [diff] [blame] | 1032 | { |
Peter Zijlstra | f4b6755 | 2008-11-04 21:25:07 +0100 | [diff] [blame] | 1033 | struct sched_entity *se = __pick_next_entity(cfs_rq); |
Mike Galbraith | f685cea | 2009-10-23 23:09:22 +0200 | [diff] [blame] | 1034 | struct sched_entity *left = se; |
Peter Zijlstra | f4b6755 | 2008-11-04 21:25:07 +0100 | [diff] [blame] | 1035 | |
Mike Galbraith | f685cea | 2009-10-23 23:09:22 +0200 | [diff] [blame] | 1036 | if (cfs_rq->next && wakeup_preempt_entity(cfs_rq->next, left) < 1) |
| 1037 | se = cfs_rq->next; |
Peter Zijlstra | aa2ac25 | 2008-03-14 21:12:12 +0100 | [diff] [blame] | 1038 | |
Mike Galbraith | f685cea | 2009-10-23 23:09:22 +0200 | [diff] [blame] | 1039 | /* |
| 1040 | * Prefer last buddy, try to return the CPU to a preempted task. |
| 1041 | */ |
| 1042 | if (cfs_rq->last && wakeup_preempt_entity(cfs_rq->last, left) < 1) |
| 1043 | se = cfs_rq->last; |
| 1044 | |
| 1045 | clear_buddies(cfs_rq, se); |
Peter Zijlstra | 4793241 | 2008-11-04 21:25:09 +0100 | [diff] [blame] | 1046 | |
| 1047 | return se; |
Peter Zijlstra | aa2ac25 | 2008-03-14 21:12:12 +0100 | [diff] [blame] | 1048 | } |
| 1049 | |
Ingo Molnar | ab6cde2 | 2007-08-09 11:16:48 +0200 | [diff] [blame] | 1050 | 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] | 1051 | { |
| 1052 | /* |
| 1053 | * If still on the runqueue then deactivate_task() |
| 1054 | * was not called and update_curr() has to be done: |
| 1055 | */ |
| 1056 | if (prev->on_rq) |
Ingo Molnar | b7cc089 | 2007-08-09 11:16:47 +0200 | [diff] [blame] | 1057 | update_curr(cfs_rq); |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 1058 | |
Peter Zijlstra | ddc9729 | 2007-10-15 17:00:10 +0200 | [diff] [blame] | 1059 | check_spread(cfs_rq, prev); |
Dmitry Adamushko | 30cfdcf | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 1060 | if (prev->on_rq) { |
Ingo Molnar | 5870db5 | 2007-08-09 11:16:47 +0200 | [diff] [blame] | 1061 | update_stats_wait_start(cfs_rq, prev); |
Dmitry Adamushko | 30cfdcf | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 1062 | /* Put 'current' back into the tree. */ |
| 1063 | __enqueue_entity(cfs_rq, prev); |
| 1064 | } |
Ingo Molnar | 429d43b | 2007-10-15 17:00:03 +0200 | [diff] [blame] | 1065 | cfs_rq->curr = NULL; |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 1066 | } |
| 1067 | |
Peter Zijlstra | 8f4d37e | 2008-01-25 21:08:29 +0100 | [diff] [blame] | 1068 | static void |
| 1069 | entity_tick(struct cfs_rq *cfs_rq, struct sched_entity *curr, int queued) |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 1070 | { |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 1071 | /* |
Dmitry Adamushko | 30cfdcf | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 1072 | * Update run-time statistics of the 'current'. |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 1073 | */ |
Dmitry Adamushko | 30cfdcf | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 1074 | update_curr(cfs_rq); |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 1075 | |
Peter Zijlstra | 8f4d37e | 2008-01-25 21:08:29 +0100 | [diff] [blame] | 1076 | #ifdef CONFIG_SCHED_HRTICK |
| 1077 | /* |
| 1078 | * queued ticks are scheduled to match the slice, so don't bother |
| 1079 | * validating it and just reschedule. |
| 1080 | */ |
Harvey Harrison | 983ed7a | 2008-04-24 18:17:55 -0700 | [diff] [blame] | 1081 | if (queued) { |
| 1082 | resched_task(rq_of(cfs_rq)->curr); |
| 1083 | return; |
| 1084 | } |
Peter Zijlstra | 8f4d37e | 2008-01-25 21:08:29 +0100 | [diff] [blame] | 1085 | /* |
| 1086 | * don't let the period tick interfere with the hrtick preemption |
| 1087 | */ |
| 1088 | if (!sched_feat(DOUBLE_TICK) && |
| 1089 | hrtimer_active(&rq_of(cfs_rq)->hrtick_timer)) |
| 1090 | return; |
| 1091 | #endif |
| 1092 | |
Peter Zijlstra | ce6c131 | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 1093 | if (cfs_rq->nr_running > 1 || !sched_feat(WAKEUP_PREEMPT)) |
Ingo Molnar | 2e09bf5 | 2007-10-15 17:00:05 +0200 | [diff] [blame] | 1094 | check_preempt_tick(cfs_rq, curr); |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 1095 | } |
| 1096 | |
| 1097 | /************************************************** |
| 1098 | * CFS operations on tasks: |
| 1099 | */ |
| 1100 | |
Peter Zijlstra | 8f4d37e | 2008-01-25 21:08:29 +0100 | [diff] [blame] | 1101 | #ifdef CONFIG_SCHED_HRTICK |
| 1102 | static void hrtick_start_fair(struct rq *rq, struct task_struct *p) |
| 1103 | { |
Peter Zijlstra | 8f4d37e | 2008-01-25 21:08:29 +0100 | [diff] [blame] | 1104 | struct sched_entity *se = &p->se; |
| 1105 | struct cfs_rq *cfs_rq = cfs_rq_of(se); |
| 1106 | |
| 1107 | WARN_ON(task_rq(p) != rq); |
| 1108 | |
| 1109 | if (hrtick_enabled(rq) && cfs_rq->nr_running > 1) { |
| 1110 | u64 slice = sched_slice(cfs_rq, se); |
| 1111 | u64 ran = se->sum_exec_runtime - se->prev_sum_exec_runtime; |
| 1112 | s64 delta = slice - ran; |
| 1113 | |
| 1114 | if (delta < 0) { |
| 1115 | if (rq->curr == p) |
| 1116 | resched_task(p); |
| 1117 | return; |
| 1118 | } |
| 1119 | |
| 1120 | /* |
| 1121 | * Don't schedule slices shorter than 10000ns, that just |
| 1122 | * doesn't make sense. Rely on vruntime for fairness. |
| 1123 | */ |
Peter Zijlstra | 3165651 | 2008-07-18 18:01:23 +0200 | [diff] [blame] | 1124 | if (rq->curr != p) |
Peter Zijlstra | 157124c | 2008-07-28 11:53:11 +0200 | [diff] [blame] | 1125 | delta = max_t(s64, 10000LL, delta); |
Peter Zijlstra | 8f4d37e | 2008-01-25 21:08:29 +0100 | [diff] [blame] | 1126 | |
Peter Zijlstra | 3165651 | 2008-07-18 18:01:23 +0200 | [diff] [blame] | 1127 | hrtick_start(rq, delta); |
Peter Zijlstra | 8f4d37e | 2008-01-25 21:08:29 +0100 | [diff] [blame] | 1128 | } |
| 1129 | } |
Peter Zijlstra | a4c2f00 | 2008-10-17 19:27:03 +0200 | [diff] [blame] | 1130 | |
| 1131 | /* |
| 1132 | * called from enqueue/dequeue and updates the hrtick when the |
| 1133 | * current task is from our class and nr_running is low enough |
| 1134 | * to matter. |
| 1135 | */ |
| 1136 | static void hrtick_update(struct rq *rq) |
| 1137 | { |
| 1138 | struct task_struct *curr = rq->curr; |
| 1139 | |
| 1140 | if (curr->sched_class != &fair_sched_class) |
| 1141 | return; |
| 1142 | |
| 1143 | if (cfs_rq_of(&curr->se)->nr_running < sched_nr_latency) |
| 1144 | hrtick_start_fair(rq, curr); |
| 1145 | } |
Dhaval Giani | 55e12e5 | 2008-06-24 23:39:43 +0530 | [diff] [blame] | 1146 | #else /* !CONFIG_SCHED_HRTICK */ |
Peter Zijlstra | 8f4d37e | 2008-01-25 21:08:29 +0100 | [diff] [blame] | 1147 | static inline void |
| 1148 | hrtick_start_fair(struct rq *rq, struct task_struct *p) |
| 1149 | { |
| 1150 | } |
Peter Zijlstra | a4c2f00 | 2008-10-17 19:27:03 +0200 | [diff] [blame] | 1151 | |
| 1152 | static inline void hrtick_update(struct rq *rq) |
| 1153 | { |
| 1154 | } |
Peter Zijlstra | 8f4d37e | 2008-01-25 21:08:29 +0100 | [diff] [blame] | 1155 | #endif |
| 1156 | |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 1157 | /* |
| 1158 | * The enqueue_task method is called before nr_running is |
| 1159 | * increased. Here we update the fair scheduling stats and |
| 1160 | * then put the task into the rbtree: |
| 1161 | */ |
Thomas Gleixner | ea87bb7 | 2010-01-20 20:58:57 +0000 | [diff] [blame] | 1162 | static void |
Peter Zijlstra | 371fd7e | 2010-03-24 16:38:48 +0100 | [diff] [blame] | 1163 | enqueue_task_fair(struct rq *rq, struct task_struct *p, int flags) |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 1164 | { |
| 1165 | struct cfs_rq *cfs_rq; |
Peter Zijlstra | 62fb185 | 2008-02-25 17:34:02 +0100 | [diff] [blame] | 1166 | struct sched_entity *se = &p->se; |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 1167 | |
| 1168 | for_each_sched_entity(se) { |
Peter Zijlstra | 62fb185 | 2008-02-25 17:34:02 +0100 | [diff] [blame] | 1169 | if (se->on_rq) |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 1170 | break; |
| 1171 | cfs_rq = cfs_rq_of(se); |
Peter Zijlstra | 88ec22d | 2009-12-16 18:04:41 +0100 | [diff] [blame] | 1172 | enqueue_entity(cfs_rq, se, flags); |
| 1173 | flags = ENQUEUE_WAKEUP; |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 1174 | } |
Peter Zijlstra | 8f4d37e | 2008-01-25 21:08:29 +0100 | [diff] [blame] | 1175 | |
Peter Zijlstra | 2069dd7 | 2010-11-15 15:47:00 -0800 | [diff] [blame] | 1176 | for_each_sched_entity(se) { |
| 1177 | struct cfs_rq *cfs_rq = cfs_rq_of(se); |
| 1178 | |
Peter Zijlstra | 3d4b47b | 2010-11-15 15:47:01 -0800 | [diff] [blame^] | 1179 | update_cfs_load(cfs_rq, 0); |
Peter Zijlstra | 2069dd7 | 2010-11-15 15:47:00 -0800 | [diff] [blame] | 1180 | update_cfs_shares(cfs_rq); |
| 1181 | } |
| 1182 | |
Peter Zijlstra | a4c2f00 | 2008-10-17 19:27:03 +0200 | [diff] [blame] | 1183 | hrtick_update(rq); |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 1184 | } |
| 1185 | |
| 1186 | /* |
| 1187 | * The dequeue_task method is called before nr_running is |
| 1188 | * decreased. We remove the task from the rbtree and |
| 1189 | * update the fair scheduling stats: |
| 1190 | */ |
Peter Zijlstra | 371fd7e | 2010-03-24 16:38:48 +0100 | [diff] [blame] | 1191 | static void dequeue_task_fair(struct rq *rq, struct task_struct *p, int flags) |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 1192 | { |
| 1193 | struct cfs_rq *cfs_rq; |
Peter Zijlstra | 62fb185 | 2008-02-25 17:34:02 +0100 | [diff] [blame] | 1194 | struct sched_entity *se = &p->se; |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 1195 | |
| 1196 | for_each_sched_entity(se) { |
| 1197 | cfs_rq = cfs_rq_of(se); |
Peter Zijlstra | 371fd7e | 2010-03-24 16:38:48 +0100 | [diff] [blame] | 1198 | dequeue_entity(cfs_rq, se, flags); |
Peter Zijlstra | 2069dd7 | 2010-11-15 15:47:00 -0800 | [diff] [blame] | 1199 | |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 1200 | /* Don't dequeue parent if it has other entities besides us */ |
Peter Zijlstra | 62fb185 | 2008-02-25 17:34:02 +0100 | [diff] [blame] | 1201 | if (cfs_rq->load.weight) |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 1202 | break; |
Peter Zijlstra | 371fd7e | 2010-03-24 16:38:48 +0100 | [diff] [blame] | 1203 | flags |= DEQUEUE_SLEEP; |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 1204 | } |
Peter Zijlstra | 8f4d37e | 2008-01-25 21:08:29 +0100 | [diff] [blame] | 1205 | |
Peter Zijlstra | 2069dd7 | 2010-11-15 15:47:00 -0800 | [diff] [blame] | 1206 | for_each_sched_entity(se) { |
| 1207 | struct cfs_rq *cfs_rq = cfs_rq_of(se); |
| 1208 | |
Peter Zijlstra | 3d4b47b | 2010-11-15 15:47:01 -0800 | [diff] [blame^] | 1209 | update_cfs_load(cfs_rq, 0); |
Peter Zijlstra | 2069dd7 | 2010-11-15 15:47:00 -0800 | [diff] [blame] | 1210 | update_cfs_shares(cfs_rq); |
| 1211 | } |
| 1212 | |
Peter Zijlstra | a4c2f00 | 2008-10-17 19:27:03 +0200 | [diff] [blame] | 1213 | hrtick_update(rq); |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 1214 | } |
| 1215 | |
| 1216 | /* |
Ingo Molnar | 1799e35 | 2007-09-19 23:34:46 +0200 | [diff] [blame] | 1217 | * sched_yield() support is very simple - we dequeue and enqueue. |
| 1218 | * |
| 1219 | * 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] | 1220 | */ |
Dmitry Adamushko | 4530d7a | 2007-10-15 17:00:08 +0200 | [diff] [blame] | 1221 | static void yield_task_fair(struct rq *rq) |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 1222 | { |
Ingo Molnar | db292ca | 2007-12-04 17:04:39 +0100 | [diff] [blame] | 1223 | struct task_struct *curr = rq->curr; |
| 1224 | struct cfs_rq *cfs_rq = task_cfs_rq(curr); |
| 1225 | struct sched_entity *rightmost, *se = &curr->se; |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 1226 | |
| 1227 | /* |
Ingo Molnar | 1799e35 | 2007-09-19 23:34:46 +0200 | [diff] [blame] | 1228 | * Are we the only task in the tree? |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 1229 | */ |
Ingo Molnar | 1799e35 | 2007-09-19 23:34:46 +0200 | [diff] [blame] | 1230 | if (unlikely(cfs_rq->nr_running == 1)) |
| 1231 | return; |
| 1232 | |
Peter Zijlstra | 2002c69 | 2008-11-11 11:52:33 +0100 | [diff] [blame] | 1233 | clear_buddies(cfs_rq, se); |
| 1234 | |
Ingo Molnar | db292ca | 2007-12-04 17:04:39 +0100 | [diff] [blame] | 1235 | if (likely(!sysctl_sched_compat_yield) && curr->policy != SCHED_BATCH) { |
Peter Zijlstra | 3e51f33 | 2008-05-03 18:29:28 +0200 | [diff] [blame] | 1236 | update_rq_clock(rq); |
Ingo Molnar | 1799e35 | 2007-09-19 23:34:46 +0200 | [diff] [blame] | 1237 | /* |
Dmitry Adamushko | a2a2d68 | 2007-10-15 17:00:13 +0200 | [diff] [blame] | 1238 | * Update run-time statistics of the 'current'. |
Ingo Molnar | 1799e35 | 2007-09-19 23:34:46 +0200 | [diff] [blame] | 1239 | */ |
Dmitry Adamushko | 2b1e315 | 2007-10-15 17:00:12 +0200 | [diff] [blame] | 1240 | update_curr(cfs_rq); |
Ingo Molnar | 1799e35 | 2007-09-19 23:34:46 +0200 | [diff] [blame] | 1241 | |
| 1242 | return; |
| 1243 | } |
| 1244 | /* |
| 1245 | * Find the rightmost entry in the rbtree: |
| 1246 | */ |
Dmitry Adamushko | 2b1e315 | 2007-10-15 17:00:12 +0200 | [diff] [blame] | 1247 | rightmost = __pick_last_entity(cfs_rq); |
Ingo Molnar | 1799e35 | 2007-09-19 23:34:46 +0200 | [diff] [blame] | 1248 | /* |
| 1249 | * Already in the rightmost position? |
| 1250 | */ |
Fabio Checconi | 54fdc58 | 2009-07-16 12:32:27 +0200 | [diff] [blame] | 1251 | if (unlikely(!rightmost || entity_before(rightmost, se))) |
Ingo Molnar | 1799e35 | 2007-09-19 23:34:46 +0200 | [diff] [blame] | 1252 | return; |
| 1253 | |
| 1254 | /* |
| 1255 | * Minimally necessary key value to be last in the tree: |
Dmitry Adamushko | 2b1e315 | 2007-10-15 17:00:12 +0200 | [diff] [blame] | 1256 | * Upon rescheduling, sched_class::put_prev_task() will place |
| 1257 | * 'current' within the tree based on its new key value. |
Ingo Molnar | 1799e35 | 2007-09-19 23:34:46 +0200 | [diff] [blame] | 1258 | */ |
Dmitry Adamushko | 30cfdcf | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 1259 | se->vruntime = rightmost->vruntime + 1; |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 1260 | } |
| 1261 | |
Gregory Haskins | e7693a3 | 2008-01-25 21:08:09 +0100 | [diff] [blame] | 1262 | #ifdef CONFIG_SMP |
Ingo Molnar | 098fb9d | 2008-03-16 20:36:10 +0100 | [diff] [blame] | 1263 | |
Peter Zijlstra | 88ec22d | 2009-12-16 18:04:41 +0100 | [diff] [blame] | 1264 | static void task_waking_fair(struct rq *rq, struct task_struct *p) |
| 1265 | { |
| 1266 | struct sched_entity *se = &p->se; |
| 1267 | struct cfs_rq *cfs_rq = cfs_rq_of(se); |
| 1268 | |
| 1269 | se->vruntime -= cfs_rq->min_vruntime; |
| 1270 | } |
| 1271 | |
Peter Zijlstra | bb3469a | 2008-06-27 13:41:27 +0200 | [diff] [blame] | 1272 | #ifdef CONFIG_FAIR_GROUP_SCHED |
Peter Zijlstra | f5bfb7d | 2008-06-27 13:41:39 +0200 | [diff] [blame] | 1273 | /* |
| 1274 | * effective_load() calculates the load change as seen from the root_task_group |
| 1275 | * |
| 1276 | * Adding load to a group doesn't make a group heavier, but can cause movement |
| 1277 | * of group shares between cpus. Assuming the shares were perfectly aligned one |
| 1278 | * can calculate the shift in shares. |
Peter Zijlstra | f5bfb7d | 2008-06-27 13:41:39 +0200 | [diff] [blame] | 1279 | */ |
Peter Zijlstra | 2069dd7 | 2010-11-15 15:47:00 -0800 | [diff] [blame] | 1280 | static long effective_load(struct task_group *tg, int cpu, long wl, long wg) |
Peter Zijlstra | bb3469a | 2008-06-27 13:41:27 +0200 | [diff] [blame] | 1281 | { |
Peter Zijlstra | 4be9daa | 2008-06-27 13:41:30 +0200 | [diff] [blame] | 1282 | struct sched_entity *se = tg->se[cpu]; |
Peter Zijlstra | f1d239f | 2008-06-27 13:41:38 +0200 | [diff] [blame] | 1283 | |
| 1284 | if (!tg->parent) |
| 1285 | return wl; |
| 1286 | |
Peter Zijlstra | 4be9daa | 2008-06-27 13:41:30 +0200 | [diff] [blame] | 1287 | for_each_sched_entity(se) { |
Peter Zijlstra | cb5ef42 | 2008-06-27 13:41:32 +0200 | [diff] [blame] | 1288 | long S, rw, s, a, b; |
Peter Zijlstra | bb3469a | 2008-06-27 13:41:27 +0200 | [diff] [blame] | 1289 | |
Peter Zijlstra | 4be9daa | 2008-06-27 13:41:30 +0200 | [diff] [blame] | 1290 | S = se->my_q->tg->shares; |
Peter Zijlstra | 2069dd7 | 2010-11-15 15:47:00 -0800 | [diff] [blame] | 1291 | s = se->load.weight; |
| 1292 | rw = se->my_q->load.weight; |
Peter Zijlstra | 4be9daa | 2008-06-27 13:41:30 +0200 | [diff] [blame] | 1293 | |
Peter Zijlstra | cb5ef42 | 2008-06-27 13:41:32 +0200 | [diff] [blame] | 1294 | a = S*(rw + wl); |
| 1295 | b = S*rw + s*wg; |
Peter Zijlstra | 4be9daa | 2008-06-27 13:41:30 +0200 | [diff] [blame] | 1296 | |
Peter Zijlstra | 940959e | 2008-09-23 15:33:42 +0200 | [diff] [blame] | 1297 | wl = s*(a-b); |
| 1298 | |
| 1299 | if (likely(b)) |
| 1300 | wl /= b; |
| 1301 | |
Peter Zijlstra | 8337826 | 2008-06-27 13:41:37 +0200 | [diff] [blame] | 1302 | /* |
| 1303 | * Assume the group is already running and will |
| 1304 | * thus already be accounted for in the weight. |
| 1305 | * |
| 1306 | * That is, moving shares between CPUs, does not |
| 1307 | * alter the group weight. |
| 1308 | */ |
Peter Zijlstra | 4be9daa | 2008-06-27 13:41:30 +0200 | [diff] [blame] | 1309 | wg = 0; |
Peter Zijlstra | 4be9daa | 2008-06-27 13:41:30 +0200 | [diff] [blame] | 1310 | } |
| 1311 | |
| 1312 | return wl; |
Peter Zijlstra | bb3469a | 2008-06-27 13:41:27 +0200 | [diff] [blame] | 1313 | } |
Peter Zijlstra | 4be9daa | 2008-06-27 13:41:30 +0200 | [diff] [blame] | 1314 | |
Peter Zijlstra | bb3469a | 2008-06-27 13:41:27 +0200 | [diff] [blame] | 1315 | #else |
Peter Zijlstra | 4be9daa | 2008-06-27 13:41:30 +0200 | [diff] [blame] | 1316 | |
Peter Zijlstra | 8337826 | 2008-06-27 13:41:37 +0200 | [diff] [blame] | 1317 | static inline unsigned long effective_load(struct task_group *tg, int cpu, |
| 1318 | unsigned long wl, unsigned long wg) |
Peter Zijlstra | 4be9daa | 2008-06-27 13:41:30 +0200 | [diff] [blame] | 1319 | { |
Peter Zijlstra | 8337826 | 2008-06-27 13:41:37 +0200 | [diff] [blame] | 1320 | return wl; |
Peter Zijlstra | bb3469a | 2008-06-27 13:41:27 +0200 | [diff] [blame] | 1321 | } |
Peter Zijlstra | 4be9daa | 2008-06-27 13:41:30 +0200 | [diff] [blame] | 1322 | |
Peter Zijlstra | bb3469a | 2008-06-27 13:41:27 +0200 | [diff] [blame] | 1323 | #endif |
| 1324 | |
Peter Zijlstra | c88d591 | 2009-09-10 13:50:02 +0200 | [diff] [blame] | 1325 | static int wake_affine(struct sched_domain *sd, struct task_struct *p, int sync) |
Ingo Molnar | 098fb9d | 2008-03-16 20:36:10 +0100 | [diff] [blame] | 1326 | { |
Peter Zijlstra | c88d591 | 2009-09-10 13:50:02 +0200 | [diff] [blame] | 1327 | unsigned long this_load, load; |
| 1328 | int idx, this_cpu, prev_cpu; |
Ingo Molnar | 098fb9d | 2008-03-16 20:36:10 +0100 | [diff] [blame] | 1329 | unsigned long tl_per_task; |
Peter Zijlstra | c88d591 | 2009-09-10 13:50:02 +0200 | [diff] [blame] | 1330 | struct task_group *tg; |
Peter Zijlstra | 8337826 | 2008-06-27 13:41:37 +0200 | [diff] [blame] | 1331 | unsigned long weight; |
Mike Galbraith | b3137bc | 2008-05-29 11:11:41 +0200 | [diff] [blame] | 1332 | int balanced; |
Ingo Molnar | 098fb9d | 2008-03-16 20:36:10 +0100 | [diff] [blame] | 1333 | |
Peter Zijlstra | c88d591 | 2009-09-10 13:50:02 +0200 | [diff] [blame] | 1334 | idx = sd->wake_idx; |
| 1335 | this_cpu = smp_processor_id(); |
| 1336 | prev_cpu = task_cpu(p); |
| 1337 | load = source_load(prev_cpu, idx); |
| 1338 | this_load = target_load(this_cpu, idx); |
Ingo Molnar | 098fb9d | 2008-03-16 20:36:10 +0100 | [diff] [blame] | 1339 | |
| 1340 | /* |
Ingo Molnar | 098fb9d | 2008-03-16 20:36:10 +0100 | [diff] [blame] | 1341 | * If sync wakeup then subtract the (maximum possible) |
| 1342 | * effect of the currently running task from the load |
| 1343 | * of the current CPU: |
| 1344 | */ |
Daniel J Blueman | f3b577d | 2010-06-01 14:06:13 +0100 | [diff] [blame] | 1345 | rcu_read_lock(); |
Peter Zijlstra | 8337826 | 2008-06-27 13:41:37 +0200 | [diff] [blame] | 1346 | if (sync) { |
| 1347 | tg = task_group(current); |
| 1348 | weight = current->se.load.weight; |
Ingo Molnar | 098fb9d | 2008-03-16 20:36:10 +0100 | [diff] [blame] | 1349 | |
Peter Zijlstra | c88d591 | 2009-09-10 13:50:02 +0200 | [diff] [blame] | 1350 | this_load += effective_load(tg, this_cpu, -weight, -weight); |
Peter Zijlstra | 8337826 | 2008-06-27 13:41:37 +0200 | [diff] [blame] | 1351 | load += effective_load(tg, prev_cpu, 0, -weight); |
| 1352 | } |
| 1353 | |
| 1354 | tg = task_group(p); |
| 1355 | weight = p->se.load.weight; |
| 1356 | |
Peter Zijlstra | 71a29aa | 2009-09-07 18:28:05 +0200 | [diff] [blame] | 1357 | /* |
| 1358 | * In low-load situations, where prev_cpu is idle and this_cpu is idle |
Peter Zijlstra | c88d591 | 2009-09-10 13:50:02 +0200 | [diff] [blame] | 1359 | * due to the sync cause above having dropped this_load to 0, we'll |
| 1360 | * always have an imbalance, but there's really nothing you can do |
| 1361 | * about that, so that's good too. |
Peter Zijlstra | 71a29aa | 2009-09-07 18:28:05 +0200 | [diff] [blame] | 1362 | * |
| 1363 | * Otherwise check if either cpus are near enough in load to allow this |
| 1364 | * task to be woken on this_cpu. |
| 1365 | */ |
Peter Zijlstra | e51fd5e | 2010-05-31 12:37:30 +0200 | [diff] [blame] | 1366 | if (this_load) { |
| 1367 | unsigned long this_eff_load, prev_eff_load; |
| 1368 | |
| 1369 | this_eff_load = 100; |
| 1370 | this_eff_load *= power_of(prev_cpu); |
| 1371 | this_eff_load *= this_load + |
| 1372 | effective_load(tg, this_cpu, weight, weight); |
| 1373 | |
| 1374 | prev_eff_load = 100 + (sd->imbalance_pct - 100) / 2; |
| 1375 | prev_eff_load *= power_of(this_cpu); |
| 1376 | prev_eff_load *= load + effective_load(tg, prev_cpu, 0, weight); |
| 1377 | |
| 1378 | balanced = this_eff_load <= prev_eff_load; |
| 1379 | } else |
| 1380 | balanced = true; |
Daniel J Blueman | f3b577d | 2010-06-01 14:06:13 +0100 | [diff] [blame] | 1381 | rcu_read_unlock(); |
Mike Galbraith | b3137bc | 2008-05-29 11:11:41 +0200 | [diff] [blame] | 1382 | |
| 1383 | /* |
| 1384 | * If the currently running task will sleep within |
| 1385 | * a reasonable amount of time then attract this newly |
| 1386 | * woken task: |
| 1387 | */ |
Peter Zijlstra | 2fb7635 | 2008-10-08 09:16:04 +0200 | [diff] [blame] | 1388 | if (sync && balanced) |
| 1389 | return 1; |
Mike Galbraith | b3137bc | 2008-05-29 11:11:41 +0200 | [diff] [blame] | 1390 | |
Lucas De Marchi | 41acab8 | 2010-03-10 23:37:45 -0300 | [diff] [blame] | 1391 | schedstat_inc(p, se.statistics.nr_wakeups_affine_attempts); |
Mike Galbraith | b3137bc | 2008-05-29 11:11:41 +0200 | [diff] [blame] | 1392 | tl_per_task = cpu_avg_load_per_task(this_cpu); |
| 1393 | |
Peter Zijlstra | c88d591 | 2009-09-10 13:50:02 +0200 | [diff] [blame] | 1394 | if (balanced || |
| 1395 | (this_load <= load && |
| 1396 | this_load + target_load(prev_cpu, idx) <= tl_per_task)) { |
Ingo Molnar | 098fb9d | 2008-03-16 20:36:10 +0100 | [diff] [blame] | 1397 | /* |
| 1398 | * This domain has SD_WAKE_AFFINE and |
| 1399 | * p is cache cold in this domain, and |
| 1400 | * there is no bad imbalance. |
| 1401 | */ |
Peter Zijlstra | c88d591 | 2009-09-10 13:50:02 +0200 | [diff] [blame] | 1402 | schedstat_inc(sd, ttwu_move_affine); |
Lucas De Marchi | 41acab8 | 2010-03-10 23:37:45 -0300 | [diff] [blame] | 1403 | schedstat_inc(p, se.statistics.nr_wakeups_affine); |
Ingo Molnar | 098fb9d | 2008-03-16 20:36:10 +0100 | [diff] [blame] | 1404 | |
| 1405 | return 1; |
| 1406 | } |
| 1407 | return 0; |
| 1408 | } |
| 1409 | |
Peter Zijlstra | aaee120 | 2009-09-10 13:36:25 +0200 | [diff] [blame] | 1410 | /* |
| 1411 | * find_idlest_group finds and returns the least busy CPU group within the |
| 1412 | * domain. |
| 1413 | */ |
| 1414 | static struct sched_group * |
Peter Zijlstra | 78e7ed5 | 2009-09-03 13:16:51 +0200 | [diff] [blame] | 1415 | find_idlest_group(struct sched_domain *sd, struct task_struct *p, |
Peter Zijlstra | 5158f4e | 2009-09-16 13:46:59 +0200 | [diff] [blame] | 1416 | int this_cpu, int load_idx) |
Gregory Haskins | e7693a3 | 2008-01-25 21:08:09 +0100 | [diff] [blame] | 1417 | { |
Andi Kleen | b3bd3de | 2010-08-10 14:17:51 -0700 | [diff] [blame] | 1418 | struct sched_group *idlest = NULL, *group = sd->groups; |
Peter Zijlstra | aaee120 | 2009-09-10 13:36:25 +0200 | [diff] [blame] | 1419 | unsigned long min_load = ULONG_MAX, this_load = 0; |
Peter Zijlstra | aaee120 | 2009-09-10 13:36:25 +0200 | [diff] [blame] | 1420 | int imbalance = 100 + (sd->imbalance_pct-100)/2; |
Gregory Haskins | e7693a3 | 2008-01-25 21:08:09 +0100 | [diff] [blame] | 1421 | |
Peter Zijlstra | aaee120 | 2009-09-10 13:36:25 +0200 | [diff] [blame] | 1422 | do { |
| 1423 | unsigned long load, avg_load; |
| 1424 | int local_group; |
| 1425 | int i; |
Gregory Haskins | e7693a3 | 2008-01-25 21:08:09 +0100 | [diff] [blame] | 1426 | |
Peter Zijlstra | aaee120 | 2009-09-10 13:36:25 +0200 | [diff] [blame] | 1427 | /* Skip over this group if it has no CPUs allowed */ |
| 1428 | if (!cpumask_intersects(sched_group_cpus(group), |
| 1429 | &p->cpus_allowed)) |
| 1430 | continue; |
| 1431 | |
| 1432 | local_group = cpumask_test_cpu(this_cpu, |
| 1433 | sched_group_cpus(group)); |
| 1434 | |
| 1435 | /* Tally up the load of all CPUs in the group */ |
| 1436 | avg_load = 0; |
| 1437 | |
| 1438 | for_each_cpu(i, sched_group_cpus(group)) { |
| 1439 | /* Bias balancing toward cpus of our domain */ |
| 1440 | if (local_group) |
| 1441 | load = source_load(i, load_idx); |
| 1442 | else |
| 1443 | load = target_load(i, load_idx); |
| 1444 | |
| 1445 | avg_load += load; |
| 1446 | } |
| 1447 | |
| 1448 | /* Adjust by relative CPU power of the group */ |
| 1449 | avg_load = (avg_load * SCHED_LOAD_SCALE) / group->cpu_power; |
| 1450 | |
| 1451 | if (local_group) { |
| 1452 | this_load = avg_load; |
Peter Zijlstra | aaee120 | 2009-09-10 13:36:25 +0200 | [diff] [blame] | 1453 | } else if (avg_load < min_load) { |
| 1454 | min_load = avg_load; |
| 1455 | idlest = group; |
| 1456 | } |
| 1457 | } while (group = group->next, group != sd->groups); |
| 1458 | |
| 1459 | if (!idlest || 100*this_load < imbalance*min_load) |
| 1460 | return NULL; |
| 1461 | return idlest; |
| 1462 | } |
| 1463 | |
| 1464 | /* |
| 1465 | * find_idlest_cpu - find the idlest cpu among the cpus in group. |
| 1466 | */ |
| 1467 | static int |
| 1468 | find_idlest_cpu(struct sched_group *group, struct task_struct *p, int this_cpu) |
| 1469 | { |
| 1470 | unsigned long load, min_load = ULONG_MAX; |
| 1471 | int idlest = -1; |
| 1472 | int i; |
| 1473 | |
| 1474 | /* Traverse only the allowed CPUs */ |
| 1475 | for_each_cpu_and(i, sched_group_cpus(group), &p->cpus_allowed) { |
| 1476 | load = weighted_cpuload(i); |
| 1477 | |
| 1478 | if (load < min_load || (load == min_load && i == this_cpu)) { |
| 1479 | min_load = load; |
| 1480 | idlest = i; |
Gregory Haskins | e7693a3 | 2008-01-25 21:08:09 +0100 | [diff] [blame] | 1481 | } |
| 1482 | } |
| 1483 | |
Peter Zijlstra | aaee120 | 2009-09-10 13:36:25 +0200 | [diff] [blame] | 1484 | return idlest; |
| 1485 | } |
Gregory Haskins | e7693a3 | 2008-01-25 21:08:09 +0100 | [diff] [blame] | 1486 | |
Peter Zijlstra | aaee120 | 2009-09-10 13:36:25 +0200 | [diff] [blame] | 1487 | /* |
Peter Zijlstra | a50bde5 | 2009-11-12 15:55:28 +0100 | [diff] [blame] | 1488 | * Try and locate an idle CPU in the sched_domain. |
| 1489 | */ |
Suresh Siddha | 99bd5e2 | 2010-03-31 16:47:45 -0700 | [diff] [blame] | 1490 | static int select_idle_sibling(struct task_struct *p, int target) |
Peter Zijlstra | a50bde5 | 2009-11-12 15:55:28 +0100 | [diff] [blame] | 1491 | { |
| 1492 | int cpu = smp_processor_id(); |
| 1493 | int prev_cpu = task_cpu(p); |
Suresh Siddha | 99bd5e2 | 2010-03-31 16:47:45 -0700 | [diff] [blame] | 1494 | struct sched_domain *sd; |
Peter Zijlstra | a50bde5 | 2009-11-12 15:55:28 +0100 | [diff] [blame] | 1495 | int i; |
| 1496 | |
| 1497 | /* |
Suresh Siddha | 99bd5e2 | 2010-03-31 16:47:45 -0700 | [diff] [blame] | 1498 | * If the task is going to be woken-up on this cpu and if it is |
| 1499 | * already idle, then it is the right target. |
Peter Zijlstra | a50bde5 | 2009-11-12 15:55:28 +0100 | [diff] [blame] | 1500 | */ |
Suresh Siddha | 99bd5e2 | 2010-03-31 16:47:45 -0700 | [diff] [blame] | 1501 | if (target == cpu && idle_cpu(cpu)) |
| 1502 | return cpu; |
| 1503 | |
| 1504 | /* |
| 1505 | * If the task is going to be woken-up on the cpu where it previously |
| 1506 | * ran and if it is currently idle, then it the right target. |
| 1507 | */ |
| 1508 | if (target == prev_cpu && idle_cpu(prev_cpu)) |
Peter Zijlstra | fe3bcfe | 2009-11-12 15:55:29 +0100 | [diff] [blame] | 1509 | return prev_cpu; |
Peter Zijlstra | a50bde5 | 2009-11-12 15:55:28 +0100 | [diff] [blame] | 1510 | |
| 1511 | /* |
Suresh Siddha | 99bd5e2 | 2010-03-31 16:47:45 -0700 | [diff] [blame] | 1512 | * Otherwise, iterate the domains and find an elegible idle cpu. |
Peter Zijlstra | a50bde5 | 2009-11-12 15:55:28 +0100 | [diff] [blame] | 1513 | */ |
Suresh Siddha | 99bd5e2 | 2010-03-31 16:47:45 -0700 | [diff] [blame] | 1514 | for_each_domain(target, sd) { |
| 1515 | if (!(sd->flags & SD_SHARE_PKG_RESOURCES)) |
Peter Zijlstra | fe3bcfe | 2009-11-12 15:55:29 +0100 | [diff] [blame] | 1516 | break; |
Suresh Siddha | 99bd5e2 | 2010-03-31 16:47:45 -0700 | [diff] [blame] | 1517 | |
| 1518 | for_each_cpu_and(i, sched_domain_span(sd), &p->cpus_allowed) { |
| 1519 | if (idle_cpu(i)) { |
| 1520 | target = i; |
| 1521 | break; |
| 1522 | } |
Peter Zijlstra | a50bde5 | 2009-11-12 15:55:28 +0100 | [diff] [blame] | 1523 | } |
Suresh Siddha | 99bd5e2 | 2010-03-31 16:47:45 -0700 | [diff] [blame] | 1524 | |
| 1525 | /* |
| 1526 | * Lets stop looking for an idle sibling when we reached |
| 1527 | * the domain that spans the current cpu and prev_cpu. |
| 1528 | */ |
| 1529 | if (cpumask_test_cpu(cpu, sched_domain_span(sd)) && |
| 1530 | cpumask_test_cpu(prev_cpu, sched_domain_span(sd))) |
| 1531 | break; |
Peter Zijlstra | a50bde5 | 2009-11-12 15:55:28 +0100 | [diff] [blame] | 1532 | } |
| 1533 | |
| 1534 | return target; |
| 1535 | } |
| 1536 | |
| 1537 | /* |
Peter Zijlstra | aaee120 | 2009-09-10 13:36:25 +0200 | [diff] [blame] | 1538 | * sched_balance_self: balance the current task (running on cpu) in domains |
| 1539 | * that have the 'flag' flag set. In practice, this is SD_BALANCE_FORK and |
| 1540 | * SD_BALANCE_EXEC. |
| 1541 | * |
| 1542 | * Balance, ie. select the least loaded group. |
| 1543 | * |
| 1544 | * Returns the target CPU number, or the same CPU if no balancing is needed. |
| 1545 | * |
| 1546 | * preempt must be disabled. |
| 1547 | */ |
Peter Zijlstra | 0017d73 | 2010-03-24 18:34:10 +0100 | [diff] [blame] | 1548 | static int |
| 1549 | select_task_rq_fair(struct rq *rq, struct task_struct *p, int sd_flag, int wake_flags) |
Peter Zijlstra | aaee120 | 2009-09-10 13:36:25 +0200 | [diff] [blame] | 1550 | { |
Peter Zijlstra | 29cd8ba | 2009-09-17 09:01:14 +0200 | [diff] [blame] | 1551 | struct sched_domain *tmp, *affine_sd = NULL, *sd = NULL; |
Peter Zijlstra | c88d591 | 2009-09-10 13:50:02 +0200 | [diff] [blame] | 1552 | int cpu = smp_processor_id(); |
| 1553 | int prev_cpu = task_cpu(p); |
| 1554 | int new_cpu = cpu; |
Suresh Siddha | 99bd5e2 | 2010-03-31 16:47:45 -0700 | [diff] [blame] | 1555 | int want_affine = 0; |
Peter Zijlstra | 29cd8ba | 2009-09-17 09:01:14 +0200 | [diff] [blame] | 1556 | int want_sd = 1; |
Peter Zijlstra | 5158f4e | 2009-09-16 13:46:59 +0200 | [diff] [blame] | 1557 | int sync = wake_flags & WF_SYNC; |
Gregory Haskins | e7693a3 | 2008-01-25 21:08:09 +0100 | [diff] [blame] | 1558 | |
Peter Zijlstra | 0763a66 | 2009-09-14 19:37:39 +0200 | [diff] [blame] | 1559 | if (sd_flag & SD_BALANCE_WAKE) { |
Mike Galbraith | beac4c7 | 2010-03-11 17:17:20 +0100 | [diff] [blame] | 1560 | if (cpumask_test_cpu(cpu, &p->cpus_allowed)) |
Peter Zijlstra | c88d591 | 2009-09-10 13:50:02 +0200 | [diff] [blame] | 1561 | want_affine = 1; |
| 1562 | new_cpu = prev_cpu; |
| 1563 | } |
Gregory Haskins | e7693a3 | 2008-01-25 21:08:09 +0100 | [diff] [blame] | 1564 | |
Peter Zijlstra | aaee120 | 2009-09-10 13:36:25 +0200 | [diff] [blame] | 1565 | for_each_domain(cpu, tmp) { |
Peter Zijlstra | e4f42888 | 2009-12-16 18:04:34 +0100 | [diff] [blame] | 1566 | if (!(tmp->flags & SD_LOAD_BALANCE)) |
| 1567 | continue; |
| 1568 | |
Peter Zijlstra | aaee120 | 2009-09-10 13:36:25 +0200 | [diff] [blame] | 1569 | /* |
Peter Zijlstra | ae154be | 2009-09-10 14:40:57 +0200 | [diff] [blame] | 1570 | * If power savings logic is enabled for a domain, see if we |
| 1571 | * are not overloaded, if so, don't balance wider. |
Peter Zijlstra | aaee120 | 2009-09-10 13:36:25 +0200 | [diff] [blame] | 1572 | */ |
Peter Zijlstra | 59abf02 | 2009-09-16 08:28:30 +0200 | [diff] [blame] | 1573 | if (tmp->flags & (SD_POWERSAVINGS_BALANCE|SD_PREFER_LOCAL)) { |
Peter Zijlstra | ae154be | 2009-09-10 14:40:57 +0200 | [diff] [blame] | 1574 | unsigned long power = 0; |
| 1575 | unsigned long nr_running = 0; |
| 1576 | unsigned long capacity; |
| 1577 | int i; |
Gregory Haskins | e7693a3 | 2008-01-25 21:08:09 +0100 | [diff] [blame] | 1578 | |
Peter Zijlstra | ae154be | 2009-09-10 14:40:57 +0200 | [diff] [blame] | 1579 | for_each_cpu(i, sched_domain_span(tmp)) { |
| 1580 | power += power_of(i); |
| 1581 | nr_running += cpu_rq(i)->cfs.nr_running; |
| 1582 | } |
Gregory Haskins | e7693a3 | 2008-01-25 21:08:09 +0100 | [diff] [blame] | 1583 | |
Peter Zijlstra | ae154be | 2009-09-10 14:40:57 +0200 | [diff] [blame] | 1584 | capacity = DIV_ROUND_CLOSEST(power, SCHED_LOAD_SCALE); |
Ingo Molnar | 4ae7d5c | 2008-03-19 01:42:00 +0100 | [diff] [blame] | 1585 | |
Peter Zijlstra | 59abf02 | 2009-09-16 08:28:30 +0200 | [diff] [blame] | 1586 | if (tmp->flags & SD_POWERSAVINGS_BALANCE) |
| 1587 | nr_running /= 2; |
| 1588 | |
| 1589 | if (nr_running < capacity) |
Peter Zijlstra | 29cd8ba | 2009-09-17 09:01:14 +0200 | [diff] [blame] | 1590 | want_sd = 0; |
Gregory Haskins | e7693a3 | 2008-01-25 21:08:09 +0100 | [diff] [blame] | 1591 | } |
Peter Zijlstra | aaee120 | 2009-09-10 13:36:25 +0200 | [diff] [blame] | 1592 | |
Peter Zijlstra | fe3bcfe | 2009-11-12 15:55:29 +0100 | [diff] [blame] | 1593 | /* |
Suresh Siddha | 99bd5e2 | 2010-03-31 16:47:45 -0700 | [diff] [blame] | 1594 | * If both cpu and prev_cpu are part of this domain, |
| 1595 | * cpu is a valid SD_WAKE_AFFINE target. |
Peter Zijlstra | fe3bcfe | 2009-11-12 15:55:29 +0100 | [diff] [blame] | 1596 | */ |
Suresh Siddha | 99bd5e2 | 2010-03-31 16:47:45 -0700 | [diff] [blame] | 1597 | if (want_affine && (tmp->flags & SD_WAKE_AFFINE) && |
| 1598 | cpumask_test_cpu(prev_cpu, sched_domain_span(tmp))) { |
| 1599 | affine_sd = tmp; |
| 1600 | want_affine = 0; |
Peter Zijlstra | c88d591 | 2009-09-10 13:50:02 +0200 | [diff] [blame] | 1601 | } |
| 1602 | |
Peter Zijlstra | 29cd8ba | 2009-09-17 09:01:14 +0200 | [diff] [blame] | 1603 | if (!want_sd && !want_affine) |
| 1604 | break; |
| 1605 | |
Peter Zijlstra | 0763a66 | 2009-09-14 19:37:39 +0200 | [diff] [blame] | 1606 | if (!(tmp->flags & sd_flag)) |
Peter Zijlstra | c88d591 | 2009-09-10 13:50:02 +0200 | [diff] [blame] | 1607 | continue; |
| 1608 | |
Peter Zijlstra | 29cd8ba | 2009-09-17 09:01:14 +0200 | [diff] [blame] | 1609 | if (want_sd) |
| 1610 | sd = tmp; |
Peter Zijlstra | c88d591 | 2009-09-10 13:50:02 +0200 | [diff] [blame] | 1611 | } |
Peter Zijlstra | aaee120 | 2009-09-10 13:36:25 +0200 | [diff] [blame] | 1612 | |
Mike Galbraith | 8b911ac | 2010-03-11 17:17:16 +0100 | [diff] [blame] | 1613 | if (affine_sd) { |
Suresh Siddha | 99bd5e2 | 2010-03-31 16:47:45 -0700 | [diff] [blame] | 1614 | if (cpu == prev_cpu || wake_affine(affine_sd, p, sync)) |
| 1615 | return select_idle_sibling(p, cpu); |
| 1616 | else |
| 1617 | return select_idle_sibling(p, prev_cpu); |
Mike Galbraith | 8b911ac | 2010-03-11 17:17:16 +0100 | [diff] [blame] | 1618 | } |
Peter Zijlstra | 3b64089 | 2009-09-16 13:44:33 +0200 | [diff] [blame] | 1619 | |
Peter Zijlstra | aaee120 | 2009-09-10 13:36:25 +0200 | [diff] [blame] | 1620 | while (sd) { |
Peter Zijlstra | 5158f4e | 2009-09-16 13:46:59 +0200 | [diff] [blame] | 1621 | int load_idx = sd->forkexec_idx; |
Peter Zijlstra | aaee120 | 2009-09-10 13:36:25 +0200 | [diff] [blame] | 1622 | struct sched_group *group; |
Peter Zijlstra | c88d591 | 2009-09-10 13:50:02 +0200 | [diff] [blame] | 1623 | int weight; |
Peter Zijlstra | aaee120 | 2009-09-10 13:36:25 +0200 | [diff] [blame] | 1624 | |
Peter Zijlstra | 0763a66 | 2009-09-14 19:37:39 +0200 | [diff] [blame] | 1625 | if (!(sd->flags & sd_flag)) { |
Peter Zijlstra | aaee120 | 2009-09-10 13:36:25 +0200 | [diff] [blame] | 1626 | sd = sd->child; |
| 1627 | continue; |
| 1628 | } |
| 1629 | |
Peter Zijlstra | 5158f4e | 2009-09-16 13:46:59 +0200 | [diff] [blame] | 1630 | if (sd_flag & SD_BALANCE_WAKE) |
| 1631 | load_idx = sd->wake_idx; |
| 1632 | |
| 1633 | group = find_idlest_group(sd, p, cpu, load_idx); |
Peter Zijlstra | aaee120 | 2009-09-10 13:36:25 +0200 | [diff] [blame] | 1634 | if (!group) { |
| 1635 | sd = sd->child; |
| 1636 | continue; |
| 1637 | } |
| 1638 | |
Peter Zijlstra | d7c33c4 | 2009-09-11 12:45:38 +0200 | [diff] [blame] | 1639 | new_cpu = find_idlest_cpu(group, p, cpu); |
Peter Zijlstra | aaee120 | 2009-09-10 13:36:25 +0200 | [diff] [blame] | 1640 | if (new_cpu == -1 || new_cpu == cpu) { |
| 1641 | /* Now try balancing at a lower domain level of cpu */ |
| 1642 | sd = sd->child; |
| 1643 | continue; |
| 1644 | } |
| 1645 | |
| 1646 | /* Now try balancing at a lower domain level of new_cpu */ |
| 1647 | cpu = new_cpu; |
Peter Zijlstra | 669c55e | 2010-04-16 14:59:29 +0200 | [diff] [blame] | 1648 | weight = sd->span_weight; |
Peter Zijlstra | aaee120 | 2009-09-10 13:36:25 +0200 | [diff] [blame] | 1649 | sd = NULL; |
| 1650 | for_each_domain(cpu, tmp) { |
Peter Zijlstra | 669c55e | 2010-04-16 14:59:29 +0200 | [diff] [blame] | 1651 | if (weight <= tmp->span_weight) |
Peter Zijlstra | aaee120 | 2009-09-10 13:36:25 +0200 | [diff] [blame] | 1652 | break; |
Peter Zijlstra | 0763a66 | 2009-09-14 19:37:39 +0200 | [diff] [blame] | 1653 | if (tmp->flags & sd_flag) |
Peter Zijlstra | aaee120 | 2009-09-10 13:36:25 +0200 | [diff] [blame] | 1654 | sd = tmp; |
| 1655 | } |
| 1656 | /* while loop will break here if sd == NULL */ |
Gregory Haskins | e7693a3 | 2008-01-25 21:08:09 +0100 | [diff] [blame] | 1657 | } |
| 1658 | |
Peter Zijlstra | c88d591 | 2009-09-10 13:50:02 +0200 | [diff] [blame] | 1659 | return new_cpu; |
Gregory Haskins | e7693a3 | 2008-01-25 21:08:09 +0100 | [diff] [blame] | 1660 | } |
| 1661 | #endif /* CONFIG_SMP */ |
| 1662 | |
Peter Zijlstra | e52fb7c | 2009-01-14 12:39:19 +0100 | [diff] [blame] | 1663 | static unsigned long |
| 1664 | wakeup_gran(struct sched_entity *curr, struct sched_entity *se) |
Peter Zijlstra | 0bbd333 | 2008-04-19 19:44:57 +0200 | [diff] [blame] | 1665 | { |
| 1666 | unsigned long gran = sysctl_sched_wakeup_granularity; |
| 1667 | |
| 1668 | /* |
Peter Zijlstra | e52fb7c | 2009-01-14 12:39:19 +0100 | [diff] [blame] | 1669 | * Since its curr running now, convert the gran from real-time |
| 1670 | * to virtual-time in his units. |
Mike Galbraith | 13814d4 | 2010-03-11 17:17:04 +0100 | [diff] [blame] | 1671 | * |
| 1672 | * By using 'se' instead of 'curr' we penalize light tasks, so |
| 1673 | * they get preempted easier. That is, if 'se' < 'curr' then |
| 1674 | * the resulting gran will be larger, therefore penalizing the |
| 1675 | * lighter, if otoh 'se' > 'curr' then the resulting gran will |
| 1676 | * be smaller, again penalizing the lighter task. |
| 1677 | * |
| 1678 | * This is especially important for buddies when the leftmost |
| 1679 | * task is higher priority than the buddy. |
Peter Zijlstra | 0bbd333 | 2008-04-19 19:44:57 +0200 | [diff] [blame] | 1680 | */ |
Mike Galbraith | 13814d4 | 2010-03-11 17:17:04 +0100 | [diff] [blame] | 1681 | if (unlikely(se->load.weight != NICE_0_LOAD)) |
| 1682 | gran = calc_delta_fair(gran, se); |
Peter Zijlstra | 0bbd333 | 2008-04-19 19:44:57 +0200 | [diff] [blame] | 1683 | |
| 1684 | return gran; |
| 1685 | } |
| 1686 | |
| 1687 | /* |
Peter Zijlstra | 464b752 | 2008-10-24 11:06:15 +0200 | [diff] [blame] | 1688 | * Should 'se' preempt 'curr'. |
| 1689 | * |
| 1690 | * |s1 |
| 1691 | * |s2 |
| 1692 | * |s3 |
| 1693 | * g |
| 1694 | * |<--->|c |
| 1695 | * |
| 1696 | * w(c, s1) = -1 |
| 1697 | * w(c, s2) = 0 |
| 1698 | * w(c, s3) = 1 |
| 1699 | * |
| 1700 | */ |
| 1701 | static int |
| 1702 | wakeup_preempt_entity(struct sched_entity *curr, struct sched_entity *se) |
| 1703 | { |
| 1704 | s64 gran, vdiff = curr->vruntime - se->vruntime; |
| 1705 | |
| 1706 | if (vdiff <= 0) |
| 1707 | return -1; |
| 1708 | |
Peter Zijlstra | e52fb7c | 2009-01-14 12:39:19 +0100 | [diff] [blame] | 1709 | gran = wakeup_gran(curr, se); |
Peter Zijlstra | 464b752 | 2008-10-24 11:06:15 +0200 | [diff] [blame] | 1710 | if (vdiff > gran) |
| 1711 | return 1; |
| 1712 | |
| 1713 | return 0; |
| 1714 | } |
| 1715 | |
Peter Zijlstra | 0247909 | 2008-11-04 21:25:10 +0100 | [diff] [blame] | 1716 | static void set_last_buddy(struct sched_entity *se) |
| 1717 | { |
Peter Zijlstra | 6bc912b | 2009-01-15 14:53:38 +0100 | [diff] [blame] | 1718 | if (likely(task_of(se)->policy != SCHED_IDLE)) { |
| 1719 | for_each_sched_entity(se) |
| 1720 | cfs_rq_of(se)->last = se; |
| 1721 | } |
Peter Zijlstra | 0247909 | 2008-11-04 21:25:10 +0100 | [diff] [blame] | 1722 | } |
| 1723 | |
| 1724 | static void set_next_buddy(struct sched_entity *se) |
| 1725 | { |
Peter Zijlstra | 6bc912b | 2009-01-15 14:53:38 +0100 | [diff] [blame] | 1726 | if (likely(task_of(se)->policy != SCHED_IDLE)) { |
| 1727 | for_each_sched_entity(se) |
| 1728 | cfs_rq_of(se)->next = se; |
| 1729 | } |
Peter Zijlstra | 0247909 | 2008-11-04 21:25:10 +0100 | [diff] [blame] | 1730 | } |
| 1731 | |
Peter Zijlstra | 464b752 | 2008-10-24 11:06:15 +0200 | [diff] [blame] | 1732 | /* |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 1733 | * Preempt the current task with a newly woken task if needed: |
| 1734 | */ |
Peter Zijlstra | 5a9b86f | 2009-09-16 13:47:58 +0200 | [diff] [blame] | 1735 | static void check_preempt_wakeup(struct rq *rq, struct task_struct *p, int wake_flags) |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 1736 | { |
| 1737 | struct task_struct *curr = rq->curr; |
Srivatsa Vaddagiri | 8651a86 | 2007-10-15 17:00:12 +0200 | [diff] [blame] | 1738 | struct sched_entity *se = &curr->se, *pse = &p->se; |
Mike Galbraith | 03e89e4 | 2008-12-16 08:45:30 +0100 | [diff] [blame] | 1739 | struct cfs_rq *cfs_rq = task_cfs_rq(curr); |
Mike Galbraith | f685cea | 2009-10-23 23:09:22 +0200 | [diff] [blame] | 1740 | int scale = cfs_rq->nr_running >= sched_nr_latency; |
Mike Galbraith | 03e89e4 | 2008-12-16 08:45:30 +0100 | [diff] [blame] | 1741 | |
Peter Zijlstra | 3a7e73a | 2009-11-28 18:51:02 +0100 | [diff] [blame] | 1742 | if (unlikely(rt_prio(p->prio))) |
| 1743 | goto preempt; |
Peter Zijlstra | aa2ac25 | 2008-03-14 21:12:12 +0100 | [diff] [blame] | 1744 | |
Peter Zijlstra | d95f98d | 2008-11-04 21:25:08 +0100 | [diff] [blame] | 1745 | if (unlikely(p->sched_class != &fair_sched_class)) |
| 1746 | return; |
| 1747 | |
Ingo Molnar | 4ae7d5c | 2008-03-19 01:42:00 +0100 | [diff] [blame] | 1748 | if (unlikely(se == pse)) |
| 1749 | return; |
| 1750 | |
Mike Galbraith | f685cea | 2009-10-23 23:09:22 +0200 | [diff] [blame] | 1751 | if (sched_feat(NEXT_BUDDY) && scale && !(wake_flags & WF_FORK)) |
Mike Galbraith | 3cb63d5 | 2009-09-11 12:01:17 +0200 | [diff] [blame] | 1752 | set_next_buddy(pse); |
Peter Zijlstra | 57fdc26 | 2008-09-23 15:33:45 +0200 | [diff] [blame] | 1753 | |
Bharata B Rao | aec0a51 | 2008-08-28 14:42:49 +0530 | [diff] [blame] | 1754 | /* |
| 1755 | * We can come here with TIF_NEED_RESCHED already set from new task |
| 1756 | * wake up path. |
| 1757 | */ |
| 1758 | if (test_tsk_need_resched(curr)) |
| 1759 | return; |
| 1760 | |
Ingo Molnar | 91c234b | 2007-10-15 17:00:18 +0200 | [diff] [blame] | 1761 | /* |
Peter Zijlstra | 6bc912b | 2009-01-15 14:53:38 +0100 | [diff] [blame] | 1762 | * Batch and idle tasks do not preempt (their preemption is driven by |
Ingo Molnar | 91c234b | 2007-10-15 17:00:18 +0200 | [diff] [blame] | 1763 | * the tick): |
| 1764 | */ |
Peter Zijlstra | 6bc912b | 2009-01-15 14:53:38 +0100 | [diff] [blame] | 1765 | if (unlikely(p->policy != SCHED_NORMAL)) |
Ingo Molnar | 91c234b | 2007-10-15 17:00:18 +0200 | [diff] [blame] | 1766 | return; |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 1767 | |
Peter Zijlstra | 6bc912b | 2009-01-15 14:53:38 +0100 | [diff] [blame] | 1768 | /* Idle tasks are by definition preempted by everybody. */ |
Peter Zijlstra | 3a7e73a | 2009-11-28 18:51:02 +0100 | [diff] [blame] | 1769 | if (unlikely(curr->policy == SCHED_IDLE)) |
| 1770 | goto preempt; |
Peter Zijlstra | 6bc912b | 2009-01-15 14:53:38 +0100 | [diff] [blame] | 1771 | |
Peter Zijlstra | ad4b78b | 2009-09-16 12:31:31 +0200 | [diff] [blame] | 1772 | if (!sched_feat(WAKEUP_PREEMPT)) |
| 1773 | return; |
| 1774 | |
Jupyung Lee | a65ac74 | 2009-11-17 18:51:40 +0900 | [diff] [blame] | 1775 | update_curr(cfs_rq); |
Peter Zijlstra | 3a7e73a | 2009-11-28 18:51:02 +0100 | [diff] [blame] | 1776 | find_matching_se(&se, &pse); |
| 1777 | BUG_ON(!pse); |
| 1778 | if (wakeup_preempt_entity(se, pse) == 1) |
| 1779 | goto preempt; |
Jupyung Lee | a65ac74 | 2009-11-17 18:51:40 +0900 | [diff] [blame] | 1780 | |
Peter Zijlstra | 3a7e73a | 2009-11-28 18:51:02 +0100 | [diff] [blame] | 1781 | return; |
| 1782 | |
| 1783 | preempt: |
| 1784 | resched_task(curr); |
| 1785 | /* |
| 1786 | * Only set the backward buddy when the current task is still |
| 1787 | * on the rq. This can happen when a wakeup gets interleaved |
| 1788 | * with schedule on the ->pre_schedule() or idle_balance() |
| 1789 | * point, either of which can * drop the rq lock. |
| 1790 | * |
| 1791 | * Also, during early boot the idle thread is in the fair class, |
| 1792 | * for obvious reasons its a bad idea to schedule back to it. |
| 1793 | */ |
| 1794 | if (unlikely(!se->on_rq || curr == rq->idle)) |
| 1795 | return; |
| 1796 | |
| 1797 | if (sched_feat(LAST_BUDDY) && scale && entity_is_task(se)) |
| 1798 | set_last_buddy(se); |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 1799 | } |
| 1800 | |
Ingo Molnar | fb8d472 | 2007-08-09 11:16:48 +0200 | [diff] [blame] | 1801 | static struct task_struct *pick_next_task_fair(struct rq *rq) |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 1802 | { |
Peter Zijlstra | 8f4d37e | 2008-01-25 21:08:29 +0100 | [diff] [blame] | 1803 | struct task_struct *p; |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 1804 | struct cfs_rq *cfs_rq = &rq->cfs; |
| 1805 | struct sched_entity *se; |
| 1806 | |
Tim Blechmann | 36ace27 | 2009-11-24 11:55:45 +0100 | [diff] [blame] | 1807 | if (!cfs_rq->nr_running) |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 1808 | return NULL; |
| 1809 | |
| 1810 | do { |
Ingo Molnar | 9948f4b | 2007-08-09 11:16:48 +0200 | [diff] [blame] | 1811 | se = pick_next_entity(cfs_rq); |
Peter Zijlstra | f4b6755 | 2008-11-04 21:25:07 +0100 | [diff] [blame] | 1812 | set_next_entity(cfs_rq, se); |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 1813 | cfs_rq = group_cfs_rq(se); |
| 1814 | } while (cfs_rq); |
| 1815 | |
Peter Zijlstra | 8f4d37e | 2008-01-25 21:08:29 +0100 | [diff] [blame] | 1816 | p = task_of(se); |
| 1817 | hrtick_start_fair(rq, p); |
| 1818 | |
| 1819 | return p; |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 1820 | } |
| 1821 | |
| 1822 | /* |
| 1823 | * Account for a descheduled task: |
| 1824 | */ |
Ingo Molnar | 31ee529 | 2007-08-09 11:16:49 +0200 | [diff] [blame] | 1825 | 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] | 1826 | { |
| 1827 | struct sched_entity *se = &prev->se; |
| 1828 | struct cfs_rq *cfs_rq; |
| 1829 | |
| 1830 | for_each_sched_entity(se) { |
| 1831 | cfs_rq = cfs_rq_of(se); |
Ingo Molnar | ab6cde2 | 2007-08-09 11:16:48 +0200 | [diff] [blame] | 1832 | put_prev_entity(cfs_rq, se); |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 1833 | } |
| 1834 | } |
| 1835 | |
Peter Williams | 681f3e6 | 2007-10-24 18:23:51 +0200 | [diff] [blame] | 1836 | #ifdef CONFIG_SMP |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 1837 | /************************************************** |
| 1838 | * Fair scheduling class load-balancing methods: |
| 1839 | */ |
| 1840 | |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 1841 | /* |
| 1842 | * pull_task - move a task from a remote runqueue to the local runqueue. |
| 1843 | * Both runqueues must be locked. |
| 1844 | */ |
| 1845 | static void pull_task(struct rq *src_rq, struct task_struct *p, |
| 1846 | struct rq *this_rq, int this_cpu) |
| 1847 | { |
| 1848 | deactivate_task(src_rq, p, 0); |
| 1849 | set_task_cpu(p, this_cpu); |
| 1850 | activate_task(this_rq, p, 0); |
| 1851 | check_preempt_curr(this_rq, p, 0); |
Nikhil Rao | fab4762 | 2010-10-15 13:12:29 -0700 | [diff] [blame] | 1852 | |
| 1853 | /* re-arm NEWIDLE balancing when moving tasks */ |
| 1854 | src_rq->avg_idle = this_rq->avg_idle = 2*sysctl_sched_migration_cost; |
| 1855 | this_rq->idle_stamp = 0; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 1856 | } |
| 1857 | |
| 1858 | /* |
| 1859 | * can_migrate_task - may task p from runqueue rq be migrated to this_cpu? |
| 1860 | */ |
| 1861 | static |
| 1862 | int can_migrate_task(struct task_struct *p, struct rq *rq, int this_cpu, |
| 1863 | struct sched_domain *sd, enum cpu_idle_type idle, |
| 1864 | int *all_pinned) |
| 1865 | { |
| 1866 | int tsk_cache_hot = 0; |
| 1867 | /* |
| 1868 | * We do not migrate tasks that are: |
| 1869 | * 1) running (obviously), or |
| 1870 | * 2) cannot be migrated to this CPU due to cpus_allowed, or |
| 1871 | * 3) are cache-hot on their current CPU. |
| 1872 | */ |
| 1873 | if (!cpumask_test_cpu(this_cpu, &p->cpus_allowed)) { |
Lucas De Marchi | 41acab8 | 2010-03-10 23:37:45 -0300 | [diff] [blame] | 1874 | schedstat_inc(p, se.statistics.nr_failed_migrations_affine); |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 1875 | return 0; |
| 1876 | } |
| 1877 | *all_pinned = 0; |
| 1878 | |
| 1879 | if (task_running(rq, p)) { |
Lucas De Marchi | 41acab8 | 2010-03-10 23:37:45 -0300 | [diff] [blame] | 1880 | schedstat_inc(p, se.statistics.nr_failed_migrations_running); |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 1881 | return 0; |
| 1882 | } |
| 1883 | |
| 1884 | /* |
| 1885 | * Aggressive migration if: |
| 1886 | * 1) task is cache cold, or |
| 1887 | * 2) too many balance attempts have failed. |
| 1888 | */ |
| 1889 | |
Venkatesh Pallipadi | 305e683 | 2010-10-04 17:03:21 -0700 | [diff] [blame] | 1890 | tsk_cache_hot = task_hot(p, rq->clock_task, sd); |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 1891 | if (!tsk_cache_hot || |
| 1892 | sd->nr_balance_failed > sd->cache_nice_tries) { |
| 1893 | #ifdef CONFIG_SCHEDSTATS |
| 1894 | if (tsk_cache_hot) { |
| 1895 | schedstat_inc(sd, lb_hot_gained[idle]); |
Lucas De Marchi | 41acab8 | 2010-03-10 23:37:45 -0300 | [diff] [blame] | 1896 | schedstat_inc(p, se.statistics.nr_forced_migrations); |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 1897 | } |
| 1898 | #endif |
| 1899 | return 1; |
| 1900 | } |
| 1901 | |
| 1902 | if (tsk_cache_hot) { |
Lucas De Marchi | 41acab8 | 2010-03-10 23:37:45 -0300 | [diff] [blame] | 1903 | schedstat_inc(p, se.statistics.nr_failed_migrations_hot); |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 1904 | return 0; |
| 1905 | } |
| 1906 | return 1; |
| 1907 | } |
| 1908 | |
Peter Zijlstra | 897c395 | 2009-12-17 17:45:42 +0100 | [diff] [blame] | 1909 | /* |
| 1910 | * move_one_task tries to move exactly one task from busiest to this_rq, as |
| 1911 | * part of active balancing operations within "domain". |
| 1912 | * Returns 1 if successful and 0 otherwise. |
| 1913 | * |
| 1914 | * Called with both runqueues locked. |
| 1915 | */ |
| 1916 | static int |
| 1917 | move_one_task(struct rq *this_rq, int this_cpu, struct rq *busiest, |
| 1918 | struct sched_domain *sd, enum cpu_idle_type idle) |
| 1919 | { |
| 1920 | struct task_struct *p, *n; |
| 1921 | struct cfs_rq *cfs_rq; |
| 1922 | int pinned = 0; |
| 1923 | |
| 1924 | for_each_leaf_cfs_rq(busiest, cfs_rq) { |
| 1925 | list_for_each_entry_safe(p, n, &cfs_rq->tasks, se.group_node) { |
| 1926 | |
| 1927 | if (!can_migrate_task(p, busiest, this_cpu, |
| 1928 | sd, idle, &pinned)) |
| 1929 | continue; |
| 1930 | |
| 1931 | pull_task(busiest, p, this_rq, this_cpu); |
| 1932 | /* |
| 1933 | * Right now, this is only the second place pull_task() |
| 1934 | * is called, so we can safely collect pull_task() |
| 1935 | * stats here rather than inside pull_task(). |
| 1936 | */ |
| 1937 | schedstat_inc(sd, lb_gained[idle]); |
| 1938 | return 1; |
| 1939 | } |
| 1940 | } |
| 1941 | |
| 1942 | return 0; |
| 1943 | } |
| 1944 | |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 1945 | static unsigned long |
| 1946 | balance_tasks(struct rq *this_rq, int this_cpu, struct rq *busiest, |
| 1947 | unsigned long max_load_move, struct sched_domain *sd, |
| 1948 | enum cpu_idle_type idle, int *all_pinned, |
Peter Zijlstra | ee00e66 | 2009-12-17 17:25:20 +0100 | [diff] [blame] | 1949 | int *this_best_prio, struct cfs_rq *busiest_cfs_rq) |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 1950 | { |
| 1951 | int loops = 0, pulled = 0, pinned = 0; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 1952 | long rem_load_move = max_load_move; |
Peter Zijlstra | ee00e66 | 2009-12-17 17:25:20 +0100 | [diff] [blame] | 1953 | struct task_struct *p, *n; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 1954 | |
| 1955 | if (max_load_move == 0) |
| 1956 | goto out; |
| 1957 | |
| 1958 | pinned = 1; |
| 1959 | |
Peter Zijlstra | ee00e66 | 2009-12-17 17:25:20 +0100 | [diff] [blame] | 1960 | list_for_each_entry_safe(p, n, &busiest_cfs_rq->tasks, se.group_node) { |
| 1961 | if (loops++ > sysctl_sched_nr_migrate) |
| 1962 | break; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 1963 | |
Peter Zijlstra | ee00e66 | 2009-12-17 17:25:20 +0100 | [diff] [blame] | 1964 | if ((p->se.load.weight >> 1) > rem_load_move || |
| 1965 | !can_migrate_task(p, busiest, this_cpu, sd, idle, &pinned)) |
| 1966 | continue; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 1967 | |
Peter Zijlstra | ee00e66 | 2009-12-17 17:25:20 +0100 | [diff] [blame] | 1968 | pull_task(busiest, p, this_rq, this_cpu); |
| 1969 | pulled++; |
| 1970 | rem_load_move -= p->se.load.weight; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 1971 | |
| 1972 | #ifdef CONFIG_PREEMPT |
Peter Zijlstra | ee00e66 | 2009-12-17 17:25:20 +0100 | [diff] [blame] | 1973 | /* |
| 1974 | * NEWIDLE balancing is a source of latency, so preemptible |
| 1975 | * kernels will stop after the first task is pulled to minimize |
| 1976 | * the critical section. |
| 1977 | */ |
| 1978 | if (idle == CPU_NEWLY_IDLE) |
| 1979 | break; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 1980 | #endif |
| 1981 | |
Peter Zijlstra | ee00e66 | 2009-12-17 17:25:20 +0100 | [diff] [blame] | 1982 | /* |
| 1983 | * We only want to steal up to the prescribed amount of |
| 1984 | * weighted load. |
| 1985 | */ |
| 1986 | if (rem_load_move <= 0) |
| 1987 | break; |
| 1988 | |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 1989 | if (p->prio < *this_best_prio) |
| 1990 | *this_best_prio = p->prio; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 1991 | } |
| 1992 | out: |
| 1993 | /* |
| 1994 | * Right now, this is one of only two places pull_task() is called, |
| 1995 | * so we can safely collect pull_task() stats here rather than |
| 1996 | * inside pull_task(). |
| 1997 | */ |
| 1998 | schedstat_add(sd, lb_gained[idle], pulled); |
| 1999 | |
| 2000 | if (all_pinned) |
| 2001 | *all_pinned = pinned; |
| 2002 | |
| 2003 | return max_load_move - rem_load_move; |
| 2004 | } |
| 2005 | |
Peter Zijlstra | 230059de | 2009-12-17 17:47:12 +0100 | [diff] [blame] | 2006 | #ifdef CONFIG_FAIR_GROUP_SCHED |
| 2007 | static unsigned long |
| 2008 | load_balance_fair(struct rq *this_rq, int this_cpu, struct rq *busiest, |
| 2009 | unsigned long max_load_move, |
| 2010 | struct sched_domain *sd, enum cpu_idle_type idle, |
| 2011 | int *all_pinned, int *this_best_prio) |
| 2012 | { |
| 2013 | long rem_load_move = max_load_move; |
| 2014 | int busiest_cpu = cpu_of(busiest); |
| 2015 | struct task_group *tg; |
| 2016 | |
| 2017 | rcu_read_lock(); |
| 2018 | update_h_load(busiest_cpu); |
| 2019 | |
| 2020 | list_for_each_entry_rcu(tg, &task_groups, list) { |
| 2021 | struct cfs_rq *busiest_cfs_rq = tg->cfs_rq[busiest_cpu]; |
| 2022 | unsigned long busiest_h_load = busiest_cfs_rq->h_load; |
| 2023 | unsigned long busiest_weight = busiest_cfs_rq->load.weight; |
| 2024 | u64 rem_load, moved_load; |
| 2025 | |
| 2026 | /* |
| 2027 | * empty group |
| 2028 | */ |
| 2029 | if (!busiest_cfs_rq->task_weight) |
| 2030 | continue; |
| 2031 | |
| 2032 | rem_load = (u64)rem_load_move * busiest_weight; |
| 2033 | rem_load = div_u64(rem_load, busiest_h_load + 1); |
| 2034 | |
| 2035 | moved_load = balance_tasks(this_rq, this_cpu, busiest, |
| 2036 | rem_load, sd, idle, all_pinned, this_best_prio, |
| 2037 | busiest_cfs_rq); |
| 2038 | |
| 2039 | if (!moved_load) |
| 2040 | continue; |
| 2041 | |
| 2042 | moved_load *= busiest_h_load; |
| 2043 | moved_load = div_u64(moved_load, busiest_weight + 1); |
| 2044 | |
| 2045 | rem_load_move -= moved_load; |
| 2046 | if (rem_load_move < 0) |
| 2047 | break; |
| 2048 | } |
| 2049 | rcu_read_unlock(); |
| 2050 | |
| 2051 | return max_load_move - rem_load_move; |
| 2052 | } |
| 2053 | #else |
| 2054 | static unsigned long |
| 2055 | load_balance_fair(struct rq *this_rq, int this_cpu, struct rq *busiest, |
| 2056 | unsigned long max_load_move, |
| 2057 | struct sched_domain *sd, enum cpu_idle_type idle, |
| 2058 | int *all_pinned, int *this_best_prio) |
| 2059 | { |
| 2060 | return balance_tasks(this_rq, this_cpu, busiest, |
| 2061 | max_load_move, sd, idle, all_pinned, |
| 2062 | this_best_prio, &busiest->cfs); |
| 2063 | } |
| 2064 | #endif |
| 2065 | |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 2066 | /* |
| 2067 | * move_tasks tries to move up to max_load_move weighted load from busiest to |
| 2068 | * this_rq, as part of a balancing operation within domain "sd". |
| 2069 | * Returns 1 if successful and 0 otherwise. |
| 2070 | * |
| 2071 | * Called with both runqueues locked. |
| 2072 | */ |
| 2073 | static int move_tasks(struct rq *this_rq, int this_cpu, struct rq *busiest, |
| 2074 | unsigned long max_load_move, |
| 2075 | struct sched_domain *sd, enum cpu_idle_type idle, |
| 2076 | int *all_pinned) |
| 2077 | { |
Peter Zijlstra | 3d45fd8 | 2009-12-17 17:12:46 +0100 | [diff] [blame] | 2078 | unsigned long total_load_moved = 0, load_moved; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 2079 | int this_best_prio = this_rq->curr->prio; |
| 2080 | |
| 2081 | do { |
Peter Zijlstra | 3d45fd8 | 2009-12-17 17:12:46 +0100 | [diff] [blame] | 2082 | load_moved = load_balance_fair(this_rq, this_cpu, busiest, |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 2083 | max_load_move - total_load_moved, |
| 2084 | sd, idle, all_pinned, &this_best_prio); |
Peter Zijlstra | 3d45fd8 | 2009-12-17 17:12:46 +0100 | [diff] [blame] | 2085 | |
| 2086 | total_load_moved += load_moved; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 2087 | |
| 2088 | #ifdef CONFIG_PREEMPT |
| 2089 | /* |
| 2090 | * NEWIDLE balancing is a source of latency, so preemptible |
| 2091 | * kernels will stop after the first task is pulled to minimize |
| 2092 | * the critical section. |
| 2093 | */ |
| 2094 | if (idle == CPU_NEWLY_IDLE && this_rq->nr_running) |
| 2095 | break; |
Peter Zijlstra | baa8c11 | 2009-12-17 18:10:09 +0100 | [diff] [blame] | 2096 | |
| 2097 | if (raw_spin_is_contended(&this_rq->lock) || |
| 2098 | raw_spin_is_contended(&busiest->lock)) |
| 2099 | break; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 2100 | #endif |
Peter Zijlstra | 3d45fd8 | 2009-12-17 17:12:46 +0100 | [diff] [blame] | 2101 | } while (load_moved && max_load_move > total_load_moved); |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 2102 | |
| 2103 | return total_load_moved > 0; |
| 2104 | } |
| 2105 | |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 2106 | /********** Helpers for find_busiest_group ************************/ |
| 2107 | /* |
| 2108 | * sd_lb_stats - Structure to store the statistics of a sched_domain |
| 2109 | * during load balancing. |
| 2110 | */ |
| 2111 | struct sd_lb_stats { |
| 2112 | struct sched_group *busiest; /* Busiest group in this sd */ |
| 2113 | struct sched_group *this; /* Local group in this sd */ |
| 2114 | unsigned long total_load; /* Total load of all groups in sd */ |
| 2115 | unsigned long total_pwr; /* Total power of all groups in sd */ |
| 2116 | unsigned long avg_load; /* Average load across all groups in sd */ |
| 2117 | |
| 2118 | /** Statistics of this group */ |
| 2119 | unsigned long this_load; |
| 2120 | unsigned long this_load_per_task; |
| 2121 | unsigned long this_nr_running; |
Nikhil Rao | fab4762 | 2010-10-15 13:12:29 -0700 | [diff] [blame] | 2122 | unsigned long this_has_capacity; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 2123 | |
| 2124 | /* Statistics of the busiest group */ |
| 2125 | unsigned long max_load; |
| 2126 | unsigned long busiest_load_per_task; |
| 2127 | unsigned long busiest_nr_running; |
Suresh Siddha | dd5feea | 2010-02-23 16:13:52 -0800 | [diff] [blame] | 2128 | unsigned long busiest_group_capacity; |
Nikhil Rao | fab4762 | 2010-10-15 13:12:29 -0700 | [diff] [blame] | 2129 | unsigned long busiest_has_capacity; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 2130 | |
| 2131 | int group_imb; /* Is there imbalance in this sd */ |
| 2132 | #if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT) |
| 2133 | int power_savings_balance; /* Is powersave balance needed for this sd */ |
| 2134 | struct sched_group *group_min; /* Least loaded group in sd */ |
| 2135 | struct sched_group *group_leader; /* Group which relieves group_min */ |
| 2136 | unsigned long min_load_per_task; /* load_per_task in group_min */ |
| 2137 | unsigned long leader_nr_running; /* Nr running of group_leader */ |
| 2138 | unsigned long min_nr_running; /* Nr running of group_min */ |
| 2139 | #endif |
| 2140 | }; |
| 2141 | |
| 2142 | /* |
| 2143 | * sg_lb_stats - stats of a sched_group required for load_balancing |
| 2144 | */ |
| 2145 | struct sg_lb_stats { |
| 2146 | unsigned long avg_load; /*Avg load across the CPUs of the group */ |
| 2147 | unsigned long group_load; /* Total load over the CPUs of the group */ |
| 2148 | unsigned long sum_nr_running; /* Nr tasks running in the group */ |
| 2149 | unsigned long sum_weighted_load; /* Weighted load of group's tasks */ |
| 2150 | unsigned long group_capacity; |
| 2151 | int group_imb; /* Is there an imbalance in the group ? */ |
Nikhil Rao | fab4762 | 2010-10-15 13:12:29 -0700 | [diff] [blame] | 2152 | int group_has_capacity; /* Is there extra capacity in the group? */ |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 2153 | }; |
| 2154 | |
| 2155 | /** |
| 2156 | * group_first_cpu - Returns the first cpu in the cpumask of a sched_group. |
| 2157 | * @group: The group whose first cpu is to be returned. |
| 2158 | */ |
| 2159 | static inline unsigned int group_first_cpu(struct sched_group *group) |
| 2160 | { |
| 2161 | return cpumask_first(sched_group_cpus(group)); |
| 2162 | } |
| 2163 | |
| 2164 | /** |
| 2165 | * get_sd_load_idx - Obtain the load index for a given sched domain. |
| 2166 | * @sd: The sched_domain whose load_idx is to be obtained. |
| 2167 | * @idle: The Idle status of the CPU for whose sd load_icx is obtained. |
| 2168 | */ |
| 2169 | static inline int get_sd_load_idx(struct sched_domain *sd, |
| 2170 | enum cpu_idle_type idle) |
| 2171 | { |
| 2172 | int load_idx; |
| 2173 | |
| 2174 | switch (idle) { |
| 2175 | case CPU_NOT_IDLE: |
| 2176 | load_idx = sd->busy_idx; |
| 2177 | break; |
| 2178 | |
| 2179 | case CPU_NEWLY_IDLE: |
| 2180 | load_idx = sd->newidle_idx; |
| 2181 | break; |
| 2182 | default: |
| 2183 | load_idx = sd->idle_idx; |
| 2184 | break; |
| 2185 | } |
| 2186 | |
| 2187 | return load_idx; |
| 2188 | } |
| 2189 | |
| 2190 | |
| 2191 | #if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT) |
| 2192 | /** |
| 2193 | * init_sd_power_savings_stats - Initialize power savings statistics for |
| 2194 | * the given sched_domain, during load balancing. |
| 2195 | * |
| 2196 | * @sd: Sched domain whose power-savings statistics are to be initialized. |
| 2197 | * @sds: Variable containing the statistics for sd. |
| 2198 | * @idle: Idle status of the CPU at which we're performing load-balancing. |
| 2199 | */ |
| 2200 | static inline void init_sd_power_savings_stats(struct sched_domain *sd, |
| 2201 | struct sd_lb_stats *sds, enum cpu_idle_type idle) |
| 2202 | { |
| 2203 | /* |
| 2204 | * Busy processors will not participate in power savings |
| 2205 | * balance. |
| 2206 | */ |
| 2207 | if (idle == CPU_NOT_IDLE || !(sd->flags & SD_POWERSAVINGS_BALANCE)) |
| 2208 | sds->power_savings_balance = 0; |
| 2209 | else { |
| 2210 | sds->power_savings_balance = 1; |
| 2211 | sds->min_nr_running = ULONG_MAX; |
| 2212 | sds->leader_nr_running = 0; |
| 2213 | } |
| 2214 | } |
| 2215 | |
| 2216 | /** |
| 2217 | * update_sd_power_savings_stats - Update the power saving stats for a |
| 2218 | * sched_domain while performing load balancing. |
| 2219 | * |
| 2220 | * @group: sched_group belonging to the sched_domain under consideration. |
| 2221 | * @sds: Variable containing the statistics of the sched_domain |
| 2222 | * @local_group: Does group contain the CPU for which we're performing |
| 2223 | * load balancing ? |
| 2224 | * @sgs: Variable containing the statistics of the group. |
| 2225 | */ |
| 2226 | static inline void update_sd_power_savings_stats(struct sched_group *group, |
| 2227 | struct sd_lb_stats *sds, int local_group, struct sg_lb_stats *sgs) |
| 2228 | { |
| 2229 | |
| 2230 | if (!sds->power_savings_balance) |
| 2231 | return; |
| 2232 | |
| 2233 | /* |
| 2234 | * If the local group is idle or completely loaded |
| 2235 | * no need to do power savings balance at this domain |
| 2236 | */ |
| 2237 | if (local_group && (sds->this_nr_running >= sgs->group_capacity || |
| 2238 | !sds->this_nr_running)) |
| 2239 | sds->power_savings_balance = 0; |
| 2240 | |
| 2241 | /* |
| 2242 | * If a group is already running at full capacity or idle, |
| 2243 | * don't include that group in power savings calculations |
| 2244 | */ |
| 2245 | if (!sds->power_savings_balance || |
| 2246 | sgs->sum_nr_running >= sgs->group_capacity || |
| 2247 | !sgs->sum_nr_running) |
| 2248 | return; |
| 2249 | |
| 2250 | /* |
| 2251 | * Calculate the group which has the least non-idle load. |
| 2252 | * This is the group from where we need to pick up the load |
| 2253 | * for saving power |
| 2254 | */ |
| 2255 | if ((sgs->sum_nr_running < sds->min_nr_running) || |
| 2256 | (sgs->sum_nr_running == sds->min_nr_running && |
| 2257 | group_first_cpu(group) > group_first_cpu(sds->group_min))) { |
| 2258 | sds->group_min = group; |
| 2259 | sds->min_nr_running = sgs->sum_nr_running; |
| 2260 | sds->min_load_per_task = sgs->sum_weighted_load / |
| 2261 | sgs->sum_nr_running; |
| 2262 | } |
| 2263 | |
| 2264 | /* |
| 2265 | * Calculate the group which is almost near its |
| 2266 | * capacity but still has some space to pick up some load |
| 2267 | * from other group and save more power |
| 2268 | */ |
| 2269 | if (sgs->sum_nr_running + 1 > sgs->group_capacity) |
| 2270 | return; |
| 2271 | |
| 2272 | if (sgs->sum_nr_running > sds->leader_nr_running || |
| 2273 | (sgs->sum_nr_running == sds->leader_nr_running && |
| 2274 | group_first_cpu(group) < group_first_cpu(sds->group_leader))) { |
| 2275 | sds->group_leader = group; |
| 2276 | sds->leader_nr_running = sgs->sum_nr_running; |
| 2277 | } |
| 2278 | } |
| 2279 | |
| 2280 | /** |
| 2281 | * check_power_save_busiest_group - see if there is potential for some power-savings balance |
| 2282 | * @sds: Variable containing the statistics of the sched_domain |
| 2283 | * under consideration. |
| 2284 | * @this_cpu: Cpu at which we're currently performing load-balancing. |
| 2285 | * @imbalance: Variable to store the imbalance. |
| 2286 | * |
| 2287 | * Description: |
| 2288 | * Check if we have potential to perform some power-savings balance. |
| 2289 | * If yes, set the busiest group to be the least loaded group in the |
| 2290 | * sched_domain, so that it's CPUs can be put to idle. |
| 2291 | * |
| 2292 | * Returns 1 if there is potential to perform power-savings balance. |
| 2293 | * Else returns 0. |
| 2294 | */ |
| 2295 | static inline int check_power_save_busiest_group(struct sd_lb_stats *sds, |
| 2296 | int this_cpu, unsigned long *imbalance) |
| 2297 | { |
| 2298 | if (!sds->power_savings_balance) |
| 2299 | return 0; |
| 2300 | |
| 2301 | if (sds->this != sds->group_leader || |
| 2302 | sds->group_leader == sds->group_min) |
| 2303 | return 0; |
| 2304 | |
| 2305 | *imbalance = sds->min_load_per_task; |
| 2306 | sds->busiest = sds->group_min; |
| 2307 | |
| 2308 | return 1; |
| 2309 | |
| 2310 | } |
| 2311 | #else /* CONFIG_SCHED_MC || CONFIG_SCHED_SMT */ |
| 2312 | static inline void init_sd_power_savings_stats(struct sched_domain *sd, |
| 2313 | struct sd_lb_stats *sds, enum cpu_idle_type idle) |
| 2314 | { |
| 2315 | return; |
| 2316 | } |
| 2317 | |
| 2318 | static inline void update_sd_power_savings_stats(struct sched_group *group, |
| 2319 | struct sd_lb_stats *sds, int local_group, struct sg_lb_stats *sgs) |
| 2320 | { |
| 2321 | return; |
| 2322 | } |
| 2323 | |
| 2324 | static inline int check_power_save_busiest_group(struct sd_lb_stats *sds, |
| 2325 | int this_cpu, unsigned long *imbalance) |
| 2326 | { |
| 2327 | return 0; |
| 2328 | } |
| 2329 | #endif /* CONFIG_SCHED_MC || CONFIG_SCHED_SMT */ |
| 2330 | |
| 2331 | |
| 2332 | unsigned long default_scale_freq_power(struct sched_domain *sd, int cpu) |
| 2333 | { |
| 2334 | return SCHED_LOAD_SCALE; |
| 2335 | } |
| 2336 | |
| 2337 | unsigned long __weak arch_scale_freq_power(struct sched_domain *sd, int cpu) |
| 2338 | { |
| 2339 | return default_scale_freq_power(sd, cpu); |
| 2340 | } |
| 2341 | |
| 2342 | unsigned long default_scale_smt_power(struct sched_domain *sd, int cpu) |
| 2343 | { |
Peter Zijlstra | 669c55e | 2010-04-16 14:59:29 +0200 | [diff] [blame] | 2344 | unsigned long weight = sd->span_weight; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 2345 | unsigned long smt_gain = sd->smt_gain; |
| 2346 | |
| 2347 | smt_gain /= weight; |
| 2348 | |
| 2349 | return smt_gain; |
| 2350 | } |
| 2351 | |
| 2352 | unsigned long __weak arch_scale_smt_power(struct sched_domain *sd, int cpu) |
| 2353 | { |
| 2354 | return default_scale_smt_power(sd, cpu); |
| 2355 | } |
| 2356 | |
| 2357 | unsigned long scale_rt_power(int cpu) |
| 2358 | { |
| 2359 | struct rq *rq = cpu_rq(cpu); |
| 2360 | u64 total, available; |
| 2361 | |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 2362 | total = sched_avg_period() + (rq->clock - rq->age_stamp); |
Venkatesh Pallipadi | aa48380 | 2010-10-04 17:03:22 -0700 | [diff] [blame] | 2363 | |
| 2364 | if (unlikely(total < rq->rt_avg)) { |
| 2365 | /* Ensures that power won't end up being negative */ |
| 2366 | available = 0; |
| 2367 | } else { |
| 2368 | available = total - rq->rt_avg; |
| 2369 | } |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 2370 | |
| 2371 | if (unlikely((s64)total < SCHED_LOAD_SCALE)) |
| 2372 | total = SCHED_LOAD_SCALE; |
| 2373 | |
| 2374 | total >>= SCHED_LOAD_SHIFT; |
| 2375 | |
| 2376 | return div_u64(available, total); |
| 2377 | } |
| 2378 | |
| 2379 | static void update_cpu_power(struct sched_domain *sd, int cpu) |
| 2380 | { |
Peter Zijlstra | 669c55e | 2010-04-16 14:59:29 +0200 | [diff] [blame] | 2381 | unsigned long weight = sd->span_weight; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 2382 | unsigned long power = SCHED_LOAD_SCALE; |
| 2383 | struct sched_group *sdg = sd->groups; |
| 2384 | |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 2385 | if ((sd->flags & SD_SHARE_CPUPOWER) && weight > 1) { |
| 2386 | if (sched_feat(ARCH_POWER)) |
| 2387 | power *= arch_scale_smt_power(sd, cpu); |
| 2388 | else |
| 2389 | power *= default_scale_smt_power(sd, cpu); |
| 2390 | |
| 2391 | power >>= SCHED_LOAD_SHIFT; |
| 2392 | } |
| 2393 | |
Srivatsa Vaddagiri | 9d5efe0 | 2010-06-08 14:57:02 +1000 | [diff] [blame] | 2394 | sdg->cpu_power_orig = power; |
| 2395 | |
| 2396 | if (sched_feat(ARCH_POWER)) |
| 2397 | power *= arch_scale_freq_power(sd, cpu); |
| 2398 | else |
| 2399 | power *= default_scale_freq_power(sd, cpu); |
| 2400 | |
| 2401 | power >>= SCHED_LOAD_SHIFT; |
| 2402 | |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 2403 | power *= scale_rt_power(cpu); |
| 2404 | power >>= SCHED_LOAD_SHIFT; |
| 2405 | |
| 2406 | if (!power) |
| 2407 | power = 1; |
| 2408 | |
Peter Zijlstra | e51fd5e | 2010-05-31 12:37:30 +0200 | [diff] [blame] | 2409 | cpu_rq(cpu)->cpu_power = power; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 2410 | sdg->cpu_power = power; |
| 2411 | } |
| 2412 | |
| 2413 | static void update_group_power(struct sched_domain *sd, int cpu) |
| 2414 | { |
| 2415 | struct sched_domain *child = sd->child; |
| 2416 | struct sched_group *group, *sdg = sd->groups; |
| 2417 | unsigned long power; |
| 2418 | |
| 2419 | if (!child) { |
| 2420 | update_cpu_power(sd, cpu); |
| 2421 | return; |
| 2422 | } |
| 2423 | |
| 2424 | power = 0; |
| 2425 | |
| 2426 | group = child->groups; |
| 2427 | do { |
| 2428 | power += group->cpu_power; |
| 2429 | group = group->next; |
| 2430 | } while (group != child->groups); |
| 2431 | |
| 2432 | sdg->cpu_power = power; |
| 2433 | } |
| 2434 | |
Srivatsa Vaddagiri | 9d5efe0 | 2010-06-08 14:57:02 +1000 | [diff] [blame] | 2435 | /* |
| 2436 | * Try and fix up capacity for tiny siblings, this is needed when |
| 2437 | * things like SD_ASYM_PACKING need f_b_g to select another sibling |
| 2438 | * which on its own isn't powerful enough. |
| 2439 | * |
| 2440 | * See update_sd_pick_busiest() and check_asym_packing(). |
| 2441 | */ |
| 2442 | static inline int |
| 2443 | fix_small_capacity(struct sched_domain *sd, struct sched_group *group) |
| 2444 | { |
| 2445 | /* |
| 2446 | * Only siblings can have significantly less than SCHED_LOAD_SCALE |
| 2447 | */ |
| 2448 | if (sd->level != SD_LV_SIBLING) |
| 2449 | return 0; |
| 2450 | |
| 2451 | /* |
| 2452 | * If ~90% of the cpu_power is still there, we're good. |
| 2453 | */ |
Michael Neuling | 694f5a1 | 2010-06-10 09:03:37 +1000 | [diff] [blame] | 2454 | if (group->cpu_power * 32 > group->cpu_power_orig * 29) |
Srivatsa Vaddagiri | 9d5efe0 | 2010-06-08 14:57:02 +1000 | [diff] [blame] | 2455 | return 1; |
| 2456 | |
| 2457 | return 0; |
| 2458 | } |
| 2459 | |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 2460 | /** |
| 2461 | * update_sg_lb_stats - Update sched_group's statistics for load balancing. |
| 2462 | * @sd: The sched_domain whose statistics are to be updated. |
| 2463 | * @group: sched_group whose statistics are to be updated. |
| 2464 | * @this_cpu: Cpu for which load balance is currently performed. |
| 2465 | * @idle: Idle status of this_cpu |
| 2466 | * @load_idx: Load index of sched_domain of this_cpu for load calc. |
| 2467 | * @sd_idle: Idle status of the sched_domain containing group. |
| 2468 | * @local_group: Does group contain this_cpu. |
| 2469 | * @cpus: Set of cpus considered for load balancing. |
| 2470 | * @balance: Should we balance. |
| 2471 | * @sgs: variable to hold the statistics for this group. |
| 2472 | */ |
| 2473 | static inline void update_sg_lb_stats(struct sched_domain *sd, |
| 2474 | struct sched_group *group, int this_cpu, |
| 2475 | enum cpu_idle_type idle, int load_idx, int *sd_idle, |
| 2476 | int local_group, const struct cpumask *cpus, |
| 2477 | int *balance, struct sg_lb_stats *sgs) |
| 2478 | { |
Nikhil Rao | 2582f0e | 2010-10-13 12:09:36 -0700 | [diff] [blame] | 2479 | unsigned long load, max_cpu_load, min_cpu_load, max_nr_running; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 2480 | int i; |
| 2481 | unsigned int balance_cpu = -1, first_idle_cpu = 0; |
Suresh Siddha | dd5feea | 2010-02-23 16:13:52 -0800 | [diff] [blame] | 2482 | unsigned long avg_load_per_task = 0; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 2483 | |
Gautham R Shenoy | 871e35b | 2010-01-20 14:02:44 -0600 | [diff] [blame] | 2484 | if (local_group) |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 2485 | balance_cpu = group_first_cpu(group); |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 2486 | |
| 2487 | /* Tally up the load of all CPUs in the group */ |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 2488 | max_cpu_load = 0; |
| 2489 | min_cpu_load = ~0UL; |
Nikhil Rao | 2582f0e | 2010-10-13 12:09:36 -0700 | [diff] [blame] | 2490 | max_nr_running = 0; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 2491 | |
| 2492 | for_each_cpu_and(i, sched_group_cpus(group), cpus) { |
| 2493 | struct rq *rq = cpu_rq(i); |
| 2494 | |
| 2495 | if (*sd_idle && rq->nr_running) |
| 2496 | *sd_idle = 0; |
| 2497 | |
| 2498 | /* Bias balancing toward cpus of our domain */ |
| 2499 | if (local_group) { |
| 2500 | if (idle_cpu(i) && !first_idle_cpu) { |
| 2501 | first_idle_cpu = 1; |
| 2502 | balance_cpu = i; |
| 2503 | } |
| 2504 | |
| 2505 | load = target_load(i, load_idx); |
| 2506 | } else { |
| 2507 | load = source_load(i, load_idx); |
Nikhil Rao | 2582f0e | 2010-10-13 12:09:36 -0700 | [diff] [blame] | 2508 | if (load > max_cpu_load) { |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 2509 | max_cpu_load = load; |
Nikhil Rao | 2582f0e | 2010-10-13 12:09:36 -0700 | [diff] [blame] | 2510 | max_nr_running = rq->nr_running; |
| 2511 | } |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 2512 | if (min_cpu_load > load) |
| 2513 | min_cpu_load = load; |
| 2514 | } |
| 2515 | |
| 2516 | sgs->group_load += load; |
| 2517 | sgs->sum_nr_running += rq->nr_running; |
| 2518 | sgs->sum_weighted_load += weighted_cpuload(i); |
| 2519 | |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 2520 | } |
| 2521 | |
| 2522 | /* |
| 2523 | * First idle cpu or the first cpu(busiest) in this sched group |
| 2524 | * is eligible for doing load balancing at this and above |
| 2525 | * domains. In the newly idle case, we will allow all the cpu's |
| 2526 | * to do the newly idle load balance. |
| 2527 | */ |
Peter Zijlstra | bbc8cb5 | 2010-07-09 15:15:43 +0200 | [diff] [blame] | 2528 | if (idle != CPU_NEWLY_IDLE && local_group) { |
| 2529 | if (balance_cpu != this_cpu) { |
| 2530 | *balance = 0; |
| 2531 | return; |
| 2532 | } |
| 2533 | update_group_power(sd, this_cpu); |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 2534 | } |
| 2535 | |
| 2536 | /* Adjust by relative CPU power of the group */ |
| 2537 | sgs->avg_load = (sgs->group_load * SCHED_LOAD_SCALE) / group->cpu_power; |
| 2538 | |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 2539 | /* |
| 2540 | * Consider the group unbalanced when the imbalance is larger |
| 2541 | * than the average weight of two tasks. |
| 2542 | * |
| 2543 | * APZ: with cgroup the avg task weight can vary wildly and |
| 2544 | * might not be a suitable number - should we keep a |
| 2545 | * normalized nr_running number somewhere that negates |
| 2546 | * the hierarchy? |
| 2547 | */ |
Suresh Siddha | dd5feea | 2010-02-23 16:13:52 -0800 | [diff] [blame] | 2548 | if (sgs->sum_nr_running) |
| 2549 | avg_load_per_task = sgs->sum_weighted_load / sgs->sum_nr_running; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 2550 | |
Nikhil Rao | 2582f0e | 2010-10-13 12:09:36 -0700 | [diff] [blame] | 2551 | if ((max_cpu_load - min_cpu_load) > 2*avg_load_per_task && max_nr_running > 1) |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 2552 | sgs->group_imb = 1; |
| 2553 | |
Nikhil Rao | 2582f0e | 2010-10-13 12:09:36 -0700 | [diff] [blame] | 2554 | sgs->group_capacity = DIV_ROUND_CLOSEST(group->cpu_power, SCHED_LOAD_SCALE); |
Srivatsa Vaddagiri | 9d5efe0 | 2010-06-08 14:57:02 +1000 | [diff] [blame] | 2555 | if (!sgs->group_capacity) |
| 2556 | sgs->group_capacity = fix_small_capacity(sd, group); |
Nikhil Rao | fab4762 | 2010-10-15 13:12:29 -0700 | [diff] [blame] | 2557 | |
| 2558 | if (sgs->group_capacity > sgs->sum_nr_running) |
| 2559 | sgs->group_has_capacity = 1; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 2560 | } |
| 2561 | |
| 2562 | /** |
Michael Neuling | 532cb4c | 2010-06-08 14:57:02 +1000 | [diff] [blame] | 2563 | * update_sd_pick_busiest - return 1 on busiest group |
| 2564 | * @sd: sched_domain whose statistics are to be checked |
| 2565 | * @sds: sched_domain statistics |
| 2566 | * @sg: sched_group candidate to be checked for being the busiest |
Michael Neuling | b6b1229 | 2010-06-10 12:06:21 +1000 | [diff] [blame] | 2567 | * @sgs: sched_group statistics |
| 2568 | * @this_cpu: the current cpu |
Michael Neuling | 532cb4c | 2010-06-08 14:57:02 +1000 | [diff] [blame] | 2569 | * |
| 2570 | * Determine if @sg is a busier group than the previously selected |
| 2571 | * busiest group. |
| 2572 | */ |
| 2573 | static bool update_sd_pick_busiest(struct sched_domain *sd, |
| 2574 | struct sd_lb_stats *sds, |
| 2575 | struct sched_group *sg, |
| 2576 | struct sg_lb_stats *sgs, |
| 2577 | int this_cpu) |
| 2578 | { |
| 2579 | if (sgs->avg_load <= sds->max_load) |
| 2580 | return false; |
| 2581 | |
| 2582 | if (sgs->sum_nr_running > sgs->group_capacity) |
| 2583 | return true; |
| 2584 | |
| 2585 | if (sgs->group_imb) |
| 2586 | return true; |
| 2587 | |
| 2588 | /* |
| 2589 | * ASYM_PACKING needs to move all the work to the lowest |
| 2590 | * numbered CPUs in the group, therefore mark all groups |
| 2591 | * higher than ourself as busy. |
| 2592 | */ |
| 2593 | if ((sd->flags & SD_ASYM_PACKING) && sgs->sum_nr_running && |
| 2594 | this_cpu < group_first_cpu(sg)) { |
| 2595 | if (!sds->busiest) |
| 2596 | return true; |
| 2597 | |
| 2598 | if (group_first_cpu(sds->busiest) > group_first_cpu(sg)) |
| 2599 | return true; |
| 2600 | } |
| 2601 | |
| 2602 | return false; |
| 2603 | } |
| 2604 | |
| 2605 | /** |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 2606 | * update_sd_lb_stats - Update sched_group's statistics for load balancing. |
| 2607 | * @sd: sched_domain whose statistics are to be updated. |
| 2608 | * @this_cpu: Cpu for which load balance is currently performed. |
| 2609 | * @idle: Idle status of this_cpu |
Michael Neuling | 532cb4c | 2010-06-08 14:57:02 +1000 | [diff] [blame] | 2610 | * @sd_idle: Idle status of the sched_domain containing sg. |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 2611 | * @cpus: Set of cpus considered for load balancing. |
| 2612 | * @balance: Should we balance. |
| 2613 | * @sds: variable to hold the statistics for this sched_domain. |
| 2614 | */ |
| 2615 | static inline void update_sd_lb_stats(struct sched_domain *sd, int this_cpu, |
| 2616 | enum cpu_idle_type idle, int *sd_idle, |
| 2617 | const struct cpumask *cpus, int *balance, |
| 2618 | struct sd_lb_stats *sds) |
| 2619 | { |
| 2620 | struct sched_domain *child = sd->child; |
Michael Neuling | 532cb4c | 2010-06-08 14:57:02 +1000 | [diff] [blame] | 2621 | struct sched_group *sg = sd->groups; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 2622 | struct sg_lb_stats sgs; |
| 2623 | int load_idx, prefer_sibling = 0; |
| 2624 | |
| 2625 | if (child && child->flags & SD_PREFER_SIBLING) |
| 2626 | prefer_sibling = 1; |
| 2627 | |
| 2628 | init_sd_power_savings_stats(sd, sds, idle); |
| 2629 | load_idx = get_sd_load_idx(sd, idle); |
| 2630 | |
| 2631 | do { |
| 2632 | int local_group; |
| 2633 | |
Michael Neuling | 532cb4c | 2010-06-08 14:57:02 +1000 | [diff] [blame] | 2634 | local_group = cpumask_test_cpu(this_cpu, sched_group_cpus(sg)); |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 2635 | memset(&sgs, 0, sizeof(sgs)); |
Michael Neuling | 532cb4c | 2010-06-08 14:57:02 +1000 | [diff] [blame] | 2636 | update_sg_lb_stats(sd, sg, this_cpu, idle, load_idx, sd_idle, |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 2637 | local_group, cpus, balance, &sgs); |
| 2638 | |
Peter Zijlstra | 8f190fb | 2009-12-24 14:18:21 +0100 | [diff] [blame] | 2639 | if (local_group && !(*balance)) |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 2640 | return; |
| 2641 | |
| 2642 | sds->total_load += sgs.group_load; |
Michael Neuling | 532cb4c | 2010-06-08 14:57:02 +1000 | [diff] [blame] | 2643 | sds->total_pwr += sg->cpu_power; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 2644 | |
| 2645 | /* |
| 2646 | * In case the child domain prefers tasks go to siblings |
Michael Neuling | 532cb4c | 2010-06-08 14:57:02 +1000 | [diff] [blame] | 2647 | * first, lower the sg capacity to one so that we'll try |
Nikhil Rao | 75dd321 | 2010-10-15 13:12:30 -0700 | [diff] [blame] | 2648 | * and move all the excess tasks away. We lower the capacity |
| 2649 | * of a group only if the local group has the capacity to fit |
| 2650 | * these excess tasks, i.e. nr_running < group_capacity. The |
| 2651 | * extra check prevents the case where you always pull from the |
| 2652 | * heaviest group when it is already under-utilized (possible |
| 2653 | * with a large weight task outweighs the tasks on the system). |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 2654 | */ |
Nikhil Rao | 75dd321 | 2010-10-15 13:12:30 -0700 | [diff] [blame] | 2655 | if (prefer_sibling && !local_group && sds->this_has_capacity) |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 2656 | sgs.group_capacity = min(sgs.group_capacity, 1UL); |
| 2657 | |
| 2658 | if (local_group) { |
| 2659 | sds->this_load = sgs.avg_load; |
Michael Neuling | 532cb4c | 2010-06-08 14:57:02 +1000 | [diff] [blame] | 2660 | sds->this = sg; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 2661 | sds->this_nr_running = sgs.sum_nr_running; |
| 2662 | sds->this_load_per_task = sgs.sum_weighted_load; |
Nikhil Rao | fab4762 | 2010-10-15 13:12:29 -0700 | [diff] [blame] | 2663 | sds->this_has_capacity = sgs.group_has_capacity; |
Michael Neuling | 532cb4c | 2010-06-08 14:57:02 +1000 | [diff] [blame] | 2664 | } else if (update_sd_pick_busiest(sd, sds, sg, &sgs, this_cpu)) { |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 2665 | sds->max_load = sgs.avg_load; |
Michael Neuling | 532cb4c | 2010-06-08 14:57:02 +1000 | [diff] [blame] | 2666 | sds->busiest = sg; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 2667 | sds->busiest_nr_running = sgs.sum_nr_running; |
Suresh Siddha | dd5feea | 2010-02-23 16:13:52 -0800 | [diff] [blame] | 2668 | sds->busiest_group_capacity = sgs.group_capacity; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 2669 | sds->busiest_load_per_task = sgs.sum_weighted_load; |
Nikhil Rao | fab4762 | 2010-10-15 13:12:29 -0700 | [diff] [blame] | 2670 | sds->busiest_has_capacity = sgs.group_has_capacity; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 2671 | sds->group_imb = sgs.group_imb; |
| 2672 | } |
| 2673 | |
Michael Neuling | 532cb4c | 2010-06-08 14:57:02 +1000 | [diff] [blame] | 2674 | update_sd_power_savings_stats(sg, sds, local_group, &sgs); |
| 2675 | sg = sg->next; |
| 2676 | } while (sg != sd->groups); |
| 2677 | } |
| 2678 | |
Michael Neuling | 2ec57d4 | 2010-06-29 12:02:01 +1000 | [diff] [blame] | 2679 | int __weak arch_sd_sibling_asym_packing(void) |
Michael Neuling | 532cb4c | 2010-06-08 14:57:02 +1000 | [diff] [blame] | 2680 | { |
| 2681 | return 0*SD_ASYM_PACKING; |
| 2682 | } |
| 2683 | |
| 2684 | /** |
| 2685 | * check_asym_packing - Check to see if the group is packed into the |
| 2686 | * sched doman. |
| 2687 | * |
| 2688 | * This is primarily intended to used at the sibling level. Some |
| 2689 | * cores like POWER7 prefer to use lower numbered SMT threads. In the |
| 2690 | * case of POWER7, it can move to lower SMT modes only when higher |
| 2691 | * threads are idle. When in lower SMT modes, the threads will |
| 2692 | * perform better since they share less core resources. Hence when we |
| 2693 | * have idle threads, we want them to be the higher ones. |
| 2694 | * |
| 2695 | * This packing function is run on idle threads. It checks to see if |
| 2696 | * the busiest CPU in this domain (core in the P7 case) has a higher |
| 2697 | * CPU number than the packing function is being run on. Here we are |
| 2698 | * assuming lower CPU number will be equivalent to lower a SMT thread |
| 2699 | * number. |
| 2700 | * |
Michael Neuling | b6b1229 | 2010-06-10 12:06:21 +1000 | [diff] [blame] | 2701 | * Returns 1 when packing is required and a task should be moved to |
| 2702 | * this CPU. The amount of the imbalance is returned in *imbalance. |
| 2703 | * |
Michael Neuling | 532cb4c | 2010-06-08 14:57:02 +1000 | [diff] [blame] | 2704 | * @sd: The sched_domain whose packing is to be checked. |
| 2705 | * @sds: Statistics of the sched_domain which is to be packed |
| 2706 | * @this_cpu: The cpu at whose sched_domain we're performing load-balance. |
| 2707 | * @imbalance: returns amount of imbalanced due to packing. |
Michael Neuling | 532cb4c | 2010-06-08 14:57:02 +1000 | [diff] [blame] | 2708 | */ |
| 2709 | static int check_asym_packing(struct sched_domain *sd, |
| 2710 | struct sd_lb_stats *sds, |
| 2711 | int this_cpu, unsigned long *imbalance) |
| 2712 | { |
| 2713 | int busiest_cpu; |
| 2714 | |
| 2715 | if (!(sd->flags & SD_ASYM_PACKING)) |
| 2716 | return 0; |
| 2717 | |
| 2718 | if (!sds->busiest) |
| 2719 | return 0; |
| 2720 | |
| 2721 | busiest_cpu = group_first_cpu(sds->busiest); |
| 2722 | if (this_cpu > busiest_cpu) |
| 2723 | return 0; |
| 2724 | |
| 2725 | *imbalance = DIV_ROUND_CLOSEST(sds->max_load * sds->busiest->cpu_power, |
| 2726 | SCHED_LOAD_SCALE); |
| 2727 | return 1; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 2728 | } |
| 2729 | |
| 2730 | /** |
| 2731 | * fix_small_imbalance - Calculate the minor imbalance that exists |
| 2732 | * amongst the groups of a sched_domain, during |
| 2733 | * load balancing. |
| 2734 | * @sds: Statistics of the sched_domain whose imbalance is to be calculated. |
| 2735 | * @this_cpu: The cpu at whose sched_domain we're performing load-balance. |
| 2736 | * @imbalance: Variable to store the imbalance. |
| 2737 | */ |
| 2738 | static inline void fix_small_imbalance(struct sd_lb_stats *sds, |
| 2739 | int this_cpu, unsigned long *imbalance) |
| 2740 | { |
| 2741 | unsigned long tmp, pwr_now = 0, pwr_move = 0; |
| 2742 | unsigned int imbn = 2; |
Suresh Siddha | dd5feea | 2010-02-23 16:13:52 -0800 | [diff] [blame] | 2743 | unsigned long scaled_busy_load_per_task; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 2744 | |
| 2745 | if (sds->this_nr_running) { |
| 2746 | sds->this_load_per_task /= sds->this_nr_running; |
| 2747 | if (sds->busiest_load_per_task > |
| 2748 | sds->this_load_per_task) |
| 2749 | imbn = 1; |
| 2750 | } else |
| 2751 | sds->this_load_per_task = |
| 2752 | cpu_avg_load_per_task(this_cpu); |
| 2753 | |
Suresh Siddha | dd5feea | 2010-02-23 16:13:52 -0800 | [diff] [blame] | 2754 | scaled_busy_load_per_task = sds->busiest_load_per_task |
| 2755 | * SCHED_LOAD_SCALE; |
| 2756 | scaled_busy_load_per_task /= sds->busiest->cpu_power; |
| 2757 | |
| 2758 | if (sds->max_load - sds->this_load + scaled_busy_load_per_task >= |
| 2759 | (scaled_busy_load_per_task * imbn)) { |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 2760 | *imbalance = sds->busiest_load_per_task; |
| 2761 | return; |
| 2762 | } |
| 2763 | |
| 2764 | /* |
| 2765 | * OK, we don't have enough imbalance to justify moving tasks, |
| 2766 | * however we may be able to increase total CPU power used by |
| 2767 | * moving them. |
| 2768 | */ |
| 2769 | |
| 2770 | pwr_now += sds->busiest->cpu_power * |
| 2771 | min(sds->busiest_load_per_task, sds->max_load); |
| 2772 | pwr_now += sds->this->cpu_power * |
| 2773 | min(sds->this_load_per_task, sds->this_load); |
| 2774 | pwr_now /= SCHED_LOAD_SCALE; |
| 2775 | |
| 2776 | /* Amount of load we'd subtract */ |
| 2777 | tmp = (sds->busiest_load_per_task * SCHED_LOAD_SCALE) / |
| 2778 | sds->busiest->cpu_power; |
| 2779 | if (sds->max_load > tmp) |
| 2780 | pwr_move += sds->busiest->cpu_power * |
| 2781 | min(sds->busiest_load_per_task, sds->max_load - tmp); |
| 2782 | |
| 2783 | /* Amount of load we'd add */ |
| 2784 | if (sds->max_load * sds->busiest->cpu_power < |
| 2785 | sds->busiest_load_per_task * SCHED_LOAD_SCALE) |
| 2786 | tmp = (sds->max_load * sds->busiest->cpu_power) / |
| 2787 | sds->this->cpu_power; |
| 2788 | else |
| 2789 | tmp = (sds->busiest_load_per_task * SCHED_LOAD_SCALE) / |
| 2790 | sds->this->cpu_power; |
| 2791 | pwr_move += sds->this->cpu_power * |
| 2792 | min(sds->this_load_per_task, sds->this_load + tmp); |
| 2793 | pwr_move /= SCHED_LOAD_SCALE; |
| 2794 | |
| 2795 | /* Move if we gain throughput */ |
| 2796 | if (pwr_move > pwr_now) |
| 2797 | *imbalance = sds->busiest_load_per_task; |
| 2798 | } |
| 2799 | |
| 2800 | /** |
| 2801 | * calculate_imbalance - Calculate the amount of imbalance present within the |
| 2802 | * groups of a given sched_domain during load balance. |
| 2803 | * @sds: statistics of the sched_domain whose imbalance is to be calculated. |
| 2804 | * @this_cpu: Cpu for which currently load balance is being performed. |
| 2805 | * @imbalance: The variable to store the imbalance. |
| 2806 | */ |
| 2807 | static inline void calculate_imbalance(struct sd_lb_stats *sds, int this_cpu, |
| 2808 | unsigned long *imbalance) |
| 2809 | { |
Suresh Siddha | dd5feea | 2010-02-23 16:13:52 -0800 | [diff] [blame] | 2810 | unsigned long max_pull, load_above_capacity = ~0UL; |
| 2811 | |
| 2812 | sds->busiest_load_per_task /= sds->busiest_nr_running; |
| 2813 | if (sds->group_imb) { |
| 2814 | sds->busiest_load_per_task = |
| 2815 | min(sds->busiest_load_per_task, sds->avg_load); |
| 2816 | } |
| 2817 | |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 2818 | /* |
| 2819 | * In the presence of smp nice balancing, certain scenarios can have |
| 2820 | * max load less than avg load(as we skip the groups at or below |
| 2821 | * its cpu_power, while calculating max_load..) |
| 2822 | */ |
| 2823 | if (sds->max_load < sds->avg_load) { |
| 2824 | *imbalance = 0; |
| 2825 | return fix_small_imbalance(sds, this_cpu, imbalance); |
| 2826 | } |
| 2827 | |
Suresh Siddha | dd5feea | 2010-02-23 16:13:52 -0800 | [diff] [blame] | 2828 | if (!sds->group_imb) { |
| 2829 | /* |
| 2830 | * Don't want to pull so many tasks that a group would go idle. |
| 2831 | */ |
| 2832 | load_above_capacity = (sds->busiest_nr_running - |
| 2833 | sds->busiest_group_capacity); |
| 2834 | |
| 2835 | load_above_capacity *= (SCHED_LOAD_SCALE * SCHED_LOAD_SCALE); |
| 2836 | |
| 2837 | load_above_capacity /= sds->busiest->cpu_power; |
| 2838 | } |
| 2839 | |
| 2840 | /* |
| 2841 | * We're trying to get all the cpus to the average_load, so we don't |
| 2842 | * want to push ourselves above the average load, nor do we wish to |
| 2843 | * reduce the max loaded cpu below the average load. At the same time, |
| 2844 | * we also don't want to reduce the group load below the group capacity |
| 2845 | * (so that we can implement power-savings policies etc). Thus we look |
| 2846 | * for the minimum possible imbalance. |
| 2847 | * Be careful of negative numbers as they'll appear as very large values |
| 2848 | * with unsigned longs. |
| 2849 | */ |
| 2850 | max_pull = min(sds->max_load - sds->avg_load, load_above_capacity); |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 2851 | |
| 2852 | /* How much load to actually move to equalise the imbalance */ |
| 2853 | *imbalance = min(max_pull * sds->busiest->cpu_power, |
| 2854 | (sds->avg_load - sds->this_load) * sds->this->cpu_power) |
| 2855 | / SCHED_LOAD_SCALE; |
| 2856 | |
| 2857 | /* |
| 2858 | * if *imbalance is less than the average load per runnable task |
| 2859 | * there is no gaurantee that any tasks will be moved so we'll have |
| 2860 | * a think about bumping its value to force at least one task to be |
| 2861 | * moved |
| 2862 | */ |
| 2863 | if (*imbalance < sds->busiest_load_per_task) |
| 2864 | return fix_small_imbalance(sds, this_cpu, imbalance); |
| 2865 | |
| 2866 | } |
Nikhil Rao | fab4762 | 2010-10-15 13:12:29 -0700 | [diff] [blame] | 2867 | |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 2868 | /******* find_busiest_group() helpers end here *********************/ |
| 2869 | |
| 2870 | /** |
| 2871 | * find_busiest_group - Returns the busiest group within the sched_domain |
| 2872 | * if there is an imbalance. If there isn't an imbalance, and |
| 2873 | * the user has opted for power-savings, it returns a group whose |
| 2874 | * CPUs can be put to idle by rebalancing those tasks elsewhere, if |
| 2875 | * such a group exists. |
| 2876 | * |
| 2877 | * Also calculates the amount of weighted load which should be moved |
| 2878 | * to restore balance. |
| 2879 | * |
| 2880 | * @sd: The sched_domain whose busiest group is to be returned. |
| 2881 | * @this_cpu: The cpu for which load balancing is currently being performed. |
| 2882 | * @imbalance: Variable which stores amount of weighted load which should |
| 2883 | * be moved to restore balance/put a group to idle. |
| 2884 | * @idle: The idle status of this_cpu. |
| 2885 | * @sd_idle: The idleness of sd |
| 2886 | * @cpus: The set of CPUs under consideration for load-balancing. |
| 2887 | * @balance: Pointer to a variable indicating if this_cpu |
| 2888 | * is the appropriate cpu to perform load balancing at this_level. |
| 2889 | * |
| 2890 | * Returns: - the busiest group if imbalance exists. |
| 2891 | * - If no imbalance and user has opted for power-savings balance, |
| 2892 | * return the least loaded group whose CPUs can be |
| 2893 | * put to idle by rebalancing its tasks onto our group. |
| 2894 | */ |
| 2895 | static struct sched_group * |
| 2896 | find_busiest_group(struct sched_domain *sd, int this_cpu, |
| 2897 | unsigned long *imbalance, enum cpu_idle_type idle, |
| 2898 | int *sd_idle, const struct cpumask *cpus, int *balance) |
| 2899 | { |
| 2900 | struct sd_lb_stats sds; |
| 2901 | |
| 2902 | memset(&sds, 0, sizeof(sds)); |
| 2903 | |
| 2904 | /* |
| 2905 | * Compute the various statistics relavent for load balancing at |
| 2906 | * this level. |
| 2907 | */ |
| 2908 | update_sd_lb_stats(sd, this_cpu, idle, sd_idle, cpus, |
| 2909 | balance, &sds); |
| 2910 | |
| 2911 | /* Cases where imbalance does not exist from POV of this_cpu */ |
| 2912 | /* 1) this_cpu is not the appropriate cpu to perform load balancing |
| 2913 | * at this level. |
| 2914 | * 2) There is no busy sibling group to pull from. |
| 2915 | * 3) This group is the busiest group. |
| 2916 | * 4) This group is more busy than the avg busieness at this |
| 2917 | * sched_domain. |
| 2918 | * 5) The imbalance is within the specified limit. |
Nikhil Rao | fab4762 | 2010-10-15 13:12:29 -0700 | [diff] [blame] | 2919 | * |
| 2920 | * Note: when doing newidle balance, if the local group has excess |
| 2921 | * capacity (i.e. nr_running < group_capacity) and the busiest group |
| 2922 | * does not have any capacity, we force a load balance to pull tasks |
| 2923 | * to the local group. In this case, we skip past checks 3, 4 and 5. |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 2924 | */ |
Peter Zijlstra | 8f190fb | 2009-12-24 14:18:21 +0100 | [diff] [blame] | 2925 | if (!(*balance)) |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 2926 | goto ret; |
| 2927 | |
Michael Neuling | 532cb4c | 2010-06-08 14:57:02 +1000 | [diff] [blame] | 2928 | if ((idle == CPU_IDLE || idle == CPU_NEWLY_IDLE) && |
| 2929 | check_asym_packing(sd, &sds, this_cpu, imbalance)) |
| 2930 | return sds.busiest; |
| 2931 | |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 2932 | if (!sds.busiest || sds.busiest_nr_running == 0) |
| 2933 | goto out_balanced; |
| 2934 | |
Nikhil Rao | fab4762 | 2010-10-15 13:12:29 -0700 | [diff] [blame] | 2935 | /* SD_BALANCE_NEWIDLE trumps SMP nice when underutilized */ |
| 2936 | if (idle == CPU_NEWLY_IDLE && sds.this_has_capacity && |
| 2937 | !sds.busiest_has_capacity) |
| 2938 | goto force_balance; |
| 2939 | |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 2940 | if (sds.this_load >= sds.max_load) |
| 2941 | goto out_balanced; |
| 2942 | |
| 2943 | sds.avg_load = (SCHED_LOAD_SCALE * sds.total_load) / sds.total_pwr; |
| 2944 | |
| 2945 | if (sds.this_load >= sds.avg_load) |
| 2946 | goto out_balanced; |
| 2947 | |
| 2948 | if (100 * sds.max_load <= sd->imbalance_pct * sds.this_load) |
| 2949 | goto out_balanced; |
| 2950 | |
Nikhil Rao | fab4762 | 2010-10-15 13:12:29 -0700 | [diff] [blame] | 2951 | force_balance: |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 2952 | /* Looks like there is an imbalance. Compute it */ |
| 2953 | calculate_imbalance(&sds, this_cpu, imbalance); |
| 2954 | return sds.busiest; |
| 2955 | |
| 2956 | out_balanced: |
| 2957 | /* |
| 2958 | * There is no obvious imbalance. But check if we can do some balancing |
| 2959 | * to save power. |
| 2960 | */ |
| 2961 | if (check_power_save_busiest_group(&sds, this_cpu, imbalance)) |
| 2962 | return sds.busiest; |
| 2963 | ret: |
| 2964 | *imbalance = 0; |
| 2965 | return NULL; |
| 2966 | } |
| 2967 | |
| 2968 | /* |
| 2969 | * find_busiest_queue - find the busiest runqueue among the cpus in group. |
| 2970 | */ |
| 2971 | static struct rq * |
Srivatsa Vaddagiri | 9d5efe0 | 2010-06-08 14:57:02 +1000 | [diff] [blame] | 2972 | find_busiest_queue(struct sched_domain *sd, struct sched_group *group, |
| 2973 | enum cpu_idle_type idle, unsigned long imbalance, |
| 2974 | const struct cpumask *cpus) |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 2975 | { |
| 2976 | struct rq *busiest = NULL, *rq; |
| 2977 | unsigned long max_load = 0; |
| 2978 | int i; |
| 2979 | |
| 2980 | for_each_cpu(i, sched_group_cpus(group)) { |
| 2981 | unsigned long power = power_of(i); |
| 2982 | unsigned long capacity = DIV_ROUND_CLOSEST(power, SCHED_LOAD_SCALE); |
| 2983 | unsigned long wl; |
| 2984 | |
Srivatsa Vaddagiri | 9d5efe0 | 2010-06-08 14:57:02 +1000 | [diff] [blame] | 2985 | if (!capacity) |
| 2986 | capacity = fix_small_capacity(sd, group); |
| 2987 | |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 2988 | if (!cpumask_test_cpu(i, cpus)) |
| 2989 | continue; |
| 2990 | |
| 2991 | rq = cpu_rq(i); |
Thomas Gleixner | 6e40f5b | 2010-02-16 16:48:56 +0100 | [diff] [blame] | 2992 | wl = weighted_cpuload(i); |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 2993 | |
Thomas Gleixner | 6e40f5b | 2010-02-16 16:48:56 +0100 | [diff] [blame] | 2994 | /* |
| 2995 | * When comparing with imbalance, use weighted_cpuload() |
| 2996 | * which is not scaled with the cpu power. |
| 2997 | */ |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 2998 | if (capacity && rq->nr_running == 1 && wl > imbalance) |
| 2999 | continue; |
| 3000 | |
Thomas Gleixner | 6e40f5b | 2010-02-16 16:48:56 +0100 | [diff] [blame] | 3001 | /* |
| 3002 | * For the load comparisons with the other cpu's, consider |
| 3003 | * the weighted_cpuload() scaled with the cpu power, so that |
| 3004 | * the load can be moved away from the cpu that is potentially |
| 3005 | * running at a lower capacity. |
| 3006 | */ |
| 3007 | wl = (wl * SCHED_LOAD_SCALE) / power; |
| 3008 | |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 3009 | if (wl > max_load) { |
| 3010 | max_load = wl; |
| 3011 | busiest = rq; |
| 3012 | } |
| 3013 | } |
| 3014 | |
| 3015 | return busiest; |
| 3016 | } |
| 3017 | |
| 3018 | /* |
| 3019 | * Max backoff if we encounter pinned tasks. Pretty arbitrary value, but |
| 3020 | * so long as it is large enough. |
| 3021 | */ |
| 3022 | #define MAX_PINNED_INTERVAL 512 |
| 3023 | |
| 3024 | /* Working cpumask for load_balance and load_balance_newidle. */ |
| 3025 | static DEFINE_PER_CPU(cpumask_var_t, load_balance_tmpmask); |
| 3026 | |
Michael Neuling | 532cb4c | 2010-06-08 14:57:02 +1000 | [diff] [blame] | 3027 | static int need_active_balance(struct sched_domain *sd, int sd_idle, int idle, |
| 3028 | int busiest_cpu, int this_cpu) |
Peter Zijlstra | 1af3ed3 | 2009-12-23 15:10:31 +0100 | [diff] [blame] | 3029 | { |
| 3030 | if (idle == CPU_NEWLY_IDLE) { |
Michael Neuling | 532cb4c | 2010-06-08 14:57:02 +1000 | [diff] [blame] | 3031 | |
| 3032 | /* |
| 3033 | * ASYM_PACKING needs to force migrate tasks from busy but |
| 3034 | * higher numbered CPUs in order to pack all tasks in the |
| 3035 | * lowest numbered CPUs. |
| 3036 | */ |
| 3037 | if ((sd->flags & SD_ASYM_PACKING) && busiest_cpu > this_cpu) |
| 3038 | return 1; |
| 3039 | |
Peter Zijlstra | 1af3ed3 | 2009-12-23 15:10:31 +0100 | [diff] [blame] | 3040 | /* |
| 3041 | * The only task running in a non-idle cpu can be moved to this |
| 3042 | * cpu in an attempt to completely freeup the other CPU |
| 3043 | * package. |
| 3044 | * |
| 3045 | * The package power saving logic comes from |
| 3046 | * find_busiest_group(). If there are no imbalance, then |
| 3047 | * f_b_g() will return NULL. However when sched_mc={1,2} then |
| 3048 | * f_b_g() will select a group from which a running task may be |
| 3049 | * pulled to this cpu in order to make the other package idle. |
| 3050 | * If there is no opportunity to make a package idle and if |
| 3051 | * there are no imbalance, then f_b_g() will return NULL and no |
| 3052 | * action will be taken in load_balance_newidle(). |
| 3053 | * |
| 3054 | * Under normal task pull operation due to imbalance, there |
| 3055 | * will be more than one task in the source run queue and |
| 3056 | * move_tasks() will succeed. ld_moved will be true and this |
| 3057 | * active balance code will not be triggered. |
| 3058 | */ |
| 3059 | if (!sd_idle && sd->flags & SD_SHARE_CPUPOWER && |
| 3060 | !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE)) |
| 3061 | return 0; |
| 3062 | |
| 3063 | if (sched_mc_power_savings < POWERSAVINGS_BALANCE_WAKEUP) |
| 3064 | return 0; |
| 3065 | } |
| 3066 | |
| 3067 | return unlikely(sd->nr_balance_failed > sd->cache_nice_tries+2); |
| 3068 | } |
| 3069 | |
Tejun Heo | 969c792 | 2010-05-06 18:49:21 +0200 | [diff] [blame] | 3070 | static int active_load_balance_cpu_stop(void *data); |
| 3071 | |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 3072 | /* |
| 3073 | * Check this_cpu to ensure it is balanced within domain. Attempt to move |
| 3074 | * tasks if there is an imbalance. |
| 3075 | */ |
| 3076 | static int load_balance(int this_cpu, struct rq *this_rq, |
| 3077 | struct sched_domain *sd, enum cpu_idle_type idle, |
| 3078 | int *balance) |
| 3079 | { |
| 3080 | int ld_moved, all_pinned = 0, active_balance = 0, sd_idle = 0; |
| 3081 | struct sched_group *group; |
| 3082 | unsigned long imbalance; |
| 3083 | struct rq *busiest; |
| 3084 | unsigned long flags; |
| 3085 | struct cpumask *cpus = __get_cpu_var(load_balance_tmpmask); |
| 3086 | |
| 3087 | cpumask_copy(cpus, cpu_active_mask); |
| 3088 | |
| 3089 | /* |
| 3090 | * When power savings policy is enabled for the parent domain, idle |
| 3091 | * sibling can pick up load irrespective of busy siblings. In this case, |
| 3092 | * let the state of idle sibling percolate up as CPU_IDLE, instead of |
| 3093 | * portraying it as CPU_NOT_IDLE. |
| 3094 | */ |
| 3095 | if (idle != CPU_NOT_IDLE && sd->flags & SD_SHARE_CPUPOWER && |
| 3096 | !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE)) |
| 3097 | sd_idle = 1; |
| 3098 | |
| 3099 | schedstat_inc(sd, lb_count[idle]); |
| 3100 | |
| 3101 | redo: |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 3102 | group = find_busiest_group(sd, this_cpu, &imbalance, idle, &sd_idle, |
| 3103 | cpus, balance); |
| 3104 | |
| 3105 | if (*balance == 0) |
| 3106 | goto out_balanced; |
| 3107 | |
| 3108 | if (!group) { |
| 3109 | schedstat_inc(sd, lb_nobusyg[idle]); |
| 3110 | goto out_balanced; |
| 3111 | } |
| 3112 | |
Srivatsa Vaddagiri | 9d5efe0 | 2010-06-08 14:57:02 +1000 | [diff] [blame] | 3113 | busiest = find_busiest_queue(sd, group, idle, imbalance, cpus); |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 3114 | if (!busiest) { |
| 3115 | schedstat_inc(sd, lb_nobusyq[idle]); |
| 3116 | goto out_balanced; |
| 3117 | } |
| 3118 | |
| 3119 | BUG_ON(busiest == this_rq); |
| 3120 | |
| 3121 | schedstat_add(sd, lb_imbalance[idle], imbalance); |
| 3122 | |
| 3123 | ld_moved = 0; |
| 3124 | if (busiest->nr_running > 1) { |
| 3125 | /* |
| 3126 | * Attempt to move tasks. If find_busiest_group has found |
| 3127 | * an imbalance but busiest->nr_running <= 1, the group is |
| 3128 | * still unbalanced. ld_moved simply stays zero, so it is |
| 3129 | * correctly treated as an imbalance. |
| 3130 | */ |
| 3131 | local_irq_save(flags); |
| 3132 | double_rq_lock(this_rq, busiest); |
| 3133 | ld_moved = move_tasks(this_rq, this_cpu, busiest, |
| 3134 | imbalance, sd, idle, &all_pinned); |
| 3135 | double_rq_unlock(this_rq, busiest); |
| 3136 | local_irq_restore(flags); |
| 3137 | |
| 3138 | /* |
| 3139 | * some other cpu did the load balance for us. |
| 3140 | */ |
| 3141 | if (ld_moved && this_cpu != smp_processor_id()) |
| 3142 | resched_cpu(this_cpu); |
| 3143 | |
| 3144 | /* All tasks on this runqueue were pinned by CPU affinity */ |
| 3145 | if (unlikely(all_pinned)) { |
| 3146 | cpumask_clear_cpu(cpu_of(busiest), cpus); |
| 3147 | if (!cpumask_empty(cpus)) |
| 3148 | goto redo; |
| 3149 | goto out_balanced; |
| 3150 | } |
| 3151 | } |
| 3152 | |
| 3153 | if (!ld_moved) { |
| 3154 | schedstat_inc(sd, lb_failed[idle]); |
Venkatesh Pallipadi | 58b26c4 | 2010-09-10 18:19:17 -0700 | [diff] [blame] | 3155 | /* |
| 3156 | * Increment the failure counter only on periodic balance. |
| 3157 | * We do not want newidle balance, which can be very |
| 3158 | * frequent, pollute the failure counter causing |
| 3159 | * excessive cache_hot migrations and active balances. |
| 3160 | */ |
| 3161 | if (idle != CPU_NEWLY_IDLE) |
| 3162 | sd->nr_balance_failed++; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 3163 | |
Michael Neuling | 532cb4c | 2010-06-08 14:57:02 +1000 | [diff] [blame] | 3164 | if (need_active_balance(sd, sd_idle, idle, cpu_of(busiest), |
| 3165 | this_cpu)) { |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 3166 | raw_spin_lock_irqsave(&busiest->lock, flags); |
| 3167 | |
Tejun Heo | 969c792 | 2010-05-06 18:49:21 +0200 | [diff] [blame] | 3168 | /* don't kick the active_load_balance_cpu_stop, |
| 3169 | * if the curr task on busiest cpu can't be |
| 3170 | * moved to this_cpu |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 3171 | */ |
| 3172 | if (!cpumask_test_cpu(this_cpu, |
| 3173 | &busiest->curr->cpus_allowed)) { |
| 3174 | raw_spin_unlock_irqrestore(&busiest->lock, |
| 3175 | flags); |
| 3176 | all_pinned = 1; |
| 3177 | goto out_one_pinned; |
| 3178 | } |
| 3179 | |
Tejun Heo | 969c792 | 2010-05-06 18:49:21 +0200 | [diff] [blame] | 3180 | /* |
| 3181 | * ->active_balance synchronizes accesses to |
| 3182 | * ->active_balance_work. Once set, it's cleared |
| 3183 | * only after active load balance is finished. |
| 3184 | */ |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 3185 | if (!busiest->active_balance) { |
| 3186 | busiest->active_balance = 1; |
| 3187 | busiest->push_cpu = this_cpu; |
| 3188 | active_balance = 1; |
| 3189 | } |
| 3190 | raw_spin_unlock_irqrestore(&busiest->lock, flags); |
Tejun Heo | 969c792 | 2010-05-06 18:49:21 +0200 | [diff] [blame] | 3191 | |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 3192 | if (active_balance) |
Tejun Heo | 969c792 | 2010-05-06 18:49:21 +0200 | [diff] [blame] | 3193 | stop_one_cpu_nowait(cpu_of(busiest), |
| 3194 | active_load_balance_cpu_stop, busiest, |
| 3195 | &busiest->active_balance_work); |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 3196 | |
| 3197 | /* |
| 3198 | * We've kicked active balancing, reset the failure |
| 3199 | * counter. |
| 3200 | */ |
| 3201 | sd->nr_balance_failed = sd->cache_nice_tries+1; |
| 3202 | } |
| 3203 | } else |
| 3204 | sd->nr_balance_failed = 0; |
| 3205 | |
| 3206 | if (likely(!active_balance)) { |
| 3207 | /* We were unbalanced, so reset the balancing interval */ |
| 3208 | sd->balance_interval = sd->min_interval; |
| 3209 | } else { |
| 3210 | /* |
| 3211 | * If we've begun active balancing, start to back off. This |
| 3212 | * case may not be covered by the all_pinned logic if there |
| 3213 | * is only 1 task on the busy runqueue (because we don't call |
| 3214 | * move_tasks). |
| 3215 | */ |
| 3216 | if (sd->balance_interval < sd->max_interval) |
| 3217 | sd->balance_interval *= 2; |
| 3218 | } |
| 3219 | |
| 3220 | if (!ld_moved && !sd_idle && sd->flags & SD_SHARE_CPUPOWER && |
| 3221 | !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE)) |
| 3222 | ld_moved = -1; |
| 3223 | |
| 3224 | goto out; |
| 3225 | |
| 3226 | out_balanced: |
| 3227 | schedstat_inc(sd, lb_balanced[idle]); |
| 3228 | |
| 3229 | sd->nr_balance_failed = 0; |
| 3230 | |
| 3231 | out_one_pinned: |
| 3232 | /* tune up the balancing interval */ |
| 3233 | if ((all_pinned && sd->balance_interval < MAX_PINNED_INTERVAL) || |
| 3234 | (sd->balance_interval < sd->max_interval)) |
| 3235 | sd->balance_interval *= 2; |
| 3236 | |
| 3237 | if (!sd_idle && sd->flags & SD_SHARE_CPUPOWER && |
| 3238 | !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE)) |
| 3239 | ld_moved = -1; |
| 3240 | else |
| 3241 | ld_moved = 0; |
| 3242 | out: |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 3243 | return ld_moved; |
| 3244 | } |
| 3245 | |
| 3246 | /* |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 3247 | * idle_balance is called by schedule() if this_cpu is about to become |
| 3248 | * idle. Attempts to pull tasks from other CPUs. |
| 3249 | */ |
| 3250 | static void idle_balance(int this_cpu, struct rq *this_rq) |
| 3251 | { |
| 3252 | struct sched_domain *sd; |
| 3253 | int pulled_task = 0; |
| 3254 | unsigned long next_balance = jiffies + HZ; |
| 3255 | |
| 3256 | this_rq->idle_stamp = this_rq->clock; |
| 3257 | |
| 3258 | if (this_rq->avg_idle < sysctl_sched_migration_cost) |
| 3259 | return; |
| 3260 | |
Peter Zijlstra | f492e12 | 2009-12-23 15:29:42 +0100 | [diff] [blame] | 3261 | /* |
| 3262 | * Drop the rq->lock, but keep IRQ/preempt disabled. |
| 3263 | */ |
| 3264 | raw_spin_unlock(&this_rq->lock); |
| 3265 | |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 3266 | for_each_domain(this_cpu, sd) { |
| 3267 | unsigned long interval; |
Peter Zijlstra | f492e12 | 2009-12-23 15:29:42 +0100 | [diff] [blame] | 3268 | int balance = 1; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 3269 | |
| 3270 | if (!(sd->flags & SD_LOAD_BALANCE)) |
| 3271 | continue; |
| 3272 | |
Peter Zijlstra | f492e12 | 2009-12-23 15:29:42 +0100 | [diff] [blame] | 3273 | if (sd->flags & SD_BALANCE_NEWIDLE) { |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 3274 | /* If we've pulled tasks over stop searching: */ |
Peter Zijlstra | f492e12 | 2009-12-23 15:29:42 +0100 | [diff] [blame] | 3275 | pulled_task = load_balance(this_cpu, this_rq, |
| 3276 | sd, CPU_NEWLY_IDLE, &balance); |
| 3277 | } |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 3278 | |
| 3279 | interval = msecs_to_jiffies(sd->balance_interval); |
| 3280 | if (time_after(next_balance, sd->last_balance + interval)) |
| 3281 | next_balance = sd->last_balance + interval; |
Nikhil Rao | fab4762 | 2010-10-15 13:12:29 -0700 | [diff] [blame] | 3282 | if (pulled_task) |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 3283 | break; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 3284 | } |
Peter Zijlstra | f492e12 | 2009-12-23 15:29:42 +0100 | [diff] [blame] | 3285 | |
| 3286 | raw_spin_lock(&this_rq->lock); |
| 3287 | |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 3288 | if (pulled_task || time_after(jiffies, this_rq->next_balance)) { |
| 3289 | /* |
| 3290 | * We are going idle. next_balance may be set based on |
| 3291 | * a busy processor. So reset next_balance. |
| 3292 | */ |
| 3293 | this_rq->next_balance = next_balance; |
| 3294 | } |
| 3295 | } |
| 3296 | |
| 3297 | /* |
Tejun Heo | 969c792 | 2010-05-06 18:49:21 +0200 | [diff] [blame] | 3298 | * active_load_balance_cpu_stop is run by cpu stopper. It pushes |
| 3299 | * running tasks off the busiest CPU onto idle CPUs. It requires at |
| 3300 | * least 1 task to be running on each physical CPU where possible, and |
| 3301 | * avoids physical / logical imbalances. |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 3302 | */ |
Tejun Heo | 969c792 | 2010-05-06 18:49:21 +0200 | [diff] [blame] | 3303 | static int active_load_balance_cpu_stop(void *data) |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 3304 | { |
Tejun Heo | 969c792 | 2010-05-06 18:49:21 +0200 | [diff] [blame] | 3305 | struct rq *busiest_rq = data; |
| 3306 | int busiest_cpu = cpu_of(busiest_rq); |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 3307 | int target_cpu = busiest_rq->push_cpu; |
Tejun Heo | 969c792 | 2010-05-06 18:49:21 +0200 | [diff] [blame] | 3308 | struct rq *target_rq = cpu_rq(target_cpu); |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 3309 | struct sched_domain *sd; |
Tejun Heo | 969c792 | 2010-05-06 18:49:21 +0200 | [diff] [blame] | 3310 | |
| 3311 | raw_spin_lock_irq(&busiest_rq->lock); |
| 3312 | |
| 3313 | /* make sure the requested cpu hasn't gone down in the meantime */ |
| 3314 | if (unlikely(busiest_cpu != smp_processor_id() || |
| 3315 | !busiest_rq->active_balance)) |
| 3316 | goto out_unlock; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 3317 | |
| 3318 | /* Is there any task to move? */ |
| 3319 | if (busiest_rq->nr_running <= 1) |
Tejun Heo | 969c792 | 2010-05-06 18:49:21 +0200 | [diff] [blame] | 3320 | goto out_unlock; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 3321 | |
| 3322 | /* |
| 3323 | * This condition is "impossible", if it occurs |
| 3324 | * we need to fix it. Originally reported by |
| 3325 | * Bjorn Helgaas on a 128-cpu setup. |
| 3326 | */ |
| 3327 | BUG_ON(busiest_rq == target_rq); |
| 3328 | |
| 3329 | /* move a task from busiest_rq to target_rq */ |
| 3330 | double_lock_balance(busiest_rq, target_rq); |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 3331 | |
| 3332 | /* Search for an sd spanning us and the target CPU. */ |
| 3333 | for_each_domain(target_cpu, sd) { |
| 3334 | if ((sd->flags & SD_LOAD_BALANCE) && |
| 3335 | cpumask_test_cpu(busiest_cpu, sched_domain_span(sd))) |
| 3336 | break; |
| 3337 | } |
| 3338 | |
| 3339 | if (likely(sd)) { |
| 3340 | schedstat_inc(sd, alb_count); |
| 3341 | |
| 3342 | if (move_one_task(target_rq, target_cpu, busiest_rq, |
| 3343 | sd, CPU_IDLE)) |
| 3344 | schedstat_inc(sd, alb_pushed); |
| 3345 | else |
| 3346 | schedstat_inc(sd, alb_failed); |
| 3347 | } |
| 3348 | double_unlock_balance(busiest_rq, target_rq); |
Tejun Heo | 969c792 | 2010-05-06 18:49:21 +0200 | [diff] [blame] | 3349 | out_unlock: |
| 3350 | busiest_rq->active_balance = 0; |
| 3351 | raw_spin_unlock_irq(&busiest_rq->lock); |
| 3352 | return 0; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 3353 | } |
| 3354 | |
| 3355 | #ifdef CONFIG_NO_HZ |
Venkatesh Pallipadi | 83cd4fe | 2010-05-21 17:09:41 -0700 | [diff] [blame] | 3356 | |
| 3357 | static DEFINE_PER_CPU(struct call_single_data, remote_sched_softirq_cb); |
| 3358 | |
| 3359 | static void trigger_sched_softirq(void *data) |
| 3360 | { |
| 3361 | raise_softirq_irqoff(SCHED_SOFTIRQ); |
| 3362 | } |
| 3363 | |
| 3364 | static inline void init_sched_softirq_csd(struct call_single_data *csd) |
| 3365 | { |
| 3366 | csd->func = trigger_sched_softirq; |
| 3367 | csd->info = NULL; |
| 3368 | csd->flags = 0; |
| 3369 | csd->priv = 0; |
| 3370 | } |
| 3371 | |
| 3372 | /* |
| 3373 | * idle load balancing details |
| 3374 | * - One of the idle CPUs nominates itself as idle load_balancer, while |
| 3375 | * entering idle. |
| 3376 | * - This idle load balancer CPU will also go into tickless mode when |
| 3377 | * it is idle, just like all other idle CPUs |
| 3378 | * - When one of the busy CPUs notice that there may be an idle rebalancing |
| 3379 | * needed, they will kick the idle load balancer, which then does idle |
| 3380 | * load balancing for all the idle CPUs. |
| 3381 | */ |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 3382 | static struct { |
| 3383 | atomic_t load_balancer; |
Venkatesh Pallipadi | 83cd4fe | 2010-05-21 17:09:41 -0700 | [diff] [blame] | 3384 | atomic_t first_pick_cpu; |
| 3385 | atomic_t second_pick_cpu; |
| 3386 | cpumask_var_t idle_cpus_mask; |
| 3387 | cpumask_var_t grp_idle_mask; |
| 3388 | unsigned long next_balance; /* in jiffy units */ |
| 3389 | } nohz ____cacheline_aligned; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 3390 | |
| 3391 | int get_nohz_load_balancer(void) |
| 3392 | { |
| 3393 | return atomic_read(&nohz.load_balancer); |
| 3394 | } |
| 3395 | |
| 3396 | #if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT) |
| 3397 | /** |
| 3398 | * lowest_flag_domain - Return lowest sched_domain containing flag. |
| 3399 | * @cpu: The cpu whose lowest level of sched domain is to |
| 3400 | * be returned. |
| 3401 | * @flag: The flag to check for the lowest sched_domain |
| 3402 | * for the given cpu. |
| 3403 | * |
| 3404 | * Returns the lowest sched_domain of a cpu which contains the given flag. |
| 3405 | */ |
| 3406 | static inline struct sched_domain *lowest_flag_domain(int cpu, int flag) |
| 3407 | { |
| 3408 | struct sched_domain *sd; |
| 3409 | |
| 3410 | for_each_domain(cpu, sd) |
| 3411 | if (sd && (sd->flags & flag)) |
| 3412 | break; |
| 3413 | |
| 3414 | return sd; |
| 3415 | } |
| 3416 | |
| 3417 | /** |
| 3418 | * for_each_flag_domain - Iterates over sched_domains containing the flag. |
| 3419 | * @cpu: The cpu whose domains we're iterating over. |
| 3420 | * @sd: variable holding the value of the power_savings_sd |
| 3421 | * for cpu. |
| 3422 | * @flag: The flag to filter the sched_domains to be iterated. |
| 3423 | * |
| 3424 | * Iterates over all the scheduler domains for a given cpu that has the 'flag' |
| 3425 | * set, starting from the lowest sched_domain to the highest. |
| 3426 | */ |
| 3427 | #define for_each_flag_domain(cpu, sd, flag) \ |
| 3428 | for (sd = lowest_flag_domain(cpu, flag); \ |
| 3429 | (sd && (sd->flags & flag)); sd = sd->parent) |
| 3430 | |
| 3431 | /** |
| 3432 | * is_semi_idle_group - Checks if the given sched_group is semi-idle. |
| 3433 | * @ilb_group: group to be checked for semi-idleness |
| 3434 | * |
| 3435 | * Returns: 1 if the group is semi-idle. 0 otherwise. |
| 3436 | * |
| 3437 | * We define a sched_group to be semi idle if it has atleast one idle-CPU |
| 3438 | * and atleast one non-idle CPU. This helper function checks if the given |
| 3439 | * sched_group is semi-idle or not. |
| 3440 | */ |
| 3441 | static inline int is_semi_idle_group(struct sched_group *ilb_group) |
| 3442 | { |
Venkatesh Pallipadi | 83cd4fe | 2010-05-21 17:09:41 -0700 | [diff] [blame] | 3443 | cpumask_and(nohz.grp_idle_mask, nohz.idle_cpus_mask, |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 3444 | sched_group_cpus(ilb_group)); |
| 3445 | |
| 3446 | /* |
| 3447 | * A sched_group is semi-idle when it has atleast one busy cpu |
| 3448 | * and atleast one idle cpu. |
| 3449 | */ |
Venkatesh Pallipadi | 83cd4fe | 2010-05-21 17:09:41 -0700 | [diff] [blame] | 3450 | if (cpumask_empty(nohz.grp_idle_mask)) |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 3451 | return 0; |
| 3452 | |
Venkatesh Pallipadi | 83cd4fe | 2010-05-21 17:09:41 -0700 | [diff] [blame] | 3453 | if (cpumask_equal(nohz.grp_idle_mask, sched_group_cpus(ilb_group))) |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 3454 | return 0; |
| 3455 | |
| 3456 | return 1; |
| 3457 | } |
| 3458 | /** |
| 3459 | * find_new_ilb - Finds the optimum idle load balancer for nomination. |
| 3460 | * @cpu: The cpu which is nominating a new idle_load_balancer. |
| 3461 | * |
| 3462 | * Returns: Returns the id of the idle load balancer if it exists, |
| 3463 | * Else, returns >= nr_cpu_ids. |
| 3464 | * |
| 3465 | * This algorithm picks the idle load balancer such that it belongs to a |
| 3466 | * semi-idle powersavings sched_domain. The idea is to try and avoid |
| 3467 | * completely idle packages/cores just for the purpose of idle load balancing |
| 3468 | * when there are other idle cpu's which are better suited for that job. |
| 3469 | */ |
| 3470 | static int find_new_ilb(int cpu) |
| 3471 | { |
| 3472 | struct sched_domain *sd; |
| 3473 | struct sched_group *ilb_group; |
| 3474 | |
| 3475 | /* |
| 3476 | * Have idle load balancer selection from semi-idle packages only |
| 3477 | * when power-aware load balancing is enabled |
| 3478 | */ |
| 3479 | if (!(sched_smt_power_savings || sched_mc_power_savings)) |
| 3480 | goto out_done; |
| 3481 | |
| 3482 | /* |
| 3483 | * Optimize for the case when we have no idle CPUs or only one |
| 3484 | * idle CPU. Don't walk the sched_domain hierarchy in such cases |
| 3485 | */ |
Venkatesh Pallipadi | 83cd4fe | 2010-05-21 17:09:41 -0700 | [diff] [blame] | 3486 | if (cpumask_weight(nohz.idle_cpus_mask) < 2) |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 3487 | goto out_done; |
| 3488 | |
| 3489 | for_each_flag_domain(cpu, sd, SD_POWERSAVINGS_BALANCE) { |
| 3490 | ilb_group = sd->groups; |
| 3491 | |
| 3492 | do { |
| 3493 | if (is_semi_idle_group(ilb_group)) |
Venkatesh Pallipadi | 83cd4fe | 2010-05-21 17:09:41 -0700 | [diff] [blame] | 3494 | return cpumask_first(nohz.grp_idle_mask); |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 3495 | |
| 3496 | ilb_group = ilb_group->next; |
| 3497 | |
| 3498 | } while (ilb_group != sd->groups); |
| 3499 | } |
| 3500 | |
| 3501 | out_done: |
Venkatesh Pallipadi | 83cd4fe | 2010-05-21 17:09:41 -0700 | [diff] [blame] | 3502 | return nr_cpu_ids; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 3503 | } |
| 3504 | #else /* (CONFIG_SCHED_MC || CONFIG_SCHED_SMT) */ |
| 3505 | static inline int find_new_ilb(int call_cpu) |
| 3506 | { |
Venkatesh Pallipadi | 83cd4fe | 2010-05-21 17:09:41 -0700 | [diff] [blame] | 3507 | return nr_cpu_ids; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 3508 | } |
| 3509 | #endif |
| 3510 | |
| 3511 | /* |
Venkatesh Pallipadi | 83cd4fe | 2010-05-21 17:09:41 -0700 | [diff] [blame] | 3512 | * Kick a CPU to do the nohz balancing, if it is time for it. We pick the |
| 3513 | * nohz_load_balancer CPU (if there is one) otherwise fallback to any idle |
| 3514 | * CPU (if there is one). |
| 3515 | */ |
| 3516 | static void nohz_balancer_kick(int cpu) |
| 3517 | { |
| 3518 | int ilb_cpu; |
| 3519 | |
| 3520 | nohz.next_balance++; |
| 3521 | |
| 3522 | ilb_cpu = get_nohz_load_balancer(); |
| 3523 | |
| 3524 | if (ilb_cpu >= nr_cpu_ids) { |
| 3525 | ilb_cpu = cpumask_first(nohz.idle_cpus_mask); |
| 3526 | if (ilb_cpu >= nr_cpu_ids) |
| 3527 | return; |
| 3528 | } |
| 3529 | |
| 3530 | if (!cpu_rq(ilb_cpu)->nohz_balance_kick) { |
| 3531 | struct call_single_data *cp; |
| 3532 | |
| 3533 | cpu_rq(ilb_cpu)->nohz_balance_kick = 1; |
| 3534 | cp = &per_cpu(remote_sched_softirq_cb, cpu); |
| 3535 | __smp_call_function_single(ilb_cpu, cp, 0); |
| 3536 | } |
| 3537 | return; |
| 3538 | } |
| 3539 | |
| 3540 | /* |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 3541 | * This routine will try to nominate the ilb (idle load balancing) |
| 3542 | * owner among the cpus whose ticks are stopped. ilb owner will do the idle |
Venkatesh Pallipadi | 83cd4fe | 2010-05-21 17:09:41 -0700 | [diff] [blame] | 3543 | * load balancing on behalf of all those cpus. |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 3544 | * |
Venkatesh Pallipadi | 83cd4fe | 2010-05-21 17:09:41 -0700 | [diff] [blame] | 3545 | * When the ilb owner becomes busy, we will not have new ilb owner until some |
| 3546 | * idle CPU wakes up and goes back to idle or some busy CPU tries to kick |
| 3547 | * idle load balancing by kicking one of the idle CPUs. |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 3548 | * |
Venkatesh Pallipadi | 83cd4fe | 2010-05-21 17:09:41 -0700 | [diff] [blame] | 3549 | * Ticks are stopped for the ilb owner as well, with busy CPU kicking this |
| 3550 | * ilb owner CPU in future (when there is a need for idle load balancing on |
| 3551 | * behalf of all idle CPUs). |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 3552 | */ |
Venkatesh Pallipadi | 83cd4fe | 2010-05-21 17:09:41 -0700 | [diff] [blame] | 3553 | void select_nohz_load_balancer(int stop_tick) |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 3554 | { |
| 3555 | int cpu = smp_processor_id(); |
| 3556 | |
| 3557 | if (stop_tick) { |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 3558 | if (!cpu_active(cpu)) { |
| 3559 | if (atomic_read(&nohz.load_balancer) != cpu) |
Venkatesh Pallipadi | 83cd4fe | 2010-05-21 17:09:41 -0700 | [diff] [blame] | 3560 | return; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 3561 | |
| 3562 | /* |
| 3563 | * If we are going offline and still the leader, |
| 3564 | * give up! |
| 3565 | */ |
Venkatesh Pallipadi | 83cd4fe | 2010-05-21 17:09:41 -0700 | [diff] [blame] | 3566 | if (atomic_cmpxchg(&nohz.load_balancer, cpu, |
| 3567 | nr_cpu_ids) != cpu) |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 3568 | BUG(); |
| 3569 | |
Venkatesh Pallipadi | 83cd4fe | 2010-05-21 17:09:41 -0700 | [diff] [blame] | 3570 | return; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 3571 | } |
| 3572 | |
Venkatesh Pallipadi | 83cd4fe | 2010-05-21 17:09:41 -0700 | [diff] [blame] | 3573 | cpumask_set_cpu(cpu, nohz.idle_cpus_mask); |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 3574 | |
Venkatesh Pallipadi | 83cd4fe | 2010-05-21 17:09:41 -0700 | [diff] [blame] | 3575 | if (atomic_read(&nohz.first_pick_cpu) == cpu) |
| 3576 | atomic_cmpxchg(&nohz.first_pick_cpu, cpu, nr_cpu_ids); |
| 3577 | if (atomic_read(&nohz.second_pick_cpu) == cpu) |
| 3578 | atomic_cmpxchg(&nohz.second_pick_cpu, cpu, nr_cpu_ids); |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 3579 | |
Venkatesh Pallipadi | 83cd4fe | 2010-05-21 17:09:41 -0700 | [diff] [blame] | 3580 | if (atomic_read(&nohz.load_balancer) >= nr_cpu_ids) { |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 3581 | int new_ilb; |
| 3582 | |
Venkatesh Pallipadi | 83cd4fe | 2010-05-21 17:09:41 -0700 | [diff] [blame] | 3583 | /* make me the ilb owner */ |
| 3584 | if (atomic_cmpxchg(&nohz.load_balancer, nr_cpu_ids, |
| 3585 | cpu) != nr_cpu_ids) |
| 3586 | return; |
| 3587 | |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 3588 | /* |
| 3589 | * Check to see if there is a more power-efficient |
| 3590 | * ilb. |
| 3591 | */ |
| 3592 | new_ilb = find_new_ilb(cpu); |
| 3593 | if (new_ilb < nr_cpu_ids && new_ilb != cpu) { |
Venkatesh Pallipadi | 83cd4fe | 2010-05-21 17:09:41 -0700 | [diff] [blame] | 3594 | atomic_set(&nohz.load_balancer, nr_cpu_ids); |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 3595 | resched_cpu(new_ilb); |
Venkatesh Pallipadi | 83cd4fe | 2010-05-21 17:09:41 -0700 | [diff] [blame] | 3596 | return; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 3597 | } |
Venkatesh Pallipadi | 83cd4fe | 2010-05-21 17:09:41 -0700 | [diff] [blame] | 3598 | return; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 3599 | } |
| 3600 | } else { |
Venkatesh Pallipadi | 83cd4fe | 2010-05-21 17:09:41 -0700 | [diff] [blame] | 3601 | if (!cpumask_test_cpu(cpu, nohz.idle_cpus_mask)) |
| 3602 | return; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 3603 | |
Venkatesh Pallipadi | 83cd4fe | 2010-05-21 17:09:41 -0700 | [diff] [blame] | 3604 | cpumask_clear_cpu(cpu, nohz.idle_cpus_mask); |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 3605 | |
| 3606 | if (atomic_read(&nohz.load_balancer) == cpu) |
Venkatesh Pallipadi | 83cd4fe | 2010-05-21 17:09:41 -0700 | [diff] [blame] | 3607 | if (atomic_cmpxchg(&nohz.load_balancer, cpu, |
| 3608 | nr_cpu_ids) != cpu) |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 3609 | BUG(); |
| 3610 | } |
Venkatesh Pallipadi | 83cd4fe | 2010-05-21 17:09:41 -0700 | [diff] [blame] | 3611 | return; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 3612 | } |
| 3613 | #endif |
| 3614 | |
| 3615 | static DEFINE_SPINLOCK(balancing); |
| 3616 | |
| 3617 | /* |
| 3618 | * It checks each scheduling domain to see if it is due to be balanced, |
| 3619 | * and initiates a balancing operation if so. |
| 3620 | * |
| 3621 | * Balancing parameters are set up in arch_init_sched_domains. |
| 3622 | */ |
| 3623 | static void rebalance_domains(int cpu, enum cpu_idle_type idle) |
| 3624 | { |
| 3625 | int balance = 1; |
| 3626 | struct rq *rq = cpu_rq(cpu); |
| 3627 | unsigned long interval; |
| 3628 | struct sched_domain *sd; |
| 3629 | /* Earliest time when we have to do rebalance again */ |
| 3630 | unsigned long next_balance = jiffies + 60*HZ; |
| 3631 | int update_next_balance = 0; |
| 3632 | int need_serialize; |
| 3633 | |
Peter Zijlstra | 2069dd7 | 2010-11-15 15:47:00 -0800 | [diff] [blame] | 3634 | update_shares(cpu); |
| 3635 | |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 3636 | for_each_domain(cpu, sd) { |
| 3637 | if (!(sd->flags & SD_LOAD_BALANCE)) |
| 3638 | continue; |
| 3639 | |
| 3640 | interval = sd->balance_interval; |
| 3641 | if (idle != CPU_IDLE) |
| 3642 | interval *= sd->busy_factor; |
| 3643 | |
| 3644 | /* scale ms to jiffies */ |
| 3645 | interval = msecs_to_jiffies(interval); |
| 3646 | if (unlikely(!interval)) |
| 3647 | interval = 1; |
| 3648 | if (interval > HZ*NR_CPUS/10) |
| 3649 | interval = HZ*NR_CPUS/10; |
| 3650 | |
| 3651 | need_serialize = sd->flags & SD_SERIALIZE; |
| 3652 | |
| 3653 | if (need_serialize) { |
| 3654 | if (!spin_trylock(&balancing)) |
| 3655 | goto out; |
| 3656 | } |
| 3657 | |
| 3658 | if (time_after_eq(jiffies, sd->last_balance + interval)) { |
| 3659 | if (load_balance(cpu, rq, sd, idle, &balance)) { |
| 3660 | /* |
| 3661 | * We've pulled tasks over so either we're no |
| 3662 | * longer idle, or one of our SMT siblings is |
| 3663 | * not idle. |
| 3664 | */ |
| 3665 | idle = CPU_NOT_IDLE; |
| 3666 | } |
| 3667 | sd->last_balance = jiffies; |
| 3668 | } |
| 3669 | if (need_serialize) |
| 3670 | spin_unlock(&balancing); |
| 3671 | out: |
| 3672 | if (time_after(next_balance, sd->last_balance + interval)) { |
| 3673 | next_balance = sd->last_balance + interval; |
| 3674 | update_next_balance = 1; |
| 3675 | } |
| 3676 | |
| 3677 | /* |
| 3678 | * Stop the load balance at this level. There is another |
| 3679 | * CPU in our sched group which is doing load balancing more |
| 3680 | * actively. |
| 3681 | */ |
| 3682 | if (!balance) |
| 3683 | break; |
| 3684 | } |
| 3685 | |
| 3686 | /* |
| 3687 | * next_balance will be updated only when there is a need. |
| 3688 | * When the cpu is attached to null domain for ex, it will not be |
| 3689 | * updated. |
| 3690 | */ |
| 3691 | if (likely(update_next_balance)) |
| 3692 | rq->next_balance = next_balance; |
| 3693 | } |
| 3694 | |
Venkatesh Pallipadi | 83cd4fe | 2010-05-21 17:09:41 -0700 | [diff] [blame] | 3695 | #ifdef CONFIG_NO_HZ |
| 3696 | /* |
| 3697 | * In CONFIG_NO_HZ case, the idle balance kickee will do the |
| 3698 | * rebalancing for all the cpus for whom scheduler ticks are stopped. |
| 3699 | */ |
| 3700 | static void nohz_idle_balance(int this_cpu, enum cpu_idle_type idle) |
| 3701 | { |
| 3702 | struct rq *this_rq = cpu_rq(this_cpu); |
| 3703 | struct rq *rq; |
| 3704 | int balance_cpu; |
| 3705 | |
| 3706 | if (idle != CPU_IDLE || !this_rq->nohz_balance_kick) |
| 3707 | return; |
| 3708 | |
| 3709 | for_each_cpu(balance_cpu, nohz.idle_cpus_mask) { |
| 3710 | if (balance_cpu == this_cpu) |
| 3711 | continue; |
| 3712 | |
| 3713 | /* |
| 3714 | * If this cpu gets work to do, stop the load balancing |
| 3715 | * work being done for other cpus. Next load |
| 3716 | * balancing owner will pick it up. |
| 3717 | */ |
| 3718 | if (need_resched()) { |
| 3719 | this_rq->nohz_balance_kick = 0; |
| 3720 | break; |
| 3721 | } |
| 3722 | |
| 3723 | raw_spin_lock_irq(&this_rq->lock); |
Suresh Siddha | 5343bdb | 2010-07-09 15:19:54 +0200 | [diff] [blame] | 3724 | update_rq_clock(this_rq); |
Venkatesh Pallipadi | 83cd4fe | 2010-05-21 17:09:41 -0700 | [diff] [blame] | 3725 | update_cpu_load(this_rq); |
| 3726 | raw_spin_unlock_irq(&this_rq->lock); |
| 3727 | |
| 3728 | rebalance_domains(balance_cpu, CPU_IDLE); |
| 3729 | |
| 3730 | rq = cpu_rq(balance_cpu); |
| 3731 | if (time_after(this_rq->next_balance, rq->next_balance)) |
| 3732 | this_rq->next_balance = rq->next_balance; |
| 3733 | } |
| 3734 | nohz.next_balance = this_rq->next_balance; |
| 3735 | this_rq->nohz_balance_kick = 0; |
| 3736 | } |
| 3737 | |
| 3738 | /* |
| 3739 | * Current heuristic for kicking the idle load balancer |
| 3740 | * - first_pick_cpu is the one of the busy CPUs. It will kick |
| 3741 | * idle load balancer when it has more than one process active. This |
| 3742 | * eliminates the need for idle load balancing altogether when we have |
| 3743 | * only one running process in the system (common case). |
| 3744 | * - If there are more than one busy CPU, idle load balancer may have |
| 3745 | * to run for active_load_balance to happen (i.e., two busy CPUs are |
| 3746 | * SMT or core siblings and can run better if they move to different |
| 3747 | * physical CPUs). So, second_pick_cpu is the second of the busy CPUs |
| 3748 | * which will kick idle load balancer as soon as it has any load. |
| 3749 | */ |
| 3750 | static inline int nohz_kick_needed(struct rq *rq, int cpu) |
| 3751 | { |
| 3752 | unsigned long now = jiffies; |
| 3753 | int ret; |
| 3754 | int first_pick_cpu, second_pick_cpu; |
| 3755 | |
| 3756 | if (time_before(now, nohz.next_balance)) |
| 3757 | return 0; |
| 3758 | |
Suresh Siddha | f6c3f16 | 2010-09-13 11:02:21 -0700 | [diff] [blame] | 3759 | if (rq->idle_at_tick) |
Venkatesh Pallipadi | 83cd4fe | 2010-05-21 17:09:41 -0700 | [diff] [blame] | 3760 | return 0; |
| 3761 | |
| 3762 | first_pick_cpu = atomic_read(&nohz.first_pick_cpu); |
| 3763 | second_pick_cpu = atomic_read(&nohz.second_pick_cpu); |
| 3764 | |
| 3765 | if (first_pick_cpu < nr_cpu_ids && first_pick_cpu != cpu && |
| 3766 | second_pick_cpu < nr_cpu_ids && second_pick_cpu != cpu) |
| 3767 | return 0; |
| 3768 | |
| 3769 | ret = atomic_cmpxchg(&nohz.first_pick_cpu, nr_cpu_ids, cpu); |
| 3770 | if (ret == nr_cpu_ids || ret == cpu) { |
| 3771 | atomic_cmpxchg(&nohz.second_pick_cpu, cpu, nr_cpu_ids); |
| 3772 | if (rq->nr_running > 1) |
| 3773 | return 1; |
| 3774 | } else { |
| 3775 | ret = atomic_cmpxchg(&nohz.second_pick_cpu, nr_cpu_ids, cpu); |
| 3776 | if (ret == nr_cpu_ids || ret == cpu) { |
| 3777 | if (rq->nr_running) |
| 3778 | return 1; |
| 3779 | } |
| 3780 | } |
| 3781 | return 0; |
| 3782 | } |
| 3783 | #else |
| 3784 | static void nohz_idle_balance(int this_cpu, enum cpu_idle_type idle) { } |
| 3785 | #endif |
| 3786 | |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 3787 | /* |
| 3788 | * run_rebalance_domains is triggered when needed from the scheduler tick. |
Venkatesh Pallipadi | 83cd4fe | 2010-05-21 17:09:41 -0700 | [diff] [blame] | 3789 | * Also triggered for nohz idle balancing (with nohz_balancing_kick set). |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 3790 | */ |
| 3791 | static void run_rebalance_domains(struct softirq_action *h) |
| 3792 | { |
| 3793 | int this_cpu = smp_processor_id(); |
| 3794 | struct rq *this_rq = cpu_rq(this_cpu); |
| 3795 | enum cpu_idle_type idle = this_rq->idle_at_tick ? |
| 3796 | CPU_IDLE : CPU_NOT_IDLE; |
| 3797 | |
| 3798 | rebalance_domains(this_cpu, idle); |
| 3799 | |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 3800 | /* |
Venkatesh Pallipadi | 83cd4fe | 2010-05-21 17:09:41 -0700 | [diff] [blame] | 3801 | * If this cpu has a pending nohz_balance_kick, then do the |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 3802 | * balancing on behalf of the other idle cpus whose ticks are |
| 3803 | * stopped. |
| 3804 | */ |
Venkatesh Pallipadi | 83cd4fe | 2010-05-21 17:09:41 -0700 | [diff] [blame] | 3805 | nohz_idle_balance(this_cpu, idle); |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 3806 | } |
| 3807 | |
| 3808 | static inline int on_null_domain(int cpu) |
| 3809 | { |
Paul E. McKenney | 90a6501 | 2010-02-28 08:32:18 -0800 | [diff] [blame] | 3810 | return !rcu_dereference_sched(cpu_rq(cpu)->sd); |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 3811 | } |
| 3812 | |
| 3813 | /* |
| 3814 | * Trigger the SCHED_SOFTIRQ if it is time to do periodic load balancing. |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 3815 | */ |
| 3816 | static inline void trigger_load_balance(struct rq *rq, int cpu) |
| 3817 | { |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 3818 | /* Don't need to rebalance while attached to NULL domain */ |
| 3819 | if (time_after_eq(jiffies, rq->next_balance) && |
| 3820 | likely(!on_null_domain(cpu))) |
| 3821 | raise_softirq(SCHED_SOFTIRQ); |
Venkatesh Pallipadi | 83cd4fe | 2010-05-21 17:09:41 -0700 | [diff] [blame] | 3822 | #ifdef CONFIG_NO_HZ |
| 3823 | else if (nohz_kick_needed(rq, cpu) && likely(!on_null_domain(cpu))) |
| 3824 | nohz_balancer_kick(cpu); |
| 3825 | #endif |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 3826 | } |
| 3827 | |
Christian Ehrhardt | 0bcdcf2 | 2009-11-30 12:16:46 +0100 | [diff] [blame] | 3828 | static void rq_online_fair(struct rq *rq) |
| 3829 | { |
| 3830 | update_sysctl(); |
| 3831 | } |
| 3832 | |
| 3833 | static void rq_offline_fair(struct rq *rq) |
| 3834 | { |
| 3835 | update_sysctl(); |
| 3836 | } |
| 3837 | |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 3838 | #else /* CONFIG_SMP */ |
| 3839 | |
| 3840 | /* |
| 3841 | * on UP we do not need to balance between CPUs: |
| 3842 | */ |
| 3843 | static inline void idle_balance(int cpu, struct rq *rq) |
| 3844 | { |
| 3845 | } |
| 3846 | |
Dhaval Giani | 55e12e5 | 2008-06-24 23:39:43 +0530 | [diff] [blame] | 3847 | #endif /* CONFIG_SMP */ |
Peter Williams | e1d1484 | 2007-10-24 18:23:51 +0200 | [diff] [blame] | 3848 | |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 3849 | /* |
| 3850 | * scheduler tick hitting a task of our scheduling class: |
| 3851 | */ |
Peter Zijlstra | 8f4d37e | 2008-01-25 21:08:29 +0100 | [diff] [blame] | 3852 | static void task_tick_fair(struct rq *rq, struct task_struct *curr, int queued) |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 3853 | { |
| 3854 | struct cfs_rq *cfs_rq; |
| 3855 | struct sched_entity *se = &curr->se; |
| 3856 | |
| 3857 | for_each_sched_entity(se) { |
| 3858 | cfs_rq = cfs_rq_of(se); |
Peter Zijlstra | 8f4d37e | 2008-01-25 21:08:29 +0100 | [diff] [blame] | 3859 | entity_tick(cfs_rq, se, queued); |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 3860 | } |
| 3861 | } |
| 3862 | |
| 3863 | /* |
Peter Zijlstra | cd29fe6 | 2009-11-27 17:32:46 +0100 | [diff] [blame] | 3864 | * called on fork with the child task as argument from the parent's context |
| 3865 | * - child not yet on the tasklist |
| 3866 | * - preemption disabled |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 3867 | */ |
Peter Zijlstra | cd29fe6 | 2009-11-27 17:32:46 +0100 | [diff] [blame] | 3868 | static void task_fork_fair(struct task_struct *p) |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 3869 | { |
Peter Zijlstra | cd29fe6 | 2009-11-27 17:32:46 +0100 | [diff] [blame] | 3870 | struct cfs_rq *cfs_rq = task_cfs_rq(current); |
Ingo Molnar | 429d43b | 2007-10-15 17:00:03 +0200 | [diff] [blame] | 3871 | struct sched_entity *se = &p->se, *curr = cfs_rq->curr; |
Ingo Molnar | 00bf7bf | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 3872 | int this_cpu = smp_processor_id(); |
Peter Zijlstra | cd29fe6 | 2009-11-27 17:32:46 +0100 | [diff] [blame] | 3873 | struct rq *rq = this_rq(); |
| 3874 | unsigned long flags; |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 3875 | |
Thomas Gleixner | 05fa785 | 2009-11-17 14:28:38 +0100 | [diff] [blame] | 3876 | raw_spin_lock_irqsave(&rq->lock, flags); |
Peter Zijlstra | cd29fe6 | 2009-11-27 17:32:46 +0100 | [diff] [blame] | 3877 | |
Peter Zijlstra | 861d034 | 2010-08-19 13:31:43 +0200 | [diff] [blame] | 3878 | update_rq_clock(rq); |
| 3879 | |
Paul E. McKenney | b0a0f66 | 2010-10-06 17:32:51 -0700 | [diff] [blame] | 3880 | if (unlikely(task_cpu(p) != this_cpu)) { |
| 3881 | rcu_read_lock(); |
Peter Zijlstra | cd29fe6 | 2009-11-27 17:32:46 +0100 | [diff] [blame] | 3882 | __set_task_cpu(p, this_cpu); |
Paul E. McKenney | b0a0f66 | 2010-10-06 17:32:51 -0700 | [diff] [blame] | 3883 | rcu_read_unlock(); |
| 3884 | } |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 3885 | |
Ting Yang | 7109c44 | 2007-08-28 12:53:24 +0200 | [diff] [blame] | 3886 | update_curr(cfs_rq); |
Peter Zijlstra | cd29fe6 | 2009-11-27 17:32:46 +0100 | [diff] [blame] | 3887 | |
Mike Galbraith | b5d9d73 | 2009-09-08 11:12:28 +0200 | [diff] [blame] | 3888 | if (curr) |
| 3889 | se->vruntime = curr->vruntime; |
Peter Zijlstra | aeb73b0 | 2007-10-15 17:00:05 +0200 | [diff] [blame] | 3890 | place_entity(cfs_rq, se, 1); |
Peter Zijlstra | 4d78e7b | 2007-10-15 17:00:04 +0200 | [diff] [blame] | 3891 | |
Peter Zijlstra | cd29fe6 | 2009-11-27 17:32:46 +0100 | [diff] [blame] | 3892 | if (sysctl_sched_child_runs_first && curr && entity_before(curr, se)) { |
Dmitry Adamushko | 87fefa3 | 2007-10-15 17:00:08 +0200 | [diff] [blame] | 3893 | /* |
Ingo Molnar | edcb60a | 2007-10-15 17:00:08 +0200 | [diff] [blame] | 3894 | * Upon rescheduling, sched_class::put_prev_task() will place |
| 3895 | * 'current' within the tree based on its new key value. |
| 3896 | */ |
Peter Zijlstra | 4d78e7b | 2007-10-15 17:00:04 +0200 | [diff] [blame] | 3897 | swap(curr->vruntime, se->vruntime); |
Bharata B Rao | aec0a51 | 2008-08-28 14:42:49 +0530 | [diff] [blame] | 3898 | resched_task(rq->curr); |
Peter Zijlstra | 4d78e7b | 2007-10-15 17:00:04 +0200 | [diff] [blame] | 3899 | } |
| 3900 | |
Peter Zijlstra | 88ec22d | 2009-12-16 18:04:41 +0100 | [diff] [blame] | 3901 | se->vruntime -= cfs_rq->min_vruntime; |
| 3902 | |
Thomas Gleixner | 05fa785 | 2009-11-17 14:28:38 +0100 | [diff] [blame] | 3903 | raw_spin_unlock_irqrestore(&rq->lock, flags); |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 3904 | } |
| 3905 | |
Steven Rostedt | cb46984 | 2008-01-25 21:08:22 +0100 | [diff] [blame] | 3906 | /* |
| 3907 | * Priority of the task has changed. Check to see if we preempt |
| 3908 | * the current task. |
| 3909 | */ |
| 3910 | static void prio_changed_fair(struct rq *rq, struct task_struct *p, |
| 3911 | int oldprio, int running) |
| 3912 | { |
| 3913 | /* |
| 3914 | * Reschedule if we are currently running on this runqueue and |
| 3915 | * our priority decreased, or if we are not currently running on |
| 3916 | * this runqueue and our priority is higher than the current's |
| 3917 | */ |
| 3918 | if (running) { |
| 3919 | if (p->prio > oldprio) |
| 3920 | resched_task(rq->curr); |
| 3921 | } else |
Peter Zijlstra | 15afe09 | 2008-09-20 23:38:02 +0200 | [diff] [blame] | 3922 | check_preempt_curr(rq, p, 0); |
Steven Rostedt | cb46984 | 2008-01-25 21:08:22 +0100 | [diff] [blame] | 3923 | } |
| 3924 | |
| 3925 | /* |
| 3926 | * We switched to the sched_fair class. |
| 3927 | */ |
| 3928 | static void switched_to_fair(struct rq *rq, struct task_struct *p, |
| 3929 | int running) |
| 3930 | { |
| 3931 | /* |
| 3932 | * We were most likely switched from sched_rt, so |
| 3933 | * kick off the schedule if running, otherwise just see |
| 3934 | * if we can still preempt the current task. |
| 3935 | */ |
| 3936 | if (running) |
| 3937 | resched_task(rq->curr); |
| 3938 | else |
Peter Zijlstra | 15afe09 | 2008-09-20 23:38:02 +0200 | [diff] [blame] | 3939 | check_preempt_curr(rq, p, 0); |
Steven Rostedt | cb46984 | 2008-01-25 21:08:22 +0100 | [diff] [blame] | 3940 | } |
| 3941 | |
Srivatsa Vaddagiri | 83b699e | 2007-10-15 17:00:08 +0200 | [diff] [blame] | 3942 | /* Account for a task changing its policy or group. |
| 3943 | * |
| 3944 | * This routine is mostly called to set cfs_rq->curr field when a task |
| 3945 | * migrates between groups/classes. |
| 3946 | */ |
| 3947 | static void set_curr_task_fair(struct rq *rq) |
| 3948 | { |
| 3949 | struct sched_entity *se = &rq->curr->se; |
| 3950 | |
| 3951 | for_each_sched_entity(se) |
| 3952 | set_next_entity(cfs_rq_of(se), se); |
| 3953 | } |
| 3954 | |
Peter Zijlstra | 810b381 | 2008-02-29 15:21:01 -0500 | [diff] [blame] | 3955 | #ifdef CONFIG_FAIR_GROUP_SCHED |
Peter Zijlstra | b2b5ce0 | 2010-10-15 15:24:15 +0200 | [diff] [blame] | 3956 | static void task_move_group_fair(struct task_struct *p, int on_rq) |
Peter Zijlstra | 810b381 | 2008-02-29 15:21:01 -0500 | [diff] [blame] | 3957 | { |
Peter Zijlstra | b2b5ce0 | 2010-10-15 15:24:15 +0200 | [diff] [blame] | 3958 | /* |
| 3959 | * If the task was not on the rq at the time of this cgroup movement |
| 3960 | * it must have been asleep, sleeping tasks keep their ->vruntime |
| 3961 | * absolute on their old rq until wakeup (needed for the fair sleeper |
| 3962 | * bonus in place_entity()). |
| 3963 | * |
| 3964 | * If it was on the rq, we've just 'preempted' it, which does convert |
| 3965 | * ->vruntime to a relative base. |
| 3966 | * |
| 3967 | * Make sure both cases convert their relative position when migrating |
| 3968 | * to another cgroup's rq. This does somewhat interfere with the |
| 3969 | * fair sleeper stuff for the first placement, but who cares. |
| 3970 | */ |
Peter Zijlstra | 88ec22d | 2009-12-16 18:04:41 +0100 | [diff] [blame] | 3971 | if (!on_rq) |
Peter Zijlstra | b2b5ce0 | 2010-10-15 15:24:15 +0200 | [diff] [blame] | 3972 | p->se.vruntime -= cfs_rq_of(&p->se)->min_vruntime; |
| 3973 | set_task_rq(p, task_cpu(p)); |
| 3974 | if (!on_rq) |
| 3975 | p->se.vruntime += cfs_rq_of(&p->se)->min_vruntime; |
Peter Zijlstra | 810b381 | 2008-02-29 15:21:01 -0500 | [diff] [blame] | 3976 | } |
| 3977 | #endif |
| 3978 | |
H Hartley Sweeten | 6d686f4 | 2010-01-13 20:21:52 -0700 | [diff] [blame] | 3979 | static unsigned int get_rr_interval_fair(struct rq *rq, struct task_struct *task) |
Peter Williams | 0d721ce | 2009-09-21 01:31:53 +0000 | [diff] [blame] | 3980 | { |
| 3981 | struct sched_entity *se = &task->se; |
Peter Williams | 0d721ce | 2009-09-21 01:31:53 +0000 | [diff] [blame] | 3982 | unsigned int rr_interval = 0; |
| 3983 | |
| 3984 | /* |
| 3985 | * Time slice is 0 for SCHED_OTHER tasks that are on an otherwise |
| 3986 | * idle runqueue: |
| 3987 | */ |
Peter Williams | 0d721ce | 2009-09-21 01:31:53 +0000 | [diff] [blame] | 3988 | if (rq->cfs.load.weight) |
| 3989 | rr_interval = NS_TO_JIFFIES(sched_slice(&rq->cfs, se)); |
Peter Williams | 0d721ce | 2009-09-21 01:31:53 +0000 | [diff] [blame] | 3990 | |
| 3991 | return rr_interval; |
| 3992 | } |
| 3993 | |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 3994 | /* |
| 3995 | * All the scheduling class methods: |
| 3996 | */ |
Ingo Molnar | 5522d5d | 2007-10-15 17:00:12 +0200 | [diff] [blame] | 3997 | static const struct sched_class fair_sched_class = { |
| 3998 | .next = &idle_sched_class, |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 3999 | .enqueue_task = enqueue_task_fair, |
| 4000 | .dequeue_task = dequeue_task_fair, |
| 4001 | .yield_task = yield_task_fair, |
| 4002 | |
Ingo Molnar | 2e09bf5 | 2007-10-15 17:00:05 +0200 | [diff] [blame] | 4003 | .check_preempt_curr = check_preempt_wakeup, |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 4004 | |
| 4005 | .pick_next_task = pick_next_task_fair, |
| 4006 | .put_prev_task = put_prev_task_fair, |
| 4007 | |
Peter Williams | 681f3e6 | 2007-10-24 18:23:51 +0200 | [diff] [blame] | 4008 | #ifdef CONFIG_SMP |
Li Zefan | 4ce72a2 | 2008-10-22 15:25:26 +0800 | [diff] [blame] | 4009 | .select_task_rq = select_task_rq_fair, |
| 4010 | |
Christian Ehrhardt | 0bcdcf2 | 2009-11-30 12:16:46 +0100 | [diff] [blame] | 4011 | .rq_online = rq_online_fair, |
| 4012 | .rq_offline = rq_offline_fair, |
Peter Zijlstra | 88ec22d | 2009-12-16 18:04:41 +0100 | [diff] [blame] | 4013 | |
| 4014 | .task_waking = task_waking_fair, |
Peter Williams | 681f3e6 | 2007-10-24 18:23:51 +0200 | [diff] [blame] | 4015 | #endif |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 4016 | |
Srivatsa Vaddagiri | 83b699e | 2007-10-15 17:00:08 +0200 | [diff] [blame] | 4017 | .set_curr_task = set_curr_task_fair, |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 4018 | .task_tick = task_tick_fair, |
Peter Zijlstra | cd29fe6 | 2009-11-27 17:32:46 +0100 | [diff] [blame] | 4019 | .task_fork = task_fork_fair, |
Steven Rostedt | cb46984 | 2008-01-25 21:08:22 +0100 | [diff] [blame] | 4020 | |
| 4021 | .prio_changed = prio_changed_fair, |
| 4022 | .switched_to = switched_to_fair, |
Peter Zijlstra | 810b381 | 2008-02-29 15:21:01 -0500 | [diff] [blame] | 4023 | |
Peter Williams | 0d721ce | 2009-09-21 01:31:53 +0000 | [diff] [blame] | 4024 | .get_rr_interval = get_rr_interval_fair, |
| 4025 | |
Peter Zijlstra | 810b381 | 2008-02-29 15:21:01 -0500 | [diff] [blame] | 4026 | #ifdef CONFIG_FAIR_GROUP_SCHED |
Peter Zijlstra | b2b5ce0 | 2010-10-15 15:24:15 +0200 | [diff] [blame] | 4027 | .task_move_group = task_move_group_fair, |
Peter Zijlstra | 810b381 | 2008-02-29 15:21:01 -0500 | [diff] [blame] | 4028 | #endif |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 4029 | }; |
| 4030 | |
| 4031 | #ifdef CONFIG_SCHED_DEBUG |
Ingo Molnar | 5cef9ec | 2007-08-09 11:16:47 +0200 | [diff] [blame] | 4032 | static void print_cfs_stats(struct seq_file *m, int cpu) |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 4033 | { |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 4034 | struct cfs_rq *cfs_rq; |
| 4035 | |
Peter Zijlstra | 5973e5b | 2008-01-25 21:08:34 +0100 | [diff] [blame] | 4036 | rcu_read_lock(); |
Ingo Molnar | c3b64f1 | 2007-08-09 11:16:51 +0200 | [diff] [blame] | 4037 | for_each_leaf_cfs_rq(cpu_rq(cpu), cfs_rq) |
Ingo Molnar | 5cef9ec | 2007-08-09 11:16:47 +0200 | [diff] [blame] | 4038 | print_cfs_rq(m, cpu, cfs_rq); |
Peter Zijlstra | 5973e5b | 2008-01-25 21:08:34 +0100 | [diff] [blame] | 4039 | rcu_read_unlock(); |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 4040 | } |
| 4041 | #endif |