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