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 | 6d0f0ebd | 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 | |
| 661 | /* |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 662 | * Update the current task's runtime statistics. Skip current tasks that |
| 663 | * are not in our scheduling class. |
| 664 | */ |
| 665 | static inline void |
Ingo Molnar | 8ebc91d | 2007-10-15 17:00:03 +0200 | [diff] [blame] | 666 | __update_curr(struct cfs_rq *cfs_rq, struct sched_entity *curr, |
| 667 | unsigned long delta_exec) |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 668 | { |
Ingo Molnar | bbdba7c | 2007-10-15 17:00:06 +0200 | [diff] [blame] | 669 | unsigned long delta_exec_weighted; |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 670 | |
Lucas De Marchi | 41acab8 | 2010-03-10 23:37:45 -0300 | [diff] [blame] | 671 | schedstat_set(curr->statistics.exec_max, |
| 672 | max((u64)delta_exec, curr->statistics.exec_max)); |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 673 | |
| 674 | curr->sum_exec_runtime += delta_exec; |
Ingo Molnar | 7a62eab | 2007-10-15 17:00:06 +0200 | [diff] [blame] | 675 | schedstat_add(cfs_rq, exec_clock, delta_exec); |
Peter Zijlstra | a7be37a | 2008-06-27 13:41:11 +0200 | [diff] [blame] | 676 | delta_exec_weighted = calc_delta_fair(delta_exec, curr); |
Peter Zijlstra | 88ec22d | 2009-12-16 18:04:41 +0100 | [diff] [blame] | 677 | |
Ingo Molnar | e9acbff | 2007-10-15 17:00:04 +0200 | [diff] [blame] | 678 | curr->vruntime += delta_exec_weighted; |
Peter Zijlstra | 1af5f73 | 2008-10-24 11:06:13 +0200 | [diff] [blame] | 679 | update_min_vruntime(cfs_rq); |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 680 | } |
| 681 | |
Ingo Molnar | b7cc089 | 2007-08-09 11:16:47 +0200 | [diff] [blame] | 682 | static void update_curr(struct cfs_rq *cfs_rq) |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 683 | { |
Ingo Molnar | 429d43b | 2007-10-15 17:00:03 +0200 | [diff] [blame] | 684 | struct sched_entity *curr = cfs_rq->curr; |
Venkatesh Pallipadi | 305e683 | 2010-10-04 17:03:21 -0700 | [diff] [blame] | 685 | u64 now = rq_of(cfs_rq)->clock_task; |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 686 | unsigned long delta_exec; |
| 687 | |
| 688 | if (unlikely(!curr)) |
| 689 | return; |
| 690 | |
| 691 | /* |
| 692 | * Get the amount of time the current task was running |
| 693 | * since the last time we changed load (this cannot |
| 694 | * overflow on 32 bits): |
| 695 | */ |
Ingo Molnar | 8ebc91d | 2007-10-15 17:00:03 +0200 | [diff] [blame] | 696 | delta_exec = (unsigned long)(now - curr->exec_start); |
Peter Zijlstra | 34f28ec | 2008-12-16 08:45:31 +0100 | [diff] [blame] | 697 | if (!delta_exec) |
| 698 | return; |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 699 | |
Ingo Molnar | 8ebc91d | 2007-10-15 17:00:03 +0200 | [diff] [blame] | 700 | __update_curr(cfs_rq, curr, delta_exec); |
| 701 | curr->exec_start = now; |
Srivatsa Vaddagiri | d842de8 | 2007-12-02 20:04:49 +0100 | [diff] [blame] | 702 | |
| 703 | if (entity_is_task(curr)) { |
| 704 | struct task_struct *curtask = task_of(curr); |
| 705 | |
Ingo Molnar | f977bb4 | 2009-09-13 18:15:54 +0200 | [diff] [blame] | 706 | trace_sched_stat_runtime(curtask, delta_exec, curr->vruntime); |
Srivatsa Vaddagiri | d842de8 | 2007-12-02 20:04:49 +0100 | [diff] [blame] | 707 | cpuacct_charge(curtask, delta_exec); |
Frank Mayhar | f06febc | 2008-09-12 09:54:39 -0700 | [diff] [blame] | 708 | account_group_exec_runtime(curtask, delta_exec); |
Srivatsa Vaddagiri | d842de8 | 2007-12-02 20:04:49 +0100 | [diff] [blame] | 709 | } |
Paul Turner | ec12cb7 | 2011-07-21 09:43:30 -0700 | [diff] [blame] | 710 | |
| 711 | account_cfs_rq_runtime(cfs_rq, delta_exec); |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 712 | } |
| 713 | |
| 714 | static inline void |
Ingo Molnar | 5870db5 | 2007-08-09 11:16:47 +0200 | [diff] [blame] | 715 | 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] | 716 | { |
Lucas De Marchi | 41acab8 | 2010-03-10 23:37:45 -0300 | [diff] [blame] | 717 | schedstat_set(se->statistics.wait_start, rq_of(cfs_rq)->clock); |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 718 | } |
| 719 | |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 720 | /* |
| 721 | * Task is being enqueued - update stats: |
| 722 | */ |
Ingo Molnar | d2417e5 | 2007-08-09 11:16:47 +0200 | [diff] [blame] | 723 | 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] | 724 | { |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 725 | /* |
| 726 | * Are we enqueueing a waiting task? (for current tasks |
| 727 | * a dequeue/enqueue event is a NOP) |
| 728 | */ |
Ingo Molnar | 429d43b | 2007-10-15 17:00:03 +0200 | [diff] [blame] | 729 | if (se != cfs_rq->curr) |
Ingo Molnar | 5870db5 | 2007-08-09 11:16:47 +0200 | [diff] [blame] | 730 | update_stats_wait_start(cfs_rq, se); |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 731 | } |
| 732 | |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 733 | static void |
Ingo Molnar | 9ef0a96 | 2007-08-09 11:16:47 +0200 | [diff] [blame] | 734 | 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] | 735 | { |
Lucas De Marchi | 41acab8 | 2010-03-10 23:37:45 -0300 | [diff] [blame] | 736 | schedstat_set(se->statistics.wait_max, max(se->statistics.wait_max, |
| 737 | rq_of(cfs_rq)->clock - se->statistics.wait_start)); |
| 738 | schedstat_set(se->statistics.wait_count, se->statistics.wait_count + 1); |
| 739 | schedstat_set(se->statistics.wait_sum, se->statistics.wait_sum + |
| 740 | rq_of(cfs_rq)->clock - se->statistics.wait_start); |
Peter Zijlstra | 768d0c2 | 2009-07-23 20:13:26 +0200 | [diff] [blame] | 741 | #ifdef CONFIG_SCHEDSTATS |
| 742 | if (entity_is_task(se)) { |
| 743 | trace_sched_stat_wait(task_of(se), |
Lucas De Marchi | 41acab8 | 2010-03-10 23:37:45 -0300 | [diff] [blame] | 744 | rq_of(cfs_rq)->clock - se->statistics.wait_start); |
Peter Zijlstra | 768d0c2 | 2009-07-23 20:13:26 +0200 | [diff] [blame] | 745 | } |
| 746 | #endif |
Lucas De Marchi | 41acab8 | 2010-03-10 23:37:45 -0300 | [diff] [blame] | 747 | schedstat_set(se->statistics.wait_start, 0); |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 748 | } |
| 749 | |
| 750 | static inline void |
Ingo Molnar | 19b6a2e | 2007-08-09 11:16:48 +0200 | [diff] [blame] | 751 | update_stats_dequeue(struct cfs_rq *cfs_rq, struct sched_entity *se) |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 752 | { |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 753 | /* |
| 754 | * Mark the end of the wait period if dequeueing a |
| 755 | * waiting task: |
| 756 | */ |
Ingo Molnar | 429d43b | 2007-10-15 17:00:03 +0200 | [diff] [blame] | 757 | if (se != cfs_rq->curr) |
Ingo Molnar | 9ef0a96 | 2007-08-09 11:16:47 +0200 | [diff] [blame] | 758 | update_stats_wait_end(cfs_rq, se); |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 759 | } |
| 760 | |
| 761 | /* |
| 762 | * We are picking a new current task - update its stats: |
| 763 | */ |
| 764 | static inline void |
Ingo Molnar | 79303e9 | 2007-08-09 11:16:47 +0200 | [diff] [blame] | 765 | 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] | 766 | { |
| 767 | /* |
| 768 | * We are starting a new run period: |
| 769 | */ |
Venkatesh Pallipadi | 305e683 | 2010-10-04 17:03:21 -0700 | [diff] [blame] | 770 | se->exec_start = rq_of(cfs_rq)->clock_task; |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 771 | } |
| 772 | |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 773 | /************************************************** |
| 774 | * Scheduling class queueing methods: |
| 775 | */ |
| 776 | |
Dmitry Adamushko | 30cfdcf | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 777 | static void |
| 778 | account_entity_enqueue(struct cfs_rq *cfs_rq, struct sched_entity *se) |
| 779 | { |
| 780 | update_load_add(&cfs_rq->load, se->load.weight); |
Peter Zijlstra | c09595f | 2008-06-27 13:41:14 +0200 | [diff] [blame] | 781 | if (!parent_entity(se)) |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 782 | update_load_add(&rq_of(cfs_rq)->load, se->load.weight); |
Peter Zijlstra | 367456c | 2012-02-20 21:49:09 +0100 | [diff] [blame] | 783 | #ifdef CONFIG_SMP |
| 784 | if (entity_is_task(se)) |
Peter Zijlstra | eb95308 | 2012-04-17 13:38:40 +0200 | [diff] [blame] | 785 | list_add(&se->group_node, &rq_of(cfs_rq)->cfs_tasks); |
Peter Zijlstra | 367456c | 2012-02-20 21:49:09 +0100 | [diff] [blame] | 786 | #endif |
Dmitry Adamushko | 30cfdcf | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 787 | cfs_rq->nr_running++; |
Dmitry Adamushko | 30cfdcf | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 788 | } |
| 789 | |
| 790 | static void |
| 791 | account_entity_dequeue(struct cfs_rq *cfs_rq, struct sched_entity *se) |
| 792 | { |
| 793 | update_load_sub(&cfs_rq->load, se->load.weight); |
Peter Zijlstra | c09595f | 2008-06-27 13:41:14 +0200 | [diff] [blame] | 794 | if (!parent_entity(se)) |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 795 | update_load_sub(&rq_of(cfs_rq)->load, se->load.weight); |
Peter Zijlstra | 367456c | 2012-02-20 21:49:09 +0100 | [diff] [blame] | 796 | if (entity_is_task(se)) |
Bharata B Rao | b87f172 | 2008-09-25 09:53:54 +0530 | [diff] [blame] | 797 | list_del_init(&se->group_node); |
Dmitry Adamushko | 30cfdcf | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 798 | cfs_rq->nr_running--; |
Dmitry Adamushko | 30cfdcf | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 799 | } |
| 800 | |
Yong Zhang | 3ff6dca | 2011-01-24 15:33:52 +0800 | [diff] [blame] | 801 | #ifdef CONFIG_FAIR_GROUP_SCHED |
| 802 | # ifdef CONFIG_SMP |
Peter Zijlstra | cf5f0ac | 2011-10-13 16:52:28 +0200 | [diff] [blame] | 803 | static inline long calc_tg_weight(struct task_group *tg, struct cfs_rq *cfs_rq) |
| 804 | { |
| 805 | long tg_weight; |
| 806 | |
| 807 | /* |
| 808 | * Use this CPU's actual weight instead of the last load_contribution |
| 809 | * to gain a more accurate current total weight. See |
| 810 | * update_cfs_rq_load_contribution(). |
| 811 | */ |
Paul Turner | 8295836 | 2012-10-04 13:18:31 +0200 | [diff] [blame^] | 812 | tg_weight = atomic64_read(&tg->load_avg); |
| 813 | tg_weight -= cfs_rq->tg_load_contrib; |
Peter Zijlstra | cf5f0ac | 2011-10-13 16:52:28 +0200 | [diff] [blame] | 814 | tg_weight += cfs_rq->load.weight; |
| 815 | |
| 816 | return tg_weight; |
| 817 | } |
| 818 | |
Paul Turner | 6d5ab29 | 2011-01-21 20:45:01 -0800 | [diff] [blame] | 819 | 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] | 820 | { |
Peter Zijlstra | cf5f0ac | 2011-10-13 16:52:28 +0200 | [diff] [blame] | 821 | long tg_weight, load, shares; |
Yong Zhang | 3ff6dca | 2011-01-24 15:33:52 +0800 | [diff] [blame] | 822 | |
Peter Zijlstra | cf5f0ac | 2011-10-13 16:52:28 +0200 | [diff] [blame] | 823 | tg_weight = calc_tg_weight(tg, cfs_rq); |
Paul Turner | 6d5ab29 | 2011-01-21 20:45:01 -0800 | [diff] [blame] | 824 | load = cfs_rq->load.weight; |
Yong Zhang | 3ff6dca | 2011-01-24 15:33:52 +0800 | [diff] [blame] | 825 | |
Yong Zhang | 3ff6dca | 2011-01-24 15:33:52 +0800 | [diff] [blame] | 826 | shares = (tg->shares * load); |
Peter Zijlstra | cf5f0ac | 2011-10-13 16:52:28 +0200 | [diff] [blame] | 827 | if (tg_weight) |
| 828 | shares /= tg_weight; |
Yong Zhang | 3ff6dca | 2011-01-24 15:33:52 +0800 | [diff] [blame] | 829 | |
| 830 | if (shares < MIN_SHARES) |
| 831 | shares = MIN_SHARES; |
| 832 | if (shares > tg->shares) |
| 833 | shares = tg->shares; |
| 834 | |
| 835 | return shares; |
| 836 | } |
Yong Zhang | 3ff6dca | 2011-01-24 15:33:52 +0800 | [diff] [blame] | 837 | # else /* CONFIG_SMP */ |
Paul Turner | 6d5ab29 | 2011-01-21 20:45:01 -0800 | [diff] [blame] | 838 | 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] | 839 | { |
| 840 | return tg->shares; |
| 841 | } |
Yong Zhang | 3ff6dca | 2011-01-24 15:33:52 +0800 | [diff] [blame] | 842 | # endif /* CONFIG_SMP */ |
Peter Zijlstra | 2069dd7 | 2010-11-15 15:47:00 -0800 | [diff] [blame] | 843 | static void reweight_entity(struct cfs_rq *cfs_rq, struct sched_entity *se, |
| 844 | unsigned long weight) |
| 845 | { |
Paul Turner | 19e5eeb | 2010-12-15 19:10:18 -0800 | [diff] [blame] | 846 | if (se->on_rq) { |
| 847 | /* commit outstanding execution time */ |
| 848 | if (cfs_rq->curr == se) |
| 849 | update_curr(cfs_rq); |
Peter Zijlstra | 2069dd7 | 2010-11-15 15:47:00 -0800 | [diff] [blame] | 850 | account_entity_dequeue(cfs_rq, se); |
Paul Turner | 19e5eeb | 2010-12-15 19:10:18 -0800 | [diff] [blame] | 851 | } |
Peter Zijlstra | 2069dd7 | 2010-11-15 15:47:00 -0800 | [diff] [blame] | 852 | |
| 853 | update_load_set(&se->load, weight); |
| 854 | |
| 855 | if (se->on_rq) |
| 856 | account_entity_enqueue(cfs_rq, se); |
| 857 | } |
| 858 | |
Paul Turner | 8295836 | 2012-10-04 13:18:31 +0200 | [diff] [blame^] | 859 | static inline int throttled_hierarchy(struct cfs_rq *cfs_rq); |
| 860 | |
Paul Turner | 6d5ab29 | 2011-01-21 20:45:01 -0800 | [diff] [blame] | 861 | static void update_cfs_shares(struct cfs_rq *cfs_rq) |
Peter Zijlstra | 2069dd7 | 2010-11-15 15:47:00 -0800 | [diff] [blame] | 862 | { |
| 863 | struct task_group *tg; |
| 864 | struct sched_entity *se; |
Yong Zhang | 3ff6dca | 2011-01-24 15:33:52 +0800 | [diff] [blame] | 865 | long shares; |
Peter Zijlstra | 2069dd7 | 2010-11-15 15:47:00 -0800 | [diff] [blame] | 866 | |
Peter Zijlstra | 2069dd7 | 2010-11-15 15:47:00 -0800 | [diff] [blame] | 867 | tg = cfs_rq->tg; |
| 868 | se = tg->se[cpu_of(rq_of(cfs_rq))]; |
Paul Turner | 64660c8 | 2011-07-21 09:43:36 -0700 | [diff] [blame] | 869 | if (!se || throttled_hierarchy(cfs_rq)) |
Peter Zijlstra | 2069dd7 | 2010-11-15 15:47:00 -0800 | [diff] [blame] | 870 | return; |
Yong Zhang | 3ff6dca | 2011-01-24 15:33:52 +0800 | [diff] [blame] | 871 | #ifndef CONFIG_SMP |
| 872 | if (likely(se->load.weight == tg->shares)) |
| 873 | return; |
| 874 | #endif |
Paul Turner | 6d5ab29 | 2011-01-21 20:45:01 -0800 | [diff] [blame] | 875 | shares = calc_cfs_shares(cfs_rq, tg); |
Peter Zijlstra | 2069dd7 | 2010-11-15 15:47:00 -0800 | [diff] [blame] | 876 | |
| 877 | reweight_entity(cfs_rq_of(se), se, shares); |
| 878 | } |
| 879 | #else /* CONFIG_FAIR_GROUP_SCHED */ |
Paul Turner | 6d5ab29 | 2011-01-21 20:45:01 -0800 | [diff] [blame] | 880 | static inline void update_cfs_shares(struct cfs_rq *cfs_rq) |
Peter Zijlstra | 2069dd7 | 2010-11-15 15:47:00 -0800 | [diff] [blame] | 881 | { |
| 882 | } |
| 883 | #endif /* CONFIG_FAIR_GROUP_SCHED */ |
| 884 | |
Paul Turner | 9d85f21 | 2012-10-04 13:18:29 +0200 | [diff] [blame] | 885 | #ifdef CONFIG_SMP |
| 886 | /* |
| 887 | * Approximate: |
| 888 | * val * y^n, where y^32 ~= 0.5 (~1 scheduling period) |
| 889 | */ |
| 890 | static __always_inline u64 decay_load(u64 val, u64 n) |
| 891 | { |
| 892 | for (; n && val; n--) { |
| 893 | val *= 4008; |
| 894 | val >>= 12; |
| 895 | } |
| 896 | |
| 897 | return val; |
| 898 | } |
| 899 | |
| 900 | /* |
| 901 | * We can represent the historical contribution to runnable average as the |
| 902 | * coefficients of a geometric series. To do this we sub-divide our runnable |
| 903 | * history into segments of approximately 1ms (1024us); label the segment that |
| 904 | * occurred N-ms ago p_N, with p_0 corresponding to the current period, e.g. |
| 905 | * |
| 906 | * [<- 1024us ->|<- 1024us ->|<- 1024us ->| ... |
| 907 | * p0 p1 p2 |
| 908 | * (now) (~1ms ago) (~2ms ago) |
| 909 | * |
| 910 | * Let u_i denote the fraction of p_i that the entity was runnable. |
| 911 | * |
| 912 | * We then designate the fractions u_i as our co-efficients, yielding the |
| 913 | * following representation of historical load: |
| 914 | * u_0 + u_1*y + u_2*y^2 + u_3*y^3 + ... |
| 915 | * |
| 916 | * We choose y based on the with of a reasonably scheduling period, fixing: |
| 917 | * y^32 = 0.5 |
| 918 | * |
| 919 | * This means that the contribution to load ~32ms ago (u_32) will be weighted |
| 920 | * approximately half as much as the contribution to load within the last ms |
| 921 | * (u_0). |
| 922 | * |
| 923 | * When a period "rolls over" and we have new u_0`, multiplying the previous |
| 924 | * sum again by y is sufficient to update: |
| 925 | * load_avg = u_0` + y*(u_0 + u_1*y + u_2*y^2 + ... ) |
| 926 | * = u_0 + u_1*y + u_2*y^2 + ... [re-labeling u_i --> u_{i+1}] |
| 927 | */ |
| 928 | static __always_inline int __update_entity_runnable_avg(u64 now, |
| 929 | struct sched_avg *sa, |
| 930 | int runnable) |
| 931 | { |
| 932 | u64 delta; |
| 933 | int delta_w, decayed = 0; |
| 934 | |
| 935 | delta = now - sa->last_runnable_update; |
| 936 | /* |
| 937 | * This should only happen when time goes backwards, which it |
| 938 | * unfortunately does during sched clock init when we swap over to TSC. |
| 939 | */ |
| 940 | if ((s64)delta < 0) { |
| 941 | sa->last_runnable_update = now; |
| 942 | return 0; |
| 943 | } |
| 944 | |
| 945 | /* |
| 946 | * Use 1024ns as the unit of measurement since it's a reasonable |
| 947 | * approximation of 1us and fast to compute. |
| 948 | */ |
| 949 | delta >>= 10; |
| 950 | if (!delta) |
| 951 | return 0; |
| 952 | sa->last_runnable_update = now; |
| 953 | |
| 954 | /* delta_w is the amount already accumulated against our next period */ |
| 955 | delta_w = sa->runnable_avg_period % 1024; |
| 956 | if (delta + delta_w >= 1024) { |
| 957 | /* period roll-over */ |
| 958 | decayed = 1; |
| 959 | |
| 960 | /* |
| 961 | * Now that we know we're crossing a period boundary, figure |
| 962 | * out how much from delta we need to complete the current |
| 963 | * period and accrue it. |
| 964 | */ |
| 965 | delta_w = 1024 - delta_w; |
| 966 | BUG_ON(delta_w > delta); |
| 967 | do { |
| 968 | if (runnable) |
| 969 | sa->runnable_avg_sum += delta_w; |
| 970 | sa->runnable_avg_period += delta_w; |
| 971 | |
| 972 | /* |
| 973 | * Remainder of delta initiates a new period, roll over |
| 974 | * the previous. |
| 975 | */ |
| 976 | sa->runnable_avg_sum = |
| 977 | decay_load(sa->runnable_avg_sum, 1); |
| 978 | sa->runnable_avg_period = |
| 979 | decay_load(sa->runnable_avg_period, 1); |
| 980 | |
| 981 | delta -= delta_w; |
| 982 | /* New period is empty */ |
| 983 | delta_w = 1024; |
| 984 | } while (delta >= 1024); |
| 985 | } |
| 986 | |
| 987 | /* Remainder of delta accrued against u_0` */ |
| 988 | if (runnable) |
| 989 | sa->runnable_avg_sum += delta; |
| 990 | sa->runnable_avg_period += delta; |
| 991 | |
| 992 | return decayed; |
| 993 | } |
| 994 | |
Paul Turner | 9ee474f | 2012-10-04 13:18:30 +0200 | [diff] [blame] | 995 | /* Synchronize an entity's decay with its parenting cfs_rq.*/ |
Paul Turner | aff3e49 | 2012-10-04 13:18:30 +0200 | [diff] [blame] | 996 | static inline u64 __synchronize_entity_decay(struct sched_entity *se) |
Paul Turner | 9ee474f | 2012-10-04 13:18:30 +0200 | [diff] [blame] | 997 | { |
| 998 | struct cfs_rq *cfs_rq = cfs_rq_of(se); |
| 999 | u64 decays = atomic64_read(&cfs_rq->decay_counter); |
| 1000 | |
| 1001 | decays -= se->avg.decay_count; |
| 1002 | if (!decays) |
Paul Turner | aff3e49 | 2012-10-04 13:18:30 +0200 | [diff] [blame] | 1003 | return 0; |
Paul Turner | 9ee474f | 2012-10-04 13:18:30 +0200 | [diff] [blame] | 1004 | |
| 1005 | se->avg.load_avg_contrib = decay_load(se->avg.load_avg_contrib, decays); |
| 1006 | se->avg.decay_count = 0; |
Paul Turner | aff3e49 | 2012-10-04 13:18:30 +0200 | [diff] [blame] | 1007 | |
| 1008 | return decays; |
Paul Turner | 9ee474f | 2012-10-04 13:18:30 +0200 | [diff] [blame] | 1009 | } |
| 1010 | |
Paul Turner | c566e8e | 2012-10-04 13:18:30 +0200 | [diff] [blame] | 1011 | #ifdef CONFIG_FAIR_GROUP_SCHED |
| 1012 | static inline void __update_cfs_rq_tg_load_contrib(struct cfs_rq *cfs_rq, |
| 1013 | int force_update) |
| 1014 | { |
| 1015 | struct task_group *tg = cfs_rq->tg; |
| 1016 | s64 tg_contrib; |
| 1017 | |
| 1018 | tg_contrib = cfs_rq->runnable_load_avg + cfs_rq->blocked_load_avg; |
| 1019 | tg_contrib -= cfs_rq->tg_load_contrib; |
| 1020 | |
| 1021 | if (force_update || abs64(tg_contrib) > cfs_rq->tg_load_contrib / 8) { |
| 1022 | atomic64_add(tg_contrib, &tg->load_avg); |
| 1023 | cfs_rq->tg_load_contrib += tg_contrib; |
| 1024 | } |
| 1025 | } |
Paul Turner | 8165e14 | 2012-10-04 13:18:31 +0200 | [diff] [blame] | 1026 | |
Paul Turner | bb17f65 | 2012-10-04 13:18:31 +0200 | [diff] [blame] | 1027 | /* |
| 1028 | * Aggregate cfs_rq runnable averages into an equivalent task_group |
| 1029 | * representation for computing load contributions. |
| 1030 | */ |
| 1031 | static inline void __update_tg_runnable_avg(struct sched_avg *sa, |
| 1032 | struct cfs_rq *cfs_rq) |
| 1033 | { |
| 1034 | struct task_group *tg = cfs_rq->tg; |
| 1035 | long contrib; |
| 1036 | |
| 1037 | /* The fraction of a cpu used by this cfs_rq */ |
| 1038 | contrib = div_u64(sa->runnable_avg_sum << NICE_0_SHIFT, |
| 1039 | sa->runnable_avg_period + 1); |
| 1040 | contrib -= cfs_rq->tg_runnable_contrib; |
| 1041 | |
| 1042 | if (abs(contrib) > cfs_rq->tg_runnable_contrib / 64) { |
| 1043 | atomic_add(contrib, &tg->runnable_avg); |
| 1044 | cfs_rq->tg_runnable_contrib += contrib; |
| 1045 | } |
| 1046 | } |
| 1047 | |
Paul Turner | 8165e14 | 2012-10-04 13:18:31 +0200 | [diff] [blame] | 1048 | static inline void __update_group_entity_contrib(struct sched_entity *se) |
| 1049 | { |
| 1050 | struct cfs_rq *cfs_rq = group_cfs_rq(se); |
| 1051 | struct task_group *tg = cfs_rq->tg; |
Paul Turner | bb17f65 | 2012-10-04 13:18:31 +0200 | [diff] [blame] | 1052 | int runnable_avg; |
| 1053 | |
Paul Turner | 8165e14 | 2012-10-04 13:18:31 +0200 | [diff] [blame] | 1054 | u64 contrib; |
| 1055 | |
| 1056 | contrib = cfs_rq->tg_load_contrib * tg->shares; |
| 1057 | se->avg.load_avg_contrib = div64_u64(contrib, |
| 1058 | atomic64_read(&tg->load_avg) + 1); |
Paul Turner | bb17f65 | 2012-10-04 13:18:31 +0200 | [diff] [blame] | 1059 | |
| 1060 | /* |
| 1061 | * For group entities we need to compute a correction term in the case |
| 1062 | * that they are consuming <1 cpu so that we would contribute the same |
| 1063 | * load as a task of equal weight. |
| 1064 | * |
| 1065 | * Explicitly co-ordinating this measurement would be expensive, but |
| 1066 | * fortunately the sum of each cpus contribution forms a usable |
| 1067 | * lower-bound on the true value. |
| 1068 | * |
| 1069 | * Consider the aggregate of 2 contributions. Either they are disjoint |
| 1070 | * (and the sum represents true value) or they are disjoint and we are |
| 1071 | * understating by the aggregate of their overlap. |
| 1072 | * |
| 1073 | * Extending this to N cpus, for a given overlap, the maximum amount we |
| 1074 | * understand is then n_i(n_i+1)/2 * w_i where n_i is the number of |
| 1075 | * cpus that overlap for this interval and w_i is the interval width. |
| 1076 | * |
| 1077 | * On a small machine; the first term is well-bounded which bounds the |
| 1078 | * total error since w_i is a subset of the period. Whereas on a |
| 1079 | * larger machine, while this first term can be larger, if w_i is the |
| 1080 | * of consequential size guaranteed to see n_i*w_i quickly converge to |
| 1081 | * our upper bound of 1-cpu. |
| 1082 | */ |
| 1083 | runnable_avg = atomic_read(&tg->runnable_avg); |
| 1084 | if (runnable_avg < NICE_0_LOAD) { |
| 1085 | se->avg.load_avg_contrib *= runnable_avg; |
| 1086 | se->avg.load_avg_contrib >>= NICE_0_SHIFT; |
| 1087 | } |
Paul Turner | 8165e14 | 2012-10-04 13:18:31 +0200 | [diff] [blame] | 1088 | } |
Paul Turner | c566e8e | 2012-10-04 13:18:30 +0200 | [diff] [blame] | 1089 | #else |
| 1090 | static inline void __update_cfs_rq_tg_load_contrib(struct cfs_rq *cfs_rq, |
| 1091 | int force_update) {} |
Paul Turner | bb17f65 | 2012-10-04 13:18:31 +0200 | [diff] [blame] | 1092 | static inline void __update_tg_runnable_avg(struct sched_avg *sa, |
| 1093 | struct cfs_rq *cfs_rq) {} |
Paul Turner | 8165e14 | 2012-10-04 13:18:31 +0200 | [diff] [blame] | 1094 | static inline void __update_group_entity_contrib(struct sched_entity *se) {} |
Paul Turner | c566e8e | 2012-10-04 13:18:30 +0200 | [diff] [blame] | 1095 | #endif |
| 1096 | |
Paul Turner | 8165e14 | 2012-10-04 13:18:31 +0200 | [diff] [blame] | 1097 | static inline void __update_task_entity_contrib(struct sched_entity *se) |
| 1098 | { |
| 1099 | u32 contrib; |
| 1100 | |
| 1101 | /* avoid overflowing a 32-bit type w/ SCHED_LOAD_SCALE */ |
| 1102 | contrib = se->avg.runnable_avg_sum * scale_load_down(se->load.weight); |
| 1103 | contrib /= (se->avg.runnable_avg_period + 1); |
| 1104 | se->avg.load_avg_contrib = scale_load(contrib); |
| 1105 | } |
| 1106 | |
Paul Turner | 2dac754 | 2012-10-04 13:18:30 +0200 | [diff] [blame] | 1107 | /* Compute the current contribution to load_avg by se, return any delta */ |
| 1108 | static long __update_entity_load_avg_contrib(struct sched_entity *se) |
| 1109 | { |
| 1110 | long old_contrib = se->avg.load_avg_contrib; |
| 1111 | |
Paul Turner | 8165e14 | 2012-10-04 13:18:31 +0200 | [diff] [blame] | 1112 | if (entity_is_task(se)) { |
| 1113 | __update_task_entity_contrib(se); |
| 1114 | } else { |
Paul Turner | bb17f65 | 2012-10-04 13:18:31 +0200 | [diff] [blame] | 1115 | __update_tg_runnable_avg(&se->avg, group_cfs_rq(se)); |
Paul Turner | 8165e14 | 2012-10-04 13:18:31 +0200 | [diff] [blame] | 1116 | __update_group_entity_contrib(se); |
| 1117 | } |
Paul Turner | 2dac754 | 2012-10-04 13:18:30 +0200 | [diff] [blame] | 1118 | |
| 1119 | return se->avg.load_avg_contrib - old_contrib; |
| 1120 | } |
| 1121 | |
Paul Turner | 9ee474f | 2012-10-04 13:18:30 +0200 | [diff] [blame] | 1122 | static inline void subtract_blocked_load_contrib(struct cfs_rq *cfs_rq, |
| 1123 | long load_contrib) |
| 1124 | { |
| 1125 | if (likely(load_contrib < cfs_rq->blocked_load_avg)) |
| 1126 | cfs_rq->blocked_load_avg -= load_contrib; |
| 1127 | else |
| 1128 | cfs_rq->blocked_load_avg = 0; |
| 1129 | } |
| 1130 | |
Paul Turner | f1b1728 | 2012-10-04 13:18:31 +0200 | [diff] [blame] | 1131 | static inline u64 cfs_rq_clock_task(struct cfs_rq *cfs_rq); |
| 1132 | |
Paul Turner | 9d85f21 | 2012-10-04 13:18:29 +0200 | [diff] [blame] | 1133 | /* Update a sched_entity's runnable average */ |
Paul Turner | 9ee474f | 2012-10-04 13:18:30 +0200 | [diff] [blame] | 1134 | static inline void update_entity_load_avg(struct sched_entity *se, |
| 1135 | int update_cfs_rq) |
Paul Turner | 9d85f21 | 2012-10-04 13:18:29 +0200 | [diff] [blame] | 1136 | { |
Paul Turner | 2dac754 | 2012-10-04 13:18:30 +0200 | [diff] [blame] | 1137 | struct cfs_rq *cfs_rq = cfs_rq_of(se); |
| 1138 | long contrib_delta; |
Paul Turner | f1b1728 | 2012-10-04 13:18:31 +0200 | [diff] [blame] | 1139 | u64 now; |
Paul Turner | 2dac754 | 2012-10-04 13:18:30 +0200 | [diff] [blame] | 1140 | |
Paul Turner | f1b1728 | 2012-10-04 13:18:31 +0200 | [diff] [blame] | 1141 | /* |
| 1142 | * For a group entity we need to use their owned cfs_rq_clock_task() in |
| 1143 | * case they are the parent of a throttled hierarchy. |
| 1144 | */ |
| 1145 | if (entity_is_task(se)) |
| 1146 | now = cfs_rq_clock_task(cfs_rq); |
| 1147 | else |
| 1148 | now = cfs_rq_clock_task(group_cfs_rq(se)); |
| 1149 | |
| 1150 | if (!__update_entity_runnable_avg(now, &se->avg, se->on_rq)) |
Paul Turner | 2dac754 | 2012-10-04 13:18:30 +0200 | [diff] [blame] | 1151 | return; |
| 1152 | |
| 1153 | contrib_delta = __update_entity_load_avg_contrib(se); |
Paul Turner | 9ee474f | 2012-10-04 13:18:30 +0200 | [diff] [blame] | 1154 | |
| 1155 | if (!update_cfs_rq) |
| 1156 | return; |
| 1157 | |
Paul Turner | 2dac754 | 2012-10-04 13:18:30 +0200 | [diff] [blame] | 1158 | if (se->on_rq) |
| 1159 | cfs_rq->runnable_load_avg += contrib_delta; |
Paul Turner | 9ee474f | 2012-10-04 13:18:30 +0200 | [diff] [blame] | 1160 | else |
| 1161 | subtract_blocked_load_contrib(cfs_rq, -contrib_delta); |
| 1162 | } |
| 1163 | |
| 1164 | /* |
| 1165 | * Decay the load contributed by all blocked children and account this so that |
| 1166 | * their contribution may appropriately discounted when they wake up. |
| 1167 | */ |
Paul Turner | aff3e49 | 2012-10-04 13:18:30 +0200 | [diff] [blame] | 1168 | 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] | 1169 | { |
Paul Turner | f1b1728 | 2012-10-04 13:18:31 +0200 | [diff] [blame] | 1170 | u64 now = cfs_rq_clock_task(cfs_rq) >> 20; |
Paul Turner | 9ee474f | 2012-10-04 13:18:30 +0200 | [diff] [blame] | 1171 | u64 decays; |
| 1172 | |
| 1173 | decays = now - cfs_rq->last_decay; |
Paul Turner | aff3e49 | 2012-10-04 13:18:30 +0200 | [diff] [blame] | 1174 | if (!decays && !force_update) |
Paul Turner | 9ee474f | 2012-10-04 13:18:30 +0200 | [diff] [blame] | 1175 | return; |
| 1176 | |
Paul Turner | aff3e49 | 2012-10-04 13:18:30 +0200 | [diff] [blame] | 1177 | if (atomic64_read(&cfs_rq->removed_load)) { |
| 1178 | u64 removed_load = atomic64_xchg(&cfs_rq->removed_load, 0); |
| 1179 | subtract_blocked_load_contrib(cfs_rq, removed_load); |
| 1180 | } |
Paul Turner | 9ee474f | 2012-10-04 13:18:30 +0200 | [diff] [blame] | 1181 | |
Paul Turner | aff3e49 | 2012-10-04 13:18:30 +0200 | [diff] [blame] | 1182 | if (decays) { |
| 1183 | cfs_rq->blocked_load_avg = decay_load(cfs_rq->blocked_load_avg, |
| 1184 | decays); |
| 1185 | atomic64_add(decays, &cfs_rq->decay_counter); |
| 1186 | cfs_rq->last_decay = now; |
| 1187 | } |
Paul Turner | c566e8e | 2012-10-04 13:18:30 +0200 | [diff] [blame] | 1188 | |
| 1189 | __update_cfs_rq_tg_load_contrib(cfs_rq, force_update); |
Paul Turner | 9d85f21 | 2012-10-04 13:18:29 +0200 | [diff] [blame] | 1190 | } |
Ben Segall | 18bf280 | 2012-10-04 12:51:20 +0200 | [diff] [blame] | 1191 | |
| 1192 | static inline void update_rq_runnable_avg(struct rq *rq, int runnable) |
| 1193 | { |
| 1194 | __update_entity_runnable_avg(rq->clock_task, &rq->avg, runnable); |
Paul Turner | bb17f65 | 2012-10-04 13:18:31 +0200 | [diff] [blame] | 1195 | __update_tg_runnable_avg(&rq->avg, &rq->cfs); |
Ben Segall | 18bf280 | 2012-10-04 12:51:20 +0200 | [diff] [blame] | 1196 | } |
Paul Turner | 2dac754 | 2012-10-04 13:18:30 +0200 | [diff] [blame] | 1197 | |
| 1198 | /* Add the load generated by se into cfs_rq's child load-average */ |
| 1199 | static inline void enqueue_entity_load_avg(struct cfs_rq *cfs_rq, |
Paul Turner | 9ee474f | 2012-10-04 13:18:30 +0200 | [diff] [blame] | 1200 | struct sched_entity *se, |
| 1201 | int wakeup) |
Paul Turner | 2dac754 | 2012-10-04 13:18:30 +0200 | [diff] [blame] | 1202 | { |
Paul Turner | aff3e49 | 2012-10-04 13:18:30 +0200 | [diff] [blame] | 1203 | /* |
| 1204 | * We track migrations using entity decay_count <= 0, on a wake-up |
| 1205 | * migration we use a negative decay count to track the remote decays |
| 1206 | * accumulated while sleeping. |
| 1207 | */ |
| 1208 | if (unlikely(se->avg.decay_count <= 0)) { |
Paul Turner | 9ee474f | 2012-10-04 13:18:30 +0200 | [diff] [blame] | 1209 | se->avg.last_runnable_update = rq_of(cfs_rq)->clock_task; |
Paul Turner | aff3e49 | 2012-10-04 13:18:30 +0200 | [diff] [blame] | 1210 | if (se->avg.decay_count) { |
| 1211 | /* |
| 1212 | * In a wake-up migration we have to approximate the |
| 1213 | * time sleeping. This is because we can't synchronize |
| 1214 | * clock_task between the two cpus, and it is not |
| 1215 | * guaranteed to be read-safe. Instead, we can |
| 1216 | * approximate this using our carried decays, which are |
| 1217 | * explicitly atomically readable. |
| 1218 | */ |
| 1219 | se->avg.last_runnable_update -= (-se->avg.decay_count) |
| 1220 | << 20; |
| 1221 | update_entity_load_avg(se, 0); |
| 1222 | /* Indicate that we're now synchronized and on-rq */ |
| 1223 | se->avg.decay_count = 0; |
| 1224 | } |
Paul Turner | 9ee474f | 2012-10-04 13:18:30 +0200 | [diff] [blame] | 1225 | wakeup = 0; |
| 1226 | } else { |
| 1227 | __synchronize_entity_decay(se); |
| 1228 | } |
| 1229 | |
Paul Turner | aff3e49 | 2012-10-04 13:18:30 +0200 | [diff] [blame] | 1230 | /* migrated tasks did not contribute to our blocked load */ |
| 1231 | if (wakeup) { |
Paul Turner | 9ee474f | 2012-10-04 13:18:30 +0200 | [diff] [blame] | 1232 | subtract_blocked_load_contrib(cfs_rq, se->avg.load_avg_contrib); |
Paul Turner | aff3e49 | 2012-10-04 13:18:30 +0200 | [diff] [blame] | 1233 | update_entity_load_avg(se, 0); |
| 1234 | } |
Paul Turner | 9ee474f | 2012-10-04 13:18:30 +0200 | [diff] [blame] | 1235 | |
Paul Turner | 2dac754 | 2012-10-04 13:18:30 +0200 | [diff] [blame] | 1236 | cfs_rq->runnable_load_avg += se->avg.load_avg_contrib; |
Paul Turner | aff3e49 | 2012-10-04 13:18:30 +0200 | [diff] [blame] | 1237 | /* we force update consideration on load-balancer moves */ |
| 1238 | update_cfs_rq_blocked_load(cfs_rq, !wakeup); |
Paul Turner | 2dac754 | 2012-10-04 13:18:30 +0200 | [diff] [blame] | 1239 | } |
| 1240 | |
Paul Turner | 9ee474f | 2012-10-04 13:18:30 +0200 | [diff] [blame] | 1241 | /* |
| 1242 | * Remove se's load from this cfs_rq child load-average, if the entity is |
| 1243 | * transitioning to a blocked state we track its projected decay using |
| 1244 | * blocked_load_avg. |
| 1245 | */ |
Paul Turner | 2dac754 | 2012-10-04 13:18:30 +0200 | [diff] [blame] | 1246 | static inline void dequeue_entity_load_avg(struct cfs_rq *cfs_rq, |
Paul Turner | 9ee474f | 2012-10-04 13:18:30 +0200 | [diff] [blame] | 1247 | struct sched_entity *se, |
| 1248 | int sleep) |
Paul Turner | 2dac754 | 2012-10-04 13:18:30 +0200 | [diff] [blame] | 1249 | { |
Paul Turner | 9ee474f | 2012-10-04 13:18:30 +0200 | [diff] [blame] | 1250 | update_entity_load_avg(se, 1); |
Paul Turner | aff3e49 | 2012-10-04 13:18:30 +0200 | [diff] [blame] | 1251 | /* we force update consideration on load-balancer moves */ |
| 1252 | update_cfs_rq_blocked_load(cfs_rq, !sleep); |
Paul Turner | 9ee474f | 2012-10-04 13:18:30 +0200 | [diff] [blame] | 1253 | |
Paul Turner | 2dac754 | 2012-10-04 13:18:30 +0200 | [diff] [blame] | 1254 | cfs_rq->runnable_load_avg -= se->avg.load_avg_contrib; |
Paul Turner | 9ee474f | 2012-10-04 13:18:30 +0200 | [diff] [blame] | 1255 | if (sleep) { |
| 1256 | cfs_rq->blocked_load_avg += se->avg.load_avg_contrib; |
| 1257 | se->avg.decay_count = atomic64_read(&cfs_rq->decay_counter); |
| 1258 | } /* migrations, e.g. sleep=0 leave decay_count == 0 */ |
Paul Turner | 2dac754 | 2012-10-04 13:18:30 +0200 | [diff] [blame] | 1259 | } |
Paul Turner | 9d85f21 | 2012-10-04 13:18:29 +0200 | [diff] [blame] | 1260 | #else |
Paul Turner | 9ee474f | 2012-10-04 13:18:30 +0200 | [diff] [blame] | 1261 | static inline void update_entity_load_avg(struct sched_entity *se, |
| 1262 | int update_cfs_rq) {} |
Ben Segall | 18bf280 | 2012-10-04 12:51:20 +0200 | [diff] [blame] | 1263 | static inline void update_rq_runnable_avg(struct rq *rq, int runnable) {} |
Paul Turner | 2dac754 | 2012-10-04 13:18:30 +0200 | [diff] [blame] | 1264 | static inline void enqueue_entity_load_avg(struct cfs_rq *cfs_rq, |
Paul Turner | 9ee474f | 2012-10-04 13:18:30 +0200 | [diff] [blame] | 1265 | struct sched_entity *se, |
| 1266 | int wakeup) {} |
Paul Turner | 2dac754 | 2012-10-04 13:18:30 +0200 | [diff] [blame] | 1267 | static inline void dequeue_entity_load_avg(struct cfs_rq *cfs_rq, |
Paul Turner | 9ee474f | 2012-10-04 13:18:30 +0200 | [diff] [blame] | 1268 | struct sched_entity *se, |
| 1269 | int sleep) {} |
Paul Turner | aff3e49 | 2012-10-04 13:18:30 +0200 | [diff] [blame] | 1270 | static inline void update_cfs_rq_blocked_load(struct cfs_rq *cfs_rq, |
| 1271 | int force_update) {} |
Paul Turner | 9d85f21 | 2012-10-04 13:18:29 +0200 | [diff] [blame] | 1272 | #endif |
| 1273 | |
Ingo Molnar | 2396af6 | 2007-08-09 11:16:48 +0200 | [diff] [blame] | 1274 | 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] | 1275 | { |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 1276 | #ifdef CONFIG_SCHEDSTATS |
Peter Zijlstra | e414314 | 2009-07-23 20:13:26 +0200 | [diff] [blame] | 1277 | struct task_struct *tsk = NULL; |
| 1278 | |
| 1279 | if (entity_is_task(se)) |
| 1280 | tsk = task_of(se); |
| 1281 | |
Lucas De Marchi | 41acab8 | 2010-03-10 23:37:45 -0300 | [diff] [blame] | 1282 | if (se->statistics.sleep_start) { |
| 1283 | u64 delta = rq_of(cfs_rq)->clock - se->statistics.sleep_start; |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 1284 | |
| 1285 | if ((s64)delta < 0) |
| 1286 | delta = 0; |
| 1287 | |
Lucas De Marchi | 41acab8 | 2010-03-10 23:37:45 -0300 | [diff] [blame] | 1288 | if (unlikely(delta > se->statistics.sleep_max)) |
| 1289 | se->statistics.sleep_max = delta; |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 1290 | |
Peter Zijlstra | 8c79a04 | 2012-01-30 14:51:37 +0100 | [diff] [blame] | 1291 | se->statistics.sleep_start = 0; |
Lucas De Marchi | 41acab8 | 2010-03-10 23:37:45 -0300 | [diff] [blame] | 1292 | se->statistics.sum_sleep_runtime += delta; |
Arjan van de Ven | 9745512 | 2008-01-25 21:08:34 +0100 | [diff] [blame] | 1293 | |
Peter Zijlstra | 768d0c2 | 2009-07-23 20:13:26 +0200 | [diff] [blame] | 1294 | if (tsk) { |
Peter Zijlstra | e414314 | 2009-07-23 20:13:26 +0200 | [diff] [blame] | 1295 | account_scheduler_latency(tsk, delta >> 10, 1); |
Peter Zijlstra | 768d0c2 | 2009-07-23 20:13:26 +0200 | [diff] [blame] | 1296 | trace_sched_stat_sleep(tsk, delta); |
| 1297 | } |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 1298 | } |
Lucas De Marchi | 41acab8 | 2010-03-10 23:37:45 -0300 | [diff] [blame] | 1299 | if (se->statistics.block_start) { |
| 1300 | u64 delta = rq_of(cfs_rq)->clock - se->statistics.block_start; |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 1301 | |
| 1302 | if ((s64)delta < 0) |
| 1303 | delta = 0; |
| 1304 | |
Lucas De Marchi | 41acab8 | 2010-03-10 23:37:45 -0300 | [diff] [blame] | 1305 | if (unlikely(delta > se->statistics.block_max)) |
| 1306 | se->statistics.block_max = delta; |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 1307 | |
Peter Zijlstra | 8c79a04 | 2012-01-30 14:51:37 +0100 | [diff] [blame] | 1308 | se->statistics.block_start = 0; |
Lucas De Marchi | 41acab8 | 2010-03-10 23:37:45 -0300 | [diff] [blame] | 1309 | se->statistics.sum_sleep_runtime += delta; |
Ingo Molnar | 30084fb | 2007-10-02 14:13:08 +0200 | [diff] [blame] | 1310 | |
Peter Zijlstra | e414314 | 2009-07-23 20:13:26 +0200 | [diff] [blame] | 1311 | if (tsk) { |
Arjan van de Ven | 8f0dfc3 | 2009-07-20 11:26:58 -0700 | [diff] [blame] | 1312 | if (tsk->in_iowait) { |
Lucas De Marchi | 41acab8 | 2010-03-10 23:37:45 -0300 | [diff] [blame] | 1313 | se->statistics.iowait_sum += delta; |
| 1314 | se->statistics.iowait_count++; |
Peter Zijlstra | 768d0c2 | 2009-07-23 20:13:26 +0200 | [diff] [blame] | 1315 | trace_sched_stat_iowait(tsk, delta); |
Arjan van de Ven | 8f0dfc3 | 2009-07-20 11:26:58 -0700 | [diff] [blame] | 1316 | } |
| 1317 | |
Andrew Vagin | b781a60 | 2011-11-28 12:03:35 +0300 | [diff] [blame] | 1318 | trace_sched_stat_blocked(tsk, delta); |
| 1319 | |
Peter Zijlstra | e414314 | 2009-07-23 20:13:26 +0200 | [diff] [blame] | 1320 | /* |
| 1321 | * Blocking time is in units of nanosecs, so shift by |
| 1322 | * 20 to get a milliseconds-range estimation of the |
| 1323 | * amount of time that the task spent sleeping: |
| 1324 | */ |
| 1325 | if (unlikely(prof_on == SLEEP_PROFILING)) { |
| 1326 | profile_hits(SLEEP_PROFILING, |
| 1327 | (void *)get_wchan(tsk), |
| 1328 | delta >> 20); |
| 1329 | } |
| 1330 | account_scheduler_latency(tsk, delta >> 10, 0); |
Ingo Molnar | 30084fb | 2007-10-02 14:13:08 +0200 | [diff] [blame] | 1331 | } |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 1332 | } |
| 1333 | #endif |
| 1334 | } |
| 1335 | |
Peter Zijlstra | ddc9729 | 2007-10-15 17:00:10 +0200 | [diff] [blame] | 1336 | static void check_spread(struct cfs_rq *cfs_rq, struct sched_entity *se) |
| 1337 | { |
| 1338 | #ifdef CONFIG_SCHED_DEBUG |
| 1339 | s64 d = se->vruntime - cfs_rq->min_vruntime; |
| 1340 | |
| 1341 | if (d < 0) |
| 1342 | d = -d; |
| 1343 | |
| 1344 | if (d > 3*sysctl_sched_latency) |
| 1345 | schedstat_inc(cfs_rq, nr_spread_over); |
| 1346 | #endif |
| 1347 | } |
| 1348 | |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 1349 | static void |
Peter Zijlstra | aeb73b0 | 2007-10-15 17:00:05 +0200 | [diff] [blame] | 1350 | place_entity(struct cfs_rq *cfs_rq, struct sched_entity *se, int initial) |
| 1351 | { |
Peter Zijlstra | 1af5f73 | 2008-10-24 11:06:13 +0200 | [diff] [blame] | 1352 | u64 vruntime = cfs_rq->min_vruntime; |
Peter Zijlstra | 94dfb5e | 2007-10-15 17:00:05 +0200 | [diff] [blame] | 1353 | |
Peter Zijlstra | 2cb8600 | 2007-11-09 22:39:37 +0100 | [diff] [blame] | 1354 | /* |
| 1355 | * The 'current' period is already promised to the current tasks, |
| 1356 | * however the extra weight of the new task will slow them down a |
| 1357 | * little, place the new task so that it fits in the slot that |
| 1358 | * stays open at the end. |
| 1359 | */ |
Peter Zijlstra | 94dfb5e | 2007-10-15 17:00:05 +0200 | [diff] [blame] | 1360 | if (initial && sched_feat(START_DEBIT)) |
Peter Zijlstra | f9c0b09 | 2008-10-17 19:27:04 +0200 | [diff] [blame] | 1361 | vruntime += sched_vslice(cfs_rq, se); |
Peter Zijlstra | aeb73b0 | 2007-10-15 17:00:05 +0200 | [diff] [blame] | 1362 | |
Mike Galbraith | a2e7a7e | 2009-09-18 09:19:25 +0200 | [diff] [blame] | 1363 | /* sleeps up to a single latency don't count. */ |
Mike Galbraith | 5ca9880 | 2010-03-11 17:17:17 +0100 | [diff] [blame] | 1364 | if (!initial) { |
Mike Galbraith | a2e7a7e | 2009-09-18 09:19:25 +0200 | [diff] [blame] | 1365 | unsigned long thresh = sysctl_sched_latency; |
Peter Zijlstra | a7be37a | 2008-06-27 13:41:11 +0200 | [diff] [blame] | 1366 | |
Mike Galbraith | a2e7a7e | 2009-09-18 09:19:25 +0200 | [diff] [blame] | 1367 | /* |
Mike Galbraith | a2e7a7e | 2009-09-18 09:19:25 +0200 | [diff] [blame] | 1368 | * Halve their sleep time's effect, to allow |
| 1369 | * for a gentler effect of sleepers: |
| 1370 | */ |
| 1371 | if (sched_feat(GENTLE_FAIR_SLEEPERS)) |
| 1372 | thresh >>= 1; |
Ingo Molnar | 51e0304 | 2009-09-16 08:54:45 +0200 | [diff] [blame] | 1373 | |
Mike Galbraith | a2e7a7e | 2009-09-18 09:19:25 +0200 | [diff] [blame] | 1374 | vruntime -= thresh; |
Peter Zijlstra | aeb73b0 | 2007-10-15 17:00:05 +0200 | [diff] [blame] | 1375 | } |
| 1376 | |
Mike Galbraith | b5d9d73 | 2009-09-08 11:12:28 +0200 | [diff] [blame] | 1377 | /* ensure we never gain time by being placed backwards. */ |
| 1378 | vruntime = max_vruntime(se->vruntime, vruntime); |
| 1379 | |
Peter Zijlstra | 67e9fb2 | 2007-10-15 17:00:10 +0200 | [diff] [blame] | 1380 | se->vruntime = vruntime; |
Peter Zijlstra | aeb73b0 | 2007-10-15 17:00:05 +0200 | [diff] [blame] | 1381 | } |
| 1382 | |
Paul Turner | d3d9dc3 | 2011-07-21 09:43:39 -0700 | [diff] [blame] | 1383 | static void check_enqueue_throttle(struct cfs_rq *cfs_rq); |
| 1384 | |
Peter Zijlstra | aeb73b0 | 2007-10-15 17:00:05 +0200 | [diff] [blame] | 1385 | static void |
Peter Zijlstra | 88ec22d | 2009-12-16 18:04:41 +0100 | [diff] [blame] | 1386 | 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] | 1387 | { |
| 1388 | /* |
Peter Zijlstra | 88ec22d | 2009-12-16 18:04:41 +0100 | [diff] [blame] | 1389 | * Update the normalized vruntime before updating min_vruntime |
| 1390 | * through callig update_curr(). |
| 1391 | */ |
Peter Zijlstra | 371fd7e | 2010-03-24 16:38:48 +0100 | [diff] [blame] | 1392 | if (!(flags & ENQUEUE_WAKEUP) || (flags & ENQUEUE_WAKING)) |
Peter Zijlstra | 88ec22d | 2009-12-16 18:04:41 +0100 | [diff] [blame] | 1393 | se->vruntime += cfs_rq->min_vruntime; |
| 1394 | |
| 1395 | /* |
Dmitry Adamushko | a2a2d68 | 2007-10-15 17:00:13 +0200 | [diff] [blame] | 1396 | * Update run-time statistics of the 'current'. |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 1397 | */ |
Ingo Molnar | b7cc089 | 2007-08-09 11:16:47 +0200 | [diff] [blame] | 1398 | update_curr(cfs_rq); |
Paul Turner | 9ee474f | 2012-10-04 13:18:30 +0200 | [diff] [blame] | 1399 | enqueue_entity_load_avg(cfs_rq, se, flags & ENQUEUE_WAKEUP); |
Peter Zijlstra | a992241 | 2008-05-05 23:56:17 +0200 | [diff] [blame] | 1400 | account_entity_enqueue(cfs_rq, se); |
Paul Turner | 6d5ab29 | 2011-01-21 20:45:01 -0800 | [diff] [blame] | 1401 | update_cfs_shares(cfs_rq); |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 1402 | |
Peter Zijlstra | 88ec22d | 2009-12-16 18:04:41 +0100 | [diff] [blame] | 1403 | if (flags & ENQUEUE_WAKEUP) { |
Peter Zijlstra | aeb73b0 | 2007-10-15 17:00:05 +0200 | [diff] [blame] | 1404 | place_entity(cfs_rq, se, 0); |
Ingo Molnar | 2396af6 | 2007-08-09 11:16:48 +0200 | [diff] [blame] | 1405 | enqueue_sleeper(cfs_rq, se); |
Ingo Molnar | e9acbff | 2007-10-15 17:00:04 +0200 | [diff] [blame] | 1406 | } |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 1407 | |
Ingo Molnar | d2417e5 | 2007-08-09 11:16:47 +0200 | [diff] [blame] | 1408 | update_stats_enqueue(cfs_rq, se); |
Peter Zijlstra | ddc9729 | 2007-10-15 17:00:10 +0200 | [diff] [blame] | 1409 | check_spread(cfs_rq, se); |
Srivatsa Vaddagiri | 83b699e | 2007-10-15 17:00:08 +0200 | [diff] [blame] | 1410 | if (se != cfs_rq->curr) |
| 1411 | __enqueue_entity(cfs_rq, se); |
Peter Zijlstra | 2069dd7 | 2010-11-15 15:47:00 -0800 | [diff] [blame] | 1412 | se->on_rq = 1; |
Peter Zijlstra | 3d4b47b | 2010-11-15 15:47:01 -0800 | [diff] [blame] | 1413 | |
Paul Turner | d3d9dc3 | 2011-07-21 09:43:39 -0700 | [diff] [blame] | 1414 | if (cfs_rq->nr_running == 1) { |
Peter Zijlstra | 3d4b47b | 2010-11-15 15:47:01 -0800 | [diff] [blame] | 1415 | list_add_leaf_cfs_rq(cfs_rq); |
Paul Turner | d3d9dc3 | 2011-07-21 09:43:39 -0700 | [diff] [blame] | 1416 | check_enqueue_throttle(cfs_rq); |
| 1417 | } |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 1418 | } |
| 1419 | |
Rik van Riel | 2c13c919 | 2011-02-01 09:48:37 -0500 | [diff] [blame] | 1420 | static void __clear_buddies_last(struct sched_entity *se) |
Peter Zijlstra | 2002c69 | 2008-11-11 11:52:33 +0100 | [diff] [blame] | 1421 | { |
Rik van Riel | 2c13c919 | 2011-02-01 09:48:37 -0500 | [diff] [blame] | 1422 | for_each_sched_entity(se) { |
| 1423 | struct cfs_rq *cfs_rq = cfs_rq_of(se); |
| 1424 | if (cfs_rq->last == se) |
| 1425 | cfs_rq->last = NULL; |
| 1426 | else |
| 1427 | break; |
| 1428 | } |
| 1429 | } |
Peter Zijlstra | 2002c69 | 2008-11-11 11:52:33 +0100 | [diff] [blame] | 1430 | |
Rik van Riel | 2c13c919 | 2011-02-01 09:48:37 -0500 | [diff] [blame] | 1431 | static void __clear_buddies_next(struct sched_entity *se) |
| 1432 | { |
| 1433 | for_each_sched_entity(se) { |
| 1434 | struct cfs_rq *cfs_rq = cfs_rq_of(se); |
| 1435 | if (cfs_rq->next == se) |
| 1436 | cfs_rq->next = NULL; |
| 1437 | else |
| 1438 | break; |
| 1439 | } |
Peter Zijlstra | 2002c69 | 2008-11-11 11:52:33 +0100 | [diff] [blame] | 1440 | } |
| 1441 | |
Rik van Riel | ac53db5 | 2011-02-01 09:51:03 -0500 | [diff] [blame] | 1442 | static void __clear_buddies_skip(struct sched_entity *se) |
| 1443 | { |
| 1444 | for_each_sched_entity(se) { |
| 1445 | struct cfs_rq *cfs_rq = cfs_rq_of(se); |
| 1446 | if (cfs_rq->skip == se) |
| 1447 | cfs_rq->skip = NULL; |
| 1448 | else |
| 1449 | break; |
| 1450 | } |
| 1451 | } |
| 1452 | |
Peter Zijlstra | a571bbe | 2009-01-28 14:51:40 +0100 | [diff] [blame] | 1453 | static void clear_buddies(struct cfs_rq *cfs_rq, struct sched_entity *se) |
| 1454 | { |
Rik van Riel | 2c13c919 | 2011-02-01 09:48:37 -0500 | [diff] [blame] | 1455 | if (cfs_rq->last == se) |
| 1456 | __clear_buddies_last(se); |
| 1457 | |
| 1458 | if (cfs_rq->next == se) |
| 1459 | __clear_buddies_next(se); |
Rik van Riel | ac53db5 | 2011-02-01 09:51:03 -0500 | [diff] [blame] | 1460 | |
| 1461 | if (cfs_rq->skip == se) |
| 1462 | __clear_buddies_skip(se); |
Peter Zijlstra | a571bbe | 2009-01-28 14:51:40 +0100 | [diff] [blame] | 1463 | } |
| 1464 | |
Peter Zijlstra | 6c16a6d | 2012-03-21 13:07:16 -0700 | [diff] [blame] | 1465 | 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] | 1466 | |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 1467 | static void |
Peter Zijlstra | 371fd7e | 2010-03-24 16:38:48 +0100 | [diff] [blame] | 1468 | 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] | 1469 | { |
Dmitry Adamushko | a2a2d68 | 2007-10-15 17:00:13 +0200 | [diff] [blame] | 1470 | /* |
| 1471 | * Update run-time statistics of the 'current'. |
| 1472 | */ |
| 1473 | update_curr(cfs_rq); |
Paul Turner | 9ee474f | 2012-10-04 13:18:30 +0200 | [diff] [blame] | 1474 | dequeue_entity_load_avg(cfs_rq, se, flags & DEQUEUE_SLEEP); |
Dmitry Adamushko | a2a2d68 | 2007-10-15 17:00:13 +0200 | [diff] [blame] | 1475 | |
Ingo Molnar | 19b6a2e | 2007-08-09 11:16:48 +0200 | [diff] [blame] | 1476 | update_stats_dequeue(cfs_rq, se); |
Peter Zijlstra | 371fd7e | 2010-03-24 16:38:48 +0100 | [diff] [blame] | 1477 | if (flags & DEQUEUE_SLEEP) { |
Peter Zijlstra | 67e9fb2 | 2007-10-15 17:00:10 +0200 | [diff] [blame] | 1478 | #ifdef CONFIG_SCHEDSTATS |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 1479 | if (entity_is_task(se)) { |
| 1480 | struct task_struct *tsk = task_of(se); |
| 1481 | |
| 1482 | if (tsk->state & TASK_INTERRUPTIBLE) |
Lucas De Marchi | 41acab8 | 2010-03-10 23:37:45 -0300 | [diff] [blame] | 1483 | se->statistics.sleep_start = rq_of(cfs_rq)->clock; |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 1484 | if (tsk->state & TASK_UNINTERRUPTIBLE) |
Lucas De Marchi | 41acab8 | 2010-03-10 23:37:45 -0300 | [diff] [blame] | 1485 | se->statistics.block_start = rq_of(cfs_rq)->clock; |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 1486 | } |
Dmitry Adamushko | db36cc7 | 2007-10-15 17:00:06 +0200 | [diff] [blame] | 1487 | #endif |
Peter Zijlstra | 67e9fb2 | 2007-10-15 17:00:10 +0200 | [diff] [blame] | 1488 | } |
| 1489 | |
Peter Zijlstra | 2002c69 | 2008-11-11 11:52:33 +0100 | [diff] [blame] | 1490 | clear_buddies(cfs_rq, se); |
Peter Zijlstra | 4793241 | 2008-11-04 21:25:09 +0100 | [diff] [blame] | 1491 | |
Srivatsa Vaddagiri | 83b699e | 2007-10-15 17:00:08 +0200 | [diff] [blame] | 1492 | if (se != cfs_rq->curr) |
Dmitry Adamushko | 30cfdcf | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 1493 | __dequeue_entity(cfs_rq, se); |
Peter Zijlstra | 2069dd7 | 2010-11-15 15:47:00 -0800 | [diff] [blame] | 1494 | se->on_rq = 0; |
Dmitry Adamushko | 30cfdcf | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 1495 | account_entity_dequeue(cfs_rq, se); |
Peter Zijlstra | 88ec22d | 2009-12-16 18:04:41 +0100 | [diff] [blame] | 1496 | |
| 1497 | /* |
| 1498 | * Normalize the entity after updating the min_vruntime because the |
| 1499 | * update can refer to the ->curr item and we need to reflect this |
| 1500 | * movement in our normalized position. |
| 1501 | */ |
Peter Zijlstra | 371fd7e | 2010-03-24 16:38:48 +0100 | [diff] [blame] | 1502 | if (!(flags & DEQUEUE_SLEEP)) |
Peter Zijlstra | 88ec22d | 2009-12-16 18:04:41 +0100 | [diff] [blame] | 1503 | se->vruntime -= cfs_rq->min_vruntime; |
Peter Zijlstra | 1e87623 | 2011-05-17 16:21:10 -0700 | [diff] [blame] | 1504 | |
Paul Turner | d8b4986 | 2011-07-21 09:43:41 -0700 | [diff] [blame] | 1505 | /* return excess runtime on last dequeue */ |
| 1506 | return_cfs_rq_runtime(cfs_rq); |
| 1507 | |
Peter Zijlstra | 1e87623 | 2011-05-17 16:21:10 -0700 | [diff] [blame] | 1508 | update_min_vruntime(cfs_rq); |
| 1509 | update_cfs_shares(cfs_rq); |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 1510 | } |
| 1511 | |
| 1512 | /* |
| 1513 | * Preempt the current task with a newly woken task if needed: |
| 1514 | */ |
Peter Zijlstra | 7c92e54 | 2007-09-05 14:32:49 +0200 | [diff] [blame] | 1515 | static void |
Ingo Molnar | 2e09bf5 | 2007-10-15 17:00:05 +0200 | [diff] [blame] | 1516 | check_preempt_tick(struct cfs_rq *cfs_rq, struct sched_entity *curr) |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 1517 | { |
Peter Zijlstra | 1169783 | 2007-09-05 14:32:49 +0200 | [diff] [blame] | 1518 | unsigned long ideal_runtime, delta_exec; |
Wang Xingchao | f4cfb33 | 2011-09-16 13:35:52 -0400 | [diff] [blame] | 1519 | struct sched_entity *se; |
| 1520 | s64 delta; |
Peter Zijlstra | 1169783 | 2007-09-05 14:32:49 +0200 | [diff] [blame] | 1521 | |
Peter Zijlstra | 6d0f0ebd | 2007-10-15 17:00:05 +0200 | [diff] [blame] | 1522 | ideal_runtime = sched_slice(cfs_rq, curr); |
Peter Zijlstra | 1169783 | 2007-09-05 14:32:49 +0200 | [diff] [blame] | 1523 | delta_exec = curr->sum_exec_runtime - curr->prev_sum_exec_runtime; |
Mike Galbraith | a9f3e2b | 2009-01-28 14:51:39 +0100 | [diff] [blame] | 1524 | if (delta_exec > ideal_runtime) { |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 1525 | resched_task(rq_of(cfs_rq)->curr); |
Mike Galbraith | a9f3e2b | 2009-01-28 14:51:39 +0100 | [diff] [blame] | 1526 | /* |
| 1527 | * The current task ran long enough, ensure it doesn't get |
| 1528 | * re-elected due to buddy favours. |
| 1529 | */ |
| 1530 | clear_buddies(cfs_rq, curr); |
Mike Galbraith | f685cea | 2009-10-23 23:09:22 +0200 | [diff] [blame] | 1531 | return; |
| 1532 | } |
| 1533 | |
| 1534 | /* |
| 1535 | * Ensure that a task that missed wakeup preemption by a |
| 1536 | * narrow margin doesn't have to wait for a full slice. |
| 1537 | * This also mitigates buddy induced latencies under load. |
| 1538 | */ |
Mike Galbraith | f685cea | 2009-10-23 23:09:22 +0200 | [diff] [blame] | 1539 | if (delta_exec < sysctl_sched_min_granularity) |
| 1540 | return; |
| 1541 | |
Wang Xingchao | f4cfb33 | 2011-09-16 13:35:52 -0400 | [diff] [blame] | 1542 | se = __pick_first_entity(cfs_rq); |
| 1543 | delta = curr->vruntime - se->vruntime; |
Mike Galbraith | f685cea | 2009-10-23 23:09:22 +0200 | [diff] [blame] | 1544 | |
Wang Xingchao | f4cfb33 | 2011-09-16 13:35:52 -0400 | [diff] [blame] | 1545 | if (delta < 0) |
| 1546 | return; |
Mike Galbraith | d7d8294 | 2011-01-05 05:41:17 +0100 | [diff] [blame] | 1547 | |
Wang Xingchao | f4cfb33 | 2011-09-16 13:35:52 -0400 | [diff] [blame] | 1548 | if (delta > ideal_runtime) |
| 1549 | resched_task(rq_of(cfs_rq)->curr); |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 1550 | } |
| 1551 | |
Srivatsa Vaddagiri | 83b699e | 2007-10-15 17:00:08 +0200 | [diff] [blame] | 1552 | static void |
Ingo Molnar | 8494f41 | 2007-08-09 11:16:48 +0200 | [diff] [blame] | 1553 | set_next_entity(struct cfs_rq *cfs_rq, struct sched_entity *se) |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 1554 | { |
Srivatsa Vaddagiri | 83b699e | 2007-10-15 17:00:08 +0200 | [diff] [blame] | 1555 | /* 'current' is not kept within the tree. */ |
| 1556 | if (se->on_rq) { |
| 1557 | /* |
| 1558 | * Any task has to be enqueued before it get to execute on |
| 1559 | * a CPU. So account for the time it spent waiting on the |
| 1560 | * runqueue. |
| 1561 | */ |
| 1562 | update_stats_wait_end(cfs_rq, se); |
| 1563 | __dequeue_entity(cfs_rq, se); |
| 1564 | } |
| 1565 | |
Ingo Molnar | 79303e9 | 2007-08-09 11:16:47 +0200 | [diff] [blame] | 1566 | update_stats_curr_start(cfs_rq, se); |
Ingo Molnar | 429d43b | 2007-10-15 17:00:03 +0200 | [diff] [blame] | 1567 | cfs_rq->curr = se; |
Ingo Molnar | eba1ed4 | 2007-10-15 17:00:02 +0200 | [diff] [blame] | 1568 | #ifdef CONFIG_SCHEDSTATS |
| 1569 | /* |
| 1570 | * Track our maximum slice length, if the CPU's load is at |
| 1571 | * least twice that of our own weight (i.e. dont track it |
| 1572 | * when there are only lesser-weight tasks around): |
| 1573 | */ |
Dmitry Adamushko | 495eca4 | 2007-10-15 17:00:06 +0200 | [diff] [blame] | 1574 | if (rq_of(cfs_rq)->load.weight >= 2*se->load.weight) { |
Lucas De Marchi | 41acab8 | 2010-03-10 23:37:45 -0300 | [diff] [blame] | 1575 | se->statistics.slice_max = max(se->statistics.slice_max, |
Ingo Molnar | eba1ed4 | 2007-10-15 17:00:02 +0200 | [diff] [blame] | 1576 | se->sum_exec_runtime - se->prev_sum_exec_runtime); |
| 1577 | } |
| 1578 | #endif |
Peter Zijlstra | 4a55b45 | 2007-09-05 14:32:49 +0200 | [diff] [blame] | 1579 | se->prev_sum_exec_runtime = se->sum_exec_runtime; |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 1580 | } |
| 1581 | |
Peter Zijlstra | 3f3a490 | 2008-10-24 11:06:16 +0200 | [diff] [blame] | 1582 | static int |
| 1583 | wakeup_preempt_entity(struct sched_entity *curr, struct sched_entity *se); |
| 1584 | |
Rik van Riel | ac53db5 | 2011-02-01 09:51:03 -0500 | [diff] [blame] | 1585 | /* |
| 1586 | * Pick the next process, keeping these things in mind, in this order: |
| 1587 | * 1) keep things fair between processes/task groups |
| 1588 | * 2) pick the "next" process, since someone really wants that to run |
| 1589 | * 3) pick the "last" process, for cache locality |
| 1590 | * 4) do not run the "skip" process, if something else is available |
| 1591 | */ |
Peter Zijlstra | f4b6755 | 2008-11-04 21:25:07 +0100 | [diff] [blame] | 1592 | static struct sched_entity *pick_next_entity(struct cfs_rq *cfs_rq) |
Peter Zijlstra | aa2ac25 | 2008-03-14 21:12:12 +0100 | [diff] [blame] | 1593 | { |
Rik van Riel | ac53db5 | 2011-02-01 09:51:03 -0500 | [diff] [blame] | 1594 | struct sched_entity *se = __pick_first_entity(cfs_rq); |
Mike Galbraith | f685cea | 2009-10-23 23:09:22 +0200 | [diff] [blame] | 1595 | struct sched_entity *left = se; |
Peter Zijlstra | f4b6755 | 2008-11-04 21:25:07 +0100 | [diff] [blame] | 1596 | |
Rik van Riel | ac53db5 | 2011-02-01 09:51:03 -0500 | [diff] [blame] | 1597 | /* |
| 1598 | * Avoid running the skip buddy, if running something else can |
| 1599 | * be done without getting too unfair. |
| 1600 | */ |
| 1601 | if (cfs_rq->skip == se) { |
| 1602 | struct sched_entity *second = __pick_next_entity(se); |
| 1603 | if (second && wakeup_preempt_entity(second, left) < 1) |
| 1604 | se = second; |
| 1605 | } |
Peter Zijlstra | aa2ac25 | 2008-03-14 21:12:12 +0100 | [diff] [blame] | 1606 | |
Mike Galbraith | f685cea | 2009-10-23 23:09:22 +0200 | [diff] [blame] | 1607 | /* |
| 1608 | * Prefer last buddy, try to return the CPU to a preempted task. |
| 1609 | */ |
| 1610 | if (cfs_rq->last && wakeup_preempt_entity(cfs_rq->last, left) < 1) |
| 1611 | se = cfs_rq->last; |
| 1612 | |
Rik van Riel | ac53db5 | 2011-02-01 09:51:03 -0500 | [diff] [blame] | 1613 | /* |
| 1614 | * Someone really wants this to run. If it's not unfair, run it. |
| 1615 | */ |
| 1616 | if (cfs_rq->next && wakeup_preempt_entity(cfs_rq->next, left) < 1) |
| 1617 | se = cfs_rq->next; |
| 1618 | |
Mike Galbraith | f685cea | 2009-10-23 23:09:22 +0200 | [diff] [blame] | 1619 | clear_buddies(cfs_rq, se); |
Peter Zijlstra | 4793241 | 2008-11-04 21:25:09 +0100 | [diff] [blame] | 1620 | |
| 1621 | return se; |
Peter Zijlstra | aa2ac25 | 2008-03-14 21:12:12 +0100 | [diff] [blame] | 1622 | } |
| 1623 | |
Paul Turner | d3d9dc3 | 2011-07-21 09:43:39 -0700 | [diff] [blame] | 1624 | static void check_cfs_rq_runtime(struct cfs_rq *cfs_rq); |
| 1625 | |
Ingo Molnar | ab6cde2 | 2007-08-09 11:16:48 +0200 | [diff] [blame] | 1626 | 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] | 1627 | { |
| 1628 | /* |
| 1629 | * If still on the runqueue then deactivate_task() |
| 1630 | * was not called and update_curr() has to be done: |
| 1631 | */ |
| 1632 | if (prev->on_rq) |
Ingo Molnar | b7cc089 | 2007-08-09 11:16:47 +0200 | [diff] [blame] | 1633 | update_curr(cfs_rq); |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 1634 | |
Paul Turner | d3d9dc3 | 2011-07-21 09:43:39 -0700 | [diff] [blame] | 1635 | /* throttle cfs_rqs exceeding runtime */ |
| 1636 | check_cfs_rq_runtime(cfs_rq); |
| 1637 | |
Peter Zijlstra | ddc9729 | 2007-10-15 17:00:10 +0200 | [diff] [blame] | 1638 | check_spread(cfs_rq, prev); |
Dmitry Adamushko | 30cfdcf | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 1639 | if (prev->on_rq) { |
Ingo Molnar | 5870db5 | 2007-08-09 11:16:47 +0200 | [diff] [blame] | 1640 | update_stats_wait_start(cfs_rq, prev); |
Dmitry Adamushko | 30cfdcf | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 1641 | /* Put 'current' back into the tree. */ |
| 1642 | __enqueue_entity(cfs_rq, prev); |
Paul Turner | 9d85f21 | 2012-10-04 13:18:29 +0200 | [diff] [blame] | 1643 | /* in !on_rq case, update occurred at dequeue */ |
Paul Turner | 9ee474f | 2012-10-04 13:18:30 +0200 | [diff] [blame] | 1644 | update_entity_load_avg(prev, 1); |
Dmitry Adamushko | 30cfdcf | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 1645 | } |
Ingo Molnar | 429d43b | 2007-10-15 17:00:03 +0200 | [diff] [blame] | 1646 | cfs_rq->curr = NULL; |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 1647 | } |
| 1648 | |
Peter Zijlstra | 8f4d37e | 2008-01-25 21:08:29 +0100 | [diff] [blame] | 1649 | static void |
| 1650 | 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] | 1651 | { |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 1652 | /* |
Dmitry Adamushko | 30cfdcf | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 1653 | * Update run-time statistics of the 'current'. |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 1654 | */ |
Dmitry Adamushko | 30cfdcf | 2007-10-15 17:00:07 +0200 | [diff] [blame] | 1655 | update_curr(cfs_rq); |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 1656 | |
Paul Turner | 43365bd | 2010-12-15 19:10:17 -0800 | [diff] [blame] | 1657 | /* |
Paul Turner | 9d85f21 | 2012-10-04 13:18:29 +0200 | [diff] [blame] | 1658 | * Ensure that runnable average is periodically updated. |
| 1659 | */ |
Paul Turner | 9ee474f | 2012-10-04 13:18:30 +0200 | [diff] [blame] | 1660 | update_entity_load_avg(curr, 1); |
Paul Turner | aff3e49 | 2012-10-04 13:18:30 +0200 | [diff] [blame] | 1661 | update_cfs_rq_blocked_load(cfs_rq, 1); |
Paul Turner | 9d85f21 | 2012-10-04 13:18:29 +0200 | [diff] [blame] | 1662 | |
Peter Zijlstra | 8f4d37e | 2008-01-25 21:08:29 +0100 | [diff] [blame] | 1663 | #ifdef CONFIG_SCHED_HRTICK |
| 1664 | /* |
| 1665 | * queued ticks are scheduled to match the slice, so don't bother |
| 1666 | * validating it and just reschedule. |
| 1667 | */ |
Harvey Harrison | 983ed7a | 2008-04-24 18:17:55 -0700 | [diff] [blame] | 1668 | if (queued) { |
| 1669 | resched_task(rq_of(cfs_rq)->curr); |
| 1670 | return; |
| 1671 | } |
Peter Zijlstra | 8f4d37e | 2008-01-25 21:08:29 +0100 | [diff] [blame] | 1672 | /* |
| 1673 | * don't let the period tick interfere with the hrtick preemption |
| 1674 | */ |
| 1675 | if (!sched_feat(DOUBLE_TICK) && |
| 1676 | hrtimer_active(&rq_of(cfs_rq)->hrtick_timer)) |
| 1677 | return; |
| 1678 | #endif |
| 1679 | |
Yong Zhang | 2c2efae | 2011-07-29 16:20:33 +0800 | [diff] [blame] | 1680 | if (cfs_rq->nr_running > 1) |
Ingo Molnar | 2e09bf5 | 2007-10-15 17:00:05 +0200 | [diff] [blame] | 1681 | check_preempt_tick(cfs_rq, curr); |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 1682 | } |
| 1683 | |
Paul Turner | ab84d31 | 2011-07-21 09:43:28 -0700 | [diff] [blame] | 1684 | |
| 1685 | /************************************************** |
| 1686 | * CFS bandwidth control machinery |
| 1687 | */ |
| 1688 | |
| 1689 | #ifdef CONFIG_CFS_BANDWIDTH |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 1690 | |
| 1691 | #ifdef HAVE_JUMP_LABEL |
Ingo Molnar | c5905af | 2012-02-24 08:31:31 +0100 | [diff] [blame] | 1692 | static struct static_key __cfs_bandwidth_used; |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 1693 | |
| 1694 | static inline bool cfs_bandwidth_used(void) |
| 1695 | { |
Ingo Molnar | c5905af | 2012-02-24 08:31:31 +0100 | [diff] [blame] | 1696 | return static_key_false(&__cfs_bandwidth_used); |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 1697 | } |
| 1698 | |
| 1699 | void account_cfs_bandwidth_used(int enabled, int was_enabled) |
| 1700 | { |
| 1701 | /* only need to count groups transitioning between enabled/!enabled */ |
| 1702 | if (enabled && !was_enabled) |
Ingo Molnar | c5905af | 2012-02-24 08:31:31 +0100 | [diff] [blame] | 1703 | static_key_slow_inc(&__cfs_bandwidth_used); |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 1704 | else if (!enabled && was_enabled) |
Ingo Molnar | c5905af | 2012-02-24 08:31:31 +0100 | [diff] [blame] | 1705 | static_key_slow_dec(&__cfs_bandwidth_used); |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 1706 | } |
| 1707 | #else /* HAVE_JUMP_LABEL */ |
| 1708 | static bool cfs_bandwidth_used(void) |
| 1709 | { |
| 1710 | return true; |
| 1711 | } |
| 1712 | |
| 1713 | void account_cfs_bandwidth_used(int enabled, int was_enabled) {} |
| 1714 | #endif /* HAVE_JUMP_LABEL */ |
| 1715 | |
Paul Turner | ab84d31 | 2011-07-21 09:43:28 -0700 | [diff] [blame] | 1716 | /* |
| 1717 | * default period for cfs group bandwidth. |
| 1718 | * default: 0.1s, units: nanoseconds |
| 1719 | */ |
| 1720 | static inline u64 default_cfs_period(void) |
| 1721 | { |
| 1722 | return 100000000ULL; |
| 1723 | } |
Paul Turner | ec12cb7 | 2011-07-21 09:43:30 -0700 | [diff] [blame] | 1724 | |
| 1725 | static inline u64 sched_cfs_bandwidth_slice(void) |
| 1726 | { |
| 1727 | return (u64)sysctl_sched_cfs_bandwidth_slice * NSEC_PER_USEC; |
| 1728 | } |
| 1729 | |
Paul Turner | a9cf55b | 2011-07-21 09:43:32 -0700 | [diff] [blame] | 1730 | /* |
| 1731 | * Replenish runtime according to assigned quota and update expiration time. |
| 1732 | * We use sched_clock_cpu directly instead of rq->clock to avoid adding |
| 1733 | * additional synchronization around rq->lock. |
| 1734 | * |
| 1735 | * requires cfs_b->lock |
| 1736 | */ |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 1737 | void __refill_cfs_bandwidth_runtime(struct cfs_bandwidth *cfs_b) |
Paul Turner | a9cf55b | 2011-07-21 09:43:32 -0700 | [diff] [blame] | 1738 | { |
| 1739 | u64 now; |
| 1740 | |
| 1741 | if (cfs_b->quota == RUNTIME_INF) |
| 1742 | return; |
| 1743 | |
| 1744 | now = sched_clock_cpu(smp_processor_id()); |
| 1745 | cfs_b->runtime = cfs_b->quota; |
| 1746 | cfs_b->runtime_expires = now + ktime_to_ns(cfs_b->period); |
| 1747 | } |
| 1748 | |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 1749 | static inline struct cfs_bandwidth *tg_cfs_bandwidth(struct task_group *tg) |
| 1750 | { |
| 1751 | return &tg->cfs_bandwidth; |
| 1752 | } |
| 1753 | |
Paul Turner | f1b1728 | 2012-10-04 13:18:31 +0200 | [diff] [blame] | 1754 | /* rq->task_clock normalized against any time this cfs_rq has spent throttled */ |
| 1755 | static inline u64 cfs_rq_clock_task(struct cfs_rq *cfs_rq) |
| 1756 | { |
| 1757 | if (unlikely(cfs_rq->throttle_count)) |
| 1758 | return cfs_rq->throttled_clock_task; |
| 1759 | |
| 1760 | return rq_of(cfs_rq)->clock_task - cfs_rq->throttled_clock_task_time; |
| 1761 | } |
| 1762 | |
Paul Turner | 85dac90 | 2011-07-21 09:43:33 -0700 | [diff] [blame] | 1763 | /* returns 0 on failure to allocate runtime */ |
| 1764 | static int assign_cfs_rq_runtime(struct cfs_rq *cfs_rq) |
Paul Turner | ec12cb7 | 2011-07-21 09:43:30 -0700 | [diff] [blame] | 1765 | { |
| 1766 | struct task_group *tg = cfs_rq->tg; |
| 1767 | struct cfs_bandwidth *cfs_b = tg_cfs_bandwidth(tg); |
Paul Turner | a9cf55b | 2011-07-21 09:43:32 -0700 | [diff] [blame] | 1768 | u64 amount = 0, min_amount, expires; |
Paul Turner | ec12cb7 | 2011-07-21 09:43:30 -0700 | [diff] [blame] | 1769 | |
| 1770 | /* note: this is a positive sum as runtime_remaining <= 0 */ |
| 1771 | min_amount = sched_cfs_bandwidth_slice() - cfs_rq->runtime_remaining; |
| 1772 | |
| 1773 | raw_spin_lock(&cfs_b->lock); |
| 1774 | if (cfs_b->quota == RUNTIME_INF) |
| 1775 | amount = min_amount; |
Paul Turner | 58088ad | 2011-07-21 09:43:31 -0700 | [diff] [blame] | 1776 | else { |
Paul Turner | a9cf55b | 2011-07-21 09:43:32 -0700 | [diff] [blame] | 1777 | /* |
| 1778 | * If the bandwidth pool has become inactive, then at least one |
| 1779 | * period must have elapsed since the last consumption. |
| 1780 | * Refresh the global state and ensure bandwidth timer becomes |
| 1781 | * active. |
| 1782 | */ |
| 1783 | if (!cfs_b->timer_active) { |
| 1784 | __refill_cfs_bandwidth_runtime(cfs_b); |
Paul Turner | 58088ad | 2011-07-21 09:43:31 -0700 | [diff] [blame] | 1785 | __start_cfs_bandwidth(cfs_b); |
Paul Turner | a9cf55b | 2011-07-21 09:43:32 -0700 | [diff] [blame] | 1786 | } |
Paul Turner | 58088ad | 2011-07-21 09:43:31 -0700 | [diff] [blame] | 1787 | |
| 1788 | if (cfs_b->runtime > 0) { |
| 1789 | amount = min(cfs_b->runtime, min_amount); |
| 1790 | cfs_b->runtime -= amount; |
| 1791 | cfs_b->idle = 0; |
| 1792 | } |
Paul Turner | ec12cb7 | 2011-07-21 09:43:30 -0700 | [diff] [blame] | 1793 | } |
Paul Turner | a9cf55b | 2011-07-21 09:43:32 -0700 | [diff] [blame] | 1794 | expires = cfs_b->runtime_expires; |
Paul Turner | ec12cb7 | 2011-07-21 09:43:30 -0700 | [diff] [blame] | 1795 | raw_spin_unlock(&cfs_b->lock); |
| 1796 | |
| 1797 | cfs_rq->runtime_remaining += amount; |
Paul Turner | a9cf55b | 2011-07-21 09:43:32 -0700 | [diff] [blame] | 1798 | /* |
| 1799 | * we may have advanced our local expiration to account for allowed |
| 1800 | * spread between our sched_clock and the one on which runtime was |
| 1801 | * issued. |
| 1802 | */ |
| 1803 | if ((s64)(expires - cfs_rq->runtime_expires) > 0) |
| 1804 | cfs_rq->runtime_expires = expires; |
Paul Turner | 85dac90 | 2011-07-21 09:43:33 -0700 | [diff] [blame] | 1805 | |
| 1806 | return cfs_rq->runtime_remaining > 0; |
Paul Turner | a9cf55b | 2011-07-21 09:43:32 -0700 | [diff] [blame] | 1807 | } |
| 1808 | |
| 1809 | /* |
| 1810 | * Note: This depends on the synchronization provided by sched_clock and the |
| 1811 | * fact that rq->clock snapshots this value. |
| 1812 | */ |
| 1813 | static void expire_cfs_rq_runtime(struct cfs_rq *cfs_rq) |
| 1814 | { |
| 1815 | struct cfs_bandwidth *cfs_b = tg_cfs_bandwidth(cfs_rq->tg); |
| 1816 | struct rq *rq = rq_of(cfs_rq); |
| 1817 | |
| 1818 | /* if the deadline is ahead of our clock, nothing to do */ |
| 1819 | if (likely((s64)(rq->clock - cfs_rq->runtime_expires) < 0)) |
| 1820 | return; |
| 1821 | |
| 1822 | if (cfs_rq->runtime_remaining < 0) |
| 1823 | return; |
| 1824 | |
| 1825 | /* |
| 1826 | * If the local deadline has passed we have to consider the |
| 1827 | * possibility that our sched_clock is 'fast' and the global deadline |
| 1828 | * has not truly expired. |
| 1829 | * |
| 1830 | * Fortunately we can check determine whether this the case by checking |
| 1831 | * whether the global deadline has advanced. |
| 1832 | */ |
| 1833 | |
| 1834 | if ((s64)(cfs_rq->runtime_expires - cfs_b->runtime_expires) >= 0) { |
| 1835 | /* extend local deadline, drift is bounded above by 2 ticks */ |
| 1836 | cfs_rq->runtime_expires += TICK_NSEC; |
| 1837 | } else { |
| 1838 | /* global deadline is ahead, expiration has passed */ |
| 1839 | cfs_rq->runtime_remaining = 0; |
| 1840 | } |
Paul Turner | ec12cb7 | 2011-07-21 09:43:30 -0700 | [diff] [blame] | 1841 | } |
| 1842 | |
| 1843 | static void __account_cfs_rq_runtime(struct cfs_rq *cfs_rq, |
| 1844 | unsigned long delta_exec) |
| 1845 | { |
Paul Turner | a9cf55b | 2011-07-21 09:43:32 -0700 | [diff] [blame] | 1846 | /* dock delta_exec before expiring quota (as it could span periods) */ |
Paul Turner | ec12cb7 | 2011-07-21 09:43:30 -0700 | [diff] [blame] | 1847 | cfs_rq->runtime_remaining -= delta_exec; |
Paul Turner | a9cf55b | 2011-07-21 09:43:32 -0700 | [diff] [blame] | 1848 | expire_cfs_rq_runtime(cfs_rq); |
| 1849 | |
| 1850 | if (likely(cfs_rq->runtime_remaining > 0)) |
Paul Turner | ec12cb7 | 2011-07-21 09:43:30 -0700 | [diff] [blame] | 1851 | return; |
| 1852 | |
Paul Turner | 85dac90 | 2011-07-21 09:43:33 -0700 | [diff] [blame] | 1853 | /* |
| 1854 | * if we're unable to extend our runtime we resched so that the active |
| 1855 | * hierarchy can be throttled |
| 1856 | */ |
| 1857 | if (!assign_cfs_rq_runtime(cfs_rq) && likely(cfs_rq->curr)) |
| 1858 | resched_task(rq_of(cfs_rq)->curr); |
Paul Turner | ec12cb7 | 2011-07-21 09:43:30 -0700 | [diff] [blame] | 1859 | } |
| 1860 | |
Peter Zijlstra | 6c16a6d | 2012-03-21 13:07:16 -0700 | [diff] [blame] | 1861 | static __always_inline |
| 1862 | 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] | 1863 | { |
Paul Turner | 56f570e | 2011-11-07 20:26:33 -0800 | [diff] [blame] | 1864 | if (!cfs_bandwidth_used() || !cfs_rq->runtime_enabled) |
Paul Turner | ec12cb7 | 2011-07-21 09:43:30 -0700 | [diff] [blame] | 1865 | return; |
| 1866 | |
| 1867 | __account_cfs_rq_runtime(cfs_rq, delta_exec); |
| 1868 | } |
| 1869 | |
Paul Turner | 85dac90 | 2011-07-21 09:43:33 -0700 | [diff] [blame] | 1870 | static inline int cfs_rq_throttled(struct cfs_rq *cfs_rq) |
| 1871 | { |
Paul Turner | 56f570e | 2011-11-07 20:26:33 -0800 | [diff] [blame] | 1872 | return cfs_bandwidth_used() && cfs_rq->throttled; |
Paul Turner | 85dac90 | 2011-07-21 09:43:33 -0700 | [diff] [blame] | 1873 | } |
| 1874 | |
Paul Turner | 64660c8 | 2011-07-21 09:43:36 -0700 | [diff] [blame] | 1875 | /* check whether cfs_rq, or any parent, is throttled */ |
| 1876 | static inline int throttled_hierarchy(struct cfs_rq *cfs_rq) |
| 1877 | { |
Paul Turner | 56f570e | 2011-11-07 20:26:33 -0800 | [diff] [blame] | 1878 | return cfs_bandwidth_used() && cfs_rq->throttle_count; |
Paul Turner | 64660c8 | 2011-07-21 09:43:36 -0700 | [diff] [blame] | 1879 | } |
| 1880 | |
| 1881 | /* |
| 1882 | * Ensure that neither of the group entities corresponding to src_cpu or |
| 1883 | * dest_cpu are members of a throttled hierarchy when performing group |
| 1884 | * load-balance operations. |
| 1885 | */ |
| 1886 | static inline int throttled_lb_pair(struct task_group *tg, |
| 1887 | int src_cpu, int dest_cpu) |
| 1888 | { |
| 1889 | struct cfs_rq *src_cfs_rq, *dest_cfs_rq; |
| 1890 | |
| 1891 | src_cfs_rq = tg->cfs_rq[src_cpu]; |
| 1892 | dest_cfs_rq = tg->cfs_rq[dest_cpu]; |
| 1893 | |
| 1894 | return throttled_hierarchy(src_cfs_rq) || |
| 1895 | throttled_hierarchy(dest_cfs_rq); |
| 1896 | } |
| 1897 | |
| 1898 | /* updated child weight may affect parent so we have to do this bottom up */ |
| 1899 | static int tg_unthrottle_up(struct task_group *tg, void *data) |
| 1900 | { |
| 1901 | struct rq *rq = data; |
| 1902 | struct cfs_rq *cfs_rq = tg->cfs_rq[cpu_of(rq)]; |
| 1903 | |
| 1904 | cfs_rq->throttle_count--; |
| 1905 | #ifdef CONFIG_SMP |
| 1906 | if (!cfs_rq->throttle_count) { |
Paul Turner | f1b1728 | 2012-10-04 13:18:31 +0200 | [diff] [blame] | 1907 | /* adjust cfs_rq_clock_task() */ |
| 1908 | cfs_rq->throttled_clock_task_time += rq->clock_task - |
| 1909 | cfs_rq->throttled_clock_task; |
Paul Turner | 64660c8 | 2011-07-21 09:43:36 -0700 | [diff] [blame] | 1910 | } |
| 1911 | #endif |
| 1912 | |
| 1913 | return 0; |
| 1914 | } |
| 1915 | |
| 1916 | static int tg_throttle_down(struct task_group *tg, void *data) |
| 1917 | { |
| 1918 | struct rq *rq = data; |
| 1919 | struct cfs_rq *cfs_rq = tg->cfs_rq[cpu_of(rq)]; |
| 1920 | |
Paul Turner | 8295836 | 2012-10-04 13:18:31 +0200 | [diff] [blame^] | 1921 | /* group is entering throttled state, stop time */ |
| 1922 | if (!cfs_rq->throttle_count) |
Paul Turner | f1b1728 | 2012-10-04 13:18:31 +0200 | [diff] [blame] | 1923 | cfs_rq->throttled_clock_task = rq->clock_task; |
Paul Turner | 64660c8 | 2011-07-21 09:43:36 -0700 | [diff] [blame] | 1924 | cfs_rq->throttle_count++; |
| 1925 | |
| 1926 | return 0; |
| 1927 | } |
| 1928 | |
Paul Turner | d3d9dc3 | 2011-07-21 09:43:39 -0700 | [diff] [blame] | 1929 | static void throttle_cfs_rq(struct cfs_rq *cfs_rq) |
Paul Turner | 85dac90 | 2011-07-21 09:43:33 -0700 | [diff] [blame] | 1930 | { |
| 1931 | struct rq *rq = rq_of(cfs_rq); |
| 1932 | struct cfs_bandwidth *cfs_b = tg_cfs_bandwidth(cfs_rq->tg); |
| 1933 | struct sched_entity *se; |
| 1934 | long task_delta, dequeue = 1; |
| 1935 | |
| 1936 | se = cfs_rq->tg->se[cpu_of(rq_of(cfs_rq))]; |
| 1937 | |
Paul Turner | f1b1728 | 2012-10-04 13:18:31 +0200 | [diff] [blame] | 1938 | /* freeze hierarchy runnable averages while throttled */ |
Paul Turner | 64660c8 | 2011-07-21 09:43:36 -0700 | [diff] [blame] | 1939 | rcu_read_lock(); |
| 1940 | walk_tg_tree_from(cfs_rq->tg, tg_throttle_down, tg_nop, (void *)rq); |
| 1941 | rcu_read_unlock(); |
Paul Turner | 85dac90 | 2011-07-21 09:43:33 -0700 | [diff] [blame] | 1942 | |
| 1943 | task_delta = cfs_rq->h_nr_running; |
| 1944 | for_each_sched_entity(se) { |
| 1945 | struct cfs_rq *qcfs_rq = cfs_rq_of(se); |
| 1946 | /* throttled entity or throttle-on-deactivate */ |
| 1947 | if (!se->on_rq) |
| 1948 | break; |
| 1949 | |
| 1950 | if (dequeue) |
| 1951 | dequeue_entity(qcfs_rq, se, DEQUEUE_SLEEP); |
| 1952 | qcfs_rq->h_nr_running -= task_delta; |
| 1953 | |
| 1954 | if (qcfs_rq->load.weight) |
| 1955 | dequeue = 0; |
| 1956 | } |
| 1957 | |
| 1958 | if (!se) |
| 1959 | rq->nr_running -= task_delta; |
| 1960 | |
| 1961 | cfs_rq->throttled = 1; |
Paul Turner | f1b1728 | 2012-10-04 13:18:31 +0200 | [diff] [blame] | 1962 | cfs_rq->throttled_clock = rq->clock; |
Paul Turner | 85dac90 | 2011-07-21 09:43:33 -0700 | [diff] [blame] | 1963 | raw_spin_lock(&cfs_b->lock); |
| 1964 | list_add_tail_rcu(&cfs_rq->throttled_list, &cfs_b->throttled_cfs_rq); |
| 1965 | raw_spin_unlock(&cfs_b->lock); |
| 1966 | } |
| 1967 | |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 1968 | void unthrottle_cfs_rq(struct cfs_rq *cfs_rq) |
Paul Turner | 671fd9d | 2011-07-21 09:43:34 -0700 | [diff] [blame] | 1969 | { |
| 1970 | struct rq *rq = rq_of(cfs_rq); |
| 1971 | struct cfs_bandwidth *cfs_b = tg_cfs_bandwidth(cfs_rq->tg); |
| 1972 | struct sched_entity *se; |
| 1973 | int enqueue = 1; |
| 1974 | long task_delta; |
| 1975 | |
| 1976 | se = cfs_rq->tg->se[cpu_of(rq_of(cfs_rq))]; |
| 1977 | |
| 1978 | cfs_rq->throttled = 0; |
| 1979 | raw_spin_lock(&cfs_b->lock); |
Paul Turner | f1b1728 | 2012-10-04 13:18:31 +0200 | [diff] [blame] | 1980 | cfs_b->throttled_time += rq->clock - cfs_rq->throttled_clock; |
Paul Turner | 671fd9d | 2011-07-21 09:43:34 -0700 | [diff] [blame] | 1981 | list_del_rcu(&cfs_rq->throttled_list); |
| 1982 | raw_spin_unlock(&cfs_b->lock); |
| 1983 | |
Paul Turner | 64660c8 | 2011-07-21 09:43:36 -0700 | [diff] [blame] | 1984 | update_rq_clock(rq); |
| 1985 | /* update hierarchical throttle state */ |
| 1986 | walk_tg_tree_from(cfs_rq->tg, tg_nop, tg_unthrottle_up, (void *)rq); |
| 1987 | |
Paul Turner | 671fd9d | 2011-07-21 09:43:34 -0700 | [diff] [blame] | 1988 | if (!cfs_rq->load.weight) |
| 1989 | return; |
| 1990 | |
| 1991 | task_delta = cfs_rq->h_nr_running; |
| 1992 | for_each_sched_entity(se) { |
| 1993 | if (se->on_rq) |
| 1994 | enqueue = 0; |
| 1995 | |
| 1996 | cfs_rq = cfs_rq_of(se); |
| 1997 | if (enqueue) |
| 1998 | enqueue_entity(cfs_rq, se, ENQUEUE_WAKEUP); |
| 1999 | cfs_rq->h_nr_running += task_delta; |
| 2000 | |
| 2001 | if (cfs_rq_throttled(cfs_rq)) |
| 2002 | break; |
| 2003 | } |
| 2004 | |
| 2005 | if (!se) |
| 2006 | rq->nr_running += task_delta; |
| 2007 | |
| 2008 | /* determine whether we need to wake up potentially idle cpu */ |
| 2009 | if (rq->curr == rq->idle && rq->cfs.nr_running) |
| 2010 | resched_task(rq->curr); |
| 2011 | } |
| 2012 | |
| 2013 | static u64 distribute_cfs_runtime(struct cfs_bandwidth *cfs_b, |
| 2014 | u64 remaining, u64 expires) |
| 2015 | { |
| 2016 | struct cfs_rq *cfs_rq; |
| 2017 | u64 runtime = remaining; |
| 2018 | |
| 2019 | rcu_read_lock(); |
| 2020 | list_for_each_entry_rcu(cfs_rq, &cfs_b->throttled_cfs_rq, |
| 2021 | throttled_list) { |
| 2022 | struct rq *rq = rq_of(cfs_rq); |
| 2023 | |
| 2024 | raw_spin_lock(&rq->lock); |
| 2025 | if (!cfs_rq_throttled(cfs_rq)) |
| 2026 | goto next; |
| 2027 | |
| 2028 | runtime = -cfs_rq->runtime_remaining + 1; |
| 2029 | if (runtime > remaining) |
| 2030 | runtime = remaining; |
| 2031 | remaining -= runtime; |
| 2032 | |
| 2033 | cfs_rq->runtime_remaining += runtime; |
| 2034 | cfs_rq->runtime_expires = expires; |
| 2035 | |
| 2036 | /* we check whether we're throttled above */ |
| 2037 | if (cfs_rq->runtime_remaining > 0) |
| 2038 | unthrottle_cfs_rq(cfs_rq); |
| 2039 | |
| 2040 | next: |
| 2041 | raw_spin_unlock(&rq->lock); |
| 2042 | |
| 2043 | if (!remaining) |
| 2044 | break; |
| 2045 | } |
| 2046 | rcu_read_unlock(); |
| 2047 | |
| 2048 | return remaining; |
| 2049 | } |
| 2050 | |
Paul Turner | 58088ad | 2011-07-21 09:43:31 -0700 | [diff] [blame] | 2051 | /* |
| 2052 | * Responsible for refilling a task_group's bandwidth and unthrottling its |
| 2053 | * cfs_rqs as appropriate. If there has been no activity within the last |
| 2054 | * period the timer is deactivated until scheduling resumes; cfs_b->idle is |
| 2055 | * used to track this state. |
| 2056 | */ |
| 2057 | static int do_sched_cfs_period_timer(struct cfs_bandwidth *cfs_b, int overrun) |
| 2058 | { |
Paul Turner | 671fd9d | 2011-07-21 09:43:34 -0700 | [diff] [blame] | 2059 | u64 runtime, runtime_expires; |
| 2060 | int idle = 1, throttled; |
Paul Turner | 58088ad | 2011-07-21 09:43:31 -0700 | [diff] [blame] | 2061 | |
| 2062 | raw_spin_lock(&cfs_b->lock); |
| 2063 | /* no need to continue the timer with no bandwidth constraint */ |
| 2064 | if (cfs_b->quota == RUNTIME_INF) |
| 2065 | goto out_unlock; |
| 2066 | |
Paul Turner | 671fd9d | 2011-07-21 09:43:34 -0700 | [diff] [blame] | 2067 | throttled = !list_empty(&cfs_b->throttled_cfs_rq); |
| 2068 | /* idle depends on !throttled (for the case of a large deficit) */ |
| 2069 | idle = cfs_b->idle && !throttled; |
Nikhil Rao | e8da1b1 | 2011-07-21 09:43:40 -0700 | [diff] [blame] | 2070 | cfs_b->nr_periods += overrun; |
Paul Turner | 671fd9d | 2011-07-21 09:43:34 -0700 | [diff] [blame] | 2071 | |
Paul Turner | a9cf55b | 2011-07-21 09:43:32 -0700 | [diff] [blame] | 2072 | /* if we're going inactive then everything else can be deferred */ |
| 2073 | if (idle) |
| 2074 | goto out_unlock; |
| 2075 | |
| 2076 | __refill_cfs_bandwidth_runtime(cfs_b); |
| 2077 | |
Paul Turner | 671fd9d | 2011-07-21 09:43:34 -0700 | [diff] [blame] | 2078 | if (!throttled) { |
| 2079 | /* mark as potentially idle for the upcoming period */ |
| 2080 | cfs_b->idle = 1; |
| 2081 | goto out_unlock; |
| 2082 | } |
Paul Turner | 58088ad | 2011-07-21 09:43:31 -0700 | [diff] [blame] | 2083 | |
Nikhil Rao | e8da1b1 | 2011-07-21 09:43:40 -0700 | [diff] [blame] | 2084 | /* account preceding periods in which throttling occurred */ |
| 2085 | cfs_b->nr_throttled += overrun; |
| 2086 | |
Paul Turner | 671fd9d | 2011-07-21 09:43:34 -0700 | [diff] [blame] | 2087 | /* |
| 2088 | * There are throttled entities so we must first use the new bandwidth |
| 2089 | * to unthrottle them before making it generally available. This |
| 2090 | * ensures that all existing debts will be paid before a new cfs_rq is |
| 2091 | * allowed to run. |
| 2092 | */ |
| 2093 | runtime = cfs_b->runtime; |
| 2094 | runtime_expires = cfs_b->runtime_expires; |
| 2095 | cfs_b->runtime = 0; |
| 2096 | |
| 2097 | /* |
| 2098 | * This check is repeated as we are holding onto the new bandwidth |
| 2099 | * while we unthrottle. This can potentially race with an unthrottled |
| 2100 | * group trying to acquire new bandwidth from the global pool. |
| 2101 | */ |
| 2102 | while (throttled && runtime > 0) { |
| 2103 | raw_spin_unlock(&cfs_b->lock); |
| 2104 | /* we can't nest cfs_b->lock while distributing bandwidth */ |
| 2105 | runtime = distribute_cfs_runtime(cfs_b, runtime, |
| 2106 | runtime_expires); |
| 2107 | raw_spin_lock(&cfs_b->lock); |
| 2108 | |
| 2109 | throttled = !list_empty(&cfs_b->throttled_cfs_rq); |
| 2110 | } |
| 2111 | |
| 2112 | /* return (any) remaining runtime */ |
| 2113 | cfs_b->runtime = runtime; |
| 2114 | /* |
| 2115 | * While we are ensured activity in the period following an |
| 2116 | * unthrottle, this also covers the case in which the new bandwidth is |
| 2117 | * insufficient to cover the existing bandwidth deficit. (Forcing the |
| 2118 | * timer to remain active while there are any throttled entities.) |
| 2119 | */ |
| 2120 | cfs_b->idle = 0; |
Paul Turner | 58088ad | 2011-07-21 09:43:31 -0700 | [diff] [blame] | 2121 | out_unlock: |
| 2122 | if (idle) |
| 2123 | cfs_b->timer_active = 0; |
| 2124 | raw_spin_unlock(&cfs_b->lock); |
| 2125 | |
| 2126 | return idle; |
| 2127 | } |
Paul Turner | d3d9dc3 | 2011-07-21 09:43:39 -0700 | [diff] [blame] | 2128 | |
Paul Turner | d8b4986 | 2011-07-21 09:43:41 -0700 | [diff] [blame] | 2129 | /* a cfs_rq won't donate quota below this amount */ |
| 2130 | static const u64 min_cfs_rq_runtime = 1 * NSEC_PER_MSEC; |
| 2131 | /* minimum remaining period time to redistribute slack quota */ |
| 2132 | static const u64 min_bandwidth_expiration = 2 * NSEC_PER_MSEC; |
| 2133 | /* how long we wait to gather additional slack before distributing */ |
| 2134 | static const u64 cfs_bandwidth_slack_period = 5 * NSEC_PER_MSEC; |
| 2135 | |
| 2136 | /* are we near the end of the current quota period? */ |
| 2137 | static int runtime_refresh_within(struct cfs_bandwidth *cfs_b, u64 min_expire) |
| 2138 | { |
| 2139 | struct hrtimer *refresh_timer = &cfs_b->period_timer; |
| 2140 | u64 remaining; |
| 2141 | |
| 2142 | /* if the call-back is running a quota refresh is already occurring */ |
| 2143 | if (hrtimer_callback_running(refresh_timer)) |
| 2144 | return 1; |
| 2145 | |
| 2146 | /* is a quota refresh about to occur? */ |
| 2147 | remaining = ktime_to_ns(hrtimer_expires_remaining(refresh_timer)); |
| 2148 | if (remaining < min_expire) |
| 2149 | return 1; |
| 2150 | |
| 2151 | return 0; |
| 2152 | } |
| 2153 | |
| 2154 | static void start_cfs_slack_bandwidth(struct cfs_bandwidth *cfs_b) |
| 2155 | { |
| 2156 | u64 min_left = cfs_bandwidth_slack_period + min_bandwidth_expiration; |
| 2157 | |
| 2158 | /* if there's a quota refresh soon don't bother with slack */ |
| 2159 | if (runtime_refresh_within(cfs_b, min_left)) |
| 2160 | return; |
| 2161 | |
| 2162 | start_bandwidth_timer(&cfs_b->slack_timer, |
| 2163 | ns_to_ktime(cfs_bandwidth_slack_period)); |
| 2164 | } |
| 2165 | |
| 2166 | /* we know any runtime found here is valid as update_curr() precedes return */ |
| 2167 | static void __return_cfs_rq_runtime(struct cfs_rq *cfs_rq) |
| 2168 | { |
| 2169 | struct cfs_bandwidth *cfs_b = tg_cfs_bandwidth(cfs_rq->tg); |
| 2170 | s64 slack_runtime = cfs_rq->runtime_remaining - min_cfs_rq_runtime; |
| 2171 | |
| 2172 | if (slack_runtime <= 0) |
| 2173 | return; |
| 2174 | |
| 2175 | raw_spin_lock(&cfs_b->lock); |
| 2176 | if (cfs_b->quota != RUNTIME_INF && |
| 2177 | cfs_rq->runtime_expires == cfs_b->runtime_expires) { |
| 2178 | cfs_b->runtime += slack_runtime; |
| 2179 | |
| 2180 | /* we are under rq->lock, defer unthrottling using a timer */ |
| 2181 | if (cfs_b->runtime > sched_cfs_bandwidth_slice() && |
| 2182 | !list_empty(&cfs_b->throttled_cfs_rq)) |
| 2183 | start_cfs_slack_bandwidth(cfs_b); |
| 2184 | } |
| 2185 | raw_spin_unlock(&cfs_b->lock); |
| 2186 | |
| 2187 | /* even if it's not valid for return we don't want to try again */ |
| 2188 | cfs_rq->runtime_remaining -= slack_runtime; |
| 2189 | } |
| 2190 | |
| 2191 | static __always_inline void return_cfs_rq_runtime(struct cfs_rq *cfs_rq) |
| 2192 | { |
Paul Turner | 56f570e | 2011-11-07 20:26:33 -0800 | [diff] [blame] | 2193 | if (!cfs_bandwidth_used()) |
| 2194 | return; |
| 2195 | |
Paul Turner | fccfdc6 | 2011-11-07 20:26:34 -0800 | [diff] [blame] | 2196 | if (!cfs_rq->runtime_enabled || cfs_rq->nr_running) |
Paul Turner | d8b4986 | 2011-07-21 09:43:41 -0700 | [diff] [blame] | 2197 | return; |
| 2198 | |
| 2199 | __return_cfs_rq_runtime(cfs_rq); |
| 2200 | } |
| 2201 | |
| 2202 | /* |
| 2203 | * This is done with a timer (instead of inline with bandwidth return) since |
| 2204 | * it's necessary to juggle rq->locks to unthrottle their respective cfs_rqs. |
| 2205 | */ |
| 2206 | static void do_sched_cfs_slack_timer(struct cfs_bandwidth *cfs_b) |
| 2207 | { |
| 2208 | u64 runtime = 0, slice = sched_cfs_bandwidth_slice(); |
| 2209 | u64 expires; |
| 2210 | |
| 2211 | /* confirm we're still not at a refresh boundary */ |
| 2212 | if (runtime_refresh_within(cfs_b, min_bandwidth_expiration)) |
| 2213 | return; |
| 2214 | |
| 2215 | raw_spin_lock(&cfs_b->lock); |
| 2216 | if (cfs_b->quota != RUNTIME_INF && cfs_b->runtime > slice) { |
| 2217 | runtime = cfs_b->runtime; |
| 2218 | cfs_b->runtime = 0; |
| 2219 | } |
| 2220 | expires = cfs_b->runtime_expires; |
| 2221 | raw_spin_unlock(&cfs_b->lock); |
| 2222 | |
| 2223 | if (!runtime) |
| 2224 | return; |
| 2225 | |
| 2226 | runtime = distribute_cfs_runtime(cfs_b, runtime, expires); |
| 2227 | |
| 2228 | raw_spin_lock(&cfs_b->lock); |
| 2229 | if (expires == cfs_b->runtime_expires) |
| 2230 | cfs_b->runtime = runtime; |
| 2231 | raw_spin_unlock(&cfs_b->lock); |
| 2232 | } |
| 2233 | |
Paul Turner | d3d9dc3 | 2011-07-21 09:43:39 -0700 | [diff] [blame] | 2234 | /* |
| 2235 | * When a group wakes up we want to make sure that its quota is not already |
| 2236 | * expired/exceeded, otherwise it may be allowed to steal additional ticks of |
| 2237 | * runtime as update_curr() throttling can not not trigger until it's on-rq. |
| 2238 | */ |
| 2239 | static void check_enqueue_throttle(struct cfs_rq *cfs_rq) |
| 2240 | { |
Paul Turner | 56f570e | 2011-11-07 20:26:33 -0800 | [diff] [blame] | 2241 | if (!cfs_bandwidth_used()) |
| 2242 | return; |
| 2243 | |
Paul Turner | d3d9dc3 | 2011-07-21 09:43:39 -0700 | [diff] [blame] | 2244 | /* an active group must be handled by the update_curr()->put() path */ |
| 2245 | if (!cfs_rq->runtime_enabled || cfs_rq->curr) |
| 2246 | return; |
| 2247 | |
| 2248 | /* ensure the group is not already throttled */ |
| 2249 | if (cfs_rq_throttled(cfs_rq)) |
| 2250 | return; |
| 2251 | |
| 2252 | /* update runtime allocation */ |
| 2253 | account_cfs_rq_runtime(cfs_rq, 0); |
| 2254 | if (cfs_rq->runtime_remaining <= 0) |
| 2255 | throttle_cfs_rq(cfs_rq); |
| 2256 | } |
| 2257 | |
| 2258 | /* conditionally throttle active cfs_rq's from put_prev_entity() */ |
| 2259 | static void check_cfs_rq_runtime(struct cfs_rq *cfs_rq) |
| 2260 | { |
Paul Turner | 56f570e | 2011-11-07 20:26:33 -0800 | [diff] [blame] | 2261 | if (!cfs_bandwidth_used()) |
| 2262 | return; |
| 2263 | |
Paul Turner | d3d9dc3 | 2011-07-21 09:43:39 -0700 | [diff] [blame] | 2264 | if (likely(!cfs_rq->runtime_enabled || cfs_rq->runtime_remaining > 0)) |
| 2265 | return; |
| 2266 | |
| 2267 | /* |
| 2268 | * it's possible for a throttled entity to be forced into a running |
| 2269 | * state (e.g. set_curr_task), in this case we're finished. |
| 2270 | */ |
| 2271 | if (cfs_rq_throttled(cfs_rq)) |
| 2272 | return; |
| 2273 | |
| 2274 | throttle_cfs_rq(cfs_rq); |
| 2275 | } |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 2276 | |
| 2277 | static inline u64 default_cfs_period(void); |
| 2278 | static int do_sched_cfs_period_timer(struct cfs_bandwidth *cfs_b, int overrun); |
| 2279 | static void do_sched_cfs_slack_timer(struct cfs_bandwidth *cfs_b); |
| 2280 | |
| 2281 | static enum hrtimer_restart sched_cfs_slack_timer(struct hrtimer *timer) |
| 2282 | { |
| 2283 | struct cfs_bandwidth *cfs_b = |
| 2284 | container_of(timer, struct cfs_bandwidth, slack_timer); |
| 2285 | do_sched_cfs_slack_timer(cfs_b); |
| 2286 | |
| 2287 | return HRTIMER_NORESTART; |
| 2288 | } |
| 2289 | |
| 2290 | static enum hrtimer_restart sched_cfs_period_timer(struct hrtimer *timer) |
| 2291 | { |
| 2292 | struct cfs_bandwidth *cfs_b = |
| 2293 | container_of(timer, struct cfs_bandwidth, period_timer); |
| 2294 | ktime_t now; |
| 2295 | int overrun; |
| 2296 | int idle = 0; |
| 2297 | |
| 2298 | for (;;) { |
| 2299 | now = hrtimer_cb_get_time(timer); |
| 2300 | overrun = hrtimer_forward(timer, now, cfs_b->period); |
| 2301 | |
| 2302 | if (!overrun) |
| 2303 | break; |
| 2304 | |
| 2305 | idle = do_sched_cfs_period_timer(cfs_b, overrun); |
| 2306 | } |
| 2307 | |
| 2308 | return idle ? HRTIMER_NORESTART : HRTIMER_RESTART; |
| 2309 | } |
| 2310 | |
| 2311 | void init_cfs_bandwidth(struct cfs_bandwidth *cfs_b) |
| 2312 | { |
| 2313 | raw_spin_lock_init(&cfs_b->lock); |
| 2314 | cfs_b->runtime = 0; |
| 2315 | cfs_b->quota = RUNTIME_INF; |
| 2316 | cfs_b->period = ns_to_ktime(default_cfs_period()); |
| 2317 | |
| 2318 | INIT_LIST_HEAD(&cfs_b->throttled_cfs_rq); |
| 2319 | hrtimer_init(&cfs_b->period_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL); |
| 2320 | cfs_b->period_timer.function = sched_cfs_period_timer; |
| 2321 | hrtimer_init(&cfs_b->slack_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL); |
| 2322 | cfs_b->slack_timer.function = sched_cfs_slack_timer; |
| 2323 | } |
| 2324 | |
| 2325 | static void init_cfs_rq_runtime(struct cfs_rq *cfs_rq) |
| 2326 | { |
| 2327 | cfs_rq->runtime_enabled = 0; |
| 2328 | INIT_LIST_HEAD(&cfs_rq->throttled_list); |
| 2329 | } |
| 2330 | |
| 2331 | /* requires cfs_b->lock, may release to reprogram timer */ |
| 2332 | void __start_cfs_bandwidth(struct cfs_bandwidth *cfs_b) |
| 2333 | { |
| 2334 | /* |
| 2335 | * The timer may be active because we're trying to set a new bandwidth |
| 2336 | * period or because we're racing with the tear-down path |
| 2337 | * (timer_active==0 becomes visible before the hrtimer call-back |
| 2338 | * terminates). In either case we ensure that it's re-programmed |
| 2339 | */ |
| 2340 | while (unlikely(hrtimer_active(&cfs_b->period_timer))) { |
| 2341 | raw_spin_unlock(&cfs_b->lock); |
| 2342 | /* ensure cfs_b->lock is available while we wait */ |
| 2343 | hrtimer_cancel(&cfs_b->period_timer); |
| 2344 | |
| 2345 | raw_spin_lock(&cfs_b->lock); |
| 2346 | /* if someone else restarted the timer then we're done */ |
| 2347 | if (cfs_b->timer_active) |
| 2348 | return; |
| 2349 | } |
| 2350 | |
| 2351 | cfs_b->timer_active = 1; |
| 2352 | start_bandwidth_timer(&cfs_b->period_timer, cfs_b->period); |
| 2353 | } |
| 2354 | |
| 2355 | static void destroy_cfs_bandwidth(struct cfs_bandwidth *cfs_b) |
| 2356 | { |
| 2357 | hrtimer_cancel(&cfs_b->period_timer); |
| 2358 | hrtimer_cancel(&cfs_b->slack_timer); |
| 2359 | } |
| 2360 | |
Peter Boonstoppel | a4c96ae | 2012-08-09 15:34:47 -0700 | [diff] [blame] | 2361 | static void unthrottle_offline_cfs_rqs(struct rq *rq) |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 2362 | { |
| 2363 | struct cfs_rq *cfs_rq; |
| 2364 | |
| 2365 | for_each_leaf_cfs_rq(rq, cfs_rq) { |
| 2366 | struct cfs_bandwidth *cfs_b = tg_cfs_bandwidth(cfs_rq->tg); |
| 2367 | |
| 2368 | if (!cfs_rq->runtime_enabled) |
| 2369 | continue; |
| 2370 | |
| 2371 | /* |
| 2372 | * clock_task is not advancing so we just need to make sure |
| 2373 | * there's some valid quota amount |
| 2374 | */ |
| 2375 | cfs_rq->runtime_remaining = cfs_b->quota; |
| 2376 | if (cfs_rq_throttled(cfs_rq)) |
| 2377 | unthrottle_cfs_rq(cfs_rq); |
| 2378 | } |
| 2379 | } |
| 2380 | |
| 2381 | #else /* CONFIG_CFS_BANDWIDTH */ |
Paul Turner | f1b1728 | 2012-10-04 13:18:31 +0200 | [diff] [blame] | 2382 | static inline u64 cfs_rq_clock_task(struct cfs_rq *cfs_rq) |
| 2383 | { |
| 2384 | return rq_of(cfs_rq)->clock_task; |
| 2385 | } |
| 2386 | |
| 2387 | static void account_cfs_rq_runtime(struct cfs_rq *cfs_rq, |
| 2388 | unsigned long delta_exec) {} |
Paul Turner | d3d9dc3 | 2011-07-21 09:43:39 -0700 | [diff] [blame] | 2389 | static void check_cfs_rq_runtime(struct cfs_rq *cfs_rq) {} |
| 2390 | static void check_enqueue_throttle(struct cfs_rq *cfs_rq) {} |
Peter Zijlstra | 6c16a6d | 2012-03-21 13:07:16 -0700 | [diff] [blame] | 2391 | 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] | 2392 | |
| 2393 | static inline int cfs_rq_throttled(struct cfs_rq *cfs_rq) |
| 2394 | { |
| 2395 | return 0; |
| 2396 | } |
Paul Turner | 64660c8 | 2011-07-21 09:43:36 -0700 | [diff] [blame] | 2397 | |
| 2398 | static inline int throttled_hierarchy(struct cfs_rq *cfs_rq) |
| 2399 | { |
| 2400 | return 0; |
| 2401 | } |
| 2402 | |
| 2403 | static inline int throttled_lb_pair(struct task_group *tg, |
| 2404 | int src_cpu, int dest_cpu) |
| 2405 | { |
| 2406 | return 0; |
| 2407 | } |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 2408 | |
| 2409 | void init_cfs_bandwidth(struct cfs_bandwidth *cfs_b) {} |
| 2410 | |
| 2411 | #ifdef CONFIG_FAIR_GROUP_SCHED |
| 2412 | static void init_cfs_rq_runtime(struct cfs_rq *cfs_rq) {} |
Paul Turner | ab84d31 | 2011-07-21 09:43:28 -0700 | [diff] [blame] | 2413 | #endif |
| 2414 | |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 2415 | static inline struct cfs_bandwidth *tg_cfs_bandwidth(struct task_group *tg) |
| 2416 | { |
| 2417 | return NULL; |
| 2418 | } |
| 2419 | static inline void destroy_cfs_bandwidth(struct cfs_bandwidth *cfs_b) {} |
Peter Boonstoppel | a4c96ae | 2012-08-09 15:34:47 -0700 | [diff] [blame] | 2420 | static inline void unthrottle_offline_cfs_rqs(struct rq *rq) {} |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 2421 | |
| 2422 | #endif /* CONFIG_CFS_BANDWIDTH */ |
| 2423 | |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 2424 | /************************************************** |
| 2425 | * CFS operations on tasks: |
| 2426 | */ |
| 2427 | |
Peter Zijlstra | 8f4d37e | 2008-01-25 21:08:29 +0100 | [diff] [blame] | 2428 | #ifdef CONFIG_SCHED_HRTICK |
| 2429 | static void hrtick_start_fair(struct rq *rq, struct task_struct *p) |
| 2430 | { |
Peter Zijlstra | 8f4d37e | 2008-01-25 21:08:29 +0100 | [diff] [blame] | 2431 | struct sched_entity *se = &p->se; |
| 2432 | struct cfs_rq *cfs_rq = cfs_rq_of(se); |
| 2433 | |
| 2434 | WARN_ON(task_rq(p) != rq); |
| 2435 | |
Mike Galbraith | b39e66e | 2011-11-22 15:20:07 +0100 | [diff] [blame] | 2436 | if (cfs_rq->nr_running > 1) { |
Peter Zijlstra | 8f4d37e | 2008-01-25 21:08:29 +0100 | [diff] [blame] | 2437 | u64 slice = sched_slice(cfs_rq, se); |
| 2438 | u64 ran = se->sum_exec_runtime - se->prev_sum_exec_runtime; |
| 2439 | s64 delta = slice - ran; |
| 2440 | |
| 2441 | if (delta < 0) { |
| 2442 | if (rq->curr == p) |
| 2443 | resched_task(p); |
| 2444 | return; |
| 2445 | } |
| 2446 | |
| 2447 | /* |
| 2448 | * Don't schedule slices shorter than 10000ns, that just |
| 2449 | * doesn't make sense. Rely on vruntime for fairness. |
| 2450 | */ |
Peter Zijlstra | 3165651 | 2008-07-18 18:01:23 +0200 | [diff] [blame] | 2451 | if (rq->curr != p) |
Peter Zijlstra | 157124c | 2008-07-28 11:53:11 +0200 | [diff] [blame] | 2452 | delta = max_t(s64, 10000LL, delta); |
Peter Zijlstra | 8f4d37e | 2008-01-25 21:08:29 +0100 | [diff] [blame] | 2453 | |
Peter Zijlstra | 3165651 | 2008-07-18 18:01:23 +0200 | [diff] [blame] | 2454 | hrtick_start(rq, delta); |
Peter Zijlstra | 8f4d37e | 2008-01-25 21:08:29 +0100 | [diff] [blame] | 2455 | } |
| 2456 | } |
Peter Zijlstra | a4c2f00 | 2008-10-17 19:27:03 +0200 | [diff] [blame] | 2457 | |
| 2458 | /* |
| 2459 | * called from enqueue/dequeue and updates the hrtick when the |
| 2460 | * current task is from our class and nr_running is low enough |
| 2461 | * to matter. |
| 2462 | */ |
| 2463 | static void hrtick_update(struct rq *rq) |
| 2464 | { |
| 2465 | struct task_struct *curr = rq->curr; |
| 2466 | |
Mike Galbraith | b39e66e | 2011-11-22 15:20:07 +0100 | [diff] [blame] | 2467 | if (!hrtick_enabled(rq) || curr->sched_class != &fair_sched_class) |
Peter Zijlstra | a4c2f00 | 2008-10-17 19:27:03 +0200 | [diff] [blame] | 2468 | return; |
| 2469 | |
| 2470 | if (cfs_rq_of(&curr->se)->nr_running < sched_nr_latency) |
| 2471 | hrtick_start_fair(rq, curr); |
| 2472 | } |
Dhaval Giani | 55e12e5 | 2008-06-24 23:39:43 +0530 | [diff] [blame] | 2473 | #else /* !CONFIG_SCHED_HRTICK */ |
Peter Zijlstra | 8f4d37e | 2008-01-25 21:08:29 +0100 | [diff] [blame] | 2474 | static inline void |
| 2475 | hrtick_start_fair(struct rq *rq, struct task_struct *p) |
| 2476 | { |
| 2477 | } |
Peter Zijlstra | a4c2f00 | 2008-10-17 19:27:03 +0200 | [diff] [blame] | 2478 | |
| 2479 | static inline void hrtick_update(struct rq *rq) |
| 2480 | { |
| 2481 | } |
Peter Zijlstra | 8f4d37e | 2008-01-25 21:08:29 +0100 | [diff] [blame] | 2482 | #endif |
| 2483 | |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 2484 | /* |
| 2485 | * The enqueue_task method is called before nr_running is |
| 2486 | * increased. Here we update the fair scheduling stats and |
| 2487 | * then put the task into the rbtree: |
| 2488 | */ |
Thomas Gleixner | ea87bb7 | 2010-01-20 20:58:57 +0000 | [diff] [blame] | 2489 | static void |
Peter Zijlstra | 371fd7e | 2010-03-24 16:38:48 +0100 | [diff] [blame] | 2490 | enqueue_task_fair(struct rq *rq, struct task_struct *p, int flags) |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 2491 | { |
| 2492 | struct cfs_rq *cfs_rq; |
Peter Zijlstra | 62fb185 | 2008-02-25 17:34:02 +0100 | [diff] [blame] | 2493 | struct sched_entity *se = &p->se; |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 2494 | |
| 2495 | for_each_sched_entity(se) { |
Peter Zijlstra | 62fb185 | 2008-02-25 17:34:02 +0100 | [diff] [blame] | 2496 | if (se->on_rq) |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 2497 | break; |
| 2498 | cfs_rq = cfs_rq_of(se); |
Peter Zijlstra | 88ec22d | 2009-12-16 18:04:41 +0100 | [diff] [blame] | 2499 | enqueue_entity(cfs_rq, se, flags); |
Paul Turner | 85dac90 | 2011-07-21 09:43:33 -0700 | [diff] [blame] | 2500 | |
| 2501 | /* |
| 2502 | * end evaluation on encountering a throttled cfs_rq |
| 2503 | * |
| 2504 | * note: in the case of encountering a throttled cfs_rq we will |
| 2505 | * post the final h_nr_running increment below. |
| 2506 | */ |
| 2507 | if (cfs_rq_throttled(cfs_rq)) |
| 2508 | break; |
Paul Turner | 953bfcd | 2011-07-21 09:43:27 -0700 | [diff] [blame] | 2509 | cfs_rq->h_nr_running++; |
Paul Turner | 85dac90 | 2011-07-21 09:43:33 -0700 | [diff] [blame] | 2510 | |
Peter Zijlstra | 88ec22d | 2009-12-16 18:04:41 +0100 | [diff] [blame] | 2511 | flags = ENQUEUE_WAKEUP; |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 2512 | } |
Peter Zijlstra | 8f4d37e | 2008-01-25 21:08:29 +0100 | [diff] [blame] | 2513 | |
Peter Zijlstra | 2069dd7 | 2010-11-15 15:47:00 -0800 | [diff] [blame] | 2514 | for_each_sched_entity(se) { |
Lin Ming | 0f31714 | 2011-07-22 09:14:31 +0800 | [diff] [blame] | 2515 | cfs_rq = cfs_rq_of(se); |
Paul Turner | 953bfcd | 2011-07-21 09:43:27 -0700 | [diff] [blame] | 2516 | cfs_rq->h_nr_running++; |
Peter Zijlstra | 2069dd7 | 2010-11-15 15:47:00 -0800 | [diff] [blame] | 2517 | |
Paul Turner | 85dac90 | 2011-07-21 09:43:33 -0700 | [diff] [blame] | 2518 | if (cfs_rq_throttled(cfs_rq)) |
| 2519 | break; |
| 2520 | |
Paul Turner | 6d5ab29 | 2011-01-21 20:45:01 -0800 | [diff] [blame] | 2521 | update_cfs_shares(cfs_rq); |
Paul Turner | 9ee474f | 2012-10-04 13:18:30 +0200 | [diff] [blame] | 2522 | update_entity_load_avg(se, 1); |
Peter Zijlstra | 2069dd7 | 2010-11-15 15:47:00 -0800 | [diff] [blame] | 2523 | } |
| 2524 | |
Ben Segall | 18bf280 | 2012-10-04 12:51:20 +0200 | [diff] [blame] | 2525 | if (!se) { |
| 2526 | update_rq_runnable_avg(rq, rq->nr_running); |
Paul Turner | 85dac90 | 2011-07-21 09:43:33 -0700 | [diff] [blame] | 2527 | inc_nr_running(rq); |
Ben Segall | 18bf280 | 2012-10-04 12:51:20 +0200 | [diff] [blame] | 2528 | } |
Peter Zijlstra | a4c2f00 | 2008-10-17 19:27:03 +0200 | [diff] [blame] | 2529 | hrtick_update(rq); |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 2530 | } |
| 2531 | |
Venkatesh Pallipadi | 2f36825 | 2011-04-14 10:30:53 -0700 | [diff] [blame] | 2532 | static void set_next_buddy(struct sched_entity *se); |
| 2533 | |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 2534 | /* |
| 2535 | * The dequeue_task method is called before nr_running is |
| 2536 | * decreased. We remove the task from the rbtree and |
| 2537 | * update the fair scheduling stats: |
| 2538 | */ |
Peter Zijlstra | 371fd7e | 2010-03-24 16:38:48 +0100 | [diff] [blame] | 2539 | 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] | 2540 | { |
| 2541 | struct cfs_rq *cfs_rq; |
Peter Zijlstra | 62fb185 | 2008-02-25 17:34:02 +0100 | [diff] [blame] | 2542 | struct sched_entity *se = &p->se; |
Venkatesh Pallipadi | 2f36825 | 2011-04-14 10:30:53 -0700 | [diff] [blame] | 2543 | int task_sleep = flags & DEQUEUE_SLEEP; |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 2544 | |
| 2545 | for_each_sched_entity(se) { |
| 2546 | cfs_rq = cfs_rq_of(se); |
Peter Zijlstra | 371fd7e | 2010-03-24 16:38:48 +0100 | [diff] [blame] | 2547 | dequeue_entity(cfs_rq, se, flags); |
Paul Turner | 85dac90 | 2011-07-21 09:43:33 -0700 | [diff] [blame] | 2548 | |
| 2549 | /* |
| 2550 | * end evaluation on encountering a throttled cfs_rq |
| 2551 | * |
| 2552 | * note: in the case of encountering a throttled cfs_rq we will |
| 2553 | * post the final h_nr_running decrement below. |
| 2554 | */ |
| 2555 | if (cfs_rq_throttled(cfs_rq)) |
| 2556 | break; |
Paul Turner | 953bfcd | 2011-07-21 09:43:27 -0700 | [diff] [blame] | 2557 | cfs_rq->h_nr_running--; |
Peter Zijlstra | 2069dd7 | 2010-11-15 15:47:00 -0800 | [diff] [blame] | 2558 | |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 2559 | /* Don't dequeue parent if it has other entities besides us */ |
Venkatesh Pallipadi | 2f36825 | 2011-04-14 10:30:53 -0700 | [diff] [blame] | 2560 | if (cfs_rq->load.weight) { |
| 2561 | /* |
| 2562 | * Bias pick_next to pick a task from this cfs_rq, as |
| 2563 | * p is sleeping when it is within its sched_slice. |
| 2564 | */ |
| 2565 | if (task_sleep && parent_entity(se)) |
| 2566 | set_next_buddy(parent_entity(se)); |
Paul Turner | 9598c82 | 2011-07-06 22:30:37 -0700 | [diff] [blame] | 2567 | |
| 2568 | /* avoid re-evaluating load for this entity */ |
| 2569 | se = parent_entity(se); |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 2570 | break; |
Venkatesh Pallipadi | 2f36825 | 2011-04-14 10:30:53 -0700 | [diff] [blame] | 2571 | } |
Peter Zijlstra | 371fd7e | 2010-03-24 16:38:48 +0100 | [diff] [blame] | 2572 | flags |= DEQUEUE_SLEEP; |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 2573 | } |
Peter Zijlstra | 8f4d37e | 2008-01-25 21:08:29 +0100 | [diff] [blame] | 2574 | |
Peter Zijlstra | 2069dd7 | 2010-11-15 15:47:00 -0800 | [diff] [blame] | 2575 | for_each_sched_entity(se) { |
Lin Ming | 0f31714 | 2011-07-22 09:14:31 +0800 | [diff] [blame] | 2576 | cfs_rq = cfs_rq_of(se); |
Paul Turner | 953bfcd | 2011-07-21 09:43:27 -0700 | [diff] [blame] | 2577 | cfs_rq->h_nr_running--; |
Peter Zijlstra | 2069dd7 | 2010-11-15 15:47:00 -0800 | [diff] [blame] | 2578 | |
Paul Turner | 85dac90 | 2011-07-21 09:43:33 -0700 | [diff] [blame] | 2579 | if (cfs_rq_throttled(cfs_rq)) |
| 2580 | break; |
| 2581 | |
Paul Turner | 6d5ab29 | 2011-01-21 20:45:01 -0800 | [diff] [blame] | 2582 | update_cfs_shares(cfs_rq); |
Paul Turner | 9ee474f | 2012-10-04 13:18:30 +0200 | [diff] [blame] | 2583 | update_entity_load_avg(se, 1); |
Peter Zijlstra | 2069dd7 | 2010-11-15 15:47:00 -0800 | [diff] [blame] | 2584 | } |
| 2585 | |
Ben Segall | 18bf280 | 2012-10-04 12:51:20 +0200 | [diff] [blame] | 2586 | if (!se) { |
Paul Turner | 85dac90 | 2011-07-21 09:43:33 -0700 | [diff] [blame] | 2587 | dec_nr_running(rq); |
Ben Segall | 18bf280 | 2012-10-04 12:51:20 +0200 | [diff] [blame] | 2588 | update_rq_runnable_avg(rq, 1); |
| 2589 | } |
Peter Zijlstra | a4c2f00 | 2008-10-17 19:27:03 +0200 | [diff] [blame] | 2590 | hrtick_update(rq); |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 2591 | } |
| 2592 | |
Gregory Haskins | e7693a3 | 2008-01-25 21:08:09 +0100 | [diff] [blame] | 2593 | #ifdef CONFIG_SMP |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 2594 | /* Used instead of source_load when we know the type == 0 */ |
| 2595 | static unsigned long weighted_cpuload(const int cpu) |
| 2596 | { |
| 2597 | return cpu_rq(cpu)->load.weight; |
| 2598 | } |
| 2599 | |
| 2600 | /* |
| 2601 | * Return a low guess at the load of a migration-source cpu weighted |
| 2602 | * according to the scheduling class and "nice" value. |
| 2603 | * |
| 2604 | * We want to under-estimate the load of migration sources, to |
| 2605 | * balance conservatively. |
| 2606 | */ |
| 2607 | static unsigned long source_load(int cpu, int type) |
| 2608 | { |
| 2609 | struct rq *rq = cpu_rq(cpu); |
| 2610 | unsigned long total = weighted_cpuload(cpu); |
| 2611 | |
| 2612 | if (type == 0 || !sched_feat(LB_BIAS)) |
| 2613 | return total; |
| 2614 | |
| 2615 | return min(rq->cpu_load[type-1], total); |
| 2616 | } |
| 2617 | |
| 2618 | /* |
| 2619 | * Return a high guess at the load of a migration-target cpu weighted |
| 2620 | * according to the scheduling class and "nice" value. |
| 2621 | */ |
| 2622 | static unsigned long target_load(int cpu, int type) |
| 2623 | { |
| 2624 | struct rq *rq = cpu_rq(cpu); |
| 2625 | unsigned long total = weighted_cpuload(cpu); |
| 2626 | |
| 2627 | if (type == 0 || !sched_feat(LB_BIAS)) |
| 2628 | return total; |
| 2629 | |
| 2630 | return max(rq->cpu_load[type-1], total); |
| 2631 | } |
| 2632 | |
| 2633 | static unsigned long power_of(int cpu) |
| 2634 | { |
| 2635 | return cpu_rq(cpu)->cpu_power; |
| 2636 | } |
| 2637 | |
| 2638 | static unsigned long cpu_avg_load_per_task(int cpu) |
| 2639 | { |
| 2640 | struct rq *rq = cpu_rq(cpu); |
| 2641 | unsigned long nr_running = ACCESS_ONCE(rq->nr_running); |
| 2642 | |
| 2643 | if (nr_running) |
| 2644 | return rq->load.weight / nr_running; |
| 2645 | |
| 2646 | return 0; |
| 2647 | } |
| 2648 | |
Ingo Molnar | 098fb9d | 2008-03-16 20:36:10 +0100 | [diff] [blame] | 2649 | |
Peter Zijlstra | 74f8e4b | 2011-04-05 17:23:47 +0200 | [diff] [blame] | 2650 | static void task_waking_fair(struct task_struct *p) |
Peter Zijlstra | 88ec22d | 2009-12-16 18:04:41 +0100 | [diff] [blame] | 2651 | { |
| 2652 | struct sched_entity *se = &p->se; |
| 2653 | struct cfs_rq *cfs_rq = cfs_rq_of(se); |
Peter Zijlstra | 3fe1698 | 2011-04-05 17:23:48 +0200 | [diff] [blame] | 2654 | u64 min_vruntime; |
Peter Zijlstra | 88ec22d | 2009-12-16 18:04:41 +0100 | [diff] [blame] | 2655 | |
Peter Zijlstra | 3fe1698 | 2011-04-05 17:23:48 +0200 | [diff] [blame] | 2656 | #ifndef CONFIG_64BIT |
| 2657 | u64 min_vruntime_copy; |
Peter Zijlstra | 74f8e4b | 2011-04-05 17:23:47 +0200 | [diff] [blame] | 2658 | |
Peter Zijlstra | 3fe1698 | 2011-04-05 17:23:48 +0200 | [diff] [blame] | 2659 | do { |
| 2660 | min_vruntime_copy = cfs_rq->min_vruntime_copy; |
| 2661 | smp_rmb(); |
| 2662 | min_vruntime = cfs_rq->min_vruntime; |
| 2663 | } while (min_vruntime != min_vruntime_copy); |
| 2664 | #else |
| 2665 | min_vruntime = cfs_rq->min_vruntime; |
| 2666 | #endif |
| 2667 | |
| 2668 | se->vruntime -= min_vruntime; |
Peter Zijlstra | 88ec22d | 2009-12-16 18:04:41 +0100 | [diff] [blame] | 2669 | } |
| 2670 | |
Peter Zijlstra | bb3469a | 2008-06-27 13:41:27 +0200 | [diff] [blame] | 2671 | #ifdef CONFIG_FAIR_GROUP_SCHED |
Peter Zijlstra | f5bfb7d | 2008-06-27 13:41:39 +0200 | [diff] [blame] | 2672 | /* |
| 2673 | * effective_load() calculates the load change as seen from the root_task_group |
| 2674 | * |
| 2675 | * Adding load to a group doesn't make a group heavier, but can cause movement |
| 2676 | * of group shares between cpus. Assuming the shares were perfectly aligned one |
| 2677 | * can calculate the shift in shares. |
Peter Zijlstra | cf5f0ac | 2011-10-13 16:52:28 +0200 | [diff] [blame] | 2678 | * |
| 2679 | * Calculate the effective load difference if @wl is added (subtracted) to @tg |
| 2680 | * on this @cpu and results in a total addition (subtraction) of @wg to the |
| 2681 | * total group weight. |
| 2682 | * |
| 2683 | * Given a runqueue weight distribution (rw_i) we can compute a shares |
| 2684 | * distribution (s_i) using: |
| 2685 | * |
| 2686 | * s_i = rw_i / \Sum rw_j (1) |
| 2687 | * |
| 2688 | * Suppose we have 4 CPUs and our @tg is a direct child of the root group and |
| 2689 | * has 7 equal weight tasks, distributed as below (rw_i), with the resulting |
| 2690 | * shares distribution (s_i): |
| 2691 | * |
| 2692 | * rw_i = { 2, 4, 1, 0 } |
| 2693 | * s_i = { 2/7, 4/7, 1/7, 0 } |
| 2694 | * |
| 2695 | * As per wake_affine() we're interested in the load of two CPUs (the CPU the |
| 2696 | * task used to run on and the CPU the waker is running on), we need to |
| 2697 | * compute the effect of waking a task on either CPU and, in case of a sync |
| 2698 | * wakeup, compute the effect of the current task going to sleep. |
| 2699 | * |
| 2700 | * So for a change of @wl to the local @cpu with an overall group weight change |
| 2701 | * of @wl we can compute the new shares distribution (s'_i) using: |
| 2702 | * |
| 2703 | * s'_i = (rw_i + @wl) / (@wg + \Sum rw_j) (2) |
| 2704 | * |
| 2705 | * Suppose we're interested in CPUs 0 and 1, and want to compute the load |
| 2706 | * differences in waking a task to CPU 0. The additional task changes the |
| 2707 | * weight and shares distributions like: |
| 2708 | * |
| 2709 | * rw'_i = { 3, 4, 1, 0 } |
| 2710 | * s'_i = { 3/8, 4/8, 1/8, 0 } |
| 2711 | * |
| 2712 | * We can then compute the difference in effective weight by using: |
| 2713 | * |
| 2714 | * dw_i = S * (s'_i - s_i) (3) |
| 2715 | * |
| 2716 | * Where 'S' is the group weight as seen by its parent. |
| 2717 | * |
| 2718 | * Therefore the effective change in loads on CPU 0 would be 5/56 (3/8 - 2/7) |
| 2719 | * times the weight of the group. The effect on CPU 1 would be -4/56 (4/8 - |
| 2720 | * 4/7) times the weight of the group. |
Peter Zijlstra | f5bfb7d | 2008-06-27 13:41:39 +0200 | [diff] [blame] | 2721 | */ |
Peter Zijlstra | 2069dd7 | 2010-11-15 15:47:00 -0800 | [diff] [blame] | 2722 | 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] | 2723 | { |
Peter Zijlstra | 4be9daa | 2008-06-27 13:41:30 +0200 | [diff] [blame] | 2724 | struct sched_entity *se = tg->se[cpu]; |
Peter Zijlstra | f1d239f | 2008-06-27 13:41:38 +0200 | [diff] [blame] | 2725 | |
Peter Zijlstra | cf5f0ac | 2011-10-13 16:52:28 +0200 | [diff] [blame] | 2726 | if (!tg->parent) /* the trivial, non-cgroup case */ |
Peter Zijlstra | f1d239f | 2008-06-27 13:41:38 +0200 | [diff] [blame] | 2727 | return wl; |
| 2728 | |
Peter Zijlstra | 4be9daa | 2008-06-27 13:41:30 +0200 | [diff] [blame] | 2729 | for_each_sched_entity(se) { |
Peter Zijlstra | cf5f0ac | 2011-10-13 16:52:28 +0200 | [diff] [blame] | 2730 | long w, W; |
Peter Zijlstra | bb3469a | 2008-06-27 13:41:27 +0200 | [diff] [blame] | 2731 | |
Paul Turner | 977dda7 | 2011-01-14 17:57:50 -0800 | [diff] [blame] | 2732 | tg = se->my_q->tg; |
Peter Zijlstra | 4be9daa | 2008-06-27 13:41:30 +0200 | [diff] [blame] | 2733 | |
Peter Zijlstra | cf5f0ac | 2011-10-13 16:52:28 +0200 | [diff] [blame] | 2734 | /* |
| 2735 | * W = @wg + \Sum rw_j |
| 2736 | */ |
| 2737 | W = wg + calc_tg_weight(tg, se->my_q); |
Peter Zijlstra | 4be9daa | 2008-06-27 13:41:30 +0200 | [diff] [blame] | 2738 | |
Peter Zijlstra | cf5f0ac | 2011-10-13 16:52:28 +0200 | [diff] [blame] | 2739 | /* |
| 2740 | * w = rw_i + @wl |
| 2741 | */ |
| 2742 | w = se->my_q->load.weight + wl; |
Peter Zijlstra | 940959e | 2008-09-23 15:33:42 +0200 | [diff] [blame] | 2743 | |
Peter Zijlstra | cf5f0ac | 2011-10-13 16:52:28 +0200 | [diff] [blame] | 2744 | /* |
| 2745 | * wl = S * s'_i; see (2) |
| 2746 | */ |
| 2747 | if (W > 0 && w < W) |
| 2748 | wl = (w * tg->shares) / W; |
Paul Turner | 977dda7 | 2011-01-14 17:57:50 -0800 | [diff] [blame] | 2749 | else |
| 2750 | wl = tg->shares; |
Peter Zijlstra | 940959e | 2008-09-23 15:33:42 +0200 | [diff] [blame] | 2751 | |
Peter Zijlstra | cf5f0ac | 2011-10-13 16:52:28 +0200 | [diff] [blame] | 2752 | /* |
| 2753 | * Per the above, wl is the new se->load.weight value; since |
| 2754 | * those are clipped to [MIN_SHARES, ...) do so now. See |
| 2755 | * calc_cfs_shares(). |
| 2756 | */ |
Paul Turner | 977dda7 | 2011-01-14 17:57:50 -0800 | [diff] [blame] | 2757 | if (wl < MIN_SHARES) |
| 2758 | wl = MIN_SHARES; |
Peter Zijlstra | cf5f0ac | 2011-10-13 16:52:28 +0200 | [diff] [blame] | 2759 | |
| 2760 | /* |
| 2761 | * wl = dw_i = S * (s'_i - s_i); see (3) |
| 2762 | */ |
Paul Turner | 977dda7 | 2011-01-14 17:57:50 -0800 | [diff] [blame] | 2763 | wl -= se->load.weight; |
Peter Zijlstra | cf5f0ac | 2011-10-13 16:52:28 +0200 | [diff] [blame] | 2764 | |
| 2765 | /* |
| 2766 | * Recursively apply this logic to all parent groups to compute |
| 2767 | * the final effective load change on the root group. Since |
| 2768 | * only the @tg group gets extra weight, all parent groups can |
| 2769 | * only redistribute existing shares. @wl is the shift in shares |
| 2770 | * resulting from this level per the above. |
| 2771 | */ |
Peter Zijlstra | 4be9daa | 2008-06-27 13:41:30 +0200 | [diff] [blame] | 2772 | wg = 0; |
Peter Zijlstra | 4be9daa | 2008-06-27 13:41:30 +0200 | [diff] [blame] | 2773 | } |
| 2774 | |
| 2775 | return wl; |
Peter Zijlstra | bb3469a | 2008-06-27 13:41:27 +0200 | [diff] [blame] | 2776 | } |
| 2777 | #else |
Peter Zijlstra | 4be9daa | 2008-06-27 13:41:30 +0200 | [diff] [blame] | 2778 | |
Peter Zijlstra | 8337826 | 2008-06-27 13:41:37 +0200 | [diff] [blame] | 2779 | static inline unsigned long effective_load(struct task_group *tg, int cpu, |
| 2780 | unsigned long wl, unsigned long wg) |
Peter Zijlstra | 4be9daa | 2008-06-27 13:41:30 +0200 | [diff] [blame] | 2781 | { |
Peter Zijlstra | 8337826 | 2008-06-27 13:41:37 +0200 | [diff] [blame] | 2782 | return wl; |
Peter Zijlstra | bb3469a | 2008-06-27 13:41:27 +0200 | [diff] [blame] | 2783 | } |
Peter Zijlstra | 4be9daa | 2008-06-27 13:41:30 +0200 | [diff] [blame] | 2784 | |
Peter Zijlstra | bb3469a | 2008-06-27 13:41:27 +0200 | [diff] [blame] | 2785 | #endif |
| 2786 | |
Peter Zijlstra | c88d591 | 2009-09-10 13:50:02 +0200 | [diff] [blame] | 2787 | 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] | 2788 | { |
Paul Turner | e37b6a7 | 2011-01-21 20:44:59 -0800 | [diff] [blame] | 2789 | s64 this_load, load; |
Peter Zijlstra | c88d591 | 2009-09-10 13:50:02 +0200 | [diff] [blame] | 2790 | int idx, this_cpu, prev_cpu; |
Ingo Molnar | 098fb9d | 2008-03-16 20:36:10 +0100 | [diff] [blame] | 2791 | unsigned long tl_per_task; |
Peter Zijlstra | c88d591 | 2009-09-10 13:50:02 +0200 | [diff] [blame] | 2792 | struct task_group *tg; |
Peter Zijlstra | 8337826 | 2008-06-27 13:41:37 +0200 | [diff] [blame] | 2793 | unsigned long weight; |
Mike Galbraith | b3137bc | 2008-05-29 11:11:41 +0200 | [diff] [blame] | 2794 | int balanced; |
Ingo Molnar | 098fb9d | 2008-03-16 20:36:10 +0100 | [diff] [blame] | 2795 | |
Peter Zijlstra | c88d591 | 2009-09-10 13:50:02 +0200 | [diff] [blame] | 2796 | idx = sd->wake_idx; |
| 2797 | this_cpu = smp_processor_id(); |
| 2798 | prev_cpu = task_cpu(p); |
| 2799 | load = source_load(prev_cpu, idx); |
| 2800 | this_load = target_load(this_cpu, idx); |
Ingo Molnar | 098fb9d | 2008-03-16 20:36:10 +0100 | [diff] [blame] | 2801 | |
| 2802 | /* |
Ingo Molnar | 098fb9d | 2008-03-16 20:36:10 +0100 | [diff] [blame] | 2803 | * If sync wakeup then subtract the (maximum possible) |
| 2804 | * effect of the currently running task from the load |
| 2805 | * of the current CPU: |
| 2806 | */ |
Peter Zijlstra | 8337826 | 2008-06-27 13:41:37 +0200 | [diff] [blame] | 2807 | if (sync) { |
| 2808 | tg = task_group(current); |
| 2809 | weight = current->se.load.weight; |
Ingo Molnar | 098fb9d | 2008-03-16 20:36:10 +0100 | [diff] [blame] | 2810 | |
Peter Zijlstra | c88d591 | 2009-09-10 13:50:02 +0200 | [diff] [blame] | 2811 | this_load += effective_load(tg, this_cpu, -weight, -weight); |
Peter Zijlstra | 8337826 | 2008-06-27 13:41:37 +0200 | [diff] [blame] | 2812 | load += effective_load(tg, prev_cpu, 0, -weight); |
| 2813 | } |
| 2814 | |
| 2815 | tg = task_group(p); |
| 2816 | weight = p->se.load.weight; |
| 2817 | |
Peter Zijlstra | 71a29aa | 2009-09-07 18:28:05 +0200 | [diff] [blame] | 2818 | /* |
| 2819 | * 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] | 2820 | * due to the sync cause above having dropped this_load to 0, we'll |
| 2821 | * always have an imbalance, but there's really nothing you can do |
| 2822 | * about that, so that's good too. |
Peter Zijlstra | 71a29aa | 2009-09-07 18:28:05 +0200 | [diff] [blame] | 2823 | * |
| 2824 | * Otherwise check if either cpus are near enough in load to allow this |
| 2825 | * task to be woken on this_cpu. |
| 2826 | */ |
Paul Turner | e37b6a7 | 2011-01-21 20:44:59 -0800 | [diff] [blame] | 2827 | if (this_load > 0) { |
| 2828 | s64 this_eff_load, prev_eff_load; |
Peter Zijlstra | e51fd5e | 2010-05-31 12:37:30 +0200 | [diff] [blame] | 2829 | |
| 2830 | this_eff_load = 100; |
| 2831 | this_eff_load *= power_of(prev_cpu); |
| 2832 | this_eff_load *= this_load + |
| 2833 | effective_load(tg, this_cpu, weight, weight); |
| 2834 | |
| 2835 | prev_eff_load = 100 + (sd->imbalance_pct - 100) / 2; |
| 2836 | prev_eff_load *= power_of(this_cpu); |
| 2837 | prev_eff_load *= load + effective_load(tg, prev_cpu, 0, weight); |
| 2838 | |
| 2839 | balanced = this_eff_load <= prev_eff_load; |
| 2840 | } else |
| 2841 | balanced = true; |
Mike Galbraith | b3137bc | 2008-05-29 11:11:41 +0200 | [diff] [blame] | 2842 | |
| 2843 | /* |
| 2844 | * If the currently running task will sleep within |
| 2845 | * a reasonable amount of time then attract this newly |
| 2846 | * woken task: |
| 2847 | */ |
Peter Zijlstra | 2fb7635 | 2008-10-08 09:16:04 +0200 | [diff] [blame] | 2848 | if (sync && balanced) |
| 2849 | return 1; |
Mike Galbraith | b3137bc | 2008-05-29 11:11:41 +0200 | [diff] [blame] | 2850 | |
Lucas De Marchi | 41acab8 | 2010-03-10 23:37:45 -0300 | [diff] [blame] | 2851 | schedstat_inc(p, se.statistics.nr_wakeups_affine_attempts); |
Mike Galbraith | b3137bc | 2008-05-29 11:11:41 +0200 | [diff] [blame] | 2852 | tl_per_task = cpu_avg_load_per_task(this_cpu); |
| 2853 | |
Peter Zijlstra | c88d591 | 2009-09-10 13:50:02 +0200 | [diff] [blame] | 2854 | if (balanced || |
| 2855 | (this_load <= load && |
| 2856 | this_load + target_load(prev_cpu, idx) <= tl_per_task)) { |
Ingo Molnar | 098fb9d | 2008-03-16 20:36:10 +0100 | [diff] [blame] | 2857 | /* |
| 2858 | * This domain has SD_WAKE_AFFINE and |
| 2859 | * p is cache cold in this domain, and |
| 2860 | * there is no bad imbalance. |
| 2861 | */ |
Peter Zijlstra | c88d591 | 2009-09-10 13:50:02 +0200 | [diff] [blame] | 2862 | schedstat_inc(sd, ttwu_move_affine); |
Lucas De Marchi | 41acab8 | 2010-03-10 23:37:45 -0300 | [diff] [blame] | 2863 | schedstat_inc(p, se.statistics.nr_wakeups_affine); |
Ingo Molnar | 098fb9d | 2008-03-16 20:36:10 +0100 | [diff] [blame] | 2864 | |
| 2865 | return 1; |
| 2866 | } |
| 2867 | return 0; |
| 2868 | } |
| 2869 | |
Peter Zijlstra | aaee120 | 2009-09-10 13:36:25 +0200 | [diff] [blame] | 2870 | /* |
| 2871 | * find_idlest_group finds and returns the least busy CPU group within the |
| 2872 | * domain. |
| 2873 | */ |
| 2874 | static struct sched_group * |
Peter Zijlstra | 78e7ed5 | 2009-09-03 13:16:51 +0200 | [diff] [blame] | 2875 | find_idlest_group(struct sched_domain *sd, struct task_struct *p, |
Peter Zijlstra | 5158f4e | 2009-09-16 13:46:59 +0200 | [diff] [blame] | 2876 | int this_cpu, int load_idx) |
Gregory Haskins | e7693a3 | 2008-01-25 21:08:09 +0100 | [diff] [blame] | 2877 | { |
Andi Kleen | b3bd3de | 2010-08-10 14:17:51 -0700 | [diff] [blame] | 2878 | struct sched_group *idlest = NULL, *group = sd->groups; |
Peter Zijlstra | aaee120 | 2009-09-10 13:36:25 +0200 | [diff] [blame] | 2879 | unsigned long min_load = ULONG_MAX, this_load = 0; |
Peter Zijlstra | aaee120 | 2009-09-10 13:36:25 +0200 | [diff] [blame] | 2880 | int imbalance = 100 + (sd->imbalance_pct-100)/2; |
Gregory Haskins | e7693a3 | 2008-01-25 21:08:09 +0100 | [diff] [blame] | 2881 | |
Peter Zijlstra | aaee120 | 2009-09-10 13:36:25 +0200 | [diff] [blame] | 2882 | do { |
| 2883 | unsigned long load, avg_load; |
| 2884 | int local_group; |
| 2885 | int i; |
Gregory Haskins | e7693a3 | 2008-01-25 21:08:09 +0100 | [diff] [blame] | 2886 | |
Peter Zijlstra | aaee120 | 2009-09-10 13:36:25 +0200 | [diff] [blame] | 2887 | /* Skip over this group if it has no CPUs allowed */ |
| 2888 | if (!cpumask_intersects(sched_group_cpus(group), |
Peter Zijlstra | fa17b50 | 2011-06-16 12:23:22 +0200 | [diff] [blame] | 2889 | tsk_cpus_allowed(p))) |
Peter Zijlstra | aaee120 | 2009-09-10 13:36:25 +0200 | [diff] [blame] | 2890 | continue; |
| 2891 | |
| 2892 | local_group = cpumask_test_cpu(this_cpu, |
| 2893 | sched_group_cpus(group)); |
| 2894 | |
| 2895 | /* Tally up the load of all CPUs in the group */ |
| 2896 | avg_load = 0; |
| 2897 | |
| 2898 | for_each_cpu(i, sched_group_cpus(group)) { |
| 2899 | /* Bias balancing toward cpus of our domain */ |
| 2900 | if (local_group) |
| 2901 | load = source_load(i, load_idx); |
| 2902 | else |
| 2903 | load = target_load(i, load_idx); |
| 2904 | |
| 2905 | avg_load += load; |
| 2906 | } |
| 2907 | |
| 2908 | /* Adjust by relative CPU power of the group */ |
Peter Zijlstra | 9c3f75c | 2011-07-14 13:00:06 +0200 | [diff] [blame] | 2909 | avg_load = (avg_load * SCHED_POWER_SCALE) / group->sgp->power; |
Peter Zijlstra | aaee120 | 2009-09-10 13:36:25 +0200 | [diff] [blame] | 2910 | |
| 2911 | if (local_group) { |
| 2912 | this_load = avg_load; |
Peter Zijlstra | aaee120 | 2009-09-10 13:36:25 +0200 | [diff] [blame] | 2913 | } else if (avg_load < min_load) { |
| 2914 | min_load = avg_load; |
| 2915 | idlest = group; |
| 2916 | } |
| 2917 | } while (group = group->next, group != sd->groups); |
| 2918 | |
| 2919 | if (!idlest || 100*this_load < imbalance*min_load) |
| 2920 | return NULL; |
| 2921 | return idlest; |
| 2922 | } |
| 2923 | |
| 2924 | /* |
| 2925 | * find_idlest_cpu - find the idlest cpu among the cpus in group. |
| 2926 | */ |
| 2927 | static int |
| 2928 | find_idlest_cpu(struct sched_group *group, struct task_struct *p, int this_cpu) |
| 2929 | { |
| 2930 | unsigned long load, min_load = ULONG_MAX; |
| 2931 | int idlest = -1; |
| 2932 | int i; |
| 2933 | |
| 2934 | /* Traverse only the allowed CPUs */ |
Peter Zijlstra | fa17b50 | 2011-06-16 12:23:22 +0200 | [diff] [blame] | 2935 | 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] | 2936 | load = weighted_cpuload(i); |
| 2937 | |
| 2938 | if (load < min_load || (load == min_load && i == this_cpu)) { |
| 2939 | min_load = load; |
| 2940 | idlest = i; |
Gregory Haskins | e7693a3 | 2008-01-25 21:08:09 +0100 | [diff] [blame] | 2941 | } |
| 2942 | } |
| 2943 | |
Peter Zijlstra | aaee120 | 2009-09-10 13:36:25 +0200 | [diff] [blame] | 2944 | return idlest; |
| 2945 | } |
Gregory Haskins | e7693a3 | 2008-01-25 21:08:09 +0100 | [diff] [blame] | 2946 | |
Peter Zijlstra | aaee120 | 2009-09-10 13:36:25 +0200 | [diff] [blame] | 2947 | /* |
Peter Zijlstra | a50bde5 | 2009-11-12 15:55:28 +0100 | [diff] [blame] | 2948 | * Try and locate an idle CPU in the sched_domain. |
| 2949 | */ |
Suresh Siddha | 99bd5e2 | 2010-03-31 16:47:45 -0700 | [diff] [blame] | 2950 | static int select_idle_sibling(struct task_struct *p, int target) |
Peter Zijlstra | a50bde5 | 2009-11-12 15:55:28 +0100 | [diff] [blame] | 2951 | { |
| 2952 | int cpu = smp_processor_id(); |
| 2953 | int prev_cpu = task_cpu(p); |
Suresh Siddha | 99bd5e2 | 2010-03-31 16:47:45 -0700 | [diff] [blame] | 2954 | struct sched_domain *sd; |
Linus Torvalds | 37407ea | 2012-09-16 12:29:43 -0700 | [diff] [blame] | 2955 | struct sched_group *sg; |
| 2956 | int i; |
Peter Zijlstra | a50bde5 | 2009-11-12 15:55:28 +0100 | [diff] [blame] | 2957 | |
| 2958 | /* |
Suresh Siddha | 99bd5e2 | 2010-03-31 16:47:45 -0700 | [diff] [blame] | 2959 | * If the task is going to be woken-up on this cpu and if it is |
| 2960 | * already idle, then it is the right target. |
Peter Zijlstra | a50bde5 | 2009-11-12 15:55:28 +0100 | [diff] [blame] | 2961 | */ |
Suresh Siddha | 99bd5e2 | 2010-03-31 16:47:45 -0700 | [diff] [blame] | 2962 | if (target == cpu && idle_cpu(cpu)) |
| 2963 | return cpu; |
| 2964 | |
| 2965 | /* |
| 2966 | * If the task is going to be woken-up on the cpu where it previously |
| 2967 | * ran and if it is currently idle, then it the right target. |
| 2968 | */ |
| 2969 | if (target == prev_cpu && idle_cpu(prev_cpu)) |
Peter Zijlstra | fe3bcfe | 2009-11-12 15:55:29 +0100 | [diff] [blame] | 2970 | return prev_cpu; |
Peter Zijlstra | a50bde5 | 2009-11-12 15:55:28 +0100 | [diff] [blame] | 2971 | |
| 2972 | /* |
Linus Torvalds | 37407ea | 2012-09-16 12:29:43 -0700 | [diff] [blame] | 2973 | * Otherwise, iterate the domains and find an elegible idle cpu. |
Peter Zijlstra | a50bde5 | 2009-11-12 15:55:28 +0100 | [diff] [blame] | 2974 | */ |
Peter Zijlstra | 518cd62 | 2011-12-07 15:07:31 +0100 | [diff] [blame] | 2975 | sd = rcu_dereference(per_cpu(sd_llc, target)); |
Suresh Siddha | 77e8136 | 2011-11-17 11:08:23 -0800 | [diff] [blame] | 2976 | for_each_lower_domain(sd) { |
Linus Torvalds | 37407ea | 2012-09-16 12:29:43 -0700 | [diff] [blame] | 2977 | sg = sd->groups; |
| 2978 | do { |
| 2979 | if (!cpumask_intersects(sched_group_cpus(sg), |
| 2980 | tsk_cpus_allowed(p))) |
| 2981 | goto next; |
Mike Galbraith | 970e178 | 2012-06-12 05:18:32 +0200 | [diff] [blame] | 2982 | |
Linus Torvalds | 37407ea | 2012-09-16 12:29:43 -0700 | [diff] [blame] | 2983 | for_each_cpu(i, sched_group_cpus(sg)) { |
| 2984 | if (!idle_cpu(i)) |
| 2985 | goto next; |
| 2986 | } |
| 2987 | |
| 2988 | target = cpumask_first_and(sched_group_cpus(sg), |
| 2989 | tsk_cpus_allowed(p)); |
| 2990 | goto done; |
| 2991 | next: |
| 2992 | sg = sg->next; |
| 2993 | } while (sg != sd->groups); |
| 2994 | } |
| 2995 | done: |
Peter Zijlstra | a50bde5 | 2009-11-12 15:55:28 +0100 | [diff] [blame] | 2996 | return target; |
| 2997 | } |
| 2998 | |
| 2999 | /* |
Peter Zijlstra | aaee120 | 2009-09-10 13:36:25 +0200 | [diff] [blame] | 3000 | * sched_balance_self: balance the current task (running on cpu) in domains |
| 3001 | * that have the 'flag' flag set. In practice, this is SD_BALANCE_FORK and |
| 3002 | * SD_BALANCE_EXEC. |
| 3003 | * |
| 3004 | * Balance, ie. select the least loaded group. |
| 3005 | * |
| 3006 | * Returns the target CPU number, or the same CPU if no balancing is needed. |
| 3007 | * |
| 3008 | * preempt must be disabled. |
| 3009 | */ |
Peter Zijlstra | 0017d73 | 2010-03-24 18:34:10 +0100 | [diff] [blame] | 3010 | static int |
Peter Zijlstra | 7608dec | 2011-04-05 17:23:46 +0200 | [diff] [blame] | 3011 | 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] | 3012 | { |
Peter Zijlstra | 29cd8ba | 2009-09-17 09:01:14 +0200 | [diff] [blame] | 3013 | struct sched_domain *tmp, *affine_sd = NULL, *sd = NULL; |
Peter Zijlstra | c88d591 | 2009-09-10 13:50:02 +0200 | [diff] [blame] | 3014 | int cpu = smp_processor_id(); |
| 3015 | int prev_cpu = task_cpu(p); |
| 3016 | int new_cpu = cpu; |
Suresh Siddha | 99bd5e2 | 2010-03-31 16:47:45 -0700 | [diff] [blame] | 3017 | int want_affine = 0; |
Peter Zijlstra | 5158f4e | 2009-09-16 13:46:59 +0200 | [diff] [blame] | 3018 | int sync = wake_flags & WF_SYNC; |
Gregory Haskins | e7693a3 | 2008-01-25 21:08:09 +0100 | [diff] [blame] | 3019 | |
Peter Zijlstra | 29baa74 | 2012-04-23 12:11:21 +0200 | [diff] [blame] | 3020 | if (p->nr_cpus_allowed == 1) |
Mike Galbraith | 76854c7 | 2011-11-22 15:18:24 +0100 | [diff] [blame] | 3021 | return prev_cpu; |
| 3022 | |
Peter Zijlstra | 0763a66 | 2009-09-14 19:37:39 +0200 | [diff] [blame] | 3023 | if (sd_flag & SD_BALANCE_WAKE) { |
Peter Zijlstra | fa17b50 | 2011-06-16 12:23:22 +0200 | [diff] [blame] | 3024 | if (cpumask_test_cpu(cpu, tsk_cpus_allowed(p))) |
Peter Zijlstra | c88d591 | 2009-09-10 13:50:02 +0200 | [diff] [blame] | 3025 | want_affine = 1; |
| 3026 | new_cpu = prev_cpu; |
| 3027 | } |
Gregory Haskins | e7693a3 | 2008-01-25 21:08:09 +0100 | [diff] [blame] | 3028 | |
Peter Zijlstra | dce840a | 2011-04-07 14:09:50 +0200 | [diff] [blame] | 3029 | rcu_read_lock(); |
Peter Zijlstra | aaee120 | 2009-09-10 13:36:25 +0200 | [diff] [blame] | 3030 | for_each_domain(cpu, tmp) { |
Peter Zijlstra | e4f42888 | 2009-12-16 18:04:34 +0100 | [diff] [blame] | 3031 | if (!(tmp->flags & SD_LOAD_BALANCE)) |
| 3032 | continue; |
| 3033 | |
Peter Zijlstra | aaee120 | 2009-09-10 13:36:25 +0200 | [diff] [blame] | 3034 | /* |
Suresh Siddha | 99bd5e2 | 2010-03-31 16:47:45 -0700 | [diff] [blame] | 3035 | * If both cpu and prev_cpu are part of this domain, |
| 3036 | * cpu is a valid SD_WAKE_AFFINE target. |
Peter Zijlstra | fe3bcfe | 2009-11-12 15:55:29 +0100 | [diff] [blame] | 3037 | */ |
Suresh Siddha | 99bd5e2 | 2010-03-31 16:47:45 -0700 | [diff] [blame] | 3038 | if (want_affine && (tmp->flags & SD_WAKE_AFFINE) && |
| 3039 | cpumask_test_cpu(prev_cpu, sched_domain_span(tmp))) { |
| 3040 | affine_sd = tmp; |
Alex Shi | f03542a | 2012-07-26 08:55:34 +0800 | [diff] [blame] | 3041 | break; |
Peter Zijlstra | c88d591 | 2009-09-10 13:50:02 +0200 | [diff] [blame] | 3042 | } |
| 3043 | |
Alex Shi | f03542a | 2012-07-26 08:55:34 +0800 | [diff] [blame] | 3044 | if (tmp->flags & sd_flag) |
Peter Zijlstra | 29cd8ba | 2009-09-17 09:01:14 +0200 | [diff] [blame] | 3045 | sd = tmp; |
Peter Zijlstra | c88d591 | 2009-09-10 13:50:02 +0200 | [diff] [blame] | 3046 | } |
Peter Zijlstra | aaee120 | 2009-09-10 13:36:25 +0200 | [diff] [blame] | 3047 | |
Mike Galbraith | 8b911ac | 2010-03-11 17:17:16 +0100 | [diff] [blame] | 3048 | if (affine_sd) { |
Alex Shi | f03542a | 2012-07-26 08:55:34 +0800 | [diff] [blame] | 3049 | if (cpu != prev_cpu && wake_affine(affine_sd, p, sync)) |
Peter Zijlstra | dce840a | 2011-04-07 14:09:50 +0200 | [diff] [blame] | 3050 | prev_cpu = cpu; |
| 3051 | |
| 3052 | new_cpu = select_idle_sibling(p, prev_cpu); |
| 3053 | goto unlock; |
Mike Galbraith | 8b911ac | 2010-03-11 17:17:16 +0100 | [diff] [blame] | 3054 | } |
Peter Zijlstra | 3b64089 | 2009-09-16 13:44:33 +0200 | [diff] [blame] | 3055 | |
Peter Zijlstra | aaee120 | 2009-09-10 13:36:25 +0200 | [diff] [blame] | 3056 | while (sd) { |
Peter Zijlstra | 5158f4e | 2009-09-16 13:46:59 +0200 | [diff] [blame] | 3057 | int load_idx = sd->forkexec_idx; |
Peter Zijlstra | aaee120 | 2009-09-10 13:36:25 +0200 | [diff] [blame] | 3058 | struct sched_group *group; |
Peter Zijlstra | c88d591 | 2009-09-10 13:50:02 +0200 | [diff] [blame] | 3059 | int weight; |
Peter Zijlstra | aaee120 | 2009-09-10 13:36:25 +0200 | [diff] [blame] | 3060 | |
Peter Zijlstra | 0763a66 | 2009-09-14 19:37:39 +0200 | [diff] [blame] | 3061 | if (!(sd->flags & sd_flag)) { |
Peter Zijlstra | aaee120 | 2009-09-10 13:36:25 +0200 | [diff] [blame] | 3062 | sd = sd->child; |
| 3063 | continue; |
| 3064 | } |
| 3065 | |
Peter Zijlstra | 5158f4e | 2009-09-16 13:46:59 +0200 | [diff] [blame] | 3066 | if (sd_flag & SD_BALANCE_WAKE) |
| 3067 | load_idx = sd->wake_idx; |
| 3068 | |
| 3069 | group = find_idlest_group(sd, p, cpu, load_idx); |
Peter Zijlstra | aaee120 | 2009-09-10 13:36:25 +0200 | [diff] [blame] | 3070 | if (!group) { |
| 3071 | sd = sd->child; |
| 3072 | continue; |
| 3073 | } |
| 3074 | |
Peter Zijlstra | d7c33c4 | 2009-09-11 12:45:38 +0200 | [diff] [blame] | 3075 | new_cpu = find_idlest_cpu(group, p, cpu); |
Peter Zijlstra | aaee120 | 2009-09-10 13:36:25 +0200 | [diff] [blame] | 3076 | if (new_cpu == -1 || new_cpu == cpu) { |
| 3077 | /* Now try balancing at a lower domain level of cpu */ |
| 3078 | sd = sd->child; |
| 3079 | continue; |
| 3080 | } |
| 3081 | |
| 3082 | /* Now try balancing at a lower domain level of new_cpu */ |
| 3083 | cpu = new_cpu; |
Peter Zijlstra | 669c55e | 2010-04-16 14:59:29 +0200 | [diff] [blame] | 3084 | weight = sd->span_weight; |
Peter Zijlstra | aaee120 | 2009-09-10 13:36:25 +0200 | [diff] [blame] | 3085 | sd = NULL; |
| 3086 | for_each_domain(cpu, tmp) { |
Peter Zijlstra | 669c55e | 2010-04-16 14:59:29 +0200 | [diff] [blame] | 3087 | if (weight <= tmp->span_weight) |
Peter Zijlstra | aaee120 | 2009-09-10 13:36:25 +0200 | [diff] [blame] | 3088 | break; |
Peter Zijlstra | 0763a66 | 2009-09-14 19:37:39 +0200 | [diff] [blame] | 3089 | if (tmp->flags & sd_flag) |
Peter Zijlstra | aaee120 | 2009-09-10 13:36:25 +0200 | [diff] [blame] | 3090 | sd = tmp; |
| 3091 | } |
| 3092 | /* while loop will break here if sd == NULL */ |
Gregory Haskins | e7693a3 | 2008-01-25 21:08:09 +0100 | [diff] [blame] | 3093 | } |
Peter Zijlstra | dce840a | 2011-04-07 14:09:50 +0200 | [diff] [blame] | 3094 | unlock: |
| 3095 | rcu_read_unlock(); |
Gregory Haskins | e7693a3 | 2008-01-25 21:08:09 +0100 | [diff] [blame] | 3096 | |
Peter Zijlstra | c88d591 | 2009-09-10 13:50:02 +0200 | [diff] [blame] | 3097 | return new_cpu; |
Gregory Haskins | e7693a3 | 2008-01-25 21:08:09 +0100 | [diff] [blame] | 3098 | } |
Paul Turner | 0a74bef | 2012-10-04 13:18:30 +0200 | [diff] [blame] | 3099 | |
| 3100 | /* |
| 3101 | * Called immediately before a task is migrated to a new cpu; task_cpu(p) and |
| 3102 | * cfs_rq_of(p) references at time of call are still valid and identify the |
| 3103 | * previous cpu. However, the caller only guarantees p->pi_lock is held; no |
| 3104 | * other assumptions, including the state of rq->lock, should be made. |
| 3105 | */ |
| 3106 | static void |
| 3107 | migrate_task_rq_fair(struct task_struct *p, int next_cpu) |
| 3108 | { |
Paul Turner | aff3e49 | 2012-10-04 13:18:30 +0200 | [diff] [blame] | 3109 | struct sched_entity *se = &p->se; |
| 3110 | struct cfs_rq *cfs_rq = cfs_rq_of(se); |
| 3111 | |
| 3112 | /* |
| 3113 | * Load tracking: accumulate removed load so that it can be processed |
| 3114 | * when we next update owning cfs_rq under rq->lock. Tasks contribute |
| 3115 | * to blocked load iff they have a positive decay-count. It can never |
| 3116 | * be negative here since on-rq tasks have decay-count == 0. |
| 3117 | */ |
| 3118 | if (se->avg.decay_count) { |
| 3119 | se->avg.decay_count = -__synchronize_entity_decay(se); |
| 3120 | atomic64_add(se->avg.load_avg_contrib, &cfs_rq->removed_load); |
| 3121 | } |
Paul Turner | 0a74bef | 2012-10-04 13:18:30 +0200 | [diff] [blame] | 3122 | } |
Gregory Haskins | e7693a3 | 2008-01-25 21:08:09 +0100 | [diff] [blame] | 3123 | #endif /* CONFIG_SMP */ |
| 3124 | |
Peter Zijlstra | e52fb7c | 2009-01-14 12:39:19 +0100 | [diff] [blame] | 3125 | static unsigned long |
| 3126 | wakeup_gran(struct sched_entity *curr, struct sched_entity *se) |
Peter Zijlstra | 0bbd333 | 2008-04-19 19:44:57 +0200 | [diff] [blame] | 3127 | { |
| 3128 | unsigned long gran = sysctl_sched_wakeup_granularity; |
| 3129 | |
| 3130 | /* |
Peter Zijlstra | e52fb7c | 2009-01-14 12:39:19 +0100 | [diff] [blame] | 3131 | * Since its curr running now, convert the gran from real-time |
| 3132 | * to virtual-time in his units. |
Mike Galbraith | 13814d4 | 2010-03-11 17:17:04 +0100 | [diff] [blame] | 3133 | * |
| 3134 | * By using 'se' instead of 'curr' we penalize light tasks, so |
| 3135 | * they get preempted easier. That is, if 'se' < 'curr' then |
| 3136 | * the resulting gran will be larger, therefore penalizing the |
| 3137 | * lighter, if otoh 'se' > 'curr' then the resulting gran will |
| 3138 | * be smaller, again penalizing the lighter task. |
| 3139 | * |
| 3140 | * This is especially important for buddies when the leftmost |
| 3141 | * task is higher priority than the buddy. |
Peter Zijlstra | 0bbd333 | 2008-04-19 19:44:57 +0200 | [diff] [blame] | 3142 | */ |
Shaohua Li | f4ad9bd | 2011-04-08 12:53:09 +0800 | [diff] [blame] | 3143 | return calc_delta_fair(gran, se); |
Peter Zijlstra | 0bbd333 | 2008-04-19 19:44:57 +0200 | [diff] [blame] | 3144 | } |
| 3145 | |
| 3146 | /* |
Peter Zijlstra | 464b752 | 2008-10-24 11:06:15 +0200 | [diff] [blame] | 3147 | * Should 'se' preempt 'curr'. |
| 3148 | * |
| 3149 | * |s1 |
| 3150 | * |s2 |
| 3151 | * |s3 |
| 3152 | * g |
| 3153 | * |<--->|c |
| 3154 | * |
| 3155 | * w(c, s1) = -1 |
| 3156 | * w(c, s2) = 0 |
| 3157 | * w(c, s3) = 1 |
| 3158 | * |
| 3159 | */ |
| 3160 | static int |
| 3161 | wakeup_preempt_entity(struct sched_entity *curr, struct sched_entity *se) |
| 3162 | { |
| 3163 | s64 gran, vdiff = curr->vruntime - se->vruntime; |
| 3164 | |
| 3165 | if (vdiff <= 0) |
| 3166 | return -1; |
| 3167 | |
Peter Zijlstra | e52fb7c | 2009-01-14 12:39:19 +0100 | [diff] [blame] | 3168 | gran = wakeup_gran(curr, se); |
Peter Zijlstra | 464b752 | 2008-10-24 11:06:15 +0200 | [diff] [blame] | 3169 | if (vdiff > gran) |
| 3170 | return 1; |
| 3171 | |
| 3172 | return 0; |
| 3173 | } |
| 3174 | |
Peter Zijlstra | 0247909 | 2008-11-04 21:25:10 +0100 | [diff] [blame] | 3175 | static void set_last_buddy(struct sched_entity *se) |
| 3176 | { |
Venkatesh Pallipadi | 69c80f3 | 2011-04-13 18:21:09 -0700 | [diff] [blame] | 3177 | if (entity_is_task(se) && unlikely(task_of(se)->policy == SCHED_IDLE)) |
| 3178 | return; |
| 3179 | |
| 3180 | for_each_sched_entity(se) |
| 3181 | cfs_rq_of(se)->last = se; |
Peter Zijlstra | 0247909 | 2008-11-04 21:25:10 +0100 | [diff] [blame] | 3182 | } |
| 3183 | |
| 3184 | static void set_next_buddy(struct sched_entity *se) |
| 3185 | { |
Venkatesh Pallipadi | 69c80f3 | 2011-04-13 18:21:09 -0700 | [diff] [blame] | 3186 | if (entity_is_task(se) && unlikely(task_of(se)->policy == SCHED_IDLE)) |
| 3187 | return; |
| 3188 | |
| 3189 | for_each_sched_entity(se) |
| 3190 | cfs_rq_of(se)->next = se; |
Peter Zijlstra | 0247909 | 2008-11-04 21:25:10 +0100 | [diff] [blame] | 3191 | } |
| 3192 | |
Rik van Riel | ac53db5 | 2011-02-01 09:51:03 -0500 | [diff] [blame] | 3193 | static void set_skip_buddy(struct sched_entity *se) |
| 3194 | { |
Venkatesh Pallipadi | 69c80f3 | 2011-04-13 18:21:09 -0700 | [diff] [blame] | 3195 | for_each_sched_entity(se) |
| 3196 | cfs_rq_of(se)->skip = se; |
Rik van Riel | ac53db5 | 2011-02-01 09:51:03 -0500 | [diff] [blame] | 3197 | } |
| 3198 | |
Peter Zijlstra | 464b752 | 2008-10-24 11:06:15 +0200 | [diff] [blame] | 3199 | /* |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 3200 | * Preempt the current task with a newly woken task if needed: |
| 3201 | */ |
Peter Zijlstra | 5a9b86f | 2009-09-16 13:47:58 +0200 | [diff] [blame] | 3202 | 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] | 3203 | { |
| 3204 | struct task_struct *curr = rq->curr; |
Srivatsa Vaddagiri | 8651a86 | 2007-10-15 17:00:12 +0200 | [diff] [blame] | 3205 | struct sched_entity *se = &curr->se, *pse = &p->se; |
Mike Galbraith | 03e89e4 | 2008-12-16 08:45:30 +0100 | [diff] [blame] | 3206 | struct cfs_rq *cfs_rq = task_cfs_rq(curr); |
Mike Galbraith | f685cea | 2009-10-23 23:09:22 +0200 | [diff] [blame] | 3207 | int scale = cfs_rq->nr_running >= sched_nr_latency; |
Venkatesh Pallipadi | 2f36825 | 2011-04-14 10:30:53 -0700 | [diff] [blame] | 3208 | int next_buddy_marked = 0; |
Mike Galbraith | 03e89e4 | 2008-12-16 08:45:30 +0100 | [diff] [blame] | 3209 | |
Ingo Molnar | 4ae7d5c | 2008-03-19 01:42:00 +0100 | [diff] [blame] | 3210 | if (unlikely(se == pse)) |
| 3211 | return; |
| 3212 | |
Paul Turner | 5238cdd | 2011-07-21 09:43:37 -0700 | [diff] [blame] | 3213 | /* |
Peter Zijlstra | ddcdf6e | 2012-02-22 19:27:40 +0100 | [diff] [blame] | 3214 | * This is possible from callers such as move_task(), in which we |
Paul Turner | 5238cdd | 2011-07-21 09:43:37 -0700 | [diff] [blame] | 3215 | * unconditionally check_prempt_curr() after an enqueue (which may have |
| 3216 | * lead to a throttle). This both saves work and prevents false |
| 3217 | * next-buddy nomination below. |
| 3218 | */ |
| 3219 | if (unlikely(throttled_hierarchy(cfs_rq_of(pse)))) |
| 3220 | return; |
| 3221 | |
Venkatesh Pallipadi | 2f36825 | 2011-04-14 10:30:53 -0700 | [diff] [blame] | 3222 | if (sched_feat(NEXT_BUDDY) && scale && !(wake_flags & WF_FORK)) { |
Mike Galbraith | 3cb63d5 | 2009-09-11 12:01:17 +0200 | [diff] [blame] | 3223 | set_next_buddy(pse); |
Venkatesh Pallipadi | 2f36825 | 2011-04-14 10:30:53 -0700 | [diff] [blame] | 3224 | next_buddy_marked = 1; |
| 3225 | } |
Peter Zijlstra | 57fdc26 | 2008-09-23 15:33:45 +0200 | [diff] [blame] | 3226 | |
Bharata B Rao | aec0a51 | 2008-08-28 14:42:49 +0530 | [diff] [blame] | 3227 | /* |
| 3228 | * We can come here with TIF_NEED_RESCHED already set from new task |
| 3229 | * wake up path. |
Paul Turner | 5238cdd | 2011-07-21 09:43:37 -0700 | [diff] [blame] | 3230 | * |
| 3231 | * Note: this also catches the edge-case of curr being in a throttled |
| 3232 | * group (e.g. via set_curr_task), since update_curr() (in the |
| 3233 | * enqueue of curr) will have resulted in resched being set. This |
| 3234 | * prevents us from potentially nominating it as a false LAST_BUDDY |
| 3235 | * below. |
Bharata B Rao | aec0a51 | 2008-08-28 14:42:49 +0530 | [diff] [blame] | 3236 | */ |
| 3237 | if (test_tsk_need_resched(curr)) |
| 3238 | return; |
| 3239 | |
Darren Hart | a2f5c9a | 2011-02-22 13:04:33 -0800 | [diff] [blame] | 3240 | /* Idle tasks are by definition preempted by non-idle tasks. */ |
| 3241 | if (unlikely(curr->policy == SCHED_IDLE) && |
| 3242 | likely(p->policy != SCHED_IDLE)) |
| 3243 | goto preempt; |
| 3244 | |
Ingo Molnar | 91c234b | 2007-10-15 17:00:18 +0200 | [diff] [blame] | 3245 | /* |
Darren Hart | a2f5c9a | 2011-02-22 13:04:33 -0800 | [diff] [blame] | 3246 | * Batch and idle tasks do not preempt non-idle tasks (their preemption |
| 3247 | * is driven by the tick): |
Ingo Molnar | 91c234b | 2007-10-15 17:00:18 +0200 | [diff] [blame] | 3248 | */ |
Peter Zijlstra | 6bc912b | 2009-01-15 14:53:38 +0100 | [diff] [blame] | 3249 | if (unlikely(p->policy != SCHED_NORMAL)) |
Ingo Molnar | 91c234b | 2007-10-15 17:00:18 +0200 | [diff] [blame] | 3250 | return; |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 3251 | |
Peter Zijlstra | 3a7e73a | 2009-11-28 18:51:02 +0100 | [diff] [blame] | 3252 | find_matching_se(&se, &pse); |
Paul Turner | 9bbd737 | 2011-07-05 19:07:21 -0700 | [diff] [blame] | 3253 | update_curr(cfs_rq_of(se)); |
Peter Zijlstra | 3a7e73a | 2009-11-28 18:51:02 +0100 | [diff] [blame] | 3254 | BUG_ON(!pse); |
Venkatesh Pallipadi | 2f36825 | 2011-04-14 10:30:53 -0700 | [diff] [blame] | 3255 | if (wakeup_preempt_entity(se, pse) == 1) { |
| 3256 | /* |
| 3257 | * Bias pick_next to pick the sched entity that is |
| 3258 | * triggering this preemption. |
| 3259 | */ |
| 3260 | if (!next_buddy_marked) |
| 3261 | set_next_buddy(pse); |
Peter Zijlstra | 3a7e73a | 2009-11-28 18:51:02 +0100 | [diff] [blame] | 3262 | goto preempt; |
Venkatesh Pallipadi | 2f36825 | 2011-04-14 10:30:53 -0700 | [diff] [blame] | 3263 | } |
Jupyung Lee | a65ac74 | 2009-11-17 18:51:40 +0900 | [diff] [blame] | 3264 | |
Peter Zijlstra | 3a7e73a | 2009-11-28 18:51:02 +0100 | [diff] [blame] | 3265 | return; |
| 3266 | |
| 3267 | preempt: |
| 3268 | resched_task(curr); |
| 3269 | /* |
| 3270 | * Only set the backward buddy when the current task is still |
| 3271 | * on the rq. This can happen when a wakeup gets interleaved |
| 3272 | * with schedule on the ->pre_schedule() or idle_balance() |
| 3273 | * point, either of which can * drop the rq lock. |
| 3274 | * |
| 3275 | * Also, during early boot the idle thread is in the fair class, |
| 3276 | * for obvious reasons its a bad idea to schedule back to it. |
| 3277 | */ |
| 3278 | if (unlikely(!se->on_rq || curr == rq->idle)) |
| 3279 | return; |
| 3280 | |
| 3281 | if (sched_feat(LAST_BUDDY) && scale && entity_is_task(se)) |
| 3282 | set_last_buddy(se); |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 3283 | } |
| 3284 | |
Ingo Molnar | fb8d472 | 2007-08-09 11:16:48 +0200 | [diff] [blame] | 3285 | static struct task_struct *pick_next_task_fair(struct rq *rq) |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 3286 | { |
Peter Zijlstra | 8f4d37e | 2008-01-25 21:08:29 +0100 | [diff] [blame] | 3287 | struct task_struct *p; |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 3288 | struct cfs_rq *cfs_rq = &rq->cfs; |
| 3289 | struct sched_entity *se; |
| 3290 | |
Tim Blechmann | 36ace27 | 2009-11-24 11:55:45 +0100 | [diff] [blame] | 3291 | if (!cfs_rq->nr_running) |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 3292 | return NULL; |
| 3293 | |
| 3294 | do { |
Ingo Molnar | 9948f4b | 2007-08-09 11:16:48 +0200 | [diff] [blame] | 3295 | se = pick_next_entity(cfs_rq); |
Peter Zijlstra | f4b6755 | 2008-11-04 21:25:07 +0100 | [diff] [blame] | 3296 | set_next_entity(cfs_rq, se); |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 3297 | cfs_rq = group_cfs_rq(se); |
| 3298 | } while (cfs_rq); |
| 3299 | |
Peter Zijlstra | 8f4d37e | 2008-01-25 21:08:29 +0100 | [diff] [blame] | 3300 | p = task_of(se); |
Mike Galbraith | b39e66e | 2011-11-22 15:20:07 +0100 | [diff] [blame] | 3301 | if (hrtick_enabled(rq)) |
| 3302 | hrtick_start_fair(rq, p); |
Peter Zijlstra | 8f4d37e | 2008-01-25 21:08:29 +0100 | [diff] [blame] | 3303 | |
| 3304 | return p; |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 3305 | } |
| 3306 | |
| 3307 | /* |
| 3308 | * Account for a descheduled task: |
| 3309 | */ |
Ingo Molnar | 31ee529 | 2007-08-09 11:16:49 +0200 | [diff] [blame] | 3310 | 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] | 3311 | { |
| 3312 | struct sched_entity *se = &prev->se; |
| 3313 | struct cfs_rq *cfs_rq; |
| 3314 | |
| 3315 | for_each_sched_entity(se) { |
| 3316 | cfs_rq = cfs_rq_of(se); |
Ingo Molnar | ab6cde2 | 2007-08-09 11:16:48 +0200 | [diff] [blame] | 3317 | put_prev_entity(cfs_rq, se); |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 3318 | } |
| 3319 | } |
| 3320 | |
Rik van Riel | ac53db5 | 2011-02-01 09:51:03 -0500 | [diff] [blame] | 3321 | /* |
| 3322 | * sched_yield() is very simple |
| 3323 | * |
| 3324 | * The magic of dealing with the ->skip buddy is in pick_next_entity. |
| 3325 | */ |
| 3326 | static void yield_task_fair(struct rq *rq) |
| 3327 | { |
| 3328 | struct task_struct *curr = rq->curr; |
| 3329 | struct cfs_rq *cfs_rq = task_cfs_rq(curr); |
| 3330 | struct sched_entity *se = &curr->se; |
| 3331 | |
| 3332 | /* |
| 3333 | * Are we the only task in the tree? |
| 3334 | */ |
| 3335 | if (unlikely(rq->nr_running == 1)) |
| 3336 | return; |
| 3337 | |
| 3338 | clear_buddies(cfs_rq, se); |
| 3339 | |
| 3340 | if (curr->policy != SCHED_BATCH) { |
| 3341 | update_rq_clock(rq); |
| 3342 | /* |
| 3343 | * Update run-time statistics of the 'current'. |
| 3344 | */ |
| 3345 | update_curr(cfs_rq); |
Mike Galbraith | 916671c | 2011-11-22 15:21:26 +0100 | [diff] [blame] | 3346 | /* |
| 3347 | * Tell update_rq_clock() that we've just updated, |
| 3348 | * so we don't do microscopic update in schedule() |
| 3349 | * and double the fastpath cost. |
| 3350 | */ |
| 3351 | rq->skip_clock_update = 1; |
Rik van Riel | ac53db5 | 2011-02-01 09:51:03 -0500 | [diff] [blame] | 3352 | } |
| 3353 | |
| 3354 | set_skip_buddy(se); |
| 3355 | } |
| 3356 | |
Mike Galbraith | d95f412 | 2011-02-01 09:50:51 -0500 | [diff] [blame] | 3357 | static bool yield_to_task_fair(struct rq *rq, struct task_struct *p, bool preempt) |
| 3358 | { |
| 3359 | struct sched_entity *se = &p->se; |
| 3360 | |
Paul Turner | 5238cdd | 2011-07-21 09:43:37 -0700 | [diff] [blame] | 3361 | /* throttled hierarchies are not runnable */ |
| 3362 | if (!se->on_rq || throttled_hierarchy(cfs_rq_of(se))) |
Mike Galbraith | d95f412 | 2011-02-01 09:50:51 -0500 | [diff] [blame] | 3363 | return false; |
| 3364 | |
| 3365 | /* Tell the scheduler that we'd really like pse to run next. */ |
| 3366 | set_next_buddy(se); |
| 3367 | |
Mike Galbraith | d95f412 | 2011-02-01 09:50:51 -0500 | [diff] [blame] | 3368 | yield_task_fair(rq); |
| 3369 | |
| 3370 | return true; |
| 3371 | } |
| 3372 | |
Peter Williams | 681f3e6 | 2007-10-24 18:23:51 +0200 | [diff] [blame] | 3373 | #ifdef CONFIG_SMP |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 3374 | /************************************************** |
| 3375 | * Fair scheduling class load-balancing methods: |
| 3376 | */ |
| 3377 | |
Hiroshi Shimamoto | ed387b7 | 2012-01-31 11:40:32 +0900 | [diff] [blame] | 3378 | static unsigned long __read_mostly max_load_balance_interval = HZ/10; |
| 3379 | |
Peter Zijlstra | ddcdf6e | 2012-02-22 19:27:40 +0100 | [diff] [blame] | 3380 | #define LBF_ALL_PINNED 0x01 |
Peter Zijlstra | 367456c | 2012-02-20 21:49:09 +0100 | [diff] [blame] | 3381 | #define LBF_NEED_BREAK 0x02 |
Srivatsa Vaddagiri | 88b8dac | 2012-06-19 17:43:15 +0530 | [diff] [blame] | 3382 | #define LBF_SOME_PINNED 0x04 |
Peter Zijlstra | ddcdf6e | 2012-02-22 19:27:40 +0100 | [diff] [blame] | 3383 | |
| 3384 | struct lb_env { |
| 3385 | struct sched_domain *sd; |
| 3386 | |
Peter Zijlstra | ddcdf6e | 2012-02-22 19:27:40 +0100 | [diff] [blame] | 3387 | struct rq *src_rq; |
Prashanth Nageshappa | 85c1e7d | 2012-06-19 17:47:34 +0530 | [diff] [blame] | 3388 | int src_cpu; |
Peter Zijlstra | ddcdf6e | 2012-02-22 19:27:40 +0100 | [diff] [blame] | 3389 | |
| 3390 | int dst_cpu; |
| 3391 | struct rq *dst_rq; |
| 3392 | |
Srivatsa Vaddagiri | 88b8dac | 2012-06-19 17:43:15 +0530 | [diff] [blame] | 3393 | struct cpumask *dst_grpmask; |
| 3394 | int new_dst_cpu; |
Peter Zijlstra | ddcdf6e | 2012-02-22 19:27:40 +0100 | [diff] [blame] | 3395 | enum cpu_idle_type idle; |
Peter Zijlstra | bd939f4 | 2012-05-02 14:20:37 +0200 | [diff] [blame] | 3396 | long imbalance; |
Michael Wang | b9403130 | 2012-07-12 16:10:13 +0800 | [diff] [blame] | 3397 | /* The set of CPUs under consideration for load-balancing */ |
| 3398 | struct cpumask *cpus; |
| 3399 | |
Peter Zijlstra | ddcdf6e | 2012-02-22 19:27:40 +0100 | [diff] [blame] | 3400 | unsigned int flags; |
Peter Zijlstra | 367456c | 2012-02-20 21:49:09 +0100 | [diff] [blame] | 3401 | |
| 3402 | unsigned int loop; |
| 3403 | unsigned int loop_break; |
| 3404 | unsigned int loop_max; |
Peter Zijlstra | ddcdf6e | 2012-02-22 19:27:40 +0100 | [diff] [blame] | 3405 | }; |
| 3406 | |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 3407 | /* |
Peter Zijlstra | ddcdf6e | 2012-02-22 19:27:40 +0100 | [diff] [blame] | 3408 | * move_task - move a task from one runqueue to another runqueue. |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 3409 | * Both runqueues must be locked. |
| 3410 | */ |
Peter Zijlstra | ddcdf6e | 2012-02-22 19:27:40 +0100 | [diff] [blame] | 3411 | static void move_task(struct task_struct *p, struct lb_env *env) |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 3412 | { |
Peter Zijlstra | ddcdf6e | 2012-02-22 19:27:40 +0100 | [diff] [blame] | 3413 | deactivate_task(env->src_rq, p, 0); |
| 3414 | set_task_cpu(p, env->dst_cpu); |
| 3415 | activate_task(env->dst_rq, p, 0); |
| 3416 | check_preempt_curr(env->dst_rq, p, 0); |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 3417 | } |
| 3418 | |
| 3419 | /* |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 3420 | * Is this task likely cache-hot: |
| 3421 | */ |
| 3422 | static int |
| 3423 | task_hot(struct task_struct *p, u64 now, struct sched_domain *sd) |
| 3424 | { |
| 3425 | s64 delta; |
| 3426 | |
| 3427 | if (p->sched_class != &fair_sched_class) |
| 3428 | return 0; |
| 3429 | |
| 3430 | if (unlikely(p->policy == SCHED_IDLE)) |
| 3431 | return 0; |
| 3432 | |
| 3433 | /* |
| 3434 | * Buddy candidates are cache hot: |
| 3435 | */ |
| 3436 | if (sched_feat(CACHE_HOT_BUDDY) && this_rq()->nr_running && |
| 3437 | (&p->se == cfs_rq_of(&p->se)->next || |
| 3438 | &p->se == cfs_rq_of(&p->se)->last)) |
| 3439 | return 1; |
| 3440 | |
| 3441 | if (sysctl_sched_migration_cost == -1) |
| 3442 | return 1; |
| 3443 | if (sysctl_sched_migration_cost == 0) |
| 3444 | return 0; |
| 3445 | |
| 3446 | delta = now - p->se.exec_start; |
| 3447 | |
| 3448 | return delta < (s64)sysctl_sched_migration_cost; |
| 3449 | } |
| 3450 | |
| 3451 | /* |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 3452 | * can_migrate_task - may task p from runqueue rq be migrated to this_cpu? |
| 3453 | */ |
| 3454 | static |
Peter Zijlstra | 8e45cb5 | 2012-02-22 12:47:19 +0100 | [diff] [blame] | 3455 | int can_migrate_task(struct task_struct *p, struct lb_env *env) |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 3456 | { |
| 3457 | int tsk_cache_hot = 0; |
| 3458 | /* |
| 3459 | * We do not migrate tasks that are: |
| 3460 | * 1) running (obviously), or |
| 3461 | * 2) cannot be migrated to this CPU due to cpus_allowed, or |
| 3462 | * 3) are cache-hot on their current CPU. |
| 3463 | */ |
Peter Zijlstra | ddcdf6e | 2012-02-22 19:27:40 +0100 | [diff] [blame] | 3464 | if (!cpumask_test_cpu(env->dst_cpu, tsk_cpus_allowed(p))) { |
Srivatsa Vaddagiri | 88b8dac | 2012-06-19 17:43:15 +0530 | [diff] [blame] | 3465 | int new_dst_cpu; |
| 3466 | |
Lucas De Marchi | 41acab8 | 2010-03-10 23:37:45 -0300 | [diff] [blame] | 3467 | schedstat_inc(p, se.statistics.nr_failed_migrations_affine); |
Srivatsa Vaddagiri | 88b8dac | 2012-06-19 17:43:15 +0530 | [diff] [blame] | 3468 | |
| 3469 | /* |
| 3470 | * Remember if this task can be migrated to any other cpu in |
| 3471 | * our sched_group. We may want to revisit it if we couldn't |
| 3472 | * meet load balance goals by pulling other tasks on src_cpu. |
| 3473 | * |
| 3474 | * Also avoid computing new_dst_cpu if we have already computed |
| 3475 | * one in current iteration. |
| 3476 | */ |
| 3477 | if (!env->dst_grpmask || (env->flags & LBF_SOME_PINNED)) |
| 3478 | return 0; |
| 3479 | |
| 3480 | new_dst_cpu = cpumask_first_and(env->dst_grpmask, |
| 3481 | tsk_cpus_allowed(p)); |
| 3482 | if (new_dst_cpu < nr_cpu_ids) { |
| 3483 | env->flags |= LBF_SOME_PINNED; |
| 3484 | env->new_dst_cpu = new_dst_cpu; |
| 3485 | } |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 3486 | return 0; |
| 3487 | } |
Srivatsa Vaddagiri | 88b8dac | 2012-06-19 17:43:15 +0530 | [diff] [blame] | 3488 | |
| 3489 | /* 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] | 3490 | env->flags &= ~LBF_ALL_PINNED; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 3491 | |
Peter Zijlstra | ddcdf6e | 2012-02-22 19:27:40 +0100 | [diff] [blame] | 3492 | if (task_running(env->src_rq, p)) { |
Lucas De Marchi | 41acab8 | 2010-03-10 23:37:45 -0300 | [diff] [blame] | 3493 | schedstat_inc(p, se.statistics.nr_failed_migrations_running); |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 3494 | return 0; |
| 3495 | } |
| 3496 | |
| 3497 | /* |
| 3498 | * Aggressive migration if: |
| 3499 | * 1) task is cache cold, or |
| 3500 | * 2) too many balance attempts have failed. |
| 3501 | */ |
| 3502 | |
Peter Zijlstra | ddcdf6e | 2012-02-22 19:27:40 +0100 | [diff] [blame] | 3503 | 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] | 3504 | if (!tsk_cache_hot || |
Peter Zijlstra | 8e45cb5 | 2012-02-22 12:47:19 +0100 | [diff] [blame] | 3505 | env->sd->nr_balance_failed > env->sd->cache_nice_tries) { |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 3506 | #ifdef CONFIG_SCHEDSTATS |
| 3507 | if (tsk_cache_hot) { |
Peter Zijlstra | 8e45cb5 | 2012-02-22 12:47:19 +0100 | [diff] [blame] | 3508 | schedstat_inc(env->sd, lb_hot_gained[env->idle]); |
Lucas De Marchi | 41acab8 | 2010-03-10 23:37:45 -0300 | [diff] [blame] | 3509 | schedstat_inc(p, se.statistics.nr_forced_migrations); |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 3510 | } |
| 3511 | #endif |
| 3512 | return 1; |
| 3513 | } |
| 3514 | |
| 3515 | if (tsk_cache_hot) { |
Lucas De Marchi | 41acab8 | 2010-03-10 23:37:45 -0300 | [diff] [blame] | 3516 | schedstat_inc(p, se.statistics.nr_failed_migrations_hot); |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 3517 | return 0; |
| 3518 | } |
| 3519 | return 1; |
| 3520 | } |
| 3521 | |
Peter Zijlstra | 897c395 | 2009-12-17 17:45:42 +0100 | [diff] [blame] | 3522 | /* |
| 3523 | * move_one_task tries to move exactly one task from busiest to this_rq, as |
| 3524 | * part of active balancing operations within "domain". |
| 3525 | * Returns 1 if successful and 0 otherwise. |
| 3526 | * |
| 3527 | * Called with both runqueues locked. |
| 3528 | */ |
Peter Zijlstra | 8e45cb5 | 2012-02-22 12:47:19 +0100 | [diff] [blame] | 3529 | static int move_one_task(struct lb_env *env) |
Peter Zijlstra | 897c395 | 2009-12-17 17:45:42 +0100 | [diff] [blame] | 3530 | { |
| 3531 | struct task_struct *p, *n; |
Peter Zijlstra | 897c395 | 2009-12-17 17:45:42 +0100 | [diff] [blame] | 3532 | |
Peter Zijlstra | 367456c | 2012-02-20 21:49:09 +0100 | [diff] [blame] | 3533 | list_for_each_entry_safe(p, n, &env->src_rq->cfs_tasks, se.group_node) { |
| 3534 | if (throttled_lb_pair(task_group(p), env->src_rq->cpu, env->dst_cpu)) |
| 3535 | continue; |
Peter Zijlstra | 897c395 | 2009-12-17 17:45:42 +0100 | [diff] [blame] | 3536 | |
Peter Zijlstra | 367456c | 2012-02-20 21:49:09 +0100 | [diff] [blame] | 3537 | if (!can_migrate_task(p, env)) |
| 3538 | continue; |
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 | move_task(p, env); |
| 3541 | /* |
| 3542 | * Right now, this is only the second place move_task() |
| 3543 | * is called, so we can safely collect move_task() |
| 3544 | * stats here rather than inside move_task(). |
| 3545 | */ |
| 3546 | schedstat_inc(env->sd, lb_gained[env->idle]); |
| 3547 | return 1; |
Peter Zijlstra | 897c395 | 2009-12-17 17:45:42 +0100 | [diff] [blame] | 3548 | } |
Peter Zijlstra | 897c395 | 2009-12-17 17:45:42 +0100 | [diff] [blame] | 3549 | return 0; |
| 3550 | } |
| 3551 | |
Peter Zijlstra | 367456c | 2012-02-20 21:49:09 +0100 | [diff] [blame] | 3552 | static unsigned long task_h_load(struct task_struct *p); |
| 3553 | |
Peter Zijlstra | eb95308 | 2012-04-17 13:38:40 +0200 | [diff] [blame] | 3554 | static const unsigned int sched_nr_migrate_break = 32; |
| 3555 | |
Peter Zijlstra | 5d6523e | 2012-03-10 00:07:36 +0100 | [diff] [blame] | 3556 | /* |
Peter Zijlstra | bd939f4 | 2012-05-02 14:20:37 +0200 | [diff] [blame] | 3557 | * 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] | 3558 | * this_rq, as part of a balancing operation within domain "sd". |
| 3559 | * Returns 1 if successful and 0 otherwise. |
| 3560 | * |
| 3561 | * Called with both runqueues locked. |
| 3562 | */ |
| 3563 | static int move_tasks(struct lb_env *env) |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 3564 | { |
Peter Zijlstra | 5d6523e | 2012-03-10 00:07:36 +0100 | [diff] [blame] | 3565 | struct list_head *tasks = &env->src_rq->cfs_tasks; |
| 3566 | struct task_struct *p; |
Peter Zijlstra | 367456c | 2012-02-20 21:49:09 +0100 | [diff] [blame] | 3567 | unsigned long load; |
| 3568 | int pulled = 0; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 3569 | |
Peter Zijlstra | bd939f4 | 2012-05-02 14:20:37 +0200 | [diff] [blame] | 3570 | if (env->imbalance <= 0) |
Peter Zijlstra | 5d6523e | 2012-03-10 00:07:36 +0100 | [diff] [blame] | 3571 | return 0; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 3572 | |
Peter Zijlstra | 5d6523e | 2012-03-10 00:07:36 +0100 | [diff] [blame] | 3573 | while (!list_empty(tasks)) { |
| 3574 | p = list_first_entry(tasks, struct task_struct, se.group_node); |
| 3575 | |
Peter Zijlstra | 367456c | 2012-02-20 21:49:09 +0100 | [diff] [blame] | 3576 | env->loop++; |
| 3577 | /* 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] | 3578 | if (env->loop > env->loop_max) |
Peter Zijlstra | 367456c | 2012-02-20 21:49:09 +0100 | [diff] [blame] | 3579 | break; |
Peter Zijlstra | 5d6523e | 2012-03-10 00:07:36 +0100 | [diff] [blame] | 3580 | |
| 3581 | /* take a breather every nr_migrate tasks */ |
Peter Zijlstra | 367456c | 2012-02-20 21:49:09 +0100 | [diff] [blame] | 3582 | if (env->loop > env->loop_break) { |
Peter Zijlstra | eb95308 | 2012-04-17 13:38:40 +0200 | [diff] [blame] | 3583 | env->loop_break += sched_nr_migrate_break; |
Peter Zijlstra | 8e45cb5 | 2012-02-22 12:47:19 +0100 | [diff] [blame] | 3584 | env->flags |= LBF_NEED_BREAK; |
Peter Zijlstra | ee00e66 | 2009-12-17 17:25:20 +0100 | [diff] [blame] | 3585 | break; |
Peter Zijlstra | a195f00 | 2011-09-22 15:30:18 +0200 | [diff] [blame] | 3586 | } |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 3587 | |
Peter Zijlstra | 5d6523e | 2012-03-10 00:07:36 +0100 | [diff] [blame] | 3588 | 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] | 3589 | goto next; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 3590 | |
Peter Zijlstra | 367456c | 2012-02-20 21:49:09 +0100 | [diff] [blame] | 3591 | load = task_h_load(p); |
Peter Zijlstra | 5d6523e | 2012-03-10 00:07:36 +0100 | [diff] [blame] | 3592 | |
Peter Zijlstra | eb95308 | 2012-04-17 13:38:40 +0200 | [diff] [blame] | 3593 | if (sched_feat(LB_MIN) && load < 16 && !env->sd->nr_balance_failed) |
Peter Zijlstra | 367456c | 2012-02-20 21:49:09 +0100 | [diff] [blame] | 3594 | goto next; |
| 3595 | |
Peter Zijlstra | bd939f4 | 2012-05-02 14:20:37 +0200 | [diff] [blame] | 3596 | if ((load / 2) > env->imbalance) |
Peter Zijlstra | 367456c | 2012-02-20 21:49:09 +0100 | [diff] [blame] | 3597 | goto next; |
| 3598 | |
| 3599 | if (!can_migrate_task(p, env)) |
| 3600 | goto next; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 3601 | |
Peter Zijlstra | ddcdf6e | 2012-02-22 19:27:40 +0100 | [diff] [blame] | 3602 | move_task(p, env); |
Peter Zijlstra | ee00e66 | 2009-12-17 17:25:20 +0100 | [diff] [blame] | 3603 | pulled++; |
Peter Zijlstra | bd939f4 | 2012-05-02 14:20:37 +0200 | [diff] [blame] | 3604 | env->imbalance -= load; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 3605 | |
| 3606 | #ifdef CONFIG_PREEMPT |
Peter Zijlstra | ee00e66 | 2009-12-17 17:25:20 +0100 | [diff] [blame] | 3607 | /* |
| 3608 | * NEWIDLE balancing is a source of latency, so preemptible |
| 3609 | * kernels will stop after the first task is pulled to minimize |
| 3610 | * the critical section. |
| 3611 | */ |
Peter Zijlstra | 5d6523e | 2012-03-10 00:07:36 +0100 | [diff] [blame] | 3612 | if (env->idle == CPU_NEWLY_IDLE) |
Peter Zijlstra | ee00e66 | 2009-12-17 17:25:20 +0100 | [diff] [blame] | 3613 | break; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 3614 | #endif |
| 3615 | |
Peter Zijlstra | ee00e66 | 2009-12-17 17:25:20 +0100 | [diff] [blame] | 3616 | /* |
| 3617 | * We only want to steal up to the prescribed amount of |
| 3618 | * weighted load. |
| 3619 | */ |
Peter Zijlstra | bd939f4 | 2012-05-02 14:20:37 +0200 | [diff] [blame] | 3620 | if (env->imbalance <= 0) |
Peter Zijlstra | ee00e66 | 2009-12-17 17:25:20 +0100 | [diff] [blame] | 3621 | break; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 3622 | |
Peter Zijlstra | 367456c | 2012-02-20 21:49:09 +0100 | [diff] [blame] | 3623 | continue; |
| 3624 | next: |
Peter Zijlstra | 5d6523e | 2012-03-10 00:07:36 +0100 | [diff] [blame] | 3625 | list_move_tail(&p->se.group_node, tasks); |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 3626 | } |
Peter Zijlstra | 5d6523e | 2012-03-10 00:07:36 +0100 | [diff] [blame] | 3627 | |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 3628 | /* |
Peter Zijlstra | ddcdf6e | 2012-02-22 19:27:40 +0100 | [diff] [blame] | 3629 | * Right now, this is one of only two places move_task() is called, |
| 3630 | * so we can safely collect move_task() stats here rather than |
| 3631 | * inside move_task(). |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 3632 | */ |
Peter Zijlstra | 8e45cb5 | 2012-02-22 12:47:19 +0100 | [diff] [blame] | 3633 | schedstat_add(env->sd, lb_gained[env->idle], pulled); |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 3634 | |
Peter Zijlstra | 5d6523e | 2012-03-10 00:07:36 +0100 | [diff] [blame] | 3635 | return pulled; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 3636 | } |
| 3637 | |
Peter Zijlstra | 230059de | 2009-12-17 17:47:12 +0100 | [diff] [blame] | 3638 | #ifdef CONFIG_FAIR_GROUP_SCHED |
Peter Zijlstra | 9e3081c | 2010-11-15 15:47:02 -0800 | [diff] [blame] | 3639 | /* |
| 3640 | * update tg->load_weight by folding this cpu's load_avg |
| 3641 | */ |
Paul Turner | 67e8625 | 2010-11-15 15:47:05 -0800 | [diff] [blame] | 3642 | static int update_shares_cpu(struct task_group *tg, int cpu) |
Peter Zijlstra | 9e3081c | 2010-11-15 15:47:02 -0800 | [diff] [blame] | 3643 | { |
Paul Turner | 8295836 | 2012-10-04 13:18:31 +0200 | [diff] [blame^] | 3644 | struct sched_entity *se; |
Peter Zijlstra | 9e3081c | 2010-11-15 15:47:02 -0800 | [diff] [blame] | 3645 | struct cfs_rq *cfs_rq; |
| 3646 | unsigned long flags; |
| 3647 | struct rq *rq; |
Peter Zijlstra | 9e3081c | 2010-11-15 15:47:02 -0800 | [diff] [blame] | 3648 | |
Peter Zijlstra | 9e3081c | 2010-11-15 15:47:02 -0800 | [diff] [blame] | 3649 | rq = cpu_rq(cpu); |
Paul Turner | 8295836 | 2012-10-04 13:18:31 +0200 | [diff] [blame^] | 3650 | se = tg->se[cpu]; |
Peter Zijlstra | 9e3081c | 2010-11-15 15:47:02 -0800 | [diff] [blame] | 3651 | cfs_rq = tg->cfs_rq[cpu]; |
| 3652 | |
| 3653 | raw_spin_lock_irqsave(&rq->lock, flags); |
| 3654 | |
| 3655 | update_rq_clock(rq); |
Paul Turner | aff3e49 | 2012-10-04 13:18:30 +0200 | [diff] [blame] | 3656 | update_cfs_rq_blocked_load(cfs_rq, 1); |
Peter Zijlstra | 9e3081c | 2010-11-15 15:47:02 -0800 | [diff] [blame] | 3657 | |
Paul Turner | 8295836 | 2012-10-04 13:18:31 +0200 | [diff] [blame^] | 3658 | if (se) { |
| 3659 | update_entity_load_avg(se, 1); |
| 3660 | /* |
| 3661 | * We pivot on our runnable average having decayed to zero for |
| 3662 | * list removal. This generally implies that all our children |
| 3663 | * have also been removed (modulo rounding error or bandwidth |
| 3664 | * control); however, such cases are rare and we can fix these |
| 3665 | * at enqueue. |
| 3666 | * |
| 3667 | * TODO: fix up out-of-order children on enqueue. |
| 3668 | */ |
| 3669 | if (!se->avg.runnable_avg_sum && !cfs_rq->nr_running) |
| 3670 | list_del_leaf_cfs_rq(cfs_rq); |
| 3671 | } else { |
| 3672 | update_rq_runnable_avg(rq, rq->nr_running); |
| 3673 | } |
Peter Zijlstra | 9e3081c | 2010-11-15 15:47:02 -0800 | [diff] [blame] | 3674 | |
| 3675 | raw_spin_unlock_irqrestore(&rq->lock, flags); |
| 3676 | |
| 3677 | return 0; |
| 3678 | } |
| 3679 | |
| 3680 | static void update_shares(int cpu) |
| 3681 | { |
| 3682 | struct cfs_rq *cfs_rq; |
| 3683 | struct rq *rq = cpu_rq(cpu); |
| 3684 | |
| 3685 | rcu_read_lock(); |
Peter Zijlstra | 9763b67 | 2011-07-13 13:09:25 +0200 | [diff] [blame] | 3686 | /* |
| 3687 | * Iterates the task_group tree in a bottom up fashion, see |
| 3688 | * list_add_leaf_cfs_rq() for details. |
| 3689 | */ |
Paul Turner | 64660c8 | 2011-07-21 09:43:36 -0700 | [diff] [blame] | 3690 | for_each_leaf_cfs_rq(rq, cfs_rq) { |
| 3691 | /* throttled entities do not contribute to load */ |
| 3692 | if (throttled_hierarchy(cfs_rq)) |
| 3693 | continue; |
| 3694 | |
Paul Turner | 67e8625 | 2010-11-15 15:47:05 -0800 | [diff] [blame] | 3695 | update_shares_cpu(cfs_rq->tg, cpu); |
Paul Turner | 64660c8 | 2011-07-21 09:43:36 -0700 | [diff] [blame] | 3696 | } |
Peter Zijlstra | 9e3081c | 2010-11-15 15:47:02 -0800 | [diff] [blame] | 3697 | rcu_read_unlock(); |
| 3698 | } |
| 3699 | |
Peter Zijlstra | 9763b67 | 2011-07-13 13:09:25 +0200 | [diff] [blame] | 3700 | /* |
| 3701 | * Compute the cpu's hierarchical load factor for each task group. |
| 3702 | * This needs to be done in a top-down fashion because the load of a child |
| 3703 | * group is a fraction of its parents load. |
| 3704 | */ |
| 3705 | static int tg_load_down(struct task_group *tg, void *data) |
| 3706 | { |
| 3707 | unsigned long load; |
| 3708 | long cpu = (long)data; |
| 3709 | |
| 3710 | if (!tg->parent) { |
| 3711 | load = cpu_rq(cpu)->load.weight; |
| 3712 | } else { |
| 3713 | load = tg->parent->cfs_rq[cpu]->h_load; |
| 3714 | load *= tg->se[cpu]->load.weight; |
| 3715 | load /= tg->parent->cfs_rq[cpu]->load.weight + 1; |
| 3716 | } |
| 3717 | |
| 3718 | tg->cfs_rq[cpu]->h_load = load; |
| 3719 | |
| 3720 | return 0; |
| 3721 | } |
| 3722 | |
| 3723 | static void update_h_load(long cpu) |
| 3724 | { |
Peter Zijlstra | a35b646 | 2012-08-08 21:46:40 +0200 | [diff] [blame] | 3725 | struct rq *rq = cpu_rq(cpu); |
| 3726 | unsigned long now = jiffies; |
| 3727 | |
| 3728 | if (rq->h_load_throttle == now) |
| 3729 | return; |
| 3730 | |
| 3731 | rq->h_load_throttle = now; |
| 3732 | |
Peter Zijlstra | 367456c | 2012-02-20 21:49:09 +0100 | [diff] [blame] | 3733 | rcu_read_lock(); |
Peter Zijlstra | 9763b67 | 2011-07-13 13:09:25 +0200 | [diff] [blame] | 3734 | walk_tg_tree(tg_load_down, tg_nop, (void *)cpu); |
Peter Zijlstra | 367456c | 2012-02-20 21:49:09 +0100 | [diff] [blame] | 3735 | rcu_read_unlock(); |
Peter Zijlstra | 9763b67 | 2011-07-13 13:09:25 +0200 | [diff] [blame] | 3736 | } |
| 3737 | |
Peter Zijlstra | 367456c | 2012-02-20 21:49:09 +0100 | [diff] [blame] | 3738 | static unsigned long task_h_load(struct task_struct *p) |
Peter Zijlstra | 230059de | 2009-12-17 17:47:12 +0100 | [diff] [blame] | 3739 | { |
Peter Zijlstra | 367456c | 2012-02-20 21:49:09 +0100 | [diff] [blame] | 3740 | struct cfs_rq *cfs_rq = task_cfs_rq(p); |
| 3741 | unsigned long load; |
Peter Zijlstra | 230059de | 2009-12-17 17:47:12 +0100 | [diff] [blame] | 3742 | |
Peter Zijlstra | 367456c | 2012-02-20 21:49:09 +0100 | [diff] [blame] | 3743 | load = p->se.load.weight; |
| 3744 | 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] | 3745 | |
Peter Zijlstra | 367456c | 2012-02-20 21:49:09 +0100 | [diff] [blame] | 3746 | return load; |
Peter Zijlstra | 230059de | 2009-12-17 17:47:12 +0100 | [diff] [blame] | 3747 | } |
| 3748 | #else |
Peter Zijlstra | 9e3081c | 2010-11-15 15:47:02 -0800 | [diff] [blame] | 3749 | static inline void update_shares(int cpu) |
| 3750 | { |
| 3751 | } |
| 3752 | |
Peter Zijlstra | 367456c | 2012-02-20 21:49:09 +0100 | [diff] [blame] | 3753 | static inline void update_h_load(long cpu) |
Peter Zijlstra | 230059de | 2009-12-17 17:47:12 +0100 | [diff] [blame] | 3754 | { |
Peter Zijlstra | 367456c | 2012-02-20 21:49:09 +0100 | [diff] [blame] | 3755 | } |
| 3756 | |
| 3757 | static unsigned long task_h_load(struct task_struct *p) |
| 3758 | { |
| 3759 | return p->se.load.weight; |
Peter Zijlstra | 230059de | 2009-12-17 17:47:12 +0100 | [diff] [blame] | 3760 | } |
| 3761 | #endif |
| 3762 | |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 3763 | /********** Helpers for find_busiest_group ************************/ |
| 3764 | /* |
| 3765 | * sd_lb_stats - Structure to store the statistics of a sched_domain |
| 3766 | * during load balancing. |
| 3767 | */ |
| 3768 | struct sd_lb_stats { |
| 3769 | struct sched_group *busiest; /* Busiest group in this sd */ |
| 3770 | struct sched_group *this; /* Local group in this sd */ |
| 3771 | unsigned long total_load; /* Total load of all groups in sd */ |
| 3772 | unsigned long total_pwr; /* Total power of all groups in sd */ |
| 3773 | unsigned long avg_load; /* Average load across all groups in sd */ |
| 3774 | |
| 3775 | /** Statistics of this group */ |
| 3776 | unsigned long this_load; |
| 3777 | unsigned long this_load_per_task; |
| 3778 | unsigned long this_nr_running; |
Nikhil Rao | fab4762 | 2010-10-15 13:12:29 -0700 | [diff] [blame] | 3779 | unsigned long this_has_capacity; |
Suresh Siddha | aae6d3d | 2010-09-17 15:02:32 -0700 | [diff] [blame] | 3780 | unsigned int this_idle_cpus; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 3781 | |
| 3782 | /* Statistics of the busiest group */ |
Suresh Siddha | aae6d3d | 2010-09-17 15:02:32 -0700 | [diff] [blame] | 3783 | unsigned int busiest_idle_cpus; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 3784 | unsigned long max_load; |
| 3785 | unsigned long busiest_load_per_task; |
| 3786 | unsigned long busiest_nr_running; |
Suresh Siddha | dd5feea | 2010-02-23 16:13:52 -0800 | [diff] [blame] | 3787 | unsigned long busiest_group_capacity; |
Nikhil Rao | fab4762 | 2010-10-15 13:12:29 -0700 | [diff] [blame] | 3788 | unsigned long busiest_has_capacity; |
Suresh Siddha | aae6d3d | 2010-09-17 15:02:32 -0700 | [diff] [blame] | 3789 | unsigned int busiest_group_weight; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 3790 | |
| 3791 | int group_imb; /* Is there imbalance in this sd */ |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 3792 | }; |
| 3793 | |
| 3794 | /* |
| 3795 | * sg_lb_stats - stats of a sched_group required for load_balancing |
| 3796 | */ |
| 3797 | struct sg_lb_stats { |
| 3798 | unsigned long avg_load; /*Avg load across the CPUs of the group */ |
| 3799 | unsigned long group_load; /* Total load over the CPUs of the group */ |
| 3800 | unsigned long sum_nr_running; /* Nr tasks running in the group */ |
| 3801 | unsigned long sum_weighted_load; /* Weighted load of group's tasks */ |
| 3802 | unsigned long group_capacity; |
Suresh Siddha | aae6d3d | 2010-09-17 15:02:32 -0700 | [diff] [blame] | 3803 | unsigned long idle_cpus; |
| 3804 | unsigned long group_weight; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 3805 | int group_imb; /* Is there an imbalance in the group ? */ |
Nikhil Rao | fab4762 | 2010-10-15 13:12:29 -0700 | [diff] [blame] | 3806 | int group_has_capacity; /* Is there extra capacity in the group? */ |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 3807 | }; |
| 3808 | |
| 3809 | /** |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 3810 | * get_sd_load_idx - Obtain the load index for a given sched domain. |
| 3811 | * @sd: The sched_domain whose load_idx is to be obtained. |
| 3812 | * @idle: The Idle status of the CPU for whose sd load_icx is obtained. |
| 3813 | */ |
| 3814 | static inline int get_sd_load_idx(struct sched_domain *sd, |
| 3815 | enum cpu_idle_type idle) |
| 3816 | { |
| 3817 | int load_idx; |
| 3818 | |
| 3819 | switch (idle) { |
| 3820 | case CPU_NOT_IDLE: |
| 3821 | load_idx = sd->busy_idx; |
| 3822 | break; |
| 3823 | |
| 3824 | case CPU_NEWLY_IDLE: |
| 3825 | load_idx = sd->newidle_idx; |
| 3826 | break; |
| 3827 | default: |
| 3828 | load_idx = sd->idle_idx; |
| 3829 | break; |
| 3830 | } |
| 3831 | |
| 3832 | return load_idx; |
| 3833 | } |
| 3834 | |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 3835 | unsigned long default_scale_freq_power(struct sched_domain *sd, int cpu) |
| 3836 | { |
Nikhil Rao | 1399fa7 | 2011-05-18 10:09:39 -0700 | [diff] [blame] | 3837 | return SCHED_POWER_SCALE; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 3838 | } |
| 3839 | |
| 3840 | unsigned long __weak arch_scale_freq_power(struct sched_domain *sd, int cpu) |
| 3841 | { |
| 3842 | return default_scale_freq_power(sd, cpu); |
| 3843 | } |
| 3844 | |
| 3845 | unsigned long default_scale_smt_power(struct sched_domain *sd, int cpu) |
| 3846 | { |
Peter Zijlstra | 669c55e | 2010-04-16 14:59:29 +0200 | [diff] [blame] | 3847 | unsigned long weight = sd->span_weight; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 3848 | unsigned long smt_gain = sd->smt_gain; |
| 3849 | |
| 3850 | smt_gain /= weight; |
| 3851 | |
| 3852 | return smt_gain; |
| 3853 | } |
| 3854 | |
| 3855 | unsigned long __weak arch_scale_smt_power(struct sched_domain *sd, int cpu) |
| 3856 | { |
| 3857 | return default_scale_smt_power(sd, cpu); |
| 3858 | } |
| 3859 | |
| 3860 | unsigned long scale_rt_power(int cpu) |
| 3861 | { |
| 3862 | struct rq *rq = cpu_rq(cpu); |
Peter Zijlstra | b654f7d | 2012-05-22 14:04:28 +0200 | [diff] [blame] | 3863 | u64 total, available, age_stamp, avg; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 3864 | |
Peter Zijlstra | b654f7d | 2012-05-22 14:04:28 +0200 | [diff] [blame] | 3865 | /* |
| 3866 | * Since we're reading these variables without serialization make sure |
| 3867 | * we read them once before doing sanity checks on them. |
| 3868 | */ |
| 3869 | age_stamp = ACCESS_ONCE(rq->age_stamp); |
| 3870 | avg = ACCESS_ONCE(rq->rt_avg); |
Venkatesh Pallipadi | aa48380 | 2010-10-04 17:03:22 -0700 | [diff] [blame] | 3871 | |
Peter Zijlstra | b654f7d | 2012-05-22 14:04:28 +0200 | [diff] [blame] | 3872 | total = sched_avg_period() + (rq->clock - age_stamp); |
| 3873 | |
| 3874 | if (unlikely(total < avg)) { |
Venkatesh Pallipadi | aa48380 | 2010-10-04 17:03:22 -0700 | [diff] [blame] | 3875 | /* Ensures that power won't end up being negative */ |
| 3876 | available = 0; |
| 3877 | } else { |
Peter Zijlstra | b654f7d | 2012-05-22 14:04:28 +0200 | [diff] [blame] | 3878 | available = total - avg; |
Venkatesh Pallipadi | aa48380 | 2010-10-04 17:03:22 -0700 | [diff] [blame] | 3879 | } |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 3880 | |
Nikhil Rao | 1399fa7 | 2011-05-18 10:09:39 -0700 | [diff] [blame] | 3881 | if (unlikely((s64)total < SCHED_POWER_SCALE)) |
| 3882 | total = SCHED_POWER_SCALE; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 3883 | |
Nikhil Rao | 1399fa7 | 2011-05-18 10:09:39 -0700 | [diff] [blame] | 3884 | total >>= SCHED_POWER_SHIFT; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 3885 | |
| 3886 | return div_u64(available, total); |
| 3887 | } |
| 3888 | |
| 3889 | static void update_cpu_power(struct sched_domain *sd, int cpu) |
| 3890 | { |
Peter Zijlstra | 669c55e | 2010-04-16 14:59:29 +0200 | [diff] [blame] | 3891 | unsigned long weight = sd->span_weight; |
Nikhil Rao | 1399fa7 | 2011-05-18 10:09:39 -0700 | [diff] [blame] | 3892 | unsigned long power = SCHED_POWER_SCALE; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 3893 | struct sched_group *sdg = sd->groups; |
| 3894 | |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 3895 | if ((sd->flags & SD_SHARE_CPUPOWER) && weight > 1) { |
| 3896 | if (sched_feat(ARCH_POWER)) |
| 3897 | power *= arch_scale_smt_power(sd, cpu); |
| 3898 | else |
| 3899 | power *= default_scale_smt_power(sd, cpu); |
| 3900 | |
Nikhil Rao | 1399fa7 | 2011-05-18 10:09:39 -0700 | [diff] [blame] | 3901 | power >>= SCHED_POWER_SHIFT; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 3902 | } |
| 3903 | |
Peter Zijlstra | 9c3f75c | 2011-07-14 13:00:06 +0200 | [diff] [blame] | 3904 | sdg->sgp->power_orig = power; |
Srivatsa Vaddagiri | 9d5efe0 | 2010-06-08 14:57:02 +1000 | [diff] [blame] | 3905 | |
| 3906 | if (sched_feat(ARCH_POWER)) |
| 3907 | power *= arch_scale_freq_power(sd, cpu); |
| 3908 | else |
| 3909 | power *= default_scale_freq_power(sd, cpu); |
| 3910 | |
Nikhil Rao | 1399fa7 | 2011-05-18 10:09:39 -0700 | [diff] [blame] | 3911 | power >>= SCHED_POWER_SHIFT; |
Srivatsa Vaddagiri | 9d5efe0 | 2010-06-08 14:57:02 +1000 | [diff] [blame] | 3912 | |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 3913 | power *= scale_rt_power(cpu); |
Nikhil Rao | 1399fa7 | 2011-05-18 10:09:39 -0700 | [diff] [blame] | 3914 | power >>= SCHED_POWER_SHIFT; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 3915 | |
| 3916 | if (!power) |
| 3917 | power = 1; |
| 3918 | |
Peter Zijlstra | e51fd5e | 2010-05-31 12:37:30 +0200 | [diff] [blame] | 3919 | cpu_rq(cpu)->cpu_power = power; |
Peter Zijlstra | 9c3f75c | 2011-07-14 13:00:06 +0200 | [diff] [blame] | 3920 | sdg->sgp->power = power; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 3921 | } |
| 3922 | |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 3923 | void update_group_power(struct sched_domain *sd, int cpu) |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 3924 | { |
| 3925 | struct sched_domain *child = sd->child; |
| 3926 | struct sched_group *group, *sdg = sd->groups; |
| 3927 | unsigned long power; |
Vincent Guittot | 4ec4412 | 2011-12-12 20:21:08 +0100 | [diff] [blame] | 3928 | unsigned long interval; |
| 3929 | |
| 3930 | interval = msecs_to_jiffies(sd->balance_interval); |
| 3931 | interval = clamp(interval, 1UL, max_load_balance_interval); |
| 3932 | sdg->sgp->next_update = jiffies + interval; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 3933 | |
| 3934 | if (!child) { |
| 3935 | update_cpu_power(sd, cpu); |
| 3936 | return; |
| 3937 | } |
| 3938 | |
| 3939 | power = 0; |
| 3940 | |
Peter Zijlstra | 74a5ce2 | 2012-05-23 18:00:43 +0200 | [diff] [blame] | 3941 | if (child->flags & SD_OVERLAP) { |
| 3942 | /* |
| 3943 | * SD_OVERLAP domains cannot assume that child groups |
| 3944 | * span the current group. |
| 3945 | */ |
| 3946 | |
| 3947 | for_each_cpu(cpu, sched_group_cpus(sdg)) |
| 3948 | power += power_of(cpu); |
| 3949 | } else { |
| 3950 | /* |
| 3951 | * !SD_OVERLAP domains can assume that child groups |
| 3952 | * span the current group. |
| 3953 | */ |
| 3954 | |
| 3955 | group = child->groups; |
| 3956 | do { |
| 3957 | power += group->sgp->power; |
| 3958 | group = group->next; |
| 3959 | } while (group != child->groups); |
| 3960 | } |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 3961 | |
Peter Zijlstra | c3decf0 | 2012-05-31 12:05:32 +0200 | [diff] [blame] | 3962 | sdg->sgp->power_orig = sdg->sgp->power = power; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 3963 | } |
| 3964 | |
Srivatsa Vaddagiri | 9d5efe0 | 2010-06-08 14:57:02 +1000 | [diff] [blame] | 3965 | /* |
| 3966 | * Try and fix up capacity for tiny siblings, this is needed when |
| 3967 | * things like SD_ASYM_PACKING need f_b_g to select another sibling |
| 3968 | * which on its own isn't powerful enough. |
| 3969 | * |
| 3970 | * See update_sd_pick_busiest() and check_asym_packing(). |
| 3971 | */ |
| 3972 | static inline int |
| 3973 | fix_small_capacity(struct sched_domain *sd, struct sched_group *group) |
| 3974 | { |
| 3975 | /* |
Nikhil Rao | 1399fa7 | 2011-05-18 10:09:39 -0700 | [diff] [blame] | 3976 | * Only siblings can have significantly less than SCHED_POWER_SCALE |
Srivatsa Vaddagiri | 9d5efe0 | 2010-06-08 14:57:02 +1000 | [diff] [blame] | 3977 | */ |
Peter Zijlstra | a6c75f2 | 2011-04-07 14:09:52 +0200 | [diff] [blame] | 3978 | if (!(sd->flags & SD_SHARE_CPUPOWER)) |
Srivatsa Vaddagiri | 9d5efe0 | 2010-06-08 14:57:02 +1000 | [diff] [blame] | 3979 | return 0; |
| 3980 | |
| 3981 | /* |
| 3982 | * If ~90% of the cpu_power is still there, we're good. |
| 3983 | */ |
Peter Zijlstra | 9c3f75c | 2011-07-14 13:00:06 +0200 | [diff] [blame] | 3984 | if (group->sgp->power * 32 > group->sgp->power_orig * 29) |
Srivatsa Vaddagiri | 9d5efe0 | 2010-06-08 14:57:02 +1000 | [diff] [blame] | 3985 | return 1; |
| 3986 | |
| 3987 | return 0; |
| 3988 | } |
| 3989 | |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 3990 | /** |
| 3991 | * update_sg_lb_stats - Update sched_group's statistics for load balancing. |
Randy Dunlap | cd96891 | 2012-06-08 13:18:33 -0700 | [diff] [blame] | 3992 | * @env: The load balancing environment. |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 3993 | * @group: sched_group whose statistics are to be updated. |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 3994 | * @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] | 3995 | * @local_group: Does group contain this_cpu. |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 3996 | * @balance: Should we balance. |
| 3997 | * @sgs: variable to hold the statistics for this group. |
| 3998 | */ |
Peter Zijlstra | bd939f4 | 2012-05-02 14:20:37 +0200 | [diff] [blame] | 3999 | static inline void update_sg_lb_stats(struct lb_env *env, |
| 4000 | struct sched_group *group, int load_idx, |
Michael Wang | b9403130 | 2012-07-12 16:10:13 +0800 | [diff] [blame] | 4001 | int local_group, int *balance, struct sg_lb_stats *sgs) |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4002 | { |
Peter Zijlstra | e44bc5c | 2012-05-11 00:22:12 +0200 | [diff] [blame] | 4003 | unsigned long nr_running, max_nr_running, min_nr_running; |
| 4004 | unsigned long load, max_cpu_load, min_cpu_load; |
Peter Zijlstra | 04f733b | 2012-05-11 00:12:02 +0200 | [diff] [blame] | 4005 | unsigned int balance_cpu = -1, first_idle_cpu = 0; |
Suresh Siddha | dd5feea | 2010-02-23 16:13:52 -0800 | [diff] [blame] | 4006 | unsigned long avg_load_per_task = 0; |
Peter Zijlstra | bd939f4 | 2012-05-02 14:20:37 +0200 | [diff] [blame] | 4007 | int i; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4008 | |
Gautham R Shenoy | 871e35b | 2010-01-20 14:02:44 -0600 | [diff] [blame] | 4009 | if (local_group) |
Peter Zijlstra | c117487 | 2012-05-31 14:47:33 +0200 | [diff] [blame] | 4010 | balance_cpu = group_balance_cpu(group); |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4011 | |
| 4012 | /* Tally up the load of all CPUs in the group */ |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4013 | max_cpu_load = 0; |
| 4014 | min_cpu_load = ~0UL; |
Nikhil Rao | 2582f0e | 2010-10-13 12:09:36 -0700 | [diff] [blame] | 4015 | max_nr_running = 0; |
Peter Zijlstra | e44bc5c | 2012-05-11 00:22:12 +0200 | [diff] [blame] | 4016 | min_nr_running = ~0UL; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4017 | |
Michael Wang | b9403130 | 2012-07-12 16:10:13 +0800 | [diff] [blame] | 4018 | for_each_cpu_and(i, sched_group_cpus(group), env->cpus) { |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4019 | struct rq *rq = cpu_rq(i); |
| 4020 | |
Peter Zijlstra | e44bc5c | 2012-05-11 00:22:12 +0200 | [diff] [blame] | 4021 | nr_running = rq->nr_running; |
| 4022 | |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4023 | /* Bias balancing toward cpus of our domain */ |
| 4024 | if (local_group) { |
Peter Zijlstra | c117487 | 2012-05-31 14:47:33 +0200 | [diff] [blame] | 4025 | if (idle_cpu(i) && !first_idle_cpu && |
| 4026 | cpumask_test_cpu(i, sched_group_mask(group))) { |
Peter Zijlstra | 04f733b | 2012-05-11 00:12:02 +0200 | [diff] [blame] | 4027 | first_idle_cpu = 1; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4028 | balance_cpu = i; |
| 4029 | } |
Peter Zijlstra | 04f733b | 2012-05-11 00:12:02 +0200 | [diff] [blame] | 4030 | |
| 4031 | load = target_load(i, load_idx); |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4032 | } else { |
| 4033 | load = source_load(i, load_idx); |
Peter Zijlstra | e44bc5c | 2012-05-11 00:22:12 +0200 | [diff] [blame] | 4034 | if (load > max_cpu_load) |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4035 | max_cpu_load = load; |
| 4036 | if (min_cpu_load > load) |
| 4037 | min_cpu_load = load; |
Peter Zijlstra | e44bc5c | 2012-05-11 00:22:12 +0200 | [diff] [blame] | 4038 | |
| 4039 | if (nr_running > max_nr_running) |
| 4040 | max_nr_running = nr_running; |
| 4041 | if (min_nr_running > nr_running) |
| 4042 | min_nr_running = nr_running; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4043 | } |
| 4044 | |
| 4045 | sgs->group_load += load; |
Peter Zijlstra | e44bc5c | 2012-05-11 00:22:12 +0200 | [diff] [blame] | 4046 | sgs->sum_nr_running += nr_running; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4047 | sgs->sum_weighted_load += weighted_cpuload(i); |
Suresh Siddha | aae6d3d | 2010-09-17 15:02:32 -0700 | [diff] [blame] | 4048 | if (idle_cpu(i)) |
| 4049 | sgs->idle_cpus++; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4050 | } |
| 4051 | |
| 4052 | /* |
| 4053 | * First idle cpu or the first cpu(busiest) in this sched group |
| 4054 | * is eligible for doing load balancing at this and above |
| 4055 | * domains. In the newly idle case, we will allow all the cpu's |
| 4056 | * to do the newly idle load balance. |
| 4057 | */ |
Vincent Guittot | 4ec4412 | 2011-12-12 20:21:08 +0100 | [diff] [blame] | 4058 | if (local_group) { |
Peter Zijlstra | bd939f4 | 2012-05-02 14:20:37 +0200 | [diff] [blame] | 4059 | if (env->idle != CPU_NEWLY_IDLE) { |
Peter Zijlstra | 04f733b | 2012-05-11 00:12:02 +0200 | [diff] [blame] | 4060 | if (balance_cpu != env->dst_cpu) { |
Vincent Guittot | 4ec4412 | 2011-12-12 20:21:08 +0100 | [diff] [blame] | 4061 | *balance = 0; |
| 4062 | return; |
| 4063 | } |
Peter Zijlstra | bd939f4 | 2012-05-02 14:20:37 +0200 | [diff] [blame] | 4064 | update_group_power(env->sd, env->dst_cpu); |
Vincent Guittot | 4ec4412 | 2011-12-12 20:21:08 +0100 | [diff] [blame] | 4065 | } else if (time_after_eq(jiffies, group->sgp->next_update)) |
Peter Zijlstra | bd939f4 | 2012-05-02 14:20:37 +0200 | [diff] [blame] | 4066 | update_group_power(env->sd, env->dst_cpu); |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4067 | } |
| 4068 | |
| 4069 | /* Adjust by relative CPU power of the group */ |
Peter Zijlstra | 9c3f75c | 2011-07-14 13:00:06 +0200 | [diff] [blame] | 4070 | sgs->avg_load = (sgs->group_load*SCHED_POWER_SCALE) / group->sgp->power; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4071 | |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4072 | /* |
| 4073 | * Consider the group unbalanced when the imbalance is larger |
Peter Zijlstra | 866ab43 | 2011-02-21 18:56:47 +0100 | [diff] [blame] | 4074 | * than the average weight of a task. |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4075 | * |
| 4076 | * APZ: with cgroup the avg task weight can vary wildly and |
| 4077 | * might not be a suitable number - should we keep a |
| 4078 | * normalized nr_running number somewhere that negates |
| 4079 | * the hierarchy? |
| 4080 | */ |
Suresh Siddha | dd5feea | 2010-02-23 16:13:52 -0800 | [diff] [blame] | 4081 | if (sgs->sum_nr_running) |
| 4082 | avg_load_per_task = sgs->sum_weighted_load / sgs->sum_nr_running; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4083 | |
Peter Zijlstra | e44bc5c | 2012-05-11 00:22:12 +0200 | [diff] [blame] | 4084 | if ((max_cpu_load - min_cpu_load) >= avg_load_per_task && |
| 4085 | (max_nr_running - min_nr_running) > 1) |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4086 | sgs->group_imb = 1; |
| 4087 | |
Peter Zijlstra | 9c3f75c | 2011-07-14 13:00:06 +0200 | [diff] [blame] | 4088 | sgs->group_capacity = DIV_ROUND_CLOSEST(group->sgp->power, |
Nikhil Rao | 1399fa7 | 2011-05-18 10:09:39 -0700 | [diff] [blame] | 4089 | SCHED_POWER_SCALE); |
Srivatsa Vaddagiri | 9d5efe0 | 2010-06-08 14:57:02 +1000 | [diff] [blame] | 4090 | if (!sgs->group_capacity) |
Peter Zijlstra | bd939f4 | 2012-05-02 14:20:37 +0200 | [diff] [blame] | 4091 | sgs->group_capacity = fix_small_capacity(env->sd, group); |
Suresh Siddha | aae6d3d | 2010-09-17 15:02:32 -0700 | [diff] [blame] | 4092 | sgs->group_weight = group->group_weight; |
Nikhil Rao | fab4762 | 2010-10-15 13:12:29 -0700 | [diff] [blame] | 4093 | |
| 4094 | if (sgs->group_capacity > sgs->sum_nr_running) |
| 4095 | sgs->group_has_capacity = 1; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4096 | } |
| 4097 | |
| 4098 | /** |
Michael Neuling | 532cb4c | 2010-06-08 14:57:02 +1000 | [diff] [blame] | 4099 | * update_sd_pick_busiest - return 1 on busiest group |
Randy Dunlap | cd96891 | 2012-06-08 13:18:33 -0700 | [diff] [blame] | 4100 | * @env: The load balancing environment. |
Michael Neuling | 532cb4c | 2010-06-08 14:57:02 +1000 | [diff] [blame] | 4101 | * @sds: sched_domain statistics |
| 4102 | * @sg: sched_group candidate to be checked for being the busiest |
Michael Neuling | b6b1229 | 2010-06-10 12:06:21 +1000 | [diff] [blame] | 4103 | * @sgs: sched_group statistics |
Michael Neuling | 532cb4c | 2010-06-08 14:57:02 +1000 | [diff] [blame] | 4104 | * |
| 4105 | * Determine if @sg is a busier group than the previously selected |
| 4106 | * busiest group. |
| 4107 | */ |
Peter Zijlstra | bd939f4 | 2012-05-02 14:20:37 +0200 | [diff] [blame] | 4108 | static bool update_sd_pick_busiest(struct lb_env *env, |
Michael Neuling | 532cb4c | 2010-06-08 14:57:02 +1000 | [diff] [blame] | 4109 | struct sd_lb_stats *sds, |
| 4110 | struct sched_group *sg, |
Peter Zijlstra | bd939f4 | 2012-05-02 14:20:37 +0200 | [diff] [blame] | 4111 | struct sg_lb_stats *sgs) |
Michael Neuling | 532cb4c | 2010-06-08 14:57:02 +1000 | [diff] [blame] | 4112 | { |
| 4113 | if (sgs->avg_load <= sds->max_load) |
| 4114 | return false; |
| 4115 | |
| 4116 | if (sgs->sum_nr_running > sgs->group_capacity) |
| 4117 | return true; |
| 4118 | |
| 4119 | if (sgs->group_imb) |
| 4120 | return true; |
| 4121 | |
| 4122 | /* |
| 4123 | * ASYM_PACKING needs to move all the work to the lowest |
| 4124 | * numbered CPUs in the group, therefore mark all groups |
| 4125 | * higher than ourself as busy. |
| 4126 | */ |
Peter Zijlstra | bd939f4 | 2012-05-02 14:20:37 +0200 | [diff] [blame] | 4127 | if ((env->sd->flags & SD_ASYM_PACKING) && sgs->sum_nr_running && |
| 4128 | env->dst_cpu < group_first_cpu(sg)) { |
Michael Neuling | 532cb4c | 2010-06-08 14:57:02 +1000 | [diff] [blame] | 4129 | if (!sds->busiest) |
| 4130 | return true; |
| 4131 | |
| 4132 | if (group_first_cpu(sds->busiest) > group_first_cpu(sg)) |
| 4133 | return true; |
| 4134 | } |
| 4135 | |
| 4136 | return false; |
| 4137 | } |
| 4138 | |
| 4139 | /** |
Hui Kang | 461819a | 2011-10-11 23:00:59 -0400 | [diff] [blame] | 4140 | * update_sd_lb_stats - Update sched_domain's statistics for load balancing. |
Randy Dunlap | cd96891 | 2012-06-08 13:18:33 -0700 | [diff] [blame] | 4141 | * @env: The load balancing environment. |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4142 | * @balance: Should we balance. |
| 4143 | * @sds: variable to hold the statistics for this sched_domain. |
| 4144 | */ |
Peter Zijlstra | bd939f4 | 2012-05-02 14:20:37 +0200 | [diff] [blame] | 4145 | static inline void update_sd_lb_stats(struct lb_env *env, |
Michael Wang | b9403130 | 2012-07-12 16:10:13 +0800 | [diff] [blame] | 4146 | int *balance, struct sd_lb_stats *sds) |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4147 | { |
Peter Zijlstra | bd939f4 | 2012-05-02 14:20:37 +0200 | [diff] [blame] | 4148 | struct sched_domain *child = env->sd->child; |
| 4149 | struct sched_group *sg = env->sd->groups; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4150 | struct sg_lb_stats sgs; |
| 4151 | int load_idx, prefer_sibling = 0; |
| 4152 | |
| 4153 | if (child && child->flags & SD_PREFER_SIBLING) |
| 4154 | prefer_sibling = 1; |
| 4155 | |
Peter Zijlstra | bd939f4 | 2012-05-02 14:20:37 +0200 | [diff] [blame] | 4156 | load_idx = get_sd_load_idx(env->sd, env->idle); |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4157 | |
| 4158 | do { |
| 4159 | int local_group; |
| 4160 | |
Peter Zijlstra | bd939f4 | 2012-05-02 14:20:37 +0200 | [diff] [blame] | 4161 | local_group = cpumask_test_cpu(env->dst_cpu, sched_group_cpus(sg)); |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4162 | memset(&sgs, 0, sizeof(sgs)); |
Michael Wang | b9403130 | 2012-07-12 16:10:13 +0800 | [diff] [blame] | 4163 | update_sg_lb_stats(env, sg, load_idx, local_group, balance, &sgs); |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4164 | |
Peter Zijlstra | 8f190fb | 2009-12-24 14:18:21 +0100 | [diff] [blame] | 4165 | if (local_group && !(*balance)) |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4166 | return; |
| 4167 | |
| 4168 | sds->total_load += sgs.group_load; |
Peter Zijlstra | 9c3f75c | 2011-07-14 13:00:06 +0200 | [diff] [blame] | 4169 | sds->total_pwr += sg->sgp->power; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4170 | |
| 4171 | /* |
| 4172 | * In case the child domain prefers tasks go to siblings |
Michael Neuling | 532cb4c | 2010-06-08 14:57:02 +1000 | [diff] [blame] | 4173 | * first, lower the sg capacity to one so that we'll try |
Nikhil Rao | 75dd321 | 2010-10-15 13:12:30 -0700 | [diff] [blame] | 4174 | * and move all the excess tasks away. We lower the capacity |
| 4175 | * of a group only if the local group has the capacity to fit |
| 4176 | * these excess tasks, i.e. nr_running < group_capacity. The |
| 4177 | * extra check prevents the case where you always pull from the |
| 4178 | * heaviest group when it is already under-utilized (possible |
| 4179 | * with a large weight task outweighs the tasks on the system). |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4180 | */ |
Nikhil Rao | 75dd321 | 2010-10-15 13:12:30 -0700 | [diff] [blame] | 4181 | if (prefer_sibling && !local_group && sds->this_has_capacity) |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4182 | sgs.group_capacity = min(sgs.group_capacity, 1UL); |
| 4183 | |
| 4184 | if (local_group) { |
| 4185 | sds->this_load = sgs.avg_load; |
Michael Neuling | 532cb4c | 2010-06-08 14:57:02 +1000 | [diff] [blame] | 4186 | sds->this = sg; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4187 | sds->this_nr_running = sgs.sum_nr_running; |
| 4188 | sds->this_load_per_task = sgs.sum_weighted_load; |
Nikhil Rao | fab4762 | 2010-10-15 13:12:29 -0700 | [diff] [blame] | 4189 | sds->this_has_capacity = sgs.group_has_capacity; |
Suresh Siddha | aae6d3d | 2010-09-17 15:02:32 -0700 | [diff] [blame] | 4190 | sds->this_idle_cpus = sgs.idle_cpus; |
Peter Zijlstra | bd939f4 | 2012-05-02 14:20:37 +0200 | [diff] [blame] | 4191 | } else if (update_sd_pick_busiest(env, sds, sg, &sgs)) { |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4192 | sds->max_load = sgs.avg_load; |
Michael Neuling | 532cb4c | 2010-06-08 14:57:02 +1000 | [diff] [blame] | 4193 | sds->busiest = sg; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4194 | sds->busiest_nr_running = sgs.sum_nr_running; |
Suresh Siddha | aae6d3d | 2010-09-17 15:02:32 -0700 | [diff] [blame] | 4195 | sds->busiest_idle_cpus = sgs.idle_cpus; |
Suresh Siddha | dd5feea | 2010-02-23 16:13:52 -0800 | [diff] [blame] | 4196 | sds->busiest_group_capacity = sgs.group_capacity; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4197 | sds->busiest_load_per_task = sgs.sum_weighted_load; |
Nikhil Rao | fab4762 | 2010-10-15 13:12:29 -0700 | [diff] [blame] | 4198 | sds->busiest_has_capacity = sgs.group_has_capacity; |
Suresh Siddha | aae6d3d | 2010-09-17 15:02:32 -0700 | [diff] [blame] | 4199 | sds->busiest_group_weight = sgs.group_weight; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4200 | sds->group_imb = sgs.group_imb; |
| 4201 | } |
| 4202 | |
Michael Neuling | 532cb4c | 2010-06-08 14:57:02 +1000 | [diff] [blame] | 4203 | sg = sg->next; |
Peter Zijlstra | bd939f4 | 2012-05-02 14:20:37 +0200 | [diff] [blame] | 4204 | } while (sg != env->sd->groups); |
Michael Neuling | 532cb4c | 2010-06-08 14:57:02 +1000 | [diff] [blame] | 4205 | } |
| 4206 | |
Michael Neuling | 532cb4c | 2010-06-08 14:57:02 +1000 | [diff] [blame] | 4207 | /** |
| 4208 | * check_asym_packing - Check to see if the group is packed into the |
| 4209 | * sched doman. |
| 4210 | * |
| 4211 | * This is primarily intended to used at the sibling level. Some |
| 4212 | * cores like POWER7 prefer to use lower numbered SMT threads. In the |
| 4213 | * case of POWER7, it can move to lower SMT modes only when higher |
| 4214 | * threads are idle. When in lower SMT modes, the threads will |
| 4215 | * perform better since they share less core resources. Hence when we |
| 4216 | * have idle threads, we want them to be the higher ones. |
| 4217 | * |
| 4218 | * This packing function is run on idle threads. It checks to see if |
| 4219 | * the busiest CPU in this domain (core in the P7 case) has a higher |
| 4220 | * CPU number than the packing function is being run on. Here we are |
| 4221 | * assuming lower CPU number will be equivalent to lower a SMT thread |
| 4222 | * number. |
| 4223 | * |
Michael Neuling | b6b1229 | 2010-06-10 12:06:21 +1000 | [diff] [blame] | 4224 | * Returns 1 when packing is required and a task should be moved to |
| 4225 | * this CPU. The amount of the imbalance is returned in *imbalance. |
| 4226 | * |
Randy Dunlap | cd96891 | 2012-06-08 13:18:33 -0700 | [diff] [blame] | 4227 | * @env: The load balancing environment. |
Michael Neuling | 532cb4c | 2010-06-08 14:57:02 +1000 | [diff] [blame] | 4228 | * @sds: Statistics of the sched_domain which is to be packed |
Michael Neuling | 532cb4c | 2010-06-08 14:57:02 +1000 | [diff] [blame] | 4229 | */ |
Peter Zijlstra | bd939f4 | 2012-05-02 14:20:37 +0200 | [diff] [blame] | 4230 | 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] | 4231 | { |
| 4232 | int busiest_cpu; |
| 4233 | |
Peter Zijlstra | bd939f4 | 2012-05-02 14:20:37 +0200 | [diff] [blame] | 4234 | if (!(env->sd->flags & SD_ASYM_PACKING)) |
Michael Neuling | 532cb4c | 2010-06-08 14:57:02 +1000 | [diff] [blame] | 4235 | return 0; |
| 4236 | |
| 4237 | if (!sds->busiest) |
| 4238 | return 0; |
| 4239 | |
| 4240 | busiest_cpu = group_first_cpu(sds->busiest); |
Peter Zijlstra | bd939f4 | 2012-05-02 14:20:37 +0200 | [diff] [blame] | 4241 | if (env->dst_cpu > busiest_cpu) |
Michael Neuling | 532cb4c | 2010-06-08 14:57:02 +1000 | [diff] [blame] | 4242 | return 0; |
| 4243 | |
Peter Zijlstra | bd939f4 | 2012-05-02 14:20:37 +0200 | [diff] [blame] | 4244 | env->imbalance = DIV_ROUND_CLOSEST( |
| 4245 | sds->max_load * sds->busiest->sgp->power, SCHED_POWER_SCALE); |
| 4246 | |
Michael Neuling | 532cb4c | 2010-06-08 14:57:02 +1000 | [diff] [blame] | 4247 | return 1; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4248 | } |
| 4249 | |
| 4250 | /** |
| 4251 | * fix_small_imbalance - Calculate the minor imbalance that exists |
| 4252 | * amongst the groups of a sched_domain, during |
| 4253 | * load balancing. |
Randy Dunlap | cd96891 | 2012-06-08 13:18:33 -0700 | [diff] [blame] | 4254 | * @env: The load balancing environment. |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4255 | * @sds: Statistics of the sched_domain whose imbalance is to be calculated. |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4256 | */ |
Peter Zijlstra | bd939f4 | 2012-05-02 14:20:37 +0200 | [diff] [blame] | 4257 | static inline |
| 4258 | 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] | 4259 | { |
| 4260 | unsigned long tmp, pwr_now = 0, pwr_move = 0; |
| 4261 | unsigned int imbn = 2; |
Suresh Siddha | dd5feea | 2010-02-23 16:13:52 -0800 | [diff] [blame] | 4262 | unsigned long scaled_busy_load_per_task; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4263 | |
| 4264 | if (sds->this_nr_running) { |
| 4265 | sds->this_load_per_task /= sds->this_nr_running; |
| 4266 | if (sds->busiest_load_per_task > |
| 4267 | sds->this_load_per_task) |
| 4268 | imbn = 1; |
Peter Zijlstra | bd939f4 | 2012-05-02 14:20:37 +0200 | [diff] [blame] | 4269 | } else { |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4270 | sds->this_load_per_task = |
Peter Zijlstra | bd939f4 | 2012-05-02 14:20:37 +0200 | [diff] [blame] | 4271 | cpu_avg_load_per_task(env->dst_cpu); |
| 4272 | } |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4273 | |
Suresh Siddha | dd5feea | 2010-02-23 16:13:52 -0800 | [diff] [blame] | 4274 | scaled_busy_load_per_task = sds->busiest_load_per_task |
Nikhil Rao | 1399fa7 | 2011-05-18 10:09:39 -0700 | [diff] [blame] | 4275 | * SCHED_POWER_SCALE; |
Peter Zijlstra | 9c3f75c | 2011-07-14 13:00:06 +0200 | [diff] [blame] | 4276 | scaled_busy_load_per_task /= sds->busiest->sgp->power; |
Suresh Siddha | dd5feea | 2010-02-23 16:13:52 -0800 | [diff] [blame] | 4277 | |
| 4278 | if (sds->max_load - sds->this_load + scaled_busy_load_per_task >= |
| 4279 | (scaled_busy_load_per_task * imbn)) { |
Peter Zijlstra | bd939f4 | 2012-05-02 14:20:37 +0200 | [diff] [blame] | 4280 | env->imbalance = sds->busiest_load_per_task; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4281 | return; |
| 4282 | } |
| 4283 | |
| 4284 | /* |
| 4285 | * OK, we don't have enough imbalance to justify moving tasks, |
| 4286 | * however we may be able to increase total CPU power used by |
| 4287 | * moving them. |
| 4288 | */ |
| 4289 | |
Peter Zijlstra | 9c3f75c | 2011-07-14 13:00:06 +0200 | [diff] [blame] | 4290 | pwr_now += sds->busiest->sgp->power * |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4291 | min(sds->busiest_load_per_task, sds->max_load); |
Peter Zijlstra | 9c3f75c | 2011-07-14 13:00:06 +0200 | [diff] [blame] | 4292 | pwr_now += sds->this->sgp->power * |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4293 | min(sds->this_load_per_task, sds->this_load); |
Nikhil Rao | 1399fa7 | 2011-05-18 10:09:39 -0700 | [diff] [blame] | 4294 | pwr_now /= SCHED_POWER_SCALE; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4295 | |
| 4296 | /* Amount of load we'd subtract */ |
Nikhil Rao | 1399fa7 | 2011-05-18 10:09:39 -0700 | [diff] [blame] | 4297 | tmp = (sds->busiest_load_per_task * SCHED_POWER_SCALE) / |
Peter Zijlstra | 9c3f75c | 2011-07-14 13:00:06 +0200 | [diff] [blame] | 4298 | sds->busiest->sgp->power; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4299 | if (sds->max_load > tmp) |
Peter Zijlstra | 9c3f75c | 2011-07-14 13:00:06 +0200 | [diff] [blame] | 4300 | pwr_move += sds->busiest->sgp->power * |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4301 | min(sds->busiest_load_per_task, sds->max_load - tmp); |
| 4302 | |
| 4303 | /* Amount of load we'd add */ |
Peter Zijlstra | 9c3f75c | 2011-07-14 13:00:06 +0200 | [diff] [blame] | 4304 | if (sds->max_load * sds->busiest->sgp->power < |
Nikhil Rao | 1399fa7 | 2011-05-18 10:09:39 -0700 | [diff] [blame] | 4305 | sds->busiest_load_per_task * SCHED_POWER_SCALE) |
Peter Zijlstra | 9c3f75c | 2011-07-14 13:00:06 +0200 | [diff] [blame] | 4306 | tmp = (sds->max_load * sds->busiest->sgp->power) / |
| 4307 | sds->this->sgp->power; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4308 | else |
Nikhil Rao | 1399fa7 | 2011-05-18 10:09:39 -0700 | [diff] [blame] | 4309 | tmp = (sds->busiest_load_per_task * SCHED_POWER_SCALE) / |
Peter Zijlstra | 9c3f75c | 2011-07-14 13:00:06 +0200 | [diff] [blame] | 4310 | sds->this->sgp->power; |
| 4311 | pwr_move += sds->this->sgp->power * |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4312 | min(sds->this_load_per_task, sds->this_load + tmp); |
Nikhil Rao | 1399fa7 | 2011-05-18 10:09:39 -0700 | [diff] [blame] | 4313 | pwr_move /= SCHED_POWER_SCALE; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4314 | |
| 4315 | /* Move if we gain throughput */ |
| 4316 | if (pwr_move > pwr_now) |
Peter Zijlstra | bd939f4 | 2012-05-02 14:20:37 +0200 | [diff] [blame] | 4317 | env->imbalance = sds->busiest_load_per_task; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4318 | } |
| 4319 | |
| 4320 | /** |
| 4321 | * calculate_imbalance - Calculate the amount of imbalance present within the |
| 4322 | * groups of a given sched_domain during load balance. |
Peter Zijlstra | bd939f4 | 2012-05-02 14:20:37 +0200 | [diff] [blame] | 4323 | * @env: load balance environment |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4324 | * @sds: statistics of the sched_domain whose imbalance is to be calculated. |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4325 | */ |
Peter Zijlstra | bd939f4 | 2012-05-02 14:20:37 +0200 | [diff] [blame] | 4326 | 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] | 4327 | { |
Suresh Siddha | dd5feea | 2010-02-23 16:13:52 -0800 | [diff] [blame] | 4328 | unsigned long max_pull, load_above_capacity = ~0UL; |
| 4329 | |
| 4330 | sds->busiest_load_per_task /= sds->busiest_nr_running; |
| 4331 | if (sds->group_imb) { |
| 4332 | sds->busiest_load_per_task = |
| 4333 | min(sds->busiest_load_per_task, sds->avg_load); |
| 4334 | } |
| 4335 | |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4336 | /* |
| 4337 | * In the presence of smp nice balancing, certain scenarios can have |
| 4338 | * max load less than avg load(as we skip the groups at or below |
| 4339 | * its cpu_power, while calculating max_load..) |
| 4340 | */ |
| 4341 | if (sds->max_load < sds->avg_load) { |
Peter Zijlstra | bd939f4 | 2012-05-02 14:20:37 +0200 | [diff] [blame] | 4342 | env->imbalance = 0; |
| 4343 | return fix_small_imbalance(env, sds); |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4344 | } |
| 4345 | |
Suresh Siddha | dd5feea | 2010-02-23 16:13:52 -0800 | [diff] [blame] | 4346 | if (!sds->group_imb) { |
| 4347 | /* |
| 4348 | * Don't want to pull so many tasks that a group would go idle. |
| 4349 | */ |
| 4350 | load_above_capacity = (sds->busiest_nr_running - |
| 4351 | sds->busiest_group_capacity); |
| 4352 | |
Nikhil Rao | 1399fa7 | 2011-05-18 10:09:39 -0700 | [diff] [blame] | 4353 | load_above_capacity *= (SCHED_LOAD_SCALE * SCHED_POWER_SCALE); |
Suresh Siddha | dd5feea | 2010-02-23 16:13:52 -0800 | [diff] [blame] | 4354 | |
Peter Zijlstra | 9c3f75c | 2011-07-14 13:00:06 +0200 | [diff] [blame] | 4355 | load_above_capacity /= sds->busiest->sgp->power; |
Suresh Siddha | dd5feea | 2010-02-23 16:13:52 -0800 | [diff] [blame] | 4356 | } |
| 4357 | |
| 4358 | /* |
| 4359 | * We're trying to get all the cpus to the average_load, so we don't |
| 4360 | * want to push ourselves above the average load, nor do we wish to |
| 4361 | * reduce the max loaded cpu below the average load. At the same time, |
| 4362 | * we also don't want to reduce the group load below the group capacity |
| 4363 | * (so that we can implement power-savings policies etc). Thus we look |
| 4364 | * for the minimum possible imbalance. |
| 4365 | * Be careful of negative numbers as they'll appear as very large values |
| 4366 | * with unsigned longs. |
| 4367 | */ |
| 4368 | max_pull = min(sds->max_load - sds->avg_load, load_above_capacity); |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4369 | |
| 4370 | /* How much load to actually move to equalise the imbalance */ |
Peter Zijlstra | bd939f4 | 2012-05-02 14:20:37 +0200 | [diff] [blame] | 4371 | env->imbalance = min(max_pull * sds->busiest->sgp->power, |
Peter Zijlstra | 9c3f75c | 2011-07-14 13:00:06 +0200 | [diff] [blame] | 4372 | (sds->avg_load - sds->this_load) * sds->this->sgp->power) |
Nikhil Rao | 1399fa7 | 2011-05-18 10:09:39 -0700 | [diff] [blame] | 4373 | / SCHED_POWER_SCALE; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4374 | |
| 4375 | /* |
| 4376 | * if *imbalance is less than the average load per runnable task |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 4377 | * 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] | 4378 | * a think about bumping its value to force at least one task to be |
| 4379 | * moved |
| 4380 | */ |
Peter Zijlstra | bd939f4 | 2012-05-02 14:20:37 +0200 | [diff] [blame] | 4381 | if (env->imbalance < sds->busiest_load_per_task) |
| 4382 | return fix_small_imbalance(env, sds); |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4383 | |
| 4384 | } |
Nikhil Rao | fab4762 | 2010-10-15 13:12:29 -0700 | [diff] [blame] | 4385 | |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4386 | /******* find_busiest_group() helpers end here *********************/ |
| 4387 | |
| 4388 | /** |
| 4389 | * find_busiest_group - Returns the busiest group within the sched_domain |
| 4390 | * if there is an imbalance. If there isn't an imbalance, and |
| 4391 | * the user has opted for power-savings, it returns a group whose |
| 4392 | * CPUs can be put to idle by rebalancing those tasks elsewhere, if |
| 4393 | * such a group exists. |
| 4394 | * |
| 4395 | * Also calculates the amount of weighted load which should be moved |
| 4396 | * to restore balance. |
| 4397 | * |
Randy Dunlap | cd96891 | 2012-06-08 13:18:33 -0700 | [diff] [blame] | 4398 | * @env: The load balancing environment. |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4399 | * @balance: Pointer to a variable indicating if this_cpu |
| 4400 | * is the appropriate cpu to perform load balancing at this_level. |
| 4401 | * |
| 4402 | * Returns: - the busiest group if imbalance exists. |
| 4403 | * - If no imbalance and user has opted for power-savings balance, |
| 4404 | * return the least loaded group whose CPUs can be |
| 4405 | * put to idle by rebalancing its tasks onto our group. |
| 4406 | */ |
| 4407 | static struct sched_group * |
Michael Wang | b9403130 | 2012-07-12 16:10:13 +0800 | [diff] [blame] | 4408 | find_busiest_group(struct lb_env *env, int *balance) |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4409 | { |
| 4410 | struct sd_lb_stats sds; |
| 4411 | |
| 4412 | memset(&sds, 0, sizeof(sds)); |
| 4413 | |
| 4414 | /* |
| 4415 | * Compute the various statistics relavent for load balancing at |
| 4416 | * this level. |
| 4417 | */ |
Michael Wang | b9403130 | 2012-07-12 16:10:13 +0800 | [diff] [blame] | 4418 | update_sd_lb_stats(env, balance, &sds); |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4419 | |
Peter Zijlstra | cc57aa8 | 2011-02-21 18:55:32 +0100 | [diff] [blame] | 4420 | /* |
| 4421 | * this_cpu is not the appropriate cpu to perform load balancing at |
| 4422 | * this level. |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4423 | */ |
Peter Zijlstra | 8f190fb | 2009-12-24 14:18:21 +0100 | [diff] [blame] | 4424 | if (!(*balance)) |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4425 | goto ret; |
| 4426 | |
Peter Zijlstra | bd939f4 | 2012-05-02 14:20:37 +0200 | [diff] [blame] | 4427 | if ((env->idle == CPU_IDLE || env->idle == CPU_NEWLY_IDLE) && |
| 4428 | check_asym_packing(env, &sds)) |
Michael Neuling | 532cb4c | 2010-06-08 14:57:02 +1000 | [diff] [blame] | 4429 | return sds.busiest; |
| 4430 | |
Peter Zijlstra | cc57aa8 | 2011-02-21 18:55:32 +0100 | [diff] [blame] | 4431 | /* There is no busy sibling group to pull tasks from */ |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4432 | if (!sds.busiest || sds.busiest_nr_running == 0) |
| 4433 | goto out_balanced; |
| 4434 | |
Nikhil Rao | 1399fa7 | 2011-05-18 10:09:39 -0700 | [diff] [blame] | 4435 | sds.avg_load = (SCHED_POWER_SCALE * sds.total_load) / sds.total_pwr; |
Ken Chen | b0432d8 | 2011-04-07 17:23:22 -0700 | [diff] [blame] | 4436 | |
Peter Zijlstra | 866ab43 | 2011-02-21 18:56:47 +0100 | [diff] [blame] | 4437 | /* |
| 4438 | * If the busiest group is imbalanced the below checks don't |
| 4439 | * work because they assumes all things are equal, which typically |
| 4440 | * isn't true due to cpus_allowed constraints and the like. |
| 4441 | */ |
| 4442 | if (sds.group_imb) |
| 4443 | goto force_balance; |
| 4444 | |
Peter Zijlstra | cc57aa8 | 2011-02-21 18:55:32 +0100 | [diff] [blame] | 4445 | /* SD_BALANCE_NEWIDLE trumps SMP nice when underutilized */ |
Peter Zijlstra | bd939f4 | 2012-05-02 14:20:37 +0200 | [diff] [blame] | 4446 | if (env->idle == CPU_NEWLY_IDLE && sds.this_has_capacity && |
Nikhil Rao | fab4762 | 2010-10-15 13:12:29 -0700 | [diff] [blame] | 4447 | !sds.busiest_has_capacity) |
| 4448 | goto force_balance; |
| 4449 | |
Peter Zijlstra | cc57aa8 | 2011-02-21 18:55:32 +0100 | [diff] [blame] | 4450 | /* |
| 4451 | * If the local group is more busy than the selected busiest group |
| 4452 | * don't try and pull any tasks. |
| 4453 | */ |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4454 | if (sds.this_load >= sds.max_load) |
| 4455 | goto out_balanced; |
| 4456 | |
Peter Zijlstra | cc57aa8 | 2011-02-21 18:55:32 +0100 | [diff] [blame] | 4457 | /* |
| 4458 | * Don't pull any tasks if this group is already above the domain |
| 4459 | * average load. |
| 4460 | */ |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4461 | if (sds.this_load >= sds.avg_load) |
| 4462 | goto out_balanced; |
| 4463 | |
Peter Zijlstra | bd939f4 | 2012-05-02 14:20:37 +0200 | [diff] [blame] | 4464 | if (env->idle == CPU_IDLE) { |
Suresh Siddha | aae6d3d | 2010-09-17 15:02:32 -0700 | [diff] [blame] | 4465 | /* |
| 4466 | * This cpu is idle. If the busiest group load doesn't |
| 4467 | * have more tasks than the number of available cpu's and |
| 4468 | * there is no imbalance between this and busiest group |
| 4469 | * wrt to idle cpu's, it is balanced. |
| 4470 | */ |
Peter Zijlstra | c186faf | 2011-02-21 18:52:53 +0100 | [diff] [blame] | 4471 | if ((sds.this_idle_cpus <= sds.busiest_idle_cpus + 1) && |
Suresh Siddha | aae6d3d | 2010-09-17 15:02:32 -0700 | [diff] [blame] | 4472 | sds.busiest_nr_running <= sds.busiest_group_weight) |
| 4473 | goto out_balanced; |
Peter Zijlstra | c186faf | 2011-02-21 18:52:53 +0100 | [diff] [blame] | 4474 | } else { |
| 4475 | /* |
| 4476 | * In the CPU_NEWLY_IDLE, CPU_NOT_IDLE cases, use |
| 4477 | * imbalance_pct to be conservative. |
| 4478 | */ |
Peter Zijlstra | bd939f4 | 2012-05-02 14:20:37 +0200 | [diff] [blame] | 4479 | if (100 * sds.max_load <= env->sd->imbalance_pct * sds.this_load) |
Peter Zijlstra | c186faf | 2011-02-21 18:52:53 +0100 | [diff] [blame] | 4480 | goto out_balanced; |
Suresh Siddha | aae6d3d | 2010-09-17 15:02:32 -0700 | [diff] [blame] | 4481 | } |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4482 | |
Nikhil Rao | fab4762 | 2010-10-15 13:12:29 -0700 | [diff] [blame] | 4483 | force_balance: |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4484 | /* Looks like there is an imbalance. Compute it */ |
Peter Zijlstra | bd939f4 | 2012-05-02 14:20:37 +0200 | [diff] [blame] | 4485 | calculate_imbalance(env, &sds); |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4486 | return sds.busiest; |
| 4487 | |
| 4488 | out_balanced: |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4489 | ret: |
Peter Zijlstra | bd939f4 | 2012-05-02 14:20:37 +0200 | [diff] [blame] | 4490 | env->imbalance = 0; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4491 | return NULL; |
| 4492 | } |
| 4493 | |
| 4494 | /* |
| 4495 | * find_busiest_queue - find the busiest runqueue among the cpus in group. |
| 4496 | */ |
Peter Zijlstra | bd939f4 | 2012-05-02 14:20:37 +0200 | [diff] [blame] | 4497 | static struct rq *find_busiest_queue(struct lb_env *env, |
Michael Wang | b9403130 | 2012-07-12 16:10:13 +0800 | [diff] [blame] | 4498 | struct sched_group *group) |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4499 | { |
| 4500 | struct rq *busiest = NULL, *rq; |
| 4501 | unsigned long max_load = 0; |
| 4502 | int i; |
| 4503 | |
| 4504 | for_each_cpu(i, sched_group_cpus(group)) { |
| 4505 | unsigned long power = power_of(i); |
Nikhil Rao | 1399fa7 | 2011-05-18 10:09:39 -0700 | [diff] [blame] | 4506 | unsigned long capacity = DIV_ROUND_CLOSEST(power, |
| 4507 | SCHED_POWER_SCALE); |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4508 | unsigned long wl; |
| 4509 | |
Srivatsa Vaddagiri | 9d5efe0 | 2010-06-08 14:57:02 +1000 | [diff] [blame] | 4510 | if (!capacity) |
Peter Zijlstra | bd939f4 | 2012-05-02 14:20:37 +0200 | [diff] [blame] | 4511 | capacity = fix_small_capacity(env->sd, group); |
Srivatsa Vaddagiri | 9d5efe0 | 2010-06-08 14:57:02 +1000 | [diff] [blame] | 4512 | |
Michael Wang | b9403130 | 2012-07-12 16:10:13 +0800 | [diff] [blame] | 4513 | if (!cpumask_test_cpu(i, env->cpus)) |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4514 | continue; |
| 4515 | |
| 4516 | rq = cpu_rq(i); |
Thomas Gleixner | 6e40f5b | 2010-02-16 16:48:56 +0100 | [diff] [blame] | 4517 | wl = weighted_cpuload(i); |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4518 | |
Thomas Gleixner | 6e40f5b | 2010-02-16 16:48:56 +0100 | [diff] [blame] | 4519 | /* |
| 4520 | * When comparing with imbalance, use weighted_cpuload() |
| 4521 | * which is not scaled with the cpu power. |
| 4522 | */ |
Peter Zijlstra | bd939f4 | 2012-05-02 14:20:37 +0200 | [diff] [blame] | 4523 | if (capacity && rq->nr_running == 1 && wl > env->imbalance) |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4524 | continue; |
| 4525 | |
Thomas Gleixner | 6e40f5b | 2010-02-16 16:48:56 +0100 | [diff] [blame] | 4526 | /* |
| 4527 | * For the load comparisons with the other cpu's, consider |
| 4528 | * the weighted_cpuload() scaled with the cpu power, so that |
| 4529 | * the load can be moved away from the cpu that is potentially |
| 4530 | * running at a lower capacity. |
| 4531 | */ |
Nikhil Rao | 1399fa7 | 2011-05-18 10:09:39 -0700 | [diff] [blame] | 4532 | wl = (wl * SCHED_POWER_SCALE) / power; |
Thomas Gleixner | 6e40f5b | 2010-02-16 16:48:56 +0100 | [diff] [blame] | 4533 | |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4534 | if (wl > max_load) { |
| 4535 | max_load = wl; |
| 4536 | busiest = rq; |
| 4537 | } |
| 4538 | } |
| 4539 | |
| 4540 | return busiest; |
| 4541 | } |
| 4542 | |
| 4543 | /* |
| 4544 | * Max backoff if we encounter pinned tasks. Pretty arbitrary value, but |
| 4545 | * so long as it is large enough. |
| 4546 | */ |
| 4547 | #define MAX_PINNED_INTERVAL 512 |
| 4548 | |
| 4549 | /* Working cpumask for load_balance and load_balance_newidle. */ |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 4550 | DEFINE_PER_CPU(cpumask_var_t, load_balance_tmpmask); |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4551 | |
Peter Zijlstra | bd939f4 | 2012-05-02 14:20:37 +0200 | [diff] [blame] | 4552 | static int need_active_balance(struct lb_env *env) |
Peter Zijlstra | 1af3ed3 | 2009-12-23 15:10:31 +0100 | [diff] [blame] | 4553 | { |
Peter Zijlstra | bd939f4 | 2012-05-02 14:20:37 +0200 | [diff] [blame] | 4554 | struct sched_domain *sd = env->sd; |
| 4555 | |
| 4556 | if (env->idle == CPU_NEWLY_IDLE) { |
Michael Neuling | 532cb4c | 2010-06-08 14:57:02 +1000 | [diff] [blame] | 4557 | |
| 4558 | /* |
| 4559 | * ASYM_PACKING needs to force migrate tasks from busy but |
| 4560 | * higher numbered CPUs in order to pack all tasks in the |
| 4561 | * lowest numbered CPUs. |
| 4562 | */ |
Peter Zijlstra | bd939f4 | 2012-05-02 14:20:37 +0200 | [diff] [blame] | 4563 | if ((sd->flags & SD_ASYM_PACKING) && env->src_cpu > env->dst_cpu) |
Michael Neuling | 532cb4c | 2010-06-08 14:57:02 +1000 | [diff] [blame] | 4564 | return 1; |
Peter Zijlstra | 1af3ed3 | 2009-12-23 15:10:31 +0100 | [diff] [blame] | 4565 | } |
| 4566 | |
| 4567 | return unlikely(sd->nr_balance_failed > sd->cache_nice_tries+2); |
| 4568 | } |
| 4569 | |
Tejun Heo | 969c792 | 2010-05-06 18:49:21 +0200 | [diff] [blame] | 4570 | static int active_load_balance_cpu_stop(void *data); |
| 4571 | |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4572 | /* |
| 4573 | * Check this_cpu to ensure it is balanced within domain. Attempt to move |
| 4574 | * tasks if there is an imbalance. |
| 4575 | */ |
| 4576 | static int load_balance(int this_cpu, struct rq *this_rq, |
| 4577 | struct sched_domain *sd, enum cpu_idle_type idle, |
| 4578 | int *balance) |
| 4579 | { |
Srivatsa Vaddagiri | 88b8dac | 2012-06-19 17:43:15 +0530 | [diff] [blame] | 4580 | int ld_moved, cur_ld_moved, active_balance = 0; |
| 4581 | int lb_iterations, max_lb_iterations; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4582 | struct sched_group *group; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4583 | struct rq *busiest; |
| 4584 | unsigned long flags; |
| 4585 | struct cpumask *cpus = __get_cpu_var(load_balance_tmpmask); |
| 4586 | |
Peter Zijlstra | 8e45cb5 | 2012-02-22 12:47:19 +0100 | [diff] [blame] | 4587 | struct lb_env env = { |
| 4588 | .sd = sd, |
Peter Zijlstra | ddcdf6e | 2012-02-22 19:27:40 +0100 | [diff] [blame] | 4589 | .dst_cpu = this_cpu, |
| 4590 | .dst_rq = this_rq, |
Srivatsa Vaddagiri | 88b8dac | 2012-06-19 17:43:15 +0530 | [diff] [blame] | 4591 | .dst_grpmask = sched_group_cpus(sd->groups), |
Peter Zijlstra | 8e45cb5 | 2012-02-22 12:47:19 +0100 | [diff] [blame] | 4592 | .idle = idle, |
Peter Zijlstra | eb95308 | 2012-04-17 13:38:40 +0200 | [diff] [blame] | 4593 | .loop_break = sched_nr_migrate_break, |
Michael Wang | b9403130 | 2012-07-12 16:10:13 +0800 | [diff] [blame] | 4594 | .cpus = cpus, |
Peter Zijlstra | 8e45cb5 | 2012-02-22 12:47:19 +0100 | [diff] [blame] | 4595 | }; |
| 4596 | |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4597 | cpumask_copy(cpus, cpu_active_mask); |
Srivatsa Vaddagiri | 88b8dac | 2012-06-19 17:43:15 +0530 | [diff] [blame] | 4598 | max_lb_iterations = cpumask_weight(env.dst_grpmask); |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4599 | |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4600 | schedstat_inc(sd, lb_count[idle]); |
| 4601 | |
| 4602 | redo: |
Michael Wang | b9403130 | 2012-07-12 16:10:13 +0800 | [diff] [blame] | 4603 | group = find_busiest_group(&env, balance); |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4604 | |
| 4605 | if (*balance == 0) |
| 4606 | goto out_balanced; |
| 4607 | |
| 4608 | if (!group) { |
| 4609 | schedstat_inc(sd, lb_nobusyg[idle]); |
| 4610 | goto out_balanced; |
| 4611 | } |
| 4612 | |
Michael Wang | b9403130 | 2012-07-12 16:10:13 +0800 | [diff] [blame] | 4613 | busiest = find_busiest_queue(&env, group); |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4614 | if (!busiest) { |
| 4615 | schedstat_inc(sd, lb_nobusyq[idle]); |
| 4616 | goto out_balanced; |
| 4617 | } |
| 4618 | |
Michael Wang | 78feefc | 2012-08-06 16:41:59 +0800 | [diff] [blame] | 4619 | BUG_ON(busiest == env.dst_rq); |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4620 | |
Peter Zijlstra | bd939f4 | 2012-05-02 14:20:37 +0200 | [diff] [blame] | 4621 | schedstat_add(sd, lb_imbalance[idle], env.imbalance); |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4622 | |
| 4623 | ld_moved = 0; |
Srivatsa Vaddagiri | 88b8dac | 2012-06-19 17:43:15 +0530 | [diff] [blame] | 4624 | lb_iterations = 1; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4625 | if (busiest->nr_running > 1) { |
| 4626 | /* |
| 4627 | * Attempt to move tasks. If find_busiest_group has found |
| 4628 | * an imbalance but busiest->nr_running <= 1, the group is |
| 4629 | * still unbalanced. ld_moved simply stays zero, so it is |
| 4630 | * correctly treated as an imbalance. |
| 4631 | */ |
Peter Zijlstra | 8e45cb5 | 2012-02-22 12:47:19 +0100 | [diff] [blame] | 4632 | env.flags |= LBF_ALL_PINNED; |
Peter Zijlstra | c82513e | 2012-04-26 13:12:27 +0200 | [diff] [blame] | 4633 | env.src_cpu = busiest->cpu; |
| 4634 | env.src_rq = busiest; |
| 4635 | env.loop_max = min(sysctl_sched_nr_migrate, busiest->nr_running); |
Peter Zijlstra | 8e45cb5 | 2012-02-22 12:47:19 +0100 | [diff] [blame] | 4636 | |
Peter Zijlstra | a35b646 | 2012-08-08 21:46:40 +0200 | [diff] [blame] | 4637 | update_h_load(env.src_cpu); |
Peter Zijlstra | 5d6523e | 2012-03-10 00:07:36 +0100 | [diff] [blame] | 4638 | more_balance: |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4639 | local_irq_save(flags); |
Michael Wang | 78feefc | 2012-08-06 16:41:59 +0800 | [diff] [blame] | 4640 | double_rq_lock(env.dst_rq, busiest); |
Srivatsa Vaddagiri | 88b8dac | 2012-06-19 17:43:15 +0530 | [diff] [blame] | 4641 | |
| 4642 | /* |
| 4643 | * cur_ld_moved - load moved in current iteration |
| 4644 | * ld_moved - cumulative load moved across iterations |
| 4645 | */ |
| 4646 | cur_ld_moved = move_tasks(&env); |
| 4647 | ld_moved += cur_ld_moved; |
Michael Wang | 78feefc | 2012-08-06 16:41:59 +0800 | [diff] [blame] | 4648 | double_rq_unlock(env.dst_rq, busiest); |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4649 | local_irq_restore(flags); |
| 4650 | |
Peter Zijlstra | 5d6523e | 2012-03-10 00:07:36 +0100 | [diff] [blame] | 4651 | if (env.flags & LBF_NEED_BREAK) { |
| 4652 | env.flags &= ~LBF_NEED_BREAK; |
| 4653 | goto more_balance; |
| 4654 | } |
| 4655 | |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4656 | /* |
| 4657 | * some other cpu did the load balance for us. |
| 4658 | */ |
Srivatsa Vaddagiri | 88b8dac | 2012-06-19 17:43:15 +0530 | [diff] [blame] | 4659 | if (cur_ld_moved && env.dst_cpu != smp_processor_id()) |
| 4660 | resched_cpu(env.dst_cpu); |
| 4661 | |
| 4662 | /* |
| 4663 | * Revisit (affine) tasks on src_cpu that couldn't be moved to |
| 4664 | * us and move them to an alternate dst_cpu in our sched_group |
| 4665 | * where they can run. The upper limit on how many times we |
| 4666 | * iterate on same src_cpu is dependent on number of cpus in our |
| 4667 | * sched_group. |
| 4668 | * |
| 4669 | * This changes load balance semantics a bit on who can move |
| 4670 | * load to a given_cpu. In addition to the given_cpu itself |
| 4671 | * (or a ilb_cpu acting on its behalf where given_cpu is |
| 4672 | * nohz-idle), we now have balance_cpu in a position to move |
| 4673 | * load to given_cpu. In rare situations, this may cause |
| 4674 | * conflicts (balance_cpu and given_cpu/ilb_cpu deciding |
| 4675 | * _independently_ and at _same_ time to move some load to |
| 4676 | * given_cpu) causing exceess load to be moved to given_cpu. |
| 4677 | * This however should not happen so much in practice and |
| 4678 | * moreover subsequent load balance cycles should correct the |
| 4679 | * excess load moved. |
| 4680 | */ |
| 4681 | if ((env.flags & LBF_SOME_PINNED) && env.imbalance > 0 && |
| 4682 | lb_iterations++ < max_lb_iterations) { |
| 4683 | |
Michael Wang | 78feefc | 2012-08-06 16:41:59 +0800 | [diff] [blame] | 4684 | env.dst_rq = cpu_rq(env.new_dst_cpu); |
Srivatsa Vaddagiri | 88b8dac | 2012-06-19 17:43:15 +0530 | [diff] [blame] | 4685 | env.dst_cpu = env.new_dst_cpu; |
| 4686 | env.flags &= ~LBF_SOME_PINNED; |
| 4687 | env.loop = 0; |
| 4688 | env.loop_break = sched_nr_migrate_break; |
| 4689 | /* |
| 4690 | * Go back to "more_balance" rather than "redo" since we |
| 4691 | * need to continue with same src_cpu. |
| 4692 | */ |
| 4693 | goto more_balance; |
| 4694 | } |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4695 | |
| 4696 | /* All tasks on this runqueue were pinned by CPU affinity */ |
Peter Zijlstra | 8e45cb5 | 2012-02-22 12:47:19 +0100 | [diff] [blame] | 4697 | if (unlikely(env.flags & LBF_ALL_PINNED)) { |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4698 | cpumask_clear_cpu(cpu_of(busiest), cpus); |
Prashanth Nageshappa | bbf18b1 | 2012-06-19 17:52:07 +0530 | [diff] [blame] | 4699 | if (!cpumask_empty(cpus)) { |
| 4700 | env.loop = 0; |
| 4701 | env.loop_break = sched_nr_migrate_break; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4702 | goto redo; |
Prashanth Nageshappa | bbf18b1 | 2012-06-19 17:52:07 +0530 | [diff] [blame] | 4703 | } |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4704 | goto out_balanced; |
| 4705 | } |
| 4706 | } |
| 4707 | |
| 4708 | if (!ld_moved) { |
| 4709 | schedstat_inc(sd, lb_failed[idle]); |
Venkatesh Pallipadi | 58b26c4 | 2010-09-10 18:19:17 -0700 | [diff] [blame] | 4710 | /* |
| 4711 | * Increment the failure counter only on periodic balance. |
| 4712 | * We do not want newidle balance, which can be very |
| 4713 | * frequent, pollute the failure counter causing |
| 4714 | * excessive cache_hot migrations and active balances. |
| 4715 | */ |
| 4716 | if (idle != CPU_NEWLY_IDLE) |
| 4717 | sd->nr_balance_failed++; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4718 | |
Peter Zijlstra | bd939f4 | 2012-05-02 14:20:37 +0200 | [diff] [blame] | 4719 | if (need_active_balance(&env)) { |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4720 | raw_spin_lock_irqsave(&busiest->lock, flags); |
| 4721 | |
Tejun Heo | 969c792 | 2010-05-06 18:49:21 +0200 | [diff] [blame] | 4722 | /* don't kick the active_load_balance_cpu_stop, |
| 4723 | * if the curr task on busiest cpu can't be |
| 4724 | * moved to this_cpu |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4725 | */ |
| 4726 | if (!cpumask_test_cpu(this_cpu, |
Peter Zijlstra | fa17b50 | 2011-06-16 12:23:22 +0200 | [diff] [blame] | 4727 | tsk_cpus_allowed(busiest->curr))) { |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4728 | raw_spin_unlock_irqrestore(&busiest->lock, |
| 4729 | flags); |
Peter Zijlstra | 8e45cb5 | 2012-02-22 12:47:19 +0100 | [diff] [blame] | 4730 | env.flags |= LBF_ALL_PINNED; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4731 | goto out_one_pinned; |
| 4732 | } |
| 4733 | |
Tejun Heo | 969c792 | 2010-05-06 18:49:21 +0200 | [diff] [blame] | 4734 | /* |
| 4735 | * ->active_balance synchronizes accesses to |
| 4736 | * ->active_balance_work. Once set, it's cleared |
| 4737 | * only after active load balance is finished. |
| 4738 | */ |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4739 | if (!busiest->active_balance) { |
| 4740 | busiest->active_balance = 1; |
| 4741 | busiest->push_cpu = this_cpu; |
| 4742 | active_balance = 1; |
| 4743 | } |
| 4744 | raw_spin_unlock_irqrestore(&busiest->lock, flags); |
Tejun Heo | 969c792 | 2010-05-06 18:49:21 +0200 | [diff] [blame] | 4745 | |
Peter Zijlstra | bd939f4 | 2012-05-02 14:20:37 +0200 | [diff] [blame] | 4746 | if (active_balance) { |
Tejun Heo | 969c792 | 2010-05-06 18:49:21 +0200 | [diff] [blame] | 4747 | stop_one_cpu_nowait(cpu_of(busiest), |
| 4748 | active_load_balance_cpu_stop, busiest, |
| 4749 | &busiest->active_balance_work); |
Peter Zijlstra | bd939f4 | 2012-05-02 14:20:37 +0200 | [diff] [blame] | 4750 | } |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4751 | |
| 4752 | /* |
| 4753 | * We've kicked active balancing, reset the failure |
| 4754 | * counter. |
| 4755 | */ |
| 4756 | sd->nr_balance_failed = sd->cache_nice_tries+1; |
| 4757 | } |
| 4758 | } else |
| 4759 | sd->nr_balance_failed = 0; |
| 4760 | |
| 4761 | if (likely(!active_balance)) { |
| 4762 | /* We were unbalanced, so reset the balancing interval */ |
| 4763 | sd->balance_interval = sd->min_interval; |
| 4764 | } else { |
| 4765 | /* |
| 4766 | * If we've begun active balancing, start to back off. This |
| 4767 | * case may not be covered by the all_pinned logic if there |
| 4768 | * is only 1 task on the busy runqueue (because we don't call |
| 4769 | * move_tasks). |
| 4770 | */ |
| 4771 | if (sd->balance_interval < sd->max_interval) |
| 4772 | sd->balance_interval *= 2; |
| 4773 | } |
| 4774 | |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4775 | goto out; |
| 4776 | |
| 4777 | out_balanced: |
| 4778 | schedstat_inc(sd, lb_balanced[idle]); |
| 4779 | |
| 4780 | sd->nr_balance_failed = 0; |
| 4781 | |
| 4782 | out_one_pinned: |
| 4783 | /* tune up the balancing interval */ |
Peter Zijlstra | 8e45cb5 | 2012-02-22 12:47:19 +0100 | [diff] [blame] | 4784 | if (((env.flags & LBF_ALL_PINNED) && |
Peter Zijlstra | 5b54b56 | 2011-09-22 15:23:13 +0200 | [diff] [blame] | 4785 | sd->balance_interval < MAX_PINNED_INTERVAL) || |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4786 | (sd->balance_interval < sd->max_interval)) |
| 4787 | sd->balance_interval *= 2; |
| 4788 | |
Venkatesh Pallipadi | 46e49b3 | 2011-02-14 14:38:50 -0800 | [diff] [blame] | 4789 | ld_moved = 0; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4790 | out: |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4791 | return ld_moved; |
| 4792 | } |
| 4793 | |
| 4794 | /* |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4795 | * idle_balance is called by schedule() if this_cpu is about to become |
| 4796 | * idle. Attempts to pull tasks from other CPUs. |
| 4797 | */ |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 4798 | void idle_balance(int this_cpu, struct rq *this_rq) |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4799 | { |
| 4800 | struct sched_domain *sd; |
| 4801 | int pulled_task = 0; |
| 4802 | unsigned long next_balance = jiffies + HZ; |
| 4803 | |
| 4804 | this_rq->idle_stamp = this_rq->clock; |
| 4805 | |
| 4806 | if (this_rq->avg_idle < sysctl_sched_migration_cost) |
| 4807 | return; |
| 4808 | |
Ben Segall | 18bf280 | 2012-10-04 12:51:20 +0200 | [diff] [blame] | 4809 | update_rq_runnable_avg(this_rq, 1); |
| 4810 | |
Peter Zijlstra | f492e12 | 2009-12-23 15:29:42 +0100 | [diff] [blame] | 4811 | /* |
| 4812 | * Drop the rq->lock, but keep IRQ/preempt disabled. |
| 4813 | */ |
| 4814 | raw_spin_unlock(&this_rq->lock); |
| 4815 | |
Paul Turner | c66eaf6 | 2010-11-15 15:47:07 -0800 | [diff] [blame] | 4816 | update_shares(this_cpu); |
Peter Zijlstra | dce840a | 2011-04-07 14:09:50 +0200 | [diff] [blame] | 4817 | rcu_read_lock(); |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4818 | for_each_domain(this_cpu, sd) { |
| 4819 | unsigned long interval; |
Peter Zijlstra | f492e12 | 2009-12-23 15:29:42 +0100 | [diff] [blame] | 4820 | int balance = 1; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4821 | |
| 4822 | if (!(sd->flags & SD_LOAD_BALANCE)) |
| 4823 | continue; |
| 4824 | |
Peter Zijlstra | f492e12 | 2009-12-23 15:29:42 +0100 | [diff] [blame] | 4825 | if (sd->flags & SD_BALANCE_NEWIDLE) { |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4826 | /* If we've pulled tasks over stop searching: */ |
Peter Zijlstra | f492e12 | 2009-12-23 15:29:42 +0100 | [diff] [blame] | 4827 | pulled_task = load_balance(this_cpu, this_rq, |
| 4828 | sd, CPU_NEWLY_IDLE, &balance); |
| 4829 | } |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4830 | |
| 4831 | interval = msecs_to_jiffies(sd->balance_interval); |
| 4832 | if (time_after(next_balance, sd->last_balance + interval)) |
| 4833 | next_balance = sd->last_balance + interval; |
Nikhil Rao | d5ad140 | 2010-11-17 11:42:04 -0800 | [diff] [blame] | 4834 | if (pulled_task) { |
| 4835 | this_rq->idle_stamp = 0; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4836 | break; |
Nikhil Rao | d5ad140 | 2010-11-17 11:42:04 -0800 | [diff] [blame] | 4837 | } |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4838 | } |
Peter Zijlstra | dce840a | 2011-04-07 14:09:50 +0200 | [diff] [blame] | 4839 | rcu_read_unlock(); |
Peter Zijlstra | f492e12 | 2009-12-23 15:29:42 +0100 | [diff] [blame] | 4840 | |
| 4841 | raw_spin_lock(&this_rq->lock); |
| 4842 | |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4843 | if (pulled_task || time_after(jiffies, this_rq->next_balance)) { |
| 4844 | /* |
| 4845 | * We are going idle. next_balance may be set based on |
| 4846 | * a busy processor. So reset next_balance. |
| 4847 | */ |
| 4848 | this_rq->next_balance = next_balance; |
| 4849 | } |
| 4850 | } |
| 4851 | |
| 4852 | /* |
Tejun Heo | 969c792 | 2010-05-06 18:49:21 +0200 | [diff] [blame] | 4853 | * active_load_balance_cpu_stop is run by cpu stopper. It pushes |
| 4854 | * running tasks off the busiest CPU onto idle CPUs. It requires at |
| 4855 | * least 1 task to be running on each physical CPU where possible, and |
| 4856 | * avoids physical / logical imbalances. |
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 | static int active_load_balance_cpu_stop(void *data) |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4859 | { |
Tejun Heo | 969c792 | 2010-05-06 18:49:21 +0200 | [diff] [blame] | 4860 | struct rq *busiest_rq = data; |
| 4861 | int busiest_cpu = cpu_of(busiest_rq); |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4862 | int target_cpu = busiest_rq->push_cpu; |
Tejun Heo | 969c792 | 2010-05-06 18:49:21 +0200 | [diff] [blame] | 4863 | struct rq *target_rq = cpu_rq(target_cpu); |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4864 | struct sched_domain *sd; |
Tejun Heo | 969c792 | 2010-05-06 18:49:21 +0200 | [diff] [blame] | 4865 | |
| 4866 | raw_spin_lock_irq(&busiest_rq->lock); |
| 4867 | |
| 4868 | /* make sure the requested cpu hasn't gone down in the meantime */ |
| 4869 | if (unlikely(busiest_cpu != smp_processor_id() || |
| 4870 | !busiest_rq->active_balance)) |
| 4871 | goto out_unlock; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4872 | |
| 4873 | /* Is there any task to move? */ |
| 4874 | if (busiest_rq->nr_running <= 1) |
Tejun Heo | 969c792 | 2010-05-06 18:49:21 +0200 | [diff] [blame] | 4875 | goto out_unlock; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4876 | |
| 4877 | /* |
| 4878 | * This condition is "impossible", if it occurs |
| 4879 | * we need to fix it. Originally reported by |
| 4880 | * Bjorn Helgaas on a 128-cpu setup. |
| 4881 | */ |
| 4882 | BUG_ON(busiest_rq == target_rq); |
| 4883 | |
| 4884 | /* move a task from busiest_rq to target_rq */ |
| 4885 | double_lock_balance(busiest_rq, target_rq); |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4886 | |
| 4887 | /* Search for an sd spanning us and the target CPU. */ |
Peter Zijlstra | dce840a | 2011-04-07 14:09:50 +0200 | [diff] [blame] | 4888 | rcu_read_lock(); |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4889 | for_each_domain(target_cpu, sd) { |
| 4890 | if ((sd->flags & SD_LOAD_BALANCE) && |
| 4891 | cpumask_test_cpu(busiest_cpu, sched_domain_span(sd))) |
| 4892 | break; |
| 4893 | } |
| 4894 | |
| 4895 | if (likely(sd)) { |
Peter Zijlstra | 8e45cb5 | 2012-02-22 12:47:19 +0100 | [diff] [blame] | 4896 | struct lb_env env = { |
| 4897 | .sd = sd, |
Peter Zijlstra | ddcdf6e | 2012-02-22 19:27:40 +0100 | [diff] [blame] | 4898 | .dst_cpu = target_cpu, |
| 4899 | .dst_rq = target_rq, |
| 4900 | .src_cpu = busiest_rq->cpu, |
| 4901 | .src_rq = busiest_rq, |
Peter Zijlstra | 8e45cb5 | 2012-02-22 12:47:19 +0100 | [diff] [blame] | 4902 | .idle = CPU_IDLE, |
| 4903 | }; |
| 4904 | |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4905 | schedstat_inc(sd, alb_count); |
| 4906 | |
Peter Zijlstra | 8e45cb5 | 2012-02-22 12:47:19 +0100 | [diff] [blame] | 4907 | if (move_one_task(&env)) |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4908 | schedstat_inc(sd, alb_pushed); |
| 4909 | else |
| 4910 | schedstat_inc(sd, alb_failed); |
| 4911 | } |
Peter Zijlstra | dce840a | 2011-04-07 14:09:50 +0200 | [diff] [blame] | 4912 | rcu_read_unlock(); |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4913 | double_unlock_balance(busiest_rq, target_rq); |
Tejun Heo | 969c792 | 2010-05-06 18:49:21 +0200 | [diff] [blame] | 4914 | out_unlock: |
| 4915 | busiest_rq->active_balance = 0; |
| 4916 | raw_spin_unlock_irq(&busiest_rq->lock); |
| 4917 | return 0; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4918 | } |
| 4919 | |
| 4920 | #ifdef CONFIG_NO_HZ |
Venkatesh Pallipadi | 83cd4fe | 2010-05-21 17:09:41 -0700 | [diff] [blame] | 4921 | /* |
| 4922 | * idle load balancing details |
Venkatesh Pallipadi | 83cd4fe | 2010-05-21 17:09:41 -0700 | [diff] [blame] | 4923 | * - When one of the busy CPUs notice that there may be an idle rebalancing |
| 4924 | * needed, they will kick the idle load balancer, which then does idle |
| 4925 | * load balancing for all the idle CPUs. |
| 4926 | */ |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4927 | static struct { |
Venkatesh Pallipadi | 83cd4fe | 2010-05-21 17:09:41 -0700 | [diff] [blame] | 4928 | cpumask_var_t idle_cpus_mask; |
Suresh Siddha | 0b005cf | 2011-12-01 17:07:34 -0800 | [diff] [blame] | 4929 | atomic_t nr_cpus; |
Venkatesh Pallipadi | 83cd4fe | 2010-05-21 17:09:41 -0700 | [diff] [blame] | 4930 | unsigned long next_balance; /* in jiffy units */ |
| 4931 | } nohz ____cacheline_aligned; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4932 | |
Peter Zijlstra | 8e7fbcb | 2012-01-09 11:28:35 +0100 | [diff] [blame] | 4933 | static inline int find_new_ilb(int call_cpu) |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4934 | { |
Suresh Siddha | 0b005cf | 2011-12-01 17:07:34 -0800 | [diff] [blame] | 4935 | int ilb = cpumask_first(nohz.idle_cpus_mask); |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4936 | |
Suresh Siddha | 786d6dc7 | 2011-12-01 17:07:35 -0800 | [diff] [blame] | 4937 | if (ilb < nr_cpu_ids && idle_cpu(ilb)) |
| 4938 | return ilb; |
| 4939 | |
| 4940 | return nr_cpu_ids; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4941 | } |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 4942 | |
| 4943 | /* |
Venkatesh Pallipadi | 83cd4fe | 2010-05-21 17:09:41 -0700 | [diff] [blame] | 4944 | * Kick a CPU to do the nohz balancing, if it is time for it. We pick the |
| 4945 | * nohz_load_balancer CPU (if there is one) otherwise fallback to any idle |
| 4946 | * CPU (if there is one). |
| 4947 | */ |
| 4948 | static void nohz_balancer_kick(int cpu) |
| 4949 | { |
| 4950 | int ilb_cpu; |
| 4951 | |
| 4952 | nohz.next_balance++; |
| 4953 | |
Suresh Siddha | 0b005cf | 2011-12-01 17:07:34 -0800 | [diff] [blame] | 4954 | ilb_cpu = find_new_ilb(cpu); |
Venkatesh Pallipadi | 83cd4fe | 2010-05-21 17:09:41 -0700 | [diff] [blame] | 4955 | |
Suresh Siddha | 0b005cf | 2011-12-01 17:07:34 -0800 | [diff] [blame] | 4956 | if (ilb_cpu >= nr_cpu_ids) |
| 4957 | return; |
Venkatesh Pallipadi | 83cd4fe | 2010-05-21 17:09:41 -0700 | [diff] [blame] | 4958 | |
Suresh Siddha | cd490c5 | 2011-12-06 11:26:34 -0800 | [diff] [blame] | 4959 | if (test_and_set_bit(NOHZ_BALANCE_KICK, nohz_flags(ilb_cpu))) |
Suresh Siddha | 1c792db | 2011-12-01 17:07:32 -0800 | [diff] [blame] | 4960 | return; |
| 4961 | /* |
| 4962 | * Use smp_send_reschedule() instead of resched_cpu(). |
| 4963 | * This way we generate a sched IPI on the target cpu which |
| 4964 | * is idle. And the softirq performing nohz idle load balance |
| 4965 | * will be run before returning from the IPI. |
| 4966 | */ |
| 4967 | smp_send_reschedule(ilb_cpu); |
Venkatesh Pallipadi | 83cd4fe | 2010-05-21 17:09:41 -0700 | [diff] [blame] | 4968 | return; |
| 4969 | } |
| 4970 | |
Alex Shi | c1cc017 | 2012-09-10 15:10:58 +0800 | [diff] [blame] | 4971 | static inline void nohz_balance_exit_idle(int cpu) |
Suresh Siddha | 7132596 | 2012-01-19 18:28:57 -0800 | [diff] [blame] | 4972 | { |
| 4973 | if (unlikely(test_bit(NOHZ_TICK_STOPPED, nohz_flags(cpu)))) { |
| 4974 | cpumask_clear_cpu(cpu, nohz.idle_cpus_mask); |
| 4975 | atomic_dec(&nohz.nr_cpus); |
| 4976 | clear_bit(NOHZ_TICK_STOPPED, nohz_flags(cpu)); |
| 4977 | } |
| 4978 | } |
| 4979 | |
Suresh Siddha | 69e1e81 | 2011-12-01 17:07:33 -0800 | [diff] [blame] | 4980 | static inline void set_cpu_sd_state_busy(void) |
| 4981 | { |
| 4982 | struct sched_domain *sd; |
| 4983 | int cpu = smp_processor_id(); |
| 4984 | |
| 4985 | if (!test_bit(NOHZ_IDLE, nohz_flags(cpu))) |
| 4986 | return; |
| 4987 | clear_bit(NOHZ_IDLE, nohz_flags(cpu)); |
| 4988 | |
| 4989 | rcu_read_lock(); |
| 4990 | for_each_domain(cpu, sd) |
| 4991 | atomic_inc(&sd->groups->sgp->nr_busy_cpus); |
| 4992 | rcu_read_unlock(); |
| 4993 | } |
| 4994 | |
| 4995 | void set_cpu_sd_state_idle(void) |
| 4996 | { |
| 4997 | struct sched_domain *sd; |
| 4998 | int cpu = smp_processor_id(); |
| 4999 | |
| 5000 | if (test_bit(NOHZ_IDLE, nohz_flags(cpu))) |
| 5001 | return; |
| 5002 | set_bit(NOHZ_IDLE, nohz_flags(cpu)); |
| 5003 | |
| 5004 | rcu_read_lock(); |
| 5005 | for_each_domain(cpu, sd) |
| 5006 | atomic_dec(&sd->groups->sgp->nr_busy_cpus); |
| 5007 | rcu_read_unlock(); |
| 5008 | } |
| 5009 | |
Venkatesh Pallipadi | 83cd4fe | 2010-05-21 17:09:41 -0700 | [diff] [blame] | 5010 | /* |
Alex Shi | c1cc017 | 2012-09-10 15:10:58 +0800 | [diff] [blame] | 5011 | * 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] | 5012 | * 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] | 5013 | */ |
Alex Shi | c1cc017 | 2012-09-10 15:10:58 +0800 | [diff] [blame] | 5014 | void nohz_balance_enter_idle(int cpu) |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 5015 | { |
Suresh Siddha | 7132596 | 2012-01-19 18:28:57 -0800 | [diff] [blame] | 5016 | /* |
| 5017 | * If this cpu is going down, then nothing needs to be done. |
| 5018 | */ |
| 5019 | if (!cpu_active(cpu)) |
| 5020 | return; |
| 5021 | |
Alex Shi | c1cc017 | 2012-09-10 15:10:58 +0800 | [diff] [blame] | 5022 | if (test_bit(NOHZ_TICK_STOPPED, nohz_flags(cpu))) |
| 5023 | return; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 5024 | |
Alex Shi | c1cc017 | 2012-09-10 15:10:58 +0800 | [diff] [blame] | 5025 | cpumask_set_cpu(cpu, nohz.idle_cpus_mask); |
| 5026 | atomic_inc(&nohz.nr_cpus); |
| 5027 | set_bit(NOHZ_TICK_STOPPED, nohz_flags(cpu)); |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 5028 | } |
Suresh Siddha | 7132596 | 2012-01-19 18:28:57 -0800 | [diff] [blame] | 5029 | |
| 5030 | static int __cpuinit sched_ilb_notifier(struct notifier_block *nfb, |
| 5031 | unsigned long action, void *hcpu) |
| 5032 | { |
| 5033 | switch (action & ~CPU_TASKS_FROZEN) { |
| 5034 | case CPU_DYING: |
Alex Shi | c1cc017 | 2012-09-10 15:10:58 +0800 | [diff] [blame] | 5035 | nohz_balance_exit_idle(smp_processor_id()); |
Suresh Siddha | 7132596 | 2012-01-19 18:28:57 -0800 | [diff] [blame] | 5036 | return NOTIFY_OK; |
| 5037 | default: |
| 5038 | return NOTIFY_DONE; |
| 5039 | } |
| 5040 | } |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 5041 | #endif |
| 5042 | |
| 5043 | static DEFINE_SPINLOCK(balancing); |
| 5044 | |
Peter Zijlstra | 49c022e | 2011-04-05 10:14:25 +0200 | [diff] [blame] | 5045 | /* |
| 5046 | * Scale the max load_balance interval with the number of CPUs in the system. |
| 5047 | * This trades load-balance latency on larger machines for less cross talk. |
| 5048 | */ |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 5049 | void update_max_interval(void) |
Peter Zijlstra | 49c022e | 2011-04-05 10:14:25 +0200 | [diff] [blame] | 5050 | { |
| 5051 | max_load_balance_interval = HZ*num_online_cpus()/10; |
| 5052 | } |
| 5053 | |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 5054 | /* |
| 5055 | * It checks each scheduling domain to see if it is due to be balanced, |
| 5056 | * and initiates a balancing operation if so. |
| 5057 | * |
| 5058 | * Balancing parameters are set up in arch_init_sched_domains. |
| 5059 | */ |
| 5060 | static void rebalance_domains(int cpu, enum cpu_idle_type idle) |
| 5061 | { |
| 5062 | int balance = 1; |
| 5063 | struct rq *rq = cpu_rq(cpu); |
| 5064 | unsigned long interval; |
Peter Zijlstra | 04f733b | 2012-05-11 00:12:02 +0200 | [diff] [blame] | 5065 | struct sched_domain *sd; |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 5066 | /* Earliest time when we have to do rebalance again */ |
| 5067 | unsigned long next_balance = jiffies + 60*HZ; |
| 5068 | int update_next_balance = 0; |
| 5069 | int need_serialize; |
| 5070 | |
Peter Zijlstra | 2069dd7 | 2010-11-15 15:47:00 -0800 | [diff] [blame] | 5071 | update_shares(cpu); |
| 5072 | |
Peter Zijlstra | dce840a | 2011-04-07 14:09:50 +0200 | [diff] [blame] | 5073 | rcu_read_lock(); |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 5074 | for_each_domain(cpu, sd) { |
| 5075 | if (!(sd->flags & SD_LOAD_BALANCE)) |
| 5076 | continue; |
| 5077 | |
| 5078 | interval = sd->balance_interval; |
| 5079 | if (idle != CPU_IDLE) |
| 5080 | interval *= sd->busy_factor; |
| 5081 | |
| 5082 | /* scale ms to jiffies */ |
| 5083 | interval = msecs_to_jiffies(interval); |
Peter Zijlstra | 49c022e | 2011-04-05 10:14:25 +0200 | [diff] [blame] | 5084 | interval = clamp(interval, 1UL, max_load_balance_interval); |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 5085 | |
| 5086 | need_serialize = sd->flags & SD_SERIALIZE; |
| 5087 | |
| 5088 | if (need_serialize) { |
| 5089 | if (!spin_trylock(&balancing)) |
| 5090 | goto out; |
| 5091 | } |
| 5092 | |
| 5093 | if (time_after_eq(jiffies, sd->last_balance + interval)) { |
| 5094 | if (load_balance(cpu, rq, sd, idle, &balance)) { |
| 5095 | /* |
| 5096 | * We've pulled tasks over so either we're no |
Peter Zijlstra | c186faf | 2011-02-21 18:52:53 +0100 | [diff] [blame] | 5097 | * longer idle. |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 5098 | */ |
| 5099 | idle = CPU_NOT_IDLE; |
| 5100 | } |
| 5101 | sd->last_balance = jiffies; |
| 5102 | } |
| 5103 | if (need_serialize) |
| 5104 | spin_unlock(&balancing); |
| 5105 | out: |
| 5106 | if (time_after(next_balance, sd->last_balance + interval)) { |
| 5107 | next_balance = sd->last_balance + interval; |
| 5108 | update_next_balance = 1; |
| 5109 | } |
| 5110 | |
| 5111 | /* |
| 5112 | * Stop the load balance at this level. There is another |
| 5113 | * CPU in our sched group which is doing load balancing more |
| 5114 | * actively. |
| 5115 | */ |
| 5116 | if (!balance) |
| 5117 | break; |
| 5118 | } |
Peter Zijlstra | dce840a | 2011-04-07 14:09:50 +0200 | [diff] [blame] | 5119 | rcu_read_unlock(); |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 5120 | |
| 5121 | /* |
| 5122 | * next_balance will be updated only when there is a need. |
| 5123 | * When the cpu is attached to null domain for ex, it will not be |
| 5124 | * updated. |
| 5125 | */ |
| 5126 | if (likely(update_next_balance)) |
| 5127 | rq->next_balance = next_balance; |
| 5128 | } |
| 5129 | |
Venkatesh Pallipadi | 83cd4fe | 2010-05-21 17:09:41 -0700 | [diff] [blame] | 5130 | #ifdef CONFIG_NO_HZ |
| 5131 | /* |
| 5132 | * In CONFIG_NO_HZ case, the idle balance kickee will do the |
| 5133 | * rebalancing for all the cpus for whom scheduler ticks are stopped. |
| 5134 | */ |
| 5135 | static void nohz_idle_balance(int this_cpu, enum cpu_idle_type idle) |
| 5136 | { |
| 5137 | struct rq *this_rq = cpu_rq(this_cpu); |
| 5138 | struct rq *rq; |
| 5139 | int balance_cpu; |
| 5140 | |
Suresh Siddha | 1c792db | 2011-12-01 17:07:32 -0800 | [diff] [blame] | 5141 | if (idle != CPU_IDLE || |
| 5142 | !test_bit(NOHZ_BALANCE_KICK, nohz_flags(this_cpu))) |
| 5143 | goto end; |
Venkatesh Pallipadi | 83cd4fe | 2010-05-21 17:09:41 -0700 | [diff] [blame] | 5144 | |
| 5145 | for_each_cpu(balance_cpu, nohz.idle_cpus_mask) { |
Suresh Siddha | 8a6d42d | 2011-12-06 11:19:37 -0800 | [diff] [blame] | 5146 | if (balance_cpu == this_cpu || !idle_cpu(balance_cpu)) |
Venkatesh Pallipadi | 83cd4fe | 2010-05-21 17:09:41 -0700 | [diff] [blame] | 5147 | continue; |
| 5148 | |
| 5149 | /* |
| 5150 | * If this cpu gets work to do, stop the load balancing |
| 5151 | * work being done for other cpus. Next load |
| 5152 | * balancing owner will pick it up. |
| 5153 | */ |
Suresh Siddha | 1c792db | 2011-12-01 17:07:32 -0800 | [diff] [blame] | 5154 | if (need_resched()) |
Venkatesh Pallipadi | 83cd4fe | 2010-05-21 17:09:41 -0700 | [diff] [blame] | 5155 | break; |
Venkatesh Pallipadi | 83cd4fe | 2010-05-21 17:09:41 -0700 | [diff] [blame] | 5156 | |
Vincent Guittot | 5ed4f1d | 2012-09-13 06:11:26 +0200 | [diff] [blame] | 5157 | rq = cpu_rq(balance_cpu); |
| 5158 | |
| 5159 | raw_spin_lock_irq(&rq->lock); |
| 5160 | update_rq_clock(rq); |
| 5161 | update_idle_cpu_load(rq); |
| 5162 | raw_spin_unlock_irq(&rq->lock); |
Venkatesh Pallipadi | 83cd4fe | 2010-05-21 17:09:41 -0700 | [diff] [blame] | 5163 | |
| 5164 | rebalance_domains(balance_cpu, CPU_IDLE); |
| 5165 | |
Venkatesh Pallipadi | 83cd4fe | 2010-05-21 17:09:41 -0700 | [diff] [blame] | 5166 | if (time_after(this_rq->next_balance, rq->next_balance)) |
| 5167 | this_rq->next_balance = rq->next_balance; |
| 5168 | } |
| 5169 | nohz.next_balance = this_rq->next_balance; |
Suresh Siddha | 1c792db | 2011-12-01 17:07:32 -0800 | [diff] [blame] | 5170 | end: |
| 5171 | clear_bit(NOHZ_BALANCE_KICK, nohz_flags(this_cpu)); |
Venkatesh Pallipadi | 83cd4fe | 2010-05-21 17:09:41 -0700 | [diff] [blame] | 5172 | } |
| 5173 | |
| 5174 | /* |
Suresh Siddha | 0b005cf | 2011-12-01 17:07:34 -0800 | [diff] [blame] | 5175 | * Current heuristic for kicking the idle load balancer in the presence |
| 5176 | * of an idle cpu is the system. |
| 5177 | * - This rq has more than one task. |
| 5178 | * - At any scheduler domain level, this cpu's scheduler group has multiple |
| 5179 | * busy cpu's exceeding the group's power. |
| 5180 | * - For SD_ASYM_PACKING, if the lower numbered cpu's in the scheduler |
| 5181 | * domain span are idle. |
Venkatesh Pallipadi | 83cd4fe | 2010-05-21 17:09:41 -0700 | [diff] [blame] | 5182 | */ |
| 5183 | static inline int nohz_kick_needed(struct rq *rq, int cpu) |
| 5184 | { |
| 5185 | unsigned long now = jiffies; |
Suresh Siddha | 0b005cf | 2011-12-01 17:07:34 -0800 | [diff] [blame] | 5186 | struct sched_domain *sd; |
Venkatesh Pallipadi | 83cd4fe | 2010-05-21 17:09:41 -0700 | [diff] [blame] | 5187 | |
Suresh Siddha | 1c792db | 2011-12-01 17:07:32 -0800 | [diff] [blame] | 5188 | if (unlikely(idle_cpu(cpu))) |
Venkatesh Pallipadi | 83cd4fe | 2010-05-21 17:09:41 -0700 | [diff] [blame] | 5189 | return 0; |
| 5190 | |
Suresh Siddha | 1c792db | 2011-12-01 17:07:32 -0800 | [diff] [blame] | 5191 | /* |
| 5192 | * We may be recently in ticked or tickless idle mode. At the first |
| 5193 | * busy tick after returning from idle, we will update the busy stats. |
| 5194 | */ |
Suresh Siddha | 69e1e81 | 2011-12-01 17:07:33 -0800 | [diff] [blame] | 5195 | set_cpu_sd_state_busy(); |
Alex Shi | c1cc017 | 2012-09-10 15:10:58 +0800 | [diff] [blame] | 5196 | nohz_balance_exit_idle(cpu); |
Suresh Siddha | 0b005cf | 2011-12-01 17:07:34 -0800 | [diff] [blame] | 5197 | |
| 5198 | /* |
| 5199 | * None are in tickless mode and hence no need for NOHZ idle load |
| 5200 | * balancing. |
| 5201 | */ |
| 5202 | if (likely(!atomic_read(&nohz.nr_cpus))) |
| 5203 | return 0; |
Suresh Siddha | 1c792db | 2011-12-01 17:07:32 -0800 | [diff] [blame] | 5204 | |
| 5205 | if (time_before(now, nohz.next_balance)) |
Venkatesh Pallipadi | 83cd4fe | 2010-05-21 17:09:41 -0700 | [diff] [blame] | 5206 | return 0; |
| 5207 | |
Suresh Siddha | 0b005cf | 2011-12-01 17:07:34 -0800 | [diff] [blame] | 5208 | if (rq->nr_running >= 2) |
| 5209 | goto need_kick; |
Venkatesh Pallipadi | 83cd4fe | 2010-05-21 17:09:41 -0700 | [diff] [blame] | 5210 | |
Peter Zijlstra | 067491b | 2011-12-07 14:32:08 +0100 | [diff] [blame] | 5211 | rcu_read_lock(); |
Suresh Siddha | 0b005cf | 2011-12-01 17:07:34 -0800 | [diff] [blame] | 5212 | for_each_domain(cpu, sd) { |
| 5213 | struct sched_group *sg = sd->groups; |
| 5214 | struct sched_group_power *sgp = sg->sgp; |
| 5215 | int nr_busy = atomic_read(&sgp->nr_busy_cpus); |
Venkatesh Pallipadi | 83cd4fe | 2010-05-21 17:09:41 -0700 | [diff] [blame] | 5216 | |
Suresh Siddha | 0b005cf | 2011-12-01 17:07:34 -0800 | [diff] [blame] | 5217 | if (sd->flags & SD_SHARE_PKG_RESOURCES && nr_busy > 1) |
Peter Zijlstra | 067491b | 2011-12-07 14:32:08 +0100 | [diff] [blame] | 5218 | goto need_kick_unlock; |
Suresh Siddha | 0b005cf | 2011-12-01 17:07:34 -0800 | [diff] [blame] | 5219 | |
| 5220 | if (sd->flags & SD_ASYM_PACKING && nr_busy != sg->group_weight |
| 5221 | && (cpumask_first_and(nohz.idle_cpus_mask, |
| 5222 | sched_domain_span(sd)) < cpu)) |
Peter Zijlstra | 067491b | 2011-12-07 14:32:08 +0100 | [diff] [blame] | 5223 | goto need_kick_unlock; |
Suresh Siddha | 0b005cf | 2011-12-01 17:07:34 -0800 | [diff] [blame] | 5224 | |
| 5225 | if (!(sd->flags & (SD_SHARE_PKG_RESOURCES | SD_ASYM_PACKING))) |
| 5226 | break; |
Venkatesh Pallipadi | 83cd4fe | 2010-05-21 17:09:41 -0700 | [diff] [blame] | 5227 | } |
Peter Zijlstra | 067491b | 2011-12-07 14:32:08 +0100 | [diff] [blame] | 5228 | rcu_read_unlock(); |
Venkatesh Pallipadi | 83cd4fe | 2010-05-21 17:09:41 -0700 | [diff] [blame] | 5229 | return 0; |
Peter Zijlstra | 067491b | 2011-12-07 14:32:08 +0100 | [diff] [blame] | 5230 | |
| 5231 | need_kick_unlock: |
| 5232 | rcu_read_unlock(); |
Suresh Siddha | 0b005cf | 2011-12-01 17:07:34 -0800 | [diff] [blame] | 5233 | need_kick: |
| 5234 | return 1; |
Venkatesh Pallipadi | 83cd4fe | 2010-05-21 17:09:41 -0700 | [diff] [blame] | 5235 | } |
| 5236 | #else |
| 5237 | static void nohz_idle_balance(int this_cpu, enum cpu_idle_type idle) { } |
| 5238 | #endif |
| 5239 | |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 5240 | /* |
| 5241 | * run_rebalance_domains is triggered when needed from the scheduler tick. |
Venkatesh Pallipadi | 83cd4fe | 2010-05-21 17:09:41 -0700 | [diff] [blame] | 5242 | * Also triggered for nohz idle balancing (with nohz_balancing_kick set). |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 5243 | */ |
| 5244 | static void run_rebalance_domains(struct softirq_action *h) |
| 5245 | { |
| 5246 | int this_cpu = smp_processor_id(); |
| 5247 | struct rq *this_rq = cpu_rq(this_cpu); |
Suresh Siddha | 6eb57e0 | 2011-10-03 15:09:01 -0700 | [diff] [blame] | 5248 | enum cpu_idle_type idle = this_rq->idle_balance ? |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 5249 | CPU_IDLE : CPU_NOT_IDLE; |
| 5250 | |
| 5251 | rebalance_domains(this_cpu, idle); |
| 5252 | |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 5253 | /* |
Venkatesh Pallipadi | 83cd4fe | 2010-05-21 17:09:41 -0700 | [diff] [blame] | 5254 | * If this cpu has a pending nohz_balance_kick, then do the |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 5255 | * balancing on behalf of the other idle cpus whose ticks are |
| 5256 | * stopped. |
| 5257 | */ |
Venkatesh Pallipadi | 83cd4fe | 2010-05-21 17:09:41 -0700 | [diff] [blame] | 5258 | nohz_idle_balance(this_cpu, idle); |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 5259 | } |
| 5260 | |
| 5261 | static inline int on_null_domain(int cpu) |
| 5262 | { |
Paul E. McKenney | 90a6501 | 2010-02-28 08:32:18 -0800 | [diff] [blame] | 5263 | return !rcu_dereference_sched(cpu_rq(cpu)->sd); |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 5264 | } |
| 5265 | |
| 5266 | /* |
| 5267 | * 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] | 5268 | */ |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 5269 | void trigger_load_balance(struct rq *rq, int cpu) |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 5270 | { |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 5271 | /* Don't need to rebalance while attached to NULL domain */ |
| 5272 | if (time_after_eq(jiffies, rq->next_balance) && |
| 5273 | likely(!on_null_domain(cpu))) |
| 5274 | raise_softirq(SCHED_SOFTIRQ); |
Venkatesh Pallipadi | 83cd4fe | 2010-05-21 17:09:41 -0700 | [diff] [blame] | 5275 | #ifdef CONFIG_NO_HZ |
Suresh Siddha | 1c792db | 2011-12-01 17:07:32 -0800 | [diff] [blame] | 5276 | if (nohz_kick_needed(rq, cpu) && likely(!on_null_domain(cpu))) |
Venkatesh Pallipadi | 83cd4fe | 2010-05-21 17:09:41 -0700 | [diff] [blame] | 5277 | nohz_balancer_kick(cpu); |
| 5278 | #endif |
Peter Zijlstra | 1e3c88b | 2009-12-17 17:00:43 +0100 | [diff] [blame] | 5279 | } |
| 5280 | |
Christian Ehrhardt | 0bcdcf2 | 2009-11-30 12:16:46 +0100 | [diff] [blame] | 5281 | static void rq_online_fair(struct rq *rq) |
| 5282 | { |
| 5283 | update_sysctl(); |
| 5284 | } |
| 5285 | |
| 5286 | static void rq_offline_fair(struct rq *rq) |
| 5287 | { |
| 5288 | update_sysctl(); |
Peter Boonstoppel | a4c96ae | 2012-08-09 15:34:47 -0700 | [diff] [blame] | 5289 | |
| 5290 | /* Ensure any throttled groups are reachable by pick_next_task */ |
| 5291 | unthrottle_offline_cfs_rqs(rq); |
Christian Ehrhardt | 0bcdcf2 | 2009-11-30 12:16:46 +0100 | [diff] [blame] | 5292 | } |
| 5293 | |
Dhaval Giani | 55e12e5 | 2008-06-24 23:39:43 +0530 | [diff] [blame] | 5294 | #endif /* CONFIG_SMP */ |
Peter Williams | e1d1484 | 2007-10-24 18:23:51 +0200 | [diff] [blame] | 5295 | |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 5296 | /* |
| 5297 | * scheduler tick hitting a task of our scheduling class: |
| 5298 | */ |
Peter Zijlstra | 8f4d37e | 2008-01-25 21:08:29 +0100 | [diff] [blame] | 5299 | 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] | 5300 | { |
| 5301 | struct cfs_rq *cfs_rq; |
| 5302 | struct sched_entity *se = &curr->se; |
| 5303 | |
| 5304 | for_each_sched_entity(se) { |
| 5305 | cfs_rq = cfs_rq_of(se); |
Peter Zijlstra | 8f4d37e | 2008-01-25 21:08:29 +0100 | [diff] [blame] | 5306 | entity_tick(cfs_rq, se, queued); |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 5307 | } |
Ben Segall | 18bf280 | 2012-10-04 12:51:20 +0200 | [diff] [blame] | 5308 | |
| 5309 | update_rq_runnable_avg(rq, 1); |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 5310 | } |
| 5311 | |
| 5312 | /* |
Peter Zijlstra | cd29fe6 | 2009-11-27 17:32:46 +0100 | [diff] [blame] | 5313 | * called on fork with the child task as argument from the parent's context |
| 5314 | * - child not yet on the tasklist |
| 5315 | * - preemption disabled |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 5316 | */ |
Peter Zijlstra | cd29fe6 | 2009-11-27 17:32:46 +0100 | [diff] [blame] | 5317 | static void task_fork_fair(struct task_struct *p) |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 5318 | { |
Daisuke Nishimura | 4fc420c | 2011-12-15 14:36:55 +0900 | [diff] [blame] | 5319 | struct cfs_rq *cfs_rq; |
| 5320 | struct sched_entity *se = &p->se, *curr; |
Ingo Molnar | 00bf7bf | 2007-10-15 17:00:14 +0200 | [diff] [blame] | 5321 | int this_cpu = smp_processor_id(); |
Peter Zijlstra | cd29fe6 | 2009-11-27 17:32:46 +0100 | [diff] [blame] | 5322 | struct rq *rq = this_rq(); |
| 5323 | unsigned long flags; |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 5324 | |
Thomas Gleixner | 05fa785 | 2009-11-17 14:28:38 +0100 | [diff] [blame] | 5325 | raw_spin_lock_irqsave(&rq->lock, flags); |
Peter Zijlstra | cd29fe6 | 2009-11-27 17:32:46 +0100 | [diff] [blame] | 5326 | |
Peter Zijlstra | 861d034 | 2010-08-19 13:31:43 +0200 | [diff] [blame] | 5327 | update_rq_clock(rq); |
| 5328 | |
Daisuke Nishimura | 4fc420c | 2011-12-15 14:36:55 +0900 | [diff] [blame] | 5329 | cfs_rq = task_cfs_rq(current); |
| 5330 | curr = cfs_rq->curr; |
| 5331 | |
Paul E. McKenney | b0a0f66 | 2010-10-06 17:32:51 -0700 | [diff] [blame] | 5332 | if (unlikely(task_cpu(p) != this_cpu)) { |
| 5333 | rcu_read_lock(); |
Peter Zijlstra | cd29fe6 | 2009-11-27 17:32:46 +0100 | [diff] [blame] | 5334 | __set_task_cpu(p, this_cpu); |
Paul E. McKenney | b0a0f66 | 2010-10-06 17:32:51 -0700 | [diff] [blame] | 5335 | rcu_read_unlock(); |
| 5336 | } |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 5337 | |
Ting Yang | 7109c44 | 2007-08-28 12:53:24 +0200 | [diff] [blame] | 5338 | update_curr(cfs_rq); |
Peter Zijlstra | cd29fe6 | 2009-11-27 17:32:46 +0100 | [diff] [blame] | 5339 | |
Mike Galbraith | b5d9d73 | 2009-09-08 11:12:28 +0200 | [diff] [blame] | 5340 | if (curr) |
| 5341 | se->vruntime = curr->vruntime; |
Peter Zijlstra | aeb73b0 | 2007-10-15 17:00:05 +0200 | [diff] [blame] | 5342 | place_entity(cfs_rq, se, 1); |
Peter Zijlstra | 4d78e7b | 2007-10-15 17:00:04 +0200 | [diff] [blame] | 5343 | |
Peter Zijlstra | cd29fe6 | 2009-11-27 17:32:46 +0100 | [diff] [blame] | 5344 | if (sysctl_sched_child_runs_first && curr && entity_before(curr, se)) { |
Dmitry Adamushko | 87fefa3 | 2007-10-15 17:00:08 +0200 | [diff] [blame] | 5345 | /* |
Ingo Molnar | edcb60a | 2007-10-15 17:00:08 +0200 | [diff] [blame] | 5346 | * Upon rescheduling, sched_class::put_prev_task() will place |
| 5347 | * 'current' within the tree based on its new key value. |
| 5348 | */ |
Peter Zijlstra | 4d78e7b | 2007-10-15 17:00:04 +0200 | [diff] [blame] | 5349 | swap(curr->vruntime, se->vruntime); |
Bharata B Rao | aec0a51 | 2008-08-28 14:42:49 +0530 | [diff] [blame] | 5350 | resched_task(rq->curr); |
Peter Zijlstra | 4d78e7b | 2007-10-15 17:00:04 +0200 | [diff] [blame] | 5351 | } |
| 5352 | |
Peter Zijlstra | 88ec22d | 2009-12-16 18:04:41 +0100 | [diff] [blame] | 5353 | se->vruntime -= cfs_rq->min_vruntime; |
| 5354 | |
Thomas Gleixner | 05fa785 | 2009-11-17 14:28:38 +0100 | [diff] [blame] | 5355 | raw_spin_unlock_irqrestore(&rq->lock, flags); |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 5356 | } |
| 5357 | |
Steven Rostedt | cb46984 | 2008-01-25 21:08:22 +0100 | [diff] [blame] | 5358 | /* |
| 5359 | * Priority of the task has changed. Check to see if we preempt |
| 5360 | * the current task. |
| 5361 | */ |
Peter Zijlstra | da7a735 | 2011-01-17 17:03:27 +0100 | [diff] [blame] | 5362 | static void |
| 5363 | prio_changed_fair(struct rq *rq, struct task_struct *p, int oldprio) |
Steven Rostedt | cb46984 | 2008-01-25 21:08:22 +0100 | [diff] [blame] | 5364 | { |
Peter Zijlstra | da7a735 | 2011-01-17 17:03:27 +0100 | [diff] [blame] | 5365 | if (!p->se.on_rq) |
| 5366 | return; |
| 5367 | |
Steven Rostedt | cb46984 | 2008-01-25 21:08:22 +0100 | [diff] [blame] | 5368 | /* |
| 5369 | * Reschedule if we are currently running on this runqueue and |
| 5370 | * our priority decreased, or if we are not currently running on |
| 5371 | * this runqueue and our priority is higher than the current's |
| 5372 | */ |
Peter Zijlstra | da7a735 | 2011-01-17 17:03:27 +0100 | [diff] [blame] | 5373 | if (rq->curr == p) { |
Steven Rostedt | cb46984 | 2008-01-25 21:08:22 +0100 | [diff] [blame] | 5374 | if (p->prio > oldprio) |
| 5375 | resched_task(rq->curr); |
| 5376 | } else |
Peter Zijlstra | 15afe09 | 2008-09-20 23:38:02 +0200 | [diff] [blame] | 5377 | check_preempt_curr(rq, p, 0); |
Steven Rostedt | cb46984 | 2008-01-25 21:08:22 +0100 | [diff] [blame] | 5378 | } |
| 5379 | |
Peter Zijlstra | da7a735 | 2011-01-17 17:03:27 +0100 | [diff] [blame] | 5380 | static void switched_from_fair(struct rq *rq, struct task_struct *p) |
| 5381 | { |
| 5382 | struct sched_entity *se = &p->se; |
| 5383 | struct cfs_rq *cfs_rq = cfs_rq_of(se); |
| 5384 | |
| 5385 | /* |
| 5386 | * Ensure the task's vruntime is normalized, so that when its |
| 5387 | * switched back to the fair class the enqueue_entity(.flags=0) will |
| 5388 | * do the right thing. |
| 5389 | * |
| 5390 | * If it was on_rq, then the dequeue_entity(.flags=0) will already |
| 5391 | * have normalized the vruntime, if it was !on_rq, then only when |
| 5392 | * the task is sleeping will it still have non-normalized vruntime. |
| 5393 | */ |
| 5394 | if (!se->on_rq && p->state != TASK_RUNNING) { |
| 5395 | /* |
| 5396 | * Fix up our vruntime so that the current sleep doesn't |
| 5397 | * cause 'unlimited' sleep bonus. |
| 5398 | */ |
| 5399 | place_entity(cfs_rq, se, 0); |
| 5400 | se->vruntime -= cfs_rq->min_vruntime; |
| 5401 | } |
Paul Turner | 9ee474f | 2012-10-04 13:18:30 +0200 | [diff] [blame] | 5402 | |
| 5403 | #if defined(CONFIG_FAIR_GROUP_SCHED) && defined(CONFIG_SMP) |
| 5404 | /* |
| 5405 | * Remove our load from contribution when we leave sched_fair |
| 5406 | * and ensure we don't carry in an old decay_count if we |
| 5407 | * switch back. |
| 5408 | */ |
| 5409 | if (p->se.avg.decay_count) { |
| 5410 | struct cfs_rq *cfs_rq = cfs_rq_of(&p->se); |
| 5411 | __synchronize_entity_decay(&p->se); |
| 5412 | subtract_blocked_load_contrib(cfs_rq, |
| 5413 | p->se.avg.load_avg_contrib); |
| 5414 | } |
| 5415 | #endif |
Peter Zijlstra | da7a735 | 2011-01-17 17:03:27 +0100 | [diff] [blame] | 5416 | } |
| 5417 | |
Steven Rostedt | cb46984 | 2008-01-25 21:08:22 +0100 | [diff] [blame] | 5418 | /* |
| 5419 | * We switched to the sched_fair class. |
| 5420 | */ |
Peter Zijlstra | da7a735 | 2011-01-17 17:03:27 +0100 | [diff] [blame] | 5421 | static void switched_to_fair(struct rq *rq, struct task_struct *p) |
Steven Rostedt | cb46984 | 2008-01-25 21:08:22 +0100 | [diff] [blame] | 5422 | { |
Peter Zijlstra | da7a735 | 2011-01-17 17:03:27 +0100 | [diff] [blame] | 5423 | if (!p->se.on_rq) |
| 5424 | return; |
| 5425 | |
Steven Rostedt | cb46984 | 2008-01-25 21:08:22 +0100 | [diff] [blame] | 5426 | /* |
| 5427 | * We were most likely switched from sched_rt, so |
| 5428 | * kick off the schedule if running, otherwise just see |
| 5429 | * if we can still preempt the current task. |
| 5430 | */ |
Peter Zijlstra | da7a735 | 2011-01-17 17:03:27 +0100 | [diff] [blame] | 5431 | if (rq->curr == p) |
Steven Rostedt | cb46984 | 2008-01-25 21:08:22 +0100 | [diff] [blame] | 5432 | resched_task(rq->curr); |
| 5433 | else |
Peter Zijlstra | 15afe09 | 2008-09-20 23:38:02 +0200 | [diff] [blame] | 5434 | check_preempt_curr(rq, p, 0); |
Steven Rostedt | cb46984 | 2008-01-25 21:08:22 +0100 | [diff] [blame] | 5435 | } |
| 5436 | |
Srivatsa Vaddagiri | 83b699e | 2007-10-15 17:00:08 +0200 | [diff] [blame] | 5437 | /* Account for a task changing its policy or group. |
| 5438 | * |
| 5439 | * This routine is mostly called to set cfs_rq->curr field when a task |
| 5440 | * migrates between groups/classes. |
| 5441 | */ |
| 5442 | static void set_curr_task_fair(struct rq *rq) |
| 5443 | { |
| 5444 | struct sched_entity *se = &rq->curr->se; |
| 5445 | |
Paul Turner | ec12cb7 | 2011-07-21 09:43:30 -0700 | [diff] [blame] | 5446 | for_each_sched_entity(se) { |
| 5447 | struct cfs_rq *cfs_rq = cfs_rq_of(se); |
| 5448 | |
| 5449 | set_next_entity(cfs_rq, se); |
| 5450 | /* ensure bandwidth has been allocated on our new cfs_rq */ |
| 5451 | account_cfs_rq_runtime(cfs_rq, 0); |
| 5452 | } |
Srivatsa Vaddagiri | 83b699e | 2007-10-15 17:00:08 +0200 | [diff] [blame] | 5453 | } |
| 5454 | |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 5455 | void init_cfs_rq(struct cfs_rq *cfs_rq) |
| 5456 | { |
| 5457 | cfs_rq->tasks_timeline = RB_ROOT; |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 5458 | cfs_rq->min_vruntime = (u64)(-(1LL << 20)); |
| 5459 | #ifndef CONFIG_64BIT |
| 5460 | cfs_rq->min_vruntime_copy = cfs_rq->min_vruntime; |
| 5461 | #endif |
Paul Turner | 9ee474f | 2012-10-04 13:18:30 +0200 | [diff] [blame] | 5462 | #if defined(CONFIG_FAIR_GROUP_SCHED) && defined(CONFIG_SMP) |
| 5463 | atomic64_set(&cfs_rq->decay_counter, 1); |
Paul Turner | aff3e49 | 2012-10-04 13:18:30 +0200 | [diff] [blame] | 5464 | atomic64_set(&cfs_rq->removed_load, 0); |
Paul Turner | 9ee474f | 2012-10-04 13:18:30 +0200 | [diff] [blame] | 5465 | #endif |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 5466 | } |
| 5467 | |
Peter Zijlstra | 810b381 | 2008-02-29 15:21:01 -0500 | [diff] [blame] | 5468 | #ifdef CONFIG_FAIR_GROUP_SCHED |
Peter Zijlstra | b2b5ce0 | 2010-10-15 15:24:15 +0200 | [diff] [blame] | 5469 | 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] | 5470 | { |
Paul Turner | aff3e49 | 2012-10-04 13:18:30 +0200 | [diff] [blame] | 5471 | struct cfs_rq *cfs_rq; |
Peter Zijlstra | b2b5ce0 | 2010-10-15 15:24:15 +0200 | [diff] [blame] | 5472 | /* |
| 5473 | * If the task was not on the rq at the time of this cgroup movement |
| 5474 | * it must have been asleep, sleeping tasks keep their ->vruntime |
| 5475 | * absolute on their old rq until wakeup (needed for the fair sleeper |
| 5476 | * bonus in place_entity()). |
| 5477 | * |
| 5478 | * If it was on the rq, we've just 'preempted' it, which does convert |
| 5479 | * ->vruntime to a relative base. |
| 5480 | * |
| 5481 | * Make sure both cases convert their relative position when migrating |
| 5482 | * to another cgroup's rq. This does somewhat interfere with the |
| 5483 | * fair sleeper stuff for the first placement, but who cares. |
| 5484 | */ |
Daisuke Nishimura | 7ceff01 | 2011-12-15 14:36:07 +0900 | [diff] [blame] | 5485 | /* |
| 5486 | * When !on_rq, vruntime of the task has usually NOT been normalized. |
| 5487 | * But there are some cases where it has already been normalized: |
| 5488 | * |
| 5489 | * - Moving a forked child which is waiting for being woken up by |
| 5490 | * wake_up_new_task(). |
Daisuke Nishimura | 62af378 | 2011-12-15 14:37:41 +0900 | [diff] [blame] | 5491 | * - Moving a task which has been woken up by try_to_wake_up() and |
| 5492 | * waiting for actually being woken up by sched_ttwu_pending(). |
Daisuke Nishimura | 7ceff01 | 2011-12-15 14:36:07 +0900 | [diff] [blame] | 5493 | * |
| 5494 | * To prevent boost or penalty in the new cfs_rq caused by delta |
| 5495 | * min_vruntime between the two cfs_rqs, we skip vruntime adjustment. |
| 5496 | */ |
Daisuke Nishimura | 62af378 | 2011-12-15 14:37:41 +0900 | [diff] [blame] | 5497 | if (!on_rq && (!p->se.sum_exec_runtime || p->state == TASK_WAKING)) |
Daisuke Nishimura | 7ceff01 | 2011-12-15 14:36:07 +0900 | [diff] [blame] | 5498 | on_rq = 1; |
| 5499 | |
Peter Zijlstra | 88ec22d | 2009-12-16 18:04:41 +0100 | [diff] [blame] | 5500 | if (!on_rq) |
Peter Zijlstra | b2b5ce0 | 2010-10-15 15:24:15 +0200 | [diff] [blame] | 5501 | p->se.vruntime -= cfs_rq_of(&p->se)->min_vruntime; |
| 5502 | set_task_rq(p, task_cpu(p)); |
Paul Turner | aff3e49 | 2012-10-04 13:18:30 +0200 | [diff] [blame] | 5503 | if (!on_rq) { |
| 5504 | cfs_rq = cfs_rq_of(&p->se); |
| 5505 | p->se.vruntime += cfs_rq->min_vruntime; |
| 5506 | #ifdef CONFIG_SMP |
| 5507 | /* |
| 5508 | * migrate_task_rq_fair() will have removed our previous |
| 5509 | * contribution, but we must synchronize for ongoing future |
| 5510 | * decay. |
| 5511 | */ |
| 5512 | p->se.avg.decay_count = atomic64_read(&cfs_rq->decay_counter); |
| 5513 | cfs_rq->blocked_load_avg += p->se.avg.load_avg_contrib; |
| 5514 | #endif |
| 5515 | } |
Peter Zijlstra | 810b381 | 2008-02-29 15:21:01 -0500 | [diff] [blame] | 5516 | } |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 5517 | |
| 5518 | void free_fair_sched_group(struct task_group *tg) |
| 5519 | { |
| 5520 | int i; |
| 5521 | |
| 5522 | destroy_cfs_bandwidth(tg_cfs_bandwidth(tg)); |
| 5523 | |
| 5524 | for_each_possible_cpu(i) { |
| 5525 | if (tg->cfs_rq) |
| 5526 | kfree(tg->cfs_rq[i]); |
| 5527 | if (tg->se) |
| 5528 | kfree(tg->se[i]); |
| 5529 | } |
| 5530 | |
| 5531 | kfree(tg->cfs_rq); |
| 5532 | kfree(tg->se); |
| 5533 | } |
| 5534 | |
| 5535 | int alloc_fair_sched_group(struct task_group *tg, struct task_group *parent) |
| 5536 | { |
| 5537 | struct cfs_rq *cfs_rq; |
| 5538 | struct sched_entity *se; |
| 5539 | int i; |
| 5540 | |
| 5541 | tg->cfs_rq = kzalloc(sizeof(cfs_rq) * nr_cpu_ids, GFP_KERNEL); |
| 5542 | if (!tg->cfs_rq) |
| 5543 | goto err; |
| 5544 | tg->se = kzalloc(sizeof(se) * nr_cpu_ids, GFP_KERNEL); |
| 5545 | if (!tg->se) |
| 5546 | goto err; |
| 5547 | |
| 5548 | tg->shares = NICE_0_LOAD; |
| 5549 | |
| 5550 | init_cfs_bandwidth(tg_cfs_bandwidth(tg)); |
| 5551 | |
| 5552 | for_each_possible_cpu(i) { |
| 5553 | cfs_rq = kzalloc_node(sizeof(struct cfs_rq), |
| 5554 | GFP_KERNEL, cpu_to_node(i)); |
| 5555 | if (!cfs_rq) |
| 5556 | goto err; |
| 5557 | |
| 5558 | se = kzalloc_node(sizeof(struct sched_entity), |
| 5559 | GFP_KERNEL, cpu_to_node(i)); |
| 5560 | if (!se) |
| 5561 | goto err_free_rq; |
| 5562 | |
| 5563 | init_cfs_rq(cfs_rq); |
| 5564 | init_tg_cfs_entry(tg, cfs_rq, se, i, parent->se[i]); |
| 5565 | } |
| 5566 | |
| 5567 | return 1; |
| 5568 | |
| 5569 | err_free_rq: |
| 5570 | kfree(cfs_rq); |
| 5571 | err: |
| 5572 | return 0; |
| 5573 | } |
| 5574 | |
| 5575 | void unregister_fair_sched_group(struct task_group *tg, int cpu) |
| 5576 | { |
| 5577 | struct rq *rq = cpu_rq(cpu); |
| 5578 | unsigned long flags; |
| 5579 | |
| 5580 | /* |
| 5581 | * Only empty task groups can be destroyed; so we can speculatively |
| 5582 | * check on_list without danger of it being re-added. |
| 5583 | */ |
| 5584 | if (!tg->cfs_rq[cpu]->on_list) |
| 5585 | return; |
| 5586 | |
| 5587 | raw_spin_lock_irqsave(&rq->lock, flags); |
| 5588 | list_del_leaf_cfs_rq(tg->cfs_rq[cpu]); |
| 5589 | raw_spin_unlock_irqrestore(&rq->lock, flags); |
| 5590 | } |
| 5591 | |
| 5592 | void init_tg_cfs_entry(struct task_group *tg, struct cfs_rq *cfs_rq, |
| 5593 | struct sched_entity *se, int cpu, |
| 5594 | struct sched_entity *parent) |
| 5595 | { |
| 5596 | struct rq *rq = cpu_rq(cpu); |
| 5597 | |
| 5598 | cfs_rq->tg = tg; |
| 5599 | cfs_rq->rq = rq; |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 5600 | init_cfs_rq_runtime(cfs_rq); |
| 5601 | |
| 5602 | tg->cfs_rq[cpu] = cfs_rq; |
| 5603 | tg->se[cpu] = se; |
| 5604 | |
| 5605 | /* se could be NULL for root_task_group */ |
| 5606 | if (!se) |
| 5607 | return; |
| 5608 | |
| 5609 | if (!parent) |
| 5610 | se->cfs_rq = &rq->cfs; |
| 5611 | else |
| 5612 | se->cfs_rq = parent->my_q; |
| 5613 | |
| 5614 | se->my_q = cfs_rq; |
| 5615 | update_load_set(&se->load, 0); |
| 5616 | se->parent = parent; |
| 5617 | } |
| 5618 | |
| 5619 | static DEFINE_MUTEX(shares_mutex); |
| 5620 | |
| 5621 | int sched_group_set_shares(struct task_group *tg, unsigned long shares) |
| 5622 | { |
| 5623 | int i; |
| 5624 | unsigned long flags; |
| 5625 | |
| 5626 | /* |
| 5627 | * We can't change the weight of the root cgroup. |
| 5628 | */ |
| 5629 | if (!tg->se[0]) |
| 5630 | return -EINVAL; |
| 5631 | |
| 5632 | shares = clamp(shares, scale_load(MIN_SHARES), scale_load(MAX_SHARES)); |
| 5633 | |
| 5634 | mutex_lock(&shares_mutex); |
| 5635 | if (tg->shares == shares) |
| 5636 | goto done; |
| 5637 | |
| 5638 | tg->shares = shares; |
| 5639 | for_each_possible_cpu(i) { |
| 5640 | struct rq *rq = cpu_rq(i); |
| 5641 | struct sched_entity *se; |
| 5642 | |
| 5643 | se = tg->se[i]; |
| 5644 | /* Propagate contribution to hierarchy */ |
| 5645 | raw_spin_lock_irqsave(&rq->lock, flags); |
| 5646 | for_each_sched_entity(se) |
| 5647 | update_cfs_shares(group_cfs_rq(se)); |
| 5648 | raw_spin_unlock_irqrestore(&rq->lock, flags); |
| 5649 | } |
| 5650 | |
| 5651 | done: |
| 5652 | mutex_unlock(&shares_mutex); |
| 5653 | return 0; |
| 5654 | } |
| 5655 | #else /* CONFIG_FAIR_GROUP_SCHED */ |
| 5656 | |
| 5657 | void free_fair_sched_group(struct task_group *tg) { } |
| 5658 | |
| 5659 | int alloc_fair_sched_group(struct task_group *tg, struct task_group *parent) |
| 5660 | { |
| 5661 | return 1; |
| 5662 | } |
| 5663 | |
| 5664 | void unregister_fair_sched_group(struct task_group *tg, int cpu) { } |
| 5665 | |
| 5666 | #endif /* CONFIG_FAIR_GROUP_SCHED */ |
| 5667 | |
Peter Zijlstra | 810b381 | 2008-02-29 15:21:01 -0500 | [diff] [blame] | 5668 | |
H Hartley Sweeten | 6d686f4 | 2010-01-13 20:21:52 -0700 | [diff] [blame] | 5669 | 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] | 5670 | { |
| 5671 | struct sched_entity *se = &task->se; |
Peter Williams | 0d721ce | 2009-09-21 01:31:53 +0000 | [diff] [blame] | 5672 | unsigned int rr_interval = 0; |
| 5673 | |
| 5674 | /* |
| 5675 | * Time slice is 0 for SCHED_OTHER tasks that are on an otherwise |
| 5676 | * idle runqueue: |
| 5677 | */ |
Peter Williams | 0d721ce | 2009-09-21 01:31:53 +0000 | [diff] [blame] | 5678 | if (rq->cfs.load.weight) |
| 5679 | rr_interval = NS_TO_JIFFIES(sched_slice(&rq->cfs, se)); |
Peter Williams | 0d721ce | 2009-09-21 01:31:53 +0000 | [diff] [blame] | 5680 | |
| 5681 | return rr_interval; |
| 5682 | } |
| 5683 | |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 5684 | /* |
| 5685 | * All the scheduling class methods: |
| 5686 | */ |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 5687 | const struct sched_class fair_sched_class = { |
Ingo Molnar | 5522d5d | 2007-10-15 17:00:12 +0200 | [diff] [blame] | 5688 | .next = &idle_sched_class, |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 5689 | .enqueue_task = enqueue_task_fair, |
| 5690 | .dequeue_task = dequeue_task_fair, |
| 5691 | .yield_task = yield_task_fair, |
Mike Galbraith | d95f412 | 2011-02-01 09:50:51 -0500 | [diff] [blame] | 5692 | .yield_to_task = yield_to_task_fair, |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 5693 | |
Ingo Molnar | 2e09bf5 | 2007-10-15 17:00:05 +0200 | [diff] [blame] | 5694 | .check_preempt_curr = check_preempt_wakeup, |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 5695 | |
| 5696 | .pick_next_task = pick_next_task_fair, |
| 5697 | .put_prev_task = put_prev_task_fair, |
| 5698 | |
Peter Williams | 681f3e6 | 2007-10-24 18:23:51 +0200 | [diff] [blame] | 5699 | #ifdef CONFIG_SMP |
Li Zefan | 4ce72a2 | 2008-10-22 15:25:26 +0800 | [diff] [blame] | 5700 | .select_task_rq = select_task_rq_fair, |
Paul Turner | 0a74bef | 2012-10-04 13:18:30 +0200 | [diff] [blame] | 5701 | .migrate_task_rq = migrate_task_rq_fair, |
Li Zefan | 4ce72a2 | 2008-10-22 15:25:26 +0800 | [diff] [blame] | 5702 | |
Christian Ehrhardt | 0bcdcf2 | 2009-11-30 12:16:46 +0100 | [diff] [blame] | 5703 | .rq_online = rq_online_fair, |
| 5704 | .rq_offline = rq_offline_fair, |
Peter Zijlstra | 88ec22d | 2009-12-16 18:04:41 +0100 | [diff] [blame] | 5705 | |
| 5706 | .task_waking = task_waking_fair, |
Peter Williams | 681f3e6 | 2007-10-24 18:23:51 +0200 | [diff] [blame] | 5707 | #endif |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 5708 | |
Srivatsa Vaddagiri | 83b699e | 2007-10-15 17:00:08 +0200 | [diff] [blame] | 5709 | .set_curr_task = set_curr_task_fair, |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 5710 | .task_tick = task_tick_fair, |
Peter Zijlstra | cd29fe6 | 2009-11-27 17:32:46 +0100 | [diff] [blame] | 5711 | .task_fork = task_fork_fair, |
Steven Rostedt | cb46984 | 2008-01-25 21:08:22 +0100 | [diff] [blame] | 5712 | |
| 5713 | .prio_changed = prio_changed_fair, |
Peter Zijlstra | da7a735 | 2011-01-17 17:03:27 +0100 | [diff] [blame] | 5714 | .switched_from = switched_from_fair, |
Steven Rostedt | cb46984 | 2008-01-25 21:08:22 +0100 | [diff] [blame] | 5715 | .switched_to = switched_to_fair, |
Peter Zijlstra | 810b381 | 2008-02-29 15:21:01 -0500 | [diff] [blame] | 5716 | |
Peter Williams | 0d721ce | 2009-09-21 01:31:53 +0000 | [diff] [blame] | 5717 | .get_rr_interval = get_rr_interval_fair, |
| 5718 | |
Peter Zijlstra | 810b381 | 2008-02-29 15:21:01 -0500 | [diff] [blame] | 5719 | #ifdef CONFIG_FAIR_GROUP_SCHED |
Peter Zijlstra | b2b5ce0 | 2010-10-15 15:24:15 +0200 | [diff] [blame] | 5720 | .task_move_group = task_move_group_fair, |
Peter Zijlstra | 810b381 | 2008-02-29 15:21:01 -0500 | [diff] [blame] | 5721 | #endif |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 5722 | }; |
| 5723 | |
| 5724 | #ifdef CONFIG_SCHED_DEBUG |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 5725 | void print_cfs_stats(struct seq_file *m, int cpu) |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 5726 | { |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 5727 | struct cfs_rq *cfs_rq; |
| 5728 | |
Peter Zijlstra | 5973e5b | 2008-01-25 21:08:34 +0100 | [diff] [blame] | 5729 | rcu_read_lock(); |
Ingo Molnar | c3b64f1 | 2007-08-09 11:16:51 +0200 | [diff] [blame] | 5730 | for_each_leaf_cfs_rq(cpu_rq(cpu), cfs_rq) |
Ingo Molnar | 5cef9ec | 2007-08-09 11:16:47 +0200 | [diff] [blame] | 5731 | print_cfs_rq(m, cpu, cfs_rq); |
Peter Zijlstra | 5973e5b | 2008-01-25 21:08:34 +0100 | [diff] [blame] | 5732 | rcu_read_unlock(); |
Ingo Molnar | bf0f6f2 | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 5733 | } |
| 5734 | #endif |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 5735 | |
| 5736 | __init void init_sched_fair_class(void) |
| 5737 | { |
| 5738 | #ifdef CONFIG_SMP |
| 5739 | open_softirq(SCHED_SOFTIRQ, run_rebalance_domains); |
| 5740 | |
| 5741 | #ifdef CONFIG_NO_HZ |
Diwakar Tundlam | 554ceca | 2012-03-07 14:44:26 -0800 | [diff] [blame] | 5742 | nohz.next_balance = jiffies; |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 5743 | zalloc_cpumask_var(&nohz.idle_cpus_mask, GFP_NOWAIT); |
Suresh Siddha | 7132596 | 2012-01-19 18:28:57 -0800 | [diff] [blame] | 5744 | cpu_notifier(sched_ilb_notifier, 0); |
Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 5745 | #endif |
| 5746 | #endif /* SMP */ |
| 5747 | |
| 5748 | } |