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> |
Sisir Koppaka | 3436ae1 | 2011-03-26 18:22:55 +0530 | [diff] [blame] | 25 | #include <linux/cpumask.h> |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 26 | #include <linux/slab.h> |
| 27 | #include <linux/profile.h> |
| 28 | #include <linux/interrupt.h> |
| 29 | |
| 30 | #include <trace/events/sched.h> |
| 31 | |
| 32 | #include "sched.h" |
Arjan van de Ven | 9745512 | 2008-01-25 21:08:34 +0100 | [diff] [blame] | 33 | |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 34 | /* |
Peter Zijlstra | 2180508 | 2007-08-25 18:41:53 +0200 | [diff] [blame] | 35 | * Targeted preemption latency for CPU-bound tasks: |
Takuya Yoshikawa | 864616e | 2010-10-14 16:09:13 +0900 | [diff] [blame] | 36 | * (default: 6ms * (1 + ilog(ncpus)), units: nanoseconds) |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 37 | * |
Peter Zijlstra | 2180508 | 2007-08-25 18:41:53 +0200 | [diff] [blame] | 38 | * NOTE: this latency value is not the same as the concept of |
Ingo Molnar | d274a4c | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 39 | * 'timeslice length' - timeslices in CFS are of variable length |
| 40 | * and have no persistent notion like in traditional, time-slice |
| 41 | * based scheduling concepts. |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 42 | * |
Ingo Molnar | d274a4c | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 43 | * (to see the precise effective timeslice length of your workload, |
| 44 | * run vmstat and monitor the context-switches (cs) field) |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 45 | */ |
Mike Galbraith | 2140692 | 2010-03-11 17:17:15 +0100 | [diff] [blame] | 46 | unsigned int sysctl_sched_latency = 6000000ULL; |
| 47 | unsigned int normalized_sysctl_sched_latency = 6000000ULL; |
Ingo Molnar | 2bd8e6d | 2007-10-15 17:00:02 +0200 | [diff] [blame] | 48 | |
| 49 | /* |
Christian Ehrhardt | 1983a92 | 2009-11-30 12:16:47 +0100 | [diff] [blame] | 50 | * The initial- and re-scaling of tunables is configurable |
| 51 | * (default SCHED_TUNABLESCALING_LOG = *(1+ilog(ncpus)) |
| 52 | * |
| 53 | * Options are: |
| 54 | * SCHED_TUNABLESCALING_NONE - unscaled, always *1 |
| 55 | * SCHED_TUNABLESCALING_LOG - scaled logarithmical, *1+ilog(ncpus) |
| 56 | * SCHED_TUNABLESCALING_LINEAR - scaled linear, *ncpus |
| 57 | */ |
| 58 | enum sched_tunable_scaling sysctl_sched_tunable_scaling |
| 59 | = SCHED_TUNABLESCALING_LOG; |
| 60 | |
| 61 | /* |
Peter Zijlstra | b2be5e9 | 2007-11-09 22:39:37 +0100 | [diff] [blame] | 62 | * Minimal preemption granularity for CPU-bound tasks: |
Takuya Yoshikawa | 864616e | 2010-10-14 16:09:13 +0900 | [diff] [blame] | 63 | * (default: 0.75 msec * (1 + ilog(ncpus)), units: nanoseconds) |
Peter Zijlstra | b2be5e9 | 2007-11-09 22:39:37 +0100 | [diff] [blame] | 64 | */ |
Ingo Molnar | 0bf377b | 2010-09-12 08:14:52 +0200 | [diff] [blame] | 65 | unsigned int sysctl_sched_min_granularity = 750000ULL; |
| 66 | unsigned int normalized_sysctl_sched_min_granularity = 750000ULL; |
Peter Zijlstra | b2be5e9 | 2007-11-09 22:39:37 +0100 | [diff] [blame] | 67 | |
| 68 | /* |
| 69 | * is kept at sysctl_sched_latency / sysctl_sched_min_granularity |
| 70 | */ |
Ingo Molnar | 0bf377b | 2010-09-12 08:14:52 +0200 | [diff] [blame] | 71 | static unsigned int sched_nr_latency = 8; |
Peter Zijlstra | b2be5e9 | 2007-11-09 22:39:37 +0100 | [diff] [blame] | 72 | |
| 73 | /* |
Mike Galbraith | 2bba22c | 2009-09-09 15:41:37 +0200 | [diff] [blame] | 74 | * After fork, child runs first. If set to 0 (default) then |
Ingo Molnar | 2bd8e6d | 2007-10-15 17:00:02 +0200 | [diff] [blame] | 75 | * parent will (try to) run first. |
| 76 | */ |
Mike Galbraith | 2bba22c | 2009-09-09 15:41:37 +0200 | [diff] [blame] | 77 | unsigned int sysctl_sched_child_runs_first __read_mostly; |
Peter Zijlstra | 2180508 | 2007-08-25 18:41:53 +0200 | [diff] [blame] | 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 | |
Paul Turner | a7a4f8a | 2010-11-15 15:47:06 -0800 | [diff] [blame] | 92 | /* |
| 93 | * The exponential sliding window over which load is averaged for shares |
| 94 | * distribution. |
| 95 | * (default: 10msec) |
| 96 | */ |
| 97 | unsigned int __read_mostly sysctl_sched_shares_window = 10000000UL; |
| 98 | |
Paul Turner | ec12cb7 | 2011-07-21 09:43:30 -0700 | [diff] [blame] | 99 | #ifdef CONFIG_CFS_BANDWIDTH |
| 100 | /* |
| 101 | * Amount of runtime to allocate from global (tg) to local (per-cfs_rq) pool |
| 102 | * each time a cfs_rq requests quota. |
| 103 | * |
| 104 | * Note: in the case that the slice exceeds the runtime remaining (either due |
| 105 | * to consumption or the quota being specified to be smaller than the slice) |
| 106 | * we will always only issue the remaining available time. |
| 107 | * |
| 108 | * default: 5 msec, units: microseconds |
| 109 | */ |
| 110 | unsigned int sysctl_sched_cfs_bandwidth_slice = 5000UL; |
| 111 | #endif |
| 112 | |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 113 | /* |
| 114 | * Increase the granularity value when there are more CPUs, |
| 115 | * because with more CPUs the 'effective latency' as visible |
| 116 | * to users decreases. But the relationship is not linear, |
| 117 | * so pick a second-best guess by going with the log2 of the |
| 118 | * number of CPUs. |
| 119 | * |
| 120 | * This idea comes from the SD scheduler of Con Kolivas: |
| 121 | */ |
| 122 | static int get_update_sysctl_factor(void) |
| 123 | { |
| 124 | unsigned int cpus = min_t(int, num_online_cpus(), 8); |
| 125 | unsigned int factor; |
| 126 | |
| 127 | switch (sysctl_sched_tunable_scaling) { |
| 128 | case SCHED_TUNABLESCALING_NONE: |
| 129 | factor = 1; |
| 130 | break; |
| 131 | case SCHED_TUNABLESCALING_LINEAR: |
| 132 | factor = cpus; |
| 133 | break; |
| 134 | case SCHED_TUNABLESCALING_LOG: |
| 135 | default: |
| 136 | factor = 1 + ilog2(cpus); |
| 137 | break; |
| 138 | } |
| 139 | |
| 140 | return factor; |
| 141 | } |
| 142 | |
| 143 | static void update_sysctl(void) |
| 144 | { |
| 145 | unsigned int factor = get_update_sysctl_factor(); |
| 146 | |
| 147 | #define SET_SYSCTL(name) \ |
| 148 | (sysctl_##name = (factor) * normalized_sysctl_##name) |
| 149 | SET_SYSCTL(sched_min_granularity); |
| 150 | SET_SYSCTL(sched_latency); |
| 151 | SET_SYSCTL(sched_wakeup_granularity); |
| 152 | #undef SET_SYSCTL |
| 153 | } |
| 154 | |
| 155 | void sched_init_granularity(void) |
| 156 | { |
| 157 | update_sysctl(); |
| 158 | } |
| 159 | |
| 160 | #if BITS_PER_LONG == 32 |
| 161 | # define WMULT_CONST (~0UL) |
| 162 | #else |
| 163 | # define WMULT_CONST (1UL << 32) |
| 164 | #endif |
| 165 | |
| 166 | #define WMULT_SHIFT 32 |
| 167 | |
| 168 | /* |
| 169 | * Shift right and round: |
| 170 | */ |
| 171 | #define SRR(x, y) (((x) + (1UL << ((y) - 1))) >> (y)) |
| 172 | |
| 173 | /* |
| 174 | * delta *= weight / lw |
| 175 | */ |
| 176 | static unsigned long |
| 177 | calc_delta_mine(unsigned long delta_exec, unsigned long weight, |
| 178 | struct load_weight *lw) |
| 179 | { |
| 180 | u64 tmp; |
| 181 | |
| 182 | /* |
| 183 | * weight can be less than 2^SCHED_LOAD_RESOLUTION for task group sched |
| 184 | * entities since MIN_SHARES = 2. Treat weight as 1 if less than |
| 185 | * 2^SCHED_LOAD_RESOLUTION. |
| 186 | */ |
| 187 | if (likely(weight > (1UL << SCHED_LOAD_RESOLUTION))) |
| 188 | tmp = (u64)delta_exec * scale_load_down(weight); |
| 189 | else |
| 190 | tmp = (u64)delta_exec; |
| 191 | |
| 192 | if (!lw->inv_weight) { |
| 193 | unsigned long w = scale_load_down(lw->weight); |
| 194 | |
| 195 | if (BITS_PER_LONG > 32 && unlikely(w >= WMULT_CONST)) |
| 196 | lw->inv_weight = 1; |
| 197 | else if (unlikely(!w)) |
| 198 | lw->inv_weight = WMULT_CONST; |
| 199 | else |
| 200 | lw->inv_weight = WMULT_CONST / w; |
| 201 | } |
| 202 | |
| 203 | /* |
| 204 | * Check whether we'd overflow the 64-bit multiplication: |
| 205 | */ |
| 206 | if (unlikely(tmp > WMULT_CONST)) |
| 207 | tmp = SRR(SRR(tmp, WMULT_SHIFT/2) * lw->inv_weight, |
| 208 | WMULT_SHIFT/2); |
| 209 | else |
| 210 | tmp = SRR(tmp * lw->inv_weight, WMULT_SHIFT); |
| 211 | |
| 212 | return (unsigned long)min(tmp, (u64)(unsigned long)LONG_MAX); |
| 213 | } |
| 214 | |
| 215 | |
| 216 | const struct sched_class fair_sched_class; |
Peter Zijlstra | a4c2f00 | 2008-10-17 19:27:03 +0200 | [diff] [blame] | 217 | |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 218 | /************************************************************** |
| 219 | * CFS operations on generic schedulable entities: |
| 220 | */ |
| 221 | |
| 222 | #ifdef CONFIG_FAIR_GROUP_SCHED |
| 223 | |
| 224 | /* cpu runqueue to which this cfs_rq is attached */ |
| 225 | static inline struct rq *rq_of(struct cfs_rq *cfs_rq) |
| 226 | { |
| 227 | return cfs_rq->rq; |
| 228 | } |
| 229 | |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 230 | /* An entity is a task if it doesn't "own" a runqueue */ |
| 231 | #define entity_is_task(se) (!se->my_q) |
| 232 | |
Peter Zijlstra | 8f48894 | 2009-07-24 12:25:30 +0200 | [diff] [blame] | 233 | static inline struct task_struct *task_of(struct sched_entity *se) |
| 234 | { |
| 235 | #ifdef CONFIG_SCHED_DEBUG |
| 236 | WARN_ON_ONCE(!entity_is_task(se)); |
| 237 | #endif |
| 238 | return container_of(se, struct task_struct, se); |
| 239 | } |
| 240 | |
Peter Zijlstra | b758149 | 2008-04-19 19:45:00 +0200 | [diff] [blame] | 241 | /* Walk up scheduling entities hierarchy */ |
| 242 | #define for_each_sched_entity(se) \ |
| 243 | for (; se; se = se->parent) |
| 244 | |
| 245 | static inline struct cfs_rq *task_cfs_rq(struct task_struct *p) |
| 246 | { |
| 247 | return p->se.cfs_rq; |
| 248 | } |
| 249 | |
| 250 | /* runqueue on which this entity is (to be) queued */ |
| 251 | static inline struct cfs_rq *cfs_rq_of(struct sched_entity *se) |
| 252 | { |
| 253 | return se->cfs_rq; |
| 254 | } |
| 255 | |
| 256 | /* runqueue "owned" by this group */ |
| 257 | static inline struct cfs_rq *group_cfs_rq(struct sched_entity *grp) |
| 258 | { |
| 259 | return grp->my_q; |
| 260 | } |
| 261 | |
Paul Turner | aff3e49 | 2012-10-04 13:18:30 +0200 | [diff] [blame] | 262 | static void update_cfs_rq_blocked_load(struct cfs_rq *cfs_rq, |
| 263 | int force_update); |
Paul Turner | 9ee474f | 2012-10-04 13:18:30 +0200 | [diff] [blame] | 264 | |
Peter Zijlstra | 3d4b47b | 2010-11-15 15:47:01 -0800 | [diff] [blame] | 265 | static inline void list_add_leaf_cfs_rq(struct cfs_rq *cfs_rq) |
| 266 | { |
| 267 | if (!cfs_rq->on_list) { |
Paul Turner | 67e8625 | 2010-11-15 15:47:05 -0800 | [diff] [blame] | 268 | /* |
| 269 | * Ensure we either appear before our parent (if already |
| 270 | * enqueued) or force our parent to appear after us when it is |
| 271 | * enqueued. The fact that we always enqueue bottom-up |
| 272 | * reduces this to two cases. |
| 273 | */ |
| 274 | if (cfs_rq->tg->parent && |
| 275 | cfs_rq->tg->parent->cfs_rq[cpu_of(rq_of(cfs_rq))]->on_list) { |
| 276 | list_add_rcu(&cfs_rq->leaf_cfs_rq_list, |
Peter Zijlstra | 3d4b47b | 2010-11-15 15:47:01 -0800 | [diff] [blame] | 277 | &rq_of(cfs_rq)->leaf_cfs_rq_list); |
Paul Turner | 67e8625 | 2010-11-15 15:47:05 -0800 | [diff] [blame] | 278 | } else { |
| 279 | list_add_tail_rcu(&cfs_rq->leaf_cfs_rq_list, |
| 280 | &rq_of(cfs_rq)->leaf_cfs_rq_list); |
| 281 | } |
Peter Zijlstra | 3d4b47b | 2010-11-15 15:47:01 -0800 | [diff] [blame] | 282 | |
| 283 | cfs_rq->on_list = 1; |
Paul Turner | 9ee474f | 2012-10-04 13:18:30 +0200 | [diff] [blame] | 284 | /* We should have no load, but we need to update last_decay. */ |
Paul Turner | aff3e49 | 2012-10-04 13:18:30 +0200 | [diff] [blame] | 285 | update_cfs_rq_blocked_load(cfs_rq, 0); |
Peter Zijlstra | 3d4b47b | 2010-11-15 15:47:01 -0800 | [diff] [blame] | 286 | } |
| 287 | } |
| 288 | |
| 289 | static inline void list_del_leaf_cfs_rq(struct cfs_rq *cfs_rq) |
| 290 | { |
| 291 | if (cfs_rq->on_list) { |
| 292 | list_del_rcu(&cfs_rq->leaf_cfs_rq_list); |
| 293 | cfs_rq->on_list = 0; |
| 294 | } |
| 295 | } |
| 296 | |
Peter Zijlstra | b758149 | 2008-04-19 19:45:00 +0200 | [diff] [blame] | 297 | /* Iterate thr' all leaf cfs_rq's on a runqueue */ |
| 298 | #define for_each_leaf_cfs_rq(rq, cfs_rq) \ |
| 299 | list_for_each_entry_rcu(cfs_rq, &rq->leaf_cfs_rq_list, leaf_cfs_rq_list) |
| 300 | |
| 301 | /* Do the two (enqueued) entities belong to the same group ? */ |
| 302 | static inline int |
| 303 | is_same_group(struct sched_entity *se, struct sched_entity *pse) |
| 304 | { |
| 305 | if (se->cfs_rq == pse->cfs_rq) |
| 306 | return 1; |
| 307 | |
| 308 | return 0; |
| 309 | } |
| 310 | |
| 311 | static inline struct sched_entity *parent_entity(struct sched_entity *se) |
| 312 | { |
| 313 | return se->parent; |
| 314 | } |
| 315 | |
Peter Zijlstra | 464b752 | 2008-10-24 11:06:15 +0200 | [diff] [blame] | 316 | /* return depth at which a sched entity is present in the hierarchy */ |
| 317 | static inline int depth_se(struct sched_entity *se) |
| 318 | { |
| 319 | int depth = 0; |
| 320 | |
| 321 | for_each_sched_entity(se) |
| 322 | depth++; |
| 323 | |
| 324 | return depth; |
| 325 | } |
| 326 | |
| 327 | static void |
| 328 | find_matching_se(struct sched_entity **se, struct sched_entity **pse) |
| 329 | { |
| 330 | int se_depth, pse_depth; |
| 331 | |
| 332 | /* |
| 333 | * preemption test can be made between sibling entities who are in the |
| 334 | * same cfs_rq i.e who have a common parent. Walk up the hierarchy of |
| 335 | * both tasks until we find their ancestors who are siblings of common |
| 336 | * parent. |
| 337 | */ |
| 338 | |
| 339 | /* First walk up until both entities are at same depth */ |
| 340 | se_depth = depth_se(*se); |
| 341 | pse_depth = depth_se(*pse); |
| 342 | |
| 343 | while (se_depth > pse_depth) { |
| 344 | se_depth--; |
| 345 | *se = parent_entity(*se); |
| 346 | } |
| 347 | |
| 348 | while (pse_depth > se_depth) { |
| 349 | pse_depth--; |
| 350 | *pse = parent_entity(*pse); |
| 351 | } |
| 352 | |
| 353 | while (!is_same_group(*se, *pse)) { |
| 354 | *se = parent_entity(*se); |
| 355 | *pse = parent_entity(*pse); |
| 356 | } |
| 357 | } |
| 358 | |
Peter Zijlstra | 8f48894 | 2009-07-24 12:25:30 +0200 | [diff] [blame] | 359 | #else /* !CONFIG_FAIR_GROUP_SCHED */ |
| 360 | |
| 361 | static inline struct task_struct *task_of(struct sched_entity *se) |
| 362 | { |
| 363 | return container_of(se, struct task_struct, se); |
| 364 | } |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 365 | |
| 366 | static inline struct rq *rq_of(struct cfs_rq *cfs_rq) |
| 367 | { |
| 368 | return container_of(cfs_rq, struct rq, cfs); |
| 369 | } |
| 370 | |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 371 | #define entity_is_task(se) 1 |
| 372 | |
Peter Zijlstra | b758149 | 2008-04-19 19:45:00 +0200 | [diff] [blame] | 373 | #define for_each_sched_entity(se) \ |
| 374 | for (; se; se = NULL) |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 375 | |
Peter Zijlstra | b758149 | 2008-04-19 19:45:00 +0200 | [diff] [blame] | 376 | static inline struct cfs_rq *task_cfs_rq(struct task_struct *p) |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 377 | { |
Peter Zijlstra | b758149 | 2008-04-19 19:45:00 +0200 | [diff] [blame] | 378 | return &task_rq(p)->cfs; |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 379 | } |
| 380 | |
Peter Zijlstra | b758149 | 2008-04-19 19:45:00 +0200 | [diff] [blame] | 381 | static inline struct cfs_rq *cfs_rq_of(struct sched_entity *se) |
| 382 | { |
| 383 | struct task_struct *p = task_of(se); |
| 384 | struct rq *rq = task_rq(p); |
| 385 | |
| 386 | return &rq->cfs; |
| 387 | } |
| 388 | |
| 389 | /* runqueue "owned" by this group */ |
| 390 | static inline struct cfs_rq *group_cfs_rq(struct sched_entity *grp) |
| 391 | { |
| 392 | return NULL; |
| 393 | } |
| 394 | |
Peter Zijlstra | 3d4b47b | 2010-11-15 15:47:01 -0800 | [diff] [blame] | 395 | static inline void list_add_leaf_cfs_rq(struct cfs_rq *cfs_rq) |
| 396 | { |
| 397 | } |
| 398 | |
| 399 | static inline void list_del_leaf_cfs_rq(struct cfs_rq *cfs_rq) |
| 400 | { |
| 401 | } |
| 402 | |
Peter Zijlstra | b758149 | 2008-04-19 19:45:00 +0200 | [diff] [blame] | 403 | #define for_each_leaf_cfs_rq(rq, cfs_rq) \ |
| 404 | for (cfs_rq = &rq->cfs; cfs_rq; cfs_rq = NULL) |
| 405 | |
| 406 | static inline int |
| 407 | is_same_group(struct sched_entity *se, struct sched_entity *pse) |
| 408 | { |
| 409 | return 1; |
| 410 | } |
| 411 | |
| 412 | static inline struct sched_entity *parent_entity(struct sched_entity *se) |
| 413 | { |
| 414 | return NULL; |
| 415 | } |
| 416 | |
Peter Zijlstra | 464b752 | 2008-10-24 11:06:15 +0200 | [diff] [blame] | 417 | static inline void |
| 418 | find_matching_se(struct sched_entity **se, struct sched_entity **pse) |
| 419 | { |
| 420 | } |
| 421 | |
Peter Zijlstra | b758149 | 2008-04-19 19:45:00 +0200 | [diff] [blame] | 422 | #endif /* CONFIG_FAIR_GROUP_SCHED */ |
| 423 | |
Peter Zijlstra | 6c16a6d | 2012-03-21 13:07:16 -0700 | [diff] [blame] | 424 | static __always_inline |
| 425 | void account_cfs_rq_runtime(struct cfs_rq *cfs_rq, unsigned long delta_exec); |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 426 | |
| 427 | /************************************************************** |
| 428 | * Scheduling class tree data structure manipulation methods: |
| 429 | */ |
| 430 | |
Ingo Molnar | 0702e3e | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 431 | static inline u64 max_vruntime(u64 min_vruntime, u64 vruntime) |
Peter Zijlstra | 02e0431 | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 432 | { |
Peter Zijlstra | 368059a | 2007-10-15 17:00:11 +0200 | [diff] [blame] | 433 | s64 delta = (s64)(vruntime - min_vruntime); |
| 434 | if (delta > 0) |
Peter Zijlstra | 02e0431 | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 435 | min_vruntime = vruntime; |
| 436 | |
| 437 | return min_vruntime; |
| 438 | } |
| 439 | |
Ingo Molnar | 0702e3e | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 440 | static inline u64 min_vruntime(u64 min_vruntime, u64 vruntime) |
Peter Zijlstra | b0ffd24 | 2007-10-15 17:00:12 +0200 | [diff] [blame] | 441 | { |
| 442 | s64 delta = (s64)(vruntime - min_vruntime); |
| 443 | if (delta < 0) |
| 444 | min_vruntime = vruntime; |
| 445 | |
| 446 | return min_vruntime; |
| 447 | } |
| 448 | |
Fabio Checconi | 54fdc58 | 2009-07-16 12:32:27 +0200 | [diff] [blame] | 449 | static inline int entity_before(struct sched_entity *a, |
| 450 | struct sched_entity *b) |
| 451 | { |
| 452 | return (s64)(a->vruntime - b->vruntime) < 0; |
| 453 | } |
| 454 | |
Peter Zijlstra | 1af5f73 | 2008-10-24 11:06:13 +0200 | [diff] [blame] | 455 | static void update_min_vruntime(struct cfs_rq *cfs_rq) |
| 456 | { |
| 457 | u64 vruntime = cfs_rq->min_vruntime; |
| 458 | |
| 459 | if (cfs_rq->curr) |
| 460 | vruntime = cfs_rq->curr->vruntime; |
| 461 | |
| 462 | if (cfs_rq->rb_leftmost) { |
| 463 | struct sched_entity *se = rb_entry(cfs_rq->rb_leftmost, |
| 464 | struct sched_entity, |
| 465 | run_node); |
| 466 | |
Peter Zijlstra | e17036d | 2009-01-15 14:53:39 +0100 | [diff] [blame] | 467 | if (!cfs_rq->curr) |
Peter Zijlstra | 1af5f73 | 2008-10-24 11:06:13 +0200 | [diff] [blame] | 468 | vruntime = se->vruntime; |
| 469 | else |
| 470 | vruntime = min_vruntime(vruntime, se->vruntime); |
| 471 | } |
| 472 | |
| 473 | cfs_rq->min_vruntime = max_vruntime(cfs_rq->min_vruntime, vruntime); |
Peter Zijlstra | 3fe1698 | 2011-04-05 17:23:48 +0200 | [diff] [blame] | 474 | #ifndef CONFIG_64BIT |
| 475 | smp_wmb(); |
| 476 | cfs_rq->min_vruntime_copy = cfs_rq->min_vruntime; |
| 477 | #endif |
Peter Zijlstra | 1af5f73 | 2008-10-24 11:06:13 +0200 | [diff] [blame] | 478 | } |
| 479 | |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 480 | /* |
| 481 | * Enqueue an entity into the rb-tree: |
| 482 | */ |
Ingo Molnar | 0702e3e | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 483 | 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] | 484 | { |
| 485 | struct rb_node **link = &cfs_rq->tasks_timeline.rb_node; |
| 486 | struct rb_node *parent = NULL; |
| 487 | struct sched_entity *entry; |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 488 | int leftmost = 1; |
| 489 | |
| 490 | /* |
| 491 | * Find the right place in the rbtree: |
| 492 | */ |
| 493 | while (*link) { |
| 494 | parent = *link; |
| 495 | entry = rb_entry(parent, struct sched_entity, run_node); |
| 496 | /* |
| 497 | * We dont care about collisions. Nodes with |
| 498 | * the same key stay together. |
| 499 | */ |
Stephan Baerwolf | 2bd2d6f | 2011-07-20 14:46:59 +0200 | [diff] [blame] | 500 | if (entity_before(se, entry)) { |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 501 | link = &parent->rb_left; |
| 502 | } else { |
| 503 | link = &parent->rb_right; |
| 504 | leftmost = 0; |
| 505 | } |
| 506 | } |
| 507 | |
| 508 | /* |
| 509 | * Maintain a cache of leftmost tree entries (it is frequently |
| 510 | * used): |
| 511 | */ |
Peter Zijlstra | 1af5f73 | 2008-10-24 11:06:13 +0200 | [diff] [blame] | 512 | if (leftmost) |
Ingo Molnar | 57cb499 | 2007-10-15 17:00:11 +0200 | [diff] [blame] | 513 | cfs_rq->rb_leftmost = &se->run_node; |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 514 | |
| 515 | rb_link_node(&se->run_node, parent, link); |
| 516 | rb_insert_color(&se->run_node, &cfs_rq->tasks_timeline); |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 517 | } |
| 518 | |
Ingo Molnar | 0702e3e | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 519 | 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] | 520 | { |
Peter Zijlstra | 3fe6974 | 2008-03-14 20:55:51 +0100 | [diff] [blame] | 521 | if (cfs_rq->rb_leftmost == &se->run_node) { |
| 522 | struct rb_node *next_node; |
Peter Zijlstra | 3fe6974 | 2008-03-14 20:55:51 +0100 | [diff] [blame] | 523 | |
| 524 | next_node = rb_next(&se->run_node); |
| 525 | cfs_rq->rb_leftmost = next_node; |
Peter Zijlstra | 3fe6974 | 2008-03-14 20:55:51 +0100 | [diff] [blame] | 526 | } |
Ingo Molnar | e9acbff | 2007-10-15 17:00:04 +0200 | [diff] [blame] | 527 | |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 528 | rb_erase(&se->run_node, &cfs_rq->tasks_timeline); |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 529 | } |
| 530 | |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 531 | struct sched_entity *__pick_first_entity(struct cfs_rq *cfs_rq) |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 532 | { |
Peter Zijlstra | f4b6755 | 2008-11-04 21:25:07 +0100 | [diff] [blame] | 533 | struct rb_node *left = cfs_rq->rb_leftmost; |
| 534 | |
| 535 | if (!left) |
| 536 | return NULL; |
| 537 | |
| 538 | return rb_entry(left, struct sched_entity, run_node); |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 539 | } |
| 540 | |
Rik van Riel | ac53db5 | 2011-02-01 09:51:03 -0500 | [diff] [blame] | 541 | static struct sched_entity *__pick_next_entity(struct sched_entity *se) |
| 542 | { |
| 543 | struct rb_node *next = rb_next(&se->run_node); |
| 544 | |
| 545 | if (!next) |
| 546 | return NULL; |
| 547 | |
| 548 | return rb_entry(next, struct sched_entity, run_node); |
| 549 | } |
| 550 | |
| 551 | #ifdef CONFIG_SCHED_DEBUG |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 552 | struct sched_entity *__pick_last_entity(struct cfs_rq *cfs_rq) |
Peter Zijlstra | aeb73b0 | 2007-10-15 17:00:05 +0200 | [diff] [blame] | 553 | { |
Ingo Molnar | 7eee3e6 | 2008-02-22 10:32:21 +0100 | [diff] [blame] | 554 | struct rb_node *last = rb_last(&cfs_rq->tasks_timeline); |
Peter Zijlstra | aeb73b0 | 2007-10-15 17:00:05 +0200 | [diff] [blame] | 555 | |
Balbir Singh | 70eee74 | 2008-02-22 13:25:53 +0530 | [diff] [blame] | 556 | if (!last) |
| 557 | return NULL; |
Ingo Molnar | 7eee3e6 | 2008-02-22 10:32:21 +0100 | [diff] [blame] | 558 | |
| 559 | return rb_entry(last, struct sched_entity, run_node); |
Peter Zijlstra | aeb73b0 | 2007-10-15 17:00:05 +0200 | [diff] [blame] | 560 | } |
| 561 | |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 562 | /************************************************************** |
| 563 | * Scheduling class statistics methods: |
| 564 | */ |
| 565 | |
Christian Ehrhardt | acb4a84 | 2009-11-30 12:16:48 +0100 | [diff] [blame] | 566 | int sched_proc_update_handler(struct ctl_table *table, int write, |
Alexey Dobriyan | 8d65af7 | 2009-09-23 15:57:19 -0700 | [diff] [blame] | 567 | void __user *buffer, size_t *lenp, |
Peter Zijlstra | b2be5e9 | 2007-11-09 22:39:37 +0100 | [diff] [blame] | 568 | loff_t *ppos) |
| 569 | { |
Alexey Dobriyan | 8d65af7 | 2009-09-23 15:57:19 -0700 | [diff] [blame] | 570 | int ret = proc_dointvec_minmax(table, write, buffer, lenp, ppos); |
Christian Ehrhardt | acb4a84 | 2009-11-30 12:16:48 +0100 | [diff] [blame] | 571 | int factor = get_update_sysctl_factor(); |
Peter Zijlstra | b2be5e9 | 2007-11-09 22:39:37 +0100 | [diff] [blame] | 572 | |
| 573 | if (ret || !write) |
| 574 | return ret; |
| 575 | |
| 576 | sched_nr_latency = DIV_ROUND_UP(sysctl_sched_latency, |
| 577 | sysctl_sched_min_granularity); |
| 578 | |
Christian Ehrhardt | acb4a84 | 2009-11-30 12:16:48 +0100 | [diff] [blame] | 579 | #define WRT_SYSCTL(name) \ |
| 580 | (normalized_sysctl_##name = sysctl_##name / (factor)) |
| 581 | WRT_SYSCTL(sched_min_granularity); |
| 582 | WRT_SYSCTL(sched_latency); |
| 583 | WRT_SYSCTL(sched_wakeup_granularity); |
Christian Ehrhardt | acb4a84 | 2009-11-30 12:16:48 +0100 | [diff] [blame] | 584 | #undef WRT_SYSCTL |
| 585 | |
Peter Zijlstra | b2be5e9 | 2007-11-09 22:39:37 +0100 | [diff] [blame] | 586 | return 0; |
| 587 | } |
| 588 | #endif |
Ingo Molnar | 647e7ca | 2007-10-15 17:00:13 +0200 | [diff] [blame] | 589 | |
| 590 | /* |
Peter Zijlstra | f9c0b09 | 2008-10-17 19:27:04 +0200 | [diff] [blame] | 591 | * delta /= w |
Peter Zijlstra | a7be37a | 2008-06-27 13:41:11 +0200 | [diff] [blame] | 592 | */ |
| 593 | static inline unsigned long |
| 594 | calc_delta_fair(unsigned long delta, struct sched_entity *se) |
| 595 | { |
Peter Zijlstra | f9c0b09 | 2008-10-17 19:27:04 +0200 | [diff] [blame] | 596 | if (unlikely(se->load.weight != NICE_0_LOAD)) |
| 597 | delta = calc_delta_mine(delta, NICE_0_LOAD, &se->load); |
Peter Zijlstra | a7be37a | 2008-06-27 13:41:11 +0200 | [diff] [blame] | 598 | |
| 599 | return delta; |
| 600 | } |
| 601 | |
| 602 | /* |
Ingo Molnar | 647e7ca | 2007-10-15 17:00:13 +0200 | [diff] [blame] | 603 | * The idea is to set a period in which each task runs once. |
| 604 | * |
Borislav Petkov | 532b185 | 2012-08-08 16:16:04 +0200 | [diff] [blame] | 605 | * When there are too many tasks (sched_nr_latency) we have to stretch |
Ingo Molnar | 647e7ca | 2007-10-15 17:00:13 +0200 | [diff] [blame] | 606 | * this period because otherwise the slices get too small. |
| 607 | * |
| 608 | * p = (nr <= nl) ? l : l*nr/nl |
| 609 | */ |
Peter Zijlstra | 4d78e7b | 2007-10-15 17:00:04 +0200 | [diff] [blame] | 610 | static u64 __sched_period(unsigned long nr_running) |
| 611 | { |
| 612 | u64 period = sysctl_sched_latency; |
Peter Zijlstra | b2be5e9 | 2007-11-09 22:39:37 +0100 | [diff] [blame] | 613 | unsigned long nr_latency = sched_nr_latency; |
Peter Zijlstra | 4d78e7b | 2007-10-15 17:00:04 +0200 | [diff] [blame] | 614 | |
| 615 | if (unlikely(nr_running > nr_latency)) { |
Peter Zijlstra | 4bf0b77 | 2008-01-25 21:08:21 +0100 | [diff] [blame] | 616 | period = sysctl_sched_min_granularity; |
Peter Zijlstra | 4d78e7b | 2007-10-15 17:00:04 +0200 | [diff] [blame] | 617 | period *= nr_running; |
Peter Zijlstra | 4d78e7b | 2007-10-15 17:00:04 +0200 | [diff] [blame] | 618 | } |
| 619 | |
| 620 | return period; |
| 621 | } |
| 622 | |
Ingo Molnar | 647e7ca | 2007-10-15 17:00:13 +0200 | [diff] [blame] | 623 | /* |
| 624 | * We calculate the wall-time slice from the period by taking a part |
| 625 | * proportional to the weight. |
| 626 | * |
Peter Zijlstra | f9c0b09 | 2008-10-17 19:27:04 +0200 | [diff] [blame] | 627 | * s = p*P[w/rw] |
Ingo Molnar | 647e7ca | 2007-10-15 17:00:13 +0200 | [diff] [blame] | 628 | */ |
Peter Zijlstra | 6d0f0eb | 2007-10-15 17:00:05 +0200 | [diff] [blame] | 629 | 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] | 630 | { |
Mike Galbraith | 0a58244 | 2009-01-02 12:16:42 +0100 | [diff] [blame] | 631 | u64 slice = __sched_period(cfs_rq->nr_running + !se->on_rq); |
Peter Zijlstra | f9c0b09 | 2008-10-17 19:27:04 +0200 | [diff] [blame] | 632 | |
Mike Galbraith | 0a58244 | 2009-01-02 12:16:42 +0100 | [diff] [blame] | 633 | for_each_sched_entity(se) { |
Lin Ming | 6272d68 | 2009-01-15 17:17:15 +0100 | [diff] [blame] | 634 | struct load_weight *load; |
Christian Engelmayer | 3104bf0 | 2009-06-16 10:35:12 +0200 | [diff] [blame] | 635 | struct load_weight lw; |
Lin Ming | 6272d68 | 2009-01-15 17:17:15 +0100 | [diff] [blame] | 636 | |
| 637 | cfs_rq = cfs_rq_of(se); |
| 638 | load = &cfs_rq->load; |
Peter Zijlstra | f9c0b09 | 2008-10-17 19:27:04 +0200 | [diff] [blame] | 639 | |
Mike Galbraith | 0a58244 | 2009-01-02 12:16:42 +0100 | [diff] [blame] | 640 | if (unlikely(!se->on_rq)) { |
Christian Engelmayer | 3104bf0 | 2009-06-16 10:35:12 +0200 | [diff] [blame] | 641 | lw = cfs_rq->load; |
Mike Galbraith | 0a58244 | 2009-01-02 12:16:42 +0100 | [diff] [blame] | 642 | |
| 643 | update_load_add(&lw, se->load.weight); |
| 644 | load = &lw; |
| 645 | } |
| 646 | slice = calc_delta_mine(slice, se->load.weight, load); |
| 647 | } |
| 648 | return slice; |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 649 | } |
| 650 | |
Ingo Molnar | 647e7ca | 2007-10-15 17:00:13 +0200 | [diff] [blame] | 651 | /* |
Peter Zijlstra | ac884de | 2008-04-19 19:45:00 +0200 | [diff] [blame] | 652 | * We calculate the vruntime slice of a to be inserted task |
Ingo Molnar | 647e7ca | 2007-10-15 17:00:13 +0200 | [diff] [blame] | 653 | * |
Peter Zijlstra | f9c0b09 | 2008-10-17 19:27:04 +0200 | [diff] [blame] | 654 | * vs = s/w |
Ingo Molnar | 647e7ca | 2007-10-15 17:00:13 +0200 | [diff] [blame] | 655 | */ |
Peter Zijlstra | f9c0b09 | 2008-10-17 19:27:04 +0200 | [diff] [blame] | 656 | 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] | 657 | { |
Peter Zijlstra | f9c0b09 | 2008-10-17 19:27:04 +0200 | [diff] [blame] | 658 | return calc_delta_fair(sched_slice(cfs_rq, se), se); |
Peter Zijlstra | a7be37a | 2008-06-27 13:41:11 +0200 | [diff] [blame] | 659 | } |
| 660 | |
Paul Turner | d6b5591 | 2010-11-15 15:47:09 -0800 | [diff] [blame] | 661 | static void update_cfs_load(struct cfs_rq *cfs_rq, int global_update); |
Paul Turner | 6d5ab29 | 2011-01-21 20:45:01 -0800 | [diff] [blame] | 662 | static void update_cfs_shares(struct cfs_rq *cfs_rq); |
Paul Turner | 3b3d190 | 2010-11-15 15:47:08 -0800 | [diff] [blame] | 663 | |
Peter Zijlstra | a7be37a | 2008-06-27 13:41:11 +0200 | [diff] [blame] | 664 | /* |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 665 | * Update the current task's runtime statistics. Skip current tasks that |
| 666 | * are not in our scheduling class. |
| 667 | */ |
| 668 | static inline void |
Ingo Molnar | 8ebc91d | 2007-10-15 17:00:03 +0200 | [diff] [blame] | 669 | __update_curr(struct cfs_rq *cfs_rq, struct sched_entity *curr, |
| 670 | unsigned long delta_exec) |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 671 | { |
Ingo Molnar | bbdba7c | 2007-10-15 17:00:06 +0200 | [diff] [blame] | 672 | unsigned long delta_exec_weighted; |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 673 | |
Lucas De Marchi | 41acab8 | 2010-03-10 23:37:45 -0300 | [diff] [blame] | 674 | schedstat_set(curr->statistics.exec_max, |
| 675 | max((u64)delta_exec, curr->statistics.exec_max)); |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 676 | |
| 677 | curr->sum_exec_runtime += delta_exec; |
Ingo Molnar | 7a62eab | 2007-10-15 17:00:06 +0200 | [diff] [blame] | 678 | schedstat_add(cfs_rq, exec_clock, delta_exec); |
Peter Zijlstra | a7be37a | 2008-06-27 13:41:11 +0200 | [diff] [blame] | 679 | delta_exec_weighted = calc_delta_fair(delta_exec, curr); |
Peter Zijlstra | 88ec22d | 2009-12-16 18:04:41 +0100 | [diff] [blame] | 680 | |
Ingo Molnar | e9acbff | 2007-10-15 17:00:04 +0200 | [diff] [blame] | 681 | curr->vruntime += delta_exec_weighted; |
Peter Zijlstra | 1af5f73 | 2008-10-24 11:06:13 +0200 | [diff] [blame] | 682 | update_min_vruntime(cfs_rq); |
Paul Turner | 3b3d190 | 2010-11-15 15:47:08 -0800 | [diff] [blame] | 683 | |
Peter Zijlstra | 70caf8a | 2010-11-20 00:53:51 +0100 | [diff] [blame] | 684 | #if defined CONFIG_SMP && defined CONFIG_FAIR_GROUP_SCHED |
Paul Turner | 3b3d190 | 2010-11-15 15:47:08 -0800 | [diff] [blame] | 685 | cfs_rq->load_unacc_exec_time += delta_exec; |
Paul Turner | 3b3d190 | 2010-11-15 15:47:08 -0800 | [diff] [blame] | 686 | #endif |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 687 | } |
| 688 | |
Ingo Molnar | b7cc089 | 2007-08-09 11:16:47 +0200 | [diff] [blame] | 689 | static void update_curr(struct cfs_rq *cfs_rq) |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 690 | { |
Ingo Molnar | 429d43b | 2007-10-15 17:00:03 +0200 | [diff] [blame] | 691 | struct sched_entity *curr = cfs_rq->curr; |
Venkatesh Pallipadi | 305e683 | 2010-10-04 17:03:21 -0700 | [diff] [blame] | 692 | u64 now = rq_of(cfs_rq)->clock_task; |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 693 | unsigned long delta_exec; |
| 694 | |
| 695 | if (unlikely(!curr)) |
| 696 | return; |
| 697 | |
| 698 | /* |
| 699 | * Get the amount of time the current task was running |
| 700 | * since the last time we changed load (this cannot |
| 701 | * overflow on 32 bits): |
| 702 | */ |
Ingo Molnar | 8ebc91d | 2007-10-15 17:00:03 +0200 | [diff] [blame] | 703 | delta_exec = (unsigned long)(now - curr->exec_start); |
Peter Zijlstra | 34f28ec | 2008-12-16 08:45:31 +0100 | [diff] [blame] | 704 | if (!delta_exec) |
| 705 | return; |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 706 | |
Ingo Molnar | 8ebc91d | 2007-10-15 17:00:03 +0200 | [diff] [blame] | 707 | __update_curr(cfs_rq, curr, delta_exec); |
| 708 | curr->exec_start = now; |
Srivatsa Vaddagiri | d842de8 | 2007-12-02 20:04:49 +0100 | [diff] [blame] | 709 | |
| 710 | if (entity_is_task(curr)) { |
| 711 | struct task_struct *curtask = task_of(curr); |
| 712 | |
Ingo Molnar | f977bb4 | 2009-09-13 18:15:54 +0200 | [diff] [blame] | 713 | trace_sched_stat_runtime(curtask, delta_exec, curr->vruntime); |
Srivatsa Vaddagiri | d842de8 | 2007-12-02 20:04:49 +0100 | [diff] [blame] | 714 | cpuacct_charge(curtask, delta_exec); |
Frank Mayhar | f06febc | 2008-09-12 09:54:39 -0700 | [diff] [blame] | 715 | account_group_exec_runtime(curtask, delta_exec); |
Srivatsa Vaddagiri | d842de8 | 2007-12-02 20:04:49 +0100 | [diff] [blame] | 716 | } |
Paul Turner | ec12cb7 | 2011-07-21 09:43:30 -0700 | [diff] [blame] | 717 | |
| 718 | account_cfs_rq_runtime(cfs_rq, delta_exec); |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 719 | } |
| 720 | |
| 721 | static inline void |
Ingo Molnar | 5870db5 | 2007-08-09 11:16:47 +0200 | [diff] [blame] | 722 | 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] | 723 | { |
Lucas De Marchi | 41acab8 | 2010-03-10 23:37:45 -0300 | [diff] [blame] | 724 | schedstat_set(se->statistics.wait_start, rq_of(cfs_rq)->clock); |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 725 | } |
| 726 | |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 727 | /* |
| 728 | * Task is being enqueued - update stats: |
| 729 | */ |
Ingo Molnar | d2417e5 | 2007-08-09 11:16:47 +0200 | [diff] [blame] | 730 | 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] | 731 | { |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 732 | /* |
| 733 | * Are we enqueueing a waiting task? (for current tasks |
| 734 | * a dequeue/enqueue event is a NOP) |
| 735 | */ |
Ingo Molnar | 429d43b | 2007-10-15 17:00:03 +0200 | [diff] [blame] | 736 | if (se != cfs_rq->curr) |
Ingo Molnar | 5870db5 | 2007-08-09 11:16:47 +0200 | [diff] [blame] | 737 | update_stats_wait_start(cfs_rq, se); |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 738 | } |
| 739 | |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 740 | static void |
Ingo Molnar | 9ef0a96 | 2007-08-09 11:16:47 +0200 | [diff] [blame] | 741 | 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] | 742 | { |
Lucas De Marchi | 41acab8 | 2010-03-10 23:37:45 -0300 | [diff] [blame] | 743 | schedstat_set(se->statistics.wait_max, max(se->statistics.wait_max, |
| 744 | rq_of(cfs_rq)->clock - se->statistics.wait_start)); |
| 745 | schedstat_set(se->statistics.wait_count, se->statistics.wait_count + 1); |
| 746 | schedstat_set(se->statistics.wait_sum, se->statistics.wait_sum + |
| 747 | rq_of(cfs_rq)->clock - se->statistics.wait_start); |
Peter Zijlstra | 768d0c2 | 2009-07-23 20:13:26 +0200 | [diff] [blame] | 748 | #ifdef CONFIG_SCHEDSTATS |
| 749 | if (entity_is_task(se)) { |
| 750 | trace_sched_stat_wait(task_of(se), |
Lucas De Marchi | 41acab8 | 2010-03-10 23:37:45 -0300 | [diff] [blame] | 751 | rq_of(cfs_rq)->clock - se->statistics.wait_start); |
Peter Zijlstra | 768d0c2 | 2009-07-23 20:13:26 +0200 | [diff] [blame] | 752 | } |
| 753 | #endif |
Lucas De Marchi | 41acab8 | 2010-03-10 23:37:45 -0300 | [diff] [blame] | 754 | schedstat_set(se->statistics.wait_start, 0); |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 755 | } |
| 756 | |
| 757 | static inline void |
Ingo Molnar | 19b6a2e | 2007-08-09 11:16:48 +0200 | [diff] [blame] | 758 | update_stats_dequeue(struct cfs_rq *cfs_rq, struct sched_entity *se) |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 759 | { |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 760 | /* |
| 761 | * Mark the end of the wait period if dequeueing a |
| 762 | * waiting task: |
| 763 | */ |
Ingo Molnar | 429d43b | 2007-10-15 17:00:03 +0200 | [diff] [blame] | 764 | if (se != cfs_rq->curr) |
Ingo Molnar | 9ef0a96 | 2007-08-09 11:16:47 +0200 | [diff] [blame] | 765 | update_stats_wait_end(cfs_rq, se); |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 766 | } |
| 767 | |
| 768 | /* |
| 769 | * We are picking a new current task - update its stats: |
| 770 | */ |
| 771 | static inline void |
Ingo Molnar | 79303e9 | 2007-08-09 11:16:47 +0200 | [diff] [blame] | 772 | 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] | 773 | { |
| 774 | /* |
| 775 | * We are starting a new run period: |
| 776 | */ |
Venkatesh Pallipadi | 305e683 | 2010-10-04 17:03:21 -0700 | [diff] [blame] | 777 | se->exec_start = rq_of(cfs_rq)->clock_task; |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 778 | } |
| 779 | |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 780 | /************************************************** |
| 781 | * Scheduling class queueing methods: |
| 782 | */ |
| 783 | |
Dmitry Adamushko | 30cfdcf | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 784 | static void |
| 785 | account_entity_enqueue(struct cfs_rq *cfs_rq, struct sched_entity *se) |
| 786 | { |
| 787 | update_load_add(&cfs_rq->load, se->load.weight); |
Peter Zijlstra | c09595f | 2008-06-27 13:41:14 +0200 | [diff] [blame] | 788 | if (!parent_entity(se)) |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 789 | update_load_add(&rq_of(cfs_rq)->load, se->load.weight); |
Peter Zijlstra | 367456c | 2012-02-20 21:49:09 +0100 | [diff] [blame] | 790 | #ifdef CONFIG_SMP |
| 791 | if (entity_is_task(se)) |
Peter Zijlstra | eb95308 | 2012-04-17 13:38:40 +0200 | [diff] [blame] | 792 | list_add(&se->group_node, &rq_of(cfs_rq)->cfs_tasks); |
Peter Zijlstra | 367456c | 2012-02-20 21:49:09 +0100 | [diff] [blame] | 793 | #endif |
Dmitry Adamushko | 30cfdcf | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 794 | cfs_rq->nr_running++; |
Dmitry Adamushko | 30cfdcf | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 795 | } |
| 796 | |
| 797 | static void |
| 798 | account_entity_dequeue(struct cfs_rq *cfs_rq, struct sched_entity *se) |
| 799 | { |
| 800 | update_load_sub(&cfs_rq->load, se->load.weight); |
Peter Zijlstra | c09595f | 2008-06-27 13:41:14 +0200 | [diff] [blame] | 801 | if (!parent_entity(se)) |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 802 | update_load_sub(&rq_of(cfs_rq)->load, se->load.weight); |
Peter Zijlstra | 367456c | 2012-02-20 21:49:09 +0100 | [diff] [blame] | 803 | if (entity_is_task(se)) |
Bharata B Rao | b87f172 | 2008-09-25 09:53:54 +0530 | [diff] [blame] | 804 | list_del_init(&se->group_node); |
Dmitry Adamushko | 30cfdcf | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 805 | cfs_rq->nr_running--; |
Dmitry Adamushko | 30cfdcf | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 806 | } |
| 807 | |
Yong Zhang | 3ff6dca | 2011-01-24 15:33:52 +0800 | [diff] [blame] | 808 | #ifdef CONFIG_FAIR_GROUP_SCHED |
Paul Turner | 64660c8 | 2011-07-21 09:43:36 -0700 | [diff] [blame] | 809 | /* we need this in update_cfs_load and load-balance functions below */ |
| 810 | static inline int throttled_hierarchy(struct cfs_rq *cfs_rq); |
Yong Zhang | 3ff6dca | 2011-01-24 15:33:52 +0800 | [diff] [blame] | 811 | # ifdef CONFIG_SMP |
Paul Turner | d6b5591 | 2010-11-15 15:47:09 -0800 | [diff] [blame] | 812 | static void update_cfs_rq_load_contribution(struct cfs_rq *cfs_rq, |
| 813 | int global_update) |
| 814 | { |
| 815 | struct task_group *tg = cfs_rq->tg; |
| 816 | long load_avg; |
| 817 | |
| 818 | load_avg = div64_u64(cfs_rq->load_avg, cfs_rq->load_period+1); |
| 819 | load_avg -= cfs_rq->load_contribution; |
| 820 | |
| 821 | if (global_update || abs(load_avg) > cfs_rq->load_contribution / 8) { |
| 822 | atomic_add(load_avg, &tg->load_weight); |
| 823 | cfs_rq->load_contribution += load_avg; |
| 824 | } |
| 825 | } |
| 826 | |
| 827 | static void update_cfs_load(struct cfs_rq *cfs_rq, int global_update) |
Peter Zijlstra | 2069dd7 | 2010-11-15 15:47:00 -0800 | [diff] [blame] | 828 | { |
Paul Turner | a7a4f8a | 2010-11-15 15:47:06 -0800 | [diff] [blame] | 829 | u64 period = sysctl_sched_shares_window; |
Peter Zijlstra | 2069dd7 | 2010-11-15 15:47:00 -0800 | [diff] [blame] | 830 | u64 now, delta; |
Paul Turner | e33078b | 2010-11-15 15:47:04 -0800 | [diff] [blame] | 831 | unsigned long load = cfs_rq->load.weight; |
Peter Zijlstra | 2069dd7 | 2010-11-15 15:47:00 -0800 | [diff] [blame] | 832 | |
Paul Turner | 64660c8 | 2011-07-21 09:43:36 -0700 | [diff] [blame] | 833 | if (cfs_rq->tg == &root_task_group || throttled_hierarchy(cfs_rq)) |
Peter Zijlstra | 2069dd7 | 2010-11-15 15:47:00 -0800 | [diff] [blame] | 834 | return; |
| 835 | |
Paul Turner | 05ca62c | 2011-01-21 20:45:02 -0800 | [diff] [blame] | 836 | now = rq_of(cfs_rq)->clock_task; |
Peter Zijlstra | 2069dd7 | 2010-11-15 15:47:00 -0800 | [diff] [blame] | 837 | delta = now - cfs_rq->load_stamp; |
| 838 | |
Paul Turner | e33078b | 2010-11-15 15:47:04 -0800 | [diff] [blame] | 839 | /* truncate load history at 4 idle periods */ |
| 840 | if (cfs_rq->load_stamp > cfs_rq->load_last && |
| 841 | now - cfs_rq->load_last > 4 * period) { |
| 842 | cfs_rq->load_period = 0; |
| 843 | cfs_rq->load_avg = 0; |
Paul Turner | f07333b | 2011-01-21 20:45:03 -0800 | [diff] [blame] | 844 | delta = period - 1; |
Paul Turner | e33078b | 2010-11-15 15:47:04 -0800 | [diff] [blame] | 845 | } |
| 846 | |
Peter Zijlstra | 2069dd7 | 2010-11-15 15:47:00 -0800 | [diff] [blame] | 847 | cfs_rq->load_stamp = now; |
Paul Turner | 3b3d190 | 2010-11-15 15:47:08 -0800 | [diff] [blame] | 848 | cfs_rq->load_unacc_exec_time = 0; |
Peter Zijlstra | 2069dd7 | 2010-11-15 15:47:00 -0800 | [diff] [blame] | 849 | cfs_rq->load_period += delta; |
Paul Turner | e33078b | 2010-11-15 15:47:04 -0800 | [diff] [blame] | 850 | if (load) { |
| 851 | cfs_rq->load_last = now; |
| 852 | cfs_rq->load_avg += delta * load; |
| 853 | } |
Peter Zijlstra | 2069dd7 | 2010-11-15 15:47:00 -0800 | [diff] [blame] | 854 | |
Paul Turner | d6b5591 | 2010-11-15 15:47:09 -0800 | [diff] [blame] | 855 | /* consider updating load contribution on each fold or truncate */ |
| 856 | if (global_update || cfs_rq->load_period > period |
| 857 | || !cfs_rq->load_period) |
| 858 | update_cfs_rq_load_contribution(cfs_rq, global_update); |
| 859 | |
Peter Zijlstra | 2069dd7 | 2010-11-15 15:47:00 -0800 | [diff] [blame] | 860 | while (cfs_rq->load_period > period) { |
| 861 | /* |
| 862 | * Inline assembly required to prevent the compiler |
| 863 | * optimising this loop into a divmod call. |
| 864 | * See __iter_div_u64_rem() for another example of this. |
| 865 | */ |
| 866 | asm("" : "+rm" (cfs_rq->load_period)); |
| 867 | cfs_rq->load_period /= 2; |
| 868 | cfs_rq->load_avg /= 2; |
| 869 | } |
Peter Zijlstra | 3d4b47b | 2010-11-15 15:47:01 -0800 | [diff] [blame] | 870 | |
Paul Turner | e33078b | 2010-11-15 15:47:04 -0800 | [diff] [blame] | 871 | if (!cfs_rq->curr && !cfs_rq->nr_running && !cfs_rq->load_avg) |
| 872 | list_del_leaf_cfs_rq(cfs_rq); |
Peter Zijlstra | 2069dd7 | 2010-11-15 15:47:00 -0800 | [diff] [blame] | 873 | } |
| 874 | |
Peter Zijlstra | cf5f0ac | 2011-10-13 16:52:28 +0200 | [diff] [blame] | 875 | static inline long calc_tg_weight(struct task_group *tg, struct cfs_rq *cfs_rq) |
| 876 | { |
| 877 | long tg_weight; |
| 878 | |
| 879 | /* |
| 880 | * Use this CPU's actual weight instead of the last load_contribution |
| 881 | * to gain a more accurate current total weight. See |
| 882 | * update_cfs_rq_load_contribution(). |
| 883 | */ |
| 884 | tg_weight = atomic_read(&tg->load_weight); |
| 885 | tg_weight -= cfs_rq->load_contribution; |
| 886 | tg_weight += cfs_rq->load.weight; |
| 887 | |
| 888 | return tg_weight; |
| 889 | } |
| 890 | |
Paul Turner | 6d5ab29 | 2011-01-21 20:45:01 -0800 | [diff] [blame] | 891 | static long calc_cfs_shares(struct cfs_rq *cfs_rq, struct task_group *tg) |
Yong Zhang | 3ff6dca | 2011-01-24 15:33:52 +0800 | [diff] [blame] | 892 | { |
Peter Zijlstra | cf5f0ac | 2011-10-13 16:52:28 +0200 | [diff] [blame] | 893 | long tg_weight, load, shares; |
Yong Zhang | 3ff6dca | 2011-01-24 15:33:52 +0800 | [diff] [blame] | 894 | |
Peter Zijlstra | cf5f0ac | 2011-10-13 16:52:28 +0200 | [diff] [blame] | 895 | tg_weight = calc_tg_weight(tg, cfs_rq); |
Paul Turner | 6d5ab29 | 2011-01-21 20:45:01 -0800 | [diff] [blame] | 896 | load = cfs_rq->load.weight; |
Yong Zhang | 3ff6dca | 2011-01-24 15:33:52 +0800 | [diff] [blame] | 897 | |
Yong Zhang | 3ff6dca | 2011-01-24 15:33:52 +0800 | [diff] [blame] | 898 | shares = (tg->shares * load); |
Peter Zijlstra | cf5f0ac | 2011-10-13 16:52:28 +0200 | [diff] [blame] | 899 | if (tg_weight) |
| 900 | shares /= tg_weight; |
Yong Zhang | 3ff6dca | 2011-01-24 15:33:52 +0800 | [diff] [blame] | 901 | |
| 902 | if (shares < MIN_SHARES) |
| 903 | shares = MIN_SHARES; |
| 904 | if (shares > tg->shares) |
| 905 | shares = tg->shares; |
| 906 | |
| 907 | return shares; |
| 908 | } |
| 909 | |
| 910 | static void update_entity_shares_tick(struct cfs_rq *cfs_rq) |
| 911 | { |
| 912 | if (cfs_rq->load_unacc_exec_time > sysctl_sched_shares_window) { |
| 913 | update_cfs_load(cfs_rq, 0); |
Paul Turner | 6d5ab29 | 2011-01-21 20:45:01 -0800 | [diff] [blame] | 914 | update_cfs_shares(cfs_rq); |
Yong Zhang | 3ff6dca | 2011-01-24 15:33:52 +0800 | [diff] [blame] | 915 | } |
| 916 | } |
| 917 | # else /* CONFIG_SMP */ |
| 918 | static void update_cfs_load(struct cfs_rq *cfs_rq, int global_update) |
| 919 | { |
| 920 | } |
| 921 | |
Paul Turner | 6d5ab29 | 2011-01-21 20:45:01 -0800 | [diff] [blame] | 922 | static inline long calc_cfs_shares(struct cfs_rq *cfs_rq, struct task_group *tg) |
Yong Zhang | 3ff6dca | 2011-01-24 15:33:52 +0800 | [diff] [blame] | 923 | { |
| 924 | return tg->shares; |
| 925 | } |
| 926 | |
| 927 | static inline void update_entity_shares_tick(struct cfs_rq *cfs_rq) |
| 928 | { |
| 929 | } |
| 930 | # endif /* CONFIG_SMP */ |
Peter Zijlstra | 2069dd7 | 2010-11-15 15:47:00 -0800 | [diff] [blame] | 931 | static void reweight_entity(struct cfs_rq *cfs_rq, struct sched_entity *se, |
| 932 | unsigned long weight) |
| 933 | { |
Paul Turner | 19e5eeb | 2010-12-15 19:10:18 -0800 | [diff] [blame] | 934 | if (se->on_rq) { |
| 935 | /* commit outstanding execution time */ |
| 936 | if (cfs_rq->curr == se) |
| 937 | update_curr(cfs_rq); |
Peter Zijlstra | 2069dd7 | 2010-11-15 15:47:00 -0800 | [diff] [blame] | 938 | account_entity_dequeue(cfs_rq, se); |
Paul Turner | 19e5eeb | 2010-12-15 19:10:18 -0800 | [diff] [blame] | 939 | } |
Peter Zijlstra | 2069dd7 | 2010-11-15 15:47:00 -0800 | [diff] [blame] | 940 | |
| 941 | update_load_set(&se->load, weight); |
| 942 | |
| 943 | if (se->on_rq) |
| 944 | account_entity_enqueue(cfs_rq, se); |
| 945 | } |
| 946 | |
Paul Turner | 6d5ab29 | 2011-01-21 20:45:01 -0800 | [diff] [blame] | 947 | static void update_cfs_shares(struct cfs_rq *cfs_rq) |
Peter Zijlstra | 2069dd7 | 2010-11-15 15:47:00 -0800 | [diff] [blame] | 948 | { |
| 949 | struct task_group *tg; |
| 950 | struct sched_entity *se; |
Yong Zhang | 3ff6dca | 2011-01-24 15:33:52 +0800 | [diff] [blame] | 951 | long shares; |
Peter Zijlstra | 2069dd7 | 2010-11-15 15:47:00 -0800 | [diff] [blame] | 952 | |
Peter Zijlstra | 2069dd7 | 2010-11-15 15:47:00 -0800 | [diff] [blame] | 953 | tg = cfs_rq->tg; |
| 954 | se = tg->se[cpu_of(rq_of(cfs_rq))]; |
Paul Turner | 64660c8 | 2011-07-21 09:43:36 -0700 | [diff] [blame] | 955 | if (!se || throttled_hierarchy(cfs_rq)) |
Peter Zijlstra | 2069dd7 | 2010-11-15 15:47:00 -0800 | [diff] [blame] | 956 | return; |
Yong Zhang | 3ff6dca | 2011-01-24 15:33:52 +0800 | [diff] [blame] | 957 | #ifndef CONFIG_SMP |
| 958 | if (likely(se->load.weight == tg->shares)) |
| 959 | return; |
| 960 | #endif |
Paul Turner | 6d5ab29 | 2011-01-21 20:45:01 -0800 | [diff] [blame] | 961 | shares = calc_cfs_shares(cfs_rq, tg); |
Peter Zijlstra | 2069dd7 | 2010-11-15 15:47:00 -0800 | [diff] [blame] | 962 | |
| 963 | reweight_entity(cfs_rq_of(se), se, shares); |
| 964 | } |
| 965 | #else /* CONFIG_FAIR_GROUP_SCHED */ |
Paul Turner | d6b5591 | 2010-11-15 15:47:09 -0800 | [diff] [blame] | 966 | static void update_cfs_load(struct cfs_rq *cfs_rq, int global_update) |
Peter Zijlstra | 2069dd7 | 2010-11-15 15:47:00 -0800 | [diff] [blame] | 967 | { |
| 968 | } |
| 969 | |
Paul Turner | 6d5ab29 | 2011-01-21 20:45:01 -0800 | [diff] [blame] | 970 | static inline void update_cfs_shares(struct cfs_rq *cfs_rq) |
Peter Zijlstra | 2069dd7 | 2010-11-15 15:47:00 -0800 | [diff] [blame] | 971 | { |
| 972 | } |
Paul Turner | 43365bd | 2010-12-15 19:10:17 -0800 | [diff] [blame] | 973 | |
| 974 | static inline void update_entity_shares_tick(struct cfs_rq *cfs_rq) |
| 975 | { |
| 976 | } |
Peter Zijlstra | 2069dd7 | 2010-11-15 15:47:00 -0800 | [diff] [blame] | 977 | #endif /* CONFIG_FAIR_GROUP_SCHED */ |
| 978 | |
Paul Turner | 9d85f21 | 2012-10-04 13:18:29 +0200 | [diff] [blame] | 979 | #ifdef CONFIG_SMP |
| 980 | /* |
| 981 | * Approximate: |
| 982 | * val * y^n, where y^32 ~= 0.5 (~1 scheduling period) |
| 983 | */ |
| 984 | static __always_inline u64 decay_load(u64 val, u64 n) |
| 985 | { |
| 986 | for (; n && val; n--) { |
| 987 | val *= 4008; |
| 988 | val >>= 12; |
| 989 | } |
| 990 | |
| 991 | return val; |
| 992 | } |
| 993 | |
| 994 | /* |
| 995 | * We can represent the historical contribution to runnable average as the |
| 996 | * coefficients of a geometric series. To do this we sub-divide our runnable |
| 997 | * history into segments of approximately 1ms (1024us); label the segment that |
| 998 | * occurred N-ms ago p_N, with p_0 corresponding to the current period, e.g. |
| 999 | * |
| 1000 | * [<- 1024us ->|<- 1024us ->|<- 1024us ->| ... |
| 1001 | * p0 p1 p2 |
| 1002 | * (now) (~1ms ago) (~2ms ago) |
| 1003 | * |
| 1004 | * Let u_i denote the fraction of p_i that the entity was runnable. |
| 1005 | * |
| 1006 | * We then designate the fractions u_i as our co-efficients, yielding the |
| 1007 | * following representation of historical load: |
| 1008 | * u_0 + u_1*y + u_2*y^2 + u_3*y^3 + ... |
| 1009 | * |
| 1010 | * We choose y based on the with of a reasonably scheduling period, fixing: |
| 1011 | * y^32 = 0.5 |
| 1012 | * |
| 1013 | * This means that the contribution to load ~32ms ago (u_32) will be weighted |
| 1014 | * approximately half as much as the contribution to load within the last ms |
| 1015 | * (u_0). |
| 1016 | * |
| 1017 | * When a period "rolls over" and we have new u_0`, multiplying the previous |
| 1018 | * sum again by y is sufficient to update: |
| 1019 | * load_avg = u_0` + y*(u_0 + u_1*y + u_2*y^2 + ... ) |
| 1020 | * = u_0 + u_1*y + u_2*y^2 + ... [re-labeling u_i --> u_{i+1}] |
| 1021 | */ |
| 1022 | static __always_inline int __update_entity_runnable_avg(u64 now, |
| 1023 | struct sched_avg *sa, |
| 1024 | int runnable) |
| 1025 | { |
| 1026 | u64 delta; |
| 1027 | int delta_w, decayed = 0; |
| 1028 | |
| 1029 | delta = now - sa->last_runnable_update; |
| 1030 | /* |
| 1031 | * This should only happen when time goes backwards, which it |
| 1032 | * unfortunately does during sched clock init when we swap over to TSC. |
| 1033 | */ |
| 1034 | if ((s64)delta < 0) { |
| 1035 | sa->last_runnable_update = now; |
| 1036 | return 0; |
| 1037 | } |
| 1038 | |
| 1039 | /* |
| 1040 | * Use 1024ns as the unit of measurement since it's a reasonable |
| 1041 | * approximation of 1us and fast to compute. |
| 1042 | */ |
| 1043 | delta >>= 10; |
| 1044 | if (!delta) |
| 1045 | return 0; |
| 1046 | sa->last_runnable_update = now; |
| 1047 | |
| 1048 | /* delta_w is the amount already accumulated against our next period */ |
| 1049 | delta_w = sa->runnable_avg_period % 1024; |
| 1050 | if (delta + delta_w >= 1024) { |
| 1051 | /* period roll-over */ |
| 1052 | decayed = 1; |
| 1053 | |
| 1054 | /* |
| 1055 | * Now that we know we're crossing a period boundary, figure |
| 1056 | * out how much from delta we need to complete the current |
| 1057 | * period and accrue it. |
| 1058 | */ |
| 1059 | delta_w = 1024 - delta_w; |
| 1060 | BUG_ON(delta_w > delta); |
| 1061 | do { |
| 1062 | if (runnable) |
| 1063 | sa->runnable_avg_sum += delta_w; |
| 1064 | sa->runnable_avg_period += delta_w; |
| 1065 | |
| 1066 | /* |
| 1067 | * Remainder of delta initiates a new period, roll over |
| 1068 | * the previous. |
| 1069 | */ |
| 1070 | sa->runnable_avg_sum = |
| 1071 | decay_load(sa->runnable_avg_sum, 1); |
| 1072 | sa->runnable_avg_period = |
| 1073 | decay_load(sa->runnable_avg_period, 1); |
| 1074 | |
| 1075 | delta -= delta_w; |
| 1076 | /* New period is empty */ |
| 1077 | delta_w = 1024; |
| 1078 | } while (delta >= 1024); |
| 1079 | } |
| 1080 | |
| 1081 | /* Remainder of delta accrued against u_0` */ |
| 1082 | if (runnable) |
| 1083 | sa->runnable_avg_sum += delta; |
| 1084 | sa->runnable_avg_period += delta; |
| 1085 | |
| 1086 | return decayed; |
| 1087 | } |
| 1088 | |
Paul Turner | 9ee474f | 2012-10-04 13:18:30 +0200 | [diff] [blame] | 1089 | /* Synchronize an entity's decay with its parenting cfs_rq.*/ |
Paul Turner | aff3e49 | 2012-10-04 13:18:30 +0200 | [diff] [blame] | 1090 | static inline u64 __synchronize_entity_decay(struct sched_entity *se) |
Paul Turner | 9ee474f | 2012-10-04 13:18:30 +0200 | [diff] [blame] | 1091 | { |
| 1092 | struct cfs_rq *cfs_rq = cfs_rq_of(se); |
| 1093 | u64 decays = atomic64_read(&cfs_rq->decay_counter); |
| 1094 | |
| 1095 | decays -= se->avg.decay_count; |
| 1096 | if (!decays) |
Paul Turner | aff3e49 | 2012-10-04 13:18:30 +0200 | [diff] [blame] | 1097 | return 0; |
Paul Turner | 9ee474f | 2012-10-04 13:18:30 +0200 | [diff] [blame] | 1098 | |
| 1099 | se->avg.load_avg_contrib = decay_load(se->avg.load_avg_contrib, decays); |
| 1100 | se->avg.decay_count = 0; |
Paul Turner | aff3e49 | 2012-10-04 13:18:30 +0200 | [diff] [blame] | 1101 | |
| 1102 | return decays; |
Paul Turner | 9ee474f | 2012-10-04 13:18:30 +0200 | [diff] [blame] | 1103 | } |
| 1104 | |
Paul Turner | c566e8e | 2012-10-04 13:18:30 +0200 | [diff] [blame^] | 1105 | #ifdef CONFIG_FAIR_GROUP_SCHED |
| 1106 | static inline void __update_cfs_rq_tg_load_contrib(struct cfs_rq *cfs_rq, |
| 1107 | int force_update) |
| 1108 | { |
| 1109 | struct task_group *tg = cfs_rq->tg; |
| 1110 | s64 tg_contrib; |
| 1111 | |
| 1112 | tg_contrib = cfs_rq->runnable_load_avg + cfs_rq->blocked_load_avg; |
| 1113 | tg_contrib -= cfs_rq->tg_load_contrib; |
| 1114 | |
| 1115 | if (force_update || abs64(tg_contrib) > cfs_rq->tg_load_contrib / 8) { |
| 1116 | atomic64_add(tg_contrib, &tg->load_avg); |
| 1117 | cfs_rq->tg_load_contrib += tg_contrib; |
| 1118 | } |
| 1119 | } |
| 1120 | #else |
| 1121 | static inline void __update_cfs_rq_tg_load_contrib(struct cfs_rq *cfs_rq, |
| 1122 | int force_update) {} |
| 1123 | #endif |
| 1124 | |
Paul Turner | 2dac754 | 2012-10-04 13:18:30 +0200 | [diff] [blame] | 1125 | /* Compute the current contribution to load_avg by se, return any delta */ |
| 1126 | static long __update_entity_load_avg_contrib(struct sched_entity *se) |
| 1127 | { |
| 1128 | long old_contrib = se->avg.load_avg_contrib; |
| 1129 | |
| 1130 | if (!entity_is_task(se)) |
| 1131 | return 0; |
| 1132 | |
| 1133 | se->avg.load_avg_contrib = div64_u64(se->avg.runnable_avg_sum * |
| 1134 | se->load.weight, |
| 1135 | se->avg.runnable_avg_period + 1); |
| 1136 | |
| 1137 | return se->avg.load_avg_contrib - old_contrib; |
| 1138 | } |
| 1139 | |
Paul Turner | 9ee474f | 2012-10-04 13:18:30 +0200 | [diff] [blame] | 1140 | static inline void subtract_blocked_load_contrib(struct cfs_rq *cfs_rq, |
| 1141 | long load_contrib) |
| 1142 | { |
| 1143 | if (likely(load_contrib < cfs_rq->blocked_load_avg)) |
| 1144 | cfs_rq->blocked_load_avg -= load_contrib; |
| 1145 | else |
| 1146 | cfs_rq->blocked_load_avg = 0; |
| 1147 | } |
| 1148 | |
Paul Turner | 9d85f21 | 2012-10-04 13:18:29 +0200 | [diff] [blame] | 1149 | /* Update a sched_entity's runnable average */ |
Paul Turner | 9ee474f | 2012-10-04 13:18:30 +0200 | [diff] [blame] | 1150 | static inline void update_entity_load_avg(struct sched_entity *se, |
| 1151 | int update_cfs_rq) |
Paul Turner | 9d85f21 | 2012-10-04 13:18:29 +0200 | [diff] [blame] | 1152 | { |
Paul Turner | 2dac754 | 2012-10-04 13:18:30 +0200 | [diff] [blame] | 1153 | struct cfs_rq *cfs_rq = cfs_rq_of(se); |
| 1154 | long contrib_delta; |
| 1155 | |
| 1156 | if (!__update_entity_runnable_avg(rq_of(cfs_rq)->clock_task, &se->avg, |
| 1157 | se->on_rq)) |
| 1158 | return; |
| 1159 | |
| 1160 | contrib_delta = __update_entity_load_avg_contrib(se); |
Paul Turner | 9ee474f | 2012-10-04 13:18:30 +0200 | [diff] [blame] | 1161 | |
| 1162 | if (!update_cfs_rq) |
| 1163 | return; |
| 1164 | |
Paul Turner | 2dac754 | 2012-10-04 13:18:30 +0200 | [diff] [blame] | 1165 | if (se->on_rq) |
| 1166 | cfs_rq->runnable_load_avg += contrib_delta; |
Paul Turner | 9ee474f | 2012-10-04 13:18:30 +0200 | [diff] [blame] | 1167 | else |
| 1168 | subtract_blocked_load_contrib(cfs_rq, -contrib_delta); |
| 1169 | } |
| 1170 | |
| 1171 | /* |
| 1172 | * Decay the load contributed by all blocked children and account this so that |
| 1173 | * their contribution may appropriately discounted when they wake up. |
| 1174 | */ |
Paul Turner | aff3e49 | 2012-10-04 13:18:30 +0200 | [diff] [blame] | 1175 | static void update_cfs_rq_blocked_load(struct cfs_rq *cfs_rq, int force_update) |
Paul Turner | 9ee474f | 2012-10-04 13:18:30 +0200 | [diff] [blame] | 1176 | { |
| 1177 | u64 now = rq_of(cfs_rq)->clock_task >> 20; |
| 1178 | u64 decays; |
| 1179 | |
| 1180 | decays = now - cfs_rq->last_decay; |
Paul Turner | aff3e49 | 2012-10-04 13:18:30 +0200 | [diff] [blame] | 1181 | if (!decays && !force_update) |
Paul Turner | 9ee474f | 2012-10-04 13:18:30 +0200 | [diff] [blame] | 1182 | return; |
| 1183 | |
Paul Turner | aff3e49 | 2012-10-04 13:18:30 +0200 | [diff] [blame] | 1184 | if (atomic64_read(&cfs_rq->removed_load)) { |
| 1185 | u64 removed_load = atomic64_xchg(&cfs_rq->removed_load, 0); |
| 1186 | subtract_blocked_load_contrib(cfs_rq, removed_load); |
| 1187 | } |
Paul Turner | 9ee474f | 2012-10-04 13:18:30 +0200 | [diff] [blame] | 1188 | |
Paul Turner | aff3e49 | 2012-10-04 13:18:30 +0200 | [diff] [blame] | 1189 | if (decays) { |
| 1190 | cfs_rq->blocked_load_avg = decay_load(cfs_rq->blocked_load_avg, |
| 1191 | decays); |
| 1192 | atomic64_add(decays, &cfs_rq->decay_counter); |
| 1193 | cfs_rq->last_decay = now; |
| 1194 | } |
Paul Turner | c566e8e | 2012-10-04 13:18:30 +0200 | [diff] [blame^] | 1195 | |
| 1196 | __update_cfs_rq_tg_load_contrib(cfs_rq, force_update); |
Paul Turner | 9d85f21 | 2012-10-04 13:18:29 +0200 | [diff] [blame] | 1197 | } |
Ben Segall | 18bf280 | 2012-10-04 12:51:20 +0200 | [diff] [blame] | 1198 | |
| 1199 | static inline void update_rq_runnable_avg(struct rq *rq, int runnable) |
| 1200 | { |
| 1201 | __update_entity_runnable_avg(rq->clock_task, &rq->avg, runnable); |
| 1202 | } |
Paul Turner | 2dac754 | 2012-10-04 13:18:30 +0200 | [diff] [blame] | 1203 | |
| 1204 | /* Add the load generated by se into cfs_rq's child load-average */ |
| 1205 | static inline void enqueue_entity_load_avg(struct cfs_rq *cfs_rq, |
Paul Turner | 9ee474f | 2012-10-04 13:18:30 +0200 | [diff] [blame] | 1206 | struct sched_entity *se, |
| 1207 | int wakeup) |
Paul Turner | 2dac754 | 2012-10-04 13:18:30 +0200 | [diff] [blame] | 1208 | { |
Paul Turner | aff3e49 | 2012-10-04 13:18:30 +0200 | [diff] [blame] | 1209 | /* |
| 1210 | * We track migrations using entity decay_count <= 0, on a wake-up |
| 1211 | * migration we use a negative decay count to track the remote decays |
| 1212 | * accumulated while sleeping. |
| 1213 | */ |
| 1214 | if (unlikely(se->avg.decay_count <= 0)) { |
Paul Turner | 9ee474f | 2012-10-04 13:18:30 +0200 | [diff] [blame] | 1215 | se->avg.last_runnable_update = rq_of(cfs_rq)->clock_task; |
Paul Turner | aff3e49 | 2012-10-04 13:18:30 +0200 | [diff] [blame] | 1216 | if (se->avg.decay_count) { |
| 1217 | /* |
| 1218 | * In a wake-up migration we have to approximate the |
| 1219 | * time sleeping. This is because we can't synchronize |
| 1220 | * clock_task between the two cpus, and it is not |
| 1221 | * guaranteed to be read-safe. Instead, we can |
| 1222 | * approximate this using our carried decays, which are |
| 1223 | * explicitly atomically readable. |
| 1224 | */ |
| 1225 | se->avg.last_runnable_update -= (-se->avg.decay_count) |
| 1226 | << 20; |
| 1227 | update_entity_load_avg(se, 0); |
| 1228 | /* Indicate that we're now synchronized and on-rq */ |
| 1229 | se->avg.decay_count = 0; |
| 1230 | } |
Paul Turner | 9ee474f | 2012-10-04 13:18:30 +0200 | [diff] [blame] | 1231 | wakeup = 0; |
| 1232 | } else { |
| 1233 | __synchronize_entity_decay(se); |
| 1234 | } |
| 1235 | |
Paul Turner | aff3e49 | 2012-10-04 13:18:30 +0200 | [diff] [blame] | 1236 | /* migrated tasks did not contribute to our blocked load */ |
| 1237 | if (wakeup) { |
Paul Turner | 9ee474f | 2012-10-04 13:18:30 +0200 | [diff] [blame] | 1238 | subtract_blocked_load_contrib(cfs_rq, se->avg.load_avg_contrib); |
Paul Turner | aff3e49 | 2012-10-04 13:18:30 +0200 | [diff] [blame] | 1239 | update_entity_load_avg(se, 0); |
| 1240 | } |
Paul Turner | 9ee474f | 2012-10-04 13:18:30 +0200 | [diff] [blame] | 1241 | |
Paul Turner | 2dac754 | 2012-10-04 13:18:30 +0200 | [diff] [blame] | 1242 | cfs_rq->runnable_load_avg += se->avg.load_avg_contrib; |
Paul Turner | aff3e49 | 2012-10-04 13:18:30 +0200 | [diff] [blame] | 1243 | /* we force update consideration on load-balancer moves */ |
| 1244 | update_cfs_rq_blocked_load(cfs_rq, !wakeup); |
Paul Turner | 2dac754 | 2012-10-04 13:18:30 +0200 | [diff] [blame] | 1245 | } |
| 1246 | |
Paul Turner | 9ee474f | 2012-10-04 13:18:30 +0200 | [diff] [blame] | 1247 | /* |
| 1248 | * Remove se's load from this cfs_rq child load-average, if the entity is |
| 1249 | * transitioning to a blocked state we track its projected decay using |
| 1250 | * blocked_load_avg. |
| 1251 | */ |
Paul Turner | 2dac754 | 2012-10-04 13:18:30 +0200 | [diff] [blame] | 1252 | static inline void dequeue_entity_load_avg(struct cfs_rq *cfs_rq, |
Paul Turner | 9ee474f | 2012-10-04 13:18:30 +0200 | [diff] [blame] | 1253 | struct sched_entity *se, |
| 1254 | int sleep) |
Paul Turner | 2dac754 | 2012-10-04 13:18:30 +0200 | [diff] [blame] | 1255 | { |
Paul Turner | 9ee474f | 2012-10-04 13:18:30 +0200 | [diff] [blame] | 1256 | update_entity_load_avg(se, 1); |
Paul Turner | aff3e49 | 2012-10-04 13:18:30 +0200 | [diff] [blame] | 1257 | /* we force update consideration on load-balancer moves */ |
| 1258 | update_cfs_rq_blocked_load(cfs_rq, !sleep); |
Paul Turner | 9ee474f | 2012-10-04 13:18:30 +0200 | [diff] [blame] | 1259 | |
Paul Turner | 2dac754 | 2012-10-04 13:18:30 +0200 | [diff] [blame] | 1260 | cfs_rq->runnable_load_avg -= se->avg.load_avg_contrib; |
Paul Turner | 9ee474f | 2012-10-04 13:18:30 +0200 | [diff] [blame] | 1261 | if (sleep) { |
| 1262 | cfs_rq->blocked_load_avg += se->avg.load_avg_contrib; |
| 1263 | se->avg.decay_count = atomic64_read(&cfs_rq->decay_counter); |
| 1264 | } /* migrations, e.g. sleep=0 leave decay_count == 0 */ |
Paul Turner | 2dac754 | 2012-10-04 13:18:30 +0200 | [diff] [blame] | 1265 | } |
Paul Turner | 9d85f21 | 2012-10-04 13:18:29 +0200 | [diff] [blame] | 1266 | #else |
Paul Turner | 9ee474f | 2012-10-04 13:18:30 +0200 | [diff] [blame] | 1267 | static inline void update_entity_load_avg(struct sched_entity *se, |
| 1268 | int update_cfs_rq) {} |
Ben Segall | 18bf280 | 2012-10-04 12:51:20 +0200 | [diff] [blame] | 1269 | static inline void update_rq_runnable_avg(struct rq *rq, int runnable) {} |
Paul Turner | 2dac754 | 2012-10-04 13:18:30 +0200 | [diff] [blame] | 1270 | static inline void enqueue_entity_load_avg(struct cfs_rq *cfs_rq, |
Paul Turner | 9ee474f | 2012-10-04 13:18:30 +0200 | [diff] [blame] | 1271 | struct sched_entity *se, |
| 1272 | int wakeup) {} |
Paul Turner | 2dac754 | 2012-10-04 13:18:30 +0200 | [diff] [blame] | 1273 | static inline void dequeue_entity_load_avg(struct cfs_rq *cfs_rq, |
Paul Turner | 9ee474f | 2012-10-04 13:18:30 +0200 | [diff] [blame] | 1274 | struct sched_entity *se, |
| 1275 | int sleep) {} |
Paul Turner | aff3e49 | 2012-10-04 13:18:30 +0200 | [diff] [blame] | 1276 | static inline void update_cfs_rq_blocked_load(struct cfs_rq *cfs_rq, |
| 1277 | int force_update) {} |
Paul Turner | 9d85f21 | 2012-10-04 13:18:29 +0200 | [diff] [blame] | 1278 | #endif |
| 1279 | |
Ingo Molnar | 2396af6 | 2007-08-09 11:16:48 +0200 | [diff] [blame] | 1280 | 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] | 1281 | { |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 1282 | #ifdef CONFIG_SCHEDSTATS |
Peter Zijlstra | e414314 | 2009-07-23 20:13:26 +0200 | [diff] [blame] | 1283 | struct task_struct *tsk = NULL; |
| 1284 | |
| 1285 | if (entity_is_task(se)) |
| 1286 | tsk = task_of(se); |
| 1287 | |
Lucas De Marchi | 41acab8 | 2010-03-10 23:37:45 -0300 | [diff] [blame] | 1288 | if (se->statistics.sleep_start) { |
| 1289 | u64 delta = rq_of(cfs_rq)->clock - se->statistics.sleep_start; |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 1290 | |
| 1291 | if ((s64)delta < 0) |
| 1292 | delta = 0; |
| 1293 | |
Lucas De Marchi | 41acab8 | 2010-03-10 23:37:45 -0300 | [diff] [blame] | 1294 | if (unlikely(delta > se->statistics.sleep_max)) |
| 1295 | se->statistics.sleep_max = delta; |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 1296 | |
Peter Zijlstra | 8c79a04 | 2012-01-30 14:51:37 +0100 | [diff] [blame] | 1297 | se->statistics.sleep_start = 0; |
Lucas De Marchi | 41acab8 | 2010-03-10 23:37:45 -0300 | [diff] [blame] | 1298 | se->statistics.sum_sleep_runtime += delta; |
Arjan van de Ven | 9745512 | 2008-01-25 21:08:34 +0100 | [diff] [blame] | 1299 | |
Peter Zijlstra | 768d0c2 | 2009-07-23 20:13:26 +0200 | [diff] [blame] | 1300 | if (tsk) { |
Peter Zijlstra | e414314 | 2009-07-23 20:13:26 +0200 | [diff] [blame] | 1301 | account_scheduler_latency(tsk, delta >> 10, 1); |
Peter Zijlstra | 768d0c2 | 2009-07-23 20:13:26 +0200 | [diff] [blame] | 1302 | trace_sched_stat_sleep(tsk, delta); |
| 1303 | } |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 1304 | } |
Lucas De Marchi | 41acab8 | 2010-03-10 23:37:45 -0300 | [diff] [blame] | 1305 | if (se->statistics.block_start) { |
| 1306 | u64 delta = rq_of(cfs_rq)->clock - se->statistics.block_start; |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 1307 | |
| 1308 | if ((s64)delta < 0) |
| 1309 | delta = 0; |
| 1310 | |
Lucas De Marchi | 41acab8 | 2010-03-10 23:37:45 -0300 | [diff] [blame] | 1311 | if (unlikely(delta > se->statistics.block_max)) |
| 1312 | se->statistics.block_max = delta; |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 1313 | |
Peter Zijlstra | 8c79a04 | 2012-01-30 14:51:37 +0100 | [diff] [blame] | 1314 | se->statistics.block_start = 0; |
Lucas De Marchi | 41acab8 | 2010-03-10 23:37:45 -0300 | [diff] [blame] | 1315 | se->statistics.sum_sleep_runtime += delta; |
Ingo Molnar | 30084fb | 2007-10-02 14:13:08 +0200 | [diff] [blame] | 1316 | |
Peter Zijlstra | e414314 | 2009-07-23 20:13:26 +0200 | [diff] [blame] | 1317 | if (tsk) { |
Arjan van de Ven | 8f0dfc3 | 2009-07-20 11:26:58 -0700 | [diff] [blame] | 1318 | if (tsk->in_iowait) { |
Lucas De Marchi | 41acab8 | 2010-03-10 23:37:45 -0300 | [diff] [blame] | 1319 | se->statistics.iowait_sum += delta; |
| 1320 | se->statistics.iowait_count++; |
Peter Zijlstra | 768d0c2 | 2009-07-23 20:13:26 +0200 | [diff] [blame] | 1321 | trace_sched_stat_iowait(tsk, delta); |
Arjan van de Ven | 8f0dfc3 | 2009-07-20 11:26:58 -0700 | [diff] [blame] | 1322 | } |
| 1323 | |
Andrew Vagin | b781a60 | 2011-11-28 12:03:35 +0300 | [diff] [blame] | 1324 | trace_sched_stat_blocked(tsk, delta); |
| 1325 | |
Peter Zijlstra | e414314 | 2009-07-23 20:13:26 +0200 | [diff] [blame] | 1326 | /* |
| 1327 | * Blocking time is in units of nanosecs, so shift by |
| 1328 | * 20 to get a milliseconds-range estimation of the |
| 1329 | * amount of time that the task spent sleeping: |
| 1330 | */ |
| 1331 | if (unlikely(prof_on == SLEEP_PROFILING)) { |
| 1332 | profile_hits(SLEEP_PROFILING, |
| 1333 | (void *)get_wchan(tsk), |
| 1334 | delta >> 20); |
| 1335 | } |
| 1336 | account_scheduler_latency(tsk, delta >> 10, 0); |
Ingo Molnar | 30084fb | 2007-10-02 14:13:08 +0200 | [diff] [blame] | 1337 | } |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 1338 | } |
| 1339 | #endif |
| 1340 | } |
| 1341 | |
Peter Zijlstra | ddc9729 | 2007-10-15 17:00:10 +0200 | [diff] [blame] | 1342 | static void check_spread(struct cfs_rq *cfs_rq, struct sched_entity *se) |
| 1343 | { |
| 1344 | #ifdef CONFIG_SCHED_DEBUG |
| 1345 | s64 d = se->vruntime - cfs_rq->min_vruntime; |
| 1346 | |
| 1347 | if (d < 0) |
| 1348 | d = -d; |
| 1349 | |
| 1350 | if (d > 3*sysctl_sched_latency) |
| 1351 | schedstat_inc(cfs_rq, nr_spread_over); |
| 1352 | #endif |
| 1353 | } |
| 1354 | |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 1355 | static void |
Peter Zijlstra | aeb73b0 | 2007-10-15 17:00:05 +0200 | [diff] [blame] | 1356 | place_entity(struct cfs_rq *cfs_rq, struct sched_entity *se, int initial) |
| 1357 | { |
Peter Zijlstra | 1af5f73 | 2008-10-24 11:06:13 +0200 | [diff] [blame] | 1358 | u64 vruntime = cfs_rq->min_vruntime; |
Peter Zijlstra | 94dfb5e | 2007-10-15 17:00:05 +0200 | [diff] [blame] | 1359 | |
Peter Zijlstra | 2cb8600 | 2007-11-09 22:39:37 +0100 | [diff] [blame] | 1360 | /* |
| 1361 | * The 'current' period is already promised to the current tasks, |
| 1362 | * however the extra weight of the new task will slow them down a |
| 1363 | * little, place the new task so that it fits in the slot that |
| 1364 | * stays open at the end. |
| 1365 | */ |
Peter Zijlstra | 94dfb5e | 2007-10-15 17:00:05 +0200 | [diff] [blame] | 1366 | if (initial && sched_feat(START_DEBIT)) |
Peter Zijlstra | f9c0b09 | 2008-10-17 19:27:04 +0200 | [diff] [blame] | 1367 | vruntime += sched_vslice(cfs_rq, se); |
Peter Zijlstra | aeb73b0 | 2007-10-15 17:00:05 +0200 | [diff] [blame] | 1368 | |
Mike Galbraith | a2e7a7e | 2009-09-18 09:19:25 +0200 | [diff] [blame] | 1369 | /* sleeps up to a single latency don't count. */ |
Mike Galbraith | 5ca9880 | 2010-03-11 17:17:17 +0100 | [diff] [blame] | 1370 | if (!initial) { |
Mike Galbraith | a2e7a7e | 2009-09-18 09:19:25 +0200 | [diff] [blame] | 1371 | unsigned long thresh = sysctl_sched_latency; |
Peter Zijlstra | a7be37a | 2008-06-27 13:41:11 +0200 | [diff] [blame] | 1372 | |
Mike Galbraith | a2e7a7e | 2009-09-18 09:19:25 +0200 | [diff] [blame] | 1373 | /* |
Mike Galbraith | a2e7a7e | 2009-09-18 09:19:25 +0200 | [diff] [blame] | 1374 | * Halve their sleep time's effect, to allow |
| 1375 | * for a gentler effect of sleepers: |
| 1376 | */ |
| 1377 | if (sched_feat(GENTLE_FAIR_SLEEPERS)) |
| 1378 | thresh >>= 1; |
Ingo Molnar | 51e0304 | 2009-09-16 08:54:45 +0200 | [diff] [blame] | 1379 | |
Mike Galbraith | a2e7a7e | 2009-09-18 09:19:25 +0200 | [diff] [blame] | 1380 | vruntime -= thresh; |
Peter Zijlstra | aeb73b0 | 2007-10-15 17:00:05 +0200 | [diff] [blame] | 1381 | } |
| 1382 | |
Mike Galbraith | b5d9d73 | 2009-09-08 11:12:28 +0200 | [diff] [blame] | 1383 | /* ensure we never gain time by being placed backwards. */ |
| 1384 | vruntime = max_vruntime(se->vruntime, vruntime); |
| 1385 | |
Peter Zijlstra | 67e9fb2 | 2007-10-15 17:00:10 +0200 | [diff] [blame] | 1386 | se->vruntime = vruntime; |
Peter Zijlstra | aeb73b0 | 2007-10-15 17:00:05 +0200 | [diff] [blame] | 1387 | } |
| 1388 | |
Paul Turner | d3d9dc3 | 2011-07-21 09:43:39 -0700 | [diff] [blame] | 1389 | static void check_enqueue_throttle(struct cfs_rq *cfs_rq); |
| 1390 | |
Peter Zijlstra | aeb73b0 | 2007-10-15 17:00:05 +0200 | [diff] [blame] | 1391 | static void |
Peter Zijlstra | 88ec22d | 2009-12-16 18:04:41 +0100 | [diff] [blame] | 1392 | 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] | 1393 | { |
| 1394 | /* |
Peter Zijlstra | 88ec22d | 2009-12-16 18:04:41 +0100 | [diff] [blame] | 1395 | * Update the normalized vruntime before updating min_vruntime |
| 1396 | * through callig update_curr(). |
| 1397 | */ |
Peter Zijlstra | 371fd7e | 2010-03-24 16:38:48 +0100 | [diff] [blame] | 1398 | if (!(flags & ENQUEUE_WAKEUP) || (flags & ENQUEUE_WAKING)) |
Peter Zijlstra | 88ec22d | 2009-12-16 18:04:41 +0100 | [diff] [blame] | 1399 | se->vruntime += cfs_rq->min_vruntime; |
| 1400 | |
| 1401 | /* |
Dmitry Adamushko | a2a2d68 | 2007-10-15 17:00:13 +0200 | [diff] [blame] | 1402 | * Update run-time statistics of the 'current'. |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 1403 | */ |
Ingo Molnar | b7cc089 | 2007-08-09 11:16:47 +0200 | [diff] [blame] | 1404 | update_curr(cfs_rq); |
Paul Turner | d6b5591 | 2010-11-15 15:47:09 -0800 | [diff] [blame] | 1405 | update_cfs_load(cfs_rq, 0); |
Paul Turner | 9ee474f | 2012-10-04 13:18:30 +0200 | [diff] [blame] | 1406 | enqueue_entity_load_avg(cfs_rq, se, flags & ENQUEUE_WAKEUP); |
Peter Zijlstra | a992241 | 2008-05-05 23:56:17 +0200 | [diff] [blame] | 1407 | account_entity_enqueue(cfs_rq, se); |
Paul Turner | 6d5ab29 | 2011-01-21 20:45:01 -0800 | [diff] [blame] | 1408 | update_cfs_shares(cfs_rq); |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 1409 | |
Peter Zijlstra | 88ec22d | 2009-12-16 18:04:41 +0100 | [diff] [blame] | 1410 | if (flags & ENQUEUE_WAKEUP) { |
Peter Zijlstra | aeb73b0 | 2007-10-15 17:00:05 +0200 | [diff] [blame] | 1411 | place_entity(cfs_rq, se, 0); |
Ingo Molnar | 2396af6 | 2007-08-09 11:16:48 +0200 | [diff] [blame] | 1412 | enqueue_sleeper(cfs_rq, se); |
Ingo Molnar | e9acbff | 2007-10-15 17:00:04 +0200 | [diff] [blame] | 1413 | } |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 1414 | |
Ingo Molnar | d2417e5 | 2007-08-09 11:16:47 +0200 | [diff] [blame] | 1415 | update_stats_enqueue(cfs_rq, se); |
Peter Zijlstra | ddc9729 | 2007-10-15 17:00:10 +0200 | [diff] [blame] | 1416 | check_spread(cfs_rq, se); |
Srivatsa Vaddagiri | 83b699e | 2007-10-15 17:00:08 +0200 | [diff] [blame] | 1417 | if (se != cfs_rq->curr) |
| 1418 | __enqueue_entity(cfs_rq, se); |
Peter Zijlstra | 2069dd7 | 2010-11-15 15:47:00 -0800 | [diff] [blame] | 1419 | se->on_rq = 1; |
Peter Zijlstra | 3d4b47b | 2010-11-15 15:47:01 -0800 | [diff] [blame] | 1420 | |
Paul Turner | d3d9dc3 | 2011-07-21 09:43:39 -0700 | [diff] [blame] | 1421 | if (cfs_rq->nr_running == 1) { |
Peter Zijlstra | 3d4b47b | 2010-11-15 15:47:01 -0800 | [diff] [blame] | 1422 | list_add_leaf_cfs_rq(cfs_rq); |
Paul Turner | d3d9dc3 | 2011-07-21 09:43:39 -0700 | [diff] [blame] | 1423 | check_enqueue_throttle(cfs_rq); |
| 1424 | } |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 1425 | } |
| 1426 | |
Rik van Riel | 2c13c919 | 2011-02-01 09:48:37 -0500 | [diff] [blame] | 1427 | static void __clear_buddies_last(struct sched_entity *se) |
Peter Zijlstra | 2002c69 | 2008-11-11 11:52:33 +0100 | [diff] [blame] | 1428 | { |
Rik van Riel | 2c13c919 | 2011-02-01 09:48:37 -0500 | [diff] [blame] | 1429 | for_each_sched_entity(se) { |
| 1430 | struct cfs_rq *cfs_rq = cfs_rq_of(se); |
| 1431 | if (cfs_rq->last == se) |
| 1432 | cfs_rq->last = NULL; |
| 1433 | else |
| 1434 | break; |
| 1435 | } |
| 1436 | } |
Peter Zijlstra | 2002c69 | 2008-11-11 11:52:33 +0100 | [diff] [blame] | 1437 | |
Rik van Riel | 2c13c919 | 2011-02-01 09:48:37 -0500 | [diff] [blame] | 1438 | static void __clear_buddies_next(struct sched_entity *se) |
| 1439 | { |
| 1440 | for_each_sched_entity(se) { |
| 1441 | struct cfs_rq *cfs_rq = cfs_rq_of(se); |
| 1442 | if (cfs_rq->next == se) |
| 1443 | cfs_rq->next = NULL; |
| 1444 | else |
| 1445 | break; |
| 1446 | } |
Peter Zijlstra | 2002c69 | 2008-11-11 11:52:33 +0100 | [diff] [blame] | 1447 | } |
| 1448 | |
Rik van Riel | ac53db5 | 2011-02-01 09:51:03 -0500 | [diff] [blame] | 1449 | static void __clear_buddies_skip(struct sched_entity *se) |
| 1450 | { |
| 1451 | for_each_sched_entity(se) { |
| 1452 | struct cfs_rq *cfs_rq = cfs_rq_of(se); |
| 1453 | if (cfs_rq->skip == se) |
| 1454 | cfs_rq->skip = NULL; |
| 1455 | else |
| 1456 | break; |
| 1457 | } |
| 1458 | } |
| 1459 | |
Peter Zijlstra | a571bbe | 2009-01-28 14:51:40 +0100 | [diff] [blame] | 1460 | static void clear_buddies(struct cfs_rq *cfs_rq, struct sched_entity *se) |
| 1461 | { |
Rik van Riel | 2c13c919 | 2011-02-01 09:48:37 -0500 | [diff] [blame] | 1462 | if (cfs_rq->last == se) |
| 1463 | __clear_buddies_last(se); |
| 1464 | |
| 1465 | if (cfs_rq->next == se) |
| 1466 | __clear_buddies_next(se); |
Rik van Riel | ac53db5 | 2011-02-01 09:51:03 -0500 | [diff] [blame] | 1467 | |
| 1468 | if (cfs_rq->skip == se) |
| 1469 | __clear_buddies_skip(se); |
Peter Zijlstra | a571bbe | 2009-01-28 14:51:40 +0100 | [diff] [blame] | 1470 | } |
| 1471 | |
Peter Zijlstra | 6c16a6d | 2012-03-21 13:07:16 -0700 | [diff] [blame] | 1472 | static __always_inline void return_cfs_rq_runtime(struct cfs_rq *cfs_rq); |
Paul Turner | d8b4986 | 2011-07-21 09:43:41 -0700 | [diff] [blame] | 1473 | |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 1474 | static void |
Peter Zijlstra | 371fd7e | 2010-03-24 16:38:48 +0100 | [diff] [blame] | 1475 | 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] | 1476 | { |
Dmitry Adamushko | a2a2d68 | 2007-10-15 17:00:13 +0200 | [diff] [blame] | 1477 | /* |
| 1478 | * Update run-time statistics of the 'current'. |
| 1479 | */ |
| 1480 | update_curr(cfs_rq); |
Paul Turner | 9ee474f | 2012-10-04 13:18:30 +0200 | [diff] [blame] | 1481 | dequeue_entity_load_avg(cfs_rq, se, flags & DEQUEUE_SLEEP); |
Dmitry Adamushko | a2a2d68 | 2007-10-15 17:00:13 +0200 | [diff] [blame] | 1482 | |
Ingo Molnar | 19b6a2e | 2007-08-09 11:16:48 +0200 | [diff] [blame] | 1483 | update_stats_dequeue(cfs_rq, se); |
Peter Zijlstra | 371fd7e | 2010-03-24 16:38:48 +0100 | [diff] [blame] | 1484 | if (flags & DEQUEUE_SLEEP) { |
Peter Zijlstra | 67e9fb2 | 2007-10-15 17:00:10 +0200 | [diff] [blame] | 1485 | #ifdef CONFIG_SCHEDSTATS |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 1486 | if (entity_is_task(se)) { |
| 1487 | struct task_struct *tsk = task_of(se); |
| 1488 | |
| 1489 | if (tsk->state & TASK_INTERRUPTIBLE) |
Lucas De Marchi | 41acab8 | 2010-03-10 23:37:45 -0300 | [diff] [blame] | 1490 | se->statistics.sleep_start = rq_of(cfs_rq)->clock; |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 1491 | if (tsk->state & TASK_UNINTERRUPTIBLE) |
Lucas De Marchi | 41acab8 | 2010-03-10 23:37:45 -0300 | [diff] [blame] | 1492 | se->statistics.block_start = rq_of(cfs_rq)->clock; |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 1493 | } |
Dmitry Adamushko | db36cc7 | 2007-10-15 17:00:06 +0200 | [diff] [blame] | 1494 | #endif |
Peter Zijlstra | 67e9fb2 | 2007-10-15 17:00:10 +0200 | [diff] [blame] | 1495 | } |
| 1496 | |
Peter Zijlstra | 2002c69 | 2008-11-11 11:52:33 +0100 | [diff] [blame] | 1497 | clear_buddies(cfs_rq, se); |
Peter Zijlstra | 4793241 | 2008-11-04 21:25:09 +0100 | [diff] [blame] | 1498 | |
Srivatsa Vaddagiri | 83b699e | 2007-10-15 17:00:08 +0200 | [diff] [blame] | 1499 | if (se != cfs_rq->curr) |
Dmitry Adamushko | 30cfdcf | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 1500 | __dequeue_entity(cfs_rq, se); |
Peter Zijlstra | 2069dd7 | 2010-11-15 15:47:00 -0800 | [diff] [blame] | 1501 | se->on_rq = 0; |
Paul Turner | d6b5591 | 2010-11-15 15:47:09 -0800 | [diff] [blame] | 1502 | update_cfs_load(cfs_rq, 0); |
Dmitry Adamushko | 30cfdcf | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 1503 | account_entity_dequeue(cfs_rq, se); |
Peter Zijlstra | 88ec22d | 2009-12-16 18:04:41 +0100 | [diff] [blame] | 1504 | |
| 1505 | /* |
| 1506 | * Normalize the entity after updating the min_vruntime because the |
| 1507 | * update can refer to the ->curr item and we need to reflect this |
| 1508 | * movement in our normalized position. |
| 1509 | */ |
Peter Zijlstra | 371fd7e | 2010-03-24 16:38:48 +0100 | [diff] [blame] | 1510 | if (!(flags & DEQUEUE_SLEEP)) |
Peter Zijlstra | 88ec22d | 2009-12-16 18:04:41 +0100 | [diff] [blame] | 1511 | se->vruntime -= cfs_rq->min_vruntime; |
Peter Zijlstra | 1e87623 | 2011-05-17 16:21:10 -0700 | [diff] [blame] | 1512 | |
Paul Turner | d8b4986 | 2011-07-21 09:43:41 -0700 | [diff] [blame] | 1513 | /* return excess runtime on last dequeue */ |
| 1514 | return_cfs_rq_runtime(cfs_rq); |
| 1515 | |
Peter Zijlstra | 1e87623 | 2011-05-17 16:21:10 -0700 | [diff] [blame] | 1516 | update_min_vruntime(cfs_rq); |
| 1517 | update_cfs_shares(cfs_rq); |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 1518 | } |
| 1519 | |
| 1520 | /* |
| 1521 | * Preempt the current task with a newly woken task if needed: |
| 1522 | */ |
Peter Zijlstra | 7c92e54 | 2007-09-05 14:32:49 +0200 | [diff] [blame] | 1523 | static void |
Ingo Molnar | 2e09bf5 | 2007-10-15 17:00:05 +0200 | [diff] [blame] | 1524 | check_preempt_tick(struct cfs_rq *cfs_rq, struct sched_entity *curr) |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 1525 | { |
Peter Zijlstra | 1169783 | 2007-09-05 14:32:49 +0200 | [diff] [blame] | 1526 | unsigned long ideal_runtime, delta_exec; |
Wang Xingchao | f4cfb33 | 2011-09-16 13:35:52 -0400 | [diff] [blame] | 1527 | struct sched_entity *se; |
| 1528 | s64 delta; |
Peter Zijlstra | 1169783 | 2007-09-05 14:32:49 +0200 | [diff] [blame] | 1529 | |
Peter Zijlstra | 6d0f0eb | 2007-10-15 17:00:05 +0200 | [diff] [blame] | 1530 | ideal_runtime = sched_slice(cfs_rq, curr); |
Peter Zijlstra | 1169783 | 2007-09-05 14:32:49 +0200 | [diff] [blame] | 1531 | delta_exec = curr->sum_exec_runtime - curr->prev_sum_exec_runtime; |
Mike Galbraith | a9f3e2b | 2009-01-28 14:51:39 +0100 | [diff] [blame] | 1532 | if (delta_exec > ideal_runtime) { |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 1533 | resched_task(rq_of(cfs_rq)->curr); |
Mike Galbraith | a9f3e2b | 2009-01-28 14:51:39 +0100 | [diff] [blame] | 1534 | /* |
| 1535 | * The current task ran long enough, ensure it doesn't get |
| 1536 | * re-elected due to buddy favours. |
| 1537 | */ |
| 1538 | clear_buddies(cfs_rq, curr); |
Mike Galbraith | f685cea | 2009-10-23 23:09:22 +0200 | [diff] [blame] | 1539 | return; |
| 1540 | } |
| 1541 | |
| 1542 | /* |
| 1543 | * Ensure that a task that missed wakeup preemption by a |
| 1544 | * narrow margin doesn't have to wait for a full slice. |
| 1545 | * This also mitigates buddy induced latencies under load. |
| 1546 | */ |
Mike Galbraith | f685cea | 2009-10-23 23:09:22 +0200 | [diff] [blame] | 1547 | if (delta_exec < sysctl_sched_min_granularity) |
| 1548 | return; |
| 1549 | |
Wang Xingchao | f4cfb33 | 2011-09-16 13:35:52 -0400 | [diff] [blame] | 1550 | se = __pick_first_entity(cfs_rq); |
| 1551 | delta = curr->vruntime - se->vruntime; |
Mike Galbraith | f685cea | 2009-10-23 23:09:22 +0200 | [diff] [blame] | 1552 | |
Wang Xingchao | f4cfb33 | 2011-09-16 13:35:52 -0400 | [diff] [blame] | 1553 | if (delta < 0) |
| 1554 | return; |
Mike Galbraith | d7d8294 | 2011-01-05 05:41:17 +0100 | [diff] [blame] | 1555 | |
Wang Xingchao | f4cfb33 | 2011-09-16 13:35:52 -0400 | [diff] [blame] | 1556 | if (delta > ideal_runtime) |
| 1557 | resched_task(rq_of(cfs_rq)->curr); |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 1558 | } |
| 1559 | |
Srivatsa Vaddagiri | 83b699e | 2007-10-15 17:00:08 +0200 | [diff] [blame] | 1560 | static void |
Ingo Molnar | 8494f41 | 2007-08-09 11:16:48 +0200 | [diff] [blame] | 1561 | set_next_entity(struct cfs_rq *cfs_rq, struct sched_entity *se) |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 1562 | { |
Srivatsa Vaddagiri | 83b699e | 2007-10-15 17:00:08 +0200 | [diff] [blame] | 1563 | /* 'current' is not kept within the tree. */ |
| 1564 | if (se->on_rq) { |
| 1565 | /* |
| 1566 | * Any task has to be enqueued before it get to execute on |
| 1567 | * a CPU. So account for the time it spent waiting on the |
| 1568 | * runqueue. |
| 1569 | */ |
| 1570 | update_stats_wait_end(cfs_rq, se); |
| 1571 | __dequeue_entity(cfs_rq, se); |
| 1572 | } |
| 1573 | |
Ingo Molnar | 79303e9 | 2007-08-09 11:16:47 +0200 | [diff] [blame] | 1574 | update_stats_curr_start(cfs_rq, se); |
Ingo Molnar | 429d43b | 2007-10-15 17:00:03 +0200 | [diff] [blame] | 1575 | cfs_rq->curr = se; |
Ingo Molnar | eba1ed4 | 2007-10-15 17:00:02 +0200 | [diff] [blame] | 1576 | #ifdef CONFIG_SCHEDSTATS |
| 1577 | /* |
| 1578 | * Track our maximum slice length, if the CPU's load is at |
| 1579 | * least twice that of our own weight (i.e. dont track it |
| 1580 | * when there are only lesser-weight tasks around): |
| 1581 | */ |
Dmitry Adamushko | 495eca4 | 2007-10-15 17:00:06 +0200 | [diff] [blame] | 1582 | if (rq_of(cfs_rq)->load.weight >= 2*se->load.weight) { |
Lucas De Marchi | 41acab8 | 2010-03-10 23:37:45 -0300 | [diff] [blame] | 1583 | se->statistics.slice_max = max(se->statistics.slice_max, |
Ingo Molnar | eba1ed4 | 2007-10-15 17:00:02 +0200 | [diff] [blame] | 1584 | se->sum_exec_runtime - se->prev_sum_exec_runtime); |
| 1585 | } |
| 1586 | #endif |
Peter Zijlstra | 4a55b45 | 2007-09-05 14:32:49 +0200 | [diff] [blame] | 1587 | se->prev_sum_exec_runtime = se->sum_exec_runtime; |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 1588 | } |
| 1589 | |
Peter Zijlstra | 3f3a490 | 2008-10-24 11:06:16 +0200 | [diff] [blame] | 1590 | static int |
| 1591 | wakeup_preempt_entity(struct sched_entity *curr, struct sched_entity *se); |
| 1592 | |
Rik van Riel | ac53db5 | 2011-02-01 09:51:03 -0500 | [diff] [blame] | 1593 | /* |
| 1594 | * Pick the next process, keeping these things in mind, in this order: |
| 1595 | * 1) keep things fair between processes/task groups |
| 1596 | * 2) pick the "next" process, since someone really wants that to run |
| 1597 | * 3) pick the "last" process, for cache locality |
| 1598 | * 4) do not run the "skip" process, if something else is available |
| 1599 | */ |
Peter Zijlstra | f4b6755 | 2008-11-04 21:25:07 +0100 | [diff] [blame] | 1600 | static struct sched_entity *pick_next_entity(struct cfs_rq *cfs_rq) |
Peter Zijlstra | aa2ac25 | 2008-03-14 21:12:12 +0100 | [diff] [blame] | 1601 | { |
Rik van Riel | ac53db5 | 2011-02-01 09:51:03 -0500 | [diff] [blame] | 1602 | struct sched_entity *se = __pick_first_entity(cfs_rq); |
Mike Galbraith | f685cea | 2009-10-23 23:09:22 +0200 | [diff] [blame] | 1603 | struct sched_entity *left = se; |
Peter Zijlstra | f4b6755 | 2008-11-04 21:25:07 +0100 | [diff] [blame] | 1604 | |
Rik van Riel | ac53db5 | 2011-02-01 09:51:03 -0500 | [diff] [blame] | 1605 | /* |
| 1606 | * Avoid running the skip buddy, if running something else can |
| 1607 | * be done without getting too unfair. |
| 1608 | */ |
| 1609 | if (cfs_rq->skip == se) { |
| 1610 | struct sched_entity *second = __pick_next_entity(se); |
| 1611 | if (second && wakeup_preempt_entity(second, left) < 1) |
| 1612 | se = second; |
| 1613 | } |
Peter Zijlstra | aa2ac25 | 2008-03-14 21:12:12 +0100 | [diff] [blame] | 1614 | |
Mike Galbraith | f685cea | 2009-10-23 23:09:22 +0200 | [diff] [blame] | 1615 | /* |
| 1616 | * Prefer last buddy, try to return the CPU to a preempted task. |
| 1617 | */ |
| 1618 | if (cfs_rq->last && wakeup_preempt_entity(cfs_rq->last, left) < 1) |
| 1619 | se = cfs_rq->last; |
| 1620 | |
Rik van Riel | ac53db5 | 2011-02-01 09:51:03 -0500 | [diff] [blame] | 1621 | /* |
| 1622 | * Someone really wants this to run. If it's not unfair, run it. |
| 1623 | */ |
| 1624 | if (cfs_rq->next && wakeup_preempt_entity(cfs_rq->next, left) < 1) |
| 1625 | se = cfs_rq->next; |
| 1626 | |
Mike Galbraith | f685cea | 2009-10-23 23:09:22 +0200 | [diff] [blame] | 1627 | clear_buddies(cfs_rq, se); |
Peter Zijlstra | 4793241 | 2008-11-04 21:25:09 +0100 | [diff] [blame] | 1628 | |
| 1629 | return se; |
Peter Zijlstra | aa2ac25 | 2008-03-14 21:12:12 +0100 | [diff] [blame] | 1630 | } |
| 1631 | |
Paul Turner | d3d9dc3 | 2011-07-21 09:43:39 -0700 | [diff] [blame] | 1632 | static void check_cfs_rq_runtime(struct cfs_rq *cfs_rq); |
| 1633 | |
Ingo Molnar | ab6cde2 | 2007-08-09 11:16:48 +0200 | [diff] [blame] | 1634 | 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] | 1635 | { |
| 1636 | /* |
| 1637 | * If still on the runqueue then deactivate_task() |
| 1638 | * was not called and update_curr() has to be done: |
| 1639 | */ |
| 1640 | if (prev->on_rq) |
Ingo Molnar | b7cc089 | 2007-08-09 11:16:47 +0200 | [diff] [blame] | 1641 | update_curr(cfs_rq); |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 1642 | |
Paul Turner | d3d9dc3 | 2011-07-21 09:43:39 -0700 | [diff] [blame] | 1643 | /* throttle cfs_rqs exceeding runtime */ |
| 1644 | check_cfs_rq_runtime(cfs_rq); |
| 1645 | |
Peter Zijlstra | ddc9729 | 2007-10-15 17:00:10 +0200 | [diff] [blame] | 1646 | check_spread(cfs_rq, prev); |
Dmitry Adamushko | 30cfdcf | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 1647 | if (prev->on_rq) { |
Ingo Molnar | 5870db5 | 2007-08-09 11:16:47 +0200 | [diff] [blame] | 1648 | update_stats_wait_start(cfs_rq, prev); |
Dmitry Adamushko | 30cfdcf | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 1649 | /* Put 'current' back into the tree. */ |
| 1650 | __enqueue_entity(cfs_rq, prev); |
Paul Turner | 9d85f21 | 2012-10-04 13:18:29 +0200 | [diff] [blame] | 1651 | /* in !on_rq case, update occurred at dequeue */ |
Paul Turner | 9ee474f | 2012-10-04 13:18:30 +0200 | [diff] [blame] | 1652 | update_entity_load_avg(prev, 1); |
Dmitry Adamushko | 30cfdcf | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 1653 | } |
Ingo Molnar | 429d43b | 2007-10-15 17:00:03 +0200 | [diff] [blame] | 1654 | cfs_rq->curr = NULL; |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 1655 | } |
| 1656 | |
Peter Zijlstra | 8f4d37e | 2008-01-25 21:08:29 +0100 | [diff] [blame] | 1657 | static void |
| 1658 | 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] | 1659 | { |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 1660 | /* |
Dmitry Adamushko | 30cfdcf | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 1661 | * Update run-time statistics of the 'current'. |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 1662 | */ |
Dmitry Adamushko | 30cfdcf | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 1663 | update_curr(cfs_rq); |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 1664 | |
Paul Turner | 43365bd | 2010-12-15 19:10:17 -0800 | [diff] [blame] | 1665 | /* |
Paul Turner | 9d85f21 | 2012-10-04 13:18:29 +0200 | [diff] [blame] | 1666 | * Ensure that runnable average is periodically updated. |
| 1667 | */ |
Paul Turner | 9ee474f | 2012-10-04 13:18:30 +0200 | [diff] [blame] | 1668 | update_entity_load_avg(curr, 1); |
Paul Turner | aff3e49 | 2012-10-04 13:18:30 +0200 | [diff] [blame] | 1669 | update_cfs_rq_blocked_load(cfs_rq, 1); |
Paul Turner | 9d85f21 | 2012-10-04 13:18:29 +0200 | [diff] [blame] | 1670 | |
| 1671 | /* |
Paul Turner | 43365bd | 2010-12-15 19:10:17 -0800 | [diff] [blame] | 1672 | * Update share accounting for long-running entities. |
| 1673 | */ |
| 1674 | update_entity_shares_tick(cfs_rq); |
| 1675 | |
Peter Zijlstra | 8f4d37e | 2008-01-25 21:08:29 +0100 | [diff] [blame] | 1676 | #ifdef CONFIG_SCHED_HRTICK |
| 1677 | /* |
| 1678 | * queued ticks are scheduled to match the slice, so don't bother |
| 1679 | * validating it and just reschedule. |
| 1680 | */ |
Harvey Harrison | 983ed7a | 2008-04-24 18:17:55 -0700 | [diff] [blame] | 1681 | if (queued) { |
| 1682 | resched_task(rq_of(cfs_rq)->curr); |
| 1683 | return; |
| 1684 | } |
Peter Zijlstra | 8f4d37e | 2008-01-25 21:08:29 +0100 | [diff] [blame] | 1685 | /* |
| 1686 | * don't let the period tick interfere with the hrtick preemption |
| 1687 | */ |
| 1688 | if (!sched_feat(DOUBLE_TICK) && |
| 1689 | hrtimer_active(&rq_of(cfs_rq)->hrtick_timer)) |
| 1690 | return; |
| 1691 | #endif |
| 1692 | |
Yong Zhang | 2c2efae | 2011-07-29 16:20:33 +0800 | [diff] [blame] | 1693 | if (cfs_rq->nr_running > 1) |
Ingo Molnar | 2e09bf5 | 2007-10-15 17:00:05 +0200 | [diff] [blame] | 1694 | check_preempt_tick(cfs_rq, curr); |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 1695 | } |
| 1696 | |
Paul Turner | ab84d31 | 2011-07-21 09:43:28 -0700 | [diff] [blame] | 1697 | |
| 1698 | /************************************************** |
| 1699 | * CFS bandwidth control machinery |
| 1700 | */ |
| 1701 | |
| 1702 | #ifdef CONFIG_CFS_BANDWIDTH |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 1703 | |
| 1704 | #ifdef HAVE_JUMP_LABEL |
Ingo Molnar | c5905af | 2012-02-24 08:31:31 +0100 | [diff] [blame] | 1705 | static struct static_key __cfs_bandwidth_used; |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 1706 | |
| 1707 | static inline bool cfs_bandwidth_used(void) |
| 1708 | { |
Ingo Molnar | c5905af | 2012-02-24 08:31:31 +0100 | [diff] [blame] | 1709 | return static_key_false(&__cfs_bandwidth_used); |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 1710 | } |
| 1711 | |
| 1712 | void account_cfs_bandwidth_used(int enabled, int was_enabled) |
| 1713 | { |
| 1714 | /* only need to count groups transitioning between enabled/!enabled */ |
| 1715 | if (enabled && !was_enabled) |
Ingo Molnar | c5905af | 2012-02-24 08:31:31 +0100 | [diff] [blame] | 1716 | static_key_slow_inc(&__cfs_bandwidth_used); |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 1717 | else if (!enabled && was_enabled) |
Ingo Molnar | c5905af | 2012-02-24 08:31:31 +0100 | [diff] [blame] | 1718 | static_key_slow_dec(&__cfs_bandwidth_used); |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 1719 | } |
| 1720 | #else /* HAVE_JUMP_LABEL */ |
| 1721 | static bool cfs_bandwidth_used(void) |
| 1722 | { |
| 1723 | return true; |
| 1724 | } |
| 1725 | |
| 1726 | void account_cfs_bandwidth_used(int enabled, int was_enabled) {} |
| 1727 | #endif /* HAVE_JUMP_LABEL */ |
| 1728 | |
Paul Turner | ab84d31 | 2011-07-21 09:43:28 -0700 | [diff] [blame] | 1729 | /* |
| 1730 | * default period for cfs group bandwidth. |
| 1731 | * default: 0.1s, units: nanoseconds |
| 1732 | */ |
| 1733 | static inline u64 default_cfs_period(void) |
| 1734 | { |
| 1735 | return 100000000ULL; |
| 1736 | } |
Paul Turner | ec12cb7 | 2011-07-21 09:43:30 -0700 | [diff] [blame] | 1737 | |
| 1738 | static inline u64 sched_cfs_bandwidth_slice(void) |
| 1739 | { |
| 1740 | return (u64)sysctl_sched_cfs_bandwidth_slice * NSEC_PER_USEC; |
| 1741 | } |
| 1742 | |
Paul Turner | a9cf55b | 2011-07-21 09:43:32 -0700 | [diff] [blame] | 1743 | /* |
| 1744 | * Replenish runtime according to assigned quota and update expiration time. |
| 1745 | * We use sched_clock_cpu directly instead of rq->clock to avoid adding |
| 1746 | * additional synchronization around rq->lock. |
| 1747 | * |
| 1748 | * requires cfs_b->lock |
| 1749 | */ |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 1750 | void __refill_cfs_bandwidth_runtime(struct cfs_bandwidth *cfs_b) |
Paul Turner | a9cf55b | 2011-07-21 09:43:32 -0700 | [diff] [blame] | 1751 | { |
| 1752 | u64 now; |
| 1753 | |
| 1754 | if (cfs_b->quota == RUNTIME_INF) |
| 1755 | return; |
| 1756 | |
| 1757 | now = sched_clock_cpu(smp_processor_id()); |
| 1758 | cfs_b->runtime = cfs_b->quota; |
| 1759 | cfs_b->runtime_expires = now + ktime_to_ns(cfs_b->period); |
| 1760 | } |
| 1761 | |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 1762 | static inline struct cfs_bandwidth *tg_cfs_bandwidth(struct task_group *tg) |
| 1763 | { |
| 1764 | return &tg->cfs_bandwidth; |
| 1765 | } |
| 1766 | |
Paul Turner | 85dac90 | 2011-07-21 09:43:33 -0700 | [diff] [blame] | 1767 | /* returns 0 on failure to allocate runtime */ |
| 1768 | static int assign_cfs_rq_runtime(struct cfs_rq *cfs_rq) |
Paul Turner | ec12cb7 | 2011-07-21 09:43:30 -0700 | [diff] [blame] | 1769 | { |
| 1770 | struct task_group *tg = cfs_rq->tg; |
| 1771 | struct cfs_bandwidth *cfs_b = tg_cfs_bandwidth(tg); |
Paul Turner | a9cf55b | 2011-07-21 09:43:32 -0700 | [diff] [blame] | 1772 | u64 amount = 0, min_amount, expires; |
Paul Turner | ec12cb7 | 2011-07-21 09:43:30 -0700 | [diff] [blame] | 1773 | |
| 1774 | /* note: this is a positive sum as runtime_remaining <= 0 */ |
| 1775 | min_amount = sched_cfs_bandwidth_slice() - cfs_rq->runtime_remaining; |
| 1776 | |
| 1777 | raw_spin_lock(&cfs_b->lock); |
| 1778 | if (cfs_b->quota == RUNTIME_INF) |
| 1779 | amount = min_amount; |
Paul Turner | 58088ad | 2011-07-21 09:43:31 -0700 | [diff] [blame] | 1780 | else { |
Paul Turner | a9cf55b | 2011-07-21 09:43:32 -0700 | [diff] [blame] | 1781 | /* |
| 1782 | * If the bandwidth pool has become inactive, then at least one |
| 1783 | * period must have elapsed since the last consumption. |
| 1784 | * Refresh the global state and ensure bandwidth timer becomes |
| 1785 | * active. |
| 1786 | */ |
| 1787 | if (!cfs_b->timer_active) { |
| 1788 | __refill_cfs_bandwidth_runtime(cfs_b); |
Paul Turner | 58088ad | 2011-07-21 09:43:31 -0700 | [diff] [blame] | 1789 | __start_cfs_bandwidth(cfs_b); |
Paul Turner | a9cf55b | 2011-07-21 09:43:32 -0700 | [diff] [blame] | 1790 | } |
Paul Turner | 58088ad | 2011-07-21 09:43:31 -0700 | [diff] [blame] | 1791 | |
| 1792 | if (cfs_b->runtime > 0) { |
| 1793 | amount = min(cfs_b->runtime, min_amount); |
| 1794 | cfs_b->runtime -= amount; |
| 1795 | cfs_b->idle = 0; |
| 1796 | } |
Paul Turner | ec12cb7 | 2011-07-21 09:43:30 -0700 | [diff] [blame] | 1797 | } |
Paul Turner | a9cf55b | 2011-07-21 09:43:32 -0700 | [diff] [blame] | 1798 | expires = cfs_b->runtime_expires; |
Paul Turner | ec12cb7 | 2011-07-21 09:43:30 -0700 | [diff] [blame] | 1799 | raw_spin_unlock(&cfs_b->lock); |
| 1800 | |
| 1801 | cfs_rq->runtime_remaining += amount; |
Paul Turner | a9cf55b | 2011-07-21 09:43:32 -0700 | [diff] [blame] | 1802 | /* |
| 1803 | * we may have advanced our local expiration to account for allowed |
| 1804 | * spread between our sched_clock and the one on which runtime was |
| 1805 | * issued. |
| 1806 | */ |
| 1807 | if ((s64)(expires - cfs_rq->runtime_expires) > 0) |
| 1808 | cfs_rq->runtime_expires = expires; |
Paul Turner | 85dac90 | 2011-07-21 09:43:33 -0700 | [diff] [blame] | 1809 | |
| 1810 | return cfs_rq->runtime_remaining > 0; |
Paul Turner | a9cf55b | 2011-07-21 09:43:32 -0700 | [diff] [blame] | 1811 | } |
| 1812 | |
| 1813 | /* |
| 1814 | * Note: This depends on the synchronization provided by sched_clock and the |
| 1815 | * fact that rq->clock snapshots this value. |
| 1816 | */ |
| 1817 | static void expire_cfs_rq_runtime(struct cfs_rq *cfs_rq) |
| 1818 | { |
| 1819 | struct cfs_bandwidth *cfs_b = tg_cfs_bandwidth(cfs_rq->tg); |
| 1820 | struct rq *rq = rq_of(cfs_rq); |
| 1821 | |
| 1822 | /* if the deadline is ahead of our clock, nothing to do */ |
| 1823 | if (likely((s64)(rq->clock - cfs_rq->runtime_expires) < 0)) |
| 1824 | return; |
| 1825 | |
| 1826 | if (cfs_rq->runtime_remaining < 0) |
| 1827 | return; |
| 1828 | |
| 1829 | /* |
| 1830 | * If the local deadline has passed we have to consider the |
| 1831 | * possibility that our sched_clock is 'fast' and the global deadline |
| 1832 | * has not truly expired. |
| 1833 | * |
| 1834 | * Fortunately we can check determine whether this the case by checking |
| 1835 | * whether the global deadline has advanced. |
| 1836 | */ |
| 1837 | |
| 1838 | if ((s64)(cfs_rq->runtime_expires - cfs_b->runtime_expires) >= 0) { |
| 1839 | /* extend local deadline, drift is bounded above by 2 ticks */ |
| 1840 | cfs_rq->runtime_expires += TICK_NSEC; |
| 1841 | } else { |
| 1842 | /* global deadline is ahead, expiration has passed */ |
| 1843 | cfs_rq->runtime_remaining = 0; |
| 1844 | } |
Paul Turner | ec12cb7 | 2011-07-21 09:43:30 -0700 | [diff] [blame] | 1845 | } |
| 1846 | |
| 1847 | static void __account_cfs_rq_runtime(struct cfs_rq *cfs_rq, |
| 1848 | unsigned long delta_exec) |
| 1849 | { |
Paul Turner | a9cf55b | 2011-07-21 09:43:32 -0700 | [diff] [blame] | 1850 | /* dock delta_exec before expiring quota (as it could span periods) */ |
Paul Turner | ec12cb7 | 2011-07-21 09:43:30 -0700 | [diff] [blame] | 1851 | cfs_rq->runtime_remaining -= delta_exec; |
Paul Turner | a9cf55b | 2011-07-21 09:43:32 -0700 | [diff] [blame] | 1852 | expire_cfs_rq_runtime(cfs_rq); |
| 1853 | |
| 1854 | if (likely(cfs_rq->runtime_remaining > 0)) |
Paul Turner | ec12cb7 | 2011-07-21 09:43:30 -0700 | [diff] [blame] | 1855 | return; |
| 1856 | |
Paul Turner | 85dac90 | 2011-07-21 09:43:33 -0700 | [diff] [blame] | 1857 | /* |
| 1858 | * if we're unable to extend our runtime we resched so that the active |
| 1859 | * hierarchy can be throttled |
| 1860 | */ |
| 1861 | if (!assign_cfs_rq_runtime(cfs_rq) && likely(cfs_rq->curr)) |
| 1862 | resched_task(rq_of(cfs_rq)->curr); |
Paul Turner | ec12cb7 | 2011-07-21 09:43:30 -0700 | [diff] [blame] | 1863 | } |
| 1864 | |
Peter Zijlstra | 6c16a6d | 2012-03-21 13:07:16 -0700 | [diff] [blame] | 1865 | static __always_inline |
| 1866 | void account_cfs_rq_runtime(struct cfs_rq *cfs_rq, unsigned long delta_exec) |
Paul Turner | ec12cb7 | 2011-07-21 09:43:30 -0700 | [diff] [blame] | 1867 | { |
Paul Turner | 56f570e | 2011-11-07 20:26:33 -0800 | [diff] [blame] | 1868 | if (!cfs_bandwidth_used() || !cfs_rq->runtime_enabled) |
Paul Turner | ec12cb7 | 2011-07-21 09:43:30 -0700 | [diff] [blame] | 1869 | return; |
| 1870 | |
| 1871 | __account_cfs_rq_runtime(cfs_rq, delta_exec); |
| 1872 | } |
| 1873 | |
Paul Turner | 85dac90 | 2011-07-21 09:43:33 -0700 | [diff] [blame] | 1874 | static inline int cfs_rq_throttled(struct cfs_rq *cfs_rq) |
| 1875 | { |
Paul Turner | 56f570e | 2011-11-07 20:26:33 -0800 | [diff] [blame] | 1876 | return cfs_bandwidth_used() && cfs_rq->throttled; |
Paul Turner | 85dac90 | 2011-07-21 09:43:33 -0700 | [diff] [blame] | 1877 | } |
| 1878 | |
Paul Turner | 64660c8 | 2011-07-21 09:43:36 -0700 | [diff] [blame] | 1879 | /* check whether cfs_rq, or any parent, is throttled */ |
| 1880 | static inline int throttled_hierarchy(struct cfs_rq *cfs_rq) |
| 1881 | { |
Paul Turner | 56f570e | 2011-11-07 20:26:33 -0800 | [diff] [blame] | 1882 | return cfs_bandwidth_used() && cfs_rq->throttle_count; |
Paul Turner | 64660c8 | 2011-07-21 09:43:36 -0700 | [diff] [blame] | 1883 | } |
| 1884 | |
| 1885 | /* |
| 1886 | * Ensure that neither of the group entities corresponding to src_cpu or |
| 1887 | * dest_cpu are members of a throttled hierarchy when performing group |
| 1888 | * load-balance operations. |
| 1889 | */ |
| 1890 | static inline int throttled_lb_pair(struct task_group *tg, |
| 1891 | int src_cpu, int dest_cpu) |
| 1892 | { |
| 1893 | struct cfs_rq *src_cfs_rq, *dest_cfs_rq; |
| 1894 | |
| 1895 | src_cfs_rq = tg->cfs_rq[src_cpu]; |
| 1896 | dest_cfs_rq = tg->cfs_rq[dest_cpu]; |
| 1897 | |
| 1898 | return throttled_hierarchy(src_cfs_rq) || |
| 1899 | throttled_hierarchy(dest_cfs_rq); |
| 1900 | } |
| 1901 | |
| 1902 | /* updated child weight may affect parent so we have to do this bottom up */ |
| 1903 | static int tg_unthrottle_up(struct task_group *tg, void *data) |
| 1904 | { |
| 1905 | struct rq *rq = data; |
| 1906 | struct cfs_rq *cfs_rq = tg->cfs_rq[cpu_of(rq)]; |
| 1907 | |
| 1908 | cfs_rq->throttle_count--; |
| 1909 | #ifdef CONFIG_SMP |
| 1910 | if (!cfs_rq->throttle_count) { |
| 1911 | u64 delta = rq->clock_task - cfs_rq->load_stamp; |
| 1912 | |
| 1913 | /* leaving throttled state, advance shares averaging windows */ |
| 1914 | cfs_rq->load_stamp += delta; |
| 1915 | cfs_rq->load_last += delta; |
| 1916 | |
| 1917 | /* update entity weight now that we are on_rq again */ |
| 1918 | update_cfs_shares(cfs_rq); |
| 1919 | } |
| 1920 | #endif |
| 1921 | |
| 1922 | return 0; |
| 1923 | } |
| 1924 | |
| 1925 | static int tg_throttle_down(struct task_group *tg, void *data) |
| 1926 | { |
| 1927 | struct rq *rq = data; |
| 1928 | struct cfs_rq *cfs_rq = tg->cfs_rq[cpu_of(rq)]; |
| 1929 | |
| 1930 | /* group is entering throttled state, record last load */ |
| 1931 | if (!cfs_rq->throttle_count) |
| 1932 | update_cfs_load(cfs_rq, 0); |
| 1933 | cfs_rq->throttle_count++; |
| 1934 | |
| 1935 | return 0; |
| 1936 | } |
| 1937 | |
Paul Turner | d3d9dc3 | 2011-07-21 09:43:39 -0700 | [diff] [blame] | 1938 | static void throttle_cfs_rq(struct cfs_rq *cfs_rq) |
Paul Turner | 85dac90 | 2011-07-21 09:43:33 -0700 | [diff] [blame] | 1939 | { |
| 1940 | struct rq *rq = rq_of(cfs_rq); |
| 1941 | struct cfs_bandwidth *cfs_b = tg_cfs_bandwidth(cfs_rq->tg); |
| 1942 | struct sched_entity *se; |
| 1943 | long task_delta, dequeue = 1; |
| 1944 | |
| 1945 | se = cfs_rq->tg->se[cpu_of(rq_of(cfs_rq))]; |
| 1946 | |
| 1947 | /* account load preceding throttle */ |
Paul Turner | 64660c8 | 2011-07-21 09:43:36 -0700 | [diff] [blame] | 1948 | rcu_read_lock(); |
| 1949 | walk_tg_tree_from(cfs_rq->tg, tg_throttle_down, tg_nop, (void *)rq); |
| 1950 | rcu_read_unlock(); |
Paul Turner | 85dac90 | 2011-07-21 09:43:33 -0700 | [diff] [blame] | 1951 | |
| 1952 | task_delta = cfs_rq->h_nr_running; |
| 1953 | for_each_sched_entity(se) { |
| 1954 | struct cfs_rq *qcfs_rq = cfs_rq_of(se); |
| 1955 | /* throttled entity or throttle-on-deactivate */ |
| 1956 | if (!se->on_rq) |
| 1957 | break; |
| 1958 | |
| 1959 | if (dequeue) |
| 1960 | dequeue_entity(qcfs_rq, se, DEQUEUE_SLEEP); |
| 1961 | qcfs_rq->h_nr_running -= task_delta; |
| 1962 | |
| 1963 | if (qcfs_rq->load.weight) |
| 1964 | dequeue = 0; |
| 1965 | } |
| 1966 | |
| 1967 | if (!se) |
| 1968 | rq->nr_running -= task_delta; |
| 1969 | |
| 1970 | cfs_rq->throttled = 1; |
Nikhil Rao | e8da1b1 | 2011-07-21 09:43:40 -0700 | [diff] [blame] | 1971 | cfs_rq->throttled_timestamp = rq->clock; |
Paul Turner | 85dac90 | 2011-07-21 09:43:33 -0700 | [diff] [blame] | 1972 | raw_spin_lock(&cfs_b->lock); |
| 1973 | list_add_tail_rcu(&cfs_rq->throttled_list, &cfs_b->throttled_cfs_rq); |
| 1974 | raw_spin_unlock(&cfs_b->lock); |
| 1975 | } |
| 1976 | |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 1977 | void unthrottle_cfs_rq(struct cfs_rq *cfs_rq) |
Paul Turner | 671fd9d | 2011-07-21 09:43:34 -0700 | [diff] [blame] | 1978 | { |
| 1979 | struct rq *rq = rq_of(cfs_rq); |
| 1980 | struct cfs_bandwidth *cfs_b = tg_cfs_bandwidth(cfs_rq->tg); |
| 1981 | struct sched_entity *se; |
| 1982 | int enqueue = 1; |
| 1983 | long task_delta; |
| 1984 | |
| 1985 | se = cfs_rq->tg->se[cpu_of(rq_of(cfs_rq))]; |
| 1986 | |
| 1987 | cfs_rq->throttled = 0; |
| 1988 | raw_spin_lock(&cfs_b->lock); |
Nikhil Rao | e8da1b1 | 2011-07-21 09:43:40 -0700 | [diff] [blame] | 1989 | cfs_b->throttled_time += rq->clock - cfs_rq->throttled_timestamp; |
Paul Turner | 671fd9d | 2011-07-21 09:43:34 -0700 | [diff] [blame] | 1990 | list_del_rcu(&cfs_rq->throttled_list); |
| 1991 | raw_spin_unlock(&cfs_b->lock); |
Nikhil Rao | e8da1b1 | 2011-07-21 09:43:40 -0700 | [diff] [blame] | 1992 | cfs_rq->throttled_timestamp = 0; |
Paul Turner | 671fd9d | 2011-07-21 09:43:34 -0700 | [diff] [blame] | 1993 | |
Paul Turner | 64660c8 | 2011-07-21 09:43:36 -0700 | [diff] [blame] | 1994 | update_rq_clock(rq); |
| 1995 | /* update hierarchical throttle state */ |
| 1996 | walk_tg_tree_from(cfs_rq->tg, tg_nop, tg_unthrottle_up, (void *)rq); |
| 1997 | |
Paul Turner | 671fd9d | 2011-07-21 09:43:34 -0700 | [diff] [blame] | 1998 | if (!cfs_rq->load.weight) |
| 1999 | return; |
| 2000 | |
| 2001 | task_delta = cfs_rq->h_nr_running; |
| 2002 | for_each_sched_entity(se) { |
| 2003 | if (se->on_rq) |
| 2004 | enqueue = 0; |
| 2005 | |
| 2006 | cfs_rq = cfs_rq_of(se); |
| 2007 | if (enqueue) |
| 2008 | enqueue_entity(cfs_rq, se, ENQUEUE_WAKEUP); |
| 2009 | cfs_rq->h_nr_running += task_delta; |
| 2010 | |
| 2011 | if (cfs_rq_throttled(cfs_rq)) |
| 2012 | break; |
| 2013 | } |
| 2014 | |
| 2015 | if (!se) |
| 2016 | rq->nr_running += task_delta; |
| 2017 | |
| 2018 | /* determine whether we need to wake up potentially idle cpu */ |
| 2019 | if (rq->curr == rq->idle && rq->cfs.nr_running) |
| 2020 | resched_task(rq->curr); |
| 2021 | } |
| 2022 | |
| 2023 | static u64 distribute_cfs_runtime(struct cfs_bandwidth *cfs_b, |
| 2024 | u64 remaining, u64 expires) |
| 2025 | { |
| 2026 | struct cfs_rq *cfs_rq; |
| 2027 | u64 runtime = remaining; |
| 2028 | |
| 2029 | rcu_read_lock(); |
| 2030 | list_for_each_entry_rcu(cfs_rq, &cfs_b->throttled_cfs_rq, |
| 2031 | throttled_list) { |
| 2032 | struct rq *rq = rq_of(cfs_rq); |
| 2033 | |
| 2034 | raw_spin_lock(&rq->lock); |
| 2035 | if (!cfs_rq_throttled(cfs_rq)) |
| 2036 | goto next; |
| 2037 | |
| 2038 | runtime = -cfs_rq->runtime_remaining + 1; |
| 2039 | if (runtime > remaining) |
| 2040 | runtime = remaining; |
| 2041 | remaining -= runtime; |
| 2042 | |
| 2043 | cfs_rq->runtime_remaining += runtime; |
| 2044 | cfs_rq->runtime_expires = expires; |
| 2045 | |
| 2046 | /* we check whether we're throttled above */ |
| 2047 | if (cfs_rq->runtime_remaining > 0) |
| 2048 | unthrottle_cfs_rq(cfs_rq); |
| 2049 | |
| 2050 | next: |
| 2051 | raw_spin_unlock(&rq->lock); |
| 2052 | |
| 2053 | if (!remaining) |
| 2054 | break; |
| 2055 | } |
| 2056 | rcu_read_unlock(); |
| 2057 | |
| 2058 | return remaining; |
| 2059 | } |
| 2060 | |
Paul Turner | 58088ad | 2011-07-21 09:43:31 -0700 | [diff] [blame] | 2061 | /* |
| 2062 | * Responsible for refilling a task_group's bandwidth and unthrottling its |
| 2063 | * cfs_rqs as appropriate. If there has been no activity within the last |
| 2064 | * period the timer is deactivated until scheduling resumes; cfs_b->idle is |
| 2065 | * used to track this state. |
| 2066 | */ |
| 2067 | static int do_sched_cfs_period_timer(struct cfs_bandwidth *cfs_b, int overrun) |
| 2068 | { |
Paul Turner | 671fd9d | 2011-07-21 09:43:34 -0700 | [diff] [blame] | 2069 | u64 runtime, runtime_expires; |
| 2070 | int idle = 1, throttled; |
Paul Turner | 58088ad | 2011-07-21 09:43:31 -0700 | [diff] [blame] | 2071 | |
| 2072 | raw_spin_lock(&cfs_b->lock); |
| 2073 | /* no need to continue the timer with no bandwidth constraint */ |
| 2074 | if (cfs_b->quota == RUNTIME_INF) |
| 2075 | goto out_unlock; |
| 2076 | |
Paul Turner | 671fd9d | 2011-07-21 09:43:34 -0700 | [diff] [blame] | 2077 | throttled = !list_empty(&cfs_b->throttled_cfs_rq); |
| 2078 | /* idle depends on !throttled (for the case of a large deficit) */ |
| 2079 | idle = cfs_b->idle && !throttled; |
Nikhil Rao | e8da1b1 | 2011-07-21 09:43:40 -0700 | [diff] [blame] | 2080 | cfs_b->nr_periods += overrun; |
Paul Turner | 671fd9d | 2011-07-21 09:43:34 -0700 | [diff] [blame] | 2081 | |
Paul Turner | a9cf55b | 2011-07-21 09:43:32 -0700 | [diff] [blame] | 2082 | /* if we're going inactive then everything else can be deferred */ |
| 2083 | if (idle) |
| 2084 | goto out_unlock; |
| 2085 | |
| 2086 | __refill_cfs_bandwidth_runtime(cfs_b); |
| 2087 | |
Paul Turner | 671fd9d | 2011-07-21 09:43:34 -0700 | [diff] [blame] | 2088 | if (!throttled) { |
| 2089 | /* mark as potentially idle for the upcoming period */ |
| 2090 | cfs_b->idle = 1; |
| 2091 | goto out_unlock; |
| 2092 | } |
Paul Turner | 58088ad | 2011-07-21 09:43:31 -0700 | [diff] [blame] | 2093 | |
Nikhil Rao | e8da1b1 | 2011-07-21 09:43:40 -0700 | [diff] [blame] | 2094 | /* account preceding periods in which throttling occurred */ |
| 2095 | cfs_b->nr_throttled += overrun; |
| 2096 | |
Paul Turner | 671fd9d | 2011-07-21 09:43:34 -0700 | [diff] [blame] | 2097 | /* |
| 2098 | * There are throttled entities so we must first use the new bandwidth |
| 2099 | * to unthrottle them before making it generally available. This |
| 2100 | * ensures that all existing debts will be paid before a new cfs_rq is |
| 2101 | * allowed to run. |
| 2102 | */ |
| 2103 | runtime = cfs_b->runtime; |
| 2104 | runtime_expires = cfs_b->runtime_expires; |
| 2105 | cfs_b->runtime = 0; |
| 2106 | |
| 2107 | /* |
| 2108 | * This check is repeated as we are holding onto the new bandwidth |
| 2109 | * while we unthrottle. This can potentially race with an unthrottled |
| 2110 | * group trying to acquire new bandwidth from the global pool. |
| 2111 | */ |
| 2112 | while (throttled && runtime > 0) { |
| 2113 | raw_spin_unlock(&cfs_b->lock); |
| 2114 | /* we can't nest cfs_b->lock while distributing bandwidth */ |
| 2115 | runtime = distribute_cfs_runtime(cfs_b, runtime, |
| 2116 | runtime_expires); |
| 2117 | raw_spin_lock(&cfs_b->lock); |
| 2118 | |
| 2119 | throttled = !list_empty(&cfs_b->throttled_cfs_rq); |
| 2120 | } |
| 2121 | |
| 2122 | /* return (any) remaining runtime */ |
| 2123 | cfs_b->runtime = runtime; |
| 2124 | /* |
| 2125 | * While we are ensured activity in the period following an |
| 2126 | * unthrottle, this also covers the case in which the new bandwidth is |
| 2127 | * insufficient to cover the existing bandwidth deficit. (Forcing the |
| 2128 | * timer to remain active while there are any throttled entities.) |
| 2129 | */ |
| 2130 | cfs_b->idle = 0; |
Paul Turner | 58088ad | 2011-07-21 09:43:31 -0700 | [diff] [blame] | 2131 | out_unlock: |
| 2132 | if (idle) |
| 2133 | cfs_b->timer_active = 0; |
| 2134 | raw_spin_unlock(&cfs_b->lock); |
| 2135 | |
| 2136 | return idle; |
| 2137 | } |
Paul Turner | d3d9dc3 | 2011-07-21 09:43:39 -0700 | [diff] [blame] | 2138 | |
Paul Turner | d8b4986 | 2011-07-21 09:43:41 -0700 | [diff] [blame] | 2139 | /* a cfs_rq won't donate quota below this amount */ |
| 2140 | static const u64 min_cfs_rq_runtime = 1 * NSEC_PER_MSEC; |
| 2141 | /* minimum remaining period time to redistribute slack quota */ |
| 2142 | static const u64 min_bandwidth_expiration = 2 * NSEC_PER_MSEC; |
| 2143 | /* how long we wait to gather additional slack before distributing */ |
| 2144 | static const u64 cfs_bandwidth_slack_period = 5 * NSEC_PER_MSEC; |
| 2145 | |
| 2146 | /* are we near the end of the current quota period? */ |
| 2147 | static int runtime_refresh_within(struct cfs_bandwidth *cfs_b, u64 min_expire) |
| 2148 | { |
| 2149 | struct hrtimer *refresh_timer = &cfs_b->period_timer; |
| 2150 | u64 remaining; |
| 2151 | |
| 2152 | /* if the call-back is running a quota refresh is already occurring */ |
| 2153 | if (hrtimer_callback_running(refresh_timer)) |
| 2154 | return 1; |
| 2155 | |
| 2156 | /* is a quota refresh about to occur? */ |
| 2157 | remaining = ktime_to_ns(hrtimer_expires_remaining(refresh_timer)); |
| 2158 | if (remaining < min_expire) |
| 2159 | return 1; |
| 2160 | |
| 2161 | return 0; |
| 2162 | } |
| 2163 | |
| 2164 | static void start_cfs_slack_bandwidth(struct cfs_bandwidth *cfs_b) |
| 2165 | { |
| 2166 | u64 min_left = cfs_bandwidth_slack_period + min_bandwidth_expiration; |
| 2167 | |
| 2168 | /* if there's a quota refresh soon don't bother with slack */ |
| 2169 | if (runtime_refresh_within(cfs_b, min_left)) |
| 2170 | return; |
| 2171 | |
| 2172 | start_bandwidth_timer(&cfs_b->slack_timer, |
| 2173 | ns_to_ktime(cfs_bandwidth_slack_period)); |
| 2174 | } |
| 2175 | |
| 2176 | /* we know any runtime found here is valid as update_curr() precedes return */ |
| 2177 | static void __return_cfs_rq_runtime(struct cfs_rq *cfs_rq) |
| 2178 | { |
| 2179 | struct cfs_bandwidth *cfs_b = tg_cfs_bandwidth(cfs_rq->tg); |
| 2180 | s64 slack_runtime = cfs_rq->runtime_remaining - min_cfs_rq_runtime; |
| 2181 | |
| 2182 | if (slack_runtime <= 0) |
| 2183 | return; |
| 2184 | |
| 2185 | raw_spin_lock(&cfs_b->lock); |
| 2186 | if (cfs_b->quota != RUNTIME_INF && |
| 2187 | cfs_rq->runtime_expires == cfs_b->runtime_expires) { |
| 2188 | cfs_b->runtime += slack_runtime; |
| 2189 | |
| 2190 | /* we are under rq->lock, defer unthrottling using a timer */ |
| 2191 | if (cfs_b->runtime > sched_cfs_bandwidth_slice() && |
| 2192 | !list_empty(&cfs_b->throttled_cfs_rq)) |
| 2193 | start_cfs_slack_bandwidth(cfs_b); |
| 2194 | } |
| 2195 | raw_spin_unlock(&cfs_b->lock); |
| 2196 | |
| 2197 | /* even if it's not valid for return we don't want to try again */ |
| 2198 | cfs_rq->runtime_remaining -= slack_runtime; |
| 2199 | } |
| 2200 | |
| 2201 | static __always_inline void return_cfs_rq_runtime(struct cfs_rq *cfs_rq) |
| 2202 | { |
Paul Turner | 56f570e | 2011-11-07 20:26:33 -0800 | [diff] [blame] | 2203 | if (!cfs_bandwidth_used()) |
| 2204 | return; |
| 2205 | |
Paul Turner | fccfdc6 | 2011-11-07 20:26:34 -0800 | [diff] [blame] | 2206 | if (!cfs_rq->runtime_enabled || cfs_rq->nr_running) |
Paul Turner | d8b4986 | 2011-07-21 09:43:41 -0700 | [diff] [blame] | 2207 | return; |
| 2208 | |
| 2209 | __return_cfs_rq_runtime(cfs_rq); |
| 2210 | } |
| 2211 | |
| 2212 | /* |
| 2213 | * This is done with a timer (instead of inline with bandwidth return) since |
| 2214 | * it's necessary to juggle rq->locks to unthrottle their respective cfs_rqs. |
| 2215 | */ |
| 2216 | static void do_sched_cfs_slack_timer(struct cfs_bandwidth *cfs_b) |
| 2217 | { |
| 2218 | u64 runtime = 0, slice = sched_cfs_bandwidth_slice(); |
| 2219 | u64 expires; |
| 2220 | |
| 2221 | /* confirm we're still not at a refresh boundary */ |
| 2222 | if (runtime_refresh_within(cfs_b, min_bandwidth_expiration)) |
| 2223 | return; |
| 2224 | |
| 2225 | raw_spin_lock(&cfs_b->lock); |
| 2226 | if (cfs_b->quota != RUNTIME_INF && cfs_b->runtime > slice) { |
| 2227 | runtime = cfs_b->runtime; |
| 2228 | cfs_b->runtime = 0; |
| 2229 | } |
| 2230 | expires = cfs_b->runtime_expires; |
| 2231 | raw_spin_unlock(&cfs_b->lock); |
| 2232 | |
| 2233 | if (!runtime) |
| 2234 | return; |
| 2235 | |
| 2236 | runtime = distribute_cfs_runtime(cfs_b, runtime, expires); |
| 2237 | |
| 2238 | raw_spin_lock(&cfs_b->lock); |
| 2239 | if (expires == cfs_b->runtime_expires) |
| 2240 | cfs_b->runtime = runtime; |
| 2241 | raw_spin_unlock(&cfs_b->lock); |
| 2242 | } |
| 2243 | |
Paul Turner | d3d9dc3 | 2011-07-21 09:43:39 -0700 | [diff] [blame] | 2244 | /* |
| 2245 | * When a group wakes up we want to make sure that its quota is not already |
| 2246 | * expired/exceeded, otherwise it may be allowed to steal additional ticks of |
| 2247 | * runtime as update_curr() throttling can not not trigger until it's on-rq. |
| 2248 | */ |
| 2249 | static void check_enqueue_throttle(struct cfs_rq *cfs_rq) |
| 2250 | { |
Paul Turner | 56f570e | 2011-11-07 20:26:33 -0800 | [diff] [blame] | 2251 | if (!cfs_bandwidth_used()) |
| 2252 | return; |
| 2253 | |
Paul Turner | d3d9dc3 | 2011-07-21 09:43:39 -0700 | [diff] [blame] | 2254 | /* an active group must be handled by the update_curr()->put() path */ |
| 2255 | if (!cfs_rq->runtime_enabled || cfs_rq->curr) |
| 2256 | return; |
| 2257 | |
| 2258 | /* ensure the group is not already throttled */ |
| 2259 | if (cfs_rq_throttled(cfs_rq)) |
| 2260 | return; |
| 2261 | |
| 2262 | /* update runtime allocation */ |
| 2263 | account_cfs_rq_runtime(cfs_rq, 0); |
| 2264 | if (cfs_rq->runtime_remaining <= 0) |
| 2265 | throttle_cfs_rq(cfs_rq); |
| 2266 | } |
| 2267 | |
| 2268 | /* conditionally throttle active cfs_rq's from put_prev_entity() */ |
| 2269 | static void check_cfs_rq_runtime(struct cfs_rq *cfs_rq) |
| 2270 | { |
Paul Turner | 56f570e | 2011-11-07 20:26:33 -0800 | [diff] [blame] | 2271 | if (!cfs_bandwidth_used()) |
| 2272 | return; |
| 2273 | |
Paul Turner | d3d9dc3 | 2011-07-21 09:43:39 -0700 | [diff] [blame] | 2274 | if (likely(!cfs_rq->runtime_enabled || cfs_rq->runtime_remaining > 0)) |
| 2275 | return; |
| 2276 | |
| 2277 | /* |
| 2278 | * it's possible for a throttled entity to be forced into a running |
| 2279 | * state (e.g. set_curr_task), in this case we're finished. |
| 2280 | */ |
| 2281 | if (cfs_rq_throttled(cfs_rq)) |
| 2282 | return; |
| 2283 | |
| 2284 | throttle_cfs_rq(cfs_rq); |
| 2285 | } |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 2286 | |
| 2287 | static inline u64 default_cfs_period(void); |
| 2288 | static int do_sched_cfs_period_timer(struct cfs_bandwidth *cfs_b, int overrun); |
| 2289 | static void do_sched_cfs_slack_timer(struct cfs_bandwidth *cfs_b); |
| 2290 | |
| 2291 | static enum hrtimer_restart sched_cfs_slack_timer(struct hrtimer *timer) |
| 2292 | { |
| 2293 | struct cfs_bandwidth *cfs_b = |
| 2294 | container_of(timer, struct cfs_bandwidth, slack_timer); |
| 2295 | do_sched_cfs_slack_timer(cfs_b); |
| 2296 | |
| 2297 | return HRTIMER_NORESTART; |
| 2298 | } |
| 2299 | |
| 2300 | static enum hrtimer_restart sched_cfs_period_timer(struct hrtimer *timer) |
| 2301 | { |
| 2302 | struct cfs_bandwidth *cfs_b = |
| 2303 | container_of(timer, struct cfs_bandwidth, period_timer); |
| 2304 | ktime_t now; |
| 2305 | int overrun; |
| 2306 | int idle = 0; |
| 2307 | |
| 2308 | for (;;) { |
| 2309 | now = hrtimer_cb_get_time(timer); |
| 2310 | overrun = hrtimer_forward(timer, now, cfs_b->period); |
| 2311 | |
| 2312 | if (!overrun) |
| 2313 | break; |
| 2314 | |
| 2315 | idle = do_sched_cfs_period_timer(cfs_b, overrun); |
| 2316 | } |
| 2317 | |
| 2318 | return idle ? HRTIMER_NORESTART : HRTIMER_RESTART; |
| 2319 | } |
| 2320 | |
| 2321 | void init_cfs_bandwidth(struct cfs_bandwidth *cfs_b) |
| 2322 | { |
| 2323 | raw_spin_lock_init(&cfs_b->lock); |
| 2324 | cfs_b->runtime = 0; |
| 2325 | cfs_b->quota = RUNTIME_INF; |
| 2326 | cfs_b->period = ns_to_ktime(default_cfs_period()); |
| 2327 | |
| 2328 | INIT_LIST_HEAD(&cfs_b->throttled_cfs_rq); |
| 2329 | hrtimer_init(&cfs_b->period_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL); |
| 2330 | cfs_b->period_timer.function = sched_cfs_period_timer; |
| 2331 | hrtimer_init(&cfs_b->slack_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL); |
| 2332 | cfs_b->slack_timer.function = sched_cfs_slack_timer; |
| 2333 | } |
| 2334 | |
| 2335 | static void init_cfs_rq_runtime(struct cfs_rq *cfs_rq) |
| 2336 | { |
| 2337 | cfs_rq->runtime_enabled = 0; |
| 2338 | INIT_LIST_HEAD(&cfs_rq->throttled_list); |
| 2339 | } |
| 2340 | |
| 2341 | /* requires cfs_b->lock, may release to reprogram timer */ |
| 2342 | void __start_cfs_bandwidth(struct cfs_bandwidth *cfs_b) |
| 2343 | { |
| 2344 | /* |
| 2345 | * The timer may be active because we're trying to set a new bandwidth |
| 2346 | * period or because we're racing with the tear-down path |
| 2347 | * (timer_active==0 becomes visible before the hrtimer call-back |
| 2348 | * terminates). In either case we ensure that it's re-programmed |
| 2349 | */ |
| 2350 | while (unlikely(hrtimer_active(&cfs_b->period_timer))) { |
| 2351 | raw_spin_unlock(&cfs_b->lock); |
| 2352 | /* ensure cfs_b->lock is available while we wait */ |
| 2353 | hrtimer_cancel(&cfs_b->period_timer); |
| 2354 | |
| 2355 | raw_spin_lock(&cfs_b->lock); |
| 2356 | /* if someone else restarted the timer then we're done */ |
| 2357 | if (cfs_b->timer_active) |
| 2358 | return; |
| 2359 | } |
| 2360 | |
| 2361 | cfs_b->timer_active = 1; |
| 2362 | start_bandwidth_timer(&cfs_b->period_timer, cfs_b->period); |
| 2363 | } |
| 2364 | |
| 2365 | static void destroy_cfs_bandwidth(struct cfs_bandwidth *cfs_b) |
| 2366 | { |
| 2367 | hrtimer_cancel(&cfs_b->period_timer); |
| 2368 | hrtimer_cancel(&cfs_b->slack_timer); |
| 2369 | } |
| 2370 | |
Peter Boonstoppel | a4c96ae | 2012-08-09 15:34:47 -0700 | [diff] [blame] | 2371 | static void unthrottle_offline_cfs_rqs(struct rq *rq) |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 2372 | { |
| 2373 | struct cfs_rq *cfs_rq; |
| 2374 | |
| 2375 | for_each_leaf_cfs_rq(rq, cfs_rq) { |
| 2376 | struct cfs_bandwidth *cfs_b = tg_cfs_bandwidth(cfs_rq->tg); |
| 2377 | |
| 2378 | if (!cfs_rq->runtime_enabled) |
| 2379 | continue; |
| 2380 | |
| 2381 | /* |
| 2382 | * clock_task is not advancing so we just need to make sure |
| 2383 | * there's some valid quota amount |
| 2384 | */ |
| 2385 | cfs_rq->runtime_remaining = cfs_b->quota; |
| 2386 | if (cfs_rq_throttled(cfs_rq)) |
| 2387 | unthrottle_cfs_rq(cfs_rq); |
| 2388 | } |
| 2389 | } |
| 2390 | |
| 2391 | #else /* CONFIG_CFS_BANDWIDTH */ |
Peter Zijlstra | 6c16a6d | 2012-03-21 13:07:16 -0700 | [diff] [blame] | 2392 | static __always_inline |
| 2393 | void account_cfs_rq_runtime(struct cfs_rq *cfs_rq, unsigned long delta_exec) {} |
Paul Turner | d3d9dc3 | 2011-07-21 09:43:39 -0700 | [diff] [blame] | 2394 | static void check_cfs_rq_runtime(struct cfs_rq *cfs_rq) {} |
| 2395 | static void check_enqueue_throttle(struct cfs_rq *cfs_rq) {} |
Peter Zijlstra | 6c16a6d | 2012-03-21 13:07:16 -0700 | [diff] [blame] | 2396 | static __always_inline void return_cfs_rq_runtime(struct cfs_rq *cfs_rq) {} |
Paul Turner | 85dac90 | 2011-07-21 09:43:33 -0700 | [diff] [blame] | 2397 | |
| 2398 | static inline int cfs_rq_throttled(struct cfs_rq *cfs_rq) |
| 2399 | { |
| 2400 | return 0; |
| 2401 | } |
Paul Turner | 64660c8 | 2011-07-21 09:43:36 -0700 | [diff] [blame] | 2402 | |
| 2403 | static inline int throttled_hierarchy(struct cfs_rq *cfs_rq) |
| 2404 | { |
| 2405 | return 0; |
| 2406 | } |
| 2407 | |
| 2408 | static inline int throttled_lb_pair(struct task_group *tg, |
| 2409 | int src_cpu, int dest_cpu) |
| 2410 | { |
| 2411 | return 0; |
| 2412 | } |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 2413 | |
| 2414 | void init_cfs_bandwidth(struct cfs_bandwidth *cfs_b) {} |
| 2415 | |
| 2416 | #ifdef CONFIG_FAIR_GROUP_SCHED |
| 2417 | static void init_cfs_rq_runtime(struct cfs_rq *cfs_rq) {} |
Paul Turner | ab84d31 | 2011-07-21 09:43:28 -0700 | [diff] [blame] | 2418 | #endif |
| 2419 | |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 2420 | static inline struct cfs_bandwidth *tg_cfs_bandwidth(struct task_group *tg) |
| 2421 | { |
| 2422 | return NULL; |
| 2423 | } |
| 2424 | static inline void destroy_cfs_bandwidth(struct cfs_bandwidth *cfs_b) {} |
Peter Boonstoppel | a4c96ae | 2012-08-09 15:34:47 -0700 | [diff] [blame] | 2425 | static inline void unthrottle_offline_cfs_rqs(struct rq *rq) {} |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 2426 | |
| 2427 | #endif /* CONFIG_CFS_BANDWIDTH */ |
| 2428 | |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 2429 | /************************************************** |
| 2430 | * CFS operations on tasks: |
| 2431 | */ |
| 2432 | |
Peter Zijlstra | 8f4d37e | 2008-01-25 21:08:29 +0100 | [diff] [blame] | 2433 | #ifdef CONFIG_SCHED_HRTICK |
| 2434 | static void hrtick_start_fair(struct rq *rq, struct task_struct *p) |
| 2435 | { |
Peter Zijlstra | 8f4d37e | 2008-01-25 21:08:29 +0100 | [diff] [blame] | 2436 | struct sched_entity *se = &p->se; |
| 2437 | struct cfs_rq *cfs_rq = cfs_rq_of(se); |
| 2438 | |
| 2439 | WARN_ON(task_rq(p) != rq); |
| 2440 | |
Mike Galbraith | b39e66e | 2011-11-22 15:20:07 +0100 | [diff] [blame] | 2441 | if (cfs_rq->nr_running > 1) { |
Peter Zijlstra | 8f4d37e | 2008-01-25 21:08:29 +0100 | [diff] [blame] | 2442 | u64 slice = sched_slice(cfs_rq, se); |
| 2443 | u64 ran = se->sum_exec_runtime - se->prev_sum_exec_runtime; |
| 2444 | s64 delta = slice - ran; |
| 2445 | |
| 2446 | if (delta < 0) { |
| 2447 | if (rq->curr == p) |
| 2448 | resched_task(p); |
| 2449 | return; |
| 2450 | } |
| 2451 | |
| 2452 | /* |
| 2453 | * Don't schedule slices shorter than 10000ns, that just |
| 2454 | * doesn't make sense. Rely on vruntime for fairness. |
| 2455 | */ |
Peter Zijlstra | 3165651 | 2008-07-18 18:01:23 +0200 | [diff] [blame] | 2456 | if (rq->curr != p) |
Peter Zijlstra | 157124c | 2008-07-28 11:53:11 +0200 | [diff] [blame] | 2457 | delta = max_t(s64, 10000LL, delta); |
Peter Zijlstra | 8f4d37e | 2008-01-25 21:08:29 +0100 | [diff] [blame] | 2458 | |
Peter Zijlstra | 3165651 | 2008-07-18 18:01:23 +0200 | [diff] [blame] | 2459 | hrtick_start(rq, delta); |
Peter Zijlstra | 8f4d37e | 2008-01-25 21:08:29 +0100 | [diff] [blame] | 2460 | } |
| 2461 | } |
Peter Zijlstra | a4c2f00 | 2008-10-17 19:27:03 +0200 | [diff] [blame] | 2462 | |
| 2463 | /* |
| 2464 | * called from enqueue/dequeue and updates the hrtick when the |
| 2465 | * current task is from our class and nr_running is low enough |
| 2466 | * to matter. |
| 2467 | */ |
| 2468 | static void hrtick_update(struct rq *rq) |
| 2469 | { |
| 2470 | struct task_struct *curr = rq->curr; |
| 2471 | |
Mike Galbraith | b39e66e | 2011-11-22 15:20:07 +0100 | [diff] [blame] | 2472 | if (!hrtick_enabled(rq) || curr->sched_class != &fair_sched_class) |
Peter Zijlstra | a4c2f00 | 2008-10-17 19:27:03 +0200 | [diff] [blame] | 2473 | return; |
| 2474 | |
| 2475 | if (cfs_rq_of(&curr->se)->nr_running < sched_nr_latency) |
| 2476 | hrtick_start_fair(rq, curr); |
| 2477 | } |
Dhaval Giani | 55e12e5 | 2008-06-24 23:39:43 +0530 | [diff] [blame] | 2478 | #else /* !CONFIG_SCHED_HRTICK */ |
Peter Zijlstra | 8f4d37e | 2008-01-25 21:08:29 +0100 | [diff] [blame] | 2479 | static inline void |
| 2480 | hrtick_start_fair(struct rq *rq, struct task_struct *p) |
| 2481 | { |
| 2482 | } |
Peter Zijlstra | a4c2f00 | 2008-10-17 19:27:03 +0200 | [diff] [blame] | 2483 | |
| 2484 | static inline void hrtick_update(struct rq *rq) |
| 2485 | { |
| 2486 | } |
Peter Zijlstra | 8f4d37e | 2008-01-25 21:08:29 +0100 | [diff] [blame] | 2487 | #endif |
| 2488 | |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 2489 | /* |
| 2490 | * The enqueue_task method is called before nr_running is |
| 2491 | * increased. Here we update the fair scheduling stats and |
| 2492 | * then put the task into the rbtree: |
| 2493 | */ |
Thomas Gleixner | ea87bb7 | 2010-01-20 20:58:57 +0000 | [diff] [blame] | 2494 | static void |
Peter Zijlstra | 371fd7e | 2010-03-24 16:38:48 +0100 | [diff] [blame] | 2495 | enqueue_task_fair(struct rq *rq, struct task_struct *p, int flags) |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 2496 | { |
| 2497 | struct cfs_rq *cfs_rq; |
Peter Zijlstra | 62fb185 | 2008-02-25 17:34:02 +0100 | [diff] [blame] | 2498 | struct sched_entity *se = &p->se; |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 2499 | |
| 2500 | for_each_sched_entity(se) { |
Peter Zijlstra | 62fb185 | 2008-02-25 17:34:02 +0100 | [diff] [blame] | 2501 | if (se->on_rq) |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 2502 | break; |
| 2503 | cfs_rq = cfs_rq_of(se); |
Peter Zijlstra | 88ec22d | 2009-12-16 18:04:41 +0100 | [diff] [blame] | 2504 | enqueue_entity(cfs_rq, se, flags); |
Paul Turner | 85dac90 | 2011-07-21 09:43:33 -0700 | [diff] [blame] | 2505 | |
| 2506 | /* |
| 2507 | * end evaluation on encountering a throttled cfs_rq |
| 2508 | * |
| 2509 | * note: in the case of encountering a throttled cfs_rq we will |
| 2510 | * post the final h_nr_running increment below. |
| 2511 | */ |
| 2512 | if (cfs_rq_throttled(cfs_rq)) |
| 2513 | break; |
Paul Turner | 953bfcd | 2011-07-21 09:43:27 -0700 | [diff] [blame] | 2514 | cfs_rq->h_nr_running++; |
Paul Turner | 85dac90 | 2011-07-21 09:43:33 -0700 | [diff] [blame] | 2515 | |
Peter Zijlstra | 88ec22d | 2009-12-16 18:04:41 +0100 | [diff] [blame] | 2516 | flags = ENQUEUE_WAKEUP; |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 2517 | } |
Peter Zijlstra | 8f4d37e | 2008-01-25 21:08:29 +0100 | [diff] [blame] | 2518 | |
Peter Zijlstra | 2069dd7 | 2010-11-15 15:47:00 -0800 | [diff] [blame] | 2519 | for_each_sched_entity(se) { |
Lin Ming | 0f31714 | 2011-07-22 09:14:31 +0800 | [diff] [blame] | 2520 | cfs_rq = cfs_rq_of(se); |
Paul Turner | 953bfcd | 2011-07-21 09:43:27 -0700 | [diff] [blame] | 2521 | cfs_rq->h_nr_running++; |
Peter Zijlstra | 2069dd7 | 2010-11-15 15:47:00 -0800 | [diff] [blame] | 2522 | |
Paul Turner | 85dac90 | 2011-07-21 09:43:33 -0700 | [diff] [blame] | 2523 | if (cfs_rq_throttled(cfs_rq)) |
| 2524 | break; |
| 2525 | |
Paul Turner | d6b5591 | 2010-11-15 15:47:09 -0800 | [diff] [blame] | 2526 | update_cfs_load(cfs_rq, 0); |
Paul Turner | 6d5ab29 | 2011-01-21 20:45:01 -0800 | [diff] [blame] | 2527 | update_cfs_shares(cfs_rq); |
Paul Turner | 9ee474f | 2012-10-04 13:18:30 +0200 | [diff] [blame] | 2528 | update_entity_load_avg(se, 1); |
Peter Zijlstra | 2069dd7 | 2010-11-15 15:47:00 -0800 | [diff] [blame] | 2529 | } |
| 2530 | |
Ben Segall | 18bf280 | 2012-10-04 12:51:20 +0200 | [diff] [blame] | 2531 | if (!se) { |
| 2532 | update_rq_runnable_avg(rq, rq->nr_running); |
Paul Turner | 85dac90 | 2011-07-21 09:43:33 -0700 | [diff] [blame] | 2533 | inc_nr_running(rq); |
Ben Segall | 18bf280 | 2012-10-04 12:51:20 +0200 | [diff] [blame] | 2534 | } |
Peter Zijlstra | a4c2f00 | 2008-10-17 19:27:03 +0200 | [diff] [blame] | 2535 | hrtick_update(rq); |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 2536 | } |
| 2537 | |
Venkatesh Pallipadi | 2f36825 | 2011-04-14 10:30:53 -0700 | [diff] [blame] | 2538 | static void set_next_buddy(struct sched_entity *se); |
| 2539 | |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 2540 | /* |
| 2541 | * The dequeue_task method is called before nr_running is |
| 2542 | * decreased. We remove the task from the rbtree and |
| 2543 | * update the fair scheduling stats: |
| 2544 | */ |
Peter Zijlstra | 371fd7e | 2010-03-24 16:38:48 +0100 | [diff] [blame] | 2545 | 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] | 2546 | { |
| 2547 | struct cfs_rq *cfs_rq; |
Peter Zijlstra | 62fb185 | 2008-02-25 17:34:02 +0100 | [diff] [blame] | 2548 | struct sched_entity *se = &p->se; |
Venkatesh Pallipadi | 2f36825 | 2011-04-14 10:30:53 -0700 | [diff] [blame] | 2549 | int task_sleep = flags & DEQUEUE_SLEEP; |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 2550 | |
| 2551 | for_each_sched_entity(se) { |
| 2552 | cfs_rq = cfs_rq_of(se); |
Peter Zijlstra | 371fd7e | 2010-03-24 16:38:48 +0100 | [diff] [blame] | 2553 | dequeue_entity(cfs_rq, se, flags); |
Paul Turner | 85dac90 | 2011-07-21 09:43:33 -0700 | [diff] [blame] | 2554 | |
| 2555 | /* |
| 2556 | * end evaluation on encountering a throttled cfs_rq |
| 2557 | * |
| 2558 | * note: in the case of encountering a throttled cfs_rq we will |
| 2559 | * post the final h_nr_running decrement below. |
| 2560 | */ |
| 2561 | if (cfs_rq_throttled(cfs_rq)) |
| 2562 | break; |
Paul Turner | 953bfcd | 2011-07-21 09:43:27 -0700 | [diff] [blame] | 2563 | cfs_rq->h_nr_running--; |
Peter Zijlstra | 2069dd7 | 2010-11-15 15:47:00 -0800 | [diff] [blame] | 2564 | |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 2565 | /* Don't dequeue parent if it has other entities besides us */ |
Venkatesh Pallipadi | 2f36825 | 2011-04-14 10:30:53 -0700 | [diff] [blame] | 2566 | if (cfs_rq->load.weight) { |
| 2567 | /* |
| 2568 | * Bias pick_next to pick a task from this cfs_rq, as |
| 2569 | * p is sleeping when it is within its sched_slice. |
| 2570 | */ |
| 2571 | if (task_sleep && parent_entity(se)) |
| 2572 | set_next_buddy(parent_entity(se)); |
Paul Turner | 9598c82 | 2011-07-06 22:30:37 -0700 | [diff] [blame] | 2573 | |
| 2574 | /* avoid re-evaluating load for this entity */ |
| 2575 | se = parent_entity(se); |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 2576 | break; |
Venkatesh Pallipadi | 2f36825 | 2011-04-14 10:30:53 -0700 | [diff] [blame] | 2577 | } |
Peter Zijlstra | 371fd7e | 2010-03-24 16:38:48 +0100 | [diff] [blame] | 2578 | flags |= DEQUEUE_SLEEP; |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 2579 | } |
Peter Zijlstra | 8f4d37e | 2008-01-25 21:08:29 +0100 | [diff] [blame] | 2580 | |
Peter Zijlstra | 2069dd7 | 2010-11-15 15:47:00 -0800 | [diff] [blame] | 2581 | for_each_sched_entity(se) { |
Lin Ming | 0f31714 | 2011-07-22 09:14:31 +0800 | [diff] [blame] | 2582 | cfs_rq = cfs_rq_of(se); |
Paul Turner | 953bfcd | 2011-07-21 09:43:27 -0700 | [diff] [blame] | 2583 | cfs_rq->h_nr_running--; |
Peter Zijlstra | 2069dd7 | 2010-11-15 15:47:00 -0800 | [diff] [blame] | 2584 | |
Paul Turner | 85dac90 | 2011-07-21 09:43:33 -0700 | [diff] [blame] | 2585 | if (cfs_rq_throttled(cfs_rq)) |
| 2586 | break; |
| 2587 | |
Paul Turner | d6b5591 | 2010-11-15 15:47:09 -0800 | [diff] [blame] | 2588 | update_cfs_load(cfs_rq, 0); |
Paul Turner | 6d5ab29 | 2011-01-21 20:45:01 -0800 | [diff] [blame] | 2589 | update_cfs_shares(cfs_rq); |
Paul Turner | 9ee474f | 2012-10-04 13:18:30 +0200 | [diff] [blame] | 2590 | update_entity_load_avg(se, 1); |
Peter Zijlstra | 2069dd7 | 2010-11-15 15:47:00 -0800 | [diff] [blame] | 2591 | } |
| 2592 | |
Ben Segall | 18bf280 | 2012-10-04 12:51:20 +0200 | [diff] [blame] | 2593 | if (!se) { |
Paul Turner | 85dac90 | 2011-07-21 09:43:33 -0700 | [diff] [blame] | 2594 | dec_nr_running(rq); |
Ben Segall | 18bf280 | 2012-10-04 12:51:20 +0200 | [diff] [blame] | 2595 | update_rq_runnable_avg(rq, 1); |
| 2596 | } |
Peter Zijlstra | a4c2f00 | 2008-10-17 19:27:03 +0200 | [diff] [blame] | 2597 | hrtick_update(rq); |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 2598 | } |
| 2599 | |
Gregory Haskins | e7693a3 | 2008-01-25 21:08:09 +0100 | [diff] [blame] | 2600 | #ifdef CONFIG_SMP |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 2601 | /* Used instead of source_load when we know the type == 0 */ |
| 2602 | static unsigned long weighted_cpuload(const int cpu) |
| 2603 | { |
| 2604 | return cpu_rq(cpu)->load.weight; |
| 2605 | } |
| 2606 | |
| 2607 | /* |
| 2608 | * Return a low guess at the load of a migration-source cpu weighted |
| 2609 | * according to the scheduling class and "nice" value. |
| 2610 | * |
| 2611 | * We want to under-estimate the load of migration sources, to |
| 2612 | * balance conservatively. |
| 2613 | */ |
| 2614 | static unsigned long source_load(int cpu, int type) |
| 2615 | { |
| 2616 | struct rq *rq = cpu_rq(cpu); |
| 2617 | unsigned long total = weighted_cpuload(cpu); |
| 2618 | |
| 2619 | if (type == 0 || !sched_feat(LB_BIAS)) |
| 2620 | return total; |
| 2621 | |
| 2622 | return min(rq->cpu_load[type-1], total); |
| 2623 | } |
| 2624 | |
| 2625 | /* |
| 2626 | * Return a high guess at the load of a migration-target cpu weighted |
| 2627 | * according to the scheduling class and "nice" value. |
| 2628 | */ |
| 2629 | static unsigned long target_load(int cpu, int type) |
| 2630 | { |
| 2631 | struct rq *rq = cpu_rq(cpu); |
| 2632 | unsigned long total = weighted_cpuload(cpu); |
| 2633 | |
| 2634 | if (type == 0 || !sched_feat(LB_BIAS)) |
| 2635 | return total; |
| 2636 | |
| 2637 | return max(rq->cpu_load[type-1], total); |
| 2638 | } |
| 2639 | |
| 2640 | static unsigned long power_of(int cpu) |
| 2641 | { |
| 2642 | return cpu_rq(cpu)->cpu_power; |
| 2643 | } |
| 2644 | |
| 2645 | static unsigned long cpu_avg_load_per_task(int cpu) |
| 2646 | { |
| 2647 | struct rq *rq = cpu_rq(cpu); |
| 2648 | unsigned long nr_running = ACCESS_ONCE(rq->nr_running); |
| 2649 | |
| 2650 | if (nr_running) |
| 2651 | return rq->load.weight / nr_running; |
| 2652 | |
| 2653 | return 0; |
| 2654 | } |
| 2655 | |
Ingo Molnar | 098fb9d | 2008-03-16 20:36:10 +0100 | [diff] [blame] | 2656 | |
Peter Zijlstra | 74f8e4b | 2011-04-05 17:23:47 +0200 | [diff] [blame] | 2657 | static void task_waking_fair(struct task_struct *p) |
Peter Zijlstra | 88ec22d | 2009-12-16 18:04:41 +0100 | [diff] [blame] | 2658 | { |
| 2659 | struct sched_entity *se = &p->se; |
| 2660 | struct cfs_rq *cfs_rq = cfs_rq_of(se); |
Peter Zijlstra | 3fe1698 | 2011-04-05 17:23:48 +0200 | [diff] [blame] | 2661 | u64 min_vruntime; |
Peter Zijlstra | 88ec22d | 2009-12-16 18:04:41 +0100 | [diff] [blame] | 2662 | |
Peter Zijlstra | 3fe1698 | 2011-04-05 17:23:48 +0200 | [diff] [blame] | 2663 | #ifndef CONFIG_64BIT |
| 2664 | u64 min_vruntime_copy; |
Peter Zijlstra | 74f8e4b | 2011-04-05 17:23:47 +0200 | [diff] [blame] | 2665 | |
Peter Zijlstra | 3fe1698 | 2011-04-05 17:23:48 +0200 | [diff] [blame] | 2666 | do { |
| 2667 | min_vruntime_copy = cfs_rq->min_vruntime_copy; |
| 2668 | smp_rmb(); |
| 2669 | min_vruntime = cfs_rq->min_vruntime; |
| 2670 | } while (min_vruntime != min_vruntime_copy); |
| 2671 | #else |
| 2672 | min_vruntime = cfs_rq->min_vruntime; |
| 2673 | #endif |
| 2674 | |
| 2675 | se->vruntime -= min_vruntime; |
Peter Zijlstra | 88ec22d | 2009-12-16 18:04:41 +0100 | [diff] [blame] | 2676 | } |
| 2677 | |
Peter Zijlstra | bb3469a | 2008-06-27 13:41:27 +0200 | [diff] [blame] | 2678 | #ifdef CONFIG_FAIR_GROUP_SCHED |
Peter Zijlstra | f5bfb7d | 2008-06-27 13:41:39 +0200 | [diff] [blame] | 2679 | /* |
| 2680 | * effective_load() calculates the load change as seen from the root_task_group |
| 2681 | * |
| 2682 | * Adding load to a group doesn't make a group heavier, but can cause movement |
| 2683 | * of group shares between cpus. Assuming the shares were perfectly aligned one |
| 2684 | * can calculate the shift in shares. |
Peter Zijlstra | cf5f0ac | 2011-10-13 16:52:28 +0200 | [diff] [blame] | 2685 | * |
| 2686 | * Calculate the effective load difference if @wl is added (subtracted) to @tg |
| 2687 | * on this @cpu and results in a total addition (subtraction) of @wg to the |
| 2688 | * total group weight. |
| 2689 | * |
| 2690 | * Given a runqueue weight distribution (rw_i) we can compute a shares |
| 2691 | * distribution (s_i) using: |
| 2692 | * |
| 2693 | * s_i = rw_i / \Sum rw_j (1) |
| 2694 | * |
| 2695 | * Suppose we have 4 CPUs and our @tg is a direct child of the root group and |
| 2696 | * has 7 equal weight tasks, distributed as below (rw_i), with the resulting |
| 2697 | * shares distribution (s_i): |
| 2698 | * |
| 2699 | * rw_i = { 2, 4, 1, 0 } |
| 2700 | * s_i = { 2/7, 4/7, 1/7, 0 } |
| 2701 | * |
| 2702 | * As per wake_affine() we're interested in the load of two CPUs (the CPU the |
| 2703 | * task used to run on and the CPU the waker is running on), we need to |
| 2704 | * compute the effect of waking a task on either CPU and, in case of a sync |
| 2705 | * wakeup, compute the effect of the current task going to sleep. |
| 2706 | * |
| 2707 | * So for a change of @wl to the local @cpu with an overall group weight change |
| 2708 | * of @wl we can compute the new shares distribution (s'_i) using: |
| 2709 | * |
| 2710 | * s'_i = (rw_i + @wl) / (@wg + \Sum rw_j) (2) |
| 2711 | * |
| 2712 | * Suppose we're interested in CPUs 0 and 1, and want to compute the load |
| 2713 | * differences in waking a task to CPU 0. The additional task changes the |
| 2714 | * weight and shares distributions like: |
| 2715 | * |
| 2716 | * rw'_i = { 3, 4, 1, 0 } |
| 2717 | * s'_i = { 3/8, 4/8, 1/8, 0 } |
| 2718 | * |
| 2719 | * We can then compute the difference in effective weight by using: |
| 2720 | * |
| 2721 | * dw_i = S * (s'_i - s_i) (3) |
| 2722 | * |
| 2723 | * Where 'S' is the group weight as seen by its parent. |
| 2724 | * |
| 2725 | * Therefore the effective change in loads on CPU 0 would be 5/56 (3/8 - 2/7) |
| 2726 | * times the weight of the group. The effect on CPU 1 would be -4/56 (4/8 - |
| 2727 | * 4/7) times the weight of the group. |
Peter Zijlstra | f5bfb7d | 2008-06-27 13:41:39 +0200 | [diff] [blame] | 2728 | */ |
Peter Zijlstra | 2069dd7 | 2010-11-15 15:47:00 -0800 | [diff] [blame] | 2729 | 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] | 2730 | { |
Peter Zijlstra | 4be9daa | 2008-06-27 13:41:30 +0200 | [diff] [blame] | 2731 | struct sched_entity *se = tg->se[cpu]; |
Peter Zijlstra | f1d239f | 2008-06-27 13:41:38 +0200 | [diff] [blame] | 2732 | |
Peter Zijlstra | cf5f0ac | 2011-10-13 16:52:28 +0200 | [diff] [blame] | 2733 | if (!tg->parent) /* the trivial, non-cgroup case */ |
Peter Zijlstra | f1d239f | 2008-06-27 13:41:38 +0200 | [diff] [blame] | 2734 | return wl; |
| 2735 | |
Peter Zijlstra | 4be9daa | 2008-06-27 13:41:30 +0200 | [diff] [blame] | 2736 | for_each_sched_entity(se) { |
Peter Zijlstra | cf5f0ac | 2011-10-13 16:52:28 +0200 | [diff] [blame] | 2737 | long w, W; |
Peter Zijlstra | bb3469a | 2008-06-27 13:41:27 +0200 | [diff] [blame] | 2738 | |
Paul Turner | 977dda7 | 2011-01-14 17:57:50 -0800 | [diff] [blame] | 2739 | tg = se->my_q->tg; |
Peter Zijlstra | 4be9daa | 2008-06-27 13:41:30 +0200 | [diff] [blame] | 2740 | |
Peter Zijlstra | cf5f0ac | 2011-10-13 16:52:28 +0200 | [diff] [blame] | 2741 | /* |
| 2742 | * W = @wg + \Sum rw_j |
| 2743 | */ |
| 2744 | W = wg + calc_tg_weight(tg, se->my_q); |
Peter Zijlstra | 4be9daa | 2008-06-27 13:41:30 +0200 | [diff] [blame] | 2745 | |
Peter Zijlstra | cf5f0ac | 2011-10-13 16:52:28 +0200 | [diff] [blame] | 2746 | /* |
| 2747 | * w = rw_i + @wl |
| 2748 | */ |
| 2749 | w = se->my_q->load.weight + wl; |
Peter Zijlstra | 940959e | 2008-09-23 15:33:42 +0200 | [diff] [blame] | 2750 | |
Peter Zijlstra | cf5f0ac | 2011-10-13 16:52:28 +0200 | [diff] [blame] | 2751 | /* |
| 2752 | * wl = S * s'_i; see (2) |
| 2753 | */ |
| 2754 | if (W > 0 && w < W) |
| 2755 | wl = (w * tg->shares) / W; |
Paul Turner | 977dda7 | 2011-01-14 17:57:50 -0800 | [diff] [blame] | 2756 | else |
| 2757 | wl = tg->shares; |
Peter Zijlstra | 940959e | 2008-09-23 15:33:42 +0200 | [diff] [blame] | 2758 | |
Peter Zijlstra | cf5f0ac | 2011-10-13 16:52:28 +0200 | [diff] [blame] | 2759 | /* |
| 2760 | * Per the above, wl is the new se->load.weight value; since |
| 2761 | * those are clipped to [MIN_SHARES, ...) do so now. See |
| 2762 | * calc_cfs_shares(). |
| 2763 | */ |
Paul Turner | 977dda7 | 2011-01-14 17:57:50 -0800 | [diff] [blame] | 2764 | if (wl < MIN_SHARES) |
| 2765 | wl = MIN_SHARES; |
Peter Zijlstra | cf5f0ac | 2011-10-13 16:52:28 +0200 | [diff] [blame] | 2766 | |
| 2767 | /* |
| 2768 | * wl = dw_i = S * (s'_i - s_i); see (3) |
| 2769 | */ |
Paul Turner | 977dda7 | 2011-01-14 17:57:50 -0800 | [diff] [blame] | 2770 | wl -= se->load.weight; |
Peter Zijlstra | cf5f0ac | 2011-10-13 16:52:28 +0200 | [diff] [blame] | 2771 | |
| 2772 | /* |
| 2773 | * Recursively apply this logic to all parent groups to compute |
| 2774 | * the final effective load change on the root group. Since |
| 2775 | * only the @tg group gets extra weight, all parent groups can |
| 2776 | * only redistribute existing shares. @wl is the shift in shares |
| 2777 | * resulting from this level per the above. |
| 2778 | */ |
Peter Zijlstra | 4be9daa | 2008-06-27 13:41:30 +0200 | [diff] [blame] | 2779 | wg = 0; |
Peter Zijlstra | 4be9daa | 2008-06-27 13:41:30 +0200 | [diff] [blame] | 2780 | } |
| 2781 | |
| 2782 | return wl; |
Peter Zijlstra | bb3469a | 2008-06-27 13:41:27 +0200 | [diff] [blame] | 2783 | } |
| 2784 | #else |
Peter Zijlstra | 4be9daa | 2008-06-27 13:41:30 +0200 | [diff] [blame] | 2785 | |
Peter Zijlstra | 8337826 | 2008-06-27 13:41:37 +0200 | [diff] [blame] | 2786 | static inline unsigned long effective_load(struct task_group *tg, int cpu, |
| 2787 | unsigned long wl, unsigned long wg) |
Peter Zijlstra | 4be9daa | 2008-06-27 13:41:30 +0200 | [diff] [blame] | 2788 | { |
Peter Zijlstra | 8337826 | 2008-06-27 13:41:37 +0200 | [diff] [blame] | 2789 | return wl; |
Peter Zijlstra | bb3469a | 2008-06-27 13:41:27 +0200 | [diff] [blame] | 2790 | } |
Peter Zijlstra | 4be9daa | 2008-06-27 13:41:30 +0200 | [diff] [blame] | 2791 | |
Peter Zijlstra | bb3469a | 2008-06-27 13:41:27 +0200 | [diff] [blame] | 2792 | #endif |
| 2793 | |
Peter Zijlstra | c88d591 | 2009-09-10 13:50:02 +0200 | [diff] [blame] | 2794 | 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] | 2795 | { |
Paul Turner | e37b6a7 | 2011-01-21 20:44:59 -0800 | [diff] [blame] | 2796 | s64 this_load, load; |
Peter Zijlstra | c88d591 | 2009-09-10 13:50:02 +0200 | [diff] [blame] | 2797 | int idx, this_cpu, prev_cpu; |
Ingo Molnar | 098fb9d | 2008-03-16 20:36:10 +0100 | [diff] [blame] | 2798 | unsigned long tl_per_task; |
Peter Zijlstra | c88d591 | 2009-09-10 13:50:02 +0200 | [diff] [blame] | 2799 | struct task_group *tg; |
Peter Zijlstra | 8337826 | 2008-06-27 13:41:37 +0200 | [diff] [blame] | 2800 | unsigned long weight; |
Mike Galbraith | b3137bc | 2008-05-29 11:11:41 +0200 | [diff] [blame] | 2801 | int balanced; |
Ingo Molnar | 098fb9d | 2008-03-16 20:36:10 +0100 | [diff] [blame] | 2802 | |
Peter Zijlstra | c88d591 | 2009-09-10 13:50:02 +0200 | [diff] [blame] | 2803 | idx = sd->wake_idx; |
| 2804 | this_cpu = smp_processor_id(); |
| 2805 | prev_cpu = task_cpu(p); |
| 2806 | load = source_load(prev_cpu, idx); |
| 2807 | this_load = target_load(this_cpu, idx); |
Ingo Molnar | 098fb9d | 2008-03-16 20:36:10 +0100 | [diff] [blame] | 2808 | |
| 2809 | /* |
Ingo Molnar | 098fb9d | 2008-03-16 20:36:10 +0100 | [diff] [blame] | 2810 | * If sync wakeup then subtract the (maximum possible) |
| 2811 | * effect of the currently running task from the load |
| 2812 | * of the current CPU: |
| 2813 | */ |
Peter Zijlstra | 8337826 | 2008-06-27 13:41:37 +0200 | [diff] [blame] | 2814 | if (sync) { |
| 2815 | tg = task_group(current); |
| 2816 | weight = current->se.load.weight; |
Ingo Molnar | 098fb9d | 2008-03-16 20:36:10 +0100 | [diff] [blame] | 2817 | |
Peter Zijlstra | c88d591 | 2009-09-10 13:50:02 +0200 | [diff] [blame] | 2818 | this_load += effective_load(tg, this_cpu, -weight, -weight); |
Peter Zijlstra | 8337826 | 2008-06-27 13:41:37 +0200 | [diff] [blame] | 2819 | load += effective_load(tg, prev_cpu, 0, -weight); |
| 2820 | } |
| 2821 | |
| 2822 | tg = task_group(p); |
| 2823 | weight = p->se.load.weight; |
| 2824 | |
Peter Zijlstra | 71a29aa | 2009-09-07 18:28:05 +0200 | [diff] [blame] | 2825 | /* |
| 2826 | * 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] | 2827 | * due to the sync cause above having dropped this_load to 0, we'll |
| 2828 | * always have an imbalance, but there's really nothing you can do |
| 2829 | * about that, so that's good too. |
Peter Zijlstra | 71a29aa | 2009-09-07 18:28:05 +0200 | [diff] [blame] | 2830 | * |
| 2831 | * Otherwise check if either cpus are near enough in load to allow this |
| 2832 | * task to be woken on this_cpu. |
| 2833 | */ |
Paul Turner | e37b6a7 | 2011-01-21 20:44:59 -0800 | [diff] [blame] | 2834 | if (this_load > 0) { |
| 2835 | s64 this_eff_load, prev_eff_load; |
Peter Zijlstra | e51fd5e | 2010-05-31 12:37:30 +0200 | [diff] [blame] | 2836 | |
| 2837 | this_eff_load = 100; |
| 2838 | this_eff_load *= power_of(prev_cpu); |
| 2839 | this_eff_load *= this_load + |
| 2840 | effective_load(tg, this_cpu, weight, weight); |
| 2841 | |
| 2842 | prev_eff_load = 100 + (sd->imbalance_pct - 100) / 2; |
| 2843 | prev_eff_load *= power_of(this_cpu); |
| 2844 | prev_eff_load *= load + effective_load(tg, prev_cpu, 0, weight); |
| 2845 | |
| 2846 | balanced = this_eff_load <= prev_eff_load; |
| 2847 | } else |
| 2848 | balanced = true; |
Mike Galbraith | b3137bc | 2008-05-29 11:11:41 +0200 | [diff] [blame] | 2849 | |
| 2850 | /* |
| 2851 | * If the currently running task will sleep within |
| 2852 | * a reasonable amount of time then attract this newly |
| 2853 | * woken task: |
| 2854 | */ |
Peter Zijlstra | 2fb7635 | 2008-10-08 09:16:04 +0200 | [diff] [blame] | 2855 | if (sync && balanced) |
| 2856 | return 1; |
Mike Galbraith | b3137bc | 2008-05-29 11:11:41 +0200 | [diff] [blame] | 2857 | |
Lucas De Marchi | 41acab8 | 2010-03-10 23:37:45 -0300 | [diff] [blame] | 2858 | schedstat_inc(p, se.statistics.nr_wakeups_affine_attempts); |
Mike Galbraith | b3137bc | 2008-05-29 11:11:41 +0200 | [diff] [blame] | 2859 | tl_per_task = cpu_avg_load_per_task(this_cpu); |
| 2860 | |
Peter Zijlstra | c88d591 | 2009-09-10 13:50:02 +0200 | [diff] [blame] | 2861 | if (balanced || |
| 2862 | (this_load <= load && |
| 2863 | this_load + target_load(prev_cpu, idx) <= tl_per_task)) { |
Ingo Molnar | 098fb9d | 2008-03-16 20:36:10 +0100 | [diff] [blame] | 2864 | /* |
| 2865 | * This domain has SD_WAKE_AFFINE and |
| 2866 | * p is cache cold in this domain, and |
| 2867 | * there is no bad imbalance. |
| 2868 | */ |
Peter Zijlstra | c88d591 | 2009-09-10 13:50:02 +0200 | [diff] [blame] | 2869 | schedstat_inc(sd, ttwu_move_affine); |
Lucas De Marchi | 41acab8 | 2010-03-10 23:37:45 -0300 | [diff] [blame] | 2870 | schedstat_inc(p, se.statistics.nr_wakeups_affine); |
Ingo Molnar | 098fb9d | 2008-03-16 20:36:10 +0100 | [diff] [blame] | 2871 | |
| 2872 | return 1; |
| 2873 | } |
| 2874 | return 0; |
| 2875 | } |
| 2876 | |
Peter Zijlstra | aaee120 | 2009-09-10 13:36:25 +0200 | [diff] [blame] | 2877 | /* |
| 2878 | * find_idlest_group finds and returns the least busy CPU group within the |
| 2879 | * domain. |
| 2880 | */ |
| 2881 | static struct sched_group * |
Peter Zijlstra | 78e7ed5 | 2009-09-03 13:16:51 +0200 | [diff] [blame] | 2882 | find_idlest_group(struct sched_domain *sd, struct task_struct *p, |
Peter Zijlstra | 5158f4e | 2009-09-16 13:46:59 +0200 | [diff] [blame] | 2883 | int this_cpu, int load_idx) |
Gregory Haskins | e7693a3 | 2008-01-25 21:08:09 +0100 | [diff] [blame] | 2884 | { |
Andi Kleen | b3bd3de | 2010-08-10 14:17:51 -0700 | [diff] [blame] | 2885 | struct sched_group *idlest = NULL, *group = sd->groups; |
Peter Zijlstra | aaee120 | 2009-09-10 13:36:25 +0200 | [diff] [blame] | 2886 | unsigned long min_load = ULONG_MAX, this_load = 0; |
Peter Zijlstra | aaee120 | 2009-09-10 13:36:25 +0200 | [diff] [blame] | 2887 | int imbalance = 100 + (sd->imbalance_pct-100)/2; |
Gregory Haskins | e7693a3 | 2008-01-25 21:08:09 +0100 | [diff] [blame] | 2888 | |
Peter Zijlstra | aaee120 | 2009-09-10 13:36:25 +0200 | [diff] [blame] | 2889 | do { |
| 2890 | unsigned long load, avg_load; |
| 2891 | int local_group; |
| 2892 | int i; |
Gregory Haskins | e7693a3 | 2008-01-25 21:08:09 +0100 | [diff] [blame] | 2893 | |
Peter Zijlstra | aaee120 | 2009-09-10 13:36:25 +0200 | [diff] [blame] | 2894 | /* Skip over this group if it has no CPUs allowed */ |
| 2895 | if (!cpumask_intersects(sched_group_cpus(group), |
Peter Zijlstra | fa17b50 | 2011-06-16 12:23:22 +0200 | [diff] [blame] | 2896 | tsk_cpus_allowed(p))) |
Peter Zijlstra | aaee120 | 2009-09-10 13:36:25 +0200 | [diff] [blame] | 2897 | continue; |
| 2898 | |
| 2899 | local_group = cpumask_test_cpu(this_cpu, |
| 2900 | sched_group_cpus(group)); |
| 2901 | |
| 2902 | /* Tally up the load of all CPUs in the group */ |
| 2903 | avg_load = 0; |
| 2904 | |
| 2905 | for_each_cpu(i, sched_group_cpus(group)) { |
| 2906 | /* Bias balancing toward cpus of our domain */ |
| 2907 | if (local_group) |
| 2908 | load = source_load(i, load_idx); |
| 2909 | else |
| 2910 | load = target_load(i, load_idx); |
| 2911 | |
| 2912 | avg_load += load; |
| 2913 | } |
| 2914 | |
| 2915 | /* Adjust by relative CPU power of the group */ |
Peter Zijlstra | 9c3f75c | 2011-07-14 13:00:06 +0200 | [diff] [blame] | 2916 | avg_load = (avg_load * SCHED_POWER_SCALE) / group->sgp->power; |
Peter Zijlstra | aaee120 | 2009-09-10 13:36:25 +0200 | [diff] [blame] | 2917 | |
| 2918 | if (local_group) { |
| 2919 | this_load = avg_load; |
Peter Zijlstra | aaee120 | 2009-09-10 13:36:25 +0200 | [diff] [blame] | 2920 | } else if (avg_load < min_load) { |
| 2921 | min_load = avg_load; |
| 2922 | idlest = group; |
| 2923 | } |
| 2924 | } while (group = group->next, group != sd->groups); |
| 2925 | |
| 2926 | if (!idlest || 100*this_load < imbalance*min_load) |
| 2927 | return NULL; |
| 2928 | return idlest; |
| 2929 | } |
| 2930 | |
| 2931 | /* |
| 2932 | * find_idlest_cpu - find the idlest cpu among the cpus in group. |
| 2933 | */ |
| 2934 | static int |
| 2935 | find_idlest_cpu(struct sched_group *group, struct task_struct *p, int this_cpu) |
| 2936 | { |
| 2937 | unsigned long load, min_load = ULONG_MAX; |
| 2938 | int idlest = -1; |
| 2939 | int i; |
| 2940 | |
| 2941 | /* Traverse only the allowed CPUs */ |
Peter Zijlstra | fa17b50 | 2011-06-16 12:23:22 +0200 | [diff] [blame] | 2942 | for_each_cpu_and(i, sched_group_cpus(group), tsk_cpus_allowed(p)) { |
Peter Zijlstra | aaee120 | 2009-09-10 13:36:25 +0200 | [diff] [blame] | 2943 | load = weighted_cpuload(i); |
| 2944 | |
| 2945 | if (load < min_load || (load == min_load && i == this_cpu)) { |
| 2946 | min_load = load; |
| 2947 | idlest = i; |
Gregory Haskins | e7693a3 | 2008-01-25 21:08:09 +0100 | [diff] [blame] | 2948 | } |
| 2949 | } |
| 2950 | |
Peter Zijlstra | aaee120 | 2009-09-10 13:36:25 +0200 | [diff] [blame] | 2951 | return idlest; |
| 2952 | } |
Gregory Haskins | e7693a3 | 2008-01-25 21:08:09 +0100 | [diff] [blame] | 2953 | |
Peter Zijlstra | aaee120 | 2009-09-10 13:36:25 +0200 | [diff] [blame] | 2954 | /* |
Peter Zijlstra | a50bde5 | 2009-11-12 15:55:28 +0100 | [diff] [blame] | 2955 | * Try and locate an idle CPU in the sched_domain. |
| 2956 | */ |
Suresh Siddha | 99bd5e2 | 2010-03-31 16:47:45 -0700 | [diff] [blame] | 2957 | static int select_idle_sibling(struct task_struct *p, int target) |
Peter Zijlstra | a50bde5 | 2009-11-12 15:55:28 +0100 | [diff] [blame] | 2958 | { |
| 2959 | int cpu = smp_processor_id(); |
| 2960 | int prev_cpu = task_cpu(p); |
Suresh Siddha | 99bd5e2 | 2010-03-31 16:47:45 -0700 | [diff] [blame] | 2961 | struct sched_domain *sd; |
Linus Torvalds | 37407ea | 2012-09-16 12:29:43 -0700 | [diff] [blame] | 2962 | struct sched_group *sg; |
| 2963 | int i; |
Peter Zijlstra | a50bde5 | 2009-11-12 15:55:28 +0100 | [diff] [blame] | 2964 | |
| 2965 | /* |
Suresh Siddha | 99bd5e2 | 2010-03-31 16:47:45 -0700 | [diff] [blame] | 2966 | * If the task is going to be woken-up on this cpu and if it is |
| 2967 | * already idle, then it is the right target. |
Peter Zijlstra | a50bde5 | 2009-11-12 15:55:28 +0100 | [diff] [blame] | 2968 | */ |
Suresh Siddha | 99bd5e2 | 2010-03-31 16:47:45 -0700 | [diff] [blame] | 2969 | if (target == cpu && idle_cpu(cpu)) |
| 2970 | return cpu; |
| 2971 | |
| 2972 | /* |
| 2973 | * If the task is going to be woken-up on the cpu where it previously |
| 2974 | * ran and if it is currently idle, then it the right target. |
| 2975 | */ |
| 2976 | if (target == prev_cpu && idle_cpu(prev_cpu)) |
Peter Zijlstra | fe3bcfe | 2009-11-12 15:55:29 +0100 | [diff] [blame] | 2977 | return prev_cpu; |
Peter Zijlstra | a50bde5 | 2009-11-12 15:55:28 +0100 | [diff] [blame] | 2978 | |
| 2979 | /* |
Linus Torvalds | 37407ea | 2012-09-16 12:29:43 -0700 | [diff] [blame] | 2980 | * Otherwise, iterate the domains and find an elegible idle cpu. |
Peter Zijlstra | a50bde5 | 2009-11-12 15:55:28 +0100 | [diff] [blame] | 2981 | */ |
Peter Zijlstra | 518cd62 | 2011-12-07 15:07:31 +0100 | [diff] [blame] | 2982 | sd = rcu_dereference(per_cpu(sd_llc, target)); |
Suresh Siddha | 77e8136 | 2011-11-17 11:08:23 -0800 | [diff] [blame] | 2983 | for_each_lower_domain(sd) { |
Linus Torvalds | 37407ea | 2012-09-16 12:29:43 -0700 | [diff] [blame] | 2984 | sg = sd->groups; |
| 2985 | do { |
| 2986 | if (!cpumask_intersects(sched_group_cpus(sg), |
| 2987 | tsk_cpus_allowed(p))) |
| 2988 | goto next; |
Mike Galbraith | 970e178 | 2012-06-12 05:18:32 +0200 | [diff] [blame] | 2989 | |
Linus Torvalds | 37407ea | 2012-09-16 12:29:43 -0700 | [diff] [blame] | 2990 | for_each_cpu(i, sched_group_cpus(sg)) { |
| 2991 | if (!idle_cpu(i)) |
| 2992 | goto next; |
| 2993 | } |
| 2994 | |
| 2995 | target = cpumask_first_and(sched_group_cpus(sg), |
| 2996 | tsk_cpus_allowed(p)); |
| 2997 | goto done; |
| 2998 | next: |
| 2999 | sg = sg->next; |
| 3000 | } while (sg != sd->groups); |
| 3001 | } |
| 3002 | done: |
Peter Zijlstra | a50bde5 | 2009-11-12 15:55:28 +0100 | [diff] [blame] | 3003 | return target; |
| 3004 | } |
| 3005 | |
| 3006 | /* |
Peter Zijlstra | aaee120 | 2009-09-10 13:36:25 +0200 | [diff] [blame] | 3007 | * sched_balance_self: balance the current task (running on cpu) in domains |
| 3008 | * that have the 'flag' flag set. In practice, this is SD_BALANCE_FORK and |
| 3009 | * SD_BALANCE_EXEC. |
| 3010 | * |
| 3011 | * Balance, ie. select the least loaded group. |
| 3012 | * |
| 3013 | * Returns the target CPU number, or the same CPU if no balancing is needed. |
| 3014 | * |
| 3015 | * preempt must be disabled. |
| 3016 | */ |
Peter Zijlstra | 0017d73 | 2010-03-24 18:34:10 +0100 | [diff] [blame] | 3017 | static int |
Peter Zijlstra | 7608dec | 2011-04-05 17:23:46 +0200 | [diff] [blame] | 3018 | select_task_rq_fair(struct task_struct *p, int sd_flag, int wake_flags) |
Peter Zijlstra | aaee120 | 2009-09-10 13:36:25 +0200 | [diff] [blame] | 3019 | { |
Peter Zijlstra | 29cd8ba | 2009-09-17 09:01:14 +0200 | [diff] [blame] | 3020 | struct sched_domain *tmp, *affine_sd = NULL, *sd = NULL; |
Peter Zijlstra | c88d591 | 2009-09-10 13:50:02 +0200 | [diff] [blame] | 3021 | int cpu = smp_processor_id(); |
| 3022 | int prev_cpu = task_cpu(p); |
| 3023 | int new_cpu = cpu; |
Suresh Siddha | 99bd5e2 | 2010-03-31 16:47:45 -0700 | [diff] [blame] | 3024 | int want_affine = 0; |
Peter Zijlstra | 5158f4e | 2009-09-16 13:46:59 +0200 | [diff] [blame] | 3025 | int sync = wake_flags & WF_SYNC; |
Gregory Haskins | e7693a3 | 2008-01-25 21:08:09 +0100 | [diff] [blame] | 3026 | |
Peter Zijlstra | 29baa74 | 2012-04-23 12:11:21 +0200 | [diff] [blame] | 3027 | if (p->nr_cpus_allowed == 1) |
Mike Galbraith | 76854c7 | 2011-11-22 15:18:24 +0100 | [diff] [blame] | 3028 | return prev_cpu; |
| 3029 | |
Peter Zijlstra | 0763a66 | 2009-09-14 19:37:39 +0200 | [diff] [blame] | 3030 | if (sd_flag & SD_BALANCE_WAKE) { |
Peter Zijlstra | fa17b50 | 2011-06-16 12:23:22 +0200 | [diff] [blame] | 3031 | if (cpumask_test_cpu(cpu, tsk_cpus_allowed(p))) |
Peter Zijlstra | c88d591 | 2009-09-10 13:50:02 +0200 | [diff] [blame] | 3032 | want_affine = 1; |
| 3033 | new_cpu = prev_cpu; |
| 3034 | } |
Gregory Haskins | e7693a3 | 2008-01-25 21:08:09 +0100 | [diff] [blame] | 3035 | |
Peter Zijlstra | dce840a | 2011-04-07 14:09:50 +0200 | [diff] [blame] | 3036 | rcu_read_lock(); |
Peter Zijlstra | aaee120 | 2009-09-10 13:36:25 +0200 | [diff] [blame] | 3037 | for_each_domain(cpu, tmp) { |
Peter Zijlstra | e4f4288 | 2009-12-16 18:04:34 +0100 | [diff] [blame] | 3038 | if (!(tmp->flags & SD_LOAD_BALANCE)) |
| 3039 | continue; |
| 3040 | |
Peter Zijlstra | aaee120 | 2009-09-10 13:36:25 +0200 | [diff] [blame] | 3041 | /* |
Suresh Siddha | 99bd5e2 | 2010-03-31 16:47:45 -0700 | [diff] [blame] | 3042 | * If both cpu and prev_cpu are part of this domain, |
| 3043 | * cpu is a valid SD_WAKE_AFFINE target. |
Peter Zijlstra | fe3bcfe | 2009-11-12 15:55:29 +0100 | [diff] [blame] | 3044 | */ |
Suresh Siddha | 99bd5e2 | 2010-03-31 16:47:45 -0700 | [diff] [blame] | 3045 | if (want_affine && (tmp->flags & SD_WAKE_AFFINE) && |
| 3046 | cpumask_test_cpu(prev_cpu, sched_domain_span(tmp))) { |
| 3047 | affine_sd = tmp; |
Alex Shi | f03542a | 2012-07-26 08:55:34 +0800 | [diff] [blame] | 3048 | break; |
Peter Zijlstra | c88d591 | 2009-09-10 13:50:02 +0200 | [diff] [blame] | 3049 | } |
| 3050 | |
Alex Shi | f03542a | 2012-07-26 08:55:34 +0800 | [diff] [blame] | 3051 | if (tmp->flags & sd_flag) |
Peter Zijlstra | 29cd8ba | 2009-09-17 09:01:14 +0200 | [diff] [blame] | 3052 | sd = tmp; |
Peter Zijlstra | c88d591 | 2009-09-10 13:50:02 +0200 | [diff] [blame] | 3053 | } |
Peter Zijlstra | aaee120 | 2009-09-10 13:36:25 +0200 | [diff] [blame] | 3054 | |
Mike Galbraith | 8b911ac | 2010-03-11 17:17:16 +0100 | [diff] [blame] | 3055 | if (affine_sd) { |
Alex Shi | f03542a | 2012-07-26 08:55:34 +0800 | [diff] [blame] | 3056 | if (cpu != prev_cpu && wake_affine(affine_sd, p, sync)) |
Peter Zijlstra | dce840a | 2011-04-07 14:09:50 +0200 | [diff] [blame] | 3057 | prev_cpu = cpu; |
| 3058 | |
| 3059 | new_cpu = select_idle_sibling(p, prev_cpu); |
| 3060 | goto unlock; |
Mike Galbraith | 8b911ac | 2010-03-11 17:17:16 +0100 | [diff] [blame] | 3061 | } |
Peter Zijlstra | 3b64089 | 2009-09-16 13:44:33 +0200 | [diff] [blame] | 3062 | |
Peter Zijlstra | aaee120 | 2009-09-10 13:36:25 +0200 | [diff] [blame] | 3063 | while (sd) { |
Peter Zijlstra | 5158f4e | 2009-09-16 13:46:59 +0200 | [diff] [blame] | 3064 | int load_idx = sd->forkexec_idx; |
Peter Zijlstra | aaee120 | 2009-09-10 13:36:25 +0200 | [diff] [blame] | 3065 | struct sched_group *group; |
Peter Zijlstra | c88d591 | 2009-09-10 13:50:02 +0200 | [diff] [blame] | 3066 | int weight; |
Peter Zijlstra | aaee120 | 2009-09-10 13:36:25 +0200 | [diff] [blame] | 3067 | |
Peter Zijlstra | 0763a66 | 2009-09-14 19:37:39 +0200 | [diff] [blame] | 3068 | if (!(sd->flags & sd_flag)) { |
Peter Zijlstra | aaee120 | 2009-09-10 13:36:25 +0200 | [diff] [blame] | 3069 | sd = sd->child; |
| 3070 | continue; |
| 3071 | } |
| 3072 | |
Peter Zijlstra | 5158f4e | 2009-09-16 13:46:59 +0200 | [diff] [blame] | 3073 | if (sd_flag & SD_BALANCE_WAKE) |
| 3074 | load_idx = sd->wake_idx; |
| 3075 | |
| 3076 | group = find_idlest_group(sd, p, cpu, load_idx); |
Peter Zijlstra | aaee120 | 2009-09-10 13:36:25 +0200 | [diff] [blame] | 3077 | if (!group) { |
| 3078 | sd = sd->child; |
| 3079 | continue; |
| 3080 | } |
| 3081 | |
Peter Zijlstra | d7c33c4 | 2009-09-11 12:45:38 +0200 | [diff] [blame] | 3082 | new_cpu = find_idlest_cpu(group, p, cpu); |
Peter Zijlstra | aaee120 | 2009-09-10 13:36:25 +0200 | [diff] [blame] | 3083 | if (new_cpu == -1 || new_cpu == cpu) { |
| 3084 | /* Now try balancing at a lower domain level of cpu */ |
| 3085 | sd = sd->child; |
| 3086 | continue; |
| 3087 | } |
| 3088 | |
| 3089 | /* Now try balancing at a lower domain level of new_cpu */ |
| 3090 | cpu = new_cpu; |
Peter Zijlstra | 669c55e | 2010-04-16 14:59:29 +0200 | [diff] [blame] | 3091 | weight = sd->span_weight; |
Peter Zijlstra | aaee120 | 2009-09-10 13:36:25 +0200 | [diff] [blame] | 3092 | sd = NULL; |
| 3093 | for_each_domain(cpu, tmp) { |
Peter Zijlstra | 669c55e | 2010-04-16 14:59:29 +0200 | [diff] [blame] | 3094 | if (weight <= tmp->span_weight) |
Peter Zijlstra | aaee120 | 2009-09-10 13:36:25 +0200 | [diff] [blame] | 3095 | break; |
Peter Zijlstra | 0763a66 | 2009-09-14 19:37:39 +0200 | [diff] [blame] | 3096 | if (tmp->flags & sd_flag) |
Peter Zijlstra | aaee120 | 2009-09-10 13:36:25 +0200 | [diff] [blame] | 3097 | sd = tmp; |
| 3098 | } |
| 3099 | /* while loop will break here if sd == NULL */ |
Gregory Haskins | e7693a3 | 2008-01-25 21:08:09 +0100 | [diff] [blame] | 3100 | } |
Peter Zijlstra | dce840a | 2011-04-07 14:09:50 +0200 | [diff] [blame] | 3101 | unlock: |
| 3102 | rcu_read_unlock(); |
Gregory Haskins | e7693a3 | 2008-01-25 21:08:09 +0100 | [diff] [blame] | 3103 | |
Peter Zijlstra | c88d591 | 2009-09-10 13:50:02 +0200 | [diff] [blame] | 3104 | return new_cpu; |
Gregory Haskins | e7693a3 | 2008-01-25 21:08:09 +0100 | [diff] [blame] | 3105 | } |
Paul Turner | 0a74bef | 2012-10-04 13:18:30 +0200 | [diff] [blame] | 3106 | |
| 3107 | /* |
| 3108 | * Called immediately before a task is migrated to a new cpu; task_cpu(p) and |
| 3109 | * cfs_rq_of(p) references at time of call are still valid and identify the |
| 3110 | * previous cpu. However, the caller only guarantees p->pi_lock is held; no |
| 3111 | * other assumptions, including the state of rq->lock, should be made. |
| 3112 | */ |
| 3113 | static void |
| 3114 | migrate_task_rq_fair(struct task_struct *p, int next_cpu) |
| 3115 | { |
Paul Turner | aff3e49 | 2012-10-04 13:18:30 +0200 | [diff] [blame] | 3116 | struct sched_entity *se = &p->se; |
| 3117 | struct cfs_rq *cfs_rq = cfs_rq_of(se); |
| 3118 | |
| 3119 | /* |
| 3120 | * Load tracking: accumulate removed load so that it can be processed |
| 3121 | * when we next update owning cfs_rq under rq->lock. Tasks contribute |
| 3122 | * to blocked load iff they have a positive decay-count. It can never |
| 3123 | * be negative here since on-rq tasks have decay-count == 0. |
| 3124 | */ |
| 3125 | if (se->avg.decay_count) { |
| 3126 | se->avg.decay_count = -__synchronize_entity_decay(se); |
| 3127 | atomic64_add(se->avg.load_avg_contrib, &cfs_rq->removed_load); |
| 3128 | } |
Paul Turner | 0a74bef | 2012-10-04 13:18:30 +0200 | [diff] [blame] | 3129 | } |
Gregory Haskins | e7693a3 | 2008-01-25 21:08:09 +0100 | [diff] [blame] | 3130 | #endif /* CONFIG_SMP */ |
| 3131 | |
Peter Zijlstra | e52fb7c | 2009-01-14 12:39:19 +0100 | [diff] [blame] | 3132 | static unsigned long |
| 3133 | wakeup_gran(struct sched_entity *curr, struct sched_entity *se) |
Peter Zijlstra | 0bbd333 | 2008-04-19 19:44:57 +0200 | [diff] [blame] | 3134 | { |
| 3135 | unsigned long gran = sysctl_sched_wakeup_granularity; |
| 3136 | |
| 3137 | /* |
Peter Zijlstra | e52fb7c | 2009-01-14 12:39:19 +0100 | [diff] [blame] | 3138 | * Since its curr running now, convert the gran from real-time |
| 3139 | * to virtual-time in his units. |
Mike Galbraith | 13814d4 | 2010-03-11 17:17:04 +0100 | [diff] [blame] | 3140 | * |
| 3141 | * By using 'se' instead of 'curr' we penalize light tasks, so |
| 3142 | * they get preempted easier. That is, if 'se' < 'curr' then |
| 3143 | * the resulting gran will be larger, therefore penalizing the |
| 3144 | * lighter, if otoh 'se' > 'curr' then the resulting gran will |
| 3145 | * be smaller, again penalizing the lighter task. |
| 3146 | * |
| 3147 | * This is especially important for buddies when the leftmost |
| 3148 | * task is higher priority than the buddy. |
Peter Zijlstra | 0bbd333 | 2008-04-19 19:44:57 +0200 | [diff] [blame] | 3149 | */ |
Shaohua Li | f4ad9bd | 2011-04-08 12:53:09 +0800 | [diff] [blame] | 3150 | return calc_delta_fair(gran, se); |
Peter Zijlstra | 0bbd333 | 2008-04-19 19:44:57 +0200 | [diff] [blame] | 3151 | } |
| 3152 | |
| 3153 | /* |
Peter Zijlstra | 464b752 | 2008-10-24 11:06:15 +0200 | [diff] [blame] | 3154 | * Should 'se' preempt 'curr'. |
| 3155 | * |
| 3156 | * |s1 |
| 3157 | * |s2 |
| 3158 | * |s3 |
| 3159 | * g |
| 3160 | * |<--->|c |
| 3161 | * |
| 3162 | * w(c, s1) = -1 |
| 3163 | * w(c, s2) = 0 |
| 3164 | * w(c, s3) = 1 |
| 3165 | * |
| 3166 | */ |
| 3167 | static int |
| 3168 | wakeup_preempt_entity(struct sched_entity *curr, struct sched_entity *se) |
| 3169 | { |
| 3170 | s64 gran, vdiff = curr->vruntime - se->vruntime; |
| 3171 | |
| 3172 | if (vdiff <= 0) |
| 3173 | return -1; |
| 3174 | |
Peter Zijlstra | e52fb7c | 2009-01-14 12:39:19 +0100 | [diff] [blame] | 3175 | gran = wakeup_gran(curr, se); |
Peter Zijlstra | 464b752 | 2008-10-24 11:06:15 +0200 | [diff] [blame] | 3176 | if (vdiff > gran) |
| 3177 | return 1; |
| 3178 | |
| 3179 | return 0; |
| 3180 | } |
| 3181 | |
Peter Zijlstra | 0247909 | 2008-11-04 21:25:10 +0100 | [diff] [blame] | 3182 | static void set_last_buddy(struct sched_entity *se) |
| 3183 | { |
Venkatesh Pallipadi | 69c80f3 | 2011-04-13 18:21:09 -0700 | [diff] [blame] | 3184 | if (entity_is_task(se) && unlikely(task_of(se)->policy == SCHED_IDLE)) |
| 3185 | return; |
| 3186 | |
| 3187 | for_each_sched_entity(se) |
| 3188 | cfs_rq_of(se)->last = se; |
Peter Zijlstra | 0247909 | 2008-11-04 21:25:10 +0100 | [diff] [blame] | 3189 | } |
| 3190 | |
| 3191 | static void set_next_buddy(struct sched_entity *se) |
| 3192 | { |
Venkatesh Pallipadi | 69c80f3 | 2011-04-13 18:21:09 -0700 | [diff] [blame] | 3193 | if (entity_is_task(se) && unlikely(task_of(se)->policy == SCHED_IDLE)) |
| 3194 | return; |
| 3195 | |
| 3196 | for_each_sched_entity(se) |
| 3197 | cfs_rq_of(se)->next = se; |
Peter Zijlstra | 0247909 | 2008-11-04 21:25:10 +0100 | [diff] [blame] | 3198 | } |
| 3199 | |
Rik van Riel | ac53db5 | 2011-02-01 09:51:03 -0500 | [diff] [blame] | 3200 | static void set_skip_buddy(struct sched_entity *se) |
| 3201 | { |
Venkatesh Pallipadi | 69c80f3 | 2011-04-13 18:21:09 -0700 | [diff] [blame] | 3202 | for_each_sched_entity(se) |
| 3203 | cfs_rq_of(se)->skip = se; |
Rik van Riel | ac53db5 | 2011-02-01 09:51:03 -0500 | [diff] [blame] | 3204 | } |
| 3205 | |
Peter Zijlstra | 464b752 | 2008-10-24 11:06:15 +0200 | [diff] [blame] | 3206 | /* |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 3207 | * Preempt the current task with a newly woken task if needed: |
| 3208 | */ |
Peter Zijlstra | 5a9b86f | 2009-09-16 13:47:58 +0200 | [diff] [blame] | 3209 | 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] | 3210 | { |
| 3211 | struct task_struct *curr = rq->curr; |
Srivatsa Vaddagiri | 8651a86 | 2007-10-15 17:00:12 +0200 | [diff] [blame] | 3212 | struct sched_entity *se = &curr->se, *pse = &p->se; |
Mike Galbraith | 03e89e4 | 2008-12-16 08:45:30 +0100 | [diff] [blame] | 3213 | struct cfs_rq *cfs_rq = task_cfs_rq(curr); |
Mike Galbraith | f685cea | 2009-10-23 23:09:22 +0200 | [diff] [blame] | 3214 | int scale = cfs_rq->nr_running >= sched_nr_latency; |
Venkatesh Pallipadi | 2f36825 | 2011-04-14 10:30:53 -0700 | [diff] [blame] | 3215 | int next_buddy_marked = 0; |
Mike Galbraith | 03e89e4 | 2008-12-16 08:45:30 +0100 | [diff] [blame] | 3216 | |
Ingo Molnar | 4ae7d5c | 2008-03-19 01:42:00 +0100 | [diff] [blame] | 3217 | if (unlikely(se == pse)) |
| 3218 | return; |
| 3219 | |
Paul Turner | 5238cdd | 2011-07-21 09:43:37 -0700 | [diff] [blame] | 3220 | /* |
Peter Zijlstra | ddcdf6e | 2012-02-22 19:27:40 +0100 | [diff] [blame] | 3221 | * This is possible from callers such as move_task(), in which we |
Paul Turner | 5238cdd | 2011-07-21 09:43:37 -0700 | [diff] [blame] | 3222 | * unconditionally check_prempt_curr() after an enqueue (which may have |
| 3223 | * lead to a throttle). This both saves work and prevents false |
| 3224 | * next-buddy nomination below. |
| 3225 | */ |
| 3226 | if (unlikely(throttled_hierarchy(cfs_rq_of(pse)))) |
| 3227 | return; |
| 3228 | |
Venkatesh Pallipadi | 2f36825 | 2011-04-14 10:30:53 -0700 | [diff] [blame] | 3229 | if (sched_feat(NEXT_BUDDY) && scale && !(wake_flags & WF_FORK)) { |
Mike Galbraith | 3cb63d5 | 2009-09-11 12:01:17 +0200 | [diff] [blame] | 3230 | set_next_buddy(pse); |
Venkatesh Pallipadi | 2f36825 | 2011-04-14 10:30:53 -0700 | [diff] [blame] | 3231 | next_buddy_marked = 1; |
| 3232 | } |
Peter Zijlstra | 57fdc26 | 2008-09-23 15:33:45 +0200 | [diff] [blame] | 3233 | |
Bharata B Rao | aec0a51 | 2008-08-28 14:42:49 +0530 | [diff] [blame] | 3234 | /* |
| 3235 | * We can come here with TIF_NEED_RESCHED already set from new task |
| 3236 | * wake up path. |
Paul Turner | 5238cdd | 2011-07-21 09:43:37 -0700 | [diff] [blame] | 3237 | * |
| 3238 | * Note: this also catches the edge-case of curr being in a throttled |
| 3239 | * group (e.g. via set_curr_task), since update_curr() (in the |
| 3240 | * enqueue of curr) will have resulted in resched being set. This |
| 3241 | * prevents us from potentially nominating it as a false LAST_BUDDY |
| 3242 | * below. |
Bharata B Rao | aec0a51 | 2008-08-28 14:42:49 +0530 | [diff] [blame] | 3243 | */ |
| 3244 | if (test_tsk_need_resched(curr)) |
| 3245 | return; |
| 3246 | |
Darren Hart | a2f5c9a | 2011-02-22 13:04:33 -0800 | [diff] [blame] | 3247 | /* Idle tasks are by definition preempted by non-idle tasks. */ |
| 3248 | if (unlikely(curr->policy == SCHED_IDLE) && |
| 3249 | likely(p->policy != SCHED_IDLE)) |
| 3250 | goto preempt; |
| 3251 | |
Ingo Molnar | 91c234b | 2007-10-15 17:00:18 +0200 | [diff] [blame] | 3252 | /* |
Darren Hart | a2f5c9a | 2011-02-22 13:04:33 -0800 | [diff] [blame] | 3253 | * Batch and idle tasks do not preempt non-idle tasks (their preemption |
| 3254 | * is driven by the tick): |
Ingo Molnar | 91c234b | 2007-10-15 17:00:18 +0200 | [diff] [blame] | 3255 | */ |
Peter Zijlstra | 6bc912b | 2009-01-15 14:53:38 +0100 | [diff] [blame] | 3256 | if (unlikely(p->policy != SCHED_NORMAL)) |
Ingo Molnar | 91c234b | 2007-10-15 17:00:18 +0200 | [diff] [blame] | 3257 | return; |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 3258 | |
Peter Zijlstra | 3a7e73a | 2009-11-28 18:51:02 +0100 | [diff] [blame] | 3259 | find_matching_se(&se, &pse); |
Paul Turner | 9bbd737 | 2011-07-05 19:07:21 -0700 | [diff] [blame] | 3260 | update_curr(cfs_rq_of(se)); |
Peter Zijlstra | 3a7e73a | 2009-11-28 18:51:02 +0100 | [diff] [blame] | 3261 | BUG_ON(!pse); |
Venkatesh Pallipadi | 2f36825 | 2011-04-14 10:30:53 -0700 | [diff] [blame] | 3262 | if (wakeup_preempt_entity(se, pse) == 1) { |
| 3263 | /* |
| 3264 | * Bias pick_next to pick the sched entity that is |
| 3265 | * triggering this preemption. |
| 3266 | */ |
| 3267 | if (!next_buddy_marked) |
| 3268 | set_next_buddy(pse); |
Peter Zijlstra | 3a7e73a | 2009-11-28 18:51:02 +0100 | [diff] [blame] | 3269 | goto preempt; |
Venkatesh Pallipadi | 2f36825 | 2011-04-14 10:30:53 -0700 | [diff] [blame] | 3270 | } |
Jupyung Lee | a65ac74 | 2009-11-17 18:51:40 +0900 | [diff] [blame] | 3271 | |
Peter Zijlstra | 3a7e73a | 2009-11-28 18:51:02 +0100 | [diff] [blame] | 3272 | return; |
| 3273 | |
| 3274 | preempt: |
| 3275 | resched_task(curr); |
| 3276 | /* |
| 3277 | * Only set the backward buddy when the current task is still |
| 3278 | * on the rq. This can happen when a wakeup gets interleaved |
| 3279 | * with schedule on the ->pre_schedule() or idle_balance() |
| 3280 | * point, either of which can * drop the rq lock. |
| 3281 | * |
| 3282 | * Also, during early boot the idle thread is in the fair class, |
| 3283 | * for obvious reasons its a bad idea to schedule back to it. |
| 3284 | */ |
| 3285 | if (unlikely(!se->on_rq || curr == rq->idle)) |
| 3286 | return; |
| 3287 | |
| 3288 | if (sched_feat(LAST_BUDDY) && scale && entity_is_task(se)) |
| 3289 | set_last_buddy(se); |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 3290 | } |
| 3291 | |
Ingo Molnar | fb8d472 | 2007-08-09 11:16:48 +0200 | [diff] [blame] | 3292 | static struct task_struct *pick_next_task_fair(struct rq *rq) |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 3293 | { |
Peter Zijlstra | 8f4d37e | 2008-01-25 21:08:29 +0100 | [diff] [blame] | 3294 | struct task_struct *p; |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 3295 | struct cfs_rq *cfs_rq = &rq->cfs; |
| 3296 | struct sched_entity *se; |
| 3297 | |
Tim Blechmann | 36ace27 | 2009-11-24 11:55:45 +0100 | [diff] [blame] | 3298 | if (!cfs_rq->nr_running) |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 3299 | return NULL; |
| 3300 | |
| 3301 | do { |
Ingo Molnar | 9948f4b | 2007-08-09 11:16:48 +0200 | [diff] [blame] | 3302 | se = pick_next_entity(cfs_rq); |
Peter Zijlstra | f4b6755 | 2008-11-04 21:25:07 +0100 | [diff] [blame] | 3303 | set_next_entity(cfs_rq, se); |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 3304 | cfs_rq = group_cfs_rq(se); |
| 3305 | } while (cfs_rq); |
| 3306 | |
Peter Zijlstra | 8f4d37e | 2008-01-25 21:08:29 +0100 | [diff] [blame] | 3307 | p = task_of(se); |
Mike Galbraith | b39e66e | 2011-11-22 15:20:07 +0100 | [diff] [blame] | 3308 | if (hrtick_enabled(rq)) |
| 3309 | hrtick_start_fair(rq, p); |
Peter Zijlstra | 8f4d37e | 2008-01-25 21:08:29 +0100 | [diff] [blame] | 3310 | |
| 3311 | return p; |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 3312 | } |
| 3313 | |
| 3314 | /* |
| 3315 | * Account for a descheduled task: |
| 3316 | */ |
Ingo Molnar | 31ee529 | 2007-08-09 11:16:49 +0200 | [diff] [blame] | 3317 | 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] | 3318 | { |
| 3319 | struct sched_entity *se = &prev->se; |
| 3320 | struct cfs_rq *cfs_rq; |
| 3321 | |
| 3322 | for_each_sched_entity(se) { |
| 3323 | cfs_rq = cfs_rq_of(se); |
Ingo Molnar | ab6cde2 | 2007-08-09 11:16:48 +0200 | [diff] [blame] | 3324 | put_prev_entity(cfs_rq, se); |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 3325 | } |
| 3326 | } |
| 3327 | |
Rik van Riel | ac53db5 | 2011-02-01 09:51:03 -0500 | [diff] [blame] | 3328 | /* |
| 3329 | * sched_yield() is very simple |
| 3330 | * |
| 3331 | * The magic of dealing with the ->skip buddy is in pick_next_entity. |
| 3332 | */ |
| 3333 | static void yield_task_fair(struct rq *rq) |
| 3334 | { |
| 3335 | struct task_struct *curr = rq->curr; |
| 3336 | struct cfs_rq *cfs_rq = task_cfs_rq(curr); |
| 3337 | struct sched_entity *se = &curr->se; |
| 3338 | |
| 3339 | /* |
| 3340 | * Are we the only task in the tree? |
| 3341 | */ |
| 3342 | if (unlikely(rq->nr_running == 1)) |
| 3343 | return; |
| 3344 | |
| 3345 | clear_buddies(cfs_rq, se); |
| 3346 | |
| 3347 | if (curr->policy != SCHED_BATCH) { |
| 3348 | update_rq_clock(rq); |
| 3349 | /* |
| 3350 | * Update run-time statistics of the 'current'. |
| 3351 | */ |
| 3352 | update_curr(cfs_rq); |
Mike Galbraith | 916671c | 2011-11-22 15:21:26 +0100 | [diff] [blame] | 3353 | /* |
| 3354 | * Tell update_rq_clock() that we've just updated, |
| 3355 | * so we don't do microscopic update in schedule() |
| 3356 | * and double the fastpath cost. |
| 3357 | */ |
| 3358 | rq->skip_clock_update = 1; |
Rik van Riel | ac53db5 | 2011-02-01 09:51:03 -0500 | [diff] [blame] | 3359 | } |
| 3360 | |
| 3361 | set_skip_buddy(se); |
| 3362 | } |
| 3363 | |
Mike Galbraith | d95f412 | 2011-02-01 09:50:51 -0500 | [diff] [blame] | 3364 | static bool yield_to_task_fair(struct rq *rq, struct task_struct *p, bool preempt) |
| 3365 | { |
| 3366 | struct sched_entity *se = &p->se; |
| 3367 | |
Paul Turner | 5238cdd | 2011-07-21 09:43:37 -0700 | [diff] [blame] | 3368 | /* throttled hierarchies are not runnable */ |
| 3369 | if (!se->on_rq || throttled_hierarchy(cfs_rq_of(se))) |
Mike Galbraith | d95f412 | 2011-02-01 09:50:51 -0500 | [diff] [blame] | 3370 | return false; |
| 3371 | |
| 3372 | /* Tell the scheduler that we'd really like pse to run next. */ |
| 3373 | set_next_buddy(se); |
| 3374 | |
Mike Galbraith | d95f412 | 2011-02-01 09:50:51 -0500 | [diff] [blame] | 3375 | yield_task_fair(rq); |
| 3376 | |
| 3377 | return true; |
| 3378 | } |
| 3379 | |
Peter Williams | 681f3e6 | 2007-10-24 18:23:51 +0200 | [diff] [blame] | 3380 | #ifdef CONFIG_SMP |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 3381 | /************************************************** |
| 3382 | * Fair scheduling class load-balancing methods: |
| 3383 | */ |
| 3384 | |
Hiroshi Shimamoto | ed387b7 | 2012-01-31 11:40:32 +0900 | [diff] [blame] | 3385 | static unsigned long __read_mostly max_load_balance_interval = HZ/10; |
| 3386 | |
Peter Zijlstra | ddcdf6e | 2012-02-22 19:27:40 +0100 | [diff] [blame] | 3387 | #define LBF_ALL_PINNED 0x01 |
Peter Zijlstra | 367456c | 2012-02-20 21:49:09 +0100 | [diff] [blame] | 3388 | #define LBF_NEED_BREAK 0x02 |
Srivatsa Vaddagiri | 88b8dac | 2012-06-19 17:43:15 +0530 | [diff] [blame] | 3389 | #define LBF_SOME_PINNED 0x04 |
Peter Zijlstra | ddcdf6e | 2012-02-22 19:27:40 +0100 | [diff] [blame] | 3390 | |
| 3391 | struct lb_env { |
| 3392 | struct sched_domain *sd; |
| 3393 | |
Peter Zijlstra | ddcdf6e | 2012-02-22 19:27:40 +0100 | [diff] [blame] | 3394 | struct rq *src_rq; |
Prashanth Nageshappa | 85c1e7d | 2012-06-19 17:47:34 +0530 | [diff] [blame] | 3395 | int src_cpu; |
Peter Zijlstra | ddcdf6e | 2012-02-22 19:27:40 +0100 | [diff] [blame] | 3396 | |
| 3397 | int dst_cpu; |
| 3398 | struct rq *dst_rq; |
| 3399 | |
Srivatsa Vaddagiri | 88b8dac | 2012-06-19 17:43:15 +0530 | [diff] [blame] | 3400 | struct cpumask *dst_grpmask; |
| 3401 | int new_dst_cpu; |
Peter Zijlstra | ddcdf6e | 2012-02-22 19:27:40 +0100 | [diff] [blame] | 3402 | enum cpu_idle_type idle; |
Peter Zijlstra | bd939f4 | 2012-05-02 14:20:37 +0200 | [diff] [blame] | 3403 | long imbalance; |
Michael Wang | b940313 | 2012-07-12 16:10:13 +0800 | [diff] [blame] | 3404 | /* The set of CPUs under consideration for load-balancing */ |
| 3405 | struct cpumask *cpus; |
| 3406 | |
Peter Zijlstra | ddcdf6e | 2012-02-22 19:27:40 +0100 | [diff] [blame] | 3407 | unsigned int flags; |
Peter Zijlstra | 367456c | 2012-02-20 21:49:09 +0100 | [diff] [blame] | 3408 | |
| 3409 | unsigned int loop; |
| 3410 | unsigned int loop_break; |
| 3411 | unsigned int loop_max; |
Peter Zijlstra | ddcdf6e | 2012-02-22 19:27:40 +0100 | [diff] [blame] | 3412 | }; |
| 3413 | |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 3414 | /* |
Peter Zijlstra | ddcdf6e | 2012-02-22 19:27:40 +0100 | [diff] [blame] | 3415 | * move_task - move a task from one runqueue to another runqueue. |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 3416 | * Both runqueues must be locked. |
| 3417 | */ |
Peter Zijlstra | ddcdf6e | 2012-02-22 19:27:40 +0100 | [diff] [blame] | 3418 | static void move_task(struct task_struct *p, struct lb_env *env) |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 3419 | { |
Peter Zijlstra | ddcdf6e | 2012-02-22 19:27:40 +0100 | [diff] [blame] | 3420 | deactivate_task(env->src_rq, p, 0); |
| 3421 | set_task_cpu(p, env->dst_cpu); |
| 3422 | activate_task(env->dst_rq, p, 0); |
| 3423 | check_preempt_curr(env->dst_rq, p, 0); |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 3424 | } |
| 3425 | |
| 3426 | /* |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 3427 | * Is this task likely cache-hot: |
| 3428 | */ |
| 3429 | static int |
| 3430 | task_hot(struct task_struct *p, u64 now, struct sched_domain *sd) |
| 3431 | { |
| 3432 | s64 delta; |
| 3433 | |
| 3434 | if (p->sched_class != &fair_sched_class) |
| 3435 | return 0; |
| 3436 | |
| 3437 | if (unlikely(p->policy == SCHED_IDLE)) |
| 3438 | return 0; |
| 3439 | |
| 3440 | /* |
| 3441 | * Buddy candidates are cache hot: |
| 3442 | */ |
| 3443 | if (sched_feat(CACHE_HOT_BUDDY) && this_rq()->nr_running && |
| 3444 | (&p->se == cfs_rq_of(&p->se)->next || |
| 3445 | &p->se == cfs_rq_of(&p->se)->last)) |
| 3446 | return 1; |
| 3447 | |
| 3448 | if (sysctl_sched_migration_cost == -1) |
| 3449 | return 1; |
| 3450 | if (sysctl_sched_migration_cost == 0) |
| 3451 | return 0; |
| 3452 | |
| 3453 | delta = now - p->se.exec_start; |
| 3454 | |
| 3455 | return delta < (s64)sysctl_sched_migration_cost; |
| 3456 | } |
| 3457 | |
| 3458 | /* |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 3459 | * can_migrate_task - may task p from runqueue rq be migrated to this_cpu? |
| 3460 | */ |
| 3461 | static |
Peter Zijlstra | 8e45cb5 | 2012-02-22 12:47:19 +0100 | [diff] [blame] | 3462 | int can_migrate_task(struct task_struct *p, struct lb_env *env) |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 3463 | { |
| 3464 | int tsk_cache_hot = 0; |
| 3465 | /* |
| 3466 | * We do not migrate tasks that are: |
| 3467 | * 1) running (obviously), or |
| 3468 | * 2) cannot be migrated to this CPU due to cpus_allowed, or |
| 3469 | * 3) are cache-hot on their current CPU. |
| 3470 | */ |
Peter Zijlstra | ddcdf6e | 2012-02-22 19:27:40 +0100 | [diff] [blame] | 3471 | if (!cpumask_test_cpu(env->dst_cpu, tsk_cpus_allowed(p))) { |
Srivatsa Vaddagiri | 88b8dac | 2012-06-19 17:43:15 +0530 | [diff] [blame] | 3472 | int new_dst_cpu; |
| 3473 | |
Lucas De Marchi | 41acab8 | 2010-03-10 23:37:45 -0300 | [diff] [blame] | 3474 | schedstat_inc(p, se.statistics.nr_failed_migrations_affine); |
Srivatsa Vaddagiri | 88b8dac | 2012-06-19 17:43:15 +0530 | [diff] [blame] | 3475 | |
| 3476 | /* |
| 3477 | * Remember if this task can be migrated to any other cpu in |
| 3478 | * our sched_group. We may want to revisit it if we couldn't |
| 3479 | * meet load balance goals by pulling other tasks on src_cpu. |
| 3480 | * |
| 3481 | * Also avoid computing new_dst_cpu if we have already computed |
| 3482 | * one in current iteration. |
| 3483 | */ |
| 3484 | if (!env->dst_grpmask || (env->flags & LBF_SOME_PINNED)) |
| 3485 | return 0; |
| 3486 | |
| 3487 | new_dst_cpu = cpumask_first_and(env->dst_grpmask, |
| 3488 | tsk_cpus_allowed(p)); |
| 3489 | if (new_dst_cpu < nr_cpu_ids) { |
| 3490 | env->flags |= LBF_SOME_PINNED; |
| 3491 | env->new_dst_cpu = new_dst_cpu; |
| 3492 | } |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 3493 | return 0; |
| 3494 | } |
Srivatsa Vaddagiri | 88b8dac | 2012-06-19 17:43:15 +0530 | [diff] [blame] | 3495 | |
| 3496 | /* Record that we found atleast one task that could run on dst_cpu */ |
Peter Zijlstra | 8e45cb5 | 2012-02-22 12:47:19 +0100 | [diff] [blame] | 3497 | env->flags &= ~LBF_ALL_PINNED; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 3498 | |
Peter Zijlstra | ddcdf6e | 2012-02-22 19:27:40 +0100 | [diff] [blame] | 3499 | if (task_running(env->src_rq, p)) { |
Lucas De Marchi | 41acab8 | 2010-03-10 23:37:45 -0300 | [diff] [blame] | 3500 | schedstat_inc(p, se.statistics.nr_failed_migrations_running); |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 3501 | return 0; |
| 3502 | } |
| 3503 | |
| 3504 | /* |
| 3505 | * Aggressive migration if: |
| 3506 | * 1) task is cache cold, or |
| 3507 | * 2) too many balance attempts have failed. |
| 3508 | */ |
| 3509 | |
Peter Zijlstra | ddcdf6e | 2012-02-22 19:27:40 +0100 | [diff] [blame] | 3510 | tsk_cache_hot = task_hot(p, env->src_rq->clock_task, env->sd); |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 3511 | if (!tsk_cache_hot || |
Peter Zijlstra | 8e45cb5 | 2012-02-22 12:47:19 +0100 | [diff] [blame] | 3512 | env->sd->nr_balance_failed > env->sd->cache_nice_tries) { |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 3513 | #ifdef CONFIG_SCHEDSTATS |
| 3514 | if (tsk_cache_hot) { |
Peter Zijlstra | 8e45cb5 | 2012-02-22 12:47:19 +0100 | [diff] [blame] | 3515 | schedstat_inc(env->sd, lb_hot_gained[env->idle]); |
Lucas De Marchi | 41acab8 | 2010-03-10 23:37:45 -0300 | [diff] [blame] | 3516 | schedstat_inc(p, se.statistics.nr_forced_migrations); |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 3517 | } |
| 3518 | #endif |
| 3519 | return 1; |
| 3520 | } |
| 3521 | |
| 3522 | if (tsk_cache_hot) { |
Lucas De Marchi | 41acab8 | 2010-03-10 23:37:45 -0300 | [diff] [blame] | 3523 | schedstat_inc(p, se.statistics.nr_failed_migrations_hot); |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 3524 | return 0; |
| 3525 | } |
| 3526 | return 1; |
| 3527 | } |
| 3528 | |
Peter Zijlstra | 897c395 | 2009-12-17 17:45:42 +0100 | [diff] [blame] | 3529 | /* |
| 3530 | * move_one_task tries to move exactly one task from busiest to this_rq, as |
| 3531 | * part of active balancing operations within "domain". |
| 3532 | * Returns 1 if successful and 0 otherwise. |
| 3533 | * |
| 3534 | * Called with both runqueues locked. |
| 3535 | */ |
Peter Zijlstra | 8e45cb5 | 2012-02-22 12:47:19 +0100 | [diff] [blame] | 3536 | static int move_one_task(struct lb_env *env) |
Peter Zijlstra | 897c395 | 2009-12-17 17:45:42 +0100 | [diff] [blame] | 3537 | { |
| 3538 | struct task_struct *p, *n; |
Peter Zijlstra | 897c395 | 2009-12-17 17:45:42 +0100 | [diff] [blame] | 3539 | |
Peter Zijlstra | 367456c | 2012-02-20 21:49:09 +0100 | [diff] [blame] | 3540 | list_for_each_entry_safe(p, n, &env->src_rq->cfs_tasks, se.group_node) { |
| 3541 | if (throttled_lb_pair(task_group(p), env->src_rq->cpu, env->dst_cpu)) |
| 3542 | continue; |
Peter Zijlstra | 897c395 | 2009-12-17 17:45:42 +0100 | [diff] [blame] | 3543 | |
Peter Zijlstra | 367456c | 2012-02-20 21:49:09 +0100 | [diff] [blame] | 3544 | if (!can_migrate_task(p, env)) |
| 3545 | continue; |
Peter Zijlstra | 897c395 | 2009-12-17 17:45:42 +0100 | [diff] [blame] | 3546 | |
Peter Zijlstra | 367456c | 2012-02-20 21:49:09 +0100 | [diff] [blame] | 3547 | move_task(p, env); |
| 3548 | /* |
| 3549 | * Right now, this is only the second place move_task() |
| 3550 | * is called, so we can safely collect move_task() |
| 3551 | * stats here rather than inside move_task(). |
| 3552 | */ |
| 3553 | schedstat_inc(env->sd, lb_gained[env->idle]); |
| 3554 | return 1; |
Peter Zijlstra | 897c395 | 2009-12-17 17:45:42 +0100 | [diff] [blame] | 3555 | } |
Peter Zijlstra | 897c395 | 2009-12-17 17:45:42 +0100 | [diff] [blame] | 3556 | return 0; |
| 3557 | } |
| 3558 | |
Peter Zijlstra | 367456c | 2012-02-20 21:49:09 +0100 | [diff] [blame] | 3559 | static unsigned long task_h_load(struct task_struct *p); |
| 3560 | |
Peter Zijlstra | eb95308 | 2012-04-17 13:38:40 +0200 | [diff] [blame] | 3561 | static const unsigned int sched_nr_migrate_break = 32; |
| 3562 | |
Peter Zijlstra | 5d6523e | 2012-03-10 00:07:36 +0100 | [diff] [blame] | 3563 | /* |
Peter Zijlstra | bd939f4 | 2012-05-02 14:20:37 +0200 | [diff] [blame] | 3564 | * move_tasks tries to move up to imbalance weighted load from busiest to |
Peter Zijlstra | 5d6523e | 2012-03-10 00:07:36 +0100 | [diff] [blame] | 3565 | * this_rq, as part of a balancing operation within domain "sd". |
| 3566 | * Returns 1 if successful and 0 otherwise. |
| 3567 | * |
| 3568 | * Called with both runqueues locked. |
| 3569 | */ |
| 3570 | static int move_tasks(struct lb_env *env) |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 3571 | { |
Peter Zijlstra | 5d6523e | 2012-03-10 00:07:36 +0100 | [diff] [blame] | 3572 | struct list_head *tasks = &env->src_rq->cfs_tasks; |
| 3573 | struct task_struct *p; |
Peter Zijlstra | 367456c | 2012-02-20 21:49:09 +0100 | [diff] [blame] | 3574 | unsigned long load; |
| 3575 | int pulled = 0; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 3576 | |
Peter Zijlstra | bd939f4 | 2012-05-02 14:20:37 +0200 | [diff] [blame] | 3577 | if (env->imbalance <= 0) |
Peter Zijlstra | 5d6523e | 2012-03-10 00:07:36 +0100 | [diff] [blame] | 3578 | return 0; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 3579 | |
Peter Zijlstra | 5d6523e | 2012-03-10 00:07:36 +0100 | [diff] [blame] | 3580 | while (!list_empty(tasks)) { |
| 3581 | p = list_first_entry(tasks, struct task_struct, se.group_node); |
| 3582 | |
Peter Zijlstra | 367456c | 2012-02-20 21:49:09 +0100 | [diff] [blame] | 3583 | env->loop++; |
| 3584 | /* We've more or less seen every task there is, call it quits */ |
Peter Zijlstra | 5d6523e | 2012-03-10 00:07:36 +0100 | [diff] [blame] | 3585 | if (env->loop > env->loop_max) |
Peter Zijlstra | 367456c | 2012-02-20 21:49:09 +0100 | [diff] [blame] | 3586 | break; |
Peter Zijlstra | 5d6523e | 2012-03-10 00:07:36 +0100 | [diff] [blame] | 3587 | |
| 3588 | /* take a breather every nr_migrate tasks */ |
Peter Zijlstra | 367456c | 2012-02-20 21:49:09 +0100 | [diff] [blame] | 3589 | if (env->loop > env->loop_break) { |
Peter Zijlstra | eb95308 | 2012-04-17 13:38:40 +0200 | [diff] [blame] | 3590 | env->loop_break += sched_nr_migrate_break; |
Peter Zijlstra | 8e45cb5 | 2012-02-22 12:47:19 +0100 | [diff] [blame] | 3591 | env->flags |= LBF_NEED_BREAK; |
Peter Zijlstra | ee00e66 | 2009-12-17 17:25:20 +0100 | [diff] [blame] | 3592 | break; |
Peter Zijlstra | a195f00 | 2011-09-22 15:30:18 +0200 | [diff] [blame] | 3593 | } |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 3594 | |
Peter Zijlstra | 5d6523e | 2012-03-10 00:07:36 +0100 | [diff] [blame] | 3595 | if (throttled_lb_pair(task_group(p), env->src_cpu, env->dst_cpu)) |
Peter Zijlstra | 367456c | 2012-02-20 21:49:09 +0100 | [diff] [blame] | 3596 | goto next; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 3597 | |
Peter Zijlstra | 367456c | 2012-02-20 21:49:09 +0100 | [diff] [blame] | 3598 | load = task_h_load(p); |
Peter Zijlstra | 5d6523e | 2012-03-10 00:07:36 +0100 | [diff] [blame] | 3599 | |
Peter Zijlstra | eb95308 | 2012-04-17 13:38:40 +0200 | [diff] [blame] | 3600 | if (sched_feat(LB_MIN) && load < 16 && !env->sd->nr_balance_failed) |
Peter Zijlstra | 367456c | 2012-02-20 21:49:09 +0100 | [diff] [blame] | 3601 | goto next; |
| 3602 | |
Peter Zijlstra | bd939f4 | 2012-05-02 14:20:37 +0200 | [diff] [blame] | 3603 | if ((load / 2) > env->imbalance) |
Peter Zijlstra | 367456c | 2012-02-20 21:49:09 +0100 | [diff] [blame] | 3604 | goto next; |
| 3605 | |
| 3606 | if (!can_migrate_task(p, env)) |
| 3607 | goto next; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 3608 | |
Peter Zijlstra | ddcdf6e | 2012-02-22 19:27:40 +0100 | [diff] [blame] | 3609 | move_task(p, env); |
Peter Zijlstra | ee00e66 | 2009-12-17 17:25:20 +0100 | [diff] [blame] | 3610 | pulled++; |
Peter Zijlstra | bd939f4 | 2012-05-02 14:20:37 +0200 | [diff] [blame] | 3611 | env->imbalance -= load; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 3612 | |
| 3613 | #ifdef CONFIG_PREEMPT |
Peter Zijlstra | ee00e66 | 2009-12-17 17:25:20 +0100 | [diff] [blame] | 3614 | /* |
| 3615 | * NEWIDLE balancing is a source of latency, so preemptible |
| 3616 | * kernels will stop after the first task is pulled to minimize |
| 3617 | * the critical section. |
| 3618 | */ |
Peter Zijlstra | 5d6523e | 2012-03-10 00:07:36 +0100 | [diff] [blame] | 3619 | if (env->idle == CPU_NEWLY_IDLE) |
Peter Zijlstra | ee00e66 | 2009-12-17 17:25:20 +0100 | [diff] [blame] | 3620 | break; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 3621 | #endif |
| 3622 | |
Peter Zijlstra | ee00e66 | 2009-12-17 17:25:20 +0100 | [diff] [blame] | 3623 | /* |
| 3624 | * We only want to steal up to the prescribed amount of |
| 3625 | * weighted load. |
| 3626 | */ |
Peter Zijlstra | bd939f4 | 2012-05-02 14:20:37 +0200 | [diff] [blame] | 3627 | if (env->imbalance <= 0) |
Peter Zijlstra | ee00e66 | 2009-12-17 17:25:20 +0100 | [diff] [blame] | 3628 | break; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 3629 | |
Peter Zijlstra | 367456c | 2012-02-20 21:49:09 +0100 | [diff] [blame] | 3630 | continue; |
| 3631 | next: |
Peter Zijlstra | 5d6523e | 2012-03-10 00:07:36 +0100 | [diff] [blame] | 3632 | list_move_tail(&p->se.group_node, tasks); |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 3633 | } |
Peter Zijlstra | 5d6523e | 2012-03-10 00:07:36 +0100 | [diff] [blame] | 3634 | |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 3635 | /* |
Peter Zijlstra | ddcdf6e | 2012-02-22 19:27:40 +0100 | [diff] [blame] | 3636 | * Right now, this is one of only two places move_task() is called, |
| 3637 | * so we can safely collect move_task() stats here rather than |
| 3638 | * inside move_task(). |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 3639 | */ |
Peter Zijlstra | 8e45cb5 | 2012-02-22 12:47:19 +0100 | [diff] [blame] | 3640 | schedstat_add(env->sd, lb_gained[env->idle], pulled); |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 3641 | |
Peter Zijlstra | 5d6523e | 2012-03-10 00:07:36 +0100 | [diff] [blame] | 3642 | return pulled; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 3643 | } |
| 3644 | |
Peter Zijlstra | 230059de | 2009-12-17 17:47:12 +0100 | [diff] [blame] | 3645 | #ifdef CONFIG_FAIR_GROUP_SCHED |
Peter Zijlstra | 9e3081c | 2010-11-15 15:47:02 -0800 | [diff] [blame] | 3646 | /* |
| 3647 | * update tg->load_weight by folding this cpu's load_avg |
| 3648 | */ |
Paul Turner | 67e8625 | 2010-11-15 15:47:05 -0800 | [diff] [blame] | 3649 | static int update_shares_cpu(struct task_group *tg, int cpu) |
Peter Zijlstra | 9e3081c | 2010-11-15 15:47:02 -0800 | [diff] [blame] | 3650 | { |
| 3651 | struct cfs_rq *cfs_rq; |
| 3652 | unsigned long flags; |
| 3653 | struct rq *rq; |
Peter Zijlstra | 9e3081c | 2010-11-15 15:47:02 -0800 | [diff] [blame] | 3654 | |
| 3655 | if (!tg->se[cpu]) |
| 3656 | return 0; |
| 3657 | |
| 3658 | rq = cpu_rq(cpu); |
| 3659 | cfs_rq = tg->cfs_rq[cpu]; |
| 3660 | |
| 3661 | raw_spin_lock_irqsave(&rq->lock, flags); |
| 3662 | |
| 3663 | update_rq_clock(rq); |
Paul Turner | d6b5591 | 2010-11-15 15:47:09 -0800 | [diff] [blame] | 3664 | update_cfs_load(cfs_rq, 1); |
Paul Turner | aff3e49 | 2012-10-04 13:18:30 +0200 | [diff] [blame] | 3665 | update_cfs_rq_blocked_load(cfs_rq, 1); |
Peter Zijlstra | 9e3081c | 2010-11-15 15:47:02 -0800 | [diff] [blame] | 3666 | |
| 3667 | /* |
| 3668 | * We need to update shares after updating tg->load_weight in |
| 3669 | * order to adjust the weight of groups with long running tasks. |
| 3670 | */ |
Paul Turner | 6d5ab29 | 2011-01-21 20:45:01 -0800 | [diff] [blame] | 3671 | update_cfs_shares(cfs_rq); |
Peter Zijlstra | 9e3081c | 2010-11-15 15:47:02 -0800 | [diff] [blame] | 3672 | |
| 3673 | raw_spin_unlock_irqrestore(&rq->lock, flags); |
| 3674 | |
| 3675 | return 0; |
| 3676 | } |
| 3677 | |
| 3678 | static void update_shares(int cpu) |
| 3679 | { |
| 3680 | struct cfs_rq *cfs_rq; |
| 3681 | struct rq *rq = cpu_rq(cpu); |
| 3682 | |
| 3683 | rcu_read_lock(); |
Peter Zijlstra | 9763b67 | 2011-07-13 13:09:25 +0200 | [diff] [blame] | 3684 | /* |
| 3685 | * Iterates the task_group tree in a bottom up fashion, see |
| 3686 | * list_add_leaf_cfs_rq() for details. |
| 3687 | */ |
Paul Turner | 64660c8 | 2011-07-21 09:43:36 -0700 | [diff] [blame] | 3688 | for_each_leaf_cfs_rq(rq, cfs_rq) { |
| 3689 | /* throttled entities do not contribute to load */ |
| 3690 | if (throttled_hierarchy(cfs_rq)) |
| 3691 | continue; |
| 3692 | |
Paul Turner | 67e8625 | 2010-11-15 15:47:05 -0800 | [diff] [blame] | 3693 | update_shares_cpu(cfs_rq->tg, cpu); |
Paul Turner | 64660c8 | 2011-07-21 09:43:36 -0700 | [diff] [blame] | 3694 | } |
Peter Zijlstra | 9e3081c | 2010-11-15 15:47:02 -0800 | [diff] [blame] | 3695 | rcu_read_unlock(); |
| 3696 | } |
| 3697 | |
Peter Zijlstra | 9763b67 | 2011-07-13 13:09:25 +0200 | [diff] [blame] | 3698 | /* |
| 3699 | * Compute the cpu's hierarchical load factor for each task group. |
| 3700 | * This needs to be done in a top-down fashion because the load of a child |
| 3701 | * group is a fraction of its parents load. |
| 3702 | */ |
| 3703 | static int tg_load_down(struct task_group *tg, void *data) |
| 3704 | { |
| 3705 | unsigned long load; |
| 3706 | long cpu = (long)data; |
| 3707 | |
| 3708 | if (!tg->parent) { |
| 3709 | load = cpu_rq(cpu)->load.weight; |
| 3710 | } else { |
| 3711 | load = tg->parent->cfs_rq[cpu]->h_load; |
| 3712 | load *= tg->se[cpu]->load.weight; |
| 3713 | load /= tg->parent->cfs_rq[cpu]->load.weight + 1; |
| 3714 | } |
| 3715 | |
| 3716 | tg->cfs_rq[cpu]->h_load = load; |
| 3717 | |
| 3718 | return 0; |
| 3719 | } |
| 3720 | |
| 3721 | static void update_h_load(long cpu) |
| 3722 | { |
Peter Zijlstra | a35b646 | 2012-08-08 21:46:40 +0200 | [diff] [blame] | 3723 | struct rq *rq = cpu_rq(cpu); |
| 3724 | unsigned long now = jiffies; |
| 3725 | |
| 3726 | if (rq->h_load_throttle == now) |
| 3727 | return; |
| 3728 | |
| 3729 | rq->h_load_throttle = now; |
| 3730 | |
Peter Zijlstra | 367456c | 2012-02-20 21:49:09 +0100 | [diff] [blame] | 3731 | rcu_read_lock(); |
Peter Zijlstra | 9763b67 | 2011-07-13 13:09:25 +0200 | [diff] [blame] | 3732 | walk_tg_tree(tg_load_down, tg_nop, (void *)cpu); |
Peter Zijlstra | 367456c | 2012-02-20 21:49:09 +0100 | [diff] [blame] | 3733 | rcu_read_unlock(); |
Peter Zijlstra | 9763b67 | 2011-07-13 13:09:25 +0200 | [diff] [blame] | 3734 | } |
| 3735 | |
Peter Zijlstra | 367456c | 2012-02-20 21:49:09 +0100 | [diff] [blame] | 3736 | static unsigned long task_h_load(struct task_struct *p) |
Peter Zijlstra | 230059de | 2009-12-17 17:47:12 +0100 | [diff] [blame] | 3737 | { |
Peter Zijlstra | 367456c | 2012-02-20 21:49:09 +0100 | [diff] [blame] | 3738 | struct cfs_rq *cfs_rq = task_cfs_rq(p); |
| 3739 | unsigned long load; |
Peter Zijlstra | 230059de | 2009-12-17 17:47:12 +0100 | [diff] [blame] | 3740 | |
Peter Zijlstra | 367456c | 2012-02-20 21:49:09 +0100 | [diff] [blame] | 3741 | load = p->se.load.weight; |
| 3742 | load = div_u64(load * cfs_rq->h_load, cfs_rq->load.weight + 1); |
Peter Zijlstra | 230059de | 2009-12-17 17:47:12 +0100 | [diff] [blame] | 3743 | |
Peter Zijlstra | 367456c | 2012-02-20 21:49:09 +0100 | [diff] [blame] | 3744 | return load; |
Peter Zijlstra | 230059de | 2009-12-17 17:47:12 +0100 | [diff] [blame] | 3745 | } |
| 3746 | #else |
Peter Zijlstra | 9e3081c | 2010-11-15 15:47:02 -0800 | [diff] [blame] | 3747 | static inline void update_shares(int cpu) |
| 3748 | { |
| 3749 | } |
| 3750 | |
Peter Zijlstra | 367456c | 2012-02-20 21:49:09 +0100 | [diff] [blame] | 3751 | static inline void update_h_load(long cpu) |
Peter Zijlstra | 230059de | 2009-12-17 17:47:12 +0100 | [diff] [blame] | 3752 | { |
Peter Zijlstra | 367456c | 2012-02-20 21:49:09 +0100 | [diff] [blame] | 3753 | } |
| 3754 | |
| 3755 | static unsigned long task_h_load(struct task_struct *p) |
| 3756 | { |
| 3757 | return p->se.load.weight; |
Peter Zijlstra | 230059de | 2009-12-17 17:47:12 +0100 | [diff] [blame] | 3758 | } |
| 3759 | #endif |
| 3760 | |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 3761 | /********** Helpers for find_busiest_group ************************/ |
| 3762 | /* |
| 3763 | * sd_lb_stats - Structure to store the statistics of a sched_domain |
| 3764 | * during load balancing. |
| 3765 | */ |
| 3766 | struct sd_lb_stats { |
| 3767 | struct sched_group *busiest; /* Busiest group in this sd */ |
| 3768 | struct sched_group *this; /* Local group in this sd */ |
| 3769 | unsigned long total_load; /* Total load of all groups in sd */ |
| 3770 | unsigned long total_pwr; /* Total power of all groups in sd */ |
| 3771 | unsigned long avg_load; /* Average load across all groups in sd */ |
| 3772 | |
| 3773 | /** Statistics of this group */ |
| 3774 | unsigned long this_load; |
| 3775 | unsigned long this_load_per_task; |
| 3776 | unsigned long this_nr_running; |
Nikhil Rao | fab4762 | 2010-10-15 13:12:29 -0700 | [diff] [blame] | 3777 | unsigned long this_has_capacity; |
Suresh Siddha | aae6d3d | 2010-09-17 15:02:32 -0700 | [diff] [blame] | 3778 | unsigned int this_idle_cpus; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 3779 | |
| 3780 | /* Statistics of the busiest group */ |
Suresh Siddha | aae6d3d | 2010-09-17 15:02:32 -0700 | [diff] [blame] | 3781 | unsigned int busiest_idle_cpus; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 3782 | unsigned long max_load; |
| 3783 | unsigned long busiest_load_per_task; |
| 3784 | unsigned long busiest_nr_running; |
Suresh Siddha | dd5feea | 2010-02-23 16:13:52 -0800 | [diff] [blame] | 3785 | unsigned long busiest_group_capacity; |
Nikhil Rao | fab4762 | 2010-10-15 13:12:29 -0700 | [diff] [blame] | 3786 | unsigned long busiest_has_capacity; |
Suresh Siddha | aae6d3d | 2010-09-17 15:02:32 -0700 | [diff] [blame] | 3787 | unsigned int busiest_group_weight; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 3788 | |
| 3789 | int group_imb; /* Is there imbalance in this sd */ |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 3790 | }; |
| 3791 | |
| 3792 | /* |
| 3793 | * sg_lb_stats - stats of a sched_group required for load_balancing |
| 3794 | */ |
| 3795 | struct sg_lb_stats { |
| 3796 | unsigned long avg_load; /*Avg load across the CPUs of the group */ |
| 3797 | unsigned long group_load; /* Total load over the CPUs of the group */ |
| 3798 | unsigned long sum_nr_running; /* Nr tasks running in the group */ |
| 3799 | unsigned long sum_weighted_load; /* Weighted load of group's tasks */ |
| 3800 | unsigned long group_capacity; |
Suresh Siddha | aae6d3d | 2010-09-17 15:02:32 -0700 | [diff] [blame] | 3801 | unsigned long idle_cpus; |
| 3802 | unsigned long group_weight; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 3803 | int group_imb; /* Is there an imbalance in the group ? */ |
Nikhil Rao | fab4762 | 2010-10-15 13:12:29 -0700 | [diff] [blame] | 3804 | int group_has_capacity; /* Is there extra capacity in the group? */ |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 3805 | }; |
| 3806 | |
| 3807 | /** |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 3808 | * get_sd_load_idx - Obtain the load index for a given sched domain. |
| 3809 | * @sd: The sched_domain whose load_idx is to be obtained. |
| 3810 | * @idle: The Idle status of the CPU for whose sd load_icx is obtained. |
| 3811 | */ |
| 3812 | static inline int get_sd_load_idx(struct sched_domain *sd, |
| 3813 | enum cpu_idle_type idle) |
| 3814 | { |
| 3815 | int load_idx; |
| 3816 | |
| 3817 | switch (idle) { |
| 3818 | case CPU_NOT_IDLE: |
| 3819 | load_idx = sd->busy_idx; |
| 3820 | break; |
| 3821 | |
| 3822 | case CPU_NEWLY_IDLE: |
| 3823 | load_idx = sd->newidle_idx; |
| 3824 | break; |
| 3825 | default: |
| 3826 | load_idx = sd->idle_idx; |
| 3827 | break; |
| 3828 | } |
| 3829 | |
| 3830 | return load_idx; |
| 3831 | } |
| 3832 | |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 3833 | unsigned long default_scale_freq_power(struct sched_domain *sd, int cpu) |
| 3834 | { |
Nikhil Rao | 1399fa7 | 2011-05-18 10:09:39 -0700 | [diff] [blame] | 3835 | return SCHED_POWER_SCALE; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 3836 | } |
| 3837 | |
| 3838 | unsigned long __weak arch_scale_freq_power(struct sched_domain *sd, int cpu) |
| 3839 | { |
| 3840 | return default_scale_freq_power(sd, cpu); |
| 3841 | } |
| 3842 | |
| 3843 | unsigned long default_scale_smt_power(struct sched_domain *sd, int cpu) |
| 3844 | { |
Peter Zijlstra | 669c55e | 2010-04-16 14:59:29 +0200 | [diff] [blame] | 3845 | unsigned long weight = sd->span_weight; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 3846 | unsigned long smt_gain = sd->smt_gain; |
| 3847 | |
| 3848 | smt_gain /= weight; |
| 3849 | |
| 3850 | return smt_gain; |
| 3851 | } |
| 3852 | |
| 3853 | unsigned long __weak arch_scale_smt_power(struct sched_domain *sd, int cpu) |
| 3854 | { |
| 3855 | return default_scale_smt_power(sd, cpu); |
| 3856 | } |
| 3857 | |
| 3858 | unsigned long scale_rt_power(int cpu) |
| 3859 | { |
| 3860 | struct rq *rq = cpu_rq(cpu); |
Peter Zijlstra | b654f7d | 2012-05-22 14:04:28 +0200 | [diff] [blame] | 3861 | u64 total, available, age_stamp, avg; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 3862 | |
Peter Zijlstra | b654f7d | 2012-05-22 14:04:28 +0200 | [diff] [blame] | 3863 | /* |
| 3864 | * Since we're reading these variables without serialization make sure |
| 3865 | * we read them once before doing sanity checks on them. |
| 3866 | */ |
| 3867 | age_stamp = ACCESS_ONCE(rq->age_stamp); |
| 3868 | avg = ACCESS_ONCE(rq->rt_avg); |
Venkatesh Pallipadi | aa48380 | 2010-10-04 17:03:22 -0700 | [diff] [blame] | 3869 | |
Peter Zijlstra | b654f7d | 2012-05-22 14:04:28 +0200 | [diff] [blame] | 3870 | total = sched_avg_period() + (rq->clock - age_stamp); |
| 3871 | |
| 3872 | if (unlikely(total < avg)) { |
Venkatesh Pallipadi | aa48380 | 2010-10-04 17:03:22 -0700 | [diff] [blame] | 3873 | /* Ensures that power won't end up being negative */ |
| 3874 | available = 0; |
| 3875 | } else { |
Peter Zijlstra | b654f7d | 2012-05-22 14:04:28 +0200 | [diff] [blame] | 3876 | available = total - avg; |
Venkatesh Pallipadi | aa48380 | 2010-10-04 17:03:22 -0700 | [diff] [blame] | 3877 | } |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 3878 | |
Nikhil Rao | 1399fa7 | 2011-05-18 10:09:39 -0700 | [diff] [blame] | 3879 | if (unlikely((s64)total < SCHED_POWER_SCALE)) |
| 3880 | total = SCHED_POWER_SCALE; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 3881 | |
Nikhil Rao | 1399fa7 | 2011-05-18 10:09:39 -0700 | [diff] [blame] | 3882 | total >>= SCHED_POWER_SHIFT; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 3883 | |
| 3884 | return div_u64(available, total); |
| 3885 | } |
| 3886 | |
| 3887 | static void update_cpu_power(struct sched_domain *sd, int cpu) |
| 3888 | { |
Peter Zijlstra | 669c55e | 2010-04-16 14:59:29 +0200 | [diff] [blame] | 3889 | unsigned long weight = sd->span_weight; |
Nikhil Rao | 1399fa7 | 2011-05-18 10:09:39 -0700 | [diff] [blame] | 3890 | unsigned long power = SCHED_POWER_SCALE; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 3891 | struct sched_group *sdg = sd->groups; |
| 3892 | |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 3893 | if ((sd->flags & SD_SHARE_CPUPOWER) && weight > 1) { |
| 3894 | if (sched_feat(ARCH_POWER)) |
| 3895 | power *= arch_scale_smt_power(sd, cpu); |
| 3896 | else |
| 3897 | power *= default_scale_smt_power(sd, cpu); |
| 3898 | |
Nikhil Rao | 1399fa7 | 2011-05-18 10:09:39 -0700 | [diff] [blame] | 3899 | power >>= SCHED_POWER_SHIFT; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 3900 | } |
| 3901 | |
Peter Zijlstra | 9c3f75c | 2011-07-14 13:00:06 +0200 | [diff] [blame] | 3902 | sdg->sgp->power_orig = power; |
Srivatsa Vaddagiri | 9d5efe0 | 2010-06-08 14:57:02 +1000 | [diff] [blame] | 3903 | |
| 3904 | if (sched_feat(ARCH_POWER)) |
| 3905 | power *= arch_scale_freq_power(sd, cpu); |
| 3906 | else |
| 3907 | power *= default_scale_freq_power(sd, cpu); |
| 3908 | |
Nikhil Rao | 1399fa7 | 2011-05-18 10:09:39 -0700 | [diff] [blame] | 3909 | power >>= SCHED_POWER_SHIFT; |
Srivatsa Vaddagiri | 9d5efe0 | 2010-06-08 14:57:02 +1000 | [diff] [blame] | 3910 | |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 3911 | power *= scale_rt_power(cpu); |
Nikhil Rao | 1399fa7 | 2011-05-18 10:09:39 -0700 | [diff] [blame] | 3912 | power >>= SCHED_POWER_SHIFT; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 3913 | |
| 3914 | if (!power) |
| 3915 | power = 1; |
| 3916 | |
Peter Zijlstra | e51fd5e | 2010-05-31 12:37:30 +0200 | [diff] [blame] | 3917 | cpu_rq(cpu)->cpu_power = power; |
Peter Zijlstra | 9c3f75c | 2011-07-14 13:00:06 +0200 | [diff] [blame] | 3918 | sdg->sgp->power = power; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 3919 | } |
| 3920 | |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 3921 | void update_group_power(struct sched_domain *sd, int cpu) |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 3922 | { |
| 3923 | struct sched_domain *child = sd->child; |
| 3924 | struct sched_group *group, *sdg = sd->groups; |
| 3925 | unsigned long power; |
Vincent Guittot | 4ec4412 | 2011-12-12 20:21:08 +0100 | [diff] [blame] | 3926 | unsigned long interval; |
| 3927 | |
| 3928 | interval = msecs_to_jiffies(sd->balance_interval); |
| 3929 | interval = clamp(interval, 1UL, max_load_balance_interval); |
| 3930 | sdg->sgp->next_update = jiffies + interval; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 3931 | |
| 3932 | if (!child) { |
| 3933 | update_cpu_power(sd, cpu); |
| 3934 | return; |
| 3935 | } |
| 3936 | |
| 3937 | power = 0; |
| 3938 | |
Peter Zijlstra | 74a5ce2 | 2012-05-23 18:00:43 +0200 | [diff] [blame] | 3939 | if (child->flags & SD_OVERLAP) { |
| 3940 | /* |
| 3941 | * SD_OVERLAP domains cannot assume that child groups |
| 3942 | * span the current group. |
| 3943 | */ |
| 3944 | |
| 3945 | for_each_cpu(cpu, sched_group_cpus(sdg)) |
| 3946 | power += power_of(cpu); |
| 3947 | } else { |
| 3948 | /* |
| 3949 | * !SD_OVERLAP domains can assume that child groups |
| 3950 | * span the current group. |
| 3951 | */ |
| 3952 | |
| 3953 | group = child->groups; |
| 3954 | do { |
| 3955 | power += group->sgp->power; |
| 3956 | group = group->next; |
| 3957 | } while (group != child->groups); |
| 3958 | } |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 3959 | |
Peter Zijlstra | c3decf0 | 2012-05-31 12:05:32 +0200 | [diff] [blame] | 3960 | sdg->sgp->power_orig = sdg->sgp->power = power; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 3961 | } |
| 3962 | |
Srivatsa Vaddagiri | 9d5efe0 | 2010-06-08 14:57:02 +1000 | [diff] [blame] | 3963 | /* |
| 3964 | * Try and fix up capacity for tiny siblings, this is needed when |
| 3965 | * things like SD_ASYM_PACKING need f_b_g to select another sibling |
| 3966 | * which on its own isn't powerful enough. |
| 3967 | * |
| 3968 | * See update_sd_pick_busiest() and check_asym_packing(). |
| 3969 | */ |
| 3970 | static inline int |
| 3971 | fix_small_capacity(struct sched_domain *sd, struct sched_group *group) |
| 3972 | { |
| 3973 | /* |
Nikhil Rao | 1399fa7 | 2011-05-18 10:09:39 -0700 | [diff] [blame] | 3974 | * Only siblings can have significantly less than SCHED_POWER_SCALE |
Srivatsa Vaddagiri | 9d5efe0 | 2010-06-08 14:57:02 +1000 | [diff] [blame] | 3975 | */ |
Peter Zijlstra | a6c75f2 | 2011-04-07 14:09:52 +0200 | [diff] [blame] | 3976 | if (!(sd->flags & SD_SHARE_CPUPOWER)) |
Srivatsa Vaddagiri | 9d5efe0 | 2010-06-08 14:57:02 +1000 | [diff] [blame] | 3977 | return 0; |
| 3978 | |
| 3979 | /* |
| 3980 | * If ~90% of the cpu_power is still there, we're good. |
| 3981 | */ |
Peter Zijlstra | 9c3f75c | 2011-07-14 13:00:06 +0200 | [diff] [blame] | 3982 | if (group->sgp->power * 32 > group->sgp->power_orig * 29) |
Srivatsa Vaddagiri | 9d5efe0 | 2010-06-08 14:57:02 +1000 | [diff] [blame] | 3983 | return 1; |
| 3984 | |
| 3985 | return 0; |
| 3986 | } |
| 3987 | |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 3988 | /** |
| 3989 | * update_sg_lb_stats - Update sched_group's statistics for load balancing. |
Randy Dunlap | cd96891 | 2012-06-08 13:18:33 -0700 | [diff] [blame] | 3990 | * @env: The load balancing environment. |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 3991 | * @group: sched_group whose statistics are to be updated. |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 3992 | * @load_idx: Load index of sched_domain of this_cpu for load calc. |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 3993 | * @local_group: Does group contain this_cpu. |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 3994 | * @balance: Should we balance. |
| 3995 | * @sgs: variable to hold the statistics for this group. |
| 3996 | */ |
Peter Zijlstra | bd939f4 | 2012-05-02 14:20:37 +0200 | [diff] [blame] | 3997 | static inline void update_sg_lb_stats(struct lb_env *env, |
| 3998 | struct sched_group *group, int load_idx, |
Michael Wang | b940313 | 2012-07-12 16:10:13 +0800 | [diff] [blame] | 3999 | int local_group, int *balance, struct sg_lb_stats *sgs) |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4000 | { |
Peter Zijlstra | e44bc5c | 2012-05-11 00:22:12 +0200 | [diff] [blame] | 4001 | unsigned long nr_running, max_nr_running, min_nr_running; |
| 4002 | unsigned long load, max_cpu_load, min_cpu_load; |
Peter Zijlstra | 04f733b | 2012-05-11 00:12:02 +0200 | [diff] [blame] | 4003 | unsigned int balance_cpu = -1, first_idle_cpu = 0; |
Suresh Siddha | dd5feea | 2010-02-23 16:13:52 -0800 | [diff] [blame] | 4004 | unsigned long avg_load_per_task = 0; |
Peter Zijlstra | bd939f4 | 2012-05-02 14:20:37 +0200 | [diff] [blame] | 4005 | int i; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4006 | |
Gautham R Shenoy | 871e35b | 2010-01-20 14:02:44 -0600 | [diff] [blame] | 4007 | if (local_group) |
Peter Zijlstra | c117487 | 2012-05-31 14:47:33 +0200 | [diff] [blame] | 4008 | balance_cpu = group_balance_cpu(group); |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4009 | |
| 4010 | /* Tally up the load of all CPUs in the group */ |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4011 | max_cpu_load = 0; |
| 4012 | min_cpu_load = ~0UL; |
Nikhil Rao | 2582f0e | 2010-10-13 12:09:36 -0700 | [diff] [blame] | 4013 | max_nr_running = 0; |
Peter Zijlstra | e44bc5c | 2012-05-11 00:22:12 +0200 | [diff] [blame] | 4014 | min_nr_running = ~0UL; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4015 | |
Michael Wang | b940313 | 2012-07-12 16:10:13 +0800 | [diff] [blame] | 4016 | for_each_cpu_and(i, sched_group_cpus(group), env->cpus) { |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4017 | struct rq *rq = cpu_rq(i); |
| 4018 | |
Peter Zijlstra | e44bc5c | 2012-05-11 00:22:12 +0200 | [diff] [blame] | 4019 | nr_running = rq->nr_running; |
| 4020 | |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4021 | /* Bias balancing toward cpus of our domain */ |
| 4022 | if (local_group) { |
Peter Zijlstra | c117487 | 2012-05-31 14:47:33 +0200 | [diff] [blame] | 4023 | if (idle_cpu(i) && !first_idle_cpu && |
| 4024 | cpumask_test_cpu(i, sched_group_mask(group))) { |
Peter Zijlstra | 04f733b | 2012-05-11 00:12:02 +0200 | [diff] [blame] | 4025 | first_idle_cpu = 1; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4026 | balance_cpu = i; |
| 4027 | } |
Peter Zijlstra | 04f733b | 2012-05-11 00:12:02 +0200 | [diff] [blame] | 4028 | |
| 4029 | load = target_load(i, load_idx); |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4030 | } else { |
| 4031 | load = source_load(i, load_idx); |
Peter Zijlstra | e44bc5c | 2012-05-11 00:22:12 +0200 | [diff] [blame] | 4032 | if (load > max_cpu_load) |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4033 | max_cpu_load = load; |
| 4034 | if (min_cpu_load > load) |
| 4035 | min_cpu_load = load; |
Peter Zijlstra | e44bc5c | 2012-05-11 00:22:12 +0200 | [diff] [blame] | 4036 | |
| 4037 | if (nr_running > max_nr_running) |
| 4038 | max_nr_running = nr_running; |
| 4039 | if (min_nr_running > nr_running) |
| 4040 | min_nr_running = nr_running; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4041 | } |
| 4042 | |
| 4043 | sgs->group_load += load; |
Peter Zijlstra | e44bc5c | 2012-05-11 00:22:12 +0200 | [diff] [blame] | 4044 | sgs->sum_nr_running += nr_running; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4045 | sgs->sum_weighted_load += weighted_cpuload(i); |
Suresh Siddha | aae6d3d | 2010-09-17 15:02:32 -0700 | [diff] [blame] | 4046 | if (idle_cpu(i)) |
| 4047 | sgs->idle_cpus++; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4048 | } |
| 4049 | |
| 4050 | /* |
| 4051 | * First idle cpu or the first cpu(busiest) in this sched group |
| 4052 | * is eligible for doing load balancing at this and above |
| 4053 | * domains. In the newly idle case, we will allow all the cpu's |
| 4054 | * to do the newly idle load balance. |
| 4055 | */ |
Vincent Guittot | 4ec4412 | 2011-12-12 20:21:08 +0100 | [diff] [blame] | 4056 | if (local_group) { |
Peter Zijlstra | bd939f4 | 2012-05-02 14:20:37 +0200 | [diff] [blame] | 4057 | if (env->idle != CPU_NEWLY_IDLE) { |
Peter Zijlstra | 04f733b | 2012-05-11 00:12:02 +0200 | [diff] [blame] | 4058 | if (balance_cpu != env->dst_cpu) { |
Vincent Guittot | 4ec4412 | 2011-12-12 20:21:08 +0100 | [diff] [blame] | 4059 | *balance = 0; |
| 4060 | return; |
| 4061 | } |
Peter Zijlstra | bd939f4 | 2012-05-02 14:20:37 +0200 | [diff] [blame] | 4062 | update_group_power(env->sd, env->dst_cpu); |
Vincent Guittot | 4ec4412 | 2011-12-12 20:21:08 +0100 | [diff] [blame] | 4063 | } else if (time_after_eq(jiffies, group->sgp->next_update)) |
Peter Zijlstra | bd939f4 | 2012-05-02 14:20:37 +0200 | [diff] [blame] | 4064 | update_group_power(env->sd, env->dst_cpu); |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4065 | } |
| 4066 | |
| 4067 | /* Adjust by relative CPU power of the group */ |
Peter Zijlstra | 9c3f75c | 2011-07-14 13:00:06 +0200 | [diff] [blame] | 4068 | sgs->avg_load = (sgs->group_load*SCHED_POWER_SCALE) / group->sgp->power; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4069 | |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4070 | /* |
| 4071 | * Consider the group unbalanced when the imbalance is larger |
Peter Zijlstra | 866ab43 | 2011-02-21 18:56:47 +0100 | [diff] [blame] | 4072 | * than the average weight of a task. |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4073 | * |
| 4074 | * APZ: with cgroup the avg task weight can vary wildly and |
| 4075 | * might not be a suitable number - should we keep a |
| 4076 | * normalized nr_running number somewhere that negates |
| 4077 | * the hierarchy? |
| 4078 | */ |
Suresh Siddha | dd5feea | 2010-02-23 16:13:52 -0800 | [diff] [blame] | 4079 | if (sgs->sum_nr_running) |
| 4080 | avg_load_per_task = sgs->sum_weighted_load / sgs->sum_nr_running; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4081 | |
Peter Zijlstra | e44bc5c | 2012-05-11 00:22:12 +0200 | [diff] [blame] | 4082 | if ((max_cpu_load - min_cpu_load) >= avg_load_per_task && |
| 4083 | (max_nr_running - min_nr_running) > 1) |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4084 | sgs->group_imb = 1; |
| 4085 | |
Peter Zijlstra | 9c3f75c | 2011-07-14 13:00:06 +0200 | [diff] [blame] | 4086 | sgs->group_capacity = DIV_ROUND_CLOSEST(group->sgp->power, |
Nikhil Rao | 1399fa7 | 2011-05-18 10:09:39 -0700 | [diff] [blame] | 4087 | SCHED_POWER_SCALE); |
Srivatsa Vaddagiri | 9d5efe0 | 2010-06-08 14:57:02 +1000 | [diff] [blame] | 4088 | if (!sgs->group_capacity) |
Peter Zijlstra | bd939f4 | 2012-05-02 14:20:37 +0200 | [diff] [blame] | 4089 | sgs->group_capacity = fix_small_capacity(env->sd, group); |
Suresh Siddha | aae6d3d | 2010-09-17 15:02:32 -0700 | [diff] [blame] | 4090 | sgs->group_weight = group->group_weight; |
Nikhil Rao | fab4762 | 2010-10-15 13:12:29 -0700 | [diff] [blame] | 4091 | |
| 4092 | if (sgs->group_capacity > sgs->sum_nr_running) |
| 4093 | sgs->group_has_capacity = 1; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4094 | } |
| 4095 | |
| 4096 | /** |
Michael Neuling | 532cb4c | 2010-06-08 14:57:02 +1000 | [diff] [blame] | 4097 | * update_sd_pick_busiest - return 1 on busiest group |
Randy Dunlap | cd96891 | 2012-06-08 13:18:33 -0700 | [diff] [blame] | 4098 | * @env: The load balancing environment. |
Michael Neuling | 532cb4c | 2010-06-08 14:57:02 +1000 | [diff] [blame] | 4099 | * @sds: sched_domain statistics |
| 4100 | * @sg: sched_group candidate to be checked for being the busiest |
Michael Neuling | b6b1229 | 2010-06-10 12:06:21 +1000 | [diff] [blame] | 4101 | * @sgs: sched_group statistics |
Michael Neuling | 532cb4c | 2010-06-08 14:57:02 +1000 | [diff] [blame] | 4102 | * |
| 4103 | * Determine if @sg is a busier group than the previously selected |
| 4104 | * busiest group. |
| 4105 | */ |
Peter Zijlstra | bd939f4 | 2012-05-02 14:20:37 +0200 | [diff] [blame] | 4106 | static bool update_sd_pick_busiest(struct lb_env *env, |
Michael Neuling | 532cb4c | 2010-06-08 14:57:02 +1000 | [diff] [blame] | 4107 | struct sd_lb_stats *sds, |
| 4108 | struct sched_group *sg, |
Peter Zijlstra | bd939f4 | 2012-05-02 14:20:37 +0200 | [diff] [blame] | 4109 | struct sg_lb_stats *sgs) |
Michael Neuling | 532cb4c | 2010-06-08 14:57:02 +1000 | [diff] [blame] | 4110 | { |
| 4111 | if (sgs->avg_load <= sds->max_load) |
| 4112 | return false; |
| 4113 | |
| 4114 | if (sgs->sum_nr_running > sgs->group_capacity) |
| 4115 | return true; |
| 4116 | |
| 4117 | if (sgs->group_imb) |
| 4118 | return true; |
| 4119 | |
| 4120 | /* |
| 4121 | * ASYM_PACKING needs to move all the work to the lowest |
| 4122 | * numbered CPUs in the group, therefore mark all groups |
| 4123 | * higher than ourself as busy. |
| 4124 | */ |
Peter Zijlstra | bd939f4 | 2012-05-02 14:20:37 +0200 | [diff] [blame] | 4125 | if ((env->sd->flags & SD_ASYM_PACKING) && sgs->sum_nr_running && |
| 4126 | env->dst_cpu < group_first_cpu(sg)) { |
Michael Neuling | 532cb4c | 2010-06-08 14:57:02 +1000 | [diff] [blame] | 4127 | if (!sds->busiest) |
| 4128 | return true; |
| 4129 | |
| 4130 | if (group_first_cpu(sds->busiest) > group_first_cpu(sg)) |
| 4131 | return true; |
| 4132 | } |
| 4133 | |
| 4134 | return false; |
| 4135 | } |
| 4136 | |
| 4137 | /** |
Hui Kang | 461819a | 2011-10-11 23:00:59 -0400 | [diff] [blame] | 4138 | * update_sd_lb_stats - Update sched_domain's statistics for load balancing. |
Randy Dunlap | cd96891 | 2012-06-08 13:18:33 -0700 | [diff] [blame] | 4139 | * @env: The load balancing environment. |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4140 | * @balance: Should we balance. |
| 4141 | * @sds: variable to hold the statistics for this sched_domain. |
| 4142 | */ |
Peter Zijlstra | bd939f4 | 2012-05-02 14:20:37 +0200 | [diff] [blame] | 4143 | static inline void update_sd_lb_stats(struct lb_env *env, |
Michael Wang | b940313 | 2012-07-12 16:10:13 +0800 | [diff] [blame] | 4144 | int *balance, struct sd_lb_stats *sds) |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4145 | { |
Peter Zijlstra | bd939f4 | 2012-05-02 14:20:37 +0200 | [diff] [blame] | 4146 | struct sched_domain *child = env->sd->child; |
| 4147 | struct sched_group *sg = env->sd->groups; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4148 | struct sg_lb_stats sgs; |
| 4149 | int load_idx, prefer_sibling = 0; |
| 4150 | |
| 4151 | if (child && child->flags & SD_PREFER_SIBLING) |
| 4152 | prefer_sibling = 1; |
| 4153 | |
Peter Zijlstra | bd939f4 | 2012-05-02 14:20:37 +0200 | [diff] [blame] | 4154 | load_idx = get_sd_load_idx(env->sd, env->idle); |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4155 | |
| 4156 | do { |
| 4157 | int local_group; |
| 4158 | |
Peter Zijlstra | bd939f4 | 2012-05-02 14:20:37 +0200 | [diff] [blame] | 4159 | local_group = cpumask_test_cpu(env->dst_cpu, sched_group_cpus(sg)); |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4160 | memset(&sgs, 0, sizeof(sgs)); |
Michael Wang | b940313 | 2012-07-12 16:10:13 +0800 | [diff] [blame] | 4161 | update_sg_lb_stats(env, sg, load_idx, local_group, balance, &sgs); |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4162 | |
Peter Zijlstra | 8f190fb | 2009-12-24 14:18:21 +0100 | [diff] [blame] | 4163 | if (local_group && !(*balance)) |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4164 | return; |
| 4165 | |
| 4166 | sds->total_load += sgs.group_load; |
Peter Zijlstra | 9c3f75c | 2011-07-14 13:00:06 +0200 | [diff] [blame] | 4167 | sds->total_pwr += sg->sgp->power; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4168 | |
| 4169 | /* |
| 4170 | * In case the child domain prefers tasks go to siblings |
Michael Neuling | 532cb4c | 2010-06-08 14:57:02 +1000 | [diff] [blame] | 4171 | * first, lower the sg capacity to one so that we'll try |
Nikhil Rao | 75dd321 | 2010-10-15 13:12:30 -0700 | [diff] [blame] | 4172 | * and move all the excess tasks away. We lower the capacity |
| 4173 | * of a group only if the local group has the capacity to fit |
| 4174 | * these excess tasks, i.e. nr_running < group_capacity. The |
| 4175 | * extra check prevents the case where you always pull from the |
| 4176 | * heaviest group when it is already under-utilized (possible |
| 4177 | * with a large weight task outweighs the tasks on the system). |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4178 | */ |
Nikhil Rao | 75dd321 | 2010-10-15 13:12:30 -0700 | [diff] [blame] | 4179 | if (prefer_sibling && !local_group && sds->this_has_capacity) |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4180 | sgs.group_capacity = min(sgs.group_capacity, 1UL); |
| 4181 | |
| 4182 | if (local_group) { |
| 4183 | sds->this_load = sgs.avg_load; |
Michael Neuling | 532cb4c | 2010-06-08 14:57:02 +1000 | [diff] [blame] | 4184 | sds->this = sg; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4185 | sds->this_nr_running = sgs.sum_nr_running; |
| 4186 | sds->this_load_per_task = sgs.sum_weighted_load; |
Nikhil Rao | fab4762 | 2010-10-15 13:12:29 -0700 | [diff] [blame] | 4187 | sds->this_has_capacity = sgs.group_has_capacity; |
Suresh Siddha | aae6d3d | 2010-09-17 15:02:32 -0700 | [diff] [blame] | 4188 | sds->this_idle_cpus = sgs.idle_cpus; |
Peter Zijlstra | bd939f4 | 2012-05-02 14:20:37 +0200 | [diff] [blame] | 4189 | } else if (update_sd_pick_busiest(env, sds, sg, &sgs)) { |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4190 | sds->max_load = sgs.avg_load; |
Michael Neuling | 532cb4c | 2010-06-08 14:57:02 +1000 | [diff] [blame] | 4191 | sds->busiest = sg; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4192 | sds->busiest_nr_running = sgs.sum_nr_running; |
Suresh Siddha | aae6d3d | 2010-09-17 15:02:32 -0700 | [diff] [blame] | 4193 | sds->busiest_idle_cpus = sgs.idle_cpus; |
Suresh Siddha | dd5feea | 2010-02-23 16:13:52 -0800 | [diff] [blame] | 4194 | sds->busiest_group_capacity = sgs.group_capacity; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4195 | sds->busiest_load_per_task = sgs.sum_weighted_load; |
Nikhil Rao | fab4762 | 2010-10-15 13:12:29 -0700 | [diff] [blame] | 4196 | sds->busiest_has_capacity = sgs.group_has_capacity; |
Suresh Siddha | aae6d3d | 2010-09-17 15:02:32 -0700 | [diff] [blame] | 4197 | sds->busiest_group_weight = sgs.group_weight; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4198 | sds->group_imb = sgs.group_imb; |
| 4199 | } |
| 4200 | |
Michael Neuling | 532cb4c | 2010-06-08 14:57:02 +1000 | [diff] [blame] | 4201 | sg = sg->next; |
Peter Zijlstra | bd939f4 | 2012-05-02 14:20:37 +0200 | [diff] [blame] | 4202 | } while (sg != env->sd->groups); |
Michael Neuling | 532cb4c | 2010-06-08 14:57:02 +1000 | [diff] [blame] | 4203 | } |
| 4204 | |
Michael Neuling | 532cb4c | 2010-06-08 14:57:02 +1000 | [diff] [blame] | 4205 | /** |
| 4206 | * check_asym_packing - Check to see if the group is packed into the |
| 4207 | * sched doman. |
| 4208 | * |
| 4209 | * This is primarily intended to used at the sibling level. Some |
| 4210 | * cores like POWER7 prefer to use lower numbered SMT threads. In the |
| 4211 | * case of POWER7, it can move to lower SMT modes only when higher |
| 4212 | * threads are idle. When in lower SMT modes, the threads will |
| 4213 | * perform better since they share less core resources. Hence when we |
| 4214 | * have idle threads, we want them to be the higher ones. |
| 4215 | * |
| 4216 | * This packing function is run on idle threads. It checks to see if |
| 4217 | * the busiest CPU in this domain (core in the P7 case) has a higher |
| 4218 | * CPU number than the packing function is being run on. Here we are |
| 4219 | * assuming lower CPU number will be equivalent to lower a SMT thread |
| 4220 | * number. |
| 4221 | * |
Michael Neuling | b6b1229 | 2010-06-10 12:06:21 +1000 | [diff] [blame] | 4222 | * Returns 1 when packing is required and a task should be moved to |
| 4223 | * this CPU. The amount of the imbalance is returned in *imbalance. |
| 4224 | * |
Randy Dunlap | cd96891 | 2012-06-08 13:18:33 -0700 | [diff] [blame] | 4225 | * @env: The load balancing environment. |
Michael Neuling | 532cb4c | 2010-06-08 14:57:02 +1000 | [diff] [blame] | 4226 | * @sds: Statistics of the sched_domain which is to be packed |
Michael Neuling | 532cb4c | 2010-06-08 14:57:02 +1000 | [diff] [blame] | 4227 | */ |
Peter Zijlstra | bd939f4 | 2012-05-02 14:20:37 +0200 | [diff] [blame] | 4228 | static int check_asym_packing(struct lb_env *env, struct sd_lb_stats *sds) |
Michael Neuling | 532cb4c | 2010-06-08 14:57:02 +1000 | [diff] [blame] | 4229 | { |
| 4230 | int busiest_cpu; |
| 4231 | |
Peter Zijlstra | bd939f4 | 2012-05-02 14:20:37 +0200 | [diff] [blame] | 4232 | if (!(env->sd->flags & SD_ASYM_PACKING)) |
Michael Neuling | 532cb4c | 2010-06-08 14:57:02 +1000 | [diff] [blame] | 4233 | return 0; |
| 4234 | |
| 4235 | if (!sds->busiest) |
| 4236 | return 0; |
| 4237 | |
| 4238 | busiest_cpu = group_first_cpu(sds->busiest); |
Peter Zijlstra | bd939f4 | 2012-05-02 14:20:37 +0200 | [diff] [blame] | 4239 | if (env->dst_cpu > busiest_cpu) |
Michael Neuling | 532cb4c | 2010-06-08 14:57:02 +1000 | [diff] [blame] | 4240 | return 0; |
| 4241 | |
Peter Zijlstra | bd939f4 | 2012-05-02 14:20:37 +0200 | [diff] [blame] | 4242 | env->imbalance = DIV_ROUND_CLOSEST( |
| 4243 | sds->max_load * sds->busiest->sgp->power, SCHED_POWER_SCALE); |
| 4244 | |
Michael Neuling | 532cb4c | 2010-06-08 14:57:02 +1000 | [diff] [blame] | 4245 | return 1; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4246 | } |
| 4247 | |
| 4248 | /** |
| 4249 | * fix_small_imbalance - Calculate the minor imbalance that exists |
| 4250 | * amongst the groups of a sched_domain, during |
| 4251 | * load balancing. |
Randy Dunlap | cd96891 | 2012-06-08 13:18:33 -0700 | [diff] [blame] | 4252 | * @env: The load balancing environment. |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4253 | * @sds: Statistics of the sched_domain whose imbalance is to be calculated. |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4254 | */ |
Peter Zijlstra | bd939f4 | 2012-05-02 14:20:37 +0200 | [diff] [blame] | 4255 | static inline |
| 4256 | void fix_small_imbalance(struct lb_env *env, struct sd_lb_stats *sds) |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4257 | { |
| 4258 | unsigned long tmp, pwr_now = 0, pwr_move = 0; |
| 4259 | unsigned int imbn = 2; |
Suresh Siddha | dd5feea | 2010-02-23 16:13:52 -0800 | [diff] [blame] | 4260 | unsigned long scaled_busy_load_per_task; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4261 | |
| 4262 | if (sds->this_nr_running) { |
| 4263 | sds->this_load_per_task /= sds->this_nr_running; |
| 4264 | if (sds->busiest_load_per_task > |
| 4265 | sds->this_load_per_task) |
| 4266 | imbn = 1; |
Peter Zijlstra | bd939f4 | 2012-05-02 14:20:37 +0200 | [diff] [blame] | 4267 | } else { |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4268 | sds->this_load_per_task = |
Peter Zijlstra | bd939f4 | 2012-05-02 14:20:37 +0200 | [diff] [blame] | 4269 | cpu_avg_load_per_task(env->dst_cpu); |
| 4270 | } |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4271 | |
Suresh Siddha | dd5feea | 2010-02-23 16:13:52 -0800 | [diff] [blame] | 4272 | scaled_busy_load_per_task = sds->busiest_load_per_task |
Nikhil Rao | 1399fa7 | 2011-05-18 10:09:39 -0700 | [diff] [blame] | 4273 | * SCHED_POWER_SCALE; |
Peter Zijlstra | 9c3f75c | 2011-07-14 13:00:06 +0200 | [diff] [blame] | 4274 | scaled_busy_load_per_task /= sds->busiest->sgp->power; |
Suresh Siddha | dd5feea | 2010-02-23 16:13:52 -0800 | [diff] [blame] | 4275 | |
| 4276 | if (sds->max_load - sds->this_load + scaled_busy_load_per_task >= |
| 4277 | (scaled_busy_load_per_task * imbn)) { |
Peter Zijlstra | bd939f4 | 2012-05-02 14:20:37 +0200 | [diff] [blame] | 4278 | env->imbalance = sds->busiest_load_per_task; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4279 | return; |
| 4280 | } |
| 4281 | |
| 4282 | /* |
| 4283 | * OK, we don't have enough imbalance to justify moving tasks, |
| 4284 | * however we may be able to increase total CPU power used by |
| 4285 | * moving them. |
| 4286 | */ |
| 4287 | |
Peter Zijlstra | 9c3f75c | 2011-07-14 13:00:06 +0200 | [diff] [blame] | 4288 | pwr_now += sds->busiest->sgp->power * |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4289 | min(sds->busiest_load_per_task, sds->max_load); |
Peter Zijlstra | 9c3f75c | 2011-07-14 13:00:06 +0200 | [diff] [blame] | 4290 | pwr_now += sds->this->sgp->power * |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4291 | min(sds->this_load_per_task, sds->this_load); |
Nikhil Rao | 1399fa7 | 2011-05-18 10:09:39 -0700 | [diff] [blame] | 4292 | pwr_now /= SCHED_POWER_SCALE; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4293 | |
| 4294 | /* Amount of load we'd subtract */ |
Nikhil Rao | 1399fa7 | 2011-05-18 10:09:39 -0700 | [diff] [blame] | 4295 | tmp = (sds->busiest_load_per_task * SCHED_POWER_SCALE) / |
Peter Zijlstra | 9c3f75c | 2011-07-14 13:00:06 +0200 | [diff] [blame] | 4296 | sds->busiest->sgp->power; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4297 | if (sds->max_load > tmp) |
Peter Zijlstra | 9c3f75c | 2011-07-14 13:00:06 +0200 | [diff] [blame] | 4298 | pwr_move += sds->busiest->sgp->power * |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4299 | min(sds->busiest_load_per_task, sds->max_load - tmp); |
| 4300 | |
| 4301 | /* Amount of load we'd add */ |
Peter Zijlstra | 9c3f75c | 2011-07-14 13:00:06 +0200 | [diff] [blame] | 4302 | if (sds->max_load * sds->busiest->sgp->power < |
Nikhil Rao | 1399fa7 | 2011-05-18 10:09:39 -0700 | [diff] [blame] | 4303 | sds->busiest_load_per_task * SCHED_POWER_SCALE) |
Peter Zijlstra | 9c3f75c | 2011-07-14 13:00:06 +0200 | [diff] [blame] | 4304 | tmp = (sds->max_load * sds->busiest->sgp->power) / |
| 4305 | sds->this->sgp->power; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4306 | else |
Nikhil Rao | 1399fa7 | 2011-05-18 10:09:39 -0700 | [diff] [blame] | 4307 | tmp = (sds->busiest_load_per_task * SCHED_POWER_SCALE) / |
Peter Zijlstra | 9c3f75c | 2011-07-14 13:00:06 +0200 | [diff] [blame] | 4308 | sds->this->sgp->power; |
| 4309 | pwr_move += sds->this->sgp->power * |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4310 | min(sds->this_load_per_task, sds->this_load + tmp); |
Nikhil Rao | 1399fa7 | 2011-05-18 10:09:39 -0700 | [diff] [blame] | 4311 | pwr_move /= SCHED_POWER_SCALE; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4312 | |
| 4313 | /* Move if we gain throughput */ |
| 4314 | if (pwr_move > pwr_now) |
Peter Zijlstra | bd939f4 | 2012-05-02 14:20:37 +0200 | [diff] [blame] | 4315 | env->imbalance = sds->busiest_load_per_task; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4316 | } |
| 4317 | |
| 4318 | /** |
| 4319 | * calculate_imbalance - Calculate the amount of imbalance present within the |
| 4320 | * groups of a given sched_domain during load balance. |
Peter Zijlstra | bd939f4 | 2012-05-02 14:20:37 +0200 | [diff] [blame] | 4321 | * @env: load balance environment |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4322 | * @sds: statistics of the sched_domain whose imbalance is to be calculated. |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4323 | */ |
Peter Zijlstra | bd939f4 | 2012-05-02 14:20:37 +0200 | [diff] [blame] | 4324 | static inline void calculate_imbalance(struct lb_env *env, struct sd_lb_stats *sds) |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4325 | { |
Suresh Siddha | dd5feea | 2010-02-23 16:13:52 -0800 | [diff] [blame] | 4326 | unsigned long max_pull, load_above_capacity = ~0UL; |
| 4327 | |
| 4328 | sds->busiest_load_per_task /= sds->busiest_nr_running; |
| 4329 | if (sds->group_imb) { |
| 4330 | sds->busiest_load_per_task = |
| 4331 | min(sds->busiest_load_per_task, sds->avg_load); |
| 4332 | } |
| 4333 | |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4334 | /* |
| 4335 | * In the presence of smp nice balancing, certain scenarios can have |
| 4336 | * max load less than avg load(as we skip the groups at or below |
| 4337 | * its cpu_power, while calculating max_load..) |
| 4338 | */ |
| 4339 | if (sds->max_load < sds->avg_load) { |
Peter Zijlstra | bd939f4 | 2012-05-02 14:20:37 +0200 | [diff] [blame] | 4340 | env->imbalance = 0; |
| 4341 | return fix_small_imbalance(env, sds); |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4342 | } |
| 4343 | |
Suresh Siddha | dd5feea | 2010-02-23 16:13:52 -0800 | [diff] [blame] | 4344 | if (!sds->group_imb) { |
| 4345 | /* |
| 4346 | * Don't want to pull so many tasks that a group would go idle. |
| 4347 | */ |
| 4348 | load_above_capacity = (sds->busiest_nr_running - |
| 4349 | sds->busiest_group_capacity); |
| 4350 | |
Nikhil Rao | 1399fa7 | 2011-05-18 10:09:39 -0700 | [diff] [blame] | 4351 | load_above_capacity *= (SCHED_LOAD_SCALE * SCHED_POWER_SCALE); |
Suresh Siddha | dd5feea | 2010-02-23 16:13:52 -0800 | [diff] [blame] | 4352 | |
Peter Zijlstra | 9c3f75c | 2011-07-14 13:00:06 +0200 | [diff] [blame] | 4353 | load_above_capacity /= sds->busiest->sgp->power; |
Suresh Siddha | dd5feea | 2010-02-23 16:13:52 -0800 | [diff] [blame] | 4354 | } |
| 4355 | |
| 4356 | /* |
| 4357 | * We're trying to get all the cpus to the average_load, so we don't |
| 4358 | * want to push ourselves above the average load, nor do we wish to |
| 4359 | * reduce the max loaded cpu below the average load. At the same time, |
| 4360 | * we also don't want to reduce the group load below the group capacity |
| 4361 | * (so that we can implement power-savings policies etc). Thus we look |
| 4362 | * for the minimum possible imbalance. |
| 4363 | * Be careful of negative numbers as they'll appear as very large values |
| 4364 | * with unsigned longs. |
| 4365 | */ |
| 4366 | max_pull = min(sds->max_load - sds->avg_load, load_above_capacity); |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4367 | |
| 4368 | /* How much load to actually move to equalise the imbalance */ |
Peter Zijlstra | bd939f4 | 2012-05-02 14:20:37 +0200 | [diff] [blame] | 4369 | env->imbalance = min(max_pull * sds->busiest->sgp->power, |
Peter Zijlstra | 9c3f75c | 2011-07-14 13:00:06 +0200 | [diff] [blame] | 4370 | (sds->avg_load - sds->this_load) * sds->this->sgp->power) |
Nikhil Rao | 1399fa7 | 2011-05-18 10:09:39 -0700 | [diff] [blame] | 4371 | / SCHED_POWER_SCALE; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4372 | |
| 4373 | /* |
| 4374 | * if *imbalance is less than the average load per runnable task |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 4375 | * there is no guarantee that any tasks will be moved so we'll have |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4376 | * a think about bumping its value to force at least one task to be |
| 4377 | * moved |
| 4378 | */ |
Peter Zijlstra | bd939f4 | 2012-05-02 14:20:37 +0200 | [diff] [blame] | 4379 | if (env->imbalance < sds->busiest_load_per_task) |
| 4380 | return fix_small_imbalance(env, sds); |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4381 | |
| 4382 | } |
Nikhil Rao | fab4762 | 2010-10-15 13:12:29 -0700 | [diff] [blame] | 4383 | |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4384 | /******* find_busiest_group() helpers end here *********************/ |
| 4385 | |
| 4386 | /** |
| 4387 | * find_busiest_group - Returns the busiest group within the sched_domain |
| 4388 | * if there is an imbalance. If there isn't an imbalance, and |
| 4389 | * the user has opted for power-savings, it returns a group whose |
| 4390 | * CPUs can be put to idle by rebalancing those tasks elsewhere, if |
| 4391 | * such a group exists. |
| 4392 | * |
| 4393 | * Also calculates the amount of weighted load which should be moved |
| 4394 | * to restore balance. |
| 4395 | * |
Randy Dunlap | cd96891 | 2012-06-08 13:18:33 -0700 | [diff] [blame] | 4396 | * @env: The load balancing environment. |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4397 | * @balance: Pointer to a variable indicating if this_cpu |
| 4398 | * is the appropriate cpu to perform load balancing at this_level. |
| 4399 | * |
| 4400 | * Returns: - the busiest group if imbalance exists. |
| 4401 | * - If no imbalance and user has opted for power-savings balance, |
| 4402 | * return the least loaded group whose CPUs can be |
| 4403 | * put to idle by rebalancing its tasks onto our group. |
| 4404 | */ |
| 4405 | static struct sched_group * |
Michael Wang | b940313 | 2012-07-12 16:10:13 +0800 | [diff] [blame] | 4406 | find_busiest_group(struct lb_env *env, int *balance) |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4407 | { |
| 4408 | struct sd_lb_stats sds; |
| 4409 | |
| 4410 | memset(&sds, 0, sizeof(sds)); |
| 4411 | |
| 4412 | /* |
| 4413 | * Compute the various statistics relavent for load balancing at |
| 4414 | * this level. |
| 4415 | */ |
Michael Wang | b940313 | 2012-07-12 16:10:13 +0800 | [diff] [blame] | 4416 | update_sd_lb_stats(env, balance, &sds); |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4417 | |
Peter Zijlstra | cc57aa8 | 2011-02-21 18:55:32 +0100 | [diff] [blame] | 4418 | /* |
| 4419 | * this_cpu is not the appropriate cpu to perform load balancing at |
| 4420 | * this level. |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4421 | */ |
Peter Zijlstra | 8f190fb | 2009-12-24 14:18:21 +0100 | [diff] [blame] | 4422 | if (!(*balance)) |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4423 | goto ret; |
| 4424 | |
Peter Zijlstra | bd939f4 | 2012-05-02 14:20:37 +0200 | [diff] [blame] | 4425 | if ((env->idle == CPU_IDLE || env->idle == CPU_NEWLY_IDLE) && |
| 4426 | check_asym_packing(env, &sds)) |
Michael Neuling | 532cb4c | 2010-06-08 14:57:02 +1000 | [diff] [blame] | 4427 | return sds.busiest; |
| 4428 | |
Peter Zijlstra | cc57aa8 | 2011-02-21 18:55:32 +0100 | [diff] [blame] | 4429 | /* There is no busy sibling group to pull tasks from */ |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4430 | if (!sds.busiest || sds.busiest_nr_running == 0) |
| 4431 | goto out_balanced; |
| 4432 | |
Nikhil Rao | 1399fa7 | 2011-05-18 10:09:39 -0700 | [diff] [blame] | 4433 | sds.avg_load = (SCHED_POWER_SCALE * sds.total_load) / sds.total_pwr; |
Ken Chen | b0432d8 | 2011-04-07 17:23:22 -0700 | [diff] [blame] | 4434 | |
Peter Zijlstra | 866ab43 | 2011-02-21 18:56:47 +0100 | [diff] [blame] | 4435 | /* |
| 4436 | * If the busiest group is imbalanced the below checks don't |
| 4437 | * work because they assumes all things are equal, which typically |
| 4438 | * isn't true due to cpus_allowed constraints and the like. |
| 4439 | */ |
| 4440 | if (sds.group_imb) |
| 4441 | goto force_balance; |
| 4442 | |
Peter Zijlstra | cc57aa8 | 2011-02-21 18:55:32 +0100 | [diff] [blame] | 4443 | /* SD_BALANCE_NEWIDLE trumps SMP nice when underutilized */ |
Peter Zijlstra | bd939f4 | 2012-05-02 14:20:37 +0200 | [diff] [blame] | 4444 | if (env->idle == CPU_NEWLY_IDLE && sds.this_has_capacity && |
Nikhil Rao | fab4762 | 2010-10-15 13:12:29 -0700 | [diff] [blame] | 4445 | !sds.busiest_has_capacity) |
| 4446 | goto force_balance; |
| 4447 | |
Peter Zijlstra | cc57aa8 | 2011-02-21 18:55:32 +0100 | [diff] [blame] | 4448 | /* |
| 4449 | * If the local group is more busy than the selected busiest group |
| 4450 | * don't try and pull any tasks. |
| 4451 | */ |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4452 | if (sds.this_load >= sds.max_load) |
| 4453 | goto out_balanced; |
| 4454 | |
Peter Zijlstra | cc57aa8 | 2011-02-21 18:55:32 +0100 | [diff] [blame] | 4455 | /* |
| 4456 | * Don't pull any tasks if this group is already above the domain |
| 4457 | * average load. |
| 4458 | */ |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4459 | if (sds.this_load >= sds.avg_load) |
| 4460 | goto out_balanced; |
| 4461 | |
Peter Zijlstra | bd939f4 | 2012-05-02 14:20:37 +0200 | [diff] [blame] | 4462 | if (env->idle == CPU_IDLE) { |
Suresh Siddha | aae6d3d | 2010-09-17 15:02:32 -0700 | [diff] [blame] | 4463 | /* |
| 4464 | * This cpu is idle. If the busiest group load doesn't |
| 4465 | * have more tasks than the number of available cpu's and |
| 4466 | * there is no imbalance between this and busiest group |
| 4467 | * wrt to idle cpu's, it is balanced. |
| 4468 | */ |
Peter Zijlstra | c186faf | 2011-02-21 18:52:53 +0100 | [diff] [blame] | 4469 | if ((sds.this_idle_cpus <= sds.busiest_idle_cpus + 1) && |
Suresh Siddha | aae6d3d | 2010-09-17 15:02:32 -0700 | [diff] [blame] | 4470 | sds.busiest_nr_running <= sds.busiest_group_weight) |
| 4471 | goto out_balanced; |
Peter Zijlstra | c186faf | 2011-02-21 18:52:53 +0100 | [diff] [blame] | 4472 | } else { |
| 4473 | /* |
| 4474 | * In the CPU_NEWLY_IDLE, CPU_NOT_IDLE cases, use |
| 4475 | * imbalance_pct to be conservative. |
| 4476 | */ |
Peter Zijlstra | bd939f4 | 2012-05-02 14:20:37 +0200 | [diff] [blame] | 4477 | if (100 * sds.max_load <= env->sd->imbalance_pct * sds.this_load) |
Peter Zijlstra | c186faf | 2011-02-21 18:52:53 +0100 | [diff] [blame] | 4478 | goto out_balanced; |
Suresh Siddha | aae6d3d | 2010-09-17 15:02:32 -0700 | [diff] [blame] | 4479 | } |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4480 | |
Nikhil Rao | fab4762 | 2010-10-15 13:12:29 -0700 | [diff] [blame] | 4481 | force_balance: |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4482 | /* Looks like there is an imbalance. Compute it */ |
Peter Zijlstra | bd939f4 | 2012-05-02 14:20:37 +0200 | [diff] [blame] | 4483 | calculate_imbalance(env, &sds); |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4484 | return sds.busiest; |
| 4485 | |
| 4486 | out_balanced: |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4487 | ret: |
Peter Zijlstra | bd939f4 | 2012-05-02 14:20:37 +0200 | [diff] [blame] | 4488 | env->imbalance = 0; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4489 | return NULL; |
| 4490 | } |
| 4491 | |
| 4492 | /* |
| 4493 | * find_busiest_queue - find the busiest runqueue among the cpus in group. |
| 4494 | */ |
Peter Zijlstra | bd939f4 | 2012-05-02 14:20:37 +0200 | [diff] [blame] | 4495 | static struct rq *find_busiest_queue(struct lb_env *env, |
Michael Wang | b940313 | 2012-07-12 16:10:13 +0800 | [diff] [blame] | 4496 | struct sched_group *group) |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4497 | { |
| 4498 | struct rq *busiest = NULL, *rq; |
| 4499 | unsigned long max_load = 0; |
| 4500 | int i; |
| 4501 | |
| 4502 | for_each_cpu(i, sched_group_cpus(group)) { |
| 4503 | unsigned long power = power_of(i); |
Nikhil Rao | 1399fa7 | 2011-05-18 10:09:39 -0700 | [diff] [blame] | 4504 | unsigned long capacity = DIV_ROUND_CLOSEST(power, |
| 4505 | SCHED_POWER_SCALE); |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4506 | unsigned long wl; |
| 4507 | |
Srivatsa Vaddagiri | 9d5efe0 | 2010-06-08 14:57:02 +1000 | [diff] [blame] | 4508 | if (!capacity) |
Peter Zijlstra | bd939f4 | 2012-05-02 14:20:37 +0200 | [diff] [blame] | 4509 | capacity = fix_small_capacity(env->sd, group); |
Srivatsa Vaddagiri | 9d5efe0 | 2010-06-08 14:57:02 +1000 | [diff] [blame] | 4510 | |
Michael Wang | b940313 | 2012-07-12 16:10:13 +0800 | [diff] [blame] | 4511 | if (!cpumask_test_cpu(i, env->cpus)) |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4512 | continue; |
| 4513 | |
| 4514 | rq = cpu_rq(i); |
Thomas Gleixner | 6e40f5b | 2010-02-16 16:48:56 +0100 | [diff] [blame] | 4515 | wl = weighted_cpuload(i); |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4516 | |
Thomas Gleixner | 6e40f5b | 2010-02-16 16:48:56 +0100 | [diff] [blame] | 4517 | /* |
| 4518 | * When comparing with imbalance, use weighted_cpuload() |
| 4519 | * which is not scaled with the cpu power. |
| 4520 | */ |
Peter Zijlstra | bd939f4 | 2012-05-02 14:20:37 +0200 | [diff] [blame] | 4521 | if (capacity && rq->nr_running == 1 && wl > env->imbalance) |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4522 | continue; |
| 4523 | |
Thomas Gleixner | 6e40f5b | 2010-02-16 16:48:56 +0100 | [diff] [blame] | 4524 | /* |
| 4525 | * For the load comparisons with the other cpu's, consider |
| 4526 | * the weighted_cpuload() scaled with the cpu power, so that |
| 4527 | * the load can be moved away from the cpu that is potentially |
| 4528 | * running at a lower capacity. |
| 4529 | */ |
Nikhil Rao | 1399fa7 | 2011-05-18 10:09:39 -0700 | [diff] [blame] | 4530 | wl = (wl * SCHED_POWER_SCALE) / power; |
Thomas Gleixner | 6e40f5b | 2010-02-16 16:48:56 +0100 | [diff] [blame] | 4531 | |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4532 | if (wl > max_load) { |
| 4533 | max_load = wl; |
| 4534 | busiest = rq; |
| 4535 | } |
| 4536 | } |
| 4537 | |
| 4538 | return busiest; |
| 4539 | } |
| 4540 | |
| 4541 | /* |
| 4542 | * Max backoff if we encounter pinned tasks. Pretty arbitrary value, but |
| 4543 | * so long as it is large enough. |
| 4544 | */ |
| 4545 | #define MAX_PINNED_INTERVAL 512 |
| 4546 | |
| 4547 | /* Working cpumask for load_balance and load_balance_newidle. */ |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 4548 | DEFINE_PER_CPU(cpumask_var_t, load_balance_tmpmask); |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4549 | |
Peter Zijlstra | bd939f4 | 2012-05-02 14:20:37 +0200 | [diff] [blame] | 4550 | static int need_active_balance(struct lb_env *env) |
Peter Zijlstra | 1af3ed3 | 2009-12-23 15:10:31 +0100 | [diff] [blame] | 4551 | { |
Peter Zijlstra | bd939f4 | 2012-05-02 14:20:37 +0200 | [diff] [blame] | 4552 | struct sched_domain *sd = env->sd; |
| 4553 | |
| 4554 | if (env->idle == CPU_NEWLY_IDLE) { |
Michael Neuling | 532cb4c | 2010-06-08 14:57:02 +1000 | [diff] [blame] | 4555 | |
| 4556 | /* |
| 4557 | * ASYM_PACKING needs to force migrate tasks from busy but |
| 4558 | * higher numbered CPUs in order to pack all tasks in the |
| 4559 | * lowest numbered CPUs. |
| 4560 | */ |
Peter Zijlstra | bd939f4 | 2012-05-02 14:20:37 +0200 | [diff] [blame] | 4561 | if ((sd->flags & SD_ASYM_PACKING) && env->src_cpu > env->dst_cpu) |
Michael Neuling | 532cb4c | 2010-06-08 14:57:02 +1000 | [diff] [blame] | 4562 | return 1; |
Peter Zijlstra | 1af3ed3 | 2009-12-23 15:10:31 +0100 | [diff] [blame] | 4563 | } |
| 4564 | |
| 4565 | return unlikely(sd->nr_balance_failed > sd->cache_nice_tries+2); |
| 4566 | } |
| 4567 | |
Tejun Heo | 969c792 | 2010-05-06 18:49:21 +0200 | [diff] [blame] | 4568 | static int active_load_balance_cpu_stop(void *data); |
| 4569 | |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4570 | /* |
| 4571 | * Check this_cpu to ensure it is balanced within domain. Attempt to move |
| 4572 | * tasks if there is an imbalance. |
| 4573 | */ |
| 4574 | static int load_balance(int this_cpu, struct rq *this_rq, |
| 4575 | struct sched_domain *sd, enum cpu_idle_type idle, |
| 4576 | int *balance) |
| 4577 | { |
Srivatsa Vaddagiri | 88b8dac | 2012-06-19 17:43:15 +0530 | [diff] [blame] | 4578 | int ld_moved, cur_ld_moved, active_balance = 0; |
| 4579 | int lb_iterations, max_lb_iterations; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4580 | struct sched_group *group; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4581 | struct rq *busiest; |
| 4582 | unsigned long flags; |
| 4583 | struct cpumask *cpus = __get_cpu_var(load_balance_tmpmask); |
| 4584 | |
Peter Zijlstra | 8e45cb5 | 2012-02-22 12:47:19 +0100 | [diff] [blame] | 4585 | struct lb_env env = { |
| 4586 | .sd = sd, |
Peter Zijlstra | ddcdf6e | 2012-02-22 19:27:40 +0100 | [diff] [blame] | 4587 | .dst_cpu = this_cpu, |
| 4588 | .dst_rq = this_rq, |
Srivatsa Vaddagiri | 88b8dac | 2012-06-19 17:43:15 +0530 | [diff] [blame] | 4589 | .dst_grpmask = sched_group_cpus(sd->groups), |
Peter Zijlstra | 8e45cb5 | 2012-02-22 12:47:19 +0100 | [diff] [blame] | 4590 | .idle = idle, |
Peter Zijlstra | eb95308 | 2012-04-17 13:38:40 +0200 | [diff] [blame] | 4591 | .loop_break = sched_nr_migrate_break, |
Michael Wang | b940313 | 2012-07-12 16:10:13 +0800 | [diff] [blame] | 4592 | .cpus = cpus, |
Peter Zijlstra | 8e45cb5 | 2012-02-22 12:47:19 +0100 | [diff] [blame] | 4593 | }; |
| 4594 | |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4595 | cpumask_copy(cpus, cpu_active_mask); |
Srivatsa Vaddagiri | 88b8dac | 2012-06-19 17:43:15 +0530 | [diff] [blame] | 4596 | max_lb_iterations = cpumask_weight(env.dst_grpmask); |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4597 | |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4598 | schedstat_inc(sd, lb_count[idle]); |
| 4599 | |
| 4600 | redo: |
Michael Wang | b940313 | 2012-07-12 16:10:13 +0800 | [diff] [blame] | 4601 | group = find_busiest_group(&env, balance); |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4602 | |
| 4603 | if (*balance == 0) |
| 4604 | goto out_balanced; |
| 4605 | |
| 4606 | if (!group) { |
| 4607 | schedstat_inc(sd, lb_nobusyg[idle]); |
| 4608 | goto out_balanced; |
| 4609 | } |
| 4610 | |
Michael Wang | b940313 | 2012-07-12 16:10:13 +0800 | [diff] [blame] | 4611 | busiest = find_busiest_queue(&env, group); |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4612 | if (!busiest) { |
| 4613 | schedstat_inc(sd, lb_nobusyq[idle]); |
| 4614 | goto out_balanced; |
| 4615 | } |
| 4616 | |
Michael Wang | 78feefc | 2012-08-06 16:41:59 +0800 | [diff] [blame] | 4617 | BUG_ON(busiest == env.dst_rq); |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4618 | |
Peter Zijlstra | bd939f4 | 2012-05-02 14:20:37 +0200 | [diff] [blame] | 4619 | schedstat_add(sd, lb_imbalance[idle], env.imbalance); |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4620 | |
| 4621 | ld_moved = 0; |
Srivatsa Vaddagiri | 88b8dac | 2012-06-19 17:43:15 +0530 | [diff] [blame] | 4622 | lb_iterations = 1; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4623 | if (busiest->nr_running > 1) { |
| 4624 | /* |
| 4625 | * Attempt to move tasks. If find_busiest_group has found |
| 4626 | * an imbalance but busiest->nr_running <= 1, the group is |
| 4627 | * still unbalanced. ld_moved simply stays zero, so it is |
| 4628 | * correctly treated as an imbalance. |
| 4629 | */ |
Peter Zijlstra | 8e45cb5 | 2012-02-22 12:47:19 +0100 | [diff] [blame] | 4630 | env.flags |= LBF_ALL_PINNED; |
Peter Zijlstra | c82513e | 2012-04-26 13:12:27 +0200 | [diff] [blame] | 4631 | env.src_cpu = busiest->cpu; |
| 4632 | env.src_rq = busiest; |
| 4633 | env.loop_max = min(sysctl_sched_nr_migrate, busiest->nr_running); |
Peter Zijlstra | 8e45cb5 | 2012-02-22 12:47:19 +0100 | [diff] [blame] | 4634 | |
Peter Zijlstra | a35b646 | 2012-08-08 21:46:40 +0200 | [diff] [blame] | 4635 | update_h_load(env.src_cpu); |
Peter Zijlstra | 5d6523e | 2012-03-10 00:07:36 +0100 | [diff] [blame] | 4636 | more_balance: |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4637 | local_irq_save(flags); |
Michael Wang | 78feefc | 2012-08-06 16:41:59 +0800 | [diff] [blame] | 4638 | double_rq_lock(env.dst_rq, busiest); |
Srivatsa Vaddagiri | 88b8dac | 2012-06-19 17:43:15 +0530 | [diff] [blame] | 4639 | |
| 4640 | /* |
| 4641 | * cur_ld_moved - load moved in current iteration |
| 4642 | * ld_moved - cumulative load moved across iterations |
| 4643 | */ |
| 4644 | cur_ld_moved = move_tasks(&env); |
| 4645 | ld_moved += cur_ld_moved; |
Michael Wang | 78feefc | 2012-08-06 16:41:59 +0800 | [diff] [blame] | 4646 | double_rq_unlock(env.dst_rq, busiest); |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4647 | local_irq_restore(flags); |
| 4648 | |
Peter Zijlstra | 5d6523e | 2012-03-10 00:07:36 +0100 | [diff] [blame] | 4649 | if (env.flags & LBF_NEED_BREAK) { |
| 4650 | env.flags &= ~LBF_NEED_BREAK; |
| 4651 | goto more_balance; |
| 4652 | } |
| 4653 | |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4654 | /* |
| 4655 | * some other cpu did the load balance for us. |
| 4656 | */ |
Srivatsa Vaddagiri | 88b8dac | 2012-06-19 17:43:15 +0530 | [diff] [blame] | 4657 | if (cur_ld_moved && env.dst_cpu != smp_processor_id()) |
| 4658 | resched_cpu(env.dst_cpu); |
| 4659 | |
| 4660 | /* |
| 4661 | * Revisit (affine) tasks on src_cpu that couldn't be moved to |
| 4662 | * us and move them to an alternate dst_cpu in our sched_group |
| 4663 | * where they can run. The upper limit on how many times we |
| 4664 | * iterate on same src_cpu is dependent on number of cpus in our |
| 4665 | * sched_group. |
| 4666 | * |
| 4667 | * This changes load balance semantics a bit on who can move |
| 4668 | * load to a given_cpu. In addition to the given_cpu itself |
| 4669 | * (or a ilb_cpu acting on its behalf where given_cpu is |
| 4670 | * nohz-idle), we now have balance_cpu in a position to move |
| 4671 | * load to given_cpu. In rare situations, this may cause |
| 4672 | * conflicts (balance_cpu and given_cpu/ilb_cpu deciding |
| 4673 | * _independently_ and at _same_ time to move some load to |
| 4674 | * given_cpu) causing exceess load to be moved to given_cpu. |
| 4675 | * This however should not happen so much in practice and |
| 4676 | * moreover subsequent load balance cycles should correct the |
| 4677 | * excess load moved. |
| 4678 | */ |
| 4679 | if ((env.flags & LBF_SOME_PINNED) && env.imbalance > 0 && |
| 4680 | lb_iterations++ < max_lb_iterations) { |
| 4681 | |
Michael Wang | 78feefc | 2012-08-06 16:41:59 +0800 | [diff] [blame] | 4682 | env.dst_rq = cpu_rq(env.new_dst_cpu); |
Srivatsa Vaddagiri | 88b8dac | 2012-06-19 17:43:15 +0530 | [diff] [blame] | 4683 | env.dst_cpu = env.new_dst_cpu; |
| 4684 | env.flags &= ~LBF_SOME_PINNED; |
| 4685 | env.loop = 0; |
| 4686 | env.loop_break = sched_nr_migrate_break; |
| 4687 | /* |
| 4688 | * Go back to "more_balance" rather than "redo" since we |
| 4689 | * need to continue with same src_cpu. |
| 4690 | */ |
| 4691 | goto more_balance; |
| 4692 | } |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4693 | |
| 4694 | /* All tasks on this runqueue were pinned by CPU affinity */ |
Peter Zijlstra | 8e45cb5 | 2012-02-22 12:47:19 +0100 | [diff] [blame] | 4695 | if (unlikely(env.flags & LBF_ALL_PINNED)) { |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4696 | cpumask_clear_cpu(cpu_of(busiest), cpus); |
Prashanth Nageshappa | bbf18b1 | 2012-06-19 17:52:07 +0530 | [diff] [blame] | 4697 | if (!cpumask_empty(cpus)) { |
| 4698 | env.loop = 0; |
| 4699 | env.loop_break = sched_nr_migrate_break; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4700 | goto redo; |
Prashanth Nageshappa | bbf18b1 | 2012-06-19 17:52:07 +0530 | [diff] [blame] | 4701 | } |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4702 | goto out_balanced; |
| 4703 | } |
| 4704 | } |
| 4705 | |
| 4706 | if (!ld_moved) { |
| 4707 | schedstat_inc(sd, lb_failed[idle]); |
Venkatesh Pallipadi | 58b26c4 | 2010-09-10 18:19:17 -0700 | [diff] [blame] | 4708 | /* |
| 4709 | * Increment the failure counter only on periodic balance. |
| 4710 | * We do not want newidle balance, which can be very |
| 4711 | * frequent, pollute the failure counter causing |
| 4712 | * excessive cache_hot migrations and active balances. |
| 4713 | */ |
| 4714 | if (idle != CPU_NEWLY_IDLE) |
| 4715 | sd->nr_balance_failed++; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4716 | |
Peter Zijlstra | bd939f4 | 2012-05-02 14:20:37 +0200 | [diff] [blame] | 4717 | if (need_active_balance(&env)) { |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4718 | raw_spin_lock_irqsave(&busiest->lock, flags); |
| 4719 | |
Tejun Heo | 969c792 | 2010-05-06 18:49:21 +0200 | [diff] [blame] | 4720 | /* don't kick the active_load_balance_cpu_stop, |
| 4721 | * if the curr task on busiest cpu can't be |
| 4722 | * moved to this_cpu |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4723 | */ |
| 4724 | if (!cpumask_test_cpu(this_cpu, |
Peter Zijlstra | fa17b50 | 2011-06-16 12:23:22 +0200 | [diff] [blame] | 4725 | tsk_cpus_allowed(busiest->curr))) { |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4726 | raw_spin_unlock_irqrestore(&busiest->lock, |
| 4727 | flags); |
Peter Zijlstra | 8e45cb5 | 2012-02-22 12:47:19 +0100 | [diff] [blame] | 4728 | env.flags |= LBF_ALL_PINNED; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4729 | goto out_one_pinned; |
| 4730 | } |
| 4731 | |
Tejun Heo | 969c792 | 2010-05-06 18:49:21 +0200 | [diff] [blame] | 4732 | /* |
| 4733 | * ->active_balance synchronizes accesses to |
| 4734 | * ->active_balance_work. Once set, it's cleared |
| 4735 | * only after active load balance is finished. |
| 4736 | */ |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4737 | if (!busiest->active_balance) { |
| 4738 | busiest->active_balance = 1; |
| 4739 | busiest->push_cpu = this_cpu; |
| 4740 | active_balance = 1; |
| 4741 | } |
| 4742 | raw_spin_unlock_irqrestore(&busiest->lock, flags); |
Tejun Heo | 969c792 | 2010-05-06 18:49:21 +0200 | [diff] [blame] | 4743 | |
Peter Zijlstra | bd939f4 | 2012-05-02 14:20:37 +0200 | [diff] [blame] | 4744 | if (active_balance) { |
Tejun Heo | 969c792 | 2010-05-06 18:49:21 +0200 | [diff] [blame] | 4745 | stop_one_cpu_nowait(cpu_of(busiest), |
| 4746 | active_load_balance_cpu_stop, busiest, |
| 4747 | &busiest->active_balance_work); |
Peter Zijlstra | bd939f4 | 2012-05-02 14:20:37 +0200 | [diff] [blame] | 4748 | } |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4749 | |
| 4750 | /* |
| 4751 | * We've kicked active balancing, reset the failure |
| 4752 | * counter. |
| 4753 | */ |
| 4754 | sd->nr_balance_failed = sd->cache_nice_tries+1; |
| 4755 | } |
| 4756 | } else |
| 4757 | sd->nr_balance_failed = 0; |
| 4758 | |
| 4759 | if (likely(!active_balance)) { |
| 4760 | /* We were unbalanced, so reset the balancing interval */ |
| 4761 | sd->balance_interval = sd->min_interval; |
| 4762 | } else { |
| 4763 | /* |
| 4764 | * If we've begun active balancing, start to back off. This |
| 4765 | * case may not be covered by the all_pinned logic if there |
| 4766 | * is only 1 task on the busy runqueue (because we don't call |
| 4767 | * move_tasks). |
| 4768 | */ |
| 4769 | if (sd->balance_interval < sd->max_interval) |
| 4770 | sd->balance_interval *= 2; |
| 4771 | } |
| 4772 | |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4773 | goto out; |
| 4774 | |
| 4775 | out_balanced: |
| 4776 | schedstat_inc(sd, lb_balanced[idle]); |
| 4777 | |
| 4778 | sd->nr_balance_failed = 0; |
| 4779 | |
| 4780 | out_one_pinned: |
| 4781 | /* tune up the balancing interval */ |
Peter Zijlstra | 8e45cb5 | 2012-02-22 12:47:19 +0100 | [diff] [blame] | 4782 | if (((env.flags & LBF_ALL_PINNED) && |
Peter Zijlstra | 5b54b56 | 2011-09-22 15:23:13 +0200 | [diff] [blame] | 4783 | sd->balance_interval < MAX_PINNED_INTERVAL) || |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4784 | (sd->balance_interval < sd->max_interval)) |
| 4785 | sd->balance_interval *= 2; |
| 4786 | |
Venkatesh Pallipadi | 46e49b3 | 2011-02-14 14:38:50 -0800 | [diff] [blame] | 4787 | ld_moved = 0; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4788 | out: |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4789 | return ld_moved; |
| 4790 | } |
| 4791 | |
| 4792 | /* |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4793 | * idle_balance is called by schedule() if this_cpu is about to become |
| 4794 | * idle. Attempts to pull tasks from other CPUs. |
| 4795 | */ |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 4796 | void idle_balance(int this_cpu, struct rq *this_rq) |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4797 | { |
| 4798 | struct sched_domain *sd; |
| 4799 | int pulled_task = 0; |
| 4800 | unsigned long next_balance = jiffies + HZ; |
| 4801 | |
| 4802 | this_rq->idle_stamp = this_rq->clock; |
| 4803 | |
| 4804 | if (this_rq->avg_idle < sysctl_sched_migration_cost) |
| 4805 | return; |
| 4806 | |
Ben Segall | 18bf280 | 2012-10-04 12:51:20 +0200 | [diff] [blame] | 4807 | update_rq_runnable_avg(this_rq, 1); |
| 4808 | |
Peter Zijlstra | f492e12 | 2009-12-23 15:29:42 +0100 | [diff] [blame] | 4809 | /* |
| 4810 | * Drop the rq->lock, but keep IRQ/preempt disabled. |
| 4811 | */ |
| 4812 | raw_spin_unlock(&this_rq->lock); |
| 4813 | |
Paul Turner | c66eaf6 | 2010-11-15 15:47:07 -0800 | [diff] [blame] | 4814 | update_shares(this_cpu); |
Peter Zijlstra | dce840a | 2011-04-07 14:09:50 +0200 | [diff] [blame] | 4815 | rcu_read_lock(); |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4816 | for_each_domain(this_cpu, sd) { |
| 4817 | unsigned long interval; |
Peter Zijlstra | f492e12 | 2009-12-23 15:29:42 +0100 | [diff] [blame] | 4818 | int balance = 1; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4819 | |
| 4820 | if (!(sd->flags & SD_LOAD_BALANCE)) |
| 4821 | continue; |
| 4822 | |
Peter Zijlstra | f492e12 | 2009-12-23 15:29:42 +0100 | [diff] [blame] | 4823 | if (sd->flags & SD_BALANCE_NEWIDLE) { |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4824 | /* If we've pulled tasks over stop searching: */ |
Peter Zijlstra | f492e12 | 2009-12-23 15:29:42 +0100 | [diff] [blame] | 4825 | pulled_task = load_balance(this_cpu, this_rq, |
| 4826 | sd, CPU_NEWLY_IDLE, &balance); |
| 4827 | } |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4828 | |
| 4829 | interval = msecs_to_jiffies(sd->balance_interval); |
| 4830 | if (time_after(next_balance, sd->last_balance + interval)) |
| 4831 | next_balance = sd->last_balance + interval; |
Nikhil Rao | d5ad140 | 2010-11-17 11:42:04 -0800 | [diff] [blame] | 4832 | if (pulled_task) { |
| 4833 | this_rq->idle_stamp = 0; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4834 | break; |
Nikhil Rao | d5ad140 | 2010-11-17 11:42:04 -0800 | [diff] [blame] | 4835 | } |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4836 | } |
Peter Zijlstra | dce840a | 2011-04-07 14:09:50 +0200 | [diff] [blame] | 4837 | rcu_read_unlock(); |
Peter Zijlstra | f492e12 | 2009-12-23 15:29:42 +0100 | [diff] [blame] | 4838 | |
| 4839 | raw_spin_lock(&this_rq->lock); |
| 4840 | |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4841 | if (pulled_task || time_after(jiffies, this_rq->next_balance)) { |
| 4842 | /* |
| 4843 | * We are going idle. next_balance may be set based on |
| 4844 | * a busy processor. So reset next_balance. |
| 4845 | */ |
| 4846 | this_rq->next_balance = next_balance; |
| 4847 | } |
| 4848 | } |
| 4849 | |
| 4850 | /* |
Tejun Heo | 969c792 | 2010-05-06 18:49:21 +0200 | [diff] [blame] | 4851 | * active_load_balance_cpu_stop is run by cpu stopper. It pushes |
| 4852 | * running tasks off the busiest CPU onto idle CPUs. It requires at |
| 4853 | * least 1 task to be running on each physical CPU where possible, and |
| 4854 | * avoids physical / logical imbalances. |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4855 | */ |
Tejun Heo | 969c792 | 2010-05-06 18:49:21 +0200 | [diff] [blame] | 4856 | static int active_load_balance_cpu_stop(void *data) |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4857 | { |
Tejun Heo | 969c792 | 2010-05-06 18:49:21 +0200 | [diff] [blame] | 4858 | struct rq *busiest_rq = data; |
| 4859 | int busiest_cpu = cpu_of(busiest_rq); |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4860 | int target_cpu = busiest_rq->push_cpu; |
Tejun Heo | 969c792 | 2010-05-06 18:49:21 +0200 | [diff] [blame] | 4861 | struct rq *target_rq = cpu_rq(target_cpu); |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4862 | struct sched_domain *sd; |
Tejun Heo | 969c792 | 2010-05-06 18:49:21 +0200 | [diff] [blame] | 4863 | |
| 4864 | raw_spin_lock_irq(&busiest_rq->lock); |
| 4865 | |
| 4866 | /* make sure the requested cpu hasn't gone down in the meantime */ |
| 4867 | if (unlikely(busiest_cpu != smp_processor_id() || |
| 4868 | !busiest_rq->active_balance)) |
| 4869 | goto out_unlock; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4870 | |
| 4871 | /* Is there any task to move? */ |
| 4872 | if (busiest_rq->nr_running <= 1) |
Tejun Heo | 969c792 | 2010-05-06 18:49:21 +0200 | [diff] [blame] | 4873 | goto out_unlock; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4874 | |
| 4875 | /* |
| 4876 | * This condition is "impossible", if it occurs |
| 4877 | * we need to fix it. Originally reported by |
| 4878 | * Bjorn Helgaas on a 128-cpu setup. |
| 4879 | */ |
| 4880 | BUG_ON(busiest_rq == target_rq); |
| 4881 | |
| 4882 | /* move a task from busiest_rq to target_rq */ |
| 4883 | double_lock_balance(busiest_rq, target_rq); |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4884 | |
| 4885 | /* Search for an sd spanning us and the target CPU. */ |
Peter Zijlstra | dce840a | 2011-04-07 14:09:50 +0200 | [diff] [blame] | 4886 | rcu_read_lock(); |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4887 | for_each_domain(target_cpu, sd) { |
| 4888 | if ((sd->flags & SD_LOAD_BALANCE) && |
| 4889 | cpumask_test_cpu(busiest_cpu, sched_domain_span(sd))) |
| 4890 | break; |
| 4891 | } |
| 4892 | |
| 4893 | if (likely(sd)) { |
Peter Zijlstra | 8e45cb5 | 2012-02-22 12:47:19 +0100 | [diff] [blame] | 4894 | struct lb_env env = { |
| 4895 | .sd = sd, |
Peter Zijlstra | ddcdf6e | 2012-02-22 19:27:40 +0100 | [diff] [blame] | 4896 | .dst_cpu = target_cpu, |
| 4897 | .dst_rq = target_rq, |
| 4898 | .src_cpu = busiest_rq->cpu, |
| 4899 | .src_rq = busiest_rq, |
Peter Zijlstra | 8e45cb5 | 2012-02-22 12:47:19 +0100 | [diff] [blame] | 4900 | .idle = CPU_IDLE, |
| 4901 | }; |
| 4902 | |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4903 | schedstat_inc(sd, alb_count); |
| 4904 | |
Peter Zijlstra | 8e45cb5 | 2012-02-22 12:47:19 +0100 | [diff] [blame] | 4905 | if (move_one_task(&env)) |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4906 | schedstat_inc(sd, alb_pushed); |
| 4907 | else |
| 4908 | schedstat_inc(sd, alb_failed); |
| 4909 | } |
Peter Zijlstra | dce840a | 2011-04-07 14:09:50 +0200 | [diff] [blame] | 4910 | rcu_read_unlock(); |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4911 | double_unlock_balance(busiest_rq, target_rq); |
Tejun Heo | 969c792 | 2010-05-06 18:49:21 +0200 | [diff] [blame] | 4912 | out_unlock: |
| 4913 | busiest_rq->active_balance = 0; |
| 4914 | raw_spin_unlock_irq(&busiest_rq->lock); |
| 4915 | return 0; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4916 | } |
| 4917 | |
| 4918 | #ifdef CONFIG_NO_HZ |
Venkatesh Pallipadi | 83cd4fe | 2010-05-21 17:09:41 -0700 | [diff] [blame] | 4919 | /* |
| 4920 | * idle load balancing details |
Venkatesh Pallipadi | 83cd4fe | 2010-05-21 17:09:41 -0700 | [diff] [blame] | 4921 | * - When one of the busy CPUs notice that there may be an idle rebalancing |
| 4922 | * needed, they will kick the idle load balancer, which then does idle |
| 4923 | * load balancing for all the idle CPUs. |
| 4924 | */ |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4925 | static struct { |
Venkatesh Pallipadi | 83cd4fe | 2010-05-21 17:09:41 -0700 | [diff] [blame] | 4926 | cpumask_var_t idle_cpus_mask; |
Suresh Siddha | 0b005cf | 2011-12-01 17:07:34 -0800 | [diff] [blame] | 4927 | atomic_t nr_cpus; |
Venkatesh Pallipadi | 83cd4fe | 2010-05-21 17:09:41 -0700 | [diff] [blame] | 4928 | unsigned long next_balance; /* in jiffy units */ |
| 4929 | } nohz ____cacheline_aligned; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4930 | |
Peter Zijlstra | 8e7fbcb | 2012-01-09 11:28:35 +0100 | [diff] [blame] | 4931 | static inline int find_new_ilb(int call_cpu) |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4932 | { |
Suresh Siddha | 0b005cf | 2011-12-01 17:07:34 -0800 | [diff] [blame] | 4933 | int ilb = cpumask_first(nohz.idle_cpus_mask); |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4934 | |
Suresh Siddha | 786d6dc | 2011-12-01 17:07:35 -0800 | [diff] [blame] | 4935 | if (ilb < nr_cpu_ids && idle_cpu(ilb)) |
| 4936 | return ilb; |
| 4937 | |
| 4938 | return nr_cpu_ids; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4939 | } |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4940 | |
| 4941 | /* |
Venkatesh Pallipadi | 83cd4fe | 2010-05-21 17:09:41 -0700 | [diff] [blame] | 4942 | * Kick a CPU to do the nohz balancing, if it is time for it. We pick the |
| 4943 | * nohz_load_balancer CPU (if there is one) otherwise fallback to any idle |
| 4944 | * CPU (if there is one). |
| 4945 | */ |
| 4946 | static void nohz_balancer_kick(int cpu) |
| 4947 | { |
| 4948 | int ilb_cpu; |
| 4949 | |
| 4950 | nohz.next_balance++; |
| 4951 | |
Suresh Siddha | 0b005cf | 2011-12-01 17:07:34 -0800 | [diff] [blame] | 4952 | ilb_cpu = find_new_ilb(cpu); |
Venkatesh Pallipadi | 83cd4fe | 2010-05-21 17:09:41 -0700 | [diff] [blame] | 4953 | |
Suresh Siddha | 0b005cf | 2011-12-01 17:07:34 -0800 | [diff] [blame] | 4954 | if (ilb_cpu >= nr_cpu_ids) |
| 4955 | return; |
Venkatesh Pallipadi | 83cd4fe | 2010-05-21 17:09:41 -0700 | [diff] [blame] | 4956 | |
Suresh Siddha | cd490c5 | 2011-12-06 11:26:34 -0800 | [diff] [blame] | 4957 | if (test_and_set_bit(NOHZ_BALANCE_KICK, nohz_flags(ilb_cpu))) |
Suresh Siddha | 1c792db | 2011-12-01 17:07:32 -0800 | [diff] [blame] | 4958 | return; |
| 4959 | /* |
| 4960 | * Use smp_send_reschedule() instead of resched_cpu(). |
| 4961 | * This way we generate a sched IPI on the target cpu which |
| 4962 | * is idle. And the softirq performing nohz idle load balance |
| 4963 | * will be run before returning from the IPI. |
| 4964 | */ |
| 4965 | smp_send_reschedule(ilb_cpu); |
Venkatesh Pallipadi | 83cd4fe | 2010-05-21 17:09:41 -0700 | [diff] [blame] | 4966 | return; |
| 4967 | } |
| 4968 | |
Alex Shi | c1cc017 | 2012-09-10 15:10:58 +0800 | [diff] [blame] | 4969 | static inline void nohz_balance_exit_idle(int cpu) |
Suresh Siddha | 7132596 | 2012-01-19 18:28:57 -0800 | [diff] [blame] | 4970 | { |
| 4971 | if (unlikely(test_bit(NOHZ_TICK_STOPPED, nohz_flags(cpu)))) { |
| 4972 | cpumask_clear_cpu(cpu, nohz.idle_cpus_mask); |
| 4973 | atomic_dec(&nohz.nr_cpus); |
| 4974 | clear_bit(NOHZ_TICK_STOPPED, nohz_flags(cpu)); |
| 4975 | } |
| 4976 | } |
| 4977 | |
Suresh Siddha | 69e1e81 | 2011-12-01 17:07:33 -0800 | [diff] [blame] | 4978 | static inline void set_cpu_sd_state_busy(void) |
| 4979 | { |
| 4980 | struct sched_domain *sd; |
| 4981 | int cpu = smp_processor_id(); |
| 4982 | |
| 4983 | if (!test_bit(NOHZ_IDLE, nohz_flags(cpu))) |
| 4984 | return; |
| 4985 | clear_bit(NOHZ_IDLE, nohz_flags(cpu)); |
| 4986 | |
| 4987 | rcu_read_lock(); |
| 4988 | for_each_domain(cpu, sd) |
| 4989 | atomic_inc(&sd->groups->sgp->nr_busy_cpus); |
| 4990 | rcu_read_unlock(); |
| 4991 | } |
| 4992 | |
| 4993 | void set_cpu_sd_state_idle(void) |
| 4994 | { |
| 4995 | struct sched_domain *sd; |
| 4996 | int cpu = smp_processor_id(); |
| 4997 | |
| 4998 | if (test_bit(NOHZ_IDLE, nohz_flags(cpu))) |
| 4999 | return; |
| 5000 | set_bit(NOHZ_IDLE, nohz_flags(cpu)); |
| 5001 | |
| 5002 | rcu_read_lock(); |
| 5003 | for_each_domain(cpu, sd) |
| 5004 | atomic_dec(&sd->groups->sgp->nr_busy_cpus); |
| 5005 | rcu_read_unlock(); |
| 5006 | } |
| 5007 | |
Venkatesh Pallipadi | 83cd4fe | 2010-05-21 17:09:41 -0700 | [diff] [blame] | 5008 | /* |
Alex Shi | c1cc017 | 2012-09-10 15:10:58 +0800 | [diff] [blame] | 5009 | * This routine will record that the cpu is going idle with tick stopped. |
Suresh Siddha | 0b005cf | 2011-12-01 17:07:34 -0800 | [diff] [blame] | 5010 | * This info will be used in performing idle load balancing in the future. |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 5011 | */ |
Alex Shi | c1cc017 | 2012-09-10 15:10:58 +0800 | [diff] [blame] | 5012 | void nohz_balance_enter_idle(int cpu) |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 5013 | { |
Suresh Siddha | 7132596 | 2012-01-19 18:28:57 -0800 | [diff] [blame] | 5014 | /* |
| 5015 | * If this cpu is going down, then nothing needs to be done. |
| 5016 | */ |
| 5017 | if (!cpu_active(cpu)) |
| 5018 | return; |
| 5019 | |
Alex Shi | c1cc017 | 2012-09-10 15:10:58 +0800 | [diff] [blame] | 5020 | if (test_bit(NOHZ_TICK_STOPPED, nohz_flags(cpu))) |
| 5021 | return; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 5022 | |
Alex Shi | c1cc017 | 2012-09-10 15:10:58 +0800 | [diff] [blame] | 5023 | cpumask_set_cpu(cpu, nohz.idle_cpus_mask); |
| 5024 | atomic_inc(&nohz.nr_cpus); |
| 5025 | set_bit(NOHZ_TICK_STOPPED, nohz_flags(cpu)); |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 5026 | } |
Suresh Siddha | 7132596 | 2012-01-19 18:28:57 -0800 | [diff] [blame] | 5027 | |
| 5028 | static int __cpuinit sched_ilb_notifier(struct notifier_block *nfb, |
| 5029 | unsigned long action, void *hcpu) |
| 5030 | { |
| 5031 | switch (action & ~CPU_TASKS_FROZEN) { |
| 5032 | case CPU_DYING: |
Alex Shi | c1cc017 | 2012-09-10 15:10:58 +0800 | [diff] [blame] | 5033 | nohz_balance_exit_idle(smp_processor_id()); |
Suresh Siddha | 7132596 | 2012-01-19 18:28:57 -0800 | [diff] [blame] | 5034 | return NOTIFY_OK; |
| 5035 | default: |
| 5036 | return NOTIFY_DONE; |
| 5037 | } |
| 5038 | } |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 5039 | #endif |
| 5040 | |
| 5041 | static DEFINE_SPINLOCK(balancing); |
| 5042 | |
Peter Zijlstra | 49c022e | 2011-04-05 10:14:25 +0200 | [diff] [blame] | 5043 | /* |
| 5044 | * Scale the max load_balance interval with the number of CPUs in the system. |
| 5045 | * This trades load-balance latency on larger machines for less cross talk. |
| 5046 | */ |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 5047 | void update_max_interval(void) |
Peter Zijlstra | 49c022e | 2011-04-05 10:14:25 +0200 | [diff] [blame] | 5048 | { |
| 5049 | max_load_balance_interval = HZ*num_online_cpus()/10; |
| 5050 | } |
| 5051 | |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 5052 | /* |
| 5053 | * It checks each scheduling domain to see if it is due to be balanced, |
| 5054 | * and initiates a balancing operation if so. |
| 5055 | * |
| 5056 | * Balancing parameters are set up in arch_init_sched_domains. |
| 5057 | */ |
| 5058 | static void rebalance_domains(int cpu, enum cpu_idle_type idle) |
| 5059 | { |
| 5060 | int balance = 1; |
| 5061 | struct rq *rq = cpu_rq(cpu); |
| 5062 | unsigned long interval; |
Peter Zijlstra | 04f733b | 2012-05-11 00:12:02 +0200 | [diff] [blame] | 5063 | struct sched_domain *sd; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 5064 | /* Earliest time when we have to do rebalance again */ |
| 5065 | unsigned long next_balance = jiffies + 60*HZ; |
| 5066 | int update_next_balance = 0; |
| 5067 | int need_serialize; |
| 5068 | |
Peter Zijlstra | 2069dd7 | 2010-11-15 15:47:00 -0800 | [diff] [blame] | 5069 | update_shares(cpu); |
| 5070 | |
Peter Zijlstra | dce840a | 2011-04-07 14:09:50 +0200 | [diff] [blame] | 5071 | rcu_read_lock(); |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 5072 | for_each_domain(cpu, sd) { |
| 5073 | if (!(sd->flags & SD_LOAD_BALANCE)) |
| 5074 | continue; |
| 5075 | |
| 5076 | interval = sd->balance_interval; |
| 5077 | if (idle != CPU_IDLE) |
| 5078 | interval *= sd->busy_factor; |
| 5079 | |
| 5080 | /* scale ms to jiffies */ |
| 5081 | interval = msecs_to_jiffies(interval); |
Peter Zijlstra | 49c022e | 2011-04-05 10:14:25 +0200 | [diff] [blame] | 5082 | interval = clamp(interval, 1UL, max_load_balance_interval); |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 5083 | |
| 5084 | need_serialize = sd->flags & SD_SERIALIZE; |
| 5085 | |
| 5086 | if (need_serialize) { |
| 5087 | if (!spin_trylock(&balancing)) |
| 5088 | goto out; |
| 5089 | } |
| 5090 | |
| 5091 | if (time_after_eq(jiffies, sd->last_balance + interval)) { |
| 5092 | if (load_balance(cpu, rq, sd, idle, &balance)) { |
| 5093 | /* |
| 5094 | * We've pulled tasks over so either we're no |
Peter Zijlstra | c186faf | 2011-02-21 18:52:53 +0100 | [diff] [blame] | 5095 | * longer idle. |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 5096 | */ |
| 5097 | idle = CPU_NOT_IDLE; |
| 5098 | } |
| 5099 | sd->last_balance = jiffies; |
| 5100 | } |
| 5101 | if (need_serialize) |
| 5102 | spin_unlock(&balancing); |
| 5103 | out: |
| 5104 | if (time_after(next_balance, sd->last_balance + interval)) { |
| 5105 | next_balance = sd->last_balance + interval; |
| 5106 | update_next_balance = 1; |
| 5107 | } |
| 5108 | |
| 5109 | /* |
| 5110 | * Stop the load balance at this level. There is another |
| 5111 | * CPU in our sched group which is doing load balancing more |
| 5112 | * actively. |
| 5113 | */ |
| 5114 | if (!balance) |
| 5115 | break; |
| 5116 | } |
Peter Zijlstra | dce840a | 2011-04-07 14:09:50 +0200 | [diff] [blame] | 5117 | rcu_read_unlock(); |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 5118 | |
| 5119 | /* |
| 5120 | * next_balance will be updated only when there is a need. |
| 5121 | * When the cpu is attached to null domain for ex, it will not be |
| 5122 | * updated. |
| 5123 | */ |
| 5124 | if (likely(update_next_balance)) |
| 5125 | rq->next_balance = next_balance; |
| 5126 | } |
| 5127 | |
Venkatesh Pallipadi | 83cd4fe | 2010-05-21 17:09:41 -0700 | [diff] [blame] | 5128 | #ifdef CONFIG_NO_HZ |
| 5129 | /* |
| 5130 | * In CONFIG_NO_HZ case, the idle balance kickee will do the |
| 5131 | * rebalancing for all the cpus for whom scheduler ticks are stopped. |
| 5132 | */ |
| 5133 | static void nohz_idle_balance(int this_cpu, enum cpu_idle_type idle) |
| 5134 | { |
| 5135 | struct rq *this_rq = cpu_rq(this_cpu); |
| 5136 | struct rq *rq; |
| 5137 | int balance_cpu; |
| 5138 | |
Suresh Siddha | 1c792db | 2011-12-01 17:07:32 -0800 | [diff] [blame] | 5139 | if (idle != CPU_IDLE || |
| 5140 | !test_bit(NOHZ_BALANCE_KICK, nohz_flags(this_cpu))) |
| 5141 | goto end; |
Venkatesh Pallipadi | 83cd4fe | 2010-05-21 17:09:41 -0700 | [diff] [blame] | 5142 | |
| 5143 | for_each_cpu(balance_cpu, nohz.idle_cpus_mask) { |
Suresh Siddha | 8a6d42d | 2011-12-06 11:19:37 -0800 | [diff] [blame] | 5144 | if (balance_cpu == this_cpu || !idle_cpu(balance_cpu)) |
Venkatesh Pallipadi | 83cd4fe | 2010-05-21 17:09:41 -0700 | [diff] [blame] | 5145 | continue; |
| 5146 | |
| 5147 | /* |
| 5148 | * If this cpu gets work to do, stop the load balancing |
| 5149 | * work being done for other cpus. Next load |
| 5150 | * balancing owner will pick it up. |
| 5151 | */ |
Suresh Siddha | 1c792db | 2011-12-01 17:07:32 -0800 | [diff] [blame] | 5152 | if (need_resched()) |
Venkatesh Pallipadi | 83cd4fe | 2010-05-21 17:09:41 -0700 | [diff] [blame] | 5153 | break; |
Venkatesh Pallipadi | 83cd4fe | 2010-05-21 17:09:41 -0700 | [diff] [blame] | 5154 | |
Vincent Guittot | 5ed4f1d | 2012-09-13 06:11:26 +0200 | [diff] [blame] | 5155 | rq = cpu_rq(balance_cpu); |
| 5156 | |
| 5157 | raw_spin_lock_irq(&rq->lock); |
| 5158 | update_rq_clock(rq); |
| 5159 | update_idle_cpu_load(rq); |
| 5160 | raw_spin_unlock_irq(&rq->lock); |
Venkatesh Pallipadi | 83cd4fe | 2010-05-21 17:09:41 -0700 | [diff] [blame] | 5161 | |
| 5162 | rebalance_domains(balance_cpu, CPU_IDLE); |
| 5163 | |
Venkatesh Pallipadi | 83cd4fe | 2010-05-21 17:09:41 -0700 | [diff] [blame] | 5164 | if (time_after(this_rq->next_balance, rq->next_balance)) |
| 5165 | this_rq->next_balance = rq->next_balance; |
| 5166 | } |
| 5167 | nohz.next_balance = this_rq->next_balance; |
Suresh Siddha | 1c792db | 2011-12-01 17:07:32 -0800 | [diff] [blame] | 5168 | end: |
| 5169 | clear_bit(NOHZ_BALANCE_KICK, nohz_flags(this_cpu)); |
Venkatesh Pallipadi | 83cd4fe | 2010-05-21 17:09:41 -0700 | [diff] [blame] | 5170 | } |
| 5171 | |
| 5172 | /* |
Suresh Siddha | 0b005cf | 2011-12-01 17:07:34 -0800 | [diff] [blame] | 5173 | * Current heuristic for kicking the idle load balancer in the presence |
| 5174 | * of an idle cpu is the system. |
| 5175 | * - This rq has more than one task. |
| 5176 | * - At any scheduler domain level, this cpu's scheduler group has multiple |
| 5177 | * busy cpu's exceeding the group's power. |
| 5178 | * - For SD_ASYM_PACKING, if the lower numbered cpu's in the scheduler |
| 5179 | * domain span are idle. |
Venkatesh Pallipadi | 83cd4fe | 2010-05-21 17:09:41 -0700 | [diff] [blame] | 5180 | */ |
| 5181 | static inline int nohz_kick_needed(struct rq *rq, int cpu) |
| 5182 | { |
| 5183 | unsigned long now = jiffies; |
Suresh Siddha | 0b005cf | 2011-12-01 17:07:34 -0800 | [diff] [blame] | 5184 | struct sched_domain *sd; |
Venkatesh Pallipadi | 83cd4fe | 2010-05-21 17:09:41 -0700 | [diff] [blame] | 5185 | |
Suresh Siddha | 1c792db | 2011-12-01 17:07:32 -0800 | [diff] [blame] | 5186 | if (unlikely(idle_cpu(cpu))) |
Venkatesh Pallipadi | 83cd4fe | 2010-05-21 17:09:41 -0700 | [diff] [blame] | 5187 | return 0; |
| 5188 | |
Suresh Siddha | 1c792db | 2011-12-01 17:07:32 -0800 | [diff] [blame] | 5189 | /* |
| 5190 | * We may be recently in ticked or tickless idle mode. At the first |
| 5191 | * busy tick after returning from idle, we will update the busy stats. |
| 5192 | */ |
Suresh Siddha | 69e1e81 | 2011-12-01 17:07:33 -0800 | [diff] [blame] | 5193 | set_cpu_sd_state_busy(); |
Alex Shi | c1cc017 | 2012-09-10 15:10:58 +0800 | [diff] [blame] | 5194 | nohz_balance_exit_idle(cpu); |
Suresh Siddha | 0b005cf | 2011-12-01 17:07:34 -0800 | [diff] [blame] | 5195 | |
| 5196 | /* |
| 5197 | * None are in tickless mode and hence no need for NOHZ idle load |
| 5198 | * balancing. |
| 5199 | */ |
| 5200 | if (likely(!atomic_read(&nohz.nr_cpus))) |
| 5201 | return 0; |
Suresh Siddha | 1c792db | 2011-12-01 17:07:32 -0800 | [diff] [blame] | 5202 | |
| 5203 | if (time_before(now, nohz.next_balance)) |
Venkatesh Pallipadi | 83cd4fe | 2010-05-21 17:09:41 -0700 | [diff] [blame] | 5204 | return 0; |
| 5205 | |
Suresh Siddha | 0b005cf | 2011-12-01 17:07:34 -0800 | [diff] [blame] | 5206 | if (rq->nr_running >= 2) |
| 5207 | goto need_kick; |
Venkatesh Pallipadi | 83cd4fe | 2010-05-21 17:09:41 -0700 | [diff] [blame] | 5208 | |
Peter Zijlstra | 067491b | 2011-12-07 14:32:08 +0100 | [diff] [blame] | 5209 | rcu_read_lock(); |
Suresh Siddha | 0b005cf | 2011-12-01 17:07:34 -0800 | [diff] [blame] | 5210 | for_each_domain(cpu, sd) { |
| 5211 | struct sched_group *sg = sd->groups; |
| 5212 | struct sched_group_power *sgp = sg->sgp; |
| 5213 | int nr_busy = atomic_read(&sgp->nr_busy_cpus); |
Venkatesh Pallipadi | 83cd4fe | 2010-05-21 17:09:41 -0700 | [diff] [blame] | 5214 | |
Suresh Siddha | 0b005cf | 2011-12-01 17:07:34 -0800 | [diff] [blame] | 5215 | if (sd->flags & SD_SHARE_PKG_RESOURCES && nr_busy > 1) |
Peter Zijlstra | 067491b | 2011-12-07 14:32:08 +0100 | [diff] [blame] | 5216 | goto need_kick_unlock; |
Suresh Siddha | 0b005cf | 2011-12-01 17:07:34 -0800 | [diff] [blame] | 5217 | |
| 5218 | if (sd->flags & SD_ASYM_PACKING && nr_busy != sg->group_weight |
| 5219 | && (cpumask_first_and(nohz.idle_cpus_mask, |
| 5220 | sched_domain_span(sd)) < cpu)) |
Peter Zijlstra | 067491b | 2011-12-07 14:32:08 +0100 | [diff] [blame] | 5221 | goto need_kick_unlock; |
Suresh Siddha | 0b005cf | 2011-12-01 17:07:34 -0800 | [diff] [blame] | 5222 | |
| 5223 | if (!(sd->flags & (SD_SHARE_PKG_RESOURCES | SD_ASYM_PACKING))) |
| 5224 | break; |
Venkatesh Pallipadi | 83cd4fe | 2010-05-21 17:09:41 -0700 | [diff] [blame] | 5225 | } |
Peter Zijlstra | 067491b | 2011-12-07 14:32:08 +0100 | [diff] [blame] | 5226 | rcu_read_unlock(); |
Venkatesh Pallipadi | 83cd4fe | 2010-05-21 17:09:41 -0700 | [diff] [blame] | 5227 | return 0; |
Peter Zijlstra | 067491b | 2011-12-07 14:32:08 +0100 | [diff] [blame] | 5228 | |
| 5229 | need_kick_unlock: |
| 5230 | rcu_read_unlock(); |
Suresh Siddha | 0b005cf | 2011-12-01 17:07:34 -0800 | [diff] [blame] | 5231 | need_kick: |
| 5232 | return 1; |
Venkatesh Pallipadi | 83cd4fe | 2010-05-21 17:09:41 -0700 | [diff] [blame] | 5233 | } |
| 5234 | #else |
| 5235 | static void nohz_idle_balance(int this_cpu, enum cpu_idle_type idle) { } |
| 5236 | #endif |
| 5237 | |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 5238 | /* |
| 5239 | * run_rebalance_domains is triggered when needed from the scheduler tick. |
Venkatesh Pallipadi | 83cd4fe | 2010-05-21 17:09:41 -0700 | [diff] [blame] | 5240 | * Also triggered for nohz idle balancing (with nohz_balancing_kick set). |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 5241 | */ |
| 5242 | static void run_rebalance_domains(struct softirq_action *h) |
| 5243 | { |
| 5244 | int this_cpu = smp_processor_id(); |
| 5245 | struct rq *this_rq = cpu_rq(this_cpu); |
Suresh Siddha | 6eb57e0 | 2011-10-03 15:09:01 -0700 | [diff] [blame] | 5246 | enum cpu_idle_type idle = this_rq->idle_balance ? |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 5247 | CPU_IDLE : CPU_NOT_IDLE; |
| 5248 | |
| 5249 | rebalance_domains(this_cpu, idle); |
| 5250 | |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 5251 | /* |
Venkatesh Pallipadi | 83cd4fe | 2010-05-21 17:09:41 -0700 | [diff] [blame] | 5252 | * If this cpu has a pending nohz_balance_kick, then do the |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 5253 | * balancing on behalf of the other idle cpus whose ticks are |
| 5254 | * stopped. |
| 5255 | */ |
Venkatesh Pallipadi | 83cd4fe | 2010-05-21 17:09:41 -0700 | [diff] [blame] | 5256 | nohz_idle_balance(this_cpu, idle); |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 5257 | } |
| 5258 | |
| 5259 | static inline int on_null_domain(int cpu) |
| 5260 | { |
Paul E. McKenney | 90a6501 | 2010-02-28 08:32:18 -0800 | [diff] [blame] | 5261 | return !rcu_dereference_sched(cpu_rq(cpu)->sd); |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 5262 | } |
| 5263 | |
| 5264 | /* |
| 5265 | * 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] | 5266 | */ |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 5267 | void trigger_load_balance(struct rq *rq, int cpu) |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 5268 | { |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 5269 | /* Don't need to rebalance while attached to NULL domain */ |
| 5270 | if (time_after_eq(jiffies, rq->next_balance) && |
| 5271 | likely(!on_null_domain(cpu))) |
| 5272 | raise_softirq(SCHED_SOFTIRQ); |
Venkatesh Pallipadi | 83cd4fe | 2010-05-21 17:09:41 -0700 | [diff] [blame] | 5273 | #ifdef CONFIG_NO_HZ |
Suresh Siddha | 1c792db | 2011-12-01 17:07:32 -0800 | [diff] [blame] | 5274 | if (nohz_kick_needed(rq, cpu) && likely(!on_null_domain(cpu))) |
Venkatesh Pallipadi | 83cd4fe | 2010-05-21 17:09:41 -0700 | [diff] [blame] | 5275 | nohz_balancer_kick(cpu); |
| 5276 | #endif |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 5277 | } |
| 5278 | |
Christian Ehrhardt | 0bcdcf2 | 2009-11-30 12:16:46 +0100 | [diff] [blame] | 5279 | static void rq_online_fair(struct rq *rq) |
| 5280 | { |
| 5281 | update_sysctl(); |
| 5282 | } |
| 5283 | |
| 5284 | static void rq_offline_fair(struct rq *rq) |
| 5285 | { |
| 5286 | update_sysctl(); |
Peter Boonstoppel | a4c96ae | 2012-08-09 15:34:47 -0700 | [diff] [blame] | 5287 | |
| 5288 | /* Ensure any throttled groups are reachable by pick_next_task */ |
| 5289 | unthrottle_offline_cfs_rqs(rq); |
Christian Ehrhardt | 0bcdcf2 | 2009-11-30 12:16:46 +0100 | [diff] [blame] | 5290 | } |
| 5291 | |
Dhaval Giani | 55e12e5 | 2008-06-24 23:39:43 +0530 | [diff] [blame] | 5292 | #endif /* CONFIG_SMP */ |
Peter Williams | e1d1484 | 2007-10-24 18:23:51 +0200 | [diff] [blame] | 5293 | |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 5294 | /* |
| 5295 | * scheduler tick hitting a task of our scheduling class: |
| 5296 | */ |
Peter Zijlstra | 8f4d37e | 2008-01-25 21:08:29 +0100 | [diff] [blame] | 5297 | 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] | 5298 | { |
| 5299 | struct cfs_rq *cfs_rq; |
| 5300 | struct sched_entity *se = &curr->se; |
| 5301 | |
| 5302 | for_each_sched_entity(se) { |
| 5303 | cfs_rq = cfs_rq_of(se); |
Peter Zijlstra | 8f4d37e | 2008-01-25 21:08:29 +0100 | [diff] [blame] | 5304 | entity_tick(cfs_rq, se, queued); |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 5305 | } |
Ben Segall | 18bf280 | 2012-10-04 12:51:20 +0200 | [diff] [blame] | 5306 | |
| 5307 | update_rq_runnable_avg(rq, 1); |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 5308 | } |
| 5309 | |
| 5310 | /* |
Peter Zijlstra | cd29fe6 | 2009-11-27 17:32:46 +0100 | [diff] [blame] | 5311 | * called on fork with the child task as argument from the parent's context |
| 5312 | * - child not yet on the tasklist |
| 5313 | * - preemption disabled |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 5314 | */ |
Peter Zijlstra | cd29fe6 | 2009-11-27 17:32:46 +0100 | [diff] [blame] | 5315 | static void task_fork_fair(struct task_struct *p) |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 5316 | { |
Daisuke Nishimura | 4fc420c | 2011-12-15 14:36:55 +0900 | [diff] [blame] | 5317 | struct cfs_rq *cfs_rq; |
| 5318 | struct sched_entity *se = &p->se, *curr; |
Ingo Molnar | 00bf7bf | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 5319 | int this_cpu = smp_processor_id(); |
Peter Zijlstra | cd29fe6 | 2009-11-27 17:32:46 +0100 | [diff] [blame] | 5320 | struct rq *rq = this_rq(); |
| 5321 | unsigned long flags; |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 5322 | |
Thomas Gleixner | 05fa785 | 2009-11-17 14:28:38 +0100 | [diff] [blame] | 5323 | raw_spin_lock_irqsave(&rq->lock, flags); |
Peter Zijlstra | cd29fe6 | 2009-11-27 17:32:46 +0100 | [diff] [blame] | 5324 | |
Peter Zijlstra | 861d034 | 2010-08-19 13:31:43 +0200 | [diff] [blame] | 5325 | update_rq_clock(rq); |
| 5326 | |
Daisuke Nishimura | 4fc420c | 2011-12-15 14:36:55 +0900 | [diff] [blame] | 5327 | cfs_rq = task_cfs_rq(current); |
| 5328 | curr = cfs_rq->curr; |
| 5329 | |
Paul E. McKenney | b0a0f66 | 2010-10-06 17:32:51 -0700 | [diff] [blame] | 5330 | if (unlikely(task_cpu(p) != this_cpu)) { |
| 5331 | rcu_read_lock(); |
Peter Zijlstra | cd29fe6 | 2009-11-27 17:32:46 +0100 | [diff] [blame] | 5332 | __set_task_cpu(p, this_cpu); |
Paul E. McKenney | b0a0f66 | 2010-10-06 17:32:51 -0700 | [diff] [blame] | 5333 | rcu_read_unlock(); |
| 5334 | } |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 5335 | |
Ting Yang | 7109c44 | 2007-08-28 12:53:24 +0200 | [diff] [blame] | 5336 | update_curr(cfs_rq); |
Peter Zijlstra | cd29fe6 | 2009-11-27 17:32:46 +0100 | [diff] [blame] | 5337 | |
Mike Galbraith | b5d9d73 | 2009-09-08 11:12:28 +0200 | [diff] [blame] | 5338 | if (curr) |
| 5339 | se->vruntime = curr->vruntime; |
Peter Zijlstra | aeb73b0 | 2007-10-15 17:00:05 +0200 | [diff] [blame] | 5340 | place_entity(cfs_rq, se, 1); |
Peter Zijlstra | 4d78e7b | 2007-10-15 17:00:04 +0200 | [diff] [blame] | 5341 | |
Peter Zijlstra | cd29fe6 | 2009-11-27 17:32:46 +0100 | [diff] [blame] | 5342 | if (sysctl_sched_child_runs_first && curr && entity_before(curr, se)) { |
Dmitry Adamushko | 87fefa3 | 2007-10-15 17:00:08 +0200 | [diff] [blame] | 5343 | /* |
Ingo Molnar | edcb60a | 2007-10-15 17:00:08 +0200 | [diff] [blame] | 5344 | * Upon rescheduling, sched_class::put_prev_task() will place |
| 5345 | * 'current' within the tree based on its new key value. |
| 5346 | */ |
Peter Zijlstra | 4d78e7b | 2007-10-15 17:00:04 +0200 | [diff] [blame] | 5347 | swap(curr->vruntime, se->vruntime); |
Bharata B Rao | aec0a51 | 2008-08-28 14:42:49 +0530 | [diff] [blame] | 5348 | resched_task(rq->curr); |
Peter Zijlstra | 4d78e7b | 2007-10-15 17:00:04 +0200 | [diff] [blame] | 5349 | } |
| 5350 | |
Peter Zijlstra | 88ec22d | 2009-12-16 18:04:41 +0100 | [diff] [blame] | 5351 | se->vruntime -= cfs_rq->min_vruntime; |
| 5352 | |
Thomas Gleixner | 05fa785 | 2009-11-17 14:28:38 +0100 | [diff] [blame] | 5353 | raw_spin_unlock_irqrestore(&rq->lock, flags); |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 5354 | } |
| 5355 | |
Steven Rostedt | cb46984 | 2008-01-25 21:08:22 +0100 | [diff] [blame] | 5356 | /* |
| 5357 | * Priority of the task has changed. Check to see if we preempt |
| 5358 | * the current task. |
| 5359 | */ |
Peter Zijlstra | da7a735 | 2011-01-17 17:03:27 +0100 | [diff] [blame] | 5360 | static void |
| 5361 | prio_changed_fair(struct rq *rq, struct task_struct *p, int oldprio) |
Steven Rostedt | cb46984 | 2008-01-25 21:08:22 +0100 | [diff] [blame] | 5362 | { |
Peter Zijlstra | da7a735 | 2011-01-17 17:03:27 +0100 | [diff] [blame] | 5363 | if (!p->se.on_rq) |
| 5364 | return; |
| 5365 | |
Steven Rostedt | cb46984 | 2008-01-25 21:08:22 +0100 | [diff] [blame] | 5366 | /* |
| 5367 | * Reschedule if we are currently running on this runqueue and |
| 5368 | * our priority decreased, or if we are not currently running on |
| 5369 | * this runqueue and our priority is higher than the current's |
| 5370 | */ |
Peter Zijlstra | da7a735 | 2011-01-17 17:03:27 +0100 | [diff] [blame] | 5371 | if (rq->curr == p) { |
Steven Rostedt | cb46984 | 2008-01-25 21:08:22 +0100 | [diff] [blame] | 5372 | if (p->prio > oldprio) |
| 5373 | resched_task(rq->curr); |
| 5374 | } else |
Peter Zijlstra | 15afe09 | 2008-09-20 23:38:02 +0200 | [diff] [blame] | 5375 | check_preempt_curr(rq, p, 0); |
Steven Rostedt | cb46984 | 2008-01-25 21:08:22 +0100 | [diff] [blame] | 5376 | } |
| 5377 | |
Peter Zijlstra | da7a735 | 2011-01-17 17:03:27 +0100 | [diff] [blame] | 5378 | static void switched_from_fair(struct rq *rq, struct task_struct *p) |
| 5379 | { |
| 5380 | struct sched_entity *se = &p->se; |
| 5381 | struct cfs_rq *cfs_rq = cfs_rq_of(se); |
| 5382 | |
| 5383 | /* |
| 5384 | * Ensure the task's vruntime is normalized, so that when its |
| 5385 | * switched back to the fair class the enqueue_entity(.flags=0) will |
| 5386 | * do the right thing. |
| 5387 | * |
| 5388 | * If it was on_rq, then the dequeue_entity(.flags=0) will already |
| 5389 | * have normalized the vruntime, if it was !on_rq, then only when |
| 5390 | * the task is sleeping will it still have non-normalized vruntime. |
| 5391 | */ |
| 5392 | if (!se->on_rq && p->state != TASK_RUNNING) { |
| 5393 | /* |
| 5394 | * Fix up our vruntime so that the current sleep doesn't |
| 5395 | * cause 'unlimited' sleep bonus. |
| 5396 | */ |
| 5397 | place_entity(cfs_rq, se, 0); |
| 5398 | se->vruntime -= cfs_rq->min_vruntime; |
| 5399 | } |
Paul Turner | 9ee474f | 2012-10-04 13:18:30 +0200 | [diff] [blame] | 5400 | |
| 5401 | #if defined(CONFIG_FAIR_GROUP_SCHED) && defined(CONFIG_SMP) |
| 5402 | /* |
| 5403 | * Remove our load from contribution when we leave sched_fair |
| 5404 | * and ensure we don't carry in an old decay_count if we |
| 5405 | * switch back. |
| 5406 | */ |
| 5407 | if (p->se.avg.decay_count) { |
| 5408 | struct cfs_rq *cfs_rq = cfs_rq_of(&p->se); |
| 5409 | __synchronize_entity_decay(&p->se); |
| 5410 | subtract_blocked_load_contrib(cfs_rq, |
| 5411 | p->se.avg.load_avg_contrib); |
| 5412 | } |
| 5413 | #endif |
Peter Zijlstra | da7a735 | 2011-01-17 17:03:27 +0100 | [diff] [blame] | 5414 | } |
| 5415 | |
Steven Rostedt | cb46984 | 2008-01-25 21:08:22 +0100 | [diff] [blame] | 5416 | /* |
| 5417 | * We switched to the sched_fair class. |
| 5418 | */ |
Peter Zijlstra | da7a735 | 2011-01-17 17:03:27 +0100 | [diff] [blame] | 5419 | static void switched_to_fair(struct rq *rq, struct task_struct *p) |
Steven Rostedt | cb46984 | 2008-01-25 21:08:22 +0100 | [diff] [blame] | 5420 | { |
Peter Zijlstra | da7a735 | 2011-01-17 17:03:27 +0100 | [diff] [blame] | 5421 | if (!p->se.on_rq) |
| 5422 | return; |
| 5423 | |
Steven Rostedt | cb46984 | 2008-01-25 21:08:22 +0100 | [diff] [blame] | 5424 | /* |
| 5425 | * We were most likely switched from sched_rt, so |
| 5426 | * kick off the schedule if running, otherwise just see |
| 5427 | * if we can still preempt the current task. |
| 5428 | */ |
Peter Zijlstra | da7a735 | 2011-01-17 17:03:27 +0100 | [diff] [blame] | 5429 | if (rq->curr == p) |
Steven Rostedt | cb46984 | 2008-01-25 21:08:22 +0100 | [diff] [blame] | 5430 | resched_task(rq->curr); |
| 5431 | else |
Peter Zijlstra | 15afe09 | 2008-09-20 23:38:02 +0200 | [diff] [blame] | 5432 | check_preempt_curr(rq, p, 0); |
Steven Rostedt | cb46984 | 2008-01-25 21:08:22 +0100 | [diff] [blame] | 5433 | } |
| 5434 | |
Srivatsa Vaddagiri | 83b699e | 2007-10-15 17:00:08 +0200 | [diff] [blame] | 5435 | /* Account for a task changing its policy or group. |
| 5436 | * |
| 5437 | * This routine is mostly called to set cfs_rq->curr field when a task |
| 5438 | * migrates between groups/classes. |
| 5439 | */ |
| 5440 | static void set_curr_task_fair(struct rq *rq) |
| 5441 | { |
| 5442 | struct sched_entity *se = &rq->curr->se; |
| 5443 | |
Paul Turner | ec12cb7 | 2011-07-21 09:43:30 -0700 | [diff] [blame] | 5444 | for_each_sched_entity(se) { |
| 5445 | struct cfs_rq *cfs_rq = cfs_rq_of(se); |
| 5446 | |
| 5447 | set_next_entity(cfs_rq, se); |
| 5448 | /* ensure bandwidth has been allocated on our new cfs_rq */ |
| 5449 | account_cfs_rq_runtime(cfs_rq, 0); |
| 5450 | } |
Srivatsa Vaddagiri | 83b699e | 2007-10-15 17:00:08 +0200 | [diff] [blame] | 5451 | } |
| 5452 | |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 5453 | void init_cfs_rq(struct cfs_rq *cfs_rq) |
| 5454 | { |
| 5455 | cfs_rq->tasks_timeline = RB_ROOT; |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 5456 | cfs_rq->min_vruntime = (u64)(-(1LL << 20)); |
| 5457 | #ifndef CONFIG_64BIT |
| 5458 | cfs_rq->min_vruntime_copy = cfs_rq->min_vruntime; |
| 5459 | #endif |
Paul Turner | 9ee474f | 2012-10-04 13:18:30 +0200 | [diff] [blame] | 5460 | #if defined(CONFIG_FAIR_GROUP_SCHED) && defined(CONFIG_SMP) |
| 5461 | atomic64_set(&cfs_rq->decay_counter, 1); |
Paul Turner | aff3e49 | 2012-10-04 13:18:30 +0200 | [diff] [blame] | 5462 | atomic64_set(&cfs_rq->removed_load, 0); |
Paul Turner | 9ee474f | 2012-10-04 13:18:30 +0200 | [diff] [blame] | 5463 | #endif |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 5464 | } |
| 5465 | |
Peter Zijlstra | 810b381 | 2008-02-29 15:21:01 -0500 | [diff] [blame] | 5466 | #ifdef CONFIG_FAIR_GROUP_SCHED |
Peter Zijlstra | b2b5ce0 | 2010-10-15 15:24:15 +0200 | [diff] [blame] | 5467 | 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] | 5468 | { |
Paul Turner | aff3e49 | 2012-10-04 13:18:30 +0200 | [diff] [blame] | 5469 | struct cfs_rq *cfs_rq; |
Peter Zijlstra | b2b5ce0 | 2010-10-15 15:24:15 +0200 | [diff] [blame] | 5470 | /* |
| 5471 | * If the task was not on the rq at the time of this cgroup movement |
| 5472 | * it must have been asleep, sleeping tasks keep their ->vruntime |
| 5473 | * absolute on their old rq until wakeup (needed for the fair sleeper |
| 5474 | * bonus in place_entity()). |
| 5475 | * |
| 5476 | * If it was on the rq, we've just 'preempted' it, which does convert |
| 5477 | * ->vruntime to a relative base. |
| 5478 | * |
| 5479 | * Make sure both cases convert their relative position when migrating |
| 5480 | * to another cgroup's rq. This does somewhat interfere with the |
| 5481 | * fair sleeper stuff for the first placement, but who cares. |
| 5482 | */ |
Daisuke Nishimura | 7ceff01 | 2011-12-15 14:36:07 +0900 | [diff] [blame] | 5483 | /* |
| 5484 | * When !on_rq, vruntime of the task has usually NOT been normalized. |
| 5485 | * But there are some cases where it has already been normalized: |
| 5486 | * |
| 5487 | * - Moving a forked child which is waiting for being woken up by |
| 5488 | * wake_up_new_task(). |
Daisuke Nishimura | 62af378 | 2011-12-15 14:37:41 +0900 | [diff] [blame] | 5489 | * - Moving a task which has been woken up by try_to_wake_up() and |
| 5490 | * waiting for actually being woken up by sched_ttwu_pending(). |
Daisuke Nishimura | 7ceff01 | 2011-12-15 14:36:07 +0900 | [diff] [blame] | 5491 | * |
| 5492 | * To prevent boost or penalty in the new cfs_rq caused by delta |
| 5493 | * min_vruntime between the two cfs_rqs, we skip vruntime adjustment. |
| 5494 | */ |
Daisuke Nishimura | 62af378 | 2011-12-15 14:37:41 +0900 | [diff] [blame] | 5495 | if (!on_rq && (!p->se.sum_exec_runtime || p->state == TASK_WAKING)) |
Daisuke Nishimura | 7ceff01 | 2011-12-15 14:36:07 +0900 | [diff] [blame] | 5496 | on_rq = 1; |
| 5497 | |
Peter Zijlstra | 88ec22d | 2009-12-16 18:04:41 +0100 | [diff] [blame] | 5498 | if (!on_rq) |
Peter Zijlstra | b2b5ce0 | 2010-10-15 15:24:15 +0200 | [diff] [blame] | 5499 | p->se.vruntime -= cfs_rq_of(&p->se)->min_vruntime; |
| 5500 | set_task_rq(p, task_cpu(p)); |
Paul Turner | aff3e49 | 2012-10-04 13:18:30 +0200 | [diff] [blame] | 5501 | if (!on_rq) { |
| 5502 | cfs_rq = cfs_rq_of(&p->se); |
| 5503 | p->se.vruntime += cfs_rq->min_vruntime; |
| 5504 | #ifdef CONFIG_SMP |
| 5505 | /* |
| 5506 | * migrate_task_rq_fair() will have removed our previous |
| 5507 | * contribution, but we must synchronize for ongoing future |
| 5508 | * decay. |
| 5509 | */ |
| 5510 | p->se.avg.decay_count = atomic64_read(&cfs_rq->decay_counter); |
| 5511 | cfs_rq->blocked_load_avg += p->se.avg.load_avg_contrib; |
| 5512 | #endif |
| 5513 | } |
Peter Zijlstra | 810b381 | 2008-02-29 15:21:01 -0500 | [diff] [blame] | 5514 | } |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 5515 | |
| 5516 | void free_fair_sched_group(struct task_group *tg) |
| 5517 | { |
| 5518 | int i; |
| 5519 | |
| 5520 | destroy_cfs_bandwidth(tg_cfs_bandwidth(tg)); |
| 5521 | |
| 5522 | for_each_possible_cpu(i) { |
| 5523 | if (tg->cfs_rq) |
| 5524 | kfree(tg->cfs_rq[i]); |
| 5525 | if (tg->se) |
| 5526 | kfree(tg->se[i]); |
| 5527 | } |
| 5528 | |
| 5529 | kfree(tg->cfs_rq); |
| 5530 | kfree(tg->se); |
| 5531 | } |
| 5532 | |
| 5533 | int alloc_fair_sched_group(struct task_group *tg, struct task_group *parent) |
| 5534 | { |
| 5535 | struct cfs_rq *cfs_rq; |
| 5536 | struct sched_entity *se; |
| 5537 | int i; |
| 5538 | |
| 5539 | tg->cfs_rq = kzalloc(sizeof(cfs_rq) * nr_cpu_ids, GFP_KERNEL); |
| 5540 | if (!tg->cfs_rq) |
| 5541 | goto err; |
| 5542 | tg->se = kzalloc(sizeof(se) * nr_cpu_ids, GFP_KERNEL); |
| 5543 | if (!tg->se) |
| 5544 | goto err; |
| 5545 | |
| 5546 | tg->shares = NICE_0_LOAD; |
| 5547 | |
| 5548 | init_cfs_bandwidth(tg_cfs_bandwidth(tg)); |
| 5549 | |
| 5550 | for_each_possible_cpu(i) { |
| 5551 | cfs_rq = kzalloc_node(sizeof(struct cfs_rq), |
| 5552 | GFP_KERNEL, cpu_to_node(i)); |
| 5553 | if (!cfs_rq) |
| 5554 | goto err; |
| 5555 | |
| 5556 | se = kzalloc_node(sizeof(struct sched_entity), |
| 5557 | GFP_KERNEL, cpu_to_node(i)); |
| 5558 | if (!se) |
| 5559 | goto err_free_rq; |
| 5560 | |
| 5561 | init_cfs_rq(cfs_rq); |
| 5562 | init_tg_cfs_entry(tg, cfs_rq, se, i, parent->se[i]); |
| 5563 | } |
| 5564 | |
| 5565 | return 1; |
| 5566 | |
| 5567 | err_free_rq: |
| 5568 | kfree(cfs_rq); |
| 5569 | err: |
| 5570 | return 0; |
| 5571 | } |
| 5572 | |
| 5573 | void unregister_fair_sched_group(struct task_group *tg, int cpu) |
| 5574 | { |
| 5575 | struct rq *rq = cpu_rq(cpu); |
| 5576 | unsigned long flags; |
| 5577 | |
| 5578 | /* |
| 5579 | * Only empty task groups can be destroyed; so we can speculatively |
| 5580 | * check on_list without danger of it being re-added. |
| 5581 | */ |
| 5582 | if (!tg->cfs_rq[cpu]->on_list) |
| 5583 | return; |
| 5584 | |
| 5585 | raw_spin_lock_irqsave(&rq->lock, flags); |
| 5586 | list_del_leaf_cfs_rq(tg->cfs_rq[cpu]); |
| 5587 | raw_spin_unlock_irqrestore(&rq->lock, flags); |
| 5588 | } |
| 5589 | |
| 5590 | void init_tg_cfs_entry(struct task_group *tg, struct cfs_rq *cfs_rq, |
| 5591 | struct sched_entity *se, int cpu, |
| 5592 | struct sched_entity *parent) |
| 5593 | { |
| 5594 | struct rq *rq = cpu_rq(cpu); |
| 5595 | |
| 5596 | cfs_rq->tg = tg; |
| 5597 | cfs_rq->rq = rq; |
| 5598 | #ifdef CONFIG_SMP |
| 5599 | /* allow initial update_cfs_load() to truncate */ |
| 5600 | cfs_rq->load_stamp = 1; |
Peter Zijlstra | 810b381 | 2008-02-29 15:21:01 -0500 | [diff] [blame] | 5601 | #endif |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 5602 | init_cfs_rq_runtime(cfs_rq); |
| 5603 | |
| 5604 | tg->cfs_rq[cpu] = cfs_rq; |
| 5605 | tg->se[cpu] = se; |
| 5606 | |
| 5607 | /* se could be NULL for root_task_group */ |
| 5608 | if (!se) |
| 5609 | return; |
| 5610 | |
| 5611 | if (!parent) |
| 5612 | se->cfs_rq = &rq->cfs; |
| 5613 | else |
| 5614 | se->cfs_rq = parent->my_q; |
| 5615 | |
| 5616 | se->my_q = cfs_rq; |
| 5617 | update_load_set(&se->load, 0); |
| 5618 | se->parent = parent; |
| 5619 | } |
| 5620 | |
| 5621 | static DEFINE_MUTEX(shares_mutex); |
| 5622 | |
| 5623 | int sched_group_set_shares(struct task_group *tg, unsigned long shares) |
| 5624 | { |
| 5625 | int i; |
| 5626 | unsigned long flags; |
| 5627 | |
| 5628 | /* |
| 5629 | * We can't change the weight of the root cgroup. |
| 5630 | */ |
| 5631 | if (!tg->se[0]) |
| 5632 | return -EINVAL; |
| 5633 | |
| 5634 | shares = clamp(shares, scale_load(MIN_SHARES), scale_load(MAX_SHARES)); |
| 5635 | |
| 5636 | mutex_lock(&shares_mutex); |
| 5637 | if (tg->shares == shares) |
| 5638 | goto done; |
| 5639 | |
| 5640 | tg->shares = shares; |
| 5641 | for_each_possible_cpu(i) { |
| 5642 | struct rq *rq = cpu_rq(i); |
| 5643 | struct sched_entity *se; |
| 5644 | |
| 5645 | se = tg->se[i]; |
| 5646 | /* Propagate contribution to hierarchy */ |
| 5647 | raw_spin_lock_irqsave(&rq->lock, flags); |
| 5648 | for_each_sched_entity(se) |
| 5649 | update_cfs_shares(group_cfs_rq(se)); |
| 5650 | raw_spin_unlock_irqrestore(&rq->lock, flags); |
| 5651 | } |
| 5652 | |
| 5653 | done: |
| 5654 | mutex_unlock(&shares_mutex); |
| 5655 | return 0; |
| 5656 | } |
| 5657 | #else /* CONFIG_FAIR_GROUP_SCHED */ |
| 5658 | |
| 5659 | void free_fair_sched_group(struct task_group *tg) { } |
| 5660 | |
| 5661 | int alloc_fair_sched_group(struct task_group *tg, struct task_group *parent) |
| 5662 | { |
| 5663 | return 1; |
| 5664 | } |
| 5665 | |
| 5666 | void unregister_fair_sched_group(struct task_group *tg, int cpu) { } |
| 5667 | |
| 5668 | #endif /* CONFIG_FAIR_GROUP_SCHED */ |
| 5669 | |
Peter Zijlstra | 810b381 | 2008-02-29 15:21:01 -0500 | [diff] [blame] | 5670 | |
H Hartley Sweeten | 6d686f4 | 2010-01-13 20:21:52 -0700 | [diff] [blame] | 5671 | 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] | 5672 | { |
| 5673 | struct sched_entity *se = &task->se; |
Peter Williams | 0d721ce | 2009-09-21 01:31:53 +0000 | [diff] [blame] | 5674 | unsigned int rr_interval = 0; |
| 5675 | |
| 5676 | /* |
| 5677 | * Time slice is 0 for SCHED_OTHER tasks that are on an otherwise |
| 5678 | * idle runqueue: |
| 5679 | */ |
Peter Williams | 0d721ce | 2009-09-21 01:31:53 +0000 | [diff] [blame] | 5680 | if (rq->cfs.load.weight) |
| 5681 | rr_interval = NS_TO_JIFFIES(sched_slice(&rq->cfs, se)); |
Peter Williams | 0d721ce | 2009-09-21 01:31:53 +0000 | [diff] [blame] | 5682 | |
| 5683 | return rr_interval; |
| 5684 | } |
| 5685 | |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 5686 | /* |
| 5687 | * All the scheduling class methods: |
| 5688 | */ |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 5689 | const struct sched_class fair_sched_class = { |
Ingo Molnar | 5522d5d | 2007-10-15 17:00:12 +0200 | [diff] [blame] | 5690 | .next = &idle_sched_class, |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 5691 | .enqueue_task = enqueue_task_fair, |
| 5692 | .dequeue_task = dequeue_task_fair, |
| 5693 | .yield_task = yield_task_fair, |
Mike Galbraith | d95f412 | 2011-02-01 09:50:51 -0500 | [diff] [blame] | 5694 | .yield_to_task = yield_to_task_fair, |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 5695 | |
Ingo Molnar | 2e09bf5 | 2007-10-15 17:00:05 +0200 | [diff] [blame] | 5696 | .check_preempt_curr = check_preempt_wakeup, |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 5697 | |
| 5698 | .pick_next_task = pick_next_task_fair, |
| 5699 | .put_prev_task = put_prev_task_fair, |
| 5700 | |
Peter Williams | 681f3e6 | 2007-10-24 18:23:51 +0200 | [diff] [blame] | 5701 | #ifdef CONFIG_SMP |
Li Zefan | 4ce72a2 | 2008-10-22 15:25:26 +0800 | [diff] [blame] | 5702 | .select_task_rq = select_task_rq_fair, |
Paul Turner | 0a74bef | 2012-10-04 13:18:30 +0200 | [diff] [blame] | 5703 | .migrate_task_rq = migrate_task_rq_fair, |
Li Zefan | 4ce72a2 | 2008-10-22 15:25:26 +0800 | [diff] [blame] | 5704 | |
Christian Ehrhardt | 0bcdcf2 | 2009-11-30 12:16:46 +0100 | [diff] [blame] | 5705 | .rq_online = rq_online_fair, |
| 5706 | .rq_offline = rq_offline_fair, |
Peter Zijlstra | 88ec22d | 2009-12-16 18:04:41 +0100 | [diff] [blame] | 5707 | |
| 5708 | .task_waking = task_waking_fair, |
Peter Williams | 681f3e6 | 2007-10-24 18:23:51 +0200 | [diff] [blame] | 5709 | #endif |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 5710 | |
Srivatsa Vaddagiri | 83b699e | 2007-10-15 17:00:08 +0200 | [diff] [blame] | 5711 | .set_curr_task = set_curr_task_fair, |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 5712 | .task_tick = task_tick_fair, |
Peter Zijlstra | cd29fe6 | 2009-11-27 17:32:46 +0100 | [diff] [blame] | 5713 | .task_fork = task_fork_fair, |
Steven Rostedt | cb46984 | 2008-01-25 21:08:22 +0100 | [diff] [blame] | 5714 | |
| 5715 | .prio_changed = prio_changed_fair, |
Peter Zijlstra | da7a735 | 2011-01-17 17:03:27 +0100 | [diff] [blame] | 5716 | .switched_from = switched_from_fair, |
Steven Rostedt | cb46984 | 2008-01-25 21:08:22 +0100 | [diff] [blame] | 5717 | .switched_to = switched_to_fair, |
Peter Zijlstra | 810b381 | 2008-02-29 15:21:01 -0500 | [diff] [blame] | 5718 | |
Peter Williams | 0d721ce | 2009-09-21 01:31:53 +0000 | [diff] [blame] | 5719 | .get_rr_interval = get_rr_interval_fair, |
| 5720 | |
Peter Zijlstra | 810b381 | 2008-02-29 15:21:01 -0500 | [diff] [blame] | 5721 | #ifdef CONFIG_FAIR_GROUP_SCHED |
Peter Zijlstra | b2b5ce0 | 2010-10-15 15:24:15 +0200 | [diff] [blame] | 5722 | .task_move_group = task_move_group_fair, |
Peter Zijlstra | 810b381 | 2008-02-29 15:21:01 -0500 | [diff] [blame] | 5723 | #endif |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 5724 | }; |
| 5725 | |
| 5726 | #ifdef CONFIG_SCHED_DEBUG |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 5727 | void print_cfs_stats(struct seq_file *m, int cpu) |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 5728 | { |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 5729 | struct cfs_rq *cfs_rq; |
| 5730 | |
Peter Zijlstra | 5973e5b | 2008-01-25 21:08:34 +0100 | [diff] [blame] | 5731 | rcu_read_lock(); |
Ingo Molnar | c3b64f1 | 2007-08-09 11:16:51 +0200 | [diff] [blame] | 5732 | for_each_leaf_cfs_rq(cpu_rq(cpu), cfs_rq) |
Ingo Molnar | 5cef9ec | 2007-08-09 11:16:47 +0200 | [diff] [blame] | 5733 | print_cfs_rq(m, cpu, cfs_rq); |
Peter Zijlstra | 5973e5b | 2008-01-25 21:08:34 +0100 | [diff] [blame] | 5734 | rcu_read_unlock(); |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 5735 | } |
| 5736 | #endif |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 5737 | |
| 5738 | __init void init_sched_fair_class(void) |
| 5739 | { |
| 5740 | #ifdef CONFIG_SMP |
| 5741 | open_softirq(SCHED_SOFTIRQ, run_rebalance_domains); |
| 5742 | |
| 5743 | #ifdef CONFIG_NO_HZ |
Diwakar Tundlam | 554ceca | 2012-03-07 14:44:26 -0800 | [diff] [blame] | 5744 | nohz.next_balance = jiffies; |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 5745 | zalloc_cpumask_var(&nohz.idle_cpus_mask, GFP_NOWAIT); |
Suresh Siddha | 7132596 | 2012-01-19 18:28:57 -0800 | [diff] [blame] | 5746 | cpu_notifier(sched_ilb_notifier, 0); |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 5747 | #endif |
| 5748 | #endif /* SMP */ |
| 5749 | |
| 5750 | } |