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