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